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