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);
1081 l_wait_event(thread->t_ctl_waitq,
1082 thread_is_running(thread) || thread_is_stopped(thread),
1087 static int ll_statahead_thread(void *arg)
1089 struct dentry *parent = (struct dentry *)arg;
1090 struct inode *dir = parent->d_inode;
1091 struct ll_inode_info *plli = ll_i2info(dir);
1092 struct ll_inode_info *clli;
1093 struct ll_sb_info *sbi = ll_i2sbi(dir);
1094 struct ll_statahead_info *sai = ll_sai_get(plli->lli_sai);
1095 struct ptlrpc_thread *thread = &sai->sai_thread;
1100 struct ll_dir_chain chain;
1101 struct l_wait_info lwi = { 0 };
1106 snprintf(pname, 15, "ll_sa_%u", plli->lli_opendir_pid);
1107 cfs_daemonize(pname);
1110 CDEBUG(D_READA, "statahead thread started: [pid %d] [parent %.*s]\n",
1111 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1113 if (sbi->ll_flags & LL_SBI_AGL_ENABLED)
1114 ll_start_agl(parent, sai);
1116 atomic_inc(&sbi->ll_sa_total);
1117 cfs_spin_lock(&plli->lli_sa_lock);
1118 thread_set_flags(thread, SVC_RUNNING);
1119 cfs_spin_unlock(&plli->lli_sa_lock);
1120 cfs_waitq_signal(&thread->t_ctl_waitq);
1122 plli->lli_sa_pos = 0;
1123 ll_dir_chain_init(&chain);
1124 page = ll_get_dir_page(NULL, dir, pos, &chain);
1127 struct lu_dirpage *dp;
1128 struct lu_dirent *ent;
1132 CDEBUG(D_READA, "error reading dir "DFID" at "LPU64
1133 "/"LPU64": [rc %d] [parent %u]\n",
1134 PFID(ll_inode2fid(dir)), pos, sai->sai_index,
1135 rc, plli->lli_opendir_pid);
1139 dp = page_address(page);
1140 for (ent = lu_dirent_start(dp); ent != NULL;
1141 ent = lu_dirent_next(ent)) {
1146 hash = le64_to_cpu(ent->lde_hash);
1147 if (unlikely(hash < pos))
1149 * Skip until we find target hash value.
1153 namelen = le16_to_cpu(ent->lde_namelen);
1154 if (unlikely(namelen == 0))
1156 * Skip dummy record.
1160 name = ent->lde_name;
1161 if (name[0] == '.') {
1167 } else if (name[1] == '.' && namelen == 2) {
1172 } else if (!sai->sai_ls_all) {
1174 * skip hidden files.
1176 sai->sai_skip_hidden++;
1182 * don't stat-ahead first entry.
1184 if (unlikely(++first == 1))
1188 l_wait_event(thread->t_ctl_waitq,
1189 !sa_sent_full(sai) ||
1190 !sa_received_empty(sai) ||
1191 !agl_list_empty(sai) ||
1192 !thread_is_running(thread),
1196 while (!sa_received_empty(sai))
1197 do_statahead_interpret(sai, NULL);
1199 if (unlikely(!thread_is_running(thread))) {
1200 ll_release_page(page, 0);
1204 /* If no window for metadata statahead, but there are
1205 * some AGL entries to be triggered, then try to help
1206 * to process the AGL entries. */
1207 if (sa_sent_full(sai)) {
1208 cfs_spin_lock(&plli->lli_agl_lock);
1209 while (!agl_list_empty(sai)) {
1210 clli = agl_first_entry(sai);
1211 cfs_list_del_init(&clli->lli_agl_list);
1212 cfs_spin_unlock(&plli->lli_agl_lock);
1213 ll_agl_trigger(&clli->lli_vfs_inode,
1216 if (!sa_received_empty(sai))
1220 !thread_is_running(thread))) {
1221 ll_release_page(page, 0);
1225 if (!sa_sent_full(sai))
1228 cfs_spin_lock(&plli->lli_agl_lock);
1230 cfs_spin_unlock(&plli->lli_agl_lock);
1236 ll_statahead_one(parent, name, namelen);
1238 pos = le64_to_cpu(dp->ldp_hash_end);
1239 if (pos == MDS_DIR_END_OFF) {
1241 * End of directory reached.
1243 ll_release_page(page, 0);
1245 l_wait_event(thread->t_ctl_waitq,
1246 !sa_received_empty(sai) ||
1247 sai->sai_sent == sai->sai_replied||
1248 !thread_is_running(thread),
1251 while (!sa_received_empty(sai))
1252 do_statahead_interpret(sai, NULL);
1254 if (unlikely(!thread_is_running(thread)))
1257 if (sai->sai_sent == sai->sai_replied &&
1258 sa_received_empty(sai))
1262 cfs_spin_lock(&plli->lli_agl_lock);
1263 while (!agl_list_empty(sai) &&
1264 thread_is_running(thread)) {
1265 clli = agl_first_entry(sai);
1266 cfs_list_del_init(&clli->lli_agl_list);
1267 cfs_spin_unlock(&plli->lli_agl_lock);
1268 ll_agl_trigger(&clli->lli_vfs_inode, sai);
1269 cfs_spin_lock(&plli->lli_agl_lock);
1271 cfs_spin_unlock(&plli->lli_agl_lock);
1276 * chain is exhausted.
1277 * Normal case: continue to the next page.
1279 ll_release_page(page, le32_to_cpu(dp->ldp_flags) &
1281 plli->lli_sa_pos = pos;
1282 sai->sai_in_readpage = 1;
1283 page = ll_get_dir_page(NULL, dir, pos, &chain);
1284 sai->sai_in_readpage = 0;
1286 LASSERT(le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1287 ll_release_page(page, 1);
1289 * go into overflow page.
1296 if (sai->sai_agl_valid) {
1297 struct ptlrpc_thread *agl_thread = &sai->sai_agl_thread;
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),
1311 ll_dir_chain_fini(&chain);
1312 cfs_spin_lock(&plli->lli_sa_lock);
1313 if (!sa_received_empty(sai)) {
1314 thread_set_flags(thread, SVC_STOPPING);
1315 cfs_spin_unlock(&plli->lli_sa_lock);
1317 /* To release the resources held by received entries. */
1318 while (!sa_received_empty(sai))
1319 do_statahead_interpret(sai, NULL);
1321 cfs_spin_lock(&plli->lli_sa_lock);
1323 thread_set_flags(thread, SVC_STOPPED);
1324 cfs_spin_unlock(&plli->lli_sa_lock);
1325 cfs_waitq_signal(&sai->sai_waitq);
1326 cfs_waitq_signal(&thread->t_ctl_waitq);
1329 CDEBUG(D_READA, "statahead thread stopped: [pid %d] [parent %.*s]\n",
1330 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1335 * called in ll_file_release().
1337 void ll_stop_statahead(struct inode *dir, void *key)
1339 struct ll_inode_info *lli = ll_i2info(dir);
1341 if (unlikely(key == NULL))
1344 cfs_spin_lock(&lli->lli_sa_lock);
1345 if (lli->lli_opendir_key != key || lli->lli_opendir_pid == 0) {
1346 cfs_spin_unlock(&lli->lli_sa_lock);
1350 lli->lli_opendir_key = NULL;
1353 struct l_wait_info lwi = { 0 };
1354 struct ptlrpc_thread *thread = &lli->lli_sai->sai_thread;
1356 if (!thread_is_stopped(thread)) {
1357 thread_set_flags(thread, SVC_STOPPING);
1358 cfs_spin_unlock(&lli->lli_sa_lock);
1359 cfs_waitq_signal(&thread->t_ctl_waitq);
1361 CDEBUG(D_READA, "stop statahead thread: [pid %d]\n",
1363 l_wait_event(thread->t_ctl_waitq,
1364 thread_is_stopped(thread),
1367 cfs_spin_unlock(&lli->lli_sa_lock);
1371 * Put the ref which was held when first statahead_enter.
1372 * It maybe not the last ref for some statahead requests
1375 ll_sai_put(lli->lli_sai);
1377 lli->lli_opendir_pid = 0;
1378 cfs_spin_unlock(&lli->lli_sa_lock);
1384 * not first dirent, or is "."
1386 LS_NONE_FIRST_DE = 0,
1388 * the first non-hidden dirent
1392 * the first hidden dirent, that is "."
1397 static int is_first_dirent(struct inode *dir, struct dentry *dentry)
1399 struct ll_inode_info *lli = ll_i2info(dir);
1400 struct ll_dir_chain chain;
1401 struct qstr *target = &dentry->d_name;
1405 int rc = LS_NONE_FIRST_DE;
1408 lli->lli_sa_pos = 0;
1409 ll_dir_chain_init(&chain);
1410 page = ll_get_dir_page(NULL, dir, pos, &chain);
1413 struct lu_dirpage *dp;
1414 struct lu_dirent *ent;
1417 struct ll_inode_info *lli = ll_i2info(dir);
1420 CERROR("error reading dir "DFID" at "LPU64": "
1421 "[rc %d] [parent %u]\n",
1422 PFID(ll_inode2fid(dir)), pos,
1423 rc, lli->lli_opendir_pid);
1427 dp = page_address(page);
1428 for (ent = lu_dirent_start(dp); ent != NULL;
1429 ent = lu_dirent_next(ent)) {
1434 hash = le64_to_cpu(ent->lde_hash);
1435 /* The ll_get_dir_page() can return any page containing
1436 * the given hash which may be not the start hash. */
1437 if (unlikely(hash < pos))
1440 namelen = le16_to_cpu(ent->lde_namelen);
1441 if (unlikely(namelen == 0))
1443 * skip dummy record.
1447 name = ent->lde_name;
1448 if (name[0] == '.') {
1454 else if (name[1] == '.' && namelen == 2)
1465 if (dot_de && target->name[0] != '.') {
1466 CDEBUG(D_READA, "%.*s skip hidden file %.*s\n",
1467 target->len, target->name,
1472 if (target->len != namelen ||
1473 memcmp(target->name, name, namelen) != 0)
1474 rc = LS_NONE_FIRST_DE;
1478 rc = LS_FIRST_DOT_DE;
1480 ll_release_page(page, 0);
1483 pos = le64_to_cpu(dp->ldp_hash_end);
1484 if (pos == MDS_DIR_END_OFF) {
1486 * End of directory reached.
1488 ll_release_page(page, 0);
1492 * chain is exhausted
1493 * Normal case: continue to the next page.
1495 ll_release_page(page, le32_to_cpu(dp->ldp_flags) &
1497 lli->lli_sa_pos = pos;
1498 page = ll_get_dir_page(NULL, dir, pos, &chain);
1501 * go into overflow page.
1503 LASSERT(le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1504 ll_release_page(page, 1);
1510 ll_dir_chain_fini(&chain);
1515 ll_sai_unplug(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
1517 struct ptlrpc_thread *thread = &sai->sai_thread;
1518 struct ll_sb_info *sbi = ll_i2sbi(sai->sai_inode);
1522 if (entry != NULL && entry->se_stat == SA_ENTRY_SUCC)
1527 ll_sa_entry_fini(sai, entry);
1530 sai->sai_consecutive_miss = 0;
1531 sai->sai_max = min(2 * sai->sai_max, sbi->ll_sa_max);
1533 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
1536 sai->sai_consecutive_miss++;
1537 if (sa_low_hit(sai) && thread_is_running(thread)) {
1538 atomic_inc(&sbi->ll_sa_wrong);
1539 CDEBUG(D_READA, "Statahead for dir "DFID" hit "
1540 "ratio too low: hit/miss "LPU64"/"LPU64
1541 ", sent/replied "LPU64"/"LPU64", stopping "
1542 "statahead thread: pid %d\n",
1543 PFID(&lli->lli_fid), sai->sai_hit,
1544 sai->sai_miss, sai->sai_sent,
1545 sai->sai_replied, cfs_curproc_pid());
1546 cfs_spin_lock(&lli->lli_sa_lock);
1547 if (!thread_is_stopped(thread))
1548 thread_set_flags(thread, SVC_STOPPING);
1549 cfs_spin_unlock(&lli->lli_sa_lock);
1553 if (!thread_is_stopped(thread))
1554 cfs_waitq_signal(&thread->t_ctl_waitq);
1560 * Start statahead thread if this is the first dir entry.
1561 * Otherwise if a thread is started already, wait it until it is ahead of me.
1562 * \retval 1 -- find entry with lock in cache, the caller needs to do
1564 * \retval 0 -- find entry in cache, but without lock, the caller needs
1566 * \retval others -- the caller need to process as non-statahead.
1568 int do_statahead_enter(struct inode *dir, struct dentry **dentryp,
1571 struct ll_inode_info *lli = ll_i2info(dir);
1572 struct ll_statahead_info *sai = lli->lli_sai;
1573 struct dentry *parent;
1574 struct ll_sa_entry *entry;
1575 struct ptlrpc_thread *thread;
1576 struct l_wait_info lwi = { 0 };
1580 LASSERT(lli->lli_opendir_pid == cfs_curproc_pid());
1583 thread = &sai->sai_thread;
1584 if (unlikely(thread_is_stopped(thread) &&
1585 cfs_list_empty(&sai->sai_entries_stated))) {
1586 /* to release resource */
1587 ll_stop_statahead(dir, lli->lli_opendir_key);
1591 if ((*dentryp)->d_name.name[0] == '.') {
1592 if (sai->sai_ls_all ||
1593 sai->sai_miss_hidden >= sai->sai_skip_hidden) {
1595 * Hidden dentry is the first one, or statahead
1596 * thread does not skip so many hidden dentries
1597 * before "sai_ls_all" enabled as below.
1600 if (!sai->sai_ls_all)
1602 * It maybe because hidden dentry is not
1603 * the first one, "sai_ls_all" was not
1604 * set, then "ls -al" missed. Enable
1605 * "sai_ls_all" for such case.
1607 sai->sai_ls_all = 1;
1610 * Such "getattr" has been skipped before
1611 * "sai_ls_all" enabled as above.
1613 sai->sai_miss_hidden++;
1618 entry = ll_sa_entry_get_byname(sai, &(*dentryp)->d_name);
1619 if (entry == NULL || only_unplug) {
1620 ll_sai_unplug(sai, entry);
1621 RETURN(entry ? 1 : -EAGAIN);
1624 while (!ll_sa_entry_stated(entry) &&
1625 sai->sai_in_readpage &&
1626 !sa_received_empty(sai))
1627 do_statahead_interpret(sai, entry);
1629 if (!ll_sa_entry_stated(entry)) {
1630 sai->sai_index_wait = entry->se_index;
1631 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(30), NULL,
1632 LWI_ON_SIGNAL_NOOP, NULL);
1633 rc = l_wait_event(sai->sai_waitq,
1634 ll_sa_entry_stated(entry) ||
1635 thread_is_stopped(thread),
1638 ll_sai_unplug(sai, entry);
1643 if (entry->se_stat == SA_ENTRY_SUCC &&
1644 entry->se_inode != NULL) {
1645 struct inode *inode = entry->se_inode;
1646 struct lookup_intent it = { .it_op = IT_GETATTR,
1647 .d.lustre.it_lock_handle =
1649 struct ll_dentry_data *lld;
1652 rc = md_revalidate_lock(ll_i2mdexp(dir), &it,
1653 ll_inode2fid(inode), &bits);
1655 if ((*dentryp)->d_inode == NULL) {
1656 *dentryp = ll_find_alias(inode,
1658 lld = ll_d2d(*dentryp);
1659 if (unlikely(lld == NULL))
1660 ll_dops_init(*dentryp, 1, 1);
1662 LASSERT((*dentryp)->d_inode == inode);
1664 ll_dentry_rehash(*dentryp, 0);
1667 entry->se_inode = NULL;
1669 ll_dentry_reset_flags(*dentryp, bits);
1670 ll_intent_release(&it);
1674 ll_sai_unplug(sai, entry);
1678 /* I am the "lli_opendir_pid" owner, only me can set "lli_sai". */
1679 rc = is_first_dirent(dir, *dentryp);
1680 if (rc == LS_NONE_FIRST_DE)
1681 /* It is not "ls -{a}l" operation, no need statahead for it. */
1682 GOTO(out, rc = -EAGAIN);
1684 sai = ll_sai_alloc();
1686 GOTO(out, rc = -ENOMEM);
1688 sai->sai_ls_all = (rc == LS_FIRST_DOT_DE);
1689 sai->sai_inode = igrab(dir);
1690 if (unlikely(sai->sai_inode == NULL)) {
1691 CWARN("Do not start stat ahead on dying inode "DFID"\n",
1692 PFID(&lli->lli_fid));
1693 GOTO(out, rc = -ESTALE);
1696 /* get parent reference count here, and put it in ll_statahead_thread */
1697 parent = dget((*dentryp)->d_parent);
1698 if (unlikely(sai->sai_inode != parent->d_inode)) {
1699 struct ll_inode_info *nlli = ll_i2info(parent->d_inode);
1701 CWARN("Race condition, someone changed %.*s just now: "
1702 "old parent "DFID", new parent "DFID"\n",
1703 (*dentryp)->d_name.len, (*dentryp)->d_name.name,
1704 PFID(&lli->lli_fid), PFID(&nlli->lli_fid));
1706 iput(sai->sai_inode);
1707 GOTO(out, rc = -EAGAIN);
1710 CDEBUG(D_READA, "start statahead thread: [pid %d] [parent %.*s]\n",
1711 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1714 rc = cfs_create_thread(ll_statahead_thread, parent, 0);
1715 thread = &sai->sai_thread;
1717 CERROR("can't start ll_sa thread, rc: %d\n", rc);
1719 lli->lli_opendir_key = NULL;
1720 thread_set_flags(thread, SVC_STOPPED);
1722 LASSERT(lli->lli_sai == NULL);
1726 l_wait_event(thread->t_ctl_waitq,
1727 thread_is_running(thread) || thread_is_stopped(thread),
1731 * We don't stat-ahead for the first dirent since we are already in
1732 * lookup, and -EEXIST also indicates that this is the first dirent.
1739 cfs_spin_lock(&lli->lli_sa_lock);
1740 lli->lli_opendir_key = NULL;
1741 lli->lli_opendir_pid = 0;
1742 cfs_spin_unlock(&lli->lli_sa_lock);