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