4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/osd-zfs/osd_io.c
38 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
39 * Author: Mike Pershin <tappro@whamcloud.com>
42 #define DEBUG_SUBSYSTEM S_OSD
44 #include <lustre_ver.h>
45 #include <libcfs/libcfs.h>
46 #include <obd_support.h>
47 #include <lustre_net.h>
49 #include <obd_class.h>
50 #include <lustre_disk.h>
51 #include <lustre_fid.h>
52 #include <lustre/lustre_idl.h> /* LLOG_CHUNK_SIZE definition */
54 #include "osd_internal.h"
56 #include <sys/dnode.h>
61 #include <sys/spa_impl.h>
62 #include <sys/zfs_znode.h>
63 #include <sys/dmu_tx.h>
64 #include <sys/dmu_objset.h>
65 #include <sys/dsl_prop.h>
66 #include <sys/sa_impl.h>
69 static char *osd_zerocopy_tag = "zerocopy";
72 static void record_start_io(struct osd_device *osd, int rw, int npages,
75 struct obd_histogram *h = osd->od_brw_stats.hist;
78 atomic_inc(&osd->od_r_in_flight);
79 lprocfs_oh_tally(&h[BRW_R_RPC_HIST],
80 atomic_read(&osd->od_r_in_flight));
81 lprocfs_oh_tally_log2(&h[BRW_R_PAGES], npages);
82 lprocfs_oh_tally(&h[BRW_R_DISCONT_PAGES], discont_pages);
85 atomic_inc(&osd->od_w_in_flight);
86 lprocfs_oh_tally(&h[BRW_W_RPC_HIST],
87 atomic_read(&osd->od_w_in_flight));
88 lprocfs_oh_tally_log2(&h[BRW_W_PAGES], npages);
89 lprocfs_oh_tally(&h[BRW_W_DISCONT_PAGES], discont_pages);
94 static void record_end_io(struct osd_device *osd, int rw,
95 unsigned long elapsed, int disksize)
97 struct obd_histogram *h = osd->od_brw_stats.hist;
100 atomic_dec(&osd->od_r_in_flight);
102 lprocfs_oh_tally_log2(&h[BRW_R_DISK_IOSIZE], disksize);
104 lprocfs_oh_tally_log2(&h[BRW_R_IO_TIME], elapsed);
107 atomic_dec(&osd->od_w_in_flight);
109 lprocfs_oh_tally_log2(&h[BRW_W_DISK_IOSIZE], disksize);
111 lprocfs_oh_tally_log2(&h[BRW_W_IO_TIME], elapsed);
115 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
116 struct lu_buf *buf, loff_t *pos,
117 struct lustre_capa *capa)
119 struct osd_object *obj = osd_dt_obj(dt);
120 struct osd_device *osd = osd_obj2dev(obj);
122 int size = buf->lb_len;
126 LASSERT(dt_object_exists(dt));
129 start = cfs_time_current();
131 read_lock(&obj->oo_attr_lock);
132 old_size = obj->oo_attr.la_size;
133 read_unlock(&obj->oo_attr_lock);
135 if (*pos + size > old_size) {
139 size = old_size - *pos;
142 record_start_io(osd, READ, (size >> PAGE_CACHE_SHIFT), 0);
144 rc = -dmu_read(osd->od_os, obj->oo_db->db_object, *pos, size,
145 buf->lb_buf, DMU_READ_PREFETCH);
147 record_end_io(osd, READ, cfs_time_current() - start, size);
155 static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
156 const struct lu_buf *buf, loff_t pos,
159 struct osd_object *obj = osd_dt_obj(dt);
160 struct osd_device *osd = osd_obj2dev(obj);
161 struct osd_thandle *oh;
165 oh = container_of0(th, struct osd_thandle, ot_super);
167 /* in some cases declare can race with creation (e.g. llog)
168 * and we need to wait till object is initialized. notice
169 * LOHA_EXISTs is supposed to be the last step in the
172 /* declare possible size change. notice we can't check
173 * current size here as another thread can change it */
175 if (dt_object_exists(dt)) {
177 oid = obj->oo_db->db_object;
179 dmu_tx_hold_sa(oh->ot_tx, obj->oo_sa_hdl, 0);
181 oid = DMU_NEW_OBJECT;
182 dmu_tx_hold_sa_create(oh->ot_tx, ZFS_SA_BASE_ATTR_SIZE);
185 /* XXX: we still miss for append declaration support in ZFS
186 * -1 means append which is used by llog mostly, llog
187 * can grow upto LLOG_CHUNK_SIZE*8 records */
189 pos = max_t(loff_t, 256 * 8 * LLOG_CHUNK_SIZE,
190 obj->oo_attr.la_size + (2 << 20));
191 dmu_tx_hold_write(oh->ot_tx, oid, pos, buf->lb_len);
193 /* dt_declare_write() is usually called for system objects, such
194 * as llog or last_rcvd files. We needn't enforce quota on those
195 * objects, so always set the lqi_space as 0. */
196 RETURN(osd_declare_quota(env, osd, obj->oo_attr.la_uid,
197 obj->oo_attr.la_gid, 0, oh, true, NULL,
201 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
202 const struct lu_buf *buf, loff_t *pos,
203 struct thandle *th, struct lustre_capa *capa,
206 struct osd_object *obj = osd_dt_obj(dt);
207 struct osd_device *osd = osd_obj2dev(obj);
208 struct osd_thandle *oh;
209 uint64_t offset = *pos;
214 LASSERT(dt_object_exists(dt));
218 oh = container_of0(th, struct osd_thandle, ot_super);
220 record_start_io(osd, WRITE, (buf->lb_len >> PAGE_CACHE_SHIFT), 0);
222 dmu_write(osd->od_os, obj->oo_db->db_object, offset,
223 (uint64_t)buf->lb_len, buf->lb_buf, oh->ot_tx);
224 write_lock(&obj->oo_attr_lock);
225 if (obj->oo_attr.la_size < offset + buf->lb_len) {
226 obj->oo_attr.la_size = offset + buf->lb_len;
227 write_unlock(&obj->oo_attr_lock);
228 /* osd_object_sa_update() will be copying directly from oo_attr
229 * into dbuf. any update within a single txg will copy the
231 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
232 &obj->oo_attr.la_size, 8, oh);
236 write_unlock(&obj->oo_attr_lock);
243 record_end_io(osd, WRITE, 0, buf->lb_len);
249 * XXX: for the moment I don't want to use lnb_flags for osd-internal
250 * purposes as it's not very well defined ...
251 * instead I use the lowest bit of the address so that:
252 * arc buffer: .lnb_obj = abuf (arc we loan for write)
253 * dbuf buffer: .lnb_obj = dbuf | 1 (dbuf we get for read)
254 * copy buffer: .lnb_page->mapping = obj (page we allocate for write)
258 static int osd_bufs_put(const struct lu_env *env, struct dt_object *dt,
259 struct niobuf_local *lnb, int npages)
261 struct osd_object *obj = osd_dt_obj(dt);
262 struct osd_device *osd = osd_obj2dev(obj);
266 LASSERT(dt_object_exists(dt));
269 for (i = 0; i < npages; i++) {
270 if (lnb[i].lnb_page == NULL)
272 if (lnb[i].lnb_page->mapping == (void *)obj) {
273 /* this is anonymous page allocated for copy-write */
274 lnb[i].lnb_page->mapping = NULL;
275 __free_page(lnb[i].lnb_page);
276 atomic_dec(&osd->od_zerocopy_alloc);
278 /* see comment in osd_bufs_get_read() */
279 ptr = (unsigned long)lnb[i].lnb_data;
282 dmu_buf_rele((void *)ptr, osd_zerocopy_tag);
283 atomic_dec(&osd->od_zerocopy_pin);
284 } else if (lnb[i].lnb_data != NULL) {
285 dmu_return_arcbuf(lnb[i].lnb_data);
286 atomic_dec(&osd->od_zerocopy_loan);
289 lnb[i].lnb_page = NULL;
290 lnb[i].lnb_data = NULL;
296 static inline struct page *kmem_to_page(void *addr)
298 if (is_vmalloc_addr(addr))
299 return vmalloc_to_page(addr);
301 return virt_to_page(addr);
304 static int osd_bufs_get_read(const struct lu_env *env, struct osd_object *obj,
305 loff_t off, ssize_t len, struct niobuf_local *lnb)
307 struct osd_device *osd = osd_obj2dev(obj);
309 int rc, i, numbufs, npages = 0;
312 /* grab buffers for read:
313 * OSD API let us to grab buffers first, then initiate IO(s)
314 * so that all required IOs will be done in parallel, but at the
315 * moment DMU doesn't provide us with a method to grab buffers.
316 * If we discover this is a vital for good performance we
317 * can get own replacement for dmu_buf_hold_array_by_bonus().
320 rc = -dmu_buf_hold_array_by_bonus(obj->oo_db, off, len, TRUE,
321 osd_zerocopy_tag, &numbufs,
326 for (i = 0; i < numbufs; i++) {
327 int bufoff, tocpy, thispage;
332 atomic_inc(&osd->od_zerocopy_pin);
334 bufoff = off - dbp[i]->db_offset;
335 tocpy = min_t(int, dbp[i]->db_size - bufoff, len);
337 /* kind of trick to differentiate dbuf vs. arcbuf */
338 LASSERT(((unsigned long)dbp[i] & 1) == 0);
339 dbf = (void *) ((unsigned long)dbp[i] | 1);
342 thispage = PAGE_CACHE_SIZE;
343 thispage -= bufoff & (PAGE_CACHE_SIZE - 1);
344 thispage = min(tocpy, thispage);
347 lnb->lnb_file_offset = off;
348 lnb->lnb_page_offset = bufoff & ~CFS_PAGE_MASK;
349 lnb->lnb_len = thispage;
350 lnb->lnb_page = kmem_to_page(dbp[i]->db_data +
352 /* mark just a single slot: we need this
353 * reference to dbuf to be release once */
366 /* steal dbuf so dmu_buf_rele_array() cant release it */
370 dmu_buf_rele_array(dbp, numbufs, osd_zerocopy_tag);
377 osd_bufs_put(env, &obj->oo_dt, lnb - npages, npages);
381 static int osd_bufs_get_write(const struct lu_env *env, struct osd_object *obj,
382 loff_t off, ssize_t len, struct niobuf_local *lnb)
384 struct osd_device *osd = osd_obj2dev(obj);
385 int plen, off_in_block, sz_in_block;
386 int rc, i = 0, npages = 0;
392 dmu_object_size_from_db(obj->oo_db, &bs, &dummy);
395 * currently only full blocks are subject to zerocopy approach:
396 * so that we're sure nobody is trying to update the same block
399 LASSERT(npages < PTLRPC_MAX_BRW_PAGES);
401 off_in_block = off & (bs - 1);
402 sz_in_block = min_t(int, bs - off_in_block, len);
404 if (sz_in_block == bs) {
405 /* full block, try to use zerocopy */
407 abuf = dmu_request_arcbuf(obj->oo_db, bs);
408 if (unlikely(abuf == NULL))
409 GOTO(out_err, rc = -ENOMEM);
411 atomic_inc(&osd->od_zerocopy_loan);
413 /* go over pages arcbuf contains, put them as
414 * local niobufs for ptlrpc's bulks */
415 while (sz_in_block > 0) {
416 plen = min_t(int, sz_in_block, PAGE_CACHE_SIZE);
418 lnb[i].lnb_file_offset = off;
419 lnb[i].lnb_page_offset = 0;
420 lnb[i].lnb_len = plen;
422 if (sz_in_block == bs)
423 lnb[i].lnb_data = abuf;
425 lnb[i].lnb_data = NULL;
427 /* this one is not supposed to fail */
428 lnb[i].lnb_page = kmem_to_page(abuf->b_data +
430 LASSERT(lnb[i].lnb_page);
432 lprocfs_counter_add(osd->od_stats,
433 LPROC_OSD_ZEROCOPY_IO, 1);
438 off_in_block += plen;
443 if (off_in_block == 0 && len < bs &&
444 off + len >= obj->oo_attr.la_size)
445 lprocfs_counter_add(osd->od_stats,
446 LPROC_OSD_TAIL_IO, 1);
448 /* can't use zerocopy, allocate temp. buffers */
449 while (sz_in_block > 0) {
450 plen = min_t(int, sz_in_block, PAGE_CACHE_SIZE);
452 lnb[i].lnb_file_offset = off;
453 lnb[i].lnb_page_offset = 0;
454 lnb[i].lnb_len = plen;
456 lnb[i].lnb_data = NULL;
458 lnb[i].lnb_page = alloc_page(OSD_GFP_IO);
459 if (unlikely(lnb[i].lnb_page == NULL))
460 GOTO(out_err, rc = -ENOMEM);
462 LASSERT(lnb[i].lnb_page->mapping == NULL);
463 lnb[i].lnb_page->mapping = (void *)obj;
465 atomic_inc(&osd->od_zerocopy_alloc);
466 lprocfs_counter_add(osd->od_stats,
467 LPROC_OSD_COPY_IO, 1);
481 osd_bufs_put(env, &obj->oo_dt, lnb, npages);
485 static int osd_bufs_get(const struct lu_env *env, struct dt_object *dt,
486 loff_t offset, ssize_t len, struct niobuf_local *lnb,
487 int rw, struct lustre_capa *capa)
489 struct osd_object *obj = osd_dt_obj(dt);
492 LASSERT(dt_object_exists(dt));
496 rc = osd_bufs_get_read(env, obj, offset, len, lnb);
498 rc = osd_bufs_get_write(env, obj, offset, len, lnb);
503 static int osd_write_prep(const struct lu_env *env, struct dt_object *dt,
504 struct niobuf_local *lnb, int npages)
506 struct osd_object *obj = osd_dt_obj(dt);
508 LASSERT(dt_object_exists(dt));
514 /* Return number of blocks that aren't mapped in the [start, start + size]
516 static int osd_count_not_mapped(struct osd_object *obj, uint64_t start,
519 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)obj->oo_db;
530 if (dn->dn_maxblkid == 0) {
531 if (start + size <= dn->dn_datablksz)
533 if (start < dn->dn_datablksz)
534 start = dn->dn_datablksz;
535 /* assume largest block size */
536 blkshift = SPA_MAXBLOCKSHIFT;
538 /* blocksize can't change */
539 blkshift = dn->dn_datablkshift;
542 /* compute address of last block */
543 end = (start + size - 1) >> blkshift;
544 /* align start on block boundaries */
547 /* size is null, can't be mapped */
548 if (obj->oo_attr.la_size == 0 || dn->dn_maxblkid == 0)
549 GOTO(out, size = (end - start + 1) << blkshift);
551 /* beyond EOF, can't be mapped */
552 if (start > dn->dn_maxblkid)
553 GOTO(out, size = (end - start + 1) << blkshift);
556 for (blkid = start; blkid <= end; blkid++) {
557 if (blkid == dn->dn_maxblkid)
558 /* this one is mapped for sure */
560 if (blkid > dn->dn_maxblkid) {
561 size += (end - blkid + 1) << blkshift;
565 rc = dbuf_hold_impl(dn, 0, blkid, TRUE, FTAG, &db);
567 /* for ENOENT (block not mapped) and any other errors,
568 * assume the block isn't mapped */
569 size += 1 << blkshift;
581 static int osd_declare_write_commit(const struct lu_env *env,
582 struct dt_object *dt,
583 struct niobuf_local *lnb, int npages,
586 struct osd_object *obj = osd_dt_obj(dt);
587 struct osd_device *osd = osd_obj2dev(obj);
588 struct osd_thandle *oh;
591 int i, rc, flags = 0;
592 bool ignore_quota = false, synced = false;
594 struct page *last_page = NULL;
595 unsigned long discont_pages = 0;
598 LASSERT(dt_object_exists(dt));
604 oh = container_of0(th, struct osd_thandle, ot_super);
606 for (i = 0; i < npages; i++) {
607 if (last_page && lnb[i].lnb_page->index != (last_page->index + 1))
609 last_page = lnb[i].lnb_page;
611 /* ENOSPC, network RPC error, etc.
612 * We don't want to book space for pages which will be
613 * skipped in osd_write_commit(). Hence we skip pages
614 * with lnb_rc != 0 here too */
616 /* ignore quota for the whole request if any page is from
617 * client cache or written by root.
619 * XXX once we drop the 1.8 client support, the checking
620 * for whether page is from cache can be simplified as:
621 * !(lnb[i].flags & OBD_BRW_SYNC)
623 * XXX we could handle this on per-lnb basis as done by
625 if ((lnb[i].lnb_flags & OBD_BRW_NOQUOTA) ||
626 (lnb[i].lnb_flags & (OBD_BRW_FROM_GRANT | OBD_BRW_SYNC)) ==
630 /* first valid lnb */
631 offset = lnb[i].lnb_file_offset;
632 size = lnb[i].lnb_len;
635 if (offset + size == lnb[i].lnb_file_offset) {
636 /* this lnb is contiguous to the previous one */
637 size += lnb[i].lnb_len;
641 dmu_tx_hold_write(oh->ot_tx, obj->oo_db->db_object,
643 /* estimating space that will be consumed by a write is rather
644 * complicated with ZFS. As a consequence, we don't account for
645 * indirect blocks and quota overrun will be adjusted once the
646 * operation is committed, if required. */
647 space += osd_count_not_mapped(obj, offset, size);
649 offset = lnb[i].lnb_file_offset;
650 size = lnb[i].lnb_len;
654 dmu_tx_hold_write(oh->ot_tx, obj->oo_db->db_object,
656 space += osd_count_not_mapped(obj, offset, size);
659 dmu_tx_hold_sa(oh->ot_tx, obj->oo_sa_hdl, 0);
661 oh->ot_write_commit = 1; /* used in osd_trans_start() for fail_loc */
663 /* backend zfs filesystem might be configured to store multiple data
665 space *= osd->od_os->os_copies;
667 CDEBUG(D_QUOTA, "writting %d pages, reserving "LPD64"K of quota "
668 "space\n", npages, space);
670 record_start_io(osd, WRITE, npages, discont_pages);
672 /* acquire quota space if needed */
673 rc = osd_declare_quota(env, osd, obj->oo_attr.la_uid,
674 obj->oo_attr.la_gid, space, oh, true, &flags,
677 if (!synced && rc == -EDQUOT && (flags & QUOTA_FL_SYNC) != 0) {
678 dt_sync(env, th->th_dev);
680 CDEBUG(D_QUOTA, "retry after sync\n");
685 /* we need only to store the overquota flags in the first lnb for
686 * now, once we support multiple objects BRW, this code needs be
688 if (flags & QUOTA_FL_OVER_USRQUOTA)
689 lnb[0].lnb_flags |= OBD_BRW_OVER_USRQUOTA;
690 if (flags & QUOTA_FL_OVER_GRPQUOTA)
691 lnb[0].lnb_flags |= OBD_BRW_OVER_GRPQUOTA;
696 static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
697 struct niobuf_local *lnb, int npages,
700 struct osd_object *obj = osd_dt_obj(dt);
701 struct osd_device *osd = osd_obj2dev(obj);
702 struct osd_thandle *oh;
703 uint64_t new_size = 0;
705 unsigned long iosize = 0;
708 LASSERT(dt_object_exists(dt));
712 oh = container_of0(th, struct osd_thandle, ot_super);
714 for (i = 0; i < npages; i++) {
715 CDEBUG(D_INODE, "write %u bytes at %u\n",
716 (unsigned) lnb[i].lnb_len,
717 (unsigned) lnb[i].lnb_file_offset);
720 /* ENOSPC, network RPC error, etc.
721 * Unlike ldiskfs, zfs allocates new blocks on rewrite,
722 * so we skip this page if lnb_rc is set to -ENOSPC */
723 CDEBUG(D_INODE, "obj "DFID": skipping lnb[%u]: rc=%d\n",
724 PFID(lu_object_fid(&dt->do_lu)), i,
729 if (lnb[i].lnb_page->mapping == (void *)obj) {
730 dmu_write(osd->od_os, obj->oo_db->db_object,
731 lnb[i].lnb_file_offset, lnb[i].lnb_len,
732 kmap(lnb[i].lnb_page), oh->ot_tx);
733 kunmap(lnb[i].lnb_page);
734 } else if (lnb[i].lnb_data) {
735 LASSERT(((unsigned long)lnb[i].lnb_data & 1) == 0);
736 /* buffer loaned for zerocopy, try to use it.
737 * notice that dmu_assign_arcbuf() is smart
738 * enough to recognize changed blocksize
739 * in this case it fallbacks to dmu_write() */
740 dmu_assign_arcbuf(obj->oo_db, lnb[i].lnb_file_offset,
741 lnb[i].lnb_data, oh->ot_tx);
742 /* drop the reference, otherwise osd_put_bufs()
743 * will be releasing it - bad! */
744 lnb[i].lnb_data = NULL;
745 atomic_dec(&osd->od_zerocopy_loan);
748 if (new_size < lnb[i].lnb_file_offset + lnb[i].lnb_len)
749 new_size = lnb[i].lnb_file_offset + lnb[i].lnb_len;
750 iosize += lnb[i].lnb_len;
753 if (unlikely(new_size == 0)) {
754 /* no pages to write, no transno is needed */
756 /* it is important to return 0 even when all lnb_rc == -ENOSPC
757 * since ofd_commitrw_write() retries several times on ENOSPC */
758 record_end_io(osd, WRITE, 0, 0);
762 write_lock(&obj->oo_attr_lock);
763 if (obj->oo_attr.la_size < new_size) {
764 obj->oo_attr.la_size = new_size;
765 write_unlock(&obj->oo_attr_lock);
766 /* osd_object_sa_update() will be copying directly from
767 * oo_attr into dbuf. any update within a single txg will copy
769 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
770 &obj->oo_attr.la_size, 8, oh);
772 write_unlock(&obj->oo_attr_lock);
775 record_end_io(osd, WRITE, 0, iosize);
780 static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
781 struct niobuf_local *lnb, int npages)
783 struct osd_object *obj = osd_dt_obj(dt);
784 struct osd_device *osd = osd_obj2dev(obj);
789 unsigned long size = 0;
791 LASSERT(dt_object_exists(dt));
794 start = cfs_time_current();
796 record_start_io(osd, READ, npages, 0);
798 for (i = 0; i < npages; i++) {
799 buf.lb_buf = kmap(lnb[i].lnb_page);
800 buf.lb_len = lnb[i].lnb_len;
801 offset = lnb[i].lnb_file_offset;
803 CDEBUG(D_OTHER, "read %u bytes at %u\n",
804 (unsigned) lnb[i].lnb_len,
805 (unsigned) lnb[i].lnb_file_offset);
806 lnb[i].lnb_rc = osd_read(env, dt, &buf, &offset, NULL);
807 kunmap(lnb[i].lnb_page);
809 size += lnb[i].lnb_rc;
811 if (lnb[i].lnb_rc < buf.lb_len) {
812 /* all subsequent rc should be 0 */
819 record_end_io(osd, READ, cfs_time_current() - start, size);
825 * Punch/truncate an object
827 * IN: db - dmu_buf of the object to free data in.
828 * off - start of section to free.
829 * len - length of section to free (DMU_OBJECT_END => to EOF).
831 * RETURN: 0 if success
832 * error code if failure
834 * The transaction passed to this routine must have
835 * dmu_tx_hold_sa() and if off < size, dmu_tx_hold_free()
836 * called and then assigned to a transaction group.
838 static int __osd_object_punch(objset_t *os, dmu_buf_t *db, dmu_tx_t *tx,
839 uint64_t size, uint64_t off, uint64_t len)
843 /* Assert that the transaction has been assigned to a
844 transaction group. */
845 LASSERT(tx->tx_txg != 0);
847 * Nothing to do if file already at desired length.
849 if (len == DMU_OBJECT_END && size == off)
853 rc = -dmu_free_range(os, db->db_object, off, len, tx);
858 static int osd_punch(const struct lu_env *env, struct dt_object *dt,
859 __u64 start, __u64 end, struct thandle *th,
860 struct lustre_capa *capa)
862 struct osd_object *obj = osd_dt_obj(dt);
863 struct osd_device *osd = osd_obj2dev(obj);
864 struct osd_thandle *oh;
869 LASSERT(dt_object_exists(dt));
870 LASSERT(osd_invariant(obj));
873 oh = container_of0(th, struct osd_thandle, ot_super);
875 write_lock(&obj->oo_attr_lock);
877 if (end == OBD_OBJECT_EOF || end >= obj->oo_attr.la_size)
878 len = DMU_OBJECT_END;
881 write_unlock(&obj->oo_attr_lock);
883 rc = __osd_object_punch(osd->od_os, obj->oo_db, oh->ot_tx,
884 obj->oo_attr.la_size, start, len);
886 if (len == DMU_OBJECT_END) {
887 write_lock(&obj->oo_attr_lock);
888 obj->oo_attr.la_size = start;
889 write_unlock(&obj->oo_attr_lock);
890 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
891 &obj->oo_attr.la_size, 8, oh);
896 static int osd_declare_punch(const struct lu_env *env, struct dt_object *dt,
897 __u64 start, __u64 end, struct thandle *handle)
899 struct osd_object *obj = osd_dt_obj(dt);
900 struct osd_device *osd = osd_obj2dev(obj);
901 struct osd_thandle *oh;
905 oh = container_of0(handle, struct osd_thandle, ot_super);
907 read_lock(&obj->oo_attr_lock);
908 if (end == OBD_OBJECT_EOF || end >= obj->oo_attr.la_size)
909 len = DMU_OBJECT_END;
913 /* declare we'll free some blocks ... */
914 if (start < obj->oo_attr.la_size) {
915 read_unlock(&obj->oo_attr_lock);
916 dmu_tx_hold_free(oh->ot_tx, obj->oo_db->db_object, start, len);
918 read_unlock(&obj->oo_attr_lock);
921 /* ... and we'll modify size attribute */
922 dmu_tx_hold_sa(oh->ot_tx, obj->oo_sa_hdl, 0);
924 RETURN(osd_declare_quota(env, osd, obj->oo_attr.la_uid,
925 obj->oo_attr.la_gid, 0, oh, true, NULL,
930 struct dt_body_operations osd_body_ops = {
931 .dbo_read = osd_read,
932 .dbo_declare_write = osd_declare_write,
933 .dbo_write = osd_write,
934 .dbo_bufs_get = osd_bufs_get,
935 .dbo_bufs_put = osd_bufs_put,
936 .dbo_write_prep = osd_write_prep,
937 .dbo_declare_write_commit = osd_declare_write_commit,
938 .dbo_write_commit = osd_write_commit,
939 .dbo_read_prep = osd_read_prep,
940 .dbo_declare_punch = osd_declare_punch,
941 .dbo_punch = osd_punch,