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_MIN_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 discont_pages)
74 struct obd_histogram *h = osd->od_brw_stats.hist;
77 atomic_inc(&osd->od_r_in_flight);
78 lprocfs_oh_tally(&h[BRW_R_RPC_HIST],
79 atomic_read(&osd->od_r_in_flight));
80 lprocfs_oh_tally(&h[BRW_R_DISCONT_PAGES], discont_pages);
83 atomic_inc(&osd->od_w_in_flight);
84 lprocfs_oh_tally(&h[BRW_W_RPC_HIST],
85 atomic_read(&osd->od_w_in_flight));
86 lprocfs_oh_tally(&h[BRW_W_DISCONT_PAGES], discont_pages);
91 static void record_end_io(struct osd_device *osd, int rw,
92 unsigned long elapsed, int disksize, int npages)
94 struct obd_histogram *h = osd->od_brw_stats.hist;
97 atomic_dec(&osd->od_r_in_flight);
98 lprocfs_oh_tally_log2(&h[BRW_R_PAGES], npages);
100 lprocfs_oh_tally_log2(&h[BRW_R_DISK_IOSIZE], disksize);
102 lprocfs_oh_tally_log2(&h[BRW_R_IO_TIME], elapsed);
105 atomic_dec(&osd->od_w_in_flight);
106 lprocfs_oh_tally_log2(&h[BRW_W_PAGES], npages);
108 lprocfs_oh_tally_log2(&h[BRW_W_DISK_IOSIZE], disksize);
110 lprocfs_oh_tally_log2(&h[BRW_W_IO_TIME], elapsed);
114 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
115 struct lu_buf *buf, loff_t *pos)
117 struct osd_object *obj = osd_dt_obj(dt);
118 struct osd_device *osd = osd_obj2dev(obj);
120 int size = buf->lb_len;
124 LASSERT(dt_object_exists(dt));
127 start = cfs_time_current();
129 read_lock(&obj->oo_attr_lock);
130 old_size = obj->oo_attr.la_size;
131 read_unlock(&obj->oo_attr_lock);
133 if (*pos + size > old_size) {
137 size = old_size - *pos;
140 record_start_io(osd, READ, 0);
142 rc = -dmu_read(osd->od_os, obj->oo_db->db_object, *pos, size,
143 buf->lb_buf, DMU_READ_PREFETCH);
145 record_end_io(osd, READ, cfs_time_current() - start, size,
146 size >> PAGE_CACHE_SHIFT);
154 static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
155 const struct lu_buf *buf, loff_t pos,
158 struct osd_object *obj = osd_dt_obj(dt);
159 struct osd_device *osd = osd_obj2dev(obj);
160 struct osd_thandle *oh;
164 oh = container_of0(th, struct osd_thandle, ot_super);
166 /* in some cases declare can race with creation (e.g. llog)
167 * and we need to wait till object is initialized. notice
168 * LOHA_EXISTs is supposed to be the last step in the
171 /* declare possible size change. notice we can't check
172 * current size here as another thread can change it */
174 if (dt_object_exists(dt)) {
176 oid = obj->oo_db->db_object;
178 dmu_tx_hold_sa(oh->ot_tx, obj->oo_sa_hdl, 0);
180 oid = DMU_NEW_OBJECT;
181 dmu_tx_hold_sa_create(oh->ot_tx, ZFS_SA_BASE_ATTR_SIZE);
184 /* XXX: we still miss for append declaration support in ZFS
185 * -1 means append which is used by llog mostly, llog
186 * can grow upto LLOG_MIN_CHUNK_SIZE*8 records */
188 pos = max_t(loff_t, 256 * 8 * LLOG_MIN_CHUNK_SIZE,
189 obj->oo_attr.la_size + (2 << 20));
190 dmu_tx_hold_write(oh->ot_tx, oid, pos, buf->lb_len);
192 /* dt_declare_write() is usually called for system objects, such
193 * as llog or last_rcvd files. We needn't enforce quota on those
194 * objects, so always set the lqi_space as 0. */
195 RETURN(osd_declare_quota(env, osd, obj->oo_attr.la_uid,
196 obj->oo_attr.la_gid, 0, oh, true, NULL,
200 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
201 const struct lu_buf *buf, loff_t *pos,
202 struct thandle *th, int ignore_quota)
204 struct osd_object *obj = osd_dt_obj(dt);
205 struct osd_device *osd = osd_obj2dev(obj);
206 struct osd_thandle *oh;
207 uint64_t offset = *pos;
212 LASSERT(dt_object_exists(dt));
216 oh = container_of0(th, struct osd_thandle, ot_super);
218 record_start_io(osd, WRITE, 0);
220 dmu_write(osd->od_os, obj->oo_db->db_object, offset,
221 (uint64_t)buf->lb_len, buf->lb_buf, oh->ot_tx);
222 write_lock(&obj->oo_attr_lock);
223 if (obj->oo_attr.la_size < offset + buf->lb_len) {
224 obj->oo_attr.la_size = offset + buf->lb_len;
225 write_unlock(&obj->oo_attr_lock);
226 /* osd_object_sa_update() will be copying directly from oo_attr
227 * into dbuf. any update within a single txg will copy the
229 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
230 &obj->oo_attr.la_size, 8, oh);
234 write_unlock(&obj->oo_attr_lock);
241 record_end_io(osd, WRITE, 0, buf->lb_len,
242 buf->lb_len >> PAGE_CACHE_SHIFT);
248 * XXX: for the moment I don't want to use lnb_flags for osd-internal
249 * purposes as it's not very well defined ...
250 * instead I use the lowest bit of the address so that:
251 * arc buffer: .lnb_data = abuf (arc we loan for write)
252 * dbuf buffer: .lnb_data = dbuf | 1 (dbuf we get for read)
253 * copy buffer: .lnb_page->mapping = obj (page we allocate for write)
257 static int osd_bufs_put(const struct lu_env *env, struct dt_object *dt,
258 struct niobuf_local *lnb, int npages)
260 struct osd_object *obj = osd_dt_obj(dt);
261 struct osd_device *osd = osd_obj2dev(obj);
265 LASSERT(dt_object_exists(dt));
268 for (i = 0; i < npages; i++) {
269 if (lnb[i].lnb_page == NULL)
271 if (lnb[i].lnb_page->mapping == (void *)obj) {
272 /* this is anonymous page allocated for copy-write */
273 lnb[i].lnb_page->mapping = NULL;
274 __free_page(lnb[i].lnb_page);
275 atomic_dec(&osd->od_zerocopy_alloc);
277 /* see comment in osd_bufs_get_read() */
278 ptr = (unsigned long)lnb[i].lnb_data;
281 dmu_buf_rele((void *)ptr, osd_zerocopy_tag);
282 atomic_dec(&osd->od_zerocopy_pin);
283 } else if (lnb[i].lnb_data != NULL) {
284 dmu_return_arcbuf(lnb[i].lnb_data);
285 atomic_dec(&osd->od_zerocopy_loan);
288 lnb[i].lnb_page = NULL;
289 lnb[i].lnb_data = NULL;
295 static inline struct page *kmem_to_page(void *addr)
297 if (is_vmalloc_addr(addr))
298 return vmalloc_to_page(addr);
300 return virt_to_page(addr);
304 * Prepare buffers for read.
306 * The function maps the range described by \a off and \a len to \a lnb array.
307 * dmu_buf_hold_array_by_bonus() finds/creates appropriate ARC buffers, then
308 * we fill \a lnb array with the pages storing ARC buffers. Notice the current
309 * implementationt passes TRUE to dmu_buf_hold_array_by_bonus() to fill ARC
310 * buffers with actual data, I/O is done in the conext of osd_bufs_get_read().
311 * A better implementation would just return the buffers (potentially unfilled)
312 * and subsequent osd_read_prep() would do I/O for many ranges concurrently.
314 * \param[in] env environment
315 * \param[in] obj object
316 * \param[in] off offset in bytes
317 * \param[in] len the number of bytes to access
318 * \param[out] lnb array of local niobufs pointing to the buffers with data
320 * \retval 0 for success
321 * \retval negative error number of failure
323 static int osd_bufs_get_read(const struct lu_env *env, struct osd_object *obj,
324 loff_t off, ssize_t len, struct niobuf_local *lnb)
326 struct osd_device *osd = osd_obj2dev(obj);
327 unsigned long start = cfs_time_current();
328 int rc, i, numbufs, npages = 0;
332 record_start_io(osd, READ, 0);
334 /* grab buffers for read:
335 * OSD API let us to grab buffers first, then initiate IO(s)
336 * so that all required IOs will be done in parallel, but at the
337 * moment DMU doesn't provide us with a method to grab buffers.
338 * If we discover this is a vital for good performance we
339 * can get own replacement for dmu_buf_hold_array_by_bonus().
342 rc = -dmu_buf_hold_array_by_bonus(obj->oo_db, off, len, TRUE,
343 osd_zerocopy_tag, &numbufs,
348 for (i = 0; i < numbufs; i++) {
349 int bufoff, tocpy, thispage;
354 atomic_inc(&osd->od_zerocopy_pin);
356 bufoff = off - dbp[i]->db_offset;
357 tocpy = min_t(int, dbp[i]->db_size - bufoff, len);
359 /* kind of trick to differentiate dbuf vs. arcbuf */
360 LASSERT(((unsigned long)dbp[i] & 1) == 0);
361 dbf = (void *) ((unsigned long)dbp[i] | 1);
364 thispage = PAGE_CACHE_SIZE;
365 thispage -= bufoff & (PAGE_CACHE_SIZE - 1);
366 thispage = min(tocpy, thispage);
369 lnb->lnb_file_offset = off;
370 lnb->lnb_page_offset = bufoff & ~PAGE_MASK;
371 lnb->lnb_len = thispage;
372 lnb->lnb_page = kmem_to_page(dbp[i]->db_data +
374 /* mark just a single slot: we need this
375 * reference to dbuf to be released once */
388 /* steal dbuf so dmu_buf_rele_array() can't release
393 dmu_buf_rele_array(dbp, numbufs, osd_zerocopy_tag);
396 record_end_io(osd, READ, cfs_time_current() - start,
397 npages * PAGE_SIZE, npages);
403 osd_bufs_put(env, &obj->oo_dt, lnb - npages, npages);
407 static int osd_bufs_get_write(const struct lu_env *env, struct osd_object *obj,
408 loff_t off, ssize_t len, struct niobuf_local *lnb)
410 struct osd_device *osd = osd_obj2dev(obj);
411 int plen, off_in_block, sz_in_block;
412 int rc, i = 0, npages = 0;
418 dmu_object_size_from_db(obj->oo_db, &bs, &dummy);
421 * currently only full blocks are subject to zerocopy approach:
422 * so that we're sure nobody is trying to update the same block
425 LASSERT(npages < PTLRPC_MAX_BRW_PAGES);
427 off_in_block = off & (bs - 1);
428 sz_in_block = min_t(int, bs - off_in_block, len);
430 if (sz_in_block == bs) {
431 /* full block, try to use zerocopy */
433 abuf = dmu_request_arcbuf(obj->oo_db, bs);
434 if (unlikely(abuf == NULL))
435 GOTO(out_err, rc = -ENOMEM);
437 atomic_inc(&osd->od_zerocopy_loan);
439 /* go over pages arcbuf contains, put them as
440 * local niobufs for ptlrpc's bulks */
441 while (sz_in_block > 0) {
442 plen = min_t(int, sz_in_block, PAGE_CACHE_SIZE);
444 lnb[i].lnb_file_offset = off;
445 lnb[i].lnb_page_offset = 0;
446 lnb[i].lnb_len = plen;
448 if (sz_in_block == bs)
449 lnb[i].lnb_data = abuf;
451 lnb[i].lnb_data = NULL;
453 /* this one is not supposed to fail */
454 lnb[i].lnb_page = kmem_to_page(abuf->b_data +
456 LASSERT(lnb[i].lnb_page);
458 lprocfs_counter_add(osd->od_stats,
459 LPROC_OSD_ZEROCOPY_IO, 1);
464 off_in_block += plen;
469 if (off_in_block == 0 && len < bs &&
470 off + len >= obj->oo_attr.la_size)
471 lprocfs_counter_add(osd->od_stats,
472 LPROC_OSD_TAIL_IO, 1);
474 /* can't use zerocopy, allocate temp. buffers */
475 while (sz_in_block > 0) {
476 plen = min_t(int, sz_in_block, PAGE_CACHE_SIZE);
478 lnb[i].lnb_file_offset = off;
479 lnb[i].lnb_page_offset = 0;
480 lnb[i].lnb_len = plen;
482 lnb[i].lnb_data = NULL;
484 lnb[i].lnb_page = alloc_page(OSD_GFP_IO);
485 if (unlikely(lnb[i].lnb_page == NULL))
486 GOTO(out_err, rc = -ENOMEM);
488 LASSERT(lnb[i].lnb_page->mapping == NULL);
489 lnb[i].lnb_page->mapping = (void *)obj;
491 atomic_inc(&osd->od_zerocopy_alloc);
492 lprocfs_counter_add(osd->od_stats,
493 LPROC_OSD_COPY_IO, 1);
507 osd_bufs_put(env, &obj->oo_dt, lnb, npages);
511 static int osd_bufs_get(const struct lu_env *env, struct dt_object *dt,
512 loff_t offset, ssize_t len, struct niobuf_local *lnb,
515 struct osd_object *obj = osd_dt_obj(dt);
518 LASSERT(dt_object_exists(dt));
522 rc = osd_bufs_get_read(env, obj, offset, len, lnb);
524 rc = osd_bufs_get_write(env, obj, offset, len, lnb);
529 static int osd_write_prep(const struct lu_env *env, struct dt_object *dt,
530 struct niobuf_local *lnb, int npages)
532 struct osd_object *obj = osd_dt_obj(dt);
534 LASSERT(dt_object_exists(dt));
540 /* Return number of blocks that aren't mapped in the [start, start + size]
542 static int osd_count_not_mapped(struct osd_object *obj, uint64_t start,
545 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)obj->oo_db;
556 if (dn->dn_maxblkid == 0) {
557 if (start + size <= dn->dn_datablksz)
559 if (start < dn->dn_datablksz)
560 start = dn->dn_datablksz;
561 /* assume largest block size */
562 blkshift = osd_spa_maxblockshift(
563 dmu_objset_spa(osd_obj2dev(obj)->od_os));
565 /* blocksize can't change */
566 blkshift = dn->dn_datablkshift;
569 /* compute address of last block */
570 end = (start + size - 1) >> blkshift;
571 /* align start on block boundaries */
574 /* size is null, can't be mapped */
575 if (obj->oo_attr.la_size == 0 || dn->dn_maxblkid == 0)
576 GOTO(out, size = (end - start + 1) << blkshift);
578 /* beyond EOF, can't be mapped */
579 if (start > dn->dn_maxblkid)
580 GOTO(out, size = (end - start + 1) << blkshift);
583 for (blkid = start; blkid <= end; blkid++) {
584 if (blkid == dn->dn_maxblkid)
585 /* this one is mapped for sure */
587 if (blkid > dn->dn_maxblkid) {
588 size += (end - blkid + 1) << blkshift;
592 rc = dbuf_hold_impl(dn, 0, blkid, TRUE, FTAG, &db);
594 /* for ENOENT (block not mapped) and any other errors,
595 * assume the block isn't mapped */
596 size += 1 << blkshift;
608 static int osd_declare_write_commit(const struct lu_env *env,
609 struct dt_object *dt,
610 struct niobuf_local *lnb, int npages,
613 struct osd_object *obj = osd_dt_obj(dt);
614 struct osd_device *osd = osd_obj2dev(obj);
615 struct osd_thandle *oh;
618 int i, rc, flags = 0;
619 bool ignore_quota = false, synced = false;
621 struct page *last_page = NULL;
622 unsigned long discont_pages = 0;
625 LASSERT(dt_object_exists(dt));
631 oh = container_of0(th, struct osd_thandle, ot_super);
633 for (i = 0; i < npages; i++) {
634 if (last_page && lnb[i].lnb_page->index != (last_page->index + 1))
636 last_page = lnb[i].lnb_page;
638 /* ENOSPC, network RPC error, etc.
639 * We don't want to book space for pages which will be
640 * skipped in osd_write_commit(). Hence we skip pages
641 * with lnb_rc != 0 here too */
643 /* ignore quota for the whole request if any page is from
644 * client cache or written by root.
646 * XXX once we drop the 1.8 client support, the checking
647 * for whether page is from cache can be simplified as:
648 * !(lnb[i].flags & OBD_BRW_SYNC)
650 * XXX we could handle this on per-lnb basis as done by
652 if ((lnb[i].lnb_flags & OBD_BRW_NOQUOTA) ||
653 (lnb[i].lnb_flags & (OBD_BRW_FROM_GRANT | OBD_BRW_SYNC)) ==
657 /* first valid lnb */
658 offset = lnb[i].lnb_file_offset;
659 size = lnb[i].lnb_len;
662 if (offset + size == lnb[i].lnb_file_offset) {
663 /* this lnb is contiguous to the previous one */
664 size += lnb[i].lnb_len;
668 dmu_tx_hold_write(oh->ot_tx, obj->oo_db->db_object,
670 /* estimating space that will be consumed by a write is rather
671 * complicated with ZFS. As a consequence, we don't account for
672 * indirect blocks and quota overrun will be adjusted once the
673 * operation is committed, if required. */
674 space += osd_count_not_mapped(obj, offset, size);
676 offset = lnb[i].lnb_file_offset;
677 size = lnb[i].lnb_len;
681 dmu_tx_hold_write(oh->ot_tx, obj->oo_db->db_object,
683 space += osd_count_not_mapped(obj, offset, size);
686 dmu_tx_hold_sa(oh->ot_tx, obj->oo_sa_hdl, 0);
688 oh->ot_write_commit = 1; /* used in osd_trans_start() for fail_loc */
690 /* backend zfs filesystem might be configured to store multiple data
692 space *= osd->od_os->os_copies;
694 CDEBUG(D_QUOTA, "writting %d pages, reserving "LPD64"K of quota "
695 "space\n", npages, space);
697 record_start_io(osd, WRITE, discont_pages);
699 /* acquire quota space if needed */
700 rc = osd_declare_quota(env, osd, obj->oo_attr.la_uid,
701 obj->oo_attr.la_gid, space, oh, true, &flags,
704 if (!synced && rc == -EDQUOT && (flags & QUOTA_FL_SYNC) != 0) {
705 dt_sync(env, th->th_dev);
707 CDEBUG(D_QUOTA, "retry after sync\n");
712 /* we need only to store the overquota flags in the first lnb for
713 * now, once we support multiple objects BRW, this code needs be
715 if (flags & QUOTA_FL_OVER_USRQUOTA)
716 lnb[0].lnb_flags |= OBD_BRW_OVER_USRQUOTA;
717 if (flags & QUOTA_FL_OVER_GRPQUOTA)
718 lnb[0].lnb_flags |= OBD_BRW_OVER_GRPQUOTA;
724 * Policy to grow ZFS block size by write pattern.
725 * For sequential write, it grows block size gradually until it reaches the
726 * maximum blocksize the dataset can support. Otherwise, it will just use
727 * the maximum block size.
729 static int osd_grow_blocksize(struct osd_object *obj, struct osd_thandle *oh,
730 uint64_t start, uint64_t end)
732 struct osd_device *osd = osd_obj2dev(obj);
733 dmu_buf_impl_t *db = (dmu_buf_impl_t *)obj->oo_db;
742 if (dn->dn_maxblkid > 0) /* can't change block size */
745 blksz = dn->dn_datablksz;
746 if (blksz >= osd->od_max_blksz)
749 /* now ZFS can support up to 16MB block size, and if the write
750 * is sequential, it just increases the block size gradually */
751 if (start <= blksz) { /* sequential */
752 blksz = (uint32_t)min_t(uint64_t, osd->od_max_blksz, end);
753 if (!is_power_of_2(blksz))
754 blksz = size_roundup_power2(blksz);
755 } else { /* otherwise, use maximum block size */
756 blksz = osd->od_max_blksz;
759 if (blksz > dn->dn_datablksz)
760 rc = -dmu_object_set_blocksize(osd->od_os, dn->dn_object,
761 blksz, 0, oh->ot_tx);
768 static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
769 struct niobuf_local *lnb, int npages,
772 struct osd_object *obj = osd_dt_obj(dt);
773 struct osd_device *osd = osd_obj2dev(obj);
774 struct osd_thandle *oh;
775 uint64_t new_size = 0;
777 unsigned long iosize = 0;
780 LASSERT(dt_object_exists(dt));
784 oh = container_of0(th, struct osd_thandle, ot_super);
786 /* adjust block size. Assume the buffers are sorted. */
787 rc = osd_grow_blocksize(obj, oh, lnb[0].lnb_file_offset,
788 lnb[npages - 1].lnb_file_offset +
789 lnb[npages - 1].lnb_len);
790 if (rc < 0) /* ignore the error */
791 CDEBUG(D_INODE, "obj "DFID": change block size error rc=%d\n",
792 PFID(lu_object_fid(&dt->do_lu)), rc);
794 for (i = 0; i < npages; i++) {
795 CDEBUG(D_INODE, "write %u bytes at %u\n",
796 (unsigned) lnb[i].lnb_len,
797 (unsigned) lnb[i].lnb_file_offset);
800 /* ENOSPC, network RPC error, etc.
801 * Unlike ldiskfs, zfs allocates new blocks on rewrite,
802 * so we skip this page if lnb_rc is set to -ENOSPC */
803 CDEBUG(D_INODE, "obj "DFID": skipping lnb[%u]: rc=%d\n",
804 PFID(lu_object_fid(&dt->do_lu)), i,
809 if (lnb[i].lnb_page->mapping == (void *)obj) {
810 dmu_write(osd->od_os, obj->oo_db->db_object,
811 lnb[i].lnb_file_offset, lnb[i].lnb_len,
812 kmap(lnb[i].lnb_page), oh->ot_tx);
813 kunmap(lnb[i].lnb_page);
814 } else if (lnb[i].lnb_data) {
815 LASSERT(((unsigned long)lnb[i].lnb_data & 1) == 0);
816 /* buffer loaned for zerocopy, try to use it.
817 * notice that dmu_assign_arcbuf() is smart
818 * enough to recognize changed blocksize
819 * in this case it fallbacks to dmu_write() */
820 dmu_assign_arcbuf(obj->oo_db, lnb[i].lnb_file_offset,
821 lnb[i].lnb_data, oh->ot_tx);
822 /* drop the reference, otherwise osd_put_bufs()
823 * will be releasing it - bad! */
824 lnb[i].lnb_data = NULL;
825 atomic_dec(&osd->od_zerocopy_loan);
828 if (new_size < lnb[i].lnb_file_offset + lnb[i].lnb_len)
829 new_size = lnb[i].lnb_file_offset + lnb[i].lnb_len;
830 iosize += lnb[i].lnb_len;
833 if (unlikely(new_size == 0)) {
834 /* no pages to write, no transno is needed */
836 /* it is important to return 0 even when all lnb_rc == -ENOSPC
837 * since ofd_commitrw_write() retries several times on ENOSPC */
838 record_end_io(osd, WRITE, 0, 0, 0);
842 write_lock(&obj->oo_attr_lock);
843 if (obj->oo_attr.la_size < new_size) {
844 obj->oo_attr.la_size = new_size;
845 write_unlock(&obj->oo_attr_lock);
846 /* osd_object_sa_update() will be copying directly from
847 * oo_attr into dbuf. any update within a single txg will copy
849 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
850 &obj->oo_attr.la_size, 8, oh);
852 write_unlock(&obj->oo_attr_lock);
855 record_end_io(osd, WRITE, 0, iosize, npages);
860 static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
861 struct niobuf_local *lnb, int npages)
863 struct osd_object *obj = osd_dt_obj(dt);
865 unsigned long size = 0;
868 LASSERT(dt_object_exists(dt));
871 read_lock(&obj->oo_attr_lock);
872 eof = obj->oo_attr.la_size;
873 read_unlock(&obj->oo_attr_lock);
875 for (i = 0; i < npages; i++) {
876 if (unlikely(lnb[i].lnb_rc < 0))
879 lnb[i].lnb_rc = lnb[i].lnb_len;
880 size += lnb[i].lnb_rc;
882 if (lnb[i].lnb_file_offset + lnb[i].lnb_len > eof) {
883 lnb[i].lnb_rc = eof - lnb[i].lnb_file_offset;
884 if (lnb[i].lnb_rc < 0)
887 /* all subsequent rc should be 0 */
898 * Punch/truncate an object
900 * IN: db - dmu_buf of the object to free data in.
901 * off - start of section to free.
902 * len - length of section to free (DMU_OBJECT_END => to EOF).
904 * RETURN: 0 if success
905 * error code if failure
907 * The transaction passed to this routine must have
908 * dmu_tx_hold_sa() and if off < size, dmu_tx_hold_free()
909 * called and then assigned to a transaction group.
911 static int __osd_object_punch(objset_t *os, dmu_buf_t *db, dmu_tx_t *tx,
912 uint64_t size, uint64_t off, uint64_t len)
916 /* Assert that the transaction has been assigned to a
917 transaction group. */
918 LASSERT(tx->tx_txg != 0);
920 * Nothing to do if file already at desired length.
922 if (len == DMU_OBJECT_END && size == off)
926 rc = -dmu_free_range(os, db->db_object, off, len, tx);
931 static int osd_punch(const struct lu_env *env, struct dt_object *dt,
932 __u64 start, __u64 end, struct thandle *th)
934 struct osd_object *obj = osd_dt_obj(dt);
935 struct osd_device *osd = osd_obj2dev(obj);
936 struct osd_thandle *oh;
941 LASSERT(dt_object_exists(dt));
942 LASSERT(osd_invariant(obj));
945 oh = container_of0(th, struct osd_thandle, ot_super);
947 write_lock(&obj->oo_attr_lock);
949 if (end == OBD_OBJECT_EOF || end >= obj->oo_attr.la_size)
950 len = DMU_OBJECT_END;
953 write_unlock(&obj->oo_attr_lock);
955 rc = __osd_object_punch(osd->od_os, obj->oo_db, oh->ot_tx,
956 obj->oo_attr.la_size, start, len);
958 if (len == DMU_OBJECT_END) {
959 write_lock(&obj->oo_attr_lock);
960 obj->oo_attr.la_size = start;
961 write_unlock(&obj->oo_attr_lock);
962 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
963 &obj->oo_attr.la_size, 8, oh);
968 static int osd_declare_punch(const struct lu_env *env, struct dt_object *dt,
969 __u64 start, __u64 end, struct thandle *handle)
971 struct osd_object *obj = osd_dt_obj(dt);
972 struct osd_device *osd = osd_obj2dev(obj);
973 struct osd_thandle *oh;
977 oh = container_of0(handle, struct osd_thandle, ot_super);
979 read_lock(&obj->oo_attr_lock);
980 if (end == OBD_OBJECT_EOF || end >= obj->oo_attr.la_size)
981 len = DMU_OBJECT_END;
985 /* declare we'll free some blocks ... */
986 if (start < obj->oo_attr.la_size) {
987 read_unlock(&obj->oo_attr_lock);
988 dmu_tx_hold_free(oh->ot_tx, obj->oo_db->db_object, start, len);
990 read_unlock(&obj->oo_attr_lock);
993 /* ... and we'll modify size attribute */
994 dmu_tx_hold_sa(oh->ot_tx, obj->oo_sa_hdl, 0);
996 RETURN(osd_declare_quota(env, osd, obj->oo_attr.la_uid,
997 obj->oo_attr.la_gid, 0, oh, true, NULL,
1002 struct dt_body_operations osd_body_ops = {
1003 .dbo_read = osd_read,
1004 .dbo_declare_write = osd_declare_write,
1005 .dbo_write = osd_write,
1006 .dbo_bufs_get = osd_bufs_get,
1007 .dbo_bufs_put = osd_bufs_put,
1008 .dbo_write_prep = osd_write_prep,
1009 .dbo_declare_write_commit = osd_declare_write_commit,
1010 .dbo_write_commit = osd_write_commit,
1011 .dbo_read_prep = osd_read_prep,
1012 .dbo_declare_punch = osd_declare_punch,
1013 .dbo_punch = osd_punch,