1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
290586523SEric Paris /*
390586523SEric Paris * Filesystem access notification for Linux
490586523SEric Paris *
590586523SEric Paris * Copyright (C) 2008 Red Hat, Inc., Eric Paris <[email protected]>
690586523SEric Paris */
790586523SEric Paris
890586523SEric Paris #ifndef __LINUX_FSNOTIFY_BACKEND_H
990586523SEric Paris #define __LINUX_FSNOTIFY_BACKEND_H
1090586523SEric Paris
1190586523SEric Paris #ifdef __KERNEL__
1290586523SEric Paris
1363c882a0SEric Paris #include <linux/idr.h> /* inotify uses this */
1490586523SEric Paris #include <linux/fs.h> /* struct inode */
1590586523SEric Paris #include <linux/list.h>
1690586523SEric Paris #include <linux/path.h> /* struct path */
1790586523SEric Paris #include <linux/spinlock.h>
1890586523SEric Paris #include <linux/types.h>
1960063497SArun Sharma #include <linux/atomic.h>
201cce1eeaSNikolay Borisov #include <linux/user_namespace.h>
217761daa6SElena Reshetova #include <linux/refcount.h>
22734a1a5eSGabriel Krisman Bertazi #include <linux/mempool.h>
2343b245a7SAmir Goldstein #include <linux/sched/mm.h>
2490586523SEric Paris
2590586523SEric Paris /*
2690586523SEric Paris * IN_* from inotfy.h lines up EXACTLY with FS_*, this is so we can easily
2790586523SEric Paris * convert between them. dnotify only needs conversion at watch creation
2890586523SEric Paris * so no perf loss there. fanotify isn't defined yet, so it can use the
2990586523SEric Paris * wholes if it needs more events.
3090586523SEric Paris */
3190586523SEric Paris #define FS_ACCESS 0x00000001 /* File was accessed */
3290586523SEric Paris #define FS_MODIFY 0x00000002 /* File was modified */
3390586523SEric Paris #define FS_ATTRIB 0x00000004 /* Metadata changed */
349fe0c03fSVicki Pfau #define FS_CLOSE_WRITE 0x00000008 /* Writable file was closed */
359fe0c03fSVicki Pfau #define FS_CLOSE_NOWRITE 0x00000010 /* Unwritable file closed */
3690586523SEric Paris #define FS_OPEN 0x00000020 /* File was opened */
3790586523SEric Paris #define FS_MOVED_FROM 0x00000040 /* File was moved from X */
3890586523SEric Paris #define FS_MOVED_TO 0x00000080 /* File was moved to Y */
3990586523SEric Paris #define FS_CREATE 0x00000100 /* Subfile was created */
4090586523SEric Paris #define FS_DELETE 0x00000200 /* Subfile was deleted */
4190586523SEric Paris #define FS_DELETE_SELF 0x00000400 /* Self was deleted */
4290586523SEric Paris #define FS_MOVE_SELF 0x00000800 /* Self was moved */
439b076f1cSMatthew Bobrowski #define FS_OPEN_EXEC 0x00001000 /* File was opened for exec */
4490586523SEric Paris
4590586523SEric Paris #define FS_UNMOUNT 0x00002000 /* inode on umount fs */
4690586523SEric Paris #define FS_Q_OVERFLOW 0x00004000 /* Event queued overflowed */
479daa8110SGabriel Krisman Bertazi #define FS_ERROR 0x00008000 /* Filesystem Error (fanotify) */
489daa8110SGabriel Krisman Bertazi
499daa8110SGabriel Krisman Bertazi /*
509daa8110SGabriel Krisman Bertazi * FS_IN_IGNORED overloads FS_ERROR. It is only used internally by inotify
519daa8110SGabriel Krisman Bertazi * which does not support FS_ERROR.
529daa8110SGabriel Krisman Bertazi */
5390586523SEric Paris #define FS_IN_IGNORED 0x00008000 /* last inotify event here */
5490586523SEric Paris
55c4ec54b4SEric Paris #define FS_OPEN_PERM 0x00010000 /* open event in an permission hook */
56c4ec54b4SEric Paris #define FS_ACCESS_PERM 0x00020000 /* access event in a permissions hook */
5766917a31SMatthew Bobrowski #define FS_OPEN_EXEC_PERM 0x00040000 /* open/exec event in a permission hook */
580a076036SAmir Goldstein /* #define FS_DIR_MODIFY 0x00080000 */ /* Deprecated (reserved) */
59c4ec54b4SEric Paris
60f156524eSAmir Goldstein #define FS_PRE_ACCESS 0x00100000 /* Pre-content access hook */
61f156524eSAmir Goldstein
62*b944249bSMiklos Szeredi #define FS_MNT_ATTACH 0x01000000 /* Mount was attached */
63*b944249bSMiklos Szeredi #define FS_MNT_DETACH 0x02000000 /* Mount was detached */
64*b944249bSMiklos Szeredi #define FS_MNT_MOVE (FS_MNT_ATTACH | FS_MNT_DETACH)
65*b944249bSMiklos Szeredi
669b93f331SAmir Goldstein /*
679b93f331SAmir Goldstein * Set on inode mark that cares about things that happen to its children.
689b93f331SAmir Goldstein * Always set for dnotify and inotify.
699b93f331SAmir Goldstein * Set on inode/sb/mount marks that care about parent/name info.
709b93f331SAmir Goldstein */
71c28f7e56SEric Paris #define FS_EVENT_ON_CHILD 0x08000000
72c28f7e56SEric Paris
73e54183faSAmir Goldstein #define FS_RENAME 0x10000000 /* File was renamed */
746473ea76SAmir Goldstein #define FS_DN_MULTISHOT 0x20000000 /* dnotify multishot */
756473ea76SAmir Goldstein #define FS_ISDIR 0x40000000 /* event occurred against dir */
766473ea76SAmir Goldstein
77e9fd702aSEric Paris #define FS_MOVE (FS_MOVED_FROM | FS_MOVED_TO)
78e9fd702aSEric Paris
79e220140fSAmir Goldstein /*
80e220140fSAmir Goldstein * Directory entry modification events - reported only to directory
81e220140fSAmir Goldstein * where entry is modified and not to a watching parent.
82e220140fSAmir Goldstein * The watching parent may get an FS_ATTRIB|FS_EVENT_ON_CHILD event
83e220140fSAmir Goldstein * when a directory entry inside a child subdir changes.
84e220140fSAmir Goldstein */
85e54183faSAmir Goldstein #define ALL_FSNOTIFY_DIRENT_EVENTS (FS_CREATE | FS_DELETE | FS_MOVE | FS_RENAME)
86e220140fSAmir Goldstein
87*b944249bSMiklos Szeredi /* Mount namespace events */
88*b944249bSMiklos Szeredi #define FSNOTIFY_MNT_EVENTS (FS_MNT_ATTACH | FS_MNT_DETACH)
89*b944249bSMiklos Szeredi
90f156524eSAmir Goldstein /* Content events can be used to inspect file content */
91f156524eSAmir Goldstein #define FSNOTIFY_CONTENT_PERM_EVENTS (FS_OPEN_PERM | FS_OPEN_EXEC_PERM | \
92f156524eSAmir Goldstein FS_ACCESS_PERM)
93318652e0SAmir Goldstein /* Pre-content events can be used to fill file content */
94f156524eSAmir Goldstein #define FSNOTIFY_PRE_CONTENT_EVENTS (FS_PRE_ACCESS)
95318652e0SAmir Goldstein
96f156524eSAmir Goldstein #define ALL_FSNOTIFY_PERM_EVENTS (FSNOTIFY_CONTENT_PERM_EVENTS | \
97f156524eSAmir Goldstein FSNOTIFY_PRE_CONTENT_EVENTS)
98ff8bcbd0SEric Paris
99e220140fSAmir Goldstein /*
1009b93f331SAmir Goldstein * This is a list of all events that may get sent to a parent that is watching
1019b93f331SAmir Goldstein * with flag FS_EVENT_ON_CHILD based on fs event on a child of that directory.
102e220140fSAmir Goldstein */
103e220140fSAmir Goldstein #define FS_EVENTS_POSS_ON_CHILD (ALL_FSNOTIFY_PERM_EVENTS | \
104e220140fSAmir Goldstein FS_ACCESS | FS_MODIFY | FS_ATTRIB | \
105e220140fSAmir Goldstein FS_CLOSE_WRITE | FS_CLOSE_NOWRITE | \
106e220140fSAmir Goldstein FS_OPEN | FS_OPEN_EXEC)
107e220140fSAmir Goldstein
1089b93f331SAmir Goldstein /*
1099b93f331SAmir Goldstein * This is a list of all events that may get sent with the parent inode as the
1109b93f331SAmir Goldstein * @to_tell argument of fsnotify().
1119b93f331SAmir Goldstein * It may include events that can be sent to an inode/sb/mount mark, but cannot
1129b93f331SAmir Goldstein * be sent to a parent watching children.
1139b93f331SAmir Goldstein */
1149b93f331SAmir Goldstein #define FS_EVENTS_POSS_TO_PARENT (FS_EVENTS_POSS_ON_CHILD)
1159b93f331SAmir Goldstein
116007d1e83SAmir Goldstein /* Events that can be reported to backends */
117e220140fSAmir Goldstein #define ALL_FSNOTIFY_EVENTS (ALL_FSNOTIFY_DIRENT_EVENTS | \
118*b944249bSMiklos Szeredi FSNOTIFY_MNT_EVENTS | \
119e220140fSAmir Goldstein FS_EVENTS_POSS_ON_CHILD | \
120e54183faSAmir Goldstein FS_DELETE_SELF | FS_MOVE_SELF | \
1219daa8110SGabriel Krisman Bertazi FS_UNMOUNT | FS_Q_OVERFLOW | FS_IN_IGNORED | \
1229daa8110SGabriel Krisman Bertazi FS_ERROR)
123007d1e83SAmir Goldstein
124007d1e83SAmir Goldstein /* Extra flags that may be reported with event or control handling of events */
12538035c04SAmir Goldstein #define ALL_FSNOTIFY_FLAGS (FS_ISDIR | FS_EVENT_ON_CHILD | FS_DN_MULTISHOT)
12620dee624SEric Paris
127007d1e83SAmir Goldstein #define ALL_FSNOTIFY_BITS (ALL_FSNOTIFY_EVENTS | ALL_FSNOTIFY_FLAGS)
128007d1e83SAmir Goldstein
12990586523SEric Paris struct fsnotify_group;
13090586523SEric Paris struct fsnotify_event;
131e61ce867SEric Paris struct fsnotify_mark;
132e4aff117SEric Paris struct fsnotify_event_private_data;
1337053aee2SJan Kara struct fsnotify_fname;
134abc77577SJan Kara struct fsnotify_iter_info;
13590586523SEric Paris
136d46eb14bSShakeel Butt struct mem_cgroup;
137d46eb14bSShakeel Butt
13890586523SEric Paris /*
13990586523SEric Paris * Each group much define these ops. The fsnotify infrastructure will call
14090586523SEric Paris * these operations for each relevant group.
14190586523SEric Paris *
14290586523SEric Paris * handle_event - main call for a group to handle an fs event
143b54cecf5SAmir Goldstein * @group: group to notify
144b54cecf5SAmir Goldstein * @mask: event type and flags
145b54cecf5SAmir Goldstein * @data: object that event happened on
146b54cecf5SAmir Goldstein * @data_type: type of object for fanotify_data_XXX() accessors
147b54cecf5SAmir Goldstein * @dir: optional directory associated with event -
148b54cecf5SAmir Goldstein * if @file_name is not NULL, this is the directory that
149b54cecf5SAmir Goldstein * @file_name is relative to
150b54cecf5SAmir Goldstein * @file_name: optional file name associated with event
151b54cecf5SAmir Goldstein * @cookie: inotify rename cookie
152b54cecf5SAmir Goldstein * @iter_info: array of marks from this group that are interested in the event
153b54cecf5SAmir Goldstein *
154b9a1b977SAmir Goldstein * handle_inode_event - simple variant of handle_event() for groups that only
155b9a1b977SAmir Goldstein * have inode marks and don't have ignore mask
156b9a1b977SAmir Goldstein * @mark: mark to notify
157b9a1b977SAmir Goldstein * @mask: event type and flags
158b9a1b977SAmir Goldstein * @inode: inode that event happened on
159b9a1b977SAmir Goldstein * @dir: optional directory associated with event -
160b9a1b977SAmir Goldstein * if @file_name is not NULL, this is the directory that
161b9a1b977SAmir Goldstein * @file_name is relative to.
16224dca905SGabriel Krisman Bertazi * Either @inode or @dir must be non-NULL.
163b9a1b977SAmir Goldstein * @file_name: optional file name associated with event
164950cc0d2SAmir Goldstein * @cookie: inotify rename cookie
165b9a1b977SAmir Goldstein *
16690586523SEric Paris * free_group_priv - called when a group refcnt hits 0 to clean up the private union
1676960b0d9SLino Sanfilippo * freeing_mark - called when a mark is being destroyed for some reason. The group
1686960b0d9SLino Sanfilippo * MUST be holding a reference on each mark and that reference must be
1696960b0d9SLino Sanfilippo * dropped in this function. inotify uses this function to send
1706960b0d9SLino Sanfilippo * userspace messages that marks have been removed.
17190586523SEric Paris */
17290586523SEric Paris struct fsnotify_ops {
173b54cecf5SAmir Goldstein int (*handle_event)(struct fsnotify_group *group, u32 mask,
174b54cecf5SAmir Goldstein const void *data, int data_type, struct inode *dir,
175e43e9c33SAl Viro const struct qstr *file_name, u32 cookie,
1769385a84dSJan Kara struct fsnotify_iter_info *iter_info);
177b9a1b977SAmir Goldstein int (*handle_inode_event)(struct fsnotify_mark *mark, u32 mask,
178b9a1b977SAmir Goldstein struct inode *inode, struct inode *dir,
179950cc0d2SAmir Goldstein const struct qstr *file_name, u32 cookie);
18090586523SEric Paris void (*free_group_priv)(struct fsnotify_group *group);
181841bdc10SEric Paris void (*freeing_mark)(struct fsnotify_mark *mark, struct fsnotify_group *group);
182330ae77dSGabriel Krisman Bertazi void (*free_event)(struct fsnotify_group *group, struct fsnotify_event *event);
183054c636eSJan Kara /* called on final put+free to free memory */
184054c636eSJan Kara void (*free_mark)(struct fsnotify_mark *mark);
1857053aee2SJan Kara };
1867053aee2SJan Kara
1877053aee2SJan Kara /*
1887053aee2SJan Kara * all of the information about the original object we want to now send to
1897053aee2SJan Kara * a group. If you want to carry more info from the accessing task to the
1907053aee2SJan Kara * listener this structure is where you need to be adding fields.
1917053aee2SJan Kara */
1927053aee2SJan Kara struct fsnotify_event {
1937053aee2SJan Kara struct list_head list;
19490586523SEric Paris };
19590586523SEric Paris
19690586523SEric Paris /*
197477cf917SAmir Goldstein * fsnotify group priorities.
198477cf917SAmir Goldstein * Events are sent in order from highest priority to lowest priority.
199477cf917SAmir Goldstein */
200477cf917SAmir Goldstein enum fsnotify_group_prio {
201477cf917SAmir Goldstein FSNOTIFY_PRIO_NORMAL = 0, /* normal notifiers, no permissions */
202477cf917SAmir Goldstein FSNOTIFY_PRIO_CONTENT, /* fanotify permission events */
203477cf917SAmir Goldstein FSNOTIFY_PRIO_PRE_CONTENT, /* fanotify pre-content events */
204477cf917SAmir Goldstein __FSNOTIFY_PRIO_NUM
205477cf917SAmir Goldstein };
206477cf917SAmir Goldstein
207477cf917SAmir Goldstein /*
20890586523SEric Paris * A group is a "thing" that wants to receive notification about filesystem
20990586523SEric Paris * events. The mask holds the subset of event types this group cares about.
21090586523SEric Paris * refcnt on a group is up to the implementor and at any moment if it goes 0
21190586523SEric Paris * everything will be cleaned up.
21290586523SEric Paris */
21390586523SEric Paris struct fsnotify_group {
214d46eb14bSShakeel Butt const struct fsnotify_ops *ops; /* how this group handles things */
215d46eb14bSShakeel Butt
21690586523SEric Paris /*
21790586523SEric Paris * How the refcnt is used is up to each group. When the refcnt hits 0
21890586523SEric Paris * fsnotify will clean up all of the resources associated with this group.
21990586523SEric Paris * As an example, the dnotify group will always have a refcnt=1 and that
22090586523SEric Paris * will never change. Inotify, on the other hand, has a group per
22190586523SEric Paris * inotify_init() and the refcnt will hit 0 only when that fd has been
22290586523SEric Paris * closed.
22390586523SEric Paris */
2247761daa6SElena Reshetova refcount_t refcnt; /* things with interest in this group */
22590586523SEric Paris
226a2d8bc6cSEric Paris /* needed to send notification to userspace */
227c21dbe20SJan Kara spinlock_t notification_lock; /* protect the notification_list */
228a2d8bc6cSEric Paris struct list_head notification_list; /* list of event_holder this group needs to send to userspace */
229a2d8bc6cSEric Paris wait_queue_head_t notification_waitq; /* read() on the notification file blocks on this waitq */
230a2d8bc6cSEric Paris unsigned int q_len; /* events on the queue */
231a2d8bc6cSEric Paris unsigned int max_events; /* maximum events allowed on the list */
232477cf917SAmir Goldstein enum fsnotify_group_prio priority; /* priority for sending events */
23312703dbfSJan Kara bool shutdown; /* group is being shut down, don't queue more events */
234a2d8bc6cSEric Paris
235867a448dSAmir Goldstein #define FSNOTIFY_GROUP_USER 0x01 /* user allocated group */
236f3010343SAmir Goldstein #define FSNOTIFY_GROUP_DUPS 0x02 /* allow multiple marks per object */
237867a448dSAmir Goldstein int flags;
23843b245a7SAmir Goldstein unsigned int owner_flags; /* stored flags of mark_mutex owner */
239867a448dSAmir Goldstein
240e61ce867SEric Paris /* stores all fastpath marks assoc with this group so they can be cleaned on unregister */
241986ab098SLino Sanfilippo struct mutex mark_mutex; /* protect marks_list */
242d46eb14bSShakeel Butt atomic_t user_waits; /* Number of tasks waiting for user
243d46eb14bSShakeel Butt * response */
244e61ce867SEric Paris struct list_head marks_list; /* all inode marks for this group */
2453be25f49SEric Paris
2460a6b6bd5SEric Paris struct fasync_struct *fsn_fa; /* async notification */
2470a6b6bd5SEric Paris
248ff57cd58SJan Kara struct fsnotify_event *overflow_event; /* Event we queue when the
2497053aee2SJan Kara * notification list is too
2507053aee2SJan Kara * full */
251d46eb14bSShakeel Butt
252d46eb14bSShakeel Butt struct mem_cgroup *memcg; /* memcg to charge allocations */
2537053aee2SJan Kara
25490586523SEric Paris /* groups can define private fields here or use the void *private */
25590586523SEric Paris union {
25690586523SEric Paris void *private;
25763c882a0SEric Paris #ifdef CONFIG_INOTIFY_USER
25863c882a0SEric Paris struct inotify_group_private_data {
25963c882a0SEric Paris spinlock_t idr_lock;
26063c882a0SEric Paris struct idr idr;
2611cce1eeaSNikolay Borisov struct ucounts *ucounts;
26263c882a0SEric Paris } inotify_data;
26363c882a0SEric Paris #endif
26480af2588SEric Paris #ifdef CONFIG_FANOTIFY
2659e66e423SEric Paris struct fanotify_group_private_data {
26694e00d28SAmir Goldstein /* Hash table of events for merge */
26794e00d28SAmir Goldstein struct hlist_head *merge_hash;
2689e66e423SEric Paris /* allows a group to block waiting for a userspace response */
2699e66e423SEric Paris struct list_head access_list;
2709e66e423SEric Paris wait_queue_head_t access_waitq;
27196a71f21SAmir Goldstein int flags; /* flags from fanotify_init() */
27296a71f21SAmir Goldstein int f_flags; /* event_f_flags from fanotify_init() */
2735b8fea65SAmir Goldstein struct ucounts *ucounts;
274734a1a5eSGabriel Krisman Bertazi mempool_t error_events_pool;
2759e66e423SEric Paris } fanotify_data;
27680af2588SEric Paris #endif /* CONFIG_FANOTIFY */
27790586523SEric Paris };
27890586523SEric Paris };
27990586523SEric Paris
28043b245a7SAmir Goldstein /*
28143b245a7SAmir Goldstein * These helpers are used to prevent deadlock when reclaiming inodes with
28243b245a7SAmir Goldstein * evictable marks of the same group that is allocating a new mark.
28343b245a7SAmir Goldstein */
fsnotify_group_lock(struct fsnotify_group * group)28443b245a7SAmir Goldstein static inline void fsnotify_group_lock(struct fsnotify_group *group)
28543b245a7SAmir Goldstein {
28643b245a7SAmir Goldstein mutex_lock(&group->mark_mutex);
28743b245a7SAmir Goldstein group->owner_flags = memalloc_nofs_save();
28843b245a7SAmir Goldstein }
28943b245a7SAmir Goldstein
fsnotify_group_unlock(struct fsnotify_group * group)29043b245a7SAmir Goldstein static inline void fsnotify_group_unlock(struct fsnotify_group *group)
29143b245a7SAmir Goldstein {
29243b245a7SAmir Goldstein memalloc_nofs_restore(group->owner_flags);
29343b245a7SAmir Goldstein mutex_unlock(&group->mark_mutex);
29443b245a7SAmir Goldstein }
29543b245a7SAmir Goldstein
fsnotify_group_assert_locked(struct fsnotify_group * group)29643b245a7SAmir Goldstein static inline void fsnotify_group_assert_locked(struct fsnotify_group *group)
29743b245a7SAmir Goldstein {
29843b245a7SAmir Goldstein WARN_ON_ONCE(!mutex_is_locked(&group->mark_mutex));
29943b245a7SAmir Goldstein WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS));
30043b245a7SAmir Goldstein }
30143b245a7SAmir Goldstein
302aa93bdc5SAmir Goldstein /* When calling fsnotify tell it if the data is a path or inode */
303aa93bdc5SAmir Goldstein enum fsnotify_data_type {
304aa93bdc5SAmir Goldstein FSNOTIFY_EVENT_NONE,
3059740d171SAmir Goldstein FSNOTIFY_EVENT_FILE_RANGE,
306aa93bdc5SAmir Goldstein FSNOTIFY_EVENT_PATH,
307aa93bdc5SAmir Goldstein FSNOTIFY_EVENT_INODE,
308fd5a3ff4SAmir Goldstein FSNOTIFY_EVENT_DENTRY,
309*b944249bSMiklos Szeredi FSNOTIFY_EVENT_MNT,
3109daa8110SGabriel Krisman Bertazi FSNOTIFY_EVENT_ERROR,
3119daa8110SGabriel Krisman Bertazi };
3129daa8110SGabriel Krisman Bertazi
3139daa8110SGabriel Krisman Bertazi struct fs_error_report {
3149daa8110SGabriel Krisman Bertazi int error;
3159daa8110SGabriel Krisman Bertazi struct inode *inode;
3169daa8110SGabriel Krisman Bertazi struct super_block *sb;
317aa93bdc5SAmir Goldstein };
318aa93bdc5SAmir Goldstein
3199740d171SAmir Goldstein struct file_range {
3209740d171SAmir Goldstein const struct path *path;
3219740d171SAmir Goldstein loff_t pos;
3229740d171SAmir Goldstein size_t count;
3239740d171SAmir Goldstein };
3249740d171SAmir Goldstein
file_range_path(const struct file_range * range)3259740d171SAmir Goldstein static inline const struct path *file_range_path(const struct file_range *range)
3269740d171SAmir Goldstein {
3279740d171SAmir Goldstein return range->path;
3289740d171SAmir Goldstein }
3299740d171SAmir Goldstein
330*b944249bSMiklos Szeredi struct fsnotify_mnt {
331*b944249bSMiklos Szeredi const struct mnt_namespace *ns;
332*b944249bSMiklos Szeredi u64 mnt_id;
333*b944249bSMiklos Szeredi };
334*b944249bSMiklos Szeredi
fsnotify_data_inode(const void * data,int data_type)335cbcf47adSAmir Goldstein static inline struct inode *fsnotify_data_inode(const void *data, int data_type)
336aa93bdc5SAmir Goldstein {
337aa93bdc5SAmir Goldstein switch (data_type) {
338aa93bdc5SAmir Goldstein case FSNOTIFY_EVENT_INODE:
339cbcf47adSAmir Goldstein return (struct inode *)data;
340fd5a3ff4SAmir Goldstein case FSNOTIFY_EVENT_DENTRY:
341fd5a3ff4SAmir Goldstein return d_inode(data);
342aa93bdc5SAmir Goldstein case FSNOTIFY_EVENT_PATH:
343aa93bdc5SAmir Goldstein return d_inode(((const struct path *)data)->dentry);
3449740d171SAmir Goldstein case FSNOTIFY_EVENT_FILE_RANGE:
3459740d171SAmir Goldstein return d_inode(file_range_path(data)->dentry);
3469daa8110SGabriel Krisman Bertazi case FSNOTIFY_EVENT_ERROR:
3479daa8110SGabriel Krisman Bertazi return ((struct fs_error_report *)data)->inode;
348aa93bdc5SAmir Goldstein default:
349aa93bdc5SAmir Goldstein return NULL;
350aa93bdc5SAmir Goldstein }
351aa93bdc5SAmir Goldstein }
352aa93bdc5SAmir Goldstein
fsnotify_data_dentry(const void * data,int data_type)353fd5a3ff4SAmir Goldstein static inline struct dentry *fsnotify_data_dentry(const void *data, int data_type)
354fd5a3ff4SAmir Goldstein {
355fd5a3ff4SAmir Goldstein switch (data_type) {
356fd5a3ff4SAmir Goldstein case FSNOTIFY_EVENT_DENTRY:
357fd5a3ff4SAmir Goldstein /* Non const is needed for dget() */
358fd5a3ff4SAmir Goldstein return (struct dentry *)data;
359fd5a3ff4SAmir Goldstein case FSNOTIFY_EVENT_PATH:
360fd5a3ff4SAmir Goldstein return ((const struct path *)data)->dentry;
3619740d171SAmir Goldstein case FSNOTIFY_EVENT_FILE_RANGE:
3629740d171SAmir Goldstein return file_range_path(data)->dentry;
363fd5a3ff4SAmir Goldstein default:
364fd5a3ff4SAmir Goldstein return NULL;
365fd5a3ff4SAmir Goldstein }
366fd5a3ff4SAmir Goldstein }
367fd5a3ff4SAmir Goldstein
fsnotify_data_path(const void * data,int data_type)368aa93bdc5SAmir Goldstein static inline const struct path *fsnotify_data_path(const void *data,
369aa93bdc5SAmir Goldstein int data_type)
370aa93bdc5SAmir Goldstein {
371aa93bdc5SAmir Goldstein switch (data_type) {
372aa93bdc5SAmir Goldstein case FSNOTIFY_EVENT_PATH:
373aa93bdc5SAmir Goldstein return data;
3749740d171SAmir Goldstein case FSNOTIFY_EVENT_FILE_RANGE:
3759740d171SAmir Goldstein return file_range_path(data);
376aa93bdc5SAmir Goldstein default:
377aa93bdc5SAmir Goldstein return NULL;
378aa93bdc5SAmir Goldstein }
379aa93bdc5SAmir Goldstein }
38090586523SEric Paris
fsnotify_data_sb(const void * data,int data_type)38129335033SGabriel Krisman Bertazi static inline struct super_block *fsnotify_data_sb(const void *data,
38229335033SGabriel Krisman Bertazi int data_type)
38329335033SGabriel Krisman Bertazi {
38429335033SGabriel Krisman Bertazi switch (data_type) {
38529335033SGabriel Krisman Bertazi case FSNOTIFY_EVENT_INODE:
38629335033SGabriel Krisman Bertazi return ((struct inode *)data)->i_sb;
38729335033SGabriel Krisman Bertazi case FSNOTIFY_EVENT_DENTRY:
38829335033SGabriel Krisman Bertazi return ((struct dentry *)data)->d_sb;
38929335033SGabriel Krisman Bertazi case FSNOTIFY_EVENT_PATH:
39029335033SGabriel Krisman Bertazi return ((const struct path *)data)->dentry->d_sb;
3919740d171SAmir Goldstein case FSNOTIFY_EVENT_FILE_RANGE:
3929740d171SAmir Goldstein return file_range_path(data)->dentry->d_sb;
3939daa8110SGabriel Krisman Bertazi case FSNOTIFY_EVENT_ERROR:
3949daa8110SGabriel Krisman Bertazi return ((struct fs_error_report *) data)->sb;
3959daa8110SGabriel Krisman Bertazi default:
3969daa8110SGabriel Krisman Bertazi return NULL;
3979daa8110SGabriel Krisman Bertazi }
3989daa8110SGabriel Krisman Bertazi }
3999daa8110SGabriel Krisman Bertazi
fsnotify_data_mnt(const void * data,int data_type)400*b944249bSMiklos Szeredi static inline const struct fsnotify_mnt *fsnotify_data_mnt(const void *data,
401*b944249bSMiklos Szeredi int data_type)
402*b944249bSMiklos Szeredi {
403*b944249bSMiklos Szeredi switch (data_type) {
404*b944249bSMiklos Szeredi case FSNOTIFY_EVENT_MNT:
405*b944249bSMiklos Szeredi return data;
406*b944249bSMiklos Szeredi default:
407*b944249bSMiklos Szeredi return NULL;
408*b944249bSMiklos Szeredi }
409*b944249bSMiklos Szeredi }
410*b944249bSMiklos Szeredi
fsnotify_data_mnt_id(const void * data,int data_type)411*b944249bSMiklos Szeredi static inline u64 fsnotify_data_mnt_id(const void *data, int data_type)
412*b944249bSMiklos Szeredi {
413*b944249bSMiklos Szeredi const struct fsnotify_mnt *mnt_data = fsnotify_data_mnt(data, data_type);
414*b944249bSMiklos Szeredi
415*b944249bSMiklos Szeredi return mnt_data ? mnt_data->mnt_id : 0;
416*b944249bSMiklos Szeredi }
417*b944249bSMiklos Szeredi
fsnotify_data_error_report(const void * data,int data_type)4189daa8110SGabriel Krisman Bertazi static inline struct fs_error_report *fsnotify_data_error_report(
4199daa8110SGabriel Krisman Bertazi const void *data,
4209daa8110SGabriel Krisman Bertazi int data_type)
4219daa8110SGabriel Krisman Bertazi {
4229daa8110SGabriel Krisman Bertazi switch (data_type) {
4239daa8110SGabriel Krisman Bertazi case FSNOTIFY_EVENT_ERROR:
4249daa8110SGabriel Krisman Bertazi return (struct fs_error_report *) data;
42529335033SGabriel Krisman Bertazi default:
42629335033SGabriel Krisman Bertazi return NULL;
42729335033SGabriel Krisman Bertazi }
42829335033SGabriel Krisman Bertazi }
42929335033SGabriel Krisman Bertazi
fsnotify_data_file_range(const void * data,int data_type)4309740d171SAmir Goldstein static inline const struct file_range *fsnotify_data_file_range(
4319740d171SAmir Goldstein const void *data,
4329740d171SAmir Goldstein int data_type)
4339740d171SAmir Goldstein {
4349740d171SAmir Goldstein switch (data_type) {
4359740d171SAmir Goldstein case FSNOTIFY_EVENT_FILE_RANGE:
4369740d171SAmir Goldstein return (struct file_range *)data;
4379740d171SAmir Goldstein default:
4389740d171SAmir Goldstein return NULL;
4399740d171SAmir Goldstein }
4409740d171SAmir Goldstein }
4419740d171SAmir Goldstein
4421c9007d6SAmir Goldstein /*
4431c9007d6SAmir Goldstein * Index to merged marks iterator array that correlates to a type of watch.
4441c9007d6SAmir Goldstein * The type of watched object can be deduced from the iterator type, but not
4451c9007d6SAmir Goldstein * the other way around, because an event can match different watched objects
4461c9007d6SAmir Goldstein * of the same object type.
4471c9007d6SAmir Goldstein * For example, both parent and child are watching an object of type inode.
4481c9007d6SAmir Goldstein */
4491c9007d6SAmir Goldstein enum fsnotify_iter_type {
4501c9007d6SAmir Goldstein FSNOTIFY_ITER_TYPE_INODE,
4511c9007d6SAmir Goldstein FSNOTIFY_ITER_TYPE_VFSMOUNT,
4521c9007d6SAmir Goldstein FSNOTIFY_ITER_TYPE_SB,
4531c9007d6SAmir Goldstein FSNOTIFY_ITER_TYPE_PARENT,
454e54183faSAmir Goldstein FSNOTIFY_ITER_TYPE_INODE2,
455*b944249bSMiklos Szeredi FSNOTIFY_ITER_TYPE_MNTNS,
4561c9007d6SAmir Goldstein FSNOTIFY_ITER_TYPE_COUNT
4571c9007d6SAmir Goldstein };
4581c9007d6SAmir Goldstein
4591c9007d6SAmir Goldstein /* The type of object that a mark is attached to */
460d6f7b98bSAmir Goldstein enum fsnotify_obj_type {
461ad69cd99SAmir Goldstein FSNOTIFY_OBJ_TYPE_ANY = -1,
462d6f7b98bSAmir Goldstein FSNOTIFY_OBJ_TYPE_INODE,
463d6f7b98bSAmir Goldstein FSNOTIFY_OBJ_TYPE_VFSMOUNT,
4641e6cb723SAmir Goldstein FSNOTIFY_OBJ_TYPE_SB,
465*b944249bSMiklos Szeredi FSNOTIFY_OBJ_TYPE_MNTNS,
466d6f7b98bSAmir Goldstein FSNOTIFY_OBJ_TYPE_COUNT,
467d6f7b98bSAmir Goldstein FSNOTIFY_OBJ_TYPE_DETACHED = FSNOTIFY_OBJ_TYPE_COUNT
468d6f7b98bSAmir Goldstein };
469d6f7b98bSAmir Goldstein
fsnotify_valid_obj_type(unsigned int obj_type)470ad69cd99SAmir Goldstein static inline bool fsnotify_valid_obj_type(unsigned int obj_type)
471b812a9f5SAmir Goldstein {
472ad69cd99SAmir Goldstein return (obj_type < FSNOTIFY_OBJ_TYPE_COUNT);
473b812a9f5SAmir Goldstein }
474b812a9f5SAmir Goldstein
4755b0457adSAmir Goldstein struct fsnotify_iter_info {
4761c9007d6SAmir Goldstein struct fsnotify_mark *marks[FSNOTIFY_ITER_TYPE_COUNT];
47714362a25SAmir Goldstein struct fsnotify_group *current_group;
4785b0457adSAmir Goldstein unsigned int report_mask;
4795b0457adSAmir Goldstein int srcu_idx;
4805b0457adSAmir Goldstein };
4815b0457adSAmir Goldstein
fsnotify_iter_should_report_type(struct fsnotify_iter_info * iter_info,int iter_type)48247d9c7ccSAmir Goldstein static inline bool fsnotify_iter_should_report_type(
4831c9007d6SAmir Goldstein struct fsnotify_iter_info *iter_info, int iter_type)
48447d9c7ccSAmir Goldstein {
4851c9007d6SAmir Goldstein return (iter_info->report_mask & (1U << iter_type));
48647d9c7ccSAmir Goldstein }
48747d9c7ccSAmir Goldstein
fsnotify_iter_set_report_type(struct fsnotify_iter_info * iter_info,int iter_type)48847d9c7ccSAmir Goldstein static inline void fsnotify_iter_set_report_type(
4891c9007d6SAmir Goldstein struct fsnotify_iter_info *iter_info, int iter_type)
49047d9c7ccSAmir Goldstein {
4911c9007d6SAmir Goldstein iter_info->report_mask |= (1U << iter_type);
49247d9c7ccSAmir Goldstein }
49347d9c7ccSAmir Goldstein
fsnotify_iter_mark(struct fsnotify_iter_info * iter_info,int iter_type)49414362a25SAmir Goldstein static inline struct fsnotify_mark *fsnotify_iter_mark(
49514362a25SAmir Goldstein struct fsnotify_iter_info *iter_info, int iter_type)
49647d9c7ccSAmir Goldstein {
49714362a25SAmir Goldstein if (fsnotify_iter_should_report_type(iter_info, iter_type))
49814362a25SAmir Goldstein return iter_info->marks[iter_type];
49914362a25SAmir Goldstein return NULL;
50014362a25SAmir Goldstein }
50114362a25SAmir Goldstein
fsnotify_iter_step(struct fsnotify_iter_info * iter,int type,struct fsnotify_mark ** markp)50214362a25SAmir Goldstein static inline int fsnotify_iter_step(struct fsnotify_iter_info *iter, int type,
50314362a25SAmir Goldstein struct fsnotify_mark **markp)
50414362a25SAmir Goldstein {
50514362a25SAmir Goldstein while (type < FSNOTIFY_ITER_TYPE_COUNT) {
50614362a25SAmir Goldstein *markp = fsnotify_iter_mark(iter, type);
50714362a25SAmir Goldstein if (*markp)
50814362a25SAmir Goldstein break;
50914362a25SAmir Goldstein type++;
51014362a25SAmir Goldstein }
51114362a25SAmir Goldstein return type;
51247d9c7ccSAmir Goldstein }
51347d9c7ccSAmir Goldstein
5145b0457adSAmir Goldstein #define FSNOTIFY_ITER_FUNCS(name, NAME) \
5155b0457adSAmir Goldstein static inline struct fsnotify_mark *fsnotify_iter_##name##_mark( \
5165b0457adSAmir Goldstein struct fsnotify_iter_info *iter_info) \
5175b0457adSAmir Goldstein { \
51814362a25SAmir Goldstein return fsnotify_iter_mark(iter_info, FSNOTIFY_ITER_TYPE_##NAME); \
5195b0457adSAmir Goldstein }
5205b0457adSAmir Goldstein
5215b0457adSAmir Goldstein FSNOTIFY_ITER_FUNCS(inode, INODE)
522fecc4559SAmir Goldstein FSNOTIFY_ITER_FUNCS(parent, PARENT)
5235b0457adSAmir Goldstein FSNOTIFY_ITER_FUNCS(vfsmount, VFSMOUNT)
5241e6cb723SAmir Goldstein FSNOTIFY_ITER_FUNCS(sb, SB)
5255b0457adSAmir Goldstein
5261c9007d6SAmir Goldstein #define fsnotify_foreach_iter_type(type) \
5271c9007d6SAmir Goldstein for (type = 0; type < FSNOTIFY_ITER_TYPE_COUNT; type++)
52814362a25SAmir Goldstein #define fsnotify_foreach_iter_mark_type(iter, mark, type) \
52914362a25SAmir Goldstein for (type = 0; \
53014362a25SAmir Goldstein type = fsnotify_iter_step(iter, type, &mark), \
53114362a25SAmir Goldstein type < FSNOTIFY_ITER_TYPE_COUNT; \
53214362a25SAmir Goldstein type++)
53347d9c7ccSAmir Goldstein
5343be25f49SEric Paris /*
5351e6cb723SAmir Goldstein * Inode/vfsmount/sb point to this structure which tracks all marks attached to
5361e6cb723SAmir Goldstein * the inode/vfsmount/sb. The reference to inode/vfsmount/sb is held by this
53708991e83SJan Kara * structure. We destroy this structure when there are no more marks attached
53808991e83SJan Kara * to it. The structure is protected by fsnotify_mark_srcu.
5399dd813c1SJan Kara */
5409dd813c1SJan Kara struct fsnotify_mark_connector {
54104662cabSJan Kara spinlock_t lock;
542a5e57b4dSAmir Goldstein unsigned char type; /* Type of object [lock] */
543a5e57b4dSAmir Goldstein unsigned char prio; /* Highest priority group */
544c9d4603bSAmir Goldstein #define FSNOTIFY_CONN_FLAG_IS_WATCHED 0x01
545c3638b5bSAmir Goldstein #define FSNOTIFY_CONN_FLAG_HAS_IREF 0x02
546c285a2f0SAmir Goldstein unsigned short flags; /* flags [lock] */
54736f10f55SAmir Goldstein union {
54836f10f55SAmir Goldstein /* Object pointer [lock] */
549687c217cSAmir Goldstein void *obj;
55008991e83SJan Kara /* Used listing heads to free after srcu period expires */
55108991e83SJan Kara struct fsnotify_mark_connector *destroy_next;
55208991e83SJan Kara };
553d90a10e2SRobert Kolchmeyer struct hlist_head list;
5549dd813c1SJan Kara };
5559dd813c1SJan Kara
55607a3b8d0SAmir Goldstein /*
55707a3b8d0SAmir Goldstein * Container for per-sb fsnotify state (sb marks and more).
55807a3b8d0SAmir Goldstein * Attached lazily on first marked object on the sb and freed when killing sb.
55907a3b8d0SAmir Goldstein */
56007a3b8d0SAmir Goldstein struct fsnotify_sb_info {
56107a3b8d0SAmir Goldstein struct fsnotify_mark_connector __rcu *sb_marks;
562cb5d4f48SAmir Goldstein /*
563cb5d4f48SAmir Goldstein * Number of inode/mount/sb objects that are being watched in this sb.
564cb5d4f48SAmir Goldstein * Note that inodes objects are currently double-accounted.
565a5e57b4dSAmir Goldstein *
566a5e57b4dSAmir Goldstein * The value in watched_objects[prio] is the number of objects that are
567a5e57b4dSAmir Goldstein * watched by groups of priority >= prio, so watched_objects[0] is the
568a5e57b4dSAmir Goldstein * total number of watched objects in this sb.
569cb5d4f48SAmir Goldstein */
570a5e57b4dSAmir Goldstein atomic_long_t watched_objects[__FSNOTIFY_PRIO_NUM];
57107a3b8d0SAmir Goldstein };
57207a3b8d0SAmir Goldstein
fsnotify_sb_info(struct super_block * sb)57307a3b8d0SAmir Goldstein static inline struct fsnotify_sb_info *fsnotify_sb_info(struct super_block *sb)
57407a3b8d0SAmir Goldstein {
57507a3b8d0SAmir Goldstein #ifdef CONFIG_FSNOTIFY
57607a3b8d0SAmir Goldstein return READ_ONCE(sb->s_fsnotify_info);
57707a3b8d0SAmir Goldstein #else
57807a3b8d0SAmir Goldstein return NULL;
57907a3b8d0SAmir Goldstein #endif
58007a3b8d0SAmir Goldstein }
58107a3b8d0SAmir Goldstein
fsnotify_sb_watched_objects(struct super_block * sb)582d2f277e2SAmir Goldstein static inline atomic_long_t *fsnotify_sb_watched_objects(struct super_block *sb)
583d2f277e2SAmir Goldstein {
584a5e57b4dSAmir Goldstein return &fsnotify_sb_info(sb)->watched_objects[0];
585d2f277e2SAmir Goldstein }
586d2f277e2SAmir Goldstein
5879dd813c1SJan Kara /*
5881e39fc01SJan Kara * A mark is simply an object attached to an in core inode which allows an
5893be25f49SEric Paris * fsnotify listener to indicate they are either no longer interested in events
5903be25f49SEric Paris * of a type matching mask or only interested in those events.
5913be25f49SEric Paris *
5921e39fc01SJan Kara * These are flushed when an inode is evicted from core and may be flushed
5931e39fc01SJan Kara * when the inode is modified (as seen by fsnotify_access). Some fsnotify
5941e39fc01SJan Kara * users (such as dnotify) will flush these when the open fd is closed and not
5951e39fc01SJan Kara * at inode eviction or modification.
5961e39fc01SJan Kara *
5971e39fc01SJan Kara * Text in brackets is showing the lock(s) protecting modifications of a
5981e39fc01SJan Kara * particular entry. obj_lock means either inode->i_lock or
5991e39fc01SJan Kara * mnt->mnt_root->d_lock depending on the mark type.
6003be25f49SEric Paris */
601e61ce867SEric Paris struct fsnotify_mark {
6021e39fc01SJan Kara /* Mask this mark is for [mark->lock, group->mark_mutex] */
6031e39fc01SJan Kara __u32 mask;
6041e39fc01SJan Kara /* We hold one for presence in g_list. Also one ref for each 'thing'
6053be25f49SEric Paris * in kernel that found and may be using this mark. */
606ab97f873SElena Reshetova refcount_t refcnt;
6071e39fc01SJan Kara /* Group this mark is for. Set on mark creation, stable until last ref
6081e39fc01SJan Kara * is dropped */
6091e39fc01SJan Kara struct fsnotify_group *group;
6108e984f86SAmir Goldstein /* List of marks by group->marks_list. Also reused for queueing
6111e39fc01SJan Kara * mark into destroy_list when it's waiting for the end of SRCU period
6121e39fc01SJan Kara * before it can be freed. [group->mark_mutex] */
6131e39fc01SJan Kara struct list_head g_list;
6141e39fc01SJan Kara /* Protects inode / mnt pointers, flags, masks */
6151e39fc01SJan Kara spinlock_t lock;
6166b3f05d2SJan Kara /* List of marks for inode / vfsmount [connector->lock, mark ref] */
6171e39fc01SJan Kara struct hlist_node obj_list;
6186b3f05d2SJan Kara /* Head of list of marks for an object [mark ref] */
61986ffe245SJan Kara struct fsnotify_mark_connector *connector;
62031a371e4SAmir Goldstein /* Events types and flags to ignore [mark->lock, group->mark_mutex] */
62131a371e4SAmir Goldstein __u32 ignore_mask;
62238035c04SAmir Goldstein /* General fsnotify mark flags */
62338035c04SAmir Goldstein #define FSNOTIFY_MARK_FLAG_ALIVE 0x0001
62438035c04SAmir Goldstein #define FSNOTIFY_MARK_FLAG_ATTACHED 0x0002
62538035c04SAmir Goldstein /* inotify mark flags */
62638035c04SAmir Goldstein #define FSNOTIFY_MARK_FLAG_EXCL_UNLINK 0x0010
62738035c04SAmir Goldstein #define FSNOTIFY_MARK_FLAG_IN_ONESHOT 0x0020
62838035c04SAmir Goldstein /* fanotify mark flags */
62938035c04SAmir Goldstein #define FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY 0x0100
630c3638b5bSAmir Goldstein #define FSNOTIFY_MARK_FLAG_NO_IREF 0x0200
63131a371e4SAmir Goldstein #define FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS 0x0400
6327232522eSAmir Goldstein #define FSNOTIFY_MARK_FLAG_HAS_FSID 0x0800
63330ad1938SAmir Goldstein #define FSNOTIFY_MARK_FLAG_WEAK_FSID 0x1000
6341e39fc01SJan Kara unsigned int flags; /* flags [mark->lock] */
6353be25f49SEric Paris };
6363be25f49SEric Paris
63790586523SEric Paris #ifdef CONFIG_FSNOTIFY
63890586523SEric Paris
63990586523SEric Paris /* called from the vfs helpers */
64090586523SEric Paris
64190586523SEric Paris /* main fsnotify call to send events */
64240a100d3SAmir Goldstein extern int fsnotify(__u32 mask, const void *data, int data_type,
64340a100d3SAmir Goldstein struct inode *dir, const struct qstr *name,
64440a100d3SAmir Goldstein struct inode *inode, u32 cookie);
64571d73410SMel Gorman extern int __fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data,
646017de65fSAmir Goldstein int data_type);
6473be25f49SEric Paris extern void __fsnotify_inode_delete(struct inode *inode);
648ca9c726eSAndreas Gruenbacher extern void __fsnotify_vfsmount_delete(struct vfsmount *mnt);
6491e6cb723SAmir Goldstein extern void fsnotify_sb_delete(struct super_block *sb);
650*b944249bSMiklos Szeredi extern void __fsnotify_mntns_delete(struct mnt_namespace *mntns);
651795bb82dSAmir Goldstein extern void fsnotify_sb_free(struct super_block *sb);
65247882c6fSEric Paris extern u32 fsnotify_get_cookie(void);
653*b944249bSMiklos Szeredi extern void fsnotify_mnt(__u32 mask, struct mnt_namespace *ns, struct vfsmount *mnt);
65490586523SEric Paris
fsnotify_parent_needed_mask(__u32 mask)6559b93f331SAmir Goldstein static inline __u32 fsnotify_parent_needed_mask(__u32 mask)
6569b93f331SAmir Goldstein {
6579b93f331SAmir Goldstein /* FS_EVENT_ON_CHILD is set on marks that want parent/name info */
6589b93f331SAmir Goldstein if (!(mask & FS_EVENT_ON_CHILD))
6599b93f331SAmir Goldstein return 0;
6609b93f331SAmir Goldstein /*
6619b93f331SAmir Goldstein * This object might be watched by a mark that cares about parent/name
6629b93f331SAmir Goldstein * info, does it care about the specific set of events that can be
6639b93f331SAmir Goldstein * reported with parent/name info?
6649b93f331SAmir Goldstein */
6659b93f331SAmir Goldstein return mask & FS_EVENTS_POSS_TO_PARENT;
6669b93f331SAmir Goldstein }
6679b93f331SAmir Goldstein
fsnotify_inode_watches_children(struct inode * inode)668c28f7e56SEric Paris static inline int fsnotify_inode_watches_children(struct inode *inode)
669c28f7e56SEric Paris {
670172e422fSAmir Goldstein __u32 parent_mask = READ_ONCE(inode->i_fsnotify_mask);
671172e422fSAmir Goldstein
672c28f7e56SEric Paris /* FS_EVENT_ON_CHILD is set if the inode may care */
673172e422fSAmir Goldstein if (!(parent_mask & FS_EVENT_ON_CHILD))
674c28f7e56SEric Paris return 0;
675c28f7e56SEric Paris /* this inode might care about child events, does it care about the
676c28f7e56SEric Paris * specific set of events that can happen on a child? */
677172e422fSAmir Goldstein return parent_mask & FS_EVENTS_POSS_ON_CHILD;
678c28f7e56SEric Paris }
679c28f7e56SEric Paris
680c28f7e56SEric Paris /*
681c28f7e56SEric Paris * Update the dentry with a flag indicating the interest of its parent to receive
682c28f7e56SEric Paris * filesystem events when those events happens to this dentry->d_inode.
683c28f7e56SEric Paris */
fsnotify_update_flags(struct dentry * dentry)684affda484SAl Viro static inline void fsnotify_update_flags(struct dentry *dentry)
685c28f7e56SEric Paris {
686c28f7e56SEric Paris assert_spin_locked(&dentry->d_lock);
687c28f7e56SEric Paris
688b5c84bf6SNick Piggin /*
689b5c84bf6SNick Piggin * Serialisation of setting PARENT_WATCHED on the dentries is provided
690b5c84bf6SNick Piggin * by d_lock. If inotify_inode_watched changes after we have taken
691172e422fSAmir Goldstein * d_lock, the following fsnotify_set_children_dentry_flags call will
692b5c84bf6SNick Piggin * find our entry, so it will spin until we complete here, and update
693b5c84bf6SNick Piggin * us with the new state.
694b5c84bf6SNick Piggin */
695affda484SAl Viro if (fsnotify_inode_watches_children(dentry->d_parent->d_inode))
696c28f7e56SEric Paris dentry->d_flags |= DCACHE_FSNOTIFY_PARENT_WATCHED;
697c28f7e56SEric Paris else
698c28f7e56SEric Paris dentry->d_flags &= ~DCACHE_FSNOTIFY_PARENT_WATCHED;
699c28f7e56SEric Paris }
700c28f7e56SEric Paris
70190586523SEric Paris /* called from fsnotify listeners, such as fanotify or dnotify */
70290586523SEric Paris
70398612952SLino Sanfilippo /* create a new group */
704867a448dSAmir Goldstein extern struct fsnotify_group *fsnotify_alloc_group(
705867a448dSAmir Goldstein const struct fsnotify_ops *ops,
706867a448dSAmir Goldstein int flags);
70798612952SLino Sanfilippo /* get reference to a group */
70898612952SLino Sanfilippo extern void fsnotify_get_group(struct fsnotify_group *group);
709ffab8340SEric Paris /* drop reference on a group from fsnotify_alloc_group */
71090586523SEric Paris extern void fsnotify_put_group(struct fsnotify_group *group);
71112703dbfSJan Kara /* group destruction begins, stop queuing new events */
71212703dbfSJan Kara extern void fsnotify_group_stop_queueing(struct fsnotify_group *group);
713d8153d4dSLino Sanfilippo /* destroy group */
714d8153d4dSLino Sanfilippo extern void fsnotify_destroy_group(struct fsnotify_group *group);
7150a6b6bd5SEric Paris /* fasync handler function */
7160a6b6bd5SEric Paris extern int fsnotify_fasync(int fd, struct file *file, int on);
7177053aee2SJan Kara /* Free event from memory */
7187053aee2SJan Kara extern void fsnotify_destroy_event(struct fsnotify_group *group,
71990586523SEric Paris struct fsnotify_event *event);
720a2d8bc6cSEric Paris /* attach the event to the group notification queue */
7211ad03c3aSGabriel Krisman Bertazi extern int fsnotify_insert_event(struct fsnotify_group *group,
72274766bbfSEric Paris struct fsnotify_event *event,
72394e00d28SAmir Goldstein int (*merge)(struct fsnotify_group *,
72494e00d28SAmir Goldstein struct fsnotify_event *),
72594e00d28SAmir Goldstein void (*insert)(struct fsnotify_group *,
726f70ab54cSEric Paris struct fsnotify_event *));
7271ad03c3aSGabriel Krisman Bertazi
fsnotify_add_event(struct fsnotify_group * group,struct fsnotify_event * event,int (* merge)(struct fsnotify_group *,struct fsnotify_event *))7281ad03c3aSGabriel Krisman Bertazi static inline int fsnotify_add_event(struct fsnotify_group *group,
7291ad03c3aSGabriel Krisman Bertazi struct fsnotify_event *event,
7301ad03c3aSGabriel Krisman Bertazi int (*merge)(struct fsnotify_group *,
7311ad03c3aSGabriel Krisman Bertazi struct fsnotify_event *))
7321ad03c3aSGabriel Krisman Bertazi {
7331ad03c3aSGabriel Krisman Bertazi return fsnotify_insert_event(group, event, merge, NULL);
7341ad03c3aSGabriel Krisman Bertazi }
7351ad03c3aSGabriel Krisman Bertazi
7367b1f6417SJan Kara /* Queue overflow event to a notification group */
fsnotify_queue_overflow(struct fsnotify_group * group)7377b1f6417SJan Kara static inline void fsnotify_queue_overflow(struct fsnotify_group *group)
7387b1f6417SJan Kara {
7391ad03c3aSGabriel Krisman Bertazi fsnotify_add_event(group, group->overflow_event, NULL);
7407b1f6417SJan Kara }
7417b1f6417SJan Kara
fsnotify_is_overflow_event(u32 mask)742808967a0SGabriel Krisman Bertazi static inline bool fsnotify_is_overflow_event(u32 mask)
743808967a0SGabriel Krisman Bertazi {
744808967a0SGabriel Krisman Bertazi return mask & FS_Q_OVERFLOW;
745808967a0SGabriel Krisman Bertazi }
746808967a0SGabriel Krisman Bertazi
fsnotify_notify_queue_is_empty(struct fsnotify_group * group)7476f73171eSAmir Goldstein static inline bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group)
7486f73171eSAmir Goldstein {
7496f73171eSAmir Goldstein assert_spin_locked(&group->notification_lock);
7506f73171eSAmir Goldstein
7516f73171eSAmir Goldstein return list_empty(&group->notification_list);
7526f73171eSAmir Goldstein }
7536f73171eSAmir Goldstein
754a2d8bc6cSEric Paris extern bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group);
755a2d8bc6cSEric Paris /* return, but do not dequeue the first event on the notification queue */
7568ba8fa91SJan Kara extern struct fsnotify_event *fsnotify_peek_first_event(struct fsnotify_group *group);
757e4aff117SEric Paris /* return AND dequeue the first event on the notification queue */
7588ba8fa91SJan Kara extern struct fsnotify_event *fsnotify_remove_first_event(struct fsnotify_group *group);
759f7db89acSJan Kara /* Remove event queued in the notification list */
760f7db89acSJan Kara extern void fsnotify_remove_queued_event(struct fsnotify_group *group,
761f7db89acSJan Kara struct fsnotify_event *event);
762a2d8bc6cSEric Paris
7633be25f49SEric Paris /* functions used to manipulate the marks attached to inodes */
7643be25f49SEric Paris
76531a371e4SAmir Goldstein /*
76631a371e4SAmir Goldstein * Canonical "ignore mask" including event flags.
76731a371e4SAmir Goldstein *
76831a371e4SAmir Goldstein * Note the subtle semantic difference from the legacy ->ignored_mask.
76931a371e4SAmir Goldstein * ->ignored_mask traditionally only meant which events should be ignored,
77031a371e4SAmir Goldstein * while ->ignore_mask also includes flags regarding the type of objects on
77131a371e4SAmir Goldstein * which events should be ignored.
77231a371e4SAmir Goldstein */
fsnotify_ignore_mask(struct fsnotify_mark * mark)77331a371e4SAmir Goldstein static inline __u32 fsnotify_ignore_mask(struct fsnotify_mark *mark)
77431a371e4SAmir Goldstein {
77531a371e4SAmir Goldstein __u32 ignore_mask = mark->ignore_mask;
77631a371e4SAmir Goldstein
77731a371e4SAmir Goldstein /* The event flags in ignore mask take effect */
77831a371e4SAmir Goldstein if (mark->flags & FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS)
77931a371e4SAmir Goldstein return ignore_mask;
78031a371e4SAmir Goldstein
78131a371e4SAmir Goldstein /*
78231a371e4SAmir Goldstein * Legacy behavior:
78331a371e4SAmir Goldstein * - Always ignore events on dir
78431a371e4SAmir Goldstein * - Ignore events on child if parent is watching children
78531a371e4SAmir Goldstein */
78631a371e4SAmir Goldstein ignore_mask |= FS_ISDIR;
78731a371e4SAmir Goldstein ignore_mask &= ~FS_EVENT_ON_CHILD;
78831a371e4SAmir Goldstein ignore_mask |= mark->mask & FS_EVENT_ON_CHILD;
78931a371e4SAmir Goldstein
79031a371e4SAmir Goldstein return ignore_mask;
79131a371e4SAmir Goldstein }
79231a371e4SAmir Goldstein
79331a371e4SAmir Goldstein /* Legacy ignored_mask - only event types to ignore */
fsnotify_ignored_events(struct fsnotify_mark * mark)79431a371e4SAmir Goldstein static inline __u32 fsnotify_ignored_events(struct fsnotify_mark *mark)
79531a371e4SAmir Goldstein {
79631a371e4SAmir Goldstein return mark->ignore_mask & ALL_FSNOTIFY_EVENTS;
79731a371e4SAmir Goldstein }
79831a371e4SAmir Goldstein
79931a371e4SAmir Goldstein /*
80031a371e4SAmir Goldstein * Check if mask (or ignore mask) should be applied depending if victim is a
80131a371e4SAmir Goldstein * directory and whether it is reported to a watching parent.
80231a371e4SAmir Goldstein */
fsnotify_mask_applicable(__u32 mask,bool is_dir,int iter_type)80331a371e4SAmir Goldstein static inline bool fsnotify_mask_applicable(__u32 mask, bool is_dir,
80431a371e4SAmir Goldstein int iter_type)
80531a371e4SAmir Goldstein {
80631a371e4SAmir Goldstein /* Should mask be applied to a directory? */
80731a371e4SAmir Goldstein if (is_dir && !(mask & FS_ISDIR))
80831a371e4SAmir Goldstein return false;
80931a371e4SAmir Goldstein
81031a371e4SAmir Goldstein /* Should mask be applied to a child? */
81131a371e4SAmir Goldstein if (iter_type == FSNOTIFY_ITER_TYPE_PARENT &&
81231a371e4SAmir Goldstein !(mask & FS_EVENT_ON_CHILD))
81331a371e4SAmir Goldstein return false;
81431a371e4SAmir Goldstein
81531a371e4SAmir Goldstein return true;
81631a371e4SAmir Goldstein }
81731a371e4SAmir Goldstein
81831a371e4SAmir Goldstein /*
81931a371e4SAmir Goldstein * Effective ignore mask taking into account if event victim is a
82031a371e4SAmir Goldstein * directory and whether it is reported to a watching parent.
82131a371e4SAmir Goldstein */
fsnotify_effective_ignore_mask(struct fsnotify_mark * mark,bool is_dir,int iter_type)82231a371e4SAmir Goldstein static inline __u32 fsnotify_effective_ignore_mask(struct fsnotify_mark *mark,
82331a371e4SAmir Goldstein bool is_dir, int iter_type)
82431a371e4SAmir Goldstein {
82531a371e4SAmir Goldstein __u32 ignore_mask = fsnotify_ignored_events(mark);
82631a371e4SAmir Goldstein
82731a371e4SAmir Goldstein if (!ignore_mask)
82831a371e4SAmir Goldstein return 0;
82931a371e4SAmir Goldstein
83031a371e4SAmir Goldstein /* For non-dir and non-child, no need to consult the event flags */
83131a371e4SAmir Goldstein if (!is_dir && iter_type != FSNOTIFY_ITER_TYPE_PARENT)
83231a371e4SAmir Goldstein return ignore_mask;
83331a371e4SAmir Goldstein
83431a371e4SAmir Goldstein ignore_mask = fsnotify_ignore_mask(mark);
83531a371e4SAmir Goldstein if (!fsnotify_mask_applicable(ignore_mask, is_dir, iter_type))
83631a371e4SAmir Goldstein return 0;
83731a371e4SAmir Goldstein
83831a371e4SAmir Goldstein return ignore_mask & ALL_FSNOTIFY_EVENTS;
83931a371e4SAmir Goldstein }
84031a371e4SAmir Goldstein
84131a371e4SAmir Goldstein /* Get mask for calculating object interest taking ignore mask into account */
fsnotify_calc_mask(struct fsnotify_mark * mark)8424f0b903dSAmir Goldstein static inline __u32 fsnotify_calc_mask(struct fsnotify_mark *mark)
8434f0b903dSAmir Goldstein {
8444f0b903dSAmir Goldstein __u32 mask = mark->mask;
8454f0b903dSAmir Goldstein
84631a371e4SAmir Goldstein if (!fsnotify_ignored_events(mark))
8474f0b903dSAmir Goldstein return mask;
8484f0b903dSAmir Goldstein
84931a371e4SAmir Goldstein /* Interest in FS_MODIFY may be needed for clearing ignore mask */
85004e317baSAmir Goldstein if (!(mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY))
85104e317baSAmir Goldstein mask |= FS_MODIFY;
85204e317baSAmir Goldstein
8534f0b903dSAmir Goldstein /*
8544f0b903dSAmir Goldstein * If mark is interested in ignoring events on children, the object must
8554f0b903dSAmir Goldstein * show interest in those events for fsnotify_parent() to notice it.
8564f0b903dSAmir Goldstein */
85731a371e4SAmir Goldstein return mask | mark->ignore_mask;
8584f0b903dSAmir Goldstein }
8594f0b903dSAmir Goldstein
8603ac70bfcSAmir Goldstein /* Get mask of events for a list of marks */
8613ac70bfcSAmir Goldstein extern __u32 fsnotify_conn_mask(struct fsnotify_mark_connector *conn);
862a242677bSJan Kara /* Calculate mask of events for a list of marks */
863a242677bSJan Kara extern void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn);
8647b129323SJan Kara extern void fsnotify_init_mark(struct fsnotify_mark *mark,
865054c636eSJan Kara struct fsnotify_group *group);
866b1362edfSJan Kara /* Find mark belonging to given group in the list of marks */
867687c217cSAmir Goldstein struct fsnotify_mark *fsnotify_find_mark(void *obj, unsigned int obj_type,
868b1362edfSJan Kara struct fsnotify_group *group);
869b812a9f5SAmir Goldstein /* attach the mark to the object */
870687c217cSAmir Goldstein int fsnotify_add_mark(struct fsnotify_mark *mark, void *obj,
871687c217cSAmir Goldstein unsigned int obj_type, int add_flags);
872687c217cSAmir Goldstein int fsnotify_add_mark_locked(struct fsnotify_mark *mark, void *obj,
8737232522eSAmir Goldstein unsigned int obj_type, int add_flags);
87477115225SAmir Goldstein
875b249f5beSAmir Goldstein /* attach the mark to the inode */
fsnotify_add_inode_mark(struct fsnotify_mark * mark,struct inode * inode,int add_flags)876b249f5beSAmir Goldstein static inline int fsnotify_add_inode_mark(struct fsnotify_mark *mark,
877b249f5beSAmir Goldstein struct inode *inode,
878f3010343SAmir Goldstein int add_flags)
879b249f5beSAmir Goldstein {
880687c217cSAmir Goldstein return fsnotify_add_mark(mark, inode, FSNOTIFY_OBJ_TYPE_INODE,
881687c217cSAmir Goldstein add_flags);
882b249f5beSAmir Goldstein }
fsnotify_add_inode_mark_locked(struct fsnotify_mark * mark,struct inode * inode,int add_flags)883b249f5beSAmir Goldstein static inline int fsnotify_add_inode_mark_locked(struct fsnotify_mark *mark,
884b249f5beSAmir Goldstein struct inode *inode,
885f3010343SAmir Goldstein int add_flags)
886b249f5beSAmir Goldstein {
887687c217cSAmir Goldstein return fsnotify_add_mark_locked(mark, inode, FSNOTIFY_OBJ_TYPE_INODE,
888687c217cSAmir Goldstein add_flags);
889b249f5beSAmir Goldstein }
89077115225SAmir Goldstein
fsnotify_find_inode_mark(struct inode * inode,struct fsnotify_group * group)891230d97d3SAmir Goldstein static inline struct fsnotify_mark *fsnotify_find_inode_mark(
892230d97d3SAmir Goldstein struct inode *inode,
893230d97d3SAmir Goldstein struct fsnotify_group *group)
894230d97d3SAmir Goldstein {
895687c217cSAmir Goldstein return fsnotify_find_mark(inode, FSNOTIFY_OBJ_TYPE_INODE, group);
896230d97d3SAmir Goldstein }
897230d97d3SAmir Goldstein
898e2a29943SLino Sanfilippo /* given a group and a mark, flag mark to be freed when all references are dropped */
899e2a29943SLino Sanfilippo extern void fsnotify_destroy_mark(struct fsnotify_mark *mark,
900e2a29943SLino Sanfilippo struct fsnotify_group *group);
9014712e722SJan Kara /* detach mark from inode / mount list, group list, drop inode reference */
9024712e722SJan Kara extern void fsnotify_detach_mark(struct fsnotify_mark *mark);
9034712e722SJan Kara /* free mark */
9044712e722SJan Kara extern void fsnotify_free_mark(struct fsnotify_mark *mark);
905b72679eeSTrond Myklebust /* Wait until all marks queued for destruction are destroyed */
906b72679eeSTrond Myklebust extern void fsnotify_wait_marks_destroyed(void);
907ad69cd99SAmir Goldstein /* Clear all of the marks of a group attached to a given object type */
908ad69cd99SAmir Goldstein extern void fsnotify_clear_marks_by_group(struct fsnotify_group *group,
909ad69cd99SAmir Goldstein unsigned int obj_type);
910841bdc10SEric Paris extern void fsnotify_get_mark(struct fsnotify_mark *mark);
911841bdc10SEric Paris extern void fsnotify_put_mark(struct fsnotify_mark *mark);
912abc77577SJan Kara extern void fsnotify_finish_user_wait(struct fsnotify_iter_info *iter_info);
913abc77577SJan Kara extern bool fsnotify_prepare_user_wait(struct fsnotify_iter_info *iter_info);
9143be25f49SEric Paris
fsnotify_init_event(struct fsnotify_event * event)9158988f11aSAmir Goldstein static inline void fsnotify_init_event(struct fsnotify_event *event)
916a0a92d26SAmir Goldstein {
917a0a92d26SAmir Goldstein INIT_LIST_HEAD(&event->list);
918a0a92d26SAmir Goldstein }
9199740d171SAmir Goldstein int fsnotify_pre_content(const struct path *path, const loff_t *ppos,
9209740d171SAmir Goldstein size_t count);
921b4e4e140SEric Paris
92290586523SEric Paris #else
92390586523SEric Paris
fsnotify_pre_content(const struct path * path,const loff_t * ppos,size_t count)9249740d171SAmir Goldstein static inline int fsnotify_pre_content(const struct path *path,
9259740d171SAmir Goldstein const loff_t *ppos, size_t count)
9269740d171SAmir Goldstein {
9279740d171SAmir Goldstein return 0;
9289740d171SAmir Goldstein }
9299740d171SAmir Goldstein
fsnotify(__u32 mask,const void * data,int data_type,struct inode * dir,const struct qstr * name,struct inode * inode,u32 cookie)93040a100d3SAmir Goldstein static inline int fsnotify(__u32 mask, const void *data, int data_type,
93140a100d3SAmir Goldstein struct inode *dir, const struct qstr *name,
93240a100d3SAmir Goldstein struct inode *inode, u32 cookie)
933c4ec54b4SEric Paris {
934c4ec54b4SEric Paris return 0;
935c4ec54b4SEric Paris }
9363be25f49SEric Paris
__fsnotify_parent(struct dentry * dentry,__u32 mask,const void * data,int data_type)93771d73410SMel Gorman static inline int __fsnotify_parent(struct dentry *dentry, __u32 mask,
938017de65fSAmir Goldstein const void *data, int data_type)
93952420392SEric Paris {
94052420392SEric Paris return 0;
94152420392SEric Paris }
942c28f7e56SEric Paris
__fsnotify_inode_delete(struct inode * inode)9433be25f49SEric Paris static inline void __fsnotify_inode_delete(struct inode *inode)
9443be25f49SEric Paris {}
9453be25f49SEric Paris
__fsnotify_vfsmount_delete(struct vfsmount * mnt)946ca9c726eSAndreas Gruenbacher static inline void __fsnotify_vfsmount_delete(struct vfsmount *mnt)
947ca9c726eSAndreas Gruenbacher {}
948ca9c726eSAndreas Gruenbacher
fsnotify_sb_delete(struct super_block * sb)9491e6cb723SAmir Goldstein static inline void fsnotify_sb_delete(struct super_block *sb)
9501e6cb723SAmir Goldstein {}
9511e6cb723SAmir Goldstein
__fsnotify_mntns_delete(struct mnt_namespace * mntns)952*b944249bSMiklos Szeredi static inline void __fsnotify_mntns_delete(struct mnt_namespace *mntns)
953*b944249bSMiklos Szeredi {}
954*b944249bSMiklos Szeredi
fsnotify_sb_free(struct super_block * sb)955795bb82dSAmir Goldstein static inline void fsnotify_sb_free(struct super_block *sb)
956795bb82dSAmir Goldstein {}
957795bb82dSAmir Goldstein
fsnotify_update_flags(struct dentry * dentry)958affda484SAl Viro static inline void fsnotify_update_flags(struct dentry *dentry)
959c28f7e56SEric Paris {}
960c28f7e56SEric Paris
fsnotify_get_cookie(void)96147882c6fSEric Paris static inline u32 fsnotify_get_cookie(void)
96247882c6fSEric Paris {
96347882c6fSEric Paris return 0;
96447882c6fSEric Paris }
96547882c6fSEric Paris
fsnotify_unmount_inodes(struct super_block * sb)96674278da9SDave Chinner static inline void fsnotify_unmount_inodes(struct super_block *sb)
967164bc619SEric Paris {}
968164bc619SEric Paris
fsnotify_mnt(__u32 mask,struct mnt_namespace * ns,struct vfsmount * mnt)969*b944249bSMiklos Szeredi static inline void fsnotify_mnt(__u32 mask, struct mnt_namespace *ns, struct vfsmount *mnt)
970*b944249bSMiklos Szeredi {}
971*b944249bSMiklos Szeredi
97290586523SEric Paris #endif /* CONFIG_FSNOTIFY */
97390586523SEric Paris
97490586523SEric Paris #endif /* __KERNEL __ */
97590586523SEric Paris
97690586523SEric Paris #endif /* __LINUX_FSNOTIFY_BACKEND_H */
977