1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011, 2012, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
40 #include <linux/sched.h>
42 #include <linux/smp_lock.h>
43 #include <linux/highmem.h>
44 #include <linux/pagemap.h>
46 #define DEBUG_SUBSYSTEM S_LLITE
48 #include <obd_support.h>
49 #include <lustre_lite.h>
50 #include <lustre_dlm.h>
51 #include <linux/lustre_version.h>
52 #include "llite_internal.h"
54 #define SA_OMITTED_ENTRY_MAX 8ULL
57 /** negative values are for error cases */
58 SA_ENTRY_INIT = 0, /** init entry */
59 SA_ENTRY_SUCC = 1, /** stat succeed */
60 SA_ENTRY_INVA = 2, /** invalid entry */
61 SA_ENTRY_DEST = 3, /** entry to be destroyed */
65 /* link into sai->sai_entries_{sent,received,stated} */
67 /* link into sai hash table locally */
69 /* entry reference count */
70 cfs_atomic_t se_refcount;
71 /* entry index in the sai */
73 /* low layer ldlm lock handle */
77 /* entry size, contains name */
79 /* pointer to async getattr enqueue info */
80 struct md_enqueue_info *se_minfo;
81 /* pointer to the async getattr request */
82 struct ptlrpc_request *se_req;
83 /* pointer to the target inode */
84 struct inode *se_inode;
89 static unsigned int sai_generation = 0;
90 static cfs_spinlock_t sai_generation_lock = CFS_SPIN_LOCK_UNLOCKED;
92 static inline int ll_sa_entry_unlinked(struct ll_sa_entry *entry)
94 return cfs_list_empty(&entry->se_list);
97 static inline int ll_sa_entry_unhashed(struct ll_sa_entry *entry)
99 return cfs_list_empty(&entry->se_hash);
103 * The entry only can be released by the caller, it is necessary to hold lock.
105 static inline int ll_sa_entry_stated(struct ll_sa_entry *entry)
108 return (entry->se_stat != SA_ENTRY_INIT);
111 static inline int ll_sa_entry_hash(int val)
113 return val & LL_SA_CACHE_MASK;
117 * Insert entry to hash SA table.
120 ll_sa_entry_enhash(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
122 int i = ll_sa_entry_hash(entry->se_qstr.hash);
124 cfs_spin_lock(&sai->sai_cache_lock[i]);
125 cfs_list_add_tail(&entry->se_hash, &sai->sai_cache[i]);
126 cfs_spin_unlock(&sai->sai_cache_lock[i]);
130 * Remove entry from SA table.
133 ll_sa_entry_unhash(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
135 int i = ll_sa_entry_hash(entry->se_qstr.hash);
137 cfs_spin_lock(&sai->sai_cache_lock[i]);
138 cfs_list_del_init(&entry->se_hash);
139 cfs_spin_unlock(&sai->sai_cache_lock[i]);
142 static inline int agl_should_run(struct ll_statahead_info *sai,
145 if (inode != NULL && S_ISREG(inode->i_mode) &&
146 ll_i2info(inode)->lli_smd != NULL && sai->sai_agl_valid)
151 static inline struct ll_sa_entry *
152 sa_first_received_entry(struct ll_statahead_info *sai)
154 return cfs_list_entry(sai->sai_entries_received.next,
155 struct ll_sa_entry, se_list);
158 static inline struct ll_inode_info *
159 agl_first_entry(struct ll_statahead_info *sai)
161 return cfs_list_entry(sai->sai_entries_agl.next,
162 struct ll_inode_info, lli_agl_list);
165 static inline int sa_sent_full(struct ll_statahead_info *sai)
167 return cfs_atomic_read(&sai->sai_cache_count) >= sai->sai_max;
170 static inline int sa_received_empty(struct ll_statahead_info *sai)
172 return cfs_list_empty(&sai->sai_entries_received);
175 static inline int agl_list_empty(struct ll_statahead_info *sai)
177 return cfs_list_empty(&sai->sai_entries_agl);
181 * (1) hit ratio less than 80%
183 * (2) consecutive miss more than 8
184 * then means low hit.
186 static inline int sa_low_hit(struct ll_statahead_info *sai)
188 return ((sai->sai_hit > 7 && sai->sai_hit < 4 * sai->sai_miss) ||
189 (sai->sai_consecutive_miss > 8));
193 * If the given index is behind of statahead window more than
194 * SA_OMITTED_ENTRY_MAX, then it is old.
196 static inline int is_omitted_entry(struct ll_statahead_info *sai, __u64 index)
198 return ((__u64)sai->sai_max + index + SA_OMITTED_ENTRY_MAX <
203 * Insert it into sai_entries_sent tail when init.
205 static struct ll_sa_entry *
206 ll_sa_entry_alloc(struct ll_statahead_info *sai, __u64 index,
207 const char *name, int len)
209 struct ll_inode_info *lli;
210 struct ll_sa_entry *entry;
215 entry_size = sizeof(struct ll_sa_entry) + (len & ~3) + 4;
216 OBD_ALLOC(entry, entry_size);
217 if (unlikely(entry == NULL))
218 RETURN(ERR_PTR(-ENOMEM));
220 CDEBUG(D_READA, "alloc sai entry %.*s(%p) index "LPU64"\n",
221 len, name, entry, index);
223 entry->se_index = index;
226 * Statahead entry reference rules:
228 * 1) When statahead entry is initialized, its reference is set as 2.
229 * One reference is used by the directory scanner. When the scanner
230 * searches the statahead cache for the given name, it can perform
231 * lockless hash lookup (only the scanner can remove entry from hash
232 * list), and once found, it needn't to call "atomic_inc()" for the
233 * entry reference. So the performance is improved. After using the
234 * statahead entry, the scanner will call "atomic_dec()" to drop the
235 * reference held when initialization. If it is the last reference,
236 * the statahead entry will be freed.
238 * 2) All other threads, including statahead thread and ptlrpcd thread,
239 * when they process the statahead entry, the reference for target
240 * should be held to guarantee the entry will not be released by the
241 * directory scanner. After processing the entry, these threads will
242 * drop the entry reference. If it is the last reference, the entry
245 * The second reference when initializes the statahead entry is used
246 * by the statahead thread, following the rule 2).
248 cfs_atomic_set(&entry->se_refcount, 2);
249 entry->se_stat = SA_ENTRY_INIT;
250 entry->se_size = entry_size;
251 dname = (char *)entry + sizeof(struct ll_sa_entry);
252 memcpy(dname, name, len);
254 entry->se_qstr.hash = full_name_hash(name, len);
255 entry->se_qstr.len = len;
256 entry->se_qstr.name = dname;
258 lli = ll_i2info(sai->sai_inode);
259 cfs_spin_lock(&lli->lli_sa_lock);
260 cfs_list_add_tail(&entry->se_list, &sai->sai_entries_sent);
261 cfs_spin_unlock(&lli->lli_sa_lock);
263 cfs_atomic_inc(&sai->sai_cache_count);
264 ll_sa_entry_enhash(sai, entry);
270 * Used by the directory scanner to search entry with name.
272 * Only the caller can remove the entry from hash, so it is unnecessary to hold
273 * hash lock. It is caller's duty to release the init refcount on the entry, so
274 * it is also unnecessary to increase refcount on the entry.
276 static struct ll_sa_entry *
277 ll_sa_entry_get_byname(struct ll_statahead_info *sai, const struct qstr *qstr)
279 struct ll_sa_entry *entry;
280 int i = ll_sa_entry_hash(qstr->hash);
282 cfs_list_for_each_entry(entry, &sai->sai_cache[i], se_hash) {
283 if (entry->se_qstr.hash == qstr->hash &&
284 entry->se_qstr.len == qstr->len &&
285 memcmp(entry->se_qstr.name, qstr->name, qstr->len) == 0)
292 * Used by the async getattr request callback to find entry with index.
294 * Inside lli_sa_lock to prevent others to change the list during the search.
295 * It needs to increase entry refcount before returning to guarantee that the
296 * entry cannot be freed by others.
298 static struct ll_sa_entry *
299 ll_sa_entry_get_byindex(struct ll_statahead_info *sai, __u64 index)
301 struct ll_sa_entry *entry;
303 cfs_list_for_each_entry(entry, &sai->sai_entries_sent, se_list) {
304 if (entry->se_index == index) {
305 cfs_atomic_inc(&entry->se_refcount);
308 if (entry->se_index > index)
314 static void ll_sa_entry_cleanup(struct ll_statahead_info *sai,
315 struct ll_sa_entry *entry)
317 struct md_enqueue_info *minfo = entry->se_minfo;
318 struct ptlrpc_request *req = entry->se_req;
321 entry->se_minfo = NULL;
322 ll_intent_release(&minfo->mi_it);
328 entry->se_req = NULL;
329 ptlrpc_req_finished(req);
333 static void ll_sa_entry_put(struct ll_statahead_info *sai,
334 struct ll_sa_entry *entry)
336 if (cfs_atomic_dec_and_test(&entry->se_refcount)) {
337 CDEBUG(D_READA, "free sai entry %.*s(%p) index "LPU64"\n",
338 entry->se_qstr.len, entry->se_qstr.name, entry,
341 LASSERT(ll_sa_entry_unhashed(entry));
342 LASSERT(ll_sa_entry_unlinked(entry));
344 ll_sa_entry_cleanup(sai, entry);
346 iput(entry->se_inode);
348 OBD_FREE(entry, entry->se_size);
349 cfs_atomic_dec(&sai->sai_cache_count);
354 do_sai_entry_fini(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
356 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
358 ll_sa_entry_unhash(sai, entry);
360 cfs_spin_lock(&lli->lli_sa_lock);
361 entry->se_stat = SA_ENTRY_DEST;
362 if (likely(!ll_sa_entry_unlinked(entry)))
363 cfs_list_del_init(&entry->se_list);
364 cfs_spin_unlock(&lli->lli_sa_lock);
366 ll_sa_entry_put(sai, entry);
370 * Delete it from sai_entries_stated list when fini.
373 ll_sa_entry_fini(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
375 struct ll_sa_entry *pos, *next;
378 do_sai_entry_fini(sai, entry);
380 /* drop old entry from sent list */
381 cfs_list_for_each_entry_safe(pos, next, &sai->sai_entries_sent,
383 if (is_omitted_entry(sai, pos->se_index))
384 do_sai_entry_fini(sai, pos);
389 /* drop old entry from stated list */
390 cfs_list_for_each_entry_safe(pos, next, &sai->sai_entries_stated,
392 if (is_omitted_entry(sai, pos->se_index))
393 do_sai_entry_fini(sai, pos);
400 * Inside lli_sa_lock.
403 do_sai_entry_to_stated(struct ll_statahead_info *sai,
404 struct ll_sa_entry *entry, int rc)
406 struct ll_sa_entry *se;
407 cfs_list_t *pos = &sai->sai_entries_stated;
409 if (!ll_sa_entry_unlinked(entry))
410 cfs_list_del_init(&entry->se_list);
412 cfs_list_for_each_entry_reverse(se, &sai->sai_entries_stated, se_list) {
413 if (se->se_index < entry->se_index) {
419 cfs_list_add(&entry->se_list, pos);
424 * Move entry to sai_entries_stated and sort with the index.
425 * \retval 1 -- entry to be destroyed.
426 * \retval 0 -- entry is inserted into stated list.
429 ll_sa_entry_to_stated(struct ll_statahead_info *sai,
430 struct ll_sa_entry *entry, int rc)
432 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
435 ll_sa_entry_cleanup(sai, entry);
437 cfs_spin_lock(&lli->lli_sa_lock);
438 if (likely(entry->se_stat != SA_ENTRY_DEST)) {
439 do_sai_entry_to_stated(sai, entry, rc);
442 cfs_spin_unlock(&lli->lli_sa_lock);
448 * Insert inode into the list of sai_entries_agl.
450 static void ll_agl_add(struct ll_statahead_info *sai,
451 struct inode *inode, int index)
453 struct ll_inode_info *child = ll_i2info(inode);
454 struct ll_inode_info *parent = ll_i2info(sai->sai_inode);
457 cfs_spin_lock(&child->lli_agl_lock);
458 if (child->lli_agl_index == 0) {
459 child->lli_agl_index = index;
460 cfs_spin_unlock(&child->lli_agl_lock);
462 LASSERT(cfs_list_empty(&child->lli_agl_list));
465 cfs_spin_lock(&parent->lli_agl_lock);
466 if (agl_list_empty(sai))
468 cfs_list_add_tail(&child->lli_agl_list, &sai->sai_entries_agl);
469 cfs_spin_unlock(&parent->lli_agl_lock);
471 cfs_spin_unlock(&child->lli_agl_lock);
475 cfs_waitq_signal(&sai->sai_agl_thread.t_ctl_waitq);
478 static struct ll_statahead_info *ll_sai_alloc(void)
480 struct ll_statahead_info *sai;
488 cfs_atomic_set(&sai->sai_refcount, 1);
490 cfs_spin_lock(&sai_generation_lock);
491 sai->sai_generation = ++sai_generation;
492 if (unlikely(sai_generation == 0))
493 sai->sai_generation = ++sai_generation;
494 cfs_spin_unlock(&sai_generation_lock);
496 sai->sai_max = LL_SA_RPC_MIN;
498 cfs_waitq_init(&sai->sai_waitq);
499 cfs_waitq_init(&sai->sai_thread.t_ctl_waitq);
500 cfs_waitq_init(&sai->sai_agl_thread.t_ctl_waitq);
502 CFS_INIT_LIST_HEAD(&sai->sai_entries_sent);
503 CFS_INIT_LIST_HEAD(&sai->sai_entries_received);
504 CFS_INIT_LIST_HEAD(&sai->sai_entries_stated);
505 CFS_INIT_LIST_HEAD(&sai->sai_entries_agl);
507 for (i = 0; i < LL_SA_CACHE_SIZE; i++) {
508 CFS_INIT_LIST_HEAD(&sai->sai_cache[i]);
509 cfs_spin_lock_init(&sai->sai_cache_lock[i]);
511 cfs_atomic_set(&sai->sai_cache_count, 0);
516 static inline struct ll_statahead_info *
517 ll_sai_get(struct ll_statahead_info *sai)
519 cfs_atomic_inc(&sai->sai_refcount);
523 static void ll_sai_put(struct ll_statahead_info *sai)
525 struct inode *inode = sai->sai_inode;
526 struct ll_inode_info *lli = ll_i2info(inode);
529 if (cfs_atomic_dec_and_lock(&sai->sai_refcount, &lli->lli_sa_lock)) {
530 struct ll_sa_entry *entry, *next;
532 if (unlikely(cfs_atomic_read(&sai->sai_refcount) > 0)) {
533 /* It is race case, the interpret callback just hold
534 * a reference count */
535 cfs_spin_unlock(&lli->lli_sa_lock);
539 LASSERT(lli->lli_opendir_key == NULL);
540 LASSERT(thread_is_stopped(&sai->sai_thread));
541 LASSERT(thread_is_stopped(&sai->sai_agl_thread));
544 lli->lli_opendir_pid = 0;
545 cfs_spin_unlock(&lli->lli_sa_lock);
547 if (sai->sai_sent > sai->sai_replied)
548 CDEBUG(D_READA,"statahead for dir "DFID" does not "
549 "finish: [sent:"LPU64"] [replied:"LPU64"]\n",
551 sai->sai_sent, sai->sai_replied);
553 cfs_list_for_each_entry_safe(entry, next,
554 &sai->sai_entries_sent, se_list)
555 do_sai_entry_fini(sai, entry);
557 LASSERT(sa_received_empty(sai));
559 cfs_list_for_each_entry_safe(entry, next,
560 &sai->sai_entries_stated, se_list)
561 do_sai_entry_fini(sai, entry);
563 LASSERT(cfs_atomic_read(&sai->sai_cache_count) == 0);
564 LASSERT(agl_list_empty(sai));
573 /* Do NOT forget to drop inode refcount when into sai_entries_agl. */
574 static void ll_agl_trigger(struct inode *inode, struct ll_statahead_info *sai)
576 struct ll_inode_info *lli = ll_i2info(inode);
577 __u64 index = lli->lli_agl_index;
581 LASSERT(cfs_list_empty(&lli->lli_agl_list));
583 /* AGL maybe fall behind statahead with one entry */
584 if (is_omitted_entry(sai, index + 1)) {
585 lli->lli_agl_index = 0;
590 /* Someone is in glimpse (sync or async), do nothing. */
591 rc = cfs_down_write_trylock(&lli->lli_glimpse_sem);
593 lli->lli_agl_index = 0;
599 * Someone triggered glimpse within 1 sec before.
600 * 1) The former glimpse succeeded with glimpse lock granted by OST, and
601 * if the lock is still cached on client, AGL needs to do nothing. If
602 * it is cancelled by other client, AGL maybe cannot obtaion new lock
603 * for no glimpse callback triggered by AGL.
604 * 2) The former glimpse succeeded, but OST did not grant glimpse lock.
605 * Under such case, it is quite possible that the OST will not grant
606 * glimpse lock for AGL also.
607 * 3) The former glimpse failed, compared with other two cases, it is
608 * relative rare. AGL can ignore such case, and it will not muchly
609 * affect the performance.
611 if (lli->lli_glimpse_time != 0 &&
612 cfs_time_before(cfs_time_shift(-1), lli->lli_glimpse_time)) {
613 cfs_up_write(&lli->lli_glimpse_sem);
614 lli->lli_agl_index = 0;
619 CDEBUG(D_READA, "Handling (init) async glimpse: inode = "
620 DFID", idx = "LPU64"\n", PFID(&lli->lli_fid), index);
623 lli->lli_agl_index = 0;
624 lli->lli_glimpse_time = cfs_time_current();
625 cfs_up_write(&lli->lli_glimpse_sem);
627 CDEBUG(D_READA, "Handled (init) async glimpse: inode= "
628 DFID", idx = "LPU64", rc = %d\n",
629 PFID(&lli->lli_fid), index, rc);
636 static void do_statahead_interpret(struct ll_statahead_info *sai,
637 struct ll_sa_entry *target)
639 struct inode *dir = sai->sai_inode;
641 struct ll_inode_info *lli = ll_i2info(dir);
642 struct ll_sa_entry *entry;
643 struct md_enqueue_info *minfo;
644 struct lookup_intent *it;
645 struct ptlrpc_request *req;
646 struct mdt_body *body;
650 cfs_spin_lock(&lli->lli_sa_lock);
651 if (target != NULL && target->se_req != NULL &&
652 !cfs_list_empty(&target->se_list)) {
654 } else if (unlikely(sa_received_empty(sai))) {
655 cfs_spin_unlock(&lli->lli_sa_lock);
658 entry = sa_first_received_entry(sai);
661 cfs_atomic_inc(&entry->se_refcount);
662 cfs_list_del_init(&entry->se_list);
663 cfs_spin_unlock(&lli->lli_sa_lock);
665 LASSERT(entry->se_handle != 0);
667 minfo = entry->se_minfo;
670 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
672 GOTO(out, rc = -EFAULT);
674 child = entry->se_inode;
679 LASSERT(fid_is_zero(&minfo->mi_data.op_fid2));
681 /* XXX: No fid in reply, this is probaly cross-ref case.
682 * SA can't handle it yet. */
683 if (body->valid & OBD_MD_MDS)
684 GOTO(out, rc = -EAGAIN);
689 /* unlinked and re-created with the same name */
690 if (unlikely(!lu_fid_eq(&minfo->mi_data.op_fid2, &body->fid1))){
691 entry->se_inode = NULL;
697 it->d.lustre.it_lock_handle = entry->se_handle;
698 rc = md_revalidate_lock(ll_i2mdexp(dir), it, NULL, NULL);
700 GOTO(out, rc = -EAGAIN);
702 rc = ll_prep_inode(&child, req, dir->i_sb);
706 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
707 child, child->i_ino, child->i_generation);
708 ll_set_lock_data(ll_i2sbi(dir)->ll_md_exp, child, it, NULL);
710 entry->se_inode = child;
712 if (agl_should_run(sai, child))
713 ll_agl_add(sai, child, entry->se_index);
718 /* The "ll_sa_entry_to_stated()" will drop related ldlm ibits lock
719 * reference count by calling "ll_intent_drop_lock()" in spite of the
720 * above operations failed or not. Do not worry about calling
721 * "ll_intent_drop_lock()" more than once. */
722 rc = ll_sa_entry_to_stated(sai, entry, rc < 0 ? rc : SA_ENTRY_SUCC);
723 if (rc == 0 && entry->se_index == sai->sai_index_wait && target == NULL)
724 cfs_waitq_signal(&sai->sai_waitq);
725 ll_sa_entry_put(sai, entry);
728 static int ll_statahead_interpret(struct ptlrpc_request *req,
729 struct md_enqueue_info *minfo, int rc)
731 struct lookup_intent *it = &minfo->mi_it;
732 struct inode *dir = minfo->mi_dir;
733 struct ll_inode_info *lli = ll_i2info(dir);
734 struct ll_statahead_info *sai = NULL;
735 struct ll_sa_entry *entry;
739 if (it_disposition(it, DISP_LOOKUP_NEG))
742 cfs_spin_lock(&lli->lli_sa_lock);
744 if (unlikely(lli->lli_sai == NULL ||
745 lli->lli_sai->sai_generation != minfo->mi_generation)) {
746 cfs_spin_unlock(&lli->lli_sa_lock);
747 GOTO(out, rc = -ESTALE);
749 sai = ll_sai_get(lli->lli_sai);
750 if (unlikely(!thread_is_running(&sai->sai_thread))) {
752 cfs_spin_unlock(&lli->lli_sa_lock);
753 GOTO(out, rc = -EBADFD);
756 entry = ll_sa_entry_get_byindex(sai, minfo->mi_cbdata);
759 cfs_spin_unlock(&lli->lli_sa_lock);
760 GOTO(out, rc = -EIDRM);
763 cfs_list_del_init(&entry->se_list);
766 do_sai_entry_to_stated(sai, entry, rc);
767 cfs_spin_unlock(&lli->lli_sa_lock);
768 if (entry->se_index == sai->sai_index_wait)
769 cfs_waitq_signal(&sai->sai_waitq);
771 entry->se_minfo = minfo;
772 entry->se_req = ptlrpc_request_addref(req);
773 /* Release the async ibits lock ASAP to avoid deadlock
774 * when statahead thread tries to enqueue lock on parent
775 * for readpage and other tries to enqueue lock on child
776 * with parent's lock held, for example: unlink. */
777 entry->se_handle = it->d.lustre.it_lock_handle;
778 ll_intent_drop_lock(it);
779 wakeup = sa_received_empty(sai);
780 cfs_list_add_tail(&entry->se_list,
781 &sai->sai_entries_received);
783 cfs_spin_unlock(&lli->lli_sa_lock);
785 cfs_waitq_signal(&sai->sai_thread.t_ctl_waitq);
787 ll_sa_entry_put(sai, entry);
794 ll_intent_release(it);
803 static void sa_args_fini(struct md_enqueue_info *minfo,
804 struct ldlm_enqueue_info *einfo)
806 LASSERT(minfo && einfo);
808 capa_put(minfo->mi_data.op_capa1);
809 capa_put(minfo->mi_data.op_capa2);
815 * There is race condition between "capa_put" and "ll_statahead_interpret" for
816 * accessing "op_data.op_capa[1,2]" as following:
817 * "capa_put" releases "op_data.op_capa[1,2]"'s reference count after calling
818 * "md_intent_getattr_async". But "ll_statahead_interpret" maybe run first, and
819 * fill "op_data.op_capa[1,2]" as POISON, then cause "capa_put" access invalid
820 * "ocapa". So here reserve "op_data.op_capa[1,2]" in "pcapa" before calling
821 * "md_intent_getattr_async".
823 static int sa_args_init(struct inode *dir, struct inode *child,
824 struct ll_sa_entry *entry, struct md_enqueue_info **pmi,
825 struct ldlm_enqueue_info **pei,
826 struct obd_capa **pcapa)
828 struct qstr *qstr = &entry->se_qstr;
829 struct ll_inode_info *lli = ll_i2info(dir);
830 struct md_enqueue_info *minfo;
831 struct ldlm_enqueue_info *einfo;
832 struct md_op_data *op_data;
834 OBD_ALLOC_PTR(einfo);
838 OBD_ALLOC_PTR(minfo);
844 op_data = ll_prep_md_op_data(&minfo->mi_data, dir, child, qstr->name,
845 qstr->len, 0, LUSTRE_OPC_ANY, NULL);
846 if (IS_ERR(op_data)) {
849 return PTR_ERR(op_data);
852 minfo->mi_it.it_op = IT_GETATTR;
853 minfo->mi_dir = igrab(dir);
854 minfo->mi_cb = ll_statahead_interpret;
855 minfo->mi_generation = lli->lli_sai->sai_generation;
856 minfo->mi_cbdata = entry->se_index;
858 einfo->ei_type = LDLM_IBITS;
859 einfo->ei_mode = it_to_lock_mode(&minfo->mi_it);
860 einfo->ei_cb_bl = ll_md_blocking_ast;
861 einfo->ei_cb_cp = ldlm_completion_ast;
862 einfo->ei_cb_gl = NULL;
863 einfo->ei_cbdata = NULL;
867 pcapa[0] = op_data->op_capa1;
868 pcapa[1] = op_data->op_capa2;
873 static int do_sa_lookup(struct inode *dir, struct ll_sa_entry *entry)
875 struct md_enqueue_info *minfo;
876 struct ldlm_enqueue_info *einfo;
877 struct obd_capa *capas[2];
881 rc = sa_args_init(dir, NULL, entry, &minfo, &einfo, capas);
885 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
890 sa_args_fini(minfo, einfo);
897 * similar to ll_revalidate_it().
898 * \retval 1 -- dentry valid
899 * \retval 0 -- will send stat-ahead request
900 * \retval others -- prepare stat-ahead request failed
902 static int do_sa_revalidate(struct inode *dir, struct ll_sa_entry *entry,
903 struct dentry *dentry)
905 struct inode *inode = dentry->d_inode;
906 struct lookup_intent it = { .it_op = IT_GETATTR,
907 .d.lustre.it_lock_handle = 0 };
908 struct md_enqueue_info *minfo;
909 struct ldlm_enqueue_info *einfo;
910 struct obd_capa *capas[2];
914 if (unlikely(inode == NULL))
917 if (d_mountpoint(dentry))
920 if (unlikely(dentry == dentry->d_sb->s_root))
923 entry->se_inode = igrab(inode);
924 rc = md_revalidate_lock(ll_i2mdexp(dir), &it, ll_inode2fid(inode),NULL);
926 entry->se_handle = it.d.lustre.it_lock_handle;
927 ll_intent_release(&it);
931 rc = sa_args_init(dir, inode, entry, &minfo, &einfo, capas);
933 entry->se_inode = NULL;
938 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
943 entry->se_inode = NULL;
945 sa_args_fini(minfo, einfo);
951 static void ll_statahead_one(struct dentry *parent, const char* entry_name,
954 struct inode *dir = parent->d_inode;
955 struct ll_inode_info *lli = ll_i2info(dir);
956 struct ll_statahead_info *sai = lli->lli_sai;
957 struct dentry *dentry = NULL;
958 struct ll_sa_entry *entry;
963 entry = ll_sa_entry_alloc(sai, sai->sai_index, entry_name,
968 dentry = d_lookup(parent, &entry->se_qstr);
970 rc = do_sa_lookup(dir, entry);
972 rc = do_sa_revalidate(dir, entry, dentry);
973 if (rc == 1 && agl_should_run(sai, dentry->d_inode))
974 ll_agl_add(sai, dentry->d_inode, entry->se_index);
981 rc1 = ll_sa_entry_to_stated(sai, entry,
982 rc < 0 ? SA_ENTRY_INVA : SA_ENTRY_SUCC);
983 if (rc1 == 0 && entry->se_index == sai->sai_index_wait)
984 cfs_waitq_signal(&sai->sai_waitq);
990 /* drop one refcount on entry by ll_sa_entry_alloc */
991 ll_sa_entry_put(sai, entry);
996 static int ll_agl_thread(void *arg)
998 struct dentry *parent = (struct dentry *)arg;
999 struct inode *dir = parent->d_inode;
1000 struct ll_inode_info *plli = ll_i2info(dir);
1001 struct ll_inode_info *clli;
1002 struct ll_sb_info *sbi = ll_i2sbi(dir);
1003 struct ll_statahead_info *sai = ll_sai_get(plli->lli_sai);
1004 struct ptlrpc_thread *thread = &sai->sai_agl_thread;
1005 struct l_wait_info lwi = { 0 };
1010 snprintf(pname, 15, "ll_agl_%u", plli->lli_opendir_pid);
1011 cfs_daemonize(pname);
1014 CDEBUG(D_READA, "agl thread started: [pid %d] [parent %.*s]\n",
1015 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1017 atomic_inc(&sbi->ll_agl_total);
1018 cfs_spin_lock(&plli->lli_agl_lock);
1019 sai->sai_agl_valid = 1;
1020 thread_set_flags(thread, SVC_RUNNING);
1021 cfs_spin_unlock(&plli->lli_agl_lock);
1022 cfs_waitq_signal(&thread->t_ctl_waitq);
1025 l_wait_event(thread->t_ctl_waitq,
1026 !agl_list_empty(sai) ||
1027 !thread_is_running(thread),
1030 if (!thread_is_running(thread))
1033 cfs_spin_lock(&plli->lli_agl_lock);
1034 /* The statahead thread maybe help to process AGL entries,
1035 * so check whether list empty again. */
1036 if (!agl_list_empty(sai)) {
1037 clli = agl_first_entry(sai);
1038 cfs_list_del_init(&clli->lli_agl_list);
1039 cfs_spin_unlock(&plli->lli_agl_lock);
1040 ll_agl_trigger(&clli->lli_vfs_inode, sai);
1042 cfs_spin_unlock(&plli->lli_agl_lock);
1046 cfs_spin_lock(&plli->lli_agl_lock);
1047 sai->sai_agl_valid = 0;
1048 while (!agl_list_empty(sai)) {
1049 clli = agl_first_entry(sai);
1050 cfs_list_del_init(&clli->lli_agl_list);
1051 cfs_spin_unlock(&plli->lli_agl_lock);
1052 clli->lli_agl_index = 0;
1053 iput(&clli->lli_vfs_inode);
1054 cfs_spin_lock(&plli->lli_agl_lock);
1056 thread_set_flags(thread, SVC_STOPPED);
1057 cfs_spin_unlock(&plli->lli_agl_lock);
1058 cfs_waitq_signal(&thread->t_ctl_waitq);
1060 CDEBUG(D_READA, "agl thread stopped: [pid %d] [parent %.*s]\n",
1061 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1065 static void ll_start_agl(struct dentry *parent, struct ll_statahead_info *sai)
1067 struct ptlrpc_thread *thread = &sai->sai_agl_thread;
1068 struct l_wait_info lwi = { 0 };
1072 CDEBUG(D_READA, "start agl thread: [pid %d] [parent %.*s]\n",
1073 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1075 rc = cfs_create_thread(ll_agl_thread, parent, 0);
1077 CERROR("can't start ll_agl thread, rc: %d\n", rc);
1078 thread_set_flags(thread, SVC_STOPPED);
1082 l_wait_event(thread->t_ctl_waitq,
1083 thread_is_running(thread) || thread_is_stopped(thread),
1088 static int ll_statahead_thread(void *arg)
1090 struct dentry *parent = (struct dentry *)arg;
1091 struct inode *dir = parent->d_inode;
1092 struct ll_inode_info *plli = ll_i2info(dir);
1093 struct ll_inode_info *clli;
1094 struct ll_sb_info *sbi = ll_i2sbi(dir);
1095 struct ll_statahead_info *sai = ll_sai_get(plli->lli_sai);
1096 struct ptlrpc_thread *thread = &sai->sai_thread;
1097 struct ptlrpc_thread *agl_thread = &sai->sai_agl_thread;
1102 struct ll_dir_chain chain;
1103 struct l_wait_info lwi = { 0 };
1108 snprintf(pname, 15, "ll_sa_%u", plli->lli_opendir_pid);
1109 cfs_daemonize(pname);
1112 CDEBUG(D_READA, "statahead thread started: [pid %d] [parent %.*s]\n",
1113 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1115 if (sbi->ll_flags & LL_SBI_AGL_ENABLED)
1116 ll_start_agl(parent, sai);
1118 atomic_inc(&sbi->ll_sa_total);
1119 cfs_spin_lock(&plli->lli_sa_lock);
1120 thread_set_flags(thread, SVC_RUNNING);
1121 cfs_spin_unlock(&plli->lli_sa_lock);
1122 cfs_waitq_signal(&thread->t_ctl_waitq);
1124 plli->lli_sa_pos = 0;
1125 ll_dir_chain_init(&chain);
1126 page = ll_get_dir_page(NULL, dir, pos, &chain);
1129 struct lu_dirpage *dp;
1130 struct lu_dirent *ent;
1134 CDEBUG(D_READA, "error reading dir "DFID" at "LPU64
1135 "/"LPU64": [rc %d] [parent %u]\n",
1136 PFID(ll_inode2fid(dir)), pos, sai->sai_index,
1137 rc, plli->lli_opendir_pid);
1141 dp = page_address(page);
1142 for (ent = lu_dirent_start(dp); ent != NULL;
1143 ent = lu_dirent_next(ent)) {
1148 hash = le64_to_cpu(ent->lde_hash);
1149 if (unlikely(hash < pos))
1151 * Skip until we find target hash value.
1155 namelen = le16_to_cpu(ent->lde_namelen);
1156 if (unlikely(namelen == 0))
1158 * Skip dummy record.
1162 name = ent->lde_name;
1163 if (name[0] == '.') {
1169 } else if (name[1] == '.' && namelen == 2) {
1174 } else if (!sai->sai_ls_all) {
1176 * skip hidden files.
1178 sai->sai_skip_hidden++;
1184 * don't stat-ahead first entry.
1186 if (unlikely(++first == 1))
1190 l_wait_event(thread->t_ctl_waitq,
1191 !sa_sent_full(sai) ||
1192 !sa_received_empty(sai) ||
1193 !agl_list_empty(sai) ||
1194 !thread_is_running(thread),
1198 while (!sa_received_empty(sai))
1199 do_statahead_interpret(sai, NULL);
1201 if (unlikely(!thread_is_running(thread))) {
1202 ll_release_page(page, 0);
1206 /* If no window for metadata statahead, but there are
1207 * some AGL entries to be triggered, then try to help
1208 * to process the AGL entries. */
1209 if (sa_sent_full(sai)) {
1210 cfs_spin_lock(&plli->lli_agl_lock);
1211 while (!agl_list_empty(sai)) {
1212 clli = agl_first_entry(sai);
1213 cfs_list_del_init(&clli->lli_agl_list);
1214 cfs_spin_unlock(&plli->lli_agl_lock);
1215 ll_agl_trigger(&clli->lli_vfs_inode,
1218 if (!sa_received_empty(sai))
1222 !thread_is_running(thread))) {
1223 ll_release_page(page, 0);
1227 if (!sa_sent_full(sai))
1230 cfs_spin_lock(&plli->lli_agl_lock);
1232 cfs_spin_unlock(&plli->lli_agl_lock);
1238 ll_statahead_one(parent, name, namelen);
1240 pos = le64_to_cpu(dp->ldp_hash_end);
1241 if (pos == MDS_DIR_END_OFF) {
1243 * End of directory reached.
1245 ll_release_page(page, 0);
1247 l_wait_event(thread->t_ctl_waitq,
1248 !sa_received_empty(sai) ||
1249 sai->sai_sent == sai->sai_replied||
1250 !thread_is_running(thread),
1253 while (!sa_received_empty(sai))
1254 do_statahead_interpret(sai, NULL);
1256 if (unlikely(!thread_is_running(thread)))
1259 if (sai->sai_sent == sai->sai_replied &&
1260 sa_received_empty(sai))
1264 cfs_spin_lock(&plli->lli_agl_lock);
1265 while (!agl_list_empty(sai) &&
1266 thread_is_running(thread)) {
1267 clli = agl_first_entry(sai);
1268 cfs_list_del_init(&clli->lli_agl_list);
1269 cfs_spin_unlock(&plli->lli_agl_lock);
1270 ll_agl_trigger(&clli->lli_vfs_inode, sai);
1271 cfs_spin_lock(&plli->lli_agl_lock);
1273 cfs_spin_unlock(&plli->lli_agl_lock);
1278 * chain is exhausted.
1279 * Normal case: continue to the next page.
1281 ll_release_page(page, le32_to_cpu(dp->ldp_flags) &
1283 plli->lli_sa_pos = pos;
1284 sai->sai_in_readpage = 1;
1285 page = ll_get_dir_page(NULL, dir, pos, &chain);
1286 sai->sai_in_readpage = 0;
1288 LASSERT(le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1289 ll_release_page(page, 1);
1291 * go into overflow page.
1298 if (sai->sai_agl_valid) {
1299 cfs_spin_lock(&plli->lli_agl_lock);
1300 thread_set_flags(agl_thread, SVC_STOPPING);
1301 cfs_spin_unlock(&plli->lli_agl_lock);
1302 cfs_waitq_signal(&agl_thread->t_ctl_waitq);
1304 CDEBUG(D_READA, "stop agl thread: [pid %d]\n",
1306 l_wait_event(agl_thread->t_ctl_waitq,
1307 thread_is_stopped(agl_thread),
1310 /* Set agl_thread flags anyway. */
1311 thread_set_flags(&sai->sai_agl_thread, SVC_STOPPED);
1313 ll_dir_chain_fini(&chain);
1314 cfs_spin_lock(&plli->lli_sa_lock);
1315 if (!sa_received_empty(sai)) {
1316 thread_set_flags(thread, SVC_STOPPING);
1317 cfs_spin_unlock(&plli->lli_sa_lock);
1319 /* To release the resources held by received entries. */
1320 while (!sa_received_empty(sai))
1321 do_statahead_interpret(sai, NULL);
1323 cfs_spin_lock(&plli->lli_sa_lock);
1325 thread_set_flags(thread, SVC_STOPPED);
1326 cfs_spin_unlock(&plli->lli_sa_lock);
1327 cfs_waitq_signal(&sai->sai_waitq);
1328 cfs_waitq_signal(&thread->t_ctl_waitq);
1331 CDEBUG(D_READA, "statahead thread stopped: [pid %d] [parent %.*s]\n",
1332 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1337 * called in ll_file_release().
1339 void ll_stop_statahead(struct inode *dir, void *key)
1341 struct ll_inode_info *lli = ll_i2info(dir);
1343 if (unlikely(key == NULL))
1346 cfs_spin_lock(&lli->lli_sa_lock);
1347 if (lli->lli_opendir_key != key || lli->lli_opendir_pid == 0) {
1348 cfs_spin_unlock(&lli->lli_sa_lock);
1352 lli->lli_opendir_key = NULL;
1355 struct l_wait_info lwi = { 0 };
1356 struct ptlrpc_thread *thread = &lli->lli_sai->sai_thread;
1358 if (!thread_is_stopped(thread)) {
1359 thread_set_flags(thread, SVC_STOPPING);
1360 cfs_spin_unlock(&lli->lli_sa_lock);
1361 cfs_waitq_signal(&thread->t_ctl_waitq);
1363 CDEBUG(D_READA, "stop statahead thread: [pid %d]\n",
1365 l_wait_event(thread->t_ctl_waitq,
1366 thread_is_stopped(thread),
1369 cfs_spin_unlock(&lli->lli_sa_lock);
1373 * Put the ref which was held when first statahead_enter.
1374 * It maybe not the last ref for some statahead requests
1377 ll_sai_put(lli->lli_sai);
1379 lli->lli_opendir_pid = 0;
1380 cfs_spin_unlock(&lli->lli_sa_lock);
1386 * not first dirent, or is "."
1388 LS_NONE_FIRST_DE = 0,
1390 * the first non-hidden dirent
1394 * the first hidden dirent, that is "."
1399 static int is_first_dirent(struct inode *dir, struct dentry *dentry)
1401 struct ll_inode_info *lli = ll_i2info(dir);
1402 struct ll_dir_chain chain;
1403 struct qstr *target = &dentry->d_name;
1407 int rc = LS_NONE_FIRST_DE;
1410 lli->lli_sa_pos = 0;
1411 ll_dir_chain_init(&chain);
1412 page = ll_get_dir_page(NULL, dir, pos, &chain);
1415 struct lu_dirpage *dp;
1416 struct lu_dirent *ent;
1419 struct ll_inode_info *lli = ll_i2info(dir);
1422 CERROR("error reading dir "DFID" at "LPU64": "
1423 "[rc %d] [parent %u]\n",
1424 PFID(ll_inode2fid(dir)), pos,
1425 rc, lli->lli_opendir_pid);
1429 dp = page_address(page);
1430 for (ent = lu_dirent_start(dp); ent != NULL;
1431 ent = lu_dirent_next(ent)) {
1436 hash = le64_to_cpu(ent->lde_hash);
1437 /* The ll_get_dir_page() can return any page containing
1438 * the given hash which may be not the start hash. */
1439 if (unlikely(hash < pos))
1442 namelen = le16_to_cpu(ent->lde_namelen);
1443 if (unlikely(namelen == 0))
1445 * skip dummy record.
1449 name = ent->lde_name;
1450 if (name[0] == '.') {
1456 else if (name[1] == '.' && namelen == 2)
1467 if (dot_de && target->name[0] != '.') {
1468 CDEBUG(D_READA, "%.*s skip hidden file %.*s\n",
1469 target->len, target->name,
1474 if (target->len != namelen ||
1475 memcmp(target->name, name, namelen) != 0)
1476 rc = LS_NONE_FIRST_DE;
1480 rc = LS_FIRST_DOT_DE;
1482 ll_release_page(page, 0);
1485 pos = le64_to_cpu(dp->ldp_hash_end);
1486 if (pos == MDS_DIR_END_OFF) {
1488 * End of directory reached.
1490 ll_release_page(page, 0);
1494 * chain is exhausted
1495 * Normal case: continue to the next page.
1497 ll_release_page(page, le32_to_cpu(dp->ldp_flags) &
1499 lli->lli_sa_pos = pos;
1500 page = ll_get_dir_page(NULL, dir, pos, &chain);
1503 * go into overflow page.
1505 LASSERT(le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1506 ll_release_page(page, 1);
1512 ll_dir_chain_fini(&chain);
1517 ll_sai_unplug(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
1519 struct ptlrpc_thread *thread = &sai->sai_thread;
1520 struct ll_sb_info *sbi = ll_i2sbi(sai->sai_inode);
1524 if (entry != NULL && entry->se_stat == SA_ENTRY_SUCC)
1529 ll_sa_entry_fini(sai, entry);
1532 sai->sai_consecutive_miss = 0;
1533 sai->sai_max = min(2 * sai->sai_max, sbi->ll_sa_max);
1535 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
1538 sai->sai_consecutive_miss++;
1539 if (sa_low_hit(sai) && thread_is_running(thread)) {
1540 atomic_inc(&sbi->ll_sa_wrong);
1541 CDEBUG(D_READA, "Statahead for dir "DFID" hit "
1542 "ratio too low: hit/miss "LPU64"/"LPU64
1543 ", sent/replied "LPU64"/"LPU64", stopping "
1544 "statahead thread: pid %d\n",
1545 PFID(&lli->lli_fid), sai->sai_hit,
1546 sai->sai_miss, sai->sai_sent,
1547 sai->sai_replied, cfs_curproc_pid());
1548 cfs_spin_lock(&lli->lli_sa_lock);
1549 if (!thread_is_stopped(thread))
1550 thread_set_flags(thread, SVC_STOPPING);
1551 cfs_spin_unlock(&lli->lli_sa_lock);
1555 if (!thread_is_stopped(thread))
1556 cfs_waitq_signal(&thread->t_ctl_waitq);
1562 * Start statahead thread if this is the first dir entry.
1563 * Otherwise if a thread is started already, wait it until it is ahead of me.
1564 * \retval 1 -- find entry with lock in cache, the caller needs to do
1566 * \retval 0 -- find entry in cache, but without lock, the caller needs
1568 * \retval others -- the caller need to process as non-statahead.
1570 int do_statahead_enter(struct inode *dir, struct dentry **dentryp,
1573 struct ll_inode_info *lli = ll_i2info(dir);
1574 struct ll_statahead_info *sai = lli->lli_sai;
1575 struct dentry *parent;
1576 struct ll_sa_entry *entry;
1577 struct ptlrpc_thread *thread;
1578 struct l_wait_info lwi = { 0 };
1582 LASSERT(lli->lli_opendir_pid == cfs_curproc_pid());
1585 thread = &sai->sai_thread;
1586 if (unlikely(thread_is_stopped(thread) &&
1587 cfs_list_empty(&sai->sai_entries_stated))) {
1588 /* to release resource */
1589 ll_stop_statahead(dir, lli->lli_opendir_key);
1593 if ((*dentryp)->d_name.name[0] == '.') {
1594 if (sai->sai_ls_all ||
1595 sai->sai_miss_hidden >= sai->sai_skip_hidden) {
1597 * Hidden dentry is the first one, or statahead
1598 * thread does not skip so many hidden dentries
1599 * before "sai_ls_all" enabled as below.
1602 if (!sai->sai_ls_all)
1604 * It maybe because hidden dentry is not
1605 * the first one, "sai_ls_all" was not
1606 * set, then "ls -al" missed. Enable
1607 * "sai_ls_all" for such case.
1609 sai->sai_ls_all = 1;
1612 * Such "getattr" has been skipped before
1613 * "sai_ls_all" enabled as above.
1615 sai->sai_miss_hidden++;
1620 entry = ll_sa_entry_get_byname(sai, &(*dentryp)->d_name);
1621 if (entry == NULL || only_unplug) {
1622 ll_sai_unplug(sai, entry);
1623 RETURN(entry ? 1 : -EAGAIN);
1626 while (!ll_sa_entry_stated(entry) &&
1627 sai->sai_in_readpage &&
1628 !sa_received_empty(sai))
1629 do_statahead_interpret(sai, entry);
1631 if (!ll_sa_entry_stated(entry)) {
1632 sai->sai_index_wait = entry->se_index;
1633 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(30), NULL,
1634 LWI_ON_SIGNAL_NOOP, NULL);
1635 rc = l_wait_event(sai->sai_waitq,
1636 ll_sa_entry_stated(entry) ||
1637 thread_is_stopped(thread),
1640 ll_sai_unplug(sai, entry);
1645 if (entry->se_stat == SA_ENTRY_SUCC &&
1646 entry->se_inode != NULL) {
1647 struct inode *inode = entry->se_inode;
1648 struct lookup_intent it = { .it_op = IT_GETATTR,
1649 .d.lustre.it_lock_handle =
1651 struct ll_dentry_data *lld;
1654 rc = md_revalidate_lock(ll_i2mdexp(dir), &it,
1655 ll_inode2fid(inode), &bits);
1657 if ((*dentryp)->d_inode == NULL) {
1658 *dentryp = ll_find_alias(inode,
1660 lld = ll_d2d(*dentryp);
1661 if (unlikely(lld == NULL))
1662 ll_dops_init(*dentryp, 1, 1);
1664 LASSERT((*dentryp)->d_inode == inode);
1666 ll_dentry_rehash(*dentryp, 0);
1669 entry->se_inode = NULL;
1671 ll_dentry_reset_flags(*dentryp, bits);
1672 ll_intent_release(&it);
1676 ll_sai_unplug(sai, entry);
1680 /* I am the "lli_opendir_pid" owner, only me can set "lli_sai". */
1681 rc = is_first_dirent(dir, *dentryp);
1682 if (rc == LS_NONE_FIRST_DE)
1683 /* It is not "ls -{a}l" operation, no need statahead for it. */
1684 GOTO(out, rc = -EAGAIN);
1686 sai = ll_sai_alloc();
1688 GOTO(out, rc = -ENOMEM);
1690 sai->sai_ls_all = (rc == LS_FIRST_DOT_DE);
1691 sai->sai_inode = igrab(dir);
1692 if (unlikely(sai->sai_inode == NULL)) {
1693 CWARN("Do not start stat ahead on dying inode "DFID"\n",
1694 PFID(&lli->lli_fid));
1695 GOTO(out, rc = -ESTALE);
1698 /* get parent reference count here, and put it in ll_statahead_thread */
1699 parent = dget((*dentryp)->d_parent);
1700 if (unlikely(sai->sai_inode != parent->d_inode)) {
1701 struct ll_inode_info *nlli = ll_i2info(parent->d_inode);
1703 CWARN("Race condition, someone changed %.*s just now: "
1704 "old parent "DFID", new parent "DFID"\n",
1705 (*dentryp)->d_name.len, (*dentryp)->d_name.name,
1706 PFID(&lli->lli_fid), PFID(&nlli->lli_fid));
1708 iput(sai->sai_inode);
1709 GOTO(out, rc = -EAGAIN);
1712 CDEBUG(D_READA, "start statahead thread: [pid %d] [parent %.*s]\n",
1713 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1716 rc = cfs_create_thread(ll_statahead_thread, parent, 0);
1717 thread = &sai->sai_thread;
1719 CERROR("can't start ll_sa thread, rc: %d\n", rc);
1721 lli->lli_opendir_key = NULL;
1722 thread_set_flags(thread, SVC_STOPPED);
1723 thread_set_flags(&sai->sai_agl_thread, SVC_STOPPED);
1725 LASSERT(lli->lli_sai == NULL);
1729 l_wait_event(thread->t_ctl_waitq,
1730 thread_is_running(thread) || thread_is_stopped(thread),
1734 * We don't stat-ahead for the first dirent since we are already in
1735 * lookup, and -EEXIST also indicates that this is the first dirent.
1742 cfs_spin_lock(&lli->lli_sa_lock);
1743 lli->lli_opendir_key = NULL;
1744 lli->lli_opendir_pid = 0;
1745 cfs_spin_unlock(&lli->lli_sa_lock);