debug_fs.c 11.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/******************************************************************************
*******************************************************************************
**
**  Copyright (C) 2005 Red Hat, Inc.  All rights reserved.
**
**  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

static struct dentry *dlm_root;

struct rsb_iter {
	int entry;
D
David Teigland 已提交
30
	int locks;
D
David Teigland 已提交
31
	int header;
32 33 34 35 36
	struct dlm_ls *ls;
	struct list_head *next;
	struct dlm_rsb *rsb;
};

D
David Teigland 已提交
37 38 39 40
/*
 * dump all rsb's in the lockspace hash table
 */

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
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 "??";
	}
}

D
David Teigland 已提交
63 64
static void print_resource_lock(struct seq_file *s, struct dlm_lkb *lkb,
				struct dlm_rsb *res)
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
{
	seq_printf(s, "%08x %s", lkb->lkb_id, print_lockmode(lkb->lkb_grmode));

	if (lkb->lkb_status == DLM_LKSTS_CONVERT
	    || lkb->lkb_status == DLM_LKSTS_WAITING)
		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);

	seq_printf(s, "\n");
}

static int print_resource(struct dlm_rsb *res, struct seq_file *s)
{
	struct dlm_lkb *lkb;
D
David Teigland 已提交
89
	int i, lvblen = res->res_ls->ls_lvblen, recover_list, root_list;
90

D
David Teigland 已提交
91 92
	lock_rsb(res);

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	seq_printf(s, "\nResource %p Name (len=%d) \"", res, res->res_length);
	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", '.');
	}
	if (res->res_nodeid > 0)
		seq_printf(s, "\"  \nLocal Copy, Master is node %d\n",
			   res->res_nodeid);
	else if (res->res_nodeid == 0)
		seq_printf(s, "\"  \nMaster Copy\n");
	else if (res->res_nodeid == -1)
		seq_printf(s, "\"  \nLooking up master (lkid %x)\n",
			   res->res_first_lkid);
	else
		seq_printf(s, "\"  \nInvalid master %d\n", res->res_nodeid);

	/* 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)");
		seq_printf(s, "\n");
	}

D
David Teigland 已提交
125 126 127 128 129 130
	root_list = !list_empty(&res->res_root_list);
	recover_list = !list_empty(&res->res_recover_list);

	if (root_list || recover_list) {
		seq_printf(s, "Recovery: root %d recover %d flags %lx "
			   "count %d\n", root_list, recover_list,
D
David Teigland 已提交
131
			   res->res_flags, res->res_recover_locks_count);
D
David Teigland 已提交
132 133
	}

134 135 136
	/* Print the locks attached to this resource */
	seq_printf(s, "Granted Queue\n");
	list_for_each_entry(lkb, &res->res_grantqueue, lkb_statequeue)
D
David Teigland 已提交
137
		print_resource_lock(s, lkb, res);
138 139 140

	seq_printf(s, "Conversion Queue\n");
	list_for_each_entry(lkb, &res->res_convertqueue, lkb_statequeue)
D
David Teigland 已提交
141
		print_resource_lock(s, lkb, res);
142 143 144

	seq_printf(s, "Waiting Queue\n");
	list_for_each_entry(lkb, &res->res_waitqueue, lkb_statequeue)
D
David Teigland 已提交
145
		print_resource_lock(s, lkb, res);
146

D
David Teigland 已提交
147 148 149 150 151 152 153 154 155 156 157 158
	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) {
		seq_printf(s, "%08x %s", lkb->lkb_id,
			   print_lockmode(lkb->lkb_rqmode));
		if (lkb->lkb_wait_type)
			seq_printf(s, " wait_type: %d", lkb->lkb_wait_type);
		seq_printf(s, "\n");
	}
 out:
D
David Teigland 已提交
159 160 161 162
	unlock_rsb(res);
	return 0;
}

D
David Teigland 已提交
163
static void print_lock(struct seq_file *s, struct dlm_lkb *lkb, struct dlm_rsb *r)
D
David Teigland 已提交
164 165 166 167 168
{
	unsigned int waiting = 0;
	uint64_t xid = 0;

	if (lkb->lkb_flags & DLM_IFL_USER) {
169 170
		if (lkb->lkb_ua)
			xid = lkb->lkb_ua->xid;
D
David Teigland 已提交
171 172 173 174 175
	}

	if (lkb->lkb_timestamp)
		waiting = jiffies_to_msecs(jiffies - lkb->lkb_timestamp);

D
David Teigland 已提交
176 177
	/* id nodeid remid pid xid exflags flags sts grmode rqmode time_ms
	   r_nodeid r_len r_name */
D
David Teigland 已提交
178

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

D
David Teigland 已提交
196
static int print_locks(struct dlm_rsb *r, struct seq_file *s)
D
David Teigland 已提交
197 198 199 200 201 202
{
	struct dlm_lkb *lkb;

	lock_rsb(r);

	list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue)
D
David Teigland 已提交
203
		print_lock(s, lkb, r);
D
David Teigland 已提交
204 205

	list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue)
D
David Teigland 已提交
206
		print_lock(s, lkb, r);
D
David Teigland 已提交
207 208

	list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue)
D
David Teigland 已提交
209
		print_lock(s, lkb, r);
D
David Teigland 已提交
210 211

	unlock_rsb(r);
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
	return 0;
}

static int rsb_iter_next(struct rsb_iter *ri)
{
	struct dlm_ls *ls = ri->ls;
	int i;

	if (!ri->next) {
 top:
		/* Find the next non-empty hash bucket */
		for (i = ri->entry; i < ls->ls_rsbtbl_size; i++) {
			read_lock(&ls->ls_rsbtbl[i].lock);
			if (!list_empty(&ls->ls_rsbtbl[i].list)) {
				ri->next = ls->ls_rsbtbl[i].list.next;
227 228 229
				ri->rsb = list_entry(ri->next, struct dlm_rsb,
							res_hashchain);
				dlm_hold_rsb(ri->rsb);
230 231 232 233 234 235 236 237 238 239
				read_unlock(&ls->ls_rsbtbl[i].lock);
				break;
			}
			read_unlock(&ls->ls_rsbtbl[i].lock);
                }
		ri->entry = i;

		if (ri->entry >= ls->ls_rsbtbl_size)
			return 1;
	} else {
240
		struct dlm_rsb *old = ri->rsb;
241 242 243 244 245 246 247 248
		i = ri->entry;
		read_lock(&ls->ls_rsbtbl[i].lock);
		ri->next = ri->next->next;
		if (ri->next->next == ls->ls_rsbtbl[i].list.next) {
			/* End of list - move to next bucket */
			ri->next = NULL;
			ri->entry++;
			read_unlock(&ls->ls_rsbtbl[i].lock);
249
			dlm_put_rsb(old);
250 251
			goto top;
                }
252
		ri->rsb = list_entry(ri->next, struct dlm_rsb, res_hashchain);
J
Josef Bacik 已提交
253
		dlm_hold_rsb(ri->rsb);
254
		read_unlock(&ls->ls_rsbtbl[i].lock);
255
		dlm_put_rsb(old);
256 257 258 259 260 261 262 263 264 265 266 267 268 269
	}

	return 0;
}

static void rsb_iter_free(struct rsb_iter *ri)
{
	kfree(ri);
}

static struct rsb_iter *rsb_iter_init(struct dlm_ls *ls)
{
	struct rsb_iter *ri;

D
David Teigland 已提交
270
	ri = kzalloc(sizeof *ri, GFP_KERNEL);
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
	if (!ri)
		return NULL;

	ri->ls = ls;
	ri->entry = 0;
	ri->next = NULL;

	if (rsb_iter_next(ri)) {
		rsb_iter_free(ri);
		return NULL;
	}

	return ri;
}

D
David Teigland 已提交
286
static void *rsb_seq_start(struct seq_file *file, loff_t *pos)
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
{
	struct rsb_iter *ri;
	loff_t n = *pos;

	ri = rsb_iter_init(file->private);
	if (!ri)
		return NULL;

	while (n--) {
		if (rsb_iter_next(ri)) {
			rsb_iter_free(ri);
			return NULL;
		}
	}

	return ri;
}

D
David Teigland 已提交
305
static void *rsb_seq_next(struct seq_file *file, void *iter_ptr, loff_t *pos)
306 307 308 309 310 311 312 313 314 315 316 317 318
{
	struct rsb_iter *ri = iter_ptr;

	(*pos)++;

	if (rsb_iter_next(ri)) {
		rsb_iter_free(ri);
		return NULL;
	}

	return ri;
}

D
David Teigland 已提交
319
static void rsb_seq_stop(struct seq_file *file, void *iter_ptr)
320 321 322 323
{
	/* nothing for now */
}

D
David Teigland 已提交
324
static int rsb_seq_show(struct seq_file *file, void *iter_ptr)
325 326 327
{
	struct rsb_iter *ri = iter_ptr;

D
David Teigland 已提交
328
	if (ri->locks) {
D
David Teigland 已提交
329
		if (ri->header) {
D
David Teigland 已提交
330 331 332
			seq_printf(file, "id nodeid remid pid xid exflags flags "
					 "sts grmode rqmode time_ms r_nodeid "
					 "r_len r_name\n");
D
David Teigland 已提交
333 334
			ri->header = 0;
		}
D
David Teigland 已提交
335
		print_locks(ri->rsb, file);
D
David Teigland 已提交
336 337 338
	} else {
		print_resource(ri->rsb, file);
	}
339 340 341 342

	return 0;
}

D
David Teigland 已提交
343 344 345 346 347
static struct seq_operations rsb_seq_ops = {
	.start = rsb_seq_start,
	.next  = rsb_seq_next,
	.stop  = rsb_seq_stop,
	.show  = rsb_seq_show,
348 349
};

D
David Teigland 已提交
350
static int rsb_open(struct inode *inode, struct file *file)
351 352 353 354
{
	struct seq_file *seq;
	int ret;

D
David Teigland 已提交
355
	ret = seq_open(file, &rsb_seq_ops);
356 357 358 359
	if (ret)
		return ret;

	seq = file->private_data;
360
	seq->private = inode->i_private;
361 362 363 364

	return 0;
}

365
static const struct file_operations rsb_fops = {
366
	.owner   = THIS_MODULE,
D
David Teigland 已提交
367
	.open    = rsb_open,
368 369 370 371 372
	.read    = seq_read,
	.llseek  = seq_lseek,
	.release = seq_release
};

D
David Teigland 已提交
373
/*
D
David Teigland 已提交
374
 * Dump state in compact per-lock listing
D
David Teigland 已提交
375 376
 */

D
David Teigland 已提交
377
static struct rsb_iter *locks_iter_init(struct dlm_ls *ls, loff_t *pos)
D
David Teigland 已提交
378 379 380 381 382 383 384 385 386 387
{
	struct rsb_iter *ri;

	ri = kzalloc(sizeof *ri, GFP_KERNEL);
	if (!ri)
		return NULL;

	ri->ls = ls;
	ri->entry = 0;
	ri->next = NULL;
D
David Teigland 已提交
388
	ri->locks = 1;
D
David Teigland 已提交
389 390 391 392 393 394 395 396 397 398 399 400

	if (*pos == 0)
		ri->header = 1;

	if (rsb_iter_next(ri)) {
		rsb_iter_free(ri);
		return NULL;
	}

	return ri;
}

D
David Teigland 已提交
401
static void *locks_seq_start(struct seq_file *file, loff_t *pos)
D
David Teigland 已提交
402 403 404 405
{
	struct rsb_iter *ri;
	loff_t n = *pos;

D
David Teigland 已提交
406
	ri = locks_iter_init(file->private, pos);
D
David Teigland 已提交
407 408 409 410 411 412 413 414 415 416 417 418 419
	if (!ri)
		return NULL;

	while (n--) {
		if (rsb_iter_next(ri)) {
			rsb_iter_free(ri);
			return NULL;
		}
	}

	return ri;
}

D
David Teigland 已提交
420 421
static struct seq_operations locks_seq_ops = {
	.start = locks_seq_start,
D
David Teigland 已提交
422 423 424 425 426
	.next  = rsb_seq_next,
	.stop  = rsb_seq_stop,
	.show  = rsb_seq_show,
};

D
David Teigland 已提交
427
static int locks_open(struct inode *inode, struct file *file)
D
David Teigland 已提交
428 429 430 431
{
	struct seq_file *seq;
	int ret;

D
David Teigland 已提交
432
	ret = seq_open(file, &locks_seq_ops);
D
David Teigland 已提交
433 434 435 436 437 438 439 440 441
	if (ret)
		return ret;

	seq = file->private_data;
	seq->private = inode->i_private;

	return 0;
}

D
David Teigland 已提交
442
static const struct file_operations locks_fops = {
D
David Teigland 已提交
443
	.owner   = THIS_MODULE,
D
David Teigland 已提交
444
	.open    = locks_open,
D
David Teigland 已提交
445 446 447 448 449
	.read    = seq_read,
	.llseek  = seq_lseek,
	.release = seq_release
};

D
David Teigland 已提交
450 451 452 453 454 455
/*
 * dump lkb's on the ls_waiters list
 */

static int waiters_open(struct inode *inode, struct file *file)
{
456
	file->private_data = inode->i_private;
D
David Teigland 已提交
457 458 459 460 461 462 463 464
	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 已提交
465
	size_t len = DLM_DEBUG_BUF_LEN, pos = 0, ret, rv;
D
David Teigland 已提交
466 467 468 469 470 471

	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 已提交
472 473 474 475 476 477
		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 已提交
478 479 480 481 482 483 484 485
	}
	mutex_unlock(&ls->ls_waiters_mutex);

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

486
static const struct file_operations waiters_fops = {
D
David Teigland 已提交
487 488 489 490 491
	.owner   = THIS_MODULE,
	.open    = waiters_open,
	.read    = waiters_read
};

492 493
int dlm_create_debug_file(struct dlm_ls *ls)
{
D
David Teigland 已提交
494 495 496 497 498 499 500
	char name[DLM_LOCKSPACE_LEN+8];

	ls->ls_debug_rsb_dentry = debugfs_create_file(ls->ls_name,
						      S_IFREG | S_IRUGO,
						      dlm_root,
						      ls,
						      &rsb_fops);
D
David Teigland 已提交
501
	if (!ls->ls_debug_rsb_dentry)
D
David Teigland 已提交
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
		return -ENOMEM;

	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) {
		debugfs_remove(ls->ls_debug_rsb_dentry);
		return -ENOMEM;
	}

D
David Teigland 已提交
517
	memset(name, 0, sizeof(name));
D
David Teigland 已提交
518 519 520 521 522 523 524 525
	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,
							&locks_fops);
	if (!ls->ls_debug_locks_dentry) {
D
David Teigland 已提交
526 527 528 529 530
		debugfs_remove(ls->ls_debug_waiters_dentry);
		debugfs_remove(ls->ls_debug_rsb_dentry);
		return -ENOMEM;
	}

D
David Teigland 已提交
531
	return 0;
532 533 534 535
}

void dlm_delete_debug_file(struct dlm_ls *ls)
{
D
David Teigland 已提交
536 537 538 539
	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);
D
David Teigland 已提交
540 541
	if (ls->ls_debug_locks_dentry)
		debugfs_remove(ls->ls_debug_locks_dentry);
542 543
}

544
int __init dlm_register_debugfs(void)
545
{
D
David Teigland 已提交
546
	mutex_init(&debug_buf_lock);
547 548 549 550 551 552 553 554 555
	dlm_root = debugfs_create_dir("dlm", NULL);
	return dlm_root ? 0 : -ENOMEM;
}

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