blob: 79add91eaa04ebf3b4355cabae415709ab3bbb6f [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _LINUX_FS_NOTIFY_H
3#define _LINUX_FS_NOTIFY_H
4
5/*
6 * include/linux/fsnotify.h - generic hooks for filesystem notification, to
7 * reduce in-source duplication from both dnotify and inotify.
8 *
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.
11 *
12 * (C) Copyright 2005 Robert Love
13 */
14
15#include <linux/fsnotify_backend.h>
16#include <linux/audit.h>
17#include <linux/slab.h>
18#include <linux/bug.h>
19
David Brazdil0f672f62019-12-10 10:32:29 +000020/*
Olivier Deprez157378f2022-04-04 15:47:50 +020021 * 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).
David Brazdil0f672f62019-12-10 10:32:29 +000024 *
25 * Unlike fsnotify_parent(), the event will be reported regardless of the
Olivier Deprez157378f2022-04-04 15:47:50 +020026 * 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.
David Brazdil0f672f62019-12-10 10:32:29 +000028 */
Olivier Deprez157378f2022-04-04 15:47:50 +020029static inline void fsnotify_name(struct inode *dir, __u32 mask,
30 struct inode *child,
31 const struct qstr *name, u32 cookie)
David Brazdil0f672f62019-12-10 10:32:29 +000032{
Olivier Deprez157378f2022-04-04 15:47:50 +020033 fsnotify(mask, child, FSNOTIFY_EVENT_INODE, dir, name, NULL, cookie);
34}
35
36static inline void fsnotify_dirent(struct inode *dir, struct dentry *dentry,
37 __u32 mask)
38{
39 fsnotify_name(dir, mask, d_inode(dentry), &dentry->d_name, 0);
40}
41
42static inline void fsnotify_inode(struct inode *inode, __u32 mask)
43{
44 if (S_ISDIR(inode->i_mode))
45 mask |= FS_ISDIR;
46
47 fsnotify(mask, inode, FSNOTIFY_EVENT_INODE, NULL, NULL, inode, 0);
David Brazdil0f672f62019-12-10 10:32:29 +000048}
49
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000050/* Notify this dentry's parent about a child's events. */
Olivier Deprez157378f2022-04-04 15:47:50 +020051static inline int fsnotify_parent(struct dentry *dentry, __u32 mask,
52 const void *data, int data_type)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000053{
Olivier Deprez157378f2022-04-04 15:47:50 +020054 struct inode *inode = d_inode(dentry);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000055
Olivier Deprez157378f2022-04-04 15:47:50 +020056 if (S_ISDIR(inode->i_mode)) {
57 mask |= FS_ISDIR;
58
59 /* sb/mount marks are not interested in name of directory */
60 if (!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED))
61 goto notify_child;
62 }
63
64 /* disconnected dentry cannot notify parent */
65 if (IS_ROOT(dentry))
66 goto notify_child;
67
68 return __fsnotify_parent(dentry, mask, data, data_type);
69
70notify_child:
71 return fsnotify(mask, data, data_type, NULL, NULL, inode, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000072}
73
David Brazdil0f672f62019-12-10 10:32:29 +000074/*
Olivier Deprez157378f2022-04-04 15:47:50 +020075 * Simple wrappers to consolidate calls to fsnotify_parent() when an event
76 * is on a file/dentry.
David Brazdil0f672f62019-12-10 10:32:29 +000077 */
Olivier Deprez157378f2022-04-04 15:47:50 +020078static inline void fsnotify_dentry(struct dentry *dentry, __u32 mask)
David Brazdil0f672f62019-12-10 10:32:29 +000079{
Olivier Deprez157378f2022-04-04 15:47:50 +020080 fsnotify_parent(dentry, mask, d_inode(dentry), FSNOTIFY_EVENT_INODE);
81}
David Brazdil0f672f62019-12-10 10:32:29 +000082
Olivier Deprez157378f2022-04-04 15:47:50 +020083static inline int fsnotify_file(struct file *file, __u32 mask)
84{
85 const struct path *path = &file->f_path;
86
87 if (file->f_mode & FMODE_NONOTIFY)
88 return 0;
89
90 return fsnotify_parent(path->dentry, mask, path, FSNOTIFY_EVENT_PATH);
David Brazdil0f672f62019-12-10 10:32:29 +000091}
92
93/* Simple call site for access decisions */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000094static inline int fsnotify_perm(struct file *file, int mask)
95{
David Brazdil0f672f62019-12-10 10:32:29 +000096 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000097 __u32 fsnotify_mask = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000098
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000099 if (!(mask & (MAY_READ | MAY_OPEN)))
100 return 0;
Olivier Deprez157378f2022-04-04 15:47:50 +0200101
David Brazdil0f672f62019-12-10 10:32:29 +0000102 if (mask & MAY_OPEN) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000103 fsnotify_mask = FS_OPEN_PERM;
David Brazdil0f672f62019-12-10 10:32:29 +0000104
105 if (file->f_flags & __FMODE_EXEC) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200106 ret = fsnotify_file(file, FS_OPEN_EXEC_PERM);
David Brazdil0f672f62019-12-10 10:32:29 +0000107
108 if (ret)
109 return ret;
110 }
111 } else if (mask & MAY_READ) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000112 fsnotify_mask = FS_ACCESS_PERM;
David Brazdil0f672f62019-12-10 10:32:29 +0000113 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000114
Olivier Deprez157378f2022-04-04 15:47:50 +0200115 return fsnotify_file(file, fsnotify_mask);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000116}
117
118/*
119 * fsnotify_link_count - inode's link count changed
120 */
121static inline void fsnotify_link_count(struct inode *inode)
122{
Olivier Deprez157378f2022-04-04 15:47:50 +0200123 fsnotify_inode(inode, FS_ATTRIB);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000124}
125
126/*
127 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
128 */
129static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
David Brazdil0f672f62019-12-10 10:32:29 +0000130 const struct qstr *old_name,
131 int isdir, struct inode *target,
132 struct dentry *moved)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000133{
134 struct inode *source = moved->d_inode;
135 u32 fs_cookie = fsnotify_get_cookie();
David Brazdil0f672f62019-12-10 10:32:29 +0000136 __u32 old_dir_mask = FS_MOVED_FROM;
137 __u32 new_dir_mask = FS_MOVED_TO;
David Brazdil0f672f62019-12-10 10:32:29 +0000138 const struct qstr *new_name = &moved->d_name;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000139
140 if (old_dir == new_dir)
141 old_dir_mask |= FS_DN_RENAME;
142
143 if (isdir) {
144 old_dir_mask |= FS_ISDIR;
145 new_dir_mask |= FS_ISDIR;
146 }
147
Olivier Deprez157378f2022-04-04 15:47:50 +0200148 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);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000150
151 if (target)
152 fsnotify_link_count(target);
Olivier Deprez157378f2022-04-04 15:47:50 +0200153 fsnotify_inode(source, FS_MOVE_SELF);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000154 audit_inode_child(new_dir, moved, AUDIT_TYPE_CHILD_CREATE);
155}
156
157/*
158 * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
159 */
160static inline void fsnotify_inode_delete(struct inode *inode)
161{
162 __fsnotify_inode_delete(inode);
163}
164
165/*
166 * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
167 */
168static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt)
169{
170 __fsnotify_vfsmount_delete(mnt);
171}
172
173/*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000174 * fsnotify_inoderemove - an inode is going away
175 */
176static inline void fsnotify_inoderemove(struct inode *inode)
177{
Olivier Deprez157378f2022-04-04 15:47:50 +0200178 fsnotify_inode(inode, FS_DELETE_SELF);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000179 __fsnotify_inode_delete(inode);
180}
181
182/*
183 * fsnotify_create - 'name' was linked in
184 */
185static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
186{
187 audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
188
David Brazdil0f672f62019-12-10 10:32:29 +0000189 fsnotify_dirent(inode, dentry, FS_CREATE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000190}
191
192/*
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
196 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200197static inline void fsnotify_link(struct inode *dir, struct inode *inode,
198 struct dentry *new_dentry)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000199{
200 fsnotify_link_count(inode);
201 audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE);
202
Olivier Deprez157378f2022-04-04 15:47:50 +0200203 fsnotify_name(dir, FS_CREATE, inode, &new_dentry->d_name, 0);
204}
205
206/*
207 * fsnotify_delete - @dentry was unlinked and unhashed
208 *
209 * Caller must make sure that dentry->d_name is stable.
210 *
211 * Note: unlike fsnotify_unlink(), we have to pass also the unlinked inode
212 * as this may be called after d_delete() and old_dentry may be negative.
213 */
214static inline void fsnotify_delete(struct inode *dir, struct inode *inode,
215 struct dentry *dentry)
216{
217 __u32 mask = FS_DELETE;
218
219 if (S_ISDIR(inode->i_mode))
220 mask |= FS_ISDIR;
221
222 fsnotify_name(dir, mask, inode, &dentry->d_name, 0);
223}
224
225/**
226 * d_delete_notify - delete a dentry and call fsnotify_delete()
227 * @dentry: The dentry to delete
228 *
229 * This helper is used to guaranty that the unlinked inode cannot be found
230 * by lookup of this name after fsnotify_delete() event has been delivered.
231 */
232static inline void d_delete_notify(struct inode *dir, struct dentry *dentry)
233{
234 struct inode *inode = d_inode(dentry);
235
236 ihold(inode);
237 d_delete(dentry);
238 fsnotify_delete(dir, inode, dentry);
239 iput(inode);
David Brazdil0f672f62019-12-10 10:32:29 +0000240}
241
242/*
243 * fsnotify_unlink - 'name' was unlinked
244 *
245 * Caller must make sure that dentry->d_name is stable.
246 */
247static inline void fsnotify_unlink(struct inode *dir, struct dentry *dentry)
248{
Olivier Deprez157378f2022-04-04 15:47:50 +0200249 if (WARN_ON_ONCE(d_is_negative(dentry)))
250 return;
David Brazdil0f672f62019-12-10 10:32:29 +0000251
Olivier Deprez157378f2022-04-04 15:47:50 +0200252 fsnotify_delete(dir, d_inode(dentry), dentry);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000253}
254
255/*
256 * fsnotify_mkdir - directory 'name' was created
257 */
258static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
259{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000260 audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
261
David Brazdil0f672f62019-12-10 10:32:29 +0000262 fsnotify_dirent(inode, dentry, FS_CREATE | FS_ISDIR);
263}
264
265/*
266 * fsnotify_rmdir - directory 'name' was removed
267 *
268 * Caller must make sure that dentry->d_name is stable.
269 */
270static inline void fsnotify_rmdir(struct inode *dir, struct dentry *dentry)
271{
Olivier Deprez157378f2022-04-04 15:47:50 +0200272 if (WARN_ON_ONCE(d_is_negative(dentry)))
273 return;
David Brazdil0f672f62019-12-10 10:32:29 +0000274
Olivier Deprez157378f2022-04-04 15:47:50 +0200275 fsnotify_delete(dir, d_inode(dentry), dentry);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000276}
277
278/*
279 * fsnotify_access - file was read
280 */
281static inline void fsnotify_access(struct file *file)
282{
Olivier Deprez157378f2022-04-04 15:47:50 +0200283 fsnotify_file(file, FS_ACCESS);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000284}
285
286/*
287 * fsnotify_modify - file was modified
288 */
289static inline void fsnotify_modify(struct file *file)
290{
Olivier Deprez157378f2022-04-04 15:47:50 +0200291 fsnotify_file(file, FS_MODIFY);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000292}
293
294/*
295 * fsnotify_open - file was opened
296 */
297static inline void fsnotify_open(struct file *file)
298{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000299 __u32 mask = FS_OPEN;
300
David Brazdil0f672f62019-12-10 10:32:29 +0000301 if (file->f_flags & __FMODE_EXEC)
302 mask |= FS_OPEN_EXEC;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000303
Olivier Deprez157378f2022-04-04 15:47:50 +0200304 fsnotify_file(file, mask);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000305}
306
307/*
308 * fsnotify_close - file was closed
309 */
310static inline void fsnotify_close(struct file *file)
311{
Olivier Deprez157378f2022-04-04 15:47:50 +0200312 __u32 mask = (file->f_mode & FMODE_WRITE) ? FS_CLOSE_WRITE :
313 FS_CLOSE_NOWRITE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000314
Olivier Deprez157378f2022-04-04 15:47:50 +0200315 fsnotify_file(file, mask);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000316}
317
318/*
319 * fsnotify_xattr - extended attributes were changed
320 */
321static inline void fsnotify_xattr(struct dentry *dentry)
322{
Olivier Deprez157378f2022-04-04 15:47:50 +0200323 fsnotify_dentry(dentry, FS_ATTRIB);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000324}
325
326/*
327 * fsnotify_change - notify_change event. file was modified and/or metadata
328 * was changed.
329 */
330static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
331{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000332 __u32 mask = 0;
333
334 if (ia_valid & ATTR_UID)
335 mask |= FS_ATTRIB;
336 if (ia_valid & ATTR_GID)
337 mask |= FS_ATTRIB;
338 if (ia_valid & ATTR_SIZE)
339 mask |= FS_MODIFY;
340
341 /* both times implies a utime(s) call */
342 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
343 mask |= FS_ATTRIB;
344 else if (ia_valid & ATTR_ATIME)
345 mask |= FS_ACCESS;
346 else if (ia_valid & ATTR_MTIME)
347 mask |= FS_MODIFY;
348
349 if (ia_valid & ATTR_MODE)
350 mask |= FS_ATTRIB;
351
Olivier Deprez157378f2022-04-04 15:47:50 +0200352 if (mask)
353 fsnotify_dentry(dentry, mask);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000354}
355
356#endif /* _LINUX_FS_NOTIFY_H */