fault_inject.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2011 Bryan Schumaker <bjschuma@netapp.com>
  4. *
  5. * Uses debugfs to create fault injection points for client testing
  6. */
  7. #include <linux/types.h>
  8. #include <linux/fs.h>
  9. #include <linux/debugfs.h>
  10. #include <linux/module.h>
  11. #include <linux/nsproxy.h>
  12. #include <linux/sunrpc/addr.h>
  13. #include <linux/uaccess.h>
  14. #include <linux/kernel.h>
  15. #include "state.h"
  16. #include "netns.h"
  17. struct nfsd_fault_inject_op {
  18. char *file;
  19. u64 (*get)(void);
  20. u64 (*set_val)(u64);
  21. u64 (*set_clnt)(struct sockaddr_storage *, size_t);
  22. };
  23. static struct dentry *debug_dir;
  24. static ssize_t fault_inject_read(struct file *file, char __user *buf,
  25. size_t len, loff_t *ppos)
  26. {
  27. static u64 val;
  28. char read_buf[25];
  29. size_t size;
  30. loff_t pos = *ppos;
  31. struct nfsd_fault_inject_op *op = file_inode(file)->i_private;
  32. if (!pos)
  33. val = op->get();
  34. size = scnprintf(read_buf, sizeof(read_buf), "%llu\n", val);
  35. return simple_read_from_buffer(buf, len, ppos, read_buf, size);
  36. }
  37. static ssize_t fault_inject_write(struct file *file, const char __user *buf,
  38. size_t len, loff_t *ppos)
  39. {
  40. char write_buf[INET6_ADDRSTRLEN];
  41. size_t size = min(sizeof(write_buf) - 1, len);
  42. struct net *net = current->nsproxy->net_ns;
  43. struct sockaddr_storage sa;
  44. struct nfsd_fault_inject_op *op = file_inode(file)->i_private;
  45. u64 val;
  46. char *nl;
  47. if (copy_from_user(write_buf, buf, size))
  48. return -EFAULT;
  49. write_buf[size] = '\0';
  50. /* Deal with any embedded newlines in the string */
  51. nl = strchr(write_buf, '\n');
  52. if (nl) {
  53. size = nl - write_buf;
  54. *nl = '\0';
  55. }
  56. size = rpc_pton(net, write_buf, size, (struct sockaddr *)&sa, sizeof(sa));
  57. if (size > 0) {
  58. val = op->set_clnt(&sa, size);
  59. if (val)
  60. pr_info("NFSD [%s]: Client %s had %llu state object(s)\n",
  61. op->file, write_buf, val);
  62. } else {
  63. val = simple_strtoll(write_buf, NULL, 0);
  64. if (val == 0)
  65. pr_info("NFSD Fault Injection: %s (all)", op->file);
  66. else
  67. pr_info("NFSD Fault Injection: %s (n = %llu)",
  68. op->file, val);
  69. val = op->set_val(val);
  70. pr_info("NFSD: %s: found %llu", op->file, val);
  71. }
  72. return len; /* on success, claim we got the whole input */
  73. }
  74. static const struct file_operations fops_nfsd = {
  75. .owner = THIS_MODULE,
  76. .read = fault_inject_read,
  77. .write = fault_inject_write,
  78. };
  79. void nfsd_fault_inject_cleanup(void)
  80. {
  81. debugfs_remove_recursive(debug_dir);
  82. }
  83. static struct nfsd_fault_inject_op inject_ops[] = {
  84. {
  85. .file = "forget_clients",
  86. .get = nfsd_inject_print_clients,
  87. .set_val = nfsd_inject_forget_clients,
  88. .set_clnt = nfsd_inject_forget_client,
  89. },
  90. {
  91. .file = "forget_locks",
  92. .get = nfsd_inject_print_locks,
  93. .set_val = nfsd_inject_forget_locks,
  94. .set_clnt = nfsd_inject_forget_client_locks,
  95. },
  96. {
  97. .file = "forget_openowners",
  98. .get = nfsd_inject_print_openowners,
  99. .set_val = nfsd_inject_forget_openowners,
  100. .set_clnt = nfsd_inject_forget_client_openowners,
  101. },
  102. {
  103. .file = "forget_delegations",
  104. .get = nfsd_inject_print_delegations,
  105. .set_val = nfsd_inject_forget_delegations,
  106. .set_clnt = nfsd_inject_forget_client_delegations,
  107. },
  108. {
  109. .file = "recall_delegations",
  110. .get = nfsd_inject_print_delegations,
  111. .set_val = nfsd_inject_recall_delegations,
  112. .set_clnt = nfsd_inject_recall_client_delegations,
  113. },
  114. };
  115. void nfsd_fault_inject_init(void)
  116. {
  117. unsigned int i;
  118. struct nfsd_fault_inject_op *op;
  119. umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
  120. debug_dir = debugfs_create_dir("nfsd", NULL);
  121. for (i = 0; i < ARRAY_SIZE(inject_ops); i++) {
  122. op = &inject_ops[i];
  123. debugfs_create_file(op->file, mode, debug_dir, op, &fops_nfsd);
  124. }
  125. }