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, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/osd-zfs/osd_io.c
33 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
34 * Author: Mike Pershin <tappro@whamcloud.com>
37 #define DEBUG_SUBSYSTEM S_OSD
39 #include <libcfs/libcfs.h>
40 #include <obd_support.h>
41 #include <lustre_net.h>
43 #include <obd_class.h>
44 #include <lustre_disk.h>
45 #include <lustre_fid.h>
46 #include <lustre_quota.h>
48 #include "osd_internal.h"
50 #include <sys/dnode.h>
55 #include <sys/spa_impl.h>
56 #include <sys/zfs_znode.h>
57 #include <sys/dmu_tx.h>
58 #include <sys/dmu_objset.h>
59 #include <sys/dsl_prop.h>
60 #include <sys/sa_impl.h>
63 static char osd_0copy_tag[] = "zerocopy";
65 static void dbuf_set_pending_evict(dmu_buf_t *db)
67 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
68 dbi->db_pending_evict = TRUE;
71 static void record_start_io(struct osd_device *osd, int rw, int discont_pages)
73 struct brw_stats *h = &osd->od_brw_stats;
76 atomic_inc(&osd->od_r_in_flight);
77 lprocfs_oh_tally_pcpu(&h->bs_hist[BRW_R_RPC_HIST],
78 atomic_read(&osd->od_r_in_flight));
79 lprocfs_oh_tally_pcpu(&h->bs_hist[BRW_R_DISCONT_PAGES],
82 atomic_inc(&osd->od_w_in_flight);
83 lprocfs_oh_tally_pcpu(&h->bs_hist[BRW_W_RPC_HIST],
84 atomic_read(&osd->od_w_in_flight));
85 lprocfs_oh_tally_pcpu(&h->bs_hist[BRW_W_DISCONT_PAGES],
90 static void record_end_io(struct osd_device *osd, int rw,
91 unsigned long elapsed, int disksize, int npages)
93 struct brw_stats *h = &osd->od_brw_stats;
96 atomic_dec(&osd->od_r_in_flight);
98 atomic_dec(&osd->od_w_in_flight);
100 lprocfs_oh_tally_log2_pcpu(&h->bs_hist[BRW_R_PAGES + rw], npages);
102 lprocfs_oh_tally_log2_pcpu(&h->bs_hist[BRW_R_DISK_IOSIZE + rw],
105 lprocfs_oh_tally_log2_pcpu(&h->bs_hist[BRW_R_IO_TIME + rw],
109 static ssize_t __osd_read(const struct lu_env *env, struct dt_object *dt,
110 struct lu_buf *buf, loff_t *pos, size_t *size)
112 struct osd_object *obj = osd_dt_obj(dt);
116 LASSERT(dt_object_exists(dt));
119 read_lock(&obj->oo_attr_lock);
120 old_size = obj->oo_attr.la_size;
121 read_unlock(&obj->oo_attr_lock);
123 if (*pos + *size > old_size) {
127 *size = old_size - *pos;
130 rc = osd_dmu_read(osd_obj2dev(obj), obj->oo_dn, *pos, *size,
131 buf->lb_buf, DMU_READ_PREFETCH);
140 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
141 struct lu_buf *buf, loff_t *pos)
143 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
144 size_t size = buf->lb_len;
145 hrtime_t start = gethrtime();
149 record_start_io(osd, READ, 0);
150 rc = __osd_read(env, dt, buf, pos, &size);
151 delta_ms = gethrtime() - start;
152 do_div(delta_ms, NSEC_PER_MSEC);
153 record_end_io(osd, READ, delta_ms, size, size >> PAGE_SHIFT);
158 static inline ssize_t osd_read_no_record(const struct lu_env *env,
159 struct dt_object *dt,
160 struct lu_buf *buf, loff_t *pos)
162 size_t size = buf->lb_len;
164 return __osd_read(env, dt, buf, pos, &size);
167 static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
168 const struct lu_buf *buf, loff_t pos,
171 struct osd_object *obj = osd_dt_obj(dt);
172 struct osd_device *osd = osd_obj2dev(obj);
173 struct osd_thandle *oh;
177 oh = container_of(th, struct osd_thandle, ot_super);
179 /* in some cases declare can race with creation (e.g. llog)
180 * and we need to wait till object is initialized. notice
181 * LOHA_EXISTs is supposed to be the last step in the
184 /* size change (in dnode) will be declared by dmu_tx_hold_write() */
185 if (dt_object_exists(dt))
186 oid = obj->oo_dn->dn_object;
188 oid = DMU_NEW_OBJECT;
190 /* XXX: we still miss for append declaration support in ZFS
191 * -1 means append which is used by llog mostly, llog
192 * can grow upto LLOG_MIN_CHUNK_SIZE*8 records */
194 pos = max_t(loff_t, 256 * 8 * LLOG_MIN_CHUNK_SIZE,
195 obj->oo_attr.la_size + (2 << 20));
196 osd_tx_hold_write(oh->ot_tx, oid, obj->oo_dn, pos, buf->lb_len);
198 /* dt_declare_write() is usually called for system objects, such
199 * as llog or last_rcvd files. We needn't enforce quota on those
200 * objects, so always set the lqi_space as 0. */
201 RETURN(osd_declare_quota(env, osd, obj->oo_attr.la_uid,
202 obj->oo_attr.la_gid, obj->oo_attr.la_projid,
203 0, oh, NULL, OSD_QID_BLK));
206 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
207 const struct lu_buf *buf, loff_t *pos,
210 struct osd_object *obj = osd_dt_obj(dt);
211 struct osd_device *osd = osd_obj2dev(obj);
212 struct osd_thandle *oh;
213 uint64_t offset = *pos;
218 LASSERT(dt_object_exists(dt));
222 oh = container_of(th, struct osd_thandle, ot_super);
224 down_read(&obj->oo_guard);
225 if (obj->oo_destroyed)
226 GOTO(out, rc = -ENOENT);
228 osd_dmu_write(osd, obj->oo_dn, offset, (uint64_t)buf->lb_len,
229 buf->lb_buf, oh->ot_tx);
230 write_lock(&obj->oo_attr_lock);
231 if (obj->oo_attr.la_size < offset + buf->lb_len) {
232 obj->oo_attr.la_size = offset + buf->lb_len;
233 write_unlock(&obj->oo_attr_lock);
234 /* osd_object_sa_update() will be copying directly from oo_attr
235 * into dbuf. any update within a single txg will copy the
237 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
238 &obj->oo_attr.la_size, 8, oh);
242 write_unlock(&obj->oo_attr_lock);
249 up_read(&obj->oo_guard);
254 * XXX: for the moment I don't want to use lnb_flags for osd-internal
255 * purposes as it's not very well defined ...
256 * instead I use the lowest bit of the address so that:
257 * arc buffer: .lnb_data = abuf (arc we loan for write)
258 * dbuf buffer: .lnb_data = dbuf | 1 (dbuf we get for read)
259 * copy buffer: .lnb_page->mapping = obj (page we allocate for write)
263 static int osd_bufs_put(const struct lu_env *env, struct dt_object *dt,
264 struct niobuf_local *lnb, int npages)
266 struct osd_object *obj = osd_dt_obj(dt);
267 struct osd_device *osd = osd_obj2dev(obj);
271 LASSERT(dt_object_exists(dt));
274 for (i = 0; i < npages; i++) {
275 if (lnb[i].lnb_page == NULL)
277 if (lnb[i].lnb_page->mapping == (void *)obj) {
278 /* this is anonymous page allocated for copy-write */
279 lnb[i].lnb_page->mapping = NULL;
280 __free_page(lnb[i].lnb_page);
281 atomic_dec(&osd->od_zerocopy_alloc);
283 /* see comment in osd_bufs_get_read() */
284 ptr = (unsigned long)lnb[i].lnb_data;
287 dmu_buf_rele((void *)ptr, osd_0copy_tag);
288 atomic_dec(&osd->od_zerocopy_pin);
289 } else if (lnb[i].lnb_data != NULL) {
290 int j, apages, abufsz;
291 abufsz = arc_buf_size(lnb[i].lnb_data);
292 apages = abufsz >> PAGE_SHIFT;
293 /* these references to pages must be invalidated
294 * to prevent access in osd_bufs_put() */
295 for (j = 0; j < apages; j++)
296 lnb[i + j].lnb_page = NULL;
297 dmu_return_arcbuf(lnb[i].lnb_data);
298 atomic_dec(&osd->od_zerocopy_loan);
301 lnb[i].lnb_page = NULL;
302 lnb[i].lnb_data = NULL;
308 static inline struct page *kmem_to_page(void *addr)
310 LASSERT(!((unsigned long)addr & ~PAGE_MASK));
311 if (is_vmalloc_addr(addr))
312 return vmalloc_to_page(addr);
314 return virt_to_page(addr);
318 * Prepare buffers for read.
320 * The function maps the range described by \a off and \a len to \a lnb array.
321 * dmu_buf_hold_array_by_bonus() finds/creates appropriate ARC buffers, then
322 * we fill \a lnb array with the pages storing ARC buffers. Notice the current
323 * implementationt passes TRUE to dmu_buf_hold_array_by_bonus() to fill ARC
324 * buffers with actual data, I/O is done in the conext of osd_bufs_get_read().
325 * A better implementation would just return the buffers (potentially unfilled)
326 * and subsequent osd_read_prep() would do I/O for many ranges concurrently.
328 * \param[in] env environment
329 * \param[in] obj object
330 * \param[in] off offset in bytes
331 * \param[in] len the number of bytes to access
332 * \param[out] lnb array of local niobufs pointing to the buffers with data
334 * \retval 0 for success
335 * \retval negative error number of failure
337 static int osd_bufs_get_read(const struct lu_env *env, struct osd_object *obj,
338 loff_t off, ssize_t len, struct niobuf_local *lnb,
341 struct osd_device *osd = osd_obj2dev(obj);
342 int rc, i, numbufs, npages = 0, drop_cache = 0;
343 hrtime_t start = gethrtime();
348 record_start_io(osd, READ, 0);
350 if (obj->oo_attr.la_size >= osd->od_readcache_max_filesize)
353 /* grab buffers for read:
354 * OSD API let us to grab buffers first, then initiate IO(s)
355 * so that all required IOs will be done in parallel, but at the
356 * moment DMU doesn't provide us with a method to grab buffers.
357 * If we discover this is a vital for good performance we
358 * can get own replacement for dmu_buf_hold_array_by_bonus().
361 (obj->oo_dn->dn_datablkshift != 0 ||
362 off < obj->oo_dn->dn_datablksz)) {
363 if (obj->oo_dn->dn_datablkshift == 0 &&
364 off + len > obj->oo_dn->dn_datablksz)
365 len = obj->oo_dn->dn_datablksz - off;
368 if (unlikely(npages >= maxlnb))
369 GOTO(err, rc = -EOVERFLOW);
371 rc = -dmu_buf_hold_array_by_bonus(&obj->oo_dn->dn_bonus->db,
372 off, len, TRUE, osd_0copy_tag,
377 for (i = 0; i < numbufs; i++) {
378 int bufoff, tocpy, thispage;
383 atomic_inc(&osd->od_zerocopy_pin);
385 bufoff = off - dbp[i]->db_offset;
386 tocpy = min_t(int, dbp[i]->db_size - bufoff, len);
388 /* kind of trick to differentiate dbuf vs. arcbuf */
389 LASSERT(((unsigned long)dbp[i] & 1) == 0);
390 dbf = (void *) ((unsigned long)dbp[i] | 1);
393 if (unlikely(npages >= maxlnb))
394 GOTO(err, rc = -EOVERFLOW);
396 thispage = PAGE_SIZE;
397 thispage -= bufoff & (PAGE_SIZE - 1);
398 thispage = min(tocpy, thispage);
401 lnb->lnb_file_offset = off;
402 lnb->lnb_page_offset = bufoff & ~PAGE_MASK;
403 lnb->lnb_len = thispage;
404 lnb->lnb_page = kmem_to_page(dbp[i]->db_data +
406 /* mark just a single slot: we need this
407 * reference to dbuf to be released once */
421 dbuf_set_pending_evict(dbp[i]);
423 /* steal dbuf so dmu_buf_rele_array() can't release
428 dmu_buf_rele_array(dbp, numbufs, osd_0copy_tag);
431 delta_ms = gethrtime() - start;
432 do_div(delta_ms, NSEC_PER_MSEC);
433 record_end_io(osd, READ, delta_ms, npages * PAGE_SIZE, npages);
440 dmu_buf_rele_array(dbp, numbufs, osd_0copy_tag);
441 osd_bufs_put(env, &obj->oo_dt, lnb - npages, npages);
445 static inline arc_buf_t *osd_request_arcbuf(dnode_t *dn, size_t bs)
449 abuf = dmu_request_arcbuf(&dn->dn_bonus->db, bs);
451 return ERR_PTR(-ENOMEM);
453 #if ZFS_VERSION_CODE < OBD_OCD_VERSION(0, 7, 0, 0)
455 * ZFS prior to 0.7.0 doesn't guarantee PAGE_SIZE alignment for zio
456 * blocks smaller than (PAGE_SIZE << 2). This poses a problem of
457 * setting up page array for RDMA transfer. See LU-9305.
459 if ((unsigned long)abuf->b_data & ~PAGE_MASK) {
460 dmu_return_arcbuf(abuf);
468 static int osd_bufs_get_write(const struct lu_env *env, struct osd_object *obj,
469 loff_t off, ssize_t len, struct niobuf_local *lnb,
472 struct osd_device *osd = osd_obj2dev(obj);
473 int poff, plen, off_in_block, sz_in_block;
474 int rc, i = 0, npages = 0;
475 dnode_t *dn = obj->oo_dn;
477 uint32_t bs = dn->dn_datablksz;
481 * currently only full blocks are subject to zerocopy approach:
482 * so that we're sure nobody is trying to update the same block
485 if (unlikely(npages >= maxlnb))
486 GOTO(out_err, rc = -EOVERFLOW);
488 off_in_block = off & (bs - 1);
489 sz_in_block = min_t(int, bs - off_in_block, len);
492 if (sz_in_block == bs) {
493 /* full block, try to use zerocopy */
494 abuf = osd_request_arcbuf(dn, bs);
495 if (unlikely(IS_ERR(abuf)))
496 GOTO(out_err, rc = PTR_ERR(abuf));
500 atomic_inc(&osd->od_zerocopy_loan);
502 /* go over pages arcbuf contains, put them as
503 * local niobufs for ptlrpc's bulks */
504 while (sz_in_block > 0) {
505 plen = min_t(int, sz_in_block, PAGE_SIZE);
507 if (unlikely(npages >= maxlnb))
508 GOTO(out_err, rc = -EOVERFLOW);
510 lnb[i].lnb_file_offset = off;
511 lnb[i].lnb_page_offset = 0;
512 lnb[i].lnb_len = plen;
514 if (sz_in_block == bs)
515 lnb[i].lnb_data = abuf;
517 lnb[i].lnb_data = NULL;
519 /* this one is not supposed to fail */
520 lnb[i].lnb_page = kmem_to_page(abuf->b_data +
522 LASSERT(lnb[i].lnb_page);
524 lprocfs_counter_add(osd->od_stats,
525 LPROC_OSD_ZEROCOPY_IO, 1);
530 off_in_block += plen;
535 if (off_in_block == 0 && len < bs &&
536 off + len >= obj->oo_attr.la_size)
537 lprocfs_counter_add(osd->od_stats,
538 LPROC_OSD_TAIL_IO, 1);
540 /* can't use zerocopy, allocate temp. buffers */
541 poff = off & (PAGE_SIZE - 1);
542 while (sz_in_block > 0) {
543 plen = min_t(int, poff + sz_in_block,
547 if (unlikely(npages >= maxlnb))
548 GOTO(out_err, rc = -EOVERFLOW);
550 lnb[i].lnb_file_offset = off;
551 lnb[i].lnb_page_offset = poff;
554 lnb[i].lnb_len = plen;
556 lnb[i].lnb_data = NULL;
558 lnb[i].lnb_page = alloc_page(OSD_GFP_IO);
559 if (unlikely(lnb[i].lnb_page == NULL))
560 GOTO(out_err, rc = -ENOMEM);
562 LASSERT(lnb[i].lnb_page->mapping == NULL);
563 lnb[i].lnb_page->mapping = (void *)obj;
565 atomic_inc(&osd->od_zerocopy_alloc);
566 lprocfs_counter_add(osd->od_stats,
567 LPROC_OSD_COPY_IO, 1);
581 osd_bufs_put(env, &obj->oo_dt, lnb, npages);
585 static int osd_bufs_get(const struct lu_env *env, struct dt_object *dt,
586 loff_t offset, ssize_t len, struct niobuf_local *lnb,
587 int maxlnb, enum dt_bufs_type rw)
589 struct osd_object *obj = osd_dt_obj(dt);
592 LASSERT(dt_object_exists(dt));
595 if (rw & DT_BUFS_TYPE_WRITE)
596 rc = osd_bufs_get_write(env, obj, offset, len, lnb, maxlnb);
598 rc = osd_bufs_get_read(env, obj, offset, len, lnb, maxlnb);
603 static int osd_write_prep(const struct lu_env *env, struct dt_object *dt,
604 struct niobuf_local *lnb, int npages)
606 struct osd_object *obj = osd_dt_obj(dt);
608 LASSERT(dt_object_exists(dt));
614 static inline uint64_t osd_roundup2blocksz(uint64_t size,
620 size += offset % blksz;
622 if (likely(is_power_of_2(blksz)))
623 return round_up(size, blksz);
625 return DIV_ROUND_UP_ULL(size, blksz) * blksz;
628 static int osd_declare_write_commit(const struct lu_env *env,
629 struct dt_object *dt,
630 struct niobuf_local *lnb, int npages,
633 struct osd_object *obj = osd_dt_obj(dt);
634 struct osd_device *osd = osd_obj2dev(obj);
635 struct osd_thandle *oh;
638 uint32_t blksz = obj->oo_dn->dn_datablksz;
642 struct page *last_page = NULL;
643 unsigned long discont_pages = 0;
644 enum osd_quota_local_flags local_flags = 0;
645 enum osd_qid_declare_flags declare_flags = OSD_QID_BLK;
648 LASSERT(dt_object_exists(dt));
654 oh = container_of(th, struct osd_thandle, ot_super);
656 for (i = 0; i < npages; i++) {
657 if (last_page && lnb[i].lnb_page->index != (last_page->index + 1))
659 last_page = lnb[i].lnb_page;
661 /* ENOSPC, network RPC error, etc.
662 * We don't want to book space for pages which will be
663 * skipped in osd_write_commit(). Hence we skip pages
664 * with lnb_rc != 0 here too */
666 /* ignore quota for the whole request if any page is from
667 * client cache or written by root.
669 * XXX we could handle this on per-lnb basis as done by
671 if ((lnb[i].lnb_flags & OBD_BRW_NOQUOTA) ||
672 (lnb[i].lnb_flags & OBD_BRW_SYS_RESOURCE) ||
673 !(lnb[i].lnb_flags & OBD_BRW_SYNC))
674 declare_flags |= OSD_QID_FORCE;
677 /* first valid lnb */
678 offset = lnb[i].lnb_file_offset;
679 size = lnb[i].lnb_len;
682 if (offset + size == lnb[i].lnb_file_offset) {
683 /* this lnb is contiguous to the previous one */
684 size += lnb[i].lnb_len;
688 osd_tx_hold_write(oh->ot_tx, obj->oo_dn->dn_object,
689 obj->oo_dn, offset, size);
690 /* Estimating space to be consumed by a write is rather
691 * complicated with ZFS. As a consequence, we don't account for
692 * indirect blocks and just use as a rough estimate the worse
693 * case where the old space is being held by a snapshot. Quota
694 * overrun will be adjusted once the operation is committed, if
696 space += osd_roundup2blocksz(size, offset, blksz);
698 offset = lnb[i].lnb_file_offset;
699 size = lnb[i].lnb_len;
703 osd_tx_hold_write(oh->ot_tx, obj->oo_dn->dn_object, obj->oo_dn,
705 space += osd_roundup2blocksz(size, offset, blksz);
708 /* backend zfs filesystem might be configured to store multiple data
710 space *= osd->od_os->os_copies;
712 CDEBUG(D_QUOTA, "writing %d pages, reserving %lldK of quota space\n",
715 record_start_io(osd, WRITE, discont_pages);
717 /* acquire quota space if needed */
718 rc = osd_declare_quota(env, osd, obj->oo_attr.la_uid,
719 obj->oo_attr.la_gid, obj->oo_attr.la_projid,
720 space, oh, &local_flags, declare_flags);
722 if (!synced && rc == -EDQUOT &&
723 (local_flags & QUOTA_FL_SYNC) != 0) {
724 dt_sync(env, th->th_dev);
726 CDEBUG(D_QUOTA, "retry after sync\n");
731 /* we need only to store the overquota flags in the first lnb for
732 * now, once we support multiple objects BRW, this code needs be
734 if (local_flags & QUOTA_FL_OVER_USRQUOTA)
735 lnb[0].lnb_flags |= OBD_BRW_OVER_USRQUOTA;
736 if (local_flags & QUOTA_FL_OVER_GRPQUOTA)
737 lnb[0].lnb_flags |= OBD_BRW_OVER_GRPQUOTA;
738 #ifdef ZFS_PROJINHERIT
739 if (local_flags & QUOTA_FL_OVER_PRJQUOTA)
740 lnb[0].lnb_flags |= OBD_BRW_OVER_PRJQUOTA;
747 * Policy to grow ZFS block size by write pattern.
748 * For sequential write, it grows block size gradually until it reaches the
749 * maximum blocksize the dataset can support. Otherwise, it will pick a
750 * a block size by the writing region of this I/O.
752 static int osd_grow_blocksize(struct osd_object *obj, struct osd_thandle *oh,
753 uint64_t start, uint64_t end)
755 struct osd_device *osd = osd_obj2dev(obj);
756 dnode_t *dn = obj->oo_dn;
762 if (dn->dn_maxblkid > 0) /* can't change block size */
765 if (dn->dn_datablksz >= osd->od_max_blksz)
768 down_write(&obj->oo_guard);
770 blksz = dn->dn_datablksz;
771 if (blksz >= osd->od_max_blksz) /* check again after grabbing lock */
772 GOTO(out_unlock, rc);
774 /* now ZFS can support up to 16MB block size, and if the write
775 * is sequential, it just increases the block size gradually */
776 if (start <= blksz) { /* sequential */
777 blksz = (uint32_t)min_t(uint64_t, osd->od_max_blksz, end);
778 } else { /* sparse, pick a block size by write region */
779 blksz = (uint32_t)min_t(uint64_t, osd->od_max_blksz,
783 if (!is_power_of_2(blksz))
784 blksz = size_roundup_power2(blksz);
786 if (blksz > dn->dn_datablksz) {
787 rc = -dmu_object_set_blocksize(osd->od_os, dn->dn_object,
788 blksz, 0, oh->ot_tx);
789 LASSERT(ergo(rc == 0, dn->dn_datablksz >= blksz));
791 CDEBUG(D_INODE, "object "DFID": change block size"
792 "%u -> %u error rc = %d\n",
793 PFID(lu_object_fid(&obj->oo_dt.do_lu)),
794 dn->dn_datablksz, blksz, rc);
798 up_write(&obj->oo_guard);
803 static void osd_evict_dbufs_after_write(struct osd_object *obj,
804 loff_t off, ssize_t len)
809 rc = -dmu_buf_hold_array_by_bonus(&obj->oo_dn->dn_bonus->db, off, len,
810 TRUE, osd_0copy_tag, &numbufs, &dbp);
814 for (i = 0; i < numbufs; i++)
815 dbuf_set_pending_evict(dbp[i]);
817 dmu_buf_rele_array(dbp, numbufs, osd_0copy_tag);
820 static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
821 struct niobuf_local *lnb, int npages,
822 struct thandle *th, __u64 user_size)
824 struct osd_object *obj = osd_dt_obj(dt);
825 struct osd_device *osd = osd_obj2dev(obj);
826 struct osd_thandle *oh;
827 uint64_t new_size = 0;
828 int i, abufsz, rc = 0, drop_cache = 0;
829 unsigned long iosize = 0;
832 LASSERT(dt_object_exists(dt));
836 oh = container_of(th, struct osd_thandle, ot_super);
838 /* adjust block size. Assume the buffers are sorted. */
839 (void)osd_grow_blocksize(obj, oh, lnb[0].lnb_file_offset,
840 lnb[npages - 1].lnb_file_offset +
841 lnb[npages - 1].lnb_len);
843 if (obj->oo_attr.la_size >= osd->od_readcache_max_filesize ||
844 lnb[npages - 1].lnb_file_offset + lnb[npages - 1].lnb_len >=
845 osd->od_readcache_max_filesize)
848 if (OBD_FAIL_CHECK(OBD_FAIL_OST_MAPBLK_ENOSPC))
851 /* if la_size is already bigger than specified user_size,
854 if (obj->oo_attr.la_size > user_size)
857 /* LU-8791: take oo_guard to avoid the deadlock that changing block
858 * size and assigning arcbuf take place at the same time.
862 * -> osd_grow_blocksize() with osd_object::oo_guard held
863 * -> dmu_object_set_blocksize()
864 * -> dnode_set_blksz(), with dnode_t::dn_struct_rwlock
867 * -> dmu_buf_will_dirty()
869 * -> wait for the dbuf state to change
872 * -> dmu_assign_arcbuf()
873 * -> dbuf_assign_arcbuf(), set dbuf state to DB_FILL
875 * -> try to hold the read lock of dnode_t::dn_struct_rwlock
877 * By taking the read lock, it can avoid thread 2 to enter into the
878 * critical section of assigning the arcbuf, while thread 1 is
879 * changing the block size.
881 down_read(&obj->oo_guard);
882 if (obj->oo_destroyed) {
883 up_read(&obj->oo_guard);
887 for (i = 0; i < npages; i++) {
888 CDEBUG(D_INODE, "write %u bytes at %u\n",
889 (unsigned) lnb[i].lnb_len,
890 (unsigned) lnb[i].lnb_file_offset);
893 /* ENOSPC, network RPC error, etc.
894 * Unlike ldiskfs, zfs allocates new blocks on rewrite,
895 * so we skip this page if lnb_rc is set to -ENOSPC */
896 CDEBUG(D_INODE, "obj "DFID": skipping lnb[%u]: rc=%d\n",
897 PFID(lu_object_fid(&dt->do_lu)), i,
902 if (new_size < lnb[i].lnb_file_offset + lnb[i].lnb_len)
903 new_size = lnb[i].lnb_file_offset + lnb[i].lnb_len;
904 if (lnb[i].lnb_page == NULL)
907 if (lnb[i].lnb_page->mapping == (void *)obj) {
908 osd_dmu_write(osd, obj->oo_dn, lnb[i].lnb_file_offset,
909 lnb[i].lnb_len, kmap(lnb[i].lnb_page) +
910 lnb[i].lnb_page_offset, oh->ot_tx);
911 kunmap(lnb[i].lnb_page);
912 iosize += lnb[i].lnb_len;
913 abufsz = lnb[i].lnb_len; /* to drop cache below */
914 } else if (lnb[i].lnb_data) {
916 LASSERT(((unsigned long)lnb[i].lnb_data & 1) == 0);
917 /* buffer loaned for zerocopy, try to use it.
918 * notice that dmu_assign_arcbuf() is smart
919 * enough to recognize changed blocksize
920 * in this case it fallbacks to dmu_write() */
921 abufsz = arc_buf_size(lnb[i].lnb_data);
922 LASSERT(abufsz & PAGE_MASK);
923 apages = abufsz >> PAGE_SHIFT;
924 LASSERT(i + apages <= npages);
925 /* these references to pages must be invalidated
926 * to prevent access in osd_bufs_put() */
927 for (j = 0; j < apages; j++)
928 lnb[i + j].lnb_page = NULL;
929 dmu_assign_arcbuf(&obj->oo_dn->dn_bonus->db,
930 lnb[i].lnb_file_offset,
931 lnb[i].lnb_data, oh->ot_tx);
932 /* drop the reference, otherwise osd_put_bufs()
933 * will be releasing it - bad! */
934 lnb[i].lnb_data = NULL;
935 atomic_dec(&osd->od_zerocopy_loan);
938 /* we don't want to deal with cache if nothing
939 * has been send to ZFS at this step */
946 /* we have to mark dbufs for eviction here because
947 * dmu_assign_arcbuf() may create a new dbuf for
949 osd_evict_dbufs_after_write(obj, lnb[i].lnb_file_offset,
953 if (unlikely(new_size == 0)) {
954 /* no pages to write, no transno is needed */
956 /* it is important to return 0 even when all lnb_rc == -ENOSPC
957 * since ofd_commitrw_write() retries several times on ENOSPC */
958 up_read(&obj->oo_guard);
959 record_end_io(osd, WRITE, 0, 0, 0);
963 /* if file has grown, take user_size into account */
964 if (user_size && new_size > user_size)
965 new_size = user_size;
966 write_lock(&obj->oo_attr_lock);
967 if (obj->oo_attr.la_size < new_size) {
968 obj->oo_attr.la_size = new_size;
969 write_unlock(&obj->oo_attr_lock);
970 /* osd_object_sa_update() will be copying directly from
971 * oo_attr into dbuf. any update within a single txg will copy
973 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
974 &obj->oo_attr.la_size, 8, oh);
976 write_unlock(&obj->oo_attr_lock);
979 up_read(&obj->oo_guard);
981 record_end_io(osd, WRITE, 0, iosize, npages);
986 static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
987 struct niobuf_local *lnb, int npages)
989 struct osd_object *obj = osd_dt_obj(dt);
993 LASSERT(dt_object_exists(dt));
996 read_lock(&obj->oo_attr_lock);
997 eof = obj->oo_attr.la_size;
998 read_unlock(&obj->oo_attr_lock);
1000 for (i = 0; i < npages; i++) {
1001 if (unlikely(lnb[i].lnb_rc < 0))
1004 lnb[i].lnb_rc = lnb[i].lnb_len;
1006 if (lnb[i].lnb_file_offset + lnb[i].lnb_len >= eof) {
1007 /* send complete pages all the time */
1008 if (eof <= lnb[i].lnb_file_offset)
1011 /* all subsequent rc should be 0 */
1012 while (++i < npages)
1022 * Punch/truncate an object
1024 * IN: db - dmu_buf of the object to free data in.
1025 * off - start of section to free.
1026 * len - length of section to free (DMU_OBJECT_END => to EOF).
1028 * RETURN: 0 if success
1029 * error code if failure
1031 * The transaction passed to this routine must have
1032 * dmu_tx_hold_sa() and if off < size, dmu_tx_hold_free()
1033 * called and then assigned to a transaction group.
1035 static int __osd_object_punch(struct osd_object *obj, objset_t *os,
1036 dmu_tx_t *tx, uint64_t off, uint64_t len)
1038 dnode_t *dn = obj->oo_dn;
1039 uint64_t size = obj->oo_attr.la_size;
1042 /* Assert that the transaction has been assigned to a
1043 transaction group. */
1044 LASSERT(tx->tx_txg != 0);
1046 * Nothing to do if file already at desired length.
1048 if (len == DMU_OBJECT_END && size == off)
1051 /* if object holds encrypted content, we need to make sure we truncate
1052 * on an encryption unit boundary, or subsequent reads will get
1055 if (len != DMU_OBJECT_END)
1056 len -= LUSTRE_ENCRYPTION_UNIT_SIZE -
1057 (off & ~LUSTRE_ENCRYPTION_MASK);
1058 if (obj->oo_lma_flags & LUSTRE_ENCRYPT_FL &&
1059 off & ~LUSTRE_ENCRYPTION_MASK)
1060 off = (off & LUSTRE_ENCRYPTION_MASK) +
1061 LUSTRE_ENCRYPTION_UNIT_SIZE;
1064 /* XXX: dnode_free_range() can be used to save on dnode lookup */
1066 dmu_free_range(os, dn->dn_object, off, len, tx);
1071 static int osd_punch(const struct lu_env *env, struct dt_object *dt,
1072 __u64 start, __u64 end, struct thandle *th)
1074 struct osd_object *obj = osd_dt_obj(dt);
1075 struct osd_device *osd = osd_obj2dev(obj);
1076 struct osd_thandle *oh;
1081 LASSERT(dt_object_exists(dt));
1082 LASSERT(osd_invariant(obj));
1084 LASSERT(th != NULL);
1085 oh = container_of(th, struct osd_thandle, ot_super);
1087 write_lock(&obj->oo_attr_lock);
1089 if (end == OBD_OBJECT_EOF || end >= obj->oo_attr.la_size)
1090 len = DMU_OBJECT_END;
1093 write_unlock(&obj->oo_attr_lock);
1095 down_read(&obj->oo_guard);
1096 if (obj->oo_destroyed)
1097 GOTO(out, rc = -ENOENT);
1099 rc = __osd_object_punch(obj, osd->od_os, oh->ot_tx, start, len);
1102 if (len == DMU_OBJECT_END) {
1103 write_lock(&obj->oo_attr_lock);
1104 obj->oo_attr.la_size = start;
1105 write_unlock(&obj->oo_attr_lock);
1106 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
1107 &obj->oo_attr.la_size, 8, oh);
1110 up_read(&obj->oo_guard);
1114 static int osd_declare_punch(const struct lu_env *env, struct dt_object *dt,
1115 __u64 start, __u64 end, struct thandle *handle)
1117 struct osd_object *obj = osd_dt_obj(dt);
1118 struct osd_device *osd = osd_obj2dev(obj);
1119 struct osd_thandle *oh;
1123 oh = container_of(handle, struct osd_thandle, ot_super);
1125 read_lock(&obj->oo_attr_lock);
1126 if (end == OBD_OBJECT_EOF || end >= obj->oo_attr.la_size)
1127 len = DMU_OBJECT_END;
1131 /* declare we'll free some blocks ... */
1132 /* if object holds encrypted content, we need to make sure we truncate
1133 * on an encryption unit boundary, or subsequent reads will get
1136 if (obj->oo_lma_flags & LUSTRE_ENCRYPT_FL &&
1137 start & ~LUSTRE_ENCRYPTION_MASK)
1138 start = (start & LUSTRE_ENCRYPTION_MASK) +
1139 LUSTRE_ENCRYPTION_UNIT_SIZE;
1140 if (start < obj->oo_attr.la_size) {
1141 read_unlock(&obj->oo_attr_lock);
1142 dmu_tx_mark_netfree(oh->ot_tx);
1143 dmu_tx_hold_free(oh->ot_tx, obj->oo_dn->dn_object, start, len);
1145 read_unlock(&obj->oo_attr_lock);
1148 RETURN(osd_declare_quota(env, osd, obj->oo_attr.la_uid,
1149 obj->oo_attr.la_gid, obj->oo_attr.la_projid,
1150 0, oh, NULL, OSD_QID_BLK));
1153 static int osd_ladvise(const struct lu_env *env, struct dt_object *dt,
1154 __u64 start, __u64 end, enum lu_ladvise_type advice)
1168 static int osd_fallocate(const struct lu_env *env, struct dt_object *dt,
1169 __u64 start, __u64 end, int mode, struct thandle *th)
1171 int rc = -EOPNOTSUPP;
1175 * space preallocation is not supported for ZFS
1176 * Returns -EOPNOTSUPP for now
1181 static int osd_declare_fallocate(const struct lu_env *env,
1182 struct dt_object *dt, __u64 start, __u64 end,
1183 int mode, struct thandle *th)
1185 int rc = -EOPNOTSUPP;
1189 * space preallocation is not supported for ZFS
1190 * Returns -EOPNOTSUPP for now
1195 static loff_t osd_lseek(const struct lu_env *env, struct dt_object *dt,
1196 loff_t offset, int whence)
1198 struct osd_object *obj = osd_dt_obj(dt);
1199 struct osd_device *osd = osd_obj2dev(obj);
1200 uint64_t size = obj->oo_attr.la_size;
1201 uint64_t result = offset;
1203 boolean_t hole = whence == SEEK_HOLE;
1207 LASSERT(dt_object_exists(dt));
1208 LASSERT(osd_invariant(obj));
1209 LASSERT(offset >= 0);
1211 /* for SEEK_HOLE treat 'offset' beyond the end of file as in real
1212 * hole. LOV to decide after all if that real hole or not.
1215 RETURN(hole ? offset : -ENXIO);
1217 /* Currently ZFS reports no valid DATA offset if object has dirty data
1218 * and we cannot just switch to generic way with reporting DATA on all
1219 * file offsets and HOLE beyond end of file, because we may get HOLE
1220 * reported correctly at some offset inside file then DATA will find
1221 * dirty state and be reported also at that offset by generic approach.
1222 * This is because for HOLE report ZFS doesn't check dirty state but
1224 * The only way to get reliable results is to call txg_wait_synced()
1225 * when ZFS reports EBUSY result and repeat lseek call and that is
1226 * controlled via od_sync_on_lseek option.
1228 if (!osd->od_sync_on_lseek)
1229 result = hole ? size : offset;
1232 rc = osd_dmu_offset_next(osd->od_os, obj->oo_dn->dn_object, hole,
1234 /* dirty inode, lseek result is unreliable without sync */
1236 txg_wait_synced(dmu_objset_pool(osd->od_os), 0ULL);
1243 /* ZFS is not exported all needed function, so fall back to the
1244 * generic logic: for HOLE return file size, for DATA return
1245 * the current offset
1247 if (rc == EOPNOTSUPP)
1248 result = hole ? size : offset;
1252 /* dmu_offset_next() only works on whole blocks so may return SEEK_HOLE
1253 * result as end of the last block instead of logical EOF which we need
1261 const struct dt_body_operations osd_body_ops = {
1262 .dbo_read = osd_read,
1263 .dbo_declare_write = osd_declare_write,
1264 .dbo_write = osd_write,
1265 .dbo_bufs_get = osd_bufs_get,
1266 .dbo_bufs_put = osd_bufs_put,
1267 .dbo_write_prep = osd_write_prep,
1268 .dbo_declare_write_commit = osd_declare_write_commit,
1269 .dbo_write_commit = osd_write_commit,
1270 .dbo_read_prep = osd_read_prep,
1271 .dbo_declare_punch = osd_declare_punch,
1272 .dbo_punch = osd_punch,
1273 .dbo_ladvise = osd_ladvise,
1274 .dbo_declare_fallocate = osd_declare_fallocate,
1275 .dbo_fallocate = osd_fallocate,
1276 .dbo_lseek = osd_lseek,
1279 const struct dt_body_operations osd_body_scrub_ops = {
1280 .dbo_read = osd_read_no_record,
1281 .dbo_declare_write = osd_declare_write,
1282 .dbo_write = osd_write,