+static inline
+struct lov_comp_md_entry_v1 *comp_entry_v1(struct lov_comp_md_v1 *comp, int i)
+{
+ LASSERTF((le32_to_cpu(comp->lcm_magic) & ~LOV_MAGIC_DEFINED) ==
+ LOV_USER_MAGIC_COMP_V1, "Wrong magic %x\n",
+ le32_to_cpu(comp->lcm_magic));
+ LASSERTF(i >= 0 && i < le16_to_cpu(comp->lcm_entry_count),
+ "bad index %d, max = %d\n",
+ i, le16_to_cpu(comp->lcm_entry_count));
+
+ return &comp->lcm_entries[i];
+}
+
+#define for_each_comp_entry_v1(comp, entry) \
+ for (entry = comp_entry_v1(comp, 0); \
+ entry <= comp_entry_v1(comp, \
+ le16_to_cpu(comp->lcm_entry_count) - 1); \
+ entry++)
+
+int lod_erase_dom_stripe(struct lov_comp_md_v1 *comp_v1,
+ struct lov_comp_md_entry_v1 *dom_ent)
+{
+ struct lov_comp_md_entry_v1 *ent;
+ __u16 entries;
+ __u32 dom_off, dom_size, comp_size, off;
+ void *src, *dst;
+ unsigned int size, shift;
+
+ entries = le16_to_cpu(comp_v1->lcm_entry_count) - 1;
+ LASSERT(entries > 0);
+ comp_v1->lcm_entry_count = cpu_to_le16(entries);
+
+ comp_size = le32_to_cpu(comp_v1->lcm_size);
+ dom_off = le32_to_cpu(dom_ent->lcme_offset);
+ dom_size = le32_to_cpu(dom_ent->lcme_size);
+
+ /* all entries offsets are shifted by entry size at least */
+ shift = sizeof(*dom_ent);
+ for_each_comp_entry_v1(comp_v1, ent) {
+ off = le32_to_cpu(ent->lcme_offset);
+ if (off == dom_off) {
+ /* Entry deletion creates two holes in layout data:
+ * - hole in entries array
+ * - hole in layout data at dom_off with dom_size
+ *
+ * First memmove is one entry shift from next entry
+ * start with size up to dom_off in blob
+ */
+ dst = (void *)ent;
+ src = (void *)(ent + 1);
+ size = (unsigned long)((void *)comp_v1 + dom_off - src);
+ memmove(dst, src, size);
+ /* take 'off' from just moved entry */
+ off = le32_to_cpu(ent->lcme_offset);
+ /* second memmove is blob tail after 'off' up to
+ * component end
+ */
+ dst = (void *)comp_v1 + dom_off - sizeof(*ent);
+ src = (void *)comp_v1 + off;
+ size = (unsigned long)(comp_size - off);
+ memmove(dst, src, size);
+ /* all entries offsets after DoM entry are shifted by
+ * dom_size additionally
+ */
+ shift += dom_size;
+ }
+ ent->lcme_offset = cpu_to_le32(off - shift);
+ }
+ comp_v1->lcm_size = cpu_to_le32(comp_size - shift);
+
+ /* notify a caller to re-check entry */
+ return -ERESTART;
+}
+
+void lod_dom_stripesize_recalc(struct lod_device *d)
+{
+ __u64 threshold_mb = d->lod_dom_threshold_free_mb;
+ __u32 max_size = d->lod_dom_stripesize_max_kb;
+ __u32 def_size = d->lod_dom_stripesize_cur_kb;
+
+ /* use maximum allowed value if free space is above threshold */
+ if (d->lod_lsfs_free_mb >= threshold_mb) {
+ def_size = max_size;
+ } else if (!d->lod_lsfs_free_mb || max_size <= LOD_DOM_MIN_SIZE_KB) {
+ def_size = 0;
+ } else {
+ /* recalc threshold like it would be with def_size as max */
+ threshold_mb = mult_frac(threshold_mb, def_size, max_size);
+ if (d->lod_lsfs_free_mb < threshold_mb)
+ def_size = rounddown(def_size / 2, LOD_DOM_MIN_SIZE_KB);
+ else if (d->lod_lsfs_free_mb > threshold_mb * 2)
+ def_size = max_t(unsigned int, def_size * 2,
+ LOD_DOM_MIN_SIZE_KB);
+ }
+
+ if (d->lod_dom_stripesize_cur_kb != def_size) {
+ CDEBUG(D_LAYOUT, "Change default DOM stripe size %d->%d\n",
+ d->lod_dom_stripesize_cur_kb, def_size);
+ d->lod_dom_stripesize_cur_kb = def_size;
+ }
+}
+
+static __u32 lod_dom_stripesize_limit(const struct lu_env *env,
+ struct lod_device *d)
+{
+ int rc;
+
+ /* set bfree as fraction of total space */
+ if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_SPOOF)) {
+ spin_lock(&d->lod_lsfs_lock);
+ d->lod_lsfs_free_mb = mult_frac(d->lod_lsfs_total_mb,
+ min_t(int, cfs_fail_val, 100), 100);
+ GOTO(recalc, rc = 0);
+ }
+
+ if (d->lod_lsfs_age < ktime_get_seconds() - LOD_DOM_SFS_MAX_AGE) {
+ struct obd_statfs sfs;
+
+ spin_lock(&d->lod_lsfs_lock);
+ if (d->lod_lsfs_age > ktime_get_seconds() - LOD_DOM_SFS_MAX_AGE)
+ GOTO(unlock, rc = 0);
+
+ d->lod_lsfs_age = ktime_get_seconds();
+ spin_unlock(&d->lod_lsfs_lock);
+ rc = dt_statfs(env, d->lod_child, &sfs);
+ if (rc) {
+ CDEBUG(D_LAYOUT,
+ "%s: failed to get OSD statfs: rc = %d\n",
+ lod2obd(d)->obd_name, rc);
+ GOTO(out, rc);
+ }
+ /* udpate local OSD cached statfs data */
+ spin_lock(&d->lod_lsfs_lock);
+ d->lod_lsfs_total_mb = (sfs.os_blocks * sfs.os_bsize) >> 20;
+ d->lod_lsfs_free_mb = (sfs.os_bfree * sfs.os_bsize) >> 20;
+recalc:
+ lod_dom_stripesize_recalc(d);
+unlock:
+ spin_unlock(&d->lod_lsfs_lock);
+ }
+out:
+ return d->lod_dom_stripesize_cur_kb << 10;
+}
+
+int lod_dom_stripesize_choose(const struct lu_env *env, struct lod_device *d,
+ struct lov_comp_md_v1 *comp_v1,
+ struct lov_comp_md_entry_v1 *dom_ent,
+ __u32 stripe_size)
+{
+ struct lov_comp_md_entry_v1 *ent;
+ struct lu_extent *dom_ext, *ext;
+ struct lov_user_md_v1 *lum;
+ __u32 max_stripe_size;
+ __u16 mid, dom_mid;
+ int rc = 0;
+ bool dom_next_entry = false;
+
+ dom_ext = &dom_ent->lcme_extent;
+ dom_mid = mirror_id_of(le32_to_cpu(dom_ent->lcme_id));
+ max_stripe_size = lod_dom_stripesize_limit(env, d);
+
+ /* Check stripe size againts current per-MDT limit */
+ if (stripe_size <= max_stripe_size)
+ return 0;
+
+ lum = (void *)comp_v1 + le32_to_cpu(dom_ent->lcme_offset);
+ CDEBUG(D_LAYOUT, "overwrite DoM component size %u with MDT limit %u\n",
+ stripe_size, max_stripe_size);
+ lum->lmm_stripe_size = cpu_to_le32(max_stripe_size);
+
+ /* In common case the DoM stripe is first entry in a mirror and
+ * can be deleted only if it is not single entry in layout or
+ * mirror, otherwise error should be returned.
+ */
+ for_each_comp_entry_v1(comp_v1, ent) {
+ if (ent == dom_ent)
+ continue;
+
+ mid = mirror_id_of(le32_to_cpu(ent->lcme_id));
+ if (mid != dom_mid)
+ continue;
+
+ ext = &ent->lcme_extent;
+ if (ext->e_start != dom_ext->e_end)
+ continue;
+
+ /* Found next component after the DoM one with the same
+ * mirror_id and adjust its start with DoM component end.
+ *
+ * NOTE: we are considering here that there can be only one
+ * DoM component in a file, all replicas are located on OSTs
+ * always and don't need adjustment since use own layouts.
+ */
+ ext->e_start = cpu_to_le64(max_stripe_size);
+ dom_next_entry = true;
+ break;
+ }
+
+ if (max_stripe_size == 0) {
+ /* DoM component size is zero due to server setting, remove
+ * it from the layout but only if next component exists in
+ * the same mirror. That must be checked prior calling the
+ * lod_erase_dom_stripe().
+ */
+ if (!dom_next_entry)
+ return -EFBIG;
+
+ rc = lod_erase_dom_stripe(comp_v1, dom_ent);
+ } else {
+ /* Update DoM extent end finally */
+ dom_ext->e_end = cpu_to_le64(max_stripe_size);
+ }
+
+ return rc;
+}
+
+/**
+ * Verify LOV striping.
+ *
+ * \param[in] d LOD device
+ * \param[in] buf buffer with LOV EA to verify
+ * \param[in] is_from_disk 0 - from user, allow some fields to be 0
+ * 1 - from disk, do not allow
+ * \param[in] start extent start for composite layout
+ *
+ * \retval 0 if the striping is valid
+ * \retval -EINVAL if striping is invalid
+ */
+int lod_verify_striping(const struct lu_env *env, struct lod_device *d,
+ struct lod_object *lo, const struct lu_buf *buf,
+ bool is_from_disk)
+{
+ struct lov_user_md_v1 *lum;
+ struct lov_comp_md_v1 *comp_v1;
+ struct lov_comp_md_entry_v1 *ent;
+ struct lu_extent *ext;
+ struct lu_buf tmp;
+ __u64 prev_end = 0;
+ __u32 stripe_size = 0;
+ __u16 prev_mid = -1, mirror_id = -1;
+ __u32 mirror_count;
+ __u32 magic;
+ int rc = 0;
+ ENTRY;
+
+ if (buf->lb_len < sizeof(lum->lmm_magic)) {
+ CDEBUG(D_LAYOUT, "invalid buf len %zu\n", buf->lb_len);
+ RETURN(-EINVAL);
+ }
+
+ lum = buf->lb_buf;
+
+ magic = le32_to_cpu(lum->lmm_magic) & ~LOV_MAGIC_DEFINED;
+ /* treat foreign LOV EA/object case first
+ * XXX is it expected to try setting again a foreign?
+ * XXX should we care about different current vs new layouts ?
+ */
+ if (unlikely(magic == LOV_USER_MAGIC_FOREIGN)) {
+ struct lov_foreign_md *lfm = buf->lb_buf;
+
+ if (buf->lb_len < offsetof(typeof(*lfm), lfm_value)) {
+ CDEBUG(D_LAYOUT,
+ "buf len %zu < min lov_foreign_md size (%zu)\n",
+ buf->lb_len, offsetof(typeof(*lfm),
+ lfm_value));
+ RETURN(-EINVAL);
+ }
+
+ if (foreign_size_le(lfm) > buf->lb_len) {
+ CDEBUG(D_LAYOUT,
+ "buf len %zu < this lov_foreign_md size (%zu)\n",
+ buf->lb_len, foreign_size_le(lfm));
+ RETURN(-EINVAL);
+ }
+ /* Don't do anything with foreign layouts */
+ RETURN(0);
+ }
+
+ /* normal LOV/layout cases */
+
+ if (buf->lb_len < sizeof(*lum)) {
+ CDEBUG(D_LAYOUT, "buf len %zu too small for lov_user_md\n",
+ buf->lb_len);
+ RETURN(-EINVAL);
+ }
+
+ if (magic != LOV_USER_MAGIC_V1 &&
+ magic != LOV_USER_MAGIC_V3 &&
+ magic != LOV_USER_MAGIC_SPECIFIC &&
+ magic != LOV_USER_MAGIC_COMP_V1) {
+ CDEBUG(D_LAYOUT, "bad userland LOV MAGIC: %#x\n",
+ le32_to_cpu(lum->lmm_magic));
+ RETURN(-EINVAL);
+ }
+
+ if (magic != LOV_USER_MAGIC_COMP_V1)
+ RETURN(lod_verify_v1v3(d, buf, is_from_disk));
+
+ /* magic == LOV_USER_MAGIC_COMP_V1 */
+ comp_v1 = buf->lb_buf;
+ if (buf->lb_len < le32_to_cpu(comp_v1->lcm_size)) {
+ CDEBUG(D_LAYOUT, "buf len %zu is less than %u\n",
+ buf->lb_len, le32_to_cpu(comp_v1->lcm_size));
+ RETURN(-EINVAL);
+ }
+
+recheck:
+ mirror_count = 0;
+ if (le16_to_cpu(comp_v1->lcm_entry_count) == 0) {
+ CDEBUG(D_LAYOUT, "entry count is zero\n");
+ RETURN(-EINVAL);
+ }
+
+ if (S_ISREG(lod2lu_obj(lo)->lo_header->loh_attr) &&
+ lo->ldo_comp_cnt > 0) {
+ /* could be called from lustre.lov.add */
+ __u32 cnt = lo->ldo_comp_cnt;
+
+ ext = &lo->ldo_comp_entries[cnt - 1].llc_extent;
+ prev_end = ext->e_end;
+
+ ++mirror_count;
+ }
+
+ for_each_comp_entry_v1(comp_v1, ent) {
+ ext = &ent->lcme_extent;
+
+ if (le64_to_cpu(ext->e_start) > le64_to_cpu(ext->e_end)) {
+ CDEBUG(D_LAYOUT, "invalid extent "DEXT"\n",
+ le64_to_cpu(ext->e_start),
+ le64_to_cpu(ext->e_end));
+ RETURN(-EINVAL);
+ }
+
+ if (is_from_disk) {
+ /* lcme_id contains valid value */
+ if (le32_to_cpu(ent->lcme_id) == 0 ||
+ le32_to_cpu(ent->lcme_id) > LCME_ID_MAX) {
+ CDEBUG(D_LAYOUT, "invalid id %u\n",
+ le32_to_cpu(ent->lcme_id));
+ RETURN(-EINVAL);
+ }
+
+ if (le16_to_cpu(comp_v1->lcm_mirror_count) > 0) {
+ mirror_id = mirror_id_of(
+ le32_to_cpu(ent->lcme_id));
+
+ /* first component must start with 0 */
+ if (mirror_id != prev_mid &&
+ le64_to_cpu(ext->e_start) != 0) {
+ CDEBUG(D_LAYOUT,
+ "invalid start:%llu, expect:0\n",
+ le64_to_cpu(ext->e_start));
+ RETURN(-EINVAL);
+ }
+
+ prev_mid = mirror_id;
+ }
+ }
+
+ if (le64_to_cpu(ext->e_start) == 0) {
+ ++mirror_count;
+ prev_end = 0;
+ }
+
+ /* the next must be adjacent with the previous one */
+ if (le64_to_cpu(ext->e_start) != prev_end) {
+ CDEBUG(D_LAYOUT,
+ "invalid start actual:%llu, expect:%llu\n",
+ le64_to_cpu(ext->e_start), prev_end);
+ RETURN(-EINVAL);
+ }
+
+ tmp.lb_buf = (char *)comp_v1 + le32_to_cpu(ent->lcme_offset);
+ tmp.lb_len = le32_to_cpu(ent->lcme_size);
+
+ /* Check DoM entry is always the first one */
+ lum = tmp.lb_buf;
+ if (lov_pattern(le32_to_cpu(lum->lmm_pattern)) ==
+ LOV_PATTERN_MDT) {
+ /* DoM component must be the first in a mirror */
+ if (le64_to_cpu(ext->e_start) > 0) {
+ CDEBUG(D_LAYOUT, "invalid DoM component "
+ "with %llu extent start\n",
+ le64_to_cpu(ext->e_start));
+ RETURN(-EINVAL);
+ }
+ stripe_size = le32_to_cpu(lum->lmm_stripe_size);
+ /* There is just one stripe on MDT and it must
+ * cover whole component size. */
+ if (stripe_size != le64_to_cpu(ext->e_end)) {
+ CDEBUG(D_LAYOUT, "invalid DoM layout "
+ "stripe size %u != %llu "
+ "(component size)\n",
+ stripe_size, prev_end);
+ RETURN(-EINVAL);
+ }
+ /* Check and adjust stripe size by per-MDT limit */
+ rc = lod_dom_stripesize_choose(env, d, comp_v1, ent,
+ stripe_size);
+ /* DoM entry was removed, re-check layout from start */
+ if (rc == -ERESTART)
+ goto recheck;
+ else if (rc)
+ RETURN(rc);
+
+ /* Any stripe count is forbidden on DoM component */
+ if (lum->lmm_stripe_count) {
+ CDEBUG(D_LAYOUT,
+ "invalid DoM layout stripe count %u, must be 0\n",
+ le16_to_cpu(lum->lmm_stripe_count));
+ RETURN(-EINVAL);
+ }
+
+ /* Any pool is forbidden on DoM component */
+ if (lum->lmm_magic == LOV_USER_MAGIC_V3) {
+ struct lov_user_md_v3 *v3 = (void *)lum;
+
+ if (v3->lmm_pool_name[0] != '\0') {
+ CDEBUG(D_LAYOUT,
+ "DoM component cannot have pool assigned\n");
+ RETURN(-EINVAL);
+ }
+ }
+ }
+
+ prev_end = le64_to_cpu(ext->e_end);
+
+ rc = lod_verify_v1v3(d, &tmp, is_from_disk);
+ if (rc)
+ RETURN(rc);
+
+ if (prev_end == LUSTRE_EOF)
+ continue;
+
+ /* extent end must be aligned with the stripe_size */
+ stripe_size = le32_to_cpu(lum->lmm_stripe_size);
+ if (stripe_size && prev_end % stripe_size) {
+ CDEBUG(D_LAYOUT, "stripe size isn't aligned, "
+ "stripe_sz: %u, [%llu, %llu)\n",
+ stripe_size, ext->e_start, prev_end);
+ RETURN(-EINVAL);
+ }
+ }
+
+ /* make sure that the mirror_count is telling the truth */
+ if (mirror_count != le16_to_cpu(comp_v1->lcm_mirror_count) + 1)
+ RETURN(-EINVAL);
+
+ RETURN(0);
+}
+
+/**
+ * set the default stripe size, if unset.
+ *
+ * \param[in,out] val number of bytes per OST stripe
+ *
+ * The minimum stripe size is 64KB to ensure that a single stripe is an
+ * even multiple of a client PAGE_SIZE (IA64, PPC, etc). Otherwise, it
+ * is difficult to split dirty pages across OSCs during writes.
+ */