1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com> 4 */ 5 6 /* 7 * fsnotify inode mark locking/lifetime/and refcnting 8 * 9 * REFCNT: 10 * The group->recnt and mark->refcnt tell how many "things" in the kernel 11 * currently are referencing the objects. Both kind of objects typically will 12 * live inside the kernel with a refcnt of 2, one for its creation and one for 13 * the reference a group and a mark hold to each other. 14 * If you are holding the appropriate locks, you can take a reference and the 15 * object itself is guaranteed to survive until the reference is dropped. 16 * 17 * LOCKING: 18 * There are 3 locks involved with fsnotify inode marks and they MUST be taken 19 * in order as follows: 20 * 21 * group->mark_mutex 22 * mark->lock 23 * mark->connector->lock 24 * 25 * group->mark_mutex protects the marks_list anchored inside a given group and 26 * each mark is hooked via the g_list. It also protects the groups private 27 * data (i.e group limits). 28 29 * mark->lock protects the marks attributes like its masks and flags. 30 * Furthermore it protects the access to a reference of the group that the mark 31 * is assigned to as well as the access to a reference of the inode/vfsmount 32 * that is being watched by the mark. 33 * 34 * mark->connector->lock protects the list of marks anchored inside an 35 * inode / vfsmount and each mark is hooked via the i_list. 36 * 37 * A list of notification marks relating to inode / mnt is contained in 38 * fsnotify_mark_connector. That structure is alive as long as there are any 39 * marks in the list and is also protected by fsnotify_mark_srcu. A mark gets 40 * detached from fsnotify_mark_connector when last reference to the mark is 41 * dropped. Thus having mark reference is enough to protect mark->connector 42 * pointer and to make sure fsnotify_mark_connector cannot disappear. Also 43 * because we remove mark from g_list before dropping mark reference associated 44 * with that, any mark found through g_list is guaranteed to have 45 * mark->connector set until we drop group->mark_mutex. 46 * 47 * LIFETIME: 48 * Inode marks survive between when they are added to an inode and when their 49 * refcnt==0. Marks are also protected by fsnotify_mark_srcu. 50 * 51 * The inode mark can be cleared for a number of different reasons including: 52 * - The inode is unlinked for the last time. (fsnotify_inode_remove) 53 * - The inode is being evicted from cache. (fsnotify_inode_delete) 54 * - The fs the inode is on is unmounted. (fsnotify_inode_delete/fsnotify_unmount_inodes) 55 * - Something explicitly requests that it be removed. (fsnotify_destroy_mark) 56 * - The fsnotify_group associated with the mark is going away and all such marks 57 * need to be cleaned up. (fsnotify_clear_marks_by_group) 58 * 59 * This has the very interesting property of being able to run concurrently with 60 * any (or all) other directions. 61 */ 62 63 #include <linux/fs.h> 64 #include <linux/init.h> 65 #include <linux/kernel.h> 66 #include <linux/kthread.h> 67 #include <linux/module.h> 68 #include <linux/mutex.h> 69 #include <linux/slab.h> 70 #include <linux/spinlock.h> 71 #include <linux/srcu.h> 72 #include <linux/ratelimit.h> 73 74 #include <linux/atomic.h> 75 76 #include <linux/fsnotify_backend.h> 77 #include "fsnotify.h" 78 79 #define FSNOTIFY_REAPER_DELAY (1) /* 1 jiffy */ 80 81 struct srcu_struct fsnotify_mark_srcu; 82 static struct kmem_cache *fsnotify_mark_connector_cachep; 83 static struct kmem_cache *fsnotify_inode_mark_connector_cachep; 84 85 static DEFINE_SPINLOCK(destroy_lock); 86 static LIST_HEAD(destroy_list); 87 static struct fsnotify_mark_connector *connector_destroy_list; 88 89 static void fsnotify_mark_destroy_workfn(struct work_struct *work); 90 static DECLARE_DELAYED_WORK(reaper_work, fsnotify_mark_destroy_workfn); 91 92 static void fsnotify_connector_destroy_workfn(struct work_struct *work); 93 static DECLARE_WORK(connector_reaper_work, fsnotify_connector_destroy_workfn); 94 95 void fsnotify_get_mark(struct fsnotify_mark *mark) 96 { 97 WARN_ON_ONCE(!refcount_read(&mark->refcnt)); 98 refcount_inc(&mark->refcnt); 99 } 100 101 static fsnotify_connp_t *fsnotify_object_connp(void *obj, 102 enum fsnotify_obj_type obj_type) 103 { 104 switch (obj_type) { 105 case FSNOTIFY_OBJ_TYPE_INODE: 106 return &((struct inode *)obj)->i_fsnotify_marks; 107 case FSNOTIFY_OBJ_TYPE_VFSMOUNT: 108 return &real_mount(obj)->mnt_fsnotify_marks; 109 case FSNOTIFY_OBJ_TYPE_SB: 110 return fsnotify_sb_marks(obj); 111 case FSNOTIFY_OBJ_TYPE_MNTNS: 112 return &((struct mnt_namespace *)obj)->n_fsnotify_marks; 113 default: 114 return NULL; 115 } 116 } 117 118 static __u32 *fsnotify_conn_mask_p(struct fsnotify_mark_connector *conn) 119 { 120 if (conn->type == FSNOTIFY_OBJ_TYPE_INODE) 121 return &fsnotify_conn_inode(conn)->i_fsnotify_mask; 122 else if (conn->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT) 123 return &fsnotify_conn_mount(conn)->mnt_fsnotify_mask; 124 else if (conn->type == FSNOTIFY_OBJ_TYPE_SB) 125 return &fsnotify_conn_sb(conn)->s_fsnotify_mask; 126 else if (conn->type == FSNOTIFY_OBJ_TYPE_MNTNS) 127 return &fsnotify_conn_mntns(conn)->n_fsnotify_mask; 128 return NULL; 129 } 130 131 __u32 fsnotify_conn_mask(struct fsnotify_mark_connector *conn) 132 { 133 if (WARN_ON(!fsnotify_valid_obj_type(conn->type))) 134 return 0; 135 136 return READ_ONCE(*fsnotify_conn_mask_p(conn)); 137 } 138 139 static void fsnotify_get_sb_watched_objects(struct super_block *sb) 140 { 141 atomic_long_inc(fsnotify_sb_watched_objects(sb)); 142 } 143 144 static void fsnotify_put_sb_watched_objects(struct super_block *sb) 145 { 146 atomic_long_t *watched_objects = fsnotify_sb_watched_objects(sb); 147 148 /* the superblock can go away after this decrement */ 149 if (atomic_long_dec_and_test(watched_objects)) 150 wake_up_var(watched_objects); 151 } 152 153 static void fsnotify_get_inode_ref(struct inode *inode) 154 { 155 ihold(inode); 156 fsnotify_get_sb_watched_objects(inode->i_sb); 157 } 158 159 static void fsnotify_put_inode_ref(struct inode *inode) 160 { 161 /* read ->i_sb before the inode can go away */ 162 struct super_block *sb = inode->i_sb; 163 164 iput(inode); 165 fsnotify_put_sb_watched_objects(sb); 166 } 167 168 /* 169 * Grab or drop watched objects reference depending on whether the connector 170 * is attached and has any marks attached. 171 */ 172 static void fsnotify_update_sb_watchers(struct super_block *sb, 173 struct fsnotify_mark_connector *conn) 174 { 175 struct fsnotify_sb_info *sbinfo = fsnotify_sb_info(sb); 176 bool is_watched = conn->flags & FSNOTIFY_CONN_FLAG_IS_WATCHED; 177 struct fsnotify_mark *first_mark = NULL; 178 unsigned int highest_prio = 0; 179 180 if (conn->obj) 181 first_mark = hlist_entry_safe(conn->list.first, 182 struct fsnotify_mark, obj_list); 183 if (first_mark) 184 highest_prio = first_mark->group->priority; 185 if (WARN_ON(highest_prio >= __FSNOTIFY_PRIO_NUM)) 186 highest_prio = 0; 187 188 /* 189 * If the highest priority of group watching this object is prio, 190 * then watched object has a reference on counters [0..prio]. 191 * Update priority >= 1 watched objects counters. 192 */ 193 for (unsigned int p = conn->prio + 1; p <= highest_prio; p++) 194 atomic_long_inc(&sbinfo->watched_objects[p]); 195 for (unsigned int p = conn->prio; p > highest_prio; p--) 196 atomic_long_dec(&sbinfo->watched_objects[p]); 197 conn->prio = highest_prio; 198 199 /* Update priority >= 0 (a.k.a total) watched objects counter */ 200 BUILD_BUG_ON(FSNOTIFY_PRIO_NORMAL != 0); 201 if (first_mark && !is_watched) { 202 conn->flags |= FSNOTIFY_CONN_FLAG_IS_WATCHED; 203 fsnotify_get_sb_watched_objects(sb); 204 } else if (!first_mark && is_watched) { 205 conn->flags &= ~FSNOTIFY_CONN_FLAG_IS_WATCHED; 206 fsnotify_put_sb_watched_objects(sb); 207 } 208 } 209 210 /* 211 * Grab or drop inode reference for the connector if needed. 212 * 213 * When it's time to drop the reference, we only clear the HAS_IREF flag and 214 * return the inode object. fsnotify_drop_object() will be resonsible for doing 215 * iput() outside of spinlocks. This happens when last mark that wanted iref is 216 * detached. 217 */ 218 static struct inode *fsnotify_update_iref(struct fsnotify_mark_connector *conn, 219 bool want_iref) 220 { 221 bool has_iref = conn->flags & FSNOTIFY_CONN_FLAG_HAS_IREF; 222 struct inode *inode = NULL; 223 224 if (conn->type != FSNOTIFY_OBJ_TYPE_INODE || 225 want_iref == has_iref) 226 return NULL; 227 228 if (want_iref) { 229 /* Pin inode if any mark wants inode refcount held */ 230 fsnotify_get_inode_ref(fsnotify_conn_inode(conn)); 231 conn->flags |= FSNOTIFY_CONN_FLAG_HAS_IREF; 232 } else { 233 /* Unpin inode after detach of last mark that wanted iref */ 234 inode = fsnotify_conn_inode(conn); 235 conn->flags &= ~FSNOTIFY_CONN_FLAG_HAS_IREF; 236 } 237 238 return inode; 239 } 240 241 static void *__fsnotify_recalc_mask(struct fsnotify_mark_connector *conn) 242 { 243 u32 new_mask = 0; 244 bool want_iref = false; 245 struct fsnotify_mark *mark; 246 247 assert_spin_locked(&conn->lock); 248 /* We can get detached connector here when inode is getting unlinked. */ 249 if (!fsnotify_valid_obj_type(conn->type)) 250 return NULL; 251 hlist_for_each_entry(mark, &conn->list, obj_list) { 252 if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) 253 continue; 254 new_mask |= fsnotify_calc_mask(mark); 255 if (conn->type == FSNOTIFY_OBJ_TYPE_INODE && 256 !(mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF)) 257 want_iref = true; 258 } 259 /* 260 * We use WRITE_ONCE() to prevent silly compiler optimizations from 261 * confusing readers not holding conn->lock with partial updates. 262 */ 263 WRITE_ONCE(*fsnotify_conn_mask_p(conn), new_mask); 264 265 return fsnotify_update_iref(conn, want_iref); 266 } 267 268 static bool fsnotify_conn_watches_children( 269 struct fsnotify_mark_connector *conn) 270 { 271 if (conn->type != FSNOTIFY_OBJ_TYPE_INODE) 272 return false; 273 274 return fsnotify_inode_watches_children(fsnotify_conn_inode(conn)); 275 } 276 277 static void fsnotify_conn_set_children_dentry_flags( 278 struct fsnotify_mark_connector *conn) 279 { 280 if (conn->type != FSNOTIFY_OBJ_TYPE_INODE) 281 return; 282 283 fsnotify_set_children_dentry_flags(fsnotify_conn_inode(conn)); 284 } 285 286 /* 287 * Calculate mask of events for a list of marks. The caller must make sure 288 * connector and connector->obj cannot disappear under us. Callers achieve 289 * this by holding a mark->lock or mark->group->mark_mutex for a mark on this 290 * list. 291 */ 292 void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn) 293 { 294 bool update_children; 295 296 if (!conn) 297 return; 298 299 spin_lock(&conn->lock); 300 update_children = !fsnotify_conn_watches_children(conn); 301 __fsnotify_recalc_mask(conn); 302 update_children &= fsnotify_conn_watches_children(conn); 303 spin_unlock(&conn->lock); 304 /* 305 * Set children's PARENT_WATCHED flags only if parent started watching. 306 * When parent stops watching, we clear false positive PARENT_WATCHED 307 * flags lazily in __fsnotify_parent(). 308 */ 309 if (update_children) 310 fsnotify_conn_set_children_dentry_flags(conn); 311 } 312 313 /* Free all connectors queued for freeing once SRCU period ends */ 314 static void fsnotify_connector_destroy_workfn(struct work_struct *work) 315 { 316 struct fsnotify_mark_connector *conn, *free; 317 318 spin_lock(&destroy_lock); 319 conn = connector_destroy_list; 320 connector_destroy_list = NULL; 321 spin_unlock(&destroy_lock); 322 323 synchronize_srcu(&fsnotify_mark_srcu); 324 while (conn) { 325 free = conn; 326 conn = conn->destroy_next; 327 kfree(free); 328 } 329 } 330 331 static void fsnotify_untrack_connector(struct fsnotify_mark_connector *conn); 332 333 static void *fsnotify_detach_connector_from_object( 334 struct fsnotify_mark_connector *conn, 335 unsigned int *type) 336 { 337 fsnotify_connp_t *connp = fsnotify_object_connp(conn->obj, conn->type); 338 struct super_block *sb = fsnotify_connector_sb(conn); 339 struct inode *inode = NULL; 340 341 *type = conn->type; 342 if (conn->type == FSNOTIFY_OBJ_TYPE_DETACHED) 343 return NULL; 344 345 if (conn->type == FSNOTIFY_OBJ_TYPE_INODE) { 346 inode = fsnotify_conn_inode(conn); 347 inode->i_fsnotify_mask = 0; 348 fsnotify_untrack_connector(conn); 349 350 /* Unpin inode when detaching from connector */ 351 if (!(conn->flags & FSNOTIFY_CONN_FLAG_HAS_IREF)) 352 inode = NULL; 353 } else if (conn->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT) { 354 fsnotify_conn_mount(conn)->mnt_fsnotify_mask = 0; 355 } else if (conn->type == FSNOTIFY_OBJ_TYPE_SB) { 356 fsnotify_conn_sb(conn)->s_fsnotify_mask = 0; 357 } else if (conn->type == FSNOTIFY_OBJ_TYPE_MNTNS) { 358 fsnotify_conn_mntns(conn)->n_fsnotify_mask = 0; 359 } 360 361 rcu_assign_pointer(*connp, NULL); 362 conn->obj = NULL; 363 conn->type = FSNOTIFY_OBJ_TYPE_DETACHED; 364 if (sb) 365 fsnotify_update_sb_watchers(sb, conn); 366 367 return inode; 368 } 369 370 static void fsnotify_final_mark_destroy(struct fsnotify_mark *mark) 371 { 372 struct fsnotify_group *group = mark->group; 373 374 if (WARN_ON_ONCE(!group)) 375 return; 376 group->ops->free_mark(mark); 377 fsnotify_put_group(group); 378 } 379 380 /* Drop object reference originally held by a connector */ 381 static void fsnotify_drop_object(unsigned int type, void *objp) 382 { 383 if (!objp) 384 return; 385 /* Currently only inode references are passed to be dropped */ 386 if (WARN_ON_ONCE(type != FSNOTIFY_OBJ_TYPE_INODE)) 387 return; 388 fsnotify_put_inode_ref(objp); 389 } 390 391 void fsnotify_put_mark(struct fsnotify_mark *mark) 392 { 393 struct fsnotify_mark_connector *conn = READ_ONCE(mark->connector); 394 void *objp = NULL; 395 unsigned int type = FSNOTIFY_OBJ_TYPE_DETACHED; 396 bool free_conn = false; 397 398 /* Catch marks that were actually never attached to object */ 399 if (!conn) { 400 if (refcount_dec_and_test(&mark->refcnt)) 401 fsnotify_final_mark_destroy(mark); 402 return; 403 } 404 405 /* 406 * We have to be careful so that traversals of obj_list under lock can 407 * safely grab mark reference. 408 */ 409 if (!refcount_dec_and_lock(&mark->refcnt, &conn->lock)) 410 return; 411 412 hlist_del_init_rcu(&mark->obj_list); 413 if (hlist_empty(&conn->list)) { 414 objp = fsnotify_detach_connector_from_object(conn, &type); 415 free_conn = true; 416 } else { 417 struct super_block *sb = fsnotify_connector_sb(conn); 418 419 /* Update watched objects after detaching mark */ 420 if (sb) 421 fsnotify_update_sb_watchers(sb, conn); 422 objp = __fsnotify_recalc_mask(conn); 423 type = conn->type; 424 } 425 WRITE_ONCE(mark->connector, NULL); 426 spin_unlock(&conn->lock); 427 428 fsnotify_drop_object(type, objp); 429 430 if (free_conn) { 431 spin_lock(&destroy_lock); 432 conn->destroy_next = connector_destroy_list; 433 connector_destroy_list = conn; 434 spin_unlock(&destroy_lock); 435 queue_work(system_dfl_wq, &connector_reaper_work); 436 } 437 /* 438 * Note that we didn't update flags telling whether inode cares about 439 * what's happening with children. We update these flags from 440 * __fsnotify_parent() lazily when next event happens on one of our 441 * children. 442 */ 443 spin_lock(&destroy_lock); 444 list_add(&mark->g_list, &destroy_list); 445 spin_unlock(&destroy_lock); 446 queue_delayed_work(system_dfl_wq, &reaper_work, 447 FSNOTIFY_REAPER_DELAY); 448 } 449 EXPORT_SYMBOL_GPL(fsnotify_put_mark); 450 451 /* 452 * Get mark reference when we found the mark via lockless traversal of object 453 * list. Mark can be already removed from the list by now and on its way to be 454 * destroyed once SRCU period ends. 455 * 456 * Also pin the group so it doesn't disappear under us. 457 */ 458 static bool fsnotify_get_mark_safe(struct fsnotify_mark *mark) 459 { 460 if (!mark) 461 return true; 462 463 if (refcount_inc_not_zero(&mark->refcnt)) { 464 spin_lock(&mark->lock); 465 if (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED) { 466 /* mark is attached, group is still alive then */ 467 atomic_inc(&mark->group->user_waits); 468 spin_unlock(&mark->lock); 469 return true; 470 } 471 spin_unlock(&mark->lock); 472 fsnotify_put_mark(mark); 473 } 474 return false; 475 } 476 477 /* 478 * Puts marks and wakes up group destruction if necessary. 479 * 480 * Pairs with fsnotify_get_mark_safe() 481 */ 482 static void fsnotify_put_mark_wake(struct fsnotify_mark *mark) 483 { 484 if (mark) { 485 struct fsnotify_group *group = mark->group; 486 487 fsnotify_put_mark(mark); 488 /* 489 * We abuse notification_waitq on group shutdown for waiting for 490 * all marks pinned when waiting for userspace. 491 */ 492 if (atomic_dec_and_test(&group->user_waits) && group->shutdown) 493 wake_up(&group->notification_waitq); 494 } 495 } 496 497 bool fsnotify_prepare_user_wait(struct fsnotify_iter_info *iter_info) 498 __releases(&fsnotify_mark_srcu) 499 { 500 int type; 501 502 fsnotify_foreach_iter_type(type) { 503 /* This can fail if mark is being removed */ 504 if (!fsnotify_get_mark_safe(iter_info->marks[type])) { 505 __release(&fsnotify_mark_srcu); 506 goto fail; 507 } 508 } 509 510 /* 511 * Now that both marks are pinned by refcount in the inode / vfsmount 512 * lists, we can drop SRCU lock, and safely resume the list iteration 513 * once userspace returns. 514 */ 515 srcu_read_unlock(&fsnotify_mark_srcu, iter_info->srcu_idx); 516 517 return true; 518 519 fail: 520 for (type--; type >= 0; type--) 521 fsnotify_put_mark_wake(iter_info->marks[type]); 522 return false; 523 } 524 525 void fsnotify_finish_user_wait(struct fsnotify_iter_info *iter_info) 526 __acquires(&fsnotify_mark_srcu) 527 { 528 int type; 529 530 iter_info->srcu_idx = srcu_read_lock(&fsnotify_mark_srcu); 531 fsnotify_foreach_iter_type(type) 532 fsnotify_put_mark_wake(iter_info->marks[type]); 533 } 534 535 /* 536 * Mark mark as detached, remove it from group list. Mark still stays in object 537 * list until its last reference is dropped. Note that we rely on mark being 538 * removed from group list before corresponding reference to it is dropped. In 539 * particular we rely on mark->connector being valid while we hold 540 * group->mark_mutex if we found the mark through g_list. 541 * 542 * Must be called with group->mark_mutex held. The caller must either hold 543 * reference to the mark or be protected by fsnotify_mark_srcu. 544 */ 545 void fsnotify_detach_mark(struct fsnotify_mark *mark) 546 { 547 fsnotify_group_assert_locked(mark->group); 548 WARN_ON_ONCE(!srcu_read_lock_held(&fsnotify_mark_srcu) && 549 refcount_read(&mark->refcnt) < 1 + 550 !!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)); 551 552 spin_lock(&mark->lock); 553 /* something else already called this function on this mark */ 554 if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) { 555 spin_unlock(&mark->lock); 556 return; 557 } 558 mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED; 559 list_del_init(&mark->g_list); 560 spin_unlock(&mark->lock); 561 562 /* Drop mark reference acquired in fsnotify_add_mark_locked() */ 563 fsnotify_put_mark(mark); 564 } 565 566 /* 567 * Free fsnotify mark. The mark is actually only marked as being freed. The 568 * freeing is actually happening only once last reference to the mark is 569 * dropped from a workqueue which first waits for srcu period end. 570 * 571 * Caller must have a reference to the mark or be protected by 572 * fsnotify_mark_srcu. 573 */ 574 void fsnotify_free_mark(struct fsnotify_mark *mark) 575 { 576 struct fsnotify_group *group = mark->group; 577 578 spin_lock(&mark->lock); 579 /* something else already called this function on this mark */ 580 if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) { 581 spin_unlock(&mark->lock); 582 return; 583 } 584 mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE; 585 spin_unlock(&mark->lock); 586 587 /* 588 * Some groups like to know that marks are being freed. This is a 589 * callback to the group function to let it know that this mark 590 * is being freed. 591 */ 592 if (group->ops->freeing_mark) 593 group->ops->freeing_mark(mark, group); 594 } 595 596 void fsnotify_destroy_mark(struct fsnotify_mark *mark, 597 struct fsnotify_group *group) 598 { 599 fsnotify_group_lock(group); 600 fsnotify_detach_mark(mark); 601 fsnotify_group_unlock(group); 602 fsnotify_free_mark(mark); 603 } 604 EXPORT_SYMBOL_GPL(fsnotify_destroy_mark); 605 606 /* 607 * Sorting function for lists of fsnotify marks. 608 * 609 * Fanotify supports different notification classes (reflected as priority of 610 * notification group). Events shall be passed to notification groups in 611 * decreasing priority order. To achieve this marks in notification lists for 612 * inodes and vfsmounts are sorted so that priorities of corresponding groups 613 * are descending. 614 * 615 * Furthermore correct handling of the ignore mask requires processing inode 616 * and vfsmount marks of each group together. Using the group address as 617 * further sort criterion provides a unique sorting order and thus we can 618 * merge inode and vfsmount lists of marks in linear time and find groups 619 * present in both lists. 620 * 621 * A return value of 1 signifies that b has priority over a. 622 * A return value of 0 signifies that the two marks have to be handled together. 623 * A return value of -1 signifies that a has priority over b. 624 */ 625 int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b) 626 { 627 if (a == b) 628 return 0; 629 if (!a) 630 return 1; 631 if (!b) 632 return -1; 633 if (a->priority < b->priority) 634 return 1; 635 if (a->priority > b->priority) 636 return -1; 637 if (a < b) 638 return 1; 639 return -1; 640 } 641 642 static int fsnotify_attach_info_to_sb(struct super_block *sb) 643 { 644 struct fsnotify_sb_info *sbinfo; 645 646 /* sb info is freed on fsnotify_sb_delete() */ 647 sbinfo = kzalloc(sizeof(*sbinfo), GFP_KERNEL); 648 if (!sbinfo) 649 return -ENOMEM; 650 651 INIT_LIST_HEAD(&sbinfo->inode_conn_list); 652 spin_lock_init(&sbinfo->list_lock); 653 /* 654 * cmpxchg() provides the barrier so that callers of fsnotify_sb_info() 655 * will observe an initialized structure 656 */ 657 if (cmpxchg(&sb->s_fsnotify_info, NULL, sbinfo)) { 658 /* Someone else created sbinfo for us */ 659 kfree(sbinfo); 660 } 661 return 0; 662 } 663 664 struct fsnotify_inode_mark_connector { 665 struct fsnotify_mark_connector common; 666 struct list_head conns_list; 667 }; 668 669 static struct inode *fsnotify_get_living_inode(struct fsnotify_sb_info *sbinfo) 670 { 671 struct fsnotify_inode_mark_connector *iconn; 672 struct inode *inode; 673 674 spin_lock(&sbinfo->list_lock); 675 /* Find the first non-evicting inode */ 676 list_for_each_entry(iconn, &sbinfo->inode_conn_list, conns_list) { 677 /* All connectors on the list are still attached to an inode */ 678 inode = iconn->common.obj; 679 /* 680 * For connectors without FSNOTIFY_CONN_FLAG_HAS_IREF 681 * (evictable marks) corresponding inode may well have 0 682 * refcount and can be undergoing eviction. OTOH list_lock 683 * protects us from the connector getting detached and inode 684 * freed. So we can poke around the inode safely. 685 */ 686 spin_lock(&inode->i_lock); 687 if (likely( 688 !(inode_state_read(inode) & (I_FREEING | I_WILL_FREE)))) { 689 __iget(inode); 690 spin_unlock(&inode->i_lock); 691 spin_unlock(&sbinfo->list_lock); 692 return inode; 693 } 694 spin_unlock(&inode->i_lock); 695 } 696 spin_unlock(&sbinfo->list_lock); 697 698 return NULL; 699 } 700 701 /** 702 * fsnotify_unmount_inodes - an sb is unmounting. Handle any watched inodes. 703 * @sbinfo: fsnotify info for superblock being unmounted. 704 * 705 * Walk all inode connectors for the superblock and free all associated marks. 706 */ 707 void fsnotify_unmount_inodes(struct fsnotify_sb_info *sbinfo) 708 { 709 struct inode *inode; 710 711 while ((inode = fsnotify_get_living_inode(sbinfo))) { 712 fsnotify_inode(inode, FS_UNMOUNT); 713 fsnotify_clear_marks_by_inode(inode); 714 iput(inode); 715 cond_resched(); 716 } 717 } 718 719 static void fsnotify_init_connector(struct fsnotify_mark_connector *conn, 720 void *obj, unsigned int obj_type) 721 { 722 spin_lock_init(&conn->lock); 723 INIT_HLIST_HEAD(&conn->list); 724 conn->flags = 0; 725 conn->prio = 0; 726 conn->type = obj_type; 727 conn->obj = obj; 728 } 729 730 static struct fsnotify_mark_connector * 731 fsnotify_alloc_inode_connector(struct inode *inode) 732 { 733 struct fsnotify_inode_mark_connector *iconn; 734 struct fsnotify_sb_info *sbinfo = fsnotify_sb_info(inode->i_sb); 735 736 iconn = kmem_cache_alloc(fsnotify_inode_mark_connector_cachep, 737 GFP_KERNEL); 738 if (!iconn) 739 return NULL; 740 741 fsnotify_init_connector(&iconn->common, inode, FSNOTIFY_OBJ_TYPE_INODE); 742 spin_lock(&sbinfo->list_lock); 743 list_add(&iconn->conns_list, &sbinfo->inode_conn_list); 744 spin_unlock(&sbinfo->list_lock); 745 746 return &iconn->common; 747 } 748 749 static void fsnotify_untrack_connector(struct fsnotify_mark_connector *conn) 750 { 751 struct fsnotify_inode_mark_connector *iconn; 752 struct fsnotify_sb_info *sbinfo; 753 754 if (conn->type != FSNOTIFY_OBJ_TYPE_INODE) 755 return; 756 757 iconn = container_of(conn, struct fsnotify_inode_mark_connector, common); 758 sbinfo = fsnotify_sb_info(fsnotify_conn_inode(conn)->i_sb); 759 spin_lock(&sbinfo->list_lock); 760 list_del(&iconn->conns_list); 761 spin_unlock(&sbinfo->list_lock); 762 } 763 764 static int fsnotify_attach_connector_to_object(fsnotify_connp_t *connp, 765 void *obj, unsigned int obj_type) 766 { 767 struct fsnotify_mark_connector *conn; 768 769 if (obj_type == FSNOTIFY_OBJ_TYPE_INODE) { 770 struct inode *inode = obj; 771 772 conn = fsnotify_alloc_inode_connector(inode); 773 } else { 774 conn = kmem_cache_alloc(fsnotify_mark_connector_cachep, 775 GFP_KERNEL); 776 if (conn) 777 fsnotify_init_connector(conn, obj, obj_type); 778 } 779 if (!conn) 780 return -ENOMEM; 781 782 /* 783 * cmpxchg() provides the barrier so that readers of *connp can see 784 * only initialized structure 785 */ 786 if (cmpxchg(connp, NULL, conn)) { 787 /* Someone else created list structure for us */ 788 fsnotify_untrack_connector(conn); 789 kfree(conn); 790 } 791 return 0; 792 } 793 794 /* 795 * Get mark connector, make sure it is alive and return with its lock held. 796 * This is for users that get connector pointer from inode or mount. Users that 797 * hold reference to a mark on the list may directly lock connector->lock as 798 * they are sure list cannot go away under them. 799 */ 800 static struct fsnotify_mark_connector *fsnotify_grab_connector( 801 fsnotify_connp_t *connp) 802 { 803 struct fsnotify_mark_connector *conn; 804 int idx; 805 806 idx = srcu_read_lock(&fsnotify_mark_srcu); 807 conn = srcu_dereference(*connp, &fsnotify_mark_srcu); 808 if (!conn) 809 goto out; 810 spin_lock(&conn->lock); 811 if (conn->type == FSNOTIFY_OBJ_TYPE_DETACHED) { 812 spin_unlock(&conn->lock); 813 srcu_read_unlock(&fsnotify_mark_srcu, idx); 814 return NULL; 815 } 816 out: 817 srcu_read_unlock(&fsnotify_mark_srcu, idx); 818 return conn; 819 } 820 821 /* 822 * Add mark into proper place in given list of marks. These marks may be used 823 * for the fsnotify backend to determine which event types should be delivered 824 * to which group and for which inodes. These marks are ordered according to 825 * priority, highest number first, and then by the group's location in memory. 826 */ 827 static int fsnotify_add_mark_list(struct fsnotify_mark *mark, void *obj, 828 unsigned int obj_type, int add_flags) 829 { 830 struct super_block *sb = fsnotify_object_sb(obj, obj_type); 831 struct fsnotify_mark *lmark, *last = NULL; 832 struct fsnotify_mark_connector *conn; 833 fsnotify_connp_t *connp; 834 int cmp; 835 int err = 0; 836 837 if (WARN_ON(!fsnotify_valid_obj_type(obj_type))) 838 return -EINVAL; 839 840 /* 841 * Attach the sb info before attaching a connector to any object on sb. 842 * The sb info will remain attached as long as sb lives. 843 */ 844 if (sb && !fsnotify_sb_info(sb)) { 845 err = fsnotify_attach_info_to_sb(sb); 846 if (err) 847 return err; 848 } 849 850 connp = fsnotify_object_connp(obj, obj_type); 851 restart: 852 spin_lock(&mark->lock); 853 conn = fsnotify_grab_connector(connp); 854 if (!conn) { 855 spin_unlock(&mark->lock); 856 err = fsnotify_attach_connector_to_object(connp, obj, obj_type); 857 if (err) 858 return err; 859 goto restart; 860 } 861 862 /* is mark the first mark? */ 863 if (hlist_empty(&conn->list)) { 864 hlist_add_head_rcu(&mark->obj_list, &conn->list); 865 goto added; 866 } 867 868 /* should mark be in the middle of the current list? */ 869 hlist_for_each_entry(lmark, &conn->list, obj_list) { 870 last = lmark; 871 872 if ((lmark->group == mark->group) && 873 (lmark->flags & FSNOTIFY_MARK_FLAG_ATTACHED) && 874 !(mark->group->flags & FSNOTIFY_GROUP_DUPS)) { 875 err = -EEXIST; 876 goto out_err; 877 } 878 879 cmp = fsnotify_compare_groups(lmark->group, mark->group); 880 if (cmp >= 0) { 881 hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list); 882 goto added; 883 } 884 } 885 886 BUG_ON(last == NULL); 887 /* mark should be the last entry. last is the current last entry */ 888 hlist_add_behind_rcu(&mark->obj_list, &last->obj_list); 889 added: 890 if (sb) 891 fsnotify_update_sb_watchers(sb, conn); 892 /* 893 * Since connector is attached to object using cmpxchg() we are 894 * guaranteed that connector initialization is fully visible by anyone 895 * seeing mark->connector set. 896 */ 897 WRITE_ONCE(mark->connector, conn); 898 out_err: 899 spin_unlock(&conn->lock); 900 spin_unlock(&mark->lock); 901 return err; 902 } 903 904 /* 905 * Attach an initialized mark to a given group and fs object. 906 * These marks may be used for the fsnotify backend to determine which 907 * event types should be delivered to which group. 908 */ 909 int fsnotify_add_mark_locked(struct fsnotify_mark *mark, 910 void *obj, unsigned int obj_type, 911 int add_flags) 912 { 913 struct fsnotify_group *group = mark->group; 914 int ret = 0; 915 916 fsnotify_group_assert_locked(group); 917 918 /* 919 * LOCKING ORDER!!!! 920 * group->mark_mutex 921 * mark->lock 922 * mark->connector->lock 923 */ 924 spin_lock(&mark->lock); 925 mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED; 926 927 list_add(&mark->g_list, &group->marks_list); 928 fsnotify_get_mark(mark); /* for g_list */ 929 spin_unlock(&mark->lock); 930 931 ret = fsnotify_add_mark_list(mark, obj, obj_type, add_flags); 932 if (ret) 933 goto err; 934 935 fsnotify_recalc_mask(mark->connector); 936 937 return ret; 938 err: 939 spin_lock(&mark->lock); 940 mark->flags &= ~(FSNOTIFY_MARK_FLAG_ALIVE | 941 FSNOTIFY_MARK_FLAG_ATTACHED); 942 list_del_init(&mark->g_list); 943 spin_unlock(&mark->lock); 944 945 fsnotify_put_mark(mark); 946 return ret; 947 } 948 949 int fsnotify_add_mark(struct fsnotify_mark *mark, void *obj, 950 unsigned int obj_type, int add_flags) 951 { 952 int ret; 953 struct fsnotify_group *group = mark->group; 954 955 fsnotify_group_lock(group); 956 ret = fsnotify_add_mark_locked(mark, obj, obj_type, add_flags); 957 fsnotify_group_unlock(group); 958 return ret; 959 } 960 EXPORT_SYMBOL_GPL(fsnotify_add_mark); 961 962 /* 963 * Given a list of marks, find the mark associated with given group. If found 964 * take a reference to that mark and return it, else return NULL. 965 */ 966 struct fsnotify_mark *fsnotify_find_mark(void *obj, unsigned int obj_type, 967 struct fsnotify_group *group) 968 { 969 fsnotify_connp_t *connp = fsnotify_object_connp(obj, obj_type); 970 struct fsnotify_mark_connector *conn; 971 struct fsnotify_mark *mark; 972 973 if (!connp) 974 return NULL; 975 976 conn = fsnotify_grab_connector(connp); 977 if (!conn) 978 return NULL; 979 980 hlist_for_each_entry(mark, &conn->list, obj_list) { 981 if (mark->group == group && 982 (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) { 983 fsnotify_get_mark(mark); 984 spin_unlock(&conn->lock); 985 return mark; 986 } 987 } 988 spin_unlock(&conn->lock); 989 return NULL; 990 } 991 EXPORT_SYMBOL_GPL(fsnotify_find_mark); 992 993 /* Clear any marks in a group with given type mask */ 994 void fsnotify_clear_marks_by_group(struct fsnotify_group *group, 995 unsigned int obj_type) 996 { 997 struct fsnotify_mark *lmark, *mark; 998 LIST_HEAD(to_free); 999 struct list_head *head = &to_free; 1000 1001 /* Skip selection step if we want to clear all marks. */ 1002 if (obj_type == FSNOTIFY_OBJ_TYPE_ANY) { 1003 head = &group->marks_list; 1004 goto clear; 1005 } 1006 /* 1007 * We have to be really careful here. Anytime we drop mark_mutex, e.g. 1008 * fsnotify_clear_marks_by_inode() can come and free marks. Even in our 1009 * to_free list so we have to use mark_mutex even when accessing that 1010 * list. And freeing mark requires us to drop mark_mutex. So we can 1011 * reliably free only the first mark in the list. That's why we first 1012 * move marks to free to to_free list in one go and then free marks in 1013 * to_free list one by one. 1014 */ 1015 fsnotify_group_lock(group); 1016 list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) { 1017 if (mark->connector->type == obj_type) 1018 list_move(&mark->g_list, &to_free); 1019 } 1020 fsnotify_group_unlock(group); 1021 1022 clear: 1023 while (1) { 1024 fsnotify_group_lock(group); 1025 if (list_empty(head)) { 1026 fsnotify_group_unlock(group); 1027 break; 1028 } 1029 mark = list_first_entry(head, struct fsnotify_mark, g_list); 1030 fsnotify_get_mark(mark); 1031 fsnotify_detach_mark(mark); 1032 fsnotify_group_unlock(group); 1033 fsnotify_free_mark(mark); 1034 fsnotify_put_mark(mark); 1035 } 1036 } 1037 1038 /* Destroy all marks attached to an object via connector */ 1039 void fsnotify_destroy_marks(fsnotify_connp_t *connp) 1040 { 1041 struct fsnotify_mark_connector *conn; 1042 struct fsnotify_mark *mark, *old_mark = NULL; 1043 void *objp; 1044 unsigned int type; 1045 1046 conn = fsnotify_grab_connector(connp); 1047 if (!conn) 1048 return; 1049 /* 1050 * We have to be careful since we can race with e.g. 1051 * fsnotify_clear_marks_by_group() and once we drop the conn->lock, the 1052 * list can get modified. However we are holding mark reference and 1053 * thus our mark cannot be removed from obj_list so we can continue 1054 * iteration after regaining conn->lock. 1055 */ 1056 hlist_for_each_entry(mark, &conn->list, obj_list) { 1057 fsnotify_get_mark(mark); 1058 spin_unlock(&conn->lock); 1059 if (old_mark) 1060 fsnotify_put_mark(old_mark); 1061 old_mark = mark; 1062 fsnotify_destroy_mark(mark, mark->group); 1063 spin_lock(&conn->lock); 1064 } 1065 /* 1066 * Detach list from object now so that we don't pin inode until all 1067 * mark references get dropped. It would lead to strange results such 1068 * as delaying inode deletion or blocking unmount. 1069 */ 1070 objp = fsnotify_detach_connector_from_object(conn, &type); 1071 spin_unlock(&conn->lock); 1072 if (old_mark) 1073 fsnotify_put_mark(old_mark); 1074 fsnotify_drop_object(type, objp); 1075 } 1076 1077 /* 1078 * Nothing fancy, just initialize lists and locks and counters. 1079 */ 1080 void fsnotify_init_mark(struct fsnotify_mark *mark, 1081 struct fsnotify_group *group) 1082 { 1083 memset(mark, 0, sizeof(*mark)); 1084 spin_lock_init(&mark->lock); 1085 refcount_set(&mark->refcnt, 1); 1086 fsnotify_get_group(group); 1087 mark->group = group; 1088 WRITE_ONCE(mark->connector, NULL); 1089 } 1090 EXPORT_SYMBOL_GPL(fsnotify_init_mark); 1091 1092 /* 1093 * Destroy all marks in destroy_list, waits for SRCU period to finish before 1094 * actually freeing marks. 1095 */ 1096 static void fsnotify_mark_destroy_workfn(struct work_struct *work) 1097 { 1098 struct fsnotify_mark *mark, *next; 1099 struct list_head private_destroy_list; 1100 1101 spin_lock(&destroy_lock); 1102 /* exchange the list head */ 1103 list_replace_init(&destroy_list, &private_destroy_list); 1104 spin_unlock(&destroy_lock); 1105 1106 synchronize_srcu(&fsnotify_mark_srcu); 1107 1108 list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) { 1109 list_del_init(&mark->g_list); 1110 fsnotify_final_mark_destroy(mark); 1111 } 1112 } 1113 1114 /* Wait for all marks queued for destruction to be actually destroyed */ 1115 void fsnotify_wait_marks_destroyed(void) 1116 { 1117 flush_delayed_work(&reaper_work); 1118 } 1119 EXPORT_SYMBOL_GPL(fsnotify_wait_marks_destroyed); 1120 1121 __init void fsnotify_init_connector_caches(void) 1122 { 1123 fsnotify_mark_connector_cachep = KMEM_CACHE(fsnotify_mark_connector, 1124 SLAB_PANIC); 1125 fsnotify_inode_mark_connector_cachep = KMEM_CACHE( 1126 fsnotify_inode_mark_connector, 1127 SLAB_PANIC); 1128 } 1129