blob: eea64912c9c0a6a1e69e0b041b8404e611bf1e81 [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
Joe Perchesd6d906b2014-09-29 16:08:23 -070051static void 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
Joe Perchesd6d906b2014-09-29 16:08:23 -070071 seq_puts(s, "\n");
David Teiglande7fd4172006-01-18 09:30:29 +000072}
73
Joe Perchesd6d906b2014-09-29 16:08:23 -070074static void 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 Teiglande7fd4172006-01-18 09:30:29 +000078
David Teigland9dd592d2007-05-29 08:47:04 -050079 lock_rsb(res);
80
Joe Perchesd6d906b2014-09-29 16:08:23 -070081 seq_printf(s, "\nResource %p Name (len=%d) \"", res, res->res_length);
David Teigland892c4462009-01-07 16:48:52 -060082
David Teiglande7fd4172006-01-18 09:30:29 +000083 for (i = 0; i < res->res_length; i++) {
84 if (isprint(res->res_name[i]))
85 seq_printf(s, "%c", res->res_name[i]);
86 else
87 seq_printf(s, "%c", '.');
88 }
David Teigland892c4462009-01-07 16:48:52 -060089
David Teiglande7fd4172006-01-18 09:30:29 +000090 if (res->res_nodeid > 0)
Joe Perchesd6d906b2014-09-29 16:08:23 -070091 seq_printf(s, "\"\nLocal Copy, Master is node %d\n",
92 res->res_nodeid);
David Teiglande7fd4172006-01-18 09:30:29 +000093 else if (res->res_nodeid == 0)
Joe Perchesd6d906b2014-09-29 16:08:23 -070094 seq_puts(s, "\"\nMaster Copy\n");
David Teiglande7fd4172006-01-18 09:30:29 +000095 else if (res->res_nodeid == -1)
Joe Perchesd6d906b2014-09-29 16:08:23 -070096 seq_printf(s, "\"\nLooking up master (lkid %x)\n",
97 res->res_first_lkid);
David Teiglande7fd4172006-01-18 09:30:29 +000098 else
Joe Perchesd6d906b2014-09-29 16:08:23 -070099 seq_printf(s, "\"\nInvalid master %d\n", res->res_nodeid);
100 if (seq_has_overflowed(s))
David Teigland892c4462009-01-07 16:48:52 -0600101 goto out;
David Teiglande7fd4172006-01-18 09:30:29 +0000102
103 /* Print the LVB: */
104 if (res->res_lvbptr) {
Fabian Frederickc1d45182014-06-06 14:38:25 -0700105 seq_puts(s, "LVB: ");
David Teiglande7fd4172006-01-18 09:30:29 +0000106 for (i = 0; i < lvblen; i++) {
107 if (i == lvblen / 2)
Fabian Frederickc1d45182014-06-06 14:38:25 -0700108 seq_puts(s, "\n ");
David Teiglande7fd4172006-01-18 09:30:29 +0000109 seq_printf(s, "%02x ",
110 (unsigned char) res->res_lvbptr[i]);
111 }
112 if (rsb_flag(res, RSB_VALNOTVALID))
Fabian Frederickc1d45182014-06-06 14:38:25 -0700113 seq_puts(s, " (INVALID)");
Joe Perchesd6d906b2014-09-29 16:08:23 -0700114 seq_puts(s, "\n");
115 if (seq_has_overflowed(s))
David Teigland892c4462009-01-07 16:48:52 -0600116 goto out;
David Teiglande7fd4172006-01-18 09:30:29 +0000117 }
118
David Teigland5de63192006-07-25 13:44:31 -0500119 root_list = !list_empty(&res->res_root_list);
120 recover_list = !list_empty(&res->res_recover_list);
121
122 if (root_list || recover_list) {
Joe Perchesd6d906b2014-09-29 16:08:23 -0700123 seq_printf(s, "Recovery: root %d recover %d flags %lx count %d\n",
124 root_list, recover_list,
125 res->res_flags, res->res_recover_locks_count);
David Teigland5de63192006-07-25 13:44:31 -0500126 }
127
David Teiglande7fd4172006-01-18 09:30:29 +0000128 /* Print the locks attached to this resource */
Fabian Frederickc1d45182014-06-06 14:38:25 -0700129 seq_puts(s, "Granted Queue\n");
David Teigland892c4462009-01-07 16:48:52 -0600130 list_for_each_entry(lkb, &res->res_grantqueue, lkb_statequeue) {
Joe Perchesd6d906b2014-09-29 16:08:23 -0700131 print_format1_lock(s, lkb, res);
132 if (seq_has_overflowed(s))
David Teigland892c4462009-01-07 16:48:52 -0600133 goto out;
134 }
David Teiglande7fd4172006-01-18 09:30:29 +0000135
Fabian Frederickc1d45182014-06-06 14:38:25 -0700136 seq_puts(s, "Conversion Queue\n");
David Teigland892c4462009-01-07 16:48:52 -0600137 list_for_each_entry(lkb, &res->res_convertqueue, lkb_statequeue) {
Joe Perchesd6d906b2014-09-29 16:08:23 -0700138 print_format1_lock(s, lkb, res);
139 if (seq_has_overflowed(s))
David Teigland892c4462009-01-07 16:48:52 -0600140 goto out;
141 }
David Teiglande7fd4172006-01-18 09:30:29 +0000142
Fabian Frederickc1d45182014-06-06 14:38:25 -0700143 seq_puts(s, "Waiting Queue\n");
David Teigland892c4462009-01-07 16:48:52 -0600144 list_for_each_entry(lkb, &res->res_waitqueue, lkb_statequeue) {
Joe Perchesd6d906b2014-09-29 16:08:23 -0700145 print_format1_lock(s, lkb, res);
146 if (seq_has_overflowed(s))
David Teigland892c4462009-01-07 16:48:52 -0600147 goto out;
148 }
David Teiglande7fd4172006-01-18 09:30:29 +0000149
David Teigland5de63192006-07-25 13:44:31 -0500150 if (list_empty(&res->res_lookup))
151 goto out;
152
Fabian Frederickc1d45182014-06-06 14:38:25 -0700153 seq_puts(s, "Lookup Queue\n");
David Teigland5de63192006-07-25 13:44:31 -0500154 list_for_each_entry(lkb, &res->res_lookup, lkb_rsb_lookup) {
Joe Perchesd6d906b2014-09-29 16:08:23 -0700155 seq_printf(s, "%08x %s",
156 lkb->lkb_id, print_lockmode(lkb->lkb_rqmode));
David Teigland5de63192006-07-25 13:44:31 -0500157 if (lkb->lkb_wait_type)
158 seq_printf(s, " wait_type: %d", lkb->lkb_wait_type);
Joe Perchesd6d906b2014-09-29 16:08:23 -0700159 seq_puts(s, "\n");
160 if (seq_has_overflowed(s))
161 goto out;
David Teigland5de63192006-07-25 13:44:31 -0500162 }
163 out:
David Teigland9dd592d2007-05-29 08:47:04 -0500164 unlock_rsb(res);
David Teigland9dd592d2007-05-29 08:47:04 -0500165}
166
Joe Perchesd6d906b2014-09-29 16:08:23 -0700167static void print_format2_lock(struct seq_file *s, struct dlm_lkb *lkb,
168 struct dlm_rsb *r)
David Teigland9dd592d2007-05-29 08:47:04 -0500169{
David Teiglandeeda4182008-12-09 14:12:21 -0600170 u64 xid = 0;
171 u64 us;
David Teigland9dd592d2007-05-29 08:47:04 -0500172
173 if (lkb->lkb_flags & DLM_IFL_USER) {
David Teiglandd292c0c2008-02-06 23:27:04 -0600174 if (lkb->lkb_ua)
175 xid = lkb->lkb_ua->xid;
David Teigland9dd592d2007-05-29 08:47:04 -0500176 }
177
David Teiglandeeda4182008-12-09 14:12:21 -0600178 /* microseconds since lkb was added to current queue */
179 us = ktime_to_us(ktime_sub(ktime_get(), lkb->lkb_timestamp));
David Teigland9dd592d2007-05-29 08:47:04 -0500180
David Teiglandeeda4182008-12-09 14:12:21 -0600181 /* id nodeid remid pid xid exflags flags sts grmode rqmode time_us
David Teiglandac90a252007-07-06 09:47:08 -0500182 r_nodeid r_len r_name */
David Teigland9dd592d2007-05-29 08:47:04 -0500183
Joe Perchesd6d906b2014-09-29 16:08:23 -0700184 seq_printf(s, "%x %d %x %u %llu %x %x %d %d %d %llu %u %d \"%s\"\n",
185 lkb->lkb_id,
186 lkb->lkb_nodeid,
187 lkb->lkb_remid,
188 lkb->lkb_ownpid,
189 (unsigned long long)xid,
190 lkb->lkb_exflags,
191 lkb->lkb_flags,
192 lkb->lkb_status,
193 lkb->lkb_grmode,
194 lkb->lkb_rqmode,
195 (unsigned long long)us,
196 r->res_nodeid,
197 r->res_length,
198 r->res_name);
David Teigland9dd592d2007-05-29 08:47:04 -0500199}
200
Joe Perchesd6d906b2014-09-29 16:08:23 -0700201static void print_format2(struct dlm_rsb *r, struct seq_file *s)
David Teigland9dd592d2007-05-29 08:47:04 -0500202{
203 struct dlm_lkb *lkb;
204
205 lock_rsb(r);
206
David Teigland892c4462009-01-07 16:48:52 -0600207 list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
Joe Perchesd6d906b2014-09-29 16:08:23 -0700208 print_format2_lock(s, lkb, r);
209 if (seq_has_overflowed(s))
David Teigland892c4462009-01-07 16:48:52 -0600210 goto out;
211 }
David Teigland9dd592d2007-05-29 08:47:04 -0500212
David Teigland892c4462009-01-07 16:48:52 -0600213 list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
Joe Perchesd6d906b2014-09-29 16:08:23 -0700214 print_format2_lock(s, lkb, r);
215 if (seq_has_overflowed(s))
David Teigland892c4462009-01-07 16:48:52 -0600216 goto out;
217 }
David Teigland9dd592d2007-05-29 08:47:04 -0500218
David Teigland892c4462009-01-07 16:48:52 -0600219 list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue) {
Joe Perchesd6d906b2014-09-29 16:08:23 -0700220 print_format2_lock(s, lkb, r);
221 if (seq_has_overflowed(s))
David Teigland892c4462009-01-07 16:48:52 -0600222 goto out;
223 }
224 out:
David Teiglandd0225092008-12-16 14:53:23 -0600225 unlock_rsb(r);
David Teiglandd0225092008-12-16 14:53:23 -0600226}
227
Joe Perchesd6d906b2014-09-29 16:08:23 -0700228static void print_format3_lock(struct seq_file *s, struct dlm_lkb *lkb,
David Teigland892c4462009-01-07 16:48:52 -0600229 int rsb_lookup)
David Teiglandd0225092008-12-16 14:53:23 -0600230{
231 u64 xid = 0;
232
233 if (lkb->lkb_flags & DLM_IFL_USER) {
234 if (lkb->lkb_ua)
235 xid = lkb->lkb_ua->xid;
236 }
237
Joe Perchesd6d906b2014-09-29 16:08:23 -0700238 seq_printf(s, "lkb %x %d %x %u %llu %x %x %d %d %d %d %d %d %u %llu %llu\n",
239 lkb->lkb_id,
240 lkb->lkb_nodeid,
241 lkb->lkb_remid,
242 lkb->lkb_ownpid,
243 (unsigned long long)xid,
244 lkb->lkb_exflags,
245 lkb->lkb_flags,
246 lkb->lkb_status,
247 lkb->lkb_grmode,
248 lkb->lkb_rqmode,
249 lkb->lkb_last_bast.mode,
250 rsb_lookup,
251 lkb->lkb_wait_type,
252 lkb->lkb_lvbseq,
253 (unsigned long long)ktime_to_ns(lkb->lkb_timestamp),
254 (unsigned long long)ktime_to_ns(lkb->lkb_last_bast_time));
David Teiglandd0225092008-12-16 14:53:23 -0600255}
256
Joe Perchesd6d906b2014-09-29 16:08:23 -0700257static void print_format3(struct dlm_rsb *r, struct seq_file *s)
David Teiglandd0225092008-12-16 14:53:23 -0600258{
259 struct dlm_lkb *lkb;
260 int i, lvblen = r->res_ls->ls_lvblen;
261 int print_name = 1;
262
263 lock_rsb(r);
264
Joe Perchesd6d906b2014-09-29 16:08:23 -0700265 seq_printf(s, "rsb %p %d %x %lx %d %d %u %d ",
266 r,
267 r->res_nodeid,
268 r->res_first_lkid,
269 r->res_flags,
270 !list_empty(&r->res_root_list),
271 !list_empty(&r->res_recover_list),
272 r->res_recover_locks_count,
273 r->res_length);
274 if (seq_has_overflowed(s))
David Teigland892c4462009-01-07 16:48:52 -0600275 goto out;
David Teiglandd0225092008-12-16 14:53:23 -0600276
277 for (i = 0; i < r->res_length; i++) {
278 if (!isascii(r->res_name[i]) || !isprint(r->res_name[i]))
279 print_name = 0;
280 }
281
Joe Perchesf365ef92014-09-29 16:08:24 -0700282 seq_puts(s, print_name ? "str " : "hex");
David Teiglandd0225092008-12-16 14:53:23 -0600283
284 for (i = 0; i < r->res_length; i++) {
285 if (print_name)
286 seq_printf(s, "%c", r->res_name[i]);
287 else
288 seq_printf(s, " %02x", (unsigned char)r->res_name[i]);
289 }
Joe Perchesd6d906b2014-09-29 16:08:23 -0700290 seq_puts(s, "\n");
291 if (seq_has_overflowed(s))
David Teigland892c4462009-01-07 16:48:52 -0600292 goto out;
David Teiglandd0225092008-12-16 14:53:23 -0600293
294 if (!r->res_lvbptr)
295 goto do_locks;
296
297 seq_printf(s, "lvb %u %d", r->res_lvbseq, lvblen);
298
299 for (i = 0; i < lvblen; i++)
300 seq_printf(s, " %02x", (unsigned char)r->res_lvbptr[i]);
Joe Perchesd6d906b2014-09-29 16:08:23 -0700301 seq_puts(s, "\n");
302 if (seq_has_overflowed(s))
David Teigland892c4462009-01-07 16:48:52 -0600303 goto out;
David Teiglandd0225092008-12-16 14:53:23 -0600304
305 do_locks:
David Teigland892c4462009-01-07 16:48:52 -0600306 list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
Joe Perchesd6d906b2014-09-29 16:08:23 -0700307 print_format3_lock(s, lkb, 0);
308 if (seq_has_overflowed(s))
David Teigland892c4462009-01-07 16:48:52 -0600309 goto out;
310 }
David Teiglandd0225092008-12-16 14:53:23 -0600311
David Teigland892c4462009-01-07 16:48:52 -0600312 list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
Joe Perchesd6d906b2014-09-29 16:08:23 -0700313 print_format3_lock(s, lkb, 0);
314 if (seq_has_overflowed(s))
David Teigland892c4462009-01-07 16:48:52 -0600315 goto out;
316 }
David Teiglandd0225092008-12-16 14:53:23 -0600317
David Teigland892c4462009-01-07 16:48:52 -0600318 list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue) {
Joe Perchesd6d906b2014-09-29 16:08:23 -0700319 print_format3_lock(s, lkb, 0);
320 if (seq_has_overflowed(s))
David Teigland892c4462009-01-07 16:48:52 -0600321 goto out;
322 }
David Teiglandd0225092008-12-16 14:53:23 -0600323
David Teigland892c4462009-01-07 16:48:52 -0600324 list_for_each_entry(lkb, &r->res_lookup, lkb_rsb_lookup) {
Joe Perchesd6d906b2014-09-29 16:08:23 -0700325 print_format3_lock(s, lkb, 1);
326 if (seq_has_overflowed(s))
David Teigland892c4462009-01-07 16:48:52 -0600327 goto out;
328 }
329 out:
David Teigland9dd592d2007-05-29 08:47:04 -0500330 unlock_rsb(r);
David Teiglande7fd4172006-01-18 09:30:29 +0000331}
332
Joe Perchesd6d906b2014-09-29 16:08:23 -0700333static void print_format4(struct dlm_rsb *r, struct seq_file *s)
David Teiglandc04fecb2012-05-10 10:18:07 -0500334{
335 int our_nodeid = dlm_our_nodeid();
336 int print_name = 1;
Joe Perchesd6d906b2014-09-29 16:08:23 -0700337 int i;
David Teiglandc04fecb2012-05-10 10:18:07 -0500338
339 lock_rsb(r);
340
Joe Perchesd6d906b2014-09-29 16:08:23 -0700341 seq_printf(s, "rsb %p %d %d %d %d %lu %lx %d ",
342 r,
343 r->res_nodeid,
344 r->res_master_nodeid,
345 r->res_dir_nodeid,
346 our_nodeid,
347 r->res_toss_time,
348 r->res_flags,
349 r->res_length);
David Teiglandc04fecb2012-05-10 10:18:07 -0500350
351 for (i = 0; i < r->res_length; i++) {
352 if (!isascii(r->res_name[i]) || !isprint(r->res_name[i]))
353 print_name = 0;
354 }
355
Joe Perchesf365ef92014-09-29 16:08:24 -0700356 seq_puts(s, print_name ? "str " : "hex");
David Teiglandc04fecb2012-05-10 10:18:07 -0500357
358 for (i = 0; i < r->res_length; i++) {
359 if (print_name)
360 seq_printf(s, "%c", r->res_name[i]);
361 else
362 seq_printf(s, " %02x", (unsigned char)r->res_name[i]);
363 }
Joe Perchesd6d906b2014-09-29 16:08:23 -0700364 seq_puts(s, "\n");
365
David Teiglandc04fecb2012-05-10 10:18:07 -0500366 unlock_rsb(r);
David Teiglandc04fecb2012-05-10 10:18:07 -0500367}
368
David Teigland892c4462009-01-07 16:48:52 -0600369struct rsbtbl_iter {
370 struct dlm_rsb *rsb;
371 unsigned bucket;
372 int format;
373 int header;
374};
375
Joe Perchesd6d906b2014-09-29 16:08:23 -0700376/*
377 * If the buffer is full, seq_printf can be called again, but it
378 * does nothing. So, the these printing routines periodically check
379 * seq_has_overflowed to avoid wasting too much time trying to print to
380 * a full buffer.
381 */
David Teigland892c4462009-01-07 16:48:52 -0600382
383static int table_seq_show(struct seq_file *seq, void *iter_ptr)
David Teiglande7fd4172006-01-18 09:30:29 +0000384{
David Teigland892c4462009-01-07 16:48:52 -0600385 struct rsbtbl_iter *ri = iter_ptr;
David Teiglande7fd4172006-01-18 09:30:29 +0000386
David Teiglandd0225092008-12-16 14:53:23 -0600387 switch (ri->format) {
388 case 1:
Joe Perchesd6d906b2014-09-29 16:08:23 -0700389 print_format1(ri->rsb, seq);
David Teiglandd0225092008-12-16 14:53:23 -0600390 break;
391 case 2:
David Teigland9dd592d2007-05-29 08:47:04 -0500392 if (ri->header) {
Joe Perchesf365ef92014-09-29 16:08:24 -0700393 seq_puts(seq, "id nodeid remid pid xid exflags flags sts grmode rqmode time_ms r_nodeid r_len r_name\n");
David Teigland9dd592d2007-05-29 08:47:04 -0500394 ri->header = 0;
395 }
Joe Perchesd6d906b2014-09-29 16:08:23 -0700396 print_format2(ri->rsb, seq);
David Teiglandd0225092008-12-16 14:53:23 -0600397 break;
398 case 3:
399 if (ri->header) {
Joe Perchesf365ef92014-09-29 16:08:24 -0700400 seq_puts(seq, "version rsb 1.1 lvb 1.1 lkb 1.1\n");
David Teiglandd0225092008-12-16 14:53:23 -0600401 ri->header = 0;
402 }
Joe Perchesd6d906b2014-09-29 16:08:23 -0700403 print_format3(ri->rsb, seq);
David Teiglandd0225092008-12-16 14:53:23 -0600404 break;
David Teiglandc04fecb2012-05-10 10:18:07 -0500405 case 4:
406 if (ri->header) {
Joe Perchesf365ef92014-09-29 16:08:24 -0700407 seq_puts(seq, "version 4 rsb 2\n");
David Teiglandc04fecb2012-05-10 10:18:07 -0500408 ri->header = 0;
409 }
Joe Perchesd6d906b2014-09-29 16:08:23 -0700410 print_format4(ri->rsb, seq);
David Teiglandc04fecb2012-05-10 10:18:07 -0500411 break;
David Teigland9dd592d2007-05-29 08:47:04 -0500412 }
David Teiglande7fd4172006-01-18 09:30:29 +0000413
Joe Perchesd6d906b2014-09-29 16:08:23 -0700414 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +0000415}
416
James Morris88e9d342009-09-22 16:43:43 -0700417static const struct seq_operations format1_seq_ops;
418static const struct seq_operations format2_seq_ops;
419static const struct seq_operations format3_seq_ops;
David Teiglandc04fecb2012-05-10 10:18:07 -0500420static const struct seq_operations format4_seq_ops;
David Teigland892c4462009-01-07 16:48:52 -0600421
422static void *table_seq_start(struct seq_file *seq, loff_t *pos)
423{
David Teiglandc04fecb2012-05-10 10:18:07 -0500424 struct rb_root *tree;
Bob Peterson9beb3bf2011-10-26 15:24:55 -0500425 struct rb_node *node;
David Teigland892c4462009-01-07 16:48:52 -0600426 struct dlm_ls *ls = seq->private;
427 struct rsbtbl_iter *ri;
428 struct dlm_rsb *r;
429 loff_t n = *pos;
430 unsigned bucket, entry;
David Teiglandc04fecb2012-05-10 10:18:07 -0500431 int toss = (seq->op == &format4_seq_ops);
David Teigland892c4462009-01-07 16:48:52 -0600432
433 bucket = n >> 32;
434 entry = n & ((1LL << 32) - 1);
435
436 if (bucket >= ls->ls_rsbtbl_size)
437 return NULL;
438
David Teigland573c24c2009-11-30 16:34:43 -0600439 ri = kzalloc(sizeof(struct rsbtbl_iter), GFP_NOFS);
David Teigland892c4462009-01-07 16:48:52 -0600440 if (!ri)
441 return NULL;
442 if (n == 0)
443 ri->header = 1;
444 if (seq->op == &format1_seq_ops)
445 ri->format = 1;
446 if (seq->op == &format2_seq_ops)
447 ri->format = 2;
448 if (seq->op == &format3_seq_ops)
449 ri->format = 3;
David Teiglandc04fecb2012-05-10 10:18:07 -0500450 if (seq->op == &format4_seq_ops)
451 ri->format = 4;
452
453 tree = toss ? &ls->ls_rsbtbl[bucket].toss : &ls->ls_rsbtbl[bucket].keep;
David Teigland892c4462009-01-07 16:48:52 -0600454
David Teiglandc7be7612009-01-07 16:50:41 -0600455 spin_lock(&ls->ls_rsbtbl[bucket].lock);
David Teiglandc04fecb2012-05-10 10:18:07 -0500456 if (!RB_EMPTY_ROOT(tree)) {
457 for (node = rb_first(tree); node; node = rb_next(node)) {
Bob Peterson9beb3bf2011-10-26 15:24:55 -0500458 r = rb_entry(node, struct dlm_rsb, res_hashnode);
David Teigland892c4462009-01-07 16:48:52 -0600459 if (!entry--) {
460 dlm_hold_rsb(r);
461 ri->rsb = r;
462 ri->bucket = bucket;
David Teiglandc7be7612009-01-07 16:50:41 -0600463 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
David Teigland892c4462009-01-07 16:48:52 -0600464 return ri;
465 }
466 }
467 }
David Teiglandc7be7612009-01-07 16:50:41 -0600468 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
David Teigland892c4462009-01-07 16:48:52 -0600469
470 /*
471 * move to the first rsb in the next non-empty bucket
472 */
473
474 /* zero the entry */
475 n &= ~((1LL << 32) - 1);
476
477 while (1) {
478 bucket++;
479 n += 1LL << 32;
480
481 if (bucket >= ls->ls_rsbtbl_size) {
482 kfree(ri);
483 return NULL;
484 }
David Teiglandc04fecb2012-05-10 10:18:07 -0500485 tree = toss ? &ls->ls_rsbtbl[bucket].toss : &ls->ls_rsbtbl[bucket].keep;
David Teigland892c4462009-01-07 16:48:52 -0600486
David Teiglandc7be7612009-01-07 16:50:41 -0600487 spin_lock(&ls->ls_rsbtbl[bucket].lock);
David Teiglandc04fecb2012-05-10 10:18:07 -0500488 if (!RB_EMPTY_ROOT(tree)) {
489 node = rb_first(tree);
Bob Peterson9beb3bf2011-10-26 15:24:55 -0500490 r = rb_entry(node, struct dlm_rsb, res_hashnode);
David Teigland892c4462009-01-07 16:48:52 -0600491 dlm_hold_rsb(r);
492 ri->rsb = r;
493 ri->bucket = bucket;
David Teiglandc7be7612009-01-07 16:50:41 -0600494 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
David Teigland892c4462009-01-07 16:48:52 -0600495 *pos = n;
496 return ri;
497 }
David Teiglandc7be7612009-01-07 16:50:41 -0600498 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
David Teigland892c4462009-01-07 16:48:52 -0600499 }
500}
501
502static void *table_seq_next(struct seq_file *seq, void *iter_ptr, loff_t *pos)
503{
504 struct dlm_ls *ls = seq->private;
505 struct rsbtbl_iter *ri = iter_ptr;
David Teiglandc04fecb2012-05-10 10:18:07 -0500506 struct rb_root *tree;
Bob Peterson9beb3bf2011-10-26 15:24:55 -0500507 struct rb_node *next;
David Teigland892c4462009-01-07 16:48:52 -0600508 struct dlm_rsb *r, *rp;
509 loff_t n = *pos;
510 unsigned bucket;
David Teiglandc04fecb2012-05-10 10:18:07 -0500511 int toss = (seq->op == &format4_seq_ops);
David Teigland892c4462009-01-07 16:48:52 -0600512
513 bucket = n >> 32;
514
515 /*
516 * move to the next rsb in the same bucket
517 */
518
David Teiglandc7be7612009-01-07 16:50:41 -0600519 spin_lock(&ls->ls_rsbtbl[bucket].lock);
David Teigland892c4462009-01-07 16:48:52 -0600520 rp = ri->rsb;
Bob Peterson9beb3bf2011-10-26 15:24:55 -0500521 next = rb_next(&rp->res_hashnode);
David Teigland892c4462009-01-07 16:48:52 -0600522
Bob Peterson9beb3bf2011-10-26 15:24:55 -0500523 if (next) {
524 r = rb_entry(next, struct dlm_rsb, res_hashnode);
David Teigland892c4462009-01-07 16:48:52 -0600525 dlm_hold_rsb(r);
526 ri->rsb = r;
David Teiglandc7be7612009-01-07 16:50:41 -0600527 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
David Teigland892c4462009-01-07 16:48:52 -0600528 dlm_put_rsb(rp);
529 ++*pos;
530 return ri;
531 }
David Teiglandc7be7612009-01-07 16:50:41 -0600532 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
David Teigland892c4462009-01-07 16:48:52 -0600533 dlm_put_rsb(rp);
534
535 /*
536 * move to the first rsb in the next non-empty bucket
537 */
538
539 /* zero the entry */
540 n &= ~((1LL << 32) - 1);
541
542 while (1) {
543 bucket++;
544 n += 1LL << 32;
545
546 if (bucket >= ls->ls_rsbtbl_size) {
547 kfree(ri);
548 return NULL;
549 }
David Teiglandc04fecb2012-05-10 10:18:07 -0500550 tree = toss ? &ls->ls_rsbtbl[bucket].toss : &ls->ls_rsbtbl[bucket].keep;
David Teigland892c4462009-01-07 16:48:52 -0600551
David Teiglandc7be7612009-01-07 16:50:41 -0600552 spin_lock(&ls->ls_rsbtbl[bucket].lock);
David Teiglandc04fecb2012-05-10 10:18:07 -0500553 if (!RB_EMPTY_ROOT(tree)) {
554 next = rb_first(tree);
Bob Peterson9beb3bf2011-10-26 15:24:55 -0500555 r = rb_entry(next, struct dlm_rsb, res_hashnode);
David Teigland892c4462009-01-07 16:48:52 -0600556 dlm_hold_rsb(r);
557 ri->rsb = r;
558 ri->bucket = bucket;
David Teiglandc7be7612009-01-07 16:50:41 -0600559 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
David Teigland892c4462009-01-07 16:48:52 -0600560 *pos = n;
561 return ri;
562 }
David Teiglandc7be7612009-01-07 16:50:41 -0600563 spin_unlock(&ls->ls_rsbtbl[bucket].lock);
David Teigland892c4462009-01-07 16:48:52 -0600564 }
565}
566
567static void table_seq_stop(struct seq_file *seq, void *iter_ptr)
568{
569 struct rsbtbl_iter *ri = iter_ptr;
570
571 if (ri) {
572 dlm_put_rsb(ri->rsb);
573 kfree(ri);
574 }
575}
576
James Morris88e9d342009-09-22 16:43:43 -0700577static const struct seq_operations format1_seq_ops = {
David Teigland892c4462009-01-07 16:48:52 -0600578 .start = table_seq_start,
579 .next = table_seq_next,
580 .stop = table_seq_stop,
581 .show = table_seq_show,
David Teiglande7fd4172006-01-18 09:30:29 +0000582};
583
James Morris88e9d342009-09-22 16:43:43 -0700584static const struct seq_operations format2_seq_ops = {
David Teigland892c4462009-01-07 16:48:52 -0600585 .start = table_seq_start,
586 .next = table_seq_next,
587 .stop = table_seq_stop,
588 .show = table_seq_show,
589};
590
James Morris88e9d342009-09-22 16:43:43 -0700591static const struct seq_operations format3_seq_ops = {
David Teigland892c4462009-01-07 16:48:52 -0600592 .start = table_seq_start,
593 .next = table_seq_next,
594 .stop = table_seq_stop,
595 .show = table_seq_show,
596};
597
David Teiglandc04fecb2012-05-10 10:18:07 -0500598static const struct seq_operations format4_seq_ops = {
599 .start = table_seq_start,
600 .next = table_seq_next,
601 .stop = table_seq_stop,
602 .show = table_seq_show,
603};
604
David Teigland892c4462009-01-07 16:48:52 -0600605static const struct file_operations format1_fops;
606static const struct file_operations format2_fops;
607static const struct file_operations format3_fops;
David Teiglandc04fecb2012-05-10 10:18:07 -0500608static const struct file_operations format4_fops;
David Teigland892c4462009-01-07 16:48:52 -0600609
610static int table_open(struct inode *inode, struct file *file)
David Teiglande7fd4172006-01-18 09:30:29 +0000611{
612 struct seq_file *seq;
David Teigland892c4462009-01-07 16:48:52 -0600613 int ret = -1;
David Teiglande7fd4172006-01-18 09:30:29 +0000614
David Teigland892c4462009-01-07 16:48:52 -0600615 if (file->f_op == &format1_fops)
616 ret = seq_open(file, &format1_seq_ops);
617 else if (file->f_op == &format2_fops)
618 ret = seq_open(file, &format2_seq_ops);
619 else if (file->f_op == &format3_fops)
620 ret = seq_open(file, &format3_seq_ops);
David Teiglandc04fecb2012-05-10 10:18:07 -0500621 else if (file->f_op == &format4_fops)
622 ret = seq_open(file, &format4_seq_ops);
David Teigland892c4462009-01-07 16:48:52 -0600623
David Teiglande7fd4172006-01-18 09:30:29 +0000624 if (ret)
625 return ret;
626
627 seq = file->private_data;
David Teigland892c4462009-01-07 16:48:52 -0600628 seq->private = inode->i_private; /* the dlm_ls */
David Teiglande7fd4172006-01-18 09:30:29 +0000629 return 0;
630}
631
David Teigland892c4462009-01-07 16:48:52 -0600632static const struct file_operations format1_fops = {
David Teiglande7fd4172006-01-18 09:30:29 +0000633 .owner = THIS_MODULE,
David Teigland892c4462009-01-07 16:48:52 -0600634 .open = table_open,
David Teiglande7fd4172006-01-18 09:30:29 +0000635 .read = seq_read,
636 .llseek = seq_lseek,
637 .release = seq_release
638};
639
David Teigland892c4462009-01-07 16:48:52 -0600640static const struct file_operations format2_fops = {
David Teigland9dd592d2007-05-29 08:47:04 -0500641 .owner = THIS_MODULE,
David Teigland892c4462009-01-07 16:48:52 -0600642 .open = table_open,
David Teigland9dd592d2007-05-29 08:47:04 -0500643 .read = seq_read,
644 .llseek = seq_lseek,
645 .release = seq_release
646};
647
David Teigland892c4462009-01-07 16:48:52 -0600648static const struct file_operations format3_fops = {
David Teiglandd0225092008-12-16 14:53:23 -0600649 .owner = THIS_MODULE,
David Teigland892c4462009-01-07 16:48:52 -0600650 .open = table_open,
David Teiglandd0225092008-12-16 14:53:23 -0600651 .read = seq_read,
652 .llseek = seq_lseek,
653 .release = seq_release
654};
655
David Teiglandc04fecb2012-05-10 10:18:07 -0500656static const struct file_operations format4_fops = {
657 .owner = THIS_MODULE,
658 .open = table_open,
659 .read = seq_read,
660 .llseek = seq_lseek,
661 .release = seq_release
662};
663
David Teiglandd0225092008-12-16 14:53:23 -0600664/*
David Teigland5de63192006-07-25 13:44:31 -0500665 * dump lkb's on the ls_waiters list
666 */
David Teigland5de63192006-07-25 13:44:31 -0500667static ssize_t waiters_read(struct file *file, char __user *userbuf,
668 size_t count, loff_t *ppos)
669{
670 struct dlm_ls *ls = file->private_data;
671 struct dlm_lkb *lkb;
David Teigland06442442006-08-08 11:31:30 -0500672 size_t len = DLM_DEBUG_BUF_LEN, pos = 0, ret, rv;
David Teigland5de63192006-07-25 13:44:31 -0500673
674 mutex_lock(&debug_buf_lock);
675 mutex_lock(&ls->ls_waiters_mutex);
676 memset(debug_buf, 0, sizeof(debug_buf));
677
678 list_for_each_entry(lkb, &ls->ls_waiters, lkb_wait_reply) {
David Teigland06442442006-08-08 11:31:30 -0500679 ret = snprintf(debug_buf + pos, len - pos, "%x %d %d %s\n",
680 lkb->lkb_id, lkb->lkb_wait_type,
681 lkb->lkb_nodeid, lkb->lkb_resource->res_name);
682 if (ret >= len - pos)
683 break;
684 pos += ret;
David Teigland5de63192006-07-25 13:44:31 -0500685 }
686 mutex_unlock(&ls->ls_waiters_mutex);
687
688 rv = simple_read_from_buffer(userbuf, count, ppos, debug_buf, pos);
689 mutex_unlock(&debug_buf_lock);
690 return rv;
691}
692
Arjan van de Ven00977a52007-02-12 00:55:34 -0800693static const struct file_operations waiters_fops = {
David Teigland5de63192006-07-25 13:44:31 -0500694 .owner = THIS_MODULE,
Stephen Boyd234e3402012-04-05 14:25:11 -0700695 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200696 .read = waiters_read,
697 .llseek = default_llseek,
David Teigland5de63192006-07-25 13:44:31 -0500698};
699
David Teiglandd0225092008-12-16 14:53:23 -0600700void dlm_delete_debug_file(struct dlm_ls *ls)
701{
Fabian Fredericke0d9bf42014-08-08 14:23:37 -0700702 debugfs_remove(ls->ls_debug_rsb_dentry);
703 debugfs_remove(ls->ls_debug_waiters_dentry);
704 debugfs_remove(ls->ls_debug_locks_dentry);
705 debugfs_remove(ls->ls_debug_all_dentry);
706 debugfs_remove(ls->ls_debug_toss_dentry);
David Teiglandd0225092008-12-16 14:53:23 -0600707}
708
David Teiglande7fd4172006-01-18 09:30:29 +0000709int dlm_create_debug_file(struct dlm_ls *ls)
710{
David Teigland5de63192006-07-25 13:44:31 -0500711 char name[DLM_LOCKSPACE_LEN+8];
712
David Teiglandd0225092008-12-16 14:53:23 -0600713 /* format 1 */
714
David Teigland5de63192006-07-25 13:44:31 -0500715 ls->ls_debug_rsb_dentry = debugfs_create_file(ls->ls_name,
716 S_IFREG | S_IRUGO,
717 dlm_root,
718 ls,
David Teigland892c4462009-01-07 16:48:52 -0600719 &format1_fops);
David Teigland20abf972006-07-26 08:29:06 -0500720 if (!ls->ls_debug_rsb_dentry)
David Teiglandd0225092008-12-16 14:53:23 -0600721 goto fail;
David Teigland5de63192006-07-25 13:44:31 -0500722
David Teiglandd0225092008-12-16 14:53:23 -0600723 /* format 2 */
David Teigland5de63192006-07-25 13:44:31 -0500724
David Teigland9dd592d2007-05-29 08:47:04 -0500725 memset(name, 0, sizeof(name));
David Teiglandac90a252007-07-06 09:47:08 -0500726 snprintf(name, DLM_LOCKSPACE_LEN+8, "%s_locks", ls->ls_name);
David Teigland9dd592d2007-05-29 08:47:04 -0500727
David Teiglandac90a252007-07-06 09:47:08 -0500728 ls->ls_debug_locks_dentry = debugfs_create_file(name,
729 S_IFREG | S_IRUGO,
730 dlm_root,
731 ls,
David Teigland892c4462009-01-07 16:48:52 -0600732 &format2_fops);
David Teiglandd0225092008-12-16 14:53:23 -0600733 if (!ls->ls_debug_locks_dentry)
734 goto fail;
735
736 /* format 3 */
737
738 memset(name, 0, sizeof(name));
739 snprintf(name, DLM_LOCKSPACE_LEN+8, "%s_all", ls->ls_name);
740
741 ls->ls_debug_all_dentry = debugfs_create_file(name,
742 S_IFREG | S_IRUGO,
743 dlm_root,
744 ls,
David Teigland892c4462009-01-07 16:48:52 -0600745 &format3_fops);
David Teiglandd0225092008-12-16 14:53:23 -0600746 if (!ls->ls_debug_all_dentry)
747 goto fail;
748
David Teiglandc04fecb2012-05-10 10:18:07 -0500749 /* format 4 */
750
751 memset(name, 0, sizeof(name));
752 snprintf(name, DLM_LOCKSPACE_LEN+8, "%s_toss", ls->ls_name);
753
754 ls->ls_debug_toss_dentry = debugfs_create_file(name,
755 S_IFREG | S_IRUGO,
756 dlm_root,
757 ls,
758 &format4_fops);
759 if (!ls->ls_debug_toss_dentry)
760 goto fail;
761
David Teiglandd0225092008-12-16 14:53:23 -0600762 memset(name, 0, sizeof(name));
763 snprintf(name, DLM_LOCKSPACE_LEN+8, "%s_waiters", ls->ls_name);
764
765 ls->ls_debug_waiters_dentry = debugfs_create_file(name,
766 S_IFREG | S_IRUGO,
767 dlm_root,
768 ls,
769 &waiters_fops);
770 if (!ls->ls_debug_waiters_dentry)
771 goto fail;
David Teigland9dd592d2007-05-29 08:47:04 -0500772
David Teigland5de63192006-07-25 13:44:31 -0500773 return 0;
David Teiglande7fd4172006-01-18 09:30:29 +0000774
David Teiglandd0225092008-12-16 14:53:23 -0600775 fail:
776 dlm_delete_debug_file(ls);
777 return -ENOMEM;
David Teiglande7fd4172006-01-18 09:30:29 +0000778}
779
Denis Cheng30727172008-02-02 01:53:46 +0800780int __init dlm_register_debugfs(void)
David Teiglande7fd4172006-01-18 09:30:29 +0000781{
David Teigland5de63192006-07-25 13:44:31 -0500782 mutex_init(&debug_buf_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000783 dlm_root = debugfs_create_dir("dlm", NULL);
784 return dlm_root ? 0 : -ENOMEM;
785}
786
787void dlm_unregister_debugfs(void)
788{
789 debugfs_remove(dlm_root);
790}
791