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