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"
55 #define SA_OMITTED_ENTRY_MAX 8ULL
58 /** negative values are for error cases */
59 SA_ENTRY_INIT = 0, /** init entry */
60 SA_ENTRY_SUCC = 1, /** stat succeed */
61 SA_ENTRY_INVA = 2, /** invalid entry */
62 SA_ENTRY_DEST = 3, /** entry to be destroyed */
66 /* link into sai->sai_entries_{sent,received,stated} */
68 /* link into sai hash table locally */
70 /* entry reference count */
71 cfs_atomic_t se_refcount;
72 /* entry index in the sai */
74 /* low layer ldlm lock handle */
78 /* entry size, contains name */
80 /* pointer to async getattr enqueue info */
81 struct md_enqueue_info *se_minfo;
82 /* pointer to the async getattr request */
83 struct ptlrpc_request *se_req;
84 /* pointer to the target inode */
85 struct inode *se_inode;
90 static unsigned int sai_generation = 0;
91 static cfs_spinlock_t sai_generation_lock = CFS_SPIN_LOCK_UNLOCKED;
93 static inline int ll_sa_entry_unlinked(struct ll_sa_entry *entry)
95 return cfs_list_empty(&entry->se_list);
98 static inline int ll_sa_entry_unhashed(struct ll_sa_entry *entry)
100 return cfs_list_empty(&entry->se_hash);
104 * The entry only can be released by the caller, it is necessary to hold lock.
106 static inline int ll_sa_entry_stated(struct ll_sa_entry *entry)
109 return (entry->se_stat != SA_ENTRY_INIT);
112 static inline int ll_sa_entry_hash(int val)
114 return val & LL_SA_CACHE_MASK;
118 * Insert entry to hash SA table.
121 ll_sa_entry_enhash(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
123 int i = ll_sa_entry_hash(entry->se_qstr.hash);
125 cfs_spin_lock(&sai->sai_cache_lock[i]);
126 cfs_list_add_tail(&entry->se_hash, &sai->sai_cache[i]);
127 cfs_spin_unlock(&sai->sai_cache_lock[i]);
131 * Remove entry from SA table.
134 ll_sa_entry_unhash(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
136 int i = ll_sa_entry_hash(entry->se_qstr.hash);
138 cfs_spin_lock(&sai->sai_cache_lock[i]);
139 cfs_list_del_init(&entry->se_hash);
140 cfs_spin_unlock(&sai->sai_cache_lock[i]);
143 static inline int sa_received_empty(struct ll_statahead_info *sai)
145 return cfs_list_empty(&sai->sai_entries_received);
148 static inline int sa_not_full(struct ll_statahead_info *sai)
150 return (cfs_atomic_read(&sai->sai_cache_count) < sai->sai_max);
154 * (1) hit ratio less than 80%
156 * (2) consecutive miss more than 8
157 * then means low hit.
159 static inline int sa_low_hit(struct ll_statahead_info *sai)
161 return ((sai->sai_hit > 7 && sai->sai_hit < 4 * sai->sai_miss) ||
162 (sai->sai_consecutive_miss > 8));
166 * If the given index is behind of statahead window more than
167 * SA_OMITTED_ENTRY_MAX, then it is old.
169 static inline int is_omitted_entry(struct ll_statahead_info *sai, __u64 index)
171 return ((__u64)sai->sai_max + index + SA_OMITTED_ENTRY_MAX <
176 * Insert it into sai_entries_sent tail when init.
178 static struct ll_sa_entry *
179 ll_sa_entry_alloc(struct ll_statahead_info *sai, __u64 index,
180 const char *name, int len)
182 struct ll_inode_info *lli;
183 struct ll_sa_entry *entry;
188 entry_size = sizeof(struct ll_sa_entry) + (len & ~3) + 4;
189 OBD_ALLOC(entry, entry_size);
190 if (unlikely(entry == NULL))
191 RETURN(ERR_PTR(-ENOMEM));
193 CDEBUG(D_READA, "alloc sai entry %.*s(%p) index "LPU64"\n",
194 len, name, entry, index);
196 entry->se_index = index;
199 * Statahead entry reference rules:
201 * 1) When statahead entry is initialized, its reference is set as 2.
202 * One reference is used by the directory scanner. When the scanner
203 * searches the statahead cache for the given name, it can perform
204 * lockless hash lookup (only the scanner can remove entry from hash
205 * list), and once found, it needn't to call "atomic_inc()" for the
206 * entry reference. So the performance is improved. After using the
207 * statahead entry, the scanner will call "atomic_dec()" to drop the
208 * reference held when initialization. If it is the last reference,
209 * the statahead entry will be freed.
211 * 2) All other threads, including statahead thread and ptlrpcd thread,
212 * when they process the statahead entry, the reference for target
213 * should be held to guarantee the entry will not be released by the
214 * directory scanner. After processing the entry, these threads will
215 * drop the entry reference. If it is the last reference, the entry
218 * The second reference when initializes the statahead entry is used
219 * by the statahead thread, following the rule 2).
221 cfs_atomic_set(&entry->se_refcount, 2);
222 entry->se_stat = SA_ENTRY_INIT;
223 entry->se_size = entry_size;
224 dname = (char *)entry + sizeof(struct ll_sa_entry);
225 memcpy(dname, name, len);
227 entry->se_qstr.hash = full_name_hash(name, len);
228 entry->se_qstr.len = len;
229 entry->se_qstr.name = dname;
231 lli = ll_i2info(sai->sai_inode);
232 cfs_spin_lock(&lli->lli_sa_lock);
233 cfs_list_add_tail(&entry->se_list, &sai->sai_entries_sent);
234 cfs_spin_unlock(&lli->lli_sa_lock);
236 cfs_atomic_inc(&sai->sai_cache_count);
237 ll_sa_entry_enhash(sai, entry);
243 * Used by the directory scanner to search entry with name.
245 * Only the caller can remove the entry from hash, so it is unnecessary to hold
246 * hash lock. It is caller's duty to release the init refcount on the entry, so
247 * it is also unnecessary to increase refcount on the entry.
249 static struct ll_sa_entry *
250 ll_sa_entry_get_byname(struct ll_statahead_info *sai, const struct qstr *qstr)
252 struct ll_sa_entry *entry;
253 int i = ll_sa_entry_hash(qstr->hash);
255 cfs_list_for_each_entry(entry, &sai->sai_cache[i], se_hash) {
256 if (entry->se_qstr.hash == qstr->hash &&
257 entry->se_qstr.len == qstr->len &&
258 memcmp(entry->se_qstr.name, qstr->name, qstr->len) == 0)
265 * Used by the async getattr request callback to find entry with index.
267 * Inside lli_sa_lock to prevent others to change the list during the search.
268 * It needs to increase entry refcount before returning to guarantee that the
269 * entry cannot be freed by others.
271 static struct ll_sa_entry *
272 ll_sa_entry_get_byindex(struct ll_statahead_info *sai, __u64 index)
274 struct ll_sa_entry *entry;
276 cfs_list_for_each_entry(entry, &sai->sai_entries_sent, se_list) {
277 if (entry->se_index == index) {
278 cfs_atomic_inc(&entry->se_refcount);
281 if (entry->se_index > index)
287 static void ll_sa_entry_cleanup(struct ll_statahead_info *sai,
288 struct ll_sa_entry *entry)
290 struct md_enqueue_info *minfo = entry->se_minfo;
291 struct ptlrpc_request *req = entry->se_req;
294 entry->se_minfo = NULL;
295 ll_intent_release(&minfo->mi_it);
301 entry->se_req = NULL;
302 ptlrpc_req_finished(req);
306 static void ll_sa_entry_put(struct ll_statahead_info *sai,
307 struct ll_sa_entry *entry)
309 if (cfs_atomic_dec_and_test(&entry->se_refcount)) {
310 CDEBUG(D_READA, "free sai entry %.*s(%p) index "LPU64"\n",
311 entry->se_qstr.len, entry->se_qstr.name, entry,
314 LASSERT(ll_sa_entry_unhashed(entry));
315 LASSERT(ll_sa_entry_unlinked(entry));
317 ll_sa_entry_cleanup(sai, entry);
319 iput(entry->se_inode);
321 OBD_FREE(entry, entry->se_size);
322 cfs_atomic_dec(&sai->sai_cache_count);
327 do_sai_entry_fini(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
329 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
331 ll_sa_entry_unhash(sai, entry);
333 cfs_spin_lock(&lli->lli_sa_lock);
334 entry->se_stat = SA_ENTRY_DEST;
335 if (likely(!ll_sa_entry_unlinked(entry)))
336 cfs_list_del_init(&entry->se_list);
337 cfs_spin_unlock(&lli->lli_sa_lock);
339 ll_sa_entry_put(sai, entry);
343 * Delete it from sai_entries_stated list when fini.
346 ll_sa_entry_fini(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
348 struct ll_sa_entry *pos, *next;
351 do_sai_entry_fini(sai, entry);
353 /* drop old entry from sent list */
354 cfs_list_for_each_entry_safe(pos, next, &sai->sai_entries_sent,
356 if (is_omitted_entry(sai, pos->se_index))
357 do_sai_entry_fini(sai, pos);
362 /* drop old entry from stated list */
363 cfs_list_for_each_entry_safe(pos, next, &sai->sai_entries_stated,
365 if (is_omitted_entry(sai, pos->se_index))
366 do_sai_entry_fini(sai, pos);
373 * Inside lli_sa_lock.
376 do_sai_entry_to_stated(struct ll_statahead_info *sai,
377 struct ll_sa_entry *entry, int rc)
379 struct ll_sa_entry *se;
380 cfs_list_t *pos = &sai->sai_entries_stated;
382 if (!ll_sa_entry_unlinked(entry))
383 cfs_list_del_init(&entry->se_list);
385 cfs_list_for_each_entry_reverse(se, &sai->sai_entries_stated, se_list) {
386 if (se->se_index < entry->se_index) {
392 cfs_list_add(&entry->se_list, pos);
397 * Move entry to sai_entries_stated and sort with the index.
398 * \retval 1 -- entry to be destroyed.
399 * \retval 0 -- entry is inserted into stated list.
402 ll_sa_entry_to_stated(struct ll_statahead_info *sai,
403 struct ll_sa_entry *entry, int rc)
405 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
408 ll_sa_entry_cleanup(sai, entry);
410 cfs_spin_lock(&lli->lli_sa_lock);
411 if (likely(entry->se_stat != SA_ENTRY_DEST)) {
412 do_sai_entry_to_stated(sai, entry, rc);
415 cfs_spin_unlock(&lli->lli_sa_lock);
420 static struct ll_statahead_info *ll_sai_alloc(void)
422 struct ll_statahead_info *sai;
430 cfs_atomic_set(&sai->sai_refcount, 1);
431 cfs_spin_lock(&sai_generation_lock);
432 sai->sai_generation = ++sai_generation;
433 if (unlikely(sai_generation == 0))
434 sai->sai_generation = ++sai_generation;
435 cfs_spin_unlock(&sai_generation_lock);
436 sai->sai_max = LL_SA_RPC_MIN;
437 cfs_waitq_init(&sai->sai_waitq);
438 cfs_waitq_init(&sai->sai_thread.t_ctl_waitq);
439 CFS_INIT_LIST_HEAD(&sai->sai_entries_sent);
440 CFS_INIT_LIST_HEAD(&sai->sai_entries_received);
441 CFS_INIT_LIST_HEAD(&sai->sai_entries_stated);
442 for (i = 0; i < LL_SA_CACHE_SIZE; i++) {
443 CFS_INIT_LIST_HEAD(&sai->sai_cache[i]);
444 cfs_spin_lock_init(&sai->sai_cache_lock[i]);
446 cfs_atomic_set(&sai->sai_cache_count, 0);
451 static inline struct ll_statahead_info *
452 ll_sai_get(struct ll_statahead_info *sai)
454 cfs_atomic_inc(&sai->sai_refcount);
458 static void ll_sai_put(struct ll_statahead_info *sai)
460 struct inode *inode = sai->sai_inode;
461 struct ll_inode_info *lli = ll_i2info(inode);
464 if (cfs_atomic_dec_and_lock(&sai->sai_refcount, &lli->lli_sa_lock)) {
465 struct ll_sa_entry *entry, *next;
467 if (unlikely(cfs_atomic_read(&sai->sai_refcount) > 0)) {
468 /* It is race case, the interpret callback just hold
469 * a reference count */
470 cfs_spin_unlock(&lli->lli_sa_lock);
474 LASSERT(lli->lli_opendir_key == NULL);
476 lli->lli_opendir_pid = 0;
477 cfs_spin_unlock(&lli->lli_sa_lock);
479 LASSERT(thread_is_stopped(&sai->sai_thread));
481 if (sai->sai_sent > sai->sai_replied)
482 CDEBUG(D_READA,"statahead for dir "DFID" does not "
483 "finish: [sent:"LPU64"] [replied:"LPU64"]\n",
485 sai->sai_sent, sai->sai_replied);
487 cfs_list_for_each_entry_safe(entry, next,
488 &sai->sai_entries_sent, se_list)
489 do_sai_entry_fini(sai, entry);
491 LASSERT(sa_received_empty(sai));
493 cfs_list_for_each_entry_safe(entry, next,
494 &sai->sai_entries_stated, se_list)
495 do_sai_entry_fini(sai, entry);
497 LASSERT(cfs_atomic_read(&sai->sai_cache_count) == 0);
506 static void do_statahead_interpret(struct ll_statahead_info *sai,
507 struct ll_sa_entry *target)
509 struct inode *dir = sai->sai_inode;
511 struct ll_inode_info *lli = ll_i2info(dir);
512 struct ll_sa_entry *entry;
513 struct md_enqueue_info *minfo;
514 struct lookup_intent *it;
515 struct ptlrpc_request *req;
516 struct mdt_body *body;
520 cfs_spin_lock(&lli->lli_sa_lock);
521 if (target != NULL && target->se_req != NULL &&
522 !cfs_list_empty(&target->se_list)) {
524 } else if (unlikely(sa_received_empty(sai))) {
525 cfs_spin_unlock(&lli->lli_sa_lock);
528 entry = cfs_list_entry(sai->sai_entries_received.next,
529 struct ll_sa_entry, se_list);
532 cfs_atomic_inc(&entry->se_refcount);
533 cfs_list_del_init(&entry->se_list);
534 cfs_spin_unlock(&lli->lli_sa_lock);
536 LASSERT(entry->se_handle != 0);
538 minfo = entry->se_minfo;
541 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
543 GOTO(out, rc = -EFAULT);
545 child = entry->se_inode;
550 LASSERT(fid_is_zero(&minfo->mi_data.op_fid2));
552 /* XXX: No fid in reply, this is probaly cross-ref case.
553 * SA can't handle it yet. */
554 if (body->valid & OBD_MD_MDS)
555 GOTO(out, rc = -EAGAIN);
560 /* unlinked and re-created with the same name */
561 if (unlikely(!lu_fid_eq(&minfo->mi_data.op_fid2, &body->fid1))){
562 entry->se_inode = NULL;
568 it->d.lustre.it_lock_handle = entry->se_handle;
569 rc = md_revalidate_lock(ll_i2mdexp(dir), it, NULL, NULL);
571 GOTO(out, rc = -EAGAIN);
573 rc = ll_prep_inode(&child, req, dir->i_sb);
577 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
578 child, child->i_ino, child->i_generation);
579 ll_set_lock_data(ll_i2sbi(dir)->ll_md_exp, child, it, NULL);
581 entry->se_inode = child;
586 /* The "ll_sa_entry_to_stated()" will drop related ldlm ibits lock
587 * reference count by calling "ll_intent_drop_lock()" in spite of the
588 * above operations failed or not. Do not worry about calling
589 * "ll_intent_drop_lock()" more than once. */
590 rc = ll_sa_entry_to_stated(sai, entry, rc < 0 ? rc : SA_ENTRY_SUCC);
591 if (rc == 0 && entry->se_index == sai->sai_index_wait && target == NULL)
592 cfs_waitq_signal(&sai->sai_waitq);
593 ll_sa_entry_put(sai, entry);
596 static int ll_statahead_interpret(struct ptlrpc_request *req,
597 struct md_enqueue_info *minfo, int rc)
599 struct lookup_intent *it = &minfo->mi_it;
600 struct inode *dir = minfo->mi_dir;
601 struct ll_inode_info *lli = ll_i2info(dir);
602 struct ll_statahead_info *sai = NULL;
603 struct ll_sa_entry *entry;
607 if (it_disposition(it, DISP_LOOKUP_NEG))
610 cfs_spin_lock(&lli->lli_sa_lock);
612 if (unlikely(lli->lli_sai == NULL ||
613 lli->lli_sai->sai_generation != minfo->mi_generation)) {
614 cfs_spin_unlock(&lli->lli_sa_lock);
615 GOTO(out, rc = -ESTALE);
617 sai = ll_sai_get(lli->lli_sai);
618 if (unlikely(!thread_is_running(&sai->sai_thread))) {
620 cfs_spin_unlock(&lli->lli_sa_lock);
621 GOTO(out, rc = -EBADFD);
624 entry = ll_sa_entry_get_byindex(sai, minfo->mi_cbdata);
627 cfs_spin_unlock(&lli->lli_sa_lock);
628 GOTO(out, rc = -EIDRM);
631 cfs_list_del_init(&entry->se_list);
634 do_sai_entry_to_stated(sai, entry, rc);
635 cfs_spin_unlock(&lli->lli_sa_lock);
636 if (entry->se_index == sai->sai_index_wait)
637 cfs_waitq_signal(&sai->sai_waitq);
639 entry->se_minfo = minfo;
640 entry->se_req = ptlrpc_request_addref(req);
641 /* Release the async ibits lock ASAP to avoid deadlock
642 * when statahead thread tries to enqueue lock on parent
643 * for readpage and other tries to enqueue lock on child
644 * with parent's lock held, for example: unlink. */
645 entry->se_handle = it->d.lustre.it_lock_handle;
646 ll_intent_drop_lock(it);
647 wakeup = sa_received_empty(sai);
648 cfs_list_add_tail(&entry->se_list,
649 &sai->sai_entries_received);
651 cfs_spin_unlock(&lli->lli_sa_lock);
653 cfs_waitq_signal(&sai->sai_thread.t_ctl_waitq);
655 ll_sa_entry_put(sai, entry);
662 ll_intent_release(it);
671 static void sa_args_fini(struct md_enqueue_info *minfo,
672 struct ldlm_enqueue_info *einfo)
674 LASSERT(minfo && einfo);
676 capa_put(minfo->mi_data.op_capa1);
677 capa_put(minfo->mi_data.op_capa2);
683 * There is race condition between "capa_put" and "ll_statahead_interpret" for
684 * accessing "op_data.op_capa[1,2]" as following:
685 * "capa_put" releases "op_data.op_capa[1,2]"'s reference count after calling
686 * "md_intent_getattr_async". But "ll_statahead_interpret" maybe run first, and
687 * fill "op_data.op_capa[1,2]" as POISON, then cause "capa_put" access invalid
688 * "ocapa". So here reserve "op_data.op_capa[1,2]" in "pcapa" before calling
689 * "md_intent_getattr_async".
691 static int sa_args_init(struct inode *dir, struct inode *child,
692 struct qstr *qstr, struct md_enqueue_info **pmi,
693 struct ldlm_enqueue_info **pei,
694 struct obd_capa **pcapa)
696 struct ll_inode_info *lli = ll_i2info(dir);
697 struct md_enqueue_info *minfo;
698 struct ldlm_enqueue_info *einfo;
699 struct md_op_data *op_data;
701 OBD_ALLOC_PTR(einfo);
705 OBD_ALLOC_PTR(minfo);
711 op_data = ll_prep_md_op_data(&minfo->mi_data, dir, child, qstr->name,
712 qstr->len, 0, LUSTRE_OPC_ANY, NULL);
713 if (IS_ERR(op_data)) {
716 return PTR_ERR(op_data);
719 minfo->mi_it.it_op = IT_GETATTR;
720 minfo->mi_dir = igrab(dir);
721 minfo->mi_cb = ll_statahead_interpret;
722 minfo->mi_generation = lli->lli_sai->sai_generation;
723 minfo->mi_cbdata = lli->lli_sai->sai_index;
725 einfo->ei_type = LDLM_IBITS;
726 einfo->ei_mode = it_to_lock_mode(&minfo->mi_it);
727 einfo->ei_cb_bl = ll_md_blocking_ast;
728 einfo->ei_cb_cp = ldlm_completion_ast;
729 einfo->ei_cb_gl = NULL;
730 einfo->ei_cbdata = NULL;
734 pcapa[0] = op_data->op_capa1;
735 pcapa[1] = op_data->op_capa2;
740 static int do_sa_lookup(struct inode *dir, struct ll_sa_entry *entry)
742 struct md_enqueue_info *minfo;
743 struct ldlm_enqueue_info *einfo;
744 struct obd_capa *capas[2];
748 rc = sa_args_init(dir, NULL, &entry->se_qstr, &minfo, &einfo, capas);
752 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
757 sa_args_fini(minfo, einfo);
764 * similar to ll_revalidate_it().
765 * \retval 1 -- dentry valid
766 * \retval 0 -- will send stat-ahead request
767 * \retval others -- prepare stat-ahead request failed
769 static int do_sa_revalidate(struct inode *dir, struct ll_sa_entry *entry,
770 struct dentry *dentry)
772 struct inode *inode = dentry->d_inode;
773 struct lookup_intent it = { .it_op = IT_GETATTR,
774 .d.lustre.it_lock_handle = 0 };
775 struct md_enqueue_info *minfo;
776 struct ldlm_enqueue_info *einfo;
777 struct obd_capa *capas[2];
781 if (unlikely(inode == NULL))
784 if (d_mountpoint(dentry))
787 if (unlikely(dentry == dentry->d_sb->s_root))
790 entry->se_inode = igrab(inode);
791 rc = md_revalidate_lock(ll_i2mdexp(dir), &it, ll_inode2fid(inode),NULL);
793 entry->se_handle = it.d.lustre.it_lock_handle;
794 ll_intent_release(&it);
798 rc = sa_args_init(dir, inode, &entry->se_qstr, &minfo, &einfo, capas);
800 entry->se_inode = NULL;
805 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
810 entry->se_inode = NULL;
812 sa_args_fini(minfo, einfo);
818 static void ll_statahead_one(struct dentry *parent, const char* entry_name,
821 struct inode *dir = parent->d_inode;
822 struct ll_inode_info *lli = ll_i2info(dir);
823 struct ll_statahead_info *sai = lli->lli_sai;
824 struct dentry *dentry = NULL;
825 struct ll_sa_entry *entry;
830 entry = ll_sa_entry_alloc(sai, sai->sai_index, entry_name,
835 dentry = d_lookup(parent, &entry->se_qstr);
837 rc = do_sa_lookup(dir, entry);
839 rc = do_sa_revalidate(dir, entry, dentry);
845 rc1 = ll_sa_entry_to_stated(sai, entry,
846 rc < 0 ? SA_ENTRY_INVA : SA_ENTRY_SUCC);
847 if (rc1 == 0 && entry->se_index == sai->sai_index_wait)
848 cfs_waitq_signal(&sai->sai_waitq);
854 /* drop one refcount on entry by ll_sa_entry_alloc */
855 ll_sa_entry_put(sai, entry);
860 static int ll_statahead_thread(void *arg)
862 struct dentry *parent = (struct dentry *)arg;
863 struct inode *dir = parent->d_inode;
864 struct ll_inode_info *lli = ll_i2info(dir);
865 struct ll_sb_info *sbi = ll_i2sbi(dir);
866 struct ll_statahead_info *sai = ll_sai_get(lli->lli_sai);
867 struct ptlrpc_thread *thread = &sai->sai_thread;
872 struct ll_dir_chain chain;
877 snprintf(pname, 15, "ll_sa_%u", lli->lli_opendir_pid);
878 cfs_daemonize(pname);
881 atomic_inc(&sbi->ll_sa_total);
882 cfs_spin_lock(&lli->lli_sa_lock);
883 thread_set_flags(thread, SVC_RUNNING);
884 cfs_spin_unlock(&lli->lli_sa_lock);
885 cfs_waitq_signal(&thread->t_ctl_waitq);
886 CDEBUG(D_READA, "start doing statahead for %s\n", parent->d_name.name);
889 ll_dir_chain_init(&chain);
890 page = ll_get_dir_page(NULL, dir, pos, &chain);
893 struct l_wait_info lwi = { 0 };
894 struct lu_dirpage *dp;
895 struct lu_dirent *ent;
899 CDEBUG(D_READA, "error reading dir "DFID" at "LPU64
900 "/"LPU64": [rc %d] [parent %u]\n",
901 PFID(ll_inode2fid(dir)), pos, sai->sai_index,
902 rc, lli->lli_opendir_pid);
906 dp = page_address(page);
907 for (ent = lu_dirent_start(dp); ent != NULL;
908 ent = lu_dirent_next(ent)) {
913 hash = le64_to_cpu(ent->lde_hash);
914 if (unlikely(hash < pos))
916 * Skip until we find target hash value.
920 namelen = le16_to_cpu(ent->lde_namelen);
921 if (unlikely(namelen == 0))
927 name = ent->lde_name;
928 if (name[0] == '.') {
934 } else if (name[1] == '.' && namelen == 2) {
939 } else if (!sai->sai_ls_all) {
943 sai->sai_skip_hidden++;
949 * don't stat-ahead first entry.
951 if (unlikely(++first == 1))
955 l_wait_event(thread->t_ctl_waitq,
957 !sa_received_empty(sai) ||
958 !thread_is_running(thread),
961 while (!sa_received_empty(sai))
962 do_statahead_interpret(sai, NULL);
964 if (unlikely(!thread_is_running(thread))) {
965 ll_release_page(page, 0);
969 if (!sa_not_full(sai))
971 * do not skip the current de.
975 ll_statahead_one(parent, name, namelen);
977 pos = le64_to_cpu(dp->ldp_hash_end);
978 if (pos == MDS_DIR_END_OFF) {
980 * End of directory reached.
982 ll_release_page(page, 0);
984 l_wait_event(thread->t_ctl_waitq,
985 !sa_received_empty(sai) ||
986 sai->sai_sent == sai->sai_replied||
987 !thread_is_running(thread),
990 while (!sa_received_empty(sai))
991 do_statahead_interpret(sai, NULL);
993 if ((sai->sai_sent == sai->sai_replied &&
994 sa_received_empty(sai)) ||
995 !thread_is_running(thread))
1000 * chain is exhausted.
1001 * Normal case: continue to the next page.
1003 ll_release_page(page, le32_to_cpu(dp->ldp_flags) &
1005 lli->lli_sa_pos = pos;
1006 sai->sai_in_readpage = 1;
1007 page = ll_get_dir_page(NULL, dir, pos, &chain);
1008 sai->sai_in_readpage = 0;
1010 LASSERT(le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1011 ll_release_page(page, 1);
1013 * go into overflow page.
1020 ll_dir_chain_fini(&chain);
1021 cfs_spin_lock(&lli->lli_sa_lock);
1022 if (!sa_received_empty(sai)) {
1023 thread_set_flags(thread, SVC_STOPPING);
1024 cfs_spin_unlock(&lli->lli_sa_lock);
1026 /* To release the resources held by received entries. */
1027 while (!sa_received_empty(sai))
1028 do_statahead_interpret(sai, NULL);
1030 cfs_spin_lock(&lli->lli_sa_lock);
1032 thread_set_flags(thread, SVC_STOPPED);
1033 cfs_spin_unlock(&lli->lli_sa_lock);
1034 cfs_waitq_signal(&sai->sai_waitq);
1035 cfs_waitq_signal(&thread->t_ctl_waitq);
1038 CDEBUG(D_READA, "statahead thread stopped, pid %d\n",
1044 * called in ll_file_release().
1046 void ll_stop_statahead(struct inode *dir, void *key)
1048 struct ll_inode_info *lli = ll_i2info(dir);
1050 if (unlikely(key == NULL))
1053 cfs_spin_lock(&lli->lli_sa_lock);
1054 if (lli->lli_opendir_key != key || lli->lli_opendir_pid == 0) {
1055 cfs_spin_unlock(&lli->lli_sa_lock);
1059 lli->lli_opendir_key = NULL;
1062 struct l_wait_info lwi = { 0 };
1063 struct ptlrpc_thread *thread = &lli->lli_sai->sai_thread;
1065 if (!thread_is_stopped(thread)) {
1066 thread_set_flags(thread, SVC_STOPPING);
1067 cfs_spin_unlock(&lli->lli_sa_lock);
1068 cfs_waitq_signal(&thread->t_ctl_waitq);
1070 CDEBUG(D_READA, "stopping statahead thread, pid %d\n",
1072 l_wait_event(thread->t_ctl_waitq,
1073 thread_is_stopped(thread),
1076 cfs_spin_unlock(&lli->lli_sa_lock);
1080 * Put the ref which was held when first statahead_enter.
1081 * It maybe not the last ref for some statahead requests
1084 ll_sai_put(lli->lli_sai);
1086 lli->lli_opendir_pid = 0;
1087 cfs_spin_unlock(&lli->lli_sa_lock);
1093 * not first dirent, or is "."
1095 LS_NONE_FIRST_DE = 0,
1097 * the first non-hidden dirent
1101 * the first hidden dirent, that is "."
1106 static int is_first_dirent(struct inode *dir, struct dentry *dentry)
1108 struct ll_inode_info *lli = ll_i2info(dir);
1109 struct ll_dir_chain chain;
1110 struct qstr *target = &dentry->d_name;
1114 int rc = LS_NONE_FIRST_DE;
1117 lli->lli_sa_pos = 0;
1118 ll_dir_chain_init(&chain);
1119 page = ll_get_dir_page(NULL, dir, pos, &chain);
1122 struct lu_dirpage *dp;
1123 struct lu_dirent *ent;
1126 struct ll_inode_info *lli = ll_i2info(dir);
1129 CERROR("error reading dir "DFID" at "LPU64": "
1130 "[rc %d] [parent %u]\n",
1131 PFID(ll_inode2fid(dir)), pos,
1132 rc, lli->lli_opendir_pid);
1136 dp = page_address(page);
1137 for (ent = lu_dirent_start(dp); ent != NULL;
1138 ent = lu_dirent_next(ent)) {
1143 hash = le64_to_cpu(ent->lde_hash);
1144 /* The ll_get_dir_page() can return any page containing
1145 * the given hash which may be not the start hash. */
1146 if (unlikely(hash < pos))
1149 namelen = le16_to_cpu(ent->lde_namelen);
1150 if (unlikely(namelen == 0))
1152 * skip dummy record.
1156 name = ent->lde_name;
1157 if (name[0] == '.') {
1163 else if (name[1] == '.' && namelen == 2)
1174 if (dot_de && target->name[0] != '.') {
1175 CDEBUG(D_READA, "%.*s skip hidden file %.*s\n",
1176 target->len, target->name,
1181 if (target->len != namelen ||
1182 memcmp(target->name, name, namelen) != 0)
1183 rc = LS_NONE_FIRST_DE;
1187 rc = LS_FIRST_DOT_DE;
1189 ll_release_page(page, 0);
1192 pos = le64_to_cpu(dp->ldp_hash_end);
1193 if (pos == MDS_DIR_END_OFF) {
1195 * End of directory reached.
1197 ll_release_page(page, 0);
1201 * chain is exhausted
1202 * Normal case: continue to the next page.
1204 ll_release_page(page, le32_to_cpu(dp->ldp_flags) &
1206 lli->lli_sa_pos = pos;
1207 page = ll_get_dir_page(NULL, dir, pos, &chain);
1210 * go into overflow page.
1212 LASSERT(le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1213 ll_release_page(page, 1);
1219 ll_dir_chain_fini(&chain);
1224 ll_sai_unplug(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
1226 struct ptlrpc_thread *thread = &sai->sai_thread;
1227 struct ll_sb_info *sbi = ll_i2sbi(sai->sai_inode);
1231 if (entry != NULL && entry->se_stat == SA_ENTRY_SUCC)
1236 ll_sa_entry_fini(sai, entry);
1239 sai->sai_consecutive_miss = 0;
1240 sai->sai_max = min(2 * sai->sai_max, sbi->ll_sa_max);
1242 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
1245 sai->sai_consecutive_miss++;
1246 if (sa_low_hit(sai) && thread_is_running(thread)) {
1247 atomic_inc(&sbi->ll_sa_wrong);
1248 CDEBUG(D_READA, "Statahead for dir "DFID" hit "
1249 "ratio too low: hit/miss "LPU64"/"LPU64
1250 ", sent/replied "LPU64"/"LPU64", stopping "
1251 "statahead thread: pid %d\n",
1252 PFID(&lli->lli_fid), sai->sai_hit,
1253 sai->sai_miss, sai->sai_sent,
1254 sai->sai_replied, cfs_curproc_pid());
1255 cfs_spin_lock(&lli->lli_sa_lock);
1256 if (!thread_is_stopped(thread))
1257 thread_set_flags(thread, SVC_STOPPING);
1258 cfs_spin_unlock(&lli->lli_sa_lock);
1262 if (!thread_is_stopped(thread))
1263 cfs_waitq_signal(&thread->t_ctl_waitq);
1269 * Start statahead thread if this is the first dir entry.
1270 * Otherwise if a thread is started already, wait it until it is ahead of me.
1271 * \retval 1 -- find entry with lock in cache, the caller needs to do
1273 * \retval 0 -- find entry in cache, but without lock, the caller needs
1275 * \retval others -- the caller need to process as non-statahead.
1277 int do_statahead_enter(struct inode *dir, struct dentry **dentryp,
1280 struct ll_inode_info *lli = ll_i2info(dir);
1281 struct ll_statahead_info *sai = lli->lli_sai;
1282 struct dentry *parent;
1283 struct ll_sa_entry *entry;
1284 struct ptlrpc_thread *thread;
1285 struct l_wait_info lwi = { 0 };
1289 LASSERT(lli->lli_opendir_pid == cfs_curproc_pid());
1292 thread = &sai->sai_thread;
1293 if (unlikely(thread_is_stopped(thread) &&
1294 cfs_list_empty(&sai->sai_entries_stated))) {
1295 /* to release resource */
1296 ll_stop_statahead(dir, lli->lli_opendir_key);
1300 if ((*dentryp)->d_name.name[0] == '.') {
1301 if (sai->sai_ls_all ||
1302 sai->sai_miss_hidden >= sai->sai_skip_hidden) {
1304 * Hidden dentry is the first one, or statahead
1305 * thread does not skip so many hidden dentries
1306 * before "sai_ls_all" enabled as below.
1309 if (!sai->sai_ls_all)
1311 * It maybe because hidden dentry is not
1312 * the first one, "sai_ls_all" was not
1313 * set, then "ls -al" missed. Enable
1314 * "sai_ls_all" for such case.
1316 sai->sai_ls_all = 1;
1319 * Such "getattr" has been skipped before
1320 * "sai_ls_all" enabled as above.
1322 sai->sai_miss_hidden++;
1327 entry = ll_sa_entry_get_byname(sai, &(*dentryp)->d_name);
1328 if (entry == NULL || only_unplug) {
1329 ll_sai_unplug(sai, entry);
1330 RETURN(entry ? 1 : -EAGAIN);
1333 while (!ll_sa_entry_stated(entry) &&
1334 sai->sai_in_readpage &&
1335 !sa_received_empty(sai))
1336 do_statahead_interpret(sai, entry);
1338 if (!ll_sa_entry_stated(entry)) {
1339 sai->sai_index_wait = entry->se_index;
1340 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(30), NULL,
1341 LWI_ON_SIGNAL_NOOP, NULL);
1342 rc = l_wait_event(sai->sai_waitq,
1343 ll_sa_entry_stated(entry) ||
1344 thread_is_stopped(thread),
1347 ll_sai_unplug(sai, entry);
1352 if (entry->se_stat == SA_ENTRY_SUCC &&
1353 entry->se_inode != NULL) {
1354 struct inode *inode = entry->se_inode;
1355 struct lookup_intent it = { .it_op = IT_GETATTR,
1356 .d.lustre.it_lock_handle =
1358 struct ll_dentry_data *lld;
1361 rc = md_revalidate_lock(ll_i2mdexp(dir), &it,
1362 ll_inode2fid(inode), &bits);
1364 if ((*dentryp)->d_inode == NULL) {
1365 *dentryp = ll_find_alias(inode,
1367 lld = ll_d2d(*dentryp);
1368 if (unlikely(lld == NULL))
1369 ll_dops_init(*dentryp, 1, 1);
1371 LASSERT((*dentryp)->d_inode == inode);
1373 ll_dentry_rehash(*dentryp, 0);
1376 entry->se_inode = NULL;
1378 ll_dentry_reset_flags(*dentryp, bits);
1379 ll_intent_release(&it);
1383 ll_sai_unplug(sai, entry);
1387 /* I am the "lli_opendir_pid" owner, only me can set "lli_sai". */
1388 rc = is_first_dirent(dir, *dentryp);
1389 if (rc == LS_NONE_FIRST_DE)
1390 /* It is not "ls -{a}l" operation, no need statahead for it. */
1391 GOTO(out, rc = -EAGAIN);
1393 sai = ll_sai_alloc();
1395 GOTO(out, rc = -ENOMEM);
1397 sai->sai_ls_all = (rc == LS_FIRST_DOT_DE);
1398 sai->sai_inode = igrab(dir);
1399 if (unlikely(sai->sai_inode == NULL)) {
1400 CWARN("Do not start stat ahead on dying inode "DFID"\n",
1401 PFID(&lli->lli_fid));
1402 GOTO(out, rc = -ESTALE);
1405 /* get parent reference count here, and put it in ll_statahead_thread */
1406 parent = dget((*dentryp)->d_parent);
1407 if (unlikely(sai->sai_inode != parent->d_inode)) {
1408 struct ll_inode_info *nlli = ll_i2info(parent->d_inode);
1410 CWARN("Race condition, someone changed %.*s just now: "
1411 "old parent "DFID", new parent "DFID"\n",
1412 (*dentryp)->d_name.len, (*dentryp)->d_name.name,
1413 PFID(&lli->lli_fid), PFID(&nlli->lli_fid));
1415 iput(sai->sai_inode);
1416 GOTO(out, rc = -EAGAIN);
1420 rc = cfs_create_thread(ll_statahead_thread, parent, 0);
1421 thread = &sai->sai_thread;
1423 CERROR("can't start ll_sa thread, rc: %d\n", rc);
1425 lli->lli_opendir_key = NULL;
1426 thread_set_flags(thread, SVC_STOPPED);
1428 LASSERT(lli->lli_sai == NULL);
1432 l_wait_event(thread->t_ctl_waitq,
1433 thread_is_running(thread) || thread_is_stopped(thread),
1437 * We don't stat-ahead for the first dirent since we are already in
1438 * lookup, and -EEXIST also indicates that this is the first dirent.
1445 cfs_spin_lock(&lli->lli_sa_lock);
1446 lli->lli_opendir_key = NULL;
1447 lli->lli_opendir_pid = 0;
1448 cfs_spin_unlock(&lli->lli_sa_lock);