blob: 7fc8d004084c3a4280591d9c2a76d7f79b9adadf [file] [log] [blame]
Eric Paris90586522009-05-21 17:01:20 -04001/*
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
Eric Parisa2d8bc62009-05-21 17:01:37 -040019/*
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
Eric Paris90586522009-05-21 17:01:20 -040034#include <linux/fs.h>
35#include <linux/init.h>
36#include <linux/kernel.h>
37#include <linux/list.h>
Eric Paris47882c62009-05-21 17:01:47 -040038#include <linux/module.h>
Eric Paris90586522009-05-21 17:01:20 -040039#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;
Eric Parisa2d8bc62009-05-21 17:01:37 -040052static 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 */
Eric Parisb4277d32009-12-17 20:12:06 -050059static struct fsnotify_event *q_overflow_event;
Eric Paris47882c62009-05-21 17:01:47 -040060static 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);
Eric Parisa2d8bc62009-05-21 17:01:37 -040071
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}
Eric Paris90586522009-05-21 17:01:20 -040078
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
Eric Parise4aff112009-05-21 17:01:50 -040093 BUG_ON(!list_empty(&event->private_data_list));
94
Eric Paris62ffe5d2009-05-21 17:01:43 -040095 kfree(event->file_name);
Andreas Gruenbacher32c32632009-12-17 21:24:27 -050096 put_pid(event->tgid);
Eric Paris90586522009-05-21 17:01:20 -040097 kmem_cache_free(fsnotify_event_cachep, event);
98 }
99}
100
Eric Parisa2d8bc62009-05-21 17:01:37 -0400101struct 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{
Eric Paris74766bb2009-12-17 21:24:21 -0500108 if (holder)
109 kmem_cache_free(fsnotify_event_holder_cachep, holder);
Eric Parisa2d8bc62009-05-21 17:01:37 -0400110}
111
Eric Paris90586522009-05-21 17:01:20 -0400112/*
Eric Parise4aff112009-05-21 17:01:50 -0400113 * 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
133/*
Eric Parisa2d8bc62009-05-21 17:01:37 -0400134 * 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.
137 */
Eric Parise4aff112009-05-21 17:01:50 -0400138int fsnotify_add_notify_event(struct fsnotify_group *group, struct fsnotify_event *event,
Eric Paris74766bb2009-12-17 21:24:21 -0500139 struct fsnotify_event_private_data *priv,
140 int (*merge)(struct list_head *, struct fsnotify_event *))
Eric Parisa2d8bc62009-05-21 17:01:37 -0400141{
142 struct fsnotify_event_holder *holder = NULL;
143 struct list_head *list = &group->notification_list;
Eric Paris74766bb2009-12-17 21:24:21 -0500144 int rc = 0;
Eric Parise4aff112009-05-21 17:01:50 -0400145
Eric Parisa2d8bc62009-05-21 17:01:37 -0400146 /*
147 * There is one fsnotify_event_holder embedded inside each fsnotify_event.
148 * Check if we expect to be able to use that holder. If not alloc a new
149 * holder.
150 * For the overflow event it's possible that something will use the in
151 * event holder before we get the lock so we may need to jump back and
152 * alloc a new holder, this can't happen for most events...
153 */
154 if (!list_empty(&event->holder.event_list)) {
155alloc_holder:
156 holder = fsnotify_alloc_event_holder();
157 if (!holder)
158 return -ENOMEM;
159 }
160
161 mutex_lock(&group->notification_mutex);
162
Eric Parise4aff112009-05-21 17:01:50 -0400163 if (group->q_len >= group->max_events) {
Eric Parisb4277d32009-12-17 20:12:06 -0500164 event = q_overflow_event;
Eric Paris74766bb2009-12-17 21:24:21 -0500165 rc = -EOVERFLOW;
Eric Parise4aff112009-05-21 17:01:50 -0400166 /* sorry, no private data on the overflow event */
167 priv = NULL;
168 }
Eric Parisa2d8bc62009-05-21 17:01:37 -0400169
Eric Paris74766bb2009-12-17 21:24:21 -0500170 if (!list_empty(list) && merge) {
171 int ret;
172
173 ret = merge(list, event);
174 if (ret) {
175 mutex_unlock(&group->notification_mutex);
176 if (holder != &event->holder)
177 fsnotify_destroy_event_holder(holder);
178 return ret;
179 }
180 }
181
Eric Parisa2d8bc62009-05-21 17:01:37 -0400182 spin_lock(&event->lock);
183
184 if (list_empty(&event->holder.event_list)) {
185 if (unlikely(holder))
186 fsnotify_destroy_event_holder(holder);
187 holder = &event->holder;
188 } else if (unlikely(!holder)) {
189 /* between the time we checked above and got the lock the in
190 * event holder was used, go back and get a new one */
191 spin_unlock(&event->lock);
192 mutex_unlock(&group->notification_mutex);
193 goto alloc_holder;
194 }
195
Eric Parisa2d8bc62009-05-21 17:01:37 -0400196 group->q_len++;
197 holder->event = event;
198
199 fsnotify_get_event(event);
200 list_add_tail(&holder->event_list, list);
Eric Parise4aff112009-05-21 17:01:50 -0400201 if (priv)
202 list_add_tail(&priv->event_list, &event->private_data_list);
Eric Parisa2d8bc62009-05-21 17:01:37 -0400203 spin_unlock(&event->lock);
204 mutex_unlock(&group->notification_mutex);
205
206 wake_up(&group->notification_waitq);
Eric Paris74766bb2009-12-17 21:24:21 -0500207 return rc;
Eric Parisa2d8bc62009-05-21 17:01:37 -0400208}
209
210/*
211 * Remove and return the first event from the notification list. There is a
212 * reference held on this event since it was on the list. It is the responsibility
213 * of the caller to drop this reference.
214 */
215struct fsnotify_event *fsnotify_remove_notify_event(struct fsnotify_group *group)
216{
217 struct fsnotify_event *event;
218 struct fsnotify_event_holder *holder;
219
220 BUG_ON(!mutex_is_locked(&group->notification_mutex));
221
222 holder = list_first_entry(&group->notification_list, struct fsnotify_event_holder, event_list);
223
224 event = holder->event;
225
226 spin_lock(&event->lock);
227 holder->event = NULL;
228 list_del_init(&holder->event_list);
229 spin_unlock(&event->lock);
230
231 /* event == holder means we are referenced through the in event holder */
232 if (holder != &event->holder)
233 fsnotify_destroy_event_holder(holder);
234
235 group->q_len--;
236
237 return event;
238}
239
240/*
241 * This will not remove the event, that must be done with fsnotify_remove_notify_event()
242 */
243struct fsnotify_event *fsnotify_peek_notify_event(struct fsnotify_group *group)
244{
245 struct fsnotify_event *event;
246 struct fsnotify_event_holder *holder;
247
248 BUG_ON(!mutex_is_locked(&group->notification_mutex));
249
250 holder = list_first_entry(&group->notification_list, struct fsnotify_event_holder, event_list);
251 event = holder->event;
252
253 return event;
254}
255
256/*
257 * Called when a group is being torn down to clean up any outstanding
258 * event notifications.
259 */
260void fsnotify_flush_notify(struct fsnotify_group *group)
261{
262 struct fsnotify_event *event;
Eric Parise4aff112009-05-21 17:01:50 -0400263 struct fsnotify_event_private_data *priv;
Eric Parisa2d8bc62009-05-21 17:01:37 -0400264
265 mutex_lock(&group->notification_mutex);
266 while (!fsnotify_notify_queue_is_empty(group)) {
267 event = fsnotify_remove_notify_event(group);
Eric Parise4aff112009-05-21 17:01:50 -0400268 /* if they don't implement free_event_priv they better not have attached any */
269 if (group->ops->free_event_priv) {
270 spin_lock(&event->lock);
271 priv = fsnotify_remove_priv_from_event(group, event);
272 spin_unlock(&event->lock);
273 if (priv)
274 group->ops->free_event_priv(priv);
275 }
Eric Parisa2d8bc62009-05-21 17:01:37 -0400276 fsnotify_put_event(event); /* matches fsnotify_add_notify_event */
277 }
278 mutex_unlock(&group->notification_mutex);
279}
280
281static void initialize_event(struct fsnotify_event *event)
282{
Eric Parisa2d8bc62009-05-21 17:01:37 -0400283 INIT_LIST_HEAD(&event->holder.event_list);
284 atomic_set(&event->refcnt, 1);
285
286 spin_lock_init(&event->lock);
287
Eric Parise4aff112009-05-21 17:01:50 -0400288 INIT_LIST_HEAD(&event->private_data_list);
Eric Parisa2d8bc62009-05-21 17:01:37 -0400289}
290
Eric Paris1201a532009-12-17 21:24:22 -0500291/*
292 * Caller damn well better be holding whatever mutex is protecting the
Eric Pariscac69da2009-12-17 21:24:22 -0500293 * old_holder->event_list and the new_event must be a clean event which
294 * cannot be found anywhere else in the kernel.
Eric Paris1201a532009-12-17 21:24:22 -0500295 */
296int fsnotify_replace_event(struct fsnotify_event_holder *old_holder,
297 struct fsnotify_event *new_event)
298{
299 struct fsnotify_event *old_event = old_holder->event;
Eric Pariscac69da2009-12-17 21:24:22 -0500300 struct fsnotify_event_holder *new_holder = &new_event->holder;
301
302 enum event_spinlock_class {
303 SPINLOCK_OLD,
304 SPINLOCK_NEW,
305 };
Eric Paris1201a532009-12-17 21:24:22 -0500306
307 /*
Eric Pariscac69da2009-12-17 21:24:22 -0500308 * if the new_event's embedded holder is in use someone
309 * screwed up and didn't give us a clean new event.
Eric Paris1201a532009-12-17 21:24:22 -0500310 */
Eric Pariscac69da2009-12-17 21:24:22 -0500311 BUG_ON(!list_empty(&new_holder->event_list));
Eric Paris1201a532009-12-17 21:24:22 -0500312
Eric Pariscac69da2009-12-17 21:24:22 -0500313 spin_lock_nested(&old_event->lock, SPINLOCK_OLD);
314 spin_lock_nested(&new_event->lock, SPINLOCK_NEW);
Eric Paris1201a532009-12-17 21:24:22 -0500315
316 new_holder->event = new_event;
317 list_replace_init(&old_holder->event_list, &new_holder->event_list);
318
319 spin_unlock(&new_event->lock);
320 spin_unlock(&old_event->lock);
321
322 /* event == holder means we are referenced through the in event holder */
323 if (old_holder != &old_event->holder)
324 fsnotify_destroy_event_holder(old_holder);
325
326 fsnotify_get_event(new_event); /* on the list take reference */
327 fsnotify_put_event(old_event); /* off the list, drop reference */
328
329 return 0;
330}
331
Eric Parisb4e4e142009-12-17 21:24:21 -0500332struct fsnotify_event *fsnotify_clone_event(struct fsnotify_event *old_event)
333{
334 struct fsnotify_event *event;
335
336 event = kmem_cache_alloc(fsnotify_event_cachep, GFP_KERNEL);
337 if (!event)
338 return NULL;
339
340 memcpy(event, old_event, sizeof(*event));
341 initialize_event(event);
342
343 if (event->name_len) {
344 event->file_name = kstrdup(old_event->file_name, GFP_KERNEL);
345 if (!event->file_name) {
346 kmem_cache_free(fsnotify_event_cachep, event);
347 return NULL;
348 }
349 }
Andreas Gruenbacher32c32632009-12-17 21:24:27 -0500350 event->tgid = get_pid(old_event->tgid);
Eric Parisb4e4e142009-12-17 21:24:21 -0500351 if (event->data_type == FSNOTIFY_EVENT_PATH)
352 path_get(&event->path);
353
354 return event;
355}
356
Eric Parisa2d8bc62009-05-21 17:01:37 -0400357/*
358 * fsnotify_create_event - Allocate a new event which will be sent to each
359 * group's handle_event function if the group was interested in this
360 * particular event.
361 *
362 * @to_tell the inode which is supposed to receive the event (sometimes a
363 * parent of the inode to which the event happened.
364 * @mask what actually happened.
365 * @data pointer to the object which was actually affected
366 * @data_type flag indication if the data is a file, path, inode, nothing...
Eric Paris62ffe5d2009-05-21 17:01:43 -0400367 * @name the filename, if available
Eric Paris90586522009-05-21 17:01:20 -0400368 */
Eric Paris47882c62009-05-21 17:01:47 -0400369struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u32 mask, void *data,
Eric Parisf44aebc2009-07-15 15:49:52 -0400370 int data_type, const char *name, u32 cookie,
371 gfp_t gfp)
Eric Paris90586522009-05-21 17:01:20 -0400372{
373 struct fsnotify_event *event;
374
Eric Paris6f3a5392009-12-17 20:12:07 -0500375 event = kmem_cache_zalloc(fsnotify_event_cachep, gfp);
Eric Paris90586522009-05-21 17:01:20 -0400376 if (!event)
377 return NULL;
378
Eric Parisa2d8bc62009-05-21 17:01:37 -0400379 initialize_event(event);
Eric Paris62ffe5d2009-05-21 17:01:43 -0400380
381 if (name) {
Eric Parisf44aebc2009-07-15 15:49:52 -0400382 event->file_name = kstrdup(name, gfp);
Eric Paris62ffe5d2009-05-21 17:01:43 -0400383 if (!event->file_name) {
384 kmem_cache_free(fsnotify_event_cachep, event);
385 return NULL;
386 }
387 event->name_len = strlen(event->file_name);
388 }
Eric Paris47882c62009-05-21 17:01:47 -0400389
Andreas Gruenbacher32c32632009-12-17 21:24:27 -0500390 event->tgid = get_pid(task_tgid(current));
Eric Paris47882c62009-05-21 17:01:47 -0400391 event->sync_cookie = cookie;
Eric Paris90586522009-05-21 17:01:20 -0400392 event->to_tell = to_tell;
Eric Parisb4e4e142009-12-17 21:24:21 -0500393 event->data_type = data_type;
Eric Paris90586522009-05-21 17:01:20 -0400394
395 switch (data_type) {
Eric Paris90586522009-05-21 17:01:20 -0400396 case FSNOTIFY_EVENT_PATH: {
397 struct path *path = data;
398 event->path.dentry = path->dentry;
399 event->path.mnt = path->mnt;
400 path_get(&event->path);
Eric Paris90586522009-05-21 17:01:20 -0400401 break;
402 }
403 case FSNOTIFY_EVENT_INODE:
404 event->inode = data;
Eric Paris90586522009-05-21 17:01:20 -0400405 break;
406 case FSNOTIFY_EVENT_NONE:
407 event->inode = NULL;
408 event->path.dentry = NULL;
409 event->path.mnt = NULL;
410 break;
411 default:
412 BUG();
413 }
414
415 event->mask = mask;
416
417 return event;
418}
419
420__init int fsnotify_notification_init(void)
421{
422 fsnotify_event_cachep = KMEM_CACHE(fsnotify_event, SLAB_PANIC);
Eric Parisa2d8bc62009-05-21 17:01:37 -0400423 fsnotify_event_holder_cachep = KMEM_CACHE(fsnotify_event_holder, SLAB_PANIC);
424
Eric Parisb4277d32009-12-17 20:12:06 -0500425 q_overflow_event = fsnotify_create_event(NULL, FS_Q_OVERFLOW, NULL,
426 FSNOTIFY_EVENT_NONE, NULL, 0,
427 GFP_KERNEL);
428 if (!q_overflow_event)
429 panic("unable to allocate fsnotify q_overflow_event\n");
Eric Paris90586522009-05-21 17:01:20 -0400430
431 return 0;
432}
433subsys_initcall(fsnotify_notification_init);
434