fsnotify: replace an event on a list
authorEric Paris <eparis@redhat.com>
Fri, 18 Dec 2009 02:24:22 +0000 (21:24 -0500)
committerEric Paris <eparis@redhat.com>
Wed, 28 Jul 2010 13:58:49 +0000 (09:58 -0400)
fanotify would like to clone events already on its notification list, make
changes to the new event, and then replace the old event on the list with
the new event.  This patch implements the replace functionality of that
process.

Signed-off-by: Eric Paris <eparis@redhat.com>
fs/notify/notification.c
include/linux/fsnotify_backend.h

index bc9470c7ece7f3c1f808876585171ac7547362e6..b493c378445fb459a198e8cdc1e00c315b9d72e0 100644 (file)
@@ -287,6 +287,62 @@ static void initialize_event(struct fsnotify_event *event)
        INIT_LIST_HEAD(&event->private_data_list);
 }
 
+/*
+ * Caller damn well better be holding whatever mutex is protecting the
+ * old_holder->event_list.
+ */
+int fsnotify_replace_event(struct fsnotify_event_holder *old_holder,
+                          struct fsnotify_event *new_event)
+{
+       struct fsnotify_event *old_event = old_holder->event;
+       struct fsnotify_event_holder *new_holder = NULL;
+
+       /*
+        * There is one fsnotify_event_holder embedded inside each fsnotify_event.
+        * Check if we expect to be able to use that holder.  If not alloc a new
+        * holder.
+        * For the overflow event it's possible that something will use the in
+        * event holder before we get the lock so we may need to jump back and
+        * alloc a new holder, this can't happen for most events...
+        */
+       if (!list_empty(&new_event->holder.event_list)) {
+alloc_holder:
+               new_holder = fsnotify_alloc_event_holder();
+               if (!new_holder)
+                       return -ENOMEM;
+       }
+
+       spin_lock(&old_event->lock);
+       spin_lock(&new_event->lock);
+
+       if (list_empty(&new_event->holder.event_list)) {
+               if (unlikely(new_holder))
+                       fsnotify_destroy_event_holder(new_holder);
+               new_holder = &new_event->holder;
+       } else if (unlikely(!new_holder)) {
+               /* between the time we checked above and got the lock the in
+                * event holder was used, go back and get a new one */
+               spin_unlock(&new_event->lock);
+               spin_unlock(&old_event->lock);
+               goto alloc_holder;
+       }
+
+       new_holder->event = new_event;
+       list_replace_init(&old_holder->event_list, &new_holder->event_list);
+
+       spin_unlock(&new_event->lock);
+       spin_unlock(&old_event->lock);
+
+       /* event == holder means we are referenced through the in event holder */
+       if (old_holder != &old_event->holder)
+               fsnotify_destroy_event_holder(old_holder);
+
+       fsnotify_get_event(new_event); /* on the list take reference */
+       fsnotify_put_event(old_event); /* off the list, drop reference */
+
+       return 0;
+}
+
 struct fsnotify_event *fsnotify_clone_event(struct fsnotify_event *old_event)
 {
        struct fsnotify_event *event;
index 3a7fff235539f2514f397f646e44372c8a77e6cf..427f6ffab127dcbce004d20f28b40d396ef715b2 100644 (file)
@@ -365,6 +365,8 @@ extern struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u32
 
 /* fanotify likes to change events after they are on lists... */
 extern struct fsnotify_event *fsnotify_clone_event(struct fsnotify_event *old_event);
+extern int fsnotify_replace_event(struct fsnotify_event_holder *old_holder,
+                                 struct fsnotify_event *new_event);
 
 #else
 
This page took 0.026363 seconds and 5 git commands to generate.