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 <libcfs/libcfs.h>
41 #include <obd_support.h>
42 #include <lustre_net.h>
44 #include <obd_class.h>
45 #include <lustre_disk.h>
46 #include <lustre_fid.h>
47 #include <lustre_quota.h>
49 #include "osd_internal.h"
51 #include <sys/dnode.h>
56 #include <sys/spa_impl.h>
57 #include <sys/zfs_znode.h>
58 #include <sys/dmu_tx.h>
59 #include <sys/dmu_objset.h>
60 #include <sys/dsl_prop.h>
61 #include <sys/sa_impl.h>
64 static char *osd_0copy_tag = "zerocopy";
67 static void record_start_io(struct osd_device *osd, int rw, int discont_pages)
69 struct obd_histogram *h = osd->od_brw_stats.hist;
72 atomic_inc(&osd->od_r_in_flight);
73 lprocfs_oh_tally(&h[BRW_R_RPC_HIST],
74 atomic_read(&osd->od_r_in_flight));
75 lprocfs_oh_tally(&h[BRW_R_DISCONT_PAGES], discont_pages);
78 atomic_inc(&osd->od_w_in_flight);
79 lprocfs_oh_tally(&h[BRW_W_RPC_HIST],
80 atomic_read(&osd->od_w_in_flight));
81 lprocfs_oh_tally(&h[BRW_W_DISCONT_PAGES], discont_pages);
86 static void record_end_io(struct osd_device *osd, int rw,
87 unsigned long elapsed, int disksize, int npages)
89 struct obd_histogram *h = osd->od_brw_stats.hist;
92 atomic_dec(&osd->od_r_in_flight);
93 lprocfs_oh_tally_log2(&h[BRW_R_PAGES], npages);
95 lprocfs_oh_tally_log2(&h[BRW_R_DISK_IOSIZE], disksize);
97 lprocfs_oh_tally_log2(&h[BRW_R_IO_TIME], elapsed);
100 atomic_dec(&osd->od_w_in_flight);
101 lprocfs_oh_tally_log2(&h[BRW_W_PAGES], npages);
103 lprocfs_oh_tally_log2(&h[BRW_W_DISK_IOSIZE], disksize);
105 lprocfs_oh_tally_log2(&h[BRW_W_IO_TIME], elapsed);
109 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
110 struct lu_buf *buf, loff_t *pos)
112 struct osd_object *obj = osd_dt_obj(dt);
113 struct osd_device *osd = osd_obj2dev(obj);
115 int size = buf->lb_len;
119 LASSERT(dt_object_exists(dt));
122 start = cfs_time_current();
124 read_lock(&obj->oo_attr_lock);
125 old_size = obj->oo_attr.la_size;
126 read_unlock(&obj->oo_attr_lock);
128 if (*pos + size > old_size) {
132 size = old_size - *pos;
135 record_start_io(osd, READ, 0);
137 rc = osd_dmu_read(osd, obj->oo_dn, *pos, size, buf->lb_buf,
140 record_end_io(osd, READ, cfs_time_current() - start, size,
149 static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
150 const struct lu_buf *buf, loff_t pos,
153 struct osd_object *obj = osd_dt_obj(dt);
154 struct osd_device *osd = osd_obj2dev(obj);
155 struct osd_thandle *oh;
159 oh = container_of0(th, struct osd_thandle, ot_super);
161 /* in some cases declare can race with creation (e.g. llog)
162 * and we need to wait till object is initialized. notice
163 * LOHA_EXISTs is supposed to be the last step in the
166 /* size change (in dnode) will be declared by dmu_tx_hold_write() */
167 if (dt_object_exists(dt))
168 oid = obj->oo_dn->dn_object;
170 oid = DMU_NEW_OBJECT;
172 /* XXX: we still miss for append declaration support in ZFS
173 * -1 means append which is used by llog mostly, llog
174 * can grow upto LLOG_MIN_CHUNK_SIZE*8 records */
176 pos = max_t(loff_t, 256 * 8 * LLOG_MIN_CHUNK_SIZE,
177 obj->oo_attr.la_size + (2 << 20));
178 osd_tx_hold_write(oh->ot_tx, oid, obj->oo_dn, pos, buf->lb_len);
180 /* dt_declare_write() is usually called for system objects, such
181 * as llog or last_rcvd files. We needn't enforce quota on those
182 * objects, so always set the lqi_space as 0. */
183 RETURN(osd_declare_quota(env, osd, obj->oo_attr.la_uid,
184 obj->oo_attr.la_gid, obj->oo_attr.la_projid,
185 0, oh, NULL, OSD_QID_BLK));
188 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
189 const struct lu_buf *buf, loff_t *pos,
190 struct thandle *th, int ignore_quota)
192 struct osd_object *obj = osd_dt_obj(dt);
193 struct osd_device *osd = osd_obj2dev(obj);
194 struct osd_thandle *oh;
195 uint64_t offset = *pos;
200 LASSERT(dt_object_exists(dt));
204 oh = container_of0(th, struct osd_thandle, ot_super);
206 osd_dmu_write(osd, obj->oo_dn, offset, (uint64_t)buf->lb_len,
207 buf->lb_buf, oh->ot_tx);
208 write_lock(&obj->oo_attr_lock);
209 if (obj->oo_attr.la_size < offset + buf->lb_len) {
210 obj->oo_attr.la_size = offset + buf->lb_len;
211 write_unlock(&obj->oo_attr_lock);
212 /* osd_object_sa_update() will be copying directly from oo_attr
213 * into dbuf. any update within a single txg will copy the
215 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
216 &obj->oo_attr.la_size, 8, oh);
220 write_unlock(&obj->oo_attr_lock);
231 * XXX: for the moment I don't want to use lnb_flags for osd-internal
232 * purposes as it's not very well defined ...
233 * instead I use the lowest bit of the address so that:
234 * arc buffer: .lnb_data = abuf (arc we loan for write)
235 * dbuf buffer: .lnb_data = dbuf | 1 (dbuf we get for read)
236 * copy buffer: .lnb_page->mapping = obj (page we allocate for write)
240 static int osd_bufs_put(const struct lu_env *env, struct dt_object *dt,
241 struct niobuf_local *lnb, int npages)
243 struct osd_object *obj = osd_dt_obj(dt);
244 struct osd_device *osd = osd_obj2dev(obj);
248 LASSERT(dt_object_exists(dt));
251 for (i = 0; i < npages; i++) {
252 if (lnb[i].lnb_page == NULL)
254 if (lnb[i].lnb_page->mapping == (void *)obj) {
255 /* this is anonymous page allocated for copy-write */
256 lnb[i].lnb_page->mapping = NULL;
257 __free_page(lnb[i].lnb_page);
258 atomic_dec(&osd->od_zerocopy_alloc);
260 /* see comment in osd_bufs_get_read() */
261 ptr = (unsigned long)lnb[i].lnb_data;
264 dmu_buf_rele((void *)ptr, osd_0copy_tag);
265 atomic_dec(&osd->od_zerocopy_pin);
266 } else if (lnb[i].lnb_data != NULL) {
267 int j, apages, abufsz;
268 abufsz = arc_buf_size(lnb[i].lnb_data);
269 apages = abufsz / PAGE_SIZE;
270 /* these references to pages must be invalidated
271 * to prevent access in osd_bufs_put() */
272 for (j = 0; j < apages; j++)
273 lnb[i + j].lnb_page = NULL;
274 dmu_return_arcbuf(lnb[i].lnb_data);
275 atomic_dec(&osd->od_zerocopy_loan);
278 lnb[i].lnb_page = NULL;
279 lnb[i].lnb_data = NULL;
285 static inline struct page *kmem_to_page(void *addr)
287 LASSERT(!((unsigned long)addr & ~PAGE_MASK));
288 if (is_vmalloc_addr(addr))
289 return vmalloc_to_page(addr);
291 return virt_to_page(addr);
295 * Prepare buffers for read.
297 * The function maps the range described by \a off and \a len to \a lnb array.
298 * dmu_buf_hold_array_by_bonus() finds/creates appropriate ARC buffers, then
299 * we fill \a lnb array with the pages storing ARC buffers. Notice the current
300 * implementationt passes TRUE to dmu_buf_hold_array_by_bonus() to fill ARC
301 * buffers with actual data, I/O is done in the conext of osd_bufs_get_read().
302 * A better implementation would just return the buffers (potentially unfilled)
303 * and subsequent osd_read_prep() would do I/O for many ranges concurrently.
305 * \param[in] env environment
306 * \param[in] obj object
307 * \param[in] off offset in bytes
308 * \param[in] len the number of bytes to access
309 * \param[out] lnb array of local niobufs pointing to the buffers with data
311 * \retval 0 for success
312 * \retval negative error number of failure
314 static int osd_bufs_get_read(const struct lu_env *env, struct osd_object *obj,
315 loff_t off, ssize_t len, struct niobuf_local *lnb)
317 struct osd_device *osd = osd_obj2dev(obj);
318 unsigned long start = cfs_time_current();
319 int rc, i, numbufs, npages = 0;
323 record_start_io(osd, READ, 0);
325 /* grab buffers for read:
326 * OSD API let us to grab buffers first, then initiate IO(s)
327 * so that all required IOs will be done in parallel, but at the
328 * moment DMU doesn't provide us with a method to grab buffers.
329 * If we discover this is a vital for good performance we
330 * can get own replacement for dmu_buf_hold_array_by_bonus().
333 rc = -dmu_buf_hold_array_by_bonus(&obj->oo_dn->dn_bonus->db,
334 off, len, TRUE, osd_0copy_tag,
339 for (i = 0; i < numbufs; i++) {
340 int bufoff, tocpy, thispage;
345 atomic_inc(&osd->od_zerocopy_pin);
347 bufoff = off - dbp[i]->db_offset;
348 tocpy = min_t(int, dbp[i]->db_size - bufoff, len);
350 /* kind of trick to differentiate dbuf vs. arcbuf */
351 LASSERT(((unsigned long)dbp[i] & 1) == 0);
352 dbf = (void *) ((unsigned long)dbp[i] | 1);
355 thispage = PAGE_SIZE;
356 thispage -= bufoff & (PAGE_SIZE - 1);
357 thispage = min(tocpy, thispage);
360 lnb->lnb_file_offset = off;
361 lnb->lnb_page_offset = bufoff & ~PAGE_MASK;
362 lnb->lnb_len = thispage;
363 lnb->lnb_page = kmem_to_page(dbp[i]->db_data +
365 /* mark just a single slot: we need this
366 * reference to dbuf to be released once */
379 /* steal dbuf so dmu_buf_rele_array() can't release
384 dmu_buf_rele_array(dbp, numbufs, osd_0copy_tag);
387 record_end_io(osd, READ, cfs_time_current() - start,
388 npages * PAGE_SIZE, npages);
394 osd_bufs_put(env, &obj->oo_dt, lnb - npages, npages);
398 static inline arc_buf_t *osd_request_arcbuf(dnode_t *dn, size_t bs)
402 abuf = dmu_request_arcbuf(&dn->dn_bonus->db, bs);
404 return ERR_PTR(-ENOMEM);
406 #if ZFS_VERSION_CODE < OBD_OCD_VERSION(0, 7, 0, 0)
408 * ZFS prior to 0.7.0 doesn't guarantee PAGE_SIZE alignment for zio
409 * blocks smaller than (PAGE_SIZE << 2). This poses a problem of
410 * setting up page array for RDMA transfer. See LU-9305.
412 if ((unsigned long)abuf->b_data & ~PAGE_MASK) {
413 dmu_return_arcbuf(abuf);
421 static int osd_bufs_get_write(const struct lu_env *env, struct osd_object *obj,
422 loff_t off, ssize_t len, struct niobuf_local *lnb)
424 struct osd_device *osd = osd_obj2dev(obj);
425 int plen, off_in_block, sz_in_block;
426 int rc, i = 0, npages = 0;
427 dnode_t *dn = obj->oo_dn;
429 uint32_t bs = dn->dn_datablksz;
433 * currently only full blocks are subject to zerocopy approach:
434 * so that we're sure nobody is trying to update the same block
437 LASSERT(npages < PTLRPC_MAX_BRW_PAGES);
439 off_in_block = off & (bs - 1);
440 sz_in_block = min_t(int, bs - off_in_block, len);
443 if (sz_in_block == bs) {
444 /* full block, try to use zerocopy */
445 abuf = osd_request_arcbuf(dn, bs);
446 if (unlikely(IS_ERR(abuf)))
447 GOTO(out_err, rc = PTR_ERR(abuf));
451 atomic_inc(&osd->od_zerocopy_loan);
453 /* go over pages arcbuf contains, put them as
454 * local niobufs for ptlrpc's bulks */
455 while (sz_in_block > 0) {
456 plen = min_t(int, sz_in_block, PAGE_SIZE);
458 lnb[i].lnb_file_offset = off;
459 lnb[i].lnb_page_offset = 0;
460 lnb[i].lnb_len = plen;
462 if (sz_in_block == bs)
463 lnb[i].lnb_data = abuf;
465 lnb[i].lnb_data = NULL;
467 /* this one is not supposed to fail */
468 lnb[i].lnb_page = kmem_to_page(abuf->b_data +
470 LASSERT(lnb[i].lnb_page);
472 lprocfs_counter_add(osd->od_stats,
473 LPROC_OSD_ZEROCOPY_IO, 1);
478 off_in_block += plen;
483 if (off_in_block == 0 && len < bs &&
484 off + len >= obj->oo_attr.la_size)
485 lprocfs_counter_add(osd->od_stats,
486 LPROC_OSD_TAIL_IO, 1);
488 /* can't use zerocopy, allocate temp. buffers */
489 while (sz_in_block > 0) {
490 plen = min_t(int, sz_in_block, PAGE_SIZE);
492 lnb[i].lnb_file_offset = off;
493 lnb[i].lnb_page_offset = 0;
494 lnb[i].lnb_len = plen;
496 lnb[i].lnb_data = NULL;
498 lnb[i].lnb_page = alloc_page(OSD_GFP_IO);
499 if (unlikely(lnb[i].lnb_page == NULL))
500 GOTO(out_err, rc = -ENOMEM);
502 LASSERT(lnb[i].lnb_page->mapping == NULL);
503 lnb[i].lnb_page->mapping = (void *)obj;
505 atomic_inc(&osd->od_zerocopy_alloc);
506 lprocfs_counter_add(osd->od_stats,
507 LPROC_OSD_COPY_IO, 1);
521 osd_bufs_put(env, &obj->oo_dt, lnb, npages);
525 static int osd_bufs_get(const struct lu_env *env, struct dt_object *dt,
526 loff_t offset, ssize_t len, struct niobuf_local *lnb,
527 enum dt_bufs_type rw)
529 struct osd_object *obj = osd_dt_obj(dt);
532 LASSERT(dt_object_exists(dt));
535 if (rw & DT_BUFS_TYPE_WRITE)
536 rc = osd_bufs_get_write(env, obj, offset, len, lnb);
538 rc = osd_bufs_get_read(env, obj, offset, len, lnb);
543 static int osd_write_prep(const struct lu_env *env, struct dt_object *dt,
544 struct niobuf_local *lnb, int npages)
546 struct osd_object *obj = osd_dt_obj(dt);
548 LASSERT(dt_object_exists(dt));
554 static inline uint64_t osd_roundup2blocksz(uint64_t size,
560 size += offset % blksz;
562 if (likely(is_power_of_2(blksz)))
563 return PO2_ROUNDUP_TYPED(size, blksz, uint64_t);
570 static int osd_declare_write_commit(const struct lu_env *env,
571 struct dt_object *dt,
572 struct niobuf_local *lnb, int npages,
575 struct osd_object *obj = osd_dt_obj(dt);
576 struct osd_device *osd = osd_obj2dev(obj);
577 struct osd_thandle *oh;
580 uint32_t blksz = obj->oo_dn->dn_datablksz;
581 int i, rc, flags = 0;
584 struct page *last_page = NULL;
585 unsigned long discont_pages = 0;
586 enum osd_qid_declare_flags declare_flags = OSD_QID_BLK;
589 LASSERT(dt_object_exists(dt));
595 oh = container_of0(th, struct osd_thandle, ot_super);
597 for (i = 0; i < npages; i++) {
598 if (last_page && lnb[i].lnb_page->index != (last_page->index + 1))
600 last_page = lnb[i].lnb_page;
602 /* ENOSPC, network RPC error, etc.
603 * We don't want to book space for pages which will be
604 * skipped in osd_write_commit(). Hence we skip pages
605 * with lnb_rc != 0 here too */
607 /* ignore quota for the whole request if any page is from
608 * client cache or written by root.
610 * XXX once we drop the 1.8 client support, the checking
611 * for whether page is from cache can be simplified as:
612 * !(lnb[i].flags & OBD_BRW_SYNC)
614 * XXX we could handle this on per-lnb basis as done by
616 if ((lnb[i].lnb_flags & OBD_BRW_NOQUOTA) ||
617 (lnb[i].lnb_flags & (OBD_BRW_FROM_GRANT | OBD_BRW_SYNC)) ==
619 declare_flags |= OSD_QID_FORCE;
622 /* first valid lnb */
623 offset = lnb[i].lnb_file_offset;
624 size = lnb[i].lnb_len;
627 if (offset + size == lnb[i].lnb_file_offset) {
628 /* this lnb is contiguous to the previous one */
629 size += lnb[i].lnb_len;
633 osd_tx_hold_write(oh->ot_tx, obj->oo_dn->dn_object,
634 obj->oo_dn, offset, size);
635 /* Estimating space to be consumed by a write is rather
636 * complicated with ZFS. As a consequence, we don't account for
637 * indirect blocks and just use as a rough estimate the worse
638 * case where the old space is being held by a snapshot. Quota
639 * overrun will be adjusted once the operation is committed, if
641 space += osd_roundup2blocksz(size, offset, blksz);
643 offset = lnb[i].lnb_file_offset;
644 size = lnb[i].lnb_len;
648 osd_tx_hold_write(oh->ot_tx, obj->oo_dn->dn_object, obj->oo_dn,
650 space += osd_roundup2blocksz(size, offset, blksz);
653 oh->ot_write_commit = 1; /* used in osd_trans_start() for fail_loc */
655 /* backend zfs filesystem might be configured to store multiple data
657 space *= osd->od_os->os_copies;
659 CDEBUG(D_QUOTA, "writing %d pages, reserving %lldK of quota space\n",
662 record_start_io(osd, WRITE, discont_pages);
664 /* acquire quota space if needed */
665 rc = osd_declare_quota(env, osd, obj->oo_attr.la_uid,
666 obj->oo_attr.la_gid, obj->oo_attr.la_projid,
667 space, oh, &flags, declare_flags);
669 if (!synced && rc == -EDQUOT && (flags & QUOTA_FL_SYNC) != 0) {
670 dt_sync(env, th->th_dev);
672 CDEBUG(D_QUOTA, "retry after sync\n");
677 /* we need only to store the overquota flags in the first lnb for
678 * now, once we support multiple objects BRW, this code needs be
680 if (flags & QUOTA_FL_OVER_USRQUOTA)
681 lnb[0].lnb_flags |= OBD_BRW_OVER_USRQUOTA;
682 if (flags & QUOTA_FL_OVER_GRPQUOTA)
683 lnb[0].lnb_flags |= OBD_BRW_OVER_GRPQUOTA;
684 #ifdef ZFS_PROJINHERIT
685 if (flags & QUOTA_FL_OVER_PRJQUOTA)
686 lnb[0].lnb_flags |= OBD_BRW_OVER_PRJQUOTA;
693 * Policy to grow ZFS block size by write pattern.
694 * For sequential write, it grows block size gradually until it reaches the
695 * maximum blocksize the dataset can support. Otherwise, it will pick a
696 * a block size by the writing region of this I/O.
698 static int osd_grow_blocksize(struct osd_object *obj, struct osd_thandle *oh,
699 uint64_t start, uint64_t end)
701 struct osd_device *osd = osd_obj2dev(obj);
702 dnode_t *dn = obj->oo_dn;
708 if (dn->dn_maxblkid > 0) /* can't change block size */
711 if (dn->dn_datablksz >= osd->od_max_blksz)
714 down_write(&obj->oo_guard);
716 blksz = dn->dn_datablksz;
717 if (blksz >= osd->od_max_blksz) /* check again after grabbing lock */
718 GOTO(out_unlock, rc);
720 /* now ZFS can support up to 16MB block size, and if the write
721 * is sequential, it just increases the block size gradually */
722 if (start <= blksz) { /* sequential */
723 blksz = (uint32_t)min_t(uint64_t, osd->od_max_blksz, end);
724 } else { /* sparse, pick a block size by write region */
725 blksz = (uint32_t)min_t(uint64_t, osd->od_max_blksz,
729 if (!is_power_of_2(blksz))
730 blksz = size_roundup_power2(blksz);
732 if (blksz > dn->dn_datablksz) {
733 rc = -dmu_object_set_blocksize(osd->od_os, dn->dn_object,
734 blksz, 0, oh->ot_tx);
735 LASSERT(ergo(rc == 0, dn->dn_datablksz >= blksz));
737 CDEBUG(D_INODE, "object "DFID": change block size"
738 "%u -> %u error rc = %d\n",
739 PFID(lu_object_fid(&obj->oo_dt.do_lu)),
740 dn->dn_datablksz, blksz, rc);
744 up_write(&obj->oo_guard);
749 static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
750 struct niobuf_local *lnb, int npages,
753 struct osd_object *obj = osd_dt_obj(dt);
754 struct osd_device *osd = osd_obj2dev(obj);
755 struct osd_thandle *oh;
756 uint64_t new_size = 0;
758 unsigned long iosize = 0;
761 LASSERT(dt_object_exists(dt));
765 oh = container_of0(th, struct osd_thandle, ot_super);
767 /* adjust block size. Assume the buffers are sorted. */
768 (void)osd_grow_blocksize(obj, oh, lnb[0].lnb_file_offset,
769 lnb[npages - 1].lnb_file_offset +
770 lnb[npages - 1].lnb_len);
772 /* LU-8791: take oo_guard to avoid the deadlock that changing block
773 * size and assigning arcbuf take place at the same time.
777 * -> osd_grow_blocksize() with osd_object::oo_guard held
778 * -> dmu_object_set_blocksize()
779 * -> dnode_set_blksz(), with dnode_t::dn_struct_rwlock
782 * -> dmu_buf_will_dirty()
784 * -> wait for the dbuf state to change
787 * -> dmu_assign_arcbuf()
788 * -> dbuf_assign_arcbuf(), set dbuf state to DB_FILL
790 * -> try to hold the read lock of dnode_t::dn_struct_rwlock
792 * By taking the read lock, it can avoid thread 2 to enter into the
793 * critical section of assigning the arcbuf, while thread 1 is
794 * changing the block size.
796 down_read(&obj->oo_guard);
797 for (i = 0; i < npages; i++) {
798 CDEBUG(D_INODE, "write %u bytes at %u\n",
799 (unsigned) lnb[i].lnb_len,
800 (unsigned) lnb[i].lnb_file_offset);
803 /* ENOSPC, network RPC error, etc.
804 * Unlike ldiskfs, zfs allocates new blocks on rewrite,
805 * so we skip this page if lnb_rc is set to -ENOSPC */
806 CDEBUG(D_INODE, "obj "DFID": skipping lnb[%u]: rc=%d\n",
807 PFID(lu_object_fid(&dt->do_lu)), i,
812 if (new_size < lnb[i].lnb_file_offset + lnb[i].lnb_len)
813 new_size = lnb[i].lnb_file_offset + lnb[i].lnb_len;
814 if (lnb[i].lnb_page == NULL)
817 if (lnb[i].lnb_page->mapping == (void *)obj) {
818 osd_dmu_write(osd, obj->oo_dn, lnb[i].lnb_file_offset,
819 lnb[i].lnb_len, kmap(lnb[i].lnb_page),
821 kunmap(lnb[i].lnb_page);
822 iosize += lnb[i].lnb_len;
823 } else if (lnb[i].lnb_data) {
824 int j, apages, abufsz;
825 LASSERT(((unsigned long)lnb[i].lnb_data & 1) == 0);
826 /* buffer loaned for zerocopy, try to use it.
827 * notice that dmu_assign_arcbuf() is smart
828 * enough to recognize changed blocksize
829 * in this case it fallbacks to dmu_write() */
830 abufsz = arc_buf_size(lnb[i].lnb_data);
831 LASSERT(abufsz & PAGE_MASK);
832 apages = abufsz / PAGE_SIZE;
833 LASSERT(i + apages <= npages);
834 /* these references to pages must be invalidated
835 * to prevent access in osd_bufs_put() */
836 for (j = 0; j < apages; j++)
837 lnb[i + j].lnb_page = NULL;
838 dmu_assign_arcbuf(&obj->oo_dn->dn_bonus->db,
839 lnb[i].lnb_file_offset,
840 lnb[i].lnb_data, oh->ot_tx);
841 /* drop the reference, otherwise osd_put_bufs()
842 * will be releasing it - bad! */
843 lnb[i].lnb_data = NULL;
844 atomic_dec(&osd->od_zerocopy_loan);
849 up_read(&obj->oo_guard);
851 if (unlikely(new_size == 0)) {
852 /* no pages to write, no transno is needed */
854 /* it is important to return 0 even when all lnb_rc == -ENOSPC
855 * since ofd_commitrw_write() retries several times on ENOSPC */
856 record_end_io(osd, WRITE, 0, 0, 0);
860 write_lock(&obj->oo_attr_lock);
861 if (obj->oo_attr.la_size < new_size) {
862 obj->oo_attr.la_size = new_size;
863 write_unlock(&obj->oo_attr_lock);
864 /* osd_object_sa_update() will be copying directly from
865 * oo_attr into dbuf. any update within a single txg will copy
867 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
868 &obj->oo_attr.la_size, 8, oh);
870 write_unlock(&obj->oo_attr_lock);
873 record_end_io(osd, WRITE, 0, iosize, npages);
878 static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
879 struct niobuf_local *lnb, int npages)
881 struct osd_object *obj = osd_dt_obj(dt);
885 LASSERT(dt_object_exists(dt));
888 read_lock(&obj->oo_attr_lock);
889 eof = obj->oo_attr.la_size;
890 read_unlock(&obj->oo_attr_lock);
892 for (i = 0; i < npages; i++) {
893 if (unlikely(lnb[i].lnb_rc < 0))
896 lnb[i].lnb_rc = lnb[i].lnb_len;
898 if (lnb[i].lnb_file_offset + lnb[i].lnb_len >= eof) {
899 if (eof <= lnb[i].lnb_file_offset)
902 lnb[i].lnb_rc = eof - lnb[i].lnb_file_offset;
904 /* all subsequent rc should be 0 */
915 * Punch/truncate an object
917 * IN: db - dmu_buf of the object to free data in.
918 * off - start of section to free.
919 * len - length of section to free (DMU_OBJECT_END => to EOF).
921 * RETURN: 0 if success
922 * error code if failure
924 * The transaction passed to this routine must have
925 * dmu_tx_hold_sa() and if off < size, dmu_tx_hold_free()
926 * called and then assigned to a transaction group.
928 static int __osd_object_punch(objset_t *os, dnode_t *dn, dmu_tx_t *tx,
929 uint64_t size, uint64_t off, uint64_t len)
933 /* Assert that the transaction has been assigned to a
934 transaction group. */
935 LASSERT(tx->tx_txg != 0);
937 * Nothing to do if file already at desired length.
939 if (len == DMU_OBJECT_END && size == off)
942 /* XXX: dnode_free_range() can be used to save on dnode lookup */
944 dmu_free_range(os, dn->dn_object, off, len, tx);
949 static int osd_punch(const struct lu_env *env, struct dt_object *dt,
950 __u64 start, __u64 end, struct thandle *th)
952 struct osd_object *obj = osd_dt_obj(dt);
953 struct osd_device *osd = osd_obj2dev(obj);
954 struct osd_thandle *oh;
959 LASSERT(dt_object_exists(dt));
960 LASSERT(osd_invariant(obj));
963 oh = container_of0(th, struct osd_thandle, ot_super);
965 write_lock(&obj->oo_attr_lock);
967 if (end == OBD_OBJECT_EOF || end >= obj->oo_attr.la_size)
968 len = DMU_OBJECT_END;
971 write_unlock(&obj->oo_attr_lock);
973 rc = __osd_object_punch(osd->od_os, obj->oo_dn, oh->ot_tx,
974 obj->oo_attr.la_size, start, len);
976 if (len == DMU_OBJECT_END) {
977 write_lock(&obj->oo_attr_lock);
978 obj->oo_attr.la_size = start;
979 write_unlock(&obj->oo_attr_lock);
980 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
981 &obj->oo_attr.la_size, 8, oh);
986 static int osd_declare_punch(const struct lu_env *env, struct dt_object *dt,
987 __u64 start, __u64 end, struct thandle *handle)
989 struct osd_object *obj = osd_dt_obj(dt);
990 struct osd_device *osd = osd_obj2dev(obj);
991 struct osd_thandle *oh;
995 oh = container_of0(handle, struct osd_thandle, ot_super);
997 read_lock(&obj->oo_attr_lock);
998 if (end == OBD_OBJECT_EOF || end >= obj->oo_attr.la_size)
999 len = DMU_OBJECT_END;
1003 /* declare we'll free some blocks ... */
1004 if (start < obj->oo_attr.la_size) {
1005 read_unlock(&obj->oo_attr_lock);
1006 dmu_tx_hold_free(oh->ot_tx, obj->oo_dn->dn_object, start, len);
1008 read_unlock(&obj->oo_attr_lock);
1011 RETURN(osd_declare_quota(env, osd, obj->oo_attr.la_uid,
1012 obj->oo_attr.la_gid, obj->oo_attr.la_projid,
1013 0, oh, NULL, OSD_QID_BLK));
1016 static int osd_ladvise(const struct lu_env *env, struct dt_object *dt,
1017 __u64 start, __u64 end, enum lu_ladvise_type advice)
1031 struct dt_body_operations osd_body_ops = {
1032 .dbo_read = osd_read,
1033 .dbo_declare_write = osd_declare_write,
1034 .dbo_write = osd_write,
1035 .dbo_bufs_get = osd_bufs_get,
1036 .dbo_bufs_put = osd_bufs_put,
1037 .dbo_write_prep = osd_write_prep,
1038 .dbo_declare_write_commit = osd_declare_write_commit,
1039 .dbo_write_commit = osd_write_commit,
1040 .dbo_read_prep = osd_read_prep,
1041 .dbo_declare_punch = osd_declare_punch,
1042 .dbo_punch = osd_punch,
1043 .dbo_ladvise = osd_ladvise,