| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* debugfs interface for sunrpc |
| 4 |
* |
| 5 |
* (c) 2014 Jeff Layton <[email protected]> |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <linux/debugfs.h> |
| 9 |
#include <linux/sunrpc/sched.h> |
| 10 |
#include <linux/sunrpc/clnt.h> |
| 11 |
|
| 12 |
#include "netns.h" |
| 13 |
#include "fail.h" |
| 14 |
|
| 15 |
static struct dentry *topdir; |
| 16 |
static struct dentry *rpc_clnt_dir; |
| 17 |
static struct dentry *rpc_xprt_dir; |
| 18 |
|
| 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, rpc_task_timeout(task), 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 |
{ |
| 45 |
struct rpc_clnt *clnt = f->private; |
| 46 |
loff_t pos = *ppos; |
| 47 |
struct rpc_task *task; |
| 48 |
|
| 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 |
{ |
| 59 |
struct rpc_clnt *clnt = f->private; |
| 60 |
struct rpc_task *task = v; |
| 61 |
struct list_head *next = task->tk_task.next; |
| 62 |
|
| 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 |
{ |
| 75 |
struct rpc_clnt *clnt = f->private; |
| 76 |
spin_unlock(&clnt->cl_lock); |
| 77 |
seq_printf(f, "clnt[%pISpc] RPC tasks[%d]\n", |
| 78 |
(struct sockaddr *)&clnt->cl_xprt->addr, |
| 79 |
atomic_read(&clnt->cl_task_count)); |
| 80 |
} |
| 81 |
|
| 82 |
static const struct seq_operations tasks_seq_operations = { |
| 83 |
.start = tasks_start, |
| 84 |
.next = tasks_next, |
| 85 |
.stop = tasks_stop, |
| 86 |
.show = tasks_show, |
| 87 |
}; |
| 88 |
|
| 89 |
static int tasks_open(struct inode *inode, struct file *filp) |
| 90 |
{ |
| 91 |
int ret = seq_open(filp, &tasks_seq_operations); |
| 92 |
if (!ret) { |
| 93 |
struct seq_file *seq = filp->private_data; |
| 94 |
struct rpc_clnt *clnt = seq->private = inode->i_private; |
| 95 |
|
| 96 |
if (!refcount_inc_not_zero(&clnt->cl_count)) { |
| 97 |
seq_release(inode, filp); |
| 98 |
ret = -EINVAL; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
return ret; |
| 103 |
} |
| 104 |
|
| 105 |
static int |
| 106 |
tasks_release(struct inode *inode, struct file *filp) |
| 107 |
{ |
| 108 |
struct seq_file *seq = filp->private_data; |
| 109 |
struct rpc_clnt *clnt = seq->private; |
| 110 |
|
| 111 |
rpc_release_client(clnt); |
| 112 |
return seq_release(inode, filp); |
| 113 |
} |
| 114 |
|
| 115 |
static const struct file_operations tasks_fops = { |
| 116 |
.owner = THIS_MODULE, |
| 117 |
.open = tasks_open, |
| 118 |
.read = seq_read, |
| 119 |
.llseek = seq_lseek, |
| 120 |
.release = tasks_release, |
| 121 |
}; |
| 122 |
|
| 123 |
static int do_xprt_debugfs(struct rpc_clnt *clnt, struct rpc_xprt *xprt, void *numv) |
| 124 |
{ |
| 125 |
int len; |
| 126 |
char name[24]; /* enough for "../../rpc_xprt/ + 8 hex digits + NULL */ |
| 127 |
char link[9]; /* enough for 8 hex digits + NULL */ |
| 128 |
int *nump = numv; |
| 129 |
|
| 130 |
if (IS_ERR_OR_NULL(xprt->debugfs)) |
| 131 |
return 0; |
| 132 |
len = snprintf(name, sizeof(name), "../../rpc_xprt/%s", |
| 133 |
xprt->debugfs->d_name.name); |
| 134 |
if (len >= sizeof(name)) |
| 135 |
return -1; |
| 136 |
if (*nump == 0) |
| 137 |
strcpy(link, "xprt"); |
| 138 |
else { |
| 139 |
len = snprintf(link, sizeof(link), "xprt%d", *nump); |
| 140 |
if (len >= sizeof(link)) |
| 141 |
return -1; |
| 142 |
} |
| 143 |
debugfs_create_symlink(link, clnt->cl_debugfs, name); |
| 144 |
(*nump)++; |
| 145 |
return 0; |
| 146 |
} |
| 147 |
|
| 148 |
void |
| 149 |
rpc_clnt_debugfs_register(struct rpc_clnt *clnt) |
| 150 |
{ |
| 151 |
int len; |
| 152 |
char name[9]; /* enough for 8 hex digits + NULL */ |
| 153 |
int xprtnum = 0; |
| 154 |
|
| 155 |
len = snprintf(name, sizeof(name), "%x", clnt->cl_clid); |
| 156 |
if (len >= sizeof(name)) |
| 157 |
return; |
| 158 |
|
| 159 |
/* make the per-client dir */ |
| 160 |
clnt->cl_debugfs = debugfs_create_dir(name, rpc_clnt_dir); |
| 161 |
|
| 162 |
/* make tasks file */ |
| 163 |
debugfs_create_file("tasks", S_IFREG | 0400, clnt->cl_debugfs, clnt, |
| 164 |
&tasks_fops); |
| 165 |
|
| 166 |
rpc_clnt_iterate_for_each_xprt(clnt, do_xprt_debugfs, &xprtnum); |
| 167 |
} |
| 168 |
|
| 169 |
void |
| 170 |
rpc_clnt_debugfs_unregister(struct rpc_clnt *clnt) |
| 171 |
{ |
| 172 |
debugfs_remove_recursive(clnt->cl_debugfs); |
| 173 |
clnt->cl_debugfs = NULL; |
| 174 |
} |
| 175 |
|
| 176 |
static int |
| 177 |
xprt_info_show(struct seq_file *f, void *v) |
| 178 |
{ |
| 179 |
struct rpc_xprt *xprt = f->private; |
| 180 |
|
| 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 |
seq_printf(f, "netns: %u\n", xprt->xprt_net->ns.inum); |
| 186 |
|
| 187 |
if (xprt->ops->get_srcaddr) { |
| 188 |
int ret, buflen; |
| 189 |
char buf[INET6_ADDRSTRLEN]; |
| 190 |
|
| 191 |
buflen = ARRAY_SIZE(buf); |
| 192 |
ret = xprt->ops->get_srcaddr(xprt, buf, buflen); |
| 193 |
if (ret < 0) |
| 194 |
ret = sprintf(buf, "<closed>"); |
| 195 |
seq_printf(f, "saddr: %.*s\n", ret, buf); |
| 196 |
} |
| 197 |
return 0; |
| 198 |
} |
| 199 |
|
| 200 |
static int |
| 201 |
xprt_info_open(struct inode *inode, struct file *filp) |
| 202 |
{ |
| 203 |
int ret; |
| 204 |
struct rpc_xprt *xprt = inode->i_private; |
| 205 |
|
| 206 |
ret = single_open(filp, xprt_info_show, xprt); |
| 207 |
|
| 208 |
if (!ret) { |
| 209 |
if (!xprt_get(xprt)) { |
| 210 |
single_release(inode, filp); |
| 211 |
ret = -EINVAL; |
| 212 |
} |
| 213 |
} |
| 214 |
return ret; |
| 215 |
} |
| 216 |
|
| 217 |
static int |
| 218 |
xprt_info_release(struct inode *inode, struct file *filp) |
| 219 |
{ |
| 220 |
struct rpc_xprt *xprt = inode->i_private; |
| 221 |
|
| 222 |
xprt_put(xprt); |
| 223 |
return single_release(inode, filp); |
| 224 |
} |
| 225 |
|
| 226 |
static const struct file_operations xprt_info_fops = { |
| 227 |
.owner = THIS_MODULE, |
| 228 |
.open = xprt_info_open, |
| 229 |
.read = seq_read, |
| 230 |
.llseek = seq_lseek, |
| 231 |
.release = xprt_info_release, |
| 232 |
}; |
| 233 |
|
| 234 |
void |
| 235 |
rpc_xprt_debugfs_register(struct rpc_xprt *xprt) |
| 236 |
{ |
| 237 |
int len, id; |
| 238 |
static atomic_t cur_id; |
| 239 |
char name[9]; /* 8 hex digits + NULL term */ |
| 240 |
|
| 241 |
id = (unsigned int)atomic_inc_return(&cur_id); |
| 242 |
|
| 243 |
len = snprintf(name, sizeof(name), "%x", id); |
| 244 |
if (len >= sizeof(name)) |
| 245 |
return; |
| 246 |
|
| 247 |
/* make the per-client dir */ |
| 248 |
xprt->debugfs = debugfs_create_dir(name, rpc_xprt_dir); |
| 249 |
|
| 250 |
/* make tasks file */ |
| 251 |
debugfs_create_file("info", S_IFREG | 0400, xprt->debugfs, xprt, |
| 252 |
&xprt_info_fops); |
| 253 |
} |
| 254 |
|
| 255 |
void |
| 256 |
rpc_xprt_debugfs_unregister(struct rpc_xprt *xprt) |
| 257 |
{ |
| 258 |
debugfs_remove_recursive(xprt->debugfs); |
| 259 |
xprt->debugfs = NULL; |
| 260 |
} |
| 261 |
|
| 262 |
#if IS_ENABLED(CONFIG_FAIL_SUNRPC) |
| 263 |
struct fail_sunrpc_attr fail_sunrpc = { |
| 264 |
.attr = FAULT_ATTR_INITIALIZER, |
| 265 |
}; |
| 266 |
EXPORT_SYMBOL_GPL(fail_sunrpc); |
| 267 |
|
| 268 |
static void fail_sunrpc_init(void) |
| 269 |
{ |
| 270 |
struct dentry *dir; |
| 271 |
|
| 272 |
dir = fault_create_debugfs_attr("fail_sunrpc", NULL, |
| 273 |
&fail_sunrpc.attr); |
| 274 |
|
| 275 |
debugfs_create_bool("ignore-client-disconnect", S_IFREG | 0600, dir, |
| 276 |
&fail_sunrpc.ignore_client_disconnect); |
| 277 |
|
| 278 |
debugfs_create_bool("ignore-server-disconnect", S_IFREG | 0600, dir, |
| 279 |
&fail_sunrpc.ignore_server_disconnect); |
| 280 |
|
| 281 |
debugfs_create_bool("ignore-cache-wait", S_IFREG | 0600, dir, |
| 282 |
&fail_sunrpc.ignore_cache_wait); |
| 283 |
} |
| 284 |
#else |
| 285 |
static void fail_sunrpc_init(void) |
| 286 |
{ |
| 287 |
} |
| 288 |
#endif |
| 289 |
|
| 290 |
void __exit |
| 291 |
sunrpc_debugfs_exit(void) |
| 292 |
{ |
| 293 |
debugfs_remove_recursive(topdir); |
| 294 |
topdir = NULL; |
| 295 |
rpc_clnt_dir = NULL; |
| 296 |
rpc_xprt_dir = NULL; |
| 297 |
} |
| 298 |
|
| 299 |
void __init |
| 300 |
sunrpc_debugfs_init(void) |
| 301 |
{ |
| 302 |
topdir = debugfs_create_dir("sunrpc", NULL); |
| 303 |
|
| 304 |
rpc_clnt_dir = debugfs_create_dir("rpc_clnt", topdir); |
| 305 |
|
| 306 |
rpc_xprt_dir = debugfs_create_dir("rpc_xprt", topdir); |
| 307 |
|
| 308 |
fail_sunrpc_init(); |
| 309 |
} |