mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/3] cephfs: prepare for changes to VFS locking
@ 2026-09-29  2:47 NeilBrown
  2026-09-29  2:47 ` [PATCH 1/3] cephfs: Use d_alloc_trylock() in ceph_readdir_prepopulate() NeilBrown
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: NeilBrown @ 2026-09-29  2:47 UTC (permalink / raw)
  To: Ilya Dryomov, Alex Markuze, Viacheslav Dubeyko, Alexander Viro,
	Christian Brauner
  Cc: Jeff Layton, Jan Kara, linux-fsdevel, ceph-devel, linux-kernel

I am working on changes to directory locking.  The medium term goal is
to lift d_alloc_parallel() locking above i_rwsem, so d_alloc_paralle()
can be run unlocked, and consequently cannot be called when ->i_rwsem is
held.

The requires:
 1/ that d_alloc_parallel() not be called while i_rwsem is held and also
    that d_alloc()/d_splice_alias() not be used (unless the filesystems
    doesn't support creating objects).
    cephfs uses d_alloc()/d_splice_alias() when priming the dcache from
    the results of readdir.  A new interface d_alloc_trylock()
    is now available and can be used instead.

 2/ a dentry must not be d_drop()ed while an operation is ongoing, as an
    unlocked d_alloc_parallel() would then be able to create a new
    dentry with the same name.  cephfs calls d_drop() in couple of places
    before d_splice_alias() but this is no longer needed as d_splice_alias()
    handles spliced aliases now.

Patch 3/3 isn't strictly needed here but I'd like to deprecate d_add()
in favour of d_splice_alias() as the latter now does everything the
former can do.  So the last patch removes uses of d_add() from fs/ceph.

The patches depend on patches recently added to the vfs tree.  I would
prefer these land in that tree too with an Acked-by or similar from cifs
maintainers.

Thanks,
NeilBrown

 [PATCH 1/3] cephfs: Use d_alloc_trylock() in
 [PATCH 2/3] cephfs: Don't d_drop() before d_splice_alias()
 [PATCH 3/3] cephfs: stop using d_add()

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/3] cephfs: Use d_alloc_trylock() in ceph_readdir_prepopulate()
  2026-09-29  2:47 [PATCH 0/3] cephfs: prepare for changes to VFS locking NeilBrown
@ 2026-09-29  2:47 ` NeilBrown
  2026-09-29  2:47 ` [PATCH 2/3] cephfs: Don't d_drop() before d_splice_alias() NeilBrown
  2026-09-29  2:47 ` [PATCH 3/3] cephfs: stop using d_add() NeilBrown
  2 siblings, 0 replies; 4+ messages in thread
From: NeilBrown @ 2026-09-29  2:47 UTC (permalink / raw)
  To: Ilya Dryomov, Alex Markuze, Viacheslav Dubeyko, Alexander Viro,
	Christian Brauner
  Cc: Jeff Layton, Jan Kara, linux-fsdevel, ceph-devel, linux-kernel

From: NeilBrown <neil@brown.name>

cephfs uses the results of readdir to prime the dcache.
ceph_readdir() is called with an exclusive lock on the directory
so it currently cannot race with any lookup which allocates a dentry.
So it is safe to simply d_alloc() a new dentry and then d_splice_alias()
to install it in the dcache.

After we lift d_alloc_parallel() out of the parent lock this won't be
safe.

The safe interface to use here is d_alloc_trylock() which will handle
any races and can fail if there is a concurrent lookup which is working
on an in-look dentry.  In the rare case that this does fail there is
little cost in simply skipping the priming of the dcache for this name -
some other thread which owns the dentry will fill those details in soon
enough.

So change to use d_alloc_trylock() and handle -EWOULDBLOCK.  Also use
QSTR_LEN() to initialise dname.  full_name_hash() and d_lookup() are no
longer needed as d_alloc_trylock() includes all that.
Because we use d_alloc_trylock() we need to call d_lookup_done() to
ensure the dentry gets unlocked.

Signed-off-by: NeilBrown <neil@brown.name>
---
 fs/ceph/inode.c | 38 +++++++++++++++++---------------------
 1 file changed, 17 insertions(+), 21 deletions(-)

diff --git a/fs/ceph/inode.c b/fs/ceph/inode.c
index d52e2b389e0b..74a107b6ef3c 100644
--- a/fs/ceph/inode.c
+++ b/fs/ceph/inode.c
@@ -2069,9 +2069,7 @@ int ceph_readdir_prepopulate(struct ceph_mds_request *req,
 		struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
 		struct ceph_vino tvino;
 
-		dname.name = rde->name;
-		dname.len = rde->name_len;
-		dname.hash = full_name_hash(parent, dname.name, dname.len);
+		dname = QSTR_LEN(rde->name, rde->name_len);
 
 		tvino.ino = le64_to_cpu(rde->inode.in->ino);
 		tvino.snap = le64_to_cpu(rde->inode.in->snapid);
@@ -2087,24 +2085,20 @@ int ceph_readdir_prepopulate(struct ceph_mds_request *req,
 		}
 
 retry_lookup:
-		dn = d_lookup(parent, &dname);
-		doutc(cl, "d_lookup on parent=%p name=%.*s got %p\n",
+		dn = d_alloc_trylock(parent, &dname);
+		doutc(cl, "d_alloc_trylock on parent=%p name=%.*s got %p\n",
 		      parent, dname.len, dname.name, dn);
-
-		if (!dn) {
-			dn = d_alloc(parent, &dname);
-			doutc(cl, "d_alloc %p '%.*s' = %p\n", parent,
-			      dname.len, dname.name, dn);
-			if (!dn) {
-				doutc(cl, "d_alloc badness\n");
-				err = -ENOMEM;
-				goto out;
-			}
-			if (rde->is_nokey) {
-				spin_lock(&dn->d_lock);
-				dn->d_flags |= DCACHE_NOKEY_NAME;
-				spin_unlock(&dn->d_lock);
-			}
+		if (dn == ERR_PTR(-EWOULDBLOCK)) {
+			/* Some other thread is working on this name */
+			continue;
+		} else if (IS_ERR(dn)) {
+			doutc(cl, "d_alloc_trylock badness\n");
+			err = PTR_ERR(dn);
+			goto out;
+		} else if (d_in_lookup(dn) && rde->is_nokey) {
+			spin_lock(&dn->d_lock);
+			dn->d_flags |= DCACHE_NOKEY_NAME;
+			spin_unlock(&dn->d_lock);
 		} else if (d_really_is_positive(dn) &&
 			   (ceph_ino(d_inode(dn)) != tvino.ino ||
 			    ceph_snap(d_inode(dn)) != tvino.snap)) {
@@ -2133,6 +2127,7 @@ int ceph_readdir_prepopulate(struct ceph_mds_request *req,
 			in = ceph_get_inode(parent->d_sb, tvino, NULL);
 			if (IS_ERR(in)) {
 				doutc(cl, "new_inode badness\n");
+				d_lookup_done(dn);
 				d_drop(dn);
 				dput(dn);
 				err = PTR_ERR(in);
@@ -2159,7 +2154,7 @@ int ceph_readdir_prepopulate(struct ceph_mds_request *req,
 		if (inode_state_read_once(in) & I_NEW)
 			unlock_new_inode(in);
 
-		if (d_really_is_negative(dn)) {
+		if (d_in_lookup(dn) || d_really_is_negative(dn)) {
 			if (ceph_security_xattr_deadlock(in)) {
 				doutc(cl, " skip splicing dn %p to inode %p"
 				      " (security xattr deadlock)\n", dn, in);
@@ -2186,6 +2181,7 @@ int ceph_readdir_prepopulate(struct ceph_mds_request *req,
 				err = ret;
 		}
 next_item:
+		d_lookup_done(dn);
 		dput(dn);
 	}
 out:

base-commit: 3879f51857325da9bf3cfb073280257cd16ae067
-- 
2.50.0.107.gf914562f5916.dirty


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/3] cephfs: Don't d_drop() before d_splice_alias()
  2026-09-29  2:47 [PATCH 0/3] cephfs: prepare for changes to VFS locking NeilBrown
  2026-09-29  2:47 ` [PATCH 1/3] cephfs: Use d_alloc_trylock() in ceph_readdir_prepopulate() NeilBrown
@ 2026-09-29  2:47 ` NeilBrown
  2026-09-29  2:47 ` [PATCH 3/3] cephfs: stop using d_add() NeilBrown
  2 siblings, 0 replies; 4+ messages in thread
From: NeilBrown @ 2026-09-29  2:47 UTC (permalink / raw)
  To: Ilya Dryomov, Alex Markuze, Viacheslav Dubeyko, Alexander Viro,
	Christian Brauner
  Cc: Jeff Layton, Jan Kara, linux-fsdevel, ceph-devel, linux-kernel

From: NeilBrown <neil@brown.name>

In two places ceph drops a dentry and then calls d_splice_alias().
The d_drop() is no longer needed before d_splice_alias() and will
cause problems for proposed changes to locking.

Signed-off-by: NeilBrown <neil@brown.name>
---
 fs/ceph/file.c  | 2 --
 fs/ceph/inode.c | 3 ---
 2 files changed, 5 deletions(-)

diff --git a/fs/ceph/file.c b/fs/ceph/file.c
index bd3e3f5c269e..62c1b77fcd90 100644
--- a/fs/ceph/file.c
+++ b/fs/ceph/file.c
@@ -772,8 +772,6 @@ static int ceph_finish_async_create(struct inode *dir, struct inode *inode,
 			unlock_new_inode(inode);
 		}
 		if (d_in_lookup(dentry) || d_really_is_negative(dentry)) {
-			if (!d_unhashed(dentry))
-				d_drop(dentry);
 			dn = d_splice_alias(inode, dentry);
 			WARN_ON_ONCE(dn && dn != dentry);
 		}
diff --git a/fs/ceph/inode.c b/fs/ceph/inode.c
index 74a107b6ef3c..01c468a509e2 100644
--- a/fs/ceph/inode.c
+++ b/fs/ceph/inode.c
@@ -1555,9 +1555,6 @@ static int splice_dentry(struct dentry **pdn, struct inode *in)
 		}
 	}
 
-	/* dn must be unhashed */
-	if (!d_unhashed(dn))
-		d_drop(dn);
 	realdn = d_splice_alias(in, dn);
 	if (IS_ERR(realdn)) {
 		pr_err_client(cl, "error %ld %p inode %p ino %llx.%llx\n",
-- 
2.50.0.107.gf914562f5916.dirty


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 3/3] cephfs: stop using d_add()
  2026-09-29  2:47 [PATCH 0/3] cephfs: prepare for changes to VFS locking NeilBrown
  2026-09-29  2:47 ` [PATCH 1/3] cephfs: Use d_alloc_trylock() in ceph_readdir_prepopulate() NeilBrown
  2026-09-29  2:47 ` [PATCH 2/3] cephfs: Don't d_drop() before d_splice_alias() NeilBrown
@ 2026-09-29  2:47 ` NeilBrown
  2 siblings, 0 replies; 4+ messages in thread
From: NeilBrown @ 2026-09-29  2:47 UTC (permalink / raw)
  To: Ilya Dryomov, Alex Markuze, Viacheslav Dubeyko, Alexander Viro,
	Christian Brauner
  Cc: Jeff Layton, Jan Kara, linux-fsdevel, ceph-devel, linux-kernel

From: NeilBrown <neil@brown.name>

d_add() is being deprecated in favour of d_splice_alias()
So replace all uses in cephfs.

Signed-off-by: NeilBrown <neil@brown.name>
---
 fs/ceph/dir.c   | 4 ++--
 fs/ceph/inode.c | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/fs/ceph/dir.c b/fs/ceph/dir.c
index 2e5c0ccb1b34..35c18020d0f4 100644
--- a/fs/ceph/dir.c
+++ b/fs/ceph/dir.c
@@ -779,7 +779,7 @@ struct dentry *ceph_finish_lookup(struct ceph_mds_request *req,
 						d_inode(dentry->d_parent);
 					if (!parent ||
 					    ceph_snap(parent) == CEPH_NOSNAP)
-						d_add(dentry, NULL);
+						d_splice_alias(NULL, dentry);
 				}
 			}
 		}
@@ -853,7 +853,7 @@ static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
 			doutc(cl, " dir %llx.%llx complete, -ENOENT\n",
 			      ceph_vinop(dir));
 			if (d_unhashed(dentry))
-				d_add(dentry, NULL);
+				d_splice_alias(NULL, dentry);
 			di->lease_shared_gen = atomic_read(&ci->i_shared_gen);
 			return NULL;
 		}
diff --git a/fs/ceph/inode.c b/fs/ceph/inode.c
index 01c468a509e2..07868fcfc6cc 100644
--- a/fs/ceph/inode.c
+++ b/fs/ceph/inode.c
@@ -1813,7 +1813,7 @@ int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req)
 			} else if (have_lease) {
 				if (d_unhashed(dn) &&
 				    ceph_snap(dir) == CEPH_NOSNAP)
-					d_add(dn, NULL);
+					d_splice_alias(NULL, dn);
 			}
 
 			if (!d_unhashed(dn) && have_lease)
-- 
2.50.0.107.gf914562f5916.dirty


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-09-29  2:55 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-29  2:47 [PATCH 0/3] cephfs: prepare for changes to VFS locking NeilBrown
2026-09-29  2:47 ` [PATCH 1/3] cephfs: Use d_alloc_trylock() in ceph_readdir_prepopulate() NeilBrown
2026-09-29  2:47 ` [PATCH 2/3] cephfs: Don't d_drop() before d_splice_alias() NeilBrown
2026-09-29  2:47 ` [PATCH 3/3] cephfs: stop using d_add() NeilBrown

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®