drbd_debugfs.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  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. /* simple_positive(file->f_dentry) respectively debugfs_positive(),
  374. * but neither is "reachable" from here.
  375. * So we have our own inline version of it above. :-( */
  376. static inline int debugfs_positive(struct dentry *dentry)
  377. {
  378. return dentry->d_inode && !d_unhashed(dentry);
  379. }
  380. /* make sure at *open* time that the respective object won't go away. */
  381. static int drbd_single_open(struct file *file, int (*show)(struct seq_file *, void *),
  382. void *data, struct kref *kref,
  383. void (*release)(struct kref *))
  384. {
  385. struct dentry *parent;
  386. int ret = -ESTALE;
  387. /* Are we still linked,
  388. * or has debugfs_remove() already been called? */
  389. parent = file->f_dentry->d_parent;
  390. /* not sure if this can happen: */
  391. if (!parent || !parent->d_inode)
  392. goto out;
  393. /* serialize with d_delete() */
  394. mutex_lock(&parent->d_inode->i_mutex);
  395. /* Make sure the object is still alive */
  396. if (debugfs_positive(file->f_dentry)
  397. && kref_get_unless_zero(kref))
  398. ret = 0;
  399. mutex_unlock(&parent->d_inode->i_mutex);
  400. if (!ret) {
  401. ret = single_open(file, show, data);
  402. if (ret)
  403. kref_put(kref, release);
  404. }
  405. out:
  406. return ret;
  407. }
  408. static int in_flight_summary_open(struct inode *inode, struct file *file)
  409. {
  410. struct drbd_resource *resource = inode->i_private;
  411. return drbd_single_open(file, in_flight_summary_show, resource,
  412. &resource->kref, drbd_destroy_resource);
  413. }
  414. static int in_flight_summary_release(struct inode *inode, struct file *file)
  415. {
  416. struct drbd_resource *resource = inode->i_private;
  417. kref_put(&resource->kref, drbd_destroy_resource);
  418. return single_release(inode, file);
  419. }
  420. static const struct file_operations in_flight_summary_fops = {
  421. .owner = THIS_MODULE,
  422. .open = in_flight_summary_open,
  423. .read = seq_read,
  424. .llseek = seq_lseek,
  425. .release = in_flight_summary_release,
  426. };
  427. void drbd_debugfs_resource_add(struct drbd_resource *resource)
  428. {
  429. struct dentry *dentry;
  430. if (!drbd_debugfs_resources)
  431. return;
  432. dentry = debugfs_create_dir(resource->name, drbd_debugfs_resources);
  433. if (IS_ERR_OR_NULL(dentry))
  434. goto fail;
  435. resource->debugfs_res = dentry;
  436. dentry = debugfs_create_dir("volumes", resource->debugfs_res);
  437. if (IS_ERR_OR_NULL(dentry))
  438. goto fail;
  439. resource->debugfs_res_volumes = dentry;
  440. dentry = debugfs_create_dir("connections", resource->debugfs_res);
  441. if (IS_ERR_OR_NULL(dentry))
  442. goto fail;
  443. resource->debugfs_res_connections = dentry;
  444. dentry = debugfs_create_file("in_flight_summary", S_IRUSR|S_IRGRP,
  445. resource->debugfs_res, resource,
  446. &in_flight_summary_fops);
  447. if (IS_ERR_OR_NULL(dentry))
  448. goto fail;
  449. resource->debugfs_res_in_flight_summary = dentry;
  450. return;
  451. fail:
  452. drbd_debugfs_resource_cleanup(resource);
  453. drbd_err(resource, "failed to create debugfs dentry\n");
  454. }
  455. static void drbd_debugfs_remove(struct dentry **dp)
  456. {
  457. debugfs_remove(*dp);
  458. *dp = NULL;
  459. }
  460. void drbd_debugfs_resource_cleanup(struct drbd_resource *resource)
  461. {
  462. /* it is ok to call debugfs_remove(NULL) */
  463. drbd_debugfs_remove(&resource->debugfs_res_in_flight_summary);
  464. drbd_debugfs_remove(&resource->debugfs_res_connections);
  465. drbd_debugfs_remove(&resource->debugfs_res_volumes);
  466. drbd_debugfs_remove(&resource->debugfs_res);
  467. }
  468. static void seq_print_one_timing_detail(struct seq_file *m,
  469. const struct drbd_thread_timing_details *tdp,
  470. unsigned long now)
  471. {
  472. struct drbd_thread_timing_details td;
  473. /* No locking...
  474. * use temporary assignment to get at consistent data. */
  475. do {
  476. td = *tdp;
  477. } while (td.cb_nr != tdp->cb_nr);
  478. if (!td.cb_addr)
  479. return;
  480. seq_printf(m, "%u\t%d\t%s:%u\t%ps\n",
  481. td.cb_nr,
  482. jiffies_to_msecs(now - td.start_jif),
  483. td.caller_fn, td.line,
  484. td.cb_addr);
  485. }
  486. static void seq_print_timing_details(struct seq_file *m,
  487. const char *title,
  488. unsigned int cb_nr, struct drbd_thread_timing_details *tdp, unsigned long now)
  489. {
  490. unsigned int start_idx;
  491. unsigned int i;
  492. seq_printf(m, "%s\n", title);
  493. /* If not much is going on, this will result in natural ordering.
  494. * If it is very busy, we will possibly skip events, or even see wrap
  495. * arounds, which could only be avoided with locking.
  496. */
  497. start_idx = cb_nr % DRBD_THREAD_DETAILS_HIST;
  498. for (i = start_idx; i < DRBD_THREAD_DETAILS_HIST; i++)
  499. seq_print_one_timing_detail(m, tdp+i, now);
  500. for (i = 0; i < start_idx; i++)
  501. seq_print_one_timing_detail(m, tdp+i, now);
  502. }
  503. static int callback_history_show(struct seq_file *m, void *ignored)
  504. {
  505. struct drbd_connection *connection = m->private;
  506. unsigned long jif = jiffies;
  507. /* BUMP me if you change the file format/content/presentation */
  508. seq_printf(m, "v: %u\n\n", 0);
  509. seq_puts(m, "n\tage\tcallsite\tfn\n");
  510. seq_print_timing_details(m, "worker", connection->w_cb_nr, connection->w_timing_details, jif);
  511. seq_print_timing_details(m, "receiver", connection->r_cb_nr, connection->r_timing_details, jif);
  512. return 0;
  513. }
  514. static int callback_history_open(struct inode *inode, struct file *file)
  515. {
  516. struct drbd_connection *connection = inode->i_private;
  517. return drbd_single_open(file, callback_history_show, connection,
  518. &connection->kref, drbd_destroy_connection);
  519. }
  520. static int callback_history_release(struct inode *inode, struct file *file)
  521. {
  522. struct drbd_connection *connection = inode->i_private;
  523. kref_put(&connection->kref, drbd_destroy_connection);
  524. return single_release(inode, file);
  525. }
  526. static const struct file_operations connection_callback_history_fops = {
  527. .owner = THIS_MODULE,
  528. .open = callback_history_open,
  529. .read = seq_read,
  530. .llseek = seq_lseek,
  531. .release = callback_history_release,
  532. };
  533. static int connection_oldest_requests_show(struct seq_file *m, void *ignored)
  534. {
  535. struct drbd_connection *connection = m->private;
  536. unsigned long now = jiffies;
  537. struct drbd_request *r1, *r2;
  538. /* BUMP me if you change the file format/content/presentation */
  539. seq_printf(m, "v: %u\n\n", 0);
  540. spin_lock_irq(&connection->resource->req_lock);
  541. r1 = connection->req_next;
  542. if (r1)
  543. seq_print_minor_vnr_req(m, r1, now);
  544. r2 = connection->req_ack_pending;
  545. if (r2 && r2 != r1) {
  546. r1 = r2;
  547. seq_print_minor_vnr_req(m, r1, now);
  548. }
  549. r2 = connection->req_not_net_done;
  550. if (r2 && r2 != r1)
  551. seq_print_minor_vnr_req(m, r2, now);
  552. spin_unlock_irq(&connection->resource->req_lock);
  553. return 0;
  554. }
  555. static int connection_oldest_requests_open(struct inode *inode, struct file *file)
  556. {
  557. struct drbd_connection *connection = inode->i_private;
  558. return drbd_single_open(file, connection_oldest_requests_show, connection,
  559. &connection->kref, drbd_destroy_connection);
  560. }
  561. static int connection_oldest_requests_release(struct inode *inode, struct file *file)
  562. {
  563. struct drbd_connection *connection = inode->i_private;
  564. kref_put(&connection->kref, drbd_destroy_connection);
  565. return single_release(inode, file);
  566. }
  567. static const struct file_operations connection_oldest_requests_fops = {
  568. .owner = THIS_MODULE,
  569. .open = connection_oldest_requests_open,
  570. .read = seq_read,
  571. .llseek = seq_lseek,
  572. .release = connection_oldest_requests_release,
  573. };
  574. void drbd_debugfs_connection_add(struct drbd_connection *connection)
  575. {
  576. struct dentry *conns_dir = connection->resource->debugfs_res_connections;
  577. struct dentry *dentry;
  578. if (!conns_dir)
  579. return;
  580. /* Once we enable mutliple peers,
  581. * these connections will have descriptive names.
  582. * For now, it is just the one connection to the (only) "peer". */
  583. dentry = debugfs_create_dir("peer", conns_dir);
  584. if (IS_ERR_OR_NULL(dentry))
  585. goto fail;
  586. connection->debugfs_conn = dentry;
  587. dentry = debugfs_create_file("callback_history", S_IRUSR|S_IRGRP,
  588. connection->debugfs_conn, connection,
  589. &connection_callback_history_fops);
  590. if (IS_ERR_OR_NULL(dentry))
  591. goto fail;
  592. connection->debugfs_conn_callback_history = dentry;
  593. dentry = debugfs_create_file("oldest_requests", S_IRUSR|S_IRGRP,
  594. connection->debugfs_conn, connection,
  595. &connection_oldest_requests_fops);
  596. if (IS_ERR_OR_NULL(dentry))
  597. goto fail;
  598. connection->debugfs_conn_oldest_requests = dentry;
  599. return;
  600. fail:
  601. drbd_debugfs_connection_cleanup(connection);
  602. drbd_err(connection, "failed to create debugfs dentry\n");
  603. }
  604. void drbd_debugfs_connection_cleanup(struct drbd_connection *connection)
  605. {
  606. drbd_debugfs_remove(&connection->debugfs_conn_callback_history);
  607. drbd_debugfs_remove(&connection->debugfs_conn_oldest_requests);
  608. drbd_debugfs_remove(&connection->debugfs_conn);
  609. }
  610. static void resync_dump_detail(struct seq_file *m, struct lc_element *e)
  611. {
  612. struct bm_extent *bme = lc_entry(e, struct bm_extent, lce);
  613. seq_printf(m, "%5d %s %s %s", bme->rs_left,
  614. test_bit(BME_NO_WRITES, &bme->flags) ? "NO_WRITES" : "---------",
  615. test_bit(BME_LOCKED, &bme->flags) ? "LOCKED" : "------",
  616. test_bit(BME_PRIORITY, &bme->flags) ? "PRIORITY" : "--------"
  617. );
  618. }
  619. static int device_resync_extents_show(struct seq_file *m, void *ignored)
  620. {
  621. struct drbd_device *device = m->private;
  622. /* BUMP me if you change the file format/content/presentation */
  623. seq_printf(m, "v: %u\n\n", 0);
  624. if (get_ldev_if_state(device, D_FAILED)) {
  625. lc_seq_printf_stats(m, device->resync);
  626. lc_seq_dump_details(m, device->resync, "rs_left flags", resync_dump_detail);
  627. put_ldev(device);
  628. }
  629. return 0;
  630. }
  631. static int device_act_log_extents_show(struct seq_file *m, void *ignored)
  632. {
  633. struct drbd_device *device = m->private;
  634. /* BUMP me if you change the file format/content/presentation */
  635. seq_printf(m, "v: %u\n\n", 0);
  636. if (get_ldev_if_state(device, D_FAILED)) {
  637. lc_seq_printf_stats(m, device->act_log);
  638. lc_seq_dump_details(m, device->act_log, "", NULL);
  639. put_ldev(device);
  640. }
  641. return 0;
  642. }
  643. static int device_oldest_requests_show(struct seq_file *m, void *ignored)
  644. {
  645. struct drbd_device *device = m->private;
  646. struct drbd_resource *resource = device->resource;
  647. unsigned long now = jiffies;
  648. struct drbd_request *r1, *r2;
  649. int i;
  650. /* BUMP me if you change the file format/content/presentation */
  651. seq_printf(m, "v: %u\n\n", 0);
  652. seq_puts(m, RQ_HDR);
  653. spin_lock_irq(&resource->req_lock);
  654. /* WRITE, then READ */
  655. for (i = 1; i >= 0; --i) {
  656. r1 = list_first_entry_or_null(&device->pending_master_completion[i],
  657. struct drbd_request, req_pending_master_completion);
  658. r2 = list_first_entry_or_null(&device->pending_completion[i],
  659. struct drbd_request, req_pending_local);
  660. if (r1)
  661. seq_print_one_request(m, r1, now);
  662. if (r2 && r2 != r1)
  663. seq_print_one_request(m, r2, now);
  664. }
  665. spin_unlock_irq(&resource->req_lock);
  666. return 0;
  667. }
  668. static int device_data_gen_id_show(struct seq_file *m, void *ignored)
  669. {
  670. struct drbd_device *device = m->private;
  671. struct drbd_md *md;
  672. enum drbd_uuid_index idx;
  673. if (!get_ldev_if_state(device, D_FAILED))
  674. return -ENODEV;
  675. md = &device->ldev->md;
  676. spin_lock_irq(&md->uuid_lock);
  677. for (idx = UI_CURRENT; idx <= UI_HISTORY_END; idx++) {
  678. seq_printf(m, "0x%016llX\n", md->uuid[idx]);
  679. }
  680. spin_unlock_irq(&md->uuid_lock);
  681. put_ldev(device);
  682. return 0;
  683. }
  684. #define drbd_debugfs_device_attr(name) \
  685. static int device_ ## name ## _open(struct inode *inode, struct file *file) \
  686. { \
  687. struct drbd_device *device = inode->i_private; \
  688. return drbd_single_open(file, device_ ## name ## _show, device, \
  689. &device->kref, drbd_destroy_device); \
  690. } \
  691. static int device_ ## name ## _release(struct inode *inode, struct file *file) \
  692. { \
  693. struct drbd_device *device = inode->i_private; \
  694. kref_put(&device->kref, drbd_destroy_device); \
  695. return single_release(inode, file); \
  696. } \
  697. static const struct file_operations device_ ## name ## _fops = { \
  698. .owner = THIS_MODULE, \
  699. .open = device_ ## name ## _open, \
  700. .read = seq_read, \
  701. .llseek = seq_lseek, \
  702. .release = device_ ## name ## _release, \
  703. };
  704. drbd_debugfs_device_attr(oldest_requests)
  705. drbd_debugfs_device_attr(act_log_extents)
  706. drbd_debugfs_device_attr(resync_extents)
  707. drbd_debugfs_device_attr(data_gen_id)
  708. void drbd_debugfs_device_add(struct drbd_device *device)
  709. {
  710. struct dentry *vols_dir = device->resource->debugfs_res_volumes;
  711. char minor_buf[8]; /* MINORMASK, MINORBITS == 20; */
  712. char vnr_buf[8]; /* volume number vnr is even 16 bit only; */
  713. char *slink_name = NULL;
  714. struct dentry *dentry;
  715. if (!vols_dir || !drbd_debugfs_minors)
  716. return;
  717. snprintf(vnr_buf, sizeof(vnr_buf), "%u", device->vnr);
  718. dentry = debugfs_create_dir(vnr_buf, vols_dir);
  719. if (IS_ERR_OR_NULL(dentry))
  720. goto fail;
  721. device->debugfs_vol = dentry;
  722. snprintf(minor_buf, sizeof(minor_buf), "%u", device->minor);
  723. slink_name = kasprintf(GFP_KERNEL, "../resources/%s/volumes/%u",
  724. device->resource->name, device->vnr);
  725. if (!slink_name)
  726. goto fail;
  727. dentry = debugfs_create_symlink(minor_buf, drbd_debugfs_minors, slink_name);
  728. kfree(slink_name);
  729. slink_name = NULL;
  730. if (IS_ERR_OR_NULL(dentry))
  731. goto fail;
  732. device->debugfs_minor = dentry;
  733. #define DCF(name) do { \
  734. dentry = debugfs_create_file(#name, S_IRUSR|S_IRGRP, \
  735. device->debugfs_vol, device, \
  736. &device_ ## name ## _fops); \
  737. if (IS_ERR_OR_NULL(dentry)) \
  738. goto fail; \
  739. device->debugfs_vol_ ## name = dentry; \
  740. } while (0)
  741. DCF(oldest_requests);
  742. DCF(act_log_extents);
  743. DCF(resync_extents);
  744. DCF(data_gen_id);
  745. #undef DCF
  746. return;
  747. fail:
  748. drbd_debugfs_device_cleanup(device);
  749. drbd_err(device, "failed to create debugfs entries\n");
  750. }
  751. void drbd_debugfs_device_cleanup(struct drbd_device *device)
  752. {
  753. drbd_debugfs_remove(&device->debugfs_minor);
  754. drbd_debugfs_remove(&device->debugfs_vol_oldest_requests);
  755. drbd_debugfs_remove(&device->debugfs_vol_act_log_extents);
  756. drbd_debugfs_remove(&device->debugfs_vol_resync_extents);
  757. drbd_debugfs_remove(&device->debugfs_vol_data_gen_id);
  758. drbd_debugfs_remove(&device->debugfs_vol);
  759. }
  760. void drbd_debugfs_peer_device_add(struct drbd_peer_device *peer_device)
  761. {
  762. struct dentry *conn_dir = peer_device->connection->debugfs_conn;
  763. struct dentry *dentry;
  764. char vnr_buf[8];
  765. if (!conn_dir)
  766. return;
  767. snprintf(vnr_buf, sizeof(vnr_buf), "%u", peer_device->device->vnr);
  768. dentry = debugfs_create_dir(vnr_buf, conn_dir);
  769. if (IS_ERR_OR_NULL(dentry))
  770. goto fail;
  771. peer_device->debugfs_peer_dev = dentry;
  772. return;
  773. fail:
  774. drbd_debugfs_peer_device_cleanup(peer_device);
  775. drbd_err(peer_device, "failed to create debugfs entries\n");
  776. }
  777. void drbd_debugfs_peer_device_cleanup(struct drbd_peer_device *peer_device)
  778. {
  779. drbd_debugfs_remove(&peer_device->debugfs_peer_dev);
  780. }
  781. static int drbd_version_show(struct seq_file *m, void *ignored)
  782. {
  783. seq_printf(m, "# %s\n", drbd_buildtag());
  784. seq_printf(m, "VERSION=%s\n", REL_VERSION);
  785. seq_printf(m, "API_VERSION=%u\n", API_VERSION);
  786. seq_printf(m, "PRO_VERSION_MIN=%u\n", PRO_VERSION_MIN);
  787. seq_printf(m, "PRO_VERSION_MAX=%u\n", PRO_VERSION_MAX);
  788. return 0;
  789. }
  790. static int drbd_version_open(struct inode *inode, struct file *file)
  791. {
  792. return single_open(file, drbd_version_show, NULL);
  793. }
  794. static struct file_operations drbd_version_fops = {
  795. .owner = THIS_MODULE,
  796. .open = drbd_version_open,
  797. .llseek = seq_lseek,
  798. .read = seq_read,
  799. .release = single_release,
  800. };
  801. /* not __exit, may be indirectly called
  802. * from the module-load-failure path as well. */
  803. void drbd_debugfs_cleanup(void)
  804. {
  805. drbd_debugfs_remove(&drbd_debugfs_resources);
  806. drbd_debugfs_remove(&drbd_debugfs_minors);
  807. drbd_debugfs_remove(&drbd_debugfs_version);
  808. drbd_debugfs_remove(&drbd_debugfs_root);
  809. }
  810. int __init drbd_debugfs_init(void)
  811. {
  812. struct dentry *dentry;
  813. dentry = debugfs_create_dir("drbd", NULL);
  814. if (IS_ERR_OR_NULL(dentry))
  815. goto fail;
  816. drbd_debugfs_root = dentry;
  817. dentry = debugfs_create_file("version", 0444, drbd_debugfs_root, NULL, &drbd_version_fops);
  818. if (IS_ERR_OR_NULL(dentry))
  819. goto fail;
  820. drbd_debugfs_version = dentry;
  821. dentry = debugfs_create_dir("resources", drbd_debugfs_root);
  822. if (IS_ERR_OR_NULL(dentry))
  823. goto fail;
  824. drbd_debugfs_resources = dentry;
  825. dentry = debugfs_create_dir("minors", drbd_debugfs_root);
  826. if (IS_ERR_OR_NULL(dentry))
  827. goto fail;
  828. drbd_debugfs_minors = dentry;
  829. return 0;
  830. fail:
  831. drbd_debugfs_cleanup();
  832. if (dentry)
  833. return PTR_ERR(dentry);
  834. else
  835. return -EINVAL;
  836. }