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