demonstrate proposed new locking strategy for directories
Subsystems (10)
cover demonstrate proposed new locking strategy for directories
This patches are still under development. In particular some proper documentation is needed. They are sufficient to demonstrate my design.
They add an alternate mechanism for providing the locking that the VFS needs for directory operations. This includes:
- only one operation per name at a time
- no operations in a directory being removed
- no concurrent cross-directory renames which might result in an ancestor loop
I had originally hoped to push the locking of i_rw_sem down into the filesystems and have the new locking on top of that. This turned out to be impractical. This series leave the i_rw_sem locking where it is, introduces new locking that happens while the directory is locked, and gives the filesystem the option of disabling (most of) the i_rw_sem locking. Once all filesystems are converted the i_rw_sem locking can be removed.
Shared lock on i_rw_sem is still used for readdir and simple lookup, to exclude it while rmdir is happening.
The problem with pushing i_rw_sem down is that I still want to use it to exclude readdir while rmdir is happening. Some readdir implementations use the result to prime the dcache which means creating d_in_lookup() dentries in the directory. If we can do this while holding i_rw_sem, then it is not safe to take i_rw_sem while holding a d_in_lookup() dentry. So i_rw_sem CANNOT be taken after a lookup has been performed - it must be before, or never.
Another issue is that after taking i_rw_sem in rmdir() I need to wait for any dentries that are still locked. Waiting for the dentry lock while holding i_rw_sem means we cannot take i_rw_sem after getting a dentry lock.
So we take i_rw_sem for filesystems that still require it (initially all) but still do the other locking which will be uncontended. This exercises the code to help ensure it is ready when we remove the i_rw_sem requirement for any given filesystem.
The central feature is a per-dentry lock implemented with a couple of d_flags and wait_var_event/wake_up_var. A single thread can take 1, sometimes 2, occasionally 3 locks on different dentries.
A second lock is needed for rename - we lock the two dentries in address-order after confirming there is no hierarchical relationship. It is also needed for silly-rename as part of unlink. In this case the plan is for the second dentry to always be a d_in_lookup dentry so the lock is guaranteed to be uncontented. I'm not sure I got that finished yet.
The three-dentry case is a rename which results in a silly-rename of the target.
For rmdir we introduce S_DYING so that marking a directory a S_DEAD is two-stage. We mark is S_DYING which will prevent more dentry locks being taken, then we wait for the locks that were already taken, then set S_DEAD.
For rename ... maybe just read the patch. I tried to explain it thoroughly.
The goal is to perform create/remove/rename without any mutex/semaphore held by the VFS. This will allow concurrent operations in a directory and prepare the way for async operation so that e.g. io_uring could be given a list of many names in a directory to unlink and it could unlink them in parallel. We probably need to make changes to the locking on the inode being removed before this can be fully achieved - I haven't explored that in detail yet.
Thanks,
NeilBrown
PATCH 1/8 VFS: use global wait-queue table for d_alloc_parallel()
d_alloc_parallel() currently requires a wait_queue_head to be passed in. This must have a life time which extends until the lookup is completed.
Future proposed patches will use d_alloc_parallel() for names being created/unlinked etc. Some filesystems combine lookup with create making a longer code path that the wq needs to live for. If it is still to be allocated on-stack this can be cumbersome.
This patch replaces the on-stack wqs with a global array of wqs which are used as needed. A wq is NOT allocated when a dentry is first created but only when a second thread attempts to use the same name and so is forced to wait. At this moment a wq is chosen using a hash of the dentry pointer and that wq is assigned to ->d_wait. The ->d_lock is then dropped and the task waits.
When the dentry is finally moved out of "in_lookup" a wake up is only sent if ->d_wait is not NULL. This avoids an (uncontended) spin lock/unlock which saves a couple of atomic operations in a common case.
The wake up passes the dentry that the wake up is for as the "key" and the waiter will only wake processes waiting on the same key. This means that when these global waitqueues are shared (which is inevitable though unlikely to be frequent), a task will not be woken prematurely.
--- Documentation/filesystems/porting.rst | 6 +++ fs/afs/dir_silly.c | 4 +- fs/dcache.c | 78 ++++++++++++++++++++++----- fs/fuse/readdir.c | 3 +- fs/namei.c | 6 +-- fs/nfs/dir.c | 3 +- fs/nfs/unlink.c | 3 +- fs/proc/base.c | 3 +- fs/proc/proc_sysctl.c | 3 +- fs/proc/self.c | 3 +- fs/proc/thread_self.c | 4 +- fs/smb/client/readdir.c | 3 +- include/linux/dcache.h | 3 +- include/linux/nfs_xdr.h | 1 - 14 files changed, 82 insertions(+), 41 deletions(-)
Documentation/filesystems/porting.rst +6 -0
@@ -1265,3 +1265,9 @@ d_splice_alias() now works on a hashed dentry. d_drop() need not, and must not, be called before d_splice_alias(). In general d_drop() must not be called in a directory-modifying operation until the operation has completed - typically when it completes with failure. +--- + +** mandatory** + +d_alloc_parallel() no longer requires a waitqueue_head. It used one +from an internal table when needed.
fs/afs/dir_silly.c +1 -3
@@ -237,13 +237,11 @@ int afs_silly_iput(struct dentry *dentry, struct inode *inode)struct dentry *alias;int ret; -DECLARE_WAIT_QUEUE_HEAD_ONSTACK (wq); -_enter ("%p{%pd},%llx" , dentry, dentry, vnode->fid.vnode);down_read (&dvnode->rmdir_lock); - alias =d_alloc_parallel (dentry->d_parent, &dentry->d_name, &wq); + alias =d_alloc_parallel (dentry->d_parent, &dentry->d_name);if (IS_ERR (alias)) {up_read (&dvnode->rmdir_lock);return 0 ;
fs/dcache.c +64 -14
144 lines not on this page · raw message
fs/fuse/readdir.c +1 -2
@@ -160,7 +160,6 @@ static int fuse_direntplus_link(struct file *file,struct inode *dir =d_inode (parent);struct fuse_conn *fc;struct inode *inode; -DECLARE_WAIT_QUEUE_HEAD_ONSTACK (wq);int epoch;if (!o->nodeid) {
@@ -197,7 +196,7 @@ static int fuse_direntplus_link(struct file *file, dentry =d_lookup (parent, &name);if (!dentry) { retry: - dentry =d_alloc_parallel (parent, &name, &wq); + dentry =d_alloc_parallel (parent, &name);if (IS_ERR (dentry))return PTR_ERR (dentry); }
fs/namei.c +2 -4
@@ -1970,13 +1970,12 @@ static struct dentry *__lookup_slow(const struct qstr *name, {struct dentry *dentry, *old;struct inode *inode = dir->d_inode; -DECLARE_WAIT_QUEUE_HEAD_ONSTACK (wq);/* Don't go there if it's already dead */ if (unlikely (IS_DEADDIR (inode)))return ERR_PTR (-ENOENT ); again: - dentry =d_alloc_parallel (dir, name, &wq); + dentry =d_alloc_parallel (dir, name);if (IS_ERR (dentry))return dentry;if (unlikely (!d_in_lookup (dentry))) {
@@ -3953,7 +3952,6 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,struct dentry *dentry;int error, create_error =0 ;umode_t mode = op->mode; -DECLARE_WAIT_QUEUE_HEAD_ONSTACK (wq);if (unlikely (IS_DEADDIR (dir_inode)))return ERR_PTR (-ENOENT );
@@ -3962,7 +3960,7 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file, dentry =d_lookup (dir, &nd->last);for (;;) {if (!dentry) { - dentry =d_alloc_parallel (dir, &nd->last, &wq); + dentry =d_alloc_parallel (dir, &nd->last);if (IS_ERR (dentry))return dentry; }
fs/nfs/dir.c +1 -2
@@ -727,7 +727,6 @@ void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry, unsigned long dir_verifier) {struct qstr filename =QSTR_INIT (entry->name, entry->len); -DECLARE_WAIT_QUEUE_HEAD_ONSTACK (wq);struct dentry *dentry;struct dentry *alias;struct inode *inode;
@@ -756,7 +755,7 @@ void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry, dentry =d_lookup (parent, &filename); again:if (!dentry) { - dentry =d_alloc_parallel (parent, &filename, &wq); + dentry =d_alloc_parallel (parent, &filename);if (IS_ERR (dentry))return ; }
fs/nfs/unlink.c +1 -2
@@ -124,7 +124,7 @@ static int nfs_call_unlink(struct dentry *dentry, struct inode *inode, struct nfstruct dentry *alias;down_read_non_owner (&NFS_I (dir)->rmdir_sem); - alias =d_alloc_parallel (dentry->d_parent, &data->args.name, &data->wq); + alias =d_alloc_parallel (dentry->d_parent, &data->args.name);if (IS_ERR (alias)) {up_read_non_owner (&NFS_I (dir)->rmdir_sem);return 0 ;
@@ -185,7 +185,6 @@ nfs_async_unlink(struct dentry *dentry, const struct qstr *name) data->cred =get_current_cred (); data->res.dir_attr = &data->dir_attr; -init_waitqueue_head (&data->wq); status = -EBUSY ;spin_lock (&dentry->d_lock);
fs/proc/base.c +1 -2
@@ -2129,8 +2129,7 @@ bool proc_fill_cache(struct file *file, struct dir_context *ctx, child =try_lookup_noperm (&qname, dir);if (!child) { -DECLARE_WAIT_QUEUE_HEAD_ONSTACK (wq); - child =d_alloc_parallel (dir, &qname, &wq); + child =d_alloc_parallel (dir, &qname);if (IS_ERR (child)) goto end_instantiate;if (d_in_lookup (child)) {
fs/proc/proc_sysctl.c +1 -2
@@ -693,8 +693,7 @@ static bool proc_sys_fill_cache(struct file *file, child =d_lookup (dir, &qname);if (!child) { -DECLARE_WAIT_QUEUE_HEAD_ONSTACK (wq); - child =d_alloc_parallel (dir, &qname, &wq); + child =d_alloc_parallel (dir, &qname);if (IS_ERR (child))return false;if (d_in_lookup (child)) {
fs/proc/self.c +1 -2
@@ -37,11 +37,10 @@ static unsigned self_inum __ro_after_init;int proc_setup_self (struct super_block *s) {struct proc_fs_info *fs_info =proc_sb_info (s); -DECLARE_WAIT_QUEUE_HEAD_ONSTACK (wq);struct dentry *self;int ret = -ENOMEM ; - self =d_alloc_parallel (s->s_root, &QSTR_HASH (s->s_root,"self" ), &wq); + self =d_alloc_parallel (s->s_root, &QSTR_HASH (s->s_root,"self" ));if (self &&lock_and_check_dentry (self, s->s_root)) {struct inode *inode =new_inode (s);if (inode) {
fs/proc/thread_self.c +1 -3
@@ -38,13 +38,11 @@ static unsigned thread_self_inum __ro_after_init;int proc_setup_thread_self (struct super_block *s) {struct proc_fs_info *fs_info =proc_sb_info (s); -DECLARE_WAIT_QUEUE_HEAD_ONSTACK (wq);struct dentry *thread_self;int ret = -ENOMEM ; thread_self =d_alloc_parallel (s->s_root, - &QSTR_HASH (s->s_root,"thread-self" ), - &wq); + &QSTR_HASH (s->s_root,"thread-self" ));if (thread_self &&lock_and_check_dentry (thread_self, s->s_root)) {struct inode *inode =new_inode (s);if (inode) {
fs/smb/client/readdir.c +1 -2
@@ -74,7 +74,6 @@ cifs_prime_dcache(struct dentry *parent, struct qstr *name,struct cifs_sb_info *cifs_sb =CIFS_SB (sb);bool posix =cifs_sb_master_tcon (cifs_sb)->posix_extensions;bool reparse_need_reval = false; -DECLARE_WAIT_QUEUE_HEAD_ONSTACK (wq);int rc;cifs_dbg (FYI ,"%s: for %s\n" , __func__, name->name);
@@ -106,7 +105,7 @@ cifs_prime_dcache(struct dentry *parent, struct qstr *name, (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)) return; - dentry =d_alloc_parallel (parent, name, &wq); + dentry =d_alloc_parallel (parent, name); }if (IS_ERR (dentry))return ;
include/linux/dcache.h +1 -2
@@ -244,8 +244,7 @@ extern void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op/* allocate/de-allocate */ extern struct dentry *d_alloc (struct dentry *,const struct qstr *);extern struct dentry *d_alloc_anon (struct super_block *); -extern struct dentry *d_alloc_parallel (struct dentry *,const struct qstr *, -wait_queue_head_t *); +extern struct dentry *d_alloc_parallel (struct dentry *,const struct qstr *);extern struct dentry *d_splice_alias (struct inode *,struct dentry *);extern struct dentry *d_add_ci (struct dentry *,struct inode *,struct qstr *); extern bool d_same_name(const struct dentry *dentry, const struct dentry *parent,
include/linux/nfs_xdr.h +0 -1
@@ -1732,7 +1732,6 @@ struct nfs_unlinkdata {struct nfs_removeargs args;struct nfs_removeres res;struct dentry *dentry; -wait_queue_head_t wq;const struct cred *cred;struct nfs_fattr dir_attr;long timeout;
Signature · 1 line
2.49.0
PATCH 2/8 VFS: use d_alloc_parallel() in lookup_one_qstr_excl().
In reply to NeilBrownlookup_one_qstr_excl() is used for lookups prior to directory modifications, whether create, unlink, rename, or whatever.
To prepare for allowing modification to happen in parallel, change lookup_one_qstr_excl() to use d_alloc_parallel() and change its name to remove the _excl requirement.
If LOOKUP_EXCL or LOOKUP_RENAME_TARGET is passed, the caller must 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 one itself and *knows* that ->lookup() always completes the lookup (currently true for all filesystems other than NFS).
--- Documentation/filesystems/porting.rst | 11 +++++ fs/namei.c | 58 ++++++++++++++++++--------- 2 files changed, 51 insertions(+), 18 deletions(-)
Documentation/filesystems/porting.rst +11 -0
@@ -1271,3 +1271,14 @@ completed - typically when it completes with failure. d_alloc_parallel() no longer requires a waitqueue_head. It used one from an internal table when needed. + +--- + +** mandatory** + +All lookup_and_lock* functions may return a d_in_lookup() dentry if +passed "O_CREATE|O_EXCL" or "O_RENAME_TARGET". dentry_unlock() 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.
fs/namei.c +40 -18
146 lines not on this page · raw message
Signature · 1 line
2.49.0
PATCH 3/8 fs/proc: take rcu_read_lock() in proc_sys_compare()
In reply to NeilBrownproc_sys_compare() is the ->d_compare function for /proc/sys. It uses rcu_dereference() which assumes the RCU read lock is held and can complain if it isn't.
However there is no guarantee that this lock is held by d_same_name() (the caller of ->d_compare). In particularly d_alloc_parallel() calls d_same_name() after rcu_read_unlock().
So this patch calls rcu_read_lock() before accessing the inode (which seems to be the focus of RCU protection here), and drops it afterwards.
--- fs/proc/proc_sysctl.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-)
fs/proc/proc_sysctl.c +12 -8
@@ -916,19 +916,23 @@ static int proc_sys_compare(const struct dentry *dentry, {struct ctl_table_header *head;struct inode *inode; +int ret;/* Although proc doesn't have negative dentries, rcu-walk means * that inode here can be NULL */ /* AV: can it, indeed? */ +rcu_read_lock (); inode =d_inode_rcu (dentry); -if (!inode) -return 1 ; -if (name->len != len) -return 1 ; -if (memcmp (name->name, str, len)) -return 1 ; - head =rcu_dereference (PROC_I (inode)->sysctl); -return !head || !sysctl_is_seen (head); +if (!inode || + name->len != len || +memcmp (name->name, str, len)) { + ret =1 ; + }else { + head =rcu_dereference (PROC_I (inode)->sysctl); + ret = !head || !sysctl_is_seen (head); + } +rcu_read_unlock (); +return ret; }static const struct dentry_operations proc_sys_dentry_operations = {
Signature · 1 line
2.49.0
PATCH 4/8 VFS: Add ability to exclusively lock a dentry and use for open/create
In reply to NeilBrowndentry_lock(), dentry_trylock(), __dentry_unlock() are added which can be used to get an exclusive lock on a dentry in preparation for a directory operation on the name. It is planned for this to be used to provide name-based exclusive access to create (mkdir, mknod, symlink, link, create) a name, to remove (unlink, rmdir) a name, to rename between 2 names (rename) or to open (atomic_open) a name. This will allow i_rwsem to be deprecated for directories.
Note that lookup does not require this exclusion as it is only performed on d_in_lookup() dentries which already provide exclusive access.
As contention on a name is rare this locking is optimised for the uncontended case. A bit is set under the d_lock spinlock to claim a lock, and wait_var_event_spinlock() is used when waiting is needed. To avoid sending a wakeup when not needed we have a second bit flag to indicate if there are any waiters.
Once the exclusive lock is obtained on the dentry we must make sure it wasn't unlinked or renamed while we slept. If it was, dentry_lock() will fail so the (in-cache) lookup can be repeated.
For this patch the lock is only taken for "open" when a positive dentry is NOT found. This is currently uncontended as the i_rwsem is still held on the directory. A future patch will make that locking optional.
The notify_create() calls are moved into the region where the dentry is locked to ensure continuing exclusion.
--- fs/dcache.c | 26 ++++++- fs/internal.h | 20 +++++ fs/namei.c | 173 +++++++++++++++++++++++++++++++++++++++-- include/linux/dcache.h | 11 +++ 4 files changed, 221 insertions(+), 9 deletions(-)
fs/dcache.c +25 -1
@@ -1680,9 +1680,10 @@ EXPORT_SYMBOL(d_invalidate);* available. On a success the dentry is returned. The name passed in is * copied and the copy passed in may be reused after this call. */ - +static struct dentry *__d_alloc (struct super_block *sb,const struct qstr *name) { +static struct lock_class_key __key;struct dentry *dentry;char *dname;int err;
@@ -1740,6 +1741,8 @@ static struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)INIT_HLIST_NODE (&dentry->d_sib);d_set_d_op (dentry, dentry->d_sb->s_d_op); +lockdep_init_map (&dentry->dentry_map,"DCACHE_LOCK" , &__key,0 ); +if (dentry->d_op && dentry->d_op->d_init) { err = dentry->d_op->d_init (dentry);if (err) {
@@ -2988,6 +2991,10 @@ static int __d_unalias(struct dentry *dentry, struct dentry *alias)struct rw_semaphore *m2 =NULL ;int ret = -ESTALE ; +if (dentry->d_flags &DCACHE_LOCK ) { +if (!dentry_trylock (alias,NULL ,NULL )) +return ret; + }/* If alias and dentry share a parent, then no extra locks required */ if (alias->d_parent == dentry->d_parent) goto out_unalias;
@@ -3012,6 +3019,12 @@ static int __d_unalias(struct dentry *dentry, struct dentry *alias)up_read (m2);if (m1)mutex_unlock (m1); +if (dentry->d_flags &DCACHE_LOCK ) { +if (ret) +__dentry_unlock (alias); +else +__dentry_unlock (dentry); + }return ret; }
@@ -3033,6 +3046,9 @@ static int __d_unalias(struct dentry *dentry, struct dentry *alias)* If a dentry was found and moved, then it is returned. Otherwise NULL * is returned. This matches the expected return value of ->lookup. * +* If a different dentry is returned, any DCACHE_LOCK on the original +* dentry will be transferred to the new. +* * Cluster filesystems may call this function with a negative, hashed dentry. * In that case, we know that the inode will be a regular file, and also this * will only occur during atomic_open. So we need to check for the dentry
@@ -3074,7 +3090,15 @@ struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry) }dput (old_parent); }else { +if (dentry->d_flags &DCACHE_LOCK ) +/* This must succeed because IS_ROOT() dentries +* are never locked - except temporarily +* here while rename_lock is held. +*/ +dentry_trylock (new,NULL ,NULL );__d_move (new, dentry, false); +if (dentry->d_flags &DCACHE_LOCK ) +__dentry_unlock (dentry);write_sequnlock (&rename_lock); }iput (inode);
fs/internal.h +20 -0
@@ -228,6 +228,26 @@ extern struct dentry *__d_lookup_rcu(const struct dentry *parent, const struct qstr *name, unsigned *seq);extern void d_genocide (struct dentry *); +extern bool dentry_trylock (struct dentry *dentry, +struct dentry *base, +const struct qstr *last); + +/* +* The name "dentry_unlock()" is current used for unlocking the parent +* directory. Once we dispense with locking the dentry, that name can +* be used for this function. +*/ +static inline void __dentry_unlock (struct dentry *dentry) +{ +WARN_ON (!(dentry->d_flags &DCACHE_LOCK )); +lock_map_release (&dentry->dentry_map); +spin_lock (&dentry->d_lock); +if (dentry->d_flags &DCACHE_LOCK_WAITER ) +wake_up_var_locked (&dentry->d_flags, &dentry->d_lock); + dentry->d_flags &= ~(DCACHE_LOCK |DCACHE_LOCK_WAITER ); +spin_unlock (&dentry->d_lock); +} +/* * pipe.c */
fs/namei.c +165 -8
238 lines not on this page · raw message
include/linux/dcache.h +11 -0
@@ -114,6 +114,8 @@ struct dentry {* possible! */ +/* lockdep tracking of DCACHE_LOCK locks */ +struct lockdep_map dentry_map;union {struct list_head d_lru;/* LRU list */ wait_queue_head_t *d_wait;/* in-lookup ones only */
@@ -229,6 +231,15 @@ enum dentry_flags {#define DCACHE_MANAGED_DENTRY \ (DCACHE_MOUNTED|DCACHE_NEED_AUTOMOUNT|DCACHE_MANAGE_TRANSIT) +#define DCACHE_LOCK BIT(27)/* Locked for update */ +#define DCACHE_LOCK_WAITER BIT(28)/* someone is waiting for LOCK */ + +/* Nesting levels for DCACHE_LOCK */ +enum { +DLOCK_NORMAL , +DLOCK_RENAME ,/* dentry with higher address in rename */ +}; +extern seqlock_t rename_lock;/*
Signature · 1 line
2.49.0
PATCH 5/8 Introduce S_DYING which warns that S_DEAD might follow.
In reply to NeilBrownOnce we support directory operations (e.g. create) without requiring the parent to be locked, the current practice locking a directory while processing rmdir() or similar will not be sufficient to wait for operations to complete and to block further operations.
This patch introduced a new inode flag S_DYING. It indicates that a rmdir or similar is being processed and new directory operations must not commence in the directory. They should not abort either as the rmdir might fail - instead they should block. They can do this by waiting for a lock on the inode.
A new interface rmdir_lock() locks the inode, sets this flag, and waits for any children with DCACHE_LOCK set to complete their operation, and for any d_in_lookup() children to complete the lookup. It should be called before attempted to delete the directory or set S_DEAD. Matching rmdir_unlock() clears the flag and unlocks the inode.
dentry_lock() and d_alloc_parallel() are changed to block while this flag it set and to fail if the parent IS_DEADDIR(), though dentry_lock() doesn't block for d_in_lookup() dentries.
--- Documentation/filesystems/porting.rst | 10 ++ fs/btrfs/ioctl.c | 4 +- fs/configfs/dir.c | 24 ++--- fs/dcache.c | 26 ++++- fs/fuse/dir.c | 4 +- fs/internal.h | 1 + fs/libfs.c | 8 +- fs/namei.c | 139 ++++++++++++++++++++++++-- include/linux/dcache.h | 1 + include/linux/fs.h | 1 + include/linux/namei.h | 3 + 11 files changed, 191 insertions(+), 30 deletions(-)
Documentation/filesystems/porting.rst +10 -0
@@ -1282,3 +1282,13 @@ 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. + +--- + +*** mandatory** + +Code that might set %S_DEAD must use rmdir_lock() on the dentry rather +than inode_lock() on the inode. This both locks the inode and waits for +any child dentrys which are locked, to be unlocked. rmdir_unlock() is +then called after %S_DEAD has been set, or after it has been decided not +to set it.
fs/btrfs/ioctl.c +2 -2
@@ -2466,9 +2466,9 @@ static noinline int btrfs_ioctl_snap_destroy(struct file *file, goto out_unlock; } -btrfs_inode_lock (BTRFS_I (inode),0 ); +rmdir_lock (parent); ret =btrfs_delete_subvolume (BTRFS_I (dir), dentry); -btrfs_inode_unlock (BTRFS_I (inode),0 ); +rmdir_unlock (parent);if (!ret)d_delete_notify (dir, dentry);
fs/configfs/dir.c +12 -12
@@ -11,6 +11,7 @@#undef DEBUG#include <linux/fs.h> +#include <linux/namei.h> #include <linux/fsnotify.h> #include <linux/mount.h> #include <linux/module.h>
@@ -660,13 +661,11 @@ static void detach_groups(struct config_group *group) child = sd->s_dentry; -inode_lock (d_inode (child)); - +rmdir_lock (child);configfs_detach_group (sd->s_element);d_inode (child)->i_flags |=S_DEAD ;dont_mount (child); - -inode_unlock (d_inode (child)); +rmdir_unlock (child);d_delete (child);dput (child);
@@ -853,11 +852,11 @@ static int configfs_attach_item(struct config_item *parent_item,* the VFS may already have hit and used them. Thus, * we must lock them as rmdir() would. */ -inode_lock (d_inode (dentry)); +rmdir_lock (dentry);configfs_remove_dir (item);d_inode (dentry)->i_flags |=S_DEAD ;dont_mount (dentry); -inode_unlock (d_inode (dentry)); +rmdir_unlock (dentry);d_delete (dentry); } }
@@ -894,7 +893,7 @@ static int configfs_attach_group(struct config_item *parent_item,* We must also lock the inode to remove it safely in case of * error, as rmdir() would. */ -inode_lock_nested (d_inode (dentry),I_MUTEX_CHILD ); +rmdir_lock (dentry);configfs_adjust_dir_dirent_depth_before_populate (sd); ret =populate_groups (to_config_group (item), frag);if (ret) {
@@ -903,7 +902,7 @@ static int configfs_attach_group(struct config_item *parent_item,dont_mount (dentry); }configfs_adjust_dir_dirent_depth_after_populate (sd); -inode_unlock (d_inode (dentry)); +rmdir_unlock (dentry);if (ret)d_delete (dentry); }
@@ -1806,7 +1805,8 @@ void configfs_unregister_group(struct config_group *group) frag->frag_dead = true;up_write (&frag->frag_sem); -inode_lock_nested (d_inode (parent),I_MUTEX_PARENT ); +rmdir_lock (parent); +spin_lock (&configfs_dirent_lock);configfs_detach_prep (dentry,NULL );spin_unlock (&configfs_dirent_lock);
@@ -1816,7 +1816,7 @@ void configfs_unregister_group(struct config_group *group)dont_mount (dentry);d_drop (dentry);fsnotify_rmdir (d_inode (parent), dentry); -inode_unlock (d_inode (parent)); +rmdir_unlock (parent);dput (dentry);
@@ -1952,7 +1952,7 @@ void configfs_unregister_subsystem(struct configfs_subsystem *subsys)inode_lock_nested (d_inode (root),I_MUTEX_PARENT ); -inode_lock_nested (d_inode (dentry),I_MUTEX_CHILD ); +rmdir_lock (dentry);mutex_lock (&configfs_symlink_mutex);spin_lock (&configfs_dirent_lock);if (configfs_detach_prep (dentry,NULL )) {
@@ -1963,7 +1963,7 @@ void configfs_unregister_subsystem(struct configfs_subsystem *subsys)configfs_detach_group (&group->cg_item);d_inode (dentry)->i_flags |=S_DEAD ;dont_mount (dentry); -inode_unlock (d_inode (dentry)); +rmdir_unlock (dentry);d_drop (dentry);fsnotify_rmdir (d_inode (root), dentry);
fs/dcache.c +23 -3
@@ -2550,7 +2550,7 @@ static inline void end_dir_add(struct inode *dir, unsigned int n,d_wake_waiters (d_wait, de); } -static void d_wait_lookup (struct dentry *dentry) +void d_wait_lookup (struct dentry *dentry) {if (d_in_lookup (dentry)) { struct par_wait_key wk = {
@@ -2672,10 +2672,30 @@ struct dentry *d_alloc_parallel(struct dentry *parent,return dentry; }rcu_read_unlock (); -/* we can't take ->d_lock here; it's OK, though. */ - new->d_flags |=DCACHE_PAR_LOOKUP ; +/* +* we can't take ->d_lock here; it's OK, though as there is no concurrency. +* barrier ensure rmdir_lock() will see the flag or we will see S_DYING +*/ +smp_store_mb (new->d_flags, new->d_flags |DCACHE_PAR_LOOKUP );/* Don't set a wait_queue until someone is actually waiting */ new->d_wait =NULL ; +if (parent->d_inode->i_flags & (S_DYING |S_DEAD )) { + new->d_flags &=DCACHE_PAR_LOOKUP ; +hlist_bl_del (&new->d_u.d_in_lookup_hash); +hlist_bl_unlock (b); +/* rmdir_lock() might be waiting already ! */ +__d_lookup_unhash_wake (new); +if (!(parent->d_inode->i_flags &S_DEAD )) { +inode_lock (parent->d_inode); +/* S_DYING must be clean now */ +inode_unlock (parent->d_inode); + } +if (parent->d_inode->i_flags &S_DEAD ) { +dput (new); +return ERR_PTR (-ENOENT ); + } + goto retry; + }hlist_bl_add_head (&new->d_u.d_in_lookup_hash, b);hlist_bl_unlock (b);return new;
fs/fuse/dir.c +2 -2
@@ -1427,7 +1427,7 @@ int fuse_reverse_inval_entry(struct fuse_conn *fc, u64 parent_nodeid,fuse_invalidate_entry_cache (entry);if (child_nodeid !=0 &&d_really_is_positive (entry)) { -inode_lock (d_inode (entry)); +rmdir_lock (entry);if (get_node_id (d_inode (entry)) != child_nodeid) { err = -ENOENT ; goto badentry;
@@ -1448,7 +1448,7 @@ int fuse_reverse_inval_entry(struct fuse_conn *fc, u64 parent_nodeid,clear_nlink (d_inode (entry)); err =0 ; badentry: -inode_unlock (d_inode (entry)); +rmdir_unlock (entry);if (!err)d_delete (entry); }else {
fs/internal.h +1 -0
@@ -231,6 +231,7 @@ extern void d_genocide(struct dentry *);extern bool dentry_trylock (struct dentry *dentry,struct dentry *base,const struct qstr *last); +void d_wait_lookup (struct dentry *dentry);/* * The name "dentry_unlock()" is current used for unlocking the parent
fs/libfs.c +6 -2
@@ -613,9 +613,13 @@ void simple_recursive_removal(struct dentry *dentry,struct dentry *victim =NULL , *child;struct inode *inode = this->d_inode; -inode_lock (inode); -if (d_is_dir (this)) +if (d_is_dir (this)) { +rmdir_lock (this); inode->i_flags |=S_DEAD ; +rmdir_unlock (this); + } + +inode_lock (inode);while ((child =find_next_child (this, victim)) ==NULL ) {// kill and ascend // update metadata while it's still locked
fs/namei.c +130 -9
188 lines not on this page · raw message
include/linux/dcache.h +1 -0
@@ -238,6 +238,7 @@ enum dentry_flags {enum {DLOCK_NORMAL ,DLOCK_RENAME ,/* dentry with higher address in rename */ +DLOCK_DYING_WAIT ,/* child of a locked parent that is dying */ };extern seqlock_t rename_lock;
include/linux/fs.h +1 -0
@@ -2395,6 +2395,7 @@ struct super_operations {#define S_CASEFOLD (1 << 15)/* Casefolded file */ #define S_VERITY (1 << 16)/* Verity file (using fs/verity/) */ #define S_KERNEL_FILE (1 << 17)/* File is in use by the kernel (eg. fs/cachefiles) */ +#define S_DYING (1 << 18)/* dir is locked ready to set S_DEAD */ #define S_ANON_INODE (1 << 19)/* Inode is an anonymous inode */ /*
include/linux/namei.h +3 -0
@@ -90,6 +90,9 @@ struct dentry *lookup_and_lock_hashed(struct qstr *last, struct dentry *base,void dentry_unlock (struct dentry *dentry);void dentry_unlock_dir_locked (struct dentry *dentry); +void rmdir_lock (struct dentry *dentry); +void rmdir_unlock (struct dentry *dentry); +extern int follow_down_one (struct path *);extern int follow_down (struct path *path,unsigned int flags);extern int follow_up (struct path *);
Signature · 1 line
2.49.0
Al Viro
maintainerRe: [PATCH 5/8] Introduce S_DYING which warns that S_DEAD might follow.
Quoted 38 lines · NeilBrown
Once we support directory operations (e.g. create) without requiring the parent to be locked, the current practice locking a directory while processing
rmdir()or similar will not be sufficient to wait for operations to complete and to block further operations.This patch introduced a new inode flag
S_DYING. It indicates that a rmdir or similar is being processed and new directory operations must not commence in the directory. They should not abort either as the rmdir might fail - instead they should block. They can do this by waiting for a lock on the inode.A new interface
rmdir_lock()locks the inode, sets this flag, and waits for any children withDCACHE_LOCKset to complete their operation, and for anyd_in_lookup()children to complete the lookup. It should be called before attempted to delete the directory or setS_DEAD. Matchingrmdir_unlock()clears the flag and unlocks the inode.
dentry_lock()andd_alloc_parallel()are changed to block while this flag it set and to fail if the parentIS_DEADDIR(), thoughdentry_lock()doesn't block ford_in_lookup()dentries.fs/namei.c +3
-0@@ -1770,8 +1770,11 @@ static bool __dentry_lock(struct dentry *dentry, struct dentry *base, const struct qstr *last, unsigned int subclass, int state) { + struct dentry *parent; +struct inode *dir;int err; +retry:lock_acquire_exclusive (&dentry->dentry_map, subclass,0 ,NULL , _THIS_IP_);spin_lock (&dentry->d_lock); err = wait_var_event_any_lock(&dentry->d_flags,
fs/namei.c static bool __dentry_lock(struct dentry *dentry, · line 1788
Quoted 6 lines
fs/namei.c +1
-2@@ -1782,10 +1785,43 @@ static bool __dentry_lock(struct dentry *dentry,spin_unlock (&dentry->d_lock);return false; } - - dentry->d_flags |=DCACHE_LOCK ; + parent = dentry->d_parent;
Why will it stay the parent? Matter of fact, why will it stay positive?
fs/namei.c static bool __dentry_lock(struct dentry *dentry, · line 1809
Quoted 1 line
fs/namei.c +1
-0+ dir =igrab (parent->d_inode);
... and not oops right here?
Quoted 6 lines
fs/namei.c +4
-1+lock_map_release (&dentry->dentry_map);spin_unlock (&dentry->d_lock); -return true; + +if (state ==TASK_KILLABLE ) { + err =down_write_killable (&dir->i_rwsem);
fs/namei.c static bool __dentry_lock(struct dentry *dentry, · line 1824
Quoted 10 lines
fs/namei.c +10
-0+if (err) { +iput (dir); +return false; + } + }else +inode_lock (dir); +/* S_DYING much be clear now */ +inode_unlock (dir); +iput (dir); + goto retry;
OK, I'm really confused now. Is it allowed to call dentry_lock() while holding ->i_rwsem of the parent?
Where does your dentry lock nest wrt ->i_rwsem? As a bonus (well, malus, I guess) question, where does it nest wrt parent *and* child inodes' ->i_rwsem for rmdir and rename?
Tangentially connected question: which locks are held for ->unlink() in your scheme? You do need *something* on the victim inode to protect ->i_nlink modifications, and anything on dentries of victim or their parent directories is not going to give that.
Re: [PATCH 5/8] Introduce S_DYING which warns that S_DEAD might follow.
Quoted 34 lines · Al Viro
Quoted 33 lines · NeilBrown
Once we support directory operations (e.g. create) without requiring the parent to be locked, the current practice locking a directory while processing
rmdir()or similar will not be sufficient to wait for operations to complete and to block further operations.This patch introduced a new inode flag
S_DYING. It indicates that a rmdir or similar is being processed and new directory operations must not commence in the directory. They should not abort either as the rmdir might fail - instead they should block. They can do this by waiting for a lock on the inode.A new interface
rmdir_lock()locks the inode, sets this flag, and waits for any children withDCACHE_LOCKset to complete their operation, and for anyd_in_lookup()children to complete the lookup. It should be called before attempted to delete the directory or setS_DEAD. Matchingrmdir_unlock()clears the flag and unlocks the inode.
dentry_lock()andd_alloc_parallel()are changed to block while this flag it set and to fail if the parentIS_DEADDIR(), thoughdentry_lock()doesn't block ford_in_lookup()dentries.fs/namei.c +2
-0@@ -1770,8 +1770,11 @@ static bool __dentry_lock(struct dentry *dentry, struct dentry *base, const struct qstr *last, unsigned int subclass, int state) { + struct dentry *parent; +struct inode *dir;int err;
Quoted 13 lines
fs/namei.c static bool __dentry_lock(struct dentry *dentry, · line 1788
Quoted 11 lines
fs/namei.c +2
-2+retry:lock_acquire_exclusive (&dentry->dentry_map, subclass,0 ,NULL , _THIS_IP_);spin_lock (&dentry->d_lock); err = wait_var_event_any_lock(&dentry->d_flags,@@ -1782,10 +1785,43 @@ static bool __dentry_lock(struct dentry *dentry,spin_unlock (&dentry->d_lock);return false; } - - dentry->d_flags |=DCACHE_LOCK ; + parent = dentry->d_parent;Why will it stay the parent? Matter of fact, why will it stay positive?
As long as we continue to hold dentry->d_lock it will stay the parent, and so will have a reference, and so will stay positive.
Quoted 4 lines
fs/namei.c static bool __dentry_lock(struct dentry *dentry, · line 1809
Quoted 2 lines
fs/namei.c +1
-0+ dir =igrab (parent->d_inode);... and not oops right here?
Still holding dentry->d_lock here so parent cannot have changed.
Quoted 12 lines
Quoted 12 lines
fs/namei.c +9
-1+lock_map_release (&dentry->dentry_map);spin_unlock (&dentry->d_lock); -return true; + +if (state ==TASK_KILLABLE ) { + err =down_write_killable (&dir->i_rwsem); +if (err) { +iput (dir); +return false; + } + }else
Quoted 8 lines
fs/namei.c static bool __dentry_lock(struct dentry *dentry, · line 1824
Quoted 5 lines
fs/namei.c +5
-0+inode_lock (dir); +/* S_DYING much be clear now */ +inode_unlock (dir); +iput (dir); + goto retry;OK, I'm really confused now. Is it allowed to call
dentry_lock()while holding ->i_rwsem of the parent?
Yes.
Quoted 4 lines
Where does your dentry lock nest wrt ->i_rwsem? As a bonus (well, malus, I guess) question, where does it nest wrt parent *and* child inodes' ->i_rwsem for rmdir and rename?
Between inode of parent of the dentry and inode of the dentry.
In this case we aren't holding the dentry lock when we lock the parent. We might already have the parent locked when calling dentry_lock() if the filesystem hasn't opted out) but in that case S_DYING will not be set. It is only set while holding the i_rw_sem in case which doesn't lock dentries. So if S_DYING is set here, then it must be safe to lock the parent.
Quoted 6 lines
Tangentially connected question: which locks are held for ->unlink() in your scheme? You do need *something* on the victim inode to protect ->i_nlink modifications, and anything on dentries of victim or their parent directories is not going to give that.
I haven't change the locking on non-directories at all. The target of ->unlink() will be locked after the dentry is locked (which is after the parent is locked if the fs requires that).
->i_nlink for non-directories is still protected by ->i_rwsem on the inode.
->i_nlink for directories is something the fs will have to handle when opting out of i_rwsem on directory ops. NFS, for example, already takes inode->i_lock when calling inc_nlink() or drop_nlink(). The set_nlink() in nfs_update_inode() isn't obviously protected but as the number is informational it possibly doesn't matter.
Thanks,
NeilBrown
Al Viro
maintainerRe: [PATCH 5/8] Introduce S_DYING which warns that S_DEAD might follow.
Quoted 8 lines · NeilBrown
Yes.
Quoted 4 lines
Where does your dentry lock nest wrt ->i_rwsem? As a bonus (well, malus, I guess) question, where does it nest wrt parent *and* child inodes' ->i_rwsem for rmdir and rename?
Between inode of parent of the dentry and inode of the dentry.
That's... going to be fun to prove the deadlock avoidance. Looking forward to such proof...
Look, the reason why I'm sceptical is that we had quite a few interesting problems with directory locking schemes; fun scenarios are easy to miss and I've fucked up more than a few times in that area. Fixing it afterwards can be a real bitch, especially if we get filesystem-specific parts in the picture.
So let's sort it out _before_ we go there. And I mean proof - verifiable statements about the functions, etc.
Incidentally, what was the problem with having dentry locked before the parent? At least that way we would have a reasonable lock ordering... It would require some preliminary work, but last time I looked at the area (not very deeply) it felt like a plausible direction... I wonder which obstacle have I missed...
Re: [PATCH 5/8] Introduce S_DYING which warns that S_DEAD might follow.
Quoted 5 lines · Al Viro
Quoted 3 lines · NeilBrown
Yes.
Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Where does your dentry lock nest wrt ->i_rwsem? As a bonus (well, malus, I guess) question, where does it nest wrt parent *and* child inodes' ->i_rwsem for rmdir and rename?
Between inode of parent of the dentry and inode of the dentry.
That's... going to be fun to prove the deadlock avoidance. Looking forward to such proof...
I do hope to write something along those lines, but I'd rather do it after getting feedback on the design. It's already changed several times and while I have growing confidence that I understand the key issues there is still room for change as discussed below.
Quoted 6 lines
Look, the reason why I'm sceptical is that we had quite a few interesting problems with directory locking schemes; fun scenarios are easy to miss and I've fucked up more than a few times in that area. Fixing it afterwards can be a real bitch, especially if we get filesystem-specific parts in the picture.
That's part of why I like bringing all the locking into namei.c using lookup_and_lock() etc. Having everything in one place won't make it easy but might make it more tractable.
Quoted 3 lines
So let's sort it out _before_ we go there. And I mean proof - verifiable statements about the functions, etc.
I was hoping to get some of the refactoring - which I think it useful in any case - in before needing a complete tidy solution. Partly this is because I thought the review discussion of the locking would be more effective when the code was clean and centralised....
Quoted 7 lines
Incidentally, what was the problem with having dentry locked before the parent? At least that way we would have a reasonable lock ordering... It would require some preliminary work, but last time I looked at the area (not very deeply) it felt like a plausible direction... I wonder which obstacle have I missed...
If we are to put the parent locking after the dentry locking it must also be after the d_alloc_parallel() locking. As some readdir implementations (quite sensibly) use d_alloc_parallel() to prime the dcache with readdir results, we would not be able to hold the parent locked across readdir. So we would need some other exclusion with rmdir.
Put another way: if we want to push the parent locking down into the filesystem for anything, we really need to do it for everything. For rmdir we would need the "set S_DYING and wait for locked dentries" to happen before taking the lock, and we cannot use parent lock for readdir.
Maybe we could do that, but we can't use spare d_flags or i_flags for the readdir locking - we need a counter. Maybe i_writecount or i_dio_count could provide the space we need as they aren't used on directories.
I thought that going down that path added more complexity than I wanted, but it does have the advantage of making the purpose and scope of the different locks more explicit.
... or maybe we could change those readdir routines to do a try-lock. i.e. have a d_alloc_parallel() variant which returned -EWOULDBLOCK rather than waiting for the dentry to be ready. Maybe that is a sensible approach anyway...
Another (unrelated) option that I've considered but not yet explored is using the same locking scheme for in_lookup dentries and for the targets of operations. There would be just two DCACHE flags (locked, and waiter-exists) and the in_lookup dentries would be recognised by having d_inode being $RESERVED_ADDRESS. Then dentries would always be created locked and then unlocked when they are ready (like inodes). I think I like this, but I also wonder if it is worth the churn.
NeilBrown
PATCH 6/8 VFS: provide alternative to s_vfs_rename_mutex
In reply to NeilBrowns_vfs_rename_mutex plays a few different roles in managing renames.
1/ It stablises all ->d_parent pointers in the filesystem so that
ancestral relationships between the source and target directories can be determined.
2/ It prevents any other cross-directory rename so that if the
source and target directories do not have an ancestral relationship then they can be locked in any order without risk if AB-BA deadlock with another rename
3/ It prevents any other cross-directory rename so that the validation
of ancestral relationship between the source and target which ensures no loop is created remains in effect until the rename has completed in the filesystem.
A future patch will introduce a new locking mechanism that will allow filesystems to support concurrent directory operations where they do not directly conflict. The use of s_vfs_rename_mutex reduces the opportunity for concurrency so this patch introduces a new mechanism. The exist mechanism will remain but only be used for filesystems that haven't advertised support for concurrent operations.
The new mechanism depends on rename_lock and locks against rename only the ancestors of the two targets that are different.
A/ rename_lock is used to stablise all ->d_parent pointers. While it is
held the nearest common ancestor to both targets is found. If this either the source or destination dentry an error is reported.
B/ While still holding rename_lock, DCACHE_RENAME_LOCK is set on all
dentries from the two targets up to, but not including, the common ancestor. If any already have DCACHE_RENAME_LOCK set, we abort before setting the flag anywhere, wait for that dentry to lose DCACHE_RENAME_LOCK, then try again.
DCACHE_RENAME_LOCK ensures that the name and parent of the dentry is not changed. DCACHE_RENAME_LOCK of the whole path up to the common ancestor ensures that these paths don't change so the ancestral relationship between the two targets don't change. This ensures that no other rename can cause this one to create a path loop.
Waiting for DCACHE_RENAME_LOCK to be cleared uses a single per-filesystem wait_queue_head. This is signalled once all DCACHE_RENAME_LOCK flags have been cleared. This ensures waiters aren't woken prematurely if they were only waiting on one other rename.
An important consideration when clearing the DCACHE_RENAME_LOCK flag is that we do this from child to parent. As we walk up we need to be sure we have a valid reference to each dentry. We will because we have a counted reference to old_dir and new_dentry. As none of these directories are empty they cannot be removed and they cannot be moved away as long as we continue to hold rename_lock. So as long as we hold rename_lock while clearing all the DCACHE_RENAME_LOCK flags we will have a usable reference through the chain of ->d_parent links. As soon as rename_lock is dropped the ancestor and everything below that we do not have an explicit counted reference on will become inaccessible.
--- fs/namei.c | 160 ++++++++++++++++++++++++++++++++++++++++- include/linux/dcache.h | 3 + include/linux/fs.h | 8 ++- 3 files changed, 168 insertions(+), 3 deletions(-)
fs/namei.c +159 -1
@@ -3722,6 +3722,151 @@ static void unlock_rename(struct dentry *p1, struct dentry *p2) } } +static void wait_not_renaming (struct dentry *d) +{ +spin_lock (&d->d_lock); +wait_event_cmd (d->d_sb->s_vfs_rename_wq, !(d->d_flags &DCACHE_RENAME_LOCK ), +spin_unlock (&d->d_lock),spin_lock (&d->d_lock)); +spin_unlock (&d->d_lock); +} + +static void renaming_unlock_one (struct dentry *d) +{ +spin_lock (&d->d_lock); +WARN_ON_ONCE (!(d->d_flags &DCACHE_RENAME_LOCK )); + d->d_flags &= ~DCACHE_RENAME_LOCK ; +spin_unlock (&d->d_lock); +} + +static void renaming_unlock (struct dentry *dir1,struct dentry *dir2, +struct dentry *ancestor, +struct dentry *d1,struct dentry *d2) +{ +struct dentry *d; + +if (!ancestor) +return ; + +read_seqlock_excl (&rename_lock); +for (d = dir1; d != ancestor; d = d->d_parent) +renaming_unlock_one (d); +for (d = dir2; d != ancestor; d = d->d_parent) +renaming_unlock_one (d); +renaming_unlock_one (d1); +renaming_unlock_one (d2); +read_sequnlock_excl (&rename_lock); +/* We don't send any wakeup until all dentries have been unlocked */ +wake_up (&dir1->d_sb->s_vfs_rename_wq); +} + +/* +* Find the nearest common ancestor of d1 and d2, and report +* if any of the children of the ancestor which are in the line +* to d1 and d2 are locked for rename - have DCACHE_RENAME_LOCK set. +*/ +static struct dentry *common_ancestor (struct dentry *d1,struct dentry *d2, +struct dentry **locked) +{ +struct dentry *p, *q; +int depth1 =0 , depth2 =0 ; + + *locked =NULL ; +/* Find depth of each and compare ancestors of equal depth */ +for (p = d1; !IS_ROOT (p); p = p->d_parent) + depth1 +=1 ; +for (q = d2; !IS_ROOT (q); q = q->d_parent) + depth2 +=1 ; +if (p != q) +/* Different root! */ +return NULL ; +while (depth1 > depth2) { +if (d1->d_flags &DCACHE_RENAME_LOCK ) + *locked = d1; + d1 = d1->d_parent; + depth1 -=1 ; + } +while (depth2 > depth1) { +if (d2->d_flags &DCACHE_RENAME_LOCK ) + *locked = d2; + d2 = d2->d_parent; + depth2 -=1 ; + } +while (d1 != d2) { +if (d1->d_flags &DCACHE_RENAME_LOCK ) + *locked = d1; +if (d2->d_flags &DCACHE_RENAME_LOCK ) + *locked = d2; + d1 = d1->d_parent; + d2 = d2->d_parent; + } +return d1; +} + +/* +* Lock all ancestors below nearest common ancestor for rename +* Returns: +* -EXDEV if there is no common ancestor +* -EINVAL if the first dentry is the common ancestor - +* you cannot move a directory into a descendent +* -ENOTEMPTY if the second dentry is the common ancestor - +* the target directory must usually be empty. +* Note that these errors might be adjusted for RENAME_EXCHANGE +* +* The ancestors will be locked against rename, and no directory between +* either target and the ancestor will be the ancestor of an active +* rename. This ensures that the common ancestor will continue to be +* the common ancestor, and that there will be no concurrent rename with +* the same ancestor. +*/ +static struct dentry *lock_ancestors (struct dentry *d1,struct dentry *d2) +{ +struct dentry *locked, *ancestor; + +if (d1->d_parent == d2->d_parent) +/* Nothing to lock */ +return NULL ; +again: +read_seqlock_excl (&rename_lock); + ancestor =common_ancestor (d1, d2, &locked); +if (!ancestor) { +read_sequnlock_excl (&rename_lock); +return ERR_PTR (-EXDEV ); + } +if (ancestor == d1) { +read_sequnlock_excl (&rename_lock); +return ERR_PTR (-EINVAL ); + } +if (ancestor == d2) { +read_sequnlock_excl (&rename_lock); +return ERR_PTR (-ENOTEMPTY ); + } +if (locked) { +dget (locked); +read_sequnlock_excl (&rename_lock); +wait_not_renaming (locked); +dput (locked); + goto again; + } +/* +* Nothing from d1,d2 up to ancestor can have DCACHE_RENAME_LOCK +* as we hold rename_lock and nothing was reported in "locked". +*/ +while (d1 != ancestor) { +spin_lock (&d1->d_lock); + d1->d_flags |=DCACHE_RENAME_LOCK ; +spin_unlock (&d1->d_lock); + d1 = d1->d_parent; + } +while (d2 != ancestor) { +spin_lock (&d2->d_lock); + d2->d_flags |=DCACHE_RENAME_LOCK ; +spin_unlock (&d2->d_lock); + d2 = d2->d_parent; + } +read_sequnlock_excl (&rename_lock); +return ancestor; +} +/** * lookup_and_lock_rename_hashed - lookup and lock names for rename * @rd: rename data containing relevant details
@@ -3753,7 +3898,7 @@ static void unlock_rename(struct dentry *p1, struct dentry *p2)int lookup_and_lock_rename_hashed (struct renamedata *rd,int lookup_flags) { -struct dentry *p; +struct dentry *p, *ancestor;struct dentry *d1, *d2;int target_flags =LOOKUP_RENAME_TARGET |LOOKUP_CREATE ;int err;
@@ -3811,8 +3956,17 @@ lookup_and_lock_rename_hashed(struct renamedata *rd, int lookup_flags) goto out_unlock_3; } + ancestor =lock_ancestors (d1, d2); +if (IS_ERR (ancestor)) { + err =PTR_ERR (ancestor); +if (err == -ENOTEMPTY && (rd->flags &RENAME_EXCHANGE )) + err = -EINVAL ; + goto out_unlock_3; + } + rd->old_dentry = d1; rd->new_dentry = d2; + rd->ancestor = ancestor;return 0 ; out_unlock_3:
@@ -3934,6 +4088,10 @@ void dentry_unlock_rename(struct renamedata *rd) {d_lookup_done (rd->old_dentry);d_lookup_done (rd->new_dentry); + +renaming_unlock (rd->old_dir, rd->new_dir, rd->ancestor, + rd->old_dentry, rd->new_dentry); +unlock_rename (rd->old_dir, rd->new_dir);dput (rd->old_dir);dput (rd->old_dentry);
include/linux/dcache.h +3 -0
@@ -226,6 +226,9 @@ enum dentry_flags {DCACHE_PAR_LOOKUP =BIT (24 ),/* being looked up (with parent locked shared) */ DCACHE_DENTRY_CURSOR =BIT (25 ),DCACHE_NORCU =BIT (26 ),/* No RCU delay for freeing */ + DCACHE_RENAME_LOCK = BIT(27), /* A directory is being renamed + * into or out-of this tree + */ };#define DCACHE_MANAGED_DENTRY \
include/linux/fs.h +6 -2
@@ -1402,10 +1402,11 @@ struct super_block {unsigned int s_max_links;/* -* The next field is for VFS *only*. No filesystems have any business -* even looking at it. You had been warned. +* The next two fields are for VFS *only*. No filesystems have any business +* even looking at them. You had been warned. */ struct mutex s_vfs_rename_mutex;/* Kludge */ +struct wait_queue_head s_vfs_rename_wq;/* waiting for DCACHE_RENAME_LOCK to clear */ /* * Filesystem subtype. If non-empty the filesystem type field
@@ -2012,6 +2013,8 @@ int vfs_unlink(struct mnt_idmap *, struct inode *, struct dentry *,* @new_dentry: destination * @new_last: name for new_dentry in new_dir, if new_dentry not given * @delegated_inode: returns an inode needing a delegation break +* @ancestor: Closest common ancestor of @old_dir and @new_dir if those +* two are differernt. * @flags: rename flags */ struct renamedata {
@@ -2024,6 +2027,7 @@ struct renamedata {struct dentry *new_dentry;struct qstr new_last;struct inode **delegated_inode; +struct dentry *ancestor;unsigned int flags; } __randomize_layout;
Signature · 1 line
2.49.0
PATCH 7/8 VFS: use new dentry locking for create/remove/rename
In reply to NeilBrownAfter taking the directory lock (or locks) we now lock the target dentries. This is pointless at present but will allow us to remove the taking of the directory lock in a future patch.
MORE WORDS
--- fs/afs/dir.c | 3 +- fs/afs/dir_silly.c | 2 +- fs/namei.c | 140 ++++++++++++++++++++++++++++++++++++----- fs/nfs/unlink.c | 2 +- include/linux/dcache.h | 3 + include/linux/namei.h | 4 +- 6 files changed, 133 insertions(+), 21 deletions(-)
fs/afs/dir.c +2 -1
@@ -943,7 +943,8 @@ static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry) }strcpy (p, name); - ret =lookup_and_lock_noperm_locked (&QSTR (buf), dentry->d_parent); + ret =lookup_and_lock_noperm_locked (&QSTR (buf), dentry->d_parent,0 , +DLOCK_SUB_LOOKUP );if (!IS_ERR (ret) &&d_is_positive (ret))dget (ret);dentry_unlock (ret);
fs/afs/dir_silly.c +1 -1
@@ -120,7 +120,7 @@ int afs_sillyrename(struct afs_vnode *dvnode, struct afs_vnode *vnode,scnprintf (silly,sizeof (silly),".__afs%04X" , sillycounter); sdentry =lookup_and_lock_noperm_locked ( &QSTR (silly), dentry->d_parent, -LOOKUP_CREATE |LOOKUP_EXCL ); +LOOKUP_CREATE |LOOKUP_EXCL ,DLOCK_SUB_LOOKUP );/* N.B. Better to return EBUSY here ... it could be dangerous * to delete the file while it's in use.
fs/namei.c +123 -17
259 lines not on this page · raw message
fs/nfs/unlink.c +1 -1
@@ -476,7 +476,7 @@ nfs_sillyrename(struct inode *dir, struct dentry *dentry)*/ sdentry =lookup_and_lock_noperm_locked ( &QSTR (silly), dentry->d_parent, -LOOKUP_CREATE ); +LOOKUP_CREATE ,DLOCK_SUB_LOOKUP );if (!IS_ERR (sdentry) &&d_is_positive (dentry)) {dput (sdentry); sdentry =ERR_PTR (-EEXIST );
include/linux/dcache.h +3 -0
@@ -242,6 +242,9 @@ enum {DLOCK_NORMAL ,DLOCK_RENAME ,/* dentry with higher address in rename */ DLOCK_DYING_WAIT ,/* child of a locked parent that is dying */ + DLOCK_SUB_LOOKUP, /* lock on target for silly-rename or other + * subordinate lookup + */ };extern seqlock_t rename_lock;
include/linux/namei.h +3 -1
@@ -83,8 +83,10 @@ struct dentry *lookup_and_lock_killable(struct mnt_idmap *idmap, unsigned int lookup_flags);struct dentry *lookup_and_lock_noperm (struct qstr *name,struct dentry *base,unsigned int lookup_flags); +struct dentry *lookup_and_lock_noperm_nested (struct qstr *name,struct dentry *base, +unsigned int lookup_flags,unsigned int class);struct dentry *lookup_and_lock_noperm_locked (struct qstr *name,struct dentry *base, -unsigned int lookup_flags); +unsigned int lookup_flags,unsigned int class);struct dentry *lookup_and_lock_hashed (struct qstr *last,struct dentry *base,unsigned int lookup_flags);void dentry_unlock (struct dentry *dentry);
Signature · 1 line
2.49.0
Al Viro
maintainerRe: [PATCH 7/8] VFS: use new dentry locking for create/remove/rename
Quoted 5 lines · NeilBrown
After taking the directory lock (or locks) we now lock the target dentries. This is pointless at present but will allow us to remove the taking of the directory lock in a future patch.
MORE WORDS
Such as "why doesn't it deadlock?", presumably, seeing that you have
fs/namei.c struct dentry *lookup_and_lock_hashed(struct qstr *last, · line 2009
Quoted 8 lines
fs/namei.c +3
-0@@ -2003,7 +2003,14 @@ struct dentry *lookup_and_lock_hashed(struct qstr *last,inode_lock_nested (base->d_inode,I_MUTEX_PARENT ); +retry: dentry =lookup_one_qstr (last, base, lookup_flags); +if (!IS_ERR (dentry) && + !dentry_lock (dentry, base, last,TASK_UNINTERRUPTIBLE )) {
... take dentry lock inside ->i_rwsem on parent and
Quoted 8 lines
fs/namei.c +1
-5bool lock_and_check_dentry (struct dentry *child,struct dentry *parent) { -inode_lock_nested (d_inode (parent),I_MUTEX_PARENT ); -if (child->d_parent == parent) { -/* get the child to balance with dentry_unlock which puts it. */ -dget (child); -return true; +if (!dentry_lock (child,NULL ,NULL ,TASK_UNINTERRUPTIBLE ))
fs/namei.c EXPORT_SYMBOL(lookup_and_lock_killable); · line 2208
Quoted 10 lines
fs/namei.c +7
-2+return false; +if (child->d_parent != parent) { +__dentry_unlock (child); +return false; } -inode_unlock (d_inode (parent)); -return false; +/* get the child to balance with dentry_unlock() which puts it. */ +dget (child); +inode_lock_nested (d_inode (parent),I_MUTEX_PARENT );
... do the same in opposite order?
How could that possibly work?
Re: [PATCH 7/8] VFS: use new dentry locking for create/remove/rename
Quoted 41 lines · Al Viro
Quoted 5 lines · NeilBrown
After taking the directory lock (or locks) we now lock the target dentries. This is pointless at present but will allow us to remove the taking of the directory lock in a future patch.
MORE WORDS
Such as "why doesn't it deadlock?", presumably, seeing that you have
fs/namei.c struct dentry *lookup_and_lock_hashed(struct qstr *last, · line 2009
Quoted 8 lines
fs/namei.c +3
-0@@ -2003,7 +2003,14 @@ struct dentry *lookup_and_lock_hashed(struct qstr *last,inode_lock_nested (base->d_inode,I_MUTEX_PARENT ); +retry: dentry =lookup_one_qstr (last, base, lookup_flags); +if (!IS_ERR (dentry) && + !dentry_lock (dentry, base, last,TASK_UNINTERRUPTIBLE )) {... take dentry lock inside ->i_rwsem on parent and
Quoted 8 lines
fs/namei.c +1
-5bool lock_and_check_dentry (struct dentry *child,struct dentry *parent) { -inode_lock_nested (d_inode (parent),I_MUTEX_PARENT ); -if (child->d_parent == parent) { -/* get the child to balance with dentry_unlock which puts it. */ -dget (child); -return true; +if (!dentry_lock (child,NULL ,NULL ,TASK_UNINTERRUPTIBLE ))fs/namei.c EXPORT_SYMBOL(lookup_and_lock_killable); · line 2208
Quoted 10 lines
fs/namei.c +7
-2+return false; +if (child->d_parent != parent) { +__dentry_unlock (child); +return false; } -inode_unlock (d_inode (parent)); -return false; +/* get the child to balance with dentry_unlock() which puts it. */ +dget (child); +inode_lock_nested (d_inode (parent),I_MUTEX_PARENT );... do the same in opposite order?
Quoted 2 lines
How could that possibly work?
Clearly it can't - thanks for finding that.
A previous iteration has the parent locking after the dentry locking, but I found that couldn't work. So I move the parent locking back to the start, but must have missed this function.
That part of the patch now reads:
@@ -2143,8 +2198,8 @@ EXPORT_SYMBOL(lookup_and_lock_killable);
bool lock_and_check_dentry(struct dentry *child, struct dentry *parent) {
inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
- if (child->d_parent == parent) {
- /* get the child to balance with dentry_unlock which puts it. */
+ if (!dentry_lock(child, parent, NULL, TASK_UNINTERRUPTIBLE)) {
+ /* get the child to balance with dentry_unlock() which puts it. */
dget(child);
return true;
}
Thanks,
NeilBrown
PATCH 8/8 VFS: allow a filesystem to opt out of directory locking.
In reply to NeilBrownThe VFS no longer needs the directory to be locked when performing updates in the directory (create/remove/rename). We only lock directories during these ops because the filesystem might expect that. Some filesystems may not need it. Allow the filesystem to opt out by setting no_dir_lock in inode_operations.
--- fs/namei.c | 75 ++++++++++++++++++++++++++++++++-------------- include/linux/fs.h | 1 + 2 files changed, 54 insertions(+), 22 deletions(-)
fs/namei.c +53 -22
@@ -2001,7 +2001,8 @@ struct dentry *lookup_and_lock_hashed(struct qstr *last, {struct dentry *dentry; -inode_lock_nested (base->d_inode,I_MUTEX_PARENT ); +if (!d_inode (base)->i_op->no_dir_lock) +inode_lock_nested (base->d_inode,I_MUTEX_PARENT ); retry: dentry =lookup_one_qstr (last, base, lookup_flags);
@@ -2011,7 +2012,8 @@ struct dentry *lookup_and_lock_hashed(struct qstr *last, goto retry; } -if (IS_ERR (dentry)) +if (IS_ERR (dentry) && + !d_inode (base)->i_op->no_dir_lock)inode_unlock (base->d_inode);return dentry; }
@@ -2066,11 +2068,13 @@ struct dentry *lookup_and_lock_noperm(struct qstr *last, {struct dentry *dentry; -inode_lock_nested (base->d_inode,I_MUTEX_PARENT ); +if (!d_inode (base)->i_op->no_dir_lock) +inode_lock_nested (base->d_inode,I_MUTEX_PARENT ); dentry =lookup_and_lock_noperm_locked (last, base, lookup_flags,DLOCK_NORMAL ); -if (IS_ERR (dentry)) +if (IS_ERR (dentry) && + !d_inode (base)->i_op->no_dir_lock)inode_unlock (base->d_inode);return dentry; }
@@ -2097,9 +2101,11 @@ struct dentry *lookup_and_lock_noperm_nested(struct qstr *last, {struct dentry *dentry; -inode_lock_nested (base->d_inode,I_MUTEX_PARENT ); +if (!d_inode (base)->i_op->no_dir_lock) +inode_lock_nested (base->d_inode,I_MUTEX_PARENT ); dentry =lookup_and_lock_noperm_locked (last, base, lookup_flags, class); -if (IS_ERR (dentry)) +if (IS_ERR (dentry) && + !d_inode (base)->i_op->no_dir_lock)inode_unlock (base->d_inode);return dentry; }
@@ -2160,9 +2166,12 @@ struct dentry *lookup_and_lock_killable(struct mnt_idmap *idmap,struct dentry *dentry;int err; - err =down_write_killable_nested (&base->d_inode->i_rwsem,I_MUTEX_PARENT ); -if (err) -return ERR_PTR (err); +if (!d_inode (base)->i_op->no_dir_lock) { + err =down_write_killable_nested (&base->d_inode->i_rwsem, +I_MUTEX_PARENT ); +if (err) +return ERR_PTR (err); + } err =lookup_one_common (idmap, last, base);if (err <0 )return ERR_PTR (err);
@@ -2176,7 +2185,8 @@ struct dentry *lookup_and_lock_killable(struct mnt_idmap *idmap,return ERR_PTR (-ERESTARTSYS ); goto retry; } -if (IS_ERR (dentry)) +if (IS_ERR (dentry) && + !d_inode (base)->i_op->no_dir_lock)inode_unlock (base->d_inode);return dentry; }
@@ -2205,7 +2215,8 @@ bool lock_and_check_dentry(struct dentry *child, struct dentry *parent) }/* get the child to balance with dentry_unlock() which puts it. */ dget (child); -inode_lock_nested (d_inode (parent),I_MUTEX_PARENT ); +if (!d_inode (parent)->i_op->no_dir_lock) +inode_lock_nested (d_inode (parent),I_MUTEX_PARENT );return true; }EXPORT_SYMBOL (lock_and_check_dentry);
@@ -2230,7 +2241,8 @@ void dentry_unlock(struct dentry *dentry) {if (!IS_ERR (dentry)) {d_lookup_done (dentry); -inode_unlock (dentry->d_parent->d_inode); +if (!dentry->d_parent->d_inode->i_op->no_dir_lock) +inode_unlock (dentry->d_parent->d_inode);dentry_unlock_dir_locked (dentry); } }
@@ -2342,9 +2354,11 @@ static struct dentry *lookup_slow(const struct qstr *name, {struct inode *inode = dir->d_inode;struct dentry *res; -inode_lock_shared (inode); +if (!inode->i_op->no_dir_lock) +inode_lock_shared (inode); res =__lookup_slow (name, dir, flags); -inode_unlock_shared (inode); +if (!inode->i_op->no_dir_lock) +inode_unlock_shared (inode);return res; }
@@ -3721,6 +3735,9 @@ static struct dentry *lock_two_directories(struct dentry *p1, struct dentry *p2)*/ static struct dentry *lock_rename (struct dentry *p1,struct dentry *p2) { +if (d_inode (p1)->i_op->no_dir_lock) +return NULL ; +if (p1 == p2) {inode_lock_nested (p1->d_inode,I_MUTEX_PARENT );return NULL ;
@@ -3735,6 +3752,9 @@ static struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)*/ static struct dentry *lock_rename_child (struct dentry *c1,struct dentry *p2) { +if (d_inode (c1)->i_op->no_dir_lock) +return NULL ; +if (READ_ONCE (c1->d_parent) == p2) {/* * hopefully won't need to touch ->s_vfs_rename_mutex at all.
@@ -3773,6 +3793,8 @@ static struct dentry *lock_rename_child(struct dentry *c1, struct dentry *p2)static void unlock_rename (struct dentry *p1,struct dentry *p2) { +if (d_inode (p1)->i_op->no_dir_lock) +return ;inode_unlock (p1->d_inode);if (p1 != p2) {inode_unlock (p2->d_inode);
@@ -3880,6 +3902,10 @@ static struct dentry *lock_ancestors(struct dentry *d1, struct dentry *d2) {struct dentry *locked, *ancestor; +if (!d_inode (d1)->i_op->no_dir_lock) +/* s_vfs_rename_mutex is being used, so skip this locking */ +return NULL ; +if (d1->d_parent == d2->d_parent)/* Nothing to lock */ return NULL ;
@@ -4194,6 +4220,7 @@ void dentry_unlock_rename(struct renamedata *rd)renaming_unlock (rd->old_dir, rd->new_dir, rd->ancestor, rd->old_dentry, rd->new_dentry); +if (!d_inode (rd->old_dir)->i_op->no_dir_lock)unlock_rename (rd->old_dir, rd->new_dir);dput (rd->old_dir);
@@ -4697,19 +4724,23 @@ static const char *open_last_lookups(struct nameidata *nd,* dropping this one anyway. */ } -if (open_flag &O_CREAT ) -inode_lock (dir->d_inode); -else -inode_lock_shared (dir->d_inode); +if (!d_inode (dir)->i_op->no_dir_lock) { +if (open_flag &O_CREAT ) +inode_lock (dir->d_inode); +else +inode_lock_shared (dir->d_inode); + } dentry =lookup_open (nd, file, op, got_write);if (!IS_ERR (dentry)) {if (file->f_mode &FMODE_OPENED )fsnotify_open (file); } -if (open_flag &O_CREAT ) -inode_unlock (dir->d_inode); -else -inode_unlock_shared (dir->d_inode); +if (!d_inode (dir)->i_op->no_dir_lock) { +if (open_flag &O_CREAT ) +inode_unlock (dir->d_inode); +else +inode_unlock_shared (dir->d_inode); + }if (got_write)mnt_drop_write (nd->path.mnt);
include/linux/fs.h +1 -0
@@ -2225,6 +2225,7 @@ int wrap_directory_iterator(struct file *, struct dir_context *, {return wrap_directory_iterator (file, ctx, x); }struct inode_operations { +bool no_dir_lock:1 ;struct dentry * (*lookup) (struct inode *,struct dentry *,unsigned int );const char * (*get_link) (struct dentry *,struct inode *,struct delayed_call *);int (*permission) (struct mnt_idmap *,struct inode *,int );
Signature · 1 line
2.49.0