1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011 Whamcloud, Inc.
36 * This file is part of Lustre, http://www.lustre.org/
37 * Lustre is a trademark of Sun Microsystems, Inc.
41 #include <linux/sched.h>
43 #include <linux/smp_lock.h>
44 #include <linux/highmem.h>
45 #include <linux/pagemap.h>
47 #define DEBUG_SUBSYSTEM S_LLITE
49 #include <obd_support.h>
50 #include <lustre_lite.h>
51 #include <lustre_dlm.h>
52 #include <linux/lustre_version.h>
53 #include "llite_internal.h"
55 #define SA_OMITTED_ENTRY_MAX 8ULL
58 /** negative values are for error cases */
59 SA_ENTRY_INIT = 0, /** init entry */
60 SA_ENTRY_SUCC = 1, /** stat succeed */
61 SA_ENTRY_INVA = 2, /** invalid entry */
62 SA_ENTRY_DEST = 3, /** entry to be destroyed */
66 /* link into sai->sai_entries_{sent,received,stated} */
68 /* link into sai hash table locally */
70 /* entry reference count */
71 cfs_atomic_t se_refcount;
72 /* entry index in the sai */
74 /* low layer ldlm lock handle */
78 /* entry size, contains name */
80 /* pointer to async getattr enqueue info */
81 struct md_enqueue_info *se_minfo;
82 /* pointer to the async getattr request */
83 struct ptlrpc_request *se_req;
84 /* pointer to the target inode */
85 struct inode *se_inode;
90 static unsigned int sai_generation = 0;
91 static cfs_spinlock_t sai_generation_lock = CFS_SPIN_LOCK_UNLOCKED;
93 static inline int ll_sa_entry_unlinked(struct ll_sa_entry *entry)
95 return cfs_list_empty(&entry->se_list);
98 static inline int ll_sa_entry_unhashed(struct ll_sa_entry *entry)
100 return cfs_list_empty(&entry->se_hash);
104 * The entry only can be released by the caller, it is necessary to hold lock.
106 static inline int ll_sa_entry_stated(struct ll_sa_entry *entry)
109 return (entry->se_stat != SA_ENTRY_INIT);
112 static inline int ll_sa_entry_hash(int val)
114 return val & LL_SA_CACHE_MASK;
118 * Insert entry to hash SA table.
121 ll_sa_entry_enhash(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
123 int i = ll_sa_entry_hash(entry->se_qstr.hash);
125 cfs_spin_lock(&sai->sai_cache_lock[i]);
126 cfs_list_add_tail(&entry->se_hash, &sai->sai_cache[i]);
127 cfs_spin_unlock(&sai->sai_cache_lock[i]);
131 * Remove entry from SA table.
134 ll_sa_entry_unhash(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
136 int i = ll_sa_entry_hash(entry->se_qstr.hash);
138 cfs_spin_lock(&sai->sai_cache_lock[i]);
139 cfs_list_del_init(&entry->se_hash);
140 cfs_spin_unlock(&sai->sai_cache_lock[i]);
143 static inline int sa_received_empty(struct ll_statahead_info *sai)
145 return cfs_list_empty(&sai->sai_entries_received);
148 static inline int sa_not_full(struct ll_statahead_info *sai)
150 return (cfs_atomic_read(&sai->sai_cache_count) < sai->sai_max);
153 static inline int sa_is_running(struct ll_statahead_info *sai)
155 return !!(sai->sai_thread.t_flags & SVC_RUNNING);
158 static inline int sa_is_stopping(struct ll_statahead_info *sai)
160 return !!(sai->sai_thread.t_flags & SVC_STOPPING);
163 static inline int sa_is_stopped(struct ll_statahead_info *sai)
165 return !!(sai->sai_thread.t_flags & SVC_STOPPED);
169 * (1) hit ratio less than 80%
171 * (2) consecutive miss more than 8
172 * then means low hit.
174 static inline int sa_low_hit(struct ll_statahead_info *sai)
176 return ((sai->sai_hit > 7 && sai->sai_hit < 4 * sai->sai_miss) ||
177 (sai->sai_consecutive_miss > 8));
181 * If the given index is behind of statahead window more than
182 * SA_OMITTED_ENTRY_MAX, then it is old.
184 static inline int is_omitted_entry(struct ll_statahead_info *sai, __u64 index)
186 return ((__u64)sai->sai_max + index + SA_OMITTED_ENTRY_MAX <
191 * Insert it into sai_entries_sent tail when init.
193 static struct ll_sa_entry *
194 ll_sa_entry_alloc(struct ll_statahead_info *sai, __u64 index,
195 const char *name, int len)
197 struct ll_inode_info *lli;
198 struct ll_sa_entry *entry;
203 entry_size = sizeof(struct ll_sa_entry) + (len & ~3) + 4;
204 OBD_ALLOC(entry, entry_size);
205 if (unlikely(entry == NULL))
206 RETURN(ERR_PTR(-ENOMEM));
208 CDEBUG(D_READA, "alloc sai entry %.*s(%p) index "LPU64"\n",
209 len, name, entry, index);
211 entry->se_index = index;
214 * Statahead entry reference rules:
216 * 1) When statahead entry is initialized, its reference is set as 2.
217 * One reference is used by the directory scanner. When the scanner
218 * searches the statahead cache for the given name, it can perform
219 * lockless hash lookup (only the scanner can remove entry from hash
220 * list), and once found, it needn't to call "atomic_inc()" for the
221 * entry reference. So the performance is improved. After using the
222 * statahead entry, the scanner will call "atomic_dec()" to drop the
223 * reference held when initialization. If it is the last reference,
224 * the statahead entry will be freed.
226 * 2) All other threads, including statahead thread and ptlrpcd thread,
227 * when they process the statahead entry, the reference for target
228 * should be held to guarantee the entry will not be released by the
229 * directory scanner. After processing the entry, these threads will
230 * drop the entry reference. If it is the last reference, the entry
233 * The second reference when initializes the statahead entry is used
234 * by the statahead thread, following the rule 2).
236 cfs_atomic_set(&entry->se_refcount, 2);
237 entry->se_stat = SA_ENTRY_INIT;
238 entry->se_size = entry_size;
239 dname = (char *)entry + sizeof(struct ll_sa_entry);
240 memcpy(dname, name, len);
242 entry->se_qstr.hash = full_name_hash(name, len);
243 entry->se_qstr.len = len;
244 entry->se_qstr.name = dname;
246 lli = ll_i2info(sai->sai_inode);
247 cfs_spin_lock(&lli->lli_sa_lock);
248 cfs_list_add_tail(&entry->se_list, &sai->sai_entries_sent);
249 cfs_spin_unlock(&lli->lli_sa_lock);
251 cfs_atomic_inc(&sai->sai_cache_count);
252 ll_sa_entry_enhash(sai, entry);
258 * Used by the directory scanner to search entry with name.
260 * Only the caller can remove the entry from hash, so it is unnecessary to hold
261 * hash lock. It is caller's duty to release the init refcount on the entry, so
262 * it is also unnecessary to increase refcount on the entry.
264 static struct ll_sa_entry *
265 ll_sa_entry_get_byname(struct ll_statahead_info *sai, const struct qstr *qstr)
267 struct ll_sa_entry *entry;
268 int i = ll_sa_entry_hash(qstr->hash);
270 cfs_list_for_each_entry(entry, &sai->sai_cache[i], se_hash) {
271 if (entry->se_qstr.hash == qstr->hash &&
272 entry->se_qstr.len == qstr->len &&
273 memcmp(entry->se_qstr.name, qstr->name, qstr->len) == 0)
280 * Used by the async getattr request callback to find entry with index.
282 * Inside lli_sa_lock to prevent others to change the list during the search.
283 * It needs to increase entry refcount before returning to guarantee that the
284 * entry cannot be freed by others.
286 static struct ll_sa_entry *
287 ll_sa_entry_get_byindex(struct ll_statahead_info *sai, __u64 index)
289 struct ll_sa_entry *entry;
291 cfs_list_for_each_entry(entry, &sai->sai_entries_sent, se_list) {
292 if (entry->se_index == index) {
293 cfs_atomic_inc(&entry->se_refcount);
296 if (entry->se_index > index)
302 static void ll_sa_entry_cleanup(struct ll_statahead_info *sai,
303 struct ll_sa_entry *entry)
305 struct md_enqueue_info *minfo = entry->se_minfo;
306 struct ptlrpc_request *req = entry->se_req;
309 entry->se_minfo = NULL;
310 ll_intent_release(&minfo->mi_it);
316 entry->se_req = NULL;
317 ptlrpc_req_finished(req);
321 static void ll_sa_entry_put(struct ll_statahead_info *sai,
322 struct ll_sa_entry *entry)
324 if (cfs_atomic_dec_and_test(&entry->se_refcount)) {
325 CDEBUG(D_READA, "free sai entry %.*s(%p) index "LPU64"\n",
326 entry->se_qstr.len, entry->se_qstr.name, entry,
329 LASSERT(ll_sa_entry_unhashed(entry));
330 LASSERT(ll_sa_entry_unlinked(entry));
332 ll_sa_entry_cleanup(sai, entry);
334 iput(entry->se_inode);
336 OBD_FREE(entry, entry->se_size);
337 cfs_atomic_dec(&sai->sai_cache_count);
342 do_sai_entry_fini(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
344 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
346 ll_sa_entry_unhash(sai, entry);
348 cfs_spin_lock(&lli->lli_sa_lock);
349 entry->se_stat = SA_ENTRY_DEST;
350 if (likely(!ll_sa_entry_unlinked(entry)))
351 cfs_list_del_init(&entry->se_list);
352 cfs_spin_unlock(&lli->lli_sa_lock);
354 ll_sa_entry_put(sai, entry);
358 * Delete it from sai_entries_stated list when fini.
361 ll_sa_entry_fini(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
363 struct ll_sa_entry *pos, *next;
366 do_sai_entry_fini(sai, entry);
368 /* drop old entry from sent list */
369 cfs_list_for_each_entry_safe(pos, next, &sai->sai_entries_sent,
371 if (is_omitted_entry(sai, pos->se_index))
372 do_sai_entry_fini(sai, pos);
377 /* drop old entry from stated list */
378 cfs_list_for_each_entry_safe(pos, next, &sai->sai_entries_stated,
380 if (is_omitted_entry(sai, pos->se_index))
381 do_sai_entry_fini(sai, pos);
388 * Inside lli_sa_lock.
391 do_sai_entry_to_stated(struct ll_statahead_info *sai,
392 struct ll_sa_entry *entry, int rc)
394 struct ll_sa_entry *se;
395 cfs_list_t *pos = &sai->sai_entries_stated;
397 if (!ll_sa_entry_unlinked(entry))
398 cfs_list_del_init(&entry->se_list);
400 cfs_list_for_each_entry_reverse(se, &sai->sai_entries_stated, se_list) {
401 if (se->se_index < entry->se_index) {
407 cfs_list_add(&entry->se_list, pos);
412 * Move entry to sai_entries_stated and sort with the index.
413 * \retval 1 -- entry to be destroyed.
414 * \retval 0 -- entry is inserted into stated list.
417 ll_sa_entry_to_stated(struct ll_statahead_info *sai,
418 struct ll_sa_entry *entry, int rc)
420 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
423 ll_sa_entry_cleanup(sai, entry);
425 cfs_spin_lock(&lli->lli_sa_lock);
426 if (likely(entry->se_stat != SA_ENTRY_DEST)) {
427 do_sai_entry_to_stated(sai, entry, rc);
430 cfs_spin_unlock(&lli->lli_sa_lock);
435 static struct ll_statahead_info *ll_sai_alloc(void)
437 struct ll_statahead_info *sai;
445 cfs_atomic_set(&sai->sai_refcount, 1);
446 cfs_spin_lock(&sai_generation_lock);
447 sai->sai_generation = ++sai_generation;
448 if (unlikely(sai_generation == 0))
449 sai->sai_generation = ++sai_generation;
450 cfs_spin_unlock(&sai_generation_lock);
451 sai->sai_max = LL_SA_RPC_MIN;
452 cfs_waitq_init(&sai->sai_waitq);
453 cfs_waitq_init(&sai->sai_thread.t_ctl_waitq);
454 CFS_INIT_LIST_HEAD(&sai->sai_entries_sent);
455 CFS_INIT_LIST_HEAD(&sai->sai_entries_received);
456 CFS_INIT_LIST_HEAD(&sai->sai_entries_stated);
457 for (i = 0; i < LL_SA_CACHE_SIZE; i++) {
458 CFS_INIT_LIST_HEAD(&sai->sai_cache[i]);
459 cfs_spin_lock_init(&sai->sai_cache_lock[i]);
461 cfs_atomic_set(&sai->sai_cache_count, 0);
466 static inline struct ll_statahead_info *
467 ll_sai_get(struct ll_statahead_info *sai)
469 cfs_atomic_inc(&sai->sai_refcount);
473 static void ll_sai_put(struct ll_statahead_info *sai)
475 struct inode *inode = sai->sai_inode;
476 struct ll_inode_info *lli = ll_i2info(inode);
479 if (cfs_atomic_dec_and_lock(&sai->sai_refcount, &lli->lli_sa_lock)) {
480 struct ll_sa_entry *entry, *next;
482 if (unlikely(cfs_atomic_read(&sai->sai_refcount) > 0)) {
483 /* It is race case, the interpret callback just hold
484 * a reference count */
485 cfs_spin_unlock(&lli->lli_sa_lock);
489 LASSERT(lli->lli_opendir_key == NULL);
491 lli->lli_opendir_pid = 0;
492 cfs_spin_unlock(&lli->lli_sa_lock);
494 LASSERT(sa_is_stopped(sai));
496 if (sai->sai_sent > sai->sai_replied)
497 CDEBUG(D_READA,"statahead for dir "DFID" does not "
498 "finish: [sent:"LPU64"] [replied:"LPU64"]\n",
500 sai->sai_sent, sai->sai_replied);
502 cfs_list_for_each_entry_safe(entry, next,
503 &sai->sai_entries_sent, se_list)
504 do_sai_entry_fini(sai, entry);
506 LASSERT(sa_received_empty(sai));
508 cfs_list_for_each_entry_safe(entry, next,
509 &sai->sai_entries_stated, se_list)
510 do_sai_entry_fini(sai, entry);
512 LASSERT(cfs_atomic_read(&sai->sai_cache_count) == 0);
521 static void do_statahead_interpret(struct ll_statahead_info *sai,
522 struct ll_sa_entry *target)
524 struct inode *dir = sai->sai_inode;
526 struct ll_inode_info *lli = ll_i2info(dir);
527 struct ll_sa_entry *entry;
528 struct md_enqueue_info *minfo;
529 struct lookup_intent *it;
530 struct ptlrpc_request *req;
531 struct mdt_body *body;
535 cfs_spin_lock(&lli->lli_sa_lock);
536 if (target != NULL && target->se_req != NULL &&
537 !cfs_list_empty(&target->se_list)) {
539 } else if (unlikely(sa_received_empty(sai))) {
540 cfs_spin_unlock(&lli->lli_sa_lock);
543 entry = cfs_list_entry(sai->sai_entries_received.next,
544 struct ll_sa_entry, se_list);
547 cfs_atomic_inc(&entry->se_refcount);
548 cfs_list_del_init(&entry->se_list);
549 cfs_spin_unlock(&lli->lli_sa_lock);
551 LASSERT(entry->se_handle != 0);
553 minfo = entry->se_minfo;
556 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
558 GOTO(out, rc = -EFAULT);
560 child = entry->se_inode;
565 LASSERT(fid_is_zero(&minfo->mi_data.op_fid2));
567 /* XXX: No fid in reply, this is probaly cross-ref case.
568 * SA can't handle it yet. */
569 if (body->valid & OBD_MD_MDS)
570 GOTO(out, rc = -EAGAIN);
575 /* unlinked and re-created with the same name */
576 if (unlikely(!lu_fid_eq(&minfo->mi_data.op_fid2, &body->fid1))){
577 entry->se_inode = NULL;
583 it->d.lustre.it_lock_handle = entry->se_handle;
584 rc = md_revalidate_lock(ll_i2mdexp(dir), it, NULL, NULL);
586 GOTO(out, rc = -EAGAIN);
588 rc = ll_prep_inode(&child, req, dir->i_sb);
592 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
593 child, child->i_ino, child->i_generation);
594 ll_set_lock_data(ll_i2sbi(dir)->ll_md_exp, child, it, NULL);
596 entry->se_inode = child;
601 /* The "ll_sa_entry_to_stated()" will drop related ldlm ibits lock
602 * reference count by calling "ll_intent_drop_lock()" in spite of the
603 * above operations failed or not. Do not worry about calling
604 * "ll_intent_drop_lock()" more than once. */
605 rc = ll_sa_entry_to_stated(sai, entry, rc < 0 ? rc : SA_ENTRY_SUCC);
606 if (rc == 0 && entry->se_index == sai->sai_index_wait && target == NULL)
607 cfs_waitq_signal(&sai->sai_waitq);
608 ll_sa_entry_put(sai, entry);
611 static int ll_statahead_interpret(struct ptlrpc_request *req,
612 struct md_enqueue_info *minfo, int rc)
614 struct lookup_intent *it = &minfo->mi_it;
615 struct inode *dir = minfo->mi_dir;
616 struct ll_inode_info *lli = ll_i2info(dir);
617 struct ll_statahead_info *sai = NULL;
618 struct ll_sa_entry *entry;
622 if (it_disposition(it, DISP_LOOKUP_NEG))
625 cfs_spin_lock(&lli->lli_sa_lock);
627 if (unlikely(lli->lli_sai == NULL ||
628 lli->lli_sai->sai_generation != minfo->mi_generation)) {
629 cfs_spin_unlock(&lli->lli_sa_lock);
630 GOTO(out, rc = -ESTALE);
632 sai = ll_sai_get(lli->lli_sai);
633 if (unlikely(!sa_is_running(sai))) {
635 cfs_spin_unlock(&lli->lli_sa_lock);
636 GOTO(out, rc = -EBADFD);
639 entry = ll_sa_entry_get_byindex(sai, minfo->mi_cbdata);
642 cfs_spin_unlock(&lli->lli_sa_lock);
643 GOTO(out, rc = -EIDRM);
646 cfs_list_del_init(&entry->se_list);
649 do_sai_entry_to_stated(sai, entry, rc);
650 cfs_spin_unlock(&lli->lli_sa_lock);
651 if (entry->se_index == sai->sai_index_wait)
652 cfs_waitq_signal(&sai->sai_waitq);
654 entry->se_minfo = minfo;
655 entry->se_req = ptlrpc_request_addref(req);
656 /* Release the async ibits lock ASAP to avoid deadlock
657 * when statahead thread tries to enqueue lock on parent
658 * for readpage and other tries to enqueue lock on child
659 * with parent's lock held, for example: unlink. */
660 entry->se_handle = it->d.lustre.it_lock_handle;
661 ll_intent_drop_lock(it);
662 wakeup = sa_received_empty(sai);
663 cfs_list_add_tail(&entry->se_list,
664 &sai->sai_entries_received);
666 cfs_spin_unlock(&lli->lli_sa_lock);
668 cfs_waitq_signal(&sai->sai_thread.t_ctl_waitq);
670 ll_sa_entry_put(sai, entry);
677 ll_intent_release(it);
686 static void sa_args_fini(struct md_enqueue_info *minfo,
687 struct ldlm_enqueue_info *einfo)
689 LASSERT(minfo && einfo);
691 capa_put(minfo->mi_data.op_capa1);
692 capa_put(minfo->mi_data.op_capa2);
698 * There is race condition between "capa_put" and "ll_statahead_interpret" for
699 * accessing "op_data.op_capa[1,2]" as following:
700 * "capa_put" releases "op_data.op_capa[1,2]"'s reference count after calling
701 * "md_intent_getattr_async". But "ll_statahead_interpret" maybe run first, and
702 * fill "op_data.op_capa[1,2]" as POISON, then cause "capa_put" access invalid
703 * "ocapa". So here reserve "op_data.op_capa[1,2]" in "pcapa" before calling
704 * "md_intent_getattr_async".
706 static int sa_args_init(struct inode *dir, struct inode *child,
707 struct qstr *qstr, struct md_enqueue_info **pmi,
708 struct ldlm_enqueue_info **pei,
709 struct obd_capa **pcapa)
711 struct ll_inode_info *lli = ll_i2info(dir);
712 struct md_enqueue_info *minfo;
713 struct ldlm_enqueue_info *einfo;
714 struct md_op_data *op_data;
716 OBD_ALLOC_PTR(einfo);
720 OBD_ALLOC_PTR(minfo);
726 op_data = ll_prep_md_op_data(&minfo->mi_data, dir, child, qstr->name,
727 qstr->len, 0, LUSTRE_OPC_ANY, NULL);
728 if (IS_ERR(op_data)) {
731 return PTR_ERR(op_data);
734 minfo->mi_it.it_op = IT_GETATTR;
735 minfo->mi_dir = igrab(dir);
736 minfo->mi_cb = ll_statahead_interpret;
737 minfo->mi_generation = lli->lli_sai->sai_generation;
738 minfo->mi_cbdata = lli->lli_sai->sai_index;
740 einfo->ei_type = LDLM_IBITS;
741 einfo->ei_mode = it_to_lock_mode(&minfo->mi_it);
742 einfo->ei_cb_bl = ll_md_blocking_ast;
743 einfo->ei_cb_cp = ldlm_completion_ast;
744 einfo->ei_cb_gl = NULL;
745 einfo->ei_cbdata = NULL;
749 pcapa[0] = op_data->op_capa1;
750 pcapa[1] = op_data->op_capa2;
755 static int do_sa_lookup(struct inode *dir, struct ll_sa_entry *entry)
757 struct md_enqueue_info *minfo;
758 struct ldlm_enqueue_info *einfo;
759 struct obd_capa *capas[2];
763 rc = sa_args_init(dir, NULL, &entry->se_qstr, &minfo, &einfo, capas);
767 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
772 sa_args_fini(minfo, einfo);
779 * similar to ll_revalidate_it().
780 * \retval 1 -- dentry valid
781 * \retval 0 -- will send stat-ahead request
782 * \retval others -- prepare stat-ahead request failed
784 static int do_sa_revalidate(struct inode *dir, struct ll_sa_entry *entry,
785 struct dentry *dentry)
787 struct inode *inode = dentry->d_inode;
788 struct lookup_intent it = { .it_op = IT_GETATTR,
789 .d.lustre.it_lock_handle = 0 };
790 struct md_enqueue_info *minfo;
791 struct ldlm_enqueue_info *einfo;
792 struct obd_capa *capas[2];
796 if (unlikely(inode == NULL))
799 if (d_mountpoint(dentry))
802 if (unlikely(dentry == dentry->d_sb->s_root))
805 entry->se_inode = igrab(inode);
806 rc = md_revalidate_lock(ll_i2mdexp(dir), &it, ll_inode2fid(inode),NULL);
808 entry->se_handle = it.d.lustre.it_lock_handle;
809 ll_intent_release(&it);
813 rc = sa_args_init(dir, inode, &entry->se_qstr, &minfo, &einfo, capas);
815 entry->se_inode = NULL;
820 rc = md_intent_getattr_async(ll_i2mdexp(dir), minfo, einfo);
825 entry->se_inode = NULL;
827 sa_args_fini(minfo, einfo);
833 static void ll_statahead_one(struct dentry *parent, const char* entry_name,
836 struct inode *dir = parent->d_inode;
837 struct ll_inode_info *lli = ll_i2info(dir);
838 struct ll_statahead_info *sai = lli->lli_sai;
839 struct dentry *dentry = NULL;
840 struct ll_sa_entry *entry;
845 entry = ll_sa_entry_alloc(sai, sai->sai_index, entry_name,
850 dentry = d_lookup(parent, &entry->se_qstr);
852 rc = do_sa_lookup(dir, entry);
854 rc = do_sa_revalidate(dir, entry, dentry);
860 rc1 = ll_sa_entry_to_stated(sai, entry,
861 rc < 0 ? SA_ENTRY_INVA : SA_ENTRY_SUCC);
862 if (rc1 == 0 && entry->se_index == sai->sai_index_wait)
863 cfs_waitq_signal(&sai->sai_waitq);
869 /* drop one refcount on entry by ll_sa_entry_alloc */
870 ll_sa_entry_put(sai, entry);
875 static int ll_statahead_thread(void *arg)
877 struct dentry *parent = (struct dentry *)arg;
878 struct inode *dir = parent->d_inode;
879 struct ll_inode_info *lli = ll_i2info(dir);
880 struct ll_sb_info *sbi = ll_i2sbi(dir);
881 struct ll_statahead_info *sai = ll_sai_get(lli->lli_sai);
882 struct ptlrpc_thread *thread = &sai->sai_thread;
887 struct ll_dir_chain chain;
892 snprintf(pname, 15, "ll_sa_%u", lli->lli_opendir_pid);
893 cfs_daemonize(pname);
896 atomic_inc(&sbi->ll_sa_total);
897 cfs_spin_lock(&lli->lli_sa_lock);
898 thread->t_flags = SVC_RUNNING;
899 cfs_spin_unlock(&lli->lli_sa_lock);
900 cfs_waitq_signal(&thread->t_ctl_waitq);
901 CDEBUG(D_READA, "start doing statahead for %s\n", parent->d_name.name);
904 ll_dir_chain_init(&chain);
905 page = ll_get_dir_page(NULL, dir, pos, &chain);
908 struct l_wait_info lwi = { 0 };
909 struct lu_dirpage *dp;
910 struct lu_dirent *ent;
914 CDEBUG(D_READA, "error reading dir "DFID" at "LPU64
915 "/"LPU64": [rc %d] [parent %u]\n",
916 PFID(ll_inode2fid(dir)), pos, sai->sai_index,
917 rc, lli->lli_opendir_pid);
921 dp = page_address(page);
922 for (ent = lu_dirent_start(dp); ent != NULL;
923 ent = lu_dirent_next(ent)) {
928 hash = le64_to_cpu(ent->lde_hash);
929 if (unlikely(hash < pos))
931 * Skip until we find target hash value.
935 namelen = le16_to_cpu(ent->lde_namelen);
936 if (unlikely(namelen == 0))
942 name = ent->lde_name;
943 if (name[0] == '.') {
949 } else if (name[1] == '.' && namelen == 2) {
954 } else if (!sai->sai_ls_all) {
958 sai->sai_skip_hidden++;
964 * don't stat-ahead first entry.
966 if (unlikely(++first == 1))
970 l_wait_event(thread->t_ctl_waitq,
972 !sa_received_empty(sai) ||
976 while (!sa_received_empty(sai))
977 do_statahead_interpret(sai, NULL);
979 if (unlikely(!sa_is_running(sai))) {
980 ll_release_page(page, 0);
984 if (!sa_not_full(sai))
986 * do not skip the current de.
990 ll_statahead_one(parent, name, namelen);
992 pos = le64_to_cpu(dp->ldp_hash_end);
993 if (pos == MDS_DIR_END_OFF) {
995 * End of directory reached.
997 ll_release_page(page, 0);
999 l_wait_event(thread->t_ctl_waitq,
1000 !sa_received_empty(sai) ||
1001 sai->sai_sent == sai->sai_replied||
1002 !sa_is_running(sai),
1005 while (!sa_received_empty(sai))
1006 do_statahead_interpret(sai, NULL);
1008 if ((sai->sai_sent == sai->sai_replied &&
1009 sa_received_empty(sai)) ||
1010 !sa_is_running(sai))
1015 * chain is exhausted.
1016 * Normal case: continue to the next page.
1018 ll_release_page(page, le32_to_cpu(dp->ldp_flags) &
1020 lli->lli_sa_pos = pos;
1021 sai->sai_in_readpage = 1;
1022 page = ll_get_dir_page(NULL, dir, pos, &chain);
1023 sai->sai_in_readpage = 0;
1025 LASSERT(le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1026 ll_release_page(page, 1);
1028 * go into overflow page.
1035 ll_dir_chain_fini(&chain);
1036 cfs_spin_lock(&lli->lli_sa_lock);
1037 if (!sa_received_empty(sai)) {
1038 thread->t_flags = SVC_STOPPING;
1039 cfs_spin_unlock(&lli->lli_sa_lock);
1041 /* To release the resources held by received entries. */
1042 while (!sa_received_empty(sai))
1043 do_statahead_interpret(sai, NULL);
1045 cfs_spin_lock(&lli->lli_sa_lock);
1047 thread->t_flags = SVC_STOPPED;
1048 cfs_spin_unlock(&lli->lli_sa_lock);
1049 cfs_waitq_signal(&sai->sai_waitq);
1050 cfs_waitq_signal(&thread->t_ctl_waitq);
1053 CDEBUG(D_READA, "statahead thread stopped, pid %d\n",
1059 * called in ll_file_release().
1061 void ll_stop_statahead(struct inode *dir, void *key)
1063 struct ll_inode_info *lli = ll_i2info(dir);
1065 if (unlikely(key == NULL))
1068 cfs_spin_lock(&lli->lli_sa_lock);
1069 if (lli->lli_opendir_key != key || lli->lli_opendir_pid == 0) {
1070 cfs_spin_unlock(&lli->lli_sa_lock);
1074 lli->lli_opendir_key = NULL;
1077 struct l_wait_info lwi = { 0 };
1078 struct ptlrpc_thread *thread = &lli->lli_sai->sai_thread;
1080 if (!sa_is_stopped(lli->lli_sai)) {
1081 thread->t_flags = SVC_STOPPING;
1082 cfs_spin_unlock(&lli->lli_sa_lock);
1083 cfs_waitq_signal(&thread->t_ctl_waitq);
1085 CDEBUG(D_READA, "stopping statahead thread, pid %d\n",
1087 l_wait_event(thread->t_ctl_waitq,
1088 sa_is_stopped(lli->lli_sai),
1091 cfs_spin_unlock(&lli->lli_sa_lock);
1095 * Put the ref which was held when first statahead_enter.
1096 * It maybe not the last ref for some statahead requests
1099 ll_sai_put(lli->lli_sai);
1101 lli->lli_opendir_pid = 0;
1102 cfs_spin_unlock(&lli->lli_sa_lock);
1108 * not first dirent, or is "."
1110 LS_NONE_FIRST_DE = 0,
1112 * the first non-hidden dirent
1116 * the first hidden dirent, that is "."
1121 static int is_first_dirent(struct inode *dir, struct dentry *dentry)
1123 struct ll_inode_info *lli = ll_i2info(dir);
1124 struct ll_dir_chain chain;
1125 struct qstr *target = &dentry->d_name;
1129 int rc = LS_NONE_FIRST_DE;
1132 lli->lli_sa_pos = 0;
1133 ll_dir_chain_init(&chain);
1134 page = ll_get_dir_page(NULL, dir, pos, &chain);
1137 struct lu_dirpage *dp;
1138 struct lu_dirent *ent;
1141 struct ll_inode_info *lli = ll_i2info(dir);
1144 CERROR("error reading dir "DFID" at "LPU64": "
1145 "[rc %d] [parent %u]\n",
1146 PFID(ll_inode2fid(dir)), pos,
1147 rc, lli->lli_opendir_pid);
1151 dp = page_address(page);
1152 for (ent = lu_dirent_start(dp); ent != NULL;
1153 ent = lu_dirent_next(ent)) {
1158 hash = le64_to_cpu(ent->lde_hash);
1159 /* The ll_get_dir_page() can return any page containing
1160 * the given hash which may be not the start hash. */
1161 if (unlikely(hash < pos))
1164 namelen = le16_to_cpu(ent->lde_namelen);
1165 if (unlikely(namelen == 0))
1167 * skip dummy record.
1171 name = ent->lde_name;
1172 if (name[0] == '.') {
1178 else if (name[1] == '.' && namelen == 2)
1189 if (dot_de && target->name[0] != '.') {
1190 CDEBUG(D_READA, "%.*s skip hidden file %.*s\n",
1191 target->len, target->name,
1196 if (target->len != namelen ||
1197 memcmp(target->name, name, namelen) != 0)
1198 rc = LS_NONE_FIRST_DE;
1202 rc = LS_FIRST_DOT_DE;
1204 ll_release_page(page, 0);
1207 pos = le64_to_cpu(dp->ldp_hash_end);
1208 if (pos == MDS_DIR_END_OFF) {
1210 * End of directory reached.
1212 ll_release_page(page, 0);
1216 * chain is exhausted
1217 * Normal case: continue to the next page.
1219 ll_release_page(page, le32_to_cpu(dp->ldp_flags) &
1221 lli->lli_sa_pos = pos;
1222 page = ll_get_dir_page(NULL, dir, pos, &chain);
1225 * go into overflow page.
1227 LASSERT(le32_to_cpu(dp->ldp_flags) & LDF_COLLIDE);
1228 ll_release_page(page, 1);
1234 ll_dir_chain_fini(&chain);
1239 ll_sai_unplug(struct ll_statahead_info *sai, struct ll_sa_entry *entry)
1241 struct ll_sb_info *sbi = ll_i2sbi(sai->sai_inode);
1245 if (entry != NULL && entry->se_stat == SA_ENTRY_SUCC)
1250 ll_sa_entry_fini(sai, entry);
1253 sai->sai_consecutive_miss = 0;
1254 sai->sai_max = min(2 * sai->sai_max, sbi->ll_sa_max);
1256 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
1259 sai->sai_consecutive_miss++;
1260 if (sa_low_hit(sai) && sa_is_running(sai)) {
1261 atomic_inc(&sbi->ll_sa_wrong);
1262 CDEBUG(D_READA, "Statahead for dir "DFID" hit "
1263 "ratio too low: hit/miss "LPU64"/"LPU64
1264 ", sent/replied "LPU64"/"LPU64", stopping "
1265 "statahead thread: pid %d\n",
1266 PFID(&lli->lli_fid), sai->sai_hit,
1267 sai->sai_miss, sai->sai_sent,
1268 sai->sai_replied, cfs_curproc_pid());
1269 cfs_spin_lock(&lli->lli_sa_lock);
1270 if (!sa_is_stopped(sai))
1271 sai->sai_thread.t_flags = SVC_STOPPING;
1272 cfs_spin_unlock(&lli->lli_sa_lock);
1276 if (!sa_is_stopped(sai))
1277 cfs_waitq_signal(&sai->sai_thread.t_ctl_waitq);
1283 * Start statahead thread if this is the first dir entry.
1284 * Otherwise if a thread is started already, wait it until it is ahead of me.
1285 * \retval 1 -- find entry with lock in cache, the caller needs to do
1287 * \retval 0 -- find entry in cache, but without lock, the caller needs
1289 * \retval others -- the caller need to process as non-statahead.
1291 int do_statahead_enter(struct inode *dir, struct dentry **dentryp,
1294 struct ll_inode_info *lli = ll_i2info(dir);
1295 struct ll_statahead_info *sai = lli->lli_sai;
1296 struct dentry *parent;
1297 struct ll_sa_entry *entry;
1298 struct l_wait_info lwi = { 0 };
1302 LASSERT(lli->lli_opendir_pid == cfs_curproc_pid());
1305 if (unlikely(sa_is_stopped(sai) &&
1306 cfs_list_empty(&sai->sai_entries_stated))) {
1307 /* to release resource */
1308 ll_stop_statahead(dir, lli->lli_opendir_key);
1312 if ((*dentryp)->d_name.name[0] == '.') {
1313 if (sai->sai_ls_all ||
1314 sai->sai_miss_hidden >= sai->sai_skip_hidden) {
1316 * Hidden dentry is the first one, or statahead
1317 * thread does not skip so many hidden dentries
1318 * before "sai_ls_all" enabled as below.
1321 if (!sai->sai_ls_all)
1323 * It maybe because hidden dentry is not
1324 * the first one, "sai_ls_all" was not
1325 * set, then "ls -al" missed. Enable
1326 * "sai_ls_all" for such case.
1328 sai->sai_ls_all = 1;
1331 * Such "getattr" has been skipped before
1332 * "sai_ls_all" enabled as above.
1334 sai->sai_miss_hidden++;
1339 entry = ll_sa_entry_get_byname(sai, &(*dentryp)->d_name);
1340 if (entry == NULL || only_unplug) {
1341 ll_sai_unplug(sai, entry);
1342 RETURN(entry ? 1 : -EAGAIN);
1345 while (!ll_sa_entry_stated(entry) &&
1346 sai->sai_in_readpage &&
1347 !sa_received_empty(sai))
1348 do_statahead_interpret(sai, entry);
1350 if (!ll_sa_entry_stated(entry)) {
1351 sai->sai_index_wait = entry->se_index;
1352 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(30), NULL,
1353 LWI_ON_SIGNAL_NOOP, NULL);
1354 rc = l_wait_event(sai->sai_waitq,
1355 ll_sa_entry_stated(entry) ||
1359 ll_sai_unplug(sai, entry);
1364 if (entry->se_stat == SA_ENTRY_SUCC &&
1365 entry->se_inode != NULL) {
1366 struct inode *inode = entry->se_inode;
1367 struct lookup_intent it = { .it_op = IT_GETATTR,
1368 .d.lustre.it_lock_handle =
1370 struct ll_dentry_data *lld;
1373 rc = md_revalidate_lock(ll_i2mdexp(dir), &it,
1374 ll_inode2fid(inode), &bits);
1376 if ((*dentryp)->d_inode == NULL) {
1377 *dentryp = ll_find_alias(inode,
1379 lld = ll_d2d(*dentryp);
1380 if (unlikely(lld == NULL))
1381 ll_dops_init(*dentryp, 1, 1);
1383 LASSERT((*dentryp)->d_inode == inode);
1385 ll_dentry_rehash(*dentryp, 0);
1388 entry->se_inode = NULL;
1390 ll_dentry_reset_flags(*dentryp, bits);
1391 ll_intent_release(&it);
1395 ll_sai_unplug(sai, entry);
1399 /* I am the "lli_opendir_pid" owner, only me can set "lli_sai". */
1400 rc = is_first_dirent(dir, *dentryp);
1401 if (rc == LS_NONE_FIRST_DE)
1402 /* It is not "ls -{a}l" operation, no need statahead for it. */
1403 GOTO(out, rc = -EAGAIN);
1405 sai = ll_sai_alloc();
1407 GOTO(out, rc = -ENOMEM);
1409 sai->sai_ls_all = (rc == LS_FIRST_DOT_DE);
1410 sai->sai_inode = igrab(dir);
1411 if (unlikely(sai->sai_inode == NULL)) {
1412 CWARN("Do not start stat ahead on dying inode "DFID"\n",
1413 PFID(&lli->lli_fid));
1414 GOTO(out, rc = -ESTALE);
1417 /* get parent reference count here, and put it in ll_statahead_thread */
1418 parent = dget((*dentryp)->d_parent);
1419 if (unlikely(sai->sai_inode != parent->d_inode)) {
1420 struct ll_inode_info *nlli = ll_i2info(parent->d_inode);
1422 CWARN("Race condition, someone changed %.*s just now: "
1423 "old parent "DFID", new parent "DFID"\n",
1424 (*dentryp)->d_name.len, (*dentryp)->d_name.name,
1425 PFID(&lli->lli_fid), PFID(&nlli->lli_fid));
1427 iput(sai->sai_inode);
1428 GOTO(out, rc = -EAGAIN);
1432 rc = cfs_create_thread(ll_statahead_thread, parent, 0);
1434 CERROR("can't start ll_sa thread, rc: %d\n", rc);
1436 lli->lli_opendir_key = NULL;
1437 sai->sai_thread.t_flags = SVC_STOPPED;
1439 LASSERT(lli->lli_sai == NULL);
1443 l_wait_event(sai->sai_thread.t_ctl_waitq,
1444 sa_is_running(sai) || sa_is_stopped(sai),
1448 * We don't stat-ahead for the first dirent since we are already in
1449 * lookup, and -EEXIST also indicates that this is the first dirent.
1456 cfs_spin_lock(&lli->lli_sa_lock);
1457 lli->lli_opendir_key = NULL;
1458 lli->lli_opendir_pid = 0;
1459 cfs_spin_unlock(&lli->lli_sa_lock);