Linux 4.19.133
[linux/fpc-iii.git] / net / sunrpc / debugfs.c
blob45a033329cd4a7156c41509a255e1ca1d969f2df
1 // SPDX-License-Identifier: GPL-2.0
2 /**
3 * debugfs interface for sunrpc
5 * (c) 2014 Jeff Layton <jlayton@primarydata.com>
6 */
8 #include <linux/debugfs.h>
9 #include <linux/sunrpc/sched.h>
10 #include <linux/sunrpc/clnt.h>
11 #include "netns.h"
13 static struct dentry *topdir;
14 static struct dentry *rpc_fault_dir;
15 static struct dentry *rpc_clnt_dir;
16 static struct dentry *rpc_xprt_dir;
18 unsigned int rpc_inject_disconnect;
20 static int
21 tasks_show(struct seq_file *f, void *v)
23 u32 xid = 0;
24 struct rpc_task *task = v;
25 struct rpc_clnt *clnt = task->tk_client;
26 const char *rpc_waitq = "none";
28 if (RPC_IS_QUEUED(task))
29 rpc_waitq = rpc_qname(task->tk_waitqueue);
31 if (task->tk_rqstp)
32 xid = be32_to_cpu(task->tk_rqstp->rq_xid);
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;
42 static void *
43 tasks_start(struct seq_file *f, loff_t *ppos)
44 __acquires(&clnt->cl_lock)
46 struct rpc_clnt *clnt = f->private;
47 loff_t pos = *ppos;
48 struct rpc_task *task;
50 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;
57 static void *
58 tasks_next(struct seq_file *f, void *v, loff_t *pos)
60 struct rpc_clnt *clnt = f->private;
61 struct rpc_task *task = v;
62 struct list_head *next = task->tk_task.next;
64 ++*pos;
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);
72 static void
73 tasks_stop(struct seq_file *f, void *v)
74 __releases(&clnt->cl_lock)
76 struct rpc_clnt *clnt = f->private;
77 spin_unlock(&clnt->cl_lock);
80 static const struct seq_operations tasks_seq_operations = {
81 .start = tasks_start,
82 .next = tasks_next,
83 .stop = tasks_stop,
84 .show = tasks_show,
87 static int tasks_open(struct inode *inode, struct file *filp)
89 int ret = seq_open(filp, &tasks_seq_operations);
90 if (!ret) {
91 struct seq_file *seq = filp->private_data;
92 struct rpc_clnt *clnt = seq->private = inode->i_private;
94 if (!atomic_inc_not_zero(&clnt->cl_count)) {
95 seq_release(inode, filp);
96 ret = -EINVAL;
100 return ret;
103 static int
104 tasks_release(struct inode *inode, struct file *filp)
106 struct seq_file *seq = filp->private_data;
107 struct rpc_clnt *clnt = seq->private;
109 rpc_release_client(clnt);
110 return seq_release(inode, filp);
113 static 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,
121 void
122 rpc_clnt_debugfs_register(struct rpc_clnt *clnt)
124 int len;
125 char name[24]; /* enough for "../../rpc_xprt/ + 8 hex digits + NULL */
126 struct rpc_xprt *xprt;
128 /* Already registered? */
129 if (clnt->cl_debugfs || !rpc_clnt_dir)
130 return;
132 len = snprintf(name, sizeof(name), "%x", clnt->cl_clid);
133 if (len >= sizeof(name))
134 return;
136 /* make the per-client dir */
137 clnt->cl_debugfs = debugfs_create_dir(name, rpc_clnt_dir);
138 if (!clnt->cl_debugfs)
139 return;
141 /* make tasks file */
142 if (!debugfs_create_file("tasks", S_IFREG | 0400, clnt->cl_debugfs,
143 clnt, &tasks_fops))
144 goto out_err;
146 rcu_read_lock();
147 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;
153 len = snprintf(name, sizeof(name), "../../rpc_xprt/%s",
154 xprt->debugfs->d_name.name);
155 rcu_read_unlock();
157 if (len >= sizeof(name))
158 goto out_err;
160 if (!debugfs_create_symlink("xprt", clnt->cl_debugfs, name))
161 goto out_err;
163 return;
164 out_err:
165 debugfs_remove_recursive(clnt->cl_debugfs);
166 clnt->cl_debugfs = NULL;
169 void
170 rpc_clnt_debugfs_unregister(struct rpc_clnt *clnt)
172 debugfs_remove_recursive(clnt->cl_debugfs);
173 clnt->cl_debugfs = NULL;
176 static int
177 xprt_info_show(struct seq_file *f, void *v)
179 struct rpc_xprt *xprt = f->private;
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;
188 static int
189 xprt_info_open(struct inode *inode, struct file *filp)
191 int ret;
192 struct rpc_xprt *xprt = inode->i_private;
194 ret = single_open(filp, xprt_info_show, xprt);
196 if (!ret) {
197 if (!xprt_get(xprt)) {
198 single_release(inode, filp);
199 ret = -EINVAL;
202 return ret;
205 static int
206 xprt_info_release(struct inode *inode, struct file *filp)
208 struct rpc_xprt *xprt = inode->i_private;
210 xprt_put(xprt);
211 return single_release(inode, filp);
214 static 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,
222 void
223 rpc_xprt_debugfs_register(struct rpc_xprt *xprt)
225 int len, id;
226 static atomic_t cur_id;
227 char name[9]; /* 8 hex digits + NULL term */
229 if (!rpc_xprt_dir)
230 return;
232 id = (unsigned int)atomic_inc_return(&cur_id);
234 len = snprintf(name, sizeof(name), "%x", id);
235 if (len >= sizeof(name))
236 return;
238 /* make the per-client dir */
239 xprt->debugfs = debugfs_create_dir(name, rpc_xprt_dir);
240 if (!xprt->debugfs)
241 return;
243 /* make tasks file */
244 if (!debugfs_create_file("info", S_IFREG | 0400, xprt->debugfs,
245 xprt, &xprt_info_fops)) {
246 debugfs_remove_recursive(xprt->debugfs);
247 xprt->debugfs = NULL;
250 atomic_set(&xprt->inject_disconnect, rpc_inject_disconnect);
253 void
254 rpc_xprt_debugfs_unregister(struct rpc_xprt *xprt)
256 debugfs_remove_recursive(xprt->debugfs);
257 xprt->debugfs = NULL;
260 static int
261 fault_open(struct inode *inode, struct file *filp)
263 filp->private_data = kmalloc(128, GFP_KERNEL);
264 if (!filp->private_data)
265 return -ENOMEM;
266 return 0;
269 static int
270 fault_release(struct inode *inode, struct file *filp)
272 kfree(filp->private_data);
273 return 0;
276 static ssize_t
277 fault_disconnect_read(struct file *filp, char __user *user_buf,
278 size_t len, loff_t *offset)
280 char *buffer = (char *)filp->private_data;
281 size_t size;
283 size = sprintf(buffer, "%u\n", rpc_inject_disconnect);
284 return simple_read_from_buffer(user_buf, len, offset, buffer, size);
287 static ssize_t
288 fault_disconnect_write(struct file *filp, const char __user *user_buf,
289 size_t len, loff_t *offset)
291 char buffer[16];
293 if (len >= sizeof(buffer))
294 len = sizeof(buffer) - 1;
295 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;
303 static 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,
311 static struct dentry *
312 inject_fault_dir(struct dentry *topdir)
314 struct dentry *faultdir;
316 faultdir = debugfs_create_dir("inject_fault", topdir);
317 if (!faultdir)
318 return NULL;
320 if (!debugfs_create_file("disconnect", S_IFREG | 0400, faultdir,
321 NULL, &fault_disconnect_fops))
322 return NULL;
324 return faultdir;
327 void __exit
328 sunrpc_debugfs_exit(void)
330 debugfs_remove_recursive(topdir);
331 topdir = NULL;
332 rpc_fault_dir = NULL;
333 rpc_clnt_dir = NULL;
334 rpc_xprt_dir = NULL;
337 void __init
338 sunrpc_debugfs_init(void)
340 topdir = debugfs_create_dir("sunrpc", NULL);
341 if (!topdir)
342 return;
344 rpc_fault_dir = inject_fault_dir(topdir);
345 if (!rpc_fault_dir)
346 goto out_remove;
348 rpc_clnt_dir = debugfs_create_dir("rpc_clnt", topdir);
349 if (!rpc_clnt_dir)
350 goto out_remove;
352 rpc_xprt_dir = debugfs_create_dir("rpc_xprt", topdir);
353 if (!rpc_xprt_dir)
354 goto out_remove;
356 return;
357 out_remove:
358 debugfs_remove_recursive(topdir);
359 topdir = NULL;
360 rpc_fault_dir = NULL;
361 rpc_clnt_dir = NULL;