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 init_waitqueue_head(&sai->sai_thread.t_ctl_waitq);
58 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 LASSERT(sai->sai_thread.t_flags & SVC_STOPPED);
81 list_for_each_entry_safe(entry, next, &sai->sai_entries,
83 list_del(&entry->se_list);
88 spin_unlock(&lli->lli_lock);
94 static struct ll_sai_entry *ll_sai_entry_get(struct ll_statahead_info *sai,
97 struct ll_inode_info *lli = ll_i2info(sai->sai_inode);
98 struct ll_sb_info *sbi = ll_i2sbi(sai->sai_inode);
99 struct ll_sai_entry *entry;
102 OBD_ALLOC_PTR(entry);
106 CDEBUG(D_READA, "alloc sai entry %p index %d, stat %d\n",
108 entry->se_index = index;
109 entry->se_stat = stat;
111 spin_lock(&lli->lli_lock);
112 list_add_tail(&entry->se_list, &sai->sai_entries);
113 sai->sai_entries_nr++;
114 sbi->ll_sa_count = sai->sai_entries_nr;
115 spin_unlock(&lli->lli_lock);
117 LASSERT(sai->sai_entries_nr <= sbi->ll_sa_max);
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);
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);
213 LASSERT(dentry->d_inode);
216 ll_lookup_finish_locks(it, dentry);
220 struct mds_body *body;
222 body = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF,
224 if (memcmp(&minfo->mi_data.fid2, &body->fid1,
225 sizeof(body->fid1))) {
226 ll_unhash_aliases(dentry->d_inode);
227 GOTO(out, rc = -EAGAIN);
230 rc = revalidate_it_finish(req, DLM_REPLY_REC_OFF, it, dentry);
232 ll_unhash_aliases(dentry->d_inode);
236 spin_lock(&dcache_lock);
239 #ifdef DCACHE_LUSTRE_INVALID
240 dentry->d_flags &= ~DCACHE_LUSTRE_INVALID;
242 unlock_dentry(dentry);
243 __d_rehash(dentry, 0);
244 spin_unlock(&dcache_lock);
246 ll_lookup_finish_locks(it, dentry);
251 spin_lock(&lli->lli_lock);
254 lli->lli_sai->sai_replied++;
255 ll_sai_entry_set(lli->lli_sai, (int)minfo->mi_cbdata,
257 wake_up(&lli->lli_sai->sai_thread.t_ctl_waitq);
259 spin_unlock(&lli->lli_lock);
260 ll_intent_release(it);
267 static void sa_args_fini(struct md_enqueue_info *minfo,
268 struct ldlm_enqueue_info *einfo)
270 LASSERT(minfo && einfo);
275 static int sa_args_prep(struct inode *dir, struct dentry *dentry,
276 struct md_enqueue_info **pmi,
277 struct ldlm_enqueue_info **pei)
279 struct ll_inode_info *lli = ll_i2info(dir);
280 struct md_enqueue_info *minfo;
281 struct ldlm_enqueue_info *einfo;
283 OBD_ALLOC_PTR(einfo);
287 OBD_ALLOC_PTR(minfo);
293 minfo->mi_exp = ll_i2mdcexp(dir);
294 minfo->mi_it.it_op = IT_GETATTR;
295 minfo->mi_dentry = dentry;
296 minfo->mi_cb = ll_statahead_interpret;
297 minfo->mi_cbdata = (void *)lli->lli_sai->sai_sent;
299 einfo->ei_type = LDLM_IBITS;
300 einfo->ei_mode = it_to_lock_mode(&minfo->mi_it);
301 einfo->ei_cb_bl = ll_mdc_blocking_ast;
302 einfo->ei_cb_cp = ldlm_completion_ast;
303 einfo->ei_cb_gl = NULL;
304 einfo->ei_cbdata = NULL;
312 /* similar to ll_lookup_it(). */
313 static int do_sa_lookup(struct inode *dir, struct dentry *dentry)
315 struct md_enqueue_info *minfo;
316 struct ldlm_enqueue_info *einfo;
320 rc = sa_args_prep(dir, dentry, &minfo, &einfo);
324 rc = ll_prepare_mdc_op_data(&minfo->mi_data, dir, NULL,
325 dentry->d_name.name, dentry->d_name.len, 0,
328 rc = mdc_intent_getattr_async(minfo->mi_exp, minfo, einfo);
331 sa_args_fini(minfo, einfo);
336 /* similar to ll_revalidate_it().
337 * return 1: dentry valid.
338 * 0: will send stat-ahead request.
339 * -errno: prepare stat-ahead request failed. */
340 static int do_sa_revalidate(struct dentry *dentry)
342 struct inode *inode = dentry->d_inode;
343 struct ll_inode_info *lli = ll_i2info(dentry->d_parent->d_inode);
345 struct lookup_intent it = { .it_op = IT_GETATTR };
346 struct md_enqueue_info *minfo;
347 struct ldlm_enqueue_info *einfo;
354 if (d_mountpoint(dentry))
357 ll_inode2fid(&fid, inode);
359 rc = mdc_revalidate_lock(ll_i2mdcexp(inode), &it, &fid);
361 ll_intent_release(&it);
362 lli->lli_sai->sai_cached++;
363 wake_up(&lli->lli_sai->sai_thread.t_ctl_waitq);
367 rc = sa_args_prep(dentry->d_parent->d_inode, dentry, &minfo, &einfo);
371 rc = ll_prepare_mdc_op_data(&minfo->mi_data, dentry->d_parent->d_inode,
372 inode, dentry->d_name.name,
373 dentry->d_name.len, 0, NULL);
375 rc = mdc_intent_getattr_async(minfo->mi_exp, minfo, einfo);
378 sa_args_fini(minfo, einfo);
383 /* copied from kernel */
384 static inline void name2qstr(struct qstr *this, const char *name, int namelen)
387 const unsigned char *p = (const unsigned char *)name;
391 hash = init_name_hash();
392 for (len = 0; len < namelen; len++, p++) {
394 hash = partial_name_hash(c, hash);
398 this->hash = end_name_hash(hash);
401 static int ll_statahead_one(struct dentry *parent, ext2_dirent *de)
403 struct inode *dir = parent->d_inode;
404 struct ll_inode_info *lli = ll_i2info(dir);
406 struct dentry *dentry;
407 struct ll_sai_entry *se;
411 name2qstr(&name, de->name, de->name_len);
413 se = ll_sai_entry_get(lli->lli_sai, lli->lli_sai->sai_sent,
416 #ifdef DCACHE_LUSTRE_INVALID
417 if (parent->d_flags & DCACHE_LUSTRE_INVALID) {
419 if (d_unhashed(parent)) {
421 CDEBUG(D_READA, "parent dentry@%p %.*s is "
422 "invalid, skip statahead\n",
423 parent, parent->d_name.len, parent->d_name.name);
424 GOTO(out, rc = -EINVAL);
427 dentry = d_lookup(parent, &name);
429 struct dentry *dentry = d_alloc(parent, &name);
433 rc = do_sa_lookup(dir, dentry);
440 rc = do_sa_revalidate(dentry);
446 CDEBUG(D_READA, "set sai entry %p index %d stat %d, rc %d\n",
447 se, se->se_index, se->se_stat, rc);
449 wake_up(&lli->lli_sai->sai_thread.t_ctl_waitq);
451 lli->lli_sai->sai_sent++;
455 static inline int sa_check_stop(struct ll_statahead_info *sai)
457 return !!(sai->sai_thread.t_flags & SVC_STOPPING);
460 static inline int sa_not_full(struct ll_statahead_info *sai)
462 return sai->sai_sent - sai->sai_miss - sai->sai_hit < sai->sai_max;
465 struct ll_sa_thread_args {
466 struct dentry *sta_parent;
470 static int ll_statahead_thread(void *arg)
472 struct ll_sa_thread_args *sta = arg;
473 struct dentry *parent = dget(sta->sta_parent);
474 struct inode *dir = parent->d_inode;
475 struct ll_inode_info *lli = ll_i2info(dir);
476 struct ll_sb_info *sbi = ll_i2sbi(dir);
477 struct ll_statahead_info *sai = ll_sai_get(lli->lli_sai);
478 struct ptlrpc_thread *thread = &sai->sai_thread;
479 struct l_wait_info lwi = { 0 };
480 unsigned long index = 0;
489 snprintf(name, 15, "ll_sa_%u", sta->sta_pid);
491 thread->t_flags = SVC_RUNNING;
492 wake_up(&thread->t_ctl_waitq);
493 CDEBUG(D_READA, "start doing statahead for %s\n", parent->d_name.name);
496 CDEBUG(D_READA, "do statahead for hidden files\n");
499 unsigned long npages = dir_pages(dir);
501 /* hit ratio < 80% */
502 if ((sai->sai_hit < 4 * sai->sai_miss && sai->sai_hit > 7) ||
503 (sai->sai_consecutive_miss > 8)) {
505 CDEBUG(D_READA, "statahead for dir %.*s hit ratio too "
506 "low: hit/miss %u/%u, sent/replied %u/%u, "
508 parent->d_name.len, parent->d_name.name,
509 sai->sai_hit, sai->sai_miss, sai->sai_sent,
510 sai->sai_replied, sai->sai_cached);
514 /* reach the end of dir */
515 if (index == npages) {
516 CDEBUG(D_READA, "reach end, index/npages %lu/%lu\n",
521 l_wait_event(thread->t_ctl_waitq,
522 sa_check_stop(sai) || sa_not_full(sai),
525 if (sa_check_stop(sai))
528 for (; index < npages; index++, offset = 0) {
533 CDEBUG(D_EXT2,"read %lu of dir %lu/%u page %lu"
535 CFS_PAGE_SIZE, dir->i_ino, dir->i_generation,
536 index, npages, dir->i_size);
538 page = ll_get_dir_page(dir, index);
539 npages = dir_pages(dir);
543 CERROR("error reading dir %lu/%u page %lu: "
545 dir->i_ino, dir->i_generation, index,
550 kaddr = page_address(page);
551 de = (ext2_dirent *)(kaddr + offset);
552 limit = kaddr + CFS_PAGE_SIZE - EXT2_DIR_REC_LEN(1);
553 for (; (char*)de <= limit && sa_not_full(sai);
554 de = ext2_next_entry(de)) {
558 /* don't stat-ahead ".", ".." */
564 /* don't stat-ahead for hidden files */
565 if (de->name[0] == '.' && !sai->sai_ls_all)
568 /* don't stat-ahead for the first de */
574 rc = ll_statahead_one(parent, de);
580 offset = (char *)de - kaddr;
583 if ((char *)de <= limit)
590 thread->t_flags = SVC_STOPPED;
591 wake_up(&thread->t_ctl_waitq);
592 lli->lli_opendir_pid = 0; /* avoid statahead again */
595 CDEBUG(D_READA, "stopped statahead thread, pid %d for %s\n",
596 current->pid, parent->d_name.name);
600 /* called in ll_file_release */
601 void ll_stop_statahead(struct inode *inode)
603 struct ll_inode_info *lli = ll_i2info(inode);
604 struct ptlrpc_thread *thread;
606 /* don't check pid here. upon fork, if parent closedir before child,
607 * child will not have chance to stop this thread. */
608 lli->lli_opendir_pid = 0;
610 spin_lock(&lli->lli_lock);
612 ll_sai_get(lli->lli_sai);
613 spin_unlock(&lli->lli_lock);
615 CDEBUG(D_READA, "stopping statahead thread, pid %d\n",
617 thread = &lli->lli_sai->sai_thread;
618 thread->t_flags = SVC_STOPPING;
619 wake_up(&thread->t_ctl_waitq);
620 wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED);
621 ll_sai_put(lli->lli_sai);
625 spin_unlock(&lli->lli_lock);
629 LS_NONE_FIRST_DE = 0, /* not first dirent, or is "." */
630 LS_FIRST_DE, /* the first non-hidden dirent */
631 LS_FIRST_DOT_DE /* the first hidden dirent, that is ".xxx" */
634 static int is_first_dirent(struct inode *dir, struct dentry *dentry)
636 struct qstr *d_name = &dentry->d_name;
637 unsigned long npages = dir_pages(dir);
643 int dot_de = 1; /* dirent is dotfile till now */
644 int rc = LS_NONE_FIRST_DE;
647 page = ll_get_dir_page(dir, 0);
649 CERROR("error reading dir %lu/%u page 0: rc %ld\n",
650 dir->i_ino, dir->i_generation, PTR_ERR(page));
651 RETURN(LS_NONE_FIRST_DE);
654 kaddr = page_address(page);
655 de = (ext2_dirent *)kaddr;
656 if (!(de->name_len == 1 && strncmp(de->name, ".", 1) == 0))
657 CWARN("Maybe got bad on-disk dir:%lu\n", dir->i_ino);
658 de = ext2_next_entry(de); /* skip ".", or ingore bad entry */
659 if (!(de->name_len == 2 && strncmp(de->name, "..", 2) == 0))
660 CWARN("Maybe got bad on-disk dir:%lu\n", dir->i_ino);
661 de = ext2_next_entry(de); /* skip "..", or ingore bad entry */
663 offset = (char *)de - kaddr;
665 for (index = 0; index < npages; offset = 0) {
666 de = (ext2_dirent *)(kaddr + offset);
667 limit = kaddr + CFS_PAGE_SIZE - EXT2_DIR_REC_LEN(1);
668 for (; (char*)de <= limit; de = ext2_next_entry(de)) {
672 if (de->name[0] != '.')
675 if (dot_de && d_name->name[0] != '.') {
676 CDEBUG(D_READA, "%.*s skip hidden file %.*s\n",
677 d_name->len, d_name->name,
678 de->name_len, de->name);
682 if (d_name->len == de->name_len &&
683 !strncmp(d_name->name, de->name, d_name->len))
684 rc = LS_FIRST_DE + dot_de;
686 rc = LS_NONE_FIRST_DE;
690 if (++index >= npages)
695 page = ll_get_dir_page(dir, index);
697 CERROR("error reading dir %lu/%u page %lu: rc %ld\n",
698 dir->i_ino, dir->i_generation, index,
700 RETURN(LS_NONE_FIRST_DE);
702 kaddr = page_address(page);
704 CERROR("%.*s not found in dir %.*s!\n", d_name->len, d_name->name,
705 dentry->d_parent->d_name.len, dentry->d_parent->d_name.name);
712 /* start stat-ahead thread if this is the first dir entry, otherwise if a thread
713 * is started already, wait until thread is ahead of me.
717 * -EEXIST -- stat ahead thread started, and this is the first try.
718 * other negative value -- error.
720 int ll_statahead_enter(struct inode *dir, struct dentry **dentryp, int lookup)
722 struct ll_sb_info *sbi = ll_i2sbi(dir);
723 struct ll_inode_info *lli = ll_i2info(dir);
724 struct ll_statahead_info *sai;
725 struct ll_sa_thread_args sta;
729 if (sbi->ll_sa_max == 0)
732 /* not the same process, don't statahead */
733 if (lli->lli_opendir_pid != current->pid)
736 spin_lock(&lli->lli_lock);
738 sai = ll_sai_get(lli->lli_sai);
739 spin_unlock(&lli->lli_lock);
741 if (ll_sai_entry_stated(sai)) {
744 struct l_wait_info lwi = { 0 };
746 sbi->ll_sa_blocked++;
747 /* thread started already, avoid double-stat */
748 l_wait_event(sai->sai_thread.t_ctl_waitq,
749 ll_sai_entry_stated(sai) ||
750 sai->sai_thread.t_flags & SVC_STOPPED,
757 struct dentry *result;
759 result = d_lookup((*dentryp)->d_parent,
760 &(*dentryp)->d_name);
762 LASSERT(result != *dentryp);
766 RETURN(result != NULL);
768 /* do nothing for revalidate */
771 spin_unlock(&lli->lli_lock);
773 rc = is_first_dirent(dir, *dentryp);
775 /* optimization: don't statahead for this pid any longer */
776 spin_lock(&lli->lli_lock);
777 if (lli->lli_sai == NULL)
778 lli->lli_opendir_pid = 0;
779 spin_unlock(&lli->lli_lock);
783 spin_lock(&lli->lli_lock);
784 if (lli->lli_sai == NULL) {
785 lli->lli_sai = ll_sai_alloc();
786 if (lli->lli_sai == NULL) {
787 spin_unlock(&lli->lli_lock);
791 /* sai is already there */
792 spin_unlock(&lli->lli_lock);
795 spin_unlock(&lli->lli_lock);
798 sai->sai_inode = igrab(dir);
799 sai->sai_ls_all = (rc == LS_FIRST_DOT_DE);
801 sta.sta_parent = (*dentryp)->d_parent;
802 sta.sta_pid = current->pid;
803 rc = kernel_thread(ll_statahead_thread, &sta, 0);
805 CERROR("can't start ll_sa thread, rc: %d\n", rc);
810 wait_event(sai->sai_thread.t_ctl_waitq,
811 sai->sai_thread.t_flags & (SVC_RUNNING | SVC_STOPPED));
814 /* we don't stat-ahead for the first dirent since we are already in
815 * lookup, and -EEXIST also indicates that this is the first dirent.
820 /* update hit/miss count */
821 void ll_statahead_exit(struct dentry *dentry, int result)
823 struct ll_inode_info *lli = ll_i2info(dentry->d_parent->d_inode);
824 struct ll_sb_info *sbi = ll_i2sbi(dentry->d_parent->d_inode);
826 if (lli->lli_opendir_pid != current->pid)
829 spin_lock(&lli->lli_lock);
831 struct ll_statahead_info *sai = lli->lli_sai;
833 ll_sai_entry_put(sai);
836 sai->sai_consecutive_miss = 0;
837 sai->sai_max = min(2 * sai->sai_max, sbi->ll_sa_max);
838 CDEBUG(D_READA, "statahead %.*s hit(hit/miss %u/%u)\n",
839 dentry->d_name.len, dentry->d_name.name,
840 sai->sai_hit, sai->sai_miss);
843 sai->sai_consecutive_miss++;
844 /* upon miss, it's always because some dentry is added
845 * by statahead thread, and at the mean time `ls`
846 * processs finds this dentry, but the d_op for this
847 * dentry is NULL, then revalidate is not done, and
848 * ll_statahead_exit() not called for this dentry,
849 * so statahead thread should be behind of `ls` process,
850 * put one entry to go ahead.
852 CDEBUG(D_READA, "statahead %.*s miss(hit/miss %u/%u)\n",
853 dentry->d_name.len, dentry->d_name.name,
854 sai->sai_hit, sai->sai_miss);
855 ll_sai_entry_put(sai);
857 wake_up(&sai->sai_thread.t_ctl_waitq);
859 spin_unlock(&lli->lli_lock);