debug_fs.c 18.5 KB
Newer Older
1 2 3
/******************************************************************************
*******************************************************************************
**
4
**  Copyright (C) 2005-2009 Red Hat, Inc.  All rights reserved.
5 6 7 8 9 10 11 12 13 14
**
**  This copyrighted material is made available to anyone wishing to use,
**  modify, copy, or redistribute it subject to the terms and conditions
**  of the GNU General Public License v.2.
**
*******************************************************************************
******************************************************************************/

#include <linux/pagemap.h>
#include <linux/seq_file.h>
15
#include <linux/init.h>
16 17
#include <linux/ctype.h>
#include <linux/debugfs.h>
18
#include <linux/slab.h>
19 20

#include "dlm_internal.h"
21
#include "lock.h"
22

D
David Teigland 已提交
23 24 25
#define DLM_DEBUG_BUF_LEN 4096
static char debug_buf[DLM_DEBUG_BUF_LEN];
static struct mutex debug_buf_lock;
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

static struct dentry *dlm_root;

static char *print_lockmode(int mode)
{
	switch (mode) {
	case DLM_LOCK_IV:
		return "--";
	case DLM_LOCK_NL:
		return "NL";
	case DLM_LOCK_CR:
		return "CR";
	case DLM_LOCK_CW:
		return "CW";
	case DLM_LOCK_PR:
		return "PR";
	case DLM_LOCK_PW:
		return "PW";
	case DLM_LOCK_EX:
		return "EX";
	default:
		return "??";
	}
}

51 52
static void print_format1_lock(struct seq_file *s, struct dlm_lkb *lkb,
			       struct dlm_rsb *res)
53 54 55
{
	seq_printf(s, "%08x %s", lkb->lkb_id, print_lockmode(lkb->lkb_grmode));

56 57
	if (lkb->lkb_status == DLM_LKSTS_CONVERT ||
	    lkb->lkb_status == DLM_LKSTS_WAITING)
58 59 60 61 62 63 64 65 66 67 68 69 70
		seq_printf(s, " (%s)", print_lockmode(lkb->lkb_rqmode));

	if (lkb->lkb_nodeid) {
		if (lkb->lkb_nodeid != res->res_nodeid)
			seq_printf(s, " Remote: %3d %08x", lkb->lkb_nodeid,
				   lkb->lkb_remid);
		else
			seq_printf(s, " Master:     %08x", lkb->lkb_remid);
	}

	if (lkb->lkb_wait_type)
		seq_printf(s, " wait_type: %d", lkb->lkb_wait_type);

71
	seq_puts(s, "\n");
72 73
}

74
static void print_format1(struct dlm_rsb *res, struct seq_file *s)
75 76
{
	struct dlm_lkb *lkb;
D
David Teigland 已提交
77
	int i, lvblen = res->res_ls->ls_lvblen, recover_list, root_list;
78

D
David Teigland 已提交
79 80
	lock_rsb(res);

81
	seq_printf(s, "\nResource %p Name (len=%d) \"", res, res->res_length);
82

83 84 85 86 87 88
	for (i = 0; i < res->res_length; i++) {
		if (isprint(res->res_name[i]))
			seq_printf(s, "%c", res->res_name[i]);
		else
			seq_printf(s, "%c", '.');
	}
89

90
	if (res->res_nodeid > 0)
91 92
		seq_printf(s, "\"\nLocal Copy, Master is node %d\n",
			   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 97
		seq_printf(s, "\"\nLooking up master (lkid %x)\n",
			   res->res_first_lkid);
98
	else
99 100
		seq_printf(s, "\"\nInvalid master %d\n", res->res_nodeid);
	if (seq_has_overflowed(s))
101
		goto out;
102 103 104

	/* Print the LVB: */
	if (res->res_lvbptr) {
105
		seq_puts(s, "LVB: ");
106 107
		for (i = 0; i < lvblen; i++) {
			if (i == lvblen / 2)
108
				seq_puts(s, "\n     ");
109 110 111 112
			seq_printf(s, "%02x ",
				   (unsigned char) res->res_lvbptr[i]);
		}
		if (rsb_flag(res, RSB_VALNOTVALID))
113
			seq_puts(s, " (INVALID)");
114 115
		seq_puts(s, "\n");
		if (seq_has_overflowed(s))
116
			goto out;
117 118
	}

D
David Teigland 已提交
119 120 121 122
	root_list = !list_empty(&res->res_root_list);
	recover_list = !list_empty(&res->res_recover_list);

	if (root_list || recover_list) {
123 124 125
		seq_printf(s, "Recovery: root %d recover %d flags %lx count %d\n",
			   root_list, recover_list,
			   res->res_flags, res->res_recover_locks_count);
D
David Teigland 已提交
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 132
		print_format1_lock(s, lkb, res);
		if (seq_has_overflowed(s))
133 134
			goto out;
	}
135

136
	seq_puts(s, "Conversion Queue\n");
137
	list_for_each_entry(lkb, &res->res_convertqueue, lkb_statequeue) {
138 139
		print_format1_lock(s, lkb, res);
		if (seq_has_overflowed(s))
140 141
			goto out;
	}
142

143
	seq_puts(s, "Waiting Queue\n");
144
	list_for_each_entry(lkb, &res->res_waitqueue, lkb_statequeue) {
145 146
		print_format1_lock(s, lkb, res);
		if (seq_has_overflowed(s))
147 148
			goto out;
	}
149

D
David Teigland 已提交
150 151 152
	if (list_empty(&res->res_lookup))
		goto out;

153
	seq_puts(s, "Lookup Queue\n");
D
David Teigland 已提交
154
	list_for_each_entry(lkb, &res->res_lookup, lkb_rsb_lookup) {
155 156
		seq_printf(s, "%08x %s",
			   lkb->lkb_id, print_lockmode(lkb->lkb_rqmode));
D
David Teigland 已提交
157 158
		if (lkb->lkb_wait_type)
			seq_printf(s, " wait_type: %d", lkb->lkb_wait_type);
159 160 161
		seq_puts(s, "\n");
		if (seq_has_overflowed(s))
			goto out;
D
David Teigland 已提交
162 163
	}
 out:
D
David Teigland 已提交
164 165 166
	unlock_rsb(res);
}

167 168
static void print_format2_lock(struct seq_file *s, struct dlm_lkb *lkb,
			       struct dlm_rsb *r)
D
David Teigland 已提交
169
{
D
David Teigland 已提交
170 171
	u64 xid = 0;
	u64 us;
D
David Teigland 已提交
172 173

	if (lkb->lkb_flags & DLM_IFL_USER) {
174 175
		if (lkb->lkb_ua)
			xid = lkb->lkb_ua->xid;
D
David Teigland 已提交
176 177
	}

D
David Teigland 已提交
178 179
	/* microseconds since lkb was added to current queue */
	us = ktime_to_us(ktime_sub(ktime_get(), lkb->lkb_timestamp));
D
David Teigland 已提交
180

D
David Teigland 已提交
181
	/* id nodeid remid pid xid exflags flags sts grmode rqmode time_us
D
David Teigland 已提交
182
	   r_nodeid r_len r_name */
D
David Teigland 已提交
183

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
	seq_printf(s, "%x %d %x %u %llu %x %x %d %d %d %llu %u %d \"%s\"\n",
		   lkb->lkb_id,
		   lkb->lkb_nodeid,
		   lkb->lkb_remid,
		   lkb->lkb_ownpid,
		   (unsigned long long)xid,
		   lkb->lkb_exflags,
		   lkb->lkb_flags,
		   lkb->lkb_status,
		   lkb->lkb_grmode,
		   lkb->lkb_rqmode,
		   (unsigned long long)us,
		   r->res_nodeid,
		   r->res_length,
		   r->res_name);
D
David Teigland 已提交
199 200
}

201
static void print_format2(struct dlm_rsb *r, struct seq_file *s)
D
David Teigland 已提交
202 203 204 205 206
{
	struct dlm_lkb *lkb;

	lock_rsb(r);

207
	list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
208 209
		print_format2_lock(s, lkb, r);
		if (seq_has_overflowed(s))
210 211
			goto out;
	}
D
David Teigland 已提交
212

213
	list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
214 215
		print_format2_lock(s, lkb, r);
		if (seq_has_overflowed(s))
216 217
			goto out;
	}
D
David Teigland 已提交
218

219
	list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue) {
220 221
		print_format2_lock(s, lkb, r);
		if (seq_has_overflowed(s))
222 223 224
			goto out;
	}
 out:
D
David Teigland 已提交
225 226 227
	unlock_rsb(r);
}

228
static void print_format3_lock(struct seq_file *s, struct dlm_lkb *lkb,
229
			      int rsb_lookup)
D
David Teigland 已提交
230 231 232 233 234 235 236 237
{
	u64 xid = 0;

	if (lkb->lkb_flags & DLM_IFL_USER) {
		if (lkb->lkb_ua)
			xid = lkb->lkb_ua->xid;
	}

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
	seq_printf(s, "lkb %x %d %x %u %llu %x %x %d %d %d %d %d %d %u %llu %llu\n",
		   lkb->lkb_id,
		   lkb->lkb_nodeid,
		   lkb->lkb_remid,
		   lkb->lkb_ownpid,
		   (unsigned long long)xid,
		   lkb->lkb_exflags,
		   lkb->lkb_flags,
		   lkb->lkb_status,
		   lkb->lkb_grmode,
		   lkb->lkb_rqmode,
		   lkb->lkb_last_bast.mode,
		   rsb_lookup,
		   lkb->lkb_wait_type,
		   lkb->lkb_lvbseq,
		   (unsigned long long)ktime_to_ns(lkb->lkb_timestamp),
		   (unsigned long long)ktime_to_ns(lkb->lkb_last_bast_time));
D
David Teigland 已提交
255 256
}

257
static void print_format3(struct dlm_rsb *r, struct seq_file *s)
D
David Teigland 已提交
258 259 260 261 262 263 264
{
	struct dlm_lkb *lkb;
	int i, lvblen = r->res_ls->ls_lvblen;
	int print_name = 1;

	lock_rsb(r);

265 266 267 268 269 270 271 272 273 274
	seq_printf(s, "rsb %p %d %x %lx %d %d %u %d ",
		   r,
		   r->res_nodeid,
		   r->res_first_lkid,
		   r->res_flags,
		   !list_empty(&r->res_root_list),
		   !list_empty(&r->res_recover_list),
		   r->res_recover_locks_count,
		   r->res_length);
	if (seq_has_overflowed(s))
275
		goto out;
D
David Teigland 已提交
276 277 278 279 280 281

	for (i = 0; i < r->res_length; i++) {
		if (!isascii(r->res_name[i]) || !isprint(r->res_name[i]))
			print_name = 0;
	}

282
	seq_puts(s, print_name ? "str " : "hex");
D
David Teigland 已提交
283 284 285 286 287 288 289

	for (i = 0; i < r->res_length; i++) {
		if (print_name)
			seq_printf(s, "%c", r->res_name[i]);
		else
			seq_printf(s, " %02x", (unsigned char)r->res_name[i]);
	}
290 291
	seq_puts(s, "\n");
	if (seq_has_overflowed(s))
292
		goto out;
D
David Teigland 已提交
293 294 295 296 297 298 299 300

	if (!r->res_lvbptr)
		goto do_locks;

	seq_printf(s, "lvb %u %d", r->res_lvbseq, lvblen);

	for (i = 0; i < lvblen; i++)
		seq_printf(s, " %02x", (unsigned char)r->res_lvbptr[i]);
301 302
	seq_puts(s, "\n");
	if (seq_has_overflowed(s))
303
		goto out;
D
David Teigland 已提交
304 305

 do_locks:
306
	list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
307 308
		print_format3_lock(s, lkb, 0);
		if (seq_has_overflowed(s))
309
			goto out;
310 311
	}

312
	list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
313 314
		print_format3_lock(s, lkb, 0);
		if (seq_has_overflowed(s))
315
			goto out;
316 317
	}

318
	list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue) {
319 320
		print_format3_lock(s, lkb, 0);
		if (seq_has_overflowed(s))
321
			goto out;
322 323
	}

324
	list_for_each_entry(lkb, &r->res_lookup, lkb_rsb_lookup) {
325 326
		print_format3_lock(s, lkb, 1);
		if (seq_has_overflowed(s))
327
			goto out;
328
	}
329 330
 out:
	unlock_rsb(r);
331 332
}

333
static void print_format4(struct dlm_rsb *r, struct seq_file *s)
334 335 336
{
	int our_nodeid = dlm_our_nodeid();
	int print_name = 1;
337
	int i;
338 339 340

	lock_rsb(r);

341 342 343 344 345 346 347 348 349
	seq_printf(s, "rsb %p %d %d %d %d %lu %lx %d ",
		   r,
		   r->res_nodeid,
		   r->res_master_nodeid,
		   r->res_dir_nodeid,
		   our_nodeid,
		   r->res_toss_time,
		   r->res_flags,
		   r->res_length);
350 351 352 353 354 355

	for (i = 0; i < r->res_length; i++) {
		if (!isascii(r->res_name[i]) || !isprint(r->res_name[i]))
			print_name = 0;
	}

356
	seq_puts(s, print_name ? "str " : "hex");
357 358 359 360 361 362 363

	for (i = 0; i < r->res_length; i++) {
		if (print_name)
			seq_printf(s, "%c", r->res_name[i]);
		else
			seq_printf(s, " %02x", (unsigned char)r->res_name[i]);
	}
364 365
	seq_puts(s, "\n");

366 367 368
	unlock_rsb(r);
}

369 370 371 372 373 374
struct rsbtbl_iter {
	struct dlm_rsb *rsb;
	unsigned bucket;
	int format;
	int header;
};
375

376 377 378 379 380 381
/*
 * If the buffer is full, seq_printf can be called again, but it
 * does nothing.  So, the these printing routines periodically check
 * seq_has_overflowed to avoid wasting too much time trying to print to
 * a full buffer.
 */
382 383

static int table_seq_show(struct seq_file *seq, void *iter_ptr)
384
{
385
	struct rsbtbl_iter *ri = iter_ptr;
386

D
David Teigland 已提交
387 388
	switch (ri->format) {
	case 1:
389
		print_format1(ri->rsb, seq);
D
David Teigland 已提交
390 391
		break;
	case 2:
D
David Teigland 已提交
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");
D
David Teigland 已提交
394 395
			ri->header = 0;
		}
396
		print_format2(ri->rsb, seq);
D
David Teigland 已提交
397 398 399
		break;
	case 3:
		if (ri->header) {
400
			seq_puts(seq, "version rsb 1.1 lvb 1.1 lkb 1.1\n");
D
David Teigland 已提交
401 402
			ri->header = 0;
		}
403
		print_format3(ri->rsb, seq);
D
David Teigland 已提交
404
		break;
405 406
	case 4:
		if (ri->header) {
407
			seq_puts(seq, "version 4 rsb 2\n");
408 409
			ri->header = 0;
		}
410
		print_format4(ri->rsb, seq);
411
		break;
D
David Teigland 已提交
412
	}
413

414
	return 0;
415 416
}

J
James Morris 已提交
417 418 419
static const struct seq_operations format1_seq_ops;
static const struct seq_operations format2_seq_ops;
static const struct seq_operations format3_seq_ops;
420
static const struct seq_operations format4_seq_ops;
421

422
static void *table_seq_start(struct seq_file *seq, loff_t *pos)
423
{
424
	struct rb_root *tree;
B
Bob Peterson 已提交
425
	struct rb_node *node;
426 427 428 429 430
	struct dlm_ls *ls = seq->private;
	struct rsbtbl_iter *ri;
	struct dlm_rsb *r;
	loff_t n = *pos;
	unsigned bucket, entry;
431
	int toss = (seq->op == &format4_seq_ops);
432

433 434
	bucket = n >> 32;
	entry = n & ((1LL << 32) - 1);
D
David Teigland 已提交
435

436 437
	if (bucket >= ls->ls_rsbtbl_size)
		return NULL;
D
David Teigland 已提交
438

D
David Teigland 已提交
439
	ri = kzalloc(sizeof(struct rsbtbl_iter), GFP_NOFS);
D
David Teigland 已提交
440 441
	if (!ri)
		return NULL;
442
	if (n == 0)
D
David Teigland 已提交
443
		ri->header = 1;
444 445 446 447 448 449
	if (seq->op == &format1_seq_ops)
		ri->format = 1;
	if (seq->op == &format2_seq_ops)
		ri->format = 2;
	if (seq->op == &format3_seq_ops)
		ri->format = 3;
450 451 452 453
	if (seq->op == &format4_seq_ops)
		ri->format = 4;

	tree = toss ? &ls->ls_rsbtbl[bucket].toss : &ls->ls_rsbtbl[bucket].keep;
454

455
	spin_lock(&ls->ls_rsbtbl[bucket].lock);
456 457
	if (!RB_EMPTY_ROOT(tree)) {
		for (node = rb_first(tree); node; node = rb_next(node)) {
B
Bob Peterson 已提交
458
			r = rb_entry(node, struct dlm_rsb, res_hashnode);
459 460 461 462
			if (!entry--) {
				dlm_hold_rsb(r);
				ri->rsb = r;
				ri->bucket = bucket;
463
				spin_unlock(&ls->ls_rsbtbl[bucket].lock);
464 465 466
				return ri;
			}
		}
D
David Teigland 已提交
467
	}
468
	spin_unlock(&ls->ls_rsbtbl[bucket].lock);
D
David Teigland 已提交
469

470 471 472
	/*
	 * move to the first rsb in the next non-empty bucket
	 */
D
David Teigland 已提交
473

474 475
	/* zero the entry */
	n &= ~((1LL << 32) - 1);
D
David Teigland 已提交
476

477 478 479
	while (1) {
		bucket++;
		n += 1LL << 32;
D
David Teigland 已提交
480

481 482
		if (bucket >= ls->ls_rsbtbl_size) {
			kfree(ri);
D
David Teigland 已提交
483 484
			return NULL;
		}
485
		tree = toss ? &ls->ls_rsbtbl[bucket].toss : &ls->ls_rsbtbl[bucket].keep;
D
David Teigland 已提交
486

487
		spin_lock(&ls->ls_rsbtbl[bucket].lock);
488 489
		if (!RB_EMPTY_ROOT(tree)) {
			node = rb_first(tree);
B
Bob Peterson 已提交
490
			r = rb_entry(node, struct dlm_rsb, res_hashnode);
491 492 493
			dlm_hold_rsb(r);
			ri->rsb = r;
			ri->bucket = bucket;
494
			spin_unlock(&ls->ls_rsbtbl[bucket].lock);
495 496 497
			*pos = n;
			return ri;
		}
498
		spin_unlock(&ls->ls_rsbtbl[bucket].lock);
499
	}
D
David Teigland 已提交
500 501
}

502
static void *table_seq_next(struct seq_file *seq, void *iter_ptr, loff_t *pos)
D
David Teigland 已提交
503
{
504 505
	struct dlm_ls *ls = seq->private;
	struct rsbtbl_iter *ri = iter_ptr;
506
	struct rb_root *tree;
B
Bob Peterson 已提交
507
	struct rb_node *next;
508 509 510
	struct dlm_rsb *r, *rp;
	loff_t n = *pos;
	unsigned bucket;
511
	int toss = (seq->op == &format4_seq_ops);
512 513 514 515 516 517 518

	bucket = n >> 32;

	/*
	 * move to the next rsb in the same bucket
	 */

519
	spin_lock(&ls->ls_rsbtbl[bucket].lock);
520
	rp = ri->rsb;
B
Bob Peterson 已提交
521
	next = rb_next(&rp->res_hashnode);
522

B
Bob Peterson 已提交
523 524
	if (next) {
		r = rb_entry(next, struct dlm_rsb, res_hashnode);
525 526
		dlm_hold_rsb(r);
		ri->rsb = r;
527
		spin_unlock(&ls->ls_rsbtbl[bucket].lock);
528 529 530 531
		dlm_put_rsb(rp);
		++*pos;
		return ri;
	}
532
	spin_unlock(&ls->ls_rsbtbl[bucket].lock);
533
	dlm_put_rsb(rp);
D
David Teigland 已提交
534

535 536 537
	/*
	 * move to the first rsb in the next non-empty bucket
	 */
D
David Teigland 已提交
538

539 540
	/* zero the entry */
	n &= ~((1LL << 32) - 1);
D
David Teigland 已提交
541

542 543 544
	while (1) {
		bucket++;
		n += 1LL << 32;
D
David Teigland 已提交
545

546 547 548 549
		if (bucket >= ls->ls_rsbtbl_size) {
			kfree(ri);
			return NULL;
		}
550
		tree = toss ? &ls->ls_rsbtbl[bucket].toss : &ls->ls_rsbtbl[bucket].keep;
D
David Teigland 已提交
551

552
		spin_lock(&ls->ls_rsbtbl[bucket].lock);
553 554
		if (!RB_EMPTY_ROOT(tree)) {
			next = rb_first(tree);
B
Bob Peterson 已提交
555
			r = rb_entry(next, struct dlm_rsb, res_hashnode);
556 557 558
			dlm_hold_rsb(r);
			ri->rsb = r;
			ri->bucket = bucket;
559
			spin_unlock(&ls->ls_rsbtbl[bucket].lock);
560 561 562
			*pos = n;
			return ri;
		}
563
		spin_unlock(&ls->ls_rsbtbl[bucket].lock);
D
David Teigland 已提交
564 565 566
	}
}

567
static void table_seq_stop(struct seq_file *seq, void *iter_ptr)
D
David Teigland 已提交
568
{
569
	struct rsbtbl_iter *ri = iter_ptr;
D
David Teigland 已提交
570

571 572 573
	if (ri) {
		dlm_put_rsb(ri->rsb);
		kfree(ri);
D
David Teigland 已提交
574 575 576
	}
}

J
James Morris 已提交
577
static const struct seq_operations format1_seq_ops = {
578 579 580 581
	.start = table_seq_start,
	.next  = table_seq_next,
	.stop  = table_seq_stop,
	.show  = table_seq_show,
D
David Teigland 已提交
582 583
};

J
James Morris 已提交
584
static const struct seq_operations format2_seq_ops = {
585 586 587 588 589 590
	.start = table_seq_start,
	.next  = table_seq_next,
	.stop  = table_seq_stop,
	.show  = table_seq_show,
};

J
James Morris 已提交
591
static const struct seq_operations format3_seq_ops = {
592 593 594 595 596 597
	.start = table_seq_start,
	.next  = table_seq_next,
	.stop  = table_seq_stop,
	.show  = table_seq_show,
};

598 599 600 601 602 603 604
static const struct seq_operations format4_seq_ops = {
	.start = table_seq_start,
	.next  = table_seq_next,
	.stop  = table_seq_stop,
	.show  = table_seq_show,
};

605 606 607
static const struct file_operations format1_fops;
static const struct file_operations format2_fops;
static const struct file_operations format3_fops;
608
static const struct file_operations format4_fops;
609

610
static int table_open1(struct inode *inode, struct file *file)
D
David Teigland 已提交
611 612
{
	struct seq_file *seq;
613
	int ret;
614

615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
	ret = seq_open(file, &format1_seq_ops);
	if (ret)
		return ret;

	seq = file->private_data;
	seq->private = inode->i_private; /* the dlm_ls */
	return 0;
}

static int table_open2(struct inode *inode, struct file *file)
{
	struct seq_file *seq;
	int ret;

	ret = seq_open(file, &format2_seq_ops);
	if (ret)
		return ret;

	seq = file->private_data;
	seq->private = inode->i_private; /* the dlm_ls */
	return 0;
}

static int table_open3(struct inode *inode, struct file *file)
{
	struct seq_file *seq;
	int ret;

	ret = seq_open(file, &format3_seq_ops);
	if (ret)
		return ret;

	seq = file->private_data;
	seq->private = inode->i_private; /* the dlm_ls */
	return 0;
}

static int table_open4(struct inode *inode, struct file *file)
{
	struct seq_file *seq;
	int ret;
D
David Teigland 已提交
656

657
	ret = seq_open(file, &format4_seq_ops);
D
David Teigland 已提交
658 659 660 661
	if (ret)
		return ret;

	seq = file->private_data;
662
	seq->private = inode->i_private; /* the dlm_ls */
D
David Teigland 已提交
663 664 665
	return 0;
}

666 667
static const struct file_operations format1_fops = {
	.owner   = THIS_MODULE,
668
	.open    = table_open1,
669 670 671 672 673 674 675
	.read    = seq_read,
	.llseek  = seq_lseek,
	.release = seq_release
};

static const struct file_operations format2_fops = {
	.owner   = THIS_MODULE,
676
	.open    = table_open2,
677 678 679 680 681 682
	.read    = seq_read,
	.llseek  = seq_lseek,
	.release = seq_release
};

static const struct file_operations format3_fops = {
D
David Teigland 已提交
683
	.owner   = THIS_MODULE,
684
	.open    = table_open3,
D
David Teigland 已提交
685 686 687 688 689
	.read    = seq_read,
	.llseek  = seq_lseek,
	.release = seq_release
};

690 691
static const struct file_operations format4_fops = {
	.owner   = THIS_MODULE,
692
	.open    = table_open4,
693 694 695 696 697
	.read    = seq_read,
	.llseek  = seq_lseek,
	.release = seq_release
};

D
David Teigland 已提交
698 699 700 701 702 703 704 705
/*
 * dump lkb's on the ls_waiters list
 */
static ssize_t waiters_read(struct file *file, char __user *userbuf,
			    size_t count, loff_t *ppos)
{
	struct dlm_ls *ls = file->private_data;
	struct dlm_lkb *lkb;
D
David Teigland 已提交
706
	size_t len = DLM_DEBUG_BUF_LEN, pos = 0, ret, rv;
D
David Teigland 已提交
707 708 709 710 711 712

	mutex_lock(&debug_buf_lock);
	mutex_lock(&ls->ls_waiters_mutex);
	memset(debug_buf, 0, sizeof(debug_buf));

	list_for_each_entry(lkb, &ls->ls_waiters, lkb_wait_reply) {
D
David Teigland 已提交
713 714 715 716 717 718
		ret = snprintf(debug_buf + pos, len - pos, "%x %d %d %s\n",
			       lkb->lkb_id, lkb->lkb_wait_type,
			       lkb->lkb_nodeid, lkb->lkb_resource->res_name);
		if (ret >= len - pos)
			break;
		pos += ret;
D
David Teigland 已提交
719 720 721 722 723 724 725 726
	}
	mutex_unlock(&ls->ls_waiters_mutex);

	rv = simple_read_from_buffer(userbuf, count, ppos, debug_buf, pos);
	mutex_unlock(&debug_buf_lock);
	return rv;
}

727
static const struct file_operations waiters_fops = {
D
David Teigland 已提交
728
	.owner   = THIS_MODULE,
729
	.open    = simple_open,
730 731
	.read    = waiters_read,
	.llseek  = default_llseek,
D
David Teigland 已提交
732 733
};

D
David Teigland 已提交
734 735
void dlm_delete_debug_file(struct dlm_ls *ls)
{
736 737 738 739 740
	debugfs_remove(ls->ls_debug_rsb_dentry);
	debugfs_remove(ls->ls_debug_waiters_dentry);
	debugfs_remove(ls->ls_debug_locks_dentry);
	debugfs_remove(ls->ls_debug_all_dentry);
	debugfs_remove(ls->ls_debug_toss_dentry);
D
David Teigland 已提交
741 742
}

743 744
int dlm_create_debug_file(struct dlm_ls *ls)
{
D
David Teigland 已提交
745 746
	char name[DLM_LOCKSPACE_LEN+8];

D
David Teigland 已提交
747 748
	/* format 1 */

D
David Teigland 已提交
749 750 751 752
	ls->ls_debug_rsb_dentry = debugfs_create_file(ls->ls_name,
						      S_IFREG | S_IRUGO,
						      dlm_root,
						      ls,
753
						      &format1_fops);
D
David Teigland 已提交
754
	if (!ls->ls_debug_rsb_dentry)
D
David Teigland 已提交
755
		goto fail;
D
David Teigland 已提交
756

D
David Teigland 已提交
757
	/* format 2 */
D
David Teigland 已提交
758

D
David Teigland 已提交
759
	memset(name, 0, sizeof(name));
D
David Teigland 已提交
760 761 762 763 764 765
	snprintf(name, DLM_LOCKSPACE_LEN+8, "%s_locks", ls->ls_name);

	ls->ls_debug_locks_dentry = debugfs_create_file(name,
							S_IFREG | S_IRUGO,
							dlm_root,
							ls,
766
							&format2_fops);
D
David Teigland 已提交
767 768 769 770 771 772 773 774 775 776 777 778
	if (!ls->ls_debug_locks_dentry)
		goto fail;

	/* format 3 */

	memset(name, 0, sizeof(name));
	snprintf(name, DLM_LOCKSPACE_LEN+8, "%s_all", ls->ls_name);

	ls->ls_debug_all_dentry = debugfs_create_file(name,
						      S_IFREG | S_IRUGO,
						      dlm_root,
						      ls,
779
						      &format3_fops);
D
David Teigland 已提交
780 781 782
	if (!ls->ls_debug_all_dentry)
		goto fail;

783 784 785 786 787 788 789 790 791 792 793 794 795
	/* format 4 */

	memset(name, 0, sizeof(name));
	snprintf(name, DLM_LOCKSPACE_LEN+8, "%s_toss", ls->ls_name);

	ls->ls_debug_toss_dentry = debugfs_create_file(name,
						       S_IFREG | S_IRUGO,
						       dlm_root,
						       ls,
						       &format4_fops);
	if (!ls->ls_debug_toss_dentry)
		goto fail;

D
David Teigland 已提交
796 797 798 799 800 801 802 803 804 805
	memset(name, 0, sizeof(name));
	snprintf(name, DLM_LOCKSPACE_LEN+8, "%s_waiters", ls->ls_name);

	ls->ls_debug_waiters_dentry = debugfs_create_file(name,
							  S_IFREG | S_IRUGO,
							  dlm_root,
							  ls,
							  &waiters_fops);
	if (!ls->ls_debug_waiters_dentry)
		goto fail;
D
David Teigland 已提交
806

D
David Teigland 已提交
807
	return 0;
808

D
David Teigland 已提交
809 810 811
 fail:
	dlm_delete_debug_file(ls);
	return -ENOMEM;
812 813
}

814
int __init dlm_register_debugfs(void)
815
{
D
David Teigland 已提交
816
	mutex_init(&debug_buf_lock);
817 818 819 820 821 822 823 824 825
	dlm_root = debugfs_create_dir("dlm", NULL);
	return dlm_root ? 0 : -ENOMEM;
}

void dlm_unregister_debugfs(void)
{
	debugfs_remove(dlm_root);
}