autofs4 - don't make expiring dentry negative
Subsystems (2)
Ian Kent
maintainerPATCH 1/7 autofs4 - don't make expiring dentry negative
Correct the error of making a positive dentry negative after it has been instantiated.
The code that makes this error attempts to re-use the dentry from a concurrent expire and mount to resolve a race and the dentry used for the lookup must be negative for mounts to trigger in the required cases. The fact is that the dentry doesn't need to be re-used because all that is needed is to preserve the flag that indicates an expire is still incomplete at the time of the mount request.
This change uses the the dentry to check the flag and wait for the expire to complete then discards it instead of attempting to re-use it.
---
fs/autofs4/autofs_i.h | 6 +-- fs/autofs4/inode.c | 6 +-- fs/autofs4/root.c | 115 ++++++++++++++++++------------------------------- 3 files changed, 49 insertions(+), 78 deletions(-)
fs/autofs4/autofs_i.h +3 -3
@@ -52,7 +52,7 @@ struct autofs_info {int flags; -struct list_head rehash; +struct list_head expiring;struct autofs_sb_info *sbi;unsigned long last_used;
@@ -112,8 +112,8 @@ struct autofs_sb_info {struct mutex wq_mutex;spinlock_t fs_lock;struct autofs_wait_queue *queues;/* Wait queue pointer */ -spinlock_t rehash_lock; -struct list_head rehash_list; +spinlock_t lookup_lock; +struct list_head expiring_list; };static inline struct autofs_sb_info *autofs4_sbi (struct super_block *sb)
fs/autofs4/inode.c +3 -3
@@ -47,7 +47,7 @@ struct autofs_info *autofs4_init_ino(struct autofs_info *ino, ino->dentry =NULL ; ino->size =0 ; -INIT_LIST_HEAD (&ino->rehash); +INIT_LIST_HEAD (&ino->expiring); ino->last_used = jiffies;atomic_set (&ino->count,0 );
@@ -338,8 +338,8 @@ int autofs4_fill_super(struct super_block *s, void *data, int silent)mutex_init (&sbi->wq_mutex);spin_lock_init (&sbi->fs_lock); sbi->queues =NULL ; -spin_lock_init (&sbi->rehash_lock); -INIT_LIST_HEAD (&sbi->rehash_list); +spin_lock_init (&sbi->lookup_lock); +INIT_LIST_HEAD (&sbi->expiring_list); s->s_blocksize =1024 ; s->s_blocksize_bits =10 ; s->s_magic =AUTOFS_SUPER_MAGIC ;
fs/autofs4/root.c +43 -72
208 lines not on this page · raw message
Ian Kent
maintainerPATCH 2/7 autofs4 - revert - redo lookup in ttfd
This patch series enables the use of a single dentry for lookups prior to the dentry being hashed and so we no longer need to redo the lookup. This patch reverts the patch of commit 033790449ba9c4dcf8478a87693d33df625c23b5.
---
fs/autofs4/root.c | 21 --------------------- 1 files changed, 0 insertions(+), 21 deletions(-)
fs/autofs4/root.c +0 -21
@@ -242,7 +242,6 @@ static int try_to_fill_dentry(struct dentry *dentry, int flags) {struct autofs_sb_info *sbi =autofs4_sbi (dentry->d_sb);struct autofs_info *ino =autofs4_dentry_ino (dentry); -struct dentry *new;int status;/* Block on any pending expiry here; invalidate the dentry
@@ -320,26 +319,6 @@ static int try_to_fill_dentry(struct dentry *dentry, int flags) dentry->d_flags &= ~DCACHE_AUTOFS_PENDING ;spin_unlock (&dentry->d_lock); -/* -* The dentry that is passed in from lookup may not be the one -* we end up using, as mkdir can create a new one. If this -* happens, and another process tries the lookup at the same time, -* it will set the PENDING flag on this new dentry, but add itself -* to our waitq. Then, if after the lookup succeeds, the first -* process that requested the mount performs another lookup of the -* same directory, it will show up as still pending! So, we need -* to redo the lookup here and clear pending on that dentry. -*/ -if (d_unhashed (dentry)) { - new =d_lookup (dentry->d_parent, &dentry->d_name); -if (new) { -spin_lock (&new->d_lock); - new->d_flags &= ~DCACHE_AUTOFS_PENDING ; -spin_unlock (&new->d_lock); -dput (new); - } - } -return 0 ; }
Ian Kent
maintainerPATCH 3/7 autofs4 - use look aside list for lookups
In reply to Ian KentA while ago a patch to resolve a deadlock during directory creation was merged. This delayed the hashing of lookup dentrys until the ->mkdir() (or ->symlink()) operation completed to ensure we always went through ->lookup() instead of also having processes go through ->revalidate() so our VFS locking remained consistent.
Now we are seeing a couple of side affects of that change in situations with heavy mount activity.
Two cases have been identified:
1) When a mount request is triggered, due to the delayed hashing, the directory created by user space for the mount point doesn't have the DCACHE_AUTOFS_PENDING flag set. In the case of an autofs multi-mount where a tree of mount point directories are created this can lead to the path walk continuing rather than the dentry being sent to the wait queue to wait for request completion. This is because, if the pending flag isn't set, the criteria for deciding this is a mount in progress fails to hold, namely that the dentry is not a mount point and has no subdirectories.
2) A mount request dentry is initially created negative and unhashed. It remains this way until the ->mkdir() callback completes. Since it is unhashed a fresh dentry is used when the user space mount request creates the mount point directory. This leaves the original dentry negative and unhashed. But revalidate has no way to tell the VFS that the dentry has changed, other than to force another ->lookup() by returning false, which is at best wastefull and at worst not possible. This results in an -ENOENT return from the original path walk when in fact the mount succeeded.
To resolve this we need to ensure that the same dentry is used in all calls to ->lookup() during the course of a mount request. This patch achieves that by adding the initial dentry to a look aside list and removes it at ->mkdir() or ->symlink() completion (or when the dentry is released), since these are the only create operations autofs4 supports.
---
fs/autofs4/autofs_i.h | 2 + fs/autofs4/inode.c | 25 ++++--- fs/autofs4/root.c | 169 ++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 156 insertions(+), 40 deletions(-)
fs/autofs4/autofs_i.h +2 -0
@@ -52,6 +52,7 @@ struct autofs_info {int flags; +struct list_head active;struct list_head expiring;struct autofs_sb_info *sbi;
@@ -113,6 +114,7 @@ struct autofs_sb_info {spinlock_t fs_lock;struct autofs_wait_queue *queues;/* Wait queue pointer */ spinlock_t lookup_lock; +struct list_head active_list;struct list_head expiring_list; };
fs/autofs4/inode.c +15 -10
@@ -24,8 +24,10 @@static void ino_lnkfree (struct autofs_info *ino) { -kfree (ino->u.symlink); - ino->u.symlink =NULL ; +if (ino->u.symlink) { +kfree (ino->u.symlink); + ino->u.symlink =NULL ; + } } struct autofs_info *autofs4_init_ino(struct autofs_info *ino,
@@ -41,16 +43,18 @@ struct autofs_info *autofs4_init_ino(struct autofs_info *ino,if (ino ==NULL )return NULL ; - ino->flags =0 ; - ino->mode = mode; - ino->inode =NULL ; - ino->dentry =NULL ; - ino->size =0 ; - -INIT_LIST_HEAD (&ino->expiring); +if (!reinit) { + ino->flags =0 ; + ino->inode =NULL ; + ino->dentry =NULL ; + ino->size =0 ; +INIT_LIST_HEAD (&ino->active); +INIT_LIST_HEAD (&ino->expiring); +atomic_set (&ino->count,0 ); + } + ino->mode = mode; ino->last_used = jiffies; -atomic_set (&ino->count,0 ); ino->sbi = sbi;
@@ -339,6 +343,7 @@ int autofs4_fill_super(struct super_block *s, void *data, int silent)spin_lock_init (&sbi->fs_lock); sbi->queues =NULL ;spin_lock_init (&sbi->lookup_lock); +INIT_LIST_HEAD (&sbi->active_list);INIT_LIST_HEAD (&sbi->expiring_list); s->s_blocksize =1024 ; s->s_blocksize_bits =10 ;
fs/autofs4/root.c +139 -30
262 lines not on this page · raw message
Ian Kent
maintainerPATCH 4/7 autofs4 - don't release directory mutex if called in oz_mode
In reply to Ian KentSince we now delay hashing of dentrys until the ->mkdir() call, droping and re-taking the directory mutex within the ->lookup() function when we are being called by user space is not needed. This can lead to a race when other processes are attempting to access the same directory during mount point directory creation.
In this case we need to hang onto the mutex to ensure we don't get user processes trying to create a mount request for a newly created dentry after the mount point entry has already been created. This ensures that when we need to check a dentry passed to autofs4_wait(), if it is hashed, it is always the mount point dentry and not a new dentry created by another lookup during directory creation.
---
fs/autofs4/root.c | 11 +++++------ 1 files changed, 5 insertions(+), 6 deletions(-)
fs/autofs4/root.c +5 -6
@@ -683,12 +683,11 @@ static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, sspin_lock (&dentry->d_lock); dentry->d_flags |=DCACHE_AUTOFS_PENDING ;spin_unlock (&dentry->d_lock); - } - -if (dentry->d_op && dentry->d_op->d_revalidate) { -mutex_unlock (&dir->i_mutex); - (dentry->d_op->d_revalidate)(dentry, nd); -mutex_lock (&dir->i_mutex); +if (dentry->d_op && dentry->d_op->d_revalidate) { +mutex_unlock (&dir->i_mutex); + (dentry->d_op->d_revalidate)(dentry, nd); +mutex_lock (&dir->i_mutex); + } }/*
Ian Kent
maintainerPATCH 5/7 autofs4 - use lookup intent flags to trigger mounts
In reply to Ian KentWhen an open(2) call is made on an autofs mount point directory that already exists and the O_DIRECTORY flag is not used the needed mount callback to the daemon is not done. This leads to the path walk continuing resulting in a callback to the daemon with an incorrect key. open(2) is called without O_DIRECTORY by the "find" utility but this should be handled properly anyway.
This happens because autofs needs to use the lookup flags to decide when to callback to the daemon to perform a mount to prevent mount storms. For example, an autofs indirect mount map that has the "browse" option will have the mount point directories are pre-created and the stat(2) call made by a color ls against each directory will cause all these directories to be mounted. It is unfortunate we need to resort to this but mount maps can be quite large. Additionally, if a user manually umounts an autofs indirect mount the directory isn't removed which also leads to this situation.
To resolve this autofs needs to use the lookup intent flags to enable it to make this decision. This patch adds this check and triggers a call back if any of the lookup intent flags are set as all these calls warrant a mount attempt be requested.
I know that external VFS code which uses the lookup flags is something that the VFS would like to eliminate but I have no choice as I can't see any other way to do this. A VFS dentry or inode operation callback which returns the lookup "type" (requires a definition) would be sufficient. But this change is needed now and I'm not aware of the form that coming VFS changes will take so I'm not willing to propose anything along these lines.
If anyone can provide an alternate method I would be happy to use it.
---
fs/autofs4/root.c | 7 +++++-- 1 files changed, 5 insertions(+), 2 deletions(-)
fs/autofs4/root.c +5 -2
@@ -31,6 +31,9 @@ static int autofs4_root_readdir(struct file * filp, void * dirent, filldir_t filstatic struct dentry *autofs4_lookup (struct inode *,struct dentry *,struct nameidata *);static void *autofs4_follow_link (struct dentry *,struct nameidata *); +#define TRIGGER_FLAGS (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) +#define TRIGGER_INTENTS (LOOKUP_OPEN | LOOKUP_ACCESS | LOOKUP_CREATE) +const struct file_operations autofs4_root_operations = { .open = dcache_dir_open, .release = dcache_dir_close,
@@ -291,7 +294,7 @@ static int try_to_fill_dentry(struct dentry *dentry, int flags)return status; }/* Trigger mount for path component or follow link */ - }else if (flags & (LOOKUP_CONTINUE |LOOKUP_DIRECTORY ) || + }else if (flags & (TRIGGER_FLAGS |TRIGGER_INTENTS ) || current->link_count) {DPRINTK ("waiting for mount name=%.*s" , dentry->d_name.len, dentry->d_name.name);
@@ -336,7 +339,7 @@ static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd) nd->flags);/* If it's our master or we shouldn't trigger a mount we're done */ - lookup_type = nd->flags & (LOOKUP_CONTINUE |LOOKUP_DIRECTORY ); + lookup_type = nd->flags & (TRIGGER_FLAGS |TRIGGER_INTENTS );if (oz_mode || !lookup_type) goto done;
Ian Kent
maintainerPATCH 6/7 autofs4 - use struct qstr in waitq.c
In reply to Ian KentFrom: Jeff Moyer <[email protected]>
The autofs_wait_queue already contains all of the fields of the struct qstr, so change it into a qstr.
This patch, from Jeff Moyer, has been modified a liitle by myself.
---
fs/autofs4/autofs_i.h | 4 +- fs/autofs4/waitq.c | 86 ++++++++++++++++++++++++++----------------------- 2 files changed, 46 insertions(+), 44 deletions(-)
fs/autofs4/autofs_i.h +1 -3
@@ -75,9 +75,7 @@ struct autofs_wait_queue {struct autofs_wait_queue *next;autofs_wqt_t wait_queue_token;/* We use the following to see what we are waiting for */ -unsigned int hash; -unsigned int len; -char *name; +struct qstr name;u32 dev;u64 ino;uid_t uid;
fs/autofs4/waitq.c +45 -41
208 lines not on this page · raw message
Re: [PATCH 6/7] autofs4 - use struct qstr in waitq.c
Quoted 9 lines · On Tue, 17 Jun 2008 20:24:06 +0800 Ian Kent <[email protected]> wrote:
From: Jeff Moyer <[email protected]>
The
autofs_wait_queuealready contains all of the fields of the struct qstr, so change it into a qstr.This patch, from Jeff Moyer, has been modified a liitle by myself.
Signed-off-by Jeff Moyer <[email protected]>Signed-off-by Ian Kent <[email protected]>
So this patch which had been happily sitting in -mm for a month has suddenly broken because linux-next's three-day-old 4bce7ce7c7d0d57b78dacc3a2bd87ec63b2d9b4c has removed LOOKUP_ACCESS.
This is suboptimal.
Now what do I do?
Ian Kent
maintainerRe: [PATCH 6/7] autofs4 - use struct qstr in waitq.c
Quoted 13 lines · Andrew Morton
Quoted 9 lines · On Tue, 17 Jun 2008 20:24:06 +0800 Ian Kent <[email protected]> wrote:
From: Jeff Moyer <[email protected]>
The
autofs_wait_queuealready contains all of the fields of the struct qstr, so change it into a qstr.This patch, from Jeff Moyer, has been modified a liitle by myself.
Signed-off-by Jeff Moyer <[email protected]>Signed-off-by Ian Kent <[email protected]>
Quoted 7 lines
So this patch which had been happily sitting in -mm for a month has suddenly broken because linux-next's three-day-old
4bce7ce7c7d0d57b78dacc3a2bd87ec63b2d9b4chas removedLOOKUP_ACCESS.This is suboptimal.
Now what do I do?
Ummm .. I'm confused.
Your patch autofs4-use-lookup-intent-flags-to-trigger-mounts-fix.patch allows the linux-next kernel to build with all the autofs4 patches currently posted for inclusion in mm but the patch you mention here isn't concerned with the lookup flags?
The removal of LOOKUP_ACCESS is quite interesting. AFAIKS it effectively prevents the patch autofs4-use-lookup-intent-flags-to-trigger-mounts.patch from also resolving an issue with recursive autofs mounts while still resolving the issue that the patch was actually meant to address.
It's hard to get exited about the former issue as Al Viro has NACKed a previous patch that added the LOOKUP_ACCESS check, indicating the availability of the lookup flags will be changing. Also there is a question as to whether autofs will support the use mount points in automount maps that themselves refer to an automount path (the recursive bit).
Ian
Re: [PATCH 6/7] autofs4 - use struct qstr in waitq.c
Quoted 21 lines · On Wed, 23 Jul 2008 13:08:40 +0800 Ian Kent <[email protected]> wrote:
Quoted 19 lines · Andrew Morton
Quoted 9 lines · On Tue, 17 Jun 2008 20:24:06 +0800 Ian Kent <[email protected]> wrote:
From: Jeff Moyer <[email protected]>
The
autofs_wait_queuealready contains all of the fields of the struct qstr, so change it into a qstr.This patch, from Jeff Moyer, has been modified a liitle by myself.
Signed-off-by Jeff Moyer <[email protected]>Signed-off-by Ian Kent <[email protected]>So this patch which had been happily sitting in -mm for a month has suddenly broken because linux-next's three-day-old
4bce7ce7c7d0d57b78dacc3a2bd87ec63b2d9b4chas removedLOOKUP_ACCESS.This is suboptimal.
Quoted 8 lines
Quoted 1 line
Now what do I do?
Ummm .. I'm confused.
Your patch autofs4-use-lookup-intent-flags-to-trigger-mounts-fix.patch allows the linux-next kernel to build with all the autofs4 patches currently posted for inclusion in mm but the patch you mention here isn't concerned with the lookup flags?
Yeah, I picked the wrong patch to reply to.
Quoted 6 lines
The removal of
LOOKUP_ACCESSis quite interesting. AFAIKS it effectively prevents the patch autofs4-use-lookup-intent-flags-to-trigger-mounts.patch from also resolving an issue with recursive autofs mounts while still resolving the issue that the patch was actually meant to address.
Quoted 6 lines
It's hard to get exited about the former issue as Al Viro has NACKed a previous patch that added the
LOOKUP_ACCESScheck, indicating the availability of the lookup flags will be changing. Also there is a question as to whether autofs will support the use mount points in automount maps that themselves refer to an automount path (the recursive bit).
So what do we do?
Seems that a great pile of newish-looking stuff hit the vfs tree yesterday. Al, is that material supposed to be going into 2.6.27?
Ian Kent
maintainerRe: [PATCH 6/7] autofs4 - use struct qstr in waitq.c
Quoted 25 lines · Andrew Morton
Quoted 23 lines · On Wed, 23 Jul 2008 13:08:40 +0800 Ian Kent <[email protected]> wrote:
Quoted 20 lines · Andrew Morton
Quoted 9 lines · On Tue, 17 Jun 2008 20:24:06 +0800 Ian Kent <[email protected]> wrote:
From: Jeff Moyer <[email protected]>
The
autofs_wait_queuealready contains all of the fields of the struct qstr, so change it into a qstr.This patch, from Jeff Moyer, has been modified a liitle by myself.
Signed-off-by Jeff Moyer <[email protected]>Signed-off-by Ian Kent <[email protected]>So this patch which had been happily sitting in -mm for a month has suddenly broken because linux-next's three-day-old
4bce7ce7c7d0d57b78dacc3a2bd87ec63b2d9b4chas removedLOOKUP_ACCESS.This is suboptimal.
Now what do I do?
Quoted 8 lines
Quoted 6 lines
Ummm .. I'm confused.
Your patch autofs4-use-lookup-intent-flags-to-trigger-mounts-fix.patch allows the linux-next kernel to build with all the autofs4 patches currently posted for inclusion in mm but the patch you mention here isn't concerned with the lookup flags?
Yeah, I picked the wrong patch to reply to.
Hahaha, ;)
Quoted 7 lines
Quoted 7 lines
The removal of
LOOKUP_ACCESSis quite interesting. AFAIKS it effectively prevents the patch autofs4-use-lookup-intent-flags-to-trigger-mounts.patch from also resolving an issue with recursive autofs mounts while still resolving the issue that the patch was actually meant to address.
Quoted 8 lines
Quoted 6 lines
It's hard to get exited about the former issue as Al Viro has NACKed a previous patch that added the
LOOKUP_ACCESScheck, indicating the availability of the lookup flags will be changing. Also there is a question as to whether autofs will support the use mount points in automount maps that themselves refer to an automount path (the recursive bit).So what do we do?
Yeah, best thing to do is to go with the patch that you did to resolve it. I'm going to need to deal with changes to the availability of the lookup flags to modules as they come up anyway.
I don't want to hold things up due to the recursive mount issue as Al has pointed out it doesn't work correctly now anyway. I haven't had time to look into it as it isn't at the top of my priority list.
Ian
Ian Kent
maintainerPATCH 7/7 autofs4 - fix pending mount race.
In reply to Ian KentClose a race between a pending mount that is about to finish and a new lookup for the same directory.
Process P1 triggers a mount of directory foo.
It sets DCACHE_AUTOFS_PENDING in the ->lookup routine, creates a waitq entry for 'foo', and calls out to the daemon to perform the mount. The autofs daemon will then create the directory 'foo', using a new dentry that will be hashed in the dcache.
Before the mount completes, another process, P2, tries to walk into the 'foo' directory. The vfs path walking code finds an entry for 'foo' and calls the revalidate method. Revalidate finds that the entry is not PENDING (because PENDING was never set on the dentry created by the mkdir), but it does find the directory is empty. Revalidate calls try_to_fill_dentry, which sets the PENDING flag and then calls into the autofs4 wait code to trigger or wait for a mount of 'foo'. The wait code finds the entry for 'foo' and goes to sleep waiting for the completion of the mount.
Yet another process, P3, tries to walk into the 'foo' directory. This process again finds a dentry in the dcache for 'foo', and calls into the autofs revalidate code.
The revalidate code finds that the PENDING flag is set, and so calls try_to_fill_dentry.
a) try_to_fill_dentry sets the PENDING flag redundantly for this dentry, then calls into the autofs4 wait code.
b) the autofs4 wait code takes the waitq mutex and searches for an entry for 'foo'
Between a and b, P1 is woken up because the mount completed. P1 takes the wait queue mutex, clears the PENDING flag from the dentry, and removes the waitqueue entry for 'foo' from the list.
When it releases the waitq mutex, P3 (eventually) acquires it. At this time, it looks for an existing waitq for 'foo', finds none, and so creates a new one and calls out to the daemon to mount the 'foo' directory.
Now, the reason that three processes are required to trigger this race is that, because the PENDING flag is not set on the dentry created by mkdir, the window for the race would be way to slim for it to ever occur. Basically, between the testing of d_mountpoint(dentry) and the taking of the waitq mutex, the mount would have to complete and the daemon would have to be woken up, and that in turn would have to wake up P1. This is simply impossible. Add the third process, though, and it becomes slightly more likely.
---
fs/autofs4/waitq.c | 135 +++++++++++++++++++++++++++++++++++++--------------- 1 files changed, 97 insertions(+), 38 deletions(-)
fs/autofs4/waitq.c +97 -38
@@ -207,19 +207,106 @@ autofs4_find_wait(struct autofs_sb_info *sbi, struct qstr *qstr)return wq; } +/* +* Check if we have a valid request. +* Returns +* 1 if the request should continue. +* In this case we can return an autofs_wait_queue entry if one is +* found or NULL to idicate a new wait needs to be created. +* 0 or a negative errno if the request shouldn't continue. +*/ +static int validate_request (struct autofs_wait_queue **wait, +struct autofs_sb_info *sbi, +struct qstr *qstr, +struct dentry *dentry,enum autofs_notify notify) +{ +struct autofs_wait_queue *wq; +struct autofs_info *ino; + +/* Wait in progress, continue; */ + wq =autofs4_find_wait (sbi, qstr); +if (wq) { + *wait = wq; +return 1 ; + } + + *wait =NULL ; + +/* If we don't yet have any info this is a new request */ + ino =autofs4_dentry_ino (dentry); +if (!ino) +return 1 ; + +/* +* If we've been asked to wait on an existing expire (NFY_NONE) +* but there is no wait in the queue ... +*/ +if (notify ==NFY_NONE ) { +/* +* Either we've betean the pending expire to post it's +* wait or it finished while we waited on the mutex. +* So we need to wait till either, the wait appears +* or the expire finishes. +*/ + +while (ino->flags &AUTOFS_INF_EXPIRING ) { +mutex_unlock (&sbi->wq_mutex); +schedule_timeout_interruptible (HZ /10 ); +if (mutex_lock_interruptible (&sbi->wq_mutex)) +return -EINTR ; + + wq =autofs4_find_wait (sbi, qstr); +if (wq) { + *wait = wq; +return 1 ; + } + } + +/* +* Not ideal but the status has already gone. Of the two +* cases where we wait on NFY_NONE neither depend on the +* return status of the wait. +*/ +return 0 ; + } + +/* +* If we've been asked to trigger a mount and the request +* completed while we waited on the mutex ... +*/ +if (notify ==NFY_MOUNT ) { +/* +* If the dentry isn't hashed just go ahead and try the +* mount again with a new wait (not much else we can do). +*/ +if (!d_unhashed (dentry)) { +/* +* But if the dentry is hashed, that means that we +* got here through the revalidate path. Thus, we +* need to check if the dentry has been mounted +* while we waited on the wq_mutex. If it has, +* simply return success. +*/ +if (d_mountpoint (dentry)) +return 0 ; + } + } + +return 1 ; +} +int autofs4_wait (struct autofs_sb_info *sbi,struct dentry *dentry,enum autofs_notify notify) { -struct autofs_info *ino;struct autofs_wait_queue *wq;struct qstr qstr;char *name; -int status, type; +int status, ret, type;/* In catatonic mode, we don't wait for nobody */ if (sbi->catatonic)return -ENOENT ; - + name =kmalloc (NAME_MAX +1 ,GFP_KERNEL );if (!name)return -ENOMEM ;
@@ -237,43 +324,15 @@ int autofs4_wait(struct autofs_sb_info *sbi, struct dentry *dentry, qstr.name = name; qstr.hash =full_name_hash (name, qstr.len); -if (mutex_lock_interruptible (&sbi->wq_mutex)) { -kfree (qstr.name); +if (mutex_lock_interruptible (&sbi->wq_mutex))return -EINTR ; - } - - wq =autofs4_find_wait (sbi, &qstr); - ino =autofs4_dentry_ino (dentry); -if (!wq && ino && notify ==NFY_NONE ) { -/* -* Either we've betean the pending expire to post it's -* wait or it finished while we waited on the mutex. -* So we need to wait till either, the wait appears -* or the expire finishes. -*/ -while (ino->flags &AUTOFS_INF_EXPIRING ) { -mutex_unlock (&sbi->wq_mutex); -schedule_timeout_interruptible (HZ /10 ); -if (mutex_lock_interruptible (&sbi->wq_mutex)) { -kfree (qstr.name); -return -EINTR ; - } - wq =autofs4_find_wait (sbi, &qstr); -if (wq) -break ; - } - -/* -* Not ideal but the status has already gone. Of the two -* cases where we wait on NFY_NONE neither depend on the -* return status of the wait. -*/ -if (!wq) { -kfree (qstr.name); + ret =validate_request (&wq, sbi, &qstr, dentry, notify); +if (ret <=0 ) { +if (ret ==0 )mutex_unlock (&sbi->wq_mutex); -return 0 ; - } +kfree (qstr.name); +return ret; }if (!wq) {
@@ -391,9 +450,9 @@ int autofs4_wait_release(struct autofs_sb_info *sbi, autofs_wqt_t wait_queue_tok } *wql = wq->next;/* Unlink from chain */ -mutex_unlock (&sbi->wq_mutex);kfree (wq->name.name); wq->name.name =NULL ;/* Do not wait on this queue */ +mutex_unlock (&sbi->wq_mutex); wq->status = status;
Re: [PATCH 7/7] autofs4 - fix pending mount race.
Quoted 68 lines · On Tue, 17 Jun 2008 20:24:12 +0800 Ian Kent <[email protected]> wrote:
Close a race between a pending mount that is about to finish and a new lookup for the same directory.
Process P1 triggers a mount of directory foo.
It setsDCACHE_AUTOFS_PENDINGin the ->lookup routine, creates a waitq entry for 'foo', and calls out to the daemon to perform the mount. The autofs daemon will then create the directory 'foo', using a new dentry that will be hashed in the dcache.Before the mount completes, another process, P2, tries to walk into the 'foo' directory. The vfs path walking code finds an entry for 'foo' and calls the revalidate method. Revalidate finds that the entry is not PENDING (because PENDING was never set on the dentry created by the mkdir), but it does find the directory is empty. Revalidate calls
try_to_fill_dentry, which sets the PENDING flag and then calls into the autofs4 wait code to trigger or wait for a mount of 'foo'. The wait code finds the entry for 'foo' and goes to sleep waiting for the completion of the mount.Yet another process, P3, tries to walk into the 'foo' directory. This process again finds a dentry in the dcache for 'foo', and calls into the autofs revalidate code.
The revalidate code finds that the PENDING flag is set, and so calls
try_to_fill_dentry.a)
try_to_fill_dentrysets the PENDING flag redundantly for this dentry, then calls into the autofs4 wait code.
b) the autofs4 wait code takes the waitq mutex and searches for an entry for 'foo'Between a and b, P1 is woken up because the mount completed. P1 takes the wait queue mutex, clears the PENDING flag from the dentry, and removes the waitqueue entry for 'foo' from the list.
When it releases the waitq mutex, P3 (eventually) acquires it. At this time, it looks for an existing waitq for 'foo', finds none, and so creates a new one and calls out to the daemon to mount the 'foo' directory.
Now, the reason that three processes are required to trigger this race is that, because the PENDING flag is not set on the dentry created by mkdir, the window for the race would be way to slim for it to ever occur. Basically, between the testing of
d_mountpoint(dentry)and the taking of the waitq mutex, the mount would have to complete and the daemon would have to be woken up, and that in turn would have to wake up P1. This is simply impossible. Add the third process, though, and it becomes slightly more likely....
fs/autofs4/waitq.c +11
-0@@ -207,19 +207,106 @@ autofs4_find_wait(struct autofs_sb_info *sbi, struct qstr *qstr)return wq; } +/* +* Check if we have a valid request. +* Returns +* 1 if the request should continue. +* In this case we can return an autofs_wait_queue entry if one is +* found or NULL to idicate a new wait needs to be created. +* 0 or a negative errno if the request shouldn't continue. +*/ +static int validate_request(struct autofs_wait_queue **wait, + struct autofs_sb_info *sbi, + struct qstr *qstr,
fs/autofs4/waitq.c autofs4_find_wait(struct autofs_sb_info *sbi, struct qstr *qstr) · line 230
Quoted 10 lines
fs/autofs4/waitq.c +10
-0+ struct dentry*dentry, enum autofs_notify notify) +{ + struct autofs_wait_queue *wq; +struct autofs_info *ino; + +/* Wait in progress, continue; */ + wq =autofs4_find_wait (sbi, qstr); +if (wq) { + *wait = wq; +return 1 ;
Returns 1 with the mutex held.
Quoted 10 lines
fs/autofs4/waitq.c +10
-0+ } + + *wait =NULL ; + +/* If we don't yet have any info this is a new request */ + ino =autofs4_dentry_ino (dentry); +if (!ino) +return 1 ; + +/*
fs/autofs4/waitq.c autofs4_find_wait(struct autofs_sb_info *sbi, struct qstr *qstr) · line 250
Quoted 10 lines
fs/autofs4/waitq.c +10
-0+* If we've been asked to wait on an existing expire (NFY_NONE) +* but there is no wait in the queue ... +*/ +if (notify ==NFY_NONE ) { +/* +* Either we've betean the pending expire to post it's +* wait or it finished while we waited on the mutex. +* So we need to wait till either, the wait appears +* or the expire finishes. +*/
Wanna have another go at that comment? The grammar and spelling should cause an oops or something.
fs/autofs4/waitq.c autofs4_find_wait(struct autofs_sb_info *sbi, struct qstr *qstr) · line 256
Quoted 5 lines
fs/autofs4/waitq.c +5
-0+while (ino->flags &AUTOFS_INF_EXPIRING ) { +mutex_unlock (&sbi->wq_mutex); +schedule_timeout_interruptible (HZ /10 ); +if (mutex_lock_interruptible (&sbi->wq_mutex)) +return -EINTR ;
Returns -EFOO with the mutex not held.
Quoted 4 lines
fs/autofs4/waitq.c +4
-0+ + wq =autofs4_find_wait (sbi, qstr); +if (wq) { + *wait = wq;
fs/autofs4/waitq.c autofs4_find_wait(struct autofs_sb_info *sbi, struct qstr *qstr) · line 270
Quoted 10 lines
fs/autofs4/waitq.c +10
-0+return 1 ; + } + } + +/* +* Not ideal but the status has already gone. Of the two +* cases where we wait on NFY_NONE neither depend on the +* return status of the wait. +*/ +return 0 ;
Returns zero with the mutex held.
Quoted 28 lines
fs/autofs4/waitq.c +26
-0+ } + +/* +* If we've been asked to trigger a mount and the request +* completed while we waited on the mutex ... +*/ +if (notify ==NFY_MOUNT ) { +/* +* If the dentry isn't hashed just go ahead and try the +* mount again with a new wait (not much else we can do). +*/ +if (!d_unhashed (dentry)) { +/* +* But if the dentry is hashed, that means that we +* got here through the revalidate path. Thus, we +* need to check if the dentry has been mounted +* while we waited on the wq_mutex. If it has, +* simply return success. +*/ +if (d_mountpoint (dentry)) +return 0 ; + } + } + +return 1 ; +}
fs/autofs4/waitq.c autofs4_find_wait(struct autofs_sb_info *sbi, struct qstr *qstr)
Quoted 10 lines
fs/autofs4/waitq.c +5
-2+ ret =validate_request (&wq, sbi, &qstr, dentry, notify); +if (ret <=0 ) { +if (ret ==0 )mutex_unlock (&sbi->wq_mutex); -return 0 ; - } +kfree (qstr.name); +return ret; }
Leave the mutex held if it returned 1. Doesn't unlock the mutex if it returned -EFOO. Presumably callers of this function will unlock the mutex if it returned zero.
Or something like that. My brain just exploded.
Please double-check the locking protocol here and then document the sorry thing.