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