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