git: 30a2fc91fa5a - main - cache: postpone NAME_MAX check as it may be unnecessary

Mateusz Guzik mjg at FreeBSD.org
Sun Jan 3 06:53:12 UTC 2021


The branch main has been updated by mjg:

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

commit 30a2fc91fa5a0fa8717da788cc18073cca786c05
Author:     Mateusz Guzik <mjg at FreeBSD.org>
AuthorDate: 2020-12-31 07:28:32 +0000
Commit:     Mateusz Guzik <mjg at FreeBSD.org>
CommitDate: 2021-01-03 06:50:06 +0000

    cache: postpone NAME_MAX check as it may be unnecessary
---
 sys/kern/vfs_cache.c | 31 +++++++++++++++++++++++++++----
 1 file changed, 27 insertions(+), 4 deletions(-)

diff --git a/sys/kern/vfs_cache.c b/sys/kern/vfs_cache.c
index e6f446d2d0e2..b4882079790d 100644
--- a/sys/kern/vfs_cache.c
+++ b/sys/kern/vfs_cache.c
@@ -2230,6 +2230,9 @@ cache_enter_time(struct vnode *dvp, struct vnode *vp, struct componentname *cnp,
 	int flag;
 	int len;
 
+	KASSERT(cnp->cn_namelen <= NAME_MAX,
+	    ("%s: passed len %ld exceeds NAME_MAX (%d)", __func__, cnp->cn_namelen,
+	    NAME_MAX));
 	VNPASS(dvp != vp, dvp);
 	VNPASS(!VN_IS_DOOMED(dvp), dvp);
 	VNPASS(dvp->v_type != VNON, dvp);
@@ -4310,6 +4313,14 @@ cache_fplookup_noentry(struct cache_fpl *fpl)
 	MPASS((cnp->cn_flags & ISDOTDOT) == 0);
 	MPASS(!cache_fpl_isdotdot(cnp));
 
+	/*
+	 * Hack: delayed name len checking.
+	 */
+	if (__predict_false(cnp->cn_namelen > NAME_MAX)) {
+		cache_fpl_smr_exit(fpl);
+		return (cache_fpl_handled(fpl, ENAMETOOLONG));
+	}
+
 	if (cnp->cn_nameiop != LOOKUP) {
 		fpl->tvp = NULL;
 		return (cache_fplookup_modifying(fpl));
@@ -4834,13 +4845,15 @@ cache_fplookup_parse(struct cache_fpl *fpl)
 	cnp->cn_nameptr[ndp->ni_pathlen - 1] = '\0';
 
 	cnp->cn_namelen = cp - cnp->cn_nameptr;
-	if (__predict_false(cnp->cn_namelen > NAME_MAX)) {
-		cache_fpl_smr_exit(fpl);
-		return (cache_fpl_handled(fpl, ENAMETOOLONG));
-	}
 	ndp->ni_pathlen -= cnp->cn_namelen;
 	KASSERT(ndp->ni_pathlen <= PATH_MAX,
 	    ("%s: ni_pathlen underflow to %zd\n", __func__, ndp->ni_pathlen));
+	/*
+	 * Hack: we have to check if the found path component's length exceeds
+	 * NAME_MAX. However, the condition is very rarely true and check can
+	 * be elided in the common case -- if an entry was found in the cache,
+	 * then it could not have been too long to begin with.
+	 */
 	ndp->ni_next = cp;
 
 #ifdef INVARIANTS
@@ -4888,12 +4901,22 @@ cache_fplookup_parse_advance(struct cache_fpl *fpl)
 static int __noinline
 cache_fplookup_failed_vexec(struct cache_fpl *fpl, int error)
 {
+	struct componentname *cnp;
 	struct vnode *dvp;
 	seqc_t dvp_seqc;
 
+	cnp = fpl->cnp;
 	dvp = fpl->dvp;
 	dvp_seqc = fpl->dvp_seqc;
 
+	/*
+	 * Hack: delayed name len checking.
+	 */
+	if (__predict_false(cnp->cn_namelen > NAME_MAX)) {
+		cache_fpl_smr_exit(fpl);
+		return (cache_fpl_handled(fpl, ENAMETOOLONG));
+	}
+
 	/*
 	 * Hack: they may be looking up foo/bar, where foo is a
 	 * regular file. In such a case we need to turn ENOTDIR,


More information about the dev-commits-src-all mailing list