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"
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;
28 static struct dentry *dlm_comms;
30 static char *print_lockmode(int mode)
52 static void print_format1_lock(struct seq_file *s, struct dlm_lkb *lkb,
55 seq_printf(s, "%08x %s", lkb->lkb_id, print_lockmode(lkb->lkb_grmode));
57 if (lkb->lkb_status == DLM_LKSTS_CONVERT ||
58 lkb->lkb_status == DLM_LKSTS_WAITING)
59 seq_printf(s, " (%s)", print_lockmode(lkb->lkb_rqmode));
61 if (lkb->lkb_nodeid) {
62 if (lkb->lkb_nodeid != res->res_nodeid)
63 seq_printf(s, " Remote: %3d %08x", lkb->lkb_nodeid,
66 seq_printf(s, " Master: %08x", lkb->lkb_remid);
69 if (lkb->lkb_wait_type)
70 seq_printf(s, " wait_type: %d", lkb->lkb_wait_type);
75 static void print_format1(struct dlm_rsb *res, struct seq_file *s)
78 int i, lvblen = res->res_ls->ls_lvblen, recover_list, root_list;
82 seq_printf(s, "\nResource %p Name (len=%d) \"", res, res->res_length);
84 for (i = 0; i < res->res_length; i++) {
85 if (isprint(res->res_name[i]))
86 seq_printf(s, "%c", res->res_name[i]);
88 seq_printf(s, "%c", '.');
91 if (res->res_nodeid > 0)
92 seq_printf(s, "\"\nLocal Copy, Master is node %d\n",
94 else if (res->res_nodeid == 0)
95 seq_puts(s, "\"\nMaster Copy\n");
96 else if (res->res_nodeid == -1)
97 seq_printf(s, "\"\nLooking up master (lkid %x)\n",
100 seq_printf(s, "\"\nInvalid master %d\n", res->res_nodeid);
101 if (seq_has_overflowed(s))
105 if (res->res_lvbptr) {
106 seq_puts(s, "LVB: ");
107 for (i = 0; i < lvblen; i++) {
110 seq_printf(s, "%02x ",
111 (unsigned char) res->res_lvbptr[i]);
113 if (rsb_flag(res, RSB_VALNOTVALID))
114 seq_puts(s, " (INVALID)");
116 if (seq_has_overflowed(s))
120 root_list = !list_empty(&res->res_root_list);
121 recover_list = !list_empty(&res->res_recover_list);
123 if (root_list || recover_list) {
124 seq_printf(s, "Recovery: root %d recover %d flags %lx count %d\n",
125 root_list, recover_list,
126 res->res_flags, res->res_recover_locks_count);
129 /* Print the locks attached to this resource */
130 seq_puts(s, "Granted Queue\n");
131 list_for_each_entry(lkb, &res->res_grantqueue, lkb_statequeue) {
132 print_format1_lock(s, lkb, res);
133 if (seq_has_overflowed(s))
137 seq_puts(s, "Conversion Queue\n");
138 list_for_each_entry(lkb, &res->res_convertqueue, lkb_statequeue) {
139 print_format1_lock(s, lkb, res);
140 if (seq_has_overflowed(s))
144 seq_puts(s, "Waiting Queue\n");
145 list_for_each_entry(lkb, &res->res_waitqueue, lkb_statequeue) {
146 print_format1_lock(s, lkb, res);
147 if (seq_has_overflowed(s))
151 if (list_empty(&res->res_lookup))
154 seq_puts(s, "Lookup Queue\n");
155 list_for_each_entry(lkb, &res->res_lookup, lkb_rsb_lookup) {
156 seq_printf(s, "%08x %s",
157 lkb->lkb_id, print_lockmode(lkb->lkb_rqmode));
158 if (lkb->lkb_wait_type)
159 seq_printf(s, " wait_type: %d", lkb->lkb_wait_type);
161 if (seq_has_overflowed(s))
168 static void print_format2_lock(struct seq_file *s, struct dlm_lkb *lkb,
174 if (test_bit(DLM_DFL_USER_BIT, &lkb->lkb_dflags)) {
176 xid = lkb->lkb_ua->xid;
179 /* microseconds since lkb was added to current queue */
180 us = ktime_to_us(ktime_sub(ktime_get(), lkb->lkb_timestamp));
182 /* id nodeid remid pid xid exflags flags sts grmode rqmode time_us
183 r_nodeid r_len r_name */
185 seq_printf(s, "%x %d %x %u %llu %x %x %d %d %d %llu %u %d \"%s\"\n",
190 (unsigned long long)xid,
196 (unsigned long long)us,
202 static void print_format2(struct dlm_rsb *r, struct seq_file *s)
208 list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
209 print_format2_lock(s, lkb, r);
210 if (seq_has_overflowed(s))
214 list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
215 print_format2_lock(s, lkb, r);
216 if (seq_has_overflowed(s))
220 list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue) {
221 print_format2_lock(s, lkb, r);
222 if (seq_has_overflowed(s))
229 static void print_format3_lock(struct seq_file *s, struct dlm_lkb *lkb,
234 if (test_bit(DLM_DFL_USER_BIT, &lkb->lkb_dflags)) {
236 xid = lkb->lkb_ua->xid;
239 seq_printf(s, "lkb %x %d %x %u %llu %x %x %d %d %d %d %d %d %u %llu %llu\n",
244 (unsigned long long)xid,
250 lkb->lkb_last_bast_mode,
254 (unsigned long long)ktime_to_ns(lkb->lkb_timestamp),
255 (unsigned long long)ktime_to_ns(lkb->lkb_last_bast_time));
258 static void print_format3(struct dlm_rsb *r, struct seq_file *s)
261 int i, lvblen = r->res_ls->ls_lvblen;
266 seq_printf(s, "rsb %p %d %x %lx %d %d %u %d ",
271 !list_empty(&r->res_root_list),
272 !list_empty(&r->res_recover_list),
273 r->res_recover_locks_count,
275 if (seq_has_overflowed(s))
278 for (i = 0; i < r->res_length; i++) {
279 if (!isascii(r->res_name[i]) || !isprint(r->res_name[i]))
283 seq_puts(s, print_name ? "str " : "hex");
285 for (i = 0; i < r->res_length; i++) {
287 seq_printf(s, "%c", r->res_name[i]);
289 seq_printf(s, " %02x", (unsigned char)r->res_name[i]);
292 if (seq_has_overflowed(s))
298 seq_printf(s, "lvb %u %d", r->res_lvbseq, lvblen);
300 for (i = 0; i < lvblen; i++)
301 seq_printf(s, " %02x", (unsigned char)r->res_lvbptr[i]);
303 if (seq_has_overflowed(s))
307 list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
308 print_format3_lock(s, lkb, 0);
309 if (seq_has_overflowed(s))
313 list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
314 print_format3_lock(s, lkb, 0);
315 if (seq_has_overflowed(s))
319 list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue) {
320 print_format3_lock(s, lkb, 0);
321 if (seq_has_overflowed(s))
325 list_for_each_entry(lkb, &r->res_lookup, lkb_rsb_lookup) {
326 print_format3_lock(s, lkb, 1);
327 if (seq_has_overflowed(s))
334 static void print_format4(struct dlm_rsb *r, struct seq_file *s)
336 int our_nodeid = dlm_our_nodeid();
342 seq_printf(s, "rsb %p %d %d %d %d %lu %lx %d ",
345 r->res_master_nodeid,
352 for (i = 0; i < r->res_length; i++) {
353 if (!isascii(r->res_name[i]) || !isprint(r->res_name[i]))
357 seq_puts(s, print_name ? "str " : "hex");
359 for (i = 0; i < r->res_length; i++) {
361 seq_printf(s, "%c", r->res_name[i]);
363 seq_printf(s, " %02x", (unsigned char)r->res_name[i]);
369 static void print_format5_lock(struct seq_file *s, struct dlm_lkb *lkb)
371 struct dlm_callback *cb;
373 /* lkb_id lkb_flags mode flags sb_status sb_flags */
375 spin_lock(&lkb->lkb_cb_lock);
376 list_for_each_entry(cb, &lkb->lkb_callbacks, list) {
377 seq_printf(s, "%x %x %d %x %d %x\n",
385 spin_unlock(&lkb->lkb_cb_lock);
388 static void print_format5(struct dlm_rsb *r, struct seq_file *s)
394 list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
395 print_format5_lock(s, lkb);
396 if (seq_has_overflowed(s))
400 list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
401 print_format5_lock(s, lkb);
402 if (seq_has_overflowed(s))
406 list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue) {
407 print_format5_lock(s, lkb);
408 if (seq_has_overflowed(s))
423 * If the buffer is full, seq_printf can be called again, but it
424 * does nothing. So, the these printing routines periodically check
425 * seq_has_overflowed to avoid wasting too much time trying to print to
429 static int table_seq_show(struct seq_file *seq, void *iter_ptr)
431 struct rsbtbl_iter *ri = iter_ptr;
433 switch (ri->format) {
435 print_format1(ri->rsb, seq);
439 seq_puts(seq, "id nodeid remid pid xid exflags flags sts grmode rqmode time_ms r_nodeid r_len r_name\n");
442 print_format2(ri->rsb, seq);
446 seq_puts(seq, "version rsb 1.1 lvb 1.1 lkb 1.1\n");
449 print_format3(ri->rsb, seq);
453 seq_puts(seq, "version 4 rsb 2\n");
456 print_format4(ri->rsb, seq);
460 seq_puts(seq, "lkb_id lkb_flags mode flags sb_status sb_flags\n");
463 print_format5(ri->rsb, seq);
470 static const struct seq_operations format1_seq_ops;
471 static const struct seq_operations format2_seq_ops;
472 static const struct seq_operations format3_seq_ops;
473 static const struct seq_operations format4_seq_ops;
474 static const struct seq_operations format5_seq_ops;
476 static void *table_seq_start(struct seq_file *seq, loff_t *pos)
478 struct rb_root *tree;
479 struct rb_node *node;
480 struct dlm_ls *ls = seq->private;
481 struct rsbtbl_iter *ri;
484 unsigned bucket, entry;
485 int toss = (seq->op == &format4_seq_ops);
488 entry = n & ((1LL << 32) - 1);
490 if (bucket >= ls->ls_rsbtbl_size)
493 ri = kzalloc(sizeof(*ri), GFP_NOFS);
498 if (seq->op == &format1_seq_ops)
500 if (seq->op == &format2_seq_ops)
502 if (seq->op == &format3_seq_ops)
504 if (seq->op == &format4_seq_ops)
506 if (seq->op == &format5_seq_ops)
509 tree = toss ? &ls->ls_rsbtbl[bucket].toss : &ls->ls_rsbtbl[bucket].keep;
511 spin_lock(&ls->ls_rsbtbl[bucket].lock);
512 if (!RB_EMPTY_ROOT(tree)) {
513 for (node = rb_first(tree); node; node = rb_next(node)) {
514 r = rb_entry(node, struct dlm_rsb, res_hashnode);
519 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
524 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
527 * move to the first rsb in the next non-empty bucket
531 n &= ~((1LL << 32) - 1);
537 if (bucket >= ls->ls_rsbtbl_size) {
541 tree = toss ? &ls->ls_rsbtbl[bucket].toss : &ls->ls_rsbtbl[bucket].keep;
543 spin_lock(&ls->ls_rsbtbl[bucket].lock);
544 if (!RB_EMPTY_ROOT(tree)) {
545 node = rb_first(tree);
546 r = rb_entry(node, struct dlm_rsb, res_hashnode);
550 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
554 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
558 static void *table_seq_next(struct seq_file *seq, void *iter_ptr, loff_t *pos)
560 struct dlm_ls *ls = seq->private;
561 struct rsbtbl_iter *ri = iter_ptr;
562 struct rb_root *tree;
563 struct rb_node *next;
564 struct dlm_rsb *r, *rp;
567 int toss = (seq->op == &format4_seq_ops);
572 * move to the next rsb in the same bucket
575 spin_lock(&ls->ls_rsbtbl[bucket].lock);
577 next = rb_next(&rp->res_hashnode);
580 r = rb_entry(next, struct dlm_rsb, res_hashnode);
583 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
588 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
592 * move to the first rsb in the next non-empty bucket
596 n &= ~((1LL << 32) - 1);
602 if (bucket >= ls->ls_rsbtbl_size) {
607 tree = toss ? &ls->ls_rsbtbl[bucket].toss : &ls->ls_rsbtbl[bucket].keep;
609 spin_lock(&ls->ls_rsbtbl[bucket].lock);
610 if (!RB_EMPTY_ROOT(tree)) {
611 next = rb_first(tree);
612 r = rb_entry(next, struct dlm_rsb, res_hashnode);
616 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
620 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
624 static void table_seq_stop(struct seq_file *seq, void *iter_ptr)
626 struct rsbtbl_iter *ri = iter_ptr;
629 dlm_put_rsb(ri->rsb);
634 static const struct seq_operations format1_seq_ops = {
635 .start = table_seq_start,
636 .next = table_seq_next,
637 .stop = table_seq_stop,
638 .show = table_seq_show,
641 static const struct seq_operations format2_seq_ops = {
642 .start = table_seq_start,
643 .next = table_seq_next,
644 .stop = table_seq_stop,
645 .show = table_seq_show,
648 static const struct seq_operations format3_seq_ops = {
649 .start = table_seq_start,
650 .next = table_seq_next,
651 .stop = table_seq_stop,
652 .show = table_seq_show,
655 static const struct seq_operations format4_seq_ops = {
656 .start = table_seq_start,
657 .next = table_seq_next,
658 .stop = table_seq_stop,
659 .show = table_seq_show,
662 static const struct seq_operations format5_seq_ops = {
663 .start = table_seq_start,
664 .next = table_seq_next,
665 .stop = table_seq_stop,
666 .show = table_seq_show,
669 static const struct file_operations format1_fops;
670 static const struct file_operations format2_fops;
671 static const struct file_operations format3_fops;
672 static const struct file_operations format4_fops;
673 static const struct file_operations format5_fops;
675 static int table_open1(struct inode *inode, struct file *file)
677 struct seq_file *seq;
680 ret = seq_open(file, &format1_seq_ops);
684 seq = file->private_data;
685 seq->private = inode->i_private; /* the dlm_ls */
689 static int table_open2(struct inode *inode, struct file *file)
691 struct seq_file *seq;
694 ret = seq_open(file, &format2_seq_ops);
698 seq = file->private_data;
699 seq->private = inode->i_private; /* the dlm_ls */
703 static ssize_t table_write2(struct file *file, const char __user *user_buf,
704 size_t count, loff_t *ppos)
706 struct seq_file *seq = file->private_data;
707 int n, len, lkb_nodeid, lkb_status, error;
708 char name[DLM_RESNAME_MAXLEN + 1] = {};
709 struct dlm_ls *ls = seq->private;
710 unsigned int lkb_flags;
714 if (copy_from_user(buf, user_buf,
715 min_t(size_t, sizeof(buf) - 1, count)))
718 n = sscanf(buf, "%x %" __stringify(DLM_RESNAME_MAXLEN) "s %x %d %d",
719 &lkb_id, name, &lkb_flags, &lkb_nodeid, &lkb_status);
723 len = strnlen(name, DLM_RESNAME_MAXLEN);
724 error = dlm_debug_add_lkb(ls, lkb_id, name, len, lkb_flags,
725 lkb_nodeid, lkb_status);
732 static int table_open3(struct inode *inode, struct file *file)
734 struct seq_file *seq;
737 ret = seq_open(file, &format3_seq_ops);
741 seq = file->private_data;
742 seq->private = inode->i_private; /* the dlm_ls */
746 static int table_open4(struct inode *inode, struct file *file)
748 struct seq_file *seq;
751 ret = seq_open(file, &format5_seq_ops);
755 seq = file->private_data;
756 seq->private = inode->i_private; /* the dlm_ls */
760 static int table_open5(struct inode *inode, struct file *file)
762 struct seq_file *seq;
765 ret = seq_open(file, &format5_seq_ops);
769 seq = file->private_data;
770 seq->private = inode->i_private; /* the dlm_ls */
774 static const struct file_operations format1_fops = {
775 .owner = THIS_MODULE,
779 .release = seq_release
782 static const struct file_operations format2_fops = {
783 .owner = THIS_MODULE,
786 .write = table_write2,
788 .release = seq_release
791 static const struct file_operations format3_fops = {
792 .owner = THIS_MODULE,
796 .release = seq_release
799 static const struct file_operations format4_fops = {
800 .owner = THIS_MODULE,
804 .release = seq_release
807 static const struct file_operations format5_fops = {
808 .owner = THIS_MODULE,
812 .release = seq_release
816 * dump lkb's on the ls_waiters list
818 static ssize_t waiters_read(struct file *file, char __user *userbuf,
819 size_t count, loff_t *ppos)
821 struct dlm_ls *ls = file->private_data;
823 size_t len = DLM_DEBUG_BUF_LEN, pos = 0, ret, rv;
825 mutex_lock(&debug_buf_lock);
826 mutex_lock(&ls->ls_waiters_mutex);
827 memset(debug_buf, 0, sizeof(debug_buf));
829 list_for_each_entry(lkb, &ls->ls_waiters, lkb_wait_reply) {
830 ret = snprintf(debug_buf + pos, len - pos, "%x %d %d %s\n",
831 lkb->lkb_id, lkb->lkb_wait_type,
832 lkb->lkb_nodeid, lkb->lkb_resource->res_name);
833 if (ret >= len - pos)
837 mutex_unlock(&ls->ls_waiters_mutex);
839 rv = simple_read_from_buffer(userbuf, count, ppos, debug_buf, pos);
840 mutex_unlock(&debug_buf_lock);
844 static ssize_t waiters_write(struct file *file, const char __user *user_buf,
845 size_t count, loff_t *ppos)
847 struct dlm_ls *ls = file->private_data;
848 int mstype, to_nodeid;
853 if (copy_from_user(buf, user_buf,
854 min_t(size_t, sizeof(buf) - 1, count)))
857 n = sscanf(buf, "%x %d %d", &lkb_id, &mstype, &to_nodeid);
861 error = dlm_debug_add_lkb_to_waiters(ls, lkb_id, mstype, to_nodeid);
868 static const struct file_operations waiters_fops = {
869 .owner = THIS_MODULE,
871 .read = waiters_read,
872 .write = waiters_write,
873 .llseek = default_llseek,
876 void dlm_delete_debug_file(struct dlm_ls *ls)
878 debugfs_remove(ls->ls_debug_rsb_dentry);
879 debugfs_remove(ls->ls_debug_waiters_dentry);
880 debugfs_remove(ls->ls_debug_locks_dentry);
881 debugfs_remove(ls->ls_debug_all_dentry);
882 debugfs_remove(ls->ls_debug_toss_dentry);
883 debugfs_remove(ls->ls_debug_queued_asts_dentry);
886 static int dlm_state_show(struct seq_file *file, void *offset)
888 seq_printf(file, "%s\n", dlm_midcomms_state(file->private));
891 DEFINE_SHOW_ATTRIBUTE(dlm_state);
893 static int dlm_flags_show(struct seq_file *file, void *offset)
895 seq_printf(file, "%lu\n", dlm_midcomms_flags(file->private));
898 DEFINE_SHOW_ATTRIBUTE(dlm_flags);
900 static int dlm_send_queue_cnt_show(struct seq_file *file, void *offset)
902 seq_printf(file, "%d\n", dlm_midcomms_send_queue_cnt(file->private));
905 DEFINE_SHOW_ATTRIBUTE(dlm_send_queue_cnt);
907 static int dlm_version_show(struct seq_file *file, void *offset)
909 seq_printf(file, "0x%08x\n", dlm_midcomms_version(file->private));
912 DEFINE_SHOW_ATTRIBUTE(dlm_version);
914 static ssize_t dlm_rawmsg_write(struct file *fp, const char __user *user_buf,
915 size_t count, loff_t *ppos)
920 if (count > PAGE_SIZE || count < sizeof(struct dlm_header))
923 buf = kmalloc(PAGE_SIZE, GFP_NOFS);
927 if (copy_from_user(buf, user_buf, count)) {
932 ret = dlm_midcomms_rawmsg_send(fp->private_data, buf, count);
944 static const struct file_operations dlm_rawmsg_fops = {
946 .write = dlm_rawmsg_write,
950 void *dlm_create_debug_comms_file(int nodeid, void *data)
952 struct dentry *d_node;
955 memset(name, 0, sizeof(name));
956 snprintf(name, 256, "%d", nodeid);
958 d_node = debugfs_create_dir(name, dlm_comms);
959 debugfs_create_file("state", 0444, d_node, data, &dlm_state_fops);
960 debugfs_create_file("flags", 0444, d_node, data, &dlm_flags_fops);
961 debugfs_create_file("send_queue_count", 0444, d_node, data,
962 &dlm_send_queue_cnt_fops);
963 debugfs_create_file("version", 0444, d_node, data, &dlm_version_fops);
964 debugfs_create_file("rawmsg", 0200, d_node, data, &dlm_rawmsg_fops);
969 void dlm_delete_debug_comms_file(void *ctx)
974 void dlm_create_debug_file(struct dlm_ls *ls)
976 /* Reserve enough space for the longest file name */
977 char name[DLM_LOCKSPACE_LEN + sizeof("_queued_asts")];
981 ls->ls_debug_rsb_dentry = debugfs_create_file(ls->ls_name,
989 snprintf(name, sizeof(name), "%s_locks", ls->ls_name);
991 ls->ls_debug_locks_dentry = debugfs_create_file(name,
999 snprintf(name, sizeof(name), "%s_all", ls->ls_name);
1001 ls->ls_debug_all_dentry = debugfs_create_file(name,
1009 snprintf(name, sizeof(name), "%s_toss", ls->ls_name);
1011 ls->ls_debug_toss_dentry = debugfs_create_file(name,
1017 snprintf(name, sizeof(name), "%s_waiters", ls->ls_name);
1019 ls->ls_debug_waiters_dentry = debugfs_create_file(name,
1027 snprintf(name, sizeof(name), "%s_queued_asts", ls->ls_name);
1029 ls->ls_debug_queued_asts_dentry = debugfs_create_file(name,
1036 void __init dlm_register_debugfs(void)
1038 mutex_init(&debug_buf_lock);
1039 dlm_root = debugfs_create_dir("dlm", NULL);
1040 dlm_comms = debugfs_create_dir("comms", dlm_root);
1043 void dlm_unregister_debugfs(void)
1045 debugfs_remove(dlm_root);