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 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 loff_t _pos = pos, max = 0;
174 struct osd_thandle *oh;
178 oh = container_of(th, struct osd_thandle, ot_super);
180 /* in some cases declare can race with creation (e.g. llog)
181 * and we need to wait till object is initialized. notice
182 * LOHA_EXISTs is supposed to be the last step in the
185 /* size change (in dnode) will be declared by dmu_tx_hold_write() */
186 if (dt_object_exists(dt))
187 oid = obj->oo_dn->dn_object;
189 oid = DMU_NEW_OBJECT;
191 /* XXX: we still miss for append declaration support in ZFS
192 * -1 means append which is used by llog mostly, llog
193 * can grow upto LLOG_MIN_CHUNK_SIZE*8 records */
194 max = max_t(loff_t, 256 * 8 * LLOG_MIN_CHUNK_SIZE,
195 obj->oo_attr.la_size + (2 << 20));
199 loff_t tstart, tend, end = pos + buf->lb_len;
202 /* try to find a close declared window to fit/extend */
203 for (txh = list_head(&oh->ot_tx->tx_holds); txh != NULL;
204 txh = list_next(&oh->ot_tx->tx_holds, txh)) {
205 if (obj->oo_dn != txh->txh_dnode)
207 if (txh->txh_type != THT_WRITE)
210 /* bytes already declared in this handle */
211 tstart = txh->txh_arg1;
212 tend = txh->txh_arg1 + txh->txh_arg2;
218 /* if this is an append, then extend it */
219 if (_pos == -1 && txh->txh_arg1 == max)
221 /* don't let too big appends */
222 if (tend - tstart > 4*1024*1024)
224 if (pos >= tend || end <= tstart)
227 txh->txh_arg1 = tstart;
228 txh->txh_arg2 = tend - tstart;
232 osd_tx_hold_write(oh->ot_tx, oid, obj->oo_dn, pos, buf->lb_len);
234 /* dt_declare_write() is usually called for system objects, such
235 * as llog or last_rcvd files. We needn't enforce quota on those
236 * objects, so always set the lqi_space as 0. */
237 RETURN(osd_declare_quota(env, osd, obj->oo_attr.la_uid,
238 obj->oo_attr.la_gid, obj->oo_attr.la_projid,
239 0, oh, NULL, OSD_QID_BLK));
242 static dmu_buf_t *osd_get_dbuf(struct osd_object *obj, uint64_t offset)
244 dmu_buf_t **dbs = obj->oo_dbs;
248 blkid = dbuf_whichblock(obj->oo_dn, 0, offset);
249 for (i = 0; i < OSD_MAX_DBUFS; i++) {
250 dmu_buf_impl_t *dbi = (void *)dbs[i];
253 if (dbi->db_blkid == blkid)
256 return (dmu_buf_t *)dbuf_hold(obj->oo_dn, blkid, osd_0copy_tag);
259 static void osd_put_dbuf(struct osd_object *obj, dmu_buf_t *db)
261 dmu_buf_t **dbs = obj->oo_dbs;
264 for (i = 0; i < OSD_MAX_DBUFS; i++) {
268 /* get rid of dbuf with blkd > 0 */
269 for (i = 0; i < OSD_MAX_DBUFS; i++) {
270 if (dbs[i] == NULL) {
274 if (dbs[i]->db_offset > 0) {
275 /* replace this one */
276 dbuf_rele((dmu_buf_impl_t *)dbs[i], osd_0copy_tag);
284 static ssize_t osd_write_llog_header(struct osd_object *obj,
285 const struct lu_buf *buf, loff_t *pos,
286 struct osd_thandle *oh)
289 int len = buf->lb_len;
290 loff_t offset = *pos;
291 char *data = buf->lb_buf;
294 dmu_buf_t *db = osd_get_dbuf(obj, offset);
296 bufoff = offset - db->db_offset;
297 tocpy = MIN(db->db_size - bufoff, len);
298 if (tocpy == db->db_size)
299 dmu_buf_will_fill(db, oh->ot_tx);
301 dmu_buf_will_dirty(db, oh->ot_tx);
302 LASSERT(offset >= db->db_offset);
303 LASSERT(offset + tocpy <= db->db_offset + db->db_size);
304 (void) memcpy((char *)db->db_data + bufoff, data, tocpy);
306 if (tocpy == db->db_size)
307 dmu_buf_fill_done(db, oh->ot_tx);
313 osd_put_dbuf(obj, db);
319 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
320 const struct lu_buf *buf, loff_t *pos,
323 struct osd_object *obj = osd_dt_obj(dt);
324 struct osd_device *osd = osd_obj2dev(obj);
325 struct osd_thandle *oh;
326 uint64_t offset = *pos;
331 LASSERT(dt_object_exists(dt));
335 oh = container_of(th, struct osd_thandle, ot_super);
337 down_read(&obj->oo_guard);
338 if (obj->oo_destroyed)
339 GOTO(out, rc = -ENOENT);
341 if (fid_is_llog(lu_object_fid(&dt->do_lu))) {
342 osd_write_llog_header(obj, buf, pos, oh);
344 osd_dmu_write(osd, obj->oo_dn, offset, (uint64_t)buf->lb_len,
345 buf->lb_buf, oh->ot_tx);
347 write_lock(&obj->oo_attr_lock);
348 if (obj->oo_attr.la_size < offset + buf->lb_len) {
349 obj->oo_attr.la_size = offset + buf->lb_len;
350 write_unlock(&obj->oo_attr_lock);
351 /* osd_object_sa_update() will be copying directly from oo_attr
352 * into dbuf. any update within a single txg will copy the
354 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
355 &obj->oo_attr.la_size, 8, oh);
359 write_unlock(&obj->oo_attr_lock);
366 up_read(&obj->oo_guard);
371 * XXX: for the moment I don't want to use lnb_flags for osd-internal
372 * purposes as it's not very well defined ...
373 * instead I use the lowest bit of the address so that:
374 * arc buffer: .lnb_data = abuf (arc we loan for write)
375 * dbuf buffer: .lnb_data = dbuf | 1 (dbuf we get for read)
376 * copy buffer: .lnb_page->mapping = obj (page we allocate for write)
380 static int osd_bufs_put(const struct lu_env *env, struct dt_object *dt,
381 struct niobuf_local *lnb, int npages)
383 struct osd_object *obj = osd_dt_obj(dt);
384 struct osd_device *osd = osd_obj2dev(obj);
388 LASSERT(dt_object_exists(dt));
391 for (i = 0; i < npages; i++) {
392 if (lnb[i].lnb_page == NULL)
394 if (lnb[i].lnb_page->mapping == (void *)obj) {
395 /* this is anonymous page allocated for copy-write */
396 lnb[i].lnb_page->mapping = NULL;
397 __free_page(lnb[i].lnb_page);
398 atomic_dec(&osd->od_zerocopy_alloc);
400 /* see comment in osd_bufs_get_read() */
401 ptr = (unsigned long)lnb[i].lnb_data;
404 dmu_buf_rele((void *)ptr, osd_0copy_tag);
405 atomic_dec(&osd->od_zerocopy_pin);
406 } else if (lnb[i].lnb_data != NULL) {
407 int j, apages, abufsz;
408 abufsz = arc_buf_size(lnb[i].lnb_data);
409 apages = abufsz >> PAGE_SHIFT;
410 /* these references to pages must be invalidated
411 * to prevent access in osd_bufs_put() */
412 for (j = 0; j < apages; j++)
413 lnb[i + j].lnb_page = NULL;
414 dmu_return_arcbuf(lnb[i].lnb_data);
415 atomic_dec(&osd->od_zerocopy_loan);
418 lnb[i].lnb_page = NULL;
419 lnb[i].lnb_data = NULL;
425 static inline struct page *kmem_to_page(void *addr)
427 if (is_vmalloc_addr(addr))
428 return vmalloc_to_page(addr);
430 return virt_to_page(addr);
434 * Prepare buffers for read.
436 * The function maps the range described by \a off and \a len to \a lnb array.
437 * dmu_buf_hold_array_by_bonus() finds/creates appropriate ARC buffers, then
438 * we fill \a lnb array with the pages storing ARC buffers. Notice the current
439 * implementationt passes TRUE to dmu_buf_hold_array_by_bonus() to fill ARC
440 * buffers with actual data, I/O is done in the conext of osd_bufs_get_read().
441 * A better implementation would just return the buffers (potentially unfilled)
442 * and subsequent osd_read_prep() would do I/O for many ranges concurrently.
444 * \param[in] env environment
445 * \param[in] obj object
446 * \param[in] off offset in bytes
447 * \param[in] len the number of bytes to access
448 * \param[out] lnb array of local niobufs pointing to the buffers with data
450 * \retval 0 for success
451 * \retval negative error number of failure
453 static int osd_bufs_get_read(const struct lu_env *env, struct osd_object *obj,
454 loff_t off, ssize_t len, struct niobuf_local *lnb,
457 struct osd_device *osd = osd_obj2dev(obj);
458 int rc, i, numbufs, npages = 0, drop_cache = 0;
459 hrtime_t start = gethrtime();
464 record_start_io(osd, READ, 0);
466 if (obj->oo_attr.la_size >= osd->od_readcache_max_filesize)
469 /* grab buffers for read:
470 * OSD API let us to grab buffers first, then initiate IO(s)
471 * so that all required IOs will be done in parallel, but at the
472 * moment DMU doesn't provide us with a method to grab buffers.
473 * If we discover this is a vital for good performance we
474 * can get own replacement for dmu_buf_hold_array_by_bonus().
477 (obj->oo_dn->dn_datablkshift != 0 ||
478 off < obj->oo_dn->dn_datablksz)) {
479 if (obj->oo_dn->dn_datablkshift == 0 &&
480 off + len > obj->oo_dn->dn_datablksz)
481 len = obj->oo_dn->dn_datablksz - off;
484 if (unlikely(npages >= maxlnb))
485 GOTO(err, rc = -EOVERFLOW);
487 rc = -dmu_buf_hold_array_by_bonus(&obj->oo_dn->dn_bonus->db,
488 off, len, TRUE, osd_0copy_tag,
493 for (i = 0; i < numbufs; i++) {
494 int bufoff, tocpy, thispage;
499 atomic_inc(&osd->od_zerocopy_pin);
501 bufoff = off - dbp[i]->db_offset;
502 tocpy = min_t(int, dbp[i]->db_size - bufoff, len);
504 /* kind of trick to differentiate dbuf vs. arcbuf */
505 LASSERT(((unsigned long)dbp[i] & 1) == 0);
506 dbf = (void *) ((unsigned long)dbp[i] | 1);
509 if (unlikely(npages >= maxlnb))
510 GOTO(err, rc = -EOVERFLOW);
512 thispage = PAGE_SIZE;
513 thispage -= bufoff & (PAGE_SIZE - 1);
514 thispage = min(tocpy, thispage);
517 lnb->lnb_file_offset = off;
518 lnb->lnb_page_offset = bufoff & ~PAGE_MASK;
519 lnb->lnb_len = thispage;
520 lnb->lnb_page = kmem_to_page(dbp[i]->db_data +
522 /* mark just a single slot: we need this
523 * reference to dbuf to be released once */
537 dbuf_set_pending_evict(dbp[i]);
539 /* steal dbuf so dmu_buf_rele_array() can't release
544 dmu_buf_rele_array(dbp, numbufs, osd_0copy_tag);
547 delta_ms = gethrtime() - start;
548 do_div(delta_ms, NSEC_PER_MSEC);
549 record_end_io(osd, READ, delta_ms, npages * PAGE_SIZE, npages);
556 dmu_buf_rele_array(dbp, numbufs, osd_0copy_tag);
557 osd_bufs_put(env, &obj->oo_dt, lnb - npages, npages);
561 static inline arc_buf_t *osd_request_arcbuf(dnode_t *dn, size_t bs)
565 abuf = dmu_request_arcbuf(&dn->dn_bonus->db, bs);
567 return ERR_PTR(-ENOMEM);
569 #if ZFS_VERSION_CODE < OBD_OCD_VERSION(0, 7, 0, 0)
571 * ZFS prior to 0.7.0 doesn't guarantee PAGE_SIZE alignment for zio
572 * blocks smaller than (PAGE_SIZE << 2). This poses a problem of
573 * setting up page array for RDMA transfer. See LU-9305.
575 if ((unsigned long)abuf->b_data & ~PAGE_MASK) {
576 dmu_return_arcbuf(abuf);
584 static int osd_bufs_get_write(const struct lu_env *env, struct osd_object *obj,
585 loff_t off, ssize_t len, struct niobuf_local *lnb,
588 struct osd_device *osd = osd_obj2dev(obj);
589 int poff, plen, off_in_block, sz_in_block;
590 int rc, i = 0, npages = 0;
591 dnode_t *dn = obj->oo_dn;
593 uint32_t bs = dn->dn_datablksz;
597 * currently only full blocks are subject to zerocopy approach:
598 * so that we're sure nobody is trying to update the same block
601 if (unlikely(npages >= maxlnb))
602 GOTO(out_err, rc = -EOVERFLOW);
604 off_in_block = off & (bs - 1);
605 sz_in_block = min_t(int, bs - off_in_block, len);
608 if (sz_in_block == bs) {
609 /* full block, try to use zerocopy */
610 abuf = osd_request_arcbuf(dn, bs);
611 if (unlikely(IS_ERR(abuf)))
612 GOTO(out_err, rc = PTR_ERR(abuf));
616 atomic_inc(&osd->od_zerocopy_loan);
618 /* go over pages arcbuf contains, put them as
619 * local niobufs for ptlrpc's bulks */
620 while (sz_in_block > 0) {
621 plen = min_t(int, sz_in_block, PAGE_SIZE);
623 if (unlikely(npages >= maxlnb))
624 GOTO(out_err, rc = -EOVERFLOW);
626 lnb[i].lnb_file_offset = off;
627 lnb[i].lnb_page_offset = 0;
628 lnb[i].lnb_len = plen;
630 if (sz_in_block == bs)
631 lnb[i].lnb_data = abuf;
633 lnb[i].lnb_data = NULL;
635 /* this one is not supposed to fail */
636 lnb[i].lnb_page = kmem_to_page(abuf->b_data +
638 LASSERT(lnb[i].lnb_page);
640 lprocfs_counter_add(osd->od_stats,
641 LPROC_OSD_ZEROCOPY_IO, 1);
646 off_in_block += plen;
651 if (off_in_block == 0 && len < bs &&
652 off + len >= obj->oo_attr.la_size)
653 lprocfs_counter_add(osd->od_stats,
654 LPROC_OSD_TAIL_IO, 1);
656 /* can't use zerocopy, allocate temp. buffers */
657 poff = off & (PAGE_SIZE - 1);
658 while (sz_in_block > 0) {
659 plen = min_t(int, poff + sz_in_block,
663 if (unlikely(npages >= maxlnb))
664 GOTO(out_err, rc = -EOVERFLOW);
666 lnb[i].lnb_file_offset = off;
667 lnb[i].lnb_page_offset = poff;
670 lnb[i].lnb_len = plen;
672 lnb[i].lnb_data = NULL;
674 lnb[i].lnb_page = alloc_page(OSD_GFP_IO);
675 if (unlikely(lnb[i].lnb_page == NULL))
676 GOTO(out_err, rc = -ENOMEM);
678 LASSERT(lnb[i].lnb_page->mapping == NULL);
679 lnb[i].lnb_page->mapping = (void *)obj;
681 atomic_inc(&osd->od_zerocopy_alloc);
682 lprocfs_counter_add(osd->od_stats,
683 LPROC_OSD_COPY_IO, 1);
697 osd_bufs_put(env, &obj->oo_dt, lnb, npages);
701 static int osd_bufs_get(const struct lu_env *env, struct dt_object *dt,
702 loff_t offset, ssize_t len, struct niobuf_local *lnb,
703 int maxlnb, enum dt_bufs_type rw)
705 struct osd_object *obj = osd_dt_obj(dt);
708 down_read(&obj->oo_guard);
710 if (unlikely(!dt_object_exists(dt) || obj->oo_destroyed))
711 GOTO(out, rc = -ENOENT);
713 if (rw & DT_BUFS_TYPE_WRITE)
714 rc = osd_bufs_get_write(env, obj, offset, len, lnb, maxlnb);
716 rc = osd_bufs_get_read(env, obj, offset, len, lnb, maxlnb);
719 up_read(&obj->oo_guard);
723 static int osd_write_prep(const struct lu_env *env, struct dt_object *dt,
724 struct niobuf_local *lnb, int npages)
726 struct osd_object *obj = osd_dt_obj(dt);
728 LASSERT(dt_object_exists(dt));
734 static inline uint64_t osd_roundup2blocksz(uint64_t size,
740 size += offset % blksz;
742 if (likely(is_power_of_2(blksz)))
743 return round_up(size, blksz);
745 return DIV_ROUND_UP_ULL(size, blksz) * blksz;
748 static int osd_declare_write_commit(const struct lu_env *env,
749 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;
758 uint32_t blksz = obj->oo_dn->dn_datablksz;
762 struct page *last_page = NULL;
763 unsigned long discont_pages = 0;
764 enum osd_quota_local_flags local_flags = 0;
765 enum osd_qid_declare_flags declare_flags = OSD_QID_BLK;
768 LASSERT(dt_object_exists(dt));
774 oh = container_of(th, struct osd_thandle, ot_super);
776 for (i = 0; i < npages; i++) {
777 if (last_page && lnb[i].lnb_page->index != (last_page->index + 1))
779 last_page = lnb[i].lnb_page;
781 /* ENOSPC, network RPC error, etc.
782 * We don't want to book space for pages which will be
783 * skipped in osd_write_commit(). Hence we skip pages
784 * with lnb_rc != 0 here too */
786 /* ignore quota for the whole request if any page is from
787 * client cache or written by root.
789 * XXX we could handle this on per-lnb basis as done by
791 if ((lnb[i].lnb_flags & OBD_BRW_NOQUOTA) ||
792 (lnb[i].lnb_flags & OBD_BRW_SYS_RESOURCE) ||
793 !(lnb[i].lnb_flags & OBD_BRW_SYNC))
794 declare_flags |= OSD_QID_FORCE;
797 /* first valid lnb */
798 offset = lnb[i].lnb_file_offset;
799 size = lnb[i].lnb_len;
802 if (offset + size == lnb[i].lnb_file_offset) {
803 /* this lnb is contiguous to the previous one */
804 size += lnb[i].lnb_len;
808 osd_tx_hold_write(oh->ot_tx, obj->oo_dn->dn_object,
809 obj->oo_dn, offset, size);
810 /* Estimating space to be consumed by a write is rather
811 * complicated with ZFS. As a consequence, we don't account for
812 * indirect blocks and just use as a rough estimate the worse
813 * case where the old space is being held by a snapshot. Quota
814 * overrun will be adjusted once the operation is committed, if
816 space += osd_roundup2blocksz(size, offset, blksz);
818 offset = lnb[i].lnb_file_offset;
819 size = lnb[i].lnb_len;
823 osd_tx_hold_write(oh->ot_tx, obj->oo_dn->dn_object, obj->oo_dn,
825 space += osd_roundup2blocksz(size, offset, blksz);
828 /* backend zfs filesystem might be configured to store multiple data
830 space *= osd->od_os->os_copies;
832 CDEBUG(D_QUOTA, "writing %d pages, reserving %lldK of quota space\n",
835 record_start_io(osd, WRITE, discont_pages);
837 /* acquire quota space if needed */
838 rc = osd_declare_quota(env, osd, obj->oo_attr.la_uid,
839 obj->oo_attr.la_gid, obj->oo_attr.la_projid,
840 space, oh, &local_flags, declare_flags);
842 if (!synced && rc == -EDQUOT &&
843 (local_flags & QUOTA_FL_SYNC) != 0) {
844 dt_sync(env, th->th_dev);
846 CDEBUG(D_QUOTA, "retry after sync\n");
851 /* we need only to store the overquota flags in the first lnb for
852 * now, once we support multiple objects BRW, this code needs be
854 if (local_flags & QUOTA_FL_OVER_USRQUOTA)
855 lnb[0].lnb_flags |= OBD_BRW_OVER_USRQUOTA;
856 if (local_flags & QUOTA_FL_OVER_GRPQUOTA)
857 lnb[0].lnb_flags |= OBD_BRW_OVER_GRPQUOTA;
858 #ifdef ZFS_PROJINHERIT
859 if (local_flags & QUOTA_FL_OVER_PRJQUOTA)
860 lnb[0].lnb_flags |= OBD_BRW_OVER_PRJQUOTA;
861 if (local_flags & QUOTA_FL_ROOT_PRJQUOTA)
862 lnb[0].lnb_flags |= OBD_BRW_ROOT_PRJQUOTA;
869 * Policy to grow ZFS block size by write pattern.
870 * For sequential write, it grows block size gradually until it reaches the
871 * maximum blocksize the dataset can support. Otherwise, it will pick a
872 * a block size by the writing region of this I/O.
874 static int osd_grow_blocksize(struct osd_object *obj, struct osd_thandle *oh,
875 uint64_t start, uint64_t end)
877 struct osd_device *osd = osd_obj2dev(obj);
878 dnode_t *dn = obj->oo_dn;
884 if (dn->dn_maxblkid > 0) /* can't change block size */
887 if (dn->dn_datablksz >= osd->od_max_blksz)
890 down_write(&obj->oo_guard);
892 blksz = dn->dn_datablksz;
893 if (blksz >= osd->od_max_blksz) /* check again after grabbing lock */
894 GOTO(out_unlock, rc);
896 /* now ZFS can support up to 16MB block size, and if the write
897 * is sequential, it just increases the block size gradually */
898 if (start <= blksz) { /* sequential */
899 blksz = (uint32_t)min_t(uint64_t, osd->od_max_blksz, end);
900 } else { /* sparse, pick a block size by write region */
901 blksz = (uint32_t)min_t(uint64_t, osd->od_max_blksz,
905 if (!is_power_of_2(blksz))
906 blksz = size_roundup_power2(blksz);
908 if (blksz > dn->dn_datablksz) {
909 rc = -dmu_object_set_blocksize(osd->od_os, dn->dn_object,
910 blksz, 0, oh->ot_tx);
911 LASSERT(ergo(rc == 0, dn->dn_datablksz >= blksz));
913 CDEBUG(D_INODE, "object "DFID": change block size"
914 "%u -> %u error rc = %d\n",
915 PFID(lu_object_fid(&obj->oo_dt.do_lu)),
916 dn->dn_datablksz, blksz, rc);
920 up_write(&obj->oo_guard);
925 static void osd_evict_dbufs_after_write(struct osd_object *obj,
926 loff_t off, ssize_t len)
931 rc = -dmu_buf_hold_array_by_bonus(&obj->oo_dn->dn_bonus->db, off, len,
932 TRUE, osd_0copy_tag, &numbufs, &dbp);
936 for (i = 0; i < numbufs; i++)
937 dbuf_set_pending_evict(dbp[i]);
939 dmu_buf_rele_array(dbp, numbufs, osd_0copy_tag);
942 static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
943 struct niobuf_local *lnb, int npages,
944 struct thandle *th, __u64 user_size)
946 struct osd_object *obj = osd_dt_obj(dt);
947 struct osd_device *osd = osd_obj2dev(obj);
948 struct osd_thandle *oh;
949 uint64_t new_size = 0;
950 int i, abufsz, rc = 0, drop_cache = 0;
951 unsigned long iosize = 0;
954 LASSERT(dt_object_exists(dt));
958 oh = container_of(th, struct osd_thandle, ot_super);
960 /* adjust block size. Assume the buffers are sorted. */
961 (void)osd_grow_blocksize(obj, oh, lnb[0].lnb_file_offset,
962 lnb[npages - 1].lnb_file_offset +
963 lnb[npages - 1].lnb_len);
965 if (obj->oo_attr.la_size >= osd->od_readcache_max_filesize ||
966 lnb[npages - 1].lnb_file_offset + lnb[npages - 1].lnb_len >=
967 osd->od_readcache_max_filesize)
970 if (CFS_FAIL_CHECK(OBD_FAIL_OST_MAPBLK_ENOSPC))
973 /* if la_size is already bigger than specified user_size,
976 if (obj->oo_attr.la_size > user_size)
979 /* LU-8791: take oo_guard to avoid the deadlock that changing block
980 * size and assigning arcbuf take place at the same time.
984 * -> osd_grow_blocksize() with osd_object::oo_guard held
985 * -> dmu_object_set_blocksize()
986 * -> dnode_set_blksz(), with dnode_t::dn_struct_rwlock
989 * -> dmu_buf_will_dirty()
991 * -> wait for the dbuf state to change
994 * -> dmu_assign_arcbuf()
995 * -> dbuf_assign_arcbuf(), set dbuf state to DB_FILL
997 * -> try to hold the read lock of dnode_t::dn_struct_rwlock
999 * By taking the read lock, it can avoid thread 2 to enter into the
1000 * critical section of assigning the arcbuf, while thread 1 is
1001 * changing the block size.
1003 down_read(&obj->oo_guard);
1004 if (obj->oo_destroyed) {
1005 up_read(&obj->oo_guard);
1009 for (i = 0; i < npages; i++) {
1010 CDEBUG(D_INODE, "write %u bytes at %u\n",
1011 (unsigned) lnb[i].lnb_len,
1012 (unsigned) lnb[i].lnb_file_offset);
1014 if (lnb[i].lnb_rc) {
1015 /* ENOSPC, network RPC error, etc.
1016 * Unlike ldiskfs, zfs allocates new blocks on rewrite,
1017 * so we skip this page if lnb_rc is set to -ENOSPC */
1018 CDEBUG(D_INODE, "obj "DFID": skipping lnb[%u]: rc=%d\n",
1019 PFID(lu_object_fid(&dt->do_lu)), i,
1024 if (new_size < lnb[i].lnb_file_offset + lnb[i].lnb_len)
1025 new_size = lnb[i].lnb_file_offset + lnb[i].lnb_len;
1026 if (lnb[i].lnb_page == NULL)
1029 if (lnb[i].lnb_page->mapping == (void *)obj) {
1030 osd_dmu_write(osd, obj->oo_dn, lnb[i].lnb_file_offset,
1031 lnb[i].lnb_len, kmap(lnb[i].lnb_page) +
1032 lnb[i].lnb_page_offset, oh->ot_tx);
1033 kunmap(lnb[i].lnb_page);
1034 iosize += lnb[i].lnb_len;
1035 abufsz = lnb[i].lnb_len; /* to drop cache below */
1036 } else if (lnb[i].lnb_data) {
1038 LASSERT(((unsigned long)lnb[i].lnb_data & 1) == 0);
1039 /* buffer loaned for zerocopy, try to use it.
1040 * notice that dmu_assign_arcbuf() is smart
1041 * enough to recognize changed blocksize
1042 * in this case it fallbacks to dmu_write() */
1043 abufsz = arc_buf_size(lnb[i].lnb_data);
1044 LASSERT(abufsz & PAGE_MASK);
1045 apages = abufsz >> PAGE_SHIFT;
1046 LASSERT(i + apages <= npages);
1047 /* these references to pages must be invalidated
1048 * to prevent access in osd_bufs_put() */
1049 for (j = 0; j < apages; j++)
1050 lnb[i + j].lnb_page = NULL;
1051 dmu_assign_arcbuf(&obj->oo_dn->dn_bonus->db,
1052 lnb[i].lnb_file_offset,
1053 lnb[i].lnb_data, oh->ot_tx);
1054 /* drop the reference, otherwise osd_put_bufs()
1055 * will be releasing it - bad! */
1056 lnb[i].lnb_data = NULL;
1057 atomic_dec(&osd->od_zerocopy_loan);
1060 /* we don't want to deal with cache if nothing
1061 * has been send to ZFS at this step */
1068 /* we have to mark dbufs for eviction here because
1069 * dmu_assign_arcbuf() may create a new dbuf for
1071 osd_evict_dbufs_after_write(obj, lnb[i].lnb_file_offset,
1075 if (unlikely(new_size == 0)) {
1076 /* no pages to write, no transno is needed */
1078 /* it is important to return 0 even when all lnb_rc == -ENOSPC
1079 * since ofd_commitrw_write() retries several times on ENOSPC */
1080 up_read(&obj->oo_guard);
1081 record_end_io(osd, WRITE, 0, 0, 0);
1085 /* if file has grown, take user_size into account */
1086 if (user_size && new_size > user_size)
1087 new_size = user_size;
1088 write_lock(&obj->oo_attr_lock);
1089 if (obj->oo_attr.la_size < new_size) {
1090 obj->oo_attr.la_size = new_size;
1091 write_unlock(&obj->oo_attr_lock);
1092 /* osd_object_sa_update() will be copying directly from
1093 * oo_attr into dbuf. any update within a single txg will copy
1094 * the most actual */
1095 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
1096 &obj->oo_attr.la_size, 8, oh);
1098 write_unlock(&obj->oo_attr_lock);
1101 up_read(&obj->oo_guard);
1103 record_end_io(osd, WRITE, 0, iosize, npages);
1108 static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
1109 struct niobuf_local *lnb, int npages)
1111 struct osd_object *obj = osd_dt_obj(dt);
1115 LASSERT(dt_object_exists(dt));
1116 LASSERT(obj->oo_dn);
1118 read_lock(&obj->oo_attr_lock);
1119 eof = obj->oo_attr.la_size;
1120 read_unlock(&obj->oo_attr_lock);
1122 for (i = 0; i < npages; i++) {
1123 if (unlikely(lnb[i].lnb_rc < 0))
1126 lnb[i].lnb_rc = lnb[i].lnb_len;
1128 if (lnb[i].lnb_file_offset + lnb[i].lnb_len >= eof) {
1129 /* send complete pages all the time */
1130 if (eof <= lnb[i].lnb_file_offset)
1133 /* all subsequent rc should be 0 */
1134 while (++i < npages)
1144 * Punch/truncate an object
1146 * IN: db - dmu_buf of the object to free data in.
1147 * off - start of section to free.
1148 * len - length of section to free (DMU_OBJECT_END => to EOF).
1150 * RETURN: 0 if success
1151 * error code if failure
1153 * The transaction passed to this routine must have
1154 * dmu_tx_hold_sa() and if off < size, dmu_tx_hold_free()
1155 * called and then assigned to a transaction group.
1157 static int __osd_object_punch(struct osd_object *obj, objset_t *os,
1158 dmu_tx_t *tx, uint64_t off, uint64_t len)
1160 dnode_t *dn = obj->oo_dn;
1161 uint64_t size = obj->oo_attr.la_size;
1164 /* Assert that the transaction has been assigned to a
1165 transaction group. */
1166 LASSERT(tx->tx_txg != 0);
1168 * Nothing to do if file already at desired length.
1170 if (len == DMU_OBJECT_END && size == off)
1173 /* if object holds encrypted content, we need to make sure we truncate
1174 * on an encryption unit boundary, or subsequent reads will get
1177 if (len != DMU_OBJECT_END)
1178 len -= LUSTRE_ENCRYPTION_UNIT_SIZE -
1179 (off & ~LUSTRE_ENCRYPTION_MASK);
1180 if (obj->oo_lma_flags & LUSTRE_ENCRYPT_FL &&
1181 off & ~LUSTRE_ENCRYPTION_MASK)
1182 off = (off & LUSTRE_ENCRYPTION_MASK) +
1183 LUSTRE_ENCRYPTION_UNIT_SIZE;
1186 /* XXX: dnode_free_range() can be used to save on dnode lookup */
1188 dmu_free_range(os, dn->dn_object, off, len, tx);
1193 static int osd_punch(const struct lu_env *env, struct dt_object *dt,
1194 __u64 start, __u64 end, struct thandle *th)
1196 struct osd_object *obj = osd_dt_obj(dt);
1197 struct osd_device *osd = osd_obj2dev(obj);
1198 struct osd_thandle *oh;
1203 LASSERT(dt_object_exists(dt));
1204 LASSERT(osd_invariant(obj));
1206 LASSERT(th != NULL);
1207 oh = container_of(th, struct osd_thandle, ot_super);
1209 write_lock(&obj->oo_attr_lock);
1211 if (end == OBD_OBJECT_EOF || end >= obj->oo_attr.la_size)
1212 len = DMU_OBJECT_END;
1215 write_unlock(&obj->oo_attr_lock);
1217 down_read(&obj->oo_guard);
1218 if (obj->oo_destroyed)
1219 GOTO(out, rc = -ENOENT);
1221 rc = __osd_object_punch(obj, osd->od_os, oh->ot_tx, start, len);
1224 if (len == DMU_OBJECT_END) {
1225 write_lock(&obj->oo_attr_lock);
1226 obj->oo_attr.la_size = start;
1227 write_unlock(&obj->oo_attr_lock);
1228 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
1229 &obj->oo_attr.la_size, 8, oh);
1232 up_read(&obj->oo_guard);
1236 static int osd_declare_punch(const struct lu_env *env, struct dt_object *dt,
1237 __u64 start, __u64 end, struct thandle *handle)
1239 struct osd_object *obj = osd_dt_obj(dt);
1240 struct osd_device *osd = osd_obj2dev(obj);
1241 struct osd_thandle *oh;
1245 oh = container_of(handle, struct osd_thandle, ot_super);
1247 read_lock(&obj->oo_attr_lock);
1248 if (end == OBD_OBJECT_EOF || end >= obj->oo_attr.la_size)
1249 len = DMU_OBJECT_END;
1253 /* declare we'll free some blocks ... */
1254 /* if object holds encrypted content, we need to make sure we truncate
1255 * on an encryption unit boundary, or subsequent reads will get
1258 if (obj->oo_lma_flags & LUSTRE_ENCRYPT_FL &&
1259 start & ~LUSTRE_ENCRYPTION_MASK)
1260 start = (start & LUSTRE_ENCRYPTION_MASK) +
1261 LUSTRE_ENCRYPTION_UNIT_SIZE;
1262 if (start < obj->oo_attr.la_size) {
1263 read_unlock(&obj->oo_attr_lock);
1264 dmu_tx_mark_netfree(oh->ot_tx);
1265 dmu_tx_hold_free(oh->ot_tx, obj->oo_dn->dn_object, start, len);
1267 read_unlock(&obj->oo_attr_lock);
1270 RETURN(osd_declare_quota(env, osd, obj->oo_attr.la_uid,
1271 obj->oo_attr.la_gid, obj->oo_attr.la_projid,
1272 0, oh, NULL, OSD_QID_BLK));
1275 static int osd_ladvise(const struct lu_env *env, struct dt_object *dt,
1276 __u64 start, __u64 end, enum lu_ladvise_type advice)
1290 static int osd_fallocate(const struct lu_env *env, struct dt_object *dt,
1291 __u64 start, __u64 end, int mode, struct thandle *th)
1293 int rc = -EOPNOTSUPP;
1297 * space preallocation is not supported for ZFS
1298 * Returns -EOPNOTSUPP for now
1303 static int osd_declare_fallocate(const struct lu_env *env,
1304 struct dt_object *dt, __u64 start, __u64 end,
1305 int mode, struct thandle *th)
1307 int rc = -EOPNOTSUPP;
1311 * space preallocation is not supported for ZFS
1312 * Returns -EOPNOTSUPP for now
1317 static loff_t osd_lseek(const struct lu_env *env, struct dt_object *dt,
1318 loff_t offset, int whence)
1320 struct osd_object *obj = osd_dt_obj(dt);
1321 struct osd_device *osd = osd_obj2dev(obj);
1322 uint64_t size = obj->oo_attr.la_size;
1323 uint64_t result = offset;
1325 boolean_t hole = whence == SEEK_HOLE;
1329 LASSERT(dt_object_exists(dt));
1330 LASSERT(osd_invariant(obj));
1331 LASSERT(offset >= 0);
1333 /* for SEEK_HOLE treat 'offset' beyond the end of file as in real
1334 * hole. LOV to decide after all if that real hole or not.
1337 RETURN(hole ? offset : -ENXIO);
1339 /* Currently ZFS reports no valid DATA offset if object has dirty data
1340 * and we cannot just switch to generic way with reporting DATA on all
1341 * file offsets and HOLE beyond end of file, because we may get HOLE
1342 * reported correctly at some offset inside file then DATA will find
1343 * dirty state and be reported also at that offset by generic approach.
1344 * This is because for HOLE report ZFS doesn't check dirty state but
1346 * The only way to get reliable results is to call txg_wait_synced()
1347 * when ZFS reports EBUSY result and repeat lseek call and that is
1348 * controlled via od_sync_on_lseek option.
1350 if (!osd->od_sync_on_lseek)
1351 result = hole ? size : offset;
1354 rc = osd_dmu_offset_next(osd->od_os, obj->oo_dn->dn_object, hole,
1356 /* dirty inode, lseek result is unreliable without sync */
1358 txg_wait_synced(dmu_objset_pool(osd->od_os), 0ULL);
1365 /* ZFS is not exported all needed function, so fall back to the
1366 * generic logic: for HOLE return file size, for DATA return
1367 * the current offset
1369 if (rc == EOPNOTSUPP)
1370 result = hole ? size : offset;
1374 /* dmu_offset_next() only works on whole blocks so may return SEEK_HOLE
1375 * result as end of the last block instead of logical EOF which we need
1383 const struct dt_body_operations osd_body_ops = {
1384 .dbo_read = osd_read,
1385 .dbo_declare_write = osd_declare_write,
1386 .dbo_write = osd_write,
1387 .dbo_bufs_get = osd_bufs_get,
1388 .dbo_bufs_put = osd_bufs_put,
1389 .dbo_write_prep = osd_write_prep,
1390 .dbo_declare_write_commit = osd_declare_write_commit,
1391 .dbo_write_commit = osd_write_commit,
1392 .dbo_read_prep = osd_read_prep,
1393 .dbo_declare_punch = osd_declare_punch,
1394 .dbo_punch = osd_punch,
1395 .dbo_ladvise = osd_ladvise,
1396 .dbo_declare_fallocate = osd_declare_fallocate,
1397 .dbo_fallocate = osd_fallocate,
1398 .dbo_lseek = osd_lseek,
1401 const struct dt_body_operations osd_body_scrub_ops = {
1402 .dbo_read = osd_read_no_record,
1403 .dbo_declare_write = osd_declare_write,
1404 .dbo_write = osd_write,