Whamcloud - gitweb
LU-14205 osd-ldiskfs: fix return of osd_extend_restart_trans()
[fs/lustre-release.git] / lustre / osd-ldiskfs / osd_io.c
index fa04a31..7631d61 100644 (file)
@@ -38,6 +38,8 @@
  *
  */
 
+#define DEBUG_SUBSYSTEM        S_OSD
+
 /* prerequisite for linux/xattr.h */
 #include <linux/types.h>
 /* prerequisite for linux/xattr.h */
@@ -100,12 +102,12 @@ static int __osd_init_iobuf(struct osd_device *d, struct osd_iobuf *iobuf,
                i <<= 1;
 
        CDEBUG(D_OTHER, "realloc %u for %u (%u) pages\n",
-              (unsigned)(pages * sizeof(iobuf->dr_pages[0])), i, pages);
+              (unsigned int)(pages * sizeof(iobuf->dr_pages[0])), i, pages);
        pages = i;
        blocks = pages * (PAGE_SIZE >> osd_sb(d)->s_blocksize_bits);
        iobuf->dr_max_pages = 0;
        CDEBUG(D_OTHER, "realloc %u for %u blocks\n",
-              (unsigned)(blocks * sizeof(iobuf->dr_blocks[0])), blocks);
+              (unsigned int)(blocks * sizeof(iobuf->dr_blocks[0])), blocks);
 
        lu_buf_realloc(&iobuf->dr_bl_buf, blocks * sizeof(iobuf->dr_blocks[0]));
        iobuf->dr_blocks = iobuf->dr_bl_buf.lb_buf;
@@ -141,76 +143,51 @@ static void osd_iobuf_add_page(struct osd_iobuf *iobuf,
 
 void osd_fini_iobuf(struct osd_device *d, struct osd_iobuf *iobuf)
 {
-        int rw = iobuf->dr_rw;
-
-        if (iobuf->dr_elapsed_valid) {
-                iobuf->dr_elapsed_valid = 0;
-                LASSERT(iobuf->dr_dev == d);
-                LASSERT(iobuf->dr_frags > 0);
-                lprocfs_oh_tally(&d->od_brw_stats.
-                                 hist[BRW_R_DIO_FRAGS+rw],
-                                 iobuf->dr_frags);
+       int rw = iobuf->dr_rw;
+
+       if (iobuf->dr_elapsed_valid) {
+               iobuf->dr_elapsed_valid = 0;
+               LASSERT(iobuf->dr_dev == d);
+               LASSERT(iobuf->dr_frags > 0);
+               lprocfs_oh_tally(&d->od_brw_stats.hist[BRW_R_DIO_FRAGS+rw],
+                                iobuf->dr_frags);
                lprocfs_oh_tally_log2(&d->od_brw_stats.hist[BRW_R_IO_TIME+rw],
                                      ktime_to_ms(iobuf->dr_elapsed));
-        }
+       }
 }
 
 #ifdef HAVE_BIO_ENDIO_USES_ONE_ARG
 static void dio_complete_routine(struct bio *bio)
 {
-# ifdef HAVE_BI_STATUS
-       int error = bio->bi_status;
-# else
-       int error = bio->bi_error;
-# endif
+       int error = blk_status_to_errno(bio->bi_status);
 #else
 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
-         * DO NOT record procfs stats here!!! */
+       /* CAVEAT EMPTOR: possibly in IRQ context
+        * DO NOT record procfs stats here!!!
+        */
 
        if (unlikely(iobuf == NULL)) {
-               CERROR("***** bio->bi_private is NULL!  This should never "
-                      "happen.  Normally, I would crash here, but instead I "
-                      "will dump the bio contents to the console.  Please "
-                      "report this to <https://jira.whamcloud.com/> , along "
-                      "with any interesting messages leading up to this point "
-                      "(like SCSI errors, perhaps).  Because bi_private is "
-                      "NULL, I can't wake up the thread that initiated this "
-                      "IO - you will probably have to reboot this node.\n");
-               CERROR("bi_next: %p, bi_flags: %lx, "
-#ifdef HAVE_BI_RW
-                      "bi_rw: %lu,"
-#else
-                      "bi_opf: %u,"
-#endif
-                      "bi_vcnt: %d, bi_idx: %d, bi->size: %d, bi_end_io: %p,"
-                      "bi_cnt: %d, bi_private: %p\n", bio->bi_next,
-                       (unsigned long)bio->bi_flags,
-#ifdef HAVE_BI_RW
-                       bio->bi_rw,
-#else
-                       bio->bi_opf,
-#endif
-                       bio->bi_vcnt, bio_idx(bio),
-                       bio_sectors(bio) << 9, bio->bi_end_io,
-#ifdef HAVE_BI_CNT
-                       atomic_read(&bio->bi_cnt),
-#else
-                       atomic_read(&bio->__bi_cnt),
-#endif
-                       bio->bi_private);
+               CERROR("***** bio->bi_private is NULL!  This should never happen.  Normally, I would crash here, but instead I will dump the bio contents to the console.  Please report this to <https://jira.whamcloud.com/> , along with any interesting messages leading up to this point (like SCSI errors, perhaps).  Because bi_private is NULL, I can't wake up the thread that initiated this IO - you will probably have to reboot this node.\n");
+               CERROR("bi_next: %p, bi_flags: %lx, " __stringify(bi_opf)
+                      ": %x, bi_vcnt: %d, bi_idx: %d, bi->size: %d, bi_end_io: %p, bi_cnt: %d, bi_private: %p\n",
+                      bio->bi_next, (unsigned long)bio->bi_flags,
+                      (unsigned int)bio->bi_opf, bio->bi_vcnt, bio_idx(bio),
+                      bio_sectors(bio) << 9, bio->bi_end_io,
+                      atomic_read(&bio->__bi_cnt),
+                      bio->bi_private);
                return;
        }
 
        /* 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)));
@@ -244,7 +221,8 @@ static void dio_complete_routine(struct bio *bio, int error)
         * filter_clear_dreq().  It was then possible to exhaust the biovec-256
         * mempool when serious on-disk fragmentation was encountered,
         * deadlocking the OST.  The bios are now released as soon as complete
-        * so the pool cannot be exhausted while IOs are competing. bug 10076 */
+        * so the pool cannot be exhausted while IOs are competing. b=10076
+        */
        bio_put(bio);
 }
 
@@ -273,15 +251,12 @@ static void record_start_io(struct osd_iobuf *iobuf, int size)
 
 static void osd_submit_bio(int rw, struct bio *bio)
 {
-        LASSERTF(rw == 0 || rw == 1, "%x\n", rw);
+       LASSERTF(rw == 0 || rw == 1, "%x\n", rw);
 #ifdef HAVE_SUBMIT_BIO_2ARGS
-        if (rw == 0)
-                submit_bio(READ, bio);
-        else
-                submit_bio(WRITE, bio);
+       submit_bio(rw ? WRITE : READ, bio);
 #else
-        bio->bi_opf |= rw;
-        submit_bio(bio);
+       bio->bi_opf |= rw;
+       submit_bio(bio);
 #endif
 }
 
@@ -293,6 +268,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
@@ -300,11 +276,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);
@@ -326,9 +302,8 @@ static int bio_dif_compare(__u16 *expected_guard_buf, void *bio_prot_buf,
        for (i = 0; i < sectors; i++) {
                bio_guard = (__u16 *)bio_prot_buf;
                if (*bio_guard != *expected_guard) {
-                       CERROR("unexpected guard tags on sector %d "
-                              "expected guard %u, bio guard "
-                              "%u, sectors %u, tuple size %d\n",
+                       CERROR(
+                              "unexpected guard tags on sector %d expected guard %u, bio guard %u, sectors %u, tuple size %d\n",
                               i, *expected_guard, *bio_guard, sectors,
                               tuple_size);
                        return -EIO;
@@ -350,12 +325,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;
@@ -381,26 +357,20 @@ static int osd_bio_integrity_handle(struct osd_device *osd, struct bio *bio,
                                    bool integrity_enabled)
 {
        struct super_block *sb = osd_sb(osd);
-       int rc;
-#ifdef HAVE_BIO_INTEGRITY_PREP_FN
        integrity_gen_fn *generate_fn = NULL;
        integrity_vrfy_fn *verify_fn = NULL;
-#endif
+       int rc;
 
        ENTRY;
 
        if (!integrity_enabled)
                RETURN(0);
 
-#ifdef HAVE_BIO_INTEGRITY_PREP_FN
        rc = osd_get_integrity_profile(osd, &generate_fn, &verify_fn);
        if (rc)
                RETURN(rc);
 
        rc = bio_integrity_prep_fn(bio, generate_fn, verify_fn);
-#else
-       rc = bio_integrity_prep(bio);
-#endif
        if (rc)
                RETURN(rc);
 
@@ -423,35 +393,35 @@ static int osd_bio_integrity_handle(struct osd_device *osd, struct bio *bio,
 #ifdef HAVE_BIO_INTEGRITY_PREP_FN
 #  ifdef HAVE_BIO_ENDIO_USES_ONE_ARG
 static void dio_integrity_complete_routine(struct bio *bio)
-{
 #  else
 static void dio_integrity_complete_routine(struct bio *bio, int error)
-{
 #  endif
+{
        struct osd_bio_private *bio_private = bio->bi_private;
 
        bio->bi_private = bio_private->obp_iobuf;
-#  ifdef HAVE_BIO_ENDIO_USES_ONE_ARG
-       dio_complete_routine(bio);
-#  else
-       dio_complete_routine(bio, error);
-#  endif
+       osd_dio_complete_routine(bio, 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,
                        struct osd_bio_private **pprivate)
 {
-#ifdef HAVE_BIO_INTEGRITY_PREP_FN
-       struct osd_bio_private *bio_private;
-
        ENTRY;
 
        *pprivate = NULL;
+
+#ifdef HAVE_BIO_INTEGRITY_PREP_FN
        if (integrity_enabled) {
+               struct osd_bio_private *bio_private = NULL;
+
                OBD_ALLOC_GFP(bio_private, sizeof(*bio_private), GFP_NOIO);
                if (bio_private == NULL)
                        RETURN(-ENOMEM);
@@ -460,28 +430,24 @@ static int osd_bio_init(struct bio *bio, struct osd_iobuf *iobuf,
                bio_private->obp_start_page_idx = start_page_idx;
                bio_private->obp_iobuf = iobuf;
                *pprivate = bio_private;
-       } else {
+       } else
+#endif
+       {
                bio->bi_end_io = dio_complete_routine;
                bio->bi_private = iobuf;
        }
-       RETURN(0);
-#else
-       ENTRY;
 
-       bio->bi_end_io = dio_complete_routine;
-       bio->bi_private = iobuf;
        RETURN(0);
-#endif
 }
 
 static int osd_do_bio(struct osd_device *osd, struct inode *inode,
-                      struct osd_iobuf *iobuf)
+                     struct osd_iobuf *iobuf, sector_t start_blocks,
+                     sector_t count)
 {
        int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
        struct page **pages = iobuf->dr_pages;
        int npages = iobuf->dr_npages;
        sector_t *blocks = iobuf->dr_blocks;
-       int total_blocks = npages * blocks_per_page;
        struct super_block *sb = inode->i_sb;
        int sector_bits = sb->s_blocksize_bits - 9;
        unsigned int blocksize = sb->s_blocksize;
@@ -493,74 +459,82 @@ static int osd_do_bio(struct osd_device *osd, struct inode *inode,
        unsigned int page_offset;
        sector_t sector;
        int nblocks;
-       int block_idx;
-       int page_idx;
+       int block_idx, block_idx_end;
+       int page_idx, page_idx_start;
        int i;
        int rc = 0;
        bool fault_inject;
        bool integrity_enabled;
-       DECLARE_PLUG(plug);
+       struct blk_plug plug;
+       int blocks_left_page;
+
        ENTRY;
 
        fault_inject = OBD_FAIL_CHECK(OBD_FAIL_OST_INTEGRITY_FAULT);
-        LASSERT(iobuf->dr_npages == npages);
+       LASSERT(iobuf->dr_npages == npages);
 
        integrity_enabled = bdev_integrity_enabled(bdev, iobuf->dr_rw);
 
        osd_brw_stats_update(osd, iobuf);
        iobuf->dr_start_time = ktime_get();
 
+       if (!count)
+               count = npages * blocks_per_page;
+       block_idx_end = start_blocks + count;
+
        blk_start_plug(&plug);
-        for (page_idx = 0, block_idx = 0;
-             page_idx < npages;
-             page_idx++, block_idx += blocks_per_page) {
-
-                page = pages[page_idx];
-                LASSERT(block_idx + blocks_per_page <= total_blocks);
-
-                for (i = 0, page_offset = 0;
-                     i < blocks_per_page;
-                     i += nblocks, page_offset += blocksize * nblocks) {
-
-                        nblocks = 1;
-
-                        if (blocks[block_idx + i] == 0) {  /* hole */
-                                LASSERTF(iobuf->dr_rw == 0,
-                                         "page_idx %u, block_idx %u, i %u\n",
-                                         page_idx, block_idx, i);
-                                memset(kmap(page) + page_offset, 0, blocksize);
-                                kunmap(page);
-                                continue;
-                        }
-
-                        sector = (sector_t)blocks[block_idx + i] << sector_bits;
-
-                        /* Additional contiguous file blocks? */
-                        while (i + nblocks < blocks_per_page &&
-                               (sector + (nblocks << sector_bits)) ==
-                               ((sector_t)blocks[block_idx + i + nblocks] <<
-                                sector_bits))
-                                nblocks++;
-
-                        if (bio != NULL &&
-                            can_be_merged(bio, sector) &&
-                            bio_add_page(bio, page,
-                                         blocksize * nblocks, page_offset) != 0)
-                                continue;       /* added this frag OK */
+
+       page_idx_start = start_blocks / blocks_per_page;
+       for (page_idx = page_idx_start, block_idx = start_blocks;
+            block_idx < block_idx_end; page_idx++,
+            block_idx += blocks_left_page) {
+               page = pages[page_idx];
+               LASSERT(page_idx < iobuf->dr_npages);
+
+               i = block_idx % blocks_per_page;
+               blocks_left_page = blocks_per_page - i;
+               for (page_offset = i * blocksize; i < blocks_left_page;
+                    i += nblocks, page_offset += blocksize * nblocks) {
+                       nblocks = 1;
+
+                       if (blocks[block_idx + i] == 0) {  /* hole */
+                               LASSERTF(iobuf->dr_rw == 0,
+                                        "page_idx %u, block_idx %u, i %u,"
+                                        "start_blocks: %llu, count: %llu, npages: %d\n",
+                                        page_idx, block_idx, i,
+                                        (unsigned long long)start_blocks,
+                                        (unsigned long long)count, npages);
+                               memset(kmap(page) + page_offset, 0, blocksize);
+                               kunmap(page);
+                               continue;
+                       }
+
+                       sector = (sector_t)blocks[block_idx + i] << sector_bits;
+
+                       /* Additional contiguous file blocks? */
+                       while (i + nblocks < blocks_left_page &&
+                              (sector + (nblocks << sector_bits)) ==
+                              ((sector_t)blocks[block_idx + i + nblocks] <<
+                                sector_bits))
+                               nblocks++;
+
+                       if (bio && can_be_merged(bio, sector) &&
+                           bio_add_page(bio, page, blocksize * nblocks,
+                                        page_offset) != 0)
+                               continue;       /* added this frag OK */
 
                        if (bio != NULL) {
                                struct request_queue *q = bio_get_queue(bio);
                                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);
@@ -576,23 +550,19 @@ static int osd_do_bio(struct osd_device *osd, struct inode *inode,
                        bio_start_page_idx = page_idx;
                        /* allocate new bio */
                        bio = bio_alloc(GFP_NOIO, min(BIO_MAX_PAGES,
-                                                     (npages - page_idx) *
-                                                     blocks_per_page));
-                        if (bio == NULL) {
-                                CERROR("Can't allocate bio %u*%u = %u pages\n",
-                                       (npages - page_idx), blocks_per_page,
-                                       (npages - page_idx) * blocks_per_page);
-                                rc = -ENOMEM;
-                                goto out;
-                        }
+                                       (block_idx_end - block_idx +
+                                        blocks_left_page - 1)));
+                       if (bio == NULL) {
+                               CERROR("Can't allocate bio %u pages\n",
+                                      block_idx_end - block_idx +
+                                      blocks_left_page - 1);
+                               rc = -ENOMEM;
+                               goto out;
+                       }
 
                        bio_set_dev(bio, bdev);
                        bio_set_sector(bio, sector);
-#ifdef HAVE_BI_RW
-                       bio->bi_rw = (iobuf->dr_rw == 0) ? READ : WRITE;
-#else
-                       bio->bi_opf = (iobuf->dr_rw == 0) ? READ : WRITE;
-#endif
+                       bio->bi_opf = iobuf->dr_rw ? WRITE : READ;
                        rc = osd_bio_init(bio, iobuf, integrity_enabled,
                                          bio_start_page_idx, &bio_private);
                        if (rc) {
@@ -627,7 +597,8 @@ out:
        /* in order to achieve better IO throughput, we don't wait for writes
         * completion here. instead we proceed with transaction commit in
         * parallel and wait for IO completion once transaction is stopped
-        * see osd_trans_stop() for more details -bzzz */
+        * see osd_trans_stop() for more details -bzzz
+        */
        if (iobuf->dr_rw == 0 || fault_inject) {
                wait_event(iobuf->dr_wait,
                           atomic_read(&iobuf->dr_numreqs) == 0);
@@ -645,18 +616,24 @@ 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)
 {
-        ENTRY;
+       int rc = 0;
+       ENTRY;
 
-        *nrpages = 0;
+       *nrpages = 0;
 
-        while (len > 0) {
+       while (len > 0) {
                int poff = offset & (PAGE_SIZE - 1);
                int plen = PAGE_SIZE - poff;
 
-                if (plen > len)
-                        plen = len;
+               if (*nrpages >= maxlnb) {
+                       rc = -EOVERFLOW;
+                       break;
+               }
+
+               if (plen > len)
+                       plen = len;
                lnb->lnb_file_offset = offset;
                lnb->lnb_page_offset = poff;
                lnb->lnb_len = plen;
@@ -666,65 +643,72 @@ 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);
+               offset += plen;
+               len -= plen;
+               lnb++;
+               (*nrpages)++;
+       }
 
-                LASSERTF(plen <= len, "plen %u, len %lld\n", plen,
-                         (long long) len);
-                offset += plen;
-                len -= plen;
-                lnb++;
-                (*nrpages)++;
-        }
-
-        RETURN(0);
+       RETURN(rc);
 }
 
 static struct page *osd_get_page(const struct lu_env *env, struct dt_object *dt,
-                                loff_t offset, gfp_t gfp_mask)
+                                loff_t offset, gfp_t gfp_mask, bool cache)
 {
        struct osd_thread_info *oti = osd_oti_get(env);
        struct inode *inode = osd_dt_obj(dt)->oo_inode;
        struct osd_device *d = osd_obj2dev(osd_dt_obj(dt));
        struct page *page;
-       int cur = oti->oti_dio_pages_used;
+       int cur;
 
-        LASSERT(inode);
+       LASSERT(inode);
 
-       if (osd_use_page_cache(d)) {
+       if (cache) {
                page = find_or_create_page(inode->i_mapping,
-                                          offset >> PAGE_SHIFT,
-                                          gfp_mask);
+                                          offset >> PAGE_SHIFT, gfp_mask);
 
-               if (likely(page))
-                       LASSERT(!test_bit(PG_private_2, &page->flags));
-               else
+               if (likely(page)) {
+                       LASSERT(!PagePrivate2(page));
+                       wait_on_page_writeback(page);
+               } else {
                        lprocfs_counter_add(d->od_stats, LPROC_OSD_NO_PAGE, 1);
-       } else {
+               }
 
-               LASSERT(oti->oti_dio_pages);
+               return page;
+       }
 
-               if (unlikely(!oti->oti_dio_pages[cur])) {
-                       LASSERT(cur < PTLRPC_MAX_BRW_PAGES);
-                       page = alloc_page(gfp_mask);
-                       if (!page)
-                               return NULL;
-                       oti->oti_dio_pages[cur] = page;
+       if (inode->i_mapping->nrpages) {
+               /* consult with pagecache, but do not create new pages */
+               /* this is normally used once */
+               page = find_lock_page(inode->i_mapping, offset >> PAGE_SHIFT);
+               if (page) {
+                       wait_on_page_writeback(page);
+                       return page;
                }
+       }
 
-               page = oti->oti_dio_pages[cur];
-               LASSERT(!test_bit(PG_private_2, &page->flags));
-               set_bit(PG_private_2, &page->flags);
-               oti->oti_dio_pages_used++;
-
-               LASSERT(!PageLocked(page));
+       LASSERT(oti->oti_dio_pages);
+       cur = oti->oti_dio_pages_used;
+       page = oti->oti_dio_pages[cur];
+
+       if (unlikely(!page)) {
+               LASSERT(cur < PTLRPC_MAX_BRW_PAGES);
+               page = alloc_page(gfp_mask);
+               if (!page)
+                       return NULL;
+               oti->oti_dio_pages[cur] = page;
+               SetPagePrivate2(page);
                lock_page(page);
-
-               LASSERT(!page->mapping);
-               LASSERT(!PageWriteback(page));
-               ClearPageUptodate(page);
-
-               page->index = offset >> PAGE_SHIFT;
        }
 
+       ClearPageUptodate(page);
+       page->index = offset >> PAGE_SHIFT;
+       oti->oti_dio_pages_used++;
+
        return page;
 }
 
@@ -773,21 +757,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);
+                * to prevent reuse
+                */
+               if (PagePrivate2(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);
                }
-               dt_object_put(env, dt);
 
                lnb[i].lnb_page = NULL;
        }
@@ -826,40 +807,90 @@ 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);
-       int npages, i, rc = 0;
+       struct osd_device *osd   = osd_obj2dev(obj);
+       int npages, i, iosize, rc = 0;
+       bool cache, write;
+       loff_t fsize;
        gfp_t gfp_mask;
 
        LASSERT(obj->oo_inode);
 
-       if (!osd_use_page_cache(osd_obj2dev(obj))) {
-               if (unlikely(!oti->oti_dio_pages)) {
-                       OBD_ALLOC(oti->oti_dio_pages,
-                                 sizeof(struct page *) * PTLRPC_MAX_BRW_PAGES);
-                       if (!oti->oti_dio_pages)
-                               return -ENOMEM;
+       rc = osd_map_remote_to_local(pos, len, &npages, lnb, maxlnb);
+       if (rc)
+               RETURN(rc);
+
+       write = rw & DT_BUFS_TYPE_WRITE;
+
+       fsize = lnb[npages - 1].lnb_file_offset + lnb[npages - 1].lnb_len;
+       iosize = fsize - lnb[0].lnb_file_offset;
+       fsize = max(fsize, i_size_read(obj->oo_inode));
+
+       cache = rw & DT_BUFS_TYPE_READAHEAD;
+       if (cache)
+               goto bypass_checks;
+
+       cache = osd_use_page_cache(osd);
+       while (cache) {
+               if (write) {
+                       if (!osd->od_writethrough_cache) {
+                               cache = false;
+                               break;
+                       }
+                       if (iosize > osd->od_writethrough_max_iosize) {
+                               cache = false;
+                               break;
+                       }
+               } else {
+                       if (!osd->od_read_cache) {
+                               cache = false;
+                               break;
+                       }
+                       if (iosize > osd->od_readcache_max_iosize) {
+                               cache = false;
+                               break;
+                       }
                }
+               /* don't use cache on large files */
+               if (osd->od_readcache_max_filesize &&
+                   fsize > osd->od_readcache_max_filesize)
+                       cache = false;
+               break;
        }
 
-       osd_map_remote_to_local(pos, len, &npages, lnb);
+bypass_checks:
+       if (!cache && unlikely(!oti->oti_dio_pages)) {
+               OBD_ALLOC_PTR_ARRAY_LARGE(oti->oti_dio_pages,
+                                         PTLRPC_MAX_BRW_PAGES);
+               if (!oti->oti_dio_pages)
+                       return -ENOMEM;
+       }
 
        /* this could also try less hard for DT_BUFS_TYPE_READAHEAD pages */
        gfp_mask = rw & DT_BUFS_TYPE_LOCAL ? (GFP_NOFS | __GFP_HIGHMEM) :
                                             GFP_HIGHUSER;
        for (i = 0; i < npages; i++, lnb++) {
                lnb->lnb_page = osd_get_page(env, dt, lnb->lnb_file_offset,
-                                            gfp_mask);
+                                            gfp_mask, cache);
                if (lnb->lnb_page == NULL)
                        GOTO(cleanup, rc = -ENOMEM);
 
-               wait_on_page_writeback(lnb->lnb_page);
-               BUG_ON(PageWriteback(lnb->lnb_page));
+               lnb->lnb_locked = 1;
+       }
 
-               lu_object_get(&dt->do_lu);
+#if 0
+       /* XXX: this version doesn't invalidate cached pages, but use them */
+       if (!cache && write && obj->oo_inode->i_mapping->nrpages) {
+               /* do not allow data aliasing, invalidate pagecache */
+               /* XXX: can be quite expensive in mixed case */
+               invalidate_mapping_pages(obj->oo_inode->i_mapping,
+                               lnb[0].lnb_file_offset >> PAGE_SHIFT,
+                               lnb[npages - 1].lnb_file_offset >> PAGE_SHIFT);
        }
+#endif
 
        RETURN(i);
 
@@ -868,364 +899,130 @@ cleanup:
                osd_bufs_put(env, dt, lnb - i, i);
        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)
+/* Borrow @ext4_chunk_trans_blocks */
+static int osd_chunk_trans_blocks(struct inode *inode, int nrblocks)
 {
-       struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
-       unsigned long bg_start;
-       unsigned long colour;
+       ldiskfs_group_t groups;
+       int gdpblocks;
+       int idxblocks;
        int depth;
+       int ret;
 
-       if (path) {
-               struct ldiskfs_extent *ex;
-               depth = path->p_depth;
+       depth = ext_depth(inode);
+       idxblocks = depth * 2;
 
-               /* try to predict block placement */
-               if ((ex = path[depth].p_ext))
-                       return ldiskfs_ext_pblock(ex) +
-                               (block - le32_to_cpu(ex->ee_block));
+       /*
+        * Now let's see how many group bitmaps and group descriptors need
+        * to account.
+        */
+       groups = idxblocks + 1;
+       gdpblocks = groups;
+       if (groups > LDISKFS_SB(inode->i_sb)->s_groups_count)
+               groups = LDISKFS_SB(inode->i_sb)->s_groups_count;
+       if (gdpblocks > LDISKFS_SB(inode->i_sb)->s_gdb_count)
+               gdpblocks = LDISKFS_SB(inode->i_sb)->s_gdb_count;
 
-               /* it looks index is empty
-                * try to find starting from index itself */
-               if (path[depth].p_bh)
-                       return path[depth].p_bh->b_blocknr;
-       }
+       /* bitmaps and block group descriptor blocks */
+       ret = idxblocks + groups + gdpblocks;
+
+       /* Blocks for super block, inode, quota and xattr blocks */
+       ret += LDISKFS_META_TRANS_BLOCKS(inode->i_sb);
 
-       /* 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;
+       return ret;
 }
 
-static unsigned long new_blocks(handle_t *handle, struct inode *inode,
-                               struct ldiskfs_ext_path *path,
-                               unsigned long block, unsigned long *count,
-                               int *err)
+#ifdef HAVE_LDISKFS_JOURNAL_ENSURE_CREDITS
+static int osd_extend_trans(handle_t *handle, int needed,
+                           struct inode *inode)
 {
-       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;
+       return  __ldiskfs_journal_ensure_credits(handle, needed, needed,
+               ldiskfs_trans_default_revoke_credits(inode->i_sb));
 }
 
-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)
+static int osd_extend_restart_trans(handle_t *handle, int needed,
+                                   struct inode *inode)
 {
-       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;
-       }
+       int rc;
 
-       /* 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;
-       }
+       rc = ldiskfs_journal_ensure_credits(handle, needed,
+               ldiskfs_trans_default_revoke_credits(inode->i_sb));
+       /* this means journal has been restarted */
+       if (rc > 0)
+               rc = 0;
 
-       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);
+       return rc;
+}
 #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);
+static int osd_extend_trans(handle_t *handle, int needed,
+                           struct inode *inode)
+{
+       if (ldiskfs_handle_has_enough_credits(handle, needed))
+               return 0;
 
-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;
-                       if (pblock != 0) {
-                               /* unmap any possible underlying metadata from
-                                * the block device mapping.  bug 6998. */
-#ifndef HAVE_CLEAN_BDEV_ALIASES
-                               unmap_underlying_metadata(inode->i_sb->s_bdev,
-                                                         *(bp->blocks));
-#else
-                               clean_bdev_aliases(inode->i_sb->s_bdev,
-                                                  *(bp->blocks), 1);
-#endif
-                       }
-                       bp->blocks++;
-                       bp->num--;
-                       bp->start++;
-               }
-       }
-       return err;
+       return ldiskfs_journal_extend(handle,
+                                     needed - handle->h_buffer_credits);
 }
 
-static int osd_ldiskfs_map_nblocks(struct inode *inode, unsigned long index,
-                                  int clen, sector_t *blocks, int create)
+static int osd_extend_restart_trans(handle_t *handle, int needed,
+                                   struct inode *inode)
 {
-       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);
+       int rc = osd_extend_trans(handle, needed, inode);
 
-       return err;
-}
+       if (rc <= 0)
+               return rc;
 
-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;
+       return ldiskfs_journal_restart(handle, needed);
 }
+#endif /* HAVE_LDISKFS_JOURNAL_ENSURE_CREDITS */
 
-static int osd_ldiskfs_map_ext_inode_pages(struct inode *inode,
-                                          struct page **page,
-                                          int pages, sector_t *blocks,
-                                          int create)
+static int osd_ldiskfs_map_write(struct inode *inode, struct osd_iobuf *iobuf,
+                                struct osd_device *osd, sector_t start_blocks,
+                                sector_t count, loff_t *disk_size,
+                                __u64 user_size)
 {
-       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;
-               }
+       /* if file has grown, take user_size into account */
+       if (user_size && *disk_size > user_size)
+               *disk_size = user_size;
 
-               /* 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);
+       spin_lock(&inode->i_lock);
+       if (*disk_size > i_size_read(inode)) {
+               i_size_write(inode, *disk_size);
+               LDISKFS_I(inode)->i_disksize = *disk_size;
+               spin_unlock(&inode->i_lock);
+               osd_dirty_inode(inode, I_DIRTY_DATASYNC);
+       } else {
+               spin_unlock(&inode->i_lock);
        }
 
-       if (fp)
-               rc = osd_ldiskfs_map_nblocks(inode, fp->index, clen,
-                                            blocks, create);
-
-cleanup:
-       return rc;
+       /*
+        * We don't do stats here as in read path because
+        * write is async: we'll do this in osd_put_bufs()
+        */
+       return osd_do_bio(osd, inode, iobuf, start_blocks, count);
 }
 
-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)
+static int osd_ldiskfs_map_inode_pages(struct inode *inode,
+                                      struct osd_iobuf *iobuf,
+                                      struct osd_device *osd,
+                                      int create, __u64 user_size,
+                                      int check_credits)
 {
        int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
-       int rc = 0, i = 0;
+       int rc = 0, i = 0, mapped_index = 0;
        struct page *fp = NULL;
        int clen = 0;
        pgoff_t max_page_index;
        handle_t *handle = NULL;
+       int credits;
+       sector_t start_blocks = 0, count = 0;
+       loff_t disk_size = 0;
+       struct page **page = iobuf->dr_pages;
+       int pages = iobuf->dr_npages;
+       sector_t *blocks = iobuf->dr_blocks;
+       struct niobuf_local *lnb1, *lnb2;
+       loff_t size1, size2;
 
        max_page_index = inode->i_sb->s_maxbytes >> PAGE_SHIFT;
 
@@ -1239,11 +1036,18 @@ static int osd_ldiskfs_map_inode_pages(struct inode *inode, struct page **page,
                rc = osd_attach_jinode(inode);
                if (rc)
                        return rc;
+               disk_size = i_size_read(inode);
+               /* if disk_size is already bigger than specified user_size,
+                * ignore user_size
+                */
+               if (disk_size > user_size)
+                       user_size = 0;
        }
        /* pages are sorted already. so, we just have to find
-        * contig. space and process them properly */
+        * contig. space and process them properly
+        */
        while (i < pages) {
-               long blen, total = 0;
+               long blen, total = 0, previous_total = 0;
                struct ldiskfs_map_blocks map = { 0 };
 
                if (fp == NULL) { /* start new extent */
@@ -1264,37 +1068,89 @@ static int osd_ldiskfs_map_inode_pages(struct inode *inode, struct page **page,
                map.m_lblk = fp->index * blocks_per_page;
                map.m_len = blen = clen * blocks_per_page;
 cont_map:
+               /**
+                * We might restart transaction for block allocations,
+                * in order to make sure data ordered mode, issue IO, disk
+                * size update and block allocations need be within same
+                * transaction to make sure consistency.
+                */
+               if (handle && check_credits) {
+                       /*
+                        * credits to insert 1 extent into extent tree.
+                        */
+                       credits = osd_chunk_trans_blocks(inode, blen);
+                       rc = osd_extend_trans(handle, credits, inode);
+                       if (rc < 0)
+                               GOTO(cleanup, rc);
+                       /*
+                        * only issue IO if restart transaction needed,
+                        * as update disk size need hold inode lock, we
+                        * want to avoid that as much as possible.
+                        */
+                       if (rc > 0) {
+                               WARN_ON_ONCE(start_blocks == 0);
+                               rc = osd_ldiskfs_map_write(inode,
+                                       iobuf, osd, start_blocks,
+                                       count, &disk_size, user_size);
+                               if (rc)
+                                       GOTO(cleanup, rc);
+#ifdef HAVE_LDISKFS_JOURNAL_ENSURE_CREDITS
+                               rc = ldiskfs_journal_restart(handle, credits,
+                                       ldiskfs_trans_default_revoke_credits(inode->i_sb));
+#else
+                               rc = ldiskfs_journal_restart(handle, credits);
+#endif
+                               if (rc)
+                                       GOTO(cleanup, rc);
+                               start_blocks += count;
+                               /* reset IO block count */
+                               count = 0;
+                       }
+               }
                rc = ldiskfs_map_blocks(handle, inode, &map, create);
                if (rc >= 0) {
                        int c = 0;
+
                        for (; total < blen && c < map.m_len; c++, total++) {
                                if (rc == 0) {
                                        *(blocks + total) = 0;
                                        total++;
                                        break;
-                               } else {
-                                       *(blocks + total) = map.m_pblk + c;
-                                       /* unmap any possible underlying
-                                        * metadata from the block device
-                                        * mapping.  bug 6998. */
-                                       if ((map.m_flags & LDISKFS_MAP_NEW) &&
-                                           create)
-#ifndef HAVE_CLEAN_BDEV_ALIASES
-                                               unmap_underlying_metadata(
-                                                       inode->i_sb->s_bdev,
-                                                       map.m_pblk + c);
-#else
-                                               clean_bdev_aliases(
-                                                       inode->i_sb->s_bdev,
-                                                       map.m_pblk + c, 1);
-#endif
                                }
+                               *(blocks + total) = map.m_pblk + c;
+                               /* unmap any possible underlying
+                                * metadata from the block device
+                                * mapping.  b=6998.
+                                */
+                               if ((map.m_flags & LDISKFS_MAP_NEW) &&
+                                   create)
+                                       clean_bdev_aliases(inode->i_sb->s_bdev,
+                                                          map.m_pblk + c, 1);
                        }
                        rc = 0;
                }
+
+               if (rc == 0 && create) {
+                       count += (total - previous_total);
+                       mapped_index = (count + blocks_per_page -
+                                       1) / blocks_per_page - 1;
+                       lnb1 = iobuf->dr_lnbs[i - clen];
+                       lnb2 = iobuf->dr_lnbs[mapped_index];
+                       size1 = lnb1->lnb_file_offset -
+                               (lnb1->lnb_file_offset % PAGE_SIZE) +
+                               (total << inode->i_blkbits);
+                       size2 = lnb2->lnb_file_offset + lnb2->lnb_len;
+
+                       if (size1 > size2)
+                               size1 = size2;
+                       if (size1 > disk_size)
+                               disk_size = size1;
+               }
+
                if (rc == 0 && total < blen) {
                        map.m_lblk = fp->index * blocks_per_page + total;
                        map.m_len = blen - total;
+                       previous_total = total;
                        goto cont_map;
                }
                if (rc != 0)
@@ -1305,27 +1161,29 @@ cont_map:
                blocks += blocks_per_page * clen;
        }
 cleanup:
+       if (rc == 0 && create &&
+           start_blocks < pages * blocks_per_page) {
+               rc = osd_ldiskfs_map_write(inode, iobuf, osd, start_blocks,
+                                          count, &disk_size, user_size);
+               LASSERT(start_blocks + count == pages * blocks_per_page);
+       }
        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)
+                         struct niobuf_local *lnb, int npages)
 {
-        struct osd_thread_info *oti   = osd_oti_get(env);
-        struct osd_iobuf       *iobuf = &oti->oti_iobuf;
-        struct inode           *inode = osd_dt_obj(dt)->oo_inode;
-        struct osd_device      *osd   = osd_obj2dev(osd_dt_obj(dt));
-       ktime_t start;
-       ktime_t end;
+       struct osd_thread_info *oti   = osd_oti_get(env);
+       struct osd_iobuf       *iobuf = &oti->oti_iobuf;
+       struct inode           *inode = osd_dt_obj(dt)->oo_inode;
+       struct osd_device      *osd   = osd_obj2dev(osd_dt_obj(dt));
+       ktime_t start, end;
        s64 timediff;
-        ssize_t                 isize;
-        __s64                   maxidx;
-        int                     rc = 0;
-        int                     i;
-        int                     cache = 0;
+       ssize_t isize;
+       __s64  maxidx;
+       int i, rc = 0;
 
-        LASSERT(inode);
+       LASSERT(inode);
 
        rc = osd_init_iobuf(osd, iobuf, 0, npages);
        if (unlikely(rc != 0))
@@ -1334,18 +1192,9 @@ static int osd_write_prep(const struct lu_env *env, struct dt_object *dt,
        isize = i_size_read(inode);
        maxidx = ((isize + PAGE_SIZE - 1) >> PAGE_SHIFT) - 1;
 
-        if (osd->od_writethrough_cache)
-                cache = 1;
-        if (isize > osd->od_readcache_max_filesize)
-                cache = 0;
-
        start = ktime_get();
        for (i = 0; i < npages; i++) {
 
-               if (cache == 0)
-                       generic_error_remove_page(inode->i_mapping,
-                                                 lnb[i].lnb_page);
-
                /*
                 * till commit the content of the page is undefined
                 * we'll set it uptodate once bulk is done. otherwise
@@ -1376,17 +1225,16 @@ static int osd_write_prep(const struct lu_env *env, struct dt_object *dt,
        timediff = ktime_us_delta(end, start);
        lprocfs_counter_add(osd->od_stats, LPROC_OSD_GET_PAGE, timediff);
 
-        if (iobuf->dr_npages) {
-               rc = osd_ldiskfs_map_inode_pages(inode, iobuf->dr_pages,
-                                                iobuf->dr_npages,
-                                                iobuf->dr_blocks, 0);
-                if (likely(rc == 0)) {
-                        rc = osd_do_bio(osd, inode, iobuf);
-                        /* do IO stats for preparation reads */
-                        osd_fini_iobuf(osd, iobuf);
-                }
-        }
-        RETURN(rc);
+       if (iobuf->dr_npages) {
+               rc = osd_ldiskfs_map_inode_pages(inode, iobuf, osd, 0,
+                                                0, 0);
+               if (likely(rc == 0)) {
+                       rc = osd_do_bio(osd, inode, iobuf, 0, 0);
+                       /* do IO stats for preparation reads */
+                       osd_fini_iobuf(osd, iobuf);
+               }
+       }
+       RETURN(rc);
 }
 
 struct osd_fextent {
@@ -1420,7 +1268,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)
@@ -1443,42 +1291,31 @@ static int osd_is_mapped(struct dt_object *dt, __u64 offset,
 }
 
 static int osd_declare_write_commit(const struct lu_env *env,
-                                    struct dt_object *dt,
-                                    struct niobuf_local *lnb, int npages,
-                                    struct thandle *handle)
+                                   struct dt_object *dt,
+                                   struct niobuf_local *lnb, int npages,
+                                   struct thandle *handle)
 {
        const struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
        struct inode            *inode = osd_dt_obj(dt)->oo_inode;
        struct osd_thandle      *oh;
-       int                     extents = 1;
+       int                     extents = 0;
        int                     depth;
        int                     i;
-       int                     newblocks;
+       int                     newblocks = 0;
        int                     rc = 0;
-       int                     flags = 0;
        int                     credits = 0;
        long long               quota_space = 0;
-       struct osd_fextent      extent = { 0 };
+       struct osd_fextent      mapped = { 0 }, extent = { 0 };
+       enum osd_quota_local_flags local_flags = 0;
        enum osd_qid_declare_flags declare_flags = OSD_QID_BLK;
        ENTRY;
 
-        LASSERT(handle != NULL);
-        oh = container_of0(handle, struct osd_thandle, ot_super);
-        LASSERT(oh->ot_handle == NULL);
-
-        newblocks = npages;
+       LASSERT(handle != NULL);
+       oh = container_of(handle, struct osd_thandle, ot_super);
+       LASSERT(oh->ot_handle == NULL);
 
-        /* calculate number of extents (probably better to pass nb) */
+       /* calculate number of extents (probably better to pass nb) */
        for (i = 0; i < npages; i++) {
-               if (i && lnb[i].lnb_file_offset !=
-                   lnb[i - 1].lnb_file_offset + lnb[i - 1].lnb_len)
-                       extents++;
-
-               if (osd_is_mapped(dt, lnb[i].lnb_file_offset, &extent))
-                       lnb[i].lnb_flags |= OBD_BRW_MAPPED;
-               else
-                       quota_space += PAGE_SIZE;
-
                /* ignore quota for the whole request if any page is from
                 * client cache or written by root.
                 *
@@ -1487,55 +1324,85 @@ static int osd_declare_write_commit(const struct lu_env *env,
                 * !(lnb[i].flags & OBD_BRW_SYNC)
                 *
                 * XXX we could handle this on per-lnb basis as done by
-                * grant. */
+                * grant.
+                */
                if ((lnb[i].lnb_flags & OBD_BRW_NOQUOTA) ||
                    (lnb[i].lnb_flags & (OBD_BRW_FROM_GRANT | OBD_BRW_SYNC)) ==
                    OBD_BRW_FROM_GRANT)
                        declare_flags |= OSD_QID_FORCE;
+
+               if (osd_is_mapped(dt, lnb[i].lnb_file_offset, &mapped)) {
+                       lnb[i].lnb_flags |= OBD_BRW_MAPPED;
+                       continue;
+               }
+
+               /* count only unmapped changes */
+               newblocks++;
+               if (lnb[i].lnb_file_offset != extent.end || extent.end == 0) {
+                       extents++;
+                       extent.end = lnb[i].lnb_file_offset + lnb[i].lnb_len;
+               } else {
+                       extent.end += lnb[i].lnb_len;
+               }
+
+               quota_space += PAGE_SIZE;
        }
 
-        /*
-         * each extent can go into new leaf causing a split
-         * 5 is max tree depth: inode + 4 index blocks
-         * with blockmaps, depth is 3 at most
-         */
-        if (LDISKFS_I(inode)->i_flags & LDISKFS_EXTENTS_FL) {
-                /*
-                 * many concurrent threads may grow tree by the time
-                 * our transaction starts. so, consider 2 is a min depth
-                 */
-                depth = ext_depth(inode);
-                depth = max(depth, 1) + 1;
-                newblocks += depth;
-               credits++; /* inode */
+       credits++; /* inode */
+       /*
+        * overwrite case, no need to modify tree and
+        * allocate blocks.
+        */
+       if (!newblocks)
+               goto out_declare;
+       /*
+        * each extent can go into new leaf causing a split
+        * 5 is max tree depth: inode + 4 index blocks
+        * with blockmaps, depth is 3 at most
+        */
+       if (LDISKFS_I(inode)->i_flags & LDISKFS_EXTENTS_FL) {
+               /*
+                * many concurrent threads may grow tree by the time
+                * our transaction starts. so, consider 2 is a min depth
+                */
+               depth = ext_depth(inode);
+               depth = max(depth, 1) + 1;
+               newblocks += depth;
                credits += depth * 2 * extents;
        } else {
                depth = 3;
                newblocks += depth;
-               credits++; /* inode */
                credits += depth * extents;
        }
 
+       /*
+        * try a bit more extents to avoid restart
+        * as much as possible in normal case.
+        */
+       if (npages > 1 && extents)
+               extents <<= 1;
+
        /* quota space for metadata blocks */
        quota_space += depth * extents * LDISKFS_BLOCK_SIZE(osd_sb(osd));
 
        /* quota space should be reported in 1K blocks */
        quota_space = toqb(quota_space);
 
-        /* each new block can go in different group (bitmap + gd) */
+       /* each new block can go in different group (bitmap + gd) */
 
-        /* we can't dirty more bitmap blocks than exist */
-        if (newblocks > LDISKFS_SB(osd_sb(osd))->s_groups_count)
+       /* we can't dirty more bitmap blocks than exist */
+       if (extents > LDISKFS_SB(osd_sb(osd))->s_groups_count)
                credits += LDISKFS_SB(osd_sb(osd))->s_groups_count;
-        else
-               credits += newblocks;
+       else
+               credits += extents;
 
        /* we can't dirty more gd blocks than exist */
-       if (newblocks > LDISKFS_SB(osd_sb(osd))->s_gdb_count)
+       if (extents > LDISKFS_SB(osd_sb(osd))->s_gdb_count)
                credits += LDISKFS_SB(osd_sb(osd))->s_gdb_count;
        else
-               credits += newblocks;
+               credits += extents;
 
+out_declare:
        osd_trans_declare_op(env, oh, OSD_OT_WRITE, credits);
 
        /* make sure the over quota flags were not set */
@@ -1543,16 +1410,17 @@ static int osd_declare_write_commit(const struct lu_env *env,
 
        rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
                                   i_projid_read(inode), quota_space, oh,
-                                  osd_dt_obj(dt), &flags, declare_flags);
+                                  osd_dt_obj(dt), &local_flags, declare_flags);
 
        /* we need only to store the overquota flags in the first lnb for
         * now, once we support multiple objects BRW, this code needs be
-        * revised. */
-       if (flags & QUOTA_FL_OVER_USRQUOTA)
+        * revised.
+        */
+       if (local_flags & QUOTA_FL_OVER_USRQUOTA)
                lnb[0].lnb_flags |= OBD_BRW_OVER_USRQUOTA;
-       if (flags & QUOTA_FL_OVER_GRPQUOTA)
+       if (local_flags & QUOTA_FL_OVER_GRPQUOTA)
                lnb[0].lnb_flags |= OBD_BRW_OVER_GRPQUOTA;
-       if (flags & QUOTA_FL_OVER_PRJQUOTA)
+       if (local_flags & QUOTA_FL_OVER_PRJQUOTA)
                lnb[0].lnb_flags |= OBD_BRW_OVER_PRJQUOTA;
 
        if (rc == 0)
@@ -1563,30 +1431,32 @@ static int osd_declare_write_commit(const struct lu_env *env,
 
 /* Check if a block is allocated or not */
 static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
-                            struct niobuf_local *lnb, int npages,
-                            struct thandle *thandle)
+                           struct niobuf_local *lnb, int npages,
+                           struct thandle *thandle, __u64 user_size)
 {
-        struct osd_thread_info *oti = osd_oti_get(env);
-        struct osd_iobuf *iobuf = &oti->oti_iobuf;
-        struct inode *inode = osd_dt_obj(dt)->oo_inode;
-        struct osd_device  *osd = osd_obj2dev(osd_dt_obj(dt));
-        loff_t isize;
-        int rc = 0, i;
+       struct osd_thread_info *oti = osd_oti_get(env);
+       struct osd_iobuf *iobuf = &oti->oti_iobuf;
+       struct inode *inode = osd_dt_obj(dt)->oo_inode;
+       struct osd_device  *osd = osd_obj2dev(osd_dt_obj(dt));
+       int rc = 0, i, check_credits = 0;
+       struct osd_thandle *oh = container_of(thandle,
+                                             struct osd_thandle, ot_super);
+       unsigned int save_credits = oh->ot_credits;
 
-        LASSERT(inode);
+       LASSERT(inode);
 
        rc = osd_init_iobuf(osd, iobuf, 1, npages);
        if (unlikely(rc != 0))
                RETURN(rc);
 
-       isize = i_size_read(inode);
-       ll_vfs_dq_init(inode);
+       dquot_initialize(inode);
 
-        for (i = 0; i < npages; i++) {
+       for (i = 0; i < npages; i++) {
                if (lnb[i].lnb_rc == -ENOSPC &&
                    (lnb[i].lnb_flags & OBD_BRW_MAPPED)) {
                        /* Allow the write to proceed if overwriting an
-                        * existing block */
+                        * existing block
+                        */
                        lnb[i].lnb_rc = 0;
                }
 
@@ -1599,12 +1469,12 @@ static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
                        continue;
                }
 
+               if (!(lnb[i].lnb_flags & OBD_BRW_MAPPED))
+                       check_credits = 1;
+
                LASSERT(PageLocked(lnb[i].lnb_page));
                LASSERT(!PageWriteback(lnb[i].lnb_page));
 
-               if (lnb[i].lnb_file_offset + lnb[i].lnb_len > isize)
-                       isize = lnb[i].lnb_file_offset + lnb[i].lnb_len;
-
                /*
                 * Since write and truncate are serialized by oo_sem, even
                 * partial-page truncate should not leave dirty pages in the
@@ -1615,39 +1485,35 @@ static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
                SetPageUptodate(lnb[i].lnb_page);
 
                osd_iobuf_add_page(iobuf, &lnb[i]);
-        }
+       }
 
        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) {
-               rc = osd_ldiskfs_map_inode_pages(inode, iobuf->dr_pages,
-                                                iobuf->dr_npages,
-                                                iobuf->dr_blocks, 1);
-        } else {
-                /* no pages to write, no transno is needed */
-                thandle->th_local = 1;
-        }
-
-       if (likely(rc == 0)) {
-               spin_lock(&inode->i_lock);
-               if (isize > i_size_read(inode)) {
-                       i_size_write(inode, isize);
-                       LDISKFS_I(inode)->i_disksize = isize;
-                       spin_unlock(&inode->i_lock);
-                       ll_dirty_inode(inode, I_DIRTY_DATASYNC);
-               } else {
-                       spin_unlock(&inode->i_lock);
-               }
+       if (OBD_FAIL_CHECK(OBD_FAIL_OST_MAPBLK_ENOSPC)) {
+               rc = -ENOSPC;
+       } else if (iobuf->dr_npages > 0) {
+               rc = osd_ldiskfs_map_inode_pages(inode, iobuf, osd,
+                                                1, user_size,
+                                                check_credits);
+               /*
+                * Write might restart transaction, extend credits
+                * if needed for operations such as attribute set.
+                */
+               if (rc == 0) {
+                       handle_t *handle = ldiskfs_journal_current_handle();
 
-               rc = osd_do_bio(osd, inode, iobuf);
-               /* we don't do stats here as in read path because
-                * write is async: we'll do this in osd_put_bufs() */
+                       LASSERT(handle != NULL);
+                       rc = osd_extend_restart_trans(handle, save_credits,
+                                                     inode);
+               }
        } else {
-               osd_fini_iobuf(osd, iobuf);
+               /* no pages to write, no transno is needed */
+               thandle->th_local = 1;
        }
 
+       if (rc != 0)
+               osd_fini_iobuf(osd, iobuf);
+
        osd_trans_exec_check(env, thandle, OSD_OT_WRITE);
 
        if (unlikely(rc != 0)) {
@@ -1655,9 +1521,11 @@ static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
                for (i = 0; i < npages; i++) {
                        if (lnb[i].lnb_page == NULL)
                                continue;
-                       LASSERT(PageLocked(lnb[i].lnb_page));
-                       generic_error_remove_page(inode->i_mapping,
-                                                 lnb[i].lnb_page);
+                       if (!PagePrivate2(lnb[i].lnb_page)) {
+                               LASSERT(PageLocked(lnb[i].lnb_page));
+                               generic_error_remove_page(inode->i_mapping,
+                                                         lnb[i].lnb_page);
+                       }
                }
        }
 
@@ -1665,18 +1533,18 @@ static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
 }
 
 static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
-                         struct niobuf_local *lnb, int npages)
+                        struct niobuf_local *lnb, int npages)
 {
-        struct osd_thread_info *oti = osd_oti_get(env);
-        struct osd_iobuf *iobuf = &oti->oti_iobuf;
-        struct inode *inode = osd_dt_obj(dt)->oo_inode;
-        struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
-       int rc = 0, i, cache = 0, cache_hits = 0, cache_misses = 0;
+       struct osd_thread_info *oti = osd_oti_get(env);
+       struct osd_iobuf *iobuf = &oti->oti_iobuf;
+       struct inode *inode = osd_dt_obj(dt)->oo_inode;
+       struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
+       int rc = 0, i, cache_hits = 0, cache_misses = 0;
        ktime_t start, end;
        s64 timediff;
        loff_t isize;
 
-        LASSERT(inode);
+       LASSERT(inode);
 
        rc = osd_init_iobuf(osd, iobuf, 0, npages);
        if (unlikely(rc != 0))
@@ -1684,23 +1552,19 @@ static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
 
        isize = i_size_read(inode);
 
-       if (osd->od_read_cache)
-               cache = 1;
-       if (isize > osd->od_readcache_max_filesize)
-               cache = 0;
-
        start = ktime_get();
        for (i = 0; i < npages; i++) {
 
                if (isize <= lnb[i].lnb_file_offset)
                        /* If there's no more data, abort early.
-                        * lnb->lnb_rc == 0, so it's easy to detect later. */
+                        * lnb->lnb_rc == 0, so it's easy to detect later.
+                        */
                        break;
 
-               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;
+               /* instead of looking if we go beyong isize, send complete
+                * pages all the time
+                */
+               lnb[i].lnb_rc = lnb[i].lnb_len;
 
                /* Bypass disk read if fail_loc is set properly */
                if (OBD_FAIL_CHECK(OBD_FAIL_OST_FAKE_RW))
@@ -1708,14 +1572,18 @@ static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
 
                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);
@@ -1731,16 +1599,23 @@ 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) {
-               rc = osd_ldiskfs_map_inode_pages(inode, iobuf->dr_pages,
-                                                iobuf->dr_npages,
-                                                iobuf->dr_blocks, 0);
-                rc = osd_do_bio(osd, inode, iobuf);
+       if (iobuf->dr_npages) {
+               rc = osd_ldiskfs_map_inode_pages(inode, iobuf, osd, 0,
+                                                0, 0);
+               if (!rc)
+                       rc = osd_do_bio(osd, inode, iobuf, 0, 0);
+
+               /* IO stats will be done in osd_bufs_put() */
 
-                /* IO stats will be done in osd_bufs_put() */
-        }
+               /* early release to let others read data during the bulk */
+               for (i = 0; i < iobuf->dr_npages; i++) {
+                       LASSERT(PageLocked(iobuf->dr_pages[i]));
+                       if (!PagePrivate2(iobuf->dr_pages[i]))
+                               unlock_page(iobuf->dr_pages[i]);
+               }
+       }
 
-        RETURN(rc);
+       RETURN(rc);
 }
 
 /*
@@ -1756,29 +1631,31 @@ static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
  */
 static int osd_ldiskfs_readlink(struct inode *inode, char *buffer, int buflen)
 {
-        struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
+       struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
 
-        memcpy(buffer, (char *)ei->i_data, buflen);
+       memcpy(buffer, (char *)ei->i_data, buflen);
 
-        return  buflen;
+       return  buflen;
 }
 
 int osd_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs)
 {
-        struct buffer_head *bh;
-        unsigned long block;
-        int osize;
-        int blocksize;
-        int csize;
-        int boffs;
-
-        /* prevent reading after eof */
+       struct buffer_head *bh;
+       unsigned long block;
+       int osize;
+       int blocksize;
+       int csize;
+       int boffs;
+
+       /* prevent reading after eof */
        spin_lock(&inode->i_lock);
        if (i_size_read(inode) < *offs + size) {
                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) {
@@ -1790,17 +1667,16 @@ int osd_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs)
                spin_unlock(&inode->i_lock);
        }
 
-        blocksize = 1 << inode->i_blkbits;
-        osize = size;
-        while (size > 0) {
-                block = *offs >> inode->i_blkbits;
-                boffs = *offs & (blocksize - 1);
-                csize = min(blocksize - boffs, size);
+       blocksize = 1 << inode->i_blkbits;
+       osize = size;
+       while (size > 0) {
+               block = *offs >> inode->i_blkbits;
+               boffs = *offs & (blocksize - 1);
+               csize = min(blocksize - boffs, size);
                bh = __ldiskfs_bread(NULL, inode, block, 0);
                if (IS_ERR(bh)) {
-                       CERROR("%s: can't read %u@%llu on ino %lu: "
-                              "rc = %ld\n", osd_ino2name(inode),
-                              csize, *offs, inode->i_ino,
+                       CERROR("%s: can't read %u@%llu on ino %lu: rc = %ld\n",
+                              osd_ino2name(inode), csize, *offs, inode->i_ino,
                               PTR_ERR(bh));
                        return PTR_ERR(bh);
                }
@@ -1812,29 +1688,37 @@ int osd_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs)
                        memset(buf, 0, csize);
                }
 
-                *offs += csize;
-                buf += csize;
-                size -= csize;
-        }
-        return osize;
+               *offs += csize;
+               buf += csize;
+               size -= csize;
+       }
+       return osize;
 }
 
 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
                        struct lu_buf *buf, loff_t *pos)
 {
-        struct inode *inode = osd_dt_obj(dt)->oo_inode;
-        int           rc;
-
-        /* Read small symlink from inode body as we need to maintain correct
-         * on-disk symlinks for ldiskfs.
-         */
-        if (S_ISLNK(dt->do_lu.lo_header->loh_attr) &&
-            (buf->lb_len < sizeof(LDISKFS_I(inode)->i_data)))
-                rc = osd_ldiskfs_readlink(inode, buf->lb_buf, buf->lb_len);
-        else
-                rc = osd_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
-
-        return rc;
+       struct inode *inode = osd_dt_obj(dt)->oo_inode;
+       int rc;
+
+       /* Read small symlink from inode body as we need to maintain correct
+        * on-disk symlinks for ldiskfs.
+        */
+       if (S_ISLNK(dt->do_lu.lo_header->loh_attr)) {
+               loff_t size = i_size_read(inode);
+
+               if (buf->lb_len < size)
+                       return -EOVERFLOW;
+
+               if (size < sizeof(LDISKFS_I(inode)->i_data))
+                       rc = osd_ldiskfs_readlink(inode, buf->lb_buf, size);
+               else
+                       rc = osd_ldiskfs_read(inode, buf->lb_buf, size, pos);
+       } else {
+               rc = osd_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
+       }
+
+       return rc;
 }
 
 static inline int osd_extents_enabled(struct super_block *sb,
@@ -1860,7 +1744,8 @@ int osd_calc_bkmap_credits(struct super_block *sb, struct inode *inode,
 
        /* legacy blockmap: 3 levels * 3 (bitmap,gd,itself)
         * we do not expect blockmaps on the large files,
-        * so let's shrink it to 2 levels (4GB files) */
+        * so let's shrink it to 2 levels (4GB files)
+        */
 
        /* this is default reservation: 2 levels */
        credits = (blocks + 2) * 3;
@@ -1912,10 +1797,10 @@ static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
        ENTRY;
 
        LASSERT(buf != NULL);
-        LASSERT(handle != NULL);
+       LASSERT(handle != NULL);
 
-        oh = container_of0(handle, struct osd_thandle, ot_super);
-        LASSERT(oh->ot_handle == NULL);
+       oh = container_of(handle, struct osd_thandle, ot_super);
+       LASSERT(oh->ot_handle == NULL);
 
        size = buf->lb_len;
        bits = sb->s_blocksize_bits;
@@ -1923,7 +1808,8 @@ static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
 
        if (_pos == -1) {
                /* if this is an append, then we
-                * should expect cross-block record */
+                * should expect cross-block record
+                */
                pos = 0;
        } else {
                pos = _pos;
@@ -1958,7 +1844,8 @@ static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
                depth = max(depth, 1) + 1;
                credits = depth;
                /* if not append, then split may need to modify
-                * existing blocks moving entries into the new ones */
+                * existing blocks moving entries into the new ones
+                */
                if (_pos != -1)
                        credits += depth;
                /* blocks to store data: bitmap,gd,itself */
@@ -1967,7 +1854,8 @@ static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
                credits = osd_calc_bkmap_credits(sb, inode, size, _pos, blocks);
        }
        /* if inode is created as part of the transaction,
-        * then it's counted already by the creation method */
+        * then it's counted already by the creation method
+        */
        if (inode != NULL)
                credits++;
 
@@ -1977,7 +1865,8 @@ out:
 
        /* dt_declare_write() is usually called for system objects, such
         * as llog or last_rcvd files. We needn't enforce quota on those
-        * objects, so always set the lqi_space as 0. */
+        * objects, so always set the lqi_space as 0.
+        */
        if (inode != NULL)
                rc = osd_declare_inode_qid(env, i_uid_read(inode),
                                           i_gid_read(inode),
@@ -2000,23 +1889,27 @@ static int osd_ldiskfs_writelink(struct inode *inode, char *buffer, int buflen)
        LDISKFS_I(inode)->i_disksize = buflen;
        i_size_write(inode, buflen);
        spin_unlock(&inode->i_lock);
-       ll_dirty_inode(inode, I_DIRTY_DATASYNC);
+       osd_dirty_inode(inode, I_DIRTY_DATASYNC);
 
        return 0;
 }
 
-int osd_ldiskfs_write_record(struct inode *inode, void *buf, int bufsize,
-                            int write_NUL, loff_t *offs, handle_t *handle)
+static int osd_ldiskfs_write_record(struct dt_object *dt, void *buf,
+                                   int bufsize, int write_NUL, loff_t *offs,
+                                   handle_t *handle)
 {
-        struct buffer_head *bh        = NULL;
-        loff_t              offset    = *offs;
-        loff_t              new_size  = i_size_read(inode);
-        unsigned long       block;
-        int                 blocksize = 1 << inode->i_blkbits;
-        int                 err = 0;
-        int                 size;
-        int                 boffs;
-        int                 dirty_inode = 0;
+       struct inode *inode = osd_dt_obj(dt)->oo_inode;
+       struct buffer_head *bh        = NULL;
+       loff_t              offset    = *offs;
+       loff_t              new_size  = i_size_read(inode);
+       unsigned long       block;
+       int                 blocksize = 1 << inode->i_blkbits;
+       struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
+       int                 err = 0;
+       int                 size;
+       int                 boffs;
+       int                 dirty_inode = 0;
+       bool create, sparse, sync = false;
 
        if (write_NUL) {
                /*
@@ -2028,8 +1921,17 @@ int osd_ldiskfs_write_record(struct inode *inode, void *buf, int bufsize,
                ++bufsize;
        }
 
+       dirty_inode = test_and_set_bit(LDISKFS_INODE_JOURNAL_DATA,
+                                      &ei->i_flags);
+
+       /* 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;
+               unsigned long last_block = (new_size == 0) ? 0 :
+                                          (new_size - 1) >> inode->i_blkbits;
 
                if (bh)
                        brelse(bh);
@@ -2037,7 +1939,40 @@ 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",
+                             osd_ino2name(inode),
+                             offset, block, bufsize, *offs);
+
+               if (IS_ERR_OR_NULL(bh)) {
+                       struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
+                       int flags = LDISKFS_GET_BLOCKS_CREATE;
+
+                       /* while the file system is being mounted, avoid
+                        * preallocation otherwise mount can take a long
+                        * time as mballoc cache is cold.
+                        * XXX: this is a workaround until we have a proper
+                        *      fix in mballoc
+                        * XXX: works with extent-based files only */
+                       if (!osd->od_cl_seq)
+                               flags |= LDISKFS_GET_BLOCKS_NO_NORMALIZE;
+                       bh = __ldiskfs_bread(handle, inode, block, flags);
+                       create = true;
+               } else {
+                       if (sync) {
+                               up(&ei->i_append_sem);
+                               sync = false;
+                       }
+                       create = false;
+               }
                if (IS_ERR_OR_NULL(bh)) {
                        if (bh == NULL) {
                                err = -EIO;
@@ -2046,35 +1981,45 @@ int osd_ldiskfs_write_record(struct inode *inode, void *buf, int bufsize,
                                bh = NULL;
                        }
 
-                       CERROR("%s: error reading offset %llu (block %lu, "
-                              "size %d, offs %llu), credits %d/%d: rc = %d\n",
-                              inode->i_sb->s_id, offset, block, bufsize, *offs,
-                              credits, handle->h_buffer_credits, err);
-                        break;
-                }
-
-                err = ldiskfs_journal_get_write_access(handle, bh);
-                if (err) {
-                        CERROR("journal_get_write_access() returned error %d\n",
-                               err);
-                        break;
-                }
+                       CERROR(
+                              "%s: error reading offset %llu (block %lu, size %d, offs %llu), credits %d/%d: rc = %d\n",
+                              osd_ino2name(inode), offset, block, bufsize,
+                              *offs, credits, handle->h_buffer_credits, err);
+                       break;
+               }
+
+               err = ldiskfs_journal_get_write_access(handle, bh);
+               if (err) {
+                       CERROR("journal_get_write_access() returned error %d\n",
+                              err);
+                       break;
+               }
                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);
+                               sync = false;
+                       }
+               }
+               memcpy(bh->b_data + boffs, buf, size);
                err = ldiskfs_handle_dirty_metadata(handle, NULL, bh);
-                if (err)
-                        break;
-
-                if (offset + size > new_size)
-                        new_size = offset + size;
-                offset += size;
-                bufsize -= size;
-                buf += size;
-        }
-        if (bh)
-                brelse(bh);
+               if (err)
+                       break;
+
+               if (offset + size > new_size)
+                       new_size = offset + size;
+               offset += size;
+               bufsize -= size;
+               buf += size;
+       }
+       if (sync)
+               up(&ei->i_append_sem);
+
+       if (bh)
+               brelse(bh);
 
        if (write_NUL)
                --new_size;
@@ -2083,18 +2028,18 @@ int osd_ldiskfs_write_record(struct inode *inode, void *buf, int bufsize,
                spin_lock(&inode->i_lock);
                if (new_size > i_size_read(inode))
                        i_size_write(inode, new_size);
-               if (i_size_read(inode) > LDISKFS_I(inode)->i_disksize) {
-                       LDISKFS_I(inode)->i_disksize = i_size_read(inode);
+               if (i_size_read(inode) > ei->i_disksize) {
+                       ei->i_disksize = i_size_read(inode);
                        dirty_inode = 1;
                }
                spin_unlock(&inode->i_lock);
-               if (dirty_inode)
-                       ll_dirty_inode(inode, I_DIRTY_DATASYNC);
-        }
+       }
+       if (dirty_inode)
+               osd_dirty_inode(inode, I_DIRTY_DATASYNC);
 
-        if (err == 0)
-                *offs = offset;
-        return err;
+       if (err == 0)
+               *offs = offset;
+       return err;
 }
 
 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
@@ -2106,17 +2051,17 @@ static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
        ssize_t                 result;
        int                     is_link;
 
-        LASSERT(dt_object_exists(dt));
+       LASSERT(dt_object_exists(dt));
 
-        LASSERT(handle != NULL);
+       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 */
+       /* XXX: don't check: one declared chunk can be used many times */
        /* osd_trans_exec_op(env, handle, OSD_OT_WRITE); */
 
-        oh = container_of(handle, struct osd_thandle, ot_super);
-        LASSERT(oh->ot_handle->h_transaction != NULL);
+       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
@@ -2128,9 +2073,8 @@ static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
        if (is_link && (buf->lb_len < sizeof(LDISKFS_I(inode)->i_data)))
                result = osd_ldiskfs_writelink(inode, buf->lb_buf, buf->lb_len);
        else
-               result = osd_ldiskfs_write_record(inode, buf->lb_buf,
-                                                 buf->lb_len, is_link, pos,
-                                                 oh->ot_handle);
+               result = osd_ldiskfs_write_record(dt, buf->lb_buf, buf->lb_len,
+                                                 is_link, pos, oh->ot_handle);
        if (result == 0)
                result = buf->lb_len;
 
@@ -2139,25 +2083,192 @@ static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
        return result;
 }
 
+static int osd_declare_fallocate(const struct lu_env *env,
+                                struct dt_object *dt, __u64 start, __u64 end,
+                                int mode, struct thandle *th)
+{
+       struct osd_thandle *oh = container_of(th, struct osd_thandle, ot_super);
+       struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
+       struct inode *inode = osd_dt_obj(dt)->oo_inode;
+       long long quota_space = 0;
+       /* 5 is max tree depth. (inode + 4 index blocks) */
+       int depth = 5;
+       int rc;
+
+       ENTRY;
+
+       /*
+        * Only mode == 0 (which is standard prealloc) is supported now.
+        * Rest of mode options is not supported yet.
+        */
+       if (mode & ~FALLOC_FL_KEEP_SIZE)
+               RETURN(-EOPNOTSUPP);
+
+       LASSERT(th);
+       LASSERT(inode);
+
+       /* quota space for metadata blocks
+        * approximate metadata estimate should be good enough.
+        */
+       quota_space += PAGE_SIZE;
+       quota_space += depth * LDISKFS_BLOCK_SIZE(osd_sb(osd));
+
+       /* quota space should be reported in 1K blocks */
+       quota_space = toqb(quota_space) + toqb(end - start) +
+                     LDISKFS_META_TRANS_BLOCKS(inode->i_sb);
+
+       /* We don't need to reserve credits for whole fallocate here.
+        * We reserve space only for metadata. Fallocate credits are
+        * extended as required
+        */
+       rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
+                                  i_projid_read(inode), quota_space, oh,
+                                  osd_dt_obj(dt), NULL, OSD_QID_BLK);
+       RETURN(rc);
+}
+
+static int osd_fallocate(const struct lu_env *env, struct dt_object *dt,
+                        __u64 start, __u64 end, int mode, struct thandle *th)
+{
+       struct osd_thandle *oh = container_of(th, struct osd_thandle, ot_super);
+       handle_t *handle = ldiskfs_journal_current_handle();
+       unsigned int save_credits = oh->ot_credits;
+       struct osd_object *obj = osd_dt_obj(dt);
+       struct inode *inode = obj->oo_inode;
+       struct ldiskfs_map_blocks map;
+       unsigned int credits;
+       ldiskfs_lblk_t blen;
+       ldiskfs_lblk_t boff;
+       loff_t new_size = 0;
+       int depth = 0;
+       int flags;
+       int rc = 0;
+
+       ENTRY;
+
+       LASSERT(dt_object_exists(dt));
+       LASSERT(osd_invariant(obj));
+       LASSERT(inode != NULL);
+
+       CDEBUG(D_INODE, "fallocate: inode #%lu: start %llu end %llu mode %d\n",
+              inode->i_ino, start, end, mode);
+
+       dquot_initialize(inode);
+
+       LASSERT(th);
+
+       boff = start >> inode->i_blkbits;
+       blen = (ALIGN(end, 1 << inode->i_blkbits) >> inode->i_blkbits) - boff;
+
+       flags = LDISKFS_GET_BLOCKS_CREATE;
+       if (mode & FALLOC_FL_KEEP_SIZE)
+               flags |= LDISKFS_GET_BLOCKS_KEEP_SIZE;
+
+       inode_lock(inode);
+
+       /*
+        * We only support preallocation for extent-based file only.
+        */
+       if (!(ldiskfs_test_inode_flag(inode, LDISKFS_INODE_EXTENTS)))
+               GOTO(out, rc = -EOPNOTSUPP);
+
+       if (!(mode & FALLOC_FL_KEEP_SIZE) && (end > i_size_read(inode) ||
+           end > LDISKFS_I(inode)->i_disksize)) {
+               new_size = end;
+               rc = inode_newsize_ok(inode, new_size);
+               if (rc)
+                       GOTO(out, rc);
+       }
+
+       inode_dio_wait(inode);
+
+       map.m_lblk = boff;
+       map.m_len = blen;
+
+       /* Don't normalize the request if it can fit in one extent so
+        * that it doesn't get unnecessarily split into multiple extents.
+        */
+       if (blen <= EXT_UNWRITTEN_MAX_LEN)
+               flags |= LDISKFS_GET_BLOCKS_NO_NORMALIZE;
+
+       /*
+        * credits to insert 1 extent into extent tree.
+        */
+       credits = osd_chunk_trans_blocks(inode, blen);
+       depth = ext_depth(inode);
+
+       while (rc >= 0 && blen) {
+               loff_t epos;
+
+               /*
+                * Recalculate credits when extent tree depth changes.
+                */
+               if (depth != ext_depth(inode)) {
+                       credits = osd_chunk_trans_blocks(inode, blen);
+                       depth = ext_depth(inode);
+               }
+
+               /* TODO: quota check */
+               rc = osd_extend_restart_trans(handle, credits, inode);
+               if (rc)
+                       break;
+
+               rc = ldiskfs_map_blocks(handle, inode, &map, flags);
+               if (rc <= 0) {
+                       CDEBUG(D_INODE,
+                              "inode #%lu: block %u: len %u: ldiskfs_map_blocks returned %d\n",
+                              inode->i_ino, map.m_lblk, map.m_len, rc);
+                       ldiskfs_mark_inode_dirty(handle, inode);
+                       break;
+               }
+
+               map.m_lblk += rc;
+               map.m_len = blen = blen - rc;
+               epos = (loff_t)map.m_lblk << inode->i_blkbits;
+               inode->i_ctime = current_time(inode);
+               if (new_size) {
+                       if (epos > end)
+                               epos = end;
+                       if (ldiskfs_update_inode_size(inode, epos) & 0x1)
+                               inode->i_mtime = inode->i_ctime;
+               } else {
+                       if (epos > inode->i_size)
+                               ldiskfs_set_inode_flag(inode,
+                                                      LDISKFS_INODE_EOFBLOCKS);
+               }
+
+               ldiskfs_mark_inode_dirty(handle, inode);
+       }
+
+out:
+       /* extand credits if needed for operations such as attribute set */
+       if (rc >= 0)
+               rc = osd_extend_restart_trans(handle, save_credits, inode);
+
+       inode_unlock(inode);
+
+       RETURN(rc);
+}
+
 static int osd_declare_punch(const struct lu_env *env, struct dt_object *dt,
-                             __u64 start, __u64 end, struct thandle *th)
+                            __u64 start, __u64 end, struct thandle *th)
 {
-        struct osd_thandle *oh;
+       struct osd_thandle *oh;
        struct inode       *inode;
        int                 rc;
-        ENTRY;
-
-        LASSERT(th);
-        oh = container_of(th, struct osd_thandle, ot_super);
-
-        /*
-         * we don't need to reserve credits for whole truncate
-         * it's not possible as truncate may need to free too many
-         * blocks and that won't fit a single transaction. instead
-         * we reserve credits to change i_size and put inode onto
-         * orphan list. if needed truncate will extend or restart
-         * transaction
-         */
+       ENTRY;
+
+       LASSERT(th);
+       oh = container_of(th, struct osd_thandle, ot_super);
+
+       /*
+        * we don't need to reserve credits for whole truncate
+        * it's not possible as truncate may need to free too many
+        * blocks and that won't fit a single transaction. instead
+        * we reserve credits to change i_size and put inode onto
+        * orphan list. if needed truncate will extend or restart
+        * transaction
+        */
        osd_trans_declare_op(env, oh, OSD_OT_PUNCH,
                             osd_dto_credits_noquota[DTO_ATTR_SET_BASE] + 3);
 
@@ -2186,11 +2297,10 @@ static int osd_punch(const struct lu_env *env, struct dt_object *dt,
        bool grow = false;
        ENTRY;
 
-       LASSERT(end == OBD_OBJECT_EOF);
        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);
@@ -2202,7 +2312,8 @@ static int osd_punch(const struct lu_env *env, struct dt_object *dt,
         * and then get truncate RPC which essentially
         * would be skipped. this is bad.. so, disable
         * this optimization on MDS till the client stop
-        * to sent MDS_REINT (LU-11033) -bzzz */
+        * to sent MDS_REINT (LU-11033) -bzzz
+        */
        if (osd->od_is_ost && i_size_read(inode) == start)
                RETURN(0);
 
@@ -2213,6 +2324,14 @@ static int osd_punch(const struct lu_env *env, struct dt_object *dt,
                grow = true;
        i_size_write(inode, start);
        spin_unlock(&inode->i_lock);
+       /* if object holds encrypted content, we need to make sure we truncate
+        * on an encryption unit boundary, or subsequent reads will get
+        * corrupted content
+        */
+       if (obj->oo_lma_flags & LUSTRE_ENCRYPT_FL &&
+           start & ~LUSTRE_ENCRYPTION_MASK)
+               start = (start & LUSTRE_ENCRYPTION_MASK) +
+                       LUSTRE_ENCRYPTION_UNIT_SIZE;
        ll_truncate_pagecache(inode, start);
 
        /* optimize grow case */
@@ -2221,10 +2340,13 @@ static int osd_punch(const struct lu_env *env, struct dt_object *dt,
                GOTO(out, rc);
        }
 
+       inode_lock(inode);
        /* add to orphan list to ensure truncate completion
         * if this transaction succeed. ldiskfs_truncate()
-        * will take the inode out of the list */
+        * will take the inode out of the list
+        */
        rc = ldiskfs_orphan_add(oh->ot_handle, inode);
+       inode_unlock(inode);
        if (rc != 0)
                GOTO(out, rc);
 
@@ -2280,7 +2402,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)
@@ -2300,27 +2422,34 @@ 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;
 }
 
 static int osd_ladvise(const struct lu_env *env, struct dt_object *dt,
                       __u64 start, __u64 end, enum lu_ladvise_type advice)
 {
-       int              rc = 0;
-       struct inode    *inode = osd_dt_obj(dt)->oo_inode;
+       struct osd_object *obj = osd_dt_obj(dt);
+       int rc = 0;
        ENTRY;
 
        switch (advice) {
        case LU_LADVISE_DONTNEED:
-               if (end == 0)
-                       break;
-               invalidate_mapping_pages(inode->i_mapping,
-                                        start >> PAGE_SHIFT,
-                                        (end - 1) >> PAGE_SHIFT);
+               if (end)
+                       invalidate_mapping_pages(obj->oo_inode->i_mapping,
+                                                start >> PAGE_SHIFT,
+                                                (end - 1) >> PAGE_SHIFT);
                break;
        default:
                rc = -ENOTSUPP;
@@ -2330,12 +2459,43 @@ static int osd_ladvise(const struct lu_env *env, struct dt_object *dt,
        RETURN(rc);
 }
 
+static loff_t osd_lseek(const struct lu_env *env, struct dt_object *dt,
+                       loff_t offset, int whence)
+{
+       struct osd_object *obj = osd_dt_obj(dt);
+       struct inode *inode = obj->oo_inode;
+       struct file *file;
+       loff_t result;
+
+       ENTRY;
+
+       LASSERT(dt_object_exists(dt));
+       LASSERT(osd_invariant(obj));
+       LASSERT(inode);
+       LASSERT(offset >= 0);
+
+       file = osd_quasi_file(env, inode);
+       result = file->f_op->llseek(file, offset, whence);
+
+       /*
+        * If 'offset' is beyond end of object file then treat it as not error
+        * but valid case for SEEK_HOLE and return 'offset' as result.
+        * LOV will decide if it is beyond real end of file or not.
+        */
+       if (whence == SEEK_HOLE && result == -ENXIO)
+               result = offset;
+
+       CDEBUG(D_INFO, "seek %s from %lld: %lld\n", whence == SEEK_HOLE ?
+                      "hole" : "data", offset, result);
+       RETURN(result);
+}
+
 /*
  * in some cases we may need declare methods for objects being created
  * e.g., when we create symlink
  */
 const struct dt_body_operations osd_body_ops_new = {
-        .dbo_declare_write = osd_declare_write,
+       .dbo_declare_write = osd_declare_write,
 };
 
 const struct dt_body_operations osd_body_ops = {
@@ -2352,6 +2512,9 @@ const struct dt_body_operations osd_body_ops = {
        .dbo_punch                      = osd_punch,
        .dbo_fiemap_get                 = osd_fiemap_get,
        .dbo_ladvise                    = osd_ladvise,
+       .dbo_declare_fallocate          = osd_declare_fallocate,
+       .dbo_fallocate                  = osd_fallocate,
+       .dbo_lseek                      = osd_lseek,
 };
 
 /**
@@ -2396,20 +2559,23 @@ int osd_trunc_lock(struct osd_object *obj, struct osd_thandle *oh, bool shared)
        else
                down_write(&obj->oo_ext_idx_sem);
        al->tl_shared = shared;
+       lu_object_get(&obj->oo_dt.do_lu);
 
        list_add(&al->tl_list, &oh->ot_trunc_locks);
 
        return 0;
 }
 
-void osd_trunc_unlock_all(struct list_head *list)
+void osd_trunc_unlock_all(const struct lu_env *env, struct list_head *list)
 {
        struct osd_access_lock *al, *tmp;
+
        list_for_each_entry_safe(al, tmp, list, tl_list) {
                if (al->tl_shared)
                        up_read(&al->tl_obj->oo_ext_idx_sem);
                else
                        up_write(&al->tl_obj->oo_ext_idx_sem);
+               osd_object_put(env, al->tl_obj);
                list_del(&al->tl_list);
                OBD_FREE_PTR(al);
        }
@@ -2432,18 +2598,29 @@ void osd_execute_truncate(struct osd_object *obj)
                return;
        }
 
-#ifdef HAVE_INODEOPS_TRUNCATE
-       if (inode->i_op->truncate)
-               inode->i_op->truncate(inode);
-       else
-#endif
-               ldiskfs_truncate(inode);
+       size = i_size_read(inode);
+       inode_lock(inode);
+       /* if object holds encrypted content, we need to make sure we truncate
+        * on an encryption unit boundary, or block content will get corrupted
+        */
+       if (obj->oo_lma_flags & LUSTRE_ENCRYPT_FL &&
+           size & ~LUSTRE_ENCRYPTION_MASK)
+               inode->i_size = (size & LUSTRE_ENCRYPTION_MASK) +
+                       LUSTRE_ENCRYPTION_UNIT_SIZE;
+       ldiskfs_truncate(inode);
+       inode_unlock(inode);
+       if (inode->i_size != size) {
+               spin_lock(&inode->i_lock);
+               i_size_write(inode, size);
+               LDISKFS_I(inode)->i_disksize = size;
+               spin_unlock(&inode->i_lock);
+               osd_dirty_inode(inode, I_DIRTY_DATASYNC);
+       }
 
        /*
         * For a partial-page truncate, flush the page to disk immediately to
         * avoid data corruption during direct disk write.  b=17397
         */
-       size = i_size_read(inode);
        if ((size & ~PAGE_MASK) == 0)
                return;
        if (osd_use_page_cache(d)) {