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