git: 2f3d64f63677 - main - namei(9): fix restart for NDINIT_ATVP

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Tue, 10 Nov 2026 00:14:21 UTC
The branch main has been updated by kib:

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

commit 2f3d64f63677a6ed4c7bb6629d92161555948038
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-11-08 19:20:37 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-11-10 00:13:33 +0000

    namei(9): fix restart for NDINIT_ATVP
    
    namei() might restart for altroot or for ERESTART from
    nameicap_tracker_add().
    
    vfs_lookup() consumes the reference on ni_startdir and clears it, so
    a restarted NDINIT_ATVP() lookup started from the current directory
    instead of the vnode passed by the caller.
    
    Save the starting vnode in a local variable, hold it while the lookup
    can be restarted, and reference it before the restarted pass. Do not
    assert NAMEI_DBG_HADSTARTDIR on the internal restart.
    
    Noted and reviewed by:  markj
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D61315
---
 sys/kern/vfs_lookup.c | 27 ++++++++++++++++++++++++++-
 sys/sys/namei.h       | 10 +++++++---
 2 files changed, 33 insertions(+), 4 deletions(-)

diff --git a/sys/kern/vfs_lookup.c b/sys/kern/vfs_lookup.c
index a1d8a8cd765f..2960d3aafb92 100644
--- a/sys/kern/vfs_lookup.c
+++ b/sys/kern/vfs_lookup.c
@@ -76,12 +76,15 @@ static void NDVALIDATE_impl(struct nameidata *, int);
 
 /*
  * Reset ndp to its original state.
+ * namei() supplies the starting directory again on restart, so unlike
+ * NDREINIT(), NAMEI_DBG_HADSTARTDIR can be set there.
  */
 #define	NDRESET(ndp) do {						\
-	NDREINIT_DBG(ndp);						\
+	NDRESET_DBG(ndp);						\
 	ndp->ni_resflags = 0;						\
 	ndp->ni_cnd.cn_flags &= ~NAMEI_INTERNAL_FLAGS;			\
 } while (0)
+
 /*
  * Prepare namei() to restart. Reset components to its original state and set
  * ISRESTARTED flag which signals the underlying lookup code to change the root
@@ -610,6 +613,7 @@ int
 namei(struct nameidata *ndp)
 {
 	struct vnode *dp;	/* the directory we are searching */
+	struct vnode *atvp;	/* NDINIT_ATVP() starting directory */
 	struct componentname *cnp;
 	struct thread *td;
 	struct pwd *pwd;
@@ -627,6 +631,7 @@ namei(struct nameidata *ndp)
 
 	cnp = &ndp->ni_cnd;
 	td = curthread;
+	atvp = ndp->ni_startdir;
 	abi_symlink = NULL;
 	abi_symlink_len = 0;
 	abi_suffix_len = 0;
@@ -701,6 +706,8 @@ restart:
 	 * We may need to start from scratch or pick up where it left off.
 	 */
 	error = cache_fplookup(ndp, &status, &pwd);
+	/* cache_can_fplookup() rejects ni_startdir. */
+	MPASS(atvp == NULL || status == CACHE_FPL_STATUS_ABORTED);
 	switch (status) {
 	case CACHE_FPL_STATUS_UNSET:
 		__assert_unreachable();
@@ -743,8 +750,17 @@ restart:
 			    false, ndp);
 			return (ENOENT);
 		}
+		/*
+		 * namei_setup() and vfs_lookup() consume the reference
+		 * on ni_startdir.  Hold the NDINIT_ATVP() vnode so that
+		 * a restart can reference it again.
+		 */
+		if (atvp != NULL)
+			vhold(atvp);
 		error = namei_setup(ndp, &dp, &pwd);
 		if (error != 0) {
+			if (atvp != NULL)
+				vdrop(atvp);
 			namei_cleanup_cnp(cnp);
 			nameicap_cleanup(ndp, error);
 			return (error);
@@ -823,6 +839,11 @@ restart:
 			if (abi_restart)
 				was_restarted = ISRESTARTED;
 			ndp->ni_cnd.cn_flags |= was_restarted;
+			if (atvp != NULL) {
+				vref(atvp);
+				ndp->ni_startdir = atvp;
+				vdrop(atvp);
+			}
 			goto restart;
 		}
 
@@ -835,6 +856,8 @@ restart:
 			nameicap_cleanup(ndp, 0);
 			pwd_drop(pwd);
 			uma_zfree(namei_zone, abi_symlink);
+			if (atvp != NULL)
+				vdrop(atvp);
 			NDVALIDATE(ndp);
 			return (0);
 		}
@@ -908,6 +931,8 @@ out:
 	nameicap_cleanup(ndp, error);
 	pwd_drop(pwd);
 	uma_zfree(namei_zone, abi_symlink);
+	if (atvp != NULL)
+		vdrop(atvp);
 	return (error);
 }
 
diff --git a/sys/sys/namei.h b/sys/sys/namei.h
index 6008d83f729d..2d03bd6deb84 100644
--- a/sys/sys/namei.h
+++ b/sys/sys/namei.h
@@ -235,19 +235,23 @@ int	cache_fplookup(struct nameidata *ndp, enum cache_fpl_status *status,
 #define NDINIT_PREFILL(arg)	memset(arg, 0xff, offsetof(struct nameidata,	\
     ni_dvp_seqc))
 #define NDINIT_DBG(arg)		{ (arg)->ni_debugflags = NAMEI_DBG_INITED; }
-#define NDREINIT_DBG(arg)	{						\
+#define NDRESET_DBG(arg)	{						\
 	if (((arg)->ni_debugflags & NAMEI_DBG_INITED) == 0)			\
 		panic("namei data not inited");					\
-	if (((arg)->ni_debugflags & NAMEI_DBG_HADSTARTDIR) != 0)		\
-		panic("NDREINIT on namei data with NAMEI_DBG_HADSTARTDIR");	\
 	if ((arg)->ni_nctrack_mnt != NULL)			\
 		panic("NDREINIT on namei data with leaked ni_nctrack_mnt");	\
 	if (!TAILQ_EMPTY(&(arg)->ni_cap_tracker))				\
 		panic("NDREINIT on namei data with leaked ni_cap_tracker");	\
+}
+#define NDREINIT_DBG(arg)	{						\
+	NDRESET_DBG(arg);							\
+	if (((arg)->ni_debugflags & NAMEI_DBG_HADSTARTDIR) != 0)		\
+		panic("NDREINIT on namei data with NAMEI_DBG_HADSTARTDIR");	\
 	(arg)->ni_debugflags = NAMEI_DBG_INITED;				\
 }
 #else
 #define NDINIT_PREFILL(arg)	do { } while (0)
+#define NDRESET_DBG(arg)	do { } while (0)
 #define NDINIT_DBG(arg)		do { } while (0)
 #define NDREINIT_DBG(arg)	do { } while (0)
 #endif