blob: 51c64ec7bbc1c2e6b3cc7fb6dce5a32cc4fc1daf [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
14static struct dentry *drbd_debugfs_root;
15static struct dentry *drbd_debugfs_resources;
16static struct dentry *drbd_debugfs_minors;
17
Lars Ellenbergdb1866ff2014-05-02 13:20:05 +020018static void seq_print_age_or_dash(struct seq_file *m, bool valid, unsigned long dt)
19{
20 if (valid)
21 seq_printf(m, "\t%d", jiffies_to_msecs(dt));
22 else
23 seq_printf(m, "\t-");
24}
25
26static void seq_print_rq_state_bit(struct seq_file *m,
27 bool is_set, char *sep, const char *name)
28{
29 if (!is_set)
30 return;
31 seq_putc(m, *sep);
32 seq_puts(m, name);
33 *sep = '|';
34}
35
36/* pretty print enum drbd_req_state_bits req->rq_state */
37static void seq_print_request_state(struct seq_file *m, struct drbd_request *req)
38{
39 unsigned int s = req->rq_state;
40 char sep = ' ';
41 seq_printf(m, "\t0x%08x", s);
42 seq_printf(m, "\tmaster: %s", req->master_bio ? "pending" : "completed");
43
44 /* RQ_WRITE ignored, already reported */
45 seq_puts(m, "\tlocal:");
46 seq_print_rq_state_bit(m, s & RQ_IN_ACT_LOG, &sep, "in-AL");
47 seq_print_rq_state_bit(m, s & RQ_POSTPONED, &sep, "postponed");
48 seq_print_rq_state_bit(m, s & RQ_COMPLETION_SUSP, &sep, "suspended");
49 sep = ' ';
50 seq_print_rq_state_bit(m, s & RQ_LOCAL_PENDING, &sep, "pending");
51 seq_print_rq_state_bit(m, s & RQ_LOCAL_COMPLETED, &sep, "completed");
52 seq_print_rq_state_bit(m, s & RQ_LOCAL_ABORTED, &sep, "aborted");
53 seq_print_rq_state_bit(m, s & RQ_LOCAL_OK, &sep, "ok");
54 if (sep == ' ')
55 seq_puts(m, " -");
56
57 /* for_each_connection ... */
58 seq_printf(m, "\tnet:");
59 sep = ' ';
60 seq_print_rq_state_bit(m, s & RQ_NET_PENDING, &sep, "pending");
61 seq_print_rq_state_bit(m, s & RQ_NET_QUEUED, &sep, "queued");
62 seq_print_rq_state_bit(m, s & RQ_NET_SENT, &sep, "sent");
63 seq_print_rq_state_bit(m, s & RQ_NET_DONE, &sep, "done");
64 seq_print_rq_state_bit(m, s & RQ_NET_SIS, &sep, "sis");
65 seq_print_rq_state_bit(m, s & RQ_NET_OK, &sep, "ok");
66 if (sep == ' ')
67 seq_puts(m, " -");
68
69 seq_printf(m, " :");
70 sep = ' ';
71 seq_print_rq_state_bit(m, s & RQ_EXP_RECEIVE_ACK, &sep, "B");
72 seq_print_rq_state_bit(m, s & RQ_EXP_WRITE_ACK, &sep, "C");
73 seq_print_rq_state_bit(m, s & RQ_EXP_BARR_ACK, &sep, "barr");
74 if (sep == ' ')
75 seq_puts(m, " -");
76 seq_printf(m, "\n");
77}
78
79static void seq_print_one_request(struct seq_file *m, struct drbd_request *req, unsigned long now)
80{
81 /* change anything here, fixup header below! */
82 unsigned int s = req->rq_state;
83
84#define RQ_HDR_1 "epoch\tsector\tsize\trw"
85 seq_printf(m, "0x%x\t%llu\t%u\t%s",
86 req->epoch,
87 (unsigned long long)req->i.sector, req->i.size >> 9,
88 (s & RQ_WRITE) ? "W" : "R");
89
90#define RQ_HDR_2 "\tstart\tin AL\tsubmit"
91 seq_printf(m, "\t%d", jiffies_to_msecs(now - req->start_jif));
92 seq_print_age_or_dash(m, s & RQ_IN_ACT_LOG, now - req->in_actlog_jif);
93 seq_print_age_or_dash(m, s & RQ_LOCAL_PENDING, now - req->pre_submit_jif);
94
95#define RQ_HDR_3 "\tsent\tacked\tdone"
96 seq_print_age_or_dash(m, s & RQ_NET_SENT, now - req->pre_send_jif);
97 seq_print_age_or_dash(m, (s & RQ_NET_SENT) && !(s & RQ_NET_PENDING), now - req->acked_jif);
98 seq_print_age_or_dash(m, s & RQ_NET_DONE, now - req->net_done_jif);
99
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
105static void seq_print_minor_vnr_req(struct seq_file *m, struct drbd_request *req, unsigned long now)
106{
107 seq_printf(m, "%u\t%u\t", req->device->minor, req->device->vnr);
108 seq_print_one_request(m, req, now);
109}
110
111static void seq_print_resource_transfer_log_summary(struct seq_file *m,
112 struct drbd_resource *resource,
113 struct drbd_connection *connection,
114 unsigned long now)
115{
116 struct drbd_request *req;
117 unsigned int count = 0;
118 unsigned int show_state = 0;
119
120 seq_puts(m, "n\tdevice\tvnr\t" RQ_HDR);
121 spin_lock_irq(&resource->req_lock);
122 list_for_each_entry(req, &connection->transfer_log, tl_requests) {
123 unsigned int tmp = 0;
124 unsigned int s;
125 ++count;
126
127 /* don't disable irq "forever" */
128 if (!(count & 0x1ff)) {
129 struct drbd_request *req_next;
130 kref_get(&req->kref);
131 spin_unlock_irq(&resource->req_lock);
132 cond_resched();
133 spin_lock_irq(&resource->req_lock);
134 req_next = list_next_entry(req, tl_requests);
135 if (kref_put(&req->kref, drbd_req_destroy))
136 req = req_next;
137 if (&req->tl_requests == &connection->transfer_log)
138 break;
139 }
140
141 s = req->rq_state;
142
143 /* This is meant to summarize timing issues, to be able to tell
144 * local disk problems from network problems.
145 * Skip requests, if we have shown an even older request with
146 * similar aspects already. */
147 if (req->master_bio == NULL)
148 tmp |= 1;
149 if ((s & RQ_LOCAL_MASK) && (s & RQ_LOCAL_PENDING))
150 tmp |= 2;
151 if (s & RQ_NET_MASK) {
152 if (!(s & RQ_NET_SENT))
153 tmp |= 4;
154 if (s & RQ_NET_PENDING)
155 tmp |= 8;
156 if (!(s & RQ_NET_DONE))
157 tmp |= 16;
158 }
159 if ((tmp & show_state) == tmp)
160 continue;
161 show_state |= tmp;
162 seq_printf(m, "%u\t", count);
163 seq_print_minor_vnr_req(m, req, now);
164 if (show_state == 0x1f)
165 break;
166 }
167 spin_unlock_irq(&resource->req_lock);
168}
169
170/* TODO: transfer_log and friends should be moved to resource */
171static int in_flight_summary_show(struct seq_file *m, void *pos)
172{
173 struct drbd_resource *resource = m->private;
174 struct drbd_connection *connection;
175 unsigned long jif = jiffies;
176
177 connection = first_connection(resource);
178 /* This does not happen, actually.
179 * But be robust and prepare for future code changes. */
Lars Ellenberg4a521cc2014-05-05 12:05:54 +0000180 if (!connection || !kref_get_unless_zero(&connection->kref))
181 return -ESTALE;
Lars Ellenbergdb1866ff2014-05-02 13:20:05 +0200182
183 seq_puts(m, "oldest application requests\n");
184 seq_print_resource_transfer_log_summary(m, resource, connection, jif);
185 seq_putc(m, '\n');
186
187 jif = jiffies - jif;
188 if (jif)
189 seq_printf(m, "generated in %d ms\n", jiffies_to_msecs(jif));
Lars Ellenberg4a521cc2014-05-05 12:05:54 +0000190 kref_put(&connection->kref, drbd_destroy_connection);
Lars Ellenbergdb1866ff2014-05-02 13:20:05 +0200191 return 0;
192}
193
Lars Ellenberg4a521cc2014-05-05 12:05:54 +0000194/* simple_positive(file->f_dentry) respectively debugfs_positive(),
195 * but neither is "reachable" from here.
196 * So we have our own inline version of it above. :-( */
197static inline int debugfs_positive(struct dentry *dentry)
198{
199 return dentry->d_inode && !d_unhashed(dentry);
200}
201
202/* make sure at *open* time that the respective object won't go away. */
203static int drbd_single_open(struct file *file, int (*show)(struct seq_file *, void *),
204 void *data, struct kref *kref,
205 void (*release)(struct kref *))
206{
207 struct dentry *parent;
208 int ret = -ESTALE;
209
210 /* Are we still linked,
211 * or has debugfs_remove() already been called? */
212 parent = file->f_dentry->d_parent;
213 /* not sure if this can happen: */
214 if (!parent || !parent->d_inode)
215 goto out;
216 /* serialize with d_delete() */
217 mutex_lock(&parent->d_inode->i_mutex);
218 if (!debugfs_positive(file->f_dentry))
219 goto out_unlock;
220 /* Make sure the object is still alive */
221 if (kref_get_unless_zero(kref))
222 ret = 0;
223out_unlock:
224 mutex_unlock(&parent->d_inode->i_mutex);
225 if (!ret) {
226 ret = single_open(file, show, data);
227 if (ret)
228 kref_put(kref, release);
229 }
230out:
231 return ret;
232}
233
Lars Ellenbergdb1866ff2014-05-02 13:20:05 +0200234static int in_flight_summary_open(struct inode *inode, struct file *file)
235{
Lars Ellenberg4a521cc2014-05-05 12:05:54 +0000236 struct drbd_resource *resource = inode->i_private;
237 return drbd_single_open(file, in_flight_summary_show, resource,
238 &resource->kref, drbd_destroy_resource);
239}
240
241static int in_flight_summary_release(struct inode *inode, struct file *file)
242{
243 struct drbd_resource *resource = inode->i_private;
244 kref_put(&resource->kref, drbd_destroy_resource);
245 return single_release(inode, file);
Lars Ellenbergdb1866ff2014-05-02 13:20:05 +0200246}
247
248static const struct file_operations in_flight_summary_fops = {
249 .owner = THIS_MODULE,
250 .open = in_flight_summary_open,
251 .read = seq_read,
252 .llseek = seq_lseek,
Lars Ellenberg4a521cc2014-05-05 12:05:54 +0000253 .release = in_flight_summary_release,
Lars Ellenbergdb1866ff2014-05-02 13:20:05 +0200254};
255
Lars Ellenberg4d3d5aa2014-05-02 13:19:51 +0200256void drbd_debugfs_resource_add(struct drbd_resource *resource)
257{
258 struct dentry *dentry;
259 if (!drbd_debugfs_resources)
260 return;
261
262 dentry = debugfs_create_dir(resource->name, drbd_debugfs_resources);
263 if (IS_ERR_OR_NULL(dentry))
264 goto fail;
265 resource->debugfs_res = dentry;
266
267 dentry = debugfs_create_dir("volumes", resource->debugfs_res);
268 if (IS_ERR_OR_NULL(dentry))
269 goto fail;
270 resource->debugfs_res_volumes = dentry;
271
272 dentry = debugfs_create_dir("connections", resource->debugfs_res);
273 if (IS_ERR_OR_NULL(dentry))
274 goto fail;
275 resource->debugfs_res_connections = dentry;
276
Lars Ellenbergdb1866ff2014-05-02 13:20:05 +0200277 dentry = debugfs_create_file("in_flight_summary", S_IRUSR|S_IRGRP,
278 resource->debugfs_res, resource,
279 &in_flight_summary_fops);
280 if (IS_ERR_OR_NULL(dentry))
281 goto fail;
282 resource->debugfs_res_in_flight_summary = dentry;
Lars Ellenberg4d3d5aa2014-05-02 13:19:51 +0200283 return;
284
285fail:
286 drbd_debugfs_resource_cleanup(resource);
287 drbd_err(resource, "failed to create debugfs dentry\n");
288}
289
290static void drbd_debugfs_remove(struct dentry **dp)
291{
292 debugfs_remove(*dp);
293 *dp = NULL;
294}
295
296void drbd_debugfs_resource_cleanup(struct drbd_resource *resource)
297{
298 /* it is ok to call debugfs_remove(NULL) */
299 drbd_debugfs_remove(&resource->debugfs_res_in_flight_summary);
300 drbd_debugfs_remove(&resource->debugfs_res_connections);
301 drbd_debugfs_remove(&resource->debugfs_res_volumes);
302 drbd_debugfs_remove(&resource->debugfs_res);
303}
304
305void drbd_debugfs_connection_add(struct drbd_connection *connection)
306{
307 struct dentry *conns_dir = connection->resource->debugfs_res_connections;
308 struct dentry *dentry;
309 if (!conns_dir)
310 return;
311
312 /* Once we enable mutliple peers,
313 * these connections will have descriptive names.
314 * For now, it is just the one connection to the (only) "peer". */
315 dentry = debugfs_create_dir("peer", conns_dir);
316 if (IS_ERR_OR_NULL(dentry))
317 goto fail;
318 connection->debugfs_conn = dentry;
319 return;
320
321fail:
322 drbd_debugfs_connection_cleanup(connection);
323 drbd_err(connection, "failed to create debugfs dentry\n");
324}
325
326void drbd_debugfs_connection_cleanup(struct drbd_connection *connection)
327{
328 drbd_debugfs_remove(&connection->debugfs_conn_callback_history);
329 drbd_debugfs_remove(&connection->debugfs_conn_oldest_requests);
330 drbd_debugfs_remove(&connection->debugfs_conn);
331}
332
333void drbd_debugfs_device_add(struct drbd_device *device)
334{
335 struct dentry *vols_dir = device->resource->debugfs_res_volumes;
336 char minor_buf[8]; /* MINORMASK, MINORBITS == 20; */
337 char vnr_buf[8]; /* volume number vnr is even 16 bit only; */
338 char *slink_name = NULL;
339
340 struct dentry *dentry;
341 if (!vols_dir || !drbd_debugfs_minors)
342 return;
343
344 snprintf(vnr_buf, sizeof(vnr_buf), "%u", device->vnr);
345 dentry = debugfs_create_dir(vnr_buf, vols_dir);
346 if (IS_ERR_OR_NULL(dentry))
347 goto fail;
348 device->debugfs_vol = dentry;
349
350 snprintf(minor_buf, sizeof(minor_buf), "%u", device->minor);
351 slink_name = kasprintf(GFP_KERNEL, "../resources/%s/volumes/%u",
352 device->resource->name, device->vnr);
353 if (!slink_name)
354 goto fail;
355 dentry = debugfs_create_symlink(minor_buf, drbd_debugfs_minors, slink_name);
356 if (IS_ERR_OR_NULL(dentry))
357 goto fail;
358 device->debugfs_minor = dentry;
359 kfree(slink_name);
360
361fail:
362 drbd_debugfs_device_cleanup(device);
363 drbd_err(device, "failed to create debugfs entries\n");
364}
365
366void drbd_debugfs_device_cleanup(struct drbd_device *device)
367{
368 drbd_debugfs_remove(&device->debugfs_minor);
369 drbd_debugfs_remove(&device->debugfs_vol_oldest_requests);
370 drbd_debugfs_remove(&device->debugfs_vol_act_log_extents);
371 drbd_debugfs_remove(&device->debugfs_vol_resync_extents);
372 drbd_debugfs_remove(&device->debugfs_vol_data_gen_id);
373 drbd_debugfs_remove(&device->debugfs_vol);
374}
375
376void drbd_debugfs_peer_device_add(struct drbd_peer_device *peer_device)
377{
378 struct dentry *conn_dir = peer_device->connection->debugfs_conn;
379 struct dentry *dentry;
380 char vnr_buf[8];
381
382 if (!conn_dir)
383 return;
384
385 snprintf(vnr_buf, sizeof(vnr_buf), "%u", peer_device->device->vnr);
386 dentry = debugfs_create_dir(vnr_buf, conn_dir);
387 if (IS_ERR_OR_NULL(dentry))
388 goto fail;
389 peer_device->debugfs_peer_dev = dentry;
390 return;
391
392fail:
393 drbd_debugfs_peer_device_cleanup(peer_device);
394 drbd_err(peer_device, "failed to create debugfs entries\n");
395}
396
397void drbd_debugfs_peer_device_cleanup(struct drbd_peer_device *peer_device)
398{
399 drbd_debugfs_remove(&peer_device->debugfs_peer_dev);
400}
401
402/* not __exit, may be indirectly called
403 * from the module-load-failure path as well. */
404void drbd_debugfs_cleanup(void)
405{
406 drbd_debugfs_remove(&drbd_debugfs_resources);
407 drbd_debugfs_remove(&drbd_debugfs_minors);
408 drbd_debugfs_remove(&drbd_debugfs_root);
409}
410
411int __init drbd_debugfs_init(void)
412{
413 struct dentry *dentry;
414
415 dentry = debugfs_create_dir("drbd", NULL);
416 if (IS_ERR_OR_NULL(dentry))
417 goto fail;
418 drbd_debugfs_root = dentry;
419
420 dentry = debugfs_create_dir("resources", drbd_debugfs_root);
421 if (IS_ERR_OR_NULL(dentry))
422 goto fail;
423 drbd_debugfs_resources = dentry;
424
425 dentry = debugfs_create_dir("minors", drbd_debugfs_root);
426 if (IS_ERR_OR_NULL(dentry))
427 goto fail;
428 drbd_debugfs_minors = dentry;
429 return 0;
430
431fail:
432 drbd_debugfs_cleanup();
433 if (dentry)
434 return PTR_ERR(dentry);
435 else
436 return -EINVAL;
437}