4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
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"
52 #define SA_OMITTED_ENTRY_MAX 8ULL
55 /** negative values are for error cases */
56 SA_ENTRY_INIT = 0, /** init entry */
57 SA_ENTRY_SUCC = 1, /** stat succeed */
58 SA_ENTRY_INVA = 2, /** invalid entry */
59 SA_ENTRY_DEST = 3, /** entry to be destroyed */
63 /* link into sai->sai_entries_{sent,received,stated} */
65 /* link into sai hash table locally */
67 /* entry reference count */
68 cfs_atomic_t se_refcount;
69 /* entry index in the sai */
71 /* low layer ldlm lock handle */
75 /* entry size, contains name */
77 /* pointer to async getattr enqueue info */
78 struct md_enqueue_info *se_minfo;
79 /* pointer to the async getattr request */
80 struct ptlrpc_request *se_req;
81 /* pointer to the target inode */
82 struct inode *se_inode;
87 static unsigned int sai_generation = 0;
88 static cfs_spinlock_t sai_generation_lock = CFS_SPIN_LOCK_UNLOCKED;
90 static inline int ll_sa_entry_unlinked(struct ll_sa_entry *entry)
92 return cfs_list_empty(&entry->se_list);
95 static inline int ll_sa_entry_unhashed(struct ll_sa_entry *entry)
97 return cfs_list_empty(&entry->se_hash);
101 * The entry only can be released by the caller, it is necessary to hold lock.
103 static inline int ll_sa_entry_stated(struct ll_sa_entry *entry)
106 return (entry->se_stat != SA_ENTRY_INIT);
109 static inline int ll_sa_entry_hash(int val)
111 return val & LL_SA_CACHE_MASK;
115 * Insert entry to hash SA table.
118 ll_sa_entry_enhash(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
120 int i = ll_sa_entry_hash(entry->se_qstr.hash);
122 cfs_spin_lock(&sai->sai_cache_lock[i]);
123 cfs_list_add_tail(&entry->se_hash, &sai->sai_cache[i]);
124 cfs_spin_unlock(&sai->sai_cache_lock[i]);
128 * Remove entry from SA table.
131 ll_sa_entry_unhash(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
133 int i = ll_sa_entry_hash(entry->se_qstr.hash);
135 cfs_spin_lock(&sai->sai_cache_lock[i]);
136 cfs_list_del_init(&entry->se_hash);
137 cfs_spin_unlock(&sai->sai_cache_lock[i]);
140 static inline int agl_should_run(struct ll_statahead_info *sai,
143 if (inode != NULL && S_ISREG(inode->i_mode) &&
144 ll_i2info(inode)->lli_smd != NULL && sai->sai_agl_valid)
149 static inline struct ll_sa_entry *
150 sa_first_received_entry(struct ll_statahead_info *sai)
152 return cfs_list_entry(sai->sai_entries_received.next,
153 struct ll_sa_entry, se_list);
156 static inline struct ll_inode_info *
157 agl_first_entry(struct ll_statahead_info *sai)
159 return cfs_list_entry(sai->sai_entries_agl.next,
160 struct ll_inode_info, lli_agl_list);
163 static inline int sa_sent_full(struct ll_statahead_info *sai)
165 return cfs_atomic_read(&sai->sai_cache_count) >= sai->sai_max;
168 static inline int sa_received_empty(struct ll_statahead_info *sai)
170 return cfs_list_empty(&sai->sai_entries_received);
173 static inline int agl_list_empty(struct ll_statahead_info *sai)
175 return cfs_list_empty(&sai->sai_entries_agl);
179 * (1) hit ratio less than 80%
181 * (2) consecutive miss more than 8
182 * then means low hit.
184 static inline int sa_low_hit(struct ll_statahead_info *sai)
186 return ((sai->sai_hit > 7 && sai->sai_hit < 4 * sai->sai_miss) ||
187 (sai->sai_consecutive_miss > 8));
191 * If the given index is behind of statahead window more than
192 * SA_OMITTED_ENTRY_MAX, then it is old.
194 static inline int is_omitted_entry(struct ll_statahead_info *sai, __u64 index)
196 return ((__u64)sai->sai_max + index + SA_OMITTED_ENTRY_MAX <
201 * Insert it into sai_entries_sent tail when init.
203 static struct ll_sa_entry *
204 ll_sa_entry_alloc(struct ll_statahead_info *sai, __u64 index,
205 const char *name, int len)
207 struct ll_inode_info *lli;
208 struct ll_sa_entry *entry;
213 entry_size = sizeof(struct ll_sa_entry) + (len & ~3) + 4;
214 OBD_ALLOC(entry, entry_size);
215 if (unlikely(entry == NULL))
216 RETURN(ERR_PTR(-ENOMEM));
218 CDEBUG(D_READA, "alloc sai entry %.*s(%p) index "LPU64"\n",
219 len, name, entry, index);
221 entry->se_index = index;
224 * Statahead entry reference rules:
226 * 1) When statahead entry is initialized, its reference is set as 2.
227 * One reference is used by the directory scanner. When the scanner
228 * searches the statahead cache for the given name, it can perform
229 * lockless hash lookup (only the scanner can remove entry from hash
230 * list), and once found, it needn't to call "atomic_inc()" for the
231 * entry reference. So the performance is improved. After using the
232 * statahead entry, the scanner will call "atomic_dec()" to drop the
233 * reference held when initialization. If it is the last reference,
234 * the statahead entry will be freed.
236 * 2) All other threads, including statahead thread and ptlrpcd thread,
237 * when they process the statahead entry, the reference for target
238 * should be held to guarantee the entry will not be released by the
239 * directory scanner. After processing the entry, these threads will
240 * drop the entry reference. If it is the last reference, the entry
243 * The second reference when initializes the statahead entry is used
244 * by the statahead thread, following the rule 2).
246 cfs_atomic_set(&entry->se_refcount, 2);
247 entry->se_stat = SA_ENTRY_INIT;
248 entry->se_size = entry_size;
249 dname = (char *)entry + sizeof(struct ll_sa_entry);
250 memcpy(dname, name, len);
252 entry->se_qstr.hash = full_name_hash(name, len);
253 entry->se_qstr.len = len;
254 entry->se_qstr.name = dname;
256 lli = ll_i2info(sai->sai_inode);
257 cfs_spin_lock(&lli->lli_sa_lock);
258 cfs_list_add_tail(&entry->se_list, &sai->sai_entries_sent);
259 cfs_spin_unlock(&lli->lli_sa_lock);
261 cfs_atomic_inc(&sai->sai_cache_count);
262 ll_sa_entry_enhash(sai, entry);
268 * Used by the directory scanner to search entry with name.
270 * Only the caller can remove the entry from hash, so it is unnecessary to hold
271 * hash lock. It is caller's duty to release the init refcount on the entry, so
272 * it is also unnecessary to increase refcount on the entry.
274 static struct ll_sa_entry *
275 ll_sa_entry_get_byname(struct ll_statahead_info *sai, const struct qstr *qstr)
277 struct ll_sa_entry *entry;
278 int i = ll_sa_entry_hash(qstr->hash);
280 cfs_list_for_each_entry(entry, &sai->sai_cache[i], se_hash) {
281 if (entry->se_qstr.hash == qstr->hash &&
282 entry->se_qstr.len == qstr->len &&
283 memcmp(entry->se_qstr.name, qstr->name, qstr->len) == 0)
290 * Used by the async getattr request callback to find entry with index.
292 * Inside lli_sa_lock to prevent others to change the list during the search.
293 * It needs to increase entry refcount before returning to guarantee that the
294 * entry cannot be freed by others.
296 static struct ll_sa_entry *
297 ll_sa_entry_get_byindex(struct ll_statahead_info *sai, __u64 index)
299 struct ll_sa_entry *entry;
301 cfs_list_for_each_entry(entry, &sai->sai_entries_sent, se_list) {
302 if (entry->se_index == index) {
303 cfs_atomic_inc(&entry->se_refcount);
306 if (entry->se_index > index)
312 static void ll_sa_entry_cleanup(struct ll_statahead_info *sai,
313 struct ll_sa_entry *entry)
315 struct md_enqueue_info *minfo = entry->se_minfo;
316 struct ptlrpc_request *req = entry->se_req;
319 entry->se_minfo = NULL;
320 ll_intent_release(&minfo->mi_it);
326 entry->se_req = NULL;
327 ptlrpc_req_finished(req);
331 static void ll_sa_entry_put(struct ll_statahead_info *sai,
332 struct ll_sa_entry *entry)
334 if (cfs_atomic_dec_and_test(&entry->se_refcount)) {
335 CDEBUG(D_READA, "free sai entry %.*s(%p) index "LPU64"\n",
336 entry->se_qstr.len, entry->se_qstr.name, entry,
339 LASSERT(ll_sa_entry_unhashed(entry));
340 LASSERT(ll_sa_entry_unlinked(entry));
342 ll_sa_entry_cleanup(sai, entry);
344 iput(entry->se_inode);
346 OBD_FREE(entry, entry->se_size);
347 cfs_atomic_dec(&sai->sai_cache_count);
352 do_sai_entry_fini(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
354 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
356 ll_sa_entry_unhash(sai, entry);
358 cfs_spin_lock(&lli->lli_sa_lock);
359 entry->se_stat = SA_ENTRY_DEST;
360 if (likely(!ll_sa_entry_unlinked(entry)))
361 cfs_list_del_init(&entry->se_list);
362 cfs_spin_unlock(&lli->lli_sa_lock);
364 ll_sa_entry_put(sai, entry);
368 * Delete it from sai_entries_stated list when fini.
371 ll_sa_entry_fini(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
373 struct ll_sa_entry *pos, *next;
376 do_sai_entry_fini(sai, entry);
378 /* drop old entry from sent list */
379 cfs_list_for_each_entry_safe(pos, next, &sai->sai_entries_sent,
381 if (is_omitted_entry(sai, pos->se_index))
382 do_sai_entry_fini(sai, pos);
387 /* drop old entry from stated list */
388 cfs_list_for_each_entry_safe(pos, next, &sai->sai_entries_stated,
390 if (is_omitted_entry(sai, pos->se_index))
391 do_sai_entry_fini(sai, pos);
398 * Inside lli_sa_lock.
401 do_sai_entry_to_stated(struct ll_statahead_info *sai,
402 struct ll_sa_entry *entry, int rc)
404 struct ll_sa_entry *se;
405 cfs_list_t *pos = &sai->sai_entries_stated;
407 if (!ll_sa_entry_unlinked(entry))
408 cfs_list_del_init(&entry->se_list);
410 cfs_list_for_each_entry_reverse(se, &sai->sai_entries_stated, se_list) {
411 if (se->se_index < entry->se_index) {
417 cfs_list_add(&entry->se_list, pos);
422 * Move entry to sai_entries_stated and sort with the index.
423 * \retval 1 -- entry to be destroyed.
424 * \retval 0 -- entry is inserted into stated list.
427 ll_sa_entry_to_stated(struct ll_statahead_info *sai,
428 struct ll_sa_entry *entry, int rc)
430 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
433 ll_sa_entry_cleanup(sai, entry);
435 cfs_spin_lock(&lli->lli_sa_lock);
436 if (likely(entry->se_stat != SA_ENTRY_DEST)) {
437 do_sai_entry_to_stated(sai, entry, rc);
440 cfs_spin_unlock(&lli->lli_sa_lock);
446 * Insert inode into the list of sai_entries_agl.
448 static void ll_agl_add(struct ll_statahead_info *sai,
449 struct inode *inode, int index)
451 struct ll_inode_info *child = ll_i2info(inode);
452 struct ll_inode_info *parent = ll_i2info(sai->sai_inode);
455 cfs_spin_lock(&child->lli_agl_lock);
456 if (child->lli_agl_index == 0) {
457 child->lli_agl_index = index;
458 cfs_spin_unlock(&child->lli_agl_lock);
460 LASSERT(cfs_list_empty(&child->lli_agl_list));
463 cfs_spin_lock(&parent->lli_agl_lock);
464 if (agl_list_empty(sai))
466 cfs_list_add_tail(&child->lli_agl_list, &sai->sai_entries_agl);
467 cfs_spin_unlock(&parent->lli_agl_lock);
469 cfs_spin_unlock(&child->lli_agl_lock);
473 cfs_waitq_signal(&sai->sai_agl_thread.t_ctl_waitq);
476 static struct ll_statahead_info *ll_sai_alloc(void)
478 struct ll_statahead_info *sai;
486 cfs_atomic_set(&sai->sai_refcount, 1);
488 cfs_spin_lock(&sai_generation_lock);
489 sai->sai_generation = ++sai_generation;
490 if (unlikely(sai_generation == 0))
491 sai->sai_generation = ++sai_generation;
492 cfs_spin_unlock(&sai_generation_lock);
494 sai->sai_max = LL_SA_RPC_MIN;
496 cfs_waitq_init(&sai->sai_waitq);
497 cfs_waitq_init(&sai->sai_thread.t_ctl_waitq);
498 cfs_waitq_init(&sai->sai_agl_thread.t_ctl_waitq);
500 CFS_INIT_LIST_HEAD(&sai->sai_entries_sent);
501 CFS_INIT_LIST_HEAD(&sai->sai_entries_received);
502 CFS_INIT_LIST_HEAD(&sai->sai_entries_stated);
503 CFS_INIT_LIST_HEAD(&sai->sai_entries_agl);
505 for (i = 0; i < LL_SA_CACHE_SIZE; i++) {
506 CFS_INIT_LIST_HEAD(&sai->sai_cache[i]);
507 cfs_spin_lock_init(&sai->sai_cache_lock[i]);
509 cfs_atomic_set(&sai->sai_cache_count, 0);
514 static inline struct ll_statahead_info *
515 ll_sai_get(struct ll_statahead_info *sai)
517 cfs_atomic_inc(&sai->sai_refcount);
521 static void ll_sai_put(struct ll_statahead_info *sai)
523 struct inode *inode = sai->sai_inode;
524 struct ll_inode_info *lli = ll_i2info(inode);
527 if (cfs_atomic_dec_and_lock(&sai->sai_refcount, &lli->lli_sa_lock)) {
528 struct ll_sa_entry *entry, *next;
530 if (unlikely(cfs_atomic_read(&sai->sai_refcount) > 0)) {
531 /* It is race case, the interpret callback just hold
532 * a reference count */
533 cfs_spin_unlock(&lli->lli_sa_lock);
537 LASSERT(lli->lli_opendir_key == NULL);
538 LASSERT(thread_is_stopped(&sai->sai_thread));
539 LASSERT(thread_is_stopped(&sai->sai_agl_thread));
542 lli->lli_opendir_pid = 0;
543 cfs_spin_unlock(&lli->lli_sa_lock);
545 if (sai->sai_sent > sai->sai_replied)
546 CDEBUG(D_READA,"statahead for dir "DFID" does not "
547 "finish: [sent:"LPU64"] [replied:"LPU64"]\n",
549 sai->sai_sent, sai->sai_replied);
551 cfs_list_for_each_entry_safe(entry, next,
552 &sai->sai_entries_sent, se_list)
553 do_sai_entry_fini(sai, entry);
555 LASSERT(sa_received_empty(sai));
557 cfs_list_for_each_entry_safe(entry, next,
558 &sai->sai_entries_stated, se_list)
559 do_sai_entry_fini(sai, entry);
561 LASSERT(cfs_atomic_read(&sai->sai_cache_count) == 0);
562 LASSERT(agl_list_empty(sai));
571 /* Do NOT forget to drop inode refcount when into sai_entries_agl. */
572 static void ll_agl_trigger(struct inode *inode, struct ll_statahead_info *sai)
574 struct ll_inode_info *lli = ll_i2info(inode);
575 __u64 index = lli->lli_agl_index;
579 LASSERT(cfs_list_empty(&lli->lli_agl_list));
581 /* AGL maybe fall behind statahead with one entry */
582 if (is_omitted_entry(sai, index + 1)) {
583 lli->lli_agl_index = 0;
588 /* Someone is in glimpse (sync or async), do nothing. */
589 rc = cfs_down_write_trylock(&lli->lli_glimpse_sem);
591 lli->lli_agl_index = 0;
597 * Someone triggered glimpse within 1 sec before.
598 * 1) The former glimpse succeeded with glimpse lock granted by OST, and
599 * if the lock is still cached on client, AGL needs to do nothing. If
600 * it is cancelled by other client, AGL maybe cannot obtaion new lock
601 * for no glimpse callback triggered by AGL.
602 * 2) The former glimpse succeeded, but OST did not grant glimpse lock.
603 * Under such case, it is quite possible that the OST will not grant
604 * glimpse lock for AGL also.
605 * 3) The former glimpse failed, compared with other two cases, it is
606 * relative rare. AGL can ignore such case, and it will not muchly
607 * affect the performance.
609 if (lli->lli_glimpse_time != 0 &&
610 cfs_time_before(cfs_time_shift(-1), lli->lli_glimpse_time)) {
611 cfs_up_write(&lli->lli_glimpse_sem);
612 lli->lli_agl_index = 0;
617 CDEBUG(D_READA, "Handling (init) async glimpse: inode = "
618 DFID", idx = "LPU64"\n", PFID(&lli->lli_fid), index);
621 lli->lli_agl_index = 0;
622 lli->lli_glimpse_time = cfs_time_current();
623 cfs_up_write(&lli->lli_glimpse_sem);
625 CDEBUG(D_READA, "Handled (init) async glimpse: inode= "
626 DFID", idx = "LPU64", rc = %d\n",
627 PFID(&lli->lli_fid), index, rc);
634 static void do_statahead_interpret(struct ll_statahead_info *sai,
635 struct ll_sa_entry *target)
637 struct inode *dir = sai->sai_inode;
639 struct ll_inode_info *lli = ll_i2info(dir);
640 struct ll_sa_entry *entry;
641 struct md_enqueue_info *minfo;
642 struct lookup_intent *it;
643 struct ptlrpc_request *req;
644 struct mdt_body *body;
648 cfs_spin_lock(&lli->lli_sa_lock);
649 if (target != NULL && target->se_req != NULL &&
650 !cfs_list_empty(&target->se_list)) {
652 } else if (unlikely(sa_received_empty(sai))) {
653 cfs_spin_unlock(&lli->lli_sa_lock);
656 entry = sa_first_received_entry(sai);
659 cfs_atomic_inc(&entry->se_refcount);
660 cfs_list_del_init(&entry->se_list);
661 cfs_spin_unlock(&lli->lli_sa_lock);
663 LASSERT(entry->se_handle != 0);
665 minfo = entry->se_minfo;
668 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
670 GOTO(out, rc = -EFAULT);
672 child = entry->se_inode;
677 LASSERT(fid_is_zero(&minfo->mi_data.op_fid2));
679 /* XXX: No fid in reply, this is probaly cross-ref case.
680 * SA can't handle it yet. */
681 if (body->valid & OBD_MD_MDS)
682 GOTO(out, rc = -EAGAIN);
687 /* unlinked and re-created with the same name */
688 if (unlikely(!lu_fid_eq(&minfo->mi_data.op_fid2, &body->fid1))){
689 entry->se_inode = NULL;
695 it->d.lustre.it_lock_handle = entry->se_handle;
696 rc = md_revalidate_lock(ll_i2mdexp(dir), it, NULL, NULL);
698 GOTO(out, rc = -EAGAIN);
700 rc = ll_prep_inode(&child, req, dir->i_sb);
704 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
705 child, child->i_ino, child->i_generation);
706 ll_set_lock_data(ll_i2sbi(dir)->ll_md_exp, child, it, NULL);
708 entry->se_inode = child;
710 if (agl_should_run(sai, child))
711 ll_agl_add(sai, child, entry->se_index);
716 /* The "ll_sa_entry_to_stated()" will drop related ldlm ibits lock
717 * reference count by calling "ll_intent_drop_lock()" in spite of the
718 * above operations failed or not. Do not worry about calling
719 * "ll_intent_drop_lock()" more than once. */
720 rc = ll_sa_entry_to_stated(sai, entry, rc < 0 ? rc : SA_ENTRY_SUCC);
721 if (rc == 0 && entry->se_index == sai->sai_index_wait && target == NULL)
722 cfs_waitq_signal(&sai->sai_waitq);
723 ll_sa_entry_put(sai, entry);
726 static int ll_statahead_interpret(struct ptlrpc_request *req,
727 struct md_enqueue_info *minfo, int rc)
729 struct lookup_intent *it = &minfo->mi_it;
730 struct inode *dir = minfo->mi_dir;
731 struct ll_inode_info *lli = ll_i2info(dir);
732 struct ll_statahead_info *sai = NULL;
733 struct ll_sa_entry *entry;
737 if (it_disposition(it, DISP_LOOKUP_NEG))
740 cfs_spin_lock(&lli->lli_sa_lock);
742 if (unlikely(lli->lli_sai == NULL ||
743 lli->lli_sai->sai_generation != minfo->mi_generation)) {
744 cfs_spin_unlock(&lli->lli_sa_lock);
745 GOTO(out, rc = -ESTALE);
747 sai = ll_sai_get(lli->lli_sai);
748 if (unlikely(!thread_is_running(&sai->sai_thread))) {
750 cfs_spin_unlock(&lli->lli_sa_lock);
751 GOTO(out, rc = -EBADFD);
754 entry = ll_sa_entry_get_byindex(sai, minfo->mi_cbdata);
757 cfs_spin_unlock(&lli->lli_sa_lock);
758 GOTO(out, rc = -EIDRM);
761 cfs_list_del_init(&entry->se_list);
764 do_sai_entry_to_stated(sai, entry, rc);
765 cfs_spin_unlock(&lli->lli_sa_lock);
766 if (entry->se_index == sai->sai_index_wait)
767 cfs_waitq_signal(&sai->sai_waitq);
769 entry->se_minfo = minfo;
770 entry->se_req = ptlrpc_request_addref(req);
771 /* Release the async ibits lock ASAP to avoid deadlock
772 * when statahead thread tries to enqueue lock on parent
773 * for readpage and other tries to enqueue lock on child
774 * with parent's lock held, for example: unlink. */
775 entry->se_handle = it->d.lustre.it_lock_handle;
776 ll_intent_drop_lock(it);
777 wakeup = sa_received_empty(sai);
778 cfs_list_add_tail(&entry->se_list,
779 &sai->sai_entries_received);
781 cfs_spin_unlock(&lli->lli_sa_lock);
783 cfs_waitq_signal(&sai->sai_thread.t_ctl_waitq);
785 ll_sa_entry_put(sai, entry);
792 ll_intent_release(it);
801 static void sa_args_fini(struct md_enqueue_info *minfo,
802 struct ldlm_enqueue_info *einfo)
804 LASSERT(minfo && einfo);
806 capa_put(minfo->mi_data.op_capa1);
807 capa_put(minfo->mi_data.op_capa2);
813 * There is race condition between "capa_put" and "ll_statahead_interpret" for
814 * accessing "op_data.op_capa[1,2]" as following:
815 * "capa_put" releases "op_data.op_capa[1,2]"'s reference count after calling
816 * "md_intent_getattr_async". But "ll_statahead_interpret" maybe run first, and
817 * fill "op_data.op_capa[1,2]" as POISON, then cause "capa_put" access invalid
818 * "ocapa". So here reserve "op_data.op_capa[1,2]" in "pcapa" before calling
819 * "md_intent_getattr_async".
821 static int sa_args_init(struct inode *dir, struct inode *child,
822 struct ll_sa_entry *entry, struct md_enqueue_info **pmi,
823 struct ldlm_enqueue_info **pei,
824 struct obd_capa **pcapa)
826 struct qstr *qstr = &entry->se_qstr;
827 struct ll_inode_info *lli = ll_i2info(dir);
828 struct md_enqueue_info *minfo;
829 struct ldlm_enqueue_info *einfo;
830 struct md_op_data *op_data;
832 OBD_ALLOC_PTR(einfo);
836 OBD_ALLOC_PTR(minfo);
842 op_data = ll_prep_md_op_data(&minfo->mi_data, dir, child, qstr->name,
843 qstr->len, 0, LUSTRE_OPC_ANY, NULL);
844 if (IS_ERR(op_data)) {
847 return PTR_ERR(op_data);
850 minfo->mi_it.it_op = IT_GETATTR;
851 minfo->mi_dir = igrab(dir);
852 minfo->mi_cb = ll_statahead_interpret;
853 minfo->mi_generation = lli->lli_sai->sai_generation;
854 minfo->mi_cbdata = entry->se_index;
856 einfo->ei_type = LDLM_IBITS;
857 einfo->ei_mode = it_to_lock_mode(&minfo->mi_it);
858 einfo->ei_cb_bl = ll_md_blocking_ast;
859 einfo->ei_cb_cp = ldlm_completion_ast;
860 einfo->ei_cb_gl = NULL;
861 einfo->ei_cbdata = NULL;
865 pcapa[0] = op_data->op_capa1;
866 pcapa[1] = op_data->op_capa2;
871 static int do_sa_lookup(struct inode *dir, struct ll_sa_entry *entry)
873 struct md_enqueue_info *minfo;
874 struct ldlm_enqueue_info *einfo;
875 struct obd_capa *capas[2];
879 rc = sa_args_init(dir, NULL, entry, &minfo, &einfo, capas);
883 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
888 sa_args_fini(minfo, einfo);
895 * similar to ll_revalidate_it().
896 * \retval 1 -- dentry valid
897 * \retval 0 -- will send stat-ahead request
898 * \retval others -- prepare stat-ahead request failed
900 static int do_sa_revalidate(struct inode *dir, struct ll_sa_entry *entry,
901 struct dentry *dentry)
903 struct inode *inode = dentry->d_inode;
904 struct lookup_intent it = { .it_op = IT_GETATTR,
905 .d.lustre.it_lock_handle = 0 };
906 struct md_enqueue_info *minfo;
907 struct ldlm_enqueue_info *einfo;
908 struct obd_capa *capas[2];
912 if (unlikely(inode == NULL))
915 if (d_mountpoint(dentry))
918 if (unlikely(dentry == dentry->d_sb->s_root))
921 entry->se_inode = igrab(inode);
922 rc = md_revalidate_lock(ll_i2mdexp(dir), &it, ll_inode2fid(inode),NULL);
924 entry->se_handle = it.d.lustre.it_lock_handle;
925 ll_intent_release(&it);
929 rc = sa_args_init(dir, inode, entry, &minfo, &einfo, capas);
931 entry->se_inode = NULL;
936 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
941 entry->se_inode = NULL;
943 sa_args_fini(minfo, einfo);
949 static void ll_statahead_one(struct dentry *parent, const char* entry_name,
952 struct inode *dir = parent->d_inode;
953 struct ll_inode_info *lli = ll_i2info(dir);
954 struct ll_statahead_info *sai = lli->lli_sai;
955 struct dentry *dentry = NULL;
956 struct ll_sa_entry *entry;
961 entry = ll_sa_entry_alloc(sai, sai->sai_index, entry_name,
966 dentry = d_lookup(parent, &entry->se_qstr);
968 rc = do_sa_lookup(dir, entry);
970 rc = do_sa_revalidate(dir, entry, dentry);
971 if (rc == 1 && agl_should_run(sai, dentry->d_inode))
972 ll_agl_add(sai, dentry->d_inode, entry->se_index);
979 rc1 = ll_sa_entry_to_stated(sai, entry,
980 rc < 0 ? SA_ENTRY_INVA : SA_ENTRY_SUCC);
981 if (rc1 == 0 && entry->se_index == sai->sai_index_wait)
982 cfs_waitq_signal(&sai->sai_waitq);
988 /* drop one refcount on entry by ll_sa_entry_alloc */
989 ll_sa_entry_put(sai, entry);
994 static int ll_agl_thread(void *arg)
996 struct dentry *parent = (struct dentry *)arg;
997 struct inode *dir = parent->d_inode;
998 struct ll_inode_info *plli = ll_i2info(dir);
999 struct ll_inode_info *clli;
1000 struct ll_sb_info *sbi = ll_i2sbi(dir);
1001 struct ll_statahead_info *sai = ll_sai_get(plli->lli_sai);
1002 struct ptlrpc_thread *thread = &sai->sai_agl_thread;
1003 struct l_wait_info lwi = { 0 };
1008 snprintf(pname, 15, "ll_agl_%u", plli->lli_opendir_pid);
1009 cfs_daemonize(pname);
1012 CDEBUG(D_READA, "agl thread started: [pid %d] [parent %.*s]\n",
1013 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1015 atomic_inc(&sbi->ll_agl_total);
1016 cfs_spin_lock(&plli->lli_agl_lock);
1017 sai->sai_agl_valid = 1;
1018 thread_set_flags(thread, SVC_RUNNING);
1019 cfs_spin_unlock(&plli->lli_agl_lock);
1020 cfs_waitq_signal(&thread->t_ctl_waitq);
1023 l_wait_event(thread->t_ctl_waitq,
1024 !agl_list_empty(sai) ||
1025 !thread_is_running(thread),
1028 if (!thread_is_running(thread))
1031 cfs_spin_lock(&plli->lli_agl_lock);
1032 /* The statahead thread maybe help to process AGL entries,
1033 * so check whether list empty again. */
1034 if (!agl_list_empty(sai)) {
1035 clli = agl_first_entry(sai);
1036 cfs_list_del_init(&clli->lli_agl_list);
1037 cfs_spin_unlock(&plli->lli_agl_lock);
1038 ll_agl_trigger(&clli->lli_vfs_inode, sai);
1040 cfs_spin_unlock(&plli->lli_agl_lock);
1044 cfs_spin_lock(&plli->lli_agl_lock);
1045 sai->sai_agl_valid = 0;
1046 while (!agl_list_empty(sai)) {
1047 clli = agl_first_entry(sai);
1048 cfs_list_del_init(&clli->lli_agl_list);
1049 cfs_spin_unlock(&plli->lli_agl_lock);
1050 clli->lli_agl_index = 0;
1051 iput(&clli->lli_vfs_inode);
1052 cfs_spin_lock(&plli->lli_agl_lock);
1054 thread_set_flags(thread, SVC_STOPPED);
1055 cfs_spin_unlock(&plli->lli_agl_lock);
1056 cfs_waitq_signal(&thread->t_ctl_waitq);
1058 CDEBUG(D_READA, "agl thread stopped: [pid %d] [parent %.*s]\n",
1059 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1063 static void ll_start_agl(struct dentry *parent, struct ll_statahead_info *sai)
1065 struct ptlrpc_thread *thread = &sai->sai_agl_thread;
1066 struct l_wait_info lwi = { 0 };
1070 CDEBUG(D_READA, "start agl thread: [pid %d] [parent %.*s]\n",
1071 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1073 rc = cfs_create_thread(ll_agl_thread, parent, 0);
1075 CERROR("can't start ll_agl thread, rc: %d\n", rc);
1076 thread_set_flags(thread, SVC_STOPPED);
1080 l_wait_event(thread->t_ctl_waitq,
1081 thread_is_running(thread) || thread_is_stopped(thread),
1086 static int ll_statahead_thread(void *arg)
1088 struct dentry *parent = (struct dentry *)arg;
1089 struct inode *dir = parent->d_inode;
1090 struct ll_inode_info *plli = ll_i2info(dir);
1091 struct ll_inode_info *clli;
1092 struct ll_sb_info *sbi = ll_i2sbi(dir);
1093 struct ll_statahead_info *sai = ll_sai_get(plli->lli_sai);
1094 struct ptlrpc_thread *thread = &sai->sai_thread;
1095 struct ptlrpc_thread *agl_thread = &sai->sai_agl_thread;
1100 struct ll_dir_chain chain;
1101 struct l_wait_info lwi = { 0 };
1106 snprintf(pname, 15, "ll_sa_%u", plli->lli_opendir_pid);
1107 cfs_daemonize(pname);
1110 CDEBUG(D_READA, "statahead thread started: [pid %d] [parent %.*s]\n",
1111 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1113 if (sbi->ll_flags & LL_SBI_AGL_ENABLED)
1114 ll_start_agl(parent, sai);
1116 atomic_inc(&sbi->ll_sa_total);
1117 cfs_spin_lock(&plli->lli_sa_lock);
1118 thread_set_flags(thread, SVC_RUNNING);
1119 cfs_spin_unlock(&plli->lli_sa_lock);
1120 cfs_waitq_signal(&thread->t_ctl_waitq);
1122 plli->lli_sa_pos = 0;
1123 ll_dir_chain_init(&chain);
1124 page = ll_get_dir_page(NULL, dir, pos, &chain);
1127 struct lu_dirpage *dp;
1128 struct lu_dirent *ent;
1132 CDEBUG(D_READA, "error reading dir "DFID" at "LPU64
1133 "/"LPU64": [rc %d] [parent %u]\n",
1134 PFID(ll_inode2fid(dir)), pos, sai->sai_index,
1135 rc, plli->lli_opendir_pid);
1139 dp = page_address(page);
1140 for (ent = lu_dirent_start(dp); ent != NULL;
1141 ent = lu_dirent_next(ent)) {
1146 hash = le64_to_cpu(ent->lde_hash);
1147 if (unlikely(hash < pos))
1149 * Skip until we find target hash value.
1153 namelen = le16_to_cpu(ent->lde_namelen);
1154 if (unlikely(namelen == 0))
1156 * Skip dummy record.
1160 name = ent->lde_name;
1161 if (name[0] == '.') {
1167 } else if (name[1] == '.' && namelen == 2) {
1172 } else if (!sai->sai_ls_all) {
1174 * skip hidden files.
1176 sai->sai_skip_hidden++;
1182 * don't stat-ahead first entry.
1184 if (unlikely(++first == 1))
1188 l_wait_event(thread->t_ctl_waitq,
1189 !sa_sent_full(sai) ||
1190 !sa_received_empty(sai) ||
1191 !agl_list_empty(sai) ||
1192 !thread_is_running(thread),
1196 while (!sa_received_empty(sai))
1197 do_statahead_interpret(sai, NULL);
1199 if (unlikely(!thread_is_running(thread))) {
1200 ll_release_page(page, 0);
1204 /* If no window for metadata statahead, but there are
1205 * some AGL entries to be triggered, then try to help
1206 * to process the AGL entries. */
1207 if (sa_sent_full(sai)) {
1208 cfs_spin_lock(&plli->lli_agl_lock);
1209 while (!agl_list_empty(sai)) {
1210 clli = agl_first_entry(sai);
1211 cfs_list_del_init(&clli->lli_agl_list);
1212 cfs_spin_unlock(&plli->lli_agl_lock);
1213 ll_agl_trigger(&clli->lli_vfs_inode,
1216 if (!sa_received_empty(sai))
1220 !thread_is_running(thread))) {
1221 ll_release_page(page, 0);
1225 if (!sa_sent_full(sai))
1228 cfs_spin_lock(&plli->lli_agl_lock);
1230 cfs_spin_unlock(&plli->lli_agl_lock);
1236 ll_statahead_one(parent, name, namelen);
1238 pos = le64_to_cpu(dp->ldp_hash_end);
1239 if (pos == MDS_DIR_END_OFF) {
1241 * End of directory reached.
1243 ll_release_page(page, 0);
1245 l_wait_event(thread->t_ctl_waitq,
1246 !sa_received_empty(sai) ||
1247 sai->sai_sent == sai->sai_replied||
1248 !thread_is_running(thread),
1251 while (!sa_received_empty(sai))
1252 do_statahead_interpret(sai, NULL);
1254 if (unlikely(!thread_is_running(thread)))
1257 if (sai->sai_sent == sai->sai_replied &&
1258 sa_received_empty(sai))
1262 cfs_spin_lock(&plli->lli_agl_lock);
1263 while (!agl_list_empty(sai) &&
1264 thread_is_running(thread)) {
1265 clli = agl_first_entry(sai);
1266 cfs_list_del_init(&clli->lli_agl_list);
1267 cfs_spin_unlock(&plli->lli_agl_lock);
1268 ll_agl_trigger(&clli->lli_vfs_inode, sai);
1269 cfs_spin_lock(&plli->lli_agl_lock);
1271 cfs_spin_unlock(&plli->lli_agl_lock);
1276 * chain is exhausted.
1277 * Normal case: continue to the next page.
1279 ll_release_page(page, le32_to_cpu(dp->ldp_flags) &
1281 plli->lli_sa_pos = pos;
1282 sai->sai_in_readpage = 1;
1283 page = ll_get_dir_page(NULL, dir, pos, &chain);
1284 sai->sai_in_readpage = 0;
1286 LASSERT(le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1287 ll_release_page(page, 1);
1289 * go into overflow page.
1296 if (sai->sai_agl_valid) {
1297 cfs_spin_lock(&plli->lli_agl_lock);
1298 thread_set_flags(agl_thread, SVC_STOPPING);
1299 cfs_spin_unlock(&plli->lli_agl_lock);
1300 cfs_waitq_signal(&agl_thread->t_ctl_waitq);
1302 CDEBUG(D_READA, "stop agl thread: [pid %d]\n",
1304 l_wait_event(agl_thread->t_ctl_waitq,
1305 thread_is_stopped(agl_thread),
1308 /* Set agl_thread flags anyway. */
1309 thread_set_flags(&sai->sai_agl_thread, SVC_STOPPED);
1311 ll_dir_chain_fini(&chain);
1312 cfs_spin_lock(&plli->lli_sa_lock);
1313 if (!sa_received_empty(sai)) {
1314 thread_set_flags(thread, SVC_STOPPING);
1315 cfs_spin_unlock(&plli->lli_sa_lock);
1317 /* To release the resources held by received entries. */
1318 while (!sa_received_empty(sai))
1319 do_statahead_interpret(sai, NULL);
1321 cfs_spin_lock(&plli->lli_sa_lock);
1323 thread_set_flags(thread, SVC_STOPPED);
1324 cfs_spin_unlock(&plli->lli_sa_lock);
1325 cfs_waitq_signal(&sai->sai_waitq);
1326 cfs_waitq_signal(&thread->t_ctl_waitq);
1329 CDEBUG(D_READA, "statahead thread stopped: [pid %d] [parent %.*s]\n",
1330 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1335 * called in ll_file_release().
1337 void ll_stop_statahead(struct inode *dir, void *key)
1339 struct ll_inode_info *lli = ll_i2info(dir);
1341 if (unlikely(key == NULL))
1344 cfs_spin_lock(&lli->lli_sa_lock);
1345 if (lli->lli_opendir_key != key || lli->lli_opendir_pid == 0) {
1346 cfs_spin_unlock(&lli->lli_sa_lock);
1350 lli->lli_opendir_key = NULL;
1353 struct l_wait_info lwi = { 0 };
1354 struct ptlrpc_thread *thread = &lli->lli_sai->sai_thread;
1356 if (!thread_is_stopped(thread)) {
1357 thread_set_flags(thread, SVC_STOPPING);
1358 cfs_spin_unlock(&lli->lli_sa_lock);
1359 cfs_waitq_signal(&thread->t_ctl_waitq);
1361 CDEBUG(D_READA, "stop statahead thread: [pid %d]\n",
1363 l_wait_event(thread->t_ctl_waitq,
1364 thread_is_stopped(thread),
1367 cfs_spin_unlock(&lli->lli_sa_lock);
1371 * Put the ref which was held when first statahead_enter.
1372 * It maybe not the last ref for some statahead requests
1375 ll_sai_put(lli->lli_sai);
1377 lli->lli_opendir_pid = 0;
1378 cfs_spin_unlock(&lli->lli_sa_lock);
1384 * not first dirent, or is "."
1386 LS_NONE_FIRST_DE = 0,
1388 * the first non-hidden dirent
1392 * the first hidden dirent, that is "."
1397 static int is_first_dirent(struct inode *dir, struct dentry *dentry)
1399 struct ll_inode_info *lli = ll_i2info(dir);
1400 struct ll_dir_chain chain;
1401 struct qstr *target = &dentry->d_name;
1405 int rc = LS_NONE_FIRST_DE;
1408 lli->lli_sa_pos = 0;
1409 ll_dir_chain_init(&chain);
1410 page = ll_get_dir_page(NULL, dir, pos, &chain);
1413 struct lu_dirpage *dp;
1414 struct lu_dirent *ent;
1417 struct ll_inode_info *lli = ll_i2info(dir);
1420 CERROR("error reading dir "DFID" at "LPU64": "
1421 "[rc %d] [parent %u]\n",
1422 PFID(ll_inode2fid(dir)), pos,
1423 rc, lli->lli_opendir_pid);
1427 dp = page_address(page);
1428 for (ent = lu_dirent_start(dp); ent != NULL;
1429 ent = lu_dirent_next(ent)) {
1434 hash = le64_to_cpu(ent->lde_hash);
1435 /* The ll_get_dir_page() can return any page containing
1436 * the given hash which may be not the start hash. */
1437 if (unlikely(hash < pos))
1440 namelen = le16_to_cpu(ent->lde_namelen);
1441 if (unlikely(namelen == 0))
1443 * skip dummy record.
1447 name = ent->lde_name;
1448 if (name[0] == '.') {
1454 else if (name[1] == '.' && namelen == 2)
1465 if (dot_de && target->name[0] != '.') {
1466 CDEBUG(D_READA, "%.*s skip hidden file %.*s\n",
1467 target->len, target->name,
1472 if (target->len != namelen ||
1473 memcmp(target->name, name, namelen) != 0)
1474 rc = LS_NONE_FIRST_DE;
1478 rc = LS_FIRST_DOT_DE;
1480 ll_release_page(page, 0);
1483 pos = le64_to_cpu(dp->ldp_hash_end);
1484 if (pos == MDS_DIR_END_OFF) {
1486 * End of directory reached.
1488 ll_release_page(page, 0);
1492 * chain is exhausted
1493 * Normal case: continue to the next page.
1495 ll_release_page(page, le32_to_cpu(dp->ldp_flags) &
1497 lli->lli_sa_pos = pos;
1498 page = ll_get_dir_page(NULL, dir, pos, &chain);
1501 * go into overflow page.
1503 LASSERT(le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1504 ll_release_page(page, 1);
1510 ll_dir_chain_fini(&chain);
1515 ll_sai_unplug(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
1517 struct ptlrpc_thread *thread = &sai->sai_thread;
1518 struct ll_sb_info *sbi = ll_i2sbi(sai->sai_inode);
1522 if (entry != NULL && entry->se_stat == SA_ENTRY_SUCC)
1527 ll_sa_entry_fini(sai, entry);
1530 sai->sai_consecutive_miss = 0;
1531 sai->sai_max = min(2 * sai->sai_max, sbi->ll_sa_max);
1533 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
1536 sai->sai_consecutive_miss++;
1537 if (sa_low_hit(sai) && thread_is_running(thread)) {
1538 atomic_inc(&sbi->ll_sa_wrong);
1539 CDEBUG(D_READA, "Statahead for dir "DFID" hit "
1540 "ratio too low: hit/miss "LPU64"/"LPU64
1541 ", sent/replied "LPU64"/"LPU64", stopping "
1542 "statahead thread: pid %d\n",
1543 PFID(&lli->lli_fid), sai->sai_hit,
1544 sai->sai_miss, sai->sai_sent,
1545 sai->sai_replied, cfs_curproc_pid());
1546 cfs_spin_lock(&lli->lli_sa_lock);
1547 if (!thread_is_stopped(thread))
1548 thread_set_flags(thread, SVC_STOPPING);
1549 cfs_spin_unlock(&lli->lli_sa_lock);
1553 if (!thread_is_stopped(thread))
1554 cfs_waitq_signal(&thread->t_ctl_waitq);
1560 * Start statahead thread if this is the first dir entry.
1561 * Otherwise if a thread is started already, wait it until it is ahead of me.
1562 * \retval 1 -- find entry with lock in cache, the caller needs to do
1564 * \retval 0 -- find entry in cache, but without lock, the caller needs
1566 * \retval others -- the caller need to process as non-statahead.
1568 int do_statahead_enter(struct inode *dir, struct dentry **dentryp,
1571 struct ll_inode_info *lli = ll_i2info(dir);
1572 struct ll_statahead_info *sai = lli->lli_sai;
1573 struct dentry *parent;
1574 struct ll_sa_entry *entry;
1575 struct ptlrpc_thread *thread;
1576 struct l_wait_info lwi = { 0 };
1580 LASSERT(lli->lli_opendir_pid == cfs_curproc_pid());
1583 thread = &sai->sai_thread;
1584 if (unlikely(thread_is_stopped(thread) &&
1585 cfs_list_empty(&sai->sai_entries_stated))) {
1586 /* to release resource */
1587 ll_stop_statahead(dir, lli->lli_opendir_key);
1591 if ((*dentryp)->d_name.name[0] == '.') {
1592 if (sai->sai_ls_all ||
1593 sai->sai_miss_hidden >= sai->sai_skip_hidden) {
1595 * Hidden dentry is the first one, or statahead
1596 * thread does not skip so many hidden dentries
1597 * before "sai_ls_all" enabled as below.
1600 if (!sai->sai_ls_all)
1602 * It maybe because hidden dentry is not
1603 * the first one, "sai_ls_all" was not
1604 * set, then "ls -al" missed. Enable
1605 * "sai_ls_all" for such case.
1607 sai->sai_ls_all = 1;
1610 * Such "getattr" has been skipped before
1611 * "sai_ls_all" enabled as above.
1613 sai->sai_miss_hidden++;
1618 entry = ll_sa_entry_get_byname(sai, &(*dentryp)->d_name);
1619 if (entry == NULL || only_unplug) {
1620 ll_sai_unplug(sai, entry);
1621 RETURN(entry ? 1 : -EAGAIN);
1624 while (!ll_sa_entry_stated(entry) &&
1625 sai->sai_in_readpage &&
1626 !sa_received_empty(sai))
1627 do_statahead_interpret(sai, entry);
1629 if (!ll_sa_entry_stated(entry)) {
1630 sai->sai_index_wait = entry->se_index;
1631 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(30), NULL,
1632 LWI_ON_SIGNAL_NOOP, NULL);
1633 rc = l_wait_event(sai->sai_waitq,
1634 ll_sa_entry_stated(entry) ||
1635 thread_is_stopped(thread),
1638 ll_sai_unplug(sai, entry);
1643 if (entry->se_stat == SA_ENTRY_SUCC &&
1644 entry->se_inode != NULL) {
1645 struct inode *inode = entry->se_inode;
1646 struct lookup_intent it = { .it_op = IT_GETATTR,
1647 .d.lustre.it_lock_handle =
1649 struct ll_dentry_data *lld;
1652 rc = md_revalidate_lock(ll_i2mdexp(dir), &it,
1653 ll_inode2fid(inode), &bits);
1655 if ((*dentryp)->d_inode == NULL) {
1656 *dentryp = ll_find_alias(inode,
1658 lld = ll_d2d(*dentryp);
1659 if (unlikely(lld == NULL))
1660 ll_dops_init(*dentryp, 1, 1);
1661 } else if ((*dentryp)->d_inode != inode) {
1662 /* revalidate, but inode is recreated */
1664 "stale dentry %.*s inode %lu/%u, "
1665 "statahead inode %lu/%u\n",
1666 (*dentryp)->d_name.len,
1667 (*dentryp)->d_name.name,
1668 (*dentryp)->d_inode->i_ino,
1669 (*dentryp)->d_inode->i_generation,
1671 inode->i_generation);
1672 ll_sai_unplug(sai, entry);
1675 ll_dentry_rehash(*dentryp, 0);
1678 entry->se_inode = NULL;
1680 ll_dentry_reset_flags(*dentryp, bits);
1681 ll_intent_release(&it);
1685 ll_sai_unplug(sai, entry);
1689 /* I am the "lli_opendir_pid" owner, only me can set "lli_sai". */
1690 rc = is_first_dirent(dir, *dentryp);
1691 if (rc == LS_NONE_FIRST_DE)
1692 /* It is not "ls -{a}l" operation, no need statahead for it. */
1693 GOTO(out, rc = -EAGAIN);
1695 sai = ll_sai_alloc();
1697 GOTO(out, rc = -ENOMEM);
1699 sai->sai_ls_all = (rc == LS_FIRST_DOT_DE);
1700 sai->sai_inode = igrab(dir);
1701 if (unlikely(sai->sai_inode == NULL)) {
1702 CWARN("Do not start stat ahead on dying inode "DFID"\n",
1703 PFID(&lli->lli_fid));
1704 GOTO(out, rc = -ESTALE);
1707 /* get parent reference count here, and put it in ll_statahead_thread */
1708 parent = dget((*dentryp)->d_parent);
1709 if (unlikely(sai->sai_inode != parent->d_inode)) {
1710 struct ll_inode_info *nlli = ll_i2info(parent->d_inode);
1712 CWARN("Race condition, someone changed %.*s just now: "
1713 "old parent "DFID", new parent "DFID"\n",
1714 (*dentryp)->d_name.len, (*dentryp)->d_name.name,
1715 PFID(&lli->lli_fid), PFID(&nlli->lli_fid));
1717 iput(sai->sai_inode);
1718 GOTO(out, rc = -EAGAIN);
1721 CDEBUG(D_READA, "start statahead thread: [pid %d] [parent %.*s]\n",
1722 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1725 rc = cfs_create_thread(ll_statahead_thread, parent, 0);
1726 thread = &sai->sai_thread;
1728 CERROR("can't start ll_sa thread, rc: %d\n", rc);
1730 lli->lli_opendir_key = NULL;
1731 thread_set_flags(thread, SVC_STOPPED);
1732 thread_set_flags(&sai->sai_agl_thread, SVC_STOPPED);
1734 LASSERT(lli->lli_sai == NULL);
1738 l_wait_event(thread->t_ctl_waitq,
1739 thread_is_running(thread) || thread_is_stopped(thread),
1743 * We don't stat-ahead for the first dirent since we are already in
1751 cfs_spin_lock(&lli->lli_sa_lock);
1752 lli->lli_opendir_key = NULL;
1753 lli->lli_opendir_pid = 0;
1754 cfs_spin_unlock(&lli->lli_sa_lock);