1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011 Whamcloud, Inc.
36 * This file is part of Lustre, http://www.lustre.org/
37 * Lustre is a trademark of Sun Microsystems, Inc.
41 #include <linux/sched.h>
43 #include <linux/smp_lock.h>
44 #include <linux/highmem.h>
45 #include <linux/pagemap.h>
47 #define DEBUG_SUBSYSTEM S_LLITE
49 #include <obd_support.h>
50 #include <lustre_lite.h>
51 #include <lustre_dlm.h>
52 #include <linux/lustre_version.h>
53 #include "llite_internal.h"
57 unsigned int se_index;
59 struct ptlrpc_request *se_req;
60 struct md_enqueue_info *se_minfo;
64 SA_ENTRY_UNSTATED = 0,
68 static unsigned int sai_generation = 0;
69 static cfs_spinlock_t sai_generation_lock = CFS_SPIN_LOCK_UNLOCKED;
72 * Check whether first entry was stated already or not.
73 * No need to hold lli_sa_lock, for:
74 * (1) it is me that remove entry from the list
75 * (2) the statahead thread only add new entry to the list
77 static int ll_sai_entry_stated(struct ll_statahead_info *sai)
79 struct ll_sai_entry *entry;
82 if (!cfs_list_empty(&sai->sai_entries_stated)) {
83 entry = cfs_list_entry(sai->sai_entries_stated.next,
84 struct ll_sai_entry, se_list);
85 if (entry->se_index == sai->sai_index_next)
91 static inline int sa_received_empty(struct ll_statahead_info *sai)
93 return cfs_list_empty(&sai->sai_entries_received);
96 static inline int sa_not_full(struct ll_statahead_info *sai)
98 return !!(sai->sai_index < sai->sai_index_next + sai->sai_max);
101 static inline int sa_is_running(struct ll_statahead_info *sai)
103 return !!(sai->sai_thread.t_flags & SVC_RUNNING);
106 static inline int sa_is_stopping(struct ll_statahead_info *sai)
108 return !!(sai->sai_thread.t_flags & SVC_STOPPING);
111 static inline int sa_is_stopped(struct ll_statahead_info *sai)
113 return !!(sai->sai_thread.t_flags & SVC_STOPPED);
117 * (1) hit ratio less than 80%
119 * (2) consecutive miss more than 8
121 static inline int sa_low_hit(struct ll_statahead_info *sai)
123 return ((sai->sai_hit > 7 && sai->sai_hit < 4 * sai->sai_miss) ||
124 (sai->sai_consecutive_miss > 8));
128 * process the deleted entry's member and free the entry.
130 * (2) free md_enqueue_info
131 * (3) drop dentry's ref count
132 * (4) release request's ref count
134 static void ll_sai_entry_cleanup(struct ll_sai_entry *entry, int free)
136 struct md_enqueue_info *minfo = entry->se_minfo;
137 struct ptlrpc_request *req = entry->se_req;
141 entry->se_minfo = NULL;
142 ll_intent_release(&minfo->mi_it);
143 dput(minfo->mi_dentry);
148 entry->se_req = NULL;
149 ptlrpc_req_finished(req);
152 LASSERT(cfs_list_empty(&entry->se_list));
159 static struct ll_statahead_info *ll_sai_alloc(void)
161 struct ll_statahead_info *sai;
167 cfs_spin_lock(&sai_generation_lock);
168 sai->sai_generation = ++sai_generation;
169 if (unlikely(sai_generation == 0))
170 sai->sai_generation = ++sai_generation;
171 cfs_spin_unlock(&sai_generation_lock);
172 cfs_atomic_set(&sai->sai_refcount, 1);
173 sai->sai_max = LL_SA_RPC_MIN;
174 cfs_waitq_init(&sai->sai_waitq);
175 cfs_waitq_init(&sai->sai_thread.t_ctl_waitq);
176 CFS_INIT_LIST_HEAD(&sai->sai_entries_sent);
177 CFS_INIT_LIST_HEAD(&sai->sai_entries_received);
178 CFS_INIT_LIST_HEAD(&sai->sai_entries_stated);
183 struct ll_statahead_info *ll_sai_get(struct ll_statahead_info *sai)
186 cfs_atomic_inc(&sai->sai_refcount);
190 static void ll_sai_put(struct ll_statahead_info *sai)
192 struct inode *inode = sai->sai_inode;
193 struct ll_inode_info *lli;
196 LASSERT(inode != NULL);
197 lli = ll_i2info(inode);
198 LASSERT(lli->lli_sai == sai);
200 if (cfs_atomic_dec_and_lock(&sai->sai_refcount, &lli->lli_sa_lock)) {
201 struct ll_sai_entry *entry, *next;
203 if (unlikely(cfs_atomic_read(&sai->sai_refcount) > 0)) {
204 /* It is race case, the interpret callback just hold
205 * a reference count */
206 cfs_spin_unlock(&lli->lli_sa_lock);
210 LASSERT(lli->lli_opendir_key == NULL);
212 lli->lli_opendir_pid = 0;
213 cfs_spin_unlock(&lli->lli_sa_lock);
215 LASSERT(sa_is_stopped(sai));
217 if (sai->sai_sent > sai->sai_replied)
218 CDEBUG(D_READA,"statahead for dir "DFID" does not "
219 "finish: [sent:%u] [replied:%u]\n",
221 sai->sai_sent, sai->sai_replied);
223 cfs_list_for_each_entry_safe(entry, next,
224 &sai->sai_entries_sent, se_list) {
225 cfs_list_del_init(&entry->se_list);
226 ll_sai_entry_cleanup(entry, 1);
228 cfs_list_for_each_entry_safe(entry, next,
229 &sai->sai_entries_received,
231 cfs_list_del_init(&entry->se_list);
232 ll_sai_entry_cleanup(entry, 1);
234 cfs_list_for_each_entry_safe(entry, next,
235 &sai->sai_entries_stated,
237 cfs_list_del_init(&entry->se_list);
238 ll_sai_entry_cleanup(entry, 1);
247 * insert it into sai_entries_sent tail when init.
249 static struct ll_sai_entry *
250 ll_sai_entry_init(struct ll_statahead_info *sai, unsigned int index)
252 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
253 struct ll_sai_entry *entry;
256 OBD_ALLOC_PTR(entry);
258 RETURN(ERR_PTR(-ENOMEM));
260 CDEBUG(D_READA, "alloc sai entry %p index %u\n",
262 entry->se_index = index;
263 entry->se_stat = SA_ENTRY_UNSTATED;
265 cfs_spin_lock(&lli->lli_sa_lock);
266 cfs_list_add_tail(&entry->se_list, &sai->sai_entries_sent);
267 cfs_spin_unlock(&lli->lli_sa_lock);
273 * delete it from sai_entries_stated head when fini, it need not
274 * to process entry's member.
276 static int ll_sai_entry_fini(struct ll_statahead_info *sai)
278 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
279 struct ll_sai_entry *entry;
283 cfs_spin_lock(&lli->lli_sa_lock);
284 sai->sai_index_next++;
285 if (likely(!cfs_list_empty(&sai->sai_entries_stated))) {
286 entry = cfs_list_entry(sai->sai_entries_stated.next,
287 struct ll_sai_entry, se_list);
288 if (entry->se_index < sai->sai_index_next) {
289 cfs_list_del_init(&entry->se_list);
294 LASSERT(sa_is_stopped(sai));
296 cfs_spin_unlock(&lli->lli_sa_lock);
302 * inside lli_sa_lock.
303 * \retval NULL : can not find the entry in sai_entries_sent with the index
304 * \retval entry: find the entry in sai_entries_sent with the index
306 static struct ll_sai_entry *
307 ll_sai_entry_set(struct ll_statahead_info *sai, unsigned int index, int stat,
308 struct ptlrpc_request *req, struct md_enqueue_info *minfo)
310 struct ll_sai_entry *entry;
313 if (!cfs_list_empty(&sai->sai_entries_sent)) {
314 cfs_list_for_each_entry(entry, &sai->sai_entries_sent,
316 if (entry->se_index == index) {
317 entry->se_stat = stat;
318 entry->se_req = ptlrpc_request_addref(req);
319 entry->se_minfo = minfo;
321 } else if (entry->se_index > index) {
330 * inside lli_sa_lock.
331 * Move entry to sai_entries_received and
332 * insert it into sai_entries_received tail.
335 ll_sai_entry_to_received(struct ll_statahead_info *sai, struct ll_sai_entry *entry)
337 if (!cfs_list_empty(&entry->se_list))
338 cfs_list_del_init(&entry->se_list);
339 cfs_list_add_tail(&entry->se_list, &sai->sai_entries_received);
343 * Move entry to sai_entries_stated and
344 * sort with the index.
347 ll_sai_entry_to_stated(struct ll_statahead_info *sai, struct ll_sai_entry *entry)
349 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
350 struct ll_sai_entry *se;
353 ll_sai_entry_cleanup(entry, 0);
355 cfs_spin_lock(&lli->lli_sa_lock);
356 if (!cfs_list_empty(&entry->se_list))
357 cfs_list_del_init(&entry->se_list);
360 if (unlikely(entry->se_index < sai->sai_index_next)) {
361 cfs_spin_unlock(&lli->lli_sa_lock);
366 cfs_list_for_each_entry_reverse(se, &sai->sai_entries_stated, se_list) {
367 if (se->se_index < entry->se_index) {
368 cfs_list_add(&entry->se_list, &se->se_list);
369 cfs_spin_unlock(&lli->lli_sa_lock);
375 * I am the first entry.
377 cfs_list_add(&entry->se_list, &sai->sai_entries_stated);
378 cfs_spin_unlock(&lli->lli_sa_lock);
383 * finish lookup/revalidate.
385 static int do_statahead_interpret(struct ll_statahead_info *sai)
387 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
388 struct ll_sai_entry *entry;
389 struct ptlrpc_request *req;
390 struct md_enqueue_info *minfo;
391 struct lookup_intent *it;
392 struct dentry *dentry;
394 struct mdt_body *body;
397 cfs_spin_lock(&lli->lli_sa_lock);
398 LASSERT(!sa_received_empty(sai));
399 entry = cfs_list_entry(sai->sai_entries_received.next,
400 struct ll_sai_entry, se_list);
401 cfs_list_del_init(&entry->se_list);
402 cfs_spin_unlock(&lli->lli_sa_lock);
404 if (unlikely(entry->se_index < sai->sai_index_next)) {
405 CWARN("Found stale entry: [index %u] [next %u]\n",
406 entry->se_index, sai->sai_index_next);
407 ll_sai_entry_cleanup(entry, 1);
411 if (entry->se_stat != SA_ENTRY_STATED)
412 GOTO(out, rc = entry->se_stat);
415 minfo = entry->se_minfo;
417 dentry = minfo->mi_dentry;
419 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
421 GOTO(out, rc = -EFAULT);
423 if (dentry->d_inode == NULL) {
427 struct dentry *save = dentry;
428 struct it_cb_data icbd = {
429 .icbd_parent = minfo->mi_dir,
430 .icbd_childp = &dentry
433 LASSERT(fid_is_zero(&minfo->mi_data.op_fid2));
435 /* XXX: No fid in reply, this is probaly cross-ref case.
436 * SA can't handle it yet. */
437 if (body->valid & OBD_MD_MDS)
438 GOTO(out, rc = -EAGAIN);
440 /* Here dentry->d_inode might be NULL, because the entry may
441 * have been removed before we start doing stat ahead. */
442 rc = ll_lookup_it_finish(req, it, &icbd);
444 ll_lookup_finish_locks(it, dentry);
446 if (dentry != save) {
447 minfo->mi_dentry = dentry;
454 if (!lu_fid_eq(&minfo->mi_data.op_fid2, &body->fid1)) {
455 ll_unhash_aliases(dentry->d_inode);
456 GOTO(out, rc = -EAGAIN);
459 rc = ll_revalidate_it_finish(req, it, dentry);
461 ll_unhash_aliases(dentry->d_inode);
465 cfs_spin_lock(&ll_lookup_lock);
466 spin_lock(&dcache_lock);
469 dentry->d_flags &= ~DCACHE_LUSTRE_INVALID;
470 unlock_dentry(dentry);
471 d_rehash_cond(dentry, 0);
472 spin_unlock(&dcache_lock);
473 cfs_spin_unlock(&ll_lookup_lock);
475 ll_lookup_finish_locks(it, dentry);
480 /* The "ll_sai_entry_to_stated()" will drop related ldlm ibits lock
481 * reference count with ll_intent_drop_lock() called in spite of the
482 * above operations failed or not. Do not worry about calling
483 * "ll_intent_drop_lock()" more than once. */
484 if (likely(ll_sai_entry_to_stated(sai, entry)))
485 cfs_waitq_signal(&sai->sai_waitq);
489 static int ll_statahead_interpret(struct ptlrpc_request *req,
490 struct md_enqueue_info *minfo,
493 struct lookup_intent *it = &minfo->mi_it;
494 struct dentry *dentry = minfo->mi_dentry;
495 struct inode *dir = minfo->mi_dir;
496 struct ll_inode_info *lli = ll_i2info(dir);
497 struct ll_statahead_info *sai;
498 struct ll_sai_entry *entry;
501 CDEBUG(D_READA, "interpret statahead %.*s rc %d\n",
502 dentry->d_name.len, dentry->d_name.name, rc);
504 cfs_spin_lock(&lli->lli_sa_lock);
506 if (unlikely(lli->lli_sai == NULL ||
507 lli->lli_sai->sai_generation != minfo->mi_generation)) {
508 cfs_spin_unlock(&lli->lli_sa_lock);
509 ll_intent_release(it);
515 sai = ll_sai_get(lli->lli_sai);
516 entry = ll_sai_entry_set(sai,
517 (unsigned int)(long)minfo->mi_cbdata,
518 rc < 0 ? rc : SA_ENTRY_STATED, req,
520 LASSERT(entry != NULL);
521 if (likely(sa_is_running(sai))) {
522 ll_sai_entry_to_received(sai, entry);
524 cfs_spin_unlock(&lli->lli_sa_lock);
525 cfs_waitq_signal(&sai->sai_thread.t_ctl_waitq);
527 if (!cfs_list_empty(&entry->se_list))
528 cfs_list_del_init(&entry->se_list);
530 cfs_spin_unlock(&lli->lli_sa_lock);
531 ll_sai_entry_cleanup(entry, 1);
538 static void sa_args_fini(struct md_enqueue_info *minfo,
539 struct ldlm_enqueue_info *einfo)
541 LASSERT(minfo && einfo);
543 capa_put(minfo->mi_data.op_capa1);
544 capa_put(minfo->mi_data.op_capa2);
550 * There is race condition between "capa_put" and "ll_statahead_interpret" for
551 * accessing "op_data.op_capa[1,2]" as following:
552 * "capa_put" releases "op_data.op_capa[1,2]"'s reference count after calling
553 * "md_intent_getattr_async". But "ll_statahead_interpret" maybe run first, and
554 * fill "op_data.op_capa[1,2]" as POISON, then cause "capa_put" access invalid
555 * "ocapa". So here reserve "op_data.op_capa[1,2]" in "pcapa" before calling
556 * "md_intent_getattr_async".
558 static int sa_args_init(struct inode *dir, struct dentry *dentry,
559 struct md_enqueue_info **pmi,
560 struct ldlm_enqueue_info **pei,
561 struct obd_capa **pcapa)
563 struct ll_inode_info *lli = ll_i2info(dir);
564 struct md_enqueue_info *minfo;
565 struct ldlm_enqueue_info *einfo;
566 struct md_op_data *op_data;
568 OBD_ALLOC_PTR(einfo);
572 OBD_ALLOC_PTR(minfo);
578 op_data = ll_prep_md_op_data(&minfo->mi_data, dir, dentry->d_inode,
579 dentry->d_name.name, dentry->d_name.len,
580 0, LUSTRE_OPC_ANY, NULL);
581 if (IS_ERR(op_data)) {
584 return PTR_ERR(op_data);
587 minfo->mi_it.it_op = IT_GETATTR;
588 minfo->mi_dentry = dentry;
589 minfo->mi_dir = igrab(dir);
590 minfo->mi_cb = ll_statahead_interpret;
591 minfo->mi_generation = lli->lli_sai->sai_generation;
592 minfo->mi_cbdata = (void *)(long)lli->lli_sai->sai_index;
594 einfo->ei_type = LDLM_IBITS;
595 einfo->ei_mode = it_to_lock_mode(&minfo->mi_it);
596 einfo->ei_cb_bl = ll_md_blocking_ast;
597 einfo->ei_cb_cp = ldlm_completion_ast;
598 einfo->ei_cb_gl = NULL;
599 einfo->ei_cbdata = NULL;
603 pcapa[0] = op_data->op_capa1;
604 pcapa[1] = op_data->op_capa2;
610 * similar to ll_lookup_it().
612 static int do_sa_lookup(struct inode *dir, struct dentry *dentry)
614 struct md_enqueue_info *minfo;
615 struct ldlm_enqueue_info *einfo;
616 struct obd_capa *capas[2];
620 rc = sa_args_init(dir, dentry, &minfo, &einfo, capas);
624 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
629 sa_args_fini(minfo, einfo);
636 * similar to ll_revalidate_it().
637 * \retval 1 -- dentry valid
638 * \retval 0 -- will send stat-ahead request
639 * \retval others -- prepare stat-ahead request failed
641 static int do_sa_revalidate(struct inode *dir, struct dentry *dentry)
643 struct inode *inode = dentry->d_inode;
644 struct lookup_intent it = { .it_op = IT_GETATTR };
645 struct md_enqueue_info *minfo;
646 struct ldlm_enqueue_info *einfo;
647 struct obd_capa *capas[2];
651 if (unlikely(inode == NULL))
654 if (d_mountpoint(dentry))
657 if (unlikely(dentry == dentry->d_sb->s_root))
660 rc = md_revalidate_lock(ll_i2mdexp(dir), &it, ll_inode2fid(inode));
662 ll_intent_release(&it);
666 rc = sa_args_init(dir, dentry, &minfo, &einfo, capas);
670 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
675 sa_args_fini(minfo, einfo);
681 static inline void ll_name2qstr(struct qstr *q, const char *name, int namelen)
685 q->hash = full_name_hash(name, namelen);
688 static int ll_statahead_one(struct dentry *parent, const char* entry_name,
691 struct inode *dir = parent->d_inode;
692 struct ll_inode_info *lli = ll_i2info(dir);
693 struct ll_statahead_info *sai = lli->lli_sai;
695 struct dentry *dentry = NULL;
696 struct ll_sai_entry *se;
700 if (parent->d_flags & DCACHE_LUSTRE_INVALID) {
701 CDEBUG(D_READA, "parent dentry@%p %.*s is "
702 "invalid, skip statahead\n",
703 parent, parent->d_name.len, parent->d_name.name);
707 se = ll_sai_entry_init(sai, sai->sai_index);
711 ll_name2qstr(&name, entry_name, entry_name_len);
712 dentry = d_lookup(parent, &name);
714 dentry = d_alloc(parent, &name);
716 rc = do_sa_lookup(dir, dentry);
718 GOTO(out, rc = -ENOMEM);
720 rc = do_sa_revalidate(dir, dentry);
729 se->se_stat = rc < 0 ? rc : SA_ENTRY_STATED;
730 CDEBUG(D_READA, "set sai entry %p index %u stat %d rc %d\n",
731 se, se->se_index, se->se_stat, rc);
732 if (ll_sai_entry_to_stated(sai, se))
733 cfs_waitq_signal(&sai->sai_waitq);
742 static int ll_statahead_thread(void *arg)
744 struct dentry *parent = (struct dentry *)arg;
745 struct inode *dir = parent->d_inode;
746 struct ll_inode_info *lli = ll_i2info(dir);
747 struct ll_sb_info *sbi = ll_i2sbi(dir);
748 struct ll_statahead_info *sai = ll_sai_get(lli->lli_sai);
749 struct ptlrpc_thread *thread = &sai->sai_thread;
754 struct ll_dir_chain chain;
759 snprintf(pname, 15, "ll_sa_%u", lli->lli_opendir_pid);
760 cfs_daemonize(pname);
763 atomic_inc(&sbi->ll_sa_total);
764 cfs_spin_lock(&lli->lli_sa_lock);
765 thread->t_flags = SVC_RUNNING;
766 cfs_spin_unlock(&lli->lli_sa_lock);
767 cfs_waitq_signal(&thread->t_ctl_waitq);
768 CDEBUG(D_READA, "start doing statahead for %s\n", parent->d_name.name);
771 ll_dir_chain_init(&chain);
772 page = ll_get_dir_page(NULL, dir, pos, &chain);
775 struct l_wait_info lwi = { 0 };
776 struct lu_dirpage *dp;
777 struct lu_dirent *ent;
781 CDEBUG(D_READA, "error reading dir "DFID" at "LPU64
782 "/%u: [rc %d] [parent %u]\n",
783 PFID(ll_inode2fid(dir)), pos, sai->sai_index,
784 rc, lli->lli_opendir_pid);
788 dp = page_address(page);
789 for (ent = lu_dirent_start(dp); ent != NULL;
790 ent = lu_dirent_next(ent)) {
795 hash = le64_to_cpu(ent->lde_hash);
796 if (unlikely(hash < pos))
798 * Skip until we find target hash value.
802 namelen = le16_to_cpu(ent->lde_namelen);
803 if (unlikely(namelen == 0))
809 name = ent->lde_name;
810 if (name[0] == '.') {
816 } else if (name[1] == '.' && namelen == 2) {
821 } else if (!sai->sai_ls_all) {
825 sai->sai_skip_hidden++;
831 * don't stat-ahead first entry.
833 if (unlikely(!first)) {
839 l_wait_event(thread->t_ctl_waitq,
840 !sa_is_running(sai) || sa_not_full(sai) ||
841 !sa_received_empty(sai),
844 while (!sa_received_empty(sai) && sa_is_running(sai))
845 do_statahead_interpret(sai);
847 if (unlikely(!sa_is_running(sai))) {
848 ll_release_page(page, 0);
852 if (!sa_not_full(sai))
854 * do not skip the current de.
858 rc = ll_statahead_one(parent, name, namelen);
860 ll_release_page(page, 0);
864 pos = le64_to_cpu(dp->ldp_hash_end);
865 if (pos == MDS_DIR_END_OFF) {
867 * End of directory reached.
869 ll_release_page(page, 0);
871 l_wait_event(thread->t_ctl_waitq,
872 !sa_is_running(sai) ||
873 !sa_received_empty(sai) ||
874 sai->sai_sent == sai->sai_replied,
876 if (!sa_received_empty(sai) &&
878 do_statahead_interpret(sai);
884 * chain is exhausted.
885 * Normal case: continue to the next page.
887 ll_release_page(page, le32_to_cpu(dp->ldp_flags) &
889 lli->lli_sa_pos = pos;
890 page = ll_get_dir_page(NULL, dir, pos, &chain);
892 LASSERT(le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
893 ll_release_page(page, 1);
895 * go into overflow page.
902 ll_dir_chain_fini(&chain);
903 cfs_spin_lock(&lli->lli_sa_lock);
904 thread->t_flags = SVC_STOPPED;
905 cfs_spin_unlock(&lli->lli_sa_lock);
906 cfs_waitq_signal(&sai->sai_waitq);
907 cfs_waitq_signal(&thread->t_ctl_waitq);
910 CDEBUG(D_READA, "statahead thread stopped, pid %d\n",
916 * called in ll_file_release().
918 void ll_stop_statahead(struct inode *dir, void *key)
920 struct ll_inode_info *lli = ll_i2info(dir);
922 if (unlikely(key == NULL))
925 cfs_spin_lock(&lli->lli_sa_lock);
926 if (lli->lli_opendir_key != key || lli->lli_opendir_pid == 0) {
927 cfs_spin_unlock(&lli->lli_sa_lock);
931 lli->lli_opendir_key = NULL;
934 struct l_wait_info lwi = { 0 };
935 struct ptlrpc_thread *thread = &lli->lli_sai->sai_thread;
937 if (!sa_is_stopped(lli->lli_sai)) {
938 thread->t_flags = SVC_STOPPING;
939 cfs_spin_unlock(&lli->lli_sa_lock);
940 cfs_waitq_signal(&thread->t_ctl_waitq);
942 CDEBUG(D_READA, "stopping statahead thread, pid %d\n",
944 l_wait_event(thread->t_ctl_waitq,
945 sa_is_stopped(lli->lli_sai),
948 cfs_spin_unlock(&lli->lli_sa_lock);
952 * Put the ref which was held when first statahead_enter.
953 * It maybe not the last ref for some statahead requests
956 ll_sai_put(lli->lli_sai);
958 lli->lli_opendir_pid = 0;
959 cfs_spin_unlock(&lli->lli_sa_lock);
965 * not first dirent, or is "."
967 LS_NONE_FIRST_DE = 0,
969 * the first non-hidden dirent
973 * the first hidden dirent, that is "."
978 static int is_first_dirent(struct inode *dir, struct dentry *dentry)
980 struct ll_inode_info *lli = ll_i2info(dir);
981 struct ll_dir_chain chain;
982 struct qstr *target = &dentry->d_name;
986 int rc = LS_NONE_FIRST_DE;
990 ll_dir_chain_init(&chain);
991 page = ll_get_dir_page(NULL, dir, pos, &chain);
994 struct lu_dirpage *dp;
995 struct lu_dirent *ent;
998 struct ll_inode_info *lli = ll_i2info(dir);
1001 CERROR("error reading dir "DFID" at "LPU64": "
1002 "[rc %d] [parent %u]\n",
1003 PFID(ll_inode2fid(dir)), pos,
1004 rc, lli->lli_opendir_pid);
1008 dp = page_address(page);
1009 for (ent = lu_dirent_start(dp); ent != NULL;
1010 ent = lu_dirent_next(ent)) {
1014 namelen = le16_to_cpu(ent->lde_namelen);
1015 if (unlikely(namelen == 0))
1017 * skip dummy record.
1021 name = ent->lde_name;
1022 if (name[0] == '.') {
1028 else if (name[1] == '.' && namelen == 2)
1039 if (dot_de && target->name[0] != '.') {
1040 CDEBUG(D_READA, "%.*s skip hidden file %.*s\n",
1041 target->len, target->name,
1046 if (target->len != namelen ||
1047 memcmp(target->name, name, namelen) != 0)
1048 rc = LS_NONE_FIRST_DE;
1052 rc = LS_FIRST_DOT_DE;
1054 ll_release_page(page, 0);
1057 pos = le64_to_cpu(dp->ldp_hash_end);
1058 if (pos == MDS_DIR_END_OFF) {
1060 * End of directory reached.
1062 ll_release_page(page, 0);
1066 * chain is exhausted
1067 * Normal case: continue to the next page.
1069 ll_release_page(page, le32_to_cpu(dp->ldp_flags) &
1071 lli->lli_sa_pos = pos;
1072 page = ll_get_dir_page(NULL, dir, pos, &chain);
1075 * go into overflow page.
1077 LASSERT(le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1078 ll_release_page(page, 1);
1084 ll_dir_chain_fini(&chain);
1089 * Start statahead thread if this is the first dir entry.
1090 * Otherwise if a thread is started already, wait it until it is ahead of me.
1091 * \retval 0 -- stat ahead thread process such dentry, for lookup, it miss
1092 * \retval 1 -- stat ahead thread process such dentry, for lookup, it hit
1093 * \retval -EEXIST -- stat ahead thread started, and this is the first dentry
1094 * \retval -EBADFD -- statahead thread exit and not dentry available
1095 * \retval -EAGAIN -- try to stat by caller
1096 * \retval others -- error
1098 int do_statahead_enter(struct inode *dir, struct dentry **dentryp, int lookup)
1100 struct ll_inode_info *lli;
1101 struct ll_statahead_info *sai;
1102 struct dentry *parent;
1103 struct l_wait_info lwi = { 0 };
1107 LASSERT(dir != NULL);
1108 lli = ll_i2info(dir);
1109 LASSERT(lli->lli_opendir_pid == cfs_curproc_pid());
1113 if (unlikely(sa_is_stopped(sai) &&
1114 cfs_list_empty(&sai->sai_entries_stated)))
1117 if ((*dentryp)->d_name.name[0] == '.') {
1118 if (likely(sai->sai_ls_all ||
1119 sai->sai_miss_hidden >= sai->sai_skip_hidden)) {
1121 * Hidden dentry is the first one, or statahead
1122 * thread does not skip so many hidden dentries
1123 * before "sai_ls_all" enabled as below.
1126 if (!sai->sai_ls_all)
1128 * It maybe because hidden dentry is not
1129 * the first one, "sai_ls_all" was not
1130 * set, then "ls -al" missed. Enable
1131 * "sai_ls_all" for such case.
1133 sai->sai_ls_all = 1;
1136 * Such "getattr" has been skipped before
1137 * "sai_ls_all" enabled as above.
1139 sai->sai_miss_hidden++;
1144 if (!ll_sai_entry_stated(sai)) {
1146 * thread started already, avoid double-stat.
1148 lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
1149 rc = l_wait_event(sai->sai_waitq,
1150 ll_sai_entry_stated(sai) ||
1153 if (unlikely(rc == -EINTR))
1158 struct dentry *result;
1160 result = d_lookup((*dentryp)->d_parent,
1161 &(*dentryp)->d_name);
1163 LASSERT(result != *dentryp);
1164 /* BUG 16303: do not drop reference count for
1165 * "*dentryp", VFS will do that by itself. */
1171 * do nothing for revalidate.
1176 /* I am the "lli_opendir_pid" owner, only me can set "lli_sai". */
1177 rc = is_first_dirent(dir, *dentryp);
1178 if (rc == LS_NONE_FIRST_DE)
1179 /* It is not "ls -{a}l" operation, no need statahead for it. */
1180 GOTO(out, rc = -EAGAIN);
1182 sai = ll_sai_alloc();
1184 GOTO(out, rc = -ENOMEM);
1186 sai->sai_ls_all = (rc == LS_FIRST_DOT_DE);
1187 sai->sai_inode = igrab(dir);
1188 if (unlikely(sai->sai_inode == NULL)) {
1189 CWARN("Do not start stat ahead on dying inode "DFID"\n",
1190 PFID(&lli->lli_fid));
1192 GOTO(out, rc = -ESTALE);
1195 /* get parent reference count here, and put it in ll_statahead_thread */
1196 parent = dget((*dentryp)->d_parent);
1197 if (unlikely(sai->sai_inode != parent->d_inode)) {
1198 struct ll_inode_info *nlli = ll_i2info(parent->d_inode);
1200 CWARN("Race condition, someone changed %.*s just now: "
1201 "old parent "DFID", new parent "DFID"\n",
1202 (*dentryp)->d_name.len, (*dentryp)->d_name.name,
1203 PFID(&lli->lli_fid), PFID(&nlli->lli_fid));
1205 iput(sai->sai_inode);
1211 rc = cfs_create_thread(ll_statahead_thread, parent, 0);
1213 CERROR("can't start ll_sa thread, rc: %d\n", rc);
1215 lli->lli_opendir_key = NULL;
1216 sai->sai_thread.t_flags = SVC_STOPPED;
1218 LASSERT(lli->lli_sai == NULL);
1222 l_wait_event(sai->sai_thread.t_ctl_waitq,
1223 sa_is_running(sai) || sa_is_stopped(sai),
1227 * We don't stat-ahead for the first dirent since we are already in
1228 * lookup, and -EEXIST also indicates that this is the first dirent.
1233 cfs_spin_lock(&lli->lli_sa_lock);
1234 lli->lli_opendir_key = NULL;
1235 lli->lli_opendir_pid = 0;
1236 cfs_spin_unlock(&lli->lli_sa_lock);
1241 * update hit/miss count.
1243 void ll_statahead_exit(struct inode *dir, struct dentry *dentry, int result)
1245 struct ll_inode_info *lli;
1246 struct ll_statahead_info *sai;
1247 struct ll_sb_info *sbi;
1248 struct ll_dentry_data *ldd = ll_d2d(dentry);
1252 LASSERT(dir != NULL);
1253 lli = ll_i2info(dir);
1254 LASSERT(lli->lli_opendir_pid == cfs_curproc_pid());
1256 LASSERT(sai != NULL);
1257 sbi = ll_i2sbi(dir);
1259 rc = ll_sai_entry_fini(sai);
1260 /* rc == -ENOENT means such dentry was removed just between statahead
1261 * readdir and pre-fetched, count it as hit.
1263 * result == -ENOENT has two meanings:
1264 * 1. such dentry was removed just between statahead pre-fetched and
1265 * main process stat such dentry.
1266 * 2. main process stat non-exist dentry.
1267 * We can not distinguish such two cases, just count them as miss. */
1268 if (result >= 1 || unlikely(rc == -ENOENT)) {
1270 sai->sai_consecutive_miss = 0;
1271 sai->sai_max = min(2 * sai->sai_max, sbi->ll_sa_max);
1274 sai->sai_consecutive_miss++;
1275 if (sa_low_hit(sai) && sa_is_running(sai)) {
1276 atomic_inc(&sbi->ll_sa_wrong);
1277 CDEBUG(D_READA, "Statahead for dir "DFID" hit ratio "
1278 "too low: hit/miss %u/%u, sent/replied %u/%u, "
1279 "stopping statahead thread: pid %d\n",
1280 PFID(&lli->lli_fid), sai->sai_hit,
1281 sai->sai_miss, sai->sai_sent,
1282 sai->sai_replied, cfs_curproc_pid());
1283 cfs_spin_lock(&lli->lli_sa_lock);
1284 if (!sa_is_stopped(sai))
1285 sai->sai_thread.t_flags = SVC_STOPPING;
1286 cfs_spin_unlock(&lli->lli_sa_lock);
1290 if (!sa_is_stopped(sai))
1291 cfs_waitq_signal(&sai->sai_thread.t_ctl_waitq);
1292 if (likely(ldd != NULL))
1293 ldd->lld_sa_generation = sai->sai_generation;