From 60e2a0d9a4b21978f60c5b608a458f3427b390c8 Mon Sep 17 00:00:00 2001 From: Mateusz Guzik Date: Sun, 6 Dec 2020 04:59:24 +0000 Subject: [PATCH] vfs: factor buffer allocation/copyin out of namei --- sys/kern/vfs_lookup.c | 61 +++++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/sys/kern/vfs_lookup.c b/sys/kern/vfs_lookup.c index b08eb90c1d4..62bfca94f0e 100644 --- a/sys/kern/vfs_lookup.c +++ b/sys/kern/vfs_lookup.c @@ -464,6 +464,43 @@ namei_setup(struct nameidata *ndp, struct vnode **dpp, struct pwd **pwdp) return (0); } +static int +namei_getpath(struct nameidata *ndp) +{ + struct componentname *cnp; + int error; + + cnp = &ndp->ni_cnd; + + /* + * Get a buffer for the name to be translated, and copy the + * name into the buffer. + */ + cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK); + if (ndp->ni_segflg == UIO_SYSSPACE) { + error = copystr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN, + &ndp->ni_pathlen); + } else { + error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN, + &ndp->ni_pathlen); + } + + if (__predict_false(error != 0)) { + return (error); + } + + /* + * Don't allow empty pathnames. + */ + if (__predict_false(*cnp->cn_pnbuf == '\0')) { + namei_cleanup_cnp(cnp); + return (ENOENT); + } + + cnp->cn_nameptr = cnp->cn_pnbuf; + return (0); +} + /* * Convert a pathname into a pointer to a locked vnode. * @@ -531,31 +568,11 @@ namei(struct nameidata *ndp) ndp->ni_lcf = 0; ndp->ni_vp = NULL; - /* - * Get a buffer for the name to be translated, and copy the - * name into the buffer. - */ - cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK); - if (ndp->ni_segflg == UIO_SYSSPACE) - error = copystr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN, - &ndp->ni_pathlen); - else - error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN, - &ndp->ni_pathlen); - + error = namei_getpath(ndp); if (__predict_false(error != 0)) { - namei_cleanup_cnp(cnp); return (error); } - /* - * Don't allow empty pathnames. - */ - if (__predict_false(*cnp->cn_pnbuf == '\0')) { - namei_cleanup_cnp(cnp); - return (ENOENT); - } - #ifdef KTRACE if (KTRPOINT(td, KTR_NAMEI)) { KASSERT(cnp->cn_thread == curthread, @@ -564,8 +581,6 @@ namei(struct nameidata *ndp) } #endif - cnp->cn_nameptr = cnp->cn_pnbuf; - /* * First try looking up the target without locking any vnodes. * -- 2.45.0