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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/osd-zfs/osd_io.c
34 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
35 * Author: Mike Pershin <tappro@whamcloud.com>
38 #define DEBUG_SUBSYSTEM S_OSD
40 #include <lustre_ver.h>
41 #include <libcfs/libcfs.h>
42 #include <obd_support.h>
43 #include <lustre_net.h>
45 #include <obd_class.h>
46 #include <lustre_disk.h>
47 #include <lustre_fid.h>
48 #include <lustre/lustre_idl.h> /* LLOG_MIN_CHUNK_SIZE definition */
50 #include "osd_internal.h"
52 #include <sys/dnode.h>
57 #include <sys/spa_impl.h>
58 #include <sys/zfs_znode.h>
59 #include <sys/dmu_tx.h>
60 #include <sys/dmu_objset.h>
61 #include <sys/dsl_prop.h>
62 #include <sys/sa_impl.h>
65 static char *osd_0copy_tag = "zerocopy";
68 static void record_start_io(struct osd_device *osd, int rw, int discont_pages)
70 struct obd_histogram *h = osd->od_brw_stats.hist;
73 atomic_inc(&osd->od_r_in_flight);
74 lprocfs_oh_tally(&h[BRW_R_RPC_HIST],
75 atomic_read(&osd->od_r_in_flight));
76 lprocfs_oh_tally(&h[BRW_R_DISCONT_PAGES], discont_pages);
79 atomic_inc(&osd->od_w_in_flight);
80 lprocfs_oh_tally(&h[BRW_W_RPC_HIST],
81 atomic_read(&osd->od_w_in_flight));
82 lprocfs_oh_tally(&h[BRW_W_DISCONT_PAGES], discont_pages);
87 static void record_end_io(struct osd_device *osd, int rw,
88 unsigned long elapsed, int disksize, int npages)
90 struct obd_histogram *h = osd->od_brw_stats.hist;
93 atomic_dec(&osd->od_r_in_flight);
94 lprocfs_oh_tally_log2(&h[BRW_R_PAGES], npages);
96 lprocfs_oh_tally_log2(&h[BRW_R_DISK_IOSIZE], disksize);
98 lprocfs_oh_tally_log2(&h[BRW_R_IO_TIME], elapsed);
101 atomic_dec(&osd->od_w_in_flight);
102 lprocfs_oh_tally_log2(&h[BRW_W_PAGES], npages);
104 lprocfs_oh_tally_log2(&h[BRW_W_DISK_IOSIZE], disksize);
106 lprocfs_oh_tally_log2(&h[BRW_W_IO_TIME], elapsed);
110 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
111 struct lu_buf *buf, loff_t *pos)
113 struct osd_object *obj = osd_dt_obj(dt);
114 struct osd_device *osd = osd_obj2dev(obj);
116 int size = buf->lb_len;
120 LASSERT(dt_object_exists(dt));
123 start = cfs_time_current();
125 read_lock(&obj->oo_attr_lock);
126 old_size = obj->oo_attr.la_size;
127 read_unlock(&obj->oo_attr_lock);
129 if (*pos + size > old_size) {
133 size = old_size - *pos;
136 record_start_io(osd, READ, 0);
138 rc = -dmu_read(osd->od_os, obj->oo_dn->dn_object, *pos, size,
139 buf->lb_buf, DMU_READ_PREFETCH);
141 record_end_io(osd, READ, cfs_time_current() - start, size,
150 static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
151 const struct lu_buf *buf, loff_t pos,
154 struct osd_object *obj = osd_dt_obj(dt);
155 struct osd_device *osd = osd_obj2dev(obj);
156 struct osd_thandle *oh;
160 oh = container_of0(th, struct osd_thandle, ot_super);
162 /* in some cases declare can race with creation (e.g. llog)
163 * and we need to wait till object is initialized. notice
164 * LOHA_EXISTs is supposed to be the last step in the
167 /* size change (in dnode) will be declared by dmu_tx_hold_write() */
168 if (dt_object_exists(dt))
169 oid = obj->oo_dn->dn_object;
171 oid = DMU_NEW_OBJECT;
173 /* XXX: we still miss for append declaration support in ZFS
174 * -1 means append which is used by llog mostly, llog
175 * can grow upto LLOG_MIN_CHUNK_SIZE*8 records */
177 pos = max_t(loff_t, 256 * 8 * LLOG_MIN_CHUNK_SIZE,
178 obj->oo_attr.la_size + (2 << 20));
179 dmu_tx_hold_write(oh->ot_tx, oid, pos, buf->lb_len);
181 /* dt_declare_write() is usually called for system objects, such
182 * as llog or last_rcvd files. We needn't enforce quota on those
183 * objects, so always set the lqi_space as 0. */
184 RETURN(osd_declare_quota(env, osd, obj->oo_attr.la_uid,
185 obj->oo_attr.la_gid, 0, oh, true, NULL,
189 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
190 const struct lu_buf *buf, loff_t *pos,
191 struct thandle *th, int ignore_quota)
193 struct osd_object *obj = osd_dt_obj(dt);
194 struct osd_device *osd = osd_obj2dev(obj);
195 struct osd_thandle *oh;
196 uint64_t offset = *pos;
201 LASSERT(dt_object_exists(dt));
205 oh = container_of0(th, struct osd_thandle, ot_super);
207 dmu_write(osd->od_os, obj->oo_dn->dn_object, offset,
208 (uint64_t)buf->lb_len, buf->lb_buf, oh->ot_tx);
209 write_lock(&obj->oo_attr_lock);
210 if (obj->oo_attr.la_size < offset + buf->lb_len) {
211 obj->oo_attr.la_size = offset + buf->lb_len;
212 write_unlock(&obj->oo_attr_lock);
213 /* osd_object_sa_update() will be copying directly from oo_attr
214 * into dbuf. any update within a single txg will copy the
216 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
217 &obj->oo_attr.la_size, 8, oh);
221 write_unlock(&obj->oo_attr_lock);
232 * XXX: for the moment I don't want to use lnb_flags for osd-internal
233 * purposes as it's not very well defined ...
234 * instead I use the lowest bit of the address so that:
235 * arc buffer: .lnb_data = abuf (arc we loan for write)
236 * dbuf buffer: .lnb_data = dbuf | 1 (dbuf we get for read)
237 * copy buffer: .lnb_page->mapping = obj (page we allocate for write)
241 static int osd_bufs_put(const struct lu_env *env, struct dt_object *dt,
242 struct niobuf_local *lnb, int npages)
244 struct osd_object *obj = osd_dt_obj(dt);
245 struct osd_device *osd = osd_obj2dev(obj);
249 LASSERT(dt_object_exists(dt));
252 for (i = 0; i < npages; i++) {
253 if (lnb[i].lnb_page == NULL)
255 if (lnb[i].lnb_page->mapping == (void *)obj) {
256 /* this is anonymous page allocated for copy-write */
257 lnb[i].lnb_page->mapping = NULL;
258 __free_page(lnb[i].lnb_page);
259 atomic_dec(&osd->od_zerocopy_alloc);
261 /* see comment in osd_bufs_get_read() */
262 ptr = (unsigned long)lnb[i].lnb_data;
265 dmu_buf_rele((void *)ptr, osd_0copy_tag);
266 atomic_dec(&osd->od_zerocopy_pin);
267 } else if (lnb[i].lnb_data != NULL) {
268 dmu_return_arcbuf(lnb[i].lnb_data);
269 atomic_dec(&osd->od_zerocopy_loan);
272 lnb[i].lnb_page = NULL;
273 lnb[i].lnb_data = NULL;
279 static inline struct page *kmem_to_page(void *addr)
281 if (is_vmalloc_addr(addr))
282 return vmalloc_to_page(addr);
284 return virt_to_page(addr);
288 * Prepare buffers for read.
290 * The function maps the range described by \a off and \a len to \a lnb array.
291 * dmu_buf_hold_array_by_bonus() finds/creates appropriate ARC buffers, then
292 * we fill \a lnb array with the pages storing ARC buffers. Notice the current
293 * implementationt passes TRUE to dmu_buf_hold_array_by_bonus() to fill ARC
294 * buffers with actual data, I/O is done in the conext of osd_bufs_get_read().
295 * A better implementation would just return the buffers (potentially unfilled)
296 * and subsequent osd_read_prep() would do I/O for many ranges concurrently.
298 * \param[in] env environment
299 * \param[in] obj object
300 * \param[in] off offset in bytes
301 * \param[in] len the number of bytes to access
302 * \param[out] lnb array of local niobufs pointing to the buffers with data
304 * \retval 0 for success
305 * \retval negative error number of failure
307 static int osd_bufs_get_read(const struct lu_env *env, struct osd_object *obj,
308 loff_t off, ssize_t len, struct niobuf_local *lnb)
310 struct osd_device *osd = osd_obj2dev(obj);
311 unsigned long start = cfs_time_current();
312 int rc, i, numbufs, npages = 0;
316 record_start_io(osd, READ, 0);
318 /* grab buffers for read:
319 * OSD API let us to grab buffers first, then initiate IO(s)
320 * so that all required IOs will be done in parallel, but at the
321 * moment DMU doesn't provide us with a method to grab buffers.
322 * If we discover this is a vital for good performance we
323 * can get own replacement for dmu_buf_hold_array_by_bonus().
326 rc = -dmu_buf_hold_array_by_bonus(&obj->oo_dn->dn_bonus->db,
327 off, len, TRUE, osd_0copy_tag,
332 for (i = 0; i < numbufs; i++) {
333 int bufoff, tocpy, thispage;
338 atomic_inc(&osd->od_zerocopy_pin);
340 bufoff = off - dbp[i]->db_offset;
341 tocpy = min_t(int, dbp[i]->db_size - bufoff, len);
343 /* kind of trick to differentiate dbuf vs. arcbuf */
344 LASSERT(((unsigned long)dbp[i] & 1) == 0);
345 dbf = (void *) ((unsigned long)dbp[i] | 1);
348 thispage = PAGE_SIZE;
349 thispage -= bufoff & (PAGE_SIZE - 1);
350 thispage = min(tocpy, thispage);
353 lnb->lnb_file_offset = off;
354 lnb->lnb_page_offset = bufoff & ~PAGE_MASK;
355 lnb->lnb_len = thispage;
356 lnb->lnb_page = kmem_to_page(dbp[i]->db_data +
358 /* mark just a single slot: we need this
359 * reference to dbuf to be released once */
372 /* steal dbuf so dmu_buf_rele_array() can't release
377 dmu_buf_rele_array(dbp, numbufs, osd_0copy_tag);
380 record_end_io(osd, READ, cfs_time_current() - start,
381 npages * PAGE_SIZE, npages);
387 osd_bufs_put(env, &obj->oo_dt, lnb - npages, npages);
391 static int osd_bufs_get_write(const struct lu_env *env, struct osd_object *obj,
392 loff_t off, ssize_t len, struct niobuf_local *lnb)
394 struct osd_device *osd = osd_obj2dev(obj);
395 int plen, off_in_block, sz_in_block;
396 int rc, i = 0, npages = 0;
397 dnode_t *dn = obj->oo_dn;
399 uint32_t bs = dn->dn_datablksz;
403 * currently only full blocks are subject to zerocopy approach:
404 * so that we're sure nobody is trying to update the same block
407 LASSERT(npages < PTLRPC_MAX_BRW_PAGES);
409 off_in_block = off & (bs - 1);
410 sz_in_block = min_t(int, bs - off_in_block, len);
412 if (sz_in_block == bs) {
413 /* full block, try to use zerocopy */
415 abuf = dmu_request_arcbuf(&dn->dn_bonus->db, bs);
416 if (unlikely(abuf == NULL))
417 GOTO(out_err, rc = -ENOMEM);
419 atomic_inc(&osd->od_zerocopy_loan);
421 /* go over pages arcbuf contains, put them as
422 * local niobufs for ptlrpc's bulks */
423 while (sz_in_block > 0) {
424 plen = min_t(int, sz_in_block, PAGE_SIZE);
426 lnb[i].lnb_file_offset = off;
427 lnb[i].lnb_page_offset = 0;
428 lnb[i].lnb_len = plen;
430 if (sz_in_block == bs)
431 lnb[i].lnb_data = abuf;
433 lnb[i].lnb_data = NULL;
435 /* this one is not supposed to fail */
436 lnb[i].lnb_page = kmem_to_page(abuf->b_data +
438 LASSERT(lnb[i].lnb_page);
440 lprocfs_counter_add(osd->od_stats,
441 LPROC_OSD_ZEROCOPY_IO, 1);
446 off_in_block += plen;
451 if (off_in_block == 0 && len < bs &&
452 off + len >= obj->oo_attr.la_size)
453 lprocfs_counter_add(osd->od_stats,
454 LPROC_OSD_TAIL_IO, 1);
456 /* can't use zerocopy, allocate temp. buffers */
457 while (sz_in_block > 0) {
458 plen = min_t(int, sz_in_block, PAGE_SIZE);
460 lnb[i].lnb_file_offset = off;
461 lnb[i].lnb_page_offset = 0;
462 lnb[i].lnb_len = plen;
464 lnb[i].lnb_data = NULL;
466 lnb[i].lnb_page = alloc_page(OSD_GFP_IO);
467 if (unlikely(lnb[i].lnb_page == NULL))
468 GOTO(out_err, rc = -ENOMEM);
470 LASSERT(lnb[i].lnb_page->mapping == NULL);
471 lnb[i].lnb_page->mapping = (void *)obj;
473 atomic_inc(&osd->od_zerocopy_alloc);
474 lprocfs_counter_add(osd->od_stats,
475 LPROC_OSD_COPY_IO, 1);
489 osd_bufs_put(env, &obj->oo_dt, lnb, npages);
493 static int osd_bufs_get(const struct lu_env *env, struct dt_object *dt,
494 loff_t offset, ssize_t len, struct niobuf_local *lnb,
497 struct osd_object *obj = osd_dt_obj(dt);
500 LASSERT(dt_object_exists(dt));
504 rc = osd_bufs_get_read(env, obj, offset, len, lnb);
506 rc = osd_bufs_get_write(env, obj, offset, len, lnb);
511 static int osd_write_prep(const struct lu_env *env, struct dt_object *dt,
512 struct niobuf_local *lnb, int npages)
514 struct osd_object *obj = osd_dt_obj(dt);
516 LASSERT(dt_object_exists(dt));
522 static inline uint64_t osd_roundup2blocksz(uint64_t size,
528 size += offset % blksz;
530 if (likely(is_power_of_2(blksz)))
531 return PO2_ROUNDUP_TYPED(size, blksz, uint64_t);
538 static int osd_declare_write_commit(const struct lu_env *env,
539 struct dt_object *dt,
540 struct niobuf_local *lnb, int npages,
543 struct osd_object *obj = osd_dt_obj(dt);
544 struct osd_device *osd = osd_obj2dev(obj);
545 struct osd_thandle *oh;
548 uint32_t blksz = obj->oo_dn->dn_datablksz;
549 int i, rc, flags = 0;
550 bool ignore_quota = false, synced = false;
552 struct page *last_page = NULL;
553 unsigned long discont_pages = 0;
556 LASSERT(dt_object_exists(dt));
562 oh = container_of0(th, struct osd_thandle, ot_super);
564 for (i = 0; i < npages; i++) {
565 if (last_page && lnb[i].lnb_page->index != (last_page->index + 1))
567 last_page = lnb[i].lnb_page;
569 /* ENOSPC, network RPC error, etc.
570 * We don't want to book space for pages which will be
571 * skipped in osd_write_commit(). Hence we skip pages
572 * with lnb_rc != 0 here too */
574 /* ignore quota for the whole request if any page is from
575 * client cache or written by root.
577 * XXX once we drop the 1.8 client support, the checking
578 * for whether page is from cache can be simplified as:
579 * !(lnb[i].flags & OBD_BRW_SYNC)
581 * XXX we could handle this on per-lnb basis as done by
583 if ((lnb[i].lnb_flags & OBD_BRW_NOQUOTA) ||
584 (lnb[i].lnb_flags & (OBD_BRW_FROM_GRANT | OBD_BRW_SYNC)) ==
588 /* first valid lnb */
589 offset = lnb[i].lnb_file_offset;
590 size = lnb[i].lnb_len;
593 if (offset + size == lnb[i].lnb_file_offset) {
594 /* this lnb is contiguous to the previous one */
595 size += lnb[i].lnb_len;
599 dmu_tx_hold_write(oh->ot_tx, obj->oo_dn->dn_object,
601 /* Estimating space to be consumed by a write is rather
602 * complicated with ZFS. As a consequence, we don't account for
603 * indirect blocks and just use as a rough estimate the worse
604 * case where the old space is being held by a snapshot. Quota
605 * overrun will be adjusted once the operation is committed, if
607 space += osd_roundup2blocksz(size, offset, blksz);
609 offset = lnb[i].lnb_file_offset;
610 size = lnb[i].lnb_len;
614 dmu_tx_hold_write(oh->ot_tx, obj->oo_dn->dn_object,
616 space += osd_roundup2blocksz(size, offset, blksz);
619 dmu_tx_hold_sa(oh->ot_tx, obj->oo_sa_hdl, 0);
621 oh->ot_write_commit = 1; /* used in osd_trans_start() for fail_loc */
623 /* backend zfs filesystem might be configured to store multiple data
625 space *= osd->od_os->os_copies;
627 CDEBUG(D_QUOTA, "writing %d pages, reserving %lldK of quota space\n",
630 record_start_io(osd, WRITE, discont_pages);
632 /* acquire quota space if needed */
633 rc = osd_declare_quota(env, osd, obj->oo_attr.la_uid,
634 obj->oo_attr.la_gid, space, oh, true, &flags,
637 if (!synced && rc == -EDQUOT && (flags & QUOTA_FL_SYNC) != 0) {
638 dt_sync(env, th->th_dev);
640 CDEBUG(D_QUOTA, "retry after sync\n");
645 /* we need only to store the overquota flags in the first lnb for
646 * now, once we support multiple objects BRW, this code needs be
648 if (flags & QUOTA_FL_OVER_USRQUOTA)
649 lnb[0].lnb_flags |= OBD_BRW_OVER_USRQUOTA;
650 if (flags & QUOTA_FL_OVER_GRPQUOTA)
651 lnb[0].lnb_flags |= OBD_BRW_OVER_GRPQUOTA;
657 * Policy to grow ZFS block size by write pattern.
658 * For sequential write, it grows block size gradually until it reaches the
659 * maximum blocksize the dataset can support. Otherwise, it will pick a
660 * a block size by the writing region of this I/O.
662 static int osd_grow_blocksize(struct osd_object *obj, struct osd_thandle *oh,
663 uint64_t start, uint64_t end)
665 struct osd_device *osd = osd_obj2dev(obj);
666 dnode_t *dn = obj->oo_dn;
672 if (dn->dn_maxblkid > 0) /* can't change block size */
675 if (dn->dn_datablksz >= osd->od_max_blksz)
678 down_write(&obj->oo_guard);
680 blksz = dn->dn_datablksz;
681 if (blksz >= osd->od_max_blksz) /* check again after grabbing lock */
682 GOTO(out_unlock, rc);
684 /* now ZFS can support up to 16MB block size, and if the write
685 * is sequential, it just increases the block size gradually */
686 if (start <= blksz) { /* sequential */
687 blksz = (uint32_t)min_t(uint64_t, osd->od_max_blksz, end);
688 } else { /* sparse, pick a block size by write region */
689 blksz = (uint32_t)min_t(uint64_t, osd->od_max_blksz,
693 if (!is_power_of_2(blksz))
694 blksz = size_roundup_power2(blksz);
696 if (blksz > dn->dn_datablksz) {
697 rc = -dmu_object_set_blocksize(osd->od_os, dn->dn_object,
698 blksz, 0, oh->ot_tx);
699 LASSERT(ergo(rc == 0, dn->dn_datablksz >= blksz));
701 CDEBUG(D_INODE, "object "DFID": change block size"
702 "%u -> %u error rc = %d\n",
703 PFID(lu_object_fid(&obj->oo_dt.do_lu)),
704 dn->dn_datablksz, blksz, rc);
708 up_write(&obj->oo_guard);
713 static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
714 struct niobuf_local *lnb, int npages,
717 struct osd_object *obj = osd_dt_obj(dt);
718 struct osd_device *osd = osd_obj2dev(obj);
719 struct osd_thandle *oh;
720 uint64_t new_size = 0;
722 unsigned long iosize = 0;
725 LASSERT(dt_object_exists(dt));
729 oh = container_of0(th, struct osd_thandle, ot_super);
731 /* adjust block size. Assume the buffers are sorted. */
732 (void)osd_grow_blocksize(obj, oh, lnb[0].lnb_file_offset,
733 lnb[npages - 1].lnb_file_offset +
734 lnb[npages - 1].lnb_len);
736 /* LU-8791: take oo_guard to avoid the deadlock that changing block
737 * size and assigning arcbuf take place at the same time.
741 * -> osd_grow_blocksize() with osd_object::oo_guard held
742 * -> dmu_object_set_blocksize()
743 * -> dnode_set_blksz(), with dnode_t::dn_struct_rwlock
746 * -> dmu_buf_will_dirty()
748 * -> wait for the dbuf state to change
751 * -> dmu_assign_arcbuf()
752 * -> dbuf_assign_arcbuf(), set dbuf state to DB_FILL
754 * -> try to hold the read lock of dnode_t::dn_struct_rwlock
756 * By taking the read lock, it can avoid thread 2 to enter into the
757 * critical section of assigning the arcbuf, while thread 1 is
758 * changing the block size.
760 down_read(&obj->oo_guard);
761 for (i = 0; i < npages; i++) {
762 CDEBUG(D_INODE, "write %u bytes at %u\n",
763 (unsigned) lnb[i].lnb_len,
764 (unsigned) lnb[i].lnb_file_offset);
767 /* ENOSPC, network RPC error, etc.
768 * Unlike ldiskfs, zfs allocates new blocks on rewrite,
769 * so we skip this page if lnb_rc is set to -ENOSPC */
770 CDEBUG(D_INODE, "obj "DFID": skipping lnb[%u]: rc=%d\n",
771 PFID(lu_object_fid(&dt->do_lu)), i,
776 if (lnb[i].lnb_page->mapping == (void *)obj) {
777 dmu_write(osd->od_os, obj->oo_dn->dn_object,
778 lnb[i].lnb_file_offset, lnb[i].lnb_len,
779 kmap(lnb[i].lnb_page), oh->ot_tx);
780 kunmap(lnb[i].lnb_page);
781 } else if (lnb[i].lnb_data) {
782 LASSERT(((unsigned long)lnb[i].lnb_data & 1) == 0);
783 /* buffer loaned for zerocopy, try to use it.
784 * notice that dmu_assign_arcbuf() is smart
785 * enough to recognize changed blocksize
786 * in this case it fallbacks to dmu_write() */
787 dmu_assign_arcbuf(&obj->oo_dn->dn_bonus->db,
788 lnb[i].lnb_file_offset,
789 lnb[i].lnb_data, oh->ot_tx);
790 /* drop the reference, otherwise osd_put_bufs()
791 * will be releasing it - bad! */
792 lnb[i].lnb_data = NULL;
793 atomic_dec(&osd->od_zerocopy_loan);
796 if (new_size < lnb[i].lnb_file_offset + lnb[i].lnb_len)
797 new_size = lnb[i].lnb_file_offset + lnb[i].lnb_len;
798 iosize += lnb[i].lnb_len;
800 up_read(&obj->oo_guard);
802 if (unlikely(new_size == 0)) {
803 /* no pages to write, no transno is needed */
805 /* it is important to return 0 even when all lnb_rc == -ENOSPC
806 * since ofd_commitrw_write() retries several times on ENOSPC */
807 record_end_io(osd, WRITE, 0, 0, 0);
811 write_lock(&obj->oo_attr_lock);
812 if (obj->oo_attr.la_size < new_size) {
813 obj->oo_attr.la_size = new_size;
814 write_unlock(&obj->oo_attr_lock);
815 /* osd_object_sa_update() will be copying directly from
816 * oo_attr into dbuf. any update within a single txg will copy
818 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
819 &obj->oo_attr.la_size, 8, oh);
821 write_unlock(&obj->oo_attr_lock);
824 record_end_io(osd, WRITE, 0, iosize, npages);
829 static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
830 struct niobuf_local *lnb, int npages)
832 struct osd_object *obj = osd_dt_obj(dt);
836 LASSERT(dt_object_exists(dt));
839 read_lock(&obj->oo_attr_lock);
840 eof = obj->oo_attr.la_size;
841 read_unlock(&obj->oo_attr_lock);
843 for (i = 0; i < npages; i++) {
844 if (unlikely(lnb[i].lnb_rc < 0))
847 lnb[i].lnb_rc = lnb[i].lnb_len;
849 if (lnb[i].lnb_file_offset + lnb[i].lnb_len >= eof) {
850 if (eof <= lnb[i].lnb_file_offset)
853 lnb[i].lnb_rc = eof - lnb[i].lnb_file_offset;
855 /* all subsequent rc should be 0 */
866 * Punch/truncate an object
868 * IN: db - dmu_buf of the object to free data in.
869 * off - start of section to free.
870 * len - length of section to free (DMU_OBJECT_END => to EOF).
872 * RETURN: 0 if success
873 * error code if failure
875 * The transaction passed to this routine must have
876 * dmu_tx_hold_sa() and if off < size, dmu_tx_hold_free()
877 * called and then assigned to a transaction group.
879 static int __osd_object_punch(objset_t *os, dnode_t *dn, dmu_tx_t *tx,
880 uint64_t size, uint64_t off, uint64_t len)
884 /* Assert that the transaction has been assigned to a
885 transaction group. */
886 LASSERT(tx->tx_txg != 0);
888 * Nothing to do if file already at desired length.
890 if (len == DMU_OBJECT_END && size == off)
893 /* XXX: dnode_free_range() can be used to save on dnode lookup */
895 dmu_free_range(os, dn->dn_object, off, len, tx);
900 static int osd_punch(const struct lu_env *env, struct dt_object *dt,
901 __u64 start, __u64 end, struct thandle *th)
903 struct osd_object *obj = osd_dt_obj(dt);
904 struct osd_device *osd = osd_obj2dev(obj);
905 struct osd_thandle *oh;
910 LASSERT(dt_object_exists(dt));
911 LASSERT(osd_invariant(obj));
914 oh = container_of0(th, struct osd_thandle, ot_super);
916 write_lock(&obj->oo_attr_lock);
918 if (end == OBD_OBJECT_EOF || end >= obj->oo_attr.la_size)
919 len = DMU_OBJECT_END;
922 write_unlock(&obj->oo_attr_lock);
924 rc = __osd_object_punch(osd->od_os, obj->oo_dn, oh->ot_tx,
925 obj->oo_attr.la_size, start, len);
927 if (len == DMU_OBJECT_END) {
928 write_lock(&obj->oo_attr_lock);
929 obj->oo_attr.la_size = start;
930 write_unlock(&obj->oo_attr_lock);
931 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
932 &obj->oo_attr.la_size, 8, oh);
937 static int osd_declare_punch(const struct lu_env *env, struct dt_object *dt,
938 __u64 start, __u64 end, struct thandle *handle)
940 struct osd_object *obj = osd_dt_obj(dt);
941 struct osd_device *osd = osd_obj2dev(obj);
942 struct osd_thandle *oh;
946 oh = container_of0(handle, struct osd_thandle, ot_super);
948 read_lock(&obj->oo_attr_lock);
949 if (end == OBD_OBJECT_EOF || end >= obj->oo_attr.la_size)
950 len = DMU_OBJECT_END;
954 /* declare we'll free some blocks ... */
955 if (start < obj->oo_attr.la_size) {
956 read_unlock(&obj->oo_attr_lock);
957 dmu_tx_hold_free(oh->ot_tx, obj->oo_dn->dn_object, start, len);
959 read_unlock(&obj->oo_attr_lock);
962 /* ... and we'll modify size attribute */
963 dmu_tx_hold_sa(oh->ot_tx, obj->oo_sa_hdl, 0);
965 RETURN(osd_declare_quota(env, osd, obj->oo_attr.la_uid,
966 obj->oo_attr.la_gid, 0, oh, true, NULL,
970 static int osd_ladvise(const struct lu_env *env, struct dt_object *dt,
971 __u64 start, __u64 end, enum lu_ladvise_type advice)
985 struct dt_body_operations osd_body_ops = {
986 .dbo_read = osd_read,
987 .dbo_declare_write = osd_declare_write,
988 .dbo_write = osd_write,
989 .dbo_bufs_get = osd_bufs_get,
990 .dbo_bufs_put = osd_bufs_put,
991 .dbo_write_prep = osd_write_prep,
992 .dbo_declare_write_commit = osd_declare_write_commit,
993 .dbo_write_commit = osd_write_commit,
994 .dbo_read_prep = osd_read_prep,
995 .dbo_declare_punch = osd_declare_punch,
996 .dbo_punch = osd_punch,
997 .dbo_ladvise = osd_ladvise,