1 // SPDX-License-Identifier: GPL-2.0
3 * debugfs interface for sunrpc
5 * (c) 2014 Jeff Layton <jlayton@primarydata.com>
8 #include <linux/debugfs.h>
9 #include <linux/sunrpc/sched.h>
10 #include <linux/sunrpc/clnt.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;
21 tasks_show(struct seq_file *f, void *v)
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);
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, rpc_task_timeout(task), task->tk_ops,
37 clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
38 task->tk_action, rpc_waitq);
43 tasks_start(struct seq_file *f, loff_t *ppos)
44 __acquires(&clnt->cl_lock)
46 struct rpc_clnt *clnt = f->private;
48 struct rpc_task *task;
50 spin_lock(&clnt->cl_lock);
51 list_for_each_entry(task, &clnt->cl_tasks, tk_task)
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;
66 /* If there's another task on list, return it */
67 if (next == &clnt->cl_tasks)
69 return list_entry(next, struct rpc_task, tk_task);
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 = {
87 static int tasks_open(struct inode *inode, struct file *filp)
89 int ret = seq_open(filp, &tasks_seq_operations);
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);
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,
118 .release = tasks_release,
121 static int do_xprt_debugfs(struct rpc_clnt *clnt, struct rpc_xprt *xprt, void *numv)
124 char name[24]; /* enough for "../../rpc_xprt/ + 8 hex digits + NULL */
125 char link[9]; /* enough for 8 hex digits + NULL */
128 if (IS_ERR_OR_NULL(xprt->debugfs))
130 len = snprintf(name, sizeof(name), "../../rpc_xprt/%s",
131 xprt->debugfs->d_name.name);
132 if (len > sizeof(name))
135 strcpy(link, "xprt");
137 len = snprintf(link, sizeof(link), "xprt%d", *nump);
138 if (len > sizeof(link))
141 if (!debugfs_create_symlink(link, clnt->cl_debugfs, name))
148 rpc_clnt_debugfs_register(struct rpc_clnt *clnt)
151 char name[9]; /* enough for 8 hex digits + NULL */
154 /* Already registered? */
155 if (clnt->cl_debugfs || !rpc_clnt_dir)
158 len = snprintf(name, sizeof(name), "%x", clnt->cl_clid);
159 if (len >= sizeof(name))
162 /* make the per-client dir */
163 clnt->cl_debugfs = debugfs_create_dir(name, rpc_clnt_dir);
164 if (!clnt->cl_debugfs)
167 /* make tasks file */
168 if (!debugfs_create_file("tasks", S_IFREG | 0400, clnt->cl_debugfs,
172 if (rpc_clnt_iterate_for_each_xprt(clnt, do_xprt_debugfs, &xprtnum) < 0)
177 debugfs_remove_recursive(clnt->cl_debugfs);
178 clnt->cl_debugfs = NULL;
182 rpc_clnt_debugfs_unregister(struct rpc_clnt *clnt)
184 debugfs_remove_recursive(clnt->cl_debugfs);
185 clnt->cl_debugfs = NULL;
189 xprt_info_show(struct seq_file *f, void *v)
191 struct rpc_xprt *xprt = f->private;
193 seq_printf(f, "netid: %s\n", xprt->address_strings[RPC_DISPLAY_NETID]);
194 seq_printf(f, "addr: %s\n", xprt->address_strings[RPC_DISPLAY_ADDR]);
195 seq_printf(f, "port: %s\n", xprt->address_strings[RPC_DISPLAY_PORT]);
196 seq_printf(f, "state: 0x%lx\n", xprt->state);
201 xprt_info_open(struct inode *inode, struct file *filp)
204 struct rpc_xprt *xprt = inode->i_private;
206 ret = single_open(filp, xprt_info_show, xprt);
209 if (!xprt_get(xprt)) {
210 single_release(inode, filp);
218 xprt_info_release(struct inode *inode, struct file *filp)
220 struct rpc_xprt *xprt = inode->i_private;
223 return single_release(inode, filp);
226 static const struct file_operations xprt_info_fops = {
227 .owner = THIS_MODULE,
228 .open = xprt_info_open,
231 .release = xprt_info_release,
235 rpc_xprt_debugfs_register(struct rpc_xprt *xprt)
238 static atomic_t cur_id;
239 char name[9]; /* 8 hex digits + NULL term */
244 id = (unsigned int)atomic_inc_return(&cur_id);
246 len = snprintf(name, sizeof(name), "%x", id);
247 if (len >= sizeof(name))
250 /* make the per-client dir */
251 xprt->debugfs = debugfs_create_dir(name, rpc_xprt_dir);
255 /* make tasks file */
256 if (!debugfs_create_file("info", S_IFREG | 0400, xprt->debugfs,
257 xprt, &xprt_info_fops)) {
258 debugfs_remove_recursive(xprt->debugfs);
259 xprt->debugfs = NULL;
262 atomic_set(&xprt->inject_disconnect, rpc_inject_disconnect);
266 rpc_xprt_debugfs_unregister(struct rpc_xprt *xprt)
268 debugfs_remove_recursive(xprt->debugfs);
269 xprt->debugfs = NULL;
273 fault_open(struct inode *inode, struct file *filp)
275 filp->private_data = kmalloc(128, GFP_KERNEL);
276 if (!filp->private_data)
282 fault_release(struct inode *inode, struct file *filp)
284 kfree(filp->private_data);
289 fault_disconnect_read(struct file *filp, char __user *user_buf,
290 size_t len, loff_t *offset)
292 char *buffer = (char *)filp->private_data;
295 size = sprintf(buffer, "%u\n", rpc_inject_disconnect);
296 return simple_read_from_buffer(user_buf, len, offset, buffer, size);
300 fault_disconnect_write(struct file *filp, const char __user *user_buf,
301 size_t len, loff_t *offset)
305 if (len >= sizeof(buffer))
306 len = sizeof(buffer) - 1;
307 if (copy_from_user(buffer, user_buf, len))
310 if (kstrtouint(buffer, 10, &rpc_inject_disconnect))
315 static const struct file_operations fault_disconnect_fops = {
316 .owner = THIS_MODULE,
318 .read = fault_disconnect_read,
319 .write = fault_disconnect_write,
320 .release = fault_release,
323 static struct dentry *
324 inject_fault_dir(struct dentry *topdir)
326 struct dentry *faultdir;
328 faultdir = debugfs_create_dir("inject_fault", topdir);
332 if (!debugfs_create_file("disconnect", S_IFREG | 0400, faultdir,
333 NULL, &fault_disconnect_fops))
340 sunrpc_debugfs_exit(void)
342 debugfs_remove_recursive(topdir);
344 rpc_fault_dir = NULL;
350 sunrpc_debugfs_init(void)
352 topdir = debugfs_create_dir("sunrpc", NULL);
356 rpc_fault_dir = inject_fault_dir(topdir);
360 rpc_clnt_dir = debugfs_create_dir("rpc_clnt", topdir);
364 rpc_xprt_dir = debugfs_create_dir("rpc_xprt", topdir);
370 debugfs_remove_recursive(topdir);
372 rpc_fault_dir = NULL;