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