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 and will not be reported if only
27 * the child is interested and not the parent.
29 static inline void fsnotify_name(struct inode
*dir
, __u32 mask
,
31 const struct qstr
*name
, u32 cookie
)
33 if (atomic_long_read(&dir
->i_sb
->s_fsnotify_connectors
) == 0)
36 fsnotify(mask
, child
, FSNOTIFY_EVENT_INODE
, dir
, name
, NULL
, cookie
);
39 static inline void fsnotify_dirent(struct inode
*dir
, struct dentry
*dentry
,
42 fsnotify_name(dir
, mask
, d_inode(dentry
), &dentry
->d_name
, 0);
45 static inline void fsnotify_inode(struct inode
*inode
, __u32 mask
)
47 if (atomic_long_read(&inode
->i_sb
->s_fsnotify_connectors
) == 0)
50 if (S_ISDIR(inode
->i_mode
))
53 fsnotify(mask
, inode
, FSNOTIFY_EVENT_INODE
, NULL
, NULL
, inode
, 0);
56 /* Notify this dentry's parent about a child's events. */
57 static inline int fsnotify_parent(struct dentry
*dentry
, __u32 mask
,
58 const void *data
, int data_type
)
60 struct inode
*inode
= d_inode(dentry
);
62 if (atomic_long_read(&inode
->i_sb
->s_fsnotify_connectors
) == 0)
65 if (S_ISDIR(inode
->i_mode
)) {
68 /* sb/mount marks are not interested in name of directory */
69 if (!(dentry
->d_flags
& DCACHE_FSNOTIFY_PARENT_WATCHED
))
73 /* disconnected dentry cannot notify parent */
77 return __fsnotify_parent(dentry
, mask
, data
, data_type
);
80 return fsnotify(mask
, data
, data_type
, NULL
, NULL
, inode
, 0);
84 * Simple wrappers to consolidate calls to fsnotify_parent() when an event
85 * is on a file/dentry.
87 static inline void fsnotify_dentry(struct dentry
*dentry
, __u32 mask
)
89 fsnotify_parent(dentry
, mask
, d_inode(dentry
), FSNOTIFY_EVENT_INODE
);
92 static inline int fsnotify_file(struct file
*file
, __u32 mask
)
94 const struct path
*path
= &file
->f_path
;
96 if (file
->f_mode
& FMODE_NONOTIFY
)
99 return fsnotify_parent(path
->dentry
, mask
, path
, FSNOTIFY_EVENT_PATH
);
102 /* Simple call site for access decisions */
103 static inline int fsnotify_perm(struct file
*file
, int mask
)
106 __u32 fsnotify_mask
= 0;
108 if (!(mask
& (MAY_READ
| MAY_OPEN
)))
111 if (mask
& MAY_OPEN
) {
112 fsnotify_mask
= FS_OPEN_PERM
;
114 if (file
->f_flags
& __FMODE_EXEC
) {
115 ret
= fsnotify_file(file
, FS_OPEN_EXEC_PERM
);
120 } else if (mask
& MAY_READ
) {
121 fsnotify_mask
= FS_ACCESS_PERM
;
124 return fsnotify_file(file
, fsnotify_mask
);
128 * fsnotify_link_count - inode's link count changed
130 static inline void fsnotify_link_count(struct inode
*inode
)
132 fsnotify_inode(inode
, FS_ATTRIB
);
136 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
138 static inline void fsnotify_move(struct inode
*old_dir
, struct inode
*new_dir
,
139 const struct qstr
*old_name
,
140 int isdir
, struct inode
*target
,
141 struct dentry
*moved
)
143 struct inode
*source
= moved
->d_inode
;
144 u32 fs_cookie
= fsnotify_get_cookie();
145 __u32 old_dir_mask
= FS_MOVED_FROM
;
146 __u32 new_dir_mask
= FS_MOVED_TO
;
147 const struct qstr
*new_name
= &moved
->d_name
;
149 if (old_dir
== new_dir
)
150 old_dir_mask
|= FS_DN_RENAME
;
153 old_dir_mask
|= FS_ISDIR
;
154 new_dir_mask
|= FS_ISDIR
;
157 fsnotify_name(old_dir
, old_dir_mask
, source
, old_name
, fs_cookie
);
158 fsnotify_name(new_dir
, new_dir_mask
, source
, new_name
, fs_cookie
);
161 fsnotify_link_count(target
);
162 fsnotify_inode(source
, FS_MOVE_SELF
);
163 audit_inode_child(new_dir
, moved
, AUDIT_TYPE_CHILD_CREATE
);
167 * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
169 static inline void fsnotify_inode_delete(struct inode
*inode
)
171 __fsnotify_inode_delete(inode
);
175 * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
177 static inline void fsnotify_vfsmount_delete(struct vfsmount
*mnt
)
179 __fsnotify_vfsmount_delete(mnt
);
183 * fsnotify_inoderemove - an inode is going away
185 static inline void fsnotify_inoderemove(struct inode
*inode
)
187 fsnotify_inode(inode
, FS_DELETE_SELF
);
188 __fsnotify_inode_delete(inode
);
192 * fsnotify_create - 'name' was linked in
194 static inline void fsnotify_create(struct inode
*inode
, struct dentry
*dentry
)
196 audit_inode_child(inode
, dentry
, AUDIT_TYPE_CHILD_CREATE
);
198 fsnotify_dirent(inode
, dentry
, FS_CREATE
);
202 * fsnotify_link - new hardlink in 'inode' directory
203 * Note: We have to pass also the linked inode ptr as some filesystems leave
204 * new_dentry->d_inode NULL and instantiate inode pointer later
206 static inline void fsnotify_link(struct inode
*dir
, struct inode
*inode
,
207 struct dentry
*new_dentry
)
209 fsnotify_link_count(inode
);
210 audit_inode_child(dir
, new_dentry
, AUDIT_TYPE_CHILD_CREATE
);
212 fsnotify_name(dir
, FS_CREATE
, inode
, &new_dentry
->d_name
, 0);
216 * fsnotify_unlink - 'name' was unlinked
218 * Caller must make sure that dentry->d_name is stable.
220 static inline void fsnotify_unlink(struct inode
*dir
, struct dentry
*dentry
)
222 /* Expected to be called before d_delete() */
223 WARN_ON_ONCE(d_is_negative(dentry
));
225 fsnotify_dirent(dir
, dentry
, FS_DELETE
);
229 * fsnotify_mkdir - directory 'name' was created
231 static inline void fsnotify_mkdir(struct inode
*inode
, struct dentry
*dentry
)
233 audit_inode_child(inode
, dentry
, AUDIT_TYPE_CHILD_CREATE
);
235 fsnotify_dirent(inode
, dentry
, FS_CREATE
| FS_ISDIR
);
239 * fsnotify_rmdir - directory 'name' was removed
241 * Caller must make sure that dentry->d_name is stable.
243 static inline void fsnotify_rmdir(struct inode
*dir
, struct dentry
*dentry
)
245 /* Expected to be called before d_delete() */
246 WARN_ON_ONCE(d_is_negative(dentry
));
248 fsnotify_dirent(dir
, dentry
, FS_DELETE
| FS_ISDIR
);
252 * fsnotify_access - file was read
254 static inline void fsnotify_access(struct file
*file
)
256 fsnotify_file(file
, FS_ACCESS
);
260 * fsnotify_modify - file was modified
262 static inline void fsnotify_modify(struct file
*file
)
264 fsnotify_file(file
, FS_MODIFY
);
268 * fsnotify_open - file was opened
270 static inline void fsnotify_open(struct file
*file
)
272 __u32 mask
= FS_OPEN
;
274 if (file
->f_flags
& __FMODE_EXEC
)
275 mask
|= FS_OPEN_EXEC
;
277 fsnotify_file(file
, mask
);
281 * fsnotify_close - file was closed
283 static inline void fsnotify_close(struct file
*file
)
285 __u32 mask
= (file
->f_mode
& FMODE_WRITE
) ? FS_CLOSE_WRITE
:
288 fsnotify_file(file
, mask
);
292 * fsnotify_xattr - extended attributes were changed
294 static inline void fsnotify_xattr(struct dentry
*dentry
)
296 fsnotify_dentry(dentry
, FS_ATTRIB
);
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
)
307 if (ia_valid
& ATTR_UID
)
309 if (ia_valid
& ATTR_GID
)
311 if (ia_valid
& ATTR_SIZE
)
314 /* both times implies a utime(s) call */
315 if ((ia_valid
& (ATTR_ATIME
| ATTR_MTIME
)) == (ATTR_ATIME
| ATTR_MTIME
))
317 else if (ia_valid
& ATTR_ATIME
)
319 else if (ia_valid
& ATTR_MTIME
)
322 if (ia_valid
& ATTR_MODE
)
326 fsnotify_dentry(dentry
, mask
);
329 #endif /* _LINUX_FS_NOTIFY_H */