xref: /linux-6.15/include/linux/fsnotify.h (revision e00a844a)
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 /* 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)
22 {
23 	if (!dentry)
24 		dentry = path->dentry;
25 
26 	return __fsnotify_parent(path, dentry, mask);
27 }
28 
29 /* simple call site for access decisions */
30 static inline int fsnotify_perm(struct file *file, int mask)
31 {
32 	const struct path *path = &file->f_path;
33 	/*
34 	 * Do not use file_inode() here or anywhere in this file to get the
35 	 * inode.  That would break *notity on overlayfs.
36 	 */
37 	struct inode *inode = path->dentry->d_inode;
38 	__u32 fsnotify_mask = 0;
39 	int ret;
40 
41 	if (file->f_mode & FMODE_NONOTIFY)
42 		return 0;
43 	if (!(mask & (MAY_READ | MAY_OPEN)))
44 		return 0;
45 	if (mask & MAY_OPEN)
46 		fsnotify_mask = FS_OPEN_PERM;
47 	else if (mask & MAY_READ)
48 		fsnotify_mask = FS_ACCESS_PERM;
49 	else
50 		BUG();
51 
52 	ret = fsnotify_parent(path, NULL, fsnotify_mask);
53 	if (ret)
54 		return ret;
55 
56 	return fsnotify(inode, fsnotify_mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
57 }
58 
59 /*
60  * fsnotify_link_count - inode's link count changed
61  */
62 static inline void fsnotify_link_count(struct inode *inode)
63 {
64 	fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
65 }
66 
67 /*
68  * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
69  */
70 static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
71 				 const unsigned char *old_name,
72 				 int isdir, struct inode *target, struct dentry *moved)
73 {
74 	struct inode *source = moved->d_inode;
75 	u32 fs_cookie = fsnotify_get_cookie();
76 	__u32 old_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_FROM);
77 	__u32 new_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_TO);
78 	const unsigned char *new_name = moved->d_name.name;
79 
80 	if (old_dir == new_dir)
81 		old_dir_mask |= FS_DN_RENAME;
82 
83 	if (isdir) {
84 		old_dir_mask |= FS_ISDIR;
85 		new_dir_mask |= FS_ISDIR;
86 	}
87 
88 	fsnotify(old_dir, old_dir_mask, source, FSNOTIFY_EVENT_INODE, old_name,
89 		 fs_cookie);
90 	fsnotify(new_dir, new_dir_mask, source, FSNOTIFY_EVENT_INODE, new_name,
91 		 fs_cookie);
92 
93 	if (target)
94 		fsnotify_link_count(target);
95 
96 	if (source)
97 		fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0);
98 	audit_inode_child(new_dir, moved, AUDIT_TYPE_CHILD_CREATE);
99 }
100 
101 /*
102  * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
103  */
104 static inline void fsnotify_inode_delete(struct inode *inode)
105 {
106 	__fsnotify_inode_delete(inode);
107 }
108 
109 /*
110  * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
111  */
112 static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt)
113 {
114 	__fsnotify_vfsmount_delete(mnt);
115 }
116 
117 /*
118  * fsnotify_nameremove - a filename was removed from a directory
119  */
120 static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
121 {
122 	__u32 mask = FS_DELETE;
123 
124 	if (isdir)
125 		mask |= FS_ISDIR;
126 
127 	fsnotify_parent(NULL, dentry, mask);
128 }
129 
130 /*
131  * fsnotify_inoderemove - an inode is going away
132  */
133 static inline void fsnotify_inoderemove(struct inode *inode)
134 {
135 	fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
136 	__fsnotify_inode_delete(inode);
137 }
138 
139 /*
140  * fsnotify_create - 'name' was linked in
141  */
142 static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
143 {
144 	audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
145 
146 	fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
147 }
148 
149 /*
150  * fsnotify_link - new hardlink in 'inode' directory
151  * Note: We have to pass also the linked inode ptr as some filesystems leave
152  *   new_dentry->d_inode NULL and instantiate inode pointer later
153  */
154 static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry)
155 {
156 	fsnotify_link_count(inode);
157 	audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE);
158 
159 	fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name, 0);
160 }
161 
162 /*
163  * fsnotify_mkdir - directory 'name' was created
164  */
165 static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
166 {
167 	__u32 mask = (FS_CREATE | FS_ISDIR);
168 	struct inode *d_inode = dentry->d_inode;
169 
170 	audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
171 
172 	fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
173 }
174 
175 /*
176  * fsnotify_access - file was read
177  */
178 static inline void fsnotify_access(struct file *file)
179 {
180 	const struct path *path = &file->f_path;
181 	struct inode *inode = path->dentry->d_inode;
182 	__u32 mask = FS_ACCESS;
183 
184 	if (S_ISDIR(inode->i_mode))
185 		mask |= FS_ISDIR;
186 
187 	if (!(file->f_mode & FMODE_NONOTIFY)) {
188 		fsnotify_parent(path, NULL, mask);
189 		fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
190 	}
191 }
192 
193 /*
194  * fsnotify_modify - file was modified
195  */
196 static inline void fsnotify_modify(struct file *file)
197 {
198 	const struct path *path = &file->f_path;
199 	struct inode *inode = path->dentry->d_inode;
200 	__u32 mask = FS_MODIFY;
201 
202 	if (S_ISDIR(inode->i_mode))
203 		mask |= FS_ISDIR;
204 
205 	if (!(file->f_mode & FMODE_NONOTIFY)) {
206 		fsnotify_parent(path, NULL, mask);
207 		fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
208 	}
209 }
210 
211 /*
212  * fsnotify_open - file was opened
213  */
214 static inline void fsnotify_open(struct file *file)
215 {
216 	const struct path *path = &file->f_path;
217 	struct inode *inode = path->dentry->d_inode;
218 	__u32 mask = FS_OPEN;
219 
220 	if (S_ISDIR(inode->i_mode))
221 		mask |= FS_ISDIR;
222 
223 	fsnotify_parent(path, NULL, mask);
224 	fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
225 }
226 
227 /*
228  * fsnotify_close - file was closed
229  */
230 static inline void fsnotify_close(struct file *file)
231 {
232 	const struct path *path = &file->f_path;
233 	struct inode *inode = path->dentry->d_inode;
234 	fmode_t mode = file->f_mode;
235 	__u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
236 
237 	if (S_ISDIR(inode->i_mode))
238 		mask |= FS_ISDIR;
239 
240 	if (!(file->f_mode & FMODE_NONOTIFY)) {
241 		fsnotify_parent(path, NULL, mask);
242 		fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
243 	}
244 }
245 
246 /*
247  * fsnotify_xattr - extended attributes were changed
248  */
249 static inline void fsnotify_xattr(struct dentry *dentry)
250 {
251 	struct inode *inode = dentry->d_inode;
252 	__u32 mask = FS_ATTRIB;
253 
254 	if (S_ISDIR(inode->i_mode))
255 		mask |= FS_ISDIR;
256 
257 	fsnotify_parent(NULL, dentry, mask);
258 	fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
259 }
260 
261 /*
262  * fsnotify_change - notify_change event.  file was modified and/or metadata
263  * was changed.
264  */
265 static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
266 {
267 	struct inode *inode = dentry->d_inode;
268 	__u32 mask = 0;
269 
270 	if (ia_valid & ATTR_UID)
271 		mask |= FS_ATTRIB;
272 	if (ia_valid & ATTR_GID)
273 		mask |= FS_ATTRIB;
274 	if (ia_valid & ATTR_SIZE)
275 		mask |= FS_MODIFY;
276 
277 	/* both times implies a utime(s) call */
278 	if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
279 		mask |= FS_ATTRIB;
280 	else if (ia_valid & ATTR_ATIME)
281 		mask |= FS_ACCESS;
282 	else if (ia_valid & ATTR_MTIME)
283 		mask |= FS_MODIFY;
284 
285 	if (ia_valid & ATTR_MODE)
286 		mask |= FS_ATTRIB;
287 
288 	if (mask) {
289 		if (S_ISDIR(inode->i_mode))
290 			mask |= FS_ISDIR;
291 
292 		fsnotify_parent(NULL, dentry, mask);
293 		fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
294 	}
295 }
296 
297 #endif	/* _LINUX_FS_NOTIFY_H */
298