Whamcloud - gitweb
LU-6142 libcfs: discard PO2_ROUNDUP_TYPED, LOWEST_BIT_SET
[fs/lustre-release.git] / lustre / osd-zfs / osd_io.c
index eda81fa..dd26c0a 100644 (file)
 #include <sys/sa_impl.h>
 #include <sys/txg.h>
 
-static char *osd_0copy_tag = "zerocopy";
+static char osd_0copy_tag[] = "zerocopy";
 
+static void dbuf_set_pending_evict(dmu_buf_t *db)
+{
+       dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
+       dbi->db_pending_evict = TRUE;
+}
 
 static void record_start_io(struct osd_device *osd, int rw, int discont_pages)
 {
@@ -88,64 +93,76 @@ static void record_end_io(struct osd_device *osd, int rw,
 {
        struct obd_histogram *h = osd->od_brw_stats.hist;
 
-       if (rw == READ) {
+       if (rw == READ)
                atomic_dec(&osd->od_r_in_flight);
-               lprocfs_oh_tally_log2(&h[BRW_R_PAGES], npages);
-               if (disksize > 0)
-                       lprocfs_oh_tally_log2(&h[BRW_R_DISK_IOSIZE], disksize);
-               if (elapsed)
-                       lprocfs_oh_tally_log2(&h[BRW_R_IO_TIME], elapsed);
-
-       } else {
+       else
                atomic_dec(&osd->od_w_in_flight);
-               lprocfs_oh_tally_log2(&h[BRW_W_PAGES], npages);
-               if (disksize > 0)
-                       lprocfs_oh_tally_log2(&h[BRW_W_DISK_IOSIZE], disksize);
-               if (elapsed)
-                       lprocfs_oh_tally_log2(&h[BRW_W_IO_TIME], elapsed);
-       }
+
+       lprocfs_oh_tally_log2(&h[BRW_R_PAGES + rw], npages);
+       if (disksize > 0)
+               lprocfs_oh_tally_log2(&h[BRW_R_DISK_IOSIZE + rw], disksize);
+       if (elapsed)
+               lprocfs_oh_tally_log2(&h[BRW_R_IO_TIME + rw], elapsed);
 }
 
-static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
-                       struct lu_buf *buf, loff_t *pos)
+static ssize_t __osd_read(const struct lu_env *env, struct dt_object *dt,
+                         struct lu_buf *buf, loff_t *pos, size_t *size)
 {
-       struct osd_object *obj  = osd_dt_obj(dt);
-       struct osd_device *osd = osd_obj2dev(obj);
-       uint64_t           old_size;
-       int                size = buf->lb_len;
-       int                rc;
-       unsigned long      start;
+       struct osd_object *obj = osd_dt_obj(dt);
+       uint64_t old_size;
+       int rc;
 
        LASSERT(dt_object_exists(dt));
        LASSERT(obj->oo_dn);
 
-       start = cfs_time_current();
-
        read_lock(&obj->oo_attr_lock);
        old_size = obj->oo_attr.la_size;
        read_unlock(&obj->oo_attr_lock);
 
-       if (*pos + size > old_size) {
+       if (*pos + *size > old_size) {
                if (old_size < *pos)
                        return 0;
-               else
-                       size = old_size - *pos;
+
+               *size = old_size - *pos;
        }
 
-       record_start_io(osd, READ, 0);
+       rc = osd_dmu_read(osd_obj2dev(obj), obj->oo_dn, *pos, *size,
+                         buf->lb_buf, DMU_READ_PREFETCH);
+       if (!rc) {
+               rc = *size;
+               *pos += *size;
+       }
+
+       return rc;
+}
 
-       rc = osd_dmu_read(osd, obj->oo_dn, *pos, size, buf->lb_buf,
-                         DMU_READ_PREFETCH);
+static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
+                       struct lu_buf *buf, loff_t *pos)
+{
+       struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
+       size_t size = buf->lb_len;
+       hrtime_t start = gethrtime();
+       s64 delta_ms;
+       int rc;
+
+       record_start_io(osd, READ, 0);
+       rc = __osd_read(env, dt, buf, pos, &size);
+       delta_ms = gethrtime() - start;
+       do_div(delta_ms, NSEC_PER_MSEC);
+       record_end_io(osd, READ, delta_ms, size, size >> PAGE_SHIFT);
 
-       record_end_io(osd, READ, cfs_time_current() - start, size,
-                     size >> PAGE_SHIFT);
-       if (rc == 0) {
-               rc = size;
-               *pos += size;
-       }
        return rc;
 }
 
+static inline ssize_t osd_read_no_record(const struct lu_env *env,
+                                        struct dt_object *dt,
+                                        struct lu_buf *buf, loff_t *pos)
+{
+       size_t size = buf->lb_len;
+
+       return __osd_read(env, dt, buf, pos, &size);
+}
+
 static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
                                const struct lu_buf *buf, loff_t pos,
                                struct thandle *th)
@@ -156,7 +173,7 @@ static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
        uint64_t            oid;
        ENTRY;
 
-       oh = container_of0(th, struct osd_thandle, ot_super);
+       oh = container_of(th, struct osd_thandle, ot_super);
 
        /* in some cases declare can race with creation (e.g. llog)
         * and we need to wait till object is initialized. notice
@@ -187,7 +204,7 @@ static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
 
 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
                        const struct lu_buf *buf, loff_t *pos,
-                       struct thandle *th, int ignore_quota)
+                        struct thandle *th)
 {
        struct osd_object  *obj  = osd_dt_obj(dt);
        struct osd_device  *osd = osd_obj2dev(obj);
@@ -201,7 +218,7 @@ static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
        LASSERT(obj->oo_dn);
 
        LASSERT(th != NULL);
-       oh = container_of0(th, struct osd_thandle, ot_super);
+       oh = container_of(th, struct osd_thandle, ot_super);
 
        osd_dmu_write(osd, obj->oo_dn, offset, (uint64_t)buf->lb_len,
                      buf->lb_buf, oh->ot_tx);
@@ -266,7 +283,7 @@ static int osd_bufs_put(const struct lu_env *env, struct dt_object *dt,
                        } else if (lnb[i].lnb_data != NULL) {
                                int j, apages, abufsz;
                                abufsz = arc_buf_size(lnb[i].lnb_data);
-                               apages = abufsz / PAGE_SIZE;
+                               apages = abufsz >> PAGE_SHIFT;
                                /* these references to pages must be invalidated
                                 * to prevent access in osd_bufs_put() */
                                for (j = 0; j < apages; j++)
@@ -312,16 +329,21 @@ static inline struct page *kmem_to_page(void *addr)
  * \retval             negative error number of failure
  */
 static int osd_bufs_get_read(const struct lu_env *env, struct osd_object *obj,
-                            loff_t off, ssize_t len, struct niobuf_local *lnb)
+                            loff_t off, ssize_t len, struct niobuf_local *lnb,
+                            int maxlnb)
 {
        struct osd_device *osd = osd_obj2dev(obj);
-       unsigned long      start = cfs_time_current();
-       int                rc, i, numbufs, npages = 0;
-       dmu_buf_t        **dbp;
-       ENTRY;
+       int rc, i, numbufs, npages = 0, drop_cache = 0;
+       hrtime_t start = gethrtime();
+       dmu_buf_t **dbp;
+       s64 delta_ms;
 
+       ENTRY;
        record_start_io(osd, READ, 0);
 
+       if (obj->oo_attr.la_size >= osd->od_readcache_max_filesize)
+               drop_cache = 1;
+
        /* grab buffers for read:
         * OSD API let us to grab buffers first, then initiate IO(s)
         * so that all required IOs will be done in parallel, but at the
@@ -336,6 +358,10 @@ static int osd_bufs_get_read(const struct lu_env *env, struct osd_object *obj,
                    off + len > obj->oo_dn->dn_datablksz)
                        len = obj->oo_dn->dn_datablksz - off;
 
+               dbp = NULL;
+               if (unlikely(npages >= maxlnb))
+                       GOTO(err, rc = -EOVERFLOW);
+
                rc = -dmu_buf_hold_array_by_bonus(&obj->oo_dn->dn_bonus->db,
                                                  off, len, TRUE, osd_0copy_tag,
                                                  &numbufs, &dbp);
@@ -358,6 +384,9 @@ static int osd_bufs_get_read(const struct lu_env *env, struct osd_object *obj,
                        dbf = (void *) ((unsigned long)dbp[i] | 1);
 
                        while (tocpy > 0) {
+                               if (unlikely(npages >= maxlnb))
+                                       GOTO(err, rc = -EOVERFLOW);
+
                                thispage = PAGE_SIZE;
                                thispage -= bufoff & (PAGE_SIZE - 1);
                                thispage = min(tocpy, thispage);
@@ -382,6 +411,9 @@ static int osd_bufs_get_read(const struct lu_env *env, struct osd_object *obj,
                                lnb++;
                        }
 
+                       if (drop_cache)
+                               dbuf_set_pending_evict(dbp[i]);
+
                        /* steal dbuf so dmu_buf_rele_array() can't release
                         * it */
                        dbp[i] = NULL;
@@ -390,13 +422,16 @@ static int osd_bufs_get_read(const struct lu_env *env, struct osd_object *obj,
                dmu_buf_rele_array(dbp, numbufs, osd_0copy_tag);
        }
 
-       record_end_io(osd, READ, cfs_time_current() - start,
-                     npages * PAGE_SIZE, npages);
+       delta_ms = gethrtime() - start;
+       do_div(delta_ms, NSEC_PER_MSEC);
+       record_end_io(osd, READ, delta_ms, npages * PAGE_SIZE, npages);
 
        RETURN(npages);
 
 err:
        LASSERT(rc < 0);
+       if (dbp)
+               dmu_buf_rele_array(dbp, numbufs, osd_0copy_tag);
        osd_bufs_put(env, &obj->oo_dt, lnb - npages, npages);
        RETURN(rc);
 }
@@ -425,10 +460,11 @@ static inline arc_buf_t *osd_request_arcbuf(dnode_t *dn, size_t bs)
 }
 
 static int osd_bufs_get_write(const struct lu_env *env, struct osd_object *obj,
-                             loff_t off, ssize_t len, struct niobuf_local *lnb)
+                             loff_t off, ssize_t len, struct niobuf_local *lnb,
+                             int maxlnb)
 {
        struct osd_device *osd = osd_obj2dev(obj);
-       int                plen, off_in_block, sz_in_block;
+       int                poff, plen, off_in_block, sz_in_block;
        int                rc, i = 0, npages = 0;
        dnode_t *dn = obj->oo_dn;
        arc_buf_t *abuf;
@@ -440,7 +476,8 @@ static int osd_bufs_get_write(const struct lu_env *env, struct osd_object *obj,
         * so that we're sure nobody is trying to update the same block
         */
        while (len > 0) {
-               LASSERT(npages < PTLRPC_MAX_BRW_PAGES);
+               if (unlikely(npages >= maxlnb))
+                       GOTO(out_err, rc = -EOVERFLOW);
 
                off_in_block = off & (bs - 1);
                sz_in_block = min_t(int, bs - off_in_block, len);
@@ -461,6 +498,9 @@ static int osd_bufs_get_write(const struct lu_env *env, struct osd_object *obj,
                        while (sz_in_block > 0) {
                                plen = min_t(int, sz_in_block, PAGE_SIZE);
 
+                               if (unlikely(npages >= maxlnb))
+                                       GOTO(out_err, rc = -EOVERFLOW);
+
                                lnb[i].lnb_file_offset = off;
                                lnb[i].lnb_page_offset = 0;
                                lnb[i].lnb_len = plen;
@@ -492,11 +532,19 @@ static int osd_bufs_get_write(const struct lu_env *env, struct osd_object *obj,
                                                LPROC_OSD_TAIL_IO, 1);
 
                        /* can't use zerocopy, allocate temp. buffers */
+                       poff = off & (PAGE_SIZE - 1);
                        while (sz_in_block > 0) {
-                               plen = min_t(int, sz_in_block, PAGE_SIZE);
+                               plen = min_t(int, poff + sz_in_block,
+                                            PAGE_SIZE);
+                               plen -= poff;
+
+                               if (unlikely(npages >= maxlnb))
+                                       GOTO(out_err, rc = -EOVERFLOW);
 
                                lnb[i].lnb_file_offset = off;
-                               lnb[i].lnb_page_offset = 0;
+                               lnb[i].lnb_page_offset = poff;
+                               poff = 0;
+
                                lnb[i].lnb_len = plen;
                                lnb[i].lnb_rc = 0;
                                lnb[i].lnb_data = NULL;
@@ -530,7 +578,7 @@ out_err:
 
 static int osd_bufs_get(const struct lu_env *env, struct dt_object *dt,
                        loff_t offset, ssize_t len, struct niobuf_local *lnb,
-                       enum dt_bufs_type rw)
+                       int maxlnb, enum dt_bufs_type rw)
 {
        struct osd_object *obj  = osd_dt_obj(dt);
        int                rc;
@@ -539,9 +587,9 @@ static int osd_bufs_get(const struct lu_env *env, struct dt_object *dt,
        LASSERT(obj->oo_dn);
 
        if (rw & DT_BUFS_TYPE_WRITE)
-               rc = osd_bufs_get_write(env, obj, offset, len, lnb);
+               rc = osd_bufs_get_write(env, obj, offset, len, lnb, maxlnb);
        else
-               rc = osd_bufs_get_read(env, obj, offset, len, lnb);
+               rc = osd_bufs_get_read(env, obj, offset, len, lnb, maxlnb);
 
        return rc;
 }
@@ -566,11 +614,9 @@ static inline uint64_t osd_roundup2blocksz(uint64_t size,
        size += offset % blksz;
 
        if (likely(is_power_of_2(blksz)))
-               return PO2_ROUNDUP_TYPED(size, blksz, uint64_t);
-
-       size += blksz - 1;
-       do_div(size, blksz);
-       return size * blksz;
+               return round_up(size, blksz);
+       else
+               return DIV_ROUND_UP_ULL(size, blksz) * blksz;
 }
 
 static int osd_declare_write_commit(const struct lu_env *env,
@@ -584,11 +630,12 @@ static int osd_declare_write_commit(const struct lu_env *env,
        uint64_t            offset = 0;
        uint32_t            size = 0;
        uint32_t blksz = obj->oo_dn->dn_datablksz;
-       int                 i, rc, flags = 0;
+       int                 i, rc;
        bool synced = false;
        long long           space = 0;
        struct page        *last_page = NULL;
        unsigned long       discont_pages = 0;
+       enum osd_quota_local_flags local_flags = 0;
        enum osd_qid_declare_flags declare_flags = OSD_QID_BLK;
        ENTRY;
 
@@ -598,7 +645,7 @@ static int osd_declare_write_commit(const struct lu_env *env,
        LASSERT(lnb);
        LASSERT(npages > 0);
 
-       oh = container_of0(th, struct osd_thandle, ot_super);
+       oh = container_of(th, struct osd_thandle, ot_super);
 
        for (i = 0; i < npages; i++) {
                if (last_page && lnb[i].lnb_page->index != (last_page->index + 1))
@@ -656,8 +703,6 @@ static int osd_declare_write_commit(const struct lu_env *env,
                space += osd_roundup2blocksz(size, offset, blksz);
        }
 
-       oh->ot_write_commit = 1; /* used in osd_trans_start() for fail_loc */
-
        /* backend zfs filesystem might be configured to store multiple data
         * copies */
        space  *= osd->od_os->os_copies;
@@ -670,25 +715,26 @@ retry:
        /* acquire quota space if needed */
        rc = osd_declare_quota(env, osd, obj->oo_attr.la_uid,
                               obj->oo_attr.la_gid, obj->oo_attr.la_projid,
-                              space, oh, &flags, declare_flags);
+                              space, oh, &local_flags, declare_flags);
 
-       if (!synced && rc == -EDQUOT && (flags & QUOTA_FL_SYNC) != 0) {
+       if (!synced && rc == -EDQUOT &&
+           (local_flags & QUOTA_FL_SYNC) != 0) {
                dt_sync(env, th->th_dev);
                synced = true;
                CDEBUG(D_QUOTA, "retry after sync\n");
-               flags = 0;
+               local_flags = 0;
                goto retry;
        }
 
        /* 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)
+       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;
 #ifdef ZFS_PROJINHERIT
-       if (flags & QUOTA_FL_OVER_PRJQUOTA)
+       if (local_flags & QUOTA_FL_OVER_PRJQUOTA)
                lnb[0].lnb_flags |= OBD_BRW_OVER_PRJQUOTA;
 #endif
 
@@ -752,15 +798,32 @@ out:
        return rc;
 }
 
+static void osd_evict_dbufs_after_write(struct osd_object *obj,
+                                       loff_t off, ssize_t len)
+{
+       dmu_buf_t **dbp;
+       int i, rc, numbufs;
+
+       rc = -dmu_buf_hold_array_by_bonus(&obj->oo_dn->dn_bonus->db, off, len,
+                                         TRUE, osd_0copy_tag, &numbufs, &dbp);
+       if (unlikely(rc))
+               return;
+
+       for (i = 0; i < numbufs; i++)
+               dbuf_set_pending_evict(dbp[i]);
+
+       dmu_buf_rele_array(dbp, numbufs, osd_0copy_tag);
+}
+
 static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
                        struct niobuf_local *lnb, int npages,
-                       struct thandle *th)
+                       struct thandle *th, __u64 user_size)
 {
        struct osd_object  *obj  = osd_dt_obj(dt);
        struct osd_device  *osd = osd_obj2dev(obj);
        struct osd_thandle *oh;
        uint64_t            new_size = 0;
-       int                 i, rc = 0;
+       int                 i, abufsz, rc = 0, drop_cache = 0;
        unsigned long      iosize = 0;
        ENTRY;
 
@@ -768,13 +831,27 @@ static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
        LASSERT(obj->oo_dn);
 
        LASSERT(th != NULL);
-       oh = container_of0(th, struct osd_thandle, ot_super);
+       oh = container_of(th, struct osd_thandle, ot_super);
 
        /* adjust block size. Assume the buffers are sorted. */
        (void)osd_grow_blocksize(obj, oh, lnb[0].lnb_file_offset,
                                 lnb[npages - 1].lnb_file_offset +
                                 lnb[npages - 1].lnb_len);
 
+       if (obj->oo_attr.la_size >= osd->od_readcache_max_filesize ||
+           lnb[npages - 1].lnb_file_offset + lnb[npages - 1].lnb_len >=
+           osd->od_readcache_max_filesize)
+               drop_cache = 1;
+
+       if (OBD_FAIL_CHECK(OBD_FAIL_OST_MAPBLK_ENOSPC))
+               RETURN(-ENOSPC);
+
+       /* if la_size is already bigger than specified user_size,
+        * ignore user_size
+        */
+       if (obj->oo_attr.la_size > user_size)
+               user_size = 0;
+
        /* LU-8791: take oo_guard to avoid the deadlock that changing block
         * size and assigning arcbuf take place at the same time.
         *
@@ -822,12 +899,13 @@ static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
 
                if (lnb[i].lnb_page->mapping == (void *)obj) {
                        osd_dmu_write(osd, obj->oo_dn, lnb[i].lnb_file_offset,
-                                     lnb[i].lnb_len, kmap(lnb[i].lnb_page),
-                                     oh->ot_tx);
+                                     lnb[i].lnb_len, kmap(lnb[i].lnb_page) +
+                                     lnb[i].lnb_page_offset, oh->ot_tx);
                        kunmap(lnb[i].lnb_page);
                        iosize += lnb[i].lnb_len;
+                       abufsz = lnb[i].lnb_len; /* to drop cache below */
                } else if (lnb[i].lnb_data) {
-                       int j, apages, abufsz;
+                       int j, apages;
                        LASSERT(((unsigned long)lnb[i].lnb_data & 1) == 0);
                        /* buffer loaned for zerocopy, try to use it.
                         * notice that dmu_assign_arcbuf() is smart
@@ -835,7 +913,7 @@ static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
                         * in this case it fallbacks to dmu_write() */
                        abufsz = arc_buf_size(lnb[i].lnb_data);
                        LASSERT(abufsz & PAGE_MASK);
-                       apages = abufsz / PAGE_SIZE;
+                       apages = abufsz >> PAGE_SHIFT;
                        LASSERT(i + apages <= npages);
                        /* these references to pages must be invalidated
                         * to prevent access in osd_bufs_put() */
@@ -849,8 +927,20 @@ static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
                        lnb[i].lnb_data = NULL;
                        atomic_dec(&osd->od_zerocopy_loan);
                        iosize += abufsz;
+               } else {
+                       /* we don't want to deal with cache if nothing
+                        * has been send to ZFS at this step */
+                       continue;
                }
 
+               if (!drop_cache)
+                       continue;
+
+               /* we have to mark dbufs for eviction here because
+                * dmu_assign_arcbuf() may create a new dbuf for
+                * loaned abuf */
+               osd_evict_dbufs_after_write(obj, lnb[i].lnb_file_offset,
+                                           abufsz);
        }
        up_read(&obj->oo_guard);
 
@@ -863,6 +953,9 @@ static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
                RETURN(0);
        }
 
+       /* if file has grown, take user_size into account */
+       if (user_size && new_size > user_size)
+               new_size = user_size;
        write_lock(&obj->oo_attr_lock);
        if (obj->oo_attr.la_size < new_size) {
                obj->oo_attr.la_size = new_size;
@@ -902,10 +995,9 @@ static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
                lnb[i].lnb_rc = lnb[i].lnb_len;
 
                if (lnb[i].lnb_file_offset + lnb[i].lnb_len >= eof) {
+                       /* send complete pages all the time */
                        if (eof <= lnb[i].lnb_file_offset)
                                lnb[i].lnb_rc = 0;
-                       else
-                               lnb[i].lnb_rc = eof - lnb[i].lnb_file_offset;
 
                        /* all subsequent rc should be 0 */
                        while (++i < npages)
@@ -931,9 +1023,11 @@ static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
  * dmu_tx_hold_sa() and if off < size, dmu_tx_hold_free()
  * called and then assigned to a transaction group.
  */
-static int __osd_object_punch(objset_t *os, dnode_t *dn, dmu_tx_t *tx,
-                               uint64_t size, uint64_t off, uint64_t len)
+static int __osd_object_punch(struct osd_object *obj, objset_t *os,
+                             dmu_tx_t *tx, uint64_t off, uint64_t len)
 {
+       dnode_t *dn = obj->oo_dn;
+       uint64_t size = obj->oo_attr.la_size;
        int rc = 0;
 
        /* Assert that the transaction has been assigned to a
@@ -945,6 +1039,19 @@ static int __osd_object_punch(objset_t *os, dnode_t *dn, dmu_tx_t *tx,
        if (len == DMU_OBJECT_END && size == off)
                return 0;
 
+       /* 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 (len != DMU_OBJECT_END)
+               len -= LUSTRE_ENCRYPTION_UNIT_SIZE -
+                       (off & ~LUSTRE_ENCRYPTION_MASK);
+       if (obj->oo_lma_flags & LUSTRE_ENCRYPT_FL &&
+           off & ~LUSTRE_ENCRYPTION_MASK)
+               off = (off & LUSTRE_ENCRYPTION_MASK) +
+                       LUSTRE_ENCRYPTION_UNIT_SIZE;
+
+
        /* XXX: dnode_free_range() can be used to save on dnode lookup */
        if (off < size)
                dmu_free_range(os, dn->dn_object, off, len, tx);
@@ -966,7 +1073,7 @@ static int osd_punch(const struct lu_env *env, struct dt_object *dt,
        LASSERT(osd_invariant(obj));
 
        LASSERT(th != NULL);
-       oh = container_of0(th, struct osd_thandle, ot_super);
+       oh = container_of(th, struct osd_thandle, ot_super);
 
        write_lock(&obj->oo_attr_lock);
        /* truncate */
@@ -976,8 +1083,8 @@ static int osd_punch(const struct lu_env *env, struct dt_object *dt,
                len = end - start;
        write_unlock(&obj->oo_attr_lock);
 
-       rc = __osd_object_punch(osd->od_os, obj->oo_dn, oh->ot_tx,
-                               obj->oo_attr.la_size, start, len);
+       rc = __osd_object_punch(obj, osd->od_os, oh->ot_tx, start, len);
+
        /* set new size */
        if (len == DMU_OBJECT_END) {
                write_lock(&obj->oo_attr_lock);
@@ -998,7 +1105,7 @@ static int osd_declare_punch(const struct lu_env *env, struct dt_object *dt,
        __u64               len;
        ENTRY;
 
-       oh = container_of0(handle, struct osd_thandle, ot_super);
+       oh = container_of(handle, struct osd_thandle, ot_super);
 
        read_lock(&obj->oo_attr_lock);
        if (end == OBD_OBJECT_EOF || end >= obj->oo_attr.la_size)
@@ -1007,8 +1114,17 @@ static int osd_declare_punch(const struct lu_env *env, struct dt_object *dt,
                len = end - start;
 
        /* declare we'll free some blocks ... */
+       /* 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;
        if (start < obj->oo_attr.la_size) {
                read_unlock(&obj->oo_attr_lock);
+               dmu_tx_mark_netfree(oh->ot_tx);
                dmu_tx_hold_free(oh->ot_tx, obj->oo_dn->dn_object, start, len);
        } else {
                read_unlock(&obj->oo_attr_lock);
@@ -1034,6 +1150,75 @@ static int osd_ladvise(const struct lu_env *env, struct dt_object *dt,
        RETURN(rc);
 }
 
+static int osd_fallocate(const struct lu_env *env, struct dt_object *dt,
+                        __u64 start, __u64 end, int mode, struct thandle *th)
+{
+       int rc = -EOPNOTSUPP;
+       ENTRY;
+
+        /*
+         * space preallocation is not supported for ZFS
+         * Returns -EOPNOTSUPP for now
+         */
+       RETURN(rc);
+}
+
+static int osd_declare_fallocate(const struct lu_env *env,
+                                struct dt_object *dt, __u64 start, __u64 end,
+                                int mode, struct thandle *th)
+{
+       int rc = -EOPNOTSUPP;
+       ENTRY;
+
+        /*
+         * space preallocation is not supported for ZFS
+         * Returns -EOPNOTSUPP for now
+         */
+       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);
+       uint64_t size = obj->oo_attr.la_size;
+       uint64_t result = offset;
+       int rc;
+       boolean_t hole = whence == SEEK_HOLE;
+
+       ENTRY;
+
+       LASSERT(dt_object_exists(dt));
+       LASSERT(osd_invariant(obj));
+       LASSERT(offset >= 0);
+
+       /* for SEEK_HOLE treat 'offset' beyond the end of file as in real
+        * hole. LOV to decide after all if that real hole or not.
+        */
+       if (offset >= size)
+               RETURN(hole ? offset : -ENXIO);
+
+       rc = osd_dmu_offset_next(osd_obj2dev(obj)->od_os,
+                                obj->oo_dn->dn_object, hole, &result);
+       if (rc == ESRCH)
+               RETURN(-ENXIO);
+
+       /* file was dirty, so fall back to using generic logic:
+        * For HOLE return file size, for DATA the result is set
+        * already to the 'offset' parameter value.
+        */
+       if (rc == EBUSY && hole)
+               result = size;
+
+       /* dmu_offset_next() only works on whole blocks so may return SEEK_HOLE
+        * result as end of the last block instead of logical EOF which we need
+        */
+       if (result > size)
+               result = size;
+
+       RETURN(result);
+}
+
 struct dt_body_operations osd_body_ops = {
        .dbo_read                       = osd_read,
        .dbo_declare_write              = osd_declare_write,
@@ -1047,4 +1232,13 @@ struct dt_body_operations osd_body_ops = {
        .dbo_declare_punch              = osd_declare_punch,
        .dbo_punch                      = osd_punch,
        .dbo_ladvise                    = osd_ladvise,
+       .dbo_declare_fallocate          = osd_declare_fallocate,
+       .dbo_fallocate                  = osd_fallocate,
+       .dbo_lseek                      = osd_lseek,
+};
+
+struct dt_body_operations osd_body_scrub_ops = {
+       .dbo_read                       = osd_read_no_record,
+       .dbo_declare_write              = osd_declare_write,
+       .dbo_write                      = osd_write,
 };