2 * debugfs interface for sunrpc
4 * (c) 2014 Jeff Layton <jlayton@primarydata.com>
7 #include <linux/debugfs.h>
8 #include <linux/sunrpc/sched.h>
9 #include <linux/sunrpc/clnt.h>
12 static struct dentry
*topdir
;
13 static struct dentry
*rpc_fault_dir
;
14 static struct dentry
*rpc_clnt_dir
;
15 static struct dentry
*rpc_xprt_dir
;
17 unsigned int rpc_inject_disconnect
;
19 struct rpc_clnt_iter
{
20 struct rpc_clnt
*clnt
;
25 tasks_show(struct seq_file
*f
, void *v
)
28 struct rpc_task
*task
= v
;
29 struct rpc_clnt
*clnt
= task
->tk_client
;
30 const char *rpc_waitq
= "none";
32 if (RPC_IS_QUEUED(task
))
33 rpc_waitq
= rpc_qname(task
->tk_waitqueue
);
36 xid
= be32_to_cpu(task
->tk_rqstp
->rq_xid
);
38 seq_printf(f
, "%5u %04x %6d 0x%x 0x%x %8ld %ps %sv%u %s a:%ps q:%s\n",
39 task
->tk_pid
, task
->tk_flags
, task
->tk_status
,
40 clnt
->cl_clid
, xid
, task
->tk_timeout
, task
->tk_ops
,
41 clnt
->cl_program
->name
, clnt
->cl_vers
, rpc_proc_name(task
),
42 task
->tk_action
, rpc_waitq
);
47 tasks_start(struct seq_file
*f
, loff_t
*ppos
)
48 __acquires(&clnt
->cl_lock
)
50 struct rpc_clnt_iter
*iter
= f
->private;
52 struct rpc_clnt
*clnt
= iter
->clnt
;
53 struct rpc_task
*task
;
56 spin_lock(&clnt
->cl_lock
);
57 list_for_each_entry(task
, &clnt
->cl_tasks
, tk_task
)
64 tasks_next(struct seq_file
*f
, void *v
, loff_t
*pos
)
66 struct rpc_clnt_iter
*iter
= f
->private;
67 struct rpc_clnt
*clnt
= iter
->clnt
;
68 struct rpc_task
*task
= v
;
69 struct list_head
*next
= task
->tk_task
.next
;
74 /* If there's another task on list, return it */
75 if (next
== &clnt
->cl_tasks
)
77 return list_entry(next
, struct rpc_task
, tk_task
);
81 tasks_stop(struct seq_file
*f
, void *v
)
82 __releases(&clnt
->cl_lock
)
84 struct rpc_clnt_iter
*iter
= f
->private;
85 struct rpc_clnt
*clnt
= iter
->clnt
;
87 spin_unlock(&clnt
->cl_lock
);
90 static const struct seq_operations tasks_seq_operations
= {
97 static int tasks_open(struct inode
*inode
, struct file
*filp
)
99 int ret
= seq_open_private(filp
, &tasks_seq_operations
,
100 sizeof(struct rpc_clnt_iter
));
103 struct seq_file
*seq
= filp
->private_data
;
104 struct rpc_clnt_iter
*iter
= seq
->private;
106 iter
->clnt
= inode
->i_private
;
108 if (!atomic_inc_not_zero(&iter
->clnt
->cl_count
)) {
109 seq_release_private(inode
, filp
);
118 tasks_release(struct inode
*inode
, struct file
*filp
)
120 struct seq_file
*seq
= filp
->private_data
;
121 struct rpc_clnt_iter
*iter
= seq
->private;
123 rpc_release_client(iter
->clnt
);
124 return seq_release_private(inode
, filp
);
127 static const struct file_operations tasks_fops
= {
128 .owner
= THIS_MODULE
,
132 .release
= tasks_release
,
136 rpc_clnt_debugfs_register(struct rpc_clnt
*clnt
)
139 char name
[24]; /* enough for "../../rpc_xprt/ + 8 hex digits + NULL */
140 struct rpc_xprt
*xprt
;
142 /* Already registered? */
143 if (clnt
->cl_debugfs
|| !rpc_clnt_dir
)
146 len
= snprintf(name
, sizeof(name
), "%x", clnt
->cl_clid
);
147 if (len
>= sizeof(name
))
150 /* make the per-client dir */
151 clnt
->cl_debugfs
= debugfs_create_dir(name
, rpc_clnt_dir
);
152 if (!clnt
->cl_debugfs
)
155 /* make tasks file */
156 if (!debugfs_create_file("tasks", S_IFREG
| S_IRUSR
, clnt
->cl_debugfs
,
161 xprt
= rcu_dereference(clnt
->cl_xprt
);
162 /* no "debugfs" dentry? Don't bother with the symlink. */
163 if (!xprt
->debugfs
) {
167 len
= snprintf(name
, sizeof(name
), "../../rpc_xprt/%s",
168 xprt
->debugfs
->d_name
.name
);
171 if (len
>= sizeof(name
))
174 if (!debugfs_create_symlink("xprt", clnt
->cl_debugfs
, name
))
179 debugfs_remove_recursive(clnt
->cl_debugfs
);
180 clnt
->cl_debugfs
= NULL
;
184 rpc_clnt_debugfs_unregister(struct rpc_clnt
*clnt
)
186 debugfs_remove_recursive(clnt
->cl_debugfs
);
187 clnt
->cl_debugfs
= NULL
;
191 xprt_info_show(struct seq_file
*f
, void *v
)
193 struct rpc_xprt
*xprt
= f
->private;
195 seq_printf(f
, "netid: %s\n", xprt
->address_strings
[RPC_DISPLAY_NETID
]);
196 seq_printf(f
, "addr: %s\n", xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
197 seq_printf(f
, "port: %s\n", xprt
->address_strings
[RPC_DISPLAY_PORT
]);
198 seq_printf(f
, "state: 0x%lx\n", xprt
->state
);
203 xprt_info_open(struct inode
*inode
, struct file
*filp
)
206 struct rpc_xprt
*xprt
= inode
->i_private
;
208 ret
= single_open(filp
, xprt_info_show
, xprt
);
211 if (!xprt_get(xprt
)) {
212 single_release(inode
, filp
);
220 xprt_info_release(struct inode
*inode
, struct file
*filp
)
222 struct rpc_xprt
*xprt
= inode
->i_private
;
225 return single_release(inode
, filp
);
228 static const struct file_operations xprt_info_fops
= {
229 .owner
= THIS_MODULE
,
230 .open
= xprt_info_open
,
233 .release
= xprt_info_release
,
237 rpc_xprt_debugfs_register(struct rpc_xprt
*xprt
)
240 static atomic_t cur_id
;
241 char name
[9]; /* 8 hex digits + NULL term */
246 id
= (unsigned int)atomic_inc_return(&cur_id
);
248 len
= snprintf(name
, sizeof(name
), "%x", id
);
249 if (len
>= sizeof(name
))
252 /* make the per-client dir */
253 xprt
->debugfs
= debugfs_create_dir(name
, rpc_xprt_dir
);
257 /* make tasks file */
258 if (!debugfs_create_file("info", S_IFREG
| S_IRUSR
, xprt
->debugfs
,
259 xprt
, &xprt_info_fops
)) {
260 debugfs_remove_recursive(xprt
->debugfs
);
261 xprt
->debugfs
= NULL
;
264 atomic_set(&xprt
->inject_disconnect
, rpc_inject_disconnect
);
268 rpc_xprt_debugfs_unregister(struct rpc_xprt
*xprt
)
270 debugfs_remove_recursive(xprt
->debugfs
);
271 xprt
->debugfs
= NULL
;
275 fault_open(struct inode
*inode
, struct file
*filp
)
277 filp
->private_data
= kmalloc(128, GFP_KERNEL
);
278 if (!filp
->private_data
)
284 fault_release(struct inode
*inode
, struct file
*filp
)
286 kfree(filp
->private_data
);
291 fault_disconnect_read(struct file
*filp
, char __user
*user_buf
,
292 size_t len
, loff_t
*offset
)
294 char *buffer
= (char *)filp
->private_data
;
297 size
= sprintf(buffer
, "%u\n", rpc_inject_disconnect
);
298 return simple_read_from_buffer(user_buf
, len
, offset
, buffer
, size
);
302 fault_disconnect_write(struct file
*filp
, const char __user
*user_buf
,
303 size_t len
, loff_t
*offset
)
307 if (len
>= sizeof(buffer
))
308 len
= sizeof(buffer
) - 1;
309 if (copy_from_user(buffer
, user_buf
, len
))
312 if (kstrtouint(buffer
, 10, &rpc_inject_disconnect
))
317 static const struct file_operations fault_disconnect_fops
= {
318 .owner
= THIS_MODULE
,
320 .read
= fault_disconnect_read
,
321 .write
= fault_disconnect_write
,
322 .release
= fault_release
,
325 static struct dentry
*
326 inject_fault_dir(struct dentry
*topdir
)
328 struct dentry
*faultdir
;
330 faultdir
= debugfs_create_dir("inject_fault", topdir
);
334 if (!debugfs_create_file("disconnect", S_IFREG
| S_IRUSR
, faultdir
,
335 NULL
, &fault_disconnect_fops
))
342 sunrpc_debugfs_exit(void)
344 debugfs_remove_recursive(topdir
);
346 rpc_fault_dir
= NULL
;
352 sunrpc_debugfs_init(void)
354 topdir
= debugfs_create_dir("sunrpc", NULL
);
358 rpc_fault_dir
= inject_fault_dir(topdir
);
362 rpc_clnt_dir
= debugfs_create_dir("rpc_clnt", topdir
);
366 rpc_xprt_dir
= debugfs_create_dir("rpc_xprt", topdir
);
372 debugfs_remove_recursive(topdir
);
374 rpc_fault_dir
= NULL
;