blob: 7dc9cc929bfd7185b1c73bd766f02cb34c80b64b [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Trond Myklebustb6580ab2019-05-30 11:24:26 -04002/*
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -05003 * 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>
Chuck Leverc782af22021-08-03 15:45:18 -040011
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -050012#include "netns.h"
Chuck Leverc782af22021-08-03 15:45:18 -040013#include "fail.h"
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -050014
15static struct dentry *topdir;
16static struct dentry *rpc_clnt_dir;
Jeff Layton388f0c72014-11-26 14:44:44 -050017static struct dentry *rpc_xprt_dir;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -050018
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -050019static int
20tasks_show(struct seq_file *f, void *v)
21{
22 u32 xid = 0;
23 struct rpc_task *task = v;
24 struct rpc_clnt *clnt = task->tk_client;
25 const char *rpc_waitq = "none";
26
27 if (RPC_IS_QUEUED(task))
28 rpc_waitq = rpc_qname(task->tk_waitqueue);
29
30 if (task->tk_rqstp)
31 xid = be32_to_cpu(task->tk_rqstp->rq_xid);
32
33 seq_printf(f, "%5u %04x %6d 0x%x 0x%x %8ld %ps %sv%u %s a:%ps q:%s\n",
34 task->tk_pid, task->tk_flags, task->tk_status,
Trond Myklebust5efd1872019-04-07 13:58:50 -040035 clnt->cl_clid, xid, rpc_task_timeout(task), task->tk_ops,
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -050036 clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
37 task->tk_action, rpc_waitq);
38 return 0;
39}
40
41static void *
42tasks_start(struct seq_file *f, loff_t *ppos)
43 __acquires(&clnt->cl_lock)
44{
Kinglong Mee3f373e82017-02-07 21:49:57 +080045 struct rpc_clnt *clnt = f->private;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -050046 loff_t pos = *ppos;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -050047 struct rpc_task *task;
48
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -050049 spin_lock(&clnt->cl_lock);
50 list_for_each_entry(task, &clnt->cl_tasks, tk_task)
51 if (pos-- == 0)
52 return task;
53 return NULL;
54}
55
56static void *
57tasks_next(struct seq_file *f, void *v, loff_t *pos)
58{
Kinglong Mee3f373e82017-02-07 21:49:57 +080059 struct rpc_clnt *clnt = f->private;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -050060 struct rpc_task *task = v;
61 struct list_head *next = task->tk_task.next;
62
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -050063 ++*pos;
64
65 /* If there's another task on list, return it */
66 if (next == &clnt->cl_tasks)
67 return NULL;
68 return list_entry(next, struct rpc_task, tk_task);
69}
70
71static void
72tasks_stop(struct seq_file *f, void *v)
73 __releases(&clnt->cl_lock)
74{
Kinglong Mee3f373e82017-02-07 21:49:57 +080075 struct rpc_clnt *clnt = f->private;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -050076 spin_unlock(&clnt->cl_lock);
77}
78
79static const struct seq_operations tasks_seq_operations = {
80 .start = tasks_start,
81 .next = tasks_next,
82 .stop = tasks_stop,
83 .show = tasks_show,
84};
85
86static int tasks_open(struct inode *inode, struct file *filp)
87{
Kinglong Mee3f373e82017-02-07 21:49:57 +080088 int ret = seq_open(filp, &tasks_seq_operations);
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -050089 if (!ret) {
90 struct seq_file *seq = filp->private_data;
Kinglong Mee3f373e82017-02-07 21:49:57 +080091 struct rpc_clnt *clnt = seq->private = inode->i_private;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -050092
Trond Myklebust71d3d0e2021-07-26 08:01:27 -040093 if (!refcount_inc_not_zero(&clnt->cl_count)) {
Kinglong Mee3f373e82017-02-07 21:49:57 +080094 seq_release(inode, filp);
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -050095 ret = -EINVAL;
96 }
97 }
98
99 return ret;
100}
101
102static int
103tasks_release(struct inode *inode, struct file *filp)
104{
105 struct seq_file *seq = filp->private_data;
Kinglong Mee3f373e82017-02-07 21:49:57 +0800106 struct rpc_clnt *clnt = seq->private;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500107
Kinglong Mee3f373e82017-02-07 21:49:57 +0800108 rpc_release_client(clnt);
109 return seq_release(inode, filp);
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500110}
111
112static const struct file_operations tasks_fops = {
113 .owner = THIS_MODULE,
114 .open = tasks_open,
115 .read = seq_read,
116 .llseek = seq_lseek,
117 .release = tasks_release,
118};
119
NeilBrown2f34b8b2019-05-30 10:41:28 +1000120static int do_xprt_debugfs(struct rpc_clnt *clnt, struct rpc_xprt *xprt, void *numv)
121{
122 int len;
123 char name[24]; /* enough for "../../rpc_xprt/ + 8 hex digits + NULL */
124 char link[9]; /* enough for 8 hex digits + NULL */
125 int *nump = numv;
126
127 if (IS_ERR_OR_NULL(xprt->debugfs))
128 return 0;
129 len = snprintf(name, sizeof(name), "../../rpc_xprt/%s",
130 xprt->debugfs->d_name.name);
Fedor Tokarev35a6d392020-10-15 16:59:08 +0300131 if (len >= sizeof(name))
NeilBrown2f34b8b2019-05-30 10:41:28 +1000132 return -1;
133 if (*nump == 0)
134 strcpy(link, "xprt");
135 else {
136 len = snprintf(link, sizeof(link), "xprt%d", *nump);
Fedor Tokarev35a6d392020-10-15 16:59:08 +0300137 if (len >= sizeof(link))
NeilBrown2f34b8b2019-05-30 10:41:28 +1000138 return -1;
139 }
Linus Torvalds6860c982019-07-18 14:32:33 -0700140 debugfs_create_symlink(link, clnt->cl_debugfs, name);
NeilBrown2f34b8b2019-05-30 10:41:28 +1000141 (*nump)++;
142 return 0;
143}
144
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400145void
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500146rpc_clnt_debugfs_register(struct rpc_clnt *clnt)
147{
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400148 int len;
NeilBrown2f34b8b2019-05-30 10:41:28 +1000149 char name[9]; /* enough for 8 hex digits + NULL */
150 int xprtnum = 0;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500151
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500152 len = snprintf(name, sizeof(name), "%x", clnt->cl_clid);
153 if (len >= sizeof(name))
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400154 return;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500155
156 /* make the per-client dir */
157 clnt->cl_debugfs = debugfs_create_dir(name, rpc_clnt_dir);
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500158
159 /* make tasks file */
Greg Kroah-Hartman0a0762c2019-06-12 16:56:22 +0200160 debugfs_create_file("tasks", S_IFREG | 0400, clnt->cl_debugfs, clnt,
161 &tasks_fops);
Jeff Layton388f0c72014-11-26 14:44:44 -0500162
Linus Torvalds6860c982019-07-18 14:32:33 -0700163 rpc_clnt_iterate_for_each_xprt(clnt, do_xprt_debugfs, &xprtnum);
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500164}
165
166void
167rpc_clnt_debugfs_unregister(struct rpc_clnt *clnt)
168{
169 debugfs_remove_recursive(clnt->cl_debugfs);
170 clnt->cl_debugfs = NULL;
171}
172
Jeff Layton388f0c72014-11-26 14:44:44 -0500173static int
174xprt_info_show(struct seq_file *f, void *v)
175{
176 struct rpc_xprt *xprt = f->private;
177
178 seq_printf(f, "netid: %s\n", xprt->address_strings[RPC_DISPLAY_NETID]);
179 seq_printf(f, "addr: %s\n", xprt->address_strings[RPC_DISPLAY_ADDR]);
180 seq_printf(f, "port: %s\n", xprt->address_strings[RPC_DISPLAY_PORT]);
181 seq_printf(f, "state: 0x%lx\n", xprt->state);
182 return 0;
183}
184
185static int
186xprt_info_open(struct inode *inode, struct file *filp)
187{
188 int ret;
189 struct rpc_xprt *xprt = inode->i_private;
190
191 ret = single_open(filp, xprt_info_show, xprt);
192
193 if (!ret) {
194 if (!xprt_get(xprt)) {
195 single_release(inode, filp);
196 ret = -EINVAL;
197 }
198 }
199 return ret;
200}
201
202static int
203xprt_info_release(struct inode *inode, struct file *filp)
204{
205 struct rpc_xprt *xprt = inode->i_private;
206
207 xprt_put(xprt);
208 return single_release(inode, filp);
209}
210
211static const struct file_operations xprt_info_fops = {
212 .owner = THIS_MODULE,
213 .open = xprt_info_open,
214 .read = seq_read,
215 .llseek = seq_lseek,
216 .release = xprt_info_release,
217};
218
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400219void
Jeff Layton388f0c72014-11-26 14:44:44 -0500220rpc_xprt_debugfs_register(struct rpc_xprt *xprt)
221{
222 int len, id;
223 static atomic_t cur_id;
224 char name[9]; /* 8 hex digits + NULL term */
225
226 id = (unsigned int)atomic_inc_return(&cur_id);
227
228 len = snprintf(name, sizeof(name), "%x", id);
229 if (len >= sizeof(name))
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400230 return;
Jeff Layton388f0c72014-11-26 14:44:44 -0500231
232 /* make the per-client dir */
233 xprt->debugfs = debugfs_create_dir(name, rpc_xprt_dir);
Jeff Layton388f0c72014-11-26 14:44:44 -0500234
235 /* make tasks file */
Greg Kroah-Hartman0a0762c2019-06-12 16:56:22 +0200236 debugfs_create_file("info", S_IFREG | 0400, xprt->debugfs, xprt,
237 &xprt_info_fops);
Jeff Layton388f0c72014-11-26 14:44:44 -0500238}
239
240void
241rpc_xprt_debugfs_unregister(struct rpc_xprt *xprt)
242{
243 debugfs_remove_recursive(xprt->debugfs);
244 xprt->debugfs = NULL;
245}
246
Chuck Leverc782af22021-08-03 15:45:18 -0400247#if IS_ENABLED(CONFIG_FAIL_SUNRPC)
248struct fail_sunrpc_attr fail_sunrpc = {
249 .attr = FAULT_ATTR_INITIALIZER,
250};
251EXPORT_SYMBOL_GPL(fail_sunrpc);
Chuck Levera4ae3082021-08-05 10:25:49 -0400252
253static void fail_sunrpc_init(void)
254{
255 struct dentry *dir;
256
257 dir = fault_create_debugfs_attr("fail_sunrpc", NULL,
258 &fail_sunrpc.attr);
259
260 debugfs_create_bool("ignore-client-disconnect", S_IFREG | 0600, dir,
261 &fail_sunrpc.ignore_client_disconnect);
Chuck Lever3a126182021-08-03 15:55:58 -0400262
263 debugfs_create_bool("ignore-server-disconnect", S_IFREG | 0600, dir,
264 &fail_sunrpc.ignore_server_disconnect);
Chuck Levera4ae3082021-08-05 10:25:49 -0400265}
266#else
267static void fail_sunrpc_init(void)
268{
269}
Chuck Leverc782af22021-08-03 15:45:18 -0400270#endif
271
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500272void __exit
273sunrpc_debugfs_exit(void)
274{
275 debugfs_remove_recursive(topdir);
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400276 topdir = NULL;
277 rpc_clnt_dir = NULL;
278 rpc_xprt_dir = NULL;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500279}
280
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400281void __init
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500282sunrpc_debugfs_init(void)
283{
Greg Kroah-Hartman0a0762c2019-06-12 16:56:22 +0200284 topdir = debugfs_create_dir("sunrpc", NULL);
Chuck Lever4a068252015-05-11 14:02:25 -0400285
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500286 rpc_clnt_dir = debugfs_create_dir("rpc_clnt", topdir);
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500287
Jeff Layton388f0c72014-11-26 14:44:44 -0500288 rpc_xprt_dir = debugfs_create_dir("rpc_xprt", topdir);
Jeff Layton388f0c72014-11-26 14:44:44 -0500289
Chuck Levera4ae3082021-08-05 10:25:49 -0400290 fail_sunrpc_init();
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500291}