From patchwork Fri Jun 14 03:44:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Snitzer X-Patchwork-Id: 13697833 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BB695ECC for ; Fri, 14 Jun 2024 03:44:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336669; cv=none; b=YdHPMr0sQRpZbRG+NMmY4n+HUfXAU7e/54uYnZ074kE2mhZTXSB4a0nivfqB7nUEixIHiMepz4LUCOouiG2JNu6jUFCF4XcosU179OPFxi+xg5Llfo52GiLT3YJdpQg8fQ0QiWWb2owm4/iKLYdaSkGQQLE6i+It1/GkVJb24X4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336669; c=relaxed/simple; bh=zqo5A9VtZlcvmmu0XkGwH+wtxeFhmulZftxFgSSuOAc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=T+U0n4oPTvTPiaDIlKmGZPW44EiYdR9hiTSKUl8Vb9dfh5bFOmxqi3hC99oKD0Y+W7/G59+B0ImDLYUUnuQA9/tb0F6MBwpNTDcKZ+QGH20X9SfvHLVwoEMe0nuXLmARY8G/ibzD+Nx5bUnLtUxAJ+zVZSN7E04mdd1uwyqTy80= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lrNyRny7; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="lrNyRny7" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E7A48C4AF48; Fri, 14 Jun 2024 03:44:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718336669; bh=zqo5A9VtZlcvmmu0XkGwH+wtxeFhmulZftxFgSSuOAc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lrNyRny7FiZI6JNPNbrvqbmd9CQXjdB3fnogfCxTCMBkQqQXxBS+0K5OvyODEu7Iw pxghXObcBdl9wrwowGcf+l00btqUDd8Laa9tlm5429zFp0g1kgpZsX3lqF6rHIOSgg 2B+h6v0v5NZuvxuKHJxy/Q1mPE16W3OJm6KZ1wW8sxOrdbDRzfzo0zh8rTSUekmYK5 GH6d8ap62vSgUPMhbchFKF+wLPRBLy5F++rMMvS2qLuovEx3lYONbWhFcPx0+zvaKe R74wplAETY8Xb+OCOOQ2iSxwLm6KtiINKggj0E10WbXSxAU8uPR2cAPf4CWLidGyPh JX6L0MrC9NY7w== From: Mike Snitzer To: linux-nfs@vger.kernel.org Cc: Jeff Layton , Chuck Lever , Trond Myklebust , NeilBrown , snitzer@hammerspace.com Subject: [PATCH v3 01/18] nfs: pass nfs_client to nfs_initiate_pgio Date: Thu, 13 Jun 2024 23:44:09 -0400 Message-ID: <20240614034426.31043-2-snitzer@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240614034426.31043-1-snitzer@kernel.org> References: <20240614034426.31043-1-snitzer@kernel.org> Precedence: bulk X-Mailing-List: linux-nfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Weston Andros Adamson The nfs_client is needed for localio support. Otherwise it won't be possible to disable localio if it is attempted but fails. Signed-off-by: Weston Andros Adamson Signed-off-by: Lance Shelton Signed-off-by: Trond Myklebust Signed-off-by: Mike Snitzer --- fs/nfs/filelayout/filelayout.c | 4 ++-- fs/nfs/flexfilelayout/flexfilelayout.c | 6 ++++-- fs/nfs/internal.h | 5 +++-- fs/nfs/pagelist.c | 10 ++++++---- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/fs/nfs/filelayout/filelayout.c b/fs/nfs/filelayout/filelayout.c index 29d84dc66ca3..43e16e9e0176 100644 --- a/fs/nfs/filelayout/filelayout.c +++ b/fs/nfs/filelayout/filelayout.c @@ -486,7 +486,7 @@ filelayout_read_pagelist(struct nfs_pgio_header *hdr) hdr->mds_offset = offset; /* Perform an asynchronous read to ds */ - nfs_initiate_pgio(ds_clnt, hdr, hdr->cred, + nfs_initiate_pgio(ds->ds_clp, ds_clnt, hdr, hdr->cred, NFS_PROTO(hdr->inode), &filelayout_read_call_ops, 0, RPC_TASK_SOFTCONN); return PNFS_ATTEMPTED; @@ -528,7 +528,7 @@ filelayout_write_pagelist(struct nfs_pgio_header *hdr, int sync) hdr->args.offset = filelayout_get_dserver_offset(lseg, offset); /* Perform an asynchronous write */ - nfs_initiate_pgio(ds_clnt, hdr, hdr->cred, + nfs_initiate_pgio(ds->ds_clp, ds_clnt, hdr, hdr->cred, NFS_PROTO(hdr->inode), &filelayout_write_call_ops, sync, RPC_TASK_SOFTCONN); return PNFS_ATTEMPTED; diff --git a/fs/nfs/flexfilelayout/flexfilelayout.c b/fs/nfs/flexfilelayout/flexfilelayout.c index 24188af56d5b..327f1a5c9fbe 100644 --- a/fs/nfs/flexfilelayout/flexfilelayout.c +++ b/fs/nfs/flexfilelayout/flexfilelayout.c @@ -1803,7 +1803,8 @@ ff_layout_read_pagelist(struct nfs_pgio_header *hdr) hdr->mds_offset = offset; /* Perform an asynchronous read to ds */ - nfs_initiate_pgio(ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops, + nfs_initiate_pgio(ds->ds_clp, ds_clnt, hdr, ds_cred, + ds->ds_clp->rpc_ops, vers == 3 ? &ff_layout_read_call_ops_v3 : &ff_layout_read_call_ops_v4, 0, RPC_TASK_SOFTCONN); @@ -1871,7 +1872,8 @@ ff_layout_write_pagelist(struct nfs_pgio_header *hdr, int sync) hdr->args.offset = offset; /* Perform an asynchronous write */ - nfs_initiate_pgio(ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops, + nfs_initiate_pgio(ds->ds_clp, ds_clnt, hdr, ds_cred, + ds->ds_clp->rpc_ops, vers == 3 ? &ff_layout_write_call_ops_v3 : &ff_layout_write_call_ops_v4, sync, RPC_TASK_SOFTCONN); diff --git a/fs/nfs/internal.h b/fs/nfs/internal.h index 9f0f4534744b..a9c0c29f7804 100644 --- a/fs/nfs/internal.h +++ b/fs/nfs/internal.h @@ -306,8 +306,9 @@ extern const struct nfs_pageio_ops nfs_pgio_rw_ops; struct nfs_pgio_header *nfs_pgio_header_alloc(const struct nfs_rw_ops *); void nfs_pgio_header_free(struct nfs_pgio_header *); int nfs_generic_pgio(struct nfs_pageio_descriptor *, struct nfs_pgio_header *); -int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_header *hdr, - const struct cred *cred, const struct nfs_rpc_ops *rpc_ops, +int nfs_initiate_pgio(struct nfs_client *clp, struct rpc_clnt *rpc_clnt, + struct nfs_pgio_header *hdr, const struct cred *cred, + const struct nfs_rpc_ops *rpc_ops, const struct rpc_call_ops *call_ops, int how, int flags); void nfs_free_request(struct nfs_page *req); struct nfs_pgio_mirror * diff --git a/fs/nfs/pagelist.c b/fs/nfs/pagelist.c index 6efb5068c116..d9b795c538cd 100644 --- a/fs/nfs/pagelist.c +++ b/fs/nfs/pagelist.c @@ -844,8 +844,9 @@ static void nfs_pgio_prepare(struct rpc_task *task, void *calldata) rpc_exit(task, err); } -int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_header *hdr, - const struct cred *cred, const struct nfs_rpc_ops *rpc_ops, +int nfs_initiate_pgio(struct nfs_client *clp, struct rpc_clnt *rpc_clnt, + struct nfs_pgio_header *hdr, const struct cred *cred, + const struct nfs_rpc_ops *rpc_ops, const struct rpc_call_ops *call_ops, int how, int flags) { struct rpc_task *task; @@ -855,7 +856,7 @@ int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_header *hdr, .rpc_cred = cred, }; struct rpc_task_setup task_setup_data = { - .rpc_client = clnt, + .rpc_client = rpc_clnt, .task = &hdr->task, .rpc_message = &msg, .callback_ops = call_ops, @@ -1070,7 +1071,8 @@ static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc) if (ret == 0) { if (NFS_SERVER(hdr->inode)->nfs_client->cl_minorversion) task_flags = RPC_TASK_MOVEABLE; - ret = nfs_initiate_pgio(NFS_CLIENT(hdr->inode), + ret = nfs_initiate_pgio(NFS_SERVER(hdr->inode)->nfs_client, + NFS_CLIENT(hdr->inode), hdr, hdr->cred, NFS_PROTO(hdr->inode), From patchwork Fri Jun 14 03:44:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Snitzer X-Patchwork-Id: 13697834 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EDDC9ECC for ; Fri, 14 Jun 2024 03:44:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336671; cv=none; b=BcFd2zvK1rP2EXHgcDdH2HswrBcPizc6aHXRVXvoVGuGBYttxIm5g1OXVPZ9NlTokNmNmgrcwNn5pbxoEU9B99WxW7/0uH6O4Ze0sRdD0SYWdti7jzrLgKolCvD3nFwAGtuYZ37WXZx7hU4kAV7sd5aLoHnZwQGf3ByXysS/iMs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336671; c=relaxed/simple; bh=YJoyVHLFZHGnnH7iXlxdzrZ6OyrIVltCHpztKjaNMD4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Y/ky37a5qHkOFZ8gHKJtVvd6rKnGc9gV+TKnv2QLsb0knEa/mKBzCkSa/a3i8i/Nd96olKtxd6tBInK9qrFmMoZyX5/DLwKroIW5w50O9xg7Csz+Dj6DCfsOU38kza1O1cxA0YdnpX3hoY9PcX1BZ9CWkW3bpWFlpFZHJaA96nE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gsxibX6Y; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="gsxibX6Y" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 50599C4AF1D; Fri, 14 Jun 2024 03:44:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718336670; bh=YJoyVHLFZHGnnH7iXlxdzrZ6OyrIVltCHpztKjaNMD4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gsxibX6YYcjAv+t/4pCMSfculsyQG8UDirsGBkAeWfqFEZV3UChbm3Sblf1H08zG7 ZhNY2rr0sbkmqFBAIqBM52Ax+Ef8+boIZVu/Ue4ItfPmcqQu8WPS1m18BaRaplAOyf H2KqWJ3Nsl1qclbbcjAFN40EZOZMlUjL3Rqyw3/QPy8XKBWpzBq8OPV78vP76S0jOl woSll/0A7VcXFT3viJc2btf+5EWmaJP0ZpS7y96ksRd2mXADug5tx2giQlZJtOq9wj 2CkdFAfJSxqjYva2gV7uMgN7UBO+G1C4pX8ix9rKAo+eK/3HHbKtUv98H3kJ2dOCIx 3/QPTUJdmu8vA== From: Mike Snitzer To: linux-nfs@vger.kernel.org Cc: Jeff Layton , Chuck Lever , Trond Myklebust , NeilBrown , snitzer@hammerspace.com Subject: [PATCH v3 02/18] nfs: pass descriptor thru nfs_initiate_pgio path Date: Thu, 13 Jun 2024 23:44:10 -0400 Message-ID: <20240614034426.31043-3-snitzer@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240614034426.31043-1-snitzer@kernel.org> References: <20240614034426.31043-1-snitzer@kernel.org> Precedence: bulk X-Mailing-List: linux-nfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Weston Andros Adamson This is needed for localio support. Signed-off-by: Weston Andros Adamson Signed-off-by: Peng Tao Signed-off-by: Lance Shelton Signed-off-by: Trond Myklebust Signed-off-by: Mike Snitzer --- fs/nfs/blocklayout/blocklayout.c | 6 ++++-- fs/nfs/filelayout/filelayout.c | 10 ++++++---- fs/nfs/flexfilelayout/flexfilelayout.c | 10 ++++++---- fs/nfs/internal.h | 6 +++--- fs/nfs/pagelist.c | 6 ++++-- fs/nfs/pnfs.c | 24 +++++++++++++----------- fs/nfs/pnfs.h | 6 ++++-- 7 files changed, 40 insertions(+), 28 deletions(-) diff --git a/fs/nfs/blocklayout/blocklayout.c b/fs/nfs/blocklayout/blocklayout.c index 6be13e0ec170..6a61ddd1835f 100644 --- a/fs/nfs/blocklayout/blocklayout.c +++ b/fs/nfs/blocklayout/blocklayout.c @@ -227,7 +227,8 @@ bl_end_par_io_read(void *data) } static enum pnfs_try_status -bl_read_pagelist(struct nfs_pgio_header *header) +bl_read_pagelist(struct nfs_pageio_descriptor *desc, + struct nfs_pgio_header *header) { struct pnfs_block_layout *bl = BLK_LSEG2EXT(header->lseg); struct pnfs_block_dev_map map = { .start = NFS4_MAX_UINT64 }; @@ -372,7 +373,8 @@ static void bl_end_par_io_write(void *data) } static enum pnfs_try_status -bl_write_pagelist(struct nfs_pgio_header *header, int sync) +bl_write_pagelist(struct nfs_pageio_descriptor *desc, + struct nfs_pgio_header *header, int sync) { struct pnfs_block_layout *bl = BLK_LSEG2EXT(header->lseg); struct pnfs_block_dev_map map = { .start = NFS4_MAX_UINT64 }; diff --git a/fs/nfs/filelayout/filelayout.c b/fs/nfs/filelayout/filelayout.c index 43e16e9e0176..f9b600c4a2b5 100644 --- a/fs/nfs/filelayout/filelayout.c +++ b/fs/nfs/filelayout/filelayout.c @@ -447,7 +447,8 @@ static const struct rpc_call_ops filelayout_commit_call_ops = { }; static enum pnfs_try_status -filelayout_read_pagelist(struct nfs_pgio_header *hdr) +filelayout_read_pagelist(struct nfs_pageio_descriptor *desc, + struct nfs_pgio_header *hdr) { struct pnfs_layout_segment *lseg = hdr->lseg; struct nfs4_pnfs_ds *ds; @@ -486,7 +487,7 @@ filelayout_read_pagelist(struct nfs_pgio_header *hdr) hdr->mds_offset = offset; /* Perform an asynchronous read to ds */ - nfs_initiate_pgio(ds->ds_clp, ds_clnt, hdr, hdr->cred, + nfs_initiate_pgio(desc, ds->ds_clp, ds_clnt, hdr, hdr->cred, NFS_PROTO(hdr->inode), &filelayout_read_call_ops, 0, RPC_TASK_SOFTCONN); return PNFS_ATTEMPTED; @@ -494,7 +495,8 @@ filelayout_read_pagelist(struct nfs_pgio_header *hdr) /* Perform async writes. */ static enum pnfs_try_status -filelayout_write_pagelist(struct nfs_pgio_header *hdr, int sync) +filelayout_write_pagelist(struct nfs_pageio_descriptor *desc, + struct nfs_pgio_header *hdr, int sync) { struct pnfs_layout_segment *lseg = hdr->lseg; struct nfs4_pnfs_ds *ds; @@ -528,7 +530,7 @@ filelayout_write_pagelist(struct nfs_pgio_header *hdr, int sync) hdr->args.offset = filelayout_get_dserver_offset(lseg, offset); /* Perform an asynchronous write */ - nfs_initiate_pgio(ds->ds_clp, ds_clnt, hdr, hdr->cred, + nfs_initiate_pgio(desc, ds->ds_clp, ds_clnt, hdr, hdr->cred, NFS_PROTO(hdr->inode), &filelayout_write_call_ops, sync, RPC_TASK_SOFTCONN); return PNFS_ATTEMPTED; diff --git a/fs/nfs/flexfilelayout/flexfilelayout.c b/fs/nfs/flexfilelayout/flexfilelayout.c index 327f1a5c9fbe..22c0e8014afb 100644 --- a/fs/nfs/flexfilelayout/flexfilelayout.c +++ b/fs/nfs/flexfilelayout/flexfilelayout.c @@ -1751,7 +1751,8 @@ static const struct rpc_call_ops ff_layout_commit_call_ops_v4 = { }; static enum pnfs_try_status -ff_layout_read_pagelist(struct nfs_pgio_header *hdr) +ff_layout_read_pagelist(struct nfs_pageio_descriptor *desc, + struct nfs_pgio_header *hdr) { struct pnfs_layout_segment *lseg = hdr->lseg; struct nfs4_pnfs_ds *ds; @@ -1803,7 +1804,7 @@ ff_layout_read_pagelist(struct nfs_pgio_header *hdr) hdr->mds_offset = offset; /* Perform an asynchronous read to ds */ - nfs_initiate_pgio(ds->ds_clp, ds_clnt, hdr, ds_cred, + nfs_initiate_pgio(desc, ds->ds_clp, ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops, vers == 3 ? &ff_layout_read_call_ops_v3 : &ff_layout_read_call_ops_v4, @@ -1822,7 +1823,8 @@ ff_layout_read_pagelist(struct nfs_pgio_header *hdr) /* Perform async writes. */ static enum pnfs_try_status -ff_layout_write_pagelist(struct nfs_pgio_header *hdr, int sync) +ff_layout_write_pagelist(struct nfs_pageio_descriptor *desc, + struct nfs_pgio_header *hdr, int sync) { struct pnfs_layout_segment *lseg = hdr->lseg; struct nfs4_pnfs_ds *ds; @@ -1872,7 +1874,7 @@ ff_layout_write_pagelist(struct nfs_pgio_header *hdr, int sync) hdr->args.offset = offset; /* Perform an asynchronous write */ - nfs_initiate_pgio(ds->ds_clp, ds_clnt, hdr, ds_cred, + nfs_initiate_pgio(desc, ds->ds_clp, ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops, vers == 3 ? &ff_layout_write_call_ops_v3 : &ff_layout_write_call_ops_v4, diff --git a/fs/nfs/internal.h b/fs/nfs/internal.h index a9c0c29f7804..f6e56fdd8bc2 100644 --- a/fs/nfs/internal.h +++ b/fs/nfs/internal.h @@ -306,9 +306,9 @@ extern const struct nfs_pageio_ops nfs_pgio_rw_ops; struct nfs_pgio_header *nfs_pgio_header_alloc(const struct nfs_rw_ops *); void nfs_pgio_header_free(struct nfs_pgio_header *); int nfs_generic_pgio(struct nfs_pageio_descriptor *, struct nfs_pgio_header *); -int nfs_initiate_pgio(struct nfs_client *clp, struct rpc_clnt *rpc_clnt, - struct nfs_pgio_header *hdr, const struct cred *cred, - const struct nfs_rpc_ops *rpc_ops, +int nfs_initiate_pgio(struct nfs_pageio_descriptor *, struct nfs_client *clp, + struct rpc_clnt *rpc_clnt, struct nfs_pgio_header *hdr, + const struct cred *cred, const struct nfs_rpc_ops *rpc_ops, const struct rpc_call_ops *call_ops, int how, int flags); void nfs_free_request(struct nfs_page *req); struct nfs_pgio_mirror * diff --git a/fs/nfs/pagelist.c b/fs/nfs/pagelist.c index d9b795c538cd..3786d767e2ff 100644 --- a/fs/nfs/pagelist.c +++ b/fs/nfs/pagelist.c @@ -844,7 +844,8 @@ static void nfs_pgio_prepare(struct rpc_task *task, void *calldata) rpc_exit(task, err); } -int nfs_initiate_pgio(struct nfs_client *clp, struct rpc_clnt *rpc_clnt, +int nfs_initiate_pgio(struct nfs_pageio_descriptor *desc, + struct nfs_client *clp, struct rpc_clnt *rpc_clnt, struct nfs_pgio_header *hdr, const struct cred *cred, const struct nfs_rpc_ops *rpc_ops, const struct rpc_call_ops *call_ops, int how, int flags) @@ -1071,7 +1072,8 @@ static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc) if (ret == 0) { if (NFS_SERVER(hdr->inode)->nfs_client->cl_minorversion) task_flags = RPC_TASK_MOVEABLE; - ret = nfs_initiate_pgio(NFS_SERVER(hdr->inode)->nfs_client, + ret = nfs_initiate_pgio(desc, + NFS_SERVER(hdr->inode)->nfs_client, NFS_CLIENT(hdr->inode), hdr, hdr->cred, diff --git a/fs/nfs/pnfs.c b/fs/nfs/pnfs.c index b5834728f31b..c9015179b72c 100644 --- a/fs/nfs/pnfs.c +++ b/fs/nfs/pnfs.c @@ -2885,10 +2885,11 @@ pnfs_write_through_mds(struct nfs_pageio_descriptor *desc, } static enum pnfs_try_status -pnfs_try_to_write_data(struct nfs_pgio_header *hdr, - const struct rpc_call_ops *call_ops, - struct pnfs_layout_segment *lseg, - int how) +pnfs_try_to_write_data(struct nfs_pageio_descriptor *desc, + struct nfs_pgio_header *hdr, + const struct rpc_call_ops *call_ops, + struct pnfs_layout_segment *lseg, + int how) { struct inode *inode = hdr->inode; enum pnfs_try_status trypnfs; @@ -2898,7 +2899,7 @@ pnfs_try_to_write_data(struct nfs_pgio_header *hdr, dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__, inode->i_ino, hdr->args.count, hdr->args.offset, how); - trypnfs = nfss->pnfs_curr_ld->write_pagelist(hdr, how); + trypnfs = nfss->pnfs_curr_ld->write_pagelist(desc, hdr, how); if (trypnfs != PNFS_NOT_ATTEMPTED) nfs_inc_stats(inode, NFSIOS_PNFS_WRITE); dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs); @@ -2913,7 +2914,7 @@ pnfs_do_write(struct nfs_pageio_descriptor *desc, struct pnfs_layout_segment *lseg = desc->pg_lseg; enum pnfs_try_status trypnfs; - trypnfs = pnfs_try_to_write_data(hdr, call_ops, lseg, how); + trypnfs = pnfs_try_to_write_data(desc, hdr, call_ops, lseg, how); switch (trypnfs) { case PNFS_NOT_ATTEMPTED: pnfs_write_through_mds(desc, hdr); @@ -3012,9 +3013,10 @@ pnfs_read_through_mds(struct nfs_pageio_descriptor *desc, * Call the appropriate parallel I/O subsystem read function. */ static enum pnfs_try_status -pnfs_try_to_read_data(struct nfs_pgio_header *hdr, - const struct rpc_call_ops *call_ops, - struct pnfs_layout_segment *lseg) +pnfs_try_to_read_data(struct nfs_pageio_descriptor *desc, + struct nfs_pgio_header *hdr, + const struct rpc_call_ops *call_ops, + struct pnfs_layout_segment *lseg) { struct inode *inode = hdr->inode; struct nfs_server *nfss = NFS_SERVER(inode); @@ -3025,7 +3027,7 @@ pnfs_try_to_read_data(struct nfs_pgio_header *hdr, dprintk("%s: Reading ino:%lu %u@%llu\n", __func__, inode->i_ino, hdr->args.count, hdr->args.offset); - trypnfs = nfss->pnfs_curr_ld->read_pagelist(hdr); + trypnfs = nfss->pnfs_curr_ld->read_pagelist(desc, hdr); if (trypnfs != PNFS_NOT_ATTEMPTED) nfs_inc_stats(inode, NFSIOS_PNFS_READ); dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs); @@ -3058,7 +3060,7 @@ pnfs_do_read(struct nfs_pageio_descriptor *desc, struct nfs_pgio_header *hdr) struct pnfs_layout_segment *lseg = desc->pg_lseg; enum pnfs_try_status trypnfs; - trypnfs = pnfs_try_to_read_data(hdr, call_ops, lseg); + trypnfs = pnfs_try_to_read_data(desc, hdr, call_ops, lseg); switch (trypnfs) { case PNFS_NOT_ATTEMPTED: pnfs_read_through_mds(desc, hdr); diff --git a/fs/nfs/pnfs.h b/fs/nfs/pnfs.h index fa5beeaaf5da..92acb837cfa6 100644 --- a/fs/nfs/pnfs.h +++ b/fs/nfs/pnfs.h @@ -157,8 +157,10 @@ struct pnfs_layoutdriver_type { * Return PNFS_ATTEMPTED to indicate the layout code has attempted * I/O, else return PNFS_NOT_ATTEMPTED to fall back to normal NFS */ - enum pnfs_try_status (*read_pagelist)(struct nfs_pgio_header *); - enum pnfs_try_status (*write_pagelist)(struct nfs_pgio_header *, int); + enum pnfs_try_status (*read_pagelist)(struct nfs_pageio_descriptor *, + struct nfs_pgio_header *); + enum pnfs_try_status (*write_pagelist)(struct nfs_pageio_descriptor *, + struct nfs_pgio_header *, int); void (*free_deviceid_node) (struct nfs4_deviceid_node *); struct nfs4_deviceid_node * (*alloc_deviceid_node) From patchwork Fri Jun 14 03:44:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Snitzer X-Patchwork-Id: 13697835 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1499FECC for ; Fri, 14 Jun 2024 03:44:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336672; cv=none; b=LWBieJsP9kTS0j8WNburGWoQDhI3NsMCwPkZv8Ir7uU19n7tNhybh+/43tV3bycN3c+pzhOte0AaeEhfnt5TdJ6EI5ZiUJTX8Y5BbWr+umxi8xBgIJy5f2oUUs5Uw/uGoDsRIwwWfo5xkdynsVpwX9IeUPkUQhn67qR5iSbIVaY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336672; c=relaxed/simple; bh=c62d+ZXcPpDaY/PyiTod1QnTXLKtUvBe/2Fn296kbD0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qKF+7wJgtFpu//dw9o/52NHKjWaA/n87wa/oVNbz2od+nPmO3US9FQaVvLFJQFEDGKE/7Nh1qFMTqWLJDQObHSP9nE3ACs3kV3yr7jIws4Xnx4vEymuzbkHIS7XOK+IP/c9fXJBN3TBFwt385zXJRrzkmGbAnFX41yS5clBbXuc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=UwNnbdit; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="UwNnbdit" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B3F8BC2BBFC; Fri, 14 Jun 2024 03:44:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718336672; bh=c62d+ZXcPpDaY/PyiTod1QnTXLKtUvBe/2Fn296kbD0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UwNnbditb7o0oCe6hRPQ5JEtaZ+TY1nQVLZjz1/kV9ChBOQfzMY9P7ZsDN6s6xVCT hUVxIX7znAYKcUojkpoZ05X0JSHt2EQfzxpgnqEx7obqnO5OioWCcJaDreendeIRep sqE7v6uZR6XoUiG9R/FC3NVECd4NNHbKe2f/ztxDFfzHcfff/o6eSCvZCnfOjhYTTx etqCmRhTEDggyRgjZawbvtr5w1xTKrv1hv6kqWw++9RmwZ5AD2+CRTcQkJpRk5d20O SP3+vMhXIb31wDOzxVFn232h0LxmagJHN+TshP1GtqfVMqBO5wj+NIKVM5oDInFdj8 vCWPtbwezF4YQ== From: Mike Snitzer To: linux-nfs@vger.kernel.org Cc: Jeff Layton , Chuck Lever , Trond Myklebust , NeilBrown , snitzer@hammerspace.com Subject: [PATCH v3 03/18] nfs: pass struct file to nfs_init_pgio and nfs_init_commit Date: Thu, 13 Jun 2024 23:44:11 -0400 Message-ID: <20240614034426.31043-4-snitzer@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240614034426.31043-1-snitzer@kernel.org> References: <20240614034426.31043-1-snitzer@kernel.org> Precedence: bulk X-Mailing-List: linux-nfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Weston Andros Adamson This is needed for localio support. Signed-off-by: Weston Andros Adamson Signed-off-by: Lance Shelton Signed-off-by: Trond Myklebust Signed-off-by: Mike Snitzer --- fs/nfs/filelayout/filelayout.c | 6 +++--- fs/nfs/flexfilelayout/flexfilelayout.c | 6 +++--- fs/nfs/internal.h | 6 ++++-- fs/nfs/pagelist.c | 6 ++++-- fs/nfs/pnfs_nfs.c | 2 +- fs/nfs/write.c | 5 +++-- 6 files changed, 18 insertions(+), 13 deletions(-) diff --git a/fs/nfs/filelayout/filelayout.c b/fs/nfs/filelayout/filelayout.c index f9b600c4a2b5..b9e5e7bd15ca 100644 --- a/fs/nfs/filelayout/filelayout.c +++ b/fs/nfs/filelayout/filelayout.c @@ -489,7 +489,7 @@ filelayout_read_pagelist(struct nfs_pageio_descriptor *desc, /* Perform an asynchronous read to ds */ nfs_initiate_pgio(desc, ds->ds_clp, ds_clnt, hdr, hdr->cred, NFS_PROTO(hdr->inode), &filelayout_read_call_ops, - 0, RPC_TASK_SOFTCONN); + 0, RPC_TASK_SOFTCONN, NULL); return PNFS_ATTEMPTED; } @@ -532,7 +532,7 @@ filelayout_write_pagelist(struct nfs_pageio_descriptor *desc, /* Perform an asynchronous write */ nfs_initiate_pgio(desc, ds->ds_clp, ds_clnt, hdr, hdr->cred, NFS_PROTO(hdr->inode), &filelayout_write_call_ops, - sync, RPC_TASK_SOFTCONN); + sync, RPC_TASK_SOFTCONN, NULL); return PNFS_ATTEMPTED; } @@ -1013,7 +1013,7 @@ static int filelayout_initiate_commit(struct nfs_commit_data *data, int how) data->args.fh = fh; return nfs_initiate_commit(ds_clnt, data, NFS_PROTO(data->inode), &filelayout_commit_call_ops, how, - RPC_TASK_SOFTCONN); + RPC_TASK_SOFTCONN, NULL); out_err: pnfs_generic_prepare_to_resend_writes(data); pnfs_generic_commit_release(data); diff --git a/fs/nfs/flexfilelayout/flexfilelayout.c b/fs/nfs/flexfilelayout/flexfilelayout.c index 22c0e8014afb..3ea07446f05a 100644 --- a/fs/nfs/flexfilelayout/flexfilelayout.c +++ b/fs/nfs/flexfilelayout/flexfilelayout.c @@ -1808,7 +1808,7 @@ ff_layout_read_pagelist(struct nfs_pageio_descriptor *desc, ds->ds_clp->rpc_ops, vers == 3 ? &ff_layout_read_call_ops_v3 : &ff_layout_read_call_ops_v4, - 0, RPC_TASK_SOFTCONN); + 0, RPC_TASK_SOFTCONN, NULL); put_cred(ds_cred); return PNFS_ATTEMPTED; @@ -1878,7 +1878,7 @@ ff_layout_write_pagelist(struct nfs_pageio_descriptor *desc, ds->ds_clp->rpc_ops, vers == 3 ? &ff_layout_write_call_ops_v3 : &ff_layout_write_call_ops_v4, - sync, RPC_TASK_SOFTCONN); + sync, RPC_TASK_SOFTCONN, NULL); put_cred(ds_cred); return PNFS_ATTEMPTED; @@ -1953,7 +1953,7 @@ static int ff_layout_initiate_commit(struct nfs_commit_data *data, int how) ret = nfs_initiate_commit(ds_clnt, data, ds->ds_clp->rpc_ops, vers == 3 ? &ff_layout_commit_call_ops_v3 : &ff_layout_commit_call_ops_v4, - how, RPC_TASK_SOFTCONN); + how, RPC_TASK_SOFTCONN, NULL); put_cred(ds_cred); return ret; out_err: diff --git a/fs/nfs/internal.h b/fs/nfs/internal.h index f6e56fdd8bc2..958c8de072e2 100644 --- a/fs/nfs/internal.h +++ b/fs/nfs/internal.h @@ -309,7 +309,8 @@ int nfs_generic_pgio(struct nfs_pageio_descriptor *, struct nfs_pgio_header *); int nfs_initiate_pgio(struct nfs_pageio_descriptor *, struct nfs_client *clp, struct rpc_clnt *rpc_clnt, struct nfs_pgio_header *hdr, const struct cred *cred, const struct nfs_rpc_ops *rpc_ops, - const struct rpc_call_ops *call_ops, int how, int flags); + const struct rpc_call_ops *call_ops, int how, int flags, + struct file *localio); void nfs_free_request(struct nfs_page *req); struct nfs_pgio_mirror * nfs_pgio_current_mirror(struct nfs_pageio_descriptor *desc); @@ -529,7 +530,8 @@ extern int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data, const struct nfs_rpc_ops *nfs_ops, const struct rpc_call_ops *call_ops, - int how, int flags); + int how, int flags, + struct file *localio); extern void nfs_init_commit(struct nfs_commit_data *data, struct list_head *head, struct pnfs_layout_segment *lseg, diff --git a/fs/nfs/pagelist.c b/fs/nfs/pagelist.c index 3786d767e2ff..57d62db3be5b 100644 --- a/fs/nfs/pagelist.c +++ b/fs/nfs/pagelist.c @@ -848,7 +848,8 @@ int nfs_initiate_pgio(struct nfs_pageio_descriptor *desc, struct nfs_client *clp, struct rpc_clnt *rpc_clnt, struct nfs_pgio_header *hdr, const struct cred *cred, const struct nfs_rpc_ops *rpc_ops, - const struct rpc_call_ops *call_ops, int how, int flags) + const struct rpc_call_ops *call_ops, int how, int flags, + struct file *localio) { struct rpc_task *task; struct rpc_message msg = { @@ -1080,7 +1081,8 @@ static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc) NFS_PROTO(hdr->inode), desc->pg_rpc_callops, desc->pg_ioflags, - RPC_TASK_CRED_NOREF | task_flags); + RPC_TASK_CRED_NOREF | task_flags, + NULL); } return ret; } diff --git a/fs/nfs/pnfs_nfs.c b/fs/nfs/pnfs_nfs.c index 88e061bd711b..ecfde2649cf3 100644 --- a/fs/nfs/pnfs_nfs.c +++ b/fs/nfs/pnfs_nfs.c @@ -537,7 +537,7 @@ pnfs_generic_commit_pagelist(struct inode *inode, struct list_head *mds_pages, nfs_initiate_commit(NFS_CLIENT(inode), data, NFS_PROTO(data->inode), data->mds_ops, how, - RPC_TASK_CRED_NOREF); + RPC_TASK_CRED_NOREF, NULL); } else { nfs_init_commit(data, NULL, data->lseg, cinfo); initiate_commit(data, how); diff --git a/fs/nfs/write.c b/fs/nfs/write.c index 2329cbb0e446..267bed2a4ceb 100644 --- a/fs/nfs/write.c +++ b/fs/nfs/write.c @@ -1670,7 +1670,8 @@ EXPORT_SYMBOL_GPL(nfs_commitdata_release); int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data, const struct nfs_rpc_ops *nfs_ops, const struct rpc_call_ops *call_ops, - int how, int flags) + int how, int flags, + struct file *localio) { struct rpc_task *task; int priority = flush_task_priority(how); @@ -1816,7 +1817,7 @@ nfs_commit_list(struct inode *inode, struct list_head *head, int how, task_flags = RPC_TASK_MOVEABLE; return nfs_initiate_commit(NFS_CLIENT(inode), data, NFS_PROTO(inode), data->mds_ops, how, - RPC_TASK_CRED_NOREF | task_flags); + RPC_TASK_CRED_NOREF | task_flags, NULL); } /* From patchwork Fri Jun 14 03:44:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Snitzer X-Patchwork-Id: 13697836 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A4A70ECC for ; Fri, 14 Jun 2024 03:44:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336673; cv=none; b=ITz50P99LKOz6VUffy6EEOBTZuLlCfPkqHza4FlgC5ui5sASc+EFe+tpHe5KjiH1wqwP63s0hK3MCW/zSypGgHv5RnvMHBadOSDjz2FnUoElwj4CK04YFOI7UEhArH1iuEcFfb8SZUqXSmKEiPPQHc8TAVKUptetW6r2JZz8dz8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336673; c=relaxed/simple; bh=tmkERfoTUiJ8+89vv7FT3X2MSkEEVlz9hWozpswzLCE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NKd7NO//q1T5uftCSgJuIG2dE41h2zSX32nIHp0PmkyjYmc6K3nd3gbAdDcD0SpKugicmYpbSaP20EI/UyA61kg2/wOZN5XS5XfVM0yrwnzku7tVgE4IOwlSPzgS4E4C1S6QE0lF4jqca3diqhmZelX6urZaAyvZ0lmI3uyMXRE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=leD085Xs; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="leD085Xs" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0AB1FC4AF1D; Fri, 14 Jun 2024 03:44:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718336673; bh=tmkERfoTUiJ8+89vv7FT3X2MSkEEVlz9hWozpswzLCE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=leD085Xs335QLOWrZ3kCdaLbdd+Lw+/UsyT311qgBGpp70OrjStw/U7d/gv8S/16P dJCSX8KX03Uv+/STtE09oF2lFCphQTU0+Mfx7wNCg7AqRdzEbWjoeqBlfiKJw2WDh2 UmWNF/Zg9LC47LQfadCL2sfHeLuXUcliNg3B/va2JvT0HpiO3lanTKj30Irr8HQekK 2d0L1/F1NIlXg57E853aODIVUbOkGPZCi0qtlgHUFAn18uadtCwbP/gkdmOrq9FK6J L0FwVdaJ8k4gpA/F4hSPshC3CaQ0te7V0Z02lBDArLHC7KNot3nCoCjqHfVXZyk15S fZroiDvPyI4Dw== From: Mike Snitzer To: linux-nfs@vger.kernel.org Cc: Jeff Layton , Chuck Lever , Trond Myklebust , NeilBrown , snitzer@hammerspace.com Subject: [PATCH v3 04/18] sunrpc: add rpcauth_map_to_svc_cred_local Date: Thu, 13 Jun 2024 23:44:12 -0400 Message-ID: <20240614034426.31043-5-snitzer@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240614034426.31043-1-snitzer@kernel.org> References: <20240614034426.31043-1-snitzer@kernel.org> Precedence: bulk X-Mailing-List: linux-nfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Weston Andros Adamson Add new funtion rpcauth_map_to_svc_cred_local which maps a generic cred to a svc_cred suitable for use in nfsd. This is needed by the localio code to map nfs client creds to nfs server credentials. Signed-off-by: Weston Andros Adamson Signed-off-by: Lance Shelton Signed-off-by: Trond Myklebust Signed-off-by: Mike Snitzer --- include/linux/sunrpc/auth.h | 4 ++++ net/sunrpc/auth.c | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/include/linux/sunrpc/auth.h b/include/linux/sunrpc/auth.h index 61e58327b1aa..872f594a924c 100644 --- a/include/linux/sunrpc/auth.h +++ b/include/linux/sunrpc/auth.h @@ -11,6 +11,7 @@ #define _LINUX_SUNRPC_AUTH_H #include +#include #include #include @@ -184,6 +185,9 @@ int rpcauth_uptodatecred(struct rpc_task *); int rpcauth_init_credcache(struct rpc_auth *); void rpcauth_destroy_credcache(struct rpc_auth *); void rpcauth_clear_credcache(struct rpc_cred_cache *); +void rpcauth_map_to_svc_cred_local(struct rpc_auth *, + const struct cred *, + struct svc_cred *); char * rpcauth_stringify_acceptor(struct rpc_cred *); static inline diff --git a/net/sunrpc/auth.c b/net/sunrpc/auth.c index 04534ea537c8..00f12ca779c5 100644 --- a/net/sunrpc/auth.c +++ b/net/sunrpc/auth.c @@ -308,6 +308,21 @@ rpcauth_init_credcache(struct rpc_auth *auth) } EXPORT_SYMBOL_GPL(rpcauth_init_credcache); +void +rpcauth_map_to_svc_cred_local(struct rpc_auth *auth, const struct cred *cred, + struct svc_cred *svc) +{ + svc->cr_uid = cred->uid; + svc->cr_gid = cred->gid; + svc->cr_flavor = auth->au_flavor; + if (cred->group_info) + svc->cr_group_info = get_group_info(cred->group_info); + /* These aren't relevant for local (network is bypassed) */ + svc->cr_principal = NULL; + svc->cr_gss_mech = NULL; +} +EXPORT_SYMBOL_GPL(rpcauth_map_to_svc_cred_local); + char * rpcauth_stringify_acceptor(struct rpc_cred *cred) { From patchwork Fri Jun 14 03:44:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Snitzer X-Patchwork-Id: 13697837 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BADE3ECC for ; Fri, 14 Jun 2024 03:44:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336674; cv=none; b=dl/2i8lb91M/MOlwPP6NmfanLWu06nqn2g+DiUVfc3+N8Nznb7HLQv6g45EvmiOXvFoFwj2h8maLbO2lBFm/S50sUs826WJFcWPXN5UZ5l7tmOuTpiyEuc9f22/SrES8RS/SBdWm2HSvW9/mLfn3RYALgwOav2fzIT5ceUJ6U4M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336674; c=relaxed/simple; bh=bLWfk615WSIQlyOeksE7TdY9GJ3Ea2xpc+Ohb9wZ9Ts=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RHGj1lTTof1nMJ3KAz14GXvU1cShjMHdB1CCoSMotG4pCSHHHgs/ChU7SHRRDW3poTK2isw03vlZzEU1V3v4QUMBVwpryXGfivNd+P1lXz6J54ed28vJHZyi5wCPRJKnl+j6HhcmVkU/1nypnnPKHOp24MFSIKydhiN8wKN945c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=iN8mU6sf; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="iN8mU6sf" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 597A7C2BBFC; Fri, 14 Jun 2024 03:44:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718336674; bh=bLWfk615WSIQlyOeksE7TdY9GJ3Ea2xpc+Ohb9wZ9Ts=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iN8mU6sf20otFGRsGfX+g0YJgEQYtRRryzF0Yv9Wsl3paVdl6p4nFbuI9aRFTrXcu USq18duU1j0i/HMJkD5m6U6UApCJBnRCNC7TTfCY2X4dSh8Yje9hYDv/cs8bWlcQpe U3Qc4AmIp6egQ4Bw0Ca6QF9nu1vMrvOVdPpJfSnRcCgqlXw+6JPBuKfScsZMWZz01Z lofM/pNuhDCkO5Wu8XUF19watYRuQqwjWZx6WYS/BgnWYn1+PfYidtq9q5sgeNLARg GyMUkZjSF3KfyIzZdugRGapULBEDOuhUP3CgqeibcpXp0NxmJNqETpxMgeWrI7808w UmsIYv5AARn6g== From: Mike Snitzer To: linux-nfs@vger.kernel.org Cc: Jeff Layton , Chuck Lever , Trond Myklebust , NeilBrown , snitzer@hammerspace.com Subject: [PATCH v3 05/18] nfs_common: add NFS LOCALIO protocol extension enablement Date: Thu, 13 Jun 2024 23:44:13 -0400 Message-ID: <20240614034426.31043-6-snitzer@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240614034426.31043-1-snitzer@kernel.org> References: <20240614034426.31043-1-snitzer@kernel.org> Precedence: bulk X-Mailing-List: linux-nfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 First use is in nfsd, to add access to a global nfsd_uuids list that will be used to identify local nfsd instances. nfsd_uuids is protected by nfsd_mutex or RCU read lock. List is composed of nfsd_uuid_t instances that are managed as nfsd creates them (per network namespace). nfsd_uuid_is_local() will be used to search all local nfsd for the client specified nfsd uuid. Signed-off-by: Mike Snitzer --- fs/Kconfig | 3 +++ fs/nfs/Kconfig | 30 +++++++++++++++++++++++++++ fs/nfs_common/Makefile | 3 +++ fs/nfs_common/nfslocalio.c | 42 ++++++++++++++++++++++++++++++++++++++ fs/nfsd/Kconfig | 30 +++++++++++++++++++++++++++ fs/nfsd/netns.h | 4 ++++ fs/nfsd/nfssvc.c | 12 ++++++++++- include/linux/nfslocalio.h | 29 ++++++++++++++++++++++++++ 8 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 fs/nfs_common/nfslocalio.c create mode 100644 include/linux/nfslocalio.h diff --git a/fs/Kconfig b/fs/Kconfig index a46b0cbc4d8f..170083ff2a51 100644 --- a/fs/Kconfig +++ b/fs/Kconfig @@ -377,6 +377,9 @@ config NFS_ACL_SUPPORT tristate select FS_POSIX_ACL +config NFS_COMMON_LOCALIO_SUPPORT + tristate + config NFS_COMMON bool depends on NFSD || NFS_FS || LOCKD diff --git a/fs/nfs/Kconfig b/fs/nfs/Kconfig index 57249f040dfc..70ff4f7a1a22 100644 --- a/fs/nfs/Kconfig +++ b/fs/nfs/Kconfig @@ -1,10 +1,16 @@ # SPDX-License-Identifier: GPL-2.0-only + +config NFS_LOCALIO + tristate + config NFS_FS tristate "NFS client support" depends on INET && FILE_LOCKING && MULTIUSER select LOCKD select SUNRPC select NFS_ACL_SUPPORT if NFS_V3_ACL + select NFS_LOCALIO if NFS_V3_LOCALIO || NFS_V4_LOCALIO + select NFS_COMMON_LOCALIO_SUPPORT if NFS_LOCALIO help Choose Y here if you want to access files residing on other computers using Sun's Network File System protocol. To compile @@ -72,6 +78,18 @@ config NFS_V3_ACL If unsure, say N. +config NFS_V3_LOCALIO + bool "NFS client support for the NFSv3 LOCALIO protocol extension" + depends on NFS_V3 + help + Some NFS servers support an auxiliary NFSv3 LOCALIO protocol + that is not an official part of the NFS version 3 protocol. + + This option enables support for version 3 of the LOCALIO + protocol in the kernel's NFS client. + + If unsure, say N. + config NFS_V4 tristate "NFS client support for NFS version 4" depends on NFS_FS @@ -86,6 +104,18 @@ config NFS_V4 If unsure, say Y. +config NFS_V4_LOCALIO + bool "NFS client support for the NFSv4 LOCALIO protocol extension" + depends on NFS_V4 + help + Some NFS servers support an auxiliary NFSv4 LOCALIO protocol + that is not an official part of the NFS version 4 protocol. + + This option enables support for version 4 of the LOCALIO + protocol in the kernel's NFS client. + + If unsure, say N. + config NFS_SWAP bool "Provide swap over NFS support" default n diff --git a/fs/nfs_common/Makefile b/fs/nfs_common/Makefile index 119c75ab9fd0..d81623b76aba 100644 --- a/fs/nfs_common/Makefile +++ b/fs/nfs_common/Makefile @@ -6,5 +6,8 @@ obj-$(CONFIG_NFS_ACL_SUPPORT) += nfs_acl.o nfs_acl-objs := nfsacl.o +obj-$(CONFIG_NFS_COMMON_LOCALIO_SUPPORT) += nfs_localio.o +nfs_localio-objs := nfslocalio.o + obj-$(CONFIG_GRACE_PERIOD) += grace.o obj-$(CONFIG_NFS_V4_2_SSC_HELPER) += nfs_ssc.o diff --git a/fs/nfs_common/nfslocalio.c b/fs/nfs_common/nfslocalio.c new file mode 100644 index 000000000000..f214cc6754a1 --- /dev/null +++ b/fs/nfs_common/nfslocalio.c @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2024 Mike Snitzer + */ + +#include +#include +#include + +MODULE_LICENSE("GPL"); + +/* + * Global list of nfsd_uuid_t instances, add/remove + * is protected by fs/nfsd/nfssvc.c:nfsd_mutex. + * Reads are protected RCU read lock (see below). + */ +LIST_HEAD(nfsd_uuids); +EXPORT_SYMBOL(nfsd_uuids); + +/* Must be called with RCU read lock held. */ +static const uuid_t * nfsd_uuid_lookup(const uuid_t *uuid) +{ + nfsd_uuid_t *nfsd_uuid; + + list_for_each_entry_rcu(nfsd_uuid, &nfsd_uuids, list) + if (uuid_equal(&nfsd_uuid->uuid, uuid)) + return &nfsd_uuid->uuid; + + return &uuid_null; +} + +bool nfsd_uuid_is_local(const uuid_t *uuid) +{ + const uuid_t *nfsd_uuid; + + rcu_read_lock(); + nfsd_uuid = nfsd_uuid_lookup(uuid); + rcu_read_unlock(); + + return !uuid_is_null(nfsd_uuid); +} +EXPORT_SYMBOL_GPL(nfsd_uuid_is_local); diff --git a/fs/nfsd/Kconfig b/fs/nfsd/Kconfig index 272ab8d5c4d7..93ea29c28aad 100644 --- a/fs/nfsd/Kconfig +++ b/fs/nfsd/Kconfig @@ -1,4 +1,8 @@ # SPDX-License-Identifier: GPL-2.0-only + +config NFSD_LOCALIO + tristate + config NFSD tristate "NFS server support" depends on INET @@ -9,6 +13,8 @@ config NFSD select EXPORTFS select NFS_ACL_SUPPORT if NFSD_V2_ACL select NFS_ACL_SUPPORT if NFSD_V3_ACL + select NFSD_LOCALIO if NFSD_V3_LOCALIO || NFSD_V4_LOCALIO + select NFS_COMMON_LOCALIO_SUPPORT if NFSD_LOCALIO depends on MULTIUSER help Choose Y here if you want to allow other computers to access @@ -69,6 +75,18 @@ config NFSD_V3_ACL If unsure, say N. +config NFSD_V3_LOCALIO + bool "NFS server support for the NFSv3 LOCALIO protocol extension" + depends on NFSD + help + Some NFS servers support an auxiliary NFSv3 LOCALIO protocol + that is not an official part of the NFS version 3 protocol. + + This option enables support for version 3 of the LOCALIO + protocol in the kernel's NFS server. + + If unsure, say N. + config NFSD_V4 bool "NFS server support for NFS version 4" depends on NFSD && PROC_FS @@ -89,6 +107,18 @@ config NFSD_V4 If unsure, say N. +config NFSD_V4_LOCALIO + bool "NFS server support for the NFSv4 LOCALIO protocol extension" + depends on NFSD_V4 + help + Some NFS servers support an auxiliary NFSv4 LOCALIO protocol + that is not an official part of the NFS version 4 protocol. + + This option enables support for version 4 of the LOCALIO + protocol in the kernel's NFS server. + + If unsure, say N. + config NFSD_PNFS bool diff --git a/fs/nfsd/netns.h b/fs/nfsd/netns.h index 14ec15656320..0c5a1d97e4ac 100644 --- a/fs/nfsd/netns.h +++ b/fs/nfsd/netns.h @@ -15,6 +15,7 @@ #include #include #include +#include /* Hash tables for nfs4_clientid state */ #define CLIENT_HASH_BITS 4 @@ -213,6 +214,9 @@ struct nfsd_net { /* last time an admin-revoke happened for NFSv4.0 */ time64_t nfs40_last_revoke; +#if IS_ENABLED(CONFIG_NFSD_LOCALIO) + nfsd_uuid_t nfsd_uuid; +#endif }; /* Simple check to find out if a given net was properly initialized */ diff --git a/fs/nfsd/nfssvc.c b/fs/nfsd/nfssvc.c index cd9a6a1a9fc8..7383dc10611c 100644 --- a/fs/nfsd/nfssvc.c +++ b/fs/nfsd/nfssvc.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -427,6 +428,10 @@ static int nfsd_startup_net(struct net *net, const struct cred *cred) #ifdef CONFIG_NFSD_V4_2_INTER_SSC nfsd4_ssc_init_umount_work(nn); +#endif +#if IS_ENABLED(CONFIG_NFSD_LOCALIO) + INIT_LIST_HEAD(&nn->nfsd_uuid.list); + list_add_tail_rcu(&nn->nfsd_uuid.list, &nfsd_uuids); #endif nn->nfsd_net_up = true; return 0; @@ -456,6 +461,9 @@ static void nfsd_shutdown_net(struct net *net) lockd_down(net); nn->lockd_up = false; } +#if IS_ENABLED(CONFIG_NFSD_LOCALIO) + list_del_rcu(&nn->nfsd_uuid.list); +#endif nn->nfsd_net_up = false; nfsd_shutdown_generic(); } @@ -787,7 +795,9 @@ nfsd_svc(int nrservs, struct net *net, const struct cred *cred, const char *scop strscpy(nn->nfsd_name, scope ? scope : utsname()->nodename, sizeof(nn->nfsd_name)); - +#if IS_ENABLED(CONFIG_NFSD_LOCALIO) + uuid_gen(&nn->nfsd_uuid.uuid); +#endif error = nfsd_create_serv(net); if (error) goto out; diff --git a/include/linux/nfslocalio.h b/include/linux/nfslocalio.h new file mode 100644 index 000000000000..d0bbacd0adcf --- /dev/null +++ b/include/linux/nfslocalio.h @@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (C) 2024 Mike Snitzer + */ +#ifndef __LINUX_NFSLOCALIO_H +#define __LINUX_NFSLOCALIO_H + +#include +#include + +/* + * Global list of nfsd_uuid_t instances, add/remove + * is protected by fs/nfsd/nfssvc.c:nfsd_mutex. + */ +extern struct list_head nfsd_uuids; + +/* + * Each nfsd instance has an nfsd_uuid_t that is accessible through the + * global nfsd_uuids list. Useful to allow a client to negotiate if localio + * possible with its server. + */ +typedef struct { + uuid_t uuid; + struct list_head list; +} nfsd_uuid_t; + +bool nfsd_uuid_is_local(const uuid_t *uuid); + +#endif /* __LINUX_NFSLOCALIO_H */ From patchwork Fri Jun 14 03:44:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Snitzer X-Patchwork-Id: 13697838 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A529CECC for ; Fri, 14 Jun 2024 03:44:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336676; cv=none; b=U4WYVDSIMLUoXyjuoiZio9wD0o7CjBByX2aIizj6DPZEkORHVewETndsc6lN99Gs+slTpdlHPvOHEh1iNjxDFI1T2vLVL6bnA8sEARa3mOeyrw+wzkIeoVQQJfHjkQjMP1PzQDH5pL0n1+/IdxCXiiOiu5tBPDukfmKxledKrQA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336676; c=relaxed/simple; bh=TyDRd64e0i/65tYTP9Co9crvIxaKL0oDKVQCntgybGg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GJtUw5P5RhL/36Yef/VQEQE8eKPjj8L0Ux6t5yD3KVTWiG7nQ/2r/gD7vKOGAgdtQ/ZqrKFXM7/uKglJBxJHg9jdCXc2pIiYkBiJVLHQSBVxQiaWtBsp7o9d7CX9G7DWy24XAfRsdWMfkZ8uZ65DyPboo5fh23NN48Bc64s20ew= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=mNC8J6im; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="mNC8J6im" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C1B40C2BBFC; Fri, 14 Jun 2024 03:44:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718336676; bh=TyDRd64e0i/65tYTP9Co9crvIxaKL0oDKVQCntgybGg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mNC8J6imI9k17LP7eYM+Xdu0bgwfY78bNyNyrnC92Pr+/nB7j+dt9zQL6BaRVOlJz M1FfMaPw6aK2r9pMNBQAzdhNhO+BhFRy77PIGSZhKhWn/6cNg1R1kkatEow+XrX8fr GCtk84H/8MxGhX9zIIFTrPPIATcsc2eFSqaYZoo0HTgkLkpNM6bZTvJjZnAj3Z0Y1W NOc1KOFsBpOrCaFC/RaCvM1eEFpBTbVspngW+RniRP4mS1yQidciLtk7N8fUYwBZ9r HfcQP7KzDE+4zm4Ip1xydxiGvFkNxTtknkGvv+CPzvkmSoZZo2c8hWvLsKcKODSMOa R3afhboc0vNCw== From: Mike Snitzer To: linux-nfs@vger.kernel.org Cc: Jeff Layton , Chuck Lever , Trond Myklebust , NeilBrown , snitzer@hammerspace.com Subject: [PATCH v3 06/18] nfs/nfsd: add "localio" support Date: Thu, 13 Jun 2024 23:44:14 -0400 Message-ID: <20240614034426.31043-7-snitzer@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240614034426.31043-1-snitzer@kernel.org> References: <20240614034426.31043-1-snitzer@kernel.org> Precedence: bulk X-Mailing-List: linux-nfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Weston Andros Adamson Add client support for bypassing NFS for localhost reads, writes, and commits. This is only useful when the client and the server are running on the same host. nfs_local_probe() is stubbed out, later commits will enable client and server handshake via a LOCALIO protocol extension. This has dynamic binding with the nfsd module. Localio will only work if nfsd is already loaded. The "localio_enabled" nfs kernel module parameter can be used to disable and enable the ability to use localio support. Also, tracepoints were added for nfs_local_open_fh, nfs_local_enable and nfs_local_disable. Signed-off-by: Weston Andros Adamson Signed-off-by: Peng Tao Signed-off-by: Lance Shelton Signed-off-by: Trond Myklebust Signed-off-by: Mike Snitzer --- fs/nfs/Makefile | 1 + fs/nfs/client.c | 7 + fs/nfs/inode.c | 5 + fs/nfs/internal.h | 53 +++ fs/nfs/localio.c | 797 ++++++++++++++++++++++++++++++++++++++ fs/nfs/nfstrace.h | 61 +++ fs/nfs/pagelist.c | 3 + fs/nfs/write.c | 3 + fs/nfsd/Makefile | 1 + fs/nfsd/filecache.c | 2 +- fs/nfsd/localio.c | 243 ++++++++++++ fs/nfsd/trace.h | 3 +- fs/nfsd/vfs.h | 8 + include/linux/nfs.h | 6 + include/linux/nfs_fs.h | 2 + include/linux/nfs_fs_sb.h | 5 + include/linux/nfs_xdr.h | 1 + 17 files changed, 1199 insertions(+), 2 deletions(-) create mode 100644 fs/nfs/localio.c create mode 100644 fs/nfsd/localio.c diff --git a/fs/nfs/Makefile b/fs/nfs/Makefile index 5f6db37f461e..9fb2f2cac87e 100644 --- a/fs/nfs/Makefile +++ b/fs/nfs/Makefile @@ -13,6 +13,7 @@ nfs-y := client.o dir.o file.o getroot.o inode.o super.o \ nfs-$(CONFIG_ROOT_NFS) += nfsroot.o nfs-$(CONFIG_SYSCTL) += sysctl.o nfs-$(CONFIG_NFS_FSCACHE) += fscache.o +nfs-$(CONFIG_NFS_LOCALIO) += localio.o obj-$(CONFIG_NFS_V2) += nfsv2.o nfsv2-y := nfs2super.o proc.o nfs2xdr.o diff --git a/fs/nfs/client.c b/fs/nfs/client.c index de77848ae654..9170e6036fd2 100644 --- a/fs/nfs/client.c +++ b/fs/nfs/client.c @@ -178,6 +178,10 @@ struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init) clp->cl_max_connect = cl_init->max_connect ? cl_init->max_connect : 1; clp->cl_net = get_net(cl_init->net); +#if IS_ENABLED(CONFIG_NFS_LOCALIO) + seqlock_init(&clp->cl_boot_lock); + ktime_get_real_ts64(&clp->cl_nfssvc_boot); +#endif clp->cl_principal = "*"; clp->cl_xprtsec = cl_init->xprtsec; return clp; @@ -233,6 +237,8 @@ static void pnfs_init_server(struct nfs_server *server) */ void nfs_free_client(struct nfs_client *clp) { + nfs_local_disable(clp); + /* -EIO all pending I/O */ if (!IS_ERR(clp->cl_rpcclient)) rpc_shutdown_client(clp->cl_rpcclient); @@ -424,6 +430,7 @@ struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init) list_add_tail(&new->cl_share_link, &nn->nfs_client_list); spin_unlock(&nn->nfs_client_lock); + nfs_local_probe(new); return rpc_ops->init_client(new, cl_init); } diff --git a/fs/nfs/inode.c b/fs/nfs/inode.c index acef52ecb1bb..4f88b860494f 100644 --- a/fs/nfs/inode.c +++ b/fs/nfs/inode.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include @@ -1053,6 +1054,7 @@ struct nfs_open_context *alloc_nfs_open_context(struct dentry *dentry, ctx->lock_context.open_context = ctx; INIT_LIST_HEAD(&ctx->list); ctx->mdsthreshold = NULL; + ctx->local_filp = NULL; return ctx; } EXPORT_SYMBOL_GPL(alloc_nfs_open_context); @@ -1084,6 +1086,8 @@ static void __put_nfs_open_context(struct nfs_open_context *ctx, int is_sync) nfs_sb_deactive(sb); put_rpccred(rcu_dereference_protected(ctx->ll_cred, 1)); kfree(ctx->mdsthreshold); + if (!IS_ERR_OR_NULL(ctx->local_filp)) + fput(ctx->local_filp); kfree_rcu(ctx, rcu_head); } @@ -2495,6 +2499,7 @@ static int __init init_nfs_fs(void) if (err) goto out1; + nfs_local_init(); err = register_nfs_fs(); if (err) goto out0; diff --git a/fs/nfs/internal.h b/fs/nfs/internal.h index 958c8de072e2..c933421eb6af 100644 --- a/fs/nfs/internal.h +++ b/fs/nfs/internal.h @@ -451,6 +451,59 @@ extern void nfs_set_cache_invalid(struct inode *inode, unsigned long flags); extern bool nfs_check_cache_invalid(struct inode *, unsigned long); extern int nfs_wait_bit_killable(struct wait_bit_key *key, int mode); +#if IS_ENABLED(CONFIG_NFS_LOCALIO) +/* localio.c */ +extern void nfs_local_init(void); +extern void nfs_local_disable(struct nfs_client *); +extern void nfs_local_probe(struct nfs_client *); +extern struct file *nfs_local_open_fh(struct nfs_client *, const struct cred *, + struct nfs_fh *, const fmode_t); +extern struct file *nfs_local_file_open(struct nfs_client *clp, + const struct cred *cred, + struct nfs_fh *fh, + struct nfs_open_context *ctx); +extern int nfs_local_doio(struct nfs_client *, struct file *, + struct nfs_pgio_header *, + const struct rpc_call_ops *); +extern int nfs_local_commit(struct file *, struct nfs_commit_data *, + const struct rpc_call_ops *, int); +extern bool nfs_server_is_local(const struct nfs_client *clp); + +#else +static inline void nfs_local_init(void) {} +static inline void nfs_local_disable(struct nfs_client *clp) {} +static inline void nfs_local_probe(struct nfs_client *clp) {} +static inline struct file *nfs_local_open_fh(struct nfs_client *clp, + const struct cred *cred, + struct nfs_fh *fh, + const fmode_t mode) +{ + return ERR_PTR(-EINVAL); +} +static inline struct file *nfs_local_file_open(struct nfs_client *clp, + const struct cred *cred, + struct nfs_fh *fh, + struct nfs_open_context *ctx) +{ + return NULL; +} +static inline int nfs_local_doio(struct nfs_client *clp, struct file *filep, + struct nfs_pgio_header *hdr, + const struct rpc_call_ops *call_ops) +{ + return -EINVAL; +} +static inline int nfs_local_commit(struct file *filep, struct nfs_commit_data *data, + const struct rpc_call_ops *call_ops, int how) +{ + return -EINVAL; +} +static inline bool nfs_server_is_local(const struct nfs_client *clp) +{ + return false; +} +#endif /* CONFIG_NFS_LOCALIO */ + /* super.c */ extern const struct super_operations nfs_sops; bool nfs_auth_info_match(const struct nfs_auth_info *, rpc_authflavor_t); diff --git a/fs/nfs/localio.c b/fs/nfs/localio.c new file mode 100644 index 000000000000..286cd0ded1b6 --- /dev/null +++ b/fs/nfs/localio.c @@ -0,0 +1,797 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * NFS client support for local clients to bypass network stack + * + * Copyright (C) 2014 Weston Andros Adamson + * Copyright (C) 2019 Trond Myklebust + * Copyright (C) 2024 Mike Snitzer + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "internal.h" +#include "pnfs.h" +#include "nfstrace.h" + +#define NFSDBG_FACILITY NFSDBG_VFS + +extern int nfsd_open_local_fh(struct rpc_clnt *rpc_clnt, + const struct cred *cred, + const struct nfs_fh *nfs_fh, const fmode_t fmode, + struct file **pfilp); +/* + * The localio code needs to call into nfsd to do the filehandle -> struct path + * mapping, but cannot be statically linked, because that will make the nfs + * module depend on the nfsd module. + * + * Instead, do dynamic linking to the nfsd module. This way the nfs module + * will only hold a reference on nfsd when it's actually in use. This also + * allows some sanity checking, like giving up on localio if nfsd isn't loaded. + */ + +struct nfs_local_open_ctx { + spinlock_t lock; + nfs_to_nfsd_open_t open_f; + atomic_t refcount; +}; + +struct nfs_local_kiocb { + struct kiocb kiocb; + struct bio_vec *bvec; + struct nfs_pgio_header *hdr; + struct work_struct work; +}; + +struct nfs_local_fsync_ctx { + struct file *filp; + struct nfs_commit_data *data; + struct work_struct work; + struct kref kref; + struct completion *done; +}; +static void nfs_local_fsync_work(struct work_struct *work); + +/* + * We need to translate between nfs status return values and + * the local errno values which may not be the same. + */ +static struct { + __u32 stat; + int errno; +} nfs_errtbl[] = { + { NFS4_OK, 0 }, + { NFS4ERR_PERM, -EPERM }, + { NFS4ERR_NOENT, -ENOENT }, + { NFS4ERR_IO, -EIO }, + { NFS4ERR_NXIO, -ENXIO }, + { NFS4ERR_FBIG, -E2BIG }, + { NFS4ERR_STALE, -EBADF }, + { NFS4ERR_ACCESS, -EACCES }, + { NFS4ERR_EXIST, -EEXIST }, + { NFS4ERR_XDEV, -EXDEV }, + { NFS4ERR_MLINK, -EMLINK }, + { NFS4ERR_NOTDIR, -ENOTDIR }, + { NFS4ERR_ISDIR, -EISDIR }, + { NFS4ERR_INVAL, -EINVAL }, + { NFS4ERR_FBIG, -EFBIG }, + { NFS4ERR_NOSPC, -ENOSPC }, + { NFS4ERR_ROFS, -EROFS }, + { NFS4ERR_NAMETOOLONG, -ENAMETOOLONG }, + { NFS4ERR_NOTEMPTY, -ENOTEMPTY }, + { NFS4ERR_DQUOT, -EDQUOT }, + { NFS4ERR_STALE, -ESTALE }, + { NFS4ERR_STALE, -EOPENSTALE }, + { NFS4ERR_DELAY, -ETIMEDOUT }, + { NFS4ERR_DELAY, -ERESTARTSYS }, + { NFS4ERR_DELAY, -EAGAIN }, + { NFS4ERR_DELAY, -ENOMEM }, + { NFS4ERR_IO, -ETXTBSY }, + { NFS4ERR_IO, -EBUSY }, + { NFS4ERR_BADHANDLE, -EBADHANDLE }, + { NFS4ERR_BAD_COOKIE, -EBADCOOKIE }, + { NFS4ERR_NOTSUPP, -EOPNOTSUPP }, + { NFS4ERR_TOOSMALL, -ETOOSMALL }, + { NFS4ERR_SERVERFAULT, -ESERVERFAULT }, + { NFS4ERR_SERVERFAULT, -ENFILE }, + { NFS4ERR_IO, -EREMOTEIO }, + { NFS4ERR_IO, -EUCLEAN }, + { NFS4ERR_PERM, -ENOKEY }, + { NFS4ERR_BADTYPE, -EBADTYPE }, + { NFS4ERR_SYMLINK, -ELOOP }, + { NFS4ERR_DEADLOCK, -EDEADLK }, +}; + +/* + * Convert an NFS error code to a local one. + * This one is used jointly by NFSv2 and NFSv3. + */ +static __u32 +nfs4errno(int errno) +{ + unsigned int i; + for (i = 0; i < ARRAY_SIZE(nfs_errtbl); i++) { + if (nfs_errtbl[i].errno == errno) + return nfs_errtbl[i].stat; + } + /* If we cannot translate the error, the recovery routines should + * handle it. + * Note: remaining NFSv4 error codes have values > 10000, so should + * not conflict with native Linux error codes. + */ + return NFS4ERR_SERVERFAULT; +} + +static struct nfs_local_open_ctx __local_open_ctx __read_mostly; + +static bool localio_enabled __read_mostly = true; +module_param(localio_enabled, bool, 0644); + +bool nfs_server_is_local(const struct nfs_client *clp) +{ + return test_bit(NFS_CS_LOCAL_IO, &clp->cl_flags) != 0 && + localio_enabled; +} +EXPORT_SYMBOL_GPL(nfs_server_is_local); + +void +nfs_local_init(void) +{ + struct nfs_local_open_ctx *ctx = &__local_open_ctx; + + ctx->open_f = NULL; + spin_lock_init(&ctx->lock); + atomic_set(&ctx->refcount, 0); +} + +static bool +nfs_local_get_lookup_ctx(void) +{ + struct nfs_local_open_ctx *ctx = &__local_open_ctx; + nfs_to_nfsd_open_t fn = NULL; + + spin_lock(&ctx->lock); + if (ctx->open_f == NULL) { + spin_unlock(&ctx->lock); + + fn = symbol_request(nfsd_open_local_fh); + if (!fn) + return false; + + spin_lock(&ctx->lock); + /* catch race */ + if (ctx->open_f == NULL) { + ctx->open_f = fn; + fn = NULL; + } + } + atomic_inc(&ctx->refcount); + spin_unlock(&ctx->lock); + if (fn) + symbol_put(nfsd_open_local_fh); + return true; +} + +static void +nfs_local_put_lookup_ctx(void) +{ + struct nfs_local_open_ctx *ctx = &__local_open_ctx; + nfs_to_nfsd_open_t fn; + + if (atomic_dec_and_lock(&ctx->refcount, &ctx->lock)) { + fn = ctx->open_f; + ctx->open_f = NULL; + spin_unlock(&ctx->lock); + if (fn) + symbol_put(nfsd_open_local_fh); + } +} + +/* + * nfs_local_enable - attempt to enable local i/o for an nfs_client + */ +static void nfs_local_enable(struct nfs_client *clp) +{ + if (nfs_local_get_lookup_ctx()) { + set_bit(NFS_CS_LOCAL_IO, &clp->cl_flags); + trace_nfs_local_enable(clp); + } +} + +/* + * nfs_local_disable - disable local i/o for an nfs_client + */ +void +nfs_local_disable(struct nfs_client *clp) +{ + if (test_and_clear_bit(NFS_CS_LOCAL_IO, &clp->cl_flags)) { + trace_nfs_local_disable(clp); + nfs_local_put_lookup_ctx(); + } +} + +/* + * nfs_local_probe - probe local i/o support for an nfs_client + */ +void +nfs_local_probe(struct nfs_client *clp) +{ + bool enable = false; + + if (enable) + nfs_local_enable(clp); +} +EXPORT_SYMBOL_GPL(nfs_local_probe); + +/* + * nfs_local_open_fh - open a local filehandle + * + * Returns a pointer to a struct file or an ERR_PTR + */ +struct file * +nfs_local_open_fh(struct nfs_client *clp, const struct cred *cred, + struct nfs_fh *fh, const fmode_t mode) +{ + struct nfs_local_open_ctx *ctx = &__local_open_ctx; + struct file *filp; + int status; + + if (mode & ~(FMODE_READ | FMODE_WRITE)) + return ERR_PTR(-EINVAL); + + status = ctx->open_f(clp->cl_rpcclient, cred, fh, mode, &filp); + if (status < 0) { + dprintk("%s: open local file failed error=%d\n", + __func__, status); + trace_nfs_local_open_fh(fh, mode, status); + switch (status) { + case -ENXIO: + nfs_local_disable(clp); + fallthrough; + case -ETIMEDOUT: + status = -EAGAIN; + } + filp = ERR_PTR(status); + } + return filp; +} +EXPORT_SYMBOL_GPL(nfs_local_open_fh); + +static struct bio_vec * +nfs_bvec_alloc_and_import_pagevec(struct page **pagevec, + unsigned int npages, gfp_t flags) +{ + struct bio_vec *bvec, *p; + + bvec = kmalloc_array(npages, sizeof(*bvec), flags); + if (bvec != NULL) { + for (p = bvec; npages > 0; p++, pagevec++, npages--) { + p->bv_page = *pagevec; + p->bv_len = PAGE_SIZE; + p->bv_offset = 0; + } + } + return bvec; +} + +static void +nfs_local_iocb_free(struct nfs_local_kiocb *iocb) +{ + kfree(iocb->bvec); + kfree(iocb); +} + +static struct nfs_local_kiocb * +nfs_local_iocb_alloc(struct nfs_pgio_header *hdr, struct file *filp, + gfp_t flags) +{ + struct nfs_local_kiocb *iocb; + + iocb = kmalloc(sizeof(*iocb), flags); + if (iocb == NULL) + return NULL; + iocb->bvec = nfs_bvec_alloc_and_import_pagevec(hdr->page_array.pagevec, + hdr->page_array.npages, flags); + if (iocb->bvec == NULL) { + kfree(iocb); + return NULL; + } + init_sync_kiocb(&iocb->kiocb, filp); + iocb->kiocb.ki_pos = hdr->args.offset; + iocb->hdr = hdr; + /* FIXME: NFS_IOHDR_ODIRECT isn't ever set */ + if (test_bit(NFS_IOHDR_ODIRECT, &hdr->flags)) + iocb->kiocb.ki_flags |= IOCB_DIRECT|IOCB_DSYNC; + iocb->kiocb.ki_flags &= ~IOCB_APPEND; + return iocb; +} + +static void +nfs_local_iter_init(struct iov_iter *i, struct nfs_local_kiocb *iocb, int dir) +{ + struct nfs_pgio_header *hdr = iocb->hdr; + + if (hdr->args.pgbase != 0) { + iov_iter_bvec(i, dir, iocb->bvec, + hdr->page_array.npages, + hdr->args.count + hdr->args.pgbase); + iov_iter_advance(i, hdr->args.pgbase); + } else + iov_iter_bvec(i, dir, iocb->bvec, + hdr->page_array.npages, hdr->args.count); +} + +static void +nfs_local_hdr_release(struct nfs_pgio_header *hdr, + const struct rpc_call_ops *call_ops) +{ + call_ops->rpc_call_done(&hdr->task, hdr); + call_ops->rpc_release(hdr); +} + +static void +nfs_local_pgio_init(struct nfs_pgio_header *hdr, + const struct rpc_call_ops *call_ops) +{ + hdr->task.tk_ops = call_ops; + if (!hdr->task.tk_start) + hdr->task.tk_start = ktime_get(); +} + +static void +nfs_local_pgio_done(struct nfs_pgio_header *hdr, long status) +{ + if (status >= 0) { + hdr->res.count = status; + hdr->res.op_status = NFS4_OK; + hdr->task.tk_status = 0; + } else { + hdr->res.op_status = nfs4errno(status); + hdr->task.tk_status = status; + } +} + +static void +nfs_local_pgio_release(struct nfs_local_kiocb *iocb) +{ + struct nfs_pgio_header *hdr = iocb->hdr; + + fput(iocb->kiocb.ki_filp); + nfs_local_iocb_free(iocb); + nfs_local_hdr_release(hdr, hdr->task.tk_ops); +} + +static void +nfs_local_read_aio_complete_work(struct work_struct *work) +{ + struct nfs_local_kiocb *iocb = container_of(work, + struct nfs_local_kiocb, work); + + nfs_local_pgio_release(iocb); +} + +/* + * Complete the I/O from iocb->kiocb.ki_complete() + * + * Note that this function can be called from a bottom half context, + * hence we need to queue the fput() etc to a workqueue + */ +static void +nfs_local_pgio_complete(struct nfs_local_kiocb *iocb) +{ + queue_work(nfsiod_workqueue, &iocb->work); +} + +static void +nfs_local_read_done(struct nfs_local_kiocb *iocb, long status) +{ + struct nfs_pgio_header *hdr = iocb->hdr; + struct file *filp = iocb->kiocb.ki_filp; + + nfs_local_pgio_done(hdr, status); + + if (hdr->res.count != hdr->args.count || + hdr->args.offset + hdr->res.count >= i_size_read(file_inode(filp))) + hdr->res.eof = true; + + dprintk("%s: read %ld bytes eof %d.\n", __func__, + status > 0 ? status : 0, hdr->res.eof); +} + +static void +nfs_local_read_aio_complete(struct kiocb *kiocb, long ret) +{ + struct nfs_local_kiocb *iocb = container_of(kiocb, + struct nfs_local_kiocb, kiocb); + + nfs_local_read_done(iocb, ret); + nfs_local_pgio_complete(iocb); +} + +static int +nfs_do_local_read(struct nfs_pgio_header *hdr, struct file *filp, + const struct rpc_call_ops *call_ops) +{ + struct nfs_local_kiocb *iocb; + struct iov_iter iter; + ssize_t status; + + dprintk("%s: vfs_read count=%u pos=%llu\n", + __func__, hdr->args.count, hdr->args.offset); + + iocb = nfs_local_iocb_alloc(hdr, filp, GFP_KERNEL); + if (iocb == NULL) + return -ENOMEM; + nfs_local_iter_init(&iter, iocb, READ); + + nfs_local_pgio_init(hdr, call_ops); + hdr->res.eof = false; + + if (iocb->kiocb.ki_flags & IOCB_DIRECT) { + INIT_WORK(&iocb->work, nfs_local_read_aio_complete_work); + iocb->kiocb.ki_complete = nfs_local_read_aio_complete; + } + + status = filp->f_op->read_iter(&iocb->kiocb, &iter); + if (status != -EIOCBQUEUED) { + nfs_local_read_done(iocb, status); + nfs_local_pgio_release(iocb); + } + return 0; +} + +static void +nfs_copy_boot_verifier(struct nfs_write_verifier *verifier, struct inode *inode) +{ + struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; + u32 *verf = (u32 *)verifier->data; + int seq = 0; + + do { + read_seqbegin_or_lock(&clp->cl_boot_lock, &seq); + verf[0] = (u32)clp->cl_nfssvc_boot.tv_sec; + verf[1] = (u32)clp->cl_nfssvc_boot.tv_nsec; + } while (need_seqretry(&clp->cl_boot_lock, seq)); + done_seqretry(&clp->cl_boot_lock, seq); +} + +static void +nfs_reset_boot_verifier(struct inode *inode) +{ + struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; + + write_seqlock(&clp->cl_boot_lock); + ktime_get_real_ts64(&clp->cl_nfssvc_boot); + write_sequnlock(&clp->cl_boot_lock); +} + +static void +nfs_set_local_verifier(struct inode *inode, + struct nfs_writeverf *verf, + enum nfs3_stable_how how) +{ + + nfs_copy_boot_verifier(&verf->verifier, inode); + verf->committed = how; +} + +static void +nfs_get_vfs_attr(struct file *filp, struct nfs_fattr *fattr) +{ + struct kstat stat; + + if (fattr != NULL && vfs_getattr(&filp->f_path, &stat, + STATX_INO | + STATX_ATIME | + STATX_MTIME | + STATX_CTIME | + STATX_SIZE | + STATX_BLOCKS, + AT_STATX_SYNC_AS_STAT) == 0) { + fattr->valid = NFS_ATTR_FATTR_FILEID | + NFS_ATTR_FATTR_CHANGE | + NFS_ATTR_FATTR_SIZE | + NFS_ATTR_FATTR_ATIME | + NFS_ATTR_FATTR_MTIME | + NFS_ATTR_FATTR_CTIME | + NFS_ATTR_FATTR_SPACE_USED; + fattr->fileid = stat.ino; + fattr->size = stat.size; + fattr->atime = stat.atime; + fattr->mtime = stat.mtime; + fattr->ctime = stat.ctime; + fattr->change_attr = nfs_timespec_to_change_attr(&fattr->ctime); + fattr->du.nfs3.used = stat.blocks << 9; + } +} + +static void +nfs_local_write_done(struct nfs_local_kiocb *iocb, long status) +{ + struct nfs_pgio_header *hdr = iocb->hdr; + + dprintk("%s: wrote %ld bytes.\n", __func__, status > 0 ? status : 0); + + /* Handle short writes as if they are ENOSPC */ + if (status > 0 && status < hdr->args.count) { + hdr->mds_offset += status; + hdr->args.offset += status; + hdr->args.pgbase += status; + hdr->args.count -= status; + nfs_set_pgio_error(hdr, -ENOSPC, hdr->args.offset); + status = -ENOSPC; + } + if (status < 0) + nfs_reset_boot_verifier(hdr->inode); + nfs_local_pgio_done(hdr, status); +} + +static void +nfs_local_write_aio_complete_work(struct work_struct *work) +{ + struct nfs_local_kiocb *iocb = container_of(work, + struct nfs_local_kiocb, work); + + nfs_get_vfs_attr(iocb->kiocb.ki_filp, iocb->hdr->res.fattr); + nfs_local_pgio_release(iocb); +} + +static void +nfs_local_write_aio_complete(struct kiocb *kiocb, long ret) +{ + struct nfs_local_kiocb *iocb = container_of(kiocb, + struct nfs_local_kiocb, kiocb); + + nfs_local_write_done(iocb, ret); + nfs_local_pgio_complete(iocb); +} + +static int +nfs_do_local_write(struct nfs_pgio_header *hdr, struct file *filp, + const struct rpc_call_ops *call_ops) +{ + struct nfs_local_kiocb *iocb; + struct iov_iter iter; + ssize_t status; + + dprintk("%s: vfs_write count=%u pos=%llu %s\n", + __func__, hdr->args.count, hdr->args.offset, + (hdr->args.stable == NFS_UNSTABLE) ? "unstable" : "stable"); + + iocb = nfs_local_iocb_alloc(hdr, filp, GFP_NOIO); + if (iocb == NULL) + return -ENOMEM; + nfs_local_iter_init(&iter, iocb, WRITE); + + switch (hdr->args.stable) { + default: + break; + case NFS_DATA_SYNC: + iocb->kiocb.ki_flags |= IOCB_DSYNC; + break; + case NFS_FILE_SYNC: + iocb->kiocb.ki_flags |= IOCB_DSYNC|IOCB_SYNC; + } + nfs_local_pgio_init(hdr, call_ops); + + if (iocb->kiocb.ki_flags & IOCB_DIRECT) { + INIT_WORK(&iocb->work, nfs_local_write_aio_complete_work); + iocb->kiocb.ki_complete = nfs_local_write_aio_complete; + } + + nfs_set_local_verifier(hdr->inode, hdr->res.verf, hdr->args.stable); + + file_start_write(filp); + status = filp->f_op->write_iter(&iocb->kiocb, &iter); + file_end_write(filp); + if (status != -EIOCBQUEUED) { + nfs_local_write_done(iocb, status); + nfs_get_vfs_attr(filp, hdr->res.fattr); + nfs_local_pgio_release(iocb); + } + return 0; +} + +static struct file * +nfs_local_file_open_cached(struct nfs_client *clp, const struct cred *cred, + struct nfs_fh *fh, struct nfs_open_context *ctx) +{ + struct file *filp = ctx->local_filp; + + if (!filp) { + struct file *new = nfs_local_open_fh(clp, cred, fh, ctx->mode); + if (IS_ERR_OR_NULL(new)) + return NULL; + /* try to put this one in the slot */ + filp = cmpxchg(&ctx->local_filp, NULL, new); + if (filp != NULL) + fput(new); + else + filp = new; + } + return get_file(filp); +} + +struct file * +nfs_local_file_open(struct nfs_client *clp, const struct cred *cred, + struct nfs_fh *fh, struct nfs_open_context *ctx) +{ + if (!nfs_server_is_local(clp)) + return NULL; + return nfs_local_file_open_cached(clp, cred, fh, ctx); +} + +int +nfs_local_doio(struct nfs_client *clp, struct file *filp, + struct nfs_pgio_header *hdr, + const struct rpc_call_ops *call_ops) +{ + int status = 0; + + if (!hdr->args.count) + goto out_fput; + /* Don't support filesystems without read_iter/write_iter */ + if (!filp->f_op->read_iter || !filp->f_op->write_iter) { + nfs_local_disable(clp); + status = -EAGAIN; + goto out_fput; + } + + switch (hdr->rw_mode) { + case FMODE_READ: + status = nfs_do_local_read(hdr, filp, call_ops); + break; + case FMODE_WRITE: + status = nfs_do_local_write(hdr, filp, call_ops); + break; + default: + dprintk("%s: invalid mode: %d\n", __func__, + hdr->rw_mode); + status = -EINVAL; + } +out_fput: + if (status != 0) { + fput(filp); + hdr->task.tk_status = status; + nfs_local_hdr_release(hdr, call_ops); + } + return status; +} + +static void +nfs_local_init_commit(struct nfs_commit_data *data, + const struct rpc_call_ops *call_ops) +{ + data->task.tk_ops = call_ops; +} + +static int +nfs_local_run_commit(struct file *filp, struct nfs_commit_data *data) +{ + loff_t start = data->args.offset; + loff_t end = LLONG_MAX; + + if (data->args.count > 0) { + end = start + data->args.count - 1; + if (end < start) + end = LLONG_MAX; + } + + dprintk("%s: commit %llu - %llu\n", __func__, start, end); + return vfs_fsync_range(filp, start, end, 0); +} + +static void +nfs_local_commit_done(struct nfs_commit_data *data, int status) +{ + if (status >= 0) { + nfs_set_local_verifier(data->inode, + data->res.verf, + NFS_FILE_SYNC); + data->res.op_status = NFS4_OK; + data->task.tk_status = 0; + } else { + nfs_reset_boot_verifier(data->inode); + data->res.op_status = nfs4errno(status); + data->task.tk_status = status; + } +} + +static void +nfs_local_release_commit_data(struct file *filp, + struct nfs_commit_data *data, + const struct rpc_call_ops *call_ops) +{ + fput(filp); + call_ops->rpc_call_done(&data->task, data); + call_ops->rpc_release(data); +} + +static struct nfs_local_fsync_ctx * +nfs_local_fsync_ctx_alloc(struct nfs_commit_data *data, struct file *filp, + gfp_t flags) +{ + struct nfs_local_fsync_ctx *ctx = kmalloc(sizeof(*ctx), flags); + + if (ctx != NULL) { + ctx->filp = filp; + ctx->data = data; + INIT_WORK(&ctx->work, nfs_local_fsync_work); + kref_init(&ctx->kref); + ctx->done = NULL; + } + return ctx; +} + +static void +nfs_local_fsync_ctx_kref_free(struct kref *kref) +{ + kfree(container_of(kref, struct nfs_local_fsync_ctx, kref)); +} + +static void +nfs_local_fsync_ctx_put(struct nfs_local_fsync_ctx *ctx) +{ + kref_put(&ctx->kref, nfs_local_fsync_ctx_kref_free); +} + +static void +nfs_local_fsync_ctx_free(struct nfs_local_fsync_ctx *ctx) +{ + nfs_local_release_commit_data(ctx->filp, ctx->data, + ctx->data->task.tk_ops); + nfs_local_fsync_ctx_put(ctx); +} + +static void +nfs_local_fsync_work(struct work_struct *work) +{ + struct nfs_local_fsync_ctx *ctx; + int status; + + ctx = container_of(work, struct nfs_local_fsync_ctx, work); + + status = nfs_local_run_commit(ctx->filp, ctx->data); + nfs_local_commit_done(ctx->data, status); + if (ctx->done != NULL) + complete(ctx->done); + nfs_local_fsync_ctx_free(ctx); +} + +int +nfs_local_commit(struct file *filp, struct nfs_commit_data *data, + const struct rpc_call_ops *call_ops, int how) +{ + struct nfs_local_fsync_ctx *ctx; + + ctx = nfs_local_fsync_ctx_alloc(data, filp, GFP_KERNEL); + if (!ctx) { + nfs_local_commit_done(data, -ENOMEM); + nfs_local_release_commit_data(filp, data, call_ops); + return -ENOMEM; + } + + nfs_local_init_commit(data, call_ops); + kref_get(&ctx->kref); + if (how & FLUSH_SYNC) { + DECLARE_COMPLETION_ONSTACK(done); + ctx->done = &done; + queue_work(nfsiod_workqueue, &ctx->work); + wait_for_completion(&done); + } else + queue_work(nfsiod_workqueue, &ctx->work); + nfs_local_fsync_ctx_put(ctx); + return 0; +} diff --git a/fs/nfs/nfstrace.h b/fs/nfs/nfstrace.h index 1e710654af11..95a2c19a9172 100644 --- a/fs/nfs/nfstrace.h +++ b/fs/nfs/nfstrace.h @@ -1681,6 +1681,67 @@ TRACE_EVENT(nfs_mount_path, TP_printk("path='%s'", __get_str(path)) ); +TRACE_EVENT(nfs_local_open_fh, + TP_PROTO( + const struct nfs_fh *fh, + fmode_t fmode, + int error + ), + + TP_ARGS(fh, fmode, error), + + TP_STRUCT__entry( + __field(int, error) + __field(u32, fhandle) + __field(unsigned int, fmode) + ), + + TP_fast_assign( + __entry->error = error; + __entry->fhandle = nfs_fhandle_hash(fh); + __entry->fmode = (__force unsigned int)fmode; + ), + + TP_printk( + "error=%d fhandle=0x%08x mode=%s", + __entry->error, + __entry->fhandle, + show_fs_fmode_flags(__entry->fmode) + ) +); + +DECLARE_EVENT_CLASS(nfs_local_client_event, + TP_PROTO( + const struct nfs_client *clp + ), + + TP_ARGS(clp), + + TP_STRUCT__entry( + __field(unsigned int, protocol) + __string(server, clp->cl_hostname) + ), + + TP_fast_assign( + __entry->protocol = clp->rpc_ops->version; + __assign_str(server); + ), + + TP_printk( + "server=%s NFSv%u", __get_str(server), __entry->protocol + ) +); + +#define DEFINE_NFS_LOCAL_CLIENT_EVENT(name) \ + DEFINE_EVENT(nfs_local_client_event, name, \ + TP_PROTO( \ + const struct nfs_client *clp \ + ), \ + TP_ARGS(clp)) + +DEFINE_NFS_LOCAL_CLIENT_EVENT(nfs_local_enable); +DEFINE_NFS_LOCAL_CLIENT_EVENT(nfs_local_disable); + DECLARE_EVENT_CLASS(nfs_xdr_event, TP_PROTO( const struct xdr_stream *xdr, diff --git a/fs/nfs/pagelist.c b/fs/nfs/pagelist.c index 57d62db3be5b..b08420b8e664 100644 --- a/fs/nfs/pagelist.c +++ b/fs/nfs/pagelist.c @@ -879,6 +879,9 @@ int nfs_initiate_pgio(struct nfs_pageio_descriptor *desc, hdr->args.count, (unsigned long long)hdr->args.offset); + if (localio) + return nfs_local_doio(clp, localio, hdr, call_ops); + task = rpc_run_task(&task_setup_data); if (IS_ERR(task)) return PTR_ERR(task); diff --git a/fs/nfs/write.c b/fs/nfs/write.c index 267bed2a4ceb..b29b0fd5431f 100644 --- a/fs/nfs/write.c +++ b/fs/nfs/write.c @@ -1700,6 +1700,9 @@ int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data, dprintk("NFS: initiated commit call\n"); + if (localio) + return nfs_local_commit(localio, data, call_ops, how); + task = rpc_run_task(&task_setup_data); if (IS_ERR(task)) return PTR_ERR(task); diff --git a/fs/nfsd/Makefile b/fs/nfsd/Makefile index b8736a82e57c..78b421778a79 100644 --- a/fs/nfsd/Makefile +++ b/fs/nfsd/Makefile @@ -23,3 +23,4 @@ nfsd-$(CONFIG_NFSD_PNFS) += nfs4layouts.o nfsd-$(CONFIG_NFSD_BLOCKLAYOUT) += blocklayout.o blocklayoutxdr.o nfsd-$(CONFIG_NFSD_SCSILAYOUT) += blocklayout.o blocklayoutxdr.o nfsd-$(CONFIG_NFSD_FLEXFILELAYOUT) += flexfilelayout.o flexfilelayoutxdr.o +nfsd-$(CONFIG_NFSD_LOCALIO) += localio.o diff --git a/fs/nfsd/filecache.c b/fs/nfsd/filecache.c index ad9083ca144b..99631fa56662 100644 --- a/fs/nfsd/filecache.c +++ b/fs/nfsd/filecache.c @@ -52,7 +52,7 @@ #define NFSD_FILE_CACHE_UP (0) /* We only care about NFSD_MAY_READ/WRITE for this cache */ -#define NFSD_FILE_MAY_MASK (NFSD_MAY_READ|NFSD_MAY_WRITE) +#define NFSD_FILE_MAY_MASK (NFSD_MAY_READ|NFSD_MAY_WRITE|NFSD_MAY_LOCALIO) static DEFINE_PER_CPU(unsigned long, nfsd_file_cache_hits); static DEFINE_PER_CPU(unsigned long, nfsd_file_acquisitions); diff --git a/fs/nfsd/localio.c b/fs/nfsd/localio.c new file mode 100644 index 000000000000..6e2918e76f49 --- /dev/null +++ b/fs/nfsd/localio.c @@ -0,0 +1,243 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * NFS server support for local clients to bypass network stack + * + * Copyright (C) 2014 Weston Andros Adamson + * Copyright (C) 2019 Trond Myklebust + * Copyright (C) 2024 Mike Snitzer + */ + +#include +#include +#include +#include +#include + +#include "nfsd.h" +#include "vfs.h" +#include "netns.h" +#include "filecache.h" + +#define NFSDDBG_FACILITY NFSDDBG_FH + +/* + * We need to translate between nfs status return values and + * the local errno values which may not be the same. + * - duplicated from fs/nfs/nfs2xdr.c to avoid needless bloat of + * all compiled nfs objects if it were in include/linux/nfs.h + */ +static const struct { + int stat; + int errno; +} nfs_common_errtbl[] = { + { NFS_OK, 0 }, + { NFSERR_PERM, -EPERM }, + { NFSERR_NOENT, -ENOENT }, + { NFSERR_IO, -EIO }, + { NFSERR_NXIO, -ENXIO }, +/* { NFSERR_EAGAIN, -EAGAIN }, */ + { NFSERR_ACCES, -EACCES }, + { NFSERR_EXIST, -EEXIST }, + { NFSERR_XDEV, -EXDEV }, + { NFSERR_NODEV, -ENODEV }, + { NFSERR_NOTDIR, -ENOTDIR }, + { NFSERR_ISDIR, -EISDIR }, + { NFSERR_INVAL, -EINVAL }, + { NFSERR_FBIG, -EFBIG }, + { NFSERR_NOSPC, -ENOSPC }, + { NFSERR_ROFS, -EROFS }, + { NFSERR_MLINK, -EMLINK }, + { NFSERR_NAMETOOLONG, -ENAMETOOLONG }, + { NFSERR_NOTEMPTY, -ENOTEMPTY }, + { NFSERR_DQUOT, -EDQUOT }, + { NFSERR_STALE, -ESTALE }, + { NFSERR_REMOTE, -EREMOTE }, +#ifdef EWFLUSH + { NFSERR_WFLUSH, -EWFLUSH }, +#endif + { NFSERR_BADHANDLE, -EBADHANDLE }, + { NFSERR_NOT_SYNC, -ENOTSYNC }, + { NFSERR_BAD_COOKIE, -EBADCOOKIE }, + { NFSERR_NOTSUPP, -ENOTSUPP }, + { NFSERR_TOOSMALL, -ETOOSMALL }, + { NFSERR_SERVERFAULT, -EREMOTEIO }, + { NFSERR_BADTYPE, -EBADTYPE }, + { NFSERR_JUKEBOX, -EJUKEBOX }, + { -1, -EIO } +}; + +/** + * nfs_stat_to_errno - convert an NFS status code to a local errno + * @status: NFS status code to convert + * + * Returns a local errno value, or -EIO if the NFS status code is + * not recognized. This function is used jointly by NFSv2 and NFSv3. + */ +static inline int nfs_stat_to_errno(enum nfs_stat status) +{ + int i; + + for (i = 0; nfs_common_errtbl[i].stat != -1; i++) { + if (nfs_common_errtbl[i].stat == (int)status) + return nfs_common_errtbl[i].errno; + } + return nfs_common_errtbl[i].errno; +} + +static void +nfsd_local_fakerqst_destroy(struct svc_rqst *rqstp) +{ + if (rqstp->rq_client) + auth_domain_put(rqstp->rq_client); + if (rqstp->rq_cred.cr_group_info) + put_group_info(rqstp->rq_cred.cr_group_info); + /* rpcauth_map_to_svc_cred_local() clears cr_principal */ + WARN_ON_ONCE(rqstp->rq_cred.cr_principal != NULL); + kfree(rqstp->rq_xprt); + kfree(rqstp); +} + +static struct svc_rqst * +nfsd_local_fakerqst_create(struct rpc_clnt *rpc_clnt, const struct cred *cred) +{ + struct svc_rqst *rqstp; + struct net *net = rpc_net_ns(rpc_clnt); + struct nfsd_net *nn = net_generic(net, nfsd_net_id); + int status; + + /* FIXME: not running in nfsd context, must get reference on nfsd_serv */ + if (unlikely(!READ_ONCE(nn->nfsd_serv))) { + dprintk("%s: localio denied. Server not running\n", __func__); + return ERR_PTR(-ENXIO); + } + + rqstp = kzalloc(sizeof(*rqstp), GFP_KERNEL); + if (!rqstp) + return ERR_PTR(-ENOMEM); + + rqstp->rq_xprt = kzalloc(sizeof(*rqstp->rq_xprt), GFP_KERNEL); + if (!rqstp->rq_xprt) { + status = -ENOMEM; + goto out_err; + } + + rqstp->rq_xprt->xpt_net = net; + __set_bit(RQ_SECURE, &rqstp->rq_flags); + rqstp->rq_proc = 1; + rqstp->rq_vers = 3; + rqstp->rq_prot = IPPROTO_TCP; + rqstp->rq_server = nn->nfsd_serv; + + /* Note: we're connecting to ourself, so source addr == peer addr */ + rqstp->rq_addrlen = rpc_peeraddr(rpc_clnt, + (struct sockaddr *)&rqstp->rq_addr, + sizeof(rqstp->rq_addr)); + + rpcauth_map_to_svc_cred_local(rpc_clnt->cl_auth, cred, &rqstp->rq_cred); + + /* + * set up enough for svcauth_unix_set_client to be able to wait + * for the cache downcall. Note that we do _not_ want to allow the + * request to be deferred for later revisit since this rqst and xprt + * are not set up to run inside of the normal svc_rqst engine. + */ + INIT_LIST_HEAD(&rqstp->rq_xprt->xpt_deferred); + kref_init(&rqstp->rq_xprt->xpt_ref); + spin_lock_init(&rqstp->rq_xprt->xpt_lock); + rqstp->rq_chandle.thread_wait = 5 * HZ; + + status = svcauth_unix_set_client(rqstp); + switch (status) { + case SVC_OK: + break; + case SVC_DENIED: + status = -ENXIO; + dprintk("%s: client %pISpc denied localio access\n", + __func__, (struct sockaddr *)&rqstp->rq_addr); + goto out_err; + default: + status = -ETIMEDOUT; + dprintk("%s: client %pISpc temporarily denied localio access\n", + __func__, (struct sockaddr *)&rqstp->rq_addr); + goto out_err; + } + + return rqstp; + +out_err: + nfsd_local_fakerqst_destroy(rqstp); + return ERR_PTR(status); +} + +/* + * nfsd_open_local_fh - lookup a local filehandle @nfs_fh and map to @file + * + * This function maps a local fh to a path on a local filesystem. + * This is useful when the nfs client has the local server mounted - it can + * avoid all the NFS overhead with reads, writes and commits. + * + * on successful return, caller is responsible for calling path_put. Also + * note that this is called from nfs.ko via find_symbol() to avoid an explicit + * dependency on knfsd. So, there is no forward declaration in a header file + * for it. + */ +int nfsd_open_local_fh(struct rpc_clnt *rpc_clnt, + const struct cred *cred, + const struct nfs_fh *nfs_fh, + const fmode_t fmode, + struct file **pfilp) +{ + const struct cred *save_cred; + struct svc_rqst *rqstp; + struct svc_fh fh; + struct nfsd_file *nf; + int status = 0; + int mayflags = NFSD_MAY_LOCALIO; + __be32 beres; + + /* Save creds before calling into nfsd */ + save_cred = get_current_cred(); + + rqstp = nfsd_local_fakerqst_create(rpc_clnt, cred); + if (IS_ERR(rqstp)) { + status = PTR_ERR(rqstp); + goto out_revertcred; + } + + /* nfs_fh -> svc_fh */ + if (nfs_fh->size > NFS4_FHSIZE) { + status = -EINVAL; + goto out; + } + fh_init(&fh, NFS4_FHSIZE); + fh.fh_handle.fh_size = nfs_fh->size; + memcpy(fh.fh_handle.fh_raw, nfs_fh->data, nfs_fh->size); + + if (fmode & FMODE_READ) + mayflags |= NFSD_MAY_READ; + if (fmode & FMODE_WRITE) + mayflags |= NFSD_MAY_WRITE; + + beres = nfsd_file_acquire(rqstp, &fh, mayflags, &nf); + if (beres) { + status = nfs_stat_to_errno(be32_to_cpu(beres)); + dprintk("%s: fh_verify failed %d\n", __func__, status); + goto out_fh_put; + } + + *pfilp = get_file(nf->nf_file); + + nfsd_file_put(nf); +out_fh_put: + fh_put(&fh); + +out: + nfsd_local_fakerqst_destroy(rqstp); +out_revertcred: + revert_creds(save_cred); + return status; +} +EXPORT_SYMBOL_GPL(nfsd_open_local_fh); + +/* Compile time type checking, not used by anything */ +static nfs_to_nfsd_open_t __maybe_unused nfsd_open_local_fh_typecheck = nfsd_open_local_fh; diff --git a/fs/nfsd/trace.h b/fs/nfsd/trace.h index 77bbd23aa150..9c0610fdd11c 100644 --- a/fs/nfsd/trace.h +++ b/fs/nfsd/trace.h @@ -86,7 +86,8 @@ DEFINE_NFSD_XDR_ERR_EVENT(cant_encode); { NFSD_MAY_NOT_BREAK_LEASE, "NOT_BREAK_LEASE" }, \ { NFSD_MAY_BYPASS_GSS, "BYPASS_GSS" }, \ { NFSD_MAY_READ_IF_EXEC, "READ_IF_EXEC" }, \ - { NFSD_MAY_64BIT_COOKIE, "64BIT_COOKIE" }) + { NFSD_MAY_64BIT_COOKIE, "64BIT_COOKIE" }, \ + { NFSD_MAY_LOCALIO, "LOCALIO" }) TRACE_EVENT(nfsd_compound, TP_PROTO( diff --git a/fs/nfsd/vfs.h b/fs/nfsd/vfs.h index 57cd70062048..91c50649a8c7 100644 --- a/fs/nfsd/vfs.h +++ b/fs/nfsd/vfs.h @@ -36,6 +36,8 @@ #define NFSD_MAY_CREATE (NFSD_MAY_EXEC|NFSD_MAY_WRITE) #define NFSD_MAY_REMOVE (NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC) +#define NFSD_MAY_LOCALIO 0x800000 + struct nfsd_file; /* @@ -158,6 +160,12 @@ __be32 nfsd_permission(struct svc_rqst *, struct svc_export *, void nfsd_filp_close(struct file *fp); +int nfsd_open_local_fh(struct rpc_clnt *rpc_clnt, + const struct cred *cred, + const struct nfs_fh *nfs_fh, + const fmode_t fmode, + struct file **pfilp); + static inline int fh_want_write(struct svc_fh *fh) { int ret; diff --git a/include/linux/nfs.h b/include/linux/nfs.h index ceb70a926b95..2dacfe9742c6 100644 --- a/include/linux/nfs.h +++ b/include/linux/nfs.h @@ -8,6 +8,8 @@ #ifndef _LINUX_NFS_H #define _LINUX_NFS_H +#include +#include #include #include #include @@ -46,6 +48,10 @@ enum nfs3_stable_how { NFS_INVALID_STABLE_HOW = -1 }; +typedef int (*nfs_to_nfsd_open_t)(struct rpc_clnt *, const struct cred *, + const struct nfs_fh *, const fmode_t, + struct file **); + #ifdef CONFIG_CRC32 /** * nfs_fhandle_hash - calculate the crc32 hash for the filehandle diff --git a/include/linux/nfs_fs.h b/include/linux/nfs_fs.h index 039898d70954..a0bb947fdd1d 100644 --- a/include/linux/nfs_fs.h +++ b/include/linux/nfs_fs.h @@ -96,6 +96,8 @@ struct nfs_open_context { struct list_head list; struct nfs4_threshold *mdsthreshold; struct rcu_head rcu_head; + + struct file *local_filp; }; struct nfs_open_dir_context { diff --git a/include/linux/nfs_fs_sb.h b/include/linux/nfs_fs_sb.h index 92de074e63b9..00fe469bc72e 100644 --- a/include/linux/nfs_fs_sb.h +++ b/include/linux/nfs_fs_sb.h @@ -49,6 +49,7 @@ struct nfs_client { #define NFS_CS_DS 7 /* - Server is a DS */ #define NFS_CS_REUSEPORT 8 /* - reuse src port on reconnect */ #define NFS_CS_PNFS 9 /* - Server used for pnfs */ +#define NFS_CS_LOCAL_IO 10 /* - client is local */ struct sockaddr_storage cl_addr; /* server identifier */ size_t cl_addrlen; char * cl_hostname; /* hostname of server */ @@ -125,6 +126,10 @@ struct nfs_client { struct net *cl_net; struct list_head pending_cb_stateids; struct rcu_head rcu; + + /* localio */ + struct timespec64 cl_nfssvc_boot; + seqlock_t cl_boot_lock; }; /* diff --git a/include/linux/nfs_xdr.h b/include/linux/nfs_xdr.h index d09b9773b20c..764513a61601 100644 --- a/include/linux/nfs_xdr.h +++ b/include/linux/nfs_xdr.h @@ -1605,6 +1605,7 @@ enum { NFS_IOHDR_RESEND_PNFS, NFS_IOHDR_RESEND_MDS, NFS_IOHDR_UNSTABLE_WRITES, + NFS_IOHDR_ODIRECT, }; struct nfs_io_completion; From patchwork Fri Jun 14 03:44:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Snitzer X-Patchwork-Id: 13697839 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EF42BECC for ; Fri, 14 Jun 2024 03:44:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336678; cv=none; b=cdL7ecYhcgk4udpRBTTGDsi1Nb+uStEizwAj6CGQ6WADyb3a0kFXb3ipSq3PlV/HC5SRdUlTOr7L5g0eMNkAaY4axxCrisxbH/dAQ7CAjRT01LXJI1UJyzuBjHYec5a6990URl5KHbBTNgVv+7MteAzeKmKBvu2RMJqb3dk6e/w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336678; c=relaxed/simple; bh=d/l4FjXmeVsnN5alsopblOmEef/SwGS88uVi97x2bmc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Zx09rF4ENFBT1YW8fFm16S1JZyA5/qNLgZN29ObnnL89DavUBt4N2MRC71WLlY/1Kc6ErVu/1NkibwTyJ14aYXwUHHLRL49SdN406rE44+kUyGm2PnFzMXjuIboS9dWDDi0pIFHSALSOzocIuUOk8+V2EWK2K+LcA/cUKEw5Vdw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=n5oDkpKr; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="n5oDkpKr" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 44944C2BBFC; Fri, 14 Jun 2024 03:44:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718336677; bh=d/l4FjXmeVsnN5alsopblOmEef/SwGS88uVi97x2bmc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=n5oDkpKr4HN5m8NwEJmBosh+uiiWXe+Fix4qBS0CDufR8F66wnyuoUJYn0ljACCrL YzZH2V1Wh4SMQsQTEmhqC5BsBUpdvn/ICepJClpFUWJfLvU6kJM4YSFLktViLqNO57 IGNZhMTzS4sujcBuOZm7Uz1pTgUSijFMAx2BZ2257QULgbZdMvFUeJ65tVSCh5LWhE eCM0EnfSoIkYsGY/S5WOkrRvXYoxCbHO9/Y0zPuBuVXpwmpwBF25vYnuPNiyHRMtb3 WDjAtOE14FCwT7o8lwEyvHfpFrJsXtmgqge5Gv2Uis3sTY2XiRY0C+xyG30X5u90Ub u1JQJmXjdWOwg== From: Mike Snitzer To: linux-nfs@vger.kernel.org Cc: Jeff Layton , Chuck Lever , Trond Myklebust , NeilBrown , snitzer@hammerspace.com Subject: [PATCH v3 07/18] NFS: for localio don't call filesystem read() and write() routines directly Date: Thu, 13 Jun 2024 23:44:15 -0400 Message-ID: <20240614034426.31043-8-snitzer@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240614034426.31043-1-snitzer@kernel.org> References: <20240614034426.31043-1-snitzer@kernel.org> Precedence: bulk X-Mailing-List: linux-nfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Trond Myklebust Some filesystem writeback routines can end up taking up a lot of stack space (particularly xfs). Instead of risking running over due to the extra overhead from the NFS stack, we should just call these routines from a workqueue job. Signed-off-by: Trond Myklebust Signed-off-by: Mike Snitzer --- fs/nfs/inode.c | 57 +++++++++++++++++--------- fs/nfs/internal.h | 1 + fs/nfs/localio.c | 102 +++++++++++++++++++++++++++++++++++----------- 3 files changed, 118 insertions(+), 42 deletions(-) diff --git a/fs/nfs/inode.c b/fs/nfs/inode.c index 4f88b860494f..58be75294a23 100644 --- a/fs/nfs/inode.c +++ b/fs/nfs/inode.c @@ -2394,35 +2394,54 @@ static void nfs_destroy_inodecache(void) kmem_cache_destroy(nfs_inode_cachep); } +struct workqueue_struct *nfslocaliod_workqueue; struct workqueue_struct *nfsiod_workqueue; EXPORT_SYMBOL_GPL(nfsiod_workqueue); /* - * start up the nfsiod workqueue - */ -static int nfsiod_start(void) -{ - struct workqueue_struct *wq; - dprintk("RPC: creating workqueue nfsiod\n"); - wq = alloc_workqueue("nfsiod", WQ_MEM_RECLAIM | WQ_UNBOUND, 0); - if (wq == NULL) - return -ENOMEM; - nfsiod_workqueue = wq; - return 0; -} - -/* - * Destroy the nfsiod workqueue + * Destroy the nfsiod workqueues */ static void nfsiod_stop(void) { struct workqueue_struct *wq; wq = nfsiod_workqueue; - if (wq == NULL) - return; - nfsiod_workqueue = NULL; - destroy_workqueue(wq); + if (wq != NULL) { + nfsiod_workqueue = NULL; + destroy_workqueue(wq); + } +#if IS_ENABLED(CONFIG_NFS_LOCALIO) + wq = nfslocaliod_workqueue; + if (wq != NULL) { + nfslocaliod_workqueue = NULL; + destroy_workqueue(wq); + } +#endif /* CONFIG_NFS_LOCALIO */ +} + +/* + * Start the nfsiod workqueues + */ +static int nfsiod_start(void) +{ + dprintk("RPC: creating workqueue nfsiod\n"); + nfsiod_workqueue = alloc_workqueue("nfsiod", WQ_MEM_RECLAIM | WQ_UNBOUND, 0); + if (nfsiod_workqueue == NULL) + return -ENOMEM; +#if IS_ENABLED(CONFIG_NFS_LOCALIO) + /* + * localio writes need to use a normal (non-memreclaim) workqueue. + * When we start getting low on space, XFS goes and calls flush_work() on + * a non-memreclaim work queue, which causes a priority inversion problem. + */ + dprintk("RPC: creating workqueue nfslocaliod\n"); + nfslocaliod_workqueue = alloc_workqueue("nfslocaliod", WQ_UNBOUND, 0); + if (unlikely(nfslocaliod_workqueue == NULL)) { + nfsiod_stop(); + return -ENOMEM; + } +#endif /* CONFIG_NFS_LOCALIO */ + return 0; } unsigned int nfs_net_id; diff --git a/fs/nfs/internal.h b/fs/nfs/internal.h index c933421eb6af..29152dc4b11c 100644 --- a/fs/nfs/internal.h +++ b/fs/nfs/internal.h @@ -440,6 +440,7 @@ int nfs_check_flags(int); /* inode.c */ extern struct workqueue_struct *nfsiod_workqueue; +extern struct workqueue_struct *nfslocaliod_workqueue; extern struct inode *nfs_alloc_inode(struct super_block *sb); extern void nfs_free_inode(struct inode *); extern int nfs_write_inode(struct inode *, struct writeback_control *); diff --git a/fs/nfs/localio.c b/fs/nfs/localio.c index 286cd0ded1b6..d51bbdbece88 100644 --- a/fs/nfs/localio.c +++ b/fs/nfs/localio.c @@ -64,6 +64,12 @@ struct nfs_local_fsync_ctx { }; static void nfs_local_fsync_work(struct work_struct *work); +struct nfs_local_io_args { + struct nfs_local_kiocb *iocb; + struct work_struct work; + struct completion *done; +}; + /* * We need to translate between nfs status return values and * the local errno values which may not be the same. @@ -420,21 +426,38 @@ nfs_local_read_aio_complete(struct kiocb *kiocb, long ret) nfs_local_pgio_complete(iocb); } -static int -nfs_do_local_read(struct nfs_pgio_header *hdr, struct file *filp, - const struct rpc_call_ops *call_ops) +static void nfs_local_call_read(struct work_struct *work) { - struct nfs_local_kiocb *iocb; + struct nfs_local_io_args *args = + container_of(work, struct nfs_local_io_args, work); + struct nfs_local_kiocb *iocb = args->iocb; + struct file *filp = iocb->kiocb.ki_filp; struct iov_iter iter; ssize_t status; + nfs_local_iter_init(&iter, iocb, READ); + + status = filp->f_op->read_iter(&iocb->kiocb, &iter); + if (status != -EIOCBQUEUED) { + nfs_local_read_done(iocb, status); + nfs_local_pgio_release(iocb); + } + complete(args->done); +} + +static int nfs_do_local_read(struct nfs_pgio_header *hdr, struct file *filp, + const struct rpc_call_ops *call_ops) +{ + struct nfs_local_io_args args; + DECLARE_COMPLETION_ONSTACK(done); + struct nfs_local_kiocb *iocb; + dprintk("%s: vfs_read count=%u pos=%llu\n", __func__, hdr->args.count, hdr->args.offset); iocb = nfs_local_iocb_alloc(hdr, filp, GFP_KERNEL); if (iocb == NULL) return -ENOMEM; - nfs_local_iter_init(&iter, iocb, READ); nfs_local_pgio_init(hdr, call_ops); hdr->res.eof = false; @@ -444,11 +467,18 @@ nfs_do_local_read(struct nfs_pgio_header *hdr, struct file *filp, iocb->kiocb.ki_complete = nfs_local_read_aio_complete; } - status = filp->f_op->read_iter(&iocb->kiocb, &iter); - if (status != -EIOCBQUEUED) { - nfs_local_read_done(iocb, status); - nfs_local_pgio_release(iocb); - } + /* + * Don't call filesystem read() routines directly. + * In order to avoid issues with stack overflow, + * call the read routines from a workqueue job. + */ + args.iocb = iocb; + args.done = &done; + INIT_WORK_ONSTACK(&args.work, nfs_local_call_read); + queue_work(nfslocaliod_workqueue, &args.work); + wait_for_completion(&done); + destroy_work_on_stack(&args.work); + return 0; } @@ -558,14 +588,35 @@ nfs_local_write_aio_complete(struct kiocb *kiocb, long ret) nfs_local_pgio_complete(iocb); } -static int -nfs_do_local_write(struct nfs_pgio_header *hdr, struct file *filp, - const struct rpc_call_ops *call_ops) +static void nfs_local_call_write(struct work_struct *work) { - struct nfs_local_kiocb *iocb; + struct nfs_local_io_args *args = + container_of(work, struct nfs_local_io_args, work); + struct nfs_local_kiocb *iocb = args->iocb; + struct file *filp = iocb->kiocb.ki_filp; struct iov_iter iter; ssize_t status; + nfs_local_iter_init(&iter, iocb, WRITE); + + file_start_write(filp); + status = filp->f_op->write_iter(&iocb->kiocb, &iter); + file_end_write(filp); + if (status != -EIOCBQUEUED) { + nfs_local_write_done(iocb, status); + nfs_get_vfs_attr(filp, iocb->hdr->res.fattr); + nfs_local_pgio_release(iocb); + } + complete(args->done); +} + +static int nfs_do_local_write(struct nfs_pgio_header *hdr, struct file *filp, + const struct rpc_call_ops *call_ops) +{ + struct nfs_local_io_args args; + DECLARE_COMPLETION_ONSTACK(done); + struct nfs_local_kiocb *iocb; + dprintk("%s: vfs_write count=%u pos=%llu %s\n", __func__, hdr->args.count, hdr->args.offset, (hdr->args.stable == NFS_UNSTABLE) ? "unstable" : "stable"); @@ -573,7 +624,6 @@ nfs_do_local_write(struct nfs_pgio_header *hdr, struct file *filp, iocb = nfs_local_iocb_alloc(hdr, filp, GFP_NOIO); if (iocb == NULL) return -ENOMEM; - nfs_local_iter_init(&iter, iocb, WRITE); switch (hdr->args.stable) { default: @@ -593,14 +643,20 @@ nfs_do_local_write(struct nfs_pgio_header *hdr, struct file *filp, nfs_set_local_verifier(hdr->inode, hdr->res.verf, hdr->args.stable); - file_start_write(filp); - status = filp->f_op->write_iter(&iocb->kiocb, &iter); - file_end_write(filp); - if (status != -EIOCBQUEUED) { - nfs_local_write_done(iocb, status); - nfs_get_vfs_attr(filp, hdr->res.fattr); - nfs_local_pgio_release(iocb); - } + /* + * Don't call filesystem write() routines directly. + * Some filesystem writeback routines can end up taking up a lot of + * stack (particularly xfs). Instead of risking running over due to + * the extra overhead from the NFS stack, call these write routines + * from a workqueue job. + */ + args.iocb = iocb; + args.done = &done; + INIT_WORK_ONSTACK(&args.work, nfs_local_call_write); + queue_work(nfslocaliod_workqueue, &args.work); + wait_for_completion(&done); + destroy_work_on_stack(&args.work); + return 0; } From patchwork Fri Jun 14 03:44:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Snitzer X-Patchwork-Id: 13697840 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 32807ECC for ; Fri, 14 Jun 2024 03:44:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336679; cv=none; b=sOXt17IOx/sxkBfAzaYUH4RJ31z1qkVf29WwWIsXy5kg9GMxbW8jGRAjTOsJo/AzVmP0/iA0AWJhPH7UIylDvv6s/lrHxDhs8ZJmIutkPuFfl6wc4+o9QMTpRaisZPbUVwGzkRPA57dA5fZ5BGKaD9n8BJiF2LnHEmUrrfJsEdw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336679; c=relaxed/simple; bh=X3eeEYlS9YLp7iD3PjCjsrl81HV+UMdSCrHhb+Bvz6Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CYicmrTrIOjPJnMvwQgqysshDjJV8Adwk/npQZzLkFQl7vJihlEDq6FPNxRI92UuenQg1Pbv6bAcLYyHWsI8cHhdS1C3xD8EAWx5oOT1gsErzBB50v61ijjw3wIB1GJYNYA+xI+cgMbM/wgUei9HStqyjnfbuMvdPh1TSfSjQLc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=g4rZ2wgh; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="g4rZ2wgh" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 918B0C2BBFC; Fri, 14 Jun 2024 03:44:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718336678; bh=X3eeEYlS9YLp7iD3PjCjsrl81HV+UMdSCrHhb+Bvz6Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=g4rZ2wghFddpF4zZd1Yk1Ef/Wz7kzuXSfAhT9pe7k56yyV2Nqpv7MJtKokpHpRbgj 3Tw3po7U+2LRFEeNeVp4+tT4XsJLIXUusWpHYHpLD2XkIj+Z3DRP4vJjwR0N4RiFDN R4AEvpQOV7AUIHdCIuUgBCpB1XYD5EY1v/KAcLK7Y9vgDwlwTD63eRGTW3/i2Vg54u pK0D1omTrbkTa8OVuZPeclLIHYS6+LK1z6LWROWIm53C+MpS3metsCRQvvWILvtkRI +rTQ9bMjwGuQhhE4NixJNGzkLnYZlUaeT87A/UrLPcegsLfgtnaQ3Q/OkiZcOtwfV+ XcT7fAx7PyNNQ== From: Mike Snitzer To: linux-nfs@vger.kernel.org Cc: Jeff Layton , Chuck Lever , Trond Myklebust , NeilBrown , snitzer@hammerspace.com Subject: [PATCH v3 08/18] NFS: Enable localio for non-pNFS I/O Date: Thu, 13 Jun 2024 23:44:16 -0400 Message-ID: <20240614034426.31043-9-snitzer@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240614034426.31043-1-snitzer@kernel.org> References: <20240614034426.31043-1-snitzer@kernel.org> Precedence: bulk X-Mailing-List: linux-nfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Trond Myklebust Try a local open of the file we're writing to, and if it succeeds, then do local I/O. Signed-off-by: Trond Myklebust Signed-off-by: Mike Snitzer --- fs/nfs/pagelist.c | 19 ++++++++++--------- fs/nfs/write.c | 7 ++++++- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/fs/nfs/pagelist.c b/fs/nfs/pagelist.c index b08420b8e664..3ee78da5ebc4 100644 --- a/fs/nfs/pagelist.c +++ b/fs/nfs/pagelist.c @@ -1063,6 +1063,7 @@ EXPORT_SYMBOL_GPL(nfs_generic_pgio); static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc) { struct nfs_pgio_header *hdr; + struct file *filp; int ret; unsigned short task_flags = 0; @@ -1074,18 +1075,18 @@ static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc) nfs_pgheader_init(desc, hdr, nfs_pgio_header_free); ret = nfs_generic_pgio(desc, hdr); if (ret == 0) { + struct nfs_client *clp = NFS_SERVER(hdr->inode)->nfs_client; + + filp = nfs_local_file_open(clp, hdr->cred, hdr->args.fh, + hdr->args.context); + if (NFS_SERVER(hdr->inode)->nfs_client->cl_minorversion) task_flags = RPC_TASK_MOVEABLE; - ret = nfs_initiate_pgio(desc, - NFS_SERVER(hdr->inode)->nfs_client, - NFS_CLIENT(hdr->inode), - hdr, - hdr->cred, - NFS_PROTO(hdr->inode), - desc->pg_rpc_callops, - desc->pg_ioflags, + ret = nfs_initiate_pgio(desc, clp, NFS_CLIENT(hdr->inode), + hdr, hdr->cred, NFS_PROTO(hdr->inode), + desc->pg_rpc_callops, desc->pg_ioflags, RPC_TASK_CRED_NOREF | task_flags, - NULL); + filp); } return ret; } diff --git a/fs/nfs/write.c b/fs/nfs/write.c index b29b0fd5431f..b2c06b8b88cd 100644 --- a/fs/nfs/write.c +++ b/fs/nfs/write.c @@ -1802,6 +1802,8 @@ nfs_commit_list(struct inode *inode, struct list_head *head, int how, struct nfs_commit_info *cinfo) { struct nfs_commit_data *data; + struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; + struct file *filp; unsigned short task_flags = 0; /* another commit raced with us */ @@ -1818,9 +1820,12 @@ nfs_commit_list(struct inode *inode, struct list_head *head, int how, nfs_init_commit(data, head, NULL, cinfo); if (NFS_SERVER(inode)->nfs_client->cl_minorversion) task_flags = RPC_TASK_MOVEABLE; + + filp = nfs_local_file_open(clp, data->cred, data->args.fh, + data->context); return nfs_initiate_commit(NFS_CLIENT(inode), data, NFS_PROTO(inode), data->mds_ops, how, - RPC_TASK_CRED_NOREF | task_flags, NULL); + RPC_TASK_CRED_NOREF | task_flags, filp); } /* From patchwork Fri Jun 14 03:44:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Snitzer X-Patchwork-Id: 13697841 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 751E7ECC for ; Fri, 14 Jun 2024 03:44:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336680; cv=none; b=SAYXECqWRFIpIvF/Myk5x9udUvXyc9q5/cH5rhqE95VSp4lYvQPxB8rV5sKZGdAt7Z3GlPn+QJgXVjv+yrjEdO+Txw3+UmMC1VvGS/3I/6ihaopklfvCupX82mFOskPHDxo7Qn/Oxu2hYkD95/eFizlVhsVLTle5y3rlzOmwryU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336680; c=relaxed/simple; bh=Noogo8VEf449iO4uxp/t7QHEdzBDQXlb77KtF8kwk7U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VR+5aQqfSegCrBtmimpFlNz5RZ+g8QrDHmMFVOH19F5ZaXzTkSEJD3YhKuZ3TLpsrtdgkfAx3Oua/yWmxG2bozqqN06jWRxaqT72Ezd+88Yt+qQkkquIkJg+L7XMOKd8KsrisHQgrdDcCbU6qH6TCQmJmsvBjG/BWqkKDcUPj7w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=QwQYq1hj; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="QwQYq1hj" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D63B8C2BBFC; Fri, 14 Jun 2024 03:44:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718336680; bh=Noogo8VEf449iO4uxp/t7QHEdzBDQXlb77KtF8kwk7U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QwQYq1hjdONG0UJeVpnWe5dmO4es1KZfHbS7Ln2Puk29+uBIQaXWsgDxOKw62hpEd w5DdAId9fpGXrOGB3Qv6/R3E4Xde3d9f/bw27iM4MdmwGSoPtYDVPPqYGNVVxY5gQq X4g34cAc3GQj2vPm5buaNrkEQ5y4um7Szkc0UPYsoU5BRCWKxDSgZvmuvpkLOL1wIc 3td7G+PO42K9xdY4+beHNn1Bqu/z1/DoFZqc0Yl2pugSMXWaQHkWVvMUcc5l1lSqdT H+w43QyUsYsibor2H161RB751nm6pwvxcncvIdQlcvsXt3Z1nTqiYUS4ssyhd0n5N9 mfDm78XtZZJ3g== From: Mike Snitzer To: linux-nfs@vger.kernel.org Cc: Jeff Layton , Chuck Lever , Trond Myklebust , NeilBrown , snitzer@hammerspace.com Subject: [PATCH v3 09/18] pnfs/flexfiles: Enable localio for flexfiles I/O Date: Thu, 13 Jun 2024 23:44:17 -0400 Message-ID: <20240614034426.31043-10-snitzer@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240614034426.31043-1-snitzer@kernel.org> References: <20240614034426.31043-1-snitzer@kernel.org> Precedence: bulk X-Mailing-List: linux-nfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Trond Myklebust If the DS is local to this client, then we should be able to use local I/O to write the data. Signed-off-by: Peng Tao Signed-off-by: Lance Shelton Signed-off-by: Trond Myklebust Signed-off-by: Mike Snitzer --- fs/nfs/flexfilelayout/flexfilelayout.c | 113 ++++++++++++++++++++-- fs/nfs/flexfilelayout/flexfilelayout.h | 2 + fs/nfs/flexfilelayout/flexfilelayoutdev.c | 6 ++ 3 files changed, 112 insertions(+), 9 deletions(-) diff --git a/fs/nfs/flexfilelayout/flexfilelayout.c b/fs/nfs/flexfilelayout/flexfilelayout.c index 3ea07446f05a..ec6aaa110a7b 100644 --- a/fs/nfs/flexfilelayout/flexfilelayout.c +++ b/fs/nfs/flexfilelayout/flexfilelayout.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -162,6 +163,52 @@ decode_name(struct xdr_stream *xdr, u32 *id) return 0; } +static struct file * +ff_local_open_fh(struct pnfs_layout_segment *lseg, + u32 ds_idx, + struct nfs_client *clp, + const struct cred *cred, + struct nfs_fh *fh, + fmode_t mode) +{ + struct nfs4_ff_layout_mirror *mirror = FF_LAYOUT_COMP(lseg, ds_idx); + struct file *filp, *new, __rcu **pfile; + + if (!nfs_server_is_local(clp)) + return NULL; + if (mode & FMODE_WRITE) { + /* + * Always request read and write access since this corresponds + * to a rw layout. + */ + mode |= FMODE_READ; + pfile = &mirror->rw_file; + } else + pfile = &mirror->ro_file; + + new = NULL; + rcu_read_lock(); + filp = rcu_dereference(*pfile); + if (!filp) { + rcu_read_unlock(); + new = nfs_local_open_fh(clp, cred, fh, mode); + if (IS_ERR(new)) + return NULL; + rcu_read_lock(); + /* try to swap in the pointer */ + filp = cmpxchg(pfile, NULL, new); + if (!filp) { + filp = new; + new = NULL; + } + } + filp = get_file_rcu(&filp); + rcu_read_unlock(); + if (new) + fput(new); + return filp; +} + static bool ff_mirror_match_fh(const struct nfs4_ff_layout_mirror *m1, const struct nfs4_ff_layout_mirror *m2) { @@ -237,8 +284,15 @@ static struct nfs4_ff_layout_mirror *ff_layout_alloc_mirror(gfp_t gfp_flags) static void ff_layout_free_mirror(struct nfs4_ff_layout_mirror *mirror) { + struct file *filp; const struct cred *cred; + filp = rcu_access_pointer(mirror->ro_file); + if (filp) + fput(filp); + filp = rcu_access_pointer(mirror->rw_file); + if (filp) + fput(filp); ff_layout_remove_mirror(mirror); kfree(mirror->fh_versions); cred = rcu_access_pointer(mirror->ro_cred); @@ -414,6 +468,7 @@ ff_layout_alloc_lseg(struct pnfs_layout_hdr *lh, struct nfs4_ff_layout_mirror *mirror; struct cred *kcred; const struct cred __rcu *cred; + const struct cred __rcu *old; kuid_t uid; kgid_t gid; u32 ds_count, fh_count, id; @@ -513,13 +568,26 @@ ff_layout_alloc_lseg(struct pnfs_layout_hdr *lh, mirror = ff_layout_add_mirror(lh, fls->mirror_array[i]); if (mirror != fls->mirror_array[i]) { + struct file *filp; + /* swap cred ptrs so free_mirror will clean up old */ if (lgr->range.iomode == IOMODE_READ) { - cred = xchg(&mirror->ro_cred, cred); - rcu_assign_pointer(fls->mirror_array[i]->ro_cred, cred); + old = xchg(&mirror->ro_cred, cred); + rcu_assign_pointer(fls->mirror_array[i]->ro_cred, old); + /* drop file if creds changed */ + if (old != cred) { + filp = rcu_dereference_protected(xchg(&mirror->ro_file, NULL), 1); + if (filp) + fput(filp); + } } else { - cred = xchg(&mirror->rw_cred, cred); - rcu_assign_pointer(fls->mirror_array[i]->rw_cred, cred); + old = xchg(&mirror->rw_cred, cred); + rcu_assign_pointer(fls->mirror_array[i]->rw_cred, old); + if (old != cred) { + filp = rcu_dereference_protected(xchg(&mirror->rw_file, NULL), 1); + if (filp) + fput(filp); + } } ff_layout_free_mirror(fls->mirror_array[i]); fls->mirror_array[i] = mirror; @@ -1757,6 +1825,7 @@ ff_layout_read_pagelist(struct nfs_pageio_descriptor *desc, struct pnfs_layout_segment *lseg = hdr->lseg; struct nfs4_pnfs_ds *ds; struct rpc_clnt *ds_clnt; + struct file *filp; struct nfs4_ff_layout_mirror *mirror; const struct cred *ds_cred; loff_t offset = hdr->args.offset; @@ -1803,12 +1872,20 @@ ff_layout_read_pagelist(struct nfs_pageio_descriptor *desc, hdr->args.offset = offset; hdr->mds_offset = offset; + /* Start IO accounting for local read */ + filp = ff_local_open_fh(lseg, idx, ds->ds_clp, ds_cred, fh, + FMODE_READ); + if (filp) { + hdr->task.tk_start = ktime_get(); + ff_layout_read_record_layoutstats_start(&hdr->task, hdr); + } + /* Perform an asynchronous read to ds */ nfs_initiate_pgio(desc, ds->ds_clp, ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops, vers == 3 ? &ff_layout_read_call_ops_v3 : &ff_layout_read_call_ops_v4, - 0, RPC_TASK_SOFTCONN, NULL); + 0, RPC_TASK_SOFTCONN, filp); put_cred(ds_cred); return PNFS_ATTEMPTED; @@ -1829,6 +1906,7 @@ ff_layout_write_pagelist(struct nfs_pageio_descriptor *desc, struct pnfs_layout_segment *lseg = hdr->lseg; struct nfs4_pnfs_ds *ds; struct rpc_clnt *ds_clnt; + struct file *filp; struct nfs4_ff_layout_mirror *mirror; const struct cred *ds_cred; loff_t offset = hdr->args.offset; @@ -1873,12 +1951,20 @@ ff_layout_write_pagelist(struct nfs_pageio_descriptor *desc, */ hdr->args.offset = offset; + /* Start IO accounting for local write */ + filp = ff_local_open_fh(lseg, idx, ds->ds_clp, ds_cred, fh, + FMODE_READ|FMODE_WRITE); + if (filp) { + hdr->task.tk_start = ktime_get(); + ff_layout_write_record_layoutstats_start(&hdr->task, hdr); + } + /* Perform an asynchronous write */ nfs_initiate_pgio(desc, ds->ds_clp, ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops, vers == 3 ? &ff_layout_write_call_ops_v3 : &ff_layout_write_call_ops_v4, - sync, RPC_TASK_SOFTCONN, NULL); + sync, RPC_TASK_SOFTCONN, filp); put_cred(ds_cred); return PNFS_ATTEMPTED; @@ -1912,6 +1998,7 @@ static int ff_layout_initiate_commit(struct nfs_commit_data *data, int how) struct pnfs_layout_segment *lseg = data->lseg; struct nfs4_pnfs_ds *ds; struct rpc_clnt *ds_clnt; + struct file *filp; struct nfs4_ff_layout_mirror *mirror; const struct cred *ds_cred; u32 idx; @@ -1950,10 +2037,18 @@ static int ff_layout_initiate_commit(struct nfs_commit_data *data, int how) if (fh) data->args.fh = fh; + /* Start IO accounting for local commit */ + filp = ff_local_open_fh(lseg, idx, ds->ds_clp, ds_cred, fh, + FMODE_READ|FMODE_WRITE); + if (filp) { + data->task.tk_start = ktime_get(); + ff_layout_commit_record_layoutstats_start(&data->task, data); + } + ret = nfs_initiate_commit(ds_clnt, data, ds->ds_clp->rpc_ops, - vers == 3 ? &ff_layout_commit_call_ops_v3 : - &ff_layout_commit_call_ops_v4, - how, RPC_TASK_SOFTCONN, NULL); + vers == 3 ? &ff_layout_commit_call_ops_v3 : + &ff_layout_commit_call_ops_v4, + how, RPC_TASK_SOFTCONN, filp); put_cred(ds_cred); return ret; out_err: diff --git a/fs/nfs/flexfilelayout/flexfilelayout.h b/fs/nfs/flexfilelayout/flexfilelayout.h index f84b3fb0dddd..8e042df5a2c9 100644 --- a/fs/nfs/flexfilelayout/flexfilelayout.h +++ b/fs/nfs/flexfilelayout/flexfilelayout.h @@ -82,7 +82,9 @@ struct nfs4_ff_layout_mirror { struct nfs_fh *fh_versions; nfs4_stateid stateid; const struct cred __rcu *ro_cred; + struct file __rcu *ro_file; const struct cred __rcu *rw_cred; + struct file __rcu *rw_file; refcount_t ref; spinlock_t lock; unsigned long flags; diff --git a/fs/nfs/flexfilelayout/flexfilelayoutdev.c b/fs/nfs/flexfilelayout/flexfilelayoutdev.c index e028f5a0ef5f..e58bedfb1dcc 100644 --- a/fs/nfs/flexfilelayout/flexfilelayoutdev.c +++ b/fs/nfs/flexfilelayout/flexfilelayoutdev.c @@ -395,6 +395,12 @@ nfs4_ff_layout_prepare_ds(struct pnfs_layout_segment *lseg, /* connect success, check rsize/wsize limit */ if (!status) { + /* + * ds_clp is put in destroy_ds(). + * keep ds_clp even if DS is local, so that if local IO cannot + * proceed somehow, we can fall back to NFS whenever we want. + */ + nfs_local_probe(ds->ds_clp); max_payload = nfs_block_size(rpc_max_payload(ds->ds_clp->cl_rpcclient), NULL); From patchwork Fri Jun 14 03:44:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Snitzer X-Patchwork-Id: 13697842 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8FCC2ECC for ; Fri, 14 Jun 2024 03:44:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336681; cv=none; b=CQt9MNn6Gn204aqucWqz7srivSgFyMLYh+lOW26BPhAT53bpiJJTD+opKZYLYcWiOKvFYIvUo0AQ5109YevwUy7ynJI9Rp/zgu2j/1Ao1YvZSd9b20sJ4qYdZMgT/Le5xjyfQD0z8PXELW4AOzm195frnwB1xrO1DciVOUeZzcM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336681; c=relaxed/simple; bh=MwJiLuLN5DLd1EUs+vDAWw+oUczIEBfH8nwIdbEReFw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tW4KHm1v2l2IXrVseLYVJ+13GEYRHV+5+A7G+8jzMAq97u57bTel5lmsVFSdILIybBW4y2KWB6EsmL6WTFYEPscINUX+3/+/ZugY2cciuhwhCJ9Xo6e8aGDTRsQKI8E0jwDWqM9oQAKc68aRJbkR0ulX94Xkcum8CbETRZycO3M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hVUIXiod; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="hVUIXiod" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 32A22C2BBFC; Fri, 14 Jun 2024 03:44:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718336681; bh=MwJiLuLN5DLd1EUs+vDAWw+oUczIEBfH8nwIdbEReFw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hVUIXiod0gacRE4W5GGwjziVobVpvlJCHhnwtYwH1U32hn6WCZ/ni3r7FwHpL8lAg T5S4/R5nL2gHuxvKi+JT4/UFszunKC+iHvd1l4VDkkZpMN5l8SviL9MNfxlcXd2gQg rxKdcpp8W0ju8bdYTxdOXQpUfyuJ/7pYMJN96LE7spM2GES11RUKZC6rMT/BjiqPR+ w264TJC6+ITQxYw2SVRG8/oYYDIu/ezdQS37IuVMQHMRoIvzK11jeBLJ76xDLY4pZq vvH5Tv8CERcnea+b3rjqLTebMFULl/W0m3FuP0561jmUEqodm/vI9rt5xAhU3VuQn5 pvZKLy5FnDRXg== From: Mike Snitzer To: linux-nfs@vger.kernel.org Cc: Jeff Layton , Chuck Lever , Trond Myklebust , NeilBrown , snitzer@hammerspace.com Subject: [PATCH v3 10/18] nfs: implement v3 and v4 client support for NFS_LOCALIO_PROGRAM Date: Thu, 13 Jun 2024 23:44:18 -0400 Message-ID: <20240614034426.31043-11-snitzer@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240614034426.31043-1-snitzer@kernel.org> References: <20240614034426.31043-1-snitzer@kernel.org> Precedence: bulk X-Mailing-List: linux-nfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 LOCALIOPROC_GETUUID allows client to discover server's uuid. nfs_local_probe() will retrieve server's uuid via LOCALIO protocol and verify the server with that uuid it is known to be local. This ensures client and server 1: support localio 2: are local to each other. While doing so, factor out nfs_init_localioclient() so it is used by both nfs3client.c and nfs4client.c Signed-off-by: Mike Snitzer --- fs/nfs/client.c | 8 ++-- fs/nfs/internal.h | 26 ++++++++++++ fs/nfs/localio.c | 87 +++++++++++++++++++++++++++++++++++---- fs/nfs/nfs3_fs.h | 1 + fs/nfs/nfs3client.c | 25 +++++++++++ fs/nfs/nfs3proc.c | 3 ++ fs/nfs/nfs3xdr.c | 67 ++++++++++++++++++++++++++++++ fs/nfs/nfs4_fs.h | 2 + fs/nfs/nfs4client.c | 23 +++++++++++ fs/nfs/nfs4proc.c | 3 ++ fs/nfs/nfs4xdr.c | 52 +++++++++++++++++++++++ include/linux/nfs_fs_sb.h | 1 + include/linux/nfs_xdr.h | 10 +++++ include/uapi/linux/nfs.h | 4 ++ 14 files changed, 300 insertions(+), 12 deletions(-) diff --git a/fs/nfs/client.c b/fs/nfs/client.c index 9170e6036fd2..7044b8b3b332 100644 --- a/fs/nfs/client.c +++ b/fs/nfs/client.c @@ -170,7 +170,7 @@ struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init) } INIT_LIST_HEAD(&clp->cl_superblocks); - clp->cl_rpcclient = ERR_PTR(-EINVAL); + clp->cl_rpcclient = clp->cl_rpcclient_localio = ERR_PTR(-EINVAL); clp->cl_flags = cl_init->init_flags; clp->cl_proto = cl_init->proto; @@ -430,8 +430,10 @@ struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init) list_add_tail(&new->cl_share_link, &nn->nfs_client_list); spin_unlock(&nn->nfs_client_lock); - nfs_local_probe(new); - return rpc_ops->init_client(new, cl_init); + new = rpc_ops->init_client(new, cl_init); + if (!IS_ERR(new)) + nfs_local_probe(new); + return new; } spin_unlock(&nn->nfs_client_lock); diff --git a/fs/nfs/internal.h b/fs/nfs/internal.h index 29152dc4b11c..aed826cdb7a7 100644 --- a/fs/nfs/internal.h +++ b/fs/nfs/internal.h @@ -453,6 +453,32 @@ extern bool nfs_check_cache_invalid(struct inode *, unsigned long); extern int nfs_wait_bit_killable(struct wait_bit_key *key, int mode); #if IS_ENABLED(CONFIG_NFS_LOCALIO) +/* + * Initialise an NFS localio client connection. + * Inlined here to allow nfs[34]client.c to share this code. + */ +static __always_inline void +nfs_init_localioclient(struct nfs_client *clp, + const struct rpc_program *program, u32 vers) +{ + bool supported = false; + + if (unlikely(!IS_ERR(clp->cl_rpcclient_localio))) { + supported = true; + goto out; + } + clp->cl_rpcclient_localio = rpc_bind_new_program(clp->cl_rpcclient, + program, vers); + if (IS_ERR(clp->cl_rpcclient_localio)) + goto out; + /* No errors! Assume that localio is supported */ + supported = true; +out: + dfprintk_rcu(CLIENT, "%s: server (%s) %s NFS v%u LOCALIO\n", __func__, + rpc_peeraddr2str(clp->cl_rpcclient_localio, RPC_DISPLAY_ADDR), + (supported ? "supports" : "does not support"), vers); +} + /* localio.c */ extern void nfs_local_init(void); extern void nfs_local_disable(struct nfs_client *); diff --git a/fs/nfs/localio.c b/fs/nfs/localio.c index d51bbdbece88..c6ad31afe6a1 100644 --- a/fs/nfs/localio.c +++ b/fs/nfs/localio.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -145,10 +146,14 @@ static struct nfs_local_open_ctx __local_open_ctx __read_mostly; static bool localio_enabled __read_mostly = true; module_param(localio_enabled, bool, 0644); +static inline bool nfs_client_is_local(const struct nfs_client *clp) +{ + return !!test_bit(NFS_CS_LOCAL_IO, &clp->cl_flags); +} + bool nfs_server_is_local(const struct nfs_client *clp) { - return test_bit(NFS_CS_LOCAL_IO, &clp->cl_flags) != 0 && - localio_enabled; + return nfs_client_is_local(clp) && localio_enabled; } EXPORT_SYMBOL_GPL(nfs_server_is_local); @@ -225,19 +230,82 @@ nfs_local_disable(struct nfs_client *clp) if (test_and_clear_bit(NFS_CS_LOCAL_IO, &clp->cl_flags)) { trace_nfs_local_disable(clp); nfs_local_put_lookup_ctx(); + if (!IS_ERR(clp->cl_rpcclient_localio)) { + rpc_shutdown_client(clp->cl_rpcclient_localio); + clp->cl_rpcclient_localio = ERR_PTR(-EINVAL); + } } } +static bool nfs_local_server_getuuid(struct nfs_client *clp, uuid_t *nfsd_uuid) +{ + u8 uuid[UUID_SIZE]; + struct nfs_getuuidres res = { + uuid, + }; + struct rpc_message msg = { + .rpc_resp = &res, + }; + int status; + + clp->rpc_ops->init_localioclient(clp); + if (IS_ERR(clp->cl_rpcclient_localio)) + return false; + + dprintk("%s: NFS issuing getuuid\n", __func__); + msg.rpc_proc = &clp->cl_rpcclient_localio->cl_procinfo[LOCALIOPROC_GETUUID]; + status = rpc_call_sync(clp->cl_rpcclient_localio, &msg, 0); + dprintk("%s: NFS reply getuuid: status=%d uuid=%pU\n", + __func__, status, res.uuid); + if (status) + return false; + + import_uuid(nfsd_uuid, res.uuid); + + return true; +} + /* - * nfs_local_probe - probe local i/o support for an nfs_client + * nfs_local_probe - probe local i/o support for an nfs_server and nfs_client + * - called after alloc_client and init_client (so cl_rpcclient exists) + * - this function is idempotent, it can be called for old or new clients */ -void -nfs_local_probe(struct nfs_client *clp) +void nfs_local_probe(struct nfs_client *clp) { - bool enable = false; + uuid_t uuid; - if (enable) - nfs_local_enable(clp); + if (!localio_enabled) + goto unsupported; + + if (nfs_client_is_local(clp)) { + /* If already enabled, disable and re-enable */ + nfs_local_disable(clp); + } + + switch (clp->cl_rpcclient->cl_vers) { + case 3: + case 4: + /* + * Retrieve server's uuid via LOCALIO protocol and verify the + * server with that uuid it is known to be local. This ensures + * client and server 1: support localio 2: are local to each other + * by verifying client's nfsd, with specified uuid, is local. + */ + if (!nfs_local_server_getuuid(clp, &uuid) || + !nfsd_uuid_is_local(&uuid, &net)) + goto unsupported; + break; + default: + goto unsupported; + } + + dprintk("%s: detected local server.\n", __func__); + nfs_local_enable(clp); + return; + +unsupported: + /* localio not supported */ + nfs_local_disable(clp); } EXPORT_SYMBOL_GPL(nfs_local_probe); @@ -264,7 +332,8 @@ nfs_local_open_fh(struct nfs_client *clp, const struct cred *cred, trace_nfs_local_open_fh(fh, mode, status); switch (status) { case -ENXIO: - nfs_local_disable(clp); + /* Revalidate localio, will disable if unsupported */ + nfs_local_probe(clp); fallthrough; case -ETIMEDOUT: status = -EAGAIN; diff --git a/fs/nfs/nfs3_fs.h b/fs/nfs/nfs3_fs.h index b333ea119ef5..efdf2b6519e9 100644 --- a/fs/nfs/nfs3_fs.h +++ b/fs/nfs/nfs3_fs.h @@ -30,6 +30,7 @@ static inline int nfs3_proc_setacls(struct inode *inode, struct posix_acl *acl, struct nfs_server *nfs3_create_server(struct fs_context *); struct nfs_server *nfs3_clone_server(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, rpc_authflavor_t); +void nfs3_init_localioclient(struct nfs_client *); /* nfs3super.c */ extern struct nfs_subversion nfs_v3; diff --git a/fs/nfs/nfs3client.c b/fs/nfs/nfs3client.c index b0c8a39c2bbd..123e7c1fd339 100644 --- a/fs/nfs/nfs3client.c +++ b/fs/nfs/nfs3client.c @@ -7,6 +7,8 @@ #include "netns.h" #include "sysfs.h" +#define NFSDBG_FACILITY NFSDBG_CLIENT + #ifdef CONFIG_NFS_V3_ACL static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program }; static const struct rpc_version *nfsacl_version[] = { @@ -130,3 +132,26 @@ struct nfs_client *nfs3_set_ds_client(struct nfs_server *mds_srv, return clp; } EXPORT_SYMBOL_GPL(nfs3_set_ds_client); + +#if defined(CONFIG_NFS_V3_LOCALIO) +static struct rpc_stat nfslocalio_rpcstat = { &nfslocalio_program3 }; +static const struct rpc_version *nfslocalio_version[] = { + [3] = &nfslocalio_version3, +}; + +const struct rpc_program nfslocalio_program3 = { + .name = "nfslocalio", + .number = NFS_LOCALIO_PROGRAM, + .nrvers = ARRAY_SIZE(nfslocalio_version), + .version = nfslocalio_version, + .stats = &nfslocalio_rpcstat, +}; + +/* + * Initialise an NFSv3 localio client connection + */ +void nfs3_init_localioclient(struct nfs_client *clp) +{ + nfs_init_localioclient(clp, &nfslocalio_program3, 3); +} +#endif /* CONFIG_NFS_V3_LOCALIO */ diff --git a/fs/nfs/nfs3proc.c b/fs/nfs/nfs3proc.c index 74bda639a7cf..40b6e4d1e7be 100644 --- a/fs/nfs/nfs3proc.c +++ b/fs/nfs/nfs3proc.c @@ -1067,4 +1067,7 @@ const struct nfs_rpc_ops nfs_v3_clientops = { .free_client = nfs_free_client, .create_server = nfs3_create_server, .clone_server = nfs3_clone_server, +#if defined(CONFIG_NFS_V3_LOCALIO) + .init_localioclient = nfs3_init_localioclient, +#endif }; diff --git a/fs/nfs/nfs3xdr.c b/fs/nfs/nfs3xdr.c index 60f032be805a..d2a17ecd12b8 100644 --- a/fs/nfs/nfs3xdr.c +++ b/fs/nfs/nfs3xdr.c @@ -2579,3 +2579,70 @@ const struct rpc_version nfsacl_version3 = { .counts = nfs3_acl_counts, }; #endif /* CONFIG_NFS_V3_ACL */ + +#if defined(CONFIG_NFS_V3_LOCALIO) + +#define LOCALIO3_getuuidres_sz (1+XDR_QUADLEN(UUID_SIZE)) + +static void nfs3_xdr_enc_getuuidargs(struct rpc_rqst *req, + struct xdr_stream *xdr, + const void *data) +{ + /* void function */ +} + +// FIXME: factor out from fs/nfs/nfs4xdr.c +static int decode_opaque_fixed(struct xdr_stream *xdr, void *buf, size_t len) +{ + ssize_t ret = xdr_stream_decode_opaque_fixed(xdr, buf, len); + if (unlikely(ret < 0)) + return -EIO; + return 0; +} + +static inline int nfs3_decode_getuuidresok(struct xdr_stream *xdr, + struct nfs_getuuidres *result) +{ + return decode_opaque_fixed(xdr, result->uuid, UUID_SIZE); +} + +static int nfs3_xdr_dec_getuuidres(struct rpc_rqst *req, + struct xdr_stream *xdr, + void *result) +{ + enum nfs_stat status; + int error; + + error = decode_nfsstat3(xdr, &status); + if (unlikely(error)) + goto out; + if (status != NFS3_OK) + goto out_default; + error = nfs3_decode_getuuidresok(xdr, result); +out: + return error; +out_default: + return nfs3_stat_to_errno(status); +} + +static const struct rpc_procinfo nfs3_localio_procedures[] = { + [LOCALIOPROC_GETUUID] = { + .p_proc = LOCALIOPROC_GETUUID, + .p_encode = nfs3_xdr_enc_getuuidargs, + .p_decode = nfs3_xdr_dec_getuuidres, + .p_arglen = 1, + .p_replen = LOCALIO3_getuuidres_sz, + .p_timer = 0, + .p_name = "GETUUID", + }, +}; + +static unsigned int nfs3_localio_counts[ARRAY_SIZE(nfs3_localio_procedures)]; +const struct rpc_version nfslocalio_version3 = { + .number = 3, + .nrprocs = ARRAY_SIZE(nfs3_localio_procedures), + .procs = nfs3_localio_procedures, + .counts = nfs3_localio_counts, +}; + +#endif /* CONFIG_NFS_V3_LOCALIO */ diff --git a/fs/nfs/nfs4_fs.h b/fs/nfs/nfs4_fs.h index 7024230f0d1d..a0a41917dec2 100644 --- a/fs/nfs/nfs4_fs.h +++ b/fs/nfs/nfs4_fs.h @@ -538,6 +538,8 @@ extern int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct extern const nfs4_stateid zero_stateid; extern const nfs4_stateid invalid_stateid; +extern void nfs4_init_localioclient(struct nfs_client *); + /* nfs4super.c */ struct nfs_mount_info; extern struct nfs_subversion nfs_v4; diff --git a/fs/nfs/nfs4client.c b/fs/nfs/nfs4client.c index 84573df5cf5a..d2f634aa1e1b 100644 --- a/fs/nfs/nfs4client.c +++ b/fs/nfs/nfs4client.c @@ -1384,3 +1384,26 @@ int nfs4_update_server(struct nfs_server *server, const char *hostname, return nfs_probe_server(server, NFS_FH(d_inode(server->super->s_root))); } + +#if defined(CONFIG_NFS_V4_LOCALIO) +static struct rpc_stat nfslocalio_rpcstat = { &nfslocalio_program4 }; +static const struct rpc_version *nfslocalio_version[] = { + [4] = &nfslocalio_version4, +}; + +const struct rpc_program nfslocalio_program4 = { + .name = "nfslocalio", + .number = NFS_LOCALIO_PROGRAM, + .nrvers = ARRAY_SIZE(nfslocalio_version), + .version = nfslocalio_version, + .stats = &nfslocalio_rpcstat, +}; + +/* + * Initialise an NFSv4 localio client connection + */ +void nfs4_init_localioclient(struct nfs_client *clp) +{ + nfs_init_localioclient(clp, &nfslocalio_program4, 4); +} +#endif /* CONFIG_NFS_V4_LOCALIO */ diff --git a/fs/nfs/nfs4proc.c b/fs/nfs/nfs4proc.c index c93c12063b3a..060bc8dbee61 100644 --- a/fs/nfs/nfs4proc.c +++ b/fs/nfs/nfs4proc.c @@ -10745,6 +10745,9 @@ const struct nfs_rpc_ops nfs_v4_clientops = { .discover_trunking = nfs4_discover_trunking, .enable_swap = nfs4_enable_swap, .disable_swap = nfs4_disable_swap, +#if defined(CONFIG_NFS_V4_LOCALIO) + .init_localioclient = nfs4_init_localioclient, +#endif }; static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = { diff --git a/fs/nfs/nfs4xdr.c b/fs/nfs/nfs4xdr.c index 1416099dfcd1..d3b4fa3245f0 100644 --- a/fs/nfs/nfs4xdr.c +++ b/fs/nfs/nfs4xdr.c @@ -7728,3 +7728,55 @@ const struct rpc_version nfs_version4 = { .procs = nfs4_procedures, .counts = nfs_version4_counts, }; + +#if defined(CONFIG_NFS_V4_LOCALIO) + +#define LOCALIO4_getuuidres_sz (op_decode_hdr_maxsz+XDR_QUADLEN(UUID_SIZE)) + +static void nfs4_xdr_enc_getuuidargs(struct rpc_rqst *req, + struct xdr_stream *xdr, + const void *data) +{ + /* void function */ +} + +static inline int nfs4_decode_getuuidresok(struct xdr_stream *xdr, + struct nfs_getuuidres *result) +{ + return decode_opaque_fixed(xdr, result->uuid, UUID_SIZE); +} + +static int nfs4_xdr_dec_getuuidres(struct rpc_rqst *req, + struct xdr_stream *xdr, + void *result) +{ + // FIXME: need proper handling that isn't abusing nfs_opnum4 + int error = decode_op_hdr(xdr, LOCALIOPROC_GETUUID); + if (unlikely(error)) + goto out; + error = nfs4_decode_getuuidresok(xdr, result); +out: + return error; +} + +static const struct rpc_procinfo nfs4_localio_procedures[] = { + [LOCALIOPROC_GETUUID] = { + .p_proc = LOCALIOPROC_GETUUID, + .p_encode = nfs4_xdr_enc_getuuidargs, + .p_decode = nfs4_xdr_dec_getuuidres, + .p_arglen = 1, + .p_replen = LOCALIO4_getuuidres_sz, + .p_statidx = LOCALIOPROC_GETUUID, + .p_name = "GETUUID", + }, +}; + +static unsigned int nfs4_localio_counts[ARRAY_SIZE(nfs4_localio_procedures)]; +const struct rpc_version nfslocalio_version4 = { + .number = 4, + .nrprocs = ARRAY_SIZE(nfs4_localio_procedures), + .procs = nfs4_localio_procedures, + .counts = nfs4_localio_counts, +}; + +#endif /* CONFIG_NFS_V4_LOCALIO */ diff --git a/include/linux/nfs_fs_sb.h b/include/linux/nfs_fs_sb.h index 00fe469bc72e..efcdb4d8e9de 100644 --- a/include/linux/nfs_fs_sb.h +++ b/include/linux/nfs_fs_sb.h @@ -130,6 +130,7 @@ struct nfs_client { /* localio */ struct timespec64 cl_nfssvc_boot; seqlock_t cl_boot_lock; + struct rpc_clnt * cl_rpcclient_localio; /* localio RPC client handle */ }; /* diff --git a/include/linux/nfs_xdr.h b/include/linux/nfs_xdr.h index 764513a61601..2a438f4c2d6d 100644 --- a/include/linux/nfs_xdr.h +++ b/include/linux/nfs_xdr.h @@ -1002,6 +1002,10 @@ struct nfs3_getaclres { struct posix_acl * acl_default; }; +struct nfs_getuuidres { + __u8 * uuid; +}; + #if IS_ENABLED(CONFIG_NFS_V4) typedef u64 clientid4; @@ -1819,6 +1823,7 @@ struct nfs_rpc_ops { int (*discover_trunking)(struct nfs_server *, struct nfs_fh *); void (*enable_swap)(struct inode *inode); void (*disable_swap)(struct inode *inode); + void (*init_localioclient)(struct nfs_client *); }; /* @@ -1834,4 +1839,9 @@ extern const struct rpc_version nfs_version4; extern const struct rpc_version nfsacl_version3; extern const struct rpc_program nfsacl_program; +extern const struct rpc_version nfslocalio_version3; +extern const struct rpc_program nfslocalio_program3; +extern const struct rpc_version nfslocalio_version4; +extern const struct rpc_program nfslocalio_program4; + #endif diff --git a/include/uapi/linux/nfs.h b/include/uapi/linux/nfs.h index f356f2ba3814..e72f5564bdc0 100644 --- a/include/uapi/linux/nfs.h +++ b/include/uapi/linux/nfs.h @@ -33,6 +33,10 @@ #define NFS_MNT_VERSION 1 #define NFS_MNT3_VERSION 3 +#define NFS_LOCALIO_PROGRAM 100229 +#define LOCALIOPROC_NULL 0 +#define LOCALIOPROC_GETUUID 1 + #define NFS_PIPE_DIRNAME "nfs" /* From patchwork Fri Jun 14 03:44:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Snitzer X-Patchwork-Id: 13697843 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0CC39146D40 for ; Fri, 14 Jun 2024 03:44:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336683; cv=none; b=KLHMrTtjqxhQIPL1U/mVofG5bqa7TxQg1PAIiy2qZd1yCz7fhQLWiR7Ej4xPWKCvEo13G8u981ERgChZ+KoTwVfRSNf6CNMFG6GOb+ERnPGZC1q1U+ETLTxVEZGkwyo46EkfjVm4xLI0wi97BGQQLyBTlk7+GWmVDoSMhRUINGk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336683; c=relaxed/simple; bh=09lWm/AKX2LDeCYV9fqxg3IzOsPq+5UtqFtqeVCjbuY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ob5Tif/qGrKRVEhVCaIzONIMIqo4jjOVo6CMLI5CFPIAbKukcogaSB4vE8OebsJ0aR1OmNik7F1ofqR2Bwa2GpfFUABshHmskkLtosuJosXa5RzEAZq2JUt0+w085duHFye7mLKqX0Fee3NN5Zw1Pn3ZsfofjSTv6mfkgxTf3oM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=WE4s2aVc; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="WE4s2aVc" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A4FB6C3277B; Fri, 14 Jun 2024 03:44:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718336682; bh=09lWm/AKX2LDeCYV9fqxg3IzOsPq+5UtqFtqeVCjbuY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WE4s2aVci5zzR4ModKphhai9goWLZDjZti8n0A12axU9jwId2AC+1q+N2QkgbHmgv vb/IiOruGoKHkF62nRLrNiOZ5eZiOnYFVA0WfDtyynHJ52NSUqVAhWahCGGcAmkKeB 9EgBFMO0uDmtWImBK38UVGSU4wYU/5XjBovamHF8YVIIfUmhaP2GWiI1VFhfR2GRJt P7UqArusW5v0fTfzYsH/yAP/a8RqDN1cJ7hdBF/WVqxZqA1ZDHo2gvqdT2+72pkPKG 7K1KfsJwMXwrW7EyfX+LC8v3+1iZT9u7XykRd5Ot5mQ3DXpwF0GQR9+qLuPtyYdEiB gTlrVzJdJcYXA== From: Mike Snitzer To: linux-nfs@vger.kernel.org Cc: Jeff Layton , Chuck Lever , Trond Myklebust , NeilBrown , snitzer@hammerspace.com Subject: [PATCH v3 11/18] nfsd: implement v3 and v4 server support for NFS_LOCALIO_PROGRAM Date: Thu, 13 Jun 2024 23:44:19 -0400 Message-ID: <20240614034426.31043-12-snitzer@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240614034426.31043-1-snitzer@kernel.org> References: <20240614034426.31043-1-snitzer@kernel.org> Precedence: bulk X-Mailing-List: linux-nfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 LOCALIOPROC_GETUUID encodes the server's uuid_t in terms of the fixed UUID_SIZE (16). The fixed size opaque encode and decode XDR methods are used instead of the less efficient variable sized methods. Signed-off-by: Mike Snitzer --- fs/nfsd/localio.c | 148 ++++++++++++++++++++++++++++++++++++++++++++++ fs/nfsd/nfsd.h | 11 ++++ fs/nfsd/nfssvc.c | 80 ++++++++++++++++++++++++- fs/nfsd/xdr.h | 6 ++ 4 files changed, 244 insertions(+), 1 deletion(-) diff --git a/fs/nfsd/localio.c b/fs/nfsd/localio.c index 6e2918e76f49..bb84e165dbe1 100644 --- a/fs/nfsd/localio.c +++ b/fs/nfsd/localio.c @@ -17,6 +17,9 @@ #include "vfs.h" #include "netns.h" #include "filecache.h" +#include "cache.h" +#include "xdr3.h" +#include "xdr4.h" #define NFSDDBG_FACILITY NFSDDBG_FH @@ -241,3 +244,148 @@ EXPORT_SYMBOL_GPL(nfsd_open_local_fh); /* Compile time type checking, not used by anything */ static nfs_to_nfsd_open_t __maybe_unused nfsd_open_local_fh_typecheck = nfsd_open_local_fh; + +/* + * GETUUID XDR encode functions + */ + +static __be32 nfsd_proc_null(struct svc_rqst *rqstp) +{ + return rpc_success; +} + +static __be32 nfsd_proc_getuuid(struct svc_rqst *rqstp) +{ + struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); + struct nfsd_getuuidres *resp = rqstp->rq_resp; + + uuid_copy(&resp->uuid, &nn->nfsd_uuid.uuid); + resp->status = nfs_ok; + + return rpc_success; +} + +#define NFS_getuuid_sz XDR_QUADLEN(UUID_SIZE) + +static inline void encode_opaque_fixed(struct xdr_stream *xdr, const void *buf, size_t len) +{ + WARN_ON_ONCE(xdr_stream_encode_opaque_fixed(xdr, buf, len) < 0); +} + +static void encode_uuid(struct xdr_stream *xdr, uuid_t *src_uuid) +{ + u8 uuid[UUID_SIZE]; + + export_uuid(uuid, src_uuid); + encode_opaque_fixed(xdr, uuid, UUID_SIZE); + dprintk("%s: uuid=%pU\n", __func__, uuid); +} + +#if defined(CONFIG_NFSD_V3_LOCALIO) +static bool nfs3svc_encode_getuuidres(struct svc_rqst *rqstp, + struct xdr_stream *xdr) +{ + struct nfsd_getuuidres *resp = rqstp->rq_resp; + + if (!svcxdr_encode_nfsstat3(xdr, resp->status)) + return false; + if (resp->status == nfs_ok) + encode_uuid(xdr, &resp->uuid); + + return true; +} + +#define ST 1 /* status */ +#define NFS3_filename_sz (1+(NFS3_MAXNAMLEN>>2)) + +static const struct svc_procedure nfsd_localio_procedures3[2] = { + [LOCALIOPROC_NULL] = { + .pc_func = nfsd_proc_null, + .pc_decode = nfssvc_decode_voidarg, + .pc_encode = nfssvc_encode_voidres, + .pc_argsize = sizeof(struct nfsd_voidargs), + .pc_ressize = sizeof(struct nfsd_voidres), + .pc_cachetype = RC_NOCACHE, + .pc_xdrressize = 1, + .pc_name = "NULL", + }, + [LOCALIOPROC_GETUUID] = { + .pc_func = nfsd_proc_getuuid, + .pc_decode = nfssvc_decode_voidarg, + .pc_encode = nfs3svc_encode_getuuidres, + .pc_argsize = sizeof(struct nfsd_voidargs), + .pc_ressize = sizeof(struct nfsd_getuuidres), + .pc_cachetype = RC_NOCACHE, + .pc_xdrressize = 1+NFS_getuuid_sz, + .pc_name = "GETUUID", + }, +}; + +static DEFINE_PER_CPU_ALIGNED(unsigned long, + nfsd_localio_count3[ARRAY_SIZE(nfsd_localio_procedures3)]); +const struct svc_version nfsd_localio_version3 = { + .vs_vers = 3, + .vs_nproc = 2, + .vs_proc = nfsd_localio_procedures3, + .vs_dispatch = nfsd_dispatch, + .vs_count = nfsd_localio_count3, + .vs_xdrsize = NFS3_SVC_XDRSIZE, +}; +#endif /* CONFIG_NFSD_V3_LOCALIO */ + +#if defined(CONFIG_NFSD_V4_LOCALIO) +static bool nfs4svc_encode_getuuidres(struct svc_rqst *rqstp, + struct xdr_stream *xdr) +{ + struct nfsd_getuuidres *resp = rqstp->rq_resp; + __be32 *p; + + p = xdr_reserve_space(xdr, 8); + if (!p) + return 0; + *p++ = cpu_to_be32(LOCALIOPROC_GETUUID); + *p++ = resp->status; + + if (resp->status == nfs_ok) + encode_uuid(xdr, &resp->uuid); + + return 1; +} + +static const struct svc_procedure nfsd_localio_procedures4[2] = { + [LOCALIOPROC_NULL] = { + .pc_func = nfsd_proc_null, + .pc_decode = nfssvc_decode_voidarg, + .pc_encode = nfssvc_encode_voidres, + .pc_argsize = sizeof(struct nfsd_voidargs), + .pc_ressize = sizeof(struct nfsd_voidres), + .pc_cachetype = RC_NOCACHE, + .pc_xdrressize = 1, + .pc_name = "NULL", + }, + [LOCALIOPROC_GETUUID] = { + .pc_func = nfsd_proc_getuuid, + .pc_decode = nfssvc_decode_voidarg, + .pc_encode = nfs4svc_encode_getuuidres, + .pc_argsize = sizeof(struct nfsd_voidargs), + .pc_ressize = sizeof(struct nfsd_getuuidres), + .pc_cachetype = RC_NOCACHE, + .pc_xdrressize = 2+NFS_getuuid_sz, + .pc_name = "GETUUID", + }, +}; + +static DEFINE_PER_CPU_ALIGNED(unsigned long, + nfsd_localio_count4[ARRAY_SIZE(nfsd_localio_procedures4)]); +const struct svc_version nfsd_localio_version4 = { + .vs_vers = 4, + .vs_nproc = 2, + .vs_proc = nfsd_localio_procedures4, + .vs_dispatch = nfsd_dispatch, + .vs_count = nfsd_localio_count4, + .vs_xdrsize = NFS4_SVC_XDRSIZE, + .vs_rpcb_optnl = true, + .vs_need_cong_ctrl = true, + +}; +#endif /* CONFIG_NFSD_V4_LOCALIO */ diff --git a/fs/nfsd/nfsd.h b/fs/nfsd/nfsd.h index 8f4f239d9f8a..dd225330837f 100644 --- a/fs/nfsd/nfsd.h +++ b/fs/nfsd/nfsd.h @@ -142,6 +142,17 @@ extern const struct svc_version nfsd_acl_version3; #endif #endif +#if defined(CONFIG_NFSD_V3_LOCALIO) +extern const struct svc_version nfsd_localio_version3; +#else +#define nfsd_localio_version3 NULL +#endif +#if defined(CONFIG_NFSD_V4_LOCALIO) +extern const struct svc_version nfsd_localio_version4; +#else +#define nfsd_localio_version4 NULL +#endif + struct nfsd_net; enum vers_op {NFSD_SET, NFSD_CLEAR, NFSD_TEST, NFSD_AVAIL }; diff --git a/fs/nfsd/nfssvc.c b/fs/nfsd/nfssvc.c index 7383dc10611c..0f2f257be94f 100644 --- a/fs/nfsd/nfssvc.c +++ b/fs/nfsd/nfssvc.c @@ -38,6 +38,16 @@ atomic_t nfsd_th_cnt = ATOMIC_INIT(0); extern struct svc_program nfsd_program; static int nfsd(void *vrqstp); +#if IS_ENABLED(CONFIG_NFSD_LOCALIO) +static int nfsd_localio_rpcbind_set(struct net *, + const struct svc_program *, + u32, int, + unsigned short, + unsigned short); +static __be32 nfsd_localio_init_request(struct svc_rqst *, + const struct svc_program *, + struct svc_process_info *); +#endif /* CONFIG_NFSD_LOCALIO */ #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) static int nfsd_acl_rpcbind_set(struct net *, const struct svc_program *, @@ -81,6 +91,31 @@ DEFINE_SPINLOCK(nfsd_drc_lock); unsigned long nfsd_drc_max_mem; unsigned long nfsd_drc_mem_used; +#if IS_ENABLED(CONFIG_NFSD_LOCALIO) +static const struct svc_version *nfsd_localio_version[] = { +#if defined(CONFIG_NFSD_V3_LOCALIO) + [3] = &nfsd_localio_version3, +#endif +#if defined(CONFIG_NFSD_V4_LOCALIO) + [4] = &nfsd_localio_version4, +#endif +}; + +#define NFSD_LOCALIO_MINVERS 3 +#define NFSD_LOCALIO_NRVERS ARRAY_SIZE(nfsd_localio_version) + +static struct svc_program nfsd_localio_program = { + .pg_prog = NFS_LOCALIO_PROGRAM, + .pg_nvers = NFSD_LOCALIO_NRVERS, + .pg_vers = nfsd_localio_version, + .pg_name = "nfslocalio", + .pg_class = "nfsd", + .pg_authenticate = &svc_set_client, + .pg_init_request = nfsd_localio_init_request, + .pg_rpcbind_set = nfsd_localio_rpcbind_set, +}; +#endif /* CONFIG_NFSD_LOCALIO */ + #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) static const struct svc_version *nfsd_acl_version[] = { # if defined(CONFIG_NFSD_V2_ACL) @@ -95,6 +130,9 @@ static const struct svc_version *nfsd_acl_version[] = { #define NFSD_ACL_NRVERS ARRAY_SIZE(nfsd_acl_version) static struct svc_program nfsd_acl_program = { +#if IS_ENABLED(CONFIG_NFSD_LOCALIO) + .pg_next = &nfsd_localio_program, +#endif /* CONFIG_NFSD_LOCALIO */ .pg_prog = NFS_ACL_PROGRAM, .pg_nvers = NFSD_ACL_NRVERS, .pg_vers = nfsd_acl_version, @@ -123,6 +161,10 @@ static const struct svc_version *nfsd_version[] = { struct svc_program nfsd_program = { #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) .pg_next = &nfsd_acl_program, +#else +#if IS_ENABLED(CONFIG_NFSD_LOCALIO) + .pg_next = &nfsd_localio_program, +#endif /* CONFIG_NFSD_LOCALIO */ #endif .pg_prog = NFS_PROGRAM, /* program number */ .pg_nvers = NFSD_NRVERS, /* nr of entries in nfsd_version */ @@ -817,6 +859,42 @@ nfsd_svc(int nrservs, struct net *net, const struct cred *cred, const char *scop return error; } +#if IS_ENABLED(CONFIG_NFSD_LOCALIO) +static bool +nfsd_support_localio_version(int vers) +{ + if (vers >= NFSD_LOCALIO_MINVERS && vers < NFSD_LOCALIO_NRVERS) + return nfsd_localio_version[vers] != NULL; + return false; +} + +static int +nfsd_localio_rpcbind_set(struct net *net, const struct svc_program *progp, + u32 version, int family, unsigned short proto, + unsigned short port) +{ + if (!nfsd_support_localio_version(version) || + !nfsd_vers(net_generic(net, nfsd_net_id), version, NFSD_TEST)) + return 0; + return svc_generic_rpcbind_set(net, progp, version, family, + proto, port); +} + +static __be32 +nfsd_localio_init_request(struct svc_rqst *rqstp, + const struct svc_program *progp, + struct svc_process_info *ret) +{ + struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); + + if (likely(nfsd_support_localio_version(rqstp->rq_vers) && + nfsd_vers(nn, rqstp->rq_vers, NFSD_TEST))) + return svc_generic_init_request(rqstp, progp, ret); + + return rpc_prog_unavail; +} +#endif /* CONFIG_NFSD_LOCALIO */ + #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) static bool nfsd_support_acl_version(int vers) @@ -959,7 +1037,7 @@ nfsd(void *vrqstp) } /** - * nfsd_dispatch - Process an NFS or NFSACL Request + * nfsd_dispatch - Process an NFS or NFSACL or NFSLOCALIO Request * @rqstp: incoming request * * This RPC dispatcher integrates the NFS server's duplicate reply cache. diff --git a/fs/nfsd/xdr.h b/fs/nfsd/xdr.h index 852f71580bd0..5714469af597 100644 --- a/fs/nfsd/xdr.h +++ b/fs/nfsd/xdr.h @@ -5,6 +5,7 @@ #define LINUX_NFSD_H #include +#include #include "nfsd.h" #include "nfsfh.h" @@ -123,6 +124,11 @@ struct nfsd_statfsres { struct kstatfs stats; }; +struct nfsd_getuuidres { + __be32 status; + uuid_t uuid; +}; + /* * Storage requirements for XDR arguments and results. */ From patchwork Fri Jun 14 03:44:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Snitzer X-Patchwork-Id: 13697844 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 534FF146D40 for ; Fri, 14 Jun 2024 03:44:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336684; cv=none; b=BUc+VwsxRxoKpgCFUEZBGoWPxHJOpKqqf0WA7n7H7g9qbNy54Gr0PgSF0SEfa1cPev+ckgiJJDGPZPSOKSm4SdGq9sAUif4yrYbNPOgjkKT8KzFFG33ajy5YUcjaTVIAnbC/jo9PIMR1U5TyJVOdIXKKI4F9AvwNFP7vliZXWPA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336684; c=relaxed/simple; bh=abK5oPUtz+Jhmhx7HxELuk/43QgqF1f5Zo34E08TUKA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XIcgYsQzMCd22xvmDXQ8jg55xo8ybDySQp/QEgNjP9MLW/Rr2lCc5yCJJkFOcjUW/OdfC1MTY+VfKXTOdYotd9fT7+vHNxREPzkGEGoPFJnbDFSdszik8T2wO8i4F+sWaY/TYheE8p0UE2jYIigKKpq/MjoelYgpQZOudrR5Iws= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=O1oLn+72; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="O1oLn+72" Received: by smtp.kernel.org (Postfix) with ESMTPSA id F2D23C2BBFC; Fri, 14 Jun 2024 03:44:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718336684; bh=abK5oPUtz+Jhmhx7HxELuk/43QgqF1f5Zo34E08TUKA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=O1oLn+72qpMYdrKfTEAqc2QyXjz4WQOGuF0z/wxMuEXLD/Qk3SjG5ySXb05ci+5ef X3V/55Xo2jr/SyHS+z39GMYj4EYH9jbRrNpe4koF9E2V0bRZRwkBhi4YNVovJk3Ky9 O/iBfCgU+RWf8lYDcR9592+x96i6/fNu49XGjByQ9tnOCl7NEWgS0X8OEmZYxkg+bc Egi8UvEL0ooW0N5T4yKd0rvYLvJRk1hWRwCCGcuchbWd16eTn8NVohpO0zd9Y5rNER GzCexdIUiD3ANZkuudGLGuo0qCtmVOTHL4IORSqRHu6wy9/o83mhODcgbMobIZApKn iEiMwaMnyyvvA== From: Mike Snitzer To: linux-nfs@vger.kernel.org Cc: Jeff Layton , Chuck Lever , Trond Myklebust , NeilBrown , snitzer@hammerspace.com Subject: [PATCH v3 12/18] nfs/nfsd: consolidate {encode,decode}_opaque_fixed in nfs_xdr.h Date: Thu, 13 Jun 2024 23:44:20 -0400 Message-ID: <20240614034426.31043-13-snitzer@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240614034426.31043-1-snitzer@kernel.org> References: <20240614034426.31043-1-snitzer@kernel.org> Precedence: bulk X-Mailing-List: linux-nfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Eliminates duplicate functions in various files. Signed-off-by: Mike Snitzer --- fs/nfs/flexfilelayout/flexfilelayout.c | 6 ------ fs/nfs/nfs3xdr.c | 9 --------- fs/nfs/nfs4xdr.c | 13 ------------- fs/nfsd/localio.c | 7 ++----- include/linux/nfs_xdr.h | 20 +++++++++++++++++++- 5 files changed, 21 insertions(+), 34 deletions(-) diff --git a/fs/nfs/flexfilelayout/flexfilelayout.c b/fs/nfs/flexfilelayout/flexfilelayout.c index ec6aaa110a7b..8b9096ad0663 100644 --- a/fs/nfs/flexfilelayout/flexfilelayout.c +++ b/fs/nfs/flexfilelayout/flexfilelayout.c @@ -2185,12 +2185,6 @@ static int ff_layout_encode_ioerr(struct xdr_stream *xdr, return ff_layout_encode_ds_ioerr(xdr, &ff_args->errors); } -static void -encode_opaque_fixed(struct xdr_stream *xdr, const void *buf, size_t len) -{ - WARN_ON_ONCE(xdr_stream_encode_opaque_fixed(xdr, buf, len) < 0); -} - static void ff_layout_encode_ff_iostat_head(struct xdr_stream *xdr, const nfs4_stateid *stateid, diff --git a/fs/nfs/nfs3xdr.c b/fs/nfs/nfs3xdr.c index d2a17ecd12b8..95a2fb0733ae 100644 --- a/fs/nfs/nfs3xdr.c +++ b/fs/nfs/nfs3xdr.c @@ -2591,15 +2591,6 @@ static void nfs3_xdr_enc_getuuidargs(struct rpc_rqst *req, /* void function */ } -// FIXME: factor out from fs/nfs/nfs4xdr.c -static int decode_opaque_fixed(struct xdr_stream *xdr, void *buf, size_t len) -{ - ssize_t ret = xdr_stream_decode_opaque_fixed(xdr, buf, len); - if (unlikely(ret < 0)) - return -EIO; - return 0; -} - static inline int nfs3_decode_getuuidresok(struct xdr_stream *xdr, struct nfs_getuuidres *result) { diff --git a/fs/nfs/nfs4xdr.c b/fs/nfs/nfs4xdr.c index d3b4fa3245f0..6b35b1d7d7ce 100644 --- a/fs/nfs/nfs4xdr.c +++ b/fs/nfs/nfs4xdr.c @@ -968,11 +968,6 @@ static __be32 *reserve_space(struct xdr_stream *xdr, size_t nbytes) return p; } -static void encode_opaque_fixed(struct xdr_stream *xdr, const void *buf, size_t len) -{ - WARN_ON_ONCE(xdr_stream_encode_opaque_fixed(xdr, buf, len) < 0); -} - static void encode_string(struct xdr_stream *xdr, unsigned int len, const char *str) { WARN_ON_ONCE(xdr_stream_encode_opaque(xdr, str, len) < 0); @@ -4352,14 +4347,6 @@ static int decode_access(struct xdr_stream *xdr, u32 *supported, u32 *access) return 0; } -static int decode_opaque_fixed(struct xdr_stream *xdr, void *buf, size_t len) -{ - ssize_t ret = xdr_stream_decode_opaque_fixed(xdr, buf, len); - if (unlikely(ret < 0)) - return -EIO; - return 0; -} - static int decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid) { return decode_opaque_fixed(xdr, stateid, NFS4_STATEID_SIZE); diff --git a/fs/nfsd/localio.c b/fs/nfsd/localio.c index bb84e165dbe1..7ecd72406dc0 100644 --- a/fs/nfsd/localio.c +++ b/fs/nfsd/localio.c @@ -11,6 +11,8 @@ #include #include #include +#include +#include #include #include "nfsd.h" @@ -267,11 +269,6 @@ static __be32 nfsd_proc_getuuid(struct svc_rqst *rqstp) #define NFS_getuuid_sz XDR_QUADLEN(UUID_SIZE) -static inline void encode_opaque_fixed(struct xdr_stream *xdr, const void *buf, size_t len) -{ - WARN_ON_ONCE(xdr_stream_encode_opaque_fixed(xdr, buf, len) < 0); -} - static void encode_uuid(struct xdr_stream *xdr, uuid_t *src_uuid) { u8 uuid[UUID_SIZE]; diff --git a/include/linux/nfs_xdr.h b/include/linux/nfs_xdr.h index 2a438f4c2d6d..daa4115f6be6 100644 --- a/include/linux/nfs_xdr.h +++ b/include/linux/nfs_xdr.h @@ -1826,6 +1826,24 @@ struct nfs_rpc_ops { void (*init_localioclient)(struct nfs_client *); }; +/* + * Helper functions used by NFS client and/or server + */ +static inline void encode_opaque_fixed(struct xdr_stream *xdr, + const void *buf, size_t len) +{ + WARN_ON_ONCE(xdr_stream_encode_opaque_fixed(xdr, buf, len) < 0); +} + +static inline int decode_opaque_fixed(struct xdr_stream *xdr, + void *buf, size_t len) +{ + ssize_t ret = xdr_stream_decode_opaque_fixed(xdr, buf, len); + if (unlikely(ret < 0)) + return -EIO; + return 0; +} + /* * Function vectors etc. for the NFS client */ @@ -1844,4 +1862,4 @@ extern const struct rpc_program nfslocalio_program3; extern const struct rpc_version nfslocalio_version4; extern const struct rpc_program nfslocalio_program4; -#endif +#endif /* _LINUX_NFS_XDR_H */ From patchwork Fri Jun 14 03:44:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Snitzer X-Patchwork-Id: 13697845 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9A66B146D40 for ; Fri, 14 Jun 2024 03:44:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336685; cv=none; b=G302se9xD+YdezkWOqH0e/JTfsbhMWURL+7Cro1YJEwEbXRHouX0B8iPqvhbzDMPzsHW5e+zsLIB200JxaXrre9OAuhZgedv9A23aC0oIBGUAeYXJbAWqpGGNPbFLMA4kx0YPUntRwRSBMVx3xtVhy7yiAcmpjM9uYqFV3YgXSg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336685; c=relaxed/simple; bh=UZeOEebiIezT/DZSMDGmcPSiS9nrCROrZLz2RR5qD7o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sanwEQEgOqiUZSymB8X7FRmXbCFb34F8AuzZhTVH+coV7vY/sE/SQP45/O2Jqp7dDw7p9PBBPnL0Qf20oRocdEJvoGSCRiYaFj5CK546nhuByiryGvDq8xGcLMhNmpse+979EvwFze0amEwe7b1ANMcC9Olvy9wu1xv65hDXPo8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EBdWKoCM; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="EBdWKoCM" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4A2DEC2BBFC; Fri, 14 Jun 2024 03:44:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718336685; bh=UZeOEebiIezT/DZSMDGmcPSiS9nrCROrZLz2RR5qD7o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EBdWKoCMPO6/TAvqo9RX9MTsvbmG/0d3MVPq/WTFARBG2I8212eb9NDU/mQBAXbbO BwDYtHLLwZAVCoKGPNqPtKlfvVpaBOcZi02qwlWJ+pgWz70AnJ/ykj4w7Rc3nFU2gy XBlyJim567TURoxXMX7bcLwJbfP4S3UEYQUmsEOXNPz2V7Y4TGebd1IApkbOYdqpUn L+r/zYD47c6oeK9T+gq0OTgjizikDW9+T4X91Q1G6e0Cdbai92W6Is7xqGOWTMRQrD T/1S+fuKiNZ0MTYO8xV0SCBA3DmHeeImUzAtNpaADnysMVNvDEDUYhEC4azTH7UJEf v76c8pSss8Qog== From: Mike Snitzer To: linux-nfs@vger.kernel.org Cc: Jeff Layton , Chuck Lever , Trond Myklebust , NeilBrown , snitzer@hammerspace.com Subject: [PATCH v3 13/18] nfs/localio: move managing nfsd_open_local_fh symbol to nfs_common Date: Thu, 13 Jun 2024 23:44:21 -0400 Message-ID: <20240614034426.31043-14-snitzer@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240614034426.31043-1-snitzer@kernel.org> References: <20240614034426.31043-1-snitzer@kernel.org> Precedence: bulk X-Mailing-List: linux-nfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Get nfsd_open_local_fh and store it in rpc_client during client creation, put the symbol during nfs_local_disable -- which is also called during client destruction. Eliminates the need for nfs_local_open_ctx and extra locking and refcounting work in fs/nfs/localio.c Also makes it so the reference to the nfsd_open_local_fh symbol is managed by the nfs_common module instead of the nfs client modules. Signed-off-by: Mike Snitzer --- fs/nfs/client.c | 1 + fs/nfs/inode.c | 1 - fs/nfs/internal.h | 15 ++++--- fs/nfs/localio.c | 86 +++----------------------------------- fs/nfs_common/nfslocalio.c | 26 ++++++++++++ include/linux/nfs.h | 4 -- include/linux/nfs_fs_sb.h | 2 + include/linux/nfslocalio.h | 8 ++++ 8 files changed, 52 insertions(+), 91 deletions(-) diff --git a/fs/nfs/client.c b/fs/nfs/client.c index 7044b8b3b332..cbabcdf3d785 100644 --- a/fs/nfs/client.c +++ b/fs/nfs/client.c @@ -171,6 +171,7 @@ struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init) INIT_LIST_HEAD(&clp->cl_superblocks); clp->cl_rpcclient = clp->cl_rpcclient_localio = ERR_PTR(-EINVAL); + clp->nfsd_open_local_fh = NULL; clp->cl_flags = cl_init->init_flags; clp->cl_proto = cl_init->proto; diff --git a/fs/nfs/inode.c b/fs/nfs/inode.c index 58be75294a23..aac8c5302503 100644 --- a/fs/nfs/inode.c +++ b/fs/nfs/inode.c @@ -2518,7 +2518,6 @@ static int __init init_nfs_fs(void) if (err) goto out1; - nfs_local_init(); err = register_nfs_fs(); if (err) goto out0; diff --git a/fs/nfs/internal.h b/fs/nfs/internal.h index aed826cdb7a7..1c82f764cffa 100644 --- a/fs/nfs/internal.h +++ b/fs/nfs/internal.h @@ -472,15 +472,21 @@ nfs_init_localioclient(struct nfs_client *clp, if (IS_ERR(clp->cl_rpcclient_localio)) goto out; /* No errors! Assume that localio is supported */ + clp->nfsd_open_local_fh = get_nfsd_open_local_fh(); + if (!clp->nfsd_open_local_fh) { + rpc_shutdown_client(clp->cl_rpcclient_localio); + clp->cl_rpcclient_localio = ERR_PTR(-EINVAL); + goto out; + } supported = true; out: - dfprintk_rcu(CLIENT, "%s: server (%s) %s NFS v%u LOCALIO\n", __func__, - rpc_peeraddr2str(clp->cl_rpcclient_localio, RPC_DISPLAY_ADDR), - (supported ? "supports" : "does not support"), vers); + dfprintk_rcu(CLIENT, "%s: server (%s) %s NFS v%u LOCALIO, nfsd_open_local_fh is %s.\n", + __func__, rpc_peeraddr2str(clp->cl_rpcclient_localio, RPC_DISPLAY_ADDR), + (supported ? "supports" : "does not support"), vers, + (clp->nfsd_open_local_fh ? "set" : "not set")); } /* localio.c */ -extern void nfs_local_init(void); extern void nfs_local_disable(struct nfs_client *); extern void nfs_local_probe(struct nfs_client *); extern struct file *nfs_local_open_fh(struct nfs_client *, const struct cred *, @@ -497,7 +503,6 @@ extern int nfs_local_commit(struct file *, struct nfs_commit_data *, extern bool nfs_server_is_local(const struct nfs_client *clp); #else -static inline void nfs_local_init(void) {} static inline void nfs_local_disable(struct nfs_client *clp) {} static inline void nfs_local_probe(struct nfs_client *clp) {} static inline struct file *nfs_local_open_fh(struct nfs_client *clp, diff --git a/fs/nfs/localio.c b/fs/nfs/localio.c index c6ad31afe6a1..6c67d83427c0 100644 --- a/fs/nfs/localio.c +++ b/fs/nfs/localio.c @@ -29,26 +29,6 @@ #define NFSDBG_FACILITY NFSDBG_VFS -extern int nfsd_open_local_fh(struct rpc_clnt *rpc_clnt, - const struct cred *cred, - const struct nfs_fh *nfs_fh, const fmode_t fmode, - struct file **pfilp); -/* - * The localio code needs to call into nfsd to do the filehandle -> struct path - * mapping, but cannot be statically linked, because that will make the nfs - * module depend on the nfsd module. - * - * Instead, do dynamic linking to the nfsd module. This way the nfs module - * will only hold a reference on nfsd when it's actually in use. This also - * allows some sanity checking, like giving up on localio if nfsd isn't loaded. - */ - -struct nfs_local_open_ctx { - spinlock_t lock; - nfs_to_nfsd_open_t open_f; - atomic_t refcount; -}; - struct nfs_local_kiocb { struct kiocb kiocb; struct bio_vec *bvec; @@ -141,8 +121,6 @@ nfs4errno(int errno) return NFS4ERR_SERVERFAULT; } -static struct nfs_local_open_ctx __local_open_ctx __read_mostly; - static bool localio_enabled __read_mostly = true; module_param(localio_enabled, bool, 0644); @@ -157,65 +135,12 @@ bool nfs_server_is_local(const struct nfs_client *clp) } EXPORT_SYMBOL_GPL(nfs_server_is_local); -void -nfs_local_init(void) -{ - struct nfs_local_open_ctx *ctx = &__local_open_ctx; - - ctx->open_f = NULL; - spin_lock_init(&ctx->lock); - atomic_set(&ctx->refcount, 0); -} - -static bool -nfs_local_get_lookup_ctx(void) -{ - struct nfs_local_open_ctx *ctx = &__local_open_ctx; - nfs_to_nfsd_open_t fn = NULL; - - spin_lock(&ctx->lock); - if (ctx->open_f == NULL) { - spin_unlock(&ctx->lock); - - fn = symbol_request(nfsd_open_local_fh); - if (!fn) - return false; - - spin_lock(&ctx->lock); - /* catch race */ - if (ctx->open_f == NULL) { - ctx->open_f = fn; - fn = NULL; - } - } - atomic_inc(&ctx->refcount); - spin_unlock(&ctx->lock); - if (fn) - symbol_put(nfsd_open_local_fh); - return true; -} - -static void -nfs_local_put_lookup_ctx(void) -{ - struct nfs_local_open_ctx *ctx = &__local_open_ctx; - nfs_to_nfsd_open_t fn; - - if (atomic_dec_and_lock(&ctx->refcount, &ctx->lock)) { - fn = ctx->open_f; - ctx->open_f = NULL; - spin_unlock(&ctx->lock); - if (fn) - symbol_put(nfsd_open_local_fh); - } -} - /* * nfs_local_enable - attempt to enable local i/o for an nfs_client */ static void nfs_local_enable(struct nfs_client *clp) { - if (nfs_local_get_lookup_ctx()) { + if (READ_ONCE(clp->nfsd_open_local_fh)) { set_bit(NFS_CS_LOCAL_IO, &clp->cl_flags); trace_nfs_local_enable(clp); } @@ -224,12 +149,12 @@ static void nfs_local_enable(struct nfs_client *clp) /* * nfs_local_disable - disable local i/o for an nfs_client */ -void -nfs_local_disable(struct nfs_client *clp) +void nfs_local_disable(struct nfs_client *clp) { if (test_and_clear_bit(NFS_CS_LOCAL_IO, &clp->cl_flags)) { trace_nfs_local_disable(clp); - nfs_local_put_lookup_ctx(); + put_nfsd_open_local_fh(); + clp->nfsd_open_local_fh = NULL; if (!IS_ERR(clp->cl_rpcclient_localio)) { rpc_shutdown_client(clp->cl_rpcclient_localio); clp->cl_rpcclient_localio = ERR_PTR(-EINVAL); @@ -318,14 +243,13 @@ struct file * nfs_local_open_fh(struct nfs_client *clp, const struct cred *cred, struct nfs_fh *fh, const fmode_t mode) { - struct nfs_local_open_ctx *ctx = &__local_open_ctx; struct file *filp; int status; if (mode & ~(FMODE_READ | FMODE_WRITE)) return ERR_PTR(-EINVAL); - status = ctx->open_f(clp->cl_rpcclient, cred, fh, mode, &filp); + status = clp->nfsd_open_local_fh(clp->cl_rpcclient, cred, fh, mode, &filp); if (status < 0) { dprintk("%s: open local file failed error=%d\n", __func__, status); diff --git a/fs/nfs_common/nfslocalio.c b/fs/nfs_common/nfslocalio.c index f214cc6754a1..c454c4100976 100644 --- a/fs/nfs_common/nfslocalio.c +++ b/fs/nfs_common/nfslocalio.c @@ -40,3 +40,29 @@ bool nfsd_uuid_is_local(const uuid_t *uuid) return !uuid_is_null(nfsd_uuid); } EXPORT_SYMBOL_GPL(nfsd_uuid_is_local); + +/* + * The nfs localio code needs to call into nfsd to do the filehandle -> struct path + * mapping, but cannot be statically linked, because that will make the nfs module + * depend on the nfsd module. + * + * Instead, do dynamic linking to the nfsd module (via nfs_common module). The + * nfs_common module will only hold a reference on nfsd when localio is in use. + * This allows some sanity checking, like giving up on localio if nfsd isn't loaded. + */ + +extern int nfsd_open_local_fh(struct rpc_clnt *rpc_clnt, + const struct cred *cred, const struct nfs_fh *nfs_fh, + const fmode_t fmode, struct file **pfilp); + +nfs_to_nfsd_open_t get_nfsd_open_local_fh(void) +{ + return symbol_request(nfsd_open_local_fh); +} +EXPORT_SYMBOL_GPL(get_nfsd_open_local_fh); + +void put_nfsd_open_local_fh(void) +{ + symbol_put(nfsd_open_local_fh); +} +EXPORT_SYMBOL_GPL(put_nfsd_open_local_fh); diff --git a/include/linux/nfs.h b/include/linux/nfs.h index 2dacfe9742c6..64ed672a0b34 100644 --- a/include/linux/nfs.h +++ b/include/linux/nfs.h @@ -48,10 +48,6 @@ enum nfs3_stable_how { NFS_INVALID_STABLE_HOW = -1 }; -typedef int (*nfs_to_nfsd_open_t)(struct rpc_clnt *, const struct cred *, - const struct nfs_fh *, const fmode_t, - struct file **); - #ifdef CONFIG_CRC32 /** * nfs_fhandle_hash - calculate the crc32 hash for the filehandle diff --git a/include/linux/nfs_fs_sb.h b/include/linux/nfs_fs_sb.h index efcdb4d8e9de..f5760b05ec87 100644 --- a/include/linux/nfs_fs_sb.h +++ b/include/linux/nfs_fs_sb.h @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -131,6 +132,7 @@ struct nfs_client { struct timespec64 cl_nfssvc_boot; seqlock_t cl_boot_lock; struct rpc_clnt * cl_rpcclient_localio; /* localio RPC client handle */ + nfs_to_nfsd_open_t nfsd_open_local_fh; }; /* diff --git a/include/linux/nfslocalio.h b/include/linux/nfslocalio.h index d0bbacd0adcf..b8df1b9f248d 100644 --- a/include/linux/nfslocalio.h +++ b/include/linux/nfslocalio.h @@ -7,6 +7,7 @@ #include #include +#include /* * Global list of nfsd_uuid_t instances, add/remove @@ -26,4 +27,11 @@ typedef struct { bool nfsd_uuid_is_local(const uuid_t *uuid); +typedef int (*nfs_to_nfsd_open_t)(struct rpc_clnt *, const struct cred *, + const struct nfs_fh *, const fmode_t, + struct file **); + +nfs_to_nfsd_open_t get_nfsd_open_local_fh(void); +void put_nfsd_open_local_fh(void); + #endif /* __LINUX_NFSLOCALIO_H */ From patchwork Fri Jun 14 03:44:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Snitzer X-Patchwork-Id: 13697846 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 34D371474A6 for ; Fri, 14 Jun 2024 03:44:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336687; cv=none; b=DPcl8UO0VuBr29Ylj+8uJ8WPFbbG4wxnf9/dQJEJ1p9fS/WIhe5NfACA+Kt2ehctfBfJbQEub9t3ATxdgDae2zWD19AivNHHyCyAOZzY7OiSU5g6Hv5SKpjaRv7LEqoq4OsTmg8Bf0KwVNiSfaFMixhwjCKY9iQK/uuu4QUXdmY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336687; c=relaxed/simple; bh=0jRJO8aVG7pnhUhcHkGseRFav4gkepzUsaVgK4yU0GI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=E5TVNNEkuICC/i/iFzuEnZ3NFXg1S2W2/o3UovsRLvQxUVpKIqyU1kH/LhoNU47ATcDfJHN+xsCEeIHNvkyD3hX+CU89wZLZnc+KGp7rRy9XeXmD2irVu8SAX2E/r2gp2QOZkhOHZ927bv15SuXz869yHd8zFMBcp++cFyF60yE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Pl9pMw+z; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Pl9pMw+z" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8B831C2BBFC; Fri, 14 Jun 2024 03:44:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718336686; bh=0jRJO8aVG7pnhUhcHkGseRFav4gkepzUsaVgK4yU0GI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Pl9pMw+zd5Rtq9Q/0zr5NSEnVIC+2opnGZzoYjDRNnru1Gg1qrJi96j53GGbZnLlP DoJmyN2Xh3ZSRZDjBAsF9gzh89IMSvrJGUecyKwL1+UPKUc6ypTsXvJoZL+LPITktF AwRunTxb4SGHLDzOW2IWqK+4cmf7KYjn3NyAO4n6PIa6cu6r0Wrny39dteGNj2DmBd M+OJQVegFytuhRiDNecHqG/0QeSt/jUL5gvl2cNeAEBwbEwDPlnxMKOQO7STCm/aUn MT5x7shu6bVH2mUzsYfCcZuPH40/eL5ZKHmwgDQKGpTTMkKj/sTx6jFqMKbk94NuQU QzyGD+SI6QiEw== From: Mike Snitzer To: linux-nfs@vger.kernel.org Cc: Jeff Layton , Chuck Lever , Trond Myklebust , NeilBrown , snitzer@hammerspace.com Subject: [PATCH v3 14/18] nfs/nfsd: ensure localio server always uses its network namespace Date: Thu, 13 Jun 2024 23:44:22 -0400 Message-ID: <20240614034426.31043-15-snitzer@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240614034426.31043-1-snitzer@kernel.org> References: <20240614034426.31043-1-snitzer@kernel.org> Precedence: bulk X-Mailing-List: linux-nfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Pass the stored cl_nfssvc_net from the client to the server as first argument to nfsd_open_local_fh() to ensure the proper network namespace is used for localio. Otherwise, before this commit, the nfs_client's network namespace was used (as extracted from the client's cl_rpcclient). This is clearly not going to allow proper functionality if the client and server happen to have disjoint network namespaces. Elected to not rename the nfsd_uuid_t structure despite it growing a non-uuid member. Can revisit later. Signed-off-by: Mike Snitzer --- fs/nfs/client.c | 1 + fs/nfs/localio.c | 10 +++++++--- fs/nfs_common/nfslocalio.c | 15 +++++++++------ fs/nfsd/localio.c | 9 +++++---- fs/nfsd/nfssvc.c | 1 + fs/nfsd/vfs.h | 3 ++- include/linux/nfs_fs_sb.h | 1 + include/linux/nfslocalio.h | 10 ++++++---- 8 files changed, 32 insertions(+), 18 deletions(-) diff --git a/fs/nfs/client.c b/fs/nfs/client.c index cbabcdf3d785..40077ad08ccb 100644 --- a/fs/nfs/client.c +++ b/fs/nfs/client.c @@ -171,6 +171,7 @@ struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init) INIT_LIST_HEAD(&clp->cl_superblocks); clp->cl_rpcclient = clp->cl_rpcclient_localio = ERR_PTR(-EINVAL); + clp->cl_nfssvc_net = NULL; clp->nfsd_open_local_fh = NULL; clp->cl_flags = cl_init->init_flags; diff --git a/fs/nfs/localio.c b/fs/nfs/localio.c index 6c67d83427c0..27fc941d9dfa 100644 --- a/fs/nfs/localio.c +++ b/fs/nfs/localio.c @@ -138,10 +138,11 @@ EXPORT_SYMBOL_GPL(nfs_server_is_local); /* * nfs_local_enable - attempt to enable local i/o for an nfs_client */ -static void nfs_local_enable(struct nfs_client *clp) +static void nfs_local_enable(struct nfs_client *clp, struct net *net) { if (READ_ONCE(clp->nfsd_open_local_fh)) { set_bit(NFS_CS_LOCAL_IO, &clp->cl_flags); + clp->cl_nfssvc_net = net; trace_nfs_local_enable(clp); } } @@ -159,6 +160,7 @@ void nfs_local_disable(struct nfs_client *clp) rpc_shutdown_client(clp->cl_rpcclient_localio); clp->cl_rpcclient_localio = ERR_PTR(-EINVAL); } + clp->cl_nfssvc_net = NULL; } } @@ -198,6 +200,7 @@ static bool nfs_local_server_getuuid(struct nfs_client *clp, uuid_t *nfsd_uuid) void nfs_local_probe(struct nfs_client *clp) { uuid_t uuid; + struct net *net = NULL; if (!localio_enabled) goto unsupported; @@ -225,7 +228,7 @@ void nfs_local_probe(struct nfs_client *clp) } dprintk("%s: detected local server.\n", __func__); - nfs_local_enable(clp); + nfs_local_enable(clp, net); return; unsupported: @@ -249,7 +252,8 @@ nfs_local_open_fh(struct nfs_client *clp, const struct cred *cred, if (mode & ~(FMODE_READ | FMODE_WRITE)) return ERR_PTR(-EINVAL); - status = clp->nfsd_open_local_fh(clp->cl_rpcclient, cred, fh, mode, &filp); + status = clp->nfsd_open_local_fh(clp->cl_nfssvc_net, clp->cl_rpcclient, + cred, fh, mode, &filp); if (status < 0) { dprintk("%s: open local file failed error=%d\n", __func__, status); diff --git a/fs/nfs_common/nfslocalio.c b/fs/nfs_common/nfslocalio.c index c454c4100976..086e09b3ec38 100644 --- a/fs/nfs_common/nfslocalio.c +++ b/fs/nfs_common/nfslocalio.c @@ -12,29 +12,32 @@ MODULE_LICENSE("GPL"); /* * Global list of nfsd_uuid_t instances, add/remove * is protected by fs/nfsd/nfssvc.c:nfsd_mutex. - * Reads are protected RCU read lock (see below). + * Reads are protected by RCU read lock (see below). */ LIST_HEAD(nfsd_uuids); EXPORT_SYMBOL(nfsd_uuids); /* Must be called with RCU read lock held. */ -static const uuid_t * nfsd_uuid_lookup(const uuid_t *uuid) +static const uuid_t * nfsd_uuid_lookup(const uuid_t *uuid, + struct net **netp) { nfsd_uuid_t *nfsd_uuid; list_for_each_entry_rcu(nfsd_uuid, &nfsd_uuids, list) - if (uuid_equal(&nfsd_uuid->uuid, uuid)) + if (uuid_equal(&nfsd_uuid->uuid, uuid)) { + *netp = nfsd_uuid->net; return &nfsd_uuid->uuid; + } return &uuid_null; } -bool nfsd_uuid_is_local(const uuid_t *uuid) +bool nfsd_uuid_is_local(const uuid_t *uuid, struct net **netp) { const uuid_t *nfsd_uuid; rcu_read_lock(); - nfsd_uuid = nfsd_uuid_lookup(uuid); + nfsd_uuid = nfsd_uuid_lookup(uuid, netp); rcu_read_unlock(); return !uuid_is_null(nfsd_uuid); @@ -51,7 +54,7 @@ EXPORT_SYMBOL_GPL(nfsd_uuid_is_local); * This allows some sanity checking, like giving up on localio if nfsd isn't loaded. */ -extern int nfsd_open_local_fh(struct rpc_clnt *rpc_clnt, +extern int nfsd_open_local_fh(struct net *, struct rpc_clnt *rpc_clnt, const struct cred *cred, const struct nfs_fh *nfs_fh, const fmode_t fmode, struct file **pfilp); diff --git a/fs/nfsd/localio.c b/fs/nfsd/localio.c index 7ecd72406dc0..34678bfed579 100644 --- a/fs/nfsd/localio.c +++ b/fs/nfsd/localio.c @@ -103,10 +103,10 @@ nfsd_local_fakerqst_destroy(struct svc_rqst *rqstp) } static struct svc_rqst * -nfsd_local_fakerqst_create(struct rpc_clnt *rpc_clnt, const struct cred *cred) +nfsd_local_fakerqst_create(struct net *net, struct rpc_clnt *rpc_clnt, + const struct cred *cred) { struct svc_rqst *rqstp; - struct net *net = rpc_net_ns(rpc_clnt); struct nfsd_net *nn = net_generic(net, nfsd_net_id); int status; @@ -186,7 +186,8 @@ nfsd_local_fakerqst_create(struct rpc_clnt *rpc_clnt, const struct cred *cred) * dependency on knfsd. So, there is no forward declaration in a header file * for it. */ -int nfsd_open_local_fh(struct rpc_clnt *rpc_clnt, +int nfsd_open_local_fh(struct net *net, + struct rpc_clnt *rpc_clnt, const struct cred *cred, const struct nfs_fh *nfs_fh, const fmode_t fmode, @@ -203,7 +204,7 @@ int nfsd_open_local_fh(struct rpc_clnt *rpc_clnt, /* Save creds before calling into nfsd */ save_cred = get_current_cred(); - rqstp = nfsd_local_fakerqst_create(rpc_clnt, cred); + rqstp = nfsd_local_fakerqst_create(net, rpc_clnt, cred); if (IS_ERR(rqstp)) { status = PTR_ERR(rqstp); goto out_revertcred; diff --git a/fs/nfsd/nfssvc.c b/fs/nfsd/nfssvc.c index 0f2f257be94f..8f4b8c341c7c 100644 --- a/fs/nfsd/nfssvc.c +++ b/fs/nfsd/nfssvc.c @@ -473,6 +473,7 @@ static int nfsd_startup_net(struct net *net, const struct cred *cred) #endif #if IS_ENABLED(CONFIG_NFSD_LOCALIO) INIT_LIST_HEAD(&nn->nfsd_uuid.list); + nn->nfsd_uuid.net = net; list_add_tail_rcu(&nn->nfsd_uuid.list, &nfsd_uuids); #endif nn->nfsd_net_up = true; diff --git a/fs/nfsd/vfs.h b/fs/nfsd/vfs.h index 91c50649a8c7..af07bb146e81 100644 --- a/fs/nfsd/vfs.h +++ b/fs/nfsd/vfs.h @@ -160,7 +160,8 @@ __be32 nfsd_permission(struct svc_rqst *, struct svc_export *, void nfsd_filp_close(struct file *fp); -int nfsd_open_local_fh(struct rpc_clnt *rpc_clnt, +int nfsd_open_local_fh(struct net *net, + struct rpc_clnt *rpc_clnt, const struct cred *cred, const struct nfs_fh *nfs_fh, const fmode_t fmode, diff --git a/include/linux/nfs_fs_sb.h b/include/linux/nfs_fs_sb.h index f5760b05ec87..f47ea512eb0a 100644 --- a/include/linux/nfs_fs_sb.h +++ b/include/linux/nfs_fs_sb.h @@ -132,6 +132,7 @@ struct nfs_client { struct timespec64 cl_nfssvc_boot; seqlock_t cl_boot_lock; struct rpc_clnt * cl_rpcclient_localio; /* localio RPC client handle */ + struct net * cl_nfssvc_net; nfs_to_nfsd_open_t nfsd_open_local_fh; }; diff --git a/include/linux/nfslocalio.h b/include/linux/nfslocalio.h index b8df1b9f248d..c9592ad0afe2 100644 --- a/include/linux/nfslocalio.h +++ b/include/linux/nfslocalio.h @@ -8,6 +8,7 @@ #include #include #include +#include /* * Global list of nfsd_uuid_t instances, add/remove @@ -23,13 +24,14 @@ extern struct list_head nfsd_uuids; typedef struct { uuid_t uuid; struct list_head list; + struct net *net; /* nfsd's network namespace */ } nfsd_uuid_t; -bool nfsd_uuid_is_local(const uuid_t *uuid); +bool nfsd_uuid_is_local(const uuid_t *uuid, struct net **netp); -typedef int (*nfs_to_nfsd_open_t)(struct rpc_clnt *, const struct cred *, - const struct nfs_fh *, const fmode_t, - struct file **); +typedef int (*nfs_to_nfsd_open_t)(struct net *, struct rpc_clnt *, + const struct cred *, const struct nfs_fh *, + const fmode_t, struct file **); nfs_to_nfsd_open_t get_nfsd_open_local_fh(void); void put_nfsd_open_local_fh(void); From patchwork Fri Jun 14 03:44:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Snitzer X-Patchwork-Id: 13697847 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2CFF31474A3 for ; Fri, 14 Jun 2024 03:44:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336688; cv=none; b=bxLN06PjktO4v+XD4dBqBMfB4KFga+VzcWDO4qkg+HBDviyG+7kZ9RDerR2Qs4ZjtwNwDVNu3Xn1V0uJEz6BwLya3ykeJ/bjjZC1WwxAXoI9ymA/TWjj3JtJAm5zuZyB4UrS9nAk1be4BiyTNssv/4akBnODYEgsO6+lSfaSUmk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336688; c=relaxed/simple; bh=+VhZPtOcWDWjNyUpwAsxKx4UiC1ph91VogY+peSTyBk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dMM623x9fXXtmVV3gfzl6IOVW7LhKwGTyRfg2Er7WDDYtJFWnvIKcleiK/PkC8nbh+ymAl1m3JPUg1UtrtdmJGHbrDyt+q/LpXgBC7C2A7QGSjcqhRaPC7OL26JQJuD5hOrIo0hSxP2nxUAMq7KgzuGc8/tsfx1j0Tvzc0DN1V4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=LA6Ghs4m; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="LA6Ghs4m" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D0A01C2BBFC; Fri, 14 Jun 2024 03:44:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718336688; bh=+VhZPtOcWDWjNyUpwAsxKx4UiC1ph91VogY+peSTyBk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LA6Ghs4movrgCvDvalPtjedvJqHv5O2VYjUlPVROctw6gL4FZKG0WescT06r395Et C8vrbST+RE+BaniJFh84WZGS4lU9FLrIbZeYUX1fHZkhH2wluOwBDerGulMy88vfIj mOi3Y5li/+1ccNZGtAF3XGnHM8oK6WE2yQFYAexOmCGypveZsjcvPBnP7czY5dR7GE rlqMJhgtFHl6QPMyDvSh4OfGlXXCgELt8Z5Nal5s9NpeE9E6YcuMHstgPzSYw0apjt Fe/9InRx0YbOnNVeFjR/BPuCLtaGAO+L7Mv15/icUzNqoRVPuy+D4ToLx0GpGokTU9 1/J9n1JFGKWrw== From: Mike Snitzer To: linux-nfs@vger.kernel.org Cc: Jeff Layton , Chuck Lever , Trond Myklebust , NeilBrown , snitzer@hammerspace.com Subject: [PATCH v3 15/18] nfsd/localio: manage netns reference in nfsd_open_local_fh Date: Thu, 13 Jun 2024 23:44:23 -0400 Message-ID: <20240614034426.31043-16-snitzer@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240614034426.31043-1-snitzer@kernel.org> References: <20240614034426.31043-1-snitzer@kernel.org> Precedence: bulk X-Mailing-List: linux-nfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Use maybe_get_net() and put_net() in nfsd_open_local_fh(). Also refactor nfsd_open_local_fh() slightly. Signed-off-by: Mike Snitzer --- fs/nfsd/localio.c | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/fs/nfsd/localio.c b/fs/nfsd/localio.c index 34678bfed579..cdf8e115b33e 100644 --- a/fs/nfsd/localio.c +++ b/fs/nfsd/localio.c @@ -104,18 +104,11 @@ nfsd_local_fakerqst_destroy(struct svc_rqst *rqstp) static struct svc_rqst * nfsd_local_fakerqst_create(struct net *net, struct rpc_clnt *rpc_clnt, - const struct cred *cred) + const struct cred *cred, struct svc_serv *serv) { struct svc_rqst *rqstp; - struct nfsd_net *nn = net_generic(net, nfsd_net_id); int status; - /* FIXME: not running in nfsd context, must get reference on nfsd_serv */ - if (unlikely(!READ_ONCE(nn->nfsd_serv))) { - dprintk("%s: localio denied. Server not running\n", __func__); - return ERR_PTR(-ENXIO); - } - rqstp = kzalloc(sizeof(*rqstp), GFP_KERNEL); if (!rqstp) return ERR_PTR(-ENOMEM); @@ -125,13 +118,13 @@ nfsd_local_fakerqst_create(struct net *net, struct rpc_clnt *rpc_clnt, status = -ENOMEM; goto out_err; } - rqstp->rq_xprt->xpt_net = net; + __set_bit(RQ_SECURE, &rqstp->rq_flags); rqstp->rq_proc = 1; rqstp->rq_vers = 3; rqstp->rq_prot = IPPROTO_TCP; - rqstp->rq_server = nn->nfsd_serv; + rqstp->rq_server = serv; /* Note: we're connecting to ourself, so source addr == peer addr */ rqstp->rq_addrlen = rpc_peeraddr(rpc_clnt, @@ -193,28 +186,44 @@ int nfsd_open_local_fh(struct net *net, const fmode_t fmode, struct file **pfilp) { + struct nfsd_net *nn; const struct cred *save_cred; struct svc_rqst *rqstp; struct svc_fh fh; struct nfsd_file *nf; int status = 0; int mayflags = NFSD_MAY_LOCALIO; + struct svc_serv *serv; __be32 beres; + if (nfs_fh->size > NFS4_FHSIZE) + return -EINVAL; + + /* Not running in nfsd context, must safely get reference on nfsd_serv */ + net = maybe_get_net(net); + if (!net) { + dprintk("%s: localio denied. Server netns not available\n", __func__); + return -ENXIO; + } + nn = net_generic(net, nfsd_net_id); + + serv = READ_ONCE(nn->nfsd_serv); + if (unlikely(!serv)) { + dprintk("%s: localio denied. Server not running\n", __func__); + status = -ENXIO; + goto out_net; + } + /* Save creds before calling into nfsd */ save_cred = get_current_cred(); - rqstp = nfsd_local_fakerqst_create(net, rpc_clnt, cred); + rqstp = nfsd_local_fakerqst_create(net, rpc_clnt, cred, serv); if (IS_ERR(rqstp)) { status = PTR_ERR(rqstp); goto out_revertcred; } /* nfs_fh -> svc_fh */ - if (nfs_fh->size > NFS4_FHSIZE) { - status = -EINVAL; - goto out; - } fh_init(&fh, NFS4_FHSIZE); fh.fh_handle.fh_size = nfs_fh->size; memcpy(fh.fh_handle.fh_raw, nfs_fh->data, nfs_fh->size); @@ -230,17 +239,15 @@ int nfsd_open_local_fh(struct net *net, dprintk("%s: fh_verify failed %d\n", __func__, status); goto out_fh_put; } - *pfilp = get_file(nf->nf_file); - nfsd_file_put(nf); out_fh_put: fh_put(&fh); - -out: nfsd_local_fakerqst_destroy(rqstp); out_revertcred: revert_creds(save_cred); +out_net: + put_net(net); return status; } EXPORT_SYMBOL_GPL(nfsd_open_local_fh); From patchwork Fri Jun 14 03:44:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Snitzer X-Patchwork-Id: 13697848 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7574C1474B3 for ; Fri, 14 Jun 2024 03:44:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336689; cv=none; b=OY/08y684MWRAA6ry/EOxOmNjctlPIUA0Q/SRreffwicaUXUEBXVXnwo7I/PAWppcA9ZydkkSHhBfueqRKEl2/tbYWsF8VQwOxqomszaFfVBrQjG2VPRecRJwrJwfIkJwk9LwXnriFQWqhGCgGz+oSVPvmegE13bhNUniqC94XE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336689; c=relaxed/simple; bh=JX6aw9YqdgNHM0+6VWobAInAGs2FApn8yzlRP5hTm4M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iNrcwY0H64D9T9E5ZNEIrNeKyHWz1pzVqfmSKdkxPPX5sIxKdJXUinjuhYh4NCJXaICvl9I3gaizcW/0yYh99f9+lkyejLrUIWRHMUTAyLypG89ZqbT8ahpSgEmi078F+YZAmn5zE8T2c4fArpWMcZl+42yvkacbpqkU0HC3PuQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EPoVgNa3; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="EPoVgNa3" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 22B9DC2BBFC; Fri, 14 Jun 2024 03:44:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718336689; bh=JX6aw9YqdgNHM0+6VWobAInAGs2FApn8yzlRP5hTm4M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EPoVgNa3QK7RlaYGthJhU7cb9NdTmSreW6acCz+lK/ORSfoTKP+oQPPCl5QLwYjQw MfvWx4c8DH8xDq0tWZdBr8ggwTjO6YtQbyuXtf+/jzwINXtHOjgG7xz2BZ3FTHfhJd rttDY0Q/U7YrqnazmcRisV4R9XSeAV43wa1TEJNLKxWL3aqQmk8+01lMHVQ83JM0tM VQkZgMwqDskNwn8cEJmvlFRdjsVXt9mjXCEYK5DTCGz8ytQY1HqcHyOIY2IDOuuvje ArHqDUlGo4M6hDStvCbU6HKgUpxUyRHPRwLuZ9gqykqpzUeSbrOQtLzFnRpcviBS1e UY6i1WIBqrcZA== From: Mike Snitzer To: linux-nfs@vger.kernel.org Cc: Jeff Layton , Chuck Lever , Trond Myklebust , NeilBrown , snitzer@hammerspace.com Subject: [PATCH v3 16/18] nfsd: prepare to use SRCU to dereference nn->nfsd_serv Date: Thu, 13 Jun 2024 23:44:24 -0400 Message-ID: <20240614034426.31043-17-snitzer@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240614034426.31043-1-snitzer@kernel.org> References: <20240614034426.31043-1-snitzer@kernel.org> Precedence: bulk X-Mailing-List: linux-nfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The next commit switches the nfsd_serv member for struct nfsd_net over to a void pointer (void __rcu *). Prepare for this by assigning nn->nfsd_serv to an struct svc_serv pointer that is then happily dereferenced. This eliminates what would otherwise be numerous void pointer dereferences after the next commit. All nfsd code what audited so that methods that hold nfsd_mutex will continue to directly dereference nn->nfsd_serv. Signed-off-by: Mike Snitzer --- fs/nfsd/nfsctl.c | 21 +++++++++++++-------- fs/nfsd/nfssvc.c | 28 ++++++++++++++++------------ 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/fs/nfsd/nfsctl.c b/fs/nfsd/nfsctl.c index 202140df8f82..e26b1bc7ad33 100644 --- a/fs/nfsd/nfsctl.c +++ b/fs/nfsd/nfsctl.c @@ -679,10 +679,11 @@ static ssize_t write_versions(struct file *file, char *buf, size_t size) static ssize_t __write_ports_names(char *buf, struct net *net) { struct nfsd_net *nn = net_generic(net, nfsd_net_id); + struct svc_serv *serv = nn->nfsd_serv; - if (nn->nfsd_serv == NULL) + if (serv == NULL) return 0; - return svc_xprt_names(nn->nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT); + return svc_xprt_names(serv, buf, SIMPLE_TRANSACTION_LIMIT); } /* @@ -709,7 +710,7 @@ static ssize_t __write_ports_addfd(char *buf, struct net *net, const struct cred serv = nn->nfsd_serv; err = svc_addsock(serv, net, fd, buf, SIMPLE_TRANSACTION_LIMIT, cred); - if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks)) + if (!serv->sv_nrthreads && list_empty(&serv->sv_permsocks)) nfsd_destroy_serv(net); return err; @@ -757,7 +758,7 @@ static ssize_t __write_ports_addxprt(char *buf, struct net *net, const struct cr svc_xprt_put(xprt); } out_err: - if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks)) + if (!serv->sv_nrthreads && list_empty(&serv->sv_permsocks)) nfsd_destroy_serv(net); return err; @@ -1666,6 +1667,7 @@ int nfsd_nl_threads_set_doit(struct sk_buff *skb, struct genl_info *info) int nthreads = 0, count = 0, nrpools, ret = -EOPNOTSUPP, rem; struct net *net = genl_info_net(info); struct nfsd_net *nn = net_generic(net, nfsd_net_id); + struct svc_serv *serv; const struct nlattr *attr; const char *scope = NULL; @@ -1693,7 +1695,8 @@ int nfsd_nl_threads_set_doit(struct sk_buff *skb, struct genl_info *info) info->attrs[NFSD_A_SERVER_LEASETIME] || info->attrs[NFSD_A_SERVER_SCOPE]) { ret = -EBUSY; - if (nn->nfsd_serv && nn->nfsd_serv->sv_nrthreads) + serv = nn->nfsd_serv; + if (serv && serv->sv_nrthreads) goto out_unlock; ret = -EINVAL; @@ -1741,6 +1744,7 @@ int nfsd_nl_threads_get_doit(struct sk_buff *skb, struct genl_info *info) { struct net *net = genl_info_net(info); struct nfsd_net *nn = net_generic(net, nfsd_net_id); + struct svc_serv *serv; void *hdr; int err; @@ -1765,11 +1769,12 @@ int nfsd_nl_threads_get_doit(struct sk_buff *skb, struct genl_info *info) if (err) goto err_unlock; - if (nn->nfsd_serv) { + serv = nn->nfsd_serv; + if (serv) { int i; for (i = 0; i < nfsd_nrpools(net); ++i) { - struct svc_pool *sp = &nn->nfsd_serv->sv_pools[i]; + struct svc_pool *sp = &serv->sv_pools[i]; err = nla_put_u32(skb, NFSD_A_SERVER_THREADS, atomic_read(&sp->sp_nrthreads)); @@ -2087,7 +2092,7 @@ int nfsd_nl_listener_set_doit(struct sk_buff *skb, struct genl_info *info) err = ret; } - if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks)) + if (!serv->sv_nrthreads && list_empty(&serv->sv_permsocks)) nfsd_destroy_serv(net); out_unlock_mtx: diff --git a/fs/nfsd/nfssvc.c b/fs/nfsd/nfssvc.c index 8f4b8c341c7c..d6e9d346e84a 100644 --- a/fs/nfsd/nfssvc.c +++ b/fs/nfsd/nfssvc.c @@ -309,10 +309,12 @@ int nfsd_nrthreads(struct net *net) { int rv = 0; struct nfsd_net *nn = net_generic(net, nfsd_net_id); + struct svc_serv *serv; mutex_lock(&nfsd_mutex); - if (nn->nfsd_serv) - rv = nn->nfsd_serv->sv_nrthreads; + serv = nn->nfsd_serv; + if (serv) + rv = serv->sv_nrthreads; mutex_unlock(&nfsd_mutex); return rv; } @@ -321,16 +323,17 @@ static int nfsd_init_socks(struct net *net, const struct cred *cred) { int error; struct nfsd_net *nn = net_generic(net, nfsd_net_id); + struct svc_serv *serv = nn->nfsd_serv; - if (!list_empty(&nn->nfsd_serv->sv_permsocks)) + if (!list_empty(&serv->sv_permsocks)) return 0; - error = svc_xprt_create(nn->nfsd_serv, "udp", net, PF_INET, NFS_PORT, + error = svc_xprt_create(serv, "udp", net, PF_INET, NFS_PORT, SVC_SOCK_DEFAULTS, cred); if (error < 0) return error; - error = svc_xprt_create(nn->nfsd_serv, "tcp", net, PF_INET, NFS_PORT, + error = svc_xprt_create(serv, "tcp", net, PF_INET, NFS_PORT, SVC_SOCK_DEFAULTS, cred); if (error < 0) return error; @@ -742,11 +745,12 @@ int nfsd_create_serv(struct net *net) int nfsd_nrpools(struct net *net) { struct nfsd_net *nn = net_generic(net, nfsd_net_id); + struct svc_serv *serv = nn->nfsd_serv; - if (nn->nfsd_serv == NULL) + if (serv == NULL) return 0; else - return nn->nfsd_serv->sv_nrpools; + return serv->sv_nrpools; } int nfsd_get_nrthreads(int n, int *nthreads, struct net *net) @@ -767,14 +771,15 @@ int nfsd_set_nrthreads(int n, int *nthreads, struct net *net) int tot = 0; int err = 0; struct nfsd_net *nn = net_generic(net, nfsd_net_id); + struct svc_serv *serv = nn->nfsd_serv; WARN_ON(!mutex_is_locked(&nfsd_mutex)); - if (nn->nfsd_serv == NULL || n <= 0) + if (serv == NULL || n <= 0) return 0; - if (n > nn->nfsd_serv->sv_nrpools) - n = nn->nfsd_serv->sv_nrpools; + if (n > serv->sv_nrpools) + n = serv->sv_nrpools; /* enforce a global maximum number of threads */ tot = 0; @@ -804,8 +809,7 @@ int nfsd_set_nrthreads(int n, int *nthreads, struct net *net) /* apply the new numbers */ for (i = 0; i < n; i++) { - err = svc_set_num_threads(nn->nfsd_serv, - &nn->nfsd_serv->sv_pools[i], + err = svc_set_num_threads(serv, &serv->sv_pools[i], nthreads[i]); if (err) break; From patchwork Fri Jun 14 03:44:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Snitzer X-Patchwork-Id: 13697849 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 35AD61474A3 for ; Fri, 14 Jun 2024 03:44:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336691; cv=none; b=g0aYovmpAxIJ6aDpkajD/zgAqECze+R0W34KSQCvga8PbEbzZMA+N/16cQWbB0QTAxQ9/0ivMCiAUMREgEb499ohm6O4vq3hgaC2ggala/IoNCjXZdQagLdw4CBgo/9efGyHU5YVG5Gwn1kGSCzKF3bDGHiXx60oqg556fddDX4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336691; c=relaxed/simple; bh=PGXgXRtxoJmS8mA/HDlNNfVNEknhKdxj7CeASCKVtJ8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UFVXOb1N9aAdQWLdJ09/CzVayVosnHr1c5HsZOj7Qwyaa7PcJZUhUTtWE3wvyK80TFsqWBClOJLsW+27X1elFEpQnQj7LGy/NOMJmqen6+d/UPsVUwn78K5xyGvApdMDMhECvEix7aI3g2or9ZRSOiTGqIJRjl3M8rtRYeBvvBo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=HbRV0bul; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="HbRV0bul" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 80161C2BBFC; Fri, 14 Jun 2024 03:44:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718336690; bh=PGXgXRtxoJmS8mA/HDlNNfVNEknhKdxj7CeASCKVtJ8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HbRV0bulg9LJEFKLbOUyjDbm4WR/kGYib1ikj1xzR3eII1upCz+++WB9UQHnj/oI/ I4egNwBU3FDOegBehMSaBpjTlCxggem2kRddUYbSEERVRr7+kAVMWe/vQKwB7AVRRv yirbhsUpincVTYsgyXJxA8DEkHQSJQzNlnUklqmHO9rFbADcCibDGEA4sPxre/Rbat uaEuuiE5bct7w/ayHQBHdPdnfHnA7gN3hhuWdejjnVh+AadJ1R1NoQw+/FvxnHGo/Q O0ZxMayskfqpxiNsSUhMLFHMDAh8CmpOqAVBr3rjcyqntFhMLRbRRNntjF4hvv4Irb ooU4W/5zX/LjQ== From: Mike Snitzer To: linux-nfs@vger.kernel.org Cc: Jeff Layton , Chuck Lever , Trond Myklebust , NeilBrown , snitzer@hammerspace.com Subject: [PATCH v3 17/18] nfsd: use SRCU to dereference nn->nfsd_serv Date: Thu, 13 Jun 2024 23:44:25 -0400 Message-ID: <20240614034426.31043-18-snitzer@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240614034426.31043-1-snitzer@kernel.org> References: <20240614034426.31043-1-snitzer@kernel.org> Precedence: bulk X-Mailing-List: linux-nfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Introduce nfsd_serv_get, nfsd_serv_put and nfsd_serv_sync and update the nfsd code to prevent nfsd_destroy_serv from destroying nn->nfsd_serv until all nfsd code is done with it (particularly the localio code that doesn't run in the context of nfsd's svc threads, nor does it take the nfsd_mutex). Commit 83d5e5b0af90 ("dm: optimize use SRCU and RCU") provided a familiar well-worn pattern for how implement. Suggested-by: NeilBrown Signed-off-by: Mike Snitzer --- fs/nfsd/filecache.c | 13 ++++++++--- fs/nfsd/netns.h | 12 ++++++++-- fs/nfsd/nfs4state.c | 25 ++++++++++++++------- fs/nfsd/nfsctl.c | 7 ++++-- fs/nfsd/nfssvc.c | 55 ++++++++++++++++++++++++++++++++++++--------- 5 files changed, 87 insertions(+), 25 deletions(-) diff --git a/fs/nfsd/filecache.c b/fs/nfsd/filecache.c index 99631fa56662..474b3a3af3fb 100644 --- a/fs/nfsd/filecache.c +++ b/fs/nfsd/filecache.c @@ -413,12 +413,15 @@ nfsd_file_dispose_list_delayed(struct list_head *dispose) struct nfsd_file *nf = list_first_entry(dispose, struct nfsd_file, nf_lru); struct nfsd_net *nn = net_generic(nf->nf_net, nfsd_net_id); + int srcu_idx; + struct svc_serv *serv = nfsd_serv_get(nn, &srcu_idx); struct nfsd_fcache_disposal *l = nn->fcache_disposal; spin_lock(&l->lock); list_move_tail(&nf->nf_lru, &l->freeme); spin_unlock(&l->lock); - svc_wake_up(nn->nfsd_serv); + svc_wake_up(serv); + nfsd_serv_put(nn, srcu_idx); } } @@ -443,11 +446,15 @@ void nfsd_file_net_dispose(struct nfsd_net *nn) for (i = 0; i < 8 && !list_empty(&l->freeme); i++) list_move(l->freeme.next, &dispose); spin_unlock(&l->lock); - if (!list_empty(&l->freeme)) + if (!list_empty(&l->freeme)) { + int srcu_idx; + struct svc_serv *serv = nfsd_serv_get(nn, &srcu_idx); /* Wake up another thread to share the work * *before* doing any actual disposing. */ - svc_wake_up(nn->nfsd_serv); + svc_wake_up(serv); + nfsd_serv_put(nn, srcu_idx); + } nfsd_file_dispose_list(&dispose); } } diff --git a/fs/nfsd/netns.h b/fs/nfsd/netns.h index 0c5a1d97e4ac..0eebcc03bcd3 100644 --- a/fs/nfsd/netns.h +++ b/fs/nfsd/netns.h @@ -139,8 +139,12 @@ struct nfsd_net { u32 clverifier_counter; struct svc_info nfsd_info; -#define nfsd_serv nfsd_info.serv - + /* + * The current 'nfsd_serv' at nfsd_info.serv + * Use nfsd_serv_get() or take nfsd_mutex to dereference. + */ + void __rcu *nfsd_serv; + struct srcu_struct nfsd_serv_srcu; /* * clientid and stateid data for construction of net unique COPY @@ -225,6 +229,10 @@ struct nfsd_net { extern bool nfsd_support_version(int vers); extern void nfsd_netns_free_versions(struct nfsd_net *nn); +extern struct svc_serv *nfsd_serv_get(struct nfsd_net *nn, int *srcu_idx); +extern void nfsd_serv_put(struct nfsd_net *nn, int srcu_idx); +extern void nfsd_serv_sync(struct nfsd_net *nn); + extern unsigned int nfsd_net_id; void nfsd_copy_write_verifier(__be32 verf[2], struct nfsd_net *nn); diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c index a20c2c9d7d45..8876810e569d 100644 --- a/fs/nfsd/nfs4state.c +++ b/fs/nfsd/nfs4state.c @@ -1919,6 +1919,8 @@ static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn u32 num = ca->maxreqs; unsigned long avail, total_avail; unsigned int scale_factor; + int srcu_idx; + struct svc_serv *serv = nfsd_serv_get(nn, &srcu_idx); spin_lock(&nfsd_drc_lock); if (nfsd_drc_max_mem > nfsd_drc_mem_used) @@ -1940,7 +1942,7 @@ static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn * Give the client one slot even if that would require * over-allocation--it is better than failure. */ - scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads); + scale_factor = max_t(unsigned int, 8, serv->sv_nrthreads); avail = clamp_t(unsigned long, avail, slotsize, total_avail/scale_factor); @@ -1949,6 +1951,8 @@ static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn nfsd_drc_mem_used += num * slotsize; spin_unlock(&nfsd_drc_lock); + nfsd_serv_put(nn, srcu_idx); + return num; } @@ -3702,12 +3706,16 @@ nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses, static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn) { - u32 maxrpc = nn->nfsd_serv->sv_max_mesg; + int srcu_idx; + struct svc_serv *serv = nfsd_serv_get(nn, &srcu_idx); + u32 maxrpc = serv->sv_max_mesg; + __be32 status = nfs_ok; - if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ) - return nfserr_toosmall; - if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ) - return nfserr_toosmall; + if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ || + ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ) { + status = nfserr_toosmall; + goto out; + } ca->headerpadsz = 0; ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc); ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc); @@ -3726,8 +3734,9 @@ static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfs * accounting is soft and provides no guarantees either way. */ ca->maxreqs = nfsd4_get_drc_mem(ca, nn); - - return nfs_ok; +out: + nfsd_serv_put(nn, srcu_idx); + return status; } /* diff --git a/fs/nfsd/nfsctl.c b/fs/nfsd/nfsctl.c index e26b1bc7ad33..b8ea5fc409f0 100644 --- a/fs/nfsd/nfsctl.c +++ b/fs/nfsd/nfsctl.c @@ -1561,10 +1561,12 @@ int nfsd_nl_rpc_status_get_dumpit(struct sk_buff *skb, { struct nfsd_net *nn = net_generic(sock_net(skb->sk), nfsd_net_id); int i, ret, rqstp_index = 0; + int srcu_idx; + struct svc_serv *serv = nfsd_serv_get(nn, &srcu_idx); rcu_read_lock(); - for (i = 0; i < nn->nfsd_serv->sv_nrpools; i++) { + for (i = 0; i < serv->sv_nrpools; i++) { struct svc_rqst *rqstp; if (i < cb->args[0]) /* already consumed */ @@ -1572,7 +1574,7 @@ int nfsd_nl_rpc_status_get_dumpit(struct sk_buff *skb, rqstp_index = 0; list_for_each_entry_rcu(rqstp, - &nn->nfsd_serv->sv_pools[i].sp_all_threads, + &serv->sv_pools[i].sp_all_threads, rq_all) { struct nfsd_genl_rqstp genl_rqstp; unsigned int status_counter; @@ -1637,6 +1639,7 @@ int nfsd_nl_rpc_status_get_dumpit(struct sk_buff *skb, ret = skb->len; out: rcu_read_unlock(); + nfsd_serv_put(nn, srcu_idx); return ret; } diff --git a/fs/nfsd/nfssvc.c b/fs/nfsd/nfssvc.c index d6e9d346e84a..5889bc88a061 100644 --- a/fs/nfsd/nfssvc.c +++ b/fs/nfsd/nfssvc.c @@ -300,6 +300,26 @@ int nfsd_minorversion(struct nfsd_net *nn, u32 minorversion, enum vers_op change return 0; } +struct svc_serv *nfsd_serv_get(struct nfsd_net *nn, int *srcu_idx) + __acquires(nn->nfsd_serv_srcu) +{ + *srcu_idx = srcu_read_lock(&nn->nfsd_serv_srcu); + + return srcu_dereference(nn->nfsd_serv, &nn->nfsd_serv_srcu); +} + +void nfsd_serv_put(struct nfsd_net *nn, int srcu_idx) + __releases(nn->nfsd_serv_srcu) +{ + srcu_read_unlock(&nn->nfsd_serv_srcu, srcu_idx); +} + +void nfsd_serv_sync(struct nfsd_net *nn) +{ + synchronize_srcu(&nn->nfsd_serv_srcu); + synchronize_rcu_expedited(); +} + /* * Maximum number of nfsd processes */ @@ -507,6 +527,7 @@ static void nfsd_shutdown_net(struct net *net) lockd_down(net); nn->lockd_up = false; } + cleanup_srcu_struct(&nn->nfsd_serv_srcu); #if IS_ENABLED(CONFIG_NFSD_LOCALIO) list_del_rcu(&nn->nfsd_uuid.list); #endif @@ -514,6 +535,7 @@ static void nfsd_shutdown_net(struct net *net) nfsd_shutdown_generic(); } +// FIXME: eliminate nfsd_notifier_lock static DEFINE_SPINLOCK(nfsd_notifier_lock); static int nfsd_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr) @@ -523,20 +545,22 @@ static int nfsd_inetaddr_event(struct notifier_block *this, unsigned long event, struct net *net = dev_net(dev); struct nfsd_net *nn = net_generic(net, nfsd_net_id); struct sockaddr_in sin; + int srcu_idx; + struct svc_serv *serv = nfsd_serv_get(nn, &srcu_idx); - if (event != NETDEV_DOWN || !nn->nfsd_serv) + if (event != NETDEV_DOWN || !serv) goto out; spin_lock(&nfsd_notifier_lock); - if (nn->nfsd_serv) { + if (serv) { dprintk("nfsd_inetaddr_event: removed %pI4\n", &ifa->ifa_local); sin.sin_family = AF_INET; sin.sin_addr.s_addr = ifa->ifa_local; - svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin); + svc_age_temp_xprts_now(serv, (struct sockaddr *)&sin); } spin_unlock(&nfsd_notifier_lock); - out: + nfsd_serv_put(nn, srcu_idx); return NOTIFY_DONE; } @@ -553,22 +577,24 @@ static int nfsd_inet6addr_event(struct notifier_block *this, struct net *net = dev_net(dev); struct nfsd_net *nn = net_generic(net, nfsd_net_id); struct sockaddr_in6 sin6; + int srcu_idx; + struct svc_serv *serv = nfsd_serv_get(nn, &srcu_idx); - if (event != NETDEV_DOWN || !nn->nfsd_serv) + if (event != NETDEV_DOWN || !serv) goto out; spin_lock(&nfsd_notifier_lock); - if (nn->nfsd_serv) { + if (serv) { dprintk("nfsd_inet6addr_event: removed %pI6\n", &ifa->addr); sin6.sin6_family = AF_INET6; sin6.sin6_addr = ifa->addr; if (ipv6_addr_type(&sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL) sin6.sin6_scope_id = ifa->idev->dev->ifindex; - svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin6); + svc_age_temp_xprts_now(serv, (struct sockaddr *)&sin6); } spin_unlock(&nfsd_notifier_lock); - out: + nfsd_serv_put(nn, srcu_idx); return NOTIFY_DONE; } @@ -589,9 +615,12 @@ void nfsd_destroy_serv(struct net *net) struct nfsd_net *nn = net_generic(net, nfsd_net_id); struct svc_serv *serv = nn->nfsd_serv; + lockdep_assert_held(&nfsd_mutex); + spin_lock(&nfsd_notifier_lock); - nn->nfsd_serv = NULL; + rcu_assign_pointer(nn->nfsd_serv, NULL); spin_unlock(&nfsd_notifier_lock); + nfsd_serv_sync(nn); /* check if the notifier still has clients */ if (atomic_dec_return(&nfsd_notifier_refcount) == 0) { @@ -711,6 +740,10 @@ int nfsd_create_serv(struct net *net) if (nn->nfsd_serv) return 0; + error = init_srcu_struct(&nn->nfsd_serv_srcu); + if (error) + return error; + if (nfsd_max_blksize == 0) nfsd_max_blksize = nfsd_get_default_max_blksize(); nfsd_reset_versions(nn); @@ -727,8 +760,10 @@ int nfsd_create_serv(struct net *net) } spin_lock(&nfsd_notifier_lock); nn->nfsd_info.mutex = &nfsd_mutex; - nn->nfsd_serv = serv; + nn->nfsd_info.serv = serv; + rcu_assign_pointer(nn->nfsd_serv, nn->nfsd_info.serv); spin_unlock(&nfsd_notifier_lock); + nfsd_serv_sync(nn); set_max_drc(); /* check if the notifier is already set */ From patchwork Fri Jun 14 03:44:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Snitzer X-Patchwork-Id: 13697850 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 386FD1474A3 for ; Fri, 14 Jun 2024 03:44:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336692; cv=none; b=AwOlCuKon4CrmN6H5CASw+9UGtlXOswqemrJsKj1a80Gvpek7RuPZWt8zrmLriXU4e7b6iW4mD1rnDMxxFFlD1QNIeG+0xZBm/gvXs+8M39OpwyTvnPTvOSDYDaqW3E0swAhb0VLnL60ANwW1PpkpRUPIqVr2+554IHVvQ/Pmng= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718336692; c=relaxed/simple; bh=FCw8d7u3yE/Yb082AyvJ4nrRXY5ztvkcW1gDSsH/7bI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=i3JyC3IMX6Y2nkiOAX7RkTJrHjxpzoT+9a7bDbC05qHQbbUlNgNm4lRYqyS4PFFpJOO/GGyTTWNiO4yXjwWvTBxhDVtLfyI7GJXGdkMK5C8LEZY2pTCdcVXFye+7pBG9p8rSfyhIUexzhROXaV7EvZplRIGakYw9t/JNSkyK9hM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=GvUrPnwZ; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="GvUrPnwZ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D9BF1C2BBFC; Fri, 14 Jun 2024 03:44:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718336692; bh=FCw8d7u3yE/Yb082AyvJ4nrRXY5ztvkcW1gDSsH/7bI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GvUrPnwZHGyld0Vv90wsNlDv6smoEeXRZ/GxfbJzZ14L9puHoRAScb61GCuBwMmbc fTtDsfhCdxugCts6RIOa1a/SgAG21YLu49DxEesATZtwzMATXgPkxeKoyiqERFPtSj ZGyeseb2pZqnO1NDLa5DxOXGGPulmG+POm7DDLJRjif/tA3jfUe/nWSU9zLfA0+SFm ckvp2nN/ZXm0prkc1J3hAoFmS8SFvgoAg1bqR83WSc+GXffcngBU1nWC04u3vcIDIx OuHc0MXHFU5hOieB0Vx275bR9lBBihwEYnMaPBaGUWylOVVjgE8HxCNCEoPU+lZCkV p3iF9EfKj76ww== From: Mike Snitzer To: linux-nfs@vger.kernel.org Cc: Jeff Layton , Chuck Lever , Trond Myklebust , NeilBrown , snitzer@hammerspace.com Subject: [PATCH v3 18/18] nfsd/localio: use nfsd_serv_get/put in nfsd_open_local_fh Date: Thu, 13 Jun 2024 23:44:26 -0400 Message-ID: <20240614034426.31043-19-snitzer@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240614034426.31043-1-snitzer@kernel.org> References: <20240614034426.31043-1-snitzer@kernel.org> Precedence: bulk X-Mailing-List: linux-nfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Use nfsd_serv_get to SRCU deference nn->nfsd_serv and pass the resulting svc_serv to nfsd_local_fakerqst_create, open the file handle and then drop the reference using nfsd_serv_put at the end of nfsd_open_local_fh. Verified to fix an easy to hit crash that would occur if an nfsd instance running in a container, with a localio client mounted, is shutdown. Upon restart of the container and associated nfsd the client would go on to crash due to NULL pointer dereference that occuured due to the nfs client's localio attempting to nfsd_open_local_fh(), using nn->nfsd_serv, without having a proper reference on nn->nfsd_serv. Signed-off-by: Mike Snitzer --- fs/nfsd/localio.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/fs/nfsd/localio.c b/fs/nfsd/localio.c index cdf8e115b33e..d1d9fbaab82e 100644 --- a/fs/nfsd/localio.c +++ b/fs/nfsd/localio.c @@ -193,6 +193,7 @@ int nfsd_open_local_fh(struct net *net, struct nfsd_file *nf; int status = 0; int mayflags = NFSD_MAY_LOCALIO; + int srcu_idx; struct svc_serv *serv; __be32 beres; @@ -207,7 +208,7 @@ int nfsd_open_local_fh(struct net *net, } nn = net_generic(net, nfsd_net_id); - serv = READ_ONCE(nn->nfsd_serv); + serv = nfsd_serv_get(nn, &srcu_idx); if (unlikely(!serv)) { dprintk("%s: localio denied. Server not running\n", __func__); status = -ENXIO; @@ -247,6 +248,7 @@ int nfsd_open_local_fh(struct net *net, out_revertcred: revert_creds(save_cred); out_net: + nfsd_serv_put(nn, srcu_idx); put_net(net); return status; }