drbd_debugfs.c revision db1866ffeed2e142208a801f7598326b92ebf7c5
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
14static struct dentry *drbd_debugfs_root;
15static struct dentry *drbd_debugfs_resources;
16static struct dentry *drbd_debugfs_minors;
17
18static 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. */
180	if (!connection)
181		return 0;
182
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));
190	return 0;
191}
192
193static int in_flight_summary_open(struct inode *inode, struct file *file)
194{
195	return single_open(file, in_flight_summary_show, inode->i_private);
196}
197
198static const struct file_operations in_flight_summary_fops = {
199	.owner		= THIS_MODULE,
200	.open		= in_flight_summary_open,
201	.read		= seq_read,
202	.llseek		= seq_lseek,
203	.release	= single_release,
204};
205
206void drbd_debugfs_resource_add(struct drbd_resource *resource)
207{
208	struct dentry *dentry;
209	if (!drbd_debugfs_resources)
210		return;
211
212	dentry = debugfs_create_dir(resource->name, drbd_debugfs_resources);
213	if (IS_ERR_OR_NULL(dentry))
214		goto fail;
215	resource->debugfs_res = dentry;
216
217	dentry = debugfs_create_dir("volumes", resource->debugfs_res);
218	if (IS_ERR_OR_NULL(dentry))
219		goto fail;
220	resource->debugfs_res_volumes = dentry;
221
222	dentry = debugfs_create_dir("connections", resource->debugfs_res);
223	if (IS_ERR_OR_NULL(dentry))
224		goto fail;
225	resource->debugfs_res_connections = dentry;
226
227	dentry = debugfs_create_file("in_flight_summary", S_IRUSR|S_IRGRP,
228			resource->debugfs_res, resource,
229			&in_flight_summary_fops);
230	if (IS_ERR_OR_NULL(dentry))
231		goto fail;
232	resource->debugfs_res_in_flight_summary = dentry;
233	return;
234
235fail:
236	drbd_debugfs_resource_cleanup(resource);
237	drbd_err(resource, "failed to create debugfs dentry\n");
238}
239
240static void drbd_debugfs_remove(struct dentry **dp)
241{
242	debugfs_remove(*dp);
243	*dp = NULL;
244}
245
246void drbd_debugfs_resource_cleanup(struct drbd_resource *resource)
247{
248	/* it is ok to call debugfs_remove(NULL) */
249	drbd_debugfs_remove(&resource->debugfs_res_in_flight_summary);
250	drbd_debugfs_remove(&resource->debugfs_res_connections);
251	drbd_debugfs_remove(&resource->debugfs_res_volumes);
252	drbd_debugfs_remove(&resource->debugfs_res);
253}
254
255void drbd_debugfs_connection_add(struct drbd_connection *connection)
256{
257	struct dentry *conns_dir = connection->resource->debugfs_res_connections;
258	struct dentry *dentry;
259	if (!conns_dir)
260		return;
261
262	/* Once we enable mutliple peers,
263	 * these connections will have descriptive names.
264	 * For now, it is just the one connection to the (only) "peer". */
265	dentry = debugfs_create_dir("peer", conns_dir);
266	if (IS_ERR_OR_NULL(dentry))
267		goto fail;
268	connection->debugfs_conn = dentry;
269	return;
270
271fail:
272	drbd_debugfs_connection_cleanup(connection);
273	drbd_err(connection, "failed to create debugfs dentry\n");
274}
275
276void drbd_debugfs_connection_cleanup(struct drbd_connection *connection)
277{
278	drbd_debugfs_remove(&connection->debugfs_conn_callback_history);
279	drbd_debugfs_remove(&connection->debugfs_conn_oldest_requests);
280	drbd_debugfs_remove(&connection->debugfs_conn);
281}
282
283void drbd_debugfs_device_add(struct drbd_device *device)
284{
285	struct dentry *vols_dir = device->resource->debugfs_res_volumes;
286	char minor_buf[8]; /* MINORMASK, MINORBITS == 20; */
287	char vnr_buf[8];   /* volume number vnr is even 16 bit only; */
288	char *slink_name = NULL;
289
290	struct dentry *dentry;
291	if (!vols_dir || !drbd_debugfs_minors)
292		return;
293
294	snprintf(vnr_buf, sizeof(vnr_buf), "%u", device->vnr);
295	dentry = debugfs_create_dir(vnr_buf, vols_dir);
296	if (IS_ERR_OR_NULL(dentry))
297		goto fail;
298	device->debugfs_vol = dentry;
299
300	snprintf(minor_buf, sizeof(minor_buf), "%u", device->minor);
301	slink_name = kasprintf(GFP_KERNEL, "../resources/%s/volumes/%u",
302			device->resource->name, device->vnr);
303	if (!slink_name)
304		goto fail;
305	dentry = debugfs_create_symlink(minor_buf, drbd_debugfs_minors, slink_name);
306	if (IS_ERR_OR_NULL(dentry))
307		goto fail;
308	device->debugfs_minor = dentry;
309	kfree(slink_name);
310
311fail:
312	drbd_debugfs_device_cleanup(device);
313	drbd_err(device, "failed to create debugfs entries\n");
314}
315
316void drbd_debugfs_device_cleanup(struct drbd_device *device)
317{
318	drbd_debugfs_remove(&device->debugfs_minor);
319	drbd_debugfs_remove(&device->debugfs_vol_oldest_requests);
320	drbd_debugfs_remove(&device->debugfs_vol_act_log_extents);
321	drbd_debugfs_remove(&device->debugfs_vol_resync_extents);
322	drbd_debugfs_remove(&device->debugfs_vol_data_gen_id);
323	drbd_debugfs_remove(&device->debugfs_vol);
324}
325
326void drbd_debugfs_peer_device_add(struct drbd_peer_device *peer_device)
327{
328	struct dentry *conn_dir = peer_device->connection->debugfs_conn;
329	struct dentry *dentry;
330	char vnr_buf[8];
331
332	if (!conn_dir)
333		return;
334
335	snprintf(vnr_buf, sizeof(vnr_buf), "%u", peer_device->device->vnr);
336	dentry = debugfs_create_dir(vnr_buf, conn_dir);
337	if (IS_ERR_OR_NULL(dentry))
338		goto fail;
339	peer_device->debugfs_peer_dev = dentry;
340	return;
341
342fail:
343	drbd_debugfs_peer_device_cleanup(peer_device);
344	drbd_err(peer_device, "failed to create debugfs entries\n");
345}
346
347void drbd_debugfs_peer_device_cleanup(struct drbd_peer_device *peer_device)
348{
349	drbd_debugfs_remove(&peer_device->debugfs_peer_dev);
350}
351
352/* not __exit, may be indirectly called
353 * from the module-load-failure path as well. */
354void drbd_debugfs_cleanup(void)
355{
356	drbd_debugfs_remove(&drbd_debugfs_resources);
357	drbd_debugfs_remove(&drbd_debugfs_minors);
358	drbd_debugfs_remove(&drbd_debugfs_root);
359}
360
361int __init drbd_debugfs_init(void)
362{
363	struct dentry *dentry;
364
365	dentry = debugfs_create_dir("drbd", NULL);
366	if (IS_ERR_OR_NULL(dentry))
367		goto fail;
368	drbd_debugfs_root = dentry;
369
370	dentry = debugfs_create_dir("resources", drbd_debugfs_root);
371	if (IS_ERR_OR_NULL(dentry))
372		goto fail;
373	drbd_debugfs_resources = dentry;
374
375	dentry = debugfs_create_dir("minors", drbd_debugfs_root);
376	if (IS_ERR_OR_NULL(dentry))
377		goto fail;
378	drbd_debugfs_minors = dentry;
379	return 0;
380
381fail:
382	drbd_debugfs_cleanup();
383	if (dentry)
384		return PTR_ERR(dentry);
385	else
386		return -EINVAL;
387}
388