From owner-freebsd-fs@freebsd.org Wed Jul 8 22:07:21 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 259CA996520 for ; Wed, 8 Jul 2015 22:07:21 +0000 (UTC) (envelope-from mjguzik@gmail.com) Received: from mail-wg0-x22d.google.com (mail-wg0-x22d.google.com [IPv6:2a00:1450:400c:c00::22d]) (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 B3B5E30A0; Wed, 8 Jul 2015 22:07:20 +0000 (UTC) (envelope-from mjguzik@gmail.com) Received: by wgxm20 with SMTP id m20so24334281wgx.3; Wed, 08 Jul 2015 15:07:19 -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=U8n52GDhp+0XaI8eZaqbkqW1/W004y/nkmgvLLFcxh8=; b=nFd+kfLfkvrSiSaRnh+2EjiBACC9mepQXcboRsS5bgRlpwilj9/E0cTUjXrEVJGi72 aa5OdVMq75R6eitqeTMB9o6xd4mOo1slopxjEoUIAOVGhrjXWn8AwHoFWHwkn51t+ptJ rCk1XNmq5yyE0fK+dbDKGsIuD2dCMHKnjiRuwYZ/peNxS3zB9sOzSpkhBuUHVXcqaPpA HIle3L9TtofoQR6t5zKRyvw0FxcOhteF5r41p9wiA//EqyGFfeqEKMOIHX7Z6NFYS9B1 OrCKFY4iRYg4uAOwGW79v+OUf7awObljbRfN82NTXeq3f85ishP/gfbmUQEg/Q+1RRbq cHZw== X-Received: by 10.181.12.20 with SMTP id em20mr29528246wid.28.1436393239034; Wed, 08 Jul 2015 15:07:19 -0700 (PDT) Received: from localhost.localdomain (ip-89-102-11-63.net.upcbroadband.cz. [89.102.11.63]) by smtp.gmail.com with ESMTPSA id fo17sm5483921wjc.46.2015.07.08.15.07.17 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 08 Jul 2015 15:07:17 -0700 (PDT) From: Mateusz Guzik To: Konstantin Belousov Cc: rwatson@FreeBSD.org, freebsd-fs@freebsd.org, Mateusz Guzik Subject: [PATCH 2/4] vfs: avoid spurious vref/vrele for absolute lookups Date: Thu, 9 Jul 2015 00:07:09 +0200 Message-Id: <1436393231-5831-3-git-send-email-mjguzik@gmail.com> X-Mailer: git-send-email 2.4.3 In-Reply-To: <1436393231-5831-1-git-send-email-mjguzik@gmail.com> References: <20150707085857.GZ2080@kib.kiev.ua> <1436393231-5831-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: Wed, 08 Jul 2015 22:07:21 -0000 From: Mateusz Guzik namei used to vref fd_cdir, which was immediatley vrele'd on entry to the loop. Check for absolute lookup and vref the right vnode the first time. --- sys/kern/vfs_lookup.c | 70 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 24 deletions(-) diff --git a/sys/kern/vfs_lookup.c b/sys/kern/vfs_lookup.c index 20f8e96..e434464 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. * @@ -222,7 +243,18 @@ namei(struct nameidata *ndp) 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); + FILEDESC_SUNLOCK(fdp); + if (error != 0) { + vrele(ndp->ni_rootdir); + if (ndp->ni_startdir != NULL) + vrele(ndp->ni_startdir); + namei_cleanup_cnp(cnp); + return (error); + } + } else { if (ndp->ni_startdir != NULL) { dp = ndp->ni_startdir; error = 0; @@ -276,29 +308,6 @@ namei(struct nameidata *ndp) 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 - vrele(ndp->ni_rootdir); - 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) { @@ -375,6 +384,19 @@ 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) { + vrele(ndp->ni_rootdir); + namei_cleanup_cnp(cnp); + return (error); + } + } } vrele(ndp->ni_rootdir); namei_cleanup_cnp(cnp); -- 2.4.5