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 fsnotify(mask
, child
, FSNOTIFY_EVENT_INODE
, dir
, name
, NULL
, cookie
);
36 static inline void fsnotify_dirent(struct inode
*dir
, struct dentry
*dentry
,
39 fsnotify_name(dir
, mask
, d_inode(dentry
), &dentry
->d_name
, 0);
42 static inline void fsnotify_inode(struct inode
*inode
, __u32 mask
)
44 if (S_ISDIR(inode
->i_mode
))
47 fsnotify(mask
, inode
, FSNOTIFY_EVENT_INODE
, NULL
, NULL
, inode
, 0);
50 /* Notify this dentry's parent about a child's events. */
51 static inline int fsnotify_parent(struct dentry
*dentry
, __u32 mask
,
52 const void *data
, int data_type
)
54 struct inode
*inode
= d_inode(dentry
);
56 if (S_ISDIR(inode
->i_mode
)) {
59 /* sb/mount marks are not interested in name of directory */
60 if (!(dentry
->d_flags
& DCACHE_FSNOTIFY_PARENT_WATCHED
))
64 /* disconnected dentry cannot notify parent */
68 return __fsnotify_parent(dentry
, mask
, data
, data_type
);
71 return fsnotify(mask
, data
, data_type
, NULL
, NULL
, inode
, 0);
75 * Simple wrappers to consolidate calls to fsnotify_parent() when an event
76 * is on a file/dentry.
78 static inline void fsnotify_dentry(struct dentry
*dentry
, __u32 mask
)
80 fsnotify_parent(dentry
, mask
, d_inode(dentry
), FSNOTIFY_EVENT_INODE
);
83 static inline int fsnotify_file(struct file
*file
, __u32 mask
)
85 const struct path
*path
= &file
->f_path
;
87 if (file
->f_mode
& FMODE_NONOTIFY
)
90 return fsnotify_parent(path
->dentry
, mask
, path
, FSNOTIFY_EVENT_PATH
);
93 /* Simple call site for access decisions */
94 static inline int fsnotify_perm(struct file
*file
, int mask
)
97 __u32 fsnotify_mask
= 0;
99 if (!(mask
& (MAY_READ
| MAY_OPEN
)))
102 if (mask
& MAY_OPEN
) {
103 fsnotify_mask
= FS_OPEN_PERM
;
105 if (file
->f_flags
& __FMODE_EXEC
) {
106 ret
= fsnotify_file(file
, FS_OPEN_EXEC_PERM
);
111 } else if (mask
& MAY_READ
) {
112 fsnotify_mask
= FS_ACCESS_PERM
;
115 return fsnotify_file(file
, fsnotify_mask
);
119 * fsnotify_link_count - inode's link count changed
121 static inline void fsnotify_link_count(struct inode
*inode
)
123 fsnotify_inode(inode
, FS_ATTRIB
);
127 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
129 static inline void fsnotify_move(struct inode
*old_dir
, struct inode
*new_dir
,
130 const struct qstr
*old_name
,
131 int isdir
, struct inode
*target
,
132 struct dentry
*moved
)
134 struct inode
*source
= moved
->d_inode
;
135 u32 fs_cookie
= fsnotify_get_cookie();
136 __u32 old_dir_mask
= FS_MOVED_FROM
;
137 __u32 new_dir_mask
= FS_MOVED_TO
;
138 const struct qstr
*new_name
= &moved
->d_name
;
140 if (old_dir
== new_dir
)
141 old_dir_mask
|= FS_DN_RENAME
;
144 old_dir_mask
|= FS_ISDIR
;
145 new_dir_mask
|= FS_ISDIR
;
148 fsnotify_name(old_dir
, old_dir_mask
, source
, old_name
, fs_cookie
);
149 fsnotify_name(new_dir
, new_dir_mask
, source
, new_name
, fs_cookie
);
152 fsnotify_link_count(target
);
153 fsnotify_inode(source
, FS_MOVE_SELF
);
154 audit_inode_child(new_dir
, moved
, AUDIT_TYPE_CHILD_CREATE
);
158 * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
160 static inline void fsnotify_inode_delete(struct inode
*inode
)
162 __fsnotify_inode_delete(inode
);
166 * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
168 static inline void fsnotify_vfsmount_delete(struct vfsmount
*mnt
)
170 __fsnotify_vfsmount_delete(mnt
);
174 * fsnotify_inoderemove - an inode is going away
176 static inline void fsnotify_inoderemove(struct inode
*inode
)
178 fsnotify_inode(inode
, FS_DELETE_SELF
);
179 __fsnotify_inode_delete(inode
);
183 * fsnotify_create - 'name' was linked in
185 static inline void fsnotify_create(struct inode
*inode
, struct dentry
*dentry
)
187 audit_inode_child(inode
, dentry
, AUDIT_TYPE_CHILD_CREATE
);
189 fsnotify_dirent(inode
, dentry
, FS_CREATE
);
193 * fsnotify_link - new hardlink in 'inode' directory
194 * Note: We have to pass also the linked inode ptr as some filesystems leave
195 * new_dentry->d_inode NULL and instantiate inode pointer later
197 static inline void fsnotify_link(struct inode
*dir
, struct inode
*inode
,
198 struct dentry
*new_dentry
)
200 fsnotify_link_count(inode
);
201 audit_inode_child(dir
, new_dentry
, AUDIT_TYPE_CHILD_CREATE
);
203 fsnotify_name(dir
, FS_CREATE
, inode
, &new_dentry
->d_name
, 0);
207 * fsnotify_unlink - 'name' was unlinked
209 * Caller must make sure that dentry->d_name is stable.
211 static inline void fsnotify_unlink(struct inode
*dir
, struct dentry
*dentry
)
213 /* Expected to be called before d_delete() */
214 WARN_ON_ONCE(d_is_negative(dentry
));
216 fsnotify_dirent(dir
, dentry
, FS_DELETE
);
220 * fsnotify_mkdir - directory 'name' was created
222 static inline void fsnotify_mkdir(struct inode
*inode
, struct dentry
*dentry
)
224 audit_inode_child(inode
, dentry
, AUDIT_TYPE_CHILD_CREATE
);
226 fsnotify_dirent(inode
, dentry
, FS_CREATE
| FS_ISDIR
);
230 * fsnotify_rmdir - directory 'name' was removed
232 * Caller must make sure that dentry->d_name is stable.
234 static inline void fsnotify_rmdir(struct inode
*dir
, struct dentry
*dentry
)
236 /* Expected to be called before d_delete() */
237 WARN_ON_ONCE(d_is_negative(dentry
));
239 fsnotify_dirent(dir
, dentry
, FS_DELETE
| FS_ISDIR
);
243 * fsnotify_access - file was read
245 static inline void fsnotify_access(struct file
*file
)
247 fsnotify_file(file
, FS_ACCESS
);
251 * fsnotify_modify - file was modified
253 static inline void fsnotify_modify(struct file
*file
)
255 fsnotify_file(file
, FS_MODIFY
);
259 * fsnotify_open - file was opened
261 static inline void fsnotify_open(struct file
*file
)
263 __u32 mask
= FS_OPEN
;
265 if (file
->f_flags
& __FMODE_EXEC
)
266 mask
|= FS_OPEN_EXEC
;
268 fsnotify_file(file
, mask
);
272 * fsnotify_close - file was closed
274 static inline void fsnotify_close(struct file
*file
)
276 __u32 mask
= (file
->f_mode
& FMODE_WRITE
) ? FS_CLOSE_WRITE
:
279 fsnotify_file(file
, mask
);
283 * fsnotify_xattr - extended attributes were changed
285 static inline void fsnotify_xattr(struct dentry
*dentry
)
287 fsnotify_dentry(dentry
, FS_ATTRIB
);
291 * fsnotify_change - notify_change event. file was modified and/or metadata
294 static inline void fsnotify_change(struct dentry
*dentry
, unsigned int ia_valid
)
298 if (ia_valid
& ATTR_UID
)
300 if (ia_valid
& ATTR_GID
)
302 if (ia_valid
& ATTR_SIZE
)
305 /* both times implies a utime(s) call */
306 if ((ia_valid
& (ATTR_ATIME
| ATTR_MTIME
)) == (ATTR_ATIME
| ATTR_MTIME
))
308 else if (ia_valid
& ATTR_ATIME
)
310 else if (ia_valid
& ATTR_MTIME
)
313 if (ia_valid
& ATTR_MODE
)
317 fsnotify_dentry(dentry
, mask
);
320 #endif /* _LINUX_FS_NOTIFY_H */