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);
30 * Simple wrapper to consolidate calls fsnotify_parent()/fsnotify() when
31 * an event is on a path.
33 static inline int fsnotify_path(struct inode *inode, const struct path *path,
36 int ret = fsnotify_parent(path, NULL, mask);
40 return fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
43 /* Simple call site for access decisions */
44 static inline int fsnotify_perm(struct file *file, int mask)
47 const struct path *path = &file->f_path;
48 struct inode *inode = file_inode(file);
49 __u32 fsnotify_mask = 0;
51 if (file->f_mode & FMODE_NONOTIFY)
53 if (!(mask & (MAY_READ | MAY_OPEN)))
55 if (mask & MAY_OPEN) {
56 fsnotify_mask = FS_OPEN_PERM;
58 if (file->f_flags & __FMODE_EXEC) {
59 ret = fsnotify_path(inode, path, FS_OPEN_EXEC_PERM);
64 } else if (mask & MAY_READ) {
65 fsnotify_mask = FS_ACCESS_PERM;
68 return fsnotify_path(inode, path, fsnotify_mask);
72 * fsnotify_link_count - inode's link count changed
74 static inline void fsnotify_link_count(struct inode *inode)
76 fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
80 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
82 static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
83 const unsigned char *old_name,
84 int isdir, struct inode *target, struct dentry *moved)
86 struct inode *source = moved->d_inode;
87 u32 fs_cookie = fsnotify_get_cookie();
88 __u32 old_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_FROM);
89 __u32 new_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_TO);
90 const unsigned char *new_name = moved->d_name.name;
92 if (old_dir == new_dir)
93 old_dir_mask |= FS_DN_RENAME;
96 old_dir_mask |= FS_ISDIR;
97 new_dir_mask |= FS_ISDIR;
100 fsnotify(old_dir, old_dir_mask, source, FSNOTIFY_EVENT_INODE, old_name,
102 fsnotify(new_dir, new_dir_mask, source, FSNOTIFY_EVENT_INODE, new_name,
106 fsnotify_link_count(target);
109 fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0);
110 audit_inode_child(new_dir, moved, AUDIT_TYPE_CHILD_CREATE);
114 * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
116 static inline void fsnotify_inode_delete(struct inode *inode)
118 __fsnotify_inode_delete(inode);
122 * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
124 static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt)
126 __fsnotify_vfsmount_delete(mnt);
130 * fsnotify_nameremove - a filename was removed from a directory
132 static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
134 __u32 mask = FS_DELETE;
139 fsnotify_parent(NULL, dentry, mask);
143 * fsnotify_inoderemove - an inode is going away
145 static inline void fsnotify_inoderemove(struct inode *inode)
147 fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
148 __fsnotify_inode_delete(inode);
152 * fsnotify_create - 'name' was linked in
154 static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
156 audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
158 fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
162 * fsnotify_link - new hardlink in 'inode' directory
163 * Note: We have to pass also the linked inode ptr as some filesystems leave
164 * new_dentry->d_inode NULL and instantiate inode pointer later
166 static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry)
168 fsnotify_link_count(inode);
169 audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE);
171 fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name, 0);
175 * fsnotify_mkdir - directory 'name' was created
177 static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
179 __u32 mask = (FS_CREATE | FS_ISDIR);
180 struct inode *d_inode = dentry->d_inode;
182 audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
184 fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
188 * fsnotify_access - file was read
190 static inline void fsnotify_access(struct file *file)
192 const struct path *path = &file->f_path;
193 struct inode *inode = file_inode(file);
194 __u32 mask = FS_ACCESS;
196 if (S_ISDIR(inode->i_mode))
199 if (!(file->f_mode & FMODE_NONOTIFY))
200 fsnotify_path(inode, path, mask);
204 * fsnotify_modify - file was modified
206 static inline void fsnotify_modify(struct file *file)
208 const struct path *path = &file->f_path;
209 struct inode *inode = file_inode(file);
210 __u32 mask = FS_MODIFY;
212 if (S_ISDIR(inode->i_mode))
215 if (!(file->f_mode & FMODE_NONOTIFY))
216 fsnotify_path(inode, path, mask);
220 * fsnotify_open - file was opened
222 static inline void fsnotify_open(struct file *file)
224 const struct path *path = &file->f_path;
225 struct inode *inode = file_inode(file);
226 __u32 mask = FS_OPEN;
228 if (S_ISDIR(inode->i_mode))
230 if (file->f_flags & __FMODE_EXEC)
231 mask |= FS_OPEN_EXEC;
233 fsnotify_path(inode, path, mask);
237 * fsnotify_close - file was closed
239 static inline void fsnotify_close(struct file *file)
241 const struct path *path = &file->f_path;
242 struct inode *inode = file_inode(file);
243 fmode_t mode = file->f_mode;
244 __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
246 if (S_ISDIR(inode->i_mode))
249 if (!(file->f_mode & FMODE_NONOTIFY))
250 fsnotify_path(inode, path, mask);
254 * fsnotify_xattr - extended attributes were changed
256 static inline void fsnotify_xattr(struct dentry *dentry)
258 struct inode *inode = dentry->d_inode;
259 __u32 mask = FS_ATTRIB;
261 if (S_ISDIR(inode->i_mode))
264 fsnotify_parent(NULL, dentry, mask);
265 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
269 * fsnotify_change - notify_change event. file was modified and/or metadata
272 static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
274 struct inode *inode = dentry->d_inode;
277 if (ia_valid & ATTR_UID)
279 if (ia_valid & ATTR_GID)
281 if (ia_valid & ATTR_SIZE)
284 /* both times implies a utime(s) call */
285 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
287 else if (ia_valid & ATTR_ATIME)
289 else if (ia_valid & ATTR_MTIME)
292 if (ia_valid & ATTR_MODE)
296 if (S_ISDIR(inode->i_mode))
299 fsnotify_parent(NULL, dentry, mask);
300 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
304 #endif /* _LINUX_FS_NOTIFY_H */