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, 2013, Intel Corporation.
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 */
63 /* link into sai->sai_entries_{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 DEFINE_SPINLOCK(sai_generation_lock);
90 static inline int ll_sa_entry_unhashed(struct ll_sa_entry *entry)
92 return cfs_list_empty(&entry->se_hash);
96 * The entry only can be released by the caller, it is necessary to hold lock.
98 static inline int ll_sa_entry_stated(struct ll_sa_entry *entry)
101 return (entry->se_stat != SA_ENTRY_INIT);
104 static inline int ll_sa_entry_hash(int val)
106 return val & LL_SA_CACHE_MASK;
110 * Insert entry to hash SA table.
113 ll_sa_entry_enhash(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
115 int i = ll_sa_entry_hash(entry->se_qstr.hash);
117 spin_lock(&sai->sai_cache_lock[i]);
118 cfs_list_add_tail(&entry->se_hash, &sai->sai_cache[i]);
119 spin_unlock(&sai->sai_cache_lock[i]);
123 * Remove entry from SA table.
126 ll_sa_entry_unhash(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
128 int i = ll_sa_entry_hash(entry->se_qstr.hash);
130 spin_lock(&sai->sai_cache_lock[i]);
131 cfs_list_del_init(&entry->se_hash);
132 spin_unlock(&sai->sai_cache_lock[i]);
135 static inline int agl_should_run(struct ll_statahead_info *sai,
138 return (inode != NULL && S_ISREG(inode->i_mode) && sai->sai_agl_valid);
141 static inline struct ll_sa_entry *
142 sa_first_received_entry(struct ll_statahead_info *sai)
144 return cfs_list_entry(sai->sai_entries_received.next,
145 struct ll_sa_entry, se_list);
148 static inline struct ll_inode_info *
149 agl_first_entry(struct ll_statahead_info *sai)
151 return cfs_list_entry(sai->sai_entries_agl.next,
152 struct ll_inode_info, lli_agl_list);
155 static inline int sa_sent_full(struct ll_statahead_info *sai)
157 return cfs_atomic_read(&sai->sai_cache_count) >= sai->sai_max;
160 static inline int sa_received_empty(struct ll_statahead_info *sai)
162 return cfs_list_empty(&sai->sai_entries_received);
165 static inline int agl_list_empty(struct ll_statahead_info *sai)
167 return cfs_list_empty(&sai->sai_entries_agl);
171 * (1) hit ratio less than 80%
173 * (2) consecutive miss more than 8
174 * then means low hit.
176 static inline int sa_low_hit(struct ll_statahead_info *sai)
178 return ((sai->sai_hit > 7 && sai->sai_hit < 4 * sai->sai_miss) ||
179 (sai->sai_consecutive_miss > 8));
183 * If the given index is behind of statahead window more than
184 * SA_OMITTED_ENTRY_MAX, then it is old.
186 static inline int is_omitted_entry(struct ll_statahead_info *sai, __u64 index)
188 return ((__u64)sai->sai_max + index + SA_OMITTED_ENTRY_MAX <
193 * Insert it into sai_entries tail when init.
195 static struct ll_sa_entry *
196 ll_sa_entry_alloc(struct ll_statahead_info *sai, __u64 index,
197 const char *name, int len)
199 struct ll_inode_info *lli;
200 struct ll_sa_entry *entry;
205 entry_size = sizeof(struct ll_sa_entry) + (len & ~3) + 4;
206 OBD_ALLOC(entry, entry_size);
207 if (unlikely(entry == NULL))
208 RETURN(ERR_PTR(-ENOMEM));
210 CDEBUG(D_READA, "alloc sa entry %.*s(%p) index "LPU64"\n",
211 len, name, entry, index);
213 entry->se_index = index;
216 * Statahead entry reference rules:
218 * 1) When statahead entry is initialized, its reference is set as 2.
219 * One reference is used by the directory scanner. When the scanner
220 * searches the statahead cache for the given name, it can perform
221 * lockless hash lookup (only the scanner can remove entry from hash
222 * list), and once found, it needn't to call "atomic_inc()" for the
223 * entry reference. So the performance is improved. After using the
224 * statahead entry, the scanner will call "atomic_dec()" to drop the
225 * reference held when initialization. If it is the last reference,
226 * the statahead entry will be freed.
228 * 2) All other threads, including statahead thread and ptlrpcd thread,
229 * when they process the statahead entry, the reference for target
230 * should be held to guarantee the entry will not be released by the
231 * directory scanner. After processing the entry, these threads will
232 * drop the entry reference. If it is the last reference, the entry
235 * The second reference when initializes the statahead entry is used
236 * by the statahead thread, following the rule 2).
238 cfs_atomic_set(&entry->se_refcount, 2);
239 entry->se_stat = SA_ENTRY_INIT;
240 entry->se_size = entry_size;
241 dname = (char *)entry + sizeof(struct ll_sa_entry);
242 memcpy(dname, name, len);
244 entry->se_qstr.hash = full_name_hash(name, len);
245 entry->se_qstr.len = len;
246 entry->se_qstr.name = dname;
248 lli = ll_i2info(sai->sai_inode);
249 spin_lock(&lli->lli_sa_lock);
250 cfs_list_add_tail(&entry->se_link, &sai->sai_entries);
251 CFS_INIT_LIST_HEAD(&entry->se_list);
252 ll_sa_entry_enhash(sai, entry);
253 spin_unlock(&lli->lli_sa_lock);
255 cfs_atomic_inc(&sai->sai_cache_count);
261 * Used by the directory scanner to search entry with name.
263 * Only the caller can remove the entry from hash, so it is unnecessary to hold
264 * hash lock. It is caller's duty to release the init refcount on the entry, so
265 * it is also unnecessary to increase refcount on the entry.
267 static struct ll_sa_entry *
268 ll_sa_entry_get_byname(struct ll_statahead_info *sai, const struct qstr *qstr)
270 struct ll_sa_entry *entry;
271 int i = ll_sa_entry_hash(qstr->hash);
273 cfs_list_for_each_entry(entry, &sai->sai_cache[i], se_hash) {
274 if (entry->se_qstr.hash == qstr->hash &&
275 entry->se_qstr.len == qstr->len &&
276 memcmp(entry->se_qstr.name, qstr->name, qstr->len) == 0)
283 * Used by the async getattr request callback to find entry with index.
285 * Inside lli_sa_lock to prevent others to change the list during the search.
286 * It needs to increase entry refcount before returning to guarantee that the
287 * entry cannot be freed by others.
289 static struct ll_sa_entry *
290 ll_sa_entry_get_byindex(struct ll_statahead_info *sai, __u64 index)
292 struct ll_sa_entry *entry;
294 cfs_list_for_each_entry(entry, &sai->sai_entries, se_link) {
295 if (entry->se_index == index) {
296 LASSERT(atomic_read(&entry->se_refcount) > 0);
297 cfs_atomic_inc(&entry->se_refcount);
300 if (entry->se_index > index)
306 static void ll_sa_entry_cleanup(struct ll_statahead_info *sai,
307 struct ll_sa_entry *entry)
309 struct md_enqueue_info *minfo = entry->se_minfo;
310 struct ptlrpc_request *req = entry->se_req;
313 entry->se_minfo = NULL;
314 ll_intent_release(&minfo->mi_it);
320 entry->se_req = NULL;
321 ptlrpc_req_finished(req);
325 static void ll_sa_entry_put(struct ll_statahead_info *sai,
326 struct ll_sa_entry *entry)
328 if (cfs_atomic_dec_and_test(&entry->se_refcount)) {
329 CDEBUG(D_READA, "free sa entry %.*s(%p) index "LPU64"\n",
330 entry->se_qstr.len, entry->se_qstr.name, entry,
333 LASSERT(cfs_list_empty(&entry->se_link));
334 LASSERT(cfs_list_empty(&entry->se_list));
335 LASSERT(ll_sa_entry_unhashed(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_sa_entry_fini(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
349 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
351 LASSERT(!ll_sa_entry_unhashed(entry));
352 LASSERT(!cfs_list_empty(&entry->se_link));
354 ll_sa_entry_unhash(sai, entry);
356 spin_lock(&lli->lli_sa_lock);
357 entry->se_stat = SA_ENTRY_DEST;
358 cfs_list_del_init(&entry->se_link);
359 if (likely(!cfs_list_empty(&entry->se_list)))
360 cfs_list_del_init(&entry->se_list);
361 spin_unlock(&lli->lli_sa_lock);
363 ll_sa_entry_put(sai, entry);
367 * Delete it from sai_entries_stated list when fini.
370 ll_sa_entry_fini(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
372 struct ll_sa_entry *pos, *next;
375 do_sa_entry_fini(sai, entry);
377 /* drop old entry, only 'scanner' process does this, no need to lock */
378 cfs_list_for_each_entry_safe(pos, next, &sai->sai_entries, se_link) {
379 if (!is_omitted_entry(sai, pos->se_index))
381 do_sa_entry_fini(sai, pos);
386 * Inside lli_sa_lock.
389 do_sa_entry_to_stated(struct ll_statahead_info *sai,
390 struct ll_sa_entry *entry, se_stat_t stat)
392 struct ll_sa_entry *se;
393 cfs_list_t *pos = &sai->sai_entries_stated;
395 if (!cfs_list_empty(&entry->se_list))
396 cfs_list_del_init(&entry->se_list);
398 cfs_list_for_each_entry_reverse(se, &sai->sai_entries_stated, se_list) {
399 if (se->se_index < entry->se_index) {
405 cfs_list_add(&entry->se_list, pos);
406 entry->se_stat = stat;
410 * Move entry to sai_entries_stated and sort with the index.
411 * \retval 1 -- entry to be destroyed.
412 * \retval 0 -- entry is inserted into stated list.
415 ll_sa_entry_to_stated(struct ll_statahead_info *sai,
416 struct ll_sa_entry *entry, se_stat_t stat)
418 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
421 ll_sa_entry_cleanup(sai, entry);
423 spin_lock(&lli->lli_sa_lock);
424 if (likely(entry->se_stat != SA_ENTRY_DEST)) {
425 do_sa_entry_to_stated(sai, entry, stat);
428 spin_unlock(&lli->lli_sa_lock);
434 * Insert inode into the list of sai_entries_agl.
436 static void ll_agl_add(struct ll_statahead_info *sai,
437 struct inode *inode, int index)
439 struct ll_inode_info *child = ll_i2info(inode);
440 struct ll_inode_info *parent = ll_i2info(sai->sai_inode);
443 spin_lock(&child->lli_agl_lock);
444 if (child->lli_agl_index == 0) {
445 child->lli_agl_index = index;
446 spin_unlock(&child->lli_agl_lock);
448 LASSERT(cfs_list_empty(&child->lli_agl_list));
451 spin_lock(&parent->lli_agl_lock);
452 if (agl_list_empty(sai))
454 cfs_list_add_tail(&child->lli_agl_list, &sai->sai_entries_agl);
455 spin_unlock(&parent->lli_agl_lock);
457 spin_unlock(&child->lli_agl_lock);
461 cfs_waitq_signal(&sai->sai_agl_thread.t_ctl_waitq);
464 static struct ll_statahead_info *ll_sai_alloc(void)
466 struct ll_statahead_info *sai;
474 cfs_atomic_set(&sai->sai_refcount, 1);
476 spin_lock(&sai_generation_lock);
477 sai->sai_generation = ++sai_generation;
478 if (unlikely(sai_generation == 0))
479 sai->sai_generation = ++sai_generation;
480 spin_unlock(&sai_generation_lock);
482 sai->sai_max = LL_SA_RPC_MIN;
484 cfs_waitq_init(&sai->sai_waitq);
485 cfs_waitq_init(&sai->sai_thread.t_ctl_waitq);
486 cfs_waitq_init(&sai->sai_agl_thread.t_ctl_waitq);
488 CFS_INIT_LIST_HEAD(&sai->sai_entries);
489 CFS_INIT_LIST_HEAD(&sai->sai_entries_received);
490 CFS_INIT_LIST_HEAD(&sai->sai_entries_stated);
491 CFS_INIT_LIST_HEAD(&sai->sai_entries_agl);
493 for (i = 0; i < LL_SA_CACHE_SIZE; i++) {
494 CFS_INIT_LIST_HEAD(&sai->sai_cache[i]);
495 spin_lock_init(&sai->sai_cache_lock[i]);
497 cfs_atomic_set(&sai->sai_cache_count, 0);
502 static inline struct ll_statahead_info *
503 ll_sai_get(struct ll_statahead_info *sai)
505 cfs_atomic_inc(&sai->sai_refcount);
509 static void ll_sai_put(struct ll_statahead_info *sai)
511 struct inode *inode = sai->sai_inode;
512 struct ll_inode_info *lli = ll_i2info(inode);
515 if (cfs_atomic_dec_and_lock(&sai->sai_refcount, &lli->lli_sa_lock)) {
516 struct ll_sa_entry *entry, *next;
518 if (unlikely(cfs_atomic_read(&sai->sai_refcount) > 0)) {
519 /* It is race case, the interpret callback just hold
520 * a reference count */
521 spin_unlock(&lli->lli_sa_lock);
525 LASSERT(lli->lli_opendir_key == NULL);
526 LASSERT(thread_is_stopped(&sai->sai_thread));
527 LASSERT(thread_is_stopped(&sai->sai_agl_thread));
530 lli->lli_opendir_pid = 0;
531 spin_unlock(&lli->lli_sa_lock);
533 if (sai->sai_sent > sai->sai_replied)
534 CDEBUG(D_READA,"statahead for dir "DFID" does not "
535 "finish: [sent:"LPU64"] [replied:"LPU64"]\n",
537 sai->sai_sent, sai->sai_replied);
539 cfs_list_for_each_entry_safe(entry, next,
540 &sai->sai_entries, se_link)
541 do_sa_entry_fini(sai, entry);
543 LASSERT(list_empty(&sai->sai_entries));
544 LASSERT(sa_received_empty(sai));
545 LASSERT(list_empty(&sai->sai_entries_stated));
547 LASSERT(cfs_atomic_read(&sai->sai_cache_count) == 0);
548 LASSERT(agl_list_empty(sai));
557 /* Do NOT forget to drop inode refcount when into sai_entries_agl. */
558 static void ll_agl_trigger(struct inode *inode, struct ll_statahead_info *sai)
560 struct ll_inode_info *lli = ll_i2info(inode);
561 __u64 index = lli->lli_agl_index;
565 LASSERT(cfs_list_empty(&lli->lli_agl_list));
567 /* AGL maybe fall behind statahead with one entry */
568 if (is_omitted_entry(sai, index + 1)) {
569 lli->lli_agl_index = 0;
574 /* Someone is in glimpse (sync or async), do nothing. */
575 rc = down_write_trylock(&lli->lli_glimpse_sem);
577 lli->lli_agl_index = 0;
583 * Someone triggered glimpse within 1 sec before.
584 * 1) The former glimpse succeeded with glimpse lock granted by OST, and
585 * if the lock is still cached on client, AGL needs to do nothing. If
586 * it is cancelled by other client, AGL maybe cannot obtaion new lock
587 * for no glimpse callback triggered by AGL.
588 * 2) The former glimpse succeeded, but OST did not grant glimpse lock.
589 * Under such case, it is quite possible that the OST will not grant
590 * glimpse lock for AGL also.
591 * 3) The former glimpse failed, compared with other two cases, it is
592 * relative rare. AGL can ignore such case, and it will not muchly
593 * affect the performance.
595 if (lli->lli_glimpse_time != 0 &&
596 cfs_time_before(cfs_time_shift(-1), lli->lli_glimpse_time)) {
597 up_write(&lli->lli_glimpse_sem);
598 lli->lli_agl_index = 0;
603 CDEBUG(D_READA, "Handling (init) async glimpse: inode = "
604 DFID", idx = "LPU64"\n", PFID(&lli->lli_fid), index);
607 lli->lli_agl_index = 0;
608 lli->lli_glimpse_time = cfs_time_current();
609 up_write(&lli->lli_glimpse_sem);
611 CDEBUG(D_READA, "Handled (init) async glimpse: inode= "
612 DFID", idx = "LPU64", rc = %d\n",
613 PFID(&lli->lli_fid), index, rc);
620 static void ll_post_statahead(struct ll_statahead_info *sai)
622 struct inode *dir = sai->sai_inode;
624 struct ll_inode_info *lli = ll_i2info(dir);
625 struct ll_sa_entry *entry;
626 struct md_enqueue_info *minfo;
627 struct lookup_intent *it;
628 struct ptlrpc_request *req;
629 struct mdt_body *body;
633 spin_lock(&lli->lli_sa_lock);
634 if (unlikely(sa_received_empty(sai))) {
635 spin_unlock(&lli->lli_sa_lock);
638 entry = sa_first_received_entry(sai);
639 cfs_atomic_inc(&entry->se_refcount);
640 cfs_list_del_init(&entry->se_list);
641 spin_unlock(&lli->lli_sa_lock);
643 LASSERT(entry->se_handle != 0);
645 minfo = entry->se_minfo;
648 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
650 GOTO(out, rc = -EFAULT);
652 child = entry->se_inode;
657 LASSERT(fid_is_zero(&minfo->mi_data.op_fid2));
659 /* XXX: No fid in reply, this is probaly cross-ref case.
660 * SA can't handle it yet. */
661 if (body->valid & OBD_MD_MDS)
662 GOTO(out, rc = -EAGAIN);
667 /* unlinked and re-created with the same name */
668 if (unlikely(!lu_fid_eq(&minfo->mi_data.op_fid2, &body->fid1))){
669 entry->se_inode = NULL;
675 it->d.lustre.it_lock_handle = entry->se_handle;
676 rc = md_revalidate_lock(ll_i2mdexp(dir), it, ll_inode2fid(dir), NULL);
678 GOTO(out, rc = -EAGAIN);
680 rc = ll_prep_inode(&child, req, dir->i_sb, it);
684 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
685 child, child->i_ino, child->i_generation);
686 ll_set_lock_data(ll_i2sbi(dir)->ll_md_exp, child, it, NULL);
688 entry->se_inode = child;
690 if (agl_should_run(sai, child))
691 ll_agl_add(sai, child, entry->se_index);
696 /* The "ll_sa_entry_to_stated()" will drop related ldlm ibits lock
697 * reference count by calling "ll_intent_drop_lock()" in spite of the
698 * above operations failed or not. Do not worry about calling
699 * "ll_intent_drop_lock()" more than once. */
700 rc = ll_sa_entry_to_stated(sai, entry,
701 rc < 0 ? SA_ENTRY_INVA : SA_ENTRY_SUCC);
702 if (rc == 0 && entry->se_index == sai->sai_index_wait)
703 cfs_waitq_signal(&sai->sai_waitq);
704 ll_sa_entry_put(sai, entry);
707 static int ll_statahead_interpret(struct ptlrpc_request *req,
708 struct md_enqueue_info *minfo, int rc)
710 struct lookup_intent *it = &minfo->mi_it;
711 struct inode *dir = minfo->mi_dir;
712 struct ll_inode_info *lli = ll_i2info(dir);
713 struct ll_statahead_info *sai = NULL;
714 struct ll_sa_entry *entry;
718 if (it_disposition(it, DISP_LOOKUP_NEG))
721 spin_lock(&lli->lli_sa_lock);
723 if (unlikely(lli->lli_sai == NULL ||
724 lli->lli_sai->sai_generation != minfo->mi_generation)) {
725 spin_unlock(&lli->lli_sa_lock);
726 GOTO(out, rc = -ESTALE);
728 sai = ll_sai_get(lli->lli_sai);
729 if (unlikely(!thread_is_running(&sai->sai_thread))) {
731 spin_unlock(&lli->lli_sa_lock);
732 GOTO(out, rc = -EBADFD);
735 entry = ll_sa_entry_get_byindex(sai, minfo->mi_cbdata);
738 spin_unlock(&lli->lli_sa_lock);
739 GOTO(out, rc = -EIDRM);
743 do_sa_entry_to_stated(sai, entry, SA_ENTRY_INVA);
744 wakeup = (entry->se_index == sai->sai_index_wait);
746 entry->se_minfo = minfo;
747 entry->se_req = ptlrpc_request_addref(req);
748 /* Release the async ibits lock ASAP to avoid deadlock
749 * when statahead thread tries to enqueue lock on parent
750 * for readpage and other tries to enqueue lock on child
751 * with parent's lock held, for example: unlink. */
752 entry->se_handle = it->d.lustre.it_lock_handle;
753 ll_intent_drop_lock(it);
754 wakeup = sa_received_empty(sai);
755 cfs_list_add_tail(&entry->se_list,
756 &sai->sai_entries_received);
759 spin_unlock(&lli->lli_sa_lock);
761 ll_sa_entry_put(sai, entry);
763 cfs_waitq_signal(&sai->sai_thread.t_ctl_waitq);
770 ll_intent_release(it);
779 static void sa_args_fini(struct md_enqueue_info *minfo,
780 struct ldlm_enqueue_info *einfo)
782 LASSERT(minfo && einfo);
784 capa_put(minfo->mi_data.op_capa1);
785 capa_put(minfo->mi_data.op_capa2);
791 * There is race condition between "capa_put" and "ll_statahead_interpret" for
792 * accessing "op_data.op_capa[1,2]" as following:
793 * "capa_put" releases "op_data.op_capa[1,2]"'s reference count after calling
794 * "md_intent_getattr_async". But "ll_statahead_interpret" maybe run first, and
795 * fill "op_data.op_capa[1,2]" as POISON, then cause "capa_put" access invalid
796 * "ocapa". So here reserve "op_data.op_capa[1,2]" in "pcapa" before calling
797 * "md_intent_getattr_async".
799 static int sa_args_init(struct inode *dir, struct inode *child,
800 struct ll_sa_entry *entry, struct md_enqueue_info **pmi,
801 struct ldlm_enqueue_info **pei,
802 struct obd_capa **pcapa)
804 struct qstr *qstr = &entry->se_qstr;
805 struct ll_inode_info *lli = ll_i2info(dir);
806 struct md_enqueue_info *minfo;
807 struct ldlm_enqueue_info *einfo;
808 struct md_op_data *op_data;
810 OBD_ALLOC_PTR(einfo);
814 OBD_ALLOC_PTR(minfo);
820 op_data = ll_prep_md_op_data(&minfo->mi_data, dir, child, qstr->name,
821 qstr->len, 0, LUSTRE_OPC_ANY, NULL);
822 if (IS_ERR(op_data)) {
825 return PTR_ERR(op_data);
828 minfo->mi_it.it_op = IT_GETATTR;
829 minfo->mi_dir = igrab(dir);
830 minfo->mi_cb = ll_statahead_interpret;
831 minfo->mi_generation = lli->lli_sai->sai_generation;
832 minfo->mi_cbdata = entry->se_index;
834 einfo->ei_type = LDLM_IBITS;
835 einfo->ei_mode = it_to_lock_mode(&minfo->mi_it);
836 einfo->ei_cb_bl = ll_md_blocking_ast;
837 einfo->ei_cb_cp = ldlm_completion_ast;
838 einfo->ei_cb_gl = NULL;
839 einfo->ei_cbdata = NULL;
843 pcapa[0] = op_data->op_capa1;
844 pcapa[1] = op_data->op_capa2;
849 static int do_sa_lookup(struct inode *dir, struct ll_sa_entry *entry)
851 struct md_enqueue_info *minfo;
852 struct ldlm_enqueue_info *einfo;
853 struct obd_capa *capas[2];
857 rc = sa_args_init(dir, NULL, entry, &minfo, &einfo, capas);
861 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
866 sa_args_fini(minfo, einfo);
873 * similar to ll_revalidate_it().
874 * \retval 1 -- dentry valid
875 * \retval 0 -- will send stat-ahead request
876 * \retval others -- prepare stat-ahead request failed
878 static int do_sa_revalidate(struct inode *dir, struct ll_sa_entry *entry,
879 struct dentry *dentry)
881 struct inode *inode = dentry->d_inode;
882 struct lookup_intent it = { .it_op = IT_GETATTR,
883 .d.lustre.it_lock_handle = 0 };
884 struct md_enqueue_info *minfo;
885 struct ldlm_enqueue_info *einfo;
886 struct obd_capa *capas[2];
890 if (unlikely(inode == NULL))
893 if (d_mountpoint(dentry))
896 if (unlikely(dentry == dentry->d_sb->s_root))
899 entry->se_inode = igrab(inode);
900 rc = md_revalidate_lock(ll_i2mdexp(dir), &it, ll_inode2fid(inode),NULL);
902 entry->se_handle = it.d.lustre.it_lock_handle;
903 ll_intent_release(&it);
907 rc = sa_args_init(dir, inode, entry, &minfo, &einfo, capas);
909 entry->se_inode = NULL;
914 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
919 entry->se_inode = NULL;
921 sa_args_fini(minfo, einfo);
927 static void ll_statahead_one(struct dentry *parent, const char* entry_name,
930 struct inode *dir = parent->d_inode;
931 struct ll_inode_info *lli = ll_i2info(dir);
932 struct ll_statahead_info *sai = lli->lli_sai;
933 struct dentry *dentry = NULL;
934 struct ll_sa_entry *entry;
939 entry = ll_sa_entry_alloc(sai, sai->sai_index, entry_name,
944 dentry = d_lookup(parent, &entry->se_qstr);
946 rc = do_sa_lookup(dir, entry);
948 rc = do_sa_revalidate(dir, entry, dentry);
949 if (rc == 1 && agl_should_run(sai, dentry->d_inode))
950 ll_agl_add(sai, dentry->d_inode, entry->se_index);
957 rc1 = ll_sa_entry_to_stated(sai, entry,
958 rc < 0 ? SA_ENTRY_INVA : SA_ENTRY_SUCC);
959 if (rc1 == 0 && entry->se_index == sai->sai_index_wait)
960 cfs_waitq_signal(&sai->sai_waitq);
966 /* drop one refcount on entry by ll_sa_entry_alloc */
967 ll_sa_entry_put(sai, entry);
972 static int ll_agl_thread(void *arg)
974 struct dentry *parent = (struct dentry *)arg;
975 struct inode *dir = parent->d_inode;
976 struct ll_inode_info *plli = ll_i2info(dir);
977 struct ll_inode_info *clli;
978 struct ll_sb_info *sbi = ll_i2sbi(dir);
979 struct ll_statahead_info *sai = ll_sai_get(plli->lli_sai);
980 struct ptlrpc_thread *thread = &sai->sai_agl_thread;
981 struct l_wait_info lwi = { 0 };
984 CDEBUG(D_READA, "agl thread started: [pid %d] [parent %.*s]\n",
985 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
987 atomic_inc(&sbi->ll_agl_total);
988 spin_lock(&plli->lli_agl_lock);
989 sai->sai_agl_valid = 1;
990 thread_set_flags(thread, SVC_RUNNING);
991 spin_unlock(&plli->lli_agl_lock);
992 cfs_waitq_signal(&thread->t_ctl_waitq);
995 l_wait_event(thread->t_ctl_waitq,
996 !agl_list_empty(sai) ||
997 !thread_is_running(thread),
1000 if (!thread_is_running(thread))
1003 spin_lock(&plli->lli_agl_lock);
1004 /* The statahead thread maybe help to process AGL entries,
1005 * so check whether list empty again. */
1006 if (!agl_list_empty(sai)) {
1007 clli = agl_first_entry(sai);
1008 cfs_list_del_init(&clli->lli_agl_list);
1009 spin_unlock(&plli->lli_agl_lock);
1010 ll_agl_trigger(&clli->lli_vfs_inode, sai);
1012 spin_unlock(&plli->lli_agl_lock);
1016 spin_lock(&plli->lli_agl_lock);
1017 sai->sai_agl_valid = 0;
1018 while (!agl_list_empty(sai)) {
1019 clli = agl_first_entry(sai);
1020 cfs_list_del_init(&clli->lli_agl_list);
1021 spin_unlock(&plli->lli_agl_lock);
1022 clli->lli_agl_index = 0;
1023 iput(&clli->lli_vfs_inode);
1024 spin_lock(&plli->lli_agl_lock);
1026 thread_set_flags(thread, SVC_STOPPED);
1027 spin_unlock(&plli->lli_agl_lock);
1028 cfs_waitq_signal(&thread->t_ctl_waitq);
1030 CDEBUG(D_READA, "agl thread stopped: [pid %d] [parent %.*s]\n",
1031 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1035 static void ll_start_agl(struct dentry *parent, struct ll_statahead_info *sai)
1037 struct ptlrpc_thread *thread = &sai->sai_agl_thread;
1038 struct l_wait_info lwi = { 0 };
1039 struct ll_inode_info *plli;
1043 CDEBUG(D_READA, "start agl thread: [pid %d] [parent %.*s]\n",
1044 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1046 plli = ll_i2info(parent->d_inode);
1047 task = kthread_run(ll_agl_thread, parent,
1048 "ll_agl_%u", plli->lli_opendir_pid);
1050 CERROR("can't start ll_agl thread, rc: %ld\n", PTR_ERR(task));
1051 thread_set_flags(thread, SVC_STOPPED);
1055 l_wait_event(thread->t_ctl_waitq,
1056 thread_is_running(thread) || thread_is_stopped(thread),
1061 static int ll_statahead_thread(void *arg)
1063 struct dentry *parent = (struct dentry *)arg;
1064 struct inode *dir = parent->d_inode;
1065 struct ll_inode_info *plli = ll_i2info(dir);
1066 struct ll_inode_info *clli;
1067 struct ll_sb_info *sbi = ll_i2sbi(dir);
1068 struct ll_statahead_info *sai = ll_sai_get(plli->lli_sai);
1069 struct ptlrpc_thread *thread = &sai->sai_thread;
1070 struct ptlrpc_thread *agl_thread = &sai->sai_agl_thread;
1075 struct ll_dir_chain chain;
1076 struct l_wait_info lwi = { 0 };
1079 CDEBUG(D_READA, "statahead thread started: [pid %d] [parent %.*s]\n",
1080 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1082 if (sbi->ll_flags & LL_SBI_AGL_ENABLED)
1083 ll_start_agl(parent, sai);
1085 atomic_inc(&sbi->ll_sa_total);
1086 spin_lock(&plli->lli_sa_lock);
1087 thread_set_flags(thread, SVC_RUNNING);
1088 spin_unlock(&plli->lli_sa_lock);
1089 cfs_waitq_signal(&thread->t_ctl_waitq);
1091 ll_dir_chain_init(&chain);
1092 page = ll_get_dir_page(dir, pos, &chain);
1095 struct lu_dirpage *dp;
1096 struct lu_dirent *ent;
1100 CDEBUG(D_READA, "error reading dir "DFID" at "LPU64
1101 "/"LPU64": [rc %d] [parent %u]\n",
1102 PFID(ll_inode2fid(dir)), pos, sai->sai_index,
1103 rc, plli->lli_opendir_pid);
1107 dp = page_address(page);
1108 for (ent = lu_dirent_start(dp); ent != NULL;
1109 ent = lu_dirent_next(ent)) {
1114 hash = le64_to_cpu(ent->lde_hash);
1115 if (unlikely(hash < pos))
1117 * Skip until we find target hash value.
1121 namelen = le16_to_cpu(ent->lde_namelen);
1122 if (unlikely(namelen == 0))
1124 * Skip dummy record.
1128 name = ent->lde_name;
1129 if (name[0] == '.') {
1135 } else if (name[1] == '.' && namelen == 2) {
1140 } else if (!sai->sai_ls_all) {
1142 * skip hidden files.
1144 sai->sai_skip_hidden++;
1150 * don't stat-ahead first entry.
1152 if (unlikely(++first == 1))
1156 l_wait_event(thread->t_ctl_waitq,
1157 !sa_sent_full(sai) ||
1158 !sa_received_empty(sai) ||
1159 !agl_list_empty(sai) ||
1160 !thread_is_running(thread),
1164 while (!sa_received_empty(sai))
1165 ll_post_statahead(sai);
1167 if (unlikely(!thread_is_running(thread))) {
1168 ll_release_page(page, 0);
1172 /* If no window for metadata statahead, but there are
1173 * some AGL entries to be triggered, then try to help
1174 * to process the AGL entries. */
1175 if (sa_sent_full(sai)) {
1176 spin_lock(&plli->lli_agl_lock);
1177 while (!agl_list_empty(sai)) {
1178 clli = agl_first_entry(sai);
1179 cfs_list_del_init(&clli->lli_agl_list);
1180 spin_unlock(&plli->lli_agl_lock);
1181 ll_agl_trigger(&clli->lli_vfs_inode,
1184 if (!sa_received_empty(sai))
1188 !thread_is_running(thread))) {
1189 ll_release_page(page, 0);
1193 if (!sa_sent_full(sai))
1196 spin_lock(&plli->lli_agl_lock);
1198 spin_unlock(&plli->lli_agl_lock);
1204 ll_statahead_one(parent, name, namelen);
1206 pos = le64_to_cpu(dp->ldp_hash_end);
1207 if (pos == MDS_DIR_END_OFF) {
1209 * End of directory reached.
1211 ll_release_page(page, 0);
1213 l_wait_event(thread->t_ctl_waitq,
1214 !sa_received_empty(sai) ||
1215 sai->sai_sent == sai->sai_replied||
1216 !thread_is_running(thread),
1219 while (!sa_received_empty(sai))
1220 ll_post_statahead(sai);
1222 if (unlikely(!thread_is_running(thread)))
1225 if (sai->sai_sent == sai->sai_replied &&
1226 sa_received_empty(sai))
1230 spin_lock(&plli->lli_agl_lock);
1231 while (!agl_list_empty(sai) &&
1232 thread_is_running(thread)) {
1233 clli = agl_first_entry(sai);
1234 cfs_list_del_init(&clli->lli_agl_list);
1235 spin_unlock(&plli->lli_agl_lock);
1236 ll_agl_trigger(&clli->lli_vfs_inode, sai);
1237 spin_lock(&plli->lli_agl_lock);
1239 spin_unlock(&plli->lli_agl_lock);
1244 * chain is exhausted.
1245 * Normal case: continue to the next page.
1247 ll_release_page(page, le32_to_cpu(dp->ldp_flags) &
1249 sai->sai_in_readpage = 1;
1250 page = ll_get_dir_page(dir, pos, &chain);
1251 sai->sai_in_readpage = 0;
1253 LASSERT(le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1254 ll_release_page(page, 1);
1256 * go into overflow page.
1263 if (sai->sai_agl_valid) {
1264 spin_lock(&plli->lli_agl_lock);
1265 thread_set_flags(agl_thread, SVC_STOPPING);
1266 spin_unlock(&plli->lli_agl_lock);
1267 cfs_waitq_signal(&agl_thread->t_ctl_waitq);
1269 CDEBUG(D_READA, "stop agl thread: [pid %d]\n",
1271 l_wait_event(agl_thread->t_ctl_waitq,
1272 thread_is_stopped(agl_thread),
1275 /* Set agl_thread flags anyway. */
1276 thread_set_flags(&sai->sai_agl_thread, SVC_STOPPED);
1278 ll_dir_chain_fini(&chain);
1279 spin_lock(&plli->lli_sa_lock);
1280 if (!sa_received_empty(sai)) {
1281 thread_set_flags(thread, SVC_STOPPING);
1282 spin_unlock(&plli->lli_sa_lock);
1284 /* To release the resources held by received entries. */
1285 while (!sa_received_empty(sai))
1286 ll_post_statahead(sai);
1288 spin_lock(&plli->lli_sa_lock);
1290 thread_set_flags(thread, SVC_STOPPED);
1291 spin_unlock(&plli->lli_sa_lock);
1292 cfs_waitq_signal(&sai->sai_waitq);
1293 cfs_waitq_signal(&thread->t_ctl_waitq);
1296 CDEBUG(D_READA, "statahead thread stopped: [pid %d] [parent %.*s]\n",
1297 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1302 * called in ll_file_release().
1304 void ll_stop_statahead(struct inode *dir, void *key)
1306 struct ll_inode_info *lli = ll_i2info(dir);
1308 if (unlikely(key == NULL))
1311 spin_lock(&lli->lli_sa_lock);
1312 if (lli->lli_opendir_key != key || lli->lli_opendir_pid == 0) {
1313 spin_unlock(&lli->lli_sa_lock);
1317 lli->lli_opendir_key = NULL;
1320 struct l_wait_info lwi = { 0 };
1321 struct ptlrpc_thread *thread = &lli->lli_sai->sai_thread;
1323 if (!thread_is_stopped(thread)) {
1324 thread_set_flags(thread, SVC_STOPPING);
1325 spin_unlock(&lli->lli_sa_lock);
1326 cfs_waitq_signal(&thread->t_ctl_waitq);
1328 CDEBUG(D_READA, "stop statahead thread: [pid %d]\n",
1330 l_wait_event(thread->t_ctl_waitq,
1331 thread_is_stopped(thread),
1334 spin_unlock(&lli->lli_sa_lock);
1338 * Put the ref which was held when first statahead_enter.
1339 * It maybe not the last ref for some statahead requests
1342 ll_sai_put(lli->lli_sai);
1344 lli->lli_opendir_pid = 0;
1345 spin_unlock(&lli->lli_sa_lock);
1351 * not first dirent, or is "."
1353 LS_NONE_FIRST_DE = 0,
1355 * the first non-hidden dirent
1359 * the first hidden dirent, that is "."
1364 static int is_first_dirent(struct inode *dir, struct dentry *dentry)
1366 struct ll_dir_chain chain;
1367 struct qstr *target = &dentry->d_name;
1371 int rc = LS_NONE_FIRST_DE;
1374 ll_dir_chain_init(&chain);
1375 page = ll_get_dir_page(dir, pos, &chain);
1378 struct lu_dirpage *dp;
1379 struct lu_dirent *ent;
1382 struct ll_inode_info *lli = ll_i2info(dir);
1385 CERROR("error reading dir "DFID" at "LPU64": "
1386 "[rc %d] [parent %u]\n",
1387 PFID(ll_inode2fid(dir)), pos,
1388 rc, lli->lli_opendir_pid);
1392 dp = page_address(page);
1393 for (ent = lu_dirent_start(dp); ent != NULL;
1394 ent = lu_dirent_next(ent)) {
1399 hash = le64_to_cpu(ent->lde_hash);
1400 /* The ll_get_dir_page() can return any page containing
1401 * the given hash which may be not the start hash. */
1402 if (unlikely(hash < pos))
1405 namelen = le16_to_cpu(ent->lde_namelen);
1406 if (unlikely(namelen == 0))
1408 * skip dummy record.
1412 name = ent->lde_name;
1413 if (name[0] == '.') {
1419 else if (name[1] == '.' && namelen == 2)
1430 if (dot_de && target->name[0] != '.') {
1431 CDEBUG(D_READA, "%.*s skip hidden file %.*s\n",
1432 target->len, target->name,
1437 if (target->len != namelen ||
1438 memcmp(target->name, name, namelen) != 0)
1439 rc = LS_NONE_FIRST_DE;
1443 rc = LS_FIRST_DOT_DE;
1445 ll_release_page(page, 0);
1448 pos = le64_to_cpu(dp->ldp_hash_end);
1449 if (pos == MDS_DIR_END_OFF) {
1451 * End of directory reached.
1453 ll_release_page(page, 0);
1457 * chain is exhausted
1458 * Normal case: continue to the next page.
1460 ll_release_page(page, le32_to_cpu(dp->ldp_flags) &
1462 page = ll_get_dir_page(dir, pos, &chain);
1465 * go into overflow page.
1467 LASSERT(le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1468 ll_release_page(page, 1);
1474 ll_dir_chain_fini(&chain);
1479 ll_sai_unplug(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
1481 struct ptlrpc_thread *thread = &sai->sai_thread;
1482 struct ll_sb_info *sbi = ll_i2sbi(sai->sai_inode);
1486 if (entry != NULL && entry->se_stat == SA_ENTRY_SUCC)
1491 ll_sa_entry_fini(sai, entry);
1494 sai->sai_consecutive_miss = 0;
1495 sai->sai_max = min(2 * sai->sai_max, sbi->ll_sa_max);
1497 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
1500 sai->sai_consecutive_miss++;
1501 if (sa_low_hit(sai) && thread_is_running(thread)) {
1502 atomic_inc(&sbi->ll_sa_wrong);
1503 CDEBUG(D_READA, "Statahead for dir "DFID" hit "
1504 "ratio too low: hit/miss "LPU64"/"LPU64
1505 ", sent/replied "LPU64"/"LPU64", stopping "
1506 "statahead thread: pid %d\n",
1507 PFID(&lli->lli_fid), sai->sai_hit,
1508 sai->sai_miss, sai->sai_sent,
1509 sai->sai_replied, cfs_curproc_pid());
1510 spin_lock(&lli->lli_sa_lock);
1511 if (!thread_is_stopped(thread))
1512 thread_set_flags(thread, SVC_STOPPING);
1513 spin_unlock(&lli->lli_sa_lock);
1517 if (!thread_is_stopped(thread))
1518 cfs_waitq_signal(&thread->t_ctl_waitq);
1524 * Start statahead thread if this is the first dir entry.
1525 * Otherwise if a thread is started already, wait it until it is ahead of me.
1526 * \retval 1 -- find entry with lock in cache, the caller needs to do
1528 * \retval 0 -- find entry in cache, but without lock, the caller needs
1530 * \retval others -- the caller need to process as non-statahead.
1532 int do_statahead_enter(struct inode *dir, struct dentry **dentryp,
1535 struct ll_inode_info *lli = ll_i2info(dir);
1536 struct ll_statahead_info *sai = lli->lli_sai;
1537 struct dentry *parent;
1538 struct ll_sa_entry *entry;
1539 struct ptlrpc_thread *thread;
1540 struct l_wait_info lwi = { 0 };
1542 struct ll_inode_info *plli;
1545 LASSERT(lli->lli_opendir_pid == cfs_curproc_pid());
1548 thread = &sai->sai_thread;
1549 if (unlikely(thread_is_stopped(thread) &&
1550 cfs_list_empty(&sai->sai_entries_stated))) {
1551 /* to release resource */
1552 ll_stop_statahead(dir, lli->lli_opendir_key);
1556 if ((*dentryp)->d_name.name[0] == '.') {
1557 if (sai->sai_ls_all ||
1558 sai->sai_miss_hidden >= sai->sai_skip_hidden) {
1560 * Hidden dentry is the first one, or statahead
1561 * thread does not skip so many hidden dentries
1562 * before "sai_ls_all" enabled as below.
1565 if (!sai->sai_ls_all)
1567 * It maybe because hidden dentry is not
1568 * the first one, "sai_ls_all" was not
1569 * set, then "ls -al" missed. Enable
1570 * "sai_ls_all" for such case.
1572 sai->sai_ls_all = 1;
1575 * Such "getattr" has been skipped before
1576 * "sai_ls_all" enabled as above.
1578 sai->sai_miss_hidden++;
1583 entry = ll_sa_entry_get_byname(sai, &(*dentryp)->d_name);
1584 if (entry == NULL || only_unplug) {
1585 ll_sai_unplug(sai, entry);
1586 RETURN(entry ? 1 : -EAGAIN);
1589 /* if statahead is busy in readdir, help it do post-work */
1590 while (!ll_sa_entry_stated(entry) &&
1591 sai->sai_in_readpage &&
1592 !sa_received_empty(sai))
1593 ll_post_statahead(sai);
1595 if (!ll_sa_entry_stated(entry)) {
1596 sai->sai_index_wait = entry->se_index;
1597 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(30), NULL,
1598 LWI_ON_SIGNAL_NOOP, NULL);
1599 rc = l_wait_event(sai->sai_waitq,
1600 ll_sa_entry_stated(entry) ||
1601 thread_is_stopped(thread),
1604 ll_sai_unplug(sai, entry);
1609 if (entry->se_stat == SA_ENTRY_SUCC &&
1610 entry->se_inode != NULL) {
1611 struct inode *inode = entry->se_inode;
1612 struct lookup_intent it = { .it_op = IT_GETATTR,
1613 .d.lustre.it_lock_handle =
1617 rc = md_revalidate_lock(ll_i2mdexp(dir), &it,
1618 ll_inode2fid(inode), &bits);
1620 if ((*dentryp)->d_inode == NULL) {
1621 *dentryp = ll_splice_alias(inode,
1623 } else if ((*dentryp)->d_inode != inode) {
1624 /* revalidate, but inode is recreated */
1626 "stale dentry %.*s inode %lu/%u, "
1627 "statahead inode %lu/%u\n",
1628 (*dentryp)->d_name.len,
1629 (*dentryp)->d_name.name,
1630 (*dentryp)->d_inode->i_ino,
1631 (*dentryp)->d_inode->i_generation,
1633 inode->i_generation);
1634 ll_sai_unplug(sai, entry);
1639 entry->se_inode = NULL;
1641 if ((bits & MDS_INODELOCK_LOOKUP) &&
1642 d_lustre_invalid(*dentryp))
1643 d_lustre_revalidate(*dentryp);
1644 ll_intent_release(&it);
1648 ll_sai_unplug(sai, entry);
1652 /* I am the "lli_opendir_pid" owner, only me can set "lli_sai". */
1653 rc = is_first_dirent(dir, *dentryp);
1654 if (rc == LS_NONE_FIRST_DE)
1655 /* It is not "ls -{a}l" operation, no need statahead for it. */
1656 GOTO(out, rc = -EAGAIN);
1658 sai = ll_sai_alloc();
1660 GOTO(out, rc = -ENOMEM);
1662 sai->sai_ls_all = (rc == LS_FIRST_DOT_DE);
1663 sai->sai_inode = igrab(dir);
1664 if (unlikely(sai->sai_inode == NULL)) {
1665 CWARN("Do not start stat ahead on dying inode "DFID"\n",
1666 PFID(&lli->lli_fid));
1667 GOTO(out, rc = -ESTALE);
1670 /* get parent reference count here, and put it in ll_statahead_thread */
1671 parent = dget((*dentryp)->d_parent);
1672 if (unlikely(sai->sai_inode != parent->d_inode)) {
1673 struct ll_inode_info *nlli = ll_i2info(parent->d_inode);
1675 CWARN("Race condition, someone changed %.*s just now: "
1676 "old parent "DFID", new parent "DFID"\n",
1677 (*dentryp)->d_name.len, (*dentryp)->d_name.name,
1678 PFID(&lli->lli_fid), PFID(&nlli->lli_fid));
1680 iput(sai->sai_inode);
1681 GOTO(out, rc = -EAGAIN);
1684 CDEBUG(D_READA, "start statahead thread: [pid %d] [parent %.*s]\n",
1685 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1689 plli = ll_i2info(parent->d_inode);
1690 rc = PTR_ERR(kthread_run(ll_statahead_thread, parent,
1691 "ll_sa_%u", plli->lli_opendir_pid));
1692 thread = &sai->sai_thread;
1693 if (IS_ERR_VALUE(rc)) {
1694 CERROR("can't start ll_sa thread, rc: %d\n", rc);
1696 lli->lli_opendir_key = NULL;
1697 thread_set_flags(thread, SVC_STOPPED);
1698 thread_set_flags(&sai->sai_agl_thread, SVC_STOPPED);
1700 LASSERT(lli->lli_sai == NULL);
1704 l_wait_event(thread->t_ctl_waitq,
1705 thread_is_running(thread) || thread_is_stopped(thread),
1709 * We don't stat-ahead for the first dirent since we are already in
1717 spin_lock(&lli->lli_sa_lock);
1718 lli->lli_opendir_key = NULL;
1719 lli->lli_opendir_pid = 0;
1720 spin_unlock(&lli->lli_sa_lock);