Whamcloud - gitweb
LU-3270 statahead: use dcache-like interface for sa entry
[fs/lustre-release.git] / lustre / llite / statahead.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #include <linux/fs.h>
38 #include <linux/sched.h>
39 #include <linux/mm.h>
40 #include <linux/highmem.h>
41 #include <linux/pagemap.h>
42
43 #define DEBUG_SUBSYSTEM S_LLITE
44
45 #include <obd_support.h>
46 #include <lustre_dlm.h>
47 #include "llite_internal.h"
48
49 #define SA_OMITTED_ENTRY_MAX 8ULL
50
51 typedef enum {
52         /** negative values are for error cases */
53         SA_ENTRY_INIT = 0,      /** init entry */
54         SA_ENTRY_SUCC = 1,      /** stat succeed */
55         SA_ENTRY_INVA = 2,      /** invalid entry */
56 } se_state_t;
57
58 /* sa_entry is not refcounted: statahead thread allocates it and do async stat,
59  * and in async stat callback ll_statahead_interpret() will add it into
60  * sai_cb_entries, later statahead thread will call sa_handle_callback() to
61  * instantiate entry and move it into sai_entries, and then only scanner process
62  * can access and free it. */
63 struct sa_entry {
64         /* link into sai_cb_entries or sai_entries */
65         struct list_head        se_list;
66         /* link into sai hash table locally */
67         struct list_head        se_hash;
68         /* entry index in the sai */
69         __u64                   se_index;
70         /* low layer ldlm lock handle */
71         __u64                   se_handle;
72         /* entry status */
73         se_state_t              se_state;
74         /* entry size, contains name */
75         int                     se_size;
76         /* pointer to async getattr enqueue info */
77         struct md_enqueue_info *se_minfo;
78         /* pointer to the async getattr request */
79         struct ptlrpc_request  *se_req;
80         /* pointer to the target inode */
81         struct inode           *se_inode;
82         /* entry name */
83         struct qstr             se_qstr;
84 };
85
86 static unsigned int sai_generation = 0;
87 static DEFINE_SPINLOCK(sai_generation_lock);
88
89 static inline int sa_unhashed(struct sa_entry *entry)
90 {
91         return list_empty(&entry->se_hash);
92 }
93
94 /*
95  * The entry only can be released by the caller, it is necessary to hold lock.
96  */
97 static inline int sa_ready(struct sa_entry *entry)
98 {
99         smp_rmb();
100         return (entry->se_state != SA_ENTRY_INIT);
101 }
102
103 static inline int sa_hash(int val)
104 {
105         return val & LL_SA_CACHE_MASK;
106 }
107
108 /*
109  * Insert entry to hash SA table.
110  */
111 static inline void
112 sa_rehash(struct ll_statahead_info *sai, struct sa_entry *entry)
113 {
114         int i = sa_hash(entry->se_qstr.hash);
115
116         spin_lock(&sai->sai_cache_lock[i]);
117         list_add_tail(&entry->se_hash, &sai->sai_cache[i]);
118         spin_unlock(&sai->sai_cache_lock[i]);
119 }
120
121 /*
122  * Remove entry from SA table.
123  */
124 static inline void
125 sa_unhash(struct ll_statahead_info *sai, struct sa_entry *entry)
126 {
127         int i = sa_hash(entry->se_qstr.hash);
128
129         spin_lock(&sai->sai_cache_lock[i]);
130         list_del_init(&entry->se_hash);
131         spin_unlock(&sai->sai_cache_lock[i]);
132 }
133
134 static inline int agl_should_run(struct ll_statahead_info *sai,
135                                  struct inode *inode)
136 {
137         return (inode != NULL && S_ISREG(inode->i_mode) && sai->sai_agl_valid);
138 }
139
140 static inline struct ll_inode_info *
141 agl_first_entry(struct ll_statahead_info *sai)
142 {
143         return list_entry(sai->sai_agls.next, struct ll_inode_info,
144                           lli_agl_list);
145 }
146
147 static inline int sa_sent_full(struct ll_statahead_info *sai)
148 {
149         return atomic_read(&sai->sai_cache_count) >= sai->sai_max;
150 }
151
152 static inline int sa_has_callback(struct ll_statahead_info *sai)
153 {
154         return !list_empty(&sai->sai_interim_entries);
155 }
156
157 static inline int agl_list_empty(struct ll_statahead_info *sai)
158 {
159         return list_empty(&sai->sai_agls);
160 }
161
162 /**
163  * (1) hit ratio less than 80%
164  * or
165  * (2) consecutive miss more than 8
166  * then means low hit.
167  */
168 static inline int sa_low_hit(struct ll_statahead_info *sai)
169 {
170         return ((sai->sai_hit > 7 && sai->sai_hit < 4 * sai->sai_miss) ||
171                 (sai->sai_consecutive_miss > 8));
172 }
173
174 /*
175  * If the given index is behind of statahead window more than
176  * SA_OMITTED_ENTRY_MAX, then it is old.
177  */
178 static inline int is_omitted_entry(struct ll_statahead_info *sai, __u64 index)
179 {
180         return ((__u64)sai->sai_max + index + SA_OMITTED_ENTRY_MAX <
181                  sai->sai_index);
182 }
183
184 /* allocate sa_entry and add it into hash to let scanner process to find it */
185 static struct sa_entry *
186 sa_alloc(struct ll_statahead_info *sai, __u64 index, const char *name, int len)
187 {
188         struct ll_inode_info *lli;
189         struct sa_entry *entry;
190         int entry_size;
191         char *dname;
192         ENTRY;
193
194         entry_size = sizeof(struct sa_entry) + (len & ~3) + 4;
195         OBD_ALLOC(entry, entry_size);
196         if (unlikely(entry == NULL))
197                 RETURN(ERR_PTR(-ENOMEM));
198
199         CDEBUG(D_READA, "alloc sa entry %.*s(%p) index "LPU64"\n",
200                len, name, entry, index);
201
202         entry->se_index = index;
203
204         entry->se_state = SA_ENTRY_INIT;
205         entry->se_size = entry_size;
206         dname = (char *)entry + sizeof(struct sa_entry);
207         memcpy(dname, name, len);
208         dname[len] = 0;
209         entry->se_qstr.hash = full_name_hash(name, len);
210         entry->se_qstr.len = len;
211         entry->se_qstr.name = dname;
212
213         lli = ll_i2info(sai->sai_inode);
214         spin_lock(&lli->lli_sa_lock);
215         INIT_LIST_HEAD(&entry->se_list);
216         sa_rehash(sai, entry);
217         spin_unlock(&lli->lli_sa_lock);
218
219         atomic_inc(&sai->sai_cache_count);
220
221         RETURN(entry);
222 }
223
224 /* free sa_entry which should have been unhashed and not in any list */
225 static void sa_free(struct ll_statahead_info *sai, struct sa_entry *entry)
226 {
227         CDEBUG(D_READA, "free sa entry %.*s(%p) index "LPU64"\n",
228                entry->se_qstr.len, entry->se_qstr.name, entry,
229                entry->se_index);
230
231         LASSERT(list_empty(&entry->se_list));
232         LASSERT(sa_unhashed(entry));
233
234         OBD_FREE(entry, entry->se_size);
235         atomic_dec(&sai->sai_cache_count);
236 }
237
238 /* find sa_entry by name, used by directory scanner, lock is not needed because
239  * only scanner can remove the entry from hash.
240  */
241 static struct sa_entry *
242 sa_get(struct ll_statahead_info *sai, const struct qstr *qstr)
243 {
244         struct sa_entry *entry;
245         int i = sa_hash(qstr->hash);
246
247         list_for_each_entry(entry, &sai->sai_cache[i], se_hash) {
248                 if (entry->se_qstr.hash == qstr->hash &&
249                     entry->se_qstr.len == qstr->len &&
250                     memcmp(entry->se_qstr.name, qstr->name, qstr->len) == 0)
251                         return entry;
252         }
253         return NULL;
254 }
255
256 static inline void
257 sa_kill(struct ll_statahead_info *sai, struct sa_entry *entry)
258 {
259         struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
260
261         LASSERT(!sa_unhashed(entry));
262         LASSERT(!list_empty(&entry->se_list));
263         LASSERT(sa_ready(entry));
264
265         sa_unhash(sai, entry);
266
267         spin_lock(&lli->lli_sa_lock);
268         list_del_init(&entry->se_list);
269         spin_unlock(&lli->lli_sa_lock);
270
271         if (entry->se_inode != NULL)
272                 iput(entry->se_inode);
273
274         sa_free(sai, entry);
275 }
276
277 /* called by scanner after use, sa_entry will be killed */
278 static void
279 sa_put(struct ll_statahead_info *sai, struct sa_entry *entry)
280 {
281         struct sa_entry *tmp, *next;
282
283         if (entry != NULL && entry->se_state == SA_ENTRY_SUCC) {
284                 struct ll_sb_info *sbi = ll_i2sbi(sai->sai_inode);
285
286                 sai->sai_hit++;
287                 sai->sai_consecutive_miss = 0;
288                 sai->sai_max = min(2 * sai->sai_max, sbi->ll_sa_max);
289         } else {
290                 sai->sai_miss++;
291                 sai->sai_consecutive_miss++;
292         }
293
294         if (entry != NULL)
295                 sa_kill(sai, entry);
296
297         /* kill old completed entries, only scanner process does this, no need
298          * to lock */
299         list_for_each_entry_safe(tmp, next, &sai->sai_entries, se_list) {
300                 if (!is_omitted_entry(sai, tmp->se_index))
301                         break;
302                 sa_kill(sai, tmp);
303         }
304
305         wake_up(&sai->sai_thread.t_ctl_waitq);
306 }
307
308 /* update state and sort add entry to sai_entries by index, return true if
309  * scanner is waiting on this entry. */
310 static bool
311 __sa_make_ready(struct ll_statahead_info *sai, struct sa_entry *entry, int ret)
312 {
313         struct sa_entry *se;
314         struct list_head *pos = &sai->sai_entries;
315
316         LASSERT(!sa_ready(entry));
317         LASSERT(list_empty(&entry->se_list));
318
319         entry->se_state = ret < 0 ? SA_ENTRY_INVA : SA_ENTRY_SUCC;
320
321         list_for_each_entry_reverse(se, &sai->sai_entries, se_list) {
322                 if (se->se_index < entry->se_index) {
323                         pos = &se->se_list;
324                         break;
325                 }
326         }
327         list_add(&entry->se_list, pos);
328
329         return (entry->se_index == sai->sai_index_wait);
330 }
331
332 /* release resources used in async stat RPC, complete entry information and
333  * wakeup if necessary */
334 static void
335 sa_make_ready(struct ll_statahead_info *sai, struct sa_entry *entry, int ret)
336 {
337         struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
338         struct md_enqueue_info *minfo = entry->se_minfo;
339         struct ptlrpc_request *req = entry->se_req;
340         bool wakeup;
341
342         /* release resources used in RPC */
343         if (minfo) {
344                 entry->se_minfo = NULL;
345                 ll_intent_release(&minfo->mi_it);
346                 iput(minfo->mi_dir);
347                 OBD_FREE_PTR(minfo);
348         }
349
350         if (req) {
351                 entry->se_req = NULL;
352                 ptlrpc_req_finished(req);
353         }
354
355         spin_lock(&lli->lli_sa_lock);
356         wakeup = __sa_make_ready(sai, entry, ret);
357         spin_unlock(&lli->lli_sa_lock);
358
359         if (wakeup)
360                 wake_up(&sai->sai_waitq);
361 }
362
363 /*
364  * Insert inode into the list of sai_agls.
365  */
366 static void ll_agl_add(struct ll_statahead_info *sai,
367                        struct inode *inode, int index)
368 {
369         struct ll_inode_info *child  = ll_i2info(inode);
370         struct ll_inode_info *parent = ll_i2info(sai->sai_inode);
371         int                   added  = 0;
372
373         spin_lock(&child->lli_agl_lock);
374         if (child->lli_agl_index == 0) {
375                 child->lli_agl_index = index;
376                 spin_unlock(&child->lli_agl_lock);
377
378                 LASSERT(list_empty(&child->lli_agl_list));
379
380                 igrab(inode);
381                 spin_lock(&parent->lli_agl_lock);
382                 if (agl_list_empty(sai))
383                         added = 1;
384                 list_add_tail(&child->lli_agl_list, &sai->sai_agls);
385                 spin_unlock(&parent->lli_agl_lock);
386         } else {
387                 spin_unlock(&child->lli_agl_lock);
388         }
389
390         if (added > 0)
391                 wake_up(&sai->sai_agl_thread.t_ctl_waitq);
392 }
393
394 static struct ll_statahead_info *ll_sai_alloc(void)
395 {
396         struct ll_statahead_info *sai;
397         int                       i;
398         ENTRY;
399
400         OBD_ALLOC_PTR(sai);
401         if (!sai)
402                 RETURN(NULL);
403
404         atomic_set(&sai->sai_refcount, 1);
405
406         spin_lock(&sai_generation_lock);
407         sai->sai_generation = ++sai_generation;
408         if (unlikely(sai_generation == 0))
409                 sai->sai_generation = ++sai_generation;
410         spin_unlock(&sai_generation_lock);
411
412         sai->sai_max = LL_SA_RPC_MIN;
413         sai->sai_index = 1;
414         init_waitqueue_head(&sai->sai_waitq);
415         init_waitqueue_head(&sai->sai_thread.t_ctl_waitq);
416         init_waitqueue_head(&sai->sai_agl_thread.t_ctl_waitq);
417
418         INIT_LIST_HEAD(&sai->sai_interim_entries);
419         INIT_LIST_HEAD(&sai->sai_entries);
420         INIT_LIST_HEAD(&sai->sai_agls);
421
422         for (i = 0; i < LL_SA_CACHE_SIZE; i++) {
423                 INIT_LIST_HEAD(&sai->sai_cache[i]);
424                 spin_lock_init(&sai->sai_cache_lock[i]);
425         }
426         atomic_set(&sai->sai_cache_count, 0);
427
428         RETURN(sai);
429 }
430
431 static inline struct ll_statahead_info *ll_sai_get(struct inode *dir)
432 {
433         struct ll_inode_info *lli = ll_i2info(dir);
434         struct ll_statahead_info *sai = NULL;
435
436         spin_lock(&lli->lli_sa_lock);
437         sai = lli->lli_sai;
438         if (sai != NULL)
439                 atomic_inc(&sai->sai_refcount);
440         spin_unlock(&lli->lli_sa_lock);
441
442         return sai;
443 }
444
445 static void ll_sai_put(struct ll_statahead_info *sai)
446 {
447         struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
448
449         if (atomic_dec_and_lock(&sai->sai_refcount, &lli->lli_sa_lock)) {
450                 struct sa_entry *entry, *next;
451                 struct ll_sb_info *sbi = ll_i2sbi(sai->sai_inode);
452
453                 lli->lli_sai = NULL;
454                 spin_unlock(&lli->lli_sa_lock);
455
456                 LASSERT(thread_is_stopped(&sai->sai_thread));
457                 LASSERT(thread_is_stopped(&sai->sai_agl_thread));
458                 LASSERT(sai->sai_sent == sai->sai_replied);
459                 LASSERT(!sa_has_callback(sai));
460
461                 list_for_each_entry_safe(entry, next, &sai->sai_entries,
462                                          se_list)
463                         sa_kill(sai, entry);
464
465                 LASSERT(atomic_read(&sai->sai_cache_count) == 0);
466                 LASSERT(agl_list_empty(sai));
467
468                 iput(sai->sai_inode);
469                 OBD_FREE_PTR(sai);
470                 atomic_dec(&sbi->ll_sa_running);
471         }
472 }
473
474 /* Do NOT forget to drop inode refcount when into sai_agls. */
475 static void ll_agl_trigger(struct inode *inode, struct ll_statahead_info *sai)
476 {
477         struct ll_inode_info *lli = ll_i2info(inode);
478         __u64 index = lli->lli_agl_index;
479         int rc;
480         ENTRY;
481
482         LASSERT(list_empty(&lli->lli_agl_list));
483
484         /* AGL maybe fall behind statahead with one entry */
485         if (is_omitted_entry(sai, index + 1)) {
486                 lli->lli_agl_index = 0;
487                 iput(inode);
488                 RETURN_EXIT;
489         }
490
491         /* Someone is in glimpse (sync or async), do nothing. */
492         rc = down_write_trylock(&lli->lli_glimpse_sem);
493         if (rc == 0) {
494                 lli->lli_agl_index = 0;
495                 iput(inode);
496                 RETURN_EXIT;
497         }
498
499         /*
500          * Someone triggered glimpse within 1 sec before.
501          * 1) The former glimpse succeeded with glimpse lock granted by OST, and
502          *    if the lock is still cached on client, AGL needs to do nothing. If
503          *    it is cancelled by other client, AGL maybe cannot obtaion new lock
504          *    for no glimpse callback triggered by AGL.
505          * 2) The former glimpse succeeded, but OST did not grant glimpse lock.
506          *    Under such case, it is quite possible that the OST will not grant
507          *    glimpse lock for AGL also.
508          * 3) The former glimpse failed, compared with other two cases, it is
509          *    relative rare. AGL can ignore such case, and it will not muchly
510          *    affect the performance.
511          */
512         if (lli->lli_glimpse_time != 0 &&
513             cfs_time_before(cfs_time_shift(-1), lli->lli_glimpse_time)) {
514                 up_write(&lli->lli_glimpse_sem);
515                 lli->lli_agl_index = 0;
516                 iput(inode);
517                 RETURN_EXIT;
518         }
519
520         CDEBUG(D_READA, "Handling (init) async glimpse: inode = "
521                DFID", idx = "LPU64"\n", PFID(&lli->lli_fid), index);
522
523         cl_agl(inode);
524         lli->lli_agl_index = 0;
525         lli->lli_glimpse_time = cfs_time_current();
526         up_write(&lli->lli_glimpse_sem);
527
528         CDEBUG(D_READA, "Handled (init) async glimpse: inode= "
529                DFID", idx = "LPU64", rc = %d\n",
530                PFID(&lli->lli_fid), index, rc);
531
532         iput(inode);
533
534         EXIT;
535 }
536
537 /* prepare inode for sa entry, add it into agl list, now sa_entry is ready
538  * to be used by scanner process. */
539 static void sa_instantiate(struct ll_statahead_info *sai,
540                                  struct sa_entry *entry)
541 {
542         struct inode *dir = sai->sai_inode;
543         struct inode *child;
544         struct md_enqueue_info *minfo;
545         struct lookup_intent *it;
546         struct ptlrpc_request *req;
547         struct mdt_body *body;
548         int rc = 0;
549         ENTRY;
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->mbo_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,
577                                         &body->mbo_fid1))) {
578                         entry->se_inode = NULL;
579                         iput(child);
580                         child = NULL;
581                 }
582         }
583
584         it->d.lustre.it_lock_handle = entry->se_handle;
585         rc = md_revalidate_lock(ll_i2mdexp(dir), it, ll_inode2fid(dir), NULL);
586         if (rc != 1)
587                 GOTO(out, rc = -EAGAIN);
588
589         rc = ll_prep_inode(&child, req, dir->i_sb, it);
590         if (rc)
591                 GOTO(out, rc);
592
593         CDEBUG(D_READA, "%s: setting %.*s"DFID" l_data to inode %p\n",
594                ll_get_fsname(child->i_sb, NULL, 0),
595                entry->se_qstr.len, entry->se_qstr.name,
596                PFID(ll_inode2fid(child)), child);
597         ll_set_lock_data(ll_i2sbi(dir)->ll_md_exp, child, it, NULL);
598
599         entry->se_inode = child;
600
601         if (agl_should_run(sai, child))
602                 ll_agl_add(sai, child, entry->se_index);
603
604         EXIT;
605
606 out:
607         /* sa_make_ready() will drop ldlm ibits lock refcount by calling
608          * ll_intent_drop_lock() in spite of failures. Do not worry about
609          * calling ll_intent_drop_lock() more than once. */
610         sa_make_ready(sai, entry, rc);
611 }
612
613 /* once there are async stat replies, instantiate sa_entry */
614 static void sa_handle_callback(struct ll_statahead_info *sai)
615 {
616         struct ll_inode_info *lli;
617
618         lli = ll_i2info(sai->sai_inode);
619
620         while (sa_has_callback(sai)) {
621                 struct sa_entry *entry;
622
623                 spin_lock(&lli->lli_sa_lock);
624                 if (unlikely(!sa_has_callback(sai))) {
625                         spin_unlock(&lli->lli_sa_lock);
626                         break;
627                 }
628                 entry = list_entry(sai->sai_interim_entries.next,
629                                    struct sa_entry, se_list);
630                 list_del_init(&entry->se_list);
631                 spin_unlock(&lli->lli_sa_lock);
632
633                 sa_instantiate(sai, entry);
634         }
635
636         spin_lock(&lli->lli_agl_lock);
637         while (!agl_list_empty(sai)) {
638                 struct ll_inode_info *clli;
639
640                 clli = agl_first_entry(sai);
641                 list_del_init(&clli->lli_agl_list);
642                 spin_unlock(&lli->lli_agl_lock);
643
644                 ll_agl_trigger(&clli->lli_vfs_inode, sai);
645
646                 spin_lock(&lli->lli_agl_lock);
647         }
648         spin_unlock(&lli->lli_agl_lock);
649 }
650
651 /* callback for async stat, because this is called in ptlrpcd context, we only
652  * put sa_entry in sai_cb_entries list, and let sa_handle_callback() to really
653  * prepare inode and instantiate sa_entry later. */
654 static int ll_statahead_interpret(struct ptlrpc_request *req,
655                                   struct md_enqueue_info *minfo, int rc)
656 {
657         struct lookup_intent *it = &minfo->mi_it;
658         struct inode *dir = minfo->mi_dir;
659         struct ll_inode_info *lli = ll_i2info(dir);
660         struct ll_statahead_info *sai = lli->lli_sai;
661         struct sa_entry *entry = (struct sa_entry *)minfo->mi_cbdata;
662         bool wakeup;
663         ENTRY;
664
665         if (it_disposition(it, DISP_LOOKUP_NEG))
666                 rc = -ENOENT;
667
668         /* because statahead thread will wait for all inflight RPC to finish,
669          * sai should be always valid, no need to refcount */
670         LASSERT(sai != NULL);
671         LASSERT(!thread_is_stopped(&sai->sai_thread));
672         LASSERT(entry != NULL);
673
674         CDEBUG(D_READA, "sa_entry %.*s rc %d\n",
675                entry->se_qstr.len, entry->se_qstr.name, rc);
676
677         spin_lock(&lli->lli_sa_lock);
678         if (rc != 0) {
679                 wakeup = __sa_make_ready(sai, entry, rc);
680         } else {
681                 entry->se_minfo = minfo;
682                 entry->se_req = ptlrpc_request_addref(req);
683                 /* Release the async ibits lock ASAP to avoid deadlock
684                  * when statahead thread tries to enqueue lock on parent
685                  * for readpage and other tries to enqueue lock on child
686                  * with parent's lock held, for example: unlink. */
687                 entry->se_handle = it->d.lustre.it_lock_handle;
688                 ll_intent_drop_lock(it);
689                 wakeup = !sa_has_callback(sai);
690                 list_add_tail(&entry->se_list, &sai->sai_interim_entries);
691         }
692         sai->sai_replied++;
693         if (wakeup)
694                 wake_up(&sai->sai_thread.t_ctl_waitq);
695         spin_unlock(&lli->lli_sa_lock);
696
697         if (rc != 0) {
698                 ll_intent_release(it);
699                 iput(dir);
700                 OBD_FREE_PTR(minfo);
701         }
702         RETURN(rc);
703 }
704
705 static void sa_fini_data(struct md_enqueue_info *minfo,
706                          struct ldlm_enqueue_info *einfo)
707 {
708         LASSERT(minfo && einfo);
709         iput(minfo->mi_dir);
710         capa_put(minfo->mi_data.op_capa1);
711         capa_put(minfo->mi_data.op_capa2);
712         OBD_FREE_PTR(minfo);
713         OBD_FREE_PTR(einfo);
714 }
715
716 /**
717  * There is race condition between "capa_put" and "ll_statahead_interpret" for
718  * accessing "op_data.op_capa[1,2]" as following:
719  * "capa_put" releases "op_data.op_capa[1,2]"'s reference count after calling
720  * "md_intent_getattr_async". But "ll_statahead_interpret" maybe run first, and
721  * fill "op_data.op_capa[1,2]" as POISON, then cause "capa_put" access invalid
722  * "ocapa". So here reserve "op_data.op_capa[1,2]" in "pcapa" before calling
723  * "md_intent_getattr_async".
724  */
725 static int sa_prep_data(struct inode *dir, struct inode *child,
726                         struct sa_entry *entry, struct md_enqueue_info **pmi,
727                         struct ldlm_enqueue_info **pei,
728                         struct obd_capa **pcapa)
729 {
730         struct qstr              *qstr = &entry->se_qstr;
731         struct md_enqueue_info   *minfo;
732         struct ldlm_enqueue_info *einfo;
733         struct md_op_data        *op_data;
734
735         OBD_ALLOC_PTR(einfo);
736         if (einfo == NULL)
737                 return -ENOMEM;
738
739         OBD_ALLOC_PTR(minfo);
740         if (minfo == NULL) {
741                 OBD_FREE_PTR(einfo);
742                 return -ENOMEM;
743         }
744
745         op_data = ll_prep_md_op_data(&minfo->mi_data, dir, child, qstr->name,
746                                      qstr->len, 0, LUSTRE_OPC_ANY, NULL);
747         if (IS_ERR(op_data)) {
748                 OBD_FREE_PTR(einfo);
749                 OBD_FREE_PTR(minfo);
750                 return PTR_ERR(op_data);
751         }
752
753         minfo->mi_it.it_op = IT_GETATTR;
754         minfo->mi_dir = igrab(dir);
755         minfo->mi_cb = ll_statahead_interpret;
756         minfo->mi_cbdata = entry;
757
758         einfo->ei_type   = LDLM_IBITS;
759         einfo->ei_mode   = it_to_lock_mode(&minfo->mi_it);
760         einfo->ei_cb_bl  = ll_md_blocking_ast;
761         einfo->ei_cb_cp  = ldlm_completion_ast;
762         einfo->ei_cb_gl  = NULL;
763         einfo->ei_cbdata = NULL;
764
765         *pmi = minfo;
766         *pei = einfo;
767         pcapa[0] = op_data->op_capa1;
768         pcapa[1] = op_data->op_capa2;
769
770         return 0;
771 }
772
773 static int sa_lookup(struct inode *dir, struct sa_entry *entry)
774 {
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         rc = sa_prep_data(dir, NULL, entry, &minfo, &einfo, capas);
782         if (rc)
783                 RETURN(rc);
784
785         rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
786         if (!rc) {
787                 capa_put(capas[0]);
788                 capa_put(capas[1]);
789         } else {
790                 sa_fini_data(minfo, einfo);
791         }
792
793         RETURN(rc);
794 }
795
796 /**
797  * similar to ll_revalidate_it().
798  * \retval      1 -- dentry valid
799  * \retval      0 -- will send stat-ahead request
800  * \retval others -- prepare stat-ahead request failed
801  */
802 static int sa_revalidate(struct inode *dir, struct sa_entry *entry,
803                          struct dentry *dentry)
804 {
805         struct inode *inode = dentry->d_inode;
806         struct lookup_intent it = { .it_op = IT_GETATTR,
807                                     .d.lustre.it_lock_handle = 0 };
808         struct md_enqueue_info *minfo;
809         struct ldlm_enqueue_info *einfo;
810         struct obd_capa *capas[2];
811         int rc;
812         ENTRY;
813
814         if (unlikely(inode == NULL))
815                 RETURN(1);
816
817         if (d_mountpoint(dentry))
818                 RETURN(1);
819
820         entry->se_inode = igrab(inode);
821         rc = md_revalidate_lock(ll_i2mdexp(dir), &it, ll_inode2fid(inode),
822                                 NULL);
823         if (rc == 1) {
824                 entry->se_handle = it.d.lustre.it_lock_handle;
825                 ll_intent_release(&it);
826                 RETURN(1);
827         }
828
829         rc = sa_prep_data(dir, inode, entry, &minfo, &einfo, capas);
830         if (rc) {
831                 entry->se_inode = NULL;
832                 iput(inode);
833                 RETURN(rc);
834         }
835
836         rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
837         if (!rc) {
838                 capa_put(capas[0]);
839                 capa_put(capas[1]);
840         } else {
841                 entry->se_inode = NULL;
842                 iput(inode);
843                 sa_fini_data(minfo, einfo);
844         }
845
846         RETURN(rc);
847 }
848
849 static void sa_statahead(struct dentry *parent, const char *name, int len)
850 {
851         struct inode *dir = parent->d_inode;
852         struct ll_inode_info *lli = ll_i2info(dir);
853         struct ll_statahead_info *sai = lli->lli_sai;
854         struct dentry *dentry = NULL;
855         struct sa_entry *entry;
856         int rc;
857         ENTRY;
858
859         entry = sa_alloc(sai, sai->sai_index, name, len);
860         if (IS_ERR(entry))
861                 RETURN_EXIT;
862
863         dentry = d_lookup(parent, &entry->se_qstr);
864         if (!dentry) {
865                 rc = sa_lookup(dir, entry);
866         } else {
867                 rc = sa_revalidate(dir, entry, dentry);
868                 if (rc == 1 && agl_should_run(sai, dentry->d_inode))
869                         ll_agl_add(sai, dentry->d_inode, entry->se_index);
870         }
871
872         if (dentry != NULL)
873                 dput(dentry);
874
875         if (rc != 0)
876                 sa_make_ready(sai, entry, rc);
877         else
878                 sai->sai_sent++;
879
880         sai->sai_index++;
881
882         EXIT;
883 }
884
885 static int ll_agl_thread(void *arg)
886 {
887         struct dentry *parent = (struct dentry *)arg;
888         struct inode *dir = parent->d_inode;
889         struct ll_inode_info *plli = ll_i2info(dir);
890         struct ll_inode_info *clli;
891         struct ll_sb_info *sbi = ll_i2sbi(dir);
892         struct ll_statahead_info *sai;
893         struct ptlrpc_thread *thread;
894         struct l_wait_info lwi = { 0 };
895         ENTRY;
896
897
898         sai = ll_sai_get(dir);
899         thread = &sai->sai_agl_thread;
900         thread->t_pid = current_pid();
901         CDEBUG(D_READA, "agl thread started: sai %p, parent %.*s\n",
902                sai, parent->d_name.len, parent->d_name.name);
903
904         atomic_inc(&sbi->ll_agl_total);
905         spin_lock(&plli->lli_agl_lock);
906         sai->sai_agl_valid = 1;
907         if (thread_is_init(thread))
908                 /* If someone else has changed the thread state
909                  * (e.g. already changed to SVC_STOPPING), we can't just
910                  * blindly overwrite that setting. */
911                 thread_set_flags(thread, SVC_RUNNING);
912         spin_unlock(&plli->lli_agl_lock);
913         wake_up(&thread->t_ctl_waitq);
914
915         while (1) {
916                 l_wait_event(thread->t_ctl_waitq,
917                              !agl_list_empty(sai) ||
918                              !thread_is_running(thread),
919                              &lwi);
920
921                 if (!thread_is_running(thread))
922                         break;
923
924                 spin_lock(&plli->lli_agl_lock);
925                 /* The statahead thread maybe help to process AGL entries,
926                  * so check whether list empty again. */
927                 if (!agl_list_empty(sai)) {
928                         clli = agl_first_entry(sai);
929                         list_del_init(&clli->lli_agl_list);
930                         spin_unlock(&plli->lli_agl_lock);
931                         ll_agl_trigger(&clli->lli_vfs_inode, sai);
932                 } else {
933                         spin_unlock(&plli->lli_agl_lock);
934                 }
935         }
936
937         spin_lock(&plli->lli_agl_lock);
938         sai->sai_agl_valid = 0;
939         while (!agl_list_empty(sai)) {
940                 clli = agl_first_entry(sai);
941                 list_del_init(&clli->lli_agl_list);
942                 spin_unlock(&plli->lli_agl_lock);
943                 clli->lli_agl_index = 0;
944                 iput(&clli->lli_vfs_inode);
945                 spin_lock(&plli->lli_agl_lock);
946         }
947         thread_set_flags(thread, SVC_STOPPED);
948         spin_unlock(&plli->lli_agl_lock);
949         wake_up(&thread->t_ctl_waitq);
950         ll_sai_put(sai);
951         CDEBUG(D_READA, "agl thread stopped: sai %p, parent %.*s\n",
952                sai, parent->d_name.len, parent->d_name.name);
953         RETURN(0);
954 }
955
956 static void ll_start_agl(struct dentry *parent, struct ll_statahead_info *sai)
957 {
958         struct ptlrpc_thread *thread = &sai->sai_agl_thread;
959         struct l_wait_info    lwi    = { 0 };
960         struct ll_inode_info  *plli;
961         struct task_struct            *task;
962         ENTRY;
963
964         CDEBUG(D_READA, "start agl thread: sai %p, parent %.*s\n",
965                sai, parent->d_name.len, parent->d_name.name);
966
967         plli = ll_i2info(parent->d_inode);
968         task = kthread_run(ll_agl_thread, parent,
969                                "ll_agl_%u", plli->lli_opendir_pid);
970         if (IS_ERR(task)) {
971                 CERROR("can't start ll_agl thread, rc: %ld\n", PTR_ERR(task));
972                 thread_set_flags(thread, SVC_STOPPED);
973                 RETURN_EXIT;
974         }
975
976         l_wait_event(thread->t_ctl_waitq,
977                      thread_is_running(thread) || thread_is_stopped(thread),
978                      &lwi);
979         EXIT;
980 }
981
982 static int ll_statahead_thread(void *arg)
983 {
984         struct dentry *parent = (struct dentry *)arg;
985         struct inode *dir = parent->d_inode;
986         struct ll_inode_info *lli = ll_i2info(dir);
987         struct ll_sb_info *sbi = ll_i2sbi(dir);
988         struct ll_statahead_info *sai;
989         struct ptlrpc_thread *thread;
990         struct ptlrpc_thread *agl_thread;
991         int first = 0;
992         struct md_op_data *op_data;
993         struct ll_dir_chain chain;
994         struct l_wait_info lwi = { 0 };
995         struct page *page = NULL;
996         __u64 pos = 0;
997         int rc = 0;
998         ENTRY;
999
1000         sai = ll_sai_get(dir);
1001         thread = &sai->sai_thread;
1002         agl_thread = &sai->sai_agl_thread;
1003         thread->t_pid = current_pid();
1004         CDEBUG(D_READA, "statahead thread starting: sai %p, parent %.*s\n",
1005                sai, parent->d_name.len, parent->d_name.name);
1006
1007         op_data = ll_prep_md_op_data(NULL, dir, dir, NULL, 0, 0,
1008                                      LUSTRE_OPC_ANY, dir);
1009         if (IS_ERR(op_data))
1010                 GOTO(out, rc = PTR_ERR(op_data));
1011
1012         op_data->op_max_pages = ll_i2sbi(dir)->ll_md_brw_pages;
1013
1014         if (sbi->ll_flags & LL_SBI_AGL_ENABLED)
1015                 ll_start_agl(parent, sai);
1016
1017         atomic_inc(&sbi->ll_sa_total);
1018         spin_lock(&lli->lli_sa_lock);
1019         if (thread_is_init(thread))
1020                 /* If someone else has changed the thread state
1021                  * (e.g. already changed to SVC_STOPPING), we can't just
1022                  * blindly overwrite that setting. */
1023                 thread_set_flags(thread, SVC_RUNNING);
1024         spin_unlock(&lli->lli_sa_lock);
1025         wake_up(&thread->t_ctl_waitq);
1026
1027         ll_dir_chain_init(&chain);
1028         while (pos != MDS_DIR_END_OFF && thread_is_running(thread)) {
1029                 struct lu_dirpage *dp;
1030                 struct lu_dirent  *ent;
1031
1032                 sai->sai_in_readpage = 1;
1033                 page = ll_get_dir_page(dir, op_data, pos, &chain);
1034                 sai->sai_in_readpage = 0;
1035                 if (IS_ERR(page)) {
1036                         rc = PTR_ERR(page);
1037                         CDEBUG(D_READA, "error reading dir "DFID" at "LPU64
1038                                "/"LPU64" opendir_pid = %u: rc = %d\n",
1039                                PFID(ll_inode2fid(dir)), pos, sai->sai_index,
1040                                lli->lli_opendir_pid, rc);
1041                         break;
1042                 }
1043
1044                 dp = page_address(page);
1045                 for (ent = lu_dirent_start(dp);
1046                      ent != NULL && thread_is_running(thread) &&
1047                      !sa_low_hit(sai);
1048                      ent = lu_dirent_next(ent)) {
1049                         __u64 hash;
1050                         int namelen;
1051                         char *name;
1052
1053                         hash = le64_to_cpu(ent->lde_hash);
1054                         if (unlikely(hash < pos))
1055                                 /*
1056                                  * Skip until we find target hash value.
1057                                  */
1058                                 continue;
1059
1060                         namelen = le16_to_cpu(ent->lde_namelen);
1061                         if (unlikely(namelen == 0))
1062                                 /*
1063                                  * Skip dummy record.
1064                                  */
1065                                 continue;
1066
1067                         name = ent->lde_name;
1068                         if (name[0] == '.') {
1069                                 if (namelen == 1) {
1070                                         /*
1071                                          * skip "."
1072                                          */
1073                                         continue;
1074                                 } else if (name[1] == '.' && namelen == 2) {
1075                                         /*
1076                                          * skip ".."
1077                                          */
1078                                         continue;
1079                                 } else if (!sai->sai_ls_all) {
1080                                         /*
1081                                          * skip hidden files.
1082                                          */
1083                                         sai->sai_skip_hidden++;
1084                                         continue;
1085                                 }
1086                         }
1087
1088                         /*
1089                          * don't stat-ahead first entry.
1090                          */
1091                         if (unlikely(++first == 1))
1092                                 continue;
1093
1094                         /* wait for spare statahead window */
1095                         do {
1096                                 l_wait_event(thread->t_ctl_waitq,
1097                                              !sa_sent_full(sai) ||
1098                                              sa_has_callback(sai) ||
1099                                              !agl_list_empty(sai) ||
1100                                              !thread_is_running(thread),
1101                                              &lwi);
1102
1103                                 sa_handle_callback(sai);
1104                         } while (sa_sent_full(sai) &&
1105                                  thread_is_running(thread));
1106
1107                         sa_statahead(parent, name, namelen);
1108                 }
1109
1110                 pos = le64_to_cpu(dp->ldp_hash_end);
1111                 ll_release_page(dir, page,
1112                                 le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1113
1114                 if (sa_low_hit(sai)) {
1115                         rc = -EFAULT;
1116                         atomic_inc(&sbi->ll_sa_wrong);
1117                         CDEBUG(D_READA, "Statahead for dir "DFID" hit "
1118                                "ratio too low: hit/miss "LPU64"/"LPU64
1119                                ", sent/replied "LPU64"/"LPU64", stopping "
1120                                "statahead thread: pid %d\n",
1121                                PFID(&lli->lli_fid), sai->sai_hit,
1122                                sai->sai_miss, sai->sai_sent,
1123                                sai->sai_replied, current_pid());
1124                         break;
1125                 }
1126         }
1127         ll_dir_chain_fini(&chain);
1128         ll_finish_md_op_data(op_data);
1129
1130         if (rc < 0) {
1131                 spin_lock(&lli->lli_sa_lock);
1132                 thread_set_flags(thread, SVC_STOPPING);
1133                 lli->lli_sa_enabled = 0;
1134                 spin_unlock(&lli->lli_sa_lock);
1135         }
1136
1137         /* statahead is finished, but statahead entries need to be cached, wait
1138          * for file release to stop me. */
1139         while (thread_is_running(thread)) {
1140                 l_wait_event(thread->t_ctl_waitq,
1141                              sa_has_callback(sai) ||
1142                              !agl_list_empty(sai) ||
1143                              !thread_is_running(thread),
1144                              &lwi);
1145
1146                 sa_handle_callback(sai);
1147         }
1148
1149         EXIT;
1150 out:
1151         if (sai->sai_agl_valid) {
1152                 spin_lock(&lli->lli_agl_lock);
1153                 thread_set_flags(agl_thread, SVC_STOPPING);
1154                 spin_unlock(&lli->lli_agl_lock);
1155                 wake_up(&agl_thread->t_ctl_waitq);
1156
1157                 CDEBUG(D_READA, "stop agl thread: sai %p pid %u\n",
1158                        sai, (unsigned int)agl_thread->t_pid);
1159                 l_wait_event(agl_thread->t_ctl_waitq,
1160                              thread_is_stopped(agl_thread),
1161                              &lwi);
1162         } else {
1163                 /* Set agl_thread flags anyway. */
1164                 thread_set_flags(agl_thread, SVC_STOPPED);
1165         }
1166
1167         /* wait for inflight statahead RPCs to finish, and then we can free sai
1168          * safely because statahead RPC will access sai data */
1169         while (sai->sai_sent != sai->sai_replied) {
1170                 /* in case we're not woken up, timeout wait */
1171                 lwi = LWI_TIMEOUT(HZ >> 3, NULL, NULL);
1172                 l_wait_event(thread->t_ctl_waitq,
1173                         sai->sai_sent == sai->sai_replied, &lwi);
1174         }
1175
1176         /* release resources held by statahead RPCs */
1177         sa_handle_callback(sai);
1178
1179         spin_lock(&lli->lli_sa_lock);
1180         thread_set_flags(thread, SVC_STOPPED);
1181         spin_unlock(&lli->lli_sa_lock);
1182
1183         wake_up(&sai->sai_waitq);
1184         wake_up(&thread->t_ctl_waitq);
1185         ll_sai_put(sai);
1186         CDEBUG(D_READA, "statahead thread stopped: sai %p, parent %.*s\n",
1187                sai, parent->d_name.len, parent->d_name.name);
1188         dput(parent);
1189         return rc;
1190 }
1191
1192 /* authorize opened dir handle @key to statahead later */
1193 void ll_authorize_statahead(struct inode *dir, void *key)
1194 {
1195         struct ll_inode_info *lli = ll_i2info(dir);
1196
1197         spin_lock(&lli->lli_sa_lock);
1198         if (lli->lli_opendir_key == NULL && lli->lli_sai == NULL) {
1199                 /*
1200                  * if lli_sai is not NULL, it means previous statahead is not
1201                  * finished yet, we'd better not start a new statahead for now.
1202                  */
1203                 LASSERT(lli->lli_opendir_pid == 0);
1204                 lli->lli_opendir_key = key;
1205                 lli->lli_opendir_pid = current_pid();
1206                 lli->lli_sa_enabled = 1;
1207         }
1208         spin_unlock(&lli->lli_sa_lock);
1209 }
1210
1211 /* deauthorize opened dir handle @key to statahead, but statahead thread may
1212  * still be running, notify it to quit. */
1213 void ll_deauthorize_statahead(struct inode *dir, void *key)
1214 {
1215         struct ll_inode_info *lli = ll_i2info(dir);
1216         struct ll_statahead_info *sai;
1217
1218         LASSERT(lli->lli_opendir_key == key);
1219         LASSERT(lli->lli_opendir_pid != 0);
1220
1221         CDEBUG(D_READA, "deauthorize statahead for "DFID"\n",
1222                 PFID(&lli->lli_fid));
1223
1224         spin_lock(&lli->lli_sa_lock);
1225         lli->lli_opendir_key = NULL;
1226         lli->lli_opendir_pid = 0;
1227         lli->lli_sa_enabled = 0;
1228         sai = lli->lli_sai;
1229         if (sai != NULL && thread_is_running(&sai->sai_thread)) {
1230                 /*
1231                  * statahead thread may not quit yet because it needs to cache
1232                  * entries, now it's time to tell it to quit.
1233                  */
1234                 thread_set_flags(&sai->sai_thread, SVC_STOPPING);
1235                 wake_up(&sai->sai_thread.t_ctl_waitq);
1236         }
1237         spin_unlock(&lli->lli_sa_lock);
1238 }
1239
1240 enum {
1241         /**
1242          * not first dirent, or is "."
1243          */
1244         LS_NONE_FIRST_DE = 0,
1245         /**
1246          * the first non-hidden dirent
1247          */
1248         LS_FIRST_DE,
1249         /**
1250          * the first hidden dirent, that is "."
1251          */
1252         LS_FIRST_DOT_DE
1253 };
1254
1255 static int is_first_dirent(struct inode *dir, struct dentry *dentry)
1256 {
1257         struct ll_dir_chain   chain;
1258         struct qstr          *target = &dentry->d_name;
1259         struct md_op_data    *op_data;
1260         int                   dot_de;
1261         struct page          *page = NULL;
1262         int                   rc     = LS_NONE_FIRST_DE;
1263         __u64                 pos = 0;
1264         ENTRY;
1265
1266         op_data = ll_prep_md_op_data(NULL, dir, dir, NULL, 0, 0,
1267                                      LUSTRE_OPC_ANY, dir);
1268         if (IS_ERR(op_data))
1269                 RETURN(PTR_ERR(op_data));
1270         /**
1271          *FIXME choose the start offset of the readdir
1272          */
1273         op_data->op_stripe_offset = 0;
1274         op_data->op_max_pages = ll_i2sbi(dir)->ll_md_brw_pages;
1275
1276         ll_dir_chain_init(&chain);
1277         page = ll_get_dir_page(dir, op_data, 0, &chain);
1278
1279         while (1) {
1280                 struct lu_dirpage *dp;
1281                 struct lu_dirent  *ent;
1282
1283                 if (IS_ERR(page)) {
1284                         struct ll_inode_info *lli = ll_i2info(dir);
1285
1286                         rc = PTR_ERR(page);
1287                         CERROR("%s: reading dir "DFID" at "LPU64
1288                                "opendir_pid = %u : rc = %d\n",
1289                                ll_get_fsname(dir->i_sb, NULL, 0),
1290                                PFID(ll_inode2fid(dir)), pos,
1291                                lli->lli_opendir_pid, rc);
1292                         break;
1293                 }
1294
1295                 dp = page_address(page);
1296                 for (ent = lu_dirent_start(dp); ent != NULL;
1297                      ent = lu_dirent_next(ent)) {
1298                         __u64 hash;
1299                         int namelen;
1300                         char *name;
1301
1302                         hash = le64_to_cpu(ent->lde_hash);
1303                         /* The ll_get_dir_page() can return any page containing
1304                          * the given hash which may be not the start hash. */
1305                         if (unlikely(hash < pos))
1306                                 continue;
1307
1308                         namelen = le16_to_cpu(ent->lde_namelen);
1309                         if (unlikely(namelen == 0))
1310                                 /*
1311                                  * skip dummy record.
1312                                  */
1313                                 continue;
1314
1315                         name = ent->lde_name;
1316                         if (name[0] == '.') {
1317                                 if (namelen == 1)
1318                                         /*
1319                                          * skip "."
1320                                          */
1321                                         continue;
1322                                 else if (name[1] == '.' && namelen == 2)
1323                                         /*
1324                                          * skip ".."
1325                                          */
1326                                         continue;
1327                                 else
1328                                         dot_de = 1;
1329                         } else {
1330                                 dot_de = 0;
1331                         }
1332
1333                         if (dot_de && target->name[0] != '.') {
1334                                 CDEBUG(D_READA, "%.*s skip hidden file %.*s\n",
1335                                        target->len, target->name,
1336                                        namelen, name);
1337                                 continue;
1338                         }
1339
1340                         if (target->len != namelen ||
1341                             memcmp(target->name, name, namelen) != 0)
1342                                 rc = LS_NONE_FIRST_DE;
1343                         else if (!dot_de)
1344                                 rc = LS_FIRST_DE;
1345                         else
1346                                 rc = LS_FIRST_DOT_DE;
1347
1348                         ll_release_page(dir, page, false);
1349                         GOTO(out, rc);
1350                 }
1351                 pos = le64_to_cpu(dp->ldp_hash_end);
1352                 if (pos == MDS_DIR_END_OFF) {
1353                         /*
1354                          * End of directory reached.
1355                          */
1356                         ll_release_page(dir, page, false);
1357                         GOTO(out, rc);
1358                 } else {
1359                         /*
1360                          * chain is exhausted
1361                          * Normal case: continue to the next page.
1362                          */
1363                         ll_release_page(dir, page, le32_to_cpu(dp->ldp_flags) &
1364                                               LDF_COLLIDE);
1365                         page = ll_get_dir_page(dir, op_data, pos, &chain);
1366                 }
1367         }
1368         EXIT;
1369 out:
1370         ll_dir_chain_fini(&chain);
1371         ll_finish_md_op_data(op_data);
1372         return rc;
1373 }
1374
1375 static int revalidate_statahead_dentry(struct inode *dir,
1376                                         struct ll_statahead_info *sai,
1377                                         struct dentry **dentryp,
1378                                         int only_unplug)
1379 {
1380         struct sa_entry *entry = NULL;
1381         struct l_wait_info lwi = { 0 };
1382         int rc = 0;
1383         ENTRY;
1384
1385         if ((*dentryp)->d_name.name[0] == '.') {
1386                 if (sai->sai_ls_all ||
1387                     sai->sai_miss_hidden >= sai->sai_skip_hidden) {
1388                         /*
1389                          * Hidden dentry is the first one, or statahead
1390                          * thread does not skip so many hidden dentries
1391                          * before "sai_ls_all" enabled as below.
1392                          */
1393                 } else {
1394                         if (!sai->sai_ls_all)
1395                                 /*
1396                                  * It maybe because hidden dentry is not
1397                                  * the first one, "sai_ls_all" was not
1398                                  * set, then "ls -al" missed. Enable
1399                                  * "sai_ls_all" for such case.
1400                                  */
1401                                 sai->sai_ls_all = 1;
1402
1403                         /*
1404                          * Such "getattr" has been skipped before
1405                          * "sai_ls_all" enabled as above.
1406                          */
1407                         sai->sai_miss_hidden++;
1408                         RETURN(-EAGAIN);
1409                 }
1410         }
1411
1412         entry = sa_get(sai, &(*dentryp)->d_name);
1413         if (entry == NULL || only_unplug) {
1414                 sa_put(sai, entry);
1415                 RETURN(entry ? 1 : -EAGAIN);
1416         }
1417
1418         /* if statahead is busy in readdir, help it do post-work */
1419         if (!sa_ready(entry) && sai->sai_in_readpage)
1420                 sa_handle_callback(sai);
1421
1422         if (!sa_ready(entry)) {
1423                 sai->sai_index_wait = entry->se_index;
1424                 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(30), NULL,
1425                                         LWI_ON_SIGNAL_NOOP, NULL);
1426                 rc = l_wait_event(sai->sai_waitq,
1427                                 sa_ready(entry) ||
1428                                 thread_is_stopped(&sai->sai_thread),
1429                                 &lwi);
1430                 if (rc < 0) {
1431                         sa_put(sai, entry);
1432                         RETURN(-EAGAIN);
1433                 }
1434         }
1435
1436         if (entry->se_state == SA_ENTRY_SUCC && entry->se_inode != NULL) {
1437                 struct inode *inode = entry->se_inode;
1438                 struct lookup_intent it = { .it_op = IT_GETATTR,
1439                                             .d.lustre.it_lock_handle =
1440                                                 entry->se_handle };
1441                 __u64 bits;
1442
1443                 rc = md_revalidate_lock(ll_i2mdexp(dir), &it,
1444                                         ll_inode2fid(inode), &bits);
1445                 if (rc == 1) {
1446                         if ((*dentryp)->d_inode == NULL) {
1447                                 struct dentry *alias;
1448
1449                                 alias = ll_splice_alias(inode, *dentryp);
1450                                 if (IS_ERR(alias)) {
1451                                         sa_put(sai, entry);
1452                                         RETURN(PTR_ERR(alias));
1453                                 }
1454                                 *dentryp = alias;
1455                         } else if ((*dentryp)->d_inode != inode) {
1456                                 /* revalidate, but inode is recreated */
1457                                 CDEBUG(D_READA,
1458                                         "%s: stale dentry %.*s inode "
1459                                         DFID", statahead inode "DFID
1460                                         "\n",
1461                                         ll_get_fsname((*dentryp)->d_inode->i_sb,
1462                                                       NULL, 0),
1463                                         (*dentryp)->d_name.len,
1464                                         (*dentryp)->d_name.name,
1465                                         PFID(ll_inode2fid((*dentryp)->d_inode)),
1466                                         PFID(ll_inode2fid(inode)));
1467                                 sa_put(sai, entry);
1468                                 RETURN(-ESTALE);
1469                         } else {
1470                                 iput(inode);
1471                         }
1472                         entry->se_inode = NULL;
1473
1474                         if ((bits & MDS_INODELOCK_LOOKUP) &&
1475                             d_lustre_invalid(*dentryp))
1476                                 d_lustre_revalidate(*dentryp);
1477                         ll_intent_release(&it);
1478                 }
1479         }
1480
1481         sa_put(sai, entry);
1482         RETURN(rc);
1483 }
1484
1485 static int start_statahead_thread(struct inode *dir, struct dentry *dentry)
1486 {
1487         struct ll_inode_info *lli = ll_i2info(dir);
1488         struct ll_statahead_info *sai = NULL;
1489         struct dentry *parent;
1490         struct ptlrpc_thread *thread;
1491         struct l_wait_info lwi = { 0 };
1492         struct task_struct *task;
1493         int rc;
1494         ENTRY;
1495
1496         /* I am the "lli_opendir_pid" owner, only me can set "lli_sai". */
1497         rc = is_first_dirent(dir, dentry);
1498         if (rc == LS_NONE_FIRST_DE)
1499                 /* It is not "ls -{a}l" operation, no need statahead for it. */
1500                 GOTO(out, rc = -EAGAIN);
1501
1502         sai = ll_sai_alloc();
1503         if (sai == NULL)
1504                 GOTO(out, rc = -ENOMEM);
1505
1506         sai->sai_ls_all = (rc == LS_FIRST_DOT_DE);
1507         sai->sai_inode = igrab(dir);
1508         if (unlikely(sai->sai_inode == NULL)) {
1509                 CWARN("Do not start stat ahead on dying inode "DFID"\n",
1510                         PFID(&lli->lli_fid));
1511                 GOTO(out, rc = -ESTALE);
1512         }
1513
1514         /* get parent reference count here, and put it in ll_statahead_thread */
1515         parent = dget(dentry->d_parent);
1516         if (unlikely(sai->sai_inode != parent->d_inode)) {
1517                 struct ll_inode_info *nlli = ll_i2info(parent->d_inode);
1518
1519                 CWARN("Race condition, someone changed %.*s just now: "
1520                         "old parent "DFID", new parent "DFID"\n",
1521                         dentry->d_name.len, dentry->d_name.name,
1522                         PFID(&lli->lli_fid), PFID(&nlli->lli_fid));
1523                 dput(parent);
1524                 iput(sai->sai_inode);
1525                 GOTO(out, rc = -EAGAIN);
1526         }
1527
1528         CDEBUG(D_READA, "start statahead thread: sai %p, parent %.*s\n",
1529                sai, parent->d_name.len, parent->d_name.name);
1530
1531         lli->lli_sai = sai;
1532
1533         task = kthread_run(ll_statahead_thread, parent, "ll_sa_%u",
1534                            lli->lli_opendir_pid);
1535         thread = &sai->sai_thread;
1536         if (IS_ERR(task)) {
1537                 rc = PTR_ERR(task);
1538                 CERROR("cannot start ll_sa thread: rc = %d\n", rc);
1539                 dput(parent);
1540                 lli->lli_opendir_key = NULL;
1541                 thread_set_flags(thread, SVC_STOPPED);
1542                 thread_set_flags(&sai->sai_agl_thread, SVC_STOPPED);
1543                 ll_sai_put(sai);
1544                 LASSERT(lli->lli_sai == NULL);
1545                 RETURN(-EAGAIN);
1546         }
1547
1548         l_wait_event(thread->t_ctl_waitq,
1549                      thread_is_running(thread) || thread_is_stopped(thread),
1550                      &lwi);
1551         atomic_inc(&ll_i2sbi(parent->d_inode)->ll_sa_running);
1552         ll_sai_put(sai);
1553
1554         /*
1555          * We don't stat-ahead for the first dirent since we are already in
1556          * lookup.
1557          */
1558         RETURN(-EAGAIN);
1559
1560 out:
1561         if (sai != NULL)
1562                 OBD_FREE_PTR(sai);
1563         spin_lock(&lli->lli_sa_lock);
1564         lli->lli_opendir_key = NULL;
1565         lli->lli_opendir_pid = 0;
1566         lli->lli_sa_enabled = 0;
1567         spin_unlock(&lli->lli_sa_lock);
1568
1569         RETURN(rc);
1570 }
1571
1572 /**
1573  * Start statahead thread if this is the first dir entry.
1574  * Otherwise if a thread is started already, wait it until it is ahead of me.
1575  * \retval 1       -- find entry with lock in cache, the caller needs to do
1576  *                    nothing.
1577  * \retval 0       -- find entry in cache, but without lock, the caller needs
1578  *                    refresh from MDS.
1579  * \retval others  -- the caller need to process as non-statahead.
1580  */
1581 int do_statahead_enter(struct inode *dir, struct dentry **dentryp,
1582                        int only_unplug)
1583 {
1584         struct ll_statahead_info *sai;
1585
1586         sai = ll_sai_get(dir);
1587         if (sai != NULL) {
1588                 int rc;
1589
1590                 rc = revalidate_statahead_dentry(dir, sai, dentryp,
1591                                                  only_unplug);
1592                 CDEBUG(D_READA, "revalidate statahead %.*s: %d.\n",
1593                         (*dentryp)->d_name.len, (*dentryp)->d_name.name, rc);
1594                 ll_sai_put(sai);
1595                 return rc;
1596         }
1597
1598         return start_statahead_thread(dir, *dentryp);
1599 }