debug_fs.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /******************************************************************************
  3. *******************************************************************************
  4. **
  5. ** Copyright (C) 2005-2009 Red Hat, Inc. All rights reserved.
  6. **
  7. **
  8. *******************************************************************************
  9. ******************************************************************************/
  10. #include <linux/pagemap.h>
  11. #include <linux/seq_file.h>
  12. #include <linux/init.h>
  13. #include <linux/ctype.h>
  14. #include <linux/debugfs.h>
  15. #include <linux/slab.h>
  16. #include "dlm_internal.h"
  17. #include "midcomms.h"
  18. #include "lock.h"
  19. #define DLM_DEBUG_BUF_LEN 4096
  20. static char debug_buf[DLM_DEBUG_BUF_LEN];
  21. static struct mutex debug_buf_lock;
  22. static struct dentry *dlm_root;
  23. static struct dentry *dlm_comms;
  24. static char *print_lockmode(int mode)
  25. {
  26. switch (mode) {
  27. case DLM_LOCK_IV:
  28. return "--";
  29. case DLM_LOCK_NL:
  30. return "NL";
  31. case DLM_LOCK_CR:
  32. return "CR";
  33. case DLM_LOCK_CW:
  34. return "CW";
  35. case DLM_LOCK_PR:
  36. return "PR";
  37. case DLM_LOCK_PW:
  38. return "PW";
  39. case DLM_LOCK_EX:
  40. return "EX";
  41. default:
  42. return "??";
  43. }
  44. }
  45. static void print_format1_lock(struct seq_file *s, struct dlm_lkb *lkb,
  46. struct dlm_rsb *res)
  47. {
  48. seq_printf(s, "%08x %s", lkb->lkb_id, print_lockmode(lkb->lkb_grmode));
  49. if (lkb->lkb_status == DLM_LKSTS_CONVERT ||
  50. lkb->lkb_status == DLM_LKSTS_WAITING)
  51. seq_printf(s, " (%s)", print_lockmode(lkb->lkb_rqmode));
  52. if (lkb->lkb_nodeid) {
  53. if (lkb->lkb_nodeid != res->res_nodeid)
  54. seq_printf(s, " Remote: %3d %08x", lkb->lkb_nodeid,
  55. lkb->lkb_remid);
  56. else
  57. seq_printf(s, " Master: %08x", lkb->lkb_remid);
  58. }
  59. if (lkb->lkb_wait_type)
  60. seq_printf(s, " wait_type: %d", lkb->lkb_wait_type);
  61. seq_putc(s, '\n');
  62. }
  63. static void print_format1(struct dlm_rsb *res, struct seq_file *s)
  64. {
  65. struct dlm_lkb *lkb;
  66. int i, lvblen = res->res_ls->ls_lvblen, recover_list, root_list;
  67. lock_rsb(res);
  68. seq_printf(s, "\nResource %p Name (len=%d) \"", res, res->res_length);
  69. for (i = 0; i < res->res_length; i++) {
  70. if (isprint(res->res_name[i]))
  71. seq_printf(s, "%c", res->res_name[i]);
  72. else
  73. seq_printf(s, "%c", '.');
  74. }
  75. if (res->res_nodeid > 0)
  76. seq_printf(s, "\"\nLocal Copy, Master is node %d\n",
  77. res->res_nodeid);
  78. else if (res->res_nodeid == 0)
  79. seq_puts(s, "\"\nMaster Copy\n");
  80. else if (res->res_nodeid == -1)
  81. seq_printf(s, "\"\nLooking up master (lkid %x)\n",
  82. res->res_first_lkid);
  83. else
  84. seq_printf(s, "\"\nInvalid master %d\n", res->res_nodeid);
  85. if (seq_has_overflowed(s))
  86. goto out;
  87. /* Print the LVB: */
  88. if (res->res_lvbptr) {
  89. seq_puts(s, "LVB: ");
  90. for (i = 0; i < lvblen; i++) {
  91. if (i == lvblen / 2)
  92. seq_puts(s, "\n ");
  93. seq_printf(s, "%02x ",
  94. (unsigned char) res->res_lvbptr[i]);
  95. }
  96. if (rsb_flag(res, RSB_VALNOTVALID))
  97. seq_puts(s, " (INVALID)");
  98. seq_putc(s, '\n');
  99. if (seq_has_overflowed(s))
  100. goto out;
  101. }
  102. root_list = !list_empty(&res->res_root_list);
  103. recover_list = !list_empty(&res->res_recover_list);
  104. if (root_list || recover_list) {
  105. seq_printf(s, "Recovery: root %d recover %d flags %lx count %d\n",
  106. root_list, recover_list,
  107. res->res_flags, res->res_recover_locks_count);
  108. }
  109. /* Print the locks attached to this resource */
  110. seq_puts(s, "Granted Queue\n");
  111. list_for_each_entry(lkb, &res->res_grantqueue, lkb_statequeue) {
  112. print_format1_lock(s, lkb, res);
  113. if (seq_has_overflowed(s))
  114. goto out;
  115. }
  116. seq_puts(s, "Conversion Queue\n");
  117. list_for_each_entry(lkb, &res->res_convertqueue, lkb_statequeue) {
  118. print_format1_lock(s, lkb, res);
  119. if (seq_has_overflowed(s))
  120. goto out;
  121. }
  122. seq_puts(s, "Waiting Queue\n");
  123. list_for_each_entry(lkb, &res->res_waitqueue, lkb_statequeue) {
  124. print_format1_lock(s, lkb, res);
  125. if (seq_has_overflowed(s))
  126. goto out;
  127. }
  128. if (list_empty(&res->res_lookup))
  129. goto out;
  130. seq_puts(s, "Lookup Queue\n");
  131. list_for_each_entry(lkb, &res->res_lookup, lkb_rsb_lookup) {
  132. seq_printf(s, "%08x %s",
  133. lkb->lkb_id, print_lockmode(lkb->lkb_rqmode));
  134. if (lkb->lkb_wait_type)
  135. seq_printf(s, " wait_type: %d", lkb->lkb_wait_type);
  136. seq_putc(s, '\n');
  137. if (seq_has_overflowed(s))
  138. goto out;
  139. }
  140. out:
  141. unlock_rsb(res);
  142. }
  143. static void print_format2_lock(struct seq_file *s, struct dlm_lkb *lkb,
  144. struct dlm_rsb *r)
  145. {
  146. u64 xid = 0;
  147. u64 us;
  148. if (lkb->lkb_flags & DLM_IFL_USER) {
  149. if (lkb->lkb_ua)
  150. xid = lkb->lkb_ua->xid;
  151. }
  152. /* microseconds since lkb was added to current queue */
  153. us = ktime_to_us(ktime_sub(ktime_get(), lkb->lkb_timestamp));
  154. /* id nodeid remid pid xid exflags flags sts grmode rqmode time_us
  155. r_nodeid r_len r_name */
  156. seq_printf(s, "%x %d %x %u %llu %x %x %d %d %d %llu %u %d \"%s\"\n",
  157. lkb->lkb_id,
  158. lkb->lkb_nodeid,
  159. lkb->lkb_remid,
  160. lkb->lkb_ownpid,
  161. (unsigned long long)xid,
  162. lkb->lkb_exflags,
  163. lkb->lkb_flags,
  164. lkb->lkb_status,
  165. lkb->lkb_grmode,
  166. lkb->lkb_rqmode,
  167. (unsigned long long)us,
  168. r->res_nodeid,
  169. r->res_length,
  170. r->res_name);
  171. }
  172. static void print_format2(struct dlm_rsb *r, struct seq_file *s)
  173. {
  174. struct dlm_lkb *lkb;
  175. lock_rsb(r);
  176. list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
  177. print_format2_lock(s, lkb, r);
  178. if (seq_has_overflowed(s))
  179. goto out;
  180. }
  181. list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
  182. print_format2_lock(s, lkb, r);
  183. if (seq_has_overflowed(s))
  184. goto out;
  185. }
  186. list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue) {
  187. print_format2_lock(s, lkb, r);
  188. if (seq_has_overflowed(s))
  189. goto out;
  190. }
  191. out:
  192. unlock_rsb(r);
  193. }
  194. static void print_format3_lock(struct seq_file *s, struct dlm_lkb *lkb,
  195. int rsb_lookup)
  196. {
  197. u64 xid = 0;
  198. if (lkb->lkb_flags & DLM_IFL_USER) {
  199. if (lkb->lkb_ua)
  200. xid = lkb->lkb_ua->xid;
  201. }
  202. seq_printf(s, "lkb %x %d %x %u %llu %x %x %d %d %d %d %d %d %u %llu %llu\n",
  203. lkb->lkb_id,
  204. lkb->lkb_nodeid,
  205. lkb->lkb_remid,
  206. lkb->lkb_ownpid,
  207. (unsigned long long)xid,
  208. lkb->lkb_exflags,
  209. lkb->lkb_flags,
  210. lkb->lkb_status,
  211. lkb->lkb_grmode,
  212. lkb->lkb_rqmode,
  213. lkb->lkb_last_bast.mode,
  214. rsb_lookup,
  215. lkb->lkb_wait_type,
  216. lkb->lkb_lvbseq,
  217. (unsigned long long)ktime_to_ns(lkb->lkb_timestamp),
  218. (unsigned long long)ktime_to_ns(lkb->lkb_last_bast_time));
  219. }
  220. static void print_format3(struct dlm_rsb *r, struct seq_file *s)
  221. {
  222. struct dlm_lkb *lkb;
  223. int i, lvblen = r->res_ls->ls_lvblen;
  224. int print_name = 1;
  225. lock_rsb(r);
  226. seq_printf(s, "rsb %p %d %x %lx %d %d %u %d ",
  227. r,
  228. r->res_nodeid,
  229. r->res_first_lkid,
  230. r->res_flags,
  231. !list_empty(&r->res_root_list),
  232. !list_empty(&r->res_recover_list),
  233. r->res_recover_locks_count,
  234. r->res_length);
  235. if (seq_has_overflowed(s))
  236. goto out;
  237. for (i = 0; i < r->res_length; i++) {
  238. if (!isascii(r->res_name[i]) || !isprint(r->res_name[i]))
  239. print_name = 0;
  240. }
  241. seq_puts(s, print_name ? "str " : "hex");
  242. for (i = 0; i < r->res_length; i++) {
  243. if (print_name)
  244. seq_printf(s, "%c", r->res_name[i]);
  245. else
  246. seq_printf(s, " %02x", (unsigned char)r->res_name[i]);
  247. }
  248. seq_putc(s, '\n');
  249. if (seq_has_overflowed(s))
  250. goto out;
  251. if (!r->res_lvbptr)
  252. goto do_locks;
  253. seq_printf(s, "lvb %u %d", r->res_lvbseq, lvblen);
  254. for (i = 0; i < lvblen; i++)
  255. seq_printf(s, " %02x", (unsigned char)r->res_lvbptr[i]);
  256. seq_putc(s, '\n');
  257. if (seq_has_overflowed(s))
  258. goto out;
  259. do_locks:
  260. list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
  261. print_format3_lock(s, lkb, 0);
  262. if (seq_has_overflowed(s))
  263. goto out;
  264. }
  265. list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
  266. print_format3_lock(s, lkb, 0);
  267. if (seq_has_overflowed(s))
  268. goto out;
  269. }
  270. list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue) {
  271. print_format3_lock(s, lkb, 0);
  272. if (seq_has_overflowed(s))
  273. goto out;
  274. }
  275. list_for_each_entry(lkb, &r->res_lookup, lkb_rsb_lookup) {
  276. print_format3_lock(s, lkb, 1);
  277. if (seq_has_overflowed(s))
  278. goto out;
  279. }
  280. out:
  281. unlock_rsb(r);
  282. }
  283. static void print_format4(struct dlm_rsb *r, struct seq_file *s)
  284. {
  285. int our_nodeid = dlm_our_nodeid();
  286. int print_name = 1;
  287. int i;
  288. lock_rsb(r);
  289. seq_printf(s, "rsb %p %d %d %d %d %lu %lx %d ",
  290. r,
  291. r->res_nodeid,
  292. r->res_master_nodeid,
  293. r->res_dir_nodeid,
  294. our_nodeid,
  295. r->res_toss_time,
  296. r->res_flags,
  297. r->res_length);
  298. for (i = 0; i < r->res_length; i++) {
  299. if (!isascii(r->res_name[i]) || !isprint(r->res_name[i]))
  300. print_name = 0;
  301. }
  302. seq_puts(s, print_name ? "str " : "hex");
  303. for (i = 0; i < r->res_length; i++) {
  304. if (print_name)
  305. seq_printf(s, "%c", r->res_name[i]);
  306. else
  307. seq_printf(s, " %02x", (unsigned char)r->res_name[i]);
  308. }
  309. seq_putc(s, '\n');
  310. unlock_rsb(r);
  311. }
  312. struct rsbtbl_iter {
  313. struct dlm_rsb *rsb;
  314. unsigned bucket;
  315. int format;
  316. int header;
  317. };
  318. /*
  319. * If the buffer is full, seq_printf can be called again, but it
  320. * does nothing. So, the these printing routines periodically check
  321. * seq_has_overflowed to avoid wasting too much time trying to print to
  322. * a full buffer.
  323. */
  324. static int table_seq_show(struct seq_file *seq, void *iter_ptr)
  325. {
  326. struct rsbtbl_iter *ri = iter_ptr;
  327. switch (ri->format) {
  328. case 1:
  329. print_format1(ri->rsb, seq);
  330. break;
  331. case 2:
  332. if (ri->header) {
  333. seq_puts(seq, "id nodeid remid pid xid exflags flags sts grmode rqmode time_ms r_nodeid r_len r_name\n");
  334. ri->header = 0;
  335. }
  336. print_format2(ri->rsb, seq);
  337. break;
  338. case 3:
  339. if (ri->header) {
  340. seq_puts(seq, "version rsb 1.1 lvb 1.1 lkb 1.1\n");
  341. ri->header = 0;
  342. }
  343. print_format3(ri->rsb, seq);
  344. break;
  345. case 4:
  346. if (ri->header) {
  347. seq_puts(seq, "version 4 rsb 2\n");
  348. ri->header = 0;
  349. }
  350. print_format4(ri->rsb, seq);
  351. break;
  352. }
  353. return 0;
  354. }
  355. static const struct seq_operations format1_seq_ops;
  356. static const struct seq_operations format2_seq_ops;
  357. static const struct seq_operations format3_seq_ops;
  358. static const struct seq_operations format4_seq_ops;
  359. static void *table_seq_start(struct seq_file *seq, loff_t *pos)
  360. {
  361. struct rb_root *tree;
  362. struct rb_node *node;
  363. struct dlm_ls *ls = seq->private;
  364. struct rsbtbl_iter *ri;
  365. struct dlm_rsb *r;
  366. loff_t n = *pos;
  367. unsigned bucket, entry;
  368. int toss = (seq->op == &format4_seq_ops);
  369. bucket = n >> 32;
  370. entry = n & ((1LL << 32) - 1);
  371. if (bucket >= ls->ls_rsbtbl_size)
  372. return NULL;
  373. ri = kzalloc(sizeof(*ri), GFP_NOFS);
  374. if (!ri)
  375. return NULL;
  376. if (n == 0)
  377. ri->header = 1;
  378. if (seq->op == &format1_seq_ops)
  379. ri->format = 1;
  380. if (seq->op == &format2_seq_ops)
  381. ri->format = 2;
  382. if (seq->op == &format3_seq_ops)
  383. ri->format = 3;
  384. if (seq->op == &format4_seq_ops)
  385. ri->format = 4;
  386. tree = toss ? &ls->ls_rsbtbl[bucket].toss : &ls->ls_rsbtbl[bucket].keep;
  387. spin_lock(&ls->ls_rsbtbl[bucket].lock);
  388. if (!RB_EMPTY_ROOT(tree)) {
  389. for (node = rb_first(tree); node; node = rb_next(node)) {
  390. r = rb_entry(node, struct dlm_rsb, res_hashnode);
  391. if (!entry--) {
  392. dlm_hold_rsb(r);
  393. ri->rsb = r;
  394. ri->bucket = bucket;
  395. spin_unlock(&ls->ls_rsbtbl[bucket].lock);
  396. return ri;
  397. }
  398. }
  399. }
  400. spin_unlock(&ls->ls_rsbtbl[bucket].lock);
  401. /*
  402. * move to the first rsb in the next non-empty bucket
  403. */
  404. /* zero the entry */
  405. n &= ~((1LL << 32) - 1);
  406. while (1) {
  407. bucket++;
  408. n += 1LL << 32;
  409. if (bucket >= ls->ls_rsbtbl_size) {
  410. kfree(ri);
  411. return NULL;
  412. }
  413. tree = toss ? &ls->ls_rsbtbl[bucket].toss : &ls->ls_rsbtbl[bucket].keep;
  414. spin_lock(&ls->ls_rsbtbl[bucket].lock);
  415. if (!RB_EMPTY_ROOT(tree)) {
  416. node = rb_first(tree);
  417. r = rb_entry(node, struct dlm_rsb, res_hashnode);
  418. dlm_hold_rsb(r);
  419. ri->rsb = r;
  420. ri->bucket = bucket;
  421. spin_unlock(&ls->ls_rsbtbl[bucket].lock);
  422. *pos = n;
  423. return ri;
  424. }
  425. spin_unlock(&ls->ls_rsbtbl[bucket].lock);
  426. }
  427. }
  428. static void *table_seq_next(struct seq_file *seq, void *iter_ptr, loff_t *pos)
  429. {
  430. struct dlm_ls *ls = seq->private;
  431. struct rsbtbl_iter *ri = iter_ptr;
  432. struct rb_root *tree;
  433. struct rb_node *next;
  434. struct dlm_rsb *r, *rp;
  435. loff_t n = *pos;
  436. unsigned bucket;
  437. int toss = (seq->op == &format4_seq_ops);
  438. bucket = n >> 32;
  439. /*
  440. * move to the next rsb in the same bucket
  441. */
  442. spin_lock(&ls->ls_rsbtbl[bucket].lock);
  443. rp = ri->rsb;
  444. next = rb_next(&rp->res_hashnode);
  445. if (next) {
  446. r = rb_entry(next, struct dlm_rsb, res_hashnode);
  447. dlm_hold_rsb(r);
  448. ri->rsb = r;
  449. spin_unlock(&ls->ls_rsbtbl[bucket].lock);
  450. dlm_put_rsb(rp);
  451. ++*pos;
  452. return ri;
  453. }
  454. spin_unlock(&ls->ls_rsbtbl[bucket].lock);
  455. dlm_put_rsb(rp);
  456. /*
  457. * move to the first rsb in the next non-empty bucket
  458. */
  459. /* zero the entry */
  460. n &= ~((1LL << 32) - 1);
  461. while (1) {
  462. bucket++;
  463. n += 1LL << 32;
  464. if (bucket >= ls->ls_rsbtbl_size) {
  465. kfree(ri);
  466. ++*pos;
  467. return NULL;
  468. }
  469. tree = toss ? &ls->ls_rsbtbl[bucket].toss : &ls->ls_rsbtbl[bucket].keep;
  470. spin_lock(&ls->ls_rsbtbl[bucket].lock);
  471. if (!RB_EMPTY_ROOT(tree)) {
  472. next = rb_first(tree);
  473. r = rb_entry(next, struct dlm_rsb, res_hashnode);
  474. dlm_hold_rsb(r);
  475. ri->rsb = r;
  476. ri->bucket = bucket;
  477. spin_unlock(&ls->ls_rsbtbl[bucket].lock);
  478. *pos = n;
  479. return ri;
  480. }
  481. spin_unlock(&ls->ls_rsbtbl[bucket].lock);
  482. }
  483. }
  484. static void table_seq_stop(struct seq_file *seq, void *iter_ptr)
  485. {
  486. struct rsbtbl_iter *ri = iter_ptr;
  487. if (ri) {
  488. dlm_put_rsb(ri->rsb);
  489. kfree(ri);
  490. }
  491. }
  492. static const struct seq_operations format1_seq_ops = {
  493. .start = table_seq_start,
  494. .next = table_seq_next,
  495. .stop = table_seq_stop,
  496. .show = table_seq_show,
  497. };
  498. static const struct seq_operations format2_seq_ops = {
  499. .start = table_seq_start,
  500. .next = table_seq_next,
  501. .stop = table_seq_stop,
  502. .show = table_seq_show,
  503. };
  504. static const struct seq_operations format3_seq_ops = {
  505. .start = table_seq_start,
  506. .next = table_seq_next,
  507. .stop = table_seq_stop,
  508. .show = table_seq_show,
  509. };
  510. static const struct seq_operations format4_seq_ops = {
  511. .start = table_seq_start,
  512. .next = table_seq_next,
  513. .stop = table_seq_stop,
  514. .show = table_seq_show,
  515. };
  516. static const struct file_operations format1_fops;
  517. static const struct file_operations format2_fops;
  518. static const struct file_operations format3_fops;
  519. static const struct file_operations format4_fops;
  520. static int table_open1(struct inode *inode, struct file *file)
  521. {
  522. struct seq_file *seq;
  523. int ret;
  524. ret = seq_open(file, &format1_seq_ops);
  525. if (ret)
  526. return ret;
  527. seq = file->private_data;
  528. seq->private = inode->i_private; /* the dlm_ls */
  529. return 0;
  530. }
  531. static int table_open2(struct inode *inode, struct file *file)
  532. {
  533. struct seq_file *seq;
  534. int ret;
  535. ret = seq_open(file, &format2_seq_ops);
  536. if (ret)
  537. return ret;
  538. seq = file->private_data;
  539. seq->private = inode->i_private; /* the dlm_ls */
  540. return 0;
  541. }
  542. static ssize_t table_write2(struct file *file, const char __user *user_buf,
  543. size_t count, loff_t *ppos)
  544. {
  545. struct seq_file *seq = file->private_data;
  546. int n, len, lkb_nodeid, lkb_status, error;
  547. char name[DLM_RESNAME_MAXLEN + 1] = {};
  548. struct dlm_ls *ls = seq->private;
  549. unsigned int lkb_flags;
  550. char buf[256] = {};
  551. uint32_t lkb_id;
  552. if (copy_from_user(buf, user_buf,
  553. min_t(size_t, sizeof(buf) - 1, count)))
  554. return -EFAULT;
  555. n = sscanf(buf, "%x %" __stringify(DLM_RESNAME_MAXLEN) "s %x %d %d",
  556. &lkb_id, name, &lkb_flags, &lkb_nodeid, &lkb_status);
  557. if (n != 5)
  558. return -EINVAL;
  559. len = strnlen(name, DLM_RESNAME_MAXLEN);
  560. error = dlm_debug_add_lkb(ls, lkb_id, name, len, lkb_flags,
  561. lkb_nodeid, lkb_status);
  562. if (error)
  563. return error;
  564. return count;
  565. }
  566. static int table_open3(struct inode *inode, struct file *file)
  567. {
  568. struct seq_file *seq;
  569. int ret;
  570. ret = seq_open(file, &format3_seq_ops);
  571. if (ret)
  572. return ret;
  573. seq = file->private_data;
  574. seq->private = inode->i_private; /* the dlm_ls */
  575. return 0;
  576. }
  577. static int table_open4(struct inode *inode, struct file *file)
  578. {
  579. struct seq_file *seq;
  580. int ret;
  581. ret = seq_open(file, &format4_seq_ops);
  582. if (ret)
  583. return ret;
  584. seq = file->private_data;
  585. seq->private = inode->i_private; /* the dlm_ls */
  586. return 0;
  587. }
  588. static const struct file_operations format1_fops = {
  589. .owner = THIS_MODULE,
  590. .open = table_open1,
  591. .read = seq_read,
  592. .llseek = seq_lseek,
  593. .release = seq_release
  594. };
  595. static const struct file_operations format2_fops = {
  596. .owner = THIS_MODULE,
  597. .open = table_open2,
  598. .read = seq_read,
  599. .write = table_write2,
  600. .llseek = seq_lseek,
  601. .release = seq_release
  602. };
  603. static const struct file_operations format3_fops = {
  604. .owner = THIS_MODULE,
  605. .open = table_open3,
  606. .read = seq_read,
  607. .llseek = seq_lseek,
  608. .release = seq_release
  609. };
  610. static const struct file_operations format4_fops = {
  611. .owner = THIS_MODULE,
  612. .open = table_open4,
  613. .read = seq_read,
  614. .llseek = seq_lseek,
  615. .release = seq_release
  616. };
  617. /*
  618. * dump lkb's on the ls_waiters list
  619. */
  620. static ssize_t waiters_read(struct file *file, char __user *userbuf,
  621. size_t count, loff_t *ppos)
  622. {
  623. struct dlm_ls *ls = file->private_data;
  624. struct dlm_lkb *lkb;
  625. size_t len = DLM_DEBUG_BUF_LEN, pos = 0, ret, rv;
  626. mutex_lock(&debug_buf_lock);
  627. mutex_lock(&ls->ls_waiters_mutex);
  628. memset(debug_buf, 0, sizeof(debug_buf));
  629. list_for_each_entry(lkb, &ls->ls_waiters, lkb_wait_reply) {
  630. ret = snprintf(debug_buf + pos, len - pos, "%x %d %d %s\n",
  631. lkb->lkb_id, lkb->lkb_wait_type,
  632. lkb->lkb_nodeid, lkb->lkb_resource->res_name);
  633. if (ret >= len - pos)
  634. break;
  635. pos += ret;
  636. }
  637. mutex_unlock(&ls->ls_waiters_mutex);
  638. rv = simple_read_from_buffer(userbuf, count, ppos, debug_buf, pos);
  639. mutex_unlock(&debug_buf_lock);
  640. return rv;
  641. }
  642. static ssize_t waiters_write(struct file *file, const char __user *user_buf,
  643. size_t count, loff_t *ppos)
  644. {
  645. struct dlm_ls *ls = file->private_data;
  646. int mstype, to_nodeid;
  647. char buf[128] = {};
  648. uint32_t lkb_id;
  649. int n, error;
  650. if (copy_from_user(buf, user_buf,
  651. min_t(size_t, sizeof(buf) - 1, count)))
  652. return -EFAULT;
  653. n = sscanf(buf, "%x %d %d", &lkb_id, &mstype, &to_nodeid);
  654. if (n != 3)
  655. return -EINVAL;
  656. error = dlm_debug_add_lkb_to_waiters(ls, lkb_id, mstype, to_nodeid);
  657. if (error)
  658. return error;
  659. return count;
  660. }
  661. static const struct file_operations waiters_fops = {
  662. .owner = THIS_MODULE,
  663. .open = simple_open,
  664. .read = waiters_read,
  665. .write = waiters_write,
  666. .llseek = default_llseek,
  667. };
  668. void dlm_delete_debug_file(struct dlm_ls *ls)
  669. {
  670. debugfs_remove(ls->ls_debug_rsb_dentry);
  671. debugfs_remove(ls->ls_debug_waiters_dentry);
  672. debugfs_remove(ls->ls_debug_locks_dentry);
  673. debugfs_remove(ls->ls_debug_all_dentry);
  674. debugfs_remove(ls->ls_debug_toss_dentry);
  675. }
  676. static int dlm_state_show(struct seq_file *file, void *offset)
  677. {
  678. seq_printf(file, "%s\n", dlm_midcomms_state(file->private));
  679. return 0;
  680. }
  681. DEFINE_SHOW_ATTRIBUTE(dlm_state);
  682. static int dlm_flags_show(struct seq_file *file, void *offset)
  683. {
  684. seq_printf(file, "%lu\n", dlm_midcomms_flags(file->private));
  685. return 0;
  686. }
  687. DEFINE_SHOW_ATTRIBUTE(dlm_flags);
  688. static int dlm_send_queue_cnt_show(struct seq_file *file, void *offset)
  689. {
  690. seq_printf(file, "%d\n", dlm_midcomms_send_queue_cnt(file->private));
  691. return 0;
  692. }
  693. DEFINE_SHOW_ATTRIBUTE(dlm_send_queue_cnt);
  694. static int dlm_version_show(struct seq_file *file, void *offset)
  695. {
  696. seq_printf(file, "0x%08x\n", dlm_midcomms_version(file->private));
  697. return 0;
  698. }
  699. DEFINE_SHOW_ATTRIBUTE(dlm_version);
  700. static ssize_t dlm_rawmsg_write(struct file *fp, const char __user *user_buf,
  701. size_t count, loff_t *ppos)
  702. {
  703. void *buf;
  704. int ret;
  705. if (count > PAGE_SIZE || count < sizeof(struct dlm_header))
  706. return -EINVAL;
  707. buf = kmalloc(PAGE_SIZE, GFP_NOFS);
  708. if (!buf)
  709. return -ENOMEM;
  710. if (copy_from_user(buf, user_buf, count)) {
  711. ret = -EFAULT;
  712. goto out;
  713. }
  714. ret = dlm_midcomms_rawmsg_send(fp->private_data, buf, count);
  715. if (ret)
  716. goto out;
  717. kfree(buf);
  718. return count;
  719. out:
  720. kfree(buf);
  721. return ret;
  722. }
  723. static const struct file_operations dlm_rawmsg_fops = {
  724. .open = simple_open,
  725. .write = dlm_rawmsg_write,
  726. .llseek = no_llseek,
  727. };
  728. void *dlm_create_debug_comms_file(int nodeid, void *data)
  729. {
  730. struct dentry *d_node;
  731. char name[256];
  732. memset(name, 0, sizeof(name));
  733. snprintf(name, 256, "%d", nodeid);
  734. d_node = debugfs_create_dir(name, dlm_comms);
  735. debugfs_create_file("state", 0444, d_node, data, &dlm_state_fops);
  736. debugfs_create_file("flags", 0444, d_node, data, &dlm_flags_fops);
  737. debugfs_create_file("send_queue_count", 0444, d_node, data,
  738. &dlm_send_queue_cnt_fops);
  739. debugfs_create_file("version", 0444, d_node, data, &dlm_version_fops);
  740. debugfs_create_file("rawmsg", 0200, d_node, data, &dlm_rawmsg_fops);
  741. return d_node;
  742. }
  743. void dlm_delete_debug_comms_file(void *ctx)
  744. {
  745. debugfs_remove(ctx);
  746. }
  747. void dlm_create_debug_file(struct dlm_ls *ls)
  748. {
  749. char name[DLM_LOCKSPACE_LEN + 8];
  750. /* format 1 */
  751. ls->ls_debug_rsb_dentry = debugfs_create_file(ls->ls_name,
  752. S_IFREG | S_IRUGO,
  753. dlm_root,
  754. ls,
  755. &format1_fops);
  756. /* format 2 */
  757. memset(name, 0, sizeof(name));
  758. snprintf(name, DLM_LOCKSPACE_LEN + 8, "%s_locks", ls->ls_name);
  759. ls->ls_debug_locks_dentry = debugfs_create_file(name,
  760. 0644,
  761. dlm_root,
  762. ls,
  763. &format2_fops);
  764. /* format 3 */
  765. memset(name, 0, sizeof(name));
  766. snprintf(name, DLM_LOCKSPACE_LEN + 8, "%s_all", ls->ls_name);
  767. ls->ls_debug_all_dentry = debugfs_create_file(name,
  768. S_IFREG | S_IRUGO,
  769. dlm_root,
  770. ls,
  771. &format3_fops);
  772. /* format 4 */
  773. memset(name, 0, sizeof(name));
  774. snprintf(name, DLM_LOCKSPACE_LEN + 8, "%s_toss", ls->ls_name);
  775. ls->ls_debug_toss_dentry = debugfs_create_file(name,
  776. S_IFREG | S_IRUGO,
  777. dlm_root,
  778. ls,
  779. &format4_fops);
  780. memset(name, 0, sizeof(name));
  781. snprintf(name, DLM_LOCKSPACE_LEN + 8, "%s_waiters", ls->ls_name);
  782. ls->ls_debug_waiters_dentry = debugfs_create_file(name,
  783. 0644,
  784. dlm_root,
  785. ls,
  786. &waiters_fops);
  787. }
  788. void __init dlm_register_debugfs(void)
  789. {
  790. mutex_init(&debug_buf_lock);
  791. dlm_root = debugfs_create_dir("dlm", NULL);
  792. dlm_comms = debugfs_create_dir("comms", dlm_root);
  793. }
  794. void dlm_unregister_debugfs(void)
  795. {
  796. debugfs_remove(dlm_root);
  797. }