From patchwork Thu Jul 14 15:28:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Layton X-Patchwork-Id: 12918112 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 31B50CCA481 for ; Thu, 14 Jul 2022 15:28:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240193AbiGNP20 (ORCPT ); Thu, 14 Jul 2022 11:28:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34560 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240163AbiGNP20 (ORCPT ); Thu, 14 Jul 2022 11:28:26 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 18AF7491D1 for ; Thu, 14 Jul 2022 08:28:25 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id B48CEB82483 for ; Thu, 14 Jul 2022 15:28:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E411EC3411C; Thu, 14 Jul 2022 15:28:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1657812502; bh=jzHqgeiO5g4IVxSmcIItv4fi6ExokyezGGmOSRUg1Cs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hvr7UTuLdYH5DArB2c+QyczhMl0VEvRsV5RUX9eGirKI+YDJfbOSJItABeOQLflwp 6vWVLRoYXkiskBPNvgo6DobJirqcvVm682rt9ltOUl18+h7o/M/vfB42tjJpwZL+gm Ba+o4RSnTSICosK++WRrBnCgkMsIMFmXwpx1noq9T+kq3kzSQYBz0t1wxD6gttU5Ou ybpInQ0x+v+Zi8T66ko6uW6dR+DroAQvnc3hPHPh4ISF9K4+WZqlDDSWdshwBfnmqr LSsUGNwo/lMxmnWCUCB8hoRDq1Ay6MvcVHNvE64r2feSy4gieq7DROJDab9MYz+w3d dBfwERImjoYLA== From: Jeff Layton To: chuck.lever@oracle.com Cc: neilb@suse.de, linux-nfs@vger.kernel.org Subject: [RFC PATCH 1/3] nfsd: drop fh argument from alloc_init_deleg Date: Thu, 14 Jul 2022 11:28:17 -0400 Message-Id: <20220714152819.128276-2-jlayton@kernel.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220714152819.128276-1-jlayton@kernel.org> References: <20220714152819.128276-1-jlayton@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org Currently, we pass the fh of the opened file down through several functions so that alloc_init_deleg can pass it to delegation_blocked. The filehandle of the open file is available in the nfs4_file struct however, so there's no need to pass it in a separate argument. Drop the argument from alloc_init_deleg, and follow suit in several of the callers. Signed-off-by: Jeff Layton --- fs/nfsd/nfs4state.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c index 994bd11bafe0..4f81c0bbd27b 100644 --- a/fs/nfsd/nfs4state.c +++ b/fs/nfsd/nfs4state.c @@ -1131,7 +1131,6 @@ static void block_delegations(struct knfsd_fh *fh) static struct nfs4_delegation * alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp, - struct svc_fh *current_fh, struct nfs4_clnt_odstate *odstate) { struct nfs4_delegation *dp; @@ -1141,7 +1140,7 @@ alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp, n = atomic_long_inc_return(&num_delegations); if (n < 0 || n > max_delegations) goto out_dec; - if (delegation_blocked(¤t_fh->fh_handle)) + if (delegation_blocked(&fp->fi_fhandle)) goto out_dec; dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg)); if (dp == NULL) @@ -5261,7 +5260,7 @@ static int nfsd4_check_conflicting_opens(struct nfs4_client *clp, } static struct nfs4_delegation * -nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh, +nfs4_set_delegation(struct nfs4_client *clp, struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate) { int status = 0; @@ -5306,7 +5305,7 @@ nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh, return ERR_PTR(status); status = -ENOMEM; - dp = alloc_init_deleg(clp, fp, fh, odstate); + dp = alloc_init_deleg(clp, fp, odstate); if (!dp) goto out_delegees; @@ -5374,8 +5373,7 @@ static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status) * proper support for them. */ static void -nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, - struct nfs4_ol_stateid *stp) +nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp) { struct nfs4_delegation *dp; struct nfs4_openowner *oo = openowner(stp->st_stateowner); @@ -5407,7 +5405,7 @@ nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, default: goto out_no_deleg; } - dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate); + dp = nfs4_set_delegation(clp, stp->st_stid.sc_file, stp->st_clnt_odstate); if (IS_ERR(dp)) goto out_no_deleg; @@ -5539,7 +5537,7 @@ nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nf * Attempt to hand out a delegation. No error return, because the * OPEN succeeds even if we fail. */ - nfs4_open_delegation(current_fh, open, stp); + nfs4_open_delegation(open, stp); nodeleg: status = nfs_ok; trace_nfsd_open(&stp->st_stid.sc_stateid); From patchwork Thu Jul 14 15:28:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Layton X-Patchwork-Id: 12918110 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BDFD3C433EF for ; Thu, 14 Jul 2022 15:28:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240135AbiGNP2Z (ORCPT ); Thu, 14 Jul 2022 11:28:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34546 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239872AbiGNP2Y (ORCPT ); Thu, 14 Jul 2022 11:28:24 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E1F61491D1 for ; Thu, 14 Jul 2022 08:28:23 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 7C66B61F32 for ; Thu, 14 Jul 2022 15:28:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 80E07C341C8; Thu, 14 Jul 2022 15:28:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1657812502; bh=4dH+Z8EaITtBuQui+zpdms7bIDirkY3nsFZuy8eBYPI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MQFpgih/YnaSVdMdHNY1sdqhWQfs3EOYverkFlmX+DEHVXnRG/2hDmL8XfU5IP/Ve rAbyMcaY30VFcBszL0ce2WBpfofUzCsFNFisOtELzxThxHZEZziJkhAlXkAKlbF5NH qM8SxQzylpmCvAywCIajVLWE+HPXuUd2lPN/3sUlr1HdKUM5gVqryE5fWM51XJLALF NbAff0P4MSFBd1feVR2EJClb6tJfSD7Qasie8Htt+ykMocrXvPrq6Ft7946py4C4/S SHSy69LO50Lb0X5DvDSViiGXPR+Q9oHMrOCVflbL/5G/uYP2EfSttKKQt296QMXCLz G0M8BkprwOfyw== From: Jeff Layton To: chuck.lever@oracle.com Cc: neilb@suse.de, linux-nfs@vger.kernel.org Subject: [RFC PATCH 2/3] nfsd: rework arguments to nfs4_set_delegation Date: Thu, 14 Jul 2022 11:28:18 -0400 Message-Id: <20220714152819.128276-3-jlayton@kernel.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220714152819.128276-1-jlayton@kernel.org> References: <20220714152819.128276-1-jlayton@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org We'll need the nfs4_open to vet the filename. Change nfs4_set_delegation to take the same arguments are nfs4_open_delegation. Signed-off-by: Jeff Layton --- fs/nfsd/nfs4state.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c index 4f81c0bbd27b..347794028c98 100644 --- a/fs/nfsd/nfs4state.c +++ b/fs/nfsd/nfs4state.c @@ -5260,10 +5260,12 @@ static int nfsd4_check_conflicting_opens(struct nfs4_client *clp, } static struct nfs4_delegation * -nfs4_set_delegation(struct nfs4_client *clp, - struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate) +nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp) { int status = 0; + struct nfs4_client *clp = stp->st_stid.sc_client; + struct nfs4_file *fp = stp->st_stid.sc_file; + struct nfs4_clnt_odstate *odstate = stp->st_clnt_odstate; struct nfs4_delegation *dp; struct nfsd_file *nf; struct file_lock *fl; @@ -5405,7 +5407,7 @@ nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp) default: goto out_no_deleg; } - dp = nfs4_set_delegation(clp, stp->st_stid.sc_file, stp->st_clnt_odstate); + dp = nfs4_set_delegation(open, stp); if (IS_ERR(dp)) goto out_no_deleg; From patchwork Thu Jul 14 15:28:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Layton X-Patchwork-Id: 12918113 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 33B5CCCA47B for ; Thu, 14 Jul 2022 15:28:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240215AbiGNP21 (ORCPT ); Thu, 14 Jul 2022 11:28:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34576 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240163AbiGNP21 (ORCPT ); Thu, 14 Jul 2022 11:28:27 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4F79F491D1 for ; Thu, 14 Jul 2022 08:28:26 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id F08E6B82591 for ; Thu, 14 Jul 2022 15:28:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1E516C34115; Thu, 14 Jul 2022 15:28:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1657812503; bh=0NqYyetOGEUAnIGbaoMuektCVvnQdJT1Zf1YEgWTtCI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WHGGHxcQvG6XOniT8Sjuc8aOrjTO8F64vhd1Z6BRP/VXaNPNp22OB2utd/NyXVL9B ZGXufSGidqiWp+d4vuBFt9Gzf1pcuv9yvDpf1qPgt/Cd8s5oVNiR7qBZTSKnqmPV3o E0PYmV3lEh1Otdzy9uPy/dt+M1xva75xo5Ab23l6UwjTCPNqACgb2cQuzXuX4vkQfD /4bPorZ/8xaDFQFvbu84BddsR9CptvR8jG4gv+VsMtNoXoRV2Qh+woZJHwJzGmwmGP ll/Kkp8vGlsoOenVbdWk2PyGtmGUoOR9o1Pmuj4/6DaJpnwDGntZDRMcvP/t/0rcty YgeNwdvY0lb9A== From: Jeff Layton To: chuck.lever@oracle.com Cc: neilb@suse.de, linux-nfs@vger.kernel.org Subject: [RFC PATCH 3/3] nfsd: vet the opened dentry after setting a delegation Date: Thu, 14 Jul 2022 11:28:19 -0400 Message-Id: <20220714152819.128276-4-jlayton@kernel.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220714152819.128276-1-jlayton@kernel.org> References: <20220714152819.128276-1-jlayton@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org Between opening a file and setting a delegation on it, someone could rename or unlink the dentry. If this happens, we do not want to grant a delegation on the open. Take the i_rwsem before setting the delegation. If we're granted a lease, redo the lookup of the file being opened and validate that the resulting dentry matches the one in the open file description. We only need to do this for the CLAIM_NULL open case however. Signed-off-by: Jeff Layton --- fs/nfsd/nfs4state.c | 55 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 5 deletions(-) diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c index 347794028c98..e5c7f6690d2d 100644 --- a/fs/nfsd/nfs4state.c +++ b/fs/nfsd/nfs4state.c @@ -1172,6 +1172,7 @@ alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp, void nfs4_put_stid(struct nfs4_stid *s) + __releases(&clp->cl_lock) { struct nfs4_file *fp = s->sc_file; struct nfs4_client *clp = s->sc_client; @@ -5259,13 +5260,41 @@ static int nfsd4_check_conflicting_opens(struct nfs4_client *clp, return 0; } +/* + * It's possible that between opening the dentry and setting the delegation, + * that it has been renamed or unlinked. Redo the lookup to validate that this + * hasn't happened. + */ +static int +nfsd4_vet_deleg_parent(struct nfsd4_open *open, struct nfs4_file *fp, struct dentry *parent) +{ + struct dentry *child; + + /* Only need to do this if this is an open-by-name */ + if (!parent) + return 0; + + child = lookup_one_len(open->op_fname, parent, open->op_fnamelen); + if (IS_ERR(child)) + return PTR_ERR(child); + dput(child); + + /* Uh-oh, there has been a rename or unlink of the file. No deleg! */ + if (child != file_dentry(fp->fi_deleg_file->nf_file)) + return -EAGAIN; + + return 0; +} + static struct nfs4_delegation * -nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp) +nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp, + struct svc_fh *parent_fh) { int status = 0; struct nfs4_client *clp = stp->st_stid.sc_client; struct nfs4_file *fp = stp->st_stid.sc_file; struct nfs4_clnt_odstate *odstate = stp->st_clnt_odstate; + struct dentry *parent = parent_fh ? parent_fh->fh_dentry : NULL; struct nfs4_delegation *dp; struct nfsd_file *nf; struct file_lock *fl; @@ -5315,11 +5344,23 @@ nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp) if (!fl) goto out_clnt_odstate; + if (parent) + inode_lock_shared_nested(d_inode(parent), I_MUTEX_PARENT); status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL); if (fl) locks_free_lock(fl); - if (status) + if (status) { + if (parent) + inode_unlock_shared(d_inode(parent)); goto out_clnt_odstate; + } + + status = nfsd4_vet_deleg_parent(open, fp, parent); + if (parent) + inode_unlock_shared(d_inode(parent)); + if (status) + goto out_unlock; + status = nfsd4_check_conflicting_opens(clp, fp); if (status) goto out_unlock; @@ -5375,11 +5416,13 @@ static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status) * proper support for them. */ static void -nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp) +nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp, + struct svc_fh *current_fh) { struct nfs4_delegation *dp; struct nfs4_openowner *oo = openowner(stp->st_stateowner); struct nfs4_client *clp = stp->st_stid.sc_client; + struct svc_fh *parent_fh = NULL; int cb_up; int status = 0; @@ -5393,6 +5436,8 @@ nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp) goto out_no_deleg; break; case NFS4_OPEN_CLAIM_NULL: + parent_fh = current_fh; + fallthrough; case NFS4_OPEN_CLAIM_FH: /* * Let's not give out any delegations till everyone's @@ -5407,7 +5452,7 @@ nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp) default: goto out_no_deleg; } - dp = nfs4_set_delegation(open, stp); + dp = nfs4_set_delegation(open, stp, parent_fh); if (IS_ERR(dp)) goto out_no_deleg; @@ -5539,7 +5584,7 @@ nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nf * Attempt to hand out a delegation. No error return, because the * OPEN succeeds even if we fail. */ - nfs4_open_delegation(open, stp); + nfs4_open_delegation(open, stp, &resp->cstate.current_fh); nodeleg: status = nfs_ok; trace_nfsd_open(&stp->st_stid.sc_stateid);