1 /******************************************************************************
2 *******************************************************************************
4 ** Copyright (C) 2005 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"
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;
30 struct list_head *next;
35 * dump all rsb's in the lockspace hash table
38 static char *print_lockmode(int mode)
60 static void print_lock(struct seq_file *s, struct dlm_lkb *lkb,
63 seq_printf(s, "%08x %s", lkb->lkb_id, print_lockmode(lkb->lkb_grmode));
65 if (lkb->lkb_status == DLM_LKSTS_CONVERT
66 || lkb->lkb_status == DLM_LKSTS_WAITING)
67 seq_printf(s, " (%s)", print_lockmode(lkb->lkb_rqmode));
69 if (lkb->lkb_nodeid) {
70 if (lkb->lkb_nodeid != res->res_nodeid)
71 seq_printf(s, " Remote: %3d %08x", lkb->lkb_nodeid,
74 seq_printf(s, " Master: %08x", lkb->lkb_remid);
77 if (lkb->lkb_wait_type)
78 seq_printf(s, " wait_type: %d", lkb->lkb_wait_type);
83 static int print_resource(struct dlm_rsb *res, struct seq_file *s)
86 int i, lvblen = res->res_ls->ls_lvblen, recover_list, root_list;
88 seq_printf(s, "\nResource %p Name (len=%d) \"", res, res->res_length);
89 for (i = 0; i < res->res_length; i++) {
90 if (isprint(res->res_name[i]))
91 seq_printf(s, "%c", res->res_name[i]);
93 seq_printf(s, "%c", '.');
95 if (res->res_nodeid > 0)
96 seq_printf(s, "\" \nLocal Copy, Master is node %d\n",
98 else if (res->res_nodeid == 0)
99 seq_printf(s, "\" \nMaster Copy\n");
100 else if (res->res_nodeid == -1)
101 seq_printf(s, "\" \nLooking up master (lkid %x)\n",
102 res->res_first_lkid);
104 seq_printf(s, "\" \nInvalid master %d\n", res->res_nodeid);
107 if (res->res_lvbptr) {
108 seq_printf(s, "LVB: ");
109 for (i = 0; i < lvblen; i++) {
111 seq_printf(s, "\n ");
112 seq_printf(s, "%02x ",
113 (unsigned char) res->res_lvbptr[i]);
115 if (rsb_flag(res, RSB_VALNOTVALID))
116 seq_printf(s, " (INVALID)");
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 "
125 "count %d\n", root_list, recover_list,
126 res->res_flags, res->res_recover_locks_count);
129 /* Print the locks attached to this resource */
130 seq_printf(s, "Granted Queue\n");
131 list_for_each_entry(lkb, &res->res_grantqueue, lkb_statequeue)
132 print_lock(s, lkb, res);
134 seq_printf(s, "Conversion Queue\n");
135 list_for_each_entry(lkb, &res->res_convertqueue, lkb_statequeue)
136 print_lock(s, lkb, res);
138 seq_printf(s, "Waiting Queue\n");
139 list_for_each_entry(lkb, &res->res_waitqueue, lkb_statequeue)
140 print_lock(s, lkb, res);
142 if (list_empty(&res->res_lookup))
145 seq_printf(s, "Lookup Queue\n");
146 list_for_each_entry(lkb, &res->res_lookup, lkb_rsb_lookup) {
147 seq_printf(s, "%08x %s", lkb->lkb_id,
148 print_lockmode(lkb->lkb_rqmode));
149 if (lkb->lkb_wait_type)
150 seq_printf(s, " wait_type: %d", lkb->lkb_wait_type);
157 static int rsb_iter_next(struct rsb_iter *ri)
159 struct dlm_ls *ls = ri->ls;
164 /* Find the next non-empty hash bucket */
165 for (i = ri->entry; i < ls->ls_rsbtbl_size; i++) {
166 read_lock(&ls->ls_rsbtbl[i].lock);
167 if (!list_empty(&ls->ls_rsbtbl[i].list)) {
168 ri->next = ls->ls_rsbtbl[i].list.next;
169 read_unlock(&ls->ls_rsbtbl[i].lock);
172 read_unlock(&ls->ls_rsbtbl[i].lock);
176 if (ri->entry >= ls->ls_rsbtbl_size)
180 read_lock(&ls->ls_rsbtbl[i].lock);
181 ri->next = ri->next->next;
182 if (ri->next->next == ls->ls_rsbtbl[i].list.next) {
183 /* End of list - move to next bucket */
186 read_unlock(&ls->ls_rsbtbl[i].lock);
189 read_unlock(&ls->ls_rsbtbl[i].lock);
191 ri->rsb = list_entry(ri->next, struct dlm_rsb, res_hashchain);
196 static void rsb_iter_free(struct rsb_iter *ri)
201 static struct rsb_iter *rsb_iter_init(struct dlm_ls *ls)
205 ri = kmalloc(sizeof *ri, GFP_KERNEL);
213 if (rsb_iter_next(ri)) {
221 static void *rsb_seq_start(struct seq_file *file, loff_t *pos)
226 ri = rsb_iter_init(file->private);
231 if (rsb_iter_next(ri)) {
240 static void *rsb_seq_next(struct seq_file *file, void *iter_ptr, loff_t *pos)
242 struct rsb_iter *ri = iter_ptr;
246 if (rsb_iter_next(ri)) {
254 static void rsb_seq_stop(struct seq_file *file, void *iter_ptr)
256 /* nothing for now */
259 static int rsb_seq_show(struct seq_file *file, void *iter_ptr)
261 struct rsb_iter *ri = iter_ptr;
263 print_resource(ri->rsb, file);
268 static struct seq_operations rsb_seq_ops = {
269 .start = rsb_seq_start,
270 .next = rsb_seq_next,
271 .stop = rsb_seq_stop,
272 .show = rsb_seq_show,
275 static int rsb_open(struct inode *inode, struct file *file)
277 struct seq_file *seq;
280 ret = seq_open(file, &rsb_seq_ops);
284 seq = file->private_data;
285 seq->private = inode->i_private;
290 static const struct file_operations rsb_fops = {
291 .owner = THIS_MODULE,
295 .release = seq_release
299 * dump lkb's on the ls_waiters list
302 static int waiters_open(struct inode *inode, struct file *file)
304 file->private_data = inode->i_private;
308 static ssize_t waiters_read(struct file *file, char __user *userbuf,
309 size_t count, loff_t *ppos)
311 struct dlm_ls *ls = file->private_data;
313 size_t len = DLM_DEBUG_BUF_LEN, pos = 0, ret, rv;
315 mutex_lock(&debug_buf_lock);
316 mutex_lock(&ls->ls_waiters_mutex);
317 memset(debug_buf, 0, sizeof(debug_buf));
319 list_for_each_entry(lkb, &ls->ls_waiters, lkb_wait_reply) {
320 ret = snprintf(debug_buf + pos, len - pos, "%x %d %d %s\n",
321 lkb->lkb_id, lkb->lkb_wait_type,
322 lkb->lkb_nodeid, lkb->lkb_resource->res_name);
323 if (ret >= len - pos)
327 mutex_unlock(&ls->ls_waiters_mutex);
329 rv = simple_read_from_buffer(userbuf, count, ppos, debug_buf, pos);
330 mutex_unlock(&debug_buf_lock);
334 static const struct file_operations waiters_fops = {
335 .owner = THIS_MODULE,
336 .open = waiters_open,
340 int dlm_create_debug_file(struct dlm_ls *ls)
342 char name[DLM_LOCKSPACE_LEN+8];
344 ls->ls_debug_rsb_dentry = debugfs_create_file(ls->ls_name,
349 if (!ls->ls_debug_rsb_dentry)
352 memset(name, 0, sizeof(name));
353 snprintf(name, DLM_LOCKSPACE_LEN+8, "%s_waiters", ls->ls_name);
355 ls->ls_debug_waiters_dentry = debugfs_create_file(name,
360 if (!ls->ls_debug_waiters_dentry) {
361 debugfs_remove(ls->ls_debug_rsb_dentry);
368 void dlm_delete_debug_file(struct dlm_ls *ls)
370 if (ls->ls_debug_rsb_dentry)
371 debugfs_remove(ls->ls_debug_rsb_dentry);
372 if (ls->ls_debug_waiters_dentry)
373 debugfs_remove(ls->ls_debug_waiters_dentry);
376 int dlm_register_debugfs(void)
378 mutex_init(&debug_buf_lock);
379 dlm_root = debugfs_create_dir("dlm", NULL);
380 return dlm_root ? 0 : -ENOMEM;
383 void dlm_unregister_debugfs(void)
385 debugfs_remove(dlm_root);