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