blob: 9eddafa4c7ba878c05f7cd42d6bd769d3b81bf0f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Directory notifications for Linux.
3 *
4 * Copyright (C) 2000,2001,2002 Stephen Rothwell
5 *
Eric Paris3c5119c2009-05-21 17:01:33 -04006 * Copyright (C) 2009 Eric Paris <Red Hat Inc>
7 * dnotify was largly rewritten to use the new fsnotify infrastructure
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2, or (at your option) any
12 * later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 */
19#include <linux/fs.h>
20#include <linux/module.h>
21#include <linux/sched.h>
22#include <linux/dnotify.h>
23#include <linux/init.h>
24#include <linux/spinlock.h>
25#include <linux/slab.h>
Al Viro9f3acc32008-04-24 07:44:08 -040026#include <linux/fdtable.h>
Eric Paris3c5119c2009-05-21 17:01:33 -040027#include <linux/fsnotify_backend.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Eric Dumazetfa3536c2006-03-26 01:37:24 -080029int dir_notify_enable __read_mostly = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Eric Paris3c5119c2009-05-21 17:01:33 -040031static struct kmem_cache *dnotify_struct_cache __read_mostly;
32static struct kmem_cache *dnotify_mark_entry_cache __read_mostly;
33static struct fsnotify_group *dnotify_group __read_mostly;
34static DEFINE_MUTEX(dnotify_mark_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Eric Paris3c5119c2009-05-21 17:01:33 -040036/*
37 * dnotify will attach one of these to each inode (i_fsnotify_mark_entries) which
38 * is being watched by dnotify. If multiple userspace applications are watching
39 * the same directory with dnotify their information is chained in dn
40 */
41struct dnotify_mark_entry {
42 struct fsnotify_mark_entry fsn_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 struct dnotify_struct *dn;
Eric Paris3c5119c2009-05-21 17:01:33 -040044};
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Eric Paris3c5119c2009-05-21 17:01:33 -040046/*
47 * When a process starts or stops watching an inode the set of events which
48 * dnotify cares about for that inode may change. This function runs the
49 * list of everything receiving dnotify events about this directory and calculates
50 * the set of all those events. After it updates what dnotify is interested in
51 * it calls the fsnotify function so it can update the set of all events relevant
52 * to this inode.
53 */
54static void dnotify_recalc_inode_mask(struct fsnotify_mark_entry *entry)
55{
56 __u32 new_mask, old_mask;
57 struct dnotify_struct *dn;
58 struct dnotify_mark_entry *dnentry = container_of(entry,
59 struct dnotify_mark_entry,
60 fsn_entry);
61
62 assert_spin_locked(&entry->lock);
63
64 old_mask = entry->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 new_mask = 0;
Eric Paris3c5119c2009-05-21 17:01:33 -040066 for (dn = dnentry->dn; dn != NULL; dn = dn->dn_next)
67 new_mask |= (dn->dn_mask & ~FS_DN_MULTISHOT);
68 entry->mask = new_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Eric Paris3c5119c2009-05-21 17:01:33 -040070 if (old_mask == new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 return;
Eric Paris3c5119c2009-05-21 17:01:33 -040072
73 if (entry->inode)
74 fsnotify_recalc_inode_mask(entry->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
Eric Paris3c5119c2009-05-21 17:01:33 -040077/*
78 * Mains fsnotify call where events are delivered to dnotify.
79 * Find the dnotify mark on the relevant inode, run the list of dnotify structs
80 * on that mark and determine which of them has expressed interest in receiving
81 * events of this type. When found send the correct process and signal and
82 * destroy the dnotify struct if it was not registered to receive multiple
83 * events.
84 */
85static int dnotify_handle_event(struct fsnotify_group *group,
86 struct fsnotify_event *event)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Eric Paris3c5119c2009-05-21 17:01:33 -040088 struct fsnotify_mark_entry *entry = NULL;
89 struct dnotify_mark_entry *dnentry;
90 struct inode *to_tell;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 struct dnotify_struct *dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 struct dnotify_struct **prev;
Eric Paris3c5119c2009-05-21 17:01:33 -040093 struct fown_struct *fown;
Andreas Gruenbacher94552682009-10-15 00:13:23 +020094 __u32 test_mask = event->mask & ~FS_EVENT_ON_CHILD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Eric Paris3c5119c2009-05-21 17:01:33 -040096 to_tell = event->to_tell;
97
98 spin_lock(&to_tell->i_lock);
99 entry = fsnotify_find_mark_entry(group, to_tell);
100 spin_unlock(&to_tell->i_lock);
101
102 /* unlikely since we alreay passed dnotify_should_send_event() */
103 if (unlikely(!entry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return 0;
Eric Paris3c5119c2009-05-21 17:01:33 -0400105 dnentry = container_of(entry, struct dnotify_mark_entry, fsn_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Eric Paris3c5119c2009-05-21 17:01:33 -0400107 spin_lock(&entry->lock);
108 prev = &dnentry->dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 while ((dn = *prev) != NULL) {
Andreas Gruenbacher94552682009-10-15 00:13:23 +0200110 if ((dn->dn_mask & test_mask) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 prev = &dn->dn_next;
112 continue;
113 }
114 fown = &dn->dn_filp->f_owner;
115 send_sigio(fown, dn->dn_fd, POLL_MSG);
Eric Paris3c5119c2009-05-21 17:01:33 -0400116 if (dn->dn_mask & FS_DN_MULTISHOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 prev = &dn->dn_next;
118 else {
119 *prev = dn->dn_next;
Eric Paris3c5119c2009-05-21 17:01:33 -0400120 kmem_cache_free(dnotify_struct_cache, dn);
121 dnotify_recalc_inode_mask(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Eric Paris3c5119c2009-05-21 17:01:33 -0400125 spin_unlock(&entry->lock);
126 fsnotify_put_mark(entry);
127
128 return 0;
129}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131/*
Eric Paris3c5119c2009-05-21 17:01:33 -0400132 * Given an inode and mask determine if dnotify would be interested in sending
133 * userspace notification for that pair.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 */
Eric Paris3c5119c2009-05-21 17:01:33 -0400135static bool dnotify_should_send_event(struct fsnotify_group *group,
Eric Paris3a9fb892009-12-17 21:24:23 -0500136 struct inode *inode, struct vfsmount *mnt,
137 __u32 mask, void *data, int data_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Eric Paris3c5119c2009-05-21 17:01:33 -0400139 struct fsnotify_mark_entry *entry;
140 bool send;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Eric Paris3c5119c2009-05-21 17:01:33 -0400142 /* !dir_notify_enable should never get here, don't waste time checking
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 if (!dir_notify_enable)
Eric Paris3c5119c2009-05-21 17:01:33 -0400144 return 0; */
145
146 /* not a dir, dnotify doesn't care */
147 if (!S_ISDIR(inode->i_mode))
148 return false;
149
150 spin_lock(&inode->i_lock);
151 entry = fsnotify_find_mark_entry(group, inode);
152 spin_unlock(&inode->i_lock);
153
154 /* no mark means no dnotify watch */
155 if (!entry)
156 return false;
157
Eric Parise42e2772009-06-11 11:09:47 -0400158 mask = (mask & ~FS_EVENT_ON_CHILD);
Eric Paris5ac697b2009-06-11 11:09:47 -0400159 send = (mask & entry->mask);
Eric Parisce618562009-06-11 11:09:47 -0400160
Eric Paris3c5119c2009-05-21 17:01:33 -0400161 fsnotify_put_mark(entry); /* matches fsnotify_find_mark_entry */
162
163 return send;
164}
165
Eric Paris3c5119c2009-05-21 17:01:33 -0400166static void dnotify_free_mark(struct fsnotify_mark_entry *entry)
167{
168 struct dnotify_mark_entry *dnentry = container_of(entry,
169 struct dnotify_mark_entry,
170 fsn_entry);
171
172 BUG_ON(dnentry->dn);
173
174 kmem_cache_free(dnotify_mark_entry_cache, dnentry);
175}
176
177static struct fsnotify_ops dnotify_fsnotify_ops = {
178 .handle_event = dnotify_handle_event,
179 .should_send_event = dnotify_should_send_event,
180 .free_group_priv = NULL,
Eric Parisa092ee22009-06-11 11:09:48 -0400181 .freeing_mark = NULL,
Eric Parise4aff112009-05-21 17:01:50 -0400182 .free_event_priv = NULL,
Eric Paris3c5119c2009-05-21 17:01:33 -0400183};
184
185/*
186 * Called every time a file is closed. Looks first for a dnotify mark on the
187 * inode. If one is found run all of the ->dn entries attached to that
188 * mark for one relevant to this process closing the file and remove that
189 * dnotify_struct. If that was the last dnotify_struct also remove the
190 * fsnotify_mark_entry.
191 */
192void dnotify_flush(struct file *filp, fl_owner_t id)
193{
194 struct fsnotify_mark_entry *entry;
195 struct dnotify_mark_entry *dnentry;
196 struct dnotify_struct *dn;
197 struct dnotify_struct **prev;
198 struct inode *inode;
199
200 inode = filp->f_path.dentry->d_inode;
201 if (!S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return;
203
Eric Paris3c5119c2009-05-21 17:01:33 -0400204 spin_lock(&inode->i_lock);
205 entry = fsnotify_find_mark_entry(dnotify_group, inode);
206 spin_unlock(&inode->i_lock);
207 if (!entry)
208 return;
209 dnentry = container_of(entry, struct dnotify_mark_entry, fsn_entry);
210
211 mutex_lock(&dnotify_mark_mutex);
212
213 spin_lock(&entry->lock);
214 prev = &dnentry->dn;
215 while ((dn = *prev) != NULL) {
216 if ((dn->dn_owner == id) && (dn->dn_filp == filp)) {
217 *prev = dn->dn_next;
218 kmem_cache_free(dnotify_struct_cache, dn);
219 dnotify_recalc_inode_mask(entry);
220 break;
221 }
222 prev = &dn->dn_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
Eric Paris3c5119c2009-05-21 17:01:33 -0400224
225 spin_unlock(&entry->lock);
226
227 /* nothing else could have found us thanks to the dnotify_mark_mutex */
228 if (dnentry->dn == NULL)
229 fsnotify_destroy_mark_by_entry(entry);
230
231 fsnotify_recalc_group_mask(dnotify_group);
232
233 mutex_unlock(&dnotify_mark_mutex);
234
235 fsnotify_put_mark(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
Eric Paris3c5119c2009-05-21 17:01:33 -0400237
238/* this conversion is done only at watch creation */
239static __u32 convert_arg(unsigned long arg)
240{
241 __u32 new_mask = FS_EVENT_ON_CHILD;
242
243 if (arg & DN_MULTISHOT)
244 new_mask |= FS_DN_MULTISHOT;
245 if (arg & DN_DELETE)
246 new_mask |= (FS_DELETE | FS_MOVED_FROM);
247 if (arg & DN_MODIFY)
248 new_mask |= FS_MODIFY;
249 if (arg & DN_ACCESS)
250 new_mask |= FS_ACCESS;
251 if (arg & DN_ATTRIB)
252 new_mask |= FS_ATTRIB;
253 if (arg & DN_RENAME)
254 new_mask |= FS_DN_RENAME;
255 if (arg & DN_CREATE)
256 new_mask |= (FS_CREATE | FS_MOVED_TO);
257
258 return new_mask;
259}
260
261/*
262 * If multiple processes watch the same inode with dnotify there is only one
263 * dnotify mark in inode->i_fsnotify_mark_entries but we chain a dnotify_struct
264 * onto that mark. This function either attaches the new dnotify_struct onto
265 * that list, or it |= the mask onto an existing dnofiy_struct.
266 */
267static int attach_dn(struct dnotify_struct *dn, struct dnotify_mark_entry *dnentry,
268 fl_owner_t id, int fd, struct file *filp, __u32 mask)
269{
270 struct dnotify_struct *odn;
271
272 odn = dnentry->dn;
273 while (odn != NULL) {
274 /* adding more events to existing dnofiy_struct? */
275 if ((odn->dn_owner == id) && (odn->dn_filp == filp)) {
276 odn->dn_fd = fd;
277 odn->dn_mask |= mask;
278 return -EEXIST;
279 }
280 odn = odn->dn_next;
281 }
282
283 dn->dn_mask = mask;
284 dn->dn_fd = fd;
285 dn->dn_filp = filp;
286 dn->dn_owner = id;
287 dn->dn_next = dnentry->dn;
288 dnentry->dn = dn;
289
290 return 0;
291}
292
293/*
294 * When a process calls fcntl to attach a dnotify watch to a directory it ends
295 * up here. Allocate both a mark for fsnotify to add and a dnotify_struct to be
296 * attached to the fsnotify_mark.
297 */
298int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg)
299{
300 struct dnotify_mark_entry *new_dnentry, *dnentry;
301 struct fsnotify_mark_entry *new_entry, *entry;
302 struct dnotify_struct *dn;
303 struct inode *inode;
304 fl_owner_t id = current->files;
305 struct file *f;
306 int destroy = 0, error = 0;
307 __u32 mask;
308
309 /* we use these to tell if we need to kfree */
310 new_entry = NULL;
311 dn = NULL;
312
313 if (!dir_notify_enable) {
314 error = -EINVAL;
315 goto out_err;
316 }
317
318 /* a 0 mask means we are explicitly removing the watch */
319 if ((arg & ~DN_MULTISHOT) == 0) {
320 dnotify_flush(filp, id);
321 error = 0;
322 goto out_err;
323 }
324
325 /* dnotify only works on directories */
326 inode = filp->f_path.dentry->d_inode;
327 if (!S_ISDIR(inode->i_mode)) {
328 error = -ENOTDIR;
329 goto out_err;
330 }
331
332 /* expect most fcntl to add new rather than augment old */
333 dn = kmem_cache_alloc(dnotify_struct_cache, GFP_KERNEL);
334 if (!dn) {
335 error = -ENOMEM;
336 goto out_err;
337 }
338
339 /* new fsnotify mark, we expect most fcntl calls to add a new mark */
340 new_dnentry = kmem_cache_alloc(dnotify_mark_entry_cache, GFP_KERNEL);
341 if (!new_dnentry) {
342 error = -ENOMEM;
343 goto out_err;
344 }
345
346 /* convert the userspace DN_* "arg" to the internal FS_* defines in fsnotify */
347 mask = convert_arg(arg);
348
349 /* set up the new_entry and new_dnentry */
350 new_entry = &new_dnentry->fsn_entry;
351 fsnotify_init_mark(new_entry, dnotify_free_mark);
352 new_entry->mask = mask;
353 new_dnentry->dn = NULL;
354
355 /* this is needed to prevent the fcntl/close race described below */
356 mutex_lock(&dnotify_mark_mutex);
357
358 /* add the new_entry or find an old one. */
359 spin_lock(&inode->i_lock);
360 entry = fsnotify_find_mark_entry(dnotify_group, inode);
361 spin_unlock(&inode->i_lock);
362 if (entry) {
363 dnentry = container_of(entry, struct dnotify_mark_entry, fsn_entry);
364 spin_lock(&entry->lock);
365 } else {
Eric Paris40554c32009-12-17 20:12:05 -0500366 fsnotify_add_mark(new_entry, dnotify_group, inode, 0);
Eric Paris3c5119c2009-05-21 17:01:33 -0400367 spin_lock(&new_entry->lock);
368 entry = new_entry;
369 dnentry = new_dnentry;
370 /* we used new_entry, so don't free it */
371 new_entry = NULL;
372 }
373
374 rcu_read_lock();
375 f = fcheck(fd);
376 rcu_read_unlock();
377
378 /* if (f != filp) means that we lost a race and another task/thread
379 * actually closed the fd we are still playing with before we grabbed
380 * the dnotify_mark_mutex and entry->lock. Since closing the fd is the
381 * only time we clean up the mark entries we need to get our mark off
382 * the list. */
383 if (f != filp) {
384 /* if we added ourselves, shoot ourselves, it's possible that
385 * the flush actually did shoot this entry. That's fine too
386 * since multiple calls to destroy_mark is perfectly safe, if
387 * we found a dnentry already attached to the inode, just sod
388 * off silently as the flush at close time dealt with it.
389 */
390 if (dnentry == new_dnentry)
391 destroy = 1;
392 goto out;
393 }
394
395 error = __f_setown(filp, task_pid(current), PIDTYPE_PID, 0);
396 if (error) {
397 /* if we added, we must shoot */
398 if (dnentry == new_dnentry)
399 destroy = 1;
400 goto out;
401 }
402
403 error = attach_dn(dn, dnentry, id, fd, filp, mask);
404 /* !error means that we attached the dn to the dnentry, so don't free it */
405 if (!error)
406 dn = NULL;
407 /* -EEXIST means that we didn't add this new dn and used an old one.
408 * that isn't an error (and the unused dn should be freed) */
409 else if (error == -EEXIST)
410 error = 0;
411
412 dnotify_recalc_inode_mask(entry);
413out:
414 spin_unlock(&entry->lock);
415
416 if (destroy)
417 fsnotify_destroy_mark_by_entry(entry);
418
419 fsnotify_recalc_group_mask(dnotify_group);
420
421 mutex_unlock(&dnotify_mark_mutex);
422 fsnotify_put_mark(entry);
423out_err:
424 if (new_entry)
425 fsnotify_put_mark(new_entry);
426 if (dn)
427 kmem_cache_free(dnotify_struct_cache, dn);
428 return error;
429}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431static int __init dnotify_init(void)
432{
Eric Paris3c5119c2009-05-21 17:01:33 -0400433 dnotify_struct_cache = KMEM_CACHE(dnotify_struct, SLAB_PANIC);
434 dnotify_mark_entry_cache = KMEM_CACHE(dnotify_mark_entry, SLAB_PANIC);
435
Eric Paris0d2e2a12009-12-17 21:24:22 -0500436 dnotify_group = fsnotify_alloc_group(&dnotify_fsnotify_ops);
Eric Paris3c5119c2009-05-21 17:01:33 -0400437 if (IS_ERR(dnotify_group))
438 panic("unable to allocate fsnotify group for dnotify\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return 0;
440}
441
442module_init(dnotify_init)