xref: /linux-6.15/include/linux/fsnotify.h (revision 36e28c42)
1 /* 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 
20 /*
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).
24  *
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.
28  */
29 static inline int fsnotify_name(__u32 mask, const void *data, int data_type,
30 				struct inode *dir, const struct qstr *name,
31 				u32 cookie)
32 {
33 	if (atomic_long_read(&dir->i_sb->s_fsnotify_connectors) == 0)
34 		return 0;
35 
36 	return fsnotify(mask, data, data_type, dir, name, NULL, cookie);
37 }
38 
39 static inline void fsnotify_dirent(struct inode *dir, struct dentry *dentry,
40 				   __u32 mask)
41 {
42 	fsnotify_name(mask, dentry, FSNOTIFY_EVENT_DENTRY, dir, &dentry->d_name, 0);
43 }
44 
45 static inline void fsnotify_inode(struct inode *inode, __u32 mask)
46 {
47 	if (atomic_long_read(&inode->i_sb->s_fsnotify_connectors) == 0)
48 		return;
49 
50 	if (S_ISDIR(inode->i_mode))
51 		mask |= FS_ISDIR;
52 
53 	fsnotify(mask, inode, FSNOTIFY_EVENT_INODE, NULL, NULL, inode, 0);
54 }
55 
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)
59 {
60 	struct inode *inode = d_inode(dentry);
61 
62 	if (atomic_long_read(&inode->i_sb->s_fsnotify_connectors) == 0)
63 		return 0;
64 
65 	if (S_ISDIR(inode->i_mode)) {
66 		mask |= FS_ISDIR;
67 
68 		/* sb/mount marks are not interested in name of directory */
69 		if (!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED))
70 			goto notify_child;
71 	}
72 
73 	/* disconnected dentry cannot notify parent */
74 	if (IS_ROOT(dentry))
75 		goto notify_child;
76 
77 	return __fsnotify_parent(dentry, mask, data, data_type);
78 
79 notify_child:
80 	return fsnotify(mask, data, data_type, NULL, NULL, inode, 0);
81 }
82 
83 /*
84  * Simple wrappers to consolidate calls to fsnotify_parent() when an event
85  * is on a file/dentry.
86  */
87 static inline void fsnotify_dentry(struct dentry *dentry, __u32 mask)
88 {
89 	fsnotify_parent(dentry, mask, dentry, FSNOTIFY_EVENT_DENTRY);
90 }
91 
92 static inline int fsnotify_file(struct file *file, __u32 mask)
93 {
94 	const struct path *path;
95 
96 	if (file->f_mode & FMODE_NONOTIFY)
97 		return 0;
98 
99 	path = &file->f_path;
100 	return fsnotify_parent(path->dentry, mask, path, FSNOTIFY_EVENT_PATH);
101 }
102 
103 /*
104  * fsnotify_file_perm - permission hook before file access
105  */
106 static inline int fsnotify_file_perm(struct file *file, int perm_mask)
107 {
108 	__u32 fsnotify_mask = FS_ACCESS_PERM;
109 
110 	if (!(perm_mask & MAY_READ))
111 		return 0;
112 
113 	return fsnotify_file(file, fsnotify_mask);
114 }
115 
116 /*
117  * fsnotify_open_perm - permission hook before file open
118  */
119 static inline int fsnotify_open_perm(struct file *file)
120 {
121 	int ret;
122 
123 	if (file->f_flags & __FMODE_EXEC) {
124 		ret = fsnotify_file(file, FS_OPEN_EXEC_PERM);
125 		if (ret)
126 			return ret;
127 	}
128 
129 	return fsnotify_file(file, FS_OPEN_PERM);
130 }
131 
132 /*
133  * fsnotify_link_count - inode's link count changed
134  */
135 static inline void fsnotify_link_count(struct inode *inode)
136 {
137 	fsnotify_inode(inode, FS_ATTRIB);
138 }
139 
140 /*
141  * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
142  */
143 static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
144 				 const struct qstr *old_name,
145 				 int isdir, struct inode *target,
146 				 struct dentry *moved)
147 {
148 	struct inode *source = moved->d_inode;
149 	u32 fs_cookie = fsnotify_get_cookie();
150 	__u32 old_dir_mask = FS_MOVED_FROM;
151 	__u32 new_dir_mask = FS_MOVED_TO;
152 	__u32 rename_mask = FS_RENAME;
153 	const struct qstr *new_name = &moved->d_name;
154 
155 	if (isdir) {
156 		old_dir_mask |= FS_ISDIR;
157 		new_dir_mask |= FS_ISDIR;
158 		rename_mask |= FS_ISDIR;
159 	}
160 
161 	/* Event with information about both old and new parent+name */
162 	fsnotify_name(rename_mask, moved, FSNOTIFY_EVENT_DENTRY,
163 		      old_dir, old_name, 0);
164 
165 	fsnotify_name(old_dir_mask, source, FSNOTIFY_EVENT_INODE,
166 		      old_dir, old_name, fs_cookie);
167 	fsnotify_name(new_dir_mask, source, FSNOTIFY_EVENT_INODE,
168 		      new_dir, new_name, fs_cookie);
169 
170 	if (target)
171 		fsnotify_link_count(target);
172 	fsnotify_inode(source, FS_MOVE_SELF);
173 	audit_inode_child(new_dir, moved, AUDIT_TYPE_CHILD_CREATE);
174 }
175 
176 /*
177  * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
178  */
179 static inline void fsnotify_inode_delete(struct inode *inode)
180 {
181 	__fsnotify_inode_delete(inode);
182 }
183 
184 /*
185  * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
186  */
187 static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt)
188 {
189 	__fsnotify_vfsmount_delete(mnt);
190 }
191 
192 /*
193  * fsnotify_inoderemove - an inode is going away
194  */
195 static inline void fsnotify_inoderemove(struct inode *inode)
196 {
197 	fsnotify_inode(inode, FS_DELETE_SELF);
198 	__fsnotify_inode_delete(inode);
199 }
200 
201 /*
202  * fsnotify_create - 'name' was linked in
203  *
204  * Caller must make sure that dentry->d_name is stable.
205  * Note: some filesystems (e.g. kernfs) leave @dentry negative and instantiate
206  * ->d_inode later
207  */
208 static inline void fsnotify_create(struct inode *dir, struct dentry *dentry)
209 {
210 	audit_inode_child(dir, dentry, AUDIT_TYPE_CHILD_CREATE);
211 
212 	fsnotify_dirent(dir, dentry, FS_CREATE);
213 }
214 
215 /*
216  * fsnotify_link - new hardlink in 'inode' directory
217  *
218  * Caller must make sure that new_dentry->d_name is stable.
219  * Note: We have to pass also the linked inode ptr as some filesystems leave
220  *   new_dentry->d_inode NULL and instantiate inode pointer later
221  */
222 static inline void fsnotify_link(struct inode *dir, struct inode *inode,
223 				 struct dentry *new_dentry)
224 {
225 	fsnotify_link_count(inode);
226 	audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE);
227 
228 	fsnotify_name(FS_CREATE, inode, FSNOTIFY_EVENT_INODE,
229 		      dir, &new_dentry->d_name, 0);
230 }
231 
232 /*
233  * fsnotify_delete - @dentry was unlinked and unhashed
234  *
235  * Caller must make sure that dentry->d_name is stable.
236  *
237  * Note: unlike fsnotify_unlink(), we have to pass also the unlinked inode
238  * as this may be called after d_delete() and old_dentry may be negative.
239  */
240 static inline void fsnotify_delete(struct inode *dir, struct inode *inode,
241 				   struct dentry *dentry)
242 {
243 	__u32 mask = FS_DELETE;
244 
245 	if (S_ISDIR(inode->i_mode))
246 		mask |= FS_ISDIR;
247 
248 	fsnotify_name(mask, inode, FSNOTIFY_EVENT_INODE, dir, &dentry->d_name,
249 		      0);
250 }
251 
252 /**
253  * d_delete_notify - delete a dentry and call fsnotify_delete()
254  * @dentry: The dentry to delete
255  *
256  * This helper is used to guaranty that the unlinked inode cannot be found
257  * by lookup of this name after fsnotify_delete() event has been delivered.
258  */
259 static inline void d_delete_notify(struct inode *dir, struct dentry *dentry)
260 {
261 	struct inode *inode = d_inode(dentry);
262 
263 	ihold(inode);
264 	d_delete(dentry);
265 	fsnotify_delete(dir, inode, dentry);
266 	iput(inode);
267 }
268 
269 /*
270  * fsnotify_unlink - 'name' was unlinked
271  *
272  * Caller must make sure that dentry->d_name is stable.
273  */
274 static inline void fsnotify_unlink(struct inode *dir, struct dentry *dentry)
275 {
276 	if (WARN_ON_ONCE(d_is_negative(dentry)))
277 		return;
278 
279 	fsnotify_delete(dir, d_inode(dentry), dentry);
280 }
281 
282 /*
283  * fsnotify_mkdir - directory 'name' was created
284  *
285  * Caller must make sure that dentry->d_name is stable.
286  * Note: some filesystems (e.g. kernfs) leave @dentry negative and instantiate
287  * ->d_inode later
288  */
289 static inline void fsnotify_mkdir(struct inode *dir, struct dentry *dentry)
290 {
291 	audit_inode_child(dir, dentry, AUDIT_TYPE_CHILD_CREATE);
292 
293 	fsnotify_dirent(dir, dentry, FS_CREATE | FS_ISDIR);
294 }
295 
296 /*
297  * fsnotify_rmdir - directory 'name' was removed
298  *
299  * Caller must make sure that dentry->d_name is stable.
300  */
301 static inline void fsnotify_rmdir(struct inode *dir, struct dentry *dentry)
302 {
303 	if (WARN_ON_ONCE(d_is_negative(dentry)))
304 		return;
305 
306 	fsnotify_delete(dir, d_inode(dentry), dentry);
307 }
308 
309 /*
310  * fsnotify_access - file was read
311  */
312 static inline void fsnotify_access(struct file *file)
313 {
314 	fsnotify_file(file, FS_ACCESS);
315 }
316 
317 /*
318  * fsnotify_modify - file was modified
319  */
320 static inline void fsnotify_modify(struct file *file)
321 {
322 	fsnotify_file(file, FS_MODIFY);
323 }
324 
325 /*
326  * fsnotify_open - file was opened
327  */
328 static inline void fsnotify_open(struct file *file)
329 {
330 	__u32 mask = FS_OPEN;
331 
332 	if (file->f_flags & __FMODE_EXEC)
333 		mask |= FS_OPEN_EXEC;
334 
335 	fsnotify_file(file, mask);
336 }
337 
338 /*
339  * fsnotify_close - file was closed
340  */
341 static inline void fsnotify_close(struct file *file)
342 {
343 	__u32 mask = (file->f_mode & FMODE_WRITE) ? FS_CLOSE_WRITE :
344 						    FS_CLOSE_NOWRITE;
345 
346 	fsnotify_file(file, mask);
347 }
348 
349 /*
350  * fsnotify_xattr - extended attributes were changed
351  */
352 static inline void fsnotify_xattr(struct dentry *dentry)
353 {
354 	fsnotify_dentry(dentry, FS_ATTRIB);
355 }
356 
357 /*
358  * fsnotify_change - notify_change event.  file was modified and/or metadata
359  * was changed.
360  */
361 static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
362 {
363 	__u32 mask = 0;
364 
365 	if (ia_valid & ATTR_UID)
366 		mask |= FS_ATTRIB;
367 	if (ia_valid & ATTR_GID)
368 		mask |= FS_ATTRIB;
369 	if (ia_valid & ATTR_SIZE)
370 		mask |= FS_MODIFY;
371 
372 	/* both times implies a utime(s) call */
373 	if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
374 		mask |= FS_ATTRIB;
375 	else if (ia_valid & ATTR_ATIME)
376 		mask |= FS_ACCESS;
377 	else if (ia_valid & ATTR_MTIME)
378 		mask |= FS_MODIFY;
379 
380 	if (ia_valid & ATTR_MODE)
381 		mask |= FS_ATTRIB;
382 
383 	if (mask)
384 		fsnotify_dentry(dentry, mask);
385 }
386 
387 static inline int fsnotify_sb_error(struct super_block *sb, struct inode *inode,
388 				    int error)
389 {
390 	struct fs_error_report report = {
391 		.error = error,
392 		.inode = inode,
393 		.sb = sb,
394 	};
395 
396 	return fsnotify(FS_ERROR, &report, FSNOTIFY_EVENT_ERROR,
397 			NULL, NULL, NULL, 0);
398 }
399 
400 #endif	/* _LINUX_FS_NOTIFY_H */
401