Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2011 Bryan Schumaker <bjschuma@netapp.com> |
| 4 | * |
| 5 | * Uses debugfs to create fault injection points for client testing |
| 6 | */ |
| 7 | |
| 8 | #include <linux/types.h> |
| 9 | #include <linux/fs.h> |
| 10 | #include <linux/debugfs.h> |
| 11 | #include <linux/module.h> |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 12 | #include <linux/nsproxy.h> |
Jeff Layton | 5976687 | 2013-02-04 12:50:00 -0500 | [diff] [blame] | 13 | #include <linux/sunrpc/addr.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 14 | #include <linux/uaccess.h> |
Jérémy Lefaure | a133552 | 2017-10-01 15:30:47 -0400 | [diff] [blame] | 15 | #include <linux/kernel.h> |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 16 | |
| 17 | #include "state.h" |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 18 | #include "netns.h" |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 19 | |
| 20 | struct nfsd_fault_inject_op { |
| 21 | char *file; |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 22 | u64 (*get)(void); |
| 23 | u64 (*set_val)(u64); |
| 24 | u64 (*set_clnt)(struct sockaddr_storage *, size_t); |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 25 | }; |
| 26 | |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 27 | static struct dentry *debug_dir; |
| 28 | |
Bryan Schumaker | d7cc431 | 2012-11-29 11:40:45 -0500 | [diff] [blame] | 29 | static ssize_t fault_inject_read(struct file *file, char __user *buf, |
| 30 | size_t len, loff_t *ppos) |
| 31 | { |
| 32 | static u64 val; |
| 33 | char read_buf[25]; |
Kinglong Mee | f3e41ec | 2014-04-08 13:04:01 +0800 | [diff] [blame] | 34 | size_t size; |
Bryan Schumaker | d7cc431 | 2012-11-29 11:40:45 -0500 | [diff] [blame] | 35 | loff_t pos = *ppos; |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 36 | struct nfsd_fault_inject_op *op = file_inode(file)->i_private; |
Bryan Schumaker | d7cc431 | 2012-11-29 11:40:45 -0500 | [diff] [blame] | 37 | |
| 38 | if (!pos) |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 39 | val = op->get(); |
Bryan Schumaker | d7cc431 | 2012-11-29 11:40:45 -0500 | [diff] [blame] | 40 | size = scnprintf(read_buf, sizeof(read_buf), "%llu\n", val); |
| 41 | |
Kinglong Mee | f3e41ec | 2014-04-08 13:04:01 +0800 | [diff] [blame] | 42 | return simple_read_from_buffer(buf, len, ppos, read_buf, size); |
Bryan Schumaker | d7cc431 | 2012-11-29 11:40:45 -0500 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | static ssize_t fault_inject_write(struct file *file, const char __user *buf, |
| 46 | size_t len, loff_t *ppos) |
| 47 | { |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 48 | char write_buf[INET6_ADDRSTRLEN]; |
Bryan Schumaker | 18d9a2c | 2012-12-07 16:17:29 -0500 | [diff] [blame] | 49 | size_t size = min(sizeof(write_buf) - 1, len); |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 50 | struct net *net = current->nsproxy->net_ns; |
| 51 | struct sockaddr_storage sa; |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 52 | struct nfsd_fault_inject_op *op = file_inode(file)->i_private; |
Bryan Schumaker | d7cc431 | 2012-11-29 11:40:45 -0500 | [diff] [blame] | 53 | u64 val; |
Jeff Layton | d4c8e34 | 2014-06-18 15:00:19 -0400 | [diff] [blame] | 54 | char *nl; |
Bryan Schumaker | d7cc431 | 2012-11-29 11:40:45 -0500 | [diff] [blame] | 55 | |
| 56 | if (copy_from_user(write_buf, buf, size)) |
| 57 | return -EFAULT; |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 58 | write_buf[size] = '\0'; |
Bryan Schumaker | d7cc431 | 2012-11-29 11:40:45 -0500 | [diff] [blame] | 59 | |
Jeff Layton | d4c8e34 | 2014-06-18 15:00:19 -0400 | [diff] [blame] | 60 | /* Deal with any embedded newlines in the string */ |
| 61 | nl = strchr(write_buf, '\n'); |
| 62 | if (nl) { |
| 63 | size = nl - write_buf; |
| 64 | *nl = '\0'; |
| 65 | } |
| 66 | |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 67 | size = rpc_pton(net, write_buf, size, (struct sockaddr *)&sa, sizeof(sa)); |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 68 | if (size > 0) { |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 69 | val = op->set_clnt(&sa, size); |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 70 | if (val) |
| 71 | pr_info("NFSD [%s]: Client %s had %llu state object(s)\n", |
| 72 | op->file, write_buf, val); |
| 73 | } else { |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 74 | val = simple_strtoll(write_buf, NULL, 0); |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 75 | if (val == 0) |
| 76 | pr_info("NFSD Fault Injection: %s (all)", op->file); |
| 77 | else |
| 78 | pr_info("NFSD Fault Injection: %s (n = %llu)", |
| 79 | op->file, val); |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 80 | val = op->set_val(val); |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 81 | pr_info("NFSD: %s: found %llu", op->file, val); |
Bryan Schumaker | 6c1e82a | 2012-11-29 11:40:46 -0500 | [diff] [blame] | 82 | } |
Bryan Schumaker | d7cc431 | 2012-11-29 11:40:45 -0500 | [diff] [blame] | 83 | return len; /* on success, claim we got the whole input */ |
| 84 | } |
| 85 | |
| 86 | static const struct file_operations fops_nfsd = { |
| 87 | .owner = THIS_MODULE, |
| 88 | .read = fault_inject_read, |
| 89 | .write = fault_inject_write, |
| 90 | }; |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 91 | |
| 92 | void nfsd_fault_inject_cleanup(void) |
| 93 | { |
| 94 | debugfs_remove_recursive(debug_dir); |
| 95 | } |
| 96 | |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 97 | static struct nfsd_fault_inject_op inject_ops[] = { |
| 98 | { |
| 99 | .file = "forget_clients", |
Jeff Layton | 7ec0e36 | 2014-07-30 08:27:17 -0400 | [diff] [blame] | 100 | .get = nfsd_inject_print_clients, |
Jeff Layton | 69fc9ed | 2014-07-30 08:27:19 -0400 | [diff] [blame] | 101 | .set_val = nfsd_inject_forget_clients, |
Jeff Layton | a0926d1 | 2014-07-30 08:27:18 -0400 | [diff] [blame] | 102 | .set_clnt = nfsd_inject_forget_client, |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 103 | }, |
| 104 | { |
| 105 | .file = "forget_locks", |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 106 | .get = nfsd_inject_print_locks, |
| 107 | .set_val = nfsd_inject_forget_locks, |
| 108 | .set_clnt = nfsd_inject_forget_client_locks, |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 109 | }, |
| 110 | { |
| 111 | .file = "forget_openowners", |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 112 | .get = nfsd_inject_print_openowners, |
| 113 | .set_val = nfsd_inject_forget_openowners, |
| 114 | .set_clnt = nfsd_inject_forget_client_openowners, |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 115 | }, |
| 116 | { |
| 117 | .file = "forget_delegations", |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 118 | .get = nfsd_inject_print_delegations, |
| 119 | .set_val = nfsd_inject_forget_delegations, |
| 120 | .set_clnt = nfsd_inject_forget_client_delegations, |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 121 | }, |
| 122 | { |
| 123 | .file = "recall_delegations", |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 124 | .get = nfsd_inject_print_delegations, |
| 125 | .set_val = nfsd_inject_recall_delegations, |
| 126 | .set_clnt = nfsd_inject_recall_client_delegations, |
Jeff Layton | c96223d | 2014-07-30 08:27:16 -0400 | [diff] [blame] | 127 | }, |
| 128 | }; |
| 129 | |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 130 | int nfsd_fault_inject_init(void) |
| 131 | { |
| 132 | unsigned int i; |
| 133 | struct nfsd_fault_inject_op *op; |
Al Viro | 8818739 | 2012-03-20 06:00:24 -0400 | [diff] [blame] | 134 | umode_t mode = S_IFREG | S_IRUSR | S_IWUSR; |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 135 | |
| 136 | debug_dir = debugfs_create_dir("nfsd", NULL); |
| 137 | if (!debug_dir) |
| 138 | goto fail; |
| 139 | |
Jérémy Lefaure | a133552 | 2017-10-01 15:30:47 -0400 | [diff] [blame] | 140 | for (i = 0; i < ARRAY_SIZE(inject_ops); i++) { |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 141 | op = &inject_ops[i]; |
| 142 | if (!debugfs_create_file(op->file, mode, debug_dir, op, &fops_nfsd)) |
| 143 | goto fail; |
| 144 | } |
| 145 | return 0; |
| 146 | |
| 147 | fail: |
| 148 | nfsd_fault_inject_cleanup(); |
| 149 | return -ENOMEM; |
| 150 | } |