xref: /openbmc/linux/drivers/block/drbd/drbd_debugfs.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
193c68cc4SChristoph Böhmwalder // SPDX-License-Identifier: GPL-2.0-only
24d3d5aa8SLars Ellenberg #define pr_fmt(fmt) "drbd debugfs: " fmt
34d3d5aa8SLars Ellenberg #include <linux/kernel.h>
44d3d5aa8SLars Ellenberg #include <linux/module.h>
54d3d5aa8SLars Ellenberg #include <linux/debugfs.h>
6db1866ffSLars Ellenberg #include <linux/seq_file.h>
74d3d5aa8SLars Ellenberg #include <linux/stat.h>
8db1866ffSLars Ellenberg #include <linux/jiffies.h>
94d3d5aa8SLars Ellenberg #include <linux/list.h>
104d3d5aa8SLars Ellenberg 
114d3d5aa8SLars Ellenberg #include "drbd_int.h"
124d3d5aa8SLars Ellenberg #include "drbd_req.h"
134d3d5aa8SLars Ellenberg #include "drbd_debugfs.h"
144d3d5aa8SLars Ellenberg 
15b44e1184SLars Ellenberg 
16b44e1184SLars Ellenberg /**********************************************************************
17b44e1184SLars Ellenberg  * Whenever you change the file format, remember to bump the version. *
18b44e1184SLars Ellenberg  **********************************************************************/
19b44e1184SLars Ellenberg 
204d3d5aa8SLars Ellenberg static struct dentry *drbd_debugfs_root;
21b44e1184SLars Ellenberg static struct dentry *drbd_debugfs_version;
224d3d5aa8SLars Ellenberg static struct dentry *drbd_debugfs_resources;
234d3d5aa8SLars Ellenberg static struct dentry *drbd_debugfs_minors;
244d3d5aa8SLars Ellenberg 
seq_print_age_or_dash(struct seq_file * m,bool valid,unsigned long dt)25db1866ffSLars Ellenberg static void seq_print_age_or_dash(struct seq_file *m, bool valid, unsigned long dt)
26db1866ffSLars Ellenberg {
27db1866ffSLars Ellenberg 	if (valid)
28db1866ffSLars Ellenberg 		seq_printf(m, "\t%d", jiffies_to_msecs(dt));
29db1866ffSLars Ellenberg 	else
30db1866ffSLars Ellenberg 		seq_printf(m, "\t-");
31db1866ffSLars Ellenberg }
32db1866ffSLars Ellenberg 
__seq_print_rq_state_bit(struct seq_file * m,bool is_set,char * sep,const char * set_name,const char * unset_name)33f418815fSLars Ellenberg static void __seq_print_rq_state_bit(struct seq_file *m,
34f418815fSLars Ellenberg 	bool is_set, char *sep, const char *set_name, const char *unset_name)
35db1866ffSLars Ellenberg {
36f418815fSLars Ellenberg 	if (is_set && set_name) {
37db1866ffSLars Ellenberg 		seq_putc(m, *sep);
38f418815fSLars Ellenberg 		seq_puts(m, set_name);
39db1866ffSLars Ellenberg 		*sep = '|';
40f418815fSLars Ellenberg 	} else if (!is_set && unset_name) {
41f418815fSLars Ellenberg 		seq_putc(m, *sep);
42f418815fSLars Ellenberg 		seq_puts(m, unset_name);
43f418815fSLars Ellenberg 		*sep = '|';
44f418815fSLars Ellenberg 	}
45f418815fSLars Ellenberg }
46f418815fSLars Ellenberg 
seq_print_rq_state_bit(struct seq_file * m,bool is_set,char * sep,const char * set_name)47f418815fSLars Ellenberg static void seq_print_rq_state_bit(struct seq_file *m,
48f418815fSLars Ellenberg 	bool is_set, char *sep, const char *set_name)
49f418815fSLars Ellenberg {
50f418815fSLars Ellenberg 	__seq_print_rq_state_bit(m, is_set, sep, set_name, NULL);
51db1866ffSLars Ellenberg }
52db1866ffSLars Ellenberg 
53db1866ffSLars Ellenberg /* pretty print enum drbd_req_state_bits req->rq_state */
seq_print_request_state(struct seq_file * m,struct drbd_request * req)54db1866ffSLars Ellenberg static void seq_print_request_state(struct seq_file *m, struct drbd_request *req)
55db1866ffSLars Ellenberg {
56db1866ffSLars Ellenberg 	unsigned int s = req->rq_state;
57db1866ffSLars Ellenberg 	char sep = ' ';
58db1866ffSLars Ellenberg 	seq_printf(m, "\t0x%08x", s);
59db1866ffSLars Ellenberg 	seq_printf(m, "\tmaster: %s", req->master_bio ? "pending" : "completed");
60db1866ffSLars Ellenberg 
61db1866ffSLars Ellenberg 	/* RQ_WRITE ignored, already reported */
62db1866ffSLars Ellenberg 	seq_puts(m, "\tlocal:");
63db1866ffSLars Ellenberg 	seq_print_rq_state_bit(m, s & RQ_IN_ACT_LOG, &sep, "in-AL");
64db1866ffSLars Ellenberg 	seq_print_rq_state_bit(m, s & RQ_POSTPONED, &sep, "postponed");
65db1866ffSLars Ellenberg 	seq_print_rq_state_bit(m, s & RQ_COMPLETION_SUSP, &sep, "suspended");
66db1866ffSLars Ellenberg 	sep = ' ';
67db1866ffSLars Ellenberg 	seq_print_rq_state_bit(m, s & RQ_LOCAL_PENDING, &sep, "pending");
68db1866ffSLars Ellenberg 	seq_print_rq_state_bit(m, s & RQ_LOCAL_COMPLETED, &sep, "completed");
69db1866ffSLars Ellenberg 	seq_print_rq_state_bit(m, s & RQ_LOCAL_ABORTED, &sep, "aborted");
70db1866ffSLars Ellenberg 	seq_print_rq_state_bit(m, s & RQ_LOCAL_OK, &sep, "ok");
71db1866ffSLars Ellenberg 	if (sep == ' ')
72db1866ffSLars Ellenberg 		seq_puts(m, " -");
73db1866ffSLars Ellenberg 
74db1866ffSLars Ellenberg 	/* for_each_connection ... */
75db1866ffSLars Ellenberg 	seq_printf(m, "\tnet:");
76db1866ffSLars Ellenberg 	sep = ' ';
77db1866ffSLars Ellenberg 	seq_print_rq_state_bit(m, s & RQ_NET_PENDING, &sep, "pending");
78db1866ffSLars Ellenberg 	seq_print_rq_state_bit(m, s & RQ_NET_QUEUED, &sep, "queued");
79db1866ffSLars Ellenberg 	seq_print_rq_state_bit(m, s & RQ_NET_SENT, &sep, "sent");
80db1866ffSLars Ellenberg 	seq_print_rq_state_bit(m, s & RQ_NET_DONE, &sep, "done");
81db1866ffSLars Ellenberg 	seq_print_rq_state_bit(m, s & RQ_NET_SIS, &sep, "sis");
82db1866ffSLars Ellenberg 	seq_print_rq_state_bit(m, s & RQ_NET_OK, &sep, "ok");
83db1866ffSLars Ellenberg 	if (sep == ' ')
84db1866ffSLars Ellenberg 		seq_puts(m, " -");
85db1866ffSLars Ellenberg 
86db1866ffSLars Ellenberg 	seq_printf(m, " :");
87db1866ffSLars Ellenberg 	sep = ' ';
88db1866ffSLars Ellenberg 	seq_print_rq_state_bit(m, s & RQ_EXP_RECEIVE_ACK, &sep, "B");
89db1866ffSLars Ellenberg 	seq_print_rq_state_bit(m, s & RQ_EXP_WRITE_ACK, &sep, "C");
90db1866ffSLars Ellenberg 	seq_print_rq_state_bit(m, s & RQ_EXP_BARR_ACK, &sep, "barr");
91db1866ffSLars Ellenberg 	if (sep == ' ')
92db1866ffSLars Ellenberg 		seq_puts(m, " -");
93db1866ffSLars Ellenberg 	seq_printf(m, "\n");
94db1866ffSLars Ellenberg }
95db1866ffSLars Ellenberg 
seq_print_one_request(struct seq_file * m,struct drbd_request * req,unsigned long now)96db1866ffSLars Ellenberg static void seq_print_one_request(struct seq_file *m, struct drbd_request *req, unsigned long now)
97db1866ffSLars Ellenberg {
98db1866ffSLars Ellenberg 	/* change anything here, fixup header below! */
99db1866ffSLars Ellenberg 	unsigned int s = req->rq_state;
100db1866ffSLars Ellenberg 
101db1866ffSLars Ellenberg #define RQ_HDR_1 "epoch\tsector\tsize\trw"
102db1866ffSLars Ellenberg 	seq_printf(m, "0x%x\t%llu\t%u\t%s",
103db1866ffSLars Ellenberg 		req->epoch,
104db1866ffSLars Ellenberg 		(unsigned long long)req->i.sector, req->i.size >> 9,
105db1866ffSLars Ellenberg 		(s & RQ_WRITE) ? "W" : "R");
106db1866ffSLars Ellenberg 
107db1866ffSLars Ellenberg #define RQ_HDR_2 "\tstart\tin AL\tsubmit"
108db1866ffSLars Ellenberg 	seq_printf(m, "\t%d", jiffies_to_msecs(now - req->start_jif));
109db1866ffSLars Ellenberg 	seq_print_age_or_dash(m, s & RQ_IN_ACT_LOG, now - req->in_actlog_jif);
110db1866ffSLars Ellenberg 	seq_print_age_or_dash(m, s & RQ_LOCAL_PENDING, now - req->pre_submit_jif);
111db1866ffSLars Ellenberg 
112db1866ffSLars Ellenberg #define RQ_HDR_3 "\tsent\tacked\tdone"
113db1866ffSLars Ellenberg 	seq_print_age_or_dash(m, s & RQ_NET_SENT, now - req->pre_send_jif);
114db1866ffSLars Ellenberg 	seq_print_age_or_dash(m, (s & RQ_NET_SENT) && !(s & RQ_NET_PENDING), now - req->acked_jif);
115db1866ffSLars Ellenberg 	seq_print_age_or_dash(m, s & RQ_NET_DONE, now - req->net_done_jif);
116db1866ffSLars Ellenberg 
117db1866ffSLars Ellenberg #define RQ_HDR_4 "\tstate\n"
118db1866ffSLars Ellenberg 	seq_print_request_state(m, req);
119db1866ffSLars Ellenberg }
120db1866ffSLars Ellenberg #define RQ_HDR RQ_HDR_1 RQ_HDR_2 RQ_HDR_3 RQ_HDR_4
121db1866ffSLars Ellenberg 
seq_print_minor_vnr_req(struct seq_file * m,struct drbd_request * req,unsigned long now)122db1866ffSLars Ellenberg static void seq_print_minor_vnr_req(struct seq_file *m, struct drbd_request *req, unsigned long now)
123db1866ffSLars Ellenberg {
124db1866ffSLars Ellenberg 	seq_printf(m, "%u\t%u\t", req->device->minor, req->device->vnr);
125db1866ffSLars Ellenberg 	seq_print_one_request(m, req, now);
126db1866ffSLars Ellenberg }
127db1866ffSLars Ellenberg 
seq_print_resource_pending_meta_io(struct seq_file * m,struct drbd_resource * resource,unsigned long now)128f418815fSLars Ellenberg static void seq_print_resource_pending_meta_io(struct seq_file *m, struct drbd_resource *resource, unsigned long now)
129f418815fSLars Ellenberg {
130f418815fSLars Ellenberg 	struct drbd_device *device;
131f418815fSLars Ellenberg 	unsigned int i;
132f418815fSLars Ellenberg 
133f418815fSLars Ellenberg 	seq_puts(m, "minor\tvnr\tstart\tsubmit\tintent\n");
134f418815fSLars Ellenberg 	rcu_read_lock();
135f418815fSLars Ellenberg 	idr_for_each_entry(&resource->devices, device, i) {
136f418815fSLars Ellenberg 		struct drbd_md_io tmp;
137f418815fSLars Ellenberg 		/* In theory this is racy,
138f418815fSLars Ellenberg 		 * in the sense that there could have been a
139f418815fSLars Ellenberg 		 * drbd_md_put_buffer(); drbd_md_get_buffer();
140f418815fSLars Ellenberg 		 * between accessing these members here.  */
141f418815fSLars Ellenberg 		tmp = device->md_io;
142f418815fSLars Ellenberg 		if (atomic_read(&tmp.in_use)) {
143f418815fSLars Ellenberg 			seq_printf(m, "%u\t%u\t%d\t",
144f418815fSLars Ellenberg 				device->minor, device->vnr,
145f418815fSLars Ellenberg 				jiffies_to_msecs(now - tmp.start_jif));
146f418815fSLars Ellenberg 			if (time_before(tmp.submit_jif, tmp.start_jif))
147f418815fSLars Ellenberg 				seq_puts(m, "-\t");
148f418815fSLars Ellenberg 			else
149f418815fSLars Ellenberg 				seq_printf(m, "%d\t", jiffies_to_msecs(now - tmp.submit_jif));
150f418815fSLars Ellenberg 			seq_printf(m, "%s\n", tmp.current_use);
151f418815fSLars Ellenberg 		}
152f418815fSLars Ellenberg 	}
153f418815fSLars Ellenberg 	rcu_read_unlock();
154f418815fSLars Ellenberg }
155f418815fSLars Ellenberg 
seq_print_waiting_for_AL(struct seq_file * m,struct drbd_resource * resource,unsigned long now)156f418815fSLars Ellenberg static void seq_print_waiting_for_AL(struct seq_file *m, struct drbd_resource *resource, unsigned long now)
157f418815fSLars Ellenberg {
158f418815fSLars Ellenberg 	struct drbd_device *device;
159f418815fSLars Ellenberg 	unsigned int i;
160f418815fSLars Ellenberg 
161f418815fSLars Ellenberg 	seq_puts(m, "minor\tvnr\tage\t#waiting\n");
162f418815fSLars Ellenberg 	rcu_read_lock();
163f418815fSLars Ellenberg 	idr_for_each_entry(&resource->devices, device, i) {
164f418815fSLars Ellenberg 		unsigned long jif;
165f418815fSLars Ellenberg 		struct drbd_request *req;
166f418815fSLars Ellenberg 		int n = atomic_read(&device->ap_actlog_cnt);
167f418815fSLars Ellenberg 		if (n) {
168f418815fSLars Ellenberg 			spin_lock_irq(&device->resource->req_lock);
169f418815fSLars Ellenberg 			req = list_first_entry_or_null(&device->pending_master_completion[1],
170f418815fSLars Ellenberg 				struct drbd_request, req_pending_master_completion);
171f418815fSLars Ellenberg 			/* if the oldest request does not wait for the activity log
172f418815fSLars Ellenberg 			 * it is not interesting for us here */
173f418815fSLars Ellenberg 			if (req && !(req->rq_state & RQ_IN_ACT_LOG))
174f418815fSLars Ellenberg 				jif = req->start_jif;
175f418815fSLars Ellenberg 			else
176f418815fSLars Ellenberg 				req = NULL;
177f418815fSLars Ellenberg 			spin_unlock_irq(&device->resource->req_lock);
178f418815fSLars Ellenberg 		}
179f418815fSLars Ellenberg 		if (n) {
180f418815fSLars Ellenberg 			seq_printf(m, "%u\t%u\t", device->minor, device->vnr);
181f418815fSLars Ellenberg 			if (req)
182f418815fSLars Ellenberg 				seq_printf(m, "%u\t", jiffies_to_msecs(now - jif));
183f418815fSLars Ellenberg 			else
184f418815fSLars Ellenberg 				seq_puts(m, "-\t");
185f418815fSLars Ellenberg 			seq_printf(m, "%u\n", n);
186f418815fSLars Ellenberg 		}
187f418815fSLars Ellenberg 	}
188f418815fSLars Ellenberg 	rcu_read_unlock();
189f418815fSLars Ellenberg }
190f418815fSLars Ellenberg 
seq_print_device_bitmap_io(struct seq_file * m,struct drbd_device * device,unsigned long now)191f418815fSLars Ellenberg static void seq_print_device_bitmap_io(struct seq_file *m, struct drbd_device *device, unsigned long now)
192f418815fSLars Ellenberg {
193f418815fSLars Ellenberg 	struct drbd_bm_aio_ctx *ctx;
194f418815fSLars Ellenberg 	unsigned long start_jif;
195f418815fSLars Ellenberg 	unsigned int in_flight;
196f418815fSLars Ellenberg 	unsigned int flags;
197f418815fSLars Ellenberg 	spin_lock_irq(&device->resource->req_lock);
198f418815fSLars Ellenberg 	ctx = list_first_entry_or_null(&device->pending_bitmap_io, struct drbd_bm_aio_ctx, list);
199f418815fSLars Ellenberg 	if (ctx && ctx->done)
200f418815fSLars Ellenberg 		ctx = NULL;
201f418815fSLars Ellenberg 	if (ctx) {
202f418815fSLars Ellenberg 		start_jif = ctx->start_jif;
203f418815fSLars Ellenberg 		in_flight = atomic_read(&ctx->in_flight);
204f418815fSLars Ellenberg 		flags = ctx->flags;
205f418815fSLars Ellenberg 	}
206f418815fSLars Ellenberg 	spin_unlock_irq(&device->resource->req_lock);
207f418815fSLars Ellenberg 	if (ctx) {
208f418815fSLars Ellenberg 		seq_printf(m, "%u\t%u\t%c\t%u\t%u\n",
209f418815fSLars Ellenberg 			device->minor, device->vnr,
210f418815fSLars Ellenberg 			(flags & BM_AIO_READ) ? 'R' : 'W',
211f418815fSLars Ellenberg 			jiffies_to_msecs(now - start_jif),
212f418815fSLars Ellenberg 			in_flight);
213f418815fSLars Ellenberg 	}
214f418815fSLars Ellenberg }
215f418815fSLars Ellenberg 
seq_print_resource_pending_bitmap_io(struct seq_file * m,struct drbd_resource * resource,unsigned long now)216f418815fSLars Ellenberg static void seq_print_resource_pending_bitmap_io(struct seq_file *m, struct drbd_resource *resource, unsigned long now)
217f418815fSLars Ellenberg {
218f418815fSLars Ellenberg 	struct drbd_device *device;
219f418815fSLars Ellenberg 	unsigned int i;
220f418815fSLars Ellenberg 
221f418815fSLars Ellenberg 	seq_puts(m, "minor\tvnr\trw\tage\t#in-flight\n");
222f418815fSLars Ellenberg 	rcu_read_lock();
223f418815fSLars Ellenberg 	idr_for_each_entry(&resource->devices, device, i) {
224f418815fSLars Ellenberg 		seq_print_device_bitmap_io(m, device, now);
225f418815fSLars Ellenberg 	}
226f418815fSLars Ellenberg 	rcu_read_unlock();
227f418815fSLars Ellenberg }
228f418815fSLars Ellenberg 
229f418815fSLars Ellenberg /* pretty print enum peer_req->flags */
seq_print_peer_request_flags(struct seq_file * m,struct drbd_peer_request * peer_req)230f418815fSLars Ellenberg static void seq_print_peer_request_flags(struct seq_file *m, struct drbd_peer_request *peer_req)
231f418815fSLars Ellenberg {
232f418815fSLars Ellenberg 	unsigned long f = peer_req->flags;
233f418815fSLars Ellenberg 	char sep = ' ';
234f418815fSLars Ellenberg 
235f418815fSLars Ellenberg 	__seq_print_rq_state_bit(m, f & EE_SUBMITTED, &sep, "submitted", "preparing");
236f418815fSLars Ellenberg 	__seq_print_rq_state_bit(m, f & EE_APPLICATION, &sep, "application", "internal");
237f418815fSLars Ellenberg 	seq_print_rq_state_bit(m, f & EE_CALL_AL_COMPLETE_IO, &sep, "in-AL");
238f418815fSLars Ellenberg 	seq_print_rq_state_bit(m, f & EE_SEND_WRITE_ACK, &sep, "C");
239f418815fSLars Ellenberg 	seq_print_rq_state_bit(m, f & EE_MAY_SET_IN_SYNC, &sep, "set-in-sync");
240f31e583aSLars Ellenberg 	seq_print_rq_state_bit(m, f & EE_TRIM, &sep, "trim");
241f31e583aSLars Ellenberg 	seq_print_rq_state_bit(m, f & EE_ZEROOUT, &sep, "zero-out");
2429104d31aSLars Ellenberg 	seq_print_rq_state_bit(m, f & EE_WRITE_SAME, &sep, "write-same");
243f418815fSLars Ellenberg 	seq_putc(m, '\n');
244f418815fSLars Ellenberg }
245f418815fSLars Ellenberg 
seq_print_peer_request(struct seq_file * m,struct drbd_device * device,struct list_head * lh,unsigned long now)246f418815fSLars Ellenberg static void seq_print_peer_request(struct seq_file *m,
247f418815fSLars Ellenberg 	struct drbd_device *device, struct list_head *lh,
248f418815fSLars Ellenberg 	unsigned long now)
249f418815fSLars Ellenberg {
250f418815fSLars Ellenberg 	bool reported_preparing = false;
251f418815fSLars Ellenberg 	struct drbd_peer_request *peer_req;
252f418815fSLars Ellenberg 	list_for_each_entry(peer_req, lh, w.list) {
253f418815fSLars Ellenberg 		if (reported_preparing && !(peer_req->flags & EE_SUBMITTED))
254f418815fSLars Ellenberg 			continue;
255f418815fSLars Ellenberg 
256f418815fSLars Ellenberg 		if (device)
257f418815fSLars Ellenberg 			seq_printf(m, "%u\t%u\t", device->minor, device->vnr);
258f418815fSLars Ellenberg 
259f418815fSLars Ellenberg 		seq_printf(m, "%llu\t%u\t%c\t%u\t",
260f418815fSLars Ellenberg 			(unsigned long long)peer_req->i.sector, peer_req->i.size >> 9,
261f418815fSLars Ellenberg 			(peer_req->flags & EE_WRITE) ? 'W' : 'R',
262f418815fSLars Ellenberg 			jiffies_to_msecs(now - peer_req->submit_jif));
263f418815fSLars Ellenberg 		seq_print_peer_request_flags(m, peer_req);
264f418815fSLars Ellenberg 		if (peer_req->flags & EE_SUBMITTED)
265f418815fSLars Ellenberg 			break;
266f418815fSLars Ellenberg 		else
267f418815fSLars Ellenberg 			reported_preparing = true;
268f418815fSLars Ellenberg 	}
269f418815fSLars Ellenberg }
270f418815fSLars Ellenberg 
seq_print_device_peer_requests(struct seq_file * m,struct drbd_device * device,unsigned long now)271f418815fSLars Ellenberg static void seq_print_device_peer_requests(struct seq_file *m,
272f418815fSLars Ellenberg 	struct drbd_device *device, unsigned long now)
273f418815fSLars Ellenberg {
274f418815fSLars Ellenberg 	seq_puts(m, "minor\tvnr\tsector\tsize\trw\tage\tflags\n");
275f418815fSLars Ellenberg 	spin_lock_irq(&device->resource->req_lock);
276f418815fSLars Ellenberg 	seq_print_peer_request(m, device, &device->active_ee, now);
277f418815fSLars Ellenberg 	seq_print_peer_request(m, device, &device->read_ee, now);
278f418815fSLars Ellenberg 	seq_print_peer_request(m, device, &device->sync_ee, now);
279f418815fSLars Ellenberg 	spin_unlock_irq(&device->resource->req_lock);
280f418815fSLars Ellenberg 	if (test_bit(FLUSH_PENDING, &device->flags)) {
281f418815fSLars Ellenberg 		seq_printf(m, "%u\t%u\t-\t-\tF\t%u\tflush\n",
282f418815fSLars Ellenberg 			device->minor, device->vnr,
283f418815fSLars Ellenberg 			jiffies_to_msecs(now - device->flush_jif));
284f418815fSLars Ellenberg 	}
285f418815fSLars Ellenberg }
286f418815fSLars Ellenberg 
seq_print_resource_pending_peer_requests(struct seq_file * m,struct drbd_resource * resource,unsigned long now)287f418815fSLars Ellenberg static void seq_print_resource_pending_peer_requests(struct seq_file *m,
288f418815fSLars Ellenberg 	struct drbd_resource *resource, unsigned long now)
289f418815fSLars Ellenberg {
290f418815fSLars Ellenberg 	struct drbd_device *device;
291f418815fSLars Ellenberg 	unsigned int i;
292f418815fSLars Ellenberg 
293f418815fSLars Ellenberg 	rcu_read_lock();
294f418815fSLars Ellenberg 	idr_for_each_entry(&resource->devices, device, i) {
295f418815fSLars Ellenberg 		seq_print_device_peer_requests(m, device, now);
296f418815fSLars Ellenberg 	}
297f418815fSLars Ellenberg 	rcu_read_unlock();
298f418815fSLars Ellenberg }
299f418815fSLars Ellenberg 
seq_print_resource_transfer_log_summary(struct seq_file * m,struct drbd_resource * resource,struct drbd_connection * connection,unsigned long now)300db1866ffSLars Ellenberg static void seq_print_resource_transfer_log_summary(struct seq_file *m,
301db1866ffSLars Ellenberg 	struct drbd_resource *resource,
302db1866ffSLars Ellenberg 	struct drbd_connection *connection,
303db1866ffSLars Ellenberg 	unsigned long now)
304db1866ffSLars Ellenberg {
305db1866ffSLars Ellenberg 	struct drbd_request *req;
306db1866ffSLars Ellenberg 	unsigned int count = 0;
307db1866ffSLars Ellenberg 	unsigned int show_state = 0;
308db1866ffSLars Ellenberg 
309db1866ffSLars Ellenberg 	seq_puts(m, "n\tdevice\tvnr\t" RQ_HDR);
310db1866ffSLars Ellenberg 	spin_lock_irq(&resource->req_lock);
311db1866ffSLars Ellenberg 	list_for_each_entry(req, &connection->transfer_log, tl_requests) {
312db1866ffSLars Ellenberg 		unsigned int tmp = 0;
313db1866ffSLars Ellenberg 		unsigned int s;
314db1866ffSLars Ellenberg 		++count;
315db1866ffSLars Ellenberg 
316db1866ffSLars Ellenberg 		/* don't disable irq "forever" */
317db1866ffSLars Ellenberg 		if (!(count & 0x1ff)) {
318db1866ffSLars Ellenberg 			struct drbd_request *req_next;
319db1866ffSLars Ellenberg 			kref_get(&req->kref);
320db1866ffSLars Ellenberg 			spin_unlock_irq(&resource->req_lock);
321db1866ffSLars Ellenberg 			cond_resched();
322db1866ffSLars Ellenberg 			spin_lock_irq(&resource->req_lock);
323db1866ffSLars Ellenberg 			req_next = list_next_entry(req, tl_requests);
324db1866ffSLars Ellenberg 			if (kref_put(&req->kref, drbd_req_destroy))
325db1866ffSLars Ellenberg 				req = req_next;
326db1866ffSLars Ellenberg 			if (&req->tl_requests == &connection->transfer_log)
327db1866ffSLars Ellenberg 				break;
328db1866ffSLars Ellenberg 		}
329db1866ffSLars Ellenberg 
330db1866ffSLars Ellenberg 		s = req->rq_state;
331db1866ffSLars Ellenberg 
332db1866ffSLars Ellenberg 		/* This is meant to summarize timing issues, to be able to tell
333db1866ffSLars Ellenberg 		 * local disk problems from network problems.
334db1866ffSLars Ellenberg 		 * Skip requests, if we have shown an even older request with
335db1866ffSLars Ellenberg 		 * similar aspects already.  */
336db1866ffSLars Ellenberg 		if (req->master_bio == NULL)
337db1866ffSLars Ellenberg 			tmp |= 1;
338db1866ffSLars Ellenberg 		if ((s & RQ_LOCAL_MASK) && (s & RQ_LOCAL_PENDING))
339db1866ffSLars Ellenberg 			tmp |= 2;
340db1866ffSLars Ellenberg 		if (s & RQ_NET_MASK) {
341db1866ffSLars Ellenberg 			if (!(s & RQ_NET_SENT))
342db1866ffSLars Ellenberg 				tmp |= 4;
343db1866ffSLars Ellenberg 			if (s & RQ_NET_PENDING)
344db1866ffSLars Ellenberg 				tmp |= 8;
345db1866ffSLars Ellenberg 			if (!(s & RQ_NET_DONE))
346db1866ffSLars Ellenberg 				tmp |= 16;
347db1866ffSLars Ellenberg 		}
348db1866ffSLars Ellenberg 		if ((tmp & show_state) == tmp)
349db1866ffSLars Ellenberg 			continue;
350db1866ffSLars Ellenberg 		show_state |= tmp;
351db1866ffSLars Ellenberg 		seq_printf(m, "%u\t", count);
352db1866ffSLars Ellenberg 		seq_print_minor_vnr_req(m, req, now);
353db1866ffSLars Ellenberg 		if (show_state == 0x1f)
354db1866ffSLars Ellenberg 			break;
355db1866ffSLars Ellenberg 	}
356db1866ffSLars Ellenberg 	spin_unlock_irq(&resource->req_lock);
357db1866ffSLars Ellenberg }
358db1866ffSLars Ellenberg 
359db1866ffSLars Ellenberg /* TODO: transfer_log and friends should be moved to resource */
in_flight_summary_show(struct seq_file * m,void * pos)360db1866ffSLars Ellenberg static int in_flight_summary_show(struct seq_file *m, void *pos)
361db1866ffSLars Ellenberg {
362db1866ffSLars Ellenberg 	struct drbd_resource *resource = m->private;
363db1866ffSLars Ellenberg 	struct drbd_connection *connection;
364db1866ffSLars Ellenberg 	unsigned long jif = jiffies;
365db1866ffSLars Ellenberg 
366db1866ffSLars Ellenberg 	connection = first_connection(resource);
367db1866ffSLars Ellenberg 	/* This does not happen, actually.
368db1866ffSLars Ellenberg 	 * But be robust and prepare for future code changes. */
3694a521ccaSLars Ellenberg 	if (!connection || !kref_get_unless_zero(&connection->kref))
3704a521ccaSLars Ellenberg 		return -ESTALE;
371db1866ffSLars Ellenberg 
372b44e1184SLars Ellenberg 	/* BUMP me if you change the file format/content/presentation */
373b44e1184SLars Ellenberg 	seq_printf(m, "v: %u\n\n", 0);
374b44e1184SLars Ellenberg 
375f418815fSLars Ellenberg 	seq_puts(m, "oldest bitmap IO\n");
376f418815fSLars Ellenberg 	seq_print_resource_pending_bitmap_io(m, resource, jif);
377f418815fSLars Ellenberg 	seq_putc(m, '\n');
378f418815fSLars Ellenberg 
379f418815fSLars Ellenberg 	seq_puts(m, "meta data IO\n");
380f418815fSLars Ellenberg 	seq_print_resource_pending_meta_io(m, resource, jif);
381f418815fSLars Ellenberg 	seq_putc(m, '\n');
382f418815fSLars Ellenberg 
383f418815fSLars Ellenberg 	seq_puts(m, "socket buffer stats\n");
384f418815fSLars Ellenberg 	/* for each connection ... once we have more than one */
385f418815fSLars Ellenberg 	rcu_read_lock();
386f418815fSLars Ellenberg 	if (connection->data.socket) {
387f418815fSLars Ellenberg 		/* open coded SIOCINQ, the "relevant" part */
388f418815fSLars Ellenberg 		struct tcp_sock *tp = tcp_sk(connection->data.socket->sk);
389f418815fSLars Ellenberg 		int answ = tp->rcv_nxt - tp->copied_seq;
390f418815fSLars Ellenberg 		seq_printf(m, "unread receive buffer: %u Byte\n", answ);
391f418815fSLars Ellenberg 		/* open coded SIOCOUTQ, the "relevant" part */
392f418815fSLars Ellenberg 		answ = tp->write_seq - tp->snd_una;
393f418815fSLars Ellenberg 		seq_printf(m, "unacked send buffer: %u Byte\n", answ);
394f418815fSLars Ellenberg 	}
395f418815fSLars Ellenberg 	rcu_read_unlock();
396f418815fSLars Ellenberg 	seq_putc(m, '\n');
397f418815fSLars Ellenberg 
398f418815fSLars Ellenberg 	seq_puts(m, "oldest peer requests\n");
399f418815fSLars Ellenberg 	seq_print_resource_pending_peer_requests(m, resource, jif);
400f418815fSLars Ellenberg 	seq_putc(m, '\n');
401f418815fSLars Ellenberg 
402f418815fSLars Ellenberg 	seq_puts(m, "application requests waiting for activity log\n");
403f418815fSLars Ellenberg 	seq_print_waiting_for_AL(m, resource, jif);
404f418815fSLars Ellenberg 	seq_putc(m, '\n');
405f418815fSLars Ellenberg 
406db1866ffSLars Ellenberg 	seq_puts(m, "oldest application requests\n");
407db1866ffSLars Ellenberg 	seq_print_resource_transfer_log_summary(m, resource, connection, jif);
408db1866ffSLars Ellenberg 	seq_putc(m, '\n');
409db1866ffSLars Ellenberg 
410db1866ffSLars Ellenberg 	jif = jiffies - jif;
411db1866ffSLars Ellenberg 	if (jif)
412db1866ffSLars Ellenberg 		seq_printf(m, "generated in %d ms\n", jiffies_to_msecs(jif));
4134a521ccaSLars Ellenberg 	kref_put(&connection->kref, drbd_destroy_connection);
414db1866ffSLars Ellenberg 	return 0;
415db1866ffSLars Ellenberg }
416db1866ffSLars Ellenberg 
4174a521ccaSLars Ellenberg /* make sure at *open* time that the respective object won't go away. */
drbd_single_open(struct file * file,int (* show)(struct seq_file *,void *),void * data,struct kref * kref,void (* release)(struct kref *))4184a521ccaSLars Ellenberg static int drbd_single_open(struct file *file, int (*show)(struct seq_file *, void *),
4194a521ccaSLars Ellenberg 		                void *data, struct kref *kref,
4204a521ccaSLars Ellenberg 				void (*release)(struct kref *))
4214a521ccaSLars Ellenberg {
4224a521ccaSLars Ellenberg 	struct dentry *parent;
4234a521ccaSLars Ellenberg 	int ret = -ESTALE;
4244a521ccaSLars Ellenberg 
4254a521ccaSLars Ellenberg 	/* Are we still linked,
4264a521ccaSLars Ellenberg 	 * or has debugfs_remove() already been called? */
427b583043eSAl Viro 	parent = file->f_path.dentry->d_parent;
4284a521ccaSLars Ellenberg 	/* serialize with d_delete() */
4295955102cSAl Viro 	inode_lock(d_inode(parent));
4304a521ccaSLars Ellenberg 	/* Make sure the object is still alive */
431dc3f4198SAl Viro 	if (simple_positive(file->f_path.dentry)
43254e6fc38SLars Ellenberg 	&& kref_get_unless_zero(kref))
4334a521ccaSLars Ellenberg 		ret = 0;
4345955102cSAl Viro 	inode_unlock(d_inode(parent));
4354a521ccaSLars Ellenberg 	if (!ret) {
4364a521ccaSLars Ellenberg 		ret = single_open(file, show, data);
4374a521ccaSLars Ellenberg 		if (ret)
4384a521ccaSLars Ellenberg 			kref_put(kref, release);
4394a521ccaSLars Ellenberg 	}
4404a521ccaSLars Ellenberg 	return ret;
4414a521ccaSLars Ellenberg }
4424a521ccaSLars Ellenberg 
in_flight_summary_open(struct inode * inode,struct file * file)443db1866ffSLars Ellenberg static int in_flight_summary_open(struct inode *inode, struct file *file)
444db1866ffSLars Ellenberg {
4454a521ccaSLars Ellenberg 	struct drbd_resource *resource = inode->i_private;
4464a521ccaSLars Ellenberg 	return drbd_single_open(file, in_flight_summary_show, resource,
4474a521ccaSLars Ellenberg 				&resource->kref, drbd_destroy_resource);
4484a521ccaSLars Ellenberg }
4494a521ccaSLars Ellenberg 
in_flight_summary_release(struct inode * inode,struct file * file)4504a521ccaSLars Ellenberg static int in_flight_summary_release(struct inode *inode, struct file *file)
4514a521ccaSLars Ellenberg {
4524a521ccaSLars Ellenberg 	struct drbd_resource *resource = inode->i_private;
4534a521ccaSLars Ellenberg 	kref_put(&resource->kref, drbd_destroy_resource);
4544a521ccaSLars Ellenberg 	return single_release(inode, file);
455db1866ffSLars Ellenberg }
456db1866ffSLars Ellenberg 
457db1866ffSLars Ellenberg static const struct file_operations in_flight_summary_fops = {
458db1866ffSLars Ellenberg 	.owner		= THIS_MODULE,
459db1866ffSLars Ellenberg 	.open		= in_flight_summary_open,
460db1866ffSLars Ellenberg 	.read		= seq_read,
461db1866ffSLars Ellenberg 	.llseek		= seq_lseek,
4624a521ccaSLars Ellenberg 	.release	= in_flight_summary_release,
463db1866ffSLars Ellenberg };
464db1866ffSLars Ellenberg 
drbd_debugfs_resource_add(struct drbd_resource * resource)4654d3d5aa8SLars Ellenberg void drbd_debugfs_resource_add(struct drbd_resource *resource)
4664d3d5aa8SLars Ellenberg {
4674d3d5aa8SLars Ellenberg 	struct dentry *dentry;
4684d3d5aa8SLars Ellenberg 
4694d3d5aa8SLars Ellenberg 	dentry = debugfs_create_dir(resource->name, drbd_debugfs_resources);
4704d3d5aa8SLars Ellenberg 	resource->debugfs_res = dentry;
4714d3d5aa8SLars Ellenberg 
4724d3d5aa8SLars Ellenberg 	dentry = debugfs_create_dir("volumes", resource->debugfs_res);
4734d3d5aa8SLars Ellenberg 	resource->debugfs_res_volumes = dentry;
4744d3d5aa8SLars Ellenberg 
4754d3d5aa8SLars Ellenberg 	dentry = debugfs_create_dir("connections", resource->debugfs_res);
4764d3d5aa8SLars Ellenberg 	resource->debugfs_res_connections = dentry;
4774d3d5aa8SLars Ellenberg 
4785657a819SJoe Perches 	dentry = debugfs_create_file("in_flight_summary", 0440,
479db1866ffSLars Ellenberg 				     resource->debugfs_res, resource,
480db1866ffSLars Ellenberg 				     &in_flight_summary_fops);
481db1866ffSLars Ellenberg 	resource->debugfs_res_in_flight_summary = dentry;
4824d3d5aa8SLars Ellenberg }
4834d3d5aa8SLars Ellenberg 
drbd_debugfs_remove(struct dentry ** dp)4844d3d5aa8SLars Ellenberg static void drbd_debugfs_remove(struct dentry **dp)
4854d3d5aa8SLars Ellenberg {
4864d3d5aa8SLars Ellenberg 	debugfs_remove(*dp);
4874d3d5aa8SLars Ellenberg 	*dp = NULL;
4884d3d5aa8SLars Ellenberg }
4894d3d5aa8SLars Ellenberg 
drbd_debugfs_resource_cleanup(struct drbd_resource * resource)4904d3d5aa8SLars Ellenberg void drbd_debugfs_resource_cleanup(struct drbd_resource *resource)
4914d3d5aa8SLars Ellenberg {
4924d3d5aa8SLars Ellenberg 	/* it is ok to call debugfs_remove(NULL) */
4934d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&resource->debugfs_res_in_flight_summary);
4944d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&resource->debugfs_res_connections);
4954d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&resource->debugfs_res_volumes);
4964d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&resource->debugfs_res);
4974d3d5aa8SLars Ellenberg }
4984d3d5aa8SLars Ellenberg 
seq_print_one_timing_detail(struct seq_file * m,const struct drbd_thread_timing_details * tdp,unsigned long now)499944410e9SLars Ellenberg static void seq_print_one_timing_detail(struct seq_file *m,
500944410e9SLars Ellenberg 	const struct drbd_thread_timing_details *tdp,
501944410e9SLars Ellenberg 	unsigned long now)
502944410e9SLars Ellenberg {
503944410e9SLars Ellenberg 	struct drbd_thread_timing_details td;
504944410e9SLars Ellenberg 	/* No locking...
505944410e9SLars Ellenberg 	 * use temporary assignment to get at consistent data. */
506944410e9SLars Ellenberg 	do {
507944410e9SLars Ellenberg 		td = *tdp;
508944410e9SLars Ellenberg 	} while (td.cb_nr != tdp->cb_nr);
509944410e9SLars Ellenberg 	if (!td.cb_addr)
510944410e9SLars Ellenberg 		return;
511944410e9SLars Ellenberg 	seq_printf(m, "%u\t%d\t%s:%u\t%ps\n",
512944410e9SLars Ellenberg 			td.cb_nr,
513944410e9SLars Ellenberg 			jiffies_to_msecs(now - td.start_jif),
514944410e9SLars Ellenberg 			td.caller_fn, td.line,
515944410e9SLars Ellenberg 			td.cb_addr);
516944410e9SLars Ellenberg }
517944410e9SLars Ellenberg 
seq_print_timing_details(struct seq_file * m,const char * title,unsigned int cb_nr,struct drbd_thread_timing_details * tdp,unsigned long now)518944410e9SLars Ellenberg static void seq_print_timing_details(struct seq_file *m,
519944410e9SLars Ellenberg 		const char *title,
520944410e9SLars Ellenberg 		unsigned int cb_nr, struct drbd_thread_timing_details *tdp, unsigned long now)
521944410e9SLars Ellenberg {
522944410e9SLars Ellenberg 	unsigned int start_idx;
523944410e9SLars Ellenberg 	unsigned int i;
524944410e9SLars Ellenberg 
525944410e9SLars Ellenberg 	seq_printf(m, "%s\n", title);
526944410e9SLars Ellenberg 	/* If not much is going on, this will result in natural ordering.
527944410e9SLars Ellenberg 	 * If it is very busy, we will possibly skip events, or even see wrap
528944410e9SLars Ellenberg 	 * arounds, which could only be avoided with locking.
529944410e9SLars Ellenberg 	 */
530944410e9SLars Ellenberg 	start_idx = cb_nr % DRBD_THREAD_DETAILS_HIST;
531944410e9SLars Ellenberg 	for (i = start_idx; i < DRBD_THREAD_DETAILS_HIST; i++)
532944410e9SLars Ellenberg 		seq_print_one_timing_detail(m, tdp+i, now);
533944410e9SLars Ellenberg 	for (i = 0; i < start_idx; i++)
534944410e9SLars Ellenberg 		seq_print_one_timing_detail(m, tdp+i, now);
535944410e9SLars Ellenberg }
536944410e9SLars Ellenberg 
callback_history_show(struct seq_file * m,void * ignored)537944410e9SLars Ellenberg static int callback_history_show(struct seq_file *m, void *ignored)
538944410e9SLars Ellenberg {
539944410e9SLars Ellenberg 	struct drbd_connection *connection = m->private;
540944410e9SLars Ellenberg 	unsigned long jif = jiffies;
541944410e9SLars Ellenberg 
542b44e1184SLars Ellenberg 	/* BUMP me if you change the file format/content/presentation */
543b44e1184SLars Ellenberg 	seq_printf(m, "v: %u\n\n", 0);
544b44e1184SLars Ellenberg 
545944410e9SLars Ellenberg 	seq_puts(m, "n\tage\tcallsite\tfn\n");
546944410e9SLars Ellenberg 	seq_print_timing_details(m, "worker", connection->w_cb_nr, connection->w_timing_details, jif);
547944410e9SLars Ellenberg 	seq_print_timing_details(m, "receiver", connection->r_cb_nr, connection->r_timing_details, jif);
548944410e9SLars Ellenberg 	return 0;
549944410e9SLars Ellenberg }
550944410e9SLars Ellenberg 
callback_history_open(struct inode * inode,struct file * file)551944410e9SLars Ellenberg static int callback_history_open(struct inode *inode, struct file *file)
552944410e9SLars Ellenberg {
553944410e9SLars Ellenberg 	struct drbd_connection *connection = inode->i_private;
554944410e9SLars Ellenberg 	return drbd_single_open(file, callback_history_show, connection,
555944410e9SLars Ellenberg 				&connection->kref, drbd_destroy_connection);
556944410e9SLars Ellenberg }
557944410e9SLars Ellenberg 
callback_history_release(struct inode * inode,struct file * file)558944410e9SLars Ellenberg static int callback_history_release(struct inode *inode, struct file *file)
559944410e9SLars Ellenberg {
560944410e9SLars Ellenberg 	struct drbd_connection *connection = inode->i_private;
561944410e9SLars Ellenberg 	kref_put(&connection->kref, drbd_destroy_connection);
562944410e9SLars Ellenberg 	return single_release(inode, file);
563944410e9SLars Ellenberg }
564944410e9SLars Ellenberg 
565944410e9SLars Ellenberg static const struct file_operations connection_callback_history_fops = {
566944410e9SLars Ellenberg 	.owner		= THIS_MODULE,
567944410e9SLars Ellenberg 	.open		= callback_history_open,
568944410e9SLars Ellenberg 	.read		= seq_read,
569944410e9SLars Ellenberg 	.llseek		= seq_lseek,
570944410e9SLars Ellenberg 	.release	= callback_history_release,
571944410e9SLars Ellenberg };
572944410e9SLars Ellenberg 
connection_oldest_requests_show(struct seq_file * m,void * ignored)57354e6fc38SLars Ellenberg static int connection_oldest_requests_show(struct seq_file *m, void *ignored)
57454e6fc38SLars Ellenberg {
57554e6fc38SLars Ellenberg 	struct drbd_connection *connection = m->private;
57654e6fc38SLars Ellenberg 	unsigned long now = jiffies;
57754e6fc38SLars Ellenberg 	struct drbd_request *r1, *r2;
57854e6fc38SLars Ellenberg 
57954e6fc38SLars Ellenberg 	/* BUMP me if you change the file format/content/presentation */
58054e6fc38SLars Ellenberg 	seq_printf(m, "v: %u\n\n", 0);
58154e6fc38SLars Ellenberg 
58254e6fc38SLars Ellenberg 	spin_lock_irq(&connection->resource->req_lock);
58354e6fc38SLars Ellenberg 	r1 = connection->req_next;
58454e6fc38SLars Ellenberg 	if (r1)
58554e6fc38SLars Ellenberg 		seq_print_minor_vnr_req(m, r1, now);
58654e6fc38SLars Ellenberg 	r2 = connection->req_ack_pending;
58754e6fc38SLars Ellenberg 	if (r2 && r2 != r1) {
58854e6fc38SLars Ellenberg 		r1 = r2;
58954e6fc38SLars Ellenberg 		seq_print_minor_vnr_req(m, r1, now);
59054e6fc38SLars Ellenberg 	}
59154e6fc38SLars Ellenberg 	r2 = connection->req_not_net_done;
59254e6fc38SLars Ellenberg 	if (r2 && r2 != r1)
59354e6fc38SLars Ellenberg 		seq_print_minor_vnr_req(m, r2, now);
59454e6fc38SLars Ellenberg 	spin_unlock_irq(&connection->resource->req_lock);
59554e6fc38SLars Ellenberg 	return 0;
59654e6fc38SLars Ellenberg }
59754e6fc38SLars Ellenberg 
connection_oldest_requests_open(struct inode * inode,struct file * file)59854e6fc38SLars Ellenberg static int connection_oldest_requests_open(struct inode *inode, struct file *file)
59954e6fc38SLars Ellenberg {
60054e6fc38SLars Ellenberg 	struct drbd_connection *connection = inode->i_private;
60154e6fc38SLars Ellenberg 	return drbd_single_open(file, connection_oldest_requests_show, connection,
60254e6fc38SLars Ellenberg 				&connection->kref, drbd_destroy_connection);
60354e6fc38SLars Ellenberg }
60454e6fc38SLars Ellenberg 
connection_oldest_requests_release(struct inode * inode,struct file * file)60554e6fc38SLars Ellenberg static int connection_oldest_requests_release(struct inode *inode, struct file *file)
60654e6fc38SLars Ellenberg {
60754e6fc38SLars Ellenberg 	struct drbd_connection *connection = inode->i_private;
60854e6fc38SLars Ellenberg 	kref_put(&connection->kref, drbd_destroy_connection);
60954e6fc38SLars Ellenberg 	return single_release(inode, file);
61054e6fc38SLars Ellenberg }
61154e6fc38SLars Ellenberg 
61254e6fc38SLars Ellenberg static const struct file_operations connection_oldest_requests_fops = {
61354e6fc38SLars Ellenberg 	.owner		= THIS_MODULE,
61454e6fc38SLars Ellenberg 	.open		= connection_oldest_requests_open,
61554e6fc38SLars Ellenberg 	.read		= seq_read,
61654e6fc38SLars Ellenberg 	.llseek		= seq_lseek,
61754e6fc38SLars Ellenberg 	.release	= connection_oldest_requests_release,
61854e6fc38SLars Ellenberg };
61954e6fc38SLars Ellenberg 
drbd_debugfs_connection_add(struct drbd_connection * connection)6204d3d5aa8SLars Ellenberg void drbd_debugfs_connection_add(struct drbd_connection *connection)
6214d3d5aa8SLars Ellenberg {
6224d3d5aa8SLars Ellenberg 	struct dentry *conns_dir = connection->resource->debugfs_res_connections;
6234d3d5aa8SLars Ellenberg 	struct dentry *dentry;
6244d3d5aa8SLars Ellenberg 
6254d3d5aa8SLars Ellenberg 	/* Once we enable mutliple peers,
6264d3d5aa8SLars Ellenberg 	 * these connections will have descriptive names.
6274d3d5aa8SLars Ellenberg 	 * For now, it is just the one connection to the (only) "peer". */
6284d3d5aa8SLars Ellenberg 	dentry = debugfs_create_dir("peer", conns_dir);
6294d3d5aa8SLars Ellenberg 	connection->debugfs_conn = dentry;
630944410e9SLars Ellenberg 
6315657a819SJoe Perches 	dentry = debugfs_create_file("callback_history", 0440,
632944410e9SLars Ellenberg 				     connection->debugfs_conn, connection,
633944410e9SLars Ellenberg 				     &connection_callback_history_fops);
634944410e9SLars Ellenberg 	connection->debugfs_conn_callback_history = dentry;
6353d299f48SLars Ellenberg 
6365657a819SJoe Perches 	dentry = debugfs_create_file("oldest_requests", 0440,
6373d299f48SLars Ellenberg 				     connection->debugfs_conn, connection,
6383d299f48SLars Ellenberg 				     &connection_oldest_requests_fops);
6393d299f48SLars Ellenberg 	connection->debugfs_conn_oldest_requests = dentry;
6404d3d5aa8SLars Ellenberg }
6414d3d5aa8SLars Ellenberg 
drbd_debugfs_connection_cleanup(struct drbd_connection * connection)6424d3d5aa8SLars Ellenberg void drbd_debugfs_connection_cleanup(struct drbd_connection *connection)
6434d3d5aa8SLars Ellenberg {
6444d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&connection->debugfs_conn_callback_history);
6454d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&connection->debugfs_conn_oldest_requests);
6464d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&connection->debugfs_conn);
6474d3d5aa8SLars Ellenberg }
6484d3d5aa8SLars Ellenberg 
resync_dump_detail(struct seq_file * m,struct lc_element * e)64954e6fc38SLars Ellenberg static void resync_dump_detail(struct seq_file *m, struct lc_element *e)
65054e6fc38SLars Ellenberg {
65154e6fc38SLars Ellenberg        struct bm_extent *bme = lc_entry(e, struct bm_extent, lce);
65254e6fc38SLars Ellenberg 
653f0c21e62SPhilipp Marek        seq_printf(m, "%5d %s %s %s", bme->rs_left,
65454e6fc38SLars Ellenberg 		  test_bit(BME_NO_WRITES, &bme->flags) ? "NO_WRITES" : "---------",
65554e6fc38SLars Ellenberg 		  test_bit(BME_LOCKED, &bme->flags) ? "LOCKED" : "------",
65654e6fc38SLars Ellenberg 		  test_bit(BME_PRIORITY, &bme->flags) ? "PRIORITY" : "--------"
65754e6fc38SLars Ellenberg 		  );
65854e6fc38SLars Ellenberg }
65954e6fc38SLars Ellenberg 
device_resync_extents_show(struct seq_file * m,void * ignored)66054e6fc38SLars Ellenberg static int device_resync_extents_show(struct seq_file *m, void *ignored)
66154e6fc38SLars Ellenberg {
66254e6fc38SLars Ellenberg 	struct drbd_device *device = m->private;
663b44e1184SLars Ellenberg 
664b44e1184SLars Ellenberg 	/* BUMP me if you change the file format/content/presentation */
665b44e1184SLars Ellenberg 	seq_printf(m, "v: %u\n\n", 0);
666b44e1184SLars Ellenberg 
66754e6fc38SLars Ellenberg 	if (get_ldev_if_state(device, D_FAILED)) {
66854e6fc38SLars Ellenberg 		lc_seq_printf_stats(m, device->resync);
66954e6fc38SLars Ellenberg 		lc_seq_dump_details(m, device->resync, "rs_left flags", resync_dump_detail);
67054e6fc38SLars Ellenberg 		put_ldev(device);
67154e6fc38SLars Ellenberg 	}
67254e6fc38SLars Ellenberg 	return 0;
67354e6fc38SLars Ellenberg }
67454e6fc38SLars Ellenberg 
device_act_log_extents_show(struct seq_file * m,void * ignored)67554e6fc38SLars Ellenberg static int device_act_log_extents_show(struct seq_file *m, void *ignored)
67654e6fc38SLars Ellenberg {
67754e6fc38SLars Ellenberg 	struct drbd_device *device = m->private;
678b44e1184SLars Ellenberg 
679b44e1184SLars Ellenberg 	/* BUMP me if you change the file format/content/presentation */
680b44e1184SLars Ellenberg 	seq_printf(m, "v: %u\n\n", 0);
681b44e1184SLars Ellenberg 
68254e6fc38SLars Ellenberg 	if (get_ldev_if_state(device, D_FAILED)) {
68354e6fc38SLars Ellenberg 		lc_seq_printf_stats(m, device->act_log);
68454e6fc38SLars Ellenberg 		lc_seq_dump_details(m, device->act_log, "", NULL);
68554e6fc38SLars Ellenberg 		put_ldev(device);
68654e6fc38SLars Ellenberg 	}
68754e6fc38SLars Ellenberg 	return 0;
68854e6fc38SLars Ellenberg }
68954e6fc38SLars Ellenberg 
device_oldest_requests_show(struct seq_file * m,void * ignored)69054e6fc38SLars Ellenberg static int device_oldest_requests_show(struct seq_file *m, void *ignored)
69154e6fc38SLars Ellenberg {
69254e6fc38SLars Ellenberg 	struct drbd_device *device = m->private;
69354e6fc38SLars Ellenberg 	struct drbd_resource *resource = device->resource;
69454e6fc38SLars Ellenberg 	unsigned long now = jiffies;
69554e6fc38SLars Ellenberg 	struct drbd_request *r1, *r2;
69654e6fc38SLars Ellenberg 	int i;
69754e6fc38SLars Ellenberg 
698b44e1184SLars Ellenberg 	/* BUMP me if you change the file format/content/presentation */
699b44e1184SLars Ellenberg 	seq_printf(m, "v: %u\n\n", 0);
700b44e1184SLars Ellenberg 
70154e6fc38SLars Ellenberg 	seq_puts(m, RQ_HDR);
70254e6fc38SLars Ellenberg 	spin_lock_irq(&resource->req_lock);
70354e6fc38SLars Ellenberg 	/* WRITE, then READ */
70454e6fc38SLars Ellenberg 	for (i = 1; i >= 0; --i) {
70554e6fc38SLars Ellenberg 		r1 = list_first_entry_or_null(&device->pending_master_completion[i],
70654e6fc38SLars Ellenberg 			struct drbd_request, req_pending_master_completion);
70754e6fc38SLars Ellenberg 		r2 = list_first_entry_or_null(&device->pending_completion[i],
70854e6fc38SLars Ellenberg 			struct drbd_request, req_pending_local);
70954e6fc38SLars Ellenberg 		if (r1)
71054e6fc38SLars Ellenberg 			seq_print_one_request(m, r1, now);
71154e6fc38SLars Ellenberg 		if (r2 && r2 != r1)
71254e6fc38SLars Ellenberg 			seq_print_one_request(m, r2, now);
71354e6fc38SLars Ellenberg 	}
71454e6fc38SLars Ellenberg 	spin_unlock_irq(&resource->req_lock);
71554e6fc38SLars Ellenberg 	return 0;
71654e6fc38SLars Ellenberg }
71754e6fc38SLars Ellenberg 
device_data_gen_id_show(struct seq_file * m,void * ignored)718cc356f85SLars Ellenberg static int device_data_gen_id_show(struct seq_file *m, void *ignored)
719cc356f85SLars Ellenberg {
720cc356f85SLars Ellenberg 	struct drbd_device *device = m->private;
721cc356f85SLars Ellenberg 	struct drbd_md *md;
722cc356f85SLars Ellenberg 	enum drbd_uuid_index idx;
723cc356f85SLars Ellenberg 
724cc356f85SLars Ellenberg 	if (!get_ldev_if_state(device, D_FAILED))
725cc356f85SLars Ellenberg 		return -ENODEV;
726cc356f85SLars Ellenberg 
727cc356f85SLars Ellenberg 	md = &device->ldev->md;
728cc356f85SLars Ellenberg 	spin_lock_irq(&md->uuid_lock);
729cc356f85SLars Ellenberg 	for (idx = UI_CURRENT; idx <= UI_HISTORY_END; idx++) {
730cc356f85SLars Ellenberg 		seq_printf(m, "0x%016llX\n", md->uuid[idx]);
731cc356f85SLars Ellenberg 	}
732cc356f85SLars Ellenberg 	spin_unlock_irq(&md->uuid_lock);
733cc356f85SLars Ellenberg 	put_ldev(device);
734cc356f85SLars Ellenberg 	return 0;
735cc356f85SLars Ellenberg }
736cc356f85SLars Ellenberg 
device_ed_gen_id_show(struct seq_file * m,void * ignored)737f5ec0173SLars Ellenberg static int device_ed_gen_id_show(struct seq_file *m, void *ignored)
738f5ec0173SLars Ellenberg {
739f5ec0173SLars Ellenberg 	struct drbd_device *device = m->private;
740f5ec0173SLars Ellenberg 	seq_printf(m, "0x%016llX\n", (unsigned long long)device->ed_uuid);
741f5ec0173SLars Ellenberg 	return 0;
742f5ec0173SLars Ellenberg }
743f5ec0173SLars Ellenberg 
74454e6fc38SLars Ellenberg #define drbd_debugfs_device_attr(name)						\
74554e6fc38SLars Ellenberg static int device_ ## name ## _open(struct inode *inode, struct file *file)	\
74654e6fc38SLars Ellenberg {										\
74754e6fc38SLars Ellenberg 	struct drbd_device *device = inode->i_private;				\
74854e6fc38SLars Ellenberg 	return drbd_single_open(file, device_ ## name ## _show, device,		\
74954e6fc38SLars Ellenberg 				&device->kref, drbd_destroy_device);		\
75054e6fc38SLars Ellenberg }										\
75154e6fc38SLars Ellenberg static int device_ ## name ## _release(struct inode *inode, struct file *file)	\
75254e6fc38SLars Ellenberg {										\
75354e6fc38SLars Ellenberg 	struct drbd_device *device = inode->i_private;				\
75454e6fc38SLars Ellenberg 	kref_put(&device->kref, drbd_destroy_device);				\
75554e6fc38SLars Ellenberg 	return single_release(inode, file);					\
75654e6fc38SLars Ellenberg }										\
75754e6fc38SLars Ellenberg static const struct file_operations device_ ## name ## _fops = {		\
75854e6fc38SLars Ellenberg 	.owner		= THIS_MODULE,						\
75954e6fc38SLars Ellenberg 	.open		= device_ ## name ## _open,				\
76054e6fc38SLars Ellenberg 	.read		= seq_read,						\
76154e6fc38SLars Ellenberg 	.llseek		= seq_lseek,						\
76254e6fc38SLars Ellenberg 	.release	= device_ ## name ## _release,				\
76354e6fc38SLars Ellenberg };
76454e6fc38SLars Ellenberg 
76554e6fc38SLars Ellenberg drbd_debugfs_device_attr(oldest_requests)
drbd_debugfs_device_attr(act_log_extents)76654e6fc38SLars Ellenberg drbd_debugfs_device_attr(act_log_extents)
76754e6fc38SLars Ellenberg drbd_debugfs_device_attr(resync_extents)
768cc356f85SLars Ellenberg drbd_debugfs_device_attr(data_gen_id)
769f5ec0173SLars Ellenberg drbd_debugfs_device_attr(ed_gen_id)
77054e6fc38SLars Ellenberg 
7714d3d5aa8SLars Ellenberg void drbd_debugfs_device_add(struct drbd_device *device)
7724d3d5aa8SLars Ellenberg {
7734d3d5aa8SLars Ellenberg 	struct dentry *vols_dir = device->resource->debugfs_res_volumes;
7744d3d5aa8SLars Ellenberg 	char minor_buf[8]; /* MINORMASK, MINORBITS == 20; */
7754d3d5aa8SLars Ellenberg 	char vnr_buf[8];   /* volume number vnr is even 16 bit only; */
7764d3d5aa8SLars Ellenberg 	char *slink_name = NULL;
7774d3d5aa8SLars Ellenberg 
7784d3d5aa8SLars Ellenberg 	struct dentry *dentry;
7794d3d5aa8SLars Ellenberg 	if (!vols_dir || !drbd_debugfs_minors)
7804d3d5aa8SLars Ellenberg 		return;
7814d3d5aa8SLars Ellenberg 
7824d3d5aa8SLars Ellenberg 	snprintf(vnr_buf, sizeof(vnr_buf), "%u", device->vnr);
7834d3d5aa8SLars Ellenberg 	dentry = debugfs_create_dir(vnr_buf, vols_dir);
7844d3d5aa8SLars Ellenberg 	device->debugfs_vol = dentry;
7854d3d5aa8SLars Ellenberg 
7864d3d5aa8SLars Ellenberg 	snprintf(minor_buf, sizeof(minor_buf), "%u", device->minor);
7874d3d5aa8SLars Ellenberg 	slink_name = kasprintf(GFP_KERNEL, "../resources/%s/volumes/%u",
7884d3d5aa8SLars Ellenberg 			device->resource->name, device->vnr);
7894d3d5aa8SLars Ellenberg 	if (!slink_name)
7904d3d5aa8SLars Ellenberg 		goto fail;
7914d3d5aa8SLars Ellenberg 	dentry = debugfs_create_symlink(minor_buf, drbd_debugfs_minors, slink_name);
792d27e84a3SGreg Kroah-Hartman 	device->debugfs_minor = dentry;
79354e6fc38SLars Ellenberg 	kfree(slink_name);
79454e6fc38SLars Ellenberg 	slink_name = NULL;
79554e6fc38SLars Ellenberg 
79654e6fc38SLars Ellenberg #define DCF(name)	do {					\
7975657a819SJoe Perches 	dentry = debugfs_create_file(#name, 0440,	\
79854e6fc38SLars Ellenberg 			device->debugfs_vol, device,		\
79954e6fc38SLars Ellenberg 			&device_ ## name ## _fops);		\
80054e6fc38SLars Ellenberg 	device->debugfs_vol_ ## name = dentry;			\
80154e6fc38SLars Ellenberg 	} while (0)
80254e6fc38SLars Ellenberg 
80354e6fc38SLars Ellenberg 	DCF(oldest_requests);
80454e6fc38SLars Ellenberg 	DCF(act_log_extents);
80554e6fc38SLars Ellenberg 	DCF(resync_extents);
806cc356f85SLars Ellenberg 	DCF(data_gen_id);
807f5ec0173SLars Ellenberg 	DCF(ed_gen_id);
808cc356f85SLars Ellenberg #undef DCF
80954e6fc38SLars Ellenberg 	return;
8104d3d5aa8SLars Ellenberg 
8114d3d5aa8SLars Ellenberg fail:
8124d3d5aa8SLars Ellenberg 	drbd_debugfs_device_cleanup(device);
8134d3d5aa8SLars Ellenberg 	drbd_err(device, "failed to create debugfs entries\n");
8144d3d5aa8SLars Ellenberg }
8154d3d5aa8SLars Ellenberg 
drbd_debugfs_device_cleanup(struct drbd_device * device)8164d3d5aa8SLars Ellenberg void drbd_debugfs_device_cleanup(struct drbd_device *device)
8174d3d5aa8SLars Ellenberg {
8184d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&device->debugfs_minor);
8194d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&device->debugfs_vol_oldest_requests);
8204d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&device->debugfs_vol_act_log_extents);
8214d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&device->debugfs_vol_resync_extents);
8224d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&device->debugfs_vol_data_gen_id);
823f5ec0173SLars Ellenberg 	drbd_debugfs_remove(&device->debugfs_vol_ed_gen_id);
8244d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&device->debugfs_vol);
8254d3d5aa8SLars Ellenberg }
8264d3d5aa8SLars Ellenberg 
drbd_debugfs_peer_device_add(struct drbd_peer_device * peer_device)8274d3d5aa8SLars Ellenberg void drbd_debugfs_peer_device_add(struct drbd_peer_device *peer_device)
8284d3d5aa8SLars Ellenberg {
8294d3d5aa8SLars Ellenberg 	struct dentry *conn_dir = peer_device->connection->debugfs_conn;
8304d3d5aa8SLars Ellenberg 	struct dentry *dentry;
8314d3d5aa8SLars Ellenberg 	char vnr_buf[8];
8324d3d5aa8SLars Ellenberg 
8334d3d5aa8SLars Ellenberg 	snprintf(vnr_buf, sizeof(vnr_buf), "%u", peer_device->device->vnr);
8344d3d5aa8SLars Ellenberg 	dentry = debugfs_create_dir(vnr_buf, conn_dir);
8354d3d5aa8SLars Ellenberg 	peer_device->debugfs_peer_dev = dentry;
8364d3d5aa8SLars Ellenberg }
8374d3d5aa8SLars Ellenberg 
drbd_debugfs_peer_device_cleanup(struct drbd_peer_device * peer_device)8384d3d5aa8SLars Ellenberg void drbd_debugfs_peer_device_cleanup(struct drbd_peer_device *peer_device)
8394d3d5aa8SLars Ellenberg {
8404d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&peer_device->debugfs_peer_dev);
8414d3d5aa8SLars Ellenberg }
8424d3d5aa8SLars Ellenberg 
drbd_version_show(struct seq_file * m,void * ignored)843b44e1184SLars Ellenberg static int drbd_version_show(struct seq_file *m, void *ignored)
844b44e1184SLars Ellenberg {
845b44e1184SLars Ellenberg 	seq_printf(m, "# %s\n", drbd_buildtag());
846b44e1184SLars Ellenberg 	seq_printf(m, "VERSION=%s\n", REL_VERSION);
847*4e2da933SChristoph Böhmwalder 	seq_printf(m, "API_VERSION=%u\n", GENL_MAGIC_VERSION);
848b44e1184SLars Ellenberg 	seq_printf(m, "PRO_VERSION_MIN=%u\n", PRO_VERSION_MIN);
849b44e1184SLars Ellenberg 	seq_printf(m, "PRO_VERSION_MAX=%u\n", PRO_VERSION_MAX);
850b44e1184SLars Ellenberg 	return 0;
851b44e1184SLars Ellenberg }
852b44e1184SLars Ellenberg 
drbd_version_open(struct inode * inode,struct file * file)853b44e1184SLars Ellenberg static int drbd_version_open(struct inode *inode, struct file *file)
854b44e1184SLars Ellenberg {
855b44e1184SLars Ellenberg 	return single_open(file, drbd_version_show, NULL);
856b44e1184SLars Ellenberg }
857b44e1184SLars Ellenberg 
8587e5fec31SFabian Frederick static const struct file_operations drbd_version_fops = {
859b44e1184SLars Ellenberg 	.owner = THIS_MODULE,
860b44e1184SLars Ellenberg 	.open = drbd_version_open,
861b44e1184SLars Ellenberg 	.llseek = seq_lseek,
862b44e1184SLars Ellenberg 	.read = seq_read,
863b44e1184SLars Ellenberg 	.release = single_release,
864b44e1184SLars Ellenberg };
865b44e1184SLars Ellenberg 
8664d3d5aa8SLars Ellenberg /* not __exit, may be indirectly called
8674d3d5aa8SLars Ellenberg  * from the module-load-failure path as well. */
drbd_debugfs_cleanup(void)8684d3d5aa8SLars Ellenberg void drbd_debugfs_cleanup(void)
8694d3d5aa8SLars Ellenberg {
8704d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&drbd_debugfs_resources);
8714d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&drbd_debugfs_minors);
872b44e1184SLars Ellenberg 	drbd_debugfs_remove(&drbd_debugfs_version);
8734d3d5aa8SLars Ellenberg 	drbd_debugfs_remove(&drbd_debugfs_root);
8744d3d5aa8SLars Ellenberg }
8754d3d5aa8SLars Ellenberg 
drbd_debugfs_init(void)876d27e84a3SGreg Kroah-Hartman void __init drbd_debugfs_init(void)
8774d3d5aa8SLars Ellenberg {
8784d3d5aa8SLars Ellenberg 	struct dentry *dentry;
8794d3d5aa8SLars Ellenberg 
8804d3d5aa8SLars Ellenberg 	dentry = debugfs_create_dir("drbd", NULL);
8814d3d5aa8SLars Ellenberg 	drbd_debugfs_root = dentry;
8824d3d5aa8SLars Ellenberg 
883b44e1184SLars Ellenberg 	dentry = debugfs_create_file("version", 0444, drbd_debugfs_root, NULL, &drbd_version_fops);
884b44e1184SLars Ellenberg 	drbd_debugfs_version = dentry;
885b44e1184SLars Ellenberg 
8864d3d5aa8SLars Ellenberg 	dentry = debugfs_create_dir("resources", drbd_debugfs_root);
8874d3d5aa8SLars Ellenberg 	drbd_debugfs_resources = dentry;
8884d3d5aa8SLars Ellenberg 
8894d3d5aa8SLars Ellenberg 	dentry = debugfs_create_dir("minors", drbd_debugfs_root);
8904d3d5aa8SLars Ellenberg 	drbd_debugfs_minors = dentry;
8914d3d5aa8SLars Ellenberg }
892