mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: NeilBrown <neilb@ownmail.net>
To: Alexander Viro <viro@zeniv.linux.org.uk>,
	Christian Brauner <brauner@kernel.org>
Cc: David Howells <dhowells@redhat.com>,
	Marc Dionne <marc.dionne@auristor.com>,
	Jan Harkes <jaharkes@cs.cmu.edu>,
	coda@cs.cmu.edu, Jan Kara <jack@suse.cz>,
	Richard Weinberger <richard@nod.at>,
	Anton Ivanov <anton.ivanov@cambridgegreys.com>,
	Johannes Berg <johannes@sipsolutions.net>,
	Jeff Layton <jlayton@kernel.org>,
	Amir Goldstein <amir73il@gmail.com>,
	linux-kernel@vger.kernel.org, codalist@coda.cs.cmu.edu,
	linux-fsdevel@vger.kernel.org, linux-um@lists.infradead.org,
	linux-afs@lists.infradead.org
Subject: [PATCH v3 08/11] VFS: use d_alloc_parallel() in lookup_one_qstr_excl().
Date: Wed, 25 Nov 2026 14:16:46 +1100	[thread overview]
Message-ID: <20261125034543.4084039-9-neilb@ownmail.net> (raw)
In-Reply-To: <20261125034543.4084039-1-neilb@ownmail.net>

From: NeilBrown <neil@brown.name>

lookup_one_qstr_excl() is used for lookups prior to directory
modifications (other than open(O_CREATE)), whether create, remove,
or rename.

A future patch will lift lookup out of the i_rwsem lock that protects
the directory during these operations (only taking a shared lock if the
target name is not yet in the dcache).

To prepare for this change and particularly to allow lookup to
always be done outside the parent i_rwsem, change lookup_one_qstr_excl()
to use d_alloc_parallel().

For the target of create and rename some filesystems skip the
preliminary lookup and combine it with the main operation.  This is only
safe if the operation has exclusive access to the dentry.  Currently
this is guaranteed by an exclusive lock on the directory.
d_alloc_parallel() provides alternate exclusive access (in the case
where the name isn't in the dcache and ->lookup will be called).

As a result of this change, ->lookup is now only ever called with a
d_in_lookup() dentry.  Consequently we can remove the d_in_lookup()
check from d_add_ci() which is only used in ->lookup.

The caller must now ensure d_lookup_done() is called at an appropriate
time, and must not assume that it can test for positive or negative
dentries without confirming that the dentry is no longer d_in_lookup() -
unless it is filesystem code acting on itself and *knows* that
->lookup() always completes the lookup (currently true for all
filesystems other than NFS).  This is all handled in start_creating()
and end_dirop() and friends.
At present only LOOKUP_EXCL and LOOKUP_RENAME_TARGET might result in
an in-lookup dentry after a successful ->lookup, but that might change.

Note that as lookup_one_qstr_excl() is called with an exclusive lock on
the directory, d_alloc_parallel() cannot race with another thread and
cannot return a non-in-lookup dentry.  However that is expected to
change so that case is handled with this patch.

As mentioned, with this change it is safe for d_alloc_parallel() to be
called without holding ->i_rwsem on the parent.  This holds providing
that any filesystem which uses d_alloc() or d_alloc_name() and then
instantiates the dentry, only does so in a context where ->lookup cannot
instantiate a (positive) dentry with the same name without relying
on ->i_rwsem.
This is already the case. e.g.
 - some filesystem use simple_lookup which only instantiates negative
   dentry.  A race with d_alloc() will mean the negative entry added with
   simple_lookup() will be later in the hash chain and so never found.
 - some filesystems only use d_alloc() during fs creation, before
   ->lookup is possible.

Signed-off-by: NeilBrown <neil@brown.name>
---
 Documentation/filesystems/porting.rst | 14 ++++++++++
 fs/dcache.c                           | 16 +++---------
 fs/namei.c                            | 37 +++++++++++++++++++--------
 3 files changed, 45 insertions(+), 22 deletions(-)

diff --git a/Documentation/filesystems/porting.rst b/Documentation/filesystems/porting.rst
index e41466904501..78df8842bddb 100644
--- a/Documentation/filesystems/porting.rst
+++ b/Documentation/filesystems/porting.rst
@@ -1422,3 +1422,17 @@ helpers take a ``const struct mnt_idmap *``. mnt_idmap() and file_mnt_idmap()
 return one. The idmapping is immutable so nothing should have modified it
 anyway. References are taken and dropped via mnt_idmap_get() and
 mnt_idmap_put() as before, both accept a const pointer.
+
+---
+
+**mandatory**
+
+All start_creating and start_renaming functions may return a
+d_in_lookup() dentry.  end_dirop() calls the necessary d_lookup_done().
+If the caller *knows* which filesystem is being used, it may know that
+this is not possible.  Otherwise it must be careful testing if the
+dentry is positive or negative as the lookup may not have been performed
+yet.
+
+inode_operations.lookup() is now only ever called with a d_in_lookup()
+dentry.
diff --git a/fs/dcache.c b/fs/dcache.c
index ba17f49f8a63..2ad39c1515b7 100644
--- a/fs/dcache.c
+++ b/fs/dcache.c
@@ -2472,18 +2472,10 @@ struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
 		iput(inode);
 		return found;
 	}
-	if (d_in_lookup(dentry)) {
-		found = d_alloc_parallel(dentry->d_parent, name);
-		if (IS_ERR(found) || !d_in_lookup(found)) {
-			iput(inode);
-			return found;
-		}
-	} else {
-		found = d_alloc(dentry->d_parent, name);
-		if (!found) {
-			iput(inode);
-			return ERR_PTR(-ENOMEM);
-		} 
+	found = d_alloc_parallel(dentry->d_parent, name);
+	if (IS_ERR(found) || !d_in_lookup(found)) {
+		iput(inode);
+		return found;
 	}
 	res = d_splice_alias(inode, found);
 	if (res) {
diff --git a/fs/namei.c b/fs/namei.c
index 6d63a4c08495..cfd37044a516 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -1785,13 +1785,13 @@ static struct dentry *lookup_dcache(const struct qstr *name,
 }
 
 /*
- * Parent directory has inode locked exclusive.  This is one
- * and only case when ->lookup() gets called on non in-lookup
- * dentries - as the matter of fact, this only gets called
- * when directory is guaranteed to have no in-lookup children
- * at all.
- * Will return -ENOENT if name isn't found and LOOKUP_CREATE wasn't passed.
- * Will return -EEXIST if name is found and LOOKUP_EXCL was passed.
+ * Parent directory has inode locked.
+ * d_lookup_done() must be called before the dentry is dput().
+ * If the dentry is not d_in_lookup():
+ *   Will return -ENOENT if name isn't found and LOOKUP_CREATE wasn't passed.
+ *   Will return -EEXIST if name is found and LOOKUP_EXCL was passed.
+ * If it is d_in_lookup() then these conditions can only be checked by the
+ * file system when carrying out the intent (create or rename).
  */
 static struct dentry *lookup_one_qstr_excl(const struct qstr *name,
 					   struct dentry *base, unsigned int flags)
@@ -1809,14 +1809,24 @@ static struct dentry *lookup_one_qstr_excl(const struct qstr *name,
 	if (unlikely(IS_DEADDIR(dir)))
 		return ERR_PTR(-ENOENT);
 
-	dentry = d_alloc(base, name);
-	if (unlikely(!dentry))
-		return ERR_PTR(-ENOMEM);
+	dentry = d_alloc_parallel(base, name);
+	if (unlikely(IS_ERR(dentry)))
+		return dentry;
+	if (unlikely(!d_in_lookup(dentry)))
+		/* Raced with another thread which did the lookup */
+		goto found;
 
 	old = dir->i_op->lookup(dir, dentry, flags);
 	if (unlikely(old)) {
+		d_lookup_done(dentry);
 		dput(dentry);
 		dentry = old;
+	} else if (d_in_lookup(dentry)) {
+		/* We cannot check for errors resulting from LOOKUP_ flags.
+		 * The caller will have to wait for any create-etc attempt
+		 * to get relevant errors.
+		 */
+		return dentry;
 	}
 found:
 	if (IS_ERR(dentry))
@@ -2938,6 +2948,7 @@ static struct dentry *__start_dirop(struct dentry *parent, struct qstr *name,
  * The lookup is performed and necessary locks are taken so that, on success,
  * the returned dentry can be operated on safely.
  * The qstr must already have the hash value calculated.
+ * The dentry may be d_in_lookup().
  *
  * Returns: a locked dentry, or an error.
  *
@@ -2959,6 +2970,7 @@ void end_dirop(struct dentry *de)
 {
 	if (!IS_ERR(de)) {
 		inode_unlock(de->d_parent->d_inode);
+		d_lookup_done(de);
 		dput(de);
 	}
 }
@@ -3918,8 +3930,10 @@ __start_renaming(struct renamedata *rd, int lookup_flags,
 	return 0;
 
 out_dput_d2:
+	d_lookup_done(d2);
 	dput(d2);
 out_dput_d1:
+	d_lookup_done(d1);
 	dput(d1);
 out_unlock:
 	unlock_rename(rd->old_parent, rd->new_parent);
@@ -4014,6 +4028,7 @@ __start_renaming_dentry(struct renamedata *rd, int lookup_flags,
 	return 0;
 
 out_dput_d2:
+	d_lookup_done(d2);
 	dput(d2);
 out_unlock:
 	unlock_rename(old_dentry->d_parent, rd->new_parent);
@@ -4123,6 +4138,8 @@ EXPORT_SYMBOL(start_renaming_two_dentries);
 
 void end_renaming(struct renamedata *rd)
 {
+	d_lookup_done(rd->old_dentry);
+	d_lookup_done(rd->new_dentry);
 	unlock_rename(rd->old_parent, rd->new_parent);
 	dput(rd->old_dentry);
 	dput(rd->new_dentry);
-- 
2.50.0.107.gf914562f5916.dirty


  parent reply	other threads:[~2026-11-25  3:46 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25  3:16 [PATCH v3 00/11] multiple-filesystems: prepare for changes to VFS locking NeilBrown
2026-11-25  3:16 ` [PATCH v3 01/11] coda: don't d_drop() early NeilBrown
2026-11-25  3:16 ` [PATCH v3 02/11] hostfs: don't d_drop() before d_splice_alias() in hostfs_mkdir() NeilBrown
2026-11-25  3:16 ` [PATCH v3 03/11] afs: don't unhash/rehash dentries during unlink/rename NeilBrown
2026-11-25  3:16 ` [PATCH v3 04/11] afs: use d_splice_alias() in afs_vnode_new_inode() NeilBrown
2026-11-25  3:16 ` [PATCH v3 05/11] afs: use d_alloc_trylock in afs_sillyrename() NeilBrown
2026-11-25  3:16 ` [PATCH v3 06/11] afs: use d_duplicate() NeilBrown
2026-11-25  3:16 ` [PATCH v3 07/11] Revert "VFS: Add LOOKUP_SHARED flag." NeilBrown
2026-11-25  3:16 ` NeilBrown [this message]
2026-11-25  3:16 ` [PATCH v3 09/11] VFS: drop parent lock across d_alloc_parallel() in d_add_ci() NeilBrown
2026-11-25  3:16 ` [PATCH v3 10/11] procfs: drop parent lock for d_alloc_parallel() in iterate_shared() NeilBrown
2026-11-25  3:16 ` [PATCH v3 11/11] afs: lookup_atsys to drop and reclaim lock NeilBrown

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261125034543.4084039-9-neilb@ownmail.net \
    --to=neilb@ownmail.net \
    --cc=amir73il@gmail.com \
    --cc=anton.ivanov@cambridgegreys.com \
    --cc=brauner@kernel.org \
    --cc=coda@cs.cmu.edu \
    --cc=codalist@coda.cs.cmu.edu \
    --cc=dhowells@redhat.com \
    --cc=jack@suse.cz \
    --cc=jaharkes@cs.cmu.edu \
    --cc=jlayton@kernel.org \
    --cc=johannes@sipsolutions.net \
    --cc=linux-afs@lists.infradead.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-um@lists.infradead.org \
    --cc=marc.dionne@auristor.com \
    --cc=neil@brown.name \
    --cc=richard@nod.at \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®