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/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/osd-zfs/osd_io.c
34 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
35 * Author: Mike Pershin <tappro@whamcloud.com>
38 #define DEBUG_SUBSYSTEM S_OSD
40 #include <libcfs/libcfs.h>
41 #include <obd_support.h>
42 #include <lustre_net.h>
44 #include <obd_class.h>
45 #include <lustre_disk.h>
46 #include <lustre_fid.h>
47 #include <lustre_quota.h>
49 #include "osd_internal.h"
51 #include <sys/dnode.h>
56 #include <sys/spa_impl.h>
57 #include <sys/zfs_znode.h>
58 #include <sys/dmu_tx.h>
59 #include <sys/dmu_objset.h>
60 #include <sys/dsl_prop.h>
61 #include <sys/sa_impl.h>
64 static char *osd_0copy_tag = "zerocopy";
67 static void dbuf_set_pending_evict(dmu_buf_t *db)
69 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
70 dbi->db_pending_evict = TRUE;
73 static void record_start_io(struct osd_device *osd, int rw, int discont_pages)
75 struct obd_histogram *h = osd->od_brw_stats.hist;
78 atomic_inc(&osd->od_r_in_flight);
79 lprocfs_oh_tally(&h[BRW_R_RPC_HIST],
80 atomic_read(&osd->od_r_in_flight));
81 lprocfs_oh_tally(&h[BRW_R_DISCONT_PAGES], discont_pages);
84 atomic_inc(&osd->od_w_in_flight);
85 lprocfs_oh_tally(&h[BRW_W_RPC_HIST],
86 atomic_read(&osd->od_w_in_flight));
87 lprocfs_oh_tally(&h[BRW_W_DISCONT_PAGES], discont_pages);
92 static void record_end_io(struct osd_device *osd, int rw,
93 unsigned long elapsed, int disksize, int npages)
95 struct obd_histogram *h = osd->od_brw_stats.hist;
98 atomic_dec(&osd->od_r_in_flight);
100 atomic_dec(&osd->od_w_in_flight);
102 lprocfs_oh_tally_log2(&h[BRW_R_PAGES + rw], npages);
104 lprocfs_oh_tally_log2(&h[BRW_R_DISK_IOSIZE + rw], disksize);
106 lprocfs_oh_tally_log2(&h[BRW_R_IO_TIME + rw], elapsed);
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;
150 record_start_io(osd, READ, 0);
151 rc = __osd_read(env, dt, buf, pos, &size);
152 delta_ms = ktime_ms_delta(ktime_get(), start);
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_of0(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_of0(th, struct osd_thandle, ot_super);
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);
249 * XXX: for the moment I don't want to use lnb_flags for osd-internal
250 * purposes as it's not very well defined ...
251 * instead I use the lowest bit of the address so that:
252 * arc buffer: .lnb_data = abuf (arc we loan for write)
253 * dbuf buffer: .lnb_data = dbuf | 1 (dbuf we get for read)
254 * copy buffer: .lnb_page->mapping = obj (page we allocate for write)
258 static int osd_bufs_put(const struct lu_env *env, struct dt_object *dt,
259 struct niobuf_local *lnb, int npages)
261 struct osd_object *obj = osd_dt_obj(dt);
262 struct osd_device *osd = osd_obj2dev(obj);
266 LASSERT(dt_object_exists(dt));
269 for (i = 0; i < npages; i++) {
270 if (lnb[i].lnb_page == NULL)
272 if (lnb[i].lnb_page->mapping == (void *)obj) {
273 /* this is anonymous page allocated for copy-write */
274 lnb[i].lnb_page->mapping = NULL;
275 __free_page(lnb[i].lnb_page);
276 atomic_dec(&osd->od_zerocopy_alloc);
278 /* see comment in osd_bufs_get_read() */
279 ptr = (unsigned long)lnb[i].lnb_data;
282 dmu_buf_rele((void *)ptr, osd_0copy_tag);
283 atomic_dec(&osd->od_zerocopy_pin);
284 } else if (lnb[i].lnb_data != NULL) {
285 int j, apages, abufsz;
286 abufsz = arc_buf_size(lnb[i].lnb_data);
287 apages = abufsz / PAGE_SIZE;
288 /* these references to pages must be invalidated
289 * to prevent access in osd_bufs_put() */
290 for (j = 0; j < apages; j++)
291 lnb[i + j].lnb_page = NULL;
292 dmu_return_arcbuf(lnb[i].lnb_data);
293 atomic_dec(&osd->od_zerocopy_loan);
296 lnb[i].lnb_page = NULL;
297 lnb[i].lnb_data = NULL;
303 static inline struct page *kmem_to_page(void *addr)
305 LASSERT(!((unsigned long)addr & ~PAGE_MASK));
306 if (is_vmalloc_addr(addr))
307 return vmalloc_to_page(addr);
309 return virt_to_page(addr);
313 * Prepare buffers for read.
315 * The function maps the range described by \a off and \a len to \a lnb array.
316 * dmu_buf_hold_array_by_bonus() finds/creates appropriate ARC buffers, then
317 * we fill \a lnb array with the pages storing ARC buffers. Notice the current
318 * implementationt passes TRUE to dmu_buf_hold_array_by_bonus() to fill ARC
319 * buffers with actual data, I/O is done in the conext of osd_bufs_get_read().
320 * A better implementation would just return the buffers (potentially unfilled)
321 * and subsequent osd_read_prep() would do I/O for many ranges concurrently.
323 * \param[in] env environment
324 * \param[in] obj object
325 * \param[in] off offset in bytes
326 * \param[in] len the number of bytes to access
327 * \param[out] lnb array of local niobufs pointing to the buffers with data
329 * \retval 0 for success
330 * \retval negative error number of failure
332 static int osd_bufs_get_read(const struct lu_env *env, struct osd_object *obj,
333 loff_t off, ssize_t len, struct niobuf_local *lnb)
335 struct osd_device *osd = osd_obj2dev(obj);
336 int rc, i, numbufs, npages = 0, drop_cache = 0;
337 ktime_t start = ktime_get();
342 record_start_io(osd, READ, 0);
344 if (obj->oo_attr.la_size >= osd->od_readcache_max_filesize)
347 /* grab buffers for read:
348 * OSD API let us to grab buffers first, then initiate IO(s)
349 * so that all required IOs will be done in parallel, but at the
350 * moment DMU doesn't provide us with a method to grab buffers.
351 * If we discover this is a vital for good performance we
352 * can get own replacement for dmu_buf_hold_array_by_bonus().
355 (obj->oo_dn->dn_datablkshift != 0 ||
356 off < obj->oo_dn->dn_datablksz)) {
357 if (obj->oo_dn->dn_datablkshift == 0 &&
358 off + len > obj->oo_dn->dn_datablksz)
359 len = obj->oo_dn->dn_datablksz - off;
361 rc = -dmu_buf_hold_array_by_bonus(&obj->oo_dn->dn_bonus->db,
362 off, len, TRUE, osd_0copy_tag,
367 for (i = 0; i < numbufs; i++) {
368 int bufoff, tocpy, thispage;
373 atomic_inc(&osd->od_zerocopy_pin);
375 bufoff = off - dbp[i]->db_offset;
376 tocpy = min_t(int, dbp[i]->db_size - bufoff, len);
378 /* kind of trick to differentiate dbuf vs. arcbuf */
379 LASSERT(((unsigned long)dbp[i] & 1) == 0);
380 dbf = (void *) ((unsigned long)dbp[i] | 1);
383 thispage = PAGE_SIZE;
384 thispage -= bufoff & (PAGE_SIZE - 1);
385 thispage = min(tocpy, thispage);
388 lnb->lnb_file_offset = off;
389 lnb->lnb_page_offset = bufoff & ~PAGE_MASK;
390 lnb->lnb_len = thispage;
391 lnb->lnb_page = kmem_to_page(dbp[i]->db_data +
393 /* mark just a single slot: we need this
394 * reference to dbuf to be released once */
408 dbuf_set_pending_evict(dbp[i]);
410 /* steal dbuf so dmu_buf_rele_array() can't release
415 dmu_buf_rele_array(dbp, numbufs, osd_0copy_tag);
418 delta_ms = ktime_ms_delta(ktime_get(), start);
419 record_end_io(osd, READ, delta_ms, npages * PAGE_SIZE, npages);
425 osd_bufs_put(env, &obj->oo_dt, lnb - npages, npages);
429 static inline arc_buf_t *osd_request_arcbuf(dnode_t *dn, size_t bs)
433 abuf = dmu_request_arcbuf(&dn->dn_bonus->db, bs);
435 return ERR_PTR(-ENOMEM);
437 #if ZFS_VERSION_CODE < OBD_OCD_VERSION(0, 7, 0, 0)
439 * ZFS prior to 0.7.0 doesn't guarantee PAGE_SIZE alignment for zio
440 * blocks smaller than (PAGE_SIZE << 2). This poses a problem of
441 * setting up page array for RDMA transfer. See LU-9305.
443 if ((unsigned long)abuf->b_data & ~PAGE_MASK) {
444 dmu_return_arcbuf(abuf);
452 static int osd_bufs_get_write(const struct lu_env *env, struct osd_object *obj,
453 loff_t off, ssize_t len, struct niobuf_local *lnb)
455 struct osd_device *osd = osd_obj2dev(obj);
456 int poff, plen, off_in_block, sz_in_block;
457 int rc, i = 0, npages = 0;
458 dnode_t *dn = obj->oo_dn;
460 uint32_t bs = dn->dn_datablksz;
464 * currently only full blocks are subject to zerocopy approach:
465 * so that we're sure nobody is trying to update the same block
468 LASSERT(npages < PTLRPC_MAX_BRW_PAGES);
470 off_in_block = off & (bs - 1);
471 sz_in_block = min_t(int, bs - off_in_block, len);
474 if (sz_in_block == bs) {
475 /* full block, try to use zerocopy */
476 abuf = osd_request_arcbuf(dn, bs);
477 if (unlikely(IS_ERR(abuf)))
478 GOTO(out_err, rc = PTR_ERR(abuf));
482 atomic_inc(&osd->od_zerocopy_loan);
484 /* go over pages arcbuf contains, put them as
485 * local niobufs for ptlrpc's bulks */
486 while (sz_in_block > 0) {
487 plen = min_t(int, sz_in_block, PAGE_SIZE);
489 lnb[i].lnb_file_offset = off;
490 lnb[i].lnb_page_offset = 0;
491 lnb[i].lnb_len = plen;
493 if (sz_in_block == bs)
494 lnb[i].lnb_data = abuf;
496 lnb[i].lnb_data = NULL;
498 /* this one is not supposed to fail */
499 lnb[i].lnb_page = kmem_to_page(abuf->b_data +
501 LASSERT(lnb[i].lnb_page);
503 lprocfs_counter_add(osd->od_stats,
504 LPROC_OSD_ZEROCOPY_IO, 1);
509 off_in_block += plen;
514 if (off_in_block == 0 && len < bs &&
515 off + len >= obj->oo_attr.la_size)
516 lprocfs_counter_add(osd->od_stats,
517 LPROC_OSD_TAIL_IO, 1);
519 /* can't use zerocopy, allocate temp. buffers */
520 poff = off & (PAGE_SIZE - 1);
521 while (sz_in_block > 0) {
522 plen = min_t(int, poff + sz_in_block,
526 lnb[i].lnb_file_offset = off;
527 lnb[i].lnb_page_offset = poff;
530 lnb[i].lnb_len = plen;
532 lnb[i].lnb_data = NULL;
534 lnb[i].lnb_page = alloc_page(OSD_GFP_IO);
535 if (unlikely(lnb[i].lnb_page == NULL))
536 GOTO(out_err, rc = -ENOMEM);
538 LASSERT(lnb[i].lnb_page->mapping == NULL);
539 lnb[i].lnb_page->mapping = (void *)obj;
541 atomic_inc(&osd->od_zerocopy_alloc);
542 lprocfs_counter_add(osd->od_stats,
543 LPROC_OSD_COPY_IO, 1);
557 osd_bufs_put(env, &obj->oo_dt, lnb, npages);
561 static int osd_bufs_get(const struct lu_env *env, struct dt_object *dt,
562 loff_t offset, ssize_t len, struct niobuf_local *lnb,
563 enum dt_bufs_type rw)
565 struct osd_object *obj = osd_dt_obj(dt);
568 LASSERT(dt_object_exists(dt));
571 if (rw & DT_BUFS_TYPE_WRITE)
572 rc = osd_bufs_get_write(env, obj, offset, len, lnb);
574 rc = osd_bufs_get_read(env, obj, offset, len, lnb);
579 static int osd_write_prep(const struct lu_env *env, struct dt_object *dt,
580 struct niobuf_local *lnb, int npages)
582 struct osd_object *obj = osd_dt_obj(dt);
584 LASSERT(dt_object_exists(dt));
590 static inline uint64_t osd_roundup2blocksz(uint64_t size,
596 size += offset % blksz;
598 if (likely(is_power_of_2(blksz)))
599 return PO2_ROUNDUP_TYPED(size, blksz, uint64_t);
606 static int osd_declare_write_commit(const struct lu_env *env,
607 struct dt_object *dt,
608 struct niobuf_local *lnb, int npages,
611 struct osd_object *obj = osd_dt_obj(dt);
612 struct osd_device *osd = osd_obj2dev(obj);
613 struct osd_thandle *oh;
616 uint32_t blksz = obj->oo_dn->dn_datablksz;
617 int i, rc, flags = 0;
620 struct page *last_page = NULL;
621 unsigned long discont_pages = 0;
622 enum osd_qid_declare_flags declare_flags = OSD_QID_BLK;
625 LASSERT(dt_object_exists(dt));
631 oh = container_of0(th, struct osd_thandle, ot_super);
633 for (i = 0; i < npages; i++) {
634 if (last_page && lnb[i].lnb_page->index != (last_page->index + 1))
636 last_page = lnb[i].lnb_page;
638 /* ENOSPC, network RPC error, etc.
639 * We don't want to book space for pages which will be
640 * skipped in osd_write_commit(). Hence we skip pages
641 * with lnb_rc != 0 here too */
643 /* ignore quota for the whole request if any page is from
644 * client cache or written by root.
646 * XXX once we drop the 1.8 client support, the checking
647 * for whether page is from cache can be simplified as:
648 * !(lnb[i].flags & OBD_BRW_SYNC)
650 * XXX we could handle this on per-lnb basis as done by
652 if ((lnb[i].lnb_flags & OBD_BRW_NOQUOTA) ||
653 (lnb[i].lnb_flags & (OBD_BRW_FROM_GRANT | OBD_BRW_SYNC)) ==
655 declare_flags |= OSD_QID_FORCE;
658 /* first valid lnb */
659 offset = lnb[i].lnb_file_offset;
660 size = lnb[i].lnb_len;
663 if (offset + size == lnb[i].lnb_file_offset) {
664 /* this lnb is contiguous to the previous one */
665 size += lnb[i].lnb_len;
669 osd_tx_hold_write(oh->ot_tx, obj->oo_dn->dn_object,
670 obj->oo_dn, offset, size);
671 /* Estimating space to be consumed by a write is rather
672 * complicated with ZFS. As a consequence, we don't account for
673 * indirect blocks and just use as a rough estimate the worse
674 * case where the old space is being held by a snapshot. Quota
675 * overrun will be adjusted once the operation is committed, if
677 space += osd_roundup2blocksz(size, offset, blksz);
679 offset = lnb[i].lnb_file_offset;
680 size = lnb[i].lnb_len;
684 osd_tx_hold_write(oh->ot_tx, obj->oo_dn->dn_object, obj->oo_dn,
686 space += osd_roundup2blocksz(size, offset, blksz);
689 /* backend zfs filesystem might be configured to store multiple data
691 space *= osd->od_os->os_copies;
693 CDEBUG(D_QUOTA, "writing %d pages, reserving %lldK of quota space\n",
696 record_start_io(osd, WRITE, discont_pages);
698 /* acquire quota space if needed */
699 rc = osd_declare_quota(env, osd, obj->oo_attr.la_uid,
700 obj->oo_attr.la_gid, obj->oo_attr.la_projid,
701 space, oh, &flags, declare_flags);
703 if (!synced && rc == -EDQUOT && (flags & QUOTA_FL_SYNC) != 0) {
704 dt_sync(env, th->th_dev);
706 CDEBUG(D_QUOTA, "retry after sync\n");
711 /* we need only to store the overquota flags in the first lnb for
712 * now, once we support multiple objects BRW, this code needs be
714 if (flags & QUOTA_FL_OVER_USRQUOTA)
715 lnb[0].lnb_flags |= OBD_BRW_OVER_USRQUOTA;
716 if (flags & QUOTA_FL_OVER_GRPQUOTA)
717 lnb[0].lnb_flags |= OBD_BRW_OVER_GRPQUOTA;
718 #ifdef ZFS_PROJINHERIT
719 if (flags & QUOTA_FL_OVER_PRJQUOTA)
720 lnb[0].lnb_flags |= OBD_BRW_OVER_PRJQUOTA;
727 * Policy to grow ZFS block size by write pattern.
728 * For sequential write, it grows block size gradually until it reaches the
729 * maximum blocksize the dataset can support. Otherwise, it will pick a
730 * a block size by the writing region of this I/O.
732 static int osd_grow_blocksize(struct osd_object *obj, struct osd_thandle *oh,
733 uint64_t start, uint64_t end)
735 struct osd_device *osd = osd_obj2dev(obj);
736 dnode_t *dn = obj->oo_dn;
742 if (dn->dn_maxblkid > 0) /* can't change block size */
745 if (dn->dn_datablksz >= osd->od_max_blksz)
748 down_write(&obj->oo_guard);
750 blksz = dn->dn_datablksz;
751 if (blksz >= osd->od_max_blksz) /* check again after grabbing lock */
752 GOTO(out_unlock, rc);
754 /* now ZFS can support up to 16MB block size, and if the write
755 * is sequential, it just increases the block size gradually */
756 if (start <= blksz) { /* sequential */
757 blksz = (uint32_t)min_t(uint64_t, osd->od_max_blksz, end);
758 } else { /* sparse, pick a block size by write region */
759 blksz = (uint32_t)min_t(uint64_t, osd->od_max_blksz,
763 if (!is_power_of_2(blksz))
764 blksz = size_roundup_power2(blksz);
766 if (blksz > dn->dn_datablksz) {
767 rc = -dmu_object_set_blocksize(osd->od_os, dn->dn_object,
768 blksz, 0, oh->ot_tx);
769 LASSERT(ergo(rc == 0, dn->dn_datablksz >= blksz));
771 CDEBUG(D_INODE, "object "DFID": change block size"
772 "%u -> %u error rc = %d\n",
773 PFID(lu_object_fid(&obj->oo_dt.do_lu)),
774 dn->dn_datablksz, blksz, rc);
778 up_write(&obj->oo_guard);
783 static void osd_evict_dbufs_after_write(struct osd_object *obj,
784 loff_t off, ssize_t len)
789 rc = -dmu_buf_hold_array_by_bonus(&obj->oo_dn->dn_bonus->db, off, len,
790 TRUE, osd_0copy_tag, &numbufs, &dbp);
794 for (i = 0; i < numbufs; i++)
795 dbuf_set_pending_evict(dbp[i]);
797 dmu_buf_rele_array(dbp, numbufs, osd_0copy_tag);
800 static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
801 struct niobuf_local *lnb, int npages,
804 struct osd_object *obj = osd_dt_obj(dt);
805 struct osd_device *osd = osd_obj2dev(obj);
806 struct osd_thandle *oh;
807 uint64_t new_size = 0;
808 int i, abufsz, rc = 0, drop_cache = 0;
809 unsigned long iosize = 0;
812 LASSERT(dt_object_exists(dt));
816 oh = container_of0(th, struct osd_thandle, ot_super);
818 /* adjust block size. Assume the buffers are sorted. */
819 (void)osd_grow_blocksize(obj, oh, lnb[0].lnb_file_offset,
820 lnb[npages - 1].lnb_file_offset +
821 lnb[npages - 1].lnb_len);
823 if (obj->oo_attr.la_size >= osd->od_readcache_max_filesize ||
824 lnb[npages - 1].lnb_file_offset + lnb[npages - 1].lnb_len >=
825 osd->od_readcache_max_filesize)
828 if (OBD_FAIL_CHECK(OBD_FAIL_OST_MAPBLK_ENOSPC))
831 /* LU-8791: take oo_guard to avoid the deadlock that changing block
832 * size and assigning arcbuf take place at the same time.
836 * -> osd_grow_blocksize() with osd_object::oo_guard held
837 * -> dmu_object_set_blocksize()
838 * -> dnode_set_blksz(), with dnode_t::dn_struct_rwlock
841 * -> dmu_buf_will_dirty()
843 * -> wait for the dbuf state to change
846 * -> dmu_assign_arcbuf()
847 * -> dbuf_assign_arcbuf(), set dbuf state to DB_FILL
849 * -> try to hold the read lock of dnode_t::dn_struct_rwlock
851 * By taking the read lock, it can avoid thread 2 to enter into the
852 * critical section of assigning the arcbuf, while thread 1 is
853 * changing the block size.
855 down_read(&obj->oo_guard);
856 for (i = 0; i < npages; i++) {
857 CDEBUG(D_INODE, "write %u bytes at %u\n",
858 (unsigned) lnb[i].lnb_len,
859 (unsigned) lnb[i].lnb_file_offset);
862 /* ENOSPC, network RPC error, etc.
863 * Unlike ldiskfs, zfs allocates new blocks on rewrite,
864 * so we skip this page if lnb_rc is set to -ENOSPC */
865 CDEBUG(D_INODE, "obj "DFID": skipping lnb[%u]: rc=%d\n",
866 PFID(lu_object_fid(&dt->do_lu)), i,
871 if (new_size < lnb[i].lnb_file_offset + lnb[i].lnb_len)
872 new_size = lnb[i].lnb_file_offset + lnb[i].lnb_len;
873 if (lnb[i].lnb_page == NULL)
876 if (lnb[i].lnb_page->mapping == (void *)obj) {
877 osd_dmu_write(osd, obj->oo_dn, lnb[i].lnb_file_offset,
878 lnb[i].lnb_len, kmap(lnb[i].lnb_page) +
879 lnb[i].lnb_page_offset, oh->ot_tx);
880 kunmap(lnb[i].lnb_page);
881 iosize += lnb[i].lnb_len;
882 abufsz = lnb[i].lnb_len; /* to drop cache below */
883 } else if (lnb[i].lnb_data) {
885 LASSERT(((unsigned long)lnb[i].lnb_data & 1) == 0);
886 /* buffer loaned for zerocopy, try to use it.
887 * notice that dmu_assign_arcbuf() is smart
888 * enough to recognize changed blocksize
889 * in this case it fallbacks to dmu_write() */
890 abufsz = arc_buf_size(lnb[i].lnb_data);
891 LASSERT(abufsz & PAGE_MASK);
892 apages = abufsz / PAGE_SIZE;
893 LASSERT(i + apages <= npages);
894 /* these references to pages must be invalidated
895 * to prevent access in osd_bufs_put() */
896 for (j = 0; j < apages; j++)
897 lnb[i + j].lnb_page = NULL;
898 dmu_assign_arcbuf(&obj->oo_dn->dn_bonus->db,
899 lnb[i].lnb_file_offset,
900 lnb[i].lnb_data, oh->ot_tx);
901 /* drop the reference, otherwise osd_put_bufs()
902 * will be releasing it - bad! */
903 lnb[i].lnb_data = NULL;
904 atomic_dec(&osd->od_zerocopy_loan);
907 /* we don't want to deal with cache if nothing
908 * has been send to ZFS at this step */
915 /* we have to mark dbufs for eviction here because
916 * dmu_assign_arcbuf() may create a new dbuf for
918 osd_evict_dbufs_after_write(obj, lnb[i].lnb_file_offset,
921 up_read(&obj->oo_guard);
923 if (unlikely(new_size == 0)) {
924 /* no pages to write, no transno is needed */
926 /* it is important to return 0 even when all lnb_rc == -ENOSPC
927 * since ofd_commitrw_write() retries several times on ENOSPC */
928 record_end_io(osd, WRITE, 0, 0, 0);
932 write_lock(&obj->oo_attr_lock);
933 if (obj->oo_attr.la_size < new_size) {
934 obj->oo_attr.la_size = new_size;
935 write_unlock(&obj->oo_attr_lock);
936 /* osd_object_sa_update() will be copying directly from
937 * oo_attr into dbuf. any update within a single txg will copy
939 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
940 &obj->oo_attr.la_size, 8, oh);
942 write_unlock(&obj->oo_attr_lock);
945 record_end_io(osd, WRITE, 0, iosize, npages);
950 static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
951 struct niobuf_local *lnb, int npages)
953 struct osd_object *obj = osd_dt_obj(dt);
957 LASSERT(dt_object_exists(dt));
960 read_lock(&obj->oo_attr_lock);
961 eof = obj->oo_attr.la_size;
962 read_unlock(&obj->oo_attr_lock);
964 for (i = 0; i < npages; i++) {
965 if (unlikely(lnb[i].lnb_rc < 0))
968 lnb[i].lnb_rc = lnb[i].lnb_len;
970 if (lnb[i].lnb_file_offset + lnb[i].lnb_len >= eof) {
971 if (eof <= lnb[i].lnb_file_offset)
974 lnb[i].lnb_rc = eof - lnb[i].lnb_file_offset;
976 /* all subsequent rc should be 0 */
987 * Punch/truncate an object
989 * IN: db - dmu_buf of the object to free data in.
990 * off - start of section to free.
991 * len - length of section to free (DMU_OBJECT_END => to EOF).
993 * RETURN: 0 if success
994 * error code if failure
996 * The transaction passed to this routine must have
997 * dmu_tx_hold_sa() and if off < size, dmu_tx_hold_free()
998 * called and then assigned to a transaction group.
1000 static int __osd_object_punch(objset_t *os, dnode_t *dn, dmu_tx_t *tx,
1001 uint64_t size, uint64_t off, uint64_t len)
1005 /* Assert that the transaction has been assigned to a
1006 transaction group. */
1007 LASSERT(tx->tx_txg != 0);
1009 * Nothing to do if file already at desired length.
1011 if (len == DMU_OBJECT_END && size == off)
1014 /* XXX: dnode_free_range() can be used to save on dnode lookup */
1016 dmu_free_range(os, dn->dn_object, off, len, tx);
1021 static int osd_punch(const struct lu_env *env, struct dt_object *dt,
1022 __u64 start, __u64 end, struct thandle *th)
1024 struct osd_object *obj = osd_dt_obj(dt);
1025 struct osd_device *osd = osd_obj2dev(obj);
1026 struct osd_thandle *oh;
1031 LASSERT(dt_object_exists(dt));
1032 LASSERT(osd_invariant(obj));
1034 LASSERT(th != NULL);
1035 oh = container_of0(th, struct osd_thandle, ot_super);
1037 write_lock(&obj->oo_attr_lock);
1039 if (end == OBD_OBJECT_EOF || end >= obj->oo_attr.la_size)
1040 len = DMU_OBJECT_END;
1043 write_unlock(&obj->oo_attr_lock);
1045 rc = __osd_object_punch(osd->od_os, obj->oo_dn, oh->ot_tx,
1046 obj->oo_attr.la_size, start, len);
1048 if (len == DMU_OBJECT_END) {
1049 write_lock(&obj->oo_attr_lock);
1050 obj->oo_attr.la_size = start;
1051 write_unlock(&obj->oo_attr_lock);
1052 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
1053 &obj->oo_attr.la_size, 8, oh);
1058 static int osd_declare_punch(const struct lu_env *env, struct dt_object *dt,
1059 __u64 start, __u64 end, struct thandle *handle)
1061 struct osd_object *obj = osd_dt_obj(dt);
1062 struct osd_device *osd = osd_obj2dev(obj);
1063 struct osd_thandle *oh;
1067 oh = container_of0(handle, struct osd_thandle, ot_super);
1069 read_lock(&obj->oo_attr_lock);
1070 if (end == OBD_OBJECT_EOF || end >= obj->oo_attr.la_size)
1071 len = DMU_OBJECT_END;
1075 /* declare we'll free some blocks ... */
1076 if (start < obj->oo_attr.la_size) {
1077 read_unlock(&obj->oo_attr_lock);
1078 dmu_tx_mark_netfree(oh->ot_tx);
1079 dmu_tx_hold_free(oh->ot_tx, obj->oo_dn->dn_object, start, len);
1081 read_unlock(&obj->oo_attr_lock);
1084 RETURN(osd_declare_quota(env, osd, obj->oo_attr.la_uid,
1085 obj->oo_attr.la_gid, obj->oo_attr.la_projid,
1086 0, oh, NULL, OSD_QID_BLK));
1089 static int osd_ladvise(const struct lu_env *env, struct dt_object *dt,
1090 __u64 start, __u64 end, enum lu_ladvise_type advice)
1104 struct dt_body_operations osd_body_ops = {
1105 .dbo_read = osd_read,
1106 .dbo_declare_write = osd_declare_write,
1107 .dbo_write = osd_write,
1108 .dbo_bufs_get = osd_bufs_get,
1109 .dbo_bufs_put = osd_bufs_put,
1110 .dbo_write_prep = osd_write_prep,
1111 .dbo_declare_write_commit = osd_declare_write_commit,
1112 .dbo_write_commit = osd_write_commit,
1113 .dbo_read_prep = osd_read_prep,
1114 .dbo_declare_punch = osd_declare_punch,
1115 .dbo_punch = osd_punch,
1116 .dbo_ladvise = osd_ladvise,
1119 struct dt_body_operations osd_body_scrub_ops = {
1120 .dbo_read = osd_read_no_record,
1121 .dbo_declare_write = osd_declare_write,
1122 .dbo_write = osd_write,