blob: f1333fc35b3305976d15b718b45418dd1bd7a52c [file] [log] [blame]
Bryan Schumaker65178db2011-11-01 13:35:21 -04001/*
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 Schumaker6c1e82a2012-11-29 11:40:46 -050011#include <linux/nsproxy.h>
Jeff Layton59766872013-02-04 12:50:00 -050012#include <linux/sunrpc/addr.h>
Bryan Schumakerd7cc4312012-11-29 11:40:45 -050013#include <asm/uaccess.h>
Bryan Schumaker65178db2011-11-01 13:35:21 -040014
15#include "state.h"
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -050016#include "netns.h"
Bryan Schumaker65178db2011-11-01 13:35:21 -040017
18struct nfsd_fault_inject_op {
19 char *file;
Bryan Schumaker8ce54e02012-11-29 11:40:43 -050020 u64 (*forget)(struct nfs4_client *, u64);
Bryan Schumaker184c1842012-11-29 11:40:44 -050021 u64 (*print)(struct nfs4_client *, u64);
Bryan Schumaker65178db2011-11-01 13:35:21 -040022};
23
24static struct nfsd_fault_inject_op inject_ops[] = {
25 {
26 .file = "forget_clients",
Bryan Schumaker8ce54e02012-11-29 11:40:43 -050027 .forget = nfsd_forget_client,
Bryan Schumaker184c1842012-11-29 11:40:44 -050028 .print = nfsd_print_client,
Bryan Schumaker65178db2011-11-01 13:35:21 -040029 },
30 {
31 .file = "forget_locks",
Bryan Schumaker8ce54e02012-11-29 11:40:43 -050032 .forget = nfsd_forget_client_locks,
Bryan Schumaker184c1842012-11-29 11:40:44 -050033 .print = nfsd_print_client_locks,
Bryan Schumaker65178db2011-11-01 13:35:21 -040034 },
35 {
36 .file = "forget_openowners",
Bryan Schumaker8ce54e02012-11-29 11:40:43 -050037 .forget = nfsd_forget_client_openowners,
Bryan Schumaker184c1842012-11-29 11:40:44 -050038 .print = nfsd_print_client_openowners,
Bryan Schumaker65178db2011-11-01 13:35:21 -040039 },
40 {
41 .file = "forget_delegations",
Bryan Schumaker8ce54e02012-11-29 11:40:43 -050042 .forget = nfsd_forget_client_delegations,
Bryan Schumaker184c1842012-11-29 11:40:44 -050043 .print = nfsd_print_client_delegations,
Bryan Schumaker65178db2011-11-01 13:35:21 -040044 },
45 {
46 .file = "recall_delegations",
Bryan Schumaker8ce54e02012-11-29 11:40:43 -050047 .forget = nfsd_recall_client_delegations,
Bryan Schumaker184c1842012-11-29 11:40:44 -050048 .print = nfsd_print_client_delegations,
Bryan Schumaker65178db2011-11-01 13:35:21 -040049 },
50};
51
52static long int NUM_INJECT_OPS = sizeof(inject_ops) / sizeof(struct nfsd_fault_inject_op);
53static struct dentry *debug_dir;
54
Bryan Schumakerd7cc4312012-11-29 11:40:45 -050055static void nfsd_inject_set(struct nfsd_fault_inject_op *op, u64 val)
Bryan Schumaker65178db2011-11-01 13:35:21 -040056{
Bryan Schumaker8ce54e02012-11-29 11:40:43 -050057 u64 count = 0;
Bryan Schumaker65178db2011-11-01 13:35:21 -040058
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 Schumaker04395832012-11-29 11:40:38 -050064 nfs4_lock_state();
Bryan Schumaker8ce54e02012-11-29 11:40:43 -050065 count = nfsd_for_n_state(val, op->forget);
Bryan Schumaker04395832012-11-29 11:40:38 -050066 nfs4_unlock_state();
Bryan Schumaker8ce54e02012-11-29 11:40:43 -050067 printk(KERN_INFO "NFSD: %s: found %llu", op->file, count);
Bryan Schumaker65178db2011-11-01 13:35:21 -040068}
69
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -050070static 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 Schumaker0a5c33e2012-12-07 16:17:28 -050082 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -050083 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 Schumakerd7cc4312012-11-29 11:40:45 -050088static void nfsd_inject_get(struct nfsd_fault_inject_op *op, u64 *val)
Bryan Schumaker65178db2011-11-01 13:35:21 -040089{
Bryan Schumaker184c1842012-11-29 11:40:44 -050090 nfs4_lock_state();
91 *val = nfsd_for_n_state(0, op->print);
92 nfs4_unlock_state();
Bryan Schumaker65178db2011-11-01 13:35:21 -040093}
94
Bryan Schumakerd7cc4312012-11-29 11:40:45 -050095static 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 Meef3e41ec2014-04-08 13:04:01 +0800100 size_t size;
Bryan Schumakerd7cc4312012-11-29 11:40:45 -0500101 loff_t pos = *ppos;
102
103 if (!pos)
Al Viro496ad9a2013-01-23 17:07:38 -0500104 nfsd_inject_get(file_inode(file)->i_private, &val);
Bryan Schumakerd7cc4312012-11-29 11:40:45 -0500105 size = scnprintf(read_buf, sizeof(read_buf), "%llu\n", val);
106
Kinglong Meef3e41ec2014-04-08 13:04:01 +0800107 return simple_read_from_buffer(buf, len, ppos, read_buf, size);
Bryan Schumakerd7cc4312012-11-29 11:40:45 -0500108}
109
110static ssize_t fault_inject_write(struct file *file, const char __user *buf,
111 size_t len, loff_t *ppos)
112{
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -0500113 char write_buf[INET6_ADDRSTRLEN];
Bryan Schumaker18d9a2c2012-12-07 16:17:29 -0500114 size_t size = min(sizeof(write_buf) - 1, len);
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -0500115 struct net *net = current->nsproxy->net_ns;
116 struct sockaddr_storage sa;
Bryan Schumakerd7cc4312012-11-29 11:40:45 -0500117 u64 val;
Jeff Laytond4c8e342014-06-18 15:00:19 -0400118 char *nl;
Bryan Schumakerd7cc4312012-11-29 11:40:45 -0500119
120 if (copy_from_user(write_buf, buf, size))
121 return -EFAULT;
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -0500122 write_buf[size] = '\0';
Bryan Schumakerd7cc4312012-11-29 11:40:45 -0500123
Jeff Laytond4c8e342014-06-18 15:00:19 -0400124 /* Deal with any embedded newlines in the string */
125 nl = strchr(write_buf, '\n');
126 if (nl) {
127 size = nl - write_buf;
128 *nl = '\0';
129 }
130
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -0500131 size = rpc_pton(net, write_buf, size, (struct sockaddr *)&sa, sizeof(sa));
132 if (size > 0)
Al Viro496ad9a2013-01-23 17:07:38 -0500133 nfsd_inject_set_client(file_inode(file)->i_private, &sa, size);
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -0500134 else {
135 val = simple_strtoll(write_buf, NULL, 0);
Al Viro496ad9a2013-01-23 17:07:38 -0500136 nfsd_inject_set(file_inode(file)->i_private, val);
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -0500137 }
Bryan Schumakerd7cc4312012-11-29 11:40:45 -0500138 return len; /* on success, claim we got the whole input */
139}
140
141static const struct file_operations fops_nfsd = {
142 .owner = THIS_MODULE,
143 .read = fault_inject_read,
144 .write = fault_inject_write,
145};
Bryan Schumaker65178db2011-11-01 13:35:21 -0400146
147void nfsd_fault_inject_cleanup(void)
148{
149 debugfs_remove_recursive(debug_dir);
150}
151
152int nfsd_fault_inject_init(void)
153{
154 unsigned int i;
155 struct nfsd_fault_inject_op *op;
Al Viro88187392012-03-20 06:00:24 -0400156 umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
Bryan Schumaker65178db2011-11-01 13:35:21 -0400157
158 debug_dir = debugfs_create_dir("nfsd", NULL);
159 if (!debug_dir)
160 goto fail;
161
162 for (i = 0; i < NUM_INJECT_OPS; i++) {
163 op = &inject_ops[i];
164 if (!debugfs_create_file(op->file, mode, debug_dir, op, &fops_nfsd))
165 goto fail;
166 }
167 return 0;
168
169fail:
170 nfsd_fault_inject_cleanup();
171 return -ENOMEM;
172}