cephfs: prepare for changes to VFS locking
Subsystems (2)
cover cephfs: prepare for changes to VFS locking
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()
PATCH 1/3 cephfs: Use d_alloc_trylock() in ceph_readdir_prepopulate()
From: NeilBrown <[email protected]>
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.
--- fs/ceph/inode.c | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-)
fs/ceph/inode.c +17 -21
@@ -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
Signature · 1 line
2.50.0.107.gf914562f5916.dirty
PATCH 2/3 cephfs: Don't d_drop() before d_splice_alias()
In reply to NeilBrownFrom: NeilBrown <[email protected]>
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.
--- fs/ceph/file.c | 2 -- fs/ceph/inode.c | 3 --- 2 files changed, 5 deletions(-)
fs/ceph/file.c +0 -2
@@ -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); }
fs/ceph/inode.c +0 -3
@@ -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",
Signature · 1 line
2.50.0.107.gf914562f5916.dirty
PATCH 3/3 cephfs: stop using d_add()
In reply to NeilBrownFrom: NeilBrown <[email protected]>
d_add() is being deprecated in favour of d_splice_alias() So replace all uses in cephfs.
--- fs/ceph/dir.c | 4 ++-- fs/ceph/inode.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)
fs/ceph/dir.c +2 -2
@@ -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 ; }
fs/ceph/inode.c +1 -1
@@ -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)
Signature · 1 line
2.50.0.107.gf914562f5916.dirty
Alex Markuze
maintainerHi Neil,
NACK. Please respin.
-EWOULDBLOCK from d_alloc_trylock() just continues, so that name is left out of the readdir cache and skipped is not incremented. We still set DID_PREPOPULATE, and a later dcache readdir can drop the name. Treat it like the security skip: skipped++; continue; so DID_PREPOPULATE is not set and dir_release_count is cleared.
Signature · 1 line
Alex Markuze