1 // SPDX-License-Identifier: GPL-2.0-only
2 /******************************************************************************
3 *******************************************************************************
5 ** Copyright (C) 2005-2009 Red Hat, Inc. All rights reserved.
8 *******************************************************************************
9 ******************************************************************************/
11 #include <linux/pagemap.h>
12 #include <linux/seq_file.h>
13 #include <linux/init.h>
14 #include <linux/ctype.h>
15 #include <linux/debugfs.h>
16 #include <linux/slab.h>
18 #include "dlm_internal.h"
21 #define DLM_DEBUG_BUF_LEN 4096
22 static char debug_buf
[DLM_DEBUG_BUF_LEN
];
23 static struct mutex debug_buf_lock
;
25 static struct dentry
*dlm_root
;
27 static char *print_lockmode(int mode
)
49 static void print_format1_lock(struct seq_file
*s
, struct dlm_lkb
*lkb
,
52 seq_printf(s
, "%08x %s", lkb
->lkb_id
, print_lockmode(lkb
->lkb_grmode
));
54 if (lkb
->lkb_status
== DLM_LKSTS_CONVERT
||
55 lkb
->lkb_status
== DLM_LKSTS_WAITING
)
56 seq_printf(s
, " (%s)", print_lockmode(lkb
->lkb_rqmode
));
58 if (lkb
->lkb_nodeid
) {
59 if (lkb
->lkb_nodeid
!= res
->res_nodeid
)
60 seq_printf(s
, " Remote: %3d %08x", lkb
->lkb_nodeid
,
63 seq_printf(s
, " Master: %08x", lkb
->lkb_remid
);
66 if (lkb
->lkb_wait_type
)
67 seq_printf(s
, " wait_type: %d", lkb
->lkb_wait_type
);
72 static void print_format1(struct dlm_rsb
*res
, struct seq_file
*s
)
75 int i
, lvblen
= res
->res_ls
->ls_lvblen
, recover_list
, root_list
;
79 seq_printf(s
, "\nResource %p Name (len=%d) \"", res
, res
->res_length
);
81 for (i
= 0; i
< res
->res_length
; i
++) {
82 if (isprint(res
->res_name
[i
]))
83 seq_printf(s
, "%c", res
->res_name
[i
]);
85 seq_printf(s
, "%c", '.');
88 if (res
->res_nodeid
> 0)
89 seq_printf(s
, "\"\nLocal Copy, Master is node %d\n",
91 else if (res
->res_nodeid
== 0)
92 seq_puts(s
, "\"\nMaster Copy\n");
93 else if (res
->res_nodeid
== -1)
94 seq_printf(s
, "\"\nLooking up master (lkid %x)\n",
97 seq_printf(s
, "\"\nInvalid master %d\n", res
->res_nodeid
);
98 if (seq_has_overflowed(s
))
102 if (res
->res_lvbptr
) {
103 seq_puts(s
, "LVB: ");
104 for (i
= 0; i
< lvblen
; i
++) {
107 seq_printf(s
, "%02x ",
108 (unsigned char) res
->res_lvbptr
[i
]);
110 if (rsb_flag(res
, RSB_VALNOTVALID
))
111 seq_puts(s
, " (INVALID)");
113 if (seq_has_overflowed(s
))
117 root_list
= !list_empty(&res
->res_root_list
);
118 recover_list
= !list_empty(&res
->res_recover_list
);
120 if (root_list
|| recover_list
) {
121 seq_printf(s
, "Recovery: root %d recover %d flags %lx count %d\n",
122 root_list
, recover_list
,
123 res
->res_flags
, res
->res_recover_locks_count
);
126 /* Print the locks attached to this resource */
127 seq_puts(s
, "Granted Queue\n");
128 list_for_each_entry(lkb
, &res
->res_grantqueue
, lkb_statequeue
) {
129 print_format1_lock(s
, lkb
, res
);
130 if (seq_has_overflowed(s
))
134 seq_puts(s
, "Conversion Queue\n");
135 list_for_each_entry(lkb
, &res
->res_convertqueue
, lkb_statequeue
) {
136 print_format1_lock(s
, lkb
, res
);
137 if (seq_has_overflowed(s
))
141 seq_puts(s
, "Waiting Queue\n");
142 list_for_each_entry(lkb
, &res
->res_waitqueue
, lkb_statequeue
) {
143 print_format1_lock(s
, lkb
, res
);
144 if (seq_has_overflowed(s
))
148 if (list_empty(&res
->res_lookup
))
151 seq_puts(s
, "Lookup Queue\n");
152 list_for_each_entry(lkb
, &res
->res_lookup
, lkb_rsb_lookup
) {
153 seq_printf(s
, "%08x %s",
154 lkb
->lkb_id
, print_lockmode(lkb
->lkb_rqmode
));
155 if (lkb
->lkb_wait_type
)
156 seq_printf(s
, " wait_type: %d", lkb
->lkb_wait_type
);
158 if (seq_has_overflowed(s
))
165 static void print_format2_lock(struct seq_file
*s
, struct dlm_lkb
*lkb
,
171 if (lkb
->lkb_flags
& DLM_IFL_USER
) {
173 xid
= lkb
->lkb_ua
->xid
;
176 /* microseconds since lkb was added to current queue */
177 us
= ktime_to_us(ktime_sub(ktime_get(), lkb
->lkb_timestamp
));
179 /* id nodeid remid pid xid exflags flags sts grmode rqmode time_us
180 r_nodeid r_len r_name */
182 seq_printf(s
, "%x %d %x %u %llu %x %x %d %d %d %llu %u %d \"%s\"\n",
187 (unsigned long long)xid
,
193 (unsigned long long)us
,
199 static void print_format2(struct dlm_rsb
*r
, struct seq_file
*s
)
205 list_for_each_entry(lkb
, &r
->res_grantqueue
, lkb_statequeue
) {
206 print_format2_lock(s
, lkb
, r
);
207 if (seq_has_overflowed(s
))
211 list_for_each_entry(lkb
, &r
->res_convertqueue
, lkb_statequeue
) {
212 print_format2_lock(s
, lkb
, r
);
213 if (seq_has_overflowed(s
))
217 list_for_each_entry(lkb
, &r
->res_waitqueue
, lkb_statequeue
) {
218 print_format2_lock(s
, lkb
, r
);
219 if (seq_has_overflowed(s
))
226 static void print_format3_lock(struct seq_file
*s
, struct dlm_lkb
*lkb
,
231 if (lkb
->lkb_flags
& DLM_IFL_USER
) {
233 xid
= lkb
->lkb_ua
->xid
;
236 seq_printf(s
, "lkb %x %d %x %u %llu %x %x %d %d %d %d %d %d %u %llu %llu\n",
241 (unsigned long long)xid
,
247 lkb
->lkb_last_bast
.mode
,
251 (unsigned long long)ktime_to_ns(lkb
->lkb_timestamp
),
252 (unsigned long long)ktime_to_ns(lkb
->lkb_last_bast_time
));
255 static void print_format3(struct dlm_rsb
*r
, struct seq_file
*s
)
258 int i
, lvblen
= r
->res_ls
->ls_lvblen
;
263 seq_printf(s
, "rsb %p %d %x %lx %d %d %u %d ",
268 !list_empty(&r
->res_root_list
),
269 !list_empty(&r
->res_recover_list
),
270 r
->res_recover_locks_count
,
272 if (seq_has_overflowed(s
))
275 for (i
= 0; i
< r
->res_length
; i
++) {
276 if (!isascii(r
->res_name
[i
]) || !isprint(r
->res_name
[i
]))
280 seq_puts(s
, print_name
? "str " : "hex");
282 for (i
= 0; i
< r
->res_length
; i
++) {
284 seq_printf(s
, "%c", r
->res_name
[i
]);
286 seq_printf(s
, " %02x", (unsigned char)r
->res_name
[i
]);
289 if (seq_has_overflowed(s
))
295 seq_printf(s
, "lvb %u %d", r
->res_lvbseq
, lvblen
);
297 for (i
= 0; i
< lvblen
; i
++)
298 seq_printf(s
, " %02x", (unsigned char)r
->res_lvbptr
[i
]);
300 if (seq_has_overflowed(s
))
304 list_for_each_entry(lkb
, &r
->res_grantqueue
, lkb_statequeue
) {
305 print_format3_lock(s
, lkb
, 0);
306 if (seq_has_overflowed(s
))
310 list_for_each_entry(lkb
, &r
->res_convertqueue
, lkb_statequeue
) {
311 print_format3_lock(s
, lkb
, 0);
312 if (seq_has_overflowed(s
))
316 list_for_each_entry(lkb
, &r
->res_waitqueue
, lkb_statequeue
) {
317 print_format3_lock(s
, lkb
, 0);
318 if (seq_has_overflowed(s
))
322 list_for_each_entry(lkb
, &r
->res_lookup
, lkb_rsb_lookup
) {
323 print_format3_lock(s
, lkb
, 1);
324 if (seq_has_overflowed(s
))
331 static void print_format4(struct dlm_rsb
*r
, struct seq_file
*s
)
333 int our_nodeid
= dlm_our_nodeid();
339 seq_printf(s
, "rsb %p %d %d %d %d %lu %lx %d ",
342 r
->res_master_nodeid
,
349 for (i
= 0; i
< r
->res_length
; i
++) {
350 if (!isascii(r
->res_name
[i
]) || !isprint(r
->res_name
[i
]))
354 seq_puts(s
, print_name
? "str " : "hex");
356 for (i
= 0; i
< r
->res_length
; i
++) {
358 seq_printf(s
, "%c", r
->res_name
[i
]);
360 seq_printf(s
, " %02x", (unsigned char)r
->res_name
[i
]);
374 * If the buffer is full, seq_printf can be called again, but it
375 * does nothing. So, the these printing routines periodically check
376 * seq_has_overflowed to avoid wasting too much time trying to print to
380 static int table_seq_show(struct seq_file
*seq
, void *iter_ptr
)
382 struct rsbtbl_iter
*ri
= iter_ptr
;
384 switch (ri
->format
) {
386 print_format1(ri
->rsb
, seq
);
390 seq_puts(seq
, "id nodeid remid pid xid exflags flags sts grmode rqmode time_ms r_nodeid r_len r_name\n");
393 print_format2(ri
->rsb
, seq
);
397 seq_puts(seq
, "version rsb 1.1 lvb 1.1 lkb 1.1\n");
400 print_format3(ri
->rsb
, seq
);
404 seq_puts(seq
, "version 4 rsb 2\n");
407 print_format4(ri
->rsb
, seq
);
414 static const struct seq_operations format1_seq_ops
;
415 static const struct seq_operations format2_seq_ops
;
416 static const struct seq_operations format3_seq_ops
;
417 static const struct seq_operations format4_seq_ops
;
419 static void *table_seq_start(struct seq_file
*seq
, loff_t
*pos
)
421 struct rb_root
*tree
;
422 struct rb_node
*node
;
423 struct dlm_ls
*ls
= seq
->private;
424 struct rsbtbl_iter
*ri
;
427 unsigned bucket
, entry
;
428 int toss
= (seq
->op
== &format4_seq_ops
);
431 entry
= n
& ((1LL << 32) - 1);
433 if (bucket
>= ls
->ls_rsbtbl_size
)
436 ri
= kzalloc(sizeof(*ri
), GFP_NOFS
);
441 if (seq
->op
== &format1_seq_ops
)
443 if (seq
->op
== &format2_seq_ops
)
445 if (seq
->op
== &format3_seq_ops
)
447 if (seq
->op
== &format4_seq_ops
)
450 tree
= toss
? &ls
->ls_rsbtbl
[bucket
].toss
: &ls
->ls_rsbtbl
[bucket
].keep
;
452 spin_lock(&ls
->ls_rsbtbl
[bucket
].lock
);
453 if (!RB_EMPTY_ROOT(tree
)) {
454 for (node
= rb_first(tree
); node
; node
= rb_next(node
)) {
455 r
= rb_entry(node
, struct dlm_rsb
, res_hashnode
);
460 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
465 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
468 * move to the first rsb in the next non-empty bucket
472 n
&= ~((1LL << 32) - 1);
478 if (bucket
>= ls
->ls_rsbtbl_size
) {
482 tree
= toss
? &ls
->ls_rsbtbl
[bucket
].toss
: &ls
->ls_rsbtbl
[bucket
].keep
;
484 spin_lock(&ls
->ls_rsbtbl
[bucket
].lock
);
485 if (!RB_EMPTY_ROOT(tree
)) {
486 node
= rb_first(tree
);
487 r
= rb_entry(node
, struct dlm_rsb
, res_hashnode
);
491 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
495 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
499 static void *table_seq_next(struct seq_file
*seq
, void *iter_ptr
, loff_t
*pos
)
501 struct dlm_ls
*ls
= seq
->private;
502 struct rsbtbl_iter
*ri
= iter_ptr
;
503 struct rb_root
*tree
;
504 struct rb_node
*next
;
505 struct dlm_rsb
*r
, *rp
;
508 int toss
= (seq
->op
== &format4_seq_ops
);
513 * move to the next rsb in the same bucket
516 spin_lock(&ls
->ls_rsbtbl
[bucket
].lock
);
518 next
= rb_next(&rp
->res_hashnode
);
521 r
= rb_entry(next
, struct dlm_rsb
, res_hashnode
);
524 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
529 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
533 * move to the first rsb in the next non-empty bucket
537 n
&= ~((1LL << 32) - 1);
543 if (bucket
>= ls
->ls_rsbtbl_size
) {
547 tree
= toss
? &ls
->ls_rsbtbl
[bucket
].toss
: &ls
->ls_rsbtbl
[bucket
].keep
;
549 spin_lock(&ls
->ls_rsbtbl
[bucket
].lock
);
550 if (!RB_EMPTY_ROOT(tree
)) {
551 next
= rb_first(tree
);
552 r
= rb_entry(next
, struct dlm_rsb
, res_hashnode
);
556 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
560 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
564 static void table_seq_stop(struct seq_file
*seq
, void *iter_ptr
)
566 struct rsbtbl_iter
*ri
= iter_ptr
;
569 dlm_put_rsb(ri
->rsb
);
574 static const struct seq_operations format1_seq_ops
= {
575 .start
= table_seq_start
,
576 .next
= table_seq_next
,
577 .stop
= table_seq_stop
,
578 .show
= table_seq_show
,
581 static const struct seq_operations format2_seq_ops
= {
582 .start
= table_seq_start
,
583 .next
= table_seq_next
,
584 .stop
= table_seq_stop
,
585 .show
= table_seq_show
,
588 static const struct seq_operations format3_seq_ops
= {
589 .start
= table_seq_start
,
590 .next
= table_seq_next
,
591 .stop
= table_seq_stop
,
592 .show
= table_seq_show
,
595 static const struct seq_operations format4_seq_ops
= {
596 .start
= table_seq_start
,
597 .next
= table_seq_next
,
598 .stop
= table_seq_stop
,
599 .show
= table_seq_show
,
602 static const struct file_operations format1_fops
;
603 static const struct file_operations format2_fops
;
604 static const struct file_operations format3_fops
;
605 static const struct file_operations format4_fops
;
607 static int table_open1(struct inode
*inode
, struct file
*file
)
609 struct seq_file
*seq
;
612 ret
= seq_open(file
, &format1_seq_ops
);
616 seq
= file
->private_data
;
617 seq
->private = inode
->i_private
; /* the dlm_ls */
621 static int table_open2(struct inode
*inode
, struct file
*file
)
623 struct seq_file
*seq
;
626 ret
= seq_open(file
, &format2_seq_ops
);
630 seq
= file
->private_data
;
631 seq
->private = inode
->i_private
; /* the dlm_ls */
635 static int table_open3(struct inode
*inode
, struct file
*file
)
637 struct seq_file
*seq
;
640 ret
= seq_open(file
, &format3_seq_ops
);
644 seq
= file
->private_data
;
645 seq
->private = inode
->i_private
; /* the dlm_ls */
649 static int table_open4(struct inode
*inode
, struct file
*file
)
651 struct seq_file
*seq
;
654 ret
= seq_open(file
, &format4_seq_ops
);
658 seq
= file
->private_data
;
659 seq
->private = inode
->i_private
; /* the dlm_ls */
663 static const struct file_operations format1_fops
= {
664 .owner
= THIS_MODULE
,
668 .release
= seq_release
671 static const struct file_operations format2_fops
= {
672 .owner
= THIS_MODULE
,
676 .release
= seq_release
679 static const struct file_operations format3_fops
= {
680 .owner
= THIS_MODULE
,
684 .release
= seq_release
687 static const struct file_operations format4_fops
= {
688 .owner
= THIS_MODULE
,
692 .release
= seq_release
696 * dump lkb's on the ls_waiters list
698 static ssize_t
waiters_read(struct file
*file
, char __user
*userbuf
,
699 size_t count
, loff_t
*ppos
)
701 struct dlm_ls
*ls
= file
->private_data
;
703 size_t len
= DLM_DEBUG_BUF_LEN
, pos
= 0, ret
, rv
;
705 mutex_lock(&debug_buf_lock
);
706 mutex_lock(&ls
->ls_waiters_mutex
);
707 memset(debug_buf
, 0, sizeof(debug_buf
));
709 list_for_each_entry(lkb
, &ls
->ls_waiters
, lkb_wait_reply
) {
710 ret
= snprintf(debug_buf
+ pos
, len
- pos
, "%x %d %d %s\n",
711 lkb
->lkb_id
, lkb
->lkb_wait_type
,
712 lkb
->lkb_nodeid
, lkb
->lkb_resource
->res_name
);
713 if (ret
>= len
- pos
)
717 mutex_unlock(&ls
->ls_waiters_mutex
);
719 rv
= simple_read_from_buffer(userbuf
, count
, ppos
, debug_buf
, pos
);
720 mutex_unlock(&debug_buf_lock
);
724 static const struct file_operations waiters_fops
= {
725 .owner
= THIS_MODULE
,
727 .read
= waiters_read
,
728 .llseek
= default_llseek
,
731 void dlm_delete_debug_file(struct dlm_ls
*ls
)
733 debugfs_remove(ls
->ls_debug_rsb_dentry
);
734 debugfs_remove(ls
->ls_debug_waiters_dentry
);
735 debugfs_remove(ls
->ls_debug_locks_dentry
);
736 debugfs_remove(ls
->ls_debug_all_dentry
);
737 debugfs_remove(ls
->ls_debug_toss_dentry
);
740 void dlm_create_debug_file(struct dlm_ls
*ls
)
742 char name
[DLM_LOCKSPACE_LEN
+ 8];
746 ls
->ls_debug_rsb_dentry
= debugfs_create_file(ls
->ls_name
,
754 memset(name
, 0, sizeof(name
));
755 snprintf(name
, DLM_LOCKSPACE_LEN
+ 8, "%s_locks", ls
->ls_name
);
757 ls
->ls_debug_locks_dentry
= debugfs_create_file(name
,
765 memset(name
, 0, sizeof(name
));
766 snprintf(name
, DLM_LOCKSPACE_LEN
+ 8, "%s_all", ls
->ls_name
);
768 ls
->ls_debug_all_dentry
= debugfs_create_file(name
,
776 memset(name
, 0, sizeof(name
));
777 snprintf(name
, DLM_LOCKSPACE_LEN
+ 8, "%s_toss", ls
->ls_name
);
779 ls
->ls_debug_toss_dentry
= debugfs_create_file(name
,
785 memset(name
, 0, sizeof(name
));
786 snprintf(name
, DLM_LOCKSPACE_LEN
+ 8, "%s_waiters", ls
->ls_name
);
788 ls
->ls_debug_waiters_dentry
= debugfs_create_file(name
,
795 void __init
dlm_register_debugfs(void)
797 mutex_init(&debug_buf_lock
);
798 dlm_root
= debugfs_create_dir("dlm", NULL
);
801 void dlm_unregister_debugfs(void)
803 debugfs_remove(dlm_root
);