+ break;
+ }
+
+ RETURN(rc);
+}
+
+/**
+ * Delete an extended attribute.
+ *
+ * Deletes specified xattr from the object and the stripes if the latter exist.
+ *
+ * \param[in] env execution environment
+ * \param[in] dt object
+ * \param[in] name name of xattr
+ * \param[in] th transaction handle
+ *
+ * \retval 0 on success
+ * \retval negative if failed
+ */
+static int lod_xattr_del_internal(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;
+
+ 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(rc);
+
+ for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
+ LASSERT(lo->ldo_stripe[i]);
+
+ rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
+ if (rc != 0)
+ break;
+ }
+
+ RETURN(rc);
+}
+
+/**
+ * Set default striping on a directory.
+ *
+ * Sets specified striping on a directory object unless it matches the default
+ * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
+ * EA. This striping will be used when regular file is being created in this
+ * directory.
+ *
+ * \param[in] env execution environment
+ * \param[in] dt the striped object
+ * \param[in] buf buffer with the striping
+ * \param[in] name name of EA
+ * \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_lov_on_dir(const struct lu_env *env,
+ struct dt_object *dt,
+ const struct lu_buf *buf,
+ const char *name, int fl,
+ struct thandle *th)
+{
+ struct lov_user_md_v1 *lum;
+ struct lov_user_md_v3 *v3 = NULL;
+ const char *pool_name = NULL;
+ int rc;
+ bool is_del;
+ ENTRY;
+
+ LASSERT(buf != NULL && buf->lb_buf != NULL);
+ lum = buf->lb_buf;
+
+ switch (lum->lmm_magic) {
+ case LOV_USER_MAGIC_SPECIFIC:
+ case LOV_USER_MAGIC_V3:
+ v3 = buf->lb_buf;
+ if (v3->lmm_pool_name[0] != '\0')
+ pool_name = v3->lmm_pool_name;
+ /* fall through */
+ case LOV_USER_MAGIC_V1:
+ /* if { size, offset, count } = { 0, -1, 0 } and no pool
+ * (i.e. all default values specified) then delete default
+ * striping from dir. */
+ CDEBUG(D_LAYOUT,
+ "set default striping: sz %u # %u offset %d %s %s\n",
+ (unsigned)lum->lmm_stripe_size,
+ (unsigned)lum->lmm_stripe_count,
+ (int)lum->lmm_stripe_offset,
+ v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
+
+ is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
+ lum->lmm_stripe_count,
+ lum->lmm_stripe_offset,
+ pool_name);
+ break;
+ case LOV_USER_MAGIC_COMP_V1:
+ is_del = false;
+ break;
+ default:
+ CERROR("Invalid magic %x\n", lum->lmm_magic);
+ RETURN(-EINVAL);
+ }
+
+ if (is_del) {
+ 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);
+ }
+
+ RETURN(rc);
+}
+
+/**
+ * Set default striping on a directory object.
+ *
+ * Sets specified striping on a directory object unless it matches the default
+ * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
+ * EA. This striping will be used when a new directory is being created in the
+ * directory.
+ *
+ * \param[in] env execution environment
+ * \param[in] dt the striped object
+ * \param[in] buf buffer with the striping
+ * \param[in] name name of EA
+ * \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_default_lmv_on_dir(const struct lu_env *env,
+ struct dt_object *dt,
+ const struct lu_buf *buf,
+ const char *name, int fl,
+ struct thandle *th)
+{
+ struct lmv_user_md_v1 *lum;
+ int rc;
+ ENTRY;
+
+ 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;
+ char *stripe_name = info->lti_key;
+ struct lu_name *sname;
+ struct linkea_data ldata = { NULL };
+ struct lu_buf linkea_buf;
+
+ dto = lo->ldo_stripe[i];
+
+ 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, 0);
+ 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, 0);
+ 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, 0);
+ 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) {
+ struct lmv_mds_md_v1 *lmm = buf->lb_buf;
+
+ if (lmm != NULL && le32_to_cpu(lmm->lmv_hash_type) &
+ LMV_HASH_FLAG_MIGRATION)
+ rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
+ else
+ rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
+
+ RETURN(rc);
+ }
+
+ 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, 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);
+ int rc;
+ int i;
+ ENTRY;
+
+ rc = lod_sub_declare_xattr_del(env, dt_object_child(dt), 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++) {
+ LASSERT(lo->ldo_stripe[i]);
+ rc = lod_sub_declare_xattr_del(env, lo->ldo_stripe[i],
+ 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))
+ 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++) {
+ LASSERT(lo->ldo_stripe[i]);
+
+ rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], 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_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;
+ 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;
+ struct lu_extent *ext;
+ 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);
+ ext = &comp_v1->lcm_entries[i].lcme_extent;
+ lod_comp->llc_extent = *ext;
+ }
+
+ 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);
+ }
+ }
+
+ 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, "Inherite from default: size:%hu "
+ "nr:%u offset:%u pattern %#x %s\n",
+ 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) {
+ 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);
+ }
+
+ /*
+ * 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);
+
+ if (likely(parent)) {
+ nextp = dt_object_child(parent);
+ lp = lod_dt_obj(parent);
+ }
+
+ nextc = dt_object_child(child);
+ lc = lod_dt_obj(child);
+
+ LASSERT(!lod_obj_is_striped(child));
+ /* default layout template may have been set on the regular file
+ * when this is called from mdd_create_data() */
+ if (S_ISREG(child_mode))
+ lod_free_comp_entries(lc);
+
+ if (!dt_object_exists(nextc))
+ nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
+
+ if (S_ISDIR(child_mode)) {
+ const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
+
+ /* other default values are 0 */
+ lc->ldo_dir_stripe_offset = -1;
+
+ /* get default striping from parent object */
+ if (likely(lp != NULL))
+ lod_get_default_striping(env, lp, lds);
+
+ /* set child default striping info, default value is NULL */
+ if (lds->lds_def_striping_set || lds->lds_dir_def_striping_set)
+ lc->ldo_def_striping = lds;
+
+ /* It should always honour the specified stripes */
+ /* Note: old client (< 2.7)might also do lfs mkdir, whose EA
+ * will have old magic. In this case, we should ignore the
+ * stripe count and try to create dir by default stripe.
+ */
+ if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
+ (le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC ||
+ le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC_SPECIFIC)) {
+ lc->ldo_dir_stripe_count =
+ le32_to_cpu(lum1->lum_stripe_count);
+ lc->ldo_dir_stripe_offset =
+ le32_to_cpu(lum1->lum_stripe_offset);
+ lc->ldo_dir_hash_type =
+ le32_to_cpu(lum1->lum_hash_type);
+ CDEBUG(D_INFO,
+ "set dirstripe: count %hu, offset %d, hash %u\n",
+ lc->ldo_dir_stripe_count,
+ (int)lc->ldo_dir_stripe_offset,
+ lc->ldo_dir_hash_type);
+ } else {
+ /* transfer defaults LMV to new directory */
+ lod_striping_from_default(lc, lds, child_mode);
+ }
+
+ /* shrink the stripe_count to the avaible MDT count */
+ if (lc->ldo_dir_stripe_count > d->lod_remote_mdt_count + 1 &&
+ !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))
+ lc->ldo_dir_stripe_count = d->lod_remote_mdt_count + 1;
+
+ /* Directory will be striped only if stripe_count > 1, if
+ * stripe_count == 1, let's reset stripe_count = 0 to avoid
+ * create single master stripe and also help to unify the
+ * stripe handling of directories and files */
+ if (lc->ldo_dir_stripe_count == 1)
+ lc->ldo_dir_stripe_count = 0;
+
+ CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
+ lc->ldo_dir_stripe_count,
+ (int)lc->ldo_dir_stripe_offset, lc->ldo_dir_hash_type);
+
+ RETURN_EXIT;
+ }
+
+ /* child object regular file*/
+
+ if (!lod_object_will_be_striped(S_ISREG(child_mode),
+ lu_object_fid(&child->do_lu)))
+ RETURN_EXIT;
+
+ /* If object is going to be striped over OSTs, transfer default
+ * striping information to the child, so that we can use it
+ * during declaration and creation.
+ *
+ * Try from the parent first.
+ */
+ if (likely(lp != NULL)) {
+ rc = lod_get_default_lov_striping(env, lp, lds);
+ if (rc == 0)
+ lod_striping_from_default(lc, lds, child_mode);
+ }
+
+ /* Initialize lod_device::lod_md_root object reference */
+ if (d->lod_md_root == NULL) {
+ struct dt_object *root;
+ struct lod_object *lroot;
+
+ lu_root_fid(&info->lti_fid);
+ root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
+ if (!IS_ERR(root)) {
+ lroot = lod_dt_obj(root);
+
+ spin_lock(&d->lod_lock);
+ if (d->lod_md_root != NULL)
+ dt_object_put(env, &d->lod_md_root->ldo_obj);
+ d->lod_md_root = lroot;
+ spin_unlock(&d->lod_lock);
+ }
+ }
+
+ /* try inherit layout from the root object (fs default) when:
+ * - parent does not have default layout; or
+ * - parent has plain(v1/v3) default layout, and some attributes
+ * are not specified in the default layout;
+ */
+ if (d->lod_md_root != NULL && lod_need_inherit_more(lc, true)) {
+ rc = lod_get_default_lov_striping(env, d->lod_md_root, lds);
+ if (rc)
+ goto out;
+ if (lc->ldo_comp_cnt == 0) {
+ lod_striping_from_default(lc, lds, child_mode);
+ } else if (!lds->lds_def_striping_is_composite) {
+ struct lod_layout_component *def_comp;
+
+ LASSERT(!lc->ldo_is_composite);
+ lod_comp = &lc->ldo_comp_entries[0];
+ def_comp = &lds->lds_def_comp_entries[0];
+
+ if (lod_comp->llc_stripe_count <= 0)
+ lod_comp->llc_stripe_count =
+ def_comp->llc_stripe_count;
+ if (lod_comp->llc_stripe_size <= 0)
+ lod_comp->llc_stripe_size =
+ def_comp->llc_stripe_size;
+ if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT)
+ lod_comp->llc_stripe_offset =
+ def_comp->llc_stripe_offset;
+ if (lod_comp->llc_pool == NULL)
+ lod_obj_set_pool(lc, 0, def_comp->llc_pool);
+ }
+ }
+out:
+ /*
+ * fs default striping may not be explicitly set, or historically set
+ * in config log, use them.
+ */
+ if (lod_need_inherit_more(lc, false)) {
+ if (lc->ldo_comp_cnt == 0) {
+ rc = lod_alloc_comp_entries(lc, 0, 1);
+ if (rc)
+ /* fail to allocate memory, will create a
+ * non-striped file. */
+ RETURN_EXIT;
+ lc->ldo_is_composite = 0;
+ lod_comp = &lc->ldo_comp_entries[0];
+ lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
+ }
+ LASSERT(!lc->ldo_is_composite);
+ lod_comp = &lc->ldo_comp_entries[0];
+ desc = &d->lod_desc;
+ lod_adjust_stripe_info(lod_comp, desc);
+ }
+
+ EXIT;
+}
+
+#define ll_do_div64(aaa,bbb) do_div((aaa), (bbb))
+/**
+ * Size initialization on late striping.
+ *
+ * Propagate the size of a truncated object to a deferred striping.
+ * This function handles a special case when truncate was done on a
+ * non-striped object and now while the striping is being created
+ * we can't lose that size, so we have to propagate it to the stripes
+ * being created.
+ *
+ * \param[in] env execution environment
+ * \param[in] dt object
+ * \param[in] th transaction handle
+ *
+ * \retval 0 on success
+ * \retval negative if failed
+ */
+static int lod_declare_init_size(const struct lu_env *env,
+ struct dt_object *dt, struct thandle *th)
+{
+ struct dt_object *next = dt_object_child(dt);
+ struct lod_object *lo = lod_dt_obj(dt);
+ struct dt_object **objects = NULL;
+ struct lu_attr *attr = &lod_env_info(env)->lti_attr;
+ uint64_t size, offs;
+ int i, rc, stripe, stripe_count = 0, stripe_size = 0;
+ struct lu_extent size_ext;
+ ENTRY;
+
+ if (!lod_obj_is_striped(dt))
+ RETURN(0);
+
+ rc = dt_attr_get(env, next, attr);
+ LASSERT(attr->la_valid & LA_SIZE);
+ if (rc)
+ RETURN(rc);
+
+ size = attr->la_size;
+ if (size == 0)
+ RETURN(0);
+
+ size_ext = (typeof(size_ext)){ .e_start = size - 1, .e_end = size };
+ for (i = 0; i < lo->ldo_comp_cnt; i++) {
+ struct lod_layout_component *lod_comp;
+ struct lu_extent *extent;
+
+ lod_comp = &lo->ldo_comp_entries[i];
+
+ if (lod_comp->llc_stripe == NULL)
+ continue;
+
+ extent = &lod_comp->llc_extent;
+ CDEBUG(D_INFO, "%lld "DEXT"\n", size, PEXT(extent));
+ if (!lo->ldo_is_composite ||
+ lu_extent_is_overlapped(extent, &size_ext)) {
+ objects = lod_comp->llc_stripe;
+ stripe_count = lod_comp->llc_stripe_count;
+ stripe_size = lod_comp->llc_stripe_size;
+
+ /* next mirror */
+ if (stripe_count == 0)
+ continue;
+
+ LASSERT(objects != NULL && stripe_size != 0);
+ /* ll_do_div64(a, b) returns a % b, and a = a / b */
+ ll_do_div64(size, (__u64)stripe_size);
+ stripe = ll_do_div64(size, (__u64)stripe_count);
+ LASSERT(objects[stripe] != NULL);
+
+ size = size * stripe_size;
+ offs = attr->la_size;
+ size += ll_do_div64(offs, stripe_size);
+
+ attr->la_valid = LA_SIZE;
+ attr->la_size = size;
+
+ rc = lod_sub_declare_attr_set(env, objects[stripe],
+ attr, th);
+ }
+ }
+
+ RETURN(rc);
+}
+
+/**
+ * Declare creation of striped object.
+ *
+ * The function declares creation stripes for a regular object. The function
+ * also declares whether the stripes will be created with non-zero size if
+ * previously size was set non-zero on the master object. If object \a dt is
+ * not local, then only fully defined striping can be applied in \a lovea.
+ * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
+ * for the details.
+ *
+ * \param[in] env execution environment
+ * \param[in] dt object
+ * \param[in] attr attributes the stripes will be created with
+ * \param[in] lovea a buffer containing striping description
+ * \param[in] th transaction handle
+ *
+ * \retval 0 on success
+ * \retval negative if failed
+ */
+int lod_declare_striped_create(const struct lu_env *env, struct dt_object *dt,
+ struct lu_attr *attr,
+ const struct lu_buf *lovea, struct thandle *th)
+{
+ struct lod_thread_info *info = lod_env_info(env);
+ struct dt_object *next = dt_object_child(dt);
+ struct lod_object *lo = lod_dt_obj(dt);
+ int rc;
+ ENTRY;
+
+ if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
+ GOTO(out, rc = -ENOMEM);
+
+ if (!dt_object_remote(next)) {
+ /* choose OST and generate appropriate objects */
+ rc = lod_prepare_create(env, lo, attr, lovea, th);
+ if (rc)
+ GOTO(out, rc);
+
+ /*
+ * declare storage for striping data
+ */
+ info->lti_buf.lb_len = lod_comp_md_size(lo, false);
+ } else {
+ /* LOD can not choose OST objects for remote objects, i.e.
+ * stripes must be ready before that. Right now, it can only
+ * happen during migrate, i.e. migrate process needs to create
+ * remote regular file (mdd_migrate_create), then the migrate
+ * process will provide stripeEA. */
+ LASSERT(lovea != NULL);
+ info->lti_buf = *lovea;
+ }
+
+ rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
+ XATTR_NAME_LOV, 0, th);
+ if (rc)
+ GOTO(out, rc);
+
+ /*
+ * if striping is created with local object's size > 0,
+ * we have to propagate this size to specific object
+ * the case is possible only when local object was created previously
+ */
+ if (dt_object_exists(next))
+ rc = lod_declare_init_size(env, dt, th);
+
+out:
+ /* failed to create striping or to set initial size, let's reset
+ * config so that others don't get confused */
+ if (rc)
+ lod_striping_free(env, lo);
+
+ RETURN(rc);
+}
+
+/**
+ * Implementation of dt_object_operations::do_declare_create.
+ *
+ * The method declares creation of a new object. If the object will be striped,
+ * then helper functions are called to find FIDs for the stripes, declare
+ * creation of the stripes and declare initialization of the striping
+ * information to be stored in the master object.
+ *
+ * \see dt_object_operations::do_declare_create() in the API description
+ * for details.
+ */
+static int lod_declare_create(const struct lu_env *env, struct dt_object *dt,
+ struct lu_attr *attr,
+ struct dt_allocation_hint *hint,
+ struct dt_object_format *dof, struct thandle *th)
+{
+ struct dt_object *next = dt_object_child(dt);
+ struct lod_object *lo = lod_dt_obj(dt);
+ int rc;
+ ENTRY;
+
+ LASSERT(dof);
+ LASSERT(attr);
+ LASSERT(th);
+
+ /*
+ * first of all, we declare creation of local object
+ */
+ rc = lod_sub_declare_create(env, next, attr, hint, dof, th);
+ if (rc != 0)
+ GOTO(out, rc);
+
+ /*
+ * it's lod_ah_init() that has decided the object will be striped
+ */
+ if (dof->dof_type == DFT_REGULAR) {
+ /* callers don't want stripes */
+ /* XXX: all tricky interactions with ->ah_make_hint() decided
+ * to use striping, then ->declare_create() behaving differently
+ * should be cleaned */
+ if (dof->u.dof_reg.striped != 0)
+ rc = lod_declare_striped_create(env, dt, attr,
+ NULL, th);
+ } else if (dof->dof_type == DFT_DIR) {
+ struct seq_server_site *ss;
+ struct lu_buf buf = { NULL };
+ struct lu_buf *lmu = NULL;
+
+ ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
+
+ /* If the parent has default stripeEA, and client
+ * did not find it before sending create request,
+ * then MDT will return -EREMOTE, and client will
+ * retrieve the default stripeEA and re-create the
+ * sub directory.
+ *
+ * Note: if dah_eadata != NULL, it means creating the
+ * striped directory with specified stripeEA, then it
+ * should ignore the default stripeEA */
+ if (hint != NULL && hint->dah_eadata == NULL) {
+ if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
+ GOTO(out, rc = -EREMOTE);
+
+ if (lo->ldo_dir_stripe_offset == -1) {
+ /* child and parent should be in the same MDT */
+ if (hint->dah_parent != NULL &&
+ dt_object_remote(hint->dah_parent))
+ GOTO(out, rc = -EREMOTE);
+ } else if (lo->ldo_dir_stripe_offset !=
+ ss->ss_node_id) {
+ struct lod_device *lod;
+ struct lod_tgt_descs *ltd;
+ struct lod_tgt_desc *tgt = NULL;
+ bool found_mdt = false;
+ int i;
+
+ lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
+ ltd = &lod->lod_mdt_descs;
+ cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
+ tgt = LTD_TGT(ltd, i);
+ if (tgt->ltd_index ==
+ lo->ldo_dir_stripe_offset) {
+ found_mdt = true;
+ break;
+ }
+ }
+
+ /* If the MDT indicated by stripe_offset can be
+ * found, then tell client to resend the create
+ * request to the correct MDT, otherwise return
+ * error to client */
+ if (found_mdt)
+ GOTO(out, rc = -EREMOTE);
+ else
+ GOTO(out, rc = -EINVAL);
+ }
+ } else if (hint && hint->dah_eadata) {
+ lmu = &buf;
+ lmu->lb_buf = (void *)hint->dah_eadata;
+ lmu->lb_len = hint->dah_eadata_len;
+ }
+
+ rc = lod_declare_dir_striping_create(env, dt, attr, lmu, dof,
+ th);
+ }
+out:
+ /* failed to create striping or to set initial size, let's reset
+ * config so that others don't get confused */
+ if (rc)
+ lod_striping_free(env, lo);
+ RETURN(rc);
+}