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.
28 static inline void fsnotify_name(struct inode *dir, __u32 mask,
30 const struct qstr *name, u32 cookie)
32 fsnotify(dir, mask, child, FSNOTIFY_EVENT_INODE, name, cookie);
35 static inline void fsnotify_dirent(struct inode *dir, struct dentry *dentry,
38 fsnotify_name(dir, mask, d_inode(dentry), &dentry->d_name, 0);
42 * Simple wrappers to consolidate calls fsnotify_parent()/fsnotify() when
43 * an event is on a file/dentry.
45 static inline void fsnotify_dentry(struct dentry *dentry, __u32 mask)
47 struct inode *inode = d_inode(dentry);
49 if (S_ISDIR(inode->i_mode))
52 fsnotify_parent(dentry, mask, inode, FSNOTIFY_EVENT_INODE);
53 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
56 static inline int fsnotify_file(struct file *file, __u32 mask)
58 const struct path *path = &file->f_path;
59 struct inode *inode = file_inode(file);
62 if (file->f_mode & FMODE_NONOTIFY)
65 if (S_ISDIR(inode->i_mode))
68 ret = fsnotify_parent(path->dentry, mask, path, FSNOTIFY_EVENT_PATH);
72 return fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
75 /* Simple call site for access decisions */
76 static inline int fsnotify_perm(struct file *file, int mask)
79 __u32 fsnotify_mask = 0;
81 if (!(mask & (MAY_READ | MAY_OPEN)))
84 if (mask & MAY_OPEN) {
85 fsnotify_mask = FS_OPEN_PERM;
87 if (file->f_flags & __FMODE_EXEC) {
88 ret = fsnotify_file(file, FS_OPEN_EXEC_PERM);
93 } else if (mask & MAY_READ) {
94 fsnotify_mask = FS_ACCESS_PERM;
97 return fsnotify_file(file, fsnotify_mask);
101 * fsnotify_link_count - inode's link count changed
103 static inline void fsnotify_link_count(struct inode *inode)
105 __u32 mask = FS_ATTRIB;
107 if (S_ISDIR(inode->i_mode))
110 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
114 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
116 static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
117 const struct qstr *old_name,
118 int isdir, struct inode *target,
119 struct dentry *moved)
121 struct inode *source = moved->d_inode;
122 u32 fs_cookie = fsnotify_get_cookie();
123 __u32 old_dir_mask = FS_MOVED_FROM;
124 __u32 new_dir_mask = FS_MOVED_TO;
125 __u32 mask = FS_MOVE_SELF;
126 const struct qstr *new_name = &moved->d_name;
128 if (old_dir == new_dir)
129 old_dir_mask |= FS_DN_RENAME;
132 old_dir_mask |= FS_ISDIR;
133 new_dir_mask |= FS_ISDIR;
137 fsnotify_name(old_dir, old_dir_mask, source, old_name, fs_cookie);
138 fsnotify_name(new_dir, new_dir_mask, source, new_name, fs_cookie);
141 fsnotify_link_count(target);
144 fsnotify(source, mask, source, FSNOTIFY_EVENT_INODE, NULL, 0);
145 audit_inode_child(new_dir, moved, AUDIT_TYPE_CHILD_CREATE);
149 * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
151 static inline void fsnotify_inode_delete(struct inode *inode)
153 __fsnotify_inode_delete(inode);
157 * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
159 static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt)
161 __fsnotify_vfsmount_delete(mnt);
165 * fsnotify_inoderemove - an inode is going away
167 static inline void fsnotify_inoderemove(struct inode *inode)
169 __u32 mask = FS_DELETE_SELF;
171 if (S_ISDIR(inode->i_mode))
174 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
175 __fsnotify_inode_delete(inode);
179 * fsnotify_create - 'name' was linked in
181 static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
183 audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
185 fsnotify_dirent(inode, dentry, FS_CREATE);
189 * fsnotify_link - new hardlink in 'inode' directory
190 * Note: We have to pass also the linked inode ptr as some filesystems leave
191 * new_dentry->d_inode NULL and instantiate inode pointer later
193 static inline void fsnotify_link(struct inode *dir, struct inode *inode,
194 struct dentry *new_dentry)
196 fsnotify_link_count(inode);
197 audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE);
199 fsnotify_name(dir, FS_CREATE, inode, &new_dentry->d_name, 0);
203 * fsnotify_unlink - 'name' was unlinked
205 * Caller must make sure that dentry->d_name is stable.
207 static inline void fsnotify_unlink(struct inode *dir, struct dentry *dentry)
209 /* Expected to be called before d_delete() */
210 WARN_ON_ONCE(d_is_negative(dentry));
212 fsnotify_dirent(dir, dentry, FS_DELETE);
216 * fsnotify_mkdir - directory 'name' was created
218 static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
220 audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
222 fsnotify_dirent(inode, dentry, FS_CREATE | FS_ISDIR);
226 * fsnotify_rmdir - directory 'name' was removed
228 * Caller must make sure that dentry->d_name is stable.
230 static inline void fsnotify_rmdir(struct inode *dir, struct dentry *dentry)
232 /* Expected to be called before d_delete() */
233 WARN_ON_ONCE(d_is_negative(dentry));
235 fsnotify_dirent(dir, dentry, FS_DELETE | FS_ISDIR);
239 * fsnotify_access - file was read
241 static inline void fsnotify_access(struct file *file)
243 fsnotify_file(file, FS_ACCESS);
247 * fsnotify_modify - file was modified
249 static inline void fsnotify_modify(struct file *file)
251 fsnotify_file(file, FS_MODIFY);
255 * fsnotify_open - file was opened
257 static inline void fsnotify_open(struct file *file)
259 __u32 mask = FS_OPEN;
261 if (file->f_flags & __FMODE_EXEC)
262 mask |= FS_OPEN_EXEC;
264 fsnotify_file(file, mask);
268 * fsnotify_close - file was closed
270 static inline void fsnotify_close(struct file *file)
272 __u32 mask = (file->f_mode & FMODE_WRITE) ? FS_CLOSE_WRITE :
275 fsnotify_file(file, mask);
279 * fsnotify_xattr - extended attributes were changed
281 static inline void fsnotify_xattr(struct dentry *dentry)
283 fsnotify_dentry(dentry, FS_ATTRIB);
287 * fsnotify_change - notify_change event. file was modified and/or metadata
290 static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
294 if (ia_valid & ATTR_UID)
296 if (ia_valid & ATTR_GID)
298 if (ia_valid & ATTR_SIZE)
301 /* both times implies a utime(s) call */
302 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
304 else if (ia_valid & ATTR_ATIME)
306 else if (ia_valid & ATTR_MTIME)
309 if (ia_valid & ATTR_MODE)
313 fsnotify_dentry(dentry, mask);
316 #endif /* _LINUX_FS_NOTIFY_H */