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