Whamcloud - gitweb
LU-451 Workaround for lockd (nfsv3) locking
[fs/lustre-release.git] / lustre / llite / statahead.c
index 51cadc5..bfd7f89 100644 (file)
@@ -26,7 +26,7 @@
  * GPL HEADER END
  */
 /*
- * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  */
 /*
@@ -67,7 +67,7 @@ static cfs_spinlock_t sai_generation_lock = CFS_SPIN_LOCK_UNLOCKED;
 
 /**
  * Check whether first entry was stated already or not.
- * No need to hold lli_lock, for:
+ * No need to hold lli_sa_lock, for:
  * (1) it is me that remove entry from the list
  * (2) the statahead thread only add new entry to the list
  */
@@ -92,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_index_next + sai->sai_max);
 }
 
 static inline int sa_is_running(struct ll_statahead_info *sai)
@@ -131,7 +131,7 @@ static inline int sa_low_hit(struct ll_statahead_info *sai)
 static void ll_sai_entry_cleanup(struct ll_sai_entry *entry, int free)
 {
         struct md_enqueue_info *minfo = entry->se_minfo;
-        struct ptlrpc_request  *req = entry->se_req;
+        struct ptlrpc_request  *req   = entry->se_req;
         ENTRY;
 
         if (minfo) {
@@ -194,22 +194,20 @@ static void ll_sai_put(struct ll_statahead_info *sai)
         lli = ll_i2info(inode);
         LASSERT(lli->lli_sai == sai);
 
-        if (cfs_atomic_dec_and_test(&sai->sai_refcount)) {
+        if (cfs_atomic_dec_and_lock(&sai->sai_refcount, &lli->lli_sa_lock)) {
                 struct ll_sai_entry *entry, *next;
 
-                cfs_spin_lock(&lli->lli_lock);
                 if (unlikely(cfs_atomic_read(&sai->sai_refcount) > 0)) {
                         /* It is race case, the interpret callback just hold
                          * a reference count */
-                        cfs_spin_unlock(&lli->lli_lock);
-                        EXIT;
-                        return;
+                        cfs_spin_unlock(&lli->lli_sa_lock);
+                        RETURN_EXIT;
                 }
 
                 LASSERT(lli->lli_opendir_key == NULL);
                 lli->lli_sai = NULL;
                 lli->lli_opendir_pid = 0;
-                cfs_spin_unlock(&lli->lli_lock);
+                cfs_spin_unlock(&lli->lli_sa_lock);
 
                 LASSERT(sa_is_stopped(sai));
 
@@ -259,11 +257,11 @@ ll_sai_entry_init(struct ll_statahead_info *sai, unsigned int index)
         CDEBUG(D_READA, "alloc sai entry %p index %u\n",
                entry, index);
         entry->se_index = index;
-        entry->se_stat  = SA_ENTRY_UNSTATED;
+        entry->se_stat = SA_ENTRY_UNSTATED;
 
-        cfs_spin_lock(&lli->lli_lock);
+        cfs_spin_lock(&lli->lli_sa_lock);
         cfs_list_add_tail(&entry->se_list, &sai->sai_entries_sent);
-        cfs_spin_unlock(&lli->lli_lock);
+        cfs_spin_unlock(&lli->lli_sa_lock);
 
         RETURN(entry);
 }
@@ -272,30 +270,33 @@ ll_sai_entry_init(struct ll_statahead_info *sai, unsigned int index)
  * delete it from sai_entries_stated head when fini, it need not
  * to process entry's member.
  */
-static void ll_sai_entry_fini(struct ll_statahead_info *sai)
+static int ll_sai_entry_fini(struct ll_statahead_info *sai)
 {
         struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
         struct ll_sai_entry  *entry;
+        int rc = 0;
         ENTRY;
 
-        cfs_spin_lock(&lli->lli_lock);
+        cfs_spin_lock(&lli->lli_sa_lock);
         sai->sai_index_next++;
         if (likely(!cfs_list_empty(&sai->sai_entries_stated))) {
                 entry = cfs_list_entry(sai->sai_entries_stated.next,
                                        struct ll_sai_entry, se_list);
                 if (entry->se_index < sai->sai_index_next) {
-                        cfs_list_del(&entry->se_list);
+                        cfs_list_del_init(&entry->se_list);
+                        rc = entry->se_stat;
                         OBD_FREE_PTR(entry);
                 }
-        } else
+        } else {
                 LASSERT(sa_is_stopped(sai));
-        cfs_spin_unlock(&lli->lli_lock);
+        }
+        cfs_spin_unlock(&lli->lli_sa_lock);
 
-        EXIT;
+        RETURN(rc);
 }
 
 /**
- * inside lli_lock.
+ * inside lli_sa_lock.
  * \retval NULL : can not find the entry in sai_entries_sent with the index
  * \retval entry: find the entry in sai_entries_sent with the index
  */
@@ -314,15 +315,16 @@ ll_sai_entry_set(struct ll_statahead_info *sai, unsigned int index, int stat,
                                 entry->se_req = ptlrpc_request_addref(req);
                                 entry->se_minfo = minfo;
                                 RETURN(entry);
-                        } else if (entry->se_index > index)
+                        } else if (entry->se_index > index) {
                                 RETURN(NULL);
+                        }
                 }
         }
         RETURN(NULL);
 }
 
 /**
- * inside lli_lock.
+ * inside lli_sa_lock.
  * Move entry to sai_entries_received and
  * insert it into sai_entries_received tail.
  */
@@ -347,12 +349,13 @@ ll_sai_entry_to_stated(struct ll_statahead_info *sai, struct ll_sai_entry *entry
 
         ll_sai_entry_cleanup(entry, 0);
 
-        cfs_spin_lock(&lli->lli_lock);
+        cfs_spin_lock(&lli->lli_sa_lock);
         if (!cfs_list_empty(&entry->se_list))
                 cfs_list_del_init(&entry->se_list);
 
+        /* stale entry */
         if (unlikely(entry->se_index < sai->sai_index_next)) {
-                cfs_spin_unlock(&lli->lli_lock);
+                cfs_spin_unlock(&lli->lli_sa_lock);
                 OBD_FREE_PTR(entry);
                 RETURN(0);
         }
@@ -360,7 +363,7 @@ ll_sai_entry_to_stated(struct ll_statahead_info *sai, struct ll_sai_entry *entry
         cfs_list_for_each_entry_reverse(se, &sai->sai_entries_stated, se_list) {
                 if (se->se_index < entry->se_index) {
                         cfs_list_add(&entry->se_list, &se->se_list);
-                        cfs_spin_unlock(&lli->lli_lock);
+                        cfs_spin_unlock(&lli->lli_sa_lock);
                         RETURN(1);
                 }
         }
@@ -369,7 +372,7 @@ ll_sai_entry_to_stated(struct ll_statahead_info *sai, struct ll_sai_entry *entry
          * I am the first entry.
          */
         cfs_list_add(&entry->se_list, &sai->sai_entries_stated);
-        cfs_spin_unlock(&lli->lli_lock);
+        cfs_spin_unlock(&lli->lli_sa_lock);
         RETURN(1);
 }
 
@@ -388,12 +391,12 @@ static int do_statahead_interpret(struct ll_statahead_info *sai)
         struct mdt_body        *body;
         ENTRY;
 
-        cfs_spin_lock(&lli->lli_lock);
+        cfs_spin_lock(&lli->lli_sa_lock);
         LASSERT(!sa_received_empty(sai));
         entry = cfs_list_entry(sai->sai_entries_received.next,
                                struct ll_sai_entry, se_list);
         cfs_list_del_init(&entry->se_list);
-        cfs_spin_unlock(&lli->lli_lock);
+        cfs_spin_unlock(&lli->lli_sa_lock);
 
         if (unlikely(entry->se_index < sai->sai_index_next)) {
                 CWARN("Found stale entry: [index %u] [next %u]\n",
@@ -426,20 +429,15 @@ static int do_statahead_interpret(struct ll_statahead_info *sai)
 
                 LASSERT(fid_is_zero(&minfo->mi_data.op_fid2));
 
-                /*
-                 * XXX: No fid in reply, this is probaly cross-ref case.
-                 * SA can't handle it yet.
-                 */
+                /* XXX: No fid in reply, this is probaly cross-ref case.
+                 * SA can't handle it yet. */
                 if (body->valid & OBD_MD_MDS)
                         GOTO(out, rc = -EAGAIN);
 
+                /* Here dentry->d_inode might be NULL, because the entry may
+                 * have been removed before we start doing stat ahead. */
                 rc = ll_lookup_it_finish(req, it, &icbd);
                 if (!rc)
-                        /*
-                         * Here dentry->d_inode might be NULL,
-                         * because the entry may have been removed before
-                         * we start doing stat ahead.
-                         */
                         ll_lookup_finish_locks(it, dentry);
 
                 if (dentry != save) {
@@ -476,6 +474,10 @@ static int do_statahead_interpret(struct ll_statahead_info *sai)
         EXIT;
 
 out:
+        /* The "ll_sai_entry_to_stated()" will drop related ldlm ibits lock
+         * reference count with ll_intent_drop_lock() called in spite of the
+         * above operations failed or not. Do not worry about calling
+         * "ll_intent_drop_lock()" more than once. */
         if (likely(ll_sai_entry_to_stated(sai, entry)))
                 cfs_waitq_signal(&sai->sai_waitq);
         return rc;
@@ -496,10 +498,11 @@ static int ll_statahead_interpret(struct ptlrpc_request *req,
         CDEBUG(D_READA, "interpret statahead %.*s rc %d\n",
                dentry->d_name.len, dentry->d_name.name, rc);
 
-        cfs_spin_lock(&lli->lli_lock);
+        cfs_spin_lock(&lli->lli_sa_lock);
+        /* stale entry */
         if (unlikely(lli->lli_sai == NULL ||
             lli->lli_sai->sai_generation != minfo->mi_generation)) {
-                cfs_spin_unlock(&lli->lli_lock);
+                cfs_spin_unlock(&lli->lli_sa_lock);
                 ll_intent_release(it);
                 dput(dentry);
                 iput(dir);
@@ -509,19 +512,19 @@ static int ll_statahead_interpret(struct ptlrpc_request *req,
                 sai = ll_sai_get(lli->lli_sai);
                 entry = ll_sai_entry_set(sai,
                                          (unsigned int)(long)minfo->mi_cbdata,
-                                         rc ? SA_ENTRY_UNSTATED :
-                                         SA_ENTRY_STATED, req, minfo);
+                                         rc < 0 ? rc : SA_ENTRY_STATED, req,
+                                         minfo);
                 LASSERT(entry != NULL);
                 if (likely(sa_is_running(sai))) {
                         ll_sai_entry_to_received(sai, entry);
                         sai->sai_replied++;
-                        cfs_spin_unlock(&lli->lli_lock);
+                        cfs_spin_unlock(&lli->lli_sa_lock);
                         cfs_waitq_signal(&sai->sai_thread.t_ctl_waitq);
                 } else {
                         if (!cfs_list_empty(&entry->se_list))
                                 cfs_list_del_init(&entry->se_list);
                         sai->sai_replied++;
-                        cfs_spin_unlock(&lli->lli_lock);
+                        cfs_spin_unlock(&lli->lli_sa_lock);
                         ll_sai_entry_cleanup(entry, 1);
                 }
                 ll_sai_put(sai);
@@ -642,13 +645,13 @@ static int do_sa_revalidate(struct inode *dir, struct dentry *dentry)
         int rc;
         ENTRY;
 
-        if (inode == NULL)
+        if (unlikely(inode == NULL))
                 RETURN(1);
 
         if (d_mountpoint(dentry))
                 RETURN(1);
 
-        if (dentry == dentry->d_sb->s_root)
+        if (unlikely(dentry == dentry->d_sb->s_root))
                 RETURN(1);
 
         rc = md_revalidate_lock(ll_i2mdexp(dir), &it, ll_inode2fid(inode));
@@ -686,7 +689,7 @@ static int ll_statahead_one(struct dentry *parent, const char* entry_name,
         struct ll_inode_info     *lli = ll_i2info(dir);
         struct ll_statahead_info *sai = lli->lli_sai;
         struct qstr               name;
-        struct dentry            *dentry;
+        struct dentry            *dentry = NULL;
         struct ll_sai_entry      *se;
         int                       rc;
         ENTRY;
@@ -706,26 +709,23 @@ static int ll_statahead_one(struct dentry *parent, const char* entry_name,
         dentry = d_lookup(parent, &name);
         if (!dentry) {
                 dentry = d_alloc(parent, &name);
-                if (dentry) {
+                if (dentry)
                         rc = do_sa_lookup(dir, dentry);
-                        if (rc)
-                                dput(dentry);
-                } else {
+                else
                         GOTO(out, rc = -ENOMEM);
-                }
         } else {
                 rc = do_sa_revalidate(dir, dentry);
-                if (rc)
-                        dput(dentry);
         }
 
         EXIT;
 
 out:
         if (rc) {
+                if (dentry != NULL)
+                        dput(dentry);
+                se->se_stat = rc < 0 ? rc : SA_ENTRY_STATED;
                 CDEBUG(D_READA, "set sai entry %p index %u stat %d rc %d\n",
                        se, se->se_index, se->se_stat, rc);
-                se->se_stat = rc;
                 if (ll_sai_entry_to_stated(sai, se))
                         cfs_waitq_signal(&sai->sai_waitq);
         } else {
@@ -757,15 +757,17 @@ static int ll_statahead_thread(void *arg)
                 cfs_daemonize(pname);
         }
 
-        sbi->ll_sa_total++;
-        cfs_spin_lock(&lli->lli_lock);
+        atomic_inc(&sbi->ll_sa_total);
+        cfs_spin_lock(&lli->lli_sa_lock);
         thread->t_flags = SVC_RUNNING;
-        cfs_spin_unlock(&lli->lli_lock);
+        cfs_spin_unlock(&lli->lli_sa_lock);
         cfs_waitq_signal(&thread->t_ctl_waitq);
         CDEBUG(D_READA, "start doing statahead for %s\n", parent->d_name.name);
 
+        sai->sai_pid = cfs_curproc_pid();
+        lli->lli_sa_pos = 0;
         ll_dir_chain_init(&chain);
-        page = ll_get_dir_page(dir, pos, 0, &chain);
+        page = ll_get_dir_page(NULL, dir, pos, 0, &chain);
 
         while (1) {
                 struct l_wait_info lwi = { 0 };
@@ -774,24 +776,35 @@ static int ll_statahead_thread(void *arg)
 
                 if (IS_ERR(page)) {
                         rc = PTR_ERR(page);
-                        CDEBUG(D_READA, "error reading dir "DFID" at "LPU64"/%u: rc %d\n",
-                               PFID(ll_inode2fid(dir)), pos,
-                               sai->sai_index, rc);
+                        CDEBUG(D_READA, "error reading dir "DFID" at "LPU64
+                               "/%u: [rc %d] [parent %u]\n",
+                               PFID(ll_inode2fid(dir)), pos, sai->sai_index,
+                               rc, lli->lli_opendir_pid);
                         break;
                 }
 
                 dp = page_address(page);
                 for (ent = lu_dirent_start(dp); ent != NULL;
                      ent = lu_dirent_next(ent)) {
-                        char *name = ent->lde_name;
-                        int namelen = le16_to_cpu(ent->lde_namelen);
+                        __u64 hash;
+                        int namelen;
+                        char *name;
 
-                        if (namelen == 0)
+                        hash = le64_to_cpu(ent->lde_hash);
+                        if (unlikely(hash < pos))
+                                /*
+                                 * Skip until we find target hash value.
+                                 */
+                                continue;
+
+                        namelen = le16_to_cpu(ent->lde_namelen);
+                        if (unlikely(namelen == 0))
                                 /*
                                  * Skip dummy record.
                                  */
                                 continue;
 
+                        name = ent->lde_name;
                         if (name[0] == '.') {
                                 if (namelen == 1) {
                                         /*
@@ -848,7 +861,7 @@ keep_de:
                 }
                 pos = le64_to_cpu(dp->ldp_hash_end);
                 ll_put_page(page);
-                if (pos == DIR_END_OFF) {
+                if (pos == MDS_DIR_END_OFF) {
                         /*
                          * End of directory reached.
                          */
@@ -869,7 +882,8 @@ keep_de:
                          * chain is exhausted.
                          * Normal case: continue to the next page.
                          */
-                        page = ll_get_dir_page(dir, pos, 1, &chain);
+                        lli->lli_sa_pos = pos;
+                        page = ll_get_dir_page(NULL, dir, pos, 1, &chain);
                 } else {
                         /*
                          * go into overflow page.
@@ -880,9 +894,9 @@ keep_de:
 
 out:
         ll_dir_chain_fini(&chain);
-        cfs_spin_lock(&lli->lli_lock);
+        cfs_spin_lock(&lli->lli_sa_lock);
         thread->t_flags = SVC_STOPPED;
-        cfs_spin_unlock(&lli->lli_lock);
+        cfs_spin_unlock(&lli->lli_sa_lock);
         cfs_waitq_signal(&sai->sai_waitq);
         cfs_waitq_signal(&thread->t_ctl_waitq);
         ll_sai_put(sai);
@@ -895,16 +909,16 @@ out:
 /**
  * called in ll_file_release().
  */
-void ll_stop_statahead(struct inode *inode, void *key)
+void ll_stop_statahead(struct inode *dir, void *key)
 {
-        struct ll_inode_info *lli = ll_i2info(inode);
+        struct ll_inode_info *lli = ll_i2info(dir);
 
         if (unlikely(key == NULL))
                 return;
 
-        cfs_spin_lock(&lli->lli_lock);
+        cfs_spin_lock(&lli->lli_sa_lock);
         if (lli->lli_opendir_key != key || lli->lli_opendir_pid == 0) {
-                cfs_spin_unlock(&lli->lli_lock);
+                cfs_spin_unlock(&lli->lli_sa_lock);
                 return;
         }
 
@@ -916,7 +930,7 @@ void ll_stop_statahead(struct inode *inode, void *key)
 
                 if (!sa_is_stopped(lli->lli_sai)) {
                         thread->t_flags = SVC_STOPPING;
-                        cfs_spin_unlock(&lli->lli_lock);
+                        cfs_spin_unlock(&lli->lli_sa_lock);
                         cfs_waitq_signal(&thread->t_ctl_waitq);
 
                         CDEBUG(D_READA, "stopping statahead thread, pid %d\n",
@@ -925,7 +939,7 @@ void ll_stop_statahead(struct inode *inode, void *key)
                                      sa_is_stopped(lli->lli_sai),
                                      &lwi);
                 } else {
-                        cfs_spin_unlock(&lli->lli_lock);
+                        cfs_spin_unlock(&lli->lli_sa_lock);
                 }
 
                 /*
@@ -936,7 +950,7 @@ void ll_stop_statahead(struct inode *inode, void *key)
                 ll_sai_put(lli->lli_sai);
         } else {
                 lli->lli_opendir_pid = 0;
-                cfs_spin_unlock(&lli->lli_lock);
+                cfs_spin_unlock(&lli->lli_sa_lock);
         }
 }
 
@@ -957,6 +971,7 @@ enum {
 
 static int is_first_dirent(struct inode *dir, struct dentry *dentry)
 {
+        struct ll_inode_info *lli = ll_i2info(dir);
         struct ll_dir_chain chain;
         struct qstr        *target = &dentry->d_name;
         struct page        *page;
@@ -965,32 +980,39 @@ static int is_first_dirent(struct inode *dir, struct dentry *dentry)
         int                 rc = LS_NONE_FIRST_DE;
         ENTRY;
 
+        lli->lli_sa_pos = 0;
         ll_dir_chain_init(&chain);
-        page = ll_get_dir_page(dir, pos, 0, &chain);
+        page = ll_get_dir_page(NULL, dir, pos, 0, &chain);
 
         while (1) {
                 struct lu_dirpage *dp;
                 struct lu_dirent  *ent;
 
                 if (IS_ERR(page)) {
+                        struct ll_inode_info *lli = ll_i2info(dir);
+
                         rc = PTR_ERR(page);
-                        CERROR("error reading dir "DFID" at "LPU64": rc %d\n",
-                               PFID(ll_inode2fid(dir)), pos, rc);
+                        CERROR("error reading dir "DFID" at "LPU64": "
+                               "[rc %d] [parent %u]\n",
+                               PFID(ll_inode2fid(dir)), pos,
+                               rc, lli->lli_opendir_pid);
                         break;
                 }
 
                 dp = page_address(page);
                 for (ent = lu_dirent_start(dp); ent != NULL;
                      ent = lu_dirent_next(ent)) {
-                        char *name = ent->lde_name;
-                        int namelen = le16_to_cpu(ent->lde_namelen);
+                        int namelen;
+                        char *name;
 
-                        if (namelen == 0)
+                        namelen = le16_to_cpu(ent->lde_namelen);
+                        if (unlikely(namelen == 0))
                                 /*
                                  * skip dummy record.
                                  */
                                 continue;
 
+                        name = ent->lde_name;
                         if (name[0] == '.') {
                                 if (namelen == 1)
                                         /*
@@ -1028,7 +1050,7 @@ static int is_first_dirent(struct inode *dir, struct dentry *dentry)
                 }
                 pos = le64_to_cpu(dp->ldp_hash_end);
                 ll_put_page(page);
-                if (pos == DIR_END_OFF) {
+                if (pos == MDS_DIR_END_OFF) {
                         /*
                          * End of directory reached.
                          */
@@ -1038,7 +1060,8 @@ static int is_first_dirent(struct inode *dir, struct dentry *dentry)
                          * chain is exhausted
                          * Normal case: continue to the next page.
                          */
-                        page = ll_get_dir_page(dir, pos, 1, &chain);
+                        lli->lli_sa_pos = pos;
+                        page = ll_get_dir_page(NULL, dir, pos, 1, &chain);
                 } else {
                         /*
                          * go into overflow page.
@@ -1077,8 +1100,6 @@ int do_statahead_enter(struct inode *dir, struct dentry **dentryp, int lookup)
         sai = lli->lli_sai;
 
         if (sai) {
-                struct ll_sb_info *sbi;
-
                 if (unlikely(sa_is_stopped(sai) &&
                              cfs_list_empty(&sai->sai_entries_stated)))
                         RETURN(-EBADFD);
@@ -1110,11 +1131,7 @@ int do_statahead_enter(struct inode *dir, struct dentry **dentryp, int lookup)
                         }
                 }
 
-                sbi = ll_i2sbi(dir);
-                if (ll_sai_entry_stated(sai)) {
-                        sbi->ll_sa_cached++;
-                } else {
-                        sbi->ll_sa_blocked++;
+                if (!ll_sai_entry_stated(sai)) {
                         /*
                          * thread started already, avoid double-stat.
                          */
@@ -1123,6 +1140,8 @@ int do_statahead_enter(struct inode *dir, struct dentry **dentryp, int lookup)
                                           ll_sai_entry_stated(sai) ||
                                           sa_is_stopped(sai),
                                           &lwi);
+                        if (unlikely(rc == -EINTR))
+                                RETURN(rc);
                 }
 
                 if (lookup) {
@@ -1141,7 +1160,7 @@ int do_statahead_enter(struct inode *dir, struct dentry **dentryp, int lookup)
                 /*
                  * do nothing for revalidate.
                  */
-                RETURN(rc);
+                RETURN(0);
         }
 
         /* I am the "lli_opendir_pid" owner, only me can set "lli_sai". */
@@ -1157,7 +1176,7 @@ int do_statahead_enter(struct inode *dir, struct dentry **dentryp, int lookup)
         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 "DFID" .\n",
+                CWARN("Do not start stat ahead on dying inode "DFID"\n",
                       PFID(&lli->lli_fid));
                 OBD_FREE_PTR(sai);
                 GOTO(out, rc = -ESTALE);
@@ -1169,7 +1188,7 @@ int do_statahead_enter(struct inode *dir, struct dentry **dentryp, int lookup)
                 struct ll_inode_info *nlli = ll_i2info(parent->d_inode);
 
                 CWARN("Race condition, someone changed %.*s just now: "
-                      "old parent "DFID", new parent "DFID" .\n",
+                      "old parent "DFID", new parent "DFID"\n",
                       (*dentryp)->d_name.len, (*dentryp)->d_name.name,
                       PFID(&lli->lli_fid), PFID(&nlli->lli_fid));
                 dput(parent);
@@ -1179,7 +1198,7 @@ int do_statahead_enter(struct inode *dir, struct dentry **dentryp, int lookup)
         }
 
         lli->lli_sai = sai;
-        rc = cfs_kernel_thread(ll_statahead_thread, parent, 0);
+        rc = cfs_create_thread(ll_statahead_thread, parent, 0);
         if (rc < 0) {
                 CERROR("can't start ll_sa thread, rc: %d\n", rc);
                 dput(parent);
@@ -1201,10 +1220,10 @@ int do_statahead_enter(struct inode *dir, struct dentry **dentryp, int lookup)
         RETURN(-EEXIST);
 
 out:
-        cfs_spin_lock(&lli->lli_lock);
+        cfs_spin_lock(&lli->lli_sa_lock);
         lli->lli_opendir_key = NULL;
         lli->lli_opendir_pid = 0;
-        cfs_spin_unlock(&lli->lli_lock);
+        cfs_spin_unlock(&lli->lli_sa_lock);
         return rc;
 }
 
@@ -1217,6 +1236,7 @@ void ll_statahead_exit(struct inode *dir, struct dentry *dentry, int result)
         struct ll_statahead_info *sai;
         struct ll_sb_info        *sbi;
         struct ll_dentry_data    *ldd = ll_d2d(dentry);
+        int                       rc;
         ENTRY;
 
         LASSERT(dir != NULL);
@@ -1226,33 +1246,39 @@ void ll_statahead_exit(struct inode *dir, struct dentry *dentry, int result)
         LASSERT(sai != NULL);
         sbi = ll_i2sbi(dir);
 
-        if (result >= 1) {
-                sbi->ll_sa_hit++;
+        rc = ll_sai_entry_fini(sai);
+        /* rc == -ENOENT means such dentry was removed just between statahead
+         * readdir and pre-fetched, count it as hit.
+         *
+         * result == -ENOENT has two meanings:
+         * 1. such dentry was removed just between statahead pre-fetched and
+         *    main process stat such dentry.
+         * 2. main process stat non-exist dentry.
+         * We can not distinguish such two cases, just count them as miss. */
+        if (result >= 1 || unlikely(rc == -ENOENT)) {
                 sai->sai_hit++;
                 sai->sai_consecutive_miss = 0;
                 sai->sai_max = min(2 * sai->sai_max, sbi->ll_sa_max);
         } else {
-                sbi->ll_sa_miss++;
                 sai->sai_miss++;
                 sai->sai_consecutive_miss++;
                 if (sa_low_hit(sai) && sa_is_running(sai)) {
-                        sbi->ll_sa_wrong++;
+                        atomic_inc(&sbi->ll_sa_wrong);
                         CDEBUG(D_READA, "Statahead for dir "DFID" hit ratio "
                                "too low: hit/miss %u/%u, sent/replied %u/%u, "
                                "stopping statahead thread: pid %d\n",
                                PFID(&lli->lli_fid), sai->sai_hit,
                                sai->sai_miss, sai->sai_sent,
                                sai->sai_replied, cfs_curproc_pid());
-                        cfs_spin_lock(&lli->lli_lock);
+                        cfs_spin_lock(&lli->lli_sa_lock);
                         if (!sa_is_stopped(sai))
                                 sai->sai_thread.t_flags = SVC_STOPPING;
-                        cfs_spin_unlock(&lli->lli_lock);
+                        cfs_spin_unlock(&lli->lli_sa_lock);
                 }
         }
 
         if (!sa_is_stopped(sai))
                 cfs_waitq_signal(&sai->sai_thread.t_ctl_waitq);
-        ll_sai_entry_fini(sai);
         if (likely(ldd != NULL))
                 ldd->lld_sa_generation = sai->sai_generation;