Whamcloud - gitweb
LU-927 ptlrpc: common interfaces for ptlrpc_thread::t_flags
[fs/lustre-release.git] / lustre / llite / statahead.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011 Whamcloud, Inc.
33  *
34  */
35 /*
36  * This file is part of Lustre, http://www.lustre.org/
37  * Lustre is a trademark of Sun Microsystems, Inc.
38  */
39
40 #include <linux/fs.h>
41 #include <linux/sched.h>
42 #include <linux/mm.h>
43 #include <linux/smp_lock.h>
44 #include <linux/highmem.h>
45 #include <linux/pagemap.h>
46
47 #define DEBUG_SUBSYSTEM S_LLITE
48
49 #include <obd_support.h>
50 #include <lustre_lite.h>
51 #include <lustre_dlm.h>
52 #include <linux/lustre_version.h>
53 #include "llite_internal.h"
54
55 #define SA_OMITTED_ENTRY_MAX 8ULL
56
57 typedef enum {
58         /** negative values are for error cases */
59         SA_ENTRY_INIT = 0,      /** init entry */
60         SA_ENTRY_SUCC = 1,      /** stat succeed */
61         SA_ENTRY_INVA = 2,      /** invalid entry */
62         SA_ENTRY_DEST = 3,      /** entry to be destroyed */
63 } se_stat_t;
64
65 struct ll_sa_entry {
66         /* link into sai->sai_entries_{sent,received,stated} */
67         cfs_list_t              se_list;
68         /* link into sai hash table locally */
69         cfs_list_t              se_hash;
70         /* entry reference count */
71         cfs_atomic_t            se_refcount;
72         /* entry index in the sai */
73         __u64                   se_index;
74         /* low layer ldlm lock handle */
75         __u64                   se_handle;
76         /* entry status */
77         se_stat_t               se_stat;
78         /* entry size, contains name */
79         int                     se_size;
80         /* pointer to async getattr enqueue info */
81         struct md_enqueue_info *se_minfo;
82         /* pointer to the async getattr request */
83         struct ptlrpc_request  *se_req;
84         /* pointer to the target inode */
85         struct inode           *se_inode;
86         /* entry name */
87         struct qstr             se_qstr;
88 };
89
90 static unsigned int sai_generation = 0;
91 static cfs_spinlock_t sai_generation_lock = CFS_SPIN_LOCK_UNLOCKED;
92
93 static inline int ll_sa_entry_unlinked(struct ll_sa_entry *entry)
94 {
95         return cfs_list_empty(&entry->se_list);
96 }
97
98 static inline int ll_sa_entry_unhashed(struct ll_sa_entry *entry)
99 {
100         return cfs_list_empty(&entry->se_hash);
101 }
102
103 /*
104  * The entry only can be released by the caller, it is necessary to hold lock.
105  */
106 static inline int ll_sa_entry_stated(struct ll_sa_entry *entry)
107 {
108         smp_rmb();
109         return (entry->se_stat != SA_ENTRY_INIT);
110 }
111
112 static inline int ll_sa_entry_hash(int val)
113 {
114         return val & LL_SA_CACHE_MASK;
115 }
116
117 /*
118  * Insert entry to hash SA table.
119  */
120 static inline void
121 ll_sa_entry_enhash(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
122 {
123         int i = ll_sa_entry_hash(entry->se_qstr.hash);
124
125         cfs_spin_lock(&sai->sai_cache_lock[i]);
126         cfs_list_add_tail(&entry->se_hash, &sai->sai_cache[i]);
127         cfs_spin_unlock(&sai->sai_cache_lock[i]);
128 }
129
130 /*
131  * Remove entry from SA table.
132  */
133 static inline void
134 ll_sa_entry_unhash(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
135 {
136         int i = ll_sa_entry_hash(entry->se_qstr.hash);
137
138         cfs_spin_lock(&sai->sai_cache_lock[i]);
139         cfs_list_del_init(&entry->se_hash);
140         cfs_spin_unlock(&sai->sai_cache_lock[i]);
141 }
142
143 static inline int sa_received_empty(struct ll_statahead_info *sai)
144 {
145         return cfs_list_empty(&sai->sai_entries_received);
146 }
147
148 static inline int sa_not_full(struct ll_statahead_info *sai)
149 {
150         return (cfs_atomic_read(&sai->sai_cache_count) < sai->sai_max);
151 }
152
153 /**
154  * (1) hit ratio less than 80%
155  * or
156  * (2) consecutive miss more than 8
157  * then means low hit.
158  */
159 static inline int sa_low_hit(struct ll_statahead_info *sai)
160 {
161         return ((sai->sai_hit > 7 && sai->sai_hit < 4 * sai->sai_miss) ||
162                 (sai->sai_consecutive_miss > 8));
163 }
164
165 /*
166  * If the given index is behind of statahead window more than
167  * SA_OMITTED_ENTRY_MAX, then it is old.
168  */
169 static inline int is_omitted_entry(struct ll_statahead_info *sai, __u64 index)
170 {
171         return ((__u64)sai->sai_max + index + SA_OMITTED_ENTRY_MAX <
172                  sai->sai_index);
173 }
174
175 /*
176  * Insert it into sai_entries_sent tail when init.
177  */
178 static struct ll_sa_entry *
179 ll_sa_entry_alloc(struct ll_statahead_info *sai, __u64 index,
180                   const char *name, int len)
181 {
182         struct ll_inode_info *lli;
183         struct ll_sa_entry   *entry;
184         int                   entry_size;
185         char                 *dname;
186         ENTRY;
187
188         entry_size = sizeof(struct ll_sa_entry) + (len & ~3) + 4;
189         OBD_ALLOC(entry, entry_size);
190         if (unlikely(entry == NULL))
191                 RETURN(ERR_PTR(-ENOMEM));
192
193         CDEBUG(D_READA, "alloc sai entry %.*s(%p) index "LPU64"\n",
194                len, name, entry, index);
195
196         entry->se_index = index;
197
198         /*
199          * Statahead entry reference rules:
200          *
201          * 1) When statahead entry is initialized, its reference is set as 2.
202          *    One reference is used by the directory scanner. When the scanner
203          *    searches the statahead cache for the given name, it can perform
204          *    lockless hash lookup (only the scanner can remove entry from hash
205          *    list), and once found, it needn't to call "atomic_inc()" for the
206          *    entry reference. So the performance is improved. After using the
207          *    statahead entry, the scanner will call "atomic_dec()" to drop the
208          *    reference held when initialization. If it is the last reference,
209          *    the statahead entry will be freed.
210          *
211          * 2) All other threads, including statahead thread and ptlrpcd thread,
212          *    when they process the statahead entry, the reference for target
213          *    should be held to guarantee the entry will not be released by the
214          *    directory scanner. After processing the entry, these threads will
215          *    drop the entry reference. If it is the last reference, the entry
216          *    will be freed.
217          *
218          *    The second reference when initializes the statahead entry is used
219          *    by the statahead thread, following the rule 2).
220          */
221         cfs_atomic_set(&entry->se_refcount, 2);
222         entry->se_stat = SA_ENTRY_INIT;
223         entry->se_size = entry_size;
224         dname = (char *)entry + sizeof(struct ll_sa_entry);
225         memcpy(dname, name, len);
226         dname[len] = 0;
227         entry->se_qstr.hash = full_name_hash(name, len);
228         entry->se_qstr.len = len;
229         entry->se_qstr.name = dname;
230
231         lli = ll_i2info(sai->sai_inode);
232         cfs_spin_lock(&lli->lli_sa_lock);
233         cfs_list_add_tail(&entry->se_list, &sai->sai_entries_sent);
234         cfs_spin_unlock(&lli->lli_sa_lock);
235
236         cfs_atomic_inc(&sai->sai_cache_count);
237         ll_sa_entry_enhash(sai, entry);
238
239         RETURN(entry);
240 }
241
242 /*
243  * Used by the directory scanner to search entry with name.
244  *
245  * Only the caller can remove the entry from hash, so it is unnecessary to hold
246  * hash lock. It is caller's duty to release the init refcount on the entry, so
247  * it is also unnecessary to increase refcount on the entry.
248  */
249 static struct ll_sa_entry *
250 ll_sa_entry_get_byname(struct ll_statahead_info *sai, const struct qstr *qstr)
251 {
252         struct ll_sa_entry *entry;
253         int i = ll_sa_entry_hash(qstr->hash);
254
255         cfs_list_for_each_entry(entry, &sai->sai_cache[i], se_hash) {
256                 if (entry->se_qstr.hash == qstr->hash &&
257                     entry->se_qstr.len == qstr->len &&
258                     memcmp(entry->se_qstr.name, qstr->name, qstr->len) == 0)
259                         return entry;
260         }
261         return NULL;
262 }
263
264 /*
265  * Used by the async getattr request callback to find entry with index.
266  *
267  * Inside lli_sa_lock to prevent others to change the list during the search.
268  * It needs to increase entry refcount before returning to guarantee that the
269  * entry cannot be freed by others.
270  */
271 static struct ll_sa_entry *
272 ll_sa_entry_get_byindex(struct ll_statahead_info *sai, __u64 index)
273 {
274         struct ll_sa_entry *entry;
275
276         cfs_list_for_each_entry(entry, &sai->sai_entries_sent, se_list) {
277                 if (entry->se_index == index) {
278                         cfs_atomic_inc(&entry->se_refcount);
279                         return entry;
280                 }
281                 if (entry->se_index > index)
282                         break;
283         }
284         return NULL;
285 }
286
287 static void ll_sa_entry_cleanup(struct ll_statahead_info *sai,
288                                  struct ll_sa_entry *entry)
289 {
290         struct md_enqueue_info *minfo = entry->se_minfo;
291         struct ptlrpc_request  *req   = entry->se_req;
292
293         if (minfo) {
294                 entry->se_minfo = NULL;
295                 ll_intent_release(&minfo->mi_it);
296                 iput(minfo->mi_dir);
297                 OBD_FREE_PTR(minfo);
298         }
299
300         if (req) {
301                 entry->se_req = NULL;
302                 ptlrpc_req_finished(req);
303         }
304 }
305
306 static void ll_sa_entry_put(struct ll_statahead_info *sai,
307                              struct ll_sa_entry *entry)
308 {
309         if (cfs_atomic_dec_and_test(&entry->se_refcount)) {
310                 CDEBUG(D_READA, "free sai entry %.*s(%p) index "LPU64"\n",
311                        entry->se_qstr.len, entry->se_qstr.name, entry,
312                        entry->se_index);
313
314                 LASSERT(ll_sa_entry_unhashed(entry));
315                 LASSERT(ll_sa_entry_unlinked(entry));
316
317                 ll_sa_entry_cleanup(sai, entry);
318                 if (entry->se_inode)
319                         iput(entry->se_inode);
320
321                 OBD_FREE(entry, entry->se_size);
322                 cfs_atomic_dec(&sai->sai_cache_count);
323         }
324 }
325
326 static inline void
327 do_sai_entry_fini(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
328 {
329         struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
330
331         ll_sa_entry_unhash(sai, entry);
332
333         cfs_spin_lock(&lli->lli_sa_lock);
334         entry->se_stat = SA_ENTRY_DEST;
335         if (likely(!ll_sa_entry_unlinked(entry)))
336                 cfs_list_del_init(&entry->se_list);
337         cfs_spin_unlock(&lli->lli_sa_lock);
338
339         ll_sa_entry_put(sai, entry);
340 }
341
342 /*
343  * Delete it from sai_entries_stated list when fini.
344  */
345 static void
346 ll_sa_entry_fini(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
347 {
348         struct ll_sa_entry *pos, *next;
349
350         if (entry)
351                 do_sai_entry_fini(sai, entry);
352
353         /* drop old entry from sent list */
354         cfs_list_for_each_entry_safe(pos, next, &sai->sai_entries_sent,
355                                      se_list) {
356                 if (is_omitted_entry(sai, pos->se_index))
357                         do_sai_entry_fini(sai, pos);
358                 else
359                         break;
360         }
361
362         /* drop old entry from stated list */
363         cfs_list_for_each_entry_safe(pos, next, &sai->sai_entries_stated,
364                                      se_list) {
365                 if (is_omitted_entry(sai, pos->se_index))
366                         do_sai_entry_fini(sai, pos);
367                 else
368                         break;
369         }
370 }
371
372 /*
373  * Inside lli_sa_lock.
374  */
375 static void
376 do_sai_entry_to_stated(struct ll_statahead_info *sai,
377                        struct ll_sa_entry *entry, int rc)
378 {
379         struct ll_sa_entry *se;
380         cfs_list_t         *pos = &sai->sai_entries_stated;
381
382         if (!ll_sa_entry_unlinked(entry))
383                 cfs_list_del_init(&entry->se_list);
384
385         cfs_list_for_each_entry_reverse(se, &sai->sai_entries_stated, se_list) {
386                 if (se->se_index < entry->se_index) {
387                         pos = &se->se_list;
388                         break;
389                 }
390         }
391
392         cfs_list_add(&entry->se_list, pos);
393         entry->se_stat = rc;
394 }
395
396 /*
397  * Move entry to sai_entries_stated and sort with the index.
398  * \retval 1    -- entry to be destroyed.
399  * \retval 0    -- entry is inserted into stated list.
400  */
401 static int
402 ll_sa_entry_to_stated(struct ll_statahead_info *sai,
403                        struct ll_sa_entry *entry, int rc)
404 {
405         struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
406         int                   ret = 1;
407
408         ll_sa_entry_cleanup(sai, entry);
409
410         cfs_spin_lock(&lli->lli_sa_lock);
411         if (likely(entry->se_stat != SA_ENTRY_DEST)) {
412                 do_sai_entry_to_stated(sai, entry, rc);
413                 ret = 0;
414         }
415         cfs_spin_unlock(&lli->lli_sa_lock);
416
417         return ret;
418 }
419
420 static struct ll_statahead_info *ll_sai_alloc(void)
421 {
422         struct ll_statahead_info *sai;
423         int                       i;
424         ENTRY;
425
426         OBD_ALLOC_PTR(sai);
427         if (!sai)
428                 RETURN(NULL);
429
430         cfs_atomic_set(&sai->sai_refcount, 1);
431         cfs_spin_lock(&sai_generation_lock);
432         sai->sai_generation = ++sai_generation;
433         if (unlikely(sai_generation == 0))
434                 sai->sai_generation = ++sai_generation;
435         cfs_spin_unlock(&sai_generation_lock);
436         sai->sai_max = LL_SA_RPC_MIN;
437         cfs_waitq_init(&sai->sai_waitq);
438         cfs_waitq_init(&sai->sai_thread.t_ctl_waitq);
439         CFS_INIT_LIST_HEAD(&sai->sai_entries_sent);
440         CFS_INIT_LIST_HEAD(&sai->sai_entries_received);
441         CFS_INIT_LIST_HEAD(&sai->sai_entries_stated);
442         for (i = 0; i < LL_SA_CACHE_SIZE; i++) {
443                 CFS_INIT_LIST_HEAD(&sai->sai_cache[i]);
444                 cfs_spin_lock_init(&sai->sai_cache_lock[i]);
445         }
446         cfs_atomic_set(&sai->sai_cache_count, 0);
447
448         RETURN(sai);
449 }
450
451 static inline struct ll_statahead_info *
452 ll_sai_get(struct ll_statahead_info *sai)
453 {
454         cfs_atomic_inc(&sai->sai_refcount);
455         return sai;
456 }
457
458 static void ll_sai_put(struct ll_statahead_info *sai)
459 {
460         struct inode         *inode = sai->sai_inode;
461         struct ll_inode_info *lli   = ll_i2info(inode);
462         ENTRY;
463
464         if (cfs_atomic_dec_and_lock(&sai->sai_refcount, &lli->lli_sa_lock)) {
465                 struct ll_sa_entry *entry, *next;
466
467                 if (unlikely(cfs_atomic_read(&sai->sai_refcount) > 0)) {
468                         /* It is race case, the interpret callback just hold
469                          * a reference count */
470                         cfs_spin_unlock(&lli->lli_sa_lock);
471                         RETURN_EXIT;
472                 }
473
474                 LASSERT(lli->lli_opendir_key == NULL);
475                 lli->lli_sai = NULL;
476                 lli->lli_opendir_pid = 0;
477                 cfs_spin_unlock(&lli->lli_sa_lock);
478
479                 LASSERT(thread_is_stopped(&sai->sai_thread));
480
481                 if (sai->sai_sent > sai->sai_replied)
482                         CDEBUG(D_READA,"statahead for dir "DFID" does not "
483                               "finish: [sent:"LPU64"] [replied:"LPU64"]\n",
484                               PFID(&lli->lli_fid),
485                               sai->sai_sent, sai->sai_replied);
486
487                 cfs_list_for_each_entry_safe(entry, next,
488                                              &sai->sai_entries_sent, se_list)
489                         do_sai_entry_fini(sai, entry);
490
491                 LASSERT(sa_received_empty(sai));
492
493                 cfs_list_for_each_entry_safe(entry, next,
494                                              &sai->sai_entries_stated, se_list)
495                         do_sai_entry_fini(sai, entry);
496
497                 LASSERT(cfs_atomic_read(&sai->sai_cache_count) == 0);
498
499                 iput(inode);
500                 OBD_FREE_PTR(sai);
501         }
502
503         EXIT;
504 }
505
506 static void do_statahead_interpret(struct ll_statahead_info *sai,
507                                    struct ll_sa_entry *target)
508 {
509         struct inode           *dir   = sai->sai_inode;
510         struct inode           *child;
511         struct ll_inode_info   *lli   = ll_i2info(dir);
512         struct ll_sa_entry     *entry;
513         struct md_enqueue_info *minfo;
514         struct lookup_intent   *it;
515         struct ptlrpc_request  *req;
516         struct mdt_body        *body;
517         int                     rc    = 0;
518         ENTRY;
519
520         cfs_spin_lock(&lli->lli_sa_lock);
521         if (target != NULL && target->se_req != NULL &&
522             !cfs_list_empty(&target->se_list)) {
523                 entry = target;
524         } else if (unlikely(sa_received_empty(sai))) {
525                 cfs_spin_unlock(&lli->lli_sa_lock);
526                 RETURN_EXIT;
527         } else {
528                 entry = cfs_list_entry(sai->sai_entries_received.next,
529                                        struct ll_sa_entry, se_list);
530         }
531
532         cfs_atomic_inc(&entry->se_refcount);
533         cfs_list_del_init(&entry->se_list);
534         cfs_spin_unlock(&lli->lli_sa_lock);
535
536         LASSERT(entry->se_handle != 0);
537
538         minfo = entry->se_minfo;
539         it = &minfo->mi_it;
540         req = entry->se_req;
541         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
542         if (body == NULL)
543                 GOTO(out, rc = -EFAULT);
544
545         child = entry->se_inode;
546         if (child == NULL) {
547                 /*
548                  * lookup.
549                  */
550                 LASSERT(fid_is_zero(&minfo->mi_data.op_fid2));
551
552                 /* XXX: No fid in reply, this is probaly cross-ref case.
553                  * SA can't handle it yet. */
554                 if (body->valid & OBD_MD_MDS)
555                         GOTO(out, rc = -EAGAIN);
556         } else {
557                 /*
558                  * revalidate.
559                  */
560                 /* unlinked and re-created with the same name */
561                 if (unlikely(!lu_fid_eq(&minfo->mi_data.op_fid2, &body->fid1))){
562                         entry->se_inode = NULL;
563                         iput(child);
564                         child = NULL;
565                 }
566         }
567
568         it->d.lustre.it_lock_handle = entry->se_handle;
569         rc = md_revalidate_lock(ll_i2mdexp(dir), it, NULL, NULL);
570         if (rc != 1)
571                 GOTO(out, rc = -EAGAIN);
572
573         rc = ll_prep_inode(&child, req, dir->i_sb);
574         if (rc)
575                 GOTO(out, rc);
576
577         CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
578                child, child->i_ino, child->i_generation);
579         ll_set_lock_data(ll_i2sbi(dir)->ll_md_exp, child, it, NULL);
580
581         entry->se_inode = child;
582
583         EXIT;
584
585 out:
586         /* The "ll_sa_entry_to_stated()" will drop related ldlm ibits lock
587          * reference count by calling "ll_intent_drop_lock()" in spite of the
588          * above operations failed or not. Do not worry about calling
589          * "ll_intent_drop_lock()" more than once. */
590         rc = ll_sa_entry_to_stated(sai, entry, rc < 0 ? rc : SA_ENTRY_SUCC);
591         if (rc == 0 && entry->se_index == sai->sai_index_wait && target == NULL)
592                 cfs_waitq_signal(&sai->sai_waitq);
593         ll_sa_entry_put(sai, entry);
594 }
595
596 static int ll_statahead_interpret(struct ptlrpc_request *req,
597                                   struct md_enqueue_info *minfo, int rc)
598 {
599         struct lookup_intent     *it  = &minfo->mi_it;
600         struct inode             *dir = minfo->mi_dir;
601         struct ll_inode_info     *lli = ll_i2info(dir);
602         struct ll_statahead_info *sai = NULL;
603         struct ll_sa_entry       *entry;
604         int                       wakeup;
605         ENTRY;
606
607         if (it_disposition(it, DISP_LOOKUP_NEG))
608                 rc = -ENOENT;
609
610         cfs_spin_lock(&lli->lli_sa_lock);
611         /* stale entry */
612         if (unlikely(lli->lli_sai == NULL ||
613                      lli->lli_sai->sai_generation != minfo->mi_generation)) {
614                 cfs_spin_unlock(&lli->lli_sa_lock);
615                 GOTO(out, rc = -ESTALE);
616         } else {
617                 sai = ll_sai_get(lli->lli_sai);
618                 if (unlikely(!thread_is_running(&sai->sai_thread))) {
619                         sai->sai_replied++;
620                         cfs_spin_unlock(&lli->lli_sa_lock);
621                         GOTO(out, rc = -EBADFD);
622                 }
623
624                 entry = ll_sa_entry_get_byindex(sai, minfo->mi_cbdata);
625                 if (entry == NULL) {
626                         sai->sai_replied++;
627                         cfs_spin_unlock(&lli->lli_sa_lock);
628                         GOTO(out, rc = -EIDRM);
629                 }
630
631                 cfs_list_del_init(&entry->se_list);
632                 if (rc != 0) {
633                         sai->sai_replied++;
634                         do_sai_entry_to_stated(sai, entry, rc);
635                         cfs_spin_unlock(&lli->lli_sa_lock);
636                         if (entry->se_index == sai->sai_index_wait)
637                                 cfs_waitq_signal(&sai->sai_waitq);
638                 } else {
639                         entry->se_minfo = minfo;
640                         entry->se_req = ptlrpc_request_addref(req);
641                         /* Release the async ibits lock ASAP to avoid deadlock
642                          * when statahead thread tries to enqueue lock on parent
643                          * for readpage and other tries to enqueue lock on child
644                          * with parent's lock held, for example: unlink. */
645                         entry->se_handle = it->d.lustre.it_lock_handle;
646                         ll_intent_drop_lock(it);
647                         wakeup = sa_received_empty(sai);
648                         cfs_list_add_tail(&entry->se_list,
649                                           &sai->sai_entries_received);
650                         sai->sai_replied++;
651                         cfs_spin_unlock(&lli->lli_sa_lock);
652                         if (wakeup)
653                                 cfs_waitq_signal(&sai->sai_thread.t_ctl_waitq);
654                 }
655                 ll_sa_entry_put(sai, entry);
656         }
657
658         EXIT;
659
660 out:
661         if (rc != 0) {
662                 ll_intent_release(it);
663                 iput(dir);
664                 OBD_FREE_PTR(minfo);
665         }
666         if (sai != NULL)
667                 ll_sai_put(sai);
668         return rc;
669 }
670
671 static void sa_args_fini(struct md_enqueue_info *minfo,
672                          struct ldlm_enqueue_info *einfo)
673 {
674         LASSERT(minfo && einfo);
675         iput(minfo->mi_dir);
676         capa_put(minfo->mi_data.op_capa1);
677         capa_put(minfo->mi_data.op_capa2);
678         OBD_FREE_PTR(minfo);
679         OBD_FREE_PTR(einfo);
680 }
681
682 /**
683  * There is race condition between "capa_put" and "ll_statahead_interpret" for
684  * accessing "op_data.op_capa[1,2]" as following:
685  * "capa_put" releases "op_data.op_capa[1,2]"'s reference count after calling
686  * "md_intent_getattr_async". But "ll_statahead_interpret" maybe run first, and
687  * fill "op_data.op_capa[1,2]" as POISON, then cause "capa_put" access invalid
688  * "ocapa". So here reserve "op_data.op_capa[1,2]" in "pcapa" before calling
689  * "md_intent_getattr_async".
690  */
691 static int sa_args_init(struct inode *dir, struct inode *child,
692                         struct qstr *qstr, struct md_enqueue_info **pmi,
693                         struct ldlm_enqueue_info **pei,
694                         struct obd_capa **pcapa)
695 {
696         struct ll_inode_info     *lli = ll_i2info(dir);
697         struct md_enqueue_info   *minfo;
698         struct ldlm_enqueue_info *einfo;
699         struct md_op_data        *op_data;
700
701         OBD_ALLOC_PTR(einfo);
702         if (einfo == NULL)
703                 return -ENOMEM;
704
705         OBD_ALLOC_PTR(minfo);
706         if (minfo == NULL) {
707                 OBD_FREE_PTR(einfo);
708                 return -ENOMEM;
709         }
710
711         op_data = ll_prep_md_op_data(&minfo->mi_data, dir, child, qstr->name,
712                                      qstr->len, 0, LUSTRE_OPC_ANY, NULL);
713         if (IS_ERR(op_data)) {
714                 OBD_FREE_PTR(einfo);
715                 OBD_FREE_PTR(minfo);
716                 return PTR_ERR(op_data);
717         }
718
719         minfo->mi_it.it_op = IT_GETATTR;
720         minfo->mi_dir = igrab(dir);
721         minfo->mi_cb = ll_statahead_interpret;
722         minfo->mi_generation = lli->lli_sai->sai_generation;
723         minfo->mi_cbdata = lli->lli_sai->sai_index;
724
725         einfo->ei_type   = LDLM_IBITS;
726         einfo->ei_mode   = it_to_lock_mode(&minfo->mi_it);
727         einfo->ei_cb_bl  = ll_md_blocking_ast;
728         einfo->ei_cb_cp  = ldlm_completion_ast;
729         einfo->ei_cb_gl  = NULL;
730         einfo->ei_cbdata = NULL;
731
732         *pmi = minfo;
733         *pei = einfo;
734         pcapa[0] = op_data->op_capa1;
735         pcapa[1] = op_data->op_capa2;
736
737         return 0;
738 }
739
740 static int do_sa_lookup(struct inode *dir, struct ll_sa_entry *entry)
741 {
742         struct md_enqueue_info   *minfo;
743         struct ldlm_enqueue_info *einfo;
744         struct obd_capa          *capas[2];
745         int                       rc;
746         ENTRY;
747
748         rc = sa_args_init(dir, NULL, &entry->se_qstr, &minfo, &einfo, capas);
749         if (rc)
750                 RETURN(rc);
751
752         rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
753         if (!rc) {
754                 capa_put(capas[0]);
755                 capa_put(capas[1]);
756         } else {
757                 sa_args_fini(minfo, einfo);
758         }
759
760         RETURN(rc);
761 }
762
763 /**
764  * similar to ll_revalidate_it().
765  * \retval      1 -- dentry valid
766  * \retval      0 -- will send stat-ahead request
767  * \retval others -- prepare stat-ahead request failed
768  */
769 static int do_sa_revalidate(struct inode *dir, struct ll_sa_entry *entry,
770                             struct dentry *dentry)
771 {
772         struct inode             *inode = dentry->d_inode;
773         struct lookup_intent      it = { .it_op = IT_GETATTR,
774                                          .d.lustre.it_lock_handle = 0 };
775         struct md_enqueue_info   *minfo;
776         struct ldlm_enqueue_info *einfo;
777         struct obd_capa          *capas[2];
778         int rc;
779         ENTRY;
780
781         if (unlikely(inode == NULL))
782                 RETURN(1);
783
784         if (d_mountpoint(dentry))
785                 RETURN(1);
786
787         if (unlikely(dentry == dentry->d_sb->s_root))
788                 RETURN(1);
789
790         entry->se_inode = igrab(inode);
791         rc = md_revalidate_lock(ll_i2mdexp(dir), &it, ll_inode2fid(inode),NULL);
792         if (rc == 1) {
793                 entry->se_handle = it.d.lustre.it_lock_handle;
794                 ll_intent_release(&it);
795                 RETURN(1);
796         }
797
798         rc = sa_args_init(dir, inode, &entry->se_qstr, &minfo, &einfo, capas);
799         if (rc) {
800                 entry->se_inode = NULL;
801                 iput(inode);
802                 RETURN(rc);
803         }
804
805         rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
806         if (!rc) {
807                 capa_put(capas[0]);
808                 capa_put(capas[1]);
809         } else {
810                 entry->se_inode = NULL;
811                 iput(inode);
812                 sa_args_fini(minfo, einfo);
813         }
814
815         RETURN(rc);
816 }
817
818 static void ll_statahead_one(struct dentry *parent, const char* entry_name,
819                              int entry_name_len)
820 {
821         struct inode             *dir = parent->d_inode;
822         struct ll_inode_info     *lli = ll_i2info(dir);
823         struct ll_statahead_info *sai = lli->lli_sai;
824         struct dentry            *dentry = NULL;
825         struct ll_sa_entry       *entry;
826         int                       rc;
827         int                       rc1;
828         ENTRY;
829
830         entry = ll_sa_entry_alloc(sai, sai->sai_index, entry_name,
831                                   entry_name_len);
832         if (IS_ERR(entry))
833                 RETURN_EXIT;
834
835         dentry = d_lookup(parent, &entry->se_qstr);
836         if (!dentry)
837                 rc = do_sa_lookup(dir, entry);
838         else
839                 rc = do_sa_revalidate(dir, entry, dentry);
840
841         if (dentry != NULL)
842                 dput(dentry);
843
844         if (rc) {
845                 rc1 = ll_sa_entry_to_stated(sai, entry,
846                                         rc < 0 ? SA_ENTRY_INVA : SA_ENTRY_SUCC);
847                 if (rc1 == 0 && entry->se_index == sai->sai_index_wait)
848                         cfs_waitq_signal(&sai->sai_waitq);
849         } else {
850                 sai->sai_sent++;
851         }
852
853         sai->sai_index++;
854         /* drop one refcount on entry by ll_sa_entry_alloc */
855         ll_sa_entry_put(sai, entry);
856
857         EXIT;
858 }
859
860 static int ll_statahead_thread(void *arg)
861 {
862         struct dentry            *parent = (struct dentry *)arg;
863         struct inode             *dir = parent->d_inode;
864         struct ll_inode_info     *lli = ll_i2info(dir);
865         struct ll_sb_info        *sbi = ll_i2sbi(dir);
866         struct ll_statahead_info *sai = ll_sai_get(lli->lli_sai);
867         struct ptlrpc_thread     *thread = &sai->sai_thread;
868         struct page              *page;
869         __u64                     pos = 0;
870         int                       first = 0;
871         int                       rc = 0;
872         struct ll_dir_chain       chain;
873         ENTRY;
874
875         {
876                 char pname[16];
877                 snprintf(pname, 15, "ll_sa_%u", lli->lli_opendir_pid);
878                 cfs_daemonize(pname);
879         }
880
881         atomic_inc(&sbi->ll_sa_total);
882         cfs_spin_lock(&lli->lli_sa_lock);
883         thread_set_flags(thread, SVC_RUNNING);
884         cfs_spin_unlock(&lli->lli_sa_lock);
885         cfs_waitq_signal(&thread->t_ctl_waitq);
886         CDEBUG(D_READA, "start doing statahead for %s\n", parent->d_name.name);
887
888         lli->lli_sa_pos = 0;
889         ll_dir_chain_init(&chain);
890         page = ll_get_dir_page(NULL, dir, pos, &chain);
891
892         while (1) {
893                 struct l_wait_info lwi = { 0 };
894                 struct lu_dirpage *dp;
895                 struct lu_dirent  *ent;
896
897                 if (IS_ERR(page)) {
898                         rc = PTR_ERR(page);
899                         CDEBUG(D_READA, "error reading dir "DFID" at "LPU64
900                                "/"LPU64": [rc %d] [parent %u]\n",
901                                PFID(ll_inode2fid(dir)), pos, sai->sai_index,
902                                rc, lli->lli_opendir_pid);
903                         GOTO(out, rc);
904                 }
905
906                 dp = page_address(page);
907                 for (ent = lu_dirent_start(dp); ent != NULL;
908                      ent = lu_dirent_next(ent)) {
909                         __u64 hash;
910                         int namelen;
911                         char *name;
912
913                         hash = le64_to_cpu(ent->lde_hash);
914                         if (unlikely(hash < pos))
915                                 /*
916                                  * Skip until we find target hash value.
917                                  */
918                                 continue;
919
920                         namelen = le16_to_cpu(ent->lde_namelen);
921                         if (unlikely(namelen == 0))
922                                 /*
923                                  * Skip dummy record.
924                                  */
925                                 continue;
926
927                         name = ent->lde_name;
928                         if (name[0] == '.') {
929                                 if (namelen == 1) {
930                                         /*
931                                          * skip "."
932                                          */
933                                         continue;
934                                 } else if (name[1] == '.' && namelen == 2) {
935                                         /*
936                                          * skip ".."
937                                          */
938                                         continue;
939                                 } else if (!sai->sai_ls_all) {
940                                         /*
941                                          * skip hidden files.
942                                          */
943                                         sai->sai_skip_hidden++;
944                                         continue;
945                                 }
946                         }
947
948                         /*
949                          * don't stat-ahead first entry.
950                          */
951                         if (unlikely(++first == 1))
952                                 continue;
953
954 keep_de:
955                         l_wait_event(thread->t_ctl_waitq,
956                                      sa_not_full(sai) ||
957                                      !sa_received_empty(sai) ||
958                                      !thread_is_running(thread),
959                                      &lwi);
960
961                         while (!sa_received_empty(sai))
962                                 do_statahead_interpret(sai, NULL);
963
964                         if (unlikely(!thread_is_running(thread))) {
965                                 ll_release_page(page, 0);
966                                 GOTO(out, rc);
967                         }
968
969                         if (!sa_not_full(sai))
970                                 /*
971                                  * do not skip the current de.
972                                  */
973                                 goto keep_de;
974
975                         ll_statahead_one(parent, name, namelen);
976                 }
977                 pos = le64_to_cpu(dp->ldp_hash_end);
978                 if (pos == MDS_DIR_END_OFF) {
979                         /*
980                          * End of directory reached.
981                          */
982                         ll_release_page(page, 0);
983                         while (1) {
984                                 l_wait_event(thread->t_ctl_waitq,
985                                              !sa_received_empty(sai) ||
986                                              sai->sai_sent == sai->sai_replied||
987                                              !thread_is_running(thread),
988                                              &lwi);
989
990                                 while (!sa_received_empty(sai))
991                                         do_statahead_interpret(sai, NULL);
992
993                                 if ((sai->sai_sent == sai->sai_replied &&
994                                      sa_received_empty(sai)) ||
995                                     !thread_is_running(thread))
996                                         GOTO(out, rc = 0);
997                         }
998                 } else if (1) {
999                         /*
1000                          * chain is exhausted.
1001                          * Normal case: continue to the next page.
1002                          */
1003                         ll_release_page(page, le32_to_cpu(dp->ldp_flags) &
1004                                               LDF_COLLIDE);
1005                         lli->lli_sa_pos = pos;
1006                         sai->sai_in_readpage = 1;
1007                         page = ll_get_dir_page(NULL, dir, pos, &chain);
1008                         sai->sai_in_readpage = 0;
1009                 } else {
1010                         LASSERT(le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1011                         ll_release_page(page, 1);
1012                         /*
1013                          * go into overflow page.
1014                          */
1015                 }
1016         }
1017         EXIT;
1018
1019 out:
1020         ll_dir_chain_fini(&chain);
1021         cfs_spin_lock(&lli->lli_sa_lock);
1022         if (!sa_received_empty(sai)) {
1023                 thread_set_flags(thread, SVC_STOPPING);
1024                 cfs_spin_unlock(&lli->lli_sa_lock);
1025
1026                 /* To release the resources held by received entries. */
1027                 while (!sa_received_empty(sai))
1028                         do_statahead_interpret(sai, NULL);
1029
1030                 cfs_spin_lock(&lli->lli_sa_lock);
1031         }
1032         thread_set_flags(thread, SVC_STOPPED);
1033         cfs_spin_unlock(&lli->lli_sa_lock);
1034         cfs_waitq_signal(&sai->sai_waitq);
1035         cfs_waitq_signal(&thread->t_ctl_waitq);
1036         ll_sai_put(sai);
1037         dput(parent);
1038         CDEBUG(D_READA, "statahead thread stopped, pid %d\n",
1039                cfs_curproc_pid());
1040         return rc;
1041 }
1042
1043 /**
1044  * called in ll_file_release().
1045  */
1046 void ll_stop_statahead(struct inode *dir, void *key)
1047 {
1048         struct ll_inode_info *lli = ll_i2info(dir);
1049
1050         if (unlikely(key == NULL))
1051                 return;
1052
1053         cfs_spin_lock(&lli->lli_sa_lock);
1054         if (lli->lli_opendir_key != key || lli->lli_opendir_pid == 0) {
1055                 cfs_spin_unlock(&lli->lli_sa_lock);
1056                 return;
1057         }
1058
1059         lli->lli_opendir_key = NULL;
1060
1061         if (lli->lli_sai) {
1062                 struct l_wait_info lwi = { 0 };
1063                 struct ptlrpc_thread *thread = &lli->lli_sai->sai_thread;
1064
1065                 if (!thread_is_stopped(thread)) {
1066                         thread_set_flags(thread, SVC_STOPPING);
1067                         cfs_spin_unlock(&lli->lli_sa_lock);
1068                         cfs_waitq_signal(&thread->t_ctl_waitq);
1069
1070                         CDEBUG(D_READA, "stopping statahead thread, pid %d\n",
1071                                cfs_curproc_pid());
1072                         l_wait_event(thread->t_ctl_waitq,
1073                                      thread_is_stopped(thread),
1074                                      &lwi);
1075                 } else {
1076                         cfs_spin_unlock(&lli->lli_sa_lock);
1077                 }
1078
1079                 /*
1080                  * Put the ref which was held when first statahead_enter.
1081                  * It maybe not the last ref for some statahead requests
1082                  * maybe inflight.
1083                  */
1084                 ll_sai_put(lli->lli_sai);
1085         } else {
1086                 lli->lli_opendir_pid = 0;
1087                 cfs_spin_unlock(&lli->lli_sa_lock);
1088         }
1089 }
1090
1091 enum {
1092         /**
1093          * not first dirent, or is "."
1094          */
1095         LS_NONE_FIRST_DE = 0,
1096         /**
1097          * the first non-hidden dirent
1098          */
1099         LS_FIRST_DE,
1100         /**
1101          * the first hidden dirent, that is "."
1102          */
1103         LS_FIRST_DOT_DE
1104 };
1105
1106 static int is_first_dirent(struct inode *dir, struct dentry *dentry)
1107 {
1108         struct ll_inode_info *lli = ll_i2info(dir);
1109         struct ll_dir_chain chain;
1110         struct qstr        *target = &dentry->d_name;
1111         struct page        *page;
1112         __u64               pos = 0;
1113         int                 dot_de;
1114         int                 rc = LS_NONE_FIRST_DE;
1115         ENTRY;
1116
1117         lli->lli_sa_pos = 0;
1118         ll_dir_chain_init(&chain);
1119         page = ll_get_dir_page(NULL, dir, pos, &chain);
1120
1121         while (1) {
1122                 struct lu_dirpage *dp;
1123                 struct lu_dirent  *ent;
1124
1125                 if (IS_ERR(page)) {
1126                         struct ll_inode_info *lli = ll_i2info(dir);
1127
1128                         rc = PTR_ERR(page);
1129                         CERROR("error reading dir "DFID" at "LPU64": "
1130                                "[rc %d] [parent %u]\n",
1131                                PFID(ll_inode2fid(dir)), pos,
1132                                rc, lli->lli_opendir_pid);
1133                         break;
1134                 }
1135
1136                 dp = page_address(page);
1137                 for (ent = lu_dirent_start(dp); ent != NULL;
1138                      ent = lu_dirent_next(ent)) {
1139                         __u64 hash;
1140                         int namelen;
1141                         char *name;
1142
1143                         hash = le64_to_cpu(ent->lde_hash);
1144                         /* The ll_get_dir_page() can return any page containing
1145                          * the given hash which may be not the start hash. */
1146                         if (unlikely(hash < pos))
1147                                 continue;
1148
1149                         namelen = le16_to_cpu(ent->lde_namelen);
1150                         if (unlikely(namelen == 0))
1151                                 /*
1152                                  * skip dummy record.
1153                                  */
1154                                 continue;
1155
1156                         name = ent->lde_name;
1157                         if (name[0] == '.') {
1158                                 if (namelen == 1)
1159                                         /*
1160                                          * skip "."
1161                                          */
1162                                         continue;
1163                                 else if (name[1] == '.' && namelen == 2)
1164                                         /*
1165                                          * skip ".."
1166                                          */
1167                                         continue;
1168                                 else
1169                                         dot_de = 1;
1170                         } else {
1171                                 dot_de = 0;
1172                         }
1173
1174                         if (dot_de && target->name[0] != '.') {
1175                                 CDEBUG(D_READA, "%.*s skip hidden file %.*s\n",
1176                                        target->len, target->name,
1177                                        namelen, name);
1178                                 continue;
1179                         }
1180
1181                         if (target->len != namelen ||
1182                             memcmp(target->name, name, namelen) != 0)
1183                                 rc = LS_NONE_FIRST_DE;
1184                         else if (!dot_de)
1185                                 rc = LS_FIRST_DE;
1186                         else
1187                                 rc = LS_FIRST_DOT_DE;
1188
1189                         ll_release_page(page, 0);
1190                         GOTO(out, rc);
1191                 }
1192                 pos = le64_to_cpu(dp->ldp_hash_end);
1193                 if (pos == MDS_DIR_END_OFF) {
1194                         /*
1195                          * End of directory reached.
1196                          */
1197                         ll_release_page(page, 0);
1198                         break;
1199                 } else if (1) {
1200                         /*
1201                          * chain is exhausted
1202                          * Normal case: continue to the next page.
1203                          */
1204                         ll_release_page(page, le32_to_cpu(dp->ldp_flags) &
1205                                               LDF_COLLIDE);
1206                         lli->lli_sa_pos = pos;
1207                         page = ll_get_dir_page(NULL, dir, pos, &chain);
1208                 } else {
1209                         /*
1210                          * go into overflow page.
1211                          */
1212                         LASSERT(le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1213                         ll_release_page(page, 1);
1214                 }
1215         }
1216         EXIT;
1217
1218 out:
1219         ll_dir_chain_fini(&chain);
1220         return rc;
1221 }
1222
1223 static void
1224 ll_sai_unplug(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
1225 {
1226         struct ptlrpc_thread *thread = &sai->sai_thread;
1227         struct ll_sb_info    *sbi    = ll_i2sbi(sai->sai_inode);
1228         int                  hit;
1229         ENTRY;
1230
1231         if (entry != NULL && entry->se_stat == SA_ENTRY_SUCC)
1232                 hit = 1;
1233         else
1234                 hit = 0;
1235
1236         ll_sa_entry_fini(sai, entry);
1237         if (hit) {
1238                 sai->sai_hit++;
1239                 sai->sai_consecutive_miss = 0;
1240                 sai->sai_max = min(2 * sai->sai_max, sbi->ll_sa_max);
1241         } else {
1242                 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
1243
1244                 sai->sai_miss++;
1245                 sai->sai_consecutive_miss++;
1246                 if (sa_low_hit(sai) && thread_is_running(thread)) {
1247                         atomic_inc(&sbi->ll_sa_wrong);
1248                         CDEBUG(D_READA, "Statahead for dir "DFID" hit "
1249                                "ratio too low: hit/miss "LPU64"/"LPU64
1250                                ", sent/replied "LPU64"/"LPU64", stopping "
1251                                "statahead thread: pid %d\n",
1252                                PFID(&lli->lli_fid), sai->sai_hit,
1253                                sai->sai_miss, sai->sai_sent,
1254                                sai->sai_replied, cfs_curproc_pid());
1255                         cfs_spin_lock(&lli->lli_sa_lock);
1256                         if (!thread_is_stopped(thread))
1257                                 thread_set_flags(thread, SVC_STOPPING);
1258                         cfs_spin_unlock(&lli->lli_sa_lock);
1259                 }
1260         }
1261
1262         if (!thread_is_stopped(thread))
1263                 cfs_waitq_signal(&thread->t_ctl_waitq);
1264
1265         EXIT;
1266 }
1267
1268 /**
1269  * Start statahead thread if this is the first dir entry.
1270  * Otherwise if a thread is started already, wait it until it is ahead of me.
1271  * \retval 1       -- find entry with lock in cache, the caller needs to do
1272  *                    nothing.
1273  * \retval 0       -- find entry in cache, but without lock, the caller needs
1274  *                    refresh from MDS.
1275  * \retval others  -- the caller need to process as non-statahead.
1276  */
1277 int do_statahead_enter(struct inode *dir, struct dentry **dentryp,
1278                        int only_unplug)
1279 {
1280         struct ll_inode_info     *lli   = ll_i2info(dir);
1281         struct ll_statahead_info *sai   = lli->lli_sai;
1282         struct dentry            *parent;
1283         struct ll_sa_entry       *entry;
1284         struct ptlrpc_thread     *thread;
1285         struct l_wait_info        lwi   = { 0 };
1286         int                       rc    = 0;
1287         ENTRY;
1288
1289         LASSERT(lli->lli_opendir_pid == cfs_curproc_pid());
1290
1291         if (sai) {
1292                 thread = &sai->sai_thread;
1293                 if (unlikely(thread_is_stopped(thread) &&
1294                              cfs_list_empty(&sai->sai_entries_stated))) {
1295                         /* to release resource */
1296                         ll_stop_statahead(dir, lli->lli_opendir_key);
1297                         RETURN(-EAGAIN);
1298                 }
1299
1300                 if ((*dentryp)->d_name.name[0] == '.') {
1301                         if (sai->sai_ls_all ||
1302                             sai->sai_miss_hidden >= sai->sai_skip_hidden) {
1303                                 /*
1304                                  * Hidden dentry is the first one, or statahead
1305                                  * thread does not skip so many hidden dentries
1306                                  * before "sai_ls_all" enabled as below.
1307                                  */
1308                         } else {
1309                                 if (!sai->sai_ls_all)
1310                                         /*
1311                                          * It maybe because hidden dentry is not
1312                                          * the first one, "sai_ls_all" was not
1313                                          * set, then "ls -al" missed. Enable
1314                                          * "sai_ls_all" for such case.
1315                                          */
1316                                         sai->sai_ls_all = 1;
1317
1318                                 /*
1319                                  * Such "getattr" has been skipped before
1320                                  * "sai_ls_all" enabled as above.
1321                                  */
1322                                 sai->sai_miss_hidden++;
1323                                 RETURN(-EAGAIN);
1324                         }
1325                 }
1326
1327                 entry = ll_sa_entry_get_byname(sai, &(*dentryp)->d_name);
1328                 if (entry == NULL || only_unplug) {
1329                         ll_sai_unplug(sai, entry);
1330                         RETURN(entry ? 1 : -EAGAIN);
1331                 }
1332
1333                 while (!ll_sa_entry_stated(entry) &&
1334                        sai->sai_in_readpage &&
1335                        !sa_received_empty(sai))
1336                         do_statahead_interpret(sai, entry);
1337
1338                 if (!ll_sa_entry_stated(entry)) {
1339                         sai->sai_index_wait = entry->se_index;
1340                         lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(30), NULL,
1341                                                LWI_ON_SIGNAL_NOOP, NULL);
1342                         rc = l_wait_event(sai->sai_waitq,
1343                                           ll_sa_entry_stated(entry) ||
1344                                           thread_is_stopped(thread),
1345                                           &lwi);
1346                         if (rc < 0) {
1347                                 ll_sai_unplug(sai, entry);
1348                                 RETURN(-EAGAIN);
1349                         }
1350                 }
1351
1352                 if (entry->se_stat == SA_ENTRY_SUCC &&
1353                     entry->se_inode != NULL) {
1354                         struct inode *inode = entry->se_inode;
1355                         struct lookup_intent it = { .it_op = IT_GETATTR,
1356                                                     .d.lustre.it_lock_handle =
1357                                                      entry->se_handle };
1358                         struct ll_dentry_data *lld;
1359                         __u64 bits;
1360
1361                         rc = md_revalidate_lock(ll_i2mdexp(dir), &it,
1362                                                 ll_inode2fid(inode), &bits);
1363                         if (rc == 1) {
1364                                 if ((*dentryp)->d_inode == NULL) {
1365                                         *dentryp = ll_find_alias(inode,
1366                                                                  *dentryp);
1367                                         lld = ll_d2d(*dentryp);
1368                                         if (unlikely(lld == NULL))
1369                                                 ll_dops_init(*dentryp, 1, 1);
1370                                 } else {
1371                                         LASSERT((*dentryp)->d_inode == inode);
1372
1373                                         ll_dentry_rehash(*dentryp, 0);
1374                                         iput(inode);
1375                                 }
1376                                 entry->se_inode = NULL;
1377
1378                                 ll_dentry_reset_flags(*dentryp, bits);
1379                                 ll_intent_release(&it);
1380                         }
1381                 }
1382
1383                 ll_sai_unplug(sai, entry);
1384                 RETURN(rc);
1385         }
1386
1387         /* I am the "lli_opendir_pid" owner, only me can set "lli_sai". */
1388         rc = is_first_dirent(dir, *dentryp);
1389         if (rc == LS_NONE_FIRST_DE)
1390                 /* It is not "ls -{a}l" operation, no need statahead for it. */
1391                 GOTO(out, rc = -EAGAIN);
1392
1393         sai = ll_sai_alloc();
1394         if (sai == NULL)
1395                 GOTO(out, rc = -ENOMEM);
1396
1397         sai->sai_ls_all = (rc == LS_FIRST_DOT_DE);
1398         sai->sai_inode = igrab(dir);
1399         if (unlikely(sai->sai_inode == NULL)) {
1400                 CWARN("Do not start stat ahead on dying inode "DFID"\n",
1401                       PFID(&lli->lli_fid));
1402                 GOTO(out, rc = -ESTALE);
1403         }
1404
1405         /* get parent reference count here, and put it in ll_statahead_thread */
1406         parent = dget((*dentryp)->d_parent);
1407         if (unlikely(sai->sai_inode != parent->d_inode)) {
1408                 struct ll_inode_info *nlli = ll_i2info(parent->d_inode);
1409
1410                 CWARN("Race condition, someone changed %.*s just now: "
1411                       "old parent "DFID", new parent "DFID"\n",
1412                       (*dentryp)->d_name.len, (*dentryp)->d_name.name,
1413                       PFID(&lli->lli_fid), PFID(&nlli->lli_fid));
1414                 dput(parent);
1415                 iput(sai->sai_inode);
1416                 GOTO(out, rc = -EAGAIN);
1417         }
1418
1419         lli->lli_sai = sai;
1420         rc = cfs_create_thread(ll_statahead_thread, parent, 0);
1421         thread = &sai->sai_thread;
1422         if (rc < 0) {
1423                 CERROR("can't start ll_sa thread, rc: %d\n", rc);
1424                 dput(parent);
1425                 lli->lli_opendir_key = NULL;
1426                 thread_set_flags(thread, SVC_STOPPED);
1427                 ll_sai_put(sai);
1428                 LASSERT(lli->lli_sai == NULL);
1429                 RETURN(-EAGAIN);
1430         }
1431
1432         l_wait_event(thread->t_ctl_waitq,
1433                      thread_is_running(thread) || thread_is_stopped(thread),
1434                      &lwi);
1435
1436         /*
1437          * We don't stat-ahead for the first dirent since we are already in
1438          * lookup, and -EEXIST also indicates that this is the first dirent.
1439          */
1440         RETURN(-EEXIST);
1441
1442 out:
1443         if (sai != NULL)
1444                 OBD_FREE_PTR(sai);
1445         cfs_spin_lock(&lli->lli_sa_lock);
1446         lli->lli_opendir_key = NULL;
1447         lli->lli_opendir_pid = 0;
1448         cfs_spin_unlock(&lli->lli_sa_lock);
1449         return rc;
1450 }