fsnotify: include pathnames with entries when possible
[safe/jmp/linux-2.6] / fs / notify / notification.c
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
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
34 #include <linux/fs.h>
35 #include <linux/init.h>
36 #include <linux/kernel.h>
37 #include <linux/list.h>
38 #include <linux/mount.h>
39 #include <linux/mutex.h>
40 #include <linux/namei.h>
41 #include <linux/path.h>
42 #include <linux/slab.h>
43 #include <linux/spinlock.h>
44
45 #include <asm/atomic.h>
46
47 #include <linux/fsnotify_backend.h>
48 #include "fsnotify.h"
49
50 static struct kmem_cache *fsnotify_event_cachep;
51 static struct kmem_cache *fsnotify_event_holder_cachep;
52 /*
53  * This is a magic event we send when the q is too full.  Since it doesn't
54  * hold real event information we just keep one system wide and use it any time
55  * it is needed.  It's refcnt is set 1 at kernel init time and will never
56  * get set to 0 so it will never get 'freed'
57  */
58 static struct fsnotify_event q_overflow_event;
59
60 /* return true if the notify queue is empty, false otherwise */
61 bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group)
62 {
63         BUG_ON(!mutex_is_locked(&group->notification_mutex));
64         return list_empty(&group->notification_list) ? true : false;
65 }
66
67 void fsnotify_get_event(struct fsnotify_event *event)
68 {
69         atomic_inc(&event->refcnt);
70 }
71
72 void fsnotify_put_event(struct fsnotify_event *event)
73 {
74         if (!event)
75                 return;
76
77         if (atomic_dec_and_test(&event->refcnt)) {
78                 if (event->data_type == FSNOTIFY_EVENT_PATH)
79                         path_put(&event->path);
80
81                 kfree(event->file_name);
82                 kmem_cache_free(fsnotify_event_cachep, event);
83         }
84 }
85
86 struct fsnotify_event_holder *fsnotify_alloc_event_holder(void)
87 {
88         return kmem_cache_alloc(fsnotify_event_holder_cachep, GFP_KERNEL);
89 }
90
91 void fsnotify_destroy_event_holder(struct fsnotify_event_holder *holder)
92 {
93         kmem_cache_free(fsnotify_event_holder_cachep, holder);
94 }
95
96 /*
97  * check if 2 events contain the same information.
98  */
99 static bool event_compare(struct fsnotify_event *old, struct fsnotify_event *new)
100 {
101         if ((old->mask == new->mask) &&
102             (old->to_tell == new->to_tell) &&
103             (old->data_type == new->data_type)) {
104                 switch (old->data_type) {
105                 case (FSNOTIFY_EVENT_INODE):
106                         if (old->inode == new->inode)
107                                 return true;
108                         break;
109                 case (FSNOTIFY_EVENT_PATH):
110                         if ((old->path.mnt == new->path.mnt) &&
111                             (old->path.dentry == new->path.dentry))
112                                 return true;
113                 case (FSNOTIFY_EVENT_NONE):
114                         return true;
115                 };
116         }
117         return false;
118 }
119
120 /*
121  * Add an event to the group notification queue.  The group can later pull this
122  * event off the queue to deal with.  If the event is successfully added to the
123  * group's notification queue, a reference is taken on event.
124  */
125 int fsnotify_add_notify_event(struct fsnotify_group *group, struct fsnotify_event *event)
126 {
127         struct fsnotify_event_holder *holder = NULL;
128         struct list_head *list = &group->notification_list;
129         struct fsnotify_event_holder *last_holder;
130         struct fsnotify_event *last_event;
131
132         /*
133          * There is one fsnotify_event_holder embedded inside each fsnotify_event.
134          * Check if we expect to be able to use that holder.  If not alloc a new
135          * holder.
136          * For the overflow event it's possible that something will use the in
137          * event holder before we get the lock so we may need to jump back and
138          * alloc a new holder, this can't happen for most events...
139          */
140         if (!list_empty(&event->holder.event_list)) {
141 alloc_holder:
142                 holder = fsnotify_alloc_event_holder();
143                 if (!holder)
144                         return -ENOMEM;
145         }
146
147         mutex_lock(&group->notification_mutex);
148
149         if (group->q_len >= group->max_events)
150                 event = &q_overflow_event;
151
152         spin_lock(&event->lock);
153
154         if (list_empty(&event->holder.event_list)) {
155                 if (unlikely(holder))
156                         fsnotify_destroy_event_holder(holder);
157                 holder = &event->holder;
158         } else if (unlikely(!holder)) {
159                 /* between the time we checked above and got the lock the in
160                  * event holder was used, go back and get a new one */
161                 spin_unlock(&event->lock);
162                 mutex_unlock(&group->notification_mutex);
163                 goto alloc_holder;
164         }
165
166         if (!list_empty(list)) {
167                 last_holder = list_entry(list->prev, struct fsnotify_event_holder, event_list);
168                 last_event = last_holder->event;
169                 if (event_compare(last_event, event)) {
170                         spin_unlock(&event->lock);
171                         mutex_unlock(&group->notification_mutex);
172                         if (holder != &event->holder)
173                                 fsnotify_destroy_event_holder(holder);
174                         return 0;
175                 }
176         }
177
178         group->q_len++;
179         holder->event = event;
180
181         fsnotify_get_event(event);
182         list_add_tail(&holder->event_list, list);
183         spin_unlock(&event->lock);
184         mutex_unlock(&group->notification_mutex);
185
186         wake_up(&group->notification_waitq);
187         return 0;
188 }
189
190 /*
191  * Remove and return the first event from the notification list.  There is a
192  * reference held on this event since it was on the list.  It is the responsibility
193  * of the caller to drop this reference.
194  */
195 struct fsnotify_event *fsnotify_remove_notify_event(struct fsnotify_group *group)
196 {
197         struct fsnotify_event *event;
198         struct fsnotify_event_holder *holder;
199
200         BUG_ON(!mutex_is_locked(&group->notification_mutex));
201
202         holder = list_first_entry(&group->notification_list, struct fsnotify_event_holder, event_list);
203
204         event = holder->event;
205
206         spin_lock(&event->lock);
207         holder->event = NULL;
208         list_del_init(&holder->event_list);
209         spin_unlock(&event->lock);
210
211         /* event == holder means we are referenced through the in event holder */
212         if (holder != &event->holder)
213                 fsnotify_destroy_event_holder(holder);
214
215         group->q_len--;
216
217         return event;
218 }
219
220 /*
221  * This will not remove the event, that must be done with fsnotify_remove_notify_event()
222  */
223 struct fsnotify_event *fsnotify_peek_notify_event(struct fsnotify_group *group)
224 {
225         struct fsnotify_event *event;
226         struct fsnotify_event_holder *holder;
227
228         BUG_ON(!mutex_is_locked(&group->notification_mutex));
229
230         holder = list_first_entry(&group->notification_list, struct fsnotify_event_holder, event_list);
231         event = holder->event;
232
233         return event;
234 }
235
236 /*
237  * Called when a group is being torn down to clean up any outstanding
238  * event notifications.
239  */
240 void fsnotify_flush_notify(struct fsnotify_group *group)
241 {
242         struct fsnotify_event *event;
243
244         mutex_lock(&group->notification_mutex);
245         while (!fsnotify_notify_queue_is_empty(group)) {
246                 event = fsnotify_remove_notify_event(group);
247                 fsnotify_put_event(event); /* matches fsnotify_add_notify_event */
248         }
249         mutex_unlock(&group->notification_mutex);
250 }
251
252 static void initialize_event(struct fsnotify_event *event)
253 {
254         event->holder.event = NULL;
255         INIT_LIST_HEAD(&event->holder.event_list);
256         atomic_set(&event->refcnt, 1);
257
258         spin_lock_init(&event->lock);
259
260         event->path.dentry = NULL;
261         event->path.mnt = NULL;
262         event->inode = NULL;
263         event->data_type = FSNOTIFY_EVENT_NONE;
264
265         event->to_tell = NULL;
266
267         event->file_name = NULL;
268         event->name_len = 0;
269 }
270
271 /*
272  * fsnotify_create_event - Allocate a new event which will be sent to each
273  * group's handle_event function if the group was interested in this
274  * particular event.
275  *
276  * @to_tell the inode which is supposed to receive the event (sometimes a
277  *      parent of the inode to which the event happened.
278  * @mask what actually happened.
279  * @data pointer to the object which was actually affected
280  * @data_type flag indication if the data is a file, path, inode, nothing...
281  * @name the filename, if available
282  */
283 struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u32 mask,
284                                              void *data, int data_type, const char *name)
285 {
286         struct fsnotify_event *event;
287
288         event = kmem_cache_alloc(fsnotify_event_cachep, GFP_KERNEL);
289         if (!event)
290                 return NULL;
291
292         initialize_event(event);
293
294         if (name) {
295                 event->file_name = kstrdup(name, GFP_KERNEL);
296                 if (!event->file_name) {
297                         kmem_cache_free(fsnotify_event_cachep, event);
298                         return NULL;
299                 }
300                 event->name_len = strlen(event->file_name);
301         }
302         event->to_tell = to_tell;
303
304         switch (data_type) {
305         case FSNOTIFY_EVENT_FILE: {
306                 struct file *file = data;
307                 struct path *path = &file->f_path;
308                 event->path.dentry = path->dentry;
309                 event->path.mnt = path->mnt;
310                 path_get(&event->path);
311                 event->data_type = FSNOTIFY_EVENT_PATH;
312                 break;
313         }
314         case FSNOTIFY_EVENT_PATH: {
315                 struct path *path = data;
316                 event->path.dentry = path->dentry;
317                 event->path.mnt = path->mnt;
318                 path_get(&event->path);
319                 event->data_type = FSNOTIFY_EVENT_PATH;
320                 break;
321         }
322         case FSNOTIFY_EVENT_INODE:
323                 event->inode = data;
324                 event->data_type = FSNOTIFY_EVENT_INODE;
325                 break;
326         case FSNOTIFY_EVENT_NONE:
327                 event->inode = NULL;
328                 event->path.dentry = NULL;
329                 event->path.mnt = NULL;
330                 break;
331         default:
332                 BUG();
333         }
334
335         event->mask = mask;
336
337         return event;
338 }
339
340 __init int fsnotify_notification_init(void)
341 {
342         fsnotify_event_cachep = KMEM_CACHE(fsnotify_event, SLAB_PANIC);
343         fsnotify_event_holder_cachep = KMEM_CACHE(fsnotify_event_holder, SLAB_PANIC);
344
345         initialize_event(&q_overflow_event);
346         q_overflow_event.mask = FS_Q_OVERFLOW;
347
348         return 0;
349 }
350 subsys_initcall(fsnotify_notification_init);
351