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;
87 static unsigned int sai_generation = 0;
88 static DEFINE_SPINLOCK(sai_generation_lock);
90 static inline int sa_unhashed(struct sa_entry *entry)
92 return list_empty(&entry->se_hash);
95 /* sa_entry is ready to use */
96 static inline int sa_ready(struct sa_entry *entry)
99 return (entry->se_state != SA_ENTRY_INIT);
102 /* hash value to put in sai_cache */
103 static inline int sa_hash(int val)
105 return val & LL_SA_CACHE_MASK;
108 /* hash entry into sai_cache */
110 sa_rehash(struct ll_statahead_info *sai, struct sa_entry *entry)
112 int i = sa_hash(entry->se_qstr.hash);
114 spin_lock(&sai->sai_cache_lock[i]);
115 list_add_tail(&entry->se_hash, &sai->sai_cache[i]);
116 spin_unlock(&sai->sai_cache_lock[i]);
119 /* unhash entry from sai_cache */
121 sa_unhash(struct ll_statahead_info *sai, struct sa_entry *entry)
123 int i = sa_hash(entry->se_qstr.hash);
125 spin_lock(&sai->sai_cache_lock[i]);
126 list_del_init(&entry->se_hash);
127 spin_unlock(&sai->sai_cache_lock[i]);
130 static inline int agl_should_run(struct ll_statahead_info *sai,
133 return (inode != NULL && S_ISREG(inode->i_mode) && sai->sai_agl_valid);
136 static inline struct ll_inode_info *
137 agl_first_entry(struct ll_statahead_info *sai)
139 return list_entry(sai->sai_agls.next, struct ll_inode_info,
143 /* statahead window is full */
144 static inline int sa_sent_full(struct ll_statahead_info *sai)
146 return atomic_read(&sai->sai_cache_count) >= sai->sai_max;
149 /* got async stat replies */
150 static inline int sa_has_callback(struct ll_statahead_info *sai)
152 return !list_empty(&sai->sai_interim_entries);
155 static inline int agl_list_empty(struct ll_statahead_info *sai)
157 return list_empty(&sai->sai_agls);
161 * (1) hit ratio less than 80%
163 * (2) consecutive miss more than 8
164 * then means low hit.
166 static inline int sa_low_hit(struct ll_statahead_info *sai)
168 return ((sai->sai_hit > 7 && sai->sai_hit < 4 * sai->sai_miss) ||
169 (sai->sai_consecutive_miss > 8));
173 * if the given index is behind of statahead window more than
174 * SA_OMITTED_ENTRY_MAX, then it is old.
176 static inline int is_omitted_entry(struct ll_statahead_info *sai, __u64 index)
178 return ((__u64)sai->sai_max + index + SA_OMITTED_ENTRY_MAX <
182 /* allocate sa_entry and hash it to allow scanner process to find it */
183 static struct sa_entry *
184 sa_alloc(struct ll_statahead_info *sai, __u64 index, const char *name, int len)
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 "LPU64"\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 = full_name_hash(name, len);
208 entry->se_qstr.len = len;
209 entry->se_qstr.name = dname;
211 lli = ll_i2info(sai->sai_dentry->d_inode);
213 spin_lock(&lli->lli_sa_lock);
214 INIT_LIST_HEAD(&entry->se_list);
215 sa_rehash(sai, entry);
216 spin_unlock(&lli->lli_sa_lock);
218 atomic_inc(&sai->sai_cache_count);
223 /* free sa_entry, which should have been unhashed and not in any list */
224 static void sa_free(struct ll_statahead_info *sai, struct sa_entry *entry)
226 CDEBUG(D_READA, "free sa entry %.*s(%p) index "LPU64"\n",
227 entry->se_qstr.len, entry->se_qstr.name, entry,
230 LASSERT(list_empty(&entry->se_list));
231 LASSERT(sa_unhashed(entry));
233 OBD_FREE(entry, entry->se_size);
234 atomic_dec(&sai->sai_cache_count);
238 * find sa_entry by name, used by directory scanner, lock is not needed because
239 * only scanner can remove the entry from cache.
241 static struct sa_entry *
242 sa_get(struct ll_statahead_info *sai, const struct qstr *qstr)
244 struct sa_entry *entry;
245 int i = sa_hash(qstr->hash);
247 list_for_each_entry(entry, &sai->sai_cache[i], se_hash) {
248 if (entry->se_qstr.hash == qstr->hash &&
249 entry->se_qstr.len == qstr->len &&
250 memcmp(entry->se_qstr.name, qstr->name, qstr->len) == 0)
256 /* unhash and unlink sa_entry, and then free it */
258 sa_kill(struct ll_statahead_info *sai, struct sa_entry *entry)
260 struct ll_inode_info *lli = ll_i2info(sai->sai_dentry->d_inode);
262 LASSERT(!sa_unhashed(entry));
263 LASSERT(!list_empty(&entry->se_list));
264 LASSERT(sa_ready(entry));
266 sa_unhash(sai, entry);
268 spin_lock(&lli->lli_sa_lock);
269 list_del_init(&entry->se_list);
270 spin_unlock(&lli->lli_sa_lock);
272 if (entry->se_inode != NULL)
273 iput(entry->se_inode);
278 /* called by scanner after use, sa_entry will be killed */
280 sa_put(struct ll_statahead_info *sai, struct sa_entry *entry)
282 struct sa_entry *tmp, *next;
284 if (entry != NULL && entry->se_state == SA_ENTRY_SUCC) {
285 struct ll_sb_info *sbi = ll_i2sbi(sai->sai_dentry->d_inode);
288 sai->sai_consecutive_miss = 0;
289 sai->sai_max = min(2 * sai->sai_max, sbi->ll_sa_max);
292 sai->sai_consecutive_miss++;
298 /* kill old completed entries, only scanner process does this, no need
300 list_for_each_entry_safe(tmp, next, &sai->sai_entries, se_list) {
301 if (!is_omitted_entry(sai, tmp->se_index))
306 wake_up(&sai->sai_thread.t_ctl_waitq);
309 /* update state and sort add entry to sai_entries by index, return true if
310 * scanner is waiting on this entry. */
312 __sa_make_ready(struct ll_statahead_info *sai, struct sa_entry *entry, int ret)
315 struct list_head *pos = &sai->sai_entries;
316 __u64 index = entry->se_index;
318 LASSERT(!sa_ready(entry));
319 LASSERT(list_empty(&entry->se_list));
321 list_for_each_entry_reverse(se, &sai->sai_entries, se_list) {
322 if (se->se_index < entry->se_index) {
327 list_add(&entry->se_list, pos);
328 entry->se_state = ret < 0 ? SA_ENTRY_INVA : SA_ENTRY_SUCC;
330 return (index == sai->sai_index_wait);
334 * release resources used in async stat RPC, update entry state and wakeup if
335 * scanner process it waiting on this entry.
338 sa_make_ready(struct ll_statahead_info *sai, struct sa_entry *entry, int ret)
340 struct ll_inode_info *lli = ll_i2info(sai->sai_dentry->d_inode);
341 struct md_enqueue_info *minfo = entry->se_minfo;
342 struct ptlrpc_request *req = entry->se_req;
345 /* release resources used in RPC */
347 entry->se_minfo = NULL;
348 ll_intent_release(&minfo->mi_it);
354 entry->se_req = NULL;
355 ptlrpc_req_finished(req);
358 spin_lock(&lli->lli_sa_lock);
359 wakeup = __sa_make_ready(sai, entry, ret);
360 spin_unlock(&lli->lli_sa_lock);
363 wake_up(&sai->sai_waitq);
366 /* insert inode into the list of sai_agls */
367 static void ll_agl_add(struct ll_statahead_info *sai,
368 struct inode *inode, int index)
370 struct ll_inode_info *child = ll_i2info(inode);
371 struct ll_inode_info *parent = ll_i2info(sai->sai_dentry->d_inode);
374 spin_lock(&child->lli_agl_lock);
375 if (child->lli_agl_index == 0) {
376 child->lli_agl_index = index;
377 spin_unlock(&child->lli_agl_lock);
379 LASSERT(list_empty(&child->lli_agl_list));
382 spin_lock(&parent->lli_agl_lock);
383 if (agl_list_empty(sai))
385 list_add_tail(&child->lli_agl_list, &sai->sai_agls);
386 spin_unlock(&parent->lli_agl_lock);
388 spin_unlock(&child->lli_agl_lock);
392 wake_up(&sai->sai_agl_thread.t_ctl_waitq);
396 static struct ll_statahead_info *ll_sai_alloc(struct dentry *dentry)
398 struct ll_statahead_info *sai;
399 struct ll_inode_info *lli = ll_i2info(dentry->d_inode);
407 sai->sai_dentry = dget(dentry);
408 atomic_set(&sai->sai_refcount, 1);
409 sai->sai_max = LL_SA_RPC_MIN;
411 init_waitqueue_head(&sai->sai_waitq);
412 init_waitqueue_head(&sai->sai_thread.t_ctl_waitq);
413 init_waitqueue_head(&sai->sai_agl_thread.t_ctl_waitq);
415 INIT_LIST_HEAD(&sai->sai_interim_entries);
416 INIT_LIST_HEAD(&sai->sai_entries);
417 INIT_LIST_HEAD(&sai->sai_agls);
419 for (i = 0; i < LL_SA_CACHE_SIZE; i++) {
420 INIT_LIST_HEAD(&sai->sai_cache[i]);
421 spin_lock_init(&sai->sai_cache_lock[i]);
423 atomic_set(&sai->sai_cache_count, 0);
425 spin_lock(&sai_generation_lock);
426 lli->lli_sa_generation = ++sai_generation;
427 if (unlikely(sai_generation == 0))
428 lli->lli_sa_generation = ++sai_generation;
429 spin_unlock(&sai_generation_lock);
435 static inline void ll_sai_free(struct ll_statahead_info *sai)
437 LASSERT(sai->sai_dentry != NULL);
438 dput(sai->sai_dentry);
443 * take refcount of sai if sai for @dir exists, which means statahead is on for
446 static inline struct ll_statahead_info *ll_sai_get(struct inode *dir)
448 struct ll_inode_info *lli = ll_i2info(dir);
449 struct ll_statahead_info *sai = NULL;
451 spin_lock(&lli->lli_sa_lock);
454 atomic_inc(&sai->sai_refcount);
455 spin_unlock(&lli->lli_sa_lock);
461 * put sai refcount after use, if refcount reaches zero, free sai and sa_entries
464 static void ll_sai_put(struct ll_statahead_info *sai)
466 struct ll_inode_info *lli = ll_i2info(sai->sai_dentry->d_inode);
468 if (atomic_dec_and_lock(&sai->sai_refcount, &lli->lli_sa_lock)) {
469 struct sa_entry *entry, *next;
470 struct ll_sb_info *sbi = ll_i2sbi(sai->sai_dentry->d_inode);
473 spin_unlock(&lli->lli_sa_lock);
475 LASSERT(thread_is_stopped(&sai->sai_thread));
476 LASSERT(thread_is_stopped(&sai->sai_agl_thread));
477 LASSERT(sai->sai_sent == sai->sai_replied);
478 LASSERT(!sa_has_callback(sai));
480 list_for_each_entry_safe(entry, next, &sai->sai_entries,
484 LASSERT(atomic_read(&sai->sai_cache_count) == 0);
485 LASSERT(agl_list_empty(sai));
488 atomic_dec(&sbi->ll_sa_running);
492 /* Do NOT forget to drop inode refcount when into sai_agls. */
493 static void ll_agl_trigger(struct inode *inode, struct ll_statahead_info *sai)
495 struct ll_inode_info *lli = ll_i2info(inode);
496 __u64 index = lli->lli_agl_index;
500 LASSERT(list_empty(&lli->lli_agl_list));
502 /* AGL maybe fall behind statahead with one entry */
503 if (is_omitted_entry(sai, index + 1)) {
504 lli->lli_agl_index = 0;
509 /* Someone is in glimpse (sync or async), do nothing. */
510 rc = down_write_trylock(&lli->lli_glimpse_sem);
512 lli->lli_agl_index = 0;
518 * Someone triggered glimpse within 1 sec before.
519 * 1) The former glimpse succeeded with glimpse lock granted by OST, and
520 * if the lock is still cached on client, AGL needs to do nothing. If
521 * it is cancelled by other client, AGL maybe cannot obtaion new lock
522 * for no glimpse callback triggered by AGL.
523 * 2) The former glimpse succeeded, but OST did not grant glimpse lock.
524 * Under such case, it is quite possible that the OST will not grant
525 * glimpse lock for AGL also.
526 * 3) The former glimpse failed, compared with other two cases, it is
527 * relative rare. AGL can ignore such case, and it will not muchly
528 * affect the performance.
530 if (lli->lli_glimpse_time != 0 &&
531 cfs_time_before(cfs_time_shift(-1), lli->lli_glimpse_time)) {
532 up_write(&lli->lli_glimpse_sem);
533 lli->lli_agl_index = 0;
538 CDEBUG(D_READA, "Handling (init) async glimpse: inode = "
539 DFID", idx = "LPU64"\n", PFID(&lli->lli_fid), index);
542 lli->lli_agl_index = 0;
543 lli->lli_glimpse_time = cfs_time_current();
544 up_write(&lli->lli_glimpse_sem);
546 CDEBUG(D_READA, "Handled (init) async glimpse: inode= "
547 DFID", idx = "LPU64", rc = %d\n",
548 PFID(&lli->lli_fid), index, rc);
556 * prepare inode for sa entry, add it into agl list, now sa_entry is ready
557 * to be used by scanner process.
559 static void sa_instantiate(struct ll_statahead_info *sai,
560 struct sa_entry *entry)
562 struct inode *dir = sai->sai_dentry->d_inode;
564 struct md_enqueue_info *minfo;
565 struct lookup_intent *it;
566 struct ptlrpc_request *req;
567 struct mdt_body *body;
571 LASSERT(entry->se_handle != 0);
573 minfo = entry->se_minfo;
576 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
578 GOTO(out, rc = -EFAULT);
580 child = entry->se_inode;
585 LASSERT(fid_is_zero(&minfo->mi_data.op_fid2));
587 /* XXX: No fid in reply, this is probaly cross-ref case.
588 * SA can't handle it yet. */
589 if (body->mbo_valid & OBD_MD_MDS)
590 GOTO(out, rc = -EAGAIN);
595 /* unlinked and re-created with the same name */
596 if (unlikely(!lu_fid_eq(&minfo->mi_data.op_fid2,
598 entry->se_inode = NULL;
604 it->d.lustre.it_lock_handle = entry->se_handle;
605 rc = md_revalidate_lock(ll_i2mdexp(dir), it, ll_inode2fid(dir), NULL);
607 GOTO(out, rc = -EAGAIN);
609 rc = ll_prep_inode(&child, req, dir->i_sb, it);
613 CDEBUG(D_READA, "%s: setting %.*s"DFID" l_data to inode %p\n",
614 ll_get_fsname(child->i_sb, NULL, 0),
615 entry->se_qstr.len, entry->se_qstr.name,
616 PFID(ll_inode2fid(child)), child);
617 ll_set_lock_data(ll_i2sbi(dir)->ll_md_exp, child, it, NULL);
619 entry->se_inode = child;
621 if (agl_should_run(sai, child))
622 ll_agl_add(sai, child, entry->se_index);
627 /* sa_make_ready() will drop ldlm ibits lock refcount by calling
628 * ll_intent_drop_lock() in spite of failures. Do not worry about
629 * calling ll_intent_drop_lock() more than once. */
630 sa_make_ready(sai, entry, rc);
633 /* once there are async stat replies, instantiate sa_entry from replies */
634 static void sa_handle_callback(struct ll_statahead_info *sai)
636 struct ll_inode_info *lli;
638 lli = ll_i2info(sai->sai_dentry->d_inode);
640 while (sa_has_callback(sai)) {
641 struct sa_entry *entry;
643 spin_lock(&lli->lli_sa_lock);
644 if (unlikely(!sa_has_callback(sai))) {
645 spin_unlock(&lli->lli_sa_lock);
648 entry = list_entry(sai->sai_interim_entries.next,
649 struct sa_entry, se_list);
650 list_del_init(&entry->se_list);
651 spin_unlock(&lli->lli_sa_lock);
653 sa_instantiate(sai, entry);
658 * callback for async stat RPC, because this is called in ptlrpcd context, we
659 * only put sa_entry in sai_interim_entries, and wake up statahead thread to
660 * really prepare inode and instantiate sa_entry later.
662 static int ll_statahead_interpret(struct ptlrpc_request *req,
663 struct md_enqueue_info *minfo, int rc)
665 struct lookup_intent *it = &minfo->mi_it;
666 struct inode *dir = minfo->mi_dir;
667 struct ll_inode_info *lli = ll_i2info(dir);
668 struct ll_statahead_info *sai = lli->lli_sai;
669 struct sa_entry *entry = (struct sa_entry *)minfo->mi_cbdata;
674 if (it_disposition(it, DISP_LOOKUP_NEG))
677 /* because statahead thread will wait for all inflight RPC to finish,
678 * sai should be always valid, no need to refcount */
679 LASSERT(sai != NULL);
680 LASSERT(!thread_is_stopped(&sai->sai_thread));
681 LASSERT(entry != NULL);
683 CDEBUG(D_READA, "sa_entry %.*s rc %d\n",
684 entry->se_qstr.len, entry->se_qstr.name, rc);
687 ll_intent_release(it);
691 /* release ibits lock ASAP to avoid deadlock when statahead
692 * thread enqueues lock on parent in readdir and another
693 * process enqueues lock on child with parent lock held, eg.
695 handle = it->d.lustre.it_lock_handle;
696 ll_intent_drop_lock(it);
699 spin_lock(&lli->lli_sa_lock);
701 wakeup = __sa_make_ready(sai, entry, rc);
703 entry->se_minfo = minfo;
704 entry->se_req = ptlrpc_request_addref(req);
705 /* Release the async ibits lock ASAP to avoid deadlock
706 * when statahead thread tries to enqueue lock on parent
707 * for readpage and other tries to enqueue lock on child
708 * with parent's lock held, for example: unlink. */
709 entry->se_handle = handle;
710 wakeup = !sa_has_callback(sai);
711 list_add_tail(&entry->se_list, &sai->sai_interim_entries);
715 wake_up(&sai->sai_thread.t_ctl_waitq);
716 spin_unlock(&lli->lli_sa_lock);
721 /* finish async stat RPC arguments */
722 static void sa_fini_data(struct md_enqueue_info *minfo,
723 struct ldlm_enqueue_info *einfo)
725 LASSERT(minfo && einfo);
732 * prepare arguments for async stat RPC.
734 static int sa_prep_data(struct inode *dir, struct inode *child,
735 struct sa_entry *entry, struct md_enqueue_info **pmi,
736 struct ldlm_enqueue_info **pei)
738 struct qstr *qstr = &entry->se_qstr;
739 struct md_enqueue_info *minfo;
740 struct ldlm_enqueue_info *einfo;
741 struct md_op_data *op_data;
743 OBD_ALLOC_PTR(einfo);
747 OBD_ALLOC_PTR(minfo);
753 op_data = ll_prep_md_op_data(&minfo->mi_data, dir, child, qstr->name,
754 qstr->len, 0, LUSTRE_OPC_ANY, NULL);
755 if (IS_ERR(op_data)) {
758 return PTR_ERR(op_data);
761 minfo->mi_it.it_op = IT_GETATTR;
762 minfo->mi_dir = igrab(dir);
763 minfo->mi_cb = ll_statahead_interpret;
764 minfo->mi_cbdata = entry;
766 einfo->ei_type = LDLM_IBITS;
767 einfo->ei_mode = it_to_lock_mode(&minfo->mi_it);
768 einfo->ei_cb_bl = ll_md_blocking_ast;
769 einfo->ei_cb_cp = ldlm_completion_ast;
770 einfo->ei_cb_gl = NULL;
771 einfo->ei_cbdata = NULL;
779 /* async stat for file not found in dcache */
780 static int sa_lookup(struct inode *dir, struct sa_entry *entry)
782 struct md_enqueue_info *minfo;
783 struct ldlm_enqueue_info *einfo;
787 rc = sa_prep_data(dir, NULL, entry, &minfo, &einfo);
791 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
793 sa_fini_data(minfo, einfo);
799 * async stat for file found in dcache, similar to .revalidate
801 * \retval 1 dentry valid, no RPC sent
802 * \retval 0 dentry invalid, will send async stat RPC
803 * \retval negative number upon error
805 static int sa_revalidate(struct inode *dir, struct sa_entry *entry,
806 struct dentry *dentry)
808 struct inode *inode = dentry->d_inode;
809 struct lookup_intent it = { .it_op = IT_GETATTR,
810 .d.lustre.it_lock_handle = 0 };
811 struct md_enqueue_info *minfo;
812 struct ldlm_enqueue_info *einfo;
816 if (unlikely(inode == NULL))
819 if (d_mountpoint(dentry))
822 entry->se_inode = igrab(inode);
823 rc = md_revalidate_lock(ll_i2mdexp(dir), &it, ll_inode2fid(inode),
826 entry->se_handle = it.d.lustre.it_lock_handle;
827 ll_intent_release(&it);
831 rc = sa_prep_data(dir, inode, entry, &minfo, &einfo);
833 entry->se_inode = NULL;
838 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
840 entry->se_inode = NULL;
842 sa_fini_data(minfo, einfo);
848 /* async stat for file with @name */
849 static void sa_statahead(struct dentry *parent, const char *name, int len)
851 struct inode *dir = parent->d_inode;
852 struct ll_inode_info *lli = ll_i2info(dir);
853 struct ll_statahead_info *sai = lli->lli_sai;
854 struct dentry *dentry = NULL;
855 struct sa_entry *entry;
859 entry = sa_alloc(sai, sai->sai_index, name, len);
863 dentry = d_lookup(parent, &entry->se_qstr);
865 rc = sa_lookup(dir, entry);
867 rc = sa_revalidate(dir, entry, dentry);
868 if (rc == 1 && agl_should_run(sai, dentry->d_inode))
869 ll_agl_add(sai, dentry->d_inode, entry->se_index);
876 sa_make_ready(sai, entry, rc);
885 /* async glimpse (agl) thread main function */
886 static int ll_agl_thread(void *arg)
888 struct dentry *parent = (struct dentry *)arg;
889 struct inode *dir = parent->d_inode;
890 struct ll_inode_info *plli = ll_i2info(dir);
891 struct ll_inode_info *clli;
892 struct ll_sb_info *sbi = ll_i2sbi(dir);
893 struct ll_statahead_info *sai;
894 struct ptlrpc_thread *thread;
895 struct l_wait_info lwi = { 0 };
899 sai = ll_sai_get(dir);
900 thread = &sai->sai_agl_thread;
901 thread->t_pid = current_pid();
902 CDEBUG(D_READA, "agl thread started: sai %p, parent %.*s\n",
903 sai, parent->d_name.len, parent->d_name.name);
905 atomic_inc(&sbi->ll_agl_total);
906 spin_lock(&plli->lli_agl_lock);
907 sai->sai_agl_valid = 1;
908 if (thread_is_init(thread))
909 /* If someone else has changed the thread state
910 * (e.g. already changed to SVC_STOPPING), we can't just
911 * blindly overwrite that setting. */
912 thread_set_flags(thread, SVC_RUNNING);
913 spin_unlock(&plli->lli_agl_lock);
914 wake_up(&thread->t_ctl_waitq);
917 l_wait_event(thread->t_ctl_waitq,
918 !agl_list_empty(sai) ||
919 !thread_is_running(thread),
922 if (!thread_is_running(thread))
925 spin_lock(&plli->lli_agl_lock);
926 /* The statahead thread maybe help to process AGL entries,
927 * so check whether list empty again. */
928 if (!agl_list_empty(sai)) {
929 clli = agl_first_entry(sai);
930 list_del_init(&clli->lli_agl_list);
931 spin_unlock(&plli->lli_agl_lock);
932 ll_agl_trigger(&clli->lli_vfs_inode, sai);
934 spin_unlock(&plli->lli_agl_lock);
938 spin_lock(&plli->lli_agl_lock);
939 sai->sai_agl_valid = 0;
940 while (!agl_list_empty(sai)) {
941 clli = agl_first_entry(sai);
942 list_del_init(&clli->lli_agl_list);
943 spin_unlock(&plli->lli_agl_lock);
944 clli->lli_agl_index = 0;
945 iput(&clli->lli_vfs_inode);
946 spin_lock(&plli->lli_agl_lock);
948 thread_set_flags(thread, SVC_STOPPED);
949 spin_unlock(&plli->lli_agl_lock);
950 wake_up(&thread->t_ctl_waitq);
952 CDEBUG(D_READA, "agl thread stopped: sai %p, parent %.*s\n",
953 sai, parent->d_name.len, parent->d_name.name);
957 /* start agl thread */
958 static void ll_start_agl(struct dentry *parent, struct ll_statahead_info *sai)
960 struct ptlrpc_thread *thread = &sai->sai_agl_thread;
961 struct l_wait_info lwi = { 0 };
962 struct ll_inode_info *plli;
963 struct task_struct *task;
966 CDEBUG(D_READA, "start agl thread: sai %p, parent %.*s\n",
967 sai, parent->d_name.len, parent->d_name.name);
969 plli = ll_i2info(parent->d_inode);
970 task = kthread_run(ll_agl_thread, parent,
971 "ll_agl_%u", plli->lli_opendir_pid);
973 CERROR("can't start ll_agl thread, rc: %ld\n", PTR_ERR(task));
974 thread_set_flags(thread, SVC_STOPPED);
978 l_wait_event(thread->t_ctl_waitq,
979 thread_is_running(thread) || thread_is_stopped(thread),
984 /* statahead thread main function */
985 static int ll_statahead_thread(void *arg)
987 struct dentry *parent = (struct dentry *)arg;
988 struct inode *dir = parent->d_inode;
989 struct ll_inode_info *lli = ll_i2info(dir);
990 struct ll_sb_info *sbi = ll_i2sbi(dir);
991 struct ll_statahead_info *sai;
992 struct ptlrpc_thread *sa_thread;
993 struct ptlrpc_thread *agl_thread;
995 struct md_op_data *op_data;
996 struct ll_dir_chain chain;
997 struct l_wait_info lwi = { 0 };
998 struct page *page = NULL;
1003 sai = ll_sai_get(dir);
1004 sa_thread = &sai->sai_thread;
1005 agl_thread = &sai->sai_agl_thread;
1006 sa_thread->t_pid = current_pid();
1007 CDEBUG(D_READA, "statahead thread starting: sai %p, parent %.*s\n",
1008 sai, parent->d_name.len, parent->d_name.name);
1010 op_data = ll_prep_md_op_data(NULL, dir, dir, NULL, 0, 0,
1011 LUSTRE_OPC_ANY, dir);
1012 if (IS_ERR(op_data))
1013 GOTO(out, rc = PTR_ERR(op_data));
1015 op_data->op_max_pages = ll_i2sbi(dir)->ll_md_brw_pages;
1017 if (sbi->ll_flags & LL_SBI_AGL_ENABLED)
1018 ll_start_agl(parent, sai);
1020 atomic_inc(&sbi->ll_sa_total);
1021 spin_lock(&lli->lli_sa_lock);
1022 if (thread_is_init(sa_thread))
1023 /* If someone else has changed the thread state
1024 * (e.g. already changed to SVC_STOPPING), we can't just
1025 * blindly overwrite that setting. */
1026 thread_set_flags(sa_thread, SVC_RUNNING);
1027 spin_unlock(&lli->lli_sa_lock);
1028 wake_up(&sa_thread->t_ctl_waitq);
1030 ll_dir_chain_init(&chain);
1031 while (pos != MDS_DIR_END_OFF && thread_is_running(sa_thread)) {
1032 struct lu_dirpage *dp;
1033 struct lu_dirent *ent;
1035 sai->sai_in_readpage = 1;
1036 page = ll_get_dir_page(dir, op_data, pos, &chain);
1037 sai->sai_in_readpage = 0;
1040 CDEBUG(D_READA, "error reading dir "DFID" at "LPU64
1041 "/"LPU64" opendir_pid = %u: rc = %d\n",
1042 PFID(ll_inode2fid(dir)), pos, sai->sai_index,
1043 lli->lli_opendir_pid, rc);
1047 dp = page_address(page);
1048 for (ent = lu_dirent_start(dp);
1049 ent != NULL && thread_is_running(sa_thread) &&
1051 ent = lu_dirent_next(ent)) {
1056 hash = le64_to_cpu(ent->lde_hash);
1057 if (unlikely(hash < pos))
1059 * Skip until we find target hash value.
1063 namelen = le16_to_cpu(ent->lde_namelen);
1064 if (unlikely(namelen == 0))
1066 * Skip dummy record.
1070 name = ent->lde_name;
1071 if (name[0] == '.') {
1077 } else if (name[1] == '.' && namelen == 2) {
1082 } else if (!sai->sai_ls_all) {
1084 * skip hidden files.
1086 sai->sai_skip_hidden++;
1092 * don't stat-ahead first entry.
1094 if (unlikely(++first == 1))
1097 /* wait for spare statahead window */
1099 l_wait_event(sa_thread->t_ctl_waitq,
1100 !sa_sent_full(sai) ||
1101 sa_has_callback(sai) ||
1102 !agl_list_empty(sai) ||
1103 !thread_is_running(sa_thread),
1106 sa_handle_callback(sai);
1108 spin_lock(&lli->lli_agl_lock);
1109 while (sa_sent_full(sai) &&
1110 !agl_list_empty(sai)) {
1111 struct ll_inode_info *clli;
1113 clli = agl_first_entry(sai);
1114 list_del_init(&clli->lli_agl_list);
1115 spin_unlock(&lli->lli_agl_lock);
1117 ll_agl_trigger(&clli->lli_vfs_inode,
1120 spin_lock(&lli->lli_agl_lock);
1122 spin_unlock(&lli->lli_agl_lock);
1123 } while (sa_sent_full(sai) &&
1124 thread_is_running(sa_thread));
1126 sa_statahead(parent, name, namelen);
1129 pos = le64_to_cpu(dp->ldp_hash_end);
1130 ll_release_page(dir, page,
1131 le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1133 if (sa_low_hit(sai)) {
1135 atomic_inc(&sbi->ll_sa_wrong);
1136 CDEBUG(D_READA, "Statahead for dir "DFID" hit "
1137 "ratio too low: hit/miss "LPU64"/"LPU64
1138 ", sent/replied "LPU64"/"LPU64", stopping "
1139 "statahead thread: pid %d\n",
1140 PFID(&lli->lli_fid), sai->sai_hit,
1141 sai->sai_miss, sai->sai_sent,
1142 sai->sai_replied, current_pid());
1146 ll_dir_chain_fini(&chain);
1147 ll_finish_md_op_data(op_data);
1150 spin_lock(&lli->lli_sa_lock);
1151 thread_set_flags(sa_thread, SVC_STOPPING);
1152 lli->lli_sa_enabled = 0;
1153 spin_unlock(&lli->lli_sa_lock);
1156 /* statahead is finished, but statahead entries need to be cached, wait
1157 * for file release to stop me. */
1158 while (thread_is_running(sa_thread)) {
1159 l_wait_event(sa_thread->t_ctl_waitq,
1160 sa_has_callback(sai) ||
1161 !thread_is_running(sa_thread),
1164 sa_handle_callback(sai);
1169 if (sai->sai_agl_valid) {
1170 spin_lock(&lli->lli_agl_lock);
1171 thread_set_flags(agl_thread, SVC_STOPPING);
1172 spin_unlock(&lli->lli_agl_lock);
1173 wake_up(&agl_thread->t_ctl_waitq);
1175 CDEBUG(D_READA, "stop agl thread: sai %p pid %u\n",
1176 sai, (unsigned int)agl_thread->t_pid);
1177 l_wait_event(agl_thread->t_ctl_waitq,
1178 thread_is_stopped(agl_thread),
1181 /* Set agl_thread flags anyway. */
1182 thread_set_flags(agl_thread, SVC_STOPPED);
1185 /* wait for inflight statahead RPCs to finish, and then we can free sai
1186 * safely because statahead RPC will access sai data */
1187 while (sai->sai_sent != sai->sai_replied) {
1188 /* in case we're not woken up, timeout wait */
1189 lwi = LWI_TIMEOUT(msecs_to_jiffies(MSEC_PER_SEC >> 3),
1191 l_wait_event(sa_thread->t_ctl_waitq,
1192 sai->sai_sent == sai->sai_replied, &lwi);
1195 /* release resources held by statahead RPCs */
1196 sa_handle_callback(sai);
1198 spin_lock(&lli->lli_sa_lock);
1199 thread_set_flags(sa_thread, SVC_STOPPED);
1200 spin_unlock(&lli->lli_sa_lock);
1202 CDEBUG(D_READA, "statahead thread stopped: sai %p, parent %.*s\n",
1203 sai, parent->d_name.len, parent->d_name.name);
1205 wake_up(&sai->sai_waitq);
1206 wake_up(&sa_thread->t_ctl_waitq);
1212 /* authorize opened dir handle @key to statahead */
1213 void ll_authorize_statahead(struct inode *dir, void *key)
1215 struct ll_inode_info *lli = ll_i2info(dir);
1217 spin_lock(&lli->lli_sa_lock);
1218 if (lli->lli_opendir_key == NULL && lli->lli_sai == NULL) {
1220 * if lli_sai is not NULL, it means previous statahead is not
1221 * finished yet, we'd better not start a new statahead for now.
1223 LASSERT(lli->lli_opendir_pid == 0);
1224 lli->lli_opendir_key = key;
1225 lli->lli_opendir_pid = current_pid();
1226 lli->lli_sa_enabled = 1;
1228 spin_unlock(&lli->lli_sa_lock);
1232 * deauthorize opened dir handle @key to statahead, and notify statahead thread
1233 * to quit if it's running.
1235 void ll_deauthorize_statahead(struct inode *dir, void *key)
1237 struct ll_inode_info *lli = ll_i2info(dir);
1238 struct ll_statahead_info *sai;
1240 LASSERT(lli->lli_opendir_key == key);
1241 LASSERT(lli->lli_opendir_pid != 0);
1243 CDEBUG(D_READA, "deauthorize statahead for "DFID"\n",
1244 PFID(&lli->lli_fid));
1246 spin_lock(&lli->lli_sa_lock);
1247 lli->lli_opendir_key = NULL;
1248 lli->lli_opendir_pid = 0;
1249 lli->lli_sa_enabled = 0;
1251 if (sai != NULL && thread_is_running(&sai->sai_thread)) {
1253 * statahead thread may not quit yet because it needs to cache
1254 * entries, now it's time to tell it to quit.
1256 thread_set_flags(&sai->sai_thread, SVC_STOPPING);
1257 wake_up(&sai->sai_thread.t_ctl_waitq);
1259 spin_unlock(&lli->lli_sa_lock);
1264 * not first dirent, or is "."
1266 LS_NOT_FIRST_DE = 0,
1268 * the first non-hidden dirent
1272 * the first hidden dirent, that is "."
1277 /* file is first dirent under @dir */
1278 static int is_first_dirent(struct inode *dir, struct dentry *dentry)
1280 struct ll_dir_chain chain;
1281 struct qstr *target = &dentry->d_name;
1282 struct md_op_data *op_data;
1284 struct page *page = NULL;
1285 int rc = LS_NOT_FIRST_DE;
1289 op_data = ll_prep_md_op_data(NULL, dir, dir, NULL, 0, 0,
1290 LUSTRE_OPC_ANY, dir);
1291 if (IS_ERR(op_data))
1292 RETURN(PTR_ERR(op_data));
1294 *FIXME choose the start offset of the readdir
1296 op_data->op_max_pages = ll_i2sbi(dir)->ll_md_brw_pages;
1298 ll_dir_chain_init(&chain);
1299 page = ll_get_dir_page(dir, op_data, 0, &chain);
1302 struct lu_dirpage *dp;
1303 struct lu_dirent *ent;
1306 struct ll_inode_info *lli = ll_i2info(dir);
1309 CERROR("%s: reading dir "DFID" at "LPU64
1310 "opendir_pid = %u : rc = %d\n",
1311 ll_get_fsname(dir->i_sb, NULL, 0),
1312 PFID(ll_inode2fid(dir)), pos,
1313 lli->lli_opendir_pid, rc);
1317 dp = page_address(page);
1318 for (ent = lu_dirent_start(dp); ent != NULL;
1319 ent = lu_dirent_next(ent)) {
1324 hash = le64_to_cpu(ent->lde_hash);
1325 /* The ll_get_dir_page() can return any page containing
1326 * the given hash which may be not the start hash. */
1327 if (unlikely(hash < pos))
1330 namelen = le16_to_cpu(ent->lde_namelen);
1331 if (unlikely(namelen == 0))
1333 * skip dummy record.
1337 name = ent->lde_name;
1338 if (name[0] == '.') {
1344 else if (name[1] == '.' && namelen == 2)
1355 if (dot_de && target->name[0] != '.') {
1356 CDEBUG(D_READA, "%.*s skip hidden file %.*s\n",
1357 target->len, target->name,
1362 if (target->len != namelen ||
1363 memcmp(target->name, name, namelen) != 0)
1364 rc = LS_NOT_FIRST_DE;
1368 rc = LS_FIRST_DOT_DE;
1370 ll_release_page(dir, page, false);
1373 pos = le64_to_cpu(dp->ldp_hash_end);
1374 if (pos == MDS_DIR_END_OFF) {
1376 * End of directory reached.
1378 ll_release_page(dir, page, false);
1382 * chain is exhausted
1383 * Normal case: continue to the next page.
1385 ll_release_page(dir, page, le32_to_cpu(dp->ldp_flags) &
1387 page = ll_get_dir_page(dir, op_data, pos, &chain);
1392 ll_dir_chain_fini(&chain);
1393 ll_finish_md_op_data(op_data);
1398 * revalidate @dentryp from statahead cache
1400 * \param[in] dir parent directory
1401 * \param[in] sai sai structure
1402 * \param[out] dentryp pointer to dentry which will be revalidated
1403 * \param[in] unplug unplug statahead window only (normally for negative
1405 * \retval 1 on success, dentry is saved in @dentryp
1406 * \retval 0 if revalidation failed (no proper lock on client)
1407 * \retval negative number upon error
1409 static int revalidate_statahead_dentry(struct inode *dir,
1410 struct ll_statahead_info *sai,
1411 struct dentry **dentryp,
1414 struct sa_entry *entry = NULL;
1415 struct l_wait_info lwi = { 0 };
1416 struct ll_dentry_data *ldd;
1417 struct ll_inode_info *lli;
1421 if ((*dentryp)->d_name.name[0] == '.') {
1422 if (sai->sai_ls_all ||
1423 sai->sai_miss_hidden >= sai->sai_skip_hidden) {
1425 * Hidden dentry is the first one, or statahead
1426 * thread does not skip so many hidden dentries
1427 * before "sai_ls_all" enabled as below.
1430 if (!sai->sai_ls_all)
1432 * It maybe because hidden dentry is not
1433 * the first one, "sai_ls_all" was not
1434 * set, then "ls -al" missed. Enable
1435 * "sai_ls_all" for such case.
1437 sai->sai_ls_all = 1;
1440 * Such "getattr" has been skipped before
1441 * "sai_ls_all" enabled as above.
1443 sai->sai_miss_hidden++;
1451 entry = sa_get(sai, &(*dentryp)->d_name);
1453 GOTO(out, rc = -EAGAIN);
1455 /* if statahead is busy in readdir, help it do post-work */
1456 if (!sa_ready(entry) && sai->sai_in_readpage)
1457 sa_handle_callback(sai);
1459 if (!sa_ready(entry)) {
1460 sai->sai_index_wait = entry->se_index;
1461 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(30), NULL,
1462 LWI_ON_SIGNAL_NOOP, NULL);
1463 rc = l_wait_event(sai->sai_waitq, sa_ready(entry), &lwi);
1466 * entry may not be ready, so it may be used by inflight
1467 * statahead RPC, don't free it.
1470 GOTO(out, rc = -EAGAIN);
1474 if (entry->se_state == SA_ENTRY_SUCC && entry->se_inode != NULL) {
1475 struct inode *inode = entry->se_inode;
1476 struct lookup_intent it = { .it_op = IT_GETATTR,
1477 .d.lustre.it_lock_handle =
1481 rc = md_revalidate_lock(ll_i2mdexp(dir), &it,
1482 ll_inode2fid(inode), &bits);
1484 if ((*dentryp)->d_inode == NULL) {
1485 struct dentry *alias;
1487 alias = ll_splice_alias(inode, *dentryp);
1489 GOTO(out, rc = PTR_ERR(alias));
1491 /* statahead prepared this inode, transfer inode
1492 * refcount from sa_entry to dentry */
1493 entry->se_inode = NULL;
1494 } else if ((*dentryp)->d_inode != inode) {
1495 /* revalidate, but inode is recreated */
1497 "%s: stale dentry %.*s inode "
1498 DFID", statahead inode "DFID
1500 ll_get_fsname((*dentryp)->d_inode->i_sb,
1502 (*dentryp)->d_name.len,
1503 (*dentryp)->d_name.name,
1504 PFID(ll_inode2fid((*dentryp)->d_inode)),
1505 PFID(ll_inode2fid(inode)));
1506 GOTO(out, rc = -ESTALE);
1509 if ((bits & MDS_INODELOCK_LOOKUP) &&
1510 d_lustre_invalid(*dentryp))
1511 d_lustre_revalidate(*dentryp);
1512 ll_intent_release(&it);
1517 * statahead cached sa_entry can be used only once, and will be killed
1518 * right after use, so if lookup/revalidate accessed statahead cache,
1519 * set dentry ldd_sa_generation to parent lli_sa_generation, later if we
1520 * stat this file again, we know we've done statahead before, see
1521 * dentry_may_statahead().
1523 ldd = ll_d2d(*dentryp);
1524 lli = ll_i2info(dir);
1525 /* ldd can be NULL if llite lookup failed. */
1527 ldd->lld_sa_generation = lli->lli_sa_generation;
1534 * start statahead thread
1536 * \param[in] dir parent directory
1537 * \param[in] dentry dentry that triggers statahead, normally the first
1539 * \retval -EAGAIN on success, because when this function is
1540 * called, it's already in lookup call, so client should
1541 * do it itself instead of waiting for statahead thread
1542 * to do it asynchronously.
1543 * \retval negative number upon error
1545 static int start_statahead_thread(struct inode *dir, struct dentry *dentry)
1547 struct ll_inode_info *lli = ll_i2info(dir);
1548 struct ll_statahead_info *sai = NULL;
1549 struct dentry *parent = dentry->d_parent;
1550 struct ptlrpc_thread *thread;
1551 struct l_wait_info lwi = { 0 };
1552 struct task_struct *task;
1556 /* I am the "lli_opendir_pid" owner, only me can set "lli_sai". */
1557 rc = is_first_dirent(dir, dentry);
1558 if (rc == LS_NOT_FIRST_DE)
1559 /* It is not "ls -{a}l" operation, no need statahead for it. */
1560 GOTO(out, rc = -EFAULT);
1562 sai = ll_sai_alloc(parent);
1564 GOTO(out, rc = -ENOMEM);
1566 sai->sai_ls_all = (rc == LS_FIRST_DOT_DE);
1568 /* if current lli_opendir_key was deauthorized, or dir re-opened by
1569 * another process, don't start statahead, otherwise the newly spawned
1570 * statahead thread won't be notified to quit. */
1571 spin_lock(&lli->lli_sa_lock);
1572 if (unlikely(lli->lli_sai != NULL ||
1573 lli->lli_opendir_key == NULL ||
1574 lli->lli_opendir_pid != current->pid)) {
1575 spin_unlock(&lli->lli_sa_lock);
1576 GOTO(out, rc = -EPERM);
1579 spin_unlock(&lli->lli_sa_lock);
1581 atomic_inc(&ll_i2sbi(parent->d_inode)->ll_sa_running);
1583 CDEBUG(D_READA, "start statahead thread: [pid %d] [parent %.*s]\n",
1584 current_pid(), parent->d_name.len, parent->d_name.name);
1586 task = kthread_run(ll_statahead_thread, parent, "ll_sa_%u",
1587 lli->lli_opendir_pid);
1588 thread = &sai->sai_thread;
1591 CERROR("can't start ll_sa thread, rc: %d\n", rc);
1595 l_wait_event(thread->t_ctl_waitq,
1596 thread_is_running(thread) || thread_is_stopped(thread),
1601 * We don't stat-ahead for the first dirent since we are already in
1607 /* once we start statahead thread failed, disable statahead so that
1608 * subsequent stat won't waste time to try it. */
1609 spin_lock(&lli->lli_sa_lock);
1610 lli->lli_sa_enabled = 0;
1611 lli->lli_sai = NULL;
1612 spin_unlock(&lli->lli_sa_lock);
1621 * statahead entry function, this is called when client getattr on a file, it
1622 * will start statahead thread if this is the first dir entry, else revalidate
1623 * dentry from statahead cache.
1625 * \param[in] dir parent directory
1626 * \param[out] dentryp dentry to getattr
1627 * \param[in] unplug unplug statahead window only (normally for negative
1629 * \retval 1 on success
1630 * \retval 0 revalidation from statahead cache failed, caller needs
1631 * to getattr from server directly
1632 * \retval negative number on error, caller often ignores this and
1633 * then getattr from server
1635 int ll_statahead(struct inode *dir, struct dentry **dentryp, bool unplug)
1637 struct ll_statahead_info *sai;
1639 sai = ll_sai_get(dir);
1643 rc = revalidate_statahead_dentry(dir, sai, dentryp, unplug);
1644 CDEBUG(D_READA, "revalidate statahead %.*s: %d.\n",
1645 (*dentryp)->d_name.len, (*dentryp)->d_name.name, rc);
1649 return start_statahead_thread(dir, *dentryp);