]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/notify/dnotify/dnotify.c
dnotify: reimplement dnotify using fsnotify
[mirror_ubuntu-artful-kernel.git] / fs / notify / dnotify / dnotify.c
CommitLineData
1da177e4
LT
1/*
2 * Directory notifications for Linux.
3 *
4 * Copyright (C) 2000,2001,2002 Stephen Rothwell
5 *
3c5119c0
EP
6 * Copyright (C) 2009 Eric Paris <Red Hat Inc>
7 * dnotify was largly rewritten to use the new fsnotify infrastructure
8 *
1da177e4
LT
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>
9f3acc31 26#include <linux/fdtable.h>
3c5119c0 27#include <linux/fsnotify_backend.h>
1da177e4 28
fa3536cc 29int dir_notify_enable __read_mostly = 1;
1da177e4 30
3c5119c0
EP
31static 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);
35
36/*
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;
43 struct dnotify_struct *dn;
44};
1da177e4 45
3c5119c0
EP
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 */
54static void dnotify_recalc_inode_mask(struct fsnotify_mark_entry *entry)
1da177e4 55{
3c5119c0 56 __u32 new_mask, old_mask;
1da177e4 57 struct dnotify_struct *dn;
3c5119c0
EP
58 struct dnotify_mark_entry *dnentry = container_of(entry,
59 struct dnotify_mark_entry,
60 fsn_entry);
61
62 assert_spin_locked(&entry->lock);
1da177e4 63
3c5119c0 64 old_mask = entry->mask;
1da177e4 65 new_mask = 0;
3c5119c0
EP
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->inode)
74 fsnotify_recalc_inode_mask(entry->inode);
1da177e4
LT
75}
76
3c5119c0
EP
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 */
85static int dnotify_handle_event(struct fsnotify_group *group,
86 struct fsnotify_event *event)
87{
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;
94
95 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))
103 return 0;
104 dnentry = container_of(entry, struct dnotify_mark_entry, fsn_entry);
105
106 spin_lock(&entry->lock);
107 prev = &dnentry->dn;
108 while ((dn = *prev) != NULL) {
109 if ((dn->dn_mask & event->mask) == 0) {
110 prev = &dn->dn_next;
111 continue;
112 }
113 fown = &dn->dn_filp->f_owner;
114 send_sigio(fown, dn->dn_fd, POLL_MSG);
115 if (dn->dn_mask & FS_DN_MULTISHOT)
116 prev = &dn->dn_next;
117 else {
118 *prev = dn->dn_next;
119 kmem_cache_free(dnotify_struct_cache, dn);
120 dnotify_recalc_inode_mask(entry);
121 }
122 }
123
124 spin_unlock(&entry->lock);
125 fsnotify_put_mark(entry);
126
127 return 0;
128}
129
130/*
131 * Given an inode and mask determine if dnotify would be interested in sending
132 * userspace notification for that pair.
133 */
134static bool dnotify_should_send_event(struct fsnotify_group *group,
135 struct inode *inode, __u32 mask)
136{
137 struct fsnotify_mark_entry *entry;
138 bool send;
139
140 /* !dir_notify_enable should never get here, don't waste time checking
141 if (!dir_notify_enable)
142 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
156 spin_lock(&entry->lock);
157 send = (mask & entry->mask) ? true : false;
158 spin_unlock(&entry->lock);
159 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,
186};
187
188/*
189 * Called every time a file is closed. Looks first for a dnotify mark on the
190 * inode. If one is found run all of the ->dn entries attached to that
191 * mark for one relevant to this process closing the file and remove that
192 * dnotify_struct. If that was the last dnotify_struct also remove the
193 * fsnotify_mark_entry.
194 */
1da177e4
LT
195void dnotify_flush(struct file *filp, fl_owner_t id)
196{
3c5119c0
EP
197 struct fsnotify_mark_entry *entry;
198 struct dnotify_mark_entry *dnentry;
1da177e4
LT
199 struct dnotify_struct *dn;
200 struct dnotify_struct **prev;
201 struct inode *inode;
202
0f7fc9e4 203 inode = filp->f_path.dentry->d_inode;
1da177e4
LT
204 if (!S_ISDIR(inode->i_mode))
205 return;
3c5119c0 206
1da177e4 207 spin_lock(&inode->i_lock);
3c5119c0
EP
208 entry = fsnotify_find_mark_entry(dnotify_group, inode);
209 spin_unlock(&inode->i_lock);
210 if (!entry)
211 return;
212 dnentry = container_of(entry, struct dnotify_mark_entry, fsn_entry);
213
214 mutex_lock(&dnotify_mark_mutex);
215
216 spin_lock(&entry->lock);
217 prev = &dnentry->dn;
1da177e4
LT
218 while ((dn = *prev) != NULL) {
219 if ((dn->dn_owner == id) && (dn->dn_filp == filp)) {
220 *prev = dn->dn_next;
3c5119c0
EP
221 kmem_cache_free(dnotify_struct_cache, dn);
222 dnotify_recalc_inode_mask(entry);
1da177e4
LT
223 break;
224 }
225 prev = &dn->dn_next;
226 }
3c5119c0
EP
227
228 spin_unlock(&entry->lock);
229
230 /* nothing else could have found us thanks to the dnotify_mark_mutex */
231 if (dnentry->dn == NULL)
232 fsnotify_destroy_mark_by_entry(entry);
233
234 fsnotify_recalc_group_mask(dnotify_group);
235
236 mutex_unlock(&dnotify_mark_mutex);
237
238 fsnotify_put_mark(entry);
239}
240
241/* this conversion is done only at watch creation */
242static __u32 convert_arg(unsigned long arg)
243{
244 __u32 new_mask = FS_EVENT_ON_CHILD;
245
246 if (arg & DN_MULTISHOT)
247 new_mask |= FS_DN_MULTISHOT;
248 if (arg & DN_DELETE)
249 new_mask |= (FS_DELETE | FS_MOVED_FROM);
250 if (arg & DN_MODIFY)
251 new_mask |= FS_MODIFY;
252 if (arg & DN_ACCESS)
253 new_mask |= FS_ACCESS;
254 if (arg & DN_ATTRIB)
255 new_mask |= FS_ATTRIB;
256 if (arg & DN_RENAME)
257 new_mask |= FS_DN_RENAME;
258 if (arg & DN_CREATE)
259 new_mask |= (FS_CREATE | FS_MOVED_TO);
260
261 return new_mask;
1da177e4
LT
262}
263
3c5119c0
EP
264/*
265 * If multiple processes watch the same inode with dnotify there is only one
266 * dnotify mark in inode->i_fsnotify_mark_entries but we chain a dnotify_struct
267 * onto that mark. This function either attaches the new dnotify_struct onto
268 * that list, or it |= the mask onto an existing dnofiy_struct.
269 */
270static int attach_dn(struct dnotify_struct *dn, struct dnotify_mark_entry *dnentry,
271 fl_owner_t id, int fd, struct file *filp, __u32 mask)
272{
273 struct dnotify_struct *odn;
274
275 odn = dnentry->dn;
276 while (odn != NULL) {
277 /* adding more events to existing dnofiy_struct? */
278 if ((odn->dn_owner == id) && (odn->dn_filp == filp)) {
279 odn->dn_fd = fd;
280 odn->dn_mask |= mask;
281 return -EEXIST;
282 }
283 odn = odn->dn_next;
284 }
285
286 dn->dn_mask = mask;
287 dn->dn_fd = fd;
288 dn->dn_filp = filp;
289 dn->dn_owner = id;
290 dn->dn_next = dnentry->dn;
291 dnentry->dn = dn;
292
293 return 0;
294}
295
296/*
297 * When a process calls fcntl to attach a dnotify watch to a directory it ends
298 * up here. Allocate both a mark for fsnotify to add and a dnotify_struct to be
299 * attached to the fsnotify_mark.
300 */
1da177e4
LT
301int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg)
302{
3c5119c0
EP
303 struct dnotify_mark_entry *new_dnentry, *dnentry;
304 struct fsnotify_mark_entry *new_entry, *entry;
1da177e4 305 struct dnotify_struct *dn;
1da177e4
LT
306 struct inode *inode;
307 fl_owner_t id = current->files;
214b7049 308 struct file *f;
3c5119c0
EP
309 int destroy = 0, error = 0;
310 __u32 mask;
311
312 /* we use these to tell if we need to kfree */
313 new_entry = NULL;
314 dn = NULL;
1da177e4 315
3c5119c0
EP
316 if (!dir_notify_enable) {
317 error = -EINVAL;
318 goto out_err;
319 }
320
321 /* a 0 mask means we are explicitly removing the watch */
1da177e4
LT
322 if ((arg & ~DN_MULTISHOT) == 0) {
323 dnotify_flush(filp, id);
3c5119c0
EP
324 error = 0;
325 goto out_err;
1da177e4 326 }
3c5119c0
EP
327
328 /* dnotify only works on directories */
0f7fc9e4 329 inode = filp->f_path.dentry->d_inode;
3c5119c0
EP
330 if (!S_ISDIR(inode->i_mode)) {
331 error = -ENOTDIR;
332 goto out_err;
1da177e4
LT
333 }
334
3c5119c0
EP
335 /* expect most fcntl to add new rather than augment old */
336 dn = kmem_cache_alloc(dnotify_struct_cache, GFP_KERNEL);
337 if (!dn) {
338 error = -ENOMEM;
339 goto out_err;
340 }
214b7049 341
3c5119c0
EP
342 /* new fsnotify mark, we expect most fcntl calls to add a new mark */
343 new_dnentry = kmem_cache_alloc(dnotify_mark_entry_cache, GFP_KERNEL);
344 if (!new_dnentry) {
345 error = -ENOMEM;
346 goto out_err;
347 }
1da177e4 348
3c5119c0
EP
349 /* convert the userspace DN_* "arg" to the internal FS_* defines in fsnotify */
350 mask = convert_arg(arg);
1da177e4 351
3c5119c0
EP
352 /* set up the new_entry and new_dnentry */
353 new_entry = &new_dnentry->fsn_entry;
354 fsnotify_init_mark(new_entry, dnotify_free_mark);
355 new_entry->mask = mask;
356 new_dnentry->dn = NULL;
1da177e4 357
3c5119c0
EP
358 /* this is needed to prevent the fcntl/close race described below */
359 mutex_lock(&dnotify_mark_mutex);
1da177e4 360
3c5119c0 361 /* add the new_entry or find an old one. */
1da177e4 362 spin_lock(&inode->i_lock);
3c5119c0 363 entry = fsnotify_find_mark_entry(dnotify_group, inode);
1da177e4 364 spin_unlock(&inode->i_lock);
3c5119c0
EP
365 if (entry) {
366 dnentry = container_of(entry, struct dnotify_mark_entry, fsn_entry);
367 spin_lock(&entry->lock);
368 } else {
369 fsnotify_add_mark(new_entry, dnotify_group, inode);
370 spin_lock(&new_entry->lock);
371 entry = new_entry;
372 dnentry = new_dnentry;
373 /* we used new_entry, so don't free it */
374 new_entry = NULL;
375 }
1da177e4 376
3c5119c0
EP
377 rcu_read_lock();
378 f = fcheck(fd);
379 rcu_read_unlock();
1da177e4 380
3c5119c0
EP
381 /* if (f != filp) means that we lost a race and another task/thread
382 * actually closed the fd we are still playing with before we grabbed
383 * the dnotify_mark_mutex and entry->lock. Since closing the fd is the
384 * only time we clean up the mark entries we need to get our mark off
385 * the list. */
386 if (f != filp) {
387 /* if we added ourselves, shoot ourselves, it's possible that
388 * the flush actually did shoot this entry. That's fine too
389 * since multiple calls to destroy_mark is perfectly safe, if
390 * we found a dnentry already attached to the inode, just sod
391 * off silently as the flush at close time dealt with it.
392 */
393 if (dnentry == new_dnentry)
394 destroy = 1;
395 goto out;
396 }
1da177e4 397
3c5119c0
EP
398 error = __f_setown(filp, task_pid(current), PIDTYPE_PID, 0);
399 if (error) {
400 /* if we added, we must shoot */
401 if (dnentry == new_dnentry)
402 destroy = 1;
403 goto out;
1da177e4 404 }
3c5119c0
EP
405
406 error = attach_dn(dn, dnentry, id, fd, filp, mask);
407 /* !error means that we attached the dn to the dnentry, so don't free it */
408 if (!error)
409 dn = NULL;
410 /* -EEXIST means that we didn't add this new dn and used an old one.
411 * that isn't an error (and the unused dn should be freed) */
412 else if (error == -EEXIST)
413 error = 0;
414
415 dnotify_recalc_inode_mask(entry);
416out:
417 spin_unlock(&entry->lock);
418
419 if (destroy)
420 fsnotify_destroy_mark_by_entry(entry);
421
422 fsnotify_recalc_group_mask(dnotify_group);
423
424 mutex_unlock(&dnotify_mark_mutex);
425 fsnotify_put_mark(entry);
426out_err:
427 if (new_entry)
428 fsnotify_put_mark(new_entry);
429 if (dn)
430 kmem_cache_free(dnotify_struct_cache, dn);
431 return error;
1da177e4 432}
1da177e4
LT
433
434static int __init dnotify_init(void)
435{
3c5119c0
EP
436 dnotify_struct_cache = KMEM_CACHE(dnotify_struct, SLAB_PANIC);
437 dnotify_mark_entry_cache = KMEM_CACHE(dnotify_mark_entry, SLAB_PANIC);
438
439 dnotify_group = fsnotify_obtain_group(DNOTIFY_GROUP_NUM,
440 0, &dnotify_fsnotify_ops);
441 if (IS_ERR(dnotify_group))
442 panic("unable to allocate fsnotify group for dnotify\n");
1da177e4
LT
443 return 0;
444}
445
446module_init(dnotify_init)