xref: /linux/fs/ocfs2/dlm/dlmdebug.c (revision 1dbb7422053fa0f4fb5f9acc3931617fc0091939)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * dlmdebug.c
4  *
5  * debug functionality for the dlm
6  *
7  * Copyright (C) 2004, 2008 Oracle.  All rights reserved.
8  */
9 
10 #include <linux/types.h>
11 #include <linux/slab.h>
12 #include <linux/highmem.h>
13 #include <linux/sysctl.h>
14 #include <linux/spinlock.h>
15 #include <linux/debugfs.h>
16 #include <linux/export.h>
17 #include <linux/string_choices.h>
18 
19 #include "../cluster/heartbeat.h"
20 #include "../cluster/nodemanager.h"
21 #include "../cluster/tcp.h"
22 
23 #include "dlmapi.h"
24 #include "dlmcommon.h"
25 #include "dlmdomain.h"
26 #include "dlmdebug.h"
27 
28 #define MLOG_MASK_PREFIX ML_DLM
29 #include "../cluster/masklog.h"
30 
31 static int stringify_lockname(const char *lockname, int locklen, char *buf,
32 			      int len);
33 
dlm_print_one_lock_resource(struct dlm_lock_resource * res)34 void dlm_print_one_lock_resource(struct dlm_lock_resource *res)
35 {
36 	spin_lock(&res->spinlock);
37 	__dlm_print_one_lock_resource(res);
38 	spin_unlock(&res->spinlock);
39 }
40 
dlm_print_lockres_refmap(struct dlm_lock_resource * res)41 static void dlm_print_lockres_refmap(struct dlm_lock_resource *res)
42 {
43 	int bit;
44 	assert_spin_locked(&res->spinlock);
45 
46 	printk("  refmap nodes: [ ");
47 	bit = 0;
48 	while (1) {
49 		bit = find_next_bit(res->refmap, O2NM_MAX_NODES, bit);
50 		if (bit >= O2NM_MAX_NODES)
51 			break;
52 		printk("%u ", bit);
53 		bit++;
54 	}
55 	printk("], inflight=%u\n", res->inflight_locks);
56 }
57 
__dlm_print_lock(struct dlm_lock * lock)58 static void __dlm_print_lock(struct dlm_lock *lock)
59 {
60 	spin_lock(&lock->spinlock);
61 
62 	printk("    type=%d, conv=%d, node=%u, cookie=%u:%llu, "
63 	       "ref=%u, ast=(empty=%c,pend=%c), bast=(empty=%c,pend=%c), "
64 	       "pending=(conv=%c,lock=%c,cancel=%c,unlock=%c)\n",
65 	       lock->ml.type, lock->ml.convert_type, lock->ml.node,
66 	       dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
67 	       dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
68 	       kref_read(&lock->lock_refs),
69 	       (list_empty(&lock->ast_list) ? 'y' : 'n'),
70 	       (lock->ast_pending ? 'y' : 'n'),
71 	       (list_empty(&lock->bast_list) ? 'y' : 'n'),
72 	       (lock->bast_pending ? 'y' : 'n'),
73 	       (lock->convert_pending ? 'y' : 'n'),
74 	       (lock->lock_pending ? 'y' : 'n'),
75 	       (lock->cancel_pending ? 'y' : 'n'),
76 	       (lock->unlock_pending ? 'y' : 'n'));
77 
78 	spin_unlock(&lock->spinlock);
79 }
80 
__dlm_print_one_lock_resource(struct dlm_lock_resource * res)81 void __dlm_print_one_lock_resource(struct dlm_lock_resource *res)
82 {
83 	struct dlm_lock *lock;
84 	char buf[DLM_LOCKID_NAME_MAX];
85 
86 	assert_spin_locked(&res->spinlock);
87 
88 	stringify_lockname(res->lockname.name, res->lockname.len,
89 			   buf, sizeof(buf));
90 	printk("lockres: %s, owner=%u, state=%u\n",
91 	       buf, res->owner, res->state);
92 	printk("  last used: %lu, refcnt: %u, on purge list: %s\n",
93 	       res->last_used, kref_read(&res->refs),
94 	       str_no_yes(list_empty(&res->purge)));
95 	printk("  on dirty list: %s, on reco list: %s, "
96 	       "migrating pending: %s\n",
97 	       str_no_yes(list_empty(&res->dirty)),
98 	       str_no_yes(list_empty(&res->recovering)),
99 	       str_yes_no(res->migration_pending));
100 	printk("  inflight locks: %d, asts reserved: %d\n",
101 	       res->inflight_locks, atomic_read(&res->asts_reserved));
102 	dlm_print_lockres_refmap(res);
103 	printk("  granted queue:\n");
104 	list_for_each_entry(lock, &res->granted, list) {
105 		__dlm_print_lock(lock);
106 	}
107 	printk("  converting queue:\n");
108 	list_for_each_entry(lock, &res->converting, list) {
109 		__dlm_print_lock(lock);
110 	}
111 	printk("  blocked queue:\n");
112 	list_for_each_entry(lock, &res->blocked, list) {
113 		__dlm_print_lock(lock);
114 	}
115 }
116 
dlm_print_one_lock(struct dlm_lock * lockid)117 void dlm_print_one_lock(struct dlm_lock *lockid)
118 {
119 	dlm_print_one_lock_resource(lockid->lockres);
120 }
121 EXPORT_SYMBOL_GPL(dlm_print_one_lock);
122 
123 static const char *dlm_errnames[] = {
124 	[DLM_NORMAL] =			"DLM_NORMAL",
125 	[DLM_GRANTED] =			"DLM_GRANTED",
126 	[DLM_DENIED] =			"DLM_DENIED",
127 	[DLM_DENIED_NOLOCKS] =		"DLM_DENIED_NOLOCKS",
128 	[DLM_WORKING] =			"DLM_WORKING",
129 	[DLM_BLOCKED] =			"DLM_BLOCKED",
130 	[DLM_BLOCKED_ORPHAN] =		"DLM_BLOCKED_ORPHAN",
131 	[DLM_DENIED_GRACE_PERIOD] =	"DLM_DENIED_GRACE_PERIOD",
132 	[DLM_SYSERR] =			"DLM_SYSERR",
133 	[DLM_NOSUPPORT] =		"DLM_NOSUPPORT",
134 	[DLM_CANCELGRANT] =		"DLM_CANCELGRANT",
135 	[DLM_IVLOCKID] =		"DLM_IVLOCKID",
136 	[DLM_SYNC] =			"DLM_SYNC",
137 	[DLM_BADTYPE] =			"DLM_BADTYPE",
138 	[DLM_BADRESOURCE] =		"DLM_BADRESOURCE",
139 	[DLM_MAXHANDLES] =		"DLM_MAXHANDLES",
140 	[DLM_NOCLINFO] =		"DLM_NOCLINFO",
141 	[DLM_NOLOCKMGR] =		"DLM_NOLOCKMGR",
142 	[DLM_NOPURGED] =		"DLM_NOPURGED",
143 	[DLM_BADARGS] =			"DLM_BADARGS",
144 	[DLM_VOID] =			"DLM_VOID",
145 	[DLM_NOTQUEUED] =		"DLM_NOTQUEUED",
146 	[DLM_IVBUFLEN] =		"DLM_IVBUFLEN",
147 	[DLM_CVTUNGRANT] =		"DLM_CVTUNGRANT",
148 	[DLM_BADPARAM] =		"DLM_BADPARAM",
149 	[DLM_VALNOTVALID] =		"DLM_VALNOTVALID",
150 	[DLM_REJECTED] =		"DLM_REJECTED",
151 	[DLM_ABORT] =			"DLM_ABORT",
152 	[DLM_CANCEL] =			"DLM_CANCEL",
153 	[DLM_IVRESHANDLE] =		"DLM_IVRESHANDLE",
154 	[DLM_DEADLOCK] =		"DLM_DEADLOCK",
155 	[DLM_DENIED_NOASTS] =		"DLM_DENIED_NOASTS",
156 	[DLM_FORWARD] =			"DLM_FORWARD",
157 	[DLM_TIMEOUT] =			"DLM_TIMEOUT",
158 	[DLM_IVGROUPID] =		"DLM_IVGROUPID",
159 	[DLM_VERS_CONFLICT] =		"DLM_VERS_CONFLICT",
160 	[DLM_BAD_DEVICE_PATH] =		"DLM_BAD_DEVICE_PATH",
161 	[DLM_NO_DEVICE_PERMISSION] =	"DLM_NO_DEVICE_PERMISSION",
162 	[DLM_NO_CONTROL_DEVICE ] =	"DLM_NO_CONTROL_DEVICE ",
163 	[DLM_RECOVERING] =		"DLM_RECOVERING",
164 	[DLM_MIGRATING] =		"DLM_MIGRATING",
165 	[DLM_MAXSTATS] =		"DLM_MAXSTATS",
166 };
167 
dlm_errname(enum dlm_status err)168 const char *dlm_errname(enum dlm_status err)
169 {
170 	if (err >= DLM_MAXSTATS || err < 0)
171 		return dlm_errnames[DLM_MAXSTATS];
172 	return dlm_errnames[err];
173 }
174 EXPORT_SYMBOL_GPL(dlm_errname);
175 
176 /* NOTE: This function converts a lockname into a string. It uses knowledge
177  * of the format of the lockname that should be outside the purview of the dlm.
178  * We are adding only to make dlm debugging slightly easier.
179  *
180  * For more on lockname formats, please refer to dlmglue.c and ocfs2_lockid.h.
181  */
stringify_lockname(const char * lockname,int locklen,char * buf,int len)182 static int stringify_lockname(const char *lockname, int locklen, char *buf,
183 			      int len)
184 {
185 	int out = 0;
186 	__be64 inode_blkno_be;
187 
188 #define OCFS2_DENTRY_LOCK_INO_START	18
189 	if (*lockname == 'N') {
190 		memcpy((__be64 *)&inode_blkno_be,
191 		       (char *)&lockname[OCFS2_DENTRY_LOCK_INO_START],
192 		       sizeof(__be64));
193 		out += scnprintf(buf + out, len - out, "%.*s%08x",
194 				OCFS2_DENTRY_LOCK_INO_START - 1, lockname,
195 				(unsigned int)be64_to_cpu(inode_blkno_be));
196 	} else
197 		out += scnprintf(buf + out, len - out, "%.*s",
198 				locklen, lockname);
199 	return out;
200 }
201 
stringify_nodemap(unsigned long * nodemap,int maxnodes,char * buf,int len)202 static int stringify_nodemap(unsigned long *nodemap, int maxnodes,
203 			     char *buf, int len)
204 {
205 	int out = 0;
206 	int i = -1;
207 
208 	while ((i = find_next_bit(nodemap, maxnodes, i + 1)) < maxnodes)
209 		out += scnprintf(buf + out, len - out, "%d ", i);
210 
211 	return out;
212 }
213 
dump_mle(struct dlm_master_list_entry * mle,char * buf,int len)214 static int dump_mle(struct dlm_master_list_entry *mle, char *buf, int len)
215 {
216 	int out = 0;
217 	char *mle_type;
218 
219 	if (mle->type == DLM_MLE_BLOCK)
220 		mle_type = "BLK";
221 	else if (mle->type == DLM_MLE_MASTER)
222 		mle_type = "MAS";
223 	else
224 		mle_type = "MIG";
225 
226 	out += stringify_lockname(mle->mname, mle->mnamelen, buf + out, len - out);
227 	out += scnprintf(buf + out, len - out,
228 			"\t%3s\tmas=%3u\tnew=%3u\tevt=%1d\tuse=%1d\tref=%3d\n",
229 			mle_type, mle->master, mle->new_master,
230 			!list_empty(&mle->hb_events),
231 			!!mle->inuse,
232 			kref_read(&mle->mle_refs));
233 
234 	out += scnprintf(buf + out, len - out, "Maybe=");
235 	out += stringify_nodemap(mle->maybe_map, O2NM_MAX_NODES,
236 				 buf + out, len - out);
237 	out += scnprintf(buf + out, len - out, "\n");
238 
239 	out += scnprintf(buf + out, len - out, "Vote=");
240 	out += stringify_nodemap(mle->vote_map, O2NM_MAX_NODES,
241 				 buf + out, len - out);
242 	out += scnprintf(buf + out, len - out, "\n");
243 
244 	out += scnprintf(buf + out, len - out, "Response=");
245 	out += stringify_nodemap(mle->response_map, O2NM_MAX_NODES,
246 				 buf + out, len - out);
247 	out += scnprintf(buf + out, len - out, "\n");
248 
249 	out += scnprintf(buf + out, len - out, "Node=");
250 	out += stringify_nodemap(mle->node_map, O2NM_MAX_NODES,
251 				 buf + out, len - out);
252 	out += scnprintf(buf + out, len - out, "\n");
253 
254 	out += scnprintf(buf + out, len - out, "\n");
255 
256 	return out;
257 }
258 
dlm_print_one_mle(struct dlm_master_list_entry * mle)259 void dlm_print_one_mle(struct dlm_master_list_entry *mle)
260 {
261 	char *buf;
262 
263 	buf = kzalloc(PAGE_SIZE, GFP_ATOMIC);
264 	if (buf) {
265 		dump_mle(mle, buf, PAGE_SIZE - 1);
266 		kfree(buf);
267 	}
268 }
269 
270 #ifdef CONFIG_DEBUG_FS
271 
272 static struct dentry *dlm_debugfs_root;
273 
274 #define DLM_DEBUGFS_DIR				"o2dlm"
275 #define DLM_DEBUGFS_DLM_STATE			"dlm_state"
276 #define DLM_DEBUGFS_LOCKING_STATE		"locking_state"
277 #define DLM_DEBUGFS_MLE_STATE			"mle_state"
278 #define DLM_DEBUGFS_PURGE_LIST			"purge_list"
279 
280 /* begin - utils funcs */
debug_release(struct inode * inode,struct file * file)281 static int debug_release(struct inode *inode, struct file *file)
282 {
283 	kfree(file->private_data);
284 	return 0;
285 }
286 
debug_read(struct file * file,char __user * buf,size_t nbytes,loff_t * ppos)287 static ssize_t debug_read(struct file *file, char __user *buf,
288 			  size_t nbytes, loff_t *ppos)
289 {
290 	return simple_read_from_buffer(buf, nbytes, ppos, file->private_data,
291 				       i_size_read(file->f_mapping->host));
292 }
293 /* end - util funcs */
294 
295 /* begin - purge list funcs */
debug_purgelist_print(struct dlm_ctxt * dlm,char * buf,int len)296 static int debug_purgelist_print(struct dlm_ctxt *dlm, char *buf, int len)
297 {
298 	struct dlm_lock_resource *res;
299 	int out = 0;
300 	unsigned long total = 0;
301 
302 	out += scnprintf(buf + out, len - out,
303 			"Dumping Purgelist for Domain: %s\n", dlm->name);
304 
305 	spin_lock(&dlm->spinlock);
306 	list_for_each_entry(res, &dlm->purge_list, purge) {
307 		++total;
308 		if (len - out < 100)
309 			continue;
310 		spin_lock(&res->spinlock);
311 		out += stringify_lockname(res->lockname.name,
312 					  res->lockname.len,
313 					  buf + out, len - out);
314 		out += scnprintf(buf + out, len - out, "\t%ld\n",
315 				(jiffies - res->last_used)/HZ);
316 		spin_unlock(&res->spinlock);
317 	}
318 	spin_unlock(&dlm->spinlock);
319 
320 	out += scnprintf(buf + out, len - out, "Total on list: %lu\n", total);
321 
322 	return out;
323 }
324 
debug_purgelist_open(struct inode * inode,struct file * file)325 static int debug_purgelist_open(struct inode *inode, struct file *file)
326 {
327 	struct dlm_ctxt *dlm = inode->i_private;
328 	char *buf = NULL;
329 
330 	buf = kzalloc(PAGE_SIZE, GFP_NOFS);
331 	if (!buf)
332 		return -ENOMEM;
333 
334 	i_size_write(inode, debug_purgelist_print(dlm, buf, PAGE_SIZE - 1));
335 
336 	file->private_data = buf;
337 
338 	return 0;
339 }
340 
341 static const struct file_operations debug_purgelist_fops = {
342 	.open =		debug_purgelist_open,
343 	.release =	debug_release,
344 	.read =		debug_read,
345 	.llseek =	generic_file_llseek,
346 };
347 /* end - purge list funcs */
348 
349 /* begin - debug mle funcs */
debug_mle_print(struct dlm_ctxt * dlm,char * buf,int len)350 static int debug_mle_print(struct dlm_ctxt *dlm, char *buf, int len)
351 {
352 	struct dlm_master_list_entry *mle;
353 	struct hlist_head *bucket;
354 	int i, out = 0;
355 	unsigned long total = 0, longest = 0, bucket_count = 0;
356 
357 	out += scnprintf(buf + out, len - out,
358 			"Dumping MLEs for Domain: %s\n", dlm->name);
359 
360 	spin_lock(&dlm->master_lock);
361 	for (i = 0; i < DLM_HASH_BUCKETS; i++) {
362 		bucket = dlm_master_hash(dlm, i);
363 		hlist_for_each_entry(mle, bucket, master_hash_node) {
364 			++total;
365 			++bucket_count;
366 			if (len - out < 200)
367 				continue;
368 			out += dump_mle(mle, buf + out, len - out);
369 		}
370 		longest = max(longest, bucket_count);
371 		bucket_count = 0;
372 	}
373 	spin_unlock(&dlm->master_lock);
374 
375 	out += scnprintf(buf + out, len - out,
376 			"Total: %lu, Longest: %lu\n", total, longest);
377 	return out;
378 }
379 
debug_mle_open(struct inode * inode,struct file * file)380 static int debug_mle_open(struct inode *inode, struct file *file)
381 {
382 	struct dlm_ctxt *dlm = inode->i_private;
383 	char *buf = NULL;
384 
385 	buf = kzalloc(PAGE_SIZE, GFP_NOFS);
386 	if (!buf)
387 		return -ENOMEM;
388 
389 	i_size_write(inode, debug_mle_print(dlm, buf, PAGE_SIZE - 1));
390 
391 	file->private_data = buf;
392 
393 	return 0;
394 }
395 
396 static const struct file_operations debug_mle_fops = {
397 	.open =		debug_mle_open,
398 	.release =	debug_release,
399 	.read =		debug_read,
400 	.llseek =	generic_file_llseek,
401 };
402 
403 /* end - debug mle funcs */
404 
405 /* begin - debug lockres funcs */
dump_lock(struct dlm_lock * lock,int list_type,char * buf,int len)406 static int dump_lock(struct dlm_lock *lock, int list_type, char *buf, int len)
407 {
408 	int out;
409 
410 #define DEBUG_LOCK_VERSION	1
411 	spin_lock(&lock->spinlock);
412 	out = scnprintf(buf, len, "LOCK:%d,%d,%d,%d,%d,%d:%lld,%d,%d,%d,%d,%d,"
413 		       "%d,%d,%d,%d\n",
414 		       DEBUG_LOCK_VERSION,
415 		       list_type, lock->ml.type, lock->ml.convert_type,
416 		       lock->ml.node,
417 		       dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
418 		       dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
419 		       !list_empty(&lock->ast_list),
420 		       !list_empty(&lock->bast_list),
421 		       lock->ast_pending, lock->bast_pending,
422 		       lock->convert_pending, lock->lock_pending,
423 		       lock->cancel_pending, lock->unlock_pending,
424 		       kref_read(&lock->lock_refs));
425 	spin_unlock(&lock->spinlock);
426 
427 	return out;
428 }
429 
dump_lockres(struct dlm_lock_resource * res,char * buf,int len)430 static int dump_lockres(struct dlm_lock_resource *res, char *buf, int len)
431 {
432 	struct dlm_lock *lock;
433 	int i;
434 	int out = 0;
435 
436 	out += scnprintf(buf + out, len - out, "NAME:");
437 	out += stringify_lockname(res->lockname.name, res->lockname.len,
438 				  buf + out, len - out);
439 	out += scnprintf(buf + out, len - out, "\n");
440 
441 #define DEBUG_LRES_VERSION	1
442 	out += scnprintf(buf + out, len - out,
443 			"LRES:%d,%d,%d,%ld,%d,%d,%d,%d,%d,%d,%d\n",
444 			DEBUG_LRES_VERSION,
445 			res->owner, res->state, res->last_used,
446 			!list_empty(&res->purge),
447 			!list_empty(&res->dirty),
448 			!list_empty(&res->recovering),
449 			res->inflight_locks, res->migration_pending,
450 			atomic_read(&res->asts_reserved),
451 			kref_read(&res->refs));
452 
453 	/* refmap */
454 	out += scnprintf(buf + out, len - out, "RMAP:");
455 	out += stringify_nodemap(res->refmap, O2NM_MAX_NODES,
456 				 buf + out, len - out);
457 	out += scnprintf(buf + out, len - out, "\n");
458 
459 	/* lvb */
460 	out += scnprintf(buf + out, len - out, "LVBX:");
461 	for (i = 0; i < DLM_LVB_LEN; i++)
462 		out += scnprintf(buf + out, len - out,
463 					"%02x", (unsigned char)res->lvb[i]);
464 	out += scnprintf(buf + out, len - out, "\n");
465 
466 	/* granted */
467 	list_for_each_entry(lock, &res->granted, list)
468 		out += dump_lock(lock, 0, buf + out, len - out);
469 
470 	/* converting */
471 	list_for_each_entry(lock, &res->converting, list)
472 		out += dump_lock(lock, 1, buf + out, len - out);
473 
474 	/* blocked */
475 	list_for_each_entry(lock, &res->blocked, list)
476 		out += dump_lock(lock, 2, buf + out, len - out);
477 
478 	out += scnprintf(buf + out, len - out, "\n");
479 
480 	return out;
481 }
482 
lockres_seq_start(struct seq_file * m,loff_t * pos)483 static void *lockres_seq_start(struct seq_file *m, loff_t *pos)
484 {
485 	struct debug_lockres *dl = m->private;
486 	struct dlm_ctxt *dlm = dl->dl_ctxt;
487 	struct dlm_lock_resource *oldres = dl->dl_res;
488 	struct dlm_lock_resource *res = NULL, *iter;
489 	struct list_head *track_list;
490 
491 	spin_lock(&dlm->track_lock);
492 	if (oldres)
493 		track_list = &oldres->tracking;
494 	else {
495 		track_list = &dlm->tracking_list;
496 		if (list_empty(track_list)) {
497 			dl = NULL;
498 			spin_unlock(&dlm->track_lock);
499 			goto bail;
500 		}
501 	}
502 
503 	list_for_each_entry(iter, track_list, tracking) {
504 		if (&iter->tracking != &dlm->tracking_list) {
505 			dlm_lockres_get(iter);
506 			res = iter;
507 		}
508 		break;
509 	}
510 	spin_unlock(&dlm->track_lock);
511 
512 	if (oldres)
513 		dlm_lockres_put(oldres);
514 
515 	dl->dl_res = res;
516 
517 	if (res) {
518 		spin_lock(&res->spinlock);
519 		dump_lockres(res, dl->dl_buf, dl->dl_len - 1);
520 		spin_unlock(&res->spinlock);
521 	} else
522 		dl = NULL;
523 
524 bail:
525 	/* passed to seq_show */
526 	return dl;
527 }
528 
lockres_seq_stop(struct seq_file * m,void * v)529 static void lockres_seq_stop(struct seq_file *m, void *v)
530 {
531 }
532 
lockres_seq_next(struct seq_file * m,void * v,loff_t * pos)533 static void *lockres_seq_next(struct seq_file *m, void *v, loff_t *pos)
534 {
535 	return NULL;
536 }
537 
lockres_seq_show(struct seq_file * s,void * v)538 static int lockres_seq_show(struct seq_file *s, void *v)
539 {
540 	struct debug_lockres *dl = (struct debug_lockres *)v;
541 
542 	seq_printf(s, "%s", dl->dl_buf);
543 
544 	return 0;
545 }
546 
547 static const struct seq_operations debug_lockres_ops = {
548 	.start =	lockres_seq_start,
549 	.stop =		lockres_seq_stop,
550 	.next =		lockres_seq_next,
551 	.show =		lockres_seq_show,
552 };
553 
debug_lockres_open(struct inode * inode,struct file * file)554 static int debug_lockres_open(struct inode *inode, struct file *file)
555 {
556 	struct dlm_ctxt *dlm = inode->i_private;
557 	struct debug_lockres *dl;
558 	void *buf;
559 	int status = -ENOMEM;
560 
561 	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
562 	if (!buf)
563 		goto bail;
564 
565 	dl = __seq_open_private(file, &debug_lockres_ops, sizeof(*dl));
566 	if (!dl)
567 		goto bailfree;
568 
569 	dl->dl_len = PAGE_SIZE;
570 	dl->dl_buf = buf;
571 
572 	/* ->release uses dl_ctxt after open, so it needs a real pin. */
573 	dl->dl_ctxt = dlm_grab(dlm);
574 	if (!dl->dl_ctxt) {
575 		status = -ENOENT;
576 		goto bailseq;
577 	}
578 
579 	return 0;
580 
581 bailseq:
582 	seq_release_private(inode, file);
583 bailfree:
584 	kfree(buf);
585 bail:
586 	if (status != -ENOENT)
587 		mlog_errno(status);
588 	return status;
589 }
590 
debug_lockres_release(struct inode * inode,struct file * file)591 static int debug_lockres_release(struct inode *inode, struct file *file)
592 {
593 	struct seq_file *seq = file->private_data;
594 	struct debug_lockres *dl = (struct debug_lockres *)seq->private;
595 
596 	if (dl->dl_res)
597 		dlm_lockres_put(dl->dl_res);
598 	dlm_put(dl->dl_ctxt);
599 	kfree(dl->dl_buf);
600 	return seq_release_private(inode, file);
601 }
602 
603 static const struct file_operations debug_lockres_fops = {
604 	.open =		debug_lockres_open,
605 	.release =	debug_lockres_release,
606 	.read =		seq_read,
607 	.llseek =	seq_lseek,
608 };
609 /* end - debug lockres funcs */
610 
611 /* begin - debug state funcs */
debug_state_print(struct dlm_ctxt * dlm,char * buf,int len)612 static int debug_state_print(struct dlm_ctxt *dlm, char *buf, int len)
613 {
614 	int out = 0;
615 	struct dlm_reco_node_data *node;
616 	char *state;
617 	int cur_mles = 0, tot_mles = 0;
618 	int i;
619 
620 	spin_lock(&dlm->spinlock);
621 
622 	switch (dlm->dlm_state) {
623 	case DLM_CTXT_NEW:
624 		state = "NEW"; break;
625 	case DLM_CTXT_JOINED:
626 		state = "JOINED"; break;
627 	case DLM_CTXT_IN_SHUTDOWN:
628 		state = "SHUTDOWN"; break;
629 	case DLM_CTXT_LEAVING:
630 		state = "LEAVING"; break;
631 	default:
632 		state = "UNKNOWN"; break;
633 	}
634 
635 	/* Domain: xxxxxxxxxx  Key: 0xdfbac769 */
636 	out += scnprintf(buf + out, len - out,
637 			"Domain: %s  Key: 0x%08x  Protocol: %d.%d\n",
638 			dlm->name, dlm->key, dlm->dlm_locking_proto.pv_major,
639 			dlm->dlm_locking_proto.pv_minor);
640 
641 	/* Thread Pid: xxx  Node: xxx  State: xxxxx */
642 	out += scnprintf(buf + out, len - out,
643 			"Thread Pid: %d  Node: %d  State: %s\n",
644 			task_pid_nr(dlm->dlm_thread_task), dlm->node_num, state);
645 
646 	/* Number of Joins: xxx  Joining Node: xxx */
647 	out += scnprintf(buf + out, len - out,
648 			"Number of Joins: %d  Joining Node: %d\n",
649 			dlm->num_joins, dlm->joining_node);
650 
651 	/* Domain Map: xx xx xx */
652 	out += scnprintf(buf + out, len - out, "Domain Map: ");
653 	out += stringify_nodemap(dlm->domain_map, O2NM_MAX_NODES,
654 				 buf + out, len - out);
655 	out += scnprintf(buf + out, len - out, "\n");
656 
657 	/* Exit Domain Map: xx xx xx */
658 	out += scnprintf(buf + out, len - out, "Exit Domain Map: ");
659 	out += stringify_nodemap(dlm->exit_domain_map, O2NM_MAX_NODES,
660 				 buf + out, len - out);
661 	out += scnprintf(buf + out, len - out, "\n");
662 
663 	/* Live Map: xx xx xx */
664 	out += scnprintf(buf + out, len - out, "Live Map: ");
665 	out += stringify_nodemap(dlm->live_nodes_map, O2NM_MAX_NODES,
666 				 buf + out, len - out);
667 	out += scnprintf(buf + out, len - out, "\n");
668 
669 	/* Lock Resources: xxx (xxx) */
670 	out += scnprintf(buf + out, len - out,
671 			"Lock Resources: %d (%d)\n",
672 			atomic_read(&dlm->res_cur_count),
673 			atomic_read(&dlm->res_tot_count));
674 
675 	for (i = 0; i < DLM_MLE_NUM_TYPES; ++i)
676 		tot_mles += atomic_read(&dlm->mle_tot_count[i]);
677 
678 	for (i = 0; i < DLM_MLE_NUM_TYPES; ++i)
679 		cur_mles += atomic_read(&dlm->mle_cur_count[i]);
680 
681 	/* MLEs: xxx (xxx) */
682 	out += scnprintf(buf + out, len - out,
683 			"MLEs: %d (%d)\n", cur_mles, tot_mles);
684 
685 	/*  Blocking: xxx (xxx) */
686 	out += scnprintf(buf + out, len - out,
687 			"  Blocking: %d (%d)\n",
688 			atomic_read(&dlm->mle_cur_count[DLM_MLE_BLOCK]),
689 			atomic_read(&dlm->mle_tot_count[DLM_MLE_BLOCK]));
690 
691 	/*  Mastery: xxx (xxx) */
692 	out += scnprintf(buf + out, len - out,
693 			"  Mastery: %d (%d)\n",
694 			atomic_read(&dlm->mle_cur_count[DLM_MLE_MASTER]),
695 			atomic_read(&dlm->mle_tot_count[DLM_MLE_MASTER]));
696 
697 	/*  Migration: xxx (xxx) */
698 	out += scnprintf(buf + out, len - out,
699 			"  Migration: %d (%d)\n",
700 			atomic_read(&dlm->mle_cur_count[DLM_MLE_MIGRATION]),
701 			atomic_read(&dlm->mle_tot_count[DLM_MLE_MIGRATION]));
702 
703 	/* Lists: Dirty=Empty  Purge=InUse  PendingASTs=Empty  ... */
704 	out += scnprintf(buf + out, len - out,
705 			"Lists: Dirty=%s  Purge=%s  PendingASTs=%s  "
706 			"PendingBASTs=%s\n",
707 			(list_empty(&dlm->dirty_list) ? "Empty" : "InUse"),
708 			(list_empty(&dlm->purge_list) ? "Empty" : "InUse"),
709 			(list_empty(&dlm->pending_asts) ? "Empty" : "InUse"),
710 			(list_empty(&dlm->pending_basts) ? "Empty" : "InUse"));
711 
712 	/* Purge Count: xxx  Refs: xxx */
713 	out += scnprintf(buf + out, len - out,
714 			"Purge Count: %d  Refs: %d\n", dlm->purge_count,
715 			kref_read(&dlm->dlm_refs));
716 
717 	/* Dead Node: xxx */
718 	out += scnprintf(buf + out, len - out,
719 			"Dead Node: %d\n", dlm->reco.dead_node);
720 
721 	/* What about DLM_RECO_STATE_FINALIZE? */
722 	if (dlm->reco.state == DLM_RECO_STATE_ACTIVE)
723 		state = "ACTIVE";
724 	else
725 		state = "INACTIVE";
726 
727 	/* Recovery Pid: xxxx  Master: xxx  State: xxxx */
728 	out += scnprintf(buf + out, len - out,
729 			"Recovery Pid: %d  Master: %d  State: %s\n",
730 			task_pid_nr(dlm->dlm_reco_thread_task),
731 			dlm->reco.new_master, state);
732 
733 	/* Recovery Map: xx xx */
734 	out += scnprintf(buf + out, len - out, "Recovery Map: ");
735 	out += stringify_nodemap(dlm->recovery_map, O2NM_MAX_NODES,
736 				 buf + out, len - out);
737 	out += scnprintf(buf + out, len - out, "\n");
738 
739 	spin_unlock(&dlm->spinlock);
740 
741 	/* Recovery Node State: */
742 	out += scnprintf(buf + out, len - out, "Recovery Node State:\n");
743 	spin_lock(&dlm_reco_state_lock);
744 	list_for_each_entry(node, &dlm->reco.node_data, list) {
745 		switch (node->state) {
746 		case DLM_RECO_NODE_DATA_INIT:
747 			state = "INIT";
748 			break;
749 		case DLM_RECO_NODE_DATA_REQUESTING:
750 			state = "REQUESTING";
751 			break;
752 		case DLM_RECO_NODE_DATA_DEAD:
753 			state = "DEAD";
754 			break;
755 		case DLM_RECO_NODE_DATA_RECEIVING:
756 			state = "RECEIVING";
757 			break;
758 		case DLM_RECO_NODE_DATA_REQUESTED:
759 			state = "REQUESTED";
760 			break;
761 		case DLM_RECO_NODE_DATA_DONE:
762 			state = "DONE";
763 			break;
764 		case DLM_RECO_NODE_DATA_FINALIZE_SENT:
765 			state = "FINALIZE-SENT";
766 			break;
767 		default:
768 			state = "BAD";
769 			break;
770 		}
771 		out += scnprintf(buf + out, len - out, "\t%u - %s\n",
772 				node->node_num, state);
773 	}
774 
775 	spin_unlock(&dlm_reco_state_lock);
776 
777 	return out;
778 }
779 
debug_state_open(struct inode * inode,struct file * file)780 static int debug_state_open(struct inode *inode, struct file *file)
781 {
782 	struct dlm_ctxt *dlm = inode->i_private;
783 	char *buf = NULL;
784 
785 	buf = kzalloc(PAGE_SIZE, GFP_NOFS);
786 	if (!buf)
787 		return -ENOMEM;
788 
789 	i_size_write(inode, debug_state_print(dlm, buf, PAGE_SIZE - 1));
790 
791 	file->private_data = buf;
792 
793 	return 0;
794 }
795 
796 static const struct file_operations debug_state_fops = {
797 	.open =		debug_state_open,
798 	.release =	debug_release,
799 	.read =		debug_read,
800 	.llseek =	generic_file_llseek,
801 };
802 /* end  - debug state funcs */
803 
804 /* files in subroot */
dlm_debug_init(struct dlm_ctxt * dlm)805 void dlm_debug_init(struct dlm_ctxt *dlm)
806 {
807 	/* for dumping dlm_ctxt */
808 	debugfs_create_file(DLM_DEBUGFS_DLM_STATE, S_IFREG|S_IRUSR,
809 			    dlm->dlm_debugfs_subroot, dlm, &debug_state_fops);
810 
811 	/* for dumping lockres */
812 	debugfs_create_file(DLM_DEBUGFS_LOCKING_STATE, S_IFREG|S_IRUSR,
813 			    dlm->dlm_debugfs_subroot, dlm, &debug_lockres_fops);
814 
815 	/* for dumping mles */
816 	debugfs_create_file(DLM_DEBUGFS_MLE_STATE, S_IFREG|S_IRUSR,
817 			    dlm->dlm_debugfs_subroot, dlm, &debug_mle_fops);
818 
819 	/* for dumping lockres on the purge list */
820 	debugfs_create_file(DLM_DEBUGFS_PURGE_LIST, S_IFREG|S_IRUSR,
821 			    dlm->dlm_debugfs_subroot, dlm,
822 			    &debug_purgelist_fops);
823 }
824 
825 /* subroot - domain dir */
dlm_create_debugfs_subroot(struct dlm_ctxt * dlm)826 void dlm_create_debugfs_subroot(struct dlm_ctxt *dlm)
827 {
828 	dlm->dlm_debugfs_subroot = debugfs_create_dir(dlm->name,
829 						      dlm_debugfs_root);
830 }
831 
dlm_destroy_debugfs_subroot(struct dlm_ctxt * dlm)832 void dlm_destroy_debugfs_subroot(struct dlm_ctxt *dlm)
833 {
834 	debugfs_remove_recursive(dlm->dlm_debugfs_subroot);
835 }
836 
837 /* debugfs root */
dlm_create_debugfs_root(void)838 void dlm_create_debugfs_root(void)
839 {
840 	dlm_debugfs_root = debugfs_create_dir(DLM_DEBUGFS_DIR, NULL);
841 }
842 
dlm_destroy_debugfs_root(void)843 void dlm_destroy_debugfs_root(void)
844 {
845 	debugfs_remove(dlm_debugfs_root);
846 }
847 #endif	/* CONFIG_DEBUG_FS */
848