+/**
+ * Remove the striped directory's master LMV EA and mark it as read-only.
+ *
+ * Take ldlm lock on the striped directory before calling the
+ * lfsck_disable_master_lmv().
+ *
+ * \param[in] env pointer to the thread context
+ * \param[in] com pointer to the lfsck component
+ * \param[in] lnr pointer to the namespace request that contains the
+ * striped directory to be handled and other information
+ *
+ * \retval positive number if nothing to be done
+ * \retval zero for succeed
+ * \retval negative error number on failure
+ */
+static int lfsck_remove_lmv(const struct lu_env *env,
+ struct lfsck_component *com,
+ struct lfsck_namespace_req *lnr)
+{
+ struct dt_object *obj = lnr->lnr_obj;
+ struct lustre_handle lh = { 0 };
+ int rc;
+
+ lnr->lnr_lmv->ll_ignore = 1;
+ rc = lfsck_ibits_lock(env, com->lc_lfsck, obj, &lh,
+ MDS_INODELOCK_UPDATE | MDS_INODELOCK_XATTR,
+ LCK_EX);
+ if (rc == 0) {
+ rc = lfsck_disable_master_lmv(env, com, obj, true);
+ lfsck_ibits_unlock(&lh, LCK_EX);
+ }
+
+ return rc;
+}
+
+/**
+ * Remove the name entry from the striped directory's master MDT-object.
+ *
+ * \param[in] env pointer to the thread context
+ * \param[in] com pointer to the lfsck component
+ * \param[in] dir pointer to the striped directory
+ * \param[in] fid the shard's FID which name entry will be removed
+ * \param[in] index the shard's index which name entry will be removed
+ *
+ * \retval positive number for repaired successfully
+ * \retval 0 if nothing to be repaired
+ * \retval negative error number on failure
+ */
+static int lfsck_remove_dirent(const struct lu_env *env,
+ struct lfsck_component *com,
+ struct dt_object *dir,
+ const struct lu_fid *fid, __u32 index)
+{
+ struct lfsck_thread_info *info = lfsck_env_info(env);
+ struct dt_object *obj;
+ int rc;
+
+ snprintf(info->lti_tmpbuf2, sizeof(info->lti_tmpbuf2), DFID":%u",
+ PFID(fid), index);
+ obj = lfsck_object_find_by_dev(env, com->lc_lfsck->li_bottom, fid);
+ if (IS_ERR(obj))
+ return PTR_ERR(obj);
+
+ rc = lfsck_namespace_repair_dirent(env, com, dir, obj,
+ info->lti_tmpbuf2, info->lti_tmpbuf2,
+ S_IFDIR, false, false);
+ lfsck_object_put(env, obj);
+ if (rc > 0) {
+ struct lfsck_namespace *ns = com->lc_file_ram;
+
+ ns->ln_dirent_repaired++;
+ }
+
+ return rc;
+}
+
+/**
+ * Remove old shard's name entry and refill the @lslr slot with new shard.
+ *
+ * Some old shard held the specified @lslr slot, but it is an invalid shard.
+ * This function will remove the bad shard's name entry, and refill the @lslr
+ * slot with the new shard.
+ *
+ * \param[in] env pointer to the thread context
+ * \param[in] com pointer to the lfsck component
+ * \param[in] lslr pointer to lfsck_disable_master_lmv slot which content
+ * will be replaced by the given information
+ * \param[in] lnr contain the shard's FID to be used to fill the
+ * @lslr slot, it also records the known max filled index
+ * and the known max stripe count
+ * \param[in] lmv contain the slave LMV EA to be used to fill the
+ * @lslr slot
+ * \param[in] index the old shard's index in the striped directory
+ * \param[in] flags the new shard's flags in the @lslr slot
+ *
+ * \retval zero for succeed
+ * \retval negative error number on failure
+ */
+static int lfsck_replace_lmv(const struct lu_env *env,
+ struct lfsck_component *com,
+ struct lfsck_slave_lmv_rec *lslr,
+ struct lfsck_namespace_req *lnr,
+ struct lmv_mds_md_v1 *lmv,
+ __u32 index, __u32 flags)
+{
+ int rc;
+
+ rc = lfsck_remove_dirent(env, com, lnr->lnr_obj,
+ &lslr->lslr_fid, index);
+ if (rc < 0)
+ return rc;
+
+ lslr->lslr_fid = lnr->lnr_fid;
+ lslr->lslr_flags = flags;
+ if (lmv != NULL) {
+ struct lfsck_lmv *llmv = lnr->lnr_lmv;
+
+ lslr->lslr_stripe_count = lmv->lmv_stripe_count;
+ lslr->lslr_index = lmv->lmv_master_mdt_index;
+ lslr->lslr_hash_type = lmv->lmv_hash_type;
+
+ if (flags == LSLF_NONE &&
+ llmv->ll_hash_type == LMV_HASH_TYPE_UNKNOWN &&
+ lmv_is_known_hash_type(lmv->lmv_hash_type))
+ llmv->ll_hash_type = lmv->lmv_hash_type;
+
+ if (flags == LSLF_NONE &&
+ lslr->lslr_stripe_count <= LFSCK_LMV_MAX_STRIPES &&
+ llmv->ll_max_stripe_count < lslr->lslr_stripe_count)
+ llmv->ll_max_stripe_count = lslr->lslr_stripe_count;
+ } else {
+ lslr->lslr_stripe_count = 0;
+ lslr->lslr_index = 0;
+ lslr->lslr_hash_type = 0;
+ }
+
+ return 0;
+}
+
+/**
+ * Record the slave LMV EA in the lfsck_lmv::ll_lslr.
+ *
+ * If the lfsck_lmv::ll_lslr slot corresponding to the given @shard_idx is free,
+ * then fill the slot with the given @lnr/@lmv/@flags directly (maybe need to
+ * extend the lfsck_lmv::ll_lslr buffer).
+ *
+ * If the lfsck_lmv::ll_lslr slot corresponding to the given @shard_idx is taken
+ * by other shard, then the LFSCK will try to resolve the conflict by checking
+ * the two conflict shards' flags, and try other possible slot (if one of them
+ * claims another possible @shard_idx).
+ *
+ * 1) If one of the two conflict shards can be recorded in another slot, then
+ * it is OK, go ahead. Otherwise,
+ *
+ * 2) If one of them is dangling name entry, then remove (one of) the dangling
+ * name entry (and replace related @lslr slot if needed). Otherwise,
+ *
+ * 3) If one of them has no slave LMV EA, then check whether the master LMV
+ * EA has ever been lost and re-generated (LMV_HASH_FLAG_LOST_LMV in the
+ * master LMV EA).
+ *
+ * 3.1) If yes, then it is possible that such object is not a real shard of
+ * the striped directory, instead, it was created by someone after the
+ * master LMV EA lost with the name that matches the shard naming rule.
+ * Then the LFSCK will remove the master LMV EA and mark the striped
+ * directory as read-only to allow those non-shard files to be visible
+ * to client.
+ *
+ * 3.2) If no, then remove (one of) the object what has no slave LMV EA.
+ *
+ * 4) If all above efforts cannot work, then the LFSCK cannot know how to
+ * recover the striped directory. To make the administrator can see the
+ * conflicts, the LFSCK will remove the master LMV EA and mark the striped
+ * directory as read-only.
+ *
+ * This function may be called recursively, to prevent overflow, we define
+ * LFSCK_REC_LMV_MAX_DEPTH to restrict the recursive call depth.
+ *
+ * \param[in] env pointer to the thread context
+ * \param[in] com pointer to the lfsck component
+ * \param[in] lnr contain the shard's FID to fill the @lslr slot,
+ * it also records the known max filled index and
+ * the known max stripe count
+ * \param[in] lmv pointer to the slave LMV EA to be recorded
+ * \param[in] shard_idx the shard's index used for locating the @lslr slot,
+ * it can be the index stored in the shard's name,
+ * it also can be the index stored in the slave LMV EA
+ * (for recursive case)
+ * \param[in] flags the shard's flags to be recorded in the @lslr slot
+ * to indicate the shard status, such as whether has
+ * slave LMV EA, whether dangling name entry, whether
+ * the name entry and slave LMV EA unmatched, and ect
+ * \param[in] flags2 when be called recursively, the @flags2 tells the
+ * former conflict shard's flags in the @lslr slot.
+ * \param[in,out] depth To prevent to be called recurisively too deep,
+ * we define the max depth can be called recursively
+ * (LFSCK_REC_LMV_MAX_DEPTH)
+ *
+ * \retval zero for succeed
+ * \retval "-ERANGE" for invalid @shard_idx
+ * \retval "-EEXIST" for the required lslr slot has been
+ * occupied by other shard
+ * \retval other negative error number on failure
+ */
+static int lfsck_record_lmv(const struct lu_env *env,
+ struct lfsck_component *com,
+ struct lfsck_namespace_req *lnr,
+ struct lmv_mds_md_v1 *lmv, __u32 shard_idx,
+ __u32 flags, __u32 flags2, __u32 *depth)
+{
+ struct lfsck_instance *lfsck = com->lc_lfsck;
+ struct lfsck_lmv *llmv = lnr->lnr_lmv;
+ struct dt_object *dir = lnr->lnr_obj;
+ const struct lu_fid *fid = &lnr->lnr_fid;
+ struct lfsck_slave_lmv_rec *lslr;
+ struct lfsck_rec_lmv_save *lrls;
+ int index = shard_idx;
+ int rc = 0;
+ ENTRY;
+
+ CDEBUG(D_LFSCK, "%s: record slave LMV EA for the striped directory "
+ DFID": shard = "DFID", index = %u, flags = %u, flags2 = %u, "
+ "depth = %d\n", lfsck_lfsck2name(lfsck),
+ PFID(lfsck_dto2fid(dir)), PFID(fid),
+ index, flags, flags2, *depth);
+
+ if (index < 0 || index >= LFSCK_LMV_MAX_STRIPES)
+ RETURN(-ERANGE);
+
+ if (index >= llmv->ll_stripes_allocated) {
+ struct lfsck_slave_lmv_rec *new_lslr;
+ int new_stripes = index + 1;
+ size_t old_size = sizeof(*lslr) * llmv->ll_stripes_allocated;
+
+ OBD_ALLOC_LARGE(new_lslr, sizeof(*new_lslr) * new_stripes);
+ if (new_lslr == NULL) {
+ llmv->ll_failed = 1;
+
+ RETURN(-ENOMEM);
+ }
+
+ memcpy(new_lslr, llmv->ll_lslr, old_size);
+ OBD_FREE_LARGE(llmv->ll_lslr, old_size);
+ llmv->ll_stripes_allocated = new_stripes;
+ llmv->ll_lslr = new_lslr;
+ }
+
+ lslr = llmv->ll_lslr + index;
+ if (unlikely(lu_fid_eq(&lslr->lslr_fid, fid)))
+ RETURN(0);
+
+ if (fid_is_zero(&lslr->lslr_fid)) {
+ lslr->lslr_fid = *fid;
+ if (lmv != NULL) {
+ lslr->lslr_stripe_count = lmv->lmv_stripe_count;
+ lslr->lslr_index = lmv->lmv_master_mdt_index;
+ lslr->lslr_hash_type = lmv->lmv_hash_type;
+
+ if (flags == LSLF_NONE &&
+ llmv->ll_hash_type == LMV_HASH_TYPE_UNKNOWN &&
+ lmv_is_known_hash_type(lmv->lmv_hash_type))
+ llmv->ll_hash_type = lmv->lmv_hash_type;
+
+ if (flags == LSLF_NONE &&
+ lslr->lslr_stripe_count <= LFSCK_LMV_MAX_STRIPES &&
+ llmv->ll_max_stripe_count < lslr->lslr_stripe_count)
+ llmv->ll_max_stripe_count =
+ lslr->lslr_stripe_count;
+ }
+
+ lslr->lslr_flags = flags;
+ llmv->ll_stripes_filled++;
+
+ if (llmv->ll_max_filled_off < index)
+ llmv->ll_max_filled_off = index;
+
+ RETURN(0);
+ }
+
+ (*depth)++;
+ if (flags != LSLF_BAD_INDEX2)
+ LASSERTF(*depth == 1, "depth = %d\n", *depth);
+
+ /* Handle conflict cases. */
+ switch (lslr->lslr_flags) {
+ case LSLF_NONE:
+ case LSLF_BAD_INDEX2:
+ /* The existing one is a normal valid object. */
+ switch (flags) {
+ case LSLF_NONE:
+ /* The two 'valid' name entries claims the same
+ * index, the LFSCK cannot distinguish which one
+ * is correct. Then remove the master LMV EA to
+ * make all shards to be visible to client, and
+ * mark the master MDT-object as read-only. The
+ * administrator can handle the conflict with
+ * more human knowledge. */
+ rc = lfsck_remove_lmv(env, com, lnr);
+ break;
+ case LSLF_BAD_INDEX2:
+ GOTO(out, rc = -EEXIST);
+ case LSLF_NO_LMVEA:
+
+no_lmvea:
+ if (llmv->ll_lmv.lmv_hash_type &
+ LMV_HASH_FLAG_LOST_LMV) {
+ /* If the master LMV EA was re-generated
+ * by the former LFSCK reparation, and
+ * before such reparation, someone has
+ * created the conflict object, but the
+ * LFSCK did not detect such conflict,
+ * then we have to remove the master
+ * LMV EA and mark the master MDT-object
+ * as read-only. The administrator can
+ * handle the conflict with more human
+ * knowledge. */
+ rc = lfsck_remove_lmv(env, com, lnr);
+ } else {
+ /* Otherwise, remove the current name entry,
+ * and add its FID in the LFSCK tracing file
+ * for further processing. */
+ rc = lfsck_namespace_trace_update(env, com, fid,
+ LNTF_CHECK_PARENT, true);
+ if (rc == 0)
+ rc = lfsck_remove_dirent(env, com, dir,
+ fid, index);
+ }
+
+ break;
+ case LSLF_DANGLING:
+ /* Remove the current dangling name entry. */
+ rc = lfsck_remove_dirent(env, com, dir, fid, index);
+ break;
+ case LSLF_BAD_INDEX1:
+ index = lmv->lmv_master_mdt_index;
+ lmv->lmv_master_mdt_index = shard_idx;
+ /* The name entry claims an index that is conflict
+ * with a valid existing name entry, then try the
+ * index in the lmv recursively. */
+ rc = lfsck_record_lmv(env, com, lnr, lmv, index,
+ LSLF_BAD_INDEX2, lslr->lslr_flags, depth);
+ lmv->lmv_master_mdt_index = index;
+ if (rc == -ERANGE || rc == -EEXIST)
+ /* The index in the lmv is invalid or
+ * also conflict with other. Then we do
+ * not know how to resolve the conflict.
+ * We will handle it as handle the case
+ * of 'LSLF_NONE' vs 'LSLF_NONE'. */
+ rc = lfsck_remove_lmv(env, com, lnr);
+
+ break;
+ default:
+ break;
+ }
+
+ break;
+ case LSLF_NO_LMVEA:
+ /* The existing one has no slave LMV EA. */
+ switch (flags) {
+ case LSLF_NONE:
+
+none:
+ if (llmv->ll_lmv.lmv_hash_type &
+ LMV_HASH_FLAG_LOST_LMV) {
+ /* If the master LMV EA was re-generated
+ * by the former LFSCK reparation, and
+ * before such reparation, someone has
+ * created the conflict object, but the
+ * LFSCK did not detect such conflict,
+ * then we have to remove the master
+ * LMV EA and mark the master MDT-object
+ * as read-only. The administrator can
+ * handle the conflict with more human
+ * knowledge. */
+ rc = lfsck_remove_lmv(env, com, lnr);
+ } else {
+ lrls = &lfsck->li_rec_lmv_save[*depth - 1];
+ lrls->lrls_fid = lslr->lslr_fid;
+ /* Otherwise, remove the existing name entry,
+ * and add its FID in the LFSCK tracing file
+ * for further processing. Refill the slot
+ * with current slave LMV EA. */
+ rc = lfsck_namespace_trace_update(env,
+ com, &lrls->lrls_fid,
+ LNTF_CHECK_PARENT, true);
+ if (rc == 0)
+ rc = lfsck_replace_lmv(env, com, lslr,
+ lnr, lmv, index, flags);
+ }
+
+ break;
+ case LSLF_BAD_INDEX2:
+ if (flags2 >= lslr->lslr_flags)
+ GOTO(out, rc = -EEXIST);
+
+ goto none;
+ case LSLF_NO_LMVEA:
+ goto no_lmvea;
+ case LSLF_DANGLING:
+ /* Remove the current dangling name entry. */
+ rc = lfsck_remove_dirent(env, com, dir, fid, index);
+ break;
+ case LSLF_BAD_INDEX1:
+ index = lmv->lmv_master_mdt_index;
+ lmv->lmv_master_mdt_index = shard_idx;
+ /* The name entry claims an index that is conflict
+ * with a valid existing name entry, then try the
+ * index in the lmv recursively. */
+ rc = lfsck_record_lmv(env, com, lnr, lmv, index,
+ LSLF_BAD_INDEX2, lslr->lslr_flags, depth);
+ lmv->lmv_master_mdt_index = index;
+ if (rc == -ERANGE || rc == -EEXIST) {
+ index = shard_idx;
+ goto no_lmvea;
+ }
+
+ break;
+ default:
+ break;
+ }
+
+ break;
+ case LSLF_DANGLING:
+ /* The existing one is a dangling name entry. */
+ switch (flags) {
+ case LSLF_NONE:
+ case LSLF_BAD_INDEX2:
+ case LSLF_NO_LMVEA:
+ /* Remove the existing dangling name entry.
+ * Refill the lslr slot with the given LMV. */
+ rc = lfsck_replace_lmv(env, com, lslr, lnr,
+ lmv, index, flags);
+ break;
+ case LSLF_DANGLING:
+ /* Two dangling name entries conflict,
+ * remove the current one. */
+ rc = lfsck_remove_dirent(env, com, dir, fid, index);
+ break;
+ case LSLF_BAD_INDEX1:
+ index = lmv->lmv_master_mdt_index;
+ lmv->lmv_master_mdt_index = shard_idx;
+ /* The name entry claims an index that is conflict
+ * with a valid existing name entry, then try the
+ * index in the lmv recursively. */
+ rc = lfsck_record_lmv(env, com, lnr, lmv, index,
+ LSLF_BAD_INDEX2, lslr->lslr_flags, depth);
+ lmv->lmv_master_mdt_index = index;
+ if (rc == -ERANGE || rc == -EEXIST)
+ /* If the index in the lmv is invalid or
+ * also conflict with other, then remove
+ * the existing dangling name entry.
+ * Refill the lslr slot with the given LMV. */
+ rc = lfsck_replace_lmv(env, com, lslr, lnr,
+ lmv, shard_idx, flags);
+
+ break;
+ default:
+ break;
+ }
+
+ break;
+ case LSLF_BAD_INDEX1: {
+ if (*depth >= LFSCK_REC_LMV_MAX_DEPTH)
+ goto conflict;
+
+ lrls = &lfsck->li_rec_lmv_save[*depth - 1];
+ lrls->lrls_fid = lnr->lnr_fid;
+ lrls->lrls_lmv = *lmv;
+
+ lnr->lnr_fid = lslr->lslr_fid;
+ lmv->lmv_master_mdt_index = index;
+ lmv->lmv_stripe_count = lslr->lslr_stripe_count;
+ lmv->lmv_hash_type = lslr->lslr_hash_type;
+ index = lslr->lslr_index;
+
+ /* The existing one has another possible slot,
+ * try it recursively. */
+ rc = lfsck_record_lmv(env, com, lnr, lmv, index,
+ LSLF_BAD_INDEX2, flags, depth);
+ *lmv = lrls->lrls_lmv;
+ lnr->lnr_fid = lrls->lrls_fid;
+ index = shard_idx;
+ if (rc != 0) {
+ if (rc == -ERANGE || rc == -EEXIST)
+ goto conflict;
+
+ break;
+ }
+
+ lslr->lslr_fid = *fid;
+ lslr->lslr_flags = flags;
+ if (lmv != NULL) {
+ lslr->lslr_stripe_count = lmv->lmv_stripe_count;
+ lslr->lslr_index = lmv->lmv_master_mdt_index;
+ lslr->lslr_hash_type = lmv->lmv_hash_type;
+
+ if (flags == LSLF_NONE &&
+ llmv->ll_hash_type == LMV_HASH_TYPE_UNKNOWN &&
+ lmv_is_known_hash_type(lmv->lmv_hash_type))
+ llmv->ll_hash_type = lmv->lmv_hash_type;
+
+ if (flags == LSLF_NONE &&
+ lslr->lslr_stripe_count <= LFSCK_LMV_MAX_STRIPES &&
+ llmv->ll_max_stripe_count < lslr->lslr_stripe_count)
+ llmv->ll_max_stripe_count =
+ lslr->lslr_stripe_count;
+ } else {
+ lslr->lslr_stripe_count = 0;
+ lslr->lslr_index = 0;
+ lslr->lslr_hash_type = 0;
+ }
+
+ break;
+
+conflict:
+ switch (flags) {
+ case LSLF_NONE:
+ /* The two 'valid' name entries claims the same
+ * index, the LFSCK cannot distinguish which one
+ * is correct. Then remove the master LMV EA to
+ * make all shards to be visible to client, and
+ * mark the master MDT-object as read-only. The
+ * administrator can handle the conflict with
+ * more human knowledge. */
+ rc = lfsck_remove_lmv(env, com, lnr);
+ break;
+ case LSLF_BAD_INDEX2:
+ GOTO(out, rc = -EEXIST);
+ case LSLF_NO_LMVEA:
+ goto no_lmvea;
+ case LSLF_DANGLING:
+ /* Remove the current dangling name entry. */
+ rc = lfsck_remove_dirent(env, com, dir, fid, index);
+ break;
+ case LSLF_BAD_INDEX1:
+ index = lmv->lmv_master_mdt_index;
+ lmv->lmv_master_mdt_index = shard_idx;
+ /* The name entry claims an index that is conflict
+ * with a valid existing name entry, then try the
+ * index in the lmv recursively. */
+ rc = lfsck_record_lmv(env, com, lnr, lmv, index,
+ LSLF_BAD_INDEX2, lslr->lslr_flags, depth);
+ lmv->lmv_master_mdt_index = index;
+ if (rc == -ERANGE || rc == -EEXIST)
+ /* The index in the lmv is invalid or
+ * also conflict with other. Then we do
+ * not know how to resolve the conflict.
+ * We will handle it as handle the case
+ * of 'LSLF_NONE' vs 'LSLF_NONE'. */
+ rc = lfsck_remove_lmv(env, com, lnr);
+
+ break;
+ }
+
+ break;
+ }
+ default:
+ break;
+ }
+
+ if (rc < 0)
+ llmv->ll_failed = 1;
+
+ GOTO(out, rc);
+
+out:
+ (*depth)--;
+
+ return rc > 0 ? 0 : rc;
+}
+