debugfs.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. // SPDX-License-Identifier: GPL-2.0
  2. /**
  3. * debugfs interface for sunrpc
  4. *
  5. * (c) 2014 Jeff Layton <jlayton@primarydata.com>
  6. */
  7. #include <linux/debugfs.h>
  8. #include <linux/sunrpc/sched.h>
  9. #include <linux/sunrpc/clnt.h>
  10. #include "netns.h"
  11. static struct dentry *topdir;
  12. static struct dentry *rpc_fault_dir;
  13. static struct dentry *rpc_clnt_dir;
  14. static struct dentry *rpc_xprt_dir;
  15. unsigned int rpc_inject_disconnect;
  16. static int
  17. tasks_show(struct seq_file *f, void *v)
  18. {
  19. u32 xid = 0;
  20. struct rpc_task *task = v;
  21. struct rpc_clnt *clnt = task->tk_client;
  22. const char *rpc_waitq = "none";
  23. if (RPC_IS_QUEUED(task))
  24. rpc_waitq = rpc_qname(task->tk_waitqueue);
  25. if (task->tk_rqstp)
  26. xid = be32_to_cpu(task->tk_rqstp->rq_xid);
  27. seq_printf(f, "%5u %04x %6d 0x%x 0x%x %8ld %ps %sv%u %s a:%ps q:%s\n",
  28. task->tk_pid, task->tk_flags, task->tk_status,
  29. clnt->cl_clid, xid, task->tk_timeout, task->tk_ops,
  30. clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
  31. task->tk_action, rpc_waitq);
  32. return 0;
  33. }
  34. static void *
  35. tasks_start(struct seq_file *f, loff_t *ppos)
  36. __acquires(&clnt->cl_lock)
  37. {
  38. struct rpc_clnt *clnt = f->private;
  39. loff_t pos = *ppos;
  40. struct rpc_task *task;
  41. spin_lock(&clnt->cl_lock);
  42. list_for_each_entry(task, &clnt->cl_tasks, tk_task)
  43. if (pos-- == 0)
  44. return task;
  45. return NULL;
  46. }
  47. static void *
  48. tasks_next(struct seq_file *f, void *v, loff_t *pos)
  49. {
  50. struct rpc_clnt *clnt = f->private;
  51. struct rpc_task *task = v;
  52. struct list_head *next = task->tk_task.next;
  53. ++*pos;
  54. /* If there's another task on list, return it */
  55. if (next == &clnt->cl_tasks)
  56. return NULL;
  57. return list_entry(next, struct rpc_task, tk_task);
  58. }
  59. static void
  60. tasks_stop(struct seq_file *f, void *v)
  61. __releases(&clnt->cl_lock)
  62. {
  63. struct rpc_clnt *clnt = f->private;
  64. spin_unlock(&clnt->cl_lock);
  65. }
  66. static const struct seq_operations tasks_seq_operations = {
  67. .start = tasks_start,
  68. .next = tasks_next,
  69. .stop = tasks_stop,
  70. .show = tasks_show,
  71. };
  72. static int tasks_open(struct inode *inode, struct file *filp)
  73. {
  74. int ret = seq_open(filp, &tasks_seq_operations);
  75. if (!ret) {
  76. struct seq_file *seq = filp->private_data;
  77. struct rpc_clnt *clnt = seq->private = inode->i_private;
  78. if (!atomic_inc_not_zero(&clnt->cl_count)) {
  79. seq_release(inode, filp);
  80. ret = -EINVAL;
  81. }
  82. }
  83. return ret;
  84. }
  85. static int
  86. tasks_release(struct inode *inode, struct file *filp)
  87. {
  88. struct seq_file *seq = filp->private_data;
  89. struct rpc_clnt *clnt = seq->private;
  90. rpc_release_client(clnt);
  91. return seq_release(inode, filp);
  92. }
  93. static const struct file_operations tasks_fops = {
  94. .owner = THIS_MODULE,
  95. .open = tasks_open,
  96. .read = seq_read,
  97. .llseek = seq_lseek,
  98. .release = tasks_release,
  99. };
  100. void
  101. rpc_clnt_debugfs_register(struct rpc_clnt *clnt)
  102. {
  103. int len;
  104. char name[24]; /* enough for "../../rpc_xprt/ + 8 hex digits + NULL */
  105. struct rpc_xprt *xprt;
  106. /* Already registered? */
  107. if (clnt->cl_debugfs || !rpc_clnt_dir)
  108. return;
  109. len = snprintf(name, sizeof(name), "%x", clnt->cl_clid);
  110. if (len >= sizeof(name))
  111. return;
  112. /* make the per-client dir */
  113. clnt->cl_debugfs = debugfs_create_dir(name, rpc_clnt_dir);
  114. if (!clnt->cl_debugfs)
  115. return;
  116. /* make tasks file */
  117. if (!debugfs_create_file("tasks", S_IFREG | S_IRUSR, clnt->cl_debugfs,
  118. clnt, &tasks_fops))
  119. goto out_err;
  120. rcu_read_lock();
  121. xprt = rcu_dereference(clnt->cl_xprt);
  122. /* no "debugfs" dentry? Don't bother with the symlink. */
  123. if (!xprt->debugfs) {
  124. rcu_read_unlock();
  125. return;
  126. }
  127. len = snprintf(name, sizeof(name), "../../rpc_xprt/%s",
  128. xprt->debugfs->d_name.name);
  129. rcu_read_unlock();
  130. if (len >= sizeof(name))
  131. goto out_err;
  132. if (!debugfs_create_symlink("xprt", clnt->cl_debugfs, name))
  133. goto out_err;
  134. return;
  135. out_err:
  136. debugfs_remove_recursive(clnt->cl_debugfs);
  137. clnt->cl_debugfs = NULL;
  138. }
  139. void
  140. rpc_clnt_debugfs_unregister(struct rpc_clnt *clnt)
  141. {
  142. debugfs_remove_recursive(clnt->cl_debugfs);
  143. clnt->cl_debugfs = NULL;
  144. }
  145. static int
  146. xprt_info_show(struct seq_file *f, void *v)
  147. {
  148. struct rpc_xprt *xprt = f->private;
  149. seq_printf(f, "netid: %s\n", xprt->address_strings[RPC_DISPLAY_NETID]);
  150. seq_printf(f, "addr: %s\n", xprt->address_strings[RPC_DISPLAY_ADDR]);
  151. seq_printf(f, "port: %s\n", xprt->address_strings[RPC_DISPLAY_PORT]);
  152. seq_printf(f, "state: 0x%lx\n", xprt->state);
  153. return 0;
  154. }
  155. static int
  156. xprt_info_open(struct inode *inode, struct file *filp)
  157. {
  158. int ret;
  159. struct rpc_xprt *xprt = inode->i_private;
  160. ret = single_open(filp, xprt_info_show, xprt);
  161. if (!ret) {
  162. if (!xprt_get(xprt)) {
  163. single_release(inode, filp);
  164. ret = -EINVAL;
  165. }
  166. }
  167. return ret;
  168. }
  169. static int
  170. xprt_info_release(struct inode *inode, struct file *filp)
  171. {
  172. struct rpc_xprt *xprt = inode->i_private;
  173. xprt_put(xprt);
  174. return single_release(inode, filp);
  175. }
  176. static const struct file_operations xprt_info_fops = {
  177. .owner = THIS_MODULE,
  178. .open = xprt_info_open,
  179. .read = seq_read,
  180. .llseek = seq_lseek,
  181. .release = xprt_info_release,
  182. };
  183. void
  184. rpc_xprt_debugfs_register(struct rpc_xprt *xprt)
  185. {
  186. int len, id;
  187. static atomic_t cur_id;
  188. char name[9]; /* 8 hex digits + NULL term */
  189. if (!rpc_xprt_dir)
  190. return;
  191. id = (unsigned int)atomic_inc_return(&cur_id);
  192. len = snprintf(name, sizeof(name), "%x", id);
  193. if (len >= sizeof(name))
  194. return;
  195. /* make the per-client dir */
  196. xprt->debugfs = debugfs_create_dir(name, rpc_xprt_dir);
  197. if (!xprt->debugfs)
  198. return;
  199. /* make tasks file */
  200. if (!debugfs_create_file("info", S_IFREG | S_IRUSR, xprt->debugfs,
  201. xprt, &xprt_info_fops)) {
  202. debugfs_remove_recursive(xprt->debugfs);
  203. xprt->debugfs = NULL;
  204. }
  205. atomic_set(&xprt->inject_disconnect, rpc_inject_disconnect);
  206. }
  207. void
  208. rpc_xprt_debugfs_unregister(struct rpc_xprt *xprt)
  209. {
  210. debugfs_remove_recursive(xprt->debugfs);
  211. xprt->debugfs = NULL;
  212. }
  213. static int
  214. fault_open(struct inode *inode, struct file *filp)
  215. {
  216. filp->private_data = kmalloc(128, GFP_KERNEL);
  217. if (!filp->private_data)
  218. return -ENOMEM;
  219. return 0;
  220. }
  221. static int
  222. fault_release(struct inode *inode, struct file *filp)
  223. {
  224. kfree(filp->private_data);
  225. return 0;
  226. }
  227. static ssize_t
  228. fault_disconnect_read(struct file *filp, char __user *user_buf,
  229. size_t len, loff_t *offset)
  230. {
  231. char *buffer = (char *)filp->private_data;
  232. size_t size;
  233. size = sprintf(buffer, "%u\n", rpc_inject_disconnect);
  234. return simple_read_from_buffer(user_buf, len, offset, buffer, size);
  235. }
  236. static ssize_t
  237. fault_disconnect_write(struct file *filp, const char __user *user_buf,
  238. size_t len, loff_t *offset)
  239. {
  240. char buffer[16];
  241. if (len >= sizeof(buffer))
  242. len = sizeof(buffer) - 1;
  243. if (copy_from_user(buffer, user_buf, len))
  244. return -EFAULT;
  245. buffer[len] = '\0';
  246. if (kstrtouint(buffer, 10, &rpc_inject_disconnect))
  247. return -EINVAL;
  248. return len;
  249. }
  250. static const struct file_operations fault_disconnect_fops = {
  251. .owner = THIS_MODULE,
  252. .open = fault_open,
  253. .read = fault_disconnect_read,
  254. .write = fault_disconnect_write,
  255. .release = fault_release,
  256. };
  257. static struct dentry *
  258. inject_fault_dir(struct dentry *topdir)
  259. {
  260. struct dentry *faultdir;
  261. faultdir = debugfs_create_dir("inject_fault", topdir);
  262. if (!faultdir)
  263. return NULL;
  264. if (!debugfs_create_file("disconnect", S_IFREG | S_IRUSR, faultdir,
  265. NULL, &fault_disconnect_fops))
  266. return NULL;
  267. return faultdir;
  268. }
  269. void __exit
  270. sunrpc_debugfs_exit(void)
  271. {
  272. debugfs_remove_recursive(topdir);
  273. topdir = NULL;
  274. rpc_fault_dir = NULL;
  275. rpc_clnt_dir = NULL;
  276. rpc_xprt_dir = NULL;
  277. }
  278. void __init
  279. sunrpc_debugfs_init(void)
  280. {
  281. topdir = debugfs_create_dir("sunrpc", NULL);
  282. if (!topdir)
  283. return;
  284. rpc_fault_dir = inject_fault_dir(topdir);
  285. if (!rpc_fault_dir)
  286. goto out_remove;
  287. rpc_clnt_dir = debugfs_create_dir("rpc_clnt", topdir);
  288. if (!rpc_clnt_dir)
  289. goto out_remove;
  290. rpc_xprt_dir = debugfs_create_dir("rpc_xprt", topdir);
  291. if (!rpc_xprt_dir)
  292. goto out_remove;
  293. return;
  294. out_remove:
  295. debugfs_remove_recursive(topdir);
  296. topdir = NULL;
  297. rpc_fault_dir = NULL;
  298. rpc_clnt_dir = NULL;
  299. }