Whamcloud - gitweb
LU-14128 lov: correctly set OST obj size
[fs/lustre-release.git] / lustre / lov / lov_io.c
index 6ac8dc7..b9592d1 100644 (file)
@@ -56,7 +56,7 @@ static inline struct lov_io_sub *lov_sub_alloc(struct lov_io *lio, int index)
                OBD_ALLOC_PTR(sub);
        }
 
-       if (sub != NULL) {
+       if (sub) {
                INIT_LIST_HEAD(&sub->sub_list);
                INIT_LIST_HEAD(&sub->sub_linkage);
                sub->sub_subio_index = index;
@@ -82,7 +82,7 @@ static void lov_io_sub_fini(const struct lu_env *env, struct lov_io *lio,
 
        cl_io_fini(sub->sub_env, &sub->sub_io);
 
-       if (sub->sub_env != NULL && !IS_ERR(sub->sub_env)) {
+       if (sub->sub_env && !IS_ERR(sub->sub_env)) {
                cl_env_put(sub->sub_env, &sub->sub_refcheck);
                sub->sub_env = NULL;
        }
@@ -115,15 +115,19 @@ static int lov_io_sub_init(const struct lu_env *env, struct lov_io *lio,
                     !lov_r0(lov, index)->lo_sub[stripe]))
                RETURN(-EIO);
 
-       LASSERTF(is_index_within_mirror(lov, index, lio->lis_mirror_index),
+       LASSERTF(ergo(lov_is_flr(lov),
+                     is_index_within_mirror(lov, index,
+                                            lio->lis_mirror_index)),
                 DFID "iot = %d, index = %d, mirror = %d\n",
                 PFID(lu_object_fid(lov2lu(lov))), io->ci_type, index,
                 lio->lis_mirror_index);
 
        /* obtain new environment */
        sub->sub_env = cl_env_get(&sub->sub_refcheck);
-       if (IS_ERR(sub->sub_env))
+       if (IS_ERR(sub->sub_env)) {
                result = PTR_ERR(sub->sub_env);
+               RETURN(result);
+       }
 
        sub_obj = lovsub2cl(lov_r0(lov, index)->lo_sub[stripe]);
        sub_io  = &sub->sub_io;
@@ -136,10 +140,11 @@ static int lov_io_sub_init(const struct lu_env *env, struct lov_io *lio,
        sub_io->ci_type    = io->ci_type;
        sub_io->ci_no_srvlock = io->ci_no_srvlock;
        sub_io->ci_noatime = io->ci_noatime;
-       sub_io->ci_pio = io->ci_pio;
+       sub_io->ci_async_readahead = io->ci_async_readahead;
        sub_io->ci_lock_no_expand = io->ci_lock_no_expand;
        sub_io->ci_ndelay = io->ci_ndelay;
        sub_io->ci_layout_version = io->ci_layout_version;
+       sub_io->ci_tried_all_mirrors = io->ci_tried_all_mirrors;
 
        result = cl_io_sub_init(sub->sub_env, sub_io, io->ci_type, sub_obj);
 
@@ -166,7 +171,7 @@ struct lov_io_sub *lov_sub_get(const struct lu_env *env,
 
        if (rc == 0) {
                sub = lov_sub_alloc(lio, index);
-               if (sub == NULL)
+               if (!sub)
                        GOTO(out, rc = -ENOMEM);
 
                rc = lov_io_sub_init(env, lio, sub);
@@ -181,6 +186,8 @@ struct lov_io_sub *lov_sub_get(const struct lu_env *env,
 out:
        if (rc < 0)
                sub = ERR_PTR(rc);
+       else
+               sub->sub_io.ci_noquota = lio->lis_cl.cis_io->ci_noquota;
        RETURN(sub);
 }
 
@@ -189,19 +196,6 @@ out:
  * Lov io operations.
  *
  */
-
-int lov_page_index(const struct cl_page *page)
-{
-       const struct cl_page_slice *slice;
-       ENTRY;
-
-       slice = cl_page_at(page, &lov_device_type);
-       LASSERT(slice != NULL);
-       LASSERT(slice->cpl_obj != NULL);
-
-       RETURN(cl2lov_page(slice)->lps_index);
-}
-
 static int lov_io_subio_init(const struct lu_env *env, struct lov_io *lio,
                              struct cl_io *io)
 {
@@ -237,7 +231,8 @@ static int lov_io_mirror_write_intent(struct lov_io *lio,
              cl_io_is_mkwrite(io)))
                RETURN(0);
 
-       /* FLR: check if it needs to send a write intent RPC to server.
+       /*
+        * FLR: check if it needs to send a write intent RPC to server.
         * Writing to sync_pending file needs write intent RPC to change
         * the file state back to write_pending, so that the layout version
         * can be increased when the state changes to sync_pending at a later
@@ -255,8 +250,10 @@ static int lov_io_mirror_write_intent(struct lov_io *lio,
        LASSERT((lov_flr_state(obj) == LCM_FL_WRITE_PENDING));
        LASSERT(comp->lo_preferred_mirror >= 0);
 
-       /* need to iterate all components to see if there are
-        * multiple components covering the writing component */
+       /*
+        * need to iterate all components to see if there are
+        * multiple components covering the writing component
+        */
        primary = &comp->lo_mirrors[comp->lo_preferred_mirror];
        LASSERT(!primary->lre_stale);
        lov_foreach_mirror_layout_entry(obj, lle, primary) {
@@ -264,8 +261,8 @@ static int lov_io_mirror_write_intent(struct lov_io *lio,
                if (!lu_extent_is_overlapped(ext, lle->lle_extent))
                        continue;
 
-               ext->e_start = MIN(ext->e_start, lle->lle_extent->e_start);
-               ext->e_end = MAX(ext->e_end, lle->lle_extent->e_end);
+               ext->e_start = min(ext->e_start, lle->lle_extent->e_start);
+               ext->e_end = max(ext->e_end, lle->lle_extent->e_end);
                ++count;
        }
        if (count == 0) {
@@ -311,8 +308,13 @@ static int lov_io_mirror_init(struct lov_io *lio, struct lov_object *obj,
        ENTRY;
 
        if (!lov_is_flr(obj)) {
-               LASSERT(comp->lo_preferred_mirror == 0);
-               lio->lis_mirror_index = comp->lo_preferred_mirror;
+               /* only locks/pages are manipulated for CIT_MISC op, no
+                * cl_io_loop() will be called, don't check/set mirror info.
+                */
+               if (io->ci_type != CIT_MISC) {
+                       LASSERT(comp->lo_preferred_mirror == 0);
+                       lio->lis_mirror_index = comp->lo_preferred_mirror;
+               }
                io->ci_ndelay = 0;
                RETURN(0);
        }
@@ -332,10 +334,12 @@ static int lov_io_mirror_init(struct lov_io *lio, struct lov_object *obj,
 
                if ((cl_io_is_trunc(io) || io->ci_type == CIT_WRITE) &&
                    (io->ci_layout_version != obj->lo_lsm->lsm_layout_gen)) {
-                       /* For resync I/O, the ci_layout_version was the layout
+                       /*
+                        * For resync I/O, the ci_layout_version was the layout
                         * version when resync starts. If it doesn't match the
                         * current object layout version, it means the layout
-                        * has been changed */
+                        * has been changed
+                        */
                        RETURN(-ESTALE);
                }
 
@@ -365,6 +369,17 @@ static int lov_io_mirror_init(struct lov_io *lio, struct lov_object *obj,
                       PFID(lu_object_fid(lov2lu(obj))),
                       lio->lis_pos, lio->lis_endpos);
 
+               if (cl_io_is_trunc(io)) {
+                       /**
+                        * for truncate, we uses [size, EOF) to judge whether
+                        * a write intent needs to be send, but we need to
+                        * restore the write extent to [0, size], in truncate,
+                        * the byte in the size position is accessed.
+                        */
+                       io->ci_write_intent.e_start = 0;
+                       io->ci_write_intent.e_end =
+                                       io->u.ci_setattr.sa_attr.lvb_size + 1;
+               }
                /* stop cl_io_init() loop */
                RETURN(1);
        }
@@ -401,13 +416,13 @@ static int lov_io_mirror_init(struct lov_io *lio, struct lov_object *obj,
                                found = true;
                                break;
                        }
-               }
-
+               } /* each component of the mirror */
                if (found) {
                        index = (index + i) % comp->lo_mirror_count;
                        break;
                }
-       }
+       } /* each mirror */
+
        if (i == comp->lo_mirror_count) {
                CERROR(DFID": failed to find a component covering "
                       "I/O region at %llu\n",
@@ -426,19 +441,26 @@ static int lov_io_mirror_init(struct lov_io *lio, struct lov_object *obj,
 
        lio->lis_mirror_index = index;
 
-       /* FLR: if all mirrors have been tried once, most likely the network
+       /*
+        * FLR: if all mirrors have been tried once, most likely the network
         * of this client has been partitioned. We should relinquish CPU for
-        * a while before trying again. */
-       ++io->ci_ndelay_tried;
-       if (io->ci_ndelay && io->ci_ndelay_tried >= comp->lo_mirror_count) {
-               set_current_state(TASK_INTERRUPTIBLE);
-               schedule_timeout(msecs_to_jiffies(MSEC_PER_SEC)); /* 10ms */
+        * a while before trying again.
+        */
+       if (io->ci_ndelay && io->ci_ndelay_tried > 0 &&
+           (io->ci_ndelay_tried % comp->lo_mirror_count == 0)) {
+               schedule_timeout_interruptible(cfs_time_seconds(1) / 100);
                if (signal_pending(current))
                        RETURN(-EINTR);
 
-               /* reset retry counter */
-               io->ci_ndelay_tried = 1;
+               /**
+                * we'd set ci_tried_all_mirrors to turn off fast mirror
+                * switching for read after we've tried all mirrors several
+                * rounds.
+                */
+               io->ci_tried_all_mirrors = io->ci_ndelay_tried %
+                                          (comp->lo_mirror_count * 4) == 0;
        }
+       ++io->ci_ndelay_tried;
 
        CDEBUG(D_VFSTRACE, "use %sdelayed RPC state for this IO\n",
               io->ci_ndelay ? "non-" : "");
@@ -449,7 +471,6 @@ static int lov_io_mirror_init(struct lov_io *lio, struct lov_object *obj,
 static int lov_io_slice_init(struct lov_io *lio,
                             struct lov_object *obj, struct cl_io *io)
 {
-       struct lu_extent ext;
        int index;
        int result = 0;
        ENTRY;
@@ -457,19 +478,19 @@ static int lov_io_slice_init(struct lov_io *lio,
        io->ci_result = 0;
        lio->lis_object = obj;
 
-       LASSERT(obj->lo_lsm != NULL);
-
        switch (io->ci_type) {
        case CIT_READ:
        case CIT_WRITE:
-               lio->lis_pos = io->u.ci_rw.rw_range.cir_pos;
-               lio->lis_endpos = lio->lis_pos + io->u.ci_rw.rw_range.cir_count;
+               lio->lis_pos = io->u.ci_rw.crw_pos;
+               lio->lis_endpos = io->u.ci_rw.crw_pos + io->u.ci_rw.crw_count;
                lio->lis_io_endpos = lio->lis_endpos;
                if (cl_io_is_append(io)) {
                        LASSERT(io->ci_type == CIT_WRITE);
 
-                       /* If there is LOV EA hole, then we may cannot locate
-                        * the current file-tail exactly. */
+                       /*
+                        * If there is LOV EA hole, then we may cannot locate
+                        * the current file-tail exactly.
+                        */
                        if (unlikely(obj->lo_lsm->lsm_entries[0]->lsme_pattern &
                                     LOV_PATTERN_F_HOLE))
                                GOTO(out, result = -EIO);
@@ -479,25 +500,31 @@ static int lov_io_slice_init(struct lov_io *lio,
                }
                break;
 
-        case CIT_SETATTR:
-                if (cl_io_is_trunc(io))
-                        lio->lis_pos = io->u.ci_setattr.sa_attr.lvb_size;
-                else
-                        lio->lis_pos = 0;
-                lio->lis_endpos = OBD_OBJECT_EOF;
-                break;
+       case CIT_SETATTR:
+               if (cl_io_is_fallocate(io)) {
+                       lio->lis_pos = io->u.ci_setattr.sa_falloc_offset;
+                       lio->lis_endpos = io->u.ci_setattr.sa_falloc_end;
+               } else if (cl_io_is_trunc(io)) {
+                       lio->lis_pos = io->u.ci_setattr.sa_attr.lvb_size;
+                       lio->lis_endpos = OBD_OBJECT_EOF;
+               } else {
+                       lio->lis_pos = 0;
+                       lio->lis_endpos = OBD_OBJECT_EOF;
+               }
+               break;
 
        case CIT_DATA_VERSION:
                lio->lis_pos = 0;
                lio->lis_endpos = OBD_OBJECT_EOF;
                break;
 
-        case CIT_FAULT: {
-                pgoff_t index = io->u.ci_fault.ft_index;
-                lio->lis_pos = cl_offset(io->ci_obj, index);
-                lio->lis_endpos = cl_offset(io->ci_obj, index + 1);
-                break;
-        }
+       case CIT_FAULT: {
+               pgoff_t index = io->u.ci_fault.ft_index;
+
+               lio->lis_pos = cl_offset(io->ci_obj, index);
+               lio->lis_endpos = cl_offset(io->ci_obj, index + 1);
+               break;
+       }
 
        case CIT_FSYNC: {
                lio->lis_pos = io->u.ci_fsync.fi_start;
@@ -511,6 +538,12 @@ static int lov_io_slice_init(struct lov_io *lio,
                break;
        }
 
+       case CIT_LSEEK: {
+               lio->lis_pos = io->u.ci_lseek.ls_start;
+               lio->lis_endpos = OBD_OBJECT_EOF;
+               break;
+       }
+
        case CIT_GLIMPSE:
                lio->lis_pos = 0;
                lio->lis_endpos = OBD_OBJECT_EOF;
@@ -521,14 +554,23 @@ static int lov_io_slice_init(struct lov_io *lio,
                        GOTO(out, result = 1);
                break;
 
-        case CIT_MISC:
-                lio->lis_pos = 0;
-                lio->lis_endpos = OBD_OBJECT_EOF;
-                break;
+       case CIT_MISC:
+               lio->lis_pos = 0;
+               lio->lis_endpos = OBD_OBJECT_EOF;
+               break;
+
+       default:
+               LBUG();
+       }
+
+       /*
+        * CIT_MISC + ci_ignore_layout can identify the I/O from the OSC layer,
+        * it won't care/access lov layout related info.
+        */
+       if (io->ci_ignore_layout && io->ci_type == CIT_MISC)
+               GOTO(out, result = 0);
 
-        default:
-                LBUG();
-        }
+       LASSERT(obj->lo_lsm != NULL);
 
        result = lov_io_mirror_init(lio, obj, io);
        if (result)
@@ -536,22 +578,32 @@ static int lov_io_slice_init(struct lov_io *lio,
 
        /* check if it needs to instantiate layout */
        if (!(io->ci_type == CIT_WRITE || cl_io_is_mkwrite(io) ||
+             cl_io_is_fallocate(io) ||
              (cl_io_is_trunc(io) && io->u.ci_setattr.sa_attr.lvb_size > 0)))
                GOTO(out, result = 0);
 
-       io->ci_write_intent.e_start = lio->lis_pos;
-       io->ci_write_intent.e_end = lio->lis_endpos;
-
-       ext = io->ci_write_intent;
-       /* for truncate, it only needs to instantiate the components
-        * before the truncated size. */
+       /*
+        * for truncate, it only needs to instantiate the components
+        * before the truncated size.
+        */
        if (cl_io_is_trunc(io)) {
-               ext.e_start = 0;
-               ext.e_end = io->u.ci_setattr.sa_attr.lvb_size;
+               io->ci_write_intent.e_start = 0;
+               /* for writes, e_end is endpos, the location of the file
+                * pointer after the write is completed, so it is not accessed.
+                * For truncate, 'end' is the size, and *is* acccessed.
+                * In other words, writes are [start, end), but truncate is
+                * [start, size], where both are included.  So add 1 to the
+                * size when creating the write intent to account for this.
+                */
+               io->ci_write_intent.e_end =
+                       io->u.ci_setattr.sa_attr.lvb_size + 1;
+       } else {
+               io->ci_write_intent.e_start = lio->lis_pos;
+               io->ci_write_intent.e_end = lio->lis_endpos;
        }
 
        index = 0;
-       lov_foreach_io_layout(index, lio, &ext) {
+       lov_foreach_io_layout(index, lio, &io->ci_write_intent) {
                if (!lsm_entry_inited(obj->lo_lsm, index)) {
                        io->ci_need_write_intent = 1;
                        break;
@@ -559,10 +611,12 @@ static int lov_io_slice_init(struct lov_io *lio,
        }
 
        if (io->ci_need_write_intent && io->ci_designated_mirror > 0) {
-               /* REINT_SYNC RPC has already tried to instantiate all of the
+               /*
+                * REINT_SYNC RPC has already tried to instantiate all of the
                 * components involved, obviously it didn't succeed. Skip this
                 * mirror for now. The server won't be able to figure out
-                * which mirror it should instantiate components */
+                * which mirror it should instantiate components
+                */
                CERROR(DFID": trying to instantiate components for designated "
                       "I/O, file state: %d\n",
                       PFID(lu_object_fid(lov2lu(obj))), lov_flr_state(obj));
@@ -617,22 +671,31 @@ static void lov_io_sub_inherit(struct lov_io_sub *sub, struct lov_io *lio,
        int index = lov_comp_entry(sub->sub_subio_index);
        int stripe = lov_comp_stripe(sub->sub_subio_index);
 
-       io->ci_pio = parent->ci_pio;
        switch (io->ci_type) {
        case CIT_SETATTR: {
                io->u.ci_setattr.sa_attr = parent->u.ci_setattr.sa_attr;
                io->u.ci_setattr.sa_attr_flags =
                        parent->u.ci_setattr.sa_attr_flags;
-               io->u.ci_setattr.sa_valid = parent->u.ci_setattr.sa_valid;
+               io->u.ci_setattr.sa_avalid = parent->u.ci_setattr.sa_avalid;
+               io->u.ci_setattr.sa_xvalid = parent->u.ci_setattr.sa_xvalid;
+               io->u.ci_setattr.sa_falloc_mode =
+                       parent->u.ci_setattr.sa_falloc_mode;
                io->u.ci_setattr.sa_stripe_index = stripe;
                io->u.ci_setattr.sa_parent_fid =
                                        parent->u.ci_setattr.sa_parent_fid;
+               /* For SETATTR(fallocate) pass the subtype to lower IO */
+               io->u.ci_setattr.sa_subtype = parent->u.ci_setattr.sa_subtype;
                if (cl_io_is_trunc(io)) {
                        loff_t new_size = parent->u.ci_setattr.sa_attr.lvb_size;
 
                        new_size = lov_size_to_stripe(lsm, index, new_size,
                                                      stripe);
                        io->u.ci_setattr.sa_attr.lvb_size = new_size;
+               } else if (cl_io_is_fallocate(io)) {
+                       io->u.ci_setattr.sa_falloc_offset = start;
+                       io->u.ci_setattr.sa_falloc_end = end;
+                       io->u.ci_setattr.sa_attr.lvb_size =
+                               parent->u.ci_setattr.sa_attr.lvb_size;
                }
                lov_lsm2layout(lsm, lsm->lsm_entries[index],
                               &io->u.ci_setattr.sa_layout);
@@ -662,16 +725,13 @@ static void lov_io_sub_inherit(struct lov_io_sub *sub, struct lov_io *lio,
        }
        case CIT_READ:
        case CIT_WRITE: {
-               io->u.ci_rw.rw_ptask = parent->u.ci_rw.rw_ptask;
-               io->u.ci_rw.rw_iter = parent->u.ci_rw.rw_iter;
-               io->u.ci_rw.rw_iocb = parent->u.ci_rw.rw_iocb;
-               io->u.ci_rw.rw_file = parent->u.ci_rw.rw_file;
-               io->u.ci_rw.rw_sync = parent->u.ci_rw.rw_sync;
+               io->u.ci_wr.wr_sync = cl_io_is_sync_write(parent);
+               io->ci_tried_all_mirrors = parent->ci_tried_all_mirrors;
                if (cl_io_is_append(parent)) {
-                       io->u.ci_rw.rw_append = 1;
+                       io->u.ci_wr.wr_append = 1;
                } else {
-                       io->u.ci_rw.rw_range.cir_pos = start;
-                       io->u.ci_rw.rw_range.cir_count = end - start;
+                       io->u.ci_rw.crw_pos = start;
+                       io->u.ci_rw.crw_count = end - start;
                }
                break;
        }
@@ -683,6 +743,12 @@ static void lov_io_sub_inherit(struct lov_io_sub *sub, struct lov_io *lio,
                io->u.ci_ladvise.li_flags = parent->u.ci_ladvise.li_flags;
                break;
        }
+       case CIT_LSEEK: {
+               io->u.ci_lseek.ls_start = start;
+               io->u.ci_lseek.ls_whence = parent->u.ci_lseek.ls_whence;
+               io->u.ci_lseek.ls_result = parent->u.ci_lseek.ls_result;
+               break;
+       }
        case CIT_GLIMPSE:
        case CIT_MISC:
        default:
@@ -692,22 +758,39 @@ static void lov_io_sub_inherit(struct lov_io_sub *sub, struct lov_io *lio,
 
 static loff_t lov_offset_mod(loff_t val, int delta)
 {
-        if (val != OBD_OBJECT_EOF)
-                val += delta;
-        return val;
+       if (val != OBD_OBJECT_EOF)
+               val += 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)
 {
-       struct lov_io        *lio = cl2lov_io(env, ios);
+       struct lov_io *lio = cl2lov_io(env, ios);
        struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
-       struct lov_io_sub    *sub;
+       struct lov_io_sub *sub;
        struct lu_extent ext;
        int index;
        int rc = 0;
 
-        ENTRY;
+       ENTRY;
 
        ext.e_start = lio->lis_pos;
        ext.e_end = lio->lis_endpos;
@@ -718,12 +801,17 @@ static int lov_io_iter_init(const struct lu_env *env,
                u64 start;
                u64 end;
                int stripe;
+               bool tested_trunc_stripe = false;
+
+               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)) {
-                       /* Read from uninitialized components should return
-                        * zero filled pages. */
+                       /*
+                        * Read from uninitialized components should return
+                        * zero filled pages.
+                        */
                        continue;
                }
 
@@ -738,7 +826,7 @@ static int lov_io_iter_init(const struct lu_env *env,
                                                   &ext, &start, &end))
                                continue;
 
-                       if (unlikely(r0->lo_sub[stripe] == NULL)) {
+                       if (unlikely(!r0->lo_sub[stripe])) {
                                if (ios->cis_io->ci_type == CIT_READ ||
                                    ios->cis_io->ci_type == CIT_WRITE ||
                                    ios->cis_io->ci_type == CIT_FAULT)
@@ -747,29 +835,79 @@ 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);
+                       if (IS_ERR(sub))
+                               return PTR_ERR(sub);
+
+                       rc = lov_io_add_sub(env, lio, sub, start, end);
+                       if (rc != 0)
                                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);
+
+                       /**
+                        * 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;
                        }
 
-                       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);
+                       (void)lov_stripe_intersects(lsm, index, stripe, &ext,
+                                                   &start, &end);
+                       rc = lov_io_add_sub(env, lio, sub, start, end);
                        if (rc != 0)
                                break;
 
-                       CDEBUG(D_VFSTRACE,
-                               "shrink stripe: {%d, %d} range: [%llu, %llu)\n",
-                               index, stripe, start, end);
-
-                       list_add_tail(&sub->sub_linkage, &lio->lis_active);
                }
-               if (rc != 0)
-                       break;
        }
        RETURN(rc);
 }
@@ -777,11 +915,10 @@ static int lov_io_iter_init(const struct lu_env *env,
 static int lov_io_rw_iter_init(const struct lu_env *env,
                               const struct cl_io_slice *ios)
 {
-       struct cl_io *io = ios->cis_io;
        struct lov_io *lio = cl2lov_io(env, ios);
+       struct cl_io *io = ios->cis_io;
        struct lov_stripe_md_entry *lse;
-       struct cl_io_range *range = &io->u.ci_rw.rw_range;
-       loff_t start = range->cir_pos;
+       loff_t start = io->u.ci_rw.crw_pos;
        loff_t next;
        int index;
 
@@ -791,14 +928,14 @@ static int lov_io_rw_iter_init(const struct lu_env *env,
        if (cl_io_is_append(io))
                RETURN(lov_io_iter_init(env, ios));
 
-       index = lov_io_layout_at(lio, range->cir_pos);
+       index = lov_io_layout_at(lio, io->u.ci_rw.crw_pos);
        if (index < 0) { /* non-existing layout component */
                if (io->ci_type == CIT_READ) {
-                       /* TODO: it needs to detect the next component and
-                        * then set the next pos */
+                       /*
+                        * TODO: it needs to detect the next component and
+                        * then set the next pos
+                        */
                        io->ci_continue = 0;
-                       /* execute it in main thread */
-                       io->ci_pio = 0;
 
                        RETURN(lov_io_iter_init(env, ios));
                }
@@ -822,28 +959,20 @@ static int lov_io_rw_iter_init(const struct lu_env *env,
                        next = MAX_LFS_FILESIZE;
        }
 
-       LASSERTF(range->cir_pos >= lse->lsme_extent.e_start,
-                "pos %lld, [%lld, %lld)\n", range->cir_pos,
+       LASSERTF(io->u.ci_rw.crw_pos >= lse->lsme_extent.e_start,
+                "pos %lld, [%lld, %lld)\n", io->u.ci_rw.crw_pos,
                 lse->lsme_extent.e_start, lse->lsme_extent.e_end);
        next = min_t(__u64, next, lse->lsme_extent.e_end);
        next = min_t(loff_t, next, lio->lis_io_endpos);
 
-       io->ci_continue  = next < lio->lis_io_endpos;
-       range->cir_count = next - range->cir_pos;
-       lio->lis_pos     = range->cir_pos;
-       lio->lis_endpos  = range->cir_pos + range->cir_count;
+       io->ci_continue = next < lio->lis_io_endpos;
+       io->u.ci_rw.crw_count = next - io->u.ci_rw.crw_pos;
+       lio->lis_pos    = io->u.ci_rw.crw_pos;
+       lio->lis_endpos = io->u.ci_rw.crw_pos + io->u.ci_rw.crw_count;
        CDEBUG(D_VFSTRACE,
-              "stripe: {%d, %llu} range: [%llu, %llu) end: %llu, count: %zd\n",
-              index, start, lio->lis_pos, lio->lis_endpos,
-              lio->lis_io_endpos, range->cir_count);
-
-       if (!io->ci_continue) {
-               /* the last piece of IO, execute it in main thread */
-               io->ci_pio = 0;
-       }
-
-       if (io->ci_pio)
-               RETURN(0);
+              "stripe: %llu chunk: [%llu, %llu) %llu, %zd\n",
+              (__u64)start, lio->lis_pos, lio->lis_endpos,
+              (__u64)lio->lis_io_endpos, io->u.ci_rw.crw_count);
 
        /*
         * XXX The following call should be optimized: we know, that
@@ -891,49 +1020,49 @@ static int lov_io_call(const struct lu_env *env, struct lov_io *lio,
 
 static int lov_io_lock(const struct lu_env *env, const struct cl_io_slice *ios)
 {
-        ENTRY;
-        RETURN(lov_io_call(env, cl2lov_io(env, ios), cl_io_lock));
+       ENTRY;
+       RETURN(lov_io_call(env, cl2lov_io(env, ios), cl_io_lock));
 }
 
 static int lov_io_start(const struct lu_env *env, const struct cl_io_slice *ios)
 {
-        ENTRY;
-        RETURN(lov_io_call(env, cl2lov_io(env, ios), cl_io_start));
+       ENTRY;
+       RETURN(lov_io_call(env, cl2lov_io(env, ios), cl_io_start));
 }
 
 static int lov_io_end_wrapper(const struct lu_env *env, struct cl_io *io)
 {
-        ENTRY;
-        /*
-         * It's possible that lov_io_start() wasn't called against this
-         * sub-io, either because previous sub-io failed, or upper layer
-         * completed IO.
-         */
-        if (io->ci_state == CIS_IO_GOING)
-                cl_io_end(env, io);
-        else
-                io->ci_state = CIS_IO_FINISHED;
-        RETURN(0);
+       ENTRY;
+       /*
+        * It's possible that lov_io_start() wasn't called against this
+        * sub-io, either because previous sub-io failed, or upper layer
+        * completed IO.
+        */
+       if (io->ci_state == CIS_IO_GOING)
+               cl_io_end(env, io);
+       else
+               io->ci_state = CIS_IO_FINISHED;
+       RETURN(0);
 }
 
 static int lov_io_iter_fini_wrapper(const struct lu_env *env, struct cl_io *io)
 {
-        cl_io_iter_fini(env, io);
-        RETURN(0);
+       cl_io_iter_fini(env, io);
+       RETURN(0);
 }
 
 static int lov_io_unlock_wrapper(const struct lu_env *env, struct cl_io *io)
 {
-        cl_io_unlock(env, io);
-        RETURN(0);
+       cl_io_unlock(env, io);
+       RETURN(0);
 }
 
 static void lov_io_end(const struct lu_env *env, const struct cl_io_slice *ios)
 {
-        int rc;
+       int rc;
 
-        rc = lov_io_call(env, cl2lov_io(env, ios), lov_io_end_wrapper);
-        LASSERT(rc == 0);
+       rc = lov_io_call(env, cl2lov_io(env, ios), lov_io_end_wrapper);
+       LASSERT(rc == 0);
 }
 
 static void
@@ -948,7 +1077,7 @@ lov_io_data_version_end(const struct lu_env *env, const struct cl_io_slice *ios)
        list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
                struct cl_data_version_io *sdv = &sub->sub_io.u.ci_data_version;
 
-               lov_io_end_wrapper(env, &sub->sub_io);
+               lov_io_end_wrapper(sub->sub_env, &sub->sub_io);
 
                pdv->dv_data_version += sdv->dv_data_version;
                if (pdv->dv_layout_version > sdv->dv_layout_version)
@@ -964,26 +1093,26 @@ lov_io_data_version_end(const struct lu_env *env, const struct cl_io_slice *ios)
 static void lov_io_iter_fini(const struct lu_env *env,
                              const struct cl_io_slice *ios)
 {
-        struct lov_io *lio = cl2lov_io(env, ios);
-        int rc;
+       struct lov_io *lio = cl2lov_io(env, ios);
+       int rc;
 
-        ENTRY;
-        rc = lov_io_call(env, lio, lov_io_iter_fini_wrapper);
-        LASSERT(rc == 0);
+       ENTRY;
+       rc = lov_io_call(env, lio, lov_io_iter_fini_wrapper);
+       LASSERT(rc == 0);
        while (!list_empty(&lio->lis_active))
                list_del_init(lio->lis_active.next);
-        EXIT;
+       EXIT;
 }
 
 static void lov_io_unlock(const struct lu_env *env,
                           const struct cl_io_slice *ios)
 {
-        int rc;
+       int rc;
 
-        ENTRY;
-        rc = lov_io_call(env, cl2lov_io(env, ios), lov_io_unlock_wrapper);
-        LASSERT(rc == 0);
-        EXIT;
+       ENTRY;
+       rc = lov_io_call(env, cl2lov_io(env, ios), lov_io_unlock_wrapper);
+       LASSERT(rc == 0);
+       EXIT;
 }
 
 static int lov_io_read_ahead(const struct lu_env *env,
@@ -1016,7 +1145,7 @@ static int lov_io_read_ahead(const struct lu_env *env,
        stripe = lov_stripe_number(loo->lo_lsm, index, offset);
 
        r0 = lov_r0(loo, index);
-       if (unlikely(r0->lo_sub[stripe] == NULL))
+       if (unlikely(!r0->lo_sub[stripe]))
                RETURN(-EIO);
 
        sub = lov_sub_get(env, lio, lov_comp_index(index, stripe));
@@ -1029,7 +1158,8 @@ static int lov_io_read_ahead(const struct lu_env *env,
                              ra);
 
        CDEBUG(D_READA, DFID " cra_end = %lu, stripes = %d, rc = %d\n",
-              PFID(lu_object_fid(lov2lu(loo))), ra->cra_end, r0->lo_nr, rc);
+              PFID(lu_object_fid(lov2lu(loo))), ra->cra_end_idx,
+                   r0->lo_nr, rc);
        if (rc != 0)
                RETURN(rc);
 
@@ -1041,29 +1171,29 @@ static int lov_io_read_ahead(const struct lu_env *env,
         */
 
        /* cra_end is stripe level, convert it into file level */
-       ra_end = ra->cra_end;
+       ra_end = ra->cra_end_idx;
        if (ra_end != CL_PAGE_EOF)
-               ra->cra_end = lov_stripe_pgoff(loo->lo_lsm, index,
-                                              ra_end, stripe);
+               ra->cra_end_idx = lov_stripe_pgoff(loo->lo_lsm, index,
+                                                  ra_end, stripe);
 
        /* boundary of current component */
        ra_end = cl_index(obj, (loff_t)lov_io_extent(lio, index)->e_end);
-       if (ra_end != CL_PAGE_EOF && ra->cra_end >= ra_end)
-               ra->cra_end = ra_end - 1;
+       if (ra_end != CL_PAGE_EOF && ra->cra_end_idx >= ra_end)
+               ra->cra_end_idx = ra_end - 1;
 
        if (r0->lo_nr == 1) /* single stripe file */
                RETURN(0);
 
        pps = lov_lse(loo, index)->lsme_stripe_size >> PAGE_SHIFT;
 
-       CDEBUG(D_READA, DFID " max_index = %lu, pps = %u, index = %u, "
+       CDEBUG(D_READA, DFID " max_index = %lu, pps = %u, index = %d, "
               "stripe_size = %u, stripe no = %u, start index = %lu\n",
-              PFID(lu_object_fid(lov2lu(loo))), ra->cra_end, pps, index,
+              PFID(lu_object_fid(lov2lu(loo))), ra->cra_end_idx, pps, index,
               lov_lse(loo, index)->lsme_stripe_size, stripe, start);
 
        /* never exceed the end of the stripe */
-       ra->cra_end = min_t(pgoff_t,
-                           ra->cra_end, start + pps - start % pps - 1);
+       ra->cra_end_idx = min_t(pgoff_t, ra->cra_end_idx,
+                               start + pps - start % pps - 1);
        RETURN(0);
 }
 
@@ -1091,6 +1221,7 @@ static int lov_io_submit(const struct lu_env *env,
        struct lov_io_sub       *sub;
        struct cl_page_list     *plist = &lov_env_info(env)->lti_plist;
        struct cl_page          *page;
+       struct cl_page          *tmp;
        int index;
        int rc = 0;
        ENTRY;
@@ -1103,9 +1234,11 @@ static int lov_io_submit(const struct lu_env *env,
                if (lov_page_is_empty(page)) {
                        cl_page_list_move(&queue->c2_qout, qin, page);
 
-                       /* it could only be mirror read to get here therefore
+                       /*
+                        * it could only be mirror read to get here therefore
                         * the pages will be transient. We don't care about
-                        * the return code of cl_page_prep() at all. */
+                        * the return code of cl_page_prep() at all.
+                        */
                        (void) cl_page_prep(env, ios->cis_io, page, crt);
                        cl_page_completion(env, page, crt, 0);
                        continue;
@@ -1114,11 +1247,11 @@ static int lov_io_submit(const struct lu_env *env,
                cl_2queue_init(cl2q);
                cl_page_list_move(&cl2q->c2_qin, qin, page);
 
-               index = lov_page_index(page);
-               while (qin->pl_nr > 0) {
-                       page = cl_page_list_first(qin);
-                       if (index != lov_page_index(page))
-                               break;
+               index = page->cp_lov_index;
+               cl_page_list_for_each_safe(page, tmp, qin) {
+                       /* this page is not on this stripe */
+                       if (index != page->cp_lov_index)
+                               continue;
 
                        cl_page_list_move(&cl2q->c2_qin, qin, page);
                }
@@ -1151,7 +1284,7 @@ static int lov_io_commit_async(const struct lu_env *env,
                               cl_commit_cbt cb)
 {
        struct cl_page_list *plist = &lov_env_info(env)->lti_plist;
-       struct lov_io     *lio = cl2lov_io(env, ios);
+       struct lov_io *lio = cl2lov_io(env, ios);
        struct lov_io_sub *sub;
        struct cl_page *page;
        int rc = 0;
@@ -1181,10 +1314,10 @@ static int lov_io_commit_async(const struct lu_env *env,
 
                cl_page_list_move(plist, queue, page);
 
-               index = lov_page_index(page);
+               index = page->cp_lov_index;
                while (queue->pl_nr > 0) {
                        page = cl_page_list_first(queue);
-                       if (index != lov_page_index(page))
+                       if (index != page->cp_lov_index)
                                break;
 
                        cl_page_list_move(plist, queue, page);
@@ -1230,7 +1363,7 @@ static int lov_io_fault_start(const struct lu_env *env,
 
        fio = &ios->cis_io->u.ci_fault;
        lio = cl2lov_io(env, ios);
-       sub = lov_sub_get(env, lio, lov_page_index(fio->ft_page));
+       sub = lov_sub_get(env, lio, fio->ft_page->cp_lov_index);
        sub->sub_io.u.ci_fault.ft_nob = fio->ft_nob;
 
        RETURN(lov_io_start(env, ios));
@@ -1256,26 +1389,121 @@ static void lov_io_fsync_end(const struct lu_env *env,
        RETURN_EXIT;
 }
 
+static void lov_io_lseek_end(const struct lu_env *env,
+                            const struct cl_io_slice *ios)
+{
+       struct lov_io *lio = cl2lov_io(env, ios);
+       struct cl_io *io = lio->lis_cl.cis_io;
+       struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
+       struct lov_io_sub *sub;
+       loff_t offset = -ENXIO;
+       __u64 hole_off = 0;
+       bool seek_hole = io->u.ci_lseek.ls_whence == SEEK_HOLE;
+
+       ENTRY;
+
+       list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
+               struct cl_io *subio = &sub->sub_io;
+               int index = lov_comp_entry(sub->sub_subio_index);
+               int stripe = lov_comp_stripe(sub->sub_subio_index);
+               loff_t sub_off, lov_off;
+               __u64 comp_end = lsm->lsm_entries[index]->lsme_extent.e_end;
+
+               lov_io_end_wrapper(sub->sub_env, subio);
+
+               if (io->ci_result == 0)
+                       io->ci_result = sub->sub_io.ci_result;
+
+               if (io->ci_result)
+                       continue;
+
+               CDEBUG(D_INFO, DFID": entry %x stripe %u: SEEK_%s from %lld\n",
+                      PFID(lu_object_fid(lov2lu(lio->lis_object))),
+                      index, stripe, seek_hole ? "HOLE" : "DATA",
+                      subio->u.ci_lseek.ls_start);
+
+               /* first subio with positive result is what we need */
+               sub_off = subio->u.ci_lseek.ls_result;
+               /* Expected error, offset is out of stripe file size */
+               if (sub_off == -ENXIO)
+                       continue;
+               /* Any other errors are not expected with ci_result == 0 */
+               if (sub_off < 0) {
+                       CDEBUG(D_INFO, "unexpected error: rc = %lld\n",
+                              sub_off);
+                       io->ci_result = sub_off;
+                       continue;
+               }
+               lov_off = lov_stripe_size(lsm, index, sub_off + 1, stripe) - 1;
+               if (lov_off < 0) {
+                       /* the only way to get negatove lov_off here is too big
+                        * result. Return -EOVERFLOW then.
+                        */
+                       io->ci_result = -EOVERFLOW;
+                       CDEBUG(D_INFO, "offset %llu is too big: rc = %d\n",
+                              (u64)lov_off, io->ci_result);
+                       continue;
+               }
+               if (lov_off < io->u.ci_lseek.ls_start) {
+                       io->ci_result = -EINVAL;
+                       CDEBUG(D_INFO, "offset %lld < start %lld: rc = %d\n",
+                              sub_off, io->u.ci_lseek.ls_start, io->ci_result);
+                       continue;
+               }
+               /* resulting offset can be out of component range if stripe
+                * object is full and its file size was returned as virtual
+                * hole start. Skip this result, the next component will give
+                * us correct lseek result but keep possible hole offset in
+                * case there is no more components ahead
+                */
+               if (lov_off >= comp_end) {
+                       /* must be SEEK_HOLE case */
+                       if (likely(seek_hole)) {
+                               /* save comp end as potential hole offset */
+                               hole_off = max_t(__u64, comp_end, hole_off);
+                       } else {
+                               io->ci_result = -EINVAL;
+                               CDEBUG(D_INFO,
+                                      "off %lld >= comp_end %llu: rc = %d\n",
+                                      lov_off, comp_end, io->ci_result);
+                       }
+                       continue;
+               }
+
+               CDEBUG(D_INFO, "SEEK_%s: %lld->%lld/%lld: rc = %d\n",
+                      seek_hole ? "HOLE" : "DATA",
+                      subio->u.ci_lseek.ls_start, sub_off, lov_off,
+                      sub->sub_io.ci_result);
+               offset = min_t(__u64, offset, lov_off);
+       }
+       /* no result but some component returns hole as component end */
+       if (seek_hole && offset == -ENXIO && hole_off > 0)
+               offset = hole_off;
+
+       io->u.ci_lseek.ls_result = offset;
+       RETURN_EXIT;
+}
+
 static const struct cl_io_operations lov_io_ops = {
-        .op = {
-                [CIT_READ] = {
-                        .cio_fini      = lov_io_fini,
-                        .cio_iter_init = lov_io_rw_iter_init,
-                        .cio_iter_fini = lov_io_iter_fini,
-                        .cio_lock      = lov_io_lock,
-                        .cio_unlock    = lov_io_unlock,
-                        .cio_start     = lov_io_start,
-                        .cio_end       = lov_io_end
-                },
-                [CIT_WRITE] = {
-                        .cio_fini      = lov_io_fini,
-                        .cio_iter_init = lov_io_rw_iter_init,
-                        .cio_iter_fini = lov_io_iter_fini,
-                        .cio_lock      = lov_io_lock,
-                        .cio_unlock    = lov_io_unlock,
-                        .cio_start     = lov_io_start,
-                        .cio_end       = lov_io_end
-                },
+       .op = {
+               [CIT_READ] = {
+                       .cio_fini      = lov_io_fini,
+                       .cio_iter_init = lov_io_rw_iter_init,
+                       .cio_iter_fini = lov_io_iter_fini,
+                       .cio_lock      = lov_io_lock,
+                       .cio_unlock    = lov_io_unlock,
+                       .cio_start     = lov_io_start,
+                       .cio_end       = lov_io_end
+               },
+               [CIT_WRITE] = {
+                       .cio_fini      = lov_io_fini,
+                       .cio_iter_init = lov_io_rw_iter_init,
+                       .cio_iter_fini = lov_io_iter_fini,
+                       .cio_lock      = lov_io_lock,
+                       .cio_unlock    = lov_io_unlock,
+                       .cio_start     = lov_io_start,
+                       .cio_end       = lov_io_end
+               },
                [CIT_SETATTR] = {
                        .cio_fini      = lov_io_fini,
                        .cio_iter_init = lov_io_setattr_iter_init,
@@ -1286,23 +1514,23 @@ static const struct cl_io_operations lov_io_ops = {
                        .cio_end       = lov_io_end
                },
                [CIT_DATA_VERSION] = {
-                       .cio_fini       = lov_io_fini,
-                       .cio_iter_init  = lov_io_iter_init,
-                       .cio_iter_fini  = lov_io_iter_fini,
-                       .cio_lock       = lov_io_lock,
-                       .cio_unlock     = lov_io_unlock,
-                       .cio_start      = lov_io_start,
-                       .cio_end        = lov_io_data_version_end,
+                       .cio_fini       = lov_io_fini,
+                       .cio_iter_init  = lov_io_iter_init,
+                       .cio_iter_fini  = lov_io_iter_fini,
+                       .cio_lock       = lov_io_lock,
+                       .cio_unlock     = lov_io_unlock,
+                       .cio_start      = lov_io_start,
+                       .cio_end        = lov_io_data_version_end,
+               },
+               [CIT_FAULT] = {
+                       .cio_fini      = lov_io_fini,
+                       .cio_iter_init = lov_io_iter_init,
+                       .cio_iter_fini = lov_io_iter_fini,
+                       .cio_lock      = lov_io_lock,
+                       .cio_unlock    = lov_io_unlock,
+                       .cio_start     = lov_io_fault_start,
+                       .cio_end       = lov_io_end
                },
-                [CIT_FAULT] = {
-                        .cio_fini      = lov_io_fini,
-                        .cio_iter_init = lov_io_iter_init,
-                        .cio_iter_fini = lov_io_iter_fini,
-                        .cio_lock      = lov_io_lock,
-                        .cio_unlock    = lov_io_unlock,
-                        .cio_start     = lov_io_fault_start,
-                        .cio_end       = lov_io_end
-                },
                [CIT_FSYNC] = {
                        .cio_fini      = lov_io_fini,
                        .cio_iter_init = lov_io_iter_init,
@@ -1321,6 +1549,15 @@ static const struct cl_io_operations lov_io_ops = {
                        .cio_start     = lov_io_start,
                        .cio_end       = lov_io_end
                },
+               [CIT_LSEEK] = {
+                       .cio_fini      = lov_io_fini,
+                       .cio_iter_init = lov_io_iter_init,
+                       .cio_iter_fini = lov_io_iter_fini,
+                       .cio_lock      = lov_io_lock,
+                       .cio_unlock    = lov_io_unlock,
+                       .cio_start     = lov_io_start,
+                       .cio_end       = lov_io_lseek_end
+               },
                [CIT_GLIMPSE] = {
                        .cio_fini      = lov_io_fini,
                },
@@ -1328,7 +1565,7 @@ static const struct cl_io_operations lov_io_ops = {
                        .cio_fini      = lov_io_fini
                }
        },
-       .cio_read_ahead                = lov_io_read_ahead,
+       .cio_read_ahead                = lov_io_read_ahead,
        .cio_submit                    = lov_io_submit,
        .cio_commit_async              = lov_io_commit_async,
 };
@@ -1360,7 +1597,7 @@ static int lov_empty_io_submit(const struct lu_env *env,
 static void lov_empty_impossible(const struct lu_env *env,
                                  struct cl_io_slice *ios)
 {
-        LBUG();
+       LBUG();
 }
 
 #define LOV_EMPTY_IMPOSSIBLE ((void *)lov_empty_impossible)
@@ -1369,37 +1606,37 @@ static void lov_empty_impossible(const struct lu_env *env,
  * An io operation vector for files without stripes.
  */
 static const struct cl_io_operations lov_empty_io_ops = {
-        .op = {
-                [CIT_READ] = {
-                        .cio_fini       = lov_empty_io_fini,
+       .op = {
+               [CIT_READ] = {
+                       .cio_fini       = lov_empty_io_fini,
 #if 0
-                        .cio_iter_init  = LOV_EMPTY_IMPOSSIBLE,
-                        .cio_lock       = LOV_EMPTY_IMPOSSIBLE,
-                        .cio_start      = LOV_EMPTY_IMPOSSIBLE,
-                        .cio_end        = LOV_EMPTY_IMPOSSIBLE
+                       .cio_iter_init  = LOV_EMPTY_IMPOSSIBLE,
+                       .cio_lock       = LOV_EMPTY_IMPOSSIBLE,
+                       .cio_start      = LOV_EMPTY_IMPOSSIBLE,
+                       .cio_end        = LOV_EMPTY_IMPOSSIBLE
 #endif
-                },
-                [CIT_WRITE] = {
-                        .cio_fini      = lov_empty_io_fini,
-                        .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
-                        .cio_lock      = LOV_EMPTY_IMPOSSIBLE,
-                        .cio_start     = LOV_EMPTY_IMPOSSIBLE,
-                        .cio_end       = LOV_EMPTY_IMPOSSIBLE
-                },
-                [CIT_SETATTR] = {
-                        .cio_fini      = lov_empty_io_fini,
-                        .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
-                        .cio_lock      = LOV_EMPTY_IMPOSSIBLE,
-                        .cio_start     = LOV_EMPTY_IMPOSSIBLE,
-                        .cio_end       = LOV_EMPTY_IMPOSSIBLE
-                },
-                [CIT_FAULT] = {
-                        .cio_fini      = lov_empty_io_fini,
-                        .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
-                        .cio_lock      = LOV_EMPTY_IMPOSSIBLE,
-                        .cio_start     = LOV_EMPTY_IMPOSSIBLE,
-                        .cio_end       = LOV_EMPTY_IMPOSSIBLE
-                },
+               },
+               [CIT_WRITE] = {
+                       .cio_fini      = lov_empty_io_fini,
+                       .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
+                       .cio_lock      = LOV_EMPTY_IMPOSSIBLE,
+                       .cio_start     = LOV_EMPTY_IMPOSSIBLE,
+                       .cio_end       = LOV_EMPTY_IMPOSSIBLE
+               },
+               [CIT_SETATTR] = {
+                       .cio_fini      = lov_empty_io_fini,
+                       .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
+                       .cio_lock      = LOV_EMPTY_IMPOSSIBLE,
+                       .cio_start     = LOV_EMPTY_IMPOSSIBLE,
+                       .cio_end       = LOV_EMPTY_IMPOSSIBLE
+               },
+               [CIT_FAULT] = {
+                       .cio_fini      = lov_empty_io_fini,
+                       .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
+                       .cio_lock      = LOV_EMPTY_IMPOSSIBLE,
+                       .cio_start     = LOV_EMPTY_IMPOSSIBLE,
+                       .cio_end       = LOV_EMPTY_IMPOSSIBLE
+               },
                [CIT_FSYNC] = {
                        .cio_fini      = lov_empty_io_fini
                },
@@ -1420,9 +1657,10 @@ static const struct cl_io_operations lov_empty_io_ops = {
 int lov_io_init_composite(const struct lu_env *env, struct cl_object *obj,
                          struct cl_io *io)
 {
-       struct lov_io       *lio = lov_env_io(env);
-       struct lov_object   *lov = cl2lov(obj);
+       struct lov_io *lio = lov_env_io(env);
+       struct lov_object *lov = cl2lov(obj);
        int result;
+
        ENTRY;
 
        INIT_LIST_HEAD(&lio->lis_active);
@@ -1460,6 +1698,7 @@ int lov_io_init_empty(const struct lu_env *env, struct cl_object *obj,
                break;
        case CIT_FSYNC:
        case CIT_LADVISE:
+       case CIT_LSEEK:
        case CIT_SETATTR:
        case CIT_DATA_VERSION:
                result = +1;
@@ -1506,12 +1745,16 @@ int lov_io_init_released(const struct lu_env *env, struct cl_object *obj,
                result = 1;
                break;
        case CIT_SETATTR:
-               /* the truncate to 0 is managed by MDT:
+               /*
+                * the truncate to 0 is managed by MDT:
                 * - in open, for open O_TRUNC
                 * - in setattr, for truncate
                 */
-               /* the truncate is for size > 0 so triggers a restore */
-               if (cl_io_is_trunc(io)) {
+               /*
+                * the truncate is for size > 0 so triggers a restore,
+                * also trigger a restore for prealloc/punch
+                */
+               if (cl_io_is_trunc(io) || cl_io_is_fallocate(io)) {
                        io->ci_restore_needed = 1;
                        result = -ENODATA;
                } else
@@ -1520,6 +1763,7 @@ int lov_io_init_released(const struct lu_env *env, struct cl_object *obj,
        case CIT_READ:
        case CIT_WRITE:
        case CIT_FAULT:
+       case CIT_LSEEK:
                io->ci_restore_needed = 1;
                result = -ENODATA;
                break;