]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
65178db4 BS |
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> | |
6c1e82a4 | 12 | #include <linux/nsproxy.h> |
5976687a | 13 | #include <linux/sunrpc/addr.h> |
7c0f6ba6 | 14 | #include <linux/uaccess.h> |
65178db4 BS |
15 | |
16 | #include "state.h" | |
6c1e82a4 | 17 | #include "netns.h" |
65178db4 BS |
18 | |
19 | struct nfsd_fault_inject_op { | |
20 | char *file; | |
285abdee JL |
21 | u64 (*get)(void); |
22 | u64 (*set_val)(u64); | |
23 | u64 (*set_clnt)(struct sockaddr_storage *, size_t); | |
65178db4 BS |
24 | }; |
25 | ||
65178db4 BS |
26 | static struct dentry *debug_dir; |
27 | ||
d7cc431e BS |
28 | static ssize_t fault_inject_read(struct file *file, char __user *buf, |
29 | size_t len, loff_t *ppos) | |
30 | { | |
31 | static u64 val; | |
32 | char read_buf[25]; | |
f3e41ec5 | 33 | size_t size; |
d7cc431e | 34 | loff_t pos = *ppos; |
c96223d3 | 35 | struct nfsd_fault_inject_op *op = file_inode(file)->i_private; |
d7cc431e BS |
36 | |
37 | if (!pos) | |
285abdee | 38 | val = op->get(); |
d7cc431e BS |
39 | size = scnprintf(read_buf, sizeof(read_buf), "%llu\n", val); |
40 | ||
f3e41ec5 | 41 | return simple_read_from_buffer(buf, len, ppos, read_buf, size); |
d7cc431e BS |
42 | } |
43 | ||
44 | static ssize_t fault_inject_write(struct file *file, const char __user *buf, | |
45 | size_t len, loff_t *ppos) | |
46 | { | |
6c1e82a4 | 47 | char write_buf[INET6_ADDRSTRLEN]; |
18d9a2ca | 48 | size_t size = min(sizeof(write_buf) - 1, len); |
6c1e82a4 BS |
49 | struct net *net = current->nsproxy->net_ns; |
50 | struct sockaddr_storage sa; | |
c96223d3 | 51 | struct nfsd_fault_inject_op *op = file_inode(file)->i_private; |
d7cc431e | 52 | u64 val; |
d4c8e34f | 53 | char *nl; |
d7cc431e BS |
54 | |
55 | if (copy_from_user(write_buf, buf, size)) | |
56 | return -EFAULT; | |
6c1e82a4 BS |
57 | write_buf[size] = '\0'; |
58 | ||
d4c8e34f JL |
59 | /* Deal with any embedded newlines in the string */ |
60 | nl = strchr(write_buf, '\n'); | |
61 | if (nl) { | |
62 | size = nl - write_buf; | |
63 | *nl = '\0'; | |
64 | } | |
65 | ||
6c1e82a4 | 66 | size = rpc_pton(net, write_buf, size, (struct sockaddr *)&sa, sizeof(sa)); |
c96223d3 | 67 | if (size > 0) { |
285abdee | 68 | val = op->set_clnt(&sa, size); |
c96223d3 JL |
69 | if (val) |
70 | pr_info("NFSD [%s]: Client %s had %llu state object(s)\n", | |
71 | op->file, write_buf, val); | |
72 | } else { | |
6c1e82a4 | 73 | val = simple_strtoll(write_buf, NULL, 0); |
c96223d3 JL |
74 | if (val == 0) |
75 | pr_info("NFSD Fault Injection: %s (all)", op->file); | |
76 | else | |
77 | pr_info("NFSD Fault Injection: %s (n = %llu)", | |
78 | op->file, val); | |
285abdee | 79 | val = op->set_val(val); |
c96223d3 | 80 | pr_info("NFSD: %s: found %llu", op->file, val); |
6c1e82a4 | 81 | } |
d7cc431e BS |
82 | return len; /* on success, claim we got the whole input */ |
83 | } | |
84 | ||
85 | static const struct file_operations fops_nfsd = { | |
86 | .owner = THIS_MODULE, | |
87 | .read = fault_inject_read, | |
88 | .write = fault_inject_write, | |
89 | }; | |
65178db4 BS |
90 | |
91 | void nfsd_fault_inject_cleanup(void) | |
92 | { | |
93 | debugfs_remove_recursive(debug_dir); | |
94 | } | |
95 | ||
c96223d3 JL |
96 | static struct nfsd_fault_inject_op inject_ops[] = { |
97 | { | |
98 | .file = "forget_clients", | |
7ec0e36f | 99 | .get = nfsd_inject_print_clients, |
69fc9edf | 100 | .set_val = nfsd_inject_forget_clients, |
a0926d15 | 101 | .set_clnt = nfsd_inject_forget_client, |
c96223d3 JL |
102 | }, |
103 | { | |
104 | .file = "forget_locks", | |
016200c3 JL |
105 | .get = nfsd_inject_print_locks, |
106 | .set_val = nfsd_inject_forget_locks, | |
107 | .set_clnt = nfsd_inject_forget_client_locks, | |
c96223d3 JL |
108 | }, |
109 | { | |
110 | .file = "forget_openowners", | |
82e05efa JL |
111 | .get = nfsd_inject_print_openowners, |
112 | .set_val = nfsd_inject_forget_openowners, | |
113 | .set_clnt = nfsd_inject_forget_client_openowners, | |
c96223d3 JL |
114 | }, |
115 | { | |
116 | .file = "forget_delegations", | |
98d5c7c5 JL |
117 | .get = nfsd_inject_print_delegations, |
118 | .set_val = nfsd_inject_forget_delegations, | |
119 | .set_clnt = nfsd_inject_forget_client_delegations, | |
c96223d3 JL |
120 | }, |
121 | { | |
122 | .file = "recall_delegations", | |
98d5c7c5 JL |
123 | .get = nfsd_inject_print_delegations, |
124 | .set_val = nfsd_inject_recall_delegations, | |
125 | .set_clnt = nfsd_inject_recall_client_delegations, | |
c96223d3 JL |
126 | }, |
127 | }; | |
128 | ||
129 | #define NUM_INJECT_OPS (sizeof(inject_ops)/sizeof(struct nfsd_fault_inject_op)) | |
130 | ||
65178db4 BS |
131 | int nfsd_fault_inject_init(void) |
132 | { | |
133 | unsigned int i; | |
134 | struct nfsd_fault_inject_op *op; | |
88187398 | 135 | umode_t mode = S_IFREG | S_IRUSR | S_IWUSR; |
65178db4 BS |
136 | |
137 | debug_dir = debugfs_create_dir("nfsd", NULL); | |
138 | if (!debug_dir) | |
139 | goto fail; | |
140 | ||
141 | for (i = 0; i < NUM_INJECT_OPS; i++) { | |
142 | op = &inject_ops[i]; | |
143 | if (!debugfs_create_file(op->file, mode, debug_dir, op, &fops_nfsd)) | |
144 | goto fail; | |
145 | } | |
146 | return 0; | |
147 | ||
148 | fail: | |
149 | nfsd_fault_inject_cleanup(); | |
150 | return -ENOMEM; | |
151 | } |