Linux Audio

Check our new training course

Yocto / OpenEmbedded training

Feb 10-13, 2025
Register
Loading...
v4.10.11
 
  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/init.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_open1(struct inode *inode, struct file *file)
611{
612	struct seq_file *seq;
613	int ret;
614
615	ret = seq_open(file, &format1_seq_ops);
616	if (ret)
617		return ret;
618
619	seq = file->private_data;
620	seq->private = inode->i_private; /* the dlm_ls */
621	return 0;
622}
623
624static int table_open2(struct inode *inode, struct file *file)
625{
626	struct seq_file *seq;
627	int ret;
628
629	ret = seq_open(file, &format2_seq_ops);
630	if (ret)
631		return ret;
632
633	seq = file->private_data;
634	seq->private = inode->i_private; /* the dlm_ls */
635	return 0;
636}
637
638static int table_open3(struct inode *inode, struct file *file)
639{
640	struct seq_file *seq;
641	int ret;
642
643	ret = seq_open(file, &format3_seq_ops);
644	if (ret)
645		return ret;
646
647	seq = file->private_data;
648	seq->private = inode->i_private; /* the dlm_ls */
649	return 0;
650}
651
652static int table_open4(struct inode *inode, struct file *file)
653{
654	struct seq_file *seq;
655	int ret;
656
657	ret = seq_open(file, &format4_seq_ops);
658	if (ret)
659		return ret;
660
661	seq = file->private_data;
662	seq->private = inode->i_private; /* the dlm_ls */
663	return 0;
664}
665
666static const struct file_operations format1_fops = {
667	.owner   = THIS_MODULE,
668	.open    = table_open1,
669	.read    = seq_read,
670	.llseek  = seq_lseek,
671	.release = seq_release
672};
673
674static const struct file_operations format2_fops = {
675	.owner   = THIS_MODULE,
676	.open    = table_open2,
677	.read    = seq_read,
678	.llseek  = seq_lseek,
679	.release = seq_release
680};
681
682static const struct file_operations format3_fops = {
683	.owner   = THIS_MODULE,
684	.open    = table_open3,
685	.read    = seq_read,
686	.llseek  = seq_lseek,
687	.release = seq_release
688};
689
690static const struct file_operations format4_fops = {
691	.owner   = THIS_MODULE,
692	.open    = table_open4,
693	.read    = seq_read,
694	.llseek  = seq_lseek,
695	.release = seq_release
696};
697
698/*
699 * dump lkb's on the ls_waiters list
700 */
701static ssize_t waiters_read(struct file *file, char __user *userbuf,
702			    size_t count, loff_t *ppos)
703{
704	struct dlm_ls *ls = file->private_data;
705	struct dlm_lkb *lkb;
706	size_t len = DLM_DEBUG_BUF_LEN, pos = 0, ret, rv;
707
708	mutex_lock(&debug_buf_lock);
709	mutex_lock(&ls->ls_waiters_mutex);
710	memset(debug_buf, 0, sizeof(debug_buf));
711
712	list_for_each_entry(lkb, &ls->ls_waiters, lkb_wait_reply) {
713		ret = snprintf(debug_buf + pos, len - pos, "%x %d %d %s\n",
714			       lkb->lkb_id, lkb->lkb_wait_type,
715			       lkb->lkb_nodeid, lkb->lkb_resource->res_name);
716		if (ret >= len - pos)
717			break;
718		pos += ret;
719	}
720	mutex_unlock(&ls->ls_waiters_mutex);
721
722	rv = simple_read_from_buffer(userbuf, count, ppos, debug_buf, pos);
723	mutex_unlock(&debug_buf_lock);
724	return rv;
725}
726
727static const struct file_operations waiters_fops = {
728	.owner   = THIS_MODULE,
729	.open    = simple_open,
730	.read    = waiters_read,
731	.llseek  = default_llseek,
732};
733
734void dlm_delete_debug_file(struct dlm_ls *ls)
735{
736	debugfs_remove(ls->ls_debug_rsb_dentry);
737	debugfs_remove(ls->ls_debug_waiters_dentry);
738	debugfs_remove(ls->ls_debug_locks_dentry);
739	debugfs_remove(ls->ls_debug_all_dentry);
740	debugfs_remove(ls->ls_debug_toss_dentry);
741}
742
743int dlm_create_debug_file(struct dlm_ls *ls)
744{
745	char name[DLM_LOCKSPACE_LEN+8];
746
747	/* format 1 */
748
749	ls->ls_debug_rsb_dentry = debugfs_create_file(ls->ls_name,
750						      S_IFREG | S_IRUGO,
751						      dlm_root,
752						      ls,
753						      &format1_fops);
754	if (!ls->ls_debug_rsb_dentry)
755		goto fail;
756
757	/* format 2 */
758
759	memset(name, 0, sizeof(name));
760	snprintf(name, DLM_LOCKSPACE_LEN+8, "%s_locks", ls->ls_name);
761
762	ls->ls_debug_locks_dentry = debugfs_create_file(name,
763							S_IFREG | S_IRUGO,
764							dlm_root,
765							ls,
766							&format2_fops);
767	if (!ls->ls_debug_locks_dentry)
768		goto fail;
769
770	/* format 3 */
771
772	memset(name, 0, sizeof(name));
773	snprintf(name, DLM_LOCKSPACE_LEN+8, "%s_all", ls->ls_name);
774
775	ls->ls_debug_all_dentry = debugfs_create_file(name,
776						      S_IFREG | S_IRUGO,
777						      dlm_root,
778						      ls,
779						      &format3_fops);
780	if (!ls->ls_debug_all_dentry)
781		goto fail;
782
783	/* format 4 */
784
785	memset(name, 0, sizeof(name));
786	snprintf(name, DLM_LOCKSPACE_LEN+8, "%s_toss", ls->ls_name);
787
788	ls->ls_debug_toss_dentry = debugfs_create_file(name,
789						       S_IFREG | S_IRUGO,
790						       dlm_root,
791						       ls,
792						       &format4_fops);
793	if (!ls->ls_debug_toss_dentry)
794		goto fail;
795
796	memset(name, 0, sizeof(name));
797	snprintf(name, DLM_LOCKSPACE_LEN+8, "%s_waiters", ls->ls_name);
798
799	ls->ls_debug_waiters_dentry = debugfs_create_file(name,
800							  S_IFREG | S_IRUGO,
801							  dlm_root,
802							  ls,
803							  &waiters_fops);
804	if (!ls->ls_debug_waiters_dentry)
805		goto fail;
806
807	return 0;
808
809 fail:
810	dlm_delete_debug_file(ls);
811	return -ENOMEM;
812}
813
814int __init dlm_register_debugfs(void)
815{
816	mutex_init(&debug_buf_lock);
817	dlm_root = debugfs_create_dir("dlm", NULL);
818	return dlm_root ? 0 : -ENOMEM;
819}
820
821void dlm_unregister_debugfs(void)
822{
823	debugfs_remove(dlm_root);
824}
825
v5.9
  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 "lock.h"
 20
 21#define DLM_DEBUG_BUF_LEN 4096
 22static char debug_buf[DLM_DEBUG_BUF_LEN];
 23static struct mutex debug_buf_lock;
 24
 25static struct dentry *dlm_root;
 26
 27static char *print_lockmode(int mode)
 28{
 29	switch (mode) {
 30	case DLM_LOCK_IV:
 31		return "--";
 32	case DLM_LOCK_NL:
 33		return "NL";
 34	case DLM_LOCK_CR:
 35		return "CR";
 36	case DLM_LOCK_CW:
 37		return "CW";
 38	case DLM_LOCK_PR:
 39		return "PR";
 40	case DLM_LOCK_PW:
 41		return "PW";
 42	case DLM_LOCK_EX:
 43		return "EX";
 44	default:
 45		return "??";
 46	}
 47}
 48
 49static void print_format1_lock(struct seq_file *s, struct dlm_lkb *lkb,
 50			       struct dlm_rsb *res)
 51{
 52	seq_printf(s, "%08x %s", lkb->lkb_id, print_lockmode(lkb->lkb_grmode));
 53
 54	if (lkb->lkb_status == DLM_LKSTS_CONVERT ||
 55	    lkb->lkb_status == DLM_LKSTS_WAITING)
 56		seq_printf(s, " (%s)", print_lockmode(lkb->lkb_rqmode));
 57
 58	if (lkb->lkb_nodeid) {
 59		if (lkb->lkb_nodeid != res->res_nodeid)
 60			seq_printf(s, " Remote: %3d %08x", lkb->lkb_nodeid,
 61				   lkb->lkb_remid);
 62		else
 63			seq_printf(s, " Master:     %08x", lkb->lkb_remid);
 64	}
 65
 66	if (lkb->lkb_wait_type)
 67		seq_printf(s, " wait_type: %d", lkb->lkb_wait_type);
 68
 69	seq_putc(s, '\n');
 70}
 71
 72static void print_format1(struct dlm_rsb *res, struct seq_file *s)
 73{
 74	struct dlm_lkb *lkb;
 75	int i, lvblen = res->res_ls->ls_lvblen, recover_list, root_list;
 76
 77	lock_rsb(res);
 78
 79	seq_printf(s, "\nResource %p Name (len=%d) \"", res, res->res_length);
 80
 81	for (i = 0; i < res->res_length; i++) {
 82		if (isprint(res->res_name[i]))
 83			seq_printf(s, "%c", res->res_name[i]);
 84		else
 85			seq_printf(s, "%c", '.');
 86	}
 87
 88	if (res->res_nodeid > 0)
 89		seq_printf(s, "\"\nLocal Copy, Master is node %d\n",
 90			   res->res_nodeid);
 91	else if (res->res_nodeid == 0)
 92		seq_puts(s, "\"\nMaster Copy\n");
 93	else if (res->res_nodeid == -1)
 94		seq_printf(s, "\"\nLooking up master (lkid %x)\n",
 95			   res->res_first_lkid);
 96	else
 97		seq_printf(s, "\"\nInvalid master %d\n", res->res_nodeid);
 98	if (seq_has_overflowed(s))
 99		goto out;
100
101	/* Print the LVB: */
102	if (res->res_lvbptr) {
103		seq_puts(s, "LVB: ");
104		for (i = 0; i < lvblen; i++) {
105			if (i == lvblen / 2)
106				seq_puts(s, "\n     ");
107			seq_printf(s, "%02x ",
108				   (unsigned char) res->res_lvbptr[i]);
109		}
110		if (rsb_flag(res, RSB_VALNOTVALID))
111			seq_puts(s, " (INVALID)");
112		seq_putc(s, '\n');
113		if (seq_has_overflowed(s))
114			goto out;
115	}
116
117	root_list = !list_empty(&res->res_root_list);
118	recover_list = !list_empty(&res->res_recover_list);
119
120	if (root_list || recover_list) {
121		seq_printf(s, "Recovery: root %d recover %d flags %lx count %d\n",
122			   root_list, recover_list,
123			   res->res_flags, res->res_recover_locks_count);
124	}
125
126	/* Print the locks attached to this resource */
127	seq_puts(s, "Granted Queue\n");
128	list_for_each_entry(lkb, &res->res_grantqueue, lkb_statequeue) {
129		print_format1_lock(s, lkb, res);
130		if (seq_has_overflowed(s))
131			goto out;
132	}
133
134	seq_puts(s, "Conversion Queue\n");
135	list_for_each_entry(lkb, &res->res_convertqueue, lkb_statequeue) {
136		print_format1_lock(s, lkb, res);
137		if (seq_has_overflowed(s))
138			goto out;
139	}
140
141	seq_puts(s, "Waiting Queue\n");
142	list_for_each_entry(lkb, &res->res_waitqueue, lkb_statequeue) {
143		print_format1_lock(s, lkb, res);
144		if (seq_has_overflowed(s))
145			goto out;
146	}
147
148	if (list_empty(&res->res_lookup))
149		goto out;
150
151	seq_puts(s, "Lookup Queue\n");
152	list_for_each_entry(lkb, &res->res_lookup, lkb_rsb_lookup) {
153		seq_printf(s, "%08x %s",
154			   lkb->lkb_id, print_lockmode(lkb->lkb_rqmode));
155		if (lkb->lkb_wait_type)
156			seq_printf(s, " wait_type: %d", lkb->lkb_wait_type);
157		seq_putc(s, '\n');
158		if (seq_has_overflowed(s))
159			goto out;
160	}
161 out:
162	unlock_rsb(res);
163}
164
165static void print_format2_lock(struct seq_file *s, struct dlm_lkb *lkb,
166			       struct dlm_rsb *r)
167{
168	u64 xid = 0;
169	u64 us;
170
171	if (lkb->lkb_flags & DLM_IFL_USER) {
172		if (lkb->lkb_ua)
173			xid = lkb->lkb_ua->xid;
174	}
175
176	/* microseconds since lkb was added to current queue */
177	us = ktime_to_us(ktime_sub(ktime_get(), lkb->lkb_timestamp));
178
179	/* id nodeid remid pid xid exflags flags sts grmode rqmode time_us
180	   r_nodeid r_len r_name */
181
182	seq_printf(s, "%x %d %x %u %llu %x %x %d %d %d %llu %u %d \"%s\"\n",
183		   lkb->lkb_id,
184		   lkb->lkb_nodeid,
185		   lkb->lkb_remid,
186		   lkb->lkb_ownpid,
187		   (unsigned long long)xid,
188		   lkb->lkb_exflags,
189		   lkb->lkb_flags,
190		   lkb->lkb_status,
191		   lkb->lkb_grmode,
192		   lkb->lkb_rqmode,
193		   (unsigned long long)us,
194		   r->res_nodeid,
195		   r->res_length,
196		   r->res_name);
197}
198
199static void print_format2(struct dlm_rsb *r, struct seq_file *s)
200{
201	struct dlm_lkb *lkb;
202
203	lock_rsb(r);
204
205	list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
206		print_format2_lock(s, lkb, r);
207		if (seq_has_overflowed(s))
208			goto out;
209	}
210
211	list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
212		print_format2_lock(s, lkb, r);
213		if (seq_has_overflowed(s))
214			goto out;
215	}
216
217	list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue) {
218		print_format2_lock(s, lkb, r);
219		if (seq_has_overflowed(s))
220			goto out;
221	}
222 out:
223	unlock_rsb(r);
224}
225
226static void print_format3_lock(struct seq_file *s, struct dlm_lkb *lkb,
227			      int rsb_lookup)
228{
229	u64 xid = 0;
230
231	if (lkb->lkb_flags & DLM_IFL_USER) {
232		if (lkb->lkb_ua)
233			xid = lkb->lkb_ua->xid;
234	}
235
236	seq_printf(s, "lkb %x %d %x %u %llu %x %x %d %d %d %d %d %d %u %llu %llu\n",
237		   lkb->lkb_id,
238		   lkb->lkb_nodeid,
239		   lkb->lkb_remid,
240		   lkb->lkb_ownpid,
241		   (unsigned long long)xid,
242		   lkb->lkb_exflags,
243		   lkb->lkb_flags,
244		   lkb->lkb_status,
245		   lkb->lkb_grmode,
246		   lkb->lkb_rqmode,
247		   lkb->lkb_last_bast.mode,
248		   rsb_lookup,
249		   lkb->lkb_wait_type,
250		   lkb->lkb_lvbseq,
251		   (unsigned long long)ktime_to_ns(lkb->lkb_timestamp),
252		   (unsigned long long)ktime_to_ns(lkb->lkb_last_bast_time));
253}
254
255static void print_format3(struct dlm_rsb *r, struct seq_file *s)
256{
257	struct dlm_lkb *lkb;
258	int i, lvblen = r->res_ls->ls_lvblen;
259	int print_name = 1;
260
261	lock_rsb(r);
262
263	seq_printf(s, "rsb %p %d %x %lx %d %d %u %d ",
264		   r,
265		   r->res_nodeid,
266		   r->res_first_lkid,
267		   r->res_flags,
268		   !list_empty(&r->res_root_list),
269		   !list_empty(&r->res_recover_list),
270		   r->res_recover_locks_count,
271		   r->res_length);
272	if (seq_has_overflowed(s))
273		goto out;
274
275	for (i = 0; i < r->res_length; i++) {
276		if (!isascii(r->res_name[i]) || !isprint(r->res_name[i]))
277			print_name = 0;
278	}
279
280	seq_puts(s, print_name ? "str " : "hex");
281
282	for (i = 0; i < r->res_length; i++) {
283		if (print_name)
284			seq_printf(s, "%c", r->res_name[i]);
285		else
286			seq_printf(s, " %02x", (unsigned char)r->res_name[i]);
287	}
288	seq_putc(s, '\n');
289	if (seq_has_overflowed(s))
290		goto out;
291
292	if (!r->res_lvbptr)
293		goto do_locks;
294
295	seq_printf(s, "lvb %u %d", r->res_lvbseq, lvblen);
296
297	for (i = 0; i < lvblen; i++)
298		seq_printf(s, " %02x", (unsigned char)r->res_lvbptr[i]);
299	seq_putc(s, '\n');
300	if (seq_has_overflowed(s))
301		goto out;
302
303 do_locks:
304	list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
305		print_format3_lock(s, lkb, 0);
306		if (seq_has_overflowed(s))
307			goto out;
308	}
309
310	list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
311		print_format3_lock(s, lkb, 0);
312		if (seq_has_overflowed(s))
313			goto out;
314	}
315
316	list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue) {
317		print_format3_lock(s, lkb, 0);
318		if (seq_has_overflowed(s))
319			goto out;
320	}
321
322	list_for_each_entry(lkb, &r->res_lookup, lkb_rsb_lookup) {
323		print_format3_lock(s, lkb, 1);
324		if (seq_has_overflowed(s))
325			goto out;
326	}
327 out:
328	unlock_rsb(r);
329}
330
331static void print_format4(struct dlm_rsb *r, struct seq_file *s)
332{
333	int our_nodeid = dlm_our_nodeid();
334	int print_name = 1;
335	int i;
336
337	lock_rsb(r);
338
339	seq_printf(s, "rsb %p %d %d %d %d %lu %lx %d ",
340		   r,
341		   r->res_nodeid,
342		   r->res_master_nodeid,
343		   r->res_dir_nodeid,
344		   our_nodeid,
345		   r->res_toss_time,
346		   r->res_flags,
347		   r->res_length);
348
349	for (i = 0; i < r->res_length; i++) {
350		if (!isascii(r->res_name[i]) || !isprint(r->res_name[i]))
351			print_name = 0;
352	}
353
354	seq_puts(s, print_name ? "str " : "hex");
355
356	for (i = 0; i < r->res_length; i++) {
357		if (print_name)
358			seq_printf(s, "%c", r->res_name[i]);
359		else
360			seq_printf(s, " %02x", (unsigned char)r->res_name[i]);
361	}
362	seq_putc(s, '\n');
 
363	unlock_rsb(r);
364}
365
366struct rsbtbl_iter {
367	struct dlm_rsb *rsb;
368	unsigned bucket;
369	int format;
370	int header;
371};
372
373/*
374 * If the buffer is full, seq_printf can be called again, but it
375 * does nothing.  So, the these printing routines periodically check
376 * seq_has_overflowed to avoid wasting too much time trying to print to
377 * a full buffer.
378 */
379
380static int table_seq_show(struct seq_file *seq, void *iter_ptr)
381{
382	struct rsbtbl_iter *ri = iter_ptr;
383
384	switch (ri->format) {
385	case 1:
386		print_format1(ri->rsb, seq);
387		break;
388	case 2:
389		if (ri->header) {
390			seq_puts(seq, "id nodeid remid pid xid exflags flags sts grmode rqmode time_ms r_nodeid r_len r_name\n");
391			ri->header = 0;
392		}
393		print_format2(ri->rsb, seq);
394		break;
395	case 3:
396		if (ri->header) {
397			seq_puts(seq, "version rsb 1.1 lvb 1.1 lkb 1.1\n");
398			ri->header = 0;
399		}
400		print_format3(ri->rsb, seq);
401		break;
402	case 4:
403		if (ri->header) {
404			seq_puts(seq, "version 4 rsb 2\n");
405			ri->header = 0;
406		}
407		print_format4(ri->rsb, seq);
408		break;
409	}
410
411	return 0;
412}
413
414static const struct seq_operations format1_seq_ops;
415static const struct seq_operations format2_seq_ops;
416static const struct seq_operations format3_seq_ops;
417static const struct seq_operations format4_seq_ops;
418
419static void *table_seq_start(struct seq_file *seq, loff_t *pos)
420{
421	struct rb_root *tree;
422	struct rb_node *node;
423	struct dlm_ls *ls = seq->private;
424	struct rsbtbl_iter *ri;
425	struct dlm_rsb *r;
426	loff_t n = *pos;
427	unsigned bucket, entry;
428	int toss = (seq->op == &format4_seq_ops);
429
430	bucket = n >> 32;
431	entry = n & ((1LL << 32) - 1);
432
433	if (bucket >= ls->ls_rsbtbl_size)
434		return NULL;
435
436	ri = kzalloc(sizeof(*ri), GFP_NOFS);
437	if (!ri)
438		return NULL;
439	if (n == 0)
440		ri->header = 1;
441	if (seq->op == &format1_seq_ops)
442		ri->format = 1;
443	if (seq->op == &format2_seq_ops)
444		ri->format = 2;
445	if (seq->op == &format3_seq_ops)
446		ri->format = 3;
447	if (seq->op == &format4_seq_ops)
448		ri->format = 4;
449
450	tree = toss ? &ls->ls_rsbtbl[bucket].toss : &ls->ls_rsbtbl[bucket].keep;
451
452	spin_lock(&ls->ls_rsbtbl[bucket].lock);
453	if (!RB_EMPTY_ROOT(tree)) {
454		for (node = rb_first(tree); node; node = rb_next(node)) {
455			r = rb_entry(node, struct dlm_rsb, res_hashnode);
456			if (!entry--) {
457				dlm_hold_rsb(r);
458				ri->rsb = r;
459				ri->bucket = bucket;
460				spin_unlock(&ls->ls_rsbtbl[bucket].lock);
461				return ri;
462			}
463		}
464	}
465	spin_unlock(&ls->ls_rsbtbl[bucket].lock);
466
467	/*
468	 * move to the first rsb in the next non-empty bucket
469	 */
470
471	/* zero the entry */
472	n &= ~((1LL << 32) - 1);
473
474	while (1) {
475		bucket++;
476		n += 1LL << 32;
477
478		if (bucket >= ls->ls_rsbtbl_size) {
479			kfree(ri);
480			return NULL;
481		}
482		tree = toss ? &ls->ls_rsbtbl[bucket].toss : &ls->ls_rsbtbl[bucket].keep;
483
484		spin_lock(&ls->ls_rsbtbl[bucket].lock);
485		if (!RB_EMPTY_ROOT(tree)) {
486			node = rb_first(tree);
487			r = rb_entry(node, struct dlm_rsb, res_hashnode);
488			dlm_hold_rsb(r);
489			ri->rsb = r;
490			ri->bucket = bucket;
491			spin_unlock(&ls->ls_rsbtbl[bucket].lock);
492			*pos = n;
493			return ri;
494		}
495		spin_unlock(&ls->ls_rsbtbl[bucket].lock);
496	}
497}
498
499static void *table_seq_next(struct seq_file *seq, void *iter_ptr, loff_t *pos)
500{
501	struct dlm_ls *ls = seq->private;
502	struct rsbtbl_iter *ri = iter_ptr;
503	struct rb_root *tree;
504	struct rb_node *next;
505	struct dlm_rsb *r, *rp;
506	loff_t n = *pos;
507	unsigned bucket;
508	int toss = (seq->op == &format4_seq_ops);
509
510	bucket = n >> 32;
511
512	/*
513	 * move to the next rsb in the same bucket
514	 */
515
516	spin_lock(&ls->ls_rsbtbl[bucket].lock);
517	rp = ri->rsb;
518	next = rb_next(&rp->res_hashnode);
519
520	if (next) {
521		r = rb_entry(next, struct dlm_rsb, res_hashnode);
522		dlm_hold_rsb(r);
523		ri->rsb = r;
524		spin_unlock(&ls->ls_rsbtbl[bucket].lock);
525		dlm_put_rsb(rp);
526		++*pos;
527		return ri;
528	}
529	spin_unlock(&ls->ls_rsbtbl[bucket].lock);
530	dlm_put_rsb(rp);
531
532	/*
533	 * move to the first rsb in the next non-empty bucket
534	 */
535
536	/* zero the entry */
537	n &= ~((1LL << 32) - 1);
538
539	while (1) {
540		bucket++;
541		n += 1LL << 32;
542
543		if (bucket >= ls->ls_rsbtbl_size) {
544			kfree(ri);
545			return NULL;
546		}
547		tree = toss ? &ls->ls_rsbtbl[bucket].toss : &ls->ls_rsbtbl[bucket].keep;
548
549		spin_lock(&ls->ls_rsbtbl[bucket].lock);
550		if (!RB_EMPTY_ROOT(tree)) {
551			next = rb_first(tree);
552			r = rb_entry(next, struct dlm_rsb, res_hashnode);
553			dlm_hold_rsb(r);
554			ri->rsb = r;
555			ri->bucket = bucket;
556			spin_unlock(&ls->ls_rsbtbl[bucket].lock);
557			*pos = n;
558			return ri;
559		}
560		spin_unlock(&ls->ls_rsbtbl[bucket].lock);
561	}
562}
563
564static void table_seq_stop(struct seq_file *seq, void *iter_ptr)
565{
566	struct rsbtbl_iter *ri = iter_ptr;
567
568	if (ri) {
569		dlm_put_rsb(ri->rsb);
570		kfree(ri);
571	}
572}
573
574static const struct seq_operations format1_seq_ops = {
575	.start = table_seq_start,
576	.next  = table_seq_next,
577	.stop  = table_seq_stop,
578	.show  = table_seq_show,
579};
580
581static const struct seq_operations format2_seq_ops = {
582	.start = table_seq_start,
583	.next  = table_seq_next,
584	.stop  = table_seq_stop,
585	.show  = table_seq_show,
586};
587
588static const struct seq_operations format3_seq_ops = {
589	.start = table_seq_start,
590	.next  = table_seq_next,
591	.stop  = table_seq_stop,
592	.show  = table_seq_show,
593};
594
595static const struct seq_operations format4_seq_ops = {
596	.start = table_seq_start,
597	.next  = table_seq_next,
598	.stop  = table_seq_stop,
599	.show  = table_seq_show,
600};
601
602static const struct file_operations format1_fops;
603static const struct file_operations format2_fops;
604static const struct file_operations format3_fops;
605static const struct file_operations format4_fops;
606
607static int table_open1(struct inode *inode, struct file *file)
608{
609	struct seq_file *seq;
610	int ret;
611
612	ret = seq_open(file, &format1_seq_ops);
613	if (ret)
614		return ret;
615
616	seq = file->private_data;
617	seq->private = inode->i_private; /* the dlm_ls */
618	return 0;
619}
620
621static int table_open2(struct inode *inode, struct file *file)
622{
623	struct seq_file *seq;
624	int ret;
625
626	ret = seq_open(file, &format2_seq_ops);
627	if (ret)
628		return ret;
629
630	seq = file->private_data;
631	seq->private = inode->i_private; /* the dlm_ls */
632	return 0;
633}
634
635static int table_open3(struct inode *inode, struct file *file)
636{
637	struct seq_file *seq;
638	int ret;
639
640	ret = seq_open(file, &format3_seq_ops);
641	if (ret)
642		return ret;
643
644	seq = file->private_data;
645	seq->private = inode->i_private; /* the dlm_ls */
646	return 0;
647}
648
649static int table_open4(struct inode *inode, struct file *file)
650{
651	struct seq_file *seq;
652	int ret;
653
654	ret = seq_open(file, &format4_seq_ops);
655	if (ret)
656		return ret;
657
658	seq = file->private_data;
659	seq->private = inode->i_private; /* the dlm_ls */
660	return 0;
661}
662
663static const struct file_operations format1_fops = {
664	.owner   = THIS_MODULE,
665	.open    = table_open1,
666	.read    = seq_read,
667	.llseek  = seq_lseek,
668	.release = seq_release
669};
670
671static const struct file_operations format2_fops = {
672	.owner   = THIS_MODULE,
673	.open    = table_open2,
674	.read    = seq_read,
675	.llseek  = seq_lseek,
676	.release = seq_release
677};
678
679static const struct file_operations format3_fops = {
680	.owner   = THIS_MODULE,
681	.open    = table_open3,
682	.read    = seq_read,
683	.llseek  = seq_lseek,
684	.release = seq_release
685};
686
687static const struct file_operations format4_fops = {
688	.owner   = THIS_MODULE,
689	.open    = table_open4,
690	.read    = seq_read,
691	.llseek  = seq_lseek,
692	.release = seq_release
693};
694
695/*
696 * dump lkb's on the ls_waiters list
697 */
698static ssize_t waiters_read(struct file *file, char __user *userbuf,
699			    size_t count, loff_t *ppos)
700{
701	struct dlm_ls *ls = file->private_data;
702	struct dlm_lkb *lkb;
703	size_t len = DLM_DEBUG_BUF_LEN, pos = 0, ret, rv;
704
705	mutex_lock(&debug_buf_lock);
706	mutex_lock(&ls->ls_waiters_mutex);
707	memset(debug_buf, 0, sizeof(debug_buf));
708
709	list_for_each_entry(lkb, &ls->ls_waiters, lkb_wait_reply) {
710		ret = snprintf(debug_buf + pos, len - pos, "%x %d %d %s\n",
711			       lkb->lkb_id, lkb->lkb_wait_type,
712			       lkb->lkb_nodeid, lkb->lkb_resource->res_name);
713		if (ret >= len - pos)
714			break;
715		pos += ret;
716	}
717	mutex_unlock(&ls->ls_waiters_mutex);
718
719	rv = simple_read_from_buffer(userbuf, count, ppos, debug_buf, pos);
720	mutex_unlock(&debug_buf_lock);
721	return rv;
722}
723
724static const struct file_operations waiters_fops = {
725	.owner   = THIS_MODULE,
726	.open    = simple_open,
727	.read    = waiters_read,
728	.llseek  = default_llseek,
729};
730
731void dlm_delete_debug_file(struct dlm_ls *ls)
732{
733	debugfs_remove(ls->ls_debug_rsb_dentry);
734	debugfs_remove(ls->ls_debug_waiters_dentry);
735	debugfs_remove(ls->ls_debug_locks_dentry);
736	debugfs_remove(ls->ls_debug_all_dentry);
737	debugfs_remove(ls->ls_debug_toss_dentry);
738}
739
740void dlm_create_debug_file(struct dlm_ls *ls)
741{
742	char name[DLM_LOCKSPACE_LEN + 8];
743
744	/* format 1 */
745
746	ls->ls_debug_rsb_dentry = debugfs_create_file(ls->ls_name,
747						      S_IFREG | S_IRUGO,
748						      dlm_root,
749						      ls,
750						      &format1_fops);
 
 
751
752	/* format 2 */
753
754	memset(name, 0, sizeof(name));
755	snprintf(name, DLM_LOCKSPACE_LEN + 8, "%s_locks", ls->ls_name);
756
757	ls->ls_debug_locks_dentry = debugfs_create_file(name,
758							S_IFREG | S_IRUGO,
759							dlm_root,
760							ls,
761							&format2_fops);
 
 
762
763	/* format 3 */
764
765	memset(name, 0, sizeof(name));
766	snprintf(name, DLM_LOCKSPACE_LEN + 8, "%s_all", ls->ls_name);
767
768	ls->ls_debug_all_dentry = debugfs_create_file(name,
769						      S_IFREG | S_IRUGO,
770						      dlm_root,
771						      ls,
772						      &format3_fops);
 
 
773
774	/* format 4 */
775
776	memset(name, 0, sizeof(name));
777	snprintf(name, DLM_LOCKSPACE_LEN + 8, "%s_toss", ls->ls_name);
778
779	ls->ls_debug_toss_dentry = debugfs_create_file(name,
780						       S_IFREG | S_IRUGO,
781						       dlm_root,
782						       ls,
783						       &format4_fops);
 
 
784
785	memset(name, 0, sizeof(name));
786	snprintf(name, DLM_LOCKSPACE_LEN + 8, "%s_waiters", ls->ls_name);
787
788	ls->ls_debug_waiters_dentry = debugfs_create_file(name,
789							  S_IFREG | S_IRUGO,
790							  dlm_root,
791							  ls,
792							  &waiters_fops);
 
 
 
 
 
 
 
 
793}
794
795void __init dlm_register_debugfs(void)
796{
797	mutex_init(&debug_buf_lock);
798	dlm_root = debugfs_create_dir("dlm", NULL);
 
799}
800
801void dlm_unregister_debugfs(void)
802{
803	debugfs_remove(dlm_root);
804}
805