LCOV - code coverage report
Current view: top level - include/linux - fsnotify.h (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc4-xfsx @ Mon Jul 31 20:08:34 PDT 2023 Lines: 125 126 99.2 %
Date: 2023-07-31 20:08:34 Functions: 17 17 100.0 %

          Line data    Source code
       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             : /*
      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).
      24             :  *
      25             :  * Unlike fsnotify_parent(), the event will be reported regardless of the
      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.
      28             :  */
      29   407056881 : static inline int fsnotify_name(__u32 mask, const void *data, int data_type,
      30             :                                 struct inode *dir, const struct qstr *name,
      31             :                                 u32 cookie)
      32             : {
      33   407056881 :         if (atomic_long_read(&dir->i_sb->s_fsnotify_connectors) == 0)
      34             :                 return 0;
      35             : 
      36    13531218 :         return fsnotify(mask, data, data_type, dir, name, NULL, cookie);
      37             : }
      38             : 
      39             : static inline void fsnotify_dirent(struct inode *dir, struct dentry *dentry,
      40             :                                    __u32 mask)
      41             : {
      42   113694634 :         fsnotify_name(mask, dentry, FSNOTIFY_EVENT_DENTRY, dir, &dentry->d_name, 0);
      43             : }
      44             : 
      45   590342445 : static inline void fsnotify_inode(struct inode *inode, __u32 mask)
      46             : {
      47   590342445 :         if (atomic_long_read(&inode->i_sb->s_fsnotify_connectors) == 0)
      48             :                 return;
      49             : 
      50    10486724 :         if (S_ISDIR(inode->i_mode))
      51       64496 :                 mask |= FS_ISDIR;
      52             : 
      53    10486724 :         fsnotify(mask, inode, FSNOTIFY_EVENT_INODE, NULL, NULL, inode, 0);
      54             : }
      55             : 
      56             : /* Notify this dentry's parent about a child's events. */
      57  9924176944 : static inline int fsnotify_parent(struct dentry *dentry, __u32 mask,
      58             :                                   const void *data, int data_type)
      59             : {
      60  9924176944 :         struct inode *inode = d_inode(dentry);
      61             : 
      62  9924176944 :         if (atomic_long_read(&inode->i_sb->s_fsnotify_connectors) == 0)
      63             :                 return 0;
      64             : 
      65  1563624468 :         if (S_ISDIR(inode->i_mode)) {
      66    23681398 :                 mask |= FS_ISDIR;
      67             : 
      68             :                 /* sb/mount marks are not interested in name of directory */
      69    23681398 :                 if (!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED))
      70    23689869 :                         goto notify_child;
      71             :         }
      72             : 
      73             :         /* disconnected dentry cannot notify parent */
      74  1539934599 :         if (IS_ROOT(dentry))
      75           0 :                 goto notify_child;
      76             : 
      77  1539934599 :         return __fsnotify_parent(dentry, mask, data, data_type);
      78             : 
      79    23689869 : notify_child:
      80    23689869 :         return fsnotify(mask, data, data_type, NULL, NULL, inode, 0);
      81             : }
      82             : 
      83             : /*
      84             :  * Simple wrappers to consolidate calls to fsnotify_parent() when an event
      85             :  * is on a file/dentry.
      86             :  */
      87             : static inline void fsnotify_dentry(struct dentry *dentry, __u32 mask)
      88             : {
      89   315821053 :         fsnotify_parent(dentry, mask, dentry, FSNOTIFY_EVENT_DENTRY);
      90    49279299 : }
      91             : 
      92  9607270350 : static inline int fsnotify_file(struct file *file, __u32 mask)
      93             : {
      94  9607270350 :         const struct path *path;
      95             : 
      96  9607270350 :         if (file->f_mode & FMODE_NONOTIFY)
      97             :                 return 0;
      98             : 
      99             :         /* Overlayfs internal files have fake f_path */
     100  9607392925 :         path = file_real_path(file);
     101  9604990221 :         return fsnotify_parent(path->dentry, mask, path, FSNOTIFY_EVENT_PATH);
     102             : }
     103             : 
     104             : /* Simple call site for access decisions */
     105             : static inline int fsnotify_perm(struct file *file, int mask)
     106             : {
     107             :         int ret;
     108             :         __u32 fsnotify_mask = 0;
     109             : 
     110             :         if (!(mask & (MAY_READ | MAY_OPEN)))
     111             :                 return 0;
     112             : 
     113             :         if (mask & MAY_OPEN) {
     114             :                 fsnotify_mask = FS_OPEN_PERM;
     115             : 
     116             :                 if (file->f_flags & __FMODE_EXEC) {
     117             :                         ret = fsnotify_file(file, FS_OPEN_EXEC_PERM);
     118             : 
     119             :                         if (ret)
     120             :                                 return ret;
     121             :                 }
     122             :         } else if (mask & MAY_READ) {
     123             :                 fsnotify_mask = FS_ACCESS_PERM;
     124             :         }
     125             : 
     126             :         return fsnotify_file(file, fsnotify_mask);
     127             : }
     128             : 
     129             : /*
     130             :  * fsnotify_link_count - inode's link count changed
     131             :  */
     132             : static inline void fsnotify_link_count(struct inode *inode)
     133             : {
     134    70238415 :         fsnotify_inode(inode, FS_ATTRIB);
     135     3181515 : }
     136             : 
     137             : /*
     138             :  * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
     139             :  */
     140    68360712 : static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
     141             :                                  const struct qstr *old_name,
     142             :                                  int isdir, struct inode *target,
     143             :                                  struct dentry *moved)
     144             : {
     145    68360712 :         struct inode *source = moved->d_inode;
     146    68360712 :         u32 fs_cookie = fsnotify_get_cookie();
     147    68360717 :         __u32 old_dir_mask = FS_MOVED_FROM;
     148    68360717 :         __u32 new_dir_mask = FS_MOVED_TO;
     149    68360717 :         __u32 rename_mask = FS_RENAME;
     150    68360717 :         const struct qstr *new_name = &moved->d_name;
     151             : 
     152    68360717 :         if (isdir) {
     153    19338728 :                 old_dir_mask |= FS_ISDIR;
     154    19338728 :                 new_dir_mask |= FS_ISDIR;
     155    19338728 :                 rename_mask |= FS_ISDIR;
     156             :         }
     157             : 
     158             :         /* Event with information about both old and new parent+name */
     159    68360717 :         fsnotify_name(rename_mask, moved, FSNOTIFY_EVENT_DENTRY,
     160             :                       old_dir, old_name, 0);
     161             : 
     162    68360705 :         fsnotify_name(old_dir_mask, source, FSNOTIFY_EVENT_INODE,
     163             :                       old_dir, old_name, fs_cookie);
     164    68360683 :         fsnotify_name(new_dir_mask, source, FSNOTIFY_EVENT_INODE,
     165             :                       new_dir, new_name, fs_cookie);
     166             : 
     167    68360681 :         if (target)
     168     3181528 :                 fsnotify_link_count(target);
     169    68360668 :         fsnotify_inode(source, FS_MOVE_SELF);
     170    68360657 :         audit_inode_child(new_dir, moved, AUDIT_TYPE_CHILD_CREATE);
     171    68360657 : }
     172             : 
     173             : /*
     174             :  * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
     175             :  */
     176             : static inline void fsnotify_inode_delete(struct inode *inode)
     177             : {
     178  1320581359 :         __fsnotify_inode_delete(inode);
     179             : }
     180             : 
     181             : /*
     182             :  * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
     183             :  */
     184             : static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt)
     185             : {
     186     3315692 :         __fsnotify_vfsmount_delete(mnt);
     187             : }
     188             : 
     189             : /*
     190             :  * fsnotify_inoderemove - an inode is going away
     191             :  */
     192   434585155 : static inline void fsnotify_inoderemove(struct inode *inode)
     193             : {
     194   434585155 :         fsnotify_inode(inode, FS_DELETE_SELF);
     195   434493959 :         __fsnotify_inode_delete(inode);
     196   433992962 : }
     197             : 
     198             : /*
     199             :  * fsnotify_create - 'name' was linked in
     200             :  *
     201             :  * Caller must make sure that dentry->d_name is stable.
     202             :  * Note: some filesystems (e.g. kernfs) leave @dentry negative and instantiate
     203             :  * ->d_inode later
     204             :  */
     205   100119512 : static inline void fsnotify_create(struct inode *dir, struct dentry *dentry)
     206             : {
     207   100119512 :         audit_inode_child(dir, dentry, AUDIT_TYPE_CHILD_CREATE);
     208             : 
     209   100119512 :         fsnotify_dirent(dir, dentry, FS_CREATE);
     210   100106191 : }
     211             : 
     212             : /*
     213             :  * fsnotify_link - new hardlink in 'inode' directory
     214             :  *
     215             :  * Caller must make sure that new_dentry->d_name is stable.
     216             :  * Note: We have to pass also the linked inode ptr as some filesystems leave
     217             :  *   new_dentry->d_inode NULL and instantiate inode pointer later
     218             :  */
     219    13972891 : static inline void fsnotify_link(struct inode *dir, struct inode *inode,
     220             :                                  struct dentry *new_dentry)
     221             : {
     222    13972891 :         fsnotify_link_count(inode);
     223    13972813 :         audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE);
     224             : 
     225    13972813 :         fsnotify_name(FS_CREATE, inode, FSNOTIFY_EVENT_INODE,
     226    13972813 :                       dir, &new_dentry->d_name, 0);
     227    13972788 : }
     228             : 
     229             : /*
     230             :  * fsnotify_delete - @dentry was unlinked and unhashed
     231             :  *
     232             :  * Caller must make sure that dentry->d_name is stable.
     233             :  *
     234             :  * Note: unlike fsnotify_unlink(), we have to pass also the unlinked inode
     235             :  * as this may be called after d_delete() and old_dentry may be negative.
     236             :  */
     237    74454395 : static inline void fsnotify_delete(struct inode *dir, struct inode *inode,
     238             :                                    struct dentry *dentry)
     239             : {
     240    74454395 :         __u32 mask = FS_DELETE;
     241             : 
     242    74454395 :         if (S_ISDIR(inode->i_mode))
     243     2495038 :                 mask |= FS_ISDIR;
     244             : 
     245    74454395 :         fsnotify_name(mask, inode, FSNOTIFY_EVENT_INODE, dir, &dentry->d_name,
     246             :                       0);
     247    74454386 : }
     248             : 
     249             : /**
     250             :  * d_delete_notify - delete a dentry and call fsnotify_delete()
     251             :  * @dentry: The dentry to delete
     252             :  *
     253             :  * This helper is used to guaranty that the unlinked inode cannot be found
     254             :  * by lookup of this name after fsnotify_delete() event has been delivered.
     255             :  */
     256    71946322 : static inline void d_delete_notify(struct inode *dir, struct dentry *dentry)
     257             : {
     258    71946322 :         struct inode *inode = d_inode(dentry);
     259             : 
     260    71946322 :         ihold(inode);
     261    71947943 :         d_delete(dentry);
     262    71946564 :         fsnotify_delete(dir, inode, dentry);
     263    71945014 :         iput(inode);
     264    71946945 : }
     265             : 
     266             : /*
     267             :  * fsnotify_unlink - 'name' was unlinked
     268             :  *
     269             :  * Caller must make sure that dentry->d_name is stable.
     270             :  */
     271     1723800 : static inline void fsnotify_unlink(struct inode *dir, struct dentry *dentry)
     272             : {
     273     1723800 :         if (WARN_ON_ONCE(d_is_negative(dentry)))
     274             :                 return;
     275             : 
     276     1723800 :         fsnotify_delete(dir, d_inode(dentry), dentry);
     277             : }
     278             : 
     279             : /*
     280             :  * fsnotify_mkdir - directory 'name' was created
     281             :  *
     282             :  * Caller must make sure that dentry->d_name is stable.
     283             :  * Note: some filesystems (e.g. kernfs) leave @dentry negative and instantiate
     284             :  * ->d_inode later
     285             :  */
     286    13575122 : static inline void fsnotify_mkdir(struct inode *dir, struct dentry *dentry)
     287             : {
     288    13575122 :         audit_inode_child(dir, dentry, AUDIT_TYPE_CHILD_CREATE);
     289             : 
     290    13575122 :         fsnotify_dirent(dir, dentry, FS_CREATE | FS_ISDIR);
     291    13573537 : }
     292             : 
     293             : /*
     294             :  * fsnotify_rmdir - directory 'name' was removed
     295             :  *
     296             :  * Caller must make sure that dentry->d_name is stable.
     297             :  */
     298      785501 : static inline void fsnotify_rmdir(struct inode *dir, struct dentry *dentry)
     299             : {
     300      785501 :         if (WARN_ON_ONCE(d_is_negative(dentry)))
     301             :                 return;
     302             : 
     303      785501 :         fsnotify_delete(dir, d_inode(dentry), dentry);
     304             : }
     305             : 
     306             : /*
     307             :  * fsnotify_access - file was read
     308             :  */
     309             : static inline void fsnotify_access(struct file *file)
     310             : {
     311  5675128944 :         fsnotify_file(file, FS_ACCESS);
     312    17714611 : }
     313             : 
     314             : /*
     315             :  * fsnotify_modify - file was modified
     316             :  */
     317             : static inline void fsnotify_modify(struct file *file)
     318             : {
     319   933427352 :         fsnotify_file(file, FS_MODIFY);
     320    92367748 : }
     321             : 
     322             : /*
     323             :  * fsnotify_open - file was opened
     324             :  */
     325  1248157986 : static inline void fsnotify_open(struct file *file)
     326             : {
     327  1248157986 :         __u32 mask = FS_OPEN;
     328             : 
     329  1248157986 :         if (file->f_flags & __FMODE_EXEC)
     330    73984816 :                 mask |= FS_OPEN_EXEC;
     331             : 
     332  1248157986 :         fsnotify_file(file, mask);
     333  1248095860 : }
     334             : 
     335             : /*
     336             :  * fsnotify_close - file was closed
     337             :  */
     338  1741531221 : static inline void fsnotify_close(struct file *file)
     339             : {
     340  1741531221 :         __u32 mask = (file->f_mode & FMODE_WRITE) ? FS_CLOSE_WRITE :
     341             :                                                     FS_CLOSE_NOWRITE;
     342             : 
     343  1741531221 :         fsnotify_file(file, mask);
     344  1742525456 : }
     345             : 
     346             : /*
     347             :  * fsnotify_xattr - extended attributes were changed
     348             :  */
     349             : static inline void fsnotify_xattr(struct dentry *dentry)
     350             : {
     351   266539854 :         fsnotify_dentry(dentry, FS_ATTRIB);
     352   266506743 : }
     353             : 
     354             : /*
     355             :  * fsnotify_change - notify_change event.  file was modified and/or metadata
     356             :  * was changed.
     357             :  */
     358    49293270 : static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
     359             : {
     360    49293270 :         __u32 mask = 0;
     361             : 
     362    49293270 :         if (ia_valid & ATTR_UID)
     363    19814145 :                 mask |= FS_ATTRIB;
     364    49293270 :         if (ia_valid & ATTR_GID)
     365    19812236 :                 mask |= FS_ATTRIB;
     366    49293270 :         if (ia_valid & ATTR_SIZE)
     367    11852851 :                 mask |= FS_MODIFY;
     368             : 
     369             :         /* both times implies a utime(s) call */
     370    49293270 :         if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
     371    15819650 :                 mask |= FS_ATTRIB;
     372    33473620 :         else if (ia_valid & ATTR_ATIME)
     373          85 :                 mask |= FS_ACCESS;
     374    33473535 :         else if (ia_valid & ATTR_MTIME)
     375     7402992 :                 mask |= FS_MODIFY;
     376             : 
     377    49293270 :         if (ia_valid & ATTR_MODE)
     378     1778272 :                 mask |= FS_ATTRIB;
     379             : 
     380    49293270 :         if (mask)
     381    49281199 :                 fsnotify_dentry(dentry, mask);
     382    49291370 : }
     383             : 
     384         428 : static inline int fsnotify_sb_error(struct super_block *sb, struct inode *inode,
     385             :                                     int error)
     386             : {
     387         428 :         struct fs_error_report report = {
     388             :                 .error = error,
     389             :                 .inode = inode,
     390             :                 .sb = sb,
     391             :         };
     392             : 
     393         428 :         return fsnotify(FS_ERROR, &report, FSNOTIFY_EVENT_ERROR,
     394             :                         NULL, NULL, NULL, 0);
     395             : }
     396             : 
     397             : #endif  /* _LINUX_FS_NOTIFY_H */

Generated by: LCOV version 1.14