Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 Bryan Schumaker <bjschuma@netapp.com> |
| 3 | * |
| 4 | * Uses debugfs to create fault injection points for client testing |
| 5 | */ |
| 6 | |
| 7 | #include <linux/types.h> |
| 8 | #include <linux/fs.h> |
| 9 | #include <linux/debugfs.h> |
| 10 | #include <linux/module.h> |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 11 | #include <linux/nsproxy.h> |
Jeff Layton | 5976687 | 2013-02-04 12:50:00 -0500 | [diff] [blame] | 12 | #include <linux/sunrpc/addr.h> |
Bryan Schumaker | d7cc431 | 2012-11-29 11:40:45 -0500 | [diff] [blame] | 13 | #include <asm/uaccess.h> |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 14 | |
| 15 | #include "state.h" |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 16 | #include "netns.h" |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 17 | |
| 18 | struct nfsd_fault_inject_op { |
| 19 | char *file; |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 20 | u64 (*get)(struct nfsd_fault_inject_op *); |
| 21 | u64 (*set_val)(struct nfsd_fault_inject_op *, u64); |
| 22 | u64 (*set_clnt)(struct nfsd_fault_inject_op *, |
| 23 | struct sockaddr_storage *, size_t); |
Bryan Schumaker | 8ce54e0 | 2012-11-29 11:40:43 -0500 | [diff] [blame] | 24 | u64 (*forget)(struct nfs4_client *, u64); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 25 | u64 (*print)(struct nfs4_client *, u64); |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 26 | }; |
| 27 | |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 28 | static struct dentry *debug_dir; |
| 29 | |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 30 | static u64 nfsd_inject_set(struct nfsd_fault_inject_op *op, u64 val) |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 31 | { |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 32 | u64 count; |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 33 | |
Bryan Schumaker | 0439583 | 2012-11-29 11:40:38 -0500 | [diff] [blame] | 34 | nfs4_lock_state(); |
Bryan Schumaker | 8ce54e0 | 2012-11-29 11:40:43 -0500 | [diff] [blame] | 35 | count = nfsd_for_n_state(val, op->forget); |
Bryan Schumaker | 0439583 | 2012-11-29 11:40:38 -0500 | [diff] [blame] | 36 | nfs4_unlock_state(); |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 37 | return count; |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 38 | } |
| 39 | |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 40 | static u64 nfsd_inject_set_client(struct nfsd_fault_inject_op *op, |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 41 | struct sockaddr_storage *addr, |
| 42 | size_t addr_size) |
| 43 | { |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 44 | struct nfs4_client *clp; |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 45 | u64 count = 0; |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 46 | |
| 47 | nfs4_lock_state(); |
| 48 | clp = nfsd_find_client(addr, addr_size); |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 49 | if (clp) |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 50 | count = op->forget(clp, 0); |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 51 | nfs4_unlock_state(); |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 52 | return count; |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 53 | } |
| 54 | |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 55 | static u64 nfsd_inject_get(struct nfsd_fault_inject_op *op) |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 56 | { |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 57 | u64 count; |
| 58 | |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 59 | nfs4_lock_state(); |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 60 | count = nfsd_for_n_state(0, op->print); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 61 | nfs4_unlock_state(); |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 62 | |
| 63 | return count; |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 64 | } |
| 65 | |
Bryan Schumaker | d7cc431 | 2012-11-29 11:40:45 -0500 | [diff] [blame] | 66 | static ssize_t fault_inject_read(struct file *file, char __user *buf, |
| 67 | size_t len, loff_t *ppos) |
| 68 | { |
| 69 | static u64 val; |
| 70 | char read_buf[25]; |
Kinglong Mee | f3e41ec | 2014-04-08 13:04:01 +0800 | [diff] [blame] | 71 | size_t size; |
Bryan Schumaker | d7cc431 | 2012-11-29 11:40:45 -0500 | [diff] [blame] | 72 | loff_t pos = *ppos; |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 73 | struct nfsd_fault_inject_op *op = file_inode(file)->i_private; |
Bryan Schumaker | d7cc431 | 2012-11-29 11:40:45 -0500 | [diff] [blame] | 74 | |
| 75 | if (!pos) |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 76 | val = op->get(op); |
Bryan Schumaker | d7cc431 | 2012-11-29 11:40:45 -0500 | [diff] [blame] | 77 | size = scnprintf(read_buf, sizeof(read_buf), "%llu\n", val); |
| 78 | |
Kinglong Mee | f3e41ec | 2014-04-08 13:04:01 +0800 | [diff] [blame] | 79 | return simple_read_from_buffer(buf, len, ppos, read_buf, size); |
Bryan Schumaker | d7cc431 | 2012-11-29 11:40:45 -0500 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static ssize_t fault_inject_write(struct file *file, const char __user *buf, |
| 83 | size_t len, loff_t *ppos) |
| 84 | { |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 85 | char write_buf[INET6_ADDRSTRLEN]; |
Bryan Schumaker | 18d9a2c | 2012-12-07 16:17:29 -0500 | [diff] [blame] | 86 | size_t size = min(sizeof(write_buf) - 1, len); |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 87 | struct net *net = current->nsproxy->net_ns; |
| 88 | struct sockaddr_storage sa; |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 89 | struct nfsd_fault_inject_op *op = file_inode(file)->i_private; |
Bryan Schumaker | d7cc431 | 2012-11-29 11:40:45 -0500 | [diff] [blame] | 90 | u64 val; |
Jeff Layton | d4c8e34 | 2014-06-18 15:00:19 -0400 | [diff] [blame] | 91 | char *nl; |
Bryan Schumaker | d7cc431 | 2012-11-29 11:40:45 -0500 | [diff] [blame] | 92 | |
| 93 | if (copy_from_user(write_buf, buf, size)) |
| 94 | return -EFAULT; |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 95 | write_buf[size] = '\0'; |
Bryan Schumaker | d7cc431 | 2012-11-29 11:40:45 -0500 | [diff] [blame] | 96 | |
Jeff Layton | d4c8e34 | 2014-06-18 15:00:19 -0400 | [diff] [blame] | 97 | /* Deal with any embedded newlines in the string */ |
| 98 | nl = strchr(write_buf, '\n'); |
| 99 | if (nl) { |
| 100 | size = nl - write_buf; |
| 101 | *nl = '\0'; |
| 102 | } |
| 103 | |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 104 | size = rpc_pton(net, write_buf, size, (struct sockaddr *)&sa, sizeof(sa)); |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 105 | if (size > 0) { |
| 106 | val = op->set_clnt(op, &sa, size); |
| 107 | if (val) |
| 108 | pr_info("NFSD [%s]: Client %s had %llu state object(s)\n", |
| 109 | op->file, write_buf, val); |
| 110 | } else { |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 111 | val = simple_strtoll(write_buf, NULL, 0); |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 112 | if (val == 0) |
| 113 | pr_info("NFSD Fault Injection: %s (all)", op->file); |
| 114 | else |
| 115 | pr_info("NFSD Fault Injection: %s (n = %llu)", |
| 116 | op->file, val); |
| 117 | val = op->set_val(op, val); |
| 118 | pr_info("NFSD: %s: found %llu", op->file, val); |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 119 | } |
Bryan Schumaker | d7cc431 | 2012-11-29 11:40:45 -0500 | [diff] [blame] | 120 | return len; /* on success, claim we got the whole input */ |
| 121 | } |
| 122 | |
| 123 | static const struct file_operations fops_nfsd = { |
| 124 | .owner = THIS_MODULE, |
| 125 | .read = fault_inject_read, |
| 126 | .write = fault_inject_write, |
| 127 | }; |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 128 | |
| 129 | void nfsd_fault_inject_cleanup(void) |
| 130 | { |
| 131 | debugfs_remove_recursive(debug_dir); |
| 132 | } |
| 133 | |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 134 | static struct nfsd_fault_inject_op inject_ops[] = { |
| 135 | { |
| 136 | .file = "forget_clients", |
Jeff Layton | 7ec0e36 | 2014-07-30 08:27:17 -0400 | [diff] [blame] | 137 | .get = nfsd_inject_print_clients, |
Jeff Layton | 69fc9ed | 2014-07-30 08:27:19 -0400 | [diff] [blame] | 138 | .set_val = nfsd_inject_forget_clients, |
Jeff Layton | a0926d1 | 2014-07-30 08:27:18 -0400 | [diff] [blame] | 139 | .set_clnt = nfsd_inject_forget_client, |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 140 | }, |
| 141 | { |
| 142 | .file = "forget_locks", |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 143 | .get = nfsd_inject_print_locks, |
| 144 | .set_val = nfsd_inject_forget_locks, |
| 145 | .set_clnt = nfsd_inject_forget_client_locks, |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 146 | }, |
| 147 | { |
| 148 | .file = "forget_openowners", |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame^] | 149 | .get = nfsd_inject_print_openowners, |
| 150 | .set_val = nfsd_inject_forget_openowners, |
| 151 | .set_clnt = nfsd_inject_forget_client_openowners, |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 152 | }, |
| 153 | { |
| 154 | .file = "forget_delegations", |
| 155 | .get = nfsd_inject_get, |
| 156 | .set_val = nfsd_inject_set, |
| 157 | .set_clnt = nfsd_inject_set_client, |
| 158 | .forget = nfsd_forget_client_delegations, |
| 159 | .print = nfsd_print_client_delegations, |
| 160 | }, |
| 161 | { |
| 162 | .file = "recall_delegations", |
| 163 | .get = nfsd_inject_get, |
| 164 | .set_val = nfsd_inject_set, |
| 165 | .set_clnt = nfsd_inject_set_client, |
| 166 | .forget = nfsd_recall_client_delegations, |
| 167 | .print = nfsd_print_client_delegations, |
| 168 | }, |
| 169 | }; |
| 170 | |
| 171 | #define NUM_INJECT_OPS (sizeof(inject_ops)/sizeof(struct nfsd_fault_inject_op)) |
| 172 | |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 173 | int nfsd_fault_inject_init(void) |
| 174 | { |
| 175 | unsigned int i; |
| 176 | struct nfsd_fault_inject_op *op; |
Al Viro | 8818739 | 2012-03-20 06:00:24 -0400 | [diff] [blame] | 177 | umode_t mode = S_IFREG | S_IRUSR | S_IWUSR; |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 178 | |
| 179 | debug_dir = debugfs_create_dir("nfsd", NULL); |
| 180 | if (!debug_dir) |
| 181 | goto fail; |
| 182 | |
| 183 | for (i = 0; i < NUM_INJECT_OPS; i++) { |
| 184 | op = &inject_ops[i]; |
| 185 | if (!debugfs_create_file(op->file, mode, debug_dir, op, &fops_nfsd)) |
| 186 | goto fail; |
| 187 | } |
| 188 | return 0; |
| 189 | |
| 190 | fail: |
| 191 | nfsd_fault_inject_cleanup(); |
| 192 | return -ENOMEM; |
| 193 | } |