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 obd_histogram *h = osd->od_brw_stats.bs_hist;
76 atomic_inc(&osd->od_r_in_flight);
77 lprocfs_oh_tally(&h[BRW_R_RPC_HIST],
78 atomic_read(&osd->od_r_in_flight));
79 lprocfs_oh_tally(&h[BRW_R_DISCONT_PAGES], discont_pages);
81 atomic_inc(&osd->od_w_in_flight);
82 lprocfs_oh_tally(&h[BRW_W_RPC_HIST],
83 atomic_read(&osd->od_w_in_flight));
84 lprocfs_oh_tally(&h[BRW_W_DISCONT_PAGES], discont_pages);
88 static void record_end_io(struct osd_device *osd, int rw,
89 unsigned long elapsed, int disksize, int npages)
91 struct obd_histogram *h = osd->od_brw_stats.bs_hist;
94 atomic_dec(&osd->od_r_in_flight);
96 atomic_dec(&osd->od_w_in_flight);
98 lprocfs_oh_tally_log2(&h[BRW_R_PAGES + rw], npages);
100 lprocfs_oh_tally_log2(&h[BRW_R_DISK_IOSIZE + rw], disksize);
102 lprocfs_oh_tally_log2(&h[BRW_R_IO_TIME + rw], elapsed);
105 static ssize_t __osd_read(const struct lu_env *env, struct dt_object *dt,
106 struct lu_buf *buf, loff_t *pos, size_t *size)
108 struct osd_object *obj = osd_dt_obj(dt);
112 LASSERT(dt_object_exists(dt));
115 read_lock(&obj->oo_attr_lock);
116 old_size = obj->oo_attr.la_size;
117 read_unlock(&obj->oo_attr_lock);
119 if (*pos + *size > old_size) {
123 *size = old_size - *pos;
126 rc = osd_dmu_read(osd_obj2dev(obj), obj->oo_dn, *pos, *size,
127 buf->lb_buf, DMU_READ_PREFETCH);
136 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
137 struct lu_buf *buf, loff_t *pos)
139 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
140 size_t size = buf->lb_len;
141 hrtime_t start = gethrtime();
145 record_start_io(osd, READ, 0);
146 rc = __osd_read(env, dt, buf, pos, &size);
147 delta_ms = gethrtime() - start;
148 do_div(delta_ms, NSEC_PER_MSEC);
149 record_end_io(osd, READ, delta_ms, size, size >> PAGE_SHIFT);
154 static inline ssize_t osd_read_no_record(const struct lu_env *env,
155 struct dt_object *dt,
156 struct lu_buf *buf, loff_t *pos)
158 size_t size = buf->lb_len;
160 return __osd_read(env, dt, buf, pos, &size);
163 static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
164 const struct lu_buf *buf, loff_t pos,
167 struct osd_object *obj = osd_dt_obj(dt);
168 struct osd_device *osd = osd_obj2dev(obj);
169 struct osd_thandle *oh;
173 oh = container_of(th, struct osd_thandle, ot_super);
175 /* in some cases declare can race with creation (e.g. llog)
176 * and we need to wait till object is initialized. notice
177 * LOHA_EXISTs is supposed to be the last step in the
180 /* size change (in dnode) will be declared by dmu_tx_hold_write() */
181 if (dt_object_exists(dt))
182 oid = obj->oo_dn->dn_object;
184 oid = DMU_NEW_OBJECT;
186 /* XXX: we still miss for append declaration support in ZFS
187 * -1 means append which is used by llog mostly, llog
188 * can grow upto LLOG_MIN_CHUNK_SIZE*8 records */
190 pos = max_t(loff_t, 256 * 8 * LLOG_MIN_CHUNK_SIZE,
191 obj->oo_attr.la_size + (2 << 20));
192 osd_tx_hold_write(oh->ot_tx, oid, obj->oo_dn, pos, buf->lb_len);
194 /* dt_declare_write() is usually called for system objects, such
195 * as llog or last_rcvd files. We needn't enforce quota on those
196 * objects, so always set the lqi_space as 0. */
197 RETURN(osd_declare_quota(env, osd, obj->oo_attr.la_uid,
198 obj->oo_attr.la_gid, obj->oo_attr.la_projid,
199 0, oh, NULL, OSD_QID_BLK));
202 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
203 const struct lu_buf *buf, loff_t *pos,
206 struct osd_object *obj = osd_dt_obj(dt);
207 struct osd_device *osd = osd_obj2dev(obj);
208 struct osd_thandle *oh;
209 uint64_t offset = *pos;
214 LASSERT(dt_object_exists(dt));
218 oh = container_of(th, struct osd_thandle, ot_super);
220 down_read(&obj->oo_guard);
221 if (obj->oo_destroyed)
222 GOTO(out, rc = -ENOENT);
224 osd_dmu_write(osd, obj->oo_dn, offset, (uint64_t)buf->lb_len,
225 buf->lb_buf, oh->ot_tx);
226 write_lock(&obj->oo_attr_lock);
227 if (obj->oo_attr.la_size < offset + buf->lb_len) {
228 obj->oo_attr.la_size = offset + buf->lb_len;
229 write_unlock(&obj->oo_attr_lock);
230 /* osd_object_sa_update() will be copying directly from oo_attr
231 * into dbuf. any update within a single txg will copy the
233 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
234 &obj->oo_attr.la_size, 8, oh);
238 write_unlock(&obj->oo_attr_lock);
245 up_read(&obj->oo_guard);
250 * XXX: for the moment I don't want to use lnb_flags for osd-internal
251 * purposes as it's not very well defined ...
252 * instead I use the lowest bit of the address so that:
253 * arc buffer: .lnb_data = abuf (arc we loan for write)
254 * dbuf buffer: .lnb_data = dbuf | 1 (dbuf we get for read)
255 * copy buffer: .lnb_page->mapping = obj (page we allocate for write)
259 static int osd_bufs_put(const struct lu_env *env, struct dt_object *dt,
260 struct niobuf_local *lnb, int npages)
262 struct osd_object *obj = osd_dt_obj(dt);
263 struct osd_device *osd = osd_obj2dev(obj);
267 LASSERT(dt_object_exists(dt));
270 for (i = 0; i < npages; i++) {
271 if (lnb[i].lnb_page == NULL)
273 if (lnb[i].lnb_page->mapping == (void *)obj) {
274 /* this is anonymous page allocated for copy-write */
275 lnb[i].lnb_page->mapping = NULL;
276 __free_page(lnb[i].lnb_page);
277 atomic_dec(&osd->od_zerocopy_alloc);
279 /* see comment in osd_bufs_get_read() */
280 ptr = (unsigned long)lnb[i].lnb_data;
283 dmu_buf_rele((void *)ptr, osd_0copy_tag);
284 atomic_dec(&osd->od_zerocopy_pin);
285 } else if (lnb[i].lnb_data != NULL) {
286 int j, apages, abufsz;
287 abufsz = arc_buf_size(lnb[i].lnb_data);
288 apages = abufsz >> PAGE_SHIFT;
289 /* these references to pages must be invalidated
290 * to prevent access in osd_bufs_put() */
291 for (j = 0; j < apages; j++)
292 lnb[i + j].lnb_page = NULL;
293 dmu_return_arcbuf(lnb[i].lnb_data);
294 atomic_dec(&osd->od_zerocopy_loan);
297 lnb[i].lnb_page = NULL;
298 lnb[i].lnb_data = NULL;
304 static inline struct page *kmem_to_page(void *addr)
306 LASSERT(!((unsigned long)addr & ~PAGE_MASK));
307 if (is_vmalloc_addr(addr))
308 return vmalloc_to_page(addr);
310 return virt_to_page(addr);
314 * Prepare buffers for read.
316 * The function maps the range described by \a off and \a len to \a lnb array.
317 * dmu_buf_hold_array_by_bonus() finds/creates appropriate ARC buffers, then
318 * we fill \a lnb array with the pages storing ARC buffers. Notice the current
319 * implementationt passes TRUE to dmu_buf_hold_array_by_bonus() to fill ARC
320 * buffers with actual data, I/O is done in the conext of osd_bufs_get_read().
321 * A better implementation would just return the buffers (potentially unfilled)
322 * and subsequent osd_read_prep() would do I/O for many ranges concurrently.
324 * \param[in] env environment
325 * \param[in] obj object
326 * \param[in] off offset in bytes
327 * \param[in] len the number of bytes to access
328 * \param[out] lnb array of local niobufs pointing to the buffers with data
330 * \retval 0 for success
331 * \retval negative error number of failure
333 static int osd_bufs_get_read(const struct lu_env *env, struct osd_object *obj,
334 loff_t off, ssize_t len, struct niobuf_local *lnb,
337 struct osd_device *osd = osd_obj2dev(obj);
338 int rc, i, numbufs, npages = 0, drop_cache = 0;
339 hrtime_t start = gethrtime();
344 record_start_io(osd, READ, 0);
346 if (obj->oo_attr.la_size >= osd->od_readcache_max_filesize)
349 /* grab buffers for read:
350 * OSD API let us to grab buffers first, then initiate IO(s)
351 * so that all required IOs will be done in parallel, but at the
352 * moment DMU doesn't provide us with a method to grab buffers.
353 * If we discover this is a vital for good performance we
354 * can get own replacement for dmu_buf_hold_array_by_bonus().
357 (obj->oo_dn->dn_datablkshift != 0 ||
358 off < obj->oo_dn->dn_datablksz)) {
359 if (obj->oo_dn->dn_datablkshift == 0 &&
360 off + len > obj->oo_dn->dn_datablksz)
361 len = obj->oo_dn->dn_datablksz - off;
364 if (unlikely(npages >= maxlnb))
365 GOTO(err, rc = -EOVERFLOW);
367 rc = -dmu_buf_hold_array_by_bonus(&obj->oo_dn->dn_bonus->db,
368 off, len, TRUE, osd_0copy_tag,
373 for (i = 0; i < numbufs; i++) {
374 int bufoff, tocpy, thispage;
379 atomic_inc(&osd->od_zerocopy_pin);
381 bufoff = off - dbp[i]->db_offset;
382 tocpy = min_t(int, dbp[i]->db_size - bufoff, len);
384 /* kind of trick to differentiate dbuf vs. arcbuf */
385 LASSERT(((unsigned long)dbp[i] & 1) == 0);
386 dbf = (void *) ((unsigned long)dbp[i] | 1);
389 if (unlikely(npages >= maxlnb))
390 GOTO(err, rc = -EOVERFLOW);
392 thispage = PAGE_SIZE;
393 thispage -= bufoff & (PAGE_SIZE - 1);
394 thispage = min(tocpy, thispage);
397 lnb->lnb_file_offset = off;
398 lnb->lnb_page_offset = bufoff & ~PAGE_MASK;
399 lnb->lnb_len = thispage;
400 lnb->lnb_page = kmem_to_page(dbp[i]->db_data +
402 /* mark just a single slot: we need this
403 * reference to dbuf to be released once */
417 dbuf_set_pending_evict(dbp[i]);
419 /* steal dbuf so dmu_buf_rele_array() can't release
424 dmu_buf_rele_array(dbp, numbufs, osd_0copy_tag);
427 delta_ms = gethrtime() - start;
428 do_div(delta_ms, NSEC_PER_MSEC);
429 record_end_io(osd, READ, delta_ms, npages * PAGE_SIZE, npages);
436 dmu_buf_rele_array(dbp, numbufs, osd_0copy_tag);
437 osd_bufs_put(env, &obj->oo_dt, lnb - npages, npages);
441 static inline arc_buf_t *osd_request_arcbuf(dnode_t *dn, size_t bs)
445 abuf = dmu_request_arcbuf(&dn->dn_bonus->db, bs);
447 return ERR_PTR(-ENOMEM);
449 #if ZFS_VERSION_CODE < OBD_OCD_VERSION(0, 7, 0, 0)
451 * ZFS prior to 0.7.0 doesn't guarantee PAGE_SIZE alignment for zio
452 * blocks smaller than (PAGE_SIZE << 2). This poses a problem of
453 * setting up page array for RDMA transfer. See LU-9305.
455 if ((unsigned long)abuf->b_data & ~PAGE_MASK) {
456 dmu_return_arcbuf(abuf);
464 static int osd_bufs_get_write(const struct lu_env *env, struct osd_object *obj,
465 loff_t off, ssize_t len, struct niobuf_local *lnb,
468 struct osd_device *osd = osd_obj2dev(obj);
469 int poff, plen, off_in_block, sz_in_block;
470 int rc, i = 0, npages = 0;
471 dnode_t *dn = obj->oo_dn;
473 uint32_t bs = dn->dn_datablksz;
477 * currently only full blocks are subject to zerocopy approach:
478 * so that we're sure nobody is trying to update the same block
481 if (unlikely(npages >= maxlnb))
482 GOTO(out_err, rc = -EOVERFLOW);
484 off_in_block = off & (bs - 1);
485 sz_in_block = min_t(int, bs - off_in_block, len);
488 if (sz_in_block == bs) {
489 /* full block, try to use zerocopy */
490 abuf = osd_request_arcbuf(dn, bs);
491 if (unlikely(IS_ERR(abuf)))
492 GOTO(out_err, rc = PTR_ERR(abuf));
496 atomic_inc(&osd->od_zerocopy_loan);
498 /* go over pages arcbuf contains, put them as
499 * local niobufs for ptlrpc's bulks */
500 while (sz_in_block > 0) {
501 plen = min_t(int, sz_in_block, PAGE_SIZE);
503 if (unlikely(npages >= maxlnb))
504 GOTO(out_err, rc = -EOVERFLOW);
506 lnb[i].lnb_file_offset = off;
507 lnb[i].lnb_page_offset = 0;
508 lnb[i].lnb_len = plen;
510 if (sz_in_block == bs)
511 lnb[i].lnb_data = abuf;
513 lnb[i].lnb_data = NULL;
515 /* this one is not supposed to fail */
516 lnb[i].lnb_page = kmem_to_page(abuf->b_data +
518 LASSERT(lnb[i].lnb_page);
520 lprocfs_counter_add(osd->od_stats,
521 LPROC_OSD_ZEROCOPY_IO, 1);
526 off_in_block += plen;
531 if (off_in_block == 0 && len < bs &&
532 off + len >= obj->oo_attr.la_size)
533 lprocfs_counter_add(osd->od_stats,
534 LPROC_OSD_TAIL_IO, 1);
536 /* can't use zerocopy, allocate temp. buffers */
537 poff = off & (PAGE_SIZE - 1);
538 while (sz_in_block > 0) {
539 plen = min_t(int, poff + sz_in_block,
543 if (unlikely(npages >= maxlnb))
544 GOTO(out_err, rc = -EOVERFLOW);
546 lnb[i].lnb_file_offset = off;
547 lnb[i].lnb_page_offset = poff;
550 lnb[i].lnb_len = plen;
552 lnb[i].lnb_data = NULL;
554 lnb[i].lnb_page = alloc_page(OSD_GFP_IO);
555 if (unlikely(lnb[i].lnb_page == NULL))
556 GOTO(out_err, rc = -ENOMEM);
558 LASSERT(lnb[i].lnb_page->mapping == NULL);
559 lnb[i].lnb_page->mapping = (void *)obj;
561 atomic_inc(&osd->od_zerocopy_alloc);
562 lprocfs_counter_add(osd->od_stats,
563 LPROC_OSD_COPY_IO, 1);
577 osd_bufs_put(env, &obj->oo_dt, lnb, npages);
581 static int osd_bufs_get(const struct lu_env *env, struct dt_object *dt,
582 loff_t offset, ssize_t len, struct niobuf_local *lnb,
583 int maxlnb, enum dt_bufs_type rw)
585 struct osd_object *obj = osd_dt_obj(dt);
588 LASSERT(dt_object_exists(dt));
591 if (rw & DT_BUFS_TYPE_WRITE)
592 rc = osd_bufs_get_write(env, obj, offset, len, lnb, maxlnb);
594 rc = osd_bufs_get_read(env, obj, offset, len, lnb, maxlnb);
599 static int osd_write_prep(const struct lu_env *env, struct dt_object *dt,
600 struct niobuf_local *lnb, int npages)
602 struct osd_object *obj = osd_dt_obj(dt);
604 LASSERT(dt_object_exists(dt));
610 static inline uint64_t osd_roundup2blocksz(uint64_t size,
616 size += offset % blksz;
618 if (likely(is_power_of_2(blksz)))
619 return round_up(size, blksz);
621 return DIV_ROUND_UP_ULL(size, blksz) * blksz;
624 static int osd_declare_write_commit(const struct lu_env *env,
625 struct dt_object *dt,
626 struct niobuf_local *lnb, int npages,
629 struct osd_object *obj = osd_dt_obj(dt);
630 struct osd_device *osd = osd_obj2dev(obj);
631 struct osd_thandle *oh;
634 uint32_t blksz = obj->oo_dn->dn_datablksz;
638 struct page *last_page = NULL;
639 unsigned long discont_pages = 0;
640 enum osd_quota_local_flags local_flags = 0;
641 enum osd_qid_declare_flags declare_flags = OSD_QID_BLK;
644 LASSERT(dt_object_exists(dt));
650 oh = container_of(th, struct osd_thandle, ot_super);
652 for (i = 0; i < npages; i++) {
653 if (last_page && lnb[i].lnb_page->index != (last_page->index + 1))
655 last_page = lnb[i].lnb_page;
657 /* ENOSPC, network RPC error, etc.
658 * We don't want to book space for pages which will be
659 * skipped in osd_write_commit(). Hence we skip pages
660 * with lnb_rc != 0 here too */
662 /* ignore quota for the whole request if any page is from
663 * client cache or written by root.
665 * XXX we could handle this on per-lnb basis as done by
667 if ((lnb[i].lnb_flags & OBD_BRW_NOQUOTA) ||
668 (lnb[i].lnb_flags & OBD_BRW_SYS_RESOURCE) ||
669 !(lnb[i].lnb_flags & OBD_BRW_SYNC))
670 declare_flags |= OSD_QID_FORCE;
673 /* first valid lnb */
674 offset = lnb[i].lnb_file_offset;
675 size = lnb[i].lnb_len;
678 if (offset + size == lnb[i].lnb_file_offset) {
679 /* this lnb is contiguous to the previous one */
680 size += lnb[i].lnb_len;
684 osd_tx_hold_write(oh->ot_tx, obj->oo_dn->dn_object,
685 obj->oo_dn, offset, size);
686 /* Estimating space to be consumed by a write is rather
687 * complicated with ZFS. As a consequence, we don't account for
688 * indirect blocks and just use as a rough estimate the worse
689 * case where the old space is being held by a snapshot. Quota
690 * overrun will be adjusted once the operation is committed, if
692 space += osd_roundup2blocksz(size, offset, blksz);
694 offset = lnb[i].lnb_file_offset;
695 size = lnb[i].lnb_len;
699 osd_tx_hold_write(oh->ot_tx, obj->oo_dn->dn_object, obj->oo_dn,
701 space += osd_roundup2blocksz(size, offset, blksz);
704 /* backend zfs filesystem might be configured to store multiple data
706 space *= osd->od_os->os_copies;
708 CDEBUG(D_QUOTA, "writing %d pages, reserving %lldK of quota space\n",
711 record_start_io(osd, WRITE, discont_pages);
713 /* acquire quota space if needed */
714 rc = osd_declare_quota(env, osd, obj->oo_attr.la_uid,
715 obj->oo_attr.la_gid, obj->oo_attr.la_projid,
716 space, oh, &local_flags, declare_flags);
718 if (!synced && rc == -EDQUOT &&
719 (local_flags & QUOTA_FL_SYNC) != 0) {
720 dt_sync(env, th->th_dev);
722 CDEBUG(D_QUOTA, "retry after sync\n");
727 /* we need only to store the overquota flags in the first lnb for
728 * now, once we support multiple objects BRW, this code needs be
730 if (local_flags & QUOTA_FL_OVER_USRQUOTA)
731 lnb[0].lnb_flags |= OBD_BRW_OVER_USRQUOTA;
732 if (local_flags & QUOTA_FL_OVER_GRPQUOTA)
733 lnb[0].lnb_flags |= OBD_BRW_OVER_GRPQUOTA;
734 #ifdef ZFS_PROJINHERIT
735 if (local_flags & QUOTA_FL_OVER_PRJQUOTA)
736 lnb[0].lnb_flags |= OBD_BRW_OVER_PRJQUOTA;
743 * Policy to grow ZFS block size by write pattern.
744 * For sequential write, it grows block size gradually until it reaches the
745 * maximum blocksize the dataset can support. Otherwise, it will pick a
746 * a block size by the writing region of this I/O.
748 static int osd_grow_blocksize(struct osd_object *obj, struct osd_thandle *oh,
749 uint64_t start, uint64_t end)
751 struct osd_device *osd = osd_obj2dev(obj);
752 dnode_t *dn = obj->oo_dn;
758 if (dn->dn_maxblkid > 0) /* can't change block size */
761 if (dn->dn_datablksz >= osd->od_max_blksz)
764 down_write(&obj->oo_guard);
766 blksz = dn->dn_datablksz;
767 if (blksz >= osd->od_max_blksz) /* check again after grabbing lock */
768 GOTO(out_unlock, rc);
770 /* now ZFS can support up to 16MB block size, and if the write
771 * is sequential, it just increases the block size gradually */
772 if (start <= blksz) { /* sequential */
773 blksz = (uint32_t)min_t(uint64_t, osd->od_max_blksz, end);
774 } else { /* sparse, pick a block size by write region */
775 blksz = (uint32_t)min_t(uint64_t, osd->od_max_blksz,
779 if (!is_power_of_2(blksz))
780 blksz = size_roundup_power2(blksz);
782 if (blksz > dn->dn_datablksz) {
783 rc = -dmu_object_set_blocksize(osd->od_os, dn->dn_object,
784 blksz, 0, oh->ot_tx);
785 LASSERT(ergo(rc == 0, dn->dn_datablksz >= blksz));
787 CDEBUG(D_INODE, "object "DFID": change block size"
788 "%u -> %u error rc = %d\n",
789 PFID(lu_object_fid(&obj->oo_dt.do_lu)),
790 dn->dn_datablksz, blksz, rc);
794 up_write(&obj->oo_guard);
799 static void osd_evict_dbufs_after_write(struct osd_object *obj,
800 loff_t off, ssize_t len)
805 rc = -dmu_buf_hold_array_by_bonus(&obj->oo_dn->dn_bonus->db, off, len,
806 TRUE, osd_0copy_tag, &numbufs, &dbp);
810 for (i = 0; i < numbufs; i++)
811 dbuf_set_pending_evict(dbp[i]);
813 dmu_buf_rele_array(dbp, numbufs, osd_0copy_tag);
816 static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
817 struct niobuf_local *lnb, int npages,
818 struct thandle *th, __u64 user_size)
820 struct osd_object *obj = osd_dt_obj(dt);
821 struct osd_device *osd = osd_obj2dev(obj);
822 struct osd_thandle *oh;
823 uint64_t new_size = 0;
824 int i, abufsz, rc = 0, drop_cache = 0;
825 unsigned long iosize = 0;
828 LASSERT(dt_object_exists(dt));
832 oh = container_of(th, struct osd_thandle, ot_super);
834 /* adjust block size. Assume the buffers are sorted. */
835 (void)osd_grow_blocksize(obj, oh, lnb[0].lnb_file_offset,
836 lnb[npages - 1].lnb_file_offset +
837 lnb[npages - 1].lnb_len);
839 if (obj->oo_attr.la_size >= osd->od_readcache_max_filesize ||
840 lnb[npages - 1].lnb_file_offset + lnb[npages - 1].lnb_len >=
841 osd->od_readcache_max_filesize)
844 if (OBD_FAIL_CHECK(OBD_FAIL_OST_MAPBLK_ENOSPC))
847 /* if la_size is already bigger than specified user_size,
850 if (obj->oo_attr.la_size > user_size)
853 /* LU-8791: take oo_guard to avoid the deadlock that changing block
854 * size and assigning arcbuf take place at the same time.
858 * -> osd_grow_blocksize() with osd_object::oo_guard held
859 * -> dmu_object_set_blocksize()
860 * -> dnode_set_blksz(), with dnode_t::dn_struct_rwlock
863 * -> dmu_buf_will_dirty()
865 * -> wait for the dbuf state to change
868 * -> dmu_assign_arcbuf()
869 * -> dbuf_assign_arcbuf(), set dbuf state to DB_FILL
871 * -> try to hold the read lock of dnode_t::dn_struct_rwlock
873 * By taking the read lock, it can avoid thread 2 to enter into the
874 * critical section of assigning the arcbuf, while thread 1 is
875 * changing the block size.
877 down_read(&obj->oo_guard);
878 if (obj->oo_destroyed) {
879 up_read(&obj->oo_guard);
883 for (i = 0; i < npages; i++) {
884 CDEBUG(D_INODE, "write %u bytes at %u\n",
885 (unsigned) lnb[i].lnb_len,
886 (unsigned) lnb[i].lnb_file_offset);
889 /* ENOSPC, network RPC error, etc.
890 * Unlike ldiskfs, zfs allocates new blocks on rewrite,
891 * so we skip this page if lnb_rc is set to -ENOSPC */
892 CDEBUG(D_INODE, "obj "DFID": skipping lnb[%u]: rc=%d\n",
893 PFID(lu_object_fid(&dt->do_lu)), i,
898 if (new_size < lnb[i].lnb_file_offset + lnb[i].lnb_len)
899 new_size = lnb[i].lnb_file_offset + lnb[i].lnb_len;
900 if (lnb[i].lnb_page == NULL)
903 if (lnb[i].lnb_page->mapping == (void *)obj) {
904 osd_dmu_write(osd, obj->oo_dn, lnb[i].lnb_file_offset,
905 lnb[i].lnb_len, kmap(lnb[i].lnb_page) +
906 lnb[i].lnb_page_offset, oh->ot_tx);
907 kunmap(lnb[i].lnb_page);
908 iosize += lnb[i].lnb_len;
909 abufsz = lnb[i].lnb_len; /* to drop cache below */
910 } else if (lnb[i].lnb_data) {
912 LASSERT(((unsigned long)lnb[i].lnb_data & 1) == 0);
913 /* buffer loaned for zerocopy, try to use it.
914 * notice that dmu_assign_arcbuf() is smart
915 * enough to recognize changed blocksize
916 * in this case it fallbacks to dmu_write() */
917 abufsz = arc_buf_size(lnb[i].lnb_data);
918 LASSERT(abufsz & PAGE_MASK);
919 apages = abufsz >> PAGE_SHIFT;
920 LASSERT(i + apages <= npages);
921 /* these references to pages must be invalidated
922 * to prevent access in osd_bufs_put() */
923 for (j = 0; j < apages; j++)
924 lnb[i + j].lnb_page = NULL;
925 dmu_assign_arcbuf(&obj->oo_dn->dn_bonus->db,
926 lnb[i].lnb_file_offset,
927 lnb[i].lnb_data, oh->ot_tx);
928 /* drop the reference, otherwise osd_put_bufs()
929 * will be releasing it - bad! */
930 lnb[i].lnb_data = NULL;
931 atomic_dec(&osd->od_zerocopy_loan);
934 /* we don't want to deal with cache if nothing
935 * has been send to ZFS at this step */
942 /* we have to mark dbufs for eviction here because
943 * dmu_assign_arcbuf() may create a new dbuf for
945 osd_evict_dbufs_after_write(obj, lnb[i].lnb_file_offset,
949 if (unlikely(new_size == 0)) {
950 /* no pages to write, no transno is needed */
952 /* it is important to return 0 even when all lnb_rc == -ENOSPC
953 * since ofd_commitrw_write() retries several times on ENOSPC */
954 up_read(&obj->oo_guard);
955 record_end_io(osd, WRITE, 0, 0, 0);
959 /* if file has grown, take user_size into account */
960 if (user_size && new_size > user_size)
961 new_size = user_size;
962 write_lock(&obj->oo_attr_lock);
963 if (obj->oo_attr.la_size < new_size) {
964 obj->oo_attr.la_size = new_size;
965 write_unlock(&obj->oo_attr_lock);
966 /* osd_object_sa_update() will be copying directly from
967 * oo_attr into dbuf. any update within a single txg will copy
969 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
970 &obj->oo_attr.la_size, 8, oh);
972 write_unlock(&obj->oo_attr_lock);
975 up_read(&obj->oo_guard);
977 record_end_io(osd, WRITE, 0, iosize, npages);
982 static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
983 struct niobuf_local *lnb, int npages)
985 struct osd_object *obj = osd_dt_obj(dt);
989 LASSERT(dt_object_exists(dt));
992 read_lock(&obj->oo_attr_lock);
993 eof = obj->oo_attr.la_size;
994 read_unlock(&obj->oo_attr_lock);
996 for (i = 0; i < npages; i++) {
997 if (unlikely(lnb[i].lnb_rc < 0))
1000 lnb[i].lnb_rc = lnb[i].lnb_len;
1002 if (lnb[i].lnb_file_offset + lnb[i].lnb_len >= eof) {
1003 /* send complete pages all the time */
1004 if (eof <= lnb[i].lnb_file_offset)
1007 /* all subsequent rc should be 0 */
1008 while (++i < npages)
1018 * Punch/truncate an object
1020 * IN: db - dmu_buf of the object to free data in.
1021 * off - start of section to free.
1022 * len - length of section to free (DMU_OBJECT_END => to EOF).
1024 * RETURN: 0 if success
1025 * error code if failure
1027 * The transaction passed to this routine must have
1028 * dmu_tx_hold_sa() and if off < size, dmu_tx_hold_free()
1029 * called and then assigned to a transaction group.
1031 static int __osd_object_punch(struct osd_object *obj, objset_t *os,
1032 dmu_tx_t *tx, uint64_t off, uint64_t len)
1034 dnode_t *dn = obj->oo_dn;
1035 uint64_t size = obj->oo_attr.la_size;
1038 /* Assert that the transaction has been assigned to a
1039 transaction group. */
1040 LASSERT(tx->tx_txg != 0);
1042 * Nothing to do if file already at desired length.
1044 if (len == DMU_OBJECT_END && size == off)
1047 /* if object holds encrypted content, we need to make sure we truncate
1048 * on an encryption unit boundary, or subsequent reads will get
1051 if (len != DMU_OBJECT_END)
1052 len -= LUSTRE_ENCRYPTION_UNIT_SIZE -
1053 (off & ~LUSTRE_ENCRYPTION_MASK);
1054 if (obj->oo_lma_flags & LUSTRE_ENCRYPT_FL &&
1055 off & ~LUSTRE_ENCRYPTION_MASK)
1056 off = (off & LUSTRE_ENCRYPTION_MASK) +
1057 LUSTRE_ENCRYPTION_UNIT_SIZE;
1060 /* XXX: dnode_free_range() can be used to save on dnode lookup */
1062 dmu_free_range(os, dn->dn_object, off, len, tx);
1067 static int osd_punch(const struct lu_env *env, struct dt_object *dt,
1068 __u64 start, __u64 end, struct thandle *th)
1070 struct osd_object *obj = osd_dt_obj(dt);
1071 struct osd_device *osd = osd_obj2dev(obj);
1072 struct osd_thandle *oh;
1077 LASSERT(dt_object_exists(dt));
1078 LASSERT(osd_invariant(obj));
1080 LASSERT(th != NULL);
1081 oh = container_of(th, struct osd_thandle, ot_super);
1083 write_lock(&obj->oo_attr_lock);
1085 if (end == OBD_OBJECT_EOF || end >= obj->oo_attr.la_size)
1086 len = DMU_OBJECT_END;
1089 write_unlock(&obj->oo_attr_lock);
1091 down_read(&obj->oo_guard);
1092 if (obj->oo_destroyed)
1093 GOTO(out, rc = -ENOENT);
1095 rc = __osd_object_punch(obj, osd->od_os, oh->ot_tx, start, len);
1098 if (len == DMU_OBJECT_END) {
1099 write_lock(&obj->oo_attr_lock);
1100 obj->oo_attr.la_size = start;
1101 write_unlock(&obj->oo_attr_lock);
1102 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
1103 &obj->oo_attr.la_size, 8, oh);
1106 up_read(&obj->oo_guard);
1110 static int osd_declare_punch(const struct lu_env *env, struct dt_object *dt,
1111 __u64 start, __u64 end, struct thandle *handle)
1113 struct osd_object *obj = osd_dt_obj(dt);
1114 struct osd_device *osd = osd_obj2dev(obj);
1115 struct osd_thandle *oh;
1119 oh = container_of(handle, struct osd_thandle, ot_super);
1121 read_lock(&obj->oo_attr_lock);
1122 if (end == OBD_OBJECT_EOF || end >= obj->oo_attr.la_size)
1123 len = DMU_OBJECT_END;
1127 /* declare we'll free some blocks ... */
1128 /* if object holds encrypted content, we need to make sure we truncate
1129 * on an encryption unit boundary, or subsequent reads will get
1132 if (obj->oo_lma_flags & LUSTRE_ENCRYPT_FL &&
1133 start & ~LUSTRE_ENCRYPTION_MASK)
1134 start = (start & LUSTRE_ENCRYPTION_MASK) +
1135 LUSTRE_ENCRYPTION_UNIT_SIZE;
1136 if (start < obj->oo_attr.la_size) {
1137 read_unlock(&obj->oo_attr_lock);
1138 dmu_tx_mark_netfree(oh->ot_tx);
1139 dmu_tx_hold_free(oh->ot_tx, obj->oo_dn->dn_object, start, len);
1141 read_unlock(&obj->oo_attr_lock);
1144 RETURN(osd_declare_quota(env, osd, obj->oo_attr.la_uid,
1145 obj->oo_attr.la_gid, obj->oo_attr.la_projid,
1146 0, oh, NULL, OSD_QID_BLK));
1149 static int osd_ladvise(const struct lu_env *env, struct dt_object *dt,
1150 __u64 start, __u64 end, enum lu_ladvise_type advice)
1164 static int osd_fallocate(const struct lu_env *env, struct dt_object *dt,
1165 __u64 start, __u64 end, int mode, struct thandle *th)
1167 int rc = -EOPNOTSUPP;
1171 * space preallocation is not supported for ZFS
1172 * Returns -EOPNOTSUPP for now
1177 static int osd_declare_fallocate(const struct lu_env *env,
1178 struct dt_object *dt, __u64 start, __u64 end,
1179 int mode, struct thandle *th)
1181 int rc = -EOPNOTSUPP;
1185 * space preallocation is not supported for ZFS
1186 * Returns -EOPNOTSUPP for now
1191 static loff_t osd_lseek(const struct lu_env *env, struct dt_object *dt,
1192 loff_t offset, int whence)
1194 struct osd_object *obj = osd_dt_obj(dt);
1195 struct osd_device *osd = osd_obj2dev(obj);
1196 uint64_t size = obj->oo_attr.la_size;
1197 uint64_t result = offset;
1199 boolean_t hole = whence == SEEK_HOLE;
1203 LASSERT(dt_object_exists(dt));
1204 LASSERT(osd_invariant(obj));
1205 LASSERT(offset >= 0);
1207 /* for SEEK_HOLE treat 'offset' beyond the end of file as in real
1208 * hole. LOV to decide after all if that real hole or not.
1211 RETURN(hole ? offset : -ENXIO);
1213 /* Currently ZFS reports no valid DATA offset if object has dirty data
1214 * and we cannot just switch to generic way with reporting DATA on all
1215 * file offsets and HOLE beyond end of file, because we may get HOLE
1216 * reported correctly at some offset inside file then DATA will find
1217 * dirty state and be reported also at that offset by generic approach.
1218 * This is because for HOLE report ZFS doesn't check dirty state but
1220 * The only way to get reliable results is to call txg_wait_synced()
1221 * when ZFS reports EBUSY result and repeat lseek call and that is
1222 * controlled via od_sync_on_lseek option.
1224 if (!osd->od_sync_on_lseek)
1225 result = hole ? size : offset;
1228 rc = osd_dmu_offset_next(osd->od_os, obj->oo_dn->dn_object, hole,
1230 /* dirty inode, lseek result is unreliable without sync */
1232 txg_wait_synced(dmu_objset_pool(osd->od_os), 0ULL);
1239 /* ZFS is not exported all needed function, so fall back to the
1240 * generic logic: for HOLE return file size, for DATA return
1241 * the current offset
1243 if (rc == EOPNOTSUPP)
1244 result = hole ? size : offset;
1248 /* dmu_offset_next() only works on whole blocks so may return SEEK_HOLE
1249 * result as end of the last block instead of logical EOF which we need
1257 const struct dt_body_operations osd_body_ops = {
1258 .dbo_read = osd_read,
1259 .dbo_declare_write = osd_declare_write,
1260 .dbo_write = osd_write,
1261 .dbo_bufs_get = osd_bufs_get,
1262 .dbo_bufs_put = osd_bufs_put,
1263 .dbo_write_prep = osd_write_prep,
1264 .dbo_declare_write_commit = osd_declare_write_commit,
1265 .dbo_write_commit = osd_write_commit,
1266 .dbo_read_prep = osd_read_prep,
1267 .dbo_declare_punch = osd_declare_punch,
1268 .dbo_punch = osd_punch,
1269 .dbo_ladvise = osd_ladvise,
1270 .dbo_declare_fallocate = osd_declare_fallocate,
1271 .dbo_fallocate = osd_fallocate,
1272 .dbo_lseek = osd_lseek,
1275 const struct dt_body_operations osd_body_scrub_ops = {
1276 .dbo_read = osd_read_no_record,
1277 .dbo_declare_write = osd_declare_write,
1278 .dbo_write = osd_write,