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