Whamcloud - gitweb
67af5aaf2f0aee057ae912a54b52a3d563072db1
[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         init_waitqueue_head(&sai->sai_thread.t_ctl_waitq);
58         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                 LASSERT(sai->sai_thread.t_flags & SVC_STOPPED);
81                 list_for_each_entry_safe(entry, next, &sai->sai_entries,
82                                          se_list) {
83                         list_del(&entry->se_list);
84                         OBD_FREE_PTR(entry);
85                 }
86                 OBD_FREE_PTR(sai);
87                 lli->lli_sai = NULL;
88                 spin_unlock(&lli->lli_lock);
89                 iput(inode);
90         }
91         EXIT;
92 }
93
94 static struct ll_sai_entry *ll_sai_entry_get(struct ll_statahead_info *sai,
95                                              int index, int stat)
96 {
97         struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
98         struct ll_sb_info    *sbi = ll_i2sbi(sai->sai_inode);
99         struct ll_sai_entry  *entry;
100         ENTRY;
101
102         OBD_ALLOC_PTR(entry);
103         if (entry == NULL)
104                 RETURN(NULL);
105         
106         CDEBUG(D_READA, "alloc sai entry %p index %d, stat %d\n",
107                entry, index, stat);
108         entry->se_index = index;
109         entry->se_stat  = stat;
110
111         spin_lock(&lli->lli_lock);
112         list_add_tail(&entry->se_list, &sai->sai_entries);
113         sai->sai_entries_nr++;
114         sbi->ll_sa_count = sai->sai_entries_nr;
115         spin_unlock(&lli->lli_lock);
116
117         LASSERT(sai->sai_entries_nr <= sbi->ll_sa_max);
118         RETURN(entry);
119 }
120
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)
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                         LASSERT(dentry->d_inode);
214                         if (dentry != save)
215                                 dput(save);
216                         ll_lookup_finish_locks(it, dentry);
217                 }
218         } else {
219                 /* revalidate */
220                 struct mds_body *body;
221
222                 body = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF,
223                                       sizeof(*body));
224                 if (memcmp(&minfo->mi_data.fid2, &body->fid1,
225                            sizeof(body->fid1))) {
226                         ll_unhash_aliases(dentry->d_inode);
227                         GOTO(out, rc = -EAGAIN);
228                 }
229
230                 rc = revalidate_it_finish(req, DLM_REPLY_REC_OFF, it, dentry);
231                 if (rc) {
232                         ll_unhash_aliases(dentry->d_inode);
233                         GOTO(out, rc);
234                 }
235
236                 spin_lock(&dcache_lock);
237                 lock_dentry(dentry);
238                 __d_drop(dentry);
239 #ifdef DCACHE_LUSTRE_INVALID
240                 dentry->d_flags &= ~DCACHE_LUSTRE_INVALID;
241 #endif
242                 unlock_dentry(dentry);
243                 __d_rehash(dentry, 0);
244                 spin_unlock(&dcache_lock);
245
246                 ll_lookup_finish_locks(it, dentry);
247
248         }
249         EXIT;
250 out:
251         spin_lock(&lli->lli_lock);
252         sai = lli->lli_sai;
253         if (sai) {
254                 lli->lli_sai->sai_replied++;
255                 ll_sai_entry_set(lli->lli_sai, (int)minfo->mi_cbdata,
256                                  SA_ENTRY_STATED);
257                 wake_up(&lli->lli_sai->sai_thread.t_ctl_waitq);
258         }
259         spin_unlock(&lli->lli_lock);
260         ll_intent_release(it);
261         OBD_FREE_PTR(minfo);
262
263         dput(dentry);
264         return rc;
265 }
266
267 static void sa_args_fini(struct md_enqueue_info *minfo,
268                          struct ldlm_enqueue_info *einfo)
269 {
270         LASSERT(minfo && einfo);
271         OBD_FREE_PTR(minfo);
272         OBD_FREE_PTR(einfo);
273 }
274
275 static int sa_args_prep(struct inode *dir, struct dentry *dentry,
276                         struct md_enqueue_info **pmi,
277                         struct ldlm_enqueue_info **pei)
278 {
279         struct ll_inode_info     *lli = ll_i2info(dir);
280         struct md_enqueue_info   *minfo;
281         struct ldlm_enqueue_info *einfo;
282
283         OBD_ALLOC_PTR(einfo);
284         if (einfo == NULL)
285                 return -ENOMEM;
286
287         OBD_ALLOC_PTR(minfo);
288         if (minfo == NULL) {
289                 OBD_FREE_PTR(einfo);
290                 return -ENOMEM;
291         }
292
293         minfo->mi_exp = ll_i2mdcexp(dir);
294         minfo->mi_it.it_op = IT_GETATTR;
295         minfo->mi_dentry = dentry;
296         minfo->mi_cb = ll_statahead_interpret;
297         minfo->mi_cbdata = (void *)lli->lli_sai->sai_sent;
298
299         einfo->ei_type   = LDLM_IBITS;
300         einfo->ei_mode   = it_to_lock_mode(&minfo->mi_it);
301         einfo->ei_cb_bl  = ll_mdc_blocking_ast;
302         einfo->ei_cb_cp  = ldlm_completion_ast;
303         einfo->ei_cb_gl  = NULL;
304         einfo->ei_cbdata = NULL;
305
306         *pmi = minfo;
307         *pei = einfo;
308
309         return 0;
310 }
311
312 /* similar to ll_lookup_it(). */
313 static int do_sa_lookup(struct inode *dir, struct dentry *dentry)
314 {
315         struct md_enqueue_info   *minfo;
316         struct ldlm_enqueue_info *einfo;
317         int                       rc;                
318         ENTRY;
319
320         rc = sa_args_prep(dir, dentry, &minfo, &einfo);
321         if (rc)
322                 RETURN(rc);
323
324         rc = ll_prepare_mdc_op_data(&minfo->mi_data, dir, NULL,
325                                     dentry->d_name.name, dentry->d_name.len, 0,
326                                     NULL);
327         if (rc == 0)
328                 rc = mdc_intent_getattr_async(minfo->mi_exp, minfo, einfo);
329
330         if (rc)
331                 sa_args_fini(minfo, einfo);
332
333         RETURN(rc);
334 }
335
336 /* similar to ll_revalidate_it().
337  * return 1: dentry valid.
338  *        0: will send stat-ahead request.
339  *        -errno: prepare stat-ahead request failed. */
340 static int do_sa_revalidate(struct dentry *dentry)
341 {
342         struct inode             *inode = dentry->d_inode;
343         struct ll_inode_info     *lli = ll_i2info(dentry->d_parent->d_inode);
344         struct ll_fid             fid;
345         struct lookup_intent      it = { .it_op = IT_GETATTR };
346         struct md_enqueue_info   *minfo;
347         struct ldlm_enqueue_info *einfo;
348         int rc;
349         ENTRY;
350
351         if (inode == NULL)
352                 RETURN(1);
353
354         if (d_mountpoint(dentry))
355                 RETURN(1);
356
357         ll_inode2fid(&fid, inode);
358
359         rc = mdc_revalidate_lock(ll_i2mdcexp(inode), &it, &fid);
360         if (rc == 1) {
361                 ll_intent_release(&it);
362                 lli->lli_sai->sai_cached++;
363                 wake_up(&lli->lli_sai->sai_thread.t_ctl_waitq);
364                 RETURN(1);
365         }
366
367         rc = sa_args_prep(dentry->d_parent->d_inode, dentry, &minfo, &einfo);
368         if (rc)
369                 RETURN(rc);
370
371         rc = ll_prepare_mdc_op_data(&minfo->mi_data, dentry->d_parent->d_inode,
372                                     inode, dentry->d_name.name,
373                                     dentry->d_name.len, 0, NULL);
374         if (rc == 0)
375                 rc = mdc_intent_getattr_async(minfo->mi_exp, minfo, einfo);
376
377         if (rc)
378                 sa_args_fini(minfo, einfo);
379
380         RETURN(rc);
381 }
382
383 /* copied from kernel */
384 static inline void name2qstr(struct qstr *this, const char *name, int namelen)
385 {
386         unsigned long        hash;
387         const unsigned char *p = (const unsigned char *)name;
388         int                  len;
389         unsigned int         c;
390
391         hash = init_name_hash();
392         for (len = 0; len < namelen; len++, p++) {
393                 c = *p;
394                 hash = partial_name_hash(c, hash);
395         }
396         this->name = name;
397         this->len  = namelen;
398         this->hash = end_name_hash(hash);
399 }
400
401 static int ll_statahead_one(struct dentry *parent, ext2_dirent *de)
402 {
403         struct inode           *dir = parent->d_inode;
404         struct ll_inode_info   *lli = ll_i2info(dir);
405         struct qstr             name;
406         struct dentry          *dentry;
407         struct ll_sai_entry    *se;
408         int                     rc;
409         ENTRY;
410
411         name2qstr(&name, de->name, de->name_len);
412
413         se = ll_sai_entry_get(lli->lli_sai, lli->lli_sai->sai_sent,
414                               SA_ENTRY_UNSTATED);
415
416 #ifdef DCACHE_LUSTRE_INVALID
417         if (parent->d_flags & DCACHE_LUSTRE_INVALID) {
418 #else
419         if (d_unhashed(parent)) {
420 #endif
421                 CDEBUG(D_READA, "parent dentry@%p %.*s is "
422                        "invalid, skip statahead\n",
423                        parent, parent->d_name.len, parent->d_name.name);
424                 GOTO(out, rc = -EINVAL);
425         }
426
427         dentry = d_lookup(parent, &name);
428         if (!dentry) {
429                 struct dentry *dentry = d_alloc(parent, &name);
430
431                 rc = -ENOMEM;
432                 if (dentry) {
433                         rc = do_sa_lookup(dir, dentry);
434                         if (rc)
435                                 dput(dentry);
436                 }
437                 GOTO(out, rc);
438         }
439
440         rc = do_sa_revalidate(dentry);
441         if (rc)
442                 dput(dentry);
443         GOTO(out, rc);
444 out:
445         if (rc) {
446                 CDEBUG(D_READA, "set sai entry %p index %d stat %d, rc %d\n",
447                        se, se->se_index, se->se_stat, rc);
448                 se->se_stat = rc;
449                 wake_up(&lli->lli_sai->sai_thread.t_ctl_waitq);
450         }
451         lli->lli_sai->sai_sent++;
452         return rc;
453 }
454                 
455 static inline int sa_check_stop(struct ll_statahead_info *sai)
456 {
457         return !!(sai->sai_thread.t_flags & SVC_STOPPING);
458 }
459
460 static inline int sa_not_full(struct ll_statahead_info *sai)
461 {
462         return sai->sai_sent - sai->sai_miss - sai->sai_hit < sai->sai_max;
463 }
464
465 struct ll_sa_thread_args {
466         struct dentry   *sta_parent;
467         pid_t            sta_pid;
468 };
469
470 static int ll_statahead_thread(void *arg)
471 {
472         struct ll_sa_thread_args *sta = arg;
473         struct dentry            *parent = dget(sta->sta_parent);
474         struct inode             *dir = parent->d_inode;
475         struct ll_inode_info     *lli = ll_i2info(dir);
476         struct ll_sb_info        *sbi = ll_i2sbi(dir);
477         struct ll_statahead_info *sai = ll_sai_get(lli->lli_sai);
478         struct ptlrpc_thread     *thread = &sai->sai_thread;
479         struct l_wait_info        lwi = { 0 };
480         unsigned long             index = 0;
481         __u64                     offset = 0;
482         int                       skip = 0;
483         int                       rc = 0;
484         char                      name[16] = "";
485         ENTRY;
486
487         sbi->ll_sa_total++;
488
489         snprintf(name, 15, "ll_sa_%u", sta->sta_pid);
490         cfs_daemonize(name);
491         thread->t_flags = SVC_RUNNING;
492         wake_up(&thread->t_ctl_waitq);
493         CDEBUG(D_READA, "start doing statahead for %s\n", parent->d_name.name);
494
495         if (sai->sai_ls_all)
496                 CDEBUG(D_READA, "do statahead for hidden files\n");
497
498         while (1) {
499                 unsigned long npages = dir_pages(dir);
500
501                 /* hit ratio < 80% */
502                 if ((sai->sai_hit < 4 * sai->sai_miss && sai->sai_hit > 7) ||
503                      (sai->sai_consecutive_miss > 8)) {
504                         sbi->ll_sa_wrong++;
505                         CDEBUG(D_READA, "statahead for dir %.*s hit ratio too "
506                                "low: hit/miss %u/%u, sent/replied %u/%u, "
507                                "cached %u\n",
508                                parent->d_name.len, parent->d_name.name,
509                                sai->sai_hit, sai->sai_miss, sai->sai_sent,
510                                sai->sai_replied, sai->sai_cached);
511                         break;
512                 }
513
514                 /* reach the end of dir */
515                 if (index == npages) {
516                         CDEBUG(D_READA, "reach end, index/npages %lu/%lu\n",
517                                index, npages);
518                         break;
519                 }
520
521                 l_wait_event(thread->t_ctl_waitq,
522                              sa_check_stop(sai) || sa_not_full(sai),
523                              &lwi);
524
525                 if (sa_check_stop(sai))
526                         break;
527
528                 for (; index < npages; index++, offset = 0) {
529                         char *kaddr, *limit;
530                         ext2_dirent *de;
531                         struct page *page;
532
533                         CDEBUG(D_EXT2,"read %lu of dir %lu/%u page %lu"
534                                "/%lu size %llu\n",
535                                CFS_PAGE_SIZE, dir->i_ino, dir->i_generation,
536                                index, npages, dir->i_size);
537
538                         page = ll_get_dir_page(dir, index);
539                         npages = dir_pages(dir);
540
541                         if (IS_ERR(page)) {
542                                 rc = PTR_ERR(page);
543                                 CERROR("error reading dir %lu/%u page %lu: "
544                                        "rc %d\n",
545                                        dir->i_ino, dir->i_generation, index,
546                                        rc);
547                                 GOTO(out, rc);
548                         }
549
550                         kaddr = page_address(page);
551                         de = (ext2_dirent *)(kaddr + offset);
552                         limit = kaddr + CFS_PAGE_SIZE - EXT2_DIR_REC_LEN(1);
553                         for (; (char*)de <= limit && sa_not_full(sai);
554                              de = ext2_next_entry(de)) {
555                                 if (!de->inode)
556                                         continue;
557
558                                 /* don't stat-ahead ".", ".." */
559                                 if (skip < 2) {
560                                         skip++;
561                                         continue;
562                                 }
563
564                                 /* don't stat-ahead for hidden files */
565                                 if (de->name[0] == '.' && !sai->sai_ls_all)
566                                         continue;
567
568                                 /* don't stat-ahead for the first de */
569                                 if (skip < 3) {
570                                         skip++;
571                                         continue;
572                                 }
573
574                                 rc = ll_statahead_one(parent, de);
575                                 if (rc < 0) {
576                                         ext2_put_page(page);
577                                         GOTO(out, rc);
578                                 }
579                         }
580                         offset = (char *)de - kaddr;
581                         ext2_put_page(page);
582
583                         if ((char *)de <= limit)
584                                 /* !sa_not_full() */
585                                 break;
586                 }
587         }
588         EXIT;
589 out:
590         thread->t_flags = SVC_STOPPED;
591         wake_up(&thread->t_ctl_waitq);
592         lli->lli_opendir_pid = 0; /* avoid statahead again */
593         ll_sai_put(sai);
594         dput(parent);
595         CDEBUG(D_READA, "stopped statahead thread, pid %d for %s\n",
596                current->pid, parent->d_name.name);
597         return 0;
598 }
599
600 /* called in ll_file_release */
601 void ll_stop_statahead(struct inode *inode)
602 {
603         struct ll_inode_info *lli = ll_i2info(inode);
604         struct ptlrpc_thread *thread;
605
606         /* don't check pid here. upon fork, if parent closedir before child,
607          * child will not have chance to stop this thread. */
608         lli->lli_opendir_pid = 0;
609
610         spin_lock(&lli->lli_lock);
611         if (lli->lli_sai) {
612                 ll_sai_get(lli->lli_sai);
613                 spin_unlock(&lli->lli_lock);
614
615                 CDEBUG(D_READA, "stopping statahead thread, pid %d\n",
616                        current->pid);
617                 thread = &lli->lli_sai->sai_thread;
618                 thread->t_flags = SVC_STOPPING;
619                 wake_up(&thread->t_ctl_waitq);
620                 wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED);
621                 ll_sai_put(lli->lli_sai);
622
623                 return;
624         }
625         spin_unlock(&lli->lli_lock);
626 }
627
628 enum {
629         LS_NONE_FIRST_DE = 0,   /* not first dirent, or is "." */
630         LS_FIRST_DE,            /* the first non-hidden dirent */
631         LS_FIRST_DOT_DE         /* the first hidden dirent, that is ".xxx" */
632 };
633
634 static int is_first_dirent(struct inode *dir, struct dentry *dentry)
635 {
636         struct qstr   *d_name = &dentry->d_name;
637         unsigned long  npages = dir_pages(dir);
638         struct page   *page;
639         ext2_dirent   *de;
640         unsigned long  index;
641         __u64          offset = 0;
642         char          *kaddr, *limit;
643         int            dot_de = 1; /* dirent is dotfile till now */
644         int            rc = LS_NONE_FIRST_DE;
645         ENTRY;
646
647         page = ll_get_dir_page(dir, 0);
648         if (IS_ERR(page)) {
649                 CERROR("error reading dir %lu/%u page 0: rc %ld\n",
650                        dir->i_ino, dir->i_generation, PTR_ERR(page));
651                 RETURN(LS_NONE_FIRST_DE);
652         }
653
654         kaddr = page_address(page);
655         de = (ext2_dirent *)kaddr;
656         if (!(de->name_len == 1 && strncmp(de->name, ".", 1) == 0))
657                 CWARN("Maybe got bad on-disk dir:%lu\n", dir->i_ino);
658         de = ext2_next_entry(de); /* skip ".", or ingore bad entry */
659         if (!(de->name_len == 2 && strncmp(de->name, "..", 2) == 0))
660                 CWARN("Maybe got bad on-disk dir:%lu\n", dir->i_ino);
661         de = ext2_next_entry(de); /* skip "..", or ingore bad entry */
662
663         offset = (char *)de - kaddr;
664
665         for (index = 0; index < npages; offset = 0) {
666                 de = (ext2_dirent *)(kaddr + offset);
667                 limit = kaddr + CFS_PAGE_SIZE - EXT2_DIR_REC_LEN(1);
668                 for (; (char*)de <= limit; de = ext2_next_entry(de)) {
669                         if (!de->inode)
670                                 continue;
671
672                         if (de->name[0] != '.')
673                                 dot_de = 0;
674
675                         if (dot_de && d_name->name[0] != '.') {
676                                 CDEBUG(D_READA, "%.*s skip hidden file %.*s\n",
677                                        d_name->len, d_name->name,
678                                        de->name_len, de->name);
679                                 continue;
680                         }
681
682                         if (d_name->len == de->name_len &&
683                             !strncmp(d_name->name, de->name, d_name->len))
684                                 rc = LS_FIRST_DE + dot_de;
685                         else
686                                 rc = LS_NONE_FIRST_DE;
687                         GOTO(out, rc);
688                 }
689
690                 if (++index >= npages)
691                         break;
692
693                 ext2_put_page(page);
694
695                 page = ll_get_dir_page(dir, index);
696                 if (IS_ERR(page)) {
697                         CERROR("error reading dir %lu/%u page %lu: rc %ld\n",
698                                dir->i_ino, dir->i_generation, index,
699                                PTR_ERR(page));
700                         RETURN(LS_NONE_FIRST_DE);
701                 }
702                 kaddr = page_address(page);
703         }
704         CERROR("%.*s not found in dir %.*s!\n", d_name->len, d_name->name,
705                dentry->d_parent->d_name.len, dentry->d_parent->d_name.name);
706         EXIT;
707 out:
708         ext2_put_page(page);
709         return rc;
710 }
711
712 /* start stat-ahead thread if this is the first dir entry, otherwise if a thread
713  * is started already, wait until thread is ahead of me.
714  * Return value: 
715  *    0 -- miss,
716  *    1 -- hit,
717  *    -EEXIST -- stat ahead thread started, and this is the first try.
718  *    other negative value -- error.
719  */
720 int ll_statahead_enter(struct inode *dir, struct dentry **dentryp, int lookup)
721 {
722         struct ll_sb_info        *sbi = ll_i2sbi(dir);
723         struct ll_inode_info     *lli = ll_i2info(dir);
724         struct ll_statahead_info *sai;
725         struct ll_sa_thread_args  sta;
726         int                       rc;
727         ENTRY;
728
729         if (sbi->ll_sa_max == 0)
730                 RETURN(-ENOTSUPP);
731
732         /* not the same process, don't statahead */
733         if (lli->lli_opendir_pid != current->pid)
734                 RETURN(-EBADF);
735
736         spin_lock(&lli->lli_lock);
737         if (lli->lli_sai) {
738                 sai = ll_sai_get(lli->lli_sai);
739                 spin_unlock(&lli->lli_lock);
740
741                 if (ll_sai_entry_stated(sai)) {
742                         sbi->ll_sa_cached++;
743                 } else {
744                         struct l_wait_info lwi = { 0 };
745
746                         sbi->ll_sa_blocked++;
747                         /* thread started already, avoid double-stat */
748                         l_wait_event(sai->sai_thread.t_ctl_waitq,
749                                      ll_sai_entry_stated(sai) ||
750                                      sai->sai_thread.t_flags & SVC_STOPPED,
751                                      &lwi);
752                 }
753
754                 ll_sai_put(sai);
755
756                 if (lookup) {
757                         struct dentry *result;
758
759                         result = d_lookup((*dentryp)->d_parent,
760                                           &(*dentryp)->d_name);
761                         if (result) {
762                                 LASSERT(result != *dentryp);
763                                 dput(*dentryp);
764                                 *dentryp = result;
765                         }
766                         RETURN(result != NULL);
767                 }
768                 /* do nothing for revalidate */
769                 RETURN(0);
770         }
771         spin_unlock(&lli->lli_lock);
772
773         rc = is_first_dirent(dir, *dentryp);
774         if (!rc) {
775                 /* optimization: don't statahead for this pid any longer */
776                 spin_lock(&lli->lli_lock);
777                 if (lli->lli_sai == NULL)
778                         lli->lli_opendir_pid = 0;
779                 spin_unlock(&lli->lli_lock);
780                 RETURN(-EBADF);
781         }
782
783         spin_lock(&lli->lli_lock);
784         if (lli->lli_sai == NULL) {
785                 lli->lli_sai = ll_sai_alloc();
786                 if (lli->lli_sai == NULL) {
787                         spin_unlock(&lli->lli_lock);
788                         RETURN(-ENOMEM);
789                 }
790         } else {
791                 /* sai is already there */
792                 spin_unlock(&lli->lli_lock);
793                 RETURN(-EBUSY);
794         }
795         spin_unlock(&lli->lli_lock);
796         
797         sai = lli->lli_sai;
798         sai->sai_inode = igrab(dir);
799         sai->sai_ls_all = (rc == LS_FIRST_DOT_DE);
800
801         sta.sta_parent = (*dentryp)->d_parent;
802         sta.sta_pid    = current->pid;
803         rc = kernel_thread(ll_statahead_thread, &sta, 0);
804         if (rc < 0) {
805                 CERROR("can't start ll_sa thread, rc: %d\n", rc);
806                 ll_sai_put(sai);
807                 RETURN(rc);
808         }
809
810         wait_event(sai->sai_thread.t_ctl_waitq, 
811                    sai->sai_thread.t_flags & (SVC_RUNNING | SVC_STOPPED));
812         ll_sai_put(sai);
813
814         /* we don't stat-ahead for the first dirent since we are already in
815          * lookup, and -EEXIST also indicates that this is the first dirent.
816          */
817         RETURN(-EEXIST);
818 }
819
820 /* update hit/miss count */
821 void ll_statahead_exit(struct dentry *dentry, int result)
822 {
823         struct ll_inode_info *lli = ll_i2info(dentry->d_parent->d_inode);
824         struct ll_sb_info    *sbi = ll_i2sbi(dentry->d_parent->d_inode);
825
826         if (lli->lli_opendir_pid != current->pid)
827                 return;
828
829         spin_lock(&lli->lli_lock);
830         if (lli->lli_sai) {
831                 struct ll_statahead_info *sai = lli->lli_sai;
832
833                 ll_sai_entry_put(sai);
834                 if (result == 1) {
835                         sai->sai_hit++;
836                         sai->sai_consecutive_miss = 0;
837                         sai->sai_max = min(2 * sai->sai_max, sbi->ll_sa_max);
838                         CDEBUG(D_READA, "statahead %.*s hit(hit/miss %u/%u)\n",
839                                dentry->d_name.len, dentry->d_name.name,
840                                sai->sai_hit, sai->sai_miss);
841                 } else {
842                         sai->sai_miss++;
843                         sai->sai_consecutive_miss++;
844                         /* upon miss, it's always because some dentry is added
845                          * by statahead thread, and at the mean time `ls`
846                          * processs finds this dentry, but the d_op for this
847                          * dentry is NULL, then revalidate is not done, and
848                          * ll_statahead_exit() not called for this dentry,
849                          * so statahead thread should be behind of `ls` process,
850                          * put one entry to go ahead.
851                          */
852                         CDEBUG(D_READA, "statahead %.*s miss(hit/miss %u/%u)\n",
853                                dentry->d_name.len, dentry->d_name.name,
854                                sai->sai_hit, sai->sai_miss);
855                         ll_sai_entry_put(sai);
856                 }
857                 wake_up(&sai->sai_thread.t_ctl_waitq);
858         }
859         spin_unlock(&lli->lli_lock);
860 }