spi-topcliff-pch: supports a spi mode setup and bit order setup by IO control
[zen-stable.git] / fs / dlm / debug_fs.c
blob3dca2b39e83fa9c8b5025f0e3aefa94252ed0a41
1 /******************************************************************************
2 *******************************************************************************
3 **
4 ** Copyright (C) 2005-2009 Red Hat, Inc. All rights reserved.
5 **
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.
9 **
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>
18 #include <linux/slab.h>
20 #include "dlm_internal.h"
21 #include "lock.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;
29 static char *print_lockmode(int mode)
31 switch (mode) {
32 case DLM_LOCK_IV:
33 return "--";
34 case DLM_LOCK_NL:
35 return "NL";
36 case DLM_LOCK_CR:
37 return "CR";
38 case DLM_LOCK_CW:
39 return "CW";
40 case DLM_LOCK_PR:
41 return "PR";
42 case DLM_LOCK_PW:
43 return "PW";
44 case DLM_LOCK_EX:
45 return "EX";
46 default:
47 return "??";
51 static int print_format1_lock(struct seq_file *s, struct dlm_lkb *lkb,
52 struct dlm_rsb *res)
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,
63 lkb->lkb_remid);
64 else
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);
71 return seq_printf(s, "\n");
74 static int print_format1(struct dlm_rsb *res, struct seq_file *s)
76 struct dlm_lkb *lkb;
77 int i, lvblen = res->res_ls->ls_lvblen, recover_list, root_list;
78 int rv;
80 lock_rsb(res);
82 rv = seq_printf(s, "\nResource %p Name (len=%d) \"",
83 res, res->res_length);
84 if (rv)
85 goto out;
87 for (i = 0; i < res->res_length; i++) {
88 if (isprint(res->res_name[i]))
89 seq_printf(s, "%c", res->res_name[i]);
90 else
91 seq_printf(s, "%c", '.');
94 if (res->res_nodeid > 0)
95 rv = seq_printf(s, "\" \nLocal Copy, Master is node %d\n",
96 res->res_nodeid);
97 else if (res->res_nodeid == 0)
98 rv = seq_printf(s, "\" \nMaster Copy\n");
99 else if (res->res_nodeid == -1)
100 rv = seq_printf(s, "\" \nLooking up master (lkid %x)\n",
101 res->res_first_lkid);
102 else
103 rv = seq_printf(s, "\" \nInvalid master %d\n",
104 res->res_nodeid);
105 if (rv)
106 goto out;
108 /* Print the LVB: */
109 if (res->res_lvbptr) {
110 seq_printf(s, "LVB: ");
111 for (i = 0; i < lvblen; i++) {
112 if (i == lvblen / 2)
113 seq_printf(s, "\n ");
114 seq_printf(s, "%02x ",
115 (unsigned char) res->res_lvbptr[i]);
117 if (rsb_flag(res, RSB_VALNOTVALID))
118 seq_printf(s, " (INVALID)");
119 rv = seq_printf(s, "\n");
120 if (rv)
121 goto out;
124 root_list = !list_empty(&res->res_root_list);
125 recover_list = !list_empty(&res->res_recover_list);
127 if (root_list || recover_list) {
128 rv = seq_printf(s, "Recovery: root %d recover %d flags %lx "
129 "count %d\n", root_list, recover_list,
130 res->res_flags, res->res_recover_locks_count);
131 if (rv)
132 goto out;
135 /* Print the locks attached to this resource */
136 seq_printf(s, "Granted Queue\n");
137 list_for_each_entry(lkb, &res->res_grantqueue, lkb_statequeue) {
138 rv = print_format1_lock(s, lkb, res);
139 if (rv)
140 goto out;
143 seq_printf(s, "Conversion Queue\n");
144 list_for_each_entry(lkb, &res->res_convertqueue, lkb_statequeue) {
145 rv = print_format1_lock(s, lkb, res);
146 if (rv)
147 goto out;
150 seq_printf(s, "Waiting Queue\n");
151 list_for_each_entry(lkb, &res->res_waitqueue, lkb_statequeue) {
152 rv = print_format1_lock(s, lkb, res);
153 if (rv)
154 goto out;
157 if (list_empty(&res->res_lookup))
158 goto out;
160 seq_printf(s, "Lookup Queue\n");
161 list_for_each_entry(lkb, &res->res_lookup, lkb_rsb_lookup) {
162 rv = seq_printf(s, "%08x %s", lkb->lkb_id,
163 print_lockmode(lkb->lkb_rqmode));
164 if (lkb->lkb_wait_type)
165 seq_printf(s, " wait_type: %d", lkb->lkb_wait_type);
166 rv = seq_printf(s, "\n");
168 out:
169 unlock_rsb(res);
170 return rv;
173 static int print_format2_lock(struct seq_file *s, struct dlm_lkb *lkb,
174 struct dlm_rsb *r)
176 u64 xid = 0;
177 u64 us;
178 int rv;
180 if (lkb->lkb_flags & DLM_IFL_USER) {
181 if (lkb->lkb_ua)
182 xid = lkb->lkb_ua->xid;
185 /* microseconds since lkb was added to current queue */
186 us = ktime_to_us(ktime_sub(ktime_get(), lkb->lkb_timestamp));
188 /* id nodeid remid pid xid exflags flags sts grmode rqmode time_us
189 r_nodeid r_len r_name */
191 rv = seq_printf(s, "%x %d %x %u %llu %x %x %d %d %d %llu %u %d \"%s\"\n",
192 lkb->lkb_id,
193 lkb->lkb_nodeid,
194 lkb->lkb_remid,
195 lkb->lkb_ownpid,
196 (unsigned long long)xid,
197 lkb->lkb_exflags,
198 lkb->lkb_flags,
199 lkb->lkb_status,
200 lkb->lkb_grmode,
201 lkb->lkb_rqmode,
202 (unsigned long long)us,
203 r->res_nodeid,
204 r->res_length,
205 r->res_name);
206 return rv;
209 static int print_format2(struct dlm_rsb *r, struct seq_file *s)
211 struct dlm_lkb *lkb;
212 int rv = 0;
214 lock_rsb(r);
216 list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
217 rv = print_format2_lock(s, lkb, r);
218 if (rv)
219 goto out;
222 list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
223 rv = print_format2_lock(s, lkb, r);
224 if (rv)
225 goto out;
228 list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue) {
229 rv = print_format2_lock(s, lkb, r);
230 if (rv)
231 goto out;
233 out:
234 unlock_rsb(r);
235 return rv;
238 static int print_format3_lock(struct seq_file *s, struct dlm_lkb *lkb,
239 int rsb_lookup)
241 u64 xid = 0;
242 int rv;
244 if (lkb->lkb_flags & DLM_IFL_USER) {
245 if (lkb->lkb_ua)
246 xid = lkb->lkb_ua->xid;
249 rv = seq_printf(s, "lkb %x %d %x %u %llu %x %x %d %d %d %d %d %d %u %llu %llu\n",
250 lkb->lkb_id,
251 lkb->lkb_nodeid,
252 lkb->lkb_remid,
253 lkb->lkb_ownpid,
254 (unsigned long long)xid,
255 lkb->lkb_exflags,
256 lkb->lkb_flags,
257 lkb->lkb_status,
258 lkb->lkb_grmode,
259 lkb->lkb_rqmode,
260 lkb->lkb_last_bast.mode,
261 rsb_lookup,
262 lkb->lkb_wait_type,
263 lkb->lkb_lvbseq,
264 (unsigned long long)ktime_to_ns(lkb->lkb_timestamp),
265 (unsigned long long)ktime_to_ns(lkb->lkb_last_bast_time));
266 return rv;
269 static int print_format3(struct dlm_rsb *r, struct seq_file *s)
271 struct dlm_lkb *lkb;
272 int i, lvblen = r->res_ls->ls_lvblen;
273 int print_name = 1;
274 int rv;
276 lock_rsb(r);
278 rv = seq_printf(s, "rsb %p %d %x %lx %d %d %u %d ",
280 r->res_nodeid,
281 r->res_first_lkid,
282 r->res_flags,
283 !list_empty(&r->res_root_list),
284 !list_empty(&r->res_recover_list),
285 r->res_recover_locks_count,
286 r->res_length);
287 if (rv)
288 goto out;
290 for (i = 0; i < r->res_length; i++) {
291 if (!isascii(r->res_name[i]) || !isprint(r->res_name[i]))
292 print_name = 0;
295 seq_printf(s, "%s", print_name ? "str " : "hex");
297 for (i = 0; i < r->res_length; i++) {
298 if (print_name)
299 seq_printf(s, "%c", r->res_name[i]);
300 else
301 seq_printf(s, " %02x", (unsigned char)r->res_name[i]);
303 rv = seq_printf(s, "\n");
304 if (rv)
305 goto out;
307 if (!r->res_lvbptr)
308 goto do_locks;
310 seq_printf(s, "lvb %u %d", r->res_lvbseq, lvblen);
312 for (i = 0; i < lvblen; i++)
313 seq_printf(s, " %02x", (unsigned char)r->res_lvbptr[i]);
314 rv = seq_printf(s, "\n");
315 if (rv)
316 goto out;
318 do_locks:
319 list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
320 rv = print_format3_lock(s, lkb, 0);
321 if (rv)
322 goto out;
325 list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
326 rv = print_format3_lock(s, lkb, 0);
327 if (rv)
328 goto out;
331 list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue) {
332 rv = print_format3_lock(s, lkb, 0);
333 if (rv)
334 goto out;
337 list_for_each_entry(lkb, &r->res_lookup, lkb_rsb_lookup) {
338 rv = print_format3_lock(s, lkb, 1);
339 if (rv)
340 goto out;
342 out:
343 unlock_rsb(r);
344 return rv;
347 struct rsbtbl_iter {
348 struct dlm_rsb *rsb;
349 unsigned bucket;
350 int format;
351 int header;
354 /* seq_printf returns -1 if the buffer is full, and 0 otherwise.
355 If the buffer is full, seq_printf can be called again, but it
356 does nothing and just returns -1. So, the these printing routines
357 periodically check the return value to avoid wasting too much time
358 trying to print to a full buffer. */
360 static int table_seq_show(struct seq_file *seq, void *iter_ptr)
362 struct rsbtbl_iter *ri = iter_ptr;
363 int rv = 0;
365 switch (ri->format) {
366 case 1:
367 rv = print_format1(ri->rsb, seq);
368 break;
369 case 2:
370 if (ri->header) {
371 seq_printf(seq, "id nodeid remid pid xid exflags "
372 "flags sts grmode rqmode time_ms "
373 "r_nodeid r_len r_name\n");
374 ri->header = 0;
376 rv = print_format2(ri->rsb, seq);
377 break;
378 case 3:
379 if (ri->header) {
380 seq_printf(seq, "version rsb 1.1 lvb 1.1 lkb 1.1\n");
381 ri->header = 0;
383 rv = print_format3(ri->rsb, seq);
384 break;
387 return rv;
390 static const struct seq_operations format1_seq_ops;
391 static const struct seq_operations format2_seq_ops;
392 static const struct seq_operations format3_seq_ops;
394 static void *table_seq_start(struct seq_file *seq, loff_t *pos)
396 struct rb_node *node;
397 struct dlm_ls *ls = seq->private;
398 struct rsbtbl_iter *ri;
399 struct dlm_rsb *r;
400 loff_t n = *pos;
401 unsigned bucket, entry;
403 bucket = n >> 32;
404 entry = n & ((1LL << 32) - 1);
406 if (bucket >= ls->ls_rsbtbl_size)
407 return NULL;
409 ri = kzalloc(sizeof(struct rsbtbl_iter), GFP_NOFS);
410 if (!ri)
411 return NULL;
412 if (n == 0)
413 ri->header = 1;
414 if (seq->op == &format1_seq_ops)
415 ri->format = 1;
416 if (seq->op == &format2_seq_ops)
417 ri->format = 2;
418 if (seq->op == &format3_seq_ops)
419 ri->format = 3;
421 spin_lock(&ls->ls_rsbtbl[bucket].lock);
422 if (!RB_EMPTY_ROOT(&ls->ls_rsbtbl[bucket].keep)) {
423 for (node = rb_first(&ls->ls_rsbtbl[bucket].keep); node;
424 node = rb_next(node)) {
425 r = rb_entry(node, struct dlm_rsb, res_hashnode);
426 if (!entry--) {
427 dlm_hold_rsb(r);
428 ri->rsb = r;
429 ri->bucket = bucket;
430 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
431 return ri;
435 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
438 * move to the first rsb in the next non-empty bucket
441 /* zero the entry */
442 n &= ~((1LL << 32) - 1);
444 while (1) {
445 bucket++;
446 n += 1LL << 32;
448 if (bucket >= ls->ls_rsbtbl_size) {
449 kfree(ri);
450 return NULL;
453 spin_lock(&ls->ls_rsbtbl[bucket].lock);
454 if (!RB_EMPTY_ROOT(&ls->ls_rsbtbl[bucket].keep)) {
455 node = rb_first(&ls->ls_rsbtbl[bucket].keep);
456 r = rb_entry(node, struct dlm_rsb, res_hashnode);
457 dlm_hold_rsb(r);
458 ri->rsb = r;
459 ri->bucket = bucket;
460 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
461 *pos = n;
462 return ri;
464 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
468 static void *table_seq_next(struct seq_file *seq, void *iter_ptr, loff_t *pos)
470 struct dlm_ls *ls = seq->private;
471 struct rsbtbl_iter *ri = iter_ptr;
472 struct rb_node *next;
473 struct dlm_rsb *r, *rp;
474 loff_t n = *pos;
475 unsigned bucket;
477 bucket = n >> 32;
480 * move to the next rsb in the same bucket
483 spin_lock(&ls->ls_rsbtbl[bucket].lock);
484 rp = ri->rsb;
485 next = rb_next(&rp->res_hashnode);
487 if (next) {
488 r = rb_entry(next, struct dlm_rsb, res_hashnode);
489 dlm_hold_rsb(r);
490 ri->rsb = r;
491 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
492 dlm_put_rsb(rp);
493 ++*pos;
494 return ri;
496 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
497 dlm_put_rsb(rp);
500 * move to the first rsb in the next non-empty bucket
503 /* zero the entry */
504 n &= ~((1LL << 32) - 1);
506 while (1) {
507 bucket++;
508 n += 1LL << 32;
510 if (bucket >= ls->ls_rsbtbl_size) {
511 kfree(ri);
512 return NULL;
515 spin_lock(&ls->ls_rsbtbl[bucket].lock);
516 if (!RB_EMPTY_ROOT(&ls->ls_rsbtbl[bucket].keep)) {
517 next = rb_first(&ls->ls_rsbtbl[bucket].keep);
518 r = rb_entry(next, struct dlm_rsb, res_hashnode);
519 dlm_hold_rsb(r);
520 ri->rsb = r;
521 ri->bucket = bucket;
522 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
523 *pos = n;
524 return ri;
526 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
530 static void table_seq_stop(struct seq_file *seq, void *iter_ptr)
532 struct rsbtbl_iter *ri = iter_ptr;
534 if (ri) {
535 dlm_put_rsb(ri->rsb);
536 kfree(ri);
540 static const struct seq_operations format1_seq_ops = {
541 .start = table_seq_start,
542 .next = table_seq_next,
543 .stop = table_seq_stop,
544 .show = table_seq_show,
547 static const struct seq_operations format2_seq_ops = {
548 .start = table_seq_start,
549 .next = table_seq_next,
550 .stop = table_seq_stop,
551 .show = table_seq_show,
554 static const struct seq_operations format3_seq_ops = {
555 .start = table_seq_start,
556 .next = table_seq_next,
557 .stop = table_seq_stop,
558 .show = table_seq_show,
561 static const struct file_operations format1_fops;
562 static const struct file_operations format2_fops;
563 static const struct file_operations format3_fops;
565 static int table_open(struct inode *inode, struct file *file)
567 struct seq_file *seq;
568 int ret = -1;
570 if (file->f_op == &format1_fops)
571 ret = seq_open(file, &format1_seq_ops);
572 else if (file->f_op == &format2_fops)
573 ret = seq_open(file, &format2_seq_ops);
574 else if (file->f_op == &format3_fops)
575 ret = seq_open(file, &format3_seq_ops);
577 if (ret)
578 return ret;
580 seq = file->private_data;
581 seq->private = inode->i_private; /* the dlm_ls */
582 return 0;
585 static const struct file_operations format1_fops = {
586 .owner = THIS_MODULE,
587 .open = table_open,
588 .read = seq_read,
589 .llseek = seq_lseek,
590 .release = seq_release
593 static const struct file_operations format2_fops = {
594 .owner = THIS_MODULE,
595 .open = table_open,
596 .read = seq_read,
597 .llseek = seq_lseek,
598 .release = seq_release
601 static const struct file_operations format3_fops = {
602 .owner = THIS_MODULE,
603 .open = table_open,
604 .read = seq_read,
605 .llseek = seq_lseek,
606 .release = seq_release
610 * dump lkb's on the ls_waiters list
613 static int waiters_open(struct inode *inode, struct file *file)
615 file->private_data = inode->i_private;
616 return 0;
619 static ssize_t waiters_read(struct file *file, char __user *userbuf,
620 size_t count, loff_t *ppos)
622 struct dlm_ls *ls = file->private_data;
623 struct dlm_lkb *lkb;
624 size_t len = DLM_DEBUG_BUF_LEN, pos = 0, ret, rv;
626 mutex_lock(&debug_buf_lock);
627 mutex_lock(&ls->ls_waiters_mutex);
628 memset(debug_buf, 0, sizeof(debug_buf));
630 list_for_each_entry(lkb, &ls->ls_waiters, lkb_wait_reply) {
631 ret = snprintf(debug_buf + pos, len - pos, "%x %d %d %s\n",
632 lkb->lkb_id, lkb->lkb_wait_type,
633 lkb->lkb_nodeid, lkb->lkb_resource->res_name);
634 if (ret >= len - pos)
635 break;
636 pos += ret;
638 mutex_unlock(&ls->ls_waiters_mutex);
640 rv = simple_read_from_buffer(userbuf, count, ppos, debug_buf, pos);
641 mutex_unlock(&debug_buf_lock);
642 return rv;
645 static const struct file_operations waiters_fops = {
646 .owner = THIS_MODULE,
647 .open = waiters_open,
648 .read = waiters_read,
649 .llseek = default_llseek,
652 void dlm_delete_debug_file(struct dlm_ls *ls)
654 if (ls->ls_debug_rsb_dentry)
655 debugfs_remove(ls->ls_debug_rsb_dentry);
656 if (ls->ls_debug_waiters_dentry)
657 debugfs_remove(ls->ls_debug_waiters_dentry);
658 if (ls->ls_debug_locks_dentry)
659 debugfs_remove(ls->ls_debug_locks_dentry);
660 if (ls->ls_debug_all_dentry)
661 debugfs_remove(ls->ls_debug_all_dentry);
664 int dlm_create_debug_file(struct dlm_ls *ls)
666 char name[DLM_LOCKSPACE_LEN+8];
668 /* format 1 */
670 ls->ls_debug_rsb_dentry = debugfs_create_file(ls->ls_name,
671 S_IFREG | S_IRUGO,
672 dlm_root,
674 &format1_fops);
675 if (!ls->ls_debug_rsb_dentry)
676 goto fail;
678 /* format 2 */
680 memset(name, 0, sizeof(name));
681 snprintf(name, DLM_LOCKSPACE_LEN+8, "%s_locks", ls->ls_name);
683 ls->ls_debug_locks_dentry = debugfs_create_file(name,
684 S_IFREG | S_IRUGO,
685 dlm_root,
687 &format2_fops);
688 if (!ls->ls_debug_locks_dentry)
689 goto fail;
691 /* format 3 */
693 memset(name, 0, sizeof(name));
694 snprintf(name, DLM_LOCKSPACE_LEN+8, "%s_all", ls->ls_name);
696 ls->ls_debug_all_dentry = debugfs_create_file(name,
697 S_IFREG | S_IRUGO,
698 dlm_root,
700 &format3_fops);
701 if (!ls->ls_debug_all_dentry)
702 goto fail;
704 memset(name, 0, sizeof(name));
705 snprintf(name, DLM_LOCKSPACE_LEN+8, "%s_waiters", ls->ls_name);
707 ls->ls_debug_waiters_dentry = debugfs_create_file(name,
708 S_IFREG | S_IRUGO,
709 dlm_root,
711 &waiters_fops);
712 if (!ls->ls_debug_waiters_dentry)
713 goto fail;
715 return 0;
717 fail:
718 dlm_delete_debug_file(ls);
719 return -ENOMEM;
722 int __init dlm_register_debugfs(void)
724 mutex_init(&debug_buf_lock);
725 dlm_root = debugfs_create_dir("dlm", NULL);
726 return dlm_root ? 0 : -ENOMEM;
729 void dlm_unregister_debugfs(void)
731 debugfs_remove(dlm_root);