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"
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
;
27 static struct dentry
*dlm_comms
;
29 static char *print_lockmode(int mode
)
51 static void 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
);
74 static void print_format1(struct dlm_rsb
*res
, struct seq_file
*s
)
77 int i
, lvblen
= res
->res_ls
->ls_lvblen
, recover_list
, root_list
;
81 seq_printf(s
, "\nResource %p Name (len=%d) \"", res
, res
->res_length
);
83 for (i
= 0; i
< res
->res_length
; i
++) {
84 if (isprint(res
->res_name
[i
]))
85 seq_printf(s
, "%c", res
->res_name
[i
]);
87 seq_printf(s
, "%c", '.');
90 if (res
->res_nodeid
> 0)
91 seq_printf(s
, "\"\nLocal Copy, Master is node %d\n",
93 else if (res
->res_nodeid
== 0)
94 seq_puts(s
, "\"\nMaster Copy\n");
95 else if (res
->res_nodeid
== -1)
96 seq_printf(s
, "\"\nLooking up master (lkid %x)\n",
99 seq_printf(s
, "\"\nInvalid master %d\n", res
->res_nodeid
);
100 if (seq_has_overflowed(s
))
104 if (res
->res_lvbptr
) {
105 seq_puts(s
, "LVB: ");
106 for (i
= 0; i
< lvblen
; i
++) {
109 seq_printf(s
, "%02x ",
110 (unsigned char) res
->res_lvbptr
[i
]);
112 if (rsb_flag(res
, RSB_VALNOTVALID
))
113 seq_puts(s
, " (INVALID)");
115 if (seq_has_overflowed(s
))
119 root_list
= !list_empty(&res
->res_root_list
);
120 recover_list
= !list_empty(&res
->res_recover_list
);
122 if (root_list
|| recover_list
) {
123 seq_printf(s
, "Recovery: root %d recover %d flags %lx count %d\n",
124 root_list
, recover_list
,
125 res
->res_flags
, res
->res_recover_locks_count
);
128 /* Print the locks attached to this resource */
129 seq_puts(s
, "Granted Queue\n");
130 list_for_each_entry(lkb
, &res
->res_grantqueue
, lkb_statequeue
) {
131 print_format1_lock(s
, lkb
, res
);
132 if (seq_has_overflowed(s
))
136 seq_puts(s
, "Conversion Queue\n");
137 list_for_each_entry(lkb
, &res
->res_convertqueue
, lkb_statequeue
) {
138 print_format1_lock(s
, lkb
, res
);
139 if (seq_has_overflowed(s
))
143 seq_puts(s
, "Waiting Queue\n");
144 list_for_each_entry(lkb
, &res
->res_waitqueue
, lkb_statequeue
) {
145 print_format1_lock(s
, lkb
, res
);
146 if (seq_has_overflowed(s
))
150 if (list_empty(&res
->res_lookup
))
153 seq_puts(s
, "Lookup Queue\n");
154 list_for_each_entry(lkb
, &res
->res_lookup
, lkb_rsb_lookup
) {
155 seq_printf(s
, "%08x %s",
156 lkb
->lkb_id
, print_lockmode(lkb
->lkb_rqmode
));
157 if (lkb
->lkb_wait_type
)
158 seq_printf(s
, " wait_type: %d", lkb
->lkb_wait_type
);
160 if (seq_has_overflowed(s
))
167 static void print_format2_lock(struct seq_file
*s
, struct dlm_lkb
*lkb
,
173 if (lkb
->lkb_flags
& DLM_IFL_USER
) {
175 xid
= lkb
->lkb_ua
->xid
;
178 /* microseconds since lkb was added to current queue */
179 us
= ktime_to_us(ktime_sub(ktime_get(), lkb
->lkb_timestamp
));
181 /* id nodeid remid pid xid exflags flags sts grmode rqmode time_us
182 r_nodeid r_len r_name */
184 seq_printf(s
, "%x %d %x %u %llu %x %x %d %d %d %llu %u %d \"%s\"\n",
189 (unsigned long long)xid
,
195 (unsigned long long)us
,
201 static void print_format2(struct dlm_rsb
*r
, struct seq_file
*s
)
207 list_for_each_entry(lkb
, &r
->res_grantqueue
, lkb_statequeue
) {
208 print_format2_lock(s
, lkb
, r
);
209 if (seq_has_overflowed(s
))
213 list_for_each_entry(lkb
, &r
->res_convertqueue
, lkb_statequeue
) {
214 print_format2_lock(s
, lkb
, r
);
215 if (seq_has_overflowed(s
))
219 list_for_each_entry(lkb
, &r
->res_waitqueue
, lkb_statequeue
) {
220 print_format2_lock(s
, lkb
, r
);
221 if (seq_has_overflowed(s
))
228 static void print_format3_lock(struct seq_file
*s
, struct dlm_lkb
*lkb
,
233 if (lkb
->lkb_flags
& DLM_IFL_USER
) {
235 xid
= lkb
->lkb_ua
->xid
;
238 seq_printf(s
, "lkb %x %d %x %u %llu %x %x %d %d %d %d %d %d %u %llu %llu\n",
243 (unsigned long long)xid
,
249 lkb
->lkb_last_bast
.mode
,
253 (unsigned long long)ktime_to_ns(lkb
->lkb_timestamp
),
254 (unsigned long long)ktime_to_ns(lkb
->lkb_last_bast_time
));
257 static void print_format3(struct dlm_rsb
*r
, struct seq_file
*s
)
260 int i
, lvblen
= r
->res_ls
->ls_lvblen
;
265 seq_printf(s
, "rsb %p %d %x %lx %d %d %u %d ",
270 !list_empty(&r
->res_root_list
),
271 !list_empty(&r
->res_recover_list
),
272 r
->res_recover_locks_count
,
274 if (seq_has_overflowed(s
))
277 for (i
= 0; i
< r
->res_length
; i
++) {
278 if (!isascii(r
->res_name
[i
]) || !isprint(r
->res_name
[i
]))
282 seq_puts(s
, print_name
? "str " : "hex");
284 for (i
= 0; i
< r
->res_length
; i
++) {
286 seq_printf(s
, "%c", r
->res_name
[i
]);
288 seq_printf(s
, " %02x", (unsigned char)r
->res_name
[i
]);
291 if (seq_has_overflowed(s
))
297 seq_printf(s
, "lvb %u %d", r
->res_lvbseq
, lvblen
);
299 for (i
= 0; i
< lvblen
; i
++)
300 seq_printf(s
, " %02x", (unsigned char)r
->res_lvbptr
[i
]);
302 if (seq_has_overflowed(s
))
306 list_for_each_entry(lkb
, &r
->res_grantqueue
, lkb_statequeue
) {
307 print_format3_lock(s
, lkb
, 0);
308 if (seq_has_overflowed(s
))
312 list_for_each_entry(lkb
, &r
->res_convertqueue
, lkb_statequeue
) {
313 print_format3_lock(s
, lkb
, 0);
314 if (seq_has_overflowed(s
))
318 list_for_each_entry(lkb
, &r
->res_waitqueue
, lkb_statequeue
) {
319 print_format3_lock(s
, lkb
, 0);
320 if (seq_has_overflowed(s
))
324 list_for_each_entry(lkb
, &r
->res_lookup
, lkb_rsb_lookup
) {
325 print_format3_lock(s
, lkb
, 1);
326 if (seq_has_overflowed(s
))
333 static void print_format4(struct dlm_rsb
*r
, struct seq_file
*s
)
335 int our_nodeid
= dlm_our_nodeid();
341 seq_printf(s
, "rsb %p %d %d %d %d %lu %lx %d ",
344 r
->res_master_nodeid
,
351 for (i
= 0; i
< r
->res_length
; i
++) {
352 if (!isascii(r
->res_name
[i
]) || !isprint(r
->res_name
[i
]))
356 seq_puts(s
, print_name
? "str " : "hex");
358 for (i
= 0; i
< r
->res_length
; i
++) {
360 seq_printf(s
, "%c", r
->res_name
[i
]);
362 seq_printf(s
, " %02x", (unsigned char)r
->res_name
[i
]);
376 * If the buffer is full, seq_printf can be called again, but it
377 * does nothing. So, the these printing routines periodically check
378 * seq_has_overflowed to avoid wasting too much time trying to print to
382 static int table_seq_show(struct seq_file
*seq
, void *iter_ptr
)
384 struct rsbtbl_iter
*ri
= iter_ptr
;
386 switch (ri
->format
) {
388 print_format1(ri
->rsb
, seq
);
392 seq_puts(seq
, "id nodeid remid pid xid exflags flags sts grmode rqmode time_ms r_nodeid r_len r_name\n");
395 print_format2(ri
->rsb
, seq
);
399 seq_puts(seq
, "version rsb 1.1 lvb 1.1 lkb 1.1\n");
402 print_format3(ri
->rsb
, seq
);
406 seq_puts(seq
, "version 4 rsb 2\n");
409 print_format4(ri
->rsb
, seq
);
416 static const struct seq_operations format1_seq_ops
;
417 static const struct seq_operations format2_seq_ops
;
418 static const struct seq_operations format3_seq_ops
;
419 static const struct seq_operations format4_seq_ops
;
421 static void *table_seq_start(struct seq_file
*seq
, loff_t
*pos
)
423 struct rb_root
*tree
;
424 struct rb_node
*node
;
425 struct dlm_ls
*ls
= seq
->private;
426 struct rsbtbl_iter
*ri
;
429 unsigned bucket
, entry
;
430 int toss
= (seq
->op
== &format4_seq_ops
);
433 entry
= n
& ((1LL << 32) - 1);
435 if (bucket
>= ls
->ls_rsbtbl_size
)
438 ri
= kzalloc(sizeof(*ri
), GFP_NOFS
);
443 if (seq
->op
== &format1_seq_ops
)
445 if (seq
->op
== &format2_seq_ops
)
447 if (seq
->op
== &format3_seq_ops
)
449 if (seq
->op
== &format4_seq_ops
)
452 tree
= toss
? &ls
->ls_rsbtbl
[bucket
].toss
: &ls
->ls_rsbtbl
[bucket
].keep
;
454 spin_lock(&ls
->ls_rsbtbl
[bucket
].lock
);
455 if (!RB_EMPTY_ROOT(tree
)) {
456 for (node
= rb_first(tree
); node
; node
= rb_next(node
)) {
457 r
= rb_entry(node
, struct dlm_rsb
, res_hashnode
);
462 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
467 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
470 * move to the first rsb in the next non-empty bucket
474 n
&= ~((1LL << 32) - 1);
480 if (bucket
>= ls
->ls_rsbtbl_size
) {
484 tree
= toss
? &ls
->ls_rsbtbl
[bucket
].toss
: &ls
->ls_rsbtbl
[bucket
].keep
;
486 spin_lock(&ls
->ls_rsbtbl
[bucket
].lock
);
487 if (!RB_EMPTY_ROOT(tree
)) {
488 node
= rb_first(tree
);
489 r
= rb_entry(node
, struct dlm_rsb
, res_hashnode
);
493 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
497 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
501 static void *table_seq_next(struct seq_file
*seq
, void *iter_ptr
, loff_t
*pos
)
503 struct dlm_ls
*ls
= seq
->private;
504 struct rsbtbl_iter
*ri
= iter_ptr
;
505 struct rb_root
*tree
;
506 struct rb_node
*next
;
507 struct dlm_rsb
*r
, *rp
;
510 int toss
= (seq
->op
== &format4_seq_ops
);
515 * move to the next rsb in the same bucket
518 spin_lock(&ls
->ls_rsbtbl
[bucket
].lock
);
520 next
= rb_next(&rp
->res_hashnode
);
523 r
= rb_entry(next
, struct dlm_rsb
, res_hashnode
);
526 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
531 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
535 * move to the first rsb in the next non-empty bucket
539 n
&= ~((1LL << 32) - 1);
545 if (bucket
>= ls
->ls_rsbtbl_size
) {
550 tree
= toss
? &ls
->ls_rsbtbl
[bucket
].toss
: &ls
->ls_rsbtbl
[bucket
].keep
;
552 spin_lock(&ls
->ls_rsbtbl
[bucket
].lock
);
553 if (!RB_EMPTY_ROOT(tree
)) {
554 next
= rb_first(tree
);
555 r
= rb_entry(next
, struct dlm_rsb
, res_hashnode
);
559 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
563 spin_unlock(&ls
->ls_rsbtbl
[bucket
].lock
);
567 static void table_seq_stop(struct seq_file
*seq
, void *iter_ptr
)
569 struct rsbtbl_iter
*ri
= iter_ptr
;
572 dlm_put_rsb(ri
->rsb
);
577 static const struct seq_operations format1_seq_ops
= {
578 .start
= table_seq_start
,
579 .next
= table_seq_next
,
580 .stop
= table_seq_stop
,
581 .show
= table_seq_show
,
584 static const struct seq_operations format2_seq_ops
= {
585 .start
= table_seq_start
,
586 .next
= table_seq_next
,
587 .stop
= table_seq_stop
,
588 .show
= table_seq_show
,
591 static const struct seq_operations format3_seq_ops
= {
592 .start
= table_seq_start
,
593 .next
= table_seq_next
,
594 .stop
= table_seq_stop
,
595 .show
= table_seq_show
,
598 static const struct seq_operations format4_seq_ops
= {
599 .start
= table_seq_start
,
600 .next
= table_seq_next
,
601 .stop
= table_seq_stop
,
602 .show
= table_seq_show
,
605 static const struct file_operations format1_fops
;
606 static const struct file_operations format2_fops
;
607 static const struct file_operations format3_fops
;
608 static const struct file_operations format4_fops
;
610 static int table_open1(struct inode
*inode
, struct file
*file
)
612 struct seq_file
*seq
;
615 ret
= seq_open(file
, &format1_seq_ops
);
619 seq
= file
->private_data
;
620 seq
->private = inode
->i_private
; /* the dlm_ls */
624 static int table_open2(struct inode
*inode
, struct file
*file
)
626 struct seq_file
*seq
;
629 ret
= seq_open(file
, &format2_seq_ops
);
633 seq
= file
->private_data
;
634 seq
->private = inode
->i_private
; /* the dlm_ls */
638 static ssize_t
table_write2(struct file
*file
, const char __user
*user_buf
,
639 size_t count
, loff_t
*ppos
)
641 struct seq_file
*seq
= file
->private_data
;
642 int n
, len
, lkb_nodeid
, lkb_status
, error
;
643 char name
[DLM_RESNAME_MAXLEN
+ 1] = {};
644 struct dlm_ls
*ls
= seq
->private;
645 unsigned int lkb_flags
;
649 if (copy_from_user(buf
, user_buf
,
650 min_t(size_t, sizeof(buf
) - 1, count
)))
653 n
= sscanf(buf
, "%x %" __stringify(DLM_RESNAME_MAXLEN
) "s %x %d %d",
654 &lkb_id
, name
, &lkb_flags
, &lkb_nodeid
, &lkb_status
);
658 len
= strnlen(name
, DLM_RESNAME_MAXLEN
);
659 error
= dlm_debug_add_lkb(ls
, lkb_id
, name
, len
, lkb_flags
,
660 lkb_nodeid
, lkb_status
);
667 static int table_open3(struct inode
*inode
, struct file
*file
)
669 struct seq_file
*seq
;
672 ret
= seq_open(file
, &format3_seq_ops
);
676 seq
= file
->private_data
;
677 seq
->private = inode
->i_private
; /* the dlm_ls */
681 static int table_open4(struct inode
*inode
, struct file
*file
)
683 struct seq_file
*seq
;
686 ret
= seq_open(file
, &format4_seq_ops
);
690 seq
= file
->private_data
;
691 seq
->private = inode
->i_private
; /* the dlm_ls */
695 static const struct file_operations format1_fops
= {
696 .owner
= THIS_MODULE
,
700 .release
= seq_release
703 static const struct file_operations format2_fops
= {
704 .owner
= THIS_MODULE
,
707 .write
= table_write2
,
709 .release
= seq_release
712 static const struct file_operations format3_fops
= {
713 .owner
= THIS_MODULE
,
717 .release
= seq_release
720 static const struct file_operations format4_fops
= {
721 .owner
= THIS_MODULE
,
725 .release
= seq_release
729 * dump lkb's on the ls_waiters list
731 static ssize_t
waiters_read(struct file
*file
, char __user
*userbuf
,
732 size_t count
, loff_t
*ppos
)
734 struct dlm_ls
*ls
= file
->private_data
;
736 size_t len
= DLM_DEBUG_BUF_LEN
, pos
= 0, ret
, rv
;
738 mutex_lock(&debug_buf_lock
);
739 mutex_lock(&ls
->ls_waiters_mutex
);
740 memset(debug_buf
, 0, sizeof(debug_buf
));
742 list_for_each_entry(lkb
, &ls
->ls_waiters
, lkb_wait_reply
) {
743 ret
= snprintf(debug_buf
+ pos
, len
- pos
, "%x %d %d %s\n",
744 lkb
->lkb_id
, lkb
->lkb_wait_type
,
745 lkb
->lkb_nodeid
, lkb
->lkb_resource
->res_name
);
746 if (ret
>= len
- pos
)
750 mutex_unlock(&ls
->ls_waiters_mutex
);
752 rv
= simple_read_from_buffer(userbuf
, count
, ppos
, debug_buf
, pos
);
753 mutex_unlock(&debug_buf_lock
);
757 static ssize_t
waiters_write(struct file
*file
, const char __user
*user_buf
,
758 size_t count
, loff_t
*ppos
)
760 struct dlm_ls
*ls
= file
->private_data
;
761 int mstype
, to_nodeid
;
766 if (copy_from_user(buf
, user_buf
,
767 min_t(size_t, sizeof(buf
) - 1, count
)))
770 n
= sscanf(buf
, "%x %d %d", &lkb_id
, &mstype
, &to_nodeid
);
774 error
= dlm_debug_add_lkb_to_waiters(ls
, lkb_id
, mstype
, to_nodeid
);
781 static const struct file_operations waiters_fops
= {
782 .owner
= THIS_MODULE
,
784 .read
= waiters_read
,
785 .write
= waiters_write
,
786 .llseek
= default_llseek
,
789 void dlm_delete_debug_file(struct dlm_ls
*ls
)
791 debugfs_remove(ls
->ls_debug_rsb_dentry
);
792 debugfs_remove(ls
->ls_debug_waiters_dentry
);
793 debugfs_remove(ls
->ls_debug_locks_dentry
);
794 debugfs_remove(ls
->ls_debug_all_dentry
);
795 debugfs_remove(ls
->ls_debug_toss_dentry
);
798 static int dlm_state_show(struct seq_file
*file
, void *offset
)
800 seq_printf(file
, "%s\n", dlm_midcomms_state(file
->private));
803 DEFINE_SHOW_ATTRIBUTE(dlm_state
);
805 static int dlm_flags_show(struct seq_file
*file
, void *offset
)
807 seq_printf(file
, "%lu\n", dlm_midcomms_flags(file
->private));
810 DEFINE_SHOW_ATTRIBUTE(dlm_flags
);
812 static int dlm_send_queue_cnt_show(struct seq_file
*file
, void *offset
)
814 seq_printf(file
, "%d\n", dlm_midcomms_send_queue_cnt(file
->private));
817 DEFINE_SHOW_ATTRIBUTE(dlm_send_queue_cnt
);
819 static int dlm_version_show(struct seq_file
*file
, void *offset
)
821 seq_printf(file
, "0x%08x\n", dlm_midcomms_version(file
->private));
824 DEFINE_SHOW_ATTRIBUTE(dlm_version
);
826 static ssize_t
dlm_rawmsg_write(struct file
*fp
, const char __user
*user_buf
,
827 size_t count
, loff_t
*ppos
)
832 if (count
> PAGE_SIZE
|| count
< sizeof(struct dlm_header
))
835 buf
= kmalloc(PAGE_SIZE
, GFP_NOFS
);
839 if (copy_from_user(buf
, user_buf
, count
)) {
844 ret
= dlm_midcomms_rawmsg_send(fp
->private_data
, buf
, count
);
856 static const struct file_operations dlm_rawmsg_fops
= {
858 .write
= dlm_rawmsg_write
,
862 void *dlm_create_debug_comms_file(int nodeid
, void *data
)
864 struct dentry
*d_node
;
867 memset(name
, 0, sizeof(name
));
868 snprintf(name
, 256, "%d", nodeid
);
870 d_node
= debugfs_create_dir(name
, dlm_comms
);
871 debugfs_create_file("state", 0444, d_node
, data
, &dlm_state_fops
);
872 debugfs_create_file("flags", 0444, d_node
, data
, &dlm_flags_fops
);
873 debugfs_create_file("send_queue_count", 0444, d_node
, data
,
874 &dlm_send_queue_cnt_fops
);
875 debugfs_create_file("version", 0444, d_node
, data
, &dlm_version_fops
);
876 debugfs_create_file("rawmsg", 0200, d_node
, data
, &dlm_rawmsg_fops
);
881 void dlm_delete_debug_comms_file(void *ctx
)
886 void dlm_create_debug_file(struct dlm_ls
*ls
)
888 char name
[DLM_LOCKSPACE_LEN
+ 8];
892 ls
->ls_debug_rsb_dentry
= debugfs_create_file(ls
->ls_name
,
900 memset(name
, 0, sizeof(name
));
901 snprintf(name
, DLM_LOCKSPACE_LEN
+ 8, "%s_locks", ls
->ls_name
);
903 ls
->ls_debug_locks_dentry
= debugfs_create_file(name
,
911 memset(name
, 0, sizeof(name
));
912 snprintf(name
, DLM_LOCKSPACE_LEN
+ 8, "%s_all", ls
->ls_name
);
914 ls
->ls_debug_all_dentry
= debugfs_create_file(name
,
922 memset(name
, 0, sizeof(name
));
923 snprintf(name
, DLM_LOCKSPACE_LEN
+ 8, "%s_toss", ls
->ls_name
);
925 ls
->ls_debug_toss_dentry
= debugfs_create_file(name
,
931 memset(name
, 0, sizeof(name
));
932 snprintf(name
, DLM_LOCKSPACE_LEN
+ 8, "%s_waiters", ls
->ls_name
);
934 ls
->ls_debug_waiters_dentry
= debugfs_create_file(name
,
941 void __init
dlm_register_debugfs(void)
943 mutex_init(&debug_buf_lock
);
944 dlm_root
= debugfs_create_dir("dlm", NULL
);
945 dlm_comms
= debugfs_create_dir("comms", dlm_root
);
948 void dlm_unregister_debugfs(void)
950 debugfs_remove(dlm_root
);