Loading...
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******************************************************************************/
12
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>
19
20#include "dlm_internal.h"
21#include "lock.h"
22
23#define DLM_DEBUG_BUF_LEN 4096
24static char debug_buf[DLM_DEBUG_BUF_LEN];
25static struct mutex debug_buf_lock;
26
27static struct dentry *dlm_root;
28
29static char *print_lockmode(int mode)
30{
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 "??";
48 }
49}
50
51static void print_format1_lock(struct seq_file *s, struct dlm_lkb *lkb,
52 struct dlm_rsb *res)
53{
54 seq_printf(s, "%08x %s", lkb->lkb_id, print_lockmode(lkb->lkb_grmode));
55
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));
59
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);
66 }
67
68 if (lkb->lkb_wait_type)
69 seq_printf(s, " wait_type: %d", lkb->lkb_wait_type);
70
71 seq_puts(s, "\n");
72}
73
74static void print_format1(struct dlm_rsb *res, struct seq_file *s)
75{
76 struct dlm_lkb *lkb;
77 int i, lvblen = res->res_ls->ls_lvblen, recover_list, root_list;
78
79 lock_rsb(res);
80
81 seq_printf(s, "\nResource %p Name (len=%d) \"", res, res->res_length);
82
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]);
86 else
87 seq_printf(s, "%c", '.');
88 }
89
90 if (res->res_nodeid > 0)
91 seq_printf(s, "\"\nLocal Copy, Master is node %d\n",
92 res->res_nodeid);
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",
97 res->res_first_lkid);
98 else
99 seq_printf(s, "\"\nInvalid master %d\n", res->res_nodeid);
100 if (seq_has_overflowed(s))
101 goto out;
102
103 /* Print the LVB: */
104 if (res->res_lvbptr) {
105 seq_puts(s, "LVB: ");
106 for (i = 0; i < lvblen; i++) {
107 if (i == lvblen / 2)
108 seq_puts(s, "\n ");
109 seq_printf(s, "%02x ",
110 (unsigned char) res->res_lvbptr[i]);
111 }
112 if (rsb_flag(res, RSB_VALNOTVALID))
113 seq_puts(s, " (INVALID)");
114 seq_puts(s, "\n");
115 if (seq_has_overflowed(s))
116 goto out;
117 }
118
119 root_list = !list_empty(&res->res_root_list);
120 recover_list = !list_empty(&res->res_recover_list);
121
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);
126 }
127
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))
133 goto out;
134 }
135
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))
140 goto out;
141 }
142
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))
147 goto out;
148 }
149
150 if (list_empty(&res->res_lookup))
151 goto out;
152
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);
159 seq_puts(s, "\n");
160 if (seq_has_overflowed(s))
161 goto out;
162 }
163 out:
164 unlock_rsb(res);
165}
166
167static void print_format2_lock(struct seq_file *s, struct dlm_lkb *lkb,
168 struct dlm_rsb *r)
169{
170 u64 xid = 0;
171 u64 us;
172
173 if (lkb->lkb_flags & DLM_IFL_USER) {
174 if (lkb->lkb_ua)
175 xid = lkb->lkb_ua->xid;
176 }
177
178 /* microseconds since lkb was added to current queue */
179 us = ktime_to_us(ktime_sub(ktime_get(), lkb->lkb_timestamp));
180
181 /* id nodeid remid pid xid exflags flags sts grmode rqmode time_us
182 r_nodeid r_len r_name */
183
184 seq_printf(s, "%x %d %x %u %llu %x %x %d %d %d %llu %u %d \"%s\"\n",
185 lkb->lkb_id,
186 lkb->lkb_nodeid,
187 lkb->lkb_remid,
188 lkb->lkb_ownpid,
189 (unsigned long long)xid,
190 lkb->lkb_exflags,
191 lkb->lkb_flags,
192 lkb->lkb_status,
193 lkb->lkb_grmode,
194 lkb->lkb_rqmode,
195 (unsigned long long)us,
196 r->res_nodeid,
197 r->res_length,
198 r->res_name);
199}
200
201static void print_format2(struct dlm_rsb *r, struct seq_file *s)
202{
203 struct dlm_lkb *lkb;
204
205 lock_rsb(r);
206
207 list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
208 print_format2_lock(s, lkb, r);
209 if (seq_has_overflowed(s))
210 goto out;
211 }
212
213 list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
214 print_format2_lock(s, lkb, r);
215 if (seq_has_overflowed(s))
216 goto out;
217 }
218
219 list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue) {
220 print_format2_lock(s, lkb, r);
221 if (seq_has_overflowed(s))
222 goto out;
223 }
224 out:
225 unlock_rsb(r);
226}
227
228static void print_format3_lock(struct seq_file *s, struct dlm_lkb *lkb,
229 int rsb_lookup)
230{
231 u64 xid = 0;
232
233 if (lkb->lkb_flags & DLM_IFL_USER) {
234 if (lkb->lkb_ua)
235 xid = lkb->lkb_ua->xid;
236 }
237
238 seq_printf(s, "lkb %x %d %x %u %llu %x %x %d %d %d %d %d %d %u %llu %llu\n",
239 lkb->lkb_id,
240 lkb->lkb_nodeid,
241 lkb->lkb_remid,
242 lkb->lkb_ownpid,
243 (unsigned long long)xid,
244 lkb->lkb_exflags,
245 lkb->lkb_flags,
246 lkb->lkb_status,
247 lkb->lkb_grmode,
248 lkb->lkb_rqmode,
249 lkb->lkb_last_bast.mode,
250 rsb_lookup,
251 lkb->lkb_wait_type,
252 lkb->lkb_lvbseq,
253 (unsigned long long)ktime_to_ns(lkb->lkb_timestamp),
254 (unsigned long long)ktime_to_ns(lkb->lkb_last_bast_time));
255}
256
257static void print_format3(struct dlm_rsb *r, struct seq_file *s)
258{
259 struct dlm_lkb *lkb;
260 int i, lvblen = r->res_ls->ls_lvblen;
261 int print_name = 1;
262
263 lock_rsb(r);
264
265 seq_printf(s, "rsb %p %d %x %lx %d %d %u %d ",
266 r,
267 r->res_nodeid,
268 r->res_first_lkid,
269 r->res_flags,
270 !list_empty(&r->res_root_list),
271 !list_empty(&r->res_recover_list),
272 r->res_recover_locks_count,
273 r->res_length);
274 if (seq_has_overflowed(s))
275 goto out;
276
277 for (i = 0; i < r->res_length; i++) {
278 if (!isascii(r->res_name[i]) || !isprint(r->res_name[i]))
279 print_name = 0;
280 }
281
282 seq_puts(s, print_name ? "str " : "hex");
283
284 for (i = 0; i < r->res_length; i++) {
285 if (print_name)
286 seq_printf(s, "%c", r->res_name[i]);
287 else
288 seq_printf(s, " %02x", (unsigned char)r->res_name[i]);
289 }
290 seq_puts(s, "\n");
291 if (seq_has_overflowed(s))
292 goto out;
293
294 if (!r->res_lvbptr)
295 goto do_locks;
296
297 seq_printf(s, "lvb %u %d", r->res_lvbseq, lvblen);
298
299 for (i = 0; i < lvblen; i++)
300 seq_printf(s, " %02x", (unsigned char)r->res_lvbptr[i]);
301 seq_puts(s, "\n");
302 if (seq_has_overflowed(s))
303 goto out;
304
305 do_locks:
306 list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
307 print_format3_lock(s, lkb, 0);
308 if (seq_has_overflowed(s))
309 goto out;
310 }
311
312 list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
313 print_format3_lock(s, lkb, 0);
314 if (seq_has_overflowed(s))
315 goto out;
316 }
317
318 list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue) {
319 print_format3_lock(s, lkb, 0);
320 if (seq_has_overflowed(s))
321 goto out;
322 }
323
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))
327 goto out;
328 }
329 out:
330 unlock_rsb(r);
331}
332
333static void print_format4(struct dlm_rsb *r, struct seq_file *s)
334{
335 int our_nodeid = dlm_our_nodeid();
336 int print_name = 1;
337 int i;
338
339 lock_rsb(r);
340
341 seq_printf(s, "rsb %p %d %d %d %d %lu %lx %d ",
342 r,
343 r->res_nodeid,
344 r->res_master_nodeid,
345 r->res_dir_nodeid,
346 our_nodeid,
347 r->res_toss_time,
348 r->res_flags,
349 r->res_length);
350
351 for (i = 0; i < r->res_length; i++) {
352 if (!isascii(r->res_name[i]) || !isprint(r->res_name[i]))
353 print_name = 0;
354 }
355
356 seq_puts(s, print_name ? "str " : "hex");
357
358 for (i = 0; i < r->res_length; i++) {
359 if (print_name)
360 seq_printf(s, "%c", r->res_name[i]);
361 else
362 seq_printf(s, " %02x", (unsigned char)r->res_name[i]);
363 }
364 seq_puts(s, "\n");
365
366 unlock_rsb(r);
367}
368
369struct rsbtbl_iter {
370 struct dlm_rsb *rsb;
371 unsigned bucket;
372 int format;
373 int header;
374};
375
376/*
377 * If the buffer is full, seq_printf can be called again, but it
378 * does nothing. So, the these printing routines periodically check
379 * seq_has_overflowed to avoid wasting too much time trying to print to
380 * a full buffer.
381 */
382
383static int table_seq_show(struct seq_file *seq, void *iter_ptr)
384{
385 struct rsbtbl_iter *ri = iter_ptr;
386
387 switch (ri->format) {
388 case 1:
389 print_format1(ri->rsb, seq);
390 break;
391 case 2:
392 if (ri->header) {
393 seq_puts(seq, "id nodeid remid pid xid exflags flags sts grmode rqmode time_ms r_nodeid r_len r_name\n");
394 ri->header = 0;
395 }
396 print_format2(ri->rsb, seq);
397 break;
398 case 3:
399 if (ri->header) {
400 seq_puts(seq, "version rsb 1.1 lvb 1.1 lkb 1.1\n");
401 ri->header = 0;
402 }
403 print_format3(ri->rsb, seq);
404 break;
405 case 4:
406 if (ri->header) {
407 seq_puts(seq, "version 4 rsb 2\n");
408 ri->header = 0;
409 }
410 print_format4(ri->rsb, seq);
411 break;
412 }
413
414 return 0;
415}
416
417static const struct seq_operations format1_seq_ops;
418static const struct seq_operations format2_seq_ops;
419static const struct seq_operations format3_seq_ops;
420static const struct seq_operations format4_seq_ops;
421
422static void *table_seq_start(struct seq_file *seq, loff_t *pos)
423{
424 struct rb_root *tree;
425 struct rb_node *node;
426 struct dlm_ls *ls = seq->private;
427 struct rsbtbl_iter *ri;
428 struct dlm_rsb *r;
429 loff_t n = *pos;
430 unsigned bucket, entry;
431 int toss = (seq->op == &format4_seq_ops);
432
433 bucket = n >> 32;
434 entry = n & ((1LL << 32) - 1);
435
436 if (bucket >= ls->ls_rsbtbl_size)
437 return NULL;
438
439 ri = kzalloc(sizeof(struct rsbtbl_iter), GFP_NOFS);
440 if (!ri)
441 return NULL;
442 if (n == 0)
443 ri->header = 1;
444 if (seq->op == &format1_seq_ops)
445 ri->format = 1;
446 if (seq->op == &format2_seq_ops)
447 ri->format = 2;
448 if (seq->op == &format3_seq_ops)
449 ri->format = 3;
450 if (seq->op == &format4_seq_ops)
451 ri->format = 4;
452
453 tree = toss ? &ls->ls_rsbtbl[bucket].toss : &ls->ls_rsbtbl[bucket].keep;
454
455 spin_lock(&ls->ls_rsbtbl[bucket].lock);
456 if (!RB_EMPTY_ROOT(tree)) {
457 for (node = rb_first(tree); node; node = rb_next(node)) {
458 r = rb_entry(node, struct dlm_rsb, res_hashnode);
459 if (!entry--) {
460 dlm_hold_rsb(r);
461 ri->rsb = r;
462 ri->bucket = bucket;
463 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
464 return ri;
465 }
466 }
467 }
468 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
469
470 /*
471 * move to the first rsb in the next non-empty bucket
472 */
473
474 /* zero the entry */
475 n &= ~((1LL << 32) - 1);
476
477 while (1) {
478 bucket++;
479 n += 1LL << 32;
480
481 if (bucket >= ls->ls_rsbtbl_size) {
482 kfree(ri);
483 return NULL;
484 }
485 tree = toss ? &ls->ls_rsbtbl[bucket].toss : &ls->ls_rsbtbl[bucket].keep;
486
487 spin_lock(&ls->ls_rsbtbl[bucket].lock);
488 if (!RB_EMPTY_ROOT(tree)) {
489 node = rb_first(tree);
490 r = rb_entry(node, struct dlm_rsb, res_hashnode);
491 dlm_hold_rsb(r);
492 ri->rsb = r;
493 ri->bucket = bucket;
494 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
495 *pos = n;
496 return ri;
497 }
498 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
499 }
500}
501
502static void *table_seq_next(struct seq_file *seq, void *iter_ptr, loff_t *pos)
503{
504 struct dlm_ls *ls = seq->private;
505 struct rsbtbl_iter *ri = iter_ptr;
506 struct rb_root *tree;
507 struct rb_node *next;
508 struct dlm_rsb *r, *rp;
509 loff_t n = *pos;
510 unsigned bucket;
511 int toss = (seq->op == &format4_seq_ops);
512
513 bucket = n >> 32;
514
515 /*
516 * move to the next rsb in the same bucket
517 */
518
519 spin_lock(&ls->ls_rsbtbl[bucket].lock);
520 rp = ri->rsb;
521 next = rb_next(&rp->res_hashnode);
522
523 if (next) {
524 r = rb_entry(next, struct dlm_rsb, res_hashnode);
525 dlm_hold_rsb(r);
526 ri->rsb = r;
527 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
528 dlm_put_rsb(rp);
529 ++*pos;
530 return ri;
531 }
532 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
533 dlm_put_rsb(rp);
534
535 /*
536 * move to the first rsb in the next non-empty bucket
537 */
538
539 /* zero the entry */
540 n &= ~((1LL << 32) - 1);
541
542 while (1) {
543 bucket++;
544 n += 1LL << 32;
545
546 if (bucket >= ls->ls_rsbtbl_size) {
547 kfree(ri);
548 return NULL;
549 }
550 tree = toss ? &ls->ls_rsbtbl[bucket].toss : &ls->ls_rsbtbl[bucket].keep;
551
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);
556 dlm_hold_rsb(r);
557 ri->rsb = r;
558 ri->bucket = bucket;
559 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
560 *pos = n;
561 return ri;
562 }
563 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
564 }
565}
566
567static void table_seq_stop(struct seq_file *seq, void *iter_ptr)
568{
569 struct rsbtbl_iter *ri = iter_ptr;
570
571 if (ri) {
572 dlm_put_rsb(ri->rsb);
573 kfree(ri);
574 }
575}
576
577static 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,
582};
583
584static 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,
589};
590
591static 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,
596};
597
598static 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,
603};
604
605static const struct file_operations format1_fops;
606static const struct file_operations format2_fops;
607static const struct file_operations format3_fops;
608static const struct file_operations format4_fops;
609
610static int table_open(struct inode *inode, struct file *file)
611{
612 struct seq_file *seq;
613 int ret = -1;
614
615 if (file->f_op == &format1_fops)
616 ret = seq_open(file, &format1_seq_ops);
617 else if (file->f_op == &format2_fops)
618 ret = seq_open(file, &format2_seq_ops);
619 else if (file->f_op == &format3_fops)
620 ret = seq_open(file, &format3_seq_ops);
621 else if (file->f_op == &format4_fops)
622 ret = seq_open(file, &format4_seq_ops);
623
624 if (ret)
625 return ret;
626
627 seq = file->private_data;
628 seq->private = inode->i_private; /* the dlm_ls */
629 return 0;
630}
631
632static const struct file_operations format1_fops = {
633 .owner = THIS_MODULE,
634 .open = table_open,
635 .read = seq_read,
636 .llseek = seq_lseek,
637 .release = seq_release
638};
639
640static const struct file_operations format2_fops = {
641 .owner = THIS_MODULE,
642 .open = table_open,
643 .read = seq_read,
644 .llseek = seq_lseek,
645 .release = seq_release
646};
647
648static const struct file_operations format3_fops = {
649 .owner = THIS_MODULE,
650 .open = table_open,
651 .read = seq_read,
652 .llseek = seq_lseek,
653 .release = seq_release
654};
655
656static const struct file_operations format4_fops = {
657 .owner = THIS_MODULE,
658 .open = table_open,
659 .read = seq_read,
660 .llseek = seq_lseek,
661 .release = seq_release
662};
663
664/*
665 * dump lkb's on the ls_waiters list
666 */
667static ssize_t waiters_read(struct file *file, char __user *userbuf,
668 size_t count, loff_t *ppos)
669{
670 struct dlm_ls *ls = file->private_data;
671 struct dlm_lkb *lkb;
672 size_t len = DLM_DEBUG_BUF_LEN, pos = 0, ret, rv;
673
674 mutex_lock(&debug_buf_lock);
675 mutex_lock(&ls->ls_waiters_mutex);
676 memset(debug_buf, 0, sizeof(debug_buf));
677
678 list_for_each_entry(lkb, &ls->ls_waiters, lkb_wait_reply) {
679 ret = snprintf(debug_buf + pos, len - pos, "%x %d %d %s\n",
680 lkb->lkb_id, lkb->lkb_wait_type,
681 lkb->lkb_nodeid, lkb->lkb_resource->res_name);
682 if (ret >= len - pos)
683 break;
684 pos += ret;
685 }
686 mutex_unlock(&ls->ls_waiters_mutex);
687
688 rv = simple_read_from_buffer(userbuf, count, ppos, debug_buf, pos);
689 mutex_unlock(&debug_buf_lock);
690 return rv;
691}
692
693static const struct file_operations waiters_fops = {
694 .owner = THIS_MODULE,
695 .open = simple_open,
696 .read = waiters_read,
697 .llseek = default_llseek,
698};
699
700void dlm_delete_debug_file(struct dlm_ls *ls)
701{
702 debugfs_remove(ls->ls_debug_rsb_dentry);
703 debugfs_remove(ls->ls_debug_waiters_dentry);
704 debugfs_remove(ls->ls_debug_locks_dentry);
705 debugfs_remove(ls->ls_debug_all_dentry);
706 debugfs_remove(ls->ls_debug_toss_dentry);
707}
708
709int dlm_create_debug_file(struct dlm_ls *ls)
710{
711 char name[DLM_LOCKSPACE_LEN+8];
712
713 /* format 1 */
714
715 ls->ls_debug_rsb_dentry = debugfs_create_file(ls->ls_name,
716 S_IFREG | S_IRUGO,
717 dlm_root,
718 ls,
719 &format1_fops);
720 if (!ls->ls_debug_rsb_dentry)
721 goto fail;
722
723 /* format 2 */
724
725 memset(name, 0, sizeof(name));
726 snprintf(name, DLM_LOCKSPACE_LEN+8, "%s_locks", ls->ls_name);
727
728 ls->ls_debug_locks_dentry = debugfs_create_file(name,
729 S_IFREG | S_IRUGO,
730 dlm_root,
731 ls,
732 &format2_fops);
733 if (!ls->ls_debug_locks_dentry)
734 goto fail;
735
736 /* format 3 */
737
738 memset(name, 0, sizeof(name));
739 snprintf(name, DLM_LOCKSPACE_LEN+8, "%s_all", ls->ls_name);
740
741 ls->ls_debug_all_dentry = debugfs_create_file(name,
742 S_IFREG | S_IRUGO,
743 dlm_root,
744 ls,
745 &format3_fops);
746 if (!ls->ls_debug_all_dentry)
747 goto fail;
748
749 /* format 4 */
750
751 memset(name, 0, sizeof(name));
752 snprintf(name, DLM_LOCKSPACE_LEN+8, "%s_toss", ls->ls_name);
753
754 ls->ls_debug_toss_dentry = debugfs_create_file(name,
755 S_IFREG | S_IRUGO,
756 dlm_root,
757 ls,
758 &format4_fops);
759 if (!ls->ls_debug_toss_dentry)
760 goto fail;
761
762 memset(name, 0, sizeof(name));
763 snprintf(name, DLM_LOCKSPACE_LEN+8, "%s_waiters", ls->ls_name);
764
765 ls->ls_debug_waiters_dentry = debugfs_create_file(name,
766 S_IFREG | S_IRUGO,
767 dlm_root,
768 ls,
769 &waiters_fops);
770 if (!ls->ls_debug_waiters_dentry)
771 goto fail;
772
773 return 0;
774
775 fail:
776 dlm_delete_debug_file(ls);
777 return -ENOMEM;
778}
779
780int __init dlm_register_debugfs(void)
781{
782 mutex_init(&debug_buf_lock);
783 dlm_root = debugfs_create_dir("dlm", NULL);
784 return dlm_root ? 0 : -ENOMEM;
785}
786
787void dlm_unregister_debugfs(void)
788{
789 debugfs_remove(dlm_root);
790}
791
1// SPDX-License-Identifier: GPL-2.0-only
2/******************************************************************************
3*******************************************************************************
4**
5** Copyright (C) 2005-2009 Red Hat, Inc. All rights reserved.
6**
7**
8*******************************************************************************
9******************************************************************************/
10
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>
17
18#include "dlm_internal.h"
19#include "midcomms.h"
20#include "lock.h"
21#include "ast.h"
22
23#define DLM_DEBUG_BUF_LEN 4096
24static char debug_buf[DLM_DEBUG_BUF_LEN];
25static struct mutex debug_buf_lock;
26
27static struct dentry *dlm_root;
28static struct dentry *dlm_comms;
29
30static char *print_lockmode(int mode)
31{
32 switch (mode) {
33 case DLM_LOCK_IV:
34 return "--";
35 case DLM_LOCK_NL:
36 return "NL";
37 case DLM_LOCK_CR:
38 return "CR";
39 case DLM_LOCK_CW:
40 return "CW";
41 case DLM_LOCK_PR:
42 return "PR";
43 case DLM_LOCK_PW:
44 return "PW";
45 case DLM_LOCK_EX:
46 return "EX";
47 default:
48 return "??";
49 }
50}
51
52static void print_format1_lock(struct seq_file *s, struct dlm_lkb *lkb,
53 struct dlm_rsb *res)
54{
55 seq_printf(s, "%08x %s", lkb->lkb_id, print_lockmode(lkb->lkb_grmode));
56
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));
60
61 if (lkb->lkb_nodeid) {
62 if (lkb->lkb_nodeid != res->res_nodeid)
63 seq_printf(s, " Remote: %3d %08x", lkb->lkb_nodeid,
64 lkb->lkb_remid);
65 else
66 seq_printf(s, " Master: %08x", lkb->lkb_remid);
67 }
68
69 if (lkb->lkb_wait_type)
70 seq_printf(s, " wait_type: %d", lkb->lkb_wait_type);
71
72 seq_putc(s, '\n');
73}
74
75static void print_format1(struct dlm_rsb *res, struct seq_file *s)
76{
77 struct dlm_lkb *lkb;
78 int i, lvblen = res->res_ls->ls_lvblen, recover_list, root_list;
79
80 lock_rsb(res);
81
82 seq_printf(s, "\nResource %p Name (len=%d) \"", res, res->res_length);
83
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]);
87 else
88 seq_printf(s, "%c", '.');
89 }
90
91 if (res->res_nodeid > 0)
92 seq_printf(s, "\"\nLocal Copy, Master is node %d\n",
93 res->res_nodeid);
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",
98 res->res_first_lkid);
99 else
100 seq_printf(s, "\"\nInvalid master %d\n", res->res_nodeid);
101 if (seq_has_overflowed(s))
102 goto out;
103
104 /* Print the LVB: */
105 if (res->res_lvbptr) {
106 seq_puts(s, "LVB: ");
107 for (i = 0; i < lvblen; i++) {
108 if (i == lvblen / 2)
109 seq_puts(s, "\n ");
110 seq_printf(s, "%02x ",
111 (unsigned char) res->res_lvbptr[i]);
112 }
113 if (rsb_flag(res, RSB_VALNOTVALID))
114 seq_puts(s, " (INVALID)");
115 seq_putc(s, '\n');
116 if (seq_has_overflowed(s))
117 goto out;
118 }
119
120 root_list = !list_empty(&res->res_root_list);
121 recover_list = !list_empty(&res->res_recover_list);
122
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);
127 }
128
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))
134 goto out;
135 }
136
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))
141 goto out;
142 }
143
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))
148 goto out;
149 }
150
151 if (list_empty(&res->res_lookup))
152 goto out;
153
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);
160 seq_putc(s, '\n');
161 if (seq_has_overflowed(s))
162 goto out;
163 }
164 out:
165 unlock_rsb(res);
166}
167
168static void print_format2_lock(struct seq_file *s, struct dlm_lkb *lkb,
169 struct dlm_rsb *r)
170{
171 u64 xid = 0;
172 u64 us;
173
174 if (test_bit(DLM_DFL_USER_BIT, &lkb->lkb_dflags)) {
175 if (lkb->lkb_ua)
176 xid = lkb->lkb_ua->xid;
177 }
178
179 /* microseconds since lkb was added to current queue */
180 us = ktime_to_us(ktime_sub(ktime_get(), lkb->lkb_timestamp));
181
182 /* id nodeid remid pid xid exflags flags sts grmode rqmode time_us
183 r_nodeid r_len r_name */
184
185 seq_printf(s, "%x %d %x %u %llu %x %x %d %d %d %llu %u %d \"%s\"\n",
186 lkb->lkb_id,
187 lkb->lkb_nodeid,
188 lkb->lkb_remid,
189 lkb->lkb_ownpid,
190 (unsigned long long)xid,
191 lkb->lkb_exflags,
192 dlm_iflags_val(lkb),
193 lkb->lkb_status,
194 lkb->lkb_grmode,
195 lkb->lkb_rqmode,
196 (unsigned long long)us,
197 r->res_nodeid,
198 r->res_length,
199 r->res_name);
200}
201
202static void print_format2(struct dlm_rsb *r, struct seq_file *s)
203{
204 struct dlm_lkb *lkb;
205
206 lock_rsb(r);
207
208 list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
209 print_format2_lock(s, lkb, r);
210 if (seq_has_overflowed(s))
211 goto out;
212 }
213
214 list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
215 print_format2_lock(s, lkb, r);
216 if (seq_has_overflowed(s))
217 goto out;
218 }
219
220 list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue) {
221 print_format2_lock(s, lkb, r);
222 if (seq_has_overflowed(s))
223 goto out;
224 }
225 out:
226 unlock_rsb(r);
227}
228
229static void print_format3_lock(struct seq_file *s, struct dlm_lkb *lkb,
230 int rsb_lookup)
231{
232 u64 xid = 0;
233
234 if (test_bit(DLM_DFL_USER_BIT, &lkb->lkb_dflags)) {
235 if (lkb->lkb_ua)
236 xid = lkb->lkb_ua->xid;
237 }
238
239 seq_printf(s, "lkb %x %d %x %u %llu %x %x %d %d %d %d %d %d %u %llu %llu\n",
240 lkb->lkb_id,
241 lkb->lkb_nodeid,
242 lkb->lkb_remid,
243 lkb->lkb_ownpid,
244 (unsigned long long)xid,
245 lkb->lkb_exflags,
246 dlm_iflags_val(lkb),
247 lkb->lkb_status,
248 lkb->lkb_grmode,
249 lkb->lkb_rqmode,
250 lkb->lkb_last_bast_mode,
251 rsb_lookup,
252 lkb->lkb_wait_type,
253 lkb->lkb_lvbseq,
254 (unsigned long long)ktime_to_ns(lkb->lkb_timestamp),
255 (unsigned long long)ktime_to_ns(lkb->lkb_last_bast_time));
256}
257
258static void print_format3(struct dlm_rsb *r, struct seq_file *s)
259{
260 struct dlm_lkb *lkb;
261 int i, lvblen = r->res_ls->ls_lvblen;
262 int print_name = 1;
263
264 lock_rsb(r);
265
266 seq_printf(s, "rsb %p %d %x %lx %d %d %u %d ",
267 r,
268 r->res_nodeid,
269 r->res_first_lkid,
270 r->res_flags,
271 !list_empty(&r->res_root_list),
272 !list_empty(&r->res_recover_list),
273 r->res_recover_locks_count,
274 r->res_length);
275 if (seq_has_overflowed(s))
276 goto out;
277
278 for (i = 0; i < r->res_length; i++) {
279 if (!isascii(r->res_name[i]) || !isprint(r->res_name[i]))
280 print_name = 0;
281 }
282
283 seq_puts(s, print_name ? "str " : "hex");
284
285 for (i = 0; i < r->res_length; i++) {
286 if (print_name)
287 seq_printf(s, "%c", r->res_name[i]);
288 else
289 seq_printf(s, " %02x", (unsigned char)r->res_name[i]);
290 }
291 seq_putc(s, '\n');
292 if (seq_has_overflowed(s))
293 goto out;
294
295 if (!r->res_lvbptr)
296 goto do_locks;
297
298 seq_printf(s, "lvb %u %d", r->res_lvbseq, lvblen);
299
300 for (i = 0; i < lvblen; i++)
301 seq_printf(s, " %02x", (unsigned char)r->res_lvbptr[i]);
302 seq_putc(s, '\n');
303 if (seq_has_overflowed(s))
304 goto out;
305
306 do_locks:
307 list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
308 print_format3_lock(s, lkb, 0);
309 if (seq_has_overflowed(s))
310 goto out;
311 }
312
313 list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
314 print_format3_lock(s, lkb, 0);
315 if (seq_has_overflowed(s))
316 goto out;
317 }
318
319 list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue) {
320 print_format3_lock(s, lkb, 0);
321 if (seq_has_overflowed(s))
322 goto out;
323 }
324
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))
328 goto out;
329 }
330 out:
331 unlock_rsb(r);
332}
333
334static void print_format4(struct dlm_rsb *r, struct seq_file *s)
335{
336 int our_nodeid = dlm_our_nodeid();
337 int print_name = 1;
338 int i;
339
340 lock_rsb(r);
341
342 seq_printf(s, "rsb %p %d %d %d %d %lu %lx %d ",
343 r,
344 r->res_nodeid,
345 r->res_master_nodeid,
346 r->res_dir_nodeid,
347 our_nodeid,
348 r->res_toss_time,
349 r->res_flags,
350 r->res_length);
351
352 for (i = 0; i < r->res_length; i++) {
353 if (!isascii(r->res_name[i]) || !isprint(r->res_name[i]))
354 print_name = 0;
355 }
356
357 seq_puts(s, print_name ? "str " : "hex");
358
359 for (i = 0; i < r->res_length; i++) {
360 if (print_name)
361 seq_printf(s, "%c", r->res_name[i]);
362 else
363 seq_printf(s, " %02x", (unsigned char)r->res_name[i]);
364 }
365 seq_putc(s, '\n');
366 unlock_rsb(r);
367}
368
369static void print_format5_lock(struct seq_file *s, struct dlm_lkb *lkb)
370{
371 struct dlm_callback *cb;
372
373 /* lkb_id lkb_flags mode flags sb_status sb_flags */
374
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",
378 lkb->lkb_id,
379 dlm_iflags_val(lkb),
380 cb->mode,
381 cb->flags,
382 cb->sb_status,
383 cb->sb_flags);
384 }
385 spin_unlock(&lkb->lkb_cb_lock);
386}
387
388static void print_format5(struct dlm_rsb *r, struct seq_file *s)
389{
390 struct dlm_lkb *lkb;
391
392 lock_rsb(r);
393
394 list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
395 print_format5_lock(s, lkb);
396 if (seq_has_overflowed(s))
397 goto out;
398 }
399
400 list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
401 print_format5_lock(s, lkb);
402 if (seq_has_overflowed(s))
403 goto out;
404 }
405
406 list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue) {
407 print_format5_lock(s, lkb);
408 if (seq_has_overflowed(s))
409 goto out;
410 }
411 out:
412 unlock_rsb(r);
413}
414
415struct rsbtbl_iter {
416 struct dlm_rsb *rsb;
417 unsigned bucket;
418 int format;
419 int header;
420};
421
422/*
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
426 * a full buffer.
427 */
428
429static int table_seq_show(struct seq_file *seq, void *iter_ptr)
430{
431 struct rsbtbl_iter *ri = iter_ptr;
432
433 switch (ri->format) {
434 case 1:
435 print_format1(ri->rsb, seq);
436 break;
437 case 2:
438 if (ri->header) {
439 seq_puts(seq, "id nodeid remid pid xid exflags flags sts grmode rqmode time_ms r_nodeid r_len r_name\n");
440 ri->header = 0;
441 }
442 print_format2(ri->rsb, seq);
443 break;
444 case 3:
445 if (ri->header) {
446 seq_puts(seq, "rsb ptr nodeid first_lkid flags !root_list_empty !recover_list_empty recover_locks_count len\n");
447 ri->header = 0;
448 }
449 print_format3(ri->rsb, seq);
450 break;
451 case 4:
452 if (ri->header) {
453 seq_puts(seq, "rsb ptr nodeid master_nodeid dir_nodeid our_nodeid toss_time flags len str|hex name\n");
454 ri->header = 0;
455 }
456 print_format4(ri->rsb, seq);
457 break;
458 case 5:
459 if (ri->header) {
460 seq_puts(seq, "lkb_id lkb_flags mode flags sb_status sb_flags\n");
461 ri->header = 0;
462 }
463 print_format5(ri->rsb, seq);
464 break;
465 }
466
467 return 0;
468}
469
470static const struct seq_operations format1_seq_ops;
471static const struct seq_operations format2_seq_ops;
472static const struct seq_operations format3_seq_ops;
473static const struct seq_operations format4_seq_ops;
474static const struct seq_operations format5_seq_ops;
475
476static void *table_seq_start(struct seq_file *seq, loff_t *pos)
477{
478 struct rb_root *tree;
479 struct rb_node *node;
480 struct dlm_ls *ls = seq->private;
481 struct rsbtbl_iter *ri;
482 struct dlm_rsb *r;
483 loff_t n = *pos;
484 unsigned bucket, entry;
485 int toss = (seq->op == &format4_seq_ops);
486
487 bucket = n >> 32;
488 entry = n & ((1LL << 32) - 1);
489
490 if (bucket >= ls->ls_rsbtbl_size)
491 return NULL;
492
493 ri = kzalloc(sizeof(*ri), GFP_NOFS);
494 if (!ri)
495 return NULL;
496 if (n == 0)
497 ri->header = 1;
498 if (seq->op == &format1_seq_ops)
499 ri->format = 1;
500 if (seq->op == &format2_seq_ops)
501 ri->format = 2;
502 if (seq->op == &format3_seq_ops)
503 ri->format = 3;
504 if (seq->op == &format4_seq_ops)
505 ri->format = 4;
506 if (seq->op == &format5_seq_ops)
507 ri->format = 5;
508
509 tree = toss ? &ls->ls_rsbtbl[bucket].toss : &ls->ls_rsbtbl[bucket].keep;
510
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);
515 if (!entry--) {
516 dlm_hold_rsb(r);
517 ri->rsb = r;
518 ri->bucket = bucket;
519 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
520 return ri;
521 }
522 }
523 }
524 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
525
526 /*
527 * move to the first rsb in the next non-empty bucket
528 */
529
530 /* zero the entry */
531 n &= ~((1LL << 32) - 1);
532
533 while (1) {
534 bucket++;
535 n += 1LL << 32;
536
537 if (bucket >= ls->ls_rsbtbl_size) {
538 kfree(ri);
539 return NULL;
540 }
541 tree = toss ? &ls->ls_rsbtbl[bucket].toss : &ls->ls_rsbtbl[bucket].keep;
542
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);
547 dlm_hold_rsb(r);
548 ri->rsb = r;
549 ri->bucket = bucket;
550 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
551 *pos = n;
552 return ri;
553 }
554 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
555 }
556}
557
558static void *table_seq_next(struct seq_file *seq, void *iter_ptr, loff_t *pos)
559{
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;
565 loff_t n = *pos;
566 unsigned bucket;
567 int toss = (seq->op == &format4_seq_ops);
568
569 bucket = n >> 32;
570
571 /*
572 * move to the next rsb in the same bucket
573 */
574
575 spin_lock(&ls->ls_rsbtbl[bucket].lock);
576 rp = ri->rsb;
577 next = rb_next(&rp->res_hashnode);
578
579 if (next) {
580 r = rb_entry(next, struct dlm_rsb, res_hashnode);
581 dlm_hold_rsb(r);
582 ri->rsb = r;
583 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
584 dlm_put_rsb(rp);
585 ++*pos;
586 return ri;
587 }
588 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
589 dlm_put_rsb(rp);
590
591 /*
592 * move to the first rsb in the next non-empty bucket
593 */
594
595 /* zero the entry */
596 n &= ~((1LL << 32) - 1);
597
598 while (1) {
599 bucket++;
600 n += 1LL << 32;
601
602 if (bucket >= ls->ls_rsbtbl_size) {
603 kfree(ri);
604 ++*pos;
605 return NULL;
606 }
607 tree = toss ? &ls->ls_rsbtbl[bucket].toss : &ls->ls_rsbtbl[bucket].keep;
608
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);
613 dlm_hold_rsb(r);
614 ri->rsb = r;
615 ri->bucket = bucket;
616 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
617 *pos = n;
618 return ri;
619 }
620 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
621 }
622}
623
624static void table_seq_stop(struct seq_file *seq, void *iter_ptr)
625{
626 struct rsbtbl_iter *ri = iter_ptr;
627
628 if (ri) {
629 dlm_put_rsb(ri->rsb);
630 kfree(ri);
631 }
632}
633
634static 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,
639};
640
641static 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,
646};
647
648static 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,
653};
654
655static 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,
660};
661
662static 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,
667};
668
669static const struct file_operations format1_fops;
670static const struct file_operations format2_fops;
671static const struct file_operations format3_fops;
672static const struct file_operations format4_fops;
673static const struct file_operations format5_fops;
674
675static int table_open1(struct inode *inode, struct file *file)
676{
677 struct seq_file *seq;
678 int ret;
679
680 ret = seq_open(file, &format1_seq_ops);
681 if (ret)
682 return ret;
683
684 seq = file->private_data;
685 seq->private = inode->i_private; /* the dlm_ls */
686 return 0;
687}
688
689static int table_open2(struct inode *inode, struct file *file)
690{
691 struct seq_file *seq;
692 int ret;
693
694 ret = seq_open(file, &format2_seq_ops);
695 if (ret)
696 return ret;
697
698 seq = file->private_data;
699 seq->private = inode->i_private; /* the dlm_ls */
700 return 0;
701}
702
703static ssize_t table_write2(struct file *file, const char __user *user_buf,
704 size_t count, loff_t *ppos)
705{
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;
711 char buf[256] = {};
712 uint32_t lkb_id;
713
714 if (copy_from_user(buf, user_buf,
715 min_t(size_t, sizeof(buf) - 1, count)))
716 return -EFAULT;
717
718 n = sscanf(buf, "%x %" __stringify(DLM_RESNAME_MAXLEN) "s %x %d %d",
719 &lkb_id, name, &lkb_flags, &lkb_nodeid, &lkb_status);
720 if (n != 5)
721 return -EINVAL;
722
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);
726 if (error)
727 return error;
728
729 return count;
730}
731
732static int table_open3(struct inode *inode, struct file *file)
733{
734 struct seq_file *seq;
735 int ret;
736
737 ret = seq_open(file, &format3_seq_ops);
738 if (ret)
739 return ret;
740
741 seq = file->private_data;
742 seq->private = inode->i_private; /* the dlm_ls */
743 return 0;
744}
745
746static int table_open4(struct inode *inode, struct file *file)
747{
748 struct seq_file *seq;
749 int ret;
750
751 ret = seq_open(file, &format4_seq_ops);
752 if (ret)
753 return ret;
754
755 seq = file->private_data;
756 seq->private = inode->i_private; /* the dlm_ls */
757 return 0;
758}
759
760static int table_open5(struct inode *inode, struct file *file)
761{
762 struct seq_file *seq;
763 int ret;
764
765 ret = seq_open(file, &format5_seq_ops);
766 if (ret)
767 return ret;
768
769 seq = file->private_data;
770 seq->private = inode->i_private; /* the dlm_ls */
771 return 0;
772}
773
774static const struct file_operations format1_fops = {
775 .owner = THIS_MODULE,
776 .open = table_open1,
777 .read = seq_read,
778 .llseek = seq_lseek,
779 .release = seq_release
780};
781
782static const struct file_operations format2_fops = {
783 .owner = THIS_MODULE,
784 .open = table_open2,
785 .read = seq_read,
786 .write = table_write2,
787 .llseek = seq_lseek,
788 .release = seq_release
789};
790
791static const struct file_operations format3_fops = {
792 .owner = THIS_MODULE,
793 .open = table_open3,
794 .read = seq_read,
795 .llseek = seq_lseek,
796 .release = seq_release
797};
798
799static const struct file_operations format4_fops = {
800 .owner = THIS_MODULE,
801 .open = table_open4,
802 .read = seq_read,
803 .llseek = seq_lseek,
804 .release = seq_release
805};
806
807static const struct file_operations format5_fops = {
808 .owner = THIS_MODULE,
809 .open = table_open5,
810 .read = seq_read,
811 .llseek = seq_lseek,
812 .release = seq_release
813};
814
815/*
816 * dump lkb's on the ls_waiters list
817 */
818static ssize_t waiters_read(struct file *file, char __user *userbuf,
819 size_t count, loff_t *ppos)
820{
821 struct dlm_ls *ls = file->private_data;
822 struct dlm_lkb *lkb;
823 size_t len = DLM_DEBUG_BUF_LEN, pos = 0, ret, rv;
824
825 mutex_lock(&debug_buf_lock);
826 mutex_lock(&ls->ls_waiters_mutex);
827 memset(debug_buf, 0, sizeof(debug_buf));
828
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)
834 break;
835 pos += ret;
836 }
837 mutex_unlock(&ls->ls_waiters_mutex);
838
839 rv = simple_read_from_buffer(userbuf, count, ppos, debug_buf, pos);
840 mutex_unlock(&debug_buf_lock);
841 return rv;
842}
843
844static ssize_t waiters_write(struct file *file, const char __user *user_buf,
845 size_t count, loff_t *ppos)
846{
847 struct dlm_ls *ls = file->private_data;
848 int mstype, to_nodeid;
849 char buf[128] = {};
850 uint32_t lkb_id;
851 int n, error;
852
853 if (copy_from_user(buf, user_buf,
854 min_t(size_t, sizeof(buf) - 1, count)))
855 return -EFAULT;
856
857 n = sscanf(buf, "%x %d %d", &lkb_id, &mstype, &to_nodeid);
858 if (n != 3)
859 return -EINVAL;
860
861 error = dlm_debug_add_lkb_to_waiters(ls, lkb_id, mstype, to_nodeid);
862 if (error)
863 return error;
864
865 return count;
866}
867
868static const struct file_operations waiters_fops = {
869 .owner = THIS_MODULE,
870 .open = simple_open,
871 .read = waiters_read,
872 .write = waiters_write,
873 .llseek = default_llseek,
874};
875
876void dlm_delete_debug_file(struct dlm_ls *ls)
877{
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);
884}
885
886static int dlm_state_show(struct seq_file *file, void *offset)
887{
888 seq_printf(file, "%s\n", dlm_midcomms_state(file->private));
889 return 0;
890}
891DEFINE_SHOW_ATTRIBUTE(dlm_state);
892
893static int dlm_flags_show(struct seq_file *file, void *offset)
894{
895 seq_printf(file, "%lu\n", dlm_midcomms_flags(file->private));
896 return 0;
897}
898DEFINE_SHOW_ATTRIBUTE(dlm_flags);
899
900static int dlm_send_queue_cnt_show(struct seq_file *file, void *offset)
901{
902 seq_printf(file, "%d\n", dlm_midcomms_send_queue_cnt(file->private));
903 return 0;
904}
905DEFINE_SHOW_ATTRIBUTE(dlm_send_queue_cnt);
906
907static int dlm_version_show(struct seq_file *file, void *offset)
908{
909 seq_printf(file, "0x%08x\n", dlm_midcomms_version(file->private));
910 return 0;
911}
912DEFINE_SHOW_ATTRIBUTE(dlm_version);
913
914static ssize_t dlm_rawmsg_write(struct file *fp, const char __user *user_buf,
915 size_t count, loff_t *ppos)
916{
917 void *buf;
918 int ret;
919
920 if (count > PAGE_SIZE || count < sizeof(struct dlm_header))
921 return -EINVAL;
922
923 buf = kmalloc(PAGE_SIZE, GFP_NOFS);
924 if (!buf)
925 return -ENOMEM;
926
927 if (copy_from_user(buf, user_buf, count)) {
928 ret = -EFAULT;
929 goto out;
930 }
931
932 ret = dlm_midcomms_rawmsg_send(fp->private_data, buf, count);
933 if (ret)
934 goto out;
935
936 kfree(buf);
937 return count;
938
939out:
940 kfree(buf);
941 return ret;
942}
943
944static const struct file_operations dlm_rawmsg_fops = {
945 .open = simple_open,
946 .write = dlm_rawmsg_write,
947 .llseek = no_llseek,
948};
949
950void *dlm_create_debug_comms_file(int nodeid, void *data)
951{
952 struct dentry *d_node;
953 char name[256];
954
955 memset(name, 0, sizeof(name));
956 snprintf(name, 256, "%d", nodeid);
957
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);
965
966 return d_node;
967}
968
969void dlm_delete_debug_comms_file(void *ctx)
970{
971 debugfs_remove(ctx);
972}
973
974void dlm_create_debug_file(struct dlm_ls *ls)
975{
976 /* Reserve enough space for the longest file name */
977 char name[DLM_LOCKSPACE_LEN + sizeof("_queued_asts")];
978
979 /* format 1 */
980
981 ls->ls_debug_rsb_dentry = debugfs_create_file(ls->ls_name,
982 S_IFREG | S_IRUGO,
983 dlm_root,
984 ls,
985 &format1_fops);
986
987 /* format 2 */
988
989 snprintf(name, sizeof(name), "%s_locks", ls->ls_name);
990
991 ls->ls_debug_locks_dentry = debugfs_create_file(name,
992 0644,
993 dlm_root,
994 ls,
995 &format2_fops);
996
997 /* format 3 */
998
999 snprintf(name, sizeof(name), "%s_all", ls->ls_name);
1000
1001 ls->ls_debug_all_dentry = debugfs_create_file(name,
1002 S_IFREG | S_IRUGO,
1003 dlm_root,
1004 ls,
1005 &format3_fops);
1006
1007 /* format 4 */
1008
1009 snprintf(name, sizeof(name), "%s_toss", ls->ls_name);
1010
1011 ls->ls_debug_toss_dentry = debugfs_create_file(name,
1012 S_IFREG | S_IRUGO,
1013 dlm_root,
1014 ls,
1015 &format4_fops);
1016
1017 snprintf(name, sizeof(name), "%s_waiters", ls->ls_name);
1018
1019 ls->ls_debug_waiters_dentry = debugfs_create_file(name,
1020 0644,
1021 dlm_root,
1022 ls,
1023 &waiters_fops);
1024
1025 /* format 5 */
1026
1027 snprintf(name, sizeof(name), "%s_queued_asts", ls->ls_name);
1028
1029 ls->ls_debug_queued_asts_dentry = debugfs_create_file(name,
1030 0644,
1031 dlm_root,
1032 ls,
1033 &format5_fops);
1034}
1035
1036void __init dlm_register_debugfs(void)
1037{
1038 mutex_init(&debug_buf_lock);
1039 dlm_root = debugfs_create_dir("dlm", NULL);
1040 dlm_comms = debugfs_create_dir("comms", dlm_root);
1041}
1042
1043void dlm_unregister_debugfs(void)
1044{
1045 debugfs_remove(dlm_root);
1046}
1047