blob: 69391fe8efb1bac7e8e450b2bfa2f1f7fe13a505 [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 */
59static 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);
Eric Paris90586522009-05-21 17:01:20 -040096 kmem_cache_free(fsnotify_event_cachep, event);
97 }
98}
99
Eric Parisa2d8bc62009-05-21 17:01:37 -0400100struct 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
Eric Paris90586522009-05-21 17:01:20 -0400110/*
Eric Parise4aff112009-05-21 17:01:50 -0400111 * 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.
Eric Parisa2d8bc62009-05-21 17:01:37 -0400134 */
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) &&
Eric Paris4a148ba2009-07-13 15:56:55 -0400139 (old->data_type == new->data_type) &&
140 (old->name_len == new->name_len)) {
Eric Parisa2d8bc62009-05-21 17:01:37 -0400141 switch (old->data_type) {
142 case (FSNOTIFY_EVENT_INODE):
Eric Paris4a148ba2009-07-13 15:56:55 -0400143 /* 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 */
146 if (old->name_len &&
147 !strcmp(old->file_name, new->file_name))
Eric Parisa2d8bc62009-05-21 17:01:37 -0400148 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;
154 case (FSNOTIFY_EVENT_NONE):
155 return true;
156 };
157 }
158 return false;
159}
160
161/*
162 * Add an event to the group notification queue. The group can later pull this
163 * event off the queue to deal with. If the event is successfully added to the
164 * group's notification queue, a reference is taken on event.
165 */
Eric Parise4aff112009-05-21 17:01:50 -0400166int fsnotify_add_notify_event(struct fsnotify_group *group, struct fsnotify_event *event,
167 struct fsnotify_event_private_data *priv)
Eric Parisa2d8bc62009-05-21 17:01:37 -0400168{
169 struct fsnotify_event_holder *holder = NULL;
170 struct list_head *list = &group->notification_list;
171 struct fsnotify_event_holder *last_holder;
172 struct fsnotify_event *last_event;
173
Eric Parise4aff112009-05-21 17:01:50 -0400174 /* easy to tell if priv was attached to the event */
175 INIT_LIST_HEAD(&priv->event_list);
176
Eric Parisa2d8bc62009-05-21 17:01:37 -0400177 /*
178 * There is one fsnotify_event_holder embedded inside each fsnotify_event.
179 * Check if we expect to be able to use that holder. If not alloc a new
180 * holder.
181 * For the overflow event it's possible that something will use the in
182 * event holder before we get the lock so we may need to jump back and
183 * alloc a new holder, this can't happen for most events...
184 */
185 if (!list_empty(&event->holder.event_list)) {
186alloc_holder:
187 holder = fsnotify_alloc_event_holder();
188 if (!holder)
189 return -ENOMEM;
190 }
191
192 mutex_lock(&group->notification_mutex);
193
Eric Parise4aff112009-05-21 17:01:50 -0400194 if (group->q_len >= group->max_events) {
Eric Parisa2d8bc62009-05-21 17:01:37 -0400195 event = &q_overflow_event;
Eric Parise4aff112009-05-21 17:01:50 -0400196 /* sorry, no private data on the overflow event */
197 priv = NULL;
198 }
Eric Parisa2d8bc62009-05-21 17:01:37 -0400199
200 spin_lock(&event->lock);
201
202 if (list_empty(&event->holder.event_list)) {
203 if (unlikely(holder))
204 fsnotify_destroy_event_holder(holder);
205 holder = &event->holder;
206 } else if (unlikely(!holder)) {
207 /* between the time we checked above and got the lock the in
208 * event holder was used, go back and get a new one */
209 spin_unlock(&event->lock);
210 mutex_unlock(&group->notification_mutex);
211 goto alloc_holder;
212 }
213
214 if (!list_empty(list)) {
215 last_holder = list_entry(list->prev, struct fsnotify_event_holder, event_list);
216 last_event = last_holder->event;
217 if (event_compare(last_event, event)) {
218 spin_unlock(&event->lock);
219 mutex_unlock(&group->notification_mutex);
220 if (holder != &event->holder)
221 fsnotify_destroy_event_holder(holder);
Eric Parise4aff112009-05-21 17:01:50 -0400222 return -EEXIST;
Eric Parisa2d8bc62009-05-21 17:01:37 -0400223 }
224 }
225
226 group->q_len++;
227 holder->event = event;
228
229 fsnotify_get_event(event);
230 list_add_tail(&holder->event_list, list);
Eric Parise4aff112009-05-21 17:01:50 -0400231 if (priv)
232 list_add_tail(&priv->event_list, &event->private_data_list);
Eric Parisa2d8bc62009-05-21 17:01:37 -0400233 spin_unlock(&event->lock);
234 mutex_unlock(&group->notification_mutex);
235
236 wake_up(&group->notification_waitq);
237 return 0;
238}
239
240/*
241 * Remove and return the first event from the notification list. There is a
242 * reference held on this event since it was on the list. It is the responsibility
243 * of the caller to drop this reference.
244 */
245struct fsnotify_event *fsnotify_remove_notify_event(struct fsnotify_group *group)
246{
247 struct fsnotify_event *event;
248 struct fsnotify_event_holder *holder;
249
250 BUG_ON(!mutex_is_locked(&group->notification_mutex));
251
252 holder = list_first_entry(&group->notification_list, struct fsnotify_event_holder, event_list);
253
254 event = holder->event;
255
256 spin_lock(&event->lock);
257 holder->event = NULL;
258 list_del_init(&holder->event_list);
259 spin_unlock(&event->lock);
260
261 /* event == holder means we are referenced through the in event holder */
262 if (holder != &event->holder)
263 fsnotify_destroy_event_holder(holder);
264
265 group->q_len--;
266
267 return event;
268}
269
270/*
271 * This will not remove the event, that must be done with fsnotify_remove_notify_event()
272 */
273struct fsnotify_event *fsnotify_peek_notify_event(struct fsnotify_group *group)
274{
275 struct fsnotify_event *event;
276 struct fsnotify_event_holder *holder;
277
278 BUG_ON(!mutex_is_locked(&group->notification_mutex));
279
280 holder = list_first_entry(&group->notification_list, struct fsnotify_event_holder, event_list);
281 event = holder->event;
282
283 return event;
284}
285
286/*
287 * Called when a group is being torn down to clean up any outstanding
288 * event notifications.
289 */
290void fsnotify_flush_notify(struct fsnotify_group *group)
291{
292 struct fsnotify_event *event;
Eric Parise4aff112009-05-21 17:01:50 -0400293 struct fsnotify_event_private_data *priv;
Eric Parisa2d8bc62009-05-21 17:01:37 -0400294
295 mutex_lock(&group->notification_mutex);
296 while (!fsnotify_notify_queue_is_empty(group)) {
297 event = fsnotify_remove_notify_event(group);
Eric Parise4aff112009-05-21 17:01:50 -0400298 /* if they don't implement free_event_priv they better not have attached any */
299 if (group->ops->free_event_priv) {
300 spin_lock(&event->lock);
301 priv = fsnotify_remove_priv_from_event(group, event);
302 spin_unlock(&event->lock);
303 if (priv)
304 group->ops->free_event_priv(priv);
305 }
Eric Parisa2d8bc62009-05-21 17:01:37 -0400306 fsnotify_put_event(event); /* matches fsnotify_add_notify_event */
307 }
308 mutex_unlock(&group->notification_mutex);
309}
310
311static void initialize_event(struct fsnotify_event *event)
312{
313 event->holder.event = NULL;
314 INIT_LIST_HEAD(&event->holder.event_list);
315 atomic_set(&event->refcnt, 1);
316
317 spin_lock_init(&event->lock);
318
319 event->path.dentry = NULL;
320 event->path.mnt = NULL;
321 event->inode = NULL;
322 event->data_type = FSNOTIFY_EVENT_NONE;
323
Eric Parise4aff112009-05-21 17:01:50 -0400324 INIT_LIST_HEAD(&event->private_data_list);
325
Eric Parisa2d8bc62009-05-21 17:01:37 -0400326 event->to_tell = NULL;
Eric Paris62ffe5d2009-05-21 17:01:43 -0400327
328 event->file_name = NULL;
329 event->name_len = 0;
Eric Paris47882c62009-05-21 17:01:47 -0400330
331 event->sync_cookie = 0;
Eric Parisa2d8bc62009-05-21 17:01:37 -0400332}
333
334/*
335 * fsnotify_create_event - Allocate a new event which will be sent to each
336 * group's handle_event function if the group was interested in this
337 * particular event.
338 *
339 * @to_tell the inode which is supposed to receive the event (sometimes a
340 * parent of the inode to which the event happened.
341 * @mask what actually happened.
342 * @data pointer to the object which was actually affected
343 * @data_type flag indication if the data is a file, path, inode, nothing...
Eric Paris62ffe5d2009-05-21 17:01:43 -0400344 * @name the filename, if available
Eric Paris90586522009-05-21 17:01:20 -0400345 */
Eric Paris47882c62009-05-21 17:01:47 -0400346struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u32 mask, void *data,
347 int data_type, const char *name, u32 cookie)
Eric Paris90586522009-05-21 17:01:20 -0400348{
349 struct fsnotify_event *event;
350
351 event = kmem_cache_alloc(fsnotify_event_cachep, GFP_KERNEL);
352 if (!event)
353 return NULL;
354
Eric Parisa2d8bc62009-05-21 17:01:37 -0400355 initialize_event(event);
Eric Paris62ffe5d2009-05-21 17:01:43 -0400356
357 if (name) {
358 event->file_name = kstrdup(name, GFP_KERNEL);
359 if (!event->file_name) {
360 kmem_cache_free(fsnotify_event_cachep, event);
361 return NULL;
362 }
363 event->name_len = strlen(event->file_name);
364 }
Eric Paris47882c62009-05-21 17:01:47 -0400365
366 event->sync_cookie = cookie;
Eric Paris90586522009-05-21 17:01:20 -0400367 event->to_tell = to_tell;
368
369 switch (data_type) {
370 case FSNOTIFY_EVENT_FILE: {
371 struct file *file = data;
372 struct path *path = &file->f_path;
373 event->path.dentry = path->dentry;
374 event->path.mnt = path->mnt;
375 path_get(&event->path);
376 event->data_type = FSNOTIFY_EVENT_PATH;
377 break;
378 }
379 case FSNOTIFY_EVENT_PATH: {
380 struct path *path = data;
381 event->path.dentry = path->dentry;
382 event->path.mnt = path->mnt;
383 path_get(&event->path);
384 event->data_type = FSNOTIFY_EVENT_PATH;
385 break;
386 }
387 case FSNOTIFY_EVENT_INODE:
388 event->inode = data;
389 event->data_type = FSNOTIFY_EVENT_INODE;
390 break;
391 case FSNOTIFY_EVENT_NONE:
392 event->inode = NULL;
393 event->path.dentry = NULL;
394 event->path.mnt = NULL;
395 break;
396 default:
397 BUG();
398 }
399
400 event->mask = mask;
401
402 return event;
403}
404
405__init int fsnotify_notification_init(void)
406{
407 fsnotify_event_cachep = KMEM_CACHE(fsnotify_event, SLAB_PANIC);
Eric Parisa2d8bc62009-05-21 17:01:37 -0400408 fsnotify_event_holder_cachep = KMEM_CACHE(fsnotify_event_holder, SLAB_PANIC);
409
410 initialize_event(&q_overflow_event);
411 q_overflow_event.mask = FS_Q_OVERFLOW;
Eric Paris90586522009-05-21 17:01:20 -0400412
413 return 0;
414}
415subsys_initcall(fsnotify_notification_init);
416