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>
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
;
28 static char *print_lockmode(int mode
)
50 static int print_format1_lock(struct seq_file
*s
, struct dlm_lkb
*lkb
,
53 seq_printf(s
, "%08x %s", lkb
->lkb_id
, print_lockmode(lkb
->lkb_grmode
));
55 if (lkb
->lkb_status
== DLM_LKSTS_CONVERT
||
56 lkb
->lkb_status
== DLM_LKSTS_WAITING
)
57 seq_printf(s
, " (%s)", print_lockmode(lkb
->lkb_rqmode
));
59 if (lkb
->lkb_nodeid
) {
60 if (lkb
->lkb_nodeid
!= res
->res_nodeid
)
61 seq_printf(s
, " Remote: %3d %08x", lkb
->lkb_nodeid
,
64 seq_printf(s
, " Master: %08x", lkb
->lkb_remid
);
67 if (lkb
->lkb_wait_type
)
68 seq_printf(s
, " wait_type: %d", lkb
->lkb_wait_type
);
70 return seq_printf(s
, "\n");
73 static int print_format1(struct dlm_rsb
*res
, struct seq_file
*s
)
76 int i
, lvblen
= res
->res_ls
->ls_lvblen
, recover_list
, root_list
;
81 rv
= seq_printf(s
, "\nResource %p Name (len=%d) \"",
82 res
, res
->res_length
);
86 for (i
= 0; i
< res
->res_length
; i
++) {
87 if (isprint(res
->res_name
[i
]))
88 seq_printf(s
, "%c", res
->res_name
[i
]);
90 seq_printf(s
, "%c", '.');
93 if (res
->res_nodeid
> 0)
94 rv
= seq_printf(s
, "\" \nLocal Copy, Master is node %d\n",
96 else if (res
->res_nodeid
== 0)
97 rv
= seq_printf(s
, "\" \nMaster Copy\n");
98 else if (res
->res_nodeid
== -1)
99 rv
= seq_printf(s
, "\" \nLooking up master (lkid %x)\n",
100 res
->res_first_lkid
);
102 rv
= seq_printf(s
, "\" \nInvalid master %d\n",
108 if (res
->res_lvbptr
) {
109 seq_printf(s
, "LVB: ");
110 for (i
= 0; i
< lvblen
; i
++) {
112 seq_printf(s
, "\n ");
113 seq_printf(s
, "%02x ",
114 (unsigned char) res
->res_lvbptr
[i
]);
116 if (rsb_flag(res
, RSB_VALNOTVALID
))
117 seq_printf(s
, " (INVALID)");
118 rv
= seq_printf(s
, "\n");
123 root_list
= !list_empty(&res
->res_root_list
);
124 recover_list
= !list_empty(&res
->res_recover_list
);
126 if (root_list
|| recover_list
) {
127 rv
= seq_printf(s
, "Recovery: root %d recover %d flags %lx "
128 "count %d\n", root_list
, recover_list
,
129 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 rv
= print_format1_lock(s
, lkb
, res
);
142 seq_printf(s
, "Conversion Queue\n");
143 list_for_each_entry(lkb
, &res
->res_convertqueue
, lkb_statequeue
) {
144 rv
= print_format1_lock(s
, lkb
, res
);
149 seq_printf(s
, "Waiting Queue\n");
150 list_for_each_entry(lkb
, &res
->res_waitqueue
, lkb_statequeue
) {
151 rv
= print_format1_lock(s
, lkb
, res
);
156 if (list_empty(&res
->res_lookup
))
159 seq_printf(s
, "Lookup Queue\n");
160 list_for_each_entry(lkb
, &res
->res_lookup
, lkb_rsb_lookup
) {
161 rv
= seq_printf(s
, "%08x %s", lkb
->lkb_id
,
162 print_lockmode(lkb
->lkb_rqmode
));
163 if (lkb
->lkb_wait_type
)
164 seq_printf(s
, " wait_type: %d", lkb
->lkb_wait_type
);
165 rv
= seq_printf(s
, "\n");
172 static int print_format2_lock(struct seq_file
*s
, struct dlm_lkb
*lkb
,
179 if (lkb
->lkb_flags
& DLM_IFL_USER
) {
181 xid
= lkb
->lkb_ua
->xid
;
184 /* microseconds since lkb was added to current queue */
185 us
= ktime_to_us(ktime_sub(ktime_get(), lkb
->lkb_timestamp
));
187 /* id nodeid remid pid xid exflags flags sts grmode rqmode time_us
188 r_nodeid r_len r_name */
190 rv
= seq_printf(s
, "%x %d %x %u %llu %x %x %d %d %d %llu %u %d \"%s\"\n",
195 (unsigned long long)xid
,
201 (unsigned long long)us
,
208 static int print_format2(struct dlm_rsb
*r
, struct seq_file
*s
)
215 list_for_each_entry(lkb
, &r
->res_grantqueue
, lkb_statequeue
) {
216 rv
= print_format2_lock(s
, lkb
, r
);
221 list_for_each_entry(lkb
, &r
->res_convertqueue
, lkb_statequeue
) {
222 rv
= print_format2_lock(s
, lkb
, r
);
227 list_for_each_entry(lkb
, &r
->res_waitqueue
, lkb_statequeue
) {
228 rv
= print_format2_lock(s
, lkb
, r
);
237 static int print_format3_lock(struct seq_file
*s
, struct dlm_lkb
*lkb
,
243 if (lkb
->lkb_flags
& DLM_IFL_USER
) {
245 xid
= lkb
->lkb_ua
->xid
;
248 rv
= seq_printf(s
, "lkb %x %d %x %u %llu %x %x %d %d %d %d %d %d %u %llu %llu\n",
253 (unsigned long long)xid
,
263 (unsigned long long)ktime_to_ns(lkb
->lkb_timestamp
),
264 (unsigned long long)ktime_to_ns(lkb
->lkb_time_bast
));
268 static int print_format3(struct dlm_rsb
*r
, struct seq_file
*s
)
271 int i
, lvblen
= r
->res_ls
->ls_lvblen
;
277 rv
= seq_printf(s
, "rsb %p %d %x %lx %d %d %u %d ",
282 !list_empty(&r
->res_root_list
),
283 !list_empty(&r
->res_recover_list
),
284 r
->res_recover_locks_count
,
289 for (i
= 0; i
< r
->res_length
; i
++) {
290 if (!isascii(r
->res_name
[i
]) || !isprint(r
->res_name
[i
]))
294 seq_printf(s
, "%s", print_name
? "str " : "hex");
296 for (i
= 0; i
< r
->res_length
; i
++) {
298 seq_printf(s
, "%c", r
->res_name
[i
]);
300 seq_printf(s
, " %02x", (unsigned char)r
->res_name
[i
]);
302 rv
= seq_printf(s
, "\n");
309 seq_printf(s
, "lvb %u %d", r
->res_lvbseq
, lvblen
);
311 for (i
= 0; i
< lvblen
; i
++)
312 seq_printf(s
, " %02x", (unsigned char)r
->res_lvbptr
[i
]);
313 rv
= seq_printf(s
, "\n");
318 list_for_each_entry(lkb
, &r
->res_grantqueue
, lkb_statequeue
) {
319 rv
= print_format3_lock(s
, lkb
, 0);
324 list_for_each_entry(lkb
, &r
->res_convertqueue
, lkb_statequeue
) {
325 rv
= print_format3_lock(s
, lkb
, 0);
330 list_for_each_entry(lkb
, &r
->res_waitqueue
, lkb_statequeue
) {
331 rv
= print_format3_lock(s
, lkb
, 0);
336 list_for_each_entry(lkb
, &r
->res_lookup
, lkb_rsb_lookup
) {
337 rv
= print_format3_lock(s
, lkb
, 1);
353 /* seq_printf returns -1 if the buffer is full, and 0 otherwise.
354 If the buffer is full, seq_printf can be called again, but it
355 does nothing and just returns -1. So, the these printing routines
356 periodically check the return value to avoid wasting too much time
357 trying to print to a full buffer. */
359 static int table_seq_show(struct seq_file
*seq
, void *iter_ptr
)
361 struct rsbtbl_iter
*ri
= iter_ptr
;
364 switch (ri
->format
) {
366 rv
= print_format1(ri
->rsb
, seq
);
370 seq_printf(seq
, "id nodeid remid pid xid exflags "
371 "flags sts grmode rqmode time_ms "
372 "r_nodeid r_len r_name\n");
375 rv
= print_format2(ri
->rsb
, seq
);
379 seq_printf(seq
, "version rsb 1.1 lvb 1.1 lkb 1.1\n");
382 rv
= print_format3(ri
->rsb
, seq
);
389 static const struct seq_operations format1_seq_ops
;
390 static const struct seq_operations format2_seq_ops
;
391 static const struct seq_operations format3_seq_ops
;
393 static void *table_seq_start(struct seq_file
*seq
, loff_t
*pos
)
395 struct dlm_ls
*ls
= seq
->private;
396 struct rsbtbl_iter
*ri
;
399 unsigned bucket
, entry
;
402 entry
= n
& ((1LL << 32) - 1);
404 if (bucket
>= ls
->ls_rsbtbl_size
)
407 ri
= kzalloc(sizeof(struct rsbtbl_iter
), GFP_KERNEL
);
412 if (seq
->op
== &format1_seq_ops
)
414 if (seq
->op
== &format2_seq_ops
)
416 if (seq
->op
== &format3_seq_ops
)
419 spin_lock(&ls
->ls_rsbtbl
[bucket
].lock
);
420 if (!list_empty(&ls
->ls_rsbtbl
[bucket
].list
)) {
421 list_for_each_entry(r
, &ls
->ls_rsbtbl
[bucket
].list
,
427 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
432 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
435 * move to the first rsb in the next non-empty bucket
439 n
&= ~((1LL << 32) - 1);
445 if (bucket
>= ls
->ls_rsbtbl_size
) {
450 spin_lock(&ls
->ls_rsbtbl
[bucket
].lock
);
451 if (!list_empty(&ls
->ls_rsbtbl
[bucket
].list
)) {
452 r
= list_first_entry(&ls
->ls_rsbtbl
[bucket
].list
,
453 struct dlm_rsb
, res_hashchain
);
457 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
461 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
465 static void *table_seq_next(struct seq_file
*seq
, void *iter_ptr
, loff_t
*pos
)
467 struct dlm_ls
*ls
= seq
->private;
468 struct rsbtbl_iter
*ri
= iter_ptr
;
469 struct list_head
*next
;
470 struct dlm_rsb
*r
, *rp
;
477 * move to the next rsb in the same bucket
480 spin_lock(&ls
->ls_rsbtbl
[bucket
].lock
);
482 next
= rp
->res_hashchain
.next
;
484 if (next
!= &ls
->ls_rsbtbl
[bucket
].list
) {
485 r
= list_entry(next
, struct dlm_rsb
, res_hashchain
);
488 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
493 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
497 * move to the first rsb in the next non-empty bucket
501 n
&= ~((1LL << 32) - 1);
507 if (bucket
>= ls
->ls_rsbtbl_size
) {
512 spin_lock(&ls
->ls_rsbtbl
[bucket
].lock
);
513 if (!list_empty(&ls
->ls_rsbtbl
[bucket
].list
)) {
514 r
= list_first_entry(&ls
->ls_rsbtbl
[bucket
].list
,
515 struct dlm_rsb
, res_hashchain
);
519 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
523 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
527 static void table_seq_stop(struct seq_file
*seq
, void *iter_ptr
)
529 struct rsbtbl_iter
*ri
= iter_ptr
;
532 dlm_put_rsb(ri
->rsb
);
537 static const struct seq_operations format1_seq_ops
= {
538 .start
= table_seq_start
,
539 .next
= table_seq_next
,
540 .stop
= table_seq_stop
,
541 .show
= table_seq_show
,
544 static const struct seq_operations format2_seq_ops
= {
545 .start
= table_seq_start
,
546 .next
= table_seq_next
,
547 .stop
= table_seq_stop
,
548 .show
= table_seq_show
,
551 static const struct seq_operations format3_seq_ops
= {
552 .start
= table_seq_start
,
553 .next
= table_seq_next
,
554 .stop
= table_seq_stop
,
555 .show
= table_seq_show
,
558 static const struct file_operations format1_fops
;
559 static const struct file_operations format2_fops
;
560 static const struct file_operations format3_fops
;
562 static int table_open(struct inode
*inode
, struct file
*file
)
564 struct seq_file
*seq
;
567 if (file
->f_op
== &format1_fops
)
568 ret
= seq_open(file
, &format1_seq_ops
);
569 else if (file
->f_op
== &format2_fops
)
570 ret
= seq_open(file
, &format2_seq_ops
);
571 else if (file
->f_op
== &format3_fops
)
572 ret
= seq_open(file
, &format3_seq_ops
);
577 seq
= file
->private_data
;
578 seq
->private = inode
->i_private
; /* the dlm_ls */
582 static const struct file_operations format1_fops
= {
583 .owner
= THIS_MODULE
,
587 .release
= seq_release
590 static const struct file_operations format2_fops
= {
591 .owner
= THIS_MODULE
,
595 .release
= seq_release
598 static const struct file_operations format3_fops
= {
599 .owner
= THIS_MODULE
,
603 .release
= seq_release
607 * dump lkb's on the ls_waiters list
610 static int waiters_open(struct inode
*inode
, struct file
*file
)
612 file
->private_data
= inode
->i_private
;
616 static ssize_t
waiters_read(struct file
*file
, char __user
*userbuf
,
617 size_t count
, loff_t
*ppos
)
619 struct dlm_ls
*ls
= file
->private_data
;
621 size_t len
= DLM_DEBUG_BUF_LEN
, pos
= 0, ret
, rv
;
623 mutex_lock(&debug_buf_lock
);
624 mutex_lock(&ls
->ls_waiters_mutex
);
625 memset(debug_buf
, 0, sizeof(debug_buf
));
627 list_for_each_entry(lkb
, &ls
->ls_waiters
, lkb_wait_reply
) {
628 ret
= snprintf(debug_buf
+ pos
, len
- pos
, "%x %d %d %s\n",
629 lkb
->lkb_id
, lkb
->lkb_wait_type
,
630 lkb
->lkb_nodeid
, lkb
->lkb_resource
->res_name
);
631 if (ret
>= len
- pos
)
635 mutex_unlock(&ls
->ls_waiters_mutex
);
637 rv
= simple_read_from_buffer(userbuf
, count
, ppos
, debug_buf
, pos
);
638 mutex_unlock(&debug_buf_lock
);
642 static const struct file_operations waiters_fops
= {
643 .owner
= THIS_MODULE
,
644 .open
= waiters_open
,
648 void dlm_delete_debug_file(struct dlm_ls
*ls
)
650 if (ls
->ls_debug_rsb_dentry
)
651 debugfs_remove(ls
->ls_debug_rsb_dentry
);
652 if (ls
->ls_debug_waiters_dentry
)
653 debugfs_remove(ls
->ls_debug_waiters_dentry
);
654 if (ls
->ls_debug_locks_dentry
)
655 debugfs_remove(ls
->ls_debug_locks_dentry
);
656 if (ls
->ls_debug_all_dentry
)
657 debugfs_remove(ls
->ls_debug_all_dentry
);
660 int dlm_create_debug_file(struct dlm_ls
*ls
)
662 char name
[DLM_LOCKSPACE_LEN
+8];
666 ls
->ls_debug_rsb_dentry
= debugfs_create_file(ls
->ls_name
,
671 if (!ls
->ls_debug_rsb_dentry
)
676 memset(name
, 0, sizeof(name
));
677 snprintf(name
, DLM_LOCKSPACE_LEN
+8, "%s_locks", ls
->ls_name
);
679 ls
->ls_debug_locks_dentry
= debugfs_create_file(name
,
684 if (!ls
->ls_debug_locks_dentry
)
689 memset(name
, 0, sizeof(name
));
690 snprintf(name
, DLM_LOCKSPACE_LEN
+8, "%s_all", ls
->ls_name
);
692 ls
->ls_debug_all_dentry
= debugfs_create_file(name
,
697 if (!ls
->ls_debug_all_dentry
)
700 memset(name
, 0, sizeof(name
));
701 snprintf(name
, DLM_LOCKSPACE_LEN
+8, "%s_waiters", ls
->ls_name
);
703 ls
->ls_debug_waiters_dentry
= debugfs_create_file(name
,
708 if (!ls
->ls_debug_waiters_dentry
)
714 dlm_delete_debug_file(ls
);
718 int __init
dlm_register_debugfs(void)
720 mutex_init(&debug_buf_lock
);
721 dlm_root
= debugfs_create_dir("dlm", NULL
);
722 return dlm_root
? 0 : -ENOMEM
;
725 void dlm_unregister_debugfs(void)
727 debugfs_remove(dlm_root
);