X-Git-Url: https://git.whamcloud.com/?a=blobdiff_plain;f=lustre%2Flfsck%2Flfsck_lib.c;h=9a7980ecac6e8de022a2e2b28a41e272225ebee2;hb=ab2dd91a18eb31d4fac35683eb4ebad7bdfc2499;hp=3b1fe0cd90bb6514f33ee84d32359c828264ba37;hpb=5ab7b4309f8ac9efe5c4455691d1f28b9d42f17d;p=fs%2Flustre-release.git diff --git a/lustre/lfsck/lfsck_lib.c b/lustre/lfsck/lfsck_lib.c index 3b1fe0c..9a7980e 100644 --- a/lustre/lfsck/lfsck_lib.c +++ b/lustre/lfsck/lfsck_lib.c @@ -58,7 +58,7 @@ static void lfsck_key_fini(const struct lu_context *ctx, LU_CONTEXT_KEY_DEFINE(lfsck, LCT_MD_THREAD | LCT_DT_THREAD); LU_KEY_INIT_GENERIC(lfsck); -static CFS_LIST_HEAD(lfsck_instance_list); +static struct list_head lfsck_instance_list; static struct list_head lfsck_ost_orphan_list; static struct list_head lfsck_mdt_orphan_list; static DEFINE_SPINLOCK(lfsck_instance_lock); @@ -94,9 +94,15 @@ const char *lfsck_param_names[] = { "all_targets", "broadcast", "orphan", + "create_ostobj", NULL }; +enum lfsck_verify_lpf_types { + LVLT_BY_BOOKMARK = 0, + LVLT_BY_NAMEENTRY = 1, +}; + const char *lfsck_status2names(enum lfsck_status status) { if (unlikely(status < 0 || status >= LS_MAX)) @@ -243,11 +249,9 @@ static int lfsck_add_target_from_orphan(const struct lu_env *env, again: spin_lock(&lfsck_instance_lock); list_for_each_entry_safe(ltd, next, head, ltd_orphan_list) { - if (ltd->ltd_key == lfsck->li_bottom) { - list_del_init(<d->ltd_orphan_list); - list_add_tail(<d->ltd_orphan_list, - <ds->ltd_orphan); - } + if (ltd->ltd_key == lfsck->li_bottom) + list_move_tail(<d->ltd_orphan_list, + <ds->ltd_orphan); } spin_unlock(&lfsck_instance_lock); @@ -278,11 +282,12 @@ again: } static inline struct lfsck_component * -__lfsck_component_find(struct lfsck_instance *lfsck, __u16 type, cfs_list_t *list) +__lfsck_component_find(struct lfsck_instance *lfsck, __u16 type, + struct list_head *list) { struct lfsck_component *com; - cfs_list_for_each_entry(com, list, lc_link) { + list_for_each_entry(com, list, lc_link) { if (com->lc_type == type) return com; } @@ -316,10 +321,10 @@ unlock: void lfsck_component_cleanup(const struct lu_env *env, struct lfsck_component *com) { - if (!cfs_list_empty(&com->lc_link)) - cfs_list_del_init(&com->lc_link); - if (!cfs_list_empty(&com->lc_link_dir)) - cfs_list_del_init(&com->lc_link_dir); + if (!list_empty(&com->lc_link)) + list_del_init(&com->lc_link); + if (!list_empty(&com->lc_link_dir)) + list_del_init(&com->lc_link_dir); lfsck_component_put(env, com); } @@ -350,8 +355,72 @@ int lfsck_fid_alloc(const struct lu_env *env, struct lfsck_instance *lfsck, RETURN(rc); } +/** + * Request the specified ibits lock for the given object. + * + * Before the LFSCK modifying on the namespace visible object, + * it needs to acquire related ibits ldlm lock. + * + * \param[in] env pointer to the thread context + * \param[in] lfsck pointer to the lfsck instance + * \param[in] obj pointer to the dt_object to be locked + * \param[out] lh pointer to the lock handle + * \param[in] ibits the bits for the ldlm lock to be acquired + * \param[in] mode the mode for the ldlm lock to be acquired + * + * \retval 0 for success + * \retval negative error number on failure + */ +int lfsck_ibits_lock(const struct lu_env *env, struct lfsck_instance *lfsck, + struct dt_object *obj, struct lustre_handle *lh, + __u64 bits, ldlm_mode_t mode) +{ + struct lfsck_thread_info *info = lfsck_env_info(env); + ldlm_policy_data_t *policy = &info->lti_policy; + struct ldlm_res_id *resid = &info->lti_resid; + __u64 flags = LDLM_FL_ATOMIC_CB; + int rc; + + LASSERT(lfsck->li_namespace != NULL); + + memset(policy, 0, sizeof(*policy)); + policy->l_inodebits.bits = bits; + fid_build_reg_res_name(lfsck_dto2fid(obj), resid); + rc = ldlm_cli_enqueue_local(lfsck->li_namespace, resid, LDLM_IBITS, + policy, mode, &flags, ldlm_blocking_ast, + ldlm_completion_ast, NULL, NULL, 0, + LVB_T_NONE, NULL, lh); + if (rc == ELDLM_OK) { + rc = 0; + } else { + memset(lh, 0, sizeof(*lh)); + rc = -EIO; + } + + return rc; +} + +/** + * Release the the specified ibits lock. + * + * If the lock has been acquired before, release it + * and cleanup the handle. Otherwise, do nothing. + * + * \param[in] lh pointer to the lock handle + * \param[in] mode the mode for the ldlm lock to be released + */ +void lfsck_ibits_unlock(struct lustre_handle *lh, ldlm_mode_t mode) +{ + if (lustre_handle_is_used(lh)) { + ldlm_lock_decref(lh, mode); + memset(lh, 0, sizeof(*lh)); + } +} + static const char dot[] = "."; static const char dotdot[] = ".."; +static const char dotlustre[] = ".lustre"; +static const char lostfound[] = "lost+found"; static int lfsck_create_lpf_local(const struct lu_env *env, struct lfsck_instance *lfsck, @@ -361,16 +430,30 @@ static int lfsck_create_lpf_local(const struct lu_env *env, struct dt_object_format *dof, const char *name) { + struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec; struct dt_device *dev = lfsck->li_bottom; struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram; struct dt_object *bk_obj = lfsck->li_bookmark_obj; - const struct lu_fid *cfid = lu_object_fid(&child->do_lu); + const struct lu_fid *cfid = lfsck_dto2fid(child); struct thandle *th = NULL; + struct linkea_data ldata = { 0 }; + struct lu_buf linkea_buf; + const struct lu_name *cname; loff_t pos = 0; int len = sizeof(struct lfsck_bookmark); - int rc = 0; + int rc; ENTRY; + rc = linkea_data_new(&ldata, + &lfsck_env_info(env)->lti_linkea_buf); + if (rc != 0) + RETURN(rc); + + cname = lfsck_name_get_const(env, name, strlen(name)); + rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent)); + if (rc != 0) + RETURN(rc); + th = dt_trans_create(env, dev); if (IS_ERR(th)) RETURN(PTR_ERR(th)); @@ -385,18 +468,28 @@ static int lfsck_create_lpf_local(const struct lu_env *env, if (rc != 0) GOTO(stop, rc); - /* 3a. insert name into parent dir */ - rc = dt_declare_insert(env, parent, (const struct dt_rec *)cfid, + /* 3a. insert linkEA for child */ + lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf, + ldata.ld_leh->leh_len); + rc = dt_declare_xattr_set(env, child, &linkea_buf, + XATTR_NAME_LINK, 0, th); + if (rc != 0) + GOTO(stop, rc); + + /* 4a. insert name into parent dir */ + rec->rec_type = S_IFDIR; + rec->rec_fid = cfid; + rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec, (const struct dt_key *)name, th); if (rc != 0) GOTO(stop, rc); - /* 4a. increase parent nlink */ + /* 5a. increase parent nlink */ rc = dt_declare_ref_add(env, parent, th); if (rc != 0) GOTO(stop, rc); - /* 5a. update bookmark */ + /* 6a. update bookmark */ rc = dt_declare_record_write(env, bk_obj, lfsck_buf_get(env, bk, len), 0, th); if (rc != 0) @@ -407,7 +500,7 @@ static int lfsck_create_lpf_local(const struct lu_env *env, GOTO(stop, rc); dt_write_lock(env, child, 0); - /* 1b.1 create child */ + /* 1b.1. create child */ rc = dt_create(env, child, la, NULL, dof, th); if (rc != 0) GOTO(unlock, rc); @@ -415,32 +508,41 @@ static int lfsck_create_lpf_local(const struct lu_env *env, if (unlikely(!dt_try_as_dir(env, child))) GOTO(unlock, rc = -ENOTDIR); - /* 1b.2 insert dot into child dir */ - rc = dt_insert(env, child, (const struct dt_rec *)cfid, + /* 1b.2. insert dot into child dir */ + rec->rec_fid = cfid; + rc = dt_insert(env, child, (const struct dt_rec *)rec, (const struct dt_key *)dot, th, BYPASS_CAPA, 1); if (rc != 0) GOTO(unlock, rc); - /* 1b.3 insert dotdot into child dir */ - rc = dt_insert(env, child, (const struct dt_rec *)&LU_LPF_FID, + /* 1b.3. insert dotdot into child dir */ + rec->rec_fid = &LU_LPF_FID; + rc = dt_insert(env, child, (const struct dt_rec *)rec, (const struct dt_key *)dotdot, th, BYPASS_CAPA, 1); if (rc != 0) GOTO(unlock, rc); /* 2b. increase child nlink */ rc = dt_ref_add(env, child, th); + if (rc != 0) + GOTO(unlock, rc); + + /* 3b. insert linkEA for child. */ + rc = dt_xattr_set(env, child, &linkea_buf, + XATTR_NAME_LINK, 0, th, BYPASS_CAPA); dt_write_unlock(env, child); if (rc != 0) GOTO(stop, rc); - /* 3b. insert name into parent dir */ - rc = dt_insert(env, parent, (const struct dt_rec *)cfid, + /* 4b. insert name into parent dir */ + rec->rec_fid = cfid; + rc = dt_insert(env, parent, (const struct dt_rec *)rec, (const struct dt_key *)name, th, BYPASS_CAPA, 1); if (rc != 0) GOTO(stop, rc); dt_write_lock(env, parent, 0); - /* 4b. increase parent nlink */ + /* 5b. increase parent nlink */ rc = dt_ref_add(env, parent, th); dt_write_unlock(env, parent); if (rc != 0) @@ -449,7 +551,7 @@ static int lfsck_create_lpf_local(const struct lu_env *env, bk->lb_lpf_fid = *cfid; lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk); - /* 5b. update bookmark */ + /* 6b. update bookmark */ rc = dt_record_write(env, bk_obj, lfsck_buf_get(env, bk, len), &pos, th); @@ -472,16 +574,30 @@ static int lfsck_create_lpf_remote(const struct lu_env *env, struct dt_object_format *dof, const char *name) { + struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec; struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram; struct dt_object *bk_obj = lfsck->li_bookmark_obj; - const struct lu_fid *cfid = lu_object_fid(&child->do_lu); + const struct lu_fid *cfid = lfsck_dto2fid(child); struct thandle *th = NULL; + struct linkea_data ldata = { 0 }; + struct lu_buf linkea_buf; + const struct lu_name *cname; struct dt_device *dev; loff_t pos = 0; int len = sizeof(struct lfsck_bookmark); - int rc = 0; + int rc; ENTRY; + rc = linkea_data_new(&ldata, + &lfsck_env_info(env)->lti_linkea_buf); + if (rc != 0) + RETURN(rc); + + cname = lfsck_name_get_const(env, name, strlen(name)); + rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent)); + if (rc != 0) + RETURN(rc); + /* Create .lustre/lost+found/MDTxxxx. */ /* XXX: Currently, cross-MDT create operation needs to create the child @@ -494,37 +610,51 @@ static int lfsck_create_lpf_remote(const struct lu_env *env, * To avoid more inconsistency, we split the create operation into * two transactions: * - * 1) create the child locally. + * 1) create the child and update the lfsck_bookmark::lb_lpf_fid + * locally. * 2) insert the name "MDTXXXX" in the parent ".lustre/lost+found" - * remotely and update the lfsck_bookmark::lb_lpf_fid locally. + * remotely. * - * If 1) done but 2) failed, then the worst case is that we lose - * one object locally, which is not a big issue. (can be repaird - * by LFSCK phase III) */ + * If 1) done, but 2) failed, then go ahead, the LFSCK will try to + * repair such inconsistency when LFSCK run next time. */ - /* Transaction I: */ + /* Transaction I: locally */ dev = lfsck->li_bottom; th = dt_trans_create(env, dev); if (IS_ERR(th)) RETURN(PTR_ERR(th)); - /* 1a. create child locally. */ + /* 1a. create child */ rc = dt_declare_create(env, child, la, NULL, dof, th); if (rc != 0) GOTO(stop, rc); - /* 2a. increase child nlink locally. */ + /* 2a. increase child nlink */ rc = dt_declare_ref_add(env, child, th); if (rc != 0) GOTO(stop, rc); + /* 3a. insert linkEA for child */ + lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf, + ldata.ld_leh->leh_len); + rc = dt_declare_xattr_set(env, child, &linkea_buf, + XATTR_NAME_LINK, 0, th); + if (rc != 0) + GOTO(stop, rc); + + /* 4a. update bookmark */ + rc = dt_declare_record_write(env, bk_obj, + lfsck_buf_get(env, bk, len), 0, th); + if (rc != 0) + GOTO(stop, rc); + rc = dt_trans_start_local(env, dev, th); if (rc != 0) GOTO(stop, rc); dt_write_lock(env, child, 0); - /* 1b. create child locally. */ + /* 1b.1. create child */ rc = dt_create(env, child, la, NULL, dof, th); if (rc != 0) GOTO(unlock, rc); @@ -532,95 +662,77 @@ static int lfsck_create_lpf_remote(const struct lu_env *env, if (unlikely(!dt_try_as_dir(env, child))) GOTO(unlock, rc = -ENOTDIR); - /* 2b.1 insert dot into child dir locally. */ - rc = dt_insert(env, child, (const struct dt_rec *)cfid, + /* 1b.2. insert dot into child dir */ + rec->rec_type = S_IFDIR; + rec->rec_fid = cfid; + rc = dt_insert(env, child, (const struct dt_rec *)rec, (const struct dt_key *)dot, th, BYPASS_CAPA, 1); if (rc != 0) GOTO(unlock, rc); - /* 2b.2 insert dotdot into child dir locally. */ - rc = dt_insert(env, child, (const struct dt_rec *)&LU_LPF_FID, + /* 1b.3. insert dotdot into child dir */ + rec->rec_fid = &LU_LPF_FID; + rc = dt_insert(env, child, (const struct dt_rec *)rec, (const struct dt_key *)dotdot, th, BYPASS_CAPA, 1); if (rc != 0) GOTO(unlock, rc); - /* 2b.3 increase child nlink locally. */ + /* 2b. increase child nlink */ rc = dt_ref_add(env, child, th); + if (rc != 0) + GOTO(unlock, rc); + + /* 3b. insert linkEA for child */ + rc = dt_xattr_set(env, child, &linkea_buf, + XATTR_NAME_LINK, 0, th, BYPASS_CAPA); + if (rc != 0) + GOTO(unlock, rc); + + bk->lb_lpf_fid = *cfid; + lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk); + + /* 4b. update bookmark */ + rc = dt_record_write(env, bk_obj, + lfsck_buf_get(env, bk, len), &pos, th); + dt_write_unlock(env, child); dt_trans_stop(env, dev, th); if (rc != 0) RETURN(rc); - /* Transaction II: */ + /* Transaction II: remotely */ dev = lfsck->li_next; th = dt_trans_create(env, dev); if (IS_ERR(th)) RETURN(PTR_ERR(th)); - /* 3a. insert name into parent dir remotely. */ - rc = dt_declare_insert(env, parent, (const struct dt_rec *)cfid, + /* 5a. insert name into parent dir */ + rec->rec_fid = cfid; + rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec, (const struct dt_key *)name, th); if (rc != 0) GOTO(stop, rc); - /* 4a. increase parent nlink remotely. */ + /* 6a. increase parent nlink */ rc = dt_declare_ref_add(env, parent, th); if (rc != 0) GOTO(stop, rc); - /* 5a. decrease child nlink for dotdot locally if former remote - * update failed. */ - rc = dt_declare_ref_del(env, child, th); - if (rc != 0) - GOTO(stop, rc); - - /* 6a. decrease child nlink for dot locally if former remote - * update failed. */ - rc = dt_declare_ref_del(env, child, th); - if (rc != 0) - GOTO(stop, rc); - - /* 7a. destroy child locally if former remote update failed. */ - rc = dt_declare_destroy(env, child, th); - if (rc != 0) - GOTO(stop, rc); - - /* 8a. update bookmark locally. */ - rc = dt_declare_record_write(env, bk_obj, - lfsck_buf_get(env, bk, len), 0, th); - if (rc != 0) - GOTO(stop, rc); - rc = dt_trans_start(env, dev, th); if (rc != 0) GOTO(stop, rc); - /* 3b. insert name into parent dir remotely. */ - rc = dt_insert(env, parent, (const struct dt_rec *)cfid, + /* 5b. insert name into parent dir */ + rc = dt_insert(env, parent, (const struct dt_rec *)rec, (const struct dt_key *)name, th, BYPASS_CAPA, 1); - if (rc == 0) { - dt_write_lock(env, parent, 0); - /* 4b. increase parent nlink remotely. */ - rc = dt_ref_add(env, parent, th); - dt_write_unlock(env, parent); - } - if (rc != 0) { - /* 5b. decrease child nlink for dotdot locally. */ - dt_ref_del(env, child, th); - /* 6b. decrease child nlink for dot locally. */ - dt_ref_del(env, child, th); - /* 7b. destroy child locally. */ - dt_destroy(env, child, th); + if (rc != 0) GOTO(stop, rc); - } - - bk->lb_lpf_fid = *cfid; - lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk); - /* 8b. update bookmark locally. */ - rc = dt_record_write(env, bk_obj, - lfsck_buf_get(env, bk, len), &pos, th); + dt_write_lock(env, parent, 0); + /* 6b. increase parent nlink */ + rc = dt_ref_add(env, parent, th); + dt_write_unlock(env, parent); GOTO(stop, rc); @@ -629,6 +741,13 @@ unlock: stop: dt_trans_stop(env, dev, th); + if (rc != 0 && dev == lfsck->li_next) + CDEBUG(D_LFSCK, "%s: partially created the object "DFID + "for orphans, but failed to insert the name %s " + "to the .lustre/lost+found/. Such inconsistency " + "will be repaired when LFSCK run next time: rc = %d\n", + lfsck_lfsck2name(lfsck), PFID(cfid), name, rc); + return rc; } @@ -644,6 +763,7 @@ int lfsck_create_lpf(const struct lu_env *env, struct lfsck_instance *lfsck) struct dt_object_format *dof = &info->lti_dof; struct dt_object *parent = NULL; struct dt_object *child = NULL; + struct lustre_handle lh = { 0 }; char name[8]; int node = lfsck_dev_idx(lfsck->li_bottom); int rc = 0; @@ -669,9 +789,17 @@ int lfsck_create_lpf(const struct lu_env *env, struct lfsck_instance *lfsck) if (IS_ERR(parent)) RETURN(PTR_ERR(parent)); + if (lfsck->li_lpf_obj != NULL) + GOTO(out, rc = 0); + if (unlikely(!dt_try_as_dir(env, parent))) GOTO(out, rc = -ENOTDIR); + rc = lfsck_ibits_lock(env, lfsck, parent, &lh, + MDS_INODELOCK_UPDATE, LCK_EX); + if (rc != 0) + GOTO(out, rc); + mutex_lock(&lfsck->li_mutex); if (lfsck->li_lpf_obj != NULL) GOTO(unlock, rc = 0); @@ -704,10 +832,11 @@ int lfsck_create_lpf(const struct lu_env *env, struct lfsck_instance *lfsck) if (dt_object_exists(child) != 0) { if (unlikely(!dt_try_as_dir(env, child))) - GOTO(unlock, rc = -ENOTDIR); + rc = -ENOTDIR; + else + lfsck->li_lpf_obj = child; - lfsck->li_lpf_obj = child; - GOTO(unlock, rc = 0); + GOTO(unlock, rc); } memset(la, 0, sizeof(*la)); @@ -731,6 +860,7 @@ int lfsck_create_lpf(const struct lu_env *env, struct lfsck_instance *lfsck) unlock: mutex_unlock(&lfsck->li_mutex); + lfsck_ibits_unlock(&lh, LCK_EX); if (rc != 0 && child != NULL && !IS_ERR(child)) lu_object_put(env, &child->do_lu); out: @@ -740,6 +870,484 @@ out: return rc; } +/** + * Scan .lustre/lost+found for bad name entries and remove them. + * + * The valid name entry should be "MDTxxxx", the "xxxx" is the MDT device + * index in the system. Any other formatted name is invalid and should be + * removed. + * + * \param[in] env pointer to the thread context + * \param[in] lfsck pointer to the lfsck instance + * \param[in] parent pointer to the lost+found object + * + * \retval 0 for success + * \retval negative error number on failure + */ +static int lfsck_scan_lpf_bad_entries(const struct lu_env *env, + struct lfsck_instance *lfsck, + struct dt_object *parent) +{ + struct lu_dirent *ent = + (struct lu_dirent *)lfsck_env_info(env)->lti_key; + const struct dt_it_ops *iops = &parent->do_index_ops->dio_it; + struct dt_it *it; + int rc; + ENTRY; + + it = iops->init(env, parent, LUDA_64BITHASH, BYPASS_CAPA); + if (IS_ERR(it)) + RETURN(PTR_ERR(it)); + + rc = iops->load(env, it, 0); + if (rc == 0) + rc = iops->next(env, it); + else if (rc > 0) + rc = 0; + + while (rc == 0) { + int off = 3; + + rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH); + if (rc != 0) + break; + + ent->lde_namelen = le16_to_cpu(ent->lde_namelen); + if (ent->lde_name[0] == '.') { + if (ent->lde_namelen == 1) + goto next; + + if (ent->lde_namelen == 2 && ent->lde_name[1] == '.') + goto next; + } + + /* name length must be strlen("MDTxxxx") */ + if (ent->lde_namelen != 7) + goto remove; + + if (memcmp(ent->lde_name, "MDT", off) != 0) + goto remove; + + while (off < 7 && isxdigit(ent->lde_name[off])) + off++; + + if (off != 7) { + +remove: + rc = lfsck_remove_name_entry(env, lfsck, parent, + ent->lde_name, S_IFDIR); + if (rc != 0) + break; + } + +next: + rc = iops->next(env, it); + } + + iops->put(env, it); + iops->fini(env, it); + + RETURN(rc > 0 ? 0 : rc); +} + +static int lfsck_update_lpf_entry(const struct lu_env *env, + struct lfsck_instance *lfsck, + struct dt_object *parent, + struct dt_object *child, + const char *name, + enum lfsck_verify_lpf_types type) +{ + int rc; + + if (type == LVLT_BY_BOOKMARK) { + rc = lfsck_update_name_entry(env, lfsck, parent, name, + lfsck_dto2fid(child), S_IFDIR); + } else /* if (type == LVLT_BY_NAMEENTRY) */ { + lfsck->li_bookmark_ram.lb_lpf_fid = *lfsck_dto2fid(child); + rc = lfsck_bookmark_store(env, lfsck); + + CDEBUG(D_LFSCK, "%s: update LPF fid "DFID + " in the bookmark file: rc = %d\n", + lfsck_lfsck2name(lfsck), + PFID(lfsck_dto2fid(child)), rc); + } + + return rc; +} + +/** + * Check whether the @child back references the @parent. + * + * Two cases: + * 1) The child's FID is stored in the bookmark file. If the child back + * references the parent (LU_LPF_FID object) via its ".." entry, then + * insert the name (MDTxxxx) to the .lustre/lost+found; otherwise, if + * the child back references another parent2, then: + * 1.1) If the parent2 recognizes the child, then update the bookmark file; + * 1.2) Otherwise, the LFSCK cannot know whether there will be parent3 that + * references the child. So keep them there. As the LFSCK processing, + * the parent3 may be found, then when the LFSCK run next time, the + * inconsistency can be repaired. + * + * 2) The child's FID is stored in the .lustre/lost+found/ sub-directory name + * entry (MDTxxxx). If the child back references the parent (LU_LPF_FID obj) + * via its ".." entry, then update the bookmark file, otherwise, if the child + * back references another parent2, then: + * 2.1) If the parent2 recognizes the child, then remove the sub-directory + * from .lustre/lost+found/; + * 2.2) Otherwise, if the parent2 does not recognizes the child, trust the + * sub-directory name entry and update the child; + * 2.3) Otherwise, if we do not know whether the parent2 recognizes the child + * or not, then keep them there. + * + * \param[in] env pointer to the thread context + * \param[in] lfsck pointer to the lfsck instance + * \param[in] parent pointer to the lost+found object + * \param[in] child pointer to the lost+found sub-directory object + * \param[in] name the name for lost+found sub-directory object + * \param[out] fid pointer to the buffer to hold the FID of the object + * (called it as parent2) that is referenced via the + * child's dotdot entry; it also can be the FID that + * is referenced by the name entry under the parent2. + * \param[in] type to indicate where the child's FID is stored in + * + * \retval positive number for uncertain inconsistency + * \retval 0 for success + * \retval negative error number on failure + */ +static int lfsck_verify_lpf_pairs(const struct lu_env *env, + struct lfsck_instance *lfsck, + struct dt_object *parent, + struct dt_object *child, const char *name, + struct lu_fid *fid, + enum lfsck_verify_lpf_types type) +{ + struct lfsck_thread_info *info = lfsck_env_info(env); + char *name2 = info->lti_key; + struct lu_fid *fid2 = &info->lti_fid3; + struct dt_object *parent2 = NULL; + struct lustre_handle lh = { 0 }; + int rc; + ENTRY; + + fid_zero(fid); + rc = dt_lookup(env, child, (struct dt_rec *)fid, + (const struct dt_key *)dotdot, BYPASS_CAPA); + if (rc != 0) + GOTO(linkea, rc); + + if (!fid_is_sane(fid)) + GOTO(linkea, rc = -EINVAL); + + if (lu_fid_eq(fid, &LU_LPF_FID)) { + const struct lu_name *cname; + + if (lfsck->li_lpf_obj == NULL) { + lu_object_get(&child->do_lu); + lfsck->li_lpf_obj = child; + } + + cname = lfsck_name_get_const(env, name, strlen(name)); + rc = lfsck_verify_linkea(env, lfsck->li_bottom, child, cname, + &LU_LPF_FID); + if (rc == 0) + rc = lfsck_update_lpf_entry(env, lfsck, parent, child, + name, type); + + GOTO(out_done, rc); + } + + parent2 = lfsck_object_find_by_dev(env, lfsck->li_next, fid); + if (IS_ERR(parent2)) + GOTO(linkea, parent2); + + if (!dt_object_exists(parent2)) { + lu_object_put(env, &parent2->do_lu); + + GOTO(linkea, parent2 = ERR_PTR(-ENOENT)); + } + + if (!dt_try_as_dir(env, parent2)) { + lu_object_put(env, &parent2->do_lu); + + GOTO(linkea, parent2 = ERR_PTR(-ENOTDIR)); + } + +linkea: + /* To prevent rename/unlink race */ + rc = lfsck_ibits_lock(env, lfsck, child, &lh, + MDS_INODELOCK_UPDATE, LCK_PR); + if (rc != 0) + GOTO(out_put, rc); + + dt_read_lock(env, child, 0); + rc = lfsck_links_get_first(env, child, name2, fid2); + if (rc != 0) { + dt_read_unlock(env, child); + lfsck_ibits_unlock(&lh, LCK_PR); + + GOTO(out_put, rc = 1); + } + + /* It is almost impossible that the bookmark file (or the name entry) + * and the linkEA hit the same data corruption. Trust the linkEA. */ + if (lu_fid_eq(fid2, &LU_LPF_FID) && strcmp(name, name2) == 0) { + dt_read_unlock(env, child); + lfsck_ibits_unlock(&lh, LCK_PR); + + *fid = *fid2; + if (lfsck->li_lpf_obj == NULL) { + lu_object_get(&child->do_lu); + lfsck->li_lpf_obj = child; + } + + /* Update the child's dotdot entry */ + rc = lfsck_update_name_entry(env, lfsck, child, dotdot, + &LU_LPF_FID, S_IFDIR); + if (rc == 0) + rc = lfsck_update_lpf_entry(env, lfsck, parent, child, + name, type); + + GOTO(out_put, rc); + } + + if (parent2 == NULL || IS_ERR(parent2)) { + dt_read_unlock(env, child); + lfsck_ibits_unlock(&lh, LCK_PR); + + GOTO(out_done, rc = 1); + } + + rc = dt_lookup(env, parent2, (struct dt_rec *)fid, + (const struct dt_key *)name2, BYPASS_CAPA); + dt_read_unlock(env, child); + lfsck_ibits_unlock(&lh, LCK_PR); + if (rc != 0 && rc != -ENOENT) + GOTO(out_put, rc); + + if (rc == -ENOENT || !lu_fid_eq(fid, lfsck_dto2fid(child))) { + if (type == LVLT_BY_BOOKMARK) + GOTO(out_put, rc = 1); + + /* Trust the name entry, update the child's dotdot entry. */ + rc = lfsck_update_name_entry(env, lfsck, child, dotdot, + &LU_LPF_FID, S_IFDIR); + + GOTO(out_put, rc); + } + + if (type == LVLT_BY_BOOKMARK) { + /* Invalid FID record in the bookmark file, reset it. */ + fid_zero(&lfsck->li_bookmark_ram.lb_lpf_fid); + rc = lfsck_bookmark_store(env, lfsck); + + CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID + " in the bookmark file: rc = %d\n", + lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(child)), rc); + } else /* if (type == LVLT_BY_NAMEENTRY) */ { + /* The name entry is wrong, remove it. */ + rc = lfsck_remove_name_entry(env, lfsck, parent, name, S_IFDIR); + } + + GOTO(out_put, rc); + +out_put: + if (parent2 != NULL && !IS_ERR(parent2)) + lu_object_put(env, &parent2->do_lu); + +out_done: + return rc; +} + +/** + * Verify the /ROOT/.lustre/lost+found/ directory. + * + * /ROOT/.lustre/lost+found/ is a special directory to hold the objects that + * the LFSCK does not exactly know how to handle, such as orphans. So before + * the LFSCK scanning the system, the consistency of such directory needs to + * be verified firstly to allow the users to use it during the LFSCK. + * + * \param[in] env pointer to the thread context + * \param[in] lfsck pointer to the lfsck instance + * + * \retval positive number for uncertain inconsistency + * \retval 0 for success + * \retval negative error number on failure + */ +int lfsck_verify_lpf(const struct lu_env *env, struct lfsck_instance *lfsck) +{ + struct lfsck_thread_info *info = lfsck_env_info(env); + struct lu_fid *pfid = &info->lti_fid; + struct lu_fid *cfid = &info->lti_fid2; + struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram; + struct dt_object *parent = NULL; + /* child1's FID is in the bookmark file. */ + struct dt_object *child1 = NULL; + /* child2's FID is in the name entry MDTxxxx. */ + struct dt_object *child2 = NULL; + struct dt_device *dev = lfsck->li_bottom; + const struct lu_name *cname; + char name[8]; + int node = lfsck_dev_idx(dev); + int rc = 0; + ENTRY; + + LASSERT(lfsck->li_master); + + if (node == 0) { + parent = lfsck_object_find_by_dev(env, dev, &LU_LPF_FID); + } else { + struct lfsck_tgt_desc *ltd; + + ltd = lfsck_tgt_get(&lfsck->li_mdt_descs, 0); + if (unlikely(ltd == NULL)) + RETURN(-ENXIO); + + parent = lfsck_object_find_by_dev(env, ltd->ltd_tgt, + &LU_LPF_FID); + lfsck_tgt_put(ltd); + } + + if (IS_ERR(parent)) + RETURN(PTR_ERR(parent)); + + LASSERT(dt_object_exists(parent)); + + if (unlikely(!dt_try_as_dir(env, parent))) + GOTO(put, rc = -ENOTDIR); + + if (node == 0) { + rc = lfsck_scan_lpf_bad_entries(env, lfsck, parent); + if (rc != 0) + CDEBUG(D_LFSCK, "%s: scan .lustre/lost+found/ " + "for bad sub-directories: rc = %d\n", + lfsck_lfsck2name(lfsck), rc); + } + + if (!fid_is_zero(&bk->lb_lpf_fid)) { + if (unlikely(!fid_is_norm(&bk->lb_lpf_fid))) { + struct lu_fid tfid = bk->lb_lpf_fid; + + /* Invalid FID record in the bookmark file, reset it. */ + fid_zero(&bk->lb_lpf_fid); + rc = lfsck_bookmark_store(env, lfsck); + + CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID + " in the bookmark file: rc = %d\n", + lfsck_lfsck2name(lfsck), PFID(&tfid), rc); + + if (rc != 0) + GOTO(put, rc); + } else { + child1 = lfsck_object_find_by_dev(env, dev, + &bk->lb_lpf_fid); + if (IS_ERR(child1)) + GOTO(put, rc = PTR_ERR(child1)); + + if (unlikely(!dt_object_exists(child1) || + dt_object_remote(child1)) || + !S_ISDIR(lfsck_object_type(child1))) { + /* Invalid FID record in the bookmark file, + * reset it. */ + fid_zero(&bk->lb_lpf_fid); + rc = lfsck_bookmark_store(env, lfsck); + + CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID + " in the bookmark file: rc = %d\n", + lfsck_lfsck2name(lfsck), + PFID(lfsck_dto2fid(child1)), rc); + + if (rc != 0) + GOTO(put, rc); + + lu_object_put(env, &child1->do_lu); + child1 = NULL; + } else if (unlikely(!dt_try_as_dir(env, child1))) { + GOTO(put, rc = -ENOTDIR); + } + } + } + + snprintf(name, 8, "MDT%04x", node); + rc = dt_lookup(env, parent, (struct dt_rec *)cfid, + (const struct dt_key *)name, BYPASS_CAPA); + if (rc == -ENOENT) + goto check_child1; + + if (rc != 0) + GOTO(put, rc); + + /* Invalid FID in the name entry, remove the name entry. */ + if (!fid_is_norm(cfid)) { + rc = lfsck_remove_name_entry(env, lfsck, parent, name, S_IFDIR); + if (rc != 0) + GOTO(put, rc); + + goto check_child1; + } + + child2 = lfsck_object_find_by_dev(env, dev, cfid); + if (IS_ERR(child2)) + GOTO(put, rc = PTR_ERR(child2)); + + if (unlikely(!dt_object_exists(child2) || + dt_object_remote(child2)) || + !S_ISDIR(lfsck_object_type(child2))) { + rc = lfsck_remove_name_entry(env, lfsck, parent, name, + S_IFDIR); + if (rc != 0) + GOTO(put, rc); + + goto check_child1; + } + + if (unlikely(!dt_try_as_dir(env, child2))) + GOTO(put, rc = -ENOTDIR); + + if (child1 == NULL) { + rc = lfsck_verify_lpf_pairs(env, lfsck, parent, child2, name, + pfid, LVLT_BY_NAMEENTRY); + } else if (!lu_fid_eq(cfid, &bk->lb_lpf_fid)) { + rc = lfsck_verify_lpf_pairs(env, lfsck, parent, child1, name, + pfid, LVLT_BY_BOOKMARK); + if (!lu_fid_eq(pfid, &LU_LPF_FID)) + rc = lfsck_verify_lpf_pairs(env, lfsck, parent, child2, + name, pfid, + LVLT_BY_NAMEENTRY); + } else { + if (lfsck->li_lpf_obj == NULL) { + lu_object_get(&child2->do_lu); + lfsck->li_lpf_obj = child2; + } + + cname = lfsck_name_get_const(env, name, strlen(name)); + rc = lfsck_verify_linkea(env, dev, child2, cname, &LU_LPF_FID); + } + + GOTO(put, rc); + +check_child1: + if (child1 != NULL) + rc = lfsck_verify_lpf_pairs(env, lfsck, parent, child1, name, + pfid, LVLT_BY_BOOKMARK); + + GOTO(put, rc); + +put: + if (lfsck->li_lpf_obj != NULL && + unlikely(!dt_try_as_dir(env, lfsck->li_lpf_obj))) + rc = -ENOTDIR; + + if (child2 != NULL && !IS_ERR(child2)) + lu_object_put(env, &child2->do_lu); + if (child1 != NULL && !IS_ERR(child1)) + lu_object_put(env, &child1->do_lu); + if (parent != NULL && !IS_ERR(parent)) + lu_object_put(env, &parent->do_lu); + + return rc; +} + static int lfsck_fid_init(struct lfsck_instance *lfsck) { struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram; @@ -793,6 +1401,7 @@ void lfsck_instance_cleanup(const struct lu_env *env, { struct ptlrpc_thread *thread = &lfsck->li_thread; struct lfsck_component *com; + struct lfsck_component *next; ENTRY; LASSERT(list_empty(&lfsck->li_link)); @@ -805,26 +1414,18 @@ void lfsck_instance_cleanup(const struct lu_env *env, LASSERT(lfsck->li_obj_dir == NULL); - while (!cfs_list_empty(&lfsck->li_list_scan)) { - com = cfs_list_entry(lfsck->li_list_scan.next, - struct lfsck_component, - lc_link); + list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) { lfsck_component_cleanup(env, com); } - LASSERT(cfs_list_empty(&lfsck->li_list_dir)); + LASSERT(list_empty(&lfsck->li_list_dir)); - while (!cfs_list_empty(&lfsck->li_list_double_scan)) { - com = cfs_list_entry(lfsck->li_list_double_scan.next, - struct lfsck_component, - lc_link); + list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan, + lc_link) { lfsck_component_cleanup(env, com); } - while (!cfs_list_empty(&lfsck->li_list_idle)) { - com = cfs_list_entry(lfsck->li_list_idle.next, - struct lfsck_component, - lc_link); + list_for_each_entry_safe(com, next, &lfsck->li_list_idle, lc_link) { lfsck_component_cleanup(env, com); } @@ -856,7 +1457,7 @@ __lfsck_instance_find(struct dt_device *key, bool ref, bool unlink) { struct lfsck_instance *lfsck; - cfs_list_for_each_entry(lfsck, &lfsck_instance_list, li_link) { + list_for_each_entry(lfsck, &lfsck_instance_list, li_link) { if (lfsck->li_bottom == key) { if (ref) lfsck_instance_get(lfsck); @@ -887,33 +1488,27 @@ static inline int lfsck_instance_add(struct lfsck_instance *lfsck) struct lfsck_instance *tmp; spin_lock(&lfsck_instance_lock); - cfs_list_for_each_entry(tmp, &lfsck_instance_list, li_link) { + list_for_each_entry(tmp, &lfsck_instance_list, li_link) { if (lfsck->li_bottom == tmp->li_bottom) { spin_unlock(&lfsck_instance_lock); return -EEXIST; } } - cfs_list_add_tail(&lfsck->li_link, &lfsck_instance_list); + list_add_tail(&lfsck->li_link, &lfsck_instance_list); spin_unlock(&lfsck_instance_lock); return 0; } -int lfsck_bits_dump(char **buf, int *len, int bits, const char *names[], +int lfsck_bits_dump(struct seq_file *m, int bits, const char *names[], const char *prefix) { - int save = *len; int flag; - int rc; int i; bool newline = (bits != 0 ? false : true); - rc = snprintf(*buf, *len, "%s:%c", prefix, newline ? '\n' : ' '); - if (rc <= 0) - return -ENOSPC; + seq_printf(m, "%s:%c", prefix, bits != 0 ? ' ' : '\n'); - *buf += rc; - *len -= rc; for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) { if (flag & bits) { bits &= ~flag; @@ -921,69 +1516,43 @@ int lfsck_bits_dump(char **buf, int *len, int bits, const char *names[], if (bits == 0) newline = true; - rc = snprintf(*buf, *len, "%s%c", names[i], - newline ? '\n' : ','); - if (rc <= 0) - return -ENOSPC; - - *buf += rc; - *len -= rc; + seq_printf(m, "%s%c", names[i], + newline ? '\n' : ','); } } } - if (!newline) { - rc = snprintf(*buf, *len, "\n"); - if (rc <= 0) - return -ENOSPC; - - *buf += rc; - *len -= rc; - } - - return save - *len; + if (!newline) + seq_printf(m, "\n"); + return 0; } -int lfsck_time_dump(char **buf, int *len, __u64 time, const char *prefix) +int lfsck_time_dump(struct seq_file *m, __u64 time, const char *prefix) { - int rc; - if (time != 0) - rc = snprintf(*buf, *len, "%s: "LPU64" seconds\n", prefix, - cfs_time_current_sec() - time); + seq_printf(m, "%s: "LPU64" seconds\n", prefix, + cfs_time_current_sec() - time); else - rc = snprintf(*buf, *len, "%s: N/A\n", prefix); - if (rc <= 0) - return -ENOSPC; - - *buf += rc; - *len -= rc; - return rc; + seq_printf(m, "%s: N/A\n", prefix); + return 0; } -int lfsck_pos_dump(char **buf, int *len, struct lfsck_position *pos, +int lfsck_pos_dump(struct seq_file *m, struct lfsck_position *pos, const char *prefix) { - int rc; - if (fid_is_zero(&pos->lp_dir_parent)) { if (pos->lp_oit_cookie == 0) - rc = snprintf(*buf, *len, "%s: N/A, N/A, N/A\n", - prefix); + seq_printf(m, "%s: N/A, N/A, N/A\n", + prefix); else - rc = snprintf(*buf, *len, "%s: "LPU64", N/A, N/A\n", - prefix, pos->lp_oit_cookie); + seq_printf(m, "%s: "LPU64", N/A, N/A\n", + prefix, pos->lp_oit_cookie); } else { - rc = snprintf(*buf, *len, "%s: "LPU64", "DFID", "LPU64"\n", - prefix, pos->lp_oit_cookie, - PFID(&pos->lp_dir_parent), pos->lp_dir_cookie); + seq_printf(m, "%s: "LPU64", "DFID", "LPX64"\n", + prefix, pos->lp_oit_cookie, + PFID(&pos->lp_dir_parent), pos->lp_dir_cookie); } - if (rc <= 0) - return -ENOSPC; - - *buf += rc; - *len -= rc; - return rc; + return 0; } void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck, @@ -1015,442 +1584,104 @@ void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck, pos->lp_dir_parent = *lfsck_dto2fid(dto); } } else { - fid_zero(&pos->lp_dir_parent); - pos->lp_dir_cookie = 0; - } -} - -static void __lfsck_set_speed(struct lfsck_instance *lfsck, __u32 limit) -{ - lfsck->li_bookmark_ram.lb_speed_limit = limit; - if (limit != LFSCK_SPEED_NO_LIMIT) { - if (limit > HZ) { - lfsck->li_sleep_rate = limit / HZ; - lfsck->li_sleep_jif = 1; - } else { - lfsck->li_sleep_rate = 1; - lfsck->li_sleep_jif = HZ / limit; - } - } else { - lfsck->li_sleep_jif = 0; - lfsck->li_sleep_rate = 0; - } -} - -void lfsck_control_speed(struct lfsck_instance *lfsck) -{ - struct ptlrpc_thread *thread = &lfsck->li_thread; - struct l_wait_info lwi; - - if (lfsck->li_sleep_jif > 0 && - lfsck->li_new_scanned >= lfsck->li_sleep_rate) { - lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL, - LWI_ON_SIGNAL_NOOP, NULL); - - l_wait_event(thread->t_ctl_waitq, - !thread_is_running(thread), - &lwi); - lfsck->li_new_scanned = 0; - } -} - -void lfsck_control_speed_by_self(struct lfsck_component *com) -{ - struct lfsck_instance *lfsck = com->lc_lfsck; - struct ptlrpc_thread *thread = &lfsck->li_thread; - struct l_wait_info lwi; - - if (lfsck->li_sleep_jif > 0 && - com->lc_new_scanned >= lfsck->li_sleep_rate) { - lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL, - LWI_ON_SIGNAL_NOOP, NULL); - - l_wait_event(thread->t_ctl_waitq, - !thread_is_running(thread), - &lwi); - com->lc_new_scanned = 0; - } -} - -static int lfsck_parent_fid(const struct lu_env *env, struct dt_object *obj, - struct lu_fid *fid) -{ - if (unlikely(!S_ISDIR(lfsck_object_type(obj)) || - !dt_try_as_dir(env, obj))) - return -ENOTDIR; - - return dt_lookup(env, obj, (struct dt_rec *)fid, - (const struct dt_key *)"..", BYPASS_CAPA); -} - -static int lfsck_needs_scan_dir(const struct lu_env *env, - struct lfsck_instance *lfsck, - struct dt_object *obj) -{ - struct lu_fid *fid = &lfsck_env_info(env)->lti_fid; - int depth = 0; - int rc; - - if (!lfsck->li_master || !S_ISDIR(lfsck_object_type(obj)) || - cfs_list_empty(&lfsck->li_list_dir)) - RETURN(0); - - while (1) { - /* XXX: Currently, we do not scan the "/REMOTE_PARENT_DIR", - * which is the agent directory to manage the objects - * which name entries reside on remote MDTs. Related - * consistency verification will be processed in LFSCK - * phase III. */ - if (lu_fid_eq(lfsck_dto2fid(obj), &lfsck->li_global_root_fid)) { - if (depth > 0) - lfsck_object_put(env, obj); - return 1; - } - - /* .lustre doesn't contain "real" user objects, no need lfsck */ - if (fid_is_dot_lustre(lfsck_dto2fid(obj))) { - if (depth > 0) - lfsck_object_put(env, obj); - return 0; - } - - dt_read_lock(env, obj, MOR_TGT_CHILD); - if (unlikely(lfsck_is_dead_obj(obj))) { - dt_read_unlock(env, obj); - if (depth > 0) - lfsck_object_put(env, obj); - return 0; - } - - rc = dt_xattr_get(env, obj, - lfsck_buf_get(env, NULL, 0), XATTR_NAME_LINK, - BYPASS_CAPA); - dt_read_unlock(env, obj); - if (rc >= 0) { - if (depth > 0) - lfsck_object_put(env, obj); - return 1; - } - - if (rc < 0 && rc != -ENODATA) { - if (depth > 0) - lfsck_object_put(env, obj); - return rc; - } - - rc = lfsck_parent_fid(env, obj, fid); - if (depth > 0) - lfsck_object_put(env, obj); - if (rc != 0) - return rc; - - if (unlikely(lu_fid_eq(fid, &lfsck->li_local_root_fid))) - return 0; - - obj = lfsck_object_find(env, lfsck, fid); - if (obj == NULL) - return 0; - else if (IS_ERR(obj)) - return PTR_ERR(obj); - - if (!dt_object_exists(obj)) { - lfsck_object_put(env, obj); - return 0; - } - - /* Currently, only client visible directory can be remote. */ - if (dt_object_remote(obj)) { - lfsck_object_put(env, obj); - return 1; - } - - depth++; - } - return 0; -} - -struct lfsck_thread_args *lfsck_thread_args_init(struct lfsck_instance *lfsck, - struct lfsck_component *com, - struct lfsck_start_param *lsp) -{ - struct lfsck_thread_args *lta; - int rc; - - OBD_ALLOC_PTR(lta); - if (lta == NULL) - return ERR_PTR(-ENOMEM); - - rc = lu_env_init(<a->lta_env, LCT_MD_THREAD | LCT_DT_THREAD); - if (rc != 0) { - OBD_FREE_PTR(lta); - return ERR_PTR(rc); - } - - lta->lta_lfsck = lfsck_instance_get(lfsck); - if (com != NULL) - lta->lta_com = lfsck_component_get(com); - - lta->lta_lsp = lsp; - - return lta; -} - -void lfsck_thread_args_fini(struct lfsck_thread_args *lta) -{ - if (lta->lta_com != NULL) - lfsck_component_put(<a->lta_env, lta->lta_com); - lfsck_instance_put(<a->lta_env, lta->lta_lfsck); - lu_env_fini(<a->lta_env); - OBD_FREE_PTR(lta); -} - -/* LFSCK wrap functions */ - -void lfsck_fail(const struct lu_env *env, struct lfsck_instance *lfsck, - bool new_checked) -{ - struct lfsck_component *com; - - cfs_list_for_each_entry(com, &lfsck->li_list_scan, lc_link) { - com->lc_ops->lfsck_fail(env, com, new_checked); - } -} - -int lfsck_checkpoint(const struct lu_env *env, struct lfsck_instance *lfsck) -{ - struct lfsck_component *com; - int rc = 0; - int rc1 = 0; - - if (likely(cfs_time_beforeq(cfs_time_current(), - lfsck->li_time_next_checkpoint))) - return 0; - - lfsck_pos_fill(env, lfsck, &lfsck->li_pos_current, false); - cfs_list_for_each_entry(com, &lfsck->li_list_scan, lc_link) { - rc = com->lc_ops->lfsck_checkpoint(env, com, false); - if (rc != 0) - rc1 = rc; - } - - lfsck->li_time_last_checkpoint = cfs_time_current(); - lfsck->li_time_next_checkpoint = lfsck->li_time_last_checkpoint + - cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL); - return rc1 != 0 ? rc1 : rc; -} - -int lfsck_prep(const struct lu_env *env, struct lfsck_instance *lfsck, - struct lfsck_start_param *lsp) -{ - struct dt_object *obj = NULL; - struct lfsck_component *com; - struct lfsck_component *next; - struct lfsck_position *pos = NULL; - const struct dt_it_ops *iops = - &lfsck->li_obj_oit->do_index_ops->dio_it; - struct dt_it *di; - int rc; - ENTRY; - - LASSERT(lfsck->li_obj_dir == NULL); - LASSERT(lfsck->li_di_dir == NULL); - - lfsck->li_current_oit_processed = 0; - cfs_list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) { - com->lc_new_checked = 0; - if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN) - com->lc_journal = 0; - - rc = com->lc_ops->lfsck_prep(env, com, lsp); - if (rc != 0) - GOTO(out, rc); - - if ((pos == NULL) || - (!lfsck_pos_is_zero(&com->lc_pos_start) && - lfsck_pos_is_eq(pos, &com->lc_pos_start) > 0)) - pos = &com->lc_pos_start; - } - - /* Init otable-based iterator. */ - if (pos == NULL) { - rc = iops->load(env, lfsck->li_di_oit, 0); - if (rc > 0) { - lfsck->li_oit_over = 1; - rc = 0; - } - - GOTO(out, rc); - } - - rc = iops->load(env, lfsck->li_di_oit, pos->lp_oit_cookie); - if (rc < 0) - GOTO(out, rc); - else if (rc > 0) - lfsck->li_oit_over = 1; - - if (!lfsck->li_master || fid_is_zero(&pos->lp_dir_parent)) - GOTO(out, rc = 0); - - /* Find the directory for namespace-based traverse. */ - obj = lfsck_object_find(env, lfsck, &pos->lp_dir_parent); - if (obj == NULL) - GOTO(out, rc = 0); - else if (IS_ERR(obj)) - RETURN(PTR_ERR(obj)); - - /* XXX: Currently, skip remote object, the consistency for - * remote object will be processed in LFSCK phase III. */ - if (!dt_object_exists(obj) || dt_object_remote(obj) || - unlikely(!S_ISDIR(lfsck_object_type(obj)))) - GOTO(out, rc = 0); - - if (unlikely(!dt_try_as_dir(env, obj))) - GOTO(out, rc = -ENOTDIR); - - /* Init the namespace-based directory traverse. */ - iops = &obj->do_index_ops->dio_it; - di = iops->init(env, obj, lfsck->li_args_dir, BYPASS_CAPA); - if (IS_ERR(di)) - GOTO(out, rc = PTR_ERR(di)); - - LASSERT(pos->lp_dir_cookie < MDS_DIR_END_OFF); - - rc = iops->load(env, di, pos->lp_dir_cookie); - if ((rc == 0) || (rc > 0 && pos->lp_dir_cookie > 0)) - rc = iops->next(env, di); - else if (rc > 0) - rc = 0; - - if (rc != 0) { - iops->put(env, di); - iops->fini(env, di); - GOTO(out, rc); + fid_zero(&pos->lp_dir_parent); + pos->lp_dir_cookie = 0; } +} - lfsck->li_obj_dir = lfsck_object_get(obj); - lfsck->li_cookie_dir = iops->store(env, di); - spin_lock(&lfsck->li_lock); - lfsck->li_di_dir = di; - spin_unlock(&lfsck->li_lock); - - GOTO(out, rc = 0); - -out: - if (obj != NULL) - lfsck_object_put(env, obj); - - if (rc < 0) { - cfs_list_for_each_entry_safe(com, next, &lfsck->li_list_scan, - lc_link) - com->lc_ops->lfsck_post(env, com, rc, true); +bool __lfsck_set_speed(struct lfsck_instance *lfsck, __u32 limit) +{ + bool dirty = false; - return rc; + if (limit != LFSCK_SPEED_NO_LIMIT) { + if (limit > HZ) { + lfsck->li_sleep_rate = limit / HZ; + lfsck->li_sleep_jif = 1; + } else { + lfsck->li_sleep_rate = 1; + lfsck->li_sleep_jif = HZ / limit; + } + } else { + lfsck->li_sleep_jif = 0; + lfsck->li_sleep_rate = 0; } - rc = 0; - lfsck_pos_fill(env, lfsck, &lfsck->li_pos_current, true); - cfs_list_for_each_entry(com, &lfsck->li_list_scan, lc_link) { - rc = com->lc_ops->lfsck_checkpoint(env, com, true); - if (rc != 0) - break; + if (lfsck->li_bookmark_ram.lb_speed_limit != limit) { + lfsck->li_bookmark_ram.lb_speed_limit = limit; + dirty = true; } - lfsck->li_time_last_checkpoint = cfs_time_current(); - lfsck->li_time_next_checkpoint = lfsck->li_time_last_checkpoint + - cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL); - return rc; + return dirty; } -int lfsck_exec_oit(const struct lu_env *env, struct lfsck_instance *lfsck, - struct dt_object *obj) +void lfsck_control_speed(struct lfsck_instance *lfsck) { - struct lfsck_component *com; - const struct dt_it_ops *iops; - struct dt_it *di; - int rc; - ENTRY; + struct ptlrpc_thread *thread = &lfsck->li_thread; + struct l_wait_info lwi; - LASSERT(lfsck->li_obj_dir == NULL); + if (lfsck->li_sleep_jif > 0 && + lfsck->li_new_scanned >= lfsck->li_sleep_rate) { + lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL, + LWI_ON_SIGNAL_NOOP, NULL); - cfs_list_for_each_entry(com, &lfsck->li_list_scan, lc_link) { - rc = com->lc_ops->lfsck_exec_oit(env, com, obj); - if (rc != 0) - RETURN(rc); + l_wait_event(thread->t_ctl_waitq, + !thread_is_running(thread), + &lwi); + lfsck->li_new_scanned = 0; } +} - rc = lfsck_needs_scan_dir(env, lfsck, obj); - if (rc <= 0) - GOTO(out, rc); - - if (unlikely(!dt_try_as_dir(env, obj))) - GOTO(out, rc = -ENOTDIR); - - iops = &obj->do_index_ops->dio_it; - di = iops->init(env, obj, lfsck->li_args_dir, BYPASS_CAPA); - if (IS_ERR(di)) - GOTO(out, rc = PTR_ERR(di)); +void lfsck_control_speed_by_self(struct lfsck_component *com) +{ + struct lfsck_instance *lfsck = com->lc_lfsck; + struct ptlrpc_thread *thread = &lfsck->li_thread; + struct l_wait_info lwi; - rc = iops->load(env, di, 0); - if (rc == 0) - rc = iops->next(env, di); - else if (rc > 0) - rc = 0; + if (lfsck->li_sleep_jif > 0 && + com->lc_new_scanned >= lfsck->li_sleep_rate) { + lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL, + LWI_ON_SIGNAL_NOOP, NULL); - if (rc != 0) { - iops->put(env, di); - iops->fini(env, di); - GOTO(out, rc); + l_wait_event(thread->t_ctl_waitq, + !thread_is_running(thread), + &lwi); + com->lc_new_scanned = 0; } - - lfsck->li_obj_dir = lfsck_object_get(obj); - lfsck->li_cookie_dir = iops->store(env, di); - spin_lock(&lfsck->li_lock); - lfsck->li_di_dir = di; - spin_unlock(&lfsck->li_lock); - - GOTO(out, rc = 0); - -out: - if (rc < 0) - lfsck_fail(env, lfsck, false); - return (rc > 0 ? 0 : rc); } -int lfsck_exec_dir(const struct lu_env *env, struct lfsck_instance *lfsck, - struct dt_object *obj, struct lu_dirent *ent) +struct lfsck_thread_args *lfsck_thread_args_init(struct lfsck_instance *lfsck, + struct lfsck_component *com, + struct lfsck_start_param *lsp) { - struct lfsck_component *com; - int rc; + struct lfsck_thread_args *lta; + int rc; - cfs_list_for_each_entry(com, &lfsck->li_list_scan, lc_link) { - rc = com->lc_ops->lfsck_exec_dir(env, com, obj, ent); - if (rc != 0) - return rc; + OBD_ALLOC_PTR(lta); + if (lta == NULL) + return ERR_PTR(-ENOMEM); + + rc = lu_env_init(<a->lta_env, LCT_MD_THREAD | LCT_DT_THREAD); + if (rc != 0) { + OBD_FREE_PTR(lta); + return ERR_PTR(rc); } - return 0; -} -int lfsck_post(const struct lu_env *env, struct lfsck_instance *lfsck, - int result) -{ - struct lfsck_component *com; - struct lfsck_component *next; - int rc = 0; - int rc1 = 0; + lta->lta_lfsck = lfsck_instance_get(lfsck); + if (com != NULL) + lta->lta_com = lfsck_component_get(com); - lfsck_pos_fill(env, lfsck, &lfsck->li_pos_current, false); - cfs_list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) { - rc = com->lc_ops->lfsck_post(env, com, result, false); - if (rc != 0) - rc1 = rc; - } + lta->lta_lsp = lsp; - lfsck->li_time_last_checkpoint = cfs_time_current(); - lfsck->li_time_next_checkpoint = lfsck->li_time_last_checkpoint + - cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL); + return lta; +} - /* Ignore some component post failure to make other can go ahead. */ - return result; +void lfsck_thread_args_fini(struct lfsck_thread_args *lta) +{ + if (lta->lta_com != NULL) + lfsck_component_put(<a->lta_env, lta->lta_com); + lfsck_instance_put(<a->lta_env, lta->lta_lfsck); + lu_env_fini(<a->lta_env); + OBD_FREE_PTR(lta); } static void lfsck_interpret(const struct lu_env *env, @@ -1480,41 +1711,6 @@ static void lfsck_interpret(const struct lu_env *env, spin_unlock(&lfsck->li_lock); } -int lfsck_double_scan(const struct lu_env *env, struct lfsck_instance *lfsck) -{ - struct lfsck_component *com; - struct lfsck_component *next; - struct l_wait_info lwi = { 0 }; - int rc = 0; - int rc1 = 0; - - list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) { - if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN) - com->lc_journal = 0; - - rc = com->lc_ops->lfsck_double_scan(env, com); - if (rc != 0) - rc1 = rc; - } - - l_wait_event(lfsck->li_thread.t_ctl_waitq, - atomic_read(&lfsck->li_double_scan_count) == 0, - &lwi); - - if (lfsck->li_status != LS_PAUSED && - lfsck->li_status != LS_CO_PAUSED) { - list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan, - lc_link) { - spin_lock(&lfsck->li_lock); - list_del_init(&com->lc_link); - list_add_tail(&com->lc_link, &lfsck->li_list_idle); - spin_unlock(&lfsck->li_lock); - } - } - - return rc1 != 0 ? rc1 : rc; -} - static int lfsck_stop_notify(const struct lu_env *env, struct lfsck_instance *lfsck, struct lfsck_tgt_descs *ltds, @@ -1532,7 +1728,7 @@ static int lfsck_stop_notify(const struct lu_env *env, &lfsck->li_list_double_scan); if (com != NULL) lfsck_component_get(com); - spin_lock(&lfsck->li_lock); + spin_unlock(&lfsck->li_lock); if (com != NULL) { if (com->lc_ops->lfsck_stop_notify != NULL) { @@ -1557,35 +1753,6 @@ static int lfsck_stop_notify(const struct lu_env *env, RETURN(rc); } -void lfsck_quit(const struct lu_env *env, struct lfsck_instance *lfsck) -{ - struct lfsck_component *com; - struct lfsck_component *next; - - list_for_each_entry_safe(com, next, &lfsck->li_list_scan, - lc_link) { - if (com->lc_ops->lfsck_quit != NULL) - com->lc_ops->lfsck_quit(env, com); - - spin_lock(&lfsck->li_lock); - list_del_init(&com->lc_link); - list_del_init(&com->lc_link_dir); - list_add_tail(&com->lc_link, &lfsck->li_list_idle); - spin_unlock(&lfsck->li_lock); - } - - list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan, - lc_link) { - if (com->lc_ops->lfsck_quit != NULL) - com->lc_ops->lfsck_quit(env, com); - - spin_lock(&lfsck->li_lock); - list_del_init(&com->lc_link); - list_add_tail(&com->lc_link, &lfsck->li_list_idle); - spin_unlock(&lfsck->li_lock); - } -} - static int lfsck_async_interpret(const struct lu_env *env, struct ptlrpc_request *req, void *args, int rc) @@ -1615,9 +1782,6 @@ int lfsck_async_request(const struct lu_env *env, struct obd_export *exp, struct req_format *format; int rc; - if (!(exp_connect_flags(exp) & OBD_CONNECT_LFSCK)) - return -EOPNOTSUPP; - switch (request) { case LFSCK_NOTIFY: format = &RQF_LFSCK_NOTIFY; @@ -1626,8 +1790,8 @@ int lfsck_async_request(const struct lu_env *env, struct obd_export *exp, format = &RQF_LFSCK_QUERY; break; default: - CERROR("%s: unknown async request: opc = %d\n", - exp->exp_obd->obd_name, request); + CDEBUG(D_LFSCK, "%s: unknown async request %d: rc = %d\n", + exp->exp_obd->obd_name, request, -EINVAL); return -EINVAL; } @@ -1658,7 +1822,7 @@ int lfsck_async_request(const struct lu_env *env, struct obd_export *exp, /* external interfaces */ -int lfsck_get_speed(struct dt_device *key, void *buf, int len) +int lfsck_get_speed(struct seq_file *m, struct dt_device *key) { struct lu_env env; struct lfsck_instance *lfsck; @@ -1671,8 +1835,7 @@ int lfsck_get_speed(struct dt_device *key, void *buf, int len) lfsck = lfsck_instance_find(key, true, false); if (likely(lfsck != NULL)) { - rc = snprintf(buf, len, "%u\n", - lfsck->li_bookmark_ram.lb_speed_limit); + seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_speed_limit); lfsck_instance_put(&env, lfsck); } else { rc = -ENXIO; @@ -1698,8 +1861,8 @@ int lfsck_set_speed(struct dt_device *key, int val) lfsck = lfsck_instance_find(key, true, false); if (likely(lfsck != NULL)) { mutex_lock(&lfsck->li_mutex); - __lfsck_set_speed(lfsck, val); - rc = lfsck_bookmark_store(&env, lfsck); + if (__lfsck_set_speed(lfsck, val)) + rc = lfsck_bookmark_store(&env, lfsck); mutex_unlock(&lfsck->li_mutex); lfsck_instance_put(&env, lfsck); } else { @@ -1712,7 +1875,7 @@ int lfsck_set_speed(struct dt_device *key, int val) } EXPORT_SYMBOL(lfsck_set_speed); -int lfsck_get_windows(struct dt_device *key, void *buf, int len) +int lfsck_get_windows(struct seq_file *m, struct dt_device *key) { struct lu_env env; struct lfsck_instance *lfsck; @@ -1725,8 +1888,7 @@ int lfsck_get_windows(struct dt_device *key, void *buf, int len) lfsck = lfsck_instance_find(key, true, false); if (likely(lfsck != NULL)) { - rc = snprintf(buf, len, "%u\n", - lfsck->li_bookmark_ram.lb_async_windows); + seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_async_windows); lfsck_instance_put(&env, lfsck); } else { rc = -ENXIO; @@ -1752,12 +1914,12 @@ int lfsck_set_windows(struct dt_device *key, int val) lfsck = lfsck_instance_find(key, true, false); if (likely(lfsck != NULL)) { if (val > LFSCK_ASYNC_WIN_MAX) { - CERROR("%s: Too large async windows size, which " - "may cause memory issues. The valid range " - "is [0 - %u]. If you do not want to restrict " - "the windows size for async requests pipeline, " - "just set it as 0.\n", - lfsck_lfsck2name(lfsck), LFSCK_ASYNC_WIN_MAX); + CWARN("%s: Too large async window size, which " + "may cause memory issues. The valid range " + "is [0 - %u]. If you do not want to restrict " + "the window size for async requests pipeline, " + "just set it as 0.\n", + lfsck_lfsck2name(lfsck), LFSCK_ASYNC_WIN_MAX); rc = -EINVAL; } else if (lfsck->li_bookmark_ram.lb_async_windows != val) { mutex_lock(&lfsck->li_mutex); @@ -1776,7 +1938,7 @@ int lfsck_set_windows(struct dt_device *key, int val) } EXPORT_SYMBOL(lfsck_set_windows); -int lfsck_dump(struct dt_device *key, void *buf, int len, enum lfsck_type type) +int lfsck_dump(struct seq_file *m, struct dt_device *key, enum lfsck_type type) { struct lu_env env; struct lfsck_instance *lfsck; @@ -1792,7 +1954,7 @@ int lfsck_dump(struct dt_device *key, void *buf, int len, enum lfsck_type type) if (likely(lfsck != NULL)) { com = lfsck_component_find(lfsck, type); if (likely(com != NULL)) { - rc = com->lc_ops->lfsck_dump(&env, com, buf, len); + rc = com->lc_ops->lfsck_dump(&env, com, m); lfsck_component_put(&env, com); } else { rc = -ENOTSUPP; @@ -1828,12 +1990,8 @@ static int lfsck_stop_all(const struct lu_env *env, LASSERT(stop->ls_flags & LPF_BROADCAST); set = ptlrpc_prep_set(); - if (unlikely(set == NULL)) { - CERROR("%s: cannot allocate memory for stop LFSCK on " - "all targets\n", lfsck_lfsck2name(lfsck)); - + if (unlikely(set == NULL)) RETURN(-ENOMEM); - } memset(lr, 0, sizeof(*lr)); lr->lr_event = LE_STOP; @@ -1861,8 +2019,8 @@ static int lfsck_stop_all(const struct lu_env *env, if (rc != 0) { lfsck_interpret(env, lfsck, NULL, laia, rc); lfsck_tgt_put(ltd); - CWARN("%s: cannot notify MDT %x for LFSCK stop: " - "rc = %d\n", lfsck_lfsck2name(lfsck), idx, rc); + CERROR("%s: cannot notify MDT %x for LFSCK stop: " + "rc = %d\n", lfsck_lfsck2name(lfsck), idx, rc); rc1 = rc; } } @@ -1878,8 +2036,8 @@ static int lfsck_stop_all(const struct lu_env *env, rc = 0; if (rc != 0) - CWARN("%s: fail to stop LFSCK on some MDTs: rc = %d\n", - lfsck_lfsck2name(lfsck), rc); + CERROR("%s: fail to stop LFSCK on some MDTs: rc = %d\n", + lfsck_lfsck2name(lfsck), rc); RETURN(rc != 0 ? rc : rc1); } @@ -1902,21 +2060,8 @@ static int lfsck_start_all(const struct lu_env *env, LASSERT(start->ls_flags & LPF_BROADCAST); set = ptlrpc_prep_set(); - if (unlikely(set == NULL)) { - if (bk->lb_param & LPF_FAILOUT) { - CERROR("%s: cannot allocate memory for start LFSCK on " - "all targets, failout.\n", - lfsck_lfsck2name(lfsck)); - - RETURN(-ENOMEM); - } else { - CWARN("%s: cannot allocate memory for start LFSCK on " - "all targets, partly scan.\n", - lfsck_lfsck2name(lfsck)); - - RETURN(0); - } - } + if (unlikely(set == NULL)) + RETURN(-ENOMEM); memset(lr, 0, sizeof(*lr)); lr->lr_event = LE_START; @@ -1948,17 +2093,10 @@ static int lfsck_start_all(const struct lu_env *env, if (rc != 0) { lfsck_interpret(env, lfsck, NULL, laia, rc); lfsck_tgt_put(ltd); - if (bk->lb_param & LPF_FAILOUT) { - CERROR("%s: cannot notify MDT %x for LFSCK " - "start, failout: rc = %d\n", - lfsck_lfsck2name(lfsck), idx, rc); - break; - } else { - CWARN("%s: cannot notify MDT %x for LFSCK " - "start, partly scan: rc = %d\n", - lfsck_lfsck2name(lfsck), idx, rc); - rc = 0; - } + CERROR("%s: cannot notify MDT %x for LFSCK " + "start, failout: rc = %d\n", + lfsck_lfsck2name(lfsck), idx, rc); + break; } } up_read(<ds->ltd_rw_sem); @@ -1976,22 +2114,15 @@ static int lfsck_start_all(const struct lu_env *env, rc = laia->laia_result; if (rc != 0) { - if (bk->lb_param & LPF_FAILOUT) { - struct lfsck_stop *stop = &info->lti_stop; + struct lfsck_stop *stop = &info->lti_stop; - CERROR("%s: cannot start LFSCK on some MDTs, " - "stop all: rc = %d\n", - lfsck_lfsck2name(lfsck), rc); - if (rc != -EALREADY) { - stop->ls_status = LS_FAILED; - stop->ls_flags = LPF_ALL_TGT | LPF_BROADCAST; - lfsck_stop_all(env, lfsck, stop); - } - } else { - CWARN("%s: cannot start LFSCK on some MDTs, " - "partly scan: rc = %d\n", - lfsck_lfsck2name(lfsck), rc); - rc = 0; + CERROR("%s: cannot start LFSCK on some MDTs, " + "stop all: rc = %d\n", + lfsck_lfsck2name(lfsck), rc); + if (rc != -EALREADY) { + stop->ls_status = LS_FAILED; + stop->ls_flags = LPF_ALL_TGT | LPF_BROADCAST; + lfsck_stop_all(env, lfsck, stop); } } @@ -2008,8 +2139,8 @@ int lfsck_start(const struct lu_env *env, struct dt_device *key, struct lfsck_component *com; struct l_wait_info lwi = { 0 }; struct lfsck_thread_args *lta; - bool dirty = false; - long rc = 0; + struct task_struct *task; + int rc = 0; __u16 valid = 0; __u16 flags = 0; __u16 type = 1; @@ -2025,7 +2156,7 @@ int lfsck_start(const struct lu_env *env, struct dt_device *key, /* start == NULL means auto trigger paused LFSCK. */ if ((start == NULL) && - (cfs_list_empty(&lfsck->li_list_scan) || + (list_empty(&lfsck->li_list_scan) || OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AUTO))) GOTO(put, rc = 0); @@ -2035,6 +2166,11 @@ int lfsck_start(const struct lu_env *env, struct dt_device *key, spin_lock(&lfsck->li_lock); if (!thread_is_init(thread) && !thread_is_stopped(thread)) { rc = -EALREADY; + if (unlikely(start == NULL)) { + spin_unlock(&lfsck->li_lock); + GOTO(out, rc); + } + while (start->ls_active != 0) { if (!(type & start->ls_active)) { type <<= 1; @@ -2082,78 +2218,6 @@ int lfsck_start(const struct lu_env *env, struct dt_device *key, } start->ls_version = bk->lb_version; - if (start->ls_valid & LSV_SPEED_LIMIT) { - __lfsck_set_speed(lfsck, start->ls_speed_limit); - dirty = true; - } - - if (start->ls_valid & LSV_ASYNC_WINDOWS && - bk->lb_async_windows != start->ls_async_windows) { - bk->lb_async_windows = start->ls_async_windows; - dirty = true; - } - - if (start->ls_valid & LSV_ERROR_HANDLE) { - valid |= DOIV_ERROR_HANDLE; - if (start->ls_flags & LPF_FAILOUT) - flags |= DOIF_FAILOUT; - - if ((start->ls_flags & LPF_FAILOUT) && - !(bk->lb_param & LPF_FAILOUT)) { - bk->lb_param |= LPF_FAILOUT; - dirty = true; - } else if (!(start->ls_flags & LPF_FAILOUT) && - (bk->lb_param & LPF_FAILOUT)) { - bk->lb_param &= ~LPF_FAILOUT; - dirty = true; - } - } - - if (start->ls_valid & LSV_DRYRUN) { - valid |= DOIV_DRYRUN; - if (start->ls_flags & LPF_DRYRUN) - flags |= DOIF_DRYRUN; - - if ((start->ls_flags & LPF_DRYRUN) && - !(bk->lb_param & LPF_DRYRUN)) { - bk->lb_param |= LPF_DRYRUN; - dirty = true; - } else if (!(start->ls_flags & LPF_DRYRUN) && - (bk->lb_param & LPF_DRYRUN)) { - bk->lb_param &= ~LPF_DRYRUN; - lfsck->li_drop_dryrun = 1; - dirty = true; - } - } - - if (bk->lb_param & LPF_ALL_TGT && - !(start->ls_flags & LPF_ALL_TGT)) { - bk->lb_param &= ~LPF_ALL_TGT; - dirty = true; - } else if (!(bk->lb_param & LPF_ALL_TGT) && - start->ls_flags & LPF_ALL_TGT) { - bk->lb_param |= LPF_ALL_TGT; - dirty = true; - } - - if (bk->lb_param & LPF_ORPHAN && - !(start->ls_flags & LPF_ORPHAN)) { - bk->lb_param &= ~LPF_ORPHAN; - dirty = true; - } else if (!(bk->lb_param & LPF_ORPHAN) && - start->ls_flags & LPF_ORPHAN) { - bk->lb_param |= LPF_ORPHAN; - dirty = true; - } - - if (dirty) { - rc = lfsck_bookmark_store(env, lfsck); - if (rc != 0) - GOTO(out, rc); - } - - if (start->ls_flags & LPF_RESET) - flags |= DOIF_RESET; if (start->ls_active != 0) { struct lfsck_component *next; @@ -2166,8 +2230,8 @@ int lfsck_start(const struct lu_env *env, struct dt_device *key, GOTO(out, rc = -ENOTSUPP); } - cfs_list_for_each_entry_safe(com, next, - &lfsck->li_list_scan, lc_link) { + list_for_each_entry_safe(com, next, + &lfsck->li_list_scan, lc_link) { if (!(com->lc_type & start->ls_active)) { rc = com->lc_ops->lfsck_post(env, com, 0, false); @@ -2180,21 +2244,40 @@ int lfsck_start(const struct lu_env *env, struct dt_device *key, if (type & start->ls_active) { com = __lfsck_component_find(lfsck, type, &lfsck->li_list_idle); - if (com != NULL) { + if (com != NULL) /* The component status will be updated * when its prep() is called later by * the LFSCK main engine. */ - cfs_list_del_init(&com->lc_link); - cfs_list_add_tail(&com->lc_link, - &lfsck->li_list_scan); - } + list_move_tail(&com->lc_link, + &lfsck->li_list_scan); start->ls_active &= ~type; } type <<= 1; } } - cfs_list_for_each_entry(com, &lfsck->li_list_scan, lc_link) { + if (list_empty(&lfsck->li_list_scan)) { + /* The speed limit will be used to control both the LFSCK and + * low layer scrub (if applied), need to be handled firstly. */ + if (start->ls_valid & LSV_SPEED_LIMIT) { + if (__lfsck_set_speed(lfsck, start->ls_speed_limit)) { + rc = lfsck_bookmark_store(env, lfsck); + if (rc != 0) + GOTO(out, rc); + } + } + + goto trigger; + } + + if (start->ls_flags & LPF_RESET) + flags |= DOIF_RESET; + + rc = lfsck_set_param(env, lfsck, start, !!(flags & DOIF_RESET)); + if (rc != 0) + GOTO(out, rc); + + list_for_each_entry(com, &lfsck->li_list_scan, lc_link) { start->ls_active |= com->lc_type; if (flags & DOIF_RESET) { rc = com->lc_ops->lfsck_reset(env, com, false); @@ -2205,18 +2288,22 @@ int lfsck_start(const struct lu_env *env, struct dt_device *key, trigger: lfsck->li_args_dir = LUDA_64BITHASH | LUDA_VERIFY; - if (bk->lb_param & LPF_DRYRUN) { + if (bk->lb_param & LPF_DRYRUN) lfsck->li_args_dir |= LUDA_VERIFY_DRYRUN; - valid |= DOIV_DRYRUN; - flags |= DOIF_DRYRUN; - } - if (bk->lb_param & LPF_FAILOUT) { + if (start != NULL && start->ls_valid & LSV_ERROR_HANDLE) { valid |= DOIV_ERROR_HANDLE; - flags |= DOIF_FAILOUT; + if (start->ls_flags & LPF_FAILOUT) + flags |= DOIF_FAILOUT; + } + + if (start != NULL && start->ls_valid & LSV_DRYRUN) { + valid |= DOIV_DRYRUN; + if (start->ls_flags & LPF_DRYRUN) + flags |= DOIF_DRYRUN; } - if (!cfs_list_empty(&lfsck->li_list_scan)) + if (!list_empty(&lfsck->li_list_scan)) flags |= DOIF_OUTUSED; lfsck->li_args_oit = (flags << DT_OTABLE_IT_FLAGS_SHIFT) | valid; @@ -2225,9 +2312,11 @@ trigger: if (IS_ERR(lta)) GOTO(out, rc = PTR_ERR(lta)); - rc = PTR_ERR(kthread_run(lfsck_master_engine, lta, "lfsck")); - if (IS_ERR_VALUE(rc)) { - CERROR("%s: cannot start LFSCK thread: rc = %ld\n", + __lfsck_set_speed(lfsck, bk->lb_speed_limit); + task = kthread_run(lfsck_master_engine, lta, "lfsck"); + if (IS_ERR(task)) { + rc = PTR_ERR(task); + CERROR("%s: cannot start LFSCK thread: rc = %d\n", lfsck_lfsck2name(lfsck), rc); lfsck_thread_args_fini(lta); @@ -2310,9 +2399,10 @@ int lfsck_stop(const struct lu_env *env, struct dt_device *key, mutex_lock(&lfsck->li_mutex); spin_lock(&lfsck->li_lock); + /* no error if LFSCK is already stopped, or was never started */ if (thread_is_init(thread) || thread_is_stopped(thread)) { spin_unlock(&lfsck->li_lock); - GOTO(out, rc = -EALREADY); + GOTO(out, rc = 0); } if (stop != NULL) { @@ -2454,7 +2544,7 @@ int lfsck_register(const struct lu_env *env, struct dt_device *key, { struct lfsck_instance *lfsck; struct dt_object *root = NULL; - struct dt_object *obj; + struct dt_object *obj = NULL; struct lu_fid *fid = &lfsck_env_info(env)->lti_fid; int rc; ENTRY; @@ -2469,11 +2559,11 @@ int lfsck_register(const struct lu_env *env, struct dt_device *key, mutex_init(&lfsck->li_mutex); spin_lock_init(&lfsck->li_lock); - CFS_INIT_LIST_HEAD(&lfsck->li_link); - CFS_INIT_LIST_HEAD(&lfsck->li_list_scan); - CFS_INIT_LIST_HEAD(&lfsck->li_list_dir); - CFS_INIT_LIST_HEAD(&lfsck->li_list_double_scan); - CFS_INIT_LIST_HEAD(&lfsck->li_list_idle); + INIT_LIST_HEAD(&lfsck->li_link); + INIT_LIST_HEAD(&lfsck->li_list_scan); + INIT_LIST_HEAD(&lfsck->li_list_dir); + INIT_LIST_HEAD(&lfsck->li_list_double_scan); + INIT_LIST_HEAD(&lfsck->li_list_idle); atomic_set(&lfsck->li_ref, 1); atomic_set(&lfsck->li_double_scan_count, 0); init_waitqueue_head(&lfsck->li_thread.t_ctl_waitq); @@ -2494,7 +2584,7 @@ int lfsck_register(const struct lu_env *env, struct dt_device *key, fid->f_seq = FID_SEQ_LOCAL_NAME; fid->f_oid = 1; fid->f_ver = 0; - rc = local_oid_storage_init(env, lfsck->li_bottom, fid, &lfsck->li_los); + rc = local_oid_storage_init(env, key, fid, &lfsck->li_los); if (rc != 0) GOTO(out, rc); @@ -2502,7 +2592,7 @@ int lfsck_register(const struct lu_env *env, struct dt_device *key, if (rc != 0) GOTO(out, rc); - root = dt_locate(env, lfsck->li_bottom, fid); + root = dt_locate(env, key, fid); if (IS_ERR(root)) GOTO(out, rc = PTR_ERR(root)); @@ -2512,30 +2602,72 @@ int lfsck_register(const struct lu_env *env, struct dt_device *key, lfsck->li_local_root_fid = *fid; if (master) { lfsck->li_master = 1; - if (lfsck_dev_idx(lfsck->li_bottom) == 0) { + if (lfsck_dev_idx(key) == 0) { + struct lu_fid *pfid = &lfsck_env_info(env)->lti_fid2; + const struct lu_name *cname; + rc = dt_lookup(env, root, (struct dt_rec *)(&lfsck->li_global_root_fid), (const struct dt_key *)"ROOT", BYPASS_CAPA); if (rc != 0) GOTO(out, rc); + + obj = dt_locate(env, key, &lfsck->li_global_root_fid); + if (IS_ERR(obj)) + GOTO(out, rc = PTR_ERR(obj)); + + rc = dt_lookup(env, obj, (struct dt_rec *)fid, + (const struct dt_key *)dotlustre, BYPASS_CAPA); + if (rc != 0) + GOTO(out, rc); + + lu_object_put(env, &obj->do_lu); + obj = dt_locate(env, key, fid); + if (IS_ERR(obj)) + GOTO(out, rc = PTR_ERR(obj)); + + cname = lfsck_name_get_const(env, dotlustre, + strlen(dotlustre)); + rc = lfsck_verify_linkea(env, key, obj, cname, + &lfsck->li_global_root_fid); + if (rc != 0) + GOTO(out, rc); + + *pfid = *fid; + rc = dt_lookup(env, obj, (struct dt_rec *)fid, + (const struct dt_key *)lostfound, + BYPASS_CAPA); + if (rc != 0) + GOTO(out, rc); + + lu_object_put(env, &obj->do_lu); + obj = dt_locate(env, key, fid); + if (IS_ERR(obj)) + GOTO(out, rc = PTR_ERR(obj)); + + cname = lfsck_name_get_const(env, lostfound, + strlen(lostfound)); + rc = lfsck_verify_linkea(env, key, obj, cname, pfid); + if (rc != 0) + GOTO(out, rc); + + lu_object_put(env, &obj->do_lu); + obj = NULL; } } fid->f_seq = FID_SEQ_LOCAL_FILE; fid->f_oid = OTABLE_IT_OID; fid->f_ver = 0; - obj = dt_locate(env, lfsck->li_bottom, fid); + obj = dt_locate(env, key, fid); if (IS_ERR(obj)) GOTO(out, rc = PTR_ERR(obj)); + lu_object_get(&obj->do_lu); lfsck->li_obj_oit = obj; rc = obj->do_ops->do_index_try(env, obj, &dt_otable_features); - if (rc != 0) { - if (rc == -ENOTSUPP) - GOTO(add, rc = 0); - + if (rc != 0) GOTO(out, rc); - } rc = lfsck_bookmark_setup(env, lfsck); if (rc != 0) @@ -2557,11 +2689,12 @@ int lfsck_register(const struct lu_env *env, struct dt_device *key, /* XXX: more LFSCK components initialization to be added here. */ -add: rc = lfsck_instance_add(lfsck); if (rc == 0) rc = lfsck_add_target_from_orphan(env, lfsck); out: + if (obj != NULL && !IS_ERR(obj)) + lu_object_put(env, &obj->do_lu); if (root != NULL && !IS_ERR(root)) lu_object_put(env, &root->do_lu); if (rc != 0) @@ -2632,7 +2765,7 @@ void lfsck_del_target(const struct lu_env *env, struct dt_device *key, { struct lfsck_instance *lfsck; struct lfsck_tgt_descs *ltds; - struct lfsck_tgt_desc *ltd = NULL; + struct lfsck_tgt_desc *ltd; struct list_head *head; if (for_ost) @@ -2651,6 +2784,7 @@ void lfsck_del_target(const struct lu_env *env, struct dt_device *key, } } + ltd = NULL; lfsck = __lfsck_instance_find(key, true, false); spin_unlock(&lfsck_instance_lock); if (unlikely(lfsck == NULL)) @@ -2682,7 +2816,7 @@ unlock: if (for_ost) head = &lfsck->li_ost_descs.ltd_orphan; else - head = &lfsck->li_ost_descs.ltd_orphan; + head = &lfsck->li_mdt_descs.ltd_orphan; list_for_each_entry(ltd, head, ltd_orphan_list) { if (ltd->ltd_tgt == tgt) { @@ -2697,7 +2831,7 @@ unlock: spin_lock(<ds->ltd_lock); ltd->ltd_dead = 1; spin_unlock(<ds->ltd_lock); - lfsck_stop_notify(env, lfsck, ltds, ltd, LT_LAYOUT); + lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_LAYOUT); lfsck_tgt_put(ltd); } @@ -2709,6 +2843,7 @@ static int __init lfsck_init(void) { int rc; + INIT_LIST_HEAD(&lfsck_instance_list); INIT_LIST_HEAD(&lfsck_ost_orphan_list); INIT_LIST_HEAD(&lfsck_mdt_orphan_list); lfsck_key_init_generic(&lfsck_thread_key, NULL); @@ -2726,7 +2861,7 @@ static void __exit lfsck_exit(void) struct lfsck_tgt_desc *ltd; struct lfsck_tgt_desc *next; - LASSERT(cfs_list_empty(&lfsck_instance_list)); + LASSERT(list_empty(&lfsck_instance_list)); list_for_each_entry_safe(ltd, next, &lfsck_ost_orphan_list, ltd_orphan_list) {