+ LASSERT(buf != NULL && buf->lb_buf != NULL);
+ lum = buf->lb_buf;
+
+ CDEBUG(D_OTHER, "set default stripe_count # %u stripe_offset %d\n",
+ le32_to_cpu(lum->lum_stripe_count),
+ (int)le32_to_cpu(lum->lum_stripe_offset));
+
+ if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
+ le32_to_cpu(lum->lum_stripe_offset)) &&
+ le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
+ rc = lod_xattr_del_internal(env, dt, name, th);
+ if (rc == -ENODATA)
+ rc = 0;
+ } else {
+ rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
+ if (rc != 0)
+ RETURN(rc);
+ }
+
+ RETURN(rc);
+}
+
+/**
+ * Turn directory into a striped directory.
+ *
+ * During replay the client sends the striping created before MDT
+ * failure, then the layer above LOD sends this defined striping
+ * using ->do_xattr_set(), so LOD uses this method to replay creation
+ * of the stripes. Notice the original information for the striping
+ * (#stripes, FIDs, etc) was transferred in declare path.
+ *
+ * \param[in] env execution environment
+ * \param[in] dt the striped object
+ * \param[in] buf not used currently
+ * \param[in] name not used currently
+ * \param[in] fl xattr flag (see OSD API description)
+ * \param[in] th transaction handle
+ *
+ * \retval 0 on success
+ * \retval negative if failed
+ */
+static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
+ const struct lu_buf *buf, const char *name,
+ int fl, struct thandle *th)
+{
+ struct lod_object *lo = lod_dt_obj(dt);
+ struct lod_thread_info *info = lod_env_info(env);
+ struct lu_attr *attr = &info->lti_attr;
+ struct dt_object_format *dof = &info->lti_format;
+ struct lu_buf lmv_buf;
+ struct lu_buf slave_lmv_buf;
+ struct lmv_mds_md_v1 *lmm;
+ struct lmv_mds_md_v1 *slave_lmm = NULL;
+ struct dt_insert_rec *rec = &info->lti_dt_rec;
+ int i;
+ int rc;
+ ENTRY;
+
+ if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
+ RETURN(-ENOTDIR);
+
+ /* The stripes are supposed to be allocated in declare phase,
+ * if there are no stripes being allocated, it will skip */
+ if (lo->ldo_dir_stripe_count == 0)
+ RETURN(0);
+
+ rc = dt_attr_get(env, dt_object_child(dt), attr);
+ if (rc != 0)
+ RETURN(rc);
+
+ attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
+ LA_MODE | LA_UID | LA_GID | LA_TYPE | LA_PROJID;
+ dof->dof_type = DFT_DIR;
+
+ rc = lod_prep_lmv_md(env, dt, &lmv_buf);
+ if (rc != 0)
+ RETURN(rc);
+ lmm = lmv_buf.lb_buf;
+
+ OBD_ALLOC_PTR(slave_lmm);
+ if (slave_lmm == NULL)
+ RETURN(-ENOMEM);
+
+ lod_prep_slave_lmv_md(slave_lmm, lmm);
+ slave_lmv_buf.lb_buf = slave_lmm;
+ slave_lmv_buf.lb_len = sizeof(*slave_lmm);
+
+ rec->rec_type = S_IFDIR;
+ for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
+ struct dt_object *dto = lo->ldo_stripe[i];
+ char *stripe_name = info->lti_key;
+ struct lu_name *sname;
+ struct linkea_data ldata = { NULL };
+ struct lu_buf linkea_buf;
+
+ /* if it's source stripe of migrating directory, don't create */
+ if (!((lo->ldo_dir_hash_type & LMV_HASH_FLAG_MIGRATION) &&
+ i >= lo->ldo_dir_migrate_offset)) {
+ dt_write_lock(env, dto, MOR_TGT_CHILD);
+ rc = lod_sub_create(env, dto, attr, NULL, dof, th);
+ if (rc != 0) {
+ dt_write_unlock(env, dto);
+ GOTO(out, rc);
+ }
+
+ rc = lod_sub_ref_add(env, dto, th);
+ dt_write_unlock(env, dto);
+ if (rc != 0)
+ GOTO(out, rc);
+
+ rec->rec_fid = lu_object_fid(&dto->do_lu);
+ rc = lod_sub_insert(env, dto,
+ (const struct dt_rec *)rec,
+ (const struct dt_key *)dot, th);
+ if (rc != 0)
+ GOTO(out, rc);
+ }
+
+ rec->rec_fid = lu_object_fid(&dt->do_lu);
+ rc = lod_sub_insert(env, dto, (struct dt_rec *)rec,
+ (const struct dt_key *)dotdot, th);
+ if (rc != 0)
+ GOTO(out, rc);
+
+ if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
+ cfs_fail_val != i) {
+ if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
+ cfs_fail_val == i)
+ slave_lmm->lmv_master_mdt_index =
+ cpu_to_le32(i + 1);
+ else
+ slave_lmm->lmv_master_mdt_index =
+ cpu_to_le32(i);
+
+ rc = lod_sub_xattr_set(env, dto, &slave_lmv_buf,
+ XATTR_NAME_LMV, fl, th);
+ if (rc != 0)
+ GOTO(out, rc);
+ }
+
+ if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
+ cfs_fail_val == i)
+ snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
+ PFID(lu_object_fid(&dto->do_lu)), i + 1);
+ else
+ snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
+ PFID(lu_object_fid(&dto->do_lu)), i);
+
+ sname = lod_name_get(env, stripe_name, strlen(stripe_name));
+ rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
+ sname, lu_object_fid(&dt->do_lu));
+ if (rc != 0)
+ GOTO(out, rc);
+
+ linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
+ linkea_buf.lb_len = ldata.ld_leh->leh_len;
+ rc = lod_sub_xattr_set(env, dto, &linkea_buf,
+ XATTR_NAME_LINK, 0, th);
+ if (rc != 0)
+ GOTO(out, rc);
+
+ rec->rec_fid = lu_object_fid(&dto->do_lu);
+ rc = lod_sub_insert(env, dt_object_child(dt),
+ (const struct dt_rec *)rec,
+ (const struct dt_key *)stripe_name, th);
+ if (rc != 0)
+ GOTO(out, rc);
+
+ rc = lod_sub_ref_add(env, dt_object_child(dt), th);
+ if (rc != 0)
+ GOTO(out, rc);
+ }
+
+ if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
+ rc = lod_sub_xattr_set(env, dt_object_child(dt),
+ &lmv_buf, XATTR_NAME_LMV, fl, th);
+out:
+ if (slave_lmm != NULL)
+ OBD_FREE_PTR(slave_lmm);
+
+ RETURN(rc);
+}
+
+/**
+ * Helper function to declare/execute creation of a striped directory
+ *
+ * Called in declare/create object path, prepare striping for a directory
+ * and prepare defaults data striping for the objects to be created in
+ * that directory. Notice the function calls "declaration" or "execution"
+ * methods depending on \a declare param. This is a consequence of the
+ * current approach while we don't have natural distributed transactions:
+ * we basically execute non-local updates in the declare phase. So, the
+ * arguments for the both phases are the same and this is the reason for
+ * this function to exist.
+ *
+ * \param[in] env execution environment
+ * \param[in] dt object
+ * \param[in] attr attributes the stripes will be created with
+ * \param[in] lmu lmv_user_md if MDT indices are specified
+ * \param[in] dof format of stripes (see OSD API description)
+ * \param[in] th transaction handle
+ * \param[in] declare where to call "declare" or "execute" methods
+ *
+ * \retval 0 on success
+ * \retval negative if failed
+ */
+static int lod_dir_striping_create_internal(const struct lu_env *env,
+ struct dt_object *dt,
+ struct lu_attr *attr,
+ const struct lu_buf *lmu,
+ struct dt_object_format *dof,
+ struct thandle *th,
+ bool declare)
+{
+ struct lod_thread_info *info = lod_env_info(env);
+ struct lod_object *lo = lod_dt_obj(dt);
+ const struct lod_default_striping *lds = lo->ldo_def_striping;
+ int rc;
+ ENTRY;
+
+ LASSERT(ergo(lds != NULL,
+ lds->lds_def_striping_set ||
+ lds->lds_dir_def_striping_set));
+
+ if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripe_count,
+ lo->ldo_dir_stripe_offset)) {
+ if (!lmu) {
+ struct lmv_user_md_v1 *v1 = info->lti_ea_store;
+ int stripe_count = lo->ldo_dir_stripe_count;
+
+ if (info->lti_ea_store_size < sizeof(*v1)) {
+ rc = lod_ea_store_resize(info, sizeof(*v1));
+ if (rc != 0)
+ RETURN(rc);
+ v1 = info->lti_ea_store;
+ }
+
+ memset(v1, 0, sizeof(*v1));
+ v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
+ v1->lum_stripe_count = cpu_to_le32(stripe_count);
+ v1->lum_stripe_offset =
+ cpu_to_le32(lo->ldo_dir_stripe_offset);
+
+ info->lti_buf.lb_buf = v1;
+ info->lti_buf.lb_len = sizeof(*v1);
+ lmu = &info->lti_buf;
+ }
+
+ if (declare)
+ rc = lod_declare_xattr_set_lmv(env, dt, attr, lmu, dof,
+ th);
+ else
+ rc = lod_xattr_set_lmv(env, dt, lmu, XATTR_NAME_LMV, 0,
+ th);
+ if (rc != 0)
+ RETURN(rc);
+ }
+
+ /* Transfer default LMV striping from the parent */
+ if (lds != NULL && lds->lds_dir_def_striping_set &&
+ !LMVEA_DELETE_VALUES(lds->lds_dir_def_stripe_count,
+ lds->lds_dir_def_stripe_offset)) {
+ struct lmv_user_md_v1 *v1 = info->lti_ea_store;
+
+ if (info->lti_ea_store_size < sizeof(*v1)) {
+ rc = lod_ea_store_resize(info, sizeof(*v1));
+ if (rc != 0)
+ RETURN(rc);
+ v1 = info->lti_ea_store;
+ }
+
+ memset(v1, 0, sizeof(*v1));
+ v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
+ v1->lum_stripe_count =
+ cpu_to_le32(lds->lds_dir_def_stripe_count);
+ v1->lum_stripe_offset =
+ cpu_to_le32(lds->lds_dir_def_stripe_offset);
+ v1->lum_hash_type =
+ cpu_to_le32(lds->lds_dir_def_hash_type);
+
+ info->lti_buf.lb_buf = v1;
+ info->lti_buf.lb_len = sizeof(*v1);
+ if (declare)
+ rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
+ XATTR_NAME_DEFAULT_LMV,
+ 0, th);
+ else
+ rc = lod_xattr_set_default_lmv_on_dir(env, dt,
+ &info->lti_buf,
+ XATTR_NAME_DEFAULT_LMV, 0,
+ th);
+ if (rc != 0)
+ RETURN(rc);
+ }
+
+ /* Transfer default LOV striping from the parent */
+ if (lds != NULL && lds->lds_def_striping_set &&
+ lds->lds_def_comp_cnt != 0) {
+ struct lov_mds_md *lmm;
+ int lmm_size = lod_comp_md_size(lo, true);
+
+ if (info->lti_ea_store_size < lmm_size) {
+ rc = lod_ea_store_resize(info, lmm_size);
+ if (rc != 0)
+ RETURN(rc);
+ }
+ lmm = info->lti_ea_store;
+
+ rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
+ if (rc != 0)
+ RETURN(rc);
+
+ info->lti_buf.lb_buf = lmm;
+ info->lti_buf.lb_len = lmm_size;
+
+ if (declare)
+ rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
+ XATTR_NAME_LOV, 0, th);
+ else
+ rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
+ XATTR_NAME_LOV, 0, th);
+ if (rc != 0)
+ RETURN(rc);
+ }
+
+ RETURN(0);
+}
+
+static int lod_declare_dir_striping_create(const struct lu_env *env,
+ struct dt_object *dt,
+ struct lu_attr *attr,
+ struct lu_buf *lmu,
+ struct dt_object_format *dof,
+ struct thandle *th)
+{
+ return lod_dir_striping_create_internal(env, dt, attr, lmu, dof, th,
+ true);
+}
+
+static int lod_dir_striping_create(const struct lu_env *env,
+ struct dt_object *dt,
+ struct lu_attr *attr,
+ struct dt_object_format *dof,
+ struct thandle *th)
+{
+ return lod_dir_striping_create_internal(env, dt, attr, NULL, dof, th,
+ false);
+}
+
+/**
+ * Make LOV EA for striped object.
+ *
+ * Generate striping information and store it in the LOV EA of the given
+ * object. The caller must ensure nobody else is calling the function
+ * against the object concurrently. The transaction must be started.
+ * FLDB service must be running as well; it's used to map FID to the target,
+ * which is stored in LOV EA.
+ *
+ * \param[in] env execution environment for this thread
+ * \param[in] lo LOD object
+ * \param[in] th transaction handle
+ *
+ * \retval 0 if LOV EA is stored successfully
+ * \retval negative error number on failure
+ */
+static int lod_generate_and_set_lovea(const struct lu_env *env,
+ struct lod_object *lo,
+ struct thandle *th)
+{
+ struct lod_thread_info *info = lod_env_info(env);
+ struct dt_object *next = dt_object_child(&lo->ldo_obj);
+ struct lov_mds_md_v1 *lmm;
+ int rc, lmm_size;
+ ENTRY;
+
+ LASSERT(lo);
+
+ if (lo->ldo_comp_cnt == 0) {
+ lod_striping_free(env, lo);
+ rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
+ RETURN(rc);
+ }
+
+ lmm_size = lod_comp_md_size(lo, false);
+ if (info->lti_ea_store_size < lmm_size) {
+ rc = lod_ea_store_resize(info, lmm_size);
+ if (rc)
+ RETURN(rc);
+ }
+ lmm = info->lti_ea_store;
+
+ rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
+ if (rc)
+ RETURN(rc);
+
+ info->lti_buf.lb_buf = lmm;
+ info->lti_buf.lb_len = lmm_size;
+ rc = lod_sub_xattr_set(env, next, &info->lti_buf,
+ XATTR_NAME_LOV, 0, th);
+ RETURN(rc);
+}
+
+/**
+ * Delete layout component(s)
+ *
+ * \param[in] env execution environment for this thread
+ * \param[in] dt object
+ * \param[in] th transaction handle
+ *
+ * \retval 0 on success
+ * \retval negative error number on failure
+ */
+static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
+ struct thandle *th)
+{
+ struct lod_layout_component *lod_comp;
+ struct lod_object *lo = lod_dt_obj(dt);
+ struct dt_object *next = dt_object_child(dt);
+ struct lu_attr *attr = &lod_env_info(env)->lti_attr;
+ int rc, i, j, left;
+
+ LASSERT(lo->ldo_is_composite);
+ LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
+
+ left = lo->ldo_comp_cnt;
+ for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
+ lod_comp = &lo->ldo_comp_entries[i];
+
+ if (lod_comp->llc_id != LCME_ID_INVAL)
+ break;
+ left--;
+
+ /* Not instantiated component */
+ if (lod_comp->llc_stripe == NULL)
+ continue;
+
+ LASSERT(lod_comp->llc_stripe_count > 0);
+ for (j = 0; j < lod_comp->llc_stripe_count; j++) {
+ struct dt_object *obj = lod_comp->llc_stripe[j];
+
+ if (obj == NULL)
+ continue;
+ rc = lod_sub_destroy(env, obj, th);
+ if (rc)
+ GOTO(out, rc);
+
+ lu_object_put(env, &obj->do_lu);
+ lod_comp->llc_stripe[j] = NULL;
+ }
+ OBD_FREE(lod_comp->llc_stripe, sizeof(struct dt_object *) *
+ lod_comp->llc_stripes_allocated);
+ lod_comp->llc_stripe = NULL;
+ OBD_FREE(lod_comp->llc_ost_indices,
+ sizeof(__u32) * lod_comp->llc_stripes_allocated);
+ lod_comp->llc_ost_indices = NULL;
+ lod_comp->llc_stripes_allocated = 0;
+ lod_obj_set_pool(lo, i, NULL);
+ if (lod_comp->llc_ostlist.op_array) {
+ OBD_FREE(lod_comp->llc_ostlist.op_array,
+ lod_comp->llc_ostlist.op_size);
+ lod_comp->llc_ostlist.op_array = NULL;
+ lod_comp->llc_ostlist.op_size = 0;
+ }
+ }
+
+ LASSERTF(left >= 0 && left < lo->ldo_comp_cnt, "left = %d\n", left);
+ if (left > 0) {
+ struct lod_layout_component *comp_array;
+
+ OBD_ALLOC(comp_array, sizeof(*comp_array) * left);
+ if (comp_array == NULL)
+ GOTO(out, rc = -ENOMEM);
+
+ memcpy(&comp_array[0], &lo->ldo_comp_entries[0],
+ sizeof(*comp_array) * left);
+
+ OBD_FREE(lo->ldo_comp_entries,
+ sizeof(*comp_array) * lo->ldo_comp_cnt);
+ lo->ldo_comp_entries = comp_array;
+ lo->ldo_comp_cnt = left;
+
+ LASSERT(lo->ldo_mirror_count == 1);
+ lo->ldo_mirrors[0].lme_end = left - 1;
+ lod_obj_inc_layout_gen(lo);
+ } else {
+ lod_free_comp_entries(lo);
+ }
+
+ LASSERT(dt_object_exists(dt));
+ rc = dt_attr_get(env, next, attr);
+ if (rc)
+ GOTO(out, rc);
+
+ if (attr->la_size > 0) {
+ attr->la_size = 0;
+ attr->la_valid = LA_SIZE;
+ rc = lod_sub_attr_set(env, next, attr, th);
+ if (rc)
+ GOTO(out, rc);
+ }
+
+ rc = lod_generate_and_set_lovea(env, lo, th);
+ EXIT;
+out:
+ if (rc)
+ lod_striping_free(env, lo);
+ return rc;
+}
+
+
+static int lod_get_default_lov_striping(const struct lu_env *env,
+ struct lod_object *lo,
+ struct lod_default_striping *lds);
+/**
+ * Implementation of dt_object_operations::do_xattr_set.
+ *
+ * Sets specified extended attribute on the object. Three types of EAs are
+ * special:
+ * LOV EA - stores striping for a regular file or default striping (when set
+ * on a directory)
+ * LMV EA - stores a marker for the striped directories
+ * DMV EA - stores default directory striping
+ *
+ * When striping is applied to a non-striped existing object (this is called
+ * late striping), then LOD notices the caller wants to turn the object into a
+ * striped one. The stripe objects are created and appropriate EA is set:
+ * LOV EA storing all the stripes directly or LMV EA storing just a small header
+ * with striping configuration.
+ *
+ * \see dt_object_operations::do_xattr_set() in the API description for details.
+ */
+static int lod_xattr_set(const struct lu_env *env,
+ struct dt_object *dt, const struct lu_buf *buf,
+ const char *name, int fl, struct thandle *th)
+{
+ struct dt_object *next = dt_object_child(dt);
+ int rc;
+ ENTRY;
+
+ if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
+ strcmp(name, XATTR_NAME_LMV) == 0) {
+ rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
+ RETURN(rc);
+ } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
+ strncmp(name, XATTR_NAME_LMV, strlen(XATTR_NAME_LMV)) == 0 &&
+ strlen(name) > strlen(XATTR_NAME_LMV) + 1) {
+ const char *op = name + strlen(XATTR_NAME_LMV) + 1;
+
+ rc = -ENOTSUPP;
+ /*
+ * XATTR_NAME_LMV".add" is never called, but only declared,
+ * because lod_xattr_set_lmv() will do the addition.
+ */
+ if (strcmp(op, "del") == 0)
+ rc = lod_dir_layout_delete(env, dt, buf, th);
+ else if (strcmp(op, "set") == 0)
+ rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LMV,
+ fl, th);
+
+ RETURN(rc);
+ } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
+ strcmp(name, XATTR_NAME_LOV) == 0) {
+ struct lod_thread_info *info = lod_env_info(env);
+ struct lod_default_striping *lds = &info->lti_def_striping;
+ struct lov_user_md_v1 *v1 = buf->lb_buf;
+ char pool[LOV_MAXPOOLNAME + 1];
+ bool is_del;
+
+ /* get existing striping config */
+ rc = lod_get_default_lov_striping(env, lod_dt_obj(dt), lds);
+ if (rc)
+ RETURN(rc);
+
+ memset(pool, 0, sizeof(pool));
+ if (lds->lds_def_striping_set == 1)
+ lod_layout_get_pool(lds->lds_def_comp_entries,
+ lds->lds_def_comp_cnt, pool,
+ sizeof(pool));
+
+ is_del = LOVEA_DELETE_VALUES(v1->lmm_stripe_size,
+ v1->lmm_stripe_count,
+ v1->lmm_stripe_offset,
+ NULL);
+
+ /* Retain the pool name if it is not given */
+ if (v1->lmm_magic == LOV_USER_MAGIC_V1 && pool[0] != '\0' &&
+ !is_del) {
+ struct lod_thread_info *info = lod_env_info(env);
+ struct lov_user_md_v3 *v3 = info->lti_ea_store;
+
+ memset(v3, 0, sizeof(*v3));
+ v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
+ v3->lmm_pattern = cpu_to_le32(v1->lmm_pattern);
+ v3->lmm_stripe_count =
+ cpu_to_le32(v1->lmm_stripe_count);
+ v3->lmm_stripe_offset =
+ cpu_to_le32(v1->lmm_stripe_offset);
+ v3->lmm_stripe_size = cpu_to_le32(v1->lmm_stripe_size);
+
+ strlcpy(v3->lmm_pool_name, pool,
+ sizeof(v3->lmm_pool_name));
+
+ info->lti_buf.lb_buf = v3;
+ info->lti_buf.lb_len = sizeof(*v3);
+ rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
+ name, fl, th);
+ } else {
+ rc = lod_xattr_set_lov_on_dir(env, dt, buf, name,
+ fl, th);
+ }
+
+ if (lds->lds_def_striping_set == 1 &&
+ lds->lds_def_comp_entries != NULL)
+ lod_free_def_comp_entries(lds);
+
+ RETURN(rc);
+ } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
+ strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
+ /* default LMVEA */
+ rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
+ th);
+ RETURN(rc);
+ } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
+ (!strcmp(name, XATTR_NAME_LOV) ||
+ !strncmp(name, XATTR_LUSTRE_LOV,
+ strlen(XATTR_LUSTRE_LOV)))) {
+ /* in case of lov EA swap, just set it
+ * if not, it is a replay so check striping match what we
+ * already have during req replay, declare_xattr_set()
+ * defines striping, then create() does the work */
+ if (fl & LU_XATTR_REPLACE) {
+ /* free stripes, then update disk */
+ lod_striping_free(env, lod_dt_obj(dt));
+
+ rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
+ } else if (dt_object_remote(dt)) {
+ /* This only happens during migration, see
+ * mdd_migrate_create(), in which Master MDT will
+ * create a remote target object, and only set
+ * (migrating) stripe EA on the remote object,
+ * and does not need creating each stripes. */
+ rc = lod_sub_xattr_set(env, next, buf, name,
+ fl, th);
+ } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
+ /* delete component(s) */
+ LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
+ rc = lod_layout_del(env, dt, th);
+ } else {
+ /*
+ * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
+ * it's going to create create file with specified
+ * component(s), the striping must have not being
+ * cached in this case;
+ *
+ * Otherwise, it's going to add/change component(s) to
+ * an existing file, the striping must have been cached
+ * in this case.
+ */
+ LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
+ !strcmp(name, XATTR_NAME_LOV),
+ !lod_dt_obj(dt)->ldo_comp_cached));
+
+ rc = lod_striped_create(env, dt, NULL, NULL, th);
+ }
+ RETURN(rc);
+ } else if (strcmp(name, XATTR_NAME_FID) == 0) {
+ rc = lod_replace_parent_fid(env, dt, buf, th, false);
+
+ RETURN(rc);
+ }
+
+ /* then all other xattr */
+ rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
+
+ RETURN(rc);
+}
+
+/**
+ * Implementation of dt_object_operations::do_declare_xattr_del.
+ *
+ * \see dt_object_operations::do_declare_xattr_del() in the API description
+ * for details.
+ */
+static int lod_declare_xattr_del(const struct lu_env *env,
+ struct dt_object *dt, const char *name,
+ struct thandle *th)
+{
+ struct lod_object *lo = lod_dt_obj(dt);
+ struct dt_object *next = dt_object_child(dt);
+ int i;
+ int rc;
+ ENTRY;
+
+ rc = lod_sub_declare_xattr_del(env, next, name, th);
+ if (rc != 0)
+ RETURN(rc);
+
+ if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
+ RETURN(0);
+
+ /* set xattr to each stripes, if needed */
+ rc = lod_striping_load(env, lo);
+ if (rc != 0)
+ RETURN(rc);
+
+ if (lo->ldo_dir_stripe_count == 0)
+ RETURN(0);
+
+ for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
+ struct dt_object *dto = lo->ldo_stripe[i];
+
+ LASSERT(dto);
+ rc = lod_sub_declare_xattr_del(env, dto, name, th);
+ if (rc != 0)
+ break;
+ }
+
+ RETURN(rc);
+}
+
+/**
+ * Implementation of dt_object_operations::do_xattr_del.
+ *
+ * If EA storing a regular striping is being deleted, then release
+ * all the references to the stripe objects in core.
+ *
+ * \see dt_object_operations::do_xattr_del() in the API description for details.
+ */
+static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
+ const char *name, struct thandle *th)
+{
+ struct dt_object *next = dt_object_child(dt);
+ struct lod_object *lo = lod_dt_obj(dt);
+ int rc;
+ int i;
+ ENTRY;
+
+ if (!strcmp(name, XATTR_NAME_LOV) || !strcmp(name, XATTR_NAME_LMV))
+ lod_striping_free(env, lod_dt_obj(dt));
+
+ rc = lod_sub_xattr_del(env, next, name, th);
+ if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
+ RETURN(rc);
+
+ if (lo->ldo_dir_stripe_count == 0)
+ RETURN(0);
+
+ for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
+ struct dt_object *dto = lo->ldo_stripe[i];
+
+ LASSERT(dto);
+
+ rc = lod_sub_xattr_del(env, dto, name, th);
+ if (rc != 0)
+ break;
+ }
+
+ RETURN(rc);
+}
+
+/**
+ * Implementation of dt_object_operations::do_xattr_list.
+ *
+ * \see dt_object_operations::do_xattr_list() in the API description
+ * for details.
+ */
+static int lod_xattr_list(const struct lu_env *env,
+ struct dt_object *dt, const struct lu_buf *buf)
+{
+ return dt_xattr_list(env, dt_object_child(dt), buf);
+}
+
+static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
+{
+ return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
+}
+
+/**
+ * Copy OST list from layout provided by user.
+ *
+ * \param[in] lod_comp layout_component to be filled
+ * \param[in] v3 LOV EA V3 user data
+ *
+ * \retval 0 on success
+ * \retval negative if failed
+ */
+int lod_comp_copy_ost_lists(struct lod_layout_component *lod_comp,
+ struct lov_user_md_v3 *v3)
+{
+ int j;
+
+ ENTRY;
+
+ if (v3->lmm_stripe_offset == LOV_OFFSET_DEFAULT)
+ v3->lmm_stripe_offset = v3->lmm_objects[0].l_ost_idx;
+
+ if (lod_comp->llc_ostlist.op_array) {
+ if (lod_comp->llc_ostlist.op_size >=
+ v3->lmm_stripe_count * sizeof(__u32)) {
+ lod_comp->llc_ostlist.op_count =
+ v3->lmm_stripe_count;
+ goto skip;
+ }
+ OBD_FREE(lod_comp->llc_ostlist.op_array,
+ lod_comp->llc_ostlist.op_size);
+ }
+
+ /* copy ost list from lmm */
+ lod_comp->llc_ostlist.op_count = v3->lmm_stripe_count;
+ lod_comp->llc_ostlist.op_size = v3->lmm_stripe_count * sizeof(__u32);
+ OBD_ALLOC(lod_comp->llc_ostlist.op_array,
+ lod_comp->llc_ostlist.op_size);
+ if (!lod_comp->llc_ostlist.op_array)
+ RETURN(-ENOMEM);
+skip:
+ for (j = 0; j < v3->lmm_stripe_count; j++) {
+ lod_comp->llc_ostlist.op_array[j] =
+ v3->lmm_objects[j].l_ost_idx;
+ }
+
+ RETURN(0);
+}
+
+
+/**
+ * Get default striping.
+ *
+ * \param[in] env execution environment
+ * \param[in] lo object
+ * \param[out] lds default striping
+ *
+ * \retval 0 on success
+ * \retval negative if failed
+ */
+static int lod_get_default_lov_striping(const struct lu_env *env,
+ struct lod_object *lo,
+ struct lod_default_striping *lds)
+{
+ struct lod_thread_info *info = lod_env_info(env);
+ struct lov_user_md_v1 *v1 = NULL;
+ struct lov_user_md_v3 *v3 = NULL;
+ struct lov_comp_md_v1 *comp_v1 = NULL;
+ __u16 comp_cnt;
+ __u16 mirror_cnt;
+ bool composite;
+ int rc, i, j;
+ ENTRY;
+
+ lds->lds_def_striping_set = 0;
+
+ rc = lod_get_lov_ea(env, lo);
+ if (rc < 0)
+ RETURN(rc);
+
+ if (rc < (typeof(rc))sizeof(struct lov_user_md))
+ RETURN(0);
+
+ v1 = info->lti_ea_store;
+ if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
+ lustre_swab_lov_user_md_v1(v1);
+ } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
+ v3 = (struct lov_user_md_v3 *)v1;
+ lustre_swab_lov_user_md_v3(v3);
+ } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_SPECIFIC)) {
+ v3 = (struct lov_user_md_v3 *)v1;
+ lustre_swab_lov_user_md_v3(v3);
+ lustre_swab_lov_user_md_objects(v3->lmm_objects,
+ v3->lmm_stripe_count);
+ } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
+ comp_v1 = (struct lov_comp_md_v1 *)v1;
+ lustre_swab_lov_comp_md_v1(comp_v1);
+ }
+
+ if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1 &&
+ v1->lmm_magic != LOV_MAGIC_COMP_V1 &&
+ v1->lmm_magic != LOV_USER_MAGIC_SPECIFIC)
+ RETURN(-ENOTSUPP);
+
+ if (v1->lmm_magic == LOV_MAGIC_COMP_V1) {
+ comp_v1 = (struct lov_comp_md_v1 *)v1;
+ comp_cnt = comp_v1->lcm_entry_count;
+ if (comp_cnt == 0)
+ RETURN(-EINVAL);
+ mirror_cnt = comp_v1->lcm_mirror_count + 1;
+ composite = true;
+ } else {
+ comp_cnt = 1;
+ mirror_cnt = 0;
+ composite = false;
+ }
+
+ /* realloc default comp entries if necessary */
+ rc = lod_def_striping_comp_resize(lds, comp_cnt);
+ if (rc < 0)
+ RETURN(rc);
+
+ lds->lds_def_comp_cnt = comp_cnt;
+ lds->lds_def_striping_is_composite = composite;
+ lds->lds_def_mirror_cnt = mirror_cnt;
+
+ for (i = 0; i < comp_cnt; i++) {
+ struct lod_layout_component *lod_comp;
+ char *pool;
+
+ lod_comp = &lds->lds_def_comp_entries[i];
+ /*
+ * reset lod_comp values, llc_stripes is always NULL in
+ * the default striping template, llc_pool will be reset
+ * later below.
+ */
+ memset(lod_comp, 0, offsetof(typeof(*lod_comp), llc_pool));
+
+ if (composite) {
+ v1 = (struct lov_user_md *)((char *)comp_v1 +
+ comp_v1->lcm_entries[i].lcme_offset);
+ lod_comp->llc_extent =
+ comp_v1->lcm_entries[i].lcme_extent;
+ /* We only inherit certain flags from the layout */
+ lod_comp->llc_flags =
+ comp_v1->lcm_entries[i].lcme_flags &
+ LCME_TEMPLATE_FLAGS;
+ }
+
+ if (v1->lmm_pattern != LOV_PATTERN_RAID0 &&
+ v1->lmm_pattern != LOV_PATTERN_MDT &&
+ v1->lmm_pattern != 0) {
+ lod_free_def_comp_entries(lds);
+ RETURN(-EINVAL);
+ }
+
+ CDEBUG(D_LAYOUT, DFID" stripe_count=%d stripe_size=%d "
+ "stripe_offset=%d\n",
+ PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
+ (int)v1->lmm_stripe_count, (int)v1->lmm_stripe_size,
+ (int)v1->lmm_stripe_offset);
+
+ lod_comp->llc_stripe_count = v1->lmm_stripe_count;
+ lod_comp->llc_stripe_size = v1->lmm_stripe_size;
+ lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
+ lod_comp->llc_pattern = v1->lmm_pattern;
+
+ pool = NULL;
+ if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
+ /* XXX: sanity check here */
+ v3 = (struct lov_user_md_v3 *) v1;
+ if (v3->lmm_pool_name[0] != '\0')
+ pool = v3->lmm_pool_name;
+ }
+ lod_set_def_pool(lds, i, pool);
+ if (v1->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
+ v3 = (struct lov_user_md_v3 *)v1;
+ rc = lod_comp_copy_ost_lists(lod_comp, v3);
+ if (rc)
+ RETURN(rc);
+ } else if (lod_comp->llc_ostlist.op_array &&
+ lod_comp->llc_ostlist.op_count) {
+ for (j = 0; j < lod_comp->llc_ostlist.op_count; j++)
+ lod_comp->llc_ostlist.op_array[j] = -1;
+ lod_comp->llc_ostlist.op_count = 0;
+ }
+ }
+
+ lds->lds_def_striping_set = 1;
+ RETURN(rc);
+}
+
+/**
+ * Get default directory striping.
+ *
+ * \param[in] env execution environment
+ * \param[in] lo object
+ * \param[out] lds default striping
+ *
+ * \retval 0 on success
+ * \retval negative if failed
+ */
+static int lod_get_default_lmv_striping(const struct lu_env *env,
+ struct lod_object *lo,
+ struct lod_default_striping *lds)
+{
+ struct lod_thread_info *info = lod_env_info(env);
+ struct lmv_user_md_v1 *v1 = NULL;
+ int rc;
+ ENTRY;
+
+ lds->lds_dir_def_striping_set = 0;
+ rc = lod_get_default_lmv_ea(env, lo);
+ if (rc < 0)
+ RETURN(rc);
+
+ if (rc < (typeof(rc))sizeof(struct lmv_user_md))
+ RETURN(0);
+
+ v1 = info->lti_ea_store;
+
+ lds->lds_dir_def_stripe_count = le32_to_cpu(v1->lum_stripe_count);
+ lds->lds_dir_def_stripe_offset = le32_to_cpu(v1->lum_stripe_offset);
+ lds->lds_dir_def_hash_type = le32_to_cpu(v1->lum_hash_type);
+ lds->lds_dir_def_striping_set = 1;
+
+ RETURN(0);
+}
+
+/**
+ * Get default striping in the object.
+ *
+ * Get object default striping and default directory striping.
+ *
+ * \param[in] env execution environment
+ * \param[in] lo object
+ * \param[out] lds default striping
+ *
+ * \retval 0 on success
+ * \retval negative if failed
+ */
+static int lod_get_default_striping(const struct lu_env *env,
+ struct lod_object *lo,
+ struct lod_default_striping *lds)
+{
+ int rc, rc1;
+
+ rc = lod_get_default_lov_striping(env, lo, lds);
+ rc1 = lod_get_default_lmv_striping(env, lo, lds);
+ if (rc == 0 && rc1 < 0)
+ rc = rc1;
+
+ return rc;
+}
+
+/**
+ * Apply default striping on object.
+ *
+ * If object striping pattern is not set, set to the one in default striping.
+ * The default striping is from parent or fs.
+ *
+ * \param[in] lo new object
+ * \param[in] lds default striping
+ * \param[in] mode new object's mode
+ */
+static void lod_striping_from_default(struct lod_object *lo,
+ const struct lod_default_striping *lds,
+ umode_t mode)
+{
+ struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
+ struct lov_desc *desc = &d->lod_desc;
+ int i, rc;
+
+ if (lds->lds_def_striping_set && S_ISREG(mode)) {
+ rc = lod_alloc_comp_entries(lo, lds->lds_def_mirror_cnt,
+ lds->lds_def_comp_cnt);
+ if (rc != 0)
+ return;
+
+ lo->ldo_is_composite = lds->lds_def_striping_is_composite;
+ if (lds->lds_def_mirror_cnt > 1)
+ lo->ldo_flr_state = LCM_FL_RDONLY;
+
+ for (i = 0; i < lo->ldo_comp_cnt; i++) {
+ struct lod_layout_component *obj_comp =
+ &lo->ldo_comp_entries[i];
+ struct lod_layout_component *def_comp =
+ &lds->lds_def_comp_entries[i];
+
+ CDEBUG(D_LAYOUT, "Inherit from default: flags=%#x "
+ "size=%hu nr=%u offset=%u pattern=%#x pool=%s\n",
+ def_comp->llc_flags,
+ def_comp->llc_stripe_size,
+ def_comp->llc_stripe_count,
+ def_comp->llc_stripe_offset,
+ def_comp->llc_pattern,
+ def_comp->llc_pool ?: "");
+
+ *obj_comp = *def_comp;
+ if (def_comp->llc_pool != NULL) {
+ /* pointer was copied from def_comp */
+ obj_comp->llc_pool = NULL;
+ lod_obj_set_pool(lo, i, def_comp->llc_pool);
+ }
+
+ /* copy ost list */
+ if (def_comp->llc_ostlist.op_array &&
+ def_comp->llc_ostlist.op_count) {
+ OBD_ALLOC(obj_comp->llc_ostlist.op_array,
+ obj_comp->llc_ostlist.op_size);
+ if (!obj_comp->llc_ostlist.op_array)
+ return;
+ memcpy(obj_comp->llc_ostlist.op_array,
+ def_comp->llc_ostlist.op_array,
+ obj_comp->llc_ostlist.op_size);
+ } else if (def_comp->llc_ostlist.op_array) {
+ obj_comp->llc_ostlist.op_array = NULL;
+ }
+
+ /*
+ * Don't initialize these fields for plain layout
+ * (v1/v3) here, they are inherited in the order of
+ * 'parent' -> 'fs default (root)' -> 'global default
+ * values for stripe_count & stripe_size'.
+ *
+ * see lod_ah_init().
+ */
+ if (!lo->ldo_is_composite)
+ continue;
+
+ lod_adjust_stripe_info(obj_comp, desc);
+ }
+ } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
+ if (lo->ldo_dir_stripe_count == 0)
+ lo->ldo_dir_stripe_count =
+ lds->lds_dir_def_stripe_count;
+ if (lo->ldo_dir_stripe_offset == -1)
+ lo->ldo_dir_stripe_offset =
+ lds->lds_dir_def_stripe_offset;
+ if (lo->ldo_dir_hash_type == 0)
+ lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
+
+ CDEBUG(D_LAYOUT, "striping from default dir: count:%hu, "
+ "offset:%u, hash_type:%u\n",
+ lo->ldo_dir_stripe_count, lo->ldo_dir_stripe_offset,
+ lo->ldo_dir_hash_type);
+ }
+}
+
+static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root)
+{
+ struct lod_layout_component *lod_comp;
+
+ if (lo->ldo_comp_cnt == 0)
+ return true;
+
+ if (lo->ldo_is_composite)
+ return false;
+
+ lod_comp = &lo->ldo_comp_entries[0];
+
+ if (lod_comp->llc_stripe_count <= 0 ||
+ lod_comp->llc_stripe_size <= 0)
+ return true;
+
+ if (from_root && (lod_comp->llc_pool == NULL ||
+ lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
+ return true;
+
+ return false;
+}
+
+/**
+ * Implementation of dt_object_operations::do_ah_init.
+ *
+ * This method is used to make a decision on the striping configuration for the
+ * object being created. It can be taken from the \a parent object if it exists,
+ * or filesystem's default. The resulting configuration (number of stripes,
+ * stripe size/offset, pool name, etc) is stored in the object itself and will
+ * be used by the methods like ->doo_declare_create().
+ *
+ * \see dt_object_operations::do_ah_init() in the API description for details.
+ */
+static void lod_ah_init(const struct lu_env *env,
+ struct dt_allocation_hint *ah,
+ struct dt_object *parent,
+ struct dt_object *child,
+ umode_t child_mode)
+{
+ struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
+ struct lod_thread_info *info = lod_env_info(env);
+ struct lod_default_striping *lds = &info->lti_def_striping;
+ struct dt_object *nextp = NULL;
+ struct dt_object *nextc;
+ struct lod_object *lp = NULL;
+ struct lod_object *lc;
+ struct lov_desc *desc;
+ struct lod_layout_component *lod_comp;
+ int rc;
+ ENTRY;
+
+ LASSERT(child);