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