Whamcloud - gitweb
b=20748
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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 struct ll_sai_entry {
53         struct list_head        se_list;
54         unsigned int            se_index;
55         int                     se_stat;
56         struct ptlrpc_request  *se_req;
57         struct md_enqueue_info *se_minfo;
58 };
59
60 enum {
61         SA_ENTRY_UNSTATED = 0,
62         SA_ENTRY_STATED
63 };
64
65 static unsigned int sai_generation = 0;
66 static spinlock_t sai_generation_lock = SPIN_LOCK_UNLOCKED;
67
68 /**
69  * Check whether first entry was stated already or not.
70  * No need to hold lli_lock, for:
71  * (1) it is me that remove entry from the list
72  * (2) the statahead thread only add new entry to the list
73  */
74 static int ll_sai_entry_stated(struct ll_statahead_info *sai)
75 {
76         struct ll_sai_entry  *entry;
77         int                   rc = 0;
78
79         if (!list_empty(&sai->sai_entries_stated)) {
80                 entry = list_entry(sai->sai_entries_stated.next,
81                                    struct ll_sai_entry, se_list);
82                 if (entry->se_index == sai->sai_index_next)
83                         rc = 1;
84         }
85         return rc;
86 }
87
88 static inline int sa_received_empty(struct ll_statahead_info *sai)
89 {
90         return list_empty(&sai->sai_entries_received);
91 }
92
93 static inline int sa_not_full(struct ll_statahead_info *sai)
94 {
95         return (sai->sai_index < sai->sai_hit + sai->sai_miss + sai->sai_max);
96 }
97
98 static inline int sa_is_running(struct ll_statahead_info *sai)
99 {
100         return !!(sai->sai_thread.t_flags & SVC_RUNNING);
101 }
102
103 static inline int sa_is_stopping(struct ll_statahead_info *sai)
104 {
105         return !!(sai->sai_thread.t_flags & SVC_STOPPING);
106 }
107
108 static inline int sa_is_stopped(struct ll_statahead_info *sai)
109 {
110         return !!(sai->sai_thread.t_flags & SVC_STOPPED);
111 }
112
113 /**
114  * (1) hit ratio less than 80%
115  * or
116  * (2) consecutive miss more than 8
117  */
118 static inline int sa_low_hit(struct ll_statahead_info *sai)
119 {
120         return ((sai->sai_hit > 7 && sai->sai_hit < 4 * sai->sai_miss) ||
121                 (sai->sai_consecutive_miss > 8));
122 }
123
124 /**
125  * process the deleted entry's member and free the entry.
126  * (1) release intent
127  * (2) free md_enqueue_info
128  * (3) drop dentry's ref count
129  * (4) release request's ref count
130  */
131 static void ll_sai_entry_cleanup(struct ll_sai_entry *entry, int free)
132 {
133         struct md_enqueue_info *minfo = entry->se_minfo;
134         struct ptlrpc_request  *req = entry->se_req;
135         ENTRY;
136
137         if (minfo) {
138                 entry->se_minfo = NULL;
139                 ll_intent_release(&minfo->mi_it);
140                 dput(minfo->mi_dentry);
141                 iput(minfo->mi_dir);
142                 OBD_FREE_PTR(minfo);
143         }
144         if (req) {
145                 entry->se_req = NULL;
146                 ptlrpc_req_finished(req);
147         }
148         if (free) {
149                 LASSERT(list_empty(&entry->se_list));
150                 OBD_FREE_PTR(entry);
151         }
152
153         EXIT;
154 }
155
156 static struct ll_statahead_info *ll_sai_alloc(void)
157 {
158         struct ll_statahead_info *sai;
159
160         OBD_ALLOC_PTR(sai);
161         if (!sai)
162                 return NULL;
163
164         spin_lock(&sai_generation_lock);
165         sai->sai_generation = ++sai_generation;
166         if (unlikely(sai_generation == 0))
167                 sai->sai_generation = ++sai_generation;
168         spin_unlock(&sai_generation_lock);
169         atomic_set(&sai->sai_refcount, 1);
170         sai->sai_max = LL_SA_RPC_MIN;
171         cfs_waitq_init(&sai->sai_waitq);
172         cfs_waitq_init(&sai->sai_thread.t_ctl_waitq);
173         CFS_INIT_LIST_HEAD(&sai->sai_entries_sent);
174         CFS_INIT_LIST_HEAD(&sai->sai_entries_received);
175         CFS_INIT_LIST_HEAD(&sai->sai_entries_stated);
176         return sai;
177 }
178
179 static inline
180 struct ll_statahead_info *ll_sai_get(struct ll_statahead_info *sai)
181 {
182         LASSERT(sai);
183         atomic_inc(&sai->sai_refcount);
184         return sai;
185 }
186
187 static void ll_sai_put(struct ll_statahead_info *sai)
188 {
189         struct inode         *inode = sai->sai_inode;
190         struct ll_inode_info *lli;
191         ENTRY;
192
193         LASSERT(inode != NULL);
194         lli = ll_i2info(inode);
195         LASSERT(lli->lli_sai == sai);
196
197         if (atomic_dec_and_test(&sai->sai_refcount)) {
198                 struct ll_sai_entry *entry, *next;
199
200                 spin_lock(&lli->lli_lock);
201                 if (unlikely(atomic_read(&sai->sai_refcount) > 0)) {
202                         /* It is race case, the interpret callback just hold
203                          * a reference count */
204                         spin_unlock(&lli->lli_lock);
205                         EXIT;
206                         return;
207                 }
208
209                 LASSERT(lli->lli_opendir_key == NULL);
210                 lli->lli_sai = NULL;
211                 lli->lli_opendir_pid = 0;
212                 spin_unlock(&lli->lli_lock);
213
214                 LASSERT(sa_is_stopped(sai));
215
216                 if (sai->sai_sent > sai->sai_replied)
217                         CDEBUG(D_READA,"statahead for dir "DFID" does not "
218                               "finish: [sent:%u] [replied:%u]\n",
219                               PFID(&lli->lli_fid),
220                               sai->sai_sent, sai->sai_replied);
221
222                 list_for_each_entry_safe(entry, next, &sai->sai_entries_sent,
223                                          se_list) {
224                         list_del_init(&entry->se_list);
225                         ll_sai_entry_cleanup(entry, 1);
226                 }
227                 list_for_each_entry_safe(entry, next, &sai->sai_entries_received,
228                                          se_list) {
229                         list_del_init(&entry->se_list);
230                         ll_sai_entry_cleanup(entry, 1);
231                 }
232                 list_for_each_entry_safe(entry, next, &sai->sai_entries_stated,
233                                          se_list) {
234                         list_del_init(&entry->se_list);
235                         ll_sai_entry_cleanup(entry, 1);
236                 }
237                 iput(inode);
238                 OBD_FREE_PTR(sai);
239         }
240         EXIT;
241 }
242
243 /**
244  * insert it into sai_entries_sent tail when init.
245  */
246 static struct ll_sai_entry *
247 ll_sai_entry_init(struct ll_statahead_info *sai, unsigned int index)
248 {
249         struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
250         struct ll_sai_entry  *entry;
251         ENTRY;
252
253         OBD_ALLOC_PTR(entry);
254         if (entry == NULL)
255                 RETURN(ERR_PTR(-ENOMEM));
256
257         CDEBUG(D_READA, "alloc sai entry %p index %u\n",
258                entry, index);
259         entry->se_index = index;
260         entry->se_stat  = SA_ENTRY_UNSTATED;
261
262         spin_lock(&lli->lli_lock);
263         list_add_tail(&entry->se_list, &sai->sai_entries_sent);
264         spin_unlock(&lli->lli_lock);
265
266         RETURN(entry);
267 }
268
269 /**
270  * delete it from sai_entries_stated head when fini, it need not
271  * to process entry's member.
272  */
273 static void ll_sai_entry_fini(struct ll_statahead_info *sai)
274 {
275         struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
276         struct ll_sai_entry  *entry;
277         ENTRY;
278
279         spin_lock(&lli->lli_lock);
280         sai->sai_index_next++;
281         if (likely(!list_empty(&sai->sai_entries_stated))) {
282                 entry = list_entry(sai->sai_entries_stated.next,
283                                    struct ll_sai_entry, se_list);
284                 if (entry->se_index < sai->sai_index_next) {
285                         list_del(&entry->se_list);
286                         OBD_FREE_PTR(entry);
287                 }
288         } else
289                 LASSERT(sa_is_stopped(sai));
290         spin_unlock(&lli->lli_lock);
291
292         EXIT;
293 }
294
295 /**
296  * inside lli_lock.
297  * \retval NULL : can not find the entry in sai_entries_sent with the index
298  * \retval entry: find the entry in sai_entries_sent with the index
299  */
300 static struct ll_sai_entry *
301 ll_sai_entry_set(struct ll_statahead_info *sai, unsigned int index, int stat,
302                  struct ptlrpc_request *req, struct md_enqueue_info *minfo)
303 {
304         struct ll_sai_entry *entry;
305         ENTRY;
306
307         if (!list_empty(&sai->sai_entries_sent)) {
308                 list_for_each_entry(entry, &sai->sai_entries_sent, se_list) {
309                         if (entry->se_index == index) {
310                                 entry->se_stat = stat;
311                                 entry->se_req = ptlrpc_request_addref(req);
312                                 entry->se_minfo = minfo;
313                                 RETURN(entry);
314                         } else if (entry->se_index > index)
315                                 RETURN(NULL);
316                 }
317         }
318         RETURN(NULL);
319 }
320
321 /**
322  * inside lli_lock.
323  * Move entry to sai_entries_received and
324  * insert it into sai_entries_received tail.
325  */
326 static inline void
327 ll_sai_entry_to_received(struct ll_statahead_info *sai, struct ll_sai_entry *entry)
328 {
329         if (!list_empty(&entry->se_list))
330                 list_del_init(&entry->se_list);
331         list_add_tail(&entry->se_list, &sai->sai_entries_received);
332 }
333
334 /**
335  * Move entry to sai_entries_stated and
336  * sort with the index.
337  */
338 static int
339 ll_sai_entry_to_stated(struct ll_statahead_info *sai, struct ll_sai_entry *entry)
340 {
341         struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
342         struct ll_sai_entry  *se;
343         ENTRY;
344
345         ll_sai_entry_cleanup(entry, 0);
346
347         spin_lock(&lli->lli_lock);
348         if (!list_empty(&entry->se_list))
349                 list_del_init(&entry->se_list);
350
351         if (unlikely(entry->se_index < sai->sai_index_next)) {
352                 spin_unlock(&lli->lli_lock);
353                 OBD_FREE_PTR(entry);
354                 RETURN(0);
355         }
356
357         list_for_each_entry_reverse(se, &sai->sai_entries_stated, se_list) {
358                 if (se->se_index < entry->se_index) {
359                         list_add(&entry->se_list, &se->se_list);
360                         spin_unlock(&lli->lli_lock);
361                         RETURN(1);
362                 }
363         }
364
365         /*
366          * I am the first entry.
367          */
368         list_add(&entry->se_list, &sai->sai_entries_stated);
369         spin_unlock(&lli->lli_lock);
370         RETURN(1);
371 }
372
373 /**
374  * finish lookup/revalidate.
375  */
376 static int do_statahead_interpret(struct ll_statahead_info *sai)
377 {
378         struct ll_inode_info   *lli = ll_i2info(sai->sai_inode);
379         struct ll_sai_entry    *entry;
380         struct ptlrpc_request  *req;
381         struct md_enqueue_info *minfo;
382         struct lookup_intent   *it;
383         struct dentry          *dentry;
384         int                     rc = 0;
385         struct mdt_body        *body;
386         ENTRY;
387
388         spin_lock(&lli->lli_lock);
389         LASSERT(!sa_received_empty(sai));
390         entry = list_entry(sai->sai_entries_received.next, struct ll_sai_entry,
391                            se_list);
392         list_del_init(&entry->se_list);
393         spin_unlock(&lli->lli_lock);
394
395         if (unlikely(entry->se_index < sai->sai_index_next)) {
396                 CWARN("Found stale entry: [index %u] [next %u]\n",
397                       entry->se_index, sai->sai_index_next);
398                 ll_sai_entry_cleanup(entry, 1);
399                 RETURN(0);
400         }
401
402         if (entry->se_stat != SA_ENTRY_STATED)
403                 GOTO(out, rc = entry->se_stat);
404
405         req = entry->se_req;
406         minfo = entry->se_minfo;
407         it = &minfo->mi_it;
408         dentry = minfo->mi_dentry;
409
410         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
411         if (body == NULL)
412                 GOTO(out, rc = -EFAULT);
413
414         if (dentry->d_inode == NULL) {
415                 /*
416                  * lookup.
417                  */
418                 struct dentry    *save = dentry;
419                 struct it_cb_data icbd = {
420                         .icbd_parent   = minfo->mi_dir,
421                         .icbd_childp   = &dentry
422                 };
423
424                 LASSERT(fid_is_zero(&minfo->mi_data.op_fid2));
425
426                 /*
427                  * XXX: No fid in reply, this is probaly cross-ref case.
428                  * SA can't handle it yet.
429                  */
430                 if (body->valid & OBD_MD_MDS)
431                         GOTO(out, rc = -EAGAIN);
432
433                 rc = ll_lookup_it_finish(req, it, &icbd);
434                 if (!rc)
435                         /*
436                          * Here dentry->d_inode might be NULL,
437                          * because the entry may have been removed before
438                          * we start doing stat ahead.
439                          */
440                         ll_lookup_finish_locks(it, dentry);
441
442                 if (dentry != save) {
443                         minfo->mi_dentry = dentry;
444                         dput(save);
445                 }
446         } else {
447                 /*
448                  * revalidate.
449                  */
450                 if (!lu_fid_eq(&minfo->mi_data.op_fid2, &body->fid1)) {
451                         ll_unhash_aliases(dentry->d_inode);
452                         GOTO(out, rc = -EAGAIN);
453                 }
454
455                 rc = ll_revalidate_it_finish(req, it, dentry);
456                 if (rc) {
457                         ll_unhash_aliases(dentry->d_inode);
458                         GOTO(out, rc);
459                 }
460
461                 spin_lock(&ll_lookup_lock);
462                 spin_lock(&dcache_lock);
463                 lock_dentry(dentry);
464                 __d_drop(dentry);
465                 dentry->d_flags &= ~DCACHE_LUSTRE_INVALID;
466                 unlock_dentry(dentry);
467                 d_rehash_cond(dentry, 0);
468                 spin_unlock(&dcache_lock);
469                 spin_unlock(&ll_lookup_lock);
470
471                 ll_lookup_finish_locks(it, dentry);
472         }
473         EXIT;
474
475 out:
476         if (likely(ll_sai_entry_to_stated(sai, entry)))
477                 cfs_waitq_signal(&sai->sai_waitq);
478         return rc;
479 }
480
481 static int ll_statahead_interpret(struct ptlrpc_request *req,
482                                   struct md_enqueue_info *minfo,
483                                   int rc)
484 {
485         struct lookup_intent     *it = &minfo->mi_it;
486         struct dentry            *dentry = minfo->mi_dentry;
487         struct inode             *dir = minfo->mi_dir;
488         struct ll_inode_info     *lli = ll_i2info(dir);
489         struct ll_statahead_info *sai;
490         struct ll_sai_entry      *entry;
491         ENTRY;
492
493         CDEBUG(D_READA, "interpret statahead %.*s rc %d\n",
494                dentry->d_name.len, dentry->d_name.name, rc);
495
496         spin_lock(&lli->lli_lock);
497         if (unlikely(lli->lli_sai == NULL ||
498             lli->lli_sai->sai_generation != minfo->mi_generation)) {
499                 spin_unlock(&lli->lli_lock);
500                 ll_intent_release(it);
501                 dput(dentry);
502                 iput(dir);
503                 OBD_FREE_PTR(minfo);
504                 RETURN(-ESTALE);
505         } else {
506                 sai = ll_sai_get(lli->lli_sai);
507                 entry = ll_sai_entry_set(sai,
508                                          (unsigned int)(long)minfo->mi_cbdata,
509                                          rc ? SA_ENTRY_UNSTATED :
510                                          SA_ENTRY_STATED, req, minfo);
511                 LASSERT(entry != NULL);
512                 if (likely(sa_is_running(sai))) {
513                         ll_sai_entry_to_received(sai, entry);
514                         sai->sai_replied++;
515                         spin_unlock(&lli->lli_lock);
516                         cfs_waitq_signal(&sai->sai_thread.t_ctl_waitq);
517                 } else {
518                         if (!list_empty(&entry->se_list))
519                                 list_del_init(&entry->se_list);
520                         sai->sai_replied++;
521                         spin_unlock(&lli->lli_lock);
522                         ll_sai_entry_cleanup(entry, 1);
523                 }
524                 ll_sai_put(sai);
525                 RETURN(rc);
526         }
527 }
528
529 static void sa_args_fini(struct md_enqueue_info *minfo,
530                          struct ldlm_enqueue_info *einfo)
531 {
532         LASSERT(minfo && einfo);
533         iput(minfo->mi_dir);
534         capa_put(minfo->mi_data.op_capa1);
535         capa_put(minfo->mi_data.op_capa2);
536         OBD_FREE_PTR(minfo);
537         OBD_FREE_PTR(einfo);
538 }
539
540 /**
541  * There is race condition between "capa_put" and "ll_statahead_interpret" for
542  * accessing "op_data.op_capa[1,2]" as following:
543  * "capa_put" releases "op_data.op_capa[1,2]"'s reference count after calling
544  * "md_intent_getattr_async". But "ll_statahead_interpret" maybe run first, and
545  * fill "op_data.op_capa[1,2]" as POISON, then cause "capa_put" access invalid
546  * "ocapa". So here reserve "op_data.op_capa[1,2]" in "pcapa" before calling
547  * "md_intent_getattr_async".
548  */
549 static int sa_args_init(struct inode *dir, struct dentry *dentry,
550                         struct md_enqueue_info **pmi,
551                         struct ldlm_enqueue_info **pei,
552                         struct obd_capa **pcapa)
553 {
554         struct ll_inode_info     *lli = ll_i2info(dir);
555         struct md_enqueue_info   *minfo;
556         struct ldlm_enqueue_info *einfo;
557         struct md_op_data        *op_data;
558
559         OBD_ALLOC_PTR(einfo);
560         if (einfo == NULL)
561                 return -ENOMEM;
562
563         OBD_ALLOC_PTR(minfo);
564         if (minfo == NULL) {
565                 OBD_FREE_PTR(einfo);
566                 return -ENOMEM;
567         }
568
569         op_data = ll_prep_md_op_data(&minfo->mi_data, dir, dentry->d_inode,
570                                      dentry->d_name.name, dentry->d_name.len,
571                                      0, LUSTRE_OPC_ANY, NULL);
572         if (IS_ERR(op_data)) {
573                 OBD_FREE_PTR(einfo);
574                 OBD_FREE_PTR(minfo);
575                 return PTR_ERR(op_data);
576         }
577
578         minfo->mi_it.it_op = IT_GETATTR;
579         minfo->mi_dentry = dentry;
580         minfo->mi_dir = igrab(dir);
581         minfo->mi_cb = ll_statahead_interpret;
582         minfo->mi_generation = lli->lli_sai->sai_generation;
583         minfo->mi_cbdata = (void *)(long)lli->lli_sai->sai_index;
584
585         einfo->ei_type   = LDLM_IBITS;
586         einfo->ei_mode   = it_to_lock_mode(&minfo->mi_it);
587         einfo->ei_cb_bl  = ll_md_blocking_ast;
588         einfo->ei_cb_cp  = ldlm_completion_ast;
589         einfo->ei_cb_gl  = NULL;
590         einfo->ei_cbdata = NULL;
591
592         *pmi = minfo;
593         *pei = einfo;
594         pcapa[0] = op_data->op_capa1;
595         pcapa[1] = op_data->op_capa2;
596
597         return 0;
598 }
599
600 /**
601  * similar to ll_lookup_it().
602  */
603 static int do_sa_lookup(struct inode *dir, struct dentry *dentry)
604 {
605         struct md_enqueue_info   *minfo;
606         struct ldlm_enqueue_info *einfo;
607         struct obd_capa          *capas[2];
608         int                       rc;
609         ENTRY;
610
611         rc = sa_args_init(dir, dentry, &minfo, &einfo, capas);
612         if (rc)
613                 RETURN(rc);
614
615         rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
616         if (!rc) {
617                 capa_put(capas[0]);
618                 capa_put(capas[1]);
619         } else {
620                 sa_args_fini(minfo, einfo);
621         }
622
623         RETURN(rc);
624 }
625
626 /**
627  * similar to ll_revalidate_it().
628  * \retval      1 -- dentry valid
629  * \retval      0 -- will send stat-ahead request
630  * \retval others -- prepare stat-ahead request failed
631  */
632 static int do_sa_revalidate(struct inode *dir, struct dentry *dentry)
633 {
634         struct inode             *inode = dentry->d_inode;
635         struct lookup_intent      it = { .it_op = IT_GETATTR };
636         struct md_enqueue_info   *minfo;
637         struct ldlm_enqueue_info *einfo;
638         struct obd_capa          *capas[2];
639         int rc;
640         ENTRY;
641
642         if (inode == NULL)
643                 RETURN(1);
644
645         if (d_mountpoint(dentry))
646                 RETURN(1);
647
648         if (dentry == dentry->d_sb->s_root)
649                 RETURN(1);
650
651         rc = md_revalidate_lock(ll_i2mdexp(dir), &it, ll_inode2fid(inode));
652         if (rc == 1) {
653                 ll_intent_release(&it);
654                 RETURN(1);
655         }
656
657         rc = sa_args_init(dir, dentry, &minfo, &einfo, capas);
658         if (rc)
659                 RETURN(rc);
660
661         rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
662         if (!rc) {
663                 capa_put(capas[0]);
664                 capa_put(capas[1]);
665         } else {
666                 sa_args_fini(minfo, einfo);
667         }
668
669         RETURN(rc);
670 }
671
672 static inline void ll_name2qstr(struct qstr *q, const char *name, int namelen)
673 {
674         q->name = name;
675         q->len  = namelen;
676         q->hash = full_name_hash(name, namelen);
677 }
678
679 static int ll_statahead_one(struct dentry *parent, const char* entry_name,
680                             int entry_name_len)
681 {
682         struct inode             *dir = parent->d_inode;
683         struct ll_inode_info     *lli = ll_i2info(dir);
684         struct ll_statahead_info *sai = lli->lli_sai;
685         struct qstr               name;
686         struct dentry            *dentry;
687         struct ll_sai_entry      *se;
688         int                       rc;
689         ENTRY;
690
691         if (parent->d_flags & DCACHE_LUSTRE_INVALID) {
692                 CDEBUG(D_READA, "parent dentry@%p %.*s is "
693                        "invalid, skip statahead\n",
694                        parent, parent->d_name.len, parent->d_name.name);
695                 RETURN(-EINVAL);
696         }
697
698         se = ll_sai_entry_init(sai, sai->sai_index);
699         if (IS_ERR(se))
700                 RETURN(PTR_ERR(se));
701
702         ll_name2qstr(&name, entry_name, entry_name_len);
703         dentry = d_lookup(parent, &name);
704         if (!dentry) {
705                 dentry = d_alloc(parent, &name);
706                 if (dentry) {
707                         rc = do_sa_lookup(dir, dentry);
708                         if (rc)
709                                 dput(dentry);
710                 } else {
711                         GOTO(out, rc = -ENOMEM);
712                 }
713         } else {
714                 rc = do_sa_revalidate(dir, dentry);
715                 if (rc)
716                         dput(dentry);
717         }
718
719         EXIT;
720
721 out:
722         if (rc) {
723                 CDEBUG(D_READA, "set sai entry %p index %u stat %d rc %d\n",
724                        se, se->se_index, se->se_stat, rc);
725                 se->se_stat = rc;
726                 if (ll_sai_entry_to_stated(sai, se))
727                         cfs_waitq_signal(&sai->sai_waitq);
728         } else {
729                 sai->sai_sent++;
730         }
731
732         sai->sai_index++;
733         return rc;
734 }
735
736 static int ll_statahead_thread(void *arg)
737 {
738         struct dentry            *parent = (struct dentry *)arg;
739         struct inode             *dir = parent->d_inode;
740         struct ll_inode_info     *lli = ll_i2info(dir);
741         struct ll_sb_info        *sbi = ll_i2sbi(dir);
742         struct ll_statahead_info *sai = ll_sai_get(lli->lli_sai);
743         struct ptlrpc_thread     *thread = &sai->sai_thread;
744         struct page              *page;
745         __u64                     pos = 0;
746         int                       first = 0;
747         int                       rc = 0;
748         struct ll_dir_chain       chain;
749         ENTRY;
750
751         {
752                 char pname[16];
753                 snprintf(pname, 15, "ll_sa_%u", lli->lli_opendir_pid);
754                 cfs_daemonize(pname);
755         }
756
757         sbi->ll_sa_total++;
758         spin_lock(&lli->lli_lock);
759         thread->t_flags = SVC_RUNNING;
760         spin_unlock(&lli->lli_lock);
761         cfs_waitq_signal(&thread->t_ctl_waitq);
762         CDEBUG(D_READA, "start doing statahead for %s\n", parent->d_name.name);
763
764         ll_dir_chain_init(&chain);
765         page = ll_get_dir_page(dir, pos, 0, &chain);
766
767         while (1) {
768                 struct l_wait_info lwi = { 0 };
769                 struct lu_dirpage *dp;
770                 struct lu_dirent  *ent;
771
772                 if (IS_ERR(page)) {
773                         rc = PTR_ERR(page);
774                         CERROR("error reading dir "DFID" at "LPU64"/%u: rc %d\n",
775                                PFID(ll_inode2fid(dir)), pos,
776                                sai->sai_index, rc);
777                         break;
778                 }
779
780                 dp = page_address(page);
781                 for (ent = lu_dirent_start(dp); ent != NULL;
782                      ent = lu_dirent_next(ent)) {
783                         char *name = ent->lde_name;
784                         int namelen = le16_to_cpu(ent->lde_namelen);
785
786                         if (namelen == 0)
787                                 /*
788                                  * Skip dummy record.
789                                  */
790                                 continue;
791
792                         if (name[0] == '.') {
793                                 if (namelen == 1) {
794                                         /*
795                                          * skip "."
796                                          */
797                                         continue;
798                                 } else if (name[1] == '.' && namelen == 2) {
799                                         /*
800                                          * skip ".."
801                                          */
802                                         continue;
803                                 } else if (!sai->sai_ls_all) {
804                                         /*
805                                          * skip hidden files.
806                                          */
807                                         sai->sai_skip_hidden++;
808                                         continue;
809                                 }
810                         }
811
812                         /*
813                          * don't stat-ahead first entry.
814                          */
815                         if (unlikely(!first)) {
816                                 first++;
817                                 continue;
818                         }
819
820 keep_de:
821                         l_wait_event(thread->t_ctl_waitq,
822                                      !sa_is_running(sai) || sa_not_full(sai) ||
823                                      !sa_received_empty(sai),
824                                      &lwi);
825
826                         while (!sa_received_empty(sai) && sa_is_running(sai))
827                                 do_statahead_interpret(sai);
828
829                         if (unlikely(!sa_is_running(sai))) {
830                                 ll_put_page(page);
831                                 GOTO(out, rc);
832                         }
833
834                         if (!sa_not_full(sai))
835                                 /*
836                                  * do not skip the current de.
837                                  */
838                                 goto keep_de;
839
840                         rc = ll_statahead_one(parent, name, namelen);
841                         if (rc < 0) {
842                                 ll_put_page(page);
843                                 GOTO(out, rc);
844                         }
845                 }
846                 pos = le64_to_cpu(dp->ldp_hash_end);
847                 ll_put_page(page);
848                 if (pos == DIR_END_OFF) {
849                         /*
850                          * End of directory reached.
851                          */
852                         while (1) {
853                                 l_wait_event(thread->t_ctl_waitq,
854                                              !sa_is_running(sai) ||
855                                              !sa_received_empty(sai) ||
856                                              sai->sai_sent == sai->sai_replied,
857                                              &lwi);
858                                 if (!sa_received_empty(sai) &&
859                                     sa_is_running(sai))
860                                         do_statahead_interpret(sai);
861                                 else
862                                         GOTO(out, rc);
863                         }
864                 } else if (1) {
865                         /*
866                          * chain is exhausted.
867                          * Normal case: continue to the next page.
868                          */
869                         page = ll_get_dir_page(dir, pos, 1, &chain);
870                 } else {
871                         /*
872                          * go into overflow page.
873                          */
874                 }
875         }
876         EXIT;
877
878 out:
879         ll_dir_chain_fini(&chain);
880         spin_lock(&lli->lli_lock);
881         thread->t_flags = SVC_STOPPED;
882         spin_unlock(&lli->lli_lock);
883         cfs_waitq_signal(&sai->sai_waitq);
884         cfs_waitq_signal(&thread->t_ctl_waitq);
885         ll_sai_put(sai);
886         dput(parent);
887         CDEBUG(D_READA, "statahead thread stopped, pid %d\n",
888                cfs_curproc_pid());
889         return rc;
890 }
891
892 /**
893  * called in ll_file_release().
894  */
895 void ll_stop_statahead(struct inode *inode, void *key)
896 {
897         struct ll_inode_info *lli = ll_i2info(inode);
898
899         if (unlikely(key == NULL))
900                 return;
901
902         spin_lock(&lli->lli_lock);
903         if (lli->lli_opendir_key != key || lli->lli_opendir_pid == 0) {
904                 spin_unlock(&lli->lli_lock);
905                 return;
906         }
907
908         lli->lli_opendir_key = NULL;
909
910         if (lli->lli_sai) {
911                 struct l_wait_info lwi = { 0 };
912                 struct ptlrpc_thread *thread = &lli->lli_sai->sai_thread;
913
914                 if (!sa_is_stopped(lli->lli_sai)) {
915                         thread->t_flags = SVC_STOPPING;
916                         spin_unlock(&lli->lli_lock);
917                         cfs_waitq_signal(&thread->t_ctl_waitq);
918
919                         CDEBUG(D_READA, "stopping statahead thread, pid %d\n",
920                                cfs_curproc_pid());
921                         l_wait_event(thread->t_ctl_waitq,
922                                      sa_is_stopped(lli->lli_sai),
923                                      &lwi);
924                 } else {
925                         spin_unlock(&lli->lli_lock);
926                 }
927
928                 /*
929                  * Put the ref which was held when first statahead_enter.
930                  * It maybe not the last ref for some statahead requests
931                  * maybe inflight.
932                  */
933                 ll_sai_put(lli->lli_sai);
934         } else {
935                 lli->lli_opendir_pid = 0;
936                 spin_unlock(&lli->lli_lock);
937         }
938 }
939
940 enum {
941         /**
942          * not first dirent, or is "."
943          */
944         LS_NONE_FIRST_DE = 0,
945         /**
946          * the first non-hidden dirent
947          */
948         LS_FIRST_DE,
949         /**
950          * the first hidden dirent, that is "." 
951          */
952         LS_FIRST_DOT_DE
953 };
954
955 static int is_first_dirent(struct inode *dir, struct dentry *dentry)
956 {
957         struct ll_dir_chain chain;
958         struct qstr        *target = &dentry->d_name;
959         struct page        *page;
960         __u64               pos = 0;
961         int                 dot_de;
962         int                 rc = LS_NONE_FIRST_DE;
963         ENTRY;
964
965         ll_dir_chain_init(&chain);
966         page = ll_get_dir_page(dir, pos, 0, &chain);
967
968         while (1) {
969                 struct lu_dirpage *dp;
970                 struct lu_dirent  *ent;
971
972                 if (IS_ERR(page)) {
973                         rc = PTR_ERR(page);
974                         CERROR("error reading dir "DFID" at "LPU64": rc %d\n",
975                                PFID(ll_inode2fid(dir)), pos, rc);
976                         break;
977                 }
978
979                 dp = page_address(page);
980                 for (ent = lu_dirent_start(dp); ent != NULL;
981                      ent = lu_dirent_next(ent)) {
982                         char *name = ent->lde_name;
983                         int namelen = le16_to_cpu(ent->lde_namelen);
984
985                         if (namelen == 0)
986                                 /*
987                                  * skip dummy record.
988                                  */
989                                 continue;
990
991                         if (name[0] == '.') {
992                                 if (namelen == 1)
993                                         /*
994                                          * skip "."
995                                          */
996                                         continue;
997                                 else if (name[1] == '.' && namelen == 2)
998                                         /*
999                                          * skip ".."
1000                                          */
1001                                         continue;
1002                                 else
1003                                         dot_de = 1;
1004                         } else {
1005                                 dot_de = 0;
1006                         }
1007
1008                         if (dot_de && target->name[0] != '.') {
1009                                 CDEBUG(D_READA, "%.*s skip hidden file %.*s\n",
1010                                        target->len, target->name,
1011                                        namelen, name);
1012                                 continue;
1013                         }
1014
1015                         if (target->len != namelen ||
1016                             memcmp(target->name, name, namelen) != 0)
1017                                 rc = LS_NONE_FIRST_DE;
1018                         else if (!dot_de)
1019                                 rc = LS_FIRST_DE;
1020                         else
1021                                 rc = LS_FIRST_DOT_DE;
1022
1023                         ll_put_page(page);
1024                         GOTO(out, rc);
1025                 }
1026                 pos = le64_to_cpu(dp->ldp_hash_end);
1027                 ll_put_page(page);
1028                 if (pos == DIR_END_OFF) {
1029                         /*
1030                          * End of directory reached.
1031                          */
1032                         break;
1033                 } else if (1) {
1034                         /*
1035                          * chain is exhausted
1036                          * Normal case: continue to the next page.
1037                          */
1038                         page = ll_get_dir_page(dir, pos, 1, &chain);
1039                 } else {
1040                         /*
1041                          * go into overflow page.
1042                          */
1043                 }
1044         }
1045         EXIT;
1046
1047 out:
1048         ll_dir_chain_fini(&chain);
1049         return rc;
1050 }
1051
1052 /**
1053  * Start statahead thread if this is the first dir entry.
1054  * Otherwise if a thread is started already, wait it until it is ahead of me.
1055  * \retval 0       -- stat ahead thread process such dentry, for lookup, it miss
1056  * \retval 1       -- stat ahead thread process such dentry, for lookup, it hit
1057  * \retval -EEXIST -- stat ahead thread started, and this is the first dentry
1058  * \retval -EBADFD -- statahead thread exit and not dentry available
1059  * \retval -EAGAIN -- try to stat by caller
1060  * \retval others  -- error
1061  */
1062 int do_statahead_enter(struct inode *dir, struct dentry **dentryp, int lookup)
1063 {
1064         struct ll_inode_info     *lli;
1065         struct ll_statahead_info *sai;
1066         struct dentry            *parent;
1067         struct l_wait_info        lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
1068         int                       rc = 0;
1069         ENTRY;
1070
1071         LASSERT(dir != NULL);
1072         lli = ll_i2info(dir);
1073         LASSERT(lli->lli_opendir_pid == cfs_curproc_pid());
1074         sai = lli->lli_sai;
1075
1076         if (sai) {
1077                 struct ll_sb_info *sbi;
1078
1079                 if (unlikely(sa_is_stopped(sai) &&
1080                              list_empty(&sai->sai_entries_stated)))
1081                         RETURN(-EBADFD);
1082
1083                 if ((*dentryp)->d_name.name[0] == '.') {
1084                         if (likely(sai->sai_ls_all ||
1085                             sai->sai_miss_hidden >= sai->sai_skip_hidden)) {
1086                                 /*
1087                                  * Hidden dentry is the first one, or statahead
1088                                  * thread does not skip so many hidden dentries
1089                                  * before "sai_ls_all" enabled as below.
1090                                  */
1091                         } else {
1092                                 if (!sai->sai_ls_all)
1093                                         /*
1094                                          * It maybe because hidden dentry is not
1095                                          * the first one, "sai_ls_all" was not
1096                                          * set, then "ls -al" missed. Enable
1097                                          * "sai_ls_all" for such case.
1098                                          */
1099                                         sai->sai_ls_all = 1;
1100
1101                                 /*
1102                                  * Such "getattr" has been skipped before
1103                                  * "sai_ls_all" enabled as above.
1104                                  */
1105                                 sai->sai_miss_hidden++;
1106                                 RETURN(-ENOENT);
1107                         }
1108                 }
1109
1110                 sbi = ll_i2sbi(dir);
1111                 if (ll_sai_entry_stated(sai)) {
1112                         sbi->ll_sa_cached++;
1113                 } else {
1114                         sbi->ll_sa_blocked++;
1115                         /*
1116                          * thread started already, avoid double-stat.
1117                          */
1118                         rc = l_wait_event(sai->sai_waitq,
1119                                           ll_sai_entry_stated(sai) ||
1120                                           sa_is_stopped(sai),
1121                                           &lwi);
1122                 }
1123
1124                 if (lookup) {
1125                         struct dentry *result;
1126
1127                         result = d_lookup((*dentryp)->d_parent,
1128                                           &(*dentryp)->d_name);
1129                         if (result) {
1130                                 LASSERT(result != *dentryp);
1131                                 /* BUG 16303: do not drop reference count for
1132                                  * "*dentryp", VFS will do that by itself. */
1133                                 *dentryp = result;
1134                                 RETURN(1);
1135                         }
1136                 }
1137                 /*
1138                  * do nothing for revalidate.
1139                  */
1140                 RETURN(rc);
1141         }
1142
1143         /* I am the "lli_opendir_pid" owner, only me can set "lli_sai". */
1144         rc = is_first_dirent(dir, *dentryp);
1145         if (rc == LS_NONE_FIRST_DE)
1146                 /* It is not "ls -{a}l" operation, no need statahead for it. */
1147                 GOTO(out, rc = -EAGAIN);
1148
1149         sai = ll_sai_alloc();
1150         if (sai == NULL)
1151                 GOTO(out, rc = -ENOMEM);
1152
1153         sai->sai_ls_all = (rc == LS_FIRST_DOT_DE);
1154         sai->sai_inode = igrab(dir);
1155         if (unlikely(sai->sai_inode == NULL)) {
1156                 CWARN("Do not start stat ahead on dying inode "DFID" .\n",
1157                       PFID(&lli->lli_fid));
1158                 OBD_FREE_PTR(sai);
1159                 GOTO(out, rc = -ESTALE);
1160         }
1161
1162         /* get parent reference count here, and put it in ll_statahead_thread */
1163         parent = dget((*dentryp)->d_parent);
1164         if (unlikely(sai->sai_inode != parent->d_inode)) {
1165                 struct ll_inode_info *nlli = ll_i2info(parent->d_inode);
1166
1167                 CWARN("Race condition, someone changed %.*s just now: "
1168                       "old parent "DFID", new parent "DFID" .\n",
1169                       (*dentryp)->d_name.len, (*dentryp)->d_name.name,
1170                       PFID(&lli->lli_fid), PFID(&nlli->lli_fid));
1171                 dput(parent);
1172                 iput(sai->sai_inode);
1173                 OBD_FREE_PTR(sai);
1174                 RETURN(-EAGAIN);
1175         }
1176
1177         lli->lli_sai = sai;
1178         rc = cfs_kernel_thread(ll_statahead_thread, parent, 0);
1179         if (rc < 0) {
1180                 CERROR("can't start ll_sa thread, rc: %d\n", rc);
1181                 dput(parent);
1182                 lli->lli_opendir_key = NULL;
1183                 sai->sai_thread.t_flags = SVC_STOPPED;
1184                 ll_sai_put(sai);
1185                 LASSERT(lli->lli_sai == NULL);
1186                 RETURN(-EAGAIN);
1187         }
1188
1189         l_wait_event(sai->sai_thread.t_ctl_waitq,
1190                      sa_is_running(sai) || sa_is_stopped(sai),
1191                      &lwi);
1192
1193         /*
1194          * We don't stat-ahead for the first dirent since we are already in
1195          * lookup, and -EEXIST also indicates that this is the first dirent.
1196          */
1197         RETURN(-EEXIST);
1198
1199 out:
1200         spin_lock(&lli->lli_lock);
1201         lli->lli_opendir_key = NULL;
1202         lli->lli_opendir_pid = 0;
1203         spin_unlock(&lli->lli_lock);
1204         return rc;
1205 }
1206
1207 /**
1208  * update hit/miss count.
1209  */
1210 void ll_statahead_exit(struct inode *dir, struct dentry *dentry, int result)
1211 {
1212         struct ll_inode_info     *lli;
1213         struct ll_statahead_info *sai;
1214         struct ll_sb_info        *sbi;
1215         struct ll_dentry_data    *ldd = ll_d2d(dentry);
1216         ENTRY;
1217
1218         LASSERT(dir != NULL);
1219         lli = ll_i2info(dir);
1220         LASSERT(lli->lli_opendir_pid == cfs_curproc_pid());
1221         sai = lli->lli_sai;
1222         LASSERT(sai != NULL);
1223         sbi = ll_i2sbi(dir);
1224
1225         if (result >= 1) {
1226                 sbi->ll_sa_hit++;
1227                 sai->sai_hit++;
1228                 sai->sai_consecutive_miss = 0;
1229                 sai->sai_max = min(2 * sai->sai_max, sbi->ll_sa_max);
1230         } else {
1231                 sbi->ll_sa_miss++;
1232                 sai->sai_miss++;
1233                 sai->sai_consecutive_miss++;
1234                 if (sa_low_hit(sai) && sa_is_running(sai)) {
1235                         sbi->ll_sa_wrong++;
1236                         CDEBUG(D_READA, "Statahead for dir "DFID" hit ratio "
1237                                "too low: hit/miss %u/%u, sent/replied %u/%u, "
1238                                "stopping statahead thread: pid %d\n",
1239                                PFID(&lli->lli_fid), sai->sai_hit,
1240                                sai->sai_miss, sai->sai_sent,
1241                                sai->sai_replied, cfs_curproc_pid());
1242                         spin_lock(&lli->lli_lock);
1243                         if (!sa_is_stopped(sai))
1244                                 sai->sai_thread.t_flags = SVC_STOPPING;
1245                         spin_unlock(&lli->lli_lock);
1246                 }
1247         }
1248
1249         if (!sa_is_stopped(sai))
1250                 cfs_waitq_signal(&sai->sai_thread.t_ctl_waitq);
1251         ll_sai_entry_fini(sai);
1252         if (likely(ldd != NULL))
1253                 ldd->lld_sa_generation = sai->sai_generation;
1254
1255         EXIT;
1256 }