1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2007 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/sched.h>
25 #include <linux/smp_lock.h>
26 #include <linux/highmem.h>
27 #include <linux/pagemap.h>
29 #define DEBUG_SUBSYSTEM S_LLITE
31 #include <obd_support.h>
32 #include <lustre_lite.h>
33 #include <lustre_dlm.h>
34 #include <linux/lustre_version.h>
35 #include "llite_internal.h"
38 struct list_head se_list;
44 SA_ENTRY_UNSTATED = 0,
48 static struct ll_statahead_info *ll_sai_alloc(void)
50 struct ll_statahead_info *sai;
56 sai->sai_max = LL_STATAHEAD_MIN;
57 cfs_waitq_init(&sai->sai_thread.t_ctl_waitq);
58 CFS_INIT_LIST_HEAD(&sai->sai_entries);
59 atomic_set(&sai->sai_refc, 1);
64 struct ll_statahead_info *ll_sai_get(struct ll_statahead_info *sai)
67 atomic_inc(&sai->sai_refc);
71 static void ll_sai_put(struct ll_statahead_info *sai)
73 struct inode *inode = sai->sai_inode;
74 struct ll_inode_info *lli = ll_i2info(inode);
77 if (atomic_dec_and_lock(&sai->sai_refc, &lli->lli_lock)) {
78 struct ll_sai_entry *entry, *next;
80 list_for_each_entry_safe(entry, next, &sai->sai_entries,
82 list_del(&entry->se_list);
87 spin_unlock(&lli->lli_lock);
93 static struct ll_sai_entry *ll_sai_entry_get(struct ll_statahead_info *sai,
96 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
97 struct ll_sb_info *sbi = ll_i2sbi(sai->sai_inode);
98 struct ll_sai_entry *entry;
101 OBD_ALLOC_PTR(entry);
105 CDEBUG(D_READA, "alloc sai entry %p index %d, stat %d\n",
107 entry->se_index = index;
108 entry->se_stat = stat;
110 spin_lock(&lli->lli_lock);
111 list_add_tail(&entry->se_list, &sai->sai_entries);
112 sai->sai_entries_nr++;
113 sbi->ll_sa_count = sai->sai_entries_nr;
114 spin_unlock(&lli->lli_lock);
116 LASSERT(sai->sai_entries_nr <= sbi->ll_sa_max);
120 /* inside lli_lock */
121 static void ll_sai_entry_set(struct ll_statahead_info *sai, int index,
124 struct ll_sai_entry *entry;
127 list_for_each_entry(entry, &sai->sai_entries, se_list) {
128 if (entry->se_index == index) {
129 LASSERT(entry->se_stat == SA_ENTRY_UNSTATED);
130 entry->se_stat = stat;
131 CDEBUG(D_READA, "set sai entry %p index %d stat %d\n",
137 /* Sometimes, this happens when entry has been put and freed */
138 CDEBUG(D_READA, "can't find sai entry index %d\n", index);
142 /* check first entry was stated already */
143 static int ll_sai_entry_stated(struct ll_statahead_info *sai)
145 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
146 struct ll_sai_entry *entry;
150 spin_lock(&lli->lli_lock);
151 if (!list_empty(&sai->sai_entries)) {
152 entry = list_entry(sai->sai_entries.next, struct ll_sai_entry,
154 CDEBUG(D_READA, "check sai entry %p index %d stat %d\n",
155 entry, entry->se_index, entry->se_stat);
156 rc = (entry->se_stat != SA_ENTRY_UNSTATED);
158 spin_unlock(&lli->lli_lock);
163 /* inside lli_lock */
164 static void ll_sai_entry_put(struct ll_statahead_info *sai)
166 struct ll_sai_entry *entry;
169 if (list_empty(&sai->sai_entries)) {
173 LASSERT(sai->sai_entries_nr > 0);
175 entry = list_entry(sai->sai_entries.next, struct ll_sai_entry, se_list);
176 list_del(&entry->se_list);
177 sai->sai_entries_nr--;
179 CDEBUG(D_READA, "free sa entry %p index %d stat %d\n",
180 entry, entry->se_index, entry->se_stat);
185 /* finish lookup/revalidate */
186 static int ll_statahead_interpret(struct obd_export *exp,
187 struct ptlrpc_request *req,
188 struct md_enqueue_info *minfo,
191 struct lookup_intent *it = &minfo->mi_it;
192 struct dentry *dentry = minfo->mi_dentry;
193 struct inode *dir = dentry->d_parent->d_inode;
194 struct ll_inode_info *lli = ll_i2info(dir);
195 struct ll_statahead_info *sai;
198 CDEBUG(D_READA, "interpret statahead %.*s rc %d\n",
199 dentry->d_name.len, dentry->d_name.name, rc);
200 if (rc || dir == NULL)
203 if (dentry->d_inode == NULL) {
205 struct dentry *save = dentry;
206 struct it_cb_data icbd = {
208 .icbd_childp = &dentry
211 rc = lookup_it_finish(req, DLM_REPLY_REC_OFF, it, &icbd);
214 * Here dentry->d_inode might be NULL,
215 * because the entry may have been removed before
216 * we start doing stat ahead.
220 ll_lookup_finish_locks(it, dentry);
224 struct mds_body *body;
226 body = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF,
228 if (memcmp(&minfo->mi_data.fid2, &body->fid1,
229 sizeof(body->fid1))) {
230 ll_unhash_aliases(dentry->d_inode);
231 GOTO(out, rc = -EAGAIN);
234 rc = revalidate_it_finish(req, DLM_REPLY_REC_OFF, it, dentry);
236 ll_unhash_aliases(dentry->d_inode);
240 spin_lock(&dcache_lock);
243 #ifdef DCACHE_LUSTRE_INVALID
244 dentry->d_flags &= ~DCACHE_LUSTRE_INVALID;
246 unlock_dentry(dentry);
247 d_rehash_cond(dentry, 0);
248 spin_unlock(&dcache_lock);
250 ll_lookup_finish_locks(it, dentry);
255 spin_lock(&lli->lli_lock);
258 lli->lli_sai->sai_replied++;
259 ll_sai_entry_set(lli->lli_sai, minfo->mi_cbdata,
261 cfs_waitq_signal(&lli->lli_sai->sai_thread.t_ctl_waitq);
263 spin_unlock(&lli->lli_lock);
264 ll_intent_release(it);
271 static void sa_args_fini(struct md_enqueue_info *minfo,
272 struct ldlm_enqueue_info *einfo)
274 LASSERT(minfo && einfo);
279 static int sa_args_prep(struct inode *dir, struct dentry *dentry,
280 struct md_enqueue_info **pmi,
281 struct ldlm_enqueue_info **pei)
283 struct ll_inode_info *lli = ll_i2info(dir);
284 struct md_enqueue_info *minfo;
285 struct ldlm_enqueue_info *einfo;
287 OBD_ALLOC_PTR(einfo);
291 OBD_ALLOC_PTR(minfo);
297 minfo->mi_exp = ll_i2mdcexp(dir);
298 minfo->mi_it.it_op = IT_GETATTR;
299 minfo->mi_dentry = dentry;
300 minfo->mi_cb = ll_statahead_interpret;
301 minfo->mi_cbdata = lli->lli_sai->sai_sent;
303 einfo->ei_type = LDLM_IBITS;
304 einfo->ei_mode = it_to_lock_mode(&minfo->mi_it);
305 einfo->ei_cb_bl = ll_mdc_blocking_ast;
306 einfo->ei_cb_cp = ldlm_completion_ast;
307 einfo->ei_cb_gl = NULL;
308 einfo->ei_cbdata = NULL;
316 /* similar to ll_lookup_it(). */
317 static int do_sa_lookup(struct inode *dir, struct dentry *dentry)
319 struct md_enqueue_info *minfo;
320 struct ldlm_enqueue_info *einfo;
324 rc = sa_args_prep(dir, dentry, &minfo, &einfo);
328 rc = ll_prepare_mdc_op_data(&minfo->mi_data, dir, NULL,
329 dentry->d_name.name, dentry->d_name.len, 0,
332 rc = mdc_intent_getattr_async(minfo->mi_exp, minfo, einfo);
335 sa_args_fini(minfo, einfo);
340 /* similar to ll_revalidate_it().
341 * return 1: dentry valid.
342 * 0: will send stat-ahead request.
343 * -errno: prepare stat-ahead request failed. */
344 static int do_sa_revalidate(struct dentry *dentry)
346 struct inode *inode = dentry->d_inode;
347 struct ll_inode_info *lli = ll_i2info(dentry->d_parent->d_inode);
349 struct lookup_intent it = { .it_op = IT_GETATTR };
350 struct md_enqueue_info *minfo;
351 struct ldlm_enqueue_info *einfo;
358 if (d_mountpoint(dentry))
361 ll_inode2fid(&fid, inode);
363 rc = mdc_revalidate_lock(ll_i2mdcexp(inode), &it, &fid);
365 ll_intent_release(&it);
366 lli->lli_sai->sai_cached++;
367 cfs_waitq_signal(&lli->lli_sai->sai_thread.t_ctl_waitq);
371 rc = sa_args_prep(dentry->d_parent->d_inode, dentry, &minfo, &einfo);
375 rc = ll_prepare_mdc_op_data(&minfo->mi_data, dentry->d_parent->d_inode,
376 inode, dentry->d_name.name,
377 dentry->d_name.len, 0, NULL);
379 rc = mdc_intent_getattr_async(minfo->mi_exp, minfo, einfo);
382 sa_args_fini(minfo, einfo);
387 /* copied from kernel */
388 static inline void name2qstr(struct qstr *this, const char *name, int namelen)
391 const unsigned char *p = (const unsigned char *)name;
395 hash = init_name_hash();
396 for (len = 0; len < namelen; len++, p++) {
398 hash = partial_name_hash(c, hash);
402 this->hash = end_name_hash(hash);
405 static int ll_statahead_one(struct dentry *parent, ext2_dirent *de)
407 struct inode *dir = parent->d_inode;
408 struct ll_inode_info *lli = ll_i2info(dir);
410 struct dentry *dentry;
411 struct ll_sai_entry *se;
415 name2qstr(&name, de->name, de->name_len);
417 se = ll_sai_entry_get(lli->lli_sai, lli->lli_sai->sai_sent,
420 #ifdef DCACHE_LUSTRE_INVALID
421 if (parent->d_flags & DCACHE_LUSTRE_INVALID) {
423 if (d_unhashed(parent)) {
425 CDEBUG(D_READA, "parent dentry@%p %.*s is "
426 "invalid, skip statahead\n",
427 parent, parent->d_name.len, parent->d_name.name);
428 GOTO(out, rc = -EINVAL);
431 dentry = d_lookup(parent, &name);
433 struct dentry *dentry = d_alloc(parent, &name);
437 rc = do_sa_lookup(dir, dentry);
444 rc = do_sa_revalidate(dentry);
450 CDEBUG(D_READA, "set sai entry %p index %d stat %d, rc %d\n",
451 se, se->se_index, se->se_stat, rc);
453 cfs_waitq_signal(&lli->lli_sai->sai_thread.t_ctl_waitq);
455 lli->lli_sai->sai_sent++;
459 static inline int sa_check_stop(struct ll_statahead_info *sai)
461 return !!(sai->sai_thread.t_flags & SVC_STOPPING);
464 static inline int sa_not_full(struct ll_statahead_info *sai)
466 return sai->sai_sent - sai->sai_miss - sai->sai_hit < sai->sai_max;
469 struct ll_sa_thread_args {
470 struct dentry *sta_parent;
474 static int ll_statahead_thread(void *arg)
476 struct ll_sa_thread_args *sta = arg;
477 struct dentry *parent = dget(sta->sta_parent);
478 struct inode *dir = parent->d_inode;
479 struct ll_inode_info *lli = ll_i2info(dir);
480 struct ll_sb_info *sbi = ll_i2sbi(dir);
481 struct ll_statahead_info *sai = ll_sai_get(lli->lli_sai);
482 struct ptlrpc_thread *thread = &sai->sai_thread;
483 struct l_wait_info lwi = { 0 };
484 unsigned long index = 0;
493 snprintf(name, 15, "ll_sa_%u", sta->sta_pid);
495 thread->t_flags = SVC_RUNNING;
496 cfs_waitq_signal(&thread->t_ctl_waitq);
497 CDEBUG(D_READA, "start doing statahead for %s\n", parent->d_name.name);
500 CDEBUG(D_READA, "do statahead for hidden files\n");
503 unsigned long npages = dir_pages(dir);
505 /* hit ratio < 80% */
506 if ((sai->sai_hit < 4 * sai->sai_miss && sai->sai_hit > 7) ||
507 (sai->sai_consecutive_miss > 8)) {
509 CDEBUG(D_READA, "statahead for dir %.*s hit ratio too "
510 "low: hit/miss %u/%u, sent/replied %u/%u, "
512 parent->d_name.len, parent->d_name.name,
513 sai->sai_hit, sai->sai_miss, sai->sai_sent,
514 sai->sai_replied, sai->sai_cached);
518 /* reach the end of dir */
519 if (index == npages) {
520 CDEBUG(D_READA, "reach end, index/npages %lu/%lu\n",
525 l_wait_event(thread->t_ctl_waitq,
526 sa_check_stop(sai) || sa_not_full(sai),
529 if (sa_check_stop(sai))
532 for (; index < npages; index++, offset = 0) {
537 CDEBUG(D_EXT2,"read %lu of dir %lu/%u page %lu"
539 CFS_PAGE_SIZE, dir->i_ino, dir->i_generation,
540 index, npages, dir->i_size);
542 page = ll_get_dir_page(dir, index);
543 npages = dir_pages(dir);
547 CERROR("error reading dir %lu/%u page %lu: "
549 dir->i_ino, dir->i_generation, index,
554 kaddr = page_address(page);
555 de = (ext2_dirent *)(kaddr + offset);
556 limit = kaddr + CFS_PAGE_SIZE - EXT2_DIR_REC_LEN(1);
557 for (; (char*)de <= limit && sa_not_full(sai);
558 de = ext2_next_entry(de)) {
562 /* don't stat-ahead ".", ".." */
568 /* don't stat-ahead for hidden files */
569 if (de->name[0] == '.' && !sai->sai_ls_all)
572 /* don't stat-ahead for the first de */
578 rc = ll_statahead_one(parent, de);
584 offset = (char *)de - kaddr;
587 if ((char *)de <= limit)
594 spin_lock(&lli->lli_lock);
595 thread->t_flags = SVC_STOPPED;
596 cfs_waitq_signal(&thread->t_ctl_waitq);
597 lli->lli_opendir_pid = 0; /* avoid statahead again */
598 spin_unlock(&lli->lli_lock);
602 CDEBUG(D_READA, "stopped statahead thread, pid %d for %s\n",
603 current->pid, parent->d_name.name);
607 /* called in ll_file_release */
608 void ll_stop_statahead(struct inode *inode)
610 struct ll_inode_info *lli = ll_i2info(inode);
611 struct ptlrpc_thread *thread;
613 spin_lock(&lli->lli_lock);
614 /* don't check pid here. upon fork, if parent closedir before child,
615 * child will not have chance to stop this thread. */
616 lli->lli_opendir_pid = 0;
618 if (lli->lli_sai && (lli->lli_sai->sai_thread.t_flags & SVC_RUNNING)) {
619 struct l_wait_info lwi = { 0 };
620 ll_sai_get(lli->lli_sai);
621 thread = &lli->lli_sai->sai_thread;
622 thread->t_flags = SVC_STOPPING;
623 cfs_waitq_signal(&thread->t_ctl_waitq);
624 spin_unlock(&lli->lli_lock);
626 CDEBUG(D_READA, "stopping statahead thread, pid %d\n",
628 l_wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED,
630 ll_sai_put(lli->lli_sai);
634 spin_unlock(&lli->lli_lock);
638 LS_NONE_FIRST_DE = 0, /* not first dirent, or is "." */
639 LS_FIRST_DE, /* the first non-hidden dirent */
640 LS_FIRST_DOT_DE /* the first hidden dirent, that is ".xxx" */
643 static int is_first_dirent(struct inode *dir, struct dentry *dentry)
645 struct qstr *d_name = &dentry->d_name;
646 unsigned long npages = dir_pages(dir);
652 int dot_de = 1; /* dirent is dotfile till now */
653 int rc = LS_NONE_FIRST_DE;
656 page = ll_get_dir_page(dir, 0);
658 CERROR("error reading dir %lu/%u page 0: rc %ld\n",
659 dir->i_ino, dir->i_generation, PTR_ERR(page));
660 RETURN(LS_NONE_FIRST_DE);
663 kaddr = page_address(page);
664 de = (ext2_dirent *)kaddr;
665 if (!(de->name_len == 1 && strncmp(de->name, ".", 1) == 0))
666 CWARN("Maybe got bad on-disk dir:%lu\n", dir->i_ino);
667 de = ext2_next_entry(de); /* skip ".", or ingore bad entry */
668 if (!(de->name_len == 2 && strncmp(de->name, "..", 2) == 0))
669 CWARN("Maybe got bad on-disk dir:%lu\n", dir->i_ino);
670 de = ext2_next_entry(de); /* skip "..", or ingore bad entry */
672 offset = (char *)de - kaddr;
674 for (index = 0; index < npages; offset = 0) {
675 de = (ext2_dirent *)(kaddr + offset);
676 limit = kaddr + CFS_PAGE_SIZE - EXT2_DIR_REC_LEN(1);
677 for (; (char*)de <= limit; de = ext2_next_entry(de)) {
681 if (de->name[0] != '.')
684 if (dot_de && d_name->name[0] != '.') {
685 CDEBUG(D_READA, "%.*s skip hidden file %.*s\n",
686 d_name->len, d_name->name,
687 de->name_len, de->name);
691 if (d_name->len == de->name_len &&
692 !strncmp(d_name->name, de->name, d_name->len))
693 rc = LS_FIRST_DE + dot_de;
695 rc = LS_NONE_FIRST_DE;
699 if (++index >= npages)
704 page = ll_get_dir_page(dir, index);
706 CERROR("error reading dir %lu/%u page %lu: rc %ld\n",
707 dir->i_ino, dir->i_generation, index,
709 RETURN(LS_NONE_FIRST_DE);
711 kaddr = page_address(page);
713 CERROR("%.*s not found in dir %.*s!\n", d_name->len, d_name->name,
714 dentry->d_parent->d_name.len, dentry->d_parent->d_name.name);
721 /* start stat-ahead thread if this is the first dir entry, otherwise if a thread
722 * is started already, wait until thread is ahead of me.
726 * -EEXIST -- stat ahead thread started, and this is the first try.
727 * other negative value -- error.
729 int ll_statahead_enter(struct inode *dir, struct dentry **dentryp, int lookup)
731 struct ll_sb_info *sbi = ll_i2sbi(dir);
732 struct ll_inode_info *lli = ll_i2info(dir);
733 struct ll_statahead_info *sai;
734 struct ll_sa_thread_args sta;
735 struct l_wait_info lwi = { 0 };
739 if (sbi->ll_sa_max == 0)
742 /* not the same process, don't statahead */
743 if (lli->lli_opendir_pid != current->pid)
746 spin_lock(&lli->lli_lock);
748 sai = ll_sai_get(lli->lli_sai);
749 spin_unlock(&lli->lli_lock);
751 if (ll_sai_entry_stated(sai)) {
754 struct l_wait_info lwi = { 0 };
756 sbi->ll_sa_blocked++;
757 /* thread started already, avoid double-stat */
758 l_wait_event(sai->sai_thread.t_ctl_waitq,
759 ll_sai_entry_stated(sai) ||
760 sai->sai_thread.t_flags & SVC_STOPPED,
767 struct dentry *result;
769 result = d_lookup((*dentryp)->d_parent,
770 &(*dentryp)->d_name);
772 LASSERT(result != *dentryp);
776 RETURN(result != NULL);
778 /* do nothing for revalidate */
781 spin_unlock(&lli->lli_lock);
783 rc = is_first_dirent(dir, *dentryp);
785 /* optimization: don't statahead for this pid any longer */
786 spin_lock(&lli->lli_lock);
787 if (lli->lli_sai == NULL)
788 lli->lli_opendir_pid = 0;
789 spin_unlock(&lli->lli_lock);
793 spin_lock(&lli->lli_lock);
794 if (lli->lli_sai == NULL) {
795 lli->lli_sai = ll_sai_alloc();
796 if (lli->lli_sai == NULL) {
797 spin_unlock(&lli->lli_lock);
801 /* sai is already there */
802 spin_unlock(&lli->lli_lock);
805 spin_unlock(&lli->lli_lock);
808 sai->sai_inode = igrab(dir);
809 sai->sai_ls_all = (rc == LS_FIRST_DOT_DE);
811 sta.sta_parent = (*dentryp)->d_parent;
812 sta.sta_pid = current->pid;
813 rc = kernel_thread(ll_statahead_thread, &sta, 0);
815 CERROR("can't start ll_sa thread, rc: %d\n", rc);
820 l_wait_event(sai->sai_thread.t_ctl_waitq,
821 sai->sai_thread.t_flags & (SVC_RUNNING | SVC_STOPPED),
825 /* we don't stat-ahead for the first dirent since we are already in
826 * lookup, and -EEXIST also indicates that this is the first dirent.
831 /* update hit/miss count */
832 void ll_statahead_exit(struct dentry *dentry, int result)
834 struct ll_inode_info *lli = ll_i2info(dentry->d_parent->d_inode);
835 struct ll_sb_info *sbi = ll_i2sbi(dentry->d_parent->d_inode);
837 if (lli->lli_opendir_pid != current->pid)
840 spin_lock(&lli->lli_lock);
842 struct ll_statahead_info *sai = lli->lli_sai;
844 ll_sai_entry_put(sai);
847 sai->sai_consecutive_miss = 0;
848 sai->sai_max = min(2 * sai->sai_max, sbi->ll_sa_max);
849 CDEBUG(D_READA, "statahead %.*s hit(hit/miss %u/%u)\n",
850 dentry->d_name.len, dentry->d_name.name,
851 sai->sai_hit, sai->sai_miss);
854 sai->sai_consecutive_miss++;
855 /* upon miss, it's always because some dentry is added
856 * by statahead thread, and at the mean time `ls`
857 * processs finds this dentry, but the d_op for this
858 * dentry is NULL, then revalidate is not done, and
859 * ll_statahead_exit() not called for this dentry,
860 * so statahead thread should be behind of `ls` process,
861 * put one entry to go ahead.
863 CDEBUG(D_READA, "statahead %.*s miss(hit/miss %u/%u)\n",
864 dentry->d_name.len, dentry->d_name.name,
865 sai->sai_hit, sai->sai_miss);
866 ll_sai_entry_put(sai);
868 cfs_waitq_signal(&sai->sai_thread.t_ctl_waitq);
870 spin_unlock(&lli->lli_lock);