Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Filesystem access notification for Linux
0004  *
0005  *  Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com>
0006  */
0007 
0008 #ifndef __LINUX_FSNOTIFY_BACKEND_H
0009 #define __LINUX_FSNOTIFY_BACKEND_H
0010 
0011 #ifdef __KERNEL__
0012 
0013 #include <linux/idr.h> /* inotify uses this */
0014 #include <linux/fs.h> /* struct inode */
0015 #include <linux/list.h>
0016 #include <linux/path.h> /* struct path */
0017 #include <linux/spinlock.h>
0018 #include <linux/types.h>
0019 #include <linux/atomic.h>
0020 #include <linux/user_namespace.h>
0021 #include <linux/refcount.h>
0022 #include <linux/mempool.h>
0023 #include <linux/sched/mm.h>
0024 
0025 /*
0026  * IN_* from inotfy.h lines up EXACTLY with FS_*, this is so we can easily
0027  * convert between them.  dnotify only needs conversion at watch creation
0028  * so no perf loss there.  fanotify isn't defined yet, so it can use the
0029  * wholes if it needs more events.
0030  */
0031 #define FS_ACCESS       0x00000001  /* File was accessed */
0032 #define FS_MODIFY       0x00000002  /* File was modified */
0033 #define FS_ATTRIB       0x00000004  /* Metadata changed */
0034 #define FS_CLOSE_WRITE      0x00000008  /* Writtable file was closed */
0035 #define FS_CLOSE_NOWRITE    0x00000010  /* Unwrittable file closed */
0036 #define FS_OPEN         0x00000020  /* File was opened */
0037 #define FS_MOVED_FROM       0x00000040  /* File was moved from X */
0038 #define FS_MOVED_TO     0x00000080  /* File was moved to Y */
0039 #define FS_CREATE       0x00000100  /* Subfile was created */
0040 #define FS_DELETE       0x00000200  /* Subfile was deleted */
0041 #define FS_DELETE_SELF      0x00000400  /* Self was deleted */
0042 #define FS_MOVE_SELF        0x00000800  /* Self was moved */
0043 #define FS_OPEN_EXEC        0x00001000  /* File was opened for exec */
0044 
0045 #define FS_UNMOUNT      0x00002000  /* inode on umount fs */
0046 #define FS_Q_OVERFLOW       0x00004000  /* Event queued overflowed */
0047 #define FS_ERROR        0x00008000  /* Filesystem Error (fanotify) */
0048 
0049 /*
0050  * FS_IN_IGNORED overloads FS_ERROR.  It is only used internally by inotify
0051  * which does not support FS_ERROR.
0052  */
0053 #define FS_IN_IGNORED       0x00008000  /* last inotify event here */
0054 
0055 #define FS_OPEN_PERM        0x00010000  /* open event in an permission hook */
0056 #define FS_ACCESS_PERM      0x00020000  /* access event in a permissions hook */
0057 #define FS_OPEN_EXEC_PERM   0x00040000  /* open/exec event in a permission hook */
0058 
0059 /*
0060  * Set on inode mark that cares about things that happen to its children.
0061  * Always set for dnotify and inotify.
0062  * Set on inode/sb/mount marks that care about parent/name info.
0063  */
0064 #define FS_EVENT_ON_CHILD   0x08000000
0065 
0066 #define FS_RENAME       0x10000000  /* File was renamed */
0067 #define FS_DN_MULTISHOT     0x20000000  /* dnotify multishot */
0068 #define FS_ISDIR        0x40000000  /* event occurred against dir */
0069 
0070 #define FS_MOVE         (FS_MOVED_FROM | FS_MOVED_TO)
0071 
0072 /*
0073  * Directory entry modification events - reported only to directory
0074  * where entry is modified and not to a watching parent.
0075  * The watching parent may get an FS_ATTRIB|FS_EVENT_ON_CHILD event
0076  * when a directory entry inside a child subdir changes.
0077  */
0078 #define ALL_FSNOTIFY_DIRENT_EVENTS (FS_CREATE | FS_DELETE | FS_MOVE | FS_RENAME)
0079 
0080 #define ALL_FSNOTIFY_PERM_EVENTS (FS_OPEN_PERM | FS_ACCESS_PERM | \
0081                   FS_OPEN_EXEC_PERM)
0082 
0083 /*
0084  * This is a list of all events that may get sent to a parent that is watching
0085  * with flag FS_EVENT_ON_CHILD based on fs event on a child of that directory.
0086  */
0087 #define FS_EVENTS_POSS_ON_CHILD   (ALL_FSNOTIFY_PERM_EVENTS | \
0088                    FS_ACCESS | FS_MODIFY | FS_ATTRIB | \
0089                    FS_CLOSE_WRITE | FS_CLOSE_NOWRITE | \
0090                    FS_OPEN | FS_OPEN_EXEC)
0091 
0092 /*
0093  * This is a list of all events that may get sent with the parent inode as the
0094  * @to_tell argument of fsnotify().
0095  * It may include events that can be sent to an inode/sb/mount mark, but cannot
0096  * be sent to a parent watching children.
0097  */
0098 #define FS_EVENTS_POSS_TO_PARENT (FS_EVENTS_POSS_ON_CHILD)
0099 
0100 /* Events that can be reported to backends */
0101 #define ALL_FSNOTIFY_EVENTS (ALL_FSNOTIFY_DIRENT_EVENTS | \
0102                  FS_EVENTS_POSS_ON_CHILD | \
0103                  FS_DELETE_SELF | FS_MOVE_SELF | \
0104                  FS_UNMOUNT | FS_Q_OVERFLOW | FS_IN_IGNORED | \
0105                  FS_ERROR)
0106 
0107 /* Extra flags that may be reported with event or control handling of events */
0108 #define ALL_FSNOTIFY_FLAGS  (FS_ISDIR | FS_EVENT_ON_CHILD | FS_DN_MULTISHOT)
0109 
0110 #define ALL_FSNOTIFY_BITS   (ALL_FSNOTIFY_EVENTS | ALL_FSNOTIFY_FLAGS)
0111 
0112 struct fsnotify_group;
0113 struct fsnotify_event;
0114 struct fsnotify_mark;
0115 struct fsnotify_event_private_data;
0116 struct fsnotify_fname;
0117 struct fsnotify_iter_info;
0118 
0119 struct mem_cgroup;
0120 
0121 /*
0122  * Each group much define these ops.  The fsnotify infrastructure will call
0123  * these operations for each relevant group.
0124  *
0125  * handle_event - main call for a group to handle an fs event
0126  * @group:  group to notify
0127  * @mask:   event type and flags
0128  * @data:   object that event happened on
0129  * @data_type:  type of object for fanotify_data_XXX() accessors
0130  * @dir:    optional directory associated with event -
0131  *      if @file_name is not NULL, this is the directory that
0132  *      @file_name is relative to
0133  * @file_name:  optional file name associated with event
0134  * @cookie: inotify rename cookie
0135  * @iter_info:  array of marks from this group that are interested in the event
0136  *
0137  * handle_inode_event - simple variant of handle_event() for groups that only
0138  *      have inode marks and don't have ignore mask
0139  * @mark:   mark to notify
0140  * @mask:   event type and flags
0141  * @inode:  inode that event happened on
0142  * @dir:    optional directory associated with event -
0143  *      if @file_name is not NULL, this is the directory that
0144  *      @file_name is relative to.
0145  *      Either @inode or @dir must be non-NULL.
0146  * @file_name:  optional file name associated with event
0147  * @cookie: inotify rename cookie
0148  *
0149  * free_group_priv - called when a group refcnt hits 0 to clean up the private union
0150  * freeing_mark - called when a mark is being destroyed for some reason.  The group
0151  *      MUST be holding a reference on each mark and that reference must be
0152  *      dropped in this function.  inotify uses this function to send
0153  *      userspace messages that marks have been removed.
0154  */
0155 struct fsnotify_ops {
0156     int (*handle_event)(struct fsnotify_group *group, u32 mask,
0157                 const void *data, int data_type, struct inode *dir,
0158                 const struct qstr *file_name, u32 cookie,
0159                 struct fsnotify_iter_info *iter_info);
0160     int (*handle_inode_event)(struct fsnotify_mark *mark, u32 mask,
0161                 struct inode *inode, struct inode *dir,
0162                 const struct qstr *file_name, u32 cookie);
0163     void (*free_group_priv)(struct fsnotify_group *group);
0164     void (*freeing_mark)(struct fsnotify_mark *mark, struct fsnotify_group *group);
0165     void (*free_event)(struct fsnotify_group *group, struct fsnotify_event *event);
0166     /* called on final put+free to free memory */
0167     void (*free_mark)(struct fsnotify_mark *mark);
0168 };
0169 
0170 /*
0171  * all of the information about the original object we want to now send to
0172  * a group.  If you want to carry more info from the accessing task to the
0173  * listener this structure is where you need to be adding fields.
0174  */
0175 struct fsnotify_event {
0176     struct list_head list;
0177 };
0178 
0179 /*
0180  * A group is a "thing" that wants to receive notification about filesystem
0181  * events.  The mask holds the subset of event types this group cares about.
0182  * refcnt on a group is up to the implementor and at any moment if it goes 0
0183  * everything will be cleaned up.
0184  */
0185 struct fsnotify_group {
0186     const struct fsnotify_ops *ops; /* how this group handles things */
0187 
0188     /*
0189      * How the refcnt is used is up to each group.  When the refcnt hits 0
0190      * fsnotify will clean up all of the resources associated with this group.
0191      * As an example, the dnotify group will always have a refcnt=1 and that
0192      * will never change.  Inotify, on the other hand, has a group per
0193      * inotify_init() and the refcnt will hit 0 only when that fd has been
0194      * closed.
0195      */
0196     refcount_t refcnt;      /* things with interest in this group */
0197 
0198     /* needed to send notification to userspace */
0199     spinlock_t notification_lock;       /* protect the notification_list */
0200     struct list_head notification_list; /* list of event_holder this group needs to send to userspace */
0201     wait_queue_head_t notification_waitq;   /* read() on the notification file blocks on this waitq */
0202     unsigned int q_len;         /* events on the queue */
0203     unsigned int max_events;        /* maximum events allowed on the list */
0204     /*
0205      * Valid fsnotify group priorities.  Events are send in order from highest
0206      * priority to lowest priority.  We default to the lowest priority.
0207      */
0208     #define FS_PRIO_0   0 /* normal notifiers, no permissions */
0209     #define FS_PRIO_1   1 /* fanotify content based access control */
0210     #define FS_PRIO_2   2 /* fanotify pre-content access */
0211     unsigned int priority;
0212     bool shutdown;      /* group is being shut down, don't queue more events */
0213 
0214 #define FSNOTIFY_GROUP_USER 0x01 /* user allocated group */
0215 #define FSNOTIFY_GROUP_DUPS 0x02 /* allow multiple marks per object */
0216 #define FSNOTIFY_GROUP_NOFS 0x04 /* group lock is not direct reclaim safe */
0217     int flags;
0218     unsigned int owner_flags;   /* stored flags of mark_mutex owner */
0219 
0220     /* stores all fastpath marks assoc with this group so they can be cleaned on unregister */
0221     struct mutex mark_mutex;    /* protect marks_list */
0222     atomic_t user_waits;        /* Number of tasks waiting for user
0223                      * response */
0224     struct list_head marks_list;    /* all inode marks for this group */
0225 
0226     struct fasync_struct *fsn_fa;    /* async notification */
0227 
0228     struct fsnotify_event *overflow_event;  /* Event we queue when the
0229                          * notification list is too
0230                          * full */
0231 
0232     struct mem_cgroup *memcg;   /* memcg to charge allocations */
0233 
0234     /* groups can define private fields here or use the void *private */
0235     union {
0236         void *private;
0237 #ifdef CONFIG_INOTIFY_USER
0238         struct inotify_group_private_data {
0239             spinlock_t  idr_lock;
0240             struct idr      idr;
0241             struct ucounts *ucounts;
0242         } inotify_data;
0243 #endif
0244 #ifdef CONFIG_FANOTIFY
0245         struct fanotify_group_private_data {
0246             /* Hash table of events for merge */
0247             struct hlist_head *merge_hash;
0248             /* allows a group to block waiting for a userspace response */
0249             struct list_head access_list;
0250             wait_queue_head_t access_waitq;
0251             int flags;           /* flags from fanotify_init() */
0252             int f_flags; /* event_f_flags from fanotify_init() */
0253             struct ucounts *ucounts;
0254             mempool_t error_events_pool;
0255         } fanotify_data;
0256 #endif /* CONFIG_FANOTIFY */
0257     };
0258 };
0259 
0260 /*
0261  * These helpers are used to prevent deadlock when reclaiming inodes with
0262  * evictable marks of the same group that is allocating a new mark.
0263  */
0264 static inline void fsnotify_group_lock(struct fsnotify_group *group)
0265 {
0266     mutex_lock(&group->mark_mutex);
0267     if (group->flags & FSNOTIFY_GROUP_NOFS)
0268         group->owner_flags = memalloc_nofs_save();
0269 }
0270 
0271 static inline void fsnotify_group_unlock(struct fsnotify_group *group)
0272 {
0273     if (group->flags & FSNOTIFY_GROUP_NOFS)
0274         memalloc_nofs_restore(group->owner_flags);
0275     mutex_unlock(&group->mark_mutex);
0276 }
0277 
0278 static inline void fsnotify_group_assert_locked(struct fsnotify_group *group)
0279 {
0280     WARN_ON_ONCE(!mutex_is_locked(&group->mark_mutex));
0281     if (group->flags & FSNOTIFY_GROUP_NOFS)
0282         WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS));
0283 }
0284 
0285 /* When calling fsnotify tell it if the data is a path or inode */
0286 enum fsnotify_data_type {
0287     FSNOTIFY_EVENT_NONE,
0288     FSNOTIFY_EVENT_PATH,
0289     FSNOTIFY_EVENT_INODE,
0290     FSNOTIFY_EVENT_DENTRY,
0291     FSNOTIFY_EVENT_ERROR,
0292 };
0293 
0294 struct fs_error_report {
0295     int error;
0296     struct inode *inode;
0297     struct super_block *sb;
0298 };
0299 
0300 static inline struct inode *fsnotify_data_inode(const void *data, int data_type)
0301 {
0302     switch (data_type) {
0303     case FSNOTIFY_EVENT_INODE:
0304         return (struct inode *)data;
0305     case FSNOTIFY_EVENT_DENTRY:
0306         return d_inode(data);
0307     case FSNOTIFY_EVENT_PATH:
0308         return d_inode(((const struct path *)data)->dentry);
0309     case FSNOTIFY_EVENT_ERROR:
0310         return ((struct fs_error_report *)data)->inode;
0311     default:
0312         return NULL;
0313     }
0314 }
0315 
0316 static inline struct dentry *fsnotify_data_dentry(const void *data, int data_type)
0317 {
0318     switch (data_type) {
0319     case FSNOTIFY_EVENT_DENTRY:
0320         /* Non const is needed for dget() */
0321         return (struct dentry *)data;
0322     case FSNOTIFY_EVENT_PATH:
0323         return ((const struct path *)data)->dentry;
0324     default:
0325         return NULL;
0326     }
0327 }
0328 
0329 static inline const struct path *fsnotify_data_path(const void *data,
0330                             int data_type)
0331 {
0332     switch (data_type) {
0333     case FSNOTIFY_EVENT_PATH:
0334         return data;
0335     default:
0336         return NULL;
0337     }
0338 }
0339 
0340 static inline struct super_block *fsnotify_data_sb(const void *data,
0341                            int data_type)
0342 {
0343     switch (data_type) {
0344     case FSNOTIFY_EVENT_INODE:
0345         return ((struct inode *)data)->i_sb;
0346     case FSNOTIFY_EVENT_DENTRY:
0347         return ((struct dentry *)data)->d_sb;
0348     case FSNOTIFY_EVENT_PATH:
0349         return ((const struct path *)data)->dentry->d_sb;
0350     case FSNOTIFY_EVENT_ERROR:
0351         return ((struct fs_error_report *) data)->sb;
0352     default:
0353         return NULL;
0354     }
0355 }
0356 
0357 static inline struct fs_error_report *fsnotify_data_error_report(
0358                             const void *data,
0359                             int data_type)
0360 {
0361     switch (data_type) {
0362     case FSNOTIFY_EVENT_ERROR:
0363         return (struct fs_error_report *) data;
0364     default:
0365         return NULL;
0366     }
0367 }
0368 
0369 /*
0370  * Index to merged marks iterator array that correlates to a type of watch.
0371  * The type of watched object can be deduced from the iterator type, but not
0372  * the other way around, because an event can match different watched objects
0373  * of the same object type.
0374  * For example, both parent and child are watching an object of type inode.
0375  */
0376 enum fsnotify_iter_type {
0377     FSNOTIFY_ITER_TYPE_INODE,
0378     FSNOTIFY_ITER_TYPE_VFSMOUNT,
0379     FSNOTIFY_ITER_TYPE_SB,
0380     FSNOTIFY_ITER_TYPE_PARENT,
0381     FSNOTIFY_ITER_TYPE_INODE2,
0382     FSNOTIFY_ITER_TYPE_COUNT
0383 };
0384 
0385 /* The type of object that a mark is attached to */
0386 enum fsnotify_obj_type {
0387     FSNOTIFY_OBJ_TYPE_ANY = -1,
0388     FSNOTIFY_OBJ_TYPE_INODE,
0389     FSNOTIFY_OBJ_TYPE_VFSMOUNT,
0390     FSNOTIFY_OBJ_TYPE_SB,
0391     FSNOTIFY_OBJ_TYPE_COUNT,
0392     FSNOTIFY_OBJ_TYPE_DETACHED = FSNOTIFY_OBJ_TYPE_COUNT
0393 };
0394 
0395 static inline bool fsnotify_valid_obj_type(unsigned int obj_type)
0396 {
0397     return (obj_type < FSNOTIFY_OBJ_TYPE_COUNT);
0398 }
0399 
0400 struct fsnotify_iter_info {
0401     struct fsnotify_mark *marks[FSNOTIFY_ITER_TYPE_COUNT];
0402     struct fsnotify_group *current_group;
0403     unsigned int report_mask;
0404     int srcu_idx;
0405 };
0406 
0407 static inline bool fsnotify_iter_should_report_type(
0408         struct fsnotify_iter_info *iter_info, int iter_type)
0409 {
0410     return (iter_info->report_mask & (1U << iter_type));
0411 }
0412 
0413 static inline void fsnotify_iter_set_report_type(
0414         struct fsnotify_iter_info *iter_info, int iter_type)
0415 {
0416     iter_info->report_mask |= (1U << iter_type);
0417 }
0418 
0419 static inline struct fsnotify_mark *fsnotify_iter_mark(
0420         struct fsnotify_iter_info *iter_info, int iter_type)
0421 {
0422     if (fsnotify_iter_should_report_type(iter_info, iter_type))
0423         return iter_info->marks[iter_type];
0424     return NULL;
0425 }
0426 
0427 static inline int fsnotify_iter_step(struct fsnotify_iter_info *iter, int type,
0428                      struct fsnotify_mark **markp)
0429 {
0430     while (type < FSNOTIFY_ITER_TYPE_COUNT) {
0431         *markp = fsnotify_iter_mark(iter, type);
0432         if (*markp)
0433             break;
0434         type++;
0435     }
0436     return type;
0437 }
0438 
0439 #define FSNOTIFY_ITER_FUNCS(name, NAME) \
0440 static inline struct fsnotify_mark *fsnotify_iter_##name##_mark( \
0441         struct fsnotify_iter_info *iter_info) \
0442 { \
0443     return fsnotify_iter_mark(iter_info, FSNOTIFY_ITER_TYPE_##NAME); \
0444 }
0445 
0446 FSNOTIFY_ITER_FUNCS(inode, INODE)
0447 FSNOTIFY_ITER_FUNCS(parent, PARENT)
0448 FSNOTIFY_ITER_FUNCS(vfsmount, VFSMOUNT)
0449 FSNOTIFY_ITER_FUNCS(sb, SB)
0450 
0451 #define fsnotify_foreach_iter_type(type) \
0452     for (type = 0; type < FSNOTIFY_ITER_TYPE_COUNT; type++)
0453 #define fsnotify_foreach_iter_mark_type(iter, mark, type) \
0454     for (type = 0; \
0455          type = fsnotify_iter_step(iter, type, &mark), \
0456          type < FSNOTIFY_ITER_TYPE_COUNT; \
0457          type++)
0458 
0459 /*
0460  * fsnotify_connp_t is what we embed in objects which connector can be attached
0461  * to. fsnotify_connp_t * is how we refer from connector back to object.
0462  */
0463 struct fsnotify_mark_connector;
0464 typedef struct fsnotify_mark_connector __rcu *fsnotify_connp_t;
0465 
0466 /*
0467  * Inode/vfsmount/sb point to this structure which tracks all marks attached to
0468  * the inode/vfsmount/sb. The reference to inode/vfsmount/sb is held by this
0469  * structure. We destroy this structure when there are no more marks attached
0470  * to it. The structure is protected by fsnotify_mark_srcu.
0471  */
0472 struct fsnotify_mark_connector {
0473     spinlock_t lock;
0474     unsigned short type;    /* Type of object [lock] */
0475 #define FSNOTIFY_CONN_FLAG_HAS_FSID 0x01
0476 #define FSNOTIFY_CONN_FLAG_HAS_IREF 0x02
0477     unsigned short flags;   /* flags [lock] */
0478     __kernel_fsid_t fsid;   /* fsid of filesystem containing object */
0479     union {
0480         /* Object pointer [lock] */
0481         fsnotify_connp_t *obj;
0482         /* Used listing heads to free after srcu period expires */
0483         struct fsnotify_mark_connector *destroy_next;
0484     };
0485     struct hlist_head list;
0486 };
0487 
0488 /*
0489  * A mark is simply an object attached to an in core inode which allows an
0490  * fsnotify listener to indicate they are either no longer interested in events
0491  * of a type matching mask or only interested in those events.
0492  *
0493  * These are flushed when an inode is evicted from core and may be flushed
0494  * when the inode is modified (as seen by fsnotify_access).  Some fsnotify
0495  * users (such as dnotify) will flush these when the open fd is closed and not
0496  * at inode eviction or modification.
0497  *
0498  * Text in brackets is showing the lock(s) protecting modifications of a
0499  * particular entry. obj_lock means either inode->i_lock or
0500  * mnt->mnt_root->d_lock depending on the mark type.
0501  */
0502 struct fsnotify_mark {
0503     /* Mask this mark is for [mark->lock, group->mark_mutex] */
0504     __u32 mask;
0505     /* We hold one for presence in g_list. Also one ref for each 'thing'
0506      * in kernel that found and may be using this mark. */
0507     refcount_t refcnt;
0508     /* Group this mark is for. Set on mark creation, stable until last ref
0509      * is dropped */
0510     struct fsnotify_group *group;
0511     /* List of marks by group->marks_list. Also reused for queueing
0512      * mark into destroy_list when it's waiting for the end of SRCU period
0513      * before it can be freed. [group->mark_mutex] */
0514     struct list_head g_list;
0515     /* Protects inode / mnt pointers, flags, masks */
0516     spinlock_t lock;
0517     /* List of marks for inode / vfsmount [connector->lock, mark ref] */
0518     struct hlist_node obj_list;
0519     /* Head of list of marks for an object [mark ref] */
0520     struct fsnotify_mark_connector *connector;
0521     /* Events types and flags to ignore [mark->lock, group->mark_mutex] */
0522     __u32 ignore_mask;
0523     /* General fsnotify mark flags */
0524 #define FSNOTIFY_MARK_FLAG_ALIVE        0x0001
0525 #define FSNOTIFY_MARK_FLAG_ATTACHED     0x0002
0526     /* inotify mark flags */
0527 #define FSNOTIFY_MARK_FLAG_EXCL_UNLINK      0x0010
0528 #define FSNOTIFY_MARK_FLAG_IN_ONESHOT       0x0020
0529     /* fanotify mark flags */
0530 #define FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY  0x0100
0531 #define FSNOTIFY_MARK_FLAG_NO_IREF      0x0200
0532 #define FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS 0x0400
0533     unsigned int flags;     /* flags [mark->lock] */
0534 };
0535 
0536 #ifdef CONFIG_FSNOTIFY
0537 
0538 /* called from the vfs helpers */
0539 
0540 /* main fsnotify call to send events */
0541 extern int fsnotify(__u32 mask, const void *data, int data_type,
0542             struct inode *dir, const struct qstr *name,
0543             struct inode *inode, u32 cookie);
0544 extern int __fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data,
0545                int data_type);
0546 extern void __fsnotify_inode_delete(struct inode *inode);
0547 extern void __fsnotify_vfsmount_delete(struct vfsmount *mnt);
0548 extern void fsnotify_sb_delete(struct super_block *sb);
0549 extern u32 fsnotify_get_cookie(void);
0550 
0551 static inline __u32 fsnotify_parent_needed_mask(__u32 mask)
0552 {
0553     /* FS_EVENT_ON_CHILD is set on marks that want parent/name info */
0554     if (!(mask & FS_EVENT_ON_CHILD))
0555         return 0;
0556     /*
0557      * This object might be watched by a mark that cares about parent/name
0558      * info, does it care about the specific set of events that can be
0559      * reported with parent/name info?
0560      */
0561     return mask & FS_EVENTS_POSS_TO_PARENT;
0562 }
0563 
0564 static inline int fsnotify_inode_watches_children(struct inode *inode)
0565 {
0566     /* FS_EVENT_ON_CHILD is set if the inode may care */
0567     if (!(inode->i_fsnotify_mask & FS_EVENT_ON_CHILD))
0568         return 0;
0569     /* this inode might care about child events, does it care about the
0570      * specific set of events that can happen on a child? */
0571     return inode->i_fsnotify_mask & FS_EVENTS_POSS_ON_CHILD;
0572 }
0573 
0574 /*
0575  * Update the dentry with a flag indicating the interest of its parent to receive
0576  * filesystem events when those events happens to this dentry->d_inode.
0577  */
0578 static inline void fsnotify_update_flags(struct dentry *dentry)
0579 {
0580     assert_spin_locked(&dentry->d_lock);
0581 
0582     /*
0583      * Serialisation of setting PARENT_WATCHED on the dentries is provided
0584      * by d_lock. If inotify_inode_watched changes after we have taken
0585      * d_lock, the following __fsnotify_update_child_dentry_flags call will
0586      * find our entry, so it will spin until we complete here, and update
0587      * us with the new state.
0588      */
0589     if (fsnotify_inode_watches_children(dentry->d_parent->d_inode))
0590         dentry->d_flags |= DCACHE_FSNOTIFY_PARENT_WATCHED;
0591     else
0592         dentry->d_flags &= ~DCACHE_FSNOTIFY_PARENT_WATCHED;
0593 }
0594 
0595 /* called from fsnotify listeners, such as fanotify or dnotify */
0596 
0597 /* create a new group */
0598 extern struct fsnotify_group *fsnotify_alloc_group(
0599                 const struct fsnotify_ops *ops,
0600                 int flags);
0601 /* get reference to a group */
0602 extern void fsnotify_get_group(struct fsnotify_group *group);
0603 /* drop reference on a group from fsnotify_alloc_group */
0604 extern void fsnotify_put_group(struct fsnotify_group *group);
0605 /* group destruction begins, stop queuing new events */
0606 extern void fsnotify_group_stop_queueing(struct fsnotify_group *group);
0607 /* destroy group */
0608 extern void fsnotify_destroy_group(struct fsnotify_group *group);
0609 /* fasync handler function */
0610 extern int fsnotify_fasync(int fd, struct file *file, int on);
0611 /* Free event from memory */
0612 extern void fsnotify_destroy_event(struct fsnotify_group *group,
0613                    struct fsnotify_event *event);
0614 /* attach the event to the group notification queue */
0615 extern int fsnotify_insert_event(struct fsnotify_group *group,
0616                  struct fsnotify_event *event,
0617                  int (*merge)(struct fsnotify_group *,
0618                           struct fsnotify_event *),
0619                  void (*insert)(struct fsnotify_group *,
0620                         struct fsnotify_event *));
0621 
0622 static inline int fsnotify_add_event(struct fsnotify_group *group,
0623                      struct fsnotify_event *event,
0624                      int (*merge)(struct fsnotify_group *,
0625                           struct fsnotify_event *))
0626 {
0627     return fsnotify_insert_event(group, event, merge, NULL);
0628 }
0629 
0630 /* Queue overflow event to a notification group */
0631 static inline void fsnotify_queue_overflow(struct fsnotify_group *group)
0632 {
0633     fsnotify_add_event(group, group->overflow_event, NULL);
0634 }
0635 
0636 static inline bool fsnotify_is_overflow_event(u32 mask)
0637 {
0638     return mask & FS_Q_OVERFLOW;
0639 }
0640 
0641 static inline bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group)
0642 {
0643     assert_spin_locked(&group->notification_lock);
0644 
0645     return list_empty(&group->notification_list);
0646 }
0647 
0648 extern bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group);
0649 /* return, but do not dequeue the first event on the notification queue */
0650 extern struct fsnotify_event *fsnotify_peek_first_event(struct fsnotify_group *group);
0651 /* return AND dequeue the first event on the notification queue */
0652 extern struct fsnotify_event *fsnotify_remove_first_event(struct fsnotify_group *group);
0653 /* Remove event queued in the notification list */
0654 extern void fsnotify_remove_queued_event(struct fsnotify_group *group,
0655                      struct fsnotify_event *event);
0656 
0657 /* functions used to manipulate the marks attached to inodes */
0658 
0659 /*
0660  * Canonical "ignore mask" including event flags.
0661  *
0662  * Note the subtle semantic difference from the legacy ->ignored_mask.
0663  * ->ignored_mask traditionally only meant which events should be ignored,
0664  * while ->ignore_mask also includes flags regarding the type of objects on
0665  * which events should be ignored.
0666  */
0667 static inline __u32 fsnotify_ignore_mask(struct fsnotify_mark *mark)
0668 {
0669     __u32 ignore_mask = mark->ignore_mask;
0670 
0671     /* The event flags in ignore mask take effect */
0672     if (mark->flags & FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS)
0673         return ignore_mask;
0674 
0675     /*
0676      * Legacy behavior:
0677      * - Always ignore events on dir
0678      * - Ignore events on child if parent is watching children
0679      */
0680     ignore_mask |= FS_ISDIR;
0681     ignore_mask &= ~FS_EVENT_ON_CHILD;
0682     ignore_mask |= mark->mask & FS_EVENT_ON_CHILD;
0683 
0684     return ignore_mask;
0685 }
0686 
0687 /* Legacy ignored_mask - only event types to ignore */
0688 static inline __u32 fsnotify_ignored_events(struct fsnotify_mark *mark)
0689 {
0690     return mark->ignore_mask & ALL_FSNOTIFY_EVENTS;
0691 }
0692 
0693 /*
0694  * Check if mask (or ignore mask) should be applied depending if victim is a
0695  * directory and whether it is reported to a watching parent.
0696  */
0697 static inline bool fsnotify_mask_applicable(__u32 mask, bool is_dir,
0698                         int iter_type)
0699 {
0700     /* Should mask be applied to a directory? */
0701     if (is_dir && !(mask & FS_ISDIR))
0702         return false;
0703 
0704     /* Should mask be applied to a child? */
0705     if (iter_type == FSNOTIFY_ITER_TYPE_PARENT &&
0706         !(mask & FS_EVENT_ON_CHILD))
0707         return false;
0708 
0709     return true;
0710 }
0711 
0712 /*
0713  * Effective ignore mask taking into account if event victim is a
0714  * directory and whether it is reported to a watching parent.
0715  */
0716 static inline __u32 fsnotify_effective_ignore_mask(struct fsnotify_mark *mark,
0717                            bool is_dir, int iter_type)
0718 {
0719     __u32 ignore_mask = fsnotify_ignored_events(mark);
0720 
0721     if (!ignore_mask)
0722         return 0;
0723 
0724     /* For non-dir and non-child, no need to consult the event flags */
0725     if (!is_dir && iter_type != FSNOTIFY_ITER_TYPE_PARENT)
0726         return ignore_mask;
0727 
0728     ignore_mask = fsnotify_ignore_mask(mark);
0729     if (!fsnotify_mask_applicable(ignore_mask, is_dir, iter_type))
0730         return 0;
0731 
0732     return ignore_mask & ALL_FSNOTIFY_EVENTS;
0733 }
0734 
0735 /* Get mask for calculating object interest taking ignore mask into account */
0736 static inline __u32 fsnotify_calc_mask(struct fsnotify_mark *mark)
0737 {
0738     __u32 mask = mark->mask;
0739 
0740     if (!fsnotify_ignored_events(mark))
0741         return mask;
0742 
0743     /* Interest in FS_MODIFY may be needed for clearing ignore mask */
0744     if (!(mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY))
0745         mask |= FS_MODIFY;
0746 
0747     /*
0748      * If mark is interested in ignoring events on children, the object must
0749      * show interest in those events for fsnotify_parent() to notice it.
0750      */
0751     return mask | mark->ignore_mask;
0752 }
0753 
0754 /* Get mask of events for a list of marks */
0755 extern __u32 fsnotify_conn_mask(struct fsnotify_mark_connector *conn);
0756 /* Calculate mask of events for a list of marks */
0757 extern void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn);
0758 extern void fsnotify_init_mark(struct fsnotify_mark *mark,
0759                    struct fsnotify_group *group);
0760 /* Find mark belonging to given group in the list of marks */
0761 extern struct fsnotify_mark *fsnotify_find_mark(fsnotify_connp_t *connp,
0762                         struct fsnotify_group *group);
0763 /* Get cached fsid of filesystem containing object */
0764 extern int fsnotify_get_conn_fsid(const struct fsnotify_mark_connector *conn,
0765                   __kernel_fsid_t *fsid);
0766 /* attach the mark to the object */
0767 extern int fsnotify_add_mark(struct fsnotify_mark *mark,
0768                  fsnotify_connp_t *connp, unsigned int obj_type,
0769                  int add_flags, __kernel_fsid_t *fsid);
0770 extern int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
0771                     fsnotify_connp_t *connp,
0772                     unsigned int obj_type, int add_flags,
0773                     __kernel_fsid_t *fsid);
0774 
0775 /* attach the mark to the inode */
0776 static inline int fsnotify_add_inode_mark(struct fsnotify_mark *mark,
0777                       struct inode *inode,
0778                       int add_flags)
0779 {
0780     return fsnotify_add_mark(mark, &inode->i_fsnotify_marks,
0781                  FSNOTIFY_OBJ_TYPE_INODE, add_flags, NULL);
0782 }
0783 static inline int fsnotify_add_inode_mark_locked(struct fsnotify_mark *mark,
0784                          struct inode *inode,
0785                          int add_flags)
0786 {
0787     return fsnotify_add_mark_locked(mark, &inode->i_fsnotify_marks,
0788                     FSNOTIFY_OBJ_TYPE_INODE, add_flags,
0789                     NULL);
0790 }
0791 
0792 /* given a group and a mark, flag mark to be freed when all references are dropped */
0793 extern void fsnotify_destroy_mark(struct fsnotify_mark *mark,
0794                   struct fsnotify_group *group);
0795 /* detach mark from inode / mount list, group list, drop inode reference */
0796 extern void fsnotify_detach_mark(struct fsnotify_mark *mark);
0797 /* free mark */
0798 extern void fsnotify_free_mark(struct fsnotify_mark *mark);
0799 /* Wait until all marks queued for destruction are destroyed */
0800 extern void fsnotify_wait_marks_destroyed(void);
0801 /* Clear all of the marks of a group attached to a given object type */
0802 extern void fsnotify_clear_marks_by_group(struct fsnotify_group *group,
0803                       unsigned int obj_type);
0804 /* run all the marks in a group, and clear all of the vfsmount marks */
0805 static inline void fsnotify_clear_vfsmount_marks_by_group(struct fsnotify_group *group)
0806 {
0807     fsnotify_clear_marks_by_group(group, FSNOTIFY_OBJ_TYPE_VFSMOUNT);
0808 }
0809 /* run all the marks in a group, and clear all of the inode marks */
0810 static inline void fsnotify_clear_inode_marks_by_group(struct fsnotify_group *group)
0811 {
0812     fsnotify_clear_marks_by_group(group, FSNOTIFY_OBJ_TYPE_INODE);
0813 }
0814 /* run all the marks in a group, and clear all of the sn marks */
0815 static inline void fsnotify_clear_sb_marks_by_group(struct fsnotify_group *group)
0816 {
0817     fsnotify_clear_marks_by_group(group, FSNOTIFY_OBJ_TYPE_SB);
0818 }
0819 extern void fsnotify_get_mark(struct fsnotify_mark *mark);
0820 extern void fsnotify_put_mark(struct fsnotify_mark *mark);
0821 extern void fsnotify_finish_user_wait(struct fsnotify_iter_info *iter_info);
0822 extern bool fsnotify_prepare_user_wait(struct fsnotify_iter_info *iter_info);
0823 
0824 static inline void fsnotify_init_event(struct fsnotify_event *event)
0825 {
0826     INIT_LIST_HEAD(&event->list);
0827 }
0828 
0829 #else
0830 
0831 static inline int fsnotify(__u32 mask, const void *data, int data_type,
0832                struct inode *dir, const struct qstr *name,
0833                struct inode *inode, u32 cookie)
0834 {
0835     return 0;
0836 }
0837 
0838 static inline int __fsnotify_parent(struct dentry *dentry, __u32 mask,
0839                   const void *data, int data_type)
0840 {
0841     return 0;
0842 }
0843 
0844 static inline void __fsnotify_inode_delete(struct inode *inode)
0845 {}
0846 
0847 static inline void __fsnotify_vfsmount_delete(struct vfsmount *mnt)
0848 {}
0849 
0850 static inline void fsnotify_sb_delete(struct super_block *sb)
0851 {}
0852 
0853 static inline void fsnotify_update_flags(struct dentry *dentry)
0854 {}
0855 
0856 static inline u32 fsnotify_get_cookie(void)
0857 {
0858     return 0;
0859 }
0860 
0861 static inline void fsnotify_unmount_inodes(struct super_block *sb)
0862 {}
0863 
0864 #endif  /* CONFIG_FSNOTIFY */
0865 
0866 #endif  /* __KERNEL __ */
0867 
0868 #endif  /* __LINUX_FSNOTIFY_BACKEND_H */