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>
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
)
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;
37 if (file
->f_mode
& FMODE_NONOTIFY
)
39 if (!(mask
& (MAY_READ
| MAY_OPEN
)))
42 fsnotify_mask
= FS_OPEN_PERM
;
43 else if (mask
& MAY_READ
)
44 fsnotify_mask
= FS_ACCESS_PERM
;
48 ret
= fsnotify_parent(path
, NULL
, fsnotify_mask
);
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
;
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
,
86 fsnotify(new_dir
, new_dir_mask
, source
, FSNOTIFY_EVENT_INODE
, new_name
,
90 fsnotify_link_count(target
);
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
;
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
))
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
))
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
))
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
))
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
))
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
261 static inline void fsnotify_change(struct dentry
*dentry
, unsigned int ia_valid
)
263 struct inode
*inode
= dentry
->d_inode
;
266 if (ia_valid
& ATTR_UID
)
268 if (ia_valid
& ATTR_GID
)
270 if (ia_valid
& ATTR_SIZE
)
273 /* both times implies a utime(s) call */
274 if ((ia_valid
& (ATTR_ATIME
| ATTR_MTIME
)) == (ATTR_ATIME
| ATTR_MTIME
))
276 else if (ia_valid
& ATTR_ATIME
)
278 else if (ia_valid
& ATTR_MTIME
)
281 if (ia_valid
& ATTR_MODE
)
285 if (S_ISDIR(inode
->i_mode
))
288 fsnotify_parent(NULL
, dentry
, mask
);
289 fsnotify(inode
, mask
, inode
, FSNOTIFY_EVENT_INODE
, NULL
, 0);
293 #endif /* _LINUX_FS_NOTIFY_H */