From owner-freebsd-fs@freebsd.org Mon Jul 6 03:05:40 2015 Return-Path: Delivered-To: freebsd-fs@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id C20A498EC0A for ; Mon, 6 Jul 2015 03:05:40 +0000 (UTC) (envelope-from mjguzik@gmail.com) Received: from mail-wg0-x235.google.com (mail-wg0-x235.google.com [IPv6:2a00:1450:400c:c00::235]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 5D3161D57; Mon, 6 Jul 2015 03:05:40 +0000 (UTC) (envelope-from mjguzik@gmail.com) Received: by wgjx7 with SMTP id x7so128112343wgj.2; Sun, 05 Jul 2015 20:05:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=DOvdJngvdWAg8DefsXCXhr8EkNIum23GeXfO+EN45BE=; b=vYbybprWU5ITRYoEyvKMZ1ThKcL04Qj1Y4yhWasIvhjm9YWbx03xFj3vhvwMQ4cFhR DBL1RzBQM+TRvnjIpTNKw6B/kDPjEIO4Dn3qGtz7Tv3V/t4+AhLK3pbgh7StVsUtfWlA sCRuO/fmbAV0RI5JEWabTg9KvtyQcWXG9O5fEvLz+eI18tcOUAXprDADJDUFbDbmLU2D AYaKlLbz4afpDZJC2alGQifGv9CiI4unxlLBxTdyaKD2ZHNebK6uJOgZl5uLnb9JZX0O e49EMeAiJEo8Hj1xKHqRWOuFXrJuO9Hk6csy5ADWpWPv65AmDvOCGH/ryOsSOhj/8CaJ ZG8Q== X-Received: by 10.194.58.69 with SMTP id o5mr91761227wjq.22.1436151938754; Sun, 05 Jul 2015 20:05:38 -0700 (PDT) Received: from localhost.localdomain (ip-89-102-11-63.net.upcbroadband.cz. [89.102.11.63]) by mx.google.com with ESMTPSA id pd7sm25571851wjb.27.2015.07.05.20.05.36 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 05 Jul 2015 20:05:37 -0700 (PDT) From: Mateusz Guzik To: freebsd-fs@freebsd.org Cc: kib@freesd.org, rwatson@FreeBSD.org, Mateusz Guzik Subject: [PATCH 1/2] vfs: avoid spurious vref/vrele for absolute lookups Date: Mon, 6 Jul 2015 05:05:31 +0200 Message-Id: <1436151932-12514-2-git-send-email-mjguzik@gmail.com> X-Mailer: git-send-email 2.4.3 In-Reply-To: <1436151932-12514-1-git-send-email-mjguzik@gmail.com> References: <1436151932-12514-1-git-send-email-mjguzik@gmail.com> X-BeenThere: freebsd-fs@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Filesystems List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 06 Jul 2015 03:05:40 -0000 From: Mateusz Guzik namei used to vref fd_cdir, which was immediatley vrele'd on entry to the loop. Simplify error handling and remove type checking for ni_startdir vnode. It is only set by nfs which does the check on its own. Assert the correct type instead. --- sys/kern/vfs_lookup.c | 92 ++++++++++++++++++++++++++++----------------------- 1 file changed, 51 insertions(+), 41 deletions(-) diff --git a/sys/kern/vfs_lookup.c b/sys/kern/vfs_lookup.c index 5dc07dc..c5218ec 100644 --- a/sys/kern/vfs_lookup.c +++ b/sys/kern/vfs_lookup.c @@ -109,6 +109,27 @@ namei_cleanup_cnp(struct componentname *cnp) #endif } +static int +namei_handle_root(struct nameidata *ndp, struct vnode **dpp) +{ + struct componentname *cnp = &ndp->ni_cnd; + + if (ndp->ni_strictrelative != 0) { +#ifdef KTRACE + if (KTRPOINT(curthread, KTR_CAPFAIL)) + ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL); +#endif + return (ENOTCAPABLE); + } + while (*(cnp->cn_nameptr) == '/') { + cnp->cn_nameptr++; + ndp->ni_pathlen--; + } + *dpp = ndp->ni_rootdir; + VREF(*dpp); + return (0); +} + /* * Convert a pathname into a pointer to a locked vnode. * @@ -148,6 +169,8 @@ namei(struct nameidata *ndp) ("namei: nameiop contaminated with flags")); KASSERT((cnp->cn_flags & OPMASK) == 0, ("namei: flags contaminated with nameiops")); + if (ndp->ni_startdir != NULL) + MPASS(ndp->ni_startdir->v_type == VDIR); if (!lookup_shared) cnp->cn_flags &= ~LOCKSHARED; fdp = p->p_fd; @@ -220,12 +243,16 @@ namei(struct nameidata *ndp) if (cnp->cn_flags & AUDITVNODE2) AUDIT_ARG_UPATH2(td, ndp->ni_dirfd, cnp->cn_pnbuf); - dp = NULL; - if (cnp->cn_pnbuf[0] != '/') { + cnp->cn_nameptr = cnp->cn_pnbuf; + if (cnp->cn_pnbuf[0] == '/') { + error = namei_handle_root(ndp, &dp); + } else { if (ndp->ni_startdir != NULL) { dp = ndp->ni_startdir; - error = 0; - } else if (ndp->ni_dirfd != AT_FDCWD) { + } else if (ndp->ni_dirfd == AT_FDCWD) { + dp = fdp->fd_cdir; + VREF(dp); + } else { cap_rights_t rights; rights = ndp->ni_rightsneeded; @@ -251,51 +278,22 @@ namei(struct nameidata *ndp) ndp->ni_strictrelative = 1; } #endif - } - if (error != 0 || dp != NULL) { - FILEDESC_SUNLOCK(fdp); - if (error == 0 && dp->v_type != VDIR) { - vrele(dp); + if (error == 0 && dp->v_type != VDIR) error = ENOTDIR; - } - } - if (error) { - namei_cleanup_cnp(cnp); - return (error); } } - if (dp == NULL) { - dp = fdp->fd_cdir; - VREF(dp); - FILEDESC_SUNLOCK(fdp); - if (ndp->ni_startdir != NULL) + FILEDESC_SUNLOCK(fdp); + if (error != 0) { + if (dp != NULL) + vrele(dp); + if (ndp->ni_startdir != NULL && dp != ndp->ni_startdir) vrele(ndp->ni_startdir); + namei_cleanup_cnp(cnp); + return (error); } SDT_PROBE(vfs, namei, lookup, entry, dp, cnp->cn_pnbuf, cnp->cn_flags, 0, 0); for (;;) { - /* - * Check if root directory should replace current directory. - * Done at start of translation and after symbolic link. - */ - cnp->cn_nameptr = cnp->cn_pnbuf; - if (*(cnp->cn_nameptr) == '/') { - vrele(dp); - if (ndp->ni_strictrelative != 0) { -#ifdef KTRACE - if (KTRPOINT(curthread, KTR_CAPFAIL)) - ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL); -#endif - namei_cleanup_cnp(cnp); - return (ENOTCAPABLE); - } - while (*(cnp->cn_nameptr) == '/') { - cnp->cn_nameptr++; - ndp->ni_pathlen--; - } - dp = ndp->ni_rootdir; - VREF(dp); - } ndp->ni_startdir = dp; error = lookup(ndp); if (error) { @@ -370,6 +368,18 @@ namei(struct nameidata *ndp) ndp->ni_pathlen += linklen; vput(ndp->ni_vp); dp = ndp->ni_dvp; + /* + * Check if root directory should replace current directory. + */ + cnp->cn_nameptr = cnp->cn_pnbuf; + if (*(cnp->cn_nameptr) == '/') { + vrele(dp); + error = namei_handle_root(ndp, &dp); + if (error != 0) { + namei_cleanup_cnp(cnp); + return (error); + } + } } namei_cleanup_cnp(cnp); vput(ndp->ni_vp); -- 2.4.5