drbd_debugfs.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. #define pr_fmt(fmt) "drbd debugfs: " fmt
  2. #include <linux/kernel.h>
  3. #include <linux/module.h>
  4. #include <linux/debugfs.h>
  5. #include <linux/seq_file.h>
  6. #include <linux/stat.h>
  7. #include <linux/jiffies.h>
  8. #include <linux/list.h>
  9. #include "drbd_int.h"
  10. #include "drbd_req.h"
  11. #include "drbd_debugfs.h"
  12. /**********************************************************************
  13. * Whenever you change the file format, remember to bump the version. *
  14. **********************************************************************/
  15. static struct dentry *drbd_debugfs_root;
  16. static struct dentry *drbd_debugfs_version;
  17. static struct dentry *drbd_debugfs_resources;
  18. static struct dentry *drbd_debugfs_minors;
  19. static void seq_print_age_or_dash(struct seq_file *m, bool valid, unsigned long dt)
  20. {
  21. if (valid)
  22. seq_printf(m, "\t%d", jiffies_to_msecs(dt));
  23. else
  24. seq_printf(m, "\t-");
  25. }
  26. static void __seq_print_rq_state_bit(struct seq_file *m,
  27. bool is_set, char *sep, const char *set_name, const char *unset_name)
  28. {
  29. if (is_set && set_name) {
  30. seq_putc(m, *sep);
  31. seq_puts(m, set_name);
  32. *sep = '|';
  33. } else if (!is_set && unset_name) {
  34. seq_putc(m, *sep);
  35. seq_puts(m, unset_name);
  36. *sep = '|';
  37. }
  38. }
  39. static void seq_print_rq_state_bit(struct seq_file *m,
  40. bool is_set, char *sep, const char *set_name)
  41. {
  42. __seq_print_rq_state_bit(m, is_set, sep, set_name, NULL);
  43. }
  44. /* pretty print enum drbd_req_state_bits req->rq_state */
  45. static void seq_print_request_state(struct seq_file *m, struct drbd_request *req)
  46. {
  47. unsigned int s = req->rq_state;
  48. char sep = ' ';
  49. seq_printf(m, "\t0x%08x", s);
  50. seq_printf(m, "\tmaster: %s", req->master_bio ? "pending" : "completed");
  51. /* RQ_WRITE ignored, already reported */
  52. seq_puts(m, "\tlocal:");
  53. seq_print_rq_state_bit(m, s & RQ_IN_ACT_LOG, &sep, "in-AL");
  54. seq_print_rq_state_bit(m, s & RQ_POSTPONED, &sep, "postponed");
  55. seq_print_rq_state_bit(m, s & RQ_COMPLETION_SUSP, &sep, "suspended");
  56. sep = ' ';
  57. seq_print_rq_state_bit(m, s & RQ_LOCAL_PENDING, &sep, "pending");
  58. seq_print_rq_state_bit(m, s & RQ_LOCAL_COMPLETED, &sep, "completed");
  59. seq_print_rq_state_bit(m, s & RQ_LOCAL_ABORTED, &sep, "aborted");
  60. seq_print_rq_state_bit(m, s & RQ_LOCAL_OK, &sep, "ok");
  61. if (sep == ' ')
  62. seq_puts(m, " -");
  63. /* for_each_connection ... */
  64. seq_printf(m, "\tnet:");
  65. sep = ' ';
  66. seq_print_rq_state_bit(m, s & RQ_NET_PENDING, &sep, "pending");
  67. seq_print_rq_state_bit(m, s & RQ_NET_QUEUED, &sep, "queued");
  68. seq_print_rq_state_bit(m, s & RQ_NET_SENT, &sep, "sent");
  69. seq_print_rq_state_bit(m, s & RQ_NET_DONE, &sep, "done");
  70. seq_print_rq_state_bit(m, s & RQ_NET_SIS, &sep, "sis");
  71. seq_print_rq_state_bit(m, s & RQ_NET_OK, &sep, "ok");
  72. if (sep == ' ')
  73. seq_puts(m, " -");
  74. seq_printf(m, " :");
  75. sep = ' ';
  76. seq_print_rq_state_bit(m, s & RQ_EXP_RECEIVE_ACK, &sep, "B");
  77. seq_print_rq_state_bit(m, s & RQ_EXP_WRITE_ACK, &sep, "C");
  78. seq_print_rq_state_bit(m, s & RQ_EXP_BARR_ACK, &sep, "barr");
  79. if (sep == ' ')
  80. seq_puts(m, " -");
  81. seq_printf(m, "\n");
  82. }
  83. static void seq_print_one_request(struct seq_file *m, struct drbd_request *req, unsigned long now)
  84. {
  85. /* change anything here, fixup header below! */
  86. unsigned int s = req->rq_state;
  87. #define RQ_HDR_1 "epoch\tsector\tsize\trw"
  88. seq_printf(m, "0x%x\t%llu\t%u\t%s",
  89. req->epoch,
  90. (unsigned long long)req->i.sector, req->i.size >> 9,
  91. (s & RQ_WRITE) ? "W" : "R");
  92. #define RQ_HDR_2 "\tstart\tin AL\tsubmit"
  93. seq_printf(m, "\t%d", jiffies_to_msecs(now - req->start_jif));
  94. seq_print_age_or_dash(m, s & RQ_IN_ACT_LOG, now - req->in_actlog_jif);
  95. seq_print_age_or_dash(m, s & RQ_LOCAL_PENDING, now - req->pre_submit_jif);
  96. #define RQ_HDR_3 "\tsent\tacked\tdone"
  97. seq_print_age_or_dash(m, s & RQ_NET_SENT, now - req->pre_send_jif);
  98. seq_print_age_or_dash(m, (s & RQ_NET_SENT) && !(s & RQ_NET_PENDING), now - req->acked_jif);
  99. seq_print_age_or_dash(m, s & RQ_NET_DONE, now - req->net_done_jif);
  100. #define RQ_HDR_4 "\tstate\n"
  101. seq_print_request_state(m, req);
  102. }
  103. #define RQ_HDR RQ_HDR_1 RQ_HDR_2 RQ_HDR_3 RQ_HDR_4
  104. static void seq_print_minor_vnr_req(struct seq_file *m, struct drbd_request *req, unsigned long now)
  105. {
  106. seq_printf(m, "%u\t%u\t", req->device->minor, req->device->vnr);
  107. seq_print_one_request(m, req, now);
  108. }
  109. static void seq_print_resource_pending_meta_io(struct seq_file *m, struct drbd_resource *resource, unsigned long now)
  110. {
  111. struct drbd_device *device;
  112. unsigned int i;
  113. seq_puts(m, "minor\tvnr\tstart\tsubmit\tintent\n");
  114. rcu_read_lock();
  115. idr_for_each_entry(&resource->devices, device, i) {
  116. struct drbd_md_io tmp;
  117. /* In theory this is racy,
  118. * in the sense that there could have been a
  119. * drbd_md_put_buffer(); drbd_md_get_buffer();
  120. * between accessing these members here. */
  121. tmp = device->md_io;
  122. if (atomic_read(&tmp.in_use)) {
  123. seq_printf(m, "%u\t%u\t%d\t",
  124. device->minor, device->vnr,
  125. jiffies_to_msecs(now - tmp.start_jif));
  126. if (time_before(tmp.submit_jif, tmp.start_jif))
  127. seq_puts(m, "-\t");
  128. else
  129. seq_printf(m, "%d\t", jiffies_to_msecs(now - tmp.submit_jif));
  130. seq_printf(m, "%s\n", tmp.current_use);
  131. }
  132. }
  133. rcu_read_unlock();
  134. }
  135. static void seq_print_waiting_for_AL(struct seq_file *m, struct drbd_resource *resource, unsigned long now)
  136. {
  137. struct drbd_device *device;
  138. unsigned int i;
  139. seq_puts(m, "minor\tvnr\tage\t#waiting\n");
  140. rcu_read_lock();
  141. idr_for_each_entry(&resource->devices, device, i) {
  142. unsigned long jif;
  143. struct drbd_request *req;
  144. int n = atomic_read(&device->ap_actlog_cnt);
  145. if (n) {
  146. spin_lock_irq(&device->resource->req_lock);
  147. req = list_first_entry_or_null(&device->pending_master_completion[1],
  148. struct drbd_request, req_pending_master_completion);
  149. /* if the oldest request does not wait for the activity log
  150. * it is not interesting for us here */
  151. if (req && !(req->rq_state & RQ_IN_ACT_LOG))
  152. jif = req->start_jif;
  153. else
  154. req = NULL;
  155. spin_unlock_irq(&device->resource->req_lock);
  156. }
  157. if (n) {
  158. seq_printf(m, "%u\t%u\t", device->minor, device->vnr);
  159. if (req)
  160. seq_printf(m, "%u\t", jiffies_to_msecs(now - jif));
  161. else
  162. seq_puts(m, "-\t");
  163. seq_printf(m, "%u\n", n);
  164. }
  165. }
  166. rcu_read_unlock();
  167. }
  168. static void seq_print_device_bitmap_io(struct seq_file *m, struct drbd_device *device, unsigned long now)
  169. {
  170. struct drbd_bm_aio_ctx *ctx;
  171. unsigned long start_jif;
  172. unsigned int in_flight;
  173. unsigned int flags;
  174. spin_lock_irq(&device->resource->req_lock);
  175. ctx = list_first_entry_or_null(&device->pending_bitmap_io, struct drbd_bm_aio_ctx, list);
  176. if (ctx && ctx->done)
  177. ctx = NULL;
  178. if (ctx) {
  179. start_jif = ctx->start_jif;
  180. in_flight = atomic_read(&ctx->in_flight);
  181. flags = ctx->flags;
  182. }
  183. spin_unlock_irq(&device->resource->req_lock);
  184. if (ctx) {
  185. seq_printf(m, "%u\t%u\t%c\t%u\t%u\n",
  186. device->minor, device->vnr,
  187. (flags & BM_AIO_READ) ? 'R' : 'W',
  188. jiffies_to_msecs(now - start_jif),
  189. in_flight);
  190. }
  191. }
  192. static void seq_print_resource_pending_bitmap_io(struct seq_file *m, struct drbd_resource *resource, unsigned long now)
  193. {
  194. struct drbd_device *device;
  195. unsigned int i;
  196. seq_puts(m, "minor\tvnr\trw\tage\t#in-flight\n");
  197. rcu_read_lock();
  198. idr_for_each_entry(&resource->devices, device, i) {
  199. seq_print_device_bitmap_io(m, device, now);
  200. }
  201. rcu_read_unlock();
  202. }
  203. /* pretty print enum peer_req->flags */
  204. static void seq_print_peer_request_flags(struct seq_file *m, struct drbd_peer_request *peer_req)
  205. {
  206. unsigned long f = peer_req->flags;
  207. char sep = ' ';
  208. __seq_print_rq_state_bit(m, f & EE_SUBMITTED, &sep, "submitted", "preparing");
  209. __seq_print_rq_state_bit(m, f & EE_APPLICATION, &sep, "application", "internal");
  210. seq_print_rq_state_bit(m, f & EE_CALL_AL_COMPLETE_IO, &sep, "in-AL");
  211. seq_print_rq_state_bit(m, f & EE_SEND_WRITE_ACK, &sep, "C");
  212. seq_print_rq_state_bit(m, f & EE_MAY_SET_IN_SYNC, &sep, "set-in-sync");
  213. if (f & EE_IS_TRIM) {
  214. seq_putc(m, sep);
  215. sep = '|';
  216. if (f & EE_IS_TRIM_USE_ZEROOUT)
  217. seq_puts(m, "zero-out");
  218. else
  219. seq_puts(m, "trim");
  220. }
  221. seq_putc(m, '\n');
  222. }
  223. static void seq_print_peer_request(struct seq_file *m,
  224. struct drbd_device *device, struct list_head *lh,
  225. unsigned long now)
  226. {
  227. bool reported_preparing = false;
  228. struct drbd_peer_request *peer_req;
  229. list_for_each_entry(peer_req, lh, w.list) {
  230. if (reported_preparing && !(peer_req->flags & EE_SUBMITTED))
  231. continue;
  232. if (device)
  233. seq_printf(m, "%u\t%u\t", device->minor, device->vnr);
  234. seq_printf(m, "%llu\t%u\t%c\t%u\t",
  235. (unsigned long long)peer_req->i.sector, peer_req->i.size >> 9,
  236. (peer_req->flags & EE_WRITE) ? 'W' : 'R',
  237. jiffies_to_msecs(now - peer_req->submit_jif));
  238. seq_print_peer_request_flags(m, peer_req);
  239. if (peer_req->flags & EE_SUBMITTED)
  240. break;
  241. else
  242. reported_preparing = true;
  243. }
  244. }
  245. static void seq_print_device_peer_requests(struct seq_file *m,
  246. struct drbd_device *device, unsigned long now)
  247. {
  248. seq_puts(m, "minor\tvnr\tsector\tsize\trw\tage\tflags\n");
  249. spin_lock_irq(&device->resource->req_lock);
  250. seq_print_peer_request(m, device, &device->active_ee, now);
  251. seq_print_peer_request(m, device, &device->read_ee, now);
  252. seq_print_peer_request(m, device, &device->sync_ee, now);
  253. spin_unlock_irq(&device->resource->req_lock);
  254. if (test_bit(FLUSH_PENDING, &device->flags)) {
  255. seq_printf(m, "%u\t%u\t-\t-\tF\t%u\tflush\n",
  256. device->minor, device->vnr,
  257. jiffies_to_msecs(now - device->flush_jif));
  258. }
  259. }
  260. static void seq_print_resource_pending_peer_requests(struct seq_file *m,
  261. struct drbd_resource *resource, unsigned long now)
  262. {
  263. struct drbd_device *device;
  264. unsigned int i;
  265. rcu_read_lock();
  266. idr_for_each_entry(&resource->devices, device, i) {
  267. seq_print_device_peer_requests(m, device, now);
  268. }
  269. rcu_read_unlock();
  270. }
  271. static void seq_print_resource_transfer_log_summary(struct seq_file *m,
  272. struct drbd_resource *resource,
  273. struct drbd_connection *connection,
  274. unsigned long now)
  275. {
  276. struct drbd_request *req;
  277. unsigned int count = 0;
  278. unsigned int show_state = 0;
  279. seq_puts(m, "n\tdevice\tvnr\t" RQ_HDR);
  280. spin_lock_irq(&resource->req_lock);
  281. list_for_each_entry(req, &connection->transfer_log, tl_requests) {
  282. unsigned int tmp = 0;
  283. unsigned int s;
  284. ++count;
  285. /* don't disable irq "forever" */
  286. if (!(count & 0x1ff)) {
  287. struct drbd_request *req_next;
  288. kref_get(&req->kref);
  289. spin_unlock_irq(&resource->req_lock);
  290. cond_resched();
  291. spin_lock_irq(&resource->req_lock);
  292. req_next = list_next_entry(req, tl_requests);
  293. if (kref_put(&req->kref, drbd_req_destroy))
  294. req = req_next;
  295. if (&req->tl_requests == &connection->transfer_log)
  296. break;
  297. }
  298. s = req->rq_state;
  299. /* This is meant to summarize timing issues, to be able to tell
  300. * local disk problems from network problems.
  301. * Skip requests, if we have shown an even older request with
  302. * similar aspects already. */
  303. if (req->master_bio == NULL)
  304. tmp |= 1;
  305. if ((s & RQ_LOCAL_MASK) && (s & RQ_LOCAL_PENDING))
  306. tmp |= 2;
  307. if (s & RQ_NET_MASK) {
  308. if (!(s & RQ_NET_SENT))
  309. tmp |= 4;
  310. if (s & RQ_NET_PENDING)
  311. tmp |= 8;
  312. if (!(s & RQ_NET_DONE))
  313. tmp |= 16;
  314. }
  315. if ((tmp & show_state) == tmp)
  316. continue;
  317. show_state |= tmp;
  318. seq_printf(m, "%u\t", count);
  319. seq_print_minor_vnr_req(m, req, now);
  320. if (show_state == 0x1f)
  321. break;
  322. }
  323. spin_unlock_irq(&resource->req_lock);
  324. }
  325. /* TODO: transfer_log and friends should be moved to resource */
  326. static int in_flight_summary_show(struct seq_file *m, void *pos)
  327. {
  328. struct drbd_resource *resource = m->private;
  329. struct drbd_connection *connection;
  330. unsigned long jif = jiffies;
  331. connection = first_connection(resource);
  332. /* This does not happen, actually.
  333. * But be robust and prepare for future code changes. */
  334. if (!connection || !kref_get_unless_zero(&connection->kref))
  335. return -ESTALE;
  336. /* BUMP me if you change the file format/content/presentation */
  337. seq_printf(m, "v: %u\n\n", 0);
  338. seq_puts(m, "oldest bitmap IO\n");
  339. seq_print_resource_pending_bitmap_io(m, resource, jif);
  340. seq_putc(m, '\n');
  341. seq_puts(m, "meta data IO\n");
  342. seq_print_resource_pending_meta_io(m, resource, jif);
  343. seq_putc(m, '\n');
  344. seq_puts(m, "socket buffer stats\n");
  345. /* for each connection ... once we have more than one */
  346. rcu_read_lock();
  347. if (connection->data.socket) {
  348. /* open coded SIOCINQ, the "relevant" part */
  349. struct tcp_sock *tp = tcp_sk(connection->data.socket->sk);
  350. int answ = tp->rcv_nxt - tp->copied_seq;
  351. seq_printf(m, "unread receive buffer: %u Byte\n", answ);
  352. /* open coded SIOCOUTQ, the "relevant" part */
  353. answ = tp->write_seq - tp->snd_una;
  354. seq_printf(m, "unacked send buffer: %u Byte\n", answ);
  355. }
  356. rcu_read_unlock();
  357. seq_putc(m, '\n');
  358. seq_puts(m, "oldest peer requests\n");
  359. seq_print_resource_pending_peer_requests(m, resource, jif);
  360. seq_putc(m, '\n');
  361. seq_puts(m, "application requests waiting for activity log\n");
  362. seq_print_waiting_for_AL(m, resource, jif);
  363. seq_putc(m, '\n');
  364. seq_puts(m, "oldest application requests\n");
  365. seq_print_resource_transfer_log_summary(m, resource, connection, jif);
  366. seq_putc(m, '\n');
  367. jif = jiffies - jif;
  368. if (jif)
  369. seq_printf(m, "generated in %d ms\n", jiffies_to_msecs(jif));
  370. kref_put(&connection->kref, drbd_destroy_connection);
  371. return 0;
  372. }
  373. /* make sure at *open* time that the respective object won't go away. */
  374. static int drbd_single_open(struct file *file, int (*show)(struct seq_file *, void *),
  375. void *data, struct kref *kref,
  376. void (*release)(struct kref *))
  377. {
  378. struct dentry *parent;
  379. int ret = -ESTALE;
  380. /* Are we still linked,
  381. * or has debugfs_remove() already been called? */
  382. parent = file->f_path.dentry->d_parent;
  383. /* not sure if this can happen: */
  384. if (!parent || d_really_is_negative(parent))
  385. goto out;
  386. /* serialize with d_delete() */
  387. mutex_lock(&d_inode(parent)->i_mutex);
  388. /* Make sure the object is still alive */
  389. if (simple_positive(file->f_path.dentry)
  390. && kref_get_unless_zero(kref))
  391. ret = 0;
  392. mutex_unlock(&d_inode(parent)->i_mutex);
  393. if (!ret) {
  394. ret = single_open(file, show, data);
  395. if (ret)
  396. kref_put(kref, release);
  397. }
  398. out:
  399. return ret;
  400. }
  401. static int in_flight_summary_open(struct inode *inode, struct file *file)
  402. {
  403. struct drbd_resource *resource = inode->i_private;
  404. return drbd_single_open(file, in_flight_summary_show, resource,
  405. &resource->kref, drbd_destroy_resource);
  406. }
  407. static int in_flight_summary_release(struct inode *inode, struct file *file)
  408. {
  409. struct drbd_resource *resource = inode->i_private;
  410. kref_put(&resource->kref, drbd_destroy_resource);
  411. return single_release(inode, file);
  412. }
  413. static const struct file_operations in_flight_summary_fops = {
  414. .owner = THIS_MODULE,
  415. .open = in_flight_summary_open,
  416. .read = seq_read,
  417. .llseek = seq_lseek,
  418. .release = in_flight_summary_release,
  419. };
  420. void drbd_debugfs_resource_add(struct drbd_resource *resource)
  421. {
  422. struct dentry *dentry;
  423. if (!drbd_debugfs_resources)
  424. return;
  425. dentry = debugfs_create_dir(resource->name, drbd_debugfs_resources);
  426. if (IS_ERR_OR_NULL(dentry))
  427. goto fail;
  428. resource->debugfs_res = dentry;
  429. dentry = debugfs_create_dir("volumes", resource->debugfs_res);
  430. if (IS_ERR_OR_NULL(dentry))
  431. goto fail;
  432. resource->debugfs_res_volumes = dentry;
  433. dentry = debugfs_create_dir("connections", resource->debugfs_res);
  434. if (IS_ERR_OR_NULL(dentry))
  435. goto fail;
  436. resource->debugfs_res_connections = dentry;
  437. dentry = debugfs_create_file("in_flight_summary", S_IRUSR|S_IRGRP,
  438. resource->debugfs_res, resource,
  439. &in_flight_summary_fops);
  440. if (IS_ERR_OR_NULL(dentry))
  441. goto fail;
  442. resource->debugfs_res_in_flight_summary = dentry;
  443. return;
  444. fail:
  445. drbd_debugfs_resource_cleanup(resource);
  446. drbd_err(resource, "failed to create debugfs dentry\n");
  447. }
  448. static void drbd_debugfs_remove(struct dentry **dp)
  449. {
  450. debugfs_remove(*dp);
  451. *dp = NULL;
  452. }
  453. void drbd_debugfs_resource_cleanup(struct drbd_resource *resource)
  454. {
  455. /* it is ok to call debugfs_remove(NULL) */
  456. drbd_debugfs_remove(&resource->debugfs_res_in_flight_summary);
  457. drbd_debugfs_remove(&resource->debugfs_res_connections);
  458. drbd_debugfs_remove(&resource->debugfs_res_volumes);
  459. drbd_debugfs_remove(&resource->debugfs_res);
  460. }
  461. static void seq_print_one_timing_detail(struct seq_file *m,
  462. const struct drbd_thread_timing_details *tdp,
  463. unsigned long now)
  464. {
  465. struct drbd_thread_timing_details td;
  466. /* No locking...
  467. * use temporary assignment to get at consistent data. */
  468. do {
  469. td = *tdp;
  470. } while (td.cb_nr != tdp->cb_nr);
  471. if (!td.cb_addr)
  472. return;
  473. seq_printf(m, "%u\t%d\t%s:%u\t%ps\n",
  474. td.cb_nr,
  475. jiffies_to_msecs(now - td.start_jif),
  476. td.caller_fn, td.line,
  477. td.cb_addr);
  478. }
  479. static void seq_print_timing_details(struct seq_file *m,
  480. const char *title,
  481. unsigned int cb_nr, struct drbd_thread_timing_details *tdp, unsigned long now)
  482. {
  483. unsigned int start_idx;
  484. unsigned int i;
  485. seq_printf(m, "%s\n", title);
  486. /* If not much is going on, this will result in natural ordering.
  487. * If it is very busy, we will possibly skip events, or even see wrap
  488. * arounds, which could only be avoided with locking.
  489. */
  490. start_idx = cb_nr % DRBD_THREAD_DETAILS_HIST;
  491. for (i = start_idx; i < DRBD_THREAD_DETAILS_HIST; i++)
  492. seq_print_one_timing_detail(m, tdp+i, now);
  493. for (i = 0; i < start_idx; i++)
  494. seq_print_one_timing_detail(m, tdp+i, now);
  495. }
  496. static int callback_history_show(struct seq_file *m, void *ignored)
  497. {
  498. struct drbd_connection *connection = m->private;
  499. unsigned long jif = jiffies;
  500. /* BUMP me if you change the file format/content/presentation */
  501. seq_printf(m, "v: %u\n\n", 0);
  502. seq_puts(m, "n\tage\tcallsite\tfn\n");
  503. seq_print_timing_details(m, "worker", connection->w_cb_nr, connection->w_timing_details, jif);
  504. seq_print_timing_details(m, "receiver", connection->r_cb_nr, connection->r_timing_details, jif);
  505. return 0;
  506. }
  507. static int callback_history_open(struct inode *inode, struct file *file)
  508. {
  509. struct drbd_connection *connection = inode->i_private;
  510. return drbd_single_open(file, callback_history_show, connection,
  511. &connection->kref, drbd_destroy_connection);
  512. }
  513. static int callback_history_release(struct inode *inode, struct file *file)
  514. {
  515. struct drbd_connection *connection = inode->i_private;
  516. kref_put(&connection->kref, drbd_destroy_connection);
  517. return single_release(inode, file);
  518. }
  519. static const struct file_operations connection_callback_history_fops = {
  520. .owner = THIS_MODULE,
  521. .open = callback_history_open,
  522. .read = seq_read,
  523. .llseek = seq_lseek,
  524. .release = callback_history_release,
  525. };
  526. static int connection_oldest_requests_show(struct seq_file *m, void *ignored)
  527. {
  528. struct drbd_connection *connection = m->private;
  529. unsigned long now = jiffies;
  530. struct drbd_request *r1, *r2;
  531. /* BUMP me if you change the file format/content/presentation */
  532. seq_printf(m, "v: %u\n\n", 0);
  533. spin_lock_irq(&connection->resource->req_lock);
  534. r1 = connection->req_next;
  535. if (r1)
  536. seq_print_minor_vnr_req(m, r1, now);
  537. r2 = connection->req_ack_pending;
  538. if (r2 && r2 != r1) {
  539. r1 = r2;
  540. seq_print_minor_vnr_req(m, r1, now);
  541. }
  542. r2 = connection->req_not_net_done;
  543. if (r2 && r2 != r1)
  544. seq_print_minor_vnr_req(m, r2, now);
  545. spin_unlock_irq(&connection->resource->req_lock);
  546. return 0;
  547. }
  548. static int connection_oldest_requests_open(struct inode *inode, struct file *file)
  549. {
  550. struct drbd_connection *connection = inode->i_private;
  551. return drbd_single_open(file, connection_oldest_requests_show, connection,
  552. &connection->kref, drbd_destroy_connection);
  553. }
  554. static int connection_oldest_requests_release(struct inode *inode, struct file *file)
  555. {
  556. struct drbd_connection *connection = inode->i_private;
  557. kref_put(&connection->kref, drbd_destroy_connection);
  558. return single_release(inode, file);
  559. }
  560. static const struct file_operations connection_oldest_requests_fops = {
  561. .owner = THIS_MODULE,
  562. .open = connection_oldest_requests_open,
  563. .read = seq_read,
  564. .llseek = seq_lseek,
  565. .release = connection_oldest_requests_release,
  566. };
  567. void drbd_debugfs_connection_add(struct drbd_connection *connection)
  568. {
  569. struct dentry *conns_dir = connection->resource->debugfs_res_connections;
  570. struct dentry *dentry;
  571. if (!conns_dir)
  572. return;
  573. /* Once we enable mutliple peers,
  574. * these connections will have descriptive names.
  575. * For now, it is just the one connection to the (only) "peer". */
  576. dentry = debugfs_create_dir("peer", conns_dir);
  577. if (IS_ERR_OR_NULL(dentry))
  578. goto fail;
  579. connection->debugfs_conn = dentry;
  580. dentry = debugfs_create_file("callback_history", S_IRUSR|S_IRGRP,
  581. connection->debugfs_conn, connection,
  582. &connection_callback_history_fops);
  583. if (IS_ERR_OR_NULL(dentry))
  584. goto fail;
  585. connection->debugfs_conn_callback_history = dentry;
  586. dentry = debugfs_create_file("oldest_requests", S_IRUSR|S_IRGRP,
  587. connection->debugfs_conn, connection,
  588. &connection_oldest_requests_fops);
  589. if (IS_ERR_OR_NULL(dentry))
  590. goto fail;
  591. connection->debugfs_conn_oldest_requests = dentry;
  592. return;
  593. fail:
  594. drbd_debugfs_connection_cleanup(connection);
  595. drbd_err(connection, "failed to create debugfs dentry\n");
  596. }
  597. void drbd_debugfs_connection_cleanup(struct drbd_connection *connection)
  598. {
  599. drbd_debugfs_remove(&connection->debugfs_conn_callback_history);
  600. drbd_debugfs_remove(&connection->debugfs_conn_oldest_requests);
  601. drbd_debugfs_remove(&connection->debugfs_conn);
  602. }
  603. static void resync_dump_detail(struct seq_file *m, struct lc_element *e)
  604. {
  605. struct bm_extent *bme = lc_entry(e, struct bm_extent, lce);
  606. seq_printf(m, "%5d %s %s %s", bme->rs_left,
  607. test_bit(BME_NO_WRITES, &bme->flags) ? "NO_WRITES" : "---------",
  608. test_bit(BME_LOCKED, &bme->flags) ? "LOCKED" : "------",
  609. test_bit(BME_PRIORITY, &bme->flags) ? "PRIORITY" : "--------"
  610. );
  611. }
  612. static int device_resync_extents_show(struct seq_file *m, void *ignored)
  613. {
  614. struct drbd_device *device = m->private;
  615. /* BUMP me if you change the file format/content/presentation */
  616. seq_printf(m, "v: %u\n\n", 0);
  617. if (get_ldev_if_state(device, D_FAILED)) {
  618. lc_seq_printf_stats(m, device->resync);
  619. lc_seq_dump_details(m, device->resync, "rs_left flags", resync_dump_detail);
  620. put_ldev(device);
  621. }
  622. return 0;
  623. }
  624. static int device_act_log_extents_show(struct seq_file *m, void *ignored)
  625. {
  626. struct drbd_device *device = m->private;
  627. /* BUMP me if you change the file format/content/presentation */
  628. seq_printf(m, "v: %u\n\n", 0);
  629. if (get_ldev_if_state(device, D_FAILED)) {
  630. lc_seq_printf_stats(m, device->act_log);
  631. lc_seq_dump_details(m, device->act_log, "", NULL);
  632. put_ldev(device);
  633. }
  634. return 0;
  635. }
  636. static int device_oldest_requests_show(struct seq_file *m, void *ignored)
  637. {
  638. struct drbd_device *device = m->private;
  639. struct drbd_resource *resource = device->resource;
  640. unsigned long now = jiffies;
  641. struct drbd_request *r1, *r2;
  642. int i;
  643. /* BUMP me if you change the file format/content/presentation */
  644. seq_printf(m, "v: %u\n\n", 0);
  645. seq_puts(m, RQ_HDR);
  646. spin_lock_irq(&resource->req_lock);
  647. /* WRITE, then READ */
  648. for (i = 1; i >= 0; --i) {
  649. r1 = list_first_entry_or_null(&device->pending_master_completion[i],
  650. struct drbd_request, req_pending_master_completion);
  651. r2 = list_first_entry_or_null(&device->pending_completion[i],
  652. struct drbd_request, req_pending_local);
  653. if (r1)
  654. seq_print_one_request(m, r1, now);
  655. if (r2 && r2 != r1)
  656. seq_print_one_request(m, r2, now);
  657. }
  658. spin_unlock_irq(&resource->req_lock);
  659. return 0;
  660. }
  661. static int device_data_gen_id_show(struct seq_file *m, void *ignored)
  662. {
  663. struct drbd_device *device = m->private;
  664. struct drbd_md *md;
  665. enum drbd_uuid_index idx;
  666. if (!get_ldev_if_state(device, D_FAILED))
  667. return -ENODEV;
  668. md = &device->ldev->md;
  669. spin_lock_irq(&md->uuid_lock);
  670. for (idx = UI_CURRENT; idx <= UI_HISTORY_END; idx++) {
  671. seq_printf(m, "0x%016llX\n", md->uuid[idx]);
  672. }
  673. spin_unlock_irq(&md->uuid_lock);
  674. put_ldev(device);
  675. return 0;
  676. }
  677. #define drbd_debugfs_device_attr(name) \
  678. static int device_ ## name ## _open(struct inode *inode, struct file *file) \
  679. { \
  680. struct drbd_device *device = inode->i_private; \
  681. return drbd_single_open(file, device_ ## name ## _show, device, \
  682. &device->kref, drbd_destroy_device); \
  683. } \
  684. static int device_ ## name ## _release(struct inode *inode, struct file *file) \
  685. { \
  686. struct drbd_device *device = inode->i_private; \
  687. kref_put(&device->kref, drbd_destroy_device); \
  688. return single_release(inode, file); \
  689. } \
  690. static const struct file_operations device_ ## name ## _fops = { \
  691. .owner = THIS_MODULE, \
  692. .open = device_ ## name ## _open, \
  693. .read = seq_read, \
  694. .llseek = seq_lseek, \
  695. .release = device_ ## name ## _release, \
  696. };
  697. drbd_debugfs_device_attr(oldest_requests)
  698. drbd_debugfs_device_attr(act_log_extents)
  699. drbd_debugfs_device_attr(resync_extents)
  700. drbd_debugfs_device_attr(data_gen_id)
  701. void drbd_debugfs_device_add(struct drbd_device *device)
  702. {
  703. struct dentry *vols_dir = device->resource->debugfs_res_volumes;
  704. char minor_buf[8]; /* MINORMASK, MINORBITS == 20; */
  705. char vnr_buf[8]; /* volume number vnr is even 16 bit only; */
  706. char *slink_name = NULL;
  707. struct dentry *dentry;
  708. if (!vols_dir || !drbd_debugfs_minors)
  709. return;
  710. snprintf(vnr_buf, sizeof(vnr_buf), "%u", device->vnr);
  711. dentry = debugfs_create_dir(vnr_buf, vols_dir);
  712. if (IS_ERR_OR_NULL(dentry))
  713. goto fail;
  714. device->debugfs_vol = dentry;
  715. snprintf(minor_buf, sizeof(minor_buf), "%u", device->minor);
  716. slink_name = kasprintf(GFP_KERNEL, "../resources/%s/volumes/%u",
  717. device->resource->name, device->vnr);
  718. if (!slink_name)
  719. goto fail;
  720. dentry = debugfs_create_symlink(minor_buf, drbd_debugfs_minors, slink_name);
  721. kfree(slink_name);
  722. slink_name = NULL;
  723. if (IS_ERR_OR_NULL(dentry))
  724. goto fail;
  725. device->debugfs_minor = dentry;
  726. #define DCF(name) do { \
  727. dentry = debugfs_create_file(#name, S_IRUSR|S_IRGRP, \
  728. device->debugfs_vol, device, \
  729. &device_ ## name ## _fops); \
  730. if (IS_ERR_OR_NULL(dentry)) \
  731. goto fail; \
  732. device->debugfs_vol_ ## name = dentry; \
  733. } while (0)
  734. DCF(oldest_requests);
  735. DCF(act_log_extents);
  736. DCF(resync_extents);
  737. DCF(data_gen_id);
  738. #undef DCF
  739. return;
  740. fail:
  741. drbd_debugfs_device_cleanup(device);
  742. drbd_err(device, "failed to create debugfs entries\n");
  743. }
  744. void drbd_debugfs_device_cleanup(struct drbd_device *device)
  745. {
  746. drbd_debugfs_remove(&device->debugfs_minor);
  747. drbd_debugfs_remove(&device->debugfs_vol_oldest_requests);
  748. drbd_debugfs_remove(&device->debugfs_vol_act_log_extents);
  749. drbd_debugfs_remove(&device->debugfs_vol_resync_extents);
  750. drbd_debugfs_remove(&device->debugfs_vol_data_gen_id);
  751. drbd_debugfs_remove(&device->debugfs_vol);
  752. }
  753. void drbd_debugfs_peer_device_add(struct drbd_peer_device *peer_device)
  754. {
  755. struct dentry *conn_dir = peer_device->connection->debugfs_conn;
  756. struct dentry *dentry;
  757. char vnr_buf[8];
  758. if (!conn_dir)
  759. return;
  760. snprintf(vnr_buf, sizeof(vnr_buf), "%u", peer_device->device->vnr);
  761. dentry = debugfs_create_dir(vnr_buf, conn_dir);
  762. if (IS_ERR_OR_NULL(dentry))
  763. goto fail;
  764. peer_device->debugfs_peer_dev = dentry;
  765. return;
  766. fail:
  767. drbd_debugfs_peer_device_cleanup(peer_device);
  768. drbd_err(peer_device, "failed to create debugfs entries\n");
  769. }
  770. void drbd_debugfs_peer_device_cleanup(struct drbd_peer_device *peer_device)
  771. {
  772. drbd_debugfs_remove(&peer_device->debugfs_peer_dev);
  773. }
  774. static int drbd_version_show(struct seq_file *m, void *ignored)
  775. {
  776. seq_printf(m, "# %s\n", drbd_buildtag());
  777. seq_printf(m, "VERSION=%s\n", REL_VERSION);
  778. seq_printf(m, "API_VERSION=%u\n", API_VERSION);
  779. seq_printf(m, "PRO_VERSION_MIN=%u\n", PRO_VERSION_MIN);
  780. seq_printf(m, "PRO_VERSION_MAX=%u\n", PRO_VERSION_MAX);
  781. return 0;
  782. }
  783. static int drbd_version_open(struct inode *inode, struct file *file)
  784. {
  785. return single_open(file, drbd_version_show, NULL);
  786. }
  787. static struct file_operations drbd_version_fops = {
  788. .owner = THIS_MODULE,
  789. .open = drbd_version_open,
  790. .llseek = seq_lseek,
  791. .read = seq_read,
  792. .release = single_release,
  793. };
  794. /* not __exit, may be indirectly called
  795. * from the module-load-failure path as well. */
  796. void drbd_debugfs_cleanup(void)
  797. {
  798. drbd_debugfs_remove(&drbd_debugfs_resources);
  799. drbd_debugfs_remove(&drbd_debugfs_minors);
  800. drbd_debugfs_remove(&drbd_debugfs_version);
  801. drbd_debugfs_remove(&drbd_debugfs_root);
  802. }
  803. int __init drbd_debugfs_init(void)
  804. {
  805. struct dentry *dentry;
  806. dentry = debugfs_create_dir("drbd", NULL);
  807. if (IS_ERR_OR_NULL(dentry))
  808. goto fail;
  809. drbd_debugfs_root = dentry;
  810. dentry = debugfs_create_file("version", 0444, drbd_debugfs_root, NULL, &drbd_version_fops);
  811. if (IS_ERR_OR_NULL(dentry))
  812. goto fail;
  813. drbd_debugfs_version = dentry;
  814. dentry = debugfs_create_dir("resources", drbd_debugfs_root);
  815. if (IS_ERR_OR_NULL(dentry))
  816. goto fail;
  817. drbd_debugfs_resources = dentry;
  818. dentry = debugfs_create_dir("minors", drbd_debugfs_root);
  819. if (IS_ERR_OR_NULL(dentry))
  820. goto fail;
  821. drbd_debugfs_minors = dentry;
  822. return 0;
  823. fail:
  824. drbd_debugfs_cleanup();
  825. if (dentry)
  826. return PTR_ERR(dentry);
  827. else
  828. return -EINVAL;
  829. }