xref: /linux/include/linux/fsnotify.h (revision d6084bb815c453de27af8071a23163a711586a6c)
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 /* Are there any inode/mount/sb objects watched with priority prio or above? */
fsnotify_sb_has_priority_watchers(struct super_block * sb,int prio)21 static inline bool fsnotify_sb_has_priority_watchers(struct super_block *sb,
22 						     int prio)
23 {
24 	struct fsnotify_sb_info *sbinfo = fsnotify_sb_info(sb);
25 
26 	/* Were any marks ever added to any object on this sb? */
27 	if (!sbinfo)
28 		return false;
29 
30 	return atomic_long_read(&sbinfo->watched_objects[prio]);
31 }
32 
33 /* Are there any inode/mount/sb objects that are being watched at all? */
fsnotify_sb_has_watchers(struct super_block * sb)34 static inline bool fsnotify_sb_has_watchers(struct super_block *sb)
35 {
36 	return fsnotify_sb_has_priority_watchers(sb, 0);
37 }
38 
39 /*
40  * Notify this @dir inode about a change in a child directory entry.
41  * The directory entry may have turned positive or negative or its inode may
42  * have changed (i.e. renamed over).
43  *
44  * Unlike fsnotify_parent(), the event will be reported regardless of the
45  * FS_EVENT_ON_CHILD mask on the parent inode and will not be reported if only
46  * the child is interested and not the parent.
47  */
fsnotify_name(__u32 mask,const void * data,int data_type,struct inode * dir,const struct qstr * name,u32 cookie)48 static inline int fsnotify_name(__u32 mask, const void *data, int data_type,
49 				struct inode *dir, const struct qstr *name,
50 				u32 cookie)
51 {
52 	if (!fsnotify_sb_has_watchers(dir->i_sb))
53 		return 0;
54 
55 	return fsnotify(mask, data, data_type, dir, name, NULL, cookie);
56 }
57 
fsnotify_dirent(struct inode * dir,struct dentry * dentry,__u32 mask)58 static inline void fsnotify_dirent(struct inode *dir, struct dentry *dentry,
59 				   __u32 mask)
60 {
61 	fsnotify_name(mask, dentry, FSNOTIFY_EVENT_DENTRY, dir, &dentry->d_name, 0);
62 }
63 
fsnotify_inode(struct inode * inode,__u32 mask)64 static inline void fsnotify_inode(struct inode *inode, __u32 mask)
65 {
66 	if (!fsnotify_sb_has_watchers(inode->i_sb))
67 		return;
68 
69 	if (S_ISDIR(inode->i_mode))
70 		mask |= FS_ISDIR;
71 
72 	fsnotify(mask, inode, FSNOTIFY_EVENT_INODE, NULL, NULL, inode, 0);
73 }
74 
75 /* Notify this dentry's parent about a child's events. */
fsnotify_parent(struct dentry * dentry,__u32 mask,const void * data,int data_type)76 static inline int fsnotify_parent(struct dentry *dentry, __u32 mask,
77 				  const void *data, int data_type)
78 {
79 	struct inode *inode = d_inode(dentry);
80 
81 	if (!fsnotify_sb_has_watchers(inode->i_sb))
82 		return 0;
83 
84 	if (S_ISDIR(inode->i_mode)) {
85 		mask |= FS_ISDIR;
86 
87 		/* sb/mount marks are not interested in name of directory */
88 		if (!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED))
89 			goto notify_child;
90 	}
91 
92 	/* disconnected dentry cannot notify parent */
93 	if (IS_ROOT(dentry))
94 		goto notify_child;
95 
96 	return __fsnotify_parent(dentry, mask, data, data_type);
97 
98 notify_child:
99 	return fsnotify(mask, data, data_type, NULL, NULL, inode, 0);
100 }
101 
102 /*
103  * Simple wrappers to consolidate calls to fsnotify_parent() when an event
104  * is on a file/dentry.
105  */
fsnotify_dentry(struct dentry * dentry,__u32 mask)106 static inline void fsnotify_dentry(struct dentry *dentry, __u32 mask)
107 {
108 	fsnotify_parent(dentry, mask, dentry, FSNOTIFY_EVENT_DENTRY);
109 }
110 
fsnotify_path(const struct path * path,__u32 mask)111 static inline int fsnotify_path(const struct path *path, __u32 mask)
112 {
113 	return fsnotify_parent(path->dentry, mask, path, FSNOTIFY_EVENT_PATH);
114 }
115 
fsnotify_file(struct file * file,__u32 mask)116 static inline int fsnotify_file(struct file *file, __u32 mask)
117 {
118 	/*
119 	 * FMODE_NONOTIFY are fds generated by fanotify itself which should not
120 	 * generate new events. We also don't want to generate events for
121 	 * FMODE_PATH fds (involves open & close events) as they are just
122 	 * handle creation / destruction events and not "real" file events.
123 	 */
124 	if (FMODE_FSNOTIFY_NONE(file->f_mode))
125 		return 0;
126 
127 	return fsnotify_path(&file->f_path, mask);
128 }
129 
130 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
131 
132 int fsnotify_open_perm_and_set_mode(struct file *file);
133 
134 /*
135  * fsnotify_file_area_perm - permission hook before access to file range
136  */
fsnotify_file_area_perm(struct file * file,int perm_mask,const loff_t * ppos,size_t count)137 static inline int fsnotify_file_area_perm(struct file *file, int perm_mask,
138 					  const loff_t *ppos, size_t count)
139 {
140 	/*
141 	 * filesystem may be modified in the context of permission events
142 	 * (e.g. by HSM filling a file on access), so sb freeze protection
143 	 * must not be held.
144 	 */
145 	lockdep_assert_once(file_write_not_started(file));
146 
147 	if (!(perm_mask & (MAY_READ | MAY_WRITE | MAY_ACCESS)))
148 		return 0;
149 
150 	/*
151 	 * read()/write() and other types of access generate pre-content events.
152 	 */
153 	if (unlikely(FMODE_FSNOTIFY_HSM(file->f_mode))) {
154 		int ret = fsnotify_pre_content(&file->f_path, ppos, count);
155 
156 		if (ret)
157 			return ret;
158 	}
159 
160 	if (!(perm_mask & MAY_READ) ||
161 	    likely(!FMODE_FSNOTIFY_ACCESS_PERM(file->f_mode)))
162 		return 0;
163 
164 	/*
165 	 * read() also generates the legacy FS_ACCESS_PERM event, so content
166 	 * scanners can inspect the content filled by pre-content event.
167 	 */
168 	return fsnotify_path(&file->f_path, FS_ACCESS_PERM);
169 }
170 
171 /*
172  * fsnotify_mmap_perm - permission hook before mmap of file range
173  */
fsnotify_mmap_perm(struct file * file,int prot,const loff_t off,size_t len)174 static inline int fsnotify_mmap_perm(struct file *file, int prot,
175 				     const loff_t off, size_t len)
176 {
177 	/*
178 	 * mmap() generates only pre-content events.
179 	 */
180 	if (!file || likely(!FMODE_FSNOTIFY_HSM(file->f_mode)))
181 		return 0;
182 
183 	return fsnotify_pre_content(&file->f_path, &off, len);
184 }
185 
186 /*
187  * fsnotify_truncate_perm - permission hook before file truncate
188  */
fsnotify_truncate_perm(const struct path * path,loff_t length)189 static inline int fsnotify_truncate_perm(const struct path *path, loff_t length)
190 {
191 	struct inode *inode = d_inode(path->dentry);
192 
193 	if (!(inode->i_sb->s_iflags & SB_I_ALLOW_HSM) ||
194 	    !fsnotify_sb_has_priority_watchers(inode->i_sb,
195 					       FSNOTIFY_PRIO_PRE_CONTENT))
196 		return 0;
197 
198 	return fsnotify_pre_content(path, &length, 0);
199 }
200 
201 /*
202  * fsnotify_file_perm - permission hook before file access (unknown range)
203  */
fsnotify_file_perm(struct file * file,int perm_mask)204 static inline int fsnotify_file_perm(struct file *file, int perm_mask)
205 {
206 	return fsnotify_file_area_perm(file, perm_mask, NULL, 0);
207 }
208 
209 #else
fsnotify_open_perm_and_set_mode(struct file * file)210 static inline int fsnotify_open_perm_and_set_mode(struct file *file)
211 {
212 	return 0;
213 }
214 
fsnotify_file_area_perm(struct file * file,int perm_mask,const loff_t * ppos,size_t count)215 static inline int fsnotify_file_area_perm(struct file *file, int perm_mask,
216 					  const loff_t *ppos, size_t count)
217 {
218 	return 0;
219 }
220 
fsnotify_mmap_perm(struct file * file,int prot,const loff_t off,size_t len)221 static inline int fsnotify_mmap_perm(struct file *file, int prot,
222 				     const loff_t off, size_t len)
223 {
224 	return 0;
225 }
226 
fsnotify_truncate_perm(const struct path * path,loff_t length)227 static inline int fsnotify_truncate_perm(const struct path *path, loff_t length)
228 {
229 	return 0;
230 }
231 
fsnotify_file_perm(struct file * file,int perm_mask)232 static inline int fsnotify_file_perm(struct file *file, int perm_mask)
233 {
234 	return 0;
235 }
236 #endif
237 
238 /*
239  * fsnotify_link_count - inode's link count changed
240  */
fsnotify_link_count(struct inode * inode)241 static inline void fsnotify_link_count(struct inode *inode)
242 {
243 	fsnotify_inode(inode, FS_ATTRIB);
244 }
245 
246 /*
247  * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
248  */
fsnotify_move(struct inode * old_dir,struct inode * new_dir,const struct qstr * old_name,int isdir,struct inode * target,struct dentry * moved)249 static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
250 				 const struct qstr *old_name,
251 				 int isdir, struct inode *target,
252 				 struct dentry *moved)
253 {
254 	struct inode *source = moved->d_inode;
255 	u32 fs_cookie = fsnotify_get_cookie();
256 	__u32 old_dir_mask = FS_MOVED_FROM;
257 	__u32 new_dir_mask = FS_MOVED_TO;
258 	__u32 rename_mask = FS_RENAME;
259 	const struct qstr *new_name = &moved->d_name;
260 
261 	if (isdir) {
262 		old_dir_mask |= FS_ISDIR;
263 		new_dir_mask |= FS_ISDIR;
264 		rename_mask |= FS_ISDIR;
265 	}
266 
267 	/* Event with information about both old and new parent+name */
268 	fsnotify_name(rename_mask, moved, FSNOTIFY_EVENT_DENTRY,
269 		      old_dir, old_name, 0);
270 
271 	fsnotify_name(old_dir_mask, source, FSNOTIFY_EVENT_INODE,
272 		      old_dir, old_name, fs_cookie);
273 	fsnotify_name(new_dir_mask, source, FSNOTIFY_EVENT_INODE,
274 		      new_dir, new_name, fs_cookie);
275 
276 	if (target)
277 		fsnotify_link_count(target);
278 	fsnotify_inode(source, FS_MOVE_SELF);
279 	audit_inode_child(new_dir, moved, AUDIT_TYPE_CHILD_CREATE);
280 }
281 
282 /*
283  * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
284  */
fsnotify_inode_delete(struct inode * inode)285 static inline void fsnotify_inode_delete(struct inode *inode)
286 {
287 	__fsnotify_inode_delete(inode);
288 }
289 
290 /*
291  * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
292  */
fsnotify_vfsmount_delete(struct vfsmount * mnt)293 static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt)
294 {
295 	__fsnotify_vfsmount_delete(mnt);
296 }
297 
fsnotify_mntns_delete(struct mnt_namespace * mntns)298 static inline void fsnotify_mntns_delete(struct mnt_namespace *mntns)
299 {
300 	__fsnotify_mntns_delete(mntns);
301 }
302 
303 /*
304  * fsnotify_inoderemove - an inode is going away
305  */
fsnotify_inoderemove(struct inode * inode)306 static inline void fsnotify_inoderemove(struct inode *inode)
307 {
308 	fsnotify_inode(inode, FS_DELETE_SELF);
309 	__fsnotify_inode_delete(inode);
310 }
311 
312 /*
313  * fsnotify_create - 'name' was linked in
314  *
315  * Caller must make sure that dentry->d_name is stable.
316  * Note: some filesystems (e.g. kernfs) leave @dentry negative and instantiate
317  * ->d_inode later
318  */
fsnotify_create(struct inode * dir,struct dentry * dentry)319 static inline void fsnotify_create(struct inode *dir, struct dentry *dentry)
320 {
321 	audit_inode_child(dir, dentry, AUDIT_TYPE_CHILD_CREATE);
322 
323 	fsnotify_dirent(dir, dentry, FS_CREATE);
324 }
325 
326 /*
327  * fsnotify_link - new hardlink in 'inode' directory
328  *
329  * Caller must make sure that new_dentry->d_name is stable.
330  * Note: We have to pass also the linked inode ptr as some filesystems leave
331  *   new_dentry->d_inode NULL and instantiate inode pointer later
332  */
fsnotify_link(struct inode * dir,struct inode * inode,struct dentry * new_dentry)333 static inline void fsnotify_link(struct inode *dir, struct inode *inode,
334 				 struct dentry *new_dentry)
335 {
336 	fsnotify_link_count(inode);
337 	audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE);
338 
339 	fsnotify_name(FS_CREATE, inode, FSNOTIFY_EVENT_INODE,
340 		      dir, &new_dentry->d_name, 0);
341 }
342 
343 /*
344  * fsnotify_delete - @dentry was unlinked and unhashed
345  *
346  * Caller must make sure that dentry->d_name is stable.
347  *
348  * Note: unlike fsnotify_unlink(), we have to pass also the unlinked inode
349  * as this may be called after d_delete() and old_dentry may be negative.
350  */
fsnotify_delete(struct inode * dir,struct inode * inode,struct dentry * dentry)351 static inline void fsnotify_delete(struct inode *dir, struct inode *inode,
352 				   struct dentry *dentry)
353 {
354 	__u32 mask = FS_DELETE;
355 
356 	if (S_ISDIR(inode->i_mode))
357 		mask |= FS_ISDIR;
358 
359 	fsnotify_name(mask, inode, FSNOTIFY_EVENT_INODE, dir, &dentry->d_name,
360 		      0);
361 }
362 
363 /**
364  * d_delete_notify - delete a dentry and call fsnotify_delete()
365  * @dentry: The dentry to delete
366  *
367  * This helper is used to guaranty that the unlinked inode cannot be found
368  * by lookup of this name after fsnotify_delete() event has been delivered.
369  */
d_delete_notify(struct inode * dir,struct dentry * dentry)370 static inline void d_delete_notify(struct inode *dir, struct dentry *dentry)
371 {
372 	struct inode *inode = d_inode(dentry);
373 
374 	ihold(inode);
375 	d_delete(dentry);
376 	fsnotify_delete(dir, inode, dentry);
377 	iput(inode);
378 }
379 
380 /*
381  * fsnotify_unlink - 'name' was unlinked
382  *
383  * Caller must make sure that dentry->d_name is stable.
384  */
fsnotify_unlink(struct inode * dir,struct dentry * dentry)385 static inline void fsnotify_unlink(struct inode *dir, struct dentry *dentry)
386 {
387 	if (WARN_ON_ONCE(d_is_negative(dentry)))
388 		return;
389 
390 	fsnotify_delete(dir, d_inode(dentry), dentry);
391 }
392 
393 /*
394  * fsnotify_mkdir - directory 'name' was created
395  *
396  * Caller must make sure that dentry->d_name is stable.
397  * Note: some filesystems (e.g. kernfs) leave @dentry negative and instantiate
398  * ->d_inode later
399  */
fsnotify_mkdir(struct inode * dir,struct dentry * dentry)400 static inline void fsnotify_mkdir(struct inode *dir, struct dentry *dentry)
401 {
402 	audit_inode_child(dir, dentry, AUDIT_TYPE_CHILD_CREATE);
403 
404 	fsnotify_dirent(dir, dentry, FS_CREATE | FS_ISDIR);
405 }
406 
407 /*
408  * fsnotify_rmdir - directory 'name' was removed
409  *
410  * Caller must make sure that dentry->d_name is stable.
411  */
fsnotify_rmdir(struct inode * dir,struct dentry * dentry)412 static inline void fsnotify_rmdir(struct inode *dir, struct dentry *dentry)
413 {
414 	if (WARN_ON_ONCE(d_is_negative(dentry)))
415 		return;
416 
417 	fsnotify_delete(dir, d_inode(dentry), dentry);
418 }
419 
420 /*
421  * fsnotify_access - file was read
422  */
fsnotify_access(struct file * file)423 static inline void fsnotify_access(struct file *file)
424 {
425 	fsnotify_file(file, FS_ACCESS);
426 }
427 
428 /*
429  * fsnotify_modify - file was modified
430  */
fsnotify_modify(struct file * file)431 static inline void fsnotify_modify(struct file *file)
432 {
433 	fsnotify_file(file, FS_MODIFY);
434 }
435 
436 /*
437  * fsnotify_open - file was opened
438  */
fsnotify_open(struct file * file)439 static inline void fsnotify_open(struct file *file)
440 {
441 	__u32 mask = FS_OPEN;
442 
443 	if (file->f_flags & __FMODE_EXEC)
444 		mask |= FS_OPEN_EXEC;
445 
446 	fsnotify_file(file, mask);
447 }
448 
449 /*
450  * fsnotify_close - file was closed
451  */
fsnotify_close(struct file * file)452 static inline void fsnotify_close(struct file *file)
453 {
454 	__u32 mask = (file->f_mode & FMODE_WRITE) ? FS_CLOSE_WRITE :
455 						    FS_CLOSE_NOWRITE;
456 
457 	fsnotify_file(file, mask);
458 }
459 
460 /*
461  * fsnotify_xattr - extended attributes were changed
462  */
fsnotify_xattr(struct dentry * dentry)463 static inline void fsnotify_xattr(struct dentry *dentry)
464 {
465 	fsnotify_dentry(dentry, FS_ATTRIB);
466 }
467 
468 /*
469  * fsnotify_change - notify_change event.  file was modified and/or metadata
470  * was changed.
471  */
fsnotify_change(struct dentry * dentry,unsigned int ia_valid)472 static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
473 {
474 	__u32 mask = 0;
475 
476 	if (ia_valid & ATTR_UID)
477 		mask |= FS_ATTRIB;
478 	if (ia_valid & ATTR_GID)
479 		mask |= FS_ATTRIB;
480 	if (ia_valid & ATTR_SIZE)
481 		mask |= FS_MODIFY;
482 
483 	/* both times implies a utime(s) call */
484 	if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
485 		mask |= FS_ATTRIB;
486 	else if (ia_valid & ATTR_ATIME)
487 		mask |= FS_ACCESS;
488 	else if (ia_valid & ATTR_MTIME)
489 		mask |= FS_MODIFY;
490 
491 	if (ia_valid & ATTR_MODE)
492 		mask |= FS_ATTRIB;
493 
494 	if (mask)
495 		fsnotify_dentry(dentry, mask);
496 }
497 
fsnotify_sb_error(struct super_block * sb,struct inode * inode,int error)498 static inline int fsnotify_sb_error(struct super_block *sb, struct inode *inode,
499 				    int error)
500 {
501 	struct fs_error_report report = {
502 		.error = error,
503 		.inode = inode,
504 		.sb = sb,
505 	};
506 
507 	return fsnotify(FS_ERROR, &report, FSNOTIFY_EVENT_ERROR,
508 			NULL, NULL, NULL, 0);
509 }
510 
fsnotify_mnt_attach(struct mnt_namespace * ns,struct vfsmount * mnt)511 static inline void fsnotify_mnt_attach(struct mnt_namespace *ns, struct vfsmount *mnt)
512 {
513 	fsnotify_mnt(FS_MNT_ATTACH, ns, mnt);
514 }
515 
fsnotify_mnt_detach(struct mnt_namespace * ns,struct vfsmount * mnt)516 static inline void fsnotify_mnt_detach(struct mnt_namespace *ns, struct vfsmount *mnt)
517 {
518 	fsnotify_mnt(FS_MNT_DETACH, ns, mnt);
519 }
520 
fsnotify_mnt_move(struct mnt_namespace * ns,struct vfsmount * mnt)521 static inline void fsnotify_mnt_move(struct mnt_namespace *ns, struct vfsmount *mnt)
522 {
523 	fsnotify_mnt(FS_MNT_MOVE, ns, mnt);
524 }
525 
526 #endif	/* _LINUX_FS_NOTIFY_H */
527