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