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