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);
770 sai->sai_pid = cfs_curproc_pid();
772 ll_dir_chain_init(&chain);
773 page = ll_get_dir_page(NULL, dir, pos, 0, &chain);
776 struct l_wait_info lwi = { 0 };
777 struct lu_dirpage *dp;
778 struct lu_dirent *ent;
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);
789 dp = page_address(page);
790 for (ent = lu_dirent_start(dp); ent != NULL;
791 ent = lu_dirent_next(ent)) {
796 hash = le64_to_cpu(ent->lde_hash);
797 if (unlikely(hash < pos))
799 * Skip until we find target hash value.
803 namelen = le16_to_cpu(ent->lde_namelen);
804 if (unlikely(namelen == 0))
810 name = ent->lde_name;
811 if (name[0] == '.') {
817 } else if (name[1] == '.' && namelen == 2) {
822 } else if (!sai->sai_ls_all) {
826 sai->sai_skip_hidden++;
832 * don't stat-ahead first entry.
834 if (unlikely(!first)) {
840 l_wait_event(thread->t_ctl_waitq,
841 !sa_is_running(sai) || sa_not_full(sai) ||
842 !sa_received_empty(sai),
845 while (!sa_received_empty(sai) && sa_is_running(sai))
846 do_statahead_interpret(sai);
848 if (unlikely(!sa_is_running(sai))) {
853 if (!sa_not_full(sai))
855 * do not skip the current de.
859 rc = ll_statahead_one(parent, name, namelen);
865 pos = le64_to_cpu(dp->ldp_hash_end);
867 if (pos == MDS_DIR_END_OFF) {
869 * End of directory reached.
872 l_wait_event(thread->t_ctl_waitq,
873 !sa_is_running(sai) ||
874 !sa_received_empty(sai) ||
875 sai->sai_sent == sai->sai_replied,
877 if (!sa_received_empty(sai) &&
879 do_statahead_interpret(sai);
885 * chain is exhausted.
886 * Normal case: continue to the next page.
888 lli->lli_sa_pos = pos;
889 page = ll_get_dir_page(NULL, dir, pos, 1, &chain);
892 * go into overflow page.
899 ll_dir_chain_fini(&chain);
900 cfs_spin_lock(&lli->lli_sa_lock);
901 thread->t_flags = SVC_STOPPED;
902 cfs_spin_unlock(&lli->lli_sa_lock);
903 cfs_waitq_signal(&sai->sai_waitq);
904 cfs_waitq_signal(&thread->t_ctl_waitq);
907 CDEBUG(D_READA, "statahead thread stopped, pid %d\n",
913 * called in ll_file_release().
915 void ll_stop_statahead(struct inode *dir, void *key)
917 struct ll_inode_info *lli = ll_i2info(dir);
919 if (unlikely(key == NULL))
922 cfs_spin_lock(&lli->lli_sa_lock);
923 if (lli->lli_opendir_key != key || lli->lli_opendir_pid == 0) {
924 cfs_spin_unlock(&lli->lli_sa_lock);
928 lli->lli_opendir_key = NULL;
931 struct l_wait_info lwi = { 0 };
932 struct ptlrpc_thread *thread = &lli->lli_sai->sai_thread;
934 if (!sa_is_stopped(lli->lli_sai)) {
935 thread->t_flags = SVC_STOPPING;
936 cfs_spin_unlock(&lli->lli_sa_lock);
937 cfs_waitq_signal(&thread->t_ctl_waitq);
939 CDEBUG(D_READA, "stopping statahead thread, pid %d\n",
941 l_wait_event(thread->t_ctl_waitq,
942 sa_is_stopped(lli->lli_sai),
945 cfs_spin_unlock(&lli->lli_sa_lock);
949 * Put the ref which was held when first statahead_enter.
950 * It maybe not the last ref for some statahead requests
953 ll_sai_put(lli->lli_sai);
955 lli->lli_opendir_pid = 0;
956 cfs_spin_unlock(&lli->lli_sa_lock);
962 * not first dirent, or is "."
964 LS_NONE_FIRST_DE = 0,
966 * the first non-hidden dirent
970 * the first hidden dirent, that is "."
975 static int is_first_dirent(struct inode *dir, struct dentry *dentry)
977 struct ll_inode_info *lli = ll_i2info(dir);
978 struct ll_dir_chain chain;
979 struct qstr *target = &dentry->d_name;
983 int rc = LS_NONE_FIRST_DE;
987 ll_dir_chain_init(&chain);
988 page = ll_get_dir_page(NULL, dir, pos, 0, &chain);
991 struct lu_dirpage *dp;
992 struct lu_dirent *ent;
995 struct ll_inode_info *lli = ll_i2info(dir);
998 CERROR("error reading dir "DFID" at "LPU64": "
999 "[rc %d] [parent %u]\n",
1000 PFID(ll_inode2fid(dir)), pos,
1001 rc, lli->lli_opendir_pid);
1005 dp = page_address(page);
1006 for (ent = lu_dirent_start(dp); ent != NULL;
1007 ent = lu_dirent_next(ent)) {
1011 namelen = le16_to_cpu(ent->lde_namelen);
1012 if (unlikely(namelen == 0))
1014 * skip dummy record.
1018 name = ent->lde_name;
1019 if (name[0] == '.') {
1025 else if (name[1] == '.' && namelen == 2)
1036 if (dot_de && target->name[0] != '.') {
1037 CDEBUG(D_READA, "%.*s skip hidden file %.*s\n",
1038 target->len, target->name,
1043 if (target->len != namelen ||
1044 memcmp(target->name, name, namelen) != 0)
1045 rc = LS_NONE_FIRST_DE;
1049 rc = LS_FIRST_DOT_DE;
1054 pos = le64_to_cpu(dp->ldp_hash_end);
1056 if (pos == MDS_DIR_END_OFF) {
1058 * End of directory reached.
1063 * chain is exhausted
1064 * Normal case: continue to the next page.
1066 lli->lli_sa_pos = pos;
1067 page = ll_get_dir_page(NULL, dir, pos, 1, &chain);
1070 * go into overflow page.
1077 ll_dir_chain_fini(&chain);
1082 * Start statahead thread if this is the first dir entry.
1083 * Otherwise if a thread is started already, wait it until it is ahead of me.
1084 * \retval 0 -- stat ahead thread process such dentry, for lookup, it miss
1085 * \retval 1 -- stat ahead thread process such dentry, for lookup, it hit
1086 * \retval -EEXIST -- stat ahead thread started, and this is the first dentry
1087 * \retval -EBADFD -- statahead thread exit and not dentry available
1088 * \retval -EAGAIN -- try to stat by caller
1089 * \retval others -- error
1091 int do_statahead_enter(struct inode *dir, struct dentry **dentryp, int lookup)
1093 struct ll_inode_info *lli;
1094 struct ll_statahead_info *sai;
1095 struct dentry *parent;
1096 struct l_wait_info lwi = { 0 };
1100 LASSERT(dir != NULL);
1101 lli = ll_i2info(dir);
1102 LASSERT(lli->lli_opendir_pid == cfs_curproc_pid());
1106 if (unlikely(sa_is_stopped(sai) &&
1107 cfs_list_empty(&sai->sai_entries_stated)))
1110 if ((*dentryp)->d_name.name[0] == '.') {
1111 if (likely(sai->sai_ls_all ||
1112 sai->sai_miss_hidden >= sai->sai_skip_hidden)) {
1114 * Hidden dentry is the first one, or statahead
1115 * thread does not skip so many hidden dentries
1116 * before "sai_ls_all" enabled as below.
1119 if (!sai->sai_ls_all)
1121 * It maybe because hidden dentry is not
1122 * the first one, "sai_ls_all" was not
1123 * set, then "ls -al" missed. Enable
1124 * "sai_ls_all" for such case.
1126 sai->sai_ls_all = 1;
1129 * Such "getattr" has been skipped before
1130 * "sai_ls_all" enabled as above.
1132 sai->sai_miss_hidden++;
1137 if (!ll_sai_entry_stated(sai)) {
1139 * thread started already, avoid double-stat.
1141 lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
1142 rc = l_wait_event(sai->sai_waitq,
1143 ll_sai_entry_stated(sai) ||
1146 if (unlikely(rc == -EINTR))
1151 struct dentry *result;
1153 result = d_lookup((*dentryp)->d_parent,
1154 &(*dentryp)->d_name);
1156 LASSERT(result != *dentryp);
1157 /* BUG 16303: do not drop reference count for
1158 * "*dentryp", VFS will do that by itself. */
1164 * do nothing for revalidate.
1169 /* I am the "lli_opendir_pid" owner, only me can set "lli_sai". */
1170 rc = is_first_dirent(dir, *dentryp);
1171 if (rc == LS_NONE_FIRST_DE)
1172 /* It is not "ls -{a}l" operation, no need statahead for it. */
1173 GOTO(out, rc = -EAGAIN);
1175 sai = ll_sai_alloc();
1177 GOTO(out, rc = -ENOMEM);
1179 sai->sai_ls_all = (rc == LS_FIRST_DOT_DE);
1180 sai->sai_inode = igrab(dir);
1181 if (unlikely(sai->sai_inode == NULL)) {
1182 CWARN("Do not start stat ahead on dying inode "DFID"\n",
1183 PFID(&lli->lli_fid));
1185 GOTO(out, rc = -ESTALE);
1188 /* get parent reference count here, and put it in ll_statahead_thread */
1189 parent = dget((*dentryp)->d_parent);
1190 if (unlikely(sai->sai_inode != parent->d_inode)) {
1191 struct ll_inode_info *nlli = ll_i2info(parent->d_inode);
1193 CWARN("Race condition, someone changed %.*s just now: "
1194 "old parent "DFID", new parent "DFID"\n",
1195 (*dentryp)->d_name.len, (*dentryp)->d_name.name,
1196 PFID(&lli->lli_fid), PFID(&nlli->lli_fid));
1198 iput(sai->sai_inode);
1204 rc = cfs_create_thread(ll_statahead_thread, parent, 0);
1206 CERROR("can't start ll_sa thread, rc: %d\n", rc);
1208 lli->lli_opendir_key = NULL;
1209 sai->sai_thread.t_flags = SVC_STOPPED;
1211 LASSERT(lli->lli_sai == NULL);
1215 l_wait_event(sai->sai_thread.t_ctl_waitq,
1216 sa_is_running(sai) || sa_is_stopped(sai),
1220 * We don't stat-ahead for the first dirent since we are already in
1221 * lookup, and -EEXIST also indicates that this is the first dirent.
1226 cfs_spin_lock(&lli->lli_sa_lock);
1227 lli->lli_opendir_key = NULL;
1228 lli->lli_opendir_pid = 0;
1229 cfs_spin_unlock(&lli->lli_sa_lock);
1234 * update hit/miss count.
1236 void ll_statahead_exit(struct inode *dir, struct dentry *dentry, int result)
1238 struct ll_inode_info *lli;
1239 struct ll_statahead_info *sai;
1240 struct ll_sb_info *sbi;
1241 struct ll_dentry_data *ldd = ll_d2d(dentry);
1245 LASSERT(dir != NULL);
1246 lli = ll_i2info(dir);
1247 LASSERT(lli->lli_opendir_pid == cfs_curproc_pid());
1249 LASSERT(sai != NULL);
1250 sbi = ll_i2sbi(dir);
1252 rc = ll_sai_entry_fini(sai);
1253 /* rc == -ENOENT means such dentry was removed just between statahead
1254 * readdir and pre-fetched, count it as hit.
1256 * result == -ENOENT has two meanings:
1257 * 1. such dentry was removed just between statahead pre-fetched and
1258 * main process stat such dentry.
1259 * 2. main process stat non-exist dentry.
1260 * We can not distinguish such two cases, just count them as miss. */
1261 if (result >= 1 || unlikely(rc == -ENOENT)) {
1263 sai->sai_consecutive_miss = 0;
1264 sai->sai_max = min(2 * sai->sai_max, sbi->ll_sa_max);
1267 sai->sai_consecutive_miss++;
1268 if (sa_low_hit(sai) && sa_is_running(sai)) {
1269 atomic_inc(&sbi->ll_sa_wrong);
1270 CDEBUG(D_READA, "Statahead for dir "DFID" hit ratio "
1271 "too low: hit/miss %u/%u, sent/replied %u/%u, "
1272 "stopping statahead thread: pid %d\n",
1273 PFID(&lli->lli_fid), sai->sai_hit,
1274 sai->sai_miss, sai->sai_sent,
1275 sai->sai_replied, cfs_curproc_pid());
1276 cfs_spin_lock(&lli->lli_sa_lock);
1277 if (!sa_is_stopped(sai))
1278 sai->sai_thread.t_flags = SVC_STOPPING;
1279 cfs_spin_unlock(&lli->lli_sa_lock);
1283 if (!sa_is_stopped(sai))
1284 cfs_waitq_signal(&sai->sai_thread.t_ctl_waitq);
1285 if (likely(ldd != NULL))
1286 ldd->lld_sa_generation = sai->sai_generation;