Linux 4.19-rc7
[linux-2.6/btrfs-unstable.git] / include / linux / fsnotify.h
blobfd1ce10553bfad43fbbfdc1faa367f9fd0fd93c8
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_FS_NOTIFY_H
3 #define _LINUX_FS_NOTIFY_H
5 /*
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)
23 if (!dentry)
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;
33 struct inode *inode = file_inode(file);
34 __u32 fsnotify_mask = 0;
35 int ret;
37 if (file->f_mode & FMODE_NONOTIFY)
38 return 0;
39 if (!(mask & (MAY_READ | MAY_OPEN)))
40 return 0;
41 if (mask & MAY_OPEN)
42 fsnotify_mask = FS_OPEN_PERM;
43 else if (mask & MAY_READ)
44 fsnotify_mask = FS_ACCESS_PERM;
45 else
46 BUG();
48 ret = fsnotify_parent(path, NULL, fsnotify_mask);
49 if (ret)
50 return ret;
52 return fsnotify(inode, fsnotify_mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
56 * fsnotify_link_count - inode's link count changed
58 static inline void fsnotify_link_count(struct inode *inode)
60 fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
64 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
66 static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
67 const unsigned char *old_name,
68 int isdir, struct inode *target, struct dentry *moved)
70 struct inode *source = moved->d_inode;
71 u32 fs_cookie = fsnotify_get_cookie();
72 __u32 old_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_FROM);
73 __u32 new_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_TO);
74 const unsigned char *new_name = moved->d_name.name;
76 if (old_dir == new_dir)
77 old_dir_mask |= FS_DN_RENAME;
79 if (isdir) {
80 old_dir_mask |= FS_ISDIR;
81 new_dir_mask |= FS_ISDIR;
84 fsnotify(old_dir, old_dir_mask, source, FSNOTIFY_EVENT_INODE, old_name,
85 fs_cookie);
86 fsnotify(new_dir, new_dir_mask, source, FSNOTIFY_EVENT_INODE, new_name,
87 fs_cookie);
89 if (target)
90 fsnotify_link_count(target);
92 if (source)
93 fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0);
94 audit_inode_child(new_dir, moved, AUDIT_TYPE_CHILD_CREATE);
98 * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
100 static inline void fsnotify_inode_delete(struct inode *inode)
102 __fsnotify_inode_delete(inode);
106 * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
108 static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt)
110 __fsnotify_vfsmount_delete(mnt);
114 * fsnotify_nameremove - a filename was removed from a directory
116 static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
118 __u32 mask = FS_DELETE;
120 if (isdir)
121 mask |= FS_ISDIR;
123 fsnotify_parent(NULL, dentry, mask);
127 * fsnotify_inoderemove - an inode is going away
129 static inline void fsnotify_inoderemove(struct inode *inode)
131 fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
132 __fsnotify_inode_delete(inode);
136 * fsnotify_create - 'name' was linked in
138 static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
140 audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
142 fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
146 * fsnotify_link - new hardlink in 'inode' directory
147 * Note: We have to pass also the linked inode ptr as some filesystems leave
148 * new_dentry->d_inode NULL and instantiate inode pointer later
150 static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry)
152 fsnotify_link_count(inode);
153 audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE);
155 fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name, 0);
159 * fsnotify_mkdir - directory 'name' was created
161 static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
163 __u32 mask = (FS_CREATE | FS_ISDIR);
164 struct inode *d_inode = dentry->d_inode;
166 audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
168 fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
172 * fsnotify_access - file was read
174 static inline void fsnotify_access(struct file *file)
176 const struct path *path = &file->f_path;
177 struct inode *inode = file_inode(file);
178 __u32 mask = FS_ACCESS;
180 if (S_ISDIR(inode->i_mode))
181 mask |= FS_ISDIR;
183 if (!(file->f_mode & FMODE_NONOTIFY)) {
184 fsnotify_parent(path, NULL, mask);
185 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
190 * fsnotify_modify - file was modified
192 static inline void fsnotify_modify(struct file *file)
194 const struct path *path = &file->f_path;
195 struct inode *inode = file_inode(file);
196 __u32 mask = FS_MODIFY;
198 if (S_ISDIR(inode->i_mode))
199 mask |= FS_ISDIR;
201 if (!(file->f_mode & FMODE_NONOTIFY)) {
202 fsnotify_parent(path, NULL, mask);
203 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
208 * fsnotify_open - file was opened
210 static inline void fsnotify_open(struct file *file)
212 const struct path *path = &file->f_path;
213 struct inode *inode = file_inode(file);
214 __u32 mask = FS_OPEN;
216 if (S_ISDIR(inode->i_mode))
217 mask |= FS_ISDIR;
219 fsnotify_parent(path, NULL, mask);
220 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
224 * fsnotify_close - file was closed
226 static inline void fsnotify_close(struct file *file)
228 const struct path *path = &file->f_path;
229 struct inode *inode = file_inode(file);
230 fmode_t mode = file->f_mode;
231 __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
233 if (S_ISDIR(inode->i_mode))
234 mask |= FS_ISDIR;
236 if (!(file->f_mode & FMODE_NONOTIFY)) {
237 fsnotify_parent(path, NULL, mask);
238 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
243 * fsnotify_xattr - extended attributes were changed
245 static inline void fsnotify_xattr(struct dentry *dentry)
247 struct inode *inode = dentry->d_inode;
248 __u32 mask = FS_ATTRIB;
250 if (S_ISDIR(inode->i_mode))
251 mask |= FS_ISDIR;
253 fsnotify_parent(NULL, dentry, mask);
254 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
258 * fsnotify_change - notify_change event. file was modified and/or metadata
259 * was changed.
261 static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
263 struct inode *inode = dentry->d_inode;
264 __u32 mask = 0;
266 if (ia_valid & ATTR_UID)
267 mask |= FS_ATTRIB;
268 if (ia_valid & ATTR_GID)
269 mask |= FS_ATTRIB;
270 if (ia_valid & ATTR_SIZE)
271 mask |= FS_MODIFY;
273 /* both times implies a utime(s) call */
274 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
275 mask |= FS_ATTRIB;
276 else if (ia_valid & ATTR_ATIME)
277 mask |= FS_ACCESS;
278 else if (ia_valid & ATTR_MTIME)
279 mask |= FS_MODIFY;
281 if (ia_valid & ATTR_MODE)
282 mask |= FS_ATTRIB;
284 if (mask) {
285 if (S_ISDIR(inode->i_mode))
286 mask |= FS_ISDIR;
288 fsnotify_parent(NULL, dentry, mask);
289 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
293 #endif /* _LINUX_FS_NOTIFY_H */