mm-only debug patch...
[mmotm.git] / fs / notify / notification.c
blobdafd0b7687b8b58a594124c7cac5839a9918fdb9
1 /*
2 * Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com>
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.
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.
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.
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.
34 #include <linux/fs.h>
35 #include <linux/init.h>
36 #include <linux/kernel.h>
37 #include <linux/list.h>
38 #include <linux/module.h>
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>
46 #include <asm/atomic.h>
48 #include <linux/fsnotify_backend.h>
49 #include "fsnotify.h"
51 static struct kmem_cache *fsnotify_event_cachep;
52 static struct kmem_cache *fsnotify_event_holder_cachep;
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'
59 static struct fsnotify_event *q_overflow_event;
60 static atomic_t fsnotify_sync_cookie = ATOMIC_INIT(0);
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.
66 u32 fsnotify_get_cookie(void)
68 return atomic_inc_return(&fsnotify_sync_cookie);
70 EXPORT_SYMBOL_GPL(fsnotify_get_cookie);
72 /* return true if the notify queue is empty, false otherwise */
73 bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group)
75 BUG_ON(!mutex_is_locked(&group->notification_mutex));
76 return list_empty(&group->notification_list) ? true : false;
79 void fsnotify_get_event(struct fsnotify_event *event)
81 atomic_inc(&event->refcnt);
84 void fsnotify_put_event(struct fsnotify_event *event)
86 if (!event)
87 return;
89 if (atomic_dec_and_test(&event->refcnt)) {
90 if (event->data_type == FSNOTIFY_EVENT_PATH)
91 path_put(&event->path);
93 BUG_ON(!list_empty(&event->private_data_list));
95 kfree(event->file_name);
96 kmem_cache_free(fsnotify_event_cachep, event);
100 struct fsnotify_event_holder *fsnotify_alloc_event_holder(void)
102 return kmem_cache_alloc(fsnotify_event_holder_cachep, GFP_KERNEL);
105 void fsnotify_destroy_event_holder(struct fsnotify_event_holder *holder)
107 if (holder)
108 kmem_cache_free(fsnotify_event_holder_cachep, holder);
112 * Find the private data that the group previously attached to this event when
113 * the group added the event to the notification queue (fsnotify_add_notify_event)
115 struct fsnotify_event_private_data *fsnotify_remove_priv_from_event(struct fsnotify_group *group, struct fsnotify_event *event)
117 struct fsnotify_event_private_data *lpriv;
118 struct fsnotify_event_private_data *priv = NULL;
120 assert_spin_locked(&event->lock);
122 list_for_each_entry(lpriv, &event->private_data_list, event_list) {
123 if (lpriv->group == group) {
124 priv = lpriv;
125 list_del(&priv->event_list);
126 break;
129 return priv;
133 * Add an event to the group notification queue. The group can later pull this
134 * event off the queue to deal with. If the event is successfully added to the
135 * group's notification queue, a reference is taken on event.
137 int fsnotify_add_notify_event(struct fsnotify_group *group, struct fsnotify_event *event,
138 struct fsnotify_event_private_data *priv,
139 int (*merge)(struct list_head *, struct fsnotify_event *))
141 struct fsnotify_event_holder *holder = NULL;
142 struct list_head *list = &group->notification_list;
143 int rc = 0;
146 * There is one fsnotify_event_holder embedded inside each fsnotify_event.
147 * Check if we expect to be able to use that holder. If not alloc a new
148 * holder.
149 * For the overflow event it's possible that something will use the in
150 * event holder before we get the lock so we may need to jump back and
151 * alloc a new holder, this can't happen for most events...
153 if (!list_empty(&event->holder.event_list)) {
154 alloc_holder:
155 holder = fsnotify_alloc_event_holder();
156 if (!holder)
157 return -ENOMEM;
160 mutex_lock(&group->notification_mutex);
162 if (group->q_len >= group->max_events) {
163 event = q_overflow_event;
164 rc = -EOVERFLOW;
165 /* sorry, no private data on the overflow event */
166 priv = NULL;
169 if (!list_empty(list) && merge) {
170 int ret;
172 ret = merge(list, event);
173 if (ret) {
174 mutex_unlock(&group->notification_mutex);
175 if (holder != &event->holder)
176 fsnotify_destroy_event_holder(holder);
177 return ret;
181 spin_lock(&event->lock);
183 if (list_empty(&event->holder.event_list)) {
184 if (unlikely(holder))
185 fsnotify_destroy_event_holder(holder);
186 holder = &event->holder;
187 } else if (unlikely(!holder)) {
188 /* between the time we checked above and got the lock the in
189 * event holder was used, go back and get a new one */
190 spin_unlock(&event->lock);
191 mutex_unlock(&group->notification_mutex);
192 goto alloc_holder;
195 group->q_len++;
196 holder->event = event;
198 fsnotify_get_event(event);
199 list_add_tail(&holder->event_list, list);
200 if (priv)
201 list_add_tail(&priv->event_list, &event->private_data_list);
202 spin_unlock(&event->lock);
203 mutex_unlock(&group->notification_mutex);
205 wake_up(&group->notification_waitq);
206 return rc;
210 * Remove and return the first event from the notification list. There is a
211 * reference held on this event since it was on the list. It is the responsibility
212 * of the caller to drop this reference.
214 struct fsnotify_event *fsnotify_remove_notify_event(struct fsnotify_group *group)
216 struct fsnotify_event *event;
217 struct fsnotify_event_holder *holder;
219 BUG_ON(!mutex_is_locked(&group->notification_mutex));
221 holder = list_first_entry(&group->notification_list, struct fsnotify_event_holder, event_list);
223 event = holder->event;
225 spin_lock(&event->lock);
226 holder->event = NULL;
227 list_del_init(&holder->event_list);
228 spin_unlock(&event->lock);
230 /* event == holder means we are referenced through the in event holder */
231 if (holder != &event->holder)
232 fsnotify_destroy_event_holder(holder);
234 group->q_len--;
236 return event;
240 * This will not remove the event, that must be done with fsnotify_remove_notify_event()
242 struct fsnotify_event *fsnotify_peek_notify_event(struct fsnotify_group *group)
244 struct fsnotify_event *event;
245 struct fsnotify_event_holder *holder;
247 BUG_ON(!mutex_is_locked(&group->notification_mutex));
249 holder = list_first_entry(&group->notification_list, struct fsnotify_event_holder, event_list);
250 event = holder->event;
252 return event;
256 * Called when a group is being torn down to clean up any outstanding
257 * event notifications.
259 void fsnotify_flush_notify(struct fsnotify_group *group)
261 struct fsnotify_event *event;
262 struct fsnotify_event_private_data *priv;
264 mutex_lock(&group->notification_mutex);
265 while (!fsnotify_notify_queue_is_empty(group)) {
266 event = fsnotify_remove_notify_event(group);
267 /* if they don't implement free_event_priv they better not have attached any */
268 if (group->ops->free_event_priv) {
269 spin_lock(&event->lock);
270 priv = fsnotify_remove_priv_from_event(group, event);
271 spin_unlock(&event->lock);
272 if (priv)
273 group->ops->free_event_priv(priv);
275 fsnotify_put_event(event); /* matches fsnotify_add_notify_event */
277 mutex_unlock(&group->notification_mutex);
280 static void initialize_event(struct fsnotify_event *event)
282 INIT_LIST_HEAD(&event->holder.event_list);
283 atomic_set(&event->refcnt, 1);
285 spin_lock_init(&event->lock);
287 INIT_LIST_HEAD(&event->private_data_list);
291 * Caller damn well better be holding whatever mutex is protecting the
292 * old_holder->event_list and the new_event must be a clean event which
293 * cannot be found anywhere else in the kernel.
295 int fsnotify_replace_event(struct fsnotify_event_holder *old_holder,
296 struct fsnotify_event *new_event)
298 struct fsnotify_event *old_event = old_holder->event;
299 struct fsnotify_event_holder *new_holder = &new_event->holder;
301 enum event_spinlock_class {
302 SPINLOCK_OLD,
303 SPINLOCK_NEW,
307 * if the new_event's embedded holder is in use someone
308 * screwed up and didn't give us a clean new event.
310 BUG_ON(!list_empty(&new_holder->event_list));
312 spin_lock_nested(&old_event->lock, SPINLOCK_OLD);
313 spin_lock_nested(&new_event->lock, SPINLOCK_NEW);
315 new_holder->event = new_event;
316 list_replace_init(&old_holder->event_list, &new_holder->event_list);
318 spin_unlock(&new_event->lock);
319 spin_unlock(&old_event->lock);
321 /* event == holder means we are referenced through the in event holder */
322 if (old_holder != &old_event->holder)
323 fsnotify_destroy_event_holder(old_holder);
325 fsnotify_get_event(new_event); /* on the list take reference */
326 fsnotify_put_event(old_event); /* off the list, drop reference */
328 return 0;
331 struct fsnotify_event *fsnotify_clone_event(struct fsnotify_event *old_event)
333 struct fsnotify_event *event;
335 event = kmem_cache_alloc(fsnotify_event_cachep, GFP_KERNEL);
336 if (!event)
337 return NULL;
339 memcpy(event, old_event, sizeof(*event));
340 initialize_event(event);
342 if (event->name_len) {
343 event->file_name = kstrdup(old_event->file_name, GFP_KERNEL);
344 if (!event->file_name) {
345 kmem_cache_free(fsnotify_event_cachep, event);
346 return NULL;
349 if (event->data_type == FSNOTIFY_EVENT_PATH)
350 path_get(&event->path);
352 return event;
356 * fsnotify_create_event - Allocate a new event which will be sent to each
357 * group's handle_event function if the group was interested in this
358 * particular event.
360 * @to_tell the inode which is supposed to receive the event (sometimes a
361 * parent of the inode to which the event happened.
362 * @mask what actually happened.
363 * @data pointer to the object which was actually affected
364 * @data_type flag indication if the data is a file, path, inode, nothing...
365 * @name the filename, if available
367 struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u32 mask, void *data,
368 int data_type, const char *name, u32 cookie,
369 gfp_t gfp)
371 struct fsnotify_event *event;
373 event = kmem_cache_zalloc(fsnotify_event_cachep, gfp);
374 if (!event)
375 return NULL;
377 initialize_event(event);
379 if (name) {
380 event->file_name = kstrdup(name, gfp);
381 if (!event->file_name) {
382 kmem_cache_free(fsnotify_event_cachep, event);
383 return NULL;
385 event->name_len = strlen(event->file_name);
388 event->sync_cookie = cookie;
389 event->to_tell = to_tell;
390 event->data_type = data_type;
392 switch (data_type) {
393 case FSNOTIFY_EVENT_FILE: {
394 struct file *file = data;
395 struct path *path = &file->f_path;
396 event->path.dentry = path->dentry;
397 event->path.mnt = path->mnt;
398 path_get(&event->path);
399 event->data_type = FSNOTIFY_EVENT_PATH;
400 break;
402 case FSNOTIFY_EVENT_PATH: {
403 struct path *path = data;
404 event->path.dentry = path->dentry;
405 event->path.mnt = path->mnt;
406 path_get(&event->path);
407 break;
409 case FSNOTIFY_EVENT_INODE:
410 event->inode = data;
411 break;
412 case FSNOTIFY_EVENT_NONE:
413 event->inode = NULL;
414 event->path.dentry = NULL;
415 event->path.mnt = NULL;
416 break;
417 default:
418 BUG();
421 event->mask = mask;
423 return event;
426 __init int fsnotify_notification_init(void)
428 fsnotify_event_cachep = KMEM_CACHE(fsnotify_event, SLAB_PANIC);
429 fsnotify_event_holder_cachep = KMEM_CACHE(fsnotify_event_holder, SLAB_PANIC);
431 q_overflow_event = fsnotify_create_event(NULL, FS_Q_OVERFLOW, NULL,
432 FSNOTIFY_EVENT_NONE, NULL, 0,
433 GFP_KERNEL);
434 if (!q_overflow_event)
435 panic("unable to allocate fsnotify q_overflow_event\n");
437 return 0;
439 subsys_initcall(fsnotify_notification_init);