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 129 struct fsnotify_group; 130 struct fsnotify_event; 131 struct fsnotify_mark; 132 struct fsnotify_event_private_data; 133 struct fsnotify_fname; 134 struct fsnotify_iter_info; 135 136 struct 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 */ 172 struct 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 */ 192 struct 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 */ 200 enum 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 */ 213 struct 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 struct user_namespace *user_ns; /* user ns where group was created */ 254 255 /* groups can define private fields here or use the void *private */ 256 union { 257 void *private; 258 #ifdef CONFIG_INOTIFY_USER 259 struct inotify_group_private_data { 260 spinlock_t idr_lock; 261 struct idr idr; 262 struct ucounts *ucounts; 263 } inotify_data; 264 #endif 265 #ifdef CONFIG_FANOTIFY 266 struct fanotify_group_private_data { 267 /* Hash table of events for merge */ 268 struct hlist_head *merge_hash; 269 /* allows a group to block waiting for a userspace response */ 270 struct list_head access_list; 271 wait_queue_head_t access_waitq; 272 int flags; /* flags from fanotify_init() */ 273 int f_flags; /* event_f_flags from fanotify_init() */ 274 struct ucounts *ucounts; 275 mempool_t error_events_pool; 276 /* chained on perm_group_list */ 277 struct list_head perm_grp_list; 278 } fanotify_data; 279 #endif /* CONFIG_FANOTIFY */ 280 }; 281 }; 282 283 /* 284 * These helpers are used to prevent deadlock when reclaiming inodes with 285 * evictable marks of the same group that is allocating a new mark. 286 */ 287 static inline void fsnotify_group_lock(struct fsnotify_group *group) 288 { 289 mutex_lock(&group->mark_mutex); 290 group->owner_flags = memalloc_nofs_save(); 291 } 292 293 static inline void fsnotify_group_unlock(struct fsnotify_group *group) 294 { 295 memalloc_nofs_restore(group->owner_flags); 296 mutex_unlock(&group->mark_mutex); 297 } 298 299 static inline void fsnotify_group_assert_locked(struct fsnotify_group *group) 300 { 301 WARN_ON_ONCE(!mutex_is_locked(&group->mark_mutex)); 302 WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS)); 303 } 304 305 /* When calling fsnotify tell it if the data is a path or inode */ 306 enum fsnotify_data_type { 307 FSNOTIFY_EVENT_NONE, 308 FSNOTIFY_EVENT_FILE_RANGE, 309 FSNOTIFY_EVENT_PATH, 310 FSNOTIFY_EVENT_INODE, 311 FSNOTIFY_EVENT_DENTRY, 312 FSNOTIFY_EVENT_MNT, 313 FSNOTIFY_EVENT_ERROR, 314 FSNOTIFY_EVENT_RENAME, 315 }; 316 317 struct fs_error_report { 318 int error; 319 struct inode *inode; 320 struct super_block *sb; 321 }; 322 323 struct file_range { 324 const struct path *path; 325 loff_t pos; 326 size_t count; 327 }; 328 329 static inline const struct path *file_range_path(const struct file_range *range) 330 { 331 return range->path; 332 } 333 334 struct fsnotify_mnt { 335 const struct mnt_namespace *ns; 336 u64 mnt_id; 337 }; 338 339 struct fsnotify_rename_data { 340 struct dentry *moved; /* the dentry that was renamed */ 341 struct inode *target; /* inode overwritten by rename, or NULL */ 342 }; 343 344 static inline struct inode *fsnotify_data_inode(const void *data, int data_type) 345 { 346 switch (data_type) { 347 case FSNOTIFY_EVENT_INODE: 348 return (struct inode *)data; 349 case FSNOTIFY_EVENT_DENTRY: 350 return d_inode(data); 351 case FSNOTIFY_EVENT_PATH: 352 return d_inode(((const struct path *)data)->dentry); 353 case FSNOTIFY_EVENT_FILE_RANGE: 354 return d_inode(file_range_path(data)->dentry); 355 case FSNOTIFY_EVENT_ERROR: 356 return ((struct fs_error_report *)data)->inode; 357 case FSNOTIFY_EVENT_RENAME: 358 return d_inode(((const struct fsnotify_rename_data *)data)->moved); 359 default: 360 return NULL; 361 } 362 } 363 364 static inline struct dentry *fsnotify_data_dentry(const void *data, int data_type) 365 { 366 switch (data_type) { 367 case FSNOTIFY_EVENT_DENTRY: 368 /* Non const is needed for dget() */ 369 return (struct dentry *)data; 370 case FSNOTIFY_EVENT_PATH: 371 return ((const struct path *)data)->dentry; 372 case FSNOTIFY_EVENT_FILE_RANGE: 373 return file_range_path(data)->dentry; 374 case FSNOTIFY_EVENT_RENAME: 375 return ((struct fsnotify_rename_data *)data)->moved; 376 default: 377 return NULL; 378 } 379 } 380 381 static inline const struct path *fsnotify_data_path(const void *data, 382 int data_type) 383 { 384 switch (data_type) { 385 case FSNOTIFY_EVENT_PATH: 386 return data; 387 case FSNOTIFY_EVENT_FILE_RANGE: 388 return file_range_path(data); 389 default: 390 return NULL; 391 } 392 } 393 394 static inline struct super_block *fsnotify_data_sb(const void *data, 395 int data_type) 396 { 397 switch (data_type) { 398 case FSNOTIFY_EVENT_INODE: 399 return ((struct inode *)data)->i_sb; 400 case FSNOTIFY_EVENT_DENTRY: 401 return ((struct dentry *)data)->d_sb; 402 case FSNOTIFY_EVENT_PATH: 403 return ((const struct path *)data)->dentry->d_sb; 404 case FSNOTIFY_EVENT_FILE_RANGE: 405 return file_range_path(data)->dentry->d_sb; 406 case FSNOTIFY_EVENT_ERROR: 407 return ((struct fs_error_report *) data)->sb; 408 case FSNOTIFY_EVENT_RENAME: 409 return ((const struct fsnotify_rename_data *)data)->moved->d_sb; 410 default: 411 return NULL; 412 } 413 } 414 415 static inline const struct fsnotify_mnt *fsnotify_data_mnt(const void *data, 416 int data_type) 417 { 418 switch (data_type) { 419 case FSNOTIFY_EVENT_MNT: 420 return data; 421 default: 422 return NULL; 423 } 424 } 425 426 static inline u64 fsnotify_data_mnt_id(const void *data, int data_type) 427 { 428 const struct fsnotify_mnt *mnt_data = fsnotify_data_mnt(data, data_type); 429 430 return mnt_data ? mnt_data->mnt_id : 0; 431 } 432 433 static inline struct fs_error_report *fsnotify_data_error_report( 434 const void *data, 435 int data_type) 436 { 437 switch (data_type) { 438 case FSNOTIFY_EVENT_ERROR: 439 return (struct fs_error_report *) data; 440 default: 441 return NULL; 442 } 443 } 444 445 static inline struct inode *fsnotify_data_rename_target(const void *data, 446 int data_type) 447 { 448 if (data_type == FSNOTIFY_EVENT_RENAME) 449 return ((const struct fsnotify_rename_data *)data)->target; 450 return NULL; 451 } 452 453 static inline const struct file_range *fsnotify_data_file_range( 454 const void *data, 455 int data_type) 456 { 457 switch (data_type) { 458 case FSNOTIFY_EVENT_FILE_RANGE: 459 return (struct file_range *)data; 460 default: 461 return NULL; 462 } 463 } 464 465 /* 466 * Index to merged marks iterator array that correlates to a type of watch. 467 * The type of watched object can be deduced from the iterator type, but not 468 * the other way around, because an event can match different watched objects 469 * of the same object type. 470 * For example, both parent and child are watching an object of type inode. 471 */ 472 enum fsnotify_iter_type { 473 FSNOTIFY_ITER_TYPE_INODE, 474 FSNOTIFY_ITER_TYPE_VFSMOUNT, 475 FSNOTIFY_ITER_TYPE_SB, 476 FSNOTIFY_ITER_TYPE_PARENT, 477 FSNOTIFY_ITER_TYPE_INODE2, 478 FSNOTIFY_ITER_TYPE_MNTNS, 479 FSNOTIFY_ITER_TYPE_COUNT 480 }; 481 482 /* The type of object that a mark is attached to */ 483 enum fsnotify_obj_type { 484 FSNOTIFY_OBJ_TYPE_ANY = -1, 485 FSNOTIFY_OBJ_TYPE_INODE, 486 FSNOTIFY_OBJ_TYPE_VFSMOUNT, 487 FSNOTIFY_OBJ_TYPE_SB, 488 FSNOTIFY_OBJ_TYPE_MNTNS, 489 FSNOTIFY_OBJ_TYPE_COUNT, 490 FSNOTIFY_OBJ_TYPE_DETACHED = FSNOTIFY_OBJ_TYPE_COUNT 491 }; 492 493 static inline bool fsnotify_valid_obj_type(unsigned int obj_type) 494 { 495 return (obj_type < FSNOTIFY_OBJ_TYPE_COUNT); 496 } 497 498 struct fsnotify_iter_info { 499 struct fsnotify_mark *marks[FSNOTIFY_ITER_TYPE_COUNT]; 500 struct fsnotify_group *current_group; 501 unsigned int report_mask; 502 int srcu_idx; 503 }; 504 505 static inline bool fsnotify_iter_should_report_type( 506 struct fsnotify_iter_info *iter_info, int iter_type) 507 { 508 return (iter_info->report_mask & (1U << iter_type)); 509 } 510 511 static inline void fsnotify_iter_set_report_type( 512 struct fsnotify_iter_info *iter_info, int iter_type) 513 { 514 iter_info->report_mask |= (1U << iter_type); 515 } 516 517 static inline struct fsnotify_mark *fsnotify_iter_mark( 518 struct fsnotify_iter_info *iter_info, int iter_type) 519 { 520 if (fsnotify_iter_should_report_type(iter_info, iter_type)) 521 return iter_info->marks[iter_type]; 522 return NULL; 523 } 524 525 static inline int fsnotify_iter_step(struct fsnotify_iter_info *iter, int type, 526 struct fsnotify_mark **markp) 527 { 528 while (type < FSNOTIFY_ITER_TYPE_COUNT) { 529 *markp = fsnotify_iter_mark(iter, type); 530 if (*markp) 531 break; 532 type++; 533 } 534 return type; 535 } 536 537 #define FSNOTIFY_ITER_FUNCS(name, NAME) \ 538 static inline struct fsnotify_mark *fsnotify_iter_##name##_mark( \ 539 struct fsnotify_iter_info *iter_info) \ 540 { \ 541 return fsnotify_iter_mark(iter_info, FSNOTIFY_ITER_TYPE_##NAME); \ 542 } 543 544 FSNOTIFY_ITER_FUNCS(inode, INODE) 545 FSNOTIFY_ITER_FUNCS(parent, PARENT) 546 FSNOTIFY_ITER_FUNCS(vfsmount, VFSMOUNT) 547 FSNOTIFY_ITER_FUNCS(sb, SB) 548 549 #define fsnotify_foreach_iter_type(type) \ 550 for (type = 0; type < FSNOTIFY_ITER_TYPE_COUNT; type++) 551 #define fsnotify_foreach_iter_mark_type(iter, mark, type) \ 552 for (type = 0; \ 553 type = fsnotify_iter_step(iter, type, &mark), \ 554 type < FSNOTIFY_ITER_TYPE_COUNT; \ 555 type++) 556 557 /* 558 * Inode/vfsmount/sb point to this structure which tracks all marks attached to 559 * the inode/vfsmount/sb. The reference to inode/vfsmount/sb is held by this 560 * structure. We destroy this structure when there are no more marks attached 561 * to it. The structure is protected by fsnotify_mark_srcu. 562 */ 563 struct fsnotify_mark_connector { 564 spinlock_t lock; 565 unsigned char type; /* Type of object [lock] */ 566 unsigned char prio; /* Highest priority group */ 567 #define FSNOTIFY_CONN_FLAG_IS_WATCHED 0x01 568 #define FSNOTIFY_CONN_FLAG_HAS_IREF 0x02 569 unsigned short flags; /* flags [lock] */ 570 union { 571 /* Object pointer [lock] */ 572 void *obj; 573 /* Used listing heads to free after srcu period expires */ 574 struct fsnotify_mark_connector *destroy_next; 575 }; 576 struct hlist_head list; /* List of marks */ 577 }; 578 579 /* 580 * Container for per-sb fsnotify state (sb marks and more). 581 * Attached lazily on first marked object on the sb and freed when killing sb. 582 */ 583 struct fsnotify_sb_info { 584 struct fsnotify_mark_connector __rcu *sb_marks; 585 /* List of connectors for inode marks */ 586 struct list_head inode_conn_list; 587 spinlock_t list_lock; /* Lock protecting inode_conn_list */ 588 /* 589 * Number of inode/mount/sb objects that are being watched in this sb. 590 * Note that inodes objects are currently double-accounted. 591 * 592 * The value in watched_objects[prio] is the number of objects that are 593 * watched by groups of priority >= prio, so watched_objects[0] is the 594 * total number of watched objects in this sb. 595 */ 596 atomic_long_t watched_objects[__FSNOTIFY_PRIO_NUM]; 597 }; 598 599 static inline struct fsnotify_sb_info *fsnotify_sb_info(struct super_block *sb) 600 { 601 #ifdef CONFIG_FSNOTIFY 602 return READ_ONCE(sb->s_fsnotify_info); 603 #else 604 return NULL; 605 #endif 606 } 607 608 static inline atomic_long_t *fsnotify_sb_watched_objects(struct super_block *sb) 609 { 610 return &fsnotify_sb_info(sb)->watched_objects[0]; 611 } 612 613 /* 614 * A mark is simply an object attached to an in core inode which allows an 615 * fsnotify listener to indicate they are either no longer interested in events 616 * of a type matching mask or only interested in those events. 617 * 618 * These are flushed when an inode is evicted from core and may be flushed 619 * when the inode is modified (as seen by fsnotify_access). Some fsnotify 620 * users (such as dnotify) will flush these when the open fd is closed and not 621 * at inode eviction or modification. 622 * 623 * Text in brackets is showing the lock(s) protecting modifications of a 624 * particular entry. obj_lock means either inode->i_lock or 625 * mnt->mnt_root->d_lock depending on the mark type. 626 */ 627 struct fsnotify_mark { 628 /* Mask this mark is for [mark->lock, group->mark_mutex] */ 629 __u32 mask; 630 /* We hold one for presence in g_list. Also one ref for each 'thing' 631 * in kernel that found and may be using this mark. */ 632 refcount_t refcnt; 633 /* Group this mark is for. Set on mark creation, stable until last ref 634 * is dropped */ 635 struct fsnotify_group *group; 636 /* List of marks by group->marks_list. Also reused for queueing 637 * mark into destroy_list when it's waiting for the end of SRCU period 638 * before it can be freed. [group->mark_mutex] */ 639 struct list_head g_list; 640 /* Protects inode / mnt pointers, flags, masks */ 641 spinlock_t lock; 642 /* List of marks for inode / vfsmount [connector->lock, mark ref] */ 643 struct hlist_node obj_list; 644 /* Head of list of marks for an object [mark ref] */ 645 struct fsnotify_mark_connector *connector; 646 /* Events types and flags to ignore [mark->lock, group->mark_mutex] */ 647 __u32 ignore_mask; 648 /* General fsnotify mark flags */ 649 #define FSNOTIFY_MARK_FLAG_ALIVE 0x0001 650 #define FSNOTIFY_MARK_FLAG_ATTACHED 0x0002 651 /* inotify mark flags */ 652 #define FSNOTIFY_MARK_FLAG_EXCL_UNLINK 0x0010 653 #define FSNOTIFY_MARK_FLAG_IN_ONESHOT 0x0020 654 /* fanotify mark flags */ 655 #define FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY 0x0100 656 #define FSNOTIFY_MARK_FLAG_NO_IREF 0x0200 657 #define FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS 0x0400 658 #define FSNOTIFY_MARK_FLAG_HAS_FSID 0x0800 659 #define FSNOTIFY_MARK_FLAG_WEAK_FSID 0x1000 660 unsigned int flags; /* flags [mark->lock] */ 661 }; 662 663 #ifdef CONFIG_FSNOTIFY 664 665 /* called from the vfs helpers */ 666 667 /* main fsnotify call to send events */ 668 extern int fsnotify(__u32 mask, const void *data, int data_type, 669 struct inode *dir, const struct qstr *name, 670 struct inode *inode, u32 cookie); 671 extern int __fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data, 672 int data_type); 673 extern void __fsnotify_inode_delete(struct inode *inode); 674 extern void __fsnotify_vfsmount_delete(struct vfsmount *mnt); 675 extern void fsnotify_sb_delete(struct super_block *sb); 676 extern void __fsnotify_mntns_delete(struct mnt_namespace *mntns); 677 extern void fsnotify_sb_free(struct super_block *sb); 678 extern u32 fsnotify_get_cookie(void); 679 extern void fsnotify_mnt(__u32 mask, struct mnt_namespace *ns, struct vfsmount *mnt); 680 681 static inline __u32 fsnotify_parent_needed_mask(__u32 mask) 682 { 683 /* FS_EVENT_ON_CHILD is set on marks that want parent/name info */ 684 if (!(mask & FS_EVENT_ON_CHILD)) 685 return 0; 686 /* 687 * This object might be watched by a mark that cares about parent/name 688 * info, does it care about the specific set of events that can be 689 * reported with parent/name info? 690 */ 691 return mask & FS_EVENTS_POSS_TO_PARENT; 692 } 693 694 static inline int fsnotify_inode_watches_children(struct inode *inode) 695 { 696 __u32 parent_mask = READ_ONCE(inode->i_fsnotify_mask); 697 698 /* FS_EVENT_ON_CHILD is set if the inode may care */ 699 if (!(parent_mask & FS_EVENT_ON_CHILD)) 700 return 0; 701 /* this inode might care about child events, does it care about the 702 * specific set of events that can happen on a child? */ 703 return parent_mask & FS_EVENTS_POSS_ON_CHILD; 704 } 705 706 /* 707 * Update the dentry with a flag indicating the interest of its parent to receive 708 * filesystem events when those events happens to this dentry->d_inode. 709 */ 710 static inline void fsnotify_update_flags(struct dentry *dentry) 711 { 712 assert_spin_locked(&dentry->d_lock); 713 714 /* 715 * Serialisation of setting PARENT_WATCHED on the dentries is provided 716 * by d_lock. If inotify_inode_watched changes after we have taken 717 * d_lock, the following fsnotify_set_children_dentry_flags call will 718 * find our entry, so it will spin until we complete here, and update 719 * us with the new state. 720 */ 721 if (fsnotify_inode_watches_children(dentry->d_parent->d_inode)) 722 dentry->d_flags |= DCACHE_FSNOTIFY_PARENT_WATCHED; 723 else 724 dentry->d_flags &= ~DCACHE_FSNOTIFY_PARENT_WATCHED; 725 } 726 727 /* called from fsnotify listeners, such as fanotify or dnotify */ 728 729 /* create a new group */ 730 extern struct fsnotify_group *fsnotify_alloc_group( 731 const struct fsnotify_ops *ops, 732 int flags); 733 /* get reference to a group */ 734 extern void fsnotify_get_group(struct fsnotify_group *group); 735 /* drop reference on a group from fsnotify_alloc_group */ 736 extern void fsnotify_put_group(struct fsnotify_group *group); 737 /* group destruction begins, stop queuing new events */ 738 extern void fsnotify_group_stop_queueing(struct fsnotify_group *group); 739 /* destroy group */ 740 extern void fsnotify_destroy_group(struct fsnotify_group *group); 741 /* fasync handler function */ 742 extern int fsnotify_fasync(int fd, struct file *file, int on); 743 /* Free event from memory */ 744 extern void fsnotify_destroy_event(struct fsnotify_group *group, 745 struct fsnotify_event *event); 746 /* attach the event to the group notification queue */ 747 extern int fsnotify_insert_event(struct fsnotify_group *group, 748 struct fsnotify_event *event, 749 int (*merge)(struct fsnotify_group *, 750 struct fsnotify_event *), 751 void (*insert)(struct fsnotify_group *, 752 struct fsnotify_event *)); 753 754 static inline int fsnotify_add_event(struct fsnotify_group *group, 755 struct fsnotify_event *event, 756 int (*merge)(struct fsnotify_group *, 757 struct fsnotify_event *)) 758 { 759 return fsnotify_insert_event(group, event, merge, NULL); 760 } 761 762 /* Queue overflow event to a notification group */ 763 static inline void fsnotify_queue_overflow(struct fsnotify_group *group) 764 { 765 fsnotify_add_event(group, group->overflow_event, NULL); 766 } 767 768 static inline bool fsnotify_is_overflow_event(u32 mask) 769 { 770 return mask & FS_Q_OVERFLOW; 771 } 772 773 static inline bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group) 774 { 775 assert_spin_locked(&group->notification_lock); 776 777 return list_empty(&group->notification_list); 778 } 779 780 extern bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group); 781 /* return, but do not dequeue the first event on the notification queue */ 782 extern struct fsnotify_event *fsnotify_peek_first_event(struct fsnotify_group *group); 783 /* return AND dequeue the first event on the notification queue */ 784 extern struct fsnotify_event *fsnotify_remove_first_event(struct fsnotify_group *group); 785 /* Remove event queued in the notification list */ 786 extern void fsnotify_remove_queued_event(struct fsnotify_group *group, 787 struct fsnotify_event *event); 788 789 /* functions used to manipulate the marks attached to inodes */ 790 791 /* 792 * Canonical "ignore mask" including event flags. 793 * 794 * Note the subtle semantic difference from the legacy ->ignored_mask. 795 * ->ignored_mask traditionally only meant which events should be ignored, 796 * while ->ignore_mask also includes flags regarding the type of objects on 797 * which events should be ignored. 798 */ 799 static inline __u32 fsnotify_ignore_mask(struct fsnotify_mark *mark) 800 { 801 __u32 ignore_mask = mark->ignore_mask; 802 803 /* The event flags in ignore mask take effect */ 804 if (mark->flags & FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS) 805 return ignore_mask; 806 807 /* 808 * Legacy behavior: 809 * - Always ignore events on dir 810 * - Ignore events on child if parent is watching children 811 */ 812 ignore_mask |= FS_ISDIR; 813 ignore_mask &= ~FS_EVENT_ON_CHILD; 814 ignore_mask |= mark->mask & FS_EVENT_ON_CHILD; 815 816 return ignore_mask; 817 } 818 819 /* Legacy ignored_mask - only event types to ignore */ 820 static inline __u32 fsnotify_ignored_events(struct fsnotify_mark *mark) 821 { 822 return mark->ignore_mask & ALL_FSNOTIFY_EVENTS; 823 } 824 825 /* 826 * Check if mask (or ignore mask) should be applied depending if victim is a 827 * directory and whether it is reported to a watching parent. 828 */ 829 static inline bool fsnotify_mask_applicable(__u32 mask, bool is_dir, 830 int iter_type) 831 { 832 /* Should mask be applied to a directory? */ 833 if (is_dir && !(mask & FS_ISDIR)) 834 return false; 835 836 /* Should mask be applied to a child? */ 837 if (iter_type == FSNOTIFY_ITER_TYPE_PARENT && 838 !(mask & FS_EVENT_ON_CHILD)) 839 return false; 840 841 return true; 842 } 843 844 /* 845 * Effective ignore mask taking into account if event victim is a 846 * directory and whether it is reported to a watching parent. 847 */ 848 static inline __u32 fsnotify_effective_ignore_mask(struct fsnotify_mark *mark, 849 bool is_dir, int iter_type) 850 { 851 __u32 ignore_mask = fsnotify_ignored_events(mark); 852 853 if (!ignore_mask) 854 return 0; 855 856 /* For non-dir and non-child, no need to consult the event flags */ 857 if (!is_dir && iter_type != FSNOTIFY_ITER_TYPE_PARENT) 858 return ignore_mask; 859 860 ignore_mask = fsnotify_ignore_mask(mark); 861 if (!fsnotify_mask_applicable(ignore_mask, is_dir, iter_type)) 862 return 0; 863 864 return ignore_mask & ALL_FSNOTIFY_EVENTS; 865 } 866 867 /* Get mask for calculating object interest taking ignore mask into account */ 868 static inline __u32 fsnotify_calc_mask(struct fsnotify_mark *mark) 869 { 870 __u32 mask = mark->mask; 871 872 if (!fsnotify_ignored_events(mark)) 873 return mask; 874 875 /* Interest in FS_MODIFY may be needed for clearing ignore mask */ 876 if (!(mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)) 877 mask |= FS_MODIFY; 878 879 /* 880 * If mark is interested in ignoring events on children, the object must 881 * show interest in those events for fsnotify_parent() to notice it. 882 */ 883 return mask | mark->ignore_mask; 884 } 885 886 /* Get mask of events for a list of marks */ 887 extern __u32 fsnotify_conn_mask(struct fsnotify_mark_connector *conn); 888 /* Calculate mask of events for a list of marks */ 889 extern void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn); 890 extern void fsnotify_init_mark(struct fsnotify_mark *mark, 891 struct fsnotify_group *group); 892 /* Find mark belonging to given group in the list of marks */ 893 struct fsnotify_mark *fsnotify_find_mark(void *obj, unsigned int obj_type, 894 struct fsnotify_group *group); 895 /* attach the mark to the object */ 896 int fsnotify_add_mark(struct fsnotify_mark *mark, void *obj, 897 unsigned int obj_type, int add_flags); 898 int fsnotify_add_mark_locked(struct fsnotify_mark *mark, void *obj, 899 unsigned int obj_type, int add_flags); 900 901 /* attach the mark to the inode */ 902 static inline int fsnotify_add_inode_mark(struct fsnotify_mark *mark, 903 struct inode *inode, 904 int add_flags) 905 { 906 return fsnotify_add_mark(mark, inode, FSNOTIFY_OBJ_TYPE_INODE, 907 add_flags); 908 } 909 static inline int fsnotify_add_inode_mark_locked(struct fsnotify_mark *mark, 910 struct inode *inode, 911 int add_flags) 912 { 913 return fsnotify_add_mark_locked(mark, inode, FSNOTIFY_OBJ_TYPE_INODE, 914 add_flags); 915 } 916 917 static inline struct fsnotify_mark *fsnotify_find_inode_mark( 918 struct inode *inode, 919 struct fsnotify_group *group) 920 { 921 return fsnotify_find_mark(inode, FSNOTIFY_OBJ_TYPE_INODE, group); 922 } 923 924 /* given a group and a mark, flag mark to be freed when all references are dropped */ 925 extern void fsnotify_destroy_mark(struct fsnotify_mark *mark, 926 struct fsnotify_group *group); 927 /* detach mark from inode / mount list, group list, drop inode reference */ 928 extern void fsnotify_detach_mark(struct fsnotify_mark *mark); 929 /* free mark */ 930 extern void fsnotify_free_mark(struct fsnotify_mark *mark); 931 /* Wait until all marks queued for destruction are destroyed */ 932 extern void fsnotify_wait_marks_destroyed(void); 933 /* Clear all of the marks of a group attached to a given object type */ 934 extern void fsnotify_clear_marks_by_group(struct fsnotify_group *group, 935 unsigned int obj_type); 936 extern void fsnotify_get_mark(struct fsnotify_mark *mark); 937 extern void fsnotify_put_mark(struct fsnotify_mark *mark); 938 struct fsnotify_mark *fsnotify_next_mark(struct fsnotify_mark *mark); 939 extern void fsnotify_finish_user_wait(struct fsnotify_iter_info *iter_info); 940 extern bool fsnotify_prepare_user_wait(struct fsnotify_iter_info *iter_info); 941 extern void fsnotify_modify_mark_mask(struct fsnotify_mark *mark, u32 set, u32 clear); 942 943 static inline void fsnotify_init_event(struct fsnotify_event *event) 944 { 945 INIT_LIST_HEAD(&event->list); 946 } 947 int fsnotify_pre_content(const struct path *path, const loff_t *ppos, 948 size_t count); 949 950 #else 951 952 static inline int fsnotify_pre_content(const struct path *path, 953 const loff_t *ppos, size_t count) 954 { 955 return 0; 956 } 957 958 static inline int fsnotify(__u32 mask, const void *data, int data_type, 959 struct inode *dir, const struct qstr *name, 960 struct inode *inode, u32 cookie) 961 { 962 return 0; 963 } 964 965 static inline int __fsnotify_parent(struct dentry *dentry, __u32 mask, 966 const void *data, int data_type) 967 { 968 return 0; 969 } 970 971 static inline void __fsnotify_inode_delete(struct inode *inode) 972 {} 973 974 static inline void __fsnotify_vfsmount_delete(struct vfsmount *mnt) 975 {} 976 977 static inline void fsnotify_sb_delete(struct super_block *sb) 978 {} 979 980 static inline void __fsnotify_mntns_delete(struct mnt_namespace *mntns) 981 {} 982 983 static inline void fsnotify_sb_free(struct super_block *sb) 984 {} 985 986 static inline void fsnotify_update_flags(struct dentry *dentry) 987 {} 988 989 static inline u32 fsnotify_get_cookie(void) 990 { 991 return 0; 992 } 993 994 static inline void fsnotify_unmount_inodes(struct super_block *sb) 995 {} 996 997 static inline void fsnotify_mnt(__u32 mask, struct mnt_namespace *ns, struct vfsmount *mnt) 998 {} 999 1000 #endif /* CONFIG_FSNOTIFY */ 1001 1002 #endif /* __KERNEL __ */ 1003 1004 #endif /* __LINUX_FSNOTIFY_BACKEND_H */ 1005