blob: eb4a04afea80a6a2e3d3954cf1b9f00a45faf4a9
1 | #include <linux/module.h> |
2 | |
3 | #include "notifier-error-inject.h" |
4 | |
5 | static int debugfs_errno_set(void *data, u64 val) |
6 | { |
7 | *(int *)data = clamp_t(int, val, -MAX_ERRNO, 0); |
8 | return 0; |
9 | } |
10 | |
11 | static int debugfs_errno_get(void *data, u64 *val) |
12 | { |
13 | *val = *(int *)data; |
14 | return 0; |
15 | } |
16 | |
17 | DEFINE_SIMPLE_ATTRIBUTE(fops_errno, debugfs_errno_get, debugfs_errno_set, |
18 | "%lld\n"); |
19 | |
20 | static struct dentry *debugfs_create_errno(const char *name, umode_t mode, |
21 | struct dentry *parent, int *value) |
22 | { |
23 | return debugfs_create_file(name, mode, parent, value, &fops_errno); |
24 | } |
25 | |
26 | static int notifier_err_inject_callback(struct notifier_block *nb, |
27 | unsigned long val, void *p) |
28 | { |
29 | int err = 0; |
30 | struct notifier_err_inject *err_inject = |
31 | container_of(nb, struct notifier_err_inject, nb); |
32 | struct notifier_err_inject_action *action; |
33 | |
34 | for (action = err_inject->actions; action->name; action++) { |
35 | if (action->val == val) { |
36 | err = action->error; |
37 | break; |
38 | } |
39 | } |
40 | if (err) |
41 | pr_info("Injecting error (%d) to %s\n", err, action->name); |
42 | |
43 | return notifier_from_errno(err); |
44 | } |
45 | |
46 | struct dentry *notifier_err_inject_dir; |
47 | EXPORT_SYMBOL_GPL(notifier_err_inject_dir); |
48 | |
49 | struct dentry *notifier_err_inject_init(const char *name, struct dentry *parent, |
50 | struct notifier_err_inject *err_inject, int priority) |
51 | { |
52 | struct notifier_err_inject_action *action; |
53 | umode_t mode = S_IFREG | S_IRUSR | S_IWUSR; |
54 | struct dentry *dir; |
55 | struct dentry *actions_dir; |
56 | |
57 | err_inject->nb.notifier_call = notifier_err_inject_callback; |
58 | err_inject->nb.priority = priority; |
59 | |
60 | dir = debugfs_create_dir(name, parent); |
61 | if (!dir) |
62 | return ERR_PTR(-ENOMEM); |
63 | |
64 | actions_dir = debugfs_create_dir("actions", dir); |
65 | if (!actions_dir) |
66 | goto fail; |
67 | |
68 | for (action = err_inject->actions; action->name; action++) { |
69 | struct dentry *action_dir; |
70 | |
71 | action_dir = debugfs_create_dir(action->name, actions_dir); |
72 | if (!action_dir) |
73 | goto fail; |
74 | |
75 | /* |
76 | * Create debugfs r/w file containing action->error. If |
77 | * notifier call chain is called with action->val, it will |
78 | * fail with the error code |
79 | */ |
80 | if (!debugfs_create_errno("error", mode, action_dir, |
81 | &action->error)) |
82 | goto fail; |
83 | } |
84 | return dir; |
85 | fail: |
86 | debugfs_remove_recursive(dir); |
87 | return ERR_PTR(-ENOMEM); |
88 | } |
89 | EXPORT_SYMBOL_GPL(notifier_err_inject_init); |
90 | |
91 | static int __init err_inject_init(void) |
92 | { |
93 | notifier_err_inject_dir = |
94 | debugfs_create_dir("notifier-error-inject", NULL); |
95 | |
96 | if (!notifier_err_inject_dir) |
97 | return -ENOMEM; |
98 | |
99 | return 0; |
100 | } |
101 | |
102 | static void __exit err_inject_exit(void) |
103 | { |
104 | debugfs_remove_recursive(notifier_err_inject_dir); |
105 | } |
106 | |
107 | module_init(err_inject_init); |
108 | module_exit(err_inject_exit); |
109 | |
110 | MODULE_DESCRIPTION("Notifier error injection module"); |
111 | MODULE_LICENSE("GPL"); |
112 | MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>"); |
113 |