Whamcloud - gitweb
LU-14128 lov: correctly set OST obj size
[fs/lustre-release.git] / lustre / lov / lov_io.c
index ce87872..b9592d1 100644 (file)
@@ -478,8 +478,6 @@ 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:
@@ -540,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;
@@ -559,12 +563,22 @@ static int lov_io_slice_init(struct lov_io *lio,
                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);
+
+       LASSERT(obj->lo_lsm != NULL);
+
        result = lov_io_mirror_init(lio, obj, io);
        if (result)
                GOTO(out, result);
 
        /* 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);
 
@@ -729,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:
@@ -743,6 +763,23 @@ static loff_t lov_offset_mod(loff_t 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)
 {
@@ -764,6 +801,9 @@ 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);
@@ -795,28 +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);
 
-                       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);
+                       /**
+                        * 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;
+                       }
+
+                       (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: %d [%llu, %llu)\n",
-                              stripe, start, end);
-
-                       list_add_tail(&sub->sub_linkage, &lio->lis_active);
                }
-               if (rc != 0)
-                       break;
        }
        RETURN(rc);
 }
@@ -1298,6 +1389,101 @@ 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] = {
@@ -1363,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,
                },
@@ -1503,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;
@@ -1567,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;