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