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