debugfs.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * debugfs interface for sunrpc
  4. *
  5. * (c) 2014 Jeff Layton <[email protected]>
  6. */
  7. #include <linux/debugfs.h>
  8. #include <linux/sunrpc/sched.h>
  9. #include <linux/sunrpc/clnt.h>
  10. #include "netns.h"
  11. #include "fail.h"
  12. static struct dentry *topdir;
  13. static struct dentry *rpc_clnt_dir;
  14. static struct dentry *rpc_xprt_dir;
  15. static int
  16. tasks_show(struct seq_file *f, void *v)
  17. {
  18. u32 xid = 0;
  19. struct rpc_task *task = v;
  20. struct rpc_clnt *clnt = task->tk_client;
  21. const char *rpc_waitq = "none";
  22. if (RPC_IS_QUEUED(task))
  23. rpc_waitq = rpc_qname(task->tk_waitqueue);
  24. if (task->tk_rqstp)
  25. xid = be32_to_cpu(task->tk_rqstp->rq_xid);
  26. seq_printf(f, "%5u %04x %6d 0x%x 0x%x %8ld %ps %sv%u %s a:%ps q:%s\n",
  27. task->tk_pid, task->tk_flags, task->tk_status,
  28. clnt->cl_clid, xid, rpc_task_timeout(task), task->tk_ops,
  29. clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
  30. task->tk_action, rpc_waitq);
  31. return 0;
  32. }
  33. static void *
  34. tasks_start(struct seq_file *f, loff_t *ppos)
  35. __acquires(&clnt->cl_lock)
  36. {
  37. struct rpc_clnt *clnt = f->private;
  38. loff_t pos = *ppos;
  39. struct rpc_task *task;
  40. spin_lock(&clnt->cl_lock);
  41. list_for_each_entry(task, &clnt->cl_tasks, tk_task)
  42. if (pos-- == 0)
  43. return task;
  44. return NULL;
  45. }
  46. static void *
  47. tasks_next(struct seq_file *f, void *v, loff_t *pos)
  48. {
  49. struct rpc_clnt *clnt = f->private;
  50. struct rpc_task *task = v;
  51. struct list_head *next = task->tk_task.next;
  52. ++*pos;
  53. /* If there's another task on list, return it */
  54. if (next == &clnt->cl_tasks)
  55. return NULL;
  56. return list_entry(next, struct rpc_task, tk_task);
  57. }
  58. static void
  59. tasks_stop(struct seq_file *f, void *v)
  60. __releases(&clnt->cl_lock)
  61. {
  62. struct rpc_clnt *clnt = f->private;
  63. spin_unlock(&clnt->cl_lock);
  64. }
  65. static const struct seq_operations tasks_seq_operations = {
  66. .start = tasks_start,
  67. .next = tasks_next,
  68. .stop = tasks_stop,
  69. .show = tasks_show,
  70. };
  71. static int tasks_open(struct inode *inode, struct file *filp)
  72. {
  73. int ret = seq_open(filp, &tasks_seq_operations);
  74. if (!ret) {
  75. struct seq_file *seq = filp->private_data;
  76. struct rpc_clnt *clnt = seq->private = inode->i_private;
  77. if (!refcount_inc_not_zero(&clnt->cl_count)) {
  78. seq_release(inode, filp);
  79. ret = -EINVAL;
  80. }
  81. }
  82. return ret;
  83. }
  84. static int
  85. tasks_release(struct inode *inode, struct file *filp)
  86. {
  87. struct seq_file *seq = filp->private_data;
  88. struct rpc_clnt *clnt = seq->private;
  89. rpc_release_client(clnt);
  90. return seq_release(inode, filp);
  91. }
  92. static const struct file_operations tasks_fops = {
  93. .owner = THIS_MODULE,
  94. .open = tasks_open,
  95. .read = seq_read,
  96. .llseek = seq_lseek,
  97. .release = tasks_release,
  98. };
  99. static int do_xprt_debugfs(struct rpc_clnt *clnt, struct rpc_xprt *xprt, void *numv)
  100. {
  101. int len;
  102. char name[24]; /* enough for "../../rpc_xprt/ + 8 hex digits + NULL */
  103. char link[9]; /* enough for 8 hex digits + NULL */
  104. int *nump = numv;
  105. if (IS_ERR_OR_NULL(xprt->debugfs))
  106. return 0;
  107. len = snprintf(name, sizeof(name), "../../rpc_xprt/%s",
  108. xprt->debugfs->d_name.name);
  109. if (len >= sizeof(name))
  110. return -1;
  111. if (*nump == 0)
  112. strcpy(link, "xprt");
  113. else {
  114. len = snprintf(link, sizeof(link), "xprt%d", *nump);
  115. if (len >= sizeof(link))
  116. return -1;
  117. }
  118. debugfs_create_symlink(link, clnt->cl_debugfs, name);
  119. (*nump)++;
  120. return 0;
  121. }
  122. void
  123. rpc_clnt_debugfs_register(struct rpc_clnt *clnt)
  124. {
  125. int len;
  126. char name[9]; /* enough for 8 hex digits + NULL */
  127. int xprtnum = 0;
  128. len = snprintf(name, sizeof(name), "%x", clnt->cl_clid);
  129. if (len >= sizeof(name))
  130. return;
  131. /* make the per-client dir */
  132. clnt->cl_debugfs = debugfs_create_dir(name, rpc_clnt_dir);
  133. /* make tasks file */
  134. debugfs_create_file("tasks", S_IFREG | 0400, clnt->cl_debugfs, clnt,
  135. &tasks_fops);
  136. rpc_clnt_iterate_for_each_xprt(clnt, do_xprt_debugfs, &xprtnum);
  137. }
  138. void
  139. rpc_clnt_debugfs_unregister(struct rpc_clnt *clnt)
  140. {
  141. debugfs_remove_recursive(clnt->cl_debugfs);
  142. clnt->cl_debugfs = NULL;
  143. }
  144. static int
  145. xprt_info_show(struct seq_file *f, void *v)
  146. {
  147. struct rpc_xprt *xprt = f->private;
  148. seq_printf(f, "netid: %s\n", xprt->address_strings[RPC_DISPLAY_NETID]);
  149. seq_printf(f, "addr: %s\n", xprt->address_strings[RPC_DISPLAY_ADDR]);
  150. seq_printf(f, "port: %s\n", xprt->address_strings[RPC_DISPLAY_PORT]);
  151. seq_printf(f, "state: 0x%lx\n", xprt->state);
  152. return 0;
  153. }
  154. static int
  155. xprt_info_open(struct inode *inode, struct file *filp)
  156. {
  157. int ret;
  158. struct rpc_xprt *xprt = inode->i_private;
  159. ret = single_open(filp, xprt_info_show, xprt);
  160. if (!ret) {
  161. if (!xprt_get(xprt)) {
  162. single_release(inode, filp);
  163. ret = -EINVAL;
  164. }
  165. }
  166. return ret;
  167. }
  168. static int
  169. xprt_info_release(struct inode *inode, struct file *filp)
  170. {
  171. struct rpc_xprt *xprt = inode->i_private;
  172. xprt_put(xprt);
  173. return single_release(inode, filp);
  174. }
  175. static const struct file_operations xprt_info_fops = {
  176. .owner = THIS_MODULE,
  177. .open = xprt_info_open,
  178. .read = seq_read,
  179. .llseek = seq_lseek,
  180. .release = xprt_info_release,
  181. };
  182. void
  183. rpc_xprt_debugfs_register(struct rpc_xprt *xprt)
  184. {
  185. int len, id;
  186. static atomic_t cur_id;
  187. char name[9]; /* 8 hex digits + NULL term */
  188. id = (unsigned int)atomic_inc_return(&cur_id);
  189. len = snprintf(name, sizeof(name), "%x", id);
  190. if (len >= sizeof(name))
  191. return;
  192. /* make the per-client dir */
  193. xprt->debugfs = debugfs_create_dir(name, rpc_xprt_dir);
  194. /* make tasks file */
  195. debugfs_create_file("info", S_IFREG | 0400, xprt->debugfs, xprt,
  196. &xprt_info_fops);
  197. }
  198. void
  199. rpc_xprt_debugfs_unregister(struct rpc_xprt *xprt)
  200. {
  201. debugfs_remove_recursive(xprt->debugfs);
  202. xprt->debugfs = NULL;
  203. }
  204. #if IS_ENABLED(CONFIG_FAIL_SUNRPC)
  205. struct fail_sunrpc_attr fail_sunrpc = {
  206. .attr = FAULT_ATTR_INITIALIZER,
  207. };
  208. EXPORT_SYMBOL_GPL(fail_sunrpc);
  209. static void fail_sunrpc_init(void)
  210. {
  211. struct dentry *dir;
  212. dir = fault_create_debugfs_attr("fail_sunrpc", NULL,
  213. &fail_sunrpc.attr);
  214. debugfs_create_bool("ignore-client-disconnect", S_IFREG | 0600, dir,
  215. &fail_sunrpc.ignore_client_disconnect);
  216. debugfs_create_bool("ignore-server-disconnect", S_IFREG | 0600, dir,
  217. &fail_sunrpc.ignore_server_disconnect);
  218. debugfs_create_bool("ignore-cache-wait", S_IFREG | 0600, dir,
  219. &fail_sunrpc.ignore_cache_wait);
  220. }
  221. #else
  222. static void fail_sunrpc_init(void)
  223. {
  224. }
  225. #endif
  226. void __exit
  227. sunrpc_debugfs_exit(void)
  228. {
  229. debugfs_remove_recursive(topdir);
  230. topdir = NULL;
  231. rpc_clnt_dir = NULL;
  232. rpc_xprt_dir = NULL;
  233. }
  234. void __init
  235. sunrpc_debugfs_init(void)
  236. {
  237. topdir = debugfs_create_dir("sunrpc", NULL);
  238. rpc_clnt_dir = debugfs_create_dir("rpc_clnt", topdir);
  239. rpc_xprt_dir = debugfs_create_dir("rpc_xprt", topdir);
  240. fail_sunrpc_init();
  241. }