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