git: 42442d7a6eeb - main - Generalize the VV_CROSSLOCK logic in vfs_lookup()

From: Jason A. Harmening <jah_at_FreeBSD.org>
Date: Sun, 11 Dec 2022 03:48:53 UTC
The branch main has been updated by jah:

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

commit 42442d7a6eeb860e00f76aae5858884b44f6cce5
Author:     Jason A. Harmening <jah@FreeBSD.org>
AuthorDate: 2022-10-26 22:25:20 +0000
Commit:     Jason A. Harmening <jah@FreeBSD.org>
CommitDate: 2022-12-11 04:02:38 +0000

    Generalize the VV_CROSSLOCK logic in vfs_lookup()
    
    When VV_CROSSLOCK is present, the lock for the vnode at the current
    stage of lookup must be held across the VFS_ROOT() call for the
    filesystem mounted at the vnode.  Since VV_CROSSLOCK implies that
    the root vnode reuses the already-held lock, the possibility for
    recursion should be made clear in the flags passed to VFS_ROOT().
    
    For cases in which the lock is held exclusive, this means passing
    LK_CANRECURSE.  For cases in which the lock is held shared, it
    means clearing LK_NODDLKTREAT to allow VFS_ROOT() to potentially
    recurse on the shared lock even in the presence of an exclusive
    waiter.
    
    That the existing code works for unionfs is due to a coincidence
    of the current unionfs implementation.
    
    Reviewed by:    kib
    Tested by:      pho
    Differential Revision:  https://reviews.freebsd.org/D37458
---
 sys/kern/vfs_lookup.c | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)

diff --git a/sys/kern/vfs_lookup.c b/sys/kern/vfs_lookup.c
index cc41849de532..404c41a1bf2c 100644
--- a/sys/kern/vfs_lookup.c
+++ b/sys/kern/vfs_lookup.c
@@ -1265,14 +1265,28 @@ good:
 		crosslock = (dp->v_vflag & VV_CROSSLOCK) != 0;
 		crosslkflags = compute_cn_lkflags(mp, cnp->cn_lkflags,
 		    cnp->cn_flags);
-		if (__predict_false(crosslock) &&
-		    (crosslkflags & LK_EXCLUSIVE) != 0 &&
-		    VOP_ISLOCKED(dp) != LK_EXCLUSIVE) {
-			vn_lock(dp, LK_UPGRADE | LK_RETRY);
-			if (VN_IS_DOOMED(dp)) {
-				error = ENOENT;
-				goto bad2;
-			}
+		if (__predict_false(crosslock)) {
+			/*
+			 * We are going to be holding the vnode lock, which
+			 * in this case is shared by the root vnode of the
+			 * filesystem mounted at mp, across the call to
+			 * VFS_ROOT().  Make the situation clear to the
+			 * filesystem by passing LK_CANRECURSE if the
+			 * lock is held exclusive, or by clearinng
+			 * LK_NODDLKTREAT to allow recursion on the shared
+			 * lock in the presence of an exclusive waiter.
+			 */
+			if (VOP_ISLOCKED(dp) == LK_EXCLUSIVE) {
+				crosslkflags &= ~LK_SHARED;
+				crosslkflags |= LK_EXCLUSIVE | LK_CANRECURSE;
+			} else if ((crosslkflags & LK_EXCLUSIVE) != 0) {
+				vn_lock(dp, LK_UPGRADE | LK_RETRY);
+				if (VN_IS_DOOMED(dp)) {
+					error = ENOENT;
+					goto bad2;
+				}
+			} else
+				crosslkflags &= ~LK_NODDLKTREAT;
 		}
 		if (vfs_busy(mp, 0) != 0)
 			continue;