NFSD: Add a custom file operations structure for fault injection
[profile/ivi/kernel-adaptation-intel-automotive.git] / fs / nfsd / fault_inject.c
1 /*
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>
11 #include <asm/uaccess.h>
12
13 #include "state.h"
14
15 struct nfsd_fault_inject_op {
16         char *file;
17         u64 (*forget)(struct nfs4_client *, u64);
18         u64 (*print)(struct nfs4_client *, u64);
19 };
20
21 static struct nfsd_fault_inject_op inject_ops[] = {
22         {
23                 .file   = "forget_clients",
24                 .forget = nfsd_forget_client,
25                 .print  = nfsd_print_client,
26         },
27         {
28                 .file   = "forget_locks",
29                 .forget = nfsd_forget_client_locks,
30                 .print  = nfsd_print_client_locks,
31         },
32         {
33                 .file   = "forget_openowners",
34                 .forget = nfsd_forget_client_openowners,
35                 .print  = nfsd_print_client_openowners,
36         },
37         {
38                 .file   = "forget_delegations",
39                 .forget = nfsd_forget_client_delegations,
40                 .print  = nfsd_print_client_delegations,
41         },
42         {
43                 .file   = "recall_delegations",
44                 .forget = nfsd_recall_client_delegations,
45                 .print  = nfsd_print_client_delegations,
46         },
47 };
48
49 static long int NUM_INJECT_OPS = sizeof(inject_ops) / sizeof(struct nfsd_fault_inject_op);
50 static struct dentry *debug_dir;
51
52 static void nfsd_inject_set(struct nfsd_fault_inject_op *op, u64 val)
53 {
54         u64 count = 0;
55
56         if (val == 0)
57                 printk(KERN_INFO "NFSD Fault Injection: %s (all)", op->file);
58         else
59                 printk(KERN_INFO "NFSD Fault Injection: %s (n = %llu)", op->file, val);
60
61         nfs4_lock_state();
62         count = nfsd_for_n_state(val, op->forget);
63         nfs4_unlock_state();
64         printk(KERN_INFO "NFSD: %s: found %llu", op->file, count);
65 }
66
67 static void nfsd_inject_get(struct nfsd_fault_inject_op *op, u64 *val)
68 {
69         nfs4_lock_state();
70         *val = nfsd_for_n_state(0, op->print);
71         nfs4_unlock_state();
72 }
73
74 static ssize_t fault_inject_read(struct file *file, char __user *buf,
75                                  size_t len, loff_t *ppos)
76 {
77         static u64 val;
78         char read_buf[25];
79         size_t size, ret;
80         loff_t pos = *ppos;
81
82         if (!pos)
83                 nfsd_inject_get(file->f_dentry->d_inode->i_private, &val);
84         size = scnprintf(read_buf, sizeof(read_buf), "%llu\n", val);
85
86         if (pos < 0)
87                 return -EINVAL;
88         if (pos >= size || !len)
89                 return 0;
90         if (len > size - pos)
91                 len = size - pos;
92         ret = copy_to_user(buf, read_buf + pos, len);
93         if (ret == len)
94                 return -EFAULT;
95         len -= ret;
96         *ppos = pos + len;
97         return len;
98 }
99
100 static ssize_t fault_inject_write(struct file *file, const char __user *buf,
101                                   size_t len, loff_t *ppos)
102 {
103         char write_buf[24];
104         size_t size = min(sizeof(write_buf), len) - 1;
105         u64 val;
106
107         if (copy_from_user(write_buf, buf, size))
108                 return -EFAULT;
109
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 */
113 }
114
115 static const struct file_operations fops_nfsd = {
116         .owner   = THIS_MODULE,
117         .read    = fault_inject_read,
118         .write   = fault_inject_write,
119 };
120
121 void nfsd_fault_inject_cleanup(void)
122 {
123         debugfs_remove_recursive(debug_dir);
124 }
125
126 int nfsd_fault_inject_init(void)
127 {
128         unsigned int i;
129         struct nfsd_fault_inject_op *op;
130         umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
131
132         debug_dir = debugfs_create_dir("nfsd", NULL);
133         if (!debug_dir)
134                 goto fail;
135
136         for (i = 0; i < NUM_INJECT_OPS; i++) {
137                 op = &inject_ops[i];
138                 if (!debugfs_create_file(op->file, mode, debug_dir, op, &fops_nfsd))
139                         goto fail;
140         }
141         return 0;
142
143 fail:
144         nfsd_fault_inject_cleanup();
145         return -ENOMEM;
146 }