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_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
41 struct dnotify_mark_entry
{
42 struct fsnotify_mark_entry 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
*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
->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
*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
;
95 to_tell
= event
->to_tell
;
97 spin_lock(&to_tell
->i_lock
);
98 entry
= fsnotify_find_mark_entry(group
, to_tell
);
99 spin_unlock(&to_tell
->i_lock
);
101 /* unlikely since we alreay passed dnotify_should_send_event() */
102 if (unlikely(!entry
))
104 dnentry
= container_of(entry
, struct dnotify_mark_entry
, fsn_entry
);
106 spin_lock(&entry
->lock
);
108 while ((dn
= *prev
) != NULL
) {
109 if ((dn
->dn_mask
& event
->mask
) == 0) {
113 fown
= &dn
->dn_filp
->f_owner
;
114 send_sigio(fown
, dn
->dn_fd
, POLL_MSG
);
115 if (dn
->dn_mask
& FS_DN_MULTISHOT
)
119 kmem_cache_free(dnotify_struct_cache
, dn
);
120 dnotify_recalc_inode_mask(entry
);
124 spin_unlock(&entry
->lock
);
125 fsnotify_put_mark(entry
);
131 * Given an inode and mask determine if dnotify would be interested in sending
132 * userspace notification for that pair.
134 static bool dnotify_should_send_event(struct fsnotify_group
*group
,
135 struct inode
*inode
, __u32 mask
)
137 struct fsnotify_mark_entry
*entry
;
140 /* !dir_notify_enable should never get here, don't waste time checking
141 if (!dir_notify_enable)
144 /* not a dir, dnotify doesn't care */
145 if (!S_ISDIR(inode
->i_mode
))
148 spin_lock(&inode
->i_lock
);
149 entry
= fsnotify_find_mark_entry(group
, inode
);
150 spin_unlock(&inode
->i_lock
);
152 /* no mark means no dnotify watch */
156 mask
= (mask
& ~FS_EVENT_ON_CHILD
);
157 send
= (mask
& entry
->mask
);
159 fsnotify_put_mark(entry
); /* matches fsnotify_find_mark_entry */
164 static void dnotify_free_mark(struct fsnotify_mark_entry
*entry
)
166 struct dnotify_mark_entry
*dnentry
= container_of(entry
,
167 struct dnotify_mark_entry
,
172 kmem_cache_free(dnotify_mark_entry_cache
, dnentry
);
175 static struct fsnotify_ops dnotify_fsnotify_ops
= {
176 .handle_event
= dnotify_handle_event
,
177 .should_send_event
= dnotify_should_send_event
,
178 .free_group_priv
= NULL
,
179 .freeing_mark
= NULL
,
180 .free_event_priv
= NULL
,
184 * Called every time a file is closed. Looks first for a dnotify mark on the
185 * inode. If one is found run all of the ->dn entries attached to that
186 * mark for one relevant to this process closing the file and remove that
187 * dnotify_struct. If that was the last dnotify_struct also remove the
188 * fsnotify_mark_entry.
190 void dnotify_flush(struct file
*filp
, fl_owner_t id
)
192 struct fsnotify_mark_entry
*entry
;
193 struct dnotify_mark_entry
*dnentry
;
194 struct dnotify_struct
*dn
;
195 struct dnotify_struct
**prev
;
198 inode
= filp
->f_path
.dentry
->d_inode
;
199 if (!S_ISDIR(inode
->i_mode
))
202 spin_lock(&inode
->i_lock
);
203 entry
= fsnotify_find_mark_entry(dnotify_group
, inode
);
204 spin_unlock(&inode
->i_lock
);
207 dnentry
= container_of(entry
, struct dnotify_mark_entry
, fsn_entry
);
209 mutex_lock(&dnotify_mark_mutex
);
211 spin_lock(&entry
->lock
);
213 while ((dn
= *prev
) != NULL
) {
214 if ((dn
->dn_owner
== id
) && (dn
->dn_filp
== filp
)) {
216 kmem_cache_free(dnotify_struct_cache
, dn
);
217 dnotify_recalc_inode_mask(entry
);
223 spin_unlock(&entry
->lock
);
225 /* nothing else could have found us thanks to the dnotify_mark_mutex */
226 if (dnentry
->dn
== NULL
)
227 fsnotify_destroy_mark_by_entry(entry
);
229 fsnotify_recalc_group_mask(dnotify_group
);
231 mutex_unlock(&dnotify_mark_mutex
);
233 fsnotify_put_mark(entry
);
236 /* this conversion is done only at watch creation */
237 static __u32
convert_arg(unsigned long arg
)
239 __u32 new_mask
= FS_EVENT_ON_CHILD
;
241 if (arg
& DN_MULTISHOT
)
242 new_mask
|= FS_DN_MULTISHOT
;
244 new_mask
|= (FS_DELETE
| FS_MOVED_FROM
);
246 new_mask
|= FS_MODIFY
;
248 new_mask
|= FS_ACCESS
;
250 new_mask
|= FS_ATTRIB
;
252 new_mask
|= FS_DN_RENAME
;
254 new_mask
|= (FS_CREATE
| FS_MOVED_TO
);
260 * If multiple processes watch the same inode with dnotify there is only one
261 * dnotify mark in inode->i_fsnotify_mark_entries but we chain a dnotify_struct
262 * onto that mark. This function either attaches the new dnotify_struct onto
263 * that list, or it |= the mask onto an existing dnofiy_struct.
265 static int attach_dn(struct dnotify_struct
*dn
, struct dnotify_mark_entry
*dnentry
,
266 fl_owner_t id
, int fd
, struct file
*filp
, __u32 mask
)
268 struct dnotify_struct
*odn
;
271 while (odn
!= NULL
) {
272 /* adding more events to existing dnofiy_struct? */
273 if ((odn
->dn_owner
== id
) && (odn
->dn_filp
== filp
)) {
275 odn
->dn_mask
|= mask
;
285 dn
->dn_next
= dnentry
->dn
;
292 * When a process calls fcntl to attach a dnotify watch to a directory it ends
293 * up here. Allocate both a mark for fsnotify to add and a dnotify_struct to be
294 * attached to the fsnotify_mark.
296 int fcntl_dirnotify(int fd
, struct file
*filp
, unsigned long arg
)
298 struct dnotify_mark_entry
*new_dnentry
, *dnentry
;
299 struct fsnotify_mark_entry
*new_entry
, *entry
;
300 struct dnotify_struct
*dn
;
302 fl_owner_t id
= current
->files
;
304 int destroy
= 0, error
= 0;
307 /* we use these to tell if we need to kfree */
311 if (!dir_notify_enable
) {
316 /* a 0 mask means we are explicitly removing the watch */
317 if ((arg
& ~DN_MULTISHOT
) == 0) {
318 dnotify_flush(filp
, id
);
323 /* dnotify only works on directories */
324 inode
= filp
->f_path
.dentry
->d_inode
;
325 if (!S_ISDIR(inode
->i_mode
)) {
330 /* expect most fcntl to add new rather than augment old */
331 dn
= kmem_cache_alloc(dnotify_struct_cache
, GFP_KERNEL
);
337 /* new fsnotify mark, we expect most fcntl calls to add a new mark */
338 new_dnentry
= kmem_cache_alloc(dnotify_mark_entry_cache
, GFP_KERNEL
);
344 /* convert the userspace DN_* "arg" to the internal FS_* defines in fsnotify */
345 mask
= convert_arg(arg
);
347 /* set up the new_entry and new_dnentry */
348 new_entry
= &new_dnentry
->fsn_entry
;
349 fsnotify_init_mark(new_entry
, dnotify_free_mark
);
350 new_entry
->mask
= mask
;
351 new_dnentry
->dn
= NULL
;
353 /* this is needed to prevent the fcntl/close race described below */
354 mutex_lock(&dnotify_mark_mutex
);
356 /* add the new_entry or find an old one. */
357 spin_lock(&inode
->i_lock
);
358 entry
= fsnotify_find_mark_entry(dnotify_group
, inode
);
359 spin_unlock(&inode
->i_lock
);
361 dnentry
= container_of(entry
, struct dnotify_mark_entry
, fsn_entry
);
362 spin_lock(&entry
->lock
);
364 fsnotify_add_mark(new_entry
, dnotify_group
, inode
);
365 spin_lock(&new_entry
->lock
);
367 dnentry
= new_dnentry
;
368 /* we used new_entry, so don't free it */
376 /* if (f != filp) means that we lost a race and another task/thread
377 * actually closed the fd we are still playing with before we grabbed
378 * the dnotify_mark_mutex and entry->lock. Since closing the fd is the
379 * only time we clean up the mark entries we need to get our mark off
382 /* if we added ourselves, shoot ourselves, it's possible that
383 * the flush actually did shoot this entry. That's fine too
384 * since multiple calls to destroy_mark is perfectly safe, if
385 * we found a dnentry already attached to the inode, just sod
386 * off silently as the flush at close time dealt with it.
388 if (dnentry
== new_dnentry
)
393 error
= __f_setown(filp
, task_pid(current
), PIDTYPE_PID
, 0);
395 /* if we added, we must shoot */
396 if (dnentry
== new_dnentry
)
401 error
= attach_dn(dn
, dnentry
, id
, fd
, filp
, mask
);
402 /* !error means that we attached the dn to the dnentry, so don't free it */
405 /* -EEXIST means that we didn't add this new dn and used an old one.
406 * that isn't an error (and the unused dn should be freed) */
407 else if (error
== -EEXIST
)
410 dnotify_recalc_inode_mask(entry
);
412 spin_unlock(&entry
->lock
);
415 fsnotify_destroy_mark_by_entry(entry
);
417 fsnotify_recalc_group_mask(dnotify_group
);
419 mutex_unlock(&dnotify_mark_mutex
);
420 fsnotify_put_mark(entry
);
423 fsnotify_put_mark(new_entry
);
425 kmem_cache_free(dnotify_struct_cache
, dn
);
429 static int __init
dnotify_init(void)
431 dnotify_struct_cache
= KMEM_CACHE(dnotify_struct
, SLAB_PANIC
);
432 dnotify_mark_entry_cache
= KMEM_CACHE(dnotify_mark_entry
, SLAB_PANIC
);
434 dnotify_group
= fsnotify_obtain_group(DNOTIFY_GROUP_NUM
,
435 0, &dnotify_fsnotify_ops
);
436 if (IS_ERR(dnotify_group
))
437 panic("unable to allocate fsnotify group for dnotify\n");
441 module_init(dnotify_init
)