Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
0eeca283 RL |
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 | ||
90586523 | 15 | #include <linux/fsnotify_backend.h> |
73241ccc | 16 | #include <linux/audit.h> |
5a0e3ad6 | 17 | #include <linux/slab.h> |
187f1882 | 18 | #include <linux/bug.h> |
0eeca283 | 19 | |
5f02a877 | 20 | /* |
a1aae057 AG |
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). | |
5f02a877 AG |
24 | * |
25 | * Unlike fsnotify_parent(), the event will be reported regardless of the | |
40a100d3 AG |
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. | |
5f02a877 | 28 | */ |
9baf93d6 AG |
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) | |
5f02a877 | 32 | { |
e43de7f0 | 33 | if (atomic_long_read(&dir->i_sb->s_fsnotify_connectors) == 0) |
9baf93d6 | 34 | return 0; |
e43de7f0 | 35 | |
9baf93d6 | 36 | return fsnotify(mask, data, data_type, dir, name, NULL, cookie); |
a1aae057 AG |
37 | } |
38 | ||
39 | static inline void fsnotify_dirent(struct inode *dir, struct dentry *dentry, | |
40 | __u32 mask) | |
41 | { | |
fd5a3ff4 | 42 | fsnotify_name(mask, dentry, FSNOTIFY_EVENT_DENTRY, dir, &dentry->d_name, 0); |
5f02a877 AG |
43 | } |
44 | ||
82ace1ef AG |
45 | static inline void fsnotify_inode(struct inode *inode, __u32 mask) |
46 | { | |
e43de7f0 AG |
47 | if (atomic_long_read(&inode->i_sb->s_fsnotify_connectors) == 0) |
48 | return; | |
49 | ||
82ace1ef AG |
50 | if (S_ISDIR(inode->i_mode)) |
51 | mask |= FS_ISDIR; | |
52 | ||
40a100d3 | 53 | fsnotify(mask, inode, FSNOTIFY_EVENT_INODE, NULL, NULL, inode, 0); |
82ace1ef AG |
54 | } |
55 | ||
71d73410 MG |
56 | /* Notify this dentry's parent about a child's events. */ |
57 | static inline int fsnotify_parent(struct dentry *dentry, __u32 mask, | |
58 | const void *data, int data_type) | |
59 | { | |
c738fbab AG |
60 | struct inode *inode = d_inode(dentry); |
61 | ||
e43de7f0 AG |
62 | if (atomic_long_read(&inode->i_sb->s_fsnotify_connectors) == 0) |
63 | return 0; | |
64 | ||
9b93f331 | 65 | if (S_ISDIR(inode->i_mode)) { |
c738fbab AG |
66 | mask |= FS_ISDIR; |
67 | ||
9b93f331 AG |
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)) | |
c738fbab | 75 | goto notify_child; |
71d73410 MG |
76 | |
77 | return __fsnotify_parent(dentry, mask, data, data_type); | |
c738fbab AG |
78 | |
79 | notify_child: | |
40a100d3 | 80 | return fsnotify(mask, data, data_type, NULL, NULL, inode, 0); |
71d73410 MG |
81 | } |
82 | ||
a704bba5 | 83 | /* |
c738fbab AG |
84 | * Simple wrappers to consolidate calls to fsnotify_parent() when an event |
85 | * is on a file/dentry. | |
a704bba5 | 86 | */ |
eae36a2b | 87 | static inline void fsnotify_dentry(struct dentry *dentry, __u32 mask) |
a704bba5 | 88 | { |
fd5a3ff4 | 89 | fsnotify_parent(dentry, mask, dentry, FSNOTIFY_EVENT_DENTRY); |
eae36a2b AG |
90 | } |
91 | ||
92 | static inline int fsnotify_file(struct file *file, __u32 mask) | |
93 | { | |
bc2473c9 | 94 | const struct path *path; |
eae36a2b AG |
95 | |
96 | if (file->f_mode & FMODE_NONOTIFY) | |
97 | return 0; | |
98 | ||
def3ae83 | 99 | path = &file->f_path; |
c738fbab | 100 | return fsnotify_parent(path->dentry, mask, path, FSNOTIFY_EVENT_PATH); |
a704bba5 MB |
101 | } |
102 | ||
7ea26f94 | 103 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
36e28c42 | 104 | /* |
d9e5d310 | 105 | * fsnotify_file_area_perm - permission hook before access to file range |
36e28c42 | 106 | */ |
d9e5d310 AG |
107 | static inline int fsnotify_file_area_perm(struct file *file, int perm_mask, |
108 | const loff_t *ppos, size_t count) | |
c4ec54b4 | 109 | { |
36e28c42 | 110 | __u32 fsnotify_mask = FS_ACCESS_PERM; |
c4ec54b4 | 111 | |
cb383f06 AG |
112 | /* |
113 | * filesystem may be modified in the context of permission events | |
114 | * (e.g. by HSM filling a file on access), so sb freeze protection | |
115 | * must not be held. | |
116 | */ | |
117 | lockdep_assert_once(file_write_not_started(file)); | |
118 | ||
36e28c42 | 119 | if (!(perm_mask & MAY_READ)) |
c4ec54b4 | 120 | return 0; |
eae36a2b | 121 | |
36e28c42 AG |
122 | return fsnotify_file(file, fsnotify_mask); |
123 | } | |
66917a31 | 124 | |
d9e5d310 AG |
125 | /* |
126 | * fsnotify_file_perm - permission hook before file access | |
127 | */ | |
128 | static inline int fsnotify_file_perm(struct file *file, int perm_mask) | |
129 | { | |
130 | return fsnotify_file_area_perm(file, perm_mask, NULL, 0); | |
131 | } | |
132 | ||
36e28c42 AG |
133 | /* |
134 | * fsnotify_open_perm - permission hook before file open | |
135 | */ | |
136 | static inline int fsnotify_open_perm(struct file *file) | |
137 | { | |
138 | int ret; | |
66917a31 | 139 | |
36e28c42 AG |
140 | if (file->f_flags & __FMODE_EXEC) { |
141 | ret = fsnotify_file(file, FS_OPEN_EXEC_PERM); | |
142 | if (ret) | |
143 | return ret; | |
66917a31 | 144 | } |
c4ec54b4 | 145 | |
36e28c42 | 146 | return fsnotify_file(file, FS_OPEN_PERM); |
c4ec54b4 EP |
147 | } |
148 | ||
7ea26f94 AG |
149 | #else |
150 | static inline int fsnotify_file_area_perm(struct file *file, int perm_mask, | |
151 | const loff_t *ppos, size_t count) | |
152 | { | |
153 | return 0; | |
154 | } | |
155 | ||
156 | static inline int fsnotify_file_perm(struct file *file, int perm_mask) | |
157 | { | |
158 | return 0; | |
159 | } | |
160 | ||
161 | static inline int fsnotify_open_perm(struct file *file) | |
162 | { | |
163 | return 0; | |
164 | } | |
165 | #endif | |
166 | ||
90586523 EP |
167 | /* |
168 | * fsnotify_link_count - inode's link count changed | |
169 | */ | |
170 | static inline void fsnotify_link_count(struct inode *inode) | |
171 | { | |
82ace1ef | 172 | fsnotify_inode(inode, FS_ATTRIB); |
90586523 EP |
173 | } |
174 | ||
0eeca283 RL |
175 | /* |
176 | * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir | |
177 | */ | |
178 | static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir, | |
f4ec3a3d | 179 | const struct qstr *old_name, |
0a20df7e AG |
180 | int isdir, struct inode *target, |
181 | struct dentry *moved) | |
0eeca283 | 182 | { |
5a190ae6 | 183 | struct inode *source = moved->d_inode; |
47882c6f | 184 | u32 fs_cookie = fsnotify_get_cookie(); |
5f02a877 AG |
185 | __u32 old_dir_mask = FS_MOVED_FROM; |
186 | __u32 new_dir_mask = FS_MOVED_TO; | |
e54183fa | 187 | __u32 rename_mask = FS_RENAME; |
25b229df | 188 | const struct qstr *new_name = &moved->d_name; |
0eeca283 | 189 | |
90586523 | 190 | if (isdir) { |
b29866aa EP |
191 | old_dir_mask |= FS_ISDIR; |
192 | new_dir_mask |= FS_ISDIR; | |
e54183fa | 193 | rename_mask |= FS_ISDIR; |
90586523 EP |
194 | } |
195 | ||
e54183fa AG |
196 | /* Event with information about both old and new parent+name */ |
197 | fsnotify_name(rename_mask, moved, FSNOTIFY_EVENT_DENTRY, | |
198 | old_dir, old_name, 0); | |
199 | ||
9baf93d6 AG |
200 | fsnotify_name(old_dir_mask, source, FSNOTIFY_EVENT_INODE, |
201 | old_dir, old_name, fs_cookie); | |
202 | fsnotify_name(new_dir_mask, source, FSNOTIFY_EVENT_INODE, | |
203 | new_dir, new_name, fs_cookie); | |
90586523 | 204 | |
2dfc1cae | 205 | if (target) |
90586523 | 206 | fsnotify_link_count(target); |
79cb299c | 207 | fsnotify_inode(source, FS_MOVE_SELF); |
4fa6b5ec | 208 | audit_inode_child(new_dir, moved, AUDIT_TYPE_CHILD_CREATE); |
0eeca283 RL |
209 | } |
210 | ||
3be25f49 EP |
211 | /* |
212 | * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed | |
213 | */ | |
214 | static inline void fsnotify_inode_delete(struct inode *inode) | |
215 | { | |
216 | __fsnotify_inode_delete(inode); | |
217 | } | |
218 | ||
ca9c726e AG |
219 | /* |
220 | * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed | |
221 | */ | |
222 | static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt) | |
223 | { | |
224 | __fsnotify_vfsmount_delete(mnt); | |
225 | } | |
226 | ||
7a91bf7f JM |
227 | /* |
228 | * fsnotify_inoderemove - an inode is going away | |
229 | */ | |
230 | static inline void fsnotify_inoderemove(struct inode *inode) | |
231 | { | |
82ace1ef | 232 | fsnotify_inode(inode, FS_DELETE_SELF); |
3be25f49 | 233 | __fsnotify_inode_delete(inode); |
ece95912 JK |
234 | } |
235 | ||
0eeca283 RL |
236 | /* |
237 | * fsnotify_create - 'name' was linked in | |
dabe729d AG |
238 | * |
239 | * Caller must make sure that dentry->d_name is stable. | |
240 | * Note: some filesystems (e.g. kernfs) leave @dentry negative and instantiate | |
241 | * ->d_inode later | |
0eeca283 | 242 | */ |
dabe729d | 243 | static inline void fsnotify_create(struct inode *dir, struct dentry *dentry) |
0eeca283 | 244 | { |
dabe729d | 245 | audit_inode_child(dir, dentry, AUDIT_TYPE_CHILD_CREATE); |
90586523 | 246 | |
dabe729d | 247 | fsnotify_dirent(dir, dentry, FS_CREATE); |
0eeca283 RL |
248 | } |
249 | ||
ece95912 JK |
250 | /* |
251 | * fsnotify_link - new hardlink in 'inode' directory | |
dabe729d AG |
252 | * |
253 | * Caller must make sure that new_dentry->d_name is stable. | |
ece95912 JK |
254 | * Note: We have to pass also the linked inode ptr as some filesystems leave |
255 | * new_dentry->d_inode NULL and instantiate inode pointer later | |
256 | */ | |
a1aae057 AG |
257 | static inline void fsnotify_link(struct inode *dir, struct inode *inode, |
258 | struct dentry *new_dentry) | |
ece95912 | 259 | { |
ece95912 | 260 | fsnotify_link_count(inode); |
4fa6b5ec | 261 | audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE); |
90586523 | 262 | |
9baf93d6 AG |
263 | fsnotify_name(FS_CREATE, inode, FSNOTIFY_EVENT_INODE, |
264 | dir, &new_dentry->d_name, 0); | |
ece95912 JK |
265 | } |
266 | ||
a37d9a17 AG |
267 | /* |
268 | * fsnotify_delete - @dentry was unlinked and unhashed | |
269 | * | |
270 | * Caller must make sure that dentry->d_name is stable. | |
271 | * | |
272 | * Note: unlike fsnotify_unlink(), we have to pass also the unlinked inode | |
273 | * as this may be called after d_delete() and old_dentry may be negative. | |
274 | */ | |
275 | static inline void fsnotify_delete(struct inode *dir, struct inode *inode, | |
276 | struct dentry *dentry) | |
277 | { | |
278 | __u32 mask = FS_DELETE; | |
279 | ||
280 | if (S_ISDIR(inode->i_mode)) | |
281 | mask |= FS_ISDIR; | |
282 | ||
283 | fsnotify_name(mask, inode, FSNOTIFY_EVENT_INODE, dir, &dentry->d_name, | |
284 | 0); | |
285 | } | |
286 | ||
287 | /** | |
288 | * d_delete_notify - delete a dentry and call fsnotify_delete() | |
289 | * @dentry: The dentry to delete | |
290 | * | |
291 | * This helper is used to guaranty that the unlinked inode cannot be found | |
292 | * by lookup of this name after fsnotify_delete() event has been delivered. | |
293 | */ | |
294 | static inline void d_delete_notify(struct inode *dir, struct dentry *dentry) | |
295 | { | |
296 | struct inode *inode = d_inode(dentry); | |
297 | ||
298 | ihold(inode); | |
299 | d_delete(dentry); | |
300 | fsnotify_delete(dir, inode, dentry); | |
301 | iput(inode); | |
302 | } | |
303 | ||
116b9731 AG |
304 | /* |
305 | * fsnotify_unlink - 'name' was unlinked | |
306 | * | |
307 | * Caller must make sure that dentry->d_name is stable. | |
308 | */ | |
309 | static inline void fsnotify_unlink(struct inode *dir, struct dentry *dentry) | |
310 | { | |
a37d9a17 AG |
311 | if (WARN_ON_ONCE(d_is_negative(dentry))) |
312 | return; | |
116b9731 | 313 | |
a37d9a17 | 314 | fsnotify_delete(dir, d_inode(dentry), dentry); |
116b9731 AG |
315 | } |
316 | ||
0eeca283 RL |
317 | /* |
318 | * fsnotify_mkdir - directory 'name' was created | |
dabe729d AG |
319 | * |
320 | * Caller must make sure that dentry->d_name is stable. | |
321 | * Note: some filesystems (e.g. kernfs) leave @dentry negative and instantiate | |
322 | * ->d_inode later | |
0eeca283 | 323 | */ |
dabe729d | 324 | static inline void fsnotify_mkdir(struct inode *dir, struct dentry *dentry) |
0eeca283 | 325 | { |
dabe729d | 326 | audit_inode_child(dir, dentry, AUDIT_TYPE_CHILD_CREATE); |
90586523 | 327 | |
dabe729d | 328 | fsnotify_dirent(dir, dentry, FS_CREATE | FS_ISDIR); |
0eeca283 RL |
329 | } |
330 | ||
116b9731 AG |
331 | /* |
332 | * fsnotify_rmdir - directory 'name' was removed | |
333 | * | |
334 | * Caller must make sure that dentry->d_name is stable. | |
335 | */ | |
336 | static inline void fsnotify_rmdir(struct inode *dir, struct dentry *dentry) | |
337 | { | |
a37d9a17 AG |
338 | if (WARN_ON_ONCE(d_is_negative(dentry))) |
339 | return; | |
116b9731 | 340 | |
a37d9a17 | 341 | fsnotify_delete(dir, d_inode(dentry), dentry); |
116b9731 AG |
342 | } |
343 | ||
0eeca283 RL |
344 | /* |
345 | * fsnotify_access - file was read | |
346 | */ | |
2a12a9d7 | 347 | static inline void fsnotify_access(struct file *file) |
0eeca283 | 348 | { |
eae36a2b | 349 | fsnotify_file(file, FS_ACCESS); |
0eeca283 RL |
350 | } |
351 | ||
352 | /* | |
353 | * fsnotify_modify - file was modified | |
354 | */ | |
2a12a9d7 | 355 | static inline void fsnotify_modify(struct file *file) |
0eeca283 | 356 | { |
eae36a2b | 357 | fsnotify_file(file, FS_MODIFY); |
0eeca283 RL |
358 | } |
359 | ||
360 | /* | |
361 | * fsnotify_open - file was opened | |
362 | */ | |
2a12a9d7 | 363 | static inline void fsnotify_open(struct file *file) |
0eeca283 | 364 | { |
90586523 | 365 | __u32 mask = FS_OPEN; |
0eeca283 | 366 | |
9b076f1c MB |
367 | if (file->f_flags & __FMODE_EXEC) |
368 | mask |= FS_OPEN_EXEC; | |
0eeca283 | 369 | |
eae36a2b | 370 | fsnotify_file(file, mask); |
0eeca283 RL |
371 | } |
372 | ||
373 | /* | |
374 | * fsnotify_close - file was closed | |
375 | */ | |
376 | static inline void fsnotify_close(struct file *file) | |
377 | { | |
eae36a2b AG |
378 | __u32 mask = (file->f_mode & FMODE_WRITE) ? FS_CLOSE_WRITE : |
379 | FS_CLOSE_NOWRITE; | |
0eeca283 | 380 | |
eae36a2b | 381 | fsnotify_file(file, mask); |
0eeca283 RL |
382 | } |
383 | ||
384 | /* | |
385 | * fsnotify_xattr - extended attributes were changed | |
386 | */ | |
387 | static inline void fsnotify_xattr(struct dentry *dentry) | |
388 | { | |
eae36a2b | 389 | fsnotify_dentry(dentry, FS_ATTRIB); |
0eeca283 RL |
390 | } |
391 | ||
392 | /* | |
393 | * fsnotify_change - notify_change event. file was modified and/or metadata | |
394 | * was changed. | |
395 | */ | |
396 | static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid) | |
397 | { | |
3c5119c0 EP |
398 | __u32 mask = 0; |
399 | ||
400 | if (ia_valid & ATTR_UID) | |
401 | mask |= FS_ATTRIB; | |
402 | if (ia_valid & ATTR_GID) | |
403 | mask |= FS_ATTRIB; | |
404 | if (ia_valid & ATTR_SIZE) | |
405 | mask |= FS_MODIFY; | |
0eeca283 | 406 | |
0eeca283 RL |
407 | /* both times implies a utime(s) call */ |
408 | if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) | |
3c5119c0 EP |
409 | mask |= FS_ATTRIB; |
410 | else if (ia_valid & ATTR_ATIME) | |
411 | mask |= FS_ACCESS; | |
412 | else if (ia_valid & ATTR_MTIME) | |
413 | mask |= FS_MODIFY; | |
414 | ||
415 | if (ia_valid & ATTR_MODE) | |
416 | mask |= FS_ATTRIB; | |
0eeca283 | 417 | |
eae36a2b AG |
418 | if (mask) |
419 | fsnotify_dentry(dentry, mask); | |
0eeca283 RL |
420 | } |
421 | ||
9daa8110 GKB |
422 | static inline int fsnotify_sb_error(struct super_block *sb, struct inode *inode, |
423 | int error) | |
424 | { | |
425 | struct fs_error_report report = { | |
426 | .error = error, | |
427 | .inode = inode, | |
428 | .sb = sb, | |
429 | }; | |
430 | ||
431 | return fsnotify(FS_ERROR, &report, FSNOTIFY_EVENT_ERROR, | |
432 | NULL, NULL, NULL, 0); | |
433 | } | |
434 | ||
0eeca283 | 435 | #endif /* _LINUX_FS_NOTIFY_H */ |