]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/notify/notification.c
fsnotify: intoduce a notification merge argument
[net-next-2.6.git] / fs / notify / notification.c
CommitLineData
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
90586523
EP
34#include <linux/fs.h>
35#include <linux/init.h>
36#include <linux/kernel.h>
37#include <linux/list.h>
47882c6f 38#include <linux/module.h>
90586523
EP
39#include <linux/mount.h>
40#include <linux/mutex.h>
41#include <linux/namei.h>
42#include <linux/path.h>
43#include <linux/slab.h>
44#include <linux/spinlock.h>
45
46#include <asm/atomic.h>
47
48#include <linux/fsnotify_backend.h>
49#include "fsnotify.h"
50
51static struct kmem_cache *fsnotify_event_cachep;
a2d8bc6c
EP
52static struct kmem_cache *fsnotify_event_holder_cachep;
53/*
54 * This is a magic event we send when the q is too full. Since it doesn't
55 * hold real event information we just keep one system wide and use it any time
56 * it is needed. It's refcnt is set 1 at kernel init time and will never
57 * get set to 0 so it will never get 'freed'
58 */
b4277d3d 59static struct fsnotify_event *q_overflow_event;
47882c6f
EP
60static atomic_t fsnotify_sync_cookie = ATOMIC_INIT(0);
61
62/**
63 * fsnotify_get_cookie - return a unique cookie for use in synchronizing events.
64 * Called from fsnotify_move, which is inlined into filesystem modules.
65 */
66u32 fsnotify_get_cookie(void)
67{
68 return atomic_inc_return(&fsnotify_sync_cookie);
69}
70EXPORT_SYMBOL_GPL(fsnotify_get_cookie);
a2d8bc6c
EP
71
72/* return true if the notify queue is empty, false otherwise */
73bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group)
74{
75 BUG_ON(!mutex_is_locked(&group->notification_mutex));
76 return list_empty(&group->notification_list) ? true : false;
77}
90586523
EP
78
79void fsnotify_get_event(struct fsnotify_event *event)
80{
81 atomic_inc(&event->refcnt);
82}
83
84void fsnotify_put_event(struct fsnotify_event *event)
85{
86 if (!event)
87 return;
88
89 if (atomic_dec_and_test(&event->refcnt)) {
90 if (event->data_type == FSNOTIFY_EVENT_PATH)
91 path_put(&event->path);
92
e4aff117
EP
93 BUG_ON(!list_empty(&event->private_data_list));
94
62ffe5df 95 kfree(event->file_name);
32c32632 96 put_pid(event->tgid);
90586523
EP
97 kmem_cache_free(fsnotify_event_cachep, event);
98 }
99}
100
a2d8bc6c
EP
101struct fsnotify_event_holder *fsnotify_alloc_event_holder(void)
102{
103 return kmem_cache_alloc(fsnotify_event_holder_cachep, GFP_KERNEL);
104}
105
106void fsnotify_destroy_event_holder(struct fsnotify_event_holder *holder)
107{
74766bbf
EP
108 if (holder)
109 kmem_cache_free(fsnotify_event_holder_cachep, holder);
a2d8bc6c
EP
110}
111
112/*
e4aff117
EP
113 * Find the private data that the group previously attached to this event when
114 * the group added the event to the notification queue (fsnotify_add_notify_event)
115 */
116struct fsnotify_event_private_data *fsnotify_remove_priv_from_event(struct fsnotify_group *group, struct fsnotify_event *event)
117{
118 struct fsnotify_event_private_data *lpriv;
119 struct fsnotify_event_private_data *priv = NULL;
120
121 assert_spin_locked(&event->lock);
122
123 list_for_each_entry(lpriv, &event->private_data_list, event_list) {
124 if (lpriv->group == group) {
125 priv = lpriv;
126 list_del(&priv->event_list);
127 break;
128 }
129 }
130 return priv;
131}
132
90586523 133/*
a2d8bc6c
EP
134 * Add an event to the group notification queue. The group can later pull this
135 * event off the queue to deal with. If the event is successfully added to the
136 * group's notification queue, a reference is taken on event.
90586523 137 */
e4aff117 138int fsnotify_add_notify_event(struct fsnotify_group *group, struct fsnotify_event *event,
74766bbf 139 struct fsnotify_event_private_data *priv,
6e5f77b3
EP
140 int (*merge)(struct list_head *,
141 struct fsnotify_event *,
142 void **arg),
143 void **arg)
a2d8bc6c
EP
144{
145 struct fsnotify_event_holder *holder = NULL;
146 struct list_head *list = &group->notification_list;
74766bbf 147 int rc = 0;
e4aff117 148
a2d8bc6c
EP
149 /*
150 * There is one fsnotify_event_holder embedded inside each fsnotify_event.
151 * Check if we expect to be able to use that holder. If not alloc a new
152 * holder.
153 * For the overflow event it's possible that something will use the in
154 * event holder before we get the lock so we may need to jump back and
155 * alloc a new holder, this can't happen for most events...
156 */
157 if (!list_empty(&event->holder.event_list)) {
158alloc_holder:
159 holder = fsnotify_alloc_event_holder();
160 if (!holder)
161 return -ENOMEM;
162 }
163
164 mutex_lock(&group->notification_mutex);
165
e4aff117 166 if (group->q_len >= group->max_events) {
b4277d3d 167 event = q_overflow_event;
74766bbf 168 rc = -EOVERFLOW;
e4aff117
EP
169 /* sorry, no private data on the overflow event */
170 priv = NULL;
171 }
a2d8bc6c 172
74766bbf
EP
173 if (!list_empty(list) && merge) {
174 int ret;
175
6e5f77b3 176 ret = merge(list, event, arg);
74766bbf
EP
177 if (ret) {
178 mutex_unlock(&group->notification_mutex);
179 if (holder != &event->holder)
180 fsnotify_destroy_event_holder(holder);
181 return ret;
182 }
183 }
184
a2d8bc6c
EP
185 spin_lock(&event->lock);
186
187 if (list_empty(&event->holder.event_list)) {
188 if (unlikely(holder))
189 fsnotify_destroy_event_holder(holder);
190 holder = &event->holder;
191 } else if (unlikely(!holder)) {
192 /* between the time we checked above and got the lock the in
193 * event holder was used, go back and get a new one */
194 spin_unlock(&event->lock);
195 mutex_unlock(&group->notification_mutex);
196 goto alloc_holder;
197 }
198
a2d8bc6c
EP
199 group->q_len++;
200 holder->event = event;
201
202 fsnotify_get_event(event);
203 list_add_tail(&holder->event_list, list);
e4aff117
EP
204 if (priv)
205 list_add_tail(&priv->event_list, &event->private_data_list);
a2d8bc6c
EP
206 spin_unlock(&event->lock);
207 mutex_unlock(&group->notification_mutex);
208
209 wake_up(&group->notification_waitq);
74766bbf 210 return rc;
a2d8bc6c
EP
211}
212
213/*
214 * Remove and return the first event from the notification list. There is a
215 * reference held on this event since it was on the list. It is the responsibility
216 * of the caller to drop this reference.
217 */
218struct fsnotify_event *fsnotify_remove_notify_event(struct fsnotify_group *group)
90586523
EP
219{
220 struct fsnotify_event *event;
a2d8bc6c 221 struct fsnotify_event_holder *holder;
90586523 222
a2d8bc6c 223 BUG_ON(!mutex_is_locked(&group->notification_mutex));
90586523 224
a2d8bc6c
EP
225 holder = list_first_entry(&group->notification_list, struct fsnotify_event_holder, event_list);
226
227 event = holder->event;
228
229 spin_lock(&event->lock);
230 holder->event = NULL;
231 list_del_init(&holder->event_list);
232 spin_unlock(&event->lock);
233
234 /* event == holder means we are referenced through the in event holder */
235 if (holder != &event->holder)
236 fsnotify_destroy_event_holder(holder);
237
238 group->q_len--;
239
240 return event;
241}
242
243/*
244 * This will not remove the event, that must be done with fsnotify_remove_notify_event()
245 */
246struct fsnotify_event *fsnotify_peek_notify_event(struct fsnotify_group *group)
247{
248 struct fsnotify_event *event;
249 struct fsnotify_event_holder *holder;
250
251 BUG_ON(!mutex_is_locked(&group->notification_mutex));
252
253 holder = list_first_entry(&group->notification_list, struct fsnotify_event_holder, event_list);
254 event = holder->event;
255
256 return event;
257}
258
259/*
260 * Called when a group is being torn down to clean up any outstanding
261 * event notifications.
262 */
263void fsnotify_flush_notify(struct fsnotify_group *group)
264{
265 struct fsnotify_event *event;
e4aff117 266 struct fsnotify_event_private_data *priv;
a2d8bc6c
EP
267
268 mutex_lock(&group->notification_mutex);
269 while (!fsnotify_notify_queue_is_empty(group)) {
270 event = fsnotify_remove_notify_event(group);
e4aff117
EP
271 /* if they don't implement free_event_priv they better not have attached any */
272 if (group->ops->free_event_priv) {
273 spin_lock(&event->lock);
274 priv = fsnotify_remove_priv_from_event(group, event);
275 spin_unlock(&event->lock);
276 if (priv)
277 group->ops->free_event_priv(priv);
278 }
a2d8bc6c
EP
279 fsnotify_put_event(event); /* matches fsnotify_add_notify_event */
280 }
281 mutex_unlock(&group->notification_mutex);
282}
283
284static void initialize_event(struct fsnotify_event *event)
285{
a2d8bc6c 286 INIT_LIST_HEAD(&event->holder.event_list);
90586523
EP
287 atomic_set(&event->refcnt, 1);
288
289 spin_lock_init(&event->lock);
290
e4aff117 291 INIT_LIST_HEAD(&event->private_data_list);
a2d8bc6c
EP
292}
293
1201a536
EP
294/*
295 * Caller damn well better be holding whatever mutex is protecting the
cac69dad
EP
296 * old_holder->event_list and the new_event must be a clean event which
297 * cannot be found anywhere else in the kernel.
1201a536
EP
298 */
299int fsnotify_replace_event(struct fsnotify_event_holder *old_holder,
300 struct fsnotify_event *new_event)
301{
302 struct fsnotify_event *old_event = old_holder->event;
cac69dad
EP
303 struct fsnotify_event_holder *new_holder = &new_event->holder;
304
305 enum event_spinlock_class {
306 SPINLOCK_OLD,
307 SPINLOCK_NEW,
308 };
1201a536
EP
309
310 /*
cac69dad
EP
311 * if the new_event's embedded holder is in use someone
312 * screwed up and didn't give us a clean new event.
1201a536 313 */
cac69dad 314 BUG_ON(!list_empty(&new_holder->event_list));
1201a536 315
cac69dad
EP
316 spin_lock_nested(&old_event->lock, SPINLOCK_OLD);
317 spin_lock_nested(&new_event->lock, SPINLOCK_NEW);
1201a536
EP
318
319 new_holder->event = new_event;
320 list_replace_init(&old_holder->event_list, &new_holder->event_list);
321
322 spin_unlock(&new_event->lock);
323 spin_unlock(&old_event->lock);
324
325 /* event == holder means we are referenced through the in event holder */
326 if (old_holder != &old_event->holder)
327 fsnotify_destroy_event_holder(old_holder);
328
329 fsnotify_get_event(new_event); /* on the list take reference */
330 fsnotify_put_event(old_event); /* off the list, drop reference */
331
332 return 0;
333}
334
b4e4e140
EP
335struct fsnotify_event *fsnotify_clone_event(struct fsnotify_event *old_event)
336{
337 struct fsnotify_event *event;
338
339 event = kmem_cache_alloc(fsnotify_event_cachep, GFP_KERNEL);
340 if (!event)
341 return NULL;
342
343 memcpy(event, old_event, sizeof(*event));
344 initialize_event(event);
345
346 if (event->name_len) {
347 event->file_name = kstrdup(old_event->file_name, GFP_KERNEL);
348 if (!event->file_name) {
349 kmem_cache_free(fsnotify_event_cachep, event);
350 return NULL;
351 }
352 }
32c32632 353 event->tgid = get_pid(old_event->tgid);
b4e4e140
EP
354 if (event->data_type == FSNOTIFY_EVENT_PATH)
355 path_get(&event->path);
356
357 return event;
358}
359
a2d8bc6c
EP
360/*
361 * fsnotify_create_event - Allocate a new event which will be sent to each
362 * group's handle_event function if the group was interested in this
363 * particular event.
364 *
365 * @to_tell the inode which is supposed to receive the event (sometimes a
366 * parent of the inode to which the event happened.
367 * @mask what actually happened.
368 * @data pointer to the object which was actually affected
369 * @data_type flag indication if the data is a file, path, inode, nothing...
62ffe5df 370 * @name the filename, if available
a2d8bc6c 371 */
47882c6f 372struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u32 mask, void *data,
f44aebcc
EP
373 int data_type, const char *name, u32 cookie,
374 gfp_t gfp)
a2d8bc6c
EP
375{
376 struct fsnotify_event *event;
377
6f3a539e 378 event = kmem_cache_zalloc(fsnotify_event_cachep, gfp);
a2d8bc6c
EP
379 if (!event)
380 return NULL;
381
382 initialize_event(event);
62ffe5df
EP
383
384 if (name) {
f44aebcc 385 event->file_name = kstrdup(name, gfp);
62ffe5df
EP
386 if (!event->file_name) {
387 kmem_cache_free(fsnotify_event_cachep, event);
388 return NULL;
389 }
390 event->name_len = strlen(event->file_name);
391 }
47882c6f 392
32c32632 393 event->tgid = get_pid(task_tgid(current));
47882c6f 394 event->sync_cookie = cookie;
90586523 395 event->to_tell = to_tell;
b4e4e140 396 event->data_type = data_type;
90586523
EP
397
398 switch (data_type) {
90586523
EP
399 case FSNOTIFY_EVENT_PATH: {
400 struct path *path = data;
401 event->path.dentry = path->dentry;
402 event->path.mnt = path->mnt;
403 path_get(&event->path);
90586523
EP
404 break;
405 }
406 case FSNOTIFY_EVENT_INODE:
407 event->inode = data;
90586523
EP
408 break;
409 case FSNOTIFY_EVENT_NONE:
410 event->inode = NULL;
411 event->path.dentry = NULL;
412 event->path.mnt = NULL;
413 break;
414 default:
415 BUG();
416 }
417
418 event->mask = mask;
419
420 return event;
421}
422
423__init int fsnotify_notification_init(void)
424{
425 fsnotify_event_cachep = KMEM_CACHE(fsnotify_event, SLAB_PANIC);
a2d8bc6c
EP
426 fsnotify_event_holder_cachep = KMEM_CACHE(fsnotify_event_holder, SLAB_PANIC);
427
b4277d3d
EP
428 q_overflow_event = fsnotify_create_event(NULL, FS_Q_OVERFLOW, NULL,
429 FSNOTIFY_EVENT_NONE, NULL, 0,
430 GFP_KERNEL);
431 if (!q_overflow_event)
432 panic("unable to allocate fsnotify q_overflow_event\n");
90586523
EP
433
434 return 0;
435}
436subsys_initcall(fsnotify_notification_init);
437