xref: /openbmc/linux/drivers/block/drbd/drbd_debugfs.c (revision b44e1184fc3158821a74c870f777a39e4aac2ce5)
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 
14*b44e1184SLars Ellenberg 
15*b44e1184SLars Ellenberg /**********************************************************************
16*b44e1184SLars Ellenberg  * Whenever you change the file format, remember to bump the version. *
17*b44e1184SLars Ellenberg  **********************************************************************/
18*b44e1184SLars Ellenberg 
194d3d5aa8SLars Ellenberg static struct dentry *drbd_debugfs_root;
20*b44e1184SLars 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 
377*b44e1184SLars Ellenberg 	/* BUMP me if you change the file format/content/presentation */
378*b44e1184SLars Ellenberg 	seq_printf(m, "v: %u\n\n", 0);
379*b44e1184SLars 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 /* simple_positive(file->f_dentry) respectively debugfs_positive(),
4234a521ccaSLars Ellenberg  * but neither is "reachable" from here.
4244a521ccaSLars Ellenberg  * So we have our own inline version of it above.  :-( */
4254a521ccaSLars Ellenberg static inline int debugfs_positive(struct dentry *dentry)
4264a521ccaSLars Ellenberg {
4274a521ccaSLars Ellenberg         return dentry->d_inode && !d_unhashed(dentry);
4284a521ccaSLars Ellenberg }
4294a521ccaSLars Ellenberg 
4304a521ccaSLars Ellenberg /* make sure at *open* time that the respective object won't go away. */
4314a521ccaSLars Ellenberg static int drbd_single_open(struct file *file, int (*show)(struct seq_file *, void *),
4324a521ccaSLars Ellenberg 		                void *data, struct kref *kref,
4334a521ccaSLars Ellenberg 				void (*release)(struct kref *))
4344a521ccaSLars Ellenberg {
4354a521ccaSLars Ellenberg 	struct dentry *parent;
4364a521ccaSLars Ellenberg 	int ret = -ESTALE;
4374a521ccaSLars Ellenberg 
4384a521ccaSLars Ellenberg 	/* Are we still linked,
4394a521ccaSLars Ellenberg 	 * or has debugfs_remove() already been called? */
4404a521ccaSLars Ellenberg 	parent = file->f_dentry->d_parent;
4414a521ccaSLars Ellenberg 	/* not sure if this can happen: */
4424a521ccaSLars Ellenberg 	if (!parent || !parent->d_inode)
4434a521ccaSLars Ellenberg 		goto out;
4444a521ccaSLars Ellenberg 	/* serialize with d_delete() */
4454a521ccaSLars Ellenberg 	mutex_lock(&parent->d_inode->i_mutex);
4464a521ccaSLars Ellenberg 	/* Make sure the object is still alive */
44754e6fc38SLars Ellenberg 	if (debugfs_positive(file->f_dentry)
44854e6fc38SLars Ellenberg 	&& kref_get_unless_zero(kref))
4494a521ccaSLars Ellenberg 		ret = 0;
4504a521ccaSLars Ellenberg 	mutex_unlock(&parent->d_inode->i_mutex);
4514a521ccaSLars Ellenberg 	if (!ret) {
4524a521ccaSLars Ellenberg 		ret = single_open(file, show, data);
4534a521ccaSLars Ellenberg 		if (ret)
4544a521ccaSLars Ellenberg 			kref_put(kref, release);
4554a521ccaSLars Ellenberg 	}
4564a521ccaSLars Ellenberg out:
4574a521ccaSLars Ellenberg 	return ret;
4584a521ccaSLars Ellenberg }
4594a521ccaSLars Ellenberg 
460db1866ffSLars Ellenberg static int in_flight_summary_open(struct inode *inode, struct file *file)
461db1866ffSLars Ellenberg {
4624a521ccaSLars Ellenberg 	struct drbd_resource *resource = inode->i_private;
4634a521ccaSLars Ellenberg 	return drbd_single_open(file, in_flight_summary_show, resource,
4644a521ccaSLars Ellenberg 				&resource->kref, drbd_destroy_resource);
4654a521ccaSLars Ellenberg }
4664a521ccaSLars Ellenberg 
4674a521ccaSLars Ellenberg static int in_flight_summary_release(struct inode *inode, struct file *file)
4684a521ccaSLars Ellenberg {
4694a521ccaSLars Ellenberg 	struct drbd_resource *resource = inode->i_private;
4704a521ccaSLars Ellenberg 	kref_put(&resource->kref, drbd_destroy_resource);
4714a521ccaSLars Ellenberg 	return single_release(inode, file);
472db1866ffSLars Ellenberg }
473db1866ffSLars Ellenberg 
474db1866ffSLars Ellenberg static const struct file_operations in_flight_summary_fops = {
475db1866ffSLars Ellenberg 	.owner		= THIS_MODULE,
476db1866ffSLars Ellenberg 	.open		= in_flight_summary_open,
477db1866ffSLars Ellenberg 	.read		= seq_read,
478db1866ffSLars Ellenberg 	.llseek		= seq_lseek,
4794a521ccaSLars Ellenberg 	.release	= in_flight_summary_release,
480db1866ffSLars Ellenberg };
481db1866ffSLars Ellenberg 
4824d3d5aa8SLars Ellenberg void drbd_debugfs_resource_add(struct drbd_resource *resource)
4834d3d5aa8SLars Ellenberg {
4844d3d5aa8SLars Ellenberg 	struct dentry *dentry;
4854d3d5aa8SLars Ellenberg 	if (!drbd_debugfs_resources)
4864d3d5aa8SLars Ellenberg 		return;
4874d3d5aa8SLars Ellenberg 
4884d3d5aa8SLars Ellenberg 	dentry = debugfs_create_dir(resource->name, drbd_debugfs_resources);
4894d3d5aa8SLars Ellenberg 	if (IS_ERR_OR_NULL(dentry))
4904d3d5aa8SLars Ellenberg 		goto fail;
4914d3d5aa8SLars Ellenberg 	resource->debugfs_res = dentry;
4924d3d5aa8SLars Ellenberg 
4934d3d5aa8SLars Ellenberg 	dentry = debugfs_create_dir("volumes", resource->debugfs_res);
4944d3d5aa8SLars Ellenberg 	if (IS_ERR_OR_NULL(dentry))
4954d3d5aa8SLars Ellenberg 		goto fail;
4964d3d5aa8SLars Ellenberg 	resource->debugfs_res_volumes = dentry;
4974d3d5aa8SLars Ellenberg 
4984d3d5aa8SLars Ellenberg 	dentry = debugfs_create_dir("connections", resource->debugfs_res);
4994d3d5aa8SLars Ellenberg 	if (IS_ERR_OR_NULL(dentry))
5004d3d5aa8SLars Ellenberg 		goto fail;
5014d3d5aa8SLars Ellenberg 	resource->debugfs_res_connections = dentry;
5024d3d5aa8SLars Ellenberg 
503db1866ffSLars Ellenberg 	dentry = debugfs_create_file("in_flight_summary", S_IRUSR|S_IRGRP,
504db1866ffSLars Ellenberg 			resource->debugfs_res, resource,
505db1866ffSLars Ellenberg 			&in_flight_summary_fops);
506db1866ffSLars Ellenberg 	if (IS_ERR_OR_NULL(dentry))
507db1866ffSLars Ellenberg 		goto fail;
508db1866ffSLars Ellenberg 	resource->debugfs_res_in_flight_summary = dentry;
5094d3d5aa8SLars Ellenberg 	return;
5104d3d5aa8SLars Ellenberg 
5114d3d5aa8SLars Ellenberg fail:
5124d3d5aa8SLars Ellenberg 	drbd_debugfs_resource_cleanup(resource);
5134d3d5aa8SLars Ellenberg 	drbd_err(resource, "failed to create debugfs dentry\n");
5144d3d5aa8SLars Ellenberg }
5154d3d5aa8SLars Ellenberg 
5164d3d5aa8SLars Ellenberg static void drbd_debugfs_remove(struct dentry **dp)
5174d3d5aa8SLars Ellenberg {
5184d3d5aa8SLars Ellenberg 	debugfs_remove(*dp);
5194d3d5aa8SLars Ellenberg 	*dp = NULL;
5204d3d5aa8SLars Ellenberg }
5214d3d5aa8SLars Ellenberg 
5224d3d5aa8SLars Ellenberg void drbd_debugfs_resource_cleanup(struct drbd_resource *resource)
5234d3d5aa8SLars Ellenberg {
5244d3d5aa8SLars Ellenberg 	/* it is ok to call debugfs_remove(NULL) */
5254d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&resource->debugfs_res_in_flight_summary);
5264d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&resource->debugfs_res_connections);
5274d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&resource->debugfs_res_volumes);
5284d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&resource->debugfs_res);
5294d3d5aa8SLars Ellenberg }
5304d3d5aa8SLars Ellenberg 
531944410e9SLars Ellenberg static void seq_print_one_timing_detail(struct seq_file *m,
532944410e9SLars Ellenberg 	const struct drbd_thread_timing_details *tdp,
533944410e9SLars Ellenberg 	unsigned long now)
534944410e9SLars Ellenberg {
535944410e9SLars Ellenberg 	struct drbd_thread_timing_details td;
536944410e9SLars Ellenberg 	/* No locking...
537944410e9SLars Ellenberg 	 * use temporary assignment to get at consistent data. */
538944410e9SLars Ellenberg 	do {
539944410e9SLars Ellenberg 		td = *tdp;
540944410e9SLars Ellenberg 	} while (td.cb_nr != tdp->cb_nr);
541944410e9SLars Ellenberg 	if (!td.cb_addr)
542944410e9SLars Ellenberg 		return;
543944410e9SLars Ellenberg 	seq_printf(m, "%u\t%d\t%s:%u\t%ps\n",
544944410e9SLars Ellenberg 			td.cb_nr,
545944410e9SLars Ellenberg 			jiffies_to_msecs(now - td.start_jif),
546944410e9SLars Ellenberg 			td.caller_fn, td.line,
547944410e9SLars Ellenberg 			td.cb_addr);
548944410e9SLars Ellenberg }
549944410e9SLars Ellenberg 
550944410e9SLars Ellenberg static void seq_print_timing_details(struct seq_file *m,
551944410e9SLars Ellenberg 		const char *title,
552944410e9SLars Ellenberg 		unsigned int cb_nr, struct drbd_thread_timing_details *tdp, unsigned long now)
553944410e9SLars Ellenberg {
554944410e9SLars Ellenberg 	unsigned int start_idx;
555944410e9SLars Ellenberg 	unsigned int i;
556944410e9SLars Ellenberg 
557944410e9SLars Ellenberg 	seq_printf(m, "%s\n", title);
558944410e9SLars Ellenberg 	/* If not much is going on, this will result in natural ordering.
559944410e9SLars Ellenberg 	 * If it is very busy, we will possibly skip events, or even see wrap
560944410e9SLars Ellenberg 	 * arounds, which could only be avoided with locking.
561944410e9SLars Ellenberg 	 */
562944410e9SLars Ellenberg 	start_idx = cb_nr % DRBD_THREAD_DETAILS_HIST;
563944410e9SLars Ellenberg 	for (i = start_idx; i < DRBD_THREAD_DETAILS_HIST; i++)
564944410e9SLars Ellenberg 		seq_print_one_timing_detail(m, tdp+i, now);
565944410e9SLars Ellenberg 	for (i = 0; i < start_idx; i++)
566944410e9SLars Ellenberg 		seq_print_one_timing_detail(m, tdp+i, now);
567944410e9SLars Ellenberg }
568944410e9SLars Ellenberg 
569944410e9SLars Ellenberg static int callback_history_show(struct seq_file *m, void *ignored)
570944410e9SLars Ellenberg {
571944410e9SLars Ellenberg 	struct drbd_connection *connection = m->private;
572944410e9SLars Ellenberg 	unsigned long jif = jiffies;
573944410e9SLars Ellenberg 
574*b44e1184SLars Ellenberg 	/* BUMP me if you change the file format/content/presentation */
575*b44e1184SLars Ellenberg 	seq_printf(m, "v: %u\n\n", 0);
576*b44e1184SLars Ellenberg 
577944410e9SLars Ellenberg 	seq_puts(m, "n\tage\tcallsite\tfn\n");
578944410e9SLars Ellenberg 	seq_print_timing_details(m, "worker", connection->w_cb_nr, connection->w_timing_details, jif);
579944410e9SLars Ellenberg 	seq_print_timing_details(m, "receiver", connection->r_cb_nr, connection->r_timing_details, jif);
580944410e9SLars Ellenberg 	return 0;
581944410e9SLars Ellenberg }
582944410e9SLars Ellenberg 
583944410e9SLars Ellenberg static int callback_history_open(struct inode *inode, struct file *file)
584944410e9SLars Ellenberg {
585944410e9SLars Ellenberg 	struct drbd_connection *connection = inode->i_private;
586944410e9SLars Ellenberg 	return drbd_single_open(file, callback_history_show, connection,
587944410e9SLars Ellenberg 				&connection->kref, drbd_destroy_connection);
588944410e9SLars Ellenberg }
589944410e9SLars Ellenberg 
590944410e9SLars Ellenberg static int callback_history_release(struct inode *inode, struct file *file)
591944410e9SLars Ellenberg {
592944410e9SLars Ellenberg 	struct drbd_connection *connection = inode->i_private;
593944410e9SLars Ellenberg 	kref_put(&connection->kref, drbd_destroy_connection);
594944410e9SLars Ellenberg 	return single_release(inode, file);
595944410e9SLars Ellenberg }
596944410e9SLars Ellenberg 
597944410e9SLars Ellenberg static const struct file_operations connection_callback_history_fops = {
598944410e9SLars Ellenberg 	.owner		= THIS_MODULE,
599944410e9SLars Ellenberg 	.open		= callback_history_open,
600944410e9SLars Ellenberg 	.read		= seq_read,
601944410e9SLars Ellenberg 	.llseek		= seq_lseek,
602944410e9SLars Ellenberg 	.release	= callback_history_release,
603944410e9SLars Ellenberg };
604944410e9SLars Ellenberg 
60554e6fc38SLars Ellenberg static int connection_oldest_requests_show(struct seq_file *m, void *ignored)
60654e6fc38SLars Ellenberg {
60754e6fc38SLars Ellenberg 	struct drbd_connection *connection = m->private;
60854e6fc38SLars Ellenberg 	unsigned long now = jiffies;
60954e6fc38SLars Ellenberg 	struct drbd_request *r1, *r2;
61054e6fc38SLars Ellenberg 
61154e6fc38SLars Ellenberg 	/* BUMP me if you change the file format/content/presentation */
61254e6fc38SLars Ellenberg 	seq_printf(m, "v: %u\n\n", 0);
61354e6fc38SLars Ellenberg 
61454e6fc38SLars Ellenberg 	spin_lock_irq(&connection->resource->req_lock);
61554e6fc38SLars Ellenberg 	r1 = connection->req_next;
61654e6fc38SLars Ellenberg 	if (r1)
61754e6fc38SLars Ellenberg 		seq_print_minor_vnr_req(m, r1, now);
61854e6fc38SLars Ellenberg 	r2 = connection->req_ack_pending;
61954e6fc38SLars Ellenberg 	if (r2 && r2 != r1) {
62054e6fc38SLars Ellenberg 		r1 = r2;
62154e6fc38SLars Ellenberg 		seq_print_minor_vnr_req(m, r1, now);
62254e6fc38SLars Ellenberg 	}
62354e6fc38SLars Ellenberg 	r2 = connection->req_not_net_done;
62454e6fc38SLars Ellenberg 	if (r2 && r2 != r1)
62554e6fc38SLars Ellenberg 		seq_print_minor_vnr_req(m, r2, now);
62654e6fc38SLars Ellenberg 	spin_unlock_irq(&connection->resource->req_lock);
62754e6fc38SLars Ellenberg 	return 0;
62854e6fc38SLars Ellenberg }
62954e6fc38SLars Ellenberg 
63054e6fc38SLars Ellenberg static int connection_oldest_requests_open(struct inode *inode, struct file *file)
63154e6fc38SLars Ellenberg {
63254e6fc38SLars Ellenberg 	struct drbd_connection *connection = inode->i_private;
63354e6fc38SLars Ellenberg 	return drbd_single_open(file, connection_oldest_requests_show, connection,
63454e6fc38SLars Ellenberg 				&connection->kref, drbd_destroy_connection);
63554e6fc38SLars Ellenberg }
63654e6fc38SLars Ellenberg 
63754e6fc38SLars Ellenberg static int connection_oldest_requests_release(struct inode *inode, struct file *file)
63854e6fc38SLars Ellenberg {
63954e6fc38SLars Ellenberg 	struct drbd_connection *connection = inode->i_private;
64054e6fc38SLars Ellenberg 	kref_put(&connection->kref, drbd_destroy_connection);
64154e6fc38SLars Ellenberg 	return single_release(inode, file);
64254e6fc38SLars Ellenberg }
64354e6fc38SLars Ellenberg 
64454e6fc38SLars Ellenberg static const struct file_operations connection_oldest_requests_fops = {
64554e6fc38SLars Ellenberg 	.owner		= THIS_MODULE,
64654e6fc38SLars Ellenberg 	.open		= connection_oldest_requests_open,
64754e6fc38SLars Ellenberg 	.read		= seq_read,
64854e6fc38SLars Ellenberg 	.llseek		= seq_lseek,
64954e6fc38SLars Ellenberg 	.release	= connection_oldest_requests_release,
65054e6fc38SLars Ellenberg };
65154e6fc38SLars Ellenberg 
6524d3d5aa8SLars Ellenberg void drbd_debugfs_connection_add(struct drbd_connection *connection)
6534d3d5aa8SLars Ellenberg {
6544d3d5aa8SLars Ellenberg 	struct dentry *conns_dir = connection->resource->debugfs_res_connections;
6554d3d5aa8SLars Ellenberg 	struct dentry *dentry;
6564d3d5aa8SLars Ellenberg 	if (!conns_dir)
6574d3d5aa8SLars Ellenberg 		return;
6584d3d5aa8SLars Ellenberg 
6594d3d5aa8SLars Ellenberg 	/* Once we enable mutliple peers,
6604d3d5aa8SLars Ellenberg 	 * these connections will have descriptive names.
6614d3d5aa8SLars Ellenberg 	 * For now, it is just the one connection to the (only) "peer". */
6624d3d5aa8SLars Ellenberg 	dentry = debugfs_create_dir("peer", conns_dir);
6634d3d5aa8SLars Ellenberg 	if (IS_ERR_OR_NULL(dentry))
6644d3d5aa8SLars Ellenberg 		goto fail;
6654d3d5aa8SLars Ellenberg 	connection->debugfs_conn = dentry;
666944410e9SLars Ellenberg 
667944410e9SLars Ellenberg 	dentry = debugfs_create_file("callback_history", S_IRUSR|S_IRGRP,
668944410e9SLars Ellenberg 			connection->debugfs_conn, connection,
669944410e9SLars Ellenberg 			&connection_callback_history_fops);
670944410e9SLars Ellenberg 	if (IS_ERR_OR_NULL(dentry))
671944410e9SLars Ellenberg 		goto fail;
672944410e9SLars Ellenberg 	connection->debugfs_conn_callback_history = dentry;
6734d3d5aa8SLars Ellenberg 	return;
6744d3d5aa8SLars Ellenberg 
6754d3d5aa8SLars Ellenberg fail:
6764d3d5aa8SLars Ellenberg 	drbd_debugfs_connection_cleanup(connection);
6774d3d5aa8SLars Ellenberg 	drbd_err(connection, "failed to create debugfs dentry\n");
6784d3d5aa8SLars Ellenberg }
6794d3d5aa8SLars Ellenberg 
6804d3d5aa8SLars Ellenberg void drbd_debugfs_connection_cleanup(struct drbd_connection *connection)
6814d3d5aa8SLars Ellenberg {
6824d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&connection->debugfs_conn_callback_history);
6834d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&connection->debugfs_conn_oldest_requests);
6844d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&connection->debugfs_conn);
6854d3d5aa8SLars Ellenberg }
6864d3d5aa8SLars Ellenberg 
68754e6fc38SLars Ellenberg static void resync_dump_detail(struct seq_file *m, struct lc_element *e)
68854e6fc38SLars Ellenberg {
68954e6fc38SLars Ellenberg        struct bm_extent *bme = lc_entry(e, struct bm_extent, lce);
69054e6fc38SLars Ellenberg 
69154e6fc38SLars Ellenberg        seq_printf(m, "%5d %s %s %s\n", bme->rs_left,
69254e6fc38SLars Ellenberg 		  test_bit(BME_NO_WRITES, &bme->flags) ? "NO_WRITES" : "---------",
69354e6fc38SLars Ellenberg 		  test_bit(BME_LOCKED, &bme->flags) ? "LOCKED" : "------",
69454e6fc38SLars Ellenberg 		  test_bit(BME_PRIORITY, &bme->flags) ? "PRIORITY" : "--------"
69554e6fc38SLars Ellenberg 		  );
69654e6fc38SLars Ellenberg }
69754e6fc38SLars Ellenberg 
69854e6fc38SLars Ellenberg static int device_resync_extents_show(struct seq_file *m, void *ignored)
69954e6fc38SLars Ellenberg {
70054e6fc38SLars Ellenberg 	struct drbd_device *device = m->private;
701*b44e1184SLars Ellenberg 
702*b44e1184SLars Ellenberg 	/* BUMP me if you change the file format/content/presentation */
703*b44e1184SLars Ellenberg 	seq_printf(m, "v: %u\n\n", 0);
704*b44e1184SLars Ellenberg 
70554e6fc38SLars Ellenberg 	if (get_ldev_if_state(device, D_FAILED)) {
70654e6fc38SLars Ellenberg 		lc_seq_printf_stats(m, device->resync);
70754e6fc38SLars Ellenberg 		lc_seq_dump_details(m, device->resync, "rs_left flags", resync_dump_detail);
70854e6fc38SLars Ellenberg 		put_ldev(device);
70954e6fc38SLars Ellenberg 	}
71054e6fc38SLars Ellenberg 	return 0;
71154e6fc38SLars Ellenberg }
71254e6fc38SLars Ellenberg 
71354e6fc38SLars Ellenberg static int device_act_log_extents_show(struct seq_file *m, void *ignored)
71454e6fc38SLars Ellenberg {
71554e6fc38SLars Ellenberg 	struct drbd_device *device = m->private;
716*b44e1184SLars Ellenberg 
717*b44e1184SLars Ellenberg 	/* BUMP me if you change the file format/content/presentation */
718*b44e1184SLars Ellenberg 	seq_printf(m, "v: %u\n\n", 0);
719*b44e1184SLars Ellenberg 
72054e6fc38SLars Ellenberg 	if (get_ldev_if_state(device, D_FAILED)) {
72154e6fc38SLars Ellenberg 		lc_seq_printf_stats(m, device->act_log);
72254e6fc38SLars Ellenberg 		lc_seq_dump_details(m, device->act_log, "", NULL);
72354e6fc38SLars Ellenberg 		put_ldev(device);
72454e6fc38SLars Ellenberg 	}
72554e6fc38SLars Ellenberg 	return 0;
72654e6fc38SLars Ellenberg }
72754e6fc38SLars Ellenberg 
72854e6fc38SLars Ellenberg static int device_oldest_requests_show(struct seq_file *m, void *ignored)
72954e6fc38SLars Ellenberg {
73054e6fc38SLars Ellenberg 	struct drbd_device *device = m->private;
73154e6fc38SLars Ellenberg 	struct drbd_resource *resource = device->resource;
73254e6fc38SLars Ellenberg 	unsigned long now = jiffies;
73354e6fc38SLars Ellenberg 	struct drbd_request *r1, *r2;
73454e6fc38SLars Ellenberg 	int i;
73554e6fc38SLars Ellenberg 
736*b44e1184SLars Ellenberg 	/* BUMP me if you change the file format/content/presentation */
737*b44e1184SLars Ellenberg 	seq_printf(m, "v: %u\n\n", 0);
738*b44e1184SLars Ellenberg 
73954e6fc38SLars Ellenberg 	seq_puts(m, RQ_HDR);
74054e6fc38SLars Ellenberg 	spin_lock_irq(&resource->req_lock);
74154e6fc38SLars Ellenberg 	/* WRITE, then READ */
74254e6fc38SLars Ellenberg 	for (i = 1; i >= 0; --i) {
74354e6fc38SLars Ellenberg 		r1 = list_first_entry_or_null(&device->pending_master_completion[i],
74454e6fc38SLars Ellenberg 			struct drbd_request, req_pending_master_completion);
74554e6fc38SLars Ellenberg 		r2 = list_first_entry_or_null(&device->pending_completion[i],
74654e6fc38SLars Ellenberg 			struct drbd_request, req_pending_local);
74754e6fc38SLars Ellenberg 		if (r1)
74854e6fc38SLars Ellenberg 			seq_print_one_request(m, r1, now);
74954e6fc38SLars Ellenberg 		if (r2 && r2 != r1)
75054e6fc38SLars Ellenberg 			seq_print_one_request(m, r2, now);
75154e6fc38SLars Ellenberg 	}
75254e6fc38SLars Ellenberg 	spin_unlock_irq(&resource->req_lock);
75354e6fc38SLars Ellenberg 	return 0;
75454e6fc38SLars Ellenberg }
75554e6fc38SLars Ellenberg 
75654e6fc38SLars Ellenberg #define drbd_debugfs_device_attr(name)						\
75754e6fc38SLars Ellenberg static int device_ ## name ## _open(struct inode *inode, struct file *file)	\
75854e6fc38SLars Ellenberg {										\
75954e6fc38SLars Ellenberg 	struct drbd_device *device = inode->i_private;				\
76054e6fc38SLars Ellenberg 	return drbd_single_open(file, device_ ## name ## _show, device,		\
76154e6fc38SLars Ellenberg 				&device->kref, drbd_destroy_device);		\
76254e6fc38SLars Ellenberg }										\
76354e6fc38SLars Ellenberg static int device_ ## name ## _release(struct inode *inode, struct file *file)	\
76454e6fc38SLars Ellenberg {										\
76554e6fc38SLars Ellenberg 	struct drbd_device *device = inode->i_private;				\
76654e6fc38SLars Ellenberg 	kref_put(&device->kref, drbd_destroy_device);				\
76754e6fc38SLars Ellenberg 	return single_release(inode, file);					\
76854e6fc38SLars Ellenberg }										\
76954e6fc38SLars Ellenberg static const struct file_operations device_ ## name ## _fops = {		\
77054e6fc38SLars Ellenberg 	.owner		= THIS_MODULE,						\
77154e6fc38SLars Ellenberg 	.open		= device_ ## name ## _open,				\
77254e6fc38SLars Ellenberg 	.read		= seq_read,						\
77354e6fc38SLars Ellenberg 	.llseek		= seq_lseek,						\
77454e6fc38SLars Ellenberg 	.release	= device_ ## name ## _release,				\
77554e6fc38SLars Ellenberg };
77654e6fc38SLars Ellenberg 
77754e6fc38SLars Ellenberg drbd_debugfs_device_attr(oldest_requests)
77854e6fc38SLars Ellenberg drbd_debugfs_device_attr(act_log_extents)
77954e6fc38SLars Ellenberg drbd_debugfs_device_attr(resync_extents)
78054e6fc38SLars Ellenberg 
7814d3d5aa8SLars Ellenberg void drbd_debugfs_device_add(struct drbd_device *device)
7824d3d5aa8SLars Ellenberg {
7834d3d5aa8SLars Ellenberg 	struct dentry *vols_dir = device->resource->debugfs_res_volumes;
7844d3d5aa8SLars Ellenberg 	char minor_buf[8]; /* MINORMASK, MINORBITS == 20; */
7854d3d5aa8SLars Ellenberg 	char vnr_buf[8];   /* volume number vnr is even 16 bit only; */
7864d3d5aa8SLars Ellenberg 	char *slink_name = NULL;
7874d3d5aa8SLars Ellenberg 
7884d3d5aa8SLars Ellenberg 	struct dentry *dentry;
7894d3d5aa8SLars Ellenberg 	if (!vols_dir || !drbd_debugfs_minors)
7904d3d5aa8SLars Ellenberg 		return;
7914d3d5aa8SLars Ellenberg 
7924d3d5aa8SLars Ellenberg 	snprintf(vnr_buf, sizeof(vnr_buf), "%u", device->vnr);
7934d3d5aa8SLars Ellenberg 	dentry = debugfs_create_dir(vnr_buf, vols_dir);
7944d3d5aa8SLars Ellenberg 	if (IS_ERR_OR_NULL(dentry))
7954d3d5aa8SLars Ellenberg 		goto fail;
7964d3d5aa8SLars Ellenberg 	device->debugfs_vol = dentry;
7974d3d5aa8SLars Ellenberg 
7984d3d5aa8SLars Ellenberg 	snprintf(minor_buf, sizeof(minor_buf), "%u", device->minor);
7994d3d5aa8SLars Ellenberg 	slink_name = kasprintf(GFP_KERNEL, "../resources/%s/volumes/%u",
8004d3d5aa8SLars Ellenberg 			device->resource->name, device->vnr);
8014d3d5aa8SLars Ellenberg 	if (!slink_name)
8024d3d5aa8SLars Ellenberg 		goto fail;
8034d3d5aa8SLars Ellenberg 	dentry = debugfs_create_symlink(minor_buf, drbd_debugfs_minors, slink_name);
80454e6fc38SLars Ellenberg 	kfree(slink_name);
80554e6fc38SLars Ellenberg 	slink_name = NULL;
8064d3d5aa8SLars Ellenberg 	if (IS_ERR_OR_NULL(dentry))
8074d3d5aa8SLars Ellenberg 		goto fail;
8084d3d5aa8SLars Ellenberg 	device->debugfs_minor = dentry;
80954e6fc38SLars Ellenberg 
81054e6fc38SLars Ellenberg #define DCF(name)	do {					\
81154e6fc38SLars Ellenberg 	dentry = debugfs_create_file(#name, S_IRUSR|S_IRGRP,	\
81254e6fc38SLars Ellenberg 			device->debugfs_vol, device,		\
81354e6fc38SLars Ellenberg 			&device_ ## name ## _fops);		\
81454e6fc38SLars Ellenberg 	if (IS_ERR_OR_NULL(dentry))				\
81554e6fc38SLars Ellenberg 		goto fail;					\
81654e6fc38SLars Ellenberg 	device->debugfs_vol_ ## name = dentry;			\
81754e6fc38SLars Ellenberg 	} while (0)
81854e6fc38SLars Ellenberg 
81954e6fc38SLars Ellenberg 	DCF(oldest_requests);
82054e6fc38SLars Ellenberg 	DCF(act_log_extents);
82154e6fc38SLars Ellenberg 	DCF(resync_extents);
82254e6fc38SLars Ellenberg 	return;
8234d3d5aa8SLars Ellenberg 
8244d3d5aa8SLars Ellenberg fail:
8254d3d5aa8SLars Ellenberg 	drbd_debugfs_device_cleanup(device);
8264d3d5aa8SLars Ellenberg 	drbd_err(device, "failed to create debugfs entries\n");
8274d3d5aa8SLars Ellenberg }
8284d3d5aa8SLars Ellenberg 
8294d3d5aa8SLars Ellenberg void drbd_debugfs_device_cleanup(struct drbd_device *device)
8304d3d5aa8SLars Ellenberg {
8314d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&device->debugfs_minor);
8324d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&device->debugfs_vol_oldest_requests);
8334d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&device->debugfs_vol_act_log_extents);
8344d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&device->debugfs_vol_resync_extents);
8354d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&device->debugfs_vol_data_gen_id);
8364d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&device->debugfs_vol);
8374d3d5aa8SLars Ellenberg }
8384d3d5aa8SLars Ellenberg 
8394d3d5aa8SLars Ellenberg void drbd_debugfs_peer_device_add(struct drbd_peer_device *peer_device)
8404d3d5aa8SLars Ellenberg {
8414d3d5aa8SLars Ellenberg 	struct dentry *conn_dir = peer_device->connection->debugfs_conn;
8424d3d5aa8SLars Ellenberg 	struct dentry *dentry;
8434d3d5aa8SLars Ellenberg 	char vnr_buf[8];
8444d3d5aa8SLars Ellenberg 
8454d3d5aa8SLars Ellenberg 	if (!conn_dir)
8464d3d5aa8SLars Ellenberg 		return;
8474d3d5aa8SLars Ellenberg 
8484d3d5aa8SLars Ellenberg 	snprintf(vnr_buf, sizeof(vnr_buf), "%u", peer_device->device->vnr);
8494d3d5aa8SLars Ellenberg 	dentry = debugfs_create_dir(vnr_buf, conn_dir);
8504d3d5aa8SLars Ellenberg 	if (IS_ERR_OR_NULL(dentry))
8514d3d5aa8SLars Ellenberg 		goto fail;
8524d3d5aa8SLars Ellenberg 	peer_device->debugfs_peer_dev = dentry;
8534d3d5aa8SLars Ellenberg 	return;
8544d3d5aa8SLars Ellenberg 
8554d3d5aa8SLars Ellenberg fail:
8564d3d5aa8SLars Ellenberg 	drbd_debugfs_peer_device_cleanup(peer_device);
8574d3d5aa8SLars Ellenberg 	drbd_err(peer_device, "failed to create debugfs entries\n");
8584d3d5aa8SLars Ellenberg }
8594d3d5aa8SLars Ellenberg 
8604d3d5aa8SLars Ellenberg void drbd_debugfs_peer_device_cleanup(struct drbd_peer_device *peer_device)
8614d3d5aa8SLars Ellenberg {
8624d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&peer_device->debugfs_peer_dev);
8634d3d5aa8SLars Ellenberg }
8644d3d5aa8SLars Ellenberg 
865*b44e1184SLars Ellenberg static int drbd_version_show(struct seq_file *m, void *ignored)
866*b44e1184SLars Ellenberg {
867*b44e1184SLars Ellenberg 	seq_printf(m, "# %s\n", drbd_buildtag());
868*b44e1184SLars Ellenberg 	seq_printf(m, "VERSION=%s\n", REL_VERSION);
869*b44e1184SLars Ellenberg 	seq_printf(m, "API_VERSION=%u\n", API_VERSION);
870*b44e1184SLars Ellenberg 	seq_printf(m, "PRO_VERSION_MIN=%u\n", PRO_VERSION_MIN);
871*b44e1184SLars Ellenberg 	seq_printf(m, "PRO_VERSION_MAX=%u\n", PRO_VERSION_MAX);
872*b44e1184SLars Ellenberg 	return 0;
873*b44e1184SLars Ellenberg }
874*b44e1184SLars Ellenberg 
875*b44e1184SLars Ellenberg static int drbd_version_open(struct inode *inode, struct file *file)
876*b44e1184SLars Ellenberg {
877*b44e1184SLars Ellenberg 	return single_open(file, drbd_version_show, NULL);
878*b44e1184SLars Ellenberg }
879*b44e1184SLars Ellenberg 
880*b44e1184SLars Ellenberg static struct file_operations drbd_version_fops = {
881*b44e1184SLars Ellenberg 	.owner = THIS_MODULE,
882*b44e1184SLars Ellenberg 	.open = drbd_version_open,
883*b44e1184SLars Ellenberg 	.llseek = seq_lseek,
884*b44e1184SLars Ellenberg 	.read = seq_read,
885*b44e1184SLars Ellenberg 	.release = single_release,
886*b44e1184SLars Ellenberg };
887*b44e1184SLars Ellenberg 
8884d3d5aa8SLars Ellenberg /* not __exit, may be indirectly called
8894d3d5aa8SLars Ellenberg  * from the module-load-failure path as well. */
8904d3d5aa8SLars Ellenberg void drbd_debugfs_cleanup(void)
8914d3d5aa8SLars Ellenberg {
8924d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&drbd_debugfs_resources);
8934d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&drbd_debugfs_minors);
894*b44e1184SLars Ellenberg 	drbd_debugfs_remove(&drbd_debugfs_version);
8954d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&drbd_debugfs_root);
8964d3d5aa8SLars Ellenberg }
8974d3d5aa8SLars Ellenberg 
8984d3d5aa8SLars Ellenberg int __init drbd_debugfs_init(void)
8994d3d5aa8SLars Ellenberg {
9004d3d5aa8SLars Ellenberg 	struct dentry *dentry;
9014d3d5aa8SLars Ellenberg 
9024d3d5aa8SLars Ellenberg 	dentry = debugfs_create_dir("drbd", NULL);
9034d3d5aa8SLars Ellenberg 	if (IS_ERR_OR_NULL(dentry))
9044d3d5aa8SLars Ellenberg 		goto fail;
9054d3d5aa8SLars Ellenberg 	drbd_debugfs_root = dentry;
9064d3d5aa8SLars Ellenberg 
907*b44e1184SLars Ellenberg 	dentry = debugfs_create_file("version", 0444, drbd_debugfs_root, NULL, &drbd_version_fops);
908*b44e1184SLars Ellenberg 	if (IS_ERR_OR_NULL(dentry))
909*b44e1184SLars Ellenberg 		goto fail;
910*b44e1184SLars Ellenberg 	drbd_debugfs_version = dentry;
911*b44e1184SLars Ellenberg 
9124d3d5aa8SLars Ellenberg 	dentry = debugfs_create_dir("resources", drbd_debugfs_root);
9134d3d5aa8SLars Ellenberg 	if (IS_ERR_OR_NULL(dentry))
9144d3d5aa8SLars Ellenberg 		goto fail;
9154d3d5aa8SLars Ellenberg 	drbd_debugfs_resources = dentry;
9164d3d5aa8SLars Ellenberg 
9174d3d5aa8SLars Ellenberg 	dentry = debugfs_create_dir("minors", drbd_debugfs_root);
9184d3d5aa8SLars Ellenberg 	if (IS_ERR_OR_NULL(dentry))
9194d3d5aa8SLars Ellenberg 		goto fail;
9204d3d5aa8SLars Ellenberg 	drbd_debugfs_minors = dentry;
9214d3d5aa8SLars Ellenberg 	return 0;
9224d3d5aa8SLars Ellenberg 
9234d3d5aa8SLars Ellenberg fail:
9244d3d5aa8SLars Ellenberg 	drbd_debugfs_cleanup();
9254d3d5aa8SLars Ellenberg 	if (dentry)
9264d3d5aa8SLars Ellenberg 		return PTR_ERR(dentry);
9274d3d5aa8SLars Ellenberg 	else
9284d3d5aa8SLars Ellenberg 		return -EINVAL;
9294d3d5aa8SLars Ellenberg }
930