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