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/highmem.h>
41 #include <linux/pagemap.h>
43 #define DEBUG_SUBSYSTEM S_LLITE
45 #include <obd_support.h>
46 #include <lustre_lite.h>
47 #include <lustre_dlm.h>
48 #include "llite_internal.h"
50 #define SA_OMITTED_ENTRY_MAX 8ULL
53 /** negative values are for error cases */
54 SA_ENTRY_INIT = 0, /** init entry */
55 SA_ENTRY_SUCC = 1, /** stat succeed */
56 SA_ENTRY_INVA = 2, /** invalid entry */
57 SA_ENTRY_DEST = 3, /** entry to be destroyed */
61 /* link into sai->sai_entries_{sent,received,stated} */
63 /* link into sai hash table locally */
65 /* entry reference count */
66 cfs_atomic_t se_refcount;
67 /* entry index in the sai */
69 /* low layer ldlm lock handle */
73 /* entry size, contains name */
75 /* pointer to async getattr enqueue info */
76 struct md_enqueue_info *se_minfo;
77 /* pointer to the async getattr request */
78 struct ptlrpc_request *se_req;
79 /* pointer to the target inode */
80 struct inode *se_inode;
85 static unsigned int sai_generation = 0;
86 static DEFINE_SPINLOCK(sai_generation_lock);
88 static inline int ll_sa_entry_unlinked(struct ll_sa_entry *entry)
90 return cfs_list_empty(&entry->se_list);
93 static inline int ll_sa_entry_unhashed(struct ll_sa_entry *entry)
95 return cfs_list_empty(&entry->se_hash);
99 * The entry only can be released by the caller, it is necessary to hold lock.
101 static inline int ll_sa_entry_stated(struct ll_sa_entry *entry)
104 return (entry->se_stat != SA_ENTRY_INIT);
107 static inline int ll_sa_entry_hash(int val)
109 return val & LL_SA_CACHE_MASK;
113 * Insert entry to hash SA table.
116 ll_sa_entry_enhash(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
118 int i = ll_sa_entry_hash(entry->se_qstr.hash);
120 spin_lock(&sai->sai_cache_lock[i]);
121 cfs_list_add_tail(&entry->se_hash, &sai->sai_cache[i]);
122 spin_unlock(&sai->sai_cache_lock[i]);
126 * Remove entry from SA table.
129 ll_sa_entry_unhash(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
131 int i = ll_sa_entry_hash(entry->se_qstr.hash);
133 spin_lock(&sai->sai_cache_lock[i]);
134 cfs_list_del_init(&entry->se_hash);
135 spin_unlock(&sai->sai_cache_lock[i]);
138 static inline int agl_should_run(struct ll_statahead_info *sai,
141 return (inode != NULL && S_ISREG(inode->i_mode) && sai->sai_agl_valid);
144 static inline struct ll_sa_entry *
145 sa_first_received_entry(struct ll_statahead_info *sai)
147 return cfs_list_entry(sai->sai_entries_received.next,
148 struct ll_sa_entry, se_list);
151 static inline struct ll_inode_info *
152 agl_first_entry(struct ll_statahead_info *sai)
154 return cfs_list_entry(sai->sai_entries_agl.next,
155 struct ll_inode_info, lli_agl_list);
158 static inline int sa_sent_full(struct ll_statahead_info *sai)
160 return cfs_atomic_read(&sai->sai_cache_count) >= sai->sai_max;
163 static inline int sa_received_empty(struct ll_statahead_info *sai)
165 return cfs_list_empty(&sai->sai_entries_received);
168 static inline int agl_list_empty(struct ll_statahead_info *sai)
170 return cfs_list_empty(&sai->sai_entries_agl);
174 * (1) hit ratio less than 80%
176 * (2) consecutive miss more than 8
177 * then means low hit.
179 static inline int sa_low_hit(struct ll_statahead_info *sai)
181 return ((sai->sai_hit > 7 && sai->sai_hit < 4 * sai->sai_miss) ||
182 (sai->sai_consecutive_miss > 8));
186 * If the given index is behind of statahead window more than
187 * SA_OMITTED_ENTRY_MAX, then it is old.
189 static inline int is_omitted_entry(struct ll_statahead_info *sai, __u64 index)
191 return ((__u64)sai->sai_max + index + SA_OMITTED_ENTRY_MAX <
196 * Insert it into sai_entries_sent tail when init.
198 static struct ll_sa_entry *
199 ll_sa_entry_alloc(struct ll_statahead_info *sai, __u64 index,
200 const char *name, int len)
202 struct ll_inode_info *lli;
203 struct ll_sa_entry *entry;
208 entry_size = sizeof(struct ll_sa_entry) + (len & ~3) + 4;
209 OBD_ALLOC(entry, entry_size);
210 if (unlikely(entry == NULL))
211 RETURN(ERR_PTR(-ENOMEM));
213 CDEBUG(D_READA, "alloc sai entry %.*s(%p) index "LPU64"\n",
214 len, name, entry, index);
216 entry->se_index = index;
219 * Statahead entry reference rules:
221 * 1) When statahead entry is initialized, its reference is set as 2.
222 * One reference is used by the directory scanner. When the scanner
223 * searches the statahead cache for the given name, it can perform
224 * lockless hash lookup (only the scanner can remove entry from hash
225 * list), and once found, it needn't to call "atomic_inc()" for the
226 * entry reference. So the performance is improved. After using the
227 * statahead entry, the scanner will call "atomic_dec()" to drop the
228 * reference held when initialization. If it is the last reference,
229 * the statahead entry will be freed.
231 * 2) All other threads, including statahead thread and ptlrpcd thread,
232 * when they process the statahead entry, the reference for target
233 * should be held to guarantee the entry will not be released by the
234 * directory scanner. After processing the entry, these threads will
235 * drop the entry reference. If it is the last reference, the entry
238 * The second reference when initializes the statahead entry is used
239 * by the statahead thread, following the rule 2).
241 cfs_atomic_set(&entry->se_refcount, 2);
242 entry->se_stat = SA_ENTRY_INIT;
243 entry->se_size = entry_size;
244 dname = (char *)entry + sizeof(struct ll_sa_entry);
245 memcpy(dname, name, len);
247 entry->se_qstr.hash = full_name_hash(name, len);
248 entry->se_qstr.len = len;
249 entry->se_qstr.name = dname;
251 lli = ll_i2info(sai->sai_inode);
252 spin_lock(&lli->lli_sa_lock);
253 cfs_list_add_tail(&entry->se_list, &sai->sai_entries_sent);
254 spin_unlock(&lli->lli_sa_lock);
256 cfs_atomic_inc(&sai->sai_cache_count);
257 ll_sa_entry_enhash(sai, entry);
263 * Used by the directory scanner to search entry with name.
265 * Only the caller can remove the entry from hash, so it is unnecessary to hold
266 * hash lock. It is caller's duty to release the init refcount on the entry, so
267 * it is also unnecessary to increase refcount on the entry.
269 static struct ll_sa_entry *
270 ll_sa_entry_get_byname(struct ll_statahead_info *sai, const struct qstr *qstr)
272 struct ll_sa_entry *entry;
273 int i = ll_sa_entry_hash(qstr->hash);
275 cfs_list_for_each_entry(entry, &sai->sai_cache[i], se_hash) {
276 if (entry->se_qstr.hash == qstr->hash &&
277 entry->se_qstr.len == qstr->len &&
278 memcmp(entry->se_qstr.name, qstr->name, qstr->len) == 0)
285 * Used by the async getattr request callback to find entry with index.
287 * Inside lli_sa_lock to prevent others to change the list during the search.
288 * It needs to increase entry refcount before returning to guarantee that the
289 * entry cannot be freed by others.
291 static struct ll_sa_entry *
292 ll_sa_entry_get_byindex(struct ll_statahead_info *sai, __u64 index)
294 struct ll_sa_entry *entry;
296 cfs_list_for_each_entry(entry, &sai->sai_entries_sent, se_list) {
297 if (entry->se_index == index) {
298 cfs_atomic_inc(&entry->se_refcount);
301 if (entry->se_index > index)
307 static void ll_sa_entry_cleanup(struct ll_statahead_info *sai,
308 struct ll_sa_entry *entry)
310 struct md_enqueue_info *minfo = entry->se_minfo;
311 struct ptlrpc_request *req = entry->se_req;
314 entry->se_minfo = NULL;
315 ll_intent_release(&minfo->mi_it);
321 entry->se_req = NULL;
322 ptlrpc_req_finished(req);
326 static void ll_sa_entry_put(struct ll_statahead_info *sai,
327 struct ll_sa_entry *entry)
329 if (cfs_atomic_dec_and_test(&entry->se_refcount)) {
330 CDEBUG(D_READA, "free sai entry %.*s(%p) index "LPU64"\n",
331 entry->se_qstr.len, entry->se_qstr.name, entry,
334 LASSERT(ll_sa_entry_unhashed(entry));
335 LASSERT(ll_sa_entry_unlinked(entry));
337 ll_sa_entry_cleanup(sai, entry);
339 iput(entry->se_inode);
341 OBD_FREE(entry, entry->se_size);
342 cfs_atomic_dec(&sai->sai_cache_count);
347 do_sai_entry_fini(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
349 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
351 ll_sa_entry_unhash(sai, entry);
353 spin_lock(&lli->lli_sa_lock);
354 entry->se_stat = SA_ENTRY_DEST;
355 if (likely(!ll_sa_entry_unlinked(entry)))
356 cfs_list_del_init(&entry->se_list);
357 spin_unlock(&lli->lli_sa_lock);
359 ll_sa_entry_put(sai, entry);
363 * Delete it from sai_entries_stated list when fini.
366 ll_sa_entry_fini(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
368 struct ll_sa_entry *pos, *next;
371 do_sai_entry_fini(sai, entry);
373 /* drop old entry from sent list */
374 cfs_list_for_each_entry_safe(pos, next, &sai->sai_entries_sent,
376 if (is_omitted_entry(sai, pos->se_index))
377 do_sai_entry_fini(sai, pos);
382 /* drop old entry from stated list */
383 cfs_list_for_each_entry_safe(pos, next, &sai->sai_entries_stated,
385 if (is_omitted_entry(sai, pos->se_index))
386 do_sai_entry_fini(sai, pos);
393 * Inside lli_sa_lock.
396 do_sai_entry_to_stated(struct ll_statahead_info *sai,
397 struct ll_sa_entry *entry, int rc)
399 struct ll_sa_entry *se;
400 cfs_list_t *pos = &sai->sai_entries_stated;
402 if (!ll_sa_entry_unlinked(entry))
403 cfs_list_del_init(&entry->se_list);
405 cfs_list_for_each_entry_reverse(se, &sai->sai_entries_stated, se_list) {
406 if (se->se_index < entry->se_index) {
412 cfs_list_add(&entry->se_list, pos);
417 * Move entry to sai_entries_stated and sort with the index.
418 * \retval 1 -- entry to be destroyed.
419 * \retval 0 -- entry is inserted into stated list.
422 ll_sa_entry_to_stated(struct ll_statahead_info *sai,
423 struct ll_sa_entry *entry, int rc)
425 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
428 ll_sa_entry_cleanup(sai, entry);
430 spin_lock(&lli->lli_sa_lock);
431 if (likely(entry->se_stat != SA_ENTRY_DEST)) {
432 do_sai_entry_to_stated(sai, entry, rc);
435 spin_unlock(&lli->lli_sa_lock);
441 * Insert inode into the list of sai_entries_agl.
443 static void ll_agl_add(struct ll_statahead_info *sai,
444 struct inode *inode, int index)
446 struct ll_inode_info *child = ll_i2info(inode);
447 struct ll_inode_info *parent = ll_i2info(sai->sai_inode);
450 spin_lock(&child->lli_agl_lock);
451 if (child->lli_agl_index == 0) {
452 child->lli_agl_index = index;
453 spin_unlock(&child->lli_agl_lock);
455 LASSERT(cfs_list_empty(&child->lli_agl_list));
458 spin_lock(&parent->lli_agl_lock);
459 if (agl_list_empty(sai))
461 cfs_list_add_tail(&child->lli_agl_list, &sai->sai_entries_agl);
462 spin_unlock(&parent->lli_agl_lock);
464 spin_unlock(&child->lli_agl_lock);
468 cfs_waitq_signal(&sai->sai_agl_thread.t_ctl_waitq);
471 static struct ll_statahead_info *ll_sai_alloc(void)
473 struct ll_statahead_info *sai;
481 cfs_atomic_set(&sai->sai_refcount, 1);
483 spin_lock(&sai_generation_lock);
484 sai->sai_generation = ++sai_generation;
485 if (unlikely(sai_generation == 0))
486 sai->sai_generation = ++sai_generation;
487 spin_unlock(&sai_generation_lock);
489 sai->sai_max = LL_SA_RPC_MIN;
491 cfs_waitq_init(&sai->sai_waitq);
492 cfs_waitq_init(&sai->sai_thread.t_ctl_waitq);
493 cfs_waitq_init(&sai->sai_agl_thread.t_ctl_waitq);
495 CFS_INIT_LIST_HEAD(&sai->sai_entries_sent);
496 CFS_INIT_LIST_HEAD(&sai->sai_entries_received);
497 CFS_INIT_LIST_HEAD(&sai->sai_entries_stated);
498 CFS_INIT_LIST_HEAD(&sai->sai_entries_agl);
500 for (i = 0; i < LL_SA_CACHE_SIZE; i++) {
501 CFS_INIT_LIST_HEAD(&sai->sai_cache[i]);
502 spin_lock_init(&sai->sai_cache_lock[i]);
504 cfs_atomic_set(&sai->sai_cache_count, 0);
509 static inline struct ll_statahead_info *
510 ll_sai_get(struct ll_statahead_info *sai)
512 cfs_atomic_inc(&sai->sai_refcount);
516 static void ll_sai_put(struct ll_statahead_info *sai)
518 struct inode *inode = sai->sai_inode;
519 struct ll_inode_info *lli = ll_i2info(inode);
522 if (cfs_atomic_dec_and_lock(&sai->sai_refcount, &lli->lli_sa_lock)) {
523 struct ll_sa_entry *entry, *next;
525 if (unlikely(cfs_atomic_read(&sai->sai_refcount) > 0)) {
526 /* It is race case, the interpret callback just hold
527 * a reference count */
528 spin_unlock(&lli->lli_sa_lock);
532 LASSERT(lli->lli_opendir_key == NULL);
533 LASSERT(thread_is_stopped(&sai->sai_thread));
534 LASSERT(thread_is_stopped(&sai->sai_agl_thread));
537 lli->lli_opendir_pid = 0;
538 spin_unlock(&lli->lli_sa_lock);
540 if (sai->sai_sent > sai->sai_replied)
541 CDEBUG(D_READA,"statahead for dir "DFID" does not "
542 "finish: [sent:"LPU64"] [replied:"LPU64"]\n",
544 sai->sai_sent, sai->sai_replied);
546 cfs_list_for_each_entry_safe(entry, next,
547 &sai->sai_entries_sent, se_list)
548 do_sai_entry_fini(sai, entry);
550 LASSERT(sa_received_empty(sai));
552 cfs_list_for_each_entry_safe(entry, next,
553 &sai->sai_entries_stated, se_list)
554 do_sai_entry_fini(sai, entry);
556 LASSERT(cfs_atomic_read(&sai->sai_cache_count) == 0);
557 LASSERT(agl_list_empty(sai));
566 /* Do NOT forget to drop inode refcount when into sai_entries_agl. */
567 static void ll_agl_trigger(struct inode *inode, struct ll_statahead_info *sai)
569 struct ll_inode_info *lli = ll_i2info(inode);
570 __u64 index = lli->lli_agl_index;
574 LASSERT(cfs_list_empty(&lli->lli_agl_list));
576 /* AGL maybe fall behind statahead with one entry */
577 if (is_omitted_entry(sai, index + 1)) {
578 lli->lli_agl_index = 0;
583 /* Someone is in glimpse (sync or async), do nothing. */
584 rc = down_write_trylock(&lli->lli_glimpse_sem);
586 lli->lli_agl_index = 0;
592 * Someone triggered glimpse within 1 sec before.
593 * 1) The former glimpse succeeded with glimpse lock granted by OST, and
594 * if the lock is still cached on client, AGL needs to do nothing. If
595 * it is cancelled by other client, AGL maybe cannot obtaion new lock
596 * for no glimpse callback triggered by AGL.
597 * 2) The former glimpse succeeded, but OST did not grant glimpse lock.
598 * Under such case, it is quite possible that the OST will not grant
599 * glimpse lock for AGL also.
600 * 3) The former glimpse failed, compared with other two cases, it is
601 * relative rare. AGL can ignore such case, and it will not muchly
602 * affect the performance.
604 if (lli->lli_glimpse_time != 0 &&
605 cfs_time_before(cfs_time_shift(-1), lli->lli_glimpse_time)) {
606 up_write(&lli->lli_glimpse_sem);
607 lli->lli_agl_index = 0;
612 CDEBUG(D_READA, "Handling (init) async glimpse: inode = "
613 DFID", idx = "LPU64"\n", PFID(&lli->lli_fid), index);
616 lli->lli_agl_index = 0;
617 lli->lli_glimpse_time = cfs_time_current();
618 up_write(&lli->lli_glimpse_sem);
620 CDEBUG(D_READA, "Handled (init) async glimpse: inode= "
621 DFID", idx = "LPU64", rc = %d\n",
622 PFID(&lli->lli_fid), index, rc);
629 static void do_statahead_interpret(struct ll_statahead_info *sai,
630 struct ll_sa_entry *target)
632 struct inode *dir = sai->sai_inode;
634 struct ll_inode_info *lli = ll_i2info(dir);
635 struct ll_sa_entry *entry;
636 struct md_enqueue_info *minfo;
637 struct lookup_intent *it;
638 struct ptlrpc_request *req;
639 struct mdt_body *body;
643 spin_lock(&lli->lli_sa_lock);
644 if (target != NULL && target->se_req != NULL &&
645 !cfs_list_empty(&target->se_list)) {
647 } else if (unlikely(sa_received_empty(sai))) {
648 spin_unlock(&lli->lli_sa_lock);
651 entry = sa_first_received_entry(sai);
654 cfs_atomic_inc(&entry->se_refcount);
655 cfs_list_del_init(&entry->se_list);
656 spin_unlock(&lli->lli_sa_lock);
658 LASSERT(entry->se_handle != 0);
660 minfo = entry->se_minfo;
663 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
665 GOTO(out, rc = -EFAULT);
667 child = entry->se_inode;
672 LASSERT(fid_is_zero(&minfo->mi_data.op_fid2));
674 /* XXX: No fid in reply, this is probaly cross-ref case.
675 * SA can't handle it yet. */
676 if (body->valid & OBD_MD_MDS)
677 GOTO(out, rc = -EAGAIN);
682 /* unlinked and re-created with the same name */
683 if (unlikely(!lu_fid_eq(&minfo->mi_data.op_fid2, &body->fid1))){
684 entry->se_inode = NULL;
690 it->d.lustre.it_lock_handle = entry->se_handle;
691 rc = md_revalidate_lock(ll_i2mdexp(dir), it, NULL, NULL);
693 GOTO(out, rc = -EAGAIN);
695 rc = ll_prep_inode(&child, req, dir->i_sb);
699 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
700 child, child->i_ino, child->i_generation);
701 ll_set_lock_data(ll_i2sbi(dir)->ll_md_exp, child, it, NULL);
703 entry->se_inode = child;
705 if (agl_should_run(sai, child))
706 ll_agl_add(sai, child, entry->se_index);
711 /* The "ll_sa_entry_to_stated()" will drop related ldlm ibits lock
712 * reference count by calling "ll_intent_drop_lock()" in spite of the
713 * above operations failed or not. Do not worry about calling
714 * "ll_intent_drop_lock()" more than once. */
715 rc = ll_sa_entry_to_stated(sai, entry, rc < 0 ? rc : SA_ENTRY_SUCC);
716 if (rc == 0 && entry->se_index == sai->sai_index_wait && target == NULL)
717 cfs_waitq_signal(&sai->sai_waitq);
718 ll_sa_entry_put(sai, entry);
721 static int ll_statahead_interpret(struct ptlrpc_request *req,
722 struct md_enqueue_info *minfo, int rc)
724 struct lookup_intent *it = &minfo->mi_it;
725 struct inode *dir = minfo->mi_dir;
726 struct ll_inode_info *lli = ll_i2info(dir);
727 struct ll_statahead_info *sai = NULL;
728 struct ll_sa_entry *entry;
732 if (it_disposition(it, DISP_LOOKUP_NEG))
735 spin_lock(&lli->lli_sa_lock);
737 if (unlikely(lli->lli_sai == NULL ||
738 lli->lli_sai->sai_generation != minfo->mi_generation)) {
739 spin_unlock(&lli->lli_sa_lock);
740 GOTO(out, rc = -ESTALE);
742 sai = ll_sai_get(lli->lli_sai);
743 if (unlikely(!thread_is_running(&sai->sai_thread))) {
745 spin_unlock(&lli->lli_sa_lock);
746 GOTO(out, rc = -EBADFD);
749 entry = ll_sa_entry_get_byindex(sai, minfo->mi_cbdata);
752 spin_unlock(&lli->lli_sa_lock);
753 GOTO(out, rc = -EIDRM);
756 cfs_list_del_init(&entry->se_list);
759 do_sai_entry_to_stated(sai, entry, rc);
760 spin_unlock(&lli->lli_sa_lock);
761 if (entry->se_index == sai->sai_index_wait)
762 cfs_waitq_signal(&sai->sai_waitq);
764 entry->se_minfo = minfo;
765 entry->se_req = ptlrpc_request_addref(req);
766 /* Release the async ibits lock ASAP to avoid deadlock
767 * when statahead thread tries to enqueue lock on parent
768 * for readpage and other tries to enqueue lock on child
769 * with parent's lock held, for example: unlink. */
770 entry->se_handle = it->d.lustre.it_lock_handle;
771 ll_intent_drop_lock(it);
772 wakeup = sa_received_empty(sai);
773 cfs_list_add_tail(&entry->se_list,
774 &sai->sai_entries_received);
776 spin_unlock(&lli->lli_sa_lock);
778 cfs_waitq_signal(&sai->sai_thread.t_ctl_waitq);
780 ll_sa_entry_put(sai, entry);
787 ll_intent_release(it);
796 static void sa_args_fini(struct md_enqueue_info *minfo,
797 struct ldlm_enqueue_info *einfo)
799 LASSERT(minfo && einfo);
801 capa_put(minfo->mi_data.op_capa1);
802 capa_put(minfo->mi_data.op_capa2);
808 * There is race condition between "capa_put" and "ll_statahead_interpret" for
809 * accessing "op_data.op_capa[1,2]" as following:
810 * "capa_put" releases "op_data.op_capa[1,2]"'s reference count after calling
811 * "md_intent_getattr_async". But "ll_statahead_interpret" maybe run first, and
812 * fill "op_data.op_capa[1,2]" as POISON, then cause "capa_put" access invalid
813 * "ocapa". So here reserve "op_data.op_capa[1,2]" in "pcapa" before calling
814 * "md_intent_getattr_async".
816 static int sa_args_init(struct inode *dir, struct inode *child,
817 struct ll_sa_entry *entry, struct md_enqueue_info **pmi,
818 struct ldlm_enqueue_info **pei,
819 struct obd_capa **pcapa)
821 struct qstr *qstr = &entry->se_qstr;
822 struct ll_inode_info *lli = ll_i2info(dir);
823 struct md_enqueue_info *minfo;
824 struct ldlm_enqueue_info *einfo;
825 struct md_op_data *op_data;
827 OBD_ALLOC_PTR(einfo);
831 OBD_ALLOC_PTR(minfo);
837 op_data = ll_prep_md_op_data(&minfo->mi_data, dir, child, qstr->name,
838 qstr->len, 0, LUSTRE_OPC_ANY, NULL);
839 if (IS_ERR(op_data)) {
842 return PTR_ERR(op_data);
845 minfo->mi_it.it_op = IT_GETATTR;
846 minfo->mi_dir = igrab(dir);
847 minfo->mi_cb = ll_statahead_interpret;
848 minfo->mi_generation = lli->lli_sai->sai_generation;
849 minfo->mi_cbdata = entry->se_index;
851 einfo->ei_type = LDLM_IBITS;
852 einfo->ei_mode = it_to_lock_mode(&minfo->mi_it);
853 einfo->ei_cb_bl = ll_md_blocking_ast;
854 einfo->ei_cb_cp = ldlm_completion_ast;
855 einfo->ei_cb_gl = NULL;
856 einfo->ei_cbdata = NULL;
860 pcapa[0] = op_data->op_capa1;
861 pcapa[1] = op_data->op_capa2;
866 static int do_sa_lookup(struct inode *dir, struct ll_sa_entry *entry)
868 struct md_enqueue_info *minfo;
869 struct ldlm_enqueue_info *einfo;
870 struct obd_capa *capas[2];
874 rc = sa_args_init(dir, NULL, entry, &minfo, &einfo, capas);
878 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
883 sa_args_fini(minfo, einfo);
890 * similar to ll_revalidate_it().
891 * \retval 1 -- dentry valid
892 * \retval 0 -- will send stat-ahead request
893 * \retval others -- prepare stat-ahead request failed
895 static int do_sa_revalidate(struct inode *dir, struct ll_sa_entry *entry,
896 struct dentry *dentry)
898 struct inode *inode = dentry->d_inode;
899 struct lookup_intent it = { .it_op = IT_GETATTR,
900 .d.lustre.it_lock_handle = 0 };
901 struct md_enqueue_info *minfo;
902 struct ldlm_enqueue_info *einfo;
903 struct obd_capa *capas[2];
907 if (unlikely(inode == NULL))
910 if (d_mountpoint(dentry))
913 if (unlikely(dentry == dentry->d_sb->s_root))
916 entry->se_inode = igrab(inode);
917 rc = md_revalidate_lock(ll_i2mdexp(dir), &it, ll_inode2fid(inode),NULL);
919 entry->se_handle = it.d.lustre.it_lock_handle;
920 ll_intent_release(&it);
924 rc = sa_args_init(dir, inode, entry, &minfo, &einfo, capas);
926 entry->se_inode = NULL;
931 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
936 entry->se_inode = NULL;
938 sa_args_fini(minfo, einfo);
944 static void ll_statahead_one(struct dentry *parent, const char* entry_name,
947 struct inode *dir = parent->d_inode;
948 struct ll_inode_info *lli = ll_i2info(dir);
949 struct ll_statahead_info *sai = lli->lli_sai;
950 struct dentry *dentry = NULL;
951 struct ll_sa_entry *entry;
956 entry = ll_sa_entry_alloc(sai, sai->sai_index, entry_name,
961 dentry = d_lookup(parent, &entry->se_qstr);
963 rc = do_sa_lookup(dir, entry);
965 rc = do_sa_revalidate(dir, entry, dentry);
966 if (rc == 1 && agl_should_run(sai, dentry->d_inode))
967 ll_agl_add(sai, dentry->d_inode, entry->se_index);
974 rc1 = ll_sa_entry_to_stated(sai, entry,
975 rc < 0 ? SA_ENTRY_INVA : SA_ENTRY_SUCC);
976 if (rc1 == 0 && entry->se_index == sai->sai_index_wait)
977 cfs_waitq_signal(&sai->sai_waitq);
983 /* drop one refcount on entry by ll_sa_entry_alloc */
984 ll_sa_entry_put(sai, entry);
989 static int ll_agl_thread(void *arg)
991 struct dentry *parent = (struct dentry *)arg;
992 struct inode *dir = parent->d_inode;
993 struct ll_inode_info *plli = ll_i2info(dir);
994 struct ll_inode_info *clli;
995 struct ll_sb_info *sbi = ll_i2sbi(dir);
996 struct ll_statahead_info *sai = ll_sai_get(plli->lli_sai);
997 struct ptlrpc_thread *thread = &sai->sai_agl_thread;
998 struct l_wait_info lwi = { 0 };
1003 snprintf(pname, 15, "ll_agl_%u", plli->lli_opendir_pid);
1004 cfs_daemonize(pname);
1007 CDEBUG(D_READA, "agl thread started: [pid %d] [parent %.*s]\n",
1008 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1010 atomic_inc(&sbi->ll_agl_total);
1011 spin_lock(&plli->lli_agl_lock);
1012 sai->sai_agl_valid = 1;
1013 thread_set_flags(thread, SVC_RUNNING);
1014 spin_unlock(&plli->lli_agl_lock);
1015 cfs_waitq_signal(&thread->t_ctl_waitq);
1018 l_wait_event(thread->t_ctl_waitq,
1019 !agl_list_empty(sai) ||
1020 !thread_is_running(thread),
1023 if (!thread_is_running(thread))
1026 spin_lock(&plli->lli_agl_lock);
1027 /* The statahead thread maybe help to process AGL entries,
1028 * so check whether list empty again. */
1029 if (!agl_list_empty(sai)) {
1030 clli = agl_first_entry(sai);
1031 cfs_list_del_init(&clli->lli_agl_list);
1032 spin_unlock(&plli->lli_agl_lock);
1033 ll_agl_trigger(&clli->lli_vfs_inode, sai);
1035 spin_unlock(&plli->lli_agl_lock);
1039 spin_lock(&plli->lli_agl_lock);
1040 sai->sai_agl_valid = 0;
1041 while (!agl_list_empty(sai)) {
1042 clli = agl_first_entry(sai);
1043 cfs_list_del_init(&clli->lli_agl_list);
1044 spin_unlock(&plli->lli_agl_lock);
1045 clli->lli_agl_index = 0;
1046 iput(&clli->lli_vfs_inode);
1047 spin_lock(&plli->lli_agl_lock);
1049 thread_set_flags(thread, SVC_STOPPED);
1050 spin_unlock(&plli->lli_agl_lock);
1051 cfs_waitq_signal(&thread->t_ctl_waitq);
1053 CDEBUG(D_READA, "agl thread stopped: [pid %d] [parent %.*s]\n",
1054 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1058 static void ll_start_agl(struct dentry *parent, struct ll_statahead_info *sai)
1060 struct ptlrpc_thread *thread = &sai->sai_agl_thread;
1061 struct l_wait_info lwi = { 0 };
1065 CDEBUG(D_READA, "start agl thread: [pid %d] [parent %.*s]\n",
1066 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1068 rc = cfs_create_thread(ll_agl_thread, parent, 0);
1070 CERROR("can't start ll_agl thread, rc: %d\n", rc);
1071 thread_set_flags(thread, SVC_STOPPED);
1075 l_wait_event(thread->t_ctl_waitq,
1076 thread_is_running(thread) || thread_is_stopped(thread),
1081 static int ll_statahead_thread(void *arg)
1083 struct dentry *parent = (struct dentry *)arg;
1084 struct inode *dir = parent->d_inode;
1085 struct ll_inode_info *plli = ll_i2info(dir);
1086 struct ll_inode_info *clli;
1087 struct ll_sb_info *sbi = ll_i2sbi(dir);
1088 struct ll_statahead_info *sai = ll_sai_get(plli->lli_sai);
1089 struct ptlrpc_thread *thread = &sai->sai_thread;
1090 struct ptlrpc_thread *agl_thread = &sai->sai_agl_thread;
1095 struct ll_dir_chain chain;
1096 struct l_wait_info lwi = { 0 };
1101 snprintf(pname, 15, "ll_sa_%u", plli->lli_opendir_pid);
1102 cfs_daemonize(pname);
1105 CDEBUG(D_READA, "statahead thread started: [pid %d] [parent %.*s]\n",
1106 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1108 if (sbi->ll_flags & LL_SBI_AGL_ENABLED)
1109 ll_start_agl(parent, sai);
1111 atomic_inc(&sbi->ll_sa_total);
1112 spin_lock(&plli->lli_sa_lock);
1113 thread_set_flags(thread, SVC_RUNNING);
1114 spin_unlock(&plli->lli_sa_lock);
1115 cfs_waitq_signal(&thread->t_ctl_waitq);
1117 ll_dir_chain_init(&chain);
1118 page = ll_get_dir_page(dir, pos, &chain);
1121 struct lu_dirpage *dp;
1122 struct lu_dirent *ent;
1126 CDEBUG(D_READA, "error reading dir "DFID" at "LPU64
1127 "/"LPU64": [rc %d] [parent %u]\n",
1128 PFID(ll_inode2fid(dir)), pos, sai->sai_index,
1129 rc, plli->lli_opendir_pid);
1133 dp = page_address(page);
1134 for (ent = lu_dirent_start(dp); ent != NULL;
1135 ent = lu_dirent_next(ent)) {
1140 hash = le64_to_cpu(ent->lde_hash);
1141 if (unlikely(hash < pos))
1143 * Skip until we find target hash value.
1147 namelen = le16_to_cpu(ent->lde_namelen);
1148 if (unlikely(namelen == 0))
1150 * Skip dummy record.
1154 name = ent->lde_name;
1155 if (name[0] == '.') {
1161 } else if (name[1] == '.' && namelen == 2) {
1166 } else if (!sai->sai_ls_all) {
1168 * skip hidden files.
1170 sai->sai_skip_hidden++;
1176 * don't stat-ahead first entry.
1178 if (unlikely(++first == 1))
1182 l_wait_event(thread->t_ctl_waitq,
1183 !sa_sent_full(sai) ||
1184 !sa_received_empty(sai) ||
1185 !agl_list_empty(sai) ||
1186 !thread_is_running(thread),
1190 while (!sa_received_empty(sai))
1191 do_statahead_interpret(sai, NULL);
1193 if (unlikely(!thread_is_running(thread))) {
1194 ll_release_page(page, 0);
1198 /* If no window for metadata statahead, but there are
1199 * some AGL entries to be triggered, then try to help
1200 * to process the AGL entries. */
1201 if (sa_sent_full(sai)) {
1202 spin_lock(&plli->lli_agl_lock);
1203 while (!agl_list_empty(sai)) {
1204 clli = agl_first_entry(sai);
1205 cfs_list_del_init(&clli->lli_agl_list);
1206 spin_unlock(&plli->lli_agl_lock);
1207 ll_agl_trigger(&clli->lli_vfs_inode,
1210 if (!sa_received_empty(sai))
1214 !thread_is_running(thread))) {
1215 ll_release_page(page, 0);
1219 if (!sa_sent_full(sai))
1222 spin_lock(&plli->lli_agl_lock);
1224 spin_unlock(&plli->lli_agl_lock);
1230 ll_statahead_one(parent, name, namelen);
1232 pos = le64_to_cpu(dp->ldp_hash_end);
1233 if (pos == MDS_DIR_END_OFF) {
1235 * End of directory reached.
1237 ll_release_page(page, 0);
1239 l_wait_event(thread->t_ctl_waitq,
1240 !sa_received_empty(sai) ||
1241 sai->sai_sent == sai->sai_replied||
1242 !thread_is_running(thread),
1245 while (!sa_received_empty(sai))
1246 do_statahead_interpret(sai, NULL);
1248 if (unlikely(!thread_is_running(thread)))
1251 if (sai->sai_sent == sai->sai_replied &&
1252 sa_received_empty(sai))
1256 spin_lock(&plli->lli_agl_lock);
1257 while (!agl_list_empty(sai) &&
1258 thread_is_running(thread)) {
1259 clli = agl_first_entry(sai);
1260 cfs_list_del_init(&clli->lli_agl_list);
1261 spin_unlock(&plli->lli_agl_lock);
1262 ll_agl_trigger(&clli->lli_vfs_inode, sai);
1263 spin_lock(&plli->lli_agl_lock);
1265 spin_unlock(&plli->lli_agl_lock);
1270 * chain is exhausted.
1271 * Normal case: continue to the next page.
1273 ll_release_page(page, le32_to_cpu(dp->ldp_flags) &
1275 sai->sai_in_readpage = 1;
1276 page = ll_get_dir_page(dir, pos, &chain);
1277 sai->sai_in_readpage = 0;
1279 LASSERT(le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1280 ll_release_page(page, 1);
1282 * go into overflow page.
1289 if (sai->sai_agl_valid) {
1290 spin_lock(&plli->lli_agl_lock);
1291 thread_set_flags(agl_thread, SVC_STOPPING);
1292 spin_unlock(&plli->lli_agl_lock);
1293 cfs_waitq_signal(&agl_thread->t_ctl_waitq);
1295 CDEBUG(D_READA, "stop agl thread: [pid %d]\n",
1297 l_wait_event(agl_thread->t_ctl_waitq,
1298 thread_is_stopped(agl_thread),
1301 /* Set agl_thread flags anyway. */
1302 thread_set_flags(&sai->sai_agl_thread, SVC_STOPPED);
1304 ll_dir_chain_fini(&chain);
1305 spin_lock(&plli->lli_sa_lock);
1306 if (!sa_received_empty(sai)) {
1307 thread_set_flags(thread, SVC_STOPPING);
1308 spin_unlock(&plli->lli_sa_lock);
1310 /* To release the resources held by received entries. */
1311 while (!sa_received_empty(sai))
1312 do_statahead_interpret(sai, NULL);
1314 spin_lock(&plli->lli_sa_lock);
1316 thread_set_flags(thread, SVC_STOPPED);
1317 spin_unlock(&plli->lli_sa_lock);
1318 cfs_waitq_signal(&sai->sai_waitq);
1319 cfs_waitq_signal(&thread->t_ctl_waitq);
1322 CDEBUG(D_READA, "statahead thread stopped: [pid %d] [parent %.*s]\n",
1323 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1328 * called in ll_file_release().
1330 void ll_stop_statahead(struct inode *dir, void *key)
1332 struct ll_inode_info *lli = ll_i2info(dir);
1334 if (unlikely(key == NULL))
1337 spin_lock(&lli->lli_sa_lock);
1338 if (lli->lli_opendir_key != key || lli->lli_opendir_pid == 0) {
1339 spin_unlock(&lli->lli_sa_lock);
1343 lli->lli_opendir_key = NULL;
1346 struct l_wait_info lwi = { 0 };
1347 struct ptlrpc_thread *thread = &lli->lli_sai->sai_thread;
1349 if (!thread_is_stopped(thread)) {
1350 thread_set_flags(thread, SVC_STOPPING);
1351 spin_unlock(&lli->lli_sa_lock);
1352 cfs_waitq_signal(&thread->t_ctl_waitq);
1354 CDEBUG(D_READA, "stop statahead thread: [pid %d]\n",
1356 l_wait_event(thread->t_ctl_waitq,
1357 thread_is_stopped(thread),
1360 spin_unlock(&lli->lli_sa_lock);
1364 * Put the ref which was held when first statahead_enter.
1365 * It maybe not the last ref for some statahead requests
1368 ll_sai_put(lli->lli_sai);
1370 lli->lli_opendir_pid = 0;
1371 spin_unlock(&lli->lli_sa_lock);
1377 * not first dirent, or is "."
1379 LS_NONE_FIRST_DE = 0,
1381 * the first non-hidden dirent
1385 * the first hidden dirent, that is "."
1390 static int is_first_dirent(struct inode *dir, struct dentry *dentry)
1392 struct ll_dir_chain chain;
1393 struct qstr *target = &dentry->d_name;
1397 int rc = LS_NONE_FIRST_DE;
1400 ll_dir_chain_init(&chain);
1401 page = ll_get_dir_page(dir, pos, &chain);
1404 struct lu_dirpage *dp;
1405 struct lu_dirent *ent;
1408 struct ll_inode_info *lli = ll_i2info(dir);
1411 CERROR("error reading dir "DFID" at "LPU64": "
1412 "[rc %d] [parent %u]\n",
1413 PFID(ll_inode2fid(dir)), pos,
1414 rc, lli->lli_opendir_pid);
1418 dp = page_address(page);
1419 for (ent = lu_dirent_start(dp); ent != NULL;
1420 ent = lu_dirent_next(ent)) {
1425 hash = le64_to_cpu(ent->lde_hash);
1426 /* The ll_get_dir_page() can return any page containing
1427 * the given hash which may be not the start hash. */
1428 if (unlikely(hash < pos))
1431 namelen = le16_to_cpu(ent->lde_namelen);
1432 if (unlikely(namelen == 0))
1434 * skip dummy record.
1438 name = ent->lde_name;
1439 if (name[0] == '.') {
1445 else if (name[1] == '.' && namelen == 2)
1456 if (dot_de && target->name[0] != '.') {
1457 CDEBUG(D_READA, "%.*s skip hidden file %.*s\n",
1458 target->len, target->name,
1463 if (target->len != namelen ||
1464 memcmp(target->name, name, namelen) != 0)
1465 rc = LS_NONE_FIRST_DE;
1469 rc = LS_FIRST_DOT_DE;
1471 ll_release_page(page, 0);
1474 pos = le64_to_cpu(dp->ldp_hash_end);
1475 if (pos == MDS_DIR_END_OFF) {
1477 * End of directory reached.
1479 ll_release_page(page, 0);
1483 * chain is exhausted
1484 * Normal case: continue to the next page.
1486 ll_release_page(page, le32_to_cpu(dp->ldp_flags) &
1488 page = ll_get_dir_page(dir, pos, &chain);
1491 * go into overflow page.
1493 LASSERT(le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1494 ll_release_page(page, 1);
1500 ll_dir_chain_fini(&chain);
1505 ll_sai_unplug(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
1507 struct ptlrpc_thread *thread = &sai->sai_thread;
1508 struct ll_sb_info *sbi = ll_i2sbi(sai->sai_inode);
1512 if (entry != NULL && entry->se_stat == SA_ENTRY_SUCC)
1517 ll_sa_entry_fini(sai, entry);
1520 sai->sai_consecutive_miss = 0;
1521 sai->sai_max = min(2 * sai->sai_max, sbi->ll_sa_max);
1523 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
1526 sai->sai_consecutive_miss++;
1527 if (sa_low_hit(sai) && thread_is_running(thread)) {
1528 atomic_inc(&sbi->ll_sa_wrong);
1529 CDEBUG(D_READA, "Statahead for dir "DFID" hit "
1530 "ratio too low: hit/miss "LPU64"/"LPU64
1531 ", sent/replied "LPU64"/"LPU64", stopping "
1532 "statahead thread: pid %d\n",
1533 PFID(&lli->lli_fid), sai->sai_hit,
1534 sai->sai_miss, sai->sai_sent,
1535 sai->sai_replied, cfs_curproc_pid());
1536 spin_lock(&lli->lli_sa_lock);
1537 if (!thread_is_stopped(thread))
1538 thread_set_flags(thread, SVC_STOPPING);
1539 spin_unlock(&lli->lli_sa_lock);
1543 if (!thread_is_stopped(thread))
1544 cfs_waitq_signal(&thread->t_ctl_waitq);
1550 * Start statahead thread if this is the first dir entry.
1551 * Otherwise if a thread is started already, wait it until it is ahead of me.
1552 * \retval 1 -- find entry with lock in cache, the caller needs to do
1554 * \retval 0 -- find entry in cache, but without lock, the caller needs
1556 * \retval others -- the caller need to process as non-statahead.
1558 int do_statahead_enter(struct inode *dir, struct dentry **dentryp,
1561 struct ll_inode_info *lli = ll_i2info(dir);
1562 struct ll_statahead_info *sai = lli->lli_sai;
1563 struct dentry *parent;
1564 struct ll_sa_entry *entry;
1565 struct ptlrpc_thread *thread;
1566 struct l_wait_info lwi = { 0 };
1570 LASSERT(lli->lli_opendir_pid == cfs_curproc_pid());
1573 thread = &sai->sai_thread;
1574 if (unlikely(thread_is_stopped(thread) &&
1575 cfs_list_empty(&sai->sai_entries_stated))) {
1576 /* to release resource */
1577 ll_stop_statahead(dir, lli->lli_opendir_key);
1581 if ((*dentryp)->d_name.name[0] == '.') {
1582 if (sai->sai_ls_all ||
1583 sai->sai_miss_hidden >= sai->sai_skip_hidden) {
1585 * Hidden dentry is the first one, or statahead
1586 * thread does not skip so many hidden dentries
1587 * before "sai_ls_all" enabled as below.
1590 if (!sai->sai_ls_all)
1592 * It maybe because hidden dentry is not
1593 * the first one, "sai_ls_all" was not
1594 * set, then "ls -al" missed. Enable
1595 * "sai_ls_all" for such case.
1597 sai->sai_ls_all = 1;
1600 * Such "getattr" has been skipped before
1601 * "sai_ls_all" enabled as above.
1603 sai->sai_miss_hidden++;
1608 entry = ll_sa_entry_get_byname(sai, &(*dentryp)->d_name);
1609 if (entry == NULL || only_unplug) {
1610 ll_sai_unplug(sai, entry);
1611 RETURN(entry ? 1 : -EAGAIN);
1614 while (!ll_sa_entry_stated(entry) &&
1615 sai->sai_in_readpage &&
1616 !sa_received_empty(sai))
1617 do_statahead_interpret(sai, entry);
1619 if (!ll_sa_entry_stated(entry)) {
1620 sai->sai_index_wait = entry->se_index;
1621 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(30), NULL,
1622 LWI_ON_SIGNAL_NOOP, NULL);
1623 rc = l_wait_event(sai->sai_waitq,
1624 ll_sa_entry_stated(entry) ||
1625 thread_is_stopped(thread),
1628 ll_sai_unplug(sai, entry);
1633 if (entry->se_stat == SA_ENTRY_SUCC &&
1634 entry->se_inode != NULL) {
1635 struct inode *inode = entry->se_inode;
1636 struct lookup_intent it = { .it_op = IT_GETATTR,
1637 .d.lustre.it_lock_handle =
1641 rc = md_revalidate_lock(ll_i2mdexp(dir), &it,
1642 ll_inode2fid(inode), &bits);
1644 if ((*dentryp)->d_inode == NULL) {
1645 *dentryp = ll_splice_alias(inode,
1647 } else if ((*dentryp)->d_inode != inode) {
1648 /* revalidate, but inode is recreated */
1650 "stale dentry %.*s inode %lu/%u, "
1651 "statahead inode %lu/%u\n",
1652 (*dentryp)->d_name.len,
1653 (*dentryp)->d_name.name,
1654 (*dentryp)->d_inode->i_ino,
1655 (*dentryp)->d_inode->i_generation,
1657 inode->i_generation);
1658 ll_sai_unplug(sai, entry);
1663 entry->se_inode = NULL;
1665 if ((bits & MDS_INODELOCK_LOOKUP) &&
1666 d_lustre_invalid(*dentryp))
1667 d_lustre_revalidate(*dentryp);
1668 ll_intent_release(&it);
1672 ll_sai_unplug(sai, entry);
1676 /* I am the "lli_opendir_pid" owner, only me can set "lli_sai". */
1677 rc = is_first_dirent(dir, *dentryp);
1678 if (rc == LS_NONE_FIRST_DE)
1679 /* It is not "ls -{a}l" operation, no need statahead for it. */
1680 GOTO(out, rc = -EAGAIN);
1682 sai = ll_sai_alloc();
1684 GOTO(out, rc = -ENOMEM);
1686 sai->sai_ls_all = (rc == LS_FIRST_DOT_DE);
1687 sai->sai_inode = igrab(dir);
1688 if (unlikely(sai->sai_inode == NULL)) {
1689 CWARN("Do not start stat ahead on dying inode "DFID"\n",
1690 PFID(&lli->lli_fid));
1691 GOTO(out, rc = -ESTALE);
1694 /* get parent reference count here, and put it in ll_statahead_thread */
1695 parent = dget((*dentryp)->d_parent);
1696 if (unlikely(sai->sai_inode != parent->d_inode)) {
1697 struct ll_inode_info *nlli = ll_i2info(parent->d_inode);
1699 CWARN("Race condition, someone changed %.*s just now: "
1700 "old parent "DFID", new parent "DFID"\n",
1701 (*dentryp)->d_name.len, (*dentryp)->d_name.name,
1702 PFID(&lli->lli_fid), PFID(&nlli->lli_fid));
1704 iput(sai->sai_inode);
1705 GOTO(out, rc = -EAGAIN);
1708 CDEBUG(D_READA, "start statahead thread: [pid %d] [parent %.*s]\n",
1709 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1712 rc = cfs_create_thread(ll_statahead_thread, parent, 0);
1713 thread = &sai->sai_thread;
1715 CERROR("can't start ll_sa thread, rc: %d\n", rc);
1717 lli->lli_opendir_key = NULL;
1718 thread_set_flags(thread, SVC_STOPPED);
1719 thread_set_flags(&sai->sai_agl_thread, SVC_STOPPED);
1721 LASSERT(lli->lli_sai == NULL);
1725 l_wait_event(thread->t_ctl_waitq,
1726 thread_is_running(thread) || thread_is_stopped(thread),
1730 * We don't stat-ahead for the first dirent since we are already in
1738 spin_lock(&lli->lli_sa_lock);
1739 lli->lli_opendir_key = NULL;
1740 lli->lli_opendir_pid = 0;
1741 spin_unlock(&lli->lli_sa_lock);