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