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"
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;
31 struct list_head *next;
36 * dump all rsb's in the lockspace hash table
39 static char *print_lockmode(int mode)
61 static void print_lock(struct seq_file *s, struct dlm_lkb *lkb,
64 seq_printf(s, "%08x %s", lkb->lkb_id, print_lockmode(lkb->lkb_grmode));
66 if (lkb->lkb_status == DLM_LKSTS_CONVERT
67 || lkb->lkb_status == DLM_LKSTS_WAITING)
68 seq_printf(s, " (%s)", print_lockmode(lkb->lkb_rqmode));
70 if (lkb->lkb_nodeid) {
71 if (lkb->lkb_nodeid != res->res_nodeid)
72 seq_printf(s, " Remote: %3d %08x", lkb->lkb_nodeid,
75 seq_printf(s, " Master: %08x", lkb->lkb_remid);
78 if (lkb->lkb_wait_type)
79 seq_printf(s, " wait_type: %d", lkb->lkb_wait_type);
84 static int print_resource(struct dlm_rsb *res, struct seq_file *s)
87 int i, lvblen = res->res_ls->ls_lvblen, recover_list, root_list;
89 seq_printf(s, "\nResource %p Name (len=%d) \"", res, res->res_length);
90 for (i = 0; i < res->res_length; i++) {
91 if (isprint(res->res_name[i]))
92 seq_printf(s, "%c", res->res_name[i]);
94 seq_printf(s, "%c", '.');
96 if (res->res_nodeid > 0)
97 seq_printf(s, "\" \nLocal Copy, Master is node %d\n",
99 else if (res->res_nodeid == 0)
100 seq_printf(s, "\" \nMaster Copy\n");
101 else if (res->res_nodeid == -1)
102 seq_printf(s, "\" \nLooking up master (lkid %x)\n",
103 res->res_first_lkid);
105 seq_printf(s, "\" \nInvalid master %d\n", res->res_nodeid);
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)");
121 root_list = !list_empty(&res->res_root_list);
122 recover_list = !list_empty(&res->res_recover_list);
124 if (root_list || recover_list) {
125 seq_printf(s, "Recovery: root %d recover %d flags %lx "
126 "count %d\n", root_list, recover_list,
127 res->res_flags, res->res_recover_locks_count);
130 /* Print the locks attached to this resource */
131 seq_printf(s, "Granted Queue\n");
132 list_for_each_entry(lkb, &res->res_grantqueue, lkb_statequeue)
133 print_lock(s, lkb, res);
135 seq_printf(s, "Conversion Queue\n");
136 list_for_each_entry(lkb, &res->res_convertqueue, lkb_statequeue)
137 print_lock(s, lkb, res);
139 seq_printf(s, "Waiting Queue\n");
140 list_for_each_entry(lkb, &res->res_waitqueue, lkb_statequeue)
141 print_lock(s, lkb, res);
143 if (list_empty(&res->res_lookup))
146 seq_printf(s, "Lookup Queue\n");
147 list_for_each_entry(lkb, &res->res_lookup, lkb_rsb_lookup) {
148 seq_printf(s, "%08x %s", lkb->lkb_id,
149 print_lockmode(lkb->lkb_rqmode));
150 if (lkb->lkb_wait_type)
151 seq_printf(s, " wait_type: %d", lkb->lkb_wait_type);
158 static int rsb_iter_next(struct rsb_iter *ri)
160 struct dlm_ls *ls = ri->ls;
165 /* Find the next non-empty hash bucket */
166 for (i = ri->entry; i < ls->ls_rsbtbl_size; i++) {
167 read_lock(&ls->ls_rsbtbl[i].lock);
168 if (!list_empty(&ls->ls_rsbtbl[i].list)) {
169 ri->next = ls->ls_rsbtbl[i].list.next;
170 ri->rsb = list_entry(ri->next, struct dlm_rsb,
172 dlm_hold_rsb(ri->rsb);
173 read_unlock(&ls->ls_rsbtbl[i].lock);
176 read_unlock(&ls->ls_rsbtbl[i].lock);
180 if (ri->entry >= ls->ls_rsbtbl_size)
183 struct dlm_rsb *old = ri->rsb;
185 read_lock(&ls->ls_rsbtbl[i].lock);
186 ri->next = ri->next->next;
187 if (ri->next->next == ls->ls_rsbtbl[i].list.next) {
188 /* End of list - move to next bucket */
191 read_unlock(&ls->ls_rsbtbl[i].lock);
195 ri->rsb = list_entry(ri->next, struct dlm_rsb, res_hashchain);
196 read_unlock(&ls->ls_rsbtbl[i].lock);
203 static void rsb_iter_free(struct rsb_iter *ri)
208 static struct rsb_iter *rsb_iter_init(struct dlm_ls *ls)
212 ri = kmalloc(sizeof *ri, GFP_KERNEL);
220 if (rsb_iter_next(ri)) {
228 static void *rsb_seq_start(struct seq_file *file, loff_t *pos)
233 ri = rsb_iter_init(file->private);
238 if (rsb_iter_next(ri)) {
247 static void *rsb_seq_next(struct seq_file *file, void *iter_ptr, loff_t *pos)
249 struct rsb_iter *ri = iter_ptr;
253 if (rsb_iter_next(ri)) {
261 static void rsb_seq_stop(struct seq_file *file, void *iter_ptr)
263 /* nothing for now */
266 static int rsb_seq_show(struct seq_file *file, void *iter_ptr)
268 struct rsb_iter *ri = iter_ptr;
270 print_resource(ri->rsb, file);
275 static struct seq_operations rsb_seq_ops = {
276 .start = rsb_seq_start,
277 .next = rsb_seq_next,
278 .stop = rsb_seq_stop,
279 .show = rsb_seq_show,
282 static int rsb_open(struct inode *inode, struct file *file)
284 struct seq_file *seq;
287 ret = seq_open(file, &rsb_seq_ops);
291 seq = file->private_data;
292 seq->private = inode->i_private;
297 static const struct file_operations rsb_fops = {
298 .owner = THIS_MODULE,
302 .release = seq_release
306 * dump lkb's on the ls_waiters list
309 static int waiters_open(struct inode *inode, struct file *file)
311 file->private_data = inode->i_private;
315 static ssize_t waiters_read(struct file *file, char __user *userbuf,
316 size_t count, loff_t *ppos)
318 struct dlm_ls *ls = file->private_data;
320 size_t len = DLM_DEBUG_BUF_LEN, pos = 0, ret, rv;
322 mutex_lock(&debug_buf_lock);
323 mutex_lock(&ls->ls_waiters_mutex);
324 memset(debug_buf, 0, sizeof(debug_buf));
326 list_for_each_entry(lkb, &ls->ls_waiters, lkb_wait_reply) {
327 ret = snprintf(debug_buf + pos, len - pos, "%x %d %d %s\n",
328 lkb->lkb_id, lkb->lkb_wait_type,
329 lkb->lkb_nodeid, lkb->lkb_resource->res_name);
330 if (ret >= len - pos)
334 mutex_unlock(&ls->ls_waiters_mutex);
336 rv = simple_read_from_buffer(userbuf, count, ppos, debug_buf, pos);
337 mutex_unlock(&debug_buf_lock);
341 static const struct file_operations waiters_fops = {
342 .owner = THIS_MODULE,
343 .open = waiters_open,
347 int dlm_create_debug_file(struct dlm_ls *ls)
349 char name[DLM_LOCKSPACE_LEN+8];
351 ls->ls_debug_rsb_dentry = debugfs_create_file(ls->ls_name,
356 if (!ls->ls_debug_rsb_dentry)
359 memset(name, 0, sizeof(name));
360 snprintf(name, DLM_LOCKSPACE_LEN+8, "%s_waiters", ls->ls_name);
362 ls->ls_debug_waiters_dentry = debugfs_create_file(name,
367 if (!ls->ls_debug_waiters_dentry) {
368 debugfs_remove(ls->ls_debug_rsb_dentry);
375 void dlm_delete_debug_file(struct dlm_ls *ls)
377 if (ls->ls_debug_rsb_dentry)
378 debugfs_remove(ls->ls_debug_rsb_dentry);
379 if (ls->ls_debug_waiters_dentry)
380 debugfs_remove(ls->ls_debug_waiters_dentry);
383 int dlm_register_debugfs(void)
385 mutex_init(&debug_buf_lock);
386 dlm_root = debugfs_create_dir("dlm", NULL);
387 return dlm_root ? 0 : -ENOMEM;
390 void dlm_unregister_debugfs(void)
392 debugfs_remove(dlm_root);