xref: /linux/include/linux/fsnotify_backend.h (revision 8883957b3c9de2087fb6cf9691c1188cccf1ac9c)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Filesystem access notification for Linux
4  *
5  *  Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com>
6  */
7 
8 #ifndef __LINUX_FSNOTIFY_BACKEND_H
9 #define __LINUX_FSNOTIFY_BACKEND_H
10 
11 #ifdef __KERNEL__
12 
13 #include <linux/idr.h> /* inotify uses this */
14 #include <linux/fs.h> /* struct inode */
15 #include <linux/list.h>
16 #include <linux/path.h> /* struct path */
17 #include <linux/spinlock.h>
18 #include <linux/types.h>
19 #include <linux/atomic.h>
20 #include <linux/user_namespace.h>
21 #include <linux/refcount.h>
22 #include <linux/mempool.h>
23 #include <linux/sched/mm.h>
24 
25 /*
26  * IN_* from inotfy.h lines up EXACTLY with FS_*, this is so we can easily
27  * convert between them.  dnotify only needs conversion at watch creation
28  * so no perf loss there.  fanotify isn't defined yet, so it can use the
29  * wholes if it needs more events.
30  */
31 #define FS_ACCESS		0x00000001	/* File was accessed */
32 #define FS_MODIFY		0x00000002	/* File was modified */
33 #define FS_ATTRIB		0x00000004	/* Metadata changed */
34 #define FS_CLOSE_WRITE		0x00000008	/* Writable file was closed */
35 #define FS_CLOSE_NOWRITE	0x00000010	/* Unwritable file closed */
36 #define FS_OPEN			0x00000020	/* File was opened */
37 #define FS_MOVED_FROM		0x00000040	/* File was moved from X */
38 #define FS_MOVED_TO		0x00000080	/* File was moved to Y */
39 #define FS_CREATE		0x00000100	/* Subfile was created */
40 #define FS_DELETE		0x00000200	/* Subfile was deleted */
41 #define FS_DELETE_SELF		0x00000400	/* Self was deleted */
42 #define FS_MOVE_SELF		0x00000800	/* Self was moved */
43 #define FS_OPEN_EXEC		0x00001000	/* File was opened for exec */
44 
45 #define FS_UNMOUNT		0x00002000	/* inode on umount fs */
46 #define FS_Q_OVERFLOW		0x00004000	/* Event queued overflowed */
47 #define FS_ERROR		0x00008000	/* Filesystem Error (fanotify) */
48 
49 /*
50  * FS_IN_IGNORED overloads FS_ERROR.  It is only used internally by inotify
51  * which does not support FS_ERROR.
52  */
53 #define FS_IN_IGNORED		0x00008000	/* last inotify event here */
54 
55 #define FS_OPEN_PERM		0x00010000	/* open event in an permission hook */
56 #define FS_ACCESS_PERM		0x00020000	/* access event in a permissions hook */
57 #define FS_OPEN_EXEC_PERM	0x00040000	/* open/exec event in a permission hook */
58 /* #define FS_DIR_MODIFY	0x00080000 */	/* Deprecated (reserved) */
59 
60 #define FS_PRE_ACCESS		0x00100000	/* Pre-content access hook */
61 
62 /*
63  * Set on inode mark that cares about things that happen to its children.
64  * Always set for dnotify and inotify.
65  * Set on inode/sb/mount marks that care about parent/name info.
66  */
67 #define FS_EVENT_ON_CHILD	0x08000000
68 
69 #define FS_RENAME		0x10000000	/* File was renamed */
70 #define FS_DN_MULTISHOT		0x20000000	/* dnotify multishot */
71 #define FS_ISDIR		0x40000000	/* event occurred against dir */
72 
73 #define FS_MOVE			(FS_MOVED_FROM | FS_MOVED_TO)
74 
75 /*
76  * Directory entry modification events - reported only to directory
77  * where entry is modified and not to a watching parent.
78  * The watching parent may get an FS_ATTRIB|FS_EVENT_ON_CHILD event
79  * when a directory entry inside a child subdir changes.
80  */
81 #define ALL_FSNOTIFY_DIRENT_EVENTS (FS_CREATE | FS_DELETE | FS_MOVE | FS_RENAME)
82 
83 /* Content events can be used to inspect file content */
84 #define FSNOTIFY_CONTENT_PERM_EVENTS (FS_OPEN_PERM | FS_OPEN_EXEC_PERM | \
85 				      FS_ACCESS_PERM)
86 /* Pre-content events can be used to fill file content */
87 #define FSNOTIFY_PRE_CONTENT_EVENTS  (FS_PRE_ACCESS)
88 
89 #define ALL_FSNOTIFY_PERM_EVENTS (FSNOTIFY_CONTENT_PERM_EVENTS | \
90 				  FSNOTIFY_PRE_CONTENT_EVENTS)
91 
92 /*
93  * This is a list of all events that may get sent to a parent that is watching
94  * with flag FS_EVENT_ON_CHILD based on fs event on a child of that directory.
95  */
96 #define FS_EVENTS_POSS_ON_CHILD   (ALL_FSNOTIFY_PERM_EVENTS | \
97 				   FS_ACCESS | FS_MODIFY | FS_ATTRIB | \
98 				   FS_CLOSE_WRITE | FS_CLOSE_NOWRITE | \
99 				   FS_OPEN | FS_OPEN_EXEC)
100 
101 /*
102  * This is a list of all events that may get sent with the parent inode as the
103  * @to_tell argument of fsnotify().
104  * It may include events that can be sent to an inode/sb/mount mark, but cannot
105  * be sent to a parent watching children.
106  */
107 #define FS_EVENTS_POSS_TO_PARENT (FS_EVENTS_POSS_ON_CHILD)
108 
109 /* Events that can be reported to backends */
110 #define ALL_FSNOTIFY_EVENTS (ALL_FSNOTIFY_DIRENT_EVENTS | \
111 			     FS_EVENTS_POSS_ON_CHILD | \
112 			     FS_DELETE_SELF | FS_MOVE_SELF | \
113 			     FS_UNMOUNT | FS_Q_OVERFLOW | FS_IN_IGNORED | \
114 			     FS_ERROR)
115 
116 /* Extra flags that may be reported with event or control handling of events */
117 #define ALL_FSNOTIFY_FLAGS  (FS_ISDIR | FS_EVENT_ON_CHILD | FS_DN_MULTISHOT)
118 
119 #define ALL_FSNOTIFY_BITS   (ALL_FSNOTIFY_EVENTS | ALL_FSNOTIFY_FLAGS)
120 
121 struct fsnotify_group;
122 struct fsnotify_event;
123 struct fsnotify_mark;
124 struct fsnotify_event_private_data;
125 struct fsnotify_fname;
126 struct fsnotify_iter_info;
127 
128 struct mem_cgroup;
129 
130 /*
131  * Each group much define these ops.  The fsnotify infrastructure will call
132  * these operations for each relevant group.
133  *
134  * handle_event - main call for a group to handle an fs event
135  * @group:	group to notify
136  * @mask:	event type and flags
137  * @data:	object that event happened on
138  * @data_type:	type of object for fanotify_data_XXX() accessors
139  * @dir:	optional directory associated with event -
140  *		if @file_name is not NULL, this is the directory that
141  *		@file_name is relative to
142  * @file_name:	optional file name associated with event
143  * @cookie:	inotify rename cookie
144  * @iter_info:	array of marks from this group that are interested in the event
145  *
146  * handle_inode_event - simple variant of handle_event() for groups that only
147  *		have inode marks and don't have ignore mask
148  * @mark:	mark to notify
149  * @mask:	event type and flags
150  * @inode:	inode that event happened on
151  * @dir:	optional directory associated with event -
152  *		if @file_name is not NULL, this is the directory that
153  *		@file_name is relative to.
154  *		Either @inode or @dir must be non-NULL.
155  * @file_name:	optional file name associated with event
156  * @cookie:	inotify rename cookie
157  *
158  * free_group_priv - called when a group refcnt hits 0 to clean up the private union
159  * freeing_mark - called when a mark is being destroyed for some reason.  The group
160  *		MUST be holding a reference on each mark and that reference must be
161  *		dropped in this function.  inotify uses this function to send
162  *		userspace messages that marks have been removed.
163  */
164 struct fsnotify_ops {
165 	int (*handle_event)(struct fsnotify_group *group, u32 mask,
166 			    const void *data, int data_type, struct inode *dir,
167 			    const struct qstr *file_name, u32 cookie,
168 			    struct fsnotify_iter_info *iter_info);
169 	int (*handle_inode_event)(struct fsnotify_mark *mark, u32 mask,
170 			    struct inode *inode, struct inode *dir,
171 			    const struct qstr *file_name, u32 cookie);
172 	void (*free_group_priv)(struct fsnotify_group *group);
173 	void (*freeing_mark)(struct fsnotify_mark *mark, struct fsnotify_group *group);
174 	void (*free_event)(struct fsnotify_group *group, struct fsnotify_event *event);
175 	/* called on final put+free to free memory */
176 	void (*free_mark)(struct fsnotify_mark *mark);
177 };
178 
179 /*
180  * all of the information about the original object we want to now send to
181  * a group.  If you want to carry more info from the accessing task to the
182  * listener this structure is where you need to be adding fields.
183  */
184 struct fsnotify_event {
185 	struct list_head list;
186 };
187 
188 /*
189  * fsnotify group priorities.
190  * Events are sent in order from highest priority to lowest priority.
191  */
192 enum fsnotify_group_prio {
193 	FSNOTIFY_PRIO_NORMAL = 0,	/* normal notifiers, no permissions */
194 	FSNOTIFY_PRIO_CONTENT,		/* fanotify permission events */
195 	FSNOTIFY_PRIO_PRE_CONTENT,	/* fanotify pre-content events */
196 	__FSNOTIFY_PRIO_NUM
197 };
198 
199 /*
200  * A group is a "thing" that wants to receive notification about filesystem
201  * events.  The mask holds the subset of event types this group cares about.
202  * refcnt on a group is up to the implementor and at any moment if it goes 0
203  * everything will be cleaned up.
204  */
205 struct fsnotify_group {
206 	const struct fsnotify_ops *ops;	/* how this group handles things */
207 
208 	/*
209 	 * How the refcnt is used is up to each group.  When the refcnt hits 0
210 	 * fsnotify will clean up all of the resources associated with this group.
211 	 * As an example, the dnotify group will always have a refcnt=1 and that
212 	 * will never change.  Inotify, on the other hand, has a group per
213 	 * inotify_init() and the refcnt will hit 0 only when that fd has been
214 	 * closed.
215 	 */
216 	refcount_t refcnt;		/* things with interest in this group */
217 
218 	/* needed to send notification to userspace */
219 	spinlock_t notification_lock;		/* protect the notification_list */
220 	struct list_head notification_list;	/* list of event_holder this group needs to send to userspace */
221 	wait_queue_head_t notification_waitq;	/* read() on the notification file blocks on this waitq */
222 	unsigned int q_len;			/* events on the queue */
223 	unsigned int max_events;		/* maximum events allowed on the list */
224 	enum fsnotify_group_prio priority;	/* priority for sending events */
225 	bool shutdown;		/* group is being shut down, don't queue more events */
226 
227 #define FSNOTIFY_GROUP_USER	0x01 /* user allocated group */
228 #define FSNOTIFY_GROUP_DUPS	0x02 /* allow multiple marks per object */
229 	int flags;
230 	unsigned int owner_flags;	/* stored flags of mark_mutex owner */
231 
232 	/* stores all fastpath marks assoc with this group so they can be cleaned on unregister */
233 	struct mutex mark_mutex;	/* protect marks_list */
234 	atomic_t user_waits;		/* Number of tasks waiting for user
235 					 * response */
236 	struct list_head marks_list;	/* all inode marks for this group */
237 
238 	struct fasync_struct *fsn_fa;    /* async notification */
239 
240 	struct fsnotify_event *overflow_event;	/* Event we queue when the
241 						 * notification list is too
242 						 * full */
243 
244 	struct mem_cgroup *memcg;	/* memcg to charge allocations */
245 
246 	/* groups can define private fields here or use the void *private */
247 	union {
248 		void *private;
249 #ifdef CONFIG_INOTIFY_USER
250 		struct inotify_group_private_data {
251 			spinlock_t	idr_lock;
252 			struct idr      idr;
253 			struct ucounts *ucounts;
254 		} inotify_data;
255 #endif
256 #ifdef CONFIG_FANOTIFY
257 		struct fanotify_group_private_data {
258 			/* Hash table of events for merge */
259 			struct hlist_head *merge_hash;
260 			/* allows a group to block waiting for a userspace response */
261 			struct list_head access_list;
262 			wait_queue_head_t access_waitq;
263 			int flags;           /* flags from fanotify_init() */
264 			int f_flags; /* event_f_flags from fanotify_init() */
265 			struct ucounts *ucounts;
266 			mempool_t error_events_pool;
267 		} fanotify_data;
268 #endif /* CONFIG_FANOTIFY */
269 	};
270 };
271 
272 /*
273  * These helpers are used to prevent deadlock when reclaiming inodes with
274  * evictable marks of the same group that is allocating a new mark.
275  */
fsnotify_group_lock(struct fsnotify_group * group)276 static inline void fsnotify_group_lock(struct fsnotify_group *group)
277 {
278 	mutex_lock(&group->mark_mutex);
279 	group->owner_flags = memalloc_nofs_save();
280 }
281 
fsnotify_group_unlock(struct fsnotify_group * group)282 static inline void fsnotify_group_unlock(struct fsnotify_group *group)
283 {
284 	memalloc_nofs_restore(group->owner_flags);
285 	mutex_unlock(&group->mark_mutex);
286 }
287 
fsnotify_group_assert_locked(struct fsnotify_group * group)288 static inline void fsnotify_group_assert_locked(struct fsnotify_group *group)
289 {
290 	WARN_ON_ONCE(!mutex_is_locked(&group->mark_mutex));
291 	WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS));
292 }
293 
294 /* When calling fsnotify tell it if the data is a path or inode */
295 enum fsnotify_data_type {
296 	FSNOTIFY_EVENT_NONE,
297 	FSNOTIFY_EVENT_FILE_RANGE,
298 	FSNOTIFY_EVENT_PATH,
299 	FSNOTIFY_EVENT_INODE,
300 	FSNOTIFY_EVENT_DENTRY,
301 	FSNOTIFY_EVENT_ERROR,
302 };
303 
304 struct fs_error_report {
305 	int error;
306 	struct inode *inode;
307 	struct super_block *sb;
308 };
309 
310 struct file_range {
311 	const struct path *path;
312 	loff_t pos;
313 	size_t count;
314 };
315 
file_range_path(const struct file_range * range)316 static inline const struct path *file_range_path(const struct file_range *range)
317 {
318 	return range->path;
319 }
320 
fsnotify_data_inode(const void * data,int data_type)321 static inline struct inode *fsnotify_data_inode(const void *data, int data_type)
322 {
323 	switch (data_type) {
324 	case FSNOTIFY_EVENT_INODE:
325 		return (struct inode *)data;
326 	case FSNOTIFY_EVENT_DENTRY:
327 		return d_inode(data);
328 	case FSNOTIFY_EVENT_PATH:
329 		return d_inode(((const struct path *)data)->dentry);
330 	case FSNOTIFY_EVENT_FILE_RANGE:
331 		return d_inode(file_range_path(data)->dentry);
332 	case FSNOTIFY_EVENT_ERROR:
333 		return ((struct fs_error_report *)data)->inode;
334 	default:
335 		return NULL;
336 	}
337 }
338 
fsnotify_data_dentry(const void * data,int data_type)339 static inline struct dentry *fsnotify_data_dentry(const void *data, int data_type)
340 {
341 	switch (data_type) {
342 	case FSNOTIFY_EVENT_DENTRY:
343 		/* Non const is needed for dget() */
344 		return (struct dentry *)data;
345 	case FSNOTIFY_EVENT_PATH:
346 		return ((const struct path *)data)->dentry;
347 	case FSNOTIFY_EVENT_FILE_RANGE:
348 		return file_range_path(data)->dentry;
349 	default:
350 		return NULL;
351 	}
352 }
353 
fsnotify_data_path(const void * data,int data_type)354 static inline const struct path *fsnotify_data_path(const void *data,
355 						    int data_type)
356 {
357 	switch (data_type) {
358 	case FSNOTIFY_EVENT_PATH:
359 		return data;
360 	case FSNOTIFY_EVENT_FILE_RANGE:
361 		return file_range_path(data);
362 	default:
363 		return NULL;
364 	}
365 }
366 
fsnotify_data_sb(const void * data,int data_type)367 static inline struct super_block *fsnotify_data_sb(const void *data,
368 						   int data_type)
369 {
370 	switch (data_type) {
371 	case FSNOTIFY_EVENT_INODE:
372 		return ((struct inode *)data)->i_sb;
373 	case FSNOTIFY_EVENT_DENTRY:
374 		return ((struct dentry *)data)->d_sb;
375 	case FSNOTIFY_EVENT_PATH:
376 		return ((const struct path *)data)->dentry->d_sb;
377 	case FSNOTIFY_EVENT_FILE_RANGE:
378 		return file_range_path(data)->dentry->d_sb;
379 	case FSNOTIFY_EVENT_ERROR:
380 		return ((struct fs_error_report *) data)->sb;
381 	default:
382 		return NULL;
383 	}
384 }
385 
fsnotify_data_error_report(const void * data,int data_type)386 static inline struct fs_error_report *fsnotify_data_error_report(
387 							const void *data,
388 							int data_type)
389 {
390 	switch (data_type) {
391 	case FSNOTIFY_EVENT_ERROR:
392 		return (struct fs_error_report *) data;
393 	default:
394 		return NULL;
395 	}
396 }
397 
fsnotify_data_file_range(const void * data,int data_type)398 static inline const struct file_range *fsnotify_data_file_range(
399 							const void *data,
400 							int data_type)
401 {
402 	switch (data_type) {
403 	case FSNOTIFY_EVENT_FILE_RANGE:
404 		return (struct file_range *)data;
405 	default:
406 		return NULL;
407 	}
408 }
409 
410 /*
411  * Index to merged marks iterator array that correlates to a type of watch.
412  * The type of watched object can be deduced from the iterator type, but not
413  * the other way around, because an event can match different watched objects
414  * of the same object type.
415  * For example, both parent and child are watching an object of type inode.
416  */
417 enum fsnotify_iter_type {
418 	FSNOTIFY_ITER_TYPE_INODE,
419 	FSNOTIFY_ITER_TYPE_VFSMOUNT,
420 	FSNOTIFY_ITER_TYPE_SB,
421 	FSNOTIFY_ITER_TYPE_PARENT,
422 	FSNOTIFY_ITER_TYPE_INODE2,
423 	FSNOTIFY_ITER_TYPE_COUNT
424 };
425 
426 /* The type of object that a mark is attached to */
427 enum fsnotify_obj_type {
428 	FSNOTIFY_OBJ_TYPE_ANY = -1,
429 	FSNOTIFY_OBJ_TYPE_INODE,
430 	FSNOTIFY_OBJ_TYPE_VFSMOUNT,
431 	FSNOTIFY_OBJ_TYPE_SB,
432 	FSNOTIFY_OBJ_TYPE_COUNT,
433 	FSNOTIFY_OBJ_TYPE_DETACHED = FSNOTIFY_OBJ_TYPE_COUNT
434 };
435 
fsnotify_valid_obj_type(unsigned int obj_type)436 static inline bool fsnotify_valid_obj_type(unsigned int obj_type)
437 {
438 	return (obj_type < FSNOTIFY_OBJ_TYPE_COUNT);
439 }
440 
441 struct fsnotify_iter_info {
442 	struct fsnotify_mark *marks[FSNOTIFY_ITER_TYPE_COUNT];
443 	struct fsnotify_group *current_group;
444 	unsigned int report_mask;
445 	int srcu_idx;
446 };
447 
fsnotify_iter_should_report_type(struct fsnotify_iter_info * iter_info,int iter_type)448 static inline bool fsnotify_iter_should_report_type(
449 		struct fsnotify_iter_info *iter_info, int iter_type)
450 {
451 	return (iter_info->report_mask & (1U << iter_type));
452 }
453 
fsnotify_iter_set_report_type(struct fsnotify_iter_info * iter_info,int iter_type)454 static inline void fsnotify_iter_set_report_type(
455 		struct fsnotify_iter_info *iter_info, int iter_type)
456 {
457 	iter_info->report_mask |= (1U << iter_type);
458 }
459 
fsnotify_iter_mark(struct fsnotify_iter_info * iter_info,int iter_type)460 static inline struct fsnotify_mark *fsnotify_iter_mark(
461 		struct fsnotify_iter_info *iter_info, int iter_type)
462 {
463 	if (fsnotify_iter_should_report_type(iter_info, iter_type))
464 		return iter_info->marks[iter_type];
465 	return NULL;
466 }
467 
fsnotify_iter_step(struct fsnotify_iter_info * iter,int type,struct fsnotify_mark ** markp)468 static inline int fsnotify_iter_step(struct fsnotify_iter_info *iter, int type,
469 				     struct fsnotify_mark **markp)
470 {
471 	while (type < FSNOTIFY_ITER_TYPE_COUNT) {
472 		*markp = fsnotify_iter_mark(iter, type);
473 		if (*markp)
474 			break;
475 		type++;
476 	}
477 	return type;
478 }
479 
480 #define FSNOTIFY_ITER_FUNCS(name, NAME) \
481 static inline struct fsnotify_mark *fsnotify_iter_##name##_mark( \
482 		struct fsnotify_iter_info *iter_info) \
483 { \
484 	return fsnotify_iter_mark(iter_info, FSNOTIFY_ITER_TYPE_##NAME); \
485 }
486 
487 FSNOTIFY_ITER_FUNCS(inode, INODE)
488 FSNOTIFY_ITER_FUNCS(parent, PARENT)
489 FSNOTIFY_ITER_FUNCS(vfsmount, VFSMOUNT)
490 FSNOTIFY_ITER_FUNCS(sb, SB)
491 
492 #define fsnotify_foreach_iter_type(type) \
493 	for (type = 0; type < FSNOTIFY_ITER_TYPE_COUNT; type++)
494 #define fsnotify_foreach_iter_mark_type(iter, mark, type) \
495 	for (type = 0; \
496 	     type = fsnotify_iter_step(iter, type, &mark), \
497 	     type < FSNOTIFY_ITER_TYPE_COUNT; \
498 	     type++)
499 
500 /*
501  * Inode/vfsmount/sb point to this structure which tracks all marks attached to
502  * the inode/vfsmount/sb. The reference to inode/vfsmount/sb is held by this
503  * structure. We destroy this structure when there are no more marks attached
504  * to it. The structure is protected by fsnotify_mark_srcu.
505  */
506 struct fsnotify_mark_connector {
507 	spinlock_t lock;
508 	unsigned char type;	/* Type of object [lock] */
509 	unsigned char prio;	/* Highest priority group */
510 #define FSNOTIFY_CONN_FLAG_IS_WATCHED	0x01
511 #define FSNOTIFY_CONN_FLAG_HAS_IREF	0x02
512 	unsigned short flags;	/* flags [lock] */
513 	union {
514 		/* Object pointer [lock] */
515 		void *obj;
516 		/* Used listing heads to free after srcu period expires */
517 		struct fsnotify_mark_connector *destroy_next;
518 	};
519 	struct hlist_head list;
520 };
521 
522 /*
523  * Container for per-sb fsnotify state (sb marks and more).
524  * Attached lazily on first marked object on the sb and freed when killing sb.
525  */
526 struct fsnotify_sb_info {
527 	struct fsnotify_mark_connector __rcu *sb_marks;
528 	/*
529 	 * Number of inode/mount/sb objects that are being watched in this sb.
530 	 * Note that inodes objects are currently double-accounted.
531 	 *
532 	 * The value in watched_objects[prio] is the number of objects that are
533 	 * watched by groups of priority >= prio, so watched_objects[0] is the
534 	 * total number of watched objects in this sb.
535 	 */
536 	atomic_long_t watched_objects[__FSNOTIFY_PRIO_NUM];
537 };
538 
fsnotify_sb_info(struct super_block * sb)539 static inline struct fsnotify_sb_info *fsnotify_sb_info(struct super_block *sb)
540 {
541 #ifdef CONFIG_FSNOTIFY
542 	return READ_ONCE(sb->s_fsnotify_info);
543 #else
544 	return NULL;
545 #endif
546 }
547 
fsnotify_sb_watched_objects(struct super_block * sb)548 static inline atomic_long_t *fsnotify_sb_watched_objects(struct super_block *sb)
549 {
550 	return &fsnotify_sb_info(sb)->watched_objects[0];
551 }
552 
553 /*
554  * A mark is simply an object attached to an in core inode which allows an
555  * fsnotify listener to indicate they are either no longer interested in events
556  * of a type matching mask or only interested in those events.
557  *
558  * These are flushed when an inode is evicted from core and may be flushed
559  * when the inode is modified (as seen by fsnotify_access).  Some fsnotify
560  * users (such as dnotify) will flush these when the open fd is closed and not
561  * at inode eviction or modification.
562  *
563  * Text in brackets is showing the lock(s) protecting modifications of a
564  * particular entry. obj_lock means either inode->i_lock or
565  * mnt->mnt_root->d_lock depending on the mark type.
566  */
567 struct fsnotify_mark {
568 	/* Mask this mark is for [mark->lock, group->mark_mutex] */
569 	__u32 mask;
570 	/* We hold one for presence in g_list. Also one ref for each 'thing'
571 	 * in kernel that found and may be using this mark. */
572 	refcount_t refcnt;
573 	/* Group this mark is for. Set on mark creation, stable until last ref
574 	 * is dropped */
575 	struct fsnotify_group *group;
576 	/* List of marks by group->marks_list. Also reused for queueing
577 	 * mark into destroy_list when it's waiting for the end of SRCU period
578 	 * before it can be freed. [group->mark_mutex] */
579 	struct list_head g_list;
580 	/* Protects inode / mnt pointers, flags, masks */
581 	spinlock_t lock;
582 	/* List of marks for inode / vfsmount [connector->lock, mark ref] */
583 	struct hlist_node obj_list;
584 	/* Head of list of marks for an object [mark ref] */
585 	struct fsnotify_mark_connector *connector;
586 	/* Events types and flags to ignore [mark->lock, group->mark_mutex] */
587 	__u32 ignore_mask;
588 	/* General fsnotify mark flags */
589 #define FSNOTIFY_MARK_FLAG_ALIVE		0x0001
590 #define FSNOTIFY_MARK_FLAG_ATTACHED		0x0002
591 	/* inotify mark flags */
592 #define FSNOTIFY_MARK_FLAG_EXCL_UNLINK		0x0010
593 #define FSNOTIFY_MARK_FLAG_IN_ONESHOT		0x0020
594 	/* fanotify mark flags */
595 #define FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY	0x0100
596 #define FSNOTIFY_MARK_FLAG_NO_IREF		0x0200
597 #define FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS	0x0400
598 #define FSNOTIFY_MARK_FLAG_HAS_FSID		0x0800
599 #define FSNOTIFY_MARK_FLAG_WEAK_FSID		0x1000
600 	unsigned int flags;		/* flags [mark->lock] */
601 };
602 
603 #ifdef CONFIG_FSNOTIFY
604 
605 /* called from the vfs helpers */
606 
607 /* main fsnotify call to send events */
608 extern int fsnotify(__u32 mask, const void *data, int data_type,
609 		    struct inode *dir, const struct qstr *name,
610 		    struct inode *inode, u32 cookie);
611 extern int __fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data,
612 			   int data_type);
613 extern void __fsnotify_inode_delete(struct inode *inode);
614 extern void __fsnotify_vfsmount_delete(struct vfsmount *mnt);
615 extern void fsnotify_sb_delete(struct super_block *sb);
616 extern void fsnotify_sb_free(struct super_block *sb);
617 extern u32 fsnotify_get_cookie(void);
618 
fsnotify_parent_needed_mask(__u32 mask)619 static inline __u32 fsnotify_parent_needed_mask(__u32 mask)
620 {
621 	/* FS_EVENT_ON_CHILD is set on marks that want parent/name info */
622 	if (!(mask & FS_EVENT_ON_CHILD))
623 		return 0;
624 	/*
625 	 * This object might be watched by a mark that cares about parent/name
626 	 * info, does it care about the specific set of events that can be
627 	 * reported with parent/name info?
628 	 */
629 	return mask & FS_EVENTS_POSS_TO_PARENT;
630 }
631 
fsnotify_inode_watches_children(struct inode * inode)632 static inline int fsnotify_inode_watches_children(struct inode *inode)
633 {
634 	__u32 parent_mask = READ_ONCE(inode->i_fsnotify_mask);
635 
636 	/* FS_EVENT_ON_CHILD is set if the inode may care */
637 	if (!(parent_mask & FS_EVENT_ON_CHILD))
638 		return 0;
639 	/* this inode might care about child events, does it care about the
640 	 * specific set of events that can happen on a child? */
641 	return parent_mask & FS_EVENTS_POSS_ON_CHILD;
642 }
643 
644 /*
645  * Update the dentry with a flag indicating the interest of its parent to receive
646  * filesystem events when those events happens to this dentry->d_inode.
647  */
fsnotify_update_flags(struct dentry * dentry)648 static inline void fsnotify_update_flags(struct dentry *dentry)
649 {
650 	assert_spin_locked(&dentry->d_lock);
651 
652 	/*
653 	 * Serialisation of setting PARENT_WATCHED on the dentries is provided
654 	 * by d_lock. If inotify_inode_watched changes after we have taken
655 	 * d_lock, the following fsnotify_set_children_dentry_flags call will
656 	 * find our entry, so it will spin until we complete here, and update
657 	 * us with the new state.
658 	 */
659 	if (fsnotify_inode_watches_children(dentry->d_parent->d_inode))
660 		dentry->d_flags |= DCACHE_FSNOTIFY_PARENT_WATCHED;
661 	else
662 		dentry->d_flags &= ~DCACHE_FSNOTIFY_PARENT_WATCHED;
663 }
664 
665 /* called from fsnotify listeners, such as fanotify or dnotify */
666 
667 /* create a new group */
668 extern struct fsnotify_group *fsnotify_alloc_group(
669 				const struct fsnotify_ops *ops,
670 				int flags);
671 /* get reference to a group */
672 extern void fsnotify_get_group(struct fsnotify_group *group);
673 /* drop reference on a group from fsnotify_alloc_group */
674 extern void fsnotify_put_group(struct fsnotify_group *group);
675 /* group destruction begins, stop queuing new events */
676 extern void fsnotify_group_stop_queueing(struct fsnotify_group *group);
677 /* destroy group */
678 extern void fsnotify_destroy_group(struct fsnotify_group *group);
679 /* fasync handler function */
680 extern int fsnotify_fasync(int fd, struct file *file, int on);
681 /* Free event from memory */
682 extern void fsnotify_destroy_event(struct fsnotify_group *group,
683 				   struct fsnotify_event *event);
684 /* attach the event to the group notification queue */
685 extern int fsnotify_insert_event(struct fsnotify_group *group,
686 				 struct fsnotify_event *event,
687 				 int (*merge)(struct fsnotify_group *,
688 					      struct fsnotify_event *),
689 				 void (*insert)(struct fsnotify_group *,
690 						struct fsnotify_event *));
691 
fsnotify_add_event(struct fsnotify_group * group,struct fsnotify_event * event,int (* merge)(struct fsnotify_group *,struct fsnotify_event *))692 static inline int fsnotify_add_event(struct fsnotify_group *group,
693 				     struct fsnotify_event *event,
694 				     int (*merge)(struct fsnotify_group *,
695 						  struct fsnotify_event *))
696 {
697 	return fsnotify_insert_event(group, event, merge, NULL);
698 }
699 
700 /* Queue overflow event to a notification group */
fsnotify_queue_overflow(struct fsnotify_group * group)701 static inline void fsnotify_queue_overflow(struct fsnotify_group *group)
702 {
703 	fsnotify_add_event(group, group->overflow_event, NULL);
704 }
705 
fsnotify_is_overflow_event(u32 mask)706 static inline bool fsnotify_is_overflow_event(u32 mask)
707 {
708 	return mask & FS_Q_OVERFLOW;
709 }
710 
fsnotify_notify_queue_is_empty(struct fsnotify_group * group)711 static inline bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group)
712 {
713 	assert_spin_locked(&group->notification_lock);
714 
715 	return list_empty(&group->notification_list);
716 }
717 
718 extern bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group);
719 /* return, but do not dequeue the first event on the notification queue */
720 extern struct fsnotify_event *fsnotify_peek_first_event(struct fsnotify_group *group);
721 /* return AND dequeue the first event on the notification queue */
722 extern struct fsnotify_event *fsnotify_remove_first_event(struct fsnotify_group *group);
723 /* Remove event queued in the notification list */
724 extern void fsnotify_remove_queued_event(struct fsnotify_group *group,
725 					 struct fsnotify_event *event);
726 
727 /* functions used to manipulate the marks attached to inodes */
728 
729 /*
730  * Canonical "ignore mask" including event flags.
731  *
732  * Note the subtle semantic difference from the legacy ->ignored_mask.
733  * ->ignored_mask traditionally only meant which events should be ignored,
734  * while ->ignore_mask also includes flags regarding the type of objects on
735  * which events should be ignored.
736  */
fsnotify_ignore_mask(struct fsnotify_mark * mark)737 static inline __u32 fsnotify_ignore_mask(struct fsnotify_mark *mark)
738 {
739 	__u32 ignore_mask = mark->ignore_mask;
740 
741 	/* The event flags in ignore mask take effect */
742 	if (mark->flags & FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS)
743 		return ignore_mask;
744 
745 	/*
746 	 * Legacy behavior:
747 	 * - Always ignore events on dir
748 	 * - Ignore events on child if parent is watching children
749 	 */
750 	ignore_mask |= FS_ISDIR;
751 	ignore_mask &= ~FS_EVENT_ON_CHILD;
752 	ignore_mask |= mark->mask & FS_EVENT_ON_CHILD;
753 
754 	return ignore_mask;
755 }
756 
757 /* Legacy ignored_mask - only event types to ignore */
fsnotify_ignored_events(struct fsnotify_mark * mark)758 static inline __u32 fsnotify_ignored_events(struct fsnotify_mark *mark)
759 {
760 	return mark->ignore_mask & ALL_FSNOTIFY_EVENTS;
761 }
762 
763 /*
764  * Check if mask (or ignore mask) should be applied depending if victim is a
765  * directory and whether it is reported to a watching parent.
766  */
fsnotify_mask_applicable(__u32 mask,bool is_dir,int iter_type)767 static inline bool fsnotify_mask_applicable(__u32 mask, bool is_dir,
768 					    int iter_type)
769 {
770 	/* Should mask be applied to a directory? */
771 	if (is_dir && !(mask & FS_ISDIR))
772 		return false;
773 
774 	/* Should mask be applied to a child? */
775 	if (iter_type == FSNOTIFY_ITER_TYPE_PARENT &&
776 	    !(mask & FS_EVENT_ON_CHILD))
777 		return false;
778 
779 	return true;
780 }
781 
782 /*
783  * Effective ignore mask taking into account if event victim is a
784  * directory and whether it is reported to a watching parent.
785  */
fsnotify_effective_ignore_mask(struct fsnotify_mark * mark,bool is_dir,int iter_type)786 static inline __u32 fsnotify_effective_ignore_mask(struct fsnotify_mark *mark,
787 						   bool is_dir, int iter_type)
788 {
789 	__u32 ignore_mask = fsnotify_ignored_events(mark);
790 
791 	if (!ignore_mask)
792 		return 0;
793 
794 	/* For non-dir and non-child, no need to consult the event flags */
795 	if (!is_dir && iter_type != FSNOTIFY_ITER_TYPE_PARENT)
796 		return ignore_mask;
797 
798 	ignore_mask = fsnotify_ignore_mask(mark);
799 	if (!fsnotify_mask_applicable(ignore_mask, is_dir, iter_type))
800 		return 0;
801 
802 	return ignore_mask & ALL_FSNOTIFY_EVENTS;
803 }
804 
805 /* Get mask for calculating object interest taking ignore mask into account */
fsnotify_calc_mask(struct fsnotify_mark * mark)806 static inline __u32 fsnotify_calc_mask(struct fsnotify_mark *mark)
807 {
808 	__u32 mask = mark->mask;
809 
810 	if (!fsnotify_ignored_events(mark))
811 		return mask;
812 
813 	/* Interest in FS_MODIFY may be needed for clearing ignore mask */
814 	if (!(mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY))
815 		mask |= FS_MODIFY;
816 
817 	/*
818 	 * If mark is interested in ignoring events on children, the object must
819 	 * show interest in those events for fsnotify_parent() to notice it.
820 	 */
821 	return mask | mark->ignore_mask;
822 }
823 
824 /* Get mask of events for a list of marks */
825 extern __u32 fsnotify_conn_mask(struct fsnotify_mark_connector *conn);
826 /* Calculate mask of events for a list of marks */
827 extern void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn);
828 extern void fsnotify_init_mark(struct fsnotify_mark *mark,
829 			       struct fsnotify_group *group);
830 /* Find mark belonging to given group in the list of marks */
831 struct fsnotify_mark *fsnotify_find_mark(void *obj, unsigned int obj_type,
832 					 struct fsnotify_group *group);
833 /* attach the mark to the object */
834 int fsnotify_add_mark(struct fsnotify_mark *mark, void *obj,
835 		      unsigned int obj_type, int add_flags);
836 int fsnotify_add_mark_locked(struct fsnotify_mark *mark, void *obj,
837 			     unsigned int obj_type, int add_flags);
838 
839 /* attach the mark to the inode */
fsnotify_add_inode_mark(struct fsnotify_mark * mark,struct inode * inode,int add_flags)840 static inline int fsnotify_add_inode_mark(struct fsnotify_mark *mark,
841 					  struct inode *inode,
842 					  int add_flags)
843 {
844 	return fsnotify_add_mark(mark, inode, FSNOTIFY_OBJ_TYPE_INODE,
845 				 add_flags);
846 }
fsnotify_add_inode_mark_locked(struct fsnotify_mark * mark,struct inode * inode,int add_flags)847 static inline int fsnotify_add_inode_mark_locked(struct fsnotify_mark *mark,
848 						 struct inode *inode,
849 						 int add_flags)
850 {
851 	return fsnotify_add_mark_locked(mark, inode, FSNOTIFY_OBJ_TYPE_INODE,
852 					add_flags);
853 }
854 
fsnotify_find_inode_mark(struct inode * inode,struct fsnotify_group * group)855 static inline struct fsnotify_mark *fsnotify_find_inode_mark(
856 						struct inode *inode,
857 						struct fsnotify_group *group)
858 {
859 	return fsnotify_find_mark(inode, FSNOTIFY_OBJ_TYPE_INODE, group);
860 }
861 
862 /* given a group and a mark, flag mark to be freed when all references are dropped */
863 extern void fsnotify_destroy_mark(struct fsnotify_mark *mark,
864 				  struct fsnotify_group *group);
865 /* detach mark from inode / mount list, group list, drop inode reference */
866 extern void fsnotify_detach_mark(struct fsnotify_mark *mark);
867 /* free mark */
868 extern void fsnotify_free_mark(struct fsnotify_mark *mark);
869 /* Wait until all marks queued for destruction are destroyed */
870 extern void fsnotify_wait_marks_destroyed(void);
871 /* Clear all of the marks of a group attached to a given object type */
872 extern void fsnotify_clear_marks_by_group(struct fsnotify_group *group,
873 					  unsigned int obj_type);
874 /* run all the marks in a group, and clear all of the vfsmount marks */
fsnotify_clear_vfsmount_marks_by_group(struct fsnotify_group * group)875 static inline void fsnotify_clear_vfsmount_marks_by_group(struct fsnotify_group *group)
876 {
877 	fsnotify_clear_marks_by_group(group, FSNOTIFY_OBJ_TYPE_VFSMOUNT);
878 }
879 /* run all the marks in a group, and clear all of the inode marks */
fsnotify_clear_inode_marks_by_group(struct fsnotify_group * group)880 static inline void fsnotify_clear_inode_marks_by_group(struct fsnotify_group *group)
881 {
882 	fsnotify_clear_marks_by_group(group, FSNOTIFY_OBJ_TYPE_INODE);
883 }
884 /* run all the marks in a group, and clear all of the sn marks */
fsnotify_clear_sb_marks_by_group(struct fsnotify_group * group)885 static inline void fsnotify_clear_sb_marks_by_group(struct fsnotify_group *group)
886 {
887 	fsnotify_clear_marks_by_group(group, FSNOTIFY_OBJ_TYPE_SB);
888 }
889 extern void fsnotify_get_mark(struct fsnotify_mark *mark);
890 extern void fsnotify_put_mark(struct fsnotify_mark *mark);
891 extern void fsnotify_finish_user_wait(struct fsnotify_iter_info *iter_info);
892 extern bool fsnotify_prepare_user_wait(struct fsnotify_iter_info *iter_info);
893 
fsnotify_init_event(struct fsnotify_event * event)894 static inline void fsnotify_init_event(struct fsnotify_event *event)
895 {
896 	INIT_LIST_HEAD(&event->list);
897 }
898 int fsnotify_pre_content(const struct path *path, const loff_t *ppos,
899 			 size_t count);
900 
901 #else
902 
fsnotify_pre_content(const struct path * path,const loff_t * ppos,size_t count)903 static inline int fsnotify_pre_content(const struct path *path,
904 				       const loff_t *ppos, size_t count)
905 {
906 	return 0;
907 }
908 
fsnotify(__u32 mask,const void * data,int data_type,struct inode * dir,const struct qstr * name,struct inode * inode,u32 cookie)909 static inline int fsnotify(__u32 mask, const void *data, int data_type,
910 			   struct inode *dir, const struct qstr *name,
911 			   struct inode *inode, u32 cookie)
912 {
913 	return 0;
914 }
915 
__fsnotify_parent(struct dentry * dentry,__u32 mask,const void * data,int data_type)916 static inline int __fsnotify_parent(struct dentry *dentry, __u32 mask,
917 				  const void *data, int data_type)
918 {
919 	return 0;
920 }
921 
__fsnotify_inode_delete(struct inode * inode)922 static inline void __fsnotify_inode_delete(struct inode *inode)
923 {}
924 
__fsnotify_vfsmount_delete(struct vfsmount * mnt)925 static inline void __fsnotify_vfsmount_delete(struct vfsmount *mnt)
926 {}
927 
fsnotify_sb_delete(struct super_block * sb)928 static inline void fsnotify_sb_delete(struct super_block *sb)
929 {}
930 
fsnotify_sb_free(struct super_block * sb)931 static inline void fsnotify_sb_free(struct super_block *sb)
932 {}
933 
fsnotify_update_flags(struct dentry * dentry)934 static inline void fsnotify_update_flags(struct dentry *dentry)
935 {}
936 
fsnotify_get_cookie(void)937 static inline u32 fsnotify_get_cookie(void)
938 {
939 	return 0;
940 }
941 
fsnotify_unmount_inodes(struct super_block * sb)942 static inline void fsnotify_unmount_inodes(struct super_block *sb)
943 {}
944 
945 #endif	/* CONFIG_FSNOTIFY */
946 
947 #endif	/* __KERNEL __ */
948 
949 #endif	/* __LINUX_FSNOTIFY_BACKEND_H */
950