]>
git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - include/linux/fsnotify.h
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 the directory entry @dentry.
23 * Unlike fsnotify_parent(), the event will be reported regardless of the
24 * FS_EVENT_ON_CHILD mask on the parent inode.
26 static inline int fsnotify_dirent(struct inode
*dir
, struct dentry
*dentry
,
29 return fsnotify(dir
, mask
, d_inode(dentry
), FSNOTIFY_EVENT_INODE
,
33 /* Notify this dentry's parent about a child's events. */
34 static inline int fsnotify_parent(const struct path
*path
,
35 struct dentry
*dentry
, __u32 mask
)
38 dentry
= path
->dentry
;
40 return __fsnotify_parent(path
, dentry
, mask
);
44 * Simple wrapper to consolidate calls fsnotify_parent()/fsnotify() when
45 * an event is on a path.
47 static inline int fsnotify_path(struct inode
*inode
, const struct path
*path
,
50 int ret
= fsnotify_parent(path
, NULL
, mask
);
54 return fsnotify(inode
, mask
, path
, FSNOTIFY_EVENT_PATH
, NULL
, 0);
57 /* Simple call site for access decisions */
58 static inline int fsnotify_perm(struct file
*file
, int mask
)
61 const struct path
*path
= &file
->f_path
;
62 struct inode
*inode
= file_inode(file
);
63 __u32 fsnotify_mask
= 0;
65 if (file
->f_mode
& FMODE_NONOTIFY
)
67 if (!(mask
& (MAY_READ
| MAY_OPEN
)))
69 if (mask
& MAY_OPEN
) {
70 fsnotify_mask
= FS_OPEN_PERM
;
72 if (file
->f_flags
& __FMODE_EXEC
) {
73 ret
= fsnotify_path(inode
, path
, FS_OPEN_EXEC_PERM
);
78 } else if (mask
& MAY_READ
) {
79 fsnotify_mask
= FS_ACCESS_PERM
;
82 if (S_ISDIR(inode
->i_mode
))
83 fsnotify_mask
|= FS_ISDIR
;
85 return fsnotify_path(inode
, path
, fsnotify_mask
);
89 * fsnotify_link_count - inode's link count changed
91 static inline void fsnotify_link_count(struct inode
*inode
)
93 __u32 mask
= FS_ATTRIB
;
95 if (S_ISDIR(inode
->i_mode
))
98 fsnotify(inode
, mask
, inode
, FSNOTIFY_EVENT_INODE
, NULL
, 0);
102 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
104 static inline void fsnotify_move(struct inode
*old_dir
, struct inode
*new_dir
,
105 const struct qstr
*old_name
,
106 int isdir
, struct inode
*target
,
107 struct dentry
*moved
)
109 struct inode
*source
= moved
->d_inode
;
110 u32 fs_cookie
= fsnotify_get_cookie();
111 __u32 old_dir_mask
= FS_MOVED_FROM
;
112 __u32 new_dir_mask
= FS_MOVED_TO
;
113 __u32 mask
= FS_MOVE_SELF
;
114 const struct qstr
*new_name
= &moved
->d_name
;
116 if (old_dir
== new_dir
)
117 old_dir_mask
|= FS_DN_RENAME
;
120 old_dir_mask
|= FS_ISDIR
;
121 new_dir_mask
|= FS_ISDIR
;
125 fsnotify(old_dir
, old_dir_mask
, source
, FSNOTIFY_EVENT_INODE
, old_name
,
127 fsnotify(new_dir
, new_dir_mask
, source
, FSNOTIFY_EVENT_INODE
, new_name
,
131 fsnotify_link_count(target
);
134 fsnotify(source
, mask
, source
, FSNOTIFY_EVENT_INODE
, NULL
, 0);
135 audit_inode_child(new_dir
, moved
, AUDIT_TYPE_CHILD_CREATE
);
139 * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
141 static inline void fsnotify_inode_delete(struct inode
*inode
)
143 __fsnotify_inode_delete(inode
);
147 * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
149 static inline void fsnotify_vfsmount_delete(struct vfsmount
*mnt
)
151 __fsnotify_vfsmount_delete(mnt
);
155 * fsnotify_inoderemove - an inode is going away
157 static inline void fsnotify_inoderemove(struct inode
*inode
)
159 __u32 mask
= FS_DELETE_SELF
;
161 if (S_ISDIR(inode
->i_mode
))
164 fsnotify(inode
, mask
, inode
, FSNOTIFY_EVENT_INODE
, NULL
, 0);
165 __fsnotify_inode_delete(inode
);
169 * fsnotify_create - 'name' was linked in
171 static inline void fsnotify_create(struct inode
*inode
, struct dentry
*dentry
)
173 audit_inode_child(inode
, dentry
, AUDIT_TYPE_CHILD_CREATE
);
175 fsnotify_dirent(inode
, dentry
, FS_CREATE
);
179 * fsnotify_link - new hardlink in 'inode' directory
180 * Note: We have to pass also the linked inode ptr as some filesystems leave
181 * new_dentry->d_inode NULL and instantiate inode pointer later
183 static inline void fsnotify_link(struct inode
*dir
, struct inode
*inode
, struct dentry
*new_dentry
)
185 fsnotify_link_count(inode
);
186 audit_inode_child(dir
, new_dentry
, AUDIT_TYPE_CHILD_CREATE
);
188 fsnotify(dir
, FS_CREATE
, inode
, FSNOTIFY_EVENT_INODE
, &new_dentry
->d_name
, 0);
192 * fsnotify_delete - @dentry was unlinked and unhashed
194 * Caller must make sure that dentry->d_name is stable.
196 * Note: unlike fsnotify_unlink(), we have to pass also the unlinked inode
197 * as this may be called after d_delete() and old_dentry may be negative.
199 static inline void fsnotify_delete(struct inode
*dir
, struct inode
*inode
,
200 struct dentry
*dentry
)
202 __u32 mask
= FS_DELETE
;
204 if (S_ISDIR(inode
->i_mode
))
207 fsnotify(dir
, mask
, inode
, FSNOTIFY_EVENT_INODE
, &dentry
->d_name
, 0);
211 * d_delete_notify - delete a dentry and call fsnotify_delete()
212 * @dentry: The dentry to delete
214 * This helper is used to guaranty that the unlinked inode cannot be found
215 * by lookup of this name after fsnotify_delete() event has been delivered.
217 static inline void d_delete_notify(struct inode
*dir
, struct dentry
*dentry
)
219 struct inode
*inode
= d_inode(dentry
);
223 fsnotify_delete(dir
, inode
, dentry
);
228 * fsnotify_unlink - 'name' was unlinked
230 * Caller must make sure that dentry->d_name is stable.
232 static inline void fsnotify_unlink(struct inode
*dir
, struct dentry
*dentry
)
234 if (WARN_ON_ONCE(d_is_negative(dentry
)))
237 fsnotify_delete(dir
, d_inode(dentry
), dentry
);
241 * fsnotify_mkdir - directory 'name' was created
243 static inline void fsnotify_mkdir(struct inode
*inode
, struct dentry
*dentry
)
245 audit_inode_child(inode
, dentry
, AUDIT_TYPE_CHILD_CREATE
);
247 fsnotify_dirent(inode
, dentry
, FS_CREATE
| FS_ISDIR
);
251 * fsnotify_rmdir - directory 'name' was removed
253 * Caller must make sure that dentry->d_name is stable.
255 static inline void fsnotify_rmdir(struct inode
*dir
, struct dentry
*dentry
)
257 if (WARN_ON_ONCE(d_is_negative(dentry
)))
260 fsnotify_delete(dir
, d_inode(dentry
), dentry
);
264 * fsnotify_access - file was read
266 static inline void fsnotify_access(struct file
*file
)
268 const struct path
*path
= &file
->f_path
;
269 struct inode
*inode
= file_inode(file
);
270 __u32 mask
= FS_ACCESS
;
272 if (S_ISDIR(inode
->i_mode
))
275 if (!(file
->f_mode
& FMODE_NONOTIFY
))
276 fsnotify_path(inode
, path
, mask
);
280 * fsnotify_modify - file was modified
282 static inline void fsnotify_modify(struct file
*file
)
284 const struct path
*path
= &file
->f_path
;
285 struct inode
*inode
= file_inode(file
);
286 __u32 mask
= FS_MODIFY
;
288 if (S_ISDIR(inode
->i_mode
))
291 if (!(file
->f_mode
& FMODE_NONOTIFY
))
292 fsnotify_path(inode
, path
, mask
);
296 * fsnotify_open - file was opened
298 static inline void fsnotify_open(struct file
*file
)
300 const struct path
*path
= &file
->f_path
;
301 struct inode
*inode
= file_inode(file
);
302 __u32 mask
= FS_OPEN
;
304 if (S_ISDIR(inode
->i_mode
))
306 if (file
->f_flags
& __FMODE_EXEC
)
307 mask
|= FS_OPEN_EXEC
;
309 fsnotify_path(inode
, path
, mask
);
313 * fsnotify_close - file was closed
315 static inline void fsnotify_close(struct file
*file
)
317 const struct path
*path
= &file
->f_path
;
318 struct inode
*inode
= file_inode(file
);
319 fmode_t mode
= file
->f_mode
;
320 __u32 mask
= (mode
& FMODE_WRITE
) ? FS_CLOSE_WRITE
: FS_CLOSE_NOWRITE
;
322 if (S_ISDIR(inode
->i_mode
))
325 if (!(file
->f_mode
& FMODE_NONOTIFY
))
326 fsnotify_path(inode
, path
, mask
);
330 * fsnotify_xattr - extended attributes were changed
332 static inline void fsnotify_xattr(struct dentry
*dentry
)
334 struct inode
*inode
= dentry
->d_inode
;
335 __u32 mask
= FS_ATTRIB
;
337 if (S_ISDIR(inode
->i_mode
))
340 fsnotify_parent(NULL
, dentry
, mask
);
341 fsnotify(inode
, mask
, inode
, FSNOTIFY_EVENT_INODE
, NULL
, 0);
345 * fsnotify_change - notify_change event. file was modified and/or metadata
348 static inline void fsnotify_change(struct dentry
*dentry
, unsigned int ia_valid
)
350 struct inode
*inode
= dentry
->d_inode
;
353 if (ia_valid
& ATTR_UID
)
355 if (ia_valid
& ATTR_GID
)
357 if (ia_valid
& ATTR_SIZE
)
360 /* both times implies a utime(s) call */
361 if ((ia_valid
& (ATTR_ATIME
| ATTR_MTIME
)) == (ATTR_ATIME
| ATTR_MTIME
))
363 else if (ia_valid
& ATTR_ATIME
)
365 else if (ia_valid
& ATTR_MTIME
)
368 if (ia_valid
& ATTR_MODE
)
372 if (S_ISDIR(inode
->i_mode
))
375 fsnotify_parent(NULL
, dentry
, mask
);
376 fsnotify(inode
, mask
, inode
, FSNOTIFY_EVENT_INODE
, NULL
, 0);
380 #endif /* _LINUX_FS_NOTIFY_H */