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, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 #include <linux/sched.h>
40 #include <linux/highmem.h>
41 #include <linux/pagemap.h>
43 #define DEBUG_SUBSYSTEM S_LLITE
45 #include <obd_support.h>
46 #include <lustre_dlm.h>
47 #include "llite_internal.h"
49 #define SA_OMITTED_ENTRY_MAX 8ULL
52 /** negative values are for error cases */
53 SA_ENTRY_INIT = 0, /** init entry */
54 SA_ENTRY_SUCC = 1, /** stat succeed */
55 SA_ENTRY_INVA = 2, /** invalid entry */
58 /* sa_entry is not refcounted: statahead thread allocates it and do async stat,
59 * and in async stat callback ll_statahead_interpret() will add it into
60 * sai_cb_entries, later statahead thread will call sa_handle_callback() to
61 * instantiate entry and move it into sai_entries, and then only scanner process
62 * can access and free it. */
64 /* link into sai_cb_entries or sai_entries */
65 struct list_head se_list;
66 /* link into sai hash table locally */
67 struct list_head se_hash;
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 DEFINE_SPINLOCK(sai_generation_lock);
89 static inline int sa_unhashed(struct sa_entry *entry)
91 return list_empty(&entry->se_hash);
95 * The entry only can be released by the caller, it is necessary to hold lock.
97 static inline int sa_ready(struct sa_entry *entry)
100 return (entry->se_state != SA_ENTRY_INIT);
103 static inline int sa_hash(int val)
105 return val & LL_SA_CACHE_MASK;
109 * Insert entry to hash SA table.
112 sa_rehash(struct ll_statahead_info *sai, struct sa_entry *entry)
114 int i = sa_hash(entry->se_qstr.hash);
116 spin_lock(&sai->sai_cache_lock[i]);
117 list_add_tail(&entry->se_hash, &sai->sai_cache[i]);
118 spin_unlock(&sai->sai_cache_lock[i]);
122 * Remove entry from SA table.
125 sa_unhash(struct ll_statahead_info *sai, struct sa_entry *entry)
127 int i = sa_hash(entry->se_qstr.hash);
129 spin_lock(&sai->sai_cache_lock[i]);
130 list_del_init(&entry->se_hash);
131 spin_unlock(&sai->sai_cache_lock[i]);
134 static inline int agl_should_run(struct ll_statahead_info *sai,
137 return (inode != NULL && S_ISREG(inode->i_mode) && sai->sai_agl_valid);
140 static inline struct ll_inode_info *
141 agl_first_entry(struct ll_statahead_info *sai)
143 return list_entry(sai->sai_agls.next, struct ll_inode_info,
147 static inline int sa_sent_full(struct ll_statahead_info *sai)
149 return atomic_read(&sai->sai_cache_count) >= sai->sai_max;
152 static inline int sa_has_callback(struct ll_statahead_info *sai)
154 return !list_empty(&sai->sai_interim_entries);
157 static inline int agl_list_empty(struct ll_statahead_info *sai)
159 return list_empty(&sai->sai_agls);
163 * (1) hit ratio less than 80%
165 * (2) consecutive miss more than 8
166 * then means low hit.
168 static inline int sa_low_hit(struct ll_statahead_info *sai)
170 return ((sai->sai_hit > 7 && sai->sai_hit < 4 * sai->sai_miss) ||
171 (sai->sai_consecutive_miss > 8));
175 * If the given index is behind of statahead window more than
176 * SA_OMITTED_ENTRY_MAX, then it is old.
178 static inline int is_omitted_entry(struct ll_statahead_info *sai, __u64 index)
180 return ((__u64)sai->sai_max + index + SA_OMITTED_ENTRY_MAX <
184 /* allocate sa_entry and add it into hash to let scanner process to find it */
185 static struct sa_entry *
186 sa_alloc(struct ll_statahead_info *sai, __u64 index, const char *name, int len)
188 struct ll_inode_info *lli;
189 struct sa_entry *entry;
194 entry_size = sizeof(struct sa_entry) + (len & ~3) + 4;
195 OBD_ALLOC(entry, entry_size);
196 if (unlikely(entry == NULL))
197 RETURN(ERR_PTR(-ENOMEM));
199 CDEBUG(D_READA, "alloc sa entry %.*s(%p) index "LPU64"\n",
200 len, name, entry, index);
202 entry->se_index = index;
204 entry->se_state = SA_ENTRY_INIT;
205 entry->se_size = entry_size;
206 dname = (char *)entry + sizeof(struct sa_entry);
207 memcpy(dname, name, len);
209 entry->se_qstr.hash = full_name_hash(name, len);
210 entry->se_qstr.len = len;
211 entry->se_qstr.name = dname;
213 lli = ll_i2info(sai->sai_inode);
214 spin_lock(&lli->lli_sa_lock);
215 INIT_LIST_HEAD(&entry->se_list);
216 sa_rehash(sai, entry);
217 spin_unlock(&lli->lli_sa_lock);
219 atomic_inc(&sai->sai_cache_count);
224 /* free sa_entry which should have been unhashed and not in any list */
225 static void sa_free(struct ll_statahead_info *sai, struct sa_entry *entry)
227 CDEBUG(D_READA, "free sa entry %.*s(%p) index "LPU64"\n",
228 entry->se_qstr.len, entry->se_qstr.name, entry,
231 LASSERT(list_empty(&entry->se_list));
232 LASSERT(sa_unhashed(entry));
234 OBD_FREE(entry, entry->se_size);
235 atomic_dec(&sai->sai_cache_count);
238 /* find sa_entry by name, used by directory scanner, lock is not needed because
239 * only scanner can remove the entry from hash.
241 static struct sa_entry *
242 sa_get(struct ll_statahead_info *sai, const struct qstr *qstr)
244 struct sa_entry *entry;
245 int i = sa_hash(qstr->hash);
247 list_for_each_entry(entry, &sai->sai_cache[i], se_hash) {
248 if (entry->se_qstr.hash == qstr->hash &&
249 entry->se_qstr.len == qstr->len &&
250 memcmp(entry->se_qstr.name, qstr->name, qstr->len) == 0)
257 sa_kill(struct ll_statahead_info *sai, struct sa_entry *entry)
259 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
261 LASSERT(!sa_unhashed(entry));
262 LASSERT(!list_empty(&entry->se_list));
263 LASSERT(sa_ready(entry));
265 sa_unhash(sai, entry);
267 spin_lock(&lli->lli_sa_lock);
268 list_del_init(&entry->se_list);
269 spin_unlock(&lli->lli_sa_lock);
271 if (entry->se_inode != NULL)
272 iput(entry->se_inode);
277 /* called by scanner after use, sa_entry will be killed */
279 sa_put(struct ll_statahead_info *sai, struct sa_entry *entry)
281 struct sa_entry *tmp, *next;
283 if (entry != NULL && entry->se_state == SA_ENTRY_SUCC) {
284 struct ll_sb_info *sbi = ll_i2sbi(sai->sai_inode);
287 sai->sai_consecutive_miss = 0;
288 sai->sai_max = min(2 * sai->sai_max, sbi->ll_sa_max);
291 sai->sai_consecutive_miss++;
297 /* kill old completed entries, only scanner process does this, no need
299 list_for_each_entry_safe(tmp, next, &sai->sai_entries, se_list) {
300 if (!is_omitted_entry(sai, tmp->se_index))
305 wake_up(&sai->sai_thread.t_ctl_waitq);
308 /* update state and sort add entry to sai_entries by index, return true if
309 * scanner is waiting on this entry. */
311 __sa_make_ready(struct ll_statahead_info *sai, struct sa_entry *entry, int ret)
314 struct list_head *pos = &sai->sai_entries;
316 LASSERT(!sa_ready(entry));
317 LASSERT(list_empty(&entry->se_list));
319 entry->se_state = ret < 0 ? SA_ENTRY_INVA : SA_ENTRY_SUCC;
321 list_for_each_entry_reverse(se, &sai->sai_entries, se_list) {
322 if (se->se_index < entry->se_index) {
327 list_add(&entry->se_list, pos);
329 return (entry->se_index == sai->sai_index_wait);
332 /* release resources used in async stat RPC, complete entry information and
333 * wakeup if necessary */
335 sa_make_ready(struct ll_statahead_info *sai, struct sa_entry *entry, int ret)
337 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
338 struct md_enqueue_info *minfo = entry->se_minfo;
339 struct ptlrpc_request *req = entry->se_req;
342 /* release resources used in RPC */
344 entry->se_minfo = NULL;
345 ll_intent_release(&minfo->mi_it);
351 entry->se_req = NULL;
352 ptlrpc_req_finished(req);
355 spin_lock(&lli->lli_sa_lock);
356 wakeup = __sa_make_ready(sai, entry, ret);
357 spin_unlock(&lli->lli_sa_lock);
360 wake_up(&sai->sai_waitq);
364 * Insert inode into the list of sai_agls.
366 static void ll_agl_add(struct ll_statahead_info *sai,
367 struct inode *inode, int index)
369 struct ll_inode_info *child = ll_i2info(inode);
370 struct ll_inode_info *parent = ll_i2info(sai->sai_inode);
373 spin_lock(&child->lli_agl_lock);
374 if (child->lli_agl_index == 0) {
375 child->lli_agl_index = index;
376 spin_unlock(&child->lli_agl_lock);
378 LASSERT(list_empty(&child->lli_agl_list));
381 spin_lock(&parent->lli_agl_lock);
382 if (agl_list_empty(sai))
384 list_add_tail(&child->lli_agl_list, &sai->sai_agls);
385 spin_unlock(&parent->lli_agl_lock);
387 spin_unlock(&child->lli_agl_lock);
391 wake_up(&sai->sai_agl_thread.t_ctl_waitq);
394 static struct ll_statahead_info *ll_sai_alloc(void)
396 struct ll_statahead_info *sai;
404 atomic_set(&sai->sai_refcount, 1);
406 spin_lock(&sai_generation_lock);
407 sai->sai_generation = ++sai_generation;
408 if (unlikely(sai_generation == 0))
409 sai->sai_generation = ++sai_generation;
410 spin_unlock(&sai_generation_lock);
412 sai->sai_max = LL_SA_RPC_MIN;
414 init_waitqueue_head(&sai->sai_waitq);
415 init_waitqueue_head(&sai->sai_thread.t_ctl_waitq);
416 init_waitqueue_head(&sai->sai_agl_thread.t_ctl_waitq);
418 INIT_LIST_HEAD(&sai->sai_interim_entries);
419 INIT_LIST_HEAD(&sai->sai_entries);
420 INIT_LIST_HEAD(&sai->sai_agls);
422 for (i = 0; i < LL_SA_CACHE_SIZE; i++) {
423 INIT_LIST_HEAD(&sai->sai_cache[i]);
424 spin_lock_init(&sai->sai_cache_lock[i]);
426 atomic_set(&sai->sai_cache_count, 0);
431 static inline struct ll_statahead_info *ll_sai_get(struct inode *dir)
433 struct ll_inode_info *lli = ll_i2info(dir);
434 struct ll_statahead_info *sai = NULL;
436 spin_lock(&lli->lli_sa_lock);
439 atomic_inc(&sai->sai_refcount);
440 spin_unlock(&lli->lli_sa_lock);
445 static void ll_sai_put(struct ll_statahead_info *sai)
447 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
449 if (atomic_dec_and_lock(&sai->sai_refcount, &lli->lli_sa_lock)) {
450 struct sa_entry *entry, *next;
451 struct ll_sb_info *sbi = ll_i2sbi(sai->sai_inode);
454 spin_unlock(&lli->lli_sa_lock);
456 LASSERT(thread_is_stopped(&sai->sai_thread));
457 LASSERT(thread_is_stopped(&sai->sai_agl_thread));
458 LASSERT(sai->sai_sent == sai->sai_replied);
459 LASSERT(!sa_has_callback(sai));
461 list_for_each_entry_safe(entry, next, &sai->sai_entries,
465 LASSERT(atomic_read(&sai->sai_cache_count) == 0);
466 LASSERT(agl_list_empty(sai));
468 iput(sai->sai_inode);
470 atomic_dec(&sbi->ll_sa_running);
474 /* Do NOT forget to drop inode refcount when into sai_agls. */
475 static void ll_agl_trigger(struct inode *inode, struct ll_statahead_info *sai)
477 struct ll_inode_info *lli = ll_i2info(inode);
478 __u64 index = lli->lli_agl_index;
482 LASSERT(list_empty(&lli->lli_agl_list));
484 /* AGL maybe fall behind statahead with one entry */
485 if (is_omitted_entry(sai, index + 1)) {
486 lli->lli_agl_index = 0;
491 /* Someone is in glimpse (sync or async), do nothing. */
492 rc = down_write_trylock(&lli->lli_glimpse_sem);
494 lli->lli_agl_index = 0;
500 * Someone triggered glimpse within 1 sec before.
501 * 1) The former glimpse succeeded with glimpse lock granted by OST, and
502 * if the lock is still cached on client, AGL needs to do nothing. If
503 * it is cancelled by other client, AGL maybe cannot obtaion new lock
504 * for no glimpse callback triggered by AGL.
505 * 2) The former glimpse succeeded, but OST did not grant glimpse lock.
506 * Under such case, it is quite possible that the OST will not grant
507 * glimpse lock for AGL also.
508 * 3) The former glimpse failed, compared with other two cases, it is
509 * relative rare. AGL can ignore such case, and it will not muchly
510 * affect the performance.
512 if (lli->lli_glimpse_time != 0 &&
513 cfs_time_before(cfs_time_shift(-1), lli->lli_glimpse_time)) {
514 up_write(&lli->lli_glimpse_sem);
515 lli->lli_agl_index = 0;
520 CDEBUG(D_READA, "Handling (init) async glimpse: inode = "
521 DFID", idx = "LPU64"\n", PFID(&lli->lli_fid), index);
524 lli->lli_agl_index = 0;
525 lli->lli_glimpse_time = cfs_time_current();
526 up_write(&lli->lli_glimpse_sem);
528 CDEBUG(D_READA, "Handled (init) async glimpse: inode= "
529 DFID", idx = "LPU64", rc = %d\n",
530 PFID(&lli->lli_fid), index, rc);
537 /* prepare inode for sa entry, add it into agl list, now sa_entry is ready
538 * to be used by scanner process. */
539 static void sa_instantiate(struct ll_statahead_info *sai,
540 struct sa_entry *entry)
542 struct inode *dir = sai->sai_inode;
544 struct md_enqueue_info *minfo;
545 struct lookup_intent *it;
546 struct ptlrpc_request *req;
547 struct mdt_body *body;
551 LASSERT(entry->se_handle != 0);
553 minfo = entry->se_minfo;
556 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
558 GOTO(out, rc = -EFAULT);
560 child = entry->se_inode;
565 LASSERT(fid_is_zero(&minfo->mi_data.op_fid2));
567 /* XXX: No fid in reply, this is probaly cross-ref case.
568 * SA can't handle it yet. */
569 if (body->mbo_valid & OBD_MD_MDS)
570 GOTO(out, rc = -EAGAIN);
575 /* unlinked and re-created with the same name */
576 if (unlikely(!lu_fid_eq(&minfo->mi_data.op_fid2,
578 entry->se_inode = NULL;
584 it->d.lustre.it_lock_handle = entry->se_handle;
585 rc = md_revalidate_lock(ll_i2mdexp(dir), it, ll_inode2fid(dir), NULL);
587 GOTO(out, rc = -EAGAIN);
589 rc = ll_prep_inode(&child, req, dir->i_sb, it);
593 CDEBUG(D_READA, "%s: setting %.*s"DFID" l_data to inode %p\n",
594 ll_get_fsname(child->i_sb, NULL, 0),
595 entry->se_qstr.len, entry->se_qstr.name,
596 PFID(ll_inode2fid(child)), child);
597 ll_set_lock_data(ll_i2sbi(dir)->ll_md_exp, child, it, NULL);
599 entry->se_inode = child;
601 if (agl_should_run(sai, child))
602 ll_agl_add(sai, child, entry->se_index);
607 /* sa_make_ready() will drop ldlm ibits lock refcount by calling
608 * ll_intent_drop_lock() in spite of failures. Do not worry about
609 * calling ll_intent_drop_lock() more than once. */
610 sa_make_ready(sai, entry, rc);
613 /* once there are async stat replies, instantiate sa_entry */
614 static void sa_handle_callback(struct ll_statahead_info *sai)
616 struct ll_inode_info *lli;
618 lli = ll_i2info(sai->sai_inode);
620 while (sa_has_callback(sai)) {
621 struct sa_entry *entry;
623 spin_lock(&lli->lli_sa_lock);
624 if (unlikely(!sa_has_callback(sai))) {
625 spin_unlock(&lli->lli_sa_lock);
628 entry = list_entry(sai->sai_interim_entries.next,
629 struct sa_entry, se_list);
630 list_del_init(&entry->se_list);
631 spin_unlock(&lli->lli_sa_lock);
633 sa_instantiate(sai, entry);
636 spin_lock(&lli->lli_agl_lock);
637 while (!agl_list_empty(sai)) {
638 struct ll_inode_info *clli;
640 clli = agl_first_entry(sai);
641 list_del_init(&clli->lli_agl_list);
642 spin_unlock(&lli->lli_agl_lock);
644 ll_agl_trigger(&clli->lli_vfs_inode, sai);
646 spin_lock(&lli->lli_agl_lock);
648 spin_unlock(&lli->lli_agl_lock);
651 /* callback for async stat, because this is called in ptlrpcd context, we only
652 * put sa_entry in sai_cb_entries list, and let sa_handle_callback() to really
653 * prepare inode and instantiate sa_entry later. */
654 static int ll_statahead_interpret(struct ptlrpc_request *req,
655 struct md_enqueue_info *minfo, int rc)
657 struct lookup_intent *it = &minfo->mi_it;
658 struct inode *dir = minfo->mi_dir;
659 struct ll_inode_info *lli = ll_i2info(dir);
660 struct ll_statahead_info *sai = lli->lli_sai;
661 struct sa_entry *entry = (struct sa_entry *)minfo->mi_cbdata;
665 if (it_disposition(it, DISP_LOOKUP_NEG))
668 /* because statahead thread will wait for all inflight RPC to finish,
669 * sai should be always valid, no need to refcount */
670 LASSERT(sai != NULL);
671 LASSERT(!thread_is_stopped(&sai->sai_thread));
672 LASSERT(entry != NULL);
674 CDEBUG(D_READA, "sa_entry %.*s rc %d\n",
675 entry->se_qstr.len, entry->se_qstr.name, rc);
677 spin_lock(&lli->lli_sa_lock);
679 wakeup = __sa_make_ready(sai, entry, rc);
681 entry->se_minfo = minfo;
682 entry->se_req = ptlrpc_request_addref(req);
683 /* Release the async ibits lock ASAP to avoid deadlock
684 * when statahead thread tries to enqueue lock on parent
685 * for readpage and other tries to enqueue lock on child
686 * with parent's lock held, for example: unlink. */
687 entry->se_handle = it->d.lustre.it_lock_handle;
688 ll_intent_drop_lock(it);
689 wakeup = !sa_has_callback(sai);
690 list_add_tail(&entry->se_list, &sai->sai_interim_entries);
694 wake_up(&sai->sai_thread.t_ctl_waitq);
695 spin_unlock(&lli->lli_sa_lock);
698 ll_intent_release(it);
705 static void sa_fini_data(struct md_enqueue_info *minfo,
706 struct ldlm_enqueue_info *einfo)
708 LASSERT(minfo && einfo);
710 capa_put(minfo->mi_data.op_capa1);
711 capa_put(minfo->mi_data.op_capa2);
717 * There is race condition between "capa_put" and "ll_statahead_interpret" for
718 * accessing "op_data.op_capa[1,2]" as following:
719 * "capa_put" releases "op_data.op_capa[1,2]"'s reference count after calling
720 * "md_intent_getattr_async". But "ll_statahead_interpret" maybe run first, and
721 * fill "op_data.op_capa[1,2]" as POISON, then cause "capa_put" access invalid
722 * "ocapa". So here reserve "op_data.op_capa[1,2]" in "pcapa" before calling
723 * "md_intent_getattr_async".
725 static int sa_prep_data(struct inode *dir, struct inode *child,
726 struct sa_entry *entry, struct md_enqueue_info **pmi,
727 struct ldlm_enqueue_info **pei,
728 struct obd_capa **pcapa)
730 struct qstr *qstr = &entry->se_qstr;
731 struct md_enqueue_info *minfo;
732 struct ldlm_enqueue_info *einfo;
733 struct md_op_data *op_data;
735 OBD_ALLOC_PTR(einfo);
739 OBD_ALLOC_PTR(minfo);
745 op_data = ll_prep_md_op_data(&minfo->mi_data, dir, child, qstr->name,
746 qstr->len, 0, LUSTRE_OPC_ANY, NULL);
747 if (IS_ERR(op_data)) {
750 return PTR_ERR(op_data);
753 minfo->mi_it.it_op = IT_GETATTR;
754 minfo->mi_dir = igrab(dir);
755 minfo->mi_cb = ll_statahead_interpret;
756 minfo->mi_cbdata = entry;
758 einfo->ei_type = LDLM_IBITS;
759 einfo->ei_mode = it_to_lock_mode(&minfo->mi_it);
760 einfo->ei_cb_bl = ll_md_blocking_ast;
761 einfo->ei_cb_cp = ldlm_completion_ast;
762 einfo->ei_cb_gl = NULL;
763 einfo->ei_cbdata = NULL;
767 pcapa[0] = op_data->op_capa1;
768 pcapa[1] = op_data->op_capa2;
773 static int sa_lookup(struct inode *dir, struct sa_entry *entry)
775 struct md_enqueue_info *minfo;
776 struct ldlm_enqueue_info *einfo;
777 struct obd_capa *capas[2];
781 rc = sa_prep_data(dir, NULL, entry, &minfo, &einfo, capas);
785 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
790 sa_fini_data(minfo, einfo);
797 * similar to ll_revalidate_it().
798 * \retval 1 -- dentry valid
799 * \retval 0 -- will send stat-ahead request
800 * \retval others -- prepare stat-ahead request failed
802 static int sa_revalidate(struct inode *dir, struct sa_entry *entry,
803 struct dentry *dentry)
805 struct inode *inode = dentry->d_inode;
806 struct lookup_intent it = { .it_op = IT_GETATTR,
807 .d.lustre.it_lock_handle = 0 };
808 struct md_enqueue_info *minfo;
809 struct ldlm_enqueue_info *einfo;
810 struct obd_capa *capas[2];
814 if (unlikely(inode == NULL))
817 if (d_mountpoint(dentry))
820 entry->se_inode = igrab(inode);
821 rc = md_revalidate_lock(ll_i2mdexp(dir), &it, ll_inode2fid(inode),
824 entry->se_handle = it.d.lustre.it_lock_handle;
825 ll_intent_release(&it);
829 rc = sa_prep_data(dir, inode, entry, &minfo, &einfo, capas);
831 entry->se_inode = NULL;
836 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
841 entry->se_inode = NULL;
843 sa_fini_data(minfo, einfo);
849 static void sa_statahead(struct dentry *parent, const char *name, int len)
851 struct inode *dir = parent->d_inode;
852 struct ll_inode_info *lli = ll_i2info(dir);
853 struct ll_statahead_info *sai = lli->lli_sai;
854 struct dentry *dentry = NULL;
855 struct sa_entry *entry;
859 entry = sa_alloc(sai, sai->sai_index, name, len);
863 dentry = d_lookup(parent, &entry->se_qstr);
865 rc = sa_lookup(dir, entry);
867 rc = sa_revalidate(dir, entry, dentry);
868 if (rc == 1 && agl_should_run(sai, dentry->d_inode))
869 ll_agl_add(sai, dentry->d_inode, entry->se_index);
876 sa_make_ready(sai, entry, rc);
885 static int ll_agl_thread(void *arg)
887 struct dentry *parent = (struct dentry *)arg;
888 struct inode *dir = parent->d_inode;
889 struct ll_inode_info *plli = ll_i2info(dir);
890 struct ll_inode_info *clli;
891 struct ll_sb_info *sbi = ll_i2sbi(dir);
892 struct ll_statahead_info *sai;
893 struct ptlrpc_thread *thread;
894 struct l_wait_info lwi = { 0 };
898 sai = ll_sai_get(dir);
899 thread = &sai->sai_agl_thread;
900 thread->t_pid = current_pid();
901 CDEBUG(D_READA, "agl thread started: sai %p, parent %.*s\n",
902 sai, parent->d_name.len, parent->d_name.name);
904 atomic_inc(&sbi->ll_agl_total);
905 spin_lock(&plli->lli_agl_lock);
906 sai->sai_agl_valid = 1;
907 if (thread_is_init(thread))
908 /* If someone else has changed the thread state
909 * (e.g. already changed to SVC_STOPPING), we can't just
910 * blindly overwrite that setting. */
911 thread_set_flags(thread, SVC_RUNNING);
912 spin_unlock(&plli->lli_agl_lock);
913 wake_up(&thread->t_ctl_waitq);
916 l_wait_event(thread->t_ctl_waitq,
917 !agl_list_empty(sai) ||
918 !thread_is_running(thread),
921 if (!thread_is_running(thread))
924 spin_lock(&plli->lli_agl_lock);
925 /* The statahead thread maybe help to process AGL entries,
926 * so check whether list empty again. */
927 if (!agl_list_empty(sai)) {
928 clli = agl_first_entry(sai);
929 list_del_init(&clli->lli_agl_list);
930 spin_unlock(&plli->lli_agl_lock);
931 ll_agl_trigger(&clli->lli_vfs_inode, sai);
933 spin_unlock(&plli->lli_agl_lock);
937 spin_lock(&plli->lli_agl_lock);
938 sai->sai_agl_valid = 0;
939 while (!agl_list_empty(sai)) {
940 clli = agl_first_entry(sai);
941 list_del_init(&clli->lli_agl_list);
942 spin_unlock(&plli->lli_agl_lock);
943 clli->lli_agl_index = 0;
944 iput(&clli->lli_vfs_inode);
945 spin_lock(&plli->lli_agl_lock);
947 thread_set_flags(thread, SVC_STOPPED);
948 spin_unlock(&plli->lli_agl_lock);
949 wake_up(&thread->t_ctl_waitq);
951 CDEBUG(D_READA, "agl thread stopped: sai %p, parent %.*s\n",
952 sai, parent->d_name.len, parent->d_name.name);
956 static void ll_start_agl(struct dentry *parent, struct ll_statahead_info *sai)
958 struct ptlrpc_thread *thread = &sai->sai_agl_thread;
959 struct l_wait_info lwi = { 0 };
960 struct ll_inode_info *plli;
961 struct task_struct *task;
964 CDEBUG(D_READA, "start agl thread: sai %p, parent %.*s\n",
965 sai, parent->d_name.len, parent->d_name.name);
967 plli = ll_i2info(parent->d_inode);
968 task = kthread_run(ll_agl_thread, parent,
969 "ll_agl_%u", plli->lli_opendir_pid);
971 CERROR("can't start ll_agl thread, rc: %ld\n", PTR_ERR(task));
972 thread_set_flags(thread, SVC_STOPPED);
976 l_wait_event(thread->t_ctl_waitq,
977 thread_is_running(thread) || thread_is_stopped(thread),
982 static int ll_statahead_thread(void *arg)
984 struct dentry *parent = (struct dentry *)arg;
985 struct inode *dir = parent->d_inode;
986 struct ll_inode_info *lli = ll_i2info(dir);
987 struct ll_sb_info *sbi = ll_i2sbi(dir);
988 struct ll_statahead_info *sai;
989 struct ptlrpc_thread *thread;
990 struct ptlrpc_thread *agl_thread;
992 struct md_op_data *op_data;
993 struct ll_dir_chain chain;
994 struct l_wait_info lwi = { 0 };
995 struct page *page = NULL;
1000 sai = ll_sai_get(dir);
1001 thread = &sai->sai_thread;
1002 agl_thread = &sai->sai_agl_thread;
1003 thread->t_pid = current_pid();
1004 CDEBUG(D_READA, "statahead thread starting: sai %p, parent %.*s\n",
1005 sai, parent->d_name.len, parent->d_name.name);
1007 op_data = ll_prep_md_op_data(NULL, dir, dir, NULL, 0, 0,
1008 LUSTRE_OPC_ANY, dir);
1009 if (IS_ERR(op_data))
1010 GOTO(out, rc = PTR_ERR(op_data));
1012 op_data->op_max_pages = ll_i2sbi(dir)->ll_md_brw_pages;
1014 if (sbi->ll_flags & LL_SBI_AGL_ENABLED)
1015 ll_start_agl(parent, sai);
1017 atomic_inc(&sbi->ll_sa_total);
1018 spin_lock(&lli->lli_sa_lock);
1019 if (thread_is_init(thread))
1020 /* If someone else has changed the thread state
1021 * (e.g. already changed to SVC_STOPPING), we can't just
1022 * blindly overwrite that setting. */
1023 thread_set_flags(thread, SVC_RUNNING);
1024 spin_unlock(&lli->lli_sa_lock);
1025 wake_up(&thread->t_ctl_waitq);
1027 ll_dir_chain_init(&chain);
1028 while (pos != MDS_DIR_END_OFF && thread_is_running(thread)) {
1029 struct lu_dirpage *dp;
1030 struct lu_dirent *ent;
1032 sai->sai_in_readpage = 1;
1033 page = ll_get_dir_page(dir, op_data, pos, &chain);
1034 sai->sai_in_readpage = 0;
1037 CDEBUG(D_READA, "error reading dir "DFID" at "LPU64
1038 "/"LPU64" opendir_pid = %u: rc = %d\n",
1039 PFID(ll_inode2fid(dir)), pos, sai->sai_index,
1040 lli->lli_opendir_pid, rc);
1044 dp = page_address(page);
1045 for (ent = lu_dirent_start(dp);
1046 ent != NULL && thread_is_running(thread) &&
1048 ent = lu_dirent_next(ent)) {
1053 hash = le64_to_cpu(ent->lde_hash);
1054 if (unlikely(hash < pos))
1056 * Skip until we find target hash value.
1060 namelen = le16_to_cpu(ent->lde_namelen);
1061 if (unlikely(namelen == 0))
1063 * Skip dummy record.
1067 name = ent->lde_name;
1068 if (name[0] == '.') {
1074 } else if (name[1] == '.' && namelen == 2) {
1079 } else if (!sai->sai_ls_all) {
1081 * skip hidden files.
1083 sai->sai_skip_hidden++;
1089 * don't stat-ahead first entry.
1091 if (unlikely(++first == 1))
1094 /* wait for spare statahead window */
1096 l_wait_event(thread->t_ctl_waitq,
1097 !sa_sent_full(sai) ||
1098 sa_has_callback(sai) ||
1099 !agl_list_empty(sai) ||
1100 !thread_is_running(thread),
1103 sa_handle_callback(sai);
1104 } while (sa_sent_full(sai) &&
1105 thread_is_running(thread));
1107 sa_statahead(parent, name, namelen);
1110 pos = le64_to_cpu(dp->ldp_hash_end);
1111 ll_release_page(dir, page,
1112 le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1114 if (sa_low_hit(sai)) {
1116 atomic_inc(&sbi->ll_sa_wrong);
1117 CDEBUG(D_READA, "Statahead for dir "DFID" hit "
1118 "ratio too low: hit/miss "LPU64"/"LPU64
1119 ", sent/replied "LPU64"/"LPU64", stopping "
1120 "statahead thread: pid %d\n",
1121 PFID(&lli->lli_fid), sai->sai_hit,
1122 sai->sai_miss, sai->sai_sent,
1123 sai->sai_replied, current_pid());
1127 ll_dir_chain_fini(&chain);
1128 ll_finish_md_op_data(op_data);
1131 spin_lock(&lli->lli_sa_lock);
1132 thread_set_flags(thread, SVC_STOPPING);
1133 lli->lli_sa_enabled = 0;
1134 spin_unlock(&lli->lli_sa_lock);
1137 /* statahead is finished, but statahead entries need to be cached, wait
1138 * for file release to stop me. */
1139 while (thread_is_running(thread)) {
1140 l_wait_event(thread->t_ctl_waitq,
1141 sa_has_callback(sai) ||
1142 !agl_list_empty(sai) ||
1143 !thread_is_running(thread),
1146 sa_handle_callback(sai);
1151 if (sai->sai_agl_valid) {
1152 spin_lock(&lli->lli_agl_lock);
1153 thread_set_flags(agl_thread, SVC_STOPPING);
1154 spin_unlock(&lli->lli_agl_lock);
1155 wake_up(&agl_thread->t_ctl_waitq);
1157 CDEBUG(D_READA, "stop agl thread: sai %p pid %u\n",
1158 sai, (unsigned int)agl_thread->t_pid);
1159 l_wait_event(agl_thread->t_ctl_waitq,
1160 thread_is_stopped(agl_thread),
1163 /* Set agl_thread flags anyway. */
1164 thread_set_flags(agl_thread, SVC_STOPPED);
1167 /* wait for inflight statahead RPCs to finish, and then we can free sai
1168 * safely because statahead RPC will access sai data */
1169 while (sai->sai_sent != sai->sai_replied) {
1170 /* in case we're not woken up, timeout wait */
1171 lwi = LWI_TIMEOUT(HZ >> 3, NULL, NULL);
1172 l_wait_event(thread->t_ctl_waitq,
1173 sai->sai_sent == sai->sai_replied, &lwi);
1176 /* release resources held by statahead RPCs */
1177 sa_handle_callback(sai);
1179 spin_lock(&lli->lli_sa_lock);
1180 thread_set_flags(thread, SVC_STOPPED);
1181 spin_unlock(&lli->lli_sa_lock);
1183 wake_up(&sai->sai_waitq);
1184 wake_up(&thread->t_ctl_waitq);
1186 CDEBUG(D_READA, "statahead thread stopped: sai %p, parent %.*s\n",
1187 sai, parent->d_name.len, parent->d_name.name);
1192 /* authorize opened dir handle @key to statahead later */
1193 void ll_authorize_statahead(struct inode *dir, void *key)
1195 struct ll_inode_info *lli = ll_i2info(dir);
1197 spin_lock(&lli->lli_sa_lock);
1198 if (lli->lli_opendir_key == NULL && lli->lli_sai == NULL) {
1200 * if lli_sai is not NULL, it means previous statahead is not
1201 * finished yet, we'd better not start a new statahead for now.
1203 LASSERT(lli->lli_opendir_pid == 0);
1204 lli->lli_opendir_key = key;
1205 lli->lli_opendir_pid = current_pid();
1206 lli->lli_sa_enabled = 1;
1208 spin_unlock(&lli->lli_sa_lock);
1211 /* deauthorize opened dir handle @key to statahead, but statahead thread may
1212 * still be running, notify it to quit. */
1213 void ll_deauthorize_statahead(struct inode *dir, void *key)
1215 struct ll_inode_info *lli = ll_i2info(dir);
1216 struct ll_statahead_info *sai;
1218 LASSERT(lli->lli_opendir_key == key);
1219 LASSERT(lli->lli_opendir_pid != 0);
1221 CDEBUG(D_READA, "deauthorize statahead for "DFID"\n",
1222 PFID(&lli->lli_fid));
1224 spin_lock(&lli->lli_sa_lock);
1225 lli->lli_opendir_key = NULL;
1226 lli->lli_opendir_pid = 0;
1227 lli->lli_sa_enabled = 0;
1229 if (sai != NULL && thread_is_running(&sai->sai_thread)) {
1231 * statahead thread may not quit yet because it needs to cache
1232 * entries, now it's time to tell it to quit.
1234 thread_set_flags(&sai->sai_thread, SVC_STOPPING);
1235 wake_up(&sai->sai_thread.t_ctl_waitq);
1237 spin_unlock(&lli->lli_sa_lock);
1242 * not first dirent, or is "."
1244 LS_NONE_FIRST_DE = 0,
1246 * the first non-hidden dirent
1250 * the first hidden dirent, that is "."
1255 static int is_first_dirent(struct inode *dir, struct dentry *dentry)
1257 struct ll_dir_chain chain;
1258 struct qstr *target = &dentry->d_name;
1259 struct md_op_data *op_data;
1261 struct page *page = NULL;
1262 int rc = LS_NONE_FIRST_DE;
1266 op_data = ll_prep_md_op_data(NULL, dir, dir, NULL, 0, 0,
1267 LUSTRE_OPC_ANY, dir);
1268 if (IS_ERR(op_data))
1269 RETURN(PTR_ERR(op_data));
1271 *FIXME choose the start offset of the readdir
1273 op_data->op_stripe_offset = 0;
1274 op_data->op_max_pages = ll_i2sbi(dir)->ll_md_brw_pages;
1276 ll_dir_chain_init(&chain);
1277 page = ll_get_dir_page(dir, op_data, 0, &chain);
1280 struct lu_dirpage *dp;
1281 struct lu_dirent *ent;
1284 struct ll_inode_info *lli = ll_i2info(dir);
1287 CERROR("%s: reading dir "DFID" at "LPU64
1288 "opendir_pid = %u : rc = %d\n",
1289 ll_get_fsname(dir->i_sb, NULL, 0),
1290 PFID(ll_inode2fid(dir)), pos,
1291 lli->lli_opendir_pid, rc);
1295 dp = page_address(page);
1296 for (ent = lu_dirent_start(dp); ent != NULL;
1297 ent = lu_dirent_next(ent)) {
1302 hash = le64_to_cpu(ent->lde_hash);
1303 /* The ll_get_dir_page() can return any page containing
1304 * the given hash which may be not the start hash. */
1305 if (unlikely(hash < pos))
1308 namelen = le16_to_cpu(ent->lde_namelen);
1309 if (unlikely(namelen == 0))
1311 * skip dummy record.
1315 name = ent->lde_name;
1316 if (name[0] == '.') {
1322 else if (name[1] == '.' && namelen == 2)
1333 if (dot_de && target->name[0] != '.') {
1334 CDEBUG(D_READA, "%.*s skip hidden file %.*s\n",
1335 target->len, target->name,
1340 if (target->len != namelen ||
1341 memcmp(target->name, name, namelen) != 0)
1342 rc = LS_NONE_FIRST_DE;
1346 rc = LS_FIRST_DOT_DE;
1348 ll_release_page(dir, page, false);
1351 pos = le64_to_cpu(dp->ldp_hash_end);
1352 if (pos == MDS_DIR_END_OFF) {
1354 * End of directory reached.
1356 ll_release_page(dir, page, false);
1360 * chain is exhausted
1361 * Normal case: continue to the next page.
1363 ll_release_page(dir, page, le32_to_cpu(dp->ldp_flags) &
1365 page = ll_get_dir_page(dir, op_data, pos, &chain);
1370 ll_dir_chain_fini(&chain);
1371 ll_finish_md_op_data(op_data);
1375 static int revalidate_statahead_dentry(struct inode *dir,
1376 struct ll_statahead_info *sai,
1377 struct dentry **dentryp,
1380 struct sa_entry *entry = NULL;
1381 struct l_wait_info lwi = { 0 };
1385 if ((*dentryp)->d_name.name[0] == '.') {
1386 if (sai->sai_ls_all ||
1387 sai->sai_miss_hidden >= sai->sai_skip_hidden) {
1389 * Hidden dentry is the first one, or statahead
1390 * thread does not skip so many hidden dentries
1391 * before "sai_ls_all" enabled as below.
1394 if (!sai->sai_ls_all)
1396 * It maybe because hidden dentry is not
1397 * the first one, "sai_ls_all" was not
1398 * set, then "ls -al" missed. Enable
1399 * "sai_ls_all" for such case.
1401 sai->sai_ls_all = 1;
1404 * Such "getattr" has been skipped before
1405 * "sai_ls_all" enabled as above.
1407 sai->sai_miss_hidden++;
1412 entry = sa_get(sai, &(*dentryp)->d_name);
1413 if (entry == NULL || only_unplug) {
1415 RETURN(entry ? 1 : -EAGAIN);
1418 /* if statahead is busy in readdir, help it do post-work */
1419 if (!sa_ready(entry) && sai->sai_in_readpage)
1420 sa_handle_callback(sai);
1422 if (!sa_ready(entry)) {
1423 sai->sai_index_wait = entry->se_index;
1424 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(30), NULL,
1425 LWI_ON_SIGNAL_NOOP, NULL);
1426 rc = l_wait_event(sai->sai_waitq,
1428 thread_is_stopped(&sai->sai_thread),
1436 if (entry->se_state == SA_ENTRY_SUCC && entry->se_inode != NULL) {
1437 struct inode *inode = entry->se_inode;
1438 struct lookup_intent it = { .it_op = IT_GETATTR,
1439 .d.lustre.it_lock_handle =
1443 rc = md_revalidate_lock(ll_i2mdexp(dir), &it,
1444 ll_inode2fid(inode), &bits);
1446 if ((*dentryp)->d_inode == NULL) {
1447 struct dentry *alias;
1449 alias = ll_splice_alias(inode, *dentryp);
1450 if (IS_ERR(alias)) {
1452 RETURN(PTR_ERR(alias));
1455 } else if ((*dentryp)->d_inode != inode) {
1456 /* revalidate, but inode is recreated */
1458 "%s: stale dentry %.*s inode "
1459 DFID", statahead inode "DFID
1461 ll_get_fsname((*dentryp)->d_inode->i_sb,
1463 (*dentryp)->d_name.len,
1464 (*dentryp)->d_name.name,
1465 PFID(ll_inode2fid((*dentryp)->d_inode)),
1466 PFID(ll_inode2fid(inode)));
1472 entry->se_inode = NULL;
1474 if ((bits & MDS_INODELOCK_LOOKUP) &&
1475 d_lustre_invalid(*dentryp))
1476 d_lustre_revalidate(*dentryp);
1477 ll_intent_release(&it);
1485 static int start_statahead_thread(struct inode *dir, struct dentry *dentry)
1487 struct ll_inode_info *lli = ll_i2info(dir);
1488 struct ll_statahead_info *sai = NULL;
1489 struct dentry *parent;
1490 struct ptlrpc_thread *thread;
1491 struct l_wait_info lwi = { 0 };
1492 struct task_struct *task;
1496 /* I am the "lli_opendir_pid" owner, only me can set "lli_sai". */
1497 rc = is_first_dirent(dir, dentry);
1498 if (rc == LS_NONE_FIRST_DE)
1499 /* It is not "ls -{a}l" operation, no need statahead for it. */
1500 GOTO(out, rc = -EAGAIN);
1502 sai = ll_sai_alloc();
1504 GOTO(out, rc = -ENOMEM);
1506 sai->sai_ls_all = (rc == LS_FIRST_DOT_DE);
1507 sai->sai_inode = igrab(dir);
1508 if (unlikely(sai->sai_inode == NULL)) {
1509 CWARN("Do not start stat ahead on dying inode "DFID"\n",
1510 PFID(&lli->lli_fid));
1511 GOTO(out, rc = -ESTALE);
1514 /* get parent reference count here, and put it in ll_statahead_thread */
1515 parent = dget(dentry->d_parent);
1516 if (unlikely(sai->sai_inode != parent->d_inode)) {
1517 struct ll_inode_info *nlli = ll_i2info(parent->d_inode);
1519 CWARN("Race condition, someone changed %.*s just now: "
1520 "old parent "DFID", new parent "DFID"\n",
1521 dentry->d_name.len, dentry->d_name.name,
1522 PFID(&lli->lli_fid), PFID(&nlli->lli_fid));
1524 iput(sai->sai_inode);
1525 GOTO(out, rc = -EAGAIN);
1528 CDEBUG(D_READA, "start statahead thread: sai %p, parent %.*s\n",
1529 sai, parent->d_name.len, parent->d_name.name);
1533 task = kthread_run(ll_statahead_thread, parent, "ll_sa_%u",
1534 lli->lli_opendir_pid);
1535 thread = &sai->sai_thread;
1538 CERROR("cannot start ll_sa thread: rc = %d\n", rc);
1540 lli->lli_opendir_key = NULL;
1541 thread_set_flags(thread, SVC_STOPPED);
1542 thread_set_flags(&sai->sai_agl_thread, SVC_STOPPED);
1544 LASSERT(lli->lli_sai == NULL);
1548 l_wait_event(thread->t_ctl_waitq,
1549 thread_is_running(thread) || thread_is_stopped(thread),
1551 atomic_inc(&ll_i2sbi(parent->d_inode)->ll_sa_running);
1555 * We don't stat-ahead for the first dirent since we are already in
1563 spin_lock(&lli->lli_sa_lock);
1564 lli->lli_opendir_key = NULL;
1565 lli->lli_opendir_pid = 0;
1566 lli->lli_sa_enabled = 0;
1567 spin_unlock(&lli->lli_sa_lock);
1573 * Start statahead thread if this is the first dir entry.
1574 * Otherwise if a thread is started already, wait it until it is ahead of me.
1575 * \retval 1 -- find entry with lock in cache, the caller needs to do
1577 * \retval 0 -- find entry in cache, but without lock, the caller needs
1579 * \retval others -- the caller need to process as non-statahead.
1581 int do_statahead_enter(struct inode *dir, struct dentry **dentryp,
1584 struct ll_statahead_info *sai;
1586 sai = ll_sai_get(dir);
1590 rc = revalidate_statahead_dentry(dir, sai, dentryp,
1592 CDEBUG(D_READA, "revalidate statahead %.*s: %d.\n",
1593 (*dentryp)->d_name.len, (*dentryp)->d_name.name, rc);
1598 return start_statahead_thread(dir, *dentryp);