X-Git-Url: https://git.whamcloud.com/?a=blobdiff_plain;f=lustre%2Fllite%2Fstatahead.c;h=b5f64b06d2bea5e6253572c5627a5156e0ed43aa;hb=e9b65026b5d74bd6538d900765386f6c99f1c3dd;hp=e5fff85a5c2b7bf82bedb06ccf0c39309ff52696;hpb=48aba8c61b31083774d6fb6b393da20b8d57e2c9;p=fs%2Flustre-release.git diff --git a/lustre/llite/statahead.c b/lustre/llite/statahead.c index e5fff85..b5f64b0 100644 --- a/lustre/llite/statahead.c +++ b/lustre/llite/statahead.c @@ -50,11 +50,13 @@ #include "llite_internal.h" struct ll_sai_entry { - struct list_head se_list; + cfs_list_t se_list; unsigned int se_index; int se_stat; struct ptlrpc_request *se_req; struct md_enqueue_info *se_minfo; + struct dentry *se_dentry; + struct inode *se_inode; }; enum { @@ -63,11 +65,11 @@ enum { }; static unsigned int sai_generation = 0; -static spinlock_t sai_generation_lock = SPIN_LOCK_UNLOCKED; +static cfs_spinlock_t sai_generation_lock = CFS_SPIN_LOCK_UNLOCKED; /** * Check whether first entry was stated already or not. - * No need to hold lli_lock, for: + * No need to hold lli_sa_lock, for: * (1) it is me that remove entry from the list * (2) the statahead thread only add new entry to the list */ @@ -76,9 +78,9 @@ static int ll_sai_entry_stated(struct ll_statahead_info *sai) struct ll_sai_entry *entry; int rc = 0; - if (!list_empty(&sai->sai_entries_stated)) { - entry = list_entry(sai->sai_entries_stated.next, - struct ll_sai_entry, se_list); + if (!cfs_list_empty(&sai->sai_entries_stated)) { + entry = cfs_list_entry(sai->sai_entries_stated.next, + struct ll_sai_entry, se_list); if (entry->se_index == sai->sai_index_next) rc = 1; } @@ -87,7 +89,7 @@ static int ll_sai_entry_stated(struct ll_statahead_info *sai) static inline int sa_received_empty(struct ll_statahead_info *sai) { - return list_empty(&sai->sai_entries_received); + return cfs_list_empty(&sai->sai_entries_received); } static inline int sa_not_full(struct ll_statahead_info *sai) @@ -117,10 +119,42 @@ static inline int sa_is_stopped(struct ll_statahead_info *sai) */ static inline int sa_low_hit(struct ll_statahead_info *sai) { - return ((sai->sai_hit < 4 * sai->sai_miss && sai->sai_hit > 7) || + return ((sai->sai_hit > 7 && sai->sai_hit < 4 * sai->sai_miss) || (sai->sai_consecutive_miss > 8)); } +static inline int sa_skip_nolock(struct ll_statahead_info *sai) +{ + return (sai->sai_nolock >= 3); +} + +static void ll_sai_entry_free(struct ll_sai_entry *entry) +{ + struct dentry *dentry = entry->se_dentry; + struct inode *inode = entry->se_inode; + + if (dentry) { + struct ll_dentry_data *lld = ll_d2d(dentry); + struct ll_inode_info *lli; + + entry->se_dentry = NULL; + LASSERT(inode != NULL); + lli = ll_i2info(inode); + if (!cfs_list_empty(&lli->lli_sa_dentry)) { + cfs_spin_lock(&lli->lli_sa_lock); + cfs_list_del_init(&lld->lld_sa_alias); + cfs_spin_unlock(&lli->lli_sa_lock); + } + dput(dentry); + } + if (inode) { + entry->se_inode = NULL; + iput(inode); + } + LASSERT(cfs_list_empty(&entry->se_list)); + OBD_FREE_PTR(entry); +} + /** * process the deleted entry's member and free the entry. * (1) release intent @@ -130,27 +164,23 @@ static inline int sa_low_hit(struct ll_statahead_info *sai) */ static void ll_sai_entry_cleanup(struct ll_sai_entry *entry, int free) { - struct ptlrpc_request *req = entry->se_req; struct md_enqueue_info *minfo = entry->se_minfo; + struct ptlrpc_request *req = entry->se_req; ENTRY; if (minfo) { - struct dentry *dentry = minfo->mi_dentry; - struct lookup_intent *it = &minfo->mi_it; - entry->se_minfo = NULL; - ll_intent_release(it); + ll_intent_release(&minfo->mi_it); + dput(minfo->mi_dentry); + iput(minfo->mi_dir); OBD_FREE_PTR(minfo); - dput(dentry); } if (req) { entry->se_req = NULL; ptlrpc_req_finished(req); } - if (free) { - LASSERT(list_empty(&entry->se_list)); - OBD_FREE_PTR(entry); - } + if (free) + ll_sai_entry_free(entry); EXIT; } @@ -163,12 +193,12 @@ static struct ll_statahead_info *ll_sai_alloc(void) if (!sai) return NULL; - spin_lock(&sai_generation_lock); + cfs_spin_lock(&sai_generation_lock); sai->sai_generation = ++sai_generation; if (unlikely(sai_generation == 0)) sai->sai_generation = ++sai_generation; - spin_unlock(&sai_generation_lock); - atomic_set(&sai->sai_refcount, 1); + cfs_spin_unlock(&sai_generation_lock); + cfs_atomic_set(&sai->sai_refcount, 1); sai->sai_max = LL_SA_RPC_MIN; cfs_waitq_init(&sai->sai_waitq); cfs_waitq_init(&sai->sai_thread.t_ctl_waitq); @@ -178,11 +208,11 @@ static struct ll_statahead_info *ll_sai_alloc(void) return sai; } -static inline +static inline struct ll_statahead_info *ll_sai_get(struct ll_statahead_info *sai) { LASSERT(sai); - atomic_inc(&sai->sai_refcount); + cfs_atomic_inc(&sai->sai_refcount); return sai; } @@ -194,13 +224,24 @@ static void ll_sai_put(struct ll_statahead_info *sai) LASSERT(inode != NULL); lli = ll_i2info(inode); - if (atomic_dec_and_lock(&sai->sai_refcount, &lli->lli_lock)) { + LASSERT(lli->lli_sai == sai); + + if (cfs_atomic_dec_and_test(&sai->sai_refcount)) { struct ll_sai_entry *entry, *next; + cfs_spin_lock(&lli->lli_sa_lock); + if (unlikely(cfs_atomic_read(&sai->sai_refcount) > 0)) { + /* It is race case, the interpret callback just hold + * a reference count */ + cfs_spin_unlock(&lli->lli_sa_lock); + EXIT; + return; + } + LASSERT(lli->lli_opendir_key == NULL); lli->lli_sai = NULL; lli->lli_opendir_pid = 0; - spin_unlock(&lli->lli_lock); + cfs_spin_unlock(&lli->lli_sa_lock); LASSERT(sa_is_stopped(sai)); @@ -210,23 +251,25 @@ static void ll_sai_put(struct ll_statahead_info *sai) PFID(&lli->lli_fid), sai->sai_sent, sai->sai_replied); - list_for_each_entry_safe(entry, next, &sai->sai_entries_sent, - se_list) { - list_del_init(&entry->se_list); + cfs_list_for_each_entry_safe(entry, next, + &sai->sai_entries_sent, se_list) { + cfs_list_del_init(&entry->se_list); ll_sai_entry_cleanup(entry, 1); } - list_for_each_entry_safe(entry, next, &sai->sai_entries_received, - se_list) { - list_del_init(&entry->se_list); + cfs_list_for_each_entry_safe(entry, next, + &sai->sai_entries_received, + se_list) { + cfs_list_del_init(&entry->se_list); ll_sai_entry_cleanup(entry, 1); } - list_for_each_entry_safe(entry, next, &sai->sai_entries_stated, - se_list) { - list_del_init(&entry->se_list); + cfs_list_for_each_entry_safe(entry, next, + &sai->sai_entries_stated, + se_list) { + cfs_list_del_init(&entry->se_list); ll_sai_entry_cleanup(entry, 1); } - OBD_FREE_PTR(sai); iput(inode); + OBD_FREE_PTR(sai); } EXIT; } @@ -248,11 +291,11 @@ ll_sai_entry_init(struct ll_statahead_info *sai, unsigned int index) CDEBUG(D_READA, "alloc sai entry %p index %u\n", entry, index); entry->se_index = index; - entry->se_stat = SA_ENTRY_UNSTATED; + entry->se_stat = SA_ENTRY_UNSTATED; - spin_lock(&lli->lli_lock); - list_add_tail(&entry->se_list, &sai->sai_entries_sent); - spin_unlock(&lli->lli_lock); + cfs_spin_lock(&lli->lli_sa_lock); + cfs_list_add_tail(&entry->se_list, &sai->sai_entries_sent); + cfs_spin_unlock(&lli->lli_sa_lock); RETURN(entry); } @@ -261,30 +304,33 @@ ll_sai_entry_init(struct ll_statahead_info *sai, unsigned int index) * delete it from sai_entries_stated head when fini, it need not * to process entry's member. */ -static void ll_sai_entry_fini(struct ll_statahead_info *sai) +static int ll_sai_entry_fini(struct ll_statahead_info *sai) { struct ll_inode_info *lli = ll_i2info(sai->sai_inode); struct ll_sai_entry *entry; + int rc = 0; ENTRY; - - spin_lock(&lli->lli_lock); + + cfs_spin_lock(&lli->lli_sa_lock); sai->sai_index_next++; - if (likely(!list_empty(&sai->sai_entries_stated))) { - entry = list_entry(sai->sai_entries_stated.next, - struct ll_sai_entry, se_list); + if (likely(!cfs_list_empty(&sai->sai_entries_stated))) { + entry = cfs_list_entry(sai->sai_entries_stated.next, + struct ll_sai_entry, se_list); if (entry->se_index < sai->sai_index_next) { - list_del(&entry->se_list); - OBD_FREE_PTR(entry); + cfs_list_del_init(&entry->se_list); + rc = entry->se_stat; + ll_sai_entry_free(entry); } - } else + } else { LASSERT(sa_is_stopped(sai)); - spin_unlock(&lli->lli_lock); + } + cfs_spin_unlock(&lli->lli_sa_lock); - EXIT; + RETURN(rc); } /** - * inside lli_lock. + * inside lli_sa_lock. * \retval NULL : can not find the entry in sai_entries_sent with the index * \retval entry: find the entry in sai_entries_sent with the index */ @@ -295,31 +341,33 @@ ll_sai_entry_set(struct ll_statahead_info *sai, unsigned int index, int stat, struct ll_sai_entry *entry; ENTRY; - if (!list_empty(&sai->sai_entries_sent)) { - list_for_each_entry(entry, &sai->sai_entries_sent, se_list) { + if (!cfs_list_empty(&sai->sai_entries_sent)) { + cfs_list_for_each_entry(entry, &sai->sai_entries_sent, + se_list) { if (entry->se_index == index) { entry->se_stat = stat; entry->se_req = ptlrpc_request_addref(req); entry->se_minfo = minfo; RETURN(entry); - } else if (entry->se_index > index) + } else if (entry->se_index > index) { RETURN(NULL); + } } } RETURN(NULL); } /** - * inside lli_lock. + * inside lli_sa_lock. * Move entry to sai_entries_received and * insert it into sai_entries_received tail. */ static inline void ll_sai_entry_to_received(struct ll_statahead_info *sai, struct ll_sai_entry *entry) { - if (!list_empty(&entry->se_list)) - list_del_init(&entry->se_list); - list_add_tail(&entry->se_list, &sai->sai_entries_received); + if (!cfs_list_empty(&entry->se_list)) + cfs_list_del_init(&entry->se_list); + cfs_list_add_tail(&entry->se_list, &sai->sai_entries_received); } /** @@ -335,20 +383,21 @@ ll_sai_entry_to_stated(struct ll_statahead_info *sai, struct ll_sai_entry *entry ll_sai_entry_cleanup(entry, 0); - spin_lock(&lli->lli_lock); - if (!list_empty(&entry->se_list)) - list_del_init(&entry->se_list); + cfs_spin_lock(&lli->lli_sa_lock); + if (!cfs_list_empty(&entry->se_list)) + cfs_list_del_init(&entry->se_list); + /* stale entry */ if (unlikely(entry->se_index < sai->sai_index_next)) { - spin_unlock(&lli->lli_lock); - OBD_FREE_PTR(entry); + cfs_spin_unlock(&lli->lli_sa_lock); + ll_sai_entry_free(entry); RETURN(0); } - list_for_each_entry_reverse(se, &sai->sai_entries_stated, se_list) { + cfs_list_for_each_entry_reverse(se, &sai->sai_entries_stated, se_list) { if (se->se_index < entry->se_index) { - list_add(&entry->se_list, &se->se_list); - spin_unlock(&lli->lli_lock); + cfs_list_add(&entry->se_list, &se->se_list); + cfs_spin_unlock(&lli->lli_sa_lock); RETURN(1); } } @@ -356,8 +405,8 @@ ll_sai_entry_to_stated(struct ll_statahead_info *sai, struct ll_sai_entry *entry /* * I am the first entry. */ - list_add(&entry->se_list, &sai->sai_entries_stated); - spin_unlock(&lli->lli_lock); + cfs_list_add(&entry->se_list, &sai->sai_entries_stated); + cfs_spin_unlock(&lli->lli_sa_lock); RETURN(1); } @@ -370,20 +419,22 @@ static int do_statahead_interpret(struct ll_statahead_info *sai) struct ll_sai_entry *entry; struct ptlrpc_request *req; struct md_enqueue_info *minfo; - struct dentry *dentry; struct lookup_intent *it; + struct dentry *dentry; int rc = 0; struct mdt_body *body; ENTRY; - spin_lock(&lli->lli_lock); + cfs_spin_lock(&lli->lli_sa_lock); LASSERT(!sa_received_empty(sai)); - entry = list_entry(sai->sai_entries_received.next, struct ll_sai_entry, - se_list); - list_del_init(&entry->se_list); - spin_unlock(&lli->lli_lock); + entry = cfs_list_entry(sai->sai_entries_received.next, + struct ll_sai_entry, se_list); + cfs_list_del_init(&entry->se_list); + cfs_spin_unlock(&lli->lli_sa_lock); if (unlikely(entry->se_index < sai->sai_index_next)) { + CWARN("Found stale entry: [index %u] [next %u]\n", + entry->se_index, sai->sai_index_next); ll_sai_entry_cleanup(entry, 1); RETURN(0); } @@ -393,8 +444,8 @@ static int do_statahead_interpret(struct ll_statahead_info *sai) req = entry->se_req; minfo = entry->se_minfo; - dentry = minfo->mi_dentry; it = &minfo->mi_it; + dentry = minfo->mi_dentry; body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY); if (body == NULL) @@ -405,33 +456,62 @@ static int do_statahead_interpret(struct ll_statahead_info *sai) * lookup. */ struct dentry *save = dentry; + __u32 bits = 0; struct it_cb_data icbd = { - .icbd_parent = dentry->d_parent->d_inode, - .icbd_childp = &dentry + .icbd_parent = minfo->mi_dir, + .icbd_childp = &dentry, + .icbd_alias = &entry->se_inode, + .bits = &bits }; LASSERT(fid_is_zero(&minfo->mi_data.op_fid2)); - /* - * XXX: No fid in reply, this is probaly cross-ref case. - * SA can't handle it yet. - */ + /* XXX: No fid in reply, this is probaly cross-ref case. + * SA can't handle it yet. */ if (body->valid & OBD_MD_MDS) GOTO(out, rc = -EAGAIN); + /* Here dentry->d_inode might be NULL, because the entry may + * have been removed before we start doing stat ahead. */ + + /* BUG 15962, 21739: since statahead thread does not hold + * parent's i_mutex, it can not alias the dentry to inode. + * Here we just create/update inode in memory, and let the + * main "ls -l" thread to alias such dentry to the inode with + * parent's i_mutex held. + * On the other hand, we hold ldlm ibits lock for the inode + * yet, to allow other operations to cancel such lock in time, + * we should drop the ldlm lock reference count, then the main + * "ls -l" thread should check/get such ldlm ibits lock before + * aliasing such dentry to the inode later. If we don't do such + * drop here, it maybe cause deadlock with i_muext held by + * others, just like bug 21739. */ rc = ll_lookup_it_finish(req, it, &icbd); - if (!rc) - /* - * Here dentry->d_inode might be NULL, - * because the entry may have been removed before - * we start doing stat ahead. - */ - ll_lookup_finish_locks(it, dentry); - - if (dentry != save) { - minfo->mi_dentry = dentry; - dput(save); + if (entry->se_inode != NULL) { + struct ll_dentry_data *lld = ll_d2d(dentry); + struct ll_inode_info *sei = ll_i2info(entry->se_inode); + + /* For statahead lookup case, both MDS_INODELOCK_LOOKUP + * and MDS_INODELOCK_UPDATE should be granted */ + if (likely(bits & MDS_INODELOCK_LOOKUP && + bits & MDS_INODELOCK_UPDATE)) { + /* the first dentry ref_count will be dropped by + * ll_sai_entry_to_stated(), so hold another ref + * in advance */ + entry->se_dentry = dget(dentry); + cfs_spin_lock(&sei->lli_sa_lock); + cfs_list_add(&lld->lld_sa_alias, + &sei->lli_sa_dentry); + cfs_spin_unlock(&sei->lli_sa_lock); + sai->sai_nolock = 0; + } else { + iput(entry->se_inode); + entry->se_inode = NULL; + sai->sai_nolock++; + } } + LASSERT(dentry == save); + ll_intent_drop_lock(it); } else { /* * revalidate. @@ -447,23 +527,25 @@ static int do_statahead_interpret(struct ll_statahead_info *sai) GOTO(out, rc); } - spin_lock(&ll_lookup_lock); + cfs_spin_lock(&ll_lookup_lock); spin_lock(&dcache_lock); lock_dentry(dentry); __d_drop(dentry); -#ifdef DCACHE_LUSTRE_INVALID dentry->d_flags &= ~DCACHE_LUSTRE_INVALID; -#endif unlock_dentry(dentry); d_rehash_cond(dentry, 0); spin_unlock(&dcache_lock); - spin_unlock(&ll_lookup_lock); + cfs_spin_unlock(&ll_lookup_lock); ll_lookup_finish_locks(it, dentry); } EXIT; out: + /* The "ll_sai_entry_to_stated()" will drop related ldlm ibits lock + * reference count with ll_intent_drop_lock() called in spite of the + * above operations failed or not. Do not worry about calling + * "ll_intent_drop_lock()" more than once. */ if (likely(ll_sai_entry_to_stated(sai, entry))) cfs_waitq_signal(&sai->sai_waitq); return rc; @@ -473,9 +555,9 @@ static int ll_statahead_interpret(struct ptlrpc_request *req, struct md_enqueue_info *minfo, int rc) { - struct dentry *dentry = minfo->mi_dentry; struct lookup_intent *it = &minfo->mi_it; - struct inode *dir = dentry->d_parent->d_inode; + struct dentry *dentry = minfo->mi_dentry; + struct inode *dir = minfo->mi_dir; struct ll_inode_info *lli = ll_i2info(dir); struct ll_statahead_info *sai; struct ll_sai_entry *entry; @@ -484,34 +566,33 @@ static int ll_statahead_interpret(struct ptlrpc_request *req, CDEBUG(D_READA, "interpret statahead %.*s rc %d\n", dentry->d_name.len, dentry->d_name.name, rc); - spin_lock(&lli->lli_lock); + cfs_spin_lock(&lli->lli_sa_lock); + /* stale entry */ if (unlikely(lli->lli_sai == NULL || lli->lli_sai->sai_generation != minfo->mi_generation)) { - spin_unlock(&lli->lli_lock); + cfs_spin_unlock(&lli->lli_sa_lock); ll_intent_release(it); dput(dentry); + iput(dir); OBD_FREE_PTR(minfo); RETURN(-ESTALE); } else { sai = ll_sai_get(lli->lli_sai); - if (rc || dir == NULL) - rc = -ESTALE; - entry = ll_sai_entry_set(sai, (unsigned int)(long)minfo->mi_cbdata, - rc ? SA_ENTRY_UNSTATED : - SA_ENTRY_STATED, req, minfo); + rc < 0 ? rc : SA_ENTRY_STATED, req, + minfo); LASSERT(entry != NULL); if (likely(sa_is_running(sai))) { ll_sai_entry_to_received(sai, entry); sai->sai_replied++; - spin_unlock(&lli->lli_lock); + cfs_spin_unlock(&lli->lli_sa_lock); cfs_waitq_signal(&sai->sai_thread.t_ctl_waitq); } else { - if (!list_empty(&entry->se_list)) - list_del_init(&entry->se_list); + if (!cfs_list_empty(&entry->se_list)) + cfs_list_del_init(&entry->se_list); sai->sai_replied++; - spin_unlock(&lli->lli_lock); + cfs_spin_unlock(&lli->lli_sa_lock); ll_sai_entry_cleanup(entry, 1); } ll_sai_put(sai); @@ -523,6 +604,7 @@ static void sa_args_fini(struct md_enqueue_info *minfo, struct ldlm_enqueue_info *einfo) { LASSERT(minfo && einfo); + iput(minfo->mi_dir); capa_put(minfo->mi_data.op_capa1); capa_put(minfo->mi_data.op_capa2); OBD_FREE_PTR(minfo); @@ -569,6 +651,7 @@ static int sa_args_init(struct inode *dir, struct dentry *dentry, minfo->mi_it.it_op = IT_GETATTR; minfo->mi_dentry = dentry; + minfo->mi_dir = igrab(dir); minfo->mi_cb = ll_statahead_interpret; minfo->mi_generation = lli->lli_sai->sai_generation; minfo->mi_cbdata = (void *)(long)lli->lli_sai->sai_index; @@ -620,10 +703,9 @@ static int do_sa_lookup(struct inode *dir, struct dentry *dentry) * \retval 0 -- will send stat-ahead request * \retval others -- prepare stat-ahead request failed */ -static int do_sa_revalidate(struct dentry *dentry) +static int do_sa_revalidate(struct inode *dir, struct dentry *dentry) { struct inode *inode = dentry->d_inode; - struct inode *dir = dentry->d_parent->d_inode; struct lookup_intent it = { .it_op = IT_GETATTR }; struct md_enqueue_info *minfo; struct ldlm_enqueue_info *einfo; @@ -631,16 +713,17 @@ static int do_sa_revalidate(struct dentry *dentry) int rc; ENTRY; - if (inode == NULL) + if (unlikely(inode == NULL)) RETURN(1); if (d_mountpoint(dentry)) RETURN(1); - if (dentry == dentry->d_sb->s_root) + if (unlikely(dentry == dentry->d_sb->s_root)) RETURN(1); - rc = md_revalidate_lock(ll_i2mdexp(dir), &it, ll_inode2fid(inode)); + rc = md_revalidate_lock(ll_i2mdexp(dir), &it, ll_inode2fid(inode), + NULL); if (rc == 1) { ll_intent_release(&it); RETURN(1); @@ -680,11 +763,7 @@ static int ll_statahead_one(struct dentry *parent, const char* entry_name, int rc; ENTRY; -#ifdef DCACHE_LUSTRE_INVALID if (parent->d_flags & DCACHE_LUSTRE_INVALID) { -#else - if (d_unhashed(parent)) { -#endif CDEBUG(D_READA, "parent dentry@%p %.*s is " "invalid, skip statahead\n", parent, parent->d_name.len, parent->d_name.name); @@ -698,16 +777,25 @@ static int ll_statahead_one(struct dentry *parent, const char* entry_name, ll_name2qstr(&name, entry_name, entry_name_len); dentry = d_lookup(parent, &name); if (!dentry) { + if (unlikely(sa_skip_nolock(sai))) { + CWARN("can not obtain lookup lock, skip the succeedent " + "lookup cases, will cause statahead miss, and " + "statahead maybe exit for that.\n"); + GOTO(out, rc = -EAGAIN); + } + dentry = d_alloc(parent, &name); if (dentry) { - rc = do_sa_lookup(dir, dentry); + rc = ll_dops_init(dentry, 1); + if (!rc) + rc = do_sa_lookup(dir, dentry); if (rc) dput(dentry); } else { GOTO(out, rc = -ENOMEM); } } else { - rc = do_sa_revalidate(dentry); + rc = do_sa_revalidate(dir, dentry); if (rc) dput(dentry); } @@ -718,7 +806,7 @@ out: if (rc) { CDEBUG(D_READA, "set sai entry %p index %u stat %d rc %d\n", se, se->se_index, se->se_stat, rc); - se->se_stat = rc; + se->se_stat = rc < 0 ? rc : SA_ENTRY_STATED; if (ll_sai_entry_to_stated(sai, se)) cfs_waitq_signal(&sai->sai_waitq); } else { @@ -729,15 +817,9 @@ out: return rc; } -struct ll_sa_thread_args { - struct dentry *sta_parent; - pid_t sta_pid; -}; - static int ll_statahead_thread(void *arg) { - struct ll_sa_thread_args *sta = arg; - struct dentry *parent = dget(sta->sta_parent); + struct dentry *parent = (struct dentry *)arg; struct inode *dir = parent->d_inode; struct ll_inode_info *lli = ll_i2info(dir); struct ll_sb_info *sbi = ll_i2sbi(dir); @@ -752,14 +834,14 @@ static int ll_statahead_thread(void *arg) { char pname[16]; - snprintf(pname, 15, "ll_sa_%u", sta->sta_pid); + snprintf(pname, 15, "ll_sa_%u", lli->lli_opendir_pid); cfs_daemonize(pname); } - sbi->ll_sa_total++; - spin_lock(&lli->lli_lock); + atomic_inc(&sbi->ll_sa_total); + cfs_spin_lock(&lli->lli_sa_lock); thread->t_flags = SVC_RUNNING; - spin_unlock(&lli->lli_lock); + cfs_spin_unlock(&lli->lli_sa_lock); cfs_waitq_signal(&thread->t_ctl_waitq); CDEBUG(D_READA, "start doing statahead for %s\n", parent->d_name.name); @@ -773,9 +855,10 @@ static int ll_statahead_thread(void *arg) if (IS_ERR(page)) { rc = PTR_ERR(page); - CERROR("error reading dir "DFID" at "LPU64"/%u: rc %d\n", - PFID(ll_inode2fid(dir)), pos, - sai->sai_index, rc); + CDEBUG(D_READA, "error reading dir "DFID" at "LPU64 + "/%u: [rc %d] [parent %u]\n", + PFID(ll_inode2fid(dir)), pos, sai->sai_index, + rc, lli->lli_opendir_pid); break; } @@ -785,7 +868,7 @@ static int ll_statahead_thread(void *arg) char *name = ent->lde_name; int namelen = le16_to_cpu(ent->lde_namelen); - if (namelen == 0) + if (unlikely(namelen == 0)) /* * Skip dummy record. */ @@ -879,9 +962,9 @@ keep_de: out: ll_dir_chain_fini(&chain); - spin_lock(&lli->lli_lock); + cfs_spin_lock(&lli->lli_sa_lock); thread->t_flags = SVC_STOPPED; - spin_unlock(&lli->lli_lock); + cfs_spin_unlock(&lli->lli_sa_lock); cfs_waitq_signal(&sai->sai_waitq); cfs_waitq_signal(&thread->t_ctl_waitq); ll_sai_put(sai); @@ -901,9 +984,9 @@ void ll_stop_statahead(struct inode *inode, void *key) if (unlikely(key == NULL)) return; - spin_lock(&lli->lli_lock); + cfs_spin_lock(&lli->lli_sa_lock); if (lli->lli_opendir_key != key || lli->lli_opendir_pid == 0) { - spin_unlock(&lli->lli_lock); + cfs_spin_unlock(&lli->lli_sa_lock); return; } @@ -915,7 +998,7 @@ void ll_stop_statahead(struct inode *inode, void *key) if (!sa_is_stopped(lli->lli_sai)) { thread->t_flags = SVC_STOPPING; - spin_unlock(&lli->lli_lock); + cfs_spin_unlock(&lli->lli_sa_lock); cfs_waitq_signal(&thread->t_ctl_waitq); CDEBUG(D_READA, "stopping statahead thread, pid %d\n", @@ -924,7 +1007,7 @@ void ll_stop_statahead(struct inode *inode, void *key) sa_is_stopped(lli->lli_sai), &lwi); } else { - spin_unlock(&lli->lli_lock); + cfs_spin_unlock(&lli->lli_sa_lock); } /* @@ -935,7 +1018,7 @@ void ll_stop_statahead(struct inode *inode, void *key) ll_sai_put(lli->lli_sai); } else { lli->lli_opendir_pid = 0; - spin_unlock(&lli->lli_lock); + cfs_spin_unlock(&lli->lli_sa_lock); } } @@ -949,7 +1032,7 @@ enum { */ LS_FIRST_DE, /** - * the first hidden dirent, that is ".xxx + * the first hidden dirent, that is "." */ LS_FIRST_DOT_DE }; @@ -972,9 +1055,13 @@ static int is_first_dirent(struct inode *dir, struct dentry *dentry) struct lu_dirent *ent; if (IS_ERR(page)) { + struct ll_inode_info *lli = ll_i2info(dir); + rc = PTR_ERR(page); - CERROR("error reading dir "DFID" at "LPU64": rc %d\n", - PFID(ll_inode2fid(dir)), pos, rc); + CERROR("error reading dir "DFID" at "LPU64": " + "[rc %d] [parent %u]\n", + PFID(ll_inode2fid(dir)), pos, + rc, lli->lli_opendir_pid); break; } @@ -1014,11 +1101,14 @@ static int is_first_dirent(struct inode *dir, struct dentry *dentry) continue; } - if (target->len == namelen && - memcmp(target->name, name, namelen) == 0) - rc = LS_FIRST_DE + dot_de; - else + if (target->len != namelen || + memcmp(target->name, name, namelen) != 0) rc = LS_NONE_FIRST_DE; + else if (!dot_de) + rc = LS_FIRST_DE; + else + rc = LS_FIRST_DOT_DE; + ll_put_page(page); GOTO(out, rc); } @@ -1031,7 +1121,7 @@ static int is_first_dirent(struct inode *dir, struct dentry *dentry) break; } else if (1) { /* - * chain is exhausted + * chain is exhausted * Normal case: continue to the next page. */ page = ll_get_dir_page(dir, pos, 1, &chain); @@ -1048,30 +1138,70 @@ out: return rc; } +/* + * tgt: the dentry to be revalidate or lookup + * new: the dentry created by statahead + */ +static int is_same_dentry(struct dentry *tgt, struct dentry *new, int lookup) +{ + if (tgt == new) { + LASSERT(lookup == 0); + return 1; + } + if (tgt->d_parent != new->d_parent) + return 0; + if (tgt->d_name.hash != new->d_name.hash) + return 0; + if (tgt->d_name.len != new->d_name.len) + return 0; + if (memcmp(tgt->d_name.name, new->d_name.name, tgt->d_name.len) != 0) + return 0; + if (tgt->d_inode == NULL && lookup) + return 1; + if (tgt->d_inode) + LASSERTF(tgt->d_flags & DCACHE_LUSTRE_INVALID, + "[%.*s/%.*s] [%x %p "DFID"] [%x %p "DFID"]\n", + tgt->d_parent->d_name.len, tgt->d_parent->d_name.name, + tgt->d_name.len, tgt->d_name.name, + tgt->d_flags, tgt, PFID(ll_inode2fid(tgt->d_inode)), + new->d_flags, new, PFID(ll_inode2fid(new->d_inode))); + else + LASSERTF(tgt->d_flags & DCACHE_LUSTRE_INVALID, + "[%.*s/%.*s] [%x %p 0] [%x %p "DFID"]\n", + tgt->d_parent->d_name.len, tgt->d_parent->d_name.name, + tgt->d_name.len, tgt->d_name.name, + tgt->d_flags, tgt, + new->d_flags, new, PFID(ll_inode2fid(new->d_inode))); + return 0; +} + /** * Start statahead thread if this is the first dir entry. * Otherwise if a thread is started already, wait it until it is ahead of me. - * \retval 0 -- stat ahead thread process such dentry, for lookup, it miss - * \retval 1 -- stat ahead thread process such dentry, for lookup, it hit + * \retval 0 -- stat ahead thread process such dentry, miss for lookup + * \retval 1 -- stat ahead thread process such dentry, hit for any case * \retval -EEXIST -- stat ahead thread started, and this is the first dentry * \retval -EBADFD -- statahead thread exit and not dentry available + * \retval -EAGAIN -- try to stat by caller * \retval others -- error */ int do_statahead_enter(struct inode *dir, struct dentry **dentryp, int lookup) { - struct ll_sb_info *sbi = ll_i2sbi(dir); - struct ll_inode_info *lli = ll_i2info(dir); - struct ll_statahead_info *sai = lli->lli_sai; - struct ll_sa_thread_args sta; + struct ll_inode_info *lli; + struct ll_statahead_info *sai; + struct dentry *parent; struct l_wait_info lwi = { 0 }; - int rc; + int rc = 0; ENTRY; + LASSERT(dir != NULL); + lli = ll_i2info(dir); LASSERT(lli->lli_opendir_pid == cfs_curproc_pid()); + sai = lli->lli_sai; if (sai) { if (unlikely(sa_is_stopped(sai) && - list_empty(&sai->sai_entries_stated))) + cfs_list_empty(&sai->sai_entries_stated))) RETURN(-EBADFD); if ((*dentryp)->d_name.name[0] == '.') { @@ -1101,16 +1231,119 @@ int do_statahead_enter(struct inode *dir, struct dentry **dentryp, int lookup) } } - if (ll_sai_entry_stated(sai)) { - sbi->ll_sa_cached++; - } else { - sbi->ll_sa_blocked++; + if (!ll_sai_entry_stated(sai)) { /* * thread started already, avoid double-stat. */ - l_wait_event(sai->sai_waitq, - ll_sai_entry_stated(sai) || sa_is_stopped(sai), - &lwi); + lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL); + rc = l_wait_event(sai->sai_waitq, + ll_sai_entry_stated(sai) || + sa_is_stopped(sai), + &lwi); + if (unlikely(rc == -EINTR)) + RETURN(rc); + } + + if (ll_sai_entry_stated(sai)) { + struct ll_sai_entry *entry; + + entry = cfs_list_entry(sai->sai_entries_stated.next, + struct ll_sai_entry, se_list); + /* This is for statahead lookup */ + if (entry->se_inode != NULL) { + struct lookup_intent it = {.it_op = IT_LOOKUP}; + struct dentry *dchild = entry->se_dentry; + struct inode *ichild = entry->se_inode; + struct ll_dentry_data *lld = ll_d2d(dchild); + struct ll_inode_info *sei = ll_i2info(ichild); + struct dentry *save = dchild; + int invalid = 0; + __u32 bits = MDS_INODELOCK_LOOKUP | + MDS_INODELOCK_UPDATE; + int found = 0; + + LASSERT(dchild != *dentryp); + + if (!lookup) + mutex_lock(&dir->i_mutex); + + /* + * Make sure dentry is still valid. + * For statahead lookup case, we need both + * LOOKUP lock and UPDATE lock which obtained + * by statahead thread originally. + * + * Consider following racer case: + * 1. statahead thread on client1 get lock with + * both LOOKUK and UPDATE bits for "aaa" + * 2. rename thread on client2 cancel such lock + * from client1, then rename "aaa" to "bbb" + * 3. ls thread on client1 obtain LOOKUP lock + * for "bbb" again + * 4. here the dentry "aaa" created by statahead + * thread should be invalid even related + * LOOKUP lock valid for the same inode + */ + rc = md_revalidate_lock(ll_i2mdexp(dir), &it, + ll_inode2fid(ichild), + &bits); + cfs_spin_lock(&sei->lli_sa_lock); + if (!cfs_list_empty(&lld->lld_sa_alias)) + cfs_list_del_init(&lld->lld_sa_alias); + else + invalid = 1; + cfs_spin_unlock(&sei->lli_sa_lock); + if (rc != 1) + /* Someone has cancelled the original + * lock before the real "revalidate" + * using it. Drop it. */ + goto out_mutex; + + if (invalid) { + /* Someone has cancelled the original + * lock, and reobtained it, the dentry + * maybe invalid anymore, Drop it. */ + ll_intent_drop_lock(&it); + goto out_mutex; + } + + ll_lookup_it_alias(&dchild, ichild, bits); + found = is_same_dentry(*dentryp, dchild, lookup); + ll_lookup_finish_locks(&it, dchild); + if (dchild != save) + dput(save); + ichild = NULL; + +out_mutex: + if (!lookup) + mutex_unlock(&dir->i_mutex); + /* Drop the inode reference count held by + * interpreter. */ + if (ichild != NULL) + iput(ichild); + + entry->se_dentry = NULL; + entry->se_inode = NULL; + if (found) { + if (lookup) { + LASSERT(*dentryp != dchild); + /* VFS will drop the reference + * count for dchild and *dentryp + * by itself. */ + *dentryp = dchild; + } else { + LASSERT(*dentryp == dchild); + /* Drop the dentry reference + * count held by statahead. */ + dput(dchild); + } + RETURN(1); + } else { + /* Drop the dentry reference count held + * by statahead. */ + dput(dchild); + } + } } if (lookup) { @@ -1132,53 +1365,53 @@ int do_statahead_enter(struct inode *dir, struct dentry **dentryp, int lookup) RETURN(0); } - /* - * I am the "lli_opendir_pid" owner, only me can set "lli_sai". - */ - LASSERT(lli->lli_sai == NULL); - + /* I am the "lli_opendir_pid" owner, only me can set "lli_sai". */ rc = is_first_dirent(dir, *dentryp); - if (rc == LS_NONE_FIRST_DE) { - /* - * It is not "ls -{a}l" operation, no need statahead for it. - */ - spin_lock(&lli->lli_lock); - lli->lli_opendir_key = NULL; - lli->lli_opendir_pid = 0; - spin_unlock(&lli->lli_lock); - RETURN(-EBADF); - } + if (rc == LS_NONE_FIRST_DE) + /* It is not "ls -{a}l" operation, no need statahead for it. */ + GOTO(out, rc = -EAGAIN); sai = ll_sai_alloc(); if (sai == NULL) - RETURN(-ENOMEM); + GOTO(out, rc = -ENOMEM); sai->sai_ls_all = (rc == LS_FIRST_DOT_DE); sai->sai_inode = igrab(dir); if (unlikely(sai->sai_inode == NULL)) { - CWARN("Do not start stat ahead on dying inode "DFID" .\n", + CWARN("Do not start stat ahead on dying inode "DFID"\n", PFID(&lli->lli_fid)); OBD_FREE_PTR(sai); - RETURN(-ESTALE); + GOTO(out, rc = -ESTALE); } - LASSERT(sai->sai_inode == (*dentryp)->d_parent->d_inode); - - sta.sta_parent = (*dentryp)->d_parent; - sta.sta_pid = cfs_curproc_pid(); + /* get parent reference count here, and put it in ll_statahead_thread */ + parent = dget((*dentryp)->d_parent); + if (unlikely(sai->sai_inode != parent->d_inode)) { + struct ll_inode_info *nlli = ll_i2info(parent->d_inode); + + CWARN("Race condition, someone changed %.*s just now: " + "old parent "DFID", new parent "DFID"\n", + (*dentryp)->d_name.len, (*dentryp)->d_name.name, + PFID(&lli->lli_fid), PFID(&nlli->lli_fid)); + dput(parent); + iput(sai->sai_inode); + OBD_FREE_PTR(sai); + RETURN(-EAGAIN); + } lli->lli_sai = sai; - rc = cfs_kernel_thread(ll_statahead_thread, &sta, 0); + rc = cfs_kernel_thread(ll_statahead_thread, parent, 0); if (rc < 0) { CERROR("can't start ll_sa thread, rc: %d\n", rc); + dput(parent); lli->lli_opendir_key = NULL; sai->sai_thread.t_flags = SVC_STOPPED; ll_sai_put(sai); LASSERT(lli->lli_sai == NULL); - RETURN(rc); + RETURN(-EAGAIN); } - l_wait_event(sai->sai_thread.t_ctl_waitq, + l_wait_event(sai->sai_thread.t_ctl_waitq, sa_is_running(sai) || sa_is_stopped(sai), &lwi); @@ -1187,55 +1420,69 @@ int do_statahead_enter(struct inode *dir, struct dentry **dentryp, int lookup) * lookup, and -EEXIST also indicates that this is the first dirent. */ RETURN(-EEXIST); + +out: + cfs_spin_lock(&lli->lli_sa_lock); + lli->lli_opendir_key = NULL; + lli->lli_opendir_pid = 0; + cfs_spin_unlock(&lli->lli_sa_lock); + return rc; } /** * update hit/miss count. */ -int ll_statahead_exit(struct dentry *dentry, int result) +void ll_statahead_exit(struct inode *dir, struct dentry *dentry, int result) { - struct dentry *parent = dentry->d_parent; - struct ll_inode_info *lli = ll_i2info(parent->d_inode); - struct ll_sb_info *sbi = ll_i2sbi(parent->d_inode); - int rc = 0; + struct ll_inode_info *lli; + struct ll_statahead_info *sai; + struct ll_sb_info *sbi; + struct ll_dentry_data *ldd = ll_d2d(dentry); + int rc; ENTRY; - if (lli->lli_opendir_pid != cfs_curproc_pid()) - RETURN(-EBADFD); - - if (lli->lli_sai) { - struct ll_statahead_info *sai = lli->lli_sai; - - if (result >= 1) { - sbi->ll_sa_hit++; - sai->sai_hit++; - sai->sai_consecutive_miss = 0; - sai->sai_max = min(2 * sai->sai_max, sbi->ll_sa_max); - } else { - sbi->ll_sa_miss++; - sai->sai_miss++; - sai->sai_consecutive_miss++; - if (sa_low_hit(sai) && sa_is_running(sai)) { - sbi->ll_sa_wrong++; - CDEBUG(D_READA, "statahead for dir %.*s hit " - "ratio too low: hit/miss %u/%u, " - "sent/replied %u/%u. stopping statahead " - "thread: pid %d\n", - parent->d_name.len, parent->d_name.name, - sai->sai_hit, sai->sai_miss, - sai->sai_sent, sai->sai_replied, - cfs_curproc_pid()); - spin_lock(&lli->lli_lock); - if (!sa_is_stopped(sai)) - sai->sai_thread.t_flags = SVC_STOPPING; - spin_unlock(&lli->lli_lock); - } + LASSERT(dir != NULL); + lli = ll_i2info(dir); + LASSERT(lli->lli_opendir_pid == cfs_curproc_pid()); + sai = lli->lli_sai; + LASSERT(sai != NULL); + sbi = ll_i2sbi(dir); + + rc = ll_sai_entry_fini(sai); + /* rc == -ENOENT means such dentry was removed just between statahead + * readdir and pre-fetched, count it as hit. + * + * result == -ENOENT has two meanings: + * 1. such dentry was removed just between statahead pre-fetched and + * main process stat such dentry. + * 2. main process stat non-exist dentry. + * We can not distinguish such two cases, just count them as miss. */ + if (result >= 1 || unlikely(rc == -ENOENT)) { + sai->sai_hit++; + sai->sai_consecutive_miss = 0; + sai->sai_max = min(2 * sai->sai_max, sbi->ll_sa_max); + } else { + sai->sai_miss++; + sai->sai_consecutive_miss++; + if (sa_low_hit(sai) && sa_is_running(sai)) { + atomic_inc(&sbi->ll_sa_wrong); + CDEBUG(D_READA, "Statahead for dir "DFID" hit ratio " + "too low: hit/miss %u/%u, sent/replied %u/%u, " + "stopping statahead thread: pid %d\n", + PFID(&lli->lli_fid), sai->sai_hit, + sai->sai_miss, sai->sai_sent, + sai->sai_replied, cfs_curproc_pid()); + cfs_spin_lock(&lli->lli_sa_lock); + if (!sa_is_stopped(sai)) + sai->sai_thread.t_flags = SVC_STOPPING; + cfs_spin_unlock(&lli->lli_sa_lock); } - - if (!sa_is_stopped(sai)) - cfs_waitq_signal(&sai->sai_thread.t_ctl_waitq); - ll_sai_entry_fini(sai); - rc = ll_statahead_mark(dentry); } - RETURN(rc); + + if (!sa_is_stopped(sai)) + cfs_waitq_signal(&sai->sai_thread.t_ctl_waitq); + if (likely(ldd != NULL)) + ldd->lld_sa_generation = sai->sai_generation; + + EXIT; }