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 the directory entry @dentry.
23 * Unlike fsnotify_parent(), the event will be reported regardless of the
24 * FS_EVENT_ON_CHILD mask on the parent inode.
26 static inline int fsnotify_dirent(struct inode *dir, struct dentry *dentry,
29 return fsnotify(dir, mask, d_inode(dentry), FSNOTIFY_EVENT_INODE,
30 dentry->d_name.name, 0);
33 /* Notify this dentry's parent about a child's events. */
34 static inline int fsnotify_parent(const struct path *path,
35 struct dentry *dentry, __u32 mask)
38 dentry = path->dentry;
40 return __fsnotify_parent(path, dentry, mask);
44 * Simple wrapper to consolidate calls fsnotify_parent()/fsnotify() when
45 * an event is on a path.
47 static inline int fsnotify_path(struct inode *inode, const struct path *path,
50 int ret = fsnotify_parent(path, NULL, mask);
54 return fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
57 /* Simple call site for access decisions */
58 static inline int fsnotify_perm(struct file *file, int mask)
61 const struct path *path = &file->f_path;
62 struct inode *inode = file_inode(file);
63 __u32 fsnotify_mask = 0;
65 if (file->f_mode & FMODE_NONOTIFY)
67 if (!(mask & (MAY_READ | MAY_OPEN)))
69 if (mask & MAY_OPEN) {
70 fsnotify_mask = FS_OPEN_PERM;
72 if (file->f_flags & __FMODE_EXEC) {
73 ret = fsnotify_path(inode, path, FS_OPEN_EXEC_PERM);
78 } else if (mask & MAY_READ) {
79 fsnotify_mask = FS_ACCESS_PERM;
82 return fsnotify_path(inode, path, fsnotify_mask);
86 * fsnotify_link_count - inode's link count changed
88 static inline void fsnotify_link_count(struct inode *inode)
90 fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
94 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
96 static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
97 const unsigned char *old_name,
98 int isdir, struct inode *target, struct dentry *moved)
100 struct inode *source = moved->d_inode;
101 u32 fs_cookie = fsnotify_get_cookie();
102 __u32 old_dir_mask = FS_MOVED_FROM;
103 __u32 new_dir_mask = FS_MOVED_TO;
104 const unsigned char *new_name = moved->d_name.name;
106 if (old_dir == new_dir)
107 old_dir_mask |= FS_DN_RENAME;
110 old_dir_mask |= FS_ISDIR;
111 new_dir_mask |= FS_ISDIR;
114 fsnotify(old_dir, old_dir_mask, source, FSNOTIFY_EVENT_INODE, old_name,
116 fsnotify(new_dir, new_dir_mask, source, FSNOTIFY_EVENT_INODE, new_name,
120 fsnotify_link_count(target);
123 fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0);
124 audit_inode_child(new_dir, moved, AUDIT_TYPE_CHILD_CREATE);
128 * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
130 static inline void fsnotify_inode_delete(struct inode *inode)
132 __fsnotify_inode_delete(inode);
136 * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
138 static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt)
140 __fsnotify_vfsmount_delete(mnt);
144 * fsnotify_nameremove - a filename was removed from a directory
146 * This is mostly called under parent vfs inode lock so name and
147 * dentry->d_parent should be stable. However there are some corner cases where
148 * inode lock is not held. So to be on the safe side and be reselient to future
149 * callers and out of tree users of d_delete(), we do not assume that d_parent
150 * and d_name are stable and we use dget_parent() and
151 * take_dentry_name_snapshot() to grab stable references.
153 static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
155 struct dentry *parent;
156 struct name_snapshot name;
157 __u32 mask = FS_DELETE;
159 /* d_delete() of pseudo inode? (e.g. __ns_get_path() playing tricks) */
166 parent = dget_parent(dentry);
167 take_dentry_name_snapshot(&name, dentry);
169 fsnotify(d_inode(parent), mask, d_inode(dentry), FSNOTIFY_EVENT_INODE,
172 release_dentry_name_snapshot(&name);
177 * fsnotify_inoderemove - an inode is going away
179 static inline void fsnotify_inoderemove(struct inode *inode)
181 fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
182 __fsnotify_inode_delete(inode);
186 * fsnotify_create - 'name' was linked in
188 static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
190 audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
192 fsnotify_dirent(inode, dentry, FS_CREATE);
196 * fsnotify_link - new hardlink in 'inode' directory
197 * Note: We have to pass also the linked inode ptr as some filesystems leave
198 * new_dentry->d_inode NULL and instantiate inode pointer later
200 static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry)
202 fsnotify_link_count(inode);
203 audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE);
205 fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name, 0);
209 * fsnotify_mkdir - directory 'name' was created
211 static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
213 audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
215 fsnotify_dirent(inode, dentry, FS_CREATE | FS_ISDIR);
219 * fsnotify_access - file was read
221 static inline void fsnotify_access(struct file *file)
223 const struct path *path = &file->f_path;
224 struct inode *inode = file_inode(file);
225 __u32 mask = FS_ACCESS;
227 if (S_ISDIR(inode->i_mode))
230 if (!(file->f_mode & FMODE_NONOTIFY))
231 fsnotify_path(inode, path, mask);
235 * fsnotify_modify - file was modified
237 static inline void fsnotify_modify(struct file *file)
239 const struct path *path = &file->f_path;
240 struct inode *inode = file_inode(file);
241 __u32 mask = FS_MODIFY;
243 if (S_ISDIR(inode->i_mode))
246 if (!(file->f_mode & FMODE_NONOTIFY))
247 fsnotify_path(inode, path, mask);
251 * fsnotify_open - file was opened
253 static inline void fsnotify_open(struct file *file)
255 const struct path *path = &file->f_path;
256 struct inode *inode = file_inode(file);
257 __u32 mask = FS_OPEN;
259 if (S_ISDIR(inode->i_mode))
261 if (file->f_flags & __FMODE_EXEC)
262 mask |= FS_OPEN_EXEC;
264 fsnotify_path(inode, path, mask);
268 * fsnotify_close - file was closed
270 static inline void fsnotify_close(struct file *file)
272 const struct path *path = &file->f_path;
273 struct inode *inode = file_inode(file);
274 fmode_t mode = file->f_mode;
275 __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
277 if (S_ISDIR(inode->i_mode))
280 if (!(file->f_mode & FMODE_NONOTIFY))
281 fsnotify_path(inode, path, mask);
285 * fsnotify_xattr - extended attributes were changed
287 static inline void fsnotify_xattr(struct dentry *dentry)
289 struct inode *inode = dentry->d_inode;
290 __u32 mask = FS_ATTRIB;
292 if (S_ISDIR(inode->i_mode))
295 fsnotify_parent(NULL, dentry, mask);
296 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
300 * fsnotify_change - notify_change event. file was modified and/or metadata
303 static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
305 struct inode *inode = dentry->d_inode;
308 if (ia_valid & ATTR_UID)
310 if (ia_valid & ATTR_GID)
312 if (ia_valid & ATTR_SIZE)
315 /* both times implies a utime(s) call */
316 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
318 else if (ia_valid & ATTR_ATIME)
320 else if (ia_valid & ATTR_MTIME)
323 if (ia_valid & ATTR_MODE)
327 if (S_ISDIR(inode->i_mode))
330 fsnotify_parent(NULL, dentry, mask);
331 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
335 #endif /* _LINUX_FS_NOTIFY_H */