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>
20 /* Notify this dentry's parent about a child's events. */
21 static inline int fsnotify_parent(const struct path *path, struct dentry *dentry, __u32 mask)
24 dentry = path->dentry;
26 return __fsnotify_parent(path, dentry, mask);
29 /* simple call site for access decisions */
30 static inline int fsnotify_perm(struct file *file, int mask)
32 const struct path *path = &file->f_path;
34 * Do not use file_inode() here or anywhere in this file to get the
35 * inode. That would break *notity on overlayfs.
37 struct inode *inode = path->dentry->d_inode;
38 __u32 fsnotify_mask = 0;
41 if (file->f_mode & FMODE_NONOTIFY)
43 if (!(mask & (MAY_READ | MAY_OPEN)))
46 fsnotify_mask = FS_OPEN_PERM;
47 else if (mask & MAY_READ)
48 fsnotify_mask = FS_ACCESS_PERM;
52 ret = fsnotify_parent(path, NULL, fsnotify_mask);
56 return fsnotify(inode, fsnotify_mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
60 * fsnotify_link_count - inode's link count changed
62 static inline void fsnotify_link_count(struct inode *inode)
64 fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
68 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
70 static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
71 const unsigned char *old_name,
72 int isdir, struct inode *target, struct dentry *moved)
74 struct inode *source = moved->d_inode;
75 u32 fs_cookie = fsnotify_get_cookie();
76 __u32 old_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_FROM);
77 __u32 new_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_TO);
78 const unsigned char *new_name = moved->d_name.name;
80 if (old_dir == new_dir)
81 old_dir_mask |= FS_DN_RENAME;
84 old_dir_mask |= FS_ISDIR;
85 new_dir_mask |= FS_ISDIR;
88 fsnotify(old_dir, old_dir_mask, source, FSNOTIFY_EVENT_INODE, old_name,
90 fsnotify(new_dir, new_dir_mask, source, FSNOTIFY_EVENT_INODE, new_name,
94 fsnotify_link_count(target);
97 fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0);
98 audit_inode_child(new_dir, moved, AUDIT_TYPE_CHILD_CREATE);
102 * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
104 static inline void fsnotify_inode_delete(struct inode *inode)
106 __fsnotify_inode_delete(inode);
110 * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
112 static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt)
114 __fsnotify_vfsmount_delete(mnt);
118 * fsnotify_nameremove - a filename was removed from a directory
120 static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
122 __u32 mask = FS_DELETE;
127 fsnotify_parent(NULL, dentry, mask);
131 * fsnotify_inoderemove - an inode is going away
133 static inline void fsnotify_inoderemove(struct inode *inode)
135 fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
136 __fsnotify_inode_delete(inode);
140 * fsnotify_create - 'name' was linked in
142 static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
144 audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
146 fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
150 * fsnotify_link - new hardlink in 'inode' directory
151 * Note: We have to pass also the linked inode ptr as some filesystems leave
152 * new_dentry->d_inode NULL and instantiate inode pointer later
154 static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry)
156 fsnotify_link_count(inode);
157 audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE);
159 fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name, 0);
163 * fsnotify_mkdir - directory 'name' was created
165 static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
167 __u32 mask = (FS_CREATE | FS_ISDIR);
168 struct inode *d_inode = dentry->d_inode;
170 audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
172 fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
176 * fsnotify_access - file was read
178 static inline void fsnotify_access(struct file *file)
180 const struct path *path = &file->f_path;
181 struct inode *inode = path->dentry->d_inode;
182 __u32 mask = FS_ACCESS;
184 if (S_ISDIR(inode->i_mode))
187 if (!(file->f_mode & FMODE_NONOTIFY)) {
188 fsnotify_parent(path, NULL, mask);
189 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
194 * fsnotify_modify - file was modified
196 static inline void fsnotify_modify(struct file *file)
198 const struct path *path = &file->f_path;
199 struct inode *inode = path->dentry->d_inode;
200 __u32 mask = FS_MODIFY;
202 if (S_ISDIR(inode->i_mode))
205 if (!(file->f_mode & FMODE_NONOTIFY)) {
206 fsnotify_parent(path, NULL, mask);
207 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
212 * fsnotify_open - file was opened
214 static inline void fsnotify_open(struct file *file)
216 const struct path *path = &file->f_path;
217 struct inode *inode = path->dentry->d_inode;
218 __u32 mask = FS_OPEN;
220 if (S_ISDIR(inode->i_mode))
223 fsnotify_parent(path, NULL, mask);
224 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
228 * fsnotify_close - file was closed
230 static inline void fsnotify_close(struct file *file)
232 const struct path *path = &file->f_path;
233 struct inode *inode = path->dentry->d_inode;
234 fmode_t mode = file->f_mode;
235 __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
237 if (S_ISDIR(inode->i_mode))
240 if (!(file->f_mode & FMODE_NONOTIFY)) {
241 fsnotify_parent(path, NULL, mask);
242 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
247 * fsnotify_xattr - extended attributes were changed
249 static inline void fsnotify_xattr(struct dentry *dentry)
251 struct inode *inode = dentry->d_inode;
252 __u32 mask = FS_ATTRIB;
254 if (S_ISDIR(inode->i_mode))
257 fsnotify_parent(NULL, dentry, mask);
258 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
262 * fsnotify_change - notify_change event. file was modified and/or metadata
265 static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
267 struct inode *inode = dentry->d_inode;
270 if (ia_valid & ATTR_UID)
272 if (ia_valid & ATTR_GID)
274 if (ia_valid & ATTR_SIZE)
277 /* both times implies a utime(s) call */
278 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
280 else if (ia_valid & ATTR_ATIME)
282 else if (ia_valid & ATTR_MTIME)
285 if (ia_valid & ATTR_MODE)
289 if (S_ISDIR(inode->i_mode))
292 fsnotify_parent(NULL, dentry, mask);
293 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
297 #endif /* _LINUX_FS_NOTIFY_H */