2 * Copyright (c) 2011 Bryan Schumaker <bjschuma@netapp.com>
4 * Uses debugfs to create fault injection points for client testing
7 #include <linux/types.h>
9 #include <linux/debugfs.h>
10 #include <linux/module.h>
11 #include <asm/uaccess.h>
15 struct nfsd_fault_inject_op {
17 u64 (*forget)(struct nfs4_client *, u64);
18 u64 (*print)(struct nfs4_client *, u64);
21 static struct nfsd_fault_inject_op inject_ops[] = {
23 .file = "forget_clients",
24 .forget = nfsd_forget_client,
25 .print = nfsd_print_client,
28 .file = "forget_locks",
29 .forget = nfsd_forget_client_locks,
30 .print = nfsd_print_client_locks,
33 .file = "forget_openowners",
34 .forget = nfsd_forget_client_openowners,
35 .print = nfsd_print_client_openowners,
38 .file = "forget_delegations",
39 .forget = nfsd_forget_client_delegations,
40 .print = nfsd_print_client_delegations,
43 .file = "recall_delegations",
44 .forget = nfsd_recall_client_delegations,
45 .print = nfsd_print_client_delegations,
49 static long int NUM_INJECT_OPS = sizeof(inject_ops) / sizeof(struct nfsd_fault_inject_op);
50 static struct dentry *debug_dir;
52 static void nfsd_inject_set(struct nfsd_fault_inject_op *op, u64 val)
57 printk(KERN_INFO "NFSD Fault Injection: %s (all)", op->file);
59 printk(KERN_INFO "NFSD Fault Injection: %s (n = %llu)", op->file, val);
62 count = nfsd_for_n_state(val, op->forget);
64 printk(KERN_INFO "NFSD: %s: found %llu", op->file, count);
67 static void nfsd_inject_get(struct nfsd_fault_inject_op *op, u64 *val)
70 *val = nfsd_for_n_state(0, op->print);
74 static ssize_t fault_inject_read(struct file *file, char __user *buf,
75 size_t len, loff_t *ppos)
83 nfsd_inject_get(file->f_dentry->d_inode->i_private, &val);
84 size = scnprintf(read_buf, sizeof(read_buf), "%llu\n", val);
88 if (pos >= size || !len)
92 ret = copy_to_user(buf, read_buf + pos, len);
100 static ssize_t fault_inject_write(struct file *file, const char __user *buf,
101 size_t len, loff_t *ppos)
104 size_t size = min(sizeof(write_buf), len) - 1;
107 if (copy_from_user(write_buf, buf, size))
110 val = simple_strtoll(write_buf, NULL, 0);
111 nfsd_inject_set(file->f_dentry->d_inode->i_private, val);
112 return len; /* on success, claim we got the whole input */
115 static const struct file_operations fops_nfsd = {
116 .owner = THIS_MODULE,
117 .read = fault_inject_read,
118 .write = fault_inject_write,
121 void nfsd_fault_inject_cleanup(void)
123 debugfs_remove_recursive(debug_dir);
126 int nfsd_fault_inject_init(void)
129 struct nfsd_fault_inject_op *op;
130 umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
132 debug_dir = debugfs_create_dir("nfsd", NULL);
136 for (i = 0; i < NUM_INJECT_OPS; i++) {
138 if (!debugfs_create_file(op->file, mode, debug_dir, op, &fops_nfsd))
144 nfsd_fault_inject_cleanup();