blob: 6dc96b35e4a75ea993171aba683beb463daed62b [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);
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{
Eric Paris74766bb2009-12-17 21:24:21 -0500107 if (holder)
108 kmem_cache_free(fsnotify_event_holder_cachep, holder);
Eric Parisa2d8bc62009-05-21 17:01:37 -0400109}
110
Eric Paris90586522009-05-21 17:01:20 -0400111/*
Eric Parise4aff112009-05-21 17:01:50 -0400112 * Find the private data that the group previously attached to this event when
113 * the group added the event to the notification queue (fsnotify_add_notify_event)
114 */
115struct fsnotify_event_private_data *fsnotify_remove_priv_from_event(struct fsnotify_group *group, struct fsnotify_event *event)
116{
117 struct fsnotify_event_private_data *lpriv;
118 struct fsnotify_event_private_data *priv = NULL;
119
120 assert_spin_locked(&event->lock);
121
122 list_for_each_entry(lpriv, &event->private_data_list, event_list) {
123 if (lpriv->group == group) {
124 priv = lpriv;
125 list_del(&priv->event_list);
126 break;
127 }
128 }
129 return priv;
130}
131
132/*
Eric Parisa2d8bc62009-05-21 17:01:37 -0400133 * Add an event to the group notification queue. The group can later pull this
134 * event off the queue to deal with. If the event is successfully added to the
135 * group's notification queue, a reference is taken on event.
136 */
Eric Parise4aff112009-05-21 17:01:50 -0400137int fsnotify_add_notify_event(struct fsnotify_group *group, struct fsnotify_event *event,
Eric Paris74766bb2009-12-17 21:24:21 -0500138 struct fsnotify_event_private_data *priv,
139 int (*merge)(struct list_head *, struct fsnotify_event *))
Eric Parisa2d8bc62009-05-21 17:01:37 -0400140{
141 struct fsnotify_event_holder *holder = NULL;
142 struct list_head *list = &group->notification_list;
Eric Paris74766bb2009-12-17 21:24:21 -0500143 int rc = 0;
Eric Parise4aff112009-05-21 17:01:50 -0400144
Eric Parisa2d8bc62009-05-21 17:01:37 -0400145 /*
146 * There is one fsnotify_event_holder embedded inside each fsnotify_event.
147 * Check if we expect to be able to use that holder. If not alloc a new
148 * holder.
149 * For the overflow event it's possible that something will use the in
150 * event holder before we get the lock so we may need to jump back and
151 * alloc a new holder, this can't happen for most events...
152 */
153 if (!list_empty(&event->holder.event_list)) {
154alloc_holder:
155 holder = fsnotify_alloc_event_holder();
156 if (!holder)
157 return -ENOMEM;
158 }
159
160 mutex_lock(&group->notification_mutex);
161
Eric Parise4aff112009-05-21 17:01:50 -0400162 if (group->q_len >= group->max_events) {
Eric Parisb4277d32009-12-17 20:12:06 -0500163 event = q_overflow_event;
Eric Paris74766bb2009-12-17 21:24:21 -0500164 rc = -EOVERFLOW;
Eric Parise4aff112009-05-21 17:01:50 -0400165 /* sorry, no private data on the overflow event */
166 priv = NULL;
167 }
Eric Parisa2d8bc62009-05-21 17:01:37 -0400168
Eric Paris74766bb2009-12-17 21:24:21 -0500169 if (!list_empty(list) && merge) {
170 int ret;
171
172 ret = merge(list, event);
173 if (ret) {
174 mutex_unlock(&group->notification_mutex);
175 if (holder != &event->holder)
176 fsnotify_destroy_event_holder(holder);
177 return ret;
178 }
179 }
180
Eric Parisa2d8bc62009-05-21 17:01:37 -0400181 spin_lock(&event->lock);
182
183 if (list_empty(&event->holder.event_list)) {
184 if (unlikely(holder))
185 fsnotify_destroy_event_holder(holder);
186 holder = &event->holder;
187 } else if (unlikely(!holder)) {
188 /* between the time we checked above and got the lock the in
189 * event holder was used, go back and get a new one */
190 spin_unlock(&event->lock);
191 mutex_unlock(&group->notification_mutex);
192 goto alloc_holder;
193 }
194
Eric Parisa2d8bc62009-05-21 17:01:37 -0400195 group->q_len++;
196 holder->event = event;
197
198 fsnotify_get_event(event);
199 list_add_tail(&holder->event_list, list);
Eric Parise4aff112009-05-21 17:01:50 -0400200 if (priv)
201 list_add_tail(&priv->event_list, &event->private_data_list);
Eric Parisa2d8bc62009-05-21 17:01:37 -0400202 spin_unlock(&event->lock);
203 mutex_unlock(&group->notification_mutex);
204
205 wake_up(&group->notification_waitq);
Eric Paris74766bb2009-12-17 21:24:21 -0500206 return rc;
Eric Parisa2d8bc62009-05-21 17:01:37 -0400207}
208
209/*
210 * Remove and return the first event from the notification list. There is a
211 * reference held on this event since it was on the list. It is the responsibility
212 * of the caller to drop this reference.
213 */
214struct fsnotify_event *fsnotify_remove_notify_event(struct fsnotify_group *group)
215{
216 struct fsnotify_event *event;
217 struct fsnotify_event_holder *holder;
218
219 BUG_ON(!mutex_is_locked(&group->notification_mutex));
220
221 holder = list_first_entry(&group->notification_list, struct fsnotify_event_holder, event_list);
222
223 event = holder->event;
224
225 spin_lock(&event->lock);
226 holder->event = NULL;
227 list_del_init(&holder->event_list);
228 spin_unlock(&event->lock);
229
230 /* event == holder means we are referenced through the in event holder */
231 if (holder != &event->holder)
232 fsnotify_destroy_event_holder(holder);
233
234 group->q_len--;
235
236 return event;
237}
238
239/*
240 * This will not remove the event, that must be done with fsnotify_remove_notify_event()
241 */
242struct fsnotify_event *fsnotify_peek_notify_event(struct fsnotify_group *group)
243{
244 struct fsnotify_event *event;
245 struct fsnotify_event_holder *holder;
246
247 BUG_ON(!mutex_is_locked(&group->notification_mutex));
248
249 holder = list_first_entry(&group->notification_list, struct fsnotify_event_holder, event_list);
250 event = holder->event;
251
252 return event;
253}
254
255/*
256 * Called when a group is being torn down to clean up any outstanding
257 * event notifications.
258 */
259void fsnotify_flush_notify(struct fsnotify_group *group)
260{
261 struct fsnotify_event *event;
Eric Parise4aff112009-05-21 17:01:50 -0400262 struct fsnotify_event_private_data *priv;
Eric Parisa2d8bc62009-05-21 17:01:37 -0400263
264 mutex_lock(&group->notification_mutex);
265 while (!fsnotify_notify_queue_is_empty(group)) {
266 event = fsnotify_remove_notify_event(group);
Eric Parise4aff112009-05-21 17:01:50 -0400267 /* if they don't implement free_event_priv they better not have attached any */
268 if (group->ops->free_event_priv) {
269 spin_lock(&event->lock);
270 priv = fsnotify_remove_priv_from_event(group, event);
271 spin_unlock(&event->lock);
272 if (priv)
273 group->ops->free_event_priv(priv);
274 }
Eric Parisa2d8bc62009-05-21 17:01:37 -0400275 fsnotify_put_event(event); /* matches fsnotify_add_notify_event */
276 }
277 mutex_unlock(&group->notification_mutex);
278}
279
280static void initialize_event(struct fsnotify_event *event)
281{
Eric Parisa2d8bc62009-05-21 17:01:37 -0400282 INIT_LIST_HEAD(&event->holder.event_list);
283 atomic_set(&event->refcnt, 1);
284
285 spin_lock_init(&event->lock);
286
Eric Parisa2d8bc62009-05-21 17:01:37 -0400287 event->data_type = FSNOTIFY_EVENT_NONE;
288
Eric Parise4aff112009-05-21 17:01:50 -0400289 INIT_LIST_HEAD(&event->private_data_list);
Eric Parisa2d8bc62009-05-21 17:01:37 -0400290}
291
292/*
293 * fsnotify_create_event - Allocate a new event which will be sent to each
294 * group's handle_event function if the group was interested in this
295 * particular event.
296 *
297 * @to_tell the inode which is supposed to receive the event (sometimes a
298 * parent of the inode to which the event happened.
299 * @mask what actually happened.
300 * @data pointer to the object which was actually affected
301 * @data_type flag indication if the data is a file, path, inode, nothing...
Eric Paris62ffe5d2009-05-21 17:01:43 -0400302 * @name the filename, if available
Eric Paris90586522009-05-21 17:01:20 -0400303 */
Eric Paris47882c62009-05-21 17:01:47 -0400304struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u32 mask, void *data,
Eric Parisf44aebc2009-07-15 15:49:52 -0400305 int data_type, const char *name, u32 cookie,
306 gfp_t gfp)
Eric Paris90586522009-05-21 17:01:20 -0400307{
308 struct fsnotify_event *event;
309
Eric Paris6f3a5392009-12-17 20:12:07 -0500310 event = kmem_cache_zalloc(fsnotify_event_cachep, gfp);
Eric Paris90586522009-05-21 17:01:20 -0400311 if (!event)
312 return NULL;
313
Eric Parisa2d8bc62009-05-21 17:01:37 -0400314 initialize_event(event);
Eric Paris62ffe5d2009-05-21 17:01:43 -0400315
316 if (name) {
Eric Parisf44aebc2009-07-15 15:49:52 -0400317 event->file_name = kstrdup(name, gfp);
Eric Paris62ffe5d2009-05-21 17:01:43 -0400318 if (!event->file_name) {
319 kmem_cache_free(fsnotify_event_cachep, event);
320 return NULL;
321 }
322 event->name_len = strlen(event->file_name);
323 }
Eric Paris47882c62009-05-21 17:01:47 -0400324
325 event->sync_cookie = cookie;
Eric Paris90586522009-05-21 17:01:20 -0400326 event->to_tell = to_tell;
327
328 switch (data_type) {
329 case FSNOTIFY_EVENT_FILE: {
330 struct file *file = data;
331 struct path *path = &file->f_path;
332 event->path.dentry = path->dentry;
333 event->path.mnt = path->mnt;
334 path_get(&event->path);
335 event->data_type = FSNOTIFY_EVENT_PATH;
336 break;
337 }
338 case FSNOTIFY_EVENT_PATH: {
339 struct path *path = data;
340 event->path.dentry = path->dentry;
341 event->path.mnt = path->mnt;
342 path_get(&event->path);
343 event->data_type = FSNOTIFY_EVENT_PATH;
344 break;
345 }
346 case FSNOTIFY_EVENT_INODE:
347 event->inode = data;
348 event->data_type = FSNOTIFY_EVENT_INODE;
349 break;
350 case FSNOTIFY_EVENT_NONE:
351 event->inode = NULL;
352 event->path.dentry = NULL;
353 event->path.mnt = NULL;
354 break;
355 default:
356 BUG();
357 }
358
359 event->mask = mask;
360
361 return event;
362}
363
364__init int fsnotify_notification_init(void)
365{
366 fsnotify_event_cachep = KMEM_CACHE(fsnotify_event, SLAB_PANIC);
Eric Parisa2d8bc62009-05-21 17:01:37 -0400367 fsnotify_event_holder_cachep = KMEM_CACHE(fsnotify_event_holder, SLAB_PANIC);
368
Eric Parisb4277d32009-12-17 20:12:06 -0500369 q_overflow_event = fsnotify_create_event(NULL, FS_Q_OVERFLOW, NULL,
370 FSNOTIFY_EVENT_NONE, NULL, 0,
371 GFP_KERNEL);
372 if (!q_overflow_event)
373 panic("unable to allocate fsnotify q_overflow_event\n");
Eric Paris90586522009-05-21 17:01:20 -0400374
375 return 0;
376}
377subsys_initcall(fsnotify_notification_init);
378