int needs_reghost;
        struct super_block *sb;
        struct semaphore wq_sem;
+       spinlock_t fs_lock;
        struct autofs_wait_queue *queues; /* Wait queue pointer */
 };
 
 static inline int autofs4_ispending(struct dentry *dentry)
 {
        struct autofs_info *inf = autofs4_dentry_ino(dentry);
+       int pending = 0;
 
-       return (dentry->d_flags & DCACHE_AUTOFS_PENDING) ||
-               (inf != NULL && inf->flags & AUTOFS_INF_EXPIRING);
+       if (dentry->d_flags & DCACHE_AUTOFS_PENDING)
+               return 1;
+
+       if (inf) {
+               spin_lock(&inf->sbi->fs_lock);
+               pending = inf->flags & AUTOFS_INF_EXPIRING;
+               spin_unlock(&inf->sbi->fs_lock);
+       }
+
+       return pending;
 }
 
 static inline void autofs4_copy_atime(struct file *src, struct file *dst)
 
        if (!autofs4_can_expire(top, timeout, do_now))
                return 0;
 
+       /* Is someone visiting anywhere in the tree ? */
+       if (may_umount_tree(mnt))
+               return 0;
+
        spin_lock(&dcache_lock);
 repeat:
        next = this_parent->d_subdirs.next;
 
                /* Case 2: tree mount, expire iff entire tree is not busy */
                if (!exp_leaves) {
+                       /* Lock the tree as we must expire as a whole */
+                       spin_lock(&sbi->fs_lock);
                        if (autofs4_check_tree(mnt, dentry, timeout, do_now)) {
-                       expired = dentry;
-                       break;
+                               struct autofs_info *inf = autofs4_dentry_ino(dentry);
+
+                               /* Set this flag early to catch sys_chdir and the like */
+                               inf->flags |= AUTOFS_INF_EXPIRING;
+                               spin_unlock(&sbi->fs_lock);
+                               expired = dentry;
+                               break;
                        }
+                       spin_unlock(&sbi->fs_lock);
                /* Case 3: direct mount, expire individual leaves */
                } else {
                        expired = autofs4_check_leaves(mnt, dentry, timeout, do_now);