Jeff Layton | b4b9d2c | 2014-11-26 14:44:43 -0500 | [diff] [blame] | 1 | /** |
| 2 | * debugfs interface for sunrpc |
| 3 | * |
| 4 | * (c) 2014 Jeff Layton <jlayton@primarydata.com> |
| 5 | */ |
| 6 | |
| 7 | #include <linux/debugfs.h> |
| 8 | #include <linux/sunrpc/sched.h> |
| 9 | #include <linux/sunrpc/clnt.h> |
| 10 | #include "netns.h" |
| 11 | |
| 12 | static struct dentry *topdir; |
Chuck Lever | 4a06825 | 2015-05-11 14:02:25 -0400 | [diff] [blame] | 13 | static struct dentry *rpc_fault_dir; |
Jeff Layton | b4b9d2c | 2014-11-26 14:44:43 -0500 | [diff] [blame] | 14 | static struct dentry *rpc_clnt_dir; |
Jeff Layton | 388f0c7 | 2014-11-26 14:44:44 -0500 | [diff] [blame] | 15 | static struct dentry *rpc_xprt_dir; |
Jeff Layton | b4b9d2c | 2014-11-26 14:44:43 -0500 | [diff] [blame] | 16 | |
Chuck Lever | 4a06825 | 2015-05-11 14:02:25 -0400 | [diff] [blame] | 17 | unsigned int rpc_inject_disconnect; |
| 18 | |
Jeff Layton | b4b9d2c | 2014-11-26 14:44:43 -0500 | [diff] [blame] | 19 | static int |
| 20 | tasks_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, |
| 35 | clnt->cl_clid, xid, task->tk_timeout, task->tk_ops, |
| 36 | clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task), |
| 37 | task->tk_action, rpc_waitq); |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | static void * |
| 42 | tasks_start(struct seq_file *f, loff_t *ppos) |
| 43 | __acquires(&clnt->cl_lock) |
| 44 | { |
Kinglong Mee | 3f373e8 | 2017-02-07 21:49:57 +0800 | [diff] [blame] | 45 | struct rpc_clnt *clnt = f->private; |
Jeff Layton | b4b9d2c | 2014-11-26 14:44:43 -0500 | [diff] [blame] | 46 | loff_t pos = *ppos; |
Jeff Layton | b4b9d2c | 2014-11-26 14:44:43 -0500 | [diff] [blame] | 47 | struct rpc_task *task; |
| 48 | |
Jeff Layton | b4b9d2c | 2014-11-26 14:44:43 -0500 | [diff] [blame] | 49 | 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 | |
| 56 | static void * |
| 57 | tasks_next(struct seq_file *f, void *v, loff_t *pos) |
| 58 | { |
Kinglong Mee | 3f373e8 | 2017-02-07 21:49:57 +0800 | [diff] [blame] | 59 | struct rpc_clnt *clnt = f->private; |
Jeff Layton | b4b9d2c | 2014-11-26 14:44:43 -0500 | [diff] [blame] | 60 | struct rpc_task *task = v; |
| 61 | struct list_head *next = task->tk_task.next; |
| 62 | |
Jeff Layton | b4b9d2c | 2014-11-26 14:44:43 -0500 | [diff] [blame] | 63 | ++*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 | |
| 71 | static void |
| 72 | tasks_stop(struct seq_file *f, void *v) |
| 73 | __releases(&clnt->cl_lock) |
| 74 | { |
Kinglong Mee | 3f373e8 | 2017-02-07 21:49:57 +0800 | [diff] [blame] | 75 | struct rpc_clnt *clnt = f->private; |
Jeff Layton | b4b9d2c | 2014-11-26 14:44:43 -0500 | [diff] [blame] | 76 | spin_unlock(&clnt->cl_lock); |
| 77 | } |
| 78 | |
| 79 | static 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 | |
| 86 | static int tasks_open(struct inode *inode, struct file *filp) |
| 87 | { |
Kinglong Mee | 3f373e8 | 2017-02-07 21:49:57 +0800 | [diff] [blame] | 88 | int ret = seq_open(filp, &tasks_seq_operations); |
Jeff Layton | b4b9d2c | 2014-11-26 14:44:43 -0500 | [diff] [blame] | 89 | if (!ret) { |
| 90 | struct seq_file *seq = filp->private_data; |
Kinglong Mee | 3f373e8 | 2017-02-07 21:49:57 +0800 | [diff] [blame] | 91 | struct rpc_clnt *clnt = seq->private = inode->i_private; |
Jeff Layton | b4b9d2c | 2014-11-26 14:44:43 -0500 | [diff] [blame] | 92 | |
Kinglong Mee | 3f373e8 | 2017-02-07 21:49:57 +0800 | [diff] [blame] | 93 | if (!atomic_inc_not_zero(&clnt->cl_count)) { |
| 94 | seq_release(inode, filp); |
Jeff Layton | b4b9d2c | 2014-11-26 14:44:43 -0500 | [diff] [blame] | 95 | ret = -EINVAL; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return ret; |
| 100 | } |
| 101 | |
| 102 | static int |
| 103 | tasks_release(struct inode *inode, struct file *filp) |
| 104 | { |
| 105 | struct seq_file *seq = filp->private_data; |
Kinglong Mee | 3f373e8 | 2017-02-07 21:49:57 +0800 | [diff] [blame] | 106 | struct rpc_clnt *clnt = seq->private; |
Jeff Layton | b4b9d2c | 2014-11-26 14:44:43 -0500 | [diff] [blame] | 107 | |
Kinglong Mee | 3f373e8 | 2017-02-07 21:49:57 +0800 | [diff] [blame] | 108 | rpc_release_client(clnt); |
| 109 | return seq_release(inode, filp); |
Jeff Layton | b4b9d2c | 2014-11-26 14:44:43 -0500 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static 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 | |
Jeff Layton | f9c72d1 | 2015-03-31 12:03:28 -0400 | [diff] [blame] | 120 | void |
Jeff Layton | b4b9d2c | 2014-11-26 14:44:43 -0500 | [diff] [blame] | 121 | rpc_clnt_debugfs_register(struct rpc_clnt *clnt) |
| 122 | { |
Jeff Layton | f9c72d1 | 2015-03-31 12:03:28 -0400 | [diff] [blame] | 123 | int len; |
Jeff Layton | 388f0c7 | 2014-11-26 14:44:44 -0500 | [diff] [blame] | 124 | char name[24]; /* enough for "../../rpc_xprt/ + 8 hex digits + NULL */ |
Jeff Layton | f9c72d1 | 2015-03-31 12:03:28 -0400 | [diff] [blame] | 125 | struct rpc_xprt *xprt; |
Jeff Layton | b4b9d2c | 2014-11-26 14:44:43 -0500 | [diff] [blame] | 126 | |
| 127 | /* Already registered? */ |
Jeff Layton | f9c72d1 | 2015-03-31 12:03:28 -0400 | [diff] [blame] | 128 | if (clnt->cl_debugfs || !rpc_clnt_dir) |
| 129 | return; |
Jeff Layton | b4b9d2c | 2014-11-26 14:44:43 -0500 | [diff] [blame] | 130 | |
| 131 | len = snprintf(name, sizeof(name), "%x", clnt->cl_clid); |
| 132 | if (len >= sizeof(name)) |
Jeff Layton | f9c72d1 | 2015-03-31 12:03:28 -0400 | [diff] [blame] | 133 | return; |
Jeff Layton | b4b9d2c | 2014-11-26 14:44:43 -0500 | [diff] [blame] | 134 | |
| 135 | /* make the per-client dir */ |
| 136 | clnt->cl_debugfs = debugfs_create_dir(name, rpc_clnt_dir); |
| 137 | if (!clnt->cl_debugfs) |
Jeff Layton | f9c72d1 | 2015-03-31 12:03:28 -0400 | [diff] [blame] | 138 | return; |
Jeff Layton | b4b9d2c | 2014-11-26 14:44:43 -0500 | [diff] [blame] | 139 | |
| 140 | /* make tasks file */ |
| 141 | if (!debugfs_create_file("tasks", S_IFREG | S_IRUSR, clnt->cl_debugfs, |
Jeff Layton | 388f0c7 | 2014-11-26 14:44:44 -0500 | [diff] [blame] | 142 | clnt, &tasks_fops)) |
| 143 | goto out_err; |
| 144 | |
Jeff Layton | 388f0c7 | 2014-11-26 14:44:44 -0500 | [diff] [blame] | 145 | rcu_read_lock(); |
Jeff Layton | f9c72d1 | 2015-03-31 12:03:28 -0400 | [diff] [blame] | 146 | xprt = rcu_dereference(clnt->cl_xprt); |
| 147 | /* no "debugfs" dentry? Don't bother with the symlink. */ |
| 148 | if (!xprt->debugfs) { |
| 149 | rcu_read_unlock(); |
| 150 | return; |
| 151 | } |
Jeff Layton | 388f0c7 | 2014-11-26 14:44:44 -0500 | [diff] [blame] | 152 | len = snprintf(name, sizeof(name), "../../rpc_xprt/%s", |
Jeff Layton | f9c72d1 | 2015-03-31 12:03:28 -0400 | [diff] [blame] | 153 | xprt->debugfs->d_name.name); |
Jeff Layton | 388f0c7 | 2014-11-26 14:44:44 -0500 | [diff] [blame] | 154 | rcu_read_unlock(); |
Jeff Layton | f9c72d1 | 2015-03-31 12:03:28 -0400 | [diff] [blame] | 155 | |
Jeff Layton | 388f0c7 | 2014-11-26 14:44:44 -0500 | [diff] [blame] | 156 | if (len >= sizeof(name)) |
| 157 | goto out_err; |
| 158 | |
Jeff Layton | 388f0c7 | 2014-11-26 14:44:44 -0500 | [diff] [blame] | 159 | if (!debugfs_create_symlink("xprt", clnt->cl_debugfs, name)) |
| 160 | goto out_err; |
Jeff Layton | b4b9d2c | 2014-11-26 14:44:43 -0500 | [diff] [blame] | 161 | |
Jeff Layton | f9c72d1 | 2015-03-31 12:03:28 -0400 | [diff] [blame] | 162 | return; |
Jeff Layton | 388f0c7 | 2014-11-26 14:44:44 -0500 | [diff] [blame] | 163 | out_err: |
| 164 | debugfs_remove_recursive(clnt->cl_debugfs); |
| 165 | clnt->cl_debugfs = NULL; |
Jeff Layton | b4b9d2c | 2014-11-26 14:44:43 -0500 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void |
| 169 | rpc_clnt_debugfs_unregister(struct rpc_clnt *clnt) |
| 170 | { |
| 171 | debugfs_remove_recursive(clnt->cl_debugfs); |
| 172 | clnt->cl_debugfs = NULL; |
| 173 | } |
| 174 | |
Jeff Layton | 388f0c7 | 2014-11-26 14:44:44 -0500 | [diff] [blame] | 175 | static int |
| 176 | xprt_info_show(struct seq_file *f, void *v) |
| 177 | { |
| 178 | struct rpc_xprt *xprt = f->private; |
| 179 | |
| 180 | seq_printf(f, "netid: %s\n", xprt->address_strings[RPC_DISPLAY_NETID]); |
| 181 | seq_printf(f, "addr: %s\n", xprt->address_strings[RPC_DISPLAY_ADDR]); |
| 182 | seq_printf(f, "port: %s\n", xprt->address_strings[RPC_DISPLAY_PORT]); |
| 183 | seq_printf(f, "state: 0x%lx\n", xprt->state); |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | static int |
| 188 | xprt_info_open(struct inode *inode, struct file *filp) |
| 189 | { |
| 190 | int ret; |
| 191 | struct rpc_xprt *xprt = inode->i_private; |
| 192 | |
| 193 | ret = single_open(filp, xprt_info_show, xprt); |
| 194 | |
| 195 | if (!ret) { |
| 196 | if (!xprt_get(xprt)) { |
| 197 | single_release(inode, filp); |
| 198 | ret = -EINVAL; |
| 199 | } |
| 200 | } |
| 201 | return ret; |
| 202 | } |
| 203 | |
| 204 | static int |
| 205 | xprt_info_release(struct inode *inode, struct file *filp) |
| 206 | { |
| 207 | struct rpc_xprt *xprt = inode->i_private; |
| 208 | |
| 209 | xprt_put(xprt); |
| 210 | return single_release(inode, filp); |
| 211 | } |
| 212 | |
| 213 | static const struct file_operations xprt_info_fops = { |
| 214 | .owner = THIS_MODULE, |
| 215 | .open = xprt_info_open, |
| 216 | .read = seq_read, |
| 217 | .llseek = seq_lseek, |
| 218 | .release = xprt_info_release, |
| 219 | }; |
| 220 | |
Jeff Layton | f9c72d1 | 2015-03-31 12:03:28 -0400 | [diff] [blame] | 221 | void |
Jeff Layton | 388f0c7 | 2014-11-26 14:44:44 -0500 | [diff] [blame] | 222 | rpc_xprt_debugfs_register(struct rpc_xprt *xprt) |
| 223 | { |
| 224 | int len, id; |
| 225 | static atomic_t cur_id; |
| 226 | char name[9]; /* 8 hex digits + NULL term */ |
| 227 | |
Jeff Layton | f9c72d1 | 2015-03-31 12:03:28 -0400 | [diff] [blame] | 228 | if (!rpc_xprt_dir) |
| 229 | return; |
| 230 | |
Jeff Layton | 388f0c7 | 2014-11-26 14:44:44 -0500 | [diff] [blame] | 231 | id = (unsigned int)atomic_inc_return(&cur_id); |
| 232 | |
| 233 | len = snprintf(name, sizeof(name), "%x", id); |
| 234 | if (len >= sizeof(name)) |
Jeff Layton | f9c72d1 | 2015-03-31 12:03:28 -0400 | [diff] [blame] | 235 | return; |
Jeff Layton | 388f0c7 | 2014-11-26 14:44:44 -0500 | [diff] [blame] | 236 | |
| 237 | /* make the per-client dir */ |
| 238 | xprt->debugfs = debugfs_create_dir(name, rpc_xprt_dir); |
| 239 | if (!xprt->debugfs) |
Jeff Layton | f9c72d1 | 2015-03-31 12:03:28 -0400 | [diff] [blame] | 240 | return; |
Jeff Layton | 388f0c7 | 2014-11-26 14:44:44 -0500 | [diff] [blame] | 241 | |
| 242 | /* make tasks file */ |
| 243 | if (!debugfs_create_file("info", S_IFREG | S_IRUSR, xprt->debugfs, |
| 244 | xprt, &xprt_info_fops)) { |
| 245 | debugfs_remove_recursive(xprt->debugfs); |
| 246 | xprt->debugfs = NULL; |
Jeff Layton | 388f0c7 | 2014-11-26 14:44:44 -0500 | [diff] [blame] | 247 | } |
Chuck Lever | 4a06825 | 2015-05-11 14:02:25 -0400 | [diff] [blame] | 248 | |
| 249 | atomic_set(&xprt->inject_disconnect, rpc_inject_disconnect); |
Jeff Layton | 388f0c7 | 2014-11-26 14:44:44 -0500 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | void |
| 253 | rpc_xprt_debugfs_unregister(struct rpc_xprt *xprt) |
| 254 | { |
| 255 | debugfs_remove_recursive(xprt->debugfs); |
| 256 | xprt->debugfs = NULL; |
| 257 | } |
| 258 | |
Chuck Lever | 4a06825 | 2015-05-11 14:02:25 -0400 | [diff] [blame] | 259 | static int |
| 260 | fault_open(struct inode *inode, struct file *filp) |
| 261 | { |
| 262 | filp->private_data = kmalloc(128, GFP_KERNEL); |
| 263 | if (!filp->private_data) |
| 264 | return -ENOMEM; |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | static int |
| 269 | fault_release(struct inode *inode, struct file *filp) |
| 270 | { |
| 271 | kfree(filp->private_data); |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | static ssize_t |
| 276 | fault_disconnect_read(struct file *filp, char __user *user_buf, |
| 277 | size_t len, loff_t *offset) |
| 278 | { |
| 279 | char *buffer = (char *)filp->private_data; |
| 280 | size_t size; |
| 281 | |
| 282 | size = sprintf(buffer, "%u\n", rpc_inject_disconnect); |
| 283 | return simple_read_from_buffer(user_buf, len, offset, buffer, size); |
| 284 | } |
| 285 | |
| 286 | static ssize_t |
| 287 | fault_disconnect_write(struct file *filp, const char __user *user_buf, |
| 288 | size_t len, loff_t *offset) |
| 289 | { |
| 290 | char buffer[16]; |
| 291 | |
Chuck Lever | 5fd23f7 | 2015-06-11 13:47:10 -0400 | [diff] [blame] | 292 | if (len >= sizeof(buffer)) |
| 293 | len = sizeof(buffer) - 1; |
Chuck Lever | 4a06825 | 2015-05-11 14:02:25 -0400 | [diff] [blame] | 294 | if (copy_from_user(buffer, user_buf, len)) |
| 295 | return -EFAULT; |
| 296 | buffer[len] = '\0'; |
| 297 | if (kstrtouint(buffer, 10, &rpc_inject_disconnect)) |
| 298 | return -EINVAL; |
| 299 | return len; |
| 300 | } |
| 301 | |
| 302 | static const struct file_operations fault_disconnect_fops = { |
| 303 | .owner = THIS_MODULE, |
| 304 | .open = fault_open, |
| 305 | .read = fault_disconnect_read, |
| 306 | .write = fault_disconnect_write, |
| 307 | .release = fault_release, |
| 308 | }; |
| 309 | |
| 310 | static struct dentry * |
| 311 | inject_fault_dir(struct dentry *topdir) |
| 312 | { |
| 313 | struct dentry *faultdir; |
| 314 | |
| 315 | faultdir = debugfs_create_dir("inject_fault", topdir); |
| 316 | if (!faultdir) |
| 317 | return NULL; |
| 318 | |
| 319 | if (!debugfs_create_file("disconnect", S_IFREG | S_IRUSR, faultdir, |
| 320 | NULL, &fault_disconnect_fops)) |
| 321 | return NULL; |
| 322 | |
| 323 | return faultdir; |
| 324 | } |
| 325 | |
Jeff Layton | b4b9d2c | 2014-11-26 14:44:43 -0500 | [diff] [blame] | 326 | void __exit |
| 327 | sunrpc_debugfs_exit(void) |
| 328 | { |
| 329 | debugfs_remove_recursive(topdir); |
Jeff Layton | f9c72d1 | 2015-03-31 12:03:28 -0400 | [diff] [blame] | 330 | topdir = NULL; |
Chuck Lever | 4a06825 | 2015-05-11 14:02:25 -0400 | [diff] [blame] | 331 | rpc_fault_dir = NULL; |
Jeff Layton | f9c72d1 | 2015-03-31 12:03:28 -0400 | [diff] [blame] | 332 | rpc_clnt_dir = NULL; |
| 333 | rpc_xprt_dir = NULL; |
Jeff Layton | b4b9d2c | 2014-11-26 14:44:43 -0500 | [diff] [blame] | 334 | } |
| 335 | |
Jeff Layton | f9c72d1 | 2015-03-31 12:03:28 -0400 | [diff] [blame] | 336 | void __init |
Jeff Layton | b4b9d2c | 2014-11-26 14:44:43 -0500 | [diff] [blame] | 337 | sunrpc_debugfs_init(void) |
| 338 | { |
| 339 | topdir = debugfs_create_dir("sunrpc", NULL); |
| 340 | if (!topdir) |
Jeff Layton | f9c72d1 | 2015-03-31 12:03:28 -0400 | [diff] [blame] | 341 | return; |
Jeff Layton | b4b9d2c | 2014-11-26 14:44:43 -0500 | [diff] [blame] | 342 | |
Chuck Lever | 4a06825 | 2015-05-11 14:02:25 -0400 | [diff] [blame] | 343 | rpc_fault_dir = inject_fault_dir(topdir); |
| 344 | if (!rpc_fault_dir) |
| 345 | goto out_remove; |
| 346 | |
Jeff Layton | b4b9d2c | 2014-11-26 14:44:43 -0500 | [diff] [blame] | 347 | rpc_clnt_dir = debugfs_create_dir("rpc_clnt", topdir); |
| 348 | if (!rpc_clnt_dir) |
| 349 | goto out_remove; |
| 350 | |
Jeff Layton | 388f0c7 | 2014-11-26 14:44:44 -0500 | [diff] [blame] | 351 | rpc_xprt_dir = debugfs_create_dir("rpc_xprt", topdir); |
| 352 | if (!rpc_xprt_dir) |
| 353 | goto out_remove; |
| 354 | |
Jeff Layton | f9c72d1 | 2015-03-31 12:03:28 -0400 | [diff] [blame] | 355 | return; |
Jeff Layton | b4b9d2c | 2014-11-26 14:44:43 -0500 | [diff] [blame] | 356 | out_remove: |
| 357 | debugfs_remove_recursive(topdir); |
| 358 | topdir = NULL; |
Chuck Lever | 4a06825 | 2015-05-11 14:02:25 -0400 | [diff] [blame] | 359 | rpc_fault_dir = NULL; |
Jeff Layton | f9c72d1 | 2015-03-31 12:03:28 -0400 | [diff] [blame] | 360 | rpc_clnt_dir = NULL; |
Jeff Layton | b4b9d2c | 2014-11-26 14:44:43 -0500 | [diff] [blame] | 361 | } |