@@ -176,6 +176,11 @@ struct lov_comp_layout_entry_ops {
struct lov_layout_raid0 {
unsigned int lo_nr;
/**
+ * record the stripe no before the truncate size, used for setting OST
+ * object size for truncate. LU-14128.
+ */
+ int lo_trunc_stripeno;
+ /**
* When this is true, lov_object::lo_attr contains
* valid up to date attributes for a top-level
* object. This field is reset to 0 when attributes of
@@ -264,6 +264,7 @@ int lov_merge_lvb_kms(struct lov_stripe_md *lsm, int index,
struct ost_lvb *lvb, u64 *kms_place);
/* lov_offset.c */
+u64 stripe_width(struct lov_stripe_md *lsm, unsigned int index);
u64 lov_stripe_size(struct lov_stripe_md *lsm, int index, u64 ost_size,
int stripeno);
int lov_stripe_offset(struct lov_stripe_md *lsm, int index, u64 lov_off,
@@ -752,6 +752,24 @@ static u64 lov_offset_mod(u64 val, int delta)
return val;
}
+static int lov_io_add_sub(const struct lu_env *env, struct lov_io *lio,
+ struct lov_io_sub *sub, u64 start, u64 end)
+{
+ int rc;
+
+ end = lov_offset_mod(end, 1);
+ lov_io_sub_inherit(sub, lio, start, end);
+ rc = cl_io_iter_init(sub->sub_env, &sub->sub_io);
+ if (rc != 0) {
+ cl_io_iter_fini(sub->sub_env, &sub->sub_io);
+ return rc;
+ }
+
+ list_add_tail(&sub->sub_linkage, &lio->lis_active);
+
+ return rc;
+}
+
static int lov_io_iter_init(const struct lu_env *env,
const struct cl_io_slice *ios)
{
@@ -768,10 +786,13 @@ static int lov_io_iter_init(const struct lu_env *env,
lov_foreach_io_layout(index, lio, &ext) {
struct lov_layout_entry *le = lov_entry(lio->lis_object, index);
struct lov_layout_raid0 *r0 = &le->lle_raid0;
+ bool tested_trunc_stripe = false;
int stripe;
u64 start;
u64 end;
+ r0->lo_trunc_stripeno = -1;
+
CDEBUG(D_VFSTRACE, "component[%d] flags %#x\n",
index, lsm->lsm_entries[index]->lsme_flags);
if (!lsm_entry_inited(lsm, index)) {
@@ -801,28 +822,76 @@ static int lov_io_iter_init(const struct lu_env *env,
continue;
}
- end = lov_offset_mod(end, 1);
+ if (cl_io_is_trunc(ios->cis_io) &&
+ !tested_trunc_stripe) {
+ int prev;
+ u64 tr_start;
+
+ prev = (stripe == 0) ? r0->lo_nr - 1 :
+ stripe - 1;
+ /**
+ * Only involving previous stripe if the
+ * truncate in this component is at the
+ * beginning of this stripe.
+ */
+ tested_trunc_stripe = true;
+ if (ext.e_start <
+ lsm->lsm_entries[index]->lsme_extent.e_start) {
+ /* need previous stripe involvement */
+ r0->lo_trunc_stripeno = prev;
+ } else {
+ tr_start = ext.e_start;
+ tr_start = lov_do_div64(tr_start,
+ stripe_width(lsm, index));
+ /* tr_start %= stripe_swidth */
+ if (tr_start == stripe * lsm->lsm_entries[index]->lsme_stripe_size)
+ r0->lo_trunc_stripeno = prev;
+ }
+ }
+
+ /* if the last stripe is the trunc stripeno */
+ if (r0->lo_trunc_stripeno == stripe)
+ r0->lo_trunc_stripeno = -1;
+
sub = lov_sub_get(env, lio,
lov_comp_index(index, stripe));
- if (IS_ERR(sub)) {
- rc = PTR_ERR(sub);
- break;
- }
+ if (IS_ERR(sub))
+ return PTR_ERR(sub);
- lov_io_sub_inherit(sub, lio, start, end);
- rc = cl_io_iter_init(sub->sub_env, &sub->sub_io);
- if (rc) {
- cl_io_iter_fini(sub->sub_env, &sub->sub_io);
+ rc = lov_io_add_sub(env, lio, sub, start, end);
+ if (rc)
break;
+ }
+ if (rc != 0)
+ break;
+
+ if (r0->lo_trunc_stripeno != -1) {
+ stripe = r0->lo_trunc_stripeno;
+ if (unlikely(!r0->lo_sub[stripe])) {
+ r0->lo_trunc_stripeno = -1;
+ continue;
}
+ sub = lov_sub_get(env, lio,
+ lov_comp_index(index, stripe));
+ if (IS_ERR(sub))
+ return PTR_ERR(sub);
- CDEBUG(D_VFSTRACE, "shrink: %d [%llu, %llu)\n",
- stripe, start, end);
+ /**
+ * the prev sub could be used by another truncate, we'd
+ * skip it. LU-14128 happends when expand truncate +
+ * read get wrong kms.
+ */
+ if (!list_empty(&sub->sub_linkage)) {
+ r0->lo_trunc_stripeno = -1;
+ continue;
+ }
- list_add_tail(&sub->sub_linkage, &lio->lis_active);
+ (void)lov_stripe_intersects(lsm, index, stripe, &ext,
+ &start, &end);
+ rc = lov_io_add_sub(env, lio, sub, start, end);
+ if (rc != 0)
+ break;
}
- if (rc)
- break;
}
return rc;
}
@@ -111,6 +111,7 @@ static int lov_sublock_init(const struct lu_env *env,
* through already created sub-locks (possibly shared with other top-locks).
*/
static struct lov_lock *lov_lock_sub_init(const struct lu_env *env,
+ const struct cl_io *io,
const struct cl_object *obj,
struct cl_lock *lock)
{
@@ -135,10 +136,14 @@ static struct lov_lock *lov_lock_sub_init(const struct lu_env *env,
struct lov_layout_raid0 *r0 = lov_r0(lov, index);
for (i = 0; i < r0->lo_nr; i++) {
- if (likely(r0->lo_sub[i]) && /* spare layout */
- lov_stripe_intersects(lov->lo_lsm, index, i,
- &ext, &start, &end))
- nr++;
+ if (likely(r0->lo_sub[i])) { /* spare layout */
+ if (lov_stripe_intersects(lov->lo_lsm, index, i,
+ &ext, &start, &end))
+ nr++;
+ else if (cl_io_is_trunc(io) &&
+ r0->lo_trunc_stripeno == i)
+ nr++;
+ }
}
}
/**
@@ -160,12 +165,22 @@ static struct lov_lock *lov_lock_sub_init(const struct lu_env *env,
for (i = 0; i < r0->lo_nr; ++i) {
struct lov_lock_sub *lls = &lovlck->lls_sub[nr];
struct cl_lock_descr *descr = &lls->sub_lock.cll_descr;
+ bool intersect = false;
- if (unlikely(!r0->lo_sub[i]) ||
- !lov_stripe_intersects(lov->lo_lsm, index, i,
- &ext, &start, &end))
+ if (unlikely(!r0->lo_sub[i]))
continue;
+ intersect = lov_stripe_intersects(lov->lo_lsm, index, i,
+ &ext, &start, &end);
+ if (intersect)
+ goto init_sublock;
+
+ if (cl_io_is_trunc(io) && i == r0->lo_trunc_stripeno)
+ goto init_sublock;
+
+ continue;
+
+init_sublock:
LASSERT(!descr->cld_obj);
descr->cld_obj = lovsub2cl(r0->lo_sub[i]);
descr->cld_start = cl_index(descr->cld_obj, start);
@@ -308,7 +323,7 @@ int lov_lock_init_composite(const struct lu_env *env, struct cl_object *obj,
struct lov_lock *lck;
int result = 0;
- lck = lov_lock_sub_init(env, obj, lock);
+ lck = lov_lock_sub_init(env, io, obj, lock);
if (!IS_ERR(lck))
cl_lock_slice_add(lock, &lck->lls_cl, obj, &lov_lock_ops);
else
@@ -215,6 +215,7 @@ static int lov_init_raid0(const struct lu_env *env, struct lov_device *dev,
spin_lock_init(&r0->lo_sub_lock);
r0->lo_nr = lse->lsme_stripe_count;
+ r0->lo_trunc_stripeno = -1;
flags = memalloc_nofs_save();
r0->lo_sub = kvmalloc_array(r0->lo_nr, sizeof(r0->lo_sub[0]),
@@ -37,7 +37,7 @@
#include "lov_internal.h"
-static u64 stripe_width(struct lov_stripe_md *lsm, unsigned int index)
+u64 stripe_width(struct lov_stripe_md *lsm, unsigned int index)
{
struct lov_stripe_md_entry *entry = lsm->lsm_entries[index];