diff --git a/lustre/llite/dcache.c b/lustre/llite/dcache.c
index 0d9510a4edce7a4df84ebbc439e98f0b6057c1af..e1b9445f46640398e919a7f6be0e8c3a2599c03c 100644
--- a/lustre/llite/dcache.c
+++ b/lustre/llite/dcache.c
@@ -52,7 +52,7 @@
 spinlock_t ll_lookup_lock = SPIN_LOCK_UNLOCKED;
 
 /* should NOT be called with the dcache lock, see fs/dcache.c */
-void ll_release(struct dentry *de)
+static void ll_release(struct dentry *de)
 {
         struct ll_dentry_data *lld;
         ENTRY;
@@ -139,14 +139,13 @@ void ll_set_dd(struct dentry *de)
         if (de->d_fsdata == NULL) {
                 struct ll_dentry_data *lld;
 
-                OBD_ALLOC(lld, sizeof(struct ll_dentry_data));
+                OBD_ALLOC_PTR(lld);
                 if (likely(lld != NULL)) {
-                        cfs_waitq_init(&lld->lld_waitq);
                         lock_dentry(de);
                         if (likely(de->d_fsdata == NULL))
                                 de->d_fsdata = lld;
                         else
-                                OBD_FREE(lld, sizeof(struct ll_dentry_data));
+                                OBD_FREE_PTR(lld);
                         unlock_dentry(de);
                 }
         }
@@ -467,6 +466,9 @@ do_lock:
                              &req, ll_mdc_blocking_ast, 0);
         if (it->it_op == IT_GETATTR && !first)
                 ll_statahead_exit(de, rc);
+        else if (first == -EEXIST)
+                ll_statahead_mark(de);
+
         /* If req is NULL, then mdc_intent_lock only tried to do a lock match;
          * if all was well, it will return 1 if it found locks, 0 otherwise. */
         if (req == NULL && rc >= 0) {
@@ -583,6 +585,8 @@ out_sa:
                 first = ll_statahead_enter(de->d_parent->d_inode, &de, 0);
                 if (!first)
                         ll_statahead_exit(de, rc);
+                else if (first == -EEXIST)
+                        ll_statahead_mark(de);
         }
 
         return rc;
@@ -774,45 +778,3 @@ struct dentry_operations ll_d_ops = {
         .d_unpin = ll_unpin,
 #endif
 };
-
-static int ll_fini_revalidate_nd(struct dentry *dentry, struct nameidata *nd)
-{
-        ENTRY;
-        /* need lookup */
-        RETURN(0);
-}
-
-struct dentry_operations ll_fini_d_ops = {
-        .d_revalidate = ll_fini_revalidate_nd,
-        .d_release = ll_release,
-};
-
-/*
- * It is for the following race condition:
- * When someone (maybe statahead thread) adds the dentry to the dentry hash
- * table, the dentry's "d_op" maybe NULL, at the same time, another (maybe
- * "ls -l") process finds such dentry by "do_lookup()" without "do_revalidate()"
- * called. It causes statahead window lost, and maybe other issues. --Fan Yong
- */
-static int ll_init_revalidate_nd(struct dentry *dentry, struct nameidata *nd)
-{
-        struct l_wait_info lwi = { 0 };
-        struct ll_dentry_data *lld;
-        ENTRY;
-
-        ll_set_dd(dentry);
-        lld = ll_d2d(dentry);
-        if (unlikely(lld == NULL))
-                RETURN(-ENOMEM);
-
-        l_wait_event(lld->lld_waitq, dentry->d_op != &ll_init_d_ops, &lwi);
-        if (likely(dentry->d_op == &ll_d_ops))
-                RETURN(ll_revalidate_nd(dentry, nd));
-        else
-                RETURN(dentry->d_op == &ll_fini_d_ops ? 0 : -EINVAL);
-}
-
-struct dentry_operations ll_init_d_ops = {
-        .d_revalidate = ll_init_revalidate_nd,
-        .d_release = ll_release,
-};
diff --git a/lustre/llite/file.c b/lustre/llite/file.c
index 875ec622dcc4ae3a548fa98bdabc4fc9e80cabae..dfa5ad9077d0850c7f98d71c9e5af2c4127f79ed 100644
--- a/lustre/llite/file.c
+++ b/lustre/llite/file.c
@@ -249,8 +249,8 @@ int ll_file_release(struct inode *inode, struct file *file)
         struct ll_inode_info *lli = ll_i2info(inode);
         struct lov_stripe_md *lsm = lli->lli_smd;
         int rc;
-
         ENTRY;
+
         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
                inode->i_generation, inode);
 
@@ -260,12 +260,10 @@ int ll_file_release(struct inode *inode, struct file *file)
         fd = LUSTRE_FPRIVATE(file);
         LASSERT(fd != NULL);
 
-        /*
-         * The last ref on @file, maybe not the the owner pid of statahead.
+        /* The last ref on @file, maybe not the the owner pid of statahead.
          * Different processes can open the same dir, "ll_opendir_key" means:
-         * it is me that should stop the statahead thread.
-         */
-        if (lli->lli_opendir_key == fd)
+         * it is me that should stop the statahead thread. */
+        if (lli->lli_opendir_key == fd && lli->lli_opendir_pid != 0)
                 ll_stop_statahead(inode, fd);
 
         if (inode->i_sb->s_root == file->f_dentry) {
@@ -428,27 +426,29 @@ int ll_file_open(struct inode *inode, struct file *file)
                 RETURN(-ENOMEM);
 
         if (S_ISDIR(inode->i_mode)) {
+again:
                 spin_lock(&lli->lli_lock);
-                /*
-                 * "lli->lli_opendir_pid != 0" means someone has set it.
-                 * "lli->lli_sai != NULL" means the previous statahead has not
-                 *                        been cleanup.
-                 */
-                if (lli->lli_opendir_pid == 0 && lli->lli_sai == NULL) {
-                        opendir_set = 1;
-                        lli->lli_opendir_pid = cfs_curproc_pid();
+                if (lli->lli_opendir_key == NULL && lli->lli_opendir_pid == 0) {
+                        LASSERT(lli->lli_sai == NULL);
                         lli->lli_opendir_key = fd;
-                } else if (unlikely(lli->lli_opendir_pid == cfs_curproc_pid())) {
+                        lli->lli_opendir_pid = cfs_curproc_pid();
+                        opendir_set = 1;
+                } else if (unlikely(lli->lli_opendir_pid == cfs_curproc_pid() &&
+                                    lli->lli_opendir_key != NULL)) {
                         /* Two cases for this:
                          * (1) The same process open such directory many times.
                          * (2) The old process opened the directory, and exited
                          *     before its children processes. Then new process
                          *     with the same pid opens such directory before the
                          *     old process's children processes exit.
-                         * Change the owner to the latest one.
-                         */
-                        opendir_set = 2;
-                        lli->lli_opendir_key = fd;
+                         * reset stat ahead for such cases. */
+                        spin_unlock(&lli->lli_lock);
+                        CDEBUG(D_INFO, "Conflict statahead for %.*s %lu/%u"
+                               " reset it.\n", file->f_dentry->d_name.len,
+                               file->f_dentry->d_name.name,
+                               inode->i_ino, inode->i_generation);
+                        ll_stop_statahead(inode, lli->lli_opendir_key);
+                        goto again;
                 }
                 spin_unlock(&lli->lli_lock);
         }
@@ -592,13 +592,10 @@ out_och_free:
                 }
                 up(&lli->lli_och_sem);
 out_openerr:
-                if (opendir_set) {
-                        lli->lli_opendir_key = NULL;
-                        lli->lli_opendir_pid = 0;
-                } else if (unlikely(opendir_set == 2)) {
+                if (opendir_set != 0)
                         ll_stop_statahead(inode, fd);
-                }
         }
+
         return rc;
 }
 
diff --git a/lustre/llite/llite_internal.h b/lustre/llite/llite_internal.h
index d004c38094beda7cc8c2d5275a41666d5b1b4cbe..2a666420836c66f8f75d1b06d48faaf12981dd61 100644
--- a/lustre/llite/llite_internal.h
+++ b/lustre/llite/llite_internal.h
@@ -113,7 +113,6 @@ struct ll_dentry_data {
         struct lookup_intent    *lld_it;
 #endif
         unsigned int             lld_sa_generation;
-        cfs_waitq_t              lld_waitq;
 };
 
 #define ll_d2d(de) ((struct ll_dentry_data*)((de)->d_fsdata))
@@ -691,10 +690,7 @@ int ll_fiemap(struct inode *inode, struct ll_user_fiemap *fiemap,
  * protect race ll_find_aliases vs ll_revalidate_it vs ll_unhash_aliases
  */
 extern spinlock_t ll_lookup_lock;
-extern struct dentry_operations ll_init_d_ops;
 extern struct dentry_operations ll_d_ops;
-extern struct dentry_operations ll_fini_d_ops;
-void ll_release(struct dentry *de);
 void ll_intent_drop_lock(struct lookup_intent *);
 void ll_intent_release(struct lookup_intent *);
 extern void ll_set_dd(struct dentry *de);
@@ -905,7 +901,6 @@ int ll_removexattr(struct dentry *dentry, const char *name);
 /* per inode struct, for dir only */
 struct ll_statahead_info {
         struct inode           *sai_inode;
-        struct dentry          *sai_first;      /* first dentry item */
         unsigned int            sai_generation; /* generation for statahead */
         atomic_t                sai_refcount;   /* when access this struct, hold
                                                  * refcount */
@@ -943,13 +938,19 @@ int ll_statahead_exit(struct dentry *dentry, int result);
 void ll_stop_statahead(struct inode *inode, void *key);
 
 static inline
-void ll_d_wakeup(struct dentry *dentry)
+int ll_statahead_mark(struct dentry *dentry)
 {
-        struct ll_dentry_data *lld = ll_d2d(dentry);
+        struct ll_inode_info *lli = ll_i2info(dentry->d_parent->d_inode);
+        struct ll_statahead_info *sai = lli->lli_sai;
+        struct ll_dentry_data *ldd = ll_d2d(dentry);
+        int rc = 0;
 
-        LASSERT(dentry->d_op != &ll_init_d_ops);
-        if (lld != NULL)
-                cfs_waitq_broadcast(&lld->lld_waitq);
+        if (likely(ldd != NULL))
+                ldd->lld_sa_generation = sai->sai_generation;
+        else
+                rc = -ENOMEM;
+
+        return rc;
 }
 
 static inline
@@ -988,6 +989,21 @@ int ll_statahead_enter(struct inode *dir, struct dentry **dentryp, int lookup)
         return do_statahead_enter(dir, dentryp, lookup);
 }
 
+static void inline ll_dops_init(struct dentry *de, int block)
+{
+        struct ll_dentry_data *lld = ll_d2d(de);
+
+        if (lld == NULL && block != 0) {
+                ll_set_dd(de);
+                lld = ll_d2d(de);
+        }
+
+        if (lld != NULL)
+                lld->lld_sa_generation = 0;
+
+        de->d_op = &ll_d_ops;
+}
+
 /* llite ioctl register support rountine */
 #ifdef __KERNEL__
 enum llioc_iter {
diff --git a/lustre/llite/llite_nfs.c b/lustre/llite/llite_nfs.c
index 4afb60eff0ac8bf47b27783fa0f8b58612b9fbf4..23bda6980d2df21a48c45ba60c5b7cf204e2d5a4 100644
--- a/lustre/llite/llite_nfs.c
+++ b/lustre/llite/llite_nfs.c
@@ -176,18 +176,7 @@ static struct dentry *ll_iget_for_nfs(struct super_block *sb, unsigned long ino,
         result->d_flags |= DCACHE_DISCONNECTED;
 
 #endif
-        ll_set_dd(result);
-
-        lock_dentry(result);
-        if (unlikely(result->d_op == &ll_init_d_ops)) {
-                result->d_op = &ll_d_ops;
-                unlock_dentry(result);
-                smp_wmb();
-                ll_d_wakeup(result);
-        } else {
-                result->d_op = &ll_d_ops;
-                unlock_dentry(result);
-        }
+        ll_dops_init(result, 1);
 
         RETURN(result);
 }
diff --git a/lustre/llite/namei.c b/lustre/llite/namei.c
index 0832d9a25df379c683d0657af77081c26fff3dad..27dfd6c626b2380e12f4656f7571c38ca91160c4 100644
--- a/lustre/llite/namei.c
+++ b/lustre/llite/namei.c
@@ -432,6 +432,7 @@ static struct dentry *ll_find_alias(struct inode *inode, struct dentry *de)
                 dentry->d_flags &= ~DCACHE_LUSTRE_INVALID;
 #endif
                 unlock_dentry(dentry);
+                ll_dops_init(dentry, 0);
                 d_rehash_cond(dentry, 0); /* avoid taking dcache_lock inside */
                 spin_unlock(&dcache_lock);
                 spin_unlock(&ll_lookup_lock);
@@ -441,18 +442,19 @@ static struct dentry *ll_find_alias(struct inode *inode, struct dentry *de)
                        de->d_parent, de->d_inode, atomic_read(&de->d_count));
                 return dentry;
         }
+
         if (last_discon) {
-                 CDEBUG(D_DENTRY, "Reuse disconnected dentry %p inode %p "
+                CDEBUG(D_DENTRY, "Reuse disconnected dentry %p inode %p "
                         "refc %d\n", last_discon, last_discon->d_inode,
                         atomic_read(&last_discon->d_count));
-                 dget_locked(last_discon);
-                 spin_unlock(&dcache_lock);
-                 d_rehash(de);
-                 d_move(last_discon, de);
-                 spin_unlock(&ll_lookup_lock);
-
-                 iput(inode);
-                 return last_discon;
+                dget_locked(last_discon);
+                spin_unlock(&dcache_lock);
+                spin_unlock(&ll_lookup_lock);
+                ll_dops_init(last_discon, 1);
+                d_rehash(de);
+                d_move(last_discon, de);
+                iput(inode);
+                return last_discon;
         }
 
         ll_d_add(de, inode);
@@ -471,16 +473,9 @@ int lookup_it_finish(struct ptlrpc_request *request, int offset,
         struct inode *parent = icbd->icbd_parent;
         struct ll_sb_info *sbi = ll_i2sbi(parent);
         struct inode *inode = NULL;
-        int set = 0, rc;
+        int rc;
         ENTRY;
 
-        lock_dentry(*de);
-        if (likely((*de)->d_op != &ll_d_ops)) {
-                (*de)->d_op = &ll_init_d_ops;
-                set = 1;
-        }
-        unlock_dentry(*de);
-
         /* NB 1 request reference will be taken away by ll_intent_lock()
          * when I return */
         if (!it_disposition(it, DISP_LOOKUP_NEG)) {
@@ -488,20 +483,8 @@ int lookup_it_finish(struct ptlrpc_request *request, int offset,
 
                 rc = ll_prep_inode(sbi->ll_osc_exp, &inode, request, offset,
                                    (*de)->d_sb);
-                if (rc) {
-                        if (set) {
-                                lock_dentry(*de);
-                                if (likely((*de)->d_op == &ll_init_d_ops)) {
-                                        (*de)->d_op = &ll_fini_d_ops;
-                                        unlock_dentry(*de);
-                                        smp_wmb();
-                                        ll_d_wakeup(*de);
-                                } else {
-                                        unlock_dentry(*de);
-                                }
-                        }
+                if (rc)
                         RETURN(rc);
-                }
 
                 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
                        inode, inode->i_ino, inode->i_generation);
@@ -515,19 +498,22 @@ int lookup_it_finish(struct ptlrpc_request *request, int offset,
                    Everybody else who needs correct file size would call
                    ll_glimpse_size or some equivalent themselves anyway.
                    Also see bug 7198. */
+
+                ll_dops_init(*de, 1);
                 *de = ll_find_alias(inode, *de);
-                if (set && *de != save) {
-                        lock_dentry(save);
-                        if (likely(save->d_op == &ll_init_d_ops)) {
-                                save->d_op = &ll_fini_d_ops;
-                                unlock_dentry(save);
-                                smp_wmb();
-                                ll_d_wakeup(save);
-                        } else {
-                                unlock_dentry(save);
+                if (*de != save) {
+                        struct ll_dentry_data *lld = ll_d2d(*de);
+
+                        /* just make sure the ll_dentry_data is ready */
+                        if (unlikely(lld == NULL)) {
+                                ll_set_dd(*de);
+                                lld = ll_d2d(*de);
+                                if (likely(lld != NULL))
+                                        lld->lld_sa_generation = 0;
                         }
                 }
         } else {
+                ll_dops_init(*de, 1);
                 /* Check that parent has UPDATE lock. If there is none, we
                    cannot afford to hash this dentry (done by ll_d_add) as it
                    might get picked up later when UPDATE lock will appear */
@@ -536,6 +522,7 @@ int lookup_it_finish(struct ptlrpc_request *request, int offset,
                         ll_d_add(*de, inode);
                         spin_unlock(&dcache_lock);
                 } else {
+                        (*de)->d_inode = NULL;
                         /* We do not want to hash the dentry if don`t have a
                          * lock, but if this dentry is later used in d_move,
                          * we'd hit uninitialised list head d_hash, so we just
@@ -546,19 +533,6 @@ int lookup_it_finish(struct ptlrpc_request *request, int offset,
                 }
         }
 
-        ll_set_dd(*de);
-
-        lock_dentry(*de);
-        if (likely((*de)->d_op == &ll_init_d_ops)) {
-                (*de)->d_op = &ll_d_ops;
-                unlock_dentry(*de);
-                smp_wmb();
-                ll_d_wakeup(*de);
-        } else {
-                (*de)->d_op = &ll_d_ops;
-                unlock_dentry(*de);
-        }
-
         RETURN(0);
 }
 
@@ -570,7 +544,7 @@ static struct dentry *ll_lookup_it(struct inode *parent, struct dentry *dentry,
         struct it_cb_data icbd;
         struct ptlrpc_request *req = NULL;
         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
-        int rc;
+        int rc, first = 0;
         ENTRY;
 
         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),intent=%s\n",
@@ -592,10 +566,10 @@ static struct dentry *ll_lookup_it(struct inode *parent, struct dentry *dentry,
         }
 
         if (it->it_op == IT_GETATTR) {
-                rc = ll_statahead_enter(parent, &dentry, 1);
-                if (rc >= 0) {
-                        ll_statahead_exit(dentry, rc);
-                        if (rc == 1)
+                first = ll_statahead_enter(parent, &dentry, 1);
+                if (first >= 0) {
+                        ll_statahead_exit(dentry, first);
+                        if (first == 1)
                                 RETURN(retval = dentry);
                 }
         }
@@ -622,6 +596,9 @@ static struct dentry *ll_lookup_it(struct inode *parent, struct dentry *dentry,
                 GOTO(out, retval = ERR_PTR(rc));
         }
 
+        if (first == -EEXIST)
+                ll_statahead_mark(dentry);
+
         if ((it->it_op & IT_OPEN) && dentry->d_inode &&
             !S_ISREG(dentry->d_inode->i_mode) &&
             !S_ISDIR(dentry->d_inode->i_mode)) {
diff --git a/lustre/llite/statahead.c b/lustre/llite/statahead.c
index f6da82ac42a76bfdab405ca108238e62ccb0a0b5..765a917acfe9525951397c2b04d793a7479aa4be 100644
--- a/lustre/llite/statahead.c
+++ b/lustre/llite/statahead.c
@@ -62,10 +62,6 @@ enum {
         SA_ENTRY_STATED
 };
 
-struct dentry_operations ll_sai_d_ops = {
-        .d_release = ll_release,
-};
-
 static unsigned int sai_generation = 0;
 static spinlock_t sai_generation_lock = SPIN_LOCK_UNLOCKED;
 
@@ -96,7 +92,7 @@ static inline int sa_received_empty(struct ll_statahead_info *sai)
 
 static inline int sa_not_full(struct ll_statahead_info *sai)
 {
-        return sai->sai_index < sai->sai_hit + sai->sai_miss + sai->sai_max;
+        return (sai->sai_index < sai->sai_hit + sai->sai_miss + sai->sai_max);
 }
 
 static inline int sa_is_running(struct ll_statahead_info *sai)
@@ -132,7 +128,7 @@ static inline int sa_low_hit(struct ll_statahead_info *sai)
  * (3) drop dentry's ref count
  * (4) release request's ref count
  */
-static void ll_sai_entry_cleanup(struct ll_sai_entry *entry)
+static void ll_sai_entry_cleanup(struct ll_sai_entry *entry, int free)
 {
         struct ptlrpc_request  *req = entry->se_req;
         struct md_enqueue_info *minfo = entry->se_minfo;
@@ -151,7 +147,10 @@ static void ll_sai_entry_cleanup(struct ll_sai_entry *entry)
                 entry->se_req = NULL;
                 ptlrpc_req_finished(req);
         }
-        OBD_FREE_PTR(entry);
+        if (free) {
+                LASSERT(list_empty(&entry->se_list));
+                OBD_FREE_PTR(entry);
+        }
 
         EXIT;
 }
@@ -190,13 +189,17 @@ struct ll_statahead_info *ll_sai_get(struct ll_statahead_info *sai)
 static void ll_sai_put(struct ll_statahead_info *sai)
 {
         struct inode         *inode = sai->sai_inode;
-        struct ll_inode_info *lli = ll_i2info(inode);
+        struct ll_inode_info *lli;
         ENTRY;
 
+        LASSERT(inode != NULL);
+        lli = ll_i2info(inode);
         if (atomic_dec_and_lock(&sai->sai_refcount, &lli->lli_lock)) {
                 struct ll_sai_entry *entry, *next;
 
+                LASSERT(lli->lli_opendir_key == NULL);
                 lli->lli_sai = NULL;
+                lli->lli_opendir_pid = 0;
                 spin_unlock(&lli->lli_lock);
 
                 LASSERT(sa_is_stopped(sai));
@@ -209,20 +212,19 @@ static void ll_sai_put(struct ll_statahead_info *sai)
 
                 list_for_each_entry_safe(entry, next, &sai->sai_entries_sent,
                                          se_list) {
-                        list_del(&entry->se_list);
-                        ll_sai_entry_cleanup(entry);
+                        list_del_init(&entry->se_list);
+                        ll_sai_entry_cleanup(entry, 1);
                 }
                 list_for_each_entry_safe(entry, next, &sai->sai_entries_received,
                                          se_list) {
-                        list_del(&entry->se_list);
-                        ll_sai_entry_cleanup(entry);
+                        list_del_init(&entry->se_list);
+                        ll_sai_entry_cleanup(entry, 1);
                 }
                 list_for_each_entry_safe(entry, next, &sai->sai_entries_stated,
                                          se_list) {
-                        list_del(&entry->se_list);
-                        ll_sai_entry_cleanup(entry);
+                        list_del_init(&entry->se_list);
+                        ll_sai_entry_cleanup(entry, 1);
                 }
-                dput(sai->sai_first);
                 OBD_FREE_PTR(sai);
                 iput(inode);
         }
@@ -294,8 +296,7 @@ ll_sai_entry_set(struct ll_statahead_info *sai, unsigned int index, int stat,
         ENTRY;
 
         if (!list_empty(&sai->sai_entries_sent)) {
-                list_for_each_entry(entry, &sai->sai_entries_sent,
-                                    se_list) {
+                list_for_each_entry(entry, &sai->sai_entries_sent, se_list) {
                         if (entry->se_index == index) {
                                 entry->se_stat = stat;
                                 entry->se_req = ptlrpc_request_addref(req);
@@ -332,13 +333,15 @@ ll_sai_entry_to_stated(struct ll_statahead_info *sai, struct ll_sai_entry *entry
         struct ll_sai_entry  *se;
         ENTRY;
 
+        ll_sai_entry_cleanup(entry, 0);
+
         spin_lock(&lli->lli_lock);
         if (!list_empty(&entry->se_list))
                 list_del_init(&entry->se_list);
 
         if (unlikely(entry->se_index < sai->sai_index_next)) {
                 spin_unlock(&lli->lli_lock);
-                ll_sai_entry_cleanup(entry);
+                OBD_FREE_PTR(entry);
                 RETURN(0);
         }
 
@@ -380,18 +383,18 @@ static int do_statahead_interpret(struct ll_statahead_info *sai)
         spin_unlock(&lli->lli_lock);
 
         if (unlikely(entry->se_index < sai->sai_index_next)) {
-                ll_sai_entry_cleanup(entry);
+                ll_sai_entry_cleanup(entry, 1);
                 RETURN(0);
         }
 
+        if (entry->se_stat != SA_ENTRY_STATED)
+                GOTO(out, rc = entry->se_stat);
+
         req = entry->se_req;
         minfo = entry->se_minfo;
         dentry = minfo->mi_dentry;
         it = &minfo->mi_it;
 
-        if (entry->se_stat != SA_ENTRY_STATED)
-                GOTO(out, rc = entry->se_stat);
-
         if (dentry->d_inode == NULL) {
                 /*
                  * lookup.
@@ -435,6 +438,7 @@ static int do_statahead_interpret(struct ll_statahead_info *sai)
                         GOTO(out, rc);
                 }
 
+                spin_lock(&ll_lookup_lock);
                 spin_lock(&dcache_lock);
                 lock_dentry(dentry);
                 __d_drop(dentry);
@@ -444,21 +448,15 @@ static int do_statahead_interpret(struct ll_statahead_info *sai)
                 unlock_dentry(dentry);
                 d_rehash_cond(dentry, 0);
                 spin_unlock(&dcache_lock);
+                spin_unlock(&ll_lookup_lock);
 
                 ll_lookup_finish_locks(it, dentry);
         }
         EXIT;
 
 out:
-        if (likely(ll_sai_entry_to_stated(sai, entry))) {
-                entry->se_minfo = NULL;
-                entry->se_req = NULL;
+        if (likely(ll_sai_entry_to_stated(sai, entry)))
                 cfs_waitq_signal(&sai->sai_waitq);
-                ll_intent_release(it);
-                OBD_FREE_PTR(minfo);
-                dput(dentry);
-                ptlrpc_req_finished(req);
-        }
         return rc;
 }
 
@@ -487,7 +485,7 @@ static int ll_statahead_interpret(struct obd_export *exp,
                 OBD_FREE_PTR(minfo);
                 RETURN(-ESTALE);
         } else {
-                sai = lli->lli_sai;
+                sai = ll_sai_get(lli->lli_sai);
                 if (rc || dir == NULL)
                         rc = -ESTALE;
 
@@ -506,8 +504,9 @@ static int ll_statahead_interpret(struct obd_export *exp,
                                 list_del_init(&entry->se_list);
                         sai->sai_replied++;
                         spin_unlock(&lli->lli_lock);
-                        ll_sai_entry_cleanup(entry);
+                        ll_sai_entry_cleanup(entry, 1);
                 }
+                ll_sai_put(sai);
                 RETURN(rc);
         }
 }
@@ -633,18 +632,11 @@ static int do_sa_revalidate(struct dentry *dentry)
         RETURN(rc);
 }
 
-static inline void ll_name2qstr(struct qstr *this, const char *name, int namelen)
+static inline void ll_name2qstr(struct qstr *q, const char *name, int namelen)
 {
-        unsigned long hash = init_name_hash();
-        unsigned int  c;
-
-        this->name = name;
-        this->len  = namelen;
-        for (; namelen > 0; namelen--, name++) {
-                c = *(const unsigned char *)name;
-                hash = partial_name_hash(c, hash);
-        }
-        this->hash = end_name_hash(hash);
+        q->name = name;
+        q->len  = namelen;
+        q->hash = full_name_hash(name, namelen);
 }
 
 static int ll_statahead_one(struct dentry *parent, struct ll_dir_entry *de)
@@ -724,12 +716,15 @@ static int ll_statahead_thread(void *arg)
         unsigned long             index = 0;
         int                       first = 0;
         int                       rc = 0;
-        char                      name[16] = "";
         ENTRY;
 
+        {
+                char pname[16];
+                snprintf(pname, 15, "ll_sa_%u", sta->sta_pid);
+                cfs_daemonize(pname);
+        }
+
         sbi->ll_sa_total++;
-        snprintf(name, 15, "ll_sa_%u", sta->sta_pid);
-        cfs_daemonize(name);
         spin_lock(&lli->lli_lock);
         thread->t_flags = SVC_RUNNING;
         spin_unlock(&lli->lli_lock);
@@ -858,22 +853,22 @@ out:
 void ll_stop_statahead(struct inode *inode, void *key)
 {
         struct ll_inode_info *lli = ll_i2info(inode);
-        struct ptlrpc_thread *thread;
+
+        if (unlikely(key == NULL))
+                return;
 
         spin_lock(&lli->lli_lock);
-        if (lli->lli_opendir_pid == 0 ||
-            unlikely(lli->lli_opendir_key != key)) {
+        if (lli->lli_opendir_key != key || lli->lli_opendir_pid == 0) {
                 spin_unlock(&lli->lli_lock);
                 return;
         }
 
         lli->lli_opendir_key = NULL;
-        lli->lli_opendir_pid = 0;
 
         if (lli->lli_sai) {
                 struct l_wait_info lwi = { 0 };
+                struct ptlrpc_thread *thread = &lli->lli_sai->sai_thread;
 
-                thread = &lli->lli_sai->sai_thread;
                 if (!sa_is_stopped(lli->lli_sai)) {
                         thread->t_flags = SVC_STOPPING;
                         spin_unlock(&lli->lli_lock);
@@ -894,9 +889,10 @@ void ll_stop_statahead(struct inode *inode, void *key)
                  * maybe inflight.
                  */
                 ll_sai_put(lli->lli_sai);
-                return;
+        } else {
+                lli->lli_opendir_pid = 0;
+                spin_unlock(&lli->lli_lock);
         }
-        spin_unlock(&lli->lli_lock);
 }
 
 enum {
@@ -949,8 +945,8 @@ static int is_first_dirent(struct inode *dir, struct dentry *dentry)
                 if (!index) {
                         if (unlikely(!(de->lde_name_len == 1 &&
                                        strncmp(de->lde_name, ".", 1) == 0)))
-                                CWARN("Maybe got bad on-disk dir: %lu\n",
-                                      dir->i_ino);
+                                CWARN("Maybe got bad on-disk dir: %lu/%u\n",
+                                      dir->i_ino, dir->i_generation);
                         /*
                          * skip "." or ingore bad entry.
                          */
@@ -958,8 +954,8 @@ static int is_first_dirent(struct inode *dir, struct dentry *dentry)
 
                         if (unlikely(!(de->lde_name_len == 2 &&
                                        strncmp(de->lde_name, "..", 2) == 0)))
-                                CWARN("Maybe got bad on-disk dir: %lu\n",
-                                      dir->i_ino);
+                                CWARN("Maybe got bad on-disk dir: %lu/%u\n",
+                                      dir->i_ino, dir->i_generation);
                         /*
                          * skip ".." or ingore bad entry.
                          */
@@ -1022,16 +1018,6 @@ int do_statahead_enter(struct inode *dir, struct dentry **dentryp, int lookup)
                              list_empty(&sai->sai_entries_stated)))
                         RETURN(-EBADFD);
 
-                /*
-                 * skip the first dentry.
-                 */
-                if (unlikely((*dentryp)->d_name.len ==
-                             sai->sai_first->d_name.len &&
-                             !strncmp((*dentryp)->d_name.name,
-                                      sai->sai_first->d_name.name,
-                                      sai->sai_first->d_name.len)))
-                        RETURN(-EEXIST);
-
                 if ((*dentryp)->d_name.name[0] == '.') {
                         if (likely(sai->sai_ls_all ||
                             sai->sai_miss_hidden >= sai->sai_skip_hidden)) {
@@ -1111,9 +1097,16 @@ int do_statahead_enter(struct inode *dir, struct dentry **dentryp, int lookup)
         if (sai == NULL)
                 RETURN(-ENOMEM);
 
-        sai->sai_inode  = igrab(dir);
-        sai->sai_first = dget(*dentryp);
         sai->sai_ls_all = (rc == LS_FIRST_DOT_DE);
+        sai->sai_inode = igrab(dir);
+        if (unlikely(sai->sai_inode == NULL)) {
+                CWARN("Do not start stat ahead on dying inode %lu/%u.\n",
+                      dir->i_ino, dir->i_generation);
+                OBD_FREE_PTR(sai);
+                RETURN(-ESTALE);
+        }
+
+        LASSERT(sai->sai_inode == (*dentryp)->d_parent->d_inode);
 
         sta.sta_parent = (*dentryp)->d_parent;
         sta.sta_pid    = cfs_curproc_pid();
@@ -1122,6 +1115,7 @@ int do_statahead_enter(struct inode *dir, struct dentry **dentryp, int lookup)
         rc = cfs_kernel_thread(ll_statahead_thread, &sta, 0);
         if (rc < 0) {
                 CERROR("can't start ll_sa thread, rc: %d\n", rc);
+                lli->lli_opendir_key = NULL;
                 sai->sai_thread.t_flags = SVC_STOPPED;
                 ll_sai_put(sai);
                 LASSERT(lli->lli_sai == NULL);
@@ -1147,7 +1141,7 @@ int ll_statahead_exit(struct dentry *dentry, int result)
         struct dentry         *parent = dentry->d_parent;
         struct ll_inode_info  *lli = ll_i2info(parent->d_inode);
         struct ll_sb_info     *sbi = ll_i2sbi(parent->d_inode);
-        struct ll_dentry_data *ldd = ll_d2d(dentry);
+        int                    rc = 0;
         ENTRY;
 
         if (lli->lli_opendir_pid != cfs_curproc_pid())
@@ -1185,21 +1179,7 @@ int ll_statahead_exit(struct dentry *dentry, int result)
                 if (!sa_is_stopped(sai))
                         cfs_waitq_signal(&sai->sai_thread.t_ctl_waitq);
                 ll_sai_entry_fini(sai);
-
-                if (unlikely(ldd == NULL)) {
-                        ll_set_dd(dentry);
-                        ldd = ll_d2d(dentry);
-                        if (ldd != NULL && dentry->d_op == NULL) {
-                                lock_dentry(dentry);
-                                dentry->d_op = dentry->d_op ? : &ll_sai_d_ops;
-                                unlock_dentry(dentry);
-                        }
-                }
-
-                if (likely(ldd != NULL))
-                        ldd->lld_sa_generation = sai->sai_generation;
-                else
-                        RETURN(-ENOMEM);
+                rc = ll_statahead_mark(dentry);
         }
-        RETURN(0);
+        RETURN(rc);
 }