Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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/* Are there any inode/mount/sb objects watched with priority prio or above? */
21static inline bool fsnotify_sb_has_priority_watchers(struct super_block *sb,
22 int prio)
23{
24 struct fsnotify_sb_info *sbinfo = fsnotify_sb_info(sb);
25
26 /* Were any marks ever added to any object on this sb? */
27 if (!sbinfo)
28 return false;
29
30 return atomic_long_read(&sbinfo->watched_objects[prio]);
31}
32
33/* Are there any inode/mount/sb objects that are being watched at all? */
34static inline bool fsnotify_sb_has_watchers(struct super_block *sb)
35{
36 return fsnotify_sb_has_priority_watchers(sb, 0);
37}
38
39/*
40 * Notify this @dir inode about a change in a child directory entry.
41 * The directory entry may have turned positive or negative or its inode may
42 * have changed (i.e. renamed over).
43 *
44 * Unlike fsnotify_parent(), the event will be reported regardless of the
45 * FS_EVENT_ON_CHILD mask on the parent inode and will not be reported if only
46 * the child is interested and not the parent.
47 */
48static inline int fsnotify_name(__u32 mask, const void *data, int data_type,
49 struct inode *dir, const struct qstr *name,
50 u32 cookie)
51{
52 if (!fsnotify_sb_has_watchers(dir->i_sb))
53 return 0;
54
55 return fsnotify(mask, data, data_type, dir, name, NULL, cookie);
56}
57
58static inline void fsnotify_dirent(struct inode *dir, struct dentry *dentry,
59 __u32 mask)
60{
61 fsnotify_name(mask, dentry, FSNOTIFY_EVENT_DENTRY, dir, &dentry->d_name, 0);
62}
63
64static inline void fsnotify_inode(struct inode *inode, __u32 mask)
65{
66 if (!fsnotify_sb_has_watchers(inode->i_sb))
67 return;
68
69 if (S_ISDIR(inode->i_mode))
70 mask |= FS_ISDIR;
71
72 fsnotify(mask, inode, FSNOTIFY_EVENT_INODE, NULL, NULL, inode, 0);
73}
74
75/* Notify this dentry's parent about a child's events. */
76static inline int fsnotify_parent(struct dentry *dentry, __u32 mask,
77 const void *data, int data_type)
78{
79 struct inode *inode = d_inode(dentry);
80
81 if (!fsnotify_sb_has_watchers(inode->i_sb))
82 return 0;
83
84 if (S_ISDIR(inode->i_mode)) {
85 mask |= FS_ISDIR;
86
87 /* sb/mount marks are not interested in name of directory */
88 if (!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED))
89 goto notify_child;
90 }
91
92 /* disconnected dentry cannot notify parent */
93 if (IS_ROOT(dentry))
94 goto notify_child;
95
96 return __fsnotify_parent(dentry, mask, data, data_type);
97
98notify_child:
99 return fsnotify(mask, data, data_type, NULL, NULL, inode, 0);
100}
101
102/*
103 * Simple wrappers to consolidate calls to fsnotify_parent() when an event
104 * is on a file/dentry.
105 */
106static inline void fsnotify_dentry(struct dentry *dentry, __u32 mask)
107{
108 fsnotify_parent(dentry, mask, dentry, FSNOTIFY_EVENT_DENTRY);
109}
110
111static inline int fsnotify_path(const struct path *path, __u32 mask)
112{
113 return fsnotify_parent(path->dentry, mask, path, FSNOTIFY_EVENT_PATH);
114}
115
116static inline int fsnotify_file(struct file *file, __u32 mask)
117{
118 /*
119 * FMODE_NONOTIFY are fds generated by fanotify itself which should not
120 * generate new events. We also don't want to generate events for
121 * FMODE_PATH fds (involves open & close events) as they are just
122 * handle creation / destruction events and not "real" file events.
123 */
124 if (FMODE_FSNOTIFY_NONE(file->f_mode))
125 return 0;
126
127 return fsnotify_path(&file->f_path, mask);
128}
129
130#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
131
132void file_set_fsnotify_mode_from_watchers(struct file *file);
133
134/*
135 * fsnotify_file_area_perm - permission hook before access to file range
136 */
137static inline int fsnotify_file_area_perm(struct file *file, int perm_mask,
138 const loff_t *ppos, size_t count)
139{
140 /*
141 * filesystem may be modified in the context of permission events
142 * (e.g. by HSM filling a file on access), so sb freeze protection
143 * must not be held.
144 */
145 lockdep_assert_once(file_write_not_started(file));
146
147 if (!(perm_mask & (MAY_READ | MAY_WRITE | MAY_ACCESS)))
148 return 0;
149
150 if (likely(!FMODE_FSNOTIFY_PERM(file->f_mode)))
151 return 0;
152
153 /*
154 * read()/write() and other types of access generate pre-content events.
155 */
156 if (unlikely(FMODE_FSNOTIFY_HSM(file->f_mode))) {
157 int ret = fsnotify_pre_content(&file->f_path, ppos, count);
158
159 if (ret)
160 return ret;
161 }
162
163 if (!(perm_mask & MAY_READ))
164 return 0;
165
166 /*
167 * read() also generates the legacy FS_ACCESS_PERM event, so content
168 * scanners can inspect the content filled by pre-content event.
169 */
170 return fsnotify_path(&file->f_path, FS_ACCESS_PERM);
171}
172
173/*
174 * fsnotify_truncate_perm - permission hook before file truncate
175 */
176static inline int fsnotify_truncate_perm(const struct path *path, loff_t length)
177{
178 struct inode *inode = d_inode(path->dentry);
179
180 if (!(inode->i_sb->s_iflags & SB_I_ALLOW_HSM) ||
181 !fsnotify_sb_has_priority_watchers(inode->i_sb,
182 FSNOTIFY_PRIO_PRE_CONTENT))
183 return 0;
184
185 return fsnotify_pre_content(path, &length, 0);
186}
187
188/*
189 * fsnotify_file_perm - permission hook before file access (unknown range)
190 */
191static inline int fsnotify_file_perm(struct file *file, int perm_mask)
192{
193 return fsnotify_file_area_perm(file, perm_mask, NULL, 0);
194}
195
196/*
197 * fsnotify_open_perm - permission hook before file open
198 */
199static inline int fsnotify_open_perm(struct file *file)
200{
201 int ret;
202
203 if (likely(!FMODE_FSNOTIFY_PERM(file->f_mode)))
204 return 0;
205
206 if (file->f_flags & __FMODE_EXEC) {
207 ret = fsnotify_path(&file->f_path, FS_OPEN_EXEC_PERM);
208 if (ret)
209 return ret;
210 }
211
212 return fsnotify_path(&file->f_path, FS_OPEN_PERM);
213}
214
215#else
216static inline void file_set_fsnotify_mode_from_watchers(struct file *file)
217{
218}
219
220static inline int fsnotify_file_area_perm(struct file *file, int perm_mask,
221 const loff_t *ppos, size_t count)
222{
223 return 0;
224}
225
226static inline int fsnotify_truncate_perm(const struct path *path, loff_t length)
227{
228 return 0;
229}
230
231static inline int fsnotify_file_perm(struct file *file, int perm_mask)
232{
233 return 0;
234}
235
236static inline int fsnotify_open_perm(struct file *file)
237{
238 return 0;
239}
240#endif
241
242/*
243 * fsnotify_link_count - inode's link count changed
244 */
245static inline void fsnotify_link_count(struct inode *inode)
246{
247 fsnotify_inode(inode, FS_ATTRIB);
248}
249
250/*
251 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
252 */
253static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
254 const struct qstr *old_name,
255 int isdir, struct inode *target,
256 struct dentry *moved)
257{
258 struct inode *source = moved->d_inode;
259 u32 fs_cookie = fsnotify_get_cookie();
260 __u32 old_dir_mask = FS_MOVED_FROM;
261 __u32 new_dir_mask = FS_MOVED_TO;
262 __u32 rename_mask = FS_RENAME;
263 const struct qstr *new_name = &moved->d_name;
264
265 if (isdir) {
266 old_dir_mask |= FS_ISDIR;
267 new_dir_mask |= FS_ISDIR;
268 rename_mask |= FS_ISDIR;
269 }
270
271 /* Event with information about both old and new parent+name */
272 fsnotify_name(rename_mask, moved, FSNOTIFY_EVENT_DENTRY,
273 old_dir, old_name, 0);
274
275 fsnotify_name(old_dir_mask, source, FSNOTIFY_EVENT_INODE,
276 old_dir, old_name, fs_cookie);
277 fsnotify_name(new_dir_mask, source, FSNOTIFY_EVENT_INODE,
278 new_dir, new_name, fs_cookie);
279
280 if (target)
281 fsnotify_link_count(target);
282 fsnotify_inode(source, FS_MOVE_SELF);
283 audit_inode_child(new_dir, moved, AUDIT_TYPE_CHILD_CREATE);
284}
285
286/*
287 * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
288 */
289static inline void fsnotify_inode_delete(struct inode *inode)
290{
291 __fsnotify_inode_delete(inode);
292}
293
294/*
295 * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
296 */
297static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt)
298{
299 __fsnotify_vfsmount_delete(mnt);
300}
301
302/*
303 * fsnotify_inoderemove - an inode is going away
304 */
305static inline void fsnotify_inoderemove(struct inode *inode)
306{
307 fsnotify_inode(inode, FS_DELETE_SELF);
308 __fsnotify_inode_delete(inode);
309}
310
311/*
312 * fsnotify_create - 'name' was linked in
313 *
314 * Caller must make sure that dentry->d_name is stable.
315 * Note: some filesystems (e.g. kernfs) leave @dentry negative and instantiate
316 * ->d_inode later
317 */
318static inline void fsnotify_create(struct inode *dir, struct dentry *dentry)
319{
320 audit_inode_child(dir, dentry, AUDIT_TYPE_CHILD_CREATE);
321
322 fsnotify_dirent(dir, dentry, FS_CREATE);
323}
324
325/*
326 * fsnotify_link - new hardlink in 'inode' directory
327 *
328 * Caller must make sure that new_dentry->d_name is stable.
329 * Note: We have to pass also the linked inode ptr as some filesystems leave
330 * new_dentry->d_inode NULL and instantiate inode pointer later
331 */
332static inline void fsnotify_link(struct inode *dir, struct inode *inode,
333 struct dentry *new_dentry)
334{
335 fsnotify_link_count(inode);
336 audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE);
337
338 fsnotify_name(FS_CREATE, inode, FSNOTIFY_EVENT_INODE,
339 dir, &new_dentry->d_name, 0);
340}
341
342/*
343 * fsnotify_delete - @dentry was unlinked and unhashed
344 *
345 * Caller must make sure that dentry->d_name is stable.
346 *
347 * Note: unlike fsnotify_unlink(), we have to pass also the unlinked inode
348 * as this may be called after d_delete() and old_dentry may be negative.
349 */
350static inline void fsnotify_delete(struct inode *dir, struct inode *inode,
351 struct dentry *dentry)
352{
353 __u32 mask = FS_DELETE;
354
355 if (S_ISDIR(inode->i_mode))
356 mask |= FS_ISDIR;
357
358 fsnotify_name(mask, inode, FSNOTIFY_EVENT_INODE, dir, &dentry->d_name,
359 0);
360}
361
362/**
363 * d_delete_notify - delete a dentry and call fsnotify_delete()
364 * @dentry: The dentry to delete
365 *
366 * This helper is used to guaranty that the unlinked inode cannot be found
367 * by lookup of this name after fsnotify_delete() event has been delivered.
368 */
369static inline void d_delete_notify(struct inode *dir, struct dentry *dentry)
370{
371 struct inode *inode = d_inode(dentry);
372
373 ihold(inode);
374 d_delete(dentry);
375 fsnotify_delete(dir, inode, dentry);
376 iput(inode);
377}
378
379/*
380 * fsnotify_unlink - 'name' was unlinked
381 *
382 * Caller must make sure that dentry->d_name is stable.
383 */
384static inline void fsnotify_unlink(struct inode *dir, struct dentry *dentry)
385{
386 if (WARN_ON_ONCE(d_is_negative(dentry)))
387 return;
388
389 fsnotify_delete(dir, d_inode(dentry), dentry);
390}
391
392/*
393 * fsnotify_mkdir - directory 'name' was created
394 *
395 * Caller must make sure that dentry->d_name is stable.
396 * Note: some filesystems (e.g. kernfs) leave @dentry negative and instantiate
397 * ->d_inode later
398 */
399static inline void fsnotify_mkdir(struct inode *dir, struct dentry *dentry)
400{
401 audit_inode_child(dir, dentry, AUDIT_TYPE_CHILD_CREATE);
402
403 fsnotify_dirent(dir, dentry, FS_CREATE | FS_ISDIR);
404}
405
406/*
407 * fsnotify_rmdir - directory 'name' was removed
408 *
409 * Caller must make sure that dentry->d_name is stable.
410 */
411static inline void fsnotify_rmdir(struct inode *dir, struct dentry *dentry)
412{
413 if (WARN_ON_ONCE(d_is_negative(dentry)))
414 return;
415
416 fsnotify_delete(dir, d_inode(dentry), dentry);
417}
418
419/*
420 * fsnotify_access - file was read
421 */
422static inline void fsnotify_access(struct file *file)
423{
424 fsnotify_file(file, FS_ACCESS);
425}
426
427/*
428 * fsnotify_modify - file was modified
429 */
430static inline void fsnotify_modify(struct file *file)
431{
432 fsnotify_file(file, FS_MODIFY);
433}
434
435/*
436 * fsnotify_open - file was opened
437 */
438static inline void fsnotify_open(struct file *file)
439{
440 __u32 mask = FS_OPEN;
441
442 if (file->f_flags & __FMODE_EXEC)
443 mask |= FS_OPEN_EXEC;
444
445 fsnotify_file(file, mask);
446}
447
448/*
449 * fsnotify_close - file was closed
450 */
451static inline void fsnotify_close(struct file *file)
452{
453 __u32 mask = (file->f_mode & FMODE_WRITE) ? FS_CLOSE_WRITE :
454 FS_CLOSE_NOWRITE;
455
456 fsnotify_file(file, mask);
457}
458
459/*
460 * fsnotify_xattr - extended attributes were changed
461 */
462static inline void fsnotify_xattr(struct dentry *dentry)
463{
464 fsnotify_dentry(dentry, FS_ATTRIB);
465}
466
467/*
468 * fsnotify_change - notify_change event. file was modified and/or metadata
469 * was changed.
470 */
471static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
472{
473 __u32 mask = 0;
474
475 if (ia_valid & ATTR_UID)
476 mask |= FS_ATTRIB;
477 if (ia_valid & ATTR_GID)
478 mask |= FS_ATTRIB;
479 if (ia_valid & ATTR_SIZE)
480 mask |= FS_MODIFY;
481
482 /* both times implies a utime(s) call */
483 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
484 mask |= FS_ATTRIB;
485 else if (ia_valid & ATTR_ATIME)
486 mask |= FS_ACCESS;
487 else if (ia_valid & ATTR_MTIME)
488 mask |= FS_MODIFY;
489
490 if (ia_valid & ATTR_MODE)
491 mask |= FS_ATTRIB;
492
493 if (mask)
494 fsnotify_dentry(dentry, mask);
495}
496
497static inline int fsnotify_sb_error(struct super_block *sb, struct inode *inode,
498 int error)
499{
500 struct fs_error_report report = {
501 .error = error,
502 .inode = inode,
503 .sb = sb,
504 };
505
506 return fsnotify(FS_ERROR, &report, FSNOTIFY_EVENT_ERROR,
507 NULL, NULL, NULL, 0);
508}
509
510#endif /* _LINUX_FS_NOTIFY_H */