[PATCH] fault injection: process filtering for fault-injection capabilities
[linux-2.6.git] / lib / fault-inject.c
1 #include <linux/kernel.h>
2 #include <linux/init.h>
3 #include <linux/random.h>
4 #include <linux/stat.h>
5 #include <linux/types.h>
6 #include <linux/fs.h>
7 #include <linux/module.h>
8 #include <linux/interrupt.h>
9 #include <linux/fault-inject.h>
10
11 /*
12  * setup_fault_attr() is a helper function for various __setup handlers, so it
13  * returns 0 on error, because that is what __setup handlers do.
14  */
15 int __init setup_fault_attr(struct fault_attr *attr, char *str)
16 {
17         unsigned long probability;
18         unsigned long interval;
19         int times;
20         int space;
21
22         /* "<interval>,<probability>,<space>,<times>" */
23         if (sscanf(str, "%lu,%lu,%d,%d",
24                         &interval, &probability, &space, &times) < 4) {
25                 printk(KERN_WARNING
26                         "FAULT_INJECTION: failed to parse arguments\n");
27                 return 0;
28         }
29
30         attr->probability = probability;
31         attr->interval = interval;
32         atomic_set(&attr->times, times);
33         atomic_set(&attr->space, space);
34
35         return 1;
36 }
37
38 static void fail_dump(struct fault_attr *attr)
39 {
40         if (attr->verbose > 0)
41                 printk(KERN_NOTICE "FAULT_INJECTION: forcing a failure\n");
42         if (attr->verbose > 1)
43                 dump_stack();
44 }
45
46 #define atomic_dec_not_zero(v)          atomic_add_unless((v), -1, 0)
47
48 static int fail_task(struct fault_attr *attr, struct task_struct *task)
49 {
50         return !in_interrupt() && task->make_it_fail;
51 }
52
53 /*
54  * This code is stolen from failmalloc-1.0
55  * http://www.nongnu.org/failmalloc/
56  */
57
58 int should_fail(struct fault_attr *attr, ssize_t size)
59 {
60         if (attr->task_filter && !fail_task(attr, current))
61                 return 0;
62
63         if (atomic_read(&attr->times) == 0)
64                 return 0;
65
66         if (atomic_read(&attr->space) > size) {
67                 atomic_sub(size, &attr->space);
68                 return 0;
69         }
70
71         if (attr->interval > 1) {
72                 attr->count++;
73                 if (attr->count % attr->interval)
74                         return 0;
75         }
76
77         if (attr->probability > random32() % 100)
78                 goto fail;
79
80         return 0;
81
82 fail:
83         fail_dump(attr);
84
85         if (atomic_read(&attr->times) != -1)
86                 atomic_dec_not_zero(&attr->times);
87
88         return 1;
89 }
90
91 #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
92
93 static void debugfs_ul_set(void *data, u64 val)
94 {
95         *(unsigned long *)data = val;
96 }
97
98 static u64 debugfs_ul_get(void *data)
99 {
100         return *(unsigned long *)data;
101 }
102
103 DEFINE_SIMPLE_ATTRIBUTE(fops_ul, debugfs_ul_get, debugfs_ul_set, "%llu\n");
104
105 static struct dentry *debugfs_create_ul(const char *name, mode_t mode,
106                                 struct dentry *parent, unsigned long *value)
107 {
108         return debugfs_create_file(name, mode, parent, value, &fops_ul);
109 }
110
111 static void debugfs_atomic_t_set(void *data, u64 val)
112 {
113         atomic_set((atomic_t *)data, val);
114 }
115
116 static u64 debugfs_atomic_t_get(void *data)
117 {
118         return atomic_read((atomic_t *)data);
119 }
120
121 DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
122                         debugfs_atomic_t_set, "%lld\n");
123
124 static struct dentry *debugfs_create_atomic_t(const char *name, mode_t mode,
125                                 struct dentry *parent, atomic_t *value)
126 {
127         return debugfs_create_file(name, mode, parent, value, &fops_atomic_t);
128 }
129
130 void cleanup_fault_attr_dentries(struct fault_attr *attr)
131 {
132         debugfs_remove(attr->dentries.probability_file);
133         attr->dentries.probability_file = NULL;
134
135         debugfs_remove(attr->dentries.interval_file);
136         attr->dentries.interval_file = NULL;
137
138         debugfs_remove(attr->dentries.times_file);
139         attr->dentries.times_file = NULL;
140
141         debugfs_remove(attr->dentries.space_file);
142         attr->dentries.space_file = NULL;
143
144         debugfs_remove(attr->dentries.verbose_file);
145         attr->dentries.verbose_file = NULL;
146
147         debugfs_remove(attr->dentries.task_filter_file);
148         attr->dentries.task_filter_file = NULL;
149
150         if (attr->dentries.dir)
151                 WARN_ON(!simple_empty(attr->dentries.dir));
152
153         debugfs_remove(attr->dentries.dir);
154         attr->dentries.dir = NULL;
155 }
156
157 int init_fault_attr_dentries(struct fault_attr *attr, const char *name)
158 {
159         mode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
160         struct dentry *dir;
161
162         memset(&attr->dentries, 0, sizeof(attr->dentries));
163
164         dir = debugfs_create_dir(name, NULL);
165         if (!dir)
166                 goto fail;
167         attr->dentries.dir = dir;
168
169         attr->dentries.probability_file =
170                 debugfs_create_ul("probability", mode, dir, &attr->probability);
171
172         attr->dentries.interval_file =
173                 debugfs_create_ul("interval", mode, dir, &attr->interval);
174
175         attr->dentries.times_file =
176                 debugfs_create_atomic_t("times", mode, dir, &attr->times);
177
178         attr->dentries.space_file =
179                 debugfs_create_atomic_t("space", mode, dir, &attr->space);
180
181         attr->dentries.verbose_file =
182                 debugfs_create_ul("verbose", mode, dir, &attr->verbose);
183
184         attr->dentries.task_filter_file = debugfs_create_bool("task-filter",
185                                                 mode, dir, &attr->task_filter);
186
187         if (!attr->dentries.probability_file || !attr->dentries.interval_file
188             || !attr->dentries.times_file || !attr->dentries.space_file
189             || !attr->dentries.verbose_file || !attr->dentries.task_filter_file)
190                 goto fail;
191
192         return 0;
193 fail:
194         cleanup_fault_attr_dentries(attr);
195         return -ENOMEM;
196 }
197
198 #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */