]>
Commit | Line | Data |
---|---|---|
90586523 EP |
1 | /* |
2 | * Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2, or (at your option) | |
7 | * any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; see the file COPYING. If not, write to | |
16 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
17 | */ | |
18 | ||
a2d8bc6c EP |
19 | /* |
20 | * Basic idea behind the notification queue: An fsnotify group (like inotify) | |
21 | * sends the userspace notification about events asyncronously some time after | |
22 | * the event happened. When inotify gets an event it will need to add that | |
23 | * event to the group notify queue. Since a single event might need to be on | |
24 | * multiple group's notification queues we can't add the event directly to each | |
25 | * queue and instead add a small "event_holder" to each queue. This event_holder | |
26 | * has a pointer back to the original event. Since the majority of events are | |
27 | * going to end up on one, and only one, notification queue we embed one | |
28 | * event_holder into each event. This means we have a single allocation instead | |
29 | * of always needing two. If the embedded event_holder is already in use by | |
30 | * another group a new event_holder (from fsnotify_event_holder_cachep) will be | |
31 | * allocated and used. | |
32 | */ | |
33 | ||
3bcf3860 | 34 | #include <linux/file.h> |
90586523 EP |
35 | #include <linux/fs.h> |
36 | #include <linux/init.h> | |
37 | #include <linux/kernel.h> | |
38 | #include <linux/list.h> | |
47882c6f | 39 | #include <linux/module.h> |
90586523 EP |
40 | #include <linux/mount.h> |
41 | #include <linux/mutex.h> | |
42 | #include <linux/namei.h> | |
43 | #include <linux/path.h> | |
44 | #include <linux/slab.h> | |
45 | #include <linux/spinlock.h> | |
46 | ||
47 | #include <asm/atomic.h> | |
48 | ||
49 | #include <linux/fsnotify_backend.h> | |
50 | #include "fsnotify.h" | |
51 | ||
52 | static struct kmem_cache *fsnotify_event_cachep; | |
a2d8bc6c EP |
53 | static struct kmem_cache *fsnotify_event_holder_cachep; |
54 | /* | |
55 | * This is a magic event we send when the q is too full. Since it doesn't | |
56 | * hold real event information we just keep one system wide and use it any time | |
57 | * it is needed. It's refcnt is set 1 at kernel init time and will never | |
58 | * get set to 0 so it will never get 'freed' | |
59 | */ | |
b4277d3d | 60 | static struct fsnotify_event *q_overflow_event; |
47882c6f EP |
61 | static atomic_t fsnotify_sync_cookie = ATOMIC_INIT(0); |
62 | ||
63 | /** | |
64 | * fsnotify_get_cookie - return a unique cookie for use in synchronizing events. | |
65 | * Called from fsnotify_move, which is inlined into filesystem modules. | |
66 | */ | |
67 | u32 fsnotify_get_cookie(void) | |
68 | { | |
69 | return atomic_inc_return(&fsnotify_sync_cookie); | |
70 | } | |
71 | EXPORT_SYMBOL_GPL(fsnotify_get_cookie); | |
a2d8bc6c EP |
72 | |
73 | /* return true if the notify queue is empty, false otherwise */ | |
74 | bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group) | |
75 | { | |
76 | BUG_ON(!mutex_is_locked(&group->notification_mutex)); | |
77 | return list_empty(&group->notification_list) ? true : false; | |
78 | } | |
90586523 EP |
79 | |
80 | void fsnotify_get_event(struct fsnotify_event *event) | |
81 | { | |
82 | atomic_inc(&event->refcnt); | |
83 | } | |
84 | ||
85 | void fsnotify_put_event(struct fsnotify_event *event) | |
86 | { | |
87 | if (!event) | |
88 | return; | |
89 | ||
90 | if (atomic_dec_and_test(&event->refcnt)) { | |
5ba08e2e EP |
91 | pr_debug("%s: event=%p\n", __func__, event); |
92 | ||
3bcf3860 EP |
93 | if (event->data_type == FSNOTIFY_EVENT_FILE) |
94 | fput(event->file); | |
90586523 | 95 | |
e4aff117 EP |
96 | BUG_ON(!list_empty(&event->private_data_list)); |
97 | ||
62ffe5df | 98 | kfree(event->file_name); |
32c32632 | 99 | put_pid(event->tgid); |
90586523 EP |
100 | kmem_cache_free(fsnotify_event_cachep, event); |
101 | } | |
102 | } | |
103 | ||
a2d8bc6c EP |
104 | struct fsnotify_event_holder *fsnotify_alloc_event_holder(void) |
105 | { | |
106 | return kmem_cache_alloc(fsnotify_event_holder_cachep, GFP_KERNEL); | |
107 | } | |
108 | ||
109 | void fsnotify_destroy_event_holder(struct fsnotify_event_holder *holder) | |
110 | { | |
74766bbf EP |
111 | if (holder) |
112 | kmem_cache_free(fsnotify_event_holder_cachep, holder); | |
a2d8bc6c EP |
113 | } |
114 | ||
115 | /* | |
e4aff117 EP |
116 | * Find the private data that the group previously attached to this event when |
117 | * the group added the event to the notification queue (fsnotify_add_notify_event) | |
118 | */ | |
119 | struct fsnotify_event_private_data *fsnotify_remove_priv_from_event(struct fsnotify_group *group, struct fsnotify_event *event) | |
120 | { | |
121 | struct fsnotify_event_private_data *lpriv; | |
122 | struct fsnotify_event_private_data *priv = NULL; | |
123 | ||
124 | assert_spin_locked(&event->lock); | |
125 | ||
126 | list_for_each_entry(lpriv, &event->private_data_list, event_list) { | |
127 | if (lpriv->group == group) { | |
128 | priv = lpriv; | |
129 | list_del(&priv->event_list); | |
130 | break; | |
131 | } | |
132 | } | |
133 | return priv; | |
134 | } | |
135 | ||
90586523 | 136 | /* |
a2d8bc6c EP |
137 | * Add an event to the group notification queue. The group can later pull this |
138 | * event off the queue to deal with. If the event is successfully added to the | |
139 | * group's notification queue, a reference is taken on event. | |
90586523 | 140 | */ |
f70ab54c EP |
141 | struct fsnotify_event *fsnotify_add_notify_event(struct fsnotify_group *group, struct fsnotify_event *event, |
142 | struct fsnotify_event_private_data *priv, | |
143 | struct fsnotify_event *(*merge)(struct list_head *, | |
144 | struct fsnotify_event *)) | |
a2d8bc6c | 145 | { |
f70ab54c | 146 | struct fsnotify_event *return_event = NULL; |
a2d8bc6c EP |
147 | struct fsnotify_event_holder *holder = NULL; |
148 | struct list_head *list = &group->notification_list; | |
e4aff117 | 149 | |
5ba08e2e EP |
150 | pr_debug("%s: group=%p event=%p priv=%p\n", __func__, group, event, priv); |
151 | ||
a2d8bc6c EP |
152 | /* |
153 | * There is one fsnotify_event_holder embedded inside each fsnotify_event. | |
154 | * Check if we expect to be able to use that holder. If not alloc a new | |
155 | * holder. | |
156 | * For the overflow event it's possible that something will use the in | |
157 | * event holder before we get the lock so we may need to jump back and | |
158 | * alloc a new holder, this can't happen for most events... | |
159 | */ | |
160 | if (!list_empty(&event->holder.event_list)) { | |
161 | alloc_holder: | |
162 | holder = fsnotify_alloc_event_holder(); | |
163 | if (!holder) | |
f70ab54c | 164 | return ERR_PTR(-ENOMEM); |
a2d8bc6c EP |
165 | } |
166 | ||
167 | mutex_lock(&group->notification_mutex); | |
168 | ||
e4aff117 | 169 | if (group->q_len >= group->max_events) { |
b4277d3d | 170 | event = q_overflow_event; |
f70ab54c EP |
171 | |
172 | /* | |
173 | * we need to return the overflow event | |
174 | * which means we need a ref | |
175 | */ | |
176 | fsnotify_get_event(event); | |
177 | return_event = event; | |
178 | ||
e4aff117 EP |
179 | /* sorry, no private data on the overflow event */ |
180 | priv = NULL; | |
181 | } | |
a2d8bc6c | 182 | |
74766bbf | 183 | if (!list_empty(list) && merge) { |
f70ab54c | 184 | struct fsnotify_event *tmp; |
74766bbf | 185 | |
f70ab54c EP |
186 | tmp = merge(list, event); |
187 | if (tmp) { | |
74766bbf | 188 | mutex_unlock(&group->notification_mutex); |
f70ab54c EP |
189 | |
190 | if (return_event) | |
191 | fsnotify_put_event(return_event); | |
74766bbf EP |
192 | if (holder != &event->holder) |
193 | fsnotify_destroy_event_holder(holder); | |
f70ab54c | 194 | return tmp; |
74766bbf EP |
195 | } |
196 | } | |
197 | ||
a2d8bc6c EP |
198 | spin_lock(&event->lock); |
199 | ||
200 | if (list_empty(&event->holder.event_list)) { | |
201 | if (unlikely(holder)) | |
202 | fsnotify_destroy_event_holder(holder); | |
203 | holder = &event->holder; | |
204 | } else if (unlikely(!holder)) { | |
205 | /* between the time we checked above and got the lock the in | |
206 | * event holder was used, go back and get a new one */ | |
207 | spin_unlock(&event->lock); | |
208 | mutex_unlock(&group->notification_mutex); | |
f70ab54c EP |
209 | |
210 | if (return_event) { | |
211 | fsnotify_put_event(return_event); | |
212 | return_event = NULL; | |
213 | } | |
214 | ||
a2d8bc6c EP |
215 | goto alloc_holder; |
216 | } | |
217 | ||
a2d8bc6c EP |
218 | group->q_len++; |
219 | holder->event = event; | |
220 | ||
221 | fsnotify_get_event(event); | |
222 | list_add_tail(&holder->event_list, list); | |
e4aff117 EP |
223 | if (priv) |
224 | list_add_tail(&priv->event_list, &event->private_data_list); | |
a2d8bc6c EP |
225 | spin_unlock(&event->lock); |
226 | mutex_unlock(&group->notification_mutex); | |
227 | ||
228 | wake_up(&group->notification_waitq); | |
f70ab54c | 229 | return return_event; |
a2d8bc6c EP |
230 | } |
231 | ||
232 | /* | |
233 | * Remove and return the first event from the notification list. There is a | |
234 | * reference held on this event since it was on the list. It is the responsibility | |
235 | * of the caller to drop this reference. | |
236 | */ | |
237 | struct fsnotify_event *fsnotify_remove_notify_event(struct fsnotify_group *group) | |
90586523 EP |
238 | { |
239 | struct fsnotify_event *event; | |
a2d8bc6c | 240 | struct fsnotify_event_holder *holder; |
90586523 | 241 | |
a2d8bc6c | 242 | BUG_ON(!mutex_is_locked(&group->notification_mutex)); |
90586523 | 243 | |
5ba08e2e EP |
244 | pr_debug("%s: group=%p\n", __func__, group); |
245 | ||
a2d8bc6c EP |
246 | holder = list_first_entry(&group->notification_list, struct fsnotify_event_holder, event_list); |
247 | ||
248 | event = holder->event; | |
249 | ||
250 | spin_lock(&event->lock); | |
251 | holder->event = NULL; | |
252 | list_del_init(&holder->event_list); | |
253 | spin_unlock(&event->lock); | |
254 | ||
255 | /* event == holder means we are referenced through the in event holder */ | |
256 | if (holder != &event->holder) | |
257 | fsnotify_destroy_event_holder(holder); | |
258 | ||
259 | group->q_len--; | |
260 | ||
261 | return event; | |
262 | } | |
263 | ||
264 | /* | |
265 | * This will not remove the event, that must be done with fsnotify_remove_notify_event() | |
266 | */ | |
267 | struct fsnotify_event *fsnotify_peek_notify_event(struct fsnotify_group *group) | |
268 | { | |
269 | struct fsnotify_event *event; | |
270 | struct fsnotify_event_holder *holder; | |
271 | ||
272 | BUG_ON(!mutex_is_locked(&group->notification_mutex)); | |
273 | ||
274 | holder = list_first_entry(&group->notification_list, struct fsnotify_event_holder, event_list); | |
275 | event = holder->event; | |
276 | ||
277 | return event; | |
278 | } | |
279 | ||
280 | /* | |
281 | * Called when a group is being torn down to clean up any outstanding | |
282 | * event notifications. | |
283 | */ | |
284 | void fsnotify_flush_notify(struct fsnotify_group *group) | |
285 | { | |
286 | struct fsnotify_event *event; | |
e4aff117 | 287 | struct fsnotify_event_private_data *priv; |
a2d8bc6c EP |
288 | |
289 | mutex_lock(&group->notification_mutex); | |
290 | while (!fsnotify_notify_queue_is_empty(group)) { | |
291 | event = fsnotify_remove_notify_event(group); | |
e4aff117 EP |
292 | /* if they don't implement free_event_priv they better not have attached any */ |
293 | if (group->ops->free_event_priv) { | |
294 | spin_lock(&event->lock); | |
295 | priv = fsnotify_remove_priv_from_event(group, event); | |
296 | spin_unlock(&event->lock); | |
297 | if (priv) | |
298 | group->ops->free_event_priv(priv); | |
299 | } | |
a2d8bc6c EP |
300 | fsnotify_put_event(event); /* matches fsnotify_add_notify_event */ |
301 | } | |
302 | mutex_unlock(&group->notification_mutex); | |
303 | } | |
304 | ||
305 | static void initialize_event(struct fsnotify_event *event) | |
306 | { | |
a2d8bc6c | 307 | INIT_LIST_HEAD(&event->holder.event_list); |
90586523 EP |
308 | atomic_set(&event->refcnt, 1); |
309 | ||
310 | spin_lock_init(&event->lock); | |
311 | ||
e4aff117 | 312 | INIT_LIST_HEAD(&event->private_data_list); |
a2d8bc6c EP |
313 | } |
314 | ||
1201a536 EP |
315 | /* |
316 | * Caller damn well better be holding whatever mutex is protecting the | |
cac69dad EP |
317 | * old_holder->event_list and the new_event must be a clean event which |
318 | * cannot be found anywhere else in the kernel. | |
1201a536 EP |
319 | */ |
320 | int fsnotify_replace_event(struct fsnotify_event_holder *old_holder, | |
321 | struct fsnotify_event *new_event) | |
322 | { | |
323 | struct fsnotify_event *old_event = old_holder->event; | |
cac69dad EP |
324 | struct fsnotify_event_holder *new_holder = &new_event->holder; |
325 | ||
326 | enum event_spinlock_class { | |
327 | SPINLOCK_OLD, | |
328 | SPINLOCK_NEW, | |
329 | }; | |
1201a536 | 330 | |
5ba08e2e EP |
331 | pr_debug("%s: old_event=%p new_event=%p\n", __func__, old_event, new_event); |
332 | ||
1201a536 | 333 | /* |
cac69dad EP |
334 | * if the new_event's embedded holder is in use someone |
335 | * screwed up and didn't give us a clean new event. | |
1201a536 | 336 | */ |
cac69dad | 337 | BUG_ON(!list_empty(&new_holder->event_list)); |
1201a536 | 338 | |
cac69dad EP |
339 | spin_lock_nested(&old_event->lock, SPINLOCK_OLD); |
340 | spin_lock_nested(&new_event->lock, SPINLOCK_NEW); | |
1201a536 EP |
341 | |
342 | new_holder->event = new_event; | |
343 | list_replace_init(&old_holder->event_list, &new_holder->event_list); | |
344 | ||
345 | spin_unlock(&new_event->lock); | |
346 | spin_unlock(&old_event->lock); | |
347 | ||
348 | /* event == holder means we are referenced through the in event holder */ | |
349 | if (old_holder != &old_event->holder) | |
350 | fsnotify_destroy_event_holder(old_holder); | |
351 | ||
352 | fsnotify_get_event(new_event); /* on the list take reference */ | |
353 | fsnotify_put_event(old_event); /* off the list, drop reference */ | |
354 | ||
355 | return 0; | |
356 | } | |
357 | ||
b4e4e140 EP |
358 | struct fsnotify_event *fsnotify_clone_event(struct fsnotify_event *old_event) |
359 | { | |
360 | struct fsnotify_event *event; | |
361 | ||
362 | event = kmem_cache_alloc(fsnotify_event_cachep, GFP_KERNEL); | |
363 | if (!event) | |
364 | return NULL; | |
365 | ||
5ba08e2e EP |
366 | pr_debug("%s: old_event=%p new_event=%p\n", __func__, old_event, event); |
367 | ||
b4e4e140 EP |
368 | memcpy(event, old_event, sizeof(*event)); |
369 | initialize_event(event); | |
370 | ||
371 | if (event->name_len) { | |
372 | event->file_name = kstrdup(old_event->file_name, GFP_KERNEL); | |
373 | if (!event->file_name) { | |
374 | kmem_cache_free(fsnotify_event_cachep, event); | |
375 | return NULL; | |
376 | } | |
377 | } | |
32c32632 | 378 | event->tgid = get_pid(old_event->tgid); |
3bcf3860 EP |
379 | if (event->data_type == FSNOTIFY_EVENT_FILE) |
380 | get_file(event->file); | |
b4e4e140 EP |
381 | |
382 | return event; | |
383 | } | |
384 | ||
a2d8bc6c EP |
385 | /* |
386 | * fsnotify_create_event - Allocate a new event which will be sent to each | |
387 | * group's handle_event function if the group was interested in this | |
388 | * particular event. | |
389 | * | |
390 | * @to_tell the inode which is supposed to receive the event (sometimes a | |
391 | * parent of the inode to which the event happened. | |
392 | * @mask what actually happened. | |
393 | * @data pointer to the object which was actually affected | |
394 | * @data_type flag indication if the data is a file, path, inode, nothing... | |
62ffe5df | 395 | * @name the filename, if available |
a2d8bc6c | 396 | */ |
47882c6f | 397 | struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u32 mask, void *data, |
59b0df21 EP |
398 | int data_type, const unsigned char *name, |
399 | u32 cookie, gfp_t gfp) | |
a2d8bc6c EP |
400 | { |
401 | struct fsnotify_event *event; | |
402 | ||
6f3a539e | 403 | event = kmem_cache_zalloc(fsnotify_event_cachep, gfp); |
a2d8bc6c EP |
404 | if (!event) |
405 | return NULL; | |
406 | ||
5ba08e2e EP |
407 | pr_debug("%s: event=%p to_tell=%p mask=%x data=%p data_type=%d\n", |
408 | __func__, event, to_tell, mask, data, data_type); | |
409 | ||
a2d8bc6c | 410 | initialize_event(event); |
62ffe5df EP |
411 | |
412 | if (name) { | |
f44aebcc | 413 | event->file_name = kstrdup(name, gfp); |
62ffe5df EP |
414 | if (!event->file_name) { |
415 | kmem_cache_free(fsnotify_event_cachep, event); | |
416 | return NULL; | |
417 | } | |
418 | event->name_len = strlen(event->file_name); | |
419 | } | |
47882c6f | 420 | |
32c32632 | 421 | event->tgid = get_pid(task_tgid(current)); |
47882c6f | 422 | event->sync_cookie = cookie; |
90586523 | 423 | event->to_tell = to_tell; |
b4e4e140 | 424 | event->data_type = data_type; |
90586523 EP |
425 | |
426 | switch (data_type) { | |
3bcf3860 EP |
427 | case FSNOTIFY_EVENT_FILE: { |
428 | event->file = data; | |
c1e5c954 EP |
429 | /* |
430 | * if this file is about to disappear hold an extra reference | |
431 | * until we return to __fput so we don't have to worry about | |
432 | * future get/put destroying the file under us or generating | |
433 | * additional events. Notice that we change f_mode without | |
434 | * holding f_lock. This is safe since this is the only possible | |
435 | * reference to this object in the kernel (it was about to be | |
436 | * freed, remember?) | |
437 | */ | |
438 | if (!atomic_long_read(&event->file->f_count)) { | |
439 | event->file->f_mode |= FMODE_NONOTIFY; | |
440 | get_file(event->file); | |
441 | } | |
3bcf3860 | 442 | get_file(event->file); |
90586523 EP |
443 | break; |
444 | } | |
445 | case FSNOTIFY_EVENT_INODE: | |
446 | event->inode = data; | |
90586523 EP |
447 | break; |
448 | case FSNOTIFY_EVENT_NONE: | |
449 | event->inode = NULL; | |
3bcf3860 | 450 | event->file = NULL; |
90586523 EP |
451 | break; |
452 | default: | |
453 | BUG(); | |
454 | } | |
455 | ||
456 | event->mask = mask; | |
457 | ||
458 | return event; | |
459 | } | |
460 | ||
461 | __init int fsnotify_notification_init(void) | |
462 | { | |
463 | fsnotify_event_cachep = KMEM_CACHE(fsnotify_event, SLAB_PANIC); | |
a2d8bc6c EP |
464 | fsnotify_event_holder_cachep = KMEM_CACHE(fsnotify_event_holder, SLAB_PANIC); |
465 | ||
b4277d3d EP |
466 | q_overflow_event = fsnotify_create_event(NULL, FS_Q_OVERFLOW, NULL, |
467 | FSNOTIFY_EVENT_NONE, NULL, 0, | |
468 | GFP_KERNEL); | |
469 | if (!q_overflow_event) | |
470 | panic("unable to allocate fsnotify q_overflow_event\n"); | |
90586523 EP |
471 | |
472 | return 0; | |
473 | } | |
474 | subsys_initcall(fsnotify_notification_init); | |
475 |