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 <eparis@redhat.com>
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 */
3490586523SEric Paris #define FS_CLOSE_WRITE		0x00000008	/* Writtable file was closed */
3590586523SEric Paris #define FS_CLOSE_NOWRITE	0x00000010	/* Unwrittable 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 */
58c4ec54b4SEric Paris 
599b93f331SAmir Goldstein /*
609b93f331SAmir Goldstein  * Set on inode mark that cares about things that happen to its children.
619b93f331SAmir Goldstein  * Always set for dnotify and inotify.
629b93f331SAmir Goldstein  * Set on inode/sb/mount marks that care about parent/name info.
639b93f331SAmir Goldstein  */
64c28f7e56SEric Paris #define FS_EVENT_ON_CHILD	0x08000000
65c28f7e56SEric Paris 
66e54183faSAmir Goldstein #define FS_RENAME		0x10000000	/* File was renamed */
676473ea76SAmir Goldstein #define FS_DN_MULTISHOT		0x20000000	/* dnotify multishot */
686473ea76SAmir Goldstein #define FS_ISDIR		0x40000000	/* event occurred against dir */
696473ea76SAmir Goldstein 
70e9fd702aSEric Paris #define FS_MOVE			(FS_MOVED_FROM | FS_MOVED_TO)
71e9fd702aSEric Paris 
72e220140fSAmir Goldstein /*
73e220140fSAmir Goldstein  * Directory entry modification events - reported only to directory
74e220140fSAmir Goldstein  * where entry is modified and not to a watching parent.
75e220140fSAmir Goldstein  * The watching parent may get an FS_ATTRIB|FS_EVENT_ON_CHILD event
76e220140fSAmir Goldstein  * when a directory entry inside a child subdir changes.
77e220140fSAmir Goldstein  */
78e54183faSAmir Goldstein #define ALL_FSNOTIFY_DIRENT_EVENTS (FS_CREATE | FS_DELETE | FS_MOVE | FS_RENAME)
79e220140fSAmir Goldstein 
8066917a31SMatthew Bobrowski #define ALL_FSNOTIFY_PERM_EVENTS (FS_OPEN_PERM | FS_ACCESS_PERM | \
8166917a31SMatthew Bobrowski 				  FS_OPEN_EXEC_PERM)
82ff8bcbd0SEric Paris 
83e220140fSAmir Goldstein /*
849b93f331SAmir Goldstein  * This is a list of all events that may get sent to a parent that is watching
859b93f331SAmir Goldstein  * with flag FS_EVENT_ON_CHILD based on fs event on a child of that directory.
86e220140fSAmir Goldstein  */
87e220140fSAmir Goldstein #define FS_EVENTS_POSS_ON_CHILD   (ALL_FSNOTIFY_PERM_EVENTS | \
88e220140fSAmir Goldstein 				   FS_ACCESS | FS_MODIFY | FS_ATTRIB | \
89e220140fSAmir Goldstein 				   FS_CLOSE_WRITE | FS_CLOSE_NOWRITE | \
90e220140fSAmir Goldstein 				   FS_OPEN | FS_OPEN_EXEC)
91e220140fSAmir Goldstein 
929b93f331SAmir Goldstein /*
939b93f331SAmir Goldstein  * This is a list of all events that may get sent with the parent inode as the
949b93f331SAmir Goldstein  * @to_tell argument of fsnotify().
959b93f331SAmir Goldstein  * It may include events that can be sent to an inode/sb/mount mark, but cannot
969b93f331SAmir Goldstein  * be sent to a parent watching children.
979b93f331SAmir Goldstein  */
989b93f331SAmir Goldstein #define FS_EVENTS_POSS_TO_PARENT (FS_EVENTS_POSS_ON_CHILD)
999b93f331SAmir Goldstein 
100007d1e83SAmir Goldstein /* Events that can be reported to backends */
101e220140fSAmir Goldstein #define ALL_FSNOTIFY_EVENTS (ALL_FSNOTIFY_DIRENT_EVENTS | \
102e220140fSAmir Goldstein 			     FS_EVENTS_POSS_ON_CHILD | \
103e54183faSAmir Goldstein 			     FS_DELETE_SELF | FS_MOVE_SELF | \
1049daa8110SGabriel Krisman Bertazi 			     FS_UNMOUNT | FS_Q_OVERFLOW | FS_IN_IGNORED | \
1059daa8110SGabriel Krisman Bertazi 			     FS_ERROR)
106007d1e83SAmir Goldstein 
107007d1e83SAmir Goldstein /* Extra flags that may be reported with event or control handling of events */
10838035c04SAmir Goldstein #define ALL_FSNOTIFY_FLAGS  (FS_ISDIR | FS_EVENT_ON_CHILD | FS_DN_MULTISHOT)
10920dee624SEric Paris 
110007d1e83SAmir Goldstein #define ALL_FSNOTIFY_BITS   (ALL_FSNOTIFY_EVENTS | ALL_FSNOTIFY_FLAGS)
111007d1e83SAmir Goldstein 
11290586523SEric Paris struct fsnotify_group;
11390586523SEric Paris struct fsnotify_event;
114e61ce867SEric Paris struct fsnotify_mark;
115e4aff117SEric Paris struct fsnotify_event_private_data;
1167053aee2SJan Kara struct fsnotify_fname;
117abc77577SJan Kara struct fsnotify_iter_info;
11890586523SEric Paris 
119d46eb14bSShakeel Butt struct mem_cgroup;
120d46eb14bSShakeel Butt 
12190586523SEric Paris /*
12290586523SEric Paris  * Each group much define these ops.  The fsnotify infrastructure will call
12390586523SEric Paris  * these operations for each relevant group.
12490586523SEric Paris  *
12590586523SEric Paris  * handle_event - main call for a group to handle an fs event
126b54cecf5SAmir Goldstein  * @group:	group to notify
127b54cecf5SAmir Goldstein  * @mask:	event type and flags
128b54cecf5SAmir Goldstein  * @data:	object that event happened on
129b54cecf5SAmir Goldstein  * @data_type:	type of object for fanotify_data_XXX() accessors
130b54cecf5SAmir Goldstein  * @dir:	optional directory associated with event -
131b54cecf5SAmir Goldstein  *		if @file_name is not NULL, this is the directory that
132b54cecf5SAmir Goldstein  *		@file_name is relative to
133b54cecf5SAmir Goldstein  * @file_name:	optional file name associated with event
134b54cecf5SAmir Goldstein  * @cookie:	inotify rename cookie
135b54cecf5SAmir Goldstein  * @iter_info:	array of marks from this group that are interested in the event
136b54cecf5SAmir Goldstein  *
137b9a1b977SAmir Goldstein  * handle_inode_event - simple variant of handle_event() for groups that only
138b9a1b977SAmir Goldstein  *		have inode marks and don't have ignore mask
139b9a1b977SAmir Goldstein  * @mark:	mark to notify
140b9a1b977SAmir Goldstein  * @mask:	event type and flags
141b9a1b977SAmir Goldstein  * @inode:	inode that event happened on
142b9a1b977SAmir Goldstein  * @dir:	optional directory associated with event -
143b9a1b977SAmir Goldstein  *		if @file_name is not NULL, this is the directory that
144b9a1b977SAmir Goldstein  *		@file_name is relative to.
14524dca905SGabriel Krisman Bertazi  *		Either @inode or @dir must be non-NULL.
146b9a1b977SAmir Goldstein  * @file_name:	optional file name associated with event
147950cc0d2SAmir Goldstein  * @cookie:	inotify rename cookie
148b9a1b977SAmir Goldstein  *
14990586523SEric Paris  * free_group_priv - called when a group refcnt hits 0 to clean up the private union
1506960b0d9SLino Sanfilippo  * freeing_mark - called when a mark is being destroyed for some reason.  The group
1516960b0d9SLino Sanfilippo  *		MUST be holding a reference on each mark and that reference must be
1526960b0d9SLino Sanfilippo  *		dropped in this function.  inotify uses this function to send
1536960b0d9SLino Sanfilippo  *		userspace messages that marks have been removed.
15490586523SEric Paris  */
15590586523SEric Paris struct fsnotify_ops {
156b54cecf5SAmir Goldstein 	int (*handle_event)(struct fsnotify_group *group, u32 mask,
157b54cecf5SAmir Goldstein 			    const void *data, int data_type, struct inode *dir,
158e43e9c33SAl Viro 			    const struct qstr *file_name, u32 cookie,
1599385a84dSJan Kara 			    struct fsnotify_iter_info *iter_info);
160b9a1b977SAmir Goldstein 	int (*handle_inode_event)(struct fsnotify_mark *mark, u32 mask,
161b9a1b977SAmir Goldstein 			    struct inode *inode, struct inode *dir,
162950cc0d2SAmir Goldstein 			    const struct qstr *file_name, u32 cookie);
16390586523SEric Paris 	void (*free_group_priv)(struct fsnotify_group *group);
164841bdc10SEric Paris 	void (*freeing_mark)(struct fsnotify_mark *mark, struct fsnotify_group *group);
165330ae77dSGabriel Krisman Bertazi 	void (*free_event)(struct fsnotify_group *group, struct fsnotify_event *event);
166054c636eSJan Kara 	/* called on final put+free to free memory */
167054c636eSJan Kara 	void (*free_mark)(struct fsnotify_mark *mark);
1687053aee2SJan Kara };
1697053aee2SJan Kara 
1707053aee2SJan Kara /*
1717053aee2SJan Kara  * all of the information about the original object we want to now send to
1727053aee2SJan Kara  * a group.  If you want to carry more info from the accessing task to the
1737053aee2SJan Kara  * listener this structure is where you need to be adding fields.
1747053aee2SJan Kara  */
1757053aee2SJan Kara struct fsnotify_event {
1767053aee2SJan Kara 	struct list_head list;
17790586523SEric Paris };
17890586523SEric Paris 
17990586523SEric Paris /*
18090586523SEric Paris  * A group is a "thing" that wants to receive notification about filesystem
18190586523SEric Paris  * events.  The mask holds the subset of event types this group cares about.
18290586523SEric Paris  * refcnt on a group is up to the implementor and at any moment if it goes 0
18390586523SEric Paris  * everything will be cleaned up.
18490586523SEric Paris  */
18590586523SEric Paris struct fsnotify_group {
186d46eb14bSShakeel Butt 	const struct fsnotify_ops *ops;	/* how this group handles things */
187d46eb14bSShakeel Butt 
18890586523SEric Paris 	/*
18990586523SEric Paris 	 * How the refcnt is used is up to each group.  When the refcnt hits 0
19090586523SEric Paris 	 * fsnotify will clean up all of the resources associated with this group.
19190586523SEric Paris 	 * As an example, the dnotify group will always have a refcnt=1 and that
19290586523SEric Paris 	 * will never change.  Inotify, on the other hand, has a group per
19390586523SEric Paris 	 * inotify_init() and the refcnt will hit 0 only when that fd has been
19490586523SEric Paris 	 * closed.
19590586523SEric Paris 	 */
1967761daa6SElena Reshetova 	refcount_t refcnt;		/* things with interest in this group */
19790586523SEric Paris 
198a2d8bc6cSEric Paris 	/* needed to send notification to userspace */
199c21dbe20SJan Kara 	spinlock_t notification_lock;		/* protect the notification_list */
200a2d8bc6cSEric Paris 	struct list_head notification_list;	/* list of event_holder this group needs to send to userspace */
201a2d8bc6cSEric Paris 	wait_queue_head_t notification_waitq;	/* read() on the notification file blocks on this waitq */
202a2d8bc6cSEric Paris 	unsigned int q_len;			/* events on the queue */
203a2d8bc6cSEric Paris 	unsigned int max_events;		/* maximum events allowed on the list */
2046ad2d4e3SEric Paris 	/*
2056ad2d4e3SEric Paris 	 * Valid fsnotify group priorities.  Events are send in order from highest
2066ad2d4e3SEric Paris 	 * priority to lowest priority.  We default to the lowest priority.
2076ad2d4e3SEric Paris 	 */
2086ad2d4e3SEric Paris 	#define FS_PRIO_0	0 /* normal notifiers, no permissions */
2096ad2d4e3SEric Paris 	#define FS_PRIO_1	1 /* fanotify content based access control */
2106ad2d4e3SEric Paris 	#define FS_PRIO_2	2 /* fanotify pre-content access */
2116ad2d4e3SEric Paris 	unsigned int priority;
21212703dbfSJan Kara 	bool shutdown;		/* group is being shut down, don't queue more events */
213a2d8bc6cSEric Paris 
214867a448dSAmir Goldstein #define FSNOTIFY_GROUP_USER	0x01 /* user allocated group */
215f3010343SAmir Goldstein #define FSNOTIFY_GROUP_DUPS	0x02 /* allow multiple marks per object */
21643b245a7SAmir Goldstein #define FSNOTIFY_GROUP_NOFS	0x04 /* group lock is not direct reclaim safe */
217867a448dSAmir Goldstein 	int flags;
21843b245a7SAmir Goldstein 	unsigned int owner_flags;	/* stored flags of mark_mutex owner */
219867a448dSAmir Goldstein 
220e61ce867SEric Paris 	/* stores all fastpath marks assoc with this group so they can be cleaned on unregister */
221986ab098SLino Sanfilippo 	struct mutex mark_mutex;	/* protect marks_list */
222d46eb14bSShakeel Butt 	atomic_t user_waits;		/* Number of tasks waiting for user
223d46eb14bSShakeel Butt 					 * response */
224e61ce867SEric Paris 	struct list_head marks_list;	/* all inode marks for this group */
2253be25f49SEric Paris 
2260a6b6bd5SEric Paris 	struct fasync_struct *fsn_fa;    /* async notification */
2270a6b6bd5SEric Paris 
228ff57cd58SJan Kara 	struct fsnotify_event *overflow_event;	/* Event we queue when the
2297053aee2SJan Kara 						 * notification list is too
2307053aee2SJan Kara 						 * full */
231d46eb14bSShakeel Butt 
232d46eb14bSShakeel Butt 	struct mem_cgroup *memcg;	/* memcg to charge allocations */
2337053aee2SJan Kara 
23490586523SEric Paris 	/* groups can define private fields here or use the void *private */
23590586523SEric Paris 	union {
23690586523SEric Paris 		void *private;
23763c882a0SEric Paris #ifdef CONFIG_INOTIFY_USER
23863c882a0SEric Paris 		struct inotify_group_private_data {
23963c882a0SEric Paris 			spinlock_t	idr_lock;
24063c882a0SEric Paris 			struct idr      idr;
2411cce1eeaSNikolay Borisov 			struct ucounts *ucounts;
24263c882a0SEric Paris 		} inotify_data;
24363c882a0SEric Paris #endif
24480af2588SEric Paris #ifdef CONFIG_FANOTIFY
2459e66e423SEric Paris 		struct fanotify_group_private_data {
24694e00d28SAmir Goldstein 			/* Hash table of events for merge */
24794e00d28SAmir Goldstein 			struct hlist_head *merge_hash;
2489e66e423SEric Paris 			/* allows a group to block waiting for a userspace response */
2499e66e423SEric Paris 			struct list_head access_list;
2509e66e423SEric Paris 			wait_queue_head_t access_waitq;
25196a71f21SAmir Goldstein 			int flags;           /* flags from fanotify_init() */
25296a71f21SAmir Goldstein 			int f_flags; /* event_f_flags from fanotify_init() */
2535b8fea65SAmir Goldstein 			struct ucounts *ucounts;
254734a1a5eSGabriel Krisman Bertazi 			mempool_t error_events_pool;
2559e66e423SEric Paris 		} fanotify_data;
25680af2588SEric Paris #endif /* CONFIG_FANOTIFY */
25790586523SEric Paris 	};
25890586523SEric Paris };
25990586523SEric Paris 
26043b245a7SAmir Goldstein /*
26143b245a7SAmir Goldstein  * These helpers are used to prevent deadlock when reclaiming inodes with
26243b245a7SAmir Goldstein  * evictable marks of the same group that is allocating a new mark.
26343b245a7SAmir Goldstein  */
fsnotify_group_lock(struct fsnotify_group * group)26443b245a7SAmir Goldstein static inline void fsnotify_group_lock(struct fsnotify_group *group)
26543b245a7SAmir Goldstein {
26643b245a7SAmir Goldstein 	mutex_lock(&group->mark_mutex);
26743b245a7SAmir Goldstein 	if (group->flags & FSNOTIFY_GROUP_NOFS)
26843b245a7SAmir Goldstein 		group->owner_flags = memalloc_nofs_save();
26943b245a7SAmir Goldstein }
27043b245a7SAmir Goldstein 
fsnotify_group_unlock(struct fsnotify_group * group)27143b245a7SAmir Goldstein static inline void fsnotify_group_unlock(struct fsnotify_group *group)
27243b245a7SAmir Goldstein {
27343b245a7SAmir Goldstein 	if (group->flags & FSNOTIFY_GROUP_NOFS)
27443b245a7SAmir Goldstein 		memalloc_nofs_restore(group->owner_flags);
27543b245a7SAmir Goldstein 	mutex_unlock(&group->mark_mutex);
27643b245a7SAmir Goldstein }
27743b245a7SAmir Goldstein 
fsnotify_group_assert_locked(struct fsnotify_group * group)27843b245a7SAmir Goldstein static inline void fsnotify_group_assert_locked(struct fsnotify_group *group)
27943b245a7SAmir Goldstein {
28043b245a7SAmir Goldstein 	WARN_ON_ONCE(!mutex_is_locked(&group->mark_mutex));
28143b245a7SAmir Goldstein 	if (group->flags & FSNOTIFY_GROUP_NOFS)
28243b245a7SAmir Goldstein 		WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS));
28343b245a7SAmir Goldstein }
28443b245a7SAmir Goldstein 
285aa93bdc5SAmir Goldstein /* When calling fsnotify tell it if the data is a path or inode */
286aa93bdc5SAmir Goldstein enum fsnotify_data_type {
287aa93bdc5SAmir Goldstein 	FSNOTIFY_EVENT_NONE,
288aa93bdc5SAmir Goldstein 	FSNOTIFY_EVENT_PATH,
289aa93bdc5SAmir Goldstein 	FSNOTIFY_EVENT_INODE,
290fd5a3ff4SAmir Goldstein 	FSNOTIFY_EVENT_DENTRY,
2919daa8110SGabriel Krisman Bertazi 	FSNOTIFY_EVENT_ERROR,
2929daa8110SGabriel Krisman Bertazi };
2939daa8110SGabriel Krisman Bertazi 
2949daa8110SGabriel Krisman Bertazi struct fs_error_report {
2959daa8110SGabriel Krisman Bertazi 	int error;
2969daa8110SGabriel Krisman Bertazi 	struct inode *inode;
2979daa8110SGabriel Krisman Bertazi 	struct super_block *sb;
298aa93bdc5SAmir Goldstein };
299aa93bdc5SAmir Goldstein 
fsnotify_data_inode(const void * data,int data_type)300cbcf47adSAmir Goldstein static inline struct inode *fsnotify_data_inode(const void *data, int data_type)
301aa93bdc5SAmir Goldstein {
302aa93bdc5SAmir Goldstein 	switch (data_type) {
303aa93bdc5SAmir Goldstein 	case FSNOTIFY_EVENT_INODE:
304cbcf47adSAmir Goldstein 		return (struct inode *)data;
305fd5a3ff4SAmir Goldstein 	case FSNOTIFY_EVENT_DENTRY:
306fd5a3ff4SAmir Goldstein 		return d_inode(data);
307aa93bdc5SAmir Goldstein 	case FSNOTIFY_EVENT_PATH:
308aa93bdc5SAmir Goldstein 		return d_inode(((const struct path *)data)->dentry);
3099daa8110SGabriel Krisman Bertazi 	case FSNOTIFY_EVENT_ERROR:
3109daa8110SGabriel Krisman Bertazi 		return ((struct fs_error_report *)data)->inode;
311aa93bdc5SAmir Goldstein 	default:
312aa93bdc5SAmir Goldstein 		return NULL;
313aa93bdc5SAmir Goldstein 	}
314aa93bdc5SAmir Goldstein }
315aa93bdc5SAmir Goldstein 
fsnotify_data_dentry(const void * data,int data_type)316fd5a3ff4SAmir Goldstein static inline struct dentry *fsnotify_data_dentry(const void *data, int data_type)
317fd5a3ff4SAmir Goldstein {
318fd5a3ff4SAmir Goldstein 	switch (data_type) {
319fd5a3ff4SAmir Goldstein 	case FSNOTIFY_EVENT_DENTRY:
320fd5a3ff4SAmir Goldstein 		/* Non const is needed for dget() */
321fd5a3ff4SAmir Goldstein 		return (struct dentry *)data;
322fd5a3ff4SAmir Goldstein 	case FSNOTIFY_EVENT_PATH:
323fd5a3ff4SAmir Goldstein 		return ((const struct path *)data)->dentry;
324fd5a3ff4SAmir Goldstein 	default:
325fd5a3ff4SAmir Goldstein 		return NULL;
326fd5a3ff4SAmir Goldstein 	}
327fd5a3ff4SAmir Goldstein }
328fd5a3ff4SAmir Goldstein 
fsnotify_data_path(const void * data,int data_type)329aa93bdc5SAmir Goldstein static inline const struct path *fsnotify_data_path(const void *data,
330aa93bdc5SAmir Goldstein 						    int data_type)
331aa93bdc5SAmir Goldstein {
332aa93bdc5SAmir Goldstein 	switch (data_type) {
333aa93bdc5SAmir Goldstein 	case FSNOTIFY_EVENT_PATH:
334aa93bdc5SAmir Goldstein 		return data;
335aa93bdc5SAmir Goldstein 	default:
336aa93bdc5SAmir Goldstein 		return NULL;
337aa93bdc5SAmir Goldstein 	}
338aa93bdc5SAmir Goldstein }
33990586523SEric Paris 
fsnotify_data_sb(const void * data,int data_type)34029335033SGabriel Krisman Bertazi static inline struct super_block *fsnotify_data_sb(const void *data,
34129335033SGabriel Krisman Bertazi 						   int data_type)
34229335033SGabriel Krisman Bertazi {
34329335033SGabriel Krisman Bertazi 	switch (data_type) {
34429335033SGabriel Krisman Bertazi 	case FSNOTIFY_EVENT_INODE:
34529335033SGabriel Krisman Bertazi 		return ((struct inode *)data)->i_sb;
34629335033SGabriel Krisman Bertazi 	case FSNOTIFY_EVENT_DENTRY:
34729335033SGabriel Krisman Bertazi 		return ((struct dentry *)data)->d_sb;
34829335033SGabriel Krisman Bertazi 	case FSNOTIFY_EVENT_PATH:
34929335033SGabriel Krisman Bertazi 		return ((const struct path *)data)->dentry->d_sb;
3509daa8110SGabriel Krisman Bertazi 	case FSNOTIFY_EVENT_ERROR:
3519daa8110SGabriel Krisman Bertazi 		return ((struct fs_error_report *) data)->sb;
3529daa8110SGabriel Krisman Bertazi 	default:
3539daa8110SGabriel Krisman Bertazi 		return NULL;
3549daa8110SGabriel Krisman Bertazi 	}
3559daa8110SGabriel Krisman Bertazi }
3569daa8110SGabriel Krisman Bertazi 
fsnotify_data_error_report(const void * data,int data_type)3579daa8110SGabriel Krisman Bertazi static inline struct fs_error_report *fsnotify_data_error_report(
3589daa8110SGabriel Krisman Bertazi 							const void *data,
3599daa8110SGabriel Krisman Bertazi 							int data_type)
3609daa8110SGabriel Krisman Bertazi {
3619daa8110SGabriel Krisman Bertazi 	switch (data_type) {
3629daa8110SGabriel Krisman Bertazi 	case FSNOTIFY_EVENT_ERROR:
3639daa8110SGabriel Krisman Bertazi 		return (struct fs_error_report *) data;
36429335033SGabriel Krisman Bertazi 	default:
36529335033SGabriel Krisman Bertazi 		return NULL;
36629335033SGabriel Krisman Bertazi 	}
36729335033SGabriel Krisman Bertazi }
36829335033SGabriel Krisman Bertazi 
3691c9007d6SAmir Goldstein /*
3701c9007d6SAmir Goldstein  * Index to merged marks iterator array that correlates to a type of watch.
3711c9007d6SAmir Goldstein  * The type of watched object can be deduced from the iterator type, but not
3721c9007d6SAmir Goldstein  * the other way around, because an event can match different watched objects
3731c9007d6SAmir Goldstein  * of the same object type.
3741c9007d6SAmir Goldstein  * For example, both parent and child are watching an object of type inode.
3751c9007d6SAmir Goldstein  */
3761c9007d6SAmir Goldstein enum fsnotify_iter_type {
3771c9007d6SAmir Goldstein 	FSNOTIFY_ITER_TYPE_INODE,
3781c9007d6SAmir Goldstein 	FSNOTIFY_ITER_TYPE_VFSMOUNT,
3791c9007d6SAmir Goldstein 	FSNOTIFY_ITER_TYPE_SB,
3801c9007d6SAmir Goldstein 	FSNOTIFY_ITER_TYPE_PARENT,
381e54183faSAmir Goldstein 	FSNOTIFY_ITER_TYPE_INODE2,
3821c9007d6SAmir Goldstein 	FSNOTIFY_ITER_TYPE_COUNT
3831c9007d6SAmir Goldstein };
3841c9007d6SAmir Goldstein 
3851c9007d6SAmir Goldstein /* The type of object that a mark is attached to */
386d6f7b98bSAmir Goldstein enum fsnotify_obj_type {
387ad69cd99SAmir Goldstein 	FSNOTIFY_OBJ_TYPE_ANY = -1,
388d6f7b98bSAmir Goldstein 	FSNOTIFY_OBJ_TYPE_INODE,
389d6f7b98bSAmir Goldstein 	FSNOTIFY_OBJ_TYPE_VFSMOUNT,
3901e6cb723SAmir Goldstein 	FSNOTIFY_OBJ_TYPE_SB,
391d6f7b98bSAmir Goldstein 	FSNOTIFY_OBJ_TYPE_COUNT,
392d6f7b98bSAmir Goldstein 	FSNOTIFY_OBJ_TYPE_DETACHED = FSNOTIFY_OBJ_TYPE_COUNT
393d6f7b98bSAmir Goldstein };
394d6f7b98bSAmir Goldstein 
fsnotify_valid_obj_type(unsigned int obj_type)395ad69cd99SAmir Goldstein static inline bool fsnotify_valid_obj_type(unsigned int obj_type)
396b812a9f5SAmir Goldstein {
397ad69cd99SAmir Goldstein 	return (obj_type < FSNOTIFY_OBJ_TYPE_COUNT);
398b812a9f5SAmir Goldstein }
399b812a9f5SAmir Goldstein 
4005b0457adSAmir Goldstein struct fsnotify_iter_info {
4011c9007d6SAmir Goldstein 	struct fsnotify_mark *marks[FSNOTIFY_ITER_TYPE_COUNT];
40214362a25SAmir Goldstein 	struct fsnotify_group *current_group;
4035b0457adSAmir Goldstein 	unsigned int report_mask;
4045b0457adSAmir Goldstein 	int srcu_idx;
4055b0457adSAmir Goldstein };
4065b0457adSAmir Goldstein 
fsnotify_iter_should_report_type(struct fsnotify_iter_info * iter_info,int iter_type)40747d9c7ccSAmir Goldstein static inline bool fsnotify_iter_should_report_type(
4081c9007d6SAmir Goldstein 		struct fsnotify_iter_info *iter_info, int iter_type)
40947d9c7ccSAmir Goldstein {
4101c9007d6SAmir Goldstein 	return (iter_info->report_mask & (1U << iter_type));
41147d9c7ccSAmir Goldstein }
41247d9c7ccSAmir Goldstein 
fsnotify_iter_set_report_type(struct fsnotify_iter_info * iter_info,int iter_type)41347d9c7ccSAmir Goldstein static inline void fsnotify_iter_set_report_type(
4141c9007d6SAmir Goldstein 		struct fsnotify_iter_info *iter_info, int iter_type)
41547d9c7ccSAmir Goldstein {
4161c9007d6SAmir Goldstein 	iter_info->report_mask |= (1U << iter_type);
41747d9c7ccSAmir Goldstein }
41847d9c7ccSAmir Goldstein 
fsnotify_iter_mark(struct fsnotify_iter_info * iter_info,int iter_type)41914362a25SAmir Goldstein static inline struct fsnotify_mark *fsnotify_iter_mark(
42014362a25SAmir Goldstein 		struct fsnotify_iter_info *iter_info, int iter_type)
42147d9c7ccSAmir Goldstein {
42214362a25SAmir Goldstein 	if (fsnotify_iter_should_report_type(iter_info, iter_type))
42314362a25SAmir Goldstein 		return iter_info->marks[iter_type];
42414362a25SAmir Goldstein 	return NULL;
42514362a25SAmir Goldstein }
42614362a25SAmir Goldstein 
fsnotify_iter_step(struct fsnotify_iter_info * iter,int type,struct fsnotify_mark ** markp)42714362a25SAmir Goldstein static inline int fsnotify_iter_step(struct fsnotify_iter_info *iter, int type,
42814362a25SAmir Goldstein 				     struct fsnotify_mark **markp)
42914362a25SAmir Goldstein {
43014362a25SAmir Goldstein 	while (type < FSNOTIFY_ITER_TYPE_COUNT) {
43114362a25SAmir Goldstein 		*markp = fsnotify_iter_mark(iter, type);
43214362a25SAmir Goldstein 		if (*markp)
43314362a25SAmir Goldstein 			break;
43414362a25SAmir Goldstein 		type++;
43514362a25SAmir Goldstein 	}
43614362a25SAmir Goldstein 	return type;
43747d9c7ccSAmir Goldstein }
43847d9c7ccSAmir Goldstein 
4395b0457adSAmir Goldstein #define FSNOTIFY_ITER_FUNCS(name, NAME) \
4405b0457adSAmir Goldstein static inline struct fsnotify_mark *fsnotify_iter_##name##_mark( \
4415b0457adSAmir Goldstein 		struct fsnotify_iter_info *iter_info) \
4425b0457adSAmir Goldstein { \
44314362a25SAmir Goldstein 	return fsnotify_iter_mark(iter_info, FSNOTIFY_ITER_TYPE_##NAME); \
4445b0457adSAmir Goldstein }
4455b0457adSAmir Goldstein 
4465b0457adSAmir Goldstein FSNOTIFY_ITER_FUNCS(inode, INODE)
447fecc4559SAmir Goldstein FSNOTIFY_ITER_FUNCS(parent, PARENT)
4485b0457adSAmir Goldstein FSNOTIFY_ITER_FUNCS(vfsmount, VFSMOUNT)
4491e6cb723SAmir Goldstein FSNOTIFY_ITER_FUNCS(sb, SB)
4505b0457adSAmir Goldstein 
4511c9007d6SAmir Goldstein #define fsnotify_foreach_iter_type(type) \
4521c9007d6SAmir Goldstein 	for (type = 0; type < FSNOTIFY_ITER_TYPE_COUNT; type++)
45314362a25SAmir Goldstein #define fsnotify_foreach_iter_mark_type(iter, mark, type) \
45414362a25SAmir Goldstein 	for (type = 0; \
45514362a25SAmir Goldstein 	     type = fsnotify_iter_step(iter, type, &mark), \
45614362a25SAmir Goldstein 	     type < FSNOTIFY_ITER_TYPE_COUNT; \
45714362a25SAmir Goldstein 	     type++)
45847d9c7ccSAmir Goldstein 
4593be25f49SEric Paris /*
46036f10f55SAmir Goldstein  * fsnotify_connp_t is what we embed in objects which connector can be attached
46136f10f55SAmir Goldstein  * to. fsnotify_connp_t * is how we refer from connector back to object.
46236f10f55SAmir Goldstein  */
46336f10f55SAmir Goldstein struct fsnotify_mark_connector;
46436f10f55SAmir Goldstein typedef struct fsnotify_mark_connector __rcu *fsnotify_connp_t;
46536f10f55SAmir Goldstein 
46636f10f55SAmir Goldstein /*
4671e6cb723SAmir Goldstein  * Inode/vfsmount/sb point to this structure which tracks all marks attached to
4681e6cb723SAmir Goldstein  * the inode/vfsmount/sb. The reference to inode/vfsmount/sb is held by this
46908991e83SJan Kara  * structure. We destroy this structure when there are no more marks attached
47008991e83SJan Kara  * to it. The structure is protected by fsnotify_mark_srcu.
4719dd813c1SJan Kara  */
4729dd813c1SJan Kara struct fsnotify_mark_connector {
47304662cabSJan Kara 	spinlock_t lock;
474c285a2f0SAmir Goldstein 	unsigned short type;	/* Type of object [lock] */
475c285a2f0SAmir Goldstein #define FSNOTIFY_CONN_FLAG_HAS_FSID	0x01
476c3638b5bSAmir Goldstein #define FSNOTIFY_CONN_FLAG_HAS_IREF	0x02
477c285a2f0SAmir Goldstein 	unsigned short flags;	/* flags [lock] */
47877115225SAmir Goldstein 	__kernel_fsid_t fsid;	/* fsid of filesystem containing object */
47936f10f55SAmir Goldstein 	union {
48036f10f55SAmir Goldstein 		/* Object pointer [lock] */
48136f10f55SAmir Goldstein 		fsnotify_connp_t *obj;
48208991e83SJan Kara 		/* Used listing heads to free after srcu period expires */
48308991e83SJan Kara 		struct fsnotify_mark_connector *destroy_next;
48408991e83SJan Kara 	};
485d90a10e2SRobert Kolchmeyer 	struct hlist_head list;
4869dd813c1SJan Kara };
4879dd813c1SJan Kara 
4889dd813c1SJan Kara /*
4891e39fc01SJan Kara  * A mark is simply an object attached to an in core inode which allows an
4903be25f49SEric Paris  * fsnotify listener to indicate they are either no longer interested in events
4913be25f49SEric Paris  * of a type matching mask or only interested in those events.
4923be25f49SEric Paris  *
4931e39fc01SJan Kara  * These are flushed when an inode is evicted from core and may be flushed
4941e39fc01SJan Kara  * when the inode is modified (as seen by fsnotify_access).  Some fsnotify
4951e39fc01SJan Kara  * users (such as dnotify) will flush these when the open fd is closed and not
4961e39fc01SJan Kara  * at inode eviction or modification.
4971e39fc01SJan Kara  *
4981e39fc01SJan Kara  * Text in brackets is showing the lock(s) protecting modifications of a
4991e39fc01SJan Kara  * particular entry. obj_lock means either inode->i_lock or
5001e39fc01SJan Kara  * mnt->mnt_root->d_lock depending on the mark type.
5013be25f49SEric Paris  */
502e61ce867SEric Paris struct fsnotify_mark {
5031e39fc01SJan Kara 	/* Mask this mark is for [mark->lock, group->mark_mutex] */
5041e39fc01SJan Kara 	__u32 mask;
5051e39fc01SJan Kara 	/* We hold one for presence in g_list. Also one ref for each 'thing'
5063be25f49SEric Paris 	 * in kernel that found and may be using this mark. */
507ab97f873SElena Reshetova 	refcount_t refcnt;
5081e39fc01SJan Kara 	/* Group this mark is for. Set on mark creation, stable until last ref
5091e39fc01SJan Kara 	 * is dropped */
5101e39fc01SJan Kara 	struct fsnotify_group *group;
5118e984f86SAmir Goldstein 	/* List of marks by group->marks_list. Also reused for queueing
5121e39fc01SJan Kara 	 * mark into destroy_list when it's waiting for the end of SRCU period
5131e39fc01SJan Kara 	 * before it can be freed. [group->mark_mutex] */
5141e39fc01SJan Kara 	struct list_head g_list;
5151e39fc01SJan Kara 	/* Protects inode / mnt pointers, flags, masks */
5161e39fc01SJan Kara 	spinlock_t lock;
5176b3f05d2SJan Kara 	/* List of marks for inode / vfsmount [connector->lock, mark ref] */
5181e39fc01SJan Kara 	struct hlist_node obj_list;
5196b3f05d2SJan Kara 	/* Head of list of marks for an object [mark ref] */
52086ffe245SJan Kara 	struct fsnotify_mark_connector *connector;
521*31a371e4SAmir Goldstein 	/* Events types and flags to ignore [mark->lock, group->mark_mutex] */
522*31a371e4SAmir Goldstein 	__u32 ignore_mask;
52338035c04SAmir Goldstein 	/* General fsnotify mark flags */
52438035c04SAmir Goldstein #define FSNOTIFY_MARK_FLAG_ALIVE		0x0001
52538035c04SAmir Goldstein #define FSNOTIFY_MARK_FLAG_ATTACHED		0x0002
52638035c04SAmir Goldstein 	/* inotify mark flags */
52738035c04SAmir Goldstein #define FSNOTIFY_MARK_FLAG_EXCL_UNLINK		0x0010
52838035c04SAmir Goldstein #define FSNOTIFY_MARK_FLAG_IN_ONESHOT		0x0020
52938035c04SAmir Goldstein 	/* fanotify mark flags */
53038035c04SAmir Goldstein #define FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY	0x0100
531c3638b5bSAmir Goldstein #define FSNOTIFY_MARK_FLAG_NO_IREF		0x0200
532*31a371e4SAmir Goldstein #define FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS	0x0400
5331e39fc01SJan Kara 	unsigned int flags;		/* flags [mark->lock] */
5343be25f49SEric Paris };
5353be25f49SEric Paris 
53690586523SEric Paris #ifdef CONFIG_FSNOTIFY
53790586523SEric Paris 
53890586523SEric Paris /* called from the vfs helpers */
53990586523SEric Paris 
54090586523SEric Paris /* main fsnotify call to send events */
54140a100d3SAmir Goldstein extern int fsnotify(__u32 mask, const void *data, int data_type,
54240a100d3SAmir Goldstein 		    struct inode *dir, const struct qstr *name,
54340a100d3SAmir Goldstein 		    struct inode *inode, u32 cookie);
54471d73410SMel Gorman extern int __fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data,
545017de65fSAmir Goldstein 			   int data_type);
5463be25f49SEric Paris extern void __fsnotify_inode_delete(struct inode *inode);
547ca9c726eSAndreas Gruenbacher extern void __fsnotify_vfsmount_delete(struct vfsmount *mnt);
5481e6cb723SAmir Goldstein extern void fsnotify_sb_delete(struct super_block *sb);
54947882c6fSEric Paris extern u32 fsnotify_get_cookie(void);
55090586523SEric Paris 
fsnotify_parent_needed_mask(__u32 mask)5519b93f331SAmir Goldstein static inline __u32 fsnotify_parent_needed_mask(__u32 mask)
5529b93f331SAmir Goldstein {
5539b93f331SAmir Goldstein 	/* FS_EVENT_ON_CHILD is set on marks that want parent/name info */
5549b93f331SAmir Goldstein 	if (!(mask & FS_EVENT_ON_CHILD))
5559b93f331SAmir Goldstein 		return 0;
5569b93f331SAmir Goldstein 	/*
5579b93f331SAmir Goldstein 	 * This object might be watched by a mark that cares about parent/name
5589b93f331SAmir Goldstein 	 * info, does it care about the specific set of events that can be
5599b93f331SAmir Goldstein 	 * reported with parent/name info?
5609b93f331SAmir Goldstein 	 */
5619b93f331SAmir Goldstein 	return mask & FS_EVENTS_POSS_TO_PARENT;
5629b93f331SAmir Goldstein }
5639b93f331SAmir Goldstein 
fsnotify_inode_watches_children(struct inode * inode)564c28f7e56SEric Paris static inline int fsnotify_inode_watches_children(struct inode *inode)
565c28f7e56SEric Paris {
566c28f7e56SEric Paris 	/* FS_EVENT_ON_CHILD is set if the inode may care */
567c28f7e56SEric Paris 	if (!(inode->i_fsnotify_mask & FS_EVENT_ON_CHILD))
568c28f7e56SEric Paris 		return 0;
569c28f7e56SEric Paris 	/* this inode might care about child events, does it care about the
570c28f7e56SEric Paris 	 * specific set of events that can happen on a child? */
571c28f7e56SEric Paris 	return inode->i_fsnotify_mask & FS_EVENTS_POSS_ON_CHILD;
572c28f7e56SEric Paris }
573c28f7e56SEric Paris 
574c28f7e56SEric Paris /*
575c28f7e56SEric Paris  * Update the dentry with a flag indicating the interest of its parent to receive
576c28f7e56SEric Paris  * filesystem events when those events happens to this dentry->d_inode.
577c28f7e56SEric Paris  */
fsnotify_update_flags(struct dentry * dentry)578affda484SAl Viro static inline void fsnotify_update_flags(struct dentry *dentry)
579c28f7e56SEric Paris {
580c28f7e56SEric Paris 	assert_spin_locked(&dentry->d_lock);
581c28f7e56SEric Paris 
582b5c84bf6SNick Piggin 	/*
583b5c84bf6SNick Piggin 	 * Serialisation of setting PARENT_WATCHED on the dentries is provided
584b5c84bf6SNick Piggin 	 * by d_lock. If inotify_inode_watched changes after we have taken
585b5c84bf6SNick Piggin 	 * d_lock, the following __fsnotify_update_child_dentry_flags call will
586b5c84bf6SNick Piggin 	 * find our entry, so it will spin until we complete here, and update
587b5c84bf6SNick Piggin 	 * us with the new state.
588b5c84bf6SNick Piggin 	 */
589affda484SAl Viro 	if (fsnotify_inode_watches_children(dentry->d_parent->d_inode))
590c28f7e56SEric Paris 		dentry->d_flags |= DCACHE_FSNOTIFY_PARENT_WATCHED;
591c28f7e56SEric Paris 	else
592c28f7e56SEric Paris 		dentry->d_flags &= ~DCACHE_FSNOTIFY_PARENT_WATCHED;
593c28f7e56SEric Paris }
594c28f7e56SEric Paris 
59590586523SEric Paris /* called from fsnotify listeners, such as fanotify or dnotify */
59690586523SEric Paris 
59798612952SLino Sanfilippo /* create a new group */
598867a448dSAmir Goldstein extern struct fsnotify_group *fsnotify_alloc_group(
599867a448dSAmir Goldstein 				const struct fsnotify_ops *ops,
600867a448dSAmir Goldstein 				int flags);
60198612952SLino Sanfilippo /* get reference to a group */
60298612952SLino Sanfilippo extern void fsnotify_get_group(struct fsnotify_group *group);
603ffab8340SEric Paris /* drop reference on a group from fsnotify_alloc_group */
60490586523SEric Paris extern void fsnotify_put_group(struct fsnotify_group *group);
60512703dbfSJan Kara /* group destruction begins, stop queuing new events */
60612703dbfSJan Kara extern void fsnotify_group_stop_queueing(struct fsnotify_group *group);
607d8153d4dSLino Sanfilippo /* destroy group */
608d8153d4dSLino Sanfilippo extern void fsnotify_destroy_group(struct fsnotify_group *group);
6090a6b6bd5SEric Paris /* fasync handler function */
6100a6b6bd5SEric Paris extern int fsnotify_fasync(int fd, struct file *file, int on);
6117053aee2SJan Kara /* Free event from memory */
6127053aee2SJan Kara extern void fsnotify_destroy_event(struct fsnotify_group *group,
61390586523SEric Paris 				   struct fsnotify_event *event);
614a2d8bc6cSEric Paris /* attach the event to the group notification queue */
6151ad03c3aSGabriel Krisman Bertazi extern int fsnotify_insert_event(struct fsnotify_group *group,
61674766bbfSEric Paris 				 struct fsnotify_event *event,
61794e00d28SAmir Goldstein 				 int (*merge)(struct fsnotify_group *,
61894e00d28SAmir Goldstein 					      struct fsnotify_event *),
61994e00d28SAmir Goldstein 				 void (*insert)(struct fsnotify_group *,
620f70ab54cSEric Paris 						struct fsnotify_event *));
6211ad03c3aSGabriel Krisman Bertazi 
fsnotify_add_event(struct fsnotify_group * group,struct fsnotify_event * event,int (* merge)(struct fsnotify_group *,struct fsnotify_event *))6221ad03c3aSGabriel Krisman Bertazi static inline int fsnotify_add_event(struct fsnotify_group *group,
6231ad03c3aSGabriel Krisman Bertazi 				     struct fsnotify_event *event,
6241ad03c3aSGabriel Krisman Bertazi 				     int (*merge)(struct fsnotify_group *,
6251ad03c3aSGabriel Krisman Bertazi 						  struct fsnotify_event *))
6261ad03c3aSGabriel Krisman Bertazi {
6271ad03c3aSGabriel Krisman Bertazi 	return fsnotify_insert_event(group, event, merge, NULL);
6281ad03c3aSGabriel Krisman Bertazi }
6291ad03c3aSGabriel Krisman Bertazi 
6307b1f6417SJan Kara /* Queue overflow event to a notification group */
fsnotify_queue_overflow(struct fsnotify_group * group)6317b1f6417SJan Kara static inline void fsnotify_queue_overflow(struct fsnotify_group *group)
6327b1f6417SJan Kara {
6331ad03c3aSGabriel Krisman Bertazi 	fsnotify_add_event(group, group->overflow_event, NULL);
6347b1f6417SJan Kara }
6357b1f6417SJan Kara 
fsnotify_is_overflow_event(u32 mask)636808967a0SGabriel Krisman Bertazi static inline bool fsnotify_is_overflow_event(u32 mask)
637808967a0SGabriel Krisman Bertazi {
638808967a0SGabriel Krisman Bertazi 	return mask & FS_Q_OVERFLOW;
639808967a0SGabriel Krisman Bertazi }
640808967a0SGabriel Krisman Bertazi 
fsnotify_notify_queue_is_empty(struct fsnotify_group * group)6416f73171eSAmir Goldstein static inline bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group)
6426f73171eSAmir Goldstein {
6436f73171eSAmir Goldstein 	assert_spin_locked(&group->notification_lock);
6446f73171eSAmir Goldstein 
6456f73171eSAmir Goldstein 	return list_empty(&group->notification_list);
6466f73171eSAmir Goldstein }
6476f73171eSAmir Goldstein 
648a2d8bc6cSEric Paris extern bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group);
649a2d8bc6cSEric Paris /* return, but do not dequeue the first event on the notification queue */
6508ba8fa91SJan Kara extern struct fsnotify_event *fsnotify_peek_first_event(struct fsnotify_group *group);
651e4aff117SEric Paris /* return AND dequeue the first event on the notification queue */
6528ba8fa91SJan Kara extern struct fsnotify_event *fsnotify_remove_first_event(struct fsnotify_group *group);
653f7db89acSJan Kara /* Remove event queued in the notification list */
654f7db89acSJan Kara extern void fsnotify_remove_queued_event(struct fsnotify_group *group,
655f7db89acSJan Kara 					 struct fsnotify_event *event);
656a2d8bc6cSEric Paris 
6573be25f49SEric Paris /* functions used to manipulate the marks attached to inodes */
6583be25f49SEric Paris 
659*31a371e4SAmir Goldstein /*
660*31a371e4SAmir Goldstein  * Canonical "ignore mask" including event flags.
661*31a371e4SAmir Goldstein  *
662*31a371e4SAmir Goldstein  * Note the subtle semantic difference from the legacy ->ignored_mask.
663*31a371e4SAmir Goldstein  * ->ignored_mask traditionally only meant which events should be ignored,
664*31a371e4SAmir Goldstein  * while ->ignore_mask also includes flags regarding the type of objects on
665*31a371e4SAmir Goldstein  * which events should be ignored.
666*31a371e4SAmir Goldstein  */
fsnotify_ignore_mask(struct fsnotify_mark * mark)667*31a371e4SAmir Goldstein static inline __u32 fsnotify_ignore_mask(struct fsnotify_mark *mark)
668*31a371e4SAmir Goldstein {
669*31a371e4SAmir Goldstein 	__u32 ignore_mask = mark->ignore_mask;
670*31a371e4SAmir Goldstein 
671*31a371e4SAmir Goldstein 	/* The event flags in ignore mask take effect */
672*31a371e4SAmir Goldstein 	if (mark->flags & FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS)
673*31a371e4SAmir Goldstein 		return ignore_mask;
674*31a371e4SAmir Goldstein 
675*31a371e4SAmir Goldstein 	/*
676*31a371e4SAmir Goldstein 	 * Legacy behavior:
677*31a371e4SAmir Goldstein 	 * - Always ignore events on dir
678*31a371e4SAmir Goldstein 	 * - Ignore events on child if parent is watching children
679*31a371e4SAmir Goldstein 	 */
680*31a371e4SAmir Goldstein 	ignore_mask |= FS_ISDIR;
681*31a371e4SAmir Goldstein 	ignore_mask &= ~FS_EVENT_ON_CHILD;
682*31a371e4SAmir Goldstein 	ignore_mask |= mark->mask & FS_EVENT_ON_CHILD;
683*31a371e4SAmir Goldstein 
684*31a371e4SAmir Goldstein 	return ignore_mask;
685*31a371e4SAmir Goldstein }
686*31a371e4SAmir Goldstein 
687*31a371e4SAmir Goldstein /* Legacy ignored_mask - only event types to ignore */
fsnotify_ignored_events(struct fsnotify_mark * mark)688*31a371e4SAmir Goldstein static inline __u32 fsnotify_ignored_events(struct fsnotify_mark *mark)
689*31a371e4SAmir Goldstein {
690*31a371e4SAmir Goldstein 	return mark->ignore_mask & ALL_FSNOTIFY_EVENTS;
691*31a371e4SAmir Goldstein }
692*31a371e4SAmir Goldstein 
693*31a371e4SAmir Goldstein /*
694*31a371e4SAmir Goldstein  * Check if mask (or ignore mask) should be applied depending if victim is a
695*31a371e4SAmir Goldstein  * directory and whether it is reported to a watching parent.
696*31a371e4SAmir Goldstein  */
fsnotify_mask_applicable(__u32 mask,bool is_dir,int iter_type)697*31a371e4SAmir Goldstein static inline bool fsnotify_mask_applicable(__u32 mask, bool is_dir,
698*31a371e4SAmir Goldstein 					    int iter_type)
699*31a371e4SAmir Goldstein {
700*31a371e4SAmir Goldstein 	/* Should mask be applied to a directory? */
701*31a371e4SAmir Goldstein 	if (is_dir && !(mask & FS_ISDIR))
702*31a371e4SAmir Goldstein 		return false;
703*31a371e4SAmir Goldstein 
704*31a371e4SAmir Goldstein 	/* Should mask be applied to a child? */
705*31a371e4SAmir Goldstein 	if (iter_type == FSNOTIFY_ITER_TYPE_PARENT &&
706*31a371e4SAmir Goldstein 	    !(mask & FS_EVENT_ON_CHILD))
707*31a371e4SAmir Goldstein 		return false;
708*31a371e4SAmir Goldstein 
709*31a371e4SAmir Goldstein 	return true;
710*31a371e4SAmir Goldstein }
711*31a371e4SAmir Goldstein 
712*31a371e4SAmir Goldstein /*
713*31a371e4SAmir Goldstein  * Effective ignore mask taking into account if event victim is a
714*31a371e4SAmir Goldstein  * directory and whether it is reported to a watching parent.
715*31a371e4SAmir Goldstein  */
fsnotify_effective_ignore_mask(struct fsnotify_mark * mark,bool is_dir,int iter_type)716*31a371e4SAmir Goldstein static inline __u32 fsnotify_effective_ignore_mask(struct fsnotify_mark *mark,
717*31a371e4SAmir Goldstein 						   bool is_dir, int iter_type)
718*31a371e4SAmir Goldstein {
719*31a371e4SAmir Goldstein 	__u32 ignore_mask = fsnotify_ignored_events(mark);
720*31a371e4SAmir Goldstein 
721*31a371e4SAmir Goldstein 	if (!ignore_mask)
722*31a371e4SAmir Goldstein 		return 0;
723*31a371e4SAmir Goldstein 
724*31a371e4SAmir Goldstein 	/* For non-dir and non-child, no need to consult the event flags */
725*31a371e4SAmir Goldstein 	if (!is_dir && iter_type != FSNOTIFY_ITER_TYPE_PARENT)
726*31a371e4SAmir Goldstein 		return ignore_mask;
727*31a371e4SAmir Goldstein 
728*31a371e4SAmir Goldstein 	ignore_mask = fsnotify_ignore_mask(mark);
729*31a371e4SAmir Goldstein 	if (!fsnotify_mask_applicable(ignore_mask, is_dir, iter_type))
730*31a371e4SAmir Goldstein 		return 0;
731*31a371e4SAmir Goldstein 
732*31a371e4SAmir Goldstein 	return ignore_mask & ALL_FSNOTIFY_EVENTS;
733*31a371e4SAmir Goldstein }
734*31a371e4SAmir Goldstein 
735*31a371e4SAmir Goldstein /* Get mask for calculating object interest taking ignore mask into account */
fsnotify_calc_mask(struct fsnotify_mark * mark)7364f0b903dSAmir Goldstein static inline __u32 fsnotify_calc_mask(struct fsnotify_mark *mark)
7374f0b903dSAmir Goldstein {
7384f0b903dSAmir Goldstein 	__u32 mask = mark->mask;
7394f0b903dSAmir Goldstein 
740*31a371e4SAmir Goldstein 	if (!fsnotify_ignored_events(mark))
7414f0b903dSAmir Goldstein 		return mask;
7424f0b903dSAmir Goldstein 
743*31a371e4SAmir Goldstein 	/* Interest in FS_MODIFY may be needed for clearing ignore mask */
74404e317baSAmir Goldstein 	if (!(mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY))
74504e317baSAmir Goldstein 		mask |= FS_MODIFY;
74604e317baSAmir Goldstein 
7474f0b903dSAmir Goldstein 	/*
7484f0b903dSAmir Goldstein 	 * If mark is interested in ignoring events on children, the object must
7494f0b903dSAmir Goldstein 	 * show interest in those events for fsnotify_parent() to notice it.
7504f0b903dSAmir Goldstein 	 */
751*31a371e4SAmir Goldstein 	return mask | mark->ignore_mask;
7524f0b903dSAmir Goldstein }
7534f0b903dSAmir Goldstein 
7543ac70bfcSAmir Goldstein /* Get mask of events for a list of marks */
7553ac70bfcSAmir Goldstein extern __u32 fsnotify_conn_mask(struct fsnotify_mark_connector *conn);
756a242677bSJan Kara /* Calculate mask of events for a list of marks */
757a242677bSJan Kara extern void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn);
7587b129323SJan Kara extern void fsnotify_init_mark(struct fsnotify_mark *mark,
759054c636eSJan Kara 			       struct fsnotify_group *group);
760b1362edfSJan Kara /* Find mark belonging to given group in the list of marks */
7619b6e5434SAmir Goldstein extern struct fsnotify_mark *fsnotify_find_mark(fsnotify_connp_t *connp,
762b1362edfSJan Kara 						struct fsnotify_group *group);
763b812a9f5SAmir Goldstein /* attach the mark to the object */
764b812a9f5SAmir Goldstein extern int fsnotify_add_mark(struct fsnotify_mark *mark,
765ad69cd99SAmir Goldstein 			     fsnotify_connp_t *connp, unsigned int obj_type,
766f3010343SAmir Goldstein 			     int add_flags, __kernel_fsid_t *fsid);
7677b129323SJan Kara extern int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
76877115225SAmir Goldstein 				    fsnotify_connp_t *connp,
769f3010343SAmir Goldstein 				    unsigned int obj_type, int add_flags,
77077115225SAmir Goldstein 				    __kernel_fsid_t *fsid);
77177115225SAmir Goldstein 
772b249f5beSAmir Goldstein /* attach the mark to the inode */
fsnotify_add_inode_mark(struct fsnotify_mark * mark,struct inode * inode,int add_flags)773b249f5beSAmir Goldstein static inline int fsnotify_add_inode_mark(struct fsnotify_mark *mark,
774b249f5beSAmir Goldstein 					  struct inode *inode,
775f3010343SAmir Goldstein 					  int add_flags)
776b249f5beSAmir Goldstein {
777b812a9f5SAmir Goldstein 	return fsnotify_add_mark(mark, &inode->i_fsnotify_marks,
778f3010343SAmir Goldstein 				 FSNOTIFY_OBJ_TYPE_INODE, add_flags, NULL);
779b249f5beSAmir Goldstein }
fsnotify_add_inode_mark_locked(struct fsnotify_mark * mark,struct inode * inode,int add_flags)780b249f5beSAmir Goldstein static inline int fsnotify_add_inode_mark_locked(struct fsnotify_mark *mark,
781b249f5beSAmir Goldstein 						 struct inode *inode,
782f3010343SAmir Goldstein 						 int add_flags)
783b249f5beSAmir Goldstein {
784b812a9f5SAmir Goldstein 	return fsnotify_add_mark_locked(mark, &inode->i_fsnotify_marks,
785f3010343SAmir Goldstein 					FSNOTIFY_OBJ_TYPE_INODE, add_flags,
78677115225SAmir Goldstein 					NULL);
787b249f5beSAmir Goldstein }
78877115225SAmir Goldstein 
789e2a29943SLino Sanfilippo /* given a group and a mark, flag mark to be freed when all references are dropped */
790e2a29943SLino Sanfilippo extern void fsnotify_destroy_mark(struct fsnotify_mark *mark,
791e2a29943SLino Sanfilippo 				  struct fsnotify_group *group);
7924712e722SJan Kara /* detach mark from inode / mount list, group list, drop inode reference */
7934712e722SJan Kara extern void fsnotify_detach_mark(struct fsnotify_mark *mark);
7944712e722SJan Kara /* free mark */
7954712e722SJan Kara extern void fsnotify_free_mark(struct fsnotify_mark *mark);
796b72679eeSTrond Myklebust /* Wait until all marks queued for destruction are destroyed */
797b72679eeSTrond Myklebust extern void fsnotify_wait_marks_destroyed(void);
798ad69cd99SAmir Goldstein /* Clear all of the marks of a group attached to a given object type */
799ad69cd99SAmir Goldstein extern void fsnotify_clear_marks_by_group(struct fsnotify_group *group,
800ad69cd99SAmir Goldstein 					  unsigned int obj_type);
801416bcdbcSJan Kara /* run all the marks in a group, and clear all of the vfsmount marks */
fsnotify_clear_vfsmount_marks_by_group(struct fsnotify_group * group)802416bcdbcSJan Kara static inline void fsnotify_clear_vfsmount_marks_by_group(struct fsnotify_group *group)
803416bcdbcSJan Kara {
804ad69cd99SAmir Goldstein 	fsnotify_clear_marks_by_group(group, FSNOTIFY_OBJ_TYPE_VFSMOUNT);
805416bcdbcSJan Kara }
806416bcdbcSJan Kara /* run all the marks in a group, and clear all of the inode marks */
fsnotify_clear_inode_marks_by_group(struct fsnotify_group * group)807416bcdbcSJan Kara static inline void fsnotify_clear_inode_marks_by_group(struct fsnotify_group *group)
808416bcdbcSJan Kara {
809ad69cd99SAmir Goldstein 	fsnotify_clear_marks_by_group(group, FSNOTIFY_OBJ_TYPE_INODE);
810416bcdbcSJan Kara }
8111e6cb723SAmir Goldstein /* run all the marks in a group, and clear all of the sn marks */
fsnotify_clear_sb_marks_by_group(struct fsnotify_group * group)8121e6cb723SAmir Goldstein static inline void fsnotify_clear_sb_marks_by_group(struct fsnotify_group *group)
8131e6cb723SAmir Goldstein {
814ad69cd99SAmir Goldstein 	fsnotify_clear_marks_by_group(group, FSNOTIFY_OBJ_TYPE_SB);
8151e6cb723SAmir Goldstein }
816841bdc10SEric Paris extern void fsnotify_get_mark(struct fsnotify_mark *mark);
817841bdc10SEric Paris extern void fsnotify_put_mark(struct fsnotify_mark *mark);
818abc77577SJan Kara extern void fsnotify_finish_user_wait(struct fsnotify_iter_info *iter_info);
819abc77577SJan Kara extern bool fsnotify_prepare_user_wait(struct fsnotify_iter_info *iter_info);
8203be25f49SEric Paris 
fsnotify_init_event(struct fsnotify_event * event)8218988f11aSAmir Goldstein static inline void fsnotify_init_event(struct fsnotify_event *event)
822a0a92d26SAmir Goldstein {
823a0a92d26SAmir Goldstein 	INIT_LIST_HEAD(&event->list);
824a0a92d26SAmir Goldstein }
825b4e4e140SEric Paris 
82690586523SEric Paris #else
82790586523SEric Paris 
fsnotify(__u32 mask,const void * data,int data_type,struct inode * dir,const struct qstr * name,struct inode * inode,u32 cookie)82840a100d3SAmir Goldstein static inline int fsnotify(__u32 mask, const void *data, int data_type,
82940a100d3SAmir Goldstein 			   struct inode *dir, const struct qstr *name,
83040a100d3SAmir Goldstein 			   struct inode *inode, u32 cookie)
831c4ec54b4SEric Paris {
832c4ec54b4SEric Paris 	return 0;
833c4ec54b4SEric Paris }
8343be25f49SEric Paris 
__fsnotify_parent(struct dentry * dentry,__u32 mask,const void * data,int data_type)83571d73410SMel Gorman static inline int __fsnotify_parent(struct dentry *dentry, __u32 mask,
836017de65fSAmir Goldstein 				  const void *data, int data_type)
83752420392SEric Paris {
83852420392SEric Paris 	return 0;
83952420392SEric Paris }
840c28f7e56SEric Paris 
__fsnotify_inode_delete(struct inode * inode)8413be25f49SEric Paris static inline void __fsnotify_inode_delete(struct inode *inode)
8423be25f49SEric Paris {}
8433be25f49SEric Paris 
__fsnotify_vfsmount_delete(struct vfsmount * mnt)844ca9c726eSAndreas Gruenbacher static inline void __fsnotify_vfsmount_delete(struct vfsmount *mnt)
845ca9c726eSAndreas Gruenbacher {}
846ca9c726eSAndreas Gruenbacher 
fsnotify_sb_delete(struct super_block * sb)8471e6cb723SAmir Goldstein static inline void fsnotify_sb_delete(struct super_block *sb)
8481e6cb723SAmir Goldstein {}
8491e6cb723SAmir Goldstein 
fsnotify_update_flags(struct dentry * dentry)850affda484SAl Viro static inline void fsnotify_update_flags(struct dentry *dentry)
851c28f7e56SEric Paris {}
852c28f7e56SEric Paris 
fsnotify_get_cookie(void)85347882c6fSEric Paris static inline u32 fsnotify_get_cookie(void)
85447882c6fSEric Paris {
85547882c6fSEric Paris 	return 0;
85647882c6fSEric Paris }
85747882c6fSEric Paris 
fsnotify_unmount_inodes(struct super_block * sb)85874278da9SDave Chinner static inline void fsnotify_unmount_inodes(struct super_block *sb)
859164bc619SEric Paris {}
860164bc619SEric Paris 
86190586523SEric Paris #endif	/* CONFIG_FSNOTIFY */
86290586523SEric Paris 
86390586523SEric Paris #endif	/* __KERNEL __ */
86490586523SEric Paris 
86590586523SEric Paris #endif	/* __LINUX_FSNOTIFY_BACKEND_H */
866