1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_FS_NOTIFY_H
3 #define _LINUX_FS_NOTIFY_H
6 * include/linux/fsnotify.h - generic hooks for filesystem notification, to
7 * reduce in-source duplication from both dnotify and inotify.
9 * We don't compile any of this away in some complicated menagerie of ifdefs.
10 * Instead, we rely on the code inside to optimize away as needed.
12 * (C) Copyright 2005 Robert Love
15 #include <linux/fsnotify_backend.h>
16 #include <linux/audit.h>
17 #include <linux/slab.h>
18 #include <linux/bug.h>
21 * Notify this @dir inode about a change in a child directory entry.
22 * The directory entry may have turned positive or negative or its inode may
23 * have changed (i.e. renamed over).
25 * Unlike fsnotify_parent(), the event will be reported regardless of the
26 * FS_EVENT_ON_CHILD mask on the parent inode and will not be reported if only
27 * the child is interested and not the parent.
29 static inline int fsnotify_name(__u32 mask, const void *data, int data_type,
30 struct inode *dir, const struct qstr *name,
33 if (atomic_long_read(&dir->i_sb->s_fsnotify_connectors) == 0)
36 return fsnotify(mask, data, data_type, dir, name, NULL, cookie);
39 static inline void fsnotify_dirent(struct inode *dir, struct dentry *dentry,
42 fsnotify_name(mask, dentry, FSNOTIFY_EVENT_DENTRY, dir, &dentry->d_name, 0);
45 static inline void fsnotify_inode(struct inode *inode, __u32 mask)
47 if (atomic_long_read(&inode->i_sb->s_fsnotify_connectors) == 0)
50 if (S_ISDIR(inode->i_mode))
53 fsnotify(mask, inode, FSNOTIFY_EVENT_INODE, NULL, NULL, inode, 0);
56 /* Notify this dentry's parent about a child's events. */
57 static inline int fsnotify_parent(struct dentry *dentry, __u32 mask,
58 const void *data, int data_type)
60 struct inode *inode = d_inode(dentry);
62 if (atomic_long_read(&inode->i_sb->s_fsnotify_connectors) == 0)
65 if (S_ISDIR(inode->i_mode)) {
68 /* sb/mount marks are not interested in name of directory */
69 if (!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED))
73 /* disconnected dentry cannot notify parent */
77 return __fsnotify_parent(dentry, mask, data, data_type);
80 return fsnotify(mask, data, data_type, NULL, NULL, inode, 0);
84 * Simple wrappers to consolidate calls to fsnotify_parent() when an event
85 * is on a file/dentry.
87 static inline void fsnotify_dentry(struct dentry *dentry, __u32 mask)
89 fsnotify_parent(dentry, mask, dentry, FSNOTIFY_EVENT_DENTRY);
92 static inline int fsnotify_file(struct file *file, __u32 mask)
94 const struct path *path;
96 if (file->f_mode & FMODE_NONOTIFY)
99 /* Overlayfs internal files have fake f_path */
100 path = file_real_path(file);
101 return fsnotify_parent(path->dentry, mask, path, FSNOTIFY_EVENT_PATH);
104 /* Simple call site for access decisions */
105 static inline int fsnotify_perm(struct file *file, int mask)
108 __u32 fsnotify_mask = 0;
110 if (!(mask & (MAY_READ | MAY_OPEN)))
113 if (mask & MAY_OPEN) {
114 fsnotify_mask = FS_OPEN_PERM;
116 if (file->f_flags & __FMODE_EXEC) {
117 ret = fsnotify_file(file, FS_OPEN_EXEC_PERM);
122 } else if (mask & MAY_READ) {
123 fsnotify_mask = FS_ACCESS_PERM;
126 return fsnotify_file(file, fsnotify_mask);
130 * fsnotify_link_count - inode's link count changed
132 static inline void fsnotify_link_count(struct inode *inode)
134 fsnotify_inode(inode, FS_ATTRIB);
138 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
140 static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
141 const struct qstr *old_name,
142 int isdir, struct inode *target,
143 struct dentry *moved)
145 struct inode *source = moved->d_inode;
146 u32 fs_cookie = fsnotify_get_cookie();
147 __u32 old_dir_mask = FS_MOVED_FROM;
148 __u32 new_dir_mask = FS_MOVED_TO;
149 __u32 rename_mask = FS_RENAME;
150 const struct qstr *new_name = &moved->d_name;
153 old_dir_mask |= FS_ISDIR;
154 new_dir_mask |= FS_ISDIR;
155 rename_mask |= FS_ISDIR;
158 /* Event with information about both old and new parent+name */
159 fsnotify_name(rename_mask, moved, FSNOTIFY_EVENT_DENTRY,
160 old_dir, old_name, 0);
162 fsnotify_name(old_dir_mask, source, FSNOTIFY_EVENT_INODE,
163 old_dir, old_name, fs_cookie);
164 fsnotify_name(new_dir_mask, source, FSNOTIFY_EVENT_INODE,
165 new_dir, new_name, fs_cookie);
168 fsnotify_link_count(target);
169 fsnotify_inode(source, FS_MOVE_SELF);
170 audit_inode_child(new_dir, moved, AUDIT_TYPE_CHILD_CREATE);
174 * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
176 static inline void fsnotify_inode_delete(struct inode *inode)
178 __fsnotify_inode_delete(inode);
182 * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
184 static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt)
186 __fsnotify_vfsmount_delete(mnt);
190 * fsnotify_inoderemove - an inode is going away
192 static inline void fsnotify_inoderemove(struct inode *inode)
194 fsnotify_inode(inode, FS_DELETE_SELF);
195 __fsnotify_inode_delete(inode);
199 * fsnotify_create - 'name' was linked in
201 * Caller must make sure that dentry->d_name is stable.
202 * Note: some filesystems (e.g. kernfs) leave @dentry negative and instantiate
205 static inline void fsnotify_create(struct inode *dir, struct dentry *dentry)
207 audit_inode_child(dir, dentry, AUDIT_TYPE_CHILD_CREATE);
209 fsnotify_dirent(dir, dentry, FS_CREATE);
213 * fsnotify_link - new hardlink in 'inode' directory
215 * Caller must make sure that new_dentry->d_name is stable.
216 * Note: We have to pass also the linked inode ptr as some filesystems leave
217 * new_dentry->d_inode NULL and instantiate inode pointer later
219 static inline void fsnotify_link(struct inode *dir, struct inode *inode,
220 struct dentry *new_dentry)
222 fsnotify_link_count(inode);
223 audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE);
225 fsnotify_name(FS_CREATE, inode, FSNOTIFY_EVENT_INODE,
226 dir, &new_dentry->d_name, 0);
230 * fsnotify_delete - @dentry was unlinked and unhashed
232 * Caller must make sure that dentry->d_name is stable.
234 * Note: unlike fsnotify_unlink(), we have to pass also the unlinked inode
235 * as this may be called after d_delete() and old_dentry may be negative.
237 static inline void fsnotify_delete(struct inode *dir, struct inode *inode,
238 struct dentry *dentry)
240 __u32 mask = FS_DELETE;
242 if (S_ISDIR(inode->i_mode))
245 fsnotify_name(mask, inode, FSNOTIFY_EVENT_INODE, dir, &dentry->d_name,
250 * d_delete_notify - delete a dentry and call fsnotify_delete()
251 * @dentry: The dentry to delete
253 * This helper is used to guaranty that the unlinked inode cannot be found
254 * by lookup of this name after fsnotify_delete() event has been delivered.
256 static inline void d_delete_notify(struct inode *dir, struct dentry *dentry)
258 struct inode *inode = d_inode(dentry);
262 fsnotify_delete(dir, inode, dentry);
267 * fsnotify_unlink - 'name' was unlinked
269 * Caller must make sure that dentry->d_name is stable.
271 static inline void fsnotify_unlink(struct inode *dir, struct dentry *dentry)
273 if (WARN_ON_ONCE(d_is_negative(dentry)))
276 fsnotify_delete(dir, d_inode(dentry), dentry);
280 * fsnotify_mkdir - directory 'name' was created
282 * Caller must make sure that dentry->d_name is stable.
283 * Note: some filesystems (e.g. kernfs) leave @dentry negative and instantiate
286 static inline void fsnotify_mkdir(struct inode *dir, struct dentry *dentry)
288 audit_inode_child(dir, dentry, AUDIT_TYPE_CHILD_CREATE);
290 fsnotify_dirent(dir, dentry, FS_CREATE | FS_ISDIR);
294 * fsnotify_rmdir - directory 'name' was removed
296 * Caller must make sure that dentry->d_name is stable.
298 static inline void fsnotify_rmdir(struct inode *dir, struct dentry *dentry)
300 if (WARN_ON_ONCE(d_is_negative(dentry)))
303 fsnotify_delete(dir, d_inode(dentry), dentry);
307 * fsnotify_access - file was read
309 static inline void fsnotify_access(struct file *file)
311 fsnotify_file(file, FS_ACCESS);
315 * fsnotify_modify - file was modified
317 static inline void fsnotify_modify(struct file *file)
319 fsnotify_file(file, FS_MODIFY);
323 * fsnotify_open - file was opened
325 static inline void fsnotify_open(struct file *file)
327 __u32 mask = FS_OPEN;
329 if (file->f_flags & __FMODE_EXEC)
330 mask |= FS_OPEN_EXEC;
332 fsnotify_file(file, mask);
336 * fsnotify_close - file was closed
338 static inline void fsnotify_close(struct file *file)
340 __u32 mask = (file->f_mode & FMODE_WRITE) ? FS_CLOSE_WRITE :
343 fsnotify_file(file, mask);
347 * fsnotify_xattr - extended attributes were changed
349 static inline void fsnotify_xattr(struct dentry *dentry)
351 fsnotify_dentry(dentry, FS_ATTRIB);
355 * fsnotify_change - notify_change event. file was modified and/or metadata
358 static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
362 if (ia_valid & ATTR_UID)
364 if (ia_valid & ATTR_GID)
366 if (ia_valid & ATTR_SIZE)
369 /* both times implies a utime(s) call */
370 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
372 else if (ia_valid & ATTR_ATIME)
374 else if (ia_valid & ATTR_MTIME)
377 if (ia_valid & ATTR_MODE)
381 fsnotify_dentry(dentry, mask);
384 static inline int fsnotify_sb_error(struct super_block *sb, struct inode *inode,
387 struct fs_error_report report = {
393 return fsnotify(FS_ERROR, &report, FSNOTIFY_EVENT_ERROR,
394 NULL, NULL, NULL, 0);
397 #endif /* _LINUX_FS_NOTIFY_H */