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 = (char *) get_zeroed_page(GFP_ATOMIC);
264 	if (buf) {
265 		dump_mle(mle, buf, PAGE_SIZE - 1);
266 		free_page((unsigned long)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 	free_page((unsigned long)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 = (char *) get_zeroed_page(GFP_NOFS);
331 	if (!buf)
332 		goto bail;
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 bail:
340 	return -ENOMEM;
341 }
342 
343 static const struct file_operations debug_purgelist_fops = {
344 	.open =		debug_purgelist_open,
345 	.release =	debug_release,
346 	.read =		debug_read,
347 	.llseek =	generic_file_llseek,
348 };
349 /* end - purge list funcs */
350 
351 /* begin - debug mle funcs */
debug_mle_print(struct dlm_ctxt * dlm,char * buf,int len)352 static int debug_mle_print(struct dlm_ctxt *dlm, char *buf, int len)
353 {
354 	struct dlm_master_list_entry *mle;
355 	struct hlist_head *bucket;
356 	int i, out = 0;
357 	unsigned long total = 0, longest = 0, bucket_count = 0;
358 
359 	out += scnprintf(buf + out, len - out,
360 			"Dumping MLEs for Domain: %s\n", dlm->name);
361 
362 	spin_lock(&dlm->master_lock);
363 	for (i = 0; i < DLM_HASH_BUCKETS; i++) {
364 		bucket = dlm_master_hash(dlm, i);
365 		hlist_for_each_entry(mle, bucket, master_hash_node) {
366 			++total;
367 			++bucket_count;
368 			if (len - out < 200)
369 				continue;
370 			out += dump_mle(mle, buf + out, len - out);
371 		}
372 		longest = max(longest, bucket_count);
373 		bucket_count = 0;
374 	}
375 	spin_unlock(&dlm->master_lock);
376 
377 	out += scnprintf(buf + out, len - out,
378 			"Total: %lu, Longest: %lu\n", total, longest);
379 	return out;
380 }
381 
debug_mle_open(struct inode * inode,struct file * file)382 static int debug_mle_open(struct inode *inode, struct file *file)
383 {
384 	struct dlm_ctxt *dlm = inode->i_private;
385 	char *buf = NULL;
386 
387 	buf = (char *) get_zeroed_page(GFP_NOFS);
388 	if (!buf)
389 		goto bail;
390 
391 	i_size_write(inode, debug_mle_print(dlm, buf, PAGE_SIZE - 1));
392 
393 	file->private_data = buf;
394 
395 	return 0;
396 bail:
397 	return -ENOMEM;
398 }
399 
400 static const struct file_operations debug_mle_fops = {
401 	.open =		debug_mle_open,
402 	.release =	debug_release,
403 	.read =		debug_read,
404 	.llseek =	generic_file_llseek,
405 };
406 
407 /* end - debug mle funcs */
408 
409 /* begin - debug lockres funcs */
dump_lock(struct dlm_lock * lock,int list_type,char * buf,int len)410 static int dump_lock(struct dlm_lock *lock, int list_type, char *buf, int len)
411 {
412 	int out;
413 
414 #define DEBUG_LOCK_VERSION	1
415 	spin_lock(&lock->spinlock);
416 	out = scnprintf(buf, len, "LOCK:%d,%d,%d,%d,%d,%d:%lld,%d,%d,%d,%d,%d,"
417 		       "%d,%d,%d,%d\n",
418 		       DEBUG_LOCK_VERSION,
419 		       list_type, lock->ml.type, lock->ml.convert_type,
420 		       lock->ml.node,
421 		       dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
422 		       dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
423 		       !list_empty(&lock->ast_list),
424 		       !list_empty(&lock->bast_list),
425 		       lock->ast_pending, lock->bast_pending,
426 		       lock->convert_pending, lock->lock_pending,
427 		       lock->cancel_pending, lock->unlock_pending,
428 		       kref_read(&lock->lock_refs));
429 	spin_unlock(&lock->spinlock);
430 
431 	return out;
432 }
433 
dump_lockres(struct dlm_lock_resource * res,char * buf,int len)434 static int dump_lockres(struct dlm_lock_resource *res, char *buf, int len)
435 {
436 	struct dlm_lock *lock;
437 	int i;
438 	int out = 0;
439 
440 	out += scnprintf(buf + out, len - out, "NAME:");
441 	out += stringify_lockname(res->lockname.name, res->lockname.len,
442 				  buf + out, len - out);
443 	out += scnprintf(buf + out, len - out, "\n");
444 
445 #define DEBUG_LRES_VERSION	1
446 	out += scnprintf(buf + out, len - out,
447 			"LRES:%d,%d,%d,%ld,%d,%d,%d,%d,%d,%d,%d\n",
448 			DEBUG_LRES_VERSION,
449 			res->owner, res->state, res->last_used,
450 			!list_empty(&res->purge),
451 			!list_empty(&res->dirty),
452 			!list_empty(&res->recovering),
453 			res->inflight_locks, res->migration_pending,
454 			atomic_read(&res->asts_reserved),
455 			kref_read(&res->refs));
456 
457 	/* refmap */
458 	out += scnprintf(buf + out, len - out, "RMAP:");
459 	out += stringify_nodemap(res->refmap, O2NM_MAX_NODES,
460 				 buf + out, len - out);
461 	out += scnprintf(buf + out, len - out, "\n");
462 
463 	/* lvb */
464 	out += scnprintf(buf + out, len - out, "LVBX:");
465 	for (i = 0; i < DLM_LVB_LEN; i++)
466 		out += scnprintf(buf + out, len - out,
467 					"%02x", (unsigned char)res->lvb[i]);
468 	out += scnprintf(buf + out, len - out, "\n");
469 
470 	/* granted */
471 	list_for_each_entry(lock, &res->granted, list)
472 		out += dump_lock(lock, 0, buf + out, len - out);
473 
474 	/* converting */
475 	list_for_each_entry(lock, &res->converting, list)
476 		out += dump_lock(lock, 1, buf + out, len - out);
477 
478 	/* blocked */
479 	list_for_each_entry(lock, &res->blocked, list)
480 		out += dump_lock(lock, 2, buf + out, len - out);
481 
482 	out += scnprintf(buf + out, len - out, "\n");
483 
484 	return out;
485 }
486 
lockres_seq_start(struct seq_file * m,loff_t * pos)487 static void *lockres_seq_start(struct seq_file *m, loff_t *pos)
488 {
489 	struct debug_lockres *dl = m->private;
490 	struct dlm_ctxt *dlm = dl->dl_ctxt;
491 	struct dlm_lock_resource *oldres = dl->dl_res;
492 	struct dlm_lock_resource *res = NULL, *iter;
493 	struct list_head *track_list;
494 
495 	spin_lock(&dlm->track_lock);
496 	if (oldres)
497 		track_list = &oldres->tracking;
498 	else {
499 		track_list = &dlm->tracking_list;
500 		if (list_empty(track_list)) {
501 			dl = NULL;
502 			spin_unlock(&dlm->track_lock);
503 			goto bail;
504 		}
505 	}
506 
507 	list_for_each_entry(iter, track_list, tracking) {
508 		if (&iter->tracking != &dlm->tracking_list) {
509 			dlm_lockres_get(iter);
510 			res = iter;
511 		}
512 		break;
513 	}
514 	spin_unlock(&dlm->track_lock);
515 
516 	if (oldres)
517 		dlm_lockres_put(oldres);
518 
519 	dl->dl_res = res;
520 
521 	if (res) {
522 		spin_lock(&res->spinlock);
523 		dump_lockres(res, dl->dl_buf, dl->dl_len - 1);
524 		spin_unlock(&res->spinlock);
525 	} else
526 		dl = NULL;
527 
528 bail:
529 	/* passed to seq_show */
530 	return dl;
531 }
532 
lockres_seq_stop(struct seq_file * m,void * v)533 static void lockres_seq_stop(struct seq_file *m, void *v)
534 {
535 }
536 
lockres_seq_next(struct seq_file * m,void * v,loff_t * pos)537 static void *lockres_seq_next(struct seq_file *m, void *v, loff_t *pos)
538 {
539 	return NULL;
540 }
541 
lockres_seq_show(struct seq_file * s,void * v)542 static int lockres_seq_show(struct seq_file *s, void *v)
543 {
544 	struct debug_lockres *dl = (struct debug_lockres *)v;
545 
546 	seq_printf(s, "%s", dl->dl_buf);
547 
548 	return 0;
549 }
550 
551 static const struct seq_operations debug_lockres_ops = {
552 	.start =	lockres_seq_start,
553 	.stop =		lockres_seq_stop,
554 	.next =		lockres_seq_next,
555 	.show =		lockres_seq_show,
556 };
557 
debug_lockres_open(struct inode * inode,struct file * file)558 static int debug_lockres_open(struct inode *inode, struct file *file)
559 {
560 	struct dlm_ctxt *dlm = inode->i_private;
561 	struct debug_lockres *dl;
562 	void *buf;
563 
564 	buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
565 	if (!buf)
566 		goto bail;
567 
568 	dl = __seq_open_private(file, &debug_lockres_ops, sizeof(*dl));
569 	if (!dl)
570 		goto bailfree;
571 
572 	dl->dl_len = PAGE_SIZE;
573 	dl->dl_buf = buf;
574 
575 	dlm_grab(dlm);
576 	dl->dl_ctxt = dlm;
577 
578 	return 0;
579 
580 bailfree:
581 	kfree(buf);
582 bail:
583 	mlog_errno(-ENOMEM);
584 	return -ENOMEM;
585 }
586 
debug_lockres_release(struct inode * inode,struct file * file)587 static int debug_lockres_release(struct inode *inode, struct file *file)
588 {
589 	struct seq_file *seq = file->private_data;
590 	struct debug_lockres *dl = (struct debug_lockres *)seq->private;
591 
592 	if (dl->dl_res)
593 		dlm_lockres_put(dl->dl_res);
594 	dlm_put(dl->dl_ctxt);
595 	kfree(dl->dl_buf);
596 	return seq_release_private(inode, file);
597 }
598 
599 static const struct file_operations debug_lockres_fops = {
600 	.open =		debug_lockres_open,
601 	.release =	debug_lockres_release,
602 	.read =		seq_read,
603 	.llseek =	seq_lseek,
604 };
605 /* end - debug lockres funcs */
606 
607 /* begin - debug state funcs */
debug_state_print(struct dlm_ctxt * dlm,char * buf,int len)608 static int debug_state_print(struct dlm_ctxt *dlm, char *buf, int len)
609 {
610 	int out = 0;
611 	struct dlm_reco_node_data *node;
612 	char *state;
613 	int cur_mles = 0, tot_mles = 0;
614 	int i;
615 
616 	spin_lock(&dlm->spinlock);
617 
618 	switch (dlm->dlm_state) {
619 	case DLM_CTXT_NEW:
620 		state = "NEW"; break;
621 	case DLM_CTXT_JOINED:
622 		state = "JOINED"; break;
623 	case DLM_CTXT_IN_SHUTDOWN:
624 		state = "SHUTDOWN"; break;
625 	case DLM_CTXT_LEAVING:
626 		state = "LEAVING"; break;
627 	default:
628 		state = "UNKNOWN"; break;
629 	}
630 
631 	/* Domain: xxxxxxxxxx  Key: 0xdfbac769 */
632 	out += scnprintf(buf + out, len - out,
633 			"Domain: %s  Key: 0x%08x  Protocol: %d.%d\n",
634 			dlm->name, dlm->key, dlm->dlm_locking_proto.pv_major,
635 			dlm->dlm_locking_proto.pv_minor);
636 
637 	/* Thread Pid: xxx  Node: xxx  State: xxxxx */
638 	out += scnprintf(buf + out, len - out,
639 			"Thread Pid: %d  Node: %d  State: %s\n",
640 			task_pid_nr(dlm->dlm_thread_task), dlm->node_num, state);
641 
642 	/* Number of Joins: xxx  Joining Node: xxx */
643 	out += scnprintf(buf + out, len - out,
644 			"Number of Joins: %d  Joining Node: %d\n",
645 			dlm->num_joins, dlm->joining_node);
646 
647 	/* Domain Map: xx xx xx */
648 	out += scnprintf(buf + out, len - out, "Domain Map: ");
649 	out += stringify_nodemap(dlm->domain_map, O2NM_MAX_NODES,
650 				 buf + out, len - out);
651 	out += scnprintf(buf + out, len - out, "\n");
652 
653 	/* Exit Domain Map: xx xx xx */
654 	out += scnprintf(buf + out, len - out, "Exit Domain Map: ");
655 	out += stringify_nodemap(dlm->exit_domain_map, O2NM_MAX_NODES,
656 				 buf + out, len - out);
657 	out += scnprintf(buf + out, len - out, "\n");
658 
659 	/* Live Map: xx xx xx */
660 	out += scnprintf(buf + out, len - out, "Live Map: ");
661 	out += stringify_nodemap(dlm->live_nodes_map, O2NM_MAX_NODES,
662 				 buf + out, len - out);
663 	out += scnprintf(buf + out, len - out, "\n");
664 
665 	/* Lock Resources: xxx (xxx) */
666 	out += scnprintf(buf + out, len - out,
667 			"Lock Resources: %d (%d)\n",
668 			atomic_read(&dlm->res_cur_count),
669 			atomic_read(&dlm->res_tot_count));
670 
671 	for (i = 0; i < DLM_MLE_NUM_TYPES; ++i)
672 		tot_mles += atomic_read(&dlm->mle_tot_count[i]);
673 
674 	for (i = 0; i < DLM_MLE_NUM_TYPES; ++i)
675 		cur_mles += atomic_read(&dlm->mle_cur_count[i]);
676 
677 	/* MLEs: xxx (xxx) */
678 	out += scnprintf(buf + out, len - out,
679 			"MLEs: %d (%d)\n", cur_mles, tot_mles);
680 
681 	/*  Blocking: xxx (xxx) */
682 	out += scnprintf(buf + out, len - out,
683 			"  Blocking: %d (%d)\n",
684 			atomic_read(&dlm->mle_cur_count[DLM_MLE_BLOCK]),
685 			atomic_read(&dlm->mle_tot_count[DLM_MLE_BLOCK]));
686 
687 	/*  Mastery: xxx (xxx) */
688 	out += scnprintf(buf + out, len - out,
689 			"  Mastery: %d (%d)\n",
690 			atomic_read(&dlm->mle_cur_count[DLM_MLE_MASTER]),
691 			atomic_read(&dlm->mle_tot_count[DLM_MLE_MASTER]));
692 
693 	/*  Migration: xxx (xxx) */
694 	out += scnprintf(buf + out, len - out,
695 			"  Migration: %d (%d)\n",
696 			atomic_read(&dlm->mle_cur_count[DLM_MLE_MIGRATION]),
697 			atomic_read(&dlm->mle_tot_count[DLM_MLE_MIGRATION]));
698 
699 	/* Lists: Dirty=Empty  Purge=InUse  PendingASTs=Empty  ... */
700 	out += scnprintf(buf + out, len - out,
701 			"Lists: Dirty=%s  Purge=%s  PendingASTs=%s  "
702 			"PendingBASTs=%s\n",
703 			(list_empty(&dlm->dirty_list) ? "Empty" : "InUse"),
704 			(list_empty(&dlm->purge_list) ? "Empty" : "InUse"),
705 			(list_empty(&dlm->pending_asts) ? "Empty" : "InUse"),
706 			(list_empty(&dlm->pending_basts) ? "Empty" : "InUse"));
707 
708 	/* Purge Count: xxx  Refs: xxx */
709 	out += scnprintf(buf + out, len - out,
710 			"Purge Count: %d  Refs: %d\n", dlm->purge_count,
711 			kref_read(&dlm->dlm_refs));
712 
713 	/* Dead Node: xxx */
714 	out += scnprintf(buf + out, len - out,
715 			"Dead Node: %d\n", dlm->reco.dead_node);
716 
717 	/* What about DLM_RECO_STATE_FINALIZE? */
718 	if (dlm->reco.state == DLM_RECO_STATE_ACTIVE)
719 		state = "ACTIVE";
720 	else
721 		state = "INACTIVE";
722 
723 	/* Recovery Pid: xxxx  Master: xxx  State: xxxx */
724 	out += scnprintf(buf + out, len - out,
725 			"Recovery Pid: %d  Master: %d  State: %s\n",
726 			task_pid_nr(dlm->dlm_reco_thread_task),
727 			dlm->reco.new_master, state);
728 
729 	/* Recovery Map: xx xx */
730 	out += scnprintf(buf + out, len - out, "Recovery Map: ");
731 	out += stringify_nodemap(dlm->recovery_map, O2NM_MAX_NODES,
732 				 buf + out, len - out);
733 	out += scnprintf(buf + out, len - out, "\n");
734 
735 	/* Recovery Node State: */
736 	out += scnprintf(buf + out, len - out, "Recovery Node State:\n");
737 	list_for_each_entry(node, &dlm->reco.node_data, list) {
738 		switch (node->state) {
739 		case DLM_RECO_NODE_DATA_INIT:
740 			state = "INIT";
741 			break;
742 		case DLM_RECO_NODE_DATA_REQUESTING:
743 			state = "REQUESTING";
744 			break;
745 		case DLM_RECO_NODE_DATA_DEAD:
746 			state = "DEAD";
747 			break;
748 		case DLM_RECO_NODE_DATA_RECEIVING:
749 			state = "RECEIVING";
750 			break;
751 		case DLM_RECO_NODE_DATA_REQUESTED:
752 			state = "REQUESTED";
753 			break;
754 		case DLM_RECO_NODE_DATA_DONE:
755 			state = "DONE";
756 			break;
757 		case DLM_RECO_NODE_DATA_FINALIZE_SENT:
758 			state = "FINALIZE-SENT";
759 			break;
760 		default:
761 			state = "BAD";
762 			break;
763 		}
764 		out += scnprintf(buf + out, len - out, "\t%u - %s\n",
765 				node->node_num, state);
766 	}
767 
768 	spin_unlock(&dlm->spinlock);
769 
770 	return out;
771 }
772 
debug_state_open(struct inode * inode,struct file * file)773 static int debug_state_open(struct inode *inode, struct file *file)
774 {
775 	struct dlm_ctxt *dlm = inode->i_private;
776 	char *buf = NULL;
777 
778 	buf = (char *) get_zeroed_page(GFP_NOFS);
779 	if (!buf)
780 		goto bail;
781 
782 	i_size_write(inode, debug_state_print(dlm, buf, PAGE_SIZE - 1));
783 
784 	file->private_data = buf;
785 
786 	return 0;
787 bail:
788 	return -ENOMEM;
789 }
790 
791 static const struct file_operations debug_state_fops = {
792 	.open =		debug_state_open,
793 	.release =	debug_release,
794 	.read =		debug_read,
795 	.llseek =	generic_file_llseek,
796 };
797 /* end  - debug state funcs */
798 
799 /* files in subroot */
dlm_debug_init(struct dlm_ctxt * dlm)800 void dlm_debug_init(struct dlm_ctxt *dlm)
801 {
802 	/* for dumping dlm_ctxt */
803 	debugfs_create_file(DLM_DEBUGFS_DLM_STATE, S_IFREG|S_IRUSR,
804 			    dlm->dlm_debugfs_subroot, dlm, &debug_state_fops);
805 
806 	/* for dumping lockres */
807 	debugfs_create_file(DLM_DEBUGFS_LOCKING_STATE, S_IFREG|S_IRUSR,
808 			    dlm->dlm_debugfs_subroot, dlm, &debug_lockres_fops);
809 
810 	/* for dumping mles */
811 	debugfs_create_file(DLM_DEBUGFS_MLE_STATE, S_IFREG|S_IRUSR,
812 			    dlm->dlm_debugfs_subroot, dlm, &debug_mle_fops);
813 
814 	/* for dumping lockres on the purge list */
815 	debugfs_create_file(DLM_DEBUGFS_PURGE_LIST, S_IFREG|S_IRUSR,
816 			    dlm->dlm_debugfs_subroot, dlm,
817 			    &debug_purgelist_fops);
818 }
819 
820 /* subroot - domain dir */
dlm_create_debugfs_subroot(struct dlm_ctxt * dlm)821 void dlm_create_debugfs_subroot(struct dlm_ctxt *dlm)
822 {
823 	dlm->dlm_debugfs_subroot = debugfs_create_dir(dlm->name,
824 						      dlm_debugfs_root);
825 }
826 
dlm_destroy_debugfs_subroot(struct dlm_ctxt * dlm)827 void dlm_destroy_debugfs_subroot(struct dlm_ctxt *dlm)
828 {
829 	debugfs_remove_recursive(dlm->dlm_debugfs_subroot);
830 }
831 
832 /* debugfs root */
dlm_create_debugfs_root(void)833 void dlm_create_debugfs_root(void)
834 {
835 	dlm_debugfs_root = debugfs_create_dir(DLM_DEBUGFS_DIR, NULL);
836 }
837 
dlm_destroy_debugfs_root(void)838 void dlm_destroy_debugfs_root(void)
839 {
840 	debugfs_remove(dlm_debugfs_root);
841 }
842 #endif	/* CONFIG_DEBUG_FS */
843