1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Directory notifications for Linux.
5 * Copyright (C) 2000,2001,2002 Stephen Rothwell
7 * Copyright (C) 2009 Eric Paris <Red Hat Inc>
8 * dnotify was largly rewritten to use the new fsnotify infrastructure
11 #include <linux/module.h>
12 #include <linux/sched.h>
13 #include <linux/sched/signal.h>
14 #include <linux/dnotify.h>
15 #include <linux/init.h>
16 #include <linux/security.h>
17 #include <linux/spinlock.h>
18 #include <linux/slab.h>
19 #include <linux/fdtable.h>
20 #include <linux/fsnotify_backend.h>
22 static int dir_notify_enable __read_mostly = 1;
24 static struct ctl_table dnotify_sysctls[] = {
26 .procname = "dir-notify-enable",
27 .data = &dir_notify_enable,
28 .maxlen = sizeof(int),
30 .proc_handler = proc_dointvec,
34 static void __init dnotify_sysctl_init(void)
36 register_sysctl_init("fs", dnotify_sysctls);
39 #define dnotify_sysctl_init() do { } while (0)
42 static struct kmem_cache *dnotify_struct_cache __ro_after_init;
43 static struct kmem_cache *dnotify_mark_cache __ro_after_init;
44 static struct fsnotify_group *dnotify_group __ro_after_init;
47 * dnotify will attach one of these to each inode (i_fsnotify_marks) which
48 * is being watched by dnotify. If multiple userspace applications are watching
49 * the same directory with dnotify their information is chained in dn
52 struct fsnotify_mark fsn_mark;
53 struct dnotify_struct *dn;
57 * When a process starts or stops watching an inode the set of events which
58 * dnotify cares about for that inode may change. This function runs the
59 * list of everything receiving dnotify events about this directory and calculates
60 * the set of all those events. After it updates what dnotify is interested in
61 * it calls the fsnotify function so it can update the set of all events relevant
64 static void dnotify_recalc_inode_mask(struct fsnotify_mark *fsn_mark)
67 struct dnotify_struct *dn;
68 struct dnotify_mark *dn_mark = container_of(fsn_mark,
72 assert_spin_locked(&fsn_mark->lock);
74 for (dn = dn_mark->dn; dn != NULL; dn = dn->dn_next)
75 new_mask |= (dn->dn_mask & ~FS_DN_MULTISHOT);
76 if (fsn_mark->mask == new_mask)
78 fsn_mark->mask = new_mask;
80 fsnotify_recalc_mask(fsn_mark->connector);
84 * Mains fsnotify call where events are delivered to dnotify.
85 * Find the dnotify mark on the relevant inode, run the list of dnotify structs
86 * on that mark and determine which of them has expressed interest in receiving
87 * events of this type. When found send the correct process and signal and
88 * destroy the dnotify struct if it was not registered to receive multiple
91 static int dnotify_handle_event(struct fsnotify_mark *inode_mark, u32 mask,
92 struct inode *inode, struct inode *dir,
93 const struct qstr *name, u32 cookie)
95 struct dnotify_mark *dn_mark;
96 struct dnotify_struct *dn;
97 struct dnotify_struct **prev;
98 struct fown_struct *fown;
99 __u32 test_mask = mask & ~FS_EVENT_ON_CHILD;
101 /* not a dir, dnotify doesn't care */
102 if (!dir && !(mask & FS_ISDIR))
105 dn_mark = container_of(inode_mark, struct dnotify_mark, fsn_mark);
107 spin_lock(&inode_mark->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(inode_mark);
125 spin_unlock(&inode_mark->lock);
130 static void dnotify_free_mark(struct fsnotify_mark *fsn_mark)
132 struct dnotify_mark *dn_mark = container_of(fsn_mark,
138 kmem_cache_free(dnotify_mark_cache, dn_mark);
141 static const struct fsnotify_ops dnotify_fsnotify_ops = {
142 .handle_inode_event = dnotify_handle_event,
143 .free_mark = dnotify_free_mark,
147 * Called every time a file is closed. Looks first for a dnotify mark on the
148 * inode. If one is found run all of the ->dn structures attached to that
149 * mark for one relevant to this process closing the file and remove that
150 * dnotify_struct. If that was the last dnotify_struct also remove the
153 void dnotify_flush(struct file *filp, fl_owner_t id)
155 struct fsnotify_mark *fsn_mark;
156 struct dnotify_mark *dn_mark;
157 struct dnotify_struct *dn;
158 struct dnotify_struct **prev;
162 inode = file_inode(filp);
163 if (!S_ISDIR(inode->i_mode))
166 fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, dnotify_group);
169 dn_mark = container_of(fsn_mark, struct dnotify_mark, fsn_mark);
171 fsnotify_group_lock(dnotify_group);
173 spin_lock(&fsn_mark->lock);
175 while ((dn = *prev) != NULL) {
176 if ((dn->dn_owner == id) && (dn->dn_filp == filp)) {
178 kmem_cache_free(dnotify_struct_cache, dn);
179 dnotify_recalc_inode_mask(fsn_mark);
185 spin_unlock(&fsn_mark->lock);
187 /* nothing else could have found us thanks to the dnotify_groups
189 if (dn_mark->dn == NULL) {
190 fsnotify_detach_mark(fsn_mark);
194 fsnotify_group_unlock(dnotify_group);
197 fsnotify_free_mark(fsn_mark);
198 fsnotify_put_mark(fsn_mark);
201 /* this conversion is done only at watch creation */
202 static __u32 convert_arg(unsigned int arg)
204 __u32 new_mask = FS_EVENT_ON_CHILD;
206 if (arg & DN_MULTISHOT)
207 new_mask |= FS_DN_MULTISHOT;
209 new_mask |= (FS_DELETE | FS_MOVED_FROM);
211 new_mask |= FS_MODIFY;
213 new_mask |= FS_ACCESS;
215 new_mask |= FS_ATTRIB;
217 new_mask |= FS_RENAME;
219 new_mask |= (FS_CREATE | FS_MOVED_TO);
225 * If multiple processes watch the same inode with dnotify there is only one
226 * dnotify mark in inode->i_fsnotify_marks but we chain a dnotify_struct
227 * onto that mark. This function either attaches the new dnotify_struct onto
228 * that list, or it |= the mask onto an existing dnofiy_struct.
230 static int attach_dn(struct dnotify_struct *dn, struct dnotify_mark *dn_mark,
231 fl_owner_t id, int fd, struct file *filp, __u32 mask)
233 struct dnotify_struct *odn;
236 while (odn != NULL) {
237 /* adding more events to existing dnofiy_struct? */
238 if ((odn->dn_owner == id) && (odn->dn_filp == filp)) {
240 odn->dn_mask |= mask;
250 dn->dn_next = dn_mark->dn;
257 * When a process calls fcntl to attach a dnotify watch to a directory it ends
258 * up here. Allocate both a mark for fsnotify to add and a dnotify_struct to be
259 * attached to the fsnotify_mark.
261 int fcntl_dirnotify(int fd, struct file *filp, unsigned int arg)
263 struct dnotify_mark *new_dn_mark, *dn_mark;
264 struct fsnotify_mark *new_fsn_mark, *fsn_mark;
265 struct dnotify_struct *dn;
267 fl_owner_t id = current->files;
268 struct file *f = NULL;
269 int destroy = 0, error = 0;
272 /* we use these to tell if we need to kfree */
276 if (!dir_notify_enable) {
281 /* a 0 mask means we are explicitly removing the watch */
282 if ((arg & ~DN_MULTISHOT) == 0) {
283 dnotify_flush(filp, id);
288 /* dnotify only works on directories */
289 inode = file_inode(filp);
290 if (!S_ISDIR(inode->i_mode)) {
296 * convert the userspace DN_* "arg" to the internal FS_*
297 * defined in fsnotify
299 mask = convert_arg(arg);
301 error = security_path_notify(&filp->f_path, mask,
302 FSNOTIFY_OBJ_TYPE_INODE);
306 /* expect most fcntl to add new rather than augment old */
307 dn = kmem_cache_alloc(dnotify_struct_cache, GFP_KERNEL);
313 /* new fsnotify mark, we expect most fcntl calls to add a new mark */
314 new_dn_mark = kmem_cache_alloc(dnotify_mark_cache, GFP_KERNEL);
320 /* set up the new_fsn_mark and new_dn_mark */
321 new_fsn_mark = &new_dn_mark->fsn_mark;
322 fsnotify_init_mark(new_fsn_mark, dnotify_group);
323 new_fsn_mark->mask = mask;
324 new_dn_mark->dn = NULL;
326 /* this is needed to prevent the fcntl/close race described below */
327 fsnotify_group_lock(dnotify_group);
329 /* add the new_fsn_mark or find an old one. */
330 fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, dnotify_group);
332 dn_mark = container_of(fsn_mark, struct dnotify_mark, fsn_mark);
333 spin_lock(&fsn_mark->lock);
335 error = fsnotify_add_inode_mark_locked(new_fsn_mark, inode, 0);
337 fsnotify_group_unlock(dnotify_group);
340 spin_lock(&new_fsn_mark->lock);
341 fsn_mark = new_fsn_mark;
342 dn_mark = new_dn_mark;
343 /* we used new_fsn_mark, so don't free it */
348 f = lookup_fdget_rcu(fd);
351 /* if (f != filp) means that we lost a race and another task/thread
352 * actually closed the fd we are still playing with before we grabbed
353 * the dnotify_groups mark_mutex and fsn_mark->lock. Since closing the
354 * fd is the only time we clean up the marks we need to get our mark
357 /* if we added ourselves, shoot ourselves, it's possible that
358 * the flush actually did shoot this fsn_mark. That's fine too
359 * since multiple calls to destroy_mark is perfectly safe, if
360 * we found a dn_mark already attached to the inode, just sod
361 * off silently as the flush at close time dealt with it.
363 if (dn_mark == new_dn_mark)
369 __f_setown(filp, task_pid(current), PIDTYPE_TGID, 0);
371 error = attach_dn(dn, dn_mark, id, fd, filp, mask);
372 /* !error means that we attached the dn to the dn_mark, so don't free it */
375 /* -EEXIST means that we didn't add this new dn and used an old one.
376 * that isn't an error (and the unused dn should be freed) */
377 else if (error == -EEXIST)
380 dnotify_recalc_inode_mask(fsn_mark);
382 spin_unlock(&fsn_mark->lock);
385 fsnotify_detach_mark(fsn_mark);
386 fsnotify_group_unlock(dnotify_group);
388 fsnotify_free_mark(fsn_mark);
389 fsnotify_put_mark(fsn_mark);
392 fsnotify_put_mark(new_fsn_mark);
394 kmem_cache_free(dnotify_struct_cache, dn);
400 static int __init dnotify_init(void)
402 dnotify_struct_cache = KMEM_CACHE(dnotify_struct,
403 SLAB_PANIC|SLAB_ACCOUNT);
404 dnotify_mark_cache = KMEM_CACHE(dnotify_mark, SLAB_PANIC|SLAB_ACCOUNT);
406 dnotify_group = fsnotify_alloc_group(&dnotify_fsnotify_ops,
407 FSNOTIFY_GROUP_NOFS);
408 if (IS_ERR(dnotify_group))
409 panic("unable to allocate fsnotify group for dnotify\n");
410 dnotify_sysctl_init();
414 module_init(dnotify_init)