1 /******************************************************************************
2 *******************************************************************************
4 ** Copyright (C) 2005-2008 Red Hat, Inc. All rights reserved.
6 ** This copyrighted material is made available to anyone wishing to use,
7 ** modify, copy, or redistribute it subject to the terms and conditions
8 ** of the GNU General Public License v.2.
10 *******************************************************************************
11 ******************************************************************************/
13 #include <linux/pagemap.h>
14 #include <linux/seq_file.h>
15 #include <linux/module.h>
16 #include <linux/ctype.h>
17 #include <linux/debugfs.h>
19 #include "dlm_internal.h"
22 #define DLM_DEBUG_BUF_LEN 4096
23 static char debug_buf[DLM_DEBUG_BUF_LEN];
24 static struct mutex debug_buf_lock;
26 static struct dentry *dlm_root;
33 struct list_head *next;
38 * dump all rsb's in the lockspace hash table
41 static char *print_lockmode(int mode)
63 static void print_format1_lock(struct seq_file *s, struct dlm_lkb *lkb,
66 seq_printf(s, "%08x %s", lkb->lkb_id, print_lockmode(lkb->lkb_grmode));
68 if (lkb->lkb_status == DLM_LKSTS_CONVERT
69 || lkb->lkb_status == DLM_LKSTS_WAITING)
70 seq_printf(s, " (%s)", print_lockmode(lkb->lkb_rqmode));
72 if (lkb->lkb_nodeid) {
73 if (lkb->lkb_nodeid != res->res_nodeid)
74 seq_printf(s, " Remote: %3d %08x", lkb->lkb_nodeid,
77 seq_printf(s, " Master: %08x", lkb->lkb_remid);
80 if (lkb->lkb_wait_type)
81 seq_printf(s, " wait_type: %d", lkb->lkb_wait_type);
86 static int print_format1(struct dlm_rsb *res, struct seq_file *s)
89 int i, lvblen = res->res_ls->ls_lvblen, recover_list, root_list;
93 seq_printf(s, "\nResource %p Name (len=%d) \"", res, res->res_length);
94 for (i = 0; i < res->res_length; i++) {
95 if (isprint(res->res_name[i]))
96 seq_printf(s, "%c", res->res_name[i]);
98 seq_printf(s, "%c", '.');
100 if (res->res_nodeid > 0)
101 seq_printf(s, "\" \nLocal Copy, Master is node %d\n",
103 else if (res->res_nodeid == 0)
104 seq_printf(s, "\" \nMaster Copy\n");
105 else if (res->res_nodeid == -1)
106 seq_printf(s, "\" \nLooking up master (lkid %x)\n",
107 res->res_first_lkid);
109 seq_printf(s, "\" \nInvalid master %d\n", res->res_nodeid);
112 if (res->res_lvbptr) {
113 seq_printf(s, "LVB: ");
114 for (i = 0; i < lvblen; i++) {
116 seq_printf(s, "\n ");
117 seq_printf(s, "%02x ",
118 (unsigned char) res->res_lvbptr[i]);
120 if (rsb_flag(res, RSB_VALNOTVALID))
121 seq_printf(s, " (INVALID)");
125 root_list = !list_empty(&res->res_root_list);
126 recover_list = !list_empty(&res->res_recover_list);
128 if (root_list || recover_list) {
129 seq_printf(s, "Recovery: root %d recover %d flags %lx "
130 "count %d\n", root_list, recover_list,
131 res->res_flags, res->res_recover_locks_count);
134 /* Print the locks attached to this resource */
135 seq_printf(s, "Granted Queue\n");
136 list_for_each_entry(lkb, &res->res_grantqueue, lkb_statequeue)
137 print_format1_lock(s, lkb, res);
139 seq_printf(s, "Conversion Queue\n");
140 list_for_each_entry(lkb, &res->res_convertqueue, lkb_statequeue)
141 print_format1_lock(s, lkb, res);
143 seq_printf(s, "Waiting Queue\n");
144 list_for_each_entry(lkb, &res->res_waitqueue, lkb_statequeue)
145 print_format1_lock(s, lkb, res);
147 if (list_empty(&res->res_lookup))
150 seq_printf(s, "Lookup Queue\n");
151 list_for_each_entry(lkb, &res->res_lookup, lkb_rsb_lookup) {
152 seq_printf(s, "%08x %s", lkb->lkb_id,
153 print_lockmode(lkb->lkb_rqmode));
154 if (lkb->lkb_wait_type)
155 seq_printf(s, " wait_type: %d", lkb->lkb_wait_type);
163 static void print_format2_lock(struct seq_file *s, struct dlm_lkb *lkb,
169 if (lkb->lkb_flags & DLM_IFL_USER) {
171 xid = lkb->lkb_ua->xid;
174 /* microseconds since lkb was added to current queue */
175 us = ktime_to_us(ktime_sub(ktime_get(), lkb->lkb_timestamp));
177 /* id nodeid remid pid xid exflags flags sts grmode rqmode time_us
178 r_nodeid r_len r_name */
180 seq_printf(s, "%x %d %x %u %llu %x %x %d %d %d %llu %u %d \"%s\"\n",
185 (unsigned long long)xid,
191 (unsigned long long)us,
197 static int print_format2(struct dlm_rsb *r, struct seq_file *s)
203 list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue)
204 print_format2_lock(s, lkb, r);
206 list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue)
207 print_format2_lock(s, lkb, r);
209 list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue)
210 print_format2_lock(s, lkb, r);
216 static void print_format3_lock(struct seq_file *s, struct dlm_lkb *lkb,
221 if (lkb->lkb_flags & DLM_IFL_USER) {
223 xid = lkb->lkb_ua->xid;
226 seq_printf(s, "lkb %x %d %x %u %llu %x %x %d %d %d %d %d %d %u %llu %llu\n",
231 (unsigned long long)xid,
241 (unsigned long long)ktime_to_ns(lkb->lkb_timestamp),
242 (unsigned long long)ktime_to_ns(lkb->lkb_time_bast));
245 static int print_format3(struct dlm_rsb *r, struct seq_file *s)
248 int i, lvblen = r->res_ls->ls_lvblen;
253 seq_printf(s, "rsb %p %d %x %lx %d %d %u %d ",
258 !list_empty(&r->res_root_list),
259 !list_empty(&r->res_recover_list),
260 r->res_recover_locks_count,
263 for (i = 0; i < r->res_length; i++) {
264 if (!isascii(r->res_name[i]) || !isprint(r->res_name[i]))
268 seq_printf(s, "%s", print_name ? "str " : "hex");
270 for (i = 0; i < r->res_length; i++) {
272 seq_printf(s, "%c", r->res_name[i]);
274 seq_printf(s, " %02x", (unsigned char)r->res_name[i]);
281 seq_printf(s, "lvb %u %d", r->res_lvbseq, lvblen);
283 for (i = 0; i < lvblen; i++)
284 seq_printf(s, " %02x", (unsigned char)r->res_lvbptr[i]);
288 list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue)
289 print_format3_lock(s, lkb, 0);
291 list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue)
292 print_format3_lock(s, lkb, 0);
294 list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue)
295 print_format3_lock(s, lkb, 0);
297 list_for_each_entry(lkb, &r->res_lookup, lkb_rsb_lookup)
298 print_format3_lock(s, lkb, 1);
304 static int rsb_iter_next(struct rsb_iter *ri)
306 struct dlm_ls *ls = ri->ls;
311 /* Find the next non-empty hash bucket */
312 for (i = ri->entry; i < ls->ls_rsbtbl_size; i++) {
313 read_lock(&ls->ls_rsbtbl[i].lock);
314 if (!list_empty(&ls->ls_rsbtbl[i].list)) {
315 ri->next = ls->ls_rsbtbl[i].list.next;
316 ri->rsb = list_entry(ri->next, struct dlm_rsb,
318 dlm_hold_rsb(ri->rsb);
319 read_unlock(&ls->ls_rsbtbl[i].lock);
322 read_unlock(&ls->ls_rsbtbl[i].lock);
326 if (ri->entry >= ls->ls_rsbtbl_size)
329 struct dlm_rsb *old = ri->rsb;
331 read_lock(&ls->ls_rsbtbl[i].lock);
332 ri->next = ri->next->next;
333 if (ri->next->next == ls->ls_rsbtbl[i].list.next) {
334 /* End of list - move to next bucket */
337 read_unlock(&ls->ls_rsbtbl[i].lock);
341 ri->rsb = list_entry(ri->next, struct dlm_rsb, res_hashchain);
342 dlm_hold_rsb(ri->rsb);
343 read_unlock(&ls->ls_rsbtbl[i].lock);
350 static void rsb_iter_free(struct rsb_iter *ri)
355 static struct rsb_iter *rsb_iter_init(struct dlm_ls *ls)
359 ri = kzalloc(sizeof *ri, GFP_KERNEL);
368 if (rsb_iter_next(ri)) {
376 static void *rsb_seq_start(struct seq_file *file, loff_t *pos)
381 ri = rsb_iter_init(file->private);
386 if (rsb_iter_next(ri)) {
395 static void *rsb_seq_next(struct seq_file *file, void *iter_ptr, loff_t *pos)
397 struct rsb_iter *ri = iter_ptr;
401 if (rsb_iter_next(ri)) {
409 static void rsb_seq_stop(struct seq_file *file, void *iter_ptr)
411 /* nothing for now */
414 static int rsb_seq_show(struct seq_file *file, void *iter_ptr)
416 struct rsb_iter *ri = iter_ptr;
418 switch (ri->format) {
420 print_format1(ri->rsb, file);
424 seq_printf(file, "id nodeid remid pid xid exflags "
425 "flags sts grmode rqmode time_ms "
426 "r_nodeid r_len r_name\n");
429 print_format2(ri->rsb, file);
433 seq_printf(file, "version rsb 1.1 lvb 1.1 lkb 1.1\n");
436 print_format3(ri->rsb, file);
443 static struct seq_operations rsb_seq_ops = {
444 .start = rsb_seq_start,
445 .next = rsb_seq_next,
446 .stop = rsb_seq_stop,
447 .show = rsb_seq_show,
450 static int rsb_open(struct inode *inode, struct file *file)
452 struct seq_file *seq;
455 ret = seq_open(file, &rsb_seq_ops);
459 seq = file->private_data;
460 seq->private = inode->i_private;
465 static const struct file_operations rsb_fops = {
466 .owner = THIS_MODULE,
470 .release = seq_release
474 * Dump state in compact per-lock listing
477 static struct rsb_iter *locks_iter_init(struct dlm_ls *ls, loff_t *pos)
481 ri = kzalloc(sizeof *ri, GFP_KERNEL);
493 if (rsb_iter_next(ri)) {
501 static void *locks_seq_start(struct seq_file *file, loff_t *pos)
506 ri = locks_iter_init(file->private, pos);
511 if (rsb_iter_next(ri)) {
520 static struct seq_operations locks_seq_ops = {
521 .start = locks_seq_start,
522 .next = rsb_seq_next,
523 .stop = rsb_seq_stop,
524 .show = rsb_seq_show,
527 static int locks_open(struct inode *inode, struct file *file)
529 struct seq_file *seq;
532 ret = seq_open(file, &locks_seq_ops);
536 seq = file->private_data;
537 seq->private = inode->i_private;
542 static const struct file_operations locks_fops = {
543 .owner = THIS_MODULE,
547 .release = seq_release
551 * Dump all rsb/lvb/lkb state in compact listing, more complete than _locks
552 * This can replace both formats 1 and 2 eventually.
555 static struct rsb_iter *all_iter_init(struct dlm_ls *ls, loff_t *pos)
559 ri = kzalloc(sizeof *ri, GFP_KERNEL);
571 if (rsb_iter_next(ri)) {
579 static void *all_seq_start(struct seq_file *file, loff_t *pos)
584 ri = all_iter_init(file->private, pos);
589 if (rsb_iter_next(ri)) {
598 static struct seq_operations all_seq_ops = {
599 .start = all_seq_start,
600 .next = rsb_seq_next,
601 .stop = rsb_seq_stop,
602 .show = rsb_seq_show,
605 static int all_open(struct inode *inode, struct file *file)
607 struct seq_file *seq;
610 ret = seq_open(file, &all_seq_ops);
614 seq = file->private_data;
615 seq->private = inode->i_private;
620 static const struct file_operations all_fops = {
621 .owner = THIS_MODULE,
625 .release = seq_release
629 * dump lkb's on the ls_waiters list
632 static int waiters_open(struct inode *inode, struct file *file)
634 file->private_data = inode->i_private;
638 static ssize_t waiters_read(struct file *file, char __user *userbuf,
639 size_t count, loff_t *ppos)
641 struct dlm_ls *ls = file->private_data;
643 size_t len = DLM_DEBUG_BUF_LEN, pos = 0, ret, rv;
645 mutex_lock(&debug_buf_lock);
646 mutex_lock(&ls->ls_waiters_mutex);
647 memset(debug_buf, 0, sizeof(debug_buf));
649 list_for_each_entry(lkb, &ls->ls_waiters, lkb_wait_reply) {
650 ret = snprintf(debug_buf + pos, len - pos, "%x %d %d %s\n",
651 lkb->lkb_id, lkb->lkb_wait_type,
652 lkb->lkb_nodeid, lkb->lkb_resource->res_name);
653 if (ret >= len - pos)
657 mutex_unlock(&ls->ls_waiters_mutex);
659 rv = simple_read_from_buffer(userbuf, count, ppos, debug_buf, pos);
660 mutex_unlock(&debug_buf_lock);
664 static const struct file_operations waiters_fops = {
665 .owner = THIS_MODULE,
666 .open = waiters_open,
670 void dlm_delete_debug_file(struct dlm_ls *ls)
672 if (ls->ls_debug_rsb_dentry)
673 debugfs_remove(ls->ls_debug_rsb_dentry);
674 if (ls->ls_debug_waiters_dentry)
675 debugfs_remove(ls->ls_debug_waiters_dentry);
676 if (ls->ls_debug_locks_dentry)
677 debugfs_remove(ls->ls_debug_locks_dentry);
678 if (ls->ls_debug_all_dentry)
679 debugfs_remove(ls->ls_debug_all_dentry);
682 int dlm_create_debug_file(struct dlm_ls *ls)
684 char name[DLM_LOCKSPACE_LEN+8];
688 ls->ls_debug_rsb_dentry = debugfs_create_file(ls->ls_name,
693 if (!ls->ls_debug_rsb_dentry)
698 memset(name, 0, sizeof(name));
699 snprintf(name, DLM_LOCKSPACE_LEN+8, "%s_locks", ls->ls_name);
701 ls->ls_debug_locks_dentry = debugfs_create_file(name,
706 if (!ls->ls_debug_locks_dentry)
711 memset(name, 0, sizeof(name));
712 snprintf(name, DLM_LOCKSPACE_LEN+8, "%s_all", ls->ls_name);
714 ls->ls_debug_all_dentry = debugfs_create_file(name,
719 if (!ls->ls_debug_all_dentry)
722 memset(name, 0, sizeof(name));
723 snprintf(name, DLM_LOCKSPACE_LEN+8, "%s_waiters", ls->ls_name);
725 ls->ls_debug_waiters_dentry = debugfs_create_file(name,
730 if (!ls->ls_debug_waiters_dentry)
736 dlm_delete_debug_file(ls);
740 int __init dlm_register_debugfs(void)
742 mutex_init(&debug_buf_lock);
743 dlm_root = debugfs_create_dir("dlm", NULL);
744 return dlm_root ? 0 : -ENOMEM;
747 void dlm_unregister_debugfs(void)
749 debugfs_remove(dlm_root);