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; |
Bryan Schumaker | 8ce54e0 | 2012-11-29 11:40:43 -0500 | [diff] [blame] | 20 | u64 (*forget)(struct nfs4_client *, u64); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 21 | u64 (*print)(struct nfs4_client *, u64); |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 22 | }; |
| 23 | |
| 24 | static struct nfsd_fault_inject_op inject_ops[] = { |
| 25 | { |
| 26 | .file = "forget_clients", |
Bryan Schumaker | 8ce54e0 | 2012-11-29 11:40:43 -0500 | [diff] [blame] | 27 | .forget = nfsd_forget_client, |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 28 | .print = nfsd_print_client, |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 29 | }, |
| 30 | { |
| 31 | .file = "forget_locks", |
Bryan Schumaker | 8ce54e0 | 2012-11-29 11:40:43 -0500 | [diff] [blame] | 32 | .forget = nfsd_forget_client_locks, |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 33 | .print = nfsd_print_client_locks, |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 34 | }, |
| 35 | { |
| 36 | .file = "forget_openowners", |
Bryan Schumaker | 8ce54e0 | 2012-11-29 11:40:43 -0500 | [diff] [blame] | 37 | .forget = nfsd_forget_client_openowners, |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 38 | .print = nfsd_print_client_openowners, |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 39 | }, |
| 40 | { |
| 41 | .file = "forget_delegations", |
Bryan Schumaker | 8ce54e0 | 2012-11-29 11:40:43 -0500 | [diff] [blame] | 42 | .forget = nfsd_forget_client_delegations, |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 43 | .print = nfsd_print_client_delegations, |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 44 | }, |
| 45 | { |
| 46 | .file = "recall_delegations", |
Bryan Schumaker | 8ce54e0 | 2012-11-29 11:40:43 -0500 | [diff] [blame] | 47 | .forget = nfsd_recall_client_delegations, |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 48 | .print = nfsd_print_client_delegations, |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 49 | }, |
| 50 | }; |
| 51 | |
| 52 | static long int NUM_INJECT_OPS = sizeof(inject_ops) / sizeof(struct nfsd_fault_inject_op); |
| 53 | static struct dentry *debug_dir; |
| 54 | |
Bryan Schumaker | d7cc431 | 2012-11-29 11:40:45 -0500 | [diff] [blame] | 55 | static void nfsd_inject_set(struct nfsd_fault_inject_op *op, u64 val) |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 56 | { |
Bryan Schumaker | 8ce54e0 | 2012-11-29 11:40:43 -0500 | [diff] [blame] | 57 | u64 count = 0; |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 58 | |
| 59 | if (val == 0) |
| 60 | printk(KERN_INFO "NFSD Fault Injection: %s (all)", op->file); |
| 61 | else |
| 62 | printk(KERN_INFO "NFSD Fault Injection: %s (n = %llu)", op->file, val); |
| 63 | |
Bryan Schumaker | 0439583 | 2012-11-29 11:40:38 -0500 | [diff] [blame] | 64 | nfs4_lock_state(); |
Bryan Schumaker | 8ce54e0 | 2012-11-29 11:40:43 -0500 | [diff] [blame] | 65 | count = nfsd_for_n_state(val, op->forget); |
Bryan Schumaker | 0439583 | 2012-11-29 11:40:38 -0500 | [diff] [blame] | 66 | nfs4_unlock_state(); |
Bryan Schumaker | 8ce54e0 | 2012-11-29 11:40:43 -0500 | [diff] [blame] | 67 | printk(KERN_INFO "NFSD: %s: found %llu", op->file, count); |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 68 | } |
| 69 | |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 70 | static void nfsd_inject_set_client(struct nfsd_fault_inject_op *op, |
| 71 | struct sockaddr_storage *addr, |
| 72 | size_t addr_size) |
| 73 | { |
| 74 | char buf[INET6_ADDRSTRLEN]; |
| 75 | struct nfs4_client *clp; |
| 76 | u64 count; |
| 77 | |
| 78 | nfs4_lock_state(); |
| 79 | clp = nfsd_find_client(addr, addr_size); |
| 80 | if (clp) { |
| 81 | count = op->forget(clp, 0); |
Bryan Schumaker | 0a5c33e | 2012-12-07 16:17:28 -0500 | [diff] [blame] | 82 | rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf)); |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 83 | printk(KERN_INFO "NFSD [%s]: Client %s had %llu state object(s)\n", op->file, buf, count); |
| 84 | } |
| 85 | nfs4_unlock_state(); |
| 86 | } |
| 87 | |
Bryan Schumaker | d7cc431 | 2012-11-29 11:40:45 -0500 | [diff] [blame] | 88 | static void nfsd_inject_get(struct nfsd_fault_inject_op *op, u64 *val) |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 89 | { |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 90 | nfs4_lock_state(); |
| 91 | *val = nfsd_for_n_state(0, op->print); |
| 92 | nfs4_unlock_state(); |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 93 | } |
| 94 | |
Bryan Schumaker | d7cc431 | 2012-11-29 11:40:45 -0500 | [diff] [blame] | 95 | static ssize_t fault_inject_read(struct file *file, char __user *buf, |
| 96 | size_t len, loff_t *ppos) |
| 97 | { |
| 98 | static u64 val; |
| 99 | char read_buf[25]; |
Kinglong Mee | f3e41ec | 2014-04-08 13:04:01 +0800 | [diff] [blame] | 100 | size_t size; |
Bryan Schumaker | d7cc431 | 2012-11-29 11:40:45 -0500 | [diff] [blame] | 101 | loff_t pos = *ppos; |
| 102 | |
| 103 | if (!pos) |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 104 | nfsd_inject_get(file_inode(file)->i_private, &val); |
Bryan Schumaker | d7cc431 | 2012-11-29 11:40:45 -0500 | [diff] [blame] | 105 | size = scnprintf(read_buf, sizeof(read_buf), "%llu\n", val); |
| 106 | |
Kinglong Mee | f3e41ec | 2014-04-08 13:04:01 +0800 | [diff] [blame] | 107 | return simple_read_from_buffer(buf, len, ppos, read_buf, size); |
Bryan Schumaker | d7cc431 | 2012-11-29 11:40:45 -0500 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static ssize_t fault_inject_write(struct file *file, const char __user *buf, |
| 111 | size_t len, loff_t *ppos) |
| 112 | { |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 113 | char write_buf[INET6_ADDRSTRLEN]; |
Bryan Schumaker | 18d9a2c | 2012-12-07 16:17:29 -0500 | [diff] [blame] | 114 | size_t size = min(sizeof(write_buf) - 1, len); |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 115 | struct net *net = current->nsproxy->net_ns; |
| 116 | struct sockaddr_storage sa; |
Bryan Schumaker | d7cc431 | 2012-11-29 11:40:45 -0500 | [diff] [blame] | 117 | u64 val; |
| 118 | |
| 119 | if (copy_from_user(write_buf, buf, size)) |
| 120 | return -EFAULT; |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 121 | write_buf[size] = '\0'; |
Bryan Schumaker | d7cc431 | 2012-11-29 11:40:45 -0500 | [diff] [blame] | 122 | |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 123 | size = rpc_pton(net, write_buf, size, (struct sockaddr *)&sa, sizeof(sa)); |
| 124 | if (size > 0) |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 125 | nfsd_inject_set_client(file_inode(file)->i_private, &sa, size); |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 126 | else { |
| 127 | val = simple_strtoll(write_buf, NULL, 0); |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 128 | nfsd_inject_set(file_inode(file)->i_private, val); |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 129 | } |
Bryan Schumaker | d7cc431 | 2012-11-29 11:40:45 -0500 | [diff] [blame] | 130 | return len; /* on success, claim we got the whole input */ |
| 131 | } |
| 132 | |
| 133 | static const struct file_operations fops_nfsd = { |
| 134 | .owner = THIS_MODULE, |
| 135 | .read = fault_inject_read, |
| 136 | .write = fault_inject_write, |
| 137 | }; |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 138 | |
| 139 | void nfsd_fault_inject_cleanup(void) |
| 140 | { |
| 141 | debugfs_remove_recursive(debug_dir); |
| 142 | } |
| 143 | |
| 144 | int nfsd_fault_inject_init(void) |
| 145 | { |
| 146 | unsigned int i; |
| 147 | struct nfsd_fault_inject_op *op; |
Al Viro | 8818739 | 2012-03-20 06:00:24 -0400 | [diff] [blame] | 148 | umode_t mode = S_IFREG | S_IRUSR | S_IWUSR; |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 149 | |
| 150 | debug_dir = debugfs_create_dir("nfsd", NULL); |
| 151 | if (!debug_dir) |
| 152 | goto fail; |
| 153 | |
| 154 | for (i = 0; i < NUM_INJECT_OPS; i++) { |
| 155 | op = &inject_ops[i]; |
| 156 | if (!debugfs_create_file(op->file, mode, debug_dir, op, &fops_nfsd)) |
| 157 | goto fail; |
| 158 | } |
| 159 | return 0; |
| 160 | |
| 161 | fail: |
| 162 | nfsd_fault_inject_cleanup(); |
| 163 | return -ENOMEM; |
| 164 | } |