Whamcloud - gitweb
LU-10165 llite: disable statahead if starting statahead fail
[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, 2016, 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 /*
334  * release resources used in async stat RPC, update entry state and wakeup if
335  * scanner process it waiting on this entry.
336  */
337 static void
338 sa_make_ready(struct ll_statahead_info *sai, struct sa_entry *entry, int ret)
339 {
340         struct ll_inode_info *lli = ll_i2info(sai->sai_dentry->d_inode);
341         struct md_enqueue_info *minfo = entry->se_minfo;
342         struct ptlrpc_request *req = entry->se_req;
343         bool wakeup;
344
345         /* release resources used in RPC */
346         if (minfo) {
347                 entry->se_minfo = NULL;
348                 ll_intent_release(&minfo->mi_it);
349                 iput(minfo->mi_dir);
350                 OBD_FREE_PTR(minfo);
351         }
352
353         if (req) {
354                 entry->se_req = NULL;
355                 ptlrpc_req_finished(req);
356         }
357
358         spin_lock(&lli->lli_sa_lock);
359         wakeup = __sa_make_ready(sai, entry, ret);
360         spin_unlock(&lli->lli_sa_lock);
361
362         if (wakeup)
363                 wake_up(&sai->sai_waitq);
364 }
365
366 /* insert inode into the list of sai_agls */
367 static void ll_agl_add(struct ll_statahead_info *sai,
368                        struct inode *inode, int index)
369 {
370         struct ll_inode_info *child  = ll_i2info(inode);
371         struct ll_inode_info *parent = ll_i2info(sai->sai_dentry->d_inode);
372         int                   added  = 0;
373
374         spin_lock(&child->lli_agl_lock);
375         if (child->lli_agl_index == 0) {
376                 child->lli_agl_index = index;
377                 spin_unlock(&child->lli_agl_lock);
378
379                 LASSERT(list_empty(&child->lli_agl_list));
380
381                 igrab(inode);
382                 spin_lock(&parent->lli_agl_lock);
383                 if (agl_list_empty(sai))
384                         added = 1;
385                 list_add_tail(&child->lli_agl_list, &sai->sai_agls);
386                 spin_unlock(&parent->lli_agl_lock);
387         } else {
388                 spin_unlock(&child->lli_agl_lock);
389         }
390
391         if (added > 0)
392                 wake_up(&sai->sai_agl_thread.t_ctl_waitq);
393 }
394
395 /* allocate sai */
396 static struct ll_statahead_info *ll_sai_alloc(struct dentry *dentry)
397 {
398         struct ll_statahead_info *sai;
399         struct ll_inode_info *lli = ll_i2info(dentry->d_inode);
400         int i;
401         ENTRY;
402
403         OBD_ALLOC_PTR(sai);
404         if (!sai)
405                 RETURN(NULL);
406
407         sai->sai_dentry = dget(dentry);
408         atomic_set(&sai->sai_refcount, 1);
409         sai->sai_max = LL_SA_RPC_MIN;
410         sai->sai_index = 1;
411         init_waitqueue_head(&sai->sai_waitq);
412         init_waitqueue_head(&sai->sai_thread.t_ctl_waitq);
413         init_waitqueue_head(&sai->sai_agl_thread.t_ctl_waitq);
414
415         INIT_LIST_HEAD(&sai->sai_interim_entries);
416         INIT_LIST_HEAD(&sai->sai_entries);
417         INIT_LIST_HEAD(&sai->sai_agls);
418
419         for (i = 0; i < LL_SA_CACHE_SIZE; i++) {
420                 INIT_LIST_HEAD(&sai->sai_cache[i]);
421                 spin_lock_init(&sai->sai_cache_lock[i]);
422         }
423         atomic_set(&sai->sai_cache_count, 0);
424
425         spin_lock(&sai_generation_lock);
426         lli->lli_sa_generation = ++sai_generation;
427         if (unlikely(sai_generation == 0))
428                 lli->lli_sa_generation = ++sai_generation;
429         spin_unlock(&sai_generation_lock);
430
431         RETURN(sai);
432 }
433
434 /* free sai */
435 static inline void ll_sai_free(struct ll_statahead_info *sai)
436 {
437         LASSERT(sai->sai_dentry != NULL);
438         dput(sai->sai_dentry);
439         OBD_FREE_PTR(sai);
440 }
441
442 /*
443  * take refcount of sai if sai for @dir exists, which means statahead is on for
444  * this directory.
445  */
446 static inline struct ll_statahead_info *ll_sai_get(struct inode *dir)
447 {
448         struct ll_inode_info *lli = ll_i2info(dir);
449         struct ll_statahead_info *sai = NULL;
450
451         spin_lock(&lli->lli_sa_lock);
452         sai = lli->lli_sai;
453         if (sai != NULL)
454                 atomic_inc(&sai->sai_refcount);
455         spin_unlock(&lli->lli_sa_lock);
456
457         return sai;
458 }
459
460 /*
461  * put sai refcount after use, if refcount reaches zero, free sai and sa_entries
462  * attached to it.
463  */
464 static void ll_sai_put(struct ll_statahead_info *sai)
465 {
466         struct ll_inode_info *lli = ll_i2info(sai->sai_dentry->d_inode);
467
468         if (atomic_dec_and_lock(&sai->sai_refcount, &lli->lli_sa_lock)) {
469                 struct sa_entry *entry, *next;
470                 struct ll_sb_info *sbi = ll_i2sbi(sai->sai_dentry->d_inode);
471
472                 lli->lli_sai = NULL;
473                 spin_unlock(&lli->lli_sa_lock);
474
475                 LASSERT(thread_is_stopped(&sai->sai_thread));
476                 LASSERT(thread_is_stopped(&sai->sai_agl_thread));
477                 LASSERT(sai->sai_sent == sai->sai_replied);
478                 LASSERT(!sa_has_callback(sai));
479
480                 list_for_each_entry_safe(entry, next, &sai->sai_entries,
481                                          se_list)
482                         sa_kill(sai, entry);
483
484                 LASSERT(atomic_read(&sai->sai_cache_count) == 0);
485                 LASSERT(agl_list_empty(sai));
486
487                 ll_sai_free(sai);
488                 atomic_dec(&sbi->ll_sa_running);
489         }
490 }
491
492 /* Do NOT forget to drop inode refcount when into sai_agls. */
493 static void ll_agl_trigger(struct inode *inode, struct ll_statahead_info *sai)
494 {
495         struct ll_inode_info *lli = ll_i2info(inode);
496         __u64 index = lli->lli_agl_index;
497         int rc;
498         ENTRY;
499
500         LASSERT(list_empty(&lli->lli_agl_list));
501
502         /* AGL maybe fall behind statahead with one entry */
503         if (is_omitted_entry(sai, index + 1)) {
504                 lli->lli_agl_index = 0;
505                 iput(inode);
506                 RETURN_EXIT;
507         }
508
509         /* In case of restore, the MDT has the right size and has already
510          * sent it back without granting the layout lock, inode is up-to-date.
511          * Then AGL (async glimpse lock) is useless.
512          * Also to glimpse we need the layout, in case of a runninh restore
513          * the MDT holds the layout lock so the glimpse will block up to the
514          * end of restore (statahead/agl will block) */
515         if (ll_file_test_flag(lli, LLIF_FILE_RESTORING)) {
516                 lli->lli_agl_index = 0;
517                 iput(inode);
518                 RETURN_EXIT;
519         }
520
521         /* Someone is in glimpse (sync or async), do nothing. */
522         rc = down_write_trylock(&lli->lli_glimpse_sem);
523         if (rc == 0) {
524                 lli->lli_agl_index = 0;
525                 iput(inode);
526                 RETURN_EXIT;
527         }
528
529         /*
530          * Someone triggered glimpse within 1 sec before.
531          * 1) The former glimpse succeeded with glimpse lock granted by OST, and
532          *    if the lock is still cached on client, AGL needs to do nothing. If
533          *    it is cancelled by other client, AGL maybe cannot obtaion new lock
534          *    for no glimpse callback triggered by AGL.
535          * 2) The former glimpse succeeded, but OST did not grant glimpse lock.
536          *    Under such case, it is quite possible that the OST will not grant
537          *    glimpse lock for AGL also.
538          * 3) The former glimpse failed, compared with other two cases, it is
539          *    relative rare. AGL can ignore such case, and it will not muchly
540          *    affect the performance.
541          */
542         if (lli->lli_glimpse_time != 0 &&
543             cfs_time_before(cfs_time_shift(-1), lli->lli_glimpse_time)) {
544                 up_write(&lli->lli_glimpse_sem);
545                 lli->lli_agl_index = 0;
546                 iput(inode);
547                 RETURN_EXIT;
548         }
549
550         CDEBUG(D_READA, "Handling (init) async glimpse: inode = "
551                DFID", idx = %llu\n", PFID(&lli->lli_fid), index);
552
553         cl_agl(inode);
554         lli->lli_agl_index = 0;
555         lli->lli_glimpse_time = cfs_time_current();
556         up_write(&lli->lli_glimpse_sem);
557
558         CDEBUG(D_READA, "Handled (init) async glimpse: inode= "
559                DFID", idx = %llu, rc = %d\n",
560                PFID(&lli->lli_fid), index, rc);
561
562         iput(inode);
563
564         EXIT;
565 }
566
567 /*
568  * prepare inode for sa entry, add it into agl list, now sa_entry is ready
569  * to be used by scanner process.
570  */
571 static void sa_instantiate(struct ll_statahead_info *sai,
572                                  struct sa_entry *entry)
573 {
574         struct inode *dir = sai->sai_dentry->d_inode;
575         struct inode *child;
576         struct md_enqueue_info *minfo;
577         struct lookup_intent *it;
578         struct ptlrpc_request *req;
579         struct mdt_body *body;
580         int rc = 0;
581         ENTRY;
582
583         LASSERT(entry->se_handle != 0);
584
585         minfo = entry->se_minfo;
586         it = &minfo->mi_it;
587         req = entry->se_req;
588         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
589         if (body == NULL)
590                 GOTO(out, rc = -EFAULT);
591
592         child = entry->se_inode;
593         if (child != NULL) {
594                 /* revalidate; unlinked and re-created with the same name */
595                 if (unlikely(!lu_fid_eq(&minfo->mi_data.op_fid2,
596                                         &body->mbo_fid1))) {
597                         entry->se_inode = NULL;
598                         iput(child);
599                         child = NULL;
600                 }
601         }
602
603         it->it_lock_handle = entry->se_handle;
604         rc = md_revalidate_lock(ll_i2mdexp(dir), it, ll_inode2fid(dir), NULL);
605         if (rc != 1)
606                 GOTO(out, rc = -EAGAIN);
607
608         rc = ll_prep_inode(&child, req, dir->i_sb, it);
609         if (rc)
610                 GOTO(out, rc);
611
612         CDEBUG(D_READA, "%s: setting %.*s"DFID" l_data to inode %p\n",
613                ll_get_fsname(child->i_sb, NULL, 0),
614                entry->se_qstr.len, entry->se_qstr.name,
615                PFID(ll_inode2fid(child)), child);
616         ll_set_lock_data(ll_i2sbi(dir)->ll_md_exp, child, it, NULL);
617
618         entry->se_inode = child;
619
620         if (agl_should_run(sai, child))
621                 ll_agl_add(sai, child, entry->se_index);
622
623         EXIT;
624
625 out:
626         /* sa_make_ready() will drop ldlm ibits lock refcount by calling
627          * ll_intent_drop_lock() in spite of failures. Do not worry about
628          * calling ll_intent_drop_lock() more than once. */
629         sa_make_ready(sai, entry, rc);
630 }
631
632 /* once there are async stat replies, instantiate sa_entry from replies */
633 static void sa_handle_callback(struct ll_statahead_info *sai)
634 {
635         struct ll_inode_info *lli;
636
637         lli = ll_i2info(sai->sai_dentry->d_inode);
638
639         while (sa_has_callback(sai)) {
640                 struct sa_entry *entry;
641
642                 spin_lock(&lli->lli_sa_lock);
643                 if (unlikely(!sa_has_callback(sai))) {
644                         spin_unlock(&lli->lli_sa_lock);
645                         break;
646                 }
647                 entry = list_entry(sai->sai_interim_entries.next,
648                                    struct sa_entry, se_list);
649                 list_del_init(&entry->se_list);
650                 spin_unlock(&lli->lli_sa_lock);
651
652                 sa_instantiate(sai, entry);
653         }
654 }
655
656 /*
657  * callback for async stat RPC, because this is called in ptlrpcd context, we
658  * only put sa_entry in sai_interim_entries, and wake up statahead thread to
659  * really prepare inode and instantiate sa_entry later.
660  */
661 static int ll_statahead_interpret(struct ptlrpc_request *req,
662                                   struct md_enqueue_info *minfo, int rc)
663 {
664         struct lookup_intent *it = &minfo->mi_it;
665         struct inode *dir = minfo->mi_dir;
666         struct ll_inode_info *lli = ll_i2info(dir);
667         struct ll_statahead_info *sai = lli->lli_sai;
668         struct sa_entry *entry = (struct sa_entry *)minfo->mi_cbdata;
669         __u64 handle = 0;
670         wait_queue_head_t *waitq = NULL;
671         ENTRY;
672
673         if (it_disposition(it, DISP_LOOKUP_NEG))
674                 rc = -ENOENT;
675
676         /* because statahead thread will wait for all inflight RPC to finish,
677          * sai should be always valid, no need to refcount */
678         LASSERT(sai != NULL);
679         LASSERT(!thread_is_stopped(&sai->sai_thread));
680         LASSERT(entry != NULL);
681
682         CDEBUG(D_READA, "sa_entry %.*s rc %d\n",
683                entry->se_qstr.len, entry->se_qstr.name, rc);
684
685         if (rc != 0) {
686                 ll_intent_release(it);
687                 iput(dir);
688                 OBD_FREE_PTR(minfo);
689         } else {
690                 /* release ibits lock ASAP to avoid deadlock when statahead
691                  * thread enqueues lock on parent in readdir and another
692                  * process enqueues lock on child with parent lock held, eg.
693                  * unlink. */
694                 handle = it->it_lock_handle;
695                 ll_intent_drop_lock(it);
696         }
697
698         spin_lock(&lli->lli_sa_lock);
699         if (rc != 0) {
700                 if (__sa_make_ready(sai, entry, rc))
701                         waitq = &sai->sai_waitq;
702         } else {
703                 entry->se_minfo = minfo;
704                 entry->se_req = ptlrpc_request_addref(req);
705                 /* Release the async ibits lock ASAP to avoid deadlock
706                  * when statahead thread tries to enqueue lock on parent
707                  * for readpage and other tries to enqueue lock on child
708                  * with parent's lock held, for example: unlink. */
709                 entry->se_handle = handle;
710                 if (!sa_has_callback(sai))
711                         waitq = &sai->sai_thread.t_ctl_waitq;
712
713                 list_add_tail(&entry->se_list, &sai->sai_interim_entries);
714         }
715         sai->sai_replied++;
716
717         smp_mb();
718         if (waitq != NULL)
719                 wake_up(waitq);
720         spin_unlock(&lli->lli_sa_lock);
721
722         RETURN(rc);
723 }
724
725 /* finish async stat RPC arguments */
726 static void sa_fini_data(struct md_enqueue_info *minfo)
727 {
728         iput(minfo->mi_dir);
729         OBD_FREE_PTR(minfo);
730 }
731
732 /*
733  * prepare arguments for async stat RPC.
734  */
735 static struct md_enqueue_info *
736 sa_prep_data(struct inode *dir, struct inode *child, struct sa_entry *entry)
737 {
738         struct md_enqueue_info   *minfo;
739         struct ldlm_enqueue_info *einfo;
740         struct md_op_data        *op_data;
741
742         OBD_ALLOC_PTR(minfo);
743         if (minfo == NULL)
744                 return ERR_PTR(-ENOMEM);
745
746         op_data = ll_prep_md_op_data(&minfo->mi_data, dir, child,
747                                      entry->se_qstr.name, entry->se_qstr.len, 0,
748                                      LUSTRE_OPC_ANY, NULL);
749         if (IS_ERR(op_data)) {
750                 OBD_FREE_PTR(minfo);
751                 return (struct md_enqueue_info *)op_data;
752         }
753
754         if (child == NULL)
755                 op_data->op_fid2 = entry->se_fid;
756
757         minfo->mi_it.it_op = IT_GETATTR;
758         minfo->mi_dir = igrab(dir);
759         minfo->mi_cb = ll_statahead_interpret;
760         minfo->mi_cbdata = entry;
761
762         einfo = &minfo->mi_einfo;
763         einfo->ei_type   = LDLM_IBITS;
764         einfo->ei_mode   = it_to_lock_mode(&minfo->mi_it);
765         einfo->ei_cb_bl  = ll_md_blocking_ast;
766         einfo->ei_cb_cp  = ldlm_completion_ast;
767         einfo->ei_cb_gl  = NULL;
768         einfo->ei_cbdata = NULL;
769
770         return minfo;
771 }
772
773 /* async stat for file not found in dcache */
774 static int sa_lookup(struct inode *dir, struct sa_entry *entry)
775 {
776         struct md_enqueue_info   *minfo;
777         int                       rc;
778         ENTRY;
779
780         minfo = sa_prep_data(dir, NULL, entry);
781         if (IS_ERR(minfo))
782                 RETURN(PTR_ERR(minfo));
783
784         rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo);
785         if (rc < 0)
786                 sa_fini_data(minfo);
787
788         RETURN(rc);
789 }
790
791 /**
792  * async stat for file found in dcache, similar to .revalidate
793  *
794  * \retval      1 dentry valid, no RPC sent
795  * \retval      0 dentry invalid, will send async stat RPC
796  * \retval      negative number upon error
797  */
798 static int sa_revalidate(struct inode *dir, struct sa_entry *entry,
799                          struct dentry *dentry)
800 {
801         struct inode *inode = dentry->d_inode;
802         struct lookup_intent it = { .it_op = IT_GETATTR,
803                                     .it_lock_handle = 0 };
804         struct md_enqueue_info *minfo;
805         int rc;
806         ENTRY;
807
808         if (unlikely(inode == NULL))
809                 RETURN(1);
810
811         if (d_mountpoint(dentry))
812                 RETURN(1);
813
814         entry->se_inode = igrab(inode);
815         rc = md_revalidate_lock(ll_i2mdexp(dir), &it, ll_inode2fid(inode),
816                                 NULL);
817         if (rc == 1) {
818                 entry->se_handle = it.it_lock_handle;
819                 ll_intent_release(&it);
820                 RETURN(1);
821         }
822
823         minfo = sa_prep_data(dir, inode, entry);
824         if (IS_ERR(minfo)) {
825                 entry->se_inode = NULL;
826                 iput(inode);
827                 RETURN(PTR_ERR(minfo));
828         }
829
830         rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo);
831         if (rc < 0) {
832                 entry->se_inode = NULL;
833                 iput(inode);
834                 sa_fini_data(minfo);
835         }
836
837         RETURN(rc);
838 }
839
840 /* async stat for file with @name */
841 static void sa_statahead(struct dentry *parent, const char *name, int len,
842                          const struct lu_fid *fid)
843 {
844         struct inode *dir = parent->d_inode;
845         struct ll_inode_info *lli = ll_i2info(dir);
846         struct ll_statahead_info *sai = lli->lli_sai;
847         struct dentry *dentry = NULL;
848         struct sa_entry *entry;
849         int rc;
850         ENTRY;
851
852         entry = sa_alloc(parent, sai, sai->sai_index, name, len, fid);
853         if (IS_ERR(entry))
854                 RETURN_EXIT;
855
856         dentry = d_lookup(parent, &entry->se_qstr);
857         if (!dentry) {
858                 rc = sa_lookup(dir, entry);
859         } else {
860                 rc = sa_revalidate(dir, entry, dentry);
861                 if (rc == 1 && agl_should_run(sai, dentry->d_inode))
862                         ll_agl_add(sai, dentry->d_inode, entry->se_index);
863         }
864
865         if (dentry != NULL)
866                 dput(dentry);
867
868         if (rc != 0)
869                 sa_make_ready(sai, entry, rc);
870         else
871                 sai->sai_sent++;
872
873         sai->sai_index++;
874
875         EXIT;
876 }
877
878 /* async glimpse (agl) thread main function */
879 static int ll_agl_thread(void *arg)
880 {
881         struct dentry *parent = (struct dentry *)arg;
882         struct inode *dir = parent->d_inode;
883         struct ll_inode_info *plli = ll_i2info(dir);
884         struct ll_inode_info *clli;
885         struct ll_sb_info *sbi = ll_i2sbi(dir);
886         struct ll_statahead_info *sai;
887         struct ptlrpc_thread *thread;
888         struct l_wait_info lwi = { 0 };
889         ENTRY;
890
891
892         sai = ll_sai_get(dir);
893         thread = &sai->sai_agl_thread;
894         thread->t_pid = current_pid();
895         CDEBUG(D_READA, "agl thread started: sai %p, parent %.*s\n",
896                sai, parent->d_name.len, parent->d_name.name);
897
898         atomic_inc(&sbi->ll_agl_total);
899         spin_lock(&plli->lli_agl_lock);
900         sai->sai_agl_valid = 1;
901         if (thread_is_init(thread))
902                 /* If someone else has changed the thread state
903                  * (e.g. already changed to SVC_STOPPING), we can't just
904                  * blindly overwrite that setting. */
905                 thread_set_flags(thread, SVC_RUNNING);
906         spin_unlock(&plli->lli_agl_lock);
907         wake_up(&thread->t_ctl_waitq);
908
909         while (1) {
910                 l_wait_event(thread->t_ctl_waitq,
911                              !agl_list_empty(sai) ||
912                              !thread_is_running(thread),
913                              &lwi);
914
915                 if (!thread_is_running(thread))
916                         break;
917
918                 spin_lock(&plli->lli_agl_lock);
919                 /* The statahead thread maybe help to process AGL entries,
920                  * so check whether list empty again. */
921                 if (!agl_list_empty(sai)) {
922                         clli = agl_first_entry(sai);
923                         list_del_init(&clli->lli_agl_list);
924                         spin_unlock(&plli->lli_agl_lock);
925                         ll_agl_trigger(&clli->lli_vfs_inode, sai);
926                 } else {
927                         spin_unlock(&plli->lli_agl_lock);
928                 }
929         }
930
931         spin_lock(&plli->lli_agl_lock);
932         sai->sai_agl_valid = 0;
933         while (!agl_list_empty(sai)) {
934                 clli = agl_first_entry(sai);
935                 list_del_init(&clli->lli_agl_list);
936                 spin_unlock(&plli->lli_agl_lock);
937                 clli->lli_agl_index = 0;
938                 iput(&clli->lli_vfs_inode);
939                 spin_lock(&plli->lli_agl_lock);
940         }
941         thread_set_flags(thread, SVC_STOPPED);
942         spin_unlock(&plli->lli_agl_lock);
943         wake_up(&thread->t_ctl_waitq);
944         ll_sai_put(sai);
945         CDEBUG(D_READA, "agl thread stopped: sai %p, parent %.*s\n",
946                sai, parent->d_name.len, parent->d_name.name);
947         RETURN(0);
948 }
949
950 /* start agl thread */
951 static void ll_start_agl(struct dentry *parent, struct ll_statahead_info *sai)
952 {
953         struct ptlrpc_thread *thread = &sai->sai_agl_thread;
954         struct l_wait_info    lwi    = { 0 };
955         struct ll_inode_info  *plli;
956         struct task_struct            *task;
957         ENTRY;
958
959         CDEBUG(D_READA, "start agl thread: sai %p, parent %.*s\n",
960                sai, parent->d_name.len, parent->d_name.name);
961
962         plli = ll_i2info(parent->d_inode);
963         task = kthread_run(ll_agl_thread, parent,
964                                "ll_agl_%u", plli->lli_opendir_pid);
965         if (IS_ERR(task)) {
966                 CERROR("can't start ll_agl thread, rc: %ld\n", PTR_ERR(task));
967                 thread_set_flags(thread, SVC_STOPPED);
968                 RETURN_EXIT;
969         }
970
971         l_wait_event(thread->t_ctl_waitq,
972                      thread_is_running(thread) || thread_is_stopped(thread),
973                      &lwi);
974         EXIT;
975 }
976
977 /* statahead thread main function */
978 static int ll_statahead_thread(void *arg)
979 {
980         struct dentry *parent = (struct dentry *)arg;
981         struct inode *dir = parent->d_inode;
982         struct ll_inode_info *lli = ll_i2info(dir);
983         struct ll_sb_info *sbi = ll_i2sbi(dir);
984         struct ll_statahead_info *sai;
985         struct ptlrpc_thread *sa_thread;
986         struct ptlrpc_thread *agl_thread;
987         int first = 0;
988         struct md_op_data *op_data;
989         struct ll_dir_chain chain;
990         struct l_wait_info lwi = { 0 };
991         struct page *page = NULL;
992         __u64 pos = 0;
993         int rc = 0;
994         ENTRY;
995
996         sai = ll_sai_get(dir);
997         sa_thread = &sai->sai_thread;
998         agl_thread = &sai->sai_agl_thread;
999         sa_thread->t_pid = current_pid();
1000         CDEBUG(D_READA, "statahead thread starting: sai %p, parent %.*s\n",
1001                sai, parent->d_name.len, parent->d_name.name);
1002
1003         op_data = ll_prep_md_op_data(NULL, dir, dir, NULL, 0, 0,
1004                                      LUSTRE_OPC_ANY, dir);
1005         if (IS_ERR(op_data))
1006                 GOTO(out, rc = PTR_ERR(op_data));
1007
1008         if (sbi->ll_flags & LL_SBI_AGL_ENABLED)
1009                 ll_start_agl(parent, sai);
1010
1011         atomic_inc(&sbi->ll_sa_total);
1012         spin_lock(&lli->lli_sa_lock);
1013         if (thread_is_init(sa_thread))
1014                 /* If someone else has changed the thread state
1015                  * (e.g. already changed to SVC_STOPPING), we can't just
1016                  * blindly overwrite that setting. */
1017                 thread_set_flags(sa_thread, SVC_RUNNING);
1018         spin_unlock(&lli->lli_sa_lock);
1019         wake_up(&sa_thread->t_ctl_waitq);
1020
1021         ll_dir_chain_init(&chain);
1022         while (pos != MDS_DIR_END_OFF && thread_is_running(sa_thread)) {
1023                 struct lu_dirpage *dp;
1024                 struct lu_dirent  *ent;
1025
1026                 sai->sai_in_readpage = 1;
1027                 page = ll_get_dir_page(dir, op_data, pos, &chain);
1028                 sai->sai_in_readpage = 0;
1029                 if (IS_ERR(page)) {
1030                         rc = PTR_ERR(page);
1031                         CDEBUG(D_READA, "error reading dir "DFID" at %llu"
1032                                "/%llu opendir_pid = %u: rc = %d\n",
1033                                PFID(ll_inode2fid(dir)), pos, sai->sai_index,
1034                                lli->lli_opendir_pid, rc);
1035                         break;
1036                 }
1037
1038                 dp = page_address(page);
1039                 for (ent = lu_dirent_start(dp);
1040                      ent != NULL && thread_is_running(sa_thread) &&
1041                      !sa_low_hit(sai);
1042                      ent = lu_dirent_next(ent)) {
1043                         __u64 hash;
1044                         int namelen;
1045                         char *name;
1046                         struct lu_fid fid;
1047
1048                         hash = le64_to_cpu(ent->lde_hash);
1049                         if (unlikely(hash < pos))
1050                                 /*
1051                                  * Skip until we find target hash value.
1052                                  */
1053                                 continue;
1054
1055                         namelen = le16_to_cpu(ent->lde_namelen);
1056                         if (unlikely(namelen == 0))
1057                                 /*
1058                                  * Skip dummy record.
1059                                  */
1060                                 continue;
1061
1062                         name = ent->lde_name;
1063                         if (name[0] == '.') {
1064                                 if (namelen == 1) {
1065                                         /*
1066                                          * skip "."
1067                                          */
1068                                         continue;
1069                                 } else if (name[1] == '.' && namelen == 2) {
1070                                         /*
1071                                          * skip ".."
1072                                          */
1073                                         continue;
1074                                 } else if (!sai->sai_ls_all) {
1075                                         /*
1076                                          * skip hidden files.
1077                                          */
1078                                         sai->sai_skip_hidden++;
1079                                         continue;
1080                                 }
1081                         }
1082
1083                         /*
1084                          * don't stat-ahead first entry.
1085                          */
1086                         if (unlikely(++first == 1))
1087                                 continue;
1088
1089                         fid_le_to_cpu(&fid, &ent->lde_fid);
1090
1091                         /* wait for spare statahead window */
1092                         do {
1093                                 l_wait_event(sa_thread->t_ctl_waitq,
1094                                              !sa_sent_full(sai) ||
1095                                              sa_has_callback(sai) ||
1096                                              !agl_list_empty(sai) ||
1097                                              !thread_is_running(sa_thread),
1098                                              &lwi);
1099
1100                                 sa_handle_callback(sai);
1101
1102                                 spin_lock(&lli->lli_agl_lock);
1103                                 while (sa_sent_full(sai) &&
1104                                        !agl_list_empty(sai)) {
1105                                         struct ll_inode_info *clli;
1106
1107                                         clli = agl_first_entry(sai);
1108                                         list_del_init(&clli->lli_agl_list);
1109                                         spin_unlock(&lli->lli_agl_lock);
1110
1111                                         ll_agl_trigger(&clli->lli_vfs_inode,
1112                                                         sai);
1113
1114                                         spin_lock(&lli->lli_agl_lock);
1115                                 }
1116                                 spin_unlock(&lli->lli_agl_lock);
1117                         } while (sa_sent_full(sai) &&
1118                                  thread_is_running(sa_thread));
1119
1120                         sa_statahead(parent, name, namelen, &fid);
1121                 }
1122
1123                 pos = le64_to_cpu(dp->ldp_hash_end);
1124                 ll_release_page(dir, page,
1125                                 le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1126
1127                 if (sa_low_hit(sai)) {
1128                         rc = -EFAULT;
1129                         atomic_inc(&sbi->ll_sa_wrong);
1130                         CDEBUG(D_READA, "Statahead for dir "DFID" hit "
1131                                "ratio too low: hit/miss %llu/%llu"
1132                                ", sent/replied %llu/%llu, stopping "
1133                                "statahead thread: pid %d\n",
1134                                PFID(&lli->lli_fid), sai->sai_hit,
1135                                sai->sai_miss, sai->sai_sent,
1136                                sai->sai_replied, current_pid());
1137                         break;
1138                 }
1139         }
1140         ll_dir_chain_fini(&chain);
1141         ll_finish_md_op_data(op_data);
1142
1143         if (rc < 0) {
1144                 spin_lock(&lli->lli_sa_lock);
1145                 thread_set_flags(sa_thread, SVC_STOPPING);
1146                 lli->lli_sa_enabled = 0;
1147                 spin_unlock(&lli->lli_sa_lock);
1148         }
1149
1150         /* statahead is finished, but statahead entries need to be cached, wait
1151          * for file release to stop me. */
1152         while (thread_is_running(sa_thread)) {
1153                 l_wait_event(sa_thread->t_ctl_waitq,
1154                              sa_has_callback(sai) ||
1155                              !thread_is_running(sa_thread),
1156                              &lwi);
1157
1158                 sa_handle_callback(sai);
1159         }
1160
1161         EXIT;
1162 out:
1163         if (sai->sai_agl_valid) {
1164                 spin_lock(&lli->lli_agl_lock);
1165                 thread_set_flags(agl_thread, SVC_STOPPING);
1166                 spin_unlock(&lli->lli_agl_lock);
1167                 wake_up(&agl_thread->t_ctl_waitq);
1168
1169                 CDEBUG(D_READA, "stop agl thread: sai %p pid %u\n",
1170                        sai, (unsigned int)agl_thread->t_pid);
1171                 l_wait_event(agl_thread->t_ctl_waitq,
1172                              thread_is_stopped(agl_thread),
1173                              &lwi);
1174         } else {
1175                 /* Set agl_thread flags anyway. */
1176                 thread_set_flags(agl_thread, SVC_STOPPED);
1177         }
1178
1179         /* wait for inflight statahead RPCs to finish, and then we can free sai
1180          * safely because statahead RPC will access sai data */
1181         while (sai->sai_sent != sai->sai_replied) {
1182                 /* in case we're not woken up, timeout wait */
1183                 lwi = LWI_TIMEOUT(msecs_to_jiffies(MSEC_PER_SEC >> 3),
1184                                   NULL, NULL);
1185                 l_wait_event(sa_thread->t_ctl_waitq,
1186                         sai->sai_sent == sai->sai_replied, &lwi);
1187         }
1188
1189         /* release resources held by statahead RPCs */
1190         sa_handle_callback(sai);
1191
1192         spin_lock(&lli->lli_sa_lock);
1193         thread_set_flags(sa_thread, SVC_STOPPED);
1194         spin_unlock(&lli->lli_sa_lock);
1195
1196         CDEBUG(D_READA, "statahead thread stopped: sai %p, parent %.*s\n",
1197                sai, parent->d_name.len, parent->d_name.name);
1198
1199         wake_up(&sai->sai_waitq);
1200         wake_up(&sa_thread->t_ctl_waitq);
1201         ll_sai_put(sai);
1202
1203         return rc;
1204 }
1205
1206 /* authorize opened dir handle @key to statahead */
1207 void ll_authorize_statahead(struct inode *dir, void *key)
1208 {
1209         struct ll_inode_info *lli = ll_i2info(dir);
1210
1211         spin_lock(&lli->lli_sa_lock);
1212         if (lli->lli_opendir_key == NULL && lli->lli_sai == NULL) {
1213                 /*
1214                  * if lli_sai is not NULL, it means previous statahead is not
1215                  * finished yet, we'd better not start a new statahead for now.
1216                  */
1217                 LASSERT(lli->lli_opendir_pid == 0);
1218                 lli->lli_opendir_key = key;
1219                 lli->lli_opendir_pid = current_pid();
1220                 lli->lli_sa_enabled = 1;
1221         }
1222         spin_unlock(&lli->lli_sa_lock);
1223 }
1224
1225 /*
1226  * deauthorize opened dir handle @key to statahead, and notify statahead thread
1227  * to quit if it's running.
1228  */
1229 void ll_deauthorize_statahead(struct inode *dir, void *key)
1230 {
1231         struct ll_inode_info *lli = ll_i2info(dir);
1232         struct ll_statahead_info *sai;
1233
1234         LASSERT(lli->lli_opendir_key == key);
1235         LASSERT(lli->lli_opendir_pid != 0);
1236
1237         CDEBUG(D_READA, "deauthorize statahead for "DFID"\n",
1238                 PFID(&lli->lli_fid));
1239
1240         spin_lock(&lli->lli_sa_lock);
1241         lli->lli_opendir_key = NULL;
1242         lli->lli_opendir_pid = 0;
1243         lli->lli_sa_enabled = 0;
1244         sai = lli->lli_sai;
1245         if (sai != NULL && thread_is_running(&sai->sai_thread)) {
1246                 /*
1247                  * statahead thread may not quit yet because it needs to cache
1248                  * entries, now it's time to tell it to quit.
1249                  *
1250                  * In case sai is released, wake_up() is called inside spinlock,
1251                  * so we have to call smp_mb() explicitely to serialize ops.
1252                  */
1253                 thread_set_flags(&sai->sai_thread, SVC_STOPPING);
1254                 smp_mb();
1255                 wake_up(&sai->sai_thread.t_ctl_waitq);
1256         }
1257         spin_unlock(&lli->lli_sa_lock);
1258 }
1259
1260 enum {
1261         /**
1262          * not first dirent, or is "."
1263          */
1264         LS_NOT_FIRST_DE = 0,
1265         /**
1266          * the first non-hidden dirent
1267          */
1268         LS_FIRST_DE,
1269         /**
1270          * the first hidden dirent, that is "."
1271          */
1272         LS_FIRST_DOT_DE
1273 };
1274
1275 /* file is first dirent under @dir */
1276 static int is_first_dirent(struct inode *dir, struct dentry *dentry)
1277 {
1278         struct ll_dir_chain   chain;
1279         struct qstr          *target = &dentry->d_name;
1280         struct md_op_data    *op_data;
1281         int                   dot_de;
1282         struct page          *page = NULL;
1283         int                   rc = LS_NOT_FIRST_DE;
1284         __u64                 pos = 0;
1285         ENTRY;
1286
1287         op_data = ll_prep_md_op_data(NULL, dir, dir, NULL, 0, 0,
1288                                      LUSTRE_OPC_ANY, dir);
1289         if (IS_ERR(op_data))
1290                 RETURN(PTR_ERR(op_data));
1291         /**
1292          *FIXME choose the start offset of the readdir
1293          */
1294
1295         ll_dir_chain_init(&chain);
1296         page = ll_get_dir_page(dir, op_data, 0, &chain);
1297
1298         while (1) {
1299                 struct lu_dirpage *dp;
1300                 struct lu_dirent  *ent;
1301
1302                 if (IS_ERR(page)) {
1303                         struct ll_inode_info *lli = ll_i2info(dir);
1304
1305                         rc = PTR_ERR(page);
1306                         CERROR("%s: reading dir "DFID" at %llu"
1307                                "opendir_pid = %u : rc = %d\n",
1308                                ll_get_fsname(dir->i_sb, NULL, 0),
1309                                PFID(ll_inode2fid(dir)), pos,
1310                                lli->lli_opendir_pid, rc);
1311                         break;
1312                 }
1313
1314                 dp = page_address(page);
1315                 for (ent = lu_dirent_start(dp); ent != NULL;
1316                      ent = lu_dirent_next(ent)) {
1317                         __u64 hash;
1318                         int namelen;
1319                         char *name;
1320
1321                         hash = le64_to_cpu(ent->lde_hash);
1322                         /* The ll_get_dir_page() can return any page containing
1323                          * the given hash which may be not the start hash. */
1324                         if (unlikely(hash < pos))
1325                                 continue;
1326
1327                         namelen = le16_to_cpu(ent->lde_namelen);
1328                         if (unlikely(namelen == 0))
1329                                 /*
1330                                  * skip dummy record.
1331                                  */
1332                                 continue;
1333
1334                         name = ent->lde_name;
1335                         if (name[0] == '.') {
1336                                 if (namelen == 1)
1337                                         /*
1338                                          * skip "."
1339                                          */
1340                                         continue;
1341                                 else if (name[1] == '.' && namelen == 2)
1342                                         /*
1343                                          * skip ".."
1344                                          */
1345                                         continue;
1346                                 else
1347                                         dot_de = 1;
1348                         } else {
1349                                 dot_de = 0;
1350                         }
1351
1352                         if (dot_de && target->name[0] != '.') {
1353                                 CDEBUG(D_READA, "%.*s skip hidden file %.*s\n",
1354                                        target->len, target->name,
1355                                        namelen, name);
1356                                 continue;
1357                         }
1358
1359                         if (target->len != namelen ||
1360                             memcmp(target->name, name, namelen) != 0)
1361                                 rc = LS_NOT_FIRST_DE;
1362                         else if (!dot_de)
1363                                 rc = LS_FIRST_DE;
1364                         else
1365                                 rc = LS_FIRST_DOT_DE;
1366
1367                         ll_release_page(dir, page, false);
1368                         GOTO(out, rc);
1369                 }
1370                 pos = le64_to_cpu(dp->ldp_hash_end);
1371                 if (pos == MDS_DIR_END_OFF) {
1372                         /*
1373                          * End of directory reached.
1374                          */
1375                         ll_release_page(dir, page, false);
1376                         GOTO(out, rc);
1377                 } else {
1378                         /*
1379                          * chain is exhausted
1380                          * Normal case: continue to the next page.
1381                          */
1382                         ll_release_page(dir, page, le32_to_cpu(dp->ldp_flags) &
1383                                               LDF_COLLIDE);
1384                         page = ll_get_dir_page(dir, op_data, pos, &chain);
1385                 }
1386         }
1387         EXIT;
1388 out:
1389         ll_dir_chain_fini(&chain);
1390         ll_finish_md_op_data(op_data);
1391         return rc;
1392 }
1393
1394 /**
1395  * revalidate @dentryp from statahead cache
1396  *
1397  * \param[in] dir       parent directory
1398  * \param[in] sai       sai structure
1399  * \param[out] dentryp  pointer to dentry which will be revalidated
1400  * \param[in] unplug    unplug statahead window only (normally for negative
1401  *                      dentry)
1402  * \retval              1 on success, dentry is saved in @dentryp
1403  * \retval              0 if revalidation failed (no proper lock on client)
1404  * \retval              negative number upon error
1405  */
1406 static int revalidate_statahead_dentry(struct inode *dir,
1407                                         struct ll_statahead_info *sai,
1408                                         struct dentry **dentryp,
1409                                         bool unplug)
1410 {
1411         struct sa_entry *entry = NULL;
1412         struct l_wait_info lwi = { 0 };
1413         struct ll_dentry_data *ldd;
1414         struct ll_inode_info *lli = ll_i2info(dir);
1415         int rc = 0;
1416         ENTRY;
1417
1418         if ((*dentryp)->d_name.name[0] == '.') {
1419                 if (sai->sai_ls_all ||
1420                     sai->sai_miss_hidden >= sai->sai_skip_hidden) {
1421                         /*
1422                          * Hidden dentry is the first one, or statahead
1423                          * thread does not skip so many hidden dentries
1424                          * before "sai_ls_all" enabled as below.
1425                          */
1426                 } else {
1427                         if (!sai->sai_ls_all)
1428                                 /*
1429                                  * It maybe because hidden dentry is not
1430                                  * the first one, "sai_ls_all" was not
1431                                  * set, then "ls -al" missed. Enable
1432                                  * "sai_ls_all" for such case.
1433                                  */
1434                                 sai->sai_ls_all = 1;
1435
1436                         /*
1437                          * Such "getattr" has been skipped before
1438                          * "sai_ls_all" enabled as above.
1439                          */
1440                         sai->sai_miss_hidden++;
1441                         RETURN(-EAGAIN);
1442                 }
1443         }
1444
1445         if (unplug)
1446                 GOTO(out, rc = 1);
1447
1448         entry = sa_get(sai, &(*dentryp)->d_name);
1449         if (entry == NULL)
1450                 GOTO(out, rc = -EAGAIN);
1451
1452         /* if statahead is busy in readdir, help it do post-work */
1453         if (!sa_ready(entry) && sai->sai_in_readpage)
1454                 sa_handle_callback(sai);
1455
1456         if (!sa_ready(entry)) {
1457                 spin_lock(&lli->lli_sa_lock);
1458                 sai->sai_index_wait = entry->se_index;
1459                 spin_unlock(&lli->lli_sa_lock);
1460                 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(30), NULL,
1461                                        LWI_ON_SIGNAL_NOOP, NULL);
1462                 rc = l_wait_event(sai->sai_waitq, sa_ready(entry), &lwi);
1463                 if (rc < 0) {
1464                         /*
1465                          * entry may not be ready, so it may be used by inflight
1466                          * statahead RPC, don't free it.
1467                          */
1468                         entry = NULL;
1469                         GOTO(out, rc = -EAGAIN);
1470                 }
1471         }
1472
1473         if (entry->se_state == SA_ENTRY_SUCC && entry->se_inode != NULL) {
1474                 struct inode *inode = entry->se_inode;
1475                 struct lookup_intent it = { .it_op = IT_GETATTR,
1476                                             .it_lock_handle =
1477                                                 entry->se_handle };
1478                 __u64 bits;
1479
1480                 rc = md_revalidate_lock(ll_i2mdexp(dir), &it,
1481                                         ll_inode2fid(inode), &bits);
1482                 if (rc == 1) {
1483                         if ((*dentryp)->d_inode == NULL) {
1484                                 struct dentry *alias;
1485
1486                                 alias = ll_splice_alias(inode, *dentryp);
1487                                 if (IS_ERR(alias)) {
1488                                         ll_intent_release(&it);
1489                                         GOTO(out, rc = PTR_ERR(alias));
1490                                 }
1491                                 *dentryp = alias;
1492                                 /* statahead prepared this inode, transfer inode
1493                                  * refcount from sa_entry to dentry */
1494                                 entry->se_inode = NULL;
1495                         } else if ((*dentryp)->d_inode != inode) {
1496                                 /* revalidate, but inode is recreated */
1497                                 CDEBUG(D_READA,
1498                                         "%s: stale dentry %.*s inode "
1499                                         DFID", statahead inode "DFID
1500                                         "\n",
1501                                         ll_get_fsname((*dentryp)->d_inode->i_sb,
1502                                                       NULL, 0),
1503                                         (*dentryp)->d_name.len,
1504                                         (*dentryp)->d_name.name,
1505                                         PFID(ll_inode2fid((*dentryp)->d_inode)),
1506                                         PFID(ll_inode2fid(inode)));
1507                                 ll_intent_release(&it);
1508                                 GOTO(out, rc = -ESTALE);
1509                         }
1510
1511                         if ((bits & MDS_INODELOCK_LOOKUP) &&
1512                             d_lustre_invalid(*dentryp))
1513                                 d_lustre_revalidate(*dentryp);
1514                         ll_intent_release(&it);
1515                 }
1516         }
1517 out:
1518         /*
1519          * statahead cached sa_entry can be used only once, and will be killed
1520          * right after use, so if lookup/revalidate accessed statahead cache,
1521          * set dentry ldd_sa_generation to parent lli_sa_generation, later if we
1522          * stat this file again, we know we've done statahead before, see
1523          * dentry_may_statahead().
1524          */
1525         ldd = ll_d2d(*dentryp);
1526         /* ldd can be NULL if llite lookup failed. */
1527         if (ldd != NULL)
1528                 ldd->lld_sa_generation = lli->lli_sa_generation;
1529         sa_put(sai, entry);
1530
1531         RETURN(rc);
1532 }
1533
1534 /**
1535  * start statahead thread
1536  *
1537  * \param[in] dir       parent directory
1538  * \param[in] dentry    dentry that triggers statahead, normally the first
1539  *                      dirent under @dir
1540  * \retval              -EAGAIN on success, because when this function is
1541  *                      called, it's already in lookup call, so client should
1542  *                      do it itself instead of waiting for statahead thread
1543  *                      to do it asynchronously.
1544  * \retval              negative number upon error
1545  */
1546 static int start_statahead_thread(struct inode *dir, struct dentry *dentry)
1547 {
1548         struct ll_inode_info *lli = ll_i2info(dir);
1549         struct ll_statahead_info *sai = NULL;
1550         struct dentry *parent = dentry->d_parent;
1551         struct ptlrpc_thread *thread;
1552         struct l_wait_info lwi = { 0 };
1553         struct task_struct *task;
1554         int rc;
1555         ENTRY;
1556
1557         /* I am the "lli_opendir_pid" owner, only me can set "lli_sai". */
1558         rc = is_first_dirent(dir, dentry);
1559         if (rc == LS_NOT_FIRST_DE)
1560                 /* It is not "ls -{a}l" operation, no need statahead for it. */
1561                 GOTO(out, rc = -EFAULT);
1562
1563         sai = ll_sai_alloc(parent);
1564         if (sai == NULL)
1565                 GOTO(out, rc = -ENOMEM);
1566
1567         sai->sai_ls_all = (rc == LS_FIRST_DOT_DE);
1568
1569         /* if current lli_opendir_key was deauthorized, or dir re-opened by
1570          * another process, don't start statahead, otherwise the newly spawned
1571          * statahead thread won't be notified to quit. */
1572         spin_lock(&lli->lli_sa_lock);
1573         if (unlikely(lli->lli_sai != NULL ||
1574                      lli->lli_opendir_key == NULL ||
1575                      lli->lli_opendir_pid != current->pid)) {
1576                 spin_unlock(&lli->lli_sa_lock);
1577                 GOTO(out, rc = -EPERM);
1578         }
1579         lli->lli_sai = sai;
1580         spin_unlock(&lli->lli_sa_lock);
1581
1582         atomic_inc(&ll_i2sbi(parent->d_inode)->ll_sa_running);
1583
1584         CDEBUG(D_READA, "start statahead thread: [pid %d] [parent %.*s]\n",
1585                current_pid(), parent->d_name.len, parent->d_name.name);
1586
1587         task = kthread_run(ll_statahead_thread, parent, "ll_sa_%u",
1588                            lli->lli_opendir_pid);
1589         thread = &sai->sai_thread;
1590         if (IS_ERR(task)) {
1591                 spin_lock(&lli->lli_sa_lock);
1592                 lli->lli_sai = NULL;
1593                 spin_unlock(&lli->lli_sa_lock);
1594                 rc = PTR_ERR(task);
1595                 CERROR("can't start ll_sa thread, rc: %d\n", rc);
1596                 GOTO(out, rc);
1597         }
1598
1599         l_wait_event(thread->t_ctl_waitq,
1600                      thread_is_running(thread) || thread_is_stopped(thread),
1601                      &lwi);
1602         ll_sai_put(sai);
1603
1604         /*
1605          * We don't stat-ahead for the first dirent since we are already in
1606          * lookup.
1607          */
1608         RETURN(-EAGAIN);
1609
1610 out:
1611         /* once we start statahead thread failed, disable statahead so that
1612          * subsequent stat won't waste time to try it. */
1613         spin_lock(&lli->lli_sa_lock);
1614         if (lli->lli_opendir_pid == current->pid)
1615                 lli->lli_sa_enabled = 0;
1616         spin_unlock(&lli->lli_sa_lock);
1617
1618         if (sai != NULL)
1619                 ll_sai_free(sai);
1620
1621         RETURN(rc);
1622 }
1623
1624 /**
1625  * statahead entry function, this is called when client getattr on a file, it
1626  * will start statahead thread if this is the first dir entry, else revalidate
1627  * dentry from statahead cache.
1628  *
1629  * \param[in]  dir      parent directory
1630  * \param[out] dentryp  dentry to getattr
1631  * \param[in]  unplug   unplug statahead window only (normally for negative
1632  *                      dentry)
1633  * \retval              1 on success
1634  * \retval              0 revalidation from statahead cache failed, caller needs
1635  *                      to getattr from server directly
1636  * \retval              negative number on error, caller often ignores this and
1637  *                      then getattr from server
1638  */
1639 int ll_statahead(struct inode *dir, struct dentry **dentryp, bool unplug)
1640 {
1641         struct ll_statahead_info *sai;
1642
1643         sai = ll_sai_get(dir);
1644         if (sai != NULL) {
1645                 int rc;
1646
1647                 rc = revalidate_statahead_dentry(dir, sai, dentryp, unplug);
1648                 CDEBUG(D_READA, "revalidate statahead %.*s: %d.\n",
1649                         (*dentryp)->d_name.len, (*dentryp)->d_name.name, rc);
1650                 ll_sai_put(sai);
1651                 return rc;
1652         }
1653         return start_statahead_thread(dir, *dentryp);
1654 }