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 */
56 /* sa_entry is not refcounted: statahead thread allocates it and do async stat,
57 * and in async stat callback ll_statahead_interpret() will add it into
58 * sai_interim_entries, later statahead thread will call sa_handle_callback() to
59 * instantiate entry and move it into sai_entries, and then only scanner process
60 * can access and free it. */
62 /* link into sai_interim_entries or sai_entries */
63 struct list_head se_list;
64 /* link into sai hash table locally */
65 struct list_head se_hash;
66 /* entry index in the sai */
68 /* low layer ldlm lock handle */
72 /* entry size, contains name */
74 /* pointer to async getattr enqueue info */
75 struct md_enqueue_info *se_minfo;
76 /* pointer to the async getattr request */
77 struct ptlrpc_request *se_req;
78 /* pointer to the target inode */
79 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);
94 /* sa_entry is ready to use */
95 static inline int sa_ready(struct sa_entry *entry)
98 return (entry->se_state != SA_ENTRY_INIT);
101 /* hash value to put in sai_cache */
102 static inline int sa_hash(int val)
104 return val & LL_SA_CACHE_MASK;
107 /* hash entry into sai_cache */
109 sa_rehash(struct ll_statahead_info *sai, struct sa_entry *entry)
111 int i = sa_hash(entry->se_qstr.hash);
113 spin_lock(&sai->sai_cache_lock[i]);
114 list_add_tail(&entry->se_hash, &sai->sai_cache[i]);
115 spin_unlock(&sai->sai_cache_lock[i]);
118 /* unhash entry from sai_cache */
120 sa_unhash(struct ll_statahead_info *sai, struct sa_entry *entry)
122 int i = sa_hash(entry->se_qstr.hash);
124 spin_lock(&sai->sai_cache_lock[i]);
125 list_del_init(&entry->se_hash);
126 spin_unlock(&sai->sai_cache_lock[i]);
129 static inline int agl_should_run(struct ll_statahead_info *sai,
132 return (inode != NULL && S_ISREG(inode->i_mode) && sai->sai_agl_valid);
135 static inline struct ll_inode_info *
136 agl_first_entry(struct ll_statahead_info *sai)
138 return list_entry(sai->sai_agls.next, struct ll_inode_info,
142 /* statahead window is full */
143 static inline int sa_sent_full(struct ll_statahead_info *sai)
145 return atomic_read(&sai->sai_cache_count) >= sai->sai_max;
148 /* got async stat replies */
149 static inline int sa_has_callback(struct ll_statahead_info *sai)
151 return !list_empty(&sai->sai_interim_entries);
154 static inline int agl_list_empty(struct ll_statahead_info *sai)
156 return list_empty(&sai->sai_agls);
160 * (1) hit ratio less than 80%
162 * (2) consecutive miss more than 8
163 * then means low hit.
165 static inline int sa_low_hit(struct ll_statahead_info *sai)
167 return ((sai->sai_hit > 7 && sai->sai_hit < 4 * sai->sai_miss) ||
168 (sai->sai_consecutive_miss > 8));
172 * if the given index is behind of statahead window more than
173 * SA_OMITTED_ENTRY_MAX, then it is old.
175 static inline int is_omitted_entry(struct ll_statahead_info *sai, __u64 index)
177 return ((__u64)sai->sai_max + index + SA_OMITTED_ENTRY_MAX <
181 /* allocate sa_entry and hash it to allow scanner process to find it */
182 static struct sa_entry *
183 sa_alloc(struct dentry *parent, struct ll_statahead_info *sai, __u64 index,
184 const char *name, int len, const struct lu_fid *fid)
186 struct ll_inode_info *lli;
187 struct sa_entry *entry;
192 entry_size = sizeof(struct sa_entry) + (len & ~3) + 4;
193 OBD_ALLOC(entry, entry_size);
194 if (unlikely(entry == NULL))
195 RETURN(ERR_PTR(-ENOMEM));
197 CDEBUG(D_READA, "alloc sa entry %.*s(%p) index %llu\n",
198 len, name, entry, index);
200 entry->se_index = index;
202 entry->se_state = SA_ENTRY_INIT;
203 entry->se_size = entry_size;
204 dname = (char *)entry + sizeof(struct sa_entry);
205 memcpy(dname, name, len);
207 entry->se_qstr.hash = ll_full_name_hash(parent, name, len);
208 entry->se_qstr.len = len;
209 entry->se_qstr.name = dname;
210 entry->se_fid = *fid;
212 lli = ll_i2info(sai->sai_dentry->d_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 %llu\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);
239 * find sa_entry by name, used by directory scanner, lock is not needed because
240 * only scanner can remove the entry from cache.
242 static struct sa_entry *
243 sa_get(struct ll_statahead_info *sai, const struct qstr *qstr)
245 struct sa_entry *entry;
246 int i = sa_hash(qstr->hash);
248 list_for_each_entry(entry, &sai->sai_cache[i], se_hash) {
249 if (entry->se_qstr.hash == qstr->hash &&
250 entry->se_qstr.len == qstr->len &&
251 memcmp(entry->se_qstr.name, qstr->name, qstr->len) == 0)
257 /* unhash and unlink sa_entry, and then free it */
259 sa_kill(struct ll_statahead_info *sai, struct sa_entry *entry)
261 struct ll_inode_info *lli = ll_i2info(sai->sai_dentry->d_inode);
263 LASSERT(!sa_unhashed(entry));
264 LASSERT(!list_empty(&entry->se_list));
265 LASSERT(sa_ready(entry));
267 sa_unhash(sai, entry);
269 spin_lock(&lli->lli_sa_lock);
270 list_del_init(&entry->se_list);
271 spin_unlock(&lli->lli_sa_lock);
273 if (entry->se_inode != NULL)
274 iput(entry->se_inode);
279 /* called by scanner after use, sa_entry will be killed */
281 sa_put(struct ll_statahead_info *sai, struct sa_entry *entry)
283 struct sa_entry *tmp, *next;
285 if (entry != NULL && entry->se_state == SA_ENTRY_SUCC) {
286 struct ll_sb_info *sbi = ll_i2sbi(sai->sai_dentry->d_inode);
289 sai->sai_consecutive_miss = 0;
290 sai->sai_max = min(2 * sai->sai_max, sbi->ll_sa_max);
293 sai->sai_consecutive_miss++;
299 /* kill old completed entries, only scanner process does this, no need
301 list_for_each_entry_safe(tmp, next, &sai->sai_entries, se_list) {
302 if (!is_omitted_entry(sai, tmp->se_index))
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;
315 __u64 index = entry->se_index;
317 LASSERT(!sa_ready(entry));
318 LASSERT(list_empty(&entry->se_list));
320 list_for_each_entry_reverse(se, &sai->sai_entries, se_list) {
321 if (se->se_index < entry->se_index) {
326 list_add(&entry->se_list, pos);
328 * LU-9210: ll_statahead_interpet must be able to see this before
331 smp_store_release(&entry->se_state, ret < 0 ? SA_ENTRY_INVA : SA_ENTRY_SUCC);
333 return (index == sai->sai_index_wait);
336 /* finish async stat RPC arguments */
337 static void sa_fini_data(struct md_enqueue_info *minfo)
339 ll_unlock_md_op_lsm(&minfo->mi_data);
344 static int ll_statahead_interpret(struct ptlrpc_request *req,
345 struct md_enqueue_info *minfo, int rc);
348 * prepare arguments for async stat RPC.
350 static struct md_enqueue_info *
351 sa_prep_data(struct inode *dir, struct inode *child, struct sa_entry *entry)
353 struct md_enqueue_info *minfo;
354 struct ldlm_enqueue_info *einfo;
355 struct md_op_data *op_data;
357 OBD_ALLOC_PTR(minfo);
359 return ERR_PTR(-ENOMEM);
361 op_data = ll_prep_md_op_data(&minfo->mi_data, dir, child,
362 entry->se_qstr.name, entry->se_qstr.len, 0,
363 LUSTRE_OPC_ANY, NULL);
364 if (IS_ERR(op_data)) {
366 return (struct md_enqueue_info *)op_data;
370 op_data->op_fid2 = entry->se_fid;
372 minfo->mi_it.it_op = IT_GETATTR;
373 minfo->mi_dir = igrab(dir);
374 minfo->mi_cb = ll_statahead_interpret;
375 minfo->mi_cbdata = entry;
377 einfo = &minfo->mi_einfo;
378 einfo->ei_type = LDLM_IBITS;
379 einfo->ei_mode = it_to_lock_mode(&minfo->mi_it);
380 einfo->ei_cb_bl = ll_md_blocking_ast;
381 einfo->ei_cb_cp = ldlm_completion_ast;
382 einfo->ei_cb_gl = NULL;
383 einfo->ei_cbdata = NULL;
389 * release resources used in async stat RPC, update entry state and wakeup if
390 * scanner process it waiting on this entry.
393 sa_make_ready(struct ll_statahead_info *sai, struct sa_entry *entry, int ret)
395 struct ll_inode_info *lli = ll_i2info(sai->sai_dentry->d_inode);
396 struct md_enqueue_info *minfo = entry->se_minfo;
397 struct ptlrpc_request *req = entry->se_req;
400 /* release resources used in RPC */
402 entry->se_minfo = NULL;
403 ll_intent_release(&minfo->mi_it);
408 entry->se_req = NULL;
409 ptlrpc_req_finished(req);
412 spin_lock(&lli->lli_sa_lock);
413 wakeup = __sa_make_ready(sai, entry, ret);
414 spin_unlock(&lli->lli_sa_lock);
417 wake_up(&sai->sai_waitq);
420 /* insert inode into the list of sai_agls */
421 static void ll_agl_add(struct ll_statahead_info *sai,
422 struct inode *inode, int index)
424 struct ll_inode_info *child = ll_i2info(inode);
425 struct ll_inode_info *parent = ll_i2info(sai->sai_dentry->d_inode);
428 spin_lock(&child->lli_agl_lock);
429 if (child->lli_agl_index == 0) {
430 child->lli_agl_index = index;
431 spin_unlock(&child->lli_agl_lock);
433 LASSERT(list_empty(&child->lli_agl_list));
436 spin_lock(&parent->lli_agl_lock);
437 if (agl_list_empty(sai))
439 list_add_tail(&child->lli_agl_list, &sai->sai_agls);
440 if (added && sai->sai_agl_task)
441 wake_up_process(sai->sai_agl_task);
442 spin_unlock(&parent->lli_agl_lock);
444 spin_unlock(&child->lli_agl_lock);
449 static struct ll_statahead_info *ll_sai_alloc(struct dentry *dentry)
451 struct ll_statahead_info *sai;
452 struct ll_inode_info *lli = ll_i2info(dentry->d_inode);
460 sai->sai_dentry = dget(dentry);
461 atomic_set(&sai->sai_refcount, 1);
462 sai->sai_max = LL_SA_RPC_MIN;
464 init_waitqueue_head(&sai->sai_waitq);
466 INIT_LIST_HEAD(&sai->sai_interim_entries);
467 INIT_LIST_HEAD(&sai->sai_entries);
468 INIT_LIST_HEAD(&sai->sai_agls);
470 for (i = 0; i < LL_SA_CACHE_SIZE; i++) {
471 INIT_LIST_HEAD(&sai->sai_cache[i]);
472 spin_lock_init(&sai->sai_cache_lock[i]);
474 atomic_set(&sai->sai_cache_count, 0);
476 spin_lock(&sai_generation_lock);
477 lli->lli_sa_generation = ++sai_generation;
478 if (unlikely(sai_generation == 0))
479 lli->lli_sa_generation = ++sai_generation;
480 spin_unlock(&sai_generation_lock);
486 static inline void ll_sai_free(struct ll_statahead_info *sai)
488 LASSERT(sai->sai_dentry != NULL);
489 dput(sai->sai_dentry);
494 * take refcount of sai if sai for @dir exists, which means statahead is on for
497 static inline struct ll_statahead_info *ll_sai_get(struct inode *dir)
499 struct ll_inode_info *lli = ll_i2info(dir);
500 struct ll_statahead_info *sai = NULL;
502 spin_lock(&lli->lli_sa_lock);
505 atomic_inc(&sai->sai_refcount);
506 spin_unlock(&lli->lli_sa_lock);
512 * put sai refcount after use, if refcount reaches zero, free sai and sa_entries
515 static void ll_sai_put(struct ll_statahead_info *sai)
517 struct ll_inode_info *lli = ll_i2info(sai->sai_dentry->d_inode);
519 if (atomic_dec_and_lock(&sai->sai_refcount, &lli->lli_sa_lock)) {
520 struct sa_entry *entry, *next;
521 struct ll_sb_info *sbi = ll_i2sbi(sai->sai_dentry->d_inode);
524 spin_unlock(&lli->lli_sa_lock);
526 LASSERT(!sai->sai_task);
527 LASSERT(!sai->sai_agl_task);
528 LASSERT(sai->sai_sent == sai->sai_replied);
529 LASSERT(!sa_has_callback(sai));
531 list_for_each_entry_safe(entry, next, &sai->sai_entries,
535 LASSERT(atomic_read(&sai->sai_cache_count) == 0);
536 LASSERT(agl_list_empty(sai));
539 atomic_dec(&sbi->ll_sa_running);
543 /* Do NOT forget to drop inode refcount when into sai_agls. */
544 static void ll_agl_trigger(struct inode *inode, struct ll_statahead_info *sai)
546 struct ll_inode_info *lli = ll_i2info(inode);
547 u64 index = lli->lli_agl_index;
552 LASSERT(list_empty(&lli->lli_agl_list));
554 /* AGL maybe fall behind statahead with one entry */
555 if (is_omitted_entry(sai, index + 1)) {
556 lli->lli_agl_index = 0;
561 /* In case of restore, the MDT has the right size and has already
562 * sent it back without granting the layout lock, inode is up-to-date.
563 * Then AGL (async glimpse lock) is useless.
564 * Also to glimpse we need the layout, in case of a runninh restore
565 * the MDT holds the layout lock so the glimpse will block up to the
566 * end of restore (statahead/agl will block) */
567 if (ll_file_test_flag(lli, LLIF_FILE_RESTORING)) {
568 lli->lli_agl_index = 0;
573 /* Someone is in glimpse (sync or async), do nothing. */
574 rc = down_write_trylock(&lli->lli_glimpse_sem);
576 lli->lli_agl_index = 0;
582 * Someone triggered glimpse within 1 sec before.
583 * 1) The former glimpse succeeded with glimpse lock granted by OST, and
584 * if the lock is still cached on client, AGL needs to do nothing. If
585 * it is cancelled by other client, AGL maybe cannot obtaion new lock
586 * for no glimpse callback triggered by AGL.
587 * 2) The former glimpse succeeded, but OST did not grant glimpse lock.
588 * Under such case, it is quite possible that the OST will not grant
589 * glimpse lock for AGL also.
590 * 3) The former glimpse failed, compared with other two cases, it is
591 * relative rare. AGL can ignore such case, and it will not muchly
592 * affect the performance.
594 expire = ktime_sub_ns(ktime_get(), NSEC_PER_SEC);
595 if (ktime_to_ns(lli->lli_glimpse_time) &&
596 ktime_before(expire, lli->lli_glimpse_time)) {
597 up_write(&lli->lli_glimpse_sem);
598 lli->lli_agl_index = 0;
603 CDEBUG(D_READA, "Handling (init) async glimpse: inode = "
604 DFID", idx = %llu\n", PFID(&lli->lli_fid), index);
607 lli->lli_agl_index = 0;
608 lli->lli_glimpse_time = ktime_get();
609 up_write(&lli->lli_glimpse_sem);
611 CDEBUG(D_READA, "Handled (init) async glimpse: inode= "
612 DFID", idx = %llu, rc = %d\n",
613 PFID(&lli->lli_fid), index, rc);
621 * prepare inode for sa entry, add it into agl list, now sa_entry is ready
622 * to be used by scanner process.
624 static void sa_instantiate(struct ll_statahead_info *sai,
625 struct sa_entry *entry)
627 struct inode *dir = sai->sai_dentry->d_inode;
629 struct md_enqueue_info *minfo;
630 struct lookup_intent *it;
631 struct ptlrpc_request *req;
632 struct mdt_body *body;
636 LASSERT(entry->se_handle != 0);
638 minfo = entry->se_minfo;
641 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
643 GOTO(out, rc = -EFAULT);
645 child = entry->se_inode;
647 /* revalidate; unlinked and re-created with the same name */
648 if (unlikely(!lu_fid_eq(&minfo->mi_data.op_fid2,
650 entry->se_inode = NULL;
656 it->it_lock_handle = entry->se_handle;
657 rc = md_revalidate_lock(ll_i2mdexp(dir), it, ll_inode2fid(dir), NULL);
659 GOTO(out, rc = -EAGAIN);
661 rc = ll_prep_inode(&child, req, dir->i_sb, it);
665 CDEBUG(D_READA, "%s: setting %.*s"DFID" l_data to inode %p\n",
666 ll_i2sbi(dir)->ll_fsname, entry->se_qstr.len,
667 entry->se_qstr.name, PFID(ll_inode2fid(child)), child);
668 ll_set_lock_data(ll_i2sbi(dir)->ll_md_exp, child, it, NULL);
670 entry->se_inode = child;
672 if (agl_should_run(sai, child))
673 ll_agl_add(sai, child, entry->se_index);
678 /* sa_make_ready() will drop ldlm ibits lock refcount by calling
679 * ll_intent_drop_lock() in spite of failures. Do not worry about
680 * calling ll_intent_drop_lock() more than once. */
681 sa_make_ready(sai, entry, rc);
684 /* once there are async stat replies, instantiate sa_entry from replies */
685 static void sa_handle_callback(struct ll_statahead_info *sai)
687 struct ll_inode_info *lli;
689 lli = ll_i2info(sai->sai_dentry->d_inode);
691 spin_lock(&lli->lli_sa_lock);
692 while (sa_has_callback(sai)) {
693 struct sa_entry *entry;
695 entry = list_entry(sai->sai_interim_entries.next,
696 struct sa_entry, se_list);
697 list_del_init(&entry->se_list);
698 spin_unlock(&lli->lli_sa_lock);
700 sa_instantiate(sai, entry);
701 spin_lock(&lli->lli_sa_lock);
703 spin_unlock(&lli->lli_sa_lock);
707 * callback for async stat RPC, because this is called in ptlrpcd context, we
708 * only put sa_entry in sai_interim_entries, and wake up statahead thread to
709 * really prepare inode and instantiate sa_entry later.
711 static int ll_statahead_interpret(struct ptlrpc_request *req,
712 struct md_enqueue_info *minfo, int rc)
714 struct lookup_intent *it = &minfo->mi_it;
715 struct inode *dir = minfo->mi_dir;
716 struct ll_inode_info *lli = ll_i2info(dir);
717 struct ll_statahead_info *sai = lli->lli_sai;
718 struct sa_entry *entry = (struct sa_entry *)minfo->mi_cbdata;
722 if (it_disposition(it, DISP_LOOKUP_NEG))
725 /* because statahead thread will wait for all inflight RPC to finish,
726 * sai should be always valid, no need to refcount */
727 LASSERT(sai != NULL);
728 LASSERT(entry != NULL);
730 CDEBUG(D_READA, "sa_entry %.*s rc %d\n",
731 entry->se_qstr.len, entry->se_qstr.name, rc);
734 ll_intent_release(it);
737 /* release ibits lock ASAP to avoid deadlock when statahead
738 * thread enqueues lock on parent in readdir and another
739 * process enqueues lock on child with parent lock held, eg.
741 handle = it->it_lock_handle;
742 ll_intent_drop_lock(it);
743 ll_unlock_md_op_lsm(&minfo->mi_data);
746 spin_lock(&lli->lli_sa_lock);
748 if (__sa_make_ready(sai, entry, rc))
749 wake_up(&sai->sai_waitq);
753 entry->se_minfo = minfo;
754 entry->se_req = ptlrpc_request_addref(req);
755 /* Release the async ibits lock ASAP to avoid deadlock
756 * when statahead thread tries to enqueue lock on parent
757 * for readpage and other tries to enqueue lock on child
758 * with parent's lock held, for example: unlink. */
759 entry->se_handle = handle;
760 if (!sa_has_callback(sai))
763 list_add_tail(&entry->se_list, &sai->sai_interim_entries);
764 if (first && sai->sai_task)
765 wake_up_process(sai->sai_task);
769 spin_unlock(&lli->lli_sa_lock);
774 /* async stat for file not found in dcache */
775 static int sa_lookup(struct inode *dir, struct sa_entry *entry)
777 struct md_enqueue_info *minfo;
781 minfo = sa_prep_data(dir, NULL, entry);
783 RETURN(PTR_ERR(minfo));
785 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo);
793 * async stat for file found in dcache, similar to .revalidate
795 * \retval 1 dentry valid, no RPC sent
796 * \retval 0 dentry invalid, will send async stat RPC
797 * \retval negative number upon error
799 static int sa_revalidate(struct inode *dir, struct sa_entry *entry,
800 struct dentry *dentry)
802 struct inode *inode = dentry->d_inode;
803 struct lookup_intent it = { .it_op = IT_GETATTR,
804 .it_lock_handle = 0 };
805 struct md_enqueue_info *minfo;
809 if (unlikely(inode == NULL))
812 if (d_mountpoint(dentry))
815 minfo = sa_prep_data(dir, inode, entry);
817 RETURN(PTR_ERR(minfo));
819 entry->se_inode = igrab(inode);
820 rc = md_revalidate_lock(ll_i2mdexp(dir), &it, ll_inode2fid(inode),
823 entry->se_handle = it.it_lock_handle;
824 ll_intent_release(&it);
829 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo);
831 entry->se_inode = NULL;
839 /* async stat for file with @name */
840 static void sa_statahead(struct dentry *parent, const char *name, int len,
841 const struct lu_fid *fid)
843 struct inode *dir = parent->d_inode;
844 struct ll_inode_info *lli = ll_i2info(dir);
845 struct ll_statahead_info *sai = lli->lli_sai;
846 struct dentry *dentry = NULL;
847 struct sa_entry *entry;
851 entry = sa_alloc(parent, sai, sai->sai_index, name, len, fid);
855 dentry = d_lookup(parent, &entry->se_qstr);
857 rc = sa_lookup(dir, entry);
859 rc = sa_revalidate(dir, entry, dentry);
860 if (rc == 1 && agl_should_run(sai, dentry->d_inode))
861 ll_agl_add(sai, dentry->d_inode, entry->se_index);
868 sa_make_ready(sai, entry, rc);
878 #define TASK_IDLE TASK_INTERRUPTIBLE
881 /* async glimpse (agl) thread main function */
882 static int ll_agl_thread(void *arg)
884 struct dentry *parent = (struct dentry *)arg;
885 struct inode *dir = parent->d_inode;
886 struct ll_inode_info *plli = ll_i2info(dir);
887 struct ll_inode_info *clli;
888 /* We already own this reference, so it is safe to take it
891 struct ll_statahead_info *sai = plli->lli_sai;
894 CDEBUG(D_READA, "agl thread started: sai %p, parent %.*s\n",
895 sai, parent->d_name.len, parent->d_name.name);
897 while (({set_current_state(TASK_IDLE);
898 !kthread_should_stop(); })) {
899 spin_lock(&plli->lli_agl_lock);
900 if (!agl_list_empty(sai)) {
901 __set_current_state(TASK_RUNNING);
902 clli = agl_first_entry(sai);
903 list_del_init(&clli->lli_agl_list);
904 spin_unlock(&plli->lli_agl_lock);
905 ll_agl_trigger(&clli->lli_vfs_inode, sai);
908 spin_unlock(&plli->lli_agl_lock);
912 __set_current_state(TASK_RUNNING);
916 static void ll_stop_agl(struct ll_statahead_info *sai)
918 struct dentry *parent = sai->sai_dentry;
919 struct ll_inode_info *plli = ll_i2info(parent->d_inode);
920 struct ll_inode_info *clli;
921 struct task_struct *agl_task;
923 spin_lock(&plli->lli_agl_lock);
924 agl_task = sai->sai_agl_task;
925 sai->sai_agl_task = NULL;
926 spin_unlock(&plli->lli_agl_lock);
930 CDEBUG(D_READA, "stop agl thread: sai %p pid %u\n",
931 sai, (unsigned int)agl_task->pid);
932 kthread_stop(agl_task);
934 spin_lock(&plli->lli_agl_lock);
935 sai->sai_agl_valid = 0;
936 while (!agl_list_empty(sai)) {
937 clli = agl_first_entry(sai);
938 list_del_init(&clli->lli_agl_list);
939 spin_unlock(&plli->lli_agl_lock);
940 clli->lli_agl_index = 0;
941 iput(&clli->lli_vfs_inode);
942 spin_lock(&plli->lli_agl_lock);
944 spin_unlock(&plli->lli_agl_lock);
945 CDEBUG(D_READA, "agl thread stopped: sai %p, parent %.*s\n",
946 sai, parent->d_name.len, parent->d_name.name);
950 /* start agl thread */
951 static void ll_start_agl(struct dentry *parent, struct ll_statahead_info *sai)
953 struct ll_inode_info *plli;
954 struct task_struct *task;
957 CDEBUG(D_READA, "start agl thread: sai %p, parent %.*s\n",
958 sai, parent->d_name.len, parent->d_name.name);
960 plli = ll_i2info(parent->d_inode);
961 task = kthread_create(ll_agl_thread, parent,
962 "ll_agl_%u", plli->lli_opendir_pid);
964 CERROR("can't start ll_agl thread, rc: %ld\n", PTR_ERR(task));
967 sai->sai_agl_task = task;
968 sai->sai_agl_valid = 1;
969 atomic_inc(&ll_i2sbi(d_inode(parent))->ll_agl_total);
970 /* Get an extra reference that the thread holds */
971 ll_sai_get(d_inode(parent));
973 wake_up_process(task);
978 /* statahead thread main function */
979 static int ll_statahead_thread(void *arg)
981 struct dentry *parent = (struct dentry *)arg;
982 struct inode *dir = parent->d_inode;
983 struct ll_inode_info *lli = ll_i2info(dir);
984 struct ll_sb_info *sbi = ll_i2sbi(dir);
985 struct ll_statahead_info *sai = lli->lli_sai;
987 struct md_op_data *op_data;
988 struct ll_dir_chain chain;
989 struct page *page = NULL;
994 CDEBUG(D_READA, "statahead thread starting: sai %p, parent %.*s\n",
995 sai, parent->d_name.len, parent->d_name.name);
997 OBD_ALLOC_PTR(op_data);
999 GOTO(out, rc = -ENOMEM);
1001 ll_dir_chain_init(&chain);
1002 while (pos != MDS_DIR_END_OFF && sai->sai_task) {
1003 struct lu_dirpage *dp;
1004 struct lu_dirent *ent;
1006 op_data = ll_prep_md_op_data(op_data, dir, dir, NULL, 0, 0,
1007 LUSTRE_OPC_ANY, dir);
1008 if (IS_ERR(op_data)) {
1009 rc = PTR_ERR(op_data);
1013 sai->sai_in_readpage = 1;
1014 page = ll_get_dir_page(dir, op_data, pos, &chain);
1015 ll_unlock_md_op_lsm(op_data);
1016 sai->sai_in_readpage = 0;
1019 CDEBUG(D_READA, "error reading dir "DFID" at %llu"
1020 "/%llu 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 && sai->sai_task &&
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 while (({set_current_state(TASK_IDLE);
1080 sai->sai_task; })) {
1081 if (sa_has_callback(sai)) {
1082 __set_current_state(TASK_RUNNING);
1083 sa_handle_callback(sai);
1086 spin_lock(&lli->lli_agl_lock);
1087 while (sa_sent_full(sai) &&
1088 !agl_list_empty(sai)) {
1089 struct ll_inode_info *clli;
1091 __set_current_state(TASK_RUNNING);
1092 clli = agl_first_entry(sai);
1093 list_del_init(&clli->lli_agl_list);
1094 spin_unlock(&lli->lli_agl_lock);
1096 ll_agl_trigger(&clli->lli_vfs_inode,
1099 spin_lock(&lli->lli_agl_lock);
1101 spin_unlock(&lli->lli_agl_lock);
1103 if (!sa_sent_full(sai))
1107 __set_current_state(TASK_RUNNING);
1109 sa_statahead(parent, name, namelen, &fid);
1112 pos = le64_to_cpu(dp->ldp_hash_end);
1113 ll_release_page(dir, page,
1114 le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1116 if (sa_low_hit(sai)) {
1118 atomic_inc(&sbi->ll_sa_wrong);
1119 CDEBUG(D_READA, "Statahead for dir "DFID" hit "
1120 "ratio too low: hit/miss %llu/%llu"
1121 ", sent/replied %llu/%llu, stopping "
1122 "statahead thread: pid %d\n",
1123 PFID(&lli->lli_fid), sai->sai_hit,
1124 sai->sai_miss, sai->sai_sent,
1125 sai->sai_replied, current->pid);
1129 ll_dir_chain_fini(&chain);
1130 ll_finish_md_op_data(op_data);
1133 spin_lock(&lli->lli_sa_lock);
1134 sai->sai_task = NULL;
1135 lli->lli_sa_enabled = 0;
1136 spin_unlock(&lli->lli_sa_lock);
1139 /* statahead is finished, but statahead entries need to be cached, wait
1140 * for file release to stop me.
1142 while (({set_current_state(TASK_IDLE);
1143 sai->sai_task; })) {
1144 if (sa_has_callback(sai)) {
1145 __set_current_state(TASK_RUNNING);
1146 sa_handle_callback(sai);
1151 __set_current_state(TASK_RUNNING);
1157 /* wait for inflight statahead RPCs to finish, and then we can free sai
1158 * safely because statahead RPC will access sai data */
1159 while (sai->sai_sent != sai->sai_replied)
1160 /* in case we're not woken up, timeout wait */
1163 /* release resources held by statahead RPCs */
1164 sa_handle_callback(sai);
1166 CDEBUG(D_READA, "statahead thread stopped: sai %p, parent %.*s\n",
1167 sai, parent->d_name.len, parent->d_name.name);
1169 spin_lock(&lli->lli_sa_lock);
1170 sai->sai_task = NULL;
1171 spin_unlock(&lli->lli_sa_lock);
1172 wake_up(&sai->sai_waitq);
1179 /* authorize opened dir handle @key to statahead */
1180 void ll_authorize_statahead(struct inode *dir, void *key)
1182 struct ll_inode_info *lli = ll_i2info(dir);
1184 spin_lock(&lli->lli_sa_lock);
1185 if (lli->lli_opendir_key == NULL && lli->lli_sai == NULL) {
1187 * if lli_sai is not NULL, it means previous statahead is not
1188 * finished yet, we'd better not start a new statahead for now.
1190 LASSERT(lli->lli_opendir_pid == 0);
1191 lli->lli_opendir_key = key;
1192 lli->lli_opendir_pid = current->pid;
1193 lli->lli_sa_enabled = 1;
1195 spin_unlock(&lli->lli_sa_lock);
1199 * deauthorize opened dir handle @key to statahead, and notify statahead thread
1200 * to quit if it's running.
1202 void ll_deauthorize_statahead(struct inode *dir, void *key)
1204 struct ll_inode_info *lli = ll_i2info(dir);
1205 struct ll_statahead_info *sai;
1207 LASSERT(lli->lli_opendir_key == key);
1208 LASSERT(lli->lli_opendir_pid != 0);
1210 CDEBUG(D_READA, "deauthorize statahead for "DFID"\n",
1211 PFID(&lli->lli_fid));
1213 spin_lock(&lli->lli_sa_lock);
1214 lli->lli_opendir_key = NULL;
1215 lli->lli_opendir_pid = 0;
1216 lli->lli_sa_enabled = 0;
1218 if (sai && sai->sai_task) {
1220 * statahead thread may not have quit yet because it needs to
1221 * cache entries, now it's time to tell it to quit.
1223 * wake_up_process() provides the necessary barriers
1224 * to pair with set_current_state().
1226 struct task_struct *task = sai->sai_task;
1228 sai->sai_task = NULL;
1229 wake_up_process(task);
1231 spin_unlock(&lli->lli_sa_lock);
1236 * not first dirent, or is "."
1238 LS_NOT_FIRST_DE = 0,
1240 * the first non-hidden dirent
1244 * the first hidden dirent, that is "."
1249 /* file is first dirent under @dir */
1250 static int is_first_dirent(struct inode *dir, struct dentry *dentry)
1252 struct ll_dir_chain chain;
1253 struct qstr *target = &dentry->d_name;
1254 struct md_op_data *op_data;
1256 struct page *page = NULL;
1257 int rc = LS_NOT_FIRST_DE;
1261 op_data = ll_prep_md_op_data(NULL, dir, dir, NULL, 0, 0,
1262 LUSTRE_OPC_ANY, dir);
1263 if (IS_ERR(op_data))
1264 RETURN(PTR_ERR(op_data));
1266 *FIXME choose the start offset of the readdir
1269 ll_dir_chain_init(&chain);
1270 page = ll_get_dir_page(dir, op_data, 0, &chain);
1273 struct lu_dirpage *dp;
1274 struct lu_dirent *ent;
1277 struct ll_inode_info *lli = ll_i2info(dir);
1280 CERROR("%s: reading dir "DFID" at %llu opendir_pid = %u : rc = %d\n",
1281 ll_i2sbi(dir)->ll_fsname,
1282 PFID(ll_inode2fid(dir)), pos,
1283 lli->lli_opendir_pid, rc);
1287 dp = page_address(page);
1288 for (ent = lu_dirent_start(dp); ent != NULL;
1289 ent = lu_dirent_next(ent)) {
1294 hash = le64_to_cpu(ent->lde_hash);
1295 /* The ll_get_dir_page() can return any page containing
1296 * the given hash which may be not the start hash. */
1297 if (unlikely(hash < pos))
1300 namelen = le16_to_cpu(ent->lde_namelen);
1301 if (unlikely(namelen == 0))
1303 * skip dummy record.
1307 name = ent->lde_name;
1308 if (name[0] == '.') {
1314 else if (name[1] == '.' && namelen == 2)
1325 if (dot_de && target->name[0] != '.') {
1326 CDEBUG(D_READA, "%.*s skip hidden file %.*s\n",
1327 target->len, target->name,
1332 if (target->len != namelen ||
1333 memcmp(target->name, name, namelen) != 0)
1334 rc = LS_NOT_FIRST_DE;
1338 rc = LS_FIRST_DOT_DE;
1340 ll_release_page(dir, page, false);
1343 pos = le64_to_cpu(dp->ldp_hash_end);
1344 if (pos == MDS_DIR_END_OFF) {
1346 * End of directory reached.
1348 ll_release_page(dir, page, false);
1352 * chain is exhausted
1353 * Normal case: continue to the next page.
1355 ll_release_page(dir, page, le32_to_cpu(dp->ldp_flags) &
1357 page = ll_get_dir_page(dir, op_data, pos, &chain);
1362 ll_dir_chain_fini(&chain);
1363 ll_finish_md_op_data(op_data);
1368 * revalidate @dentryp from statahead cache
1370 * \param[in] dir parent directory
1371 * \param[in] sai sai structure
1372 * \param[out] dentryp pointer to dentry which will be revalidated
1373 * \param[in] unplug unplug statahead window only (normally for negative
1375 * \retval 1 on success, dentry is saved in @dentryp
1376 * \retval 0 if revalidation failed (no proper lock on client)
1377 * \retval negative number upon error
1379 static int revalidate_statahead_dentry(struct inode *dir,
1380 struct ll_statahead_info *sai,
1381 struct dentry **dentryp,
1384 struct sa_entry *entry = NULL;
1385 struct ll_dentry_data *ldd;
1386 struct ll_inode_info *lli = ll_i2info(dir);
1390 if ((*dentryp)->d_name.name[0] == '.') {
1391 if (sai->sai_ls_all ||
1392 sai->sai_miss_hidden >= sai->sai_skip_hidden) {
1394 * Hidden dentry is the first one, or statahead
1395 * thread does not skip so many hidden dentries
1396 * before "sai_ls_all" enabled as below.
1399 if (!sai->sai_ls_all)
1401 * It maybe because hidden dentry is not
1402 * the first one, "sai_ls_all" was not
1403 * set, then "ls -al" missed. Enable
1404 * "sai_ls_all" for such case.
1406 sai->sai_ls_all = 1;
1409 * Such "getattr" has been skipped before
1410 * "sai_ls_all" enabled as above.
1412 sai->sai_miss_hidden++;
1420 entry = sa_get(sai, &(*dentryp)->d_name);
1422 GOTO(out, rc = -EAGAIN);
1424 /* if statahead is busy in readdir, help it do post-work */
1425 if (!sa_ready(entry) && sai->sai_in_readpage)
1426 sa_handle_callback(sai);
1428 if (!sa_ready(entry)) {
1429 spin_lock(&lli->lli_sa_lock);
1430 sai->sai_index_wait = entry->se_index;
1431 spin_unlock(&lli->lli_sa_lock);
1432 rc = wait_event_idle_timeout(sai->sai_waitq, sa_ready(entry),
1433 cfs_time_seconds(30));
1436 * entry may not be ready, so it may be used by inflight
1437 * statahead RPC, don't free it.
1440 GOTO(out, rc = -EAGAIN);
1445 * We need to see the value that was set immediately before we
1448 if (smp_load_acquire(&entry->se_state) == SA_ENTRY_SUCC &&
1450 struct inode *inode = entry->se_inode;
1451 struct lookup_intent it = { .it_op = IT_GETATTR,
1456 rc = md_revalidate_lock(ll_i2mdexp(dir), &it,
1457 ll_inode2fid(inode), &bits);
1459 if ((*dentryp)->d_inode == NULL) {
1460 struct dentry *alias;
1462 alias = ll_splice_alias(inode, *dentryp);
1463 if (IS_ERR(alias)) {
1464 ll_intent_release(&it);
1465 GOTO(out, rc = PTR_ERR(alias));
1468 /* statahead prepared this inode, transfer inode
1469 * refcount from sa_entry to dentry */
1470 entry->se_inode = NULL;
1471 } else if ((*dentryp)->d_inode != inode) {
1472 /* revalidate, but inode is recreated */
1474 "%s: stale dentry %.*s inode "
1475 DFID", statahead inode "DFID
1477 ll_i2sbi(inode)->ll_fsname,
1478 (*dentryp)->d_name.len,
1479 (*dentryp)->d_name.name,
1480 PFID(ll_inode2fid((*dentryp)->d_inode)),
1481 PFID(ll_inode2fid(inode)));
1482 ll_intent_release(&it);
1483 GOTO(out, rc = -ESTALE);
1486 if ((bits & MDS_INODELOCK_LOOKUP) &&
1487 d_lustre_invalid(*dentryp))
1488 d_lustre_revalidate(*dentryp);
1489 ll_intent_release(&it);
1494 * statahead cached sa_entry can be used only once, and will be killed
1495 * right after use, so if lookup/revalidate accessed statahead cache,
1496 * set dentry ldd_sa_generation to parent lli_sa_generation, later if we
1497 * stat this file again, we know we've done statahead before, see
1498 * dentry_may_statahead().
1500 ldd = ll_d2d(*dentryp);
1501 /* ldd can be NULL if llite lookup failed. */
1503 ldd->lld_sa_generation = lli->lli_sa_generation;
1505 spin_lock(&lli->lli_sa_lock);
1507 wake_up_process(sai->sai_task);
1508 spin_unlock(&lli->lli_sa_lock);
1514 * start statahead thread
1516 * \param[in] dir parent directory
1517 * \param[in] dentry dentry that triggers statahead, normally the first
1519 * \retval -EAGAIN on success, because when this function is
1520 * called, it's already in lookup call, so client should
1521 * do it itself instead of waiting for statahead thread
1522 * to do it asynchronously.
1523 * \retval negative number upon error
1525 static int start_statahead_thread(struct inode *dir, struct dentry *dentry)
1527 struct ll_inode_info *lli = ll_i2info(dir);
1528 struct ll_statahead_info *sai = NULL;
1529 struct dentry *parent = dentry->d_parent;
1530 struct task_struct *task;
1531 struct ll_sb_info *sbi = ll_i2sbi(parent->d_inode);
1532 int first = LS_FIRST_DE;
1536 /* I am the "lli_opendir_pid" owner, only me can set "lli_sai". */
1537 first = is_first_dirent(dir, dentry);
1538 if (first == LS_NOT_FIRST_DE)
1539 /* It is not "ls -{a}l" operation, no need statahead for it. */
1540 GOTO(out, rc = -EFAULT);
1542 if (unlikely(atomic_inc_return(&sbi->ll_sa_running) >
1543 sbi->ll_sa_running_max)) {
1545 "Too many concurrent statahead instances, "
1546 "avoid new statahead instance temporarily.\n");
1547 GOTO(out, rc = -EMFILE);
1550 sai = ll_sai_alloc(parent);
1552 GOTO(out, rc = -ENOMEM);
1554 sai->sai_ls_all = (first == LS_FIRST_DOT_DE);
1556 /* if current lli_opendir_key was deauthorized, or dir re-opened by
1557 * another process, don't start statahead, otherwise the newly spawned
1558 * statahead thread won't be notified to quit. */
1559 spin_lock(&lli->lli_sa_lock);
1560 if (unlikely(lli->lli_sai != NULL ||
1561 lli->lli_opendir_key == NULL ||
1562 lli->lli_opendir_pid != current->pid)) {
1563 spin_unlock(&lli->lli_sa_lock);
1564 GOTO(out, rc = -EPERM);
1567 spin_unlock(&lli->lli_sa_lock);
1569 CDEBUG(D_READA, "start statahead thread: [pid %d] [parent %.*s]\n",
1570 current->pid, parent->d_name.len, parent->d_name.name);
1572 task = kthread_create(ll_statahead_thread, parent, "ll_sa_%u",
1573 lli->lli_opendir_pid);
1575 spin_lock(&lli->lli_sa_lock);
1576 lli->lli_sai = NULL;
1577 spin_unlock(&lli->lli_sa_lock);
1579 CERROR("can't start ll_sa thread, rc: %d\n", rc);
1583 if (ll_i2sbi(parent->d_inode)->ll_flags & LL_SBI_AGL_ENABLED)
1584 ll_start_agl(parent, sai);
1586 atomic_inc(&ll_i2sbi(parent->d_inode)->ll_sa_total);
1587 sai->sai_task = task;
1589 wake_up_process(task);
1591 * We don't stat-ahead for the first dirent since we are already in
1597 /* once we start statahead thread failed, disable statahead so that
1598 * subsequent stat won't waste time to try it. */
1599 spin_lock(&lli->lli_sa_lock);
1600 if (lli->lli_opendir_pid == current->pid)
1601 lli->lli_sa_enabled = 0;
1602 spin_unlock(&lli->lli_sa_lock);
1606 if (first != LS_NOT_FIRST_DE)
1607 atomic_dec(&sbi->ll_sa_running);
1613 * statahead entry function, this is called when client getattr on a file, it
1614 * will start statahead thread if this is the first dir entry, else revalidate
1615 * dentry from statahead cache.
1617 * \param[in] dir parent directory
1618 * \param[out] dentryp dentry to getattr
1619 * \param[in] unplug unplug statahead window only (normally for negative
1621 * \retval 1 on success
1622 * \retval 0 revalidation from statahead cache failed, caller needs
1623 * to getattr from server directly
1624 * \retval negative number on error, caller often ignores this and
1625 * then getattr from server
1627 int ll_statahead(struct inode *dir, struct dentry **dentryp, bool unplug)
1629 struct ll_statahead_info *sai;
1631 sai = ll_sai_get(dir);
1635 rc = revalidate_statahead_dentry(dir, sai, dentryp, unplug);
1636 CDEBUG(D_READA, "revalidate statahead %.*s: %d.\n",
1637 (*dentryp)->d_name.len, (*dentryp)->d_name.name, rc);
1641 return start_statahead_thread(dir, *dentryp);