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