blob: e980d2a493dec17dcee93db225c59bb7bad187a1 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -05002/**
3 * debugfs interface for sunrpc
4 *
5 * (c) 2014 Jeff Layton <jlayton@primarydata.com>
6 */
7
8#include <linux/debugfs.h>
9#include <linux/sunrpc/sched.h>
10#include <linux/sunrpc/clnt.h>
11#include "netns.h"
12
13static struct dentry *topdir;
Chuck Lever4a068252015-05-11 14:02:25 -040014static struct dentry *rpc_fault_dir;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -050015static struct dentry *rpc_clnt_dir;
Jeff Layton388f0c72014-11-26 14:44:44 -050016static struct dentry *rpc_xprt_dir;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -050017
Chuck Lever4a068252015-05-11 14:02:25 -040018unsigned int rpc_inject_disconnect;
19
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -050020static int
21tasks_show(struct seq_file *f, void *v)
22{
23 u32 xid = 0;
24 struct rpc_task *task = v;
25 struct rpc_clnt *clnt = task->tk_client;
26 const char *rpc_waitq = "none";
27
28 if (RPC_IS_QUEUED(task))
29 rpc_waitq = rpc_qname(task->tk_waitqueue);
30
31 if (task->tk_rqstp)
32 xid = be32_to_cpu(task->tk_rqstp->rq_xid);
33
34 seq_printf(f, "%5u %04x %6d 0x%x 0x%x %8ld %ps %sv%u %s a:%ps q:%s\n",
35 task->tk_pid, task->tk_flags, task->tk_status,
36 clnt->cl_clid, xid, task->tk_timeout, task->tk_ops,
37 clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
38 task->tk_action, rpc_waitq);
39 return 0;
40}
41
42static void *
43tasks_start(struct seq_file *f, loff_t *ppos)
44 __acquires(&clnt->cl_lock)
45{
Kinglong Mee3f373e82017-02-07 21:49:57 +080046 struct rpc_clnt *clnt = f->private;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -050047 loff_t pos = *ppos;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -050048 struct rpc_task *task;
49
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -050050 spin_lock(&clnt->cl_lock);
51 list_for_each_entry(task, &clnt->cl_tasks, tk_task)
52 if (pos-- == 0)
53 return task;
54 return NULL;
55}
56
57static void *
58tasks_next(struct seq_file *f, void *v, loff_t *pos)
59{
Kinglong Mee3f373e82017-02-07 21:49:57 +080060 struct rpc_clnt *clnt = f->private;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -050061 struct rpc_task *task = v;
62 struct list_head *next = task->tk_task.next;
63
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -050064 ++*pos;
65
66 /* If there's another task on list, return it */
67 if (next == &clnt->cl_tasks)
68 return NULL;
69 return list_entry(next, struct rpc_task, tk_task);
70}
71
72static void
73tasks_stop(struct seq_file *f, void *v)
74 __releases(&clnt->cl_lock)
75{
Kinglong Mee3f373e82017-02-07 21:49:57 +080076 struct rpc_clnt *clnt = f->private;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -050077 spin_unlock(&clnt->cl_lock);
78}
79
80static const struct seq_operations tasks_seq_operations = {
81 .start = tasks_start,
82 .next = tasks_next,
83 .stop = tasks_stop,
84 .show = tasks_show,
85};
86
87static int tasks_open(struct inode *inode, struct file *filp)
88{
Kinglong Mee3f373e82017-02-07 21:49:57 +080089 int ret = seq_open(filp, &tasks_seq_operations);
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -050090 if (!ret) {
91 struct seq_file *seq = filp->private_data;
Kinglong Mee3f373e82017-02-07 21:49:57 +080092 struct rpc_clnt *clnt = seq->private = inode->i_private;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -050093
Kinglong Mee3f373e82017-02-07 21:49:57 +080094 if (!atomic_inc_not_zero(&clnt->cl_count)) {
95 seq_release(inode, filp);
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -050096 ret = -EINVAL;
97 }
98 }
99
100 return ret;
101}
102
103static int
104tasks_release(struct inode *inode, struct file *filp)
105{
106 struct seq_file *seq = filp->private_data;
Kinglong Mee3f373e82017-02-07 21:49:57 +0800107 struct rpc_clnt *clnt = seq->private;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500108
Kinglong Mee3f373e82017-02-07 21:49:57 +0800109 rpc_release_client(clnt);
110 return seq_release(inode, filp);
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500111}
112
113static const struct file_operations tasks_fops = {
114 .owner = THIS_MODULE,
115 .open = tasks_open,
116 .read = seq_read,
117 .llseek = seq_lseek,
118 .release = tasks_release,
119};
120
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400121void
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500122rpc_clnt_debugfs_register(struct rpc_clnt *clnt)
123{
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400124 int len;
Jeff Layton388f0c72014-11-26 14:44:44 -0500125 char name[24]; /* enough for "../../rpc_xprt/ + 8 hex digits + NULL */
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400126 struct rpc_xprt *xprt;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500127
128 /* Already registered? */
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400129 if (clnt->cl_debugfs || !rpc_clnt_dir)
130 return;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500131
132 len = snprintf(name, sizeof(name), "%x", clnt->cl_clid);
133 if (len >= sizeof(name))
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400134 return;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500135
136 /* make the per-client dir */
137 clnt->cl_debugfs = debugfs_create_dir(name, rpc_clnt_dir);
138 if (!clnt->cl_debugfs)
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400139 return;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500140
141 /* make tasks file */
142 if (!debugfs_create_file("tasks", S_IFREG | S_IRUSR, clnt->cl_debugfs,
Jeff Layton388f0c72014-11-26 14:44:44 -0500143 clnt, &tasks_fops))
144 goto out_err;
145
Jeff Layton388f0c72014-11-26 14:44:44 -0500146 rcu_read_lock();
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400147 xprt = rcu_dereference(clnt->cl_xprt);
148 /* no "debugfs" dentry? Don't bother with the symlink. */
149 if (!xprt->debugfs) {
150 rcu_read_unlock();
151 return;
152 }
Jeff Layton388f0c72014-11-26 14:44:44 -0500153 len = snprintf(name, sizeof(name), "../../rpc_xprt/%s",
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400154 xprt->debugfs->d_name.name);
Jeff Layton388f0c72014-11-26 14:44:44 -0500155 rcu_read_unlock();
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400156
Jeff Layton388f0c72014-11-26 14:44:44 -0500157 if (len >= sizeof(name))
158 goto out_err;
159
Jeff Layton388f0c72014-11-26 14:44:44 -0500160 if (!debugfs_create_symlink("xprt", clnt->cl_debugfs, name))
161 goto out_err;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500162
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400163 return;
Jeff Layton388f0c72014-11-26 14:44:44 -0500164out_err:
165 debugfs_remove_recursive(clnt->cl_debugfs);
166 clnt->cl_debugfs = NULL;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500167}
168
169void
170rpc_clnt_debugfs_unregister(struct rpc_clnt *clnt)
171{
172 debugfs_remove_recursive(clnt->cl_debugfs);
173 clnt->cl_debugfs = NULL;
174}
175
Jeff Layton388f0c72014-11-26 14:44:44 -0500176static int
177xprt_info_show(struct seq_file *f, void *v)
178{
179 struct rpc_xprt *xprt = f->private;
180
181 seq_printf(f, "netid: %s\n", xprt->address_strings[RPC_DISPLAY_NETID]);
182 seq_printf(f, "addr: %s\n", xprt->address_strings[RPC_DISPLAY_ADDR]);
183 seq_printf(f, "port: %s\n", xprt->address_strings[RPC_DISPLAY_PORT]);
184 seq_printf(f, "state: 0x%lx\n", xprt->state);
185 return 0;
186}
187
188static int
189xprt_info_open(struct inode *inode, struct file *filp)
190{
191 int ret;
192 struct rpc_xprt *xprt = inode->i_private;
193
194 ret = single_open(filp, xprt_info_show, xprt);
195
196 if (!ret) {
197 if (!xprt_get(xprt)) {
198 single_release(inode, filp);
199 ret = -EINVAL;
200 }
201 }
202 return ret;
203}
204
205static int
206xprt_info_release(struct inode *inode, struct file *filp)
207{
208 struct rpc_xprt *xprt = inode->i_private;
209
210 xprt_put(xprt);
211 return single_release(inode, filp);
212}
213
214static const struct file_operations xprt_info_fops = {
215 .owner = THIS_MODULE,
216 .open = xprt_info_open,
217 .read = seq_read,
218 .llseek = seq_lseek,
219 .release = xprt_info_release,
220};
221
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400222void
Jeff Layton388f0c72014-11-26 14:44:44 -0500223rpc_xprt_debugfs_register(struct rpc_xprt *xprt)
224{
225 int len, id;
226 static atomic_t cur_id;
227 char name[9]; /* 8 hex digits + NULL term */
228
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400229 if (!rpc_xprt_dir)
230 return;
231
Jeff Layton388f0c72014-11-26 14:44:44 -0500232 id = (unsigned int)atomic_inc_return(&cur_id);
233
234 len = snprintf(name, sizeof(name), "%x", id);
235 if (len >= sizeof(name))
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400236 return;
Jeff Layton388f0c72014-11-26 14:44:44 -0500237
238 /* make the per-client dir */
239 xprt->debugfs = debugfs_create_dir(name, rpc_xprt_dir);
240 if (!xprt->debugfs)
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400241 return;
Jeff Layton388f0c72014-11-26 14:44:44 -0500242
243 /* make tasks file */
244 if (!debugfs_create_file("info", S_IFREG | S_IRUSR, xprt->debugfs,
245 xprt, &xprt_info_fops)) {
246 debugfs_remove_recursive(xprt->debugfs);
247 xprt->debugfs = NULL;
Jeff Layton388f0c72014-11-26 14:44:44 -0500248 }
Chuck Lever4a068252015-05-11 14:02:25 -0400249
250 atomic_set(&xprt->inject_disconnect, rpc_inject_disconnect);
Jeff Layton388f0c72014-11-26 14:44:44 -0500251}
252
253void
254rpc_xprt_debugfs_unregister(struct rpc_xprt *xprt)
255{
256 debugfs_remove_recursive(xprt->debugfs);
257 xprt->debugfs = NULL;
258}
259
Chuck Lever4a068252015-05-11 14:02:25 -0400260static int
261fault_open(struct inode *inode, struct file *filp)
262{
263 filp->private_data = kmalloc(128, GFP_KERNEL);
264 if (!filp->private_data)
265 return -ENOMEM;
266 return 0;
267}
268
269static int
270fault_release(struct inode *inode, struct file *filp)
271{
272 kfree(filp->private_data);
273 return 0;
274}
275
276static ssize_t
277fault_disconnect_read(struct file *filp, char __user *user_buf,
278 size_t len, loff_t *offset)
279{
280 char *buffer = (char *)filp->private_data;
281 size_t size;
282
283 size = sprintf(buffer, "%u\n", rpc_inject_disconnect);
284 return simple_read_from_buffer(user_buf, len, offset, buffer, size);
285}
286
287static ssize_t
288fault_disconnect_write(struct file *filp, const char __user *user_buf,
289 size_t len, loff_t *offset)
290{
291 char buffer[16];
292
Chuck Lever5fd23f72015-06-11 13:47:10 -0400293 if (len >= sizeof(buffer))
294 len = sizeof(buffer) - 1;
Chuck Lever4a068252015-05-11 14:02:25 -0400295 if (copy_from_user(buffer, user_buf, len))
296 return -EFAULT;
297 buffer[len] = '\0';
298 if (kstrtouint(buffer, 10, &rpc_inject_disconnect))
299 return -EINVAL;
300 return len;
301}
302
303static const struct file_operations fault_disconnect_fops = {
304 .owner = THIS_MODULE,
305 .open = fault_open,
306 .read = fault_disconnect_read,
307 .write = fault_disconnect_write,
308 .release = fault_release,
309};
310
311static struct dentry *
312inject_fault_dir(struct dentry *topdir)
313{
314 struct dentry *faultdir;
315
316 faultdir = debugfs_create_dir("inject_fault", topdir);
317 if (!faultdir)
318 return NULL;
319
320 if (!debugfs_create_file("disconnect", S_IFREG | S_IRUSR, faultdir,
321 NULL, &fault_disconnect_fops))
322 return NULL;
323
324 return faultdir;
325}
326
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500327void __exit
328sunrpc_debugfs_exit(void)
329{
330 debugfs_remove_recursive(topdir);
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400331 topdir = NULL;
Chuck Lever4a068252015-05-11 14:02:25 -0400332 rpc_fault_dir = NULL;
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400333 rpc_clnt_dir = NULL;
334 rpc_xprt_dir = NULL;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500335}
336
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400337void __init
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500338sunrpc_debugfs_init(void)
339{
340 topdir = debugfs_create_dir("sunrpc", NULL);
341 if (!topdir)
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400342 return;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500343
Chuck Lever4a068252015-05-11 14:02:25 -0400344 rpc_fault_dir = inject_fault_dir(topdir);
345 if (!rpc_fault_dir)
346 goto out_remove;
347
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500348 rpc_clnt_dir = debugfs_create_dir("rpc_clnt", topdir);
349 if (!rpc_clnt_dir)
350 goto out_remove;
351
Jeff Layton388f0c72014-11-26 14:44:44 -0500352 rpc_xprt_dir = debugfs_create_dir("rpc_xprt", topdir);
353 if (!rpc_xprt_dir)
354 goto out_remove;
355
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400356 return;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500357out_remove:
358 debugfs_remove_recursive(topdir);
359 topdir = NULL;
Chuck Lever4a068252015-05-11 14:02:25 -0400360 rpc_fault_dir = NULL;
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400361 rpc_clnt_dir = NULL;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500362}