blob: 1c9b08095f987e0899d13801e3faadbcbf74876c [file] [log] [blame]
David Teiglande7fd4172006-01-18 09:30:29 +00001/******************************************************************************
2*******************************************************************************
3**
David Teigland892c4462009-01-07 16:48:52 -06004** Copyright (C) 2005-2009 Red Hat, Inc. All rights reserved.
David Teiglande7fd4172006-01-18 09:30:29 +00005**
6** This copyrighted material is made available to anyone wishing to use,
7** modify, copy, or redistribute it subject to the terms and conditions
8** of the GNU General Public License v.2.
9**
10*******************************************************************************
11******************************************************************************/
12
13#include <linux/pagemap.h>
14#include <linux/seq_file.h>
15#include <linux/module.h>
16#include <linux/ctype.h>
17#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
David Teiglande7fd4172006-01-18 09:30:29 +000019
20#include "dlm_internal.h"
Josef Bacik916297a2007-05-16 15:56:13 -040021#include "lock.h"
David Teiglande7fd4172006-01-18 09:30:29 +000022
David Teigland5de63192006-07-25 13:44:31 -050023#define DLM_DEBUG_BUF_LEN 4096
24static char debug_buf[DLM_DEBUG_BUF_LEN];
25static struct mutex debug_buf_lock;
David Teiglande7fd4172006-01-18 09:30:29 +000026
27static struct dentry *dlm_root;
28
David Teiglande7fd4172006-01-18 09:30:29 +000029static char *print_lockmode(int mode)
30{
31 switch (mode) {
32 case DLM_LOCK_IV:
33 return "--";
34 case DLM_LOCK_NL:
35 return "NL";
36 case DLM_LOCK_CR:
37 return "CR";
38 case DLM_LOCK_CW:
39 return "CW";
40 case DLM_LOCK_PR:
41 return "PR";
42 case DLM_LOCK_PW:
43 return "PW";
44 case DLM_LOCK_EX:
45 return "EX";
46 default:
47 return "??";
48 }
49}
50
David Teigland892c4462009-01-07 16:48:52 -060051static int print_format1_lock(struct seq_file *s, struct dlm_lkb *lkb,
52 struct dlm_rsb *res)
David Teiglande7fd4172006-01-18 09:30:29 +000053{
54 seq_printf(s, "%08x %s", lkb->lkb_id, print_lockmode(lkb->lkb_grmode));
55
David Teigland892c4462009-01-07 16:48:52 -060056 if (lkb->lkb_status == DLM_LKSTS_CONVERT ||
57 lkb->lkb_status == DLM_LKSTS_WAITING)
David Teiglande7fd4172006-01-18 09:30:29 +000058 seq_printf(s, " (%s)", print_lockmode(lkb->lkb_rqmode));
59
David Teiglande7fd4172006-01-18 09:30:29 +000060 if (lkb->lkb_nodeid) {
61 if (lkb->lkb_nodeid != res->res_nodeid)
62 seq_printf(s, " Remote: %3d %08x", lkb->lkb_nodeid,
63 lkb->lkb_remid);
64 else
65 seq_printf(s, " Master: %08x", lkb->lkb_remid);
66 }
67
68 if (lkb->lkb_wait_type)
69 seq_printf(s, " wait_type: %d", lkb->lkb_wait_type);
70
David Teigland892c4462009-01-07 16:48:52 -060071 return seq_printf(s, "\n");
David Teiglande7fd4172006-01-18 09:30:29 +000072}
73
David Teiglandd0225092008-12-16 14:53:23 -060074static int print_format1(struct dlm_rsb *res, struct seq_file *s)
David Teiglande7fd4172006-01-18 09:30:29 +000075{
76 struct dlm_lkb *lkb;
David Teigland5de63192006-07-25 13:44:31 -050077 int i, lvblen = res->res_ls->ls_lvblen, recover_list, root_list;
David Teigland892c4462009-01-07 16:48:52 -060078 int rv;
David Teiglande7fd4172006-01-18 09:30:29 +000079
David Teigland9dd592d2007-05-29 08:47:04 -050080 lock_rsb(res);
81
David Teigland892c4462009-01-07 16:48:52 -060082 rv = seq_printf(s, "\nResource %p Name (len=%d) \"",
83 res, res->res_length);
84 if (rv)
85 goto out;
86
David Teiglande7fd4172006-01-18 09:30:29 +000087 for (i = 0; i < res->res_length; i++) {
88 if (isprint(res->res_name[i]))
89 seq_printf(s, "%c", res->res_name[i]);
90 else
91 seq_printf(s, "%c", '.');
92 }
David Teigland892c4462009-01-07 16:48:52 -060093
David Teiglande7fd4172006-01-18 09:30:29 +000094 if (res->res_nodeid > 0)
David Teigland892c4462009-01-07 16:48:52 -060095 rv = seq_printf(s, "\" \nLocal Copy, Master is node %d\n",
96 res->res_nodeid);
David Teiglande7fd4172006-01-18 09:30:29 +000097 else if (res->res_nodeid == 0)
David Teigland892c4462009-01-07 16:48:52 -060098 rv = seq_printf(s, "\" \nMaster Copy\n");
David Teiglande7fd4172006-01-18 09:30:29 +000099 else if (res->res_nodeid == -1)
David Teigland892c4462009-01-07 16:48:52 -0600100 rv = seq_printf(s, "\" \nLooking up master (lkid %x)\n",
101 res->res_first_lkid);
David Teiglande7fd4172006-01-18 09:30:29 +0000102 else
David Teigland892c4462009-01-07 16:48:52 -0600103 rv = seq_printf(s, "\" \nInvalid master %d\n",
104 res->res_nodeid);
105 if (rv)
106 goto out;
David Teiglande7fd4172006-01-18 09:30:29 +0000107
108 /* Print the LVB: */
109 if (res->res_lvbptr) {
110 seq_printf(s, "LVB: ");
111 for (i = 0; i < lvblen; i++) {
112 if (i == lvblen / 2)
113 seq_printf(s, "\n ");
114 seq_printf(s, "%02x ",
115 (unsigned char) res->res_lvbptr[i]);
116 }
117 if (rsb_flag(res, RSB_VALNOTVALID))
118 seq_printf(s, " (INVALID)");
David Teigland892c4462009-01-07 16:48:52 -0600119 rv = seq_printf(s, "\n");
120 if (rv)
121 goto out;
David Teiglande7fd4172006-01-18 09:30:29 +0000122 }
123
David Teigland5de63192006-07-25 13:44:31 -0500124 root_list = !list_empty(&res->res_root_list);
125 recover_list = !list_empty(&res->res_recover_list);
126
127 if (root_list || recover_list) {
David Teigland892c4462009-01-07 16:48:52 -0600128 rv = seq_printf(s, "Recovery: root %d recover %d flags %lx "
129 "count %d\n", root_list, recover_list,
130 res->res_flags, res->res_recover_locks_count);
131 if (rv)
132 goto out;
David Teigland5de63192006-07-25 13:44:31 -0500133 }
134
David Teiglande7fd4172006-01-18 09:30:29 +0000135 /* Print the locks attached to this resource */
136 seq_printf(s, "Granted Queue\n");
David Teigland892c4462009-01-07 16:48:52 -0600137 list_for_each_entry(lkb, &res->res_grantqueue, lkb_statequeue) {
138 rv = print_format1_lock(s, lkb, res);
139 if (rv)
140 goto out;
141 }
David Teiglande7fd4172006-01-18 09:30:29 +0000142
143 seq_printf(s, "Conversion Queue\n");
David Teigland892c4462009-01-07 16:48:52 -0600144 list_for_each_entry(lkb, &res->res_convertqueue, lkb_statequeue) {
145 rv = print_format1_lock(s, lkb, res);
146 if (rv)
147 goto out;
148 }
David Teiglande7fd4172006-01-18 09:30:29 +0000149
150 seq_printf(s, "Waiting Queue\n");
David Teigland892c4462009-01-07 16:48:52 -0600151 list_for_each_entry(lkb, &res->res_waitqueue, lkb_statequeue) {
152 rv = print_format1_lock(s, lkb, res);
153 if (rv)
154 goto out;
155 }
David Teiglande7fd4172006-01-18 09:30:29 +0000156
David Teigland5de63192006-07-25 13:44:31 -0500157 if (list_empty(&res->res_lookup))
158 goto out;
159
160 seq_printf(s, "Lookup Queue\n");
161 list_for_each_entry(lkb, &res->res_lookup, lkb_rsb_lookup) {
David Teigland892c4462009-01-07 16:48:52 -0600162 rv = seq_printf(s, "%08x %s", lkb->lkb_id,
163 print_lockmode(lkb->lkb_rqmode));
David Teigland5de63192006-07-25 13:44:31 -0500164 if (lkb->lkb_wait_type)
165 seq_printf(s, " wait_type: %d", lkb->lkb_wait_type);
David Teigland892c4462009-01-07 16:48:52 -0600166 rv = seq_printf(s, "\n");
David Teigland5de63192006-07-25 13:44:31 -0500167 }
168 out:
David Teigland9dd592d2007-05-29 08:47:04 -0500169 unlock_rsb(res);
David Teigland892c4462009-01-07 16:48:52 -0600170 return rv;
David Teigland9dd592d2007-05-29 08:47:04 -0500171}
172
David Teigland892c4462009-01-07 16:48:52 -0600173static int print_format2_lock(struct seq_file *s, struct dlm_lkb *lkb,
174 struct dlm_rsb *r)
David Teigland9dd592d2007-05-29 08:47:04 -0500175{
David Teiglandeeda4182008-12-09 14:12:21 -0600176 u64 xid = 0;
177 u64 us;
David Teigland892c4462009-01-07 16:48:52 -0600178 int rv;
David Teigland9dd592d2007-05-29 08:47:04 -0500179
180 if (lkb->lkb_flags & DLM_IFL_USER) {
David Teiglandd292c0c2008-02-06 23:27:04 -0600181 if (lkb->lkb_ua)
182 xid = lkb->lkb_ua->xid;
David Teigland9dd592d2007-05-29 08:47:04 -0500183 }
184
David Teiglandeeda4182008-12-09 14:12:21 -0600185 /* microseconds since lkb was added to current queue */
186 us = ktime_to_us(ktime_sub(ktime_get(), lkb->lkb_timestamp));
David Teigland9dd592d2007-05-29 08:47:04 -0500187
David Teiglandeeda4182008-12-09 14:12:21 -0600188 /* id nodeid remid pid xid exflags flags sts grmode rqmode time_us
David Teiglandac90a252007-07-06 09:47:08 -0500189 r_nodeid r_len r_name */
David Teigland9dd592d2007-05-29 08:47:04 -0500190
David Teigland892c4462009-01-07 16:48:52 -0600191 rv = seq_printf(s, "%x %d %x %u %llu %x %x %d %d %d %llu %u %d \"%s\"\n",
192 lkb->lkb_id,
193 lkb->lkb_nodeid,
194 lkb->lkb_remid,
195 lkb->lkb_ownpid,
196 (unsigned long long)xid,
197 lkb->lkb_exflags,
198 lkb->lkb_flags,
199 lkb->lkb_status,
200 lkb->lkb_grmode,
201 lkb->lkb_rqmode,
202 (unsigned long long)us,
203 r->res_nodeid,
204 r->res_length,
205 r->res_name);
206 return rv;
David Teigland9dd592d2007-05-29 08:47:04 -0500207}
208
David Teiglandd0225092008-12-16 14:53:23 -0600209static int print_format2(struct dlm_rsb *r, struct seq_file *s)
David Teigland9dd592d2007-05-29 08:47:04 -0500210{
211 struct dlm_lkb *lkb;
David Teigland892c4462009-01-07 16:48:52 -0600212 int rv = 0;
David Teigland9dd592d2007-05-29 08:47:04 -0500213
214 lock_rsb(r);
215
David Teigland892c4462009-01-07 16:48:52 -0600216 list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
217 rv = print_format2_lock(s, lkb, r);
218 if (rv)
219 goto out;
220 }
David Teigland9dd592d2007-05-29 08:47:04 -0500221
David Teigland892c4462009-01-07 16:48:52 -0600222 list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
223 rv = print_format2_lock(s, lkb, r);
224 if (rv)
225 goto out;
226 }
David Teigland9dd592d2007-05-29 08:47:04 -0500227
David Teigland892c4462009-01-07 16:48:52 -0600228 list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue) {
229 rv = print_format2_lock(s, lkb, r);
230 if (rv)
231 goto out;
232 }
233 out:
David Teiglandd0225092008-12-16 14:53:23 -0600234 unlock_rsb(r);
David Teigland892c4462009-01-07 16:48:52 -0600235 return rv;
David Teiglandd0225092008-12-16 14:53:23 -0600236}
237
David Teigland892c4462009-01-07 16:48:52 -0600238static int print_format3_lock(struct seq_file *s, struct dlm_lkb *lkb,
239 int rsb_lookup)
David Teiglandd0225092008-12-16 14:53:23 -0600240{
241 u64 xid = 0;
David Teigland892c4462009-01-07 16:48:52 -0600242 int rv;
David Teiglandd0225092008-12-16 14:53:23 -0600243
244 if (lkb->lkb_flags & DLM_IFL_USER) {
245 if (lkb->lkb_ua)
246 xid = lkb->lkb_ua->xid;
247 }
248
David Teigland892c4462009-01-07 16:48:52 -0600249 rv = seq_printf(s, "lkb %x %d %x %u %llu %x %x %d %d %d %d %d %d %u %llu %llu\n",
250 lkb->lkb_id,
251 lkb->lkb_nodeid,
252 lkb->lkb_remid,
253 lkb->lkb_ownpid,
254 (unsigned long long)xid,
255 lkb->lkb_exflags,
256 lkb->lkb_flags,
257 lkb->lkb_status,
258 lkb->lkb_grmode,
259 lkb->lkb_rqmode,
David Teigland8304d6f2011-02-21 14:58:21 -0600260 lkb->lkb_last_bast.mode,
David Teigland892c4462009-01-07 16:48:52 -0600261 rsb_lookup,
262 lkb->lkb_wait_type,
263 lkb->lkb_lvbseq,
264 (unsigned long long)ktime_to_ns(lkb->lkb_timestamp),
David Teigland8304d6f2011-02-21 14:58:21 -0600265 (unsigned long long)ktime_to_ns(lkb->lkb_last_bast_time));
David Teigland892c4462009-01-07 16:48:52 -0600266 return rv;
David Teiglandd0225092008-12-16 14:53:23 -0600267}
268
269static int print_format3(struct dlm_rsb *r, struct seq_file *s)
270{
271 struct dlm_lkb *lkb;
272 int i, lvblen = r->res_ls->ls_lvblen;
273 int print_name = 1;
David Teigland892c4462009-01-07 16:48:52 -0600274 int rv;
David Teiglandd0225092008-12-16 14:53:23 -0600275
276 lock_rsb(r);
277
David Teigland892c4462009-01-07 16:48:52 -0600278 rv = seq_printf(s, "rsb %p %d %x %lx %d %d %u %d ",
279 r,
280 r->res_nodeid,
281 r->res_first_lkid,
282 r->res_flags,
283 !list_empty(&r->res_root_list),
284 !list_empty(&r->res_recover_list),
285 r->res_recover_locks_count,
286 r->res_length);
287 if (rv)
288 goto out;
David Teiglandd0225092008-12-16 14:53:23 -0600289
290 for (i = 0; i < r->res_length; i++) {
291 if (!isascii(r->res_name[i]) || !isprint(r->res_name[i]))
292 print_name = 0;
293 }
294
295 seq_printf(s, "%s", print_name ? "str " : "hex");
296
297 for (i = 0; i < r->res_length; i++) {
298 if (print_name)
299 seq_printf(s, "%c", r->res_name[i]);
300 else
301 seq_printf(s, " %02x", (unsigned char)r->res_name[i]);
302 }
David Teigland892c4462009-01-07 16:48:52 -0600303 rv = seq_printf(s, "\n");
304 if (rv)
305 goto out;
David Teiglandd0225092008-12-16 14:53:23 -0600306
307 if (!r->res_lvbptr)
308 goto do_locks;
309
310 seq_printf(s, "lvb %u %d", r->res_lvbseq, lvblen);
311
312 for (i = 0; i < lvblen; i++)
313 seq_printf(s, " %02x", (unsigned char)r->res_lvbptr[i]);
David Teigland892c4462009-01-07 16:48:52 -0600314 rv = seq_printf(s, "\n");
315 if (rv)
316 goto out;
David Teiglandd0225092008-12-16 14:53:23 -0600317
318 do_locks:
David Teigland892c4462009-01-07 16:48:52 -0600319 list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
320 rv = print_format3_lock(s, lkb, 0);
321 if (rv)
322 goto out;
323 }
David Teiglandd0225092008-12-16 14:53:23 -0600324
David Teigland892c4462009-01-07 16:48:52 -0600325 list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
326 rv = print_format3_lock(s, lkb, 0);
327 if (rv)
328 goto out;
329 }
David Teiglandd0225092008-12-16 14:53:23 -0600330
David Teigland892c4462009-01-07 16:48:52 -0600331 list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue) {
332 rv = print_format3_lock(s, lkb, 0);
333 if (rv)
334 goto out;
335 }
David Teiglandd0225092008-12-16 14:53:23 -0600336
David Teigland892c4462009-01-07 16:48:52 -0600337 list_for_each_entry(lkb, &r->res_lookup, lkb_rsb_lookup) {
338 rv = print_format3_lock(s, lkb, 1);
339 if (rv)
340 goto out;
341 }
342 out:
David Teigland9dd592d2007-05-29 08:47:04 -0500343 unlock_rsb(r);
David Teigland892c4462009-01-07 16:48:52 -0600344 return rv;
David Teiglande7fd4172006-01-18 09:30:29 +0000345}
346
David Teigland892c4462009-01-07 16:48:52 -0600347struct rsbtbl_iter {
348 struct dlm_rsb *rsb;
349 unsigned bucket;
350 int format;
351 int header;
352};
353
354/* seq_printf returns -1 if the buffer is full, and 0 otherwise.
355 If the buffer is full, seq_printf can be called again, but it
356 does nothing and just returns -1. So, the these printing routines
357 periodically check the return value to avoid wasting too much time
358 trying to print to a full buffer. */
359
360static int table_seq_show(struct seq_file *seq, void *iter_ptr)
David Teiglande7fd4172006-01-18 09:30:29 +0000361{
David Teigland892c4462009-01-07 16:48:52 -0600362 struct rsbtbl_iter *ri = iter_ptr;
363 int rv = 0;
David Teiglande7fd4172006-01-18 09:30:29 +0000364
David Teiglandd0225092008-12-16 14:53:23 -0600365 switch (ri->format) {
366 case 1:
David Teigland892c4462009-01-07 16:48:52 -0600367 rv = print_format1(ri->rsb, seq);
David Teiglandd0225092008-12-16 14:53:23 -0600368 break;
369 case 2:
David Teigland9dd592d2007-05-29 08:47:04 -0500370 if (ri->header) {
David Teigland892c4462009-01-07 16:48:52 -0600371 seq_printf(seq, "id nodeid remid pid xid exflags "
372 "flags sts grmode rqmode time_ms "
373 "r_nodeid r_len r_name\n");
David Teigland9dd592d2007-05-29 08:47:04 -0500374 ri->header = 0;
375 }
David Teigland892c4462009-01-07 16:48:52 -0600376 rv = print_format2(ri->rsb, seq);
David Teiglandd0225092008-12-16 14:53:23 -0600377 break;
378 case 3:
379 if (ri->header) {
David Teigland892c4462009-01-07 16:48:52 -0600380 seq_printf(seq, "version rsb 1.1 lvb 1.1 lkb 1.1\n");
David Teiglandd0225092008-12-16 14:53:23 -0600381 ri->header = 0;
382 }
David Teigland892c4462009-01-07 16:48:52 -0600383 rv = print_format3(ri->rsb, seq);
David Teiglandd0225092008-12-16 14:53:23 -0600384 break;
David Teigland9dd592d2007-05-29 08:47:04 -0500385 }
David Teiglande7fd4172006-01-18 09:30:29 +0000386
David Teigland892c4462009-01-07 16:48:52 -0600387 return rv;
David Teiglande7fd4172006-01-18 09:30:29 +0000388}
389
James Morris88e9d342009-09-22 16:43:43 -0700390static const struct seq_operations format1_seq_ops;
391static const struct seq_operations format2_seq_ops;
392static const struct seq_operations format3_seq_ops;
David Teigland892c4462009-01-07 16:48:52 -0600393
394static void *table_seq_start(struct seq_file *seq, loff_t *pos)
395{
Bob Peterson9beb3bf2011-10-26 15:24:55 -0500396 struct rb_node *node;
David Teigland892c4462009-01-07 16:48:52 -0600397 struct dlm_ls *ls = seq->private;
398 struct rsbtbl_iter *ri;
399 struct dlm_rsb *r;
400 loff_t n = *pos;
401 unsigned bucket, entry;
402
403 bucket = n >> 32;
404 entry = n & ((1LL << 32) - 1);
405
406 if (bucket >= ls->ls_rsbtbl_size)
407 return NULL;
408
David Teigland573c24c2009-11-30 16:34:43 -0600409 ri = kzalloc(sizeof(struct rsbtbl_iter), GFP_NOFS);
David Teigland892c4462009-01-07 16:48:52 -0600410 if (!ri)
411 return NULL;
412 if (n == 0)
413 ri->header = 1;
414 if (seq->op == &format1_seq_ops)
415 ri->format = 1;
416 if (seq->op == &format2_seq_ops)
417 ri->format = 2;
418 if (seq->op == &format3_seq_ops)
419 ri->format = 3;
420
David Teiglandc7be7612009-01-07 16:50:41 -0600421 spin_lock(&ls->ls_rsbtbl[bucket].lock);
Bob Peterson9beb3bf2011-10-26 15:24:55 -0500422 if (!RB_EMPTY_ROOT(&ls->ls_rsbtbl[bucket].keep)) {
423 for (node = rb_first(&ls->ls_rsbtbl[bucket].keep); node;
424 node = rb_next(node)) {
425 r = rb_entry(node, struct dlm_rsb, res_hashnode);
David Teigland892c4462009-01-07 16:48:52 -0600426 if (!entry--) {
427 dlm_hold_rsb(r);
428 ri->rsb = r;
429 ri->bucket = bucket;
David Teiglandc7be7612009-01-07 16:50:41 -0600430 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
David Teigland892c4462009-01-07 16:48:52 -0600431 return ri;
432 }
433 }
434 }
David Teiglandc7be7612009-01-07 16:50:41 -0600435 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
David Teigland892c4462009-01-07 16:48:52 -0600436
437 /*
438 * move to the first rsb in the next non-empty bucket
439 */
440
441 /* zero the entry */
442 n &= ~((1LL << 32) - 1);
443
444 while (1) {
445 bucket++;
446 n += 1LL << 32;
447
448 if (bucket >= ls->ls_rsbtbl_size) {
449 kfree(ri);
450 return NULL;
451 }
452
David Teiglandc7be7612009-01-07 16:50:41 -0600453 spin_lock(&ls->ls_rsbtbl[bucket].lock);
Bob Peterson9beb3bf2011-10-26 15:24:55 -0500454 if (!RB_EMPTY_ROOT(&ls->ls_rsbtbl[bucket].keep)) {
455 node = rb_first(&ls->ls_rsbtbl[bucket].keep);
456 r = rb_entry(node, struct dlm_rsb, res_hashnode);
David Teigland892c4462009-01-07 16:48:52 -0600457 dlm_hold_rsb(r);
458 ri->rsb = r;
459 ri->bucket = bucket;
David Teiglandc7be7612009-01-07 16:50:41 -0600460 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
David Teigland892c4462009-01-07 16:48:52 -0600461 *pos = n;
462 return ri;
463 }
David Teiglandc7be7612009-01-07 16:50:41 -0600464 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
David Teigland892c4462009-01-07 16:48:52 -0600465 }
466}
467
468static void *table_seq_next(struct seq_file *seq, void *iter_ptr, loff_t *pos)
469{
470 struct dlm_ls *ls = seq->private;
471 struct rsbtbl_iter *ri = iter_ptr;
Bob Peterson9beb3bf2011-10-26 15:24:55 -0500472 struct rb_node *next;
David Teigland892c4462009-01-07 16:48:52 -0600473 struct dlm_rsb *r, *rp;
474 loff_t n = *pos;
475 unsigned bucket;
476
477 bucket = n >> 32;
478
479 /*
480 * move to the next rsb in the same bucket
481 */
482
David Teiglandc7be7612009-01-07 16:50:41 -0600483 spin_lock(&ls->ls_rsbtbl[bucket].lock);
David Teigland892c4462009-01-07 16:48:52 -0600484 rp = ri->rsb;
Bob Peterson9beb3bf2011-10-26 15:24:55 -0500485 next = rb_next(&rp->res_hashnode);
David Teigland892c4462009-01-07 16:48:52 -0600486
Bob Peterson9beb3bf2011-10-26 15:24:55 -0500487 if (next) {
488 r = rb_entry(next, struct dlm_rsb, res_hashnode);
David Teigland892c4462009-01-07 16:48:52 -0600489 dlm_hold_rsb(r);
490 ri->rsb = r;
David Teiglandc7be7612009-01-07 16:50:41 -0600491 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
David Teigland892c4462009-01-07 16:48:52 -0600492 dlm_put_rsb(rp);
493 ++*pos;
494 return ri;
495 }
David Teiglandc7be7612009-01-07 16:50:41 -0600496 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
David Teigland892c4462009-01-07 16:48:52 -0600497 dlm_put_rsb(rp);
498
499 /*
500 * move to the first rsb in the next non-empty bucket
501 */
502
503 /* zero the entry */
504 n &= ~((1LL << 32) - 1);
505
506 while (1) {
507 bucket++;
508 n += 1LL << 32;
509
510 if (bucket >= ls->ls_rsbtbl_size) {
511 kfree(ri);
512 return NULL;
513 }
514
David Teiglandc7be7612009-01-07 16:50:41 -0600515 spin_lock(&ls->ls_rsbtbl[bucket].lock);
Bob Peterson9beb3bf2011-10-26 15:24:55 -0500516 if (!RB_EMPTY_ROOT(&ls->ls_rsbtbl[bucket].keep)) {
517 next = rb_first(&ls->ls_rsbtbl[bucket].keep);
518 r = rb_entry(next, struct dlm_rsb, res_hashnode);
David Teigland892c4462009-01-07 16:48:52 -0600519 dlm_hold_rsb(r);
520 ri->rsb = r;
521 ri->bucket = bucket;
David Teiglandc7be7612009-01-07 16:50:41 -0600522 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
David Teigland892c4462009-01-07 16:48:52 -0600523 *pos = n;
524 return ri;
525 }
David Teiglandc7be7612009-01-07 16:50:41 -0600526 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
David Teigland892c4462009-01-07 16:48:52 -0600527 }
528}
529
530static void table_seq_stop(struct seq_file *seq, void *iter_ptr)
531{
532 struct rsbtbl_iter *ri = iter_ptr;
533
534 if (ri) {
535 dlm_put_rsb(ri->rsb);
536 kfree(ri);
537 }
538}
539
James Morris88e9d342009-09-22 16:43:43 -0700540static const struct seq_operations format1_seq_ops = {
David Teigland892c4462009-01-07 16:48:52 -0600541 .start = table_seq_start,
542 .next = table_seq_next,
543 .stop = table_seq_stop,
544 .show = table_seq_show,
David Teiglande7fd4172006-01-18 09:30:29 +0000545};
546
James Morris88e9d342009-09-22 16:43:43 -0700547static const struct seq_operations format2_seq_ops = {
David Teigland892c4462009-01-07 16:48:52 -0600548 .start = table_seq_start,
549 .next = table_seq_next,
550 .stop = table_seq_stop,
551 .show = table_seq_show,
552};
553
James Morris88e9d342009-09-22 16:43:43 -0700554static const struct seq_operations format3_seq_ops = {
David Teigland892c4462009-01-07 16:48:52 -0600555 .start = table_seq_start,
556 .next = table_seq_next,
557 .stop = table_seq_stop,
558 .show = table_seq_show,
559};
560
561static const struct file_operations format1_fops;
562static const struct file_operations format2_fops;
563static const struct file_operations format3_fops;
564
565static int table_open(struct inode *inode, struct file *file)
David Teiglande7fd4172006-01-18 09:30:29 +0000566{
567 struct seq_file *seq;
David Teigland892c4462009-01-07 16:48:52 -0600568 int ret = -1;
David Teiglande7fd4172006-01-18 09:30:29 +0000569
David Teigland892c4462009-01-07 16:48:52 -0600570 if (file->f_op == &format1_fops)
571 ret = seq_open(file, &format1_seq_ops);
572 else if (file->f_op == &format2_fops)
573 ret = seq_open(file, &format2_seq_ops);
574 else if (file->f_op == &format3_fops)
575 ret = seq_open(file, &format3_seq_ops);
576
David Teiglande7fd4172006-01-18 09:30:29 +0000577 if (ret)
578 return ret;
579
580 seq = file->private_data;
David Teigland892c4462009-01-07 16:48:52 -0600581 seq->private = inode->i_private; /* the dlm_ls */
David Teiglande7fd4172006-01-18 09:30:29 +0000582 return 0;
583}
584
David Teigland892c4462009-01-07 16:48:52 -0600585static const struct file_operations format1_fops = {
David Teiglande7fd4172006-01-18 09:30:29 +0000586 .owner = THIS_MODULE,
David Teigland892c4462009-01-07 16:48:52 -0600587 .open = table_open,
David Teiglande7fd4172006-01-18 09:30:29 +0000588 .read = seq_read,
589 .llseek = seq_lseek,
590 .release = seq_release
591};
592
David Teigland892c4462009-01-07 16:48:52 -0600593static const struct file_operations format2_fops = {
David Teigland9dd592d2007-05-29 08:47:04 -0500594 .owner = THIS_MODULE,
David Teigland892c4462009-01-07 16:48:52 -0600595 .open = table_open,
David Teigland9dd592d2007-05-29 08:47:04 -0500596 .read = seq_read,
597 .llseek = seq_lseek,
598 .release = seq_release
599};
600
David Teigland892c4462009-01-07 16:48:52 -0600601static const struct file_operations format3_fops = {
David Teiglandd0225092008-12-16 14:53:23 -0600602 .owner = THIS_MODULE,
David Teigland892c4462009-01-07 16:48:52 -0600603 .open = table_open,
David Teiglandd0225092008-12-16 14:53:23 -0600604 .read = seq_read,
605 .llseek = seq_lseek,
606 .release = seq_release
607};
608
609/*
David Teigland5de63192006-07-25 13:44:31 -0500610 * dump lkb's on the ls_waiters list
611 */
David Teigland5de63192006-07-25 13:44:31 -0500612static ssize_t waiters_read(struct file *file, char __user *userbuf,
613 size_t count, loff_t *ppos)
614{
615 struct dlm_ls *ls = file->private_data;
616 struct dlm_lkb *lkb;
David Teigland06442442006-08-08 11:31:30 -0500617 size_t len = DLM_DEBUG_BUF_LEN, pos = 0, ret, rv;
David Teigland5de63192006-07-25 13:44:31 -0500618
619 mutex_lock(&debug_buf_lock);
620 mutex_lock(&ls->ls_waiters_mutex);
621 memset(debug_buf, 0, sizeof(debug_buf));
622
623 list_for_each_entry(lkb, &ls->ls_waiters, lkb_wait_reply) {
David Teigland06442442006-08-08 11:31:30 -0500624 ret = snprintf(debug_buf + pos, len - pos, "%x %d %d %s\n",
625 lkb->lkb_id, lkb->lkb_wait_type,
626 lkb->lkb_nodeid, lkb->lkb_resource->res_name);
627 if (ret >= len - pos)
628 break;
629 pos += ret;
David Teigland5de63192006-07-25 13:44:31 -0500630 }
631 mutex_unlock(&ls->ls_waiters_mutex);
632
633 rv = simple_read_from_buffer(userbuf, count, ppos, debug_buf, pos);
634 mutex_unlock(&debug_buf_lock);
635 return rv;
636}
637
Arjan van de Ven00977a52007-02-12 00:55:34 -0800638static const struct file_operations waiters_fops = {
David Teigland5de63192006-07-25 13:44:31 -0500639 .owner = THIS_MODULE,
Stephen Boyd234e3402012-04-05 14:25:11 -0700640 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200641 .read = waiters_read,
642 .llseek = default_llseek,
David Teigland5de63192006-07-25 13:44:31 -0500643};
644
David Teiglandd0225092008-12-16 14:53:23 -0600645void dlm_delete_debug_file(struct dlm_ls *ls)
646{
647 if (ls->ls_debug_rsb_dentry)
648 debugfs_remove(ls->ls_debug_rsb_dentry);
649 if (ls->ls_debug_waiters_dentry)
650 debugfs_remove(ls->ls_debug_waiters_dentry);
651 if (ls->ls_debug_locks_dentry)
652 debugfs_remove(ls->ls_debug_locks_dentry);
653 if (ls->ls_debug_all_dentry)
654 debugfs_remove(ls->ls_debug_all_dentry);
655}
656
David Teiglande7fd4172006-01-18 09:30:29 +0000657int dlm_create_debug_file(struct dlm_ls *ls)
658{
David Teigland5de63192006-07-25 13:44:31 -0500659 char name[DLM_LOCKSPACE_LEN+8];
660
David Teiglandd0225092008-12-16 14:53:23 -0600661 /* format 1 */
662
David Teigland5de63192006-07-25 13:44:31 -0500663 ls->ls_debug_rsb_dentry = debugfs_create_file(ls->ls_name,
664 S_IFREG | S_IRUGO,
665 dlm_root,
666 ls,
David Teigland892c4462009-01-07 16:48:52 -0600667 &format1_fops);
David Teigland20abf972006-07-26 08:29:06 -0500668 if (!ls->ls_debug_rsb_dentry)
David Teiglandd0225092008-12-16 14:53:23 -0600669 goto fail;
David Teigland5de63192006-07-25 13:44:31 -0500670
David Teiglandd0225092008-12-16 14:53:23 -0600671 /* format 2 */
David Teigland5de63192006-07-25 13:44:31 -0500672
David Teigland9dd592d2007-05-29 08:47:04 -0500673 memset(name, 0, sizeof(name));
David Teiglandac90a252007-07-06 09:47:08 -0500674 snprintf(name, DLM_LOCKSPACE_LEN+8, "%s_locks", ls->ls_name);
David Teigland9dd592d2007-05-29 08:47:04 -0500675
David Teiglandac90a252007-07-06 09:47:08 -0500676 ls->ls_debug_locks_dentry = debugfs_create_file(name,
677 S_IFREG | S_IRUGO,
678 dlm_root,
679 ls,
David Teigland892c4462009-01-07 16:48:52 -0600680 &format2_fops);
David Teiglandd0225092008-12-16 14:53:23 -0600681 if (!ls->ls_debug_locks_dentry)
682 goto fail;
683
684 /* format 3 */
685
686 memset(name, 0, sizeof(name));
687 snprintf(name, DLM_LOCKSPACE_LEN+8, "%s_all", ls->ls_name);
688
689 ls->ls_debug_all_dentry = debugfs_create_file(name,
690 S_IFREG | S_IRUGO,
691 dlm_root,
692 ls,
David Teigland892c4462009-01-07 16:48:52 -0600693 &format3_fops);
David Teiglandd0225092008-12-16 14:53:23 -0600694 if (!ls->ls_debug_all_dentry)
695 goto fail;
696
697 memset(name, 0, sizeof(name));
698 snprintf(name, DLM_LOCKSPACE_LEN+8, "%s_waiters", ls->ls_name);
699
700 ls->ls_debug_waiters_dentry = debugfs_create_file(name,
701 S_IFREG | S_IRUGO,
702 dlm_root,
703 ls,
704 &waiters_fops);
705 if (!ls->ls_debug_waiters_dentry)
706 goto fail;
David Teigland9dd592d2007-05-29 08:47:04 -0500707
David Teigland5de63192006-07-25 13:44:31 -0500708 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +0000709
David Teiglandd0225092008-12-16 14:53:23 -0600710 fail:
711 dlm_delete_debug_file(ls);
712 return -ENOMEM;
David Teiglande7fd4172006-01-18 09:30:29 +0000713}
714
Denis Cheng30727172008-02-02 01:53:46 +0800715int __init dlm_register_debugfs(void)
David Teiglande7fd4172006-01-18 09:30:29 +0000716{
David Teigland5de63192006-07-25 13:44:31 -0500717 mutex_init(&debug_buf_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000718 dlm_root = debugfs_create_dir("dlm", NULL);
719 return dlm_root ? 0 : -ENOMEM;
720}
721
722void dlm_unregister_debugfs(void)
723{
724 debugfs_remove(dlm_root);
725}
726