blob: 3ec72c931ac5a2f90f35fa61e97261fee8abe1a1 [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>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080013#include <linux/uaccess.h>
Jérémy Lefaurea1335522017-10-01 15:30:47 -040014#include <linux/kernel.h>
Bryan Schumaker65178db2011-11-01 13:35:21 -040015
16#include "state.h"
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -050017#include "netns.h"
Bryan Schumaker65178db2011-11-01 13:35:21 -040018
19struct nfsd_fault_inject_op {
20 char *file;
Jeff Layton285abde2014-07-30 08:27:24 -040021 u64 (*get)(void);
22 u64 (*set_val)(u64);
23 u64 (*set_clnt)(struct sockaddr_storage *, size_t);
Bryan Schumaker65178db2011-11-01 13:35:21 -040024};
25
Bryan Schumaker65178db2011-11-01 13:35:21 -040026static struct dentry *debug_dir;
27
Bryan Schumakerd7cc4312012-11-29 11:40:45 -050028static 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];
Kinglong Meef3e41ec2014-04-08 13:04:01 +080033 size_t size;
Bryan Schumakerd7cc4312012-11-29 11:40:45 -050034 loff_t pos = *ppos;
Jeff Laytonc96223d2014-07-30 08:27:16 -040035 struct nfsd_fault_inject_op *op = file_inode(file)->i_private;
Bryan Schumakerd7cc4312012-11-29 11:40:45 -050036
37 if (!pos)
Jeff Layton285abde2014-07-30 08:27:24 -040038 val = op->get();
Bryan Schumakerd7cc4312012-11-29 11:40:45 -050039 size = scnprintf(read_buf, sizeof(read_buf), "%llu\n", val);
40
Kinglong Meef3e41ec2014-04-08 13:04:01 +080041 return simple_read_from_buffer(buf, len, ppos, read_buf, size);
Bryan Schumakerd7cc4312012-11-29 11:40:45 -050042}
43
44static ssize_t fault_inject_write(struct file *file, const char __user *buf,
45 size_t len, loff_t *ppos)
46{
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -050047 char write_buf[INET6_ADDRSTRLEN];
Bryan Schumaker18d9a2c2012-12-07 16:17:29 -050048 size_t size = min(sizeof(write_buf) - 1, len);
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -050049 struct net *net = current->nsproxy->net_ns;
50 struct sockaddr_storage sa;
Jeff Laytonc96223d2014-07-30 08:27:16 -040051 struct nfsd_fault_inject_op *op = file_inode(file)->i_private;
Bryan Schumakerd7cc4312012-11-29 11:40:45 -050052 u64 val;
Jeff Laytond4c8e342014-06-18 15:00:19 -040053 char *nl;
Bryan Schumakerd7cc4312012-11-29 11:40:45 -050054
55 if (copy_from_user(write_buf, buf, size))
56 return -EFAULT;
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -050057 write_buf[size] = '\0';
Bryan Schumakerd7cc4312012-11-29 11:40:45 -050058
Jeff Laytond4c8e342014-06-18 15:00:19 -040059 /* 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
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -050066 size = rpc_pton(net, write_buf, size, (struct sockaddr *)&sa, sizeof(sa));
Jeff Laytonc96223d2014-07-30 08:27:16 -040067 if (size > 0) {
Jeff Layton285abde2014-07-30 08:27:24 -040068 val = op->set_clnt(&sa, size);
Jeff Laytonc96223d2014-07-30 08:27:16 -040069 if (val)
70 pr_info("NFSD [%s]: Client %s had %llu state object(s)\n",
71 op->file, write_buf, val);
72 } else {
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -050073 val = simple_strtoll(write_buf, NULL, 0);
Jeff Laytonc96223d2014-07-30 08:27:16 -040074 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);
Jeff Layton285abde2014-07-30 08:27:24 -040079 val = op->set_val(val);
Jeff Laytonc96223d2014-07-30 08:27:16 -040080 pr_info("NFSD: %s: found %llu", op->file, val);
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -050081 }
Bryan Schumakerd7cc4312012-11-29 11:40:45 -050082 return len; /* on success, claim we got the whole input */
83}
84
85static const struct file_operations fops_nfsd = {
86 .owner = THIS_MODULE,
87 .read = fault_inject_read,
88 .write = fault_inject_write,
89};
Bryan Schumaker65178db2011-11-01 13:35:21 -040090
91void nfsd_fault_inject_cleanup(void)
92{
93 debugfs_remove_recursive(debug_dir);
94}
95
Jeff Laytonc96223d2014-07-30 08:27:16 -040096static struct nfsd_fault_inject_op inject_ops[] = {
97 {
98 .file = "forget_clients",
Jeff Layton7ec0e362014-07-30 08:27:17 -040099 .get = nfsd_inject_print_clients,
Jeff Layton69fc9ed2014-07-30 08:27:19 -0400100 .set_val = nfsd_inject_forget_clients,
Jeff Laytona0926d12014-07-30 08:27:18 -0400101 .set_clnt = nfsd_inject_forget_client,
Jeff Laytonc96223d2014-07-30 08:27:16 -0400102 },
103 {
104 .file = "forget_locks",
Jeff Layton016200c2014-07-30 08:27:21 -0400105 .get = nfsd_inject_print_locks,
106 .set_val = nfsd_inject_forget_locks,
107 .set_clnt = nfsd_inject_forget_client_locks,
Jeff Laytonc96223d2014-07-30 08:27:16 -0400108 },
109 {
110 .file = "forget_openowners",
Jeff Layton82e05ef2014-07-30 08:27:22 -0400111 .get = nfsd_inject_print_openowners,
112 .set_val = nfsd_inject_forget_openowners,
113 .set_clnt = nfsd_inject_forget_client_openowners,
Jeff Laytonc96223d2014-07-30 08:27:16 -0400114 },
115 {
116 .file = "forget_delegations",
Jeff Layton98d5c7c2014-07-30 08:27:23 -0400117 .get = nfsd_inject_print_delegations,
118 .set_val = nfsd_inject_forget_delegations,
119 .set_clnt = nfsd_inject_forget_client_delegations,
Jeff Laytonc96223d2014-07-30 08:27:16 -0400120 },
121 {
122 .file = "recall_delegations",
Jeff Layton98d5c7c2014-07-30 08:27:23 -0400123 .get = nfsd_inject_print_delegations,
124 .set_val = nfsd_inject_recall_delegations,
125 .set_clnt = nfsd_inject_recall_client_delegations,
Jeff Laytonc96223d2014-07-30 08:27:16 -0400126 },
127};
128
Bryan Schumaker65178db2011-11-01 13:35:21 -0400129int nfsd_fault_inject_init(void)
130{
131 unsigned int i;
132 struct nfsd_fault_inject_op *op;
Al Viro88187392012-03-20 06:00:24 -0400133 umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
Bryan Schumaker65178db2011-11-01 13:35:21 -0400134
135 debug_dir = debugfs_create_dir("nfsd", NULL);
136 if (!debug_dir)
137 goto fail;
138
Jérémy Lefaurea1335522017-10-01 15:30:47 -0400139 for (i = 0; i < ARRAY_SIZE(inject_ops); i++) {
Bryan Schumaker65178db2011-11-01 13:35:21 -0400140 op = &inject_ops[i];
141 if (!debugfs_create_file(op->file, mode, debug_dir, op, &fops_nfsd))
142 goto fail;
143 }
144 return 0;
145
146fail:
147 nfsd_fault_inject_cleanup();
148 return -ENOMEM;
149}