fault_inject.c 3.7 KB

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