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