/**
  *  This function send a restore request to the MDT
  */
-int ll_layout_restore(struct inode *inode)
+int ll_layout_restore(struct inode *inode, loff_t offset, __u64 length)
 {
        struct hsm_user_request *hur;
        int                      len, rc;
        hur->hur_request.hr_flags = 0;
        memcpy(&hur->hur_user_item[0].hui_fid, &ll_i2info(inode)->lli_fid,
               sizeof(hur->hur_user_item[0].hui_fid));
-       hur->hur_user_item[0].hui_extent.length = -1;
+       hur->hur_user_item[0].hui_extent.offset = offset;
+       hur->hur_user_item[0].hui_extent.length = length;
        hur->hur_request.hr_itemcount = 1;
        rc = obd_iocontrol(LL_IOC_HSM_REQUEST, ll_i2sbi(inode)->ll_md_exp,
                           len, hur, NULL);
 
                       LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
                       (s64)ktime_get_real_seconds());
 
-       /* If we are changing file size, file content is modified, flag it. */
-       if (attr->ia_valid & ATTR_SIZE) {
-               attr->ia_valid |= MDS_OPEN_OWNEROVERRIDE;
-               spin_lock(&lli->lli_lock);
-               lli->lli_flags |= LLIF_DATA_MODIFIED;
-               spin_unlock(&lli->lli_lock);
-       }
-
        /* We always do an MDS RPC, even if we're only changing the size;
         * only the MDS knows whether truncate() should fail with -ETXTBUSY
         */
        if (!S_ISDIR(inode->i_mode))
                inode_unlock(inode);
 
-       memcpy(&op_data->op_attr, attr, sizeof(*attr));
-
-       /* Open epoch for truncate. */
-       if (exp_connect_som(ll_i2mdexp(inode)) &&
-           (attr->ia_valid & (ATTR_SIZE | ATTR_MTIME | ATTR_MTIME_SET)))
-               op_data->op_flags = MF_EPOCH_OPEN;
-
        /* truncate on a released file must failed with -ENODATA,
         * so size must not be set on MDS for released file
         * but other attributes must be set
                if (lsm && lsm->lsm_pattern & LOV_PATTERN_F_RELEASED)
                        file_is_released = true;
                ccc_inode_lsm_put(inode, lsm);
+
+               if (!hsm_import && attr->ia_valid & ATTR_SIZE) {
+                       if (file_is_released) {
+                               rc = ll_layout_restore(inode, 0, attr->ia_size);
+                               if (rc < 0)
+                                       goto out;
+
+                               file_is_released = false;
+                               ll_layout_refresh(inode, &gen);
+                       }
+
+                       /*
+                        * If we are changing file size, file content is
+                        * modified, flag it.
+                        */
+                       attr->ia_valid |= MDS_OPEN_OWNEROVERRIDE;
+                       spin_lock(&lli->lli_lock);
+                       lli->lli_flags |= LLIF_DATA_MODIFIED;
+                       spin_unlock(&lli->lli_lock);
+                       op_data->op_bias |= MDS_DATA_MODIFIED;
+               }
        }
 
-       /* if not in HSM import mode, clear size attr for released file
-        * we clear the attribute send to MDT in op_data, not the original
-        * received from caller in attr which is used later to
-        * decide return code
-        */
-       if (file_is_released && (attr->ia_valid & ATTR_SIZE) && !hsm_import)
-               op_data->op_attr.ia_valid &= ~ATTR_SIZE;
+       memcpy(&op_data->op_attr, attr, sizeof(*attr));
+
+       /* Open epoch for truncate. */
+       if (exp_connect_som(ll_i2mdexp(inode)) && !hsm_import &&
+           (attr->ia_valid & (ATTR_SIZE | ATTR_MTIME | ATTR_MTIME_SET)))
+               op_data->op_flags = MF_EPOCH_OPEN;
 
        rc = ll_md_setattr(dentry, op_data, &mod);
        if (rc)
                goto out;
 
-       /* truncate failed (only when non HSM import), others succeed */
-       if (file_is_released) {
-               if ((attr->ia_valid & ATTR_SIZE) && !hsm_import)
-                       rc = -ENODATA;
-               else
-                       rc = 0;
-               goto out;
-       }
-
        /* RPC to MDT is sent, cancel data modification flag */
        if (op_data->op_bias & MDS_DATA_MODIFIED) {
                spin_lock(&lli->lli_lock);
        }
 
        ll_ioepoch_open(lli, op_data->op_ioepoch);
-       if (!S_ISREG(inode->i_mode)) {
+       if (!S_ISREG(inode->i_mode) || file_is_released) {
                rc = 0;
                goto out;
        }
 
        struct cl_io     *io  = ios->cis_io;
        struct cl_object *obj = io->ci_obj;
        struct vvp_io    *vio = cl2vvp_io(env, ios);
+       struct inode *inode = vvp_object_inode(obj);
 
        CLOBINVRNT(env, obj, vvp_object_invariant(obj));
 
                /* file was detected release, we need to restore it
                 * before finishing the io
                 */
-               rc = ll_layout_restore(vvp_object_inode(obj));
+               rc = ll_layout_restore(inode, 0, OBD_OBJECT_EOF);
                /* if restore registration failed, no restart,
                 * we will return -ENODATA
                 */
                __u32 gen = 0;
 
                /* check layout version */
-               ll_layout_refresh(vvp_object_inode(obj), &gen);
+               ll_layout_refresh(inode, &gen);
                io->ci_need_restart = vio->vui_layout_gen != gen;
                if (io->ci_need_restart) {
                        CDEBUG(D_VFSTRACE,