git: ab7955aec728 - stable/13 - fusefs: during F_GETLK, don't change l_pid if no lock is found

From: Alan Somers <asomers_at_FreeBSD.org>
Date: Mon, 31 Oct 2022 01:50:45 UTC
The branch stable/13 has been updated by asomers:

URL: https://cgit.FreeBSD.org/src/commit/?id=ab7955aec728c03d9388aff7b03ca748c69a0eb4

commit ab7955aec728c03d9388aff7b03ca748c69a0eb4
Author:     Alan Somers <asomers@FreeBSD.org>
AuthorDate: 2022-10-07 14:46:22 +0000
Commit:     Alan Somers <asomers@FreeBSD.org>
CommitDate: 2022-10-31 00:51:30 +0000

    fusefs: during F_GETLK, don't change l_pid if no lock is found
    
    PR:             266885
    Submitted by:   John Millikin <jmillikin@gmail.com>
    Sponsored by:   Axcient
    Reviewed by:    emaste
    Differential Revision: https://reviews.freebsd.org/D36905
    
    (cherry picked from commit 46fcf947c6c8db1e1ceb3cbd75b69d1d1e494929)
---
 sys/fs/fuse/fuse_vnops.c     |  2 +-
 tests/sys/fs/fusefs/locks.cc | 16 ++++++++++++++--
 2 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/sys/fs/fuse/fuse_vnops.c b/sys/fs/fuse/fuse_vnops.c
index f68bbb358ca2..cd3985049576 100644
--- a/sys/fs/fuse/fuse_vnops.c
+++ b/sys/fs/fuse/fuse_vnops.c
@@ -535,8 +535,8 @@ fuse_vnop_advlock(struct vop_advlock_args *ap)
 	if (err == 0 && op == FUSE_GETLK) {
 		flo = fdi.answ;
 		fl->l_type = flo->lk.type;
-		fl->l_pid = flo->lk.pid;
 		if (flo->lk.type != F_UNLCK) {
+			fl->l_pid = flo->lk.pid;
 			fl->l_start = flo->lk.start;
 			if (flo->lk.end == INT64_MAX)
 				fl->l_len = 0;
diff --git a/tests/sys/fs/fusefs/locks.cc b/tests/sys/fs/fusefs/locks.cc
index cace779e981a..aeb24704624b 100644
--- a/tests/sys/fs/fusefs/locks.cc
+++ b/tests/sys/fs/fusefs/locks.cc
@@ -278,12 +278,24 @@ TEST_F(Getlk, no_locks)
 	ASSERT_LE(0, fd) << strerror(errno);
 	fl.l_start = 10;
 	fl.l_len = 1000;
-	fl.l_pid = 0;
+	fl.l_pid = 42;
 	fl.l_type = F_RDLCK;
 	fl.l_whence = SEEK_SET;
-	fl.l_sysid = 0;
+	fl.l_sysid = 42;
 	ASSERT_NE(-1, fcntl(fd, F_GETLK, &fl)) << strerror(errno);
+
+	/*
+	 * If no lock is found that would prevent this lock from being created,
+	 * the structure is left unchanged by this system call except for the
+	 * lock type which is set to F_UNLCK.
+	 */
 	ASSERT_EQ(F_UNLCK, fl.l_type);
+	ASSERT_EQ(fl.l_pid, 42);
+	ASSERT_EQ(fl.l_start, 10);
+	ASSERT_EQ(fl.l_len, 1000);
+	ASSERT_EQ(fl.l_whence, SEEK_SET);
+	ASSERT_EQ(fl.l_sysid, 42);
+
 	leak(fd);
 }