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 cfs_spin_lock(&sai->sai_cache_lock[i]);
121 cfs_list_add_tail(&entry->se_hash, &sai->sai_cache[i]);
122 cfs_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 cfs_spin_lock(&sai->sai_cache_lock[i]);
134 cfs_list_del_init(&entry->se_hash);
135 cfs_spin_unlock(&sai->sai_cache_lock[i]);
138 static inline int agl_should_run(struct ll_statahead_info *sai,
141 if (inode != NULL && S_ISREG(inode->i_mode) &&
142 ll_i2info(inode)->lli_has_smd && sai->sai_agl_valid)
147 static inline struct ll_sa_entry *
148 sa_first_received_entry(struct ll_statahead_info *sai)
150 return cfs_list_entry(sai->sai_entries_received.next,
151 struct ll_sa_entry, se_list);
154 static inline struct ll_inode_info *
155 agl_first_entry(struct ll_statahead_info *sai)
157 return cfs_list_entry(sai->sai_entries_agl.next,
158 struct ll_inode_info, lli_agl_list);
161 static inline int sa_sent_full(struct ll_statahead_info *sai)
163 return cfs_atomic_read(&sai->sai_cache_count) >= sai->sai_max;
166 static inline int sa_received_empty(struct ll_statahead_info *sai)
168 return cfs_list_empty(&sai->sai_entries_received);
171 static inline int agl_list_empty(struct ll_statahead_info *sai)
173 return cfs_list_empty(&sai->sai_entries_agl);
177 * (1) hit ratio less than 80%
179 * (2) consecutive miss more than 8
180 * then means low hit.
182 static inline int sa_low_hit(struct ll_statahead_info *sai)
184 return ((sai->sai_hit > 7 && sai->sai_hit < 4 * sai->sai_miss) ||
185 (sai->sai_consecutive_miss > 8));
189 * If the given index is behind of statahead window more than
190 * SA_OMITTED_ENTRY_MAX, then it is old.
192 static inline int is_omitted_entry(struct ll_statahead_info *sai, __u64 index)
194 return ((__u64)sai->sai_max + index + SA_OMITTED_ENTRY_MAX <
199 * Insert it into sai_entries_sent tail when init.
201 static struct ll_sa_entry *
202 ll_sa_entry_alloc(struct ll_statahead_info *sai, __u64 index,
203 const char *name, int len)
205 struct ll_inode_info *lli;
206 struct ll_sa_entry *entry;
211 entry_size = sizeof(struct ll_sa_entry) + (len & ~3) + 4;
212 OBD_ALLOC(entry, entry_size);
213 if (unlikely(entry == NULL))
214 RETURN(ERR_PTR(-ENOMEM));
216 CDEBUG(D_READA, "alloc sai entry %.*s(%p) index "LPU64"\n",
217 len, name, entry, index);
219 entry->se_index = index;
222 * Statahead entry reference rules:
224 * 1) When statahead entry is initialized, its reference is set as 2.
225 * One reference is used by the directory scanner. When the scanner
226 * searches the statahead cache for the given name, it can perform
227 * lockless hash lookup (only the scanner can remove entry from hash
228 * list), and once found, it needn't to call "atomic_inc()" for the
229 * entry reference. So the performance is improved. After using the
230 * statahead entry, the scanner will call "atomic_dec()" to drop the
231 * reference held when initialization. If it is the last reference,
232 * the statahead entry will be freed.
234 * 2) All other threads, including statahead thread and ptlrpcd thread,
235 * when they process the statahead entry, the reference for target
236 * should be held to guarantee the entry will not be released by the
237 * directory scanner. After processing the entry, these threads will
238 * drop the entry reference. If it is the last reference, the entry
241 * The second reference when initializes the statahead entry is used
242 * by the statahead thread, following the rule 2).
244 cfs_atomic_set(&entry->se_refcount, 2);
245 entry->se_stat = SA_ENTRY_INIT;
246 entry->se_size = entry_size;
247 dname = (char *)entry + sizeof(struct ll_sa_entry);
248 memcpy(dname, name, len);
250 entry->se_qstr.hash = full_name_hash(name, len);
251 entry->se_qstr.len = len;
252 entry->se_qstr.name = dname;
254 lli = ll_i2info(sai->sai_inode);
255 cfs_spin_lock(&lli->lli_sa_lock);
256 cfs_list_add_tail(&entry->se_list, &sai->sai_entries_sent);
257 cfs_spin_unlock(&lli->lli_sa_lock);
259 cfs_atomic_inc(&sai->sai_cache_count);
260 ll_sa_entry_enhash(sai, entry);
266 * Used by the directory scanner to search entry with name.
268 * Only the caller can remove the entry from hash, so it is unnecessary to hold
269 * hash lock. It is caller's duty to release the init refcount on the entry, so
270 * it is also unnecessary to increase refcount on the entry.
272 static struct ll_sa_entry *
273 ll_sa_entry_get_byname(struct ll_statahead_info *sai, const struct qstr *qstr)
275 struct ll_sa_entry *entry;
276 int i = ll_sa_entry_hash(qstr->hash);
278 cfs_list_for_each_entry(entry, &sai->sai_cache[i], se_hash) {
279 if (entry->se_qstr.hash == qstr->hash &&
280 entry->se_qstr.len == qstr->len &&
281 memcmp(entry->se_qstr.name, qstr->name, qstr->len) == 0)
288 * Used by the async getattr request callback to find entry with index.
290 * Inside lli_sa_lock to prevent others to change the list during the search.
291 * It needs to increase entry refcount before returning to guarantee that the
292 * entry cannot be freed by others.
294 static struct ll_sa_entry *
295 ll_sa_entry_get_byindex(struct ll_statahead_info *sai, __u64 index)
297 struct ll_sa_entry *entry;
299 cfs_list_for_each_entry(entry, &sai->sai_entries_sent, se_list) {
300 if (entry->se_index == index) {
301 cfs_atomic_inc(&entry->se_refcount);
304 if (entry->se_index > index)
310 static void ll_sa_entry_cleanup(struct ll_statahead_info *sai,
311 struct ll_sa_entry *entry)
313 struct md_enqueue_info *minfo = entry->se_minfo;
314 struct ptlrpc_request *req = entry->se_req;
317 entry->se_minfo = NULL;
318 ll_intent_release(&minfo->mi_it);
324 entry->se_req = NULL;
325 ptlrpc_req_finished(req);
329 static void ll_sa_entry_put(struct ll_statahead_info *sai,
330 struct ll_sa_entry *entry)
332 if (cfs_atomic_dec_and_test(&entry->se_refcount)) {
333 CDEBUG(D_READA, "free sai entry %.*s(%p) index "LPU64"\n",
334 entry->se_qstr.len, entry->se_qstr.name, entry,
337 LASSERT(ll_sa_entry_unhashed(entry));
338 LASSERT(ll_sa_entry_unlinked(entry));
340 ll_sa_entry_cleanup(sai, entry);
342 iput(entry->se_inode);
344 OBD_FREE(entry, entry->se_size);
345 cfs_atomic_dec(&sai->sai_cache_count);
350 do_sai_entry_fini(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
352 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
354 ll_sa_entry_unhash(sai, entry);
356 cfs_spin_lock(&lli->lli_sa_lock);
357 entry->se_stat = SA_ENTRY_DEST;
358 if (likely(!ll_sa_entry_unlinked(entry)))
359 cfs_list_del_init(&entry->se_list);
360 cfs_spin_unlock(&lli->lli_sa_lock);
362 ll_sa_entry_put(sai, entry);
366 * Delete it from sai_entries_stated list when fini.
369 ll_sa_entry_fini(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
371 struct ll_sa_entry *pos, *next;
374 do_sai_entry_fini(sai, entry);
376 /* drop old entry from sent list */
377 cfs_list_for_each_entry_safe(pos, next, &sai->sai_entries_sent,
379 if (is_omitted_entry(sai, pos->se_index))
380 do_sai_entry_fini(sai, pos);
385 /* drop old entry from stated list */
386 cfs_list_for_each_entry_safe(pos, next, &sai->sai_entries_stated,
388 if (is_omitted_entry(sai, pos->se_index))
389 do_sai_entry_fini(sai, pos);
396 * Inside lli_sa_lock.
399 do_sai_entry_to_stated(struct ll_statahead_info *sai,
400 struct ll_sa_entry *entry, int rc)
402 struct ll_sa_entry *se;
403 cfs_list_t *pos = &sai->sai_entries_stated;
405 if (!ll_sa_entry_unlinked(entry))
406 cfs_list_del_init(&entry->se_list);
408 cfs_list_for_each_entry_reverse(se, &sai->sai_entries_stated, se_list) {
409 if (se->se_index < entry->se_index) {
415 cfs_list_add(&entry->se_list, pos);
420 * Move entry to sai_entries_stated and sort with the index.
421 * \retval 1 -- entry to be destroyed.
422 * \retval 0 -- entry is inserted into stated list.
425 ll_sa_entry_to_stated(struct ll_statahead_info *sai,
426 struct ll_sa_entry *entry, int rc)
428 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
431 ll_sa_entry_cleanup(sai, entry);
433 cfs_spin_lock(&lli->lli_sa_lock);
434 if (likely(entry->se_stat != SA_ENTRY_DEST)) {
435 do_sai_entry_to_stated(sai, entry, rc);
438 cfs_spin_unlock(&lli->lli_sa_lock);
444 * Insert inode into the list of sai_entries_agl.
446 static void ll_agl_add(struct ll_statahead_info *sai,
447 struct inode *inode, int index)
449 struct ll_inode_info *child = ll_i2info(inode);
450 struct ll_inode_info *parent = ll_i2info(sai->sai_inode);
453 cfs_spin_lock(&child->lli_agl_lock);
454 if (child->lli_agl_index == 0) {
455 child->lli_agl_index = index;
456 cfs_spin_unlock(&child->lli_agl_lock);
458 LASSERT(cfs_list_empty(&child->lli_agl_list));
461 cfs_spin_lock(&parent->lli_agl_lock);
462 if (agl_list_empty(sai))
464 cfs_list_add_tail(&child->lli_agl_list, &sai->sai_entries_agl);
465 cfs_spin_unlock(&parent->lli_agl_lock);
467 cfs_spin_unlock(&child->lli_agl_lock);
471 cfs_waitq_signal(&sai->sai_agl_thread.t_ctl_waitq);
474 static struct ll_statahead_info *ll_sai_alloc(void)
476 struct ll_statahead_info *sai;
484 cfs_atomic_set(&sai->sai_refcount, 1);
486 cfs_spin_lock(&sai_generation_lock);
487 sai->sai_generation = ++sai_generation;
488 if (unlikely(sai_generation == 0))
489 sai->sai_generation = ++sai_generation;
490 cfs_spin_unlock(&sai_generation_lock);
492 sai->sai_max = LL_SA_RPC_MIN;
494 cfs_waitq_init(&sai->sai_waitq);
495 cfs_waitq_init(&sai->sai_thread.t_ctl_waitq);
496 cfs_waitq_init(&sai->sai_agl_thread.t_ctl_waitq);
498 CFS_INIT_LIST_HEAD(&sai->sai_entries_sent);
499 CFS_INIT_LIST_HEAD(&sai->sai_entries_received);
500 CFS_INIT_LIST_HEAD(&sai->sai_entries_stated);
501 CFS_INIT_LIST_HEAD(&sai->sai_entries_agl);
503 for (i = 0; i < LL_SA_CACHE_SIZE; i++) {
504 CFS_INIT_LIST_HEAD(&sai->sai_cache[i]);
505 cfs_spin_lock_init(&sai->sai_cache_lock[i]);
507 cfs_atomic_set(&sai->sai_cache_count, 0);
512 static inline struct ll_statahead_info *
513 ll_sai_get(struct ll_statahead_info *sai)
515 cfs_atomic_inc(&sai->sai_refcount);
519 static void ll_sai_put(struct ll_statahead_info *sai)
521 struct inode *inode = sai->sai_inode;
522 struct ll_inode_info *lli = ll_i2info(inode);
525 if (cfs_atomic_dec_and_lock(&sai->sai_refcount, &lli->lli_sa_lock)) {
526 struct ll_sa_entry *entry, *next;
528 if (unlikely(cfs_atomic_read(&sai->sai_refcount) > 0)) {
529 /* It is race case, the interpret callback just hold
530 * a reference count */
531 cfs_spin_unlock(&lli->lli_sa_lock);
535 LASSERT(lli->lli_opendir_key == NULL);
536 LASSERT(thread_is_stopped(&sai->sai_thread));
537 LASSERT(thread_is_stopped(&sai->sai_agl_thread));
540 lli->lli_opendir_pid = 0;
541 cfs_spin_unlock(&lli->lli_sa_lock);
543 if (sai->sai_sent > sai->sai_replied)
544 CDEBUG(D_READA,"statahead for dir "DFID" does not "
545 "finish: [sent:"LPU64"] [replied:"LPU64"]\n",
547 sai->sai_sent, sai->sai_replied);
549 cfs_list_for_each_entry_safe(entry, next,
550 &sai->sai_entries_sent, se_list)
551 do_sai_entry_fini(sai, entry);
553 LASSERT(sa_received_empty(sai));
555 cfs_list_for_each_entry_safe(entry, next,
556 &sai->sai_entries_stated, se_list)
557 do_sai_entry_fini(sai, entry);
559 LASSERT(cfs_atomic_read(&sai->sai_cache_count) == 0);
560 LASSERT(agl_list_empty(sai));
569 /* Do NOT forget to drop inode refcount when into sai_entries_agl. */
570 static void ll_agl_trigger(struct inode *inode, struct ll_statahead_info *sai)
572 struct ll_inode_info *lli = ll_i2info(inode);
573 __u64 index = lli->lli_agl_index;
577 LASSERT(cfs_list_empty(&lli->lli_agl_list));
579 /* AGL maybe fall behind statahead with one entry */
580 if (is_omitted_entry(sai, index + 1)) {
581 lli->lli_agl_index = 0;
586 /* Someone is in glimpse (sync or async), do nothing. */
587 rc = cfs_down_write_trylock(&lli->lli_glimpse_sem);
589 lli->lli_agl_index = 0;
595 * Someone triggered glimpse within 1 sec before.
596 * 1) The former glimpse succeeded with glimpse lock granted by OST, and
597 * if the lock is still cached on client, AGL needs to do nothing. If
598 * it is cancelled by other client, AGL maybe cannot obtaion new lock
599 * for no glimpse callback triggered by AGL.
600 * 2) The former glimpse succeeded, but OST did not grant glimpse lock.
601 * Under such case, it is quite possible that the OST will not grant
602 * glimpse lock for AGL also.
603 * 3) The former glimpse failed, compared with other two cases, it is
604 * relative rare. AGL can ignore such case, and it will not muchly
605 * affect the performance.
607 if (lli->lli_glimpse_time != 0 &&
608 cfs_time_before(cfs_time_shift(-1), lli->lli_glimpse_time)) {
609 cfs_up_write(&lli->lli_glimpse_sem);
610 lli->lli_agl_index = 0;
615 CDEBUG(D_READA, "Handling (init) async glimpse: inode = "
616 DFID", idx = "LPU64"\n", PFID(&lli->lli_fid), index);
619 lli->lli_agl_index = 0;
620 lli->lli_glimpse_time = cfs_time_current();
621 cfs_up_write(&lli->lli_glimpse_sem);
623 CDEBUG(D_READA, "Handled (init) async glimpse: inode= "
624 DFID", idx = "LPU64", rc = %d\n",
625 PFID(&lli->lli_fid), index, rc);
632 static void do_statahead_interpret(struct ll_statahead_info *sai,
633 struct ll_sa_entry *target)
635 struct inode *dir = sai->sai_inode;
637 struct ll_inode_info *lli = ll_i2info(dir);
638 struct ll_sa_entry *entry;
639 struct md_enqueue_info *minfo;
640 struct lookup_intent *it;
641 struct ptlrpc_request *req;
642 struct mdt_body *body;
646 cfs_spin_lock(&lli->lli_sa_lock);
647 if (target != NULL && target->se_req != NULL &&
648 !cfs_list_empty(&target->se_list)) {
650 } else if (unlikely(sa_received_empty(sai))) {
651 cfs_spin_unlock(&lli->lli_sa_lock);
654 entry = sa_first_received_entry(sai);
657 cfs_atomic_inc(&entry->se_refcount);
658 cfs_list_del_init(&entry->se_list);
659 cfs_spin_unlock(&lli->lli_sa_lock);
661 LASSERT(entry->se_handle != 0);
663 minfo = entry->se_minfo;
666 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
668 GOTO(out, rc = -EFAULT);
670 child = entry->se_inode;
675 LASSERT(fid_is_zero(&minfo->mi_data.op_fid2));
677 /* XXX: No fid in reply, this is probaly cross-ref case.
678 * SA can't handle it yet. */
679 if (body->valid & OBD_MD_MDS)
680 GOTO(out, rc = -EAGAIN);
685 /* unlinked and re-created with the same name */
686 if (unlikely(!lu_fid_eq(&minfo->mi_data.op_fid2, &body->fid1))){
687 entry->se_inode = NULL;
693 it->d.lustre.it_lock_handle = entry->se_handle;
694 rc = md_revalidate_lock(ll_i2mdexp(dir), it, NULL, NULL);
696 GOTO(out, rc = -EAGAIN);
698 rc = ll_prep_inode(&child, req, dir->i_sb);
702 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
703 child, child->i_ino, child->i_generation);
704 ll_set_lock_data(ll_i2sbi(dir)->ll_md_exp, child, it, NULL);
706 entry->se_inode = child;
708 if (agl_should_run(sai, child))
709 ll_agl_add(sai, child, entry->se_index);
714 /* The "ll_sa_entry_to_stated()" will drop related ldlm ibits lock
715 * reference count by calling "ll_intent_drop_lock()" in spite of the
716 * above operations failed or not. Do not worry about calling
717 * "ll_intent_drop_lock()" more than once. */
718 rc = ll_sa_entry_to_stated(sai, entry, rc < 0 ? rc : SA_ENTRY_SUCC);
719 if (rc == 0 && entry->se_index == sai->sai_index_wait && target == NULL)
720 cfs_waitq_signal(&sai->sai_waitq);
721 ll_sa_entry_put(sai, entry);
724 static int ll_statahead_interpret(struct ptlrpc_request *req,
725 struct md_enqueue_info *minfo, int rc)
727 struct lookup_intent *it = &minfo->mi_it;
728 struct inode *dir = minfo->mi_dir;
729 struct ll_inode_info *lli = ll_i2info(dir);
730 struct ll_statahead_info *sai = NULL;
731 struct ll_sa_entry *entry;
735 if (it_disposition(it, DISP_LOOKUP_NEG))
738 cfs_spin_lock(&lli->lli_sa_lock);
740 if (unlikely(lli->lli_sai == NULL ||
741 lli->lli_sai->sai_generation != minfo->mi_generation)) {
742 cfs_spin_unlock(&lli->lli_sa_lock);
743 GOTO(out, rc = -ESTALE);
745 sai = ll_sai_get(lli->lli_sai);
746 if (unlikely(!thread_is_running(&sai->sai_thread))) {
748 cfs_spin_unlock(&lli->lli_sa_lock);
749 GOTO(out, rc = -EBADFD);
752 entry = ll_sa_entry_get_byindex(sai, minfo->mi_cbdata);
755 cfs_spin_unlock(&lli->lli_sa_lock);
756 GOTO(out, rc = -EIDRM);
759 cfs_list_del_init(&entry->se_list);
762 do_sai_entry_to_stated(sai, entry, rc);
763 cfs_spin_unlock(&lli->lli_sa_lock);
764 if (entry->se_index == sai->sai_index_wait)
765 cfs_waitq_signal(&sai->sai_waitq);
767 entry->se_minfo = minfo;
768 entry->se_req = ptlrpc_request_addref(req);
769 /* Release the async ibits lock ASAP to avoid deadlock
770 * when statahead thread tries to enqueue lock on parent
771 * for readpage and other tries to enqueue lock on child
772 * with parent's lock held, for example: unlink. */
773 entry->se_handle = it->d.lustre.it_lock_handle;
774 ll_intent_drop_lock(it);
775 wakeup = sa_received_empty(sai);
776 cfs_list_add_tail(&entry->se_list,
777 &sai->sai_entries_received);
779 cfs_spin_unlock(&lli->lli_sa_lock);
781 cfs_waitq_signal(&sai->sai_thread.t_ctl_waitq);
783 ll_sa_entry_put(sai, entry);
790 ll_intent_release(it);
799 static void sa_args_fini(struct md_enqueue_info *minfo,
800 struct ldlm_enqueue_info *einfo)
802 LASSERT(minfo && einfo);
804 capa_put(minfo->mi_data.op_capa1);
805 capa_put(minfo->mi_data.op_capa2);
811 * There is race condition between "capa_put" and "ll_statahead_interpret" for
812 * accessing "op_data.op_capa[1,2]" as following:
813 * "capa_put" releases "op_data.op_capa[1,2]"'s reference count after calling
814 * "md_intent_getattr_async". But "ll_statahead_interpret" maybe run first, and
815 * fill "op_data.op_capa[1,2]" as POISON, then cause "capa_put" access invalid
816 * "ocapa". So here reserve "op_data.op_capa[1,2]" in "pcapa" before calling
817 * "md_intent_getattr_async".
819 static int sa_args_init(struct inode *dir, struct inode *child,
820 struct ll_sa_entry *entry, struct md_enqueue_info **pmi,
821 struct ldlm_enqueue_info **pei,
822 struct obd_capa **pcapa)
824 struct qstr *qstr = &entry->se_qstr;
825 struct ll_inode_info *lli = ll_i2info(dir);
826 struct md_enqueue_info *minfo;
827 struct ldlm_enqueue_info *einfo;
828 struct md_op_data *op_data;
830 OBD_ALLOC_PTR(einfo);
834 OBD_ALLOC_PTR(minfo);
840 op_data = ll_prep_md_op_data(&minfo->mi_data, dir, child, qstr->name,
841 qstr->len, 0, LUSTRE_OPC_ANY, NULL);
842 if (IS_ERR(op_data)) {
845 return PTR_ERR(op_data);
848 minfo->mi_it.it_op = IT_GETATTR;
849 minfo->mi_dir = igrab(dir);
850 minfo->mi_cb = ll_statahead_interpret;
851 minfo->mi_generation = lli->lli_sai->sai_generation;
852 minfo->mi_cbdata = entry->se_index;
854 einfo->ei_type = LDLM_IBITS;
855 einfo->ei_mode = it_to_lock_mode(&minfo->mi_it);
856 einfo->ei_cb_bl = ll_md_blocking_ast;
857 einfo->ei_cb_cp = ldlm_completion_ast;
858 einfo->ei_cb_gl = NULL;
859 einfo->ei_cbdata = NULL;
863 pcapa[0] = op_data->op_capa1;
864 pcapa[1] = op_data->op_capa2;
869 static int do_sa_lookup(struct inode *dir, struct ll_sa_entry *entry)
871 struct md_enqueue_info *minfo;
872 struct ldlm_enqueue_info *einfo;
873 struct obd_capa *capas[2];
877 rc = sa_args_init(dir, NULL, entry, &minfo, &einfo, capas);
881 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
886 sa_args_fini(minfo, einfo);
893 * similar to ll_revalidate_it().
894 * \retval 1 -- dentry valid
895 * \retval 0 -- will send stat-ahead request
896 * \retval others -- prepare stat-ahead request failed
898 static int do_sa_revalidate(struct inode *dir, struct ll_sa_entry *entry,
899 struct dentry *dentry)
901 struct inode *inode = dentry->d_inode;
902 struct lookup_intent it = { .it_op = IT_GETATTR,
903 .d.lustre.it_lock_handle = 0 };
904 struct md_enqueue_info *minfo;
905 struct ldlm_enqueue_info *einfo;
906 struct obd_capa *capas[2];
910 if (unlikely(inode == NULL))
913 if (d_mountpoint(dentry))
916 if (unlikely(dentry == dentry->d_sb->s_root))
919 entry->se_inode = igrab(inode);
920 rc = md_revalidate_lock(ll_i2mdexp(dir), &it, ll_inode2fid(inode),NULL);
922 entry->se_handle = it.d.lustre.it_lock_handle;
923 ll_intent_release(&it);
927 rc = sa_args_init(dir, inode, entry, &minfo, &einfo, capas);
929 entry->se_inode = NULL;
934 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
939 entry->se_inode = NULL;
941 sa_args_fini(minfo, einfo);
947 static void ll_statahead_one(struct dentry *parent, const char* entry_name,
950 struct inode *dir = parent->d_inode;
951 struct ll_inode_info *lli = ll_i2info(dir);
952 struct ll_statahead_info *sai = lli->lli_sai;
953 struct dentry *dentry = NULL;
954 struct ll_sa_entry *entry;
959 entry = ll_sa_entry_alloc(sai, sai->sai_index, entry_name,
964 dentry = d_lookup(parent, &entry->se_qstr);
966 rc = do_sa_lookup(dir, entry);
968 rc = do_sa_revalidate(dir, entry, dentry);
969 if (rc == 1 && agl_should_run(sai, dentry->d_inode))
970 ll_agl_add(sai, dentry->d_inode, entry->se_index);
977 rc1 = ll_sa_entry_to_stated(sai, entry,
978 rc < 0 ? SA_ENTRY_INVA : SA_ENTRY_SUCC);
979 if (rc1 == 0 && entry->se_index == sai->sai_index_wait)
980 cfs_waitq_signal(&sai->sai_waitq);
986 /* drop one refcount on entry by ll_sa_entry_alloc */
987 ll_sa_entry_put(sai, entry);
992 static int ll_agl_thread(void *arg)
994 struct dentry *parent = (struct dentry *)arg;
995 struct inode *dir = parent->d_inode;
996 struct ll_inode_info *plli = ll_i2info(dir);
997 struct ll_inode_info *clli;
998 struct ll_sb_info *sbi = ll_i2sbi(dir);
999 struct ll_statahead_info *sai = ll_sai_get(plli->lli_sai);
1000 struct ptlrpc_thread *thread = &sai->sai_agl_thread;
1001 struct l_wait_info lwi = { 0 };
1006 snprintf(pname, 15, "ll_agl_%u", plli->lli_opendir_pid);
1007 cfs_daemonize(pname);
1010 CDEBUG(D_READA, "agl thread started: [pid %d] [parent %.*s]\n",
1011 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1013 atomic_inc(&sbi->ll_agl_total);
1014 cfs_spin_lock(&plli->lli_agl_lock);
1015 sai->sai_agl_valid = 1;
1016 thread_set_flags(thread, SVC_RUNNING);
1017 cfs_spin_unlock(&plli->lli_agl_lock);
1018 cfs_waitq_signal(&thread->t_ctl_waitq);
1021 l_wait_event(thread->t_ctl_waitq,
1022 !agl_list_empty(sai) ||
1023 !thread_is_running(thread),
1026 if (!thread_is_running(thread))
1029 cfs_spin_lock(&plli->lli_agl_lock);
1030 /* The statahead thread maybe help to process AGL entries,
1031 * so check whether list empty again. */
1032 if (!agl_list_empty(sai)) {
1033 clli = agl_first_entry(sai);
1034 cfs_list_del_init(&clli->lli_agl_list);
1035 cfs_spin_unlock(&plli->lli_agl_lock);
1036 ll_agl_trigger(&clli->lli_vfs_inode, sai);
1038 cfs_spin_unlock(&plli->lli_agl_lock);
1042 cfs_spin_lock(&plli->lli_agl_lock);
1043 sai->sai_agl_valid = 0;
1044 while (!agl_list_empty(sai)) {
1045 clli = agl_first_entry(sai);
1046 cfs_list_del_init(&clli->lli_agl_list);
1047 cfs_spin_unlock(&plli->lli_agl_lock);
1048 clli->lli_agl_index = 0;
1049 iput(&clli->lli_vfs_inode);
1050 cfs_spin_lock(&plli->lli_agl_lock);
1052 thread_set_flags(thread, SVC_STOPPED);
1053 cfs_spin_unlock(&plli->lli_agl_lock);
1054 cfs_waitq_signal(&thread->t_ctl_waitq);
1056 CDEBUG(D_READA, "agl thread stopped: [pid %d] [parent %.*s]\n",
1057 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1061 static void ll_start_agl(struct dentry *parent, struct ll_statahead_info *sai)
1063 struct ptlrpc_thread *thread = &sai->sai_agl_thread;
1064 struct l_wait_info lwi = { 0 };
1068 CDEBUG(D_READA, "start agl thread: [pid %d] [parent %.*s]\n",
1069 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1071 rc = cfs_create_thread(ll_agl_thread, parent, 0);
1073 CERROR("can't start ll_agl thread, rc: %d\n", rc);
1074 thread_set_flags(thread, SVC_STOPPED);
1078 l_wait_event(thread->t_ctl_waitq,
1079 thread_is_running(thread) || thread_is_stopped(thread),
1084 static int ll_statahead_thread(void *arg)
1086 struct dentry *parent = (struct dentry *)arg;
1087 struct inode *dir = parent->d_inode;
1088 struct ll_inode_info *plli = ll_i2info(dir);
1089 struct ll_inode_info *clli;
1090 struct ll_sb_info *sbi = ll_i2sbi(dir);
1091 struct ll_statahead_info *sai = ll_sai_get(plli->lli_sai);
1092 struct ptlrpc_thread *thread = &sai->sai_thread;
1093 struct ptlrpc_thread *agl_thread = &sai->sai_agl_thread;
1098 struct ll_dir_chain chain;
1099 struct l_wait_info lwi = { 0 };
1104 snprintf(pname, 15, "ll_sa_%u", plli->lli_opendir_pid);
1105 cfs_daemonize(pname);
1108 CDEBUG(D_READA, "statahead thread started: [pid %d] [parent %.*s]\n",
1109 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1111 if (sbi->ll_flags & LL_SBI_AGL_ENABLED)
1112 ll_start_agl(parent, sai);
1114 atomic_inc(&sbi->ll_sa_total);
1115 cfs_spin_lock(&plli->lli_sa_lock);
1116 thread_set_flags(thread, SVC_RUNNING);
1117 cfs_spin_unlock(&plli->lli_sa_lock);
1118 cfs_waitq_signal(&thread->t_ctl_waitq);
1120 ll_dir_chain_init(&chain);
1121 page = ll_get_dir_page(dir, pos, &chain);
1124 struct lu_dirpage *dp;
1125 struct lu_dirent *ent;
1129 CDEBUG(D_READA, "error reading dir "DFID" at "LPU64
1130 "/"LPU64": [rc %d] [parent %u]\n",
1131 PFID(ll_inode2fid(dir)), pos, sai->sai_index,
1132 rc, plli->lli_opendir_pid);
1136 dp = page_address(page);
1137 for (ent = lu_dirent_start(dp); ent != NULL;
1138 ent = lu_dirent_next(ent)) {
1143 hash = le64_to_cpu(ent->lde_hash);
1144 if (unlikely(hash < pos))
1146 * Skip until we find target hash value.
1150 namelen = le16_to_cpu(ent->lde_namelen);
1151 if (unlikely(namelen == 0))
1153 * Skip dummy record.
1157 name = ent->lde_name;
1158 if (name[0] == '.') {
1164 } else if (name[1] == '.' && namelen == 2) {
1169 } else if (!sai->sai_ls_all) {
1171 * skip hidden files.
1173 sai->sai_skip_hidden++;
1179 * don't stat-ahead first entry.
1181 if (unlikely(++first == 1))
1185 l_wait_event(thread->t_ctl_waitq,
1186 !sa_sent_full(sai) ||
1187 !sa_received_empty(sai) ||
1188 !agl_list_empty(sai) ||
1189 !thread_is_running(thread),
1193 while (!sa_received_empty(sai))
1194 do_statahead_interpret(sai, NULL);
1196 if (unlikely(!thread_is_running(thread))) {
1197 ll_release_page(page, 0);
1201 /* If no window for metadata statahead, but there are
1202 * some AGL entries to be triggered, then try to help
1203 * to process the AGL entries. */
1204 if (sa_sent_full(sai)) {
1205 cfs_spin_lock(&plli->lli_agl_lock);
1206 while (!agl_list_empty(sai)) {
1207 clli = agl_first_entry(sai);
1208 cfs_list_del_init(&clli->lli_agl_list);
1209 cfs_spin_unlock(&plli->lli_agl_lock);
1210 ll_agl_trigger(&clli->lli_vfs_inode,
1213 if (!sa_received_empty(sai))
1217 !thread_is_running(thread))) {
1218 ll_release_page(page, 0);
1222 if (!sa_sent_full(sai))
1225 cfs_spin_lock(&plli->lli_agl_lock);
1227 cfs_spin_unlock(&plli->lli_agl_lock);
1233 ll_statahead_one(parent, name, namelen);
1235 pos = le64_to_cpu(dp->ldp_hash_end);
1236 if (pos == MDS_DIR_END_OFF) {
1238 * End of directory reached.
1240 ll_release_page(page, 0);
1242 l_wait_event(thread->t_ctl_waitq,
1243 !sa_received_empty(sai) ||
1244 sai->sai_sent == sai->sai_replied||
1245 !thread_is_running(thread),
1248 while (!sa_received_empty(sai))
1249 do_statahead_interpret(sai, NULL);
1251 if (unlikely(!thread_is_running(thread)))
1254 if (sai->sai_sent == sai->sai_replied &&
1255 sa_received_empty(sai))
1259 cfs_spin_lock(&plli->lli_agl_lock);
1260 while (!agl_list_empty(sai) &&
1261 thread_is_running(thread)) {
1262 clli = agl_first_entry(sai);
1263 cfs_list_del_init(&clli->lli_agl_list);
1264 cfs_spin_unlock(&plli->lli_agl_lock);
1265 ll_agl_trigger(&clli->lli_vfs_inode, sai);
1266 cfs_spin_lock(&plli->lli_agl_lock);
1268 cfs_spin_unlock(&plli->lli_agl_lock);
1273 * chain is exhausted.
1274 * Normal case: continue to the next page.
1276 ll_release_page(page, le32_to_cpu(dp->ldp_flags) &
1278 sai->sai_in_readpage = 1;
1279 page = ll_get_dir_page(dir, pos, &chain);
1280 sai->sai_in_readpage = 0;
1282 LASSERT(le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1283 ll_release_page(page, 1);
1285 * go into overflow page.
1292 if (sai->sai_agl_valid) {
1293 cfs_spin_lock(&plli->lli_agl_lock);
1294 thread_set_flags(agl_thread, SVC_STOPPING);
1295 cfs_spin_unlock(&plli->lli_agl_lock);
1296 cfs_waitq_signal(&agl_thread->t_ctl_waitq);
1298 CDEBUG(D_READA, "stop agl thread: [pid %d]\n",
1300 l_wait_event(agl_thread->t_ctl_waitq,
1301 thread_is_stopped(agl_thread),
1304 /* Set agl_thread flags anyway. */
1305 thread_set_flags(&sai->sai_agl_thread, SVC_STOPPED);
1307 ll_dir_chain_fini(&chain);
1308 cfs_spin_lock(&plli->lli_sa_lock);
1309 if (!sa_received_empty(sai)) {
1310 thread_set_flags(thread, SVC_STOPPING);
1311 cfs_spin_unlock(&plli->lli_sa_lock);
1313 /* To release the resources held by received entries. */
1314 while (!sa_received_empty(sai))
1315 do_statahead_interpret(sai, NULL);
1317 cfs_spin_lock(&plli->lli_sa_lock);
1319 thread_set_flags(thread, SVC_STOPPED);
1320 cfs_spin_unlock(&plli->lli_sa_lock);
1321 cfs_waitq_signal(&sai->sai_waitq);
1322 cfs_waitq_signal(&thread->t_ctl_waitq);
1325 CDEBUG(D_READA, "statahead thread stopped: [pid %d] [parent %.*s]\n",
1326 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1331 * called in ll_file_release().
1333 void ll_stop_statahead(struct inode *dir, void *key)
1335 struct ll_inode_info *lli = ll_i2info(dir);
1337 if (unlikely(key == NULL))
1340 cfs_spin_lock(&lli->lli_sa_lock);
1341 if (lli->lli_opendir_key != key || lli->lli_opendir_pid == 0) {
1342 cfs_spin_unlock(&lli->lli_sa_lock);
1346 lli->lli_opendir_key = NULL;
1349 struct l_wait_info lwi = { 0 };
1350 struct ptlrpc_thread *thread = &lli->lli_sai->sai_thread;
1352 if (!thread_is_stopped(thread)) {
1353 thread_set_flags(thread, SVC_STOPPING);
1354 cfs_spin_unlock(&lli->lli_sa_lock);
1355 cfs_waitq_signal(&thread->t_ctl_waitq);
1357 CDEBUG(D_READA, "stop statahead thread: [pid %d]\n",
1359 l_wait_event(thread->t_ctl_waitq,
1360 thread_is_stopped(thread),
1363 cfs_spin_unlock(&lli->lli_sa_lock);
1367 * Put the ref which was held when first statahead_enter.
1368 * It maybe not the last ref for some statahead requests
1371 ll_sai_put(lli->lli_sai);
1373 lli->lli_opendir_pid = 0;
1374 cfs_spin_unlock(&lli->lli_sa_lock);
1380 * not first dirent, or is "."
1382 LS_NONE_FIRST_DE = 0,
1384 * the first non-hidden dirent
1388 * the first hidden dirent, that is "."
1393 static int is_first_dirent(struct inode *dir, struct dentry *dentry)
1395 struct ll_dir_chain chain;
1396 struct qstr *target = &dentry->d_name;
1400 int rc = LS_NONE_FIRST_DE;
1403 ll_dir_chain_init(&chain);
1404 page = ll_get_dir_page(dir, pos, &chain);
1407 struct lu_dirpage *dp;
1408 struct lu_dirent *ent;
1411 struct ll_inode_info *lli = ll_i2info(dir);
1414 CERROR("error reading dir "DFID" at "LPU64": "
1415 "[rc %d] [parent %u]\n",
1416 PFID(ll_inode2fid(dir)), pos,
1417 rc, lli->lli_opendir_pid);
1421 dp = page_address(page);
1422 for (ent = lu_dirent_start(dp); ent != NULL;
1423 ent = lu_dirent_next(ent)) {
1428 hash = le64_to_cpu(ent->lde_hash);
1429 /* The ll_get_dir_page() can return any page containing
1430 * the given hash which may be not the start hash. */
1431 if (unlikely(hash < pos))
1434 namelen = le16_to_cpu(ent->lde_namelen);
1435 if (unlikely(namelen == 0))
1437 * skip dummy record.
1441 name = ent->lde_name;
1442 if (name[0] == '.') {
1448 else if (name[1] == '.' && namelen == 2)
1459 if (dot_de && target->name[0] != '.') {
1460 CDEBUG(D_READA, "%.*s skip hidden file %.*s\n",
1461 target->len, target->name,
1466 if (target->len != namelen ||
1467 memcmp(target->name, name, namelen) != 0)
1468 rc = LS_NONE_FIRST_DE;
1472 rc = LS_FIRST_DOT_DE;
1474 ll_release_page(page, 0);
1477 pos = le64_to_cpu(dp->ldp_hash_end);
1478 if (pos == MDS_DIR_END_OFF) {
1480 * End of directory reached.
1482 ll_release_page(page, 0);
1486 * chain is exhausted
1487 * Normal case: continue to the next page.
1489 ll_release_page(page, le32_to_cpu(dp->ldp_flags) &
1491 page = ll_get_dir_page(dir, pos, &chain);
1494 * go into overflow page.
1496 LASSERT(le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1497 ll_release_page(page, 1);
1503 ll_dir_chain_fini(&chain);
1508 ll_sai_unplug(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
1510 struct ptlrpc_thread *thread = &sai->sai_thread;
1511 struct ll_sb_info *sbi = ll_i2sbi(sai->sai_inode);
1515 if (entry != NULL && entry->se_stat == SA_ENTRY_SUCC)
1520 ll_sa_entry_fini(sai, entry);
1523 sai->sai_consecutive_miss = 0;
1524 sai->sai_max = min(2 * sai->sai_max, sbi->ll_sa_max);
1526 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
1529 sai->sai_consecutive_miss++;
1530 if (sa_low_hit(sai) && thread_is_running(thread)) {
1531 atomic_inc(&sbi->ll_sa_wrong);
1532 CDEBUG(D_READA, "Statahead for dir "DFID" hit "
1533 "ratio too low: hit/miss "LPU64"/"LPU64
1534 ", sent/replied "LPU64"/"LPU64", stopping "
1535 "statahead thread: pid %d\n",
1536 PFID(&lli->lli_fid), sai->sai_hit,
1537 sai->sai_miss, sai->sai_sent,
1538 sai->sai_replied, cfs_curproc_pid());
1539 cfs_spin_lock(&lli->lli_sa_lock);
1540 if (!thread_is_stopped(thread))
1541 thread_set_flags(thread, SVC_STOPPING);
1542 cfs_spin_unlock(&lli->lli_sa_lock);
1546 if (!thread_is_stopped(thread))
1547 cfs_waitq_signal(&thread->t_ctl_waitq);
1553 * Start statahead thread if this is the first dir entry.
1554 * Otherwise if a thread is started already, wait it until it is ahead of me.
1555 * \retval 1 -- find entry with lock in cache, the caller needs to do
1557 * \retval 0 -- find entry in cache, but without lock, the caller needs
1559 * \retval others -- the caller need to process as non-statahead.
1561 int do_statahead_enter(struct inode *dir, struct dentry **dentryp,
1564 struct ll_inode_info *lli = ll_i2info(dir);
1565 struct ll_statahead_info *sai = lli->lli_sai;
1566 struct dentry *parent;
1567 struct ll_sa_entry *entry;
1568 struct ptlrpc_thread *thread;
1569 struct l_wait_info lwi = { 0 };
1573 LASSERT(lli->lli_opendir_pid == cfs_curproc_pid());
1576 thread = &sai->sai_thread;
1577 if (unlikely(thread_is_stopped(thread) &&
1578 cfs_list_empty(&sai->sai_entries_stated))) {
1579 /* to release resource */
1580 ll_stop_statahead(dir, lli->lli_opendir_key);
1584 if ((*dentryp)->d_name.name[0] == '.') {
1585 if (sai->sai_ls_all ||
1586 sai->sai_miss_hidden >= sai->sai_skip_hidden) {
1588 * Hidden dentry is the first one, or statahead
1589 * thread does not skip so many hidden dentries
1590 * before "sai_ls_all" enabled as below.
1593 if (!sai->sai_ls_all)
1595 * It maybe because hidden dentry is not
1596 * the first one, "sai_ls_all" was not
1597 * set, then "ls -al" missed. Enable
1598 * "sai_ls_all" for such case.
1600 sai->sai_ls_all = 1;
1603 * Such "getattr" has been skipped before
1604 * "sai_ls_all" enabled as above.
1606 sai->sai_miss_hidden++;
1611 entry = ll_sa_entry_get_byname(sai, &(*dentryp)->d_name);
1612 if (entry == NULL || only_unplug) {
1613 ll_sai_unplug(sai, entry);
1614 RETURN(entry ? 1 : -EAGAIN);
1617 while (!ll_sa_entry_stated(entry) &&
1618 sai->sai_in_readpage &&
1619 !sa_received_empty(sai))
1620 do_statahead_interpret(sai, entry);
1622 if (!ll_sa_entry_stated(entry)) {
1623 sai->sai_index_wait = entry->se_index;
1624 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(30), NULL,
1625 LWI_ON_SIGNAL_NOOP, NULL);
1626 rc = l_wait_event(sai->sai_waitq,
1627 ll_sa_entry_stated(entry) ||
1628 thread_is_stopped(thread),
1631 ll_sai_unplug(sai, entry);
1636 if (entry->se_stat == SA_ENTRY_SUCC &&
1637 entry->se_inode != NULL) {
1638 struct inode *inode = entry->se_inode;
1639 struct lookup_intent it = { .it_op = IT_GETATTR,
1640 .d.lustre.it_lock_handle =
1644 rc = md_revalidate_lock(ll_i2mdexp(dir), &it,
1645 ll_inode2fid(inode), &bits);
1647 if ((*dentryp)->d_inode == NULL) {
1648 *dentryp = ll_splice_alias(inode,
1650 } else if ((*dentryp)->d_inode != inode) {
1651 /* revalidate, but inode is recreated */
1653 "stale dentry %.*s inode %lu/%u, "
1654 "statahead inode %lu/%u\n",
1655 (*dentryp)->d_name.len,
1656 (*dentryp)->d_name.name,
1657 (*dentryp)->d_inode->i_ino,
1658 (*dentryp)->d_inode->i_generation,
1660 inode->i_generation);
1661 ll_sai_unplug(sai, entry);
1666 entry->se_inode = NULL;
1668 if ((bits & MDS_INODELOCK_LOOKUP) &&
1669 d_lustre_invalid(*dentryp))
1670 d_lustre_revalidate(*dentryp);
1671 ll_intent_release(&it);
1675 ll_sai_unplug(sai, entry);
1679 /* I am the "lli_opendir_pid" owner, only me can set "lli_sai". */
1680 rc = is_first_dirent(dir, *dentryp);
1681 if (rc == LS_NONE_FIRST_DE)
1682 /* It is not "ls -{a}l" operation, no need statahead for it. */
1683 GOTO(out, rc = -EAGAIN);
1685 sai = ll_sai_alloc();
1687 GOTO(out, rc = -ENOMEM);
1689 sai->sai_ls_all = (rc == LS_FIRST_DOT_DE);
1690 sai->sai_inode = igrab(dir);
1691 if (unlikely(sai->sai_inode == NULL)) {
1692 CWARN("Do not start stat ahead on dying inode "DFID"\n",
1693 PFID(&lli->lli_fid));
1694 GOTO(out, rc = -ESTALE);
1697 /* get parent reference count here, and put it in ll_statahead_thread */
1698 parent = dget((*dentryp)->d_parent);
1699 if (unlikely(sai->sai_inode != parent->d_inode)) {
1700 struct ll_inode_info *nlli = ll_i2info(parent->d_inode);
1702 CWARN("Race condition, someone changed %.*s just now: "
1703 "old parent "DFID", new parent "DFID"\n",
1704 (*dentryp)->d_name.len, (*dentryp)->d_name.name,
1705 PFID(&lli->lli_fid), PFID(&nlli->lli_fid));
1707 iput(sai->sai_inode);
1708 GOTO(out, rc = -EAGAIN);
1711 CDEBUG(D_READA, "start statahead thread: [pid %d] [parent %.*s]\n",
1712 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1715 rc = cfs_create_thread(ll_statahead_thread, parent, 0);
1716 thread = &sai->sai_thread;
1718 CERROR("can't start ll_sa thread, rc: %d\n", rc);
1720 lli->lli_opendir_key = NULL;
1721 thread_set_flags(thread, SVC_STOPPED);
1722 thread_set_flags(&sai->sai_agl_thread, SVC_STOPPED);
1724 LASSERT(lli->lli_sai == NULL);
1728 l_wait_event(thread->t_ctl_waitq,
1729 thread_is_running(thread) || thread_is_stopped(thread),
1733 * We don't stat-ahead for the first dirent since we are already in
1741 cfs_spin_lock(&lli->lli_sa_lock);
1742 lli->lli_opendir_key = NULL;
1743 lli->lli_opendir_pid = 0;
1744 cfs_spin_unlock(&lli->lli_sa_lock);