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 Whamcloud, Inc.
36 * This file is part of Lustre, http://www.lustre.org/
37 * Lustre is a trademark of Sun Microsystems, Inc.
41 #include <linux/sched.h>
43 #include <linux/smp_lock.h>
44 #include <linux/highmem.h>
45 #include <linux/pagemap.h>
47 #define DEBUG_SUBSYSTEM S_LLITE
49 #include <obd_support.h>
50 #include <lustre_lite.h>
51 #include <lustre_dlm.h>
52 #include <linux/lustre_version.h>
53 #include "llite_internal.h"
55 #define SA_OMITTED_ENTRY_MAX 8ULL
58 /** negative values are for error cases */
59 SA_ENTRY_INIT = 0, /** init entry */
60 SA_ENTRY_SUCC = 1, /** stat succeed */
61 SA_ENTRY_INVA = 2, /** invalid entry */
62 SA_ENTRY_DEST = 3, /** entry to be destroyed */
66 /* link into sai->sai_entries_{sent,received,stated} */
68 /* link into sai hash table locally */
70 /* entry reference count */
71 cfs_atomic_t se_refcount;
72 /* entry index in the sai */
74 /* low layer ldlm lock handle */
78 /* entry size, contains name */
80 /* pointer to async getattr enqueue info */
81 struct md_enqueue_info *se_minfo;
82 /* pointer to the async getattr request */
83 struct ptlrpc_request *se_req;
84 /* pointer to the target inode */
85 struct inode *se_inode;
90 static unsigned int sai_generation = 0;
91 static cfs_spinlock_t sai_generation_lock = CFS_SPIN_LOCK_UNLOCKED;
93 static inline int ll_sa_entry_unlinked(struct ll_sa_entry *entry)
95 return cfs_list_empty(&entry->se_list);
98 static inline int ll_sa_entry_unhashed(struct ll_sa_entry *entry)
100 return cfs_list_empty(&entry->se_hash);
104 * The entry only can be released by the caller, it is necessary to hold lock.
106 static inline int ll_sa_entry_stated(struct ll_sa_entry *entry)
109 return (entry->se_stat != SA_ENTRY_INIT);
112 static inline int ll_sa_entry_hash(int val)
114 return val & LL_SA_CACHE_MASK;
118 * Insert entry to hash SA table.
121 ll_sa_entry_enhash(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
123 int i = ll_sa_entry_hash(entry->se_qstr.hash);
125 cfs_spin_lock(&sai->sai_cache_lock[i]);
126 cfs_list_add_tail(&entry->se_hash, &sai->sai_cache[i]);
127 cfs_spin_unlock(&sai->sai_cache_lock[i]);
131 * Remove entry from SA table.
134 ll_sa_entry_unhash(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
136 int i = ll_sa_entry_hash(entry->se_qstr.hash);
138 cfs_spin_lock(&sai->sai_cache_lock[i]);
139 cfs_list_del_init(&entry->se_hash);
140 cfs_spin_unlock(&sai->sai_cache_lock[i]);
143 static inline int agl_should_run(struct ll_statahead_info *sai,
146 if (inode != NULL && S_ISREG(inode->i_mode) &&
147 ll_i2info(inode)->lli_smd != NULL && sai->sai_agl_valid)
152 static inline struct ll_sa_entry *
153 sa_first_received_entry(struct ll_statahead_info *sai)
155 return cfs_list_entry(sai->sai_entries_received.next,
156 struct ll_sa_entry, se_list);
159 static inline struct ll_inode_info *
160 agl_first_entry(struct ll_statahead_info *sai)
162 return cfs_list_entry(sai->sai_entries_agl.next,
163 struct ll_inode_info, lli_agl_list);
166 static inline int sa_sent_full(struct ll_statahead_info *sai)
168 return cfs_atomic_read(&sai->sai_cache_count) >= sai->sai_max;
171 static inline int sa_received_empty(struct ll_statahead_info *sai)
173 return cfs_list_empty(&sai->sai_entries_received);
176 static inline int agl_list_empty(struct ll_statahead_info *sai)
178 return cfs_list_empty(&sai->sai_entries_agl);
182 * (1) hit ratio less than 80%
184 * (2) consecutive miss more than 8
185 * then means low hit.
187 static inline int sa_low_hit(struct ll_statahead_info *sai)
189 return ((sai->sai_hit > 7 && sai->sai_hit < 4 * sai->sai_miss) ||
190 (sai->sai_consecutive_miss > 8));
194 * If the given index is behind of statahead window more than
195 * SA_OMITTED_ENTRY_MAX, then it is old.
197 static inline int is_omitted_entry(struct ll_statahead_info *sai, __u64 index)
199 return ((__u64)sai->sai_max + index + SA_OMITTED_ENTRY_MAX <
204 * Insert it into sai_entries_sent tail when init.
206 static struct ll_sa_entry *
207 ll_sa_entry_alloc(struct ll_statahead_info *sai, __u64 index,
208 const char *name, int len)
210 struct ll_inode_info *lli;
211 struct ll_sa_entry *entry;
216 entry_size = sizeof(struct ll_sa_entry) + (len & ~3) + 4;
217 OBD_ALLOC(entry, entry_size);
218 if (unlikely(entry == NULL))
219 RETURN(ERR_PTR(-ENOMEM));
221 CDEBUG(D_READA, "alloc sai entry %.*s(%p) index "LPU64"\n",
222 len, name, entry, index);
224 entry->se_index = index;
227 * Statahead entry reference rules:
229 * 1) When statahead entry is initialized, its reference is set as 2.
230 * One reference is used by the directory scanner. When the scanner
231 * searches the statahead cache for the given name, it can perform
232 * lockless hash lookup (only the scanner can remove entry from hash
233 * list), and once found, it needn't to call "atomic_inc()" for the
234 * entry reference. So the performance is improved. After using the
235 * statahead entry, the scanner will call "atomic_dec()" to drop the
236 * reference held when initialization. If it is the last reference,
237 * the statahead entry will be freed.
239 * 2) All other threads, including statahead thread and ptlrpcd thread,
240 * when they process the statahead entry, the reference for target
241 * should be held to guarantee the entry will not be released by the
242 * directory scanner. After processing the entry, these threads will
243 * drop the entry reference. If it is the last reference, the entry
246 * The second reference when initializes the statahead entry is used
247 * by the statahead thread, following the rule 2).
249 cfs_atomic_set(&entry->se_refcount, 2);
250 entry->se_stat = SA_ENTRY_INIT;
251 entry->se_size = entry_size;
252 dname = (char *)entry + sizeof(struct ll_sa_entry);
253 memcpy(dname, name, len);
255 entry->se_qstr.hash = full_name_hash(name, len);
256 entry->se_qstr.len = len;
257 entry->se_qstr.name = dname;
259 lli = ll_i2info(sai->sai_inode);
260 cfs_spin_lock(&lli->lli_sa_lock);
261 cfs_list_add_tail(&entry->se_list, &sai->sai_entries_sent);
262 cfs_spin_unlock(&lli->lli_sa_lock);
264 cfs_atomic_inc(&sai->sai_cache_count);
265 ll_sa_entry_enhash(sai, entry);
271 * Used by the directory scanner to search entry with name.
273 * Only the caller can remove the entry from hash, so it is unnecessary to hold
274 * hash lock. It is caller's duty to release the init refcount on the entry, so
275 * it is also unnecessary to increase refcount on the entry.
277 static struct ll_sa_entry *
278 ll_sa_entry_get_byname(struct ll_statahead_info *sai, const struct qstr *qstr)
280 struct ll_sa_entry *entry;
281 int i = ll_sa_entry_hash(qstr->hash);
283 cfs_list_for_each_entry(entry, &sai->sai_cache[i], se_hash) {
284 if (entry->se_qstr.hash == qstr->hash &&
285 entry->se_qstr.len == qstr->len &&
286 memcmp(entry->se_qstr.name, qstr->name, qstr->len) == 0)
293 * Used by the async getattr request callback to find entry with index.
295 * Inside lli_sa_lock to prevent others to change the list during the search.
296 * It needs to increase entry refcount before returning to guarantee that the
297 * entry cannot be freed by others.
299 static struct ll_sa_entry *
300 ll_sa_entry_get_byindex(struct ll_statahead_info *sai, __u64 index)
302 struct ll_sa_entry *entry;
304 cfs_list_for_each_entry(entry, &sai->sai_entries_sent, se_list) {
305 if (entry->se_index == index) {
306 cfs_atomic_inc(&entry->se_refcount);
309 if (entry->se_index > index)
315 static void ll_sa_entry_cleanup(struct ll_statahead_info *sai,
316 struct ll_sa_entry *entry)
318 struct md_enqueue_info *minfo = entry->se_minfo;
319 struct ptlrpc_request *req = entry->se_req;
322 entry->se_minfo = NULL;
323 ll_intent_release(&minfo->mi_it);
329 entry->se_req = NULL;
330 ptlrpc_req_finished(req);
334 static void ll_sa_entry_put(struct ll_statahead_info *sai,
335 struct ll_sa_entry *entry)
337 if (cfs_atomic_dec_and_test(&entry->se_refcount)) {
338 CDEBUG(D_READA, "free sai entry %.*s(%p) index "LPU64"\n",
339 entry->se_qstr.len, entry->se_qstr.name, entry,
342 LASSERT(ll_sa_entry_unhashed(entry));
343 LASSERT(ll_sa_entry_unlinked(entry));
345 ll_sa_entry_cleanup(sai, entry);
347 iput(entry->se_inode);
349 OBD_FREE(entry, entry->se_size);
350 cfs_atomic_dec(&sai->sai_cache_count);
355 do_sai_entry_fini(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
357 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
359 ll_sa_entry_unhash(sai, entry);
361 cfs_spin_lock(&lli->lli_sa_lock);
362 entry->se_stat = SA_ENTRY_DEST;
363 if (likely(!ll_sa_entry_unlinked(entry)))
364 cfs_list_del_init(&entry->se_list);
365 cfs_spin_unlock(&lli->lli_sa_lock);
367 ll_sa_entry_put(sai, entry);
371 * Delete it from sai_entries_stated list when fini.
374 ll_sa_entry_fini(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
376 struct ll_sa_entry *pos, *next;
379 do_sai_entry_fini(sai, entry);
381 /* drop old entry from sent list */
382 cfs_list_for_each_entry_safe(pos, next, &sai->sai_entries_sent,
384 if (is_omitted_entry(sai, pos->se_index))
385 do_sai_entry_fini(sai, pos);
390 /* drop old entry from stated list */
391 cfs_list_for_each_entry_safe(pos, next, &sai->sai_entries_stated,
393 if (is_omitted_entry(sai, pos->se_index))
394 do_sai_entry_fini(sai, pos);
401 * Inside lli_sa_lock.
404 do_sai_entry_to_stated(struct ll_statahead_info *sai,
405 struct ll_sa_entry *entry, int rc)
407 struct ll_sa_entry *se;
408 cfs_list_t *pos = &sai->sai_entries_stated;
410 if (!ll_sa_entry_unlinked(entry))
411 cfs_list_del_init(&entry->se_list);
413 cfs_list_for_each_entry_reverse(se, &sai->sai_entries_stated, se_list) {
414 if (se->se_index < entry->se_index) {
420 cfs_list_add(&entry->se_list, pos);
425 * Move entry to sai_entries_stated and sort with the index.
426 * \retval 1 -- entry to be destroyed.
427 * \retval 0 -- entry is inserted into stated list.
430 ll_sa_entry_to_stated(struct ll_statahead_info *sai,
431 struct ll_sa_entry *entry, int rc)
433 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
436 ll_sa_entry_cleanup(sai, entry);
438 cfs_spin_lock(&lli->lli_sa_lock);
439 if (likely(entry->se_stat != SA_ENTRY_DEST)) {
440 do_sai_entry_to_stated(sai, entry, rc);
443 cfs_spin_unlock(&lli->lli_sa_lock);
449 * Insert inode into the list of sai_entries_agl.
451 static void ll_agl_add(struct ll_statahead_info *sai,
452 struct inode *inode, int index)
454 struct ll_inode_info *child = ll_i2info(inode);
455 struct ll_inode_info *parent = ll_i2info(sai->sai_inode);
458 cfs_spin_lock(&child->lli_agl_lock);
459 if (child->lli_agl_index == 0) {
460 child->lli_agl_index = index;
461 cfs_spin_unlock(&child->lli_agl_lock);
463 LASSERT(cfs_list_empty(&child->lli_agl_list));
466 cfs_spin_lock(&parent->lli_agl_lock);
467 if (agl_list_empty(sai))
469 cfs_list_add_tail(&child->lli_agl_list, &sai->sai_entries_agl);
470 cfs_spin_unlock(&parent->lli_agl_lock);
472 cfs_spin_unlock(&child->lli_agl_lock);
476 cfs_waitq_signal(&sai->sai_agl_thread.t_ctl_waitq);
479 static struct ll_statahead_info *ll_sai_alloc(void)
481 struct ll_statahead_info *sai;
489 cfs_atomic_set(&sai->sai_refcount, 1);
491 cfs_spin_lock(&sai_generation_lock);
492 sai->sai_generation = ++sai_generation;
493 if (unlikely(sai_generation == 0))
494 sai->sai_generation = ++sai_generation;
495 cfs_spin_unlock(&sai_generation_lock);
497 sai->sai_max = LL_SA_RPC_MIN;
499 cfs_waitq_init(&sai->sai_waitq);
500 cfs_waitq_init(&sai->sai_thread.t_ctl_waitq);
501 cfs_waitq_init(&sai->sai_agl_thread.t_ctl_waitq);
503 CFS_INIT_LIST_HEAD(&sai->sai_entries_sent);
504 CFS_INIT_LIST_HEAD(&sai->sai_entries_received);
505 CFS_INIT_LIST_HEAD(&sai->sai_entries_stated);
506 CFS_INIT_LIST_HEAD(&sai->sai_entries_agl);
508 for (i = 0; i < LL_SA_CACHE_SIZE; i++) {
509 CFS_INIT_LIST_HEAD(&sai->sai_cache[i]);
510 cfs_spin_lock_init(&sai->sai_cache_lock[i]);
512 cfs_atomic_set(&sai->sai_cache_count, 0);
517 static inline struct ll_statahead_info *
518 ll_sai_get(struct ll_statahead_info *sai)
520 cfs_atomic_inc(&sai->sai_refcount);
524 static void ll_sai_put(struct ll_statahead_info *sai)
526 struct inode *inode = sai->sai_inode;
527 struct ll_inode_info *lli = ll_i2info(inode);
530 if (cfs_atomic_dec_and_lock(&sai->sai_refcount, &lli->lli_sa_lock)) {
531 struct ll_sa_entry *entry, *next;
533 if (unlikely(cfs_atomic_read(&sai->sai_refcount) > 0)) {
534 /* It is race case, the interpret callback just hold
535 * a reference count */
536 cfs_spin_unlock(&lli->lli_sa_lock);
540 LASSERT(lli->lli_opendir_key == NULL);
541 LASSERT(thread_is_stopped(&sai->sai_thread));
542 LASSERT(thread_is_stopped(&sai->sai_agl_thread));
545 lli->lli_opendir_pid = 0;
546 cfs_spin_unlock(&lli->lli_sa_lock);
548 if (sai->sai_sent > sai->sai_replied)
549 CDEBUG(D_READA,"statahead for dir "DFID" does not "
550 "finish: [sent:"LPU64"] [replied:"LPU64"]\n",
552 sai->sai_sent, sai->sai_replied);
554 cfs_list_for_each_entry_safe(entry, next,
555 &sai->sai_entries_sent, se_list)
556 do_sai_entry_fini(sai, entry);
558 LASSERT(sa_received_empty(sai));
560 cfs_list_for_each_entry_safe(entry, next,
561 &sai->sai_entries_stated, se_list)
562 do_sai_entry_fini(sai, entry);
564 LASSERT(cfs_atomic_read(&sai->sai_cache_count) == 0);
565 LASSERT(agl_list_empty(sai));
574 #ifndef HAVE_AGL_SUPPORT
575 # define cl_agl(inode) do {} while (0)
578 /* Do NOT forget to drop inode refcount when into sai_entries_agl. */
579 static void ll_agl_trigger(struct inode *inode, struct ll_statahead_info *sai)
581 struct ll_inode_info *lli = ll_i2info(inode);
582 __u64 index = lli->lli_agl_index;
586 LASSERT(cfs_list_empty(&lli->lli_agl_list));
588 /* AGL maybe fall behind statahead with one entry */
589 if (is_omitted_entry(sai, index + 1)) {
590 lli->lli_agl_index = 0;
595 /* Someone is in glimpse (sync or async), do nothing. */
596 rc = cfs_down_write_trylock(&lli->lli_glimpse_sem);
598 lli->lli_agl_index = 0;
604 * Someone triggered glimpse within 1 sec before.
605 * 1) The former glimpse succeeded with glimpse lock granted by OST, and
606 * if the lock is still cached on client, AGL needs to do nothing. If
607 * it is cancelled by other client, AGL maybe cannot obtaion new lock
608 * for no glimpse callback triggered by AGL.
609 * 2) The former glimpse succeeded, but OST did not grant glimpse lock.
610 * Under such case, it is quite possible that the OST will not grant
611 * glimpse lock for AGL also.
612 * 3) The former glimpse failed, compared with other two cases, it is
613 * relative rare. AGL can ignore such case, and it will not muchly
614 * affect the performance.
616 if (lli->lli_glimpse_time != 0 &&
617 cfs_time_before(cfs_time_shift(-1), lli->lli_glimpse_time)) {
618 cfs_up_write(&lli->lli_glimpse_sem);
619 lli->lli_agl_index = 0;
624 CDEBUG(D_READA, "Handling (init) async glimpse: inode = "
625 DFID", idx = "LPU64"\n", PFID(&lli->lli_fid), index);
628 lli->lli_agl_index = 0;
629 lli->lli_glimpse_time = cfs_time_current();
630 cfs_up_write(&lli->lli_glimpse_sem);
632 CDEBUG(D_READA, "Handled (init) async glimpse: inode= "
633 DFID", idx = "LPU64", rc = %d\n",
634 PFID(&lli->lli_fid), index, rc);
641 static void do_statahead_interpret(struct ll_statahead_info *sai,
642 struct ll_sa_entry *target)
644 struct inode *dir = sai->sai_inode;
646 struct ll_inode_info *lli = ll_i2info(dir);
647 struct ll_sa_entry *entry;
648 struct md_enqueue_info *minfo;
649 struct lookup_intent *it;
650 struct ptlrpc_request *req;
651 struct mdt_body *body;
655 cfs_spin_lock(&lli->lli_sa_lock);
656 if (target != NULL && target->se_req != NULL &&
657 !cfs_list_empty(&target->se_list)) {
659 } else if (unlikely(sa_received_empty(sai))) {
660 cfs_spin_unlock(&lli->lli_sa_lock);
663 entry = sa_first_received_entry(sai);
666 cfs_atomic_inc(&entry->se_refcount);
667 cfs_list_del_init(&entry->se_list);
668 cfs_spin_unlock(&lli->lli_sa_lock);
670 LASSERT(entry->se_handle != 0);
672 minfo = entry->se_minfo;
675 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
677 GOTO(out, rc = -EFAULT);
679 child = entry->se_inode;
684 LASSERT(fid_is_zero(&minfo->mi_data.op_fid2));
686 /* XXX: No fid in reply, this is probaly cross-ref case.
687 * SA can't handle it yet. */
688 if (body->valid & OBD_MD_MDS)
689 GOTO(out, rc = -EAGAIN);
694 /* unlinked and re-created with the same name */
695 if (unlikely(!lu_fid_eq(&minfo->mi_data.op_fid2, &body->fid1))){
696 entry->se_inode = NULL;
702 it->d.lustre.it_lock_handle = entry->se_handle;
703 rc = md_revalidate_lock(ll_i2mdexp(dir), it, NULL, NULL);
705 GOTO(out, rc = -EAGAIN);
707 rc = ll_prep_inode(&child, req, dir->i_sb);
711 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
712 child, child->i_ino, child->i_generation);
713 ll_set_lock_data(ll_i2sbi(dir)->ll_md_exp, child, it, NULL);
715 entry->se_inode = child;
717 if (agl_should_run(sai, child))
718 ll_agl_add(sai, child, entry->se_index);
723 /* The "ll_sa_entry_to_stated()" will drop related ldlm ibits lock
724 * reference count by calling "ll_intent_drop_lock()" in spite of the
725 * above operations failed or not. Do not worry about calling
726 * "ll_intent_drop_lock()" more than once. */
727 rc = ll_sa_entry_to_stated(sai, entry, rc < 0 ? rc : SA_ENTRY_SUCC);
728 if (rc == 0 && entry->se_index == sai->sai_index_wait && target == NULL)
729 cfs_waitq_signal(&sai->sai_waitq);
730 ll_sa_entry_put(sai, entry);
733 static int ll_statahead_interpret(struct ptlrpc_request *req,
734 struct md_enqueue_info *minfo, int rc)
736 struct lookup_intent *it = &minfo->mi_it;
737 struct inode *dir = minfo->mi_dir;
738 struct ll_inode_info *lli = ll_i2info(dir);
739 struct ll_statahead_info *sai = NULL;
740 struct ll_sa_entry *entry;
744 if (it_disposition(it, DISP_LOOKUP_NEG))
747 cfs_spin_lock(&lli->lli_sa_lock);
749 if (unlikely(lli->lli_sai == NULL ||
750 lli->lli_sai->sai_generation != minfo->mi_generation)) {
751 cfs_spin_unlock(&lli->lli_sa_lock);
752 GOTO(out, rc = -ESTALE);
754 sai = ll_sai_get(lli->lli_sai);
755 if (unlikely(!thread_is_running(&sai->sai_thread))) {
757 cfs_spin_unlock(&lli->lli_sa_lock);
758 GOTO(out, rc = -EBADFD);
761 entry = ll_sa_entry_get_byindex(sai, minfo->mi_cbdata);
764 cfs_spin_unlock(&lli->lli_sa_lock);
765 GOTO(out, rc = -EIDRM);
768 cfs_list_del_init(&entry->se_list);
771 do_sai_entry_to_stated(sai, entry, rc);
772 cfs_spin_unlock(&lli->lli_sa_lock);
773 if (entry->se_index == sai->sai_index_wait)
774 cfs_waitq_signal(&sai->sai_waitq);
776 entry->se_minfo = minfo;
777 entry->se_req = ptlrpc_request_addref(req);
778 /* Release the async ibits lock ASAP to avoid deadlock
779 * when statahead thread tries to enqueue lock on parent
780 * for readpage and other tries to enqueue lock on child
781 * with parent's lock held, for example: unlink. */
782 entry->se_handle = it->d.lustre.it_lock_handle;
783 ll_intent_drop_lock(it);
784 wakeup = sa_received_empty(sai);
785 cfs_list_add_tail(&entry->se_list,
786 &sai->sai_entries_received);
788 cfs_spin_unlock(&lli->lli_sa_lock);
790 cfs_waitq_signal(&sai->sai_thread.t_ctl_waitq);
792 ll_sa_entry_put(sai, entry);
799 ll_intent_release(it);
808 static void sa_args_fini(struct md_enqueue_info *minfo,
809 struct ldlm_enqueue_info *einfo)
811 LASSERT(minfo && einfo);
813 capa_put(minfo->mi_data.op_capa1);
814 capa_put(minfo->mi_data.op_capa2);
820 * There is race condition between "capa_put" and "ll_statahead_interpret" for
821 * accessing "op_data.op_capa[1,2]" as following:
822 * "capa_put" releases "op_data.op_capa[1,2]"'s reference count after calling
823 * "md_intent_getattr_async". But "ll_statahead_interpret" maybe run first, and
824 * fill "op_data.op_capa[1,2]" as POISON, then cause "capa_put" access invalid
825 * "ocapa". So here reserve "op_data.op_capa[1,2]" in "pcapa" before calling
826 * "md_intent_getattr_async".
828 static int sa_args_init(struct inode *dir, struct inode *child,
829 struct ll_sa_entry *entry, struct md_enqueue_info **pmi,
830 struct ldlm_enqueue_info **pei,
831 struct obd_capa **pcapa)
833 struct qstr *qstr = &entry->se_qstr;
834 struct ll_inode_info *lli = ll_i2info(dir);
835 struct md_enqueue_info *minfo;
836 struct ldlm_enqueue_info *einfo;
837 struct md_op_data *op_data;
839 OBD_ALLOC_PTR(einfo);
843 OBD_ALLOC_PTR(minfo);
849 op_data = ll_prep_md_op_data(&minfo->mi_data, dir, child, qstr->name,
850 qstr->len, 0, LUSTRE_OPC_ANY, NULL);
851 if (IS_ERR(op_data)) {
854 return PTR_ERR(op_data);
857 minfo->mi_it.it_op = IT_GETATTR;
858 minfo->mi_dir = igrab(dir);
859 minfo->mi_cb = ll_statahead_interpret;
860 minfo->mi_generation = lli->lli_sai->sai_generation;
861 minfo->mi_cbdata = entry->se_index;
863 einfo->ei_type = LDLM_IBITS;
864 einfo->ei_mode = it_to_lock_mode(&minfo->mi_it);
865 einfo->ei_cb_bl = ll_md_blocking_ast;
866 einfo->ei_cb_cp = ldlm_completion_ast;
867 einfo->ei_cb_gl = NULL;
868 einfo->ei_cbdata = NULL;
872 pcapa[0] = op_data->op_capa1;
873 pcapa[1] = op_data->op_capa2;
878 static int do_sa_lookup(struct inode *dir, struct ll_sa_entry *entry)
880 struct md_enqueue_info *minfo;
881 struct ldlm_enqueue_info *einfo;
882 struct obd_capa *capas[2];
886 rc = sa_args_init(dir, NULL, entry, &minfo, &einfo, capas);
890 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
895 sa_args_fini(minfo, einfo);
902 * similar to ll_revalidate_it().
903 * \retval 1 -- dentry valid
904 * \retval 0 -- will send stat-ahead request
905 * \retval others -- prepare stat-ahead request failed
907 static int do_sa_revalidate(struct inode *dir, struct ll_sa_entry *entry,
908 struct dentry *dentry)
910 struct inode *inode = dentry->d_inode;
911 struct lookup_intent it = { .it_op = IT_GETATTR,
912 .d.lustre.it_lock_handle = 0 };
913 struct md_enqueue_info *minfo;
914 struct ldlm_enqueue_info *einfo;
915 struct obd_capa *capas[2];
919 if (unlikely(inode == NULL))
922 if (d_mountpoint(dentry))
925 if (unlikely(dentry == dentry->d_sb->s_root))
928 entry->se_inode = igrab(inode);
929 rc = md_revalidate_lock(ll_i2mdexp(dir), &it, ll_inode2fid(inode),NULL);
931 entry->se_handle = it.d.lustre.it_lock_handle;
932 ll_intent_release(&it);
936 rc = sa_args_init(dir, inode, entry, &minfo, &einfo, capas);
938 entry->se_inode = NULL;
943 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
948 entry->se_inode = NULL;
950 sa_args_fini(minfo, einfo);
956 static void ll_statahead_one(struct dentry *parent, const char* entry_name,
959 struct inode *dir = parent->d_inode;
960 struct ll_inode_info *lli = ll_i2info(dir);
961 struct ll_statahead_info *sai = lli->lli_sai;
962 struct dentry *dentry = NULL;
963 struct ll_sa_entry *entry;
968 entry = ll_sa_entry_alloc(sai, sai->sai_index, entry_name,
973 dentry = d_lookup(parent, &entry->se_qstr);
975 rc = do_sa_lookup(dir, entry);
977 rc = do_sa_revalidate(dir, entry, dentry);
978 if (rc == 1 && agl_should_run(sai, dentry->d_inode))
979 ll_agl_add(sai, dentry->d_inode, entry->se_index);
986 rc1 = ll_sa_entry_to_stated(sai, entry,
987 rc < 0 ? SA_ENTRY_INVA : SA_ENTRY_SUCC);
988 if (rc1 == 0 && entry->se_index == sai->sai_index_wait)
989 cfs_waitq_signal(&sai->sai_waitq);
995 /* drop one refcount on entry by ll_sa_entry_alloc */
996 ll_sa_entry_put(sai, entry);
1001 static int ll_agl_thread(void *arg)
1003 struct dentry *parent = (struct dentry *)arg;
1004 struct inode *dir = parent->d_inode;
1005 struct ll_inode_info *plli = ll_i2info(dir);
1006 struct ll_inode_info *clli;
1007 struct ll_sb_info *sbi = ll_i2sbi(dir);
1008 struct ll_statahead_info *sai = ll_sai_get(plli->lli_sai);
1009 struct ptlrpc_thread *thread = &sai->sai_agl_thread;
1010 struct l_wait_info lwi = { 0 };
1015 snprintf(pname, 15, "ll_agl_%u", plli->lli_opendir_pid);
1016 cfs_daemonize(pname);
1019 CDEBUG(D_READA, "agl thread started: [pid %d] [parent %.*s]\n",
1020 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1022 atomic_inc(&sbi->ll_agl_total);
1023 cfs_spin_lock(&plli->lli_agl_lock);
1024 sai->sai_agl_valid = 1;
1025 thread_set_flags(thread, SVC_RUNNING);
1026 cfs_spin_unlock(&plli->lli_agl_lock);
1027 cfs_waitq_signal(&thread->t_ctl_waitq);
1030 l_wait_event(thread->t_ctl_waitq,
1031 !agl_list_empty(sai) ||
1032 !thread_is_running(thread),
1035 if (!thread_is_running(thread))
1038 cfs_spin_lock(&plli->lli_agl_lock);
1039 /* The statahead thread maybe help to process AGL entries,
1040 * so check whether list empty again. */
1041 if (!agl_list_empty(sai)) {
1042 clli = agl_first_entry(sai);
1043 cfs_list_del_init(&clli->lli_agl_list);
1044 cfs_spin_unlock(&plli->lli_agl_lock);
1045 ll_agl_trigger(&clli->lli_vfs_inode, sai);
1047 cfs_spin_unlock(&plli->lli_agl_lock);
1051 cfs_spin_lock(&plli->lli_agl_lock);
1052 sai->sai_agl_valid = 0;
1053 while (!agl_list_empty(sai)) {
1054 clli = agl_first_entry(sai);
1055 cfs_list_del_init(&clli->lli_agl_list);
1056 cfs_spin_unlock(&plli->lli_agl_lock);
1057 clli->lli_agl_index = 0;
1058 iput(&clli->lli_vfs_inode);
1059 cfs_spin_lock(&plli->lli_agl_lock);
1061 thread_set_flags(thread, SVC_STOPPED);
1062 cfs_spin_unlock(&plli->lli_agl_lock);
1063 cfs_waitq_signal(&thread->t_ctl_waitq);
1065 CDEBUG(D_READA, "agl thread stopped: [pid %d] [parent %.*s]\n",
1066 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1070 static void ll_start_agl(struct dentry *parent, struct ll_statahead_info *sai)
1072 struct ptlrpc_thread *thread = &sai->sai_agl_thread;
1073 struct l_wait_info lwi = { 0 };
1077 CDEBUG(D_READA, "start agl thread: [pid %d] [parent %.*s]\n",
1078 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1080 rc = cfs_create_thread(ll_agl_thread, parent, 0);
1082 CERROR("can't start ll_agl thread, rc: %d\n", rc);
1086 l_wait_event(thread->t_ctl_waitq,
1087 thread_is_running(thread) || thread_is_stopped(thread),
1092 static int ll_statahead_thread(void *arg)
1094 struct dentry *parent = (struct dentry *)arg;
1095 struct inode *dir = parent->d_inode;
1096 struct ll_inode_info *plli = ll_i2info(dir);
1097 struct ll_inode_info *clli;
1098 struct ll_sb_info *sbi = ll_i2sbi(dir);
1099 struct ll_statahead_info *sai = ll_sai_get(plli->lli_sai);
1100 struct ptlrpc_thread *thread = &sai->sai_thread;
1105 struct ll_dir_chain chain;
1106 struct l_wait_info lwi = { 0 };
1111 snprintf(pname, 15, "ll_sa_%u", plli->lli_opendir_pid);
1112 cfs_daemonize(pname);
1115 CDEBUG(D_READA, "statahead thread started: [pid %d] [parent %.*s]\n",
1116 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1118 if (sbi->ll_flags & LL_SBI_AGL_ENABLED)
1119 ll_start_agl(parent, sai);
1121 atomic_inc(&sbi->ll_sa_total);
1122 cfs_spin_lock(&plli->lli_sa_lock);
1123 thread_set_flags(thread, SVC_RUNNING);
1124 cfs_spin_unlock(&plli->lli_sa_lock);
1125 cfs_waitq_signal(&thread->t_ctl_waitq);
1127 plli->lli_sa_pos = 0;
1128 ll_dir_chain_init(&chain);
1129 page = ll_get_dir_page(NULL, dir, pos, &chain);
1132 struct lu_dirpage *dp;
1133 struct lu_dirent *ent;
1137 CDEBUG(D_READA, "error reading dir "DFID" at "LPU64
1138 "/"LPU64": [rc %d] [parent %u]\n",
1139 PFID(ll_inode2fid(dir)), pos, sai->sai_index,
1140 rc, plli->lli_opendir_pid);
1144 dp = page_address(page);
1145 for (ent = lu_dirent_start(dp); ent != NULL;
1146 ent = lu_dirent_next(ent)) {
1151 hash = le64_to_cpu(ent->lde_hash);
1152 if (unlikely(hash < pos))
1154 * Skip until we find target hash value.
1158 namelen = le16_to_cpu(ent->lde_namelen);
1159 if (unlikely(namelen == 0))
1161 * Skip dummy record.
1165 name = ent->lde_name;
1166 if (name[0] == '.') {
1172 } else if (name[1] == '.' && namelen == 2) {
1177 } else if (!sai->sai_ls_all) {
1179 * skip hidden files.
1181 sai->sai_skip_hidden++;
1187 * don't stat-ahead first entry.
1189 if (unlikely(++first == 1))
1193 l_wait_event(thread->t_ctl_waitq,
1194 !sa_sent_full(sai) ||
1195 !sa_received_empty(sai) ||
1196 !agl_list_empty(sai) ||
1197 !thread_is_running(thread),
1201 while (!sa_received_empty(sai))
1202 do_statahead_interpret(sai, NULL);
1204 if (unlikely(!thread_is_running(thread))) {
1205 ll_release_page(page, 0);
1209 /* If no window for metadata statahead, but there are
1210 * some AGL entries to be triggered, then try to help
1211 * to process the AGL entries. */
1212 if (sa_sent_full(sai)) {
1213 cfs_spin_lock(&plli->lli_agl_lock);
1214 while (!agl_list_empty(sai)) {
1215 clli = agl_first_entry(sai);
1216 cfs_list_del_init(&clli->lli_agl_list);
1217 cfs_spin_unlock(&plli->lli_agl_lock);
1218 ll_agl_trigger(&clli->lli_vfs_inode,
1221 if (!sa_received_empty(sai))
1225 !thread_is_running(thread))) {
1226 ll_release_page(page, 0);
1230 if (!sa_sent_full(sai))
1233 cfs_spin_lock(&plli->lli_agl_lock);
1235 cfs_spin_unlock(&plli->lli_agl_lock);
1241 ll_statahead_one(parent, name, namelen);
1243 pos = le64_to_cpu(dp->ldp_hash_end);
1244 if (pos == MDS_DIR_END_OFF) {
1246 * End of directory reached.
1248 ll_release_page(page, 0);
1250 l_wait_event(thread->t_ctl_waitq,
1251 !sa_received_empty(sai) ||
1252 sai->sai_sent == sai->sai_replied||
1253 !thread_is_running(thread),
1256 while (!sa_received_empty(sai))
1257 do_statahead_interpret(sai, NULL);
1259 if (unlikely(!thread_is_running(thread)))
1262 if (sai->sai_sent == sai->sai_replied &&
1263 sa_received_empty(sai))
1267 cfs_spin_lock(&plli->lli_agl_lock);
1268 while (!agl_list_empty(sai) &&
1269 thread_is_running(thread)) {
1270 clli = agl_first_entry(sai);
1271 cfs_list_del_init(&clli->lli_agl_list);
1272 cfs_spin_unlock(&plli->lli_agl_lock);
1273 ll_agl_trigger(&clli->lli_vfs_inode, sai);
1274 cfs_spin_lock(&plli->lli_agl_lock);
1276 cfs_spin_unlock(&plli->lli_agl_lock);
1281 * chain is exhausted.
1282 * Normal case: continue to the next page.
1284 ll_release_page(page, le32_to_cpu(dp->ldp_flags) &
1286 plli->lli_sa_pos = pos;
1287 sai->sai_in_readpage = 1;
1288 page = ll_get_dir_page(NULL, dir, pos, &chain);
1289 sai->sai_in_readpage = 0;
1291 LASSERT(le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1292 ll_release_page(page, 1);
1294 * go into overflow page.
1301 if (sai->sai_agl_valid) {
1302 struct ptlrpc_thread *agl_thread = &sai->sai_agl_thread;
1304 cfs_spin_lock(&plli->lli_agl_lock);
1305 thread_set_flags(agl_thread, SVC_STOPPING);
1306 cfs_spin_unlock(&plli->lli_agl_lock);
1307 cfs_waitq_signal(&agl_thread->t_ctl_waitq);
1309 CDEBUG(D_READA, "stop agl thread: [pid %d]\n",
1311 l_wait_event(agl_thread->t_ctl_waitq,
1312 thread_is_stopped(agl_thread),
1316 ll_dir_chain_fini(&chain);
1317 cfs_spin_lock(&plli->lli_sa_lock);
1318 if (!sa_received_empty(sai)) {
1319 thread_set_flags(thread, SVC_STOPPING);
1320 cfs_spin_unlock(&plli->lli_sa_lock);
1322 /* To release the resources held by received entries. */
1323 while (!sa_received_empty(sai))
1324 do_statahead_interpret(sai, NULL);
1326 cfs_spin_lock(&plli->lli_sa_lock);
1328 thread_set_flags(thread, SVC_STOPPED);
1329 cfs_spin_unlock(&plli->lli_sa_lock);
1330 cfs_waitq_signal(&sai->sai_waitq);
1331 cfs_waitq_signal(&thread->t_ctl_waitq);
1334 CDEBUG(D_READA, "statahead thread stopped: [pid %d] [parent %.*s]\n",
1335 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1340 * called in ll_file_release().
1342 void ll_stop_statahead(struct inode *dir, void *key)
1344 struct ll_inode_info *lli = ll_i2info(dir);
1346 if (unlikely(key == NULL))
1349 cfs_spin_lock(&lli->lli_sa_lock);
1350 if (lli->lli_opendir_key != key || lli->lli_opendir_pid == 0) {
1351 cfs_spin_unlock(&lli->lli_sa_lock);
1355 lli->lli_opendir_key = NULL;
1358 struct l_wait_info lwi = { 0 };
1359 struct ptlrpc_thread *thread = &lli->lli_sai->sai_thread;
1361 if (!thread_is_stopped(thread)) {
1362 thread_set_flags(thread, SVC_STOPPING);
1363 cfs_spin_unlock(&lli->lli_sa_lock);
1364 cfs_waitq_signal(&thread->t_ctl_waitq);
1366 CDEBUG(D_READA, "stop statahead thread: [pid %d]\n",
1368 l_wait_event(thread->t_ctl_waitq,
1369 thread_is_stopped(thread),
1372 cfs_spin_unlock(&lli->lli_sa_lock);
1376 * Put the ref which was held when first statahead_enter.
1377 * It maybe not the last ref for some statahead requests
1380 ll_sai_put(lli->lli_sai);
1382 lli->lli_opendir_pid = 0;
1383 cfs_spin_unlock(&lli->lli_sa_lock);
1389 * not first dirent, or is "."
1391 LS_NONE_FIRST_DE = 0,
1393 * the first non-hidden dirent
1397 * the first hidden dirent, that is "."
1402 static int is_first_dirent(struct inode *dir, struct dentry *dentry)
1404 struct ll_inode_info *lli = ll_i2info(dir);
1405 struct ll_dir_chain chain;
1406 struct qstr *target = &dentry->d_name;
1410 int rc = LS_NONE_FIRST_DE;
1413 lli->lli_sa_pos = 0;
1414 ll_dir_chain_init(&chain);
1415 page = ll_get_dir_page(NULL, dir, pos, &chain);
1418 struct lu_dirpage *dp;
1419 struct lu_dirent *ent;
1422 struct ll_inode_info *lli = ll_i2info(dir);
1425 CERROR("error reading dir "DFID" at "LPU64": "
1426 "[rc %d] [parent %u]\n",
1427 PFID(ll_inode2fid(dir)), pos,
1428 rc, lli->lli_opendir_pid);
1432 dp = page_address(page);
1433 for (ent = lu_dirent_start(dp); ent != NULL;
1434 ent = lu_dirent_next(ent)) {
1439 hash = le64_to_cpu(ent->lde_hash);
1440 /* The ll_get_dir_page() can return any page containing
1441 * the given hash which may be not the start hash. */
1442 if (unlikely(hash < pos))
1445 namelen = le16_to_cpu(ent->lde_namelen);
1446 if (unlikely(namelen == 0))
1448 * skip dummy record.
1452 name = ent->lde_name;
1453 if (name[0] == '.') {
1459 else if (name[1] == '.' && namelen == 2)
1470 if (dot_de && target->name[0] != '.') {
1471 CDEBUG(D_READA, "%.*s skip hidden file %.*s\n",
1472 target->len, target->name,
1477 if (target->len != namelen ||
1478 memcmp(target->name, name, namelen) != 0)
1479 rc = LS_NONE_FIRST_DE;
1483 rc = LS_FIRST_DOT_DE;
1485 ll_release_page(page, 0);
1488 pos = le64_to_cpu(dp->ldp_hash_end);
1489 if (pos == MDS_DIR_END_OFF) {
1491 * End of directory reached.
1493 ll_release_page(page, 0);
1497 * chain is exhausted
1498 * Normal case: continue to the next page.
1500 ll_release_page(page, le32_to_cpu(dp->ldp_flags) &
1502 lli->lli_sa_pos = pos;
1503 page = ll_get_dir_page(NULL, dir, pos, &chain);
1506 * go into overflow page.
1508 LASSERT(le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1509 ll_release_page(page, 1);
1515 ll_dir_chain_fini(&chain);
1520 ll_sai_unplug(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
1522 struct ptlrpc_thread *thread = &sai->sai_thread;
1523 struct ll_sb_info *sbi = ll_i2sbi(sai->sai_inode);
1527 if (entry != NULL && entry->se_stat == SA_ENTRY_SUCC)
1532 ll_sa_entry_fini(sai, entry);
1535 sai->sai_consecutive_miss = 0;
1536 sai->sai_max = min(2 * sai->sai_max, sbi->ll_sa_max);
1538 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
1541 sai->sai_consecutive_miss++;
1542 if (sa_low_hit(sai) && thread_is_running(thread)) {
1543 atomic_inc(&sbi->ll_sa_wrong);
1544 CDEBUG(D_READA, "Statahead for dir "DFID" hit "
1545 "ratio too low: hit/miss "LPU64"/"LPU64
1546 ", sent/replied "LPU64"/"LPU64", stopping "
1547 "statahead thread: pid %d\n",
1548 PFID(&lli->lli_fid), sai->sai_hit,
1549 sai->sai_miss, sai->sai_sent,
1550 sai->sai_replied, cfs_curproc_pid());
1551 cfs_spin_lock(&lli->lli_sa_lock);
1552 if (!thread_is_stopped(thread))
1553 thread_set_flags(thread, SVC_STOPPING);
1554 cfs_spin_unlock(&lli->lli_sa_lock);
1558 if (!thread_is_stopped(thread))
1559 cfs_waitq_signal(&thread->t_ctl_waitq);
1565 * Start statahead thread if this is the first dir entry.
1566 * Otherwise if a thread is started already, wait it until it is ahead of me.
1567 * \retval 1 -- find entry with lock in cache, the caller needs to do
1569 * \retval 0 -- find entry in cache, but without lock, the caller needs
1571 * \retval others -- the caller need to process as non-statahead.
1573 int do_statahead_enter(struct inode *dir, struct dentry **dentryp,
1576 struct ll_inode_info *lli = ll_i2info(dir);
1577 struct ll_statahead_info *sai = lli->lli_sai;
1578 struct dentry *parent;
1579 struct ll_sa_entry *entry;
1580 struct ptlrpc_thread *thread;
1581 struct l_wait_info lwi = { 0 };
1585 LASSERT(lli->lli_opendir_pid == cfs_curproc_pid());
1588 thread = &sai->sai_thread;
1589 if (unlikely(thread_is_stopped(thread) &&
1590 cfs_list_empty(&sai->sai_entries_stated))) {
1591 /* to release resource */
1592 ll_stop_statahead(dir, lli->lli_opendir_key);
1596 if ((*dentryp)->d_name.name[0] == '.') {
1597 if (sai->sai_ls_all ||
1598 sai->sai_miss_hidden >= sai->sai_skip_hidden) {
1600 * Hidden dentry is the first one, or statahead
1601 * thread does not skip so many hidden dentries
1602 * before "sai_ls_all" enabled as below.
1605 if (!sai->sai_ls_all)
1607 * It maybe because hidden dentry is not
1608 * the first one, "sai_ls_all" was not
1609 * set, then "ls -al" missed. Enable
1610 * "sai_ls_all" for such case.
1612 sai->sai_ls_all = 1;
1615 * Such "getattr" has been skipped before
1616 * "sai_ls_all" enabled as above.
1618 sai->sai_miss_hidden++;
1623 entry = ll_sa_entry_get_byname(sai, &(*dentryp)->d_name);
1624 if (entry == NULL || only_unplug) {
1625 ll_sai_unplug(sai, entry);
1626 RETURN(entry ? 1 : -EAGAIN);
1629 while (!ll_sa_entry_stated(entry) &&
1630 sai->sai_in_readpage &&
1631 !sa_received_empty(sai))
1632 do_statahead_interpret(sai, entry);
1634 if (!ll_sa_entry_stated(entry)) {
1635 sai->sai_index_wait = entry->se_index;
1636 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(30), NULL,
1637 LWI_ON_SIGNAL_NOOP, NULL);
1638 rc = l_wait_event(sai->sai_waitq,
1639 ll_sa_entry_stated(entry) ||
1640 thread_is_stopped(thread),
1643 ll_sai_unplug(sai, entry);
1648 if (entry->se_stat == SA_ENTRY_SUCC &&
1649 entry->se_inode != NULL) {
1650 struct inode *inode = entry->se_inode;
1651 struct lookup_intent it = { .it_op = IT_GETATTR,
1652 .d.lustre.it_lock_handle =
1654 struct ll_dentry_data *lld;
1657 rc = md_revalidate_lock(ll_i2mdexp(dir), &it,
1658 ll_inode2fid(inode), &bits);
1660 if ((*dentryp)->d_inode == NULL) {
1661 *dentryp = ll_find_alias(inode,
1663 lld = ll_d2d(*dentryp);
1664 if (unlikely(lld == NULL))
1665 ll_dops_init(*dentryp, 1, 1);
1667 LASSERT((*dentryp)->d_inode == inode);
1669 ll_dentry_rehash(*dentryp, 0);
1672 entry->se_inode = NULL;
1674 ll_dentry_reset_flags(*dentryp, bits);
1675 ll_intent_release(&it);
1679 ll_sai_unplug(sai, entry);
1683 /* I am the "lli_opendir_pid" owner, only me can set "lli_sai". */
1684 rc = is_first_dirent(dir, *dentryp);
1685 if (rc == LS_NONE_FIRST_DE)
1686 /* It is not "ls -{a}l" operation, no need statahead for it. */
1687 GOTO(out, rc = -EAGAIN);
1689 sai = ll_sai_alloc();
1691 GOTO(out, rc = -ENOMEM);
1693 sai->sai_ls_all = (rc == LS_FIRST_DOT_DE);
1694 sai->sai_inode = igrab(dir);
1695 if (unlikely(sai->sai_inode == NULL)) {
1696 CWARN("Do not start stat ahead on dying inode "DFID"\n",
1697 PFID(&lli->lli_fid));
1698 GOTO(out, rc = -ESTALE);
1701 /* get parent reference count here, and put it in ll_statahead_thread */
1702 parent = dget((*dentryp)->d_parent);
1703 if (unlikely(sai->sai_inode != parent->d_inode)) {
1704 struct ll_inode_info *nlli = ll_i2info(parent->d_inode);
1706 CWARN("Race condition, someone changed %.*s just now: "
1707 "old parent "DFID", new parent "DFID"\n",
1708 (*dentryp)->d_name.len, (*dentryp)->d_name.name,
1709 PFID(&lli->lli_fid), PFID(&nlli->lli_fid));
1711 iput(sai->sai_inode);
1712 GOTO(out, rc = -EAGAIN);
1715 CDEBUG(D_READA, "start statahead thread: [pid %d] [parent %.*s]\n",
1716 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1719 rc = cfs_create_thread(ll_statahead_thread, parent, 0);
1720 thread = &sai->sai_thread;
1722 CERROR("can't start ll_sa thread, rc: %d\n", rc);
1724 lli->lli_opendir_key = NULL;
1725 thread_set_flags(thread, SVC_STOPPED);
1727 LASSERT(lli->lli_sai == NULL);
1731 l_wait_event(thread->t_ctl_waitq,
1732 thread_is_running(thread) || thread_is_stopped(thread),
1736 * We don't stat-ahead for the first dirent since we are already in
1737 * lookup, and -EEXIST also indicates that this is the first dirent.
1744 cfs_spin_lock(&lli->lli_sa_lock);
1745 lli->lli_opendir_key = NULL;
1746 lli->lli_opendir_pid = 0;
1747 cfs_spin_unlock(&lli->lli_sa_lock);