2 * Directory notifications for Linux.
4 * Copyright (C) 2000,2001,2002 Stephen Rothwell
6 * Copyright (C) 2009 Eric Paris <Red Hat Inc>
7 * dnotify was largly rewritten to use the new fsnotify infrastructure
9 * 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
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.
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>
26 #include <linux/fdtable.h>
27 #include <linux/fsnotify_backend.h>
29 int dir_notify_enable __read_mostly = 1;
31 static struct kmem_cache *dnotify_struct_cache __read_mostly;
32 static struct kmem_cache *dnotify_mark_entry_cache __read_mostly;
33 static struct fsnotify_group *dnotify_group __read_mostly;
34 static DEFINE_MUTEX(dnotify_mark_mutex);
37 * dnotify will attach one of these to each inode (i_fsnotify_marks) 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
41 struct dnotify_mark_entry {
42 struct fsnotify_mark fsn_entry;
43 struct dnotify_struct *dn;
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
54 static void dnotify_recalc_inode_mask(struct fsnotify_mark *entry)
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,
62 assert_spin_locked(&entry->lock);
64 old_mask = entry->mask;
66 for (dn = dnentry->dn; dn != NULL; dn = dn->dn_next)
67 new_mask |= (dn->dn_mask & ~FS_DN_MULTISHOT);
68 entry->mask = new_mask;
70 if (old_mask == new_mask)
74 fsnotify_recalc_inode_mask(entry->i.inode);
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
85 static int dnotify_handle_event(struct fsnotify_group *group,
86 struct fsnotify_event *event)
88 struct fsnotify_mark *entry = NULL;
89 struct dnotify_mark_entry *dnentry;
90 struct inode *to_tell;
91 struct dnotify_struct *dn;
92 struct dnotify_struct **prev;
93 struct fown_struct *fown;
94 __u32 test_mask = event->mask & ~FS_EVENT_ON_CHILD;
96 to_tell = event->to_tell;
98 spin_lock(&to_tell->i_lock);
99 entry = fsnotify_find_mark(group, to_tell);
100 spin_unlock(&to_tell->i_lock);
102 /* unlikely since we alreay passed dnotify_should_send_event() */
103 if (unlikely(!entry))
105 dnentry = container_of(entry, struct dnotify_mark_entry, fsn_entry);
107 spin_lock(&entry->lock);
109 while ((dn = *prev) != NULL) {
110 if ((dn->dn_mask & test_mask) == 0) {
114 fown = &dn->dn_filp->f_owner;
115 send_sigio(fown, dn->dn_fd, POLL_MSG);
116 if (dn->dn_mask & FS_DN_MULTISHOT)
120 kmem_cache_free(dnotify_struct_cache, dn);
121 dnotify_recalc_inode_mask(entry);
125 spin_unlock(&entry->lock);
126 fsnotify_put_mark(entry);
132 * Given an inode and mask determine if dnotify would be interested in sending
133 * userspace notification for that pair.
135 static bool dnotify_should_send_event(struct fsnotify_group *group,
136 struct inode *inode, struct vfsmount *mnt,
137 __u32 mask, void *data, int data_type)
139 struct fsnotify_mark *entry;
142 /* !dir_notify_enable should never get here, don't waste time checking
143 if (!dir_notify_enable)
146 /* not a dir, dnotify doesn't care */
147 if (!S_ISDIR(inode->i_mode))
150 spin_lock(&inode->i_lock);
151 entry = fsnotify_find_mark(group, inode);
152 spin_unlock(&inode->i_lock);
154 /* no mark means no dnotify watch */
158 mask = (mask & ~FS_EVENT_ON_CHILD);
159 send = (mask & entry->mask);
161 fsnotify_put_mark(entry); /* matches fsnotify_find_mark */
166 static void dnotify_free_mark(struct fsnotify_mark *entry)
168 struct dnotify_mark_entry *dnentry = container_of(entry,
169 struct dnotify_mark_entry,
174 kmem_cache_free(dnotify_mark_entry_cache, dnentry);
177 static 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,
181 .freeing_mark = NULL,
182 .free_event_priv = NULL,
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 structures 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
192 void dnotify_flush(struct file *filp, fl_owner_t id)
194 struct fsnotify_mark *entry;
195 struct dnotify_mark_entry *dnentry;
196 struct dnotify_struct *dn;
197 struct dnotify_struct **prev;
200 inode = filp->f_path.dentry->d_inode;
201 if (!S_ISDIR(inode->i_mode))
204 spin_lock(&inode->i_lock);
205 entry = fsnotify_find_mark(dnotify_group, inode);
206 spin_unlock(&inode->i_lock);
209 dnentry = container_of(entry, struct dnotify_mark_entry, fsn_entry);
211 mutex_lock(&dnotify_mark_mutex);
213 spin_lock(&entry->lock);
215 while ((dn = *prev) != NULL) {
216 if ((dn->dn_owner == id) && (dn->dn_filp == filp)) {
218 kmem_cache_free(dnotify_struct_cache, dn);
219 dnotify_recalc_inode_mask(entry);
225 spin_unlock(&entry->lock);
227 /* nothing else could have found us thanks to the dnotify_mark_mutex */
228 if (dnentry->dn == NULL)
229 fsnotify_destroy_mark(entry);
231 fsnotify_recalc_group_mask(dnotify_group);
233 mutex_unlock(&dnotify_mark_mutex);
235 fsnotify_put_mark(entry);
238 /* this conversion is done only at watch creation */
239 static __u32 convert_arg(unsigned long arg)
241 __u32 new_mask = FS_EVENT_ON_CHILD;
243 if (arg & DN_MULTISHOT)
244 new_mask |= FS_DN_MULTISHOT;
246 new_mask |= (FS_DELETE | FS_MOVED_FROM);
248 new_mask |= FS_MODIFY;
250 new_mask |= FS_ACCESS;
252 new_mask |= FS_ATTRIB;
254 new_mask |= FS_DN_RENAME;
256 new_mask |= (FS_CREATE | FS_MOVED_TO);
262 * If multiple processes watch the same inode with dnotify there is only one
263 * dnotify mark in inode->i_fsnotify_marks 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.
267 static int attach_dn(struct dnotify_struct *dn, struct dnotify_mark_entry *dnentry,
268 fl_owner_t id, int fd, struct file *filp, __u32 mask)
270 struct dnotify_struct *odn;
273 while (odn != NULL) {
274 /* adding more events to existing dnofiy_struct? */
275 if ((odn->dn_owner == id) && (odn->dn_filp == filp)) {
277 odn->dn_mask |= mask;
287 dn->dn_next = dnentry->dn;
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.
298 int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg)
300 struct dnotify_mark_entry *new_dnentry, *dnentry;
301 struct fsnotify_mark *new_entry, *entry;
302 struct dnotify_struct *dn;
304 fl_owner_t id = current->files;
306 int destroy = 0, error = 0;
309 /* we use these to tell if we need to kfree */
313 if (!dir_notify_enable) {
318 /* a 0 mask means we are explicitly removing the watch */
319 if ((arg & ~DN_MULTISHOT) == 0) {
320 dnotify_flush(filp, id);
325 /* dnotify only works on directories */
326 inode = filp->f_path.dentry->d_inode;
327 if (!S_ISDIR(inode->i_mode)) {
332 /* expect most fcntl to add new rather than augment old */
333 dn = kmem_cache_alloc(dnotify_struct_cache, GFP_KERNEL);
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);
346 /* convert the userspace DN_* "arg" to the internal FS_* defines in fsnotify */
347 mask = convert_arg(arg);
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;
355 /* this is needed to prevent the fcntl/close race described below */
356 mutex_lock(&dnotify_mark_mutex);
358 /* add the new_entry or find an old one. */
359 spin_lock(&inode->i_lock);
360 entry = fsnotify_find_mark(dnotify_group, inode);
361 spin_unlock(&inode->i_lock);
363 dnentry = container_of(entry, struct dnotify_mark_entry, fsn_entry);
364 spin_lock(&entry->lock);
366 fsnotify_add_mark(new_entry, dnotify_group, inode, 0);
367 spin_lock(&new_entry->lock);
369 dnentry = new_dnentry;
370 /* we used new_entry, so don't free it */
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 marks we need to get our mark off
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.
390 if (dnentry == new_dnentry)
395 error = __f_setown(filp, task_pid(current), PIDTYPE_PID, 0);
397 /* if we added, we must shoot */
398 if (dnentry == new_dnentry)
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 */
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)
412 dnotify_recalc_inode_mask(entry);
414 spin_unlock(&entry->lock);
417 fsnotify_destroy_mark(entry);
419 fsnotify_recalc_group_mask(dnotify_group);
421 mutex_unlock(&dnotify_mark_mutex);
422 fsnotify_put_mark(entry);
425 fsnotify_put_mark(new_entry);
427 kmem_cache_free(dnotify_struct_cache, dn);
431 static int __init dnotify_init(void)
433 dnotify_struct_cache = KMEM_CACHE(dnotify_struct, SLAB_PANIC);
434 dnotify_mark_entry_cache = KMEM_CACHE(dnotify_mark_entry, SLAB_PANIC);
436 dnotify_group = fsnotify_alloc_group(&dnotify_fsnotify_ops);
437 if (IS_ERR(dnotify_group))
438 panic("unable to allocate fsnotify group for dnotify\n");
442 module_init(dnotify_init)