From mboxrd@z Thu Jan 1 00:00:00 1970 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 8E6EB23BCF4; Mon, 19 May 2025 21:23:08 +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=1747689788; cv=none; b=LAQJAVjI/WZ2OryeKI7LElWQzpqycu+qpfwp3petnmjFlLumuxQ63ZUtQjxBbhtJEHohawH3s6i1t/aRgN8IrsGaGE7mWOt7cafxmMXsAUIiX7/9X7kwfaEGzRxjTYPJeu+luBiWKp/kdSNHIy/N8npUAxy75QM2isLPLRLE9c8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747689788; c=relaxed/simple; bh=Dlv3w4F1QbFjvkdzyOVgTmZlcHVR56hTFA91+JwoJ2Q=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hsoEyUXHhEGbMl9xVypcdosHstKqWMfUsrMP0Vp/ACB/t6N605VjtJjid8NNndvmL+3ea5J4L7lfVXrRuF0tVpTKbKau1cCoPc7UvDLcoBfcM/aQbbNiND4G8JhD3A+6FwTUU30U/qEwCPvfMeHOSBr/7lk6ezoj970yBly36mw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=nK/ERx02; 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="nK/ERx02" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 865EFC4CEE4; Mon, 19 May 2025 21:23:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1747689788; bh=Dlv3w4F1QbFjvkdzyOVgTmZlcHVR56hTFA91+JwoJ2Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nK/ERx022E0gwHCWl1QrO++3z+uhc1ayoy52pUuYii5LD9MUlnvNhs0RmP/vXWNQy WHnw1I5Nua5bCPgoq/n+uxuZsPbpKoWpUOeA12oSBIg54wWpDsxV5yNq+rQbQaI9c9 4b4sRGmCwcEIN5ZYucmo07sDPt3GHG+VZnUl/YztSxrfjcHAO0JzArDdl1r1BrPZrc AlGBLLzYLp+g9OStIN2WdLtoCVHL7uS/LOHwYsdiONwAELoGArgWn67fpK8oeYvGJ6 XM5iucLtTw5ab9aUq9F5E2nei9SKAoBFakvdSVs4ec0OaTH6PXkX08fdWqdUcbB4te kaU73qTRCFJJQ== From: Sasha Levin To: patches@lists.linux.dev, stable@vger.kernel.org Cc: Trond Myklebust , Jeff Layton , Sasha Levin , anna.schumaker@netapp.com, linux-nfs@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH AUTOSEL 6.1 8/8] NFS: Avoid flushing data while holding directory locks in nfs_rename() Date: Mon, 19 May 2025 17:22:55 -0400 Message-Id: <20250519212255.1986527-8-sashal@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250519212255.1986527-1-sashal@kernel.org> References: <20250519212255.1986527-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.1.139 Content-Transfer-Encoding: 8bit From: Trond Myklebust [ Upstream commit dcd21b609d4abc7303f8683bce4f35d78d7d6830 ] The Linux client assumes that all filehandles are non-volatile for renames within the same directory (otherwise sillyrename cannot work). However, the existence of the Linux 'subtree_check' export option has meant that nfs_rename() has always assumed it needs to flush writes before attempting to rename. Since NFSv4 does allow the client to query whether or not the server exhibits this behaviour, and since knfsd does actually set the appropriate flag when 'subtree_check' is enabled on an export, it should be OK to optimise away the write flushing behaviour in the cases where it is clearly not needed. Signed-off-by: Trond Myklebust Reviewed-by: Jeff Layton Signed-off-by: Sasha Levin --- fs/nfs/client.c | 2 ++ fs/nfs/dir.c | 15 ++++++++++++++- include/linux/nfs_fs_sb.h | 12 +++++++++--- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/fs/nfs/client.c b/fs/nfs/client.c index a8930e6c417fc..de4ad41b14e2a 100644 --- a/fs/nfs/client.c +++ b/fs/nfs/client.c @@ -1052,6 +1052,8 @@ struct nfs_server *nfs_create_server(struct fs_context *fc) if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN) server->namelen = NFS2_MAXNAMLEN; } + /* Linux 'subtree_check' borkenness mandates this setting */ + server->fh_expire_type = NFS_FH_VOL_RENAME; if (!(fattr->valid & NFS_ATTR_FATTR)) { error = ctx->nfs_mod->rpc_ops->getattr(server, ctx->mntfh, diff --git a/fs/nfs/dir.c b/fs/nfs/dir.c index 70660ff248b79..1876978107ca1 100644 --- a/fs/nfs/dir.c +++ b/fs/nfs/dir.c @@ -2632,6 +2632,18 @@ nfs_unblock_rename(struct rpc_task *task, struct nfs_renamedata *data) unblock_revalidate(new_dentry); } +static bool nfs_rename_is_unsafe_cross_dir(struct dentry *old_dentry, + struct dentry *new_dentry) +{ + struct nfs_server *server = NFS_SB(old_dentry->d_sb); + + if (old_dentry->d_parent != new_dentry->d_parent) + return false; + if (server->fh_expire_type & NFS_FH_RENAME_UNSAFE) + return !(server->fh_expire_type & NFS_FH_NOEXPIRE_WITH_OPEN); + return true; +} + /* * RENAME * FIXME: Some nfsds, like the Linux user space nfsd, may generate a @@ -2719,7 +2731,8 @@ int nfs_rename(struct user_namespace *mnt_userns, struct inode *old_dir, } - if (S_ISREG(old_inode->i_mode)) + if (S_ISREG(old_inode->i_mode) && + nfs_rename_is_unsafe_cross_dir(old_dentry, new_dentry)) nfs_sync_inode(old_inode); task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, must_unblock ? nfs_unblock_rename : NULL); diff --git a/include/linux/nfs_fs_sb.h b/include/linux/nfs_fs_sb.h index 9ea9f9087a712..a9671f9300848 100644 --- a/include/linux/nfs_fs_sb.h +++ b/include/linux/nfs_fs_sb.h @@ -196,6 +196,15 @@ struct nfs_server { char *fscache_uniq; /* Uniquifier (or NULL) */ #endif + /* The following #defines numerically match the NFSv4 equivalents */ +#define NFS_FH_NOEXPIRE_WITH_OPEN (0x1) +#define NFS_FH_VOLATILE_ANY (0x2) +#define NFS_FH_VOL_MIGRATION (0x4) +#define NFS_FH_VOL_RENAME (0x8) +#define NFS_FH_RENAME_UNSAFE (NFS_FH_VOLATILE_ANY | NFS_FH_VOL_RENAME) + u32 fh_expire_type; /* V4 bitmask representing file + handle volatility type for + this filesystem */ u32 pnfs_blksize; /* layout_blksize attr */ #if IS_ENABLED(CONFIG_NFS_V4) u32 attr_bitmask[3];/* V4 bitmask representing the set @@ -219,9 +228,6 @@ struct nfs_server { u32 acl_bitmask; /* V4 bitmask representing the ACEs that are supported on this filesystem */ - u32 fh_expire_type; /* V4 bitmask representing file - handle volatility type for - this filesystem */ struct pnfs_layoutdriver_type *pnfs_curr_ld; /* Active layout driver */ struct rpc_wait_queue roc_rpcwaitq; void *pnfs_ld_data; /* per mount point data */ -- 2.39.5