Whamcloud - gitweb
Branch b1_6
[fs/lustre-release.git] / lustre / llite / statahead.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2007 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include <linux/fs.h>
23 #include <linux/sched.h>
24 #include <linux/mm.h>
25 #include <linux/smp_lock.h>
26 #include <linux/highmem.h>
27 #include <linux/pagemap.h>
28
29 #define DEBUG_SUBSYSTEM S_LLITE
30
31 #include <obd_support.h>
32 #include <lustre_lite.h>
33 #include <lustre_dlm.h>
34 #include <linux/lustre_version.h>
35 #include "llite_internal.h"
36
37 struct ll_sai_entry {
38         struct list_head        se_list;
39         int                     se_index;
40         int                     se_stat;
41 };
42
43 enum {
44         SA_ENTRY_UNSTATED = 0,
45         SA_ENTRY_STATED
46 };
47
48 static struct ll_statahead_info *ll_sai_alloc(void)
49 {
50         struct ll_statahead_info *sai;
51
52         OBD_ALLOC_PTR(sai);
53         if (!sai)
54                 return NULL;
55
56         sai->sai_max = LL_STATAHEAD_MIN;
57         cfs_waitq_init(&sai->sai_thread.t_ctl_waitq);
58         CFS_INIT_LIST_HEAD(&sai->sai_entries);
59         atomic_set(&sai->sai_refc, 1);
60         return sai;
61 }
62
63 static inline 
64 struct ll_statahead_info *ll_sai_get(struct ll_statahead_info *sai)
65 {
66         LASSERT(sai);
67         atomic_inc(&sai->sai_refc);
68         return sai;
69 }
70
71 static void ll_sai_put(struct ll_statahead_info *sai)
72 {
73         struct inode *inode = sai->sai_inode;
74         struct ll_inode_info *lli = ll_i2info(inode);
75         ENTRY;
76
77         if (atomic_dec_and_lock(&sai->sai_refc, &lli->lli_lock)) {
78                 struct ll_sai_entry  *entry, *next;
79
80                 list_for_each_entry_safe(entry, next, &sai->sai_entries,
81                                          se_list) {
82                         list_del(&entry->se_list);
83                         OBD_FREE_PTR(entry);
84                 }
85                 OBD_FREE_PTR(sai);
86                 lli->lli_sai = NULL;
87                 spin_unlock(&lli->lli_lock);
88                 iput(inode);
89         }
90         EXIT;
91 }
92
93 static struct ll_sai_entry *ll_sai_entry_get(struct ll_statahead_info *sai,
94                                              int index, int stat)
95 {
96         struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
97         struct ll_sb_info    *sbi = ll_i2sbi(sai->sai_inode);
98         struct ll_sai_entry  *entry;
99         ENTRY;
100
101         OBD_ALLOC_PTR(entry);
102         if (entry == NULL)
103                 RETURN(NULL);
104         
105         CDEBUG(D_READA, "alloc sai entry %p index %d, stat %d\n",
106                entry, index, stat);
107         entry->se_index = index;
108         entry->se_stat  = stat;
109
110         spin_lock(&lli->lli_lock);
111         list_add_tail(&entry->se_list, &sai->sai_entries);
112         sai->sai_entries_nr++;
113         sbi->ll_sa_count = sai->sai_entries_nr;
114         spin_unlock(&lli->lli_lock);
115
116         LASSERT(sai->sai_entries_nr <= sbi->ll_sa_max);
117         RETURN(entry);
118 }
119
120 /* inside lli_lock */
121 static void ll_sai_entry_set(struct ll_statahead_info *sai, int index,
122                              int stat)
123 {
124         struct ll_sai_entry *entry;
125         ENTRY;
126
127         list_for_each_entry(entry, &sai->sai_entries, se_list) {
128                 if (entry->se_index == index) {
129                         LASSERT(entry->se_stat == SA_ENTRY_UNSTATED);
130                         entry->se_stat = stat;
131                         CDEBUG(D_READA, "set sai entry %p index %d stat %d\n",
132                                entry, index, stat);
133                         EXIT;
134                         return;
135                 }
136         }
137         /* Sometimes, this happens when entry has been put and freed */
138         CDEBUG(D_READA, "can't find sai entry index %d\n", index);
139         EXIT;
140 }
141
142 /* check first entry was stated already */
143 static int ll_sai_entry_stated(struct ll_statahead_info *sai)
144 {
145         struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
146         struct ll_sai_entry  *entry;
147         int                   rc = 0;
148         ENTRY;
149
150         spin_lock(&lli->lli_lock);
151         if (!list_empty(&sai->sai_entries)) {
152                 entry = list_entry(sai->sai_entries.next, struct ll_sai_entry,
153                                    se_list);
154                 CDEBUG(D_READA, "check sai entry %p index %d stat %d\n",
155                        entry, entry->se_index, entry->se_stat);
156                 rc = (entry->se_stat != SA_ENTRY_UNSTATED);
157         }
158         spin_unlock(&lli->lli_lock);
159
160         RETURN(rc);
161 }
162
163 /* inside lli_lock */
164 static void ll_sai_entry_put(struct ll_statahead_info *sai)
165 {
166         struct ll_sai_entry  *entry;
167         ENTRY;
168         
169         if (list_empty(&sai->sai_entries)) {
170                 EXIT;
171                 return;
172         }
173         LASSERT(sai->sai_entries_nr > 0);
174
175         entry = list_entry(sai->sai_entries.next, struct ll_sai_entry, se_list);
176         list_del(&entry->se_list);
177         sai->sai_entries_nr--;
178
179         CDEBUG(D_READA, "free sa entry %p index %d stat %d\n",
180                entry, entry->se_index, entry->se_stat);
181         OBD_FREE_PTR(entry);
182         EXIT;
183 }
184
185 /* finish lookup/revalidate */
186 static int ll_statahead_interpret(struct obd_export *exp,
187                                   struct ptlrpc_request *req,
188                                   struct md_enqueue_info *minfo,
189                                   int rc)
190 {
191         struct lookup_intent     *it = &minfo->mi_it;
192         struct dentry            *dentry = minfo->mi_dentry;
193         struct inode             *dir = dentry->d_parent->d_inode;
194         struct ll_inode_info     *lli = ll_i2info(dir);
195         struct ll_statahead_info *sai;
196         ENTRY;
197
198         CDEBUG(D_READA, "interpret statahead %.*s rc %d\n",
199                dentry->d_name.len, dentry->d_name.name, rc);
200         if (rc || dir == NULL)
201                 GOTO(out, rc);
202
203         if (dentry->d_inode == NULL) {
204                 /* lookup */
205                 struct dentry    *save = dentry;
206                 struct it_cb_data icbd = {
207                         .icbd_parent = dir,
208                         .icbd_childp = &dentry
209                 };
210
211                 rc = lookup_it_finish(req, DLM_REPLY_REC_OFF, it, &icbd);
212                 if (!rc) {
213                         /* 
214                          * Here dentry->d_inode might be NULL,
215                          * because the entry may have been removed before
216                          * we start doing stat ahead.
217                          */
218                         if (dentry != save)
219                                 dput(save);
220                         ll_lookup_finish_locks(it, dentry);
221                 }
222         } else {
223                 /* revalidate */
224                 struct mds_body *body;
225
226                 body = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF,
227                                       sizeof(*body));
228                 if (memcmp(&minfo->mi_data.fid2, &body->fid1,
229                            sizeof(body->fid1))) {
230                         ll_unhash_aliases(dentry->d_inode);
231                         GOTO(out, rc = -EAGAIN);
232                 }
233
234                 rc = revalidate_it_finish(req, DLM_REPLY_REC_OFF, it, dentry);
235                 if (rc) {
236                         ll_unhash_aliases(dentry->d_inode);
237                         GOTO(out, rc);
238                 }
239
240                 spin_lock(&dcache_lock);
241                 lock_dentry(dentry);
242                 __d_drop(dentry);
243 #ifdef DCACHE_LUSTRE_INVALID
244                 dentry->d_flags &= ~DCACHE_LUSTRE_INVALID;
245 #endif
246                 unlock_dentry(dentry);
247                 d_rehash_cond(dentry, 0);
248                 spin_unlock(&dcache_lock);
249
250                 ll_lookup_finish_locks(it, dentry);
251
252         }
253         EXIT;
254 out:
255         spin_lock(&lli->lli_lock);
256         sai = lli->lli_sai;
257         if (sai) {
258                 lli->lli_sai->sai_replied++;
259                 ll_sai_entry_set(lli->lli_sai, minfo->mi_cbdata,
260                                  SA_ENTRY_STATED);
261                 cfs_waitq_signal(&lli->lli_sai->sai_thread.t_ctl_waitq);
262         }
263         spin_unlock(&lli->lli_lock);
264         ll_intent_release(it);
265         OBD_FREE_PTR(minfo);
266
267         dput(dentry);
268         return rc;
269 }
270
271 static void sa_args_fini(struct md_enqueue_info *minfo,
272                          struct ldlm_enqueue_info *einfo)
273 {
274         LASSERT(minfo && einfo);
275         OBD_FREE_PTR(minfo);
276         OBD_FREE_PTR(einfo);
277 }
278
279 static int sa_args_prep(struct inode *dir, struct dentry *dentry,
280                         struct md_enqueue_info **pmi,
281                         struct ldlm_enqueue_info **pei)
282 {
283         struct ll_inode_info     *lli = ll_i2info(dir);
284         struct md_enqueue_info   *minfo;
285         struct ldlm_enqueue_info *einfo;
286
287         OBD_ALLOC_PTR(einfo);
288         if (einfo == NULL)
289                 return -ENOMEM;
290
291         OBD_ALLOC_PTR(minfo);
292         if (minfo == NULL) {
293                 OBD_FREE_PTR(einfo);
294                 return -ENOMEM;
295         }
296
297         minfo->mi_exp = ll_i2mdcexp(dir);
298         minfo->mi_it.it_op = IT_GETATTR;
299         minfo->mi_dentry = dentry;
300         minfo->mi_cb = ll_statahead_interpret;
301         minfo->mi_cbdata = lli->lli_sai->sai_sent;
302
303         einfo->ei_type   = LDLM_IBITS;
304         einfo->ei_mode   = it_to_lock_mode(&minfo->mi_it);
305         einfo->ei_cb_bl  = ll_mdc_blocking_ast;
306         einfo->ei_cb_cp  = ldlm_completion_ast;
307         einfo->ei_cb_gl  = NULL;
308         einfo->ei_cbdata = NULL;
309
310         *pmi = minfo;
311         *pei = einfo;
312
313         return 0;
314 }
315
316 /* similar to ll_lookup_it(). */
317 static int do_sa_lookup(struct inode *dir, struct dentry *dentry)
318 {
319         struct md_enqueue_info   *minfo;
320         struct ldlm_enqueue_info *einfo;
321         int                       rc;                
322         ENTRY;
323
324         rc = sa_args_prep(dir, dentry, &minfo, &einfo);
325         if (rc)
326                 RETURN(rc);
327
328         rc = ll_prepare_mdc_op_data(&minfo->mi_data, dir, NULL,
329                                     dentry->d_name.name, dentry->d_name.len, 0,
330                                     NULL);
331         if (rc == 0)
332                 rc = mdc_intent_getattr_async(minfo->mi_exp, minfo, einfo);
333
334         if (rc)
335                 sa_args_fini(minfo, einfo);
336
337         RETURN(rc);
338 }
339
340 /* similar to ll_revalidate_it().
341  * return 1: dentry valid.
342  *        0: will send stat-ahead request.
343  *        -errno: prepare stat-ahead request failed. */
344 static int do_sa_revalidate(struct dentry *dentry)
345 {
346         struct inode             *inode = dentry->d_inode;
347         struct ll_inode_info     *lli = ll_i2info(dentry->d_parent->d_inode);
348         struct ll_fid             fid;
349         struct lookup_intent      it = { .it_op = IT_GETATTR };
350         struct md_enqueue_info   *minfo;
351         struct ldlm_enqueue_info *einfo;
352         int rc;
353         ENTRY;
354
355         if (inode == NULL)
356                 RETURN(1);
357
358         if (d_mountpoint(dentry))
359                 RETURN(1);
360
361         ll_inode2fid(&fid, inode);
362
363         rc = mdc_revalidate_lock(ll_i2mdcexp(inode), &it, &fid);
364         if (rc == 1) {
365                 ll_intent_release(&it);
366                 lli->lli_sai->sai_cached++;
367                 cfs_waitq_signal(&lli->lli_sai->sai_thread.t_ctl_waitq);
368                 RETURN(1);
369         }
370
371         rc = sa_args_prep(dentry->d_parent->d_inode, dentry, &minfo, &einfo);
372         if (rc)
373                 RETURN(rc);
374
375         rc = ll_prepare_mdc_op_data(&minfo->mi_data, dentry->d_parent->d_inode,
376                                     inode, dentry->d_name.name,
377                                     dentry->d_name.len, 0, NULL);
378         if (rc == 0)
379                 rc = mdc_intent_getattr_async(minfo->mi_exp, minfo, einfo);
380
381         if (rc)
382                 sa_args_fini(minfo, einfo);
383
384         RETURN(rc);
385 }
386
387 /* copied from kernel */
388 static inline void name2qstr(struct qstr *this, const char *name, int namelen)
389 {
390         unsigned long        hash;
391         const unsigned char *p = (const unsigned char *)name;
392         int                  len;
393         unsigned int         c;
394
395         hash = init_name_hash();
396         for (len = 0; len < namelen; len++, p++) {
397                 c = *p;
398                 hash = partial_name_hash(c, hash);
399         }
400         this->name = name;
401         this->len  = namelen;
402         this->hash = end_name_hash(hash);
403 }
404
405 static int ll_statahead_one(struct dentry *parent, ext2_dirent *de)
406 {
407         struct inode           *dir = parent->d_inode;
408         struct ll_inode_info   *lli = ll_i2info(dir);
409         struct qstr             name;
410         struct dentry          *dentry;
411         struct ll_sai_entry    *se;
412         int                     rc;
413         ENTRY;
414
415         name2qstr(&name, de->name, de->name_len);
416
417         se = ll_sai_entry_get(lli->lli_sai, lli->lli_sai->sai_sent,
418                               SA_ENTRY_UNSTATED);
419
420 #ifdef DCACHE_LUSTRE_INVALID
421         if (parent->d_flags & DCACHE_LUSTRE_INVALID) {
422 #else
423         if (d_unhashed(parent)) {
424 #endif
425                 CDEBUG(D_READA, "parent dentry@%p %.*s is "
426                        "invalid, skip statahead\n",
427                        parent, parent->d_name.len, parent->d_name.name);
428                 GOTO(out, rc = -EINVAL);
429         }
430
431         dentry = d_lookup(parent, &name);
432         if (!dentry) {
433                 struct dentry *dentry = d_alloc(parent, &name);
434
435                 rc = -ENOMEM;
436                 if (dentry) {
437                         rc = do_sa_lookup(dir, dentry);
438                         if (rc)
439                                 dput(dentry);
440                 }
441                 GOTO(out, rc);
442         }
443
444         rc = do_sa_revalidate(dentry);
445         if (rc)
446                 dput(dentry);
447         GOTO(out, rc);
448 out:
449         if (rc) {
450                 CDEBUG(D_READA, "set sai entry %p index %d stat %d, rc %d\n",
451                        se, se->se_index, se->se_stat, rc);
452                 se->se_stat = rc;
453                 cfs_waitq_signal(&lli->lli_sai->sai_thread.t_ctl_waitq);
454         }
455         lli->lli_sai->sai_sent++;
456         return rc;
457 }
458                 
459 static inline int sa_check_stop(struct ll_statahead_info *sai)
460 {
461         return !!(sai->sai_thread.t_flags & SVC_STOPPING);
462 }
463
464 static inline int sa_not_full(struct ll_statahead_info *sai)
465 {
466         return sai->sai_sent - sai->sai_miss - sai->sai_hit < sai->sai_max;
467 }
468
469 struct ll_sa_thread_args {
470         struct dentry   *sta_parent;
471         pid_t            sta_pid;
472 };
473
474 static int ll_statahead_thread(void *arg)
475 {
476         struct ll_sa_thread_args *sta = arg;
477         struct dentry            *parent = dget(sta->sta_parent);
478         struct inode             *dir = parent->d_inode;
479         struct ll_inode_info     *lli = ll_i2info(dir);
480         struct ll_sb_info        *sbi = ll_i2sbi(dir);
481         struct ll_statahead_info *sai = ll_sai_get(lli->lli_sai);
482         struct ptlrpc_thread     *thread = &sai->sai_thread;
483         struct l_wait_info        lwi = { 0 };
484         unsigned long             index = 0;
485         __u64                     offset = 0;
486         int                       skip = 0;
487         int                       rc = 0;
488         char                      name[16] = "";
489         ENTRY;
490
491         sbi->ll_sa_total++;
492
493         snprintf(name, 15, "ll_sa_%u", sta->sta_pid);
494         cfs_daemonize(name);
495         thread->t_flags = SVC_RUNNING;
496         cfs_waitq_signal(&thread->t_ctl_waitq);
497         CDEBUG(D_READA, "start doing statahead for %s\n", parent->d_name.name);
498
499         if (sai->sai_ls_all)
500                 CDEBUG(D_READA, "do statahead for hidden files\n");
501
502         while (1) {
503                 unsigned long npages = dir_pages(dir);
504
505                 /* hit ratio < 80% */
506                 if ((sai->sai_hit < 4 * sai->sai_miss && sai->sai_hit > 7) ||
507                      (sai->sai_consecutive_miss > 8)) {
508                         sbi->ll_sa_wrong++;
509                         CDEBUG(D_READA, "statahead for dir %.*s hit ratio too "
510                                "low: hit/miss %u/%u, sent/replied %u/%u, "
511                                "cached %u\n",
512                                parent->d_name.len, parent->d_name.name,
513                                sai->sai_hit, sai->sai_miss, sai->sai_sent,
514                                sai->sai_replied, sai->sai_cached);
515                         break;
516                 }
517
518                 /* reach the end of dir */
519                 if (index == npages) {
520                         CDEBUG(D_READA, "reach end, index/npages %lu/%lu\n",
521                                index, npages);
522                         break;
523                 }
524
525                 l_wait_event(thread->t_ctl_waitq,
526                              sa_check_stop(sai) || sa_not_full(sai),
527                              &lwi);
528
529                 if (sa_check_stop(sai))
530                         break;
531
532                 for (; index < npages; index++, offset = 0) {
533                         char *kaddr, *limit;
534                         ext2_dirent *de;
535                         struct page *page;
536
537                         CDEBUG(D_EXT2,"read %lu of dir %lu/%u page %lu"
538                                "/%lu size %llu\n",
539                                CFS_PAGE_SIZE, dir->i_ino, dir->i_generation,
540                                index, npages, dir->i_size);
541
542                         page = ll_get_dir_page(dir, index);
543                         npages = dir_pages(dir);
544
545                         if (IS_ERR(page)) {
546                                 rc = PTR_ERR(page);
547                                 CERROR("error reading dir %lu/%u page %lu: "
548                                        "rc %d\n",
549                                        dir->i_ino, dir->i_generation, index,
550                                        rc);
551                                 GOTO(out, rc);
552                         }
553
554                         kaddr = page_address(page);
555                         de = (ext2_dirent *)(kaddr + offset);
556                         limit = kaddr + CFS_PAGE_SIZE - EXT2_DIR_REC_LEN(1);
557                         for (; (char*)de <= limit && sa_not_full(sai);
558                              de = ext2_next_entry(de)) {
559                                 if (!de->inode)
560                                         continue;
561
562                                 /* don't stat-ahead ".", ".." */
563                                 if (skip < 2) {
564                                         skip++;
565                                         continue;
566                                 }
567
568                                 /* don't stat-ahead for hidden files */
569                                 if (de->name[0] == '.' && !sai->sai_ls_all)
570                                         continue;
571
572                                 /* don't stat-ahead for the first de */
573                                 if (skip < 3) {
574                                         skip++;
575                                         continue;
576                                 }
577
578                                 rc = ll_statahead_one(parent, de);
579                                 if (rc < 0) {
580                                         ext2_put_page(page);
581                                         GOTO(out, rc);
582                                 }
583                         }
584                         offset = (char *)de - kaddr;
585                         ext2_put_page(page);
586
587                         if ((char *)de <= limit)
588                                 /* !sa_not_full() */
589                                 break;
590                 }
591         }
592         EXIT;
593 out:
594         spin_lock(&lli->lli_lock);
595         thread->t_flags = SVC_STOPPED;
596         cfs_waitq_signal(&thread->t_ctl_waitq);
597         lli->lli_opendir_pid = 0; /* avoid statahead again */
598         spin_unlock(&lli->lli_lock);
599
600         ll_sai_put(sai);
601         dput(parent);
602         CDEBUG(D_READA, "stopped statahead thread, pid %d for %s\n",
603                current->pid, parent->d_name.name);
604         return 0;
605 }
606
607 /* called in ll_file_release */
608 void ll_stop_statahead(struct inode *inode)
609 {
610         struct ll_inode_info *lli = ll_i2info(inode);
611         struct ptlrpc_thread *thread;
612
613         spin_lock(&lli->lli_lock);
614         /* don't check pid here. upon fork, if parent closedir before child,
615          * child will not have chance to stop this thread. */
616         lli->lli_opendir_pid = 0;
617
618         if (lli->lli_sai && (lli->lli_sai->sai_thread.t_flags & SVC_RUNNING)) {
619                 struct l_wait_info lwi = { 0 };
620                 ll_sai_get(lli->lli_sai);
621                 thread = &lli->lli_sai->sai_thread;
622                 thread->t_flags = SVC_STOPPING;
623                 cfs_waitq_signal(&thread->t_ctl_waitq);
624                 spin_unlock(&lli->lli_lock);
625
626                 CDEBUG(D_READA, "stopping statahead thread, pid %d\n",
627                        current->pid);
628                 l_wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED,
629                              &lwi);
630                 ll_sai_put(lli->lli_sai);
631
632                 return;
633         }
634         spin_unlock(&lli->lli_lock);
635 }
636
637 enum {
638         LS_NONE_FIRST_DE = 0,   /* not first dirent, or is "." */
639         LS_FIRST_DE,            /* the first non-hidden dirent */
640         LS_FIRST_DOT_DE         /* the first hidden dirent, that is ".xxx" */
641 };
642
643 static int is_first_dirent(struct inode *dir, struct dentry *dentry)
644 {
645         struct qstr   *d_name = &dentry->d_name;
646         unsigned long  npages = dir_pages(dir);
647         struct page   *page;
648         ext2_dirent   *de;
649         unsigned long  index;
650         __u64          offset = 0;
651         char          *kaddr, *limit;
652         int            dot_de = 1; /* dirent is dotfile till now */
653         int            rc = LS_NONE_FIRST_DE;
654         ENTRY;
655
656         page = ll_get_dir_page(dir, 0);
657         if (IS_ERR(page)) {
658                 CERROR("error reading dir %lu/%u page 0: rc %ld\n",
659                        dir->i_ino, dir->i_generation, PTR_ERR(page));
660                 RETURN(LS_NONE_FIRST_DE);
661         }
662
663         kaddr = page_address(page);
664         de = (ext2_dirent *)kaddr;
665         if (!(de->name_len == 1 && strncmp(de->name, ".", 1) == 0))
666                 CWARN("Maybe got bad on-disk dir:%lu\n", dir->i_ino);
667         de = ext2_next_entry(de); /* skip ".", or ingore bad entry */
668         if (!(de->name_len == 2 && strncmp(de->name, "..", 2) == 0))
669                 CWARN("Maybe got bad on-disk dir:%lu\n", dir->i_ino);
670         de = ext2_next_entry(de); /* skip "..", or ingore bad entry */
671
672         offset = (char *)de - kaddr;
673
674         for (index = 0; index < npages; offset = 0) {
675                 de = (ext2_dirent *)(kaddr + offset);
676                 limit = kaddr + CFS_PAGE_SIZE - EXT2_DIR_REC_LEN(1);
677                 for (; (char*)de <= limit; de = ext2_next_entry(de)) {
678                         if (!de->inode)
679                                 continue;
680
681                         if (de->name[0] != '.')
682                                 dot_de = 0;
683
684                         if (dot_de && d_name->name[0] != '.') {
685                                 CDEBUG(D_READA, "%.*s skip hidden file %.*s\n",
686                                        d_name->len, d_name->name,
687                                        de->name_len, de->name);
688                                 continue;
689                         }
690
691                         if (d_name->len == de->name_len &&
692                             !strncmp(d_name->name, de->name, d_name->len))
693                                 rc = LS_FIRST_DE + dot_de;
694                         else
695                                 rc = LS_NONE_FIRST_DE;
696                         GOTO(out, rc);
697                 }
698
699                 if (++index >= npages)
700                         break;
701
702                 ext2_put_page(page);
703
704                 page = ll_get_dir_page(dir, index);
705                 if (IS_ERR(page)) {
706                         CERROR("error reading dir %lu/%u page %lu: rc %ld\n",
707                                dir->i_ino, dir->i_generation, index,
708                                PTR_ERR(page));
709                         RETURN(LS_NONE_FIRST_DE);
710                 }
711                 kaddr = page_address(page);
712         }
713         CERROR("%.*s not found in dir %.*s!\n", d_name->len, d_name->name,
714                dentry->d_parent->d_name.len, dentry->d_parent->d_name.name);
715         EXIT;
716 out:
717         ext2_put_page(page);
718         return rc;
719 }
720
721 /* start stat-ahead thread if this is the first dir entry, otherwise if a thread
722  * is started already, wait until thread is ahead of me.
723  * Return value: 
724  *    0 -- miss,
725  *    1 -- hit,
726  *    -EEXIST -- stat ahead thread started, and this is the first try.
727  *    other negative value -- error.
728  */
729 int ll_statahead_enter(struct inode *dir, struct dentry **dentryp, int lookup)
730 {
731         struct ll_sb_info        *sbi = ll_i2sbi(dir);
732         struct ll_inode_info     *lli = ll_i2info(dir);
733         struct ll_statahead_info *sai;
734         struct ll_sa_thread_args  sta;
735         struct l_wait_info        lwi = { 0 };
736         int                       rc;
737         ENTRY;
738
739         if (sbi->ll_sa_max == 0)
740                 RETURN(-ENOTSUPP);
741
742         /* not the same process, don't statahead */
743         if (lli->lli_opendir_pid != current->pid)
744                 RETURN(-EBADF);
745
746         spin_lock(&lli->lli_lock);
747         if (lli->lli_sai) {
748                 sai = ll_sai_get(lli->lli_sai);
749                 spin_unlock(&lli->lli_lock);
750
751                 if (ll_sai_entry_stated(sai)) {
752                         sbi->ll_sa_cached++;
753                 } else {
754                         struct l_wait_info lwi = { 0 };
755
756                         sbi->ll_sa_blocked++;
757                         /* thread started already, avoid double-stat */
758                         l_wait_event(sai->sai_thread.t_ctl_waitq,
759                                      ll_sai_entry_stated(sai) ||
760                                      sai->sai_thread.t_flags & SVC_STOPPED,
761                                      &lwi);
762                 }
763
764                 ll_sai_put(sai);
765
766                 if (lookup) {
767                         struct dentry *result;
768
769                         result = d_lookup((*dentryp)->d_parent,
770                                           &(*dentryp)->d_name);
771                         if (result) {
772                                 LASSERT(result != *dentryp);
773                                 dput(*dentryp);
774                                 *dentryp = result;
775                         }
776                         RETURN(result != NULL);
777                 }
778                 /* do nothing for revalidate */
779                 RETURN(0);
780         }
781         spin_unlock(&lli->lli_lock);
782
783         rc = is_first_dirent(dir, *dentryp);
784         if (!rc) {
785                 /* optimization: don't statahead for this pid any longer */
786                 spin_lock(&lli->lli_lock);
787                 if (lli->lli_sai == NULL)
788                         lli->lli_opendir_pid = 0;
789                 spin_unlock(&lli->lli_lock);
790                 RETURN(-EBADF);
791         }
792
793         spin_lock(&lli->lli_lock);
794         if (lli->lli_sai == NULL) {
795                 lli->lli_sai = ll_sai_alloc();
796                 if (lli->lli_sai == NULL) {
797                         spin_unlock(&lli->lli_lock);
798                         RETURN(-ENOMEM);
799                 }
800         } else {
801                 /* sai is already there */
802                 spin_unlock(&lli->lli_lock);
803                 RETURN(-EBUSY);
804         }
805         spin_unlock(&lli->lli_lock);
806         
807         sai = lli->lli_sai;
808         sai->sai_inode = igrab(dir);
809         sai->sai_ls_all = (rc == LS_FIRST_DOT_DE);
810
811         sta.sta_parent = (*dentryp)->d_parent;
812         sta.sta_pid    = current->pid;
813         rc = kernel_thread(ll_statahead_thread, &sta, 0);
814         if (rc < 0) {
815                 CERROR("can't start ll_sa thread, rc: %d\n", rc);
816                 ll_sai_put(sai);
817                 RETURN(rc);
818         }
819
820         l_wait_event(sai->sai_thread.t_ctl_waitq, 
821                      sai->sai_thread.t_flags & (SVC_RUNNING | SVC_STOPPED),
822                      &lwi);
823         ll_sai_put(sai);
824
825         /* we don't stat-ahead for the first dirent since we are already in
826          * lookup, and -EEXIST also indicates that this is the first dirent.
827          */
828         RETURN(-EEXIST);
829 }
830
831 /* update hit/miss count */
832 void ll_statahead_exit(struct dentry *dentry, int result)
833 {
834         struct ll_inode_info *lli = ll_i2info(dentry->d_parent->d_inode);
835         struct ll_sb_info    *sbi = ll_i2sbi(dentry->d_parent->d_inode);
836
837         if (lli->lli_opendir_pid != current->pid)
838                 return;
839
840         spin_lock(&lli->lli_lock);
841         if (lli->lli_sai) {
842                 struct ll_statahead_info *sai = lli->lli_sai;
843
844                 ll_sai_entry_put(sai);
845                 if (result == 1) {
846                         sai->sai_hit++;
847                         sai->sai_consecutive_miss = 0;
848                         sai->sai_max = min(2 * sai->sai_max, sbi->ll_sa_max);
849                         CDEBUG(D_READA, "statahead %.*s hit(hit/miss %u/%u)\n",
850                                dentry->d_name.len, dentry->d_name.name,
851                                sai->sai_hit, sai->sai_miss);
852                 } else {
853                         sai->sai_miss++;
854                         sai->sai_consecutive_miss++;
855                         /* upon miss, it's always because some dentry is added
856                          * by statahead thread, and at the mean time `ls`
857                          * processs finds this dentry, but the d_op for this
858                          * dentry is NULL, then revalidate is not done, and
859                          * ll_statahead_exit() not called for this dentry,
860                          * so statahead thread should be behind of `ls` process,
861                          * put one entry to go ahead.
862                          */
863                         CDEBUG(D_READA, "statahead %.*s miss(hit/miss %u/%u)\n",
864                                dentry->d_name.len, dentry->d_name.name,
865                                sai->sai_hit, sai->sai_miss);
866                         ll_sai_entry_put(sai);
867                 }
868                 cfs_waitq_signal(&sai->sai_thread.t_ctl_waitq);
869         }
870         spin_unlock(&lli->lli_lock);
871 }