Whamcloud - gitweb
LU-6179 llite: Implement ladvise lockahead
[fs/lustre-release.git] / lustre / lov / lov_io.c
index b8c529f..577e7d1 100644 (file)
@@ -102,7 +102,8 @@ static int lov_io_sub_init(const struct lu_env *env, struct lov_io *lio,
        LASSERT(sub->sub_env == NULL);
        ENTRY;
 
-       if (unlikely(lov_r0(lov, index)->lo_sub[stripe] == NULL))
+       if (unlikely(!lov_r0(lov, index)->lo_sub ||
+                    !lov_r0(lov, index)->lo_sub[stripe]))
                RETURN(-EIO);
 
        /* obtain new environment */
@@ -121,6 +122,8 @@ 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_lock_no_expand = io->ci_lock_no_expand;
 
        result = cl_io_sub_init(sub->sub_env, sub_io, io->ci_type, sub_obj);
 
@@ -207,14 +210,14 @@ static int lov_io_slice_init(struct lov_io *lio,
 
        LASSERT(obj->lo_lsm != NULL);
 
-        switch (io->ci_type) {
-        case CIT_READ:
-        case CIT_WRITE:
-                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);
+       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_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. */
@@ -222,10 +225,10 @@ static int lov_io_slice_init(struct lov_io *lio,
                                     LOV_PATTERN_F_HOLE))
                                RETURN(-EIO);
 
-                        lio->lis_pos = 0;
-                        lio->lis_endpos = OBD_OBJECT_EOF;
-                }
-                break;
+                       lio->lis_pos = 0;
+                       lio->lis_endpos = OBD_OBJECT_EOF;
+               }
+               break;
 
         case CIT_SETATTR:
                 if (cl_io_is_trunc(io))
@@ -308,6 +311,7 @@ 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;
@@ -324,6 +328,8 @@ static void lov_io_sub_inherit(struct lov_io_sub *sub, struct lov_io *lio,
                                                      stripe);
                        io->u.ci_setattr.sa_attr.lvb_size = new_size;
                }
+               lov_lsm2layout(lsm, lsm->lsm_entries[index],
+                              &io->u.ci_setattr.sa_layout);
                break;
        }
        case CIT_DATA_VERSION: {
@@ -350,12 +356,16 @@ static void lov_io_sub_inherit(struct lov_io_sub *sub, struct lov_io *lio,
        }
        case CIT_READ:
        case CIT_WRITE: {
-               io->u.ci_wr.wr_sync = cl_io_is_sync_write(parent);
+               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;
                if (cl_io_is_append(parent)) {
-                       io->u.ci_wr.wr_append = 1;
+                       io->u.ci_rw.rw_append = 1;
                } else {
-                       io->u.ci_rw.crw_pos = start;
-                       io->u.ci_rw.crw_count = end - start;
+                       io->u.ci_rw.rw_range.cir_pos = start;
+                       io->u.ci_rw.rw_range.cir_count = end - start;
                }
                break;
        }
@@ -382,6 +392,7 @@ static loff_t lov_offset_mod(loff_t val, int delta)
 static int lov_io_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 lov_stripe_md *lsm = lio->lis_object->lo_lsm;
        struct lov_io_sub    *sub;
@@ -406,6 +417,24 @@ static int lov_io_iter_init(const struct lu_env *env,
                if (!lu_extent_is_overlapped(&ext, &le->lle_extent))
                        continue;
 
+               CDEBUG(D_VFSTRACE, "component[%d] flags %#x\n",
+                      index - 1, lsm->lsm_entries[index - 1]->lsme_flags);
+               if (!lsm_entry_inited(lsm, index - 1)) {
+                       /* truncate IO will trigger write intent as well, and
+                        * it's handled in lov_io_setattr_iter_init() */
+                       if (io->ci_type == CIT_WRITE || cl_io_is_mkwrite(io)) {
+                               io->ci_need_write_intent = 1;
+                               /* execute it in main thread */
+                               io->ci_pio = 0;
+                               rc = -ENODATA;
+                               break;
+                       }
+
+                       /* Read from uninitialized components should return
+                        * zero filled pages. */
+                       continue;
+               }
+
                for (stripe = 0; stripe < r0->lo_nr; stripe++) {
                        if (!lov_stripe_intersects(lsm, index - 1, stripe,
                                                   &ext, &start, &end))
@@ -435,8 +464,9 @@ static int lov_io_iter_init(const struct lu_env *env,
                        if (rc != 0)
                                break;
 
-                       CDEBUG(D_VFSTRACE, "shrink: %d [%llu, %llu)\n",
-                              stripe, start, end);
+                       CDEBUG(D_VFSTRACE,
+                               "shrink stripe: {%d, %d} range: [%llu, %llu)\n",
+                               index, stripe, start, end);
 
                        list_add_tail(&sub->sub_linkage, &lio->lis_active);
                }
@@ -449,12 +479,13 @@ 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 lov_io        *lio = cl2lov_io(env, ios);
-       struct cl_io         *io  = ios->cis_io;
+       struct cl_io *io = ios->cis_io;
+       struct lov_io *lio = cl2lov_io(env, ios);
+       struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
        struct lov_stripe_md_entry *lse;
-       loff_t start = io->u.ci_rw.crw_pos;
+       struct cl_io_range *range = &io->u.ci_rw.rw_range;
+       loff_t start = range->cir_pos;
        loff_t next;
-       unsigned long ssize;
        int index;
 
        LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
@@ -463,12 +494,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_lsm_entry(lio->lis_object->lo_lsm, io->u.ci_rw.crw_pos);
+       index = lov_lsm_entry(lsm, range->cir_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 */
                        io->ci_continue = 0;
+                       /* execute it in main thread */
+                       io->ci_pio = 0;
 
                        RETURN(lov_io_iter_init(env, ios));
                }
@@ -478,24 +511,47 @@ static int lov_io_rw_iter_init(const struct lu_env *env,
 
        lse = lov_lse(lio->lis_object, index);
 
-       ssize = lse->lsme_stripe_size;
-       lov_do_div64(start, ssize);
-       next = (start + 1) * ssize;
-       if (next <= start * ssize)
-               next = ~0ull;
+       next = MAX_LFS_FILESIZE;
+       if (lse->lsme_stripe_count > 1) {
+               unsigned long ssize = lse->lsme_stripe_size;
+
+               lov_do_div64(start, ssize);
+               next = (start + 1) * ssize;
+               if (next <= start * ssize)
+                       next = MAX_LFS_FILESIZE;
+       }
 
-       LASSERT(io->u.ci_rw.crw_pos >= lse->lsme_extent.e_start);
+       LASSERTF(range->cir_pos >= lse->lsme_extent.e_start,
+                "pos %lld, [%lld, %lld)\n", range->cir_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;
-       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;
+       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;
        CDEBUG(D_VFSTRACE,
-              "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);
+              "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) {
+               /* it only splits IO here for parallel IO,
+                * there will be no actual IO going to occur,
+                * so it doesn't need to invoke lov_io_iter_init()
+                * to initialize sub IOs. */
+               if (!lsm_entry_inited(lsm, index)) {
+                       io->ci_need_write_intent = 1;
+                       RETURN(-ENODATA);
+               }
+               RETURN(0);
+       }
 
        /*
         * XXX The following call should be optimized: we know, that
@@ -504,6 +560,26 @@ static int lov_io_rw_iter_init(const struct lu_env *env,
        RETURN(lov_io_iter_init(env, ios));
 }
 
+static int lov_io_setattr_iter_init(const struct lu_env *env,
+                                   const struct cl_io_slice *ios)
+{
+       struct lov_io *lio = cl2lov_io(env, ios);
+       struct cl_io *io = ios->cis_io;
+       struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
+       int index;
+       ENTRY;
+
+       if (cl_io_is_trunc(io) && lio->lis_pos > 0) {
+               index = lov_lsm_entry(lsm, lio->lis_pos - 1);
+               if (index > 0 && !lsm_entry_inited(lsm, index)) {
+                       io->ci_need_write_intent = 1;
+                       RETURN(io->ci_result = -ENODATA);
+               }
+       }
+
+       RETURN(lov_io_iter_init(env, ios));
+}
+
 static int lov_io_call(const struct lu_env *env, struct lov_io *lio,
                       int (*iofunc)(const struct lu_env *, struct cl_io *))
 {
@@ -636,7 +712,7 @@ static int lov_io_read_ahead(const struct lu_env *env,
 
        offset = cl_offset(obj, start);
        index = lov_lsm_entry(loo->lo_lsm, offset);
-       if (index < 0)
+       if (index < 0 || !lsm_entry_inited(loo->lo_lsm, index))
                RETURN(-ENODATA);
 
        stripe = lov_stripe_number(loo->lo_lsm, index, offset);
@@ -660,28 +736,36 @@ static int lov_io_read_ahead(const struct lu_env *env,
                RETURN(rc);
 
        /**
-        * Adjust the stripe index by layout of raid0. ra->cra_end is the
+        * Adjust the stripe index by layout of comp. ra->cra_end is the
         * maximum page index covered by an underlying DLM lock.
         * This function converts cra_end from stripe level to file level, and
-        * make sure it's not beyond stripe boundary.
+        * make sure it's not beyond stripe and component boundary.
         */
-       if (r0->lo_nr == 1) /* single stripe file */
-               RETURN(0);
 
        /* cra_end is stripe level, convert it into file level */
        ra_end = ra->cra_end;
        if (ra_end != CL_PAGE_EOF)
-               ra_end = lov_stripe_pgoff(loo->lo_lsm, index, ra_end, stripe);
+               ra->cra_end = lov_stripe_pgoff(loo->lo_lsm, index,
+                                              ra_end, stripe);
+
+       /* boundary of current component */
+       ra_end = cl_index(obj, (loff_t)lov_lse(loo, index)->lsme_extent.e_end);
+       if (ra_end != CL_PAGE_EOF && ra->cra_end >= ra_end)
+               ra->cra_end = 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, "
               "stripe_size = %u, stripe no = %u, start index = %lu\n",
-              PFID(lu_object_fid(lov2lu(loo))), ra_end, pps, index,
+              PFID(lu_object_fid(lov2lu(loo))), ra->cra_end, 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_end, start + pps - start % pps - 1);
+       ra->cra_end = min_t(pgoff_t,
+                           ra->cra_end, start + pps - start % pps - 1);
        RETURN(0);
 }
 
@@ -891,15 +975,15 @@ static const struct cl_io_operations lov_io_ops = {
                         .cio_start     = lov_io_start,
                         .cio_end       = lov_io_end
                 },
-                [CIT_SETATTR] = {
-                        .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_end
-                },
+               [CIT_SETATTR] = {
+                       .cio_fini      = lov_io_fini,
+                       .cio_iter_init = lov_io_setattr_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_DATA_VERSION] = {
                        .cio_fini       = lov_io_fini,
                        .cio_iter_init  = lov_io_iter_init,