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, 2014, 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>
39 #include <linux/kthread.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_dlm.h>
48 #include "llite_internal.h"
50 #define SA_OMITTED_ENTRY_MAX 8ULL
53 /** negative values are for error cases */
54 SA_ENTRY_INIT = 0, /** init entry */
55 SA_ENTRY_SUCC = 1, /** stat succeed */
56 SA_ENTRY_INVA = 2, /** invalid entry */
59 /* sa_entry is not refcounted: statahead thread allocates it and do async stat,
60 * and in async stat callback ll_statahead_interpret() will add it into
61 * sai_interim_entries, later statahead thread will call sa_handle_callback() to
62 * instantiate entry and move it into sai_entries, and then only scanner process
63 * can access and free it. */
65 /* link into sai_interim_entries or sai_entries */
66 struct list_head se_list;
67 /* link into sai hash table locally */
68 struct list_head se_hash;
69 /* entry index in the sai */
71 /* low layer ldlm lock handle */
75 /* entry size, contains name */
77 /* pointer to async getattr enqueue info */
78 struct md_enqueue_info *se_minfo;
79 /* pointer to the async getattr request */
80 struct ptlrpc_request *se_req;
81 /* pointer to the target inode */
82 struct inode *se_inode;
89 static unsigned int sai_generation = 0;
90 static DEFINE_SPINLOCK(sai_generation_lock);
92 static inline int sa_unhashed(struct sa_entry *entry)
94 return list_empty(&entry->se_hash);
97 /* sa_entry is ready to use */
98 static inline int sa_ready(struct sa_entry *entry)
101 return (entry->se_state != SA_ENTRY_INIT);
104 /* hash value to put in sai_cache */
105 static inline int sa_hash(int val)
107 return val & LL_SA_CACHE_MASK;
110 /* hash entry into sai_cache */
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]);
121 /* unhash entry from sai_cache */
123 sa_unhash(struct ll_statahead_info *sai, struct sa_entry *entry)
125 int i = sa_hash(entry->se_qstr.hash);
127 spin_lock(&sai->sai_cache_lock[i]);
128 list_del_init(&entry->se_hash);
129 spin_unlock(&sai->sai_cache_lock[i]);
132 static inline int agl_should_run(struct ll_statahead_info *sai,
135 return (inode != NULL && S_ISREG(inode->i_mode) && sai->sai_agl_valid);
138 static inline struct ll_inode_info *
139 agl_first_entry(struct ll_statahead_info *sai)
141 return list_entry(sai->sai_agls.next, struct ll_inode_info,
145 /* statahead window is full */
146 static inline int sa_sent_full(struct ll_statahead_info *sai)
148 return atomic_read(&sai->sai_cache_count) >= sai->sai_max;
151 /* got async stat replies */
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 hash it to allow 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,
187 const struct lu_fid *fid)
189 struct ll_inode_info *lli;
190 struct sa_entry *entry;
195 entry_size = sizeof(struct sa_entry) + (len & ~3) + 4;
196 OBD_ALLOC(entry, entry_size);
197 if (unlikely(entry == NULL))
198 RETURN(ERR_PTR(-ENOMEM));
200 CDEBUG(D_READA, "alloc sa entry %.*s(%p) index "LPU64"\n",
201 len, name, entry, index);
203 entry->se_index = index;
205 entry->se_state = SA_ENTRY_INIT;
206 entry->se_size = entry_size;
207 dname = (char *)entry + sizeof(struct sa_entry);
208 memcpy(dname, name, len);
210 entry->se_qstr.hash = full_name_hash(name, len);
211 entry->se_qstr.len = len;
212 entry->se_qstr.name = dname;
213 entry->se_fid = *fid;
215 lli = ll_i2info(sai->sai_dentry->d_inode);
217 spin_lock(&lli->lli_sa_lock);
218 INIT_LIST_HEAD(&entry->se_list);
219 sa_rehash(sai, entry);
220 spin_unlock(&lli->lli_sa_lock);
222 atomic_inc(&sai->sai_cache_count);
227 /* free sa_entry, which should have been unhashed and not in any list */
228 static void sa_free(struct ll_statahead_info *sai, struct sa_entry *entry)
230 CDEBUG(D_READA, "free sa entry %.*s(%p) index "LPU64"\n",
231 entry->se_qstr.len, entry->se_qstr.name, entry,
234 LASSERT(list_empty(&entry->se_list));
235 LASSERT(sa_unhashed(entry));
237 OBD_FREE(entry, entry->se_size);
238 atomic_dec(&sai->sai_cache_count);
242 * find sa_entry by name, used by directory scanner, lock is not needed because
243 * only scanner can remove the entry from cache.
245 static struct sa_entry *
246 sa_get(struct ll_statahead_info *sai, const struct qstr *qstr)
248 struct sa_entry *entry;
249 int i = sa_hash(qstr->hash);
251 list_for_each_entry(entry, &sai->sai_cache[i], se_hash) {
252 if (entry->se_qstr.hash == qstr->hash &&
253 entry->se_qstr.len == qstr->len &&
254 memcmp(entry->se_qstr.name, qstr->name, qstr->len) == 0)
260 /* unhash and unlink sa_entry, and then free it */
262 sa_kill(struct ll_statahead_info *sai, struct sa_entry *entry)
264 struct ll_inode_info *lli = ll_i2info(sai->sai_dentry->d_inode);
266 LASSERT(!sa_unhashed(entry));
267 LASSERT(!list_empty(&entry->se_list));
268 LASSERT(sa_ready(entry));
270 sa_unhash(sai, entry);
272 spin_lock(&lli->lli_sa_lock);
273 list_del_init(&entry->se_list);
274 spin_unlock(&lli->lli_sa_lock);
276 if (entry->se_inode != NULL)
277 iput(entry->se_inode);
282 /* called by scanner after use, sa_entry will be killed */
284 sa_put(struct ll_statahead_info *sai, struct sa_entry *entry)
286 struct sa_entry *tmp, *next;
288 if (entry != NULL && entry->se_state == SA_ENTRY_SUCC) {
289 struct ll_sb_info *sbi = ll_i2sbi(sai->sai_dentry->d_inode);
292 sai->sai_consecutive_miss = 0;
293 sai->sai_max = min(2 * sai->sai_max, sbi->ll_sa_max);
296 sai->sai_consecutive_miss++;
302 /* kill old completed entries, only scanner process does this, no need
304 list_for_each_entry_safe(tmp, next, &sai->sai_entries, se_list) {
305 if (!is_omitted_entry(sai, tmp->se_index))
310 wake_up(&sai->sai_thread.t_ctl_waitq);
313 /* update state and sort add entry to sai_entries by index, return true if
314 * scanner is waiting on this entry. */
316 __sa_make_ready(struct ll_statahead_info *sai, struct sa_entry *entry, int ret)
319 struct list_head *pos = &sai->sai_entries;
320 __u64 index = entry->se_index;
322 LASSERT(!sa_ready(entry));
323 LASSERT(list_empty(&entry->se_list));
325 list_for_each_entry_reverse(se, &sai->sai_entries, se_list) {
326 if (se->se_index < entry->se_index) {
331 list_add(&entry->se_list, pos);
332 entry->se_state = ret < 0 ? SA_ENTRY_INVA : SA_ENTRY_SUCC;
334 return (index == sai->sai_index_wait);
338 * release resources used in async stat RPC, update entry state and wakeup if
339 * scanner process it waiting on this entry.
342 sa_make_ready(struct ll_statahead_info *sai, struct sa_entry *entry, int ret)
344 struct ll_inode_info *lli = ll_i2info(sai->sai_dentry->d_inode);
345 struct md_enqueue_info *minfo = entry->se_minfo;
346 struct ptlrpc_request *req = entry->se_req;
349 /* release resources used in RPC */
351 entry->se_minfo = NULL;
352 ll_intent_release(&minfo->mi_it);
358 entry->se_req = NULL;
359 ptlrpc_req_finished(req);
362 spin_lock(&lli->lli_sa_lock);
363 wakeup = __sa_make_ready(sai, entry, ret);
364 spin_unlock(&lli->lli_sa_lock);
367 wake_up(&sai->sai_waitq);
370 /* insert inode into the list of sai_agls */
371 static void ll_agl_add(struct ll_statahead_info *sai,
372 struct inode *inode, int index)
374 struct ll_inode_info *child = ll_i2info(inode);
375 struct ll_inode_info *parent = ll_i2info(sai->sai_dentry->d_inode);
378 spin_lock(&child->lli_agl_lock);
379 if (child->lli_agl_index == 0) {
380 child->lli_agl_index = index;
381 spin_unlock(&child->lli_agl_lock);
383 LASSERT(list_empty(&child->lli_agl_list));
386 spin_lock(&parent->lli_agl_lock);
387 if (agl_list_empty(sai))
389 list_add_tail(&child->lli_agl_list, &sai->sai_agls);
390 spin_unlock(&parent->lli_agl_lock);
392 spin_unlock(&child->lli_agl_lock);
396 wake_up(&sai->sai_agl_thread.t_ctl_waitq);
400 static struct ll_statahead_info *ll_sai_alloc(struct dentry *dentry)
402 struct ll_statahead_info *sai;
403 struct ll_inode_info *lli = ll_i2info(dentry->d_inode);
411 sai->sai_dentry = dget(dentry);
412 atomic_set(&sai->sai_refcount, 1);
413 sai->sai_max = LL_SA_RPC_MIN;
415 init_waitqueue_head(&sai->sai_waitq);
416 init_waitqueue_head(&sai->sai_thread.t_ctl_waitq);
417 init_waitqueue_head(&sai->sai_agl_thread.t_ctl_waitq);
419 INIT_LIST_HEAD(&sai->sai_interim_entries);
420 INIT_LIST_HEAD(&sai->sai_entries);
421 INIT_LIST_HEAD(&sai->sai_agls);
423 for (i = 0; i < LL_SA_CACHE_SIZE; i++) {
424 INIT_LIST_HEAD(&sai->sai_cache[i]);
425 spin_lock_init(&sai->sai_cache_lock[i]);
427 atomic_set(&sai->sai_cache_count, 0);
429 spin_lock(&sai_generation_lock);
430 lli->lli_sa_generation = ++sai_generation;
431 if (unlikely(sai_generation == 0))
432 lli->lli_sa_generation = ++sai_generation;
433 spin_unlock(&sai_generation_lock);
439 static inline void ll_sai_free(struct ll_statahead_info *sai)
441 LASSERT(sai->sai_dentry != NULL);
442 dput(sai->sai_dentry);
447 * take refcount of sai if sai for @dir exists, which means statahead is on for
450 static inline struct ll_statahead_info *ll_sai_get(struct inode *dir)
452 struct ll_inode_info *lli = ll_i2info(dir);
453 struct ll_statahead_info *sai = NULL;
455 spin_lock(&lli->lli_sa_lock);
458 atomic_inc(&sai->sai_refcount);
459 spin_unlock(&lli->lli_sa_lock);
465 * put sai refcount after use, if refcount reaches zero, free sai and sa_entries
468 static void ll_sai_put(struct ll_statahead_info *sai)
470 struct ll_inode_info *lli = ll_i2info(sai->sai_dentry->d_inode);
472 if (atomic_dec_and_lock(&sai->sai_refcount, &lli->lli_sa_lock)) {
473 struct sa_entry *entry, *next;
474 struct ll_sb_info *sbi = ll_i2sbi(sai->sai_dentry->d_inode);
477 spin_unlock(&lli->lli_sa_lock);
479 LASSERT(thread_is_stopped(&sai->sai_thread));
480 LASSERT(thread_is_stopped(&sai->sai_agl_thread));
481 LASSERT(sai->sai_sent == sai->sai_replied);
482 LASSERT(!sa_has_callback(sai));
484 list_for_each_entry_safe(entry, next, &sai->sai_entries,
488 LASSERT(atomic_read(&sai->sai_cache_count) == 0);
489 LASSERT(agl_list_empty(sai));
492 atomic_dec(&sbi->ll_sa_running);
496 /* Do NOT forget to drop inode refcount when into sai_agls. */
497 static void ll_agl_trigger(struct inode *inode, struct ll_statahead_info *sai)
499 struct ll_inode_info *lli = ll_i2info(inode);
500 __u64 index = lli->lli_agl_index;
504 LASSERT(list_empty(&lli->lli_agl_list));
506 /* AGL maybe fall behind statahead with one entry */
507 if (is_omitted_entry(sai, index + 1)) {
508 lli->lli_agl_index = 0;
513 /* Someone is in glimpse (sync or async), do nothing. */
514 rc = down_write_trylock(&lli->lli_glimpse_sem);
516 lli->lli_agl_index = 0;
522 * Someone triggered glimpse within 1 sec before.
523 * 1) The former glimpse succeeded with glimpse lock granted by OST, and
524 * if the lock is still cached on client, AGL needs to do nothing. If
525 * it is cancelled by other client, AGL maybe cannot obtaion new lock
526 * for no glimpse callback triggered by AGL.
527 * 2) The former glimpse succeeded, but OST did not grant glimpse lock.
528 * Under such case, it is quite possible that the OST will not grant
529 * glimpse lock for AGL also.
530 * 3) The former glimpse failed, compared with other two cases, it is
531 * relative rare. AGL can ignore such case, and it will not muchly
532 * affect the performance.
534 if (lli->lli_glimpse_time != 0 &&
535 cfs_time_before(cfs_time_shift(-1), lli->lli_glimpse_time)) {
536 up_write(&lli->lli_glimpse_sem);
537 lli->lli_agl_index = 0;
542 CDEBUG(D_READA, "Handling (init) async glimpse: inode = "
543 DFID", idx = "LPU64"\n", PFID(&lli->lli_fid), index);
546 lli->lli_agl_index = 0;
547 lli->lli_glimpse_time = cfs_time_current();
548 up_write(&lli->lli_glimpse_sem);
550 CDEBUG(D_READA, "Handled (init) async glimpse: inode= "
551 DFID", idx = "LPU64", rc = %d\n",
552 PFID(&lli->lli_fid), index, rc);
560 * prepare inode for sa entry, add it into agl list, now sa_entry is ready
561 * to be used by scanner process.
563 static void sa_instantiate(struct ll_statahead_info *sai,
564 struct sa_entry *entry)
566 struct inode *dir = sai->sai_dentry->d_inode;
568 struct md_enqueue_info *minfo;
569 struct lookup_intent *it;
570 struct ptlrpc_request *req;
571 struct mdt_body *body;
575 LASSERT(entry->se_handle != 0);
577 minfo = entry->se_minfo;
580 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
582 GOTO(out, rc = -EFAULT);
584 child = entry->se_inode;
586 /* revalidate; unlinked and re-created with the same name */
587 if (unlikely(!lu_fid_eq(&minfo->mi_data.op_fid2,
589 entry->se_inode = NULL;
595 it->d.lustre.it_lock_handle = entry->se_handle;
596 rc = md_revalidate_lock(ll_i2mdexp(dir), it, ll_inode2fid(dir), NULL);
598 GOTO(out, rc = -EAGAIN);
600 rc = ll_prep_inode(&child, req, dir->i_sb, it);
604 CDEBUG(D_READA, "%s: setting %.*s"DFID" l_data to inode %p\n",
605 ll_get_fsname(child->i_sb, NULL, 0),
606 entry->se_qstr.len, entry->se_qstr.name,
607 PFID(ll_inode2fid(child)), child);
608 ll_set_lock_data(ll_i2sbi(dir)->ll_md_exp, child, it, NULL);
610 entry->se_inode = child;
612 if (agl_should_run(sai, child))
613 ll_agl_add(sai, child, entry->se_index);
618 /* sa_make_ready() will drop ldlm ibits lock refcount by calling
619 * ll_intent_drop_lock() in spite of failures. Do not worry about
620 * calling ll_intent_drop_lock() more than once. */
621 sa_make_ready(sai, entry, rc);
624 /* once there are async stat replies, instantiate sa_entry from replies */
625 static void sa_handle_callback(struct ll_statahead_info *sai)
627 struct ll_inode_info *lli;
629 lli = ll_i2info(sai->sai_dentry->d_inode);
631 while (sa_has_callback(sai)) {
632 struct sa_entry *entry;
634 spin_lock(&lli->lli_sa_lock);
635 if (unlikely(!sa_has_callback(sai))) {
636 spin_unlock(&lli->lli_sa_lock);
639 entry = list_entry(sai->sai_interim_entries.next,
640 struct sa_entry, se_list);
641 list_del_init(&entry->se_list);
642 spin_unlock(&lli->lli_sa_lock);
644 sa_instantiate(sai, entry);
649 * callback for async stat RPC, because this is called in ptlrpcd context, we
650 * only put sa_entry in sai_interim_entries, and wake up statahead thread to
651 * really prepare inode and instantiate sa_entry later.
653 static int ll_statahead_interpret(struct ptlrpc_request *req,
654 struct md_enqueue_info *minfo, int rc)
656 struct lookup_intent *it = &minfo->mi_it;
657 struct inode *dir = minfo->mi_dir;
658 struct ll_inode_info *lli = ll_i2info(dir);
659 struct ll_statahead_info *sai = lli->lli_sai;
660 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);
678 ll_intent_release(it);
682 /* release ibits lock ASAP to avoid deadlock when statahead
683 * thread enqueues lock on parent in readdir and another
684 * process enqueues lock on child with parent lock held, eg.
686 handle = it->d.lustre.it_lock_handle;
687 ll_intent_drop_lock(it);
690 spin_lock(&lli->lli_sa_lock);
692 wakeup = __sa_make_ready(sai, entry, rc);
694 entry->se_minfo = minfo;
695 entry->se_req = ptlrpc_request_addref(req);
696 /* Release the async ibits lock ASAP to avoid deadlock
697 * when statahead thread tries to enqueue lock on parent
698 * for readpage and other tries to enqueue lock on child
699 * with parent's lock held, for example: unlink. */
700 entry->se_handle = handle;
701 wakeup = !sa_has_callback(sai);
702 list_add_tail(&entry->se_list, &sai->sai_interim_entries);
706 wake_up(&sai->sai_thread.t_ctl_waitq);
707 spin_unlock(&lli->lli_sa_lock);
712 /* finish async stat RPC arguments */
713 static void sa_fini_data(struct md_enqueue_info *minfo)
720 * prepare arguments for async stat RPC.
722 static struct md_enqueue_info *
723 sa_prep_data(struct inode *dir, struct inode *child, struct sa_entry *entry)
725 struct md_enqueue_info *minfo;
726 struct ldlm_enqueue_info *einfo;
727 struct md_op_data *op_data;
729 OBD_ALLOC_PTR(minfo);
731 return ERR_PTR(-ENOMEM);
733 op_data = ll_prep_md_op_data(&minfo->mi_data, dir, child, NULL, 0, 0,
734 LUSTRE_OPC_ANY, NULL);
735 if (IS_ERR(op_data)) {
737 return (struct md_enqueue_info *)op_data;
741 op_data->op_fid2 = entry->se_fid;
743 minfo->mi_it.it_op = IT_GETATTR;
744 minfo->mi_dir = igrab(dir);
745 minfo->mi_cb = ll_statahead_interpret;
746 minfo->mi_cbdata = entry;
748 einfo = &minfo->mi_einfo;
749 einfo->ei_type = LDLM_IBITS;
750 einfo->ei_mode = it_to_lock_mode(&minfo->mi_it);
751 einfo->ei_cb_bl = ll_md_blocking_ast;
752 einfo->ei_cb_cp = ldlm_completion_ast;
753 einfo->ei_cb_gl = NULL;
754 einfo->ei_cbdata = NULL;
759 /* async stat for file not found in dcache */
760 static int sa_lookup(struct inode *dir, struct sa_entry *entry)
762 struct md_enqueue_info *minfo;
766 minfo = sa_prep_data(dir, NULL, entry);
768 RETURN(PTR_ERR(minfo));
770 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo);
778 * async stat for file found in dcache, similar to .revalidate
780 * \retval 1 dentry valid, no RPC sent
781 * \retval 0 dentry invalid, will send async stat RPC
782 * \retval negative number upon error
784 static int sa_revalidate(struct inode *dir, struct sa_entry *entry,
785 struct dentry *dentry)
787 struct inode *inode = dentry->d_inode;
788 struct lookup_intent it = { .it_op = IT_GETATTR,
789 .d.lustre.it_lock_handle = 0 };
790 struct md_enqueue_info *minfo;
794 if (unlikely(inode == NULL))
797 if (d_mountpoint(dentry))
800 entry->se_inode = igrab(inode);
801 rc = md_revalidate_lock(ll_i2mdexp(dir), &it, ll_inode2fid(inode),
804 entry->se_handle = it.d.lustre.it_lock_handle;
805 ll_intent_release(&it);
809 minfo = sa_prep_data(dir, inode, entry);
811 entry->se_inode = NULL;
813 RETURN(PTR_ERR(minfo));
816 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo);
818 entry->se_inode = NULL;
826 /* async stat for file with @name */
827 static void sa_statahead(struct dentry *parent, const char *name, int len,
828 const struct lu_fid *fid)
830 struct inode *dir = parent->d_inode;
831 struct ll_inode_info *lli = ll_i2info(dir);
832 struct ll_statahead_info *sai = lli->lli_sai;
833 struct dentry *dentry = NULL;
834 struct sa_entry *entry;
838 entry = sa_alloc(sai, sai->sai_index, name, len, fid);
842 dentry = d_lookup(parent, &entry->se_qstr);
844 rc = sa_lookup(dir, entry);
846 rc = sa_revalidate(dir, entry, dentry);
847 if (rc == 1 && agl_should_run(sai, dentry->d_inode))
848 ll_agl_add(sai, dentry->d_inode, entry->se_index);
855 sa_make_ready(sai, entry, rc);
864 /* async glimpse (agl) thread main function */
865 static int ll_agl_thread(void *arg)
867 struct dentry *parent = (struct dentry *)arg;
868 struct inode *dir = parent->d_inode;
869 struct ll_inode_info *plli = ll_i2info(dir);
870 struct ll_inode_info *clli;
871 struct ll_sb_info *sbi = ll_i2sbi(dir);
872 struct ll_statahead_info *sai;
873 struct ptlrpc_thread *thread;
874 struct l_wait_info lwi = { 0 };
878 sai = ll_sai_get(dir);
879 thread = &sai->sai_agl_thread;
880 thread->t_pid = current_pid();
881 CDEBUG(D_READA, "agl thread started: sai %p, parent %.*s\n",
882 sai, parent->d_name.len, parent->d_name.name);
884 atomic_inc(&sbi->ll_agl_total);
885 spin_lock(&plli->lli_agl_lock);
886 sai->sai_agl_valid = 1;
887 if (thread_is_init(thread))
888 /* If someone else has changed the thread state
889 * (e.g. already changed to SVC_STOPPING), we can't just
890 * blindly overwrite that setting. */
891 thread_set_flags(thread, SVC_RUNNING);
892 spin_unlock(&plli->lli_agl_lock);
893 wake_up(&thread->t_ctl_waitq);
896 l_wait_event(thread->t_ctl_waitq,
897 !agl_list_empty(sai) ||
898 !thread_is_running(thread),
901 if (!thread_is_running(thread))
904 spin_lock(&plli->lli_agl_lock);
905 /* The statahead thread maybe help to process AGL entries,
906 * so check whether list empty again. */
907 if (!agl_list_empty(sai)) {
908 clli = agl_first_entry(sai);
909 list_del_init(&clli->lli_agl_list);
910 spin_unlock(&plli->lli_agl_lock);
911 ll_agl_trigger(&clli->lli_vfs_inode, sai);
913 spin_unlock(&plli->lli_agl_lock);
917 spin_lock(&plli->lli_agl_lock);
918 sai->sai_agl_valid = 0;
919 while (!agl_list_empty(sai)) {
920 clli = agl_first_entry(sai);
921 list_del_init(&clli->lli_agl_list);
922 spin_unlock(&plli->lli_agl_lock);
923 clli->lli_agl_index = 0;
924 iput(&clli->lli_vfs_inode);
925 spin_lock(&plli->lli_agl_lock);
927 thread_set_flags(thread, SVC_STOPPED);
928 spin_unlock(&plli->lli_agl_lock);
929 wake_up(&thread->t_ctl_waitq);
931 CDEBUG(D_READA, "agl thread stopped: sai %p, parent %.*s\n",
932 sai, parent->d_name.len, parent->d_name.name);
936 /* start agl thread */
937 static void ll_start_agl(struct dentry *parent, struct ll_statahead_info *sai)
939 struct ptlrpc_thread *thread = &sai->sai_agl_thread;
940 struct l_wait_info lwi = { 0 };
941 struct ll_inode_info *plli;
942 struct task_struct *task;
945 CDEBUG(D_READA, "start agl thread: sai %p, parent %.*s\n",
946 sai, parent->d_name.len, parent->d_name.name);
948 plli = ll_i2info(parent->d_inode);
949 task = kthread_run(ll_agl_thread, parent,
950 "ll_agl_%u", plli->lli_opendir_pid);
952 CERROR("can't start ll_agl thread, rc: %ld\n", PTR_ERR(task));
953 thread_set_flags(thread, SVC_STOPPED);
957 l_wait_event(thread->t_ctl_waitq,
958 thread_is_running(thread) || thread_is_stopped(thread),
963 /* statahead thread main function */
964 static int ll_statahead_thread(void *arg)
966 struct dentry *parent = (struct dentry *)arg;
967 struct inode *dir = parent->d_inode;
968 struct ll_inode_info *lli = ll_i2info(dir);
969 struct ll_sb_info *sbi = ll_i2sbi(dir);
970 struct ll_statahead_info *sai;
971 struct ptlrpc_thread *sa_thread;
972 struct ptlrpc_thread *agl_thread;
974 struct md_op_data *op_data;
975 struct ll_dir_chain chain;
976 struct l_wait_info lwi = { 0 };
977 struct page *page = NULL;
982 sai = ll_sai_get(dir);
983 sa_thread = &sai->sai_thread;
984 agl_thread = &sai->sai_agl_thread;
985 sa_thread->t_pid = current_pid();
986 CDEBUG(D_READA, "statahead thread starting: sai %p, parent %.*s\n",
987 sai, parent->d_name.len, parent->d_name.name);
989 op_data = ll_prep_md_op_data(NULL, dir, dir, NULL, 0, 0,
990 LUSTRE_OPC_ANY, dir);
992 GOTO(out, rc = PTR_ERR(op_data));
994 op_data->op_max_pages = ll_i2sbi(dir)->ll_md_brw_pages;
996 if (sbi->ll_flags & LL_SBI_AGL_ENABLED)
997 ll_start_agl(parent, sai);
999 atomic_inc(&sbi->ll_sa_total);
1000 spin_lock(&lli->lli_sa_lock);
1001 if (thread_is_init(sa_thread))
1002 /* If someone else has changed the thread state
1003 * (e.g. already changed to SVC_STOPPING), we can't just
1004 * blindly overwrite that setting. */
1005 thread_set_flags(sa_thread, SVC_RUNNING);
1006 spin_unlock(&lli->lli_sa_lock);
1007 wake_up(&sa_thread->t_ctl_waitq);
1009 ll_dir_chain_init(&chain);
1010 while (pos != MDS_DIR_END_OFF && thread_is_running(sa_thread)) {
1011 struct lu_dirpage *dp;
1012 struct lu_dirent *ent;
1014 sai->sai_in_readpage = 1;
1015 page = ll_get_dir_page(dir, op_data, pos, &chain);
1016 sai->sai_in_readpage = 0;
1019 CDEBUG(D_READA, "error reading dir "DFID" at "LPU64
1020 "/"LPU64" opendir_pid = %u: rc = %d\n",
1021 PFID(ll_inode2fid(dir)), pos, sai->sai_index,
1022 lli->lli_opendir_pid, rc);
1026 dp = page_address(page);
1027 for (ent = lu_dirent_start(dp);
1028 ent != NULL && thread_is_running(sa_thread) &&
1030 ent = lu_dirent_next(ent)) {
1036 hash = le64_to_cpu(ent->lde_hash);
1037 if (unlikely(hash < pos))
1039 * Skip until we find target hash value.
1043 namelen = le16_to_cpu(ent->lde_namelen);
1044 if (unlikely(namelen == 0))
1046 * Skip dummy record.
1050 name = ent->lde_name;
1051 if (name[0] == '.') {
1057 } else if (name[1] == '.' && namelen == 2) {
1062 } else if (!sai->sai_ls_all) {
1064 * skip hidden files.
1066 sai->sai_skip_hidden++;
1072 * don't stat-ahead first entry.
1074 if (unlikely(++first == 1))
1077 fid_le_to_cpu(&fid, &ent->lde_fid);
1079 /* wait for spare statahead window */
1081 l_wait_event(sa_thread->t_ctl_waitq,
1082 !sa_sent_full(sai) ||
1083 sa_has_callback(sai) ||
1084 !agl_list_empty(sai) ||
1085 !thread_is_running(sa_thread),
1088 sa_handle_callback(sai);
1090 spin_lock(&lli->lli_agl_lock);
1091 while (sa_sent_full(sai) &&
1092 !agl_list_empty(sai)) {
1093 struct ll_inode_info *clli;
1095 clli = agl_first_entry(sai);
1096 list_del_init(&clli->lli_agl_list);
1097 spin_unlock(&lli->lli_agl_lock);
1099 ll_agl_trigger(&clli->lli_vfs_inode,
1102 spin_lock(&lli->lli_agl_lock);
1104 spin_unlock(&lli->lli_agl_lock);
1105 } while (sa_sent_full(sai) &&
1106 thread_is_running(sa_thread));
1108 sa_statahead(parent, name, namelen, &fid);
1111 pos = le64_to_cpu(dp->ldp_hash_end);
1112 ll_release_page(dir, page,
1113 le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1115 if (sa_low_hit(sai)) {
1117 atomic_inc(&sbi->ll_sa_wrong);
1118 CDEBUG(D_READA, "Statahead for dir "DFID" hit "
1119 "ratio too low: hit/miss "LPU64"/"LPU64
1120 ", sent/replied "LPU64"/"LPU64", stopping "
1121 "statahead thread: pid %d\n",
1122 PFID(&lli->lli_fid), sai->sai_hit,
1123 sai->sai_miss, sai->sai_sent,
1124 sai->sai_replied, current_pid());
1128 ll_dir_chain_fini(&chain);
1129 ll_finish_md_op_data(op_data);
1132 spin_lock(&lli->lli_sa_lock);
1133 thread_set_flags(sa_thread, SVC_STOPPING);
1134 lli->lli_sa_enabled = 0;
1135 spin_unlock(&lli->lli_sa_lock);
1138 /* statahead is finished, but statahead entries need to be cached, wait
1139 * for file release to stop me. */
1140 while (thread_is_running(sa_thread)) {
1141 l_wait_event(sa_thread->t_ctl_waitq,
1142 sa_has_callback(sai) ||
1143 !thread_is_running(sa_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(msecs_to_jiffies(MSEC_PER_SEC >> 3),
1173 l_wait_event(sa_thread->t_ctl_waitq,
1174 sai->sai_sent == sai->sai_replied, &lwi);
1177 /* release resources held by statahead RPCs */
1178 sa_handle_callback(sai);
1180 spin_lock(&lli->lli_sa_lock);
1181 thread_set_flags(sa_thread, SVC_STOPPED);
1182 spin_unlock(&lli->lli_sa_lock);
1184 CDEBUG(D_READA, "statahead thread stopped: sai %p, parent %.*s\n",
1185 sai, parent->d_name.len, parent->d_name.name);
1187 wake_up(&sai->sai_waitq);
1188 wake_up(&sa_thread->t_ctl_waitq);
1194 /* authorize opened dir handle @key to statahead */
1195 void ll_authorize_statahead(struct inode *dir, void *key)
1197 struct ll_inode_info *lli = ll_i2info(dir);
1199 spin_lock(&lli->lli_sa_lock);
1200 if (lli->lli_opendir_key == NULL && lli->lli_sai == NULL) {
1202 * if lli_sai is not NULL, it means previous statahead is not
1203 * finished yet, we'd better not start a new statahead for now.
1205 LASSERT(lli->lli_opendir_pid == 0);
1206 lli->lli_opendir_key = key;
1207 lli->lli_opendir_pid = current_pid();
1208 lli->lli_sa_enabled = 1;
1210 spin_unlock(&lli->lli_sa_lock);
1214 * deauthorize opened dir handle @key to statahead, and notify statahead thread
1215 * to quit if it's running.
1217 void ll_deauthorize_statahead(struct inode *dir, void *key)
1219 struct ll_inode_info *lli = ll_i2info(dir);
1220 struct ll_statahead_info *sai;
1222 LASSERT(lli->lli_opendir_key == key);
1223 LASSERT(lli->lli_opendir_pid != 0);
1225 CDEBUG(D_READA, "deauthorize statahead for "DFID"\n",
1226 PFID(&lli->lli_fid));
1228 spin_lock(&lli->lli_sa_lock);
1229 lli->lli_opendir_key = NULL;
1230 lli->lli_opendir_pid = 0;
1231 lli->lli_sa_enabled = 0;
1233 if (sai != NULL && thread_is_running(&sai->sai_thread)) {
1235 * statahead thread may not quit yet because it needs to cache
1236 * entries, now it's time to tell it to quit.
1238 thread_set_flags(&sai->sai_thread, SVC_STOPPING);
1239 wake_up(&sai->sai_thread.t_ctl_waitq);
1241 spin_unlock(&lli->lli_sa_lock);
1246 * not first dirent, or is "."
1248 LS_NOT_FIRST_DE = 0,
1250 * the first non-hidden dirent
1254 * the first hidden dirent, that is "."
1259 /* file is first dirent under @dir */
1260 static int is_first_dirent(struct inode *dir, struct dentry *dentry)
1262 struct ll_dir_chain chain;
1263 struct qstr *target = &dentry->d_name;
1264 struct md_op_data *op_data;
1266 struct page *page = NULL;
1267 int rc = LS_NOT_FIRST_DE;
1271 op_data = ll_prep_md_op_data(NULL, dir, dir, NULL, 0, 0,
1272 LUSTRE_OPC_ANY, dir);
1273 if (IS_ERR(op_data))
1274 RETURN(PTR_ERR(op_data));
1276 *FIXME choose the start offset of the readdir
1278 op_data->op_max_pages = ll_i2sbi(dir)->ll_md_brw_pages;
1280 ll_dir_chain_init(&chain);
1281 page = ll_get_dir_page(dir, op_data, 0, &chain);
1284 struct lu_dirpage *dp;
1285 struct lu_dirent *ent;
1288 struct ll_inode_info *lli = ll_i2info(dir);
1291 CERROR("%s: reading dir "DFID" at "LPU64
1292 "opendir_pid = %u : rc = %d\n",
1293 ll_get_fsname(dir->i_sb, NULL, 0),
1294 PFID(ll_inode2fid(dir)), pos,
1295 lli->lli_opendir_pid, rc);
1299 dp = page_address(page);
1300 for (ent = lu_dirent_start(dp); ent != NULL;
1301 ent = lu_dirent_next(ent)) {
1306 hash = le64_to_cpu(ent->lde_hash);
1307 /* The ll_get_dir_page() can return any page containing
1308 * the given hash which may be not the start hash. */
1309 if (unlikely(hash < pos))
1312 namelen = le16_to_cpu(ent->lde_namelen);
1313 if (unlikely(namelen == 0))
1315 * skip dummy record.
1319 name = ent->lde_name;
1320 if (name[0] == '.') {
1326 else if (name[1] == '.' && namelen == 2)
1337 if (dot_de && target->name[0] != '.') {
1338 CDEBUG(D_READA, "%.*s skip hidden file %.*s\n",
1339 target->len, target->name,
1344 if (target->len != namelen ||
1345 memcmp(target->name, name, namelen) != 0)
1346 rc = LS_NOT_FIRST_DE;
1350 rc = LS_FIRST_DOT_DE;
1352 ll_release_page(dir, page, false);
1355 pos = le64_to_cpu(dp->ldp_hash_end);
1356 if (pos == MDS_DIR_END_OFF) {
1358 * End of directory reached.
1360 ll_release_page(dir, page, false);
1364 * chain is exhausted
1365 * Normal case: continue to the next page.
1367 ll_release_page(dir, page, le32_to_cpu(dp->ldp_flags) &
1369 page = ll_get_dir_page(dir, op_data, pos, &chain);
1374 ll_dir_chain_fini(&chain);
1375 ll_finish_md_op_data(op_data);
1380 * revalidate @dentryp from statahead cache
1382 * \param[in] dir parent directory
1383 * \param[in] sai sai structure
1384 * \param[out] dentryp pointer to dentry which will be revalidated
1385 * \param[in] unplug unplug statahead window only (normally for negative
1387 * \retval 1 on success, dentry is saved in @dentryp
1388 * \retval 0 if revalidation failed (no proper lock on client)
1389 * \retval negative number upon error
1391 static int revalidate_statahead_dentry(struct inode *dir,
1392 struct ll_statahead_info *sai,
1393 struct dentry **dentryp,
1396 struct sa_entry *entry = NULL;
1397 struct l_wait_info lwi = { 0 };
1398 struct ll_dentry_data *ldd;
1399 struct ll_inode_info *lli;
1403 if ((*dentryp)->d_name.name[0] == '.') {
1404 if (sai->sai_ls_all ||
1405 sai->sai_miss_hidden >= sai->sai_skip_hidden) {
1407 * Hidden dentry is the first one, or statahead
1408 * thread does not skip so many hidden dentries
1409 * before "sai_ls_all" enabled as below.
1412 if (!sai->sai_ls_all)
1414 * It maybe because hidden dentry is not
1415 * the first one, "sai_ls_all" was not
1416 * set, then "ls -al" missed. Enable
1417 * "sai_ls_all" for such case.
1419 sai->sai_ls_all = 1;
1422 * Such "getattr" has been skipped before
1423 * "sai_ls_all" enabled as above.
1425 sai->sai_miss_hidden++;
1433 entry = sa_get(sai, &(*dentryp)->d_name);
1435 GOTO(out, rc = -EAGAIN);
1437 /* if statahead is busy in readdir, help it do post-work */
1438 if (!sa_ready(entry) && sai->sai_in_readpage)
1439 sa_handle_callback(sai);
1441 if (!sa_ready(entry)) {
1442 sai->sai_index_wait = entry->se_index;
1443 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(30), NULL,
1444 LWI_ON_SIGNAL_NOOP, NULL);
1445 rc = l_wait_event(sai->sai_waitq, sa_ready(entry), &lwi);
1448 * entry may not be ready, so it may be used by inflight
1449 * statahead RPC, don't free it.
1452 GOTO(out, rc = -EAGAIN);
1456 if (entry->se_state == SA_ENTRY_SUCC && entry->se_inode != NULL) {
1457 struct inode *inode = entry->se_inode;
1458 struct lookup_intent it = { .it_op = IT_GETATTR,
1459 .d.lustre.it_lock_handle =
1463 rc = md_revalidate_lock(ll_i2mdexp(dir), &it,
1464 ll_inode2fid(inode), &bits);
1466 if ((*dentryp)->d_inode == NULL) {
1467 struct dentry *alias;
1469 alias = ll_splice_alias(inode, *dentryp);
1471 GOTO(out, rc = PTR_ERR(alias));
1473 /* statahead prepared this inode, transfer inode
1474 * refcount from sa_entry to dentry */
1475 entry->se_inode = NULL;
1476 } else if ((*dentryp)->d_inode != inode) {
1477 /* revalidate, but inode is recreated */
1479 "%s: stale dentry %.*s inode "
1480 DFID", statahead inode "DFID
1482 ll_get_fsname((*dentryp)->d_inode->i_sb,
1484 (*dentryp)->d_name.len,
1485 (*dentryp)->d_name.name,
1486 PFID(ll_inode2fid((*dentryp)->d_inode)),
1487 PFID(ll_inode2fid(inode)));
1488 GOTO(out, rc = -ESTALE);
1491 if ((bits & MDS_INODELOCK_LOOKUP) &&
1492 d_lustre_invalid(*dentryp))
1493 d_lustre_revalidate(*dentryp);
1494 ll_intent_release(&it);
1499 * statahead cached sa_entry can be used only once, and will be killed
1500 * right after use, so if lookup/revalidate accessed statahead cache,
1501 * set dentry ldd_sa_generation to parent lli_sa_generation, later if we
1502 * stat this file again, we know we've done statahead before, see
1503 * dentry_may_statahead().
1505 ldd = ll_d2d(*dentryp);
1506 lli = ll_i2info(dir);
1507 /* ldd can be NULL if llite lookup failed. */
1509 ldd->lld_sa_generation = lli->lli_sa_generation;
1516 * start statahead thread
1518 * \param[in] dir parent directory
1519 * \param[in] dentry dentry that triggers statahead, normally the first
1521 * \retval -EAGAIN on success, because when this function is
1522 * called, it's already in lookup call, so client should
1523 * do it itself instead of waiting for statahead thread
1524 * to do it asynchronously.
1525 * \retval negative number upon error
1527 static int start_statahead_thread(struct inode *dir, struct dentry *dentry)
1529 struct ll_inode_info *lli = ll_i2info(dir);
1530 struct ll_statahead_info *sai = NULL;
1531 struct dentry *parent = dentry->d_parent;
1532 struct ptlrpc_thread *thread;
1533 struct l_wait_info lwi = { 0 };
1534 struct task_struct *task;
1538 /* I am the "lli_opendir_pid" owner, only me can set "lli_sai". */
1539 rc = is_first_dirent(dir, dentry);
1540 if (rc == LS_NOT_FIRST_DE)
1541 /* It is not "ls -{a}l" operation, no need statahead for it. */
1542 GOTO(out, rc = -EFAULT);
1544 sai = ll_sai_alloc(parent);
1546 GOTO(out, rc = -ENOMEM);
1548 sai->sai_ls_all = (rc == LS_FIRST_DOT_DE);
1550 /* if current lli_opendir_key was deauthorized, or dir re-opened by
1551 * another process, don't start statahead, otherwise the newly spawned
1552 * statahead thread won't be notified to quit. */
1553 spin_lock(&lli->lli_sa_lock);
1554 if (unlikely(lli->lli_sai != NULL ||
1555 lli->lli_opendir_key == NULL ||
1556 lli->lli_opendir_pid != current->pid)) {
1557 spin_unlock(&lli->lli_sa_lock);
1558 GOTO(out, rc = -EPERM);
1561 spin_unlock(&lli->lli_sa_lock);
1563 atomic_inc(&ll_i2sbi(parent->d_inode)->ll_sa_running);
1565 CDEBUG(D_READA, "start statahead thread: [pid %d] [parent %.*s]\n",
1566 current_pid(), parent->d_name.len, parent->d_name.name);
1568 task = kthread_run(ll_statahead_thread, parent, "ll_sa_%u",
1569 lli->lli_opendir_pid);
1570 thread = &sai->sai_thread;
1573 CERROR("can't start ll_sa thread, rc: %d\n", rc);
1577 l_wait_event(thread->t_ctl_waitq,
1578 thread_is_running(thread) || thread_is_stopped(thread),
1583 * We don't stat-ahead for the first dirent since we are already in
1589 /* once we start statahead thread failed, disable statahead so that
1590 * subsequent stat won't waste time to try it. */
1591 spin_lock(&lli->lli_sa_lock);
1592 lli->lli_sa_enabled = 0;
1593 lli->lli_sai = NULL;
1594 spin_unlock(&lli->lli_sa_lock);
1603 * statahead entry function, this is called when client getattr on a file, it
1604 * will start statahead thread if this is the first dir entry, else revalidate
1605 * dentry from statahead cache.
1607 * \param[in] dir parent directory
1608 * \param[out] dentryp dentry to getattr
1609 * \param[in] unplug unplug statahead window only (normally for negative
1611 * \retval 1 on success
1612 * \retval 0 revalidation from statahead cache failed, caller needs
1613 * to getattr from server directly
1614 * \retval negative number on error, caller often ignores this and
1615 * then getattr from server
1617 int ll_statahead(struct inode *dir, struct dentry **dentryp, bool unplug)
1619 struct ll_statahead_info *sai;
1621 sai = ll_sai_get(dir);
1625 rc = revalidate_statahead_dentry(dir, sai, dentryp, unplug);
1626 CDEBUG(D_READA, "revalidate statahead %.*s: %d.\n",
1627 (*dentryp)->d_name.len, (*dentryp)->d_name.name, rc);
1631 return start_statahead_thread(dir, *dentryp);