Commit MetaInfo

Revision00c569b567c7f1f0da6162868fd02a9f29411805 (tree)
Time2018-12-28 10:12:30
AuthorLinus Torvalds <torvalds@linu...>
CommiterLinus Torvalds

Log Message

File locking changes for v4.21
-----BEGIN PGP SIGNATURE-----

iQIcBAABAgAGBQJcILryAAoJEAAOaEEZVoIVByMP/RYty0dsV9ALt0PKqxKyDVT7
9KOGA73JIahpeO2dnlIiZAXdeC3Y350UH2axrX6Xy/X6ktTCca3X/xqZMn/wK5kt
zeMeqcjsyTz4wJoOCWUm7nsteMALfusDAIMd8axEnBFgWk9nsQwgh+jS1gYVj2D1
GUSdCceKcQ0ZOSsZDzDFgd/R34dNMobvYd1aOE2bgHL19BAXj1aKIv81yAjjY41A
D+VVvEyzIHQUtxmWk1X3X8kYfhHMu4X5AQhhqPxw8jw8CC0w0lfQTOZe/ApeMfxZ
BFhusMdwf8QhkGWMOfhOTldTm3GobBmdsNX5HmukvcAEjDVPiOLCiKXaLWEBJ68r
HbmB3YyxzT2re0PfSa72WIu6W8aKZHUny+BLgTHiuW3KIV1khJK4AflWKMLfe8yi
0xUWm0SeiwMCdBLtkD8IykC19LBCAKM15JBxpUHadBvBsO9G+54DfzRImjnxSjAH
tX0RnFWmA7hXt02fZjcywT+/+n84kt0lbjdJAKXK6tl0DbGxlEj8YoJVBNG/p94q
ARHgBKMFTrsDPJiMY5WSobHapjmnSNDBk6uSf6TPe2E5wmnhLcwfTqwCdnpocW6c
A7CX+q/Jfyk1oNEGajXXyIsjaDc2Ma0Fs/mfb3o5Lhkjq68e+LT0+eIjn4veuY5H
KoZ/KVBGs4Uxy81qMPkl
=sUSp
-----END PGP SIGNATURE-----

Merge tag 'locks-v4.21-1' of git://git.kernel.org/pub/scm/linux/kernel/git/jlayton/linux

Pull file locking updates from Jeff Layton:

"The main change in this set is Neil Brown's work to reduce the
thundering herd problem when a heavily-contended file lock is
released.
Previously we'd always wake up all waiters when this occurred. With
this set, we'll now we only wake up waiters that were blocked on the
range being released"

* tag 'locks-v4.21-1' of git://git.kernel.org/pub/scm/linux/kernel/git/jlayton/linux:

locks: Use inode_is_open_for_write
fs/locks: remove unnecessary white space.
fs/locks: merge posix_unblock_lock() and locks_delete_block()
fs/locks: create a tree of dependent requests.
fs/locks: change all *_conflict() functions to return bool.
fs/locks: always delete_block after waiting.
fs/locks: allow a lock request to block other requests.
fs/locks: use properly initialized file_lock when unlocking.
ocfs2: properly initial file_lock used for unlock.
gfs2: properly initial file_lock used for unlock.
NFS: use locks_copy_lock() to copy locks.
fs/locks: split out locks_wake_up_blocks().
fs/locks: rename some lists and pointers.

Change Summary

Incremental Difference

--- a/fs/cifs/file.c
+++ b/fs/cifs/file.c
@@ -1103,10 +1103,10 @@ try_again:
11031103 rc = posix_lock_file(file, flock, NULL);
11041104 up_write(&cinode->lock_sem);
11051105 if (rc == FILE_LOCK_DEFERRED) {
1106- rc = wait_event_interruptible(flock->fl_wait, !flock->fl_next);
1106+ rc = wait_event_interruptible(flock->fl_wait, !flock->fl_blocker);
11071107 if (!rc)
11081108 goto try_again;
1109- posix_unblock_lock(flock);
1109+ locks_delete_block(flock);
11101110 }
11111111 return rc;
11121112 }
--- a/fs/gfs2/file.c
+++ b/fs/gfs2/file.c
@@ -1199,13 +1199,13 @@ static int do_flock(struct file *file, int cmd, struct file_lock *fl)
11991199 mutex_lock(&fp->f_fl_mutex);
12001200
12011201 if (gfs2_holder_initialized(fl_gh)) {
1202+ struct file_lock request;
12021203 if (fl_gh->gh_state == state)
12031204 goto out;
1204- locks_lock_file_wait(file,
1205- &(struct file_lock) {
1206- .fl_type = F_UNLCK,
1207- .fl_flags = FL_FLOCK
1208- });
1205+ locks_init_lock(&request);
1206+ request.fl_type = F_UNLCK;
1207+ request.fl_flags = FL_FLOCK;
1208+ locks_lock_file_wait(file, &request);
12091209 gfs2_glock_dq(fl_gh);
12101210 gfs2_holder_reinit(state, flags, fl_gh);
12111211 } else {
--- a/fs/lockd/svclock.c
+++ b/fs/lockd/svclock.c
@@ -276,7 +276,7 @@ static int nlmsvc_unlink_block(struct nlm_block *block)
276276 dprintk("lockd: unlinking block %p...\n", block);
277277
278278 /* Remove block from list */
279- status = posix_unblock_lock(&block->b_call->a_args.lock.fl);
279+ status = locks_delete_block(&block->b_call->a_args.lock.fl);
280280 nlmsvc_remove_block(block);
281281 return status;
282282 }
--- a/fs/locks.c
+++ b/fs/locks.c
@@ -11,11 +11,11 @@
1111 *
1212 * Miscellaneous edits, and a total rewrite of posix_lock_file() code.
1313 * Kai Petzke (wpp@marie.physik.tu-berlin.de), 1994
14- *
14+ *
1515 * Converted file_lock_table to a linked list from an array, which eliminates
1616 * the limits on how many active file locks are open.
1717 * Chad Page (pageone@netcom.com), November 27, 1994
18- *
18+ *
1919 * Removed dependency on file descriptors. dup()'ed file descriptors now
2020 * get the same locks as the original file descriptors, and a close() on
2121 * any file descriptor removes ALL the locks on the file for the current
@@ -41,7 +41,7 @@
4141 * with a file pointer (filp). As a result they can be shared by a parent
4242 * process and its children after a fork(). They are removed when the last
4343 * file descriptor referring to the file pointer is closed (unless explicitly
44- * unlocked).
44+ * unlocked).
4545 *
4646 * FL_FLOCK locks never deadlock, an existing lock is always removed before
4747 * upgrading from shared to exclusive (or vice versa). When this happens
@@ -50,7 +50,7 @@
5050 * Andy Walker (andy@lysaker.kvaerner.no), June 09, 1995
5151 *
5252 * Removed some race conditions in flock_lock_file(), marked other possible
53- * races. Just grep for FIXME to see them.
53+ * races. Just grep for FIXME to see them.
5454 * Dmitry Gorodchanin (pgmdsg@ibi.com), February 09, 1996.
5555 *
5656 * Addressed Dmitry's concerns. Deadlock checking no longer recursive.
@@ -112,6 +112,46 @@
112112 * Leases and LOCK_MAND
113113 * Matthew Wilcox <willy@debian.org>, June, 2000.
114114 * Stephen Rothwell <sfr@canb.auug.org.au>, June, 2000.
115+ *
116+ * Locking conflicts and dependencies:
117+ * If multiple threads attempt to lock the same byte (or flock the same file)
118+ * only one can be granted the lock, and other must wait their turn.
119+ * The first lock has been "applied" or "granted", the others are "waiting"
120+ * and are "blocked" by the "applied" lock..
121+ *
122+ * Waiting and applied locks are all kept in trees whose properties are:
123+ *
124+ * - the root of a tree may be an applied or waiting lock.
125+ * - every other node in the tree is a waiting lock that
126+ * conflicts with every ancestor of that node.
127+ *
128+ * Every such tree begins life as a waiting singleton which obviously
129+ * satisfies the above properties.
130+ *
131+ * The only ways we modify trees preserve these properties:
132+ *
133+ * 1. We may add a new leaf node, but only after first verifying that it
134+ * conflicts with all of its ancestors.
135+ * 2. We may remove the root of a tree, creating a new singleton
136+ * tree from the root and N new trees rooted in the immediate
137+ * children.
138+ * 3. If the root of a tree is not currently an applied lock, we may
139+ * apply it (if possible).
140+ * 4. We may upgrade the root of the tree (either extend its range,
141+ * or upgrade its entire range from read to write).
142+ *
143+ * When an applied lock is modified in a way that reduces or downgrades any
144+ * part of its range, we remove all its children (2 above). This particularly
145+ * happens when a lock is unlocked.
146+ *
147+ * For each of those child trees we "wake up" the thread which is
148+ * waiting for the lock so it can continue handling as follows: if the
149+ * root of the tree applies, we do so (3). If it doesn't, it must
150+ * conflict with some applied lock. We remove (wake up) all of its children
151+ * (2), and add it is a new leaf to the tree rooted in the applied
152+ * lock (1). We then repeat the process recursively with those
153+ * children.
154+ *
115155 */
116156
117157 #include <linux/capability.h>
@@ -189,9 +229,9 @@ static DEFINE_HASHTABLE(blocked_hash, BLOCKED_HASH_BITS);
189229 * This lock protects the blocked_hash. Generally, if you're accessing it, you
190230 * want to be holding this lock.
191231 *
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).
232+ * In addition, it also protects the fl->fl_blocked_requests list, and the
233+ * fl->fl_blocker pointer for file_lock structures that are acting as lock
234+ * requests (in contrast to those that are acting as records of acquired locks).
195235 *
196236 * Note that when we acquire this lock in order to change the above fields,
197237 * we often hold the flc_lock as well. In certain cases, when reading the fields
@@ -293,7 +333,8 @@ static void locks_init_lock_heads(struct file_lock *fl)
293333 {
294334 INIT_HLIST_NODE(&fl->fl_link);
295335 INIT_LIST_HEAD(&fl->fl_list);
296- INIT_LIST_HEAD(&fl->fl_block);
336+ INIT_LIST_HEAD(&fl->fl_blocked_requests);
337+ INIT_LIST_HEAD(&fl->fl_blocked_member);
297338 init_waitqueue_head(&fl->fl_wait);
298339 }
299340
@@ -332,7 +373,8 @@ void locks_free_lock(struct file_lock *fl)
332373 {
333374 BUG_ON(waitqueue_active(&fl->fl_wait));
334375 BUG_ON(!list_empty(&fl->fl_list));
335- BUG_ON(!list_empty(&fl->fl_block));
376+ BUG_ON(!list_empty(&fl->fl_blocked_requests));
377+ BUG_ON(!list_empty(&fl->fl_blocked_member));
336378 BUG_ON(!hlist_unhashed(&fl->fl_link));
337379
338380 locks_release_private(fl);
@@ -357,7 +399,6 @@ void locks_init_lock(struct file_lock *fl)
357399 memset(fl, 0, sizeof(struct file_lock));
358400 locks_init_lock_heads(fl);
359401 }
360-
361402 EXPORT_SYMBOL(locks_init_lock);
362403
363404 /*
@@ -397,9 +438,26 @@ void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
397438 fl->fl_ops->fl_copy_lock(new, fl);
398439 }
399440 }
400-
401441 EXPORT_SYMBOL(locks_copy_lock);
402442
443+static void locks_move_blocks(struct file_lock *new, struct file_lock *fl)
444+{
445+ struct file_lock *f;
446+
447+ /*
448+ * As ctx->flc_lock is held, new requests cannot be added to
449+ * ->fl_blocked_requests, so we don't need a lock to check if it
450+ * is empty.
451+ */
452+ if (list_empty(&fl->fl_blocked_requests))
453+ return;
454+ spin_lock(&blocked_lock_lock);
455+ list_splice_init(&fl->fl_blocked_requests, &new->fl_blocked_requests);
456+ list_for_each_entry(f, &fl->fl_blocked_requests, fl_blocked_member)
457+ f->fl_blocker = new;
458+ spin_unlock(&blocked_lock_lock);
459+}
460+
403461 static inline int flock_translate_cmd(int cmd) {
404462 if (cmd & LOCK_MAND)
405463 return cmd & (LOCK_MAND | LOCK_RW);
@@ -416,17 +474,20 @@ static inline int flock_translate_cmd(int cmd) {
416474
417475 /* Fill in a file_lock structure with an appropriate FLOCK lock. */
418476 static struct file_lock *
419-flock_make_lock(struct file *filp, unsigned int cmd)
477+flock_make_lock(struct file *filp, unsigned int cmd, struct file_lock *fl)
420478 {
421- struct file_lock *fl;
422479 int type = flock_translate_cmd(cmd);
423480
424481 if (type < 0)
425482 return ERR_PTR(type);
426-
427- fl = locks_alloc_lock();
428- if (fl == NULL)
429- return ERR_PTR(-ENOMEM);
483+
484+ if (fl == NULL) {
485+ fl = locks_alloc_lock();
486+ if (fl == NULL)
487+ return ERR_PTR(-ENOMEM);
488+ } else {
489+ locks_init_lock(fl);
490+ }
430491
431492 fl->fl_file = filp;
432493 fl->fl_owner = filp;
@@ -434,7 +495,7 @@ flock_make_lock(struct file *filp, unsigned int cmd)
434495 fl->fl_flags = FL_FLOCK;
435496 fl->fl_type = type;
436497 fl->fl_end = OFFSET_MAX;
437-
498+
438499 return fl;
439500 }
440501
@@ -666,16 +727,58 @@ static void locks_delete_global_blocked(struct file_lock *waiter)
666727 static void __locks_delete_block(struct file_lock *waiter)
667728 {
668729 locks_delete_global_blocked(waiter);
669- list_del_init(&waiter->fl_block);
670- waiter->fl_next = NULL;
730+ list_del_init(&waiter->fl_blocked_member);
731+ waiter->fl_blocker = NULL;
671732 }
672733
673-static void locks_delete_block(struct file_lock *waiter)
734+static void __locks_wake_up_blocks(struct file_lock *blocker)
674735 {
736+ while (!list_empty(&blocker->fl_blocked_requests)) {
737+ struct file_lock *waiter;
738+
739+ waiter = list_first_entry(&blocker->fl_blocked_requests,
740+ struct file_lock, fl_blocked_member);
741+ __locks_delete_block(waiter);
742+ if (waiter->fl_lmops && waiter->fl_lmops->lm_notify)
743+ waiter->fl_lmops->lm_notify(waiter);
744+ else
745+ wake_up(&waiter->fl_wait);
746+ }
747+}
748+
749+/**
750+ * locks_delete_lock - stop waiting for a file lock
751+ * @waiter: the lock which was waiting
752+ *
753+ * lockd/nfsd need to disconnect the lock while working on it.
754+ */
755+int locks_delete_block(struct file_lock *waiter)
756+{
757+ int status = -ENOENT;
758+
759+ /*
760+ * If fl_blocker is NULL, it won't be set again as this thread
761+ * "owns" the lock and is the only one that might try to claim
762+ * the lock. So it is safe to test fl_blocker locklessly.
763+ * Also if fl_blocker is NULL, this waiter is not listed on
764+ * fl_blocked_requests for some lock, so no other request can
765+ * be added to the list of fl_blocked_requests for this
766+ * request. So if fl_blocker is NULL, it is safe to
767+ * locklessly check if fl_blocked_requests is empty. If both
768+ * of these checks succeed, there is no need to take the lock.
769+ */
770+ if (waiter->fl_blocker == NULL &&
771+ list_empty(&waiter->fl_blocked_requests))
772+ return status;
675773 spin_lock(&blocked_lock_lock);
774+ if (waiter->fl_blocker)
775+ status = 0;
776+ __locks_wake_up_blocks(waiter);
676777 __locks_delete_block(waiter);
677778 spin_unlock(&blocked_lock_lock);
779+ return status;
678780 }
781+EXPORT_SYMBOL(locks_delete_block);
679782
680783 /* Insert waiter into blocker's block list.
681784 * We use a circular list so that processes can be easily woken up in
@@ -683,26 +786,49 @@ static void locks_delete_block(struct file_lock *waiter)
683786 * it seems like the reasonable thing to do.
684787 *
685788 * 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.
789+ * fl_blocked_requests list itself is protected by the blocked_lock_lock,
790+ * but by ensuring that the flc_lock is also held on insertions we can avoid
791+ * taking the blocked_lock_lock in some cases when we see that the
792+ * fl_blocked_requests list is empty.
793+ *
794+ * Rather than just adding to the list, we check for conflicts with any existing
795+ * waiters, and add beneath any waiter that blocks the new waiter.
796+ * Thus wakeups don't happen until needed.
689797 */
690798 static void __locks_insert_block(struct file_lock *blocker,
691- struct file_lock *waiter)
799+ struct file_lock *waiter,
800+ bool conflict(struct file_lock *,
801+ struct file_lock *))
692802 {
693- BUG_ON(!list_empty(&waiter->fl_block));
694- waiter->fl_next = blocker;
695- list_add_tail(&waiter->fl_block, &blocker->fl_block);
803+ struct file_lock *fl;
804+ BUG_ON(!list_empty(&waiter->fl_blocked_member));
805+
806+new_blocker:
807+ list_for_each_entry(fl, &blocker->fl_blocked_requests, fl_blocked_member)
808+ if (conflict(fl, waiter)) {
809+ blocker = fl;
810+ goto new_blocker;
811+ }
812+ waiter->fl_blocker = blocker;
813+ list_add_tail(&waiter->fl_blocked_member, &blocker->fl_blocked_requests);
696814 if (IS_POSIX(blocker) && !IS_OFDLCK(blocker))
697815 locks_insert_global_blocked(waiter);
816+
817+ /* The requests in waiter->fl_blocked are known to conflict with
818+ * waiter, but might not conflict with blocker, or the requests
819+ * and lock which block it. So they all need to be woken.
820+ */
821+ __locks_wake_up_blocks(waiter);
698822 }
699823
700824 /* Must be called with flc_lock held. */
701825 static void locks_insert_block(struct file_lock *blocker,
702- struct file_lock *waiter)
826+ struct file_lock *waiter,
827+ bool conflict(struct file_lock *,
828+ struct file_lock *))
703829 {
704830 spin_lock(&blocked_lock_lock);
705- __locks_insert_block(blocker, waiter);
831+ __locks_insert_block(blocker, waiter, conflict);
706832 spin_unlock(&blocked_lock_lock);
707833 }
708834
@@ -716,25 +842,15 @@ static void locks_wake_up_blocks(struct file_lock *blocker)
716842 /*
717843 * Avoid taking global lock if list is empty. This is safe since new
718844 * 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.
845+ * the flc_lock is always held here. Note that removal from the
846+ * fl_blocked_requests list does not require the flc_lock, so we must
847+ * recheck list_empty() after acquiring the blocked_lock_lock.
722848 */
723- if (list_empty(&blocker->fl_block))
849+ if (list_empty(&blocker->fl_blocked_requests))
724850 return;
725851
726852 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- }
853+ __locks_wake_up_blocks(blocker);
738854 spin_unlock(&blocked_lock_lock);
739855 }
740856
@@ -766,47 +882,50 @@ locks_delete_lock_ctx(struct file_lock *fl, struct list_head *dispose)
766882 /* Determine if lock sys_fl blocks lock caller_fl. Common functionality
767883 * checks for shared/exclusive status of overlapping locks.
768884 */
769-static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
885+static bool locks_conflict(struct file_lock *caller_fl,
886+ struct file_lock *sys_fl)
770887 {
771888 if (sys_fl->fl_type == F_WRLCK)
772- return 1;
889+ return true;
773890 if (caller_fl->fl_type == F_WRLCK)
774- return 1;
775- return 0;
891+ return true;
892+ return false;
776893 }
777894
778895 /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
779896 * checking before calling the locks_conflict().
780897 */
781-static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
898+static bool posix_locks_conflict(struct file_lock *caller_fl,
899+ struct file_lock *sys_fl)
782900 {
783901 /* POSIX locks owned by the same process do not conflict with
784902 * each other.
785903 */
786904 if (posix_same_owner(caller_fl, sys_fl))
787- return (0);
905+ return false;
788906
789907 /* Check whether they overlap */
790908 if (!locks_overlap(caller_fl, sys_fl))
791- return 0;
909+ return false;
792910
793- return (locks_conflict(caller_fl, sys_fl));
911+ return locks_conflict(caller_fl, sys_fl);
794912 }
795913
796914 /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
797915 * checking before calling the locks_conflict().
798916 */
799-static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
917+static bool flock_locks_conflict(struct file_lock *caller_fl,
918+ struct file_lock *sys_fl)
800919 {
801920 /* FLOCK locks referring to the same filp do not conflict with
802921 * each other.
803922 */
804923 if (caller_fl->fl_file == sys_fl->fl_file)
805- return (0);
924+ return false;
806925 if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND))
807- return 0;
926+ return false;
808927
809- return (locks_conflict(caller_fl, sys_fl));
928+ return locks_conflict(caller_fl, sys_fl);
810929 }
811930
812931 void
@@ -877,8 +996,11 @@ static struct file_lock *what_owner_is_waiting_for(struct file_lock *block_fl)
877996 struct file_lock *fl;
878997
879998 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;
999+ if (posix_same_owner(fl, block_fl)) {
1000+ while (fl->fl_blocker)
1001+ fl = fl->fl_blocker;
1002+ return fl;
1003+ }
8821004 }
8831005 return NULL;
8841006 }
@@ -965,12 +1087,13 @@ find_conflict:
9651087 if (!(request->fl_flags & FL_SLEEP))
9661088 goto out;
9671089 error = FILE_LOCK_DEFERRED;
968- locks_insert_block(fl, request);
1090+ locks_insert_block(fl, request, flock_locks_conflict);
9691091 goto out;
9701092 }
9711093 if (request->fl_flags & FL_ACCESS)
9721094 goto out;
9731095 locks_copy_lock(new_fl, request);
1096+ locks_move_blocks(new_fl, request);
9741097 locks_insert_lock_ctx(new_fl, &ctx->flc_flock);
9751098 new_fl = NULL;
9761099 error = 0;
@@ -1039,12 +1162,13 @@ static int posix_lock_inode(struct inode *inode, struct file_lock *request,
10391162 spin_lock(&blocked_lock_lock);
10401163 if (likely(!posix_locks_deadlock(request, fl))) {
10411164 error = FILE_LOCK_DEFERRED;
1042- __locks_insert_block(fl, request);
1165+ __locks_insert_block(fl, request,
1166+ posix_locks_conflict);
10431167 }
10441168 spin_unlock(&blocked_lock_lock);
10451169 goto out;
1046- }
1047- }
1170+ }
1171+ }
10481172
10491173 /* If we're just looking for a conflict, we're done. */
10501174 error = 0;
@@ -1164,6 +1288,7 @@ static int posix_lock_inode(struct inode *inode, struct file_lock *request,
11641288 goto out;
11651289 }
11661290 locks_copy_lock(new_fl, request);
1291+ locks_move_blocks(new_fl, request);
11671292 locks_insert_lock_ctx(new_fl, &fl->fl_list);
11681293 fl = new_fl;
11691294 new_fl = NULL;
@@ -1237,13 +1362,11 @@ static int posix_lock_inode_wait(struct inode *inode, struct file_lock *fl)
12371362 error = posix_lock_inode(inode, fl, NULL);
12381363 if (error != FILE_LOCK_DEFERRED)
12391364 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;
1365+ error = wait_event_interruptible(fl->fl_wait, !fl->fl_blocker);
1366+ if (error)
1367+ break;
12461368 }
1369+ locks_delete_block(fl);
12471370 return error;
12481371 }
12491372
@@ -1324,7 +1447,7 @@ int locks_mandatory_area(struct inode *inode, struct file *filp, loff_t start,
13241447 error = posix_lock_inode(inode, &fl, NULL);
13251448 if (error != FILE_LOCK_DEFERRED)
13261449 break;
1327- error = wait_event_interruptible(fl.fl_wait, !fl.fl_next);
1450+ error = wait_event_interruptible(fl.fl_wait, !fl.fl_blocker);
13281451 if (!error) {
13291452 /*
13301453 * If we've been sleeping someone might have
@@ -1334,13 +1457,12 @@ int locks_mandatory_area(struct inode *inode, struct file *filp, loff_t start,
13341457 continue;
13351458 }
13361459
1337- locks_delete_block(&fl);
13381460 break;
13391461 }
1462+ locks_delete_block(&fl);
13401463
13411464 return error;
13421465 }
1343-
13441466 EXPORT_SYMBOL(locks_mandatory_area);
13451467 #endif /* CONFIG_MANDATORY_FILE_LOCKING */
13461468
@@ -1511,14 +1633,14 @@ restart:
15111633 break_time -= jiffies;
15121634 if (break_time == 0)
15131635 break_time++;
1514- locks_insert_block(fl, new_fl);
1636+ locks_insert_block(fl, new_fl, leases_conflict);
15151637 trace_break_lease_block(inode, new_fl);
15161638 spin_unlock(&ctx->flc_lock);
15171639 percpu_up_read_preempt_enable(&file_rwsem);
15181640
15191641 locks_dispose_list(&dispose);
15201642 error = wait_event_interruptible_timeout(new_fl->fl_wait,
1521- !new_fl->fl_next, break_time);
1643+ !new_fl->fl_blocker, break_time);
15221644
15231645 percpu_down_read_preempt_disable(&file_rwsem);
15241646 spin_lock(&ctx->flc_lock);
@@ -1542,7 +1664,6 @@ out:
15421664 locks_free_lock(new_fl);
15431665 return error;
15441666 }
1545-
15461667 EXPORT_SYMBOL(__break_lease);
15471668
15481669 /**
@@ -1573,7 +1694,6 @@ void lease_get_mtime(struct inode *inode, struct timespec64 *time)
15731694 if (has_lease)
15741695 *time = current_time(inode);
15751696 }
1576-
15771697 EXPORT_SYMBOL(lease_get_mtime);
15781698
15791699 /**
@@ -1628,8 +1748,8 @@ int fcntl_getlease(struct file *filp)
16281748
16291749 /**
16301750 * 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.
1751+ * an existing open that would conflict with the
1752+ * desired lease.
16331753 * @dentry: dentry to check
16341754 * @arg: type of lease that we're trying to acquire
16351755 * @flags: current lock flags
@@ -1646,7 +1766,7 @@ check_conflicting_open(const struct dentry *dentry, const long arg, int flags)
16461766 if (flags & FL_LAYOUT)
16471767 return 0;
16481768
1649- if ((arg == F_RDLCK) && (atomic_read(&inode->i_writecount) > 0))
1769+ if ((arg == F_RDLCK) && inode_is_open_for_write(inode))
16501770 return -EAGAIN;
16511771
16521772 if ((arg == F_WRLCK) && ((d_count(dentry) > 1) ||
@@ -1853,7 +1973,7 @@ EXPORT_SYMBOL(generic_setlease);
18531973 * @arg: type of lease to obtain
18541974 * @lease: file_lock to use when adding a lease
18551975 * @priv: private info for lm_setup when adding a lease (may be
1856- * NULL if lm_setup doesn't require it)
1976+ * NULL if lm_setup doesn't require it)
18571977 *
18581978 * Call this to establish a lease on the file. The "lease" argument is not
18591979 * used for F_UNLCK requests and may be NULL. For commands that set or alter
@@ -1931,13 +2051,11 @@ static int flock_lock_inode_wait(struct inode *inode, struct file_lock *fl)
19312051 error = flock_lock_inode(inode, fl);
19322052 if (error != FILE_LOCK_DEFERRED)
19332053 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;
2054+ error = wait_event_interruptible(fl->fl_wait, !fl->fl_blocker);
2055+ if (error)
2056+ break;
19402057 }
2058+ locks_delete_block(fl);
19412059 return error;
19422060 }
19432061
@@ -2001,7 +2119,7 @@ SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
20012119 !(f.file->f_mode & (FMODE_READ|FMODE_WRITE)))
20022120 goto out_putf;
20032121
2004- lock = flock_make_lock(f.file, cmd);
2122+ lock = flock_make_lock(f.file, cmd, NULL);
20052123 if (IS_ERR(lock)) {
20062124 error = PTR_ERR(lock);
20072125 goto out_putf;
@@ -2143,7 +2261,7 @@ int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock *flock)
21432261 error = vfs_test_lock(filp, fl);
21442262 if (error)
21452263 goto out;
2146-
2264+
21472265 flock->l_type = fl->fl_type;
21482266 if (fl->fl_type != F_UNLCK) {
21492267 error = posix_lock_to_flock(flock, fl);
@@ -2210,13 +2328,11 @@ static int do_lock_file_wait(struct file *filp, unsigned int cmd,
22102328 error = vfs_lock_file(filp, cmd, fl, NULL);
22112329 if (error != FILE_LOCK_DEFERRED)
22122330 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;
2331+ error = wait_event_interruptible(fl->fl_wait, !fl->fl_blocker);
2332+ if (error)
2333+ break;
22192334 }
2335+ locks_delete_block(fl);
22202336
22212337 return error;
22222338 }
@@ -2476,6 +2592,7 @@ void locks_remove_posix(struct file *filp, fl_owner_t owner)
24762592 if (!ctx || list_empty(&ctx->flc_posix))
24772593 return;
24782594
2595+ locks_init_lock(&lock);
24792596 lock.fl_type = F_UNLCK;
24802597 lock.fl_flags = FL_POSIX | FL_CLOSE;
24812598 lock.fl_start = 0;
@@ -2492,26 +2609,21 @@ void locks_remove_posix(struct file *filp, fl_owner_t owner)
24922609 lock.fl_ops->fl_release_private(&lock);
24932610 trace_locks_remove_posix(inode, &lock, error);
24942611 }
2495-
24962612 EXPORT_SYMBOL(locks_remove_posix);
24972613
24982614 /* The i_flctx must be valid when calling into here */
24992615 static void
25002616 locks_remove_flock(struct file *filp, struct file_lock_context *flctx)
25012617 {
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- };
2618+ struct file_lock fl;
25102619 struct inode *inode = locks_inode(filp);
25112620
25122621 if (list_empty(&flctx->flc_flock))
25132622 return;
25142623
2624+ flock_make_lock(filp, LOCK_UN, &fl);
2625+ fl.fl_flags |= FL_CLOSE;
2626+
25152627 if (filp->f_op->flock)
25162628 filp->f_op->flock(filp, F_SETLKW, &fl);
25172629 else
@@ -2570,27 +2682,6 @@ void locks_remove_file(struct file *filp)
25702682 }
25712683
25722684 /**
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-/**
25942685 * vfs_cancel_lock - file byte range unblock lock
25952686 * @filp: The file to apply the unblock to
25962687 * @fl: The lock to be unblocked
@@ -2603,7 +2694,6 @@ int vfs_cancel_lock(struct file *filp, struct file_lock *fl)
26032694 return filp->f_op->lock(filp, F_CANCELLK, fl);
26042695 return 0;
26052696 }
2606-
26072697 EXPORT_SYMBOL_GPL(vfs_cancel_lock);
26082698
26092699 #ifdef CONFIG_PROC_FS
@@ -2707,7 +2797,7 @@ static int locks_show(struct seq_file *f, void *v)
27072797
27082798 lock_get_status(f, fl, iter->li_pos, "");
27092799
2710- list_for_each_entry(bfl, &fl->fl_block, fl_block)
2800+ list_for_each_entry(bfl, &fl->fl_blocked_requests, fl_blocked_member)
27112801 lock_get_status(f, bfl, iter->li_pos, " ->");
27122802
27132803 return 0;
@@ -2803,7 +2893,6 @@ static int __init filelock_init(void)
28032893 filelock_cache = kmem_cache_create("file_lock_cache",
28042894 sizeof(struct file_lock), 0, SLAB_PANIC, NULL);
28052895
2806-
28072896 for_each_possible_cpu(i) {
28082897 struct file_lock_list_struct *fll = per_cpu_ptr(&file_lock_list, i);
28092898
@@ -2813,5 +2902,4 @@ static int __init filelock_init(void)
28132902
28142903 return 0;
28152904 }
2816-
28172905 core_initcall(filelock_init);
--- a/fs/nfs/nfs4proc.c
+++ b/fs/nfs/nfs4proc.c
@@ -6311,7 +6311,8 @@ static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
63116311 /* Ensure we don't close file until we're done freeing locks! */
63126312 p->ctx = get_nfs_open_context(ctx);
63136313 p->l_ctx = nfs_get_lock_context(ctx);
6314- memcpy(&p->fl, fl, sizeof(p->fl));
6314+ locks_init_lock(&p->fl);
6315+ locks_copy_lock(&p->fl, fl);
63156316 p->server = NFS_SERVER(inode);
63166317 return p;
63176318 }
@@ -6533,7 +6534,8 @@ static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
65336534 p->server = server;
65346535 refcount_inc(&lsp->ls_count);
65356536 p->ctx = get_nfs_open_context(ctx);
6536- memcpy(&p->fl, fl, sizeof(p->fl));
6537+ locks_init_lock(&p->fl);
6538+ locks_copy_lock(&p->fl, fl);
65376539 return p;
65386540 out_free_seqid:
65396541 nfs_free_seqid(p->arg.open_seqid);
--- a/fs/nfsd/nfs4state.c
+++ b/fs/nfsd/nfs4state.c
@@ -238,7 +238,7 @@ find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
238238 }
239239 spin_unlock(&nn->blocked_locks_lock);
240240 if (found)
241- posix_unblock_lock(&found->nbl_lock);
241+ locks_delete_block(&found->nbl_lock);
242242 return found;
243243 }
244244
@@ -293,7 +293,7 @@ remove_blocked_locks(struct nfs4_lockowner *lo)
293293 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
294294 nbl_lru);
295295 list_del_init(&nbl->nbl_lru);
296- posix_unblock_lock(&nbl->nbl_lock);
296+ locks_delete_block(&nbl->nbl_lock);
297297 free_blocked_lock(nbl);
298298 }
299299 }
@@ -4863,7 +4863,7 @@ nfs4_laundromat(struct nfsd_net *nn)
48634863 nbl = list_first_entry(&reaplist,
48644864 struct nfsd4_blocked_lock, nbl_lru);
48654865 list_del_init(&nbl->nbl_lru);
4866- posix_unblock_lock(&nbl->nbl_lock);
4866+ locks_delete_block(&nbl->nbl_lock);
48674867 free_blocked_lock(nbl);
48684868 }
48694869 out:
--- a/fs/ocfs2/locks.c
+++ b/fs/ocfs2/locks.c
@@ -52,6 +52,7 @@ static int ocfs2_do_flock(struct file *file, struct inode *inode,
5252 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
5353 lockres->l_level > LKM_NLMODE) {
5454 int old_level = 0;
55+ struct file_lock request;
5556
5657 if (lockres->l_level == LKM_EXMODE)
5758 old_level = 1;
@@ -66,11 +67,10 @@ static int ocfs2_do_flock(struct file *file, struct inode *inode,
6667 * level.
6768 */
6869
69- locks_lock_file_wait(file,
70- &(struct file_lock) {
71- .fl_type = F_UNLCK,
72- .fl_flags = FL_FLOCK
73- });
70+ locks_init_lock(&request);
71+ request.fl_type = F_UNLCK;
72+ request.fl_flags = FL_FLOCK;
73+ locks_lock_file_wait(file, &request);
7474
7575 ocfs2_file_unlock(file);
7676 }
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -1044,10 +1044,15 @@ bool opens_in_grace(struct net *);
10441044 * Obviously, the last two criteria only matter for POSIX locks.
10451045 */
10461046 struct file_lock {
1047- struct file_lock *fl_next; /* singly linked list for this inode */
1047+ struct file_lock *fl_blocker; /* The lock, that is blocking us */
10481048 struct list_head fl_list; /* link into file_lock_context */
10491049 struct hlist_node fl_link; /* node in global lists */
1050- struct list_head fl_block; /* circular list of blocked processes */
1050+ struct list_head fl_blocked_requests; /* list of requests with
1051+ * ->fl_blocker pointing here
1052+ */
1053+ struct list_head fl_blocked_member; /* node in
1054+ * ->fl_blocker->fl_blocked_requests
1055+ */
10511056 fl_owner_t fl_owner;
10521057 unsigned int fl_flags;
10531058 unsigned char fl_type;
@@ -1119,7 +1124,7 @@ extern void locks_remove_file(struct file *);
11191124 extern void locks_release_private(struct file_lock *);
11201125 extern void posix_test_lock(struct file *, struct file_lock *);
11211126 extern int posix_lock_file(struct file *, struct file_lock *, struct file_lock *);
1122-extern int posix_unblock_lock(struct file_lock *);
1127+extern int locks_delete_block(struct file_lock *);
11231128 extern int vfs_test_lock(struct file *, struct file_lock *);
11241129 extern int vfs_lock_file(struct file *, unsigned int, struct file_lock *, struct file_lock *);
11251130 extern int vfs_cancel_lock(struct file *filp, struct file_lock *fl);
@@ -1209,7 +1214,7 @@ static inline int posix_lock_file(struct file *filp, struct file_lock *fl,
12091214 return -ENOLCK;
12101215 }
12111216
1212-static inline int posix_unblock_lock(struct file_lock *waiter)
1217+static inline int locks_delete_block(struct file_lock *waiter)
12131218 {
12141219 return -ENOENT;
12151220 }
--- a/include/trace/events/filelock.h
+++ b/include/trace/events/filelock.h
@@ -68,7 +68,7 @@ DECLARE_EVENT_CLASS(filelock_lock,
6868 __field(struct file_lock *, fl)
6969 __field(unsigned long, i_ino)
7070 __field(dev_t, s_dev)
71- __field(struct file_lock *, fl_next)
71+ __field(struct file_lock *, fl_blocker)
7272 __field(fl_owner_t, fl_owner)
7373 __field(unsigned int, fl_pid)
7474 __field(unsigned int, fl_flags)
@@ -82,7 +82,7 @@ DECLARE_EVENT_CLASS(filelock_lock,
8282 __entry->fl = fl ? fl : NULL;
8383 __entry->s_dev = inode->i_sb->s_dev;
8484 __entry->i_ino = inode->i_ino;
85- __entry->fl_next = fl ? fl->fl_next : NULL;
85+ __entry->fl_blocker = fl ? fl->fl_blocker : NULL;
8686 __entry->fl_owner = fl ? fl->fl_owner : NULL;
8787 __entry->fl_pid = fl ? fl->fl_pid : 0;
8888 __entry->fl_flags = fl ? fl->fl_flags : 0;
@@ -92,9 +92,9 @@ DECLARE_EVENT_CLASS(filelock_lock,
9292 __entry->ret = ret;
9393 ),
9494
95- TP_printk("fl=0x%p dev=0x%x:0x%x ino=0x%lx fl_next=0x%p fl_owner=0x%p fl_pid=%u fl_flags=%s fl_type=%s fl_start=%lld fl_end=%lld ret=%d",
95+ TP_printk("fl=0x%p dev=0x%x:0x%x ino=0x%lx fl_blocker=0x%p fl_owner=0x%p fl_pid=%u fl_flags=%s fl_type=%s fl_start=%lld fl_end=%lld ret=%d",
9696 __entry->fl, MAJOR(__entry->s_dev), MINOR(__entry->s_dev),
97- __entry->i_ino, __entry->fl_next, __entry->fl_owner,
97+ __entry->i_ino, __entry->fl_blocker, __entry->fl_owner,
9898 __entry->fl_pid, show_fl_flags(__entry->fl_flags),
9999 show_fl_type(__entry->fl_type),
100100 __entry->fl_start, __entry->fl_end, __entry->ret)
@@ -125,7 +125,7 @@ DECLARE_EVENT_CLASS(filelock_lease,
125125 __field(struct file_lock *, fl)
126126 __field(unsigned long, i_ino)
127127 __field(dev_t, s_dev)
128- __field(struct file_lock *, fl_next)
128+ __field(struct file_lock *, fl_blocker)
129129 __field(fl_owner_t, fl_owner)
130130 __field(unsigned int, fl_flags)
131131 __field(unsigned char, fl_type)
@@ -137,7 +137,7 @@ DECLARE_EVENT_CLASS(filelock_lease,
137137 __entry->fl = fl ? fl : NULL;
138138 __entry->s_dev = inode->i_sb->s_dev;
139139 __entry->i_ino = inode->i_ino;
140- __entry->fl_next = fl ? fl->fl_next : NULL;
140+ __entry->fl_blocker = fl ? fl->fl_blocker : NULL;
141141 __entry->fl_owner = fl ? fl->fl_owner : NULL;
142142 __entry->fl_flags = fl ? fl->fl_flags : 0;
143143 __entry->fl_type = fl ? fl->fl_type : 0;
@@ -145,9 +145,9 @@ DECLARE_EVENT_CLASS(filelock_lease,
145145 __entry->fl_downgrade_time = fl ? fl->fl_downgrade_time : 0;
146146 ),
147147
148- TP_printk("fl=0x%p dev=0x%x:0x%x ino=0x%lx fl_next=0x%p fl_owner=0x%p fl_flags=%s fl_type=%s fl_break_time=%lu fl_downgrade_time=%lu",
148+ TP_printk("fl=0x%p dev=0x%x:0x%x ino=0x%lx fl_blocker=0x%p fl_owner=0x%p fl_flags=%s fl_type=%s fl_break_time=%lu fl_downgrade_time=%lu",
149149 __entry->fl, MAJOR(__entry->s_dev), MINOR(__entry->s_dev),
150- __entry->i_ino, __entry->fl_next, __entry->fl_owner,
150+ __entry->i_ino, __entry->fl_blocker, __entry->fl_owner,
151151 show_fl_flags(__entry->fl_flags),
152152 show_fl_type(__entry->fl_type),
153153 __entry->fl_break_time, __entry->fl_downgrade_time)
Show on old repository browser