From 4f01fdec5bed5abd43cdb9823baca30d55cf01f6 Mon Sep 17 00:00:00 2001
From: Salvatore Bonaccorso <carnil@debian.org>
Date: Mon, 4 Dec 2017 11:16:30 +0100
Subject: [PATCH 2/2] locks: __break_lease cleanup in preparation of allowing
 direct removal of leases

Closes: #883217
---
 ...ak_lease-cleanup-in-preparation-of-allowi.patch | 130 +++++++++++++++++++++
 debian/patches/series                              |   1 +
 2 files changed, 131 insertions(+)
 create mode 100644 debian/patches/bugfix/all/locks-__break_lease-cleanup-in-preparation-of-allowi.patch

diff --git a/debian/patches/bugfix/all/locks-__break_lease-cleanup-in-preparation-of-allowi.patch b/debian/patches/bugfix/all/locks-__break_lease-cleanup-in-preparation-of-allowi.patch
new file mode 100644
index 000000000..b52e876fa
--- /dev/null
+++ b/debian/patches/bugfix/all/locks-__break_lease-cleanup-in-preparation-of-allowi.patch
@@ -0,0 +1,130 @@
+From: Jeff Layton <jlayton@primarydata.com>
+Date: Mon, 1 Sep 2014 14:53:41 -0400
+Subject: locks: __break_lease cleanup in preparation of allowing direct
+ removal of leases
+Origin: https://git.kernel.org/linus/03d12ddf845a4eb874ffa558d65a548aee9b715b
+Bug-Debian: https://bugs.debian.org/883217
+
+Eliminate an unneeded "flock" variable. We can use "fl" as a loop cursor
+everywhere. Add a any_leases_conflict helper function as well to
+consolidate a bit of code.
+
+Signed-off-by: Jeff Layton <jlayton@primarydata.com>
+Reviewed-by: Christoph Hellwig <hch@lst.de>
+[carnil: backport for 3.16:
+ - adjust context
+]
+---
+ fs/locks.c | 49 +++++++++++++++++++++++++------------------------
+ 1 file changed, 25 insertions(+), 24 deletions(-)
+
+--- a/fs/locks.c
++++ b/fs/locks.c
+@@ -1307,6 +1307,20 @@ static bool leases_conflict(struct file_
+ 	return locks_conflict(breaker, lease);
+ }
+ 
++static bool
++any_leases_conflict(struct inode *inode, struct file_lock *breaker)
++{
++	struct file_lock *fl;
++
++	lockdep_assert_held(&inode->i_lock);
++
++	for (fl = inode->i_flock ; fl && IS_LEASE(fl); fl = fl->fl_next) {
++		if (leases_conflict(fl, breaker))
++			return true;
++	}
++	return false;
++}
++
+ /**
+  *	__break_lease	-	revoke all outstanding leases on file
+  *	@inode: the inode of the file to return
+@@ -1323,10 +1337,9 @@ static bool leases_conflict(struct file_
+ int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
+ {
+ 	int error = 0;
+-	struct file_lock *new_fl, *flock;
++	struct file_lock *new_fl;
+ 	struct file_lock *fl;
+ 	unsigned long break_time;
+-	bool lease_conflict = false;
+ 	int want_write = (mode & O_ACCMODE) != O_RDONLY;
+ 
+ 	new_fl = lease_alloc(NULL, want_write ? F_WRLCK : F_RDLCK);
+@@ -1338,17 +1351,7 @@ int __break_lease(struct inode *inode, u
+ 
+ 	time_out_leases(inode);
+ 
+-	flock = inode->i_flock;
+-	if ((flock == NULL) || !IS_LEASE(flock))
+-		goto out;
+-
+-	for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next) {
+-		if (leases_conflict(fl, new_fl)) {
+-			lease_conflict = true;
+-			break;
+-		}
+-	}
+-	if (!lease_conflict)
++	if (!any_leases_conflict(inode, new_fl))
+ 		goto out;
+ 
+ 	break_time = 0;
+@@ -1358,7 +1361,7 @@ int __break_lease(struct inode *inode, u
+ 			break_time++;	/* so that 0 means no break time */
+ 	}
+ 
+-	for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next) {
++	for (fl = inode->i_flock; fl && IS_LEASE(fl); fl = fl->fl_next) {
+ 		if (!leases_conflict(fl, new_fl))
+ 			continue;
+ 		if (want_write) {
+@@ -1367,7 +1370,7 @@ int __break_lease(struct inode *inode, u
+ 			fl->fl_flags |= FL_UNLOCK_PENDING;
+ 			fl->fl_break_time = break_time;
+ 		} else {
+-			if (lease_breaking(flock))
++			if (lease_breaking(inode->i_flock))
+ 				continue;
+ 			fl->fl_flags |= FL_DOWNGRADE_PENDING;
+ 			fl->fl_downgrade_time = break_time;
+@@ -1382,12 +1385,12 @@ int __break_lease(struct inode *inode, u
+ 	}
+ 
+ restart:
+-	break_time = flock->fl_break_time;
++	break_time = inode->i_flock->fl_break_time;
+ 	if (break_time != 0)
+ 		break_time -= jiffies;
+ 	if (break_time == 0)
+ 		break_time++;
+-	locks_insert_block(flock, new_fl);
++	locks_insert_block(inode->i_flock, new_fl);
+ 	trace_break_lease_block(inode, new_fl);
+ 	spin_unlock(&inode->i_lock);
+ 	error = wait_event_interruptible_timeout(new_fl->fl_wait,
+@@ -1396,17 +1399,15 @@ restart:
+ 	trace_break_lease_unblock(inode, new_fl);
+ 	locks_delete_block(new_fl);
+ 	if (error >= 0) {
+-		if (error == 0)
+-			time_out_leases(inode);
+ 		/*
+ 		 * Wait for the next conflicting lease that has not been
+ 		 * broken yet
+ 		 */
+-		for (flock = inode->i_flock; flock && IS_LEASE(flock);
+-				flock = flock->fl_next) {
+-			if (leases_conflict(new_fl, flock))
+-				goto restart;
+-		}
++		if (error == 0)
++			time_out_leases(inode);
++		if (any_leases_conflict(inode, new_fl))
++			goto restart;
++
+ 		error = 0;
+ 	}
+ 
diff --git a/debian/patches/series b/debian/patches/series
index 4ab96adb2..50a0f5f7a 100644
--- a/debian/patches/series
+++ b/debian/patches/series
@@ -252,6 +252,7 @@ bugfix/all/mm-mmap.c-expand_downwards-don-t-require-the-gap-if-.patch
 bugfix/x86/mmap-remember-the-map_fixed-flag-as-vm_fixed.patch
 bugfix/x86/mmap-add-an-exception-to-the-stack-gap-for-hotspot-jvm.patch
 bugfix/all/locks-remove-i_have_this_lease-check-from-__break_le.patch
+bugfix/all/locks-__break_lease-cleanup-in-preparation-of-allowi.patch
 
 # memfd_create() & kdbus backport
 features/all/kdbus/mm-allow-drivers-to-prevent-new-writable-mappings.patch
-- 
2.15.1

