Whamcloud - gitweb
LU-8210 osd-ldiskfs: fix setting pages PageUptodate state
[fs/lustre-release.git] / lustre / osd-ldiskfs / osd_io.c
index 3657495..aaea2f0 100644 (file)
@@ -27,7 +27,7 @@
  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2012, 2014, Intel Corporation.
+ * Copyright (c) 2012, 2015, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -179,10 +179,10 @@ static void dio_complete_routine(struct bio *bio, int error)
 
        /* the check is outside of the cycle for performance reason -bzzz */
        if (!test_bit(__REQ_WRITE, &bio->bi_rw)) {
-               bio_for_each_segment(bvl, bio, iter) {
+               bio_for_each_segment_all(bvl, bio, iter) {
                        if (likely(error == 0))
-                               SetPageUptodate(bvec_iter_page(&bvl, iter));
-                       LASSERT(PageLocked(bvec_iter_page(&bvl, iter)));
+                               SetPageUptodate(bvl_to_page(bvl));
+                       LASSERT(PageLocked(bvl_to_page(bvl)));
                }
                atomic_dec(&iobuf->dr_dev->od_r_in_flight);
        } else {
@@ -619,7 +619,7 @@ static int ldiskfs_ext_new_extent_cb(struct inode *inode,
 {
        struct bpointers *bp = cbdata;
        struct ldiskfs_extent nex;
-       unsigned long pblock;
+       unsigned long pblock = 0;
        unsigned long tgen;
        int err, i;
        unsigned long count;
@@ -745,11 +745,7 @@ map:
                                        i, cex->ec_len);
                for (; i < cex->ec_len && bp->num; i++) {
                        *(bp->blocks) = cex->ec_start + i;
-#ifdef LDISKFS_EXT_CACHE_EXTENT /* until kernel 2.6.37 */
-                       if (cex->ec_type != LDISKFS_EXT_CACHE_EXTENT) {
-#else
-                       if ((cex->ec_len == 0) || (cex->ec_start == 0)) {
-#endif
+                       if (pblock != 0) {
                                /* unmap any possible underlying metadata from
                                 * the block device mapping.  bug 6998. */
                                unmap_underlying_metadata(inode->i_sb->s_bdev,
@@ -1043,29 +1039,57 @@ static int osd_write_prep(const struct lu_env *env, struct dt_object *dt,
         RETURN(rc);
 }
 
-/* Check if a block is allocated or not */
-static int osd_is_mapped(struct inode *inode, u64 offset)
-{
-       sector_t (*fs_bmap)(struct address_space *, sector_t);
+struct osd_fextent {
+       sector_t        start;
+       sector_t        end;
+       unsigned int    mapped:1;
+};
 
-       fs_bmap = inode->i_mapping->a_ops->bmap;
+static int osd_is_mapped(struct dt_object *dt, __u64 offset,
+                        struct osd_fextent *cached_extent)
+{
+       struct inode *inode = osd_dt_obj(dt)->oo_inode;
+       sector_t block = offset >> inode->i_blkbits;
+       sector_t start;
+       struct fiemap_extent_info fei = { 0 };
+       struct fiemap_extent fe = { 0 };
+       mm_segment_t saved_fs;
+       int rc;
 
-       /* We can't know if we are overwriting or not */
-       if (unlikely(fs_bmap == NULL))
-               return 0;
+       if (block >= cached_extent->start && block < cached_extent->end)
+               return cached_extent->mapped;
 
        if (i_size_read(inode) == 0)
                return 0;
 
        /* Beyond EOF, must not be mapped */
-       if (((i_size_read(inode) - 1) >> inode->i_blkbits) <
-           (offset >> inode->i_blkbits))
+       if (((i_size_read(inode) - 1) >> inode->i_blkbits) < block)
                return 0;
 
-       if (fs_bmap(inode->i_mapping, offset >> inode->i_blkbits) == 0)
+       fei.fi_extents_max = 1;
+       fei.fi_extents_start = &fe;
+
+       saved_fs = get_fs();
+       set_fs(get_ds());
+       rc = inode->i_op->fiemap(inode, &fei, offset, FIEMAP_MAX_OFFSET-offset);
+       set_fs(saved_fs);
+       if (rc != 0)
                return 0;
 
-       return 1;
+       start = fe.fe_logical >> inode->i_blkbits;
+
+       if (start > block) {
+               cached_extent->start = block;
+               cached_extent->end = start;
+               cached_extent->mapped = 0;
+       } else {
+               cached_extent->start = start;
+               cached_extent->end = (fe.fe_logical + fe.fe_length) >>
+                                     inode->i_blkbits;
+               cached_extent->mapped = 1;
+       }
+
+       return cached_extent->mapped;
 }
 
 static int osd_declare_write_commit(const struct lu_env *env,
@@ -1082,8 +1106,10 @@ static int osd_declare_write_commit(const struct lu_env *env,
         int                      newblocks;
        int                      rc = 0;
        int                      flags = 0;
+       int                      credits = 0;
        bool                     ignore_quota = false;
        long long                quota_space = 0;
+       struct osd_fextent       extent = { 0 };
        ENTRY;
 
         LASSERT(handle != NULL);
@@ -1098,7 +1124,7 @@ static int osd_declare_write_commit(const struct lu_env *env,
                    lnb[i - 1].lnb_file_offset + lnb[i - 1].lnb_len)
                        extents++;
 
-               if (!osd_is_mapped(inode, lnb[i].lnb_file_offset))
+               if (!osd_is_mapped(dt, lnb[i].lnb_file_offset, &extent))
                        quota_space += PAGE_CACHE_SIZE;
 
                /* ignore quota for the whole request if any page is from
@@ -1129,14 +1155,14 @@ static int osd_declare_write_commit(const struct lu_env *env,
                 depth = ext_depth(inode);
                 depth = max(depth, 1) + 1;
                 newblocks += depth;
-                oh->ot_credits++; /* inode */
-                oh->ot_credits += depth * 2 * extents;
-        } else {
-                depth = 3;
-                newblocks += depth;
-                oh->ot_credits++; /* inode */
-                oh->ot_credits += depth * extents;
-        }
+               credits++; /* inode */
+               credits += depth * 2 * extents;
+       } else {
+               depth = 3;
+               newblocks += depth;
+               credits++; /* inode */
+               credits += depth * extents;
+       }
 
        /* quota space for metadata blocks */
        quota_space += depth * extents * LDISKFS_BLOCK_SIZE(osd_sb(osd));
@@ -1148,15 +1174,17 @@ static int osd_declare_write_commit(const struct lu_env *env,
 
         /* we can't dirty more bitmap blocks than exist */
         if (newblocks > LDISKFS_SB(osd_sb(osd))->s_groups_count)
-                oh->ot_credits += LDISKFS_SB(osd_sb(osd))->s_groups_count;
+               credits += LDISKFS_SB(osd_sb(osd))->s_groups_count;
         else
-                oh->ot_credits += newblocks;
+               credits += newblocks;
 
-        /* we can't dirty more gd blocks than exist */
-        if (newblocks > LDISKFS_SB(osd_sb(osd))->s_gdb_count)
-                oh->ot_credits += LDISKFS_SB(osd_sb(osd))->s_gdb_count;
-        else
-                oh->ot_credits += newblocks;
+       /* we can't dirty more gd blocks than exist */
+       if (newblocks > LDISKFS_SB(osd_sb(osd))->s_gdb_count)
+               credits += LDISKFS_SB(osd_sb(osd))->s_gdb_count;
+       else
+               credits += newblocks;
+
+       osd_trans_declare_op(env, oh, OSD_OT_WRITE, credits);
 
        /* make sure the over quota flags were not set */
        lnb[0].lnb_flags &= ~(OBD_BRW_OVER_USRQUOTA | OBD_BRW_OVER_GRPQUOTA);
@@ -1187,6 +1215,7 @@ static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
         struct osd_device  *osd = osd_obj2dev(osd_dt_obj(dt));
         loff_t isize;
         int rc = 0, i;
+       struct osd_fextent extent = { 0 };
 
         LASSERT(inode);
 
@@ -1199,7 +1228,7 @@ static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
 
         for (i = 0; i < npages; i++) {
                if (lnb[i].lnb_rc == -ENOSPC &&
-                   osd_is_mapped(inode, lnb[i].lnb_file_offset)) {
+                   osd_is_mapped(dt, lnb[i].lnb_file_offset, &extent)) {
                        /* Allow the write to proceed if overwriting an
                         * existing block */
                        lnb[i].lnb_rc = 0;
@@ -1232,6 +1261,8 @@ static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
                osd_iobuf_add_page(iobuf, lnb[i].lnb_page);
         }
 
+       osd_trans_exec_op(env, thandle, OSD_OT_WRITE);
+
         if (OBD_FAIL_CHECK(OBD_FAIL_OST_MAPBLK_ENOSPC)) {
                 rc = -ENOSPC;
         } else if (iobuf->dr_npages > 0) {
@@ -1257,9 +1288,11 @@ static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
                osd_fini_iobuf(osd, iobuf);
        }
 
-        if (unlikely(rc != 0)) {
-                /* if write fails, we should drop pages from the cache */
-                for (i = 0; i < npages; i++) {
+       osd_trans_exec_check(env, thandle, OSD_OT_WRITE);
+
+       if (unlikely(rc != 0)) {
+               /* if write fails, we should drop pages from the cache */
+               for (i = 0; i < npages; i++) {
                        if (lnb[i].lnb_page == NULL)
                                continue;
                        LASSERT(PageLocked(lnb[i].lnb_page));
@@ -1304,7 +1337,7 @@ static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
                         * lnb->lnb_rc == 0, so it's easy to detect later. */
                        break;
 
-               if (isize < lnb[i].lnb_file_offset + lnb[i].lnb_len - 1)
+               if (isize < lnb[i].lnb_file_offset + lnb[i].lnb_len)
                        lnb[i].lnb_rc = isize - lnb[i].lnb_file_offset;
                else
                        lnb[i].lnb_rc = lnb[i].lnb_len;
@@ -1374,7 +1407,7 @@ int osd_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs)
         int blocksize;
         int csize;
         int boffs;
-        int err;
+       int err = 0;
 
         /* prevent reading after eof */
        spin_lock(&inode->i_lock);
@@ -1455,11 +1488,9 @@ static inline int osd_extents_enabled(struct super_block *sb,
        return 0;
 }
 
-static inline int osd_calc_bkmap_credits(struct super_block *sb,
-                                        struct inode *inode,
-                                        const loff_t size,
-                                        const loff_t pos,
-                                        const int blocks)
+int osd_calc_bkmap_credits(struct super_block *sb, struct inode *inode,
+                          const loff_t size, const loff_t pos,
+                          const int blocks)
 {
        int credits, bits, bs, i;
 
@@ -1494,9 +1525,12 @@ static inline int osd_calc_bkmap_credits(struct super_block *sb,
        } else if (pos + size <= (LDISKFS_NDIR_BLOCKS + 1024) * bs) {
                /* single indirect */
                credits = blocks * 3;
-               /* probably indirect block has been allocated already */
-               if (!inode || LDISKFS_I(inode)->i_data[LDISKFS_IND_BLOCK])
+               if (inode == NULL ||
+                   LDISKFS_I(inode)->i_data[LDISKFS_IND_BLOCK] == 0)
                        credits += 3;
+               else
+                       /* The indirect block may be modified. */
+                       credits += 1;
        }
 
        return credits;
@@ -1634,6 +1668,7 @@ int osd_ldiskfs_write_record(struct inode *inode, void *buf, int bufsize,
                 size = min(blocksize - boffs, bufsize);
                 bh = ldiskfs_bread(handle, inode, block, 1, &err);
                 if (!bh) {
+                       err = err ? err : -EIO;
                         CERROR("%s: error reading offset %llu (block %lu): "
                                "rc = %d\n",
                                inode->i_sb->s_id, offset, block, err);
@@ -1704,6 +1739,8 @@ static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
 
         oh = container_of(handle, struct osd_thandle, ot_super);
         LASSERT(oh->ot_handle->h_transaction != NULL);
+       osd_trans_exec_op(env, handle, OSD_OT_WRITE);
+
        /* Write small symlink to inode body as we need to maintain correct
         * on-disk symlinks for ldiskfs.
         * Note: the buf->lb_buf contains a NUL terminator while buf->lb_len
@@ -1716,9 +1753,12 @@ static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
                result = osd_ldiskfs_write_record(inode, buf->lb_buf,
                                                  buf->lb_len, is_link, pos,
                                                  oh->ot_handle);
-        if (result == 0)
-                result = buf->lb_len;
-        return result;
+       if (result == 0)
+               result = buf->lb_len;
+
+       osd_trans_exec_check(env, handle, OSD_OT_WRITE);
+
+       return result;
 }
 
 static int osd_declare_punch(const struct lu_env *env, struct dt_object *dt,
@@ -1796,6 +1836,10 @@ static int osd_punch(const struct lu_env *env, struct dt_object *dt,
         LASSERT(h != NULL);
         LASSERT(h == oh->ot_handle);
 
+       /* do not check credits with osd_trans_exec_check() as the truncate
+        * can restart the transaction internally and we restart the
+        * transaction in this case */
+
         if (tid != h->h_transaction->t_tid) {
                 int credits = oh->ot_credits;
                 /*
@@ -1875,6 +1919,21 @@ static int osd_fiemap_get(const struct lu_env *env, struct dt_object *dt,
        return rc;
 }
 
+static int osd_ladvise(const struct lu_env *env, struct dt_object *dt,
+                      __u64 start, __u64 end, enum lu_ladvise_type advice)
+{
+       int     rc;
+       ENTRY;
+
+       switch (advice) {
+       default:
+               rc = -ENOTSUPP;
+               break;
+       }
+
+       RETURN(rc);
+}
+
 /*
  * in some cases we may need declare methods for objects being created
  * e.g., when we create symlink
@@ -1884,17 +1943,18 @@ const struct dt_body_operations osd_body_ops_new = {
 };
 
 const struct dt_body_operations osd_body_ops = {
-        .dbo_read                 = osd_read,
-        .dbo_declare_write        = osd_declare_write,
-        .dbo_write                = osd_write,
-        .dbo_bufs_get             = osd_bufs_get,
-        .dbo_bufs_put             = osd_bufs_put,
-        .dbo_write_prep           = osd_write_prep,
-        .dbo_declare_write_commit = osd_declare_write_commit,
-        .dbo_write_commit         = osd_write_commit,
-        .dbo_read_prep            = osd_read_prep,
-        .dbo_declare_punch         = osd_declare_punch,
-        .dbo_punch                 = osd_punch,
-        .dbo_fiemap_get           = osd_fiemap_get,
+       .dbo_read                       = osd_read,
+       .dbo_declare_write              = osd_declare_write,
+       .dbo_write                      = osd_write,
+       .dbo_bufs_get                   = osd_bufs_get,
+       .dbo_bufs_put                   = osd_bufs_put,
+       .dbo_write_prep                 = osd_write_prep,
+       .dbo_declare_write_commit       = osd_declare_write_commit,
+       .dbo_write_commit               = osd_write_commit,
+       .dbo_read_prep                  = osd_read_prep,
+       .dbo_declare_punch              = osd_declare_punch,
+       .dbo_punch                      = osd_punch,
+       .dbo_fiemap_get                 = osd_fiemap_get,
+       .dbo_ladvise                    = osd_ladvise,
 };