afs: prepare for VFS locking changes.
Subsystems (2)
v2 cover afs: prepare for VFS locking changes.
The previous version of this series has a problem in 3/5 in that it didn't handle an error from afs_lookup() properly - such errors should be fatal and not cause a retry.
Original intro:
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/ the d_alloc_parallel() not be called while i_rwsem is held. NFS does
this in a few places. New interfaces d_alloc_trylock() and d_duplicate() are now available and can be used instead. In one case we must drop and reclaim the lock and LOOKUP_SHARED helps with that.
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. In particular d_rehash() must be phased out as it won't be safe. This particularly affect the mechanism to block lookups/revalidates while an unlink/rename is happening. The mechanism is changed to use one similar to how NFS addresses this needed, though using the new LOOKUP_PRIVATE flag.
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 AFS maintainers.
Thanks,
NeilBrown
[PATCH v2 1/5] afs: don't unhash/rehash dentries during unlink/rename [PATCH v2 2/5] afs: use d_splice_alias() in afs_vnode_new_inode() [PATCH v2 3/5] afs: use d_alloc_trylock in afs_sillyrename() [PATCH v2 4/5] afs: lookup_atsys to drop and reclaim lock. [PATCH v2 5/5] afs: use d_duplicate()
PATCH v2 1/5 afs: don't unhash/rehash dentries during unlink/rename
From: NeilBrown <[email protected]>
afs needs to block lookup of dentries during unlink and rename. There are two reasons:
1/ If the target is to be removed, not silly-renamed, the subsequent
opens cannot be allowed as the file won't exist on the server.
2/ If the rename source is being moved between directories a lookup,
particularly d_revalidate, might change ->d_time asynchronously with rename changing ->d_time with possible incorrect results.
afs current unhashes the dentry to force a lookup which will wait on the directory lock, and rehashes afterwards. This is incompatible with proposed changed to directory locking which will require a dentry to remain hashed throughout rename/unlink/etc operations.
This patch copies a mechanism developed for NFS but uses the new DCACHE_PRIVATE flag rather then ->d_fsdata. DCACHE_PRIVATE is given the private name DCACHE_BLOCKED within afs.
The flag is set when lookups and revalidates must be blocked. d_revalidate checks for this flag and waits for it to become be cleared.
->d_lock is used to ensure d_revalidate never updates the time stamp in ->d_fsdata while DCACHE_PRIVATE is set.
When setting DCACHE_BLOCKED is used to prevent lookups we need write_seqcount_invalidate(&dentry->d_seq) as well to ensure RCU-walk don't proceed to an open until DCACHE_BLOCKED is cleared. When being used to avoid revalidating the timestamp in d_fsdata, that invalidation isn't needed.
The unlocking in afs_rename_edit_dir() is not needed. It was previously needed because it would not be safe to d_rehash() after calling d_move(). But now that we don't use d_rehash() that is not a concern. afs_rename_put() is always called and is sufficient for all rename unlocking.
--- fs/afs/dir.c | 95 +++++++++++++++++++++++++++++++++-------------- fs/afs/internal.h | 5 +-- 2 files changed, 69 insertions(+), 31 deletions(-)
fs/afs/dir.c +68 -27
@@ -49,6 +49,14 @@ static int afs_rename(struct mnt_idmap *idmap, struct inode *old_dir,static int afs_dir_writepages (struct address_space *mapping,struct writeback_control *wbc); +/* +* This is set to stop d_revalidate looking at, and possibly changing, +* ->d_fsdata (a timestamp) on a dentry which is being moved between +* directories, and to block lookup and possible open() for a dentry +* that is being removed without silly-rename. +*/ +#define DCACHE_BLOCKED DCACHE_PRIVATE +const struct file_operations afs_dir_file_operations = { .open = afs_dir_open, .release = afs_release,
@@ -1031,6 +1039,10 @@ static int afs_d_revalidate_rcu(struct afs_vnode *dvnode, struct dentry *dentry)if (!afs_check_validity (dvnode))return -ECHILD ; +/* A rename/unlink is pending */ +if (dentry->d_flags &DCACHE_BLOCKED ) +return -ECHILD ; +/* We only need to invalidate a dentry if the server's copy changed * behind our back. If we made the change, it's no problem. Note that * on a 32-bit system, we only have 32 bits in the dentry to store the
@@ -1066,6 +1078,10 @@ static int afs_d_revalidate(struct inode *parent_dir, const struct qstr *name,if (flags &LOOKUP_RCU )return afs_d_revalidate_rcu (dir, dentry); +/* Wait for rename/unlink to complete */ +wait_for_rename: +wait_var_event (&dentry->d_flags, !(dentry->d_flags &DCACHE_BLOCKED )); +if (d_really_is_positive (dentry)) { vnode =AFS_FS_I (d_inode (dentry)); _enter("{v={%llx:%llu} n=%pd fl=%lx},",
@@ -1158,7 +1174,13 @@ static int afs_d_revalidate(struct inode *parent_dir, const struct qstr *name, } out_valid: +spin_lock (&dentry->d_lock); +if (dentry->d_flags &DCACHE_BLOCKED ) { +spin_unlock (&dentry->d_lock); + goto wait_for_rename; + } dentry->d_fsdata = (void *)(unsigned long )dir_version; +spin_unlock (&dentry->d_lock); out_valid_noupdate:key_put (key);_leave (" = 1 [valid]" );
@@ -1533,8 +1555,10 @@ static void afs_unlink_edit_dir(struct afs_operation *op)static void afs_unlink_put (struct afs_operation *op) {_enter ("op=%08x" , op->debug_id); -if (op->unlink.need_rehash &&afs_op_error (op) <0 &&afs_op_error (op) != -ENOENT ) -d_rehash (op->dentry); +spin_lock (&op->dentry->d_lock); +store_release_wake_up (&op->dentry->d_flags, + op->dentry->d_flags &~DCACHE_BLOCKED ); +spin_unlock (&op->dentry->d_lock); }static const struct afs_operation_ops afs_unlink_operation = {
@@ -1588,11 +1612,13 @@ static int afs_unlink(struct inode *dir, struct dentry *dentry)afs_op_set_error (op,afs_sillyrename (dvnode, vnode, dentry, op->key)); goto error; } -if (!d_unhashed (dentry)) { -/* Prevent a race with RCU lookup. */ -__d_drop (dentry); - op->unlink.need_rehash = true; - } +/* +* As RCU-walk calls d_revalidate() before incrementing d_count +* it may have already run. We need to invalidate d_seq so +* legitimize_path() will trigger a retry. +*/ +write_seqcount_invalidate (&dentry->d_seq); + dentry->d_flags |=DCACHE_BLOCKED ;spin_unlock (&dentry->d_lock); op->file[1 ].vnode = vnode;
@@ -1899,11 +1925,6 @@ static void afs_rename_edit_dir(struct afs_operation *op)_enter ("op=%08x" , op->debug_id); -if (op->rename.rehash) { -d_rehash (op->rename.rehash); - op->rename.rehash =NULL ; - } -fscache_begin_write_operation (&orig_cres,afs_vnode_cache (orig_dvnode));if (new_dvnode != orig_dvnode)fscache_begin_write_operation (&new_cres,afs_vnode_cache (new_dvnode));
@@ -2023,11 +2044,18 @@ static void afs_rename_exchange_edit_dir(struct afs_operation *op)static void afs_rename_put (struct afs_operation *op) {_enter ("op=%08x" , op->debug_id); -if (op->rename.rehash) -d_rehash (op->rename.rehash); +if (op->rename.unblock) { +spin_lock (&op->rename.unblock->d_lock); +store_release_wake_up (&op->rename.unblock->d_flags, + op->rename.unblock->d_flags &~DCACHE_BLOCKED ); +spin_unlock (&op->rename.unblock->d_lock); + op->rename.unblock =NULL ; + } +spin_lock (&op->dentry->d_lock); +store_release_wake_up (&op->dentry->d_flags, + op->dentry->d_flags &~DCACHE_BLOCKED ); +spin_unlock (&op->dentry->d_lock);dput (op->rename.tmp); -if (afs_op_error (op)) -d_rehash (op->dentry); }static const struct afs_operation_ops afs_rename_operation = {
@@ -2135,7 +2163,11 @@ static int afs_rename(struct mnt_idmap *idmap, struct inode *old_dir, op->ops = &afs_rename_noreplace_operation; }else if (flags &RENAME_EXCHANGE ) { op->ops = &afs_rename_exchange_operation; -d_drop (new_dentry); +/* Block revalidate on new_dentry until rename completes */ +spin_lock (&new_dentry->d_lock); + new_dentry->d_flags |=DCACHE_BLOCKED ; + op->rename.unblock = new_dentry; +spin_unlock (&new_dentry->d_lock); }else {/* If we might displace the target, we might need to do silly * rename.
@@ -2148,15 +2180,18 @@ static int afs_rename(struct mnt_idmap *idmap, struct inode *old_dir,* and becomes the new target. */ if (d_is_positive (new_dentry) && !d_is_dir (new_dentry)) { -/* To prevent any new references to the target during -* the rename, we unhash the dentry in advance. + +/* +* To prevent any new references to the target +* during the rename, we set DCACHE_BLOCKED +* which afs_d_revalidate will wait for. d_lock +* ensures d_count() and DCACHE_BLOCKED are +* consistent. */ -if (!d_unhashed (new_dentry)) { -d_drop (new_dentry); - op->rename.rehash = new_dentry; - } +spin_lock (&new_dentry->d_lock);if (d_count (new_dentry) >2 ) { +spin_unlock (&new_dentry->d_lock);/* copy the target dentry's name */ op->rename.tmp =d_alloc (new_dentry->d_parent, &new_dentry->d_name);
@@ -2174,8 +2209,13 @@ static int afs_rename(struct mnt_idmap *idmap, struct inode *old_dir, } op->dentry_2 = op->rename.tmp; - op->rename.rehash =NULL ; op->rename.new_negative = true; + }else { +/* Block any lookups to target until the rename completes */ +write_seqcount_invalidate (&new_dentry->d_seq); + new_dentry->d_flags |=DCACHE_BLOCKED ; + op->rename.unblock = new_dentry; +spin_unlock (&new_dentry->d_lock); } } }
@@ -2186,10 +2226,11 @@ static int afs_rename(struct mnt_idmap *idmap, struct inode *old_dir,* d_revalidate may see old_dentry between the op having taken place * and the version being updated. * -* So drop the old_dentry for now to make other threads go through -* lookup instead - which we hold a lock against. +* So block revalidate on the old_dentry until the rename completes. */ -d_drop (old_dentry); +spin_lock (&old_dentry->d_lock); + old_dentry->d_flags |=DCACHE_BLOCKED ; +spin_unlock (&old_dentry->d_lock); ret =afs_do_sync_operation (op);if (ret == -ENOTSUPP )
fs/afs/internal.h +1 -4
@@ -899,10 +899,7 @@ struct afs_operation {struct afs_symlink *symlink; } create;struct { -bool need_rehash; - } unlink; -struct { -struct dentry *rehash; +struct dentry *unblock;struct dentry *tmp;unsigned int rename_flags;bool new_negative;
base-commit: 3879f51857325da9bf3cfb073280257cd16ae067
Signature · 1 line
2.50.0.107.gf914562f5916.dirty
PATCH v2 2/5 afs: use d_splice_alias() in afs_vnode_new_inode()
In reply to NeilBrownFrom: NeilBrown <[email protected]>
As afs supports the fhandle interfaces there is a theoretical possibility that the inode created for mkdir could be found by open_by_handle_at() and given a dentry before d_instantiate() is called. This would result in two dentries for the one directory inode, which is not permitted.
So this patch changes afs_mkdir() to use d_splice_alias() and to return the alternate dentry from ->mkdir() if appropriate.
--- fs/afs/dir.c | 13 ++++++++----- fs/afs/internal.h | 1 + 2 files changed, 9 insertions(+), 5 deletions(-)
fs/afs/dir.c +8 -5
@@ -1253,7 +1253,7 @@ void afs_check_for_remote_deletion(struct afs_operation *op)/* * Create a new inode for create/mkdir/symlink */ -static void afs_vnode_new_inode (struct afs_operation *op) +static struct dentry *afs_vnode_new_inode (struct afs_operation *op) {struct afs_vnode_param *dvp = &op->file[0 ];struct afs_vnode_param *vp = &op->file[1 ];
@@ -1270,7 +1270,7 @@ static void afs_vnode_new_inode(struct afs_operation *op)* the new directory on the server. */ afs_op_accumulate_error (op,PTR_ERR (inode),0 ); -return ; +return NULL ; } vnode =AFS_FS_I (inode);
@@ -1281,7 +1281,7 @@ static void afs_vnode_new_inode(struct afs_operation *op)afs_init_new_symlink (vnode, op);if (!afs_op_error (op))afs_cache_permit (vnode, op->key, vnode->cb_break, &vp->scb); -d_instantiate (op->dentry, inode); +return d_splice_alias (inode, op->dentry); }static void afs_create_success (struct afs_operation *op)
@@ -1290,7 +1290,7 @@ static void afs_create_success(struct afs_operation *op) op->ctime = op->file[0 ].scb.status.mtime_client;afs_vnode_commit_status (op, &op->file[0 ]);afs_update_dentry_version (op, &op->file[0 ], op->dentry); -afs_vnode_new_inode (op); + op->create.ret =afs_vnode_new_inode (op); }static void afs_create_edit_dir (struct afs_operation *op)
@@ -1361,7 +1361,10 @@ static struct dentry *afs_mkdir(struct mnt_idmap *idmap, struct inode *dir, op->ops = &afs_mkdir_operation; ret =afs_do_sync_operation (op);afs_dir_unuse_cookie (dvnode, ret); -return ERR_PTR (ret); +if (ret) +return ERR_PTR (ret); +else +return op->create.ret; }/*
fs/afs/internal.h +1 -0
@@ -897,6 +897,7 @@ struct afs_operation {int reason;/* enum afs_edit_dir_reason */ mode_t mode;struct afs_symlink *symlink; +struct dentry *ret; } create;struct {struct dentry *unblock;
Signature · 1 line
2.50.0.107.gf914562f5916.dirty
PATCH v2 3/5 afs: use d_alloc_trylock in afs_sillyrename()
In reply to NeilBrownFrom: NeilBrown <[email protected]>
Rather than performing a normal lookup (which will be awkward with future locking changes) use d_alloc_trylock() to find a dentry for an unused name, and use an open-coded lookup_slow() to see if it is free on the server.
--- fs/afs/dir.c | 6 ++---- fs/afs/dir_silly.c | 54 +++++++++++++++++++++++++++++++--------------- fs/afs/internal.h | 2 ++ 3 files changed, 41 insertions(+), 21 deletions(-)
fs/afs/dir.c +2 -4
@@ -19,8 +19,6 @@#include "afs_fs.h" #include "xdr_fs.h" -static struct dentry *afs_lookup (struct inode *dir,struct dentry *dentry, -unsigned int flags);static int afs_dir_open (struct inode *inode,struct file *file);static int afs_readdir (struct file *file,struct dir_context *ctx); static int afs_d_revalidate(struct inode *dir, const struct qstr *name,
@@ -967,8 +965,8 @@ static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry)/* * look up an entry in a directory */ -static struct dentry *afs_lookup (struct inode *dir,struct dentry *dentry, -unsigned int flags) +struct dentry *afs_lookup (struct inode *dir,struct dentry *dentry, +unsigned int flags) {struct afs_vnode *dvnode =AFS_FS_I (dir);struct afs_fid fid = {};
fs/afs/dir_silly.c +37 -17
67 lines not on this page · raw message
fs/afs/internal.h +2 -0
@@ -1109,6 +1109,8 @@ ssize_t afs_read_dir(struct afs_vnode *dvnode, struct file *file)__acquires (&dvnode->validate_lock);extern void afs_d_release (struct dentry *);extern void afs_check_for_remote_deletion (struct afs_operation *); +extern struct dentry *afs_lookup (struct inode *dir,struct dentry *dentry, +unsigned int flags);/* * dir_edit.c
Signature · 1 line
2.50.0.107.gf914562f5916.dirty
PATCH v2 4/5 afs: lookup_atsys to drop and reclaim lock.
In reply to NeilBrownFrom: NeilBrown <[email protected]>
If afs is asked to lookup a name ending with @sys, it needs to look up a different name for which it might need to allocate a dentry with d_alloc_parallel().
This is done while the parent lock is held which will be a problem in a future patch where the ordering of the parent lock and d_alloc_parallel() locking is reversed.
There is no actual need to hold the lock during this nested lookup, so with this patch we drop the lock and reclaim it, and use lookup_noperm_unlocked().
--- fs/afs/dir.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-)
fs/afs/dir.c +22 -3
@@ -911,7 +911,8 @@ static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry)/* * Look up an entry in a directory with @sys substitution. */ -static struct dentry *afs_lookup_atsys (struct inode *dir,struct dentry *dentry) +static struct dentry *afs_lookup_atsys (struct inode *dir,struct dentry *dentry, +unsigned int flags) {struct afs_sysnames *subs;struct afs_net *net =afs_i2net (dir);
@@ -936,6 +937,15 @@ static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry)refcount_inc (&subs->usage);read_unlock (&net->sysnames_lock); +/* +* We need the directory to be unlocked so we can perform other lookups. +* We don't really need the lock any more. The in-lookup status of +* dentry gives us sufficient exclusion. +*/ +if (flags &LOOKUP_SHARED ) +inode_unlock_shared (dir); +else +inode_unlock (dir);for (i =0 ; i < subs->nr; i++) { name = subs->subs[i]; len = dentry->d_name.len -4 +strlen (name);
@@ -945,7 +955,7 @@ static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry) }strcpy (p, name); - ret =lookup_noperm (&QSTR (buf), dentry->d_parent); + ret =lookup_noperm_unlocked (&QSTR (buf), dentry->d_parent);if (IS_ERR (ret) ||d_is_positive (ret)) goto out_s;dput (ret);
@@ -956,6 +966,15 @@ static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry)*/ ret =NULL ; out_s: +/* +* Cannot take parent lock while we hold an in-lookup dentry so +* must make sure that lookup is done. +*/ +d_lookup_done (dentry); +if (flags &LOOKUP_SHARED ) +inode_lock_shared (dir); +else +inode_lock_nested (dir,I_MUTEX_PARENT );afs_put_sysnames (subs);kfree (buf); out_p:
@@ -1001,7 +1020,7 @@ struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry, dentry->d_name.name[dentry->d_name.len - 3] == 's' && dentry->d_name.name[dentry->d_name.len - 2] == 'y' && dentry->d_name.name[dentry->d_name.len - 1] == 's') -return afs_lookup_atsys (dir, dentry); +return afs_lookup_atsys (dir, dentry, flags);afs_stat_v (dvnode, n_lookup); inode =afs_do_lookup (dir, dentry);
Signature · 1 line
2.50.0.107.gf914562f5916.dirty
PATCH v2 5/5 afs: use d_duplicate()
In reply to NeilBrownFrom: NeilBrown <[email protected]>
To prepare for d_alloc_parallel() being permitted without a directory lock, use d_duplicate() when duplicating a dentry in order to perform a silly-rename. This creates on in-lookup dentry which will contine to block concurrent lookups after the original dentry is silly-renamed, and until the final rename happens.
As the resulting dentry is in-lookup, we need to call d_lookup_done() before d_put().
--- fs/afs/dir.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
fs/afs/dir.c +5 -3
@@ -2075,7 +2075,10 @@ static void afs_rename_put(struct afs_operation *op)store_release_wake_up (&op->dentry->d_flags, op->dentry->d_flags &~DCACHE_BLOCKED );spin_unlock (&op->dentry->d_lock); -dput (op->rename.tmp); +if (op->rename.tmp) { +d_lookup_done (op->rename.tmp); +dput (op->rename.tmp); + } }static const struct afs_operation_ops afs_rename_operation = {
@@ -2213,8 +2216,7 @@ static int afs_rename(struct mnt_idmap *idmap, struct inode *old_dir,if (d_count (new_dentry) >2 ) {spin_unlock (&new_dentry->d_lock);/* copy the target dentry's name */ - op->rename.tmp =d_alloc (new_dentry->d_parent, - &new_dentry->d_name); + op->rename.tmp =d_duplicate (new_dentry);if (!op->rename.tmp) {afs_op_nomem (op); goto error;
Signature · 1 line
2.50.0.107.gf914562f5916.dirty