]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/notify/notification.c
fsnotify: use kmem_cache_zalloc to simplify event initialization
[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);
90586523
EP
96 kmem_cache_free(fsnotify_event_cachep, event);
97 }
98}
99
a2d8bc6c
EP
100struct fsnotify_event_holder *fsnotify_alloc_event_holder(void)
101{
102 return kmem_cache_alloc(fsnotify_event_holder_cachep, GFP_KERNEL);
103}
104
105void fsnotify_destroy_event_holder(struct fsnotify_event_holder *holder)
106{
107 kmem_cache_free(fsnotify_event_holder_cachep, holder);
108}
109
110/*
e4aff117
EP
111 * Find the private data that the group previously attached to this event when
112 * the group added the event to the notification queue (fsnotify_add_notify_event)
113 */
114struct fsnotify_event_private_data *fsnotify_remove_priv_from_event(struct fsnotify_group *group, struct fsnotify_event *event)
115{
116 struct fsnotify_event_private_data *lpriv;
117 struct fsnotify_event_private_data *priv = NULL;
118
119 assert_spin_locked(&event->lock);
120
121 list_for_each_entry(lpriv, &event->private_data_list, event_list) {
122 if (lpriv->group == group) {
123 priv = lpriv;
124 list_del(&priv->event_list);
125 break;
126 }
127 }
128 return priv;
129}
130
131/*
132 * Check if 2 events contain the same information. We do not compare private data
133 * but at this moment that isn't a problem for any know fsnotify listeners.
a2d8bc6c
EP
134 */
135static bool event_compare(struct fsnotify_event *old, struct fsnotify_event *new)
136{
137 if ((old->mask == new->mask) &&
138 (old->to_tell == new->to_tell) &&
4a148ba9
EP
139 (old->data_type == new->data_type) &&
140 (old->name_len == new->name_len)) {
a2d8bc6c
EP
141 switch (old->data_type) {
142 case (FSNOTIFY_EVENT_INODE):
4a148ba9
EP
143 /* remember, after old was put on the wait_q we aren't
144 * allowed to look at the inode any more, only thing
145 * left to check was if the file_name is the same */
3de0ef4f 146 if (!old->name_len ||
4a148ba9 147 !strcmp(old->file_name, new->file_name))
a2d8bc6c
EP
148 return true;
149 break;
150 case (FSNOTIFY_EVENT_PATH):
151 if ((old->path.mnt == new->path.mnt) &&
152 (old->path.dentry == new->path.dentry))
153 return true;
c05594b6 154 break;
a2d8bc6c 155 case (FSNOTIFY_EVENT_NONE):
cd94c8bb
EP
156 if (old->mask & FS_Q_OVERFLOW)
157 return true;
158 else if (old->mask & FS_IN_IGNORED)
159 return false;
f44aebcc 160 return false;
a2d8bc6c
EP
161 };
162 }
163 return false;
164}
165
90586523 166/*
a2d8bc6c
EP
167 * Add an event to the group notification queue. The group can later pull this
168 * event off the queue to deal with. If the event is successfully added to the
169 * group's notification queue, a reference is taken on event.
90586523 170 */
e4aff117
EP
171int fsnotify_add_notify_event(struct fsnotify_group *group, struct fsnotify_event *event,
172 struct fsnotify_event_private_data *priv)
a2d8bc6c
EP
173{
174 struct fsnotify_event_holder *holder = NULL;
175 struct list_head *list = &group->notification_list;
176 struct fsnotify_event_holder *last_holder;
177 struct fsnotify_event *last_event;
eef3a116 178 int ret = 0;
e4aff117 179
a2d8bc6c
EP
180 /*
181 * There is one fsnotify_event_holder embedded inside each fsnotify_event.
182 * Check if we expect to be able to use that holder. If not alloc a new
183 * holder.
184 * For the overflow event it's possible that something will use the in
185 * event holder before we get the lock so we may need to jump back and
186 * alloc a new holder, this can't happen for most events...
187 */
188 if (!list_empty(&event->holder.event_list)) {
189alloc_holder:
190 holder = fsnotify_alloc_event_holder();
191 if (!holder)
192 return -ENOMEM;
193 }
194
195 mutex_lock(&group->notification_mutex);
196
e4aff117 197 if (group->q_len >= group->max_events) {
b4277d3d 198 event = q_overflow_event;
eef3a116 199 ret = -EOVERFLOW;
e4aff117
EP
200 /* sorry, no private data on the overflow event */
201 priv = NULL;
202 }
a2d8bc6c
EP
203
204 spin_lock(&event->lock);
205
206 if (list_empty(&event->holder.event_list)) {
207 if (unlikely(holder))
208 fsnotify_destroy_event_holder(holder);
209 holder = &event->holder;
210 } else if (unlikely(!holder)) {
211 /* between the time we checked above and got the lock the in
212 * event holder was used, go back and get a new one */
213 spin_unlock(&event->lock);
214 mutex_unlock(&group->notification_mutex);
215 goto alloc_holder;
216 }
217
218 if (!list_empty(list)) {
219 last_holder = list_entry(list->prev, struct fsnotify_event_holder, event_list);
220 last_event = last_holder->event;
221 if (event_compare(last_event, event)) {
222 spin_unlock(&event->lock);
223 mutex_unlock(&group->notification_mutex);
224 if (holder != &event->holder)
225 fsnotify_destroy_event_holder(holder);
e4aff117 226 return -EEXIST;
a2d8bc6c
EP
227 }
228 }
229
230 group->q_len++;
231 holder->event = event;
232
233 fsnotify_get_event(event);
234 list_add_tail(&holder->event_list, list);
e4aff117
EP
235 if (priv)
236 list_add_tail(&priv->event_list, &event->private_data_list);
a2d8bc6c
EP
237 spin_unlock(&event->lock);
238 mutex_unlock(&group->notification_mutex);
239
240 wake_up(&group->notification_waitq);
eef3a116 241 return ret;
a2d8bc6c
EP
242}
243
244/*
245 * Remove and return the first event from the notification list. There is a
246 * reference held on this event since it was on the list. It is the responsibility
247 * of the caller to drop this reference.
248 */
249struct fsnotify_event *fsnotify_remove_notify_event(struct fsnotify_group *group)
90586523
EP
250{
251 struct fsnotify_event *event;
a2d8bc6c 252 struct fsnotify_event_holder *holder;
90586523 253
a2d8bc6c 254 BUG_ON(!mutex_is_locked(&group->notification_mutex));
90586523 255
a2d8bc6c
EP
256 holder = list_first_entry(&group->notification_list, struct fsnotify_event_holder, event_list);
257
258 event = holder->event;
259
260 spin_lock(&event->lock);
261 holder->event = NULL;
262 list_del_init(&holder->event_list);
263 spin_unlock(&event->lock);
264
265 /* event == holder means we are referenced through the in event holder */
266 if (holder != &event->holder)
267 fsnotify_destroy_event_holder(holder);
268
269 group->q_len--;
270
271 return event;
272}
273
274/*
275 * This will not remove the event, that must be done with fsnotify_remove_notify_event()
276 */
277struct fsnotify_event *fsnotify_peek_notify_event(struct fsnotify_group *group)
278{
279 struct fsnotify_event *event;
280 struct fsnotify_event_holder *holder;
281
282 BUG_ON(!mutex_is_locked(&group->notification_mutex));
283
284 holder = list_first_entry(&group->notification_list, struct fsnotify_event_holder, event_list);
285 event = holder->event;
286
287 return event;
288}
289
290/*
291 * Called when a group is being torn down to clean up any outstanding
292 * event notifications.
293 */
294void fsnotify_flush_notify(struct fsnotify_group *group)
295{
296 struct fsnotify_event *event;
e4aff117 297 struct fsnotify_event_private_data *priv;
a2d8bc6c
EP
298
299 mutex_lock(&group->notification_mutex);
300 while (!fsnotify_notify_queue_is_empty(group)) {
301 event = fsnotify_remove_notify_event(group);
e4aff117
EP
302 /* if they don't implement free_event_priv they better not have attached any */
303 if (group->ops->free_event_priv) {
304 spin_lock(&event->lock);
305 priv = fsnotify_remove_priv_from_event(group, event);
306 spin_unlock(&event->lock);
307 if (priv)
308 group->ops->free_event_priv(priv);
309 }
a2d8bc6c
EP
310 fsnotify_put_event(event); /* matches fsnotify_add_notify_event */
311 }
312 mutex_unlock(&group->notification_mutex);
313}
314
315static void initialize_event(struct fsnotify_event *event)
316{
a2d8bc6c 317 INIT_LIST_HEAD(&event->holder.event_list);
90586523
EP
318 atomic_set(&event->refcnt, 1);
319
320 spin_lock_init(&event->lock);
321
a2d8bc6c 322 event->data_type = FSNOTIFY_EVENT_NONE;
90586523 323
e4aff117 324 INIT_LIST_HEAD(&event->private_data_list);
a2d8bc6c
EP
325}
326
327/*
328 * fsnotify_create_event - Allocate a new event which will be sent to each
329 * group's handle_event function if the group was interested in this
330 * particular event.
331 *
332 * @to_tell the inode which is supposed to receive the event (sometimes a
333 * parent of the inode to which the event happened.
334 * @mask what actually happened.
335 * @data pointer to the object which was actually affected
336 * @data_type flag indication if the data is a file, path, inode, nothing...
62ffe5df 337 * @name the filename, if available
a2d8bc6c 338 */
47882c6f 339struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u32 mask, void *data,
f44aebcc
EP
340 int data_type, const char *name, u32 cookie,
341 gfp_t gfp)
a2d8bc6c
EP
342{
343 struct fsnotify_event *event;
344
6f3a539e 345 event = kmem_cache_zalloc(fsnotify_event_cachep, gfp);
a2d8bc6c
EP
346 if (!event)
347 return NULL;
348
349 initialize_event(event);
62ffe5df
EP
350
351 if (name) {
f44aebcc 352 event->file_name = kstrdup(name, gfp);
62ffe5df
EP
353 if (!event->file_name) {
354 kmem_cache_free(fsnotify_event_cachep, event);
355 return NULL;
356 }
357 event->name_len = strlen(event->file_name);
358 }
47882c6f
EP
359
360 event->sync_cookie = cookie;
90586523
EP
361 event->to_tell = to_tell;
362
363 switch (data_type) {
364 case FSNOTIFY_EVENT_FILE: {
365 struct file *file = data;
366 struct path *path = &file->f_path;
367 event->path.dentry = path->dentry;
368 event->path.mnt = path->mnt;
369 path_get(&event->path);
370 event->data_type = FSNOTIFY_EVENT_PATH;
371 break;
372 }
373 case FSNOTIFY_EVENT_PATH: {
374 struct path *path = data;
375 event->path.dentry = path->dentry;
376 event->path.mnt = path->mnt;
377 path_get(&event->path);
378 event->data_type = FSNOTIFY_EVENT_PATH;
379 break;
380 }
381 case FSNOTIFY_EVENT_INODE:
382 event->inode = data;
383 event->data_type = FSNOTIFY_EVENT_INODE;
384 break;
385 case FSNOTIFY_EVENT_NONE:
386 event->inode = NULL;
387 event->path.dentry = NULL;
388 event->path.mnt = NULL;
389 break;
390 default:
391 BUG();
392 }
393
394 event->mask = mask;
395
396 return event;
397}
398
399__init int fsnotify_notification_init(void)
400{
401 fsnotify_event_cachep = KMEM_CACHE(fsnotify_event, SLAB_PANIC);
a2d8bc6c
EP
402 fsnotify_event_holder_cachep = KMEM_CACHE(fsnotify_event_holder, SLAB_PANIC);
403
b4277d3d
EP
404 q_overflow_event = fsnotify_create_event(NULL, FS_Q_OVERFLOW, NULL,
405 FSNOTIFY_EVENT_NONE, NULL, 0,
406 GFP_KERNEL);
407 if (!q_overflow_event)
408 panic("unable to allocate fsnotify q_overflow_event\n");
90586523
EP
409
410 return 0;
411}
412subsys_initcall(fsnotify_notification_init);
413