xref: /openbmc/linux/drivers/block/drbd/drbd_debugfs.c (revision dc3f4198eac14e52a98dfc79cd84b45e280f59cd)
14d3d5aa8SLars Ellenberg #define pr_fmt(fmt) "drbd debugfs: " fmt
24d3d5aa8SLars Ellenberg #include <linux/kernel.h>
34d3d5aa8SLars Ellenberg #include <linux/module.h>
44d3d5aa8SLars Ellenberg #include <linux/debugfs.h>
5db1866ffSLars Ellenberg #include <linux/seq_file.h>
64d3d5aa8SLars Ellenberg #include <linux/stat.h>
7db1866ffSLars Ellenberg #include <linux/jiffies.h>
84d3d5aa8SLars Ellenberg #include <linux/list.h>
94d3d5aa8SLars Ellenberg 
104d3d5aa8SLars Ellenberg #include "drbd_int.h"
114d3d5aa8SLars Ellenberg #include "drbd_req.h"
124d3d5aa8SLars Ellenberg #include "drbd_debugfs.h"
134d3d5aa8SLars Ellenberg 
14b44e1184SLars Ellenberg 
15b44e1184SLars Ellenberg /**********************************************************************
16b44e1184SLars Ellenberg  * Whenever you change the file format, remember to bump the version. *
17b44e1184SLars Ellenberg  **********************************************************************/
18b44e1184SLars Ellenberg 
194d3d5aa8SLars Ellenberg static struct dentry *drbd_debugfs_root;
20b44e1184SLars Ellenberg static struct dentry *drbd_debugfs_version;
214d3d5aa8SLars Ellenberg static struct dentry *drbd_debugfs_resources;
224d3d5aa8SLars Ellenberg static struct dentry *drbd_debugfs_minors;
234d3d5aa8SLars Ellenberg 
24db1866ffSLars Ellenberg static void seq_print_age_or_dash(struct seq_file *m, bool valid, unsigned long dt)
25db1866ffSLars Ellenberg {
26db1866ffSLars Ellenberg 	if (valid)
27db1866ffSLars Ellenberg 		seq_printf(m, "\t%d", jiffies_to_msecs(dt));
28db1866ffSLars Ellenberg 	else
29db1866ffSLars Ellenberg 		seq_printf(m, "\t-");
30db1866ffSLars Ellenberg }
31db1866ffSLars Ellenberg 
32f418815fSLars Ellenberg static void __seq_print_rq_state_bit(struct seq_file *m,
33f418815fSLars Ellenberg 	bool is_set, char *sep, const char *set_name, const char *unset_name)
34db1866ffSLars Ellenberg {
35f418815fSLars Ellenberg 	if (is_set && set_name) {
36db1866ffSLars Ellenberg 		seq_putc(m, *sep);
37f418815fSLars Ellenberg 		seq_puts(m, set_name);
38db1866ffSLars Ellenberg 		*sep = '|';
39f418815fSLars Ellenberg 	} else if (!is_set && unset_name) {
40f418815fSLars Ellenberg 		seq_putc(m, *sep);
41f418815fSLars Ellenberg 		seq_puts(m, unset_name);
42f418815fSLars Ellenberg 		*sep = '|';
43f418815fSLars Ellenberg 	}
44f418815fSLars Ellenberg }
45f418815fSLars Ellenberg 
46f418815fSLars Ellenberg static void seq_print_rq_state_bit(struct seq_file *m,
47f418815fSLars Ellenberg 	bool is_set, char *sep, const char *set_name)
48f418815fSLars Ellenberg {
49f418815fSLars Ellenberg 	__seq_print_rq_state_bit(m, is_set, sep, set_name, NULL);
50db1866ffSLars Ellenberg }
51db1866ffSLars Ellenberg 
52db1866ffSLars Ellenberg /* pretty print enum drbd_req_state_bits req->rq_state */
53db1866ffSLars Ellenberg static void seq_print_request_state(struct seq_file *m, struct drbd_request *req)
54db1866ffSLars Ellenberg {
55db1866ffSLars Ellenberg 	unsigned int s = req->rq_state;
56db1866ffSLars Ellenberg 	char sep = ' ';
57db1866ffSLars Ellenberg 	seq_printf(m, "\t0x%08x", s);
58db1866ffSLars Ellenberg 	seq_printf(m, "\tmaster: %s", req->master_bio ? "pending" : "completed");
59db1866ffSLars Ellenberg 
60db1866ffSLars Ellenberg 	/* RQ_WRITE ignored, already reported */
61db1866ffSLars Ellenberg 	seq_puts(m, "\tlocal:");
62db1866ffSLars Ellenberg 	seq_print_rq_state_bit(m, s & RQ_IN_ACT_LOG, &sep, "in-AL");
63db1866ffSLars Ellenberg 	seq_print_rq_state_bit(m, s & RQ_POSTPONED, &sep, "postponed");
64db1866ffSLars Ellenberg 	seq_print_rq_state_bit(m, s & RQ_COMPLETION_SUSP, &sep, "suspended");
65db1866ffSLars Ellenberg 	sep = ' ';
66db1866ffSLars Ellenberg 	seq_print_rq_state_bit(m, s & RQ_LOCAL_PENDING, &sep, "pending");
67db1866ffSLars Ellenberg 	seq_print_rq_state_bit(m, s & RQ_LOCAL_COMPLETED, &sep, "completed");
68db1866ffSLars Ellenberg 	seq_print_rq_state_bit(m, s & RQ_LOCAL_ABORTED, &sep, "aborted");
69db1866ffSLars Ellenberg 	seq_print_rq_state_bit(m, s & RQ_LOCAL_OK, &sep, "ok");
70db1866ffSLars Ellenberg 	if (sep == ' ')
71db1866ffSLars Ellenberg 		seq_puts(m, " -");
72db1866ffSLars Ellenberg 
73db1866ffSLars Ellenberg 	/* for_each_connection ... */
74db1866ffSLars Ellenberg 	seq_printf(m, "\tnet:");
75db1866ffSLars Ellenberg 	sep = ' ';
76db1866ffSLars Ellenberg 	seq_print_rq_state_bit(m, s & RQ_NET_PENDING, &sep, "pending");
77db1866ffSLars Ellenberg 	seq_print_rq_state_bit(m, s & RQ_NET_QUEUED, &sep, "queued");
78db1866ffSLars Ellenberg 	seq_print_rq_state_bit(m, s & RQ_NET_SENT, &sep, "sent");
79db1866ffSLars Ellenberg 	seq_print_rq_state_bit(m, s & RQ_NET_DONE, &sep, "done");
80db1866ffSLars Ellenberg 	seq_print_rq_state_bit(m, s & RQ_NET_SIS, &sep, "sis");
81db1866ffSLars Ellenberg 	seq_print_rq_state_bit(m, s & RQ_NET_OK, &sep, "ok");
82db1866ffSLars Ellenberg 	if (sep == ' ')
83db1866ffSLars Ellenberg 		seq_puts(m, " -");
84db1866ffSLars Ellenberg 
85db1866ffSLars Ellenberg 	seq_printf(m, " :");
86db1866ffSLars Ellenberg 	sep = ' ';
87db1866ffSLars Ellenberg 	seq_print_rq_state_bit(m, s & RQ_EXP_RECEIVE_ACK, &sep, "B");
88db1866ffSLars Ellenberg 	seq_print_rq_state_bit(m, s & RQ_EXP_WRITE_ACK, &sep, "C");
89db1866ffSLars Ellenberg 	seq_print_rq_state_bit(m, s & RQ_EXP_BARR_ACK, &sep, "barr");
90db1866ffSLars Ellenberg 	if (sep == ' ')
91db1866ffSLars Ellenberg 		seq_puts(m, " -");
92db1866ffSLars Ellenberg 	seq_printf(m, "\n");
93db1866ffSLars Ellenberg }
94db1866ffSLars Ellenberg 
95db1866ffSLars Ellenberg static void seq_print_one_request(struct seq_file *m, struct drbd_request *req, unsigned long now)
96db1866ffSLars Ellenberg {
97db1866ffSLars Ellenberg 	/* change anything here, fixup header below! */
98db1866ffSLars Ellenberg 	unsigned int s = req->rq_state;
99db1866ffSLars Ellenberg 
100db1866ffSLars Ellenberg #define RQ_HDR_1 "epoch\tsector\tsize\trw"
101db1866ffSLars Ellenberg 	seq_printf(m, "0x%x\t%llu\t%u\t%s",
102db1866ffSLars Ellenberg 		req->epoch,
103db1866ffSLars Ellenberg 		(unsigned long long)req->i.sector, req->i.size >> 9,
104db1866ffSLars Ellenberg 		(s & RQ_WRITE) ? "W" : "R");
105db1866ffSLars Ellenberg 
106db1866ffSLars Ellenberg #define RQ_HDR_2 "\tstart\tin AL\tsubmit"
107db1866ffSLars Ellenberg 	seq_printf(m, "\t%d", jiffies_to_msecs(now - req->start_jif));
108db1866ffSLars Ellenberg 	seq_print_age_or_dash(m, s & RQ_IN_ACT_LOG, now - req->in_actlog_jif);
109db1866ffSLars Ellenberg 	seq_print_age_or_dash(m, s & RQ_LOCAL_PENDING, now - req->pre_submit_jif);
110db1866ffSLars Ellenberg 
111db1866ffSLars Ellenberg #define RQ_HDR_3 "\tsent\tacked\tdone"
112db1866ffSLars Ellenberg 	seq_print_age_or_dash(m, s & RQ_NET_SENT, now - req->pre_send_jif);
113db1866ffSLars Ellenberg 	seq_print_age_or_dash(m, (s & RQ_NET_SENT) && !(s & RQ_NET_PENDING), now - req->acked_jif);
114db1866ffSLars Ellenberg 	seq_print_age_or_dash(m, s & RQ_NET_DONE, now - req->net_done_jif);
115db1866ffSLars Ellenberg 
116db1866ffSLars Ellenberg #define RQ_HDR_4 "\tstate\n"
117db1866ffSLars Ellenberg 	seq_print_request_state(m, req);
118db1866ffSLars Ellenberg }
119db1866ffSLars Ellenberg #define RQ_HDR RQ_HDR_1 RQ_HDR_2 RQ_HDR_3 RQ_HDR_4
120db1866ffSLars Ellenberg 
121db1866ffSLars Ellenberg static void seq_print_minor_vnr_req(struct seq_file *m, struct drbd_request *req, unsigned long now)
122db1866ffSLars Ellenberg {
123db1866ffSLars Ellenberg 	seq_printf(m, "%u\t%u\t", req->device->minor, req->device->vnr);
124db1866ffSLars Ellenberg 	seq_print_one_request(m, req, now);
125db1866ffSLars Ellenberg }
126db1866ffSLars Ellenberg 
127f418815fSLars Ellenberg static void seq_print_resource_pending_meta_io(struct seq_file *m, struct drbd_resource *resource, unsigned long now)
128f418815fSLars Ellenberg {
129f418815fSLars Ellenberg 	struct drbd_device *device;
130f418815fSLars Ellenberg 	unsigned int i;
131f418815fSLars Ellenberg 
132f418815fSLars Ellenberg 	seq_puts(m, "minor\tvnr\tstart\tsubmit\tintent\n");
133f418815fSLars Ellenberg 	rcu_read_lock();
134f418815fSLars Ellenberg 	idr_for_each_entry(&resource->devices, device, i) {
135f418815fSLars Ellenberg 		struct drbd_md_io tmp;
136f418815fSLars Ellenberg 		/* In theory this is racy,
137f418815fSLars Ellenberg 		 * in the sense that there could have been a
138f418815fSLars Ellenberg 		 * drbd_md_put_buffer(); drbd_md_get_buffer();
139f418815fSLars Ellenberg 		 * between accessing these members here.  */
140f418815fSLars Ellenberg 		tmp = device->md_io;
141f418815fSLars Ellenberg 		if (atomic_read(&tmp.in_use)) {
142f418815fSLars Ellenberg 			seq_printf(m, "%u\t%u\t%d\t",
143f418815fSLars Ellenberg 				device->minor, device->vnr,
144f418815fSLars Ellenberg 				jiffies_to_msecs(now - tmp.start_jif));
145f418815fSLars Ellenberg 			if (time_before(tmp.submit_jif, tmp.start_jif))
146f418815fSLars Ellenberg 				seq_puts(m, "-\t");
147f418815fSLars Ellenberg 			else
148f418815fSLars Ellenberg 				seq_printf(m, "%d\t", jiffies_to_msecs(now - tmp.submit_jif));
149f418815fSLars Ellenberg 			seq_printf(m, "%s\n", tmp.current_use);
150f418815fSLars Ellenberg 		}
151f418815fSLars Ellenberg 	}
152f418815fSLars Ellenberg 	rcu_read_unlock();
153f418815fSLars Ellenberg }
154f418815fSLars Ellenberg 
155f418815fSLars Ellenberg static void seq_print_waiting_for_AL(struct seq_file *m, struct drbd_resource *resource, unsigned long now)
156f418815fSLars Ellenberg {
157f418815fSLars Ellenberg 	struct drbd_device *device;
158f418815fSLars Ellenberg 	unsigned int i;
159f418815fSLars Ellenberg 
160f418815fSLars Ellenberg 	seq_puts(m, "minor\tvnr\tage\t#waiting\n");
161f418815fSLars Ellenberg 	rcu_read_lock();
162f418815fSLars Ellenberg 	idr_for_each_entry(&resource->devices, device, i) {
163f418815fSLars Ellenberg 		unsigned long jif;
164f418815fSLars Ellenberg 		struct drbd_request *req;
165f418815fSLars Ellenberg 		int n = atomic_read(&device->ap_actlog_cnt);
166f418815fSLars Ellenberg 		if (n) {
167f418815fSLars Ellenberg 			spin_lock_irq(&device->resource->req_lock);
168f418815fSLars Ellenberg 			req = list_first_entry_or_null(&device->pending_master_completion[1],
169f418815fSLars Ellenberg 				struct drbd_request, req_pending_master_completion);
170f418815fSLars Ellenberg 			/* if the oldest request does not wait for the activity log
171f418815fSLars Ellenberg 			 * it is not interesting for us here */
172f418815fSLars Ellenberg 			if (req && !(req->rq_state & RQ_IN_ACT_LOG))
173f418815fSLars Ellenberg 				jif = req->start_jif;
174f418815fSLars Ellenberg 			else
175f418815fSLars Ellenberg 				req = NULL;
176f418815fSLars Ellenberg 			spin_unlock_irq(&device->resource->req_lock);
177f418815fSLars Ellenberg 		}
178f418815fSLars Ellenberg 		if (n) {
179f418815fSLars Ellenberg 			seq_printf(m, "%u\t%u\t", device->minor, device->vnr);
180f418815fSLars Ellenberg 			if (req)
181f418815fSLars Ellenberg 				seq_printf(m, "%u\t", jiffies_to_msecs(now - jif));
182f418815fSLars Ellenberg 			else
183f418815fSLars Ellenberg 				seq_puts(m, "-\t");
184f418815fSLars Ellenberg 			seq_printf(m, "%u\n", n);
185f418815fSLars Ellenberg 		}
186f418815fSLars Ellenberg 	}
187f418815fSLars Ellenberg 	rcu_read_unlock();
188f418815fSLars Ellenberg }
189f418815fSLars Ellenberg 
190f418815fSLars Ellenberg static void seq_print_device_bitmap_io(struct seq_file *m, struct drbd_device *device, unsigned long now)
191f418815fSLars Ellenberg {
192f418815fSLars Ellenberg 	struct drbd_bm_aio_ctx *ctx;
193f418815fSLars Ellenberg 	unsigned long start_jif;
194f418815fSLars Ellenberg 	unsigned int in_flight;
195f418815fSLars Ellenberg 	unsigned int flags;
196f418815fSLars Ellenberg 	spin_lock_irq(&device->resource->req_lock);
197f418815fSLars Ellenberg 	ctx = list_first_entry_or_null(&device->pending_bitmap_io, struct drbd_bm_aio_ctx, list);
198f418815fSLars Ellenberg 	if (ctx && ctx->done)
199f418815fSLars Ellenberg 		ctx = NULL;
200f418815fSLars Ellenberg 	if (ctx) {
201f418815fSLars Ellenberg 		start_jif = ctx->start_jif;
202f418815fSLars Ellenberg 		in_flight = atomic_read(&ctx->in_flight);
203f418815fSLars Ellenberg 		flags = ctx->flags;
204f418815fSLars Ellenberg 	}
205f418815fSLars Ellenberg 	spin_unlock_irq(&device->resource->req_lock);
206f418815fSLars Ellenberg 	if (ctx) {
207f418815fSLars Ellenberg 		seq_printf(m, "%u\t%u\t%c\t%u\t%u\n",
208f418815fSLars Ellenberg 			device->minor, device->vnr,
209f418815fSLars Ellenberg 			(flags & BM_AIO_READ) ? 'R' : 'W',
210f418815fSLars Ellenberg 			jiffies_to_msecs(now - start_jif),
211f418815fSLars Ellenberg 			in_flight);
212f418815fSLars Ellenberg 	}
213f418815fSLars Ellenberg }
214f418815fSLars Ellenberg 
215f418815fSLars Ellenberg static void seq_print_resource_pending_bitmap_io(struct seq_file *m, struct drbd_resource *resource, unsigned long now)
216f418815fSLars Ellenberg {
217f418815fSLars Ellenberg 	struct drbd_device *device;
218f418815fSLars Ellenberg 	unsigned int i;
219f418815fSLars Ellenberg 
220f418815fSLars Ellenberg 	seq_puts(m, "minor\tvnr\trw\tage\t#in-flight\n");
221f418815fSLars Ellenberg 	rcu_read_lock();
222f418815fSLars Ellenberg 	idr_for_each_entry(&resource->devices, device, i) {
223f418815fSLars Ellenberg 		seq_print_device_bitmap_io(m, device, now);
224f418815fSLars Ellenberg 	}
225f418815fSLars Ellenberg 	rcu_read_unlock();
226f418815fSLars Ellenberg }
227f418815fSLars Ellenberg 
228f418815fSLars Ellenberg /* pretty print enum peer_req->flags */
229f418815fSLars Ellenberg static void seq_print_peer_request_flags(struct seq_file *m, struct drbd_peer_request *peer_req)
230f418815fSLars Ellenberg {
231f418815fSLars Ellenberg 	unsigned long f = peer_req->flags;
232f418815fSLars Ellenberg 	char sep = ' ';
233f418815fSLars Ellenberg 
234f418815fSLars Ellenberg 	__seq_print_rq_state_bit(m, f & EE_SUBMITTED, &sep, "submitted", "preparing");
235f418815fSLars Ellenberg 	__seq_print_rq_state_bit(m, f & EE_APPLICATION, &sep, "application", "internal");
236f418815fSLars Ellenberg 	seq_print_rq_state_bit(m, f & EE_CALL_AL_COMPLETE_IO, &sep, "in-AL");
237f418815fSLars Ellenberg 	seq_print_rq_state_bit(m, f & EE_SEND_WRITE_ACK, &sep, "C");
238f418815fSLars Ellenberg 	seq_print_rq_state_bit(m, f & EE_MAY_SET_IN_SYNC, &sep, "set-in-sync");
239f418815fSLars Ellenberg 
240f418815fSLars Ellenberg 	if (f & EE_IS_TRIM) {
241f418815fSLars Ellenberg 		seq_putc(m, sep);
242f418815fSLars Ellenberg 		sep = '|';
243f418815fSLars Ellenberg 		if (f & EE_IS_TRIM_USE_ZEROOUT)
244f418815fSLars Ellenberg 			seq_puts(m, "zero-out");
245f418815fSLars Ellenberg 		else
246f418815fSLars Ellenberg 			seq_puts(m, "trim");
247f418815fSLars Ellenberg 	}
248f418815fSLars Ellenberg 	seq_putc(m, '\n');
249f418815fSLars Ellenberg }
250f418815fSLars Ellenberg 
251f418815fSLars Ellenberg static void seq_print_peer_request(struct seq_file *m,
252f418815fSLars Ellenberg 	struct drbd_device *device, struct list_head *lh,
253f418815fSLars Ellenberg 	unsigned long now)
254f418815fSLars Ellenberg {
255f418815fSLars Ellenberg 	bool reported_preparing = false;
256f418815fSLars Ellenberg 	struct drbd_peer_request *peer_req;
257f418815fSLars Ellenberg 	list_for_each_entry(peer_req, lh, w.list) {
258f418815fSLars Ellenberg 		if (reported_preparing && !(peer_req->flags & EE_SUBMITTED))
259f418815fSLars Ellenberg 			continue;
260f418815fSLars Ellenberg 
261f418815fSLars Ellenberg 		if (device)
262f418815fSLars Ellenberg 			seq_printf(m, "%u\t%u\t", device->minor, device->vnr);
263f418815fSLars Ellenberg 
264f418815fSLars Ellenberg 		seq_printf(m, "%llu\t%u\t%c\t%u\t",
265f418815fSLars Ellenberg 			(unsigned long long)peer_req->i.sector, peer_req->i.size >> 9,
266f418815fSLars Ellenberg 			(peer_req->flags & EE_WRITE) ? 'W' : 'R',
267f418815fSLars Ellenberg 			jiffies_to_msecs(now - peer_req->submit_jif));
268f418815fSLars Ellenberg 		seq_print_peer_request_flags(m, peer_req);
269f418815fSLars Ellenberg 		if (peer_req->flags & EE_SUBMITTED)
270f418815fSLars Ellenberg 			break;
271f418815fSLars Ellenberg 		else
272f418815fSLars Ellenberg 			reported_preparing = true;
273f418815fSLars Ellenberg 	}
274f418815fSLars Ellenberg }
275f418815fSLars Ellenberg 
276f418815fSLars Ellenberg static void seq_print_device_peer_requests(struct seq_file *m,
277f418815fSLars Ellenberg 	struct drbd_device *device, unsigned long now)
278f418815fSLars Ellenberg {
279f418815fSLars Ellenberg 	seq_puts(m, "minor\tvnr\tsector\tsize\trw\tage\tflags\n");
280f418815fSLars Ellenberg 	spin_lock_irq(&device->resource->req_lock);
281f418815fSLars Ellenberg 	seq_print_peer_request(m, device, &device->active_ee, now);
282f418815fSLars Ellenberg 	seq_print_peer_request(m, device, &device->read_ee, now);
283f418815fSLars Ellenberg 	seq_print_peer_request(m, device, &device->sync_ee, now);
284f418815fSLars Ellenberg 	spin_unlock_irq(&device->resource->req_lock);
285f418815fSLars Ellenberg 	if (test_bit(FLUSH_PENDING, &device->flags)) {
286f418815fSLars Ellenberg 		seq_printf(m, "%u\t%u\t-\t-\tF\t%u\tflush\n",
287f418815fSLars Ellenberg 			device->minor, device->vnr,
288f418815fSLars Ellenberg 			jiffies_to_msecs(now - device->flush_jif));
289f418815fSLars Ellenberg 	}
290f418815fSLars Ellenberg }
291f418815fSLars Ellenberg 
292f418815fSLars Ellenberg static void seq_print_resource_pending_peer_requests(struct seq_file *m,
293f418815fSLars Ellenberg 	struct drbd_resource *resource, unsigned long now)
294f418815fSLars Ellenberg {
295f418815fSLars Ellenberg 	struct drbd_device *device;
296f418815fSLars Ellenberg 	unsigned int i;
297f418815fSLars Ellenberg 
298f418815fSLars Ellenberg 	rcu_read_lock();
299f418815fSLars Ellenberg 	idr_for_each_entry(&resource->devices, device, i) {
300f418815fSLars Ellenberg 		seq_print_device_peer_requests(m, device, now);
301f418815fSLars Ellenberg 	}
302f418815fSLars Ellenberg 	rcu_read_unlock();
303f418815fSLars Ellenberg }
304f418815fSLars Ellenberg 
305db1866ffSLars Ellenberg static void seq_print_resource_transfer_log_summary(struct seq_file *m,
306db1866ffSLars Ellenberg 	struct drbd_resource *resource,
307db1866ffSLars Ellenberg 	struct drbd_connection *connection,
308db1866ffSLars Ellenberg 	unsigned long now)
309db1866ffSLars Ellenberg {
310db1866ffSLars Ellenberg 	struct drbd_request *req;
311db1866ffSLars Ellenberg 	unsigned int count = 0;
312db1866ffSLars Ellenberg 	unsigned int show_state = 0;
313db1866ffSLars Ellenberg 
314db1866ffSLars Ellenberg 	seq_puts(m, "n\tdevice\tvnr\t" RQ_HDR);
315db1866ffSLars Ellenberg 	spin_lock_irq(&resource->req_lock);
316db1866ffSLars Ellenberg 	list_for_each_entry(req, &connection->transfer_log, tl_requests) {
317db1866ffSLars Ellenberg 		unsigned int tmp = 0;
318db1866ffSLars Ellenberg 		unsigned int s;
319db1866ffSLars Ellenberg 		++count;
320db1866ffSLars Ellenberg 
321db1866ffSLars Ellenberg 		/* don't disable irq "forever" */
322db1866ffSLars Ellenberg 		if (!(count & 0x1ff)) {
323db1866ffSLars Ellenberg 			struct drbd_request *req_next;
324db1866ffSLars Ellenberg 			kref_get(&req->kref);
325db1866ffSLars Ellenberg 			spin_unlock_irq(&resource->req_lock);
326db1866ffSLars Ellenberg 			cond_resched();
327db1866ffSLars Ellenberg 			spin_lock_irq(&resource->req_lock);
328db1866ffSLars Ellenberg 			req_next = list_next_entry(req, tl_requests);
329db1866ffSLars Ellenberg 			if (kref_put(&req->kref, drbd_req_destroy))
330db1866ffSLars Ellenberg 				req = req_next;
331db1866ffSLars Ellenberg 			if (&req->tl_requests == &connection->transfer_log)
332db1866ffSLars Ellenberg 				break;
333db1866ffSLars Ellenberg 		}
334db1866ffSLars Ellenberg 
335db1866ffSLars Ellenberg 		s = req->rq_state;
336db1866ffSLars Ellenberg 
337db1866ffSLars Ellenberg 		/* This is meant to summarize timing issues, to be able to tell
338db1866ffSLars Ellenberg 		 * local disk problems from network problems.
339db1866ffSLars Ellenberg 		 * Skip requests, if we have shown an even older request with
340db1866ffSLars Ellenberg 		 * similar aspects already.  */
341db1866ffSLars Ellenberg 		if (req->master_bio == NULL)
342db1866ffSLars Ellenberg 			tmp |= 1;
343db1866ffSLars Ellenberg 		if ((s & RQ_LOCAL_MASK) && (s & RQ_LOCAL_PENDING))
344db1866ffSLars Ellenberg 			tmp |= 2;
345db1866ffSLars Ellenberg 		if (s & RQ_NET_MASK) {
346db1866ffSLars Ellenberg 			if (!(s & RQ_NET_SENT))
347db1866ffSLars Ellenberg 				tmp |= 4;
348db1866ffSLars Ellenberg 			if (s & RQ_NET_PENDING)
349db1866ffSLars Ellenberg 				tmp |= 8;
350db1866ffSLars Ellenberg 			if (!(s & RQ_NET_DONE))
351db1866ffSLars Ellenberg 				tmp |= 16;
352db1866ffSLars Ellenberg 		}
353db1866ffSLars Ellenberg 		if ((tmp & show_state) == tmp)
354db1866ffSLars Ellenberg 			continue;
355db1866ffSLars Ellenberg 		show_state |= tmp;
356db1866ffSLars Ellenberg 		seq_printf(m, "%u\t", count);
357db1866ffSLars Ellenberg 		seq_print_minor_vnr_req(m, req, now);
358db1866ffSLars Ellenberg 		if (show_state == 0x1f)
359db1866ffSLars Ellenberg 			break;
360db1866ffSLars Ellenberg 	}
361db1866ffSLars Ellenberg 	spin_unlock_irq(&resource->req_lock);
362db1866ffSLars Ellenberg }
363db1866ffSLars Ellenberg 
364db1866ffSLars Ellenberg /* TODO: transfer_log and friends should be moved to resource */
365db1866ffSLars Ellenberg static int in_flight_summary_show(struct seq_file *m, void *pos)
366db1866ffSLars Ellenberg {
367db1866ffSLars Ellenberg 	struct drbd_resource *resource = m->private;
368db1866ffSLars Ellenberg 	struct drbd_connection *connection;
369db1866ffSLars Ellenberg 	unsigned long jif = jiffies;
370db1866ffSLars Ellenberg 
371db1866ffSLars Ellenberg 	connection = first_connection(resource);
372db1866ffSLars Ellenberg 	/* This does not happen, actually.
373db1866ffSLars Ellenberg 	 * But be robust and prepare for future code changes. */
3744a521ccaSLars Ellenberg 	if (!connection || !kref_get_unless_zero(&connection->kref))
3754a521ccaSLars Ellenberg 		return -ESTALE;
376db1866ffSLars Ellenberg 
377b44e1184SLars Ellenberg 	/* BUMP me if you change the file format/content/presentation */
378b44e1184SLars Ellenberg 	seq_printf(m, "v: %u\n\n", 0);
379b44e1184SLars Ellenberg 
380f418815fSLars Ellenberg 	seq_puts(m, "oldest bitmap IO\n");
381f418815fSLars Ellenberg 	seq_print_resource_pending_bitmap_io(m, resource, jif);
382f418815fSLars Ellenberg 	seq_putc(m, '\n');
383f418815fSLars Ellenberg 
384f418815fSLars Ellenberg 	seq_puts(m, "meta data IO\n");
385f418815fSLars Ellenberg 	seq_print_resource_pending_meta_io(m, resource, jif);
386f418815fSLars Ellenberg 	seq_putc(m, '\n');
387f418815fSLars Ellenberg 
388f418815fSLars Ellenberg 	seq_puts(m, "socket buffer stats\n");
389f418815fSLars Ellenberg 	/* for each connection ... once we have more than one */
390f418815fSLars Ellenberg 	rcu_read_lock();
391f418815fSLars Ellenberg 	if (connection->data.socket) {
392f418815fSLars Ellenberg 		/* open coded SIOCINQ, the "relevant" part */
393f418815fSLars Ellenberg 		struct tcp_sock *tp = tcp_sk(connection->data.socket->sk);
394f418815fSLars Ellenberg 		int answ = tp->rcv_nxt - tp->copied_seq;
395f418815fSLars Ellenberg 		seq_printf(m, "unread receive buffer: %u Byte\n", answ);
396f418815fSLars Ellenberg 		/* open coded SIOCOUTQ, the "relevant" part */
397f418815fSLars Ellenberg 		answ = tp->write_seq - tp->snd_una;
398f418815fSLars Ellenberg 		seq_printf(m, "unacked send buffer: %u Byte\n", answ);
399f418815fSLars Ellenberg 	}
400f418815fSLars Ellenberg 	rcu_read_unlock();
401f418815fSLars Ellenberg 	seq_putc(m, '\n');
402f418815fSLars Ellenberg 
403f418815fSLars Ellenberg 	seq_puts(m, "oldest peer requests\n");
404f418815fSLars Ellenberg 	seq_print_resource_pending_peer_requests(m, resource, jif);
405f418815fSLars Ellenberg 	seq_putc(m, '\n');
406f418815fSLars Ellenberg 
407f418815fSLars Ellenberg 	seq_puts(m, "application requests waiting for activity log\n");
408f418815fSLars Ellenberg 	seq_print_waiting_for_AL(m, resource, jif);
409f418815fSLars Ellenberg 	seq_putc(m, '\n');
410f418815fSLars Ellenberg 
411db1866ffSLars Ellenberg 	seq_puts(m, "oldest application requests\n");
412db1866ffSLars Ellenberg 	seq_print_resource_transfer_log_summary(m, resource, connection, jif);
413db1866ffSLars Ellenberg 	seq_putc(m, '\n');
414db1866ffSLars Ellenberg 
415db1866ffSLars Ellenberg 	jif = jiffies - jif;
416db1866ffSLars Ellenberg 	if (jif)
417db1866ffSLars Ellenberg 		seq_printf(m, "generated in %d ms\n", jiffies_to_msecs(jif));
4184a521ccaSLars Ellenberg 	kref_put(&connection->kref, drbd_destroy_connection);
419db1866ffSLars Ellenberg 	return 0;
420db1866ffSLars Ellenberg }
421db1866ffSLars Ellenberg 
4224a521ccaSLars Ellenberg /* make sure at *open* time that the respective object won't go away. */
4234a521ccaSLars Ellenberg static int drbd_single_open(struct file *file, int (*show)(struct seq_file *, void *),
4244a521ccaSLars Ellenberg 		                void *data, struct kref *kref,
4254a521ccaSLars Ellenberg 				void (*release)(struct kref *))
4264a521ccaSLars Ellenberg {
4274a521ccaSLars Ellenberg 	struct dentry *parent;
4284a521ccaSLars Ellenberg 	int ret = -ESTALE;
4294a521ccaSLars Ellenberg 
4304a521ccaSLars Ellenberg 	/* Are we still linked,
4314a521ccaSLars Ellenberg 	 * or has debugfs_remove() already been called? */
432b583043eSAl Viro 	parent = file->f_path.dentry->d_parent;
4334a521ccaSLars Ellenberg 	/* not sure if this can happen: */
43475c3cfa8SDavid Howells 	if (!parent || d_really_is_negative(parent))
4354a521ccaSLars Ellenberg 		goto out;
4364a521ccaSLars Ellenberg 	/* serialize with d_delete() */
43775c3cfa8SDavid Howells 	mutex_lock(&d_inode(parent)->i_mutex);
4384a521ccaSLars Ellenberg 	/* Make sure the object is still alive */
439*dc3f4198SAl Viro 	if (simple_positive(file->f_path.dentry)
44054e6fc38SLars Ellenberg 	&& kref_get_unless_zero(kref))
4414a521ccaSLars Ellenberg 		ret = 0;
44275c3cfa8SDavid Howells 	mutex_unlock(&d_inode(parent)->i_mutex);
4434a521ccaSLars Ellenberg 	if (!ret) {
4444a521ccaSLars Ellenberg 		ret = single_open(file, show, data);
4454a521ccaSLars Ellenberg 		if (ret)
4464a521ccaSLars Ellenberg 			kref_put(kref, release);
4474a521ccaSLars Ellenberg 	}
4484a521ccaSLars Ellenberg out:
4494a521ccaSLars Ellenberg 	return ret;
4504a521ccaSLars Ellenberg }
4514a521ccaSLars Ellenberg 
452db1866ffSLars Ellenberg static int in_flight_summary_open(struct inode *inode, struct file *file)
453db1866ffSLars Ellenberg {
4544a521ccaSLars Ellenberg 	struct drbd_resource *resource = inode->i_private;
4554a521ccaSLars Ellenberg 	return drbd_single_open(file, in_flight_summary_show, resource,
4564a521ccaSLars Ellenberg 				&resource->kref, drbd_destroy_resource);
4574a521ccaSLars Ellenberg }
4584a521ccaSLars Ellenberg 
4594a521ccaSLars Ellenberg static int in_flight_summary_release(struct inode *inode, struct file *file)
4604a521ccaSLars Ellenberg {
4614a521ccaSLars Ellenberg 	struct drbd_resource *resource = inode->i_private;
4624a521ccaSLars Ellenberg 	kref_put(&resource->kref, drbd_destroy_resource);
4634a521ccaSLars Ellenberg 	return single_release(inode, file);
464db1866ffSLars Ellenberg }
465db1866ffSLars Ellenberg 
466db1866ffSLars Ellenberg static const struct file_operations in_flight_summary_fops = {
467db1866ffSLars Ellenberg 	.owner		= THIS_MODULE,
468db1866ffSLars Ellenberg 	.open		= in_flight_summary_open,
469db1866ffSLars Ellenberg 	.read		= seq_read,
470db1866ffSLars Ellenberg 	.llseek		= seq_lseek,
4714a521ccaSLars Ellenberg 	.release	= in_flight_summary_release,
472db1866ffSLars Ellenberg };
473db1866ffSLars Ellenberg 
4744d3d5aa8SLars Ellenberg void drbd_debugfs_resource_add(struct drbd_resource *resource)
4754d3d5aa8SLars Ellenberg {
4764d3d5aa8SLars Ellenberg 	struct dentry *dentry;
4774d3d5aa8SLars Ellenberg 	if (!drbd_debugfs_resources)
4784d3d5aa8SLars Ellenberg 		return;
4794d3d5aa8SLars Ellenberg 
4804d3d5aa8SLars Ellenberg 	dentry = debugfs_create_dir(resource->name, drbd_debugfs_resources);
4814d3d5aa8SLars Ellenberg 	if (IS_ERR_OR_NULL(dentry))
4824d3d5aa8SLars Ellenberg 		goto fail;
4834d3d5aa8SLars Ellenberg 	resource->debugfs_res = dentry;
4844d3d5aa8SLars Ellenberg 
4854d3d5aa8SLars Ellenberg 	dentry = debugfs_create_dir("volumes", resource->debugfs_res);
4864d3d5aa8SLars Ellenberg 	if (IS_ERR_OR_NULL(dentry))
4874d3d5aa8SLars Ellenberg 		goto fail;
4884d3d5aa8SLars Ellenberg 	resource->debugfs_res_volumes = dentry;
4894d3d5aa8SLars Ellenberg 
4904d3d5aa8SLars Ellenberg 	dentry = debugfs_create_dir("connections", resource->debugfs_res);
4914d3d5aa8SLars Ellenberg 	if (IS_ERR_OR_NULL(dentry))
4924d3d5aa8SLars Ellenberg 		goto fail;
4934d3d5aa8SLars Ellenberg 	resource->debugfs_res_connections = dentry;
4944d3d5aa8SLars Ellenberg 
495db1866ffSLars Ellenberg 	dentry = debugfs_create_file("in_flight_summary", S_IRUSR|S_IRGRP,
496db1866ffSLars Ellenberg 			resource->debugfs_res, resource,
497db1866ffSLars Ellenberg 			&in_flight_summary_fops);
498db1866ffSLars Ellenberg 	if (IS_ERR_OR_NULL(dentry))
499db1866ffSLars Ellenberg 		goto fail;
500db1866ffSLars Ellenberg 	resource->debugfs_res_in_flight_summary = dentry;
5014d3d5aa8SLars Ellenberg 	return;
5024d3d5aa8SLars Ellenberg 
5034d3d5aa8SLars Ellenberg fail:
5044d3d5aa8SLars Ellenberg 	drbd_debugfs_resource_cleanup(resource);
5054d3d5aa8SLars Ellenberg 	drbd_err(resource, "failed to create debugfs dentry\n");
5064d3d5aa8SLars Ellenberg }
5074d3d5aa8SLars Ellenberg 
5084d3d5aa8SLars Ellenberg static void drbd_debugfs_remove(struct dentry **dp)
5094d3d5aa8SLars Ellenberg {
5104d3d5aa8SLars Ellenberg 	debugfs_remove(*dp);
5114d3d5aa8SLars Ellenberg 	*dp = NULL;
5124d3d5aa8SLars Ellenberg }
5134d3d5aa8SLars Ellenberg 
5144d3d5aa8SLars Ellenberg void drbd_debugfs_resource_cleanup(struct drbd_resource *resource)
5154d3d5aa8SLars Ellenberg {
5164d3d5aa8SLars Ellenberg 	/* it is ok to call debugfs_remove(NULL) */
5174d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&resource->debugfs_res_in_flight_summary);
5184d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&resource->debugfs_res_connections);
5194d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&resource->debugfs_res_volumes);
5204d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&resource->debugfs_res);
5214d3d5aa8SLars Ellenberg }
5224d3d5aa8SLars Ellenberg 
523944410e9SLars Ellenberg static void seq_print_one_timing_detail(struct seq_file *m,
524944410e9SLars Ellenberg 	const struct drbd_thread_timing_details *tdp,
525944410e9SLars Ellenberg 	unsigned long now)
526944410e9SLars Ellenberg {
527944410e9SLars Ellenberg 	struct drbd_thread_timing_details td;
528944410e9SLars Ellenberg 	/* No locking...
529944410e9SLars Ellenberg 	 * use temporary assignment to get at consistent data. */
530944410e9SLars Ellenberg 	do {
531944410e9SLars Ellenberg 		td = *tdp;
532944410e9SLars Ellenberg 	} while (td.cb_nr != tdp->cb_nr);
533944410e9SLars Ellenberg 	if (!td.cb_addr)
534944410e9SLars Ellenberg 		return;
535944410e9SLars Ellenberg 	seq_printf(m, "%u\t%d\t%s:%u\t%ps\n",
536944410e9SLars Ellenberg 			td.cb_nr,
537944410e9SLars Ellenberg 			jiffies_to_msecs(now - td.start_jif),
538944410e9SLars Ellenberg 			td.caller_fn, td.line,
539944410e9SLars Ellenberg 			td.cb_addr);
540944410e9SLars Ellenberg }
541944410e9SLars Ellenberg 
542944410e9SLars Ellenberg static void seq_print_timing_details(struct seq_file *m,
543944410e9SLars Ellenberg 		const char *title,
544944410e9SLars Ellenberg 		unsigned int cb_nr, struct drbd_thread_timing_details *tdp, unsigned long now)
545944410e9SLars Ellenberg {
546944410e9SLars Ellenberg 	unsigned int start_idx;
547944410e9SLars Ellenberg 	unsigned int i;
548944410e9SLars Ellenberg 
549944410e9SLars Ellenberg 	seq_printf(m, "%s\n", title);
550944410e9SLars Ellenberg 	/* If not much is going on, this will result in natural ordering.
551944410e9SLars Ellenberg 	 * If it is very busy, we will possibly skip events, or even see wrap
552944410e9SLars Ellenberg 	 * arounds, which could only be avoided with locking.
553944410e9SLars Ellenberg 	 */
554944410e9SLars Ellenberg 	start_idx = cb_nr % DRBD_THREAD_DETAILS_HIST;
555944410e9SLars Ellenberg 	for (i = start_idx; i < DRBD_THREAD_DETAILS_HIST; i++)
556944410e9SLars Ellenberg 		seq_print_one_timing_detail(m, tdp+i, now);
557944410e9SLars Ellenberg 	for (i = 0; i < start_idx; i++)
558944410e9SLars Ellenberg 		seq_print_one_timing_detail(m, tdp+i, now);
559944410e9SLars Ellenberg }
560944410e9SLars Ellenberg 
561944410e9SLars Ellenberg static int callback_history_show(struct seq_file *m, void *ignored)
562944410e9SLars Ellenberg {
563944410e9SLars Ellenberg 	struct drbd_connection *connection = m->private;
564944410e9SLars Ellenberg 	unsigned long jif = jiffies;
565944410e9SLars Ellenberg 
566b44e1184SLars Ellenberg 	/* BUMP me if you change the file format/content/presentation */
567b44e1184SLars Ellenberg 	seq_printf(m, "v: %u\n\n", 0);
568b44e1184SLars Ellenberg 
569944410e9SLars Ellenberg 	seq_puts(m, "n\tage\tcallsite\tfn\n");
570944410e9SLars Ellenberg 	seq_print_timing_details(m, "worker", connection->w_cb_nr, connection->w_timing_details, jif);
571944410e9SLars Ellenberg 	seq_print_timing_details(m, "receiver", connection->r_cb_nr, connection->r_timing_details, jif);
572944410e9SLars Ellenberg 	return 0;
573944410e9SLars Ellenberg }
574944410e9SLars Ellenberg 
575944410e9SLars Ellenberg static int callback_history_open(struct inode *inode, struct file *file)
576944410e9SLars Ellenberg {
577944410e9SLars Ellenberg 	struct drbd_connection *connection = inode->i_private;
578944410e9SLars Ellenberg 	return drbd_single_open(file, callback_history_show, connection,
579944410e9SLars Ellenberg 				&connection->kref, drbd_destroy_connection);
580944410e9SLars Ellenberg }
581944410e9SLars Ellenberg 
582944410e9SLars Ellenberg static int callback_history_release(struct inode *inode, struct file *file)
583944410e9SLars Ellenberg {
584944410e9SLars Ellenberg 	struct drbd_connection *connection = inode->i_private;
585944410e9SLars Ellenberg 	kref_put(&connection->kref, drbd_destroy_connection);
586944410e9SLars Ellenberg 	return single_release(inode, file);
587944410e9SLars Ellenberg }
588944410e9SLars Ellenberg 
589944410e9SLars Ellenberg static const struct file_operations connection_callback_history_fops = {
590944410e9SLars Ellenberg 	.owner		= THIS_MODULE,
591944410e9SLars Ellenberg 	.open		= callback_history_open,
592944410e9SLars Ellenberg 	.read		= seq_read,
593944410e9SLars Ellenberg 	.llseek		= seq_lseek,
594944410e9SLars Ellenberg 	.release	= callback_history_release,
595944410e9SLars Ellenberg };
596944410e9SLars Ellenberg 
59754e6fc38SLars Ellenberg static int connection_oldest_requests_show(struct seq_file *m, void *ignored)
59854e6fc38SLars Ellenberg {
59954e6fc38SLars Ellenberg 	struct drbd_connection *connection = m->private;
60054e6fc38SLars Ellenberg 	unsigned long now = jiffies;
60154e6fc38SLars Ellenberg 	struct drbd_request *r1, *r2;
60254e6fc38SLars Ellenberg 
60354e6fc38SLars Ellenberg 	/* BUMP me if you change the file format/content/presentation */
60454e6fc38SLars Ellenberg 	seq_printf(m, "v: %u\n\n", 0);
60554e6fc38SLars Ellenberg 
60654e6fc38SLars Ellenberg 	spin_lock_irq(&connection->resource->req_lock);
60754e6fc38SLars Ellenberg 	r1 = connection->req_next;
60854e6fc38SLars Ellenberg 	if (r1)
60954e6fc38SLars Ellenberg 		seq_print_minor_vnr_req(m, r1, now);
61054e6fc38SLars Ellenberg 	r2 = connection->req_ack_pending;
61154e6fc38SLars Ellenberg 	if (r2 && r2 != r1) {
61254e6fc38SLars Ellenberg 		r1 = r2;
61354e6fc38SLars Ellenberg 		seq_print_minor_vnr_req(m, r1, now);
61454e6fc38SLars Ellenberg 	}
61554e6fc38SLars Ellenberg 	r2 = connection->req_not_net_done;
61654e6fc38SLars Ellenberg 	if (r2 && r2 != r1)
61754e6fc38SLars Ellenberg 		seq_print_minor_vnr_req(m, r2, now);
61854e6fc38SLars Ellenberg 	spin_unlock_irq(&connection->resource->req_lock);
61954e6fc38SLars Ellenberg 	return 0;
62054e6fc38SLars Ellenberg }
62154e6fc38SLars Ellenberg 
62254e6fc38SLars Ellenberg static int connection_oldest_requests_open(struct inode *inode, struct file *file)
62354e6fc38SLars Ellenberg {
62454e6fc38SLars Ellenberg 	struct drbd_connection *connection = inode->i_private;
62554e6fc38SLars Ellenberg 	return drbd_single_open(file, connection_oldest_requests_show, connection,
62654e6fc38SLars Ellenberg 				&connection->kref, drbd_destroy_connection);
62754e6fc38SLars Ellenberg }
62854e6fc38SLars Ellenberg 
62954e6fc38SLars Ellenberg static int connection_oldest_requests_release(struct inode *inode, struct file *file)
63054e6fc38SLars Ellenberg {
63154e6fc38SLars Ellenberg 	struct drbd_connection *connection = inode->i_private;
63254e6fc38SLars Ellenberg 	kref_put(&connection->kref, drbd_destroy_connection);
63354e6fc38SLars Ellenberg 	return single_release(inode, file);
63454e6fc38SLars Ellenberg }
63554e6fc38SLars Ellenberg 
63654e6fc38SLars Ellenberg static const struct file_operations connection_oldest_requests_fops = {
63754e6fc38SLars Ellenberg 	.owner		= THIS_MODULE,
63854e6fc38SLars Ellenberg 	.open		= connection_oldest_requests_open,
63954e6fc38SLars Ellenberg 	.read		= seq_read,
64054e6fc38SLars Ellenberg 	.llseek		= seq_lseek,
64154e6fc38SLars Ellenberg 	.release	= connection_oldest_requests_release,
64254e6fc38SLars Ellenberg };
64354e6fc38SLars Ellenberg 
6444d3d5aa8SLars Ellenberg void drbd_debugfs_connection_add(struct drbd_connection *connection)
6454d3d5aa8SLars Ellenberg {
6464d3d5aa8SLars Ellenberg 	struct dentry *conns_dir = connection->resource->debugfs_res_connections;
6474d3d5aa8SLars Ellenberg 	struct dentry *dentry;
6484d3d5aa8SLars Ellenberg 	if (!conns_dir)
6494d3d5aa8SLars Ellenberg 		return;
6504d3d5aa8SLars Ellenberg 
6514d3d5aa8SLars Ellenberg 	/* Once we enable mutliple peers,
6524d3d5aa8SLars Ellenberg 	 * these connections will have descriptive names.
6534d3d5aa8SLars Ellenberg 	 * For now, it is just the one connection to the (only) "peer". */
6544d3d5aa8SLars Ellenberg 	dentry = debugfs_create_dir("peer", conns_dir);
6554d3d5aa8SLars Ellenberg 	if (IS_ERR_OR_NULL(dentry))
6564d3d5aa8SLars Ellenberg 		goto fail;
6574d3d5aa8SLars Ellenberg 	connection->debugfs_conn = dentry;
658944410e9SLars Ellenberg 
659944410e9SLars Ellenberg 	dentry = debugfs_create_file("callback_history", S_IRUSR|S_IRGRP,
660944410e9SLars Ellenberg 			connection->debugfs_conn, connection,
661944410e9SLars Ellenberg 			&connection_callback_history_fops);
662944410e9SLars Ellenberg 	if (IS_ERR_OR_NULL(dentry))
663944410e9SLars Ellenberg 		goto fail;
664944410e9SLars Ellenberg 	connection->debugfs_conn_callback_history = dentry;
6653d299f48SLars Ellenberg 
6663d299f48SLars Ellenberg 	dentry = debugfs_create_file("oldest_requests", S_IRUSR|S_IRGRP,
6673d299f48SLars Ellenberg 			connection->debugfs_conn, connection,
6683d299f48SLars Ellenberg 			&connection_oldest_requests_fops);
6693d299f48SLars Ellenberg 	if (IS_ERR_OR_NULL(dentry))
6703d299f48SLars Ellenberg 		goto fail;
6713d299f48SLars Ellenberg 	connection->debugfs_conn_oldest_requests = dentry;
6724d3d5aa8SLars Ellenberg 	return;
6734d3d5aa8SLars Ellenberg 
6744d3d5aa8SLars Ellenberg fail:
6754d3d5aa8SLars Ellenberg 	drbd_debugfs_connection_cleanup(connection);
6764d3d5aa8SLars Ellenberg 	drbd_err(connection, "failed to create debugfs dentry\n");
6774d3d5aa8SLars Ellenberg }
6784d3d5aa8SLars Ellenberg 
6794d3d5aa8SLars Ellenberg void drbd_debugfs_connection_cleanup(struct drbd_connection *connection)
6804d3d5aa8SLars Ellenberg {
6814d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&connection->debugfs_conn_callback_history);
6824d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&connection->debugfs_conn_oldest_requests);
6834d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&connection->debugfs_conn);
6844d3d5aa8SLars Ellenberg }
6854d3d5aa8SLars Ellenberg 
68654e6fc38SLars Ellenberg static void resync_dump_detail(struct seq_file *m, struct lc_element *e)
68754e6fc38SLars Ellenberg {
68854e6fc38SLars Ellenberg        struct bm_extent *bme = lc_entry(e, struct bm_extent, lce);
68954e6fc38SLars Ellenberg 
690f0c21e62SPhilipp Marek        seq_printf(m, "%5d %s %s %s", bme->rs_left,
69154e6fc38SLars Ellenberg 		  test_bit(BME_NO_WRITES, &bme->flags) ? "NO_WRITES" : "---------",
69254e6fc38SLars Ellenberg 		  test_bit(BME_LOCKED, &bme->flags) ? "LOCKED" : "------",
69354e6fc38SLars Ellenberg 		  test_bit(BME_PRIORITY, &bme->flags) ? "PRIORITY" : "--------"
69454e6fc38SLars Ellenberg 		  );
69554e6fc38SLars Ellenberg }
69654e6fc38SLars Ellenberg 
69754e6fc38SLars Ellenberg static int device_resync_extents_show(struct seq_file *m, void *ignored)
69854e6fc38SLars Ellenberg {
69954e6fc38SLars Ellenberg 	struct drbd_device *device = m->private;
700b44e1184SLars Ellenberg 
701b44e1184SLars Ellenberg 	/* BUMP me if you change the file format/content/presentation */
702b44e1184SLars Ellenberg 	seq_printf(m, "v: %u\n\n", 0);
703b44e1184SLars Ellenberg 
70454e6fc38SLars Ellenberg 	if (get_ldev_if_state(device, D_FAILED)) {
70554e6fc38SLars Ellenberg 		lc_seq_printf_stats(m, device->resync);
70654e6fc38SLars Ellenberg 		lc_seq_dump_details(m, device->resync, "rs_left flags", resync_dump_detail);
70754e6fc38SLars Ellenberg 		put_ldev(device);
70854e6fc38SLars Ellenberg 	}
70954e6fc38SLars Ellenberg 	return 0;
71054e6fc38SLars Ellenberg }
71154e6fc38SLars Ellenberg 
71254e6fc38SLars Ellenberg static int device_act_log_extents_show(struct seq_file *m, void *ignored)
71354e6fc38SLars Ellenberg {
71454e6fc38SLars Ellenberg 	struct drbd_device *device = m->private;
715b44e1184SLars Ellenberg 
716b44e1184SLars Ellenberg 	/* BUMP me if you change the file format/content/presentation */
717b44e1184SLars Ellenberg 	seq_printf(m, "v: %u\n\n", 0);
718b44e1184SLars Ellenberg 
71954e6fc38SLars Ellenberg 	if (get_ldev_if_state(device, D_FAILED)) {
72054e6fc38SLars Ellenberg 		lc_seq_printf_stats(m, device->act_log);
72154e6fc38SLars Ellenberg 		lc_seq_dump_details(m, device->act_log, "", NULL);
72254e6fc38SLars Ellenberg 		put_ldev(device);
72354e6fc38SLars Ellenberg 	}
72454e6fc38SLars Ellenberg 	return 0;
72554e6fc38SLars Ellenberg }
72654e6fc38SLars Ellenberg 
72754e6fc38SLars Ellenberg static int device_oldest_requests_show(struct seq_file *m, void *ignored)
72854e6fc38SLars Ellenberg {
72954e6fc38SLars Ellenberg 	struct drbd_device *device = m->private;
73054e6fc38SLars Ellenberg 	struct drbd_resource *resource = device->resource;
73154e6fc38SLars Ellenberg 	unsigned long now = jiffies;
73254e6fc38SLars Ellenberg 	struct drbd_request *r1, *r2;
73354e6fc38SLars Ellenberg 	int i;
73454e6fc38SLars Ellenberg 
735b44e1184SLars Ellenberg 	/* BUMP me if you change the file format/content/presentation */
736b44e1184SLars Ellenberg 	seq_printf(m, "v: %u\n\n", 0);
737b44e1184SLars Ellenberg 
73854e6fc38SLars Ellenberg 	seq_puts(m, RQ_HDR);
73954e6fc38SLars Ellenberg 	spin_lock_irq(&resource->req_lock);
74054e6fc38SLars Ellenberg 	/* WRITE, then READ */
74154e6fc38SLars Ellenberg 	for (i = 1; i >= 0; --i) {
74254e6fc38SLars Ellenberg 		r1 = list_first_entry_or_null(&device->pending_master_completion[i],
74354e6fc38SLars Ellenberg 			struct drbd_request, req_pending_master_completion);
74454e6fc38SLars Ellenberg 		r2 = list_first_entry_or_null(&device->pending_completion[i],
74554e6fc38SLars Ellenberg 			struct drbd_request, req_pending_local);
74654e6fc38SLars Ellenberg 		if (r1)
74754e6fc38SLars Ellenberg 			seq_print_one_request(m, r1, now);
74854e6fc38SLars Ellenberg 		if (r2 && r2 != r1)
74954e6fc38SLars Ellenberg 			seq_print_one_request(m, r2, now);
75054e6fc38SLars Ellenberg 	}
75154e6fc38SLars Ellenberg 	spin_unlock_irq(&resource->req_lock);
75254e6fc38SLars Ellenberg 	return 0;
75354e6fc38SLars Ellenberg }
75454e6fc38SLars Ellenberg 
755cc356f85SLars Ellenberg static int device_data_gen_id_show(struct seq_file *m, void *ignored)
756cc356f85SLars Ellenberg {
757cc356f85SLars Ellenberg 	struct drbd_device *device = m->private;
758cc356f85SLars Ellenberg 	struct drbd_md *md;
759cc356f85SLars Ellenberg 	enum drbd_uuid_index idx;
760cc356f85SLars Ellenberg 
761cc356f85SLars Ellenberg 	if (!get_ldev_if_state(device, D_FAILED))
762cc356f85SLars Ellenberg 		return -ENODEV;
763cc356f85SLars Ellenberg 
764cc356f85SLars Ellenberg 	md = &device->ldev->md;
765cc356f85SLars Ellenberg 	spin_lock_irq(&md->uuid_lock);
766cc356f85SLars Ellenberg 	for (idx = UI_CURRENT; idx <= UI_HISTORY_END; idx++) {
767cc356f85SLars Ellenberg 		seq_printf(m, "0x%016llX\n", md->uuid[idx]);
768cc356f85SLars Ellenberg 	}
769cc356f85SLars Ellenberg 	spin_unlock_irq(&md->uuid_lock);
770cc356f85SLars Ellenberg 	put_ldev(device);
771cc356f85SLars Ellenberg 	return 0;
772cc356f85SLars Ellenberg }
773cc356f85SLars Ellenberg 
77454e6fc38SLars Ellenberg #define drbd_debugfs_device_attr(name)						\
77554e6fc38SLars Ellenberg static int device_ ## name ## _open(struct inode *inode, struct file *file)	\
77654e6fc38SLars Ellenberg {										\
77754e6fc38SLars Ellenberg 	struct drbd_device *device = inode->i_private;				\
77854e6fc38SLars Ellenberg 	return drbd_single_open(file, device_ ## name ## _show, device,		\
77954e6fc38SLars Ellenberg 				&device->kref, drbd_destroy_device);		\
78054e6fc38SLars Ellenberg }										\
78154e6fc38SLars Ellenberg static int device_ ## name ## _release(struct inode *inode, struct file *file)	\
78254e6fc38SLars Ellenberg {										\
78354e6fc38SLars Ellenberg 	struct drbd_device *device = inode->i_private;				\
78454e6fc38SLars Ellenberg 	kref_put(&device->kref, drbd_destroy_device);				\
78554e6fc38SLars Ellenberg 	return single_release(inode, file);					\
78654e6fc38SLars Ellenberg }										\
78754e6fc38SLars Ellenberg static const struct file_operations device_ ## name ## _fops = {		\
78854e6fc38SLars Ellenberg 	.owner		= THIS_MODULE,						\
78954e6fc38SLars Ellenberg 	.open		= device_ ## name ## _open,				\
79054e6fc38SLars Ellenberg 	.read		= seq_read,						\
79154e6fc38SLars Ellenberg 	.llseek		= seq_lseek,						\
79254e6fc38SLars Ellenberg 	.release	= device_ ## name ## _release,				\
79354e6fc38SLars Ellenberg };
79454e6fc38SLars Ellenberg 
79554e6fc38SLars Ellenberg drbd_debugfs_device_attr(oldest_requests)
79654e6fc38SLars Ellenberg drbd_debugfs_device_attr(act_log_extents)
79754e6fc38SLars Ellenberg drbd_debugfs_device_attr(resync_extents)
798cc356f85SLars Ellenberg drbd_debugfs_device_attr(data_gen_id)
79954e6fc38SLars Ellenberg 
8004d3d5aa8SLars Ellenberg void drbd_debugfs_device_add(struct drbd_device *device)
8014d3d5aa8SLars Ellenberg {
8024d3d5aa8SLars Ellenberg 	struct dentry *vols_dir = device->resource->debugfs_res_volumes;
8034d3d5aa8SLars Ellenberg 	char minor_buf[8]; /* MINORMASK, MINORBITS == 20; */
8044d3d5aa8SLars Ellenberg 	char vnr_buf[8];   /* volume number vnr is even 16 bit only; */
8054d3d5aa8SLars Ellenberg 	char *slink_name = NULL;
8064d3d5aa8SLars Ellenberg 
8074d3d5aa8SLars Ellenberg 	struct dentry *dentry;
8084d3d5aa8SLars Ellenberg 	if (!vols_dir || !drbd_debugfs_minors)
8094d3d5aa8SLars Ellenberg 		return;
8104d3d5aa8SLars Ellenberg 
8114d3d5aa8SLars Ellenberg 	snprintf(vnr_buf, sizeof(vnr_buf), "%u", device->vnr);
8124d3d5aa8SLars Ellenberg 	dentry = debugfs_create_dir(vnr_buf, vols_dir);
8134d3d5aa8SLars Ellenberg 	if (IS_ERR_OR_NULL(dentry))
8144d3d5aa8SLars Ellenberg 		goto fail;
8154d3d5aa8SLars Ellenberg 	device->debugfs_vol = dentry;
8164d3d5aa8SLars Ellenberg 
8174d3d5aa8SLars Ellenberg 	snprintf(minor_buf, sizeof(minor_buf), "%u", device->minor);
8184d3d5aa8SLars Ellenberg 	slink_name = kasprintf(GFP_KERNEL, "../resources/%s/volumes/%u",
8194d3d5aa8SLars Ellenberg 			device->resource->name, device->vnr);
8204d3d5aa8SLars Ellenberg 	if (!slink_name)
8214d3d5aa8SLars Ellenberg 		goto fail;
8224d3d5aa8SLars Ellenberg 	dentry = debugfs_create_symlink(minor_buf, drbd_debugfs_minors, slink_name);
82354e6fc38SLars Ellenberg 	kfree(slink_name);
82454e6fc38SLars Ellenberg 	slink_name = NULL;
8254d3d5aa8SLars Ellenberg 	if (IS_ERR_OR_NULL(dentry))
8264d3d5aa8SLars Ellenberg 		goto fail;
8274d3d5aa8SLars Ellenberg 	device->debugfs_minor = dentry;
82854e6fc38SLars Ellenberg 
82954e6fc38SLars Ellenberg #define DCF(name)	do {					\
83054e6fc38SLars Ellenberg 	dentry = debugfs_create_file(#name, S_IRUSR|S_IRGRP,	\
83154e6fc38SLars Ellenberg 			device->debugfs_vol, device,		\
83254e6fc38SLars Ellenberg 			&device_ ## name ## _fops);		\
83354e6fc38SLars Ellenberg 	if (IS_ERR_OR_NULL(dentry))				\
83454e6fc38SLars Ellenberg 		goto fail;					\
83554e6fc38SLars Ellenberg 	device->debugfs_vol_ ## name = dentry;			\
83654e6fc38SLars Ellenberg 	} while (0)
83754e6fc38SLars Ellenberg 
83854e6fc38SLars Ellenberg 	DCF(oldest_requests);
83954e6fc38SLars Ellenberg 	DCF(act_log_extents);
84054e6fc38SLars Ellenberg 	DCF(resync_extents);
841cc356f85SLars Ellenberg 	DCF(data_gen_id);
842cc356f85SLars Ellenberg #undef DCF
84354e6fc38SLars Ellenberg 	return;
8444d3d5aa8SLars Ellenberg 
8454d3d5aa8SLars Ellenberg fail:
8464d3d5aa8SLars Ellenberg 	drbd_debugfs_device_cleanup(device);
8474d3d5aa8SLars Ellenberg 	drbd_err(device, "failed to create debugfs entries\n");
8484d3d5aa8SLars Ellenberg }
8494d3d5aa8SLars Ellenberg 
8504d3d5aa8SLars Ellenberg void drbd_debugfs_device_cleanup(struct drbd_device *device)
8514d3d5aa8SLars Ellenberg {
8524d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&device->debugfs_minor);
8534d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&device->debugfs_vol_oldest_requests);
8544d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&device->debugfs_vol_act_log_extents);
8554d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&device->debugfs_vol_resync_extents);
8564d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&device->debugfs_vol_data_gen_id);
8574d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&device->debugfs_vol);
8584d3d5aa8SLars Ellenberg }
8594d3d5aa8SLars Ellenberg 
8604d3d5aa8SLars Ellenberg void drbd_debugfs_peer_device_add(struct drbd_peer_device *peer_device)
8614d3d5aa8SLars Ellenberg {
8624d3d5aa8SLars Ellenberg 	struct dentry *conn_dir = peer_device->connection->debugfs_conn;
8634d3d5aa8SLars Ellenberg 	struct dentry *dentry;
8644d3d5aa8SLars Ellenberg 	char vnr_buf[8];
8654d3d5aa8SLars Ellenberg 
8664d3d5aa8SLars Ellenberg 	if (!conn_dir)
8674d3d5aa8SLars Ellenberg 		return;
8684d3d5aa8SLars Ellenberg 
8694d3d5aa8SLars Ellenberg 	snprintf(vnr_buf, sizeof(vnr_buf), "%u", peer_device->device->vnr);
8704d3d5aa8SLars Ellenberg 	dentry = debugfs_create_dir(vnr_buf, conn_dir);
8714d3d5aa8SLars Ellenberg 	if (IS_ERR_OR_NULL(dentry))
8724d3d5aa8SLars Ellenberg 		goto fail;
8734d3d5aa8SLars Ellenberg 	peer_device->debugfs_peer_dev = dentry;
8744d3d5aa8SLars Ellenberg 	return;
8754d3d5aa8SLars Ellenberg 
8764d3d5aa8SLars Ellenberg fail:
8774d3d5aa8SLars Ellenberg 	drbd_debugfs_peer_device_cleanup(peer_device);
8784d3d5aa8SLars Ellenberg 	drbd_err(peer_device, "failed to create debugfs entries\n");
8794d3d5aa8SLars Ellenberg }
8804d3d5aa8SLars Ellenberg 
8814d3d5aa8SLars Ellenberg void drbd_debugfs_peer_device_cleanup(struct drbd_peer_device *peer_device)
8824d3d5aa8SLars Ellenberg {
8834d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&peer_device->debugfs_peer_dev);
8844d3d5aa8SLars Ellenberg }
8854d3d5aa8SLars Ellenberg 
886b44e1184SLars Ellenberg static int drbd_version_show(struct seq_file *m, void *ignored)
887b44e1184SLars Ellenberg {
888b44e1184SLars Ellenberg 	seq_printf(m, "# %s\n", drbd_buildtag());
889b44e1184SLars Ellenberg 	seq_printf(m, "VERSION=%s\n", REL_VERSION);
890b44e1184SLars Ellenberg 	seq_printf(m, "API_VERSION=%u\n", API_VERSION);
891b44e1184SLars Ellenberg 	seq_printf(m, "PRO_VERSION_MIN=%u\n", PRO_VERSION_MIN);
892b44e1184SLars Ellenberg 	seq_printf(m, "PRO_VERSION_MAX=%u\n", PRO_VERSION_MAX);
893b44e1184SLars Ellenberg 	return 0;
894b44e1184SLars Ellenberg }
895b44e1184SLars Ellenberg 
896b44e1184SLars Ellenberg static int drbd_version_open(struct inode *inode, struct file *file)
897b44e1184SLars Ellenberg {
898b44e1184SLars Ellenberg 	return single_open(file, drbd_version_show, NULL);
899b44e1184SLars Ellenberg }
900b44e1184SLars Ellenberg 
901b44e1184SLars Ellenberg static struct file_operations drbd_version_fops = {
902b44e1184SLars Ellenberg 	.owner = THIS_MODULE,
903b44e1184SLars Ellenberg 	.open = drbd_version_open,
904b44e1184SLars Ellenberg 	.llseek = seq_lseek,
905b44e1184SLars Ellenberg 	.read = seq_read,
906b44e1184SLars Ellenberg 	.release = single_release,
907b44e1184SLars Ellenberg };
908b44e1184SLars Ellenberg 
9094d3d5aa8SLars Ellenberg /* not __exit, may be indirectly called
9104d3d5aa8SLars Ellenberg  * from the module-load-failure path as well. */
9114d3d5aa8SLars Ellenberg void drbd_debugfs_cleanup(void)
9124d3d5aa8SLars Ellenberg {
9134d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&drbd_debugfs_resources);
9144d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&drbd_debugfs_minors);
915b44e1184SLars Ellenberg 	drbd_debugfs_remove(&drbd_debugfs_version);
9164d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&drbd_debugfs_root);
9174d3d5aa8SLars Ellenberg }
9184d3d5aa8SLars Ellenberg 
9194d3d5aa8SLars Ellenberg int __init drbd_debugfs_init(void)
9204d3d5aa8SLars Ellenberg {
9214d3d5aa8SLars Ellenberg 	struct dentry *dentry;
9224d3d5aa8SLars Ellenberg 
9234d3d5aa8SLars Ellenberg 	dentry = debugfs_create_dir("drbd", NULL);
9244d3d5aa8SLars Ellenberg 	if (IS_ERR_OR_NULL(dentry))
9254d3d5aa8SLars Ellenberg 		goto fail;
9264d3d5aa8SLars Ellenberg 	drbd_debugfs_root = dentry;
9274d3d5aa8SLars Ellenberg 
928b44e1184SLars Ellenberg 	dentry = debugfs_create_file("version", 0444, drbd_debugfs_root, NULL, &drbd_version_fops);
929b44e1184SLars Ellenberg 	if (IS_ERR_OR_NULL(dentry))
930b44e1184SLars Ellenberg 		goto fail;
931b44e1184SLars Ellenberg 	drbd_debugfs_version = dentry;
932b44e1184SLars Ellenberg 
9334d3d5aa8SLars Ellenberg 	dentry = debugfs_create_dir("resources", drbd_debugfs_root);
9344d3d5aa8SLars Ellenberg 	if (IS_ERR_OR_NULL(dentry))
9354d3d5aa8SLars Ellenberg 		goto fail;
9364d3d5aa8SLars Ellenberg 	drbd_debugfs_resources = dentry;
9374d3d5aa8SLars Ellenberg 
9384d3d5aa8SLars Ellenberg 	dentry = debugfs_create_dir("minors", drbd_debugfs_root);
9394d3d5aa8SLars Ellenberg 	if (IS_ERR_OR_NULL(dentry))
9404d3d5aa8SLars Ellenberg 		goto fail;
9414d3d5aa8SLars Ellenberg 	drbd_debugfs_minors = dentry;
9424d3d5aa8SLars Ellenberg 	return 0;
9434d3d5aa8SLars Ellenberg 
9444d3d5aa8SLars Ellenberg fail:
9454d3d5aa8SLars Ellenberg 	drbd_debugfs_cleanup();
9464d3d5aa8SLars Ellenberg 	if (dentry)
9474d3d5aa8SLars Ellenberg 		return PTR_ERR(dentry);
9484d3d5aa8SLars Ellenberg 	else
9494d3d5aa8SLars Ellenberg 		return -EINVAL;
9504d3d5aa8SLars Ellenberg }
951