+ * Append the FID for each shard of the striped directory after the
+ * given LMV EA header.
+ *
+ * To simplify striped directory and the consistency verification,
+ * we only store the LMV EA header on disk, for both master object
+ * and slave objects. When someone wants to know the whole LMV EA,
+ * such as client readdir(), we can build the entrie LMV EA on the
+ * MDT side (in RAM) via iterating the sub-directory entries that
+ * are contained in the master object of the stripe directory.
+ *
+ * For the master object of the striped directroy, the valid name
+ * for each shard is composed of the ${shard_FID}:${shard_idx}.
+ *
+ * There may be holes in the LMV EA if some shards' name entries
+ * are corrupted or lost.
+ *
+ * \param[in] env pointer to the thread context
+ * \param[in] lo pointer to the master object of the striped directory
+ * \param[in] buf pointer to the lu_buf which will hold the LMV EA
+ * \param[in] resize whether re-allocate the buffer if it is not big enough
+ *
+ * \retval positive size of the LMV EA
+ * \retval 0 for nothing to be loaded
+ * \retval negative error number on failure
+ */
+int lod_load_lmv_shards(const struct lu_env *env, struct lod_object *lo,
+ struct lu_buf *buf, bool resize)
+{
+ struct lu_dirent *ent =
+ (struct lu_dirent *)lod_env_info(env)->lti_key;
+ struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
+ struct dt_object *obj = dt_object_child(&lo->ldo_obj);
+ struct lmv_mds_md_v1 *lmv1 = buf->lb_buf;
+ struct dt_it *it;
+ const struct dt_it_ops *iops;
+ __u32 stripes;
+ __u32 magic = le32_to_cpu(lmv1->lmv_magic);
+ size_t lmv1_size;
+ int rc;
+ ENTRY;
+
+ /* If it is not a striped directory, then load nothing. */
+ if (magic != LMV_MAGIC_V1)
+ RETURN(0);
+
+ /* If it is in migration (or failure), then load nothing. */
+ if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
+ RETURN(0);
+
+ stripes = le32_to_cpu(lmv1->lmv_stripe_count);
+ if (stripes < 1)
+ RETURN(0);
+
+ rc = lmv_mds_md_size(stripes, magic);
+ if (rc < 0)
+ RETURN(rc);
+ lmv1_size = rc;
+ if (buf->lb_len < lmv1_size) {
+ struct lu_buf tbuf;
+
+ if (!resize)
+ RETURN(-ERANGE);
+
+ tbuf = *buf;
+ buf->lb_buf = NULL;
+ buf->lb_len = 0;
+ lu_buf_alloc(buf, lmv1_size);
+ lmv1 = buf->lb_buf;
+ if (lmv1 == NULL)
+ RETURN(-ENOMEM);
+
+ memcpy(buf->lb_buf, tbuf.lb_buf, tbuf.lb_len);
+ }
+
+ if (unlikely(!dt_try_as_dir(env, obj)))
+ RETURN(-ENOTDIR);
+
+ memset(&lmv1->lmv_stripe_fids[0], 0, stripes * sizeof(struct lu_fid));
+ iops = &obj->do_index_ops->dio_it;
+ it = iops->init(env, obj, 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) {
+ char name[FID_LEN + 2] = "";
+ struct lu_fid fid;
+ __u32 index;
+ int len;
+
+ rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
+ if (rc != 0)
+ break;
+
+ rc = -EIO;
+
+ fid_le_to_cpu(&fid, &ent->lde_fid);
+ 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;
+ }
+
+ len = snprintf(name, FID_LEN + 1, DFID":", PFID(&ent->lde_fid));
+ /* The ent->lde_name is composed of ${FID}:${index} */
+ if (ent->lde_namelen < len + 1 ||
+ memcmp(ent->lde_name, name, len) != 0) {
+ CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
+ "%s: invalid shard name %.*s with the FID "DFID
+ " for the striped directory "DFID", %s\n",
+ lod2obd(lod)->obd_name, ent->lde_namelen,
+ ent->lde_name, PFID(&fid),
+ PFID(lu_object_fid(&obj->do_lu)),
+ lod->lod_lmv_failout ? "failout" : "skip");
+
+ if (lod->lod_lmv_failout)
+ break;
+
+ goto next;
+ }
+
+ index = 0;
+ do {
+ if (ent->lde_name[len] < '0' ||
+ ent->lde_name[len] > '9') {
+ CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
+ "%s: invalid shard name %.*s with the "
+ "FID "DFID" for the striped directory "
+ DFID", %s\n",
+ lod2obd(lod)->obd_name, ent->lde_namelen,
+ ent->lde_name, PFID(&fid),
+ PFID(lu_object_fid(&obj->do_lu)),
+ lod->lod_lmv_failout ?
+ "failout" : "skip");
+
+ if (lod->lod_lmv_failout)
+ break;
+
+ goto next;
+ }
+
+ index = index * 10 + ent->lde_name[len++] - '0';
+ } while (len < ent->lde_namelen);
+
+ if (len == ent->lde_namelen) {
+ /* Out of LMV EA range. */
+ if (index >= stripes) {
+ CERROR("%s: the shard %.*s for the striped "
+ "directory "DFID" is out of the known "
+ "LMV EA range [0 - %u], failout\n",
+ lod2obd(lod)->obd_name, ent->lde_namelen,
+ ent->lde_name,
+ PFID(lu_object_fid(&obj->do_lu)),
+ stripes - 1);
+
+ break;
+ }
+
+ /* The slot has been occupied. */
+ if (!fid_is_zero(&lmv1->lmv_stripe_fids[index])) {
+ struct lu_fid fid0;
+
+ fid_le_to_cpu(&fid0,
+ &lmv1->lmv_stripe_fids[index]);
+ CERROR("%s: both the shard "DFID" and "DFID
+ " for the striped directory "DFID
+ " claim the same LMV EA slot at the "
+ "index %d, failout\n",
+ lod2obd(lod)->obd_name,
+ PFID(&fid0), PFID(&fid),
+ PFID(lu_object_fid(&obj->do_lu)), index);
+
+ break;
+ }
+
+ /* stored as LE mode */
+ lmv1->lmv_stripe_fids[index] = ent->lde_fid;
+
+next:
+ rc = iops->next(env, it);
+ }
+ }
+
+ iops->put(env, it);
+ iops->fini(env, it);
+
+ RETURN(rc > 0 ? lmv_mds_md_size(stripes, magic) : rc);
+}
+
+/**