Whamcloud - gitweb
LU-12593 osd: zeroing a freshly allocated block buffer
[fs/lustre-release.git] / lustre / osd-ldiskfs / osd_io.c
index 134dcdb..3503e5a 100644 (file)
@@ -164,7 +164,6 @@ static void dio_complete_routine(struct bio *bio, int error)
 {
 #endif
        struct osd_iobuf *iobuf = bio->bi_private;
-       int iter;
        struct bio_vec *bvl;
 
         /* CAVEAT EMPTOR: possibly in IRQ context
@@ -191,7 +190,9 @@ static void dio_complete_routine(struct bio *bio, int error)
 
        /* the check is outside of the cycle for performance reason -bzzz */
        if (!bio_data_dir(bio)) {
-               bio_for_each_segment_all(bvl, bio, iter) {
+               DECLARE_BVEC_ITER_ALL(iter_all);
+
+               bio_for_each_segment_all(bvl, bio, iter_all) {
                        if (likely(error == 0))
                                SetPageUptodate(bvl_to_page(bvl));
                        LASSERT(PageLocked(bvl_to_page(bvl)));
@@ -271,6 +272,7 @@ static int can_be_merged(struct bio *bio, sector_t sector)
        return bio_end_sector(bio) == sector ? 1 : 0;
 }
 
+#if IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY)
 /*
  * This function will change the data written, thus it should only be
  * used when checking data integrity feature
@@ -278,11 +280,11 @@ static int can_be_merged(struct bio *bio, sector_t sector)
 static void bio_integrity_fault_inject(struct bio *bio)
 {
        struct bio_vec *bvec;
-       int i;
+       DECLARE_BVEC_ITER_ALL(iter_all);
        void *kaddr;
        char *addr;
 
-       bio_for_each_segment_all(bvec, bio, i) {
+       bio_for_each_segment_all(bvec, bio, iter_all) {
                struct page *page = bvec->bv_page;
 
                kaddr = kmap(page);
@@ -328,12 +330,13 @@ static int osd_bio_integrity_compare(struct bio *bio, struct block_device *bdev,
                bip->bip_vec->bv_offset;
        struct bio_vec *bv;
        sector_t sector = bio_start_sector(bio);
-       unsigned int i, sectors, total;
+       unsigned int sectors, total;
+       DECLARE_BVEC_ITER_ALL(iter_all);
        __u16 *expected_guard;
        int rc;
 
        total = 0;
-       bio_for_each_segment_all(bv, bio, i) {
+       bio_for_each_segment_all(bv, bio, iter_all) {
                lnb = iobuf->dr_lnbs[index];
                expected_guard = lnb->lnb_guards;
                sectors = bv->bv_len / sector_size;
@@ -406,7 +409,11 @@ static void dio_integrity_complete_routine(struct bio *bio, int error)
 
        OBD_FREE_PTR(bio_private);
 }
-#endif
+#endif /* HAVE_BIO_INTEGRITY_PREP_FN */
+#else  /* !CONFIG_BLK_DEV_INTEGRITY */
+#define osd_bio_integrity_handle(osd, bio, iobuf, start_page_idx, \
+                                fault_inject, integrity_enabled) 0
+#endif /* CONFIG_BLK_DEV_INTEGRITY */
 
 static int osd_bio_init(struct bio *bio, struct osd_iobuf *iobuf,
                        bool integrity_enabled, int start_page_idx,
@@ -463,7 +470,7 @@ static int osd_do_bio(struct osd_device *osd, struct inode *inode,
        int rc = 0;
        bool fault_inject;
        bool integrity_enabled;
-       DECLARE_PLUG(plug);
+       struct blk_plug plug;
        ENTRY;
 
        fault_inject = OBD_FAIL_CHECK(OBD_FAIL_OST_INTEGRITY_FAULT);
@@ -517,14 +524,13 @@ static int osd_do_bio(struct osd_device *osd, struct inode *inode,
                                unsigned int bi_size = bio_sectors(bio) << 9;
 
                                /* Dang! I have to fragment this I/O */
-                               CDEBUG(D_INODE, "bio++ sz %d vcnt %d(%d) "
-                                      "sectors %d(%d) psg %d(%d) hsg %d(%d)\n",
+                               CDEBUG(D_INODE,
+                                      "bio++ sz %d vcnt %d(%d) sectors %d(%d) psg %d(%d)\n",
                                       bi_size, bio->bi_vcnt, bio->bi_max_vecs,
                                       bio_sectors(bio),
                                       queue_max_sectors(q),
-                                       bio_phys_segments(q, bio),
-                                       queue_max_phys_segments(q),
-                                      0, queue_max_hw_segments(q));
+                                      osd_bio_nr_segs(bio),
+                                      queue_max_segments(q));
                                rc = osd_bio_integrity_handle(osd, bio,
                                        iobuf, bio_start_page_idx,
                                        fault_inject, integrity_enabled);
@@ -605,8 +611,9 @@ out:
 }
 
 static int osd_map_remote_to_local(loff_t offset, ssize_t len, int *nrpages,
-                                   struct niobuf_local *lnb)
+                                  struct niobuf_local *lnb, int maxlnb)
 {
+       int rc = 0;
         ENTRY;
 
         *nrpages = 0;
@@ -615,6 +622,11 @@ static int osd_map_remote_to_local(loff_t offset, ssize_t len, int *nrpages,
                int poff = offset & (PAGE_SIZE - 1);
                int plen = PAGE_SIZE - poff;
 
+               if (*nrpages >= maxlnb) {
+                       rc = -EOVERFLOW;
+                       break;
+               }
+
                 if (plen > len)
                         plen = len;
                lnb->lnb_file_offset = offset;
@@ -626,6 +638,7 @@ static int osd_map_remote_to_local(loff_t offset, ssize_t len, int *nrpages,
                lnb->lnb_rc = 0;
                lnb->lnb_guard_rpc = 0;
                lnb->lnb_guard_disk = 0;
+               lnb->lnb_locked = 0;
 
                 LASSERTF(plen <= len, "plen %u, len %lld\n", plen,
                          (long long) len);
@@ -635,7 +648,7 @@ static int osd_map_remote_to_local(loff_t offset, ssize_t len, int *nrpages,
                 (*nrpages)++;
         }
 
-        RETURN(0);
+       RETURN(rc);
 }
 
 static struct page *osd_get_page(const struct lu_env *env, struct dt_object *dt,
@@ -733,17 +746,18 @@ static int osd_bufs_put(const struct lu_env *env, struct dt_object *dt,
 
                if (page == NULL)
                        continue;
-               LASSERT(PageLocked(page));
 
                /* if the page isn't cached, then reset uptodate
                 * to prevent reuse */
                if (test_bit(PG_private_2, &page->flags)) {
                        clear_bit(PG_private_2, &page->flags);
                        ClearPageUptodate(page);
-                       unlock_page(page);
+                       if (lnb[i].lnb_locked)
+                               unlock_page(page);
                        oti->oti_dio_pages_used--;
                } else {
-                       unlock_page(page);
+                       if (lnb[i].lnb_locked)
+                               unlock_page(page);
                        if (pagevec_add(&pvec, page) == 0)
                                pagevec_release(&pvec);
                }
@@ -786,7 +800,7 @@ static int osd_bufs_put(const struct lu_env *env, struct dt_object *dt,
  */
 static int osd_bufs_get(const struct lu_env *env, struct dt_object *dt,
                        loff_t pos, ssize_t len, struct niobuf_local *lnb,
-                       enum dt_bufs_type rw)
+                       int maxlnb, enum dt_bufs_type rw)
 {
        struct osd_thread_info *oti = osd_oti_get(env);
        struct osd_object *obj = osd_dt_obj(dt);
@@ -804,7 +818,9 @@ static int osd_bufs_get(const struct lu_env *env, struct dt_object *dt,
                }
        }
 
-       osd_map_remote_to_local(pos, len, &npages, lnb);
+       rc = osd_map_remote_to_local(pos, len, &npages, lnb, maxlnb);
+       if (rc)
+               RETURN(rc);
 
        /* this could also try less hard for DT_BUFS_TYPE_READAHEAD pages */
        gfp_mask = rw & DT_BUFS_TYPE_LOCAL ? (GFP_NOFS | __GFP_HIGHMEM) :
@@ -815,6 +831,7 @@ static int osd_bufs_get(const struct lu_env *env, struct dt_object *dt,
                if (lnb->lnb_page == NULL)
                        GOTO(cleanup, rc = -ENOMEM);
 
+               lnb->lnb_locked = 1;
                wait_on_page_writeback(lnb->lnb_page);
                BUG_ON(PageWriteback(lnb->lnb_page));
 
@@ -829,348 +846,6 @@ cleanup:
        return rc;
 }
 
-#ifndef HAVE_LDISKFS_MAP_BLOCKS
-
-#ifdef HAVE_EXT_PBLOCK /* Name changed to ext4_ext_pblock for kernel 2.6.35 */
-#define ldiskfs_ext_pblock(ex) ext_pblock((ex))
-#endif
-
-struct bpointers {
-       sector_t *blocks;
-       unsigned long start;
-       int num;
-       int init_num;
-       int create;
-};
-
-static long ldiskfs_ext_find_goal(struct inode *inode,
-                                 struct ldiskfs_ext_path *path,
-                                 unsigned long block, int *aflags)
-{
-       struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
-       unsigned long bg_start;
-       unsigned long colour;
-       int depth;
-
-       if (path) {
-               struct ldiskfs_extent *ex;
-               depth = path->p_depth;
-
-               /* try to predict block placement */
-               if ((ex = path[depth].p_ext))
-                       return ldiskfs_ext_pblock(ex) +
-                               (block - le32_to_cpu(ex->ee_block));
-
-               /* it looks index is empty
-                * try to find starting from index itself */
-               if (path[depth].p_bh)
-                       return path[depth].p_bh->b_blocknr;
-       }
-
-       /* OK. use inode's group */
-       bg_start = (ei->i_block_group * LDISKFS_BLOCKS_PER_GROUP(inode->i_sb)) +
-               le32_to_cpu(LDISKFS_SB(inode->i_sb)->s_es->s_first_data_block);
-       colour = (current->pid % 16) *
-               (LDISKFS_BLOCKS_PER_GROUP(inode->i_sb) / 16);
-       return bg_start + colour + block;
-}
-
-static unsigned long new_blocks(handle_t *handle, struct inode *inode,
-                               struct ldiskfs_ext_path *path,
-                               unsigned long block, unsigned long *count,
-                               int *err)
-{
-       struct ldiskfs_allocation_request ar;
-       unsigned long pblock;
-       int aflags;
-
-       /* find neighbour allocated blocks */
-       ar.lleft = block;
-       *err = ldiskfs_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
-       if (*err)
-               return 0;
-       ar.lright = block;
-       *err = ldiskfs_ext_search_right(inode, path, &ar.lright, &ar.pright);
-       if (*err)
-               return 0;
-
-       /* allocate new block */
-       ar.goal = ldiskfs_ext_find_goal(inode, path, block, &aflags);
-       ar.inode = inode;
-       ar.logical = block;
-       ar.len = *count;
-       ar.flags = LDISKFS_MB_HINT_DATA;
-       pblock = ldiskfs_mb_new_blocks(handle, &ar, err);
-       *count = ar.len;
-       return pblock;
-}
-
-static int ldiskfs_ext_new_extent_cb(struct inode *inode,
-                                    struct ldiskfs_ext_path *path,
-                                    struct ldiskfs_ext_cache *cex,
-#ifdef HAVE_EXT_PREPARE_CB_EXTENT
-                                    struct ldiskfs_extent *ex,
-#endif
-                                    void *cbdata)
-{
-       struct bpointers *bp = cbdata;
-       struct ldiskfs_extent nex;
-       unsigned long pblock = 0;
-       unsigned long tgen;
-       int err, i;
-       unsigned long count;
-       handle_t *handle;
-
-#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
-               err = EXT_CONTINUE;
-               goto map;
-       }
-
-       if (bp->create == 0) {
-               i = 0;
-               if (cex->ec_block < bp->start)
-                       i = bp->start - cex->ec_block;
-               if (i >= cex->ec_len)
-                       CERROR("nothing to do?! i = %d, e_num = %u\n",
-                                       i, cex->ec_len);
-               for (; i < cex->ec_len && bp->num; i++) {
-                       *(bp->blocks) = 0;
-                       bp->blocks++;
-                       bp->num--;
-                       bp->start++;
-               }
-
-               return EXT_CONTINUE;
-       }
-
-       tgen = LDISKFS_I(inode)->i_ext_generation;
-       count = ldiskfs_ext_calc_credits_for_insert(inode, path);
-
-       handle = osd_journal_start(inode, LDISKFS_HT_MISC,
-                                  count + LDISKFS_ALLOC_NEEDED + 1);
-       if (IS_ERR(handle)) {
-               return PTR_ERR(handle);
-       }
-
-       if (tgen != LDISKFS_I(inode)->i_ext_generation) {
-               /* the tree has changed. so path can be invalid at moment */
-               ldiskfs_journal_stop(handle);
-               return EXT_REPEAT;
-       }
-
-       /* In 2.6.32 kernel, ldiskfs_ext_walk_space()'s callback func is not
-        * protected by i_data_sem as whole. so we patch it to store
-        * generation to path and now verify the tree hasn't changed */
-       down_write((&LDISKFS_I(inode)->i_data_sem));
-
-       /* validate extent, make sure the extent tree does not changed */
-       if (LDISKFS_I(inode)->i_ext_generation != path[0].p_generation) {
-               /* cex is invalid, try again */
-               up_write(&LDISKFS_I(inode)->i_data_sem);
-               ldiskfs_journal_stop(handle);
-               return EXT_REPEAT;
-       }
-
-       count = cex->ec_len;
-       pblock = new_blocks(handle, inode, path, cex->ec_block, &count, &err);
-       if (!pblock)
-               goto out;
-       BUG_ON(count > cex->ec_len);
-
-       /* insert new extent */
-       nex.ee_block = cpu_to_le32(cex->ec_block);
-       ldiskfs_ext_store_pblock(&nex, pblock);
-       nex.ee_len = cpu_to_le16(count);
-       err = ldiskfs_ext_insert_extent(handle, inode, path, &nex, 0);
-       if (err) {
-               /* free data blocks we just allocated */
-               /* not a good idea to call discard here directly,
-                * but otherwise we'd need to call it every free() */
-               ldiskfs_discard_preallocations(inode);
-#ifdef HAVE_EXT_FREE_BLOCK_WITH_BUFFER_HEAD /* Introduced in 2.6.32-rc7 */
-               ldiskfs_free_blocks(handle, inode, NULL,
-                                   ldiskfs_ext_pblock(&nex),
-                                   le16_to_cpu(nex.ee_len), 0);
-#else
-               ldiskfs_free_blocks(handle, inode, ldiskfs_ext_pblock(&nex),
-                                   le16_to_cpu(nex.ee_len), 0);
-#endif
-               goto out;
-       }
-
-       /*
-        * Putting len of the actual extent we just inserted,
-        * we are asking ldiskfs_ext_walk_space() to continue
-        * scaning after that block
-        */
-       cex->ec_len = le16_to_cpu(nex.ee_len);
-       cex->ec_start = ldiskfs_ext_pblock(&nex);
-       BUG_ON(le16_to_cpu(nex.ee_len) == 0);
-       BUG_ON(le32_to_cpu(nex.ee_block) != cex->ec_block);
-
-out:
-       up_write((&LDISKFS_I(inode)->i_data_sem));
-       ldiskfs_journal_stop(handle);
-map:
-       if (err >= 0) {
-               /* map blocks */
-               if (bp->num == 0) {
-                       CERROR("hmm. why do we find this extent?\n");
-                       CERROR("initial space: %lu:%u\n",
-                               bp->start, bp->init_num);
-#ifdef LDISKFS_EXT_CACHE_EXTENT /* until kernel 2.6.37 */
-                       CERROR("current extent: %u/%u/%llu %d\n",
-                               cex->ec_block, cex->ec_len,
-                               (unsigned long long)cex->ec_start,
-                               cex->ec_type);
-#else
-                       CERROR("current extent: %u/%u/%llu\n",
-                               cex->ec_block, cex->ec_len,
-                               (unsigned long long)cex->ec_start);
-#endif
-               }
-               i = 0;
-               if (cex->ec_block < bp->start)
-                       i = bp->start - cex->ec_block;
-               if (i >= cex->ec_len)
-                       CERROR("nothing to do?! i = %d, e_num = %u\n",
-                                       i, cex->ec_len);
-               for (; i < cex->ec_len && bp->num; i++) {
-                       *(bp->blocks) = cex->ec_start + i;
-                       /* unmap any underlying metadata from
-                        * the block device mapping.  b=6998.
-                        */
-                       if (pblock != 0)
-                               clean_bdev_aliases(inode->i_sb->s_bdev,
-                                                  *(bp->blocks), 1);
-                       bp->blocks++;
-                       bp->num--;
-                       bp->start++;
-               }
-       }
-       return err;
-}
-
-static int osd_ldiskfs_map_nblocks(struct inode *inode, unsigned long index,
-                                  int clen, sector_t *blocks, int create)
-{
-       int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
-       struct bpointers bp;
-       int err;
-
-       if (index + clen >= inode->i_sb->s_maxbytes >> PAGE_SHIFT)
-               return -EFBIG;
-
-       bp.blocks = blocks;
-       bp.start = index * blocks_per_page;
-       bp.init_num = bp.num = clen * blocks_per_page;
-       bp.create = create;
-
-       CDEBUG(D_OTHER, "blocks %lu-%lu requested for inode %u\n",
-              bp.start, bp.start + bp.num - 1, (unsigned)inode->i_ino);
-
-       err = ldiskfs_ext_walk_space(inode, bp.start, bp.num,
-                                    ldiskfs_ext_new_extent_cb, &bp);
-       ldiskfs_ext_invalidate_cache(inode);
-
-       return err;
-}
-
-static int osd_ldiskfs_map_bm_inode_pages(struct inode *inode,
-                                         struct page **page, int pages,
-                                         sector_t *blocks, int create)
-{
-       int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
-       pgoff_t bitmap_max_page_index;
-       sector_t *b;
-       int rc = 0, i;
-
-       bitmap_max_page_index = LDISKFS_SB(inode->i_sb)->s_bitmap_maxbytes >>
-                               PAGE_SHIFT;
-       for (i = 0, b = blocks; i < pages; i++, page++) {
-               if ((*page)->index + 1 >= bitmap_max_page_index) {
-                       rc = -EFBIG;
-                       break;
-               }
-               rc = ldiskfs_map_inode_page(inode, *page, b, create);
-               if (rc) {
-                       CERROR("ino %lu, blk %llu create %d: rc %d\n",
-                              inode->i_ino,
-                              (unsigned long long)*b, create, rc);
-                       break;
-               }
-               b += blocks_per_page;
-       }
-       return rc;
-}
-
-static int osd_ldiskfs_map_ext_inode_pages(struct inode *inode,
-                                          struct page **page,
-                                          int pages, sector_t *blocks,
-                                          int create)
-{
-       int rc = 0, i = 0, clen = 0;
-       struct page *fp = NULL;
-
-       CDEBUG(D_OTHER, "inode %lu: map %d pages from %lu\n",
-               inode->i_ino, pages, (*page)->index);
-
-       /* pages are sorted already. so, we just have to find
-        * contig. space and process them properly */
-       while (i < pages) {
-               if (fp == NULL) {
-                       /* start new extent */
-                       fp = *page++;
-                       clen = 1;
-                       i++;
-                       continue;
-               } else if (fp->index + clen == (*page)->index) {
-                       /* continue the extent */
-                       page++;
-                       clen++;
-                       i++;
-                       continue;
-               }
-
-               /* process found extent */
-               rc = osd_ldiskfs_map_nblocks(inode, fp->index, clen,
-                                            blocks, create);
-               if (rc)
-                       GOTO(cleanup, rc);
-
-               /* look for next extent */
-               fp = NULL;
-               blocks += clen * (PAGE_SIZE >> inode->i_blkbits);
-       }
-
-       if (fp)
-               rc = osd_ldiskfs_map_nblocks(inode, fp->index, clen,
-                                            blocks, create);
-
-cleanup:
-       return rc;
-}
-
-static int osd_ldiskfs_map_inode_pages(struct inode *inode, struct page **page,
-                                      int pages, sector_t *blocks,
-                                      int create)
-{
-       int rc;
-
-       if (LDISKFS_I(inode)->i_flags & LDISKFS_EXTENTS_FL) {
-               rc = osd_ldiskfs_map_ext_inode_pages(inode, page, pages,
-                                                    blocks, create);
-               return rc;
-       }
-       rc = osd_ldiskfs_map_bm_inode_pages(inode, page, pages, blocks, create);
-
-       return rc;
-}
-#else
 static int osd_ldiskfs_map_inode_pages(struct inode *inode, struct page **page,
                                       int pages, sector_t *blocks,
                                       int create)
@@ -1256,7 +931,6 @@ cont_map:
 cleanup:
        return rc;
 }
-#endif /* HAVE_LDISKFS_MAP_BLOCKS */
 
 static int osd_write_prep(const struct lu_env *env, struct dt_object *dt,
                           struct niobuf_local *lnb, int npages)
@@ -1369,7 +1043,7 @@ static int osd_is_mapped(struct dt_object *dt, __u64 offset,
        fei.fi_extents_start = &fe;
 
        saved_fs = get_fs();
-       set_fs(get_ds());
+       set_fs(KERNEL_DS);
        rc = inode->i_op->fiemap(inode, &fei, offset, FIEMAP_MAX_OFFSET-offset);
        set_fs(saved_fs);
        if (rc != 0)
@@ -1529,7 +1203,7 @@ static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
                RETURN(rc);
 
        isize = i_size_read(inode);
-       ll_vfs_dq_init(inode);
+       dquot_initialize(inode);
 
         for (i = 0; i < npages; i++) {
                if (lnb[i].lnb_rc == -ENOSPC &&
@@ -1655,16 +1329,23 @@ static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
                if (OBD_FAIL_CHECK(OBD_FAIL_OST_FAKE_RW))
                        SetPageUptodate(lnb[i].lnb_page);
 
+               if (cache == 0)
+                       generic_error_remove_page(inode->i_mapping,
+                                                 lnb[i].lnb_page);
+
                if (PageUptodate(lnb[i].lnb_page)) {
                        cache_hits++;
+                       unlock_page(lnb[i].lnb_page);
                } else {
                        cache_misses++;
                        osd_iobuf_add_page(iobuf, &lnb[i]);
                }
-
-               if (cache == 0)
-                       generic_error_remove_page(inode->i_mapping,
-                                                 lnb[i].lnb_page);
+               /* no need to unlock in osd_bufs_put(), the sooner page is
+                * unlocked, the earlier another client can access it.
+                * notice real unlock_page() can be called few lines
+                * below after osd_do_bio(). lnb is a per-thread, so it's
+                * fine to have PG_locked and lnb_locked inconsistent here */
+               lnb[i].lnb_locked = 0;
        }
        end = ktime_get();
        timediff = ktime_us_delta(end, start);
@@ -1680,16 +1361,22 @@ static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
                lprocfs_counter_add(osd->od_stats, LPROC_OSD_CACHE_ACCESS,
                                    cache_hits + cache_misses);
 
-        if (iobuf->dr_npages) {
+       if (iobuf->dr_npages) {
                rc = osd_ldiskfs_map_inode_pages(inode, iobuf->dr_pages,
                                                 iobuf->dr_npages,
                                                 iobuf->dr_blocks, 0);
-                rc = osd_do_bio(osd, inode, iobuf);
+               rc = osd_do_bio(osd, inode, iobuf);
 
-                /* IO stats will be done in osd_bufs_put() */
-        }
+               /* IO stats will be done in osd_bufs_put() */
 
-        RETURN(rc);
+               /* early release to let others read data during the bulk */
+               for (i = 0; i < iobuf->dr_npages; i++) {
+                       LASSERT(PageLocked(iobuf->dr_pages[i]));
+                       unlock_page(iobuf->dr_pages[i]);
+               }
+       }
+
+       RETURN(rc);
 }
 
 /*
@@ -1727,7 +1414,8 @@ int osd_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs)
                loff_t diff = i_size_read(inode) - *offs;
                spin_unlock(&inode->i_lock);
                if (diff < 0) {
-                       CDEBUG(D_EXT2, "size %llu is too short to read @%llu\n",
+                       CDEBUG(D_OTHER,
+                              "size %llu is too short to read @%llu\n",
                               i_size_read(inode), *offs);
                        return -EBADR;
                } else if (diff == 0) {
@@ -1966,6 +1654,8 @@ int osd_ldiskfs_write_record(struct inode *inode, void *buf, int bufsize,
         int                 size;
         int                 boffs;
         int                 dirty_inode = 0;
+       struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
+       bool create, sparse;
 
        if (write_NUL) {
                /*
@@ -1977,8 +1667,15 @@ int osd_ldiskfs_write_record(struct inode *inode, void *buf, int bufsize,
                ++bufsize;
        }
 
+       /* sparse checking is racy, but sparse is very rare case, leave as is */
+       sparse = (new_size > 0 && (inode->i_blocks >> (inode->i_blkbits - 9)) <
+                 ((new_size - 1) >> inode->i_blkbits) + 1);
+
        while (bufsize > 0) {
                int credits = handle->h_buffer_credits;
+               bool sync;
+               unsigned long last_block = (new_size == 0) ? 0 :
+                                          (new_size - 1) >> inode->i_blkbits;
 
                if (bh)
                        brelse(bh);
@@ -1986,7 +1683,26 @@ int osd_ldiskfs_write_record(struct inode *inode, void *buf, int bufsize,
                block = offset >> inode->i_blkbits;
                boffs = offset & (blocksize - 1);
                size = min(blocksize - boffs, bufsize);
-               bh = __ldiskfs_bread(handle, inode, block, 1);
+               sync = (block > last_block || new_size == 0 || sparse);
+
+               if (sync)
+                       down(&ei->i_append_sem);
+
+               bh = __ldiskfs_bread(handle, inode, block, 0);
+
+               if (unlikely(IS_ERR_OR_NULL(bh) && !sync))
+                       CWARN("%s: adding bh without locking off %llu (block %lu, "
+                             "size %d, offs %llu)\n", inode->i_sb->s_id,
+                             offset, block, bufsize, *offs);
+
+               if (IS_ERR_OR_NULL(bh)) {
+                       bh = __ldiskfs_bread(handle, inode, block, 1);
+                       create = true;
+               } else {
+                       if (sync)
+                               up(&ei->i_append_sem);
+                       create = false;
+               }
                if (IS_ERR_OR_NULL(bh)) {
                        if (bh == NULL) {
                                err = -EIO;
@@ -2011,7 +1727,12 @@ int osd_ldiskfs_write_record(struct inode *inode, void *buf, int bufsize,
                LASSERTF(boffs + size <= bh->b_size,
                         "boffs %d size %d bh->b_size %lu\n",
                         boffs, size, (unsigned long)bh->b_size);
-                memcpy(bh->b_data + boffs, buf, size);
+               if (create) {
+                       memset(bh->b_data, 0, bh->b_size);
+                       if (sync)
+                               up(&ei->i_append_sem);
+               }
+               memcpy(bh->b_data + boffs, buf, size);
                err = ldiskfs_handle_dirty_metadata(handle, NULL, bh);
                 if (err)
                         break;
@@ -2059,7 +1780,7 @@ static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
 
         LASSERT(handle != NULL);
        LASSERT(inode != NULL);
-       ll_vfs_dq_init(inode);
+       dquot_initialize(inode);
 
         /* XXX: don't check: one declared chunk can be used many times */
        /* osd_trans_exec_op(env, handle, OSD_OT_WRITE); */
@@ -2139,7 +1860,7 @@ static int osd_punch(const struct lu_env *env, struct dt_object *dt,
        LASSERT(dt_object_exists(dt));
        LASSERT(osd_invariant(obj));
        LASSERT(inode != NULL);
-       ll_vfs_dq_init(inode);
+       dquot_initialize(inode);
 
        LASSERT(th);
        oh = container_of(th, struct osd_thandle, ot_super);
@@ -2229,7 +1950,7 @@ static int osd_fiemap_get(const struct lu_env *env, struct dt_object *dt,
        struct inode *inode = osd_dt_obj(dt)->oo_inode;
        u64 len;
        int rc;
-
+       mm_segment_t cur_fs;
 
        LASSERT(inode);
        if (inode->i_op->fiemap == NULL)
@@ -2249,10 +1970,18 @@ static int osd_fiemap_get(const struct lu_env *env, struct dt_object *dt,
        if (fieinfo.fi_flags & FIEMAP_FLAG_SYNC)
                filemap_write_and_wait(inode->i_mapping);
 
+       /* Save previous value address limit */
+       cur_fs = get_fs();
+       /* Set the address limit of the kernel */
+       set_fs(KERNEL_DS);
+
        rc = inode->i_op->fiemap(inode, &fieinfo, fm->fm_start, len);
        fm->fm_flags = fieinfo.fi_flags;
        fm->fm_mapped_extents = fieinfo.fi_extents_mapped;
 
+       /* Restore the previous address limt */
+       set_fs(cur_fs);
+
        return rc;
 }