]> bbs.cooldavid.org Git - net-next-2.6.git/blob - fs/notify/dnotify/dnotify.c
e6edae60894d1ecf2b4072002b6fad1dde9cc35a
[net-next-2.6.git] / fs / notify / dnotify / dnotify.c
1 /*
2  * Directory notifications for Linux.
3  *
4  * Copyright (C) 2000,2001,2002 Stephen Rothwell
5  *
6  * Copyright (C) 2009 Eric Paris <Red Hat Inc>
7  * dnotify was largly rewritten to use the new fsnotify infrastructure
8  *
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
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>
26 #include <linux/fdtable.h>
27 #include <linux/fsnotify_backend.h>
28
29 int dir_notify_enable __read_mostly = 1;
30
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);
35
36 /*
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
40  */
41 struct dnotify_mark_entry {
42         struct fsnotify_mark fsn_entry;
43         struct dnotify_struct *dn;
44 };
45
46 /*
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  */
54 static void dnotify_recalc_inode_mask(struct fsnotify_mark *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;
65         new_mask = 0;
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;
69
70         if (old_mask == new_mask)
71                 return;
72
73         if (entry->i.inode)
74                 fsnotify_recalc_inode_mask(entry->i.inode);
75 }
76
77 /*
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  */
85 static int dnotify_handle_event(struct fsnotify_group *group,
86                                 struct fsnotify_event *event)
87 {
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;
95
96         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))
104                 return 0;
105         dnentry = container_of(entry, struct dnotify_mark_entry, fsn_entry);
106
107         spin_lock(&entry->lock);
108         prev = &dnentry->dn;
109         while ((dn = *prev) != NULL) {
110                 if ((dn->dn_mask & test_mask) == 0) {
111                         prev = &dn->dn_next;
112                         continue;
113                 }
114                 fown = &dn->dn_filp->f_owner;
115                 send_sigio(fown, dn->dn_fd, POLL_MSG);
116                 if (dn->dn_mask & FS_DN_MULTISHOT)
117                         prev = &dn->dn_next;
118                 else {
119                         *prev = dn->dn_next;
120                         kmem_cache_free(dnotify_struct_cache, dn);
121                         dnotify_recalc_inode_mask(entry);
122                 }
123         }
124
125         spin_unlock(&entry->lock);
126         fsnotify_put_mark(entry);
127
128         return 0;
129 }
130
131 /*
132  * Given an inode and mask determine if dnotify would be interested in sending
133  * userspace notification for that pair.
134  */
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)
138 {
139         struct fsnotify_mark *entry;
140         bool send;
141
142         /* !dir_notify_enable should never get here, don't waste time checking
143         if (!dir_notify_enable)
144                 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
158         mask = (mask & ~FS_EVENT_ON_CHILD);
159         send = (mask & entry->mask);
160
161         fsnotify_put_mark(entry); /* matches fsnotify_find_mark_entry */
162
163         return send;
164 }
165
166 static void dnotify_free_mark(struct fsnotify_mark *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
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,
183 };
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 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
190  * fsnotify_mark.
191  */
192 void dnotify_flush(struct file *filp, fl_owner_t id)
193 {
194         struct fsnotify_mark *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))
202                 return;
203
204         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;
223         }
224
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);
236 }
237
238 /* this conversion is done only at watch creation */
239 static __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_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.
266  */
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)
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  */
298 int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg)
299 {
300         struct dnotify_mark_entry *new_dnentry, *dnentry;
301         struct fsnotify_mark *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 {
366                 fsnotify_add_mark(new_entry, dnotify_group, inode, 0);
367                 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 marks 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);
413 out:
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);
423 out_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 }
430
431 static int __init dnotify_init(void)
432 {
433         dnotify_struct_cache = KMEM_CACHE(dnotify_struct, SLAB_PANIC);
434         dnotify_mark_entry_cache = KMEM_CACHE(dnotify_mark_entry, SLAB_PANIC);
435
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");
439         return 0;
440 }
441
442 module_init(dnotify_init)