]>
Commit | Line | Data |
---|---|---|
33d3dfff | 1 | #include <linux/fanotify.h> |
ff0b16a9 EP |
2 | #include <linux/fdtable.h> |
3 | #include <linux/fsnotify_backend.h> | |
4 | #include <linux/init.h> | |
9e66e423 | 5 | #include <linux/jiffies.h> |
ff0b16a9 | 6 | #include <linux/kernel.h> /* UINT_MAX */ |
1c529063 | 7 | #include <linux/mount.h> |
9e66e423 | 8 | #include <linux/sched.h> |
ff0b16a9 | 9 | #include <linux/types.h> |
9e66e423 | 10 | #include <linux/wait.h> |
ff0b16a9 | 11 | |
767cd46c EP |
12 | static bool should_merge(struct fsnotify_event *old, struct fsnotify_event *new) |
13 | { | |
14 | pr_debug("%s: old=%p new=%p\n", __func__, old, new); | |
15 | ||
32c32632 AG |
16 | if (old->to_tell == new->to_tell && |
17 | old->data_type == new->data_type && | |
18 | old->tgid == new->tgid) { | |
767cd46c | 19 | switch (old->data_type) { |
3bcf3860 EP |
20 | case (FSNOTIFY_EVENT_FILE): |
21 | if ((old->file->f_path.mnt == new->file->f_path.mnt) && | |
22 | (old->file->f_path.dentry == new->file->f_path.dentry)) | |
767cd46c EP |
23 | return true; |
24 | case (FSNOTIFY_EVENT_NONE): | |
25 | return true; | |
26 | default: | |
27 | BUG(); | |
28 | }; | |
29 | } | |
30 | return false; | |
31 | } | |
32 | ||
f70ab54c EP |
33 | /* and the list better be locked by something too! */ |
34 | static struct fsnotify_event *fanotify_merge(struct list_head *list, | |
35 | struct fsnotify_event *event) | |
767cd46c | 36 | { |
a12a7dd3 | 37 | struct fsnotify_event_holder *test_holder; |
f70ab54c | 38 | struct fsnotify_event *test_event = NULL; |
a12a7dd3 | 39 | struct fsnotify_event *new_event; |
767cd46c EP |
40 | |
41 | pr_debug("%s: list=%p event=%p\n", __func__, list, event); | |
42 | ||
767cd46c | 43 | |
a12a7dd3 | 44 | list_for_each_entry_reverse(test_holder, list, event_list) { |
f70ab54c EP |
45 | if (should_merge(test_holder->event, event)) { |
46 | test_event = test_holder->event; | |
a12a7dd3 EP |
47 | break; |
48 | } | |
49 | } | |
f70ab54c EP |
50 | |
51 | if (!test_event) | |
52 | return NULL; | |
53 | ||
54 | fsnotify_get_event(test_event); | |
55 | ||
56 | /* if they are exactly the same we are done */ | |
57 | if (test_event->mask == event->mask) | |
58 | return test_event; | |
59 | ||
60 | /* | |
61 | * if the refcnt == 2 this is the only queue | |
62 | * for this event and so we can update the mask | |
63 | * in place. | |
64 | */ | |
65 | if (atomic_read(&test_event->refcnt) == 2) { | |
66 | test_event->mask |= event->mask; | |
67 | return test_event; | |
68 | } | |
69 | ||
70 | new_event = fsnotify_clone_event(test_event); | |
71 | ||
72 | /* done with test_event */ | |
73 | fsnotify_put_event(test_event); | |
74 | ||
75 | /* couldn't allocate memory, merge was not possible */ | |
76 | if (unlikely(!new_event)) | |
77 | return ERR_PTR(-ENOMEM); | |
78 | ||
79 | /* build new event and replace it on the list */ | |
80 | new_event->mask = (test_event->mask | event->mask); | |
81 | fsnotify_replace_event(test_holder, new_event); | |
82 | ||
83 | /* we hold a reference on new_event from clone_event */ | |
84 | return new_event; | |
767cd46c EP |
85 | } |
86 | ||
9e66e423 EP |
87 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
88 | static int fanotify_get_response_from_access(struct fsnotify_group *group, | |
89 | struct fsnotify_event *event) | |
90 | { | |
91 | int ret; | |
92 | ||
93 | pr_debug("%s: group=%p event=%p\n", __func__, group, event); | |
94 | ||
95 | wait_event(group->fanotify_data.access_waitq, event->response); | |
96 | ||
97 | /* userspace responded, convert to something usable */ | |
98 | spin_lock(&event->lock); | |
99 | switch (event->response) { | |
100 | case FAN_ALLOW: | |
101 | ret = 0; | |
102 | break; | |
103 | case FAN_DENY: | |
104 | default: | |
105 | ret = -EPERM; | |
106 | } | |
107 | event->response = 0; | |
108 | spin_unlock(&event->lock); | |
109 | ||
b2d87909 EP |
110 | pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__, |
111 | group, event, ret); | |
112 | ||
9e66e423 EP |
113 | return ret; |
114 | } | |
115 | #endif | |
116 | ||
3a9b16b4 | 117 | static int fanotify_handle_event(struct fsnotify_group *group, |
ce8f76fb EP |
118 | struct fsnotify_mark *inode_mark, |
119 | struct fsnotify_mark *fanotify_mark, | |
3a9b16b4 | 120 | struct fsnotify_event *event) |
ff0b16a9 | 121 | { |
f70ab54c | 122 | int ret = 0; |
9e66e423 | 123 | struct fsnotify_event *notify_event = NULL; |
ff0b16a9 EP |
124 | |
125 | BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS); | |
126 | BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY); | |
127 | BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE); | |
128 | BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE); | |
129 | BUILD_BUG_ON(FAN_OPEN != FS_OPEN); | |
130 | BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD); | |
131 | BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW); | |
9e66e423 EP |
132 | BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM); |
133 | BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM); | |
ff0b16a9 EP |
134 | |
135 | pr_debug("%s: group=%p event=%p\n", __func__, group, event); | |
136 | ||
f70ab54c EP |
137 | notify_event = fsnotify_add_notify_event(group, event, NULL, fanotify_merge); |
138 | if (IS_ERR(notify_event)) | |
139 | return PTR_ERR(notify_event); | |
9e66e423 EP |
140 | |
141 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS | |
142 | if (event->mask & FAN_ALL_PERM_EVENTS) { | |
143 | /* if we merged we need to wait on the new event */ | |
144 | if (notify_event) | |
145 | event = notify_event; | |
146 | ret = fanotify_get_response_from_access(group, event); | |
147 | } | |
148 | #endif | |
149 | ||
9e66e423 EP |
150 | if (notify_event) |
151 | fsnotify_put_event(notify_event); | |
f70ab54c | 152 | |
ff0b16a9 EP |
153 | return ret; |
154 | } | |
155 | ||
0215054f EP |
156 | static bool should_send_vfsmount_event(struct fsnotify_group *group, |
157 | struct vfsmount *mnt, | |
158 | struct inode *inode, | |
159 | struct fsnotify_mark *mnt_mark, | |
160 | __u32 mask) | |
ff0b16a9 | 161 | { |
32a4df13 | 162 | struct fsnotify_mark *inode_mark; |
ff0b16a9 | 163 | |
0215054f EP |
164 | pr_debug("%s: group=%p vfsmount=%p mark=%p mask=%x\n", |
165 | __func__, group, mnt, mnt_mark, mask); | |
ff0b16a9 | 166 | |
32a4df13 EP |
167 | mask &= mnt_mark->mask; |
168 | mask &= ~mnt_mark->ignored_mask; | |
169 | ||
170 | if (mask) { | |
171 | inode_mark = fsnotify_find_inode_mark(group, inode); | |
172 | if (inode_mark) { | |
173 | mask &= ~inode_mark->ignored_mask; | |
174 | fsnotify_put_mark(inode_mark); | |
175 | } | |
176 | } | |
1c529063 | 177 | |
32a4df13 | 178 | return mask; |
1c529063 EP |
179 | } |
180 | ||
0215054f EP |
181 | static bool should_send_inode_event(struct fsnotify_group *group, |
182 | struct inode *inode, | |
183 | struct fsnotify_mark *mark, | |
1c529063 EP |
184 | __u32 mask) |
185 | { | |
0215054f EP |
186 | pr_debug("%s: group=%p inode=%p mark=%p mask=%x\n", |
187 | __func__, group, inode, mark, mask); | |
ff0b16a9 | 188 | |
2612abb5 EP |
189 | /* |
190 | * if the event is for a child and this inode doesn't care about | |
191 | * events on the child, don't send it! | |
192 | */ | |
ff0b16a9 | 193 | if ((mask & FS_EVENT_ON_CHILD) && |
2612abb5 EP |
194 | !(mark->mask & FS_EVENT_ON_CHILD)) |
195 | return false; | |
196 | else | |
197 | return true; | |
ff0b16a9 EP |
198 | } |
199 | ||
ce8f76fb EP |
200 | static bool fanotify_should_send_event(struct fsnotify_group *group, |
201 | struct inode *to_tell, | |
202 | struct vfsmount *mnt, | |
203 | struct fsnotify_mark *inode_mark, | |
204 | struct fsnotify_mark *vfsmount_mark, | |
3a9b16b4 | 205 | __u32 mask, void *data, int data_type) |
1c529063 EP |
206 | { |
207 | pr_debug("%s: group=%p to_tell=%p mnt=%p mask=%x data=%p data_type=%d\n", | |
208 | __func__, group, to_tell, mnt, mask, data, data_type); | |
209 | ||
210 | /* sorry, fanotify only gives a damn about files and dirs */ | |
211 | if (!S_ISREG(to_tell->i_mode) && | |
212 | !S_ISDIR(to_tell->i_mode)) | |
213 | return false; | |
214 | ||
215 | /* if we don't have enough info to send an event to userspace say no */ | |
3bcf3860 | 216 | if (data_type != FSNOTIFY_EVENT_FILE) |
1c529063 EP |
217 | return false; |
218 | ||
219 | if (mnt) | |
ce8f76fb EP |
220 | return should_send_vfsmount_event(group, mnt, to_tell, |
221 | vfsmount_mark, mask); | |
1c529063 | 222 | else |
ce8f76fb | 223 | return should_send_inode_event(group, to_tell, inode_mark, mask); |
1c529063 EP |
224 | } |
225 | ||
ff0b16a9 EP |
226 | const struct fsnotify_ops fanotify_fsnotify_ops = { |
227 | .handle_event = fanotify_handle_event, | |
228 | .should_send_event = fanotify_should_send_event, | |
229 | .free_group_priv = NULL, | |
230 | .free_event_priv = NULL, | |
231 | .freeing_mark = NULL, | |
232 | }; |