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