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.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 #include <linux/sched.h>
40 #include <linux/smp_lock.h>
41 #include <linux/highmem.h>
42 #include <linux/pagemap.h>
44 #define DEBUG_SUBSYSTEM S_LLITE
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"
54 unsigned int se_index;
56 struct ptlrpc_request *se_req;
57 struct md_enqueue_info *se_minfo;
58 struct dentry *se_dentry;
59 struct inode *se_inode;
63 SA_ENTRY_UNSTATED = 0,
67 static unsigned int sai_generation = 0;
68 static cfs_spinlock_t sai_generation_lock = CFS_SPIN_LOCK_UNLOCKED;
71 * Check whether first entry was stated already or not.
72 * No need to hold lli_sa_lock, for:
73 * (1) it is me that remove entry from the list
74 * (2) the statahead thread only add new entry to the list
76 static int ll_sai_entry_stated(struct ll_statahead_info *sai)
78 struct ll_sai_entry *entry;
81 if (!cfs_list_empty(&sai->sai_entries_stated)) {
82 entry = cfs_list_entry(sai->sai_entries_stated.next,
83 struct ll_sai_entry, se_list);
84 if (entry->se_index == sai->sai_index_next)
90 static inline int sa_received_empty(struct ll_statahead_info *sai)
92 return cfs_list_empty(&sai->sai_entries_received);
95 static inline int sa_not_full(struct ll_statahead_info *sai)
97 return (sai->sai_index < sai->sai_hit + sai->sai_miss + sai->sai_max);
100 static inline int sa_is_running(struct ll_statahead_info *sai)
102 return !!(sai->sai_thread.t_flags & SVC_RUNNING);
105 static inline int sa_is_stopping(struct ll_statahead_info *sai)
107 return !!(sai->sai_thread.t_flags & SVC_STOPPING);
110 static inline int sa_is_stopped(struct ll_statahead_info *sai)
112 return !!(sai->sai_thread.t_flags & SVC_STOPPED);
116 * (1) hit ratio less than 80%
118 * (2) consecutive miss more than 8
120 static inline int sa_low_hit(struct ll_statahead_info *sai)
122 return ((sai->sai_hit > 7 && sai->sai_hit < 4 * sai->sai_miss) ||
123 (sai->sai_consecutive_miss > 8));
126 static inline int sa_skip_nolock(struct ll_statahead_info *sai)
128 return (sai->sai_nolock >= 3);
131 static void ll_sai_entry_free(struct ll_sai_entry *entry)
133 struct dentry *dentry = entry->se_dentry;
134 struct inode *inode = entry->se_inode;
137 struct ll_dentry_data *lld = ll_d2d(dentry);
138 struct ll_inode_info *lli;
140 entry->se_dentry = NULL;
141 LASSERT(inode != NULL);
142 lli = ll_i2info(inode);
143 if (!cfs_list_empty(&lli->lli_sa_dentry)) {
144 cfs_spin_lock(&lli->lli_sa_lock);
145 cfs_list_del_init(&lld->lld_sa_alias);
146 cfs_spin_unlock(&lli->lli_sa_lock);
151 entry->se_inode = NULL;
154 LASSERT(cfs_list_empty(&entry->se_list));
159 * process the deleted entry's member and free the entry.
161 * (2) free md_enqueue_info
162 * (3) drop dentry's ref count
163 * (4) release request's ref count
165 static void ll_sai_entry_cleanup(struct ll_sai_entry *entry, int free)
167 struct md_enqueue_info *minfo = entry->se_minfo;
168 struct ptlrpc_request *req = entry->se_req;
172 entry->se_minfo = NULL;
173 ll_intent_release(&minfo->mi_it);
174 dput(minfo->mi_dentry);
179 entry->se_req = NULL;
180 ptlrpc_req_finished(req);
183 ll_sai_entry_free(entry);
188 static struct ll_statahead_info *ll_sai_alloc(void)
190 struct ll_statahead_info *sai;
196 cfs_spin_lock(&sai_generation_lock);
197 sai->sai_generation = ++sai_generation;
198 if (unlikely(sai_generation == 0))
199 sai->sai_generation = ++sai_generation;
200 cfs_spin_unlock(&sai_generation_lock);
201 cfs_atomic_set(&sai->sai_refcount, 1);
202 sai->sai_max = LL_SA_RPC_MIN;
203 cfs_waitq_init(&sai->sai_waitq);
204 cfs_waitq_init(&sai->sai_thread.t_ctl_waitq);
205 CFS_INIT_LIST_HEAD(&sai->sai_entries_sent);
206 CFS_INIT_LIST_HEAD(&sai->sai_entries_received);
207 CFS_INIT_LIST_HEAD(&sai->sai_entries_stated);
212 struct ll_statahead_info *ll_sai_get(struct ll_statahead_info *sai)
215 cfs_atomic_inc(&sai->sai_refcount);
219 static void ll_sai_put(struct ll_statahead_info *sai)
221 struct inode *inode = sai->sai_inode;
222 struct ll_inode_info *lli;
225 LASSERT(inode != NULL);
226 lli = ll_i2info(inode);
227 LASSERT(lli->lli_sai == sai);
229 if (cfs_atomic_dec_and_test(&sai->sai_refcount)) {
230 struct ll_sai_entry *entry, *next;
232 cfs_spin_lock(&lli->lli_sa_lock);
233 if (unlikely(cfs_atomic_read(&sai->sai_refcount) > 0)) {
234 /* It is race case, the interpret callback just hold
235 * a reference count */
236 cfs_spin_unlock(&lli->lli_sa_lock);
241 LASSERT(lli->lli_opendir_key == NULL);
243 lli->lli_opendir_pid = 0;
244 cfs_spin_unlock(&lli->lli_sa_lock);
246 LASSERT(sa_is_stopped(sai));
248 if (sai->sai_sent > sai->sai_replied)
249 CDEBUG(D_READA,"statahead for dir "DFID" does not "
250 "finish: [sent:%u] [replied:%u]\n",
252 sai->sai_sent, sai->sai_replied);
254 cfs_list_for_each_entry_safe(entry, next,
255 &sai->sai_entries_sent, se_list) {
256 cfs_list_del_init(&entry->se_list);
257 ll_sai_entry_cleanup(entry, 1);
259 cfs_list_for_each_entry_safe(entry, next,
260 &sai->sai_entries_received,
262 cfs_list_del_init(&entry->se_list);
263 ll_sai_entry_cleanup(entry, 1);
265 cfs_list_for_each_entry_safe(entry, next,
266 &sai->sai_entries_stated,
268 cfs_list_del_init(&entry->se_list);
269 ll_sai_entry_cleanup(entry, 1);
278 * insert it into sai_entries_sent tail when init.
280 static struct ll_sai_entry *
281 ll_sai_entry_init(struct ll_statahead_info *sai, unsigned int index)
283 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
284 struct ll_sai_entry *entry;
287 OBD_ALLOC_PTR(entry);
289 RETURN(ERR_PTR(-ENOMEM));
291 CDEBUG(D_READA, "alloc sai entry %p index %u\n",
293 entry->se_index = index;
294 entry->se_stat = SA_ENTRY_UNSTATED;
296 cfs_spin_lock(&lli->lli_sa_lock);
297 cfs_list_add_tail(&entry->se_list, &sai->sai_entries_sent);
298 cfs_spin_unlock(&lli->lli_sa_lock);
304 * delete it from sai_entries_stated head when fini, it need not
305 * to process entry's member.
307 static int ll_sai_entry_fini(struct ll_statahead_info *sai)
309 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
310 struct ll_sai_entry *entry;
314 cfs_spin_lock(&lli->lli_sa_lock);
315 sai->sai_index_next++;
316 if (likely(!cfs_list_empty(&sai->sai_entries_stated))) {
317 entry = cfs_list_entry(sai->sai_entries_stated.next,
318 struct ll_sai_entry, se_list);
319 if (entry->se_index < sai->sai_index_next) {
320 cfs_list_del_init(&entry->se_list);
322 ll_sai_entry_free(entry);
325 LASSERT(sa_is_stopped(sai));
327 cfs_spin_unlock(&lli->lli_sa_lock);
333 * inside lli_sa_lock.
334 * \retval NULL : can not find the entry in sai_entries_sent with the index
335 * \retval entry: find the entry in sai_entries_sent with the index
337 static struct ll_sai_entry *
338 ll_sai_entry_set(struct ll_statahead_info *sai, unsigned int index, int stat,
339 struct ptlrpc_request *req, struct md_enqueue_info *minfo)
341 struct ll_sai_entry *entry;
344 if (!cfs_list_empty(&sai->sai_entries_sent)) {
345 cfs_list_for_each_entry(entry, &sai->sai_entries_sent,
347 if (entry->se_index == index) {
348 entry->se_stat = stat;
349 entry->se_req = ptlrpc_request_addref(req);
350 entry->se_minfo = minfo;
352 } else if (entry->se_index > index) {
361 * inside lli_sa_lock.
362 * Move entry to sai_entries_received and
363 * insert it into sai_entries_received tail.
366 ll_sai_entry_to_received(struct ll_statahead_info *sai, struct ll_sai_entry *entry)
368 if (!cfs_list_empty(&entry->se_list))
369 cfs_list_del_init(&entry->se_list);
370 cfs_list_add_tail(&entry->se_list, &sai->sai_entries_received);
374 * Move entry to sai_entries_stated and
375 * sort with the index.
378 ll_sai_entry_to_stated(struct ll_statahead_info *sai, struct ll_sai_entry *entry)
380 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
381 struct ll_sai_entry *se;
384 ll_sai_entry_cleanup(entry, 0);
386 cfs_spin_lock(&lli->lli_sa_lock);
387 if (!cfs_list_empty(&entry->se_list))
388 cfs_list_del_init(&entry->se_list);
391 if (unlikely(entry->se_index < sai->sai_index_next)) {
392 cfs_spin_unlock(&lli->lli_sa_lock);
393 ll_sai_entry_free(entry);
397 cfs_list_for_each_entry_reverse(se, &sai->sai_entries_stated, se_list) {
398 if (se->se_index < entry->se_index) {
399 cfs_list_add(&entry->se_list, &se->se_list);
400 cfs_spin_unlock(&lli->lli_sa_lock);
406 * I am the first entry.
408 cfs_list_add(&entry->se_list, &sai->sai_entries_stated);
409 cfs_spin_unlock(&lli->lli_sa_lock);
414 * finish lookup/revalidate.
416 static int do_statahead_interpret(struct ll_statahead_info *sai)
418 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
419 struct ll_sai_entry *entry;
420 struct ptlrpc_request *req;
421 struct md_enqueue_info *minfo;
422 struct lookup_intent *it;
423 struct dentry *dentry;
425 struct mdt_body *body;
428 cfs_spin_lock(&lli->lli_sa_lock);
429 LASSERT(!sa_received_empty(sai));
430 entry = cfs_list_entry(sai->sai_entries_received.next,
431 struct ll_sai_entry, se_list);
432 cfs_list_del_init(&entry->se_list);
433 cfs_spin_unlock(&lli->lli_sa_lock);
435 if (unlikely(entry->se_index < sai->sai_index_next)) {
436 CWARN("Found stale entry: [index %u] [next %u]\n",
437 entry->se_index, sai->sai_index_next);
438 ll_sai_entry_cleanup(entry, 1);
442 if (entry->se_stat != SA_ENTRY_STATED)
443 GOTO(out, rc = entry->se_stat);
446 minfo = entry->se_minfo;
448 dentry = minfo->mi_dentry;
450 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
452 GOTO(out, rc = -EFAULT);
454 if (dentry->d_inode == NULL) {
458 struct dentry *save = dentry;
460 struct it_cb_data icbd = {
461 .icbd_parent = minfo->mi_dir,
462 .icbd_childp = &dentry,
463 .icbd_alias = &entry->se_inode,
467 LASSERT(fid_is_zero(&minfo->mi_data.op_fid2));
469 /* XXX: No fid in reply, this is probaly cross-ref case.
470 * SA can't handle it yet. */
471 if (body->valid & OBD_MD_MDS)
472 GOTO(out, rc = -EAGAIN);
474 /* Here dentry->d_inode might be NULL, because the entry may
475 * have been removed before we start doing stat ahead. */
477 /* BUG 15962, 21739: since statahead thread does not hold
478 * parent's i_mutex, it can not alias the dentry to inode.
479 * Here we just create/update inode in memory, and let the
480 * main "ls -l" thread to alias such dentry to the inode with
481 * parent's i_mutex held.
482 * On the other hand, we hold ldlm ibits lock for the inode
483 * yet, to allow other operations to cancel such lock in time,
484 * we should drop the ldlm lock reference count, then the main
485 * "ls -l" thread should check/get such ldlm ibits lock before
486 * aliasing such dentry to the inode later. If we don't do such
487 * drop here, it maybe cause deadlock with i_muext held by
488 * others, just like bug 21739. */
489 rc = ll_lookup_it_finish(req, it, &icbd);
490 if (entry->se_inode != NULL) {
491 struct ll_dentry_data *lld = ll_d2d(dentry);
492 struct ll_inode_info *sei = ll_i2info(entry->se_inode);
494 /* For statahead lookup case, both MDS_INODELOCK_LOOKUP
495 * and MDS_INODELOCK_UPDATE should be granted */
496 if (likely(bits & MDS_INODELOCK_LOOKUP &&
497 bits & MDS_INODELOCK_UPDATE)) {
498 /* the first dentry ref_count will be dropped by
499 * ll_sai_entry_to_stated(), so hold another ref
501 entry->se_dentry = dget(dentry);
502 cfs_spin_lock(&sei->lli_sa_lock);
503 cfs_list_add(&lld->lld_sa_alias,
504 &sei->lli_sa_dentry);
505 cfs_spin_unlock(&sei->lli_sa_lock);
508 iput(entry->se_inode);
509 entry->se_inode = NULL;
513 LASSERT(dentry == save);
514 ll_intent_drop_lock(it);
519 if (!lu_fid_eq(&minfo->mi_data.op_fid2, &body->fid1)) {
520 ll_unhash_aliases(dentry->d_inode);
521 GOTO(out, rc = -EAGAIN);
524 rc = ll_revalidate_it_finish(req, it, dentry);
526 ll_unhash_aliases(dentry->d_inode);
530 cfs_spin_lock(&ll_lookup_lock);
531 spin_lock(&dcache_lock);
534 dentry->d_flags &= ~DCACHE_LUSTRE_INVALID;
535 unlock_dentry(dentry);
536 d_rehash_cond(dentry, 0);
537 spin_unlock(&dcache_lock);
538 cfs_spin_unlock(&ll_lookup_lock);
540 ll_lookup_finish_locks(it, dentry);
545 /* The "ll_sai_entry_to_stated()" will drop related ldlm ibits lock
546 * reference count with ll_intent_drop_lock() called in spite of the
547 * above operations failed or not. Do not worry about calling
548 * "ll_intent_drop_lock()" more than once. */
549 if (likely(ll_sai_entry_to_stated(sai, entry)))
550 cfs_waitq_signal(&sai->sai_waitq);
554 static int ll_statahead_interpret(struct ptlrpc_request *req,
555 struct md_enqueue_info *minfo,
558 struct lookup_intent *it = &minfo->mi_it;
559 struct dentry *dentry = minfo->mi_dentry;
560 struct inode *dir = minfo->mi_dir;
561 struct ll_inode_info *lli = ll_i2info(dir);
562 struct ll_statahead_info *sai;
563 struct ll_sai_entry *entry;
566 CDEBUG(D_READA, "interpret statahead %.*s rc %d\n",
567 dentry->d_name.len, dentry->d_name.name, rc);
569 cfs_spin_lock(&lli->lli_sa_lock);
571 if (unlikely(lli->lli_sai == NULL ||
572 lli->lli_sai->sai_generation != minfo->mi_generation)) {
573 cfs_spin_unlock(&lli->lli_sa_lock);
574 ll_intent_release(it);
580 sai = ll_sai_get(lli->lli_sai);
581 entry = ll_sai_entry_set(sai,
582 (unsigned int)(long)minfo->mi_cbdata,
583 rc < 0 ? rc : SA_ENTRY_STATED, req,
585 LASSERT(entry != NULL);
586 if (likely(sa_is_running(sai))) {
587 ll_sai_entry_to_received(sai, entry);
589 cfs_spin_unlock(&lli->lli_sa_lock);
590 cfs_waitq_signal(&sai->sai_thread.t_ctl_waitq);
592 if (!cfs_list_empty(&entry->se_list))
593 cfs_list_del_init(&entry->se_list);
595 cfs_spin_unlock(&lli->lli_sa_lock);
596 ll_sai_entry_cleanup(entry, 1);
603 static void sa_args_fini(struct md_enqueue_info *minfo,
604 struct ldlm_enqueue_info *einfo)
606 LASSERT(minfo && einfo);
608 capa_put(minfo->mi_data.op_capa1);
609 capa_put(minfo->mi_data.op_capa2);
615 * There is race condition between "capa_put" and "ll_statahead_interpret" for
616 * accessing "op_data.op_capa[1,2]" as following:
617 * "capa_put" releases "op_data.op_capa[1,2]"'s reference count after calling
618 * "md_intent_getattr_async". But "ll_statahead_interpret" maybe run first, and
619 * fill "op_data.op_capa[1,2]" as POISON, then cause "capa_put" access invalid
620 * "ocapa". So here reserve "op_data.op_capa[1,2]" in "pcapa" before calling
621 * "md_intent_getattr_async".
623 static int sa_args_init(struct inode *dir, struct dentry *dentry,
624 struct md_enqueue_info **pmi,
625 struct ldlm_enqueue_info **pei,
626 struct obd_capa **pcapa)
628 struct ll_inode_info *lli = ll_i2info(dir);
629 struct md_enqueue_info *minfo;
630 struct ldlm_enqueue_info *einfo;
631 struct md_op_data *op_data;
633 OBD_ALLOC_PTR(einfo);
637 OBD_ALLOC_PTR(minfo);
643 op_data = ll_prep_md_op_data(&minfo->mi_data, dir, dentry->d_inode,
644 dentry->d_name.name, dentry->d_name.len,
645 0, LUSTRE_OPC_ANY, NULL);
646 if (IS_ERR(op_data)) {
649 return PTR_ERR(op_data);
652 minfo->mi_it.it_op = IT_GETATTR;
653 minfo->mi_dentry = dentry;
654 minfo->mi_dir = igrab(dir);
655 minfo->mi_cb = ll_statahead_interpret;
656 minfo->mi_generation = lli->lli_sai->sai_generation;
657 minfo->mi_cbdata = (void *)(long)lli->lli_sai->sai_index;
659 einfo->ei_type = LDLM_IBITS;
660 einfo->ei_mode = it_to_lock_mode(&minfo->mi_it);
661 einfo->ei_cb_bl = ll_md_blocking_ast;
662 einfo->ei_cb_cp = ldlm_completion_ast;
663 einfo->ei_cb_gl = NULL;
664 einfo->ei_cbdata = NULL;
668 pcapa[0] = op_data->op_capa1;
669 pcapa[1] = op_data->op_capa2;
675 * similar to ll_lookup_it().
677 static int do_sa_lookup(struct inode *dir, struct dentry *dentry)
679 struct md_enqueue_info *minfo;
680 struct ldlm_enqueue_info *einfo;
681 struct obd_capa *capas[2];
685 rc = sa_args_init(dir, dentry, &minfo, &einfo, capas);
689 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
694 sa_args_fini(minfo, einfo);
701 * similar to ll_revalidate_it().
702 * \retval 1 -- dentry valid
703 * \retval 0 -- will send stat-ahead request
704 * \retval others -- prepare stat-ahead request failed
706 static int do_sa_revalidate(struct inode *dir, struct dentry *dentry)
708 struct inode *inode = dentry->d_inode;
709 struct lookup_intent it = { .it_op = IT_GETATTR };
710 struct md_enqueue_info *minfo;
711 struct ldlm_enqueue_info *einfo;
712 struct obd_capa *capas[2];
716 if (unlikely(inode == NULL))
719 if (d_mountpoint(dentry))
722 if (unlikely(dentry == dentry->d_sb->s_root))
725 rc = md_revalidate_lock(ll_i2mdexp(dir), &it, ll_inode2fid(inode),
728 ll_intent_release(&it);
732 rc = sa_args_init(dir, dentry, &minfo, &einfo, capas);
736 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
741 sa_args_fini(minfo, einfo);
747 static inline void ll_name2qstr(struct qstr *q, const char *name, int namelen)
751 q->hash = full_name_hash(name, namelen);
754 static int ll_statahead_one(struct dentry *parent, const char* entry_name,
757 struct inode *dir = parent->d_inode;
758 struct ll_inode_info *lli = ll_i2info(dir);
759 struct ll_statahead_info *sai = lli->lli_sai;
761 struct dentry *dentry;
762 struct ll_sai_entry *se;
766 if (parent->d_flags & DCACHE_LUSTRE_INVALID) {
767 CDEBUG(D_READA, "parent dentry@%p %.*s is "
768 "invalid, skip statahead\n",
769 parent, parent->d_name.len, parent->d_name.name);
773 se = ll_sai_entry_init(sai, sai->sai_index);
777 ll_name2qstr(&name, entry_name, entry_name_len);
778 dentry = d_lookup(parent, &name);
780 if (unlikely(sa_skip_nolock(sai))) {
781 CWARN("can not obtain lookup lock, skip the succeedent "
782 "lookup cases, will cause statahead miss, and "
783 "statahead maybe exit for that.\n");
784 GOTO(out, rc = -EAGAIN);
787 dentry = d_alloc(parent, &name);
789 rc = ll_dops_init(dentry, 1);
791 rc = do_sa_lookup(dir, dentry);
795 GOTO(out, rc = -ENOMEM);
798 rc = do_sa_revalidate(dir, dentry);
807 CDEBUG(D_READA, "set sai entry %p index %u stat %d rc %d\n",
808 se, se->se_index, se->se_stat, rc);
809 se->se_stat = rc < 0 ? rc : SA_ENTRY_STATED;
810 if (ll_sai_entry_to_stated(sai, se))
811 cfs_waitq_signal(&sai->sai_waitq);
820 static int ll_statahead_thread(void *arg)
822 struct dentry *parent = (struct dentry *)arg;
823 struct inode *dir = parent->d_inode;
824 struct ll_inode_info *lli = ll_i2info(dir);
825 struct ll_sb_info *sbi = ll_i2sbi(dir);
826 struct ll_statahead_info *sai = ll_sai_get(lli->lli_sai);
827 struct ptlrpc_thread *thread = &sai->sai_thread;
832 struct ll_dir_chain chain;
837 snprintf(pname, 15, "ll_sa_%u", lli->lli_opendir_pid);
838 cfs_daemonize(pname);
841 atomic_inc(&sbi->ll_sa_total);
842 cfs_spin_lock(&lli->lli_sa_lock);
843 thread->t_flags = SVC_RUNNING;
844 cfs_spin_unlock(&lli->lli_sa_lock);
845 cfs_waitq_signal(&thread->t_ctl_waitq);
846 CDEBUG(D_READA, "start doing statahead for %s\n", parent->d_name.name);
848 ll_dir_chain_init(&chain);
849 page = ll_get_dir_page(dir, pos, 0, &chain);
852 struct l_wait_info lwi = { 0 };
853 struct lu_dirpage *dp;
854 struct lu_dirent *ent;
858 CDEBUG(D_READA, "error reading dir "DFID" at "LPU64
859 "/%u: [rc %d] [parent %u]\n",
860 PFID(ll_inode2fid(dir)), pos, sai->sai_index,
861 rc, lli->lli_opendir_pid);
865 dp = page_address(page);
866 for (ent = lu_dirent_start(dp); ent != NULL;
867 ent = lu_dirent_next(ent)) {
868 char *name = ent->lde_name;
869 int namelen = le16_to_cpu(ent->lde_namelen);
871 if (unlikely(namelen == 0))
877 if (name[0] == '.') {
883 } else if (name[1] == '.' && namelen == 2) {
888 } else if (!sai->sai_ls_all) {
892 sai->sai_skip_hidden++;
898 * don't stat-ahead first entry.
900 if (unlikely(!first)) {
906 l_wait_event(thread->t_ctl_waitq,
907 !sa_is_running(sai) || sa_not_full(sai) ||
908 !sa_received_empty(sai),
911 while (!sa_received_empty(sai) && sa_is_running(sai))
912 do_statahead_interpret(sai);
914 if (unlikely(!sa_is_running(sai))) {
919 if (!sa_not_full(sai))
921 * do not skip the current de.
925 rc = ll_statahead_one(parent, name, namelen);
931 pos = le64_to_cpu(dp->ldp_hash_end);
933 if (pos == DIR_END_OFF) {
935 * End of directory reached.
938 l_wait_event(thread->t_ctl_waitq,
939 !sa_is_running(sai) ||
940 !sa_received_empty(sai) ||
941 sai->sai_sent == sai->sai_replied,
943 if (!sa_received_empty(sai) &&
945 do_statahead_interpret(sai);
951 * chain is exhausted.
952 * Normal case: continue to the next page.
954 page = ll_get_dir_page(dir, pos, 1, &chain);
957 * go into overflow page.
964 ll_dir_chain_fini(&chain);
965 cfs_spin_lock(&lli->lli_sa_lock);
966 thread->t_flags = SVC_STOPPED;
967 cfs_spin_unlock(&lli->lli_sa_lock);
968 cfs_waitq_signal(&sai->sai_waitq);
969 cfs_waitq_signal(&thread->t_ctl_waitq);
972 CDEBUG(D_READA, "statahead thread stopped, pid %d\n",
978 * called in ll_file_release().
980 void ll_stop_statahead(struct inode *inode, void *key)
982 struct ll_inode_info *lli = ll_i2info(inode);
984 if (unlikely(key == NULL))
987 cfs_spin_lock(&lli->lli_sa_lock);
988 if (lli->lli_opendir_key != key || lli->lli_opendir_pid == 0) {
989 cfs_spin_unlock(&lli->lli_sa_lock);
993 lli->lli_opendir_key = NULL;
996 struct l_wait_info lwi = { 0 };
997 struct ptlrpc_thread *thread = &lli->lli_sai->sai_thread;
999 if (!sa_is_stopped(lli->lli_sai)) {
1000 thread->t_flags = SVC_STOPPING;
1001 cfs_spin_unlock(&lli->lli_sa_lock);
1002 cfs_waitq_signal(&thread->t_ctl_waitq);
1004 CDEBUG(D_READA, "stopping statahead thread, pid %d\n",
1006 l_wait_event(thread->t_ctl_waitq,
1007 sa_is_stopped(lli->lli_sai),
1010 cfs_spin_unlock(&lli->lli_sa_lock);
1014 * Put the ref which was held when first statahead_enter.
1015 * It maybe not the last ref for some statahead requests
1018 ll_sai_put(lli->lli_sai);
1020 lli->lli_opendir_pid = 0;
1021 cfs_spin_unlock(&lli->lli_sa_lock);
1027 * not first dirent, or is "."
1029 LS_NONE_FIRST_DE = 0,
1031 * the first non-hidden dirent
1035 * the first hidden dirent, that is "."
1040 static int is_first_dirent(struct inode *dir, struct dentry *dentry)
1042 struct ll_dir_chain chain;
1043 struct qstr *target = &dentry->d_name;
1047 int rc = LS_NONE_FIRST_DE;
1050 ll_dir_chain_init(&chain);
1051 page = ll_get_dir_page(dir, pos, 0, &chain);
1054 struct lu_dirpage *dp;
1055 struct lu_dirent *ent;
1058 struct ll_inode_info *lli = ll_i2info(dir);
1061 CERROR("error reading dir "DFID" at "LPU64": "
1062 "[rc %d] [parent %u]\n",
1063 PFID(ll_inode2fid(dir)), pos,
1064 rc, lli->lli_opendir_pid);
1068 dp = page_address(page);
1069 for (ent = lu_dirent_start(dp); ent != NULL;
1070 ent = lu_dirent_next(ent)) {
1071 char *name = ent->lde_name;
1072 int namelen = le16_to_cpu(ent->lde_namelen);
1076 * skip dummy record.
1080 if (name[0] == '.') {
1086 else if (name[1] == '.' && namelen == 2)
1097 if (dot_de && target->name[0] != '.') {
1098 CDEBUG(D_READA, "%.*s skip hidden file %.*s\n",
1099 target->len, target->name,
1104 if (target->len != namelen ||
1105 memcmp(target->name, name, namelen) != 0)
1106 rc = LS_NONE_FIRST_DE;
1110 rc = LS_FIRST_DOT_DE;
1115 pos = le64_to_cpu(dp->ldp_hash_end);
1117 if (pos == DIR_END_OFF) {
1119 * End of directory reached.
1124 * chain is exhausted
1125 * Normal case: continue to the next page.
1127 page = ll_get_dir_page(dir, pos, 1, &chain);
1130 * go into overflow page.
1137 ll_dir_chain_fini(&chain);
1142 * tgt: the dentry to be revalidate or lookup
1143 * new: the dentry created by statahead
1145 static int is_same_dentry(struct dentry *tgt, struct dentry *new, int lookup)
1148 LASSERT(lookup == 0);
1151 if (tgt->d_parent != new->d_parent)
1153 if (tgt->d_name.hash != new->d_name.hash)
1155 if (tgt->d_name.len != new->d_name.len)
1157 if (memcmp(tgt->d_name.name, new->d_name.name, tgt->d_name.len) != 0)
1159 if (tgt->d_inode == NULL && lookup)
1162 LASSERTF(tgt->d_flags & DCACHE_LUSTRE_INVALID,
1163 "[%.*s/%.*s] [%x %p "DFID"] [%x %p "DFID"]\n",
1164 tgt->d_parent->d_name.len, tgt->d_parent->d_name.name,
1165 tgt->d_name.len, tgt->d_name.name,
1166 tgt->d_flags, tgt, PFID(ll_inode2fid(tgt->d_inode)),
1167 new->d_flags, new, PFID(ll_inode2fid(new->d_inode)));
1169 LASSERTF(tgt->d_flags & DCACHE_LUSTRE_INVALID,
1170 "[%.*s/%.*s] [%x %p 0] [%x %p "DFID"]\n",
1171 tgt->d_parent->d_name.len, tgt->d_parent->d_name.name,
1172 tgt->d_name.len, tgt->d_name.name,
1174 new->d_flags, new, PFID(ll_inode2fid(new->d_inode)));
1179 * Start statahead thread if this is the first dir entry.
1180 * Otherwise if a thread is started already, wait it until it is ahead of me.
1181 * \retval 0 -- stat ahead thread process such dentry, miss for lookup
1182 * \retval 1 -- stat ahead thread process such dentry, hit for any case
1183 * \retval -EEXIST -- stat ahead thread started, and this is the first dentry
1184 * \retval -EBADFD -- statahead thread exit and not dentry available
1185 * \retval -EAGAIN -- try to stat by caller
1186 * \retval others -- error
1188 int do_statahead_enter(struct inode *dir, struct dentry **dentryp, int lookup)
1190 struct ll_inode_info *lli;
1191 struct ll_statahead_info *sai;
1192 struct dentry *parent;
1193 struct l_wait_info lwi = { 0 };
1197 LASSERT(dir != NULL);
1198 lli = ll_i2info(dir);
1199 LASSERT(lli->lli_opendir_pid == cfs_curproc_pid());
1203 if (unlikely(sa_is_stopped(sai) &&
1204 cfs_list_empty(&sai->sai_entries_stated)))
1207 if ((*dentryp)->d_name.name[0] == '.') {
1208 if (likely(sai->sai_ls_all ||
1209 sai->sai_miss_hidden >= sai->sai_skip_hidden)) {
1211 * Hidden dentry is the first one, or statahead
1212 * thread does not skip so many hidden dentries
1213 * before "sai_ls_all" enabled as below.
1216 if (!sai->sai_ls_all)
1218 * It maybe because hidden dentry is not
1219 * the first one, "sai_ls_all" was not
1220 * set, then "ls -al" missed. Enable
1221 * "sai_ls_all" for such case.
1223 sai->sai_ls_all = 1;
1226 * Such "getattr" has been skipped before
1227 * "sai_ls_all" enabled as above.
1229 sai->sai_miss_hidden++;
1234 if (!ll_sai_entry_stated(sai)) {
1236 * thread started already, avoid double-stat.
1238 lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
1239 rc = l_wait_event(sai->sai_waitq,
1240 ll_sai_entry_stated(sai) ||
1243 if (unlikely(rc == -EINTR))
1247 if (ll_sai_entry_stated(sai)) {
1248 struct ll_sai_entry *entry;
1250 entry = cfs_list_entry(sai->sai_entries_stated.next,
1251 struct ll_sai_entry, se_list);
1252 /* This is for statahead lookup */
1253 if (entry->se_inode != NULL) {
1254 struct lookup_intent it = {.it_op = IT_LOOKUP};
1255 struct dentry *dchild = entry->se_dentry;
1256 struct inode *ichild = entry->se_inode;
1257 struct ll_dentry_data *lld = ll_d2d(dchild);
1258 struct ll_inode_info *sei = ll_i2info(ichild);
1259 struct dentry *save = dchild;
1261 __u32 bits = MDS_INODELOCK_LOOKUP |
1262 MDS_INODELOCK_UPDATE;
1265 LASSERT(dchild != *dentryp);
1268 mutex_lock(&dir->i_mutex);
1271 * Make sure dentry is still valid.
1272 * For statahead lookup case, we need both
1273 * LOOKUP lock and UPDATE lock which obtained
1274 * by statahead thread originally.
1276 * Consider following racer case:
1277 * 1. statahead thread on client1 get lock with
1278 * both LOOKUK and UPDATE bits for "aaa"
1279 * 2. rename thread on client2 cancel such lock
1280 * from client1, then rename "aaa" to "bbb"
1281 * 3. ls thread on client1 obtain LOOKUP lock
1283 * 4. here the dentry "aaa" created by statahead
1284 * thread should be invalid even related
1285 * LOOKUP lock valid for the same inode
1287 rc = md_revalidate_lock(ll_i2mdexp(dir), &it,
1288 ll_inode2fid(ichild),
1290 cfs_spin_lock(&sei->lli_sa_lock);
1291 if (!cfs_list_empty(&lld->lld_sa_alias))
1292 cfs_list_del_init(&lld->lld_sa_alias);
1295 cfs_spin_unlock(&sei->lli_sa_lock);
1297 /* Someone has cancelled the original
1298 * lock before the real "revalidate"
1299 * using it. Drop it. */
1303 /* Someone has cancelled the original
1304 * lock, and reobtained it, the dentry
1305 * maybe invalid anymore, Drop it. */
1306 ll_intent_drop_lock(&it);
1310 ll_lookup_it_alias(&dchild, ichild, bits);
1311 found = is_same_dentry(*dentryp, dchild, lookup);
1312 ll_lookup_finish_locks(&it, dchild);
1319 mutex_unlock(&dir->i_mutex);
1320 /* Drop the inode reference count held by
1325 entry->se_dentry = NULL;
1326 entry->se_inode = NULL;
1329 LASSERT(*dentryp != dchild);
1330 /* VFS will drop the reference
1331 * count for dchild and *dentryp
1335 LASSERT(*dentryp == dchild);
1336 /* Drop the dentry reference
1337 * count held by statahead. */
1342 /* Drop the dentry reference count held
1350 struct dentry *result;
1352 result = d_lookup((*dentryp)->d_parent,
1353 &(*dentryp)->d_name);
1355 LASSERT(result != *dentryp);
1356 /* BUG 16303: do not drop reference count for
1357 * "*dentryp", VFS will do that by itself. */
1363 * do nothing for revalidate.
1368 /* I am the "lli_opendir_pid" owner, only me can set "lli_sai". */
1369 rc = is_first_dirent(dir, *dentryp);
1370 if (rc == LS_NONE_FIRST_DE)
1371 /* It is not "ls -{a}l" operation, no need statahead for it. */
1372 GOTO(out, rc = -EAGAIN);
1374 sai = ll_sai_alloc();
1376 GOTO(out, rc = -ENOMEM);
1378 sai->sai_ls_all = (rc == LS_FIRST_DOT_DE);
1379 sai->sai_inode = igrab(dir);
1380 if (unlikely(sai->sai_inode == NULL)) {
1381 CWARN("Do not start stat ahead on dying inode "DFID"\n",
1382 PFID(&lli->lli_fid));
1384 GOTO(out, rc = -ESTALE);
1387 /* get parent reference count here, and put it in ll_statahead_thread */
1388 parent = dget((*dentryp)->d_parent);
1389 if (unlikely(sai->sai_inode != parent->d_inode)) {
1390 struct ll_inode_info *nlli = ll_i2info(parent->d_inode);
1392 CWARN("Race condition, someone changed %.*s just now: "
1393 "old parent "DFID", new parent "DFID"\n",
1394 (*dentryp)->d_name.len, (*dentryp)->d_name.name,
1395 PFID(&lli->lli_fid), PFID(&nlli->lli_fid));
1397 iput(sai->sai_inode);
1403 rc = cfs_kernel_thread(ll_statahead_thread, parent, 0);
1405 CERROR("can't start ll_sa thread, rc: %d\n", rc);
1407 lli->lli_opendir_key = NULL;
1408 sai->sai_thread.t_flags = SVC_STOPPED;
1410 LASSERT(lli->lli_sai == NULL);
1414 l_wait_event(sai->sai_thread.t_ctl_waitq,
1415 sa_is_running(sai) || sa_is_stopped(sai),
1419 * We don't stat-ahead for the first dirent since we are already in
1420 * lookup, and -EEXIST also indicates that this is the first dirent.
1425 cfs_spin_lock(&lli->lli_sa_lock);
1426 lli->lli_opendir_key = NULL;
1427 lli->lli_opendir_pid = 0;
1428 cfs_spin_unlock(&lli->lli_sa_lock);
1433 * update hit/miss count.
1435 void ll_statahead_exit(struct inode *dir, struct dentry *dentry, int result)
1437 struct ll_inode_info *lli;
1438 struct ll_statahead_info *sai;
1439 struct ll_sb_info *sbi;
1440 struct ll_dentry_data *ldd = ll_d2d(dentry);
1444 LASSERT(dir != NULL);
1445 lli = ll_i2info(dir);
1446 LASSERT(lli->lli_opendir_pid == cfs_curproc_pid());
1448 LASSERT(sai != NULL);
1449 sbi = ll_i2sbi(dir);
1451 rc = ll_sai_entry_fini(sai);
1452 /* rc == -ENOENT means such dentry was removed just between statahead
1453 * readdir and pre-fetched, count it as hit.
1455 * result == -ENOENT has two meanings:
1456 * 1. such dentry was removed just between statahead pre-fetched and
1457 * main process stat such dentry.
1458 * 2. main process stat non-exist dentry.
1459 * We can not distinguish such two cases, just count them as miss. */
1460 if (result >= 1 || unlikely(rc == -ENOENT)) {
1462 sai->sai_consecutive_miss = 0;
1463 sai->sai_max = min(2 * sai->sai_max, sbi->ll_sa_max);
1466 sai->sai_consecutive_miss++;
1467 if (sa_low_hit(sai) && sa_is_running(sai)) {
1468 atomic_inc(&sbi->ll_sa_wrong);
1469 CDEBUG(D_READA, "Statahead for dir "DFID" hit ratio "
1470 "too low: hit/miss %u/%u, sent/replied %u/%u, "
1471 "stopping statahead thread: pid %d\n",
1472 PFID(&lli->lli_fid), sai->sai_hit,
1473 sai->sai_miss, sai->sai_sent,
1474 sai->sai_replied, cfs_curproc_pid());
1475 cfs_spin_lock(&lli->lli_sa_lock);
1476 if (!sa_is_stopped(sai))
1477 sai->sai_thread.t_flags = SVC_STOPPING;
1478 cfs_spin_unlock(&lli->lli_sa_lock);
1482 if (!sa_is_stopped(sai))
1483 cfs_waitq_signal(&sai->sai_thread.t_ctl_waitq);
1484 if (likely(ldd != NULL))
1485 ldd->lld_sa_generation = sai->sai_generation;