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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 #include <linux/sched.h>
35 #include <linux/kthread.h>
37 #include <linux/highmem.h>
38 #include <linux/pagemap.h>
39 #include <linux/delay.h>
41 #define DEBUG_SUBSYSTEM S_LLITE
43 #include <obd_support.h>
44 #include <lustre_dlm.h>
45 #include "llite_internal.h"
47 #define SA_OMITTED_ENTRY_MAX 8ULL
50 /** negative values are for error cases */
51 SA_ENTRY_INIT = 0, /** init entry */
52 SA_ENTRY_SUCC = 1, /** stat succeed */
53 SA_ENTRY_INVA = 2, /** invalid entry */
57 * sa_entry is not refcounted: statahead thread allocates it and do async stat,
58 * and in async stat callback ll_statahead_interpret() will add it into
59 * sai_interim_entries, later statahead thread will call sa_handle_callback() to
60 * instantiate entry and move it into sai_entries, and then only scanner process
61 * can access and free it.
64 /* link into sai_interim_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;
88 static unsigned int sai_generation;
89 static DEFINE_SPINLOCK(sai_generation_lock);
91 static inline int sa_unhashed(struct sa_entry *entry)
93 return list_empty(&entry->se_hash);
96 /* sa_entry is ready to use */
97 static inline int sa_ready(struct sa_entry *entry)
99 /* Make sure sa_entry is updated and ready to use */
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 && S_ISREG(inode->i_mode) && sai->sai_agl_task;
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 dentry *parent, struct ll_statahead_info *sai, __u64 index,
187 const char *name, int len, const struct lu_fid *fid)
189 struct ll_inode_info *lli;
190 struct sa_entry *entry;
196 entry_size = sizeof(struct sa_entry) + (len & ~3) + 4;
197 OBD_ALLOC(entry, entry_size);
198 if (unlikely(!entry))
199 RETURN(ERR_PTR(-ENOMEM));
201 CDEBUG(D_READA, "alloc sa entry %.*s(%p) index %llu\n",
202 len, name, entry, index);
204 entry->se_index = index;
206 entry->se_state = SA_ENTRY_INIT;
207 entry->se_size = entry_size;
208 dname = (char *)entry + sizeof(struct sa_entry);
209 memcpy(dname, name, len);
211 entry->se_qstr.hash = ll_full_name_hash(parent, name, len);
212 entry->se_qstr.len = len;
213 entry->se_qstr.name = dname;
214 entry->se_fid = *fid;
216 lli = ll_i2info(sai->sai_dentry->d_inode);
218 spin_lock(&lli->lli_sa_lock);
219 INIT_LIST_HEAD(&entry->se_list);
220 sa_rehash(sai, entry);
221 spin_unlock(&lli->lli_sa_lock);
223 atomic_inc(&sai->sai_cache_count);
228 /* free sa_entry, which should have been unhashed and not in any list */
229 static void sa_free(struct ll_statahead_info *sai, struct sa_entry *entry)
231 CDEBUG(D_READA, "free sa entry %.*s(%p) index %llu\n",
232 entry->se_qstr.len, entry->se_qstr.name, entry,
235 LASSERT(list_empty(&entry->se_list));
236 LASSERT(sa_unhashed(entry));
238 OBD_FREE(entry, entry->se_size);
239 atomic_dec(&sai->sai_cache_count);
243 * find sa_entry by name, used by directory scanner, lock is not needed because
244 * only scanner can remove the entry from cache.
246 static struct sa_entry *
247 sa_get(struct ll_statahead_info *sai, const struct qstr *qstr)
249 struct sa_entry *entry;
250 int i = sa_hash(qstr->hash);
252 list_for_each_entry(entry, &sai->sai_cache[i], se_hash) {
253 if (entry->se_qstr.hash == qstr->hash &&
254 entry->se_qstr.len == qstr->len &&
255 memcmp(entry->se_qstr.name, qstr->name, qstr->len) == 0)
261 /* unhash and unlink sa_entry, and then free it */
263 sa_kill(struct ll_statahead_info *sai, struct sa_entry *entry)
265 struct ll_inode_info *lli = ll_i2info(sai->sai_dentry->d_inode);
267 LASSERT(!sa_unhashed(entry));
268 LASSERT(!list_empty(&entry->se_list));
269 LASSERT(sa_ready(entry));
271 sa_unhash(sai, entry);
273 spin_lock(&lli->lli_sa_lock);
274 list_del_init(&entry->se_list);
275 spin_unlock(&lli->lli_sa_lock);
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 && 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++;
303 * kill old completed entries, only scanner process does this, no need
306 list_for_each_entry_safe(tmp, next, &sai->sai_entries, se_list) {
307 if (!is_omitted_entry(sai, tmp->se_index))
314 * update state and sort add entry to sai_entries by index, return true if
315 * scanner is waiting on this entry.
318 __sa_make_ready(struct ll_statahead_info *sai, struct sa_entry *entry, int ret)
321 struct list_head *pos = &sai->sai_entries;
322 __u64 index = entry->se_index;
324 LASSERT(!sa_ready(entry));
325 LASSERT(list_empty(&entry->se_list));
327 list_for_each_entry_reverse(se, &sai->sai_entries, se_list) {
328 if (se->se_index < entry->se_index) {
333 list_add(&entry->se_list, pos);
335 * LU-9210: ll_statahead_interpet must be able to see this before
338 smp_store_release(&entry->se_state,
339 ret < 0 ? SA_ENTRY_INVA : SA_ENTRY_SUCC);
341 return (index == sai->sai_index_wait);
344 /* finish async stat RPC arguments */
345 static void sa_fini_data(struct md_enqueue_info *minfo)
347 ll_unlock_md_op_lsm(&minfo->mi_data);
352 static int ll_statahead_interpret(struct ptlrpc_request *req,
353 struct md_enqueue_info *minfo, int rc);
356 * prepare arguments for async stat RPC.
358 static struct md_enqueue_info *
359 sa_prep_data(struct inode *dir, struct inode *child, struct sa_entry *entry)
361 struct md_enqueue_info *minfo;
362 struct ldlm_enqueue_info *einfo;
363 struct md_op_data *op_data;
365 OBD_ALLOC_PTR(minfo);
367 return ERR_PTR(-ENOMEM);
369 op_data = ll_prep_md_op_data(&minfo->mi_data, dir, child,
370 entry->se_qstr.name, entry->se_qstr.len, 0,
371 LUSTRE_OPC_ANY, NULL);
372 if (IS_ERR(op_data)) {
374 return (struct md_enqueue_info *)op_data;
378 op_data->op_fid2 = entry->se_fid;
380 minfo->mi_it.it_op = IT_GETATTR;
381 minfo->mi_dir = igrab(dir);
382 minfo->mi_cb = ll_statahead_interpret;
383 minfo->mi_cbdata = entry;
385 einfo = &minfo->mi_einfo;
386 einfo->ei_type = LDLM_IBITS;
387 einfo->ei_mode = it_to_lock_mode(&minfo->mi_it);
388 einfo->ei_cb_bl = ll_md_blocking_ast;
389 einfo->ei_cb_cp = ldlm_completion_ast;
390 einfo->ei_cb_gl = NULL;
391 einfo->ei_cbdata = NULL;
397 * release resources used in async stat RPC, update entry state and wakeup if
398 * scanner process it waiting on this entry.
401 sa_make_ready(struct ll_statahead_info *sai, struct sa_entry *entry, int ret)
403 struct ll_inode_info *lli = ll_i2info(sai->sai_dentry->d_inode);
404 struct md_enqueue_info *minfo = entry->se_minfo;
405 struct ptlrpc_request *req = entry->se_req;
408 /* release resources used in RPC */
410 entry->se_minfo = NULL;
411 ll_intent_release(&minfo->mi_it);
416 entry->se_req = NULL;
417 ptlrpc_req_finished(req);
420 spin_lock(&lli->lli_sa_lock);
421 wakeup = __sa_make_ready(sai, entry, ret);
422 spin_unlock(&lli->lli_sa_lock);
425 wake_up(&sai->sai_waitq);
428 /* insert inode into the list of sai_agls */
429 static void ll_agl_add(struct ll_statahead_info *sai,
430 struct inode *inode, int index)
432 struct ll_inode_info *child = ll_i2info(inode);
433 struct ll_inode_info *parent = ll_i2info(sai->sai_dentry->d_inode);
435 spin_lock(&child->lli_agl_lock);
436 if (child->lli_agl_index == 0) {
437 child->lli_agl_index = index;
438 spin_unlock(&child->lli_agl_lock);
440 LASSERT(list_empty(&child->lli_agl_list));
442 spin_lock(&parent->lli_agl_lock);
443 /* Re-check under the lock */
444 if (agl_should_run(sai, inode)) {
445 if (agl_list_empty(sai))
446 wake_up_process(sai->sai_agl_task);
448 list_add_tail(&child->lli_agl_list, &sai->sai_agls);
450 child->lli_agl_index = 0;
451 spin_unlock(&parent->lli_agl_lock);
453 spin_unlock(&child->lli_agl_lock);
458 static struct ll_statahead_info *ll_sai_alloc(struct dentry *dentry)
460 struct ll_statahead_info *sai;
461 struct ll_inode_info *lli = ll_i2info(dentry->d_inode);
470 sai->sai_dentry = dget(dentry);
471 atomic_set(&sai->sai_refcount, 1);
472 sai->sai_max = LL_SA_RPC_MIN;
474 init_waitqueue_head(&sai->sai_waitq);
476 INIT_LIST_HEAD(&sai->sai_interim_entries);
477 INIT_LIST_HEAD(&sai->sai_entries);
478 INIT_LIST_HEAD(&sai->sai_agls);
480 for (i = 0; i < LL_SA_CACHE_SIZE; i++) {
481 INIT_LIST_HEAD(&sai->sai_cache[i]);
482 spin_lock_init(&sai->sai_cache_lock[i]);
484 atomic_set(&sai->sai_cache_count, 0);
486 spin_lock(&sai_generation_lock);
487 lli->lli_sa_generation = ++sai_generation;
488 if (unlikely(sai_generation == 0))
489 lli->lli_sa_generation = ++sai_generation;
490 spin_unlock(&sai_generation_lock);
496 static inline void ll_sai_free(struct ll_statahead_info *sai)
498 LASSERT(sai->sai_dentry != NULL);
499 dput(sai->sai_dentry);
504 * take refcount of sai if sai for @dir exists, which means statahead is on for
507 static inline struct ll_statahead_info *ll_sai_get(struct inode *dir)
509 struct ll_inode_info *lli = ll_i2info(dir);
510 struct ll_statahead_info *sai = NULL;
512 spin_lock(&lli->lli_sa_lock);
515 atomic_inc(&sai->sai_refcount);
516 spin_unlock(&lli->lli_sa_lock);
522 * put sai refcount after use, if refcount reaches zero, free sai and sa_entries
525 static void ll_sai_put(struct ll_statahead_info *sai)
527 struct ll_inode_info *lli = ll_i2info(sai->sai_dentry->d_inode);
529 if (atomic_dec_and_lock(&sai->sai_refcount, &lli->lli_sa_lock)) {
530 struct sa_entry *entry, *next;
531 struct ll_sb_info *sbi = ll_i2sbi(sai->sai_dentry->d_inode);
534 spin_unlock(&lli->lli_sa_lock);
536 LASSERT(!sai->sai_task);
537 LASSERT(!sai->sai_agl_task);
538 LASSERT(sai->sai_sent == sai->sai_replied);
539 LASSERT(!sa_has_callback(sai));
541 list_for_each_entry_safe(entry, next, &sai->sai_entries,
545 LASSERT(atomic_read(&sai->sai_cache_count) == 0);
546 LASSERT(agl_list_empty(sai));
549 atomic_dec(&sbi->ll_sa_running);
553 /* Do NOT forget to drop inode refcount when into sai_agls. */
554 static void ll_agl_trigger(struct inode *inode, struct ll_statahead_info *sai)
556 struct ll_inode_info *lli = ll_i2info(inode);
557 u64 index = lli->lli_agl_index;
563 LASSERT(list_empty(&lli->lli_agl_list));
565 /* AGL maybe fall behind statahead with one entry */
566 if (is_omitted_entry(sai, index + 1)) {
567 lli->lli_agl_index = 0;
573 * In case of restore, the MDT has the right size and has already
574 * sent it back without granting the layout lock, inode is up-to-date.
575 * Then AGL (async glimpse lock) is useless.
576 * Also to glimpse we need the layout, in case of a runninh restore
577 * the MDT holds the layout lock so the glimpse will block up to the
578 * end of restore (statahead/agl will block)
580 if (ll_file_test_flag(lli, LLIF_FILE_RESTORING)) {
581 lli->lli_agl_index = 0;
586 /* Someone is in glimpse (sync or async), do nothing. */
587 rc = down_write_trylock(&lli->lli_glimpse_sem);
589 lli->lli_agl_index = 0;
595 * Someone triggered glimpse within 1 sec before.
596 * 1) The former glimpse succeeded with glimpse lock granted by OST, and
597 * if the lock is still cached on client, AGL needs to do nothing. If
598 * it is cancelled by other client, AGL maybe cannot obtaion new lock
599 * for no glimpse callback triggered by AGL.
600 * 2) The former glimpse succeeded, but OST did not grant glimpse lock.
601 * Under such case, it is quite possible that the OST will not grant
602 * glimpse lock for AGL also.
603 * 3) The former glimpse failed, compared with other two cases, it is
604 * relative rare. AGL can ignore such case, and it will not muchly
605 * affect the performance.
607 expire = ktime_sub_ns(ktime_get(), NSEC_PER_SEC);
608 if (ktime_to_ns(lli->lli_glimpse_time) &&
609 ktime_before(expire, lli->lli_glimpse_time)) {
610 up_write(&lli->lli_glimpse_sem);
611 lli->lli_agl_index = 0;
617 "Handling (init) async glimpse: inode = " DFID", idx = %llu\n",
618 PFID(&lli->lli_fid), index);
621 lli->lli_agl_index = 0;
622 lli->lli_glimpse_time = ktime_get();
623 up_write(&lli->lli_glimpse_sem);
626 "Handled (init) async glimpse: inode= " DFID", idx = %llu, rc = %d\n",
627 PFID(&lli->lli_fid), index, rc);
635 * prepare inode for sa entry, add it into agl list, now sa_entry is ready
636 * to be used by scanner process.
638 static void sa_instantiate(struct ll_statahead_info *sai,
639 struct sa_entry *entry)
641 struct inode *dir = sai->sai_dentry->d_inode;
643 struct md_enqueue_info *minfo;
644 struct lookup_intent *it;
645 struct ptlrpc_request *req;
646 struct mdt_body *body;
651 LASSERT(entry->se_handle != 0);
653 minfo = entry->se_minfo;
656 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
658 GOTO(out, rc = -EFAULT);
660 child = entry->se_inode;
661 /* revalidate; unlinked and re-created with the same name */
662 if (unlikely(!lu_fid_eq(&minfo->mi_data.op_fid2, &body->mbo_fid1))) {
664 entry->se_inode = NULL;
667 /* The mdt_body is invalid. Skip this entry */
668 GOTO(out, rc = -EAGAIN);
671 it->it_lock_handle = entry->se_handle;
672 rc = md_revalidate_lock(ll_i2mdexp(dir), it, ll_inode2fid(dir), NULL);
674 GOTO(out, rc = -EAGAIN);
676 rc = ll_prep_inode(&child, req, dir->i_sb, it);
680 CDEBUG(D_READA, "%s: setting %.*s"DFID" l_data to inode %p\n",
681 ll_i2sbi(dir)->ll_fsname, entry->se_qstr.len,
682 entry->se_qstr.name, PFID(ll_inode2fid(child)), child);
683 ll_set_lock_data(ll_i2sbi(dir)->ll_md_exp, child, it, NULL);
685 entry->se_inode = child;
687 if (agl_should_run(sai, child))
688 ll_agl_add(sai, child, entry->se_index);
694 * sa_make_ready() will drop ldlm ibits lock refcount by calling
695 * ll_intent_drop_lock() in spite of failures. Do not worry about
696 * calling ll_intent_drop_lock() more than once.
698 sa_make_ready(sai, entry, rc);
701 /* once there are async stat replies, instantiate sa_entry from replies */
702 static void sa_handle_callback(struct ll_statahead_info *sai)
704 struct ll_inode_info *lli;
706 lli = ll_i2info(sai->sai_dentry->d_inode);
708 spin_lock(&lli->lli_sa_lock);
709 while (sa_has_callback(sai)) {
710 struct sa_entry *entry;
712 entry = list_entry(sai->sai_interim_entries.next,
713 struct sa_entry, se_list);
714 list_del_init(&entry->se_list);
715 spin_unlock(&lli->lli_sa_lock);
717 sa_instantiate(sai, entry);
718 spin_lock(&lli->lli_sa_lock);
720 spin_unlock(&lli->lli_sa_lock);
724 * callback for async stat RPC, because this is called in ptlrpcd context, we
725 * only put sa_entry in sai_interim_entries, and wake up statahead thread to
726 * really prepare inode and instantiate sa_entry later.
728 static int ll_statahead_interpret(struct ptlrpc_request *req,
729 struct md_enqueue_info *minfo, int rc)
731 struct lookup_intent *it = &minfo->mi_it;
732 struct inode *dir = minfo->mi_dir;
733 struct ll_inode_info *lli = ll_i2info(dir);
734 struct ll_statahead_info *sai = lli->lli_sai;
735 struct sa_entry *entry = (struct sa_entry *)minfo->mi_cbdata;
740 if (it_disposition(it, DISP_LOOKUP_NEG))
744 * because statahead thread will wait for all inflight RPC to finish,
745 * sai should be always valid, no need to refcount
747 LASSERT(sai != NULL);
748 LASSERT(entry != NULL);
750 CDEBUG(D_READA, "sa_entry %.*s rc %d\n",
751 entry->se_qstr.len, entry->se_qstr.name, rc);
754 ll_intent_release(it);
758 * release ibits lock ASAP to avoid deadlock when statahead
759 * thread enqueues lock on parent in readdir and another
760 * process enqueues lock on child with parent lock held, eg.
763 handle = it->it_lock_handle;
764 ll_intent_drop_lock(it);
765 ll_unlock_md_op_lsm(&minfo->mi_data);
768 spin_lock(&lli->lli_sa_lock);
770 if (__sa_make_ready(sai, entry, rc))
771 wake_up(&sai->sai_waitq);
775 entry->se_minfo = minfo;
776 entry->se_req = ptlrpc_request_addref(req);
778 * Release the async ibits lock ASAP to avoid deadlock
779 * when statahead thread tries to enqueue lock on parent
780 * for readpage and other tries to enqueue lock on child
781 * with parent's lock held, for example: unlink.
783 entry->se_handle = handle;
784 if (!sa_has_callback(sai))
787 list_add_tail(&entry->se_list, &sai->sai_interim_entries);
788 if (first && sai->sai_task)
789 wake_up_process(sai->sai_task);
793 spin_unlock(&lli->lli_sa_lock);
798 /* async stat for file not found in dcache */
799 static int sa_lookup(struct inode *dir, struct sa_entry *entry)
801 struct md_enqueue_info *minfo;
806 minfo = sa_prep_data(dir, NULL, entry);
808 RETURN(PTR_ERR(minfo));
810 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo);
818 * async stat for file found in dcache, similar to .revalidate
820 * \retval 1 dentry valid, no RPC sent
821 * \retval 0 dentry invalid, will send async stat RPC
822 * \retval negative number upon error
824 static int sa_revalidate(struct inode *dir, struct sa_entry *entry,
825 struct dentry *dentry)
827 struct inode *inode = dentry->d_inode;
828 struct lookup_intent it = { .it_op = IT_GETATTR,
829 .it_lock_handle = 0 };
830 struct md_enqueue_info *minfo;
835 if (unlikely(!inode))
838 if (d_mountpoint(dentry))
841 minfo = sa_prep_data(dir, inode, entry);
843 RETURN(PTR_ERR(minfo));
845 entry->se_inode = igrab(inode);
846 rc = md_revalidate_lock(ll_i2mdexp(dir), &it, ll_inode2fid(inode),
849 entry->se_handle = it.it_lock_handle;
850 ll_intent_release(&it);
855 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo);
857 entry->se_inode = NULL;
865 /* async stat for file with @name */
866 static void sa_statahead(struct dentry *parent, const char *name, int len,
867 const struct lu_fid *fid)
869 struct inode *dir = parent->d_inode;
870 struct ll_inode_info *lli = ll_i2info(dir);
871 struct ll_statahead_info *sai = lli->lli_sai;
872 struct dentry *dentry = NULL;
873 struct sa_entry *entry;
878 entry = sa_alloc(parent, sai, sai->sai_index, name, len, fid);
882 dentry = d_lookup(parent, &entry->se_qstr);
884 rc = sa_lookup(dir, entry);
886 rc = sa_revalidate(dir, entry, dentry);
887 if (rc == 1 && agl_should_run(sai, dentry->d_inode))
888 ll_agl_add(sai, dentry->d_inode, entry->se_index);
895 sa_make_ready(sai, entry, rc);
904 /* async glimpse (agl) thread main function */
905 static int ll_agl_thread(void *arg)
907 struct dentry *parent = (struct dentry *)arg;
908 struct inode *dir = parent->d_inode;
909 struct ll_inode_info *plli = ll_i2info(dir);
910 struct ll_inode_info *clli;
912 * We already own this reference, so it is safe to take it
915 struct ll_statahead_info *sai = plli->lli_sai;
919 CDEBUG(D_READA, "agl thread started: sai %p, parent %pd\n",
922 while (({set_current_state(TASK_IDLE);
923 !kthread_should_stop(); })) {
924 spin_lock(&plli->lli_agl_lock);
925 if (!agl_list_empty(sai)) {
926 __set_current_state(TASK_RUNNING);
927 clli = agl_first_entry(sai);
928 list_del_init(&clli->lli_agl_list);
929 spin_unlock(&plli->lli_agl_lock);
930 ll_agl_trigger(&clli->lli_vfs_inode, sai);
933 spin_unlock(&plli->lli_agl_lock);
937 __set_current_state(TASK_RUNNING);
941 static void ll_stop_agl(struct ll_statahead_info *sai)
943 struct dentry *parent = sai->sai_dentry;
944 struct ll_inode_info *plli = ll_i2info(parent->d_inode);
945 struct ll_inode_info *clli;
946 struct task_struct *agl_task;
948 spin_lock(&plli->lli_agl_lock);
949 agl_task = sai->sai_agl_task;
950 sai->sai_agl_task = NULL;
951 spin_unlock(&plli->lli_agl_lock);
955 CDEBUG(D_READA, "stop agl thread: sai %p pid %u\n",
956 sai, (unsigned int)agl_task->pid);
957 kthread_stop(agl_task);
959 spin_lock(&plli->lli_agl_lock);
960 while (!agl_list_empty(sai)) {
961 clli = agl_first_entry(sai);
962 list_del_init(&clli->lli_agl_list);
963 spin_unlock(&plli->lli_agl_lock);
964 clli->lli_agl_index = 0;
965 iput(&clli->lli_vfs_inode);
966 spin_lock(&plli->lli_agl_lock);
968 spin_unlock(&plli->lli_agl_lock);
969 CDEBUG(D_READA, "agl thread stopped: sai %p, parent %pd\n",
974 /* start agl thread */
975 static void ll_start_agl(struct dentry *parent, struct ll_statahead_info *sai)
977 int node = cfs_cpt_spread_node(cfs_cpt_tab, CFS_CPT_ANY);
978 struct ll_inode_info *plli;
979 struct task_struct *task;
983 CDEBUG(D_READA, "start agl thread: sai %p, parent %pd\n",
986 plli = ll_i2info(parent->d_inode);
987 task = kthread_create_on_node(ll_agl_thread, parent, node, "ll_agl_%d",
988 plli->lli_opendir_pid);
990 CERROR("can't start ll_agl thread, rc: %ld\n", PTR_ERR(task));
993 sai->sai_agl_task = task;
994 atomic_inc(&ll_i2sbi(d_inode(parent))->ll_agl_total);
995 /* Get an extra reference that the thread holds */
996 ll_sai_get(d_inode(parent));
998 wake_up_process(task);
1003 /* statahead thread main function */
1004 static int ll_statahead_thread(void *arg)
1006 struct dentry *parent = (struct dentry *)arg;
1007 struct inode *dir = parent->d_inode;
1008 struct ll_inode_info *lli = ll_i2info(dir);
1009 struct ll_sb_info *sbi = ll_i2sbi(dir);
1010 struct ll_statahead_info *sai = lli->lli_sai;
1012 struct md_op_data *op_data;
1013 struct page *page = NULL;
1019 CDEBUG(D_READA, "statahead thread starting: sai %p, parent %pd\n",
1022 OBD_ALLOC_PTR(op_data);
1024 GOTO(out, rc = -ENOMEM);
1026 while (pos != MDS_DIR_END_OFF && sai->sai_task) {
1027 struct lu_dirpage *dp;
1028 struct lu_dirent *ent;
1030 op_data = ll_prep_md_op_data(op_data, dir, dir, NULL, 0, 0,
1031 LUSTRE_OPC_ANY, dir);
1032 if (IS_ERR(op_data)) {
1033 rc = PTR_ERR(op_data);
1037 sai->sai_in_readpage = 1;
1038 page = ll_get_dir_page(dir, op_data, pos);
1039 ll_unlock_md_op_lsm(op_data);
1040 sai->sai_in_readpage = 0;
1044 "error reading dir "DFID" at %llu /%llu opendir_pid = %u: rc = %d\n",
1045 PFID(ll_inode2fid(dir)), pos, sai->sai_index,
1046 lli->lli_opendir_pid, rc);
1050 dp = page_address(page);
1051 for (ent = lu_dirent_start(dp);
1052 ent != NULL && sai->sai_task &&
1054 ent = lu_dirent_next(ent)) {
1060 hash = le64_to_cpu(ent->lde_hash);
1061 if (unlikely(hash < pos))
1063 * Skip until we find target hash value.
1067 namelen = le16_to_cpu(ent->lde_namelen);
1068 if (unlikely(namelen == 0))
1070 * Skip dummy record.
1074 name = ent->lde_name;
1075 if (name[0] == '.') {
1081 } else if (name[1] == '.' && namelen == 2) {
1086 } else if (!sai->sai_ls_all) {
1088 * skip hidden files.
1090 sai->sai_skip_hidden++;
1096 * don't stat-ahead first entry.
1098 if (unlikely(++first == 1))
1101 fid_le_to_cpu(&fid, &ent->lde_fid);
1103 while (({set_current_state(TASK_IDLE);
1104 sai->sai_task; })) {
1105 if (sa_has_callback(sai)) {
1106 __set_current_state(TASK_RUNNING);
1107 sa_handle_callback(sai);
1110 spin_lock(&lli->lli_agl_lock);
1111 while (sa_sent_full(sai) &&
1112 !agl_list_empty(sai)) {
1113 struct ll_inode_info *clli;
1115 __set_current_state(TASK_RUNNING);
1116 clli = agl_first_entry(sai);
1117 list_del_init(&clli->lli_agl_list);
1118 spin_unlock(&lli->lli_agl_lock);
1120 ll_agl_trigger(&clli->lli_vfs_inode,
1123 spin_lock(&lli->lli_agl_lock);
1125 spin_unlock(&lli->lli_agl_lock);
1127 if (!sa_sent_full(sai))
1131 __set_current_state(TASK_RUNNING);
1133 sa_statahead(parent, name, namelen, &fid);
1136 pos = le64_to_cpu(dp->ldp_hash_end);
1137 ll_release_page(dir, page,
1138 le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1140 if (sa_low_hit(sai)) {
1142 atomic_inc(&sbi->ll_sa_wrong);
1144 "Statahead for dir "DFID" hit ratio too low: hit/miss %llu/%llu, sent/replied %llu/%llu, stoppingstatahead thread: pid %d\n",
1145 PFID(&lli->lli_fid), sai->sai_hit,
1146 sai->sai_miss, sai->sai_sent,
1147 sai->sai_replied, current->pid);
1151 ll_finish_md_op_data(op_data);
1154 spin_lock(&lli->lli_sa_lock);
1155 sai->sai_task = NULL;
1156 lli->lli_sa_enabled = 0;
1157 spin_unlock(&lli->lli_sa_lock);
1161 * statahead is finished, but statahead entries need to be cached, wait
1162 * for file release to stop me.
1164 while (({set_current_state(TASK_IDLE);
1165 sai->sai_task; })) {
1166 if (sa_has_callback(sai)) {
1167 __set_current_state(TASK_RUNNING);
1168 sa_handle_callback(sai);
1173 __set_current_state(TASK_RUNNING);
1180 * wait for inflight statahead RPCs to finish, and then we can free sai
1181 * safely because statahead RPC will access sai data
1183 while (sai->sai_sent != sai->sai_replied)
1184 /* in case we're not woken up, timeout wait */
1187 /* release resources held by statahead RPCs */
1188 sa_handle_callback(sai);
1190 CDEBUG(D_READA, "%s: statahead thread stopped: sai %p, parent %pd\n",
1191 sbi->ll_fsname, sai, parent);
1193 spin_lock(&lli->lli_sa_lock);
1194 sai->sai_task = NULL;
1195 spin_unlock(&lli->lli_sa_lock);
1196 wake_up(&sai->sai_waitq);
1203 /* authorize opened dir handle @key to statahead */
1204 void ll_authorize_statahead(struct inode *dir, void *key)
1206 struct ll_inode_info *lli = ll_i2info(dir);
1208 spin_lock(&lli->lli_sa_lock);
1209 if (!lli->lli_opendir_key && !lli->lli_sai) {
1211 * if lli_sai is not NULL, it means previous statahead is not
1212 * finished yet, we'd better not start a new statahead for now.
1214 LASSERT(lli->lli_opendir_pid == 0);
1215 lli->lli_opendir_key = key;
1216 lli->lli_opendir_pid = current->pid;
1217 lli->lli_sa_enabled = 1;
1219 spin_unlock(&lli->lli_sa_lock);
1223 * deauthorize opened dir handle @key to statahead, and notify statahead thread
1224 * to quit if it's running.
1226 void ll_deauthorize_statahead(struct inode *dir, void *key)
1228 struct ll_inode_info *lli = ll_i2info(dir);
1229 struct ll_statahead_info *sai;
1231 LASSERT(lli->lli_opendir_key == key);
1232 LASSERT(lli->lli_opendir_pid != 0);
1234 CDEBUG(D_READA, "deauthorize statahead for "DFID"\n",
1235 PFID(&lli->lli_fid));
1237 spin_lock(&lli->lli_sa_lock);
1238 lli->lli_opendir_key = NULL;
1239 lli->lli_opendir_pid = 0;
1240 lli->lli_sa_enabled = 0;
1242 if (sai && sai->sai_task) {
1244 * statahead thread may not have quit yet because it needs to
1245 * cache entries, now it's time to tell it to quit.
1247 * wake_up_process() provides the necessary barriers
1248 * to pair with set_current_state().
1250 struct task_struct *task = sai->sai_task;
1252 sai->sai_task = NULL;
1253 wake_up_process(task);
1255 spin_unlock(&lli->lli_sa_lock);
1260 * not first dirent, or is "."
1262 LS_NOT_FIRST_DE = 0,
1264 * the first non-hidden dirent
1268 * the first hidden dirent, that is "."
1273 /* file is first dirent under @dir */
1274 static int is_first_dirent(struct inode *dir, struct dentry *dentry)
1276 struct qstr *target = &dentry->d_name;
1277 struct md_op_data *op_data;
1279 struct page *page = NULL;
1280 int rc = LS_NOT_FIRST_DE;
1285 op_data = ll_prep_md_op_data(NULL, dir, dir, NULL, 0, 0,
1286 LUSTRE_OPC_ANY, dir);
1287 if (IS_ERR(op_data))
1288 RETURN(PTR_ERR(op_data));
1290 *FIXME choose the start offset of the readdir
1293 page = ll_get_dir_page(dir, op_data, 0);
1296 struct lu_dirpage *dp;
1297 struct lu_dirent *ent;
1300 struct ll_inode_info *lli = ll_i2info(dir);
1303 CERROR("%s: reading dir "DFID" at %llu opendir_pid = %u : rc = %d\n",
1304 ll_i2sbi(dir)->ll_fsname,
1305 PFID(ll_inode2fid(dir)), pos,
1306 lli->lli_opendir_pid, rc);
1310 dp = page_address(page);
1311 for (ent = lu_dirent_start(dp); ent != NULL;
1312 ent = lu_dirent_next(ent)) {
1317 hash = le64_to_cpu(ent->lde_hash);
1319 * The ll_get_dir_page() can return any page containing
1320 * the given hash which may be not the start hash.
1322 if (unlikely(hash < pos))
1325 namelen = le16_to_cpu(ent->lde_namelen);
1326 if (unlikely(namelen == 0))
1328 * skip dummy record.
1332 name = ent->lde_name;
1333 if (name[0] == '.') {
1339 else if (name[1] == '.' && namelen == 2)
1350 if (dot_de && target->name[0] != '.') {
1351 CDEBUG(D_READA, "%.*s skip hidden file %.*s\n",
1352 target->len, target->name,
1357 if (target->len != namelen ||
1358 memcmp(target->name, name, namelen) != 0)
1359 rc = LS_NOT_FIRST_DE;
1363 rc = LS_FIRST_DOT_DE;
1365 ll_release_page(dir, page, false);
1368 pos = le64_to_cpu(dp->ldp_hash_end);
1369 if (pos == MDS_DIR_END_OFF) {
1371 * End of directory reached.
1373 ll_release_page(dir, page, false);
1377 * chain is exhausted
1378 * Normal case: continue to the next page.
1380 ll_release_page(dir, page, le32_to_cpu(dp->ldp_flags) &
1382 page = ll_get_dir_page(dir, op_data, pos);
1387 ll_finish_md_op_data(op_data);
1393 * revalidate @dentryp from statahead cache
1395 * \param[in] dir parent directory
1396 * \param[in] sai sai structure
1397 * \param[out] dentryp pointer to dentry which will be revalidated
1398 * \param[in] unplug unplug statahead window only (normally for negative
1400 * \retval 1 on success, dentry is saved in @dentryp
1401 * \retval 0 if revalidation failed (no proper lock on client)
1402 * \retval negative number upon error
1404 static int revalidate_statahead_dentry(struct inode *dir,
1405 struct ll_statahead_info *sai,
1406 struct dentry **dentryp,
1409 struct sa_entry *entry = NULL;
1410 struct ll_dentry_data *ldd;
1411 struct ll_inode_info *lli = ll_i2info(dir);
1416 if ((*dentryp)->d_name.name[0] == '.') {
1417 if (sai->sai_ls_all ||
1418 sai->sai_miss_hidden >= sai->sai_skip_hidden) {
1420 * Hidden dentry is the first one, or statahead
1421 * thread does not skip so many hidden dentries
1422 * before "sai_ls_all" enabled as below.
1425 if (!sai->sai_ls_all)
1427 * It maybe because hidden dentry is not
1428 * the first one, "sai_ls_all" was not
1429 * set, then "ls -al" missed. Enable
1430 * "sai_ls_all" for such case.
1432 sai->sai_ls_all = 1;
1435 * Such "getattr" has been skipped before
1436 * "sai_ls_all" enabled as above.
1438 sai->sai_miss_hidden++;
1446 entry = sa_get(sai, &(*dentryp)->d_name);
1448 GOTO(out, rc = -EAGAIN);
1450 /* if statahead is busy in readdir, help it do post-work */
1451 if (!sa_ready(entry) && sai->sai_in_readpage)
1452 sa_handle_callback(sai);
1454 if (!sa_ready(entry)) {
1455 spin_lock(&lli->lli_sa_lock);
1456 sai->sai_index_wait = entry->se_index;
1457 spin_unlock(&lli->lli_sa_lock);
1458 rc = wait_event_idle_timeout(sai->sai_waitq, sa_ready(entry),
1459 cfs_time_seconds(30));
1462 * entry may not be ready, so it may be used by inflight
1463 * statahead RPC, don't free it.
1466 GOTO(out, rc = -EAGAIN);
1471 * We need to see the value that was set immediately before we
1474 if (smp_load_acquire(&entry->se_state) == SA_ENTRY_SUCC &&
1476 struct inode *inode = entry->se_inode;
1477 struct lookup_intent it = { .it_op = IT_GETATTR,
1482 rc = md_revalidate_lock(ll_i2mdexp(dir), &it,
1483 ll_inode2fid(inode), &bits);
1485 if (!(*dentryp)->d_inode) {
1486 struct dentry *alias;
1488 alias = ll_splice_alias(inode, *dentryp);
1489 if (IS_ERR(alias)) {
1490 ll_intent_release(&it);
1491 GOTO(out, rc = PTR_ERR(alias));
1495 * statahead prepared this inode, transfer inode
1496 * refcount from sa_entry to dentry
1498 entry->se_inode = NULL;
1499 } else if ((*dentryp)->d_inode != inode) {
1500 /* revalidate, but inode is recreated */
1502 "%s: stale dentry %pd inode " DFID", statahead inode "DFID "\n",
1503 ll_i2sbi(inode)->ll_fsname, *dentryp,
1504 PFID(ll_inode2fid((*dentryp)->d_inode)),
1505 PFID(ll_inode2fid(inode)));
1506 ll_intent_release(&it);
1507 GOTO(out, rc = -ESTALE);
1510 if ((bits & MDS_INODELOCK_LOOKUP) &&
1511 d_lustre_invalid(*dentryp))
1512 d_lustre_revalidate(*dentryp);
1513 ll_intent_release(&it);
1518 * statahead cached sa_entry can be used only once, and will be killed
1519 * right after use, so if lookup/revalidate accessed statahead cache,
1520 * set dentry ldd_sa_generation to parent lli_sa_generation, later if we
1521 * stat this file again, we know we've done statahead before, see
1522 * dentry_may_statahead().
1524 ldd = ll_d2d(*dentryp);
1525 /* ldd can be NULL if llite lookup failed. */
1527 ldd->lld_sa_generation = lli->lli_sa_generation;
1529 spin_lock(&lli->lli_sa_lock);
1531 wake_up_process(sai->sai_task);
1532 spin_unlock(&lli->lli_sa_lock);
1538 * start statahead thread
1540 * \param[in] dir parent directory
1541 * \param[in] dentry dentry that triggers statahead, normally the first
1543 * \param[in] agl indicate whether AGL is needed
1544 * \retval -EAGAIN on success, because when this function is
1545 * called, it's already in lookup call, so client should
1546 * do it itself instead of waiting for statahead thread
1547 * to do it asynchronously.
1548 * \retval negative number upon error
1550 static int start_statahead_thread(struct inode *dir, struct dentry *dentry,
1553 int node = cfs_cpt_spread_node(cfs_cpt_tab, CFS_CPT_ANY);
1554 struct ll_inode_info *lli = ll_i2info(dir);
1555 struct ll_statahead_info *sai = NULL;
1556 struct dentry *parent = dentry->d_parent;
1557 struct task_struct *task;
1558 struct ll_sb_info *sbi = ll_i2sbi(parent->d_inode);
1559 int first = LS_FIRST_DE;
1564 /* I am the "lli_opendir_pid" owner, only me can set "lli_sai". */
1565 first = is_first_dirent(dir, dentry);
1566 if (first == LS_NOT_FIRST_DE)
1567 /* It is not "ls -{a}l" operation, no need statahead for it. */
1568 GOTO(out, rc = -EFAULT);
1570 if (unlikely(atomic_inc_return(&sbi->ll_sa_running) >
1571 sbi->ll_sa_running_max)) {
1573 "Too many concurrent statahead instances, avoid new statahead instance temporarily.\n");
1574 GOTO(out, rc = -EMFILE);
1577 sai = ll_sai_alloc(parent);
1579 GOTO(out, rc = -ENOMEM);
1581 sai->sai_ls_all = (first == LS_FIRST_DOT_DE);
1584 * if current lli_opendir_key was deauthorized, or dir re-opened by
1585 * another process, don't start statahead, otherwise the newly spawned
1586 * statahead thread won't be notified to quit.
1588 spin_lock(&lli->lli_sa_lock);
1589 if (unlikely(lli->lli_sai || !lli->lli_opendir_key ||
1590 lli->lli_opendir_pid != current->pid)) {
1591 spin_unlock(&lli->lli_sa_lock);
1592 GOTO(out, rc = -EPERM);
1595 spin_unlock(&lli->lli_sa_lock);
1597 CDEBUG(D_READA, "start statahead thread: [pid %d] [parent %pd]\n",
1598 current->pid, parent);
1600 task = kthread_create_on_node(ll_statahead_thread, parent, node,
1601 "ll_sa_%u", lli->lli_opendir_pid);
1603 spin_lock(&lli->lli_sa_lock);
1604 lli->lli_sai = NULL;
1605 spin_unlock(&lli->lli_sa_lock);
1607 CERROR("can't start ll_sa thread, rc: %d\n", rc);
1611 if (ll_i2sbi(parent->d_inode)->ll_flags & LL_SBI_AGL_ENABLED && agl)
1612 ll_start_agl(parent, sai);
1614 atomic_inc(&ll_i2sbi(parent->d_inode)->ll_sa_total);
1615 sai->sai_task = task;
1617 wake_up_process(task);
1619 * We don't stat-ahead for the first dirent since we are already in
1626 * once we start statahead thread failed, disable statahead so that
1627 * subsequent stat won't waste time to try it.
1629 spin_lock(&lli->lli_sa_lock);
1630 if (lli->lli_opendir_pid == current->pid)
1631 lli->lli_sa_enabled = 0;
1632 spin_unlock(&lli->lli_sa_lock);
1636 if (first != LS_NOT_FIRST_DE)
1637 atomic_dec(&sbi->ll_sa_running);
1643 * Check whether statahead for @dir was started.
1645 static inline bool ll_statahead_started(struct inode *dir, bool agl)
1647 struct ll_inode_info *lli = ll_i2info(dir);
1648 struct ll_statahead_info *sai;
1650 spin_lock(&lli->lli_sa_lock);
1652 if (sai && (sai->sai_agl_task != NULL) != agl)
1654 "%s: Statahead AGL hint changed from %d to %d\n",
1655 ll_i2sbi(dir)->ll_fsname,
1656 sai->sai_agl_task != NULL, agl);
1657 spin_unlock(&lli->lli_sa_lock);
1663 * statahead entry function, this is called when client getattr on a file, it
1664 * will start statahead thread if this is the first dir entry, else revalidate
1665 * dentry from statahead cache.
1667 * \param[in] dir parent directory
1668 * \param[out] dentryp dentry to getattr
1669 * \param[in] agl whether start the agl thread
1671 * \retval 1 on success
1672 * \retval 0 revalidation from statahead cache failed, caller needs
1673 * to getattr from server directly
1674 * \retval negative number on error, caller often ignores this and
1675 * then getattr from server
1677 int ll_start_statahead(struct inode *dir, struct dentry *dentry, bool agl)
1679 if (!ll_statahead_started(dir, agl))
1680 return start_statahead_thread(dir, dentry, agl);
1685 * revalidate dentry from statahead cache.
1687 * \param[in] dir parent directory
1688 * \param[out] dentryp dentry to getattr
1689 * \param[in] unplug unplug statahead window only (normally for negative
1691 * \retval 1 on success
1692 * \retval 0 revalidation from statahead cache failed, caller needs
1693 * to getattr from server directly
1694 * \retval negative number on error, caller often ignores this and
1695 * then getattr from server
1697 int ll_revalidate_statahead(struct inode *dir, struct dentry **dentryp,
1700 struct ll_statahead_info *sai;
1703 sai = ll_sai_get(dir);
1705 rc = revalidate_statahead_dentry(dir, sai, dentryp, unplug);
1706 CDEBUG(D_READA, "revalidate statahead %pd: rc = %d.\n",