4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 #include <linux/sched.h>
40 #include <linux/smp_lock.h>
41 #include <linux/highmem.h>
42 #include <linux/pagemap.h>
44 #define DEBUG_SUBSYSTEM S_LLITE
46 #include <obd_support.h>
47 #include <lustre_lite.h>
48 #include <lustre_dlm.h>
49 #include "llite_internal.h"
51 #define SA_OMITTED_ENTRY_MAX 8ULL
54 /** negative values are for error cases */
55 SA_ENTRY_INIT = 0, /** init entry */
56 SA_ENTRY_SUCC = 1, /** stat succeed */
57 SA_ENTRY_INVA = 2, /** invalid entry */
58 SA_ENTRY_DEST = 3, /** entry to be destroyed */
62 /* link into sai->sai_entries_{sent,received,stated} */
64 /* link into sai hash table locally */
66 /* entry reference count */
67 cfs_atomic_t se_refcount;
68 /* entry index in the sai */
70 /* low layer ldlm lock handle */
74 /* entry size, contains name */
76 /* pointer to async getattr enqueue info */
77 struct md_enqueue_info *se_minfo;
78 /* pointer to the async getattr request */
79 struct ptlrpc_request *se_req;
80 /* pointer to the target inode */
81 struct inode *se_inode;
86 static unsigned int sai_generation = 0;
87 static cfs_spinlock_t sai_generation_lock = CFS_SPIN_LOCK_UNLOCKED;
89 static inline int ll_sa_entry_unlinked(struct ll_sa_entry *entry)
91 return cfs_list_empty(&entry->se_list);
94 static inline int ll_sa_entry_unhashed(struct ll_sa_entry *entry)
96 return cfs_list_empty(&entry->se_hash);
100 * The entry only can be released by the caller, it is necessary to hold lock.
102 static inline int ll_sa_entry_stated(struct ll_sa_entry *entry)
105 return (entry->se_stat != SA_ENTRY_INIT);
108 static inline int ll_sa_entry_hash(int val)
110 return val & LL_SA_CACHE_MASK;
114 * Insert entry to hash SA table.
117 ll_sa_entry_enhash(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
119 int i = ll_sa_entry_hash(entry->se_qstr.hash);
121 cfs_spin_lock(&sai->sai_cache_lock[i]);
122 cfs_list_add_tail(&entry->se_hash, &sai->sai_cache[i]);
123 cfs_spin_unlock(&sai->sai_cache_lock[i]);
127 * Remove entry from SA table.
130 ll_sa_entry_unhash(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
132 int i = ll_sa_entry_hash(entry->se_qstr.hash);
134 cfs_spin_lock(&sai->sai_cache_lock[i]);
135 cfs_list_del_init(&entry->se_hash);
136 cfs_spin_unlock(&sai->sai_cache_lock[i]);
139 static inline int agl_should_run(struct ll_statahead_info *sai,
142 if (inode != NULL && S_ISREG(inode->i_mode) &&
143 ll_i2info(inode)->lli_smd != NULL && sai->sai_agl_valid)
148 static inline struct ll_sa_entry *
149 sa_first_received_entry(struct ll_statahead_info *sai)
151 return cfs_list_entry(sai->sai_entries_received.next,
152 struct ll_sa_entry, se_list);
155 static inline struct ll_inode_info *
156 agl_first_entry(struct ll_statahead_info *sai)
158 return cfs_list_entry(sai->sai_entries_agl.next,
159 struct ll_inode_info, lli_agl_list);
162 static inline int sa_sent_full(struct ll_statahead_info *sai)
164 return cfs_atomic_read(&sai->sai_cache_count) >= sai->sai_max;
167 static inline int sa_received_empty(struct ll_statahead_info *sai)
169 return cfs_list_empty(&sai->sai_entries_received);
172 static inline int agl_list_empty(struct ll_statahead_info *sai)
174 return cfs_list_empty(&sai->sai_entries_agl);
178 * (1) hit ratio less than 80%
180 * (2) consecutive miss more than 8
181 * then means low hit.
183 static inline int sa_low_hit(struct ll_statahead_info *sai)
185 return ((sai->sai_hit > 7 && sai->sai_hit < 4 * sai->sai_miss) ||
186 (sai->sai_consecutive_miss > 8));
190 * If the given index is behind of statahead window more than
191 * SA_OMITTED_ENTRY_MAX, then it is old.
193 static inline int is_omitted_entry(struct ll_statahead_info *sai, __u64 index)
195 return ((__u64)sai->sai_max + index + SA_OMITTED_ENTRY_MAX <
200 * Insert it into sai_entries_sent tail when init.
202 static struct ll_sa_entry *
203 ll_sa_entry_alloc(struct ll_statahead_info *sai, __u64 index,
204 const char *name, int len)
206 struct ll_inode_info *lli;
207 struct ll_sa_entry *entry;
212 entry_size = sizeof(struct ll_sa_entry) + (len & ~3) + 4;
213 OBD_ALLOC(entry, entry_size);
214 if (unlikely(entry == NULL))
215 RETURN(ERR_PTR(-ENOMEM));
217 CDEBUG(D_READA, "alloc sai entry %.*s(%p) index "LPU64"\n",
218 len, name, entry, index);
220 entry->se_index = index;
223 * Statahead entry reference rules:
225 * 1) When statahead entry is initialized, its reference is set as 2.
226 * One reference is used by the directory scanner. When the scanner
227 * searches the statahead cache for the given name, it can perform
228 * lockless hash lookup (only the scanner can remove entry from hash
229 * list), and once found, it needn't to call "atomic_inc()" for the
230 * entry reference. So the performance is improved. After using the
231 * statahead entry, the scanner will call "atomic_dec()" to drop the
232 * reference held when initialization. If it is the last reference,
233 * the statahead entry will be freed.
235 * 2) All other threads, including statahead thread and ptlrpcd thread,
236 * when they process the statahead entry, the reference for target
237 * should be held to guarantee the entry will not be released by the
238 * directory scanner. After processing the entry, these threads will
239 * drop the entry reference. If it is the last reference, the entry
242 * The second reference when initializes the statahead entry is used
243 * by the statahead thread, following the rule 2).
245 cfs_atomic_set(&entry->se_refcount, 2);
246 entry->se_stat = SA_ENTRY_INIT;
247 entry->se_size = entry_size;
248 dname = (char *)entry + sizeof(struct ll_sa_entry);
249 memcpy(dname, name, len);
251 entry->se_qstr.hash = full_name_hash(name, len);
252 entry->se_qstr.len = len;
253 entry->se_qstr.name = dname;
255 lli = ll_i2info(sai->sai_inode);
256 cfs_spin_lock(&lli->lli_sa_lock);
257 cfs_list_add_tail(&entry->se_list, &sai->sai_entries_sent);
258 cfs_spin_unlock(&lli->lli_sa_lock);
260 cfs_atomic_inc(&sai->sai_cache_count);
261 ll_sa_entry_enhash(sai, entry);
267 * Used by the directory scanner to search entry with name.
269 * Only the caller can remove the entry from hash, so it is unnecessary to hold
270 * hash lock. It is caller's duty to release the init refcount on the entry, so
271 * it is also unnecessary to increase refcount on the entry.
273 static struct ll_sa_entry *
274 ll_sa_entry_get_byname(struct ll_statahead_info *sai, const struct qstr *qstr)
276 struct ll_sa_entry *entry;
277 int i = ll_sa_entry_hash(qstr->hash);
279 cfs_list_for_each_entry(entry, &sai->sai_cache[i], se_hash) {
280 if (entry->se_qstr.hash == qstr->hash &&
281 entry->se_qstr.len == qstr->len &&
282 memcmp(entry->se_qstr.name, qstr->name, qstr->len) == 0)
289 * Used by the async getattr request callback to find entry with index.
291 * Inside lli_sa_lock to prevent others to change the list during the search.
292 * It needs to increase entry refcount before returning to guarantee that the
293 * entry cannot be freed by others.
295 static struct ll_sa_entry *
296 ll_sa_entry_get_byindex(struct ll_statahead_info *sai, __u64 index)
298 struct ll_sa_entry *entry;
300 cfs_list_for_each_entry(entry, &sai->sai_entries_sent, se_list) {
301 if (entry->se_index == index) {
302 cfs_atomic_inc(&entry->se_refcount);
305 if (entry->se_index > index)
311 static void ll_sa_entry_cleanup(struct ll_statahead_info *sai,
312 struct ll_sa_entry *entry)
314 struct md_enqueue_info *minfo = entry->se_minfo;
315 struct ptlrpc_request *req = entry->se_req;
318 entry->se_minfo = NULL;
319 ll_intent_release(&minfo->mi_it);
325 entry->se_req = NULL;
326 ptlrpc_req_finished(req);
330 static void ll_sa_entry_put(struct ll_statahead_info *sai,
331 struct ll_sa_entry *entry)
333 if (cfs_atomic_dec_and_test(&entry->se_refcount)) {
334 CDEBUG(D_READA, "free sai entry %.*s(%p) index "LPU64"\n",
335 entry->se_qstr.len, entry->se_qstr.name, entry,
338 LASSERT(ll_sa_entry_unhashed(entry));
339 LASSERT(ll_sa_entry_unlinked(entry));
341 ll_sa_entry_cleanup(sai, entry);
343 iput(entry->se_inode);
345 OBD_FREE(entry, entry->se_size);
346 cfs_atomic_dec(&sai->sai_cache_count);
351 do_sai_entry_fini(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
353 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
355 ll_sa_entry_unhash(sai, entry);
357 cfs_spin_lock(&lli->lli_sa_lock);
358 entry->se_stat = SA_ENTRY_DEST;
359 if (likely(!ll_sa_entry_unlinked(entry)))
360 cfs_list_del_init(&entry->se_list);
361 cfs_spin_unlock(&lli->lli_sa_lock);
363 ll_sa_entry_put(sai, entry);
367 * Delete it from sai_entries_stated list when fini.
370 ll_sa_entry_fini(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
372 struct ll_sa_entry *pos, *next;
375 do_sai_entry_fini(sai, entry);
377 /* drop old entry from sent list */
378 cfs_list_for_each_entry_safe(pos, next, &sai->sai_entries_sent,
380 if (is_omitted_entry(sai, pos->se_index))
381 do_sai_entry_fini(sai, pos);
386 /* drop old entry from stated list */
387 cfs_list_for_each_entry_safe(pos, next, &sai->sai_entries_stated,
389 if (is_omitted_entry(sai, pos->se_index))
390 do_sai_entry_fini(sai, pos);
397 * Inside lli_sa_lock.
400 do_sai_entry_to_stated(struct ll_statahead_info *sai,
401 struct ll_sa_entry *entry, int rc)
403 struct ll_sa_entry *se;
404 cfs_list_t *pos = &sai->sai_entries_stated;
406 if (!ll_sa_entry_unlinked(entry))
407 cfs_list_del_init(&entry->se_list);
409 cfs_list_for_each_entry_reverse(se, &sai->sai_entries_stated, se_list) {
410 if (se->se_index < entry->se_index) {
416 cfs_list_add(&entry->se_list, pos);
421 * Move entry to sai_entries_stated and sort with the index.
422 * \retval 1 -- entry to be destroyed.
423 * \retval 0 -- entry is inserted into stated list.
426 ll_sa_entry_to_stated(struct ll_statahead_info *sai,
427 struct ll_sa_entry *entry, int rc)
429 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
432 ll_sa_entry_cleanup(sai, entry);
434 cfs_spin_lock(&lli->lli_sa_lock);
435 if (likely(entry->se_stat != SA_ENTRY_DEST)) {
436 do_sai_entry_to_stated(sai, entry, rc);
439 cfs_spin_unlock(&lli->lli_sa_lock);
445 * Insert inode into the list of sai_entries_agl.
447 static void ll_agl_add(struct ll_statahead_info *sai,
448 struct inode *inode, int index)
450 struct ll_inode_info *child = ll_i2info(inode);
451 struct ll_inode_info *parent = ll_i2info(sai->sai_inode);
454 cfs_spin_lock(&child->lli_agl_lock);
455 if (child->lli_agl_index == 0) {
456 child->lli_agl_index = index;
457 cfs_spin_unlock(&child->lli_agl_lock);
459 LASSERT(cfs_list_empty(&child->lli_agl_list));
462 cfs_spin_lock(&parent->lli_agl_lock);
463 if (agl_list_empty(sai))
465 cfs_list_add_tail(&child->lli_agl_list, &sai->sai_entries_agl);
466 cfs_spin_unlock(&parent->lli_agl_lock);
468 cfs_spin_unlock(&child->lli_agl_lock);
472 cfs_waitq_signal(&sai->sai_agl_thread.t_ctl_waitq);
475 static struct ll_statahead_info *ll_sai_alloc(void)
477 struct ll_statahead_info *sai;
485 cfs_atomic_set(&sai->sai_refcount, 1);
487 cfs_spin_lock(&sai_generation_lock);
488 sai->sai_generation = ++sai_generation;
489 if (unlikely(sai_generation == 0))
490 sai->sai_generation = ++sai_generation;
491 cfs_spin_unlock(&sai_generation_lock);
493 sai->sai_max = LL_SA_RPC_MIN;
495 cfs_waitq_init(&sai->sai_waitq);
496 cfs_waitq_init(&sai->sai_thread.t_ctl_waitq);
497 cfs_waitq_init(&sai->sai_agl_thread.t_ctl_waitq);
499 CFS_INIT_LIST_HEAD(&sai->sai_entries_sent);
500 CFS_INIT_LIST_HEAD(&sai->sai_entries_received);
501 CFS_INIT_LIST_HEAD(&sai->sai_entries_stated);
502 CFS_INIT_LIST_HEAD(&sai->sai_entries_agl);
504 for (i = 0; i < LL_SA_CACHE_SIZE; i++) {
505 CFS_INIT_LIST_HEAD(&sai->sai_cache[i]);
506 cfs_spin_lock_init(&sai->sai_cache_lock[i]);
508 cfs_atomic_set(&sai->sai_cache_count, 0);
513 static inline struct ll_statahead_info *
514 ll_sai_get(struct ll_statahead_info *sai)
516 cfs_atomic_inc(&sai->sai_refcount);
520 static void ll_sai_put(struct ll_statahead_info *sai)
522 struct inode *inode = sai->sai_inode;
523 struct ll_inode_info *lli = ll_i2info(inode);
526 if (cfs_atomic_dec_and_lock(&sai->sai_refcount, &lli->lli_sa_lock)) {
527 struct ll_sa_entry *entry, *next;
529 if (unlikely(cfs_atomic_read(&sai->sai_refcount) > 0)) {
530 /* It is race case, the interpret callback just hold
531 * a reference count */
532 cfs_spin_unlock(&lli->lli_sa_lock);
536 LASSERT(lli->lli_opendir_key == NULL);
537 LASSERT(thread_is_stopped(&sai->sai_thread));
538 LASSERT(thread_is_stopped(&sai->sai_agl_thread));
541 lli->lli_opendir_pid = 0;
542 cfs_spin_unlock(&lli->lli_sa_lock);
544 if (sai->sai_sent > sai->sai_replied)
545 CDEBUG(D_READA,"statahead for dir "DFID" does not "
546 "finish: [sent:"LPU64"] [replied:"LPU64"]\n",
548 sai->sai_sent, sai->sai_replied);
550 cfs_list_for_each_entry_safe(entry, next,
551 &sai->sai_entries_sent, se_list)
552 do_sai_entry_fini(sai, entry);
554 LASSERT(sa_received_empty(sai));
556 cfs_list_for_each_entry_safe(entry, next,
557 &sai->sai_entries_stated, se_list)
558 do_sai_entry_fini(sai, entry);
560 LASSERT(cfs_atomic_read(&sai->sai_cache_count) == 0);
561 LASSERT(agl_list_empty(sai));
570 /* Do NOT forget to drop inode refcount when into sai_entries_agl. */
571 static void ll_agl_trigger(struct inode *inode, struct ll_statahead_info *sai)
573 struct ll_inode_info *lli = ll_i2info(inode);
574 __u64 index = lli->lli_agl_index;
578 LASSERT(cfs_list_empty(&lli->lli_agl_list));
580 /* AGL maybe fall behind statahead with one entry */
581 if (is_omitted_entry(sai, index + 1)) {
582 lli->lli_agl_index = 0;
587 /* Someone is in glimpse (sync or async), do nothing. */
588 rc = cfs_down_write_trylock(&lli->lli_glimpse_sem);
590 lli->lli_agl_index = 0;
596 * Someone triggered glimpse within 1 sec before.
597 * 1) The former glimpse succeeded with glimpse lock granted by OST, and
598 * if the lock is still cached on client, AGL needs to do nothing. If
599 * it is cancelled by other client, AGL maybe cannot obtaion new lock
600 * for no glimpse callback triggered by AGL.
601 * 2) The former glimpse succeeded, but OST did not grant glimpse lock.
602 * Under such case, it is quite possible that the OST will not grant
603 * glimpse lock for AGL also.
604 * 3) The former glimpse failed, compared with other two cases, it is
605 * relative rare. AGL can ignore such case, and it will not muchly
606 * affect the performance.
608 if (lli->lli_glimpse_time != 0 &&
609 cfs_time_before(cfs_time_shift(-1), lli->lli_glimpse_time)) {
610 cfs_up_write(&lli->lli_glimpse_sem);
611 lli->lli_agl_index = 0;
616 CDEBUG(D_READA, "Handling (init) async glimpse: inode = "
617 DFID", idx = "LPU64"\n", PFID(&lli->lli_fid), index);
620 lli->lli_agl_index = 0;
621 lli->lli_glimpse_time = cfs_time_current();
622 cfs_up_write(&lli->lli_glimpse_sem);
624 CDEBUG(D_READA, "Handled (init) async glimpse: inode= "
625 DFID", idx = "LPU64", rc = %d\n",
626 PFID(&lli->lli_fid), index, rc);
633 static void do_statahead_interpret(struct ll_statahead_info *sai,
634 struct ll_sa_entry *target)
636 struct inode *dir = sai->sai_inode;
638 struct ll_inode_info *lli = ll_i2info(dir);
639 struct ll_sa_entry *entry;
640 struct md_enqueue_info *minfo;
641 struct lookup_intent *it;
642 struct ptlrpc_request *req;
643 struct mdt_body *body;
647 cfs_spin_lock(&lli->lli_sa_lock);
648 if (target != NULL && target->se_req != NULL &&
649 !cfs_list_empty(&target->se_list)) {
651 } else if (unlikely(sa_received_empty(sai))) {
652 cfs_spin_unlock(&lli->lli_sa_lock);
655 entry = sa_first_received_entry(sai);
658 cfs_atomic_inc(&entry->se_refcount);
659 cfs_list_del_init(&entry->se_list);
660 cfs_spin_unlock(&lli->lli_sa_lock);
662 LASSERT(entry->se_handle != 0);
664 minfo = entry->se_minfo;
667 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
669 GOTO(out, rc = -EFAULT);
671 child = entry->se_inode;
676 LASSERT(fid_is_zero(&minfo->mi_data.op_fid2));
678 /* XXX: No fid in reply, this is probaly cross-ref case.
679 * SA can't handle it yet. */
680 if (body->valid & OBD_MD_MDS)
681 GOTO(out, rc = -EAGAIN);
686 /* unlinked and re-created with the same name */
687 if (unlikely(!lu_fid_eq(&minfo->mi_data.op_fid2, &body->fid1))){
688 entry->se_inode = NULL;
694 it->d.lustre.it_lock_handle = entry->se_handle;
695 rc = md_revalidate_lock(ll_i2mdexp(dir), it, NULL, NULL);
697 GOTO(out, rc = -EAGAIN);
699 rc = ll_prep_inode(&child, req, dir->i_sb);
703 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
704 child, child->i_ino, child->i_generation);
705 ll_set_lock_data(ll_i2sbi(dir)->ll_md_exp, child, it, NULL);
707 entry->se_inode = child;
709 if (agl_should_run(sai, child))
710 ll_agl_add(sai, child, entry->se_index);
715 /* The "ll_sa_entry_to_stated()" will drop related ldlm ibits lock
716 * reference count by calling "ll_intent_drop_lock()" in spite of the
717 * above operations failed or not. Do not worry about calling
718 * "ll_intent_drop_lock()" more than once. */
719 rc = ll_sa_entry_to_stated(sai, entry, rc < 0 ? rc : SA_ENTRY_SUCC);
720 if (rc == 0 && entry->se_index == sai->sai_index_wait && target == NULL)
721 cfs_waitq_signal(&sai->sai_waitq);
722 ll_sa_entry_put(sai, entry);
725 static int ll_statahead_interpret(struct ptlrpc_request *req,
726 struct md_enqueue_info *minfo, int rc)
728 struct lookup_intent *it = &minfo->mi_it;
729 struct inode *dir = minfo->mi_dir;
730 struct ll_inode_info *lli = ll_i2info(dir);
731 struct ll_statahead_info *sai = NULL;
732 struct ll_sa_entry *entry;
736 if (it_disposition(it, DISP_LOOKUP_NEG))
739 cfs_spin_lock(&lli->lli_sa_lock);
741 if (unlikely(lli->lli_sai == NULL ||
742 lli->lli_sai->sai_generation != minfo->mi_generation)) {
743 cfs_spin_unlock(&lli->lli_sa_lock);
744 GOTO(out, rc = -ESTALE);
746 sai = ll_sai_get(lli->lli_sai);
747 if (unlikely(!thread_is_running(&sai->sai_thread))) {
749 cfs_spin_unlock(&lli->lli_sa_lock);
750 GOTO(out, rc = -EBADFD);
753 entry = ll_sa_entry_get_byindex(sai, minfo->mi_cbdata);
756 cfs_spin_unlock(&lli->lli_sa_lock);
757 GOTO(out, rc = -EIDRM);
760 cfs_list_del_init(&entry->se_list);
763 do_sai_entry_to_stated(sai, entry, rc);
764 cfs_spin_unlock(&lli->lli_sa_lock);
765 if (entry->se_index == sai->sai_index_wait)
766 cfs_waitq_signal(&sai->sai_waitq);
768 entry->se_minfo = minfo;
769 entry->se_req = ptlrpc_request_addref(req);
770 /* Release the async ibits lock ASAP to avoid deadlock
771 * when statahead thread tries to enqueue lock on parent
772 * for readpage and other tries to enqueue lock on child
773 * with parent's lock held, for example: unlink. */
774 entry->se_handle = it->d.lustre.it_lock_handle;
775 ll_intent_drop_lock(it);
776 wakeup = sa_received_empty(sai);
777 cfs_list_add_tail(&entry->se_list,
778 &sai->sai_entries_received);
780 cfs_spin_unlock(&lli->lli_sa_lock);
782 cfs_waitq_signal(&sai->sai_thread.t_ctl_waitq);
784 ll_sa_entry_put(sai, entry);
791 ll_intent_release(it);
800 static void sa_args_fini(struct md_enqueue_info *minfo,
801 struct ldlm_enqueue_info *einfo)
803 LASSERT(minfo && einfo);
805 capa_put(minfo->mi_data.op_capa1);
806 capa_put(minfo->mi_data.op_capa2);
812 * There is race condition between "capa_put" and "ll_statahead_interpret" for
813 * accessing "op_data.op_capa[1,2]" as following:
814 * "capa_put" releases "op_data.op_capa[1,2]"'s reference count after calling
815 * "md_intent_getattr_async". But "ll_statahead_interpret" maybe run first, and
816 * fill "op_data.op_capa[1,2]" as POISON, then cause "capa_put" access invalid
817 * "ocapa". So here reserve "op_data.op_capa[1,2]" in "pcapa" before calling
818 * "md_intent_getattr_async".
820 static int sa_args_init(struct inode *dir, struct inode *child,
821 struct ll_sa_entry *entry, struct md_enqueue_info **pmi,
822 struct ldlm_enqueue_info **pei,
823 struct obd_capa **pcapa)
825 struct qstr *qstr = &entry->se_qstr;
826 struct ll_inode_info *lli = ll_i2info(dir);
827 struct md_enqueue_info *minfo;
828 struct ldlm_enqueue_info *einfo;
829 struct md_op_data *op_data;
831 OBD_ALLOC_PTR(einfo);
835 OBD_ALLOC_PTR(minfo);
841 op_data = ll_prep_md_op_data(&minfo->mi_data, dir, child, qstr->name,
842 qstr->len, 0, LUSTRE_OPC_ANY, NULL);
843 if (IS_ERR(op_data)) {
846 return PTR_ERR(op_data);
849 minfo->mi_it.it_op = IT_GETATTR;
850 minfo->mi_dir = igrab(dir);
851 minfo->mi_cb = ll_statahead_interpret;
852 minfo->mi_generation = lli->lli_sai->sai_generation;
853 minfo->mi_cbdata = entry->se_index;
855 einfo->ei_type = LDLM_IBITS;
856 einfo->ei_mode = it_to_lock_mode(&minfo->mi_it);
857 einfo->ei_cb_bl = ll_md_blocking_ast;
858 einfo->ei_cb_cp = ldlm_completion_ast;
859 einfo->ei_cb_gl = NULL;
860 einfo->ei_cbdata = NULL;
864 pcapa[0] = op_data->op_capa1;
865 pcapa[1] = op_data->op_capa2;
870 static int do_sa_lookup(struct inode *dir, struct ll_sa_entry *entry)
872 struct md_enqueue_info *minfo;
873 struct ldlm_enqueue_info *einfo;
874 struct obd_capa *capas[2];
878 rc = sa_args_init(dir, NULL, entry, &minfo, &einfo, capas);
882 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
887 sa_args_fini(minfo, einfo);
894 * similar to ll_revalidate_it().
895 * \retval 1 -- dentry valid
896 * \retval 0 -- will send stat-ahead request
897 * \retval others -- prepare stat-ahead request failed
899 static int do_sa_revalidate(struct inode *dir, struct ll_sa_entry *entry,
900 struct dentry *dentry)
902 struct inode *inode = dentry->d_inode;
903 struct lookup_intent it = { .it_op = IT_GETATTR,
904 .d.lustre.it_lock_handle = 0 };
905 struct md_enqueue_info *minfo;
906 struct ldlm_enqueue_info *einfo;
907 struct obd_capa *capas[2];
911 if (unlikely(inode == NULL))
914 if (d_mountpoint(dentry))
917 if (unlikely(dentry == dentry->d_sb->s_root))
920 entry->se_inode = igrab(inode);
921 rc = md_revalidate_lock(ll_i2mdexp(dir), &it, ll_inode2fid(inode),NULL);
923 entry->se_handle = it.d.lustre.it_lock_handle;
924 ll_intent_release(&it);
928 rc = sa_args_init(dir, inode, entry, &minfo, &einfo, capas);
930 entry->se_inode = NULL;
935 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
940 entry->se_inode = NULL;
942 sa_args_fini(minfo, einfo);
948 static void ll_statahead_one(struct dentry *parent, const char* entry_name,
951 struct inode *dir = parent->d_inode;
952 struct ll_inode_info *lli = ll_i2info(dir);
953 struct ll_statahead_info *sai = lli->lli_sai;
954 struct dentry *dentry = NULL;
955 struct ll_sa_entry *entry;
960 entry = ll_sa_entry_alloc(sai, sai->sai_index, entry_name,
965 dentry = d_lookup(parent, &entry->se_qstr);
967 rc = do_sa_lookup(dir, entry);
969 rc = do_sa_revalidate(dir, entry, dentry);
970 if (rc == 1 && agl_should_run(sai, dentry->d_inode))
971 ll_agl_add(sai, dentry->d_inode, entry->se_index);
978 rc1 = ll_sa_entry_to_stated(sai, entry,
979 rc < 0 ? SA_ENTRY_INVA : SA_ENTRY_SUCC);
980 if (rc1 == 0 && entry->se_index == sai->sai_index_wait)
981 cfs_waitq_signal(&sai->sai_waitq);
987 /* drop one refcount on entry by ll_sa_entry_alloc */
988 ll_sa_entry_put(sai, entry);
993 static int ll_agl_thread(void *arg)
995 struct dentry *parent = (struct dentry *)arg;
996 struct inode *dir = parent->d_inode;
997 struct ll_inode_info *plli = ll_i2info(dir);
998 struct ll_inode_info *clli;
999 struct ll_sb_info *sbi = ll_i2sbi(dir);
1000 struct ll_statahead_info *sai = ll_sai_get(plli->lli_sai);
1001 struct ptlrpc_thread *thread = &sai->sai_agl_thread;
1002 struct l_wait_info lwi = { 0 };
1007 snprintf(pname, 15, "ll_agl_%u", plli->lli_opendir_pid);
1008 cfs_daemonize(pname);
1011 CDEBUG(D_READA, "agl thread started: [pid %d] [parent %.*s]\n",
1012 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1014 atomic_inc(&sbi->ll_agl_total);
1015 cfs_spin_lock(&plli->lli_agl_lock);
1016 sai->sai_agl_valid = 1;
1017 thread_set_flags(thread, SVC_RUNNING);
1018 cfs_spin_unlock(&plli->lli_agl_lock);
1019 cfs_waitq_signal(&thread->t_ctl_waitq);
1022 l_wait_event(thread->t_ctl_waitq,
1023 !agl_list_empty(sai) ||
1024 !thread_is_running(thread),
1027 if (!thread_is_running(thread))
1030 cfs_spin_lock(&plli->lli_agl_lock);
1031 /* The statahead thread maybe help to process AGL entries,
1032 * so check whether list empty again. */
1033 if (!agl_list_empty(sai)) {
1034 clli = agl_first_entry(sai);
1035 cfs_list_del_init(&clli->lli_agl_list);
1036 cfs_spin_unlock(&plli->lli_agl_lock);
1037 ll_agl_trigger(&clli->lli_vfs_inode, sai);
1039 cfs_spin_unlock(&plli->lli_agl_lock);
1043 cfs_spin_lock(&plli->lli_agl_lock);
1044 sai->sai_agl_valid = 0;
1045 while (!agl_list_empty(sai)) {
1046 clli = agl_first_entry(sai);
1047 cfs_list_del_init(&clli->lli_agl_list);
1048 cfs_spin_unlock(&plli->lli_agl_lock);
1049 clli->lli_agl_index = 0;
1050 iput(&clli->lli_vfs_inode);
1051 cfs_spin_lock(&plli->lli_agl_lock);
1053 thread_set_flags(thread, SVC_STOPPED);
1054 cfs_spin_unlock(&plli->lli_agl_lock);
1055 cfs_waitq_signal(&thread->t_ctl_waitq);
1057 CDEBUG(D_READA, "agl thread stopped: [pid %d] [parent %.*s]\n",
1058 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1062 static void ll_start_agl(struct dentry *parent, struct ll_statahead_info *sai)
1064 struct ptlrpc_thread *thread = &sai->sai_agl_thread;
1065 struct l_wait_info lwi = { 0 };
1069 CDEBUG(D_READA, "start agl thread: [pid %d] [parent %.*s]\n",
1070 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1072 rc = cfs_create_thread(ll_agl_thread, parent, 0);
1074 CERROR("can't start ll_agl thread, rc: %d\n", rc);
1075 thread_set_flags(thread, SVC_STOPPED);
1079 l_wait_event(thread->t_ctl_waitq,
1080 thread_is_running(thread) || thread_is_stopped(thread),
1085 static int ll_statahead_thread(void *arg)
1087 struct dentry *parent = (struct dentry *)arg;
1088 struct inode *dir = parent->d_inode;
1089 struct ll_inode_info *plli = ll_i2info(dir);
1090 struct ll_inode_info *clli;
1091 struct ll_sb_info *sbi = ll_i2sbi(dir);
1092 struct ll_statahead_info *sai = ll_sai_get(plli->lli_sai);
1093 struct ptlrpc_thread *thread = &sai->sai_thread;
1094 struct ptlrpc_thread *agl_thread = &sai->sai_agl_thread;
1099 struct ll_dir_chain chain;
1100 struct l_wait_info lwi = { 0 };
1105 snprintf(pname, 15, "ll_sa_%u", plli->lli_opendir_pid);
1106 cfs_daemonize(pname);
1109 CDEBUG(D_READA, "statahead thread started: [pid %d] [parent %.*s]\n",
1110 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1112 if (sbi->ll_flags & LL_SBI_AGL_ENABLED)
1113 ll_start_agl(parent, sai);
1115 atomic_inc(&sbi->ll_sa_total);
1116 cfs_spin_lock(&plli->lli_sa_lock);
1117 thread_set_flags(thread, SVC_RUNNING);
1118 cfs_spin_unlock(&plli->lli_sa_lock);
1119 cfs_waitq_signal(&thread->t_ctl_waitq);
1121 plli->lli_sa_pos = 0;
1122 ll_dir_chain_init(&chain);
1123 page = ll_get_dir_page(NULL, dir, pos, &chain);
1126 struct lu_dirpage *dp;
1127 struct lu_dirent *ent;
1131 CDEBUG(D_READA, "error reading dir "DFID" at "LPU64
1132 "/"LPU64": [rc %d] [parent %u]\n",
1133 PFID(ll_inode2fid(dir)), pos, sai->sai_index,
1134 rc, plli->lli_opendir_pid);
1138 dp = page_address(page);
1139 for (ent = lu_dirent_start(dp); ent != NULL;
1140 ent = lu_dirent_next(ent)) {
1145 hash = le64_to_cpu(ent->lde_hash);
1146 if (unlikely(hash < pos))
1148 * Skip until we find target hash value.
1152 namelen = le16_to_cpu(ent->lde_namelen);
1153 if (unlikely(namelen == 0))
1155 * Skip dummy record.
1159 name = ent->lde_name;
1160 if (name[0] == '.') {
1166 } else if (name[1] == '.' && namelen == 2) {
1171 } else if (!sai->sai_ls_all) {
1173 * skip hidden files.
1175 sai->sai_skip_hidden++;
1181 * don't stat-ahead first entry.
1183 if (unlikely(++first == 1))
1187 l_wait_event(thread->t_ctl_waitq,
1188 !sa_sent_full(sai) ||
1189 !sa_received_empty(sai) ||
1190 !agl_list_empty(sai) ||
1191 !thread_is_running(thread),
1195 while (!sa_received_empty(sai))
1196 do_statahead_interpret(sai, NULL);
1198 if (unlikely(!thread_is_running(thread))) {
1199 ll_release_page(page, 0);
1203 /* If no window for metadata statahead, but there are
1204 * some AGL entries to be triggered, then try to help
1205 * to process the AGL entries. */
1206 if (sa_sent_full(sai)) {
1207 cfs_spin_lock(&plli->lli_agl_lock);
1208 while (!agl_list_empty(sai)) {
1209 clli = agl_first_entry(sai);
1210 cfs_list_del_init(&clli->lli_agl_list);
1211 cfs_spin_unlock(&plli->lli_agl_lock);
1212 ll_agl_trigger(&clli->lli_vfs_inode,
1215 if (!sa_received_empty(sai))
1219 !thread_is_running(thread))) {
1220 ll_release_page(page, 0);
1224 if (!sa_sent_full(sai))
1227 cfs_spin_lock(&plli->lli_agl_lock);
1229 cfs_spin_unlock(&plli->lli_agl_lock);
1235 ll_statahead_one(parent, name, namelen);
1237 pos = le64_to_cpu(dp->ldp_hash_end);
1238 if (pos == MDS_DIR_END_OFF) {
1240 * End of directory reached.
1242 ll_release_page(page, 0);
1244 l_wait_event(thread->t_ctl_waitq,
1245 !sa_received_empty(sai) ||
1246 sai->sai_sent == sai->sai_replied||
1247 !thread_is_running(thread),
1250 while (!sa_received_empty(sai))
1251 do_statahead_interpret(sai, NULL);
1253 if (unlikely(!thread_is_running(thread)))
1256 if (sai->sai_sent == sai->sai_replied &&
1257 sa_received_empty(sai))
1261 cfs_spin_lock(&plli->lli_agl_lock);
1262 while (!agl_list_empty(sai) &&
1263 thread_is_running(thread)) {
1264 clli = agl_first_entry(sai);
1265 cfs_list_del_init(&clli->lli_agl_list);
1266 cfs_spin_unlock(&plli->lli_agl_lock);
1267 ll_agl_trigger(&clli->lli_vfs_inode, sai);
1268 cfs_spin_lock(&plli->lli_agl_lock);
1270 cfs_spin_unlock(&plli->lli_agl_lock);
1275 * chain is exhausted.
1276 * Normal case: continue to the next page.
1278 ll_release_page(page, le32_to_cpu(dp->ldp_flags) &
1280 plli->lli_sa_pos = pos;
1281 sai->sai_in_readpage = 1;
1282 page = ll_get_dir_page(NULL, dir, pos, &chain);
1283 sai->sai_in_readpage = 0;
1285 LASSERT(le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1286 ll_release_page(page, 1);
1288 * go into overflow page.
1295 if (sai->sai_agl_valid) {
1296 cfs_spin_lock(&plli->lli_agl_lock);
1297 thread_set_flags(agl_thread, SVC_STOPPING);
1298 cfs_spin_unlock(&plli->lli_agl_lock);
1299 cfs_waitq_signal(&agl_thread->t_ctl_waitq);
1301 CDEBUG(D_READA, "stop agl thread: [pid %d]\n",
1303 l_wait_event(agl_thread->t_ctl_waitq,
1304 thread_is_stopped(agl_thread),
1307 /* Set agl_thread flags anyway. */
1308 thread_set_flags(&sai->sai_agl_thread, SVC_STOPPED);
1310 ll_dir_chain_fini(&chain);
1311 cfs_spin_lock(&plli->lli_sa_lock);
1312 if (!sa_received_empty(sai)) {
1313 thread_set_flags(thread, SVC_STOPPING);
1314 cfs_spin_unlock(&plli->lli_sa_lock);
1316 /* To release the resources held by received entries. */
1317 while (!sa_received_empty(sai))
1318 do_statahead_interpret(sai, NULL);
1320 cfs_spin_lock(&plli->lli_sa_lock);
1322 thread_set_flags(thread, SVC_STOPPED);
1323 cfs_spin_unlock(&plli->lli_sa_lock);
1324 cfs_waitq_signal(&sai->sai_waitq);
1325 cfs_waitq_signal(&thread->t_ctl_waitq);
1328 CDEBUG(D_READA, "statahead thread stopped: [pid %d] [parent %.*s]\n",
1329 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1334 * called in ll_file_release().
1336 void ll_stop_statahead(struct inode *dir, void *key)
1338 struct ll_inode_info *lli = ll_i2info(dir);
1340 if (unlikely(key == NULL))
1343 cfs_spin_lock(&lli->lli_sa_lock);
1344 if (lli->lli_opendir_key != key || lli->lli_opendir_pid == 0) {
1345 cfs_spin_unlock(&lli->lli_sa_lock);
1349 lli->lli_opendir_key = NULL;
1352 struct l_wait_info lwi = { 0 };
1353 struct ptlrpc_thread *thread = &lli->lli_sai->sai_thread;
1355 if (!thread_is_stopped(thread)) {
1356 thread_set_flags(thread, SVC_STOPPING);
1357 cfs_spin_unlock(&lli->lli_sa_lock);
1358 cfs_waitq_signal(&thread->t_ctl_waitq);
1360 CDEBUG(D_READA, "stop statahead thread: [pid %d]\n",
1362 l_wait_event(thread->t_ctl_waitq,
1363 thread_is_stopped(thread),
1366 cfs_spin_unlock(&lli->lli_sa_lock);
1370 * Put the ref which was held when first statahead_enter.
1371 * It maybe not the last ref for some statahead requests
1374 ll_sai_put(lli->lli_sai);
1376 lli->lli_opendir_pid = 0;
1377 cfs_spin_unlock(&lli->lli_sa_lock);
1383 * not first dirent, or is "."
1385 LS_NONE_FIRST_DE = 0,
1387 * the first non-hidden dirent
1391 * the first hidden dirent, that is "."
1396 static int is_first_dirent(struct inode *dir, struct dentry *dentry)
1398 struct ll_inode_info *lli = ll_i2info(dir);
1399 struct ll_dir_chain chain;
1400 struct qstr *target = &dentry->d_name;
1404 int rc = LS_NONE_FIRST_DE;
1407 lli->lli_sa_pos = 0;
1408 ll_dir_chain_init(&chain);
1409 page = ll_get_dir_page(NULL, dir, pos, &chain);
1412 struct lu_dirpage *dp;
1413 struct lu_dirent *ent;
1416 struct ll_inode_info *lli = ll_i2info(dir);
1419 CERROR("error reading dir "DFID" at "LPU64": "
1420 "[rc %d] [parent %u]\n",
1421 PFID(ll_inode2fid(dir)), pos,
1422 rc, lli->lli_opendir_pid);
1426 dp = page_address(page);
1427 for (ent = lu_dirent_start(dp); ent != NULL;
1428 ent = lu_dirent_next(ent)) {
1433 hash = le64_to_cpu(ent->lde_hash);
1434 /* The ll_get_dir_page() can return any page containing
1435 * the given hash which may be not the start hash. */
1436 if (unlikely(hash < pos))
1439 namelen = le16_to_cpu(ent->lde_namelen);
1440 if (unlikely(namelen == 0))
1442 * skip dummy record.
1446 name = ent->lde_name;
1447 if (name[0] == '.') {
1453 else if (name[1] == '.' && namelen == 2)
1464 if (dot_de && target->name[0] != '.') {
1465 CDEBUG(D_READA, "%.*s skip hidden file %.*s\n",
1466 target->len, target->name,
1471 if (target->len != namelen ||
1472 memcmp(target->name, name, namelen) != 0)
1473 rc = LS_NONE_FIRST_DE;
1477 rc = LS_FIRST_DOT_DE;
1479 ll_release_page(page, 0);
1482 pos = le64_to_cpu(dp->ldp_hash_end);
1483 if (pos == MDS_DIR_END_OFF) {
1485 * End of directory reached.
1487 ll_release_page(page, 0);
1491 * chain is exhausted
1492 * Normal case: continue to the next page.
1494 ll_release_page(page, le32_to_cpu(dp->ldp_flags) &
1496 lli->lli_sa_pos = pos;
1497 page = ll_get_dir_page(NULL, dir, pos, &chain);
1500 * go into overflow page.
1502 LASSERT(le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1503 ll_release_page(page, 1);
1509 ll_dir_chain_fini(&chain);
1514 ll_sai_unplug(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
1516 struct ptlrpc_thread *thread = &sai->sai_thread;
1517 struct ll_sb_info *sbi = ll_i2sbi(sai->sai_inode);
1521 if (entry != NULL && entry->se_stat == SA_ENTRY_SUCC)
1526 ll_sa_entry_fini(sai, entry);
1529 sai->sai_consecutive_miss = 0;
1530 sai->sai_max = min(2 * sai->sai_max, sbi->ll_sa_max);
1532 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
1535 sai->sai_consecutive_miss++;
1536 if (sa_low_hit(sai) && thread_is_running(thread)) {
1537 atomic_inc(&sbi->ll_sa_wrong);
1538 CDEBUG(D_READA, "Statahead for dir "DFID" hit "
1539 "ratio too low: hit/miss "LPU64"/"LPU64
1540 ", sent/replied "LPU64"/"LPU64", stopping "
1541 "statahead thread: pid %d\n",
1542 PFID(&lli->lli_fid), sai->sai_hit,
1543 sai->sai_miss, sai->sai_sent,
1544 sai->sai_replied, cfs_curproc_pid());
1545 cfs_spin_lock(&lli->lli_sa_lock);
1546 if (!thread_is_stopped(thread))
1547 thread_set_flags(thread, SVC_STOPPING);
1548 cfs_spin_unlock(&lli->lli_sa_lock);
1552 if (!thread_is_stopped(thread))
1553 cfs_waitq_signal(&thread->t_ctl_waitq);
1559 * Start statahead thread if this is the first dir entry.
1560 * Otherwise if a thread is started already, wait it until it is ahead of me.
1561 * \retval 1 -- find entry with lock in cache, the caller needs to do
1563 * \retval 0 -- find entry in cache, but without lock, the caller needs
1565 * \retval others -- the caller need to process as non-statahead.
1567 int do_statahead_enter(struct inode *dir, struct dentry **dentryp,
1570 struct ll_inode_info *lli = ll_i2info(dir);
1571 struct ll_statahead_info *sai = lli->lli_sai;
1572 struct dentry *parent;
1573 struct ll_sa_entry *entry;
1574 struct ptlrpc_thread *thread;
1575 struct l_wait_info lwi = { 0 };
1579 LASSERT(lli->lli_opendir_pid == cfs_curproc_pid());
1582 thread = &sai->sai_thread;
1583 if (unlikely(thread_is_stopped(thread) &&
1584 cfs_list_empty(&sai->sai_entries_stated))) {
1585 /* to release resource */
1586 ll_stop_statahead(dir, lli->lli_opendir_key);
1590 if ((*dentryp)->d_name.name[0] == '.') {
1591 if (sai->sai_ls_all ||
1592 sai->sai_miss_hidden >= sai->sai_skip_hidden) {
1594 * Hidden dentry is the first one, or statahead
1595 * thread does not skip so many hidden dentries
1596 * before "sai_ls_all" enabled as below.
1599 if (!sai->sai_ls_all)
1601 * It maybe because hidden dentry is not
1602 * the first one, "sai_ls_all" was not
1603 * set, then "ls -al" missed. Enable
1604 * "sai_ls_all" for such case.
1606 sai->sai_ls_all = 1;
1609 * Such "getattr" has been skipped before
1610 * "sai_ls_all" enabled as above.
1612 sai->sai_miss_hidden++;
1617 entry = ll_sa_entry_get_byname(sai, &(*dentryp)->d_name);
1618 if (entry == NULL || only_unplug) {
1619 ll_sai_unplug(sai, entry);
1620 RETURN(entry ? 1 : -EAGAIN);
1623 while (!ll_sa_entry_stated(entry) &&
1624 sai->sai_in_readpage &&
1625 !sa_received_empty(sai))
1626 do_statahead_interpret(sai, entry);
1628 if (!ll_sa_entry_stated(entry)) {
1629 sai->sai_index_wait = entry->se_index;
1630 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(30), NULL,
1631 LWI_ON_SIGNAL_NOOP, NULL);
1632 rc = l_wait_event(sai->sai_waitq,
1633 ll_sa_entry_stated(entry) ||
1634 thread_is_stopped(thread),
1637 ll_sai_unplug(sai, entry);
1642 if (entry->se_stat == SA_ENTRY_SUCC &&
1643 entry->se_inode != NULL) {
1644 struct inode *inode = entry->se_inode;
1645 struct lookup_intent it = { .it_op = IT_GETATTR,
1646 .d.lustre.it_lock_handle =
1648 struct ll_dentry_data *lld;
1651 rc = md_revalidate_lock(ll_i2mdexp(dir), &it,
1652 ll_inode2fid(inode), &bits);
1654 if ((*dentryp)->d_inode == NULL) {
1655 *dentryp = ll_find_alias(inode,
1657 lld = ll_d2d(*dentryp);
1658 if (unlikely(lld == NULL))
1659 ll_dops_init(*dentryp, 1, 1);
1660 } else if ((*dentryp)->d_inode != inode) {
1661 /* revalidate, but inode is recreated */
1663 "stale dentry %.*s inode %lu/%u, "
1664 "statahead inode %lu/%u\n",
1665 (*dentryp)->d_name.len,
1666 (*dentryp)->d_name.name,
1667 (*dentryp)->d_inode->i_ino,
1668 (*dentryp)->d_inode->i_generation,
1670 inode->i_generation);
1671 ll_sai_unplug(sai, entry);
1674 ll_dentry_rehash(*dentryp, 0);
1677 entry->se_inode = NULL;
1679 ll_dentry_reset_flags(*dentryp, bits);
1680 ll_intent_release(&it);
1684 ll_sai_unplug(sai, entry);
1688 /* I am the "lli_opendir_pid" owner, only me can set "lli_sai". */
1689 rc = is_first_dirent(dir, *dentryp);
1690 if (rc == LS_NONE_FIRST_DE)
1691 /* It is not "ls -{a}l" operation, no need statahead for it. */
1692 GOTO(out, rc = -EAGAIN);
1694 sai = ll_sai_alloc();
1696 GOTO(out, rc = -ENOMEM);
1698 sai->sai_ls_all = (rc == LS_FIRST_DOT_DE);
1699 sai->sai_inode = igrab(dir);
1700 if (unlikely(sai->sai_inode == NULL)) {
1701 CWARN("Do not start stat ahead on dying inode "DFID"\n",
1702 PFID(&lli->lli_fid));
1703 GOTO(out, rc = -ESTALE);
1706 /* get parent reference count here, and put it in ll_statahead_thread */
1707 parent = dget((*dentryp)->d_parent);
1708 if (unlikely(sai->sai_inode != parent->d_inode)) {
1709 struct ll_inode_info *nlli = ll_i2info(parent->d_inode);
1711 CWARN("Race condition, someone changed %.*s just now: "
1712 "old parent "DFID", new parent "DFID"\n",
1713 (*dentryp)->d_name.len, (*dentryp)->d_name.name,
1714 PFID(&lli->lli_fid), PFID(&nlli->lli_fid));
1716 iput(sai->sai_inode);
1717 GOTO(out, rc = -EAGAIN);
1720 CDEBUG(D_READA, "start statahead thread: [pid %d] [parent %.*s]\n",
1721 cfs_curproc_pid(), parent->d_name.len, parent->d_name.name);
1724 rc = cfs_create_thread(ll_statahead_thread, parent, 0);
1725 thread = &sai->sai_thread;
1727 CERROR("can't start ll_sa thread, rc: %d\n", rc);
1729 lli->lli_opendir_key = NULL;
1730 thread_set_flags(thread, SVC_STOPPED);
1731 thread_set_flags(&sai->sai_agl_thread, SVC_STOPPED);
1733 LASSERT(lli->lli_sai == NULL);
1737 l_wait_event(thread->t_ctl_waitq,
1738 thread_is_running(thread) || thread_is_stopped(thread),
1742 * We don't stat-ahead for the first dirent since we are already in
1750 cfs_spin_lock(&lli->lli_sa_lock);
1751 lli->lli_opendir_key = NULL;
1752 lli->lli_opendir_pid = 0;
1753 cfs_spin_unlock(&lli->lli_sa_lock);