debug_fs.c 16.3 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 15 16 17 18 19
**
**  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>
#include <linux/module.h>
#include <linux/ctype.h>
#include <linux/debugfs.h>

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

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

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 "??";
	}
}

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

55 56
	if (lkb->lkb_status == DLM_LKSTS_CONVERT ||
	    lkb->lkb_status == DLM_LKSTS_WAITING)
57 58 59 60 61 62 63 64 65 66 67 68 69
		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);

70
	return seq_printf(s, "\n");
71 72
}

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

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

81 82 83 84 85
	rv = seq_printf(s, "\nResource %p Name (len=%d) \"",
			res, res->res_length);
	if (rv)
		goto out;

86 87 88 89 90 91
	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", '.');
	}
92

93
	if (res->res_nodeid > 0)
94 95
		rv = seq_printf(s, "\"  \nLocal Copy, Master is node %d\n",
				res->res_nodeid);
96
	else if (res->res_nodeid == 0)
97
		rv = seq_printf(s, "\"  \nMaster Copy\n");
98
	else if (res->res_nodeid == -1)
99 100
		rv = seq_printf(s, "\"  \nLooking up master (lkid %x)\n",
			   	res->res_first_lkid);
101
	else
102 103 104 105
		rv = seq_printf(s, "\"  \nInvalid master %d\n",
				res->res_nodeid);
	if (rv)
		goto out;
106 107 108 109 110 111 112 113 114 115 116 117

	/* Print the LVB: */
	if (res->res_lvbptr) {
		seq_printf(s, "LVB: ");
		for (i = 0; i < lvblen; i++) {
			if (i == lvblen / 2)
				seq_printf(s, "\n     ");
			seq_printf(s, "%02x ",
				   (unsigned char) res->res_lvbptr[i]);
		}
		if (rsb_flag(res, RSB_VALNOTVALID))
			seq_printf(s, " (INVALID)");
118 119 120
		rv = seq_printf(s, "\n");
		if (rv)
			goto out;
121 122
	}

D
David Teigland 已提交
123 124 125 126
	root_list = !list_empty(&res->res_root_list);
	recover_list = !list_empty(&res->res_recover_list);

	if (root_list || recover_list) {
127 128 129 130 131
		rv = 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);
		if (rv)
			goto out;
D
David Teigland 已提交
132 133
	}

134 135
	/* Print the locks attached to this resource */
	seq_printf(s, "Granted Queue\n");
136 137 138 139 140
	list_for_each_entry(lkb, &res->res_grantqueue, lkb_statequeue) {
		rv = print_format1_lock(s, lkb, res);
		if (rv)
			goto out;
	}
141 142

	seq_printf(s, "Conversion Queue\n");
143 144 145 146 147
	list_for_each_entry(lkb, &res->res_convertqueue, lkb_statequeue) {
		rv = print_format1_lock(s, lkb, res);
		if (rv)
			goto out;
	}
148 149

	seq_printf(s, "Waiting Queue\n");
150 151 152 153 154
	list_for_each_entry(lkb, &res->res_waitqueue, lkb_statequeue) {
		rv = print_format1_lock(s, lkb, res);
		if (rv)
			goto out;
	}
155

D
David Teigland 已提交
156 157 158 159 160
	if (list_empty(&res->res_lookup))
		goto out;

	seq_printf(s, "Lookup Queue\n");
	list_for_each_entry(lkb, &res->res_lookup, lkb_rsb_lookup) {
161 162
		rv = seq_printf(s, "%08x %s", lkb->lkb_id,
				print_lockmode(lkb->lkb_rqmode));
D
David Teigland 已提交
163 164
		if (lkb->lkb_wait_type)
			seq_printf(s, " wait_type: %d", lkb->lkb_wait_type);
165
		rv = seq_printf(s, "\n");
D
David Teigland 已提交
166 167
	}
 out:
D
David Teigland 已提交
168
	unlock_rsb(res);
169
	return rv;
D
David Teigland 已提交
170 171
}

172 173
static int print_format2_lock(struct seq_file *s, struct dlm_lkb *lkb,
			      struct dlm_rsb *r)
D
David Teigland 已提交
174
{
D
David Teigland 已提交
175 176
	u64 xid = 0;
	u64 us;
177
	int rv;
D
David Teigland 已提交
178 179

	if (lkb->lkb_flags & DLM_IFL_USER) {
180 181
		if (lkb->lkb_ua)
			xid = lkb->lkb_ua->xid;
D
David Teigland 已提交
182 183
	}

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

D
David Teigland 已提交
187
	/* id nodeid remid pid xid exflags flags sts grmode rqmode time_us
D
David Teigland 已提交
188
	   r_nodeid r_len r_name */
D
David Teigland 已提交
189

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
	rv = 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);
	return rv;
D
David Teigland 已提交
206 207
}

D
David Teigland 已提交
208
static int print_format2(struct dlm_rsb *r, struct seq_file *s)
D
David Teigland 已提交
209 210
{
	struct dlm_lkb *lkb;
211
	int rv = 0;
D
David Teigland 已提交
212 213 214

	lock_rsb(r);

215 216 217 218 219
	list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
		rv = print_format2_lock(s, lkb, r);
		if (rv)
			goto out;
	}
D
David Teigland 已提交
220

221 222 223 224 225
	list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
		rv = print_format2_lock(s, lkb, r);
		if (rv)
			goto out;
	}
D
David Teigland 已提交
226

227 228 229 230 231 232
	list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue) {
		rv = print_format2_lock(s, lkb, r);
		if (rv)
			goto out;
	}
 out:
D
David Teigland 已提交
233
	unlock_rsb(r);
234
	return rv;
D
David Teigland 已提交
235 236
}

237 238
static int print_format3_lock(struct seq_file *s, struct dlm_lkb *lkb,
			      int rsb_lookup)
D
David Teigland 已提交
239 240
{
	u64 xid = 0;
241
	int rv;
D
David Teigland 已提交
242 243 244 245 246 247

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

248 249 250 251 252 253 254 255 256 257 258
	rv = 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,
259
			lkb->lkb_bastmode,
260 261 262 263 264 265
			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_time_bast));
	return rv;
D
David Teigland 已提交
266 267 268 269 270 271 272
}

static int print_format3(struct dlm_rsb *r, struct seq_file *s)
{
	struct dlm_lkb *lkb;
	int i, lvblen = r->res_ls->ls_lvblen;
	int print_name = 1;
273
	int rv;
D
David Teigland 已提交
274 275 276

	lock_rsb(r);

277 278 279 280 281 282 283 284 285 286 287
	rv = 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 (rv)
		goto out;
D
David Teigland 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301

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

	seq_printf(s, "%s", print_name ? "str " : "hex");

	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]);
	}
302 303 304
	rv = seq_printf(s, "\n");
	if (rv)
		goto out;
D
David Teigland 已提交
305 306 307 308 309 310 311 312

	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]);
313 314 315
	rv = seq_printf(s, "\n");
	if (rv)
		goto out;
D
David Teigland 已提交
316 317

 do_locks:
318 319 320 321
	list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
		rv = print_format3_lock(s, lkb, 0);
		if (rv)
			goto out;
322 323
	}

324 325 326 327
	list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
		rv = print_format3_lock(s, lkb, 0);
		if (rv)
			goto out;
328 329
	}

330 331 332 333
	list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue) {
		rv = print_format3_lock(s, lkb, 0);
		if (rv)
			goto out;
334 335
	}

336 337 338 339
	list_for_each_entry(lkb, &r->res_lookup, lkb_rsb_lookup) {
		rv = print_format3_lock(s, lkb, 1);
		if (rv)
			goto out;
340
	}
341 342 343
 out:
	unlock_rsb(r);
	return rv;
344 345
}

346 347 348 349 350 351
struct rsbtbl_iter {
	struct dlm_rsb *rsb;
	unsigned bucket;
	int format;
	int header;
};
352

353 354 355 356 357 358 359
/* seq_printf returns -1 if the buffer is full, and 0 otherwise.
   If the buffer is full, seq_printf can be called again, but it
   does nothing and just returns -1.  So, the these printing routines
   periodically check the return value to avoid wasting too much time
   trying to print to a full buffer. */

static int table_seq_show(struct seq_file *seq, void *iter_ptr)
360
{
361 362
	struct rsbtbl_iter *ri = iter_ptr;
	int rv = 0;
363

D
David Teigland 已提交
364 365
	switch (ri->format) {
	case 1:
366
		rv = print_format1(ri->rsb, seq);
D
David Teigland 已提交
367 368
		break;
	case 2:
D
David Teigland 已提交
369
		if (ri->header) {
370 371 372
			seq_printf(seq, "id nodeid remid pid xid exflags "
					"flags sts grmode rqmode time_ms "
					"r_nodeid r_len r_name\n");
D
David Teigland 已提交
373 374
			ri->header = 0;
		}
375
		rv = print_format2(ri->rsb, seq);
D
David Teigland 已提交
376 377 378
		break;
	case 3:
		if (ri->header) {
379
			seq_printf(seq, "version rsb 1.1 lvb 1.1 lkb 1.1\n");
D
David Teigland 已提交
380 381
			ri->header = 0;
		}
382
		rv = print_format3(ri->rsb, seq);
D
David Teigland 已提交
383
		break;
D
David Teigland 已提交
384
	}
385

386
	return rv;
387 388
}

J
James Morris 已提交
389 390 391
static const struct seq_operations format1_seq_ops;
static const struct seq_operations format2_seq_ops;
static const struct seq_operations format3_seq_ops;
392

393
static void *table_seq_start(struct seq_file *seq, loff_t *pos)
394
{
395 396 397 398 399
	struct dlm_ls *ls = seq->private;
	struct rsbtbl_iter *ri;
	struct dlm_rsb *r;
	loff_t n = *pos;
	unsigned bucket, entry;
400

401 402
	bucket = n >> 32;
	entry = n & ((1LL << 32) - 1);
D
David Teigland 已提交
403

404 405
	if (bucket >= ls->ls_rsbtbl_size)
		return NULL;
D
David Teigland 已提交
406

D
David Teigland 已提交
407
	ri = kzalloc(sizeof(struct rsbtbl_iter), GFP_NOFS);
D
David Teigland 已提交
408 409
	if (!ri)
		return NULL;
410
	if (n == 0)
D
David Teigland 已提交
411
		ri->header = 1;
412 413 414 415 416 417 418
	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;

419
	spin_lock(&ls->ls_rsbtbl[bucket].lock);
420 421 422 423 424 425 426
	if (!list_empty(&ls->ls_rsbtbl[bucket].list)) {
		list_for_each_entry(r, &ls->ls_rsbtbl[bucket].list,
				    res_hashchain) {
			if (!entry--) {
				dlm_hold_rsb(r);
				ri->rsb = r;
				ri->bucket = bucket;
427
				spin_unlock(&ls->ls_rsbtbl[bucket].lock);
428 429 430
				return ri;
			}
		}
D
David Teigland 已提交
431
	}
432
	spin_unlock(&ls->ls_rsbtbl[bucket].lock);
D
David Teigland 已提交
433

434 435 436
	/*
	 * move to the first rsb in the next non-empty bucket
	 */
D
David Teigland 已提交
437

438 439
	/* zero the entry */
	n &= ~((1LL << 32) - 1);
D
David Teigland 已提交
440

441 442 443
	while (1) {
		bucket++;
		n += 1LL << 32;
D
David Teigland 已提交
444

445 446
		if (bucket >= ls->ls_rsbtbl_size) {
			kfree(ri);
D
David Teigland 已提交
447 448 449
			return NULL;
		}

450
		spin_lock(&ls->ls_rsbtbl[bucket].lock);
451 452 453 454 455 456
		if (!list_empty(&ls->ls_rsbtbl[bucket].list)) {
			r = list_first_entry(&ls->ls_rsbtbl[bucket].list,
					     struct dlm_rsb, res_hashchain);
			dlm_hold_rsb(r);
			ri->rsb = r;
			ri->bucket = bucket;
457
			spin_unlock(&ls->ls_rsbtbl[bucket].lock);
458 459 460
			*pos = n;
			return ri;
		}
461
		spin_unlock(&ls->ls_rsbtbl[bucket].lock);
462
	}
D
David Teigland 已提交
463 464
}

465
static void *table_seq_next(struct seq_file *seq, void *iter_ptr, loff_t *pos)
D
David Teigland 已提交
466
{
467 468 469 470 471 472 473 474 475 476 477 478 479
	struct dlm_ls *ls = seq->private;
	struct rsbtbl_iter *ri = iter_ptr;
	struct list_head *next;
	struct dlm_rsb *r, *rp;
	loff_t n = *pos;
	unsigned bucket;

	bucket = n >> 32;

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

480
	spin_lock(&ls->ls_rsbtbl[bucket].lock);
481 482 483 484 485 486 487
	rp = ri->rsb;
	next = rp->res_hashchain.next;

	if (next != &ls->ls_rsbtbl[bucket].list) {
		r = list_entry(next, struct dlm_rsb, res_hashchain);
		dlm_hold_rsb(r);
		ri->rsb = r;
488
		spin_unlock(&ls->ls_rsbtbl[bucket].lock);
489 490 491 492
		dlm_put_rsb(rp);
		++*pos;
		return ri;
	}
493
	spin_unlock(&ls->ls_rsbtbl[bucket].lock);
494
	dlm_put_rsb(rp);
D
David Teigland 已提交
495

496 497 498
	/*
	 * move to the first rsb in the next non-empty bucket
	 */
D
David Teigland 已提交
499

500 501
	/* zero the entry */
	n &= ~((1LL << 32) - 1);
D
David Teigland 已提交
502

503 504 505
	while (1) {
		bucket++;
		n += 1LL << 32;
D
David Teigland 已提交
506

507 508 509 510
		if (bucket >= ls->ls_rsbtbl_size) {
			kfree(ri);
			return NULL;
		}
D
David Teigland 已提交
511

512
		spin_lock(&ls->ls_rsbtbl[bucket].lock);
513 514 515 516 517 518
		if (!list_empty(&ls->ls_rsbtbl[bucket].list)) {
			r = list_first_entry(&ls->ls_rsbtbl[bucket].list,
					     struct dlm_rsb, res_hashchain);
			dlm_hold_rsb(r);
			ri->rsb = r;
			ri->bucket = bucket;
519
			spin_unlock(&ls->ls_rsbtbl[bucket].lock);
520 521 522
			*pos = n;
			return ri;
		}
523
		spin_unlock(&ls->ls_rsbtbl[bucket].lock);
D
David Teigland 已提交
524 525 526
	}
}

527
static void table_seq_stop(struct seq_file *seq, void *iter_ptr)
D
David Teigland 已提交
528
{
529
	struct rsbtbl_iter *ri = iter_ptr;
D
David Teigland 已提交
530

531 532 533
	if (ri) {
		dlm_put_rsb(ri->rsb);
		kfree(ri);
D
David Teigland 已提交
534 535 536
	}
}

J
James Morris 已提交
537
static const struct seq_operations format1_seq_ops = {
538 539 540 541
	.start = table_seq_start,
	.next  = table_seq_next,
	.stop  = table_seq_stop,
	.show  = table_seq_show,
D
David Teigland 已提交
542 543
};

J
James Morris 已提交
544
static const struct seq_operations format2_seq_ops = {
545 546 547 548 549 550
	.start = table_seq_start,
	.next  = table_seq_next,
	.stop  = table_seq_stop,
	.show  = table_seq_show,
};

J
James Morris 已提交
551
static const struct seq_operations format3_seq_ops = {
552 553 554 555 556 557 558 559 560 561 562
	.start = table_seq_start,
	.next  = table_seq_next,
	.stop  = table_seq_stop,
	.show  = table_seq_show,
};

static const struct file_operations format1_fops;
static const struct file_operations format2_fops;
static const struct file_operations format3_fops;

static int table_open(struct inode *inode, struct file *file)
D
David Teigland 已提交
563 564
{
	struct seq_file *seq;
565 566 567 568 569 570 571 572
	int ret = -1;

	if (file->f_op == &format1_fops)
		ret = seq_open(file, &format1_seq_ops);
	else if (file->f_op == &format2_fops)
		ret = seq_open(file, &format2_seq_ops);
	else if (file->f_op == &format3_fops)
		ret = seq_open(file, &format3_seq_ops);
D
David Teigland 已提交
573 574 575 576 577

	if (ret)
		return ret;

	seq = file->private_data;
578
	seq->private = inode->i_private; /* the dlm_ls */
D
David Teigland 已提交
579 580 581
	return 0;
}

582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
static const struct file_operations format1_fops = {
	.owner   = THIS_MODULE,
	.open    = table_open,
	.read    = seq_read,
	.llseek  = seq_lseek,
	.release = seq_release
};

static const struct file_operations format2_fops = {
	.owner   = THIS_MODULE,
	.open    = table_open,
	.read    = seq_read,
	.llseek  = seq_lseek,
	.release = seq_release
};

static const struct file_operations format3_fops = {
D
David Teigland 已提交
599
	.owner   = THIS_MODULE,
600
	.open    = table_open,
D
David Teigland 已提交
601 602 603 604 605
	.read    = seq_read,
	.llseek  = seq_lseek,
	.release = seq_release
};

D
David Teigland 已提交
606 607 608 609 610 611
/*
 * dump lkb's on the ls_waiters list
 */

static int waiters_open(struct inode *inode, struct file *file)
{
612
	file->private_data = inode->i_private;
D
David Teigland 已提交
613 614 615 616 617 618 619 620
	return 0;
}

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 已提交
621
	size_t len = DLM_DEBUG_BUF_LEN, pos = 0, ret, rv;
D
David Teigland 已提交
622 623 624 625 626 627

	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 已提交
628 629 630 631 632 633
		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 已提交
634 635 636 637 638 639 640 641
	}
	mutex_unlock(&ls->ls_waiters_mutex);

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

642
static const struct file_operations waiters_fops = {
D
David Teigland 已提交
643 644 645 646 647
	.owner   = THIS_MODULE,
	.open    = waiters_open,
	.read    = waiters_read
};

D
David Teigland 已提交
648 649 650 651 652 653 654 655 656 657 658 659
void dlm_delete_debug_file(struct dlm_ls *ls)
{
	if (ls->ls_debug_rsb_dentry)
		debugfs_remove(ls->ls_debug_rsb_dentry);
	if (ls->ls_debug_waiters_dentry)
		debugfs_remove(ls->ls_debug_waiters_dentry);
	if (ls->ls_debug_locks_dentry)
		debugfs_remove(ls->ls_debug_locks_dentry);
	if (ls->ls_debug_all_dentry)
		debugfs_remove(ls->ls_debug_all_dentry);
}

660 661
int dlm_create_debug_file(struct dlm_ls *ls)
{
D
David Teigland 已提交
662 663
	char name[DLM_LOCKSPACE_LEN+8];

D
David Teigland 已提交
664 665
	/* format 1 */

D
David Teigland 已提交
666 667 668 669
	ls->ls_debug_rsb_dentry = debugfs_create_file(ls->ls_name,
						      S_IFREG | S_IRUGO,
						      dlm_root,
						      ls,
670
						      &format1_fops);
D
David Teigland 已提交
671
	if (!ls->ls_debug_rsb_dentry)
D
David Teigland 已提交
672
		goto fail;
D
David Teigland 已提交
673

D
David Teigland 已提交
674
	/* format 2 */
D
David Teigland 已提交
675

D
David Teigland 已提交
676
	memset(name, 0, sizeof(name));
D
David Teigland 已提交
677 678 679 680 681 682
	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,
683
							&format2_fops);
D
David Teigland 已提交
684 685 686 687 688 689 690 691 692 693 694 695
	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,
696
						      &format3_fops);
D
David Teigland 已提交
697 698 699 700 701 702 703 704 705 706 707 708 709
	if (!ls->ls_debug_all_dentry)
		goto fail;

	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 已提交
710

D
David Teigland 已提交
711
	return 0;
712

D
David Teigland 已提交
713 714 715
 fail:
	dlm_delete_debug_file(ls);
	return -ENOMEM;
716 717
}

718
int __init dlm_register_debugfs(void)
719
{
D
David Teigland 已提交
720
	mutex_init(&debug_buf_lock);
721 722 723 724 725 726 727 728 729
	dlm_root = debugfs_create_dir("dlm", NULL);
	return dlm_root ? 0 : -ENOMEM;
}

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