| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * linux/fs/locks.c |
|---|
| 3 | 4 | * |
|---|
| .. | .. |
|---|
| 11 | 12 | * |
|---|
| 12 | 13 | * Miscellaneous edits, and a total rewrite of posix_lock_file() code. |
|---|
| 13 | 14 | * Kai Petzke (wpp@marie.physik.tu-berlin.de), 1994 |
|---|
| 14 | | - * |
|---|
| 15 | + * |
|---|
| 15 | 16 | * Converted file_lock_table to a linked list from an array, which eliminates |
|---|
| 16 | 17 | * the limits on how many active file locks are open. |
|---|
| 17 | 18 | * Chad Page (pageone@netcom.com), November 27, 1994 |
|---|
| 18 | | - * |
|---|
| 19 | + * |
|---|
| 19 | 20 | * Removed dependency on file descriptors. dup()'ed file descriptors now |
|---|
| 20 | 21 | * get the same locks as the original file descriptors, and a close() on |
|---|
| 21 | 22 | * any file descriptor removes ALL the locks on the file for the current |
|---|
| .. | .. |
|---|
| 41 | 42 | * with a file pointer (filp). As a result they can be shared by a parent |
|---|
| 42 | 43 | * process and its children after a fork(). They are removed when the last |
|---|
| 43 | 44 | * file descriptor referring to the file pointer is closed (unless explicitly |
|---|
| 44 | | - * unlocked). |
|---|
| 45 | + * unlocked). |
|---|
| 45 | 46 | * |
|---|
| 46 | 47 | * FL_FLOCK locks never deadlock, an existing lock is always removed before |
|---|
| 47 | 48 | * upgrading from shared to exclusive (or vice versa). When this happens |
|---|
| .. | .. |
|---|
| 50 | 51 | * Andy Walker (andy@lysaker.kvaerner.no), June 09, 1995 |
|---|
| 51 | 52 | * |
|---|
| 52 | 53 | * Removed some race conditions in flock_lock_file(), marked other possible |
|---|
| 53 | | - * races. Just grep for FIXME to see them. |
|---|
| 54 | + * races. Just grep for FIXME to see them. |
|---|
| 54 | 55 | * Dmitry Gorodchanin (pgmdsg@ibi.com), February 09, 1996. |
|---|
| 55 | 56 | * |
|---|
| 56 | 57 | * Addressed Dmitry's concerns. Deadlock checking no longer recursive. |
|---|
| .. | .. |
|---|
| 60 | 61 | * |
|---|
| 61 | 62 | * Initial implementation of mandatory locks. SunOS turned out to be |
|---|
| 62 | 63 | * a rotten model, so I implemented the "obvious" semantics. |
|---|
| 63 | | - * See 'Documentation/filesystems/mandatory-locking.txt' for details. |
|---|
| 64 | + * See 'Documentation/filesystems/mandatory-locking.rst' for details. |
|---|
| 64 | 65 | * Andy Walker (andy@lysaker.kvaerner.no), April 06, 1996. |
|---|
| 65 | 66 | * |
|---|
| 66 | 67 | * Don't allow mandatory locks on mmap()'ed files. Added simple functions to |
|---|
| .. | .. |
|---|
| 112 | 113 | * Leases and LOCK_MAND |
|---|
| 113 | 114 | * Matthew Wilcox <willy@debian.org>, June, 2000. |
|---|
| 114 | 115 | * Stephen Rothwell <sfr@canb.auug.org.au>, June, 2000. |
|---|
| 116 | + * |
|---|
| 117 | + * Locking conflicts and dependencies: |
|---|
| 118 | + * If multiple threads attempt to lock the same byte (or flock the same file) |
|---|
| 119 | + * only one can be granted the lock, and other must wait their turn. |
|---|
| 120 | + * The first lock has been "applied" or "granted", the others are "waiting" |
|---|
| 121 | + * and are "blocked" by the "applied" lock.. |
|---|
| 122 | + * |
|---|
| 123 | + * Waiting and applied locks are all kept in trees whose properties are: |
|---|
| 124 | + * |
|---|
| 125 | + * - the root of a tree may be an applied or waiting lock. |
|---|
| 126 | + * - every other node in the tree is a waiting lock that |
|---|
| 127 | + * conflicts with every ancestor of that node. |
|---|
| 128 | + * |
|---|
| 129 | + * Every such tree begins life as a waiting singleton which obviously |
|---|
| 130 | + * satisfies the above properties. |
|---|
| 131 | + * |
|---|
| 132 | + * The only ways we modify trees preserve these properties: |
|---|
| 133 | + * |
|---|
| 134 | + * 1. We may add a new leaf node, but only after first verifying that it |
|---|
| 135 | + * conflicts with all of its ancestors. |
|---|
| 136 | + * 2. We may remove the root of a tree, creating a new singleton |
|---|
| 137 | + * tree from the root and N new trees rooted in the immediate |
|---|
| 138 | + * children. |
|---|
| 139 | + * 3. If the root of a tree is not currently an applied lock, we may |
|---|
| 140 | + * apply it (if possible). |
|---|
| 141 | + * 4. We may upgrade the root of the tree (either extend its range, |
|---|
| 142 | + * or upgrade its entire range from read to write). |
|---|
| 143 | + * |
|---|
| 144 | + * When an applied lock is modified in a way that reduces or downgrades any |
|---|
| 145 | + * part of its range, we remove all its children (2 above). This particularly |
|---|
| 146 | + * happens when a lock is unlocked. |
|---|
| 147 | + * |
|---|
| 148 | + * For each of those child trees we "wake up" the thread which is |
|---|
| 149 | + * waiting for the lock so it can continue handling as follows: if the |
|---|
| 150 | + * root of the tree applies, we do so (3). If it doesn't, it must |
|---|
| 151 | + * conflict with some applied lock. We remove (wake up) all of its children |
|---|
| 152 | + * (2), and add it is a new leaf to the tree rooted in the applied |
|---|
| 153 | + * lock (1). We then repeat the process recursively with those |
|---|
| 154 | + * children. |
|---|
| 155 | + * |
|---|
| 115 | 156 | */ |
|---|
| 116 | 157 | |
|---|
| 117 | 158 | #include <linux/capability.h> |
|---|
| .. | .. |
|---|
| 171 | 212 | static DEFINE_PER_CPU(struct file_lock_list_struct, file_lock_list); |
|---|
| 172 | 213 | DEFINE_STATIC_PERCPU_RWSEM(file_rwsem); |
|---|
| 173 | 214 | |
|---|
| 215 | + |
|---|
| 174 | 216 | /* |
|---|
| 175 | 217 | * The blocked_hash is used to find POSIX lock loops for deadlock detection. |
|---|
| 176 | 218 | * It is protected by blocked_lock_lock. |
|---|
| .. | .. |
|---|
| 189 | 231 | * This lock protects the blocked_hash. Generally, if you're accessing it, you |
|---|
| 190 | 232 | * want to be holding this lock. |
|---|
| 191 | 233 | * |
|---|
| 192 | | - * In addition, it also protects the fl->fl_block list, and the fl->fl_next |
|---|
| 193 | | - * pointer for file_lock structures that are acting as lock requests (in |
|---|
| 194 | | - * contrast to those that are acting as records of acquired locks). |
|---|
| 234 | + * In addition, it also protects the fl->fl_blocked_requests list, and the |
|---|
| 235 | + * fl->fl_blocker pointer for file_lock structures that are acting as lock |
|---|
| 236 | + * requests (in contrast to those that are acting as records of acquired locks). |
|---|
| 195 | 237 | * |
|---|
| 196 | 238 | * Note that when we acquire this lock in order to change the above fields, |
|---|
| 197 | 239 | * we often hold the flc_lock as well. In certain cases, when reading the fields |
|---|
| .. | .. |
|---|
| 293 | 335 | { |
|---|
| 294 | 336 | INIT_HLIST_NODE(&fl->fl_link); |
|---|
| 295 | 337 | INIT_LIST_HEAD(&fl->fl_list); |
|---|
| 296 | | - INIT_LIST_HEAD(&fl->fl_block); |
|---|
| 338 | + INIT_LIST_HEAD(&fl->fl_blocked_requests); |
|---|
| 339 | + INIT_LIST_HEAD(&fl->fl_blocked_member); |
|---|
| 297 | 340 | init_waitqueue_head(&fl->fl_wait); |
|---|
| 298 | 341 | } |
|---|
| 299 | 342 | |
|---|
| .. | .. |
|---|
| 311 | 354 | |
|---|
| 312 | 355 | void locks_release_private(struct file_lock *fl) |
|---|
| 313 | 356 | { |
|---|
| 357 | + BUG_ON(waitqueue_active(&fl->fl_wait)); |
|---|
| 358 | + BUG_ON(!list_empty(&fl->fl_list)); |
|---|
| 359 | + BUG_ON(!list_empty(&fl->fl_blocked_requests)); |
|---|
| 360 | + BUG_ON(!list_empty(&fl->fl_blocked_member)); |
|---|
| 361 | + BUG_ON(!hlist_unhashed(&fl->fl_link)); |
|---|
| 362 | + |
|---|
| 314 | 363 | if (fl->fl_ops) { |
|---|
| 315 | 364 | if (fl->fl_ops->fl_release_private) |
|---|
| 316 | 365 | fl->fl_ops->fl_release_private(fl); |
|---|
| .. | .. |
|---|
| 330 | 379 | /* Free a lock which is not in use. */ |
|---|
| 331 | 380 | void locks_free_lock(struct file_lock *fl) |
|---|
| 332 | 381 | { |
|---|
| 333 | | - BUG_ON(waitqueue_active(&fl->fl_wait)); |
|---|
| 334 | | - BUG_ON(!list_empty(&fl->fl_list)); |
|---|
| 335 | | - BUG_ON(!list_empty(&fl->fl_block)); |
|---|
| 336 | | - BUG_ON(!hlist_unhashed(&fl->fl_link)); |
|---|
| 337 | | - |
|---|
| 338 | 382 | locks_release_private(fl); |
|---|
| 339 | 383 | kmem_cache_free(filelock_cache, fl); |
|---|
| 340 | 384 | } |
|---|
| .. | .. |
|---|
| 357 | 401 | memset(fl, 0, sizeof(struct file_lock)); |
|---|
| 358 | 402 | locks_init_lock_heads(fl); |
|---|
| 359 | 403 | } |
|---|
| 360 | | - |
|---|
| 361 | 404 | EXPORT_SYMBOL(locks_init_lock); |
|---|
| 362 | 405 | |
|---|
| 363 | 406 | /* |
|---|
| .. | .. |
|---|
| 397 | 440 | fl->fl_ops->fl_copy_lock(new, fl); |
|---|
| 398 | 441 | } |
|---|
| 399 | 442 | } |
|---|
| 400 | | - |
|---|
| 401 | 443 | EXPORT_SYMBOL(locks_copy_lock); |
|---|
| 444 | + |
|---|
| 445 | +static void locks_move_blocks(struct file_lock *new, struct file_lock *fl) |
|---|
| 446 | +{ |
|---|
| 447 | + struct file_lock *f; |
|---|
| 448 | + |
|---|
| 449 | + /* |
|---|
| 450 | + * As ctx->flc_lock is held, new requests cannot be added to |
|---|
| 451 | + * ->fl_blocked_requests, so we don't need a lock to check if it |
|---|
| 452 | + * is empty. |
|---|
| 453 | + */ |
|---|
| 454 | + if (list_empty(&fl->fl_blocked_requests)) |
|---|
| 455 | + return; |
|---|
| 456 | + spin_lock(&blocked_lock_lock); |
|---|
| 457 | + list_splice_init(&fl->fl_blocked_requests, &new->fl_blocked_requests); |
|---|
| 458 | + list_for_each_entry(f, &new->fl_blocked_requests, fl_blocked_member) |
|---|
| 459 | + f->fl_blocker = new; |
|---|
| 460 | + spin_unlock(&blocked_lock_lock); |
|---|
| 461 | +} |
|---|
| 402 | 462 | |
|---|
| 403 | 463 | static inline int flock_translate_cmd(int cmd) { |
|---|
| 404 | 464 | if (cmd & LOCK_MAND) |
|---|
| .. | .. |
|---|
| 416 | 476 | |
|---|
| 417 | 477 | /* Fill in a file_lock structure with an appropriate FLOCK lock. */ |
|---|
| 418 | 478 | static struct file_lock * |
|---|
| 419 | | -flock_make_lock(struct file *filp, unsigned int cmd) |
|---|
| 479 | +flock_make_lock(struct file *filp, unsigned int cmd, struct file_lock *fl) |
|---|
| 420 | 480 | { |
|---|
| 421 | | - struct file_lock *fl; |
|---|
| 422 | 481 | int type = flock_translate_cmd(cmd); |
|---|
| 423 | 482 | |
|---|
| 424 | 483 | if (type < 0) |
|---|
| 425 | 484 | return ERR_PTR(type); |
|---|
| 426 | | - |
|---|
| 427 | | - fl = locks_alloc_lock(); |
|---|
| 428 | | - if (fl == NULL) |
|---|
| 429 | | - return ERR_PTR(-ENOMEM); |
|---|
| 485 | + |
|---|
| 486 | + if (fl == NULL) { |
|---|
| 487 | + fl = locks_alloc_lock(); |
|---|
| 488 | + if (fl == NULL) |
|---|
| 489 | + return ERR_PTR(-ENOMEM); |
|---|
| 490 | + } else { |
|---|
| 491 | + locks_init_lock(fl); |
|---|
| 492 | + } |
|---|
| 430 | 493 | |
|---|
| 431 | 494 | fl->fl_file = filp; |
|---|
| 432 | 495 | fl->fl_owner = filp; |
|---|
| .. | .. |
|---|
| 434 | 497 | fl->fl_flags = FL_FLOCK; |
|---|
| 435 | 498 | fl->fl_type = type; |
|---|
| 436 | 499 | fl->fl_end = OFFSET_MAX; |
|---|
| 437 | | - |
|---|
| 500 | + |
|---|
| 438 | 501 | return fl; |
|---|
| 439 | 502 | } |
|---|
| 440 | 503 | |
|---|
| .. | .. |
|---|
| 596 | 659 | */ |
|---|
| 597 | 660 | static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2) |
|---|
| 598 | 661 | { |
|---|
| 599 | | - if (fl1->fl_lmops && fl1->fl_lmops->lm_compare_owner) |
|---|
| 600 | | - return fl2->fl_lmops == fl1->fl_lmops && |
|---|
| 601 | | - fl1->fl_lmops->lm_compare_owner(fl1, fl2); |
|---|
| 602 | 662 | return fl1->fl_owner == fl2->fl_owner; |
|---|
| 603 | 663 | } |
|---|
| 604 | 664 | |
|---|
| .. | .. |
|---|
| 639 | 699 | static unsigned long |
|---|
| 640 | 700 | posix_owner_key(struct file_lock *fl) |
|---|
| 641 | 701 | { |
|---|
| 642 | | - if (fl->fl_lmops && fl->fl_lmops->lm_owner_key) |
|---|
| 643 | | - return fl->fl_lmops->lm_owner_key(fl); |
|---|
| 644 | 702 | return (unsigned long)fl->fl_owner; |
|---|
| 645 | 703 | } |
|---|
| 646 | 704 | |
|---|
| .. | .. |
|---|
| 666 | 724 | static void __locks_delete_block(struct file_lock *waiter) |
|---|
| 667 | 725 | { |
|---|
| 668 | 726 | locks_delete_global_blocked(waiter); |
|---|
| 669 | | - list_del_init(&waiter->fl_block); |
|---|
| 670 | | - waiter->fl_next = NULL; |
|---|
| 727 | + list_del_init(&waiter->fl_blocked_member); |
|---|
| 671 | 728 | } |
|---|
| 672 | 729 | |
|---|
| 673 | | -static void locks_delete_block(struct file_lock *waiter) |
|---|
| 730 | +static void __locks_wake_up_blocks(struct file_lock *blocker) |
|---|
| 674 | 731 | { |
|---|
| 675 | | - spin_lock(&blocked_lock_lock); |
|---|
| 676 | | - __locks_delete_block(waiter); |
|---|
| 677 | | - spin_unlock(&blocked_lock_lock); |
|---|
| 732 | + while (!list_empty(&blocker->fl_blocked_requests)) { |
|---|
| 733 | + struct file_lock *waiter; |
|---|
| 734 | + |
|---|
| 735 | + waiter = list_first_entry(&blocker->fl_blocked_requests, |
|---|
| 736 | + struct file_lock, fl_blocked_member); |
|---|
| 737 | + __locks_delete_block(waiter); |
|---|
| 738 | + if (waiter->fl_lmops && waiter->fl_lmops->lm_notify) |
|---|
| 739 | + waiter->fl_lmops->lm_notify(waiter); |
|---|
| 740 | + else |
|---|
| 741 | + wake_up(&waiter->fl_wait); |
|---|
| 742 | + |
|---|
| 743 | + /* |
|---|
| 744 | + * The setting of fl_blocker to NULL marks the "done" |
|---|
| 745 | + * point in deleting a block. Paired with acquire at the top |
|---|
| 746 | + * of locks_delete_block(). |
|---|
| 747 | + */ |
|---|
| 748 | + smp_store_release(&waiter->fl_blocker, NULL); |
|---|
| 749 | + } |
|---|
| 678 | 750 | } |
|---|
| 751 | + |
|---|
| 752 | +/** |
|---|
| 753 | + * locks_delete_lock - stop waiting for a file lock |
|---|
| 754 | + * @waiter: the lock which was waiting |
|---|
| 755 | + * |
|---|
| 756 | + * lockd/nfsd need to disconnect the lock while working on it. |
|---|
| 757 | + */ |
|---|
| 758 | +int locks_delete_block(struct file_lock *waiter) |
|---|
| 759 | +{ |
|---|
| 760 | + int status = -ENOENT; |
|---|
| 761 | + |
|---|
| 762 | + /* |
|---|
| 763 | + * If fl_blocker is NULL, it won't be set again as this thread "owns" |
|---|
| 764 | + * the lock and is the only one that might try to claim the lock. |
|---|
| 765 | + * |
|---|
| 766 | + * We use acquire/release to manage fl_blocker so that we can |
|---|
| 767 | + * optimize away taking the blocked_lock_lock in many cases. |
|---|
| 768 | + * |
|---|
| 769 | + * The smp_load_acquire guarantees two things: |
|---|
| 770 | + * |
|---|
| 771 | + * 1/ that fl_blocked_requests can be tested locklessly. If something |
|---|
| 772 | + * was recently added to that list it must have been in a locked region |
|---|
| 773 | + * *before* the locked region when fl_blocker was set to NULL. |
|---|
| 774 | + * |
|---|
| 775 | + * 2/ that no other thread is accessing 'waiter', so it is safe to free |
|---|
| 776 | + * it. __locks_wake_up_blocks is careful not to touch waiter after |
|---|
| 777 | + * fl_blocker is released. |
|---|
| 778 | + * |
|---|
| 779 | + * If a lockless check of fl_blocker shows it to be NULL, we know that |
|---|
| 780 | + * no new locks can be inserted into its fl_blocked_requests list, and |
|---|
| 781 | + * can avoid doing anything further if the list is empty. |
|---|
| 782 | + */ |
|---|
| 783 | + if (!smp_load_acquire(&waiter->fl_blocker) && |
|---|
| 784 | + list_empty(&waiter->fl_blocked_requests)) |
|---|
| 785 | + return status; |
|---|
| 786 | + |
|---|
| 787 | + spin_lock(&blocked_lock_lock); |
|---|
| 788 | + if (waiter->fl_blocker) |
|---|
| 789 | + status = 0; |
|---|
| 790 | + __locks_wake_up_blocks(waiter); |
|---|
| 791 | + __locks_delete_block(waiter); |
|---|
| 792 | + |
|---|
| 793 | + /* |
|---|
| 794 | + * The setting of fl_blocker to NULL marks the "done" point in deleting |
|---|
| 795 | + * a block. Paired with acquire at the top of this function. |
|---|
| 796 | + */ |
|---|
| 797 | + smp_store_release(&waiter->fl_blocker, NULL); |
|---|
| 798 | + spin_unlock(&blocked_lock_lock); |
|---|
| 799 | + return status; |
|---|
| 800 | +} |
|---|
| 801 | +EXPORT_SYMBOL(locks_delete_block); |
|---|
| 679 | 802 | |
|---|
| 680 | 803 | /* Insert waiter into blocker's block list. |
|---|
| 681 | 804 | * We use a circular list so that processes can be easily woken up in |
|---|
| .. | .. |
|---|
| 683 | 806 | * it seems like the reasonable thing to do. |
|---|
| 684 | 807 | * |
|---|
| 685 | 808 | * Must be called with both the flc_lock and blocked_lock_lock held. The |
|---|
| 686 | | - * fl_block list itself is protected by the blocked_lock_lock, but by ensuring |
|---|
| 687 | | - * that the flc_lock is also held on insertions we can avoid taking the |
|---|
| 688 | | - * blocked_lock_lock in some cases when we see that the fl_block list is empty. |
|---|
| 809 | + * fl_blocked_requests list itself is protected by the blocked_lock_lock, |
|---|
| 810 | + * but by ensuring that the flc_lock is also held on insertions we can avoid |
|---|
| 811 | + * taking the blocked_lock_lock in some cases when we see that the |
|---|
| 812 | + * fl_blocked_requests list is empty. |
|---|
| 813 | + * |
|---|
| 814 | + * Rather than just adding to the list, we check for conflicts with any existing |
|---|
| 815 | + * waiters, and add beneath any waiter that blocks the new waiter. |
|---|
| 816 | + * Thus wakeups don't happen until needed. |
|---|
| 689 | 817 | */ |
|---|
| 690 | 818 | static void __locks_insert_block(struct file_lock *blocker, |
|---|
| 691 | | - struct file_lock *waiter) |
|---|
| 819 | + struct file_lock *waiter, |
|---|
| 820 | + bool conflict(struct file_lock *, |
|---|
| 821 | + struct file_lock *)) |
|---|
| 692 | 822 | { |
|---|
| 693 | | - BUG_ON(!list_empty(&waiter->fl_block)); |
|---|
| 694 | | - waiter->fl_next = blocker; |
|---|
| 695 | | - list_add_tail(&waiter->fl_block, &blocker->fl_block); |
|---|
| 823 | + struct file_lock *fl; |
|---|
| 824 | + BUG_ON(!list_empty(&waiter->fl_blocked_member)); |
|---|
| 825 | + |
|---|
| 826 | +new_blocker: |
|---|
| 827 | + list_for_each_entry(fl, &blocker->fl_blocked_requests, fl_blocked_member) |
|---|
| 828 | + if (conflict(fl, waiter)) { |
|---|
| 829 | + blocker = fl; |
|---|
| 830 | + goto new_blocker; |
|---|
| 831 | + } |
|---|
| 832 | + waiter->fl_blocker = blocker; |
|---|
| 833 | + list_add_tail(&waiter->fl_blocked_member, &blocker->fl_blocked_requests); |
|---|
| 696 | 834 | if (IS_POSIX(blocker) && !IS_OFDLCK(blocker)) |
|---|
| 697 | 835 | locks_insert_global_blocked(waiter); |
|---|
| 836 | + |
|---|
| 837 | + /* The requests in waiter->fl_blocked are known to conflict with |
|---|
| 838 | + * waiter, but might not conflict with blocker, or the requests |
|---|
| 839 | + * and lock which block it. So they all need to be woken. |
|---|
| 840 | + */ |
|---|
| 841 | + __locks_wake_up_blocks(waiter); |
|---|
| 698 | 842 | } |
|---|
| 699 | 843 | |
|---|
| 700 | 844 | /* Must be called with flc_lock held. */ |
|---|
| 701 | 845 | static void locks_insert_block(struct file_lock *blocker, |
|---|
| 702 | | - struct file_lock *waiter) |
|---|
| 846 | + struct file_lock *waiter, |
|---|
| 847 | + bool conflict(struct file_lock *, |
|---|
| 848 | + struct file_lock *)) |
|---|
| 703 | 849 | { |
|---|
| 704 | 850 | spin_lock(&blocked_lock_lock); |
|---|
| 705 | | - __locks_insert_block(blocker, waiter); |
|---|
| 851 | + __locks_insert_block(blocker, waiter, conflict); |
|---|
| 706 | 852 | spin_unlock(&blocked_lock_lock); |
|---|
| 707 | 853 | } |
|---|
| 708 | 854 | |
|---|
| .. | .. |
|---|
| 716 | 862 | /* |
|---|
| 717 | 863 | * Avoid taking global lock if list is empty. This is safe since new |
|---|
| 718 | 864 | * blocked requests are only added to the list under the flc_lock, and |
|---|
| 719 | | - * the flc_lock is always held here. Note that removal from the fl_block |
|---|
| 720 | | - * list does not require the flc_lock, so we must recheck list_empty() |
|---|
| 721 | | - * after acquiring the blocked_lock_lock. |
|---|
| 865 | + * the flc_lock is always held here. Note that removal from the |
|---|
| 866 | + * fl_blocked_requests list does not require the flc_lock, so we must |
|---|
| 867 | + * recheck list_empty() after acquiring the blocked_lock_lock. |
|---|
| 722 | 868 | */ |
|---|
| 723 | | - if (list_empty(&blocker->fl_block)) |
|---|
| 869 | + if (list_empty(&blocker->fl_blocked_requests)) |
|---|
| 724 | 870 | return; |
|---|
| 725 | 871 | |
|---|
| 726 | 872 | spin_lock(&blocked_lock_lock); |
|---|
| 727 | | - while (!list_empty(&blocker->fl_block)) { |
|---|
| 728 | | - struct file_lock *waiter; |
|---|
| 729 | | - |
|---|
| 730 | | - waiter = list_first_entry(&blocker->fl_block, |
|---|
| 731 | | - struct file_lock, fl_block); |
|---|
| 732 | | - __locks_delete_block(waiter); |
|---|
| 733 | | - if (waiter->fl_lmops && waiter->fl_lmops->lm_notify) |
|---|
| 734 | | - waiter->fl_lmops->lm_notify(waiter); |
|---|
| 735 | | - else |
|---|
| 736 | | - wake_up(&waiter->fl_wait); |
|---|
| 737 | | - } |
|---|
| 873 | + __locks_wake_up_blocks(blocker); |
|---|
| 738 | 874 | spin_unlock(&blocked_lock_lock); |
|---|
| 739 | 875 | } |
|---|
| 740 | 876 | |
|---|
| .. | .. |
|---|
| 766 | 902 | /* Determine if lock sys_fl blocks lock caller_fl. Common functionality |
|---|
| 767 | 903 | * checks for shared/exclusive status of overlapping locks. |
|---|
| 768 | 904 | */ |
|---|
| 769 | | -static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl) |
|---|
| 905 | +static bool locks_conflict(struct file_lock *caller_fl, |
|---|
| 906 | + struct file_lock *sys_fl) |
|---|
| 770 | 907 | { |
|---|
| 771 | 908 | if (sys_fl->fl_type == F_WRLCK) |
|---|
| 772 | | - return 1; |
|---|
| 909 | + return true; |
|---|
| 773 | 910 | if (caller_fl->fl_type == F_WRLCK) |
|---|
| 774 | | - return 1; |
|---|
| 775 | | - return 0; |
|---|
| 911 | + return true; |
|---|
| 912 | + return false; |
|---|
| 776 | 913 | } |
|---|
| 777 | 914 | |
|---|
| 778 | 915 | /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific |
|---|
| 779 | 916 | * checking before calling the locks_conflict(). |
|---|
| 780 | 917 | */ |
|---|
| 781 | | -static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl) |
|---|
| 918 | +static bool posix_locks_conflict(struct file_lock *caller_fl, |
|---|
| 919 | + struct file_lock *sys_fl) |
|---|
| 782 | 920 | { |
|---|
| 783 | 921 | /* POSIX locks owned by the same process do not conflict with |
|---|
| 784 | 922 | * each other. |
|---|
| 785 | 923 | */ |
|---|
| 786 | 924 | if (posix_same_owner(caller_fl, sys_fl)) |
|---|
| 787 | | - return (0); |
|---|
| 925 | + return false; |
|---|
| 788 | 926 | |
|---|
| 789 | 927 | /* Check whether they overlap */ |
|---|
| 790 | 928 | if (!locks_overlap(caller_fl, sys_fl)) |
|---|
| 791 | | - return 0; |
|---|
| 929 | + return false; |
|---|
| 792 | 930 | |
|---|
| 793 | | - return (locks_conflict(caller_fl, sys_fl)); |
|---|
| 931 | + return locks_conflict(caller_fl, sys_fl); |
|---|
| 794 | 932 | } |
|---|
| 795 | 933 | |
|---|
| 796 | 934 | /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific |
|---|
| 797 | 935 | * checking before calling the locks_conflict(). |
|---|
| 798 | 936 | */ |
|---|
| 799 | | -static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl) |
|---|
| 937 | +static bool flock_locks_conflict(struct file_lock *caller_fl, |
|---|
| 938 | + struct file_lock *sys_fl) |
|---|
| 800 | 939 | { |
|---|
| 801 | 940 | /* FLOCK locks referring to the same filp do not conflict with |
|---|
| 802 | 941 | * each other. |
|---|
| 803 | 942 | */ |
|---|
| 804 | 943 | if (caller_fl->fl_file == sys_fl->fl_file) |
|---|
| 805 | | - return (0); |
|---|
| 944 | + return false; |
|---|
| 806 | 945 | if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND)) |
|---|
| 807 | | - return 0; |
|---|
| 946 | + return false; |
|---|
| 808 | 947 | |
|---|
| 809 | | - return (locks_conflict(caller_fl, sys_fl)); |
|---|
| 948 | + return locks_conflict(caller_fl, sys_fl); |
|---|
| 810 | 949 | } |
|---|
| 811 | 950 | |
|---|
| 812 | 951 | void |
|---|
| .. | .. |
|---|
| 877 | 1016 | struct file_lock *fl; |
|---|
| 878 | 1017 | |
|---|
| 879 | 1018 | hash_for_each_possible(blocked_hash, fl, fl_link, posix_owner_key(block_fl)) { |
|---|
| 880 | | - if (posix_same_owner(fl, block_fl)) |
|---|
| 881 | | - return fl->fl_next; |
|---|
| 1019 | + if (posix_same_owner(fl, block_fl)) { |
|---|
| 1020 | + while (fl->fl_blocker) |
|---|
| 1021 | + fl = fl->fl_blocker; |
|---|
| 1022 | + return fl; |
|---|
| 1023 | + } |
|---|
| 882 | 1024 | } |
|---|
| 883 | 1025 | return NULL; |
|---|
| 884 | 1026 | } |
|---|
| .. | .. |
|---|
| 936 | 1078 | return -ENOMEM; |
|---|
| 937 | 1079 | } |
|---|
| 938 | 1080 | |
|---|
| 939 | | - percpu_down_read_preempt_disable(&file_rwsem); |
|---|
| 1081 | + percpu_down_read(&file_rwsem); |
|---|
| 940 | 1082 | spin_lock(&ctx->flc_lock); |
|---|
| 941 | 1083 | if (request->fl_flags & FL_ACCESS) |
|---|
| 942 | 1084 | goto find_conflict; |
|---|
| .. | .. |
|---|
| 965 | 1107 | if (!(request->fl_flags & FL_SLEEP)) |
|---|
| 966 | 1108 | goto out; |
|---|
| 967 | 1109 | error = FILE_LOCK_DEFERRED; |
|---|
| 968 | | - locks_insert_block(fl, request); |
|---|
| 1110 | + locks_insert_block(fl, request, flock_locks_conflict); |
|---|
| 969 | 1111 | goto out; |
|---|
| 970 | 1112 | } |
|---|
| 971 | 1113 | if (request->fl_flags & FL_ACCESS) |
|---|
| 972 | 1114 | goto out; |
|---|
| 973 | 1115 | locks_copy_lock(new_fl, request); |
|---|
| 1116 | + locks_move_blocks(new_fl, request); |
|---|
| 974 | 1117 | locks_insert_lock_ctx(new_fl, &ctx->flc_flock); |
|---|
| 975 | 1118 | new_fl = NULL; |
|---|
| 976 | 1119 | error = 0; |
|---|
| 977 | 1120 | |
|---|
| 978 | 1121 | out: |
|---|
| 979 | 1122 | spin_unlock(&ctx->flc_lock); |
|---|
| 980 | | - percpu_up_read_preempt_enable(&file_rwsem); |
|---|
| 1123 | + percpu_up_read(&file_rwsem); |
|---|
| 981 | 1124 | if (new_fl) |
|---|
| 982 | 1125 | locks_free_lock(new_fl); |
|---|
| 983 | 1126 | locks_dispose_list(&dispose); |
|---|
| .. | .. |
|---|
| 1015 | 1158 | new_fl2 = locks_alloc_lock(); |
|---|
| 1016 | 1159 | } |
|---|
| 1017 | 1160 | |
|---|
| 1018 | | - percpu_down_read_preempt_disable(&file_rwsem); |
|---|
| 1161 | + percpu_down_read(&file_rwsem); |
|---|
| 1019 | 1162 | spin_lock(&ctx->flc_lock); |
|---|
| 1020 | 1163 | /* |
|---|
| 1021 | 1164 | * New lock request. Walk all POSIX locks and look for conflicts. If |
|---|
| .. | .. |
|---|
| 1037 | 1180 | */ |
|---|
| 1038 | 1181 | error = -EDEADLK; |
|---|
| 1039 | 1182 | spin_lock(&blocked_lock_lock); |
|---|
| 1183 | + /* |
|---|
| 1184 | + * Ensure that we don't find any locks blocked on this |
|---|
| 1185 | + * request during deadlock detection. |
|---|
| 1186 | + */ |
|---|
| 1187 | + __locks_wake_up_blocks(request); |
|---|
| 1040 | 1188 | if (likely(!posix_locks_deadlock(request, fl))) { |
|---|
| 1041 | 1189 | error = FILE_LOCK_DEFERRED; |
|---|
| 1042 | | - __locks_insert_block(fl, request); |
|---|
| 1190 | + __locks_insert_block(fl, request, |
|---|
| 1191 | + posix_locks_conflict); |
|---|
| 1043 | 1192 | } |
|---|
| 1044 | 1193 | spin_unlock(&blocked_lock_lock); |
|---|
| 1045 | 1194 | goto out; |
|---|
| 1046 | | - } |
|---|
| 1047 | | - } |
|---|
| 1195 | + } |
|---|
| 1196 | + } |
|---|
| 1048 | 1197 | |
|---|
| 1049 | 1198 | /* If we're just looking for a conflict, we're done. */ |
|---|
| 1050 | 1199 | error = 0; |
|---|
| .. | .. |
|---|
| 1133 | 1282 | if (!new_fl) |
|---|
| 1134 | 1283 | goto out; |
|---|
| 1135 | 1284 | locks_copy_lock(new_fl, request); |
|---|
| 1285 | + locks_move_blocks(new_fl, request); |
|---|
| 1136 | 1286 | request = new_fl; |
|---|
| 1137 | 1287 | new_fl = NULL; |
|---|
| 1138 | 1288 | locks_insert_lock_ctx(request, &fl->fl_list); |
|---|
| .. | .. |
|---|
| 1164 | 1314 | goto out; |
|---|
| 1165 | 1315 | } |
|---|
| 1166 | 1316 | locks_copy_lock(new_fl, request); |
|---|
| 1317 | + locks_move_blocks(new_fl, request); |
|---|
| 1167 | 1318 | locks_insert_lock_ctx(new_fl, &fl->fl_list); |
|---|
| 1168 | 1319 | fl = new_fl; |
|---|
| 1169 | 1320 | new_fl = NULL; |
|---|
| .. | .. |
|---|
| 1187 | 1338 | } |
|---|
| 1188 | 1339 | out: |
|---|
| 1189 | 1340 | spin_unlock(&ctx->flc_lock); |
|---|
| 1190 | | - percpu_up_read_preempt_enable(&file_rwsem); |
|---|
| 1341 | + percpu_up_read(&file_rwsem); |
|---|
| 1191 | 1342 | /* |
|---|
| 1192 | 1343 | * Free any unused locks. |
|---|
| 1193 | 1344 | */ |
|---|
| .. | .. |
|---|
| 1237 | 1388 | error = posix_lock_inode(inode, fl, NULL); |
|---|
| 1238 | 1389 | if (error != FILE_LOCK_DEFERRED) |
|---|
| 1239 | 1390 | break; |
|---|
| 1240 | | - error = wait_event_interruptible(fl->fl_wait, !fl->fl_next); |
|---|
| 1241 | | - if (!error) |
|---|
| 1242 | | - continue; |
|---|
| 1243 | | - |
|---|
| 1244 | | - locks_delete_block(fl); |
|---|
| 1245 | | - break; |
|---|
| 1391 | + error = wait_event_interruptible(fl->fl_wait, |
|---|
| 1392 | + list_empty(&fl->fl_blocked_member)); |
|---|
| 1393 | + if (error) |
|---|
| 1394 | + break; |
|---|
| 1246 | 1395 | } |
|---|
| 1396 | + locks_delete_block(fl); |
|---|
| 1247 | 1397 | return error; |
|---|
| 1248 | 1398 | } |
|---|
| 1249 | 1399 | |
|---|
| .. | .. |
|---|
| 1324 | 1474 | error = posix_lock_inode(inode, &fl, NULL); |
|---|
| 1325 | 1475 | if (error != FILE_LOCK_DEFERRED) |
|---|
| 1326 | 1476 | break; |
|---|
| 1327 | | - error = wait_event_interruptible(fl.fl_wait, !fl.fl_next); |
|---|
| 1477 | + error = wait_event_interruptible(fl.fl_wait, |
|---|
| 1478 | + list_empty(&fl.fl_blocked_member)); |
|---|
| 1328 | 1479 | if (!error) { |
|---|
| 1329 | 1480 | /* |
|---|
| 1330 | 1481 | * If we've been sleeping someone might have |
|---|
| .. | .. |
|---|
| 1334 | 1485 | continue; |
|---|
| 1335 | 1486 | } |
|---|
| 1336 | 1487 | |
|---|
| 1337 | | - locks_delete_block(&fl); |
|---|
| 1338 | 1488 | break; |
|---|
| 1339 | 1489 | } |
|---|
| 1490 | + locks_delete_block(&fl); |
|---|
| 1340 | 1491 | |
|---|
| 1341 | 1492 | return error; |
|---|
| 1342 | 1493 | } |
|---|
| 1343 | | - |
|---|
| 1344 | 1494 | EXPORT_SYMBOL(locks_mandatory_area); |
|---|
| 1345 | 1495 | #endif /* CONFIG_MANDATORY_FILE_LOCKING */ |
|---|
| 1346 | 1496 | |
|---|
| .. | .. |
|---|
| 1349 | 1499 | switch (arg) { |
|---|
| 1350 | 1500 | case F_UNLCK: |
|---|
| 1351 | 1501 | fl->fl_flags &= ~FL_UNLOCK_PENDING; |
|---|
| 1352 | | - /* fall through: */ |
|---|
| 1502 | + fallthrough; |
|---|
| 1353 | 1503 | case F_RDLCK: |
|---|
| 1354 | 1504 | fl->fl_flags &= ~FL_DOWNGRADE_PENDING; |
|---|
| 1355 | 1505 | } |
|---|
| .. | .. |
|---|
| 1406 | 1556 | |
|---|
| 1407 | 1557 | static bool leases_conflict(struct file_lock *lease, struct file_lock *breaker) |
|---|
| 1408 | 1558 | { |
|---|
| 1409 | | - if ((breaker->fl_flags & FL_LAYOUT) != (lease->fl_flags & FL_LAYOUT)) |
|---|
| 1559 | + bool rc; |
|---|
| 1560 | + |
|---|
| 1561 | + if (lease->fl_lmops->lm_breaker_owns_lease |
|---|
| 1562 | + && lease->fl_lmops->lm_breaker_owns_lease(lease)) |
|---|
| 1410 | 1563 | return false; |
|---|
| 1411 | | - if ((breaker->fl_flags & FL_DELEG) && (lease->fl_flags & FL_LEASE)) |
|---|
| 1412 | | - return false; |
|---|
| 1413 | | - return locks_conflict(breaker, lease); |
|---|
| 1564 | + if ((breaker->fl_flags & FL_LAYOUT) != (lease->fl_flags & FL_LAYOUT)) { |
|---|
| 1565 | + rc = false; |
|---|
| 1566 | + goto trace; |
|---|
| 1567 | + } |
|---|
| 1568 | + if ((breaker->fl_flags & FL_DELEG) && (lease->fl_flags & FL_LEASE)) { |
|---|
| 1569 | + rc = false; |
|---|
| 1570 | + goto trace; |
|---|
| 1571 | + } |
|---|
| 1572 | + |
|---|
| 1573 | + rc = locks_conflict(breaker, lease); |
|---|
| 1574 | +trace: |
|---|
| 1575 | + trace_leases_conflict(rc, lease, breaker); |
|---|
| 1576 | + return rc; |
|---|
| 1414 | 1577 | } |
|---|
| 1415 | 1578 | |
|---|
| 1416 | 1579 | static bool |
|---|
| .. | .. |
|---|
| 1459 | 1622 | ctx = smp_load_acquire(&inode->i_flctx); |
|---|
| 1460 | 1623 | if (!ctx) { |
|---|
| 1461 | 1624 | WARN_ON_ONCE(1); |
|---|
| 1462 | | - return error; |
|---|
| 1625 | + goto free_lock; |
|---|
| 1463 | 1626 | } |
|---|
| 1464 | 1627 | |
|---|
| 1465 | | - percpu_down_read_preempt_disable(&file_rwsem); |
|---|
| 1628 | + percpu_down_read(&file_rwsem); |
|---|
| 1466 | 1629 | spin_lock(&ctx->flc_lock); |
|---|
| 1467 | 1630 | |
|---|
| 1468 | 1631 | time_out_leases(inode, &dispose); |
|---|
| .. | .. |
|---|
| 1511 | 1674 | break_time -= jiffies; |
|---|
| 1512 | 1675 | if (break_time == 0) |
|---|
| 1513 | 1676 | break_time++; |
|---|
| 1514 | | - locks_insert_block(fl, new_fl); |
|---|
| 1677 | + locks_insert_block(fl, new_fl, leases_conflict); |
|---|
| 1515 | 1678 | trace_break_lease_block(inode, new_fl); |
|---|
| 1516 | 1679 | spin_unlock(&ctx->flc_lock); |
|---|
| 1517 | | - percpu_up_read_preempt_enable(&file_rwsem); |
|---|
| 1680 | + percpu_up_read(&file_rwsem); |
|---|
| 1518 | 1681 | |
|---|
| 1519 | 1682 | locks_dispose_list(&dispose); |
|---|
| 1520 | 1683 | error = wait_event_interruptible_timeout(new_fl->fl_wait, |
|---|
| 1521 | | - !new_fl->fl_next, break_time); |
|---|
| 1684 | + list_empty(&new_fl->fl_blocked_member), |
|---|
| 1685 | + break_time); |
|---|
| 1522 | 1686 | |
|---|
| 1523 | | - percpu_down_read_preempt_disable(&file_rwsem); |
|---|
| 1687 | + percpu_down_read(&file_rwsem); |
|---|
| 1524 | 1688 | spin_lock(&ctx->flc_lock); |
|---|
| 1525 | 1689 | trace_break_lease_unblock(inode, new_fl); |
|---|
| 1526 | 1690 | locks_delete_block(new_fl); |
|---|
| .. | .. |
|---|
| 1537 | 1701 | } |
|---|
| 1538 | 1702 | out: |
|---|
| 1539 | 1703 | spin_unlock(&ctx->flc_lock); |
|---|
| 1540 | | - percpu_up_read_preempt_enable(&file_rwsem); |
|---|
| 1704 | + percpu_up_read(&file_rwsem); |
|---|
| 1541 | 1705 | locks_dispose_list(&dispose); |
|---|
| 1706 | +free_lock: |
|---|
| 1542 | 1707 | locks_free_lock(new_fl); |
|---|
| 1543 | 1708 | return error; |
|---|
| 1544 | 1709 | } |
|---|
| 1545 | | - |
|---|
| 1546 | 1710 | EXPORT_SYMBOL(__break_lease); |
|---|
| 1547 | 1711 | |
|---|
| 1548 | 1712 | /** |
|---|
| .. | .. |
|---|
| 1573 | 1737 | if (has_lease) |
|---|
| 1574 | 1738 | *time = current_time(inode); |
|---|
| 1575 | 1739 | } |
|---|
| 1576 | | - |
|---|
| 1577 | 1740 | EXPORT_SYMBOL(lease_get_mtime); |
|---|
| 1578 | 1741 | |
|---|
| 1579 | 1742 | /** |
|---|
| .. | .. |
|---|
| 1609 | 1772 | |
|---|
| 1610 | 1773 | ctx = smp_load_acquire(&inode->i_flctx); |
|---|
| 1611 | 1774 | if (ctx && !list_empty_careful(&ctx->flc_lease)) { |
|---|
| 1612 | | - percpu_down_read_preempt_disable(&file_rwsem); |
|---|
| 1775 | + percpu_down_read(&file_rwsem); |
|---|
| 1613 | 1776 | spin_lock(&ctx->flc_lock); |
|---|
| 1614 | 1777 | time_out_leases(inode, &dispose); |
|---|
| 1615 | 1778 | list_for_each_entry(fl, &ctx->flc_lease, fl_list) { |
|---|
| .. | .. |
|---|
| 1619 | 1782 | break; |
|---|
| 1620 | 1783 | } |
|---|
| 1621 | 1784 | spin_unlock(&ctx->flc_lock); |
|---|
| 1622 | | - percpu_up_read_preempt_enable(&file_rwsem); |
|---|
| 1785 | + percpu_up_read(&file_rwsem); |
|---|
| 1623 | 1786 | |
|---|
| 1624 | 1787 | locks_dispose_list(&dispose); |
|---|
| 1625 | 1788 | } |
|---|
| .. | .. |
|---|
| 1627 | 1790 | } |
|---|
| 1628 | 1791 | |
|---|
| 1629 | 1792 | /** |
|---|
| 1630 | | - * check_conflicting_open - see if the given dentry points to a file that has |
|---|
| 1631 | | - * an existing open that would conflict with the |
|---|
| 1632 | | - * desired lease. |
|---|
| 1633 | | - * @dentry: dentry to check |
|---|
| 1793 | + * check_conflicting_open - see if the given file points to an inode that has |
|---|
| 1794 | + * an existing open that would conflict with the |
|---|
| 1795 | + * desired lease. |
|---|
| 1796 | + * @filp: file to check |
|---|
| 1634 | 1797 | * @arg: type of lease that we're trying to acquire |
|---|
| 1635 | 1798 | * @flags: current lock flags |
|---|
| 1636 | 1799 | * |
|---|
| .. | .. |
|---|
| 1638 | 1801 | * conflict with the lease we're trying to set. |
|---|
| 1639 | 1802 | */ |
|---|
| 1640 | 1803 | static int |
|---|
| 1641 | | -check_conflicting_open(const struct dentry *dentry, const long arg, int flags) |
|---|
| 1804 | +check_conflicting_open(struct file *filp, const long arg, int flags) |
|---|
| 1642 | 1805 | { |
|---|
| 1643 | | - int ret = 0; |
|---|
| 1644 | | - struct inode *inode = dentry->d_inode; |
|---|
| 1806 | + struct inode *inode = locks_inode(filp); |
|---|
| 1807 | + int self_wcount = 0, self_rcount = 0; |
|---|
| 1645 | 1808 | |
|---|
| 1646 | 1809 | if (flags & FL_LAYOUT) |
|---|
| 1647 | 1810 | return 0; |
|---|
| 1648 | 1811 | |
|---|
| 1649 | | - if ((arg == F_RDLCK) && (atomic_read(&inode->i_writecount) > 0)) |
|---|
| 1812 | + if (arg == F_RDLCK) |
|---|
| 1813 | + return inode_is_open_for_write(inode) ? -EAGAIN : 0; |
|---|
| 1814 | + else if (arg != F_WRLCK) |
|---|
| 1815 | + return 0; |
|---|
| 1816 | + |
|---|
| 1817 | + /* |
|---|
| 1818 | + * Make sure that only read/write count is from lease requestor. |
|---|
| 1819 | + * Note that this will result in denying write leases when i_writecount |
|---|
| 1820 | + * is negative, which is what we want. (We shouldn't grant write leases |
|---|
| 1821 | + * on files open for execution.) |
|---|
| 1822 | + */ |
|---|
| 1823 | + if (filp->f_mode & FMODE_WRITE) |
|---|
| 1824 | + self_wcount = 1; |
|---|
| 1825 | + else if (filp->f_mode & FMODE_READ) |
|---|
| 1826 | + self_rcount = 1; |
|---|
| 1827 | + |
|---|
| 1828 | + if (atomic_read(&inode->i_writecount) != self_wcount || |
|---|
| 1829 | + atomic_read(&inode->i_readcount) != self_rcount) |
|---|
| 1650 | 1830 | return -EAGAIN; |
|---|
| 1651 | 1831 | |
|---|
| 1652 | | - if ((arg == F_WRLCK) && ((d_count(dentry) > 1) || |
|---|
| 1653 | | - (atomic_read(&inode->i_count) > 1))) |
|---|
| 1654 | | - ret = -EAGAIN; |
|---|
| 1655 | | - |
|---|
| 1656 | | - return ret; |
|---|
| 1832 | + return 0; |
|---|
| 1657 | 1833 | } |
|---|
| 1658 | 1834 | |
|---|
| 1659 | 1835 | static int |
|---|
| 1660 | 1836 | generic_add_lease(struct file *filp, long arg, struct file_lock **flp, void **priv) |
|---|
| 1661 | 1837 | { |
|---|
| 1662 | 1838 | struct file_lock *fl, *my_fl = NULL, *lease; |
|---|
| 1663 | | - struct dentry *dentry = filp->f_path.dentry; |
|---|
| 1664 | | - struct inode *inode = dentry->d_inode; |
|---|
| 1839 | + struct inode *inode = locks_inode(filp); |
|---|
| 1665 | 1840 | struct file_lock_context *ctx; |
|---|
| 1666 | 1841 | bool is_deleg = (*flp)->fl_flags & FL_DELEG; |
|---|
| 1667 | 1842 | int error; |
|---|
| .. | .. |
|---|
| 1693 | 1868 | return -EINVAL; |
|---|
| 1694 | 1869 | } |
|---|
| 1695 | 1870 | |
|---|
| 1696 | | - percpu_down_read_preempt_disable(&file_rwsem); |
|---|
| 1871 | + percpu_down_read(&file_rwsem); |
|---|
| 1697 | 1872 | spin_lock(&ctx->flc_lock); |
|---|
| 1698 | 1873 | time_out_leases(inode, &dispose); |
|---|
| 1699 | | - error = check_conflicting_open(dentry, arg, lease->fl_flags); |
|---|
| 1874 | + error = check_conflicting_open(filp, arg, lease->fl_flags); |
|---|
| 1700 | 1875 | if (error) |
|---|
| 1701 | 1876 | goto out; |
|---|
| 1702 | 1877 | |
|---|
| .. | .. |
|---|
| 1753 | 1928 | * precedes these checks. |
|---|
| 1754 | 1929 | */ |
|---|
| 1755 | 1930 | smp_mb(); |
|---|
| 1756 | | - error = check_conflicting_open(dentry, arg, lease->fl_flags); |
|---|
| 1931 | + error = check_conflicting_open(filp, arg, lease->fl_flags); |
|---|
| 1757 | 1932 | if (error) { |
|---|
| 1758 | 1933 | locks_unlink_lock_ctx(lease); |
|---|
| 1759 | 1934 | goto out; |
|---|
| .. | .. |
|---|
| 1764 | 1939 | lease->fl_lmops->lm_setup(lease, priv); |
|---|
| 1765 | 1940 | out: |
|---|
| 1766 | 1941 | spin_unlock(&ctx->flc_lock); |
|---|
| 1767 | | - percpu_up_read_preempt_enable(&file_rwsem); |
|---|
| 1942 | + percpu_up_read(&file_rwsem); |
|---|
| 1768 | 1943 | locks_dispose_list(&dispose); |
|---|
| 1769 | 1944 | if (is_deleg) |
|---|
| 1770 | 1945 | inode_unlock(inode); |
|---|
| .. | .. |
|---|
| 1787 | 1962 | return error; |
|---|
| 1788 | 1963 | } |
|---|
| 1789 | 1964 | |
|---|
| 1790 | | - percpu_down_read_preempt_disable(&file_rwsem); |
|---|
| 1965 | + percpu_down_read(&file_rwsem); |
|---|
| 1791 | 1966 | spin_lock(&ctx->flc_lock); |
|---|
| 1792 | 1967 | list_for_each_entry(fl, &ctx->flc_lease, fl_list) { |
|---|
| 1793 | 1968 | if (fl->fl_file == filp && |
|---|
| .. | .. |
|---|
| 1800 | 1975 | if (victim) |
|---|
| 1801 | 1976 | error = fl->fl_lmops->lm_change(victim, F_UNLCK, &dispose); |
|---|
| 1802 | 1977 | spin_unlock(&ctx->flc_lock); |
|---|
| 1803 | | - percpu_up_read_preempt_enable(&file_rwsem); |
|---|
| 1978 | + percpu_up_read(&file_rwsem); |
|---|
| 1804 | 1979 | locks_dispose_list(&dispose); |
|---|
| 1805 | 1980 | return error; |
|---|
| 1806 | 1981 | } |
|---|
| .. | .. |
|---|
| 1847 | 2022 | } |
|---|
| 1848 | 2023 | EXPORT_SYMBOL(generic_setlease); |
|---|
| 1849 | 2024 | |
|---|
| 2025 | +#if IS_ENABLED(CONFIG_SRCU) |
|---|
| 2026 | +/* |
|---|
| 2027 | + * Kernel subsystems can register to be notified on any attempt to set |
|---|
| 2028 | + * a new lease with the lease_notifier_chain. This is used by (e.g.) nfsd |
|---|
| 2029 | + * to close files that it may have cached when there is an attempt to set a |
|---|
| 2030 | + * conflicting lease. |
|---|
| 2031 | + */ |
|---|
| 2032 | +static struct srcu_notifier_head lease_notifier_chain; |
|---|
| 2033 | + |
|---|
| 2034 | +static inline void |
|---|
| 2035 | +lease_notifier_chain_init(void) |
|---|
| 2036 | +{ |
|---|
| 2037 | + srcu_init_notifier_head(&lease_notifier_chain); |
|---|
| 2038 | +} |
|---|
| 2039 | + |
|---|
| 2040 | +static inline void |
|---|
| 2041 | +setlease_notifier(long arg, struct file_lock *lease) |
|---|
| 2042 | +{ |
|---|
| 2043 | + if (arg != F_UNLCK) |
|---|
| 2044 | + srcu_notifier_call_chain(&lease_notifier_chain, arg, lease); |
|---|
| 2045 | +} |
|---|
| 2046 | + |
|---|
| 2047 | +int lease_register_notifier(struct notifier_block *nb) |
|---|
| 2048 | +{ |
|---|
| 2049 | + return srcu_notifier_chain_register(&lease_notifier_chain, nb); |
|---|
| 2050 | +} |
|---|
| 2051 | +EXPORT_SYMBOL_GPL(lease_register_notifier); |
|---|
| 2052 | + |
|---|
| 2053 | +void lease_unregister_notifier(struct notifier_block *nb) |
|---|
| 2054 | +{ |
|---|
| 2055 | + srcu_notifier_chain_unregister(&lease_notifier_chain, nb); |
|---|
| 2056 | +} |
|---|
| 2057 | +EXPORT_SYMBOL_GPL(lease_unregister_notifier); |
|---|
| 2058 | + |
|---|
| 2059 | +#else /* !IS_ENABLED(CONFIG_SRCU) */ |
|---|
| 2060 | +static inline void |
|---|
| 2061 | +lease_notifier_chain_init(void) |
|---|
| 2062 | +{ |
|---|
| 2063 | +} |
|---|
| 2064 | + |
|---|
| 2065 | +static inline void |
|---|
| 2066 | +setlease_notifier(long arg, struct file_lock *lease) |
|---|
| 2067 | +{ |
|---|
| 2068 | +} |
|---|
| 2069 | + |
|---|
| 2070 | +int lease_register_notifier(struct notifier_block *nb) |
|---|
| 2071 | +{ |
|---|
| 2072 | + return 0; |
|---|
| 2073 | +} |
|---|
| 2074 | +EXPORT_SYMBOL_GPL(lease_register_notifier); |
|---|
| 2075 | + |
|---|
| 2076 | +void lease_unregister_notifier(struct notifier_block *nb) |
|---|
| 2077 | +{ |
|---|
| 2078 | +} |
|---|
| 2079 | +EXPORT_SYMBOL_GPL(lease_unregister_notifier); |
|---|
| 2080 | + |
|---|
| 2081 | +#endif /* IS_ENABLED(CONFIG_SRCU) */ |
|---|
| 2082 | + |
|---|
| 1850 | 2083 | /** |
|---|
| 1851 | 2084 | * vfs_setlease - sets a lease on an open file |
|---|
| 1852 | 2085 | * @filp: file pointer |
|---|
| 1853 | 2086 | * @arg: type of lease to obtain |
|---|
| 1854 | 2087 | * @lease: file_lock to use when adding a lease |
|---|
| 1855 | 2088 | * @priv: private info for lm_setup when adding a lease (may be |
|---|
| 1856 | | - * NULL if lm_setup doesn't require it) |
|---|
| 2089 | + * NULL if lm_setup doesn't require it) |
|---|
| 1857 | 2090 | * |
|---|
| 1858 | 2091 | * Call this to establish a lease on the file. The "lease" argument is not |
|---|
| 1859 | 2092 | * used for F_UNLCK requests and may be NULL. For commands that set or alter |
|---|
| .. | .. |
|---|
| 1867 | 2100 | int |
|---|
| 1868 | 2101 | vfs_setlease(struct file *filp, long arg, struct file_lock **lease, void **priv) |
|---|
| 1869 | 2102 | { |
|---|
| 2103 | + if (lease) |
|---|
| 2104 | + setlease_notifier(arg, *lease); |
|---|
| 1870 | 2105 | if (filp->f_op->setlease) |
|---|
| 1871 | 2106 | return filp->f_op->setlease(filp, arg, lease, priv); |
|---|
| 1872 | 2107 | else |
|---|
| .. | .. |
|---|
| 1931 | 2166 | error = flock_lock_inode(inode, fl); |
|---|
| 1932 | 2167 | if (error != FILE_LOCK_DEFERRED) |
|---|
| 1933 | 2168 | break; |
|---|
| 1934 | | - error = wait_event_interruptible(fl->fl_wait, !fl->fl_next); |
|---|
| 1935 | | - if (!error) |
|---|
| 1936 | | - continue; |
|---|
| 1937 | | - |
|---|
| 1938 | | - locks_delete_block(fl); |
|---|
| 1939 | | - break; |
|---|
| 2169 | + error = wait_event_interruptible(fl->fl_wait, |
|---|
| 2170 | + list_empty(&fl->fl_blocked_member)); |
|---|
| 2171 | + if (error) |
|---|
| 2172 | + break; |
|---|
| 1940 | 2173 | } |
|---|
| 2174 | + locks_delete_block(fl); |
|---|
| 1941 | 2175 | return error; |
|---|
| 1942 | 2176 | } |
|---|
| 1943 | 2177 | |
|---|
| .. | .. |
|---|
| 2001 | 2235 | !(f.file->f_mode & (FMODE_READ|FMODE_WRITE))) |
|---|
| 2002 | 2236 | goto out_putf; |
|---|
| 2003 | 2237 | |
|---|
| 2004 | | - lock = flock_make_lock(f.file, cmd); |
|---|
| 2238 | + lock = flock_make_lock(f.file, cmd, NULL); |
|---|
| 2005 | 2239 | if (IS_ERR(lock)) { |
|---|
| 2006 | 2240 | error = PTR_ERR(lock); |
|---|
| 2007 | 2241 | goto out_putf; |
|---|
| .. | .. |
|---|
| 2143 | 2377 | error = vfs_test_lock(filp, fl); |
|---|
| 2144 | 2378 | if (error) |
|---|
| 2145 | 2379 | goto out; |
|---|
| 2146 | | - |
|---|
| 2380 | + |
|---|
| 2147 | 2381 | flock->l_type = fl->fl_type; |
|---|
| 2148 | 2382 | if (fl->fl_type != F_UNLCK) { |
|---|
| 2149 | 2383 | error = posix_lock_to_flock(flock, fl); |
|---|
| .. | .. |
|---|
| 2210 | 2444 | error = vfs_lock_file(filp, cmd, fl, NULL); |
|---|
| 2211 | 2445 | if (error != FILE_LOCK_DEFERRED) |
|---|
| 2212 | 2446 | break; |
|---|
| 2213 | | - error = wait_event_interruptible(fl->fl_wait, !fl->fl_next); |
|---|
| 2214 | | - if (!error) |
|---|
| 2215 | | - continue; |
|---|
| 2216 | | - |
|---|
| 2217 | | - locks_delete_block(fl); |
|---|
| 2218 | | - break; |
|---|
| 2447 | + error = wait_event_interruptible(fl->fl_wait, |
|---|
| 2448 | + list_empty(&fl->fl_blocked_member)); |
|---|
| 2449 | + if (error) |
|---|
| 2450 | + break; |
|---|
| 2219 | 2451 | } |
|---|
| 2452 | + locks_delete_block(fl); |
|---|
| 2220 | 2453 | |
|---|
| 2221 | 2454 | return error; |
|---|
| 2222 | 2455 | } |
|---|
| .. | .. |
|---|
| 2289 | 2522 | cmd = F_SETLKW; |
|---|
| 2290 | 2523 | file_lock->fl_flags |= FL_OFDLCK; |
|---|
| 2291 | 2524 | file_lock->fl_owner = filp; |
|---|
| 2292 | | - /* Fallthrough */ |
|---|
| 2525 | + fallthrough; |
|---|
| 2293 | 2526 | case F_SETLKW: |
|---|
| 2294 | 2527 | file_lock->fl_flags |= FL_SLEEP; |
|---|
| 2295 | 2528 | } |
|---|
| .. | .. |
|---|
| 2420 | 2653 | cmd = F_SETLKW64; |
|---|
| 2421 | 2654 | file_lock->fl_flags |= FL_OFDLCK; |
|---|
| 2422 | 2655 | file_lock->fl_owner = filp; |
|---|
| 2423 | | - /* Fallthrough */ |
|---|
| 2656 | + fallthrough; |
|---|
| 2424 | 2657 | case F_SETLKW64: |
|---|
| 2425 | 2658 | file_lock->fl_flags |= FL_SLEEP; |
|---|
| 2426 | 2659 | } |
|---|
| .. | .. |
|---|
| 2476 | 2709 | if (!ctx || list_empty(&ctx->flc_posix)) |
|---|
| 2477 | 2710 | return; |
|---|
| 2478 | 2711 | |
|---|
| 2712 | + locks_init_lock(&lock); |
|---|
| 2479 | 2713 | lock.fl_type = F_UNLCK; |
|---|
| 2480 | 2714 | lock.fl_flags = FL_POSIX | FL_CLOSE; |
|---|
| 2481 | 2715 | lock.fl_start = 0; |
|---|
| .. | .. |
|---|
| 2492 | 2726 | lock.fl_ops->fl_release_private(&lock); |
|---|
| 2493 | 2727 | trace_locks_remove_posix(inode, &lock, error); |
|---|
| 2494 | 2728 | } |
|---|
| 2495 | | - |
|---|
| 2496 | 2729 | EXPORT_SYMBOL(locks_remove_posix); |
|---|
| 2497 | 2730 | |
|---|
| 2498 | 2731 | /* The i_flctx must be valid when calling into here */ |
|---|
| 2499 | 2732 | static void |
|---|
| 2500 | 2733 | locks_remove_flock(struct file *filp, struct file_lock_context *flctx) |
|---|
| 2501 | 2734 | { |
|---|
| 2502 | | - struct file_lock fl = { |
|---|
| 2503 | | - .fl_owner = filp, |
|---|
| 2504 | | - .fl_pid = current->tgid, |
|---|
| 2505 | | - .fl_file = filp, |
|---|
| 2506 | | - .fl_flags = FL_FLOCK | FL_CLOSE, |
|---|
| 2507 | | - .fl_type = F_UNLCK, |
|---|
| 2508 | | - .fl_end = OFFSET_MAX, |
|---|
| 2509 | | - }; |
|---|
| 2735 | + struct file_lock fl; |
|---|
| 2510 | 2736 | struct inode *inode = locks_inode(filp); |
|---|
| 2511 | 2737 | |
|---|
| 2512 | 2738 | if (list_empty(&flctx->flc_flock)) |
|---|
| 2513 | 2739 | return; |
|---|
| 2740 | + |
|---|
| 2741 | + flock_make_lock(filp, LOCK_UN, &fl); |
|---|
| 2742 | + fl.fl_flags |= FL_CLOSE; |
|---|
| 2514 | 2743 | |
|---|
| 2515 | 2744 | if (filp->f_op->flock) |
|---|
| 2516 | 2745 | filp->f_op->flock(filp, F_SETLKW, &fl); |
|---|
| .. | .. |
|---|
| 2531 | 2760 | if (list_empty(&ctx->flc_lease)) |
|---|
| 2532 | 2761 | return; |
|---|
| 2533 | 2762 | |
|---|
| 2534 | | - percpu_down_read_preempt_disable(&file_rwsem); |
|---|
| 2763 | + percpu_down_read(&file_rwsem); |
|---|
| 2535 | 2764 | spin_lock(&ctx->flc_lock); |
|---|
| 2536 | 2765 | list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) |
|---|
| 2537 | 2766 | if (filp == fl->fl_file) |
|---|
| 2538 | 2767 | lease_modify(fl, F_UNLCK, &dispose); |
|---|
| 2539 | 2768 | spin_unlock(&ctx->flc_lock); |
|---|
| 2540 | | - percpu_up_read_preempt_enable(&file_rwsem); |
|---|
| 2769 | + percpu_up_read(&file_rwsem); |
|---|
| 2541 | 2770 | |
|---|
| 2542 | 2771 | locks_dispose_list(&dispose); |
|---|
| 2543 | 2772 | } |
|---|
| .. | .. |
|---|
| 2570 | 2799 | } |
|---|
| 2571 | 2800 | |
|---|
| 2572 | 2801 | /** |
|---|
| 2573 | | - * posix_unblock_lock - stop waiting for a file lock |
|---|
| 2574 | | - * @waiter: the lock which was waiting |
|---|
| 2575 | | - * |
|---|
| 2576 | | - * lockd needs to block waiting for locks. |
|---|
| 2577 | | - */ |
|---|
| 2578 | | -int |
|---|
| 2579 | | -posix_unblock_lock(struct file_lock *waiter) |
|---|
| 2580 | | -{ |
|---|
| 2581 | | - int status = 0; |
|---|
| 2582 | | - |
|---|
| 2583 | | - spin_lock(&blocked_lock_lock); |
|---|
| 2584 | | - if (waiter->fl_next) |
|---|
| 2585 | | - __locks_delete_block(waiter); |
|---|
| 2586 | | - else |
|---|
| 2587 | | - status = -ENOENT; |
|---|
| 2588 | | - spin_unlock(&blocked_lock_lock); |
|---|
| 2589 | | - return status; |
|---|
| 2590 | | -} |
|---|
| 2591 | | -EXPORT_SYMBOL(posix_unblock_lock); |
|---|
| 2592 | | - |
|---|
| 2593 | | -/** |
|---|
| 2594 | 2802 | * vfs_cancel_lock - file byte range unblock lock |
|---|
| 2595 | 2803 | * @filp: The file to apply the unblock to |
|---|
| 2596 | 2804 | * @fl: The lock to be unblocked |
|---|
| .. | .. |
|---|
| 2603 | 2811 | return filp->f_op->lock(filp, F_CANCELLK, fl); |
|---|
| 2604 | 2812 | return 0; |
|---|
| 2605 | 2813 | } |
|---|
| 2606 | | - |
|---|
| 2607 | 2814 | EXPORT_SYMBOL_GPL(vfs_cancel_lock); |
|---|
| 2608 | 2815 | |
|---|
| 2609 | 2816 | #ifdef CONFIG_PROC_FS |
|---|
| .. | .. |
|---|
| 2620 | 2827 | { |
|---|
| 2621 | 2828 | struct inode *inode = NULL; |
|---|
| 2622 | 2829 | unsigned int fl_pid; |
|---|
| 2623 | | - struct pid_namespace *proc_pidns = file_inode(f->file)->i_sb->s_fs_info; |
|---|
| 2830 | + struct pid_namespace *proc_pidns = proc_pid_ns(file_inode(f->file)->i_sb); |
|---|
| 2624 | 2831 | |
|---|
| 2625 | 2832 | fl_pid = locks_translate_pid(fl, proc_pidns); |
|---|
| 2626 | 2833 | /* |
|---|
| .. | .. |
|---|
| 2671 | 2878 | ? (fl->fl_type & LOCK_WRITE) ? "RW " : "READ " |
|---|
| 2672 | 2879 | : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE "); |
|---|
| 2673 | 2880 | } else { |
|---|
| 2674 | | - seq_printf(f, "%s ", |
|---|
| 2675 | | - (lease_breaking(fl)) |
|---|
| 2676 | | - ? (fl->fl_type == F_UNLCK) ? "UNLCK" : "READ " |
|---|
| 2677 | | - : (fl->fl_type == F_WRLCK) ? "WRITE" : "READ "); |
|---|
| 2881 | + int type = IS_LEASE(fl) ? target_leasetype(fl) : fl->fl_type; |
|---|
| 2882 | + |
|---|
| 2883 | + seq_printf(f, "%s ", (type == F_WRLCK) ? "WRITE" : |
|---|
| 2884 | + (type == F_RDLCK) ? "READ" : "UNLCK"); |
|---|
| 2678 | 2885 | } |
|---|
| 2679 | 2886 | if (inode) { |
|---|
| 2680 | 2887 | /* userspace relies on this representation of dev_t */ |
|---|
| .. | .. |
|---|
| 2698 | 2905 | { |
|---|
| 2699 | 2906 | struct locks_iterator *iter = f->private; |
|---|
| 2700 | 2907 | struct file_lock *fl, *bfl; |
|---|
| 2701 | | - struct pid_namespace *proc_pidns = file_inode(f->file)->i_sb->s_fs_info; |
|---|
| 2908 | + struct pid_namespace *proc_pidns = proc_pid_ns(file_inode(f->file)->i_sb); |
|---|
| 2702 | 2909 | |
|---|
| 2703 | 2910 | fl = hlist_entry(v, struct file_lock, fl_link); |
|---|
| 2704 | 2911 | |
|---|
| .. | .. |
|---|
| 2707 | 2914 | |
|---|
| 2708 | 2915 | lock_get_status(f, fl, iter->li_pos, ""); |
|---|
| 2709 | 2916 | |
|---|
| 2710 | | - list_for_each_entry(bfl, &fl->fl_block, fl_block) |
|---|
| 2917 | + list_for_each_entry(bfl, &fl->fl_blocked_requests, fl_blocked_member) |
|---|
| 2711 | 2918 | lock_get_status(f, bfl, iter->li_pos, " ->"); |
|---|
| 2712 | 2919 | |
|---|
| 2713 | 2920 | return 0; |
|---|
| .. | .. |
|---|
| 2803 | 3010 | filelock_cache = kmem_cache_create("file_lock_cache", |
|---|
| 2804 | 3011 | sizeof(struct file_lock), 0, SLAB_PANIC, NULL); |
|---|
| 2805 | 3012 | |
|---|
| 2806 | | - |
|---|
| 2807 | 3013 | for_each_possible_cpu(i) { |
|---|
| 2808 | 3014 | struct file_lock_list_struct *fll = per_cpu_ptr(&file_lock_list, i); |
|---|
| 2809 | 3015 | |
|---|
| .. | .. |
|---|
| 2811 | 3017 | INIT_HLIST_HEAD(&fll->hlist); |
|---|
| 2812 | 3018 | } |
|---|
| 2813 | 3019 | |
|---|
| 3020 | + lease_notifier_chain_init(); |
|---|
| 2814 | 3021 | return 0; |
|---|
| 2815 | 3022 | } |
|---|
| 2816 | | - |
|---|
| 2817 | 3023 | core_initcall(filelock_init); |
|---|