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