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 /* Writtable file was closed */
35 #define FS_CLOSE_NOWRITE 0x00000010 /* Unwrittable 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 * A group is a "thing" that wants to receive notification about filesystem
181 * events. The mask holds the subset of event types this group cares about.
182 * refcnt on a group is up to the implementor and at any moment if it goes 0
183 * everything will be cleaned up.
184 */
185 struct fsnotify_group {
186 const struct fsnotify_ops *ops; /* how this group handles things */
187
188 /*
189 * How the refcnt is used is up to each group. When the refcnt hits 0
190 * fsnotify will clean up all of the resources associated with this group.
191 * As an example, the dnotify group will always have a refcnt=1 and that
192 * will never change. Inotify, on the other hand, has a group per
193 * inotify_init() and the refcnt will hit 0 only when that fd has been
194 * closed.
195 */
196 refcount_t refcnt; /* things with interest in this group */
197
198 /* needed to send notification to userspace */
199 spinlock_t notification_lock; /* protect the notification_list */
200 struct list_head notification_list; /* list of event_holder this group needs to send to userspace */
201 wait_queue_head_t notification_waitq; /* read() on the notification file blocks on this waitq */
202 unsigned int q_len; /* events on the queue */
203 unsigned int max_events; /* maximum events allowed on the list */
204 /*
205 * Valid fsnotify group priorities. Events are send in order from highest
206 * priority to lowest priority. We default to the lowest priority.
207 */
208 #define FS_PRIO_0 0 /* normal notifiers, no permissions */
209 #define FS_PRIO_1 1 /* fanotify content based access control */
210 #define FS_PRIO_2 2 /* fanotify pre-content access */
211 unsigned int priority;
212 bool shutdown; /* group is being shut down, don't queue more events */
213
214 #define FSNOTIFY_GROUP_USER 0x01 /* user allocated group */
215 #define FSNOTIFY_GROUP_DUPS 0x02 /* allow multiple marks per object */
216 #define FSNOTIFY_GROUP_NOFS 0x04 /* group lock is not direct reclaim safe */
217 int flags;
218 unsigned int owner_flags; /* stored flags of mark_mutex owner */
219
220 /* stores all fastpath marks assoc with this group so they can be cleaned on unregister */
221 struct mutex mark_mutex; /* protect marks_list */
222 atomic_t user_waits; /* Number of tasks waiting for user
223 * response */
224 struct list_head marks_list; /* all inode marks for this group */
225
226 struct fasync_struct *fsn_fa; /* async notification */
227
228 struct fsnotify_event *overflow_event; /* Event we queue when the
229 * notification list is too
230 * full */
231
232 struct mem_cgroup *memcg; /* memcg to charge allocations */
233
234 /* groups can define private fields here or use the void *private */
235 union {
236 void *private;
237 #ifdef CONFIG_INOTIFY_USER
238 struct inotify_group_private_data {
239 spinlock_t idr_lock;
240 struct idr idr;
241 struct ucounts *ucounts;
242 } inotify_data;
243 #endif
244 #ifdef CONFIG_FANOTIFY
245 struct fanotify_group_private_data {
246 /* Hash table of events for merge */
247 struct hlist_head *merge_hash;
248 /* allows a group to block waiting for a userspace response */
249 struct list_head access_list;
250 wait_queue_head_t access_waitq;
251 int flags; /* flags from fanotify_init() */
252 int f_flags; /* event_f_flags from fanotify_init() */
253 struct ucounts *ucounts;
254 mempool_t error_events_pool;
255 } fanotify_data;
256 #endif /* CONFIG_FANOTIFY */
257 };
258 };
259
260 /*
261 * These helpers are used to prevent deadlock when reclaiming inodes with
262 * evictable marks of the same group that is allocating a new mark.
263 */
fsnotify_group_lock(struct fsnotify_group * group)264 static inline void fsnotify_group_lock(struct fsnotify_group *group)
265 {
266 mutex_lock(&group->mark_mutex);
267 if (group->flags & FSNOTIFY_GROUP_NOFS)
268 group->owner_flags = memalloc_nofs_save();
269 }
270
fsnotify_group_unlock(struct fsnotify_group * group)271 static inline void fsnotify_group_unlock(struct fsnotify_group *group)
272 {
273 if (group->flags & FSNOTIFY_GROUP_NOFS)
274 memalloc_nofs_restore(group->owner_flags);
275 mutex_unlock(&group->mark_mutex);
276 }
277
fsnotify_group_assert_locked(struct fsnotify_group * group)278 static inline void fsnotify_group_assert_locked(struct fsnotify_group *group)
279 {
280 WARN_ON_ONCE(!mutex_is_locked(&group->mark_mutex));
281 if (group->flags & FSNOTIFY_GROUP_NOFS)
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 * fsnotify_connp_t is what we embed in objects which connector can be attached
461 * to. fsnotify_connp_t * is how we refer from connector back to object.
462 */
463 struct fsnotify_mark_connector;
464 typedef struct fsnotify_mark_connector __rcu *fsnotify_connp_t;
465
466 /*
467 * Inode/vfsmount/sb point to this structure which tracks all marks attached to
468 * the inode/vfsmount/sb. The reference to inode/vfsmount/sb is held by this
469 * structure. We destroy this structure when there are no more marks attached
470 * to it. The structure is protected by fsnotify_mark_srcu.
471 */
472 struct fsnotify_mark_connector {
473 spinlock_t lock;
474 unsigned short type; /* Type of object [lock] */
475 #define FSNOTIFY_CONN_FLAG_HAS_FSID 0x01
476 #define FSNOTIFY_CONN_FLAG_HAS_IREF 0x02
477 unsigned short flags; /* flags [lock] */
478 __kernel_fsid_t fsid; /* fsid of filesystem containing object */
479 union {
480 /* Object pointer [lock] */
481 fsnotify_connp_t *obj;
482 /* Used listing heads to free after srcu period expires */
483 struct fsnotify_mark_connector *destroy_next;
484 };
485 struct hlist_head list;
486 };
487
488 /*
489 * A mark is simply an object attached to an in core inode which allows an
490 * fsnotify listener to indicate they are either no longer interested in events
491 * of a type matching mask or only interested in those events.
492 *
493 * These are flushed when an inode is evicted from core and may be flushed
494 * when the inode is modified (as seen by fsnotify_access). Some fsnotify
495 * users (such as dnotify) will flush these when the open fd is closed and not
496 * at inode eviction or modification.
497 *
498 * Text in brackets is showing the lock(s) protecting modifications of a
499 * particular entry. obj_lock means either inode->i_lock or
500 * mnt->mnt_root->d_lock depending on the mark type.
501 */
502 struct fsnotify_mark {
503 /* Mask this mark is for [mark->lock, group->mark_mutex] */
504 __u32 mask;
505 /* We hold one for presence in g_list. Also one ref for each 'thing'
506 * in kernel that found and may be using this mark. */
507 refcount_t refcnt;
508 /* Group this mark is for. Set on mark creation, stable until last ref
509 * is dropped */
510 struct fsnotify_group *group;
511 /* List of marks by group->marks_list. Also reused for queueing
512 * mark into destroy_list when it's waiting for the end of SRCU period
513 * before it can be freed. [group->mark_mutex] */
514 struct list_head g_list;
515 /* Protects inode / mnt pointers, flags, masks */
516 spinlock_t lock;
517 /* List of marks for inode / vfsmount [connector->lock, mark ref] */
518 struct hlist_node obj_list;
519 /* Head of list of marks for an object [mark ref] */
520 struct fsnotify_mark_connector *connector;
521 /* Events types and flags to ignore [mark->lock, group->mark_mutex] */
522 __u32 ignore_mask;
523 /* General fsnotify mark flags */
524 #define FSNOTIFY_MARK_FLAG_ALIVE 0x0001
525 #define FSNOTIFY_MARK_FLAG_ATTACHED 0x0002
526 /* inotify mark flags */
527 #define FSNOTIFY_MARK_FLAG_EXCL_UNLINK 0x0010
528 #define FSNOTIFY_MARK_FLAG_IN_ONESHOT 0x0020
529 /* fanotify mark flags */
530 #define FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY 0x0100
531 #define FSNOTIFY_MARK_FLAG_NO_IREF 0x0200
532 #define FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS 0x0400
533 unsigned int flags; /* flags [mark->lock] */
534 };
535
536 #ifdef CONFIG_FSNOTIFY
537
538 /* called from the vfs helpers */
539
540 /* main fsnotify call to send events */
541 extern int fsnotify(__u32 mask, const void *data, int data_type,
542 struct inode *dir, const struct qstr *name,
543 struct inode *inode, u32 cookie);
544 extern int __fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data,
545 int data_type);
546 extern void __fsnotify_inode_delete(struct inode *inode);
547 extern void __fsnotify_vfsmount_delete(struct vfsmount *mnt);
548 extern void fsnotify_sb_delete(struct super_block *sb);
549 extern u32 fsnotify_get_cookie(void);
550
fsnotify_parent_needed_mask(__u32 mask)551 static inline __u32 fsnotify_parent_needed_mask(__u32 mask)
552 {
553 /* FS_EVENT_ON_CHILD is set on marks that want parent/name info */
554 if (!(mask & FS_EVENT_ON_CHILD))
555 return 0;
556 /*
557 * This object might be watched by a mark that cares about parent/name
558 * info, does it care about the specific set of events that can be
559 * reported with parent/name info?
560 */
561 return mask & FS_EVENTS_POSS_TO_PARENT;
562 }
563
fsnotify_inode_watches_children(struct inode * inode)564 static inline int fsnotify_inode_watches_children(struct inode *inode)
565 {
566 __u32 parent_mask = READ_ONCE(inode->i_fsnotify_mask);
567
568 /* FS_EVENT_ON_CHILD is set if the inode may care */
569 if (!(parent_mask & FS_EVENT_ON_CHILD))
570 return 0;
571 /* this inode might care about child events, does it care about the
572 * specific set of events that can happen on a child? */
573 return parent_mask & FS_EVENTS_POSS_ON_CHILD;
574 }
575
576 /*
577 * Update the dentry with a flag indicating the interest of its parent to receive
578 * filesystem events when those events happens to this dentry->d_inode.
579 */
fsnotify_update_flags(struct dentry * dentry)580 static inline void fsnotify_update_flags(struct dentry *dentry)
581 {
582 assert_spin_locked(&dentry->d_lock);
583
584 /*
585 * Serialisation of setting PARENT_WATCHED on the dentries is provided
586 * by d_lock. If inotify_inode_watched changes after we have taken
587 * d_lock, the following fsnotify_set_children_dentry_flags call will
588 * find our entry, so it will spin until we complete here, and update
589 * us with the new state.
590 */
591 if (fsnotify_inode_watches_children(dentry->d_parent->d_inode))
592 dentry->d_flags |= DCACHE_FSNOTIFY_PARENT_WATCHED;
593 else
594 dentry->d_flags &= ~DCACHE_FSNOTIFY_PARENT_WATCHED;
595 }
596
597 /* called from fsnotify listeners, such as fanotify or dnotify */
598
599 /* create a new group */
600 extern struct fsnotify_group *fsnotify_alloc_group(
601 const struct fsnotify_ops *ops,
602 int flags);
603 /* get reference to a group */
604 extern void fsnotify_get_group(struct fsnotify_group *group);
605 /* drop reference on a group from fsnotify_alloc_group */
606 extern void fsnotify_put_group(struct fsnotify_group *group);
607 /* group destruction begins, stop queuing new events */
608 extern void fsnotify_group_stop_queueing(struct fsnotify_group *group);
609 /* destroy group */
610 extern void fsnotify_destroy_group(struct fsnotify_group *group);
611 /* fasync handler function */
612 extern int fsnotify_fasync(int fd, struct file *file, int on);
613 /* Free event from memory */
614 extern void fsnotify_destroy_event(struct fsnotify_group *group,
615 struct fsnotify_event *event);
616 /* attach the event to the group notification queue */
617 extern int fsnotify_insert_event(struct fsnotify_group *group,
618 struct fsnotify_event *event,
619 int (*merge)(struct fsnotify_group *,
620 struct fsnotify_event *),
621 void (*insert)(struct fsnotify_group *,
622 struct fsnotify_event *));
623
fsnotify_add_event(struct fsnotify_group * group,struct fsnotify_event * event,int (* merge)(struct fsnotify_group *,struct fsnotify_event *))624 static inline int fsnotify_add_event(struct fsnotify_group *group,
625 struct fsnotify_event *event,
626 int (*merge)(struct fsnotify_group *,
627 struct fsnotify_event *))
628 {
629 return fsnotify_insert_event(group, event, merge, NULL);
630 }
631
632 /* Queue overflow event to a notification group */
fsnotify_queue_overflow(struct fsnotify_group * group)633 static inline void fsnotify_queue_overflow(struct fsnotify_group *group)
634 {
635 fsnotify_add_event(group, group->overflow_event, NULL);
636 }
637
fsnotify_is_overflow_event(u32 mask)638 static inline bool fsnotify_is_overflow_event(u32 mask)
639 {
640 return mask & FS_Q_OVERFLOW;
641 }
642
fsnotify_notify_queue_is_empty(struct fsnotify_group * group)643 static inline bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group)
644 {
645 assert_spin_locked(&group->notification_lock);
646
647 return list_empty(&group->notification_list);
648 }
649
650 extern bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group);
651 /* return, but do not dequeue the first event on the notification queue */
652 extern struct fsnotify_event *fsnotify_peek_first_event(struct fsnotify_group *group);
653 /* return AND dequeue the first event on the notification queue */
654 extern struct fsnotify_event *fsnotify_remove_first_event(struct fsnotify_group *group);
655 /* Remove event queued in the notification list */
656 extern void fsnotify_remove_queued_event(struct fsnotify_group *group,
657 struct fsnotify_event *event);
658
659 /* functions used to manipulate the marks attached to inodes */
660
661 /*
662 * Canonical "ignore mask" including event flags.
663 *
664 * Note the subtle semantic difference from the legacy ->ignored_mask.
665 * ->ignored_mask traditionally only meant which events should be ignored,
666 * while ->ignore_mask also includes flags regarding the type of objects on
667 * which events should be ignored.
668 */
fsnotify_ignore_mask(struct fsnotify_mark * mark)669 static inline __u32 fsnotify_ignore_mask(struct fsnotify_mark *mark)
670 {
671 __u32 ignore_mask = mark->ignore_mask;
672
673 /* The event flags in ignore mask take effect */
674 if (mark->flags & FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS)
675 return ignore_mask;
676
677 /*
678 * Legacy behavior:
679 * - Always ignore events on dir
680 * - Ignore events on child if parent is watching children
681 */
682 ignore_mask |= FS_ISDIR;
683 ignore_mask &= ~FS_EVENT_ON_CHILD;
684 ignore_mask |= mark->mask & FS_EVENT_ON_CHILD;
685
686 return ignore_mask;
687 }
688
689 /* Legacy ignored_mask - only event types to ignore */
fsnotify_ignored_events(struct fsnotify_mark * mark)690 static inline __u32 fsnotify_ignored_events(struct fsnotify_mark *mark)
691 {
692 return mark->ignore_mask & ALL_FSNOTIFY_EVENTS;
693 }
694
695 /*
696 * Check if mask (or ignore mask) should be applied depending if victim is a
697 * directory and whether it is reported to a watching parent.
698 */
fsnotify_mask_applicable(__u32 mask,bool is_dir,int iter_type)699 static inline bool fsnotify_mask_applicable(__u32 mask, bool is_dir,
700 int iter_type)
701 {
702 /* Should mask be applied to a directory? */
703 if (is_dir && !(mask & FS_ISDIR))
704 return false;
705
706 /* Should mask be applied to a child? */
707 if (iter_type == FSNOTIFY_ITER_TYPE_PARENT &&
708 !(mask & FS_EVENT_ON_CHILD))
709 return false;
710
711 return true;
712 }
713
714 /*
715 * Effective ignore mask taking into account if event victim is a
716 * directory and whether it is reported to a watching parent.
717 */
fsnotify_effective_ignore_mask(struct fsnotify_mark * mark,bool is_dir,int iter_type)718 static inline __u32 fsnotify_effective_ignore_mask(struct fsnotify_mark *mark,
719 bool is_dir, int iter_type)
720 {
721 __u32 ignore_mask = fsnotify_ignored_events(mark);
722
723 if (!ignore_mask)
724 return 0;
725
726 /* For non-dir and non-child, no need to consult the event flags */
727 if (!is_dir && iter_type != FSNOTIFY_ITER_TYPE_PARENT)
728 return ignore_mask;
729
730 ignore_mask = fsnotify_ignore_mask(mark);
731 if (!fsnotify_mask_applicable(ignore_mask, is_dir, iter_type))
732 return 0;
733
734 return ignore_mask & ALL_FSNOTIFY_EVENTS;
735 }
736
737 /* Get mask for calculating object interest taking ignore mask into account */
fsnotify_calc_mask(struct fsnotify_mark * mark)738 static inline __u32 fsnotify_calc_mask(struct fsnotify_mark *mark)
739 {
740 __u32 mask = mark->mask;
741
742 if (!fsnotify_ignored_events(mark))
743 return mask;
744
745 /* Interest in FS_MODIFY may be needed for clearing ignore mask */
746 if (!(mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY))
747 mask |= FS_MODIFY;
748
749 /*
750 * If mark is interested in ignoring events on children, the object must
751 * show interest in those events for fsnotify_parent() to notice it.
752 */
753 return mask | mark->ignore_mask;
754 }
755
756 /* Get mask of events for a list of marks */
757 extern __u32 fsnotify_conn_mask(struct fsnotify_mark_connector *conn);
758 /* Calculate mask of events for a list of marks */
759 extern void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn);
760 extern void fsnotify_init_mark(struct fsnotify_mark *mark,
761 struct fsnotify_group *group);
762 /* Find mark belonging to given group in the list of marks */
763 extern struct fsnotify_mark *fsnotify_find_mark(fsnotify_connp_t *connp,
764 struct fsnotify_group *group);
765 /* attach the mark to the object */
766 extern int fsnotify_add_mark(struct fsnotify_mark *mark,
767 fsnotify_connp_t *connp, unsigned int obj_type,
768 int add_flags, __kernel_fsid_t *fsid);
769 extern int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
770 fsnotify_connp_t *connp,
771 unsigned int obj_type, int add_flags,
772 __kernel_fsid_t *fsid);
773
774 /* attach the mark to the inode */
fsnotify_add_inode_mark(struct fsnotify_mark * mark,struct inode * inode,int add_flags)775 static inline int fsnotify_add_inode_mark(struct fsnotify_mark *mark,
776 struct inode *inode,
777 int add_flags)
778 {
779 return fsnotify_add_mark(mark, &inode->i_fsnotify_marks,
780 FSNOTIFY_OBJ_TYPE_INODE, add_flags, NULL);
781 }
fsnotify_add_inode_mark_locked(struct fsnotify_mark * mark,struct inode * inode,int add_flags)782 static inline int fsnotify_add_inode_mark_locked(struct fsnotify_mark *mark,
783 struct inode *inode,
784 int add_flags)
785 {
786 return fsnotify_add_mark_locked(mark, &inode->i_fsnotify_marks,
787 FSNOTIFY_OBJ_TYPE_INODE, add_flags,
788 NULL);
789 }
790
791 /* given a group and a mark, flag mark to be freed when all references are dropped */
792 extern void fsnotify_destroy_mark(struct fsnotify_mark *mark,
793 struct fsnotify_group *group);
794 /* detach mark from inode / mount list, group list, drop inode reference */
795 extern void fsnotify_detach_mark(struct fsnotify_mark *mark);
796 /* free mark */
797 extern void fsnotify_free_mark(struct fsnotify_mark *mark);
798 /* Wait until all marks queued for destruction are destroyed */
799 extern void fsnotify_wait_marks_destroyed(void);
800 /* Clear all of the marks of a group attached to a given object type */
801 extern void fsnotify_clear_marks_by_group(struct fsnotify_group *group,
802 unsigned int obj_type);
803 /* run all the marks in a group, and clear all of the vfsmount marks */
fsnotify_clear_vfsmount_marks_by_group(struct fsnotify_group * group)804 static inline void fsnotify_clear_vfsmount_marks_by_group(struct fsnotify_group *group)
805 {
806 fsnotify_clear_marks_by_group(group, FSNOTIFY_OBJ_TYPE_VFSMOUNT);
807 }
808 /* run all the marks in a group, and clear all of the inode marks */
fsnotify_clear_inode_marks_by_group(struct fsnotify_group * group)809 static inline void fsnotify_clear_inode_marks_by_group(struct fsnotify_group *group)
810 {
811 fsnotify_clear_marks_by_group(group, FSNOTIFY_OBJ_TYPE_INODE);
812 }
813 /* run all the marks in a group, and clear all of the sn marks */
fsnotify_clear_sb_marks_by_group(struct fsnotify_group * group)814 static inline void fsnotify_clear_sb_marks_by_group(struct fsnotify_group *group)
815 {
816 fsnotify_clear_marks_by_group(group, FSNOTIFY_OBJ_TYPE_SB);
817 }
818 extern void fsnotify_get_mark(struct fsnotify_mark *mark);
819 extern void fsnotify_put_mark(struct fsnotify_mark *mark);
820 extern void fsnotify_finish_user_wait(struct fsnotify_iter_info *iter_info);
821 extern bool fsnotify_prepare_user_wait(struct fsnotify_iter_info *iter_info);
822
fsnotify_init_event(struct fsnotify_event * event)823 static inline void fsnotify_init_event(struct fsnotify_event *event)
824 {
825 INIT_LIST_HEAD(&event->list);
826 }
827
828 #else
829
fsnotify(__u32 mask,const void * data,int data_type,struct inode * dir,const struct qstr * name,struct inode * inode,u32 cookie)830 static inline int fsnotify(__u32 mask, const void *data, int data_type,
831 struct inode *dir, const struct qstr *name,
832 struct inode *inode, u32 cookie)
833 {
834 return 0;
835 }
836
__fsnotify_parent(struct dentry * dentry,__u32 mask,const void * data,int data_type)837 static inline int __fsnotify_parent(struct dentry *dentry, __u32 mask,
838 const void *data, int data_type)
839 {
840 return 0;
841 }
842
__fsnotify_inode_delete(struct inode * inode)843 static inline void __fsnotify_inode_delete(struct inode *inode)
844 {}
845
__fsnotify_vfsmount_delete(struct vfsmount * mnt)846 static inline void __fsnotify_vfsmount_delete(struct vfsmount *mnt)
847 {}
848
fsnotify_sb_delete(struct super_block * sb)849 static inline void fsnotify_sb_delete(struct super_block *sb)
850 {}
851
fsnotify_update_flags(struct dentry * dentry)852 static inline void fsnotify_update_flags(struct dentry *dentry)
853 {}
854
fsnotify_get_cookie(void)855 static inline u32 fsnotify_get_cookie(void)
856 {
857 return 0;
858 }
859
fsnotify_unmount_inodes(struct super_block * sb)860 static inline void fsnotify_unmount_inodes(struct super_block *sb)
861 {}
862
863 #endif /* CONFIG_FSNOTIFY */
864
865 #endif /* __KERNEL __ */
866
867 #endif /* __LINUX_FSNOTIFY_BACKEND_H */
868