1 /******************************************************************************
2 *******************************************************************************
4 ** Copyright (C) 2005-2009 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>
18 #include <linux/slab.h>
20 #include "dlm_internal.h"
23 #define DLM_DEBUG_BUF_LEN 4096
24 static char debug_buf
[DLM_DEBUG_BUF_LEN
];
25 static struct mutex debug_buf_lock
;
27 static struct dentry
*dlm_root
;
29 static char *print_lockmode(int mode
)
51 static int print_format1_lock(struct seq_file
*s
, struct dlm_lkb
*lkb
,
54 seq_printf(s
, "%08x %s", lkb
->lkb_id
, print_lockmode(lkb
->lkb_grmode
));
56 if (lkb
->lkb_status
== DLM_LKSTS_CONVERT
||
57 lkb
->lkb_status
== DLM_LKSTS_WAITING
)
58 seq_printf(s
, " (%s)", print_lockmode(lkb
->lkb_rqmode
));
60 if (lkb
->lkb_nodeid
) {
61 if (lkb
->lkb_nodeid
!= res
->res_nodeid
)
62 seq_printf(s
, " Remote: %3d %08x", lkb
->lkb_nodeid
,
65 seq_printf(s
, " Master: %08x", lkb
->lkb_remid
);
68 if (lkb
->lkb_wait_type
)
69 seq_printf(s
, " wait_type: %d", lkb
->lkb_wait_type
);
71 return seq_puts(s
, "\n");
74 static int print_format1(struct dlm_rsb
*res
, struct seq_file
*s
)
77 int i
, lvblen
= res
->res_ls
->ls_lvblen
, recover_list
, root_list
;
82 rv
= seq_printf(s
, "\nResource %p Name (len=%d) \"",
83 res
, res
->res_length
);
87 for (i
= 0; i
< res
->res_length
; i
++) {
88 if (isprint(res
->res_name
[i
]))
89 seq_printf(s
, "%c", res
->res_name
[i
]);
91 seq_printf(s
, "%c", '.');
94 if (res
->res_nodeid
> 0)
95 rv
= seq_printf(s
, "\"\nLocal Copy, Master is node %d\n",
97 else if (res
->res_nodeid
== 0)
98 rv
= seq_puts(s
, "\"\nMaster Copy\n");
99 else if (res
->res_nodeid
== -1)
100 rv
= seq_printf(s
, "\"\nLooking up master (lkid %x)\n",
101 res
->res_first_lkid
);
103 rv
= seq_printf(s
, "\"\nInvalid master %d\n",
109 if (res
->res_lvbptr
) {
110 seq_puts(s
, "LVB: ");
111 for (i
= 0; i
< lvblen
; i
++) {
114 seq_printf(s
, "%02x ",
115 (unsigned char) res
->res_lvbptr
[i
]);
117 if (rsb_flag(res
, RSB_VALNOTVALID
))
118 seq_puts(s
, " (INVALID)");
119 rv
= seq_puts(s
, "\n");
124 root_list
= !list_empty(&res
->res_root_list
);
125 recover_list
= !list_empty(&res
->res_recover_list
);
127 if (root_list
|| recover_list
) {
128 rv
= seq_printf(s
, "Recovery: root %d recover %d flags %lx "
129 "count %d\n", root_list
, recover_list
,
130 res
->res_flags
, res
->res_recover_locks_count
);
135 /* Print the locks attached to this resource */
136 seq_puts(s
, "Granted Queue\n");
137 list_for_each_entry(lkb
, &res
->res_grantqueue
, lkb_statequeue
) {
138 rv
= print_format1_lock(s
, lkb
, res
);
143 seq_puts(s
, "Conversion Queue\n");
144 list_for_each_entry(lkb
, &res
->res_convertqueue
, lkb_statequeue
) {
145 rv
= print_format1_lock(s
, lkb
, res
);
150 seq_puts(s
, "Waiting Queue\n");
151 list_for_each_entry(lkb
, &res
->res_waitqueue
, lkb_statequeue
) {
152 rv
= print_format1_lock(s
, lkb
, res
);
157 if (list_empty(&res
->res_lookup
))
160 seq_puts(s
, "Lookup Queue\n");
161 list_for_each_entry(lkb
, &res
->res_lookup
, lkb_rsb_lookup
) {
162 rv
= seq_printf(s
, "%08x %s", lkb
->lkb_id
,
163 print_lockmode(lkb
->lkb_rqmode
));
164 if (lkb
->lkb_wait_type
)
165 seq_printf(s
, " wait_type: %d", lkb
->lkb_wait_type
);
166 rv
= seq_puts(s
, "\n");
173 static int print_format2_lock(struct seq_file
*s
, struct dlm_lkb
*lkb
,
180 if (lkb
->lkb_flags
& DLM_IFL_USER
) {
182 xid
= lkb
->lkb_ua
->xid
;
185 /* microseconds since lkb was added to current queue */
186 us
= ktime_to_us(ktime_sub(ktime_get(), lkb
->lkb_timestamp
));
188 /* id nodeid remid pid xid exflags flags sts grmode rqmode time_us
189 r_nodeid r_len r_name */
191 rv
= seq_printf(s
, "%x %d %x %u %llu %x %x %d %d %d %llu %u %d \"%s\"\n",
196 (unsigned long long)xid
,
202 (unsigned long long)us
,
209 static int print_format2(struct dlm_rsb
*r
, struct seq_file
*s
)
216 list_for_each_entry(lkb
, &r
->res_grantqueue
, lkb_statequeue
) {
217 rv
= print_format2_lock(s
, lkb
, r
);
222 list_for_each_entry(lkb
, &r
->res_convertqueue
, lkb_statequeue
) {
223 rv
= print_format2_lock(s
, lkb
, r
);
228 list_for_each_entry(lkb
, &r
->res_waitqueue
, lkb_statequeue
) {
229 rv
= print_format2_lock(s
, lkb
, r
);
238 static int print_format3_lock(struct seq_file
*s
, struct dlm_lkb
*lkb
,
244 if (lkb
->lkb_flags
& DLM_IFL_USER
) {
246 xid
= lkb
->lkb_ua
->xid
;
249 rv
= seq_printf(s
, "lkb %x %d %x %u %llu %x %x %d %d %d %d %d %d %u %llu %llu\n",
254 (unsigned long long)xid
,
260 lkb
->lkb_last_bast
.mode
,
264 (unsigned long long)ktime_to_ns(lkb
->lkb_timestamp
),
265 (unsigned long long)ktime_to_ns(lkb
->lkb_last_bast_time
));
269 static int print_format3(struct dlm_rsb
*r
, struct seq_file
*s
)
272 int i
, lvblen
= r
->res_ls
->ls_lvblen
;
278 rv
= seq_printf(s
, "rsb %p %d %x %lx %d %d %u %d ",
283 !list_empty(&r
->res_root_list
),
284 !list_empty(&r
->res_recover_list
),
285 r
->res_recover_locks_count
,
290 for (i
= 0; i
< r
->res_length
; i
++) {
291 if (!isascii(r
->res_name
[i
]) || !isprint(r
->res_name
[i
]))
295 seq_printf(s
, "%s", print_name
? "str " : "hex");
297 for (i
= 0; i
< r
->res_length
; i
++) {
299 seq_printf(s
, "%c", r
->res_name
[i
]);
301 seq_printf(s
, " %02x", (unsigned char)r
->res_name
[i
]);
303 rv
= seq_puts(s
, "\n");
310 seq_printf(s
, "lvb %u %d", r
->res_lvbseq
, lvblen
);
312 for (i
= 0; i
< lvblen
; i
++)
313 seq_printf(s
, " %02x", (unsigned char)r
->res_lvbptr
[i
]);
314 rv
= seq_puts(s
, "\n");
319 list_for_each_entry(lkb
, &r
->res_grantqueue
, lkb_statequeue
) {
320 rv
= print_format3_lock(s
, lkb
, 0);
325 list_for_each_entry(lkb
, &r
->res_convertqueue
, lkb_statequeue
) {
326 rv
= print_format3_lock(s
, lkb
, 0);
331 list_for_each_entry(lkb
, &r
->res_waitqueue
, lkb_statequeue
) {
332 rv
= print_format3_lock(s
, lkb
, 0);
337 list_for_each_entry(lkb
, &r
->res_lookup
, lkb_rsb_lookup
) {
338 rv
= print_format3_lock(s
, lkb
, 1);
347 static int print_format4(struct dlm_rsb
*r
, struct seq_file
*s
)
349 int our_nodeid
= dlm_our_nodeid();
355 rv
= seq_printf(s
, "rsb %p %d %d %d %d %lu %lx %d ",
358 r
->res_master_nodeid
,
367 for (i
= 0; i
< r
->res_length
; i
++) {
368 if (!isascii(r
->res_name
[i
]) || !isprint(r
->res_name
[i
]))
372 seq_printf(s
, "%s", print_name
? "str " : "hex");
374 for (i
= 0; i
< r
->res_length
; i
++) {
376 seq_printf(s
, "%c", r
->res_name
[i
]);
378 seq_printf(s
, " %02x", (unsigned char)r
->res_name
[i
]);
380 rv
= seq_puts(s
, "\n");
393 /* seq_printf returns -1 if the buffer is full, and 0 otherwise.
394 If the buffer is full, seq_printf can be called again, but it
395 does nothing and just returns -1. So, the these printing routines
396 periodically check the return value to avoid wasting too much time
397 trying to print to a full buffer. */
399 static int table_seq_show(struct seq_file
*seq
, void *iter_ptr
)
401 struct rsbtbl_iter
*ri
= iter_ptr
;
404 switch (ri
->format
) {
406 rv
= print_format1(ri
->rsb
, seq
);
410 seq_printf(seq
, "id nodeid remid pid xid exflags "
411 "flags sts grmode rqmode time_ms "
412 "r_nodeid r_len r_name\n");
415 rv
= print_format2(ri
->rsb
, seq
);
419 seq_printf(seq
, "version rsb 1.1 lvb 1.1 lkb 1.1\n");
422 rv
= print_format3(ri
->rsb
, seq
);
426 seq_printf(seq
, "version 4 rsb 2\n");
429 rv
= print_format4(ri
->rsb
, seq
);
436 static const struct seq_operations format1_seq_ops
;
437 static const struct seq_operations format2_seq_ops
;
438 static const struct seq_operations format3_seq_ops
;
439 static const struct seq_operations format4_seq_ops
;
441 static void *table_seq_start(struct seq_file
*seq
, loff_t
*pos
)
443 struct rb_root
*tree
;
444 struct rb_node
*node
;
445 struct dlm_ls
*ls
= seq
->private;
446 struct rsbtbl_iter
*ri
;
449 unsigned bucket
, entry
;
450 int toss
= (seq
->op
== &format4_seq_ops
);
453 entry
= n
& ((1LL << 32) - 1);
455 if (bucket
>= ls
->ls_rsbtbl_size
)
458 ri
= kzalloc(sizeof(struct rsbtbl_iter
), GFP_NOFS
);
463 if (seq
->op
== &format1_seq_ops
)
465 if (seq
->op
== &format2_seq_ops
)
467 if (seq
->op
== &format3_seq_ops
)
469 if (seq
->op
== &format4_seq_ops
)
472 tree
= toss
? &ls
->ls_rsbtbl
[bucket
].toss
: &ls
->ls_rsbtbl
[bucket
].keep
;
474 spin_lock(&ls
->ls_rsbtbl
[bucket
].lock
);
475 if (!RB_EMPTY_ROOT(tree
)) {
476 for (node
= rb_first(tree
); node
; node
= rb_next(node
)) {
477 r
= rb_entry(node
, struct dlm_rsb
, res_hashnode
);
482 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
487 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
490 * move to the first rsb in the next non-empty bucket
494 n
&= ~((1LL << 32) - 1);
500 if (bucket
>= ls
->ls_rsbtbl_size
) {
504 tree
= toss
? &ls
->ls_rsbtbl
[bucket
].toss
: &ls
->ls_rsbtbl
[bucket
].keep
;
506 spin_lock(&ls
->ls_rsbtbl
[bucket
].lock
);
507 if (!RB_EMPTY_ROOT(tree
)) {
508 node
= rb_first(tree
);
509 r
= rb_entry(node
, struct dlm_rsb
, res_hashnode
);
513 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
517 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
521 static void *table_seq_next(struct seq_file
*seq
, void *iter_ptr
, loff_t
*pos
)
523 struct dlm_ls
*ls
= seq
->private;
524 struct rsbtbl_iter
*ri
= iter_ptr
;
525 struct rb_root
*tree
;
526 struct rb_node
*next
;
527 struct dlm_rsb
*r
, *rp
;
530 int toss
= (seq
->op
== &format4_seq_ops
);
535 * move to the next rsb in the same bucket
538 spin_lock(&ls
->ls_rsbtbl
[bucket
].lock
);
540 next
= rb_next(&rp
->res_hashnode
);
543 r
= rb_entry(next
, struct dlm_rsb
, res_hashnode
);
546 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
551 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
555 * move to the first rsb in the next non-empty bucket
559 n
&= ~((1LL << 32) - 1);
565 if (bucket
>= ls
->ls_rsbtbl_size
) {
569 tree
= toss
? &ls
->ls_rsbtbl
[bucket
].toss
: &ls
->ls_rsbtbl
[bucket
].keep
;
571 spin_lock(&ls
->ls_rsbtbl
[bucket
].lock
);
572 if (!RB_EMPTY_ROOT(tree
)) {
573 next
= rb_first(tree
);
574 r
= rb_entry(next
, struct dlm_rsb
, res_hashnode
);
578 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
582 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
586 static void table_seq_stop(struct seq_file
*seq
, void *iter_ptr
)
588 struct rsbtbl_iter
*ri
= iter_ptr
;
591 dlm_put_rsb(ri
->rsb
);
596 static const struct seq_operations format1_seq_ops
= {
597 .start
= table_seq_start
,
598 .next
= table_seq_next
,
599 .stop
= table_seq_stop
,
600 .show
= table_seq_show
,
603 static const struct seq_operations format2_seq_ops
= {
604 .start
= table_seq_start
,
605 .next
= table_seq_next
,
606 .stop
= table_seq_stop
,
607 .show
= table_seq_show
,
610 static const struct seq_operations format3_seq_ops
= {
611 .start
= table_seq_start
,
612 .next
= table_seq_next
,
613 .stop
= table_seq_stop
,
614 .show
= table_seq_show
,
617 static const struct seq_operations format4_seq_ops
= {
618 .start
= table_seq_start
,
619 .next
= table_seq_next
,
620 .stop
= table_seq_stop
,
621 .show
= table_seq_show
,
624 static const struct file_operations format1_fops
;
625 static const struct file_operations format2_fops
;
626 static const struct file_operations format3_fops
;
627 static const struct file_operations format4_fops
;
629 static int table_open(struct inode
*inode
, struct file
*file
)
631 struct seq_file
*seq
;
634 if (file
->f_op
== &format1_fops
)
635 ret
= seq_open(file
, &format1_seq_ops
);
636 else if (file
->f_op
== &format2_fops
)
637 ret
= seq_open(file
, &format2_seq_ops
);
638 else if (file
->f_op
== &format3_fops
)
639 ret
= seq_open(file
, &format3_seq_ops
);
640 else if (file
->f_op
== &format4_fops
)
641 ret
= seq_open(file
, &format4_seq_ops
);
646 seq
= file
->private_data
;
647 seq
->private = inode
->i_private
; /* the dlm_ls */
651 static const struct file_operations format1_fops
= {
652 .owner
= THIS_MODULE
,
656 .release
= seq_release
659 static const struct file_operations format2_fops
= {
660 .owner
= THIS_MODULE
,
664 .release
= seq_release
667 static const struct file_operations format3_fops
= {
668 .owner
= THIS_MODULE
,
672 .release
= seq_release
675 static const struct file_operations format4_fops
= {
676 .owner
= THIS_MODULE
,
680 .release
= seq_release
684 * dump lkb's on the ls_waiters list
686 static ssize_t
waiters_read(struct file
*file
, char __user
*userbuf
,
687 size_t count
, loff_t
*ppos
)
689 struct dlm_ls
*ls
= file
->private_data
;
691 size_t len
= DLM_DEBUG_BUF_LEN
, pos
= 0, ret
, rv
;
693 mutex_lock(&debug_buf_lock
);
694 mutex_lock(&ls
->ls_waiters_mutex
);
695 memset(debug_buf
, 0, sizeof(debug_buf
));
697 list_for_each_entry(lkb
, &ls
->ls_waiters
, lkb_wait_reply
) {
698 ret
= snprintf(debug_buf
+ pos
, len
- pos
, "%x %d %d %s\n",
699 lkb
->lkb_id
, lkb
->lkb_wait_type
,
700 lkb
->lkb_nodeid
, lkb
->lkb_resource
->res_name
);
701 if (ret
>= len
- pos
)
705 mutex_unlock(&ls
->ls_waiters_mutex
);
707 rv
= simple_read_from_buffer(userbuf
, count
, ppos
, debug_buf
, pos
);
708 mutex_unlock(&debug_buf_lock
);
712 static const struct file_operations waiters_fops
= {
713 .owner
= THIS_MODULE
,
715 .read
= waiters_read
,
716 .llseek
= default_llseek
,
719 void dlm_delete_debug_file(struct dlm_ls
*ls
)
721 debugfs_remove(ls
->ls_debug_rsb_dentry
);
722 debugfs_remove(ls
->ls_debug_waiters_dentry
);
723 debugfs_remove(ls
->ls_debug_locks_dentry
);
724 debugfs_remove(ls
->ls_debug_all_dentry
);
725 debugfs_remove(ls
->ls_debug_toss_dentry
);
728 int dlm_create_debug_file(struct dlm_ls
*ls
)
730 char name
[DLM_LOCKSPACE_LEN
+8];
734 ls
->ls_debug_rsb_dentry
= debugfs_create_file(ls
->ls_name
,
739 if (!ls
->ls_debug_rsb_dentry
)
744 memset(name
, 0, sizeof(name
));
745 snprintf(name
, DLM_LOCKSPACE_LEN
+8, "%s_locks", ls
->ls_name
);
747 ls
->ls_debug_locks_dentry
= debugfs_create_file(name
,
752 if (!ls
->ls_debug_locks_dentry
)
757 memset(name
, 0, sizeof(name
));
758 snprintf(name
, DLM_LOCKSPACE_LEN
+8, "%s_all", ls
->ls_name
);
760 ls
->ls_debug_all_dentry
= debugfs_create_file(name
,
765 if (!ls
->ls_debug_all_dentry
)
770 memset(name
, 0, sizeof(name
));
771 snprintf(name
, DLM_LOCKSPACE_LEN
+8, "%s_toss", ls
->ls_name
);
773 ls
->ls_debug_toss_dentry
= debugfs_create_file(name
,
778 if (!ls
->ls_debug_toss_dentry
)
781 memset(name
, 0, sizeof(name
));
782 snprintf(name
, DLM_LOCKSPACE_LEN
+8, "%s_waiters", ls
->ls_name
);
784 ls
->ls_debug_waiters_dentry
= debugfs_create_file(name
,
789 if (!ls
->ls_debug_waiters_dentry
)
795 dlm_delete_debug_file(ls
);
799 int __init
dlm_register_debugfs(void)
801 mutex_init(&debug_buf_lock
);
802 dlm_root
= debugfs_create_dir("dlm", NULL
);
803 return dlm_root
? 0 : -ENOMEM
;
806 void dlm_unregister_debugfs(void)
808 debugfs_remove(dlm_root
);