blob: 98a751614c748076e4b3514582ec0e56cfac2770 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Eric Paris3c5119c2009-05-21 17:01:33 -040095 to_tell = event->to_tell;
96
97 spin_lock(&to_tell->i_lock);
98 entry = fsnotify_find_mark_entry(group, to_tell);
99 spin_unlock(&to_tell->i_lock);
100
101 /* unlikely since we alreay passed dnotify_should_send_event() */
102 if (unlikely(!entry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return 0;
Eric Paris3c5119c2009-05-21 17:01:33 -0400104 dnentry = container_of(entry, struct dnotify_mark_entry, fsn_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Eric Paris3c5119c2009-05-21 17:01:33 -0400106 spin_lock(&entry->lock);
107 prev = &dnentry->dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 while ((dn = *prev) != NULL) {
Eric Paris3c5119c2009-05-21 17:01:33 -0400109 if ((dn->dn_mask & event->mask) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 prev = &dn->dn_next;
111 continue;
112 }
113 fown = &dn->dn_filp->f_owner;
114 send_sigio(fown, dn->dn_fd, POLL_MSG);
Eric Paris3c5119c2009-05-21 17:01:33 -0400115 if (dn->dn_mask & FS_DN_MULTISHOT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 prev = &dn->dn_next;
117 else {
118 *prev = dn->dn_next;
Eric Paris3c5119c2009-05-21 17:01:33 -0400119 kmem_cache_free(dnotify_struct_cache, dn);
120 dnotify_recalc_inode_mask(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Eric Paris3c5119c2009-05-21 17:01:33 -0400124 spin_unlock(&entry->lock);
125 fsnotify_put_mark(entry);
126
127 return 0;
128}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130/*
Eric Paris3c5119c2009-05-21 17:01:33 -0400131 * Given an inode and mask determine if dnotify would be interested in sending
132 * userspace notification for that pair.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 */
Eric Paris3c5119c2009-05-21 17:01:33 -0400134static bool dnotify_should_send_event(struct fsnotify_group *group,
135 struct inode *inode, __u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Eric Paris3c5119c2009-05-21 17:01:33 -0400137 struct fsnotify_mark_entry *entry;
138 bool send;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Eric Paris3c5119c2009-05-21 17:01:33 -0400140 /* !dir_notify_enable should never get here, don't waste time checking
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if (!dir_notify_enable)
Eric Paris3c5119c2009-05-21 17:01:33 -0400142 return 0; */
143
144 /* not a dir, dnotify doesn't care */
145 if (!S_ISDIR(inode->i_mode))
146 return false;
147
148 spin_lock(&inode->i_lock);
149 entry = fsnotify_find_mark_entry(group, inode);
150 spin_unlock(&inode->i_lock);
151
152 /* no mark means no dnotify watch */
153 if (!entry)
154 return false;
155
Eric Parise42e2772009-06-11 11:09:47 -0400156 mask = (mask & ~FS_EVENT_ON_CHILD);
Eric Paris5ac697b2009-06-11 11:09:47 -0400157 send = (mask & entry->mask);
Eric Parisce618562009-06-11 11:09:47 -0400158
Eric Paris3c5119c2009-05-21 17:01:33 -0400159 fsnotify_put_mark(entry); /* matches fsnotify_find_mark_entry */
160
161 return send;
162}
163
164static void dnotify_freeing_mark(struct fsnotify_mark_entry *entry,
165 struct fsnotify_group *group)
166{
167 /* dnotify doesn't care than an inode is on the way out */
168}
169
170static void dnotify_free_mark(struct fsnotify_mark_entry *entry)
171{
172 struct dnotify_mark_entry *dnentry = container_of(entry,
173 struct dnotify_mark_entry,
174 fsn_entry);
175
176 BUG_ON(dnentry->dn);
177
178 kmem_cache_free(dnotify_mark_entry_cache, dnentry);
179}
180
181static struct fsnotify_ops dnotify_fsnotify_ops = {
182 .handle_event = dnotify_handle_event,
183 .should_send_event = dnotify_should_send_event,
184 .free_group_priv = NULL,
185 .freeing_mark = dnotify_freeing_mark,
Eric Parise4aff112009-05-21 17:01:50 -0400186 .free_event_priv = NULL,
Eric Paris3c5119c2009-05-21 17:01:33 -0400187};
188
189/*
190 * Called every time a file is closed. Looks first for a dnotify mark on the
191 * inode. If one is found run all of the ->dn entries attached to that
192 * mark for one relevant to this process closing the file and remove that
193 * dnotify_struct. If that was the last dnotify_struct also remove the
194 * fsnotify_mark_entry.
195 */
196void dnotify_flush(struct file *filp, fl_owner_t id)
197{
198 struct fsnotify_mark_entry *entry;
199 struct dnotify_mark_entry *dnentry;
200 struct dnotify_struct *dn;
201 struct dnotify_struct **prev;
202 struct inode *inode;
203
204 inode = filp->f_path.dentry->d_inode;
205 if (!S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return;
207
Eric Paris3c5119c2009-05-21 17:01:33 -0400208 spin_lock(&inode->i_lock);
209 entry = fsnotify_find_mark_entry(dnotify_group, inode);
210 spin_unlock(&inode->i_lock);
211 if (!entry)
212 return;
213 dnentry = container_of(entry, struct dnotify_mark_entry, fsn_entry);
214
215 mutex_lock(&dnotify_mark_mutex);
216
217 spin_lock(&entry->lock);
218 prev = &dnentry->dn;
219 while ((dn = *prev) != NULL) {
220 if ((dn->dn_owner == id) && (dn->dn_filp == filp)) {
221 *prev = dn->dn_next;
222 kmem_cache_free(dnotify_struct_cache, dn);
223 dnotify_recalc_inode_mask(entry);
224 break;
225 }
226 prev = &dn->dn_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
Eric Paris3c5119c2009-05-21 17:01:33 -0400228
229 spin_unlock(&entry->lock);
230
231 /* nothing else could have found us thanks to the dnotify_mark_mutex */
232 if (dnentry->dn == NULL)
233 fsnotify_destroy_mark_by_entry(entry);
234
235 fsnotify_recalc_group_mask(dnotify_group);
236
237 mutex_unlock(&dnotify_mark_mutex);
238
239 fsnotify_put_mark(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
Eric Paris3c5119c2009-05-21 17:01:33 -0400241
242/* this conversion is done only at watch creation */
243static __u32 convert_arg(unsigned long arg)
244{
245 __u32 new_mask = FS_EVENT_ON_CHILD;
246
247 if (arg & DN_MULTISHOT)
248 new_mask |= FS_DN_MULTISHOT;
249 if (arg & DN_DELETE)
250 new_mask |= (FS_DELETE | FS_MOVED_FROM);
251 if (arg & DN_MODIFY)
252 new_mask |= FS_MODIFY;
253 if (arg & DN_ACCESS)
254 new_mask |= FS_ACCESS;
255 if (arg & DN_ATTRIB)
256 new_mask |= FS_ATTRIB;
257 if (arg & DN_RENAME)
258 new_mask |= FS_DN_RENAME;
259 if (arg & DN_CREATE)
260 new_mask |= (FS_CREATE | FS_MOVED_TO);
261
262 return new_mask;
263}
264
265/*
266 * If multiple processes watch the same inode with dnotify there is only one
267 * dnotify mark in inode->i_fsnotify_mark_entries but we chain a dnotify_struct
268 * onto that mark. This function either attaches the new dnotify_struct onto
269 * that list, or it |= the mask onto an existing dnofiy_struct.
270 */
271static int attach_dn(struct dnotify_struct *dn, struct dnotify_mark_entry *dnentry,
272 fl_owner_t id, int fd, struct file *filp, __u32 mask)
273{
274 struct dnotify_struct *odn;
275
276 odn = dnentry->dn;
277 while (odn != NULL) {
278 /* adding more events to existing dnofiy_struct? */
279 if ((odn->dn_owner == id) && (odn->dn_filp == filp)) {
280 odn->dn_fd = fd;
281 odn->dn_mask |= mask;
282 return -EEXIST;
283 }
284 odn = odn->dn_next;
285 }
286
287 dn->dn_mask = mask;
288 dn->dn_fd = fd;
289 dn->dn_filp = filp;
290 dn->dn_owner = id;
291 dn->dn_next = dnentry->dn;
292 dnentry->dn = dn;
293
294 return 0;
295}
296
297/*
298 * When a process calls fcntl to attach a dnotify watch to a directory it ends
299 * up here. Allocate both a mark for fsnotify to add and a dnotify_struct to be
300 * attached to the fsnotify_mark.
301 */
302int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg)
303{
304 struct dnotify_mark_entry *new_dnentry, *dnentry;
305 struct fsnotify_mark_entry *new_entry, *entry;
306 struct dnotify_struct *dn;
307 struct inode *inode;
308 fl_owner_t id = current->files;
309 struct file *f;
310 int destroy = 0, error = 0;
311 __u32 mask;
312
313 /* we use these to tell if we need to kfree */
314 new_entry = NULL;
315 dn = NULL;
316
317 if (!dir_notify_enable) {
318 error = -EINVAL;
319 goto out_err;
320 }
321
322 /* a 0 mask means we are explicitly removing the watch */
323 if ((arg & ~DN_MULTISHOT) == 0) {
324 dnotify_flush(filp, id);
325 error = 0;
326 goto out_err;
327 }
328
329 /* dnotify only works on directories */
330 inode = filp->f_path.dentry->d_inode;
331 if (!S_ISDIR(inode->i_mode)) {
332 error = -ENOTDIR;
333 goto out_err;
334 }
335
336 /* expect most fcntl to add new rather than augment old */
337 dn = kmem_cache_alloc(dnotify_struct_cache, GFP_KERNEL);
338 if (!dn) {
339 error = -ENOMEM;
340 goto out_err;
341 }
342
343 /* new fsnotify mark, we expect most fcntl calls to add a new mark */
344 new_dnentry = kmem_cache_alloc(dnotify_mark_entry_cache, GFP_KERNEL);
345 if (!new_dnentry) {
346 error = -ENOMEM;
347 goto out_err;
348 }
349
350 /* convert the userspace DN_* "arg" to the internal FS_* defines in fsnotify */
351 mask = convert_arg(arg);
352
353 /* set up the new_entry and new_dnentry */
354 new_entry = &new_dnentry->fsn_entry;
355 fsnotify_init_mark(new_entry, dnotify_free_mark);
356 new_entry->mask = mask;
357 new_dnentry->dn = NULL;
358
359 /* this is needed to prevent the fcntl/close race described below */
360 mutex_lock(&dnotify_mark_mutex);
361
362 /* add the new_entry or find an old one. */
363 spin_lock(&inode->i_lock);
364 entry = fsnotify_find_mark_entry(dnotify_group, inode);
365 spin_unlock(&inode->i_lock);
366 if (entry) {
367 dnentry = container_of(entry, struct dnotify_mark_entry, fsn_entry);
368 spin_lock(&entry->lock);
369 } else {
370 fsnotify_add_mark(new_entry, dnotify_group, inode);
371 spin_lock(&new_entry->lock);
372 entry = new_entry;
373 dnentry = new_dnentry;
374 /* we used new_entry, so don't free it */
375 new_entry = NULL;
376 }
377
378 rcu_read_lock();
379 f = fcheck(fd);
380 rcu_read_unlock();
381
382 /* if (f != filp) means that we lost a race and another task/thread
383 * actually closed the fd we are still playing with before we grabbed
384 * the dnotify_mark_mutex and entry->lock. Since closing the fd is the
385 * only time we clean up the mark entries we need to get our mark off
386 * the list. */
387 if (f != filp) {
388 /* if we added ourselves, shoot ourselves, it's possible that
389 * the flush actually did shoot this entry. That's fine too
390 * since multiple calls to destroy_mark is perfectly safe, if
391 * we found a dnentry already attached to the inode, just sod
392 * off silently as the flush at close time dealt with it.
393 */
394 if (dnentry == new_dnentry)
395 destroy = 1;
396 goto out;
397 }
398
399 error = __f_setown(filp, task_pid(current), PIDTYPE_PID, 0);
400 if (error) {
401 /* if we added, we must shoot */
402 if (dnentry == new_dnentry)
403 destroy = 1;
404 goto out;
405 }
406
407 error = attach_dn(dn, dnentry, id, fd, filp, mask);
408 /* !error means that we attached the dn to the dnentry, so don't free it */
409 if (!error)
410 dn = NULL;
411 /* -EEXIST means that we didn't add this new dn and used an old one.
412 * that isn't an error (and the unused dn should be freed) */
413 else if (error == -EEXIST)
414 error = 0;
415
416 dnotify_recalc_inode_mask(entry);
417out:
418 spin_unlock(&entry->lock);
419
420 if (destroy)
421 fsnotify_destroy_mark_by_entry(entry);
422
423 fsnotify_recalc_group_mask(dnotify_group);
424
425 mutex_unlock(&dnotify_mark_mutex);
426 fsnotify_put_mark(entry);
427out_err:
428 if (new_entry)
429 fsnotify_put_mark(new_entry);
430 if (dn)
431 kmem_cache_free(dnotify_struct_cache, dn);
432 return error;
433}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435static int __init dnotify_init(void)
436{
Eric Paris3c5119c2009-05-21 17:01:33 -0400437 dnotify_struct_cache = KMEM_CACHE(dnotify_struct, SLAB_PANIC);
438 dnotify_mark_entry_cache = KMEM_CACHE(dnotify_mark_entry, SLAB_PANIC);
439
440 dnotify_group = fsnotify_obtain_group(DNOTIFY_GROUP_NUM,
441 0, &dnotify_fsnotify_ops);
442 if (IS_ERR(dnotify_group))
443 panic("unable to allocate fsnotify group for dnotify\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return 0;
445}
446
447module_init(dnotify_init)