Merge tag 'leds_for_4.8' of git://git.kernel.org/pub/scm/linux/kernel/git/j.anaszewsk...
[sfrench/cifs-2.6.git] / drivers / block / drbd / drbd_debugfs.c
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
10 #include "drbd_int.h"
11 #include "drbd_req.h"
12 #include "drbd_debugfs.h"
13
14
15 /**********************************************************************
16  * Whenever you change the file format, remember to bump the version. *
17  **********************************************************************/
18
19 static struct dentry *drbd_debugfs_root;
20 static struct dentry *drbd_debugfs_version;
21 static struct dentry *drbd_debugfs_resources;
22 static struct dentry *drbd_debugfs_minors;
23
24 static 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
32 static void __seq_print_rq_state_bit(struct seq_file *m,
33         bool is_set, char *sep, const char *set_name, const char *unset_name)
34 {
35         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
46 static 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);
50 }
51
52 /* pretty print enum drbd_req_state_bits req->rq_state */
53 static 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
95 static 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
121 static 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
127 static 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
155 static 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
190 static 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
215 static 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 */
229 static 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");
239
240         if (f & EE_IS_TRIM)
241                 __seq_print_rq_state_bit(m, f & EE_IS_TRIM_USE_ZEROOUT, &sep, "zero-out", "trim");
242         seq_print_rq_state_bit(m, f & EE_WRITE_SAME, &sep, "write-same");
243         seq_putc(m, '\n');
244 }
245
246 static void seq_print_peer_request(struct seq_file *m,
247         struct drbd_device *device, struct list_head *lh,
248         unsigned long now)
249 {
250         bool reported_preparing = false;
251         struct drbd_peer_request *peer_req;
252         list_for_each_entry(peer_req, lh, w.list) {
253                 if (reported_preparing && !(peer_req->flags & EE_SUBMITTED))
254                         continue;
255
256                 if (device)
257                         seq_printf(m, "%u\t%u\t", device->minor, device->vnr);
258
259                 seq_printf(m, "%llu\t%u\t%c\t%u\t",
260                         (unsigned long long)peer_req->i.sector, peer_req->i.size >> 9,
261                         (peer_req->flags & EE_WRITE) ? 'W' : 'R',
262                         jiffies_to_msecs(now - peer_req->submit_jif));
263                 seq_print_peer_request_flags(m, peer_req);
264                 if (peer_req->flags & EE_SUBMITTED)
265                         break;
266                 else
267                         reported_preparing = true;
268         }
269 }
270
271 static void seq_print_device_peer_requests(struct seq_file *m,
272         struct drbd_device *device, unsigned long now)
273 {
274         seq_puts(m, "minor\tvnr\tsector\tsize\trw\tage\tflags\n");
275         spin_lock_irq(&device->resource->req_lock);
276         seq_print_peer_request(m, device, &device->active_ee, now);
277         seq_print_peer_request(m, device, &device->read_ee, now);
278         seq_print_peer_request(m, device, &device->sync_ee, now);
279         spin_unlock_irq(&device->resource->req_lock);
280         if (test_bit(FLUSH_PENDING, &device->flags)) {
281                 seq_printf(m, "%u\t%u\t-\t-\tF\t%u\tflush\n",
282                         device->minor, device->vnr,
283                         jiffies_to_msecs(now - device->flush_jif));
284         }
285 }
286
287 static void seq_print_resource_pending_peer_requests(struct seq_file *m,
288         struct drbd_resource *resource, unsigned long now)
289 {
290         struct drbd_device *device;
291         unsigned int i;
292
293         rcu_read_lock();
294         idr_for_each_entry(&resource->devices, device, i) {
295                 seq_print_device_peer_requests(m, device, now);
296         }
297         rcu_read_unlock();
298 }
299
300 static void seq_print_resource_transfer_log_summary(struct seq_file *m,
301         struct drbd_resource *resource,
302         struct drbd_connection *connection,
303         unsigned long now)
304 {
305         struct drbd_request *req;
306         unsigned int count = 0;
307         unsigned int show_state = 0;
308
309         seq_puts(m, "n\tdevice\tvnr\t" RQ_HDR);
310         spin_lock_irq(&resource->req_lock);
311         list_for_each_entry(req, &connection->transfer_log, tl_requests) {
312                 unsigned int tmp = 0;
313                 unsigned int s;
314                 ++count;
315
316                 /* don't disable irq "forever" */
317                 if (!(count & 0x1ff)) {
318                         struct drbd_request *req_next;
319                         kref_get(&req->kref);
320                         spin_unlock_irq(&resource->req_lock);
321                         cond_resched();
322                         spin_lock_irq(&resource->req_lock);
323                         req_next = list_next_entry(req, tl_requests);
324                         if (kref_put(&req->kref, drbd_req_destroy))
325                                 req = req_next;
326                         if (&req->tl_requests == &connection->transfer_log)
327                                 break;
328                 }
329
330                 s = req->rq_state;
331
332                 /* This is meant to summarize timing issues, to be able to tell
333                  * local disk problems from network problems.
334                  * Skip requests, if we have shown an even older request with
335                  * similar aspects already.  */
336                 if (req->master_bio == NULL)
337                         tmp |= 1;
338                 if ((s & RQ_LOCAL_MASK) && (s & RQ_LOCAL_PENDING))
339                         tmp |= 2;
340                 if (s & RQ_NET_MASK) {
341                         if (!(s & RQ_NET_SENT))
342                                 tmp |= 4;
343                         if (s & RQ_NET_PENDING)
344                                 tmp |= 8;
345                         if (!(s & RQ_NET_DONE))
346                                 tmp |= 16;
347                 }
348                 if ((tmp & show_state) == tmp)
349                         continue;
350                 show_state |= tmp;
351                 seq_printf(m, "%u\t", count);
352                 seq_print_minor_vnr_req(m, req, now);
353                 if (show_state == 0x1f)
354                         break;
355         }
356         spin_unlock_irq(&resource->req_lock);
357 }
358
359 /* TODO: transfer_log and friends should be moved to resource */
360 static int in_flight_summary_show(struct seq_file *m, void *pos)
361 {
362         struct drbd_resource *resource = m->private;
363         struct drbd_connection *connection;
364         unsigned long jif = jiffies;
365
366         connection = first_connection(resource);
367         /* This does not happen, actually.
368          * But be robust and prepare for future code changes. */
369         if (!connection || !kref_get_unless_zero(&connection->kref))
370                 return -ESTALE;
371
372         /* BUMP me if you change the file format/content/presentation */
373         seq_printf(m, "v: %u\n\n", 0);
374
375         seq_puts(m, "oldest bitmap IO\n");
376         seq_print_resource_pending_bitmap_io(m, resource, jif);
377         seq_putc(m, '\n');
378
379         seq_puts(m, "meta data IO\n");
380         seq_print_resource_pending_meta_io(m, resource, jif);
381         seq_putc(m, '\n');
382
383         seq_puts(m, "socket buffer stats\n");
384         /* for each connection ... once we have more than one */
385         rcu_read_lock();
386         if (connection->data.socket) {
387                 /* open coded SIOCINQ, the "relevant" part */
388                 struct tcp_sock *tp = tcp_sk(connection->data.socket->sk);
389                 int answ = tp->rcv_nxt - tp->copied_seq;
390                 seq_printf(m, "unread receive buffer: %u Byte\n", answ);
391                 /* open coded SIOCOUTQ, the "relevant" part */
392                 answ = tp->write_seq - tp->snd_una;
393                 seq_printf(m, "unacked send buffer: %u Byte\n", answ);
394         }
395         rcu_read_unlock();
396         seq_putc(m, '\n');
397
398         seq_puts(m, "oldest peer requests\n");
399         seq_print_resource_pending_peer_requests(m, resource, jif);
400         seq_putc(m, '\n');
401
402         seq_puts(m, "application requests waiting for activity log\n");
403         seq_print_waiting_for_AL(m, resource, jif);
404         seq_putc(m, '\n');
405
406         seq_puts(m, "oldest application requests\n");
407         seq_print_resource_transfer_log_summary(m, resource, connection, jif);
408         seq_putc(m, '\n');
409
410         jif = jiffies - jif;
411         if (jif)
412                 seq_printf(m, "generated in %d ms\n", jiffies_to_msecs(jif));
413         kref_put(&connection->kref, drbd_destroy_connection);
414         return 0;
415 }
416
417 /* make sure at *open* time that the respective object won't go away. */
418 static int drbd_single_open(struct file *file, int (*show)(struct seq_file *, void *),
419                                 void *data, struct kref *kref,
420                                 void (*release)(struct kref *))
421 {
422         struct dentry *parent;
423         int ret = -ESTALE;
424
425         /* Are we still linked,
426          * or has debugfs_remove() already been called? */
427         parent = file->f_path.dentry->d_parent;
428         /* not sure if this can happen: */
429         if (!parent || d_really_is_negative(parent))
430                 goto out;
431         /* serialize with d_delete() */
432         inode_lock(d_inode(parent));
433         /* Make sure the object is still alive */
434         if (simple_positive(file->f_path.dentry)
435         && kref_get_unless_zero(kref))
436                 ret = 0;
437         inode_unlock(d_inode(parent));
438         if (!ret) {
439                 ret = single_open(file, show, data);
440                 if (ret)
441                         kref_put(kref, release);
442         }
443 out:
444         return ret;
445 }
446
447 static int in_flight_summary_open(struct inode *inode, struct file *file)
448 {
449         struct drbd_resource *resource = inode->i_private;
450         return drbd_single_open(file, in_flight_summary_show, resource,
451                                 &resource->kref, drbd_destroy_resource);
452 }
453
454 static int in_flight_summary_release(struct inode *inode, struct file *file)
455 {
456         struct drbd_resource *resource = inode->i_private;
457         kref_put(&resource->kref, drbd_destroy_resource);
458         return single_release(inode, file);
459 }
460
461 static const struct file_operations in_flight_summary_fops = {
462         .owner          = THIS_MODULE,
463         .open           = in_flight_summary_open,
464         .read           = seq_read,
465         .llseek         = seq_lseek,
466         .release        = in_flight_summary_release,
467 };
468
469 void drbd_debugfs_resource_add(struct drbd_resource *resource)
470 {
471         struct dentry *dentry;
472         if (!drbd_debugfs_resources)
473                 return;
474
475         dentry = debugfs_create_dir(resource->name, drbd_debugfs_resources);
476         if (IS_ERR_OR_NULL(dentry))
477                 goto fail;
478         resource->debugfs_res = dentry;
479
480         dentry = debugfs_create_dir("volumes", resource->debugfs_res);
481         if (IS_ERR_OR_NULL(dentry))
482                 goto fail;
483         resource->debugfs_res_volumes = dentry;
484
485         dentry = debugfs_create_dir("connections", resource->debugfs_res);
486         if (IS_ERR_OR_NULL(dentry))
487                 goto fail;
488         resource->debugfs_res_connections = dentry;
489
490         dentry = debugfs_create_file("in_flight_summary", S_IRUSR|S_IRGRP,
491                         resource->debugfs_res, resource,
492                         &in_flight_summary_fops);
493         if (IS_ERR_OR_NULL(dentry))
494                 goto fail;
495         resource->debugfs_res_in_flight_summary = dentry;
496         return;
497
498 fail:
499         drbd_debugfs_resource_cleanup(resource);
500         drbd_err(resource, "failed to create debugfs dentry\n");
501 }
502
503 static void drbd_debugfs_remove(struct dentry **dp)
504 {
505         debugfs_remove(*dp);
506         *dp = NULL;
507 }
508
509 void drbd_debugfs_resource_cleanup(struct drbd_resource *resource)
510 {
511         /* it is ok to call debugfs_remove(NULL) */
512         drbd_debugfs_remove(&resource->debugfs_res_in_flight_summary);
513         drbd_debugfs_remove(&resource->debugfs_res_connections);
514         drbd_debugfs_remove(&resource->debugfs_res_volumes);
515         drbd_debugfs_remove(&resource->debugfs_res);
516 }
517
518 static void seq_print_one_timing_detail(struct seq_file *m,
519         const struct drbd_thread_timing_details *tdp,
520         unsigned long now)
521 {
522         struct drbd_thread_timing_details td;
523         /* No locking...
524          * use temporary assignment to get at consistent data. */
525         do {
526                 td = *tdp;
527         } while (td.cb_nr != tdp->cb_nr);
528         if (!td.cb_addr)
529                 return;
530         seq_printf(m, "%u\t%d\t%s:%u\t%ps\n",
531                         td.cb_nr,
532                         jiffies_to_msecs(now - td.start_jif),
533                         td.caller_fn, td.line,
534                         td.cb_addr);
535 }
536
537 static void seq_print_timing_details(struct seq_file *m,
538                 const char *title,
539                 unsigned int cb_nr, struct drbd_thread_timing_details *tdp, unsigned long now)
540 {
541         unsigned int start_idx;
542         unsigned int i;
543
544         seq_printf(m, "%s\n", title);
545         /* If not much is going on, this will result in natural ordering.
546          * If it is very busy, we will possibly skip events, or even see wrap
547          * arounds, which could only be avoided with locking.
548          */
549         start_idx = cb_nr % DRBD_THREAD_DETAILS_HIST;
550         for (i = start_idx; i < DRBD_THREAD_DETAILS_HIST; i++)
551                 seq_print_one_timing_detail(m, tdp+i, now);
552         for (i = 0; i < start_idx; i++)
553                 seq_print_one_timing_detail(m, tdp+i, now);
554 }
555
556 static int callback_history_show(struct seq_file *m, void *ignored)
557 {
558         struct drbd_connection *connection = m->private;
559         unsigned long jif = jiffies;
560
561         /* BUMP me if you change the file format/content/presentation */
562         seq_printf(m, "v: %u\n\n", 0);
563
564         seq_puts(m, "n\tage\tcallsite\tfn\n");
565         seq_print_timing_details(m, "worker", connection->w_cb_nr, connection->w_timing_details, jif);
566         seq_print_timing_details(m, "receiver", connection->r_cb_nr, connection->r_timing_details, jif);
567         return 0;
568 }
569
570 static int callback_history_open(struct inode *inode, struct file *file)
571 {
572         struct drbd_connection *connection = inode->i_private;
573         return drbd_single_open(file, callback_history_show, connection,
574                                 &connection->kref, drbd_destroy_connection);
575 }
576
577 static int callback_history_release(struct inode *inode, struct file *file)
578 {
579         struct drbd_connection *connection = inode->i_private;
580         kref_put(&connection->kref, drbd_destroy_connection);
581         return single_release(inode, file);
582 }
583
584 static const struct file_operations connection_callback_history_fops = {
585         .owner          = THIS_MODULE,
586         .open           = callback_history_open,
587         .read           = seq_read,
588         .llseek         = seq_lseek,
589         .release        = callback_history_release,
590 };
591
592 static int connection_oldest_requests_show(struct seq_file *m, void *ignored)
593 {
594         struct drbd_connection *connection = m->private;
595         unsigned long now = jiffies;
596         struct drbd_request *r1, *r2;
597
598         /* BUMP me if you change the file format/content/presentation */
599         seq_printf(m, "v: %u\n\n", 0);
600
601         spin_lock_irq(&connection->resource->req_lock);
602         r1 = connection->req_next;
603         if (r1)
604                 seq_print_minor_vnr_req(m, r1, now);
605         r2 = connection->req_ack_pending;
606         if (r2 && r2 != r1) {
607                 r1 = r2;
608                 seq_print_minor_vnr_req(m, r1, now);
609         }
610         r2 = connection->req_not_net_done;
611         if (r2 && r2 != r1)
612                 seq_print_minor_vnr_req(m, r2, now);
613         spin_unlock_irq(&connection->resource->req_lock);
614         return 0;
615 }
616
617 static int connection_oldest_requests_open(struct inode *inode, struct file *file)
618 {
619         struct drbd_connection *connection = inode->i_private;
620         return drbd_single_open(file, connection_oldest_requests_show, connection,
621                                 &connection->kref, drbd_destroy_connection);
622 }
623
624 static int connection_oldest_requests_release(struct inode *inode, struct file *file)
625 {
626         struct drbd_connection *connection = inode->i_private;
627         kref_put(&connection->kref, drbd_destroy_connection);
628         return single_release(inode, file);
629 }
630
631 static const struct file_operations connection_oldest_requests_fops = {
632         .owner          = THIS_MODULE,
633         .open           = connection_oldest_requests_open,
634         .read           = seq_read,
635         .llseek         = seq_lseek,
636         .release        = connection_oldest_requests_release,
637 };
638
639 void drbd_debugfs_connection_add(struct drbd_connection *connection)
640 {
641         struct dentry *conns_dir = connection->resource->debugfs_res_connections;
642         struct dentry *dentry;
643         if (!conns_dir)
644                 return;
645
646         /* Once we enable mutliple peers,
647          * these connections will have descriptive names.
648          * For now, it is just the one connection to the (only) "peer". */
649         dentry = debugfs_create_dir("peer", conns_dir);
650         if (IS_ERR_OR_NULL(dentry))
651                 goto fail;
652         connection->debugfs_conn = dentry;
653
654         dentry = debugfs_create_file("callback_history", S_IRUSR|S_IRGRP,
655                         connection->debugfs_conn, connection,
656                         &connection_callback_history_fops);
657         if (IS_ERR_OR_NULL(dentry))
658                 goto fail;
659         connection->debugfs_conn_callback_history = dentry;
660
661         dentry = debugfs_create_file("oldest_requests", S_IRUSR|S_IRGRP,
662                         connection->debugfs_conn, connection,
663                         &connection_oldest_requests_fops);
664         if (IS_ERR_OR_NULL(dentry))
665                 goto fail;
666         connection->debugfs_conn_oldest_requests = dentry;
667         return;
668
669 fail:
670         drbd_debugfs_connection_cleanup(connection);
671         drbd_err(connection, "failed to create debugfs dentry\n");
672 }
673
674 void drbd_debugfs_connection_cleanup(struct drbd_connection *connection)
675 {
676         drbd_debugfs_remove(&connection->debugfs_conn_callback_history);
677         drbd_debugfs_remove(&connection->debugfs_conn_oldest_requests);
678         drbd_debugfs_remove(&connection->debugfs_conn);
679 }
680
681 static void resync_dump_detail(struct seq_file *m, struct lc_element *e)
682 {
683        struct bm_extent *bme = lc_entry(e, struct bm_extent, lce);
684
685        seq_printf(m, "%5d %s %s %s", bme->rs_left,
686                   test_bit(BME_NO_WRITES, &bme->flags) ? "NO_WRITES" : "---------",
687                   test_bit(BME_LOCKED, &bme->flags) ? "LOCKED" : "------",
688                   test_bit(BME_PRIORITY, &bme->flags) ? "PRIORITY" : "--------"
689                   );
690 }
691
692 static int device_resync_extents_show(struct seq_file *m, void *ignored)
693 {
694         struct drbd_device *device = m->private;
695
696         /* BUMP me if you change the file format/content/presentation */
697         seq_printf(m, "v: %u\n\n", 0);
698
699         if (get_ldev_if_state(device, D_FAILED)) {
700                 lc_seq_printf_stats(m, device->resync);
701                 lc_seq_dump_details(m, device->resync, "rs_left flags", resync_dump_detail);
702                 put_ldev(device);
703         }
704         return 0;
705 }
706
707 static int device_act_log_extents_show(struct seq_file *m, void *ignored)
708 {
709         struct drbd_device *device = m->private;
710
711         /* BUMP me if you change the file format/content/presentation */
712         seq_printf(m, "v: %u\n\n", 0);
713
714         if (get_ldev_if_state(device, D_FAILED)) {
715                 lc_seq_printf_stats(m, device->act_log);
716                 lc_seq_dump_details(m, device->act_log, "", NULL);
717                 put_ldev(device);
718         }
719         return 0;
720 }
721
722 static int device_oldest_requests_show(struct seq_file *m, void *ignored)
723 {
724         struct drbd_device *device = m->private;
725         struct drbd_resource *resource = device->resource;
726         unsigned long now = jiffies;
727         struct drbd_request *r1, *r2;
728         int i;
729
730         /* BUMP me if you change the file format/content/presentation */
731         seq_printf(m, "v: %u\n\n", 0);
732
733         seq_puts(m, RQ_HDR);
734         spin_lock_irq(&resource->req_lock);
735         /* WRITE, then READ */
736         for (i = 1; i >= 0; --i) {
737                 r1 = list_first_entry_or_null(&device->pending_master_completion[i],
738                         struct drbd_request, req_pending_master_completion);
739                 r2 = list_first_entry_or_null(&device->pending_completion[i],
740                         struct drbd_request, req_pending_local);
741                 if (r1)
742                         seq_print_one_request(m, r1, now);
743                 if (r2 && r2 != r1)
744                         seq_print_one_request(m, r2, now);
745         }
746         spin_unlock_irq(&resource->req_lock);
747         return 0;
748 }
749
750 static int device_data_gen_id_show(struct seq_file *m, void *ignored)
751 {
752         struct drbd_device *device = m->private;
753         struct drbd_md *md;
754         enum drbd_uuid_index idx;
755
756         if (!get_ldev_if_state(device, D_FAILED))
757                 return -ENODEV;
758
759         md = &device->ldev->md;
760         spin_lock_irq(&md->uuid_lock);
761         for (idx = UI_CURRENT; idx <= UI_HISTORY_END; idx++) {
762                 seq_printf(m, "0x%016llX\n", md->uuid[idx]);
763         }
764         spin_unlock_irq(&md->uuid_lock);
765         put_ldev(device);
766         return 0;
767 }
768
769 static int device_ed_gen_id_show(struct seq_file *m, void *ignored)
770 {
771         struct drbd_device *device = m->private;
772         seq_printf(m, "0x%016llX\n", (unsigned long long)device->ed_uuid);
773         return 0;
774 }
775
776 #define drbd_debugfs_device_attr(name)                                          \
777 static int device_ ## name ## _open(struct inode *inode, struct file *file)     \
778 {                                                                               \
779         struct drbd_device *device = inode->i_private;                          \
780         return drbd_single_open(file, device_ ## name ## _show, device,         \
781                                 &device->kref, drbd_destroy_device);            \
782 }                                                                               \
783 static int device_ ## name ## _release(struct inode *inode, struct file *file)  \
784 {                                                                               \
785         struct drbd_device *device = inode->i_private;                          \
786         kref_put(&device->kref, drbd_destroy_device);                           \
787         return single_release(inode, file);                                     \
788 }                                                                               \
789 static const struct file_operations device_ ## name ## _fops = {                \
790         .owner          = THIS_MODULE,                                          \
791         .open           = device_ ## name ## _open,                             \
792         .read           = seq_read,                                             \
793         .llseek         = seq_lseek,                                            \
794         .release        = device_ ## name ## _release,                          \
795 };
796
797 drbd_debugfs_device_attr(oldest_requests)
798 drbd_debugfs_device_attr(act_log_extents)
799 drbd_debugfs_device_attr(resync_extents)
800 drbd_debugfs_device_attr(data_gen_id)
801 drbd_debugfs_device_attr(ed_gen_id)
802
803 void drbd_debugfs_device_add(struct drbd_device *device)
804 {
805         struct dentry *vols_dir = device->resource->debugfs_res_volumes;
806         char minor_buf[8]; /* MINORMASK, MINORBITS == 20; */
807         char vnr_buf[8];   /* volume number vnr is even 16 bit only; */
808         char *slink_name = NULL;
809
810         struct dentry *dentry;
811         if (!vols_dir || !drbd_debugfs_minors)
812                 return;
813
814         snprintf(vnr_buf, sizeof(vnr_buf), "%u", device->vnr);
815         dentry = debugfs_create_dir(vnr_buf, vols_dir);
816         if (IS_ERR_OR_NULL(dentry))
817                 goto fail;
818         device->debugfs_vol = dentry;
819
820         snprintf(minor_buf, sizeof(minor_buf), "%u", device->minor);
821         slink_name = kasprintf(GFP_KERNEL, "../resources/%s/volumes/%u",
822                         device->resource->name, device->vnr);
823         if (!slink_name)
824                 goto fail;
825         dentry = debugfs_create_symlink(minor_buf, drbd_debugfs_minors, slink_name);
826         kfree(slink_name);
827         slink_name = NULL;
828         if (IS_ERR_OR_NULL(dentry))
829                 goto fail;
830         device->debugfs_minor = dentry;
831
832 #define DCF(name)       do {                                    \
833         dentry = debugfs_create_file(#name, S_IRUSR|S_IRGRP,    \
834                         device->debugfs_vol, device,            \
835                         &device_ ## name ## _fops);             \
836         if (IS_ERR_OR_NULL(dentry))                             \
837                 goto fail;                                      \
838         device->debugfs_vol_ ## name = dentry;                  \
839         } while (0)
840
841         DCF(oldest_requests);
842         DCF(act_log_extents);
843         DCF(resync_extents);
844         DCF(data_gen_id);
845         DCF(ed_gen_id);
846 #undef DCF
847         return;
848
849 fail:
850         drbd_debugfs_device_cleanup(device);
851         drbd_err(device, "failed to create debugfs entries\n");
852 }
853
854 void drbd_debugfs_device_cleanup(struct drbd_device *device)
855 {
856         drbd_debugfs_remove(&device->debugfs_minor);
857         drbd_debugfs_remove(&device->debugfs_vol_oldest_requests);
858         drbd_debugfs_remove(&device->debugfs_vol_act_log_extents);
859         drbd_debugfs_remove(&device->debugfs_vol_resync_extents);
860         drbd_debugfs_remove(&device->debugfs_vol_data_gen_id);
861         drbd_debugfs_remove(&device->debugfs_vol_ed_gen_id);
862         drbd_debugfs_remove(&device->debugfs_vol);
863 }
864
865 void drbd_debugfs_peer_device_add(struct drbd_peer_device *peer_device)
866 {
867         struct dentry *conn_dir = peer_device->connection->debugfs_conn;
868         struct dentry *dentry;
869         char vnr_buf[8];
870
871         if (!conn_dir)
872                 return;
873
874         snprintf(vnr_buf, sizeof(vnr_buf), "%u", peer_device->device->vnr);
875         dentry = debugfs_create_dir(vnr_buf, conn_dir);
876         if (IS_ERR_OR_NULL(dentry))
877                 goto fail;
878         peer_device->debugfs_peer_dev = dentry;
879         return;
880
881 fail:
882         drbd_debugfs_peer_device_cleanup(peer_device);
883         drbd_err(peer_device, "failed to create debugfs entries\n");
884 }
885
886 void drbd_debugfs_peer_device_cleanup(struct drbd_peer_device *peer_device)
887 {
888         drbd_debugfs_remove(&peer_device->debugfs_peer_dev);
889 }
890
891 static int drbd_version_show(struct seq_file *m, void *ignored)
892 {
893         seq_printf(m, "# %s\n", drbd_buildtag());
894         seq_printf(m, "VERSION=%s\n", REL_VERSION);
895         seq_printf(m, "API_VERSION=%u\n", API_VERSION);
896         seq_printf(m, "PRO_VERSION_MIN=%u\n", PRO_VERSION_MIN);
897         seq_printf(m, "PRO_VERSION_MAX=%u\n", PRO_VERSION_MAX);
898         return 0;
899 }
900
901 static int drbd_version_open(struct inode *inode, struct file *file)
902 {
903         return single_open(file, drbd_version_show, NULL);
904 }
905
906 static const struct file_operations drbd_version_fops = {
907         .owner = THIS_MODULE,
908         .open = drbd_version_open,
909         .llseek = seq_lseek,
910         .read = seq_read,
911         .release = single_release,
912 };
913
914 /* not __exit, may be indirectly called
915  * from the module-load-failure path as well. */
916 void drbd_debugfs_cleanup(void)
917 {
918         drbd_debugfs_remove(&drbd_debugfs_resources);
919         drbd_debugfs_remove(&drbd_debugfs_minors);
920         drbd_debugfs_remove(&drbd_debugfs_version);
921         drbd_debugfs_remove(&drbd_debugfs_root);
922 }
923
924 int __init drbd_debugfs_init(void)
925 {
926         struct dentry *dentry;
927
928         dentry = debugfs_create_dir("drbd", NULL);
929         if (IS_ERR_OR_NULL(dentry))
930                 goto fail;
931         drbd_debugfs_root = dentry;
932
933         dentry = debugfs_create_file("version", 0444, drbd_debugfs_root, NULL, &drbd_version_fops);
934         if (IS_ERR_OR_NULL(dentry))
935                 goto fail;
936         drbd_debugfs_version = dentry;
937
938         dentry = debugfs_create_dir("resources", drbd_debugfs_root);
939         if (IS_ERR_OR_NULL(dentry))
940                 goto fail;
941         drbd_debugfs_resources = dentry;
942
943         dentry = debugfs_create_dir("minors", drbd_debugfs_root);
944         if (IS_ERR_OR_NULL(dentry))
945                 goto fail;
946         drbd_debugfs_minors = dentry;
947         return 0;
948
949 fail:
950         drbd_debugfs_cleanup();
951         if (dentry)
952                 return PTR_ERR(dentry);
953         else
954                 return -EINVAL;
955 }