autofs4: correct offset mount expire check
[safe/jmp/linux-2.6] / fs / autofs4 / expire.c
index 19f5bea..4b6fb3f 100644 (file)
@@ -56,12 +56,23 @@ static int autofs4_mount_busy(struct vfsmount *mnt, struct dentry *dentry)
        mntget(mnt);
        dget(dentry);
 
-       if (!autofs4_follow_mount(&mnt, &dentry))
+       if (!follow_down(&mnt, &dentry))
                goto done;
 
-       /* This is an autofs submount, we can't expire it */
-       if (is_autofs4_dentry(dentry))
-               goto done;
+       if (is_autofs4_dentry(dentry)) {
+               struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
+
+               /* This is an autofs submount, we can't expire it */
+               if (sbi->type == AUTOFS_TYPE_INDIRECT)
+                       goto done;
+
+               /*
+                * Otherwise it's an offset mount and we need to check
+                * if we can umount its mount, if there is one.
+                */
+               if (!d_mountpoint(dentry))
+                       goto done;
+       }
 
        /* Update the expiry counter if fs is busy */
        if (!may_umount_tree(mnt)) {
@@ -244,10 +255,10 @@ cont:
 }
 
 /* Check if we can expire a direct mount (possibly a tree) */
-static struct dentry *autofs4_expire_direct(struct super_block *sb,
-                                           struct vfsmount *mnt,
-                                           struct autofs_sb_info *sbi,
-                                           int how)
+struct dentry *autofs4_expire_direct(struct super_block *sb,
+                                    struct vfsmount *mnt,
+                                    struct autofs_sb_info *sbi,
+                                    int how)
 {
        unsigned long timeout;
        struct dentry *root = dget(sb->s_root);
@@ -259,13 +270,15 @@ static struct dentry *autofs4_expire_direct(struct super_block *sb,
        now = jiffies;
        timeout = sbi->exp_timeout;
 
-       /* Lock the tree as we must expire as a whole */
        spin_lock(&sbi->fs_lock);
        if (!autofs4_direct_busy(mnt, root, timeout, do_now)) {
                struct autofs_info *ino = autofs4_dentry_ino(root);
-
-               /* Set this flag early to catch sys_chdir and the like */
+               if (d_mountpoint(root)) {
+                       ino->flags |= AUTOFS_INF_MOUNTPOINT;
+                       root->d_mounted--;
+               }
                ino->flags |= AUTOFS_INF_EXPIRING;
+               init_completion(&ino->expire_complete);
                spin_unlock(&sbi->fs_lock);
                return root;
        }
@@ -281,10 +294,10 @@ static struct dentry *autofs4_expire_direct(struct super_block *sb,
  *  - it is unused by any user process
  *  - it has been unused for exp_timeout time
  */
-static struct dentry *autofs4_expire_indirect(struct super_block *sb,
-                                             struct vfsmount *mnt,
-                                             struct autofs_sb_info *sbi,
-                                             int how)
+struct dentry *autofs4_expire_indirect(struct super_block *sb,
+                                      struct vfsmount *mnt,
+                                      struct autofs_sb_info *sbi,
+                                      int how)
 {
        unsigned long timeout;
        struct dentry *root = sb->s_root;
@@ -392,6 +405,7 @@ found:
                expired, (int)expired->d_name.len, expired->d_name.name);
        ino = autofs4_dentry_ino(expired);
        ino->flags |= AUTOFS_INF_EXPIRING;
+       init_completion(&ino->expire_complete);
        spin_unlock(&sbi->fs_lock);
        spin_lock(&dcache_lock);
        list_move(&expired->d_parent->d_subdirs, &expired->d_u.d_child);
@@ -399,6 +413,35 @@ found:
        return expired;
 }
 
+int autofs4_expire_wait(struct dentry *dentry)
+{
+       struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
+       struct autofs_info *ino = autofs4_dentry_ino(dentry);
+       int status;
+
+       /* Block on any pending expire */
+       spin_lock(&sbi->fs_lock);
+       if (ino->flags & AUTOFS_INF_EXPIRING) {
+               spin_unlock(&sbi->fs_lock);
+
+               DPRINTK("waiting for expire %p name=%.*s",
+                        dentry, dentry->d_name.len, dentry->d_name.name);
+
+               status = autofs4_wait(sbi, dentry, NFY_NONE);
+               wait_for_completion(&ino->expire_complete);
+
+               DPRINTK("expire done status=%d", status);
+
+               if (d_unhashed(dentry))
+                       return -EAGAIN;
+
+               return status;
+       }
+       spin_unlock(&sbi->fs_lock);
+
+       return 0;
+}
+
 /* Perform an expiry operation */
 int autofs4_expire_run(struct super_block *sb,
                      struct vfsmount *mnt,
@@ -429,6 +472,7 @@ int autofs4_expire_run(struct super_block *sb,
        spin_lock(&sbi->fs_lock);
        ino = autofs4_dentry_ino(dentry);
        ino->flags &= ~AUTOFS_INF_EXPIRING;
+       complete_all(&ino->expire_complete);
        spin_unlock(&sbi->fs_lock);
 
        return ret;
@@ -446,7 +490,7 @@ int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt,
        if (arg && get_user(do_now, arg))
                return -EFAULT;
 
-       if (sbi->type & AUTOFS_TYPE_DIRECT)
+       if (sbi->type & AUTOFS_TYPE_TRIGGER)
                dentry = autofs4_expire_direct(sb, mnt, sbi, do_now);
        else
                dentry = autofs4_expire_indirect(sb, mnt, sbi, do_now);
@@ -457,8 +501,14 @@ int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt,
                /* This is synchronous because it makes the daemon a
                    little easier */
                ret = autofs4_wait(sbi, dentry, NFY_EXPIRE);
+
                spin_lock(&sbi->fs_lock);
+               if (ino->flags & AUTOFS_INF_MOUNTPOINT) {
+                       sb->s_root->d_mounted++;
+                       ino->flags &= ~AUTOFS_INF_MOUNTPOINT;
+               }
                ino->flags &= ~AUTOFS_INF_EXPIRING;
+               complete_all(&ino->expire_complete);
                spin_unlock(&sbi->fs_lock);
                dput(dentry);
        }