From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2E5AD106ACEC for ; Thu, 12 Mar 2026 21:46:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 905FE6B0089; Thu, 12 Mar 2026 17:46:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8F0B06B008C; Thu, 12 Mar 2026 17:46:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7DC6B6B0092; Thu, 12 Mar 2026 17:46:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 6E8C46B0089 for ; Thu, 12 Mar 2026 17:46:14 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 34D9A1C47C for ; Thu, 12 Mar 2026 21:46:14 +0000 (UTC) X-FDA: 84538744668.04.83892DA Received: from flow-b6-smtp.messagingengine.com (flow-b6-smtp.messagingengine.com [202.12.124.141]) by imf13.hostedemail.com (Postfix) with ESMTP id 44FC520008 for ; Thu, 12 Mar 2026 21:46:12 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=ownmail.net header.s=fm1 header.b=WQJKthP0; dkim=pass header.d=messagingengine.com header.s=fm1 header.b=XqN3zbVr; spf=pass (imf13.hostedemail.com: domain of neilb@ownmail.net designates 202.12.124.141 as permitted sender) smtp.mailfrom=neilb@ownmail.net; dmarc=pass (policy=none) header.from=ownmail.net ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1773351972; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=jtoJ4VcsB5KJ5RIkAzW9fPn73bKnKljRG2pvIdddt9Y=; b=RxlMZ46kzPcjzPPwwcQVaMWnV8TyNyzshDaNO/cm/5RhdS/5Pb+hPTHrZpjjq3VVlQOk/x U4ERroccw9ET+cVPaZgS7I18j3YJ4fxtrVUAazyZ/zRD8D89ciK2L6b0HZ8YKJzJRaEKAf hmygnT3o1maVbbSePSGY6aS3ho0GUJw= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=ownmail.net header.s=fm1 header.b=WQJKthP0; dkim=pass header.d=messagingengine.com header.s=fm1 header.b=XqN3zbVr; spf=pass (imf13.hostedemail.com: domain of neilb@ownmail.net designates 202.12.124.141 as permitted sender) smtp.mailfrom=neilb@ownmail.net; dmarc=pass (policy=none) header.from=ownmail.net ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1773351972; a=rsa-sha256; cv=none; b=237JkClpwgYAvEdsjKOFHQiF9pFy3JTZRP3BhX54bGNd8nxT8+nWyCdvSidiXjsEIC7Sfn AlJ4Y87eAlgGF80fxWJIjaodYH9Pbk3JB6GjN/GnsaBfWNCUqVoSNqREtgz8eBfXHGQ3M+ cU/urTNHGu+e6dIhQF8KfYd2SRbw2Ow= Received: from phl-compute-04.internal (phl-compute-04.internal [10.202.2.44]) by mailflow.stl.internal (Postfix) with ESMTP id 5A0321301B47; Thu, 12 Mar 2026 17:46:10 -0400 (EDT) Received: from phl-frontend-03 ([10.202.2.162]) by phl-compute-04.internal (MEProxy); Thu, 12 Mar 2026 17:46:11 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ownmail.net; h= cc:cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:reply-to:subject:subject:to:to; s=fm1; t=1773351970; x=1773359170; bh=jtoJ4VcsB5KJ5RIkAzW9fPn73bKnKljRG2pvIdddt9Y=; b= WQJKthP0rTsiI3RJocxIH3ZvletwK+s1SspQgny4TfWHov2isTsHrUD4GI2x+h1N 0Uz3Ba5LJCq/zK5+RzsV2iv55k10jF6H59khluBI7mUd+MOoaLyvYsOhfvQyAgVj zeCrNgqGyjwp2B+tZiItl7z8gshz0/F73xa/PNbn+IQTYiY1gr32QdTX2H7Hrw8C v2ChvnkqOxTcfxmT5LfOrub5UMwIcDz/kXudaMUhKmneYYSQME5b3zRy6VW9u4BV BlEmUX20QekaXZmvDj12T54B2/nh09aPaUIx5wAGpvwz5mHzIyUWvw8oh5jS9w89 3hHbH6QXcFcOR/CylE4G0g== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; t=1773351970; x=1773359170; bh=j toJ4VcsB5KJ5RIkAzW9fPn73bKnKljRG2pvIdddt9Y=; b=XqN3zbVrMt4Dgh7Xe Hu9v4x7EFwXrTqUlq7pGIydeYfW4eXt0t2LewIJtqvHwWZQ5vpf0H+vKOJahySNe ly4TeGTRFpEdg/6Bv1bvNAezzqcp4yK98kxJn0+LkZSH866Fp88T0OTZeguLrslq EB4fcaD5sgj2UJILJgyCfYX/ohZjgN0WVfx80rKtOk7DvlXiIkfOuPyQGuLjcU4H pDO9Y3ljMvqWBzqQn1SL9Y6COsNusqrqWpN1JDr8F03oMSpiF1GNMBuemKgFrzkT RRG6Wqns755H7VAlLA4g0/2gE5Qy5zY4Nr0+UjY2OTfoBIPRyHZ78Gkm+MTFasH4 5PU+A== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefgedrtddtgddvkeejledtucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujf gurhephffvvefufffkofgjfhhrggfgsedtkeertdertddtnecuhfhrohhmpefpvghilheu rhhofihnuceonhgvihhlsgesohifnhhmrghilhdrnhgvtheqnecuggftrfgrthhtvghrnh epveevkeffudeuvefhieeghffgudektdelkeejiedtjedugfeukedvkeffvdefvddunecu vehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepnhgvihhlsg esohifnhhmrghilhdrnhgvthdpnhgspghrtghpthhtohephedupdhmohguvgepshhmthhp ohhuthdprhgtphhtthhopehvihhrohesiigvnhhivhdrlhhinhhugidrohhrghdruhhkpd hrtghpthhtoheplhhinhhugidqgihfshesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgt phhtthhopehlihhnuhigqdhunhhiohhnfhhssehvghgvrhdrkhgvrhhnvghlrdhorhhgpd hrtghpthhtoheplhhinhhugidqthhrrggtvgdqkhgvrhhnvghlsehvghgvrhdrkhgvrhhn vghlrdhorhhgpdhrtghpthhtoheplhhinhhugidqnhhfshesvhhgvghrrdhkvghrnhgvlh drohhrghdprhgtphhtthhopehlihhnuhigqdhkvghrnhgvlhesvhhgvghrrdhkvghrnhgv lhdrohhrghdprhgtphhtthhopehlihhnuhigqdhfshguvghvvghlsehvghgvrhdrkhgvrh hnvghlrdhorhhgpdhrtghpthhtoheplhhinhhugidqvgigthegsehvghgvrhdrkhgvrhhn vghlrdhorhhgpdhrtghpthhtoheplhhinhhugidqvghfihesvhhgvghrrdhkvghrnhgvlh drohhrgh X-ME-Proxy: Feedback-ID: i9d664b8f:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 12 Mar 2026 17:45:56 -0400 (EDT) From: NeilBrown To: Linus Torvalds , Alexander Viro , Christian Brauner , Jan Kara , Jeff Layton , Trond Myklebust , Anna Schumaker , Carlos Maiolino , Miklos Szeredi , Amir Goldstein , Jan Harkes , Hugh Dickins , Baolin Wang , David Howells , Marc Dionne , Steve French , Namjae Jeon , Sungjong Seo , Yuezhang Mo , Andreas Hindborg , Breno Leitao , "Theodore Ts'o" , Andreas Dilger , Steven Rostedt , Masami Hiramatsu , Ilya Dryomov , Alex Markuze , Viacheslav Dubeyko , Tyler Hicks , Andreas Gruenbacher , Richard Weinberger , Anton Ivanov , Johannes Berg , Jeremy Kerr , Ard Biesheuvel Cc: linux-fsdevel@vger.kernel.org, linux-nfs@vger.kernel.org, linux-xfs@vger.kernel.org, linux-unionfs@vger.kernel.org, coda@cs.cmu.edu, linux-mm@kvack.org, linux-afs@lists.infradead.org, linux-cifs@vger.kernel.org, linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, ceph-devel@vger.kernel.org, ecryptfs@vger.kernel.org, gfs2@lists.linux.dev, linux-um@lists.infradead.org, linux-efi@vger.kernel.org Subject: [PATCH 06/53] VFS: add d_duplicate() Date: Fri, 13 Mar 2026 08:11:53 +1100 Message-ID: <20260312214330.3885211-7-neilb@ownmail.net> X-Mailer: git-send-email 2.50.0.107.gf914562f5916.dirty In-Reply-To: <20260312214330.3885211-1-neilb@ownmail.net> References: <20260312214330.3885211-1-neilb@ownmail.net> Reply-To: NeilBrown MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 44FC520008 X-Stat-Signature: hmgko8rrqnsr6anax1bmjxib3rgq857a X-Rspam-User: X-Rspamd-Server: rspam06 X-HE-Tag: 1773351972-312165 X-HE-Meta: U2FsdGVkX1/n5i9jp9w05EEi/+/wg+/I8OD5N+hnGdtDlLB8rYhKmAsDCNyETPB566YGzgbcnUSBgm12oHZYzH68vMGFI3yWB0IQSMqb3/kiAw4kSr+Cc8xqW+mJLR5TOQcKy8Pb3ErIRPH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hFxjiqzF/v3nRs11TCnItlM/DJGndYf6wDuLq/y1ZFAcxgS/JGA56HQOcqz9wiNJjrt/vCs3tSW/ywDT8gnVGxhSxu6U2D65Y7GlSVNdRCmeYNcYirE1+ZL0BukAJU2Avmuee3k11S3dzypSr6DxWOGlKfwK8vg+ZwictU+gA62LmMB2pHjFwvC6K0uNcDGRnGTRUBfWJCi8kIQQpl3T+J/Oz9pEqD2Eqo1w61tjroMLlIBmjf+/w97Mrr4sx6YF/Dxz6j+ygiKdOTktoe4Bst3uBllsh2JO95ZhWhFtOy8srLQN4fWX/Fxgr69XETj4bcn/0OxHdY5clsLfCi0b8UveXRClPJS0HYodAiWmfst3grGXJUlYiMIZtbHItMTotasoU8O0oS6qXcl2IMvQLZDGuA4jVNswket8= Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: NeilBrown Occasionally a single operation can require two sub-operations on the same name, and it is important that a d_alloc_parallel() (once that can be run unlocked) does not create another dentry with the same name between the operations. Two examples: 1/ rename where the target name (a positive dentry) needs to be "silly-renamed" to a temporary name so it will remain available on the server (NFS and AFS). Here the same name needs to be the subject of one rename, and the target of another. 2/ rename where the subject needs to be replaced with a white-out (shmemfs). Here the same name need to be the target of a rename and the target of a mknod() In both cases the original dentry is renamed to something else, and a replacement is instantiated, possibly as the target of d_move(), possibly by d_instantiate(). Currently d_alloc() is used to create the dentry and the exclusive lock on the parent ensures no other dentry is created. When d_alloc_parallel() is moved out of the parent lock, this will no longer be sufficient. In particular if the original is renamed away before the new is instantiated, there is a window where d_alloc_parallel() could create another name. "silly-rename" does work in this order. shmemfs whiteout doesn't open this hole but is essentially the same pattern and should use the same approach. The new d_duplicate() creates an in-lookup dentry with the same name as the original dentry, which must be hashed. There is no need to check if an in-lookup dentry exists with the same name as d_alloc_parallel() will never try add one while the hashed dentry exists. Once the new in-lookup is created, d_alloc_parallel() will find it and wait for it to complete, then use it. Signed-off-by: NeilBrown --- fs/dcache.c | 52 ++++++++++++++++++++++++++++++++++++++++++ include/linux/dcache.h | 1 + 2 files changed, 53 insertions(+) diff --git a/fs/dcache.c b/fs/dcache.c index f4d7d200bc46..c12319097d6e 100644 --- a/fs/dcache.c +++ b/fs/dcache.c @@ -1832,6 +1832,58 @@ struct dentry *d_alloc(struct dentry * parent, const struct qstr *name) } EXPORT_SYMBOL(d_alloc); +/** + * d_duplicate: duplicate a dentry for combined atomic operation + * @dentry: the dentry to duplicate + * + * Some rename operations need to be combined with another operation + * inside the filesystem. + * 1/ A cluster filesystem when renaming to an in-use file might need to + * first "silly-rename" that target out of the way before the main rename + * 2/ A filesystem that supports white-out might want to create a whiteout + * in place of the file being moved. + * + * For this they need two dentries which temporarily have the same name, + * before one is renamed. d_duplicate() provides for this. Given a + * positive hashed dentry, it creates a second in-lookup dentry. + * Because the original dentry exists, no other thread will try to + * create an in-lookup dentry, os there can be no race in this create. + * + * The caller should d_move() the original to a new name, often via a + * rename request, and should call d_lookup_done() on the newly created + * dentry. If the new is instantiated and the old MUST either be moved + * or dropped. + * + * Parent must be locked. + * + * Returns: an in-lookup dentry, or an error. + */ +struct dentry *d_duplicate(struct dentry *dentry) +{ + unsigned int hash = dentry->d_name.hash; + struct dentry *parent = dentry->d_parent; + struct hlist_bl_head *b = in_lookup_hash(parent, hash); + struct dentry *new = __d_alloc(parent->d_sb, &dentry->d_name); + + if (unlikely(!new)) + return ERR_PTR(-ENOMEM); + + new->d_flags |= DCACHE_PAR_LOOKUP; + new->d_wait = NULL; + spin_lock(&parent->d_lock); + new->d_parent = dget_dlock(parent); + hlist_add_head(&new->d_sib, &parent->d_children); + if (parent->d_flags & DCACHE_DISCONNECTED) + new->d_flags |= DCACHE_DISCONNECTED; + spin_unlock(&dentry->d_parent->d_lock); + + hlist_bl_lock(b); + hlist_bl_add_head(&new->d_u.d_in_lookup_hash, b); + hlist_bl_unlock(b); + return new; +} +EXPORT_SYMBOL(d_duplicate); + struct dentry *d_alloc_anon(struct super_block *sb) { return __d_alloc(sb, NULL); diff --git a/include/linux/dcache.h b/include/linux/dcache.h index 3cb70b3398f0..2a3ebd368ed9 100644 --- a/include/linux/dcache.h +++ b/include/linux/dcache.h @@ -247,6 +247,7 @@ extern struct dentry * d_alloc_anon(struct super_block *); extern struct dentry * d_alloc_parallel(struct dentry *, const struct qstr *); extern struct dentry * d_alloc_noblock(struct dentry *, struct qstr *); extern struct dentry * d_splice_alias(struct inode *, struct dentry *); +struct dentry *d_duplicate(struct dentry *dentry); /* weird procfs mess; *NOT* exported */ extern struct dentry * d_splice_alias_ops(struct inode *, struct dentry *, const struct dentry_operations *); -- 2.50.0.107.gf914562f5916.dirty