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";
66 static void dbuf_set_pending_evict(dmu_buf_t *db)
68 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
69 dbi->db_pending_evict = TRUE;
72 static void record_start_io(struct osd_device *osd, int rw, int discont_pages)
74 struct obd_histogram *h = osd->od_brw_stats.hist;
77 atomic_inc(&osd->od_r_in_flight);
78 lprocfs_oh_tally(&h[BRW_R_RPC_HIST],
79 atomic_read(&osd->od_r_in_flight));
80 lprocfs_oh_tally(&h[BRW_R_DISCONT_PAGES], discont_pages);
83 atomic_inc(&osd->od_w_in_flight);
84 lprocfs_oh_tally(&h[BRW_W_RPC_HIST],
85 atomic_read(&osd->od_w_in_flight));
86 lprocfs_oh_tally(&h[BRW_W_DISCONT_PAGES], discont_pages);
91 static void record_end_io(struct osd_device *osd, int rw,
92 unsigned long elapsed, int disksize, int npages)
94 struct obd_histogram *h = osd->od_brw_stats.hist;
97 atomic_dec(&osd->od_r_in_flight);
99 atomic_dec(&osd->od_w_in_flight);
101 lprocfs_oh_tally_log2(&h[BRW_R_PAGES + rw], npages);
103 lprocfs_oh_tally_log2(&h[BRW_R_DISK_IOSIZE + rw], disksize);
105 lprocfs_oh_tally_log2(&h[BRW_R_IO_TIME + rw], elapsed);
108 static ssize_t __osd_read(const struct lu_env *env, struct dt_object *dt,
109 struct lu_buf *buf, loff_t *pos, size_t *size)
111 struct osd_object *obj = osd_dt_obj(dt);
115 LASSERT(dt_object_exists(dt));
118 read_lock(&obj->oo_attr_lock);
119 old_size = obj->oo_attr.la_size;
120 read_unlock(&obj->oo_attr_lock);
122 if (*pos + *size > old_size) {
126 *size = old_size - *pos;
129 rc = osd_dmu_read(osd_obj2dev(obj), obj->oo_dn, *pos, *size,
130 buf->lb_buf, DMU_READ_PREFETCH);
139 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
140 struct lu_buf *buf, loff_t *pos)
142 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
143 size_t size = buf->lb_len;
149 record_start_io(osd, READ, 0);
150 rc = __osd_read(env, dt, buf, pos, &size);
151 delta_ms = ktime_ms_delta(ktime_get(), start);
152 record_end_io(osd, READ, delta_ms, size, size >> PAGE_SHIFT);
157 static inline ssize_t osd_read_no_record(const struct lu_env *env,
158 struct dt_object *dt,
159 struct lu_buf *buf, loff_t *pos)
161 size_t size = buf->lb_len;
163 return __osd_read(env, dt, buf, pos, &size);
166 static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
167 const struct lu_buf *buf, loff_t pos,
170 struct osd_object *obj = osd_dt_obj(dt);
171 struct osd_device *osd = osd_obj2dev(obj);
172 struct osd_thandle *oh;
176 oh = container_of(th, struct osd_thandle, ot_super);
178 /* in some cases declare can race with creation (e.g. llog)
179 * and we need to wait till object is initialized. notice
180 * LOHA_EXISTs is supposed to be the last step in the
183 /* size change (in dnode) will be declared by dmu_tx_hold_write() */
184 if (dt_object_exists(dt))
185 oid = obj->oo_dn->dn_object;
187 oid = DMU_NEW_OBJECT;
189 /* XXX: we still miss for append declaration support in ZFS
190 * -1 means append which is used by llog mostly, llog
191 * can grow upto LLOG_MIN_CHUNK_SIZE*8 records */
193 pos = max_t(loff_t, 256 * 8 * LLOG_MIN_CHUNK_SIZE,
194 obj->oo_attr.la_size + (2 << 20));
195 osd_tx_hold_write(oh->ot_tx, oid, obj->oo_dn, pos, buf->lb_len);
197 /* dt_declare_write() is usually called for system objects, such
198 * as llog or last_rcvd files. We needn't enforce quota on those
199 * objects, so always set the lqi_space as 0. */
200 RETURN(osd_declare_quota(env, osd, obj->oo_attr.la_uid,
201 obj->oo_attr.la_gid, obj->oo_attr.la_projid,
202 0, oh, NULL, OSD_QID_BLK));
205 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
206 const struct lu_buf *buf, loff_t *pos,
209 struct osd_object *obj = osd_dt_obj(dt);
210 struct osd_device *osd = osd_obj2dev(obj);
211 struct osd_thandle *oh;
212 uint64_t offset = *pos;
217 LASSERT(dt_object_exists(dt));
221 oh = container_of(th, struct osd_thandle, ot_super);
223 osd_dmu_write(osd, obj->oo_dn, offset, (uint64_t)buf->lb_len,
224 buf->lb_buf, oh->ot_tx);
225 write_lock(&obj->oo_attr_lock);
226 if (obj->oo_attr.la_size < offset + buf->lb_len) {
227 obj->oo_attr.la_size = offset + buf->lb_len;
228 write_unlock(&obj->oo_attr_lock);
229 /* osd_object_sa_update() will be copying directly from oo_attr
230 * into dbuf. any update within a single txg will copy the
232 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
233 &obj->oo_attr.la_size, 8, oh);
237 write_unlock(&obj->oo_attr_lock);
248 * XXX: for the moment I don't want to use lnb_flags for osd-internal
249 * purposes as it's not very well defined ...
250 * instead I use the lowest bit of the address so that:
251 * arc buffer: .lnb_data = abuf (arc we loan for write)
252 * dbuf buffer: .lnb_data = dbuf | 1 (dbuf we get for read)
253 * copy buffer: .lnb_page->mapping = obj (page we allocate for write)
257 static int osd_bufs_put(const struct lu_env *env, struct dt_object *dt,
258 struct niobuf_local *lnb, int npages)
260 struct osd_object *obj = osd_dt_obj(dt);
261 struct osd_device *osd = osd_obj2dev(obj);
265 LASSERT(dt_object_exists(dt));
268 for (i = 0; i < npages; i++) {
269 if (lnb[i].lnb_page == NULL)
271 if (lnb[i].lnb_page->mapping == (void *)obj) {
272 /* this is anonymous page allocated for copy-write */
273 lnb[i].lnb_page->mapping = NULL;
274 __free_page(lnb[i].lnb_page);
275 atomic_dec(&osd->od_zerocopy_alloc);
277 /* see comment in osd_bufs_get_read() */
278 ptr = (unsigned long)lnb[i].lnb_data;
281 dmu_buf_rele((void *)ptr, osd_0copy_tag);
282 atomic_dec(&osd->od_zerocopy_pin);
283 } else if (lnb[i].lnb_data != NULL) {
284 int j, apages, abufsz;
285 abufsz = arc_buf_size(lnb[i].lnb_data);
286 apages = abufsz >> PAGE_SHIFT;
287 /* these references to pages must be invalidated
288 * to prevent access in osd_bufs_put() */
289 for (j = 0; j < apages; j++)
290 lnb[i + j].lnb_page = NULL;
291 dmu_return_arcbuf(lnb[i].lnb_data);
292 atomic_dec(&osd->od_zerocopy_loan);
295 lnb[i].lnb_page = NULL;
296 lnb[i].lnb_data = NULL;
302 static inline struct page *kmem_to_page(void *addr)
304 LASSERT(!((unsigned long)addr & ~PAGE_MASK));
305 if (is_vmalloc_addr(addr))
306 return vmalloc_to_page(addr);
308 return virt_to_page(addr);
312 * Prepare buffers for read.
314 * The function maps the range described by \a off and \a len to \a lnb array.
315 * dmu_buf_hold_array_by_bonus() finds/creates appropriate ARC buffers, then
316 * we fill \a lnb array with the pages storing ARC buffers. Notice the current
317 * implementationt passes TRUE to dmu_buf_hold_array_by_bonus() to fill ARC
318 * buffers with actual data, I/O is done in the conext of osd_bufs_get_read().
319 * A better implementation would just return the buffers (potentially unfilled)
320 * and subsequent osd_read_prep() would do I/O for many ranges concurrently.
322 * \param[in] env environment
323 * \param[in] obj object
324 * \param[in] off offset in bytes
325 * \param[in] len the number of bytes to access
326 * \param[out] lnb array of local niobufs pointing to the buffers with data
328 * \retval 0 for success
329 * \retval negative error number of failure
331 static int osd_bufs_get_read(const struct lu_env *env, struct osd_object *obj,
332 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;
362 if (unlikely(npages >= maxlnb))
363 GOTO(err, rc = -EOVERFLOW);
365 rc = -dmu_buf_hold_array_by_bonus(&obj->oo_dn->dn_bonus->db,
366 off, len, TRUE, osd_0copy_tag,
371 for (i = 0; i < numbufs; i++) {
372 int bufoff, tocpy, thispage;
377 atomic_inc(&osd->od_zerocopy_pin);
379 bufoff = off - dbp[i]->db_offset;
380 tocpy = min_t(int, dbp[i]->db_size - bufoff, len);
382 /* kind of trick to differentiate dbuf vs. arcbuf */
383 LASSERT(((unsigned long)dbp[i] & 1) == 0);
384 dbf = (void *) ((unsigned long)dbp[i] | 1);
387 if (unlikely(npages >= maxlnb))
388 GOTO(err, rc = -EOVERFLOW);
390 thispage = PAGE_SIZE;
391 thispage -= bufoff & (PAGE_SIZE - 1);
392 thispage = min(tocpy, thispage);
395 lnb->lnb_file_offset = off;
396 lnb->lnb_page_offset = bufoff & ~PAGE_MASK;
397 lnb->lnb_len = thispage;
398 lnb->lnb_page = kmem_to_page(dbp[i]->db_data +
400 /* mark just a single slot: we need this
401 * reference to dbuf to be released once */
415 dbuf_set_pending_evict(dbp[i]);
417 /* steal dbuf so dmu_buf_rele_array() can't release
422 dmu_buf_rele_array(dbp, numbufs, osd_0copy_tag);
425 delta_ms = ktime_ms_delta(ktime_get(), start);
426 record_end_io(osd, READ, delta_ms, npages * PAGE_SIZE, npages);
433 dmu_buf_rele_array(dbp, numbufs, osd_0copy_tag);
434 osd_bufs_put(env, &obj->oo_dt, lnb - npages, npages);
438 static inline arc_buf_t *osd_request_arcbuf(dnode_t *dn, size_t bs)
442 abuf = dmu_request_arcbuf(&dn->dn_bonus->db, bs);
444 return ERR_PTR(-ENOMEM);
446 #if ZFS_VERSION_CODE < OBD_OCD_VERSION(0, 7, 0, 0)
448 * ZFS prior to 0.7.0 doesn't guarantee PAGE_SIZE alignment for zio
449 * blocks smaller than (PAGE_SIZE << 2). This poses a problem of
450 * setting up page array for RDMA transfer. See LU-9305.
452 if ((unsigned long)abuf->b_data & ~PAGE_MASK) {
453 dmu_return_arcbuf(abuf);
461 static int osd_bufs_get_write(const struct lu_env *env, struct osd_object *obj,
462 loff_t off, ssize_t len, struct niobuf_local *lnb,
465 struct osd_device *osd = osd_obj2dev(obj);
466 int poff, plen, off_in_block, sz_in_block;
467 int rc, i = 0, npages = 0;
468 dnode_t *dn = obj->oo_dn;
470 uint32_t bs = dn->dn_datablksz;
474 * currently only full blocks are subject to zerocopy approach:
475 * so that we're sure nobody is trying to update the same block
478 if (unlikely(npages >= maxlnb))
479 GOTO(out_err, rc = -EOVERFLOW);
481 off_in_block = off & (bs - 1);
482 sz_in_block = min_t(int, bs - off_in_block, len);
485 if (sz_in_block == bs) {
486 /* full block, try to use zerocopy */
487 abuf = osd_request_arcbuf(dn, bs);
488 if (unlikely(IS_ERR(abuf)))
489 GOTO(out_err, rc = PTR_ERR(abuf));
493 atomic_inc(&osd->od_zerocopy_loan);
495 /* go over pages arcbuf contains, put them as
496 * local niobufs for ptlrpc's bulks */
497 while (sz_in_block > 0) {
498 plen = min_t(int, sz_in_block, PAGE_SIZE);
500 if (unlikely(npages >= maxlnb))
501 GOTO(out_err, rc = -EOVERFLOW);
503 lnb[i].lnb_file_offset = off;
504 lnb[i].lnb_page_offset = 0;
505 lnb[i].lnb_len = plen;
507 if (sz_in_block == bs)
508 lnb[i].lnb_data = abuf;
510 lnb[i].lnb_data = NULL;
512 /* this one is not supposed to fail */
513 lnb[i].lnb_page = kmem_to_page(abuf->b_data +
515 LASSERT(lnb[i].lnb_page);
517 lprocfs_counter_add(osd->od_stats,
518 LPROC_OSD_ZEROCOPY_IO, 1);
523 off_in_block += plen;
528 if (off_in_block == 0 && len < bs &&
529 off + len >= obj->oo_attr.la_size)
530 lprocfs_counter_add(osd->od_stats,
531 LPROC_OSD_TAIL_IO, 1);
533 /* can't use zerocopy, allocate temp. buffers */
534 poff = off & (PAGE_SIZE - 1);
535 while (sz_in_block > 0) {
536 plen = min_t(int, poff + sz_in_block,
540 if (unlikely(npages >= maxlnb))
541 GOTO(out_err, rc = -EOVERFLOW);
543 lnb[i].lnb_file_offset = off;
544 lnb[i].lnb_page_offset = poff;
547 lnb[i].lnb_len = plen;
549 lnb[i].lnb_data = NULL;
551 lnb[i].lnb_page = alloc_page(OSD_GFP_IO);
552 if (unlikely(lnb[i].lnb_page == NULL))
553 GOTO(out_err, rc = -ENOMEM);
555 LASSERT(lnb[i].lnb_page->mapping == NULL);
556 lnb[i].lnb_page->mapping = (void *)obj;
558 atomic_inc(&osd->od_zerocopy_alloc);
559 lprocfs_counter_add(osd->od_stats,
560 LPROC_OSD_COPY_IO, 1);
574 osd_bufs_put(env, &obj->oo_dt, lnb, npages);
578 static int osd_bufs_get(const struct lu_env *env, struct dt_object *dt,
579 loff_t offset, ssize_t len, struct niobuf_local *lnb,
580 int maxlnb, enum dt_bufs_type rw)
582 struct osd_object *obj = osd_dt_obj(dt);
585 LASSERT(dt_object_exists(dt));
588 if (rw & DT_BUFS_TYPE_WRITE)
589 rc = osd_bufs_get_write(env, obj, offset, len, lnb, maxlnb);
591 rc = osd_bufs_get_read(env, obj, offset, len, lnb, maxlnb);
596 static int osd_write_prep(const struct lu_env *env, struct dt_object *dt,
597 struct niobuf_local *lnb, int npages)
599 struct osd_object *obj = osd_dt_obj(dt);
601 LASSERT(dt_object_exists(dt));
607 static inline uint64_t osd_roundup2blocksz(uint64_t size,
613 size += offset % blksz;
615 if (likely(is_power_of_2(blksz)))
616 return PO2_ROUNDUP_TYPED(size, blksz, uint64_t);
623 static int osd_declare_write_commit(const struct lu_env *env,
624 struct dt_object *dt,
625 struct niobuf_local *lnb, int npages,
628 struct osd_object *obj = osd_dt_obj(dt);
629 struct osd_device *osd = osd_obj2dev(obj);
630 struct osd_thandle *oh;
633 uint32_t blksz = obj->oo_dn->dn_datablksz;
637 struct page *last_page = NULL;
638 unsigned long discont_pages = 0;
639 enum osd_quota_local_flags local_flags = 0;
640 enum osd_qid_declare_flags declare_flags = OSD_QID_BLK;
643 LASSERT(dt_object_exists(dt));
649 oh = container_of(th, struct osd_thandle, ot_super);
651 for (i = 0; i < npages; i++) {
652 if (last_page && lnb[i].lnb_page->index != (last_page->index + 1))
654 last_page = lnb[i].lnb_page;
656 /* ENOSPC, network RPC error, etc.
657 * We don't want to book space for pages which will be
658 * skipped in osd_write_commit(). Hence we skip pages
659 * with lnb_rc != 0 here too */
661 /* ignore quota for the whole request if any page is from
662 * client cache or written by root.
664 * XXX once we drop the 1.8 client support, the checking
665 * for whether page is from cache can be simplified as:
666 * !(lnb[i].flags & OBD_BRW_SYNC)
668 * XXX we could handle this on per-lnb basis as done by
670 if ((lnb[i].lnb_flags & OBD_BRW_NOQUOTA) ||
671 (lnb[i].lnb_flags & (OBD_BRW_FROM_GRANT | OBD_BRW_SYNC)) ==
673 declare_flags |= OSD_QID_FORCE;
676 /* first valid lnb */
677 offset = lnb[i].lnb_file_offset;
678 size = lnb[i].lnb_len;
681 if (offset + size == lnb[i].lnb_file_offset) {
682 /* this lnb is contiguous to the previous one */
683 size += lnb[i].lnb_len;
687 osd_tx_hold_write(oh->ot_tx, obj->oo_dn->dn_object,
688 obj->oo_dn, offset, size);
689 /* Estimating space to be consumed by a write is rather
690 * complicated with ZFS. As a consequence, we don't account for
691 * indirect blocks and just use as a rough estimate the worse
692 * case where the old space is being held by a snapshot. Quota
693 * overrun will be adjusted once the operation is committed, if
695 space += osd_roundup2blocksz(size, offset, blksz);
697 offset = lnb[i].lnb_file_offset;
698 size = lnb[i].lnb_len;
702 osd_tx_hold_write(oh->ot_tx, obj->oo_dn->dn_object, obj->oo_dn,
704 space += osd_roundup2blocksz(size, offset, blksz);
707 /* backend zfs filesystem might be configured to store multiple data
709 space *= osd->od_os->os_copies;
711 CDEBUG(D_QUOTA, "writing %d pages, reserving %lldK of quota space\n",
714 record_start_io(osd, WRITE, discont_pages);
716 /* acquire quota space if needed */
717 rc = osd_declare_quota(env, osd, obj->oo_attr.la_uid,
718 obj->oo_attr.la_gid, obj->oo_attr.la_projid,
719 space, oh, &local_flags, declare_flags);
721 if (!synced && rc == -EDQUOT &&
722 (local_flags & QUOTA_FL_SYNC) != 0) {
723 dt_sync(env, th->th_dev);
725 CDEBUG(D_QUOTA, "retry after sync\n");
730 /* we need only to store the overquota flags in the first lnb for
731 * now, once we support multiple objects BRW, this code needs be
733 if (local_flags & QUOTA_FL_OVER_USRQUOTA)
734 lnb[0].lnb_flags |= OBD_BRW_OVER_USRQUOTA;
735 if (local_flags & QUOTA_FL_OVER_GRPQUOTA)
736 lnb[0].lnb_flags |= OBD_BRW_OVER_GRPQUOTA;
737 #ifdef ZFS_PROJINHERIT
738 if (local_flags & QUOTA_FL_OVER_PRJQUOTA)
739 lnb[0].lnb_flags |= OBD_BRW_OVER_PRJQUOTA;
746 * Policy to grow ZFS block size by write pattern.
747 * For sequential write, it grows block size gradually until it reaches the
748 * maximum blocksize the dataset can support. Otherwise, it will pick a
749 * a block size by the writing region of this I/O.
751 static int osd_grow_blocksize(struct osd_object *obj, struct osd_thandle *oh,
752 uint64_t start, uint64_t end)
754 struct osd_device *osd = osd_obj2dev(obj);
755 dnode_t *dn = obj->oo_dn;
761 if (dn->dn_maxblkid > 0) /* can't change block size */
764 if (dn->dn_datablksz >= osd->od_max_blksz)
767 down_write(&obj->oo_guard);
769 blksz = dn->dn_datablksz;
770 if (blksz >= osd->od_max_blksz) /* check again after grabbing lock */
771 GOTO(out_unlock, rc);
773 /* now ZFS can support up to 16MB block size, and if the write
774 * is sequential, it just increases the block size gradually */
775 if (start <= blksz) { /* sequential */
776 blksz = (uint32_t)min_t(uint64_t, osd->od_max_blksz, end);
777 } else { /* sparse, pick a block size by write region */
778 blksz = (uint32_t)min_t(uint64_t, osd->od_max_blksz,
782 if (!is_power_of_2(blksz))
783 blksz = size_roundup_power2(blksz);
785 if (blksz > dn->dn_datablksz) {
786 rc = -dmu_object_set_blocksize(osd->od_os, dn->dn_object,
787 blksz, 0, oh->ot_tx);
788 LASSERT(ergo(rc == 0, dn->dn_datablksz >= blksz));
790 CDEBUG(D_INODE, "object "DFID": change block size"
791 "%u -> %u error rc = %d\n",
792 PFID(lu_object_fid(&obj->oo_dt.do_lu)),
793 dn->dn_datablksz, blksz, rc);
797 up_write(&obj->oo_guard);
802 static void osd_evict_dbufs_after_write(struct osd_object *obj,
803 loff_t off, ssize_t len)
808 rc = -dmu_buf_hold_array_by_bonus(&obj->oo_dn->dn_bonus->db, off, len,
809 TRUE, osd_0copy_tag, &numbufs, &dbp);
813 for (i = 0; i < numbufs; i++)
814 dbuf_set_pending_evict(dbp[i]);
816 dmu_buf_rele_array(dbp, numbufs, osd_0copy_tag);
819 static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
820 struct niobuf_local *lnb, int npages,
821 struct thandle *th, __u64 user_size)
823 struct osd_object *obj = osd_dt_obj(dt);
824 struct osd_device *osd = osd_obj2dev(obj);
825 struct osd_thandle *oh;
826 uint64_t new_size = 0;
827 int i, abufsz, rc = 0, drop_cache = 0;
828 unsigned long iosize = 0;
831 LASSERT(dt_object_exists(dt));
835 oh = container_of(th, struct osd_thandle, ot_super);
837 /* adjust block size. Assume the buffers are sorted. */
838 (void)osd_grow_blocksize(obj, oh, lnb[0].lnb_file_offset,
839 lnb[npages - 1].lnb_file_offset +
840 lnb[npages - 1].lnb_len);
842 if (obj->oo_attr.la_size >= osd->od_readcache_max_filesize ||
843 lnb[npages - 1].lnb_file_offset + lnb[npages - 1].lnb_len >=
844 osd->od_readcache_max_filesize)
847 if (OBD_FAIL_CHECK(OBD_FAIL_OST_MAPBLK_ENOSPC))
850 /* if la_size is already bigger than specified user_size,
853 if (obj->oo_attr.la_size > user_size)
856 /* LU-8791: take oo_guard to avoid the deadlock that changing block
857 * size and assigning arcbuf take place at the same time.
861 * -> osd_grow_blocksize() with osd_object::oo_guard held
862 * -> dmu_object_set_blocksize()
863 * -> dnode_set_blksz(), with dnode_t::dn_struct_rwlock
866 * -> dmu_buf_will_dirty()
868 * -> wait for the dbuf state to change
871 * -> dmu_assign_arcbuf()
872 * -> dbuf_assign_arcbuf(), set dbuf state to DB_FILL
874 * -> try to hold the read lock of dnode_t::dn_struct_rwlock
876 * By taking the read lock, it can avoid thread 2 to enter into the
877 * critical section of assigning the arcbuf, while thread 1 is
878 * changing the block size.
880 down_read(&obj->oo_guard);
881 for (i = 0; i < npages; i++) {
882 CDEBUG(D_INODE, "write %u bytes at %u\n",
883 (unsigned) lnb[i].lnb_len,
884 (unsigned) lnb[i].lnb_file_offset);
887 /* ENOSPC, network RPC error, etc.
888 * Unlike ldiskfs, zfs allocates new blocks on rewrite,
889 * so we skip this page if lnb_rc is set to -ENOSPC */
890 CDEBUG(D_INODE, "obj "DFID": skipping lnb[%u]: rc=%d\n",
891 PFID(lu_object_fid(&dt->do_lu)), i,
896 if (new_size < lnb[i].lnb_file_offset + lnb[i].lnb_len)
897 new_size = lnb[i].lnb_file_offset + lnb[i].lnb_len;
898 if (lnb[i].lnb_page == NULL)
901 if (lnb[i].lnb_page->mapping == (void *)obj) {
902 osd_dmu_write(osd, obj->oo_dn, lnb[i].lnb_file_offset,
903 lnb[i].lnb_len, kmap(lnb[i].lnb_page) +
904 lnb[i].lnb_page_offset, oh->ot_tx);
905 kunmap(lnb[i].lnb_page);
906 iosize += lnb[i].lnb_len;
907 abufsz = lnb[i].lnb_len; /* to drop cache below */
908 } else if (lnb[i].lnb_data) {
910 LASSERT(((unsigned long)lnb[i].lnb_data & 1) == 0);
911 /* buffer loaned for zerocopy, try to use it.
912 * notice that dmu_assign_arcbuf() is smart
913 * enough to recognize changed blocksize
914 * in this case it fallbacks to dmu_write() */
915 abufsz = arc_buf_size(lnb[i].lnb_data);
916 LASSERT(abufsz & PAGE_MASK);
917 apages = abufsz >> PAGE_SHIFT;
918 LASSERT(i + apages <= npages);
919 /* these references to pages must be invalidated
920 * to prevent access in osd_bufs_put() */
921 for (j = 0; j < apages; j++)
922 lnb[i + j].lnb_page = NULL;
923 dmu_assign_arcbuf(&obj->oo_dn->dn_bonus->db,
924 lnb[i].lnb_file_offset,
925 lnb[i].lnb_data, oh->ot_tx);
926 /* drop the reference, otherwise osd_put_bufs()
927 * will be releasing it - bad! */
928 lnb[i].lnb_data = NULL;
929 atomic_dec(&osd->od_zerocopy_loan);
932 /* we don't want to deal with cache if nothing
933 * has been send to ZFS at this step */
940 /* we have to mark dbufs for eviction here because
941 * dmu_assign_arcbuf() may create a new dbuf for
943 osd_evict_dbufs_after_write(obj, lnb[i].lnb_file_offset,
946 up_read(&obj->oo_guard);
948 if (unlikely(new_size == 0)) {
949 /* no pages to write, no transno is needed */
951 /* it is important to return 0 even when all lnb_rc == -ENOSPC
952 * since ofd_commitrw_write() retries several times on ENOSPC */
953 record_end_io(osd, WRITE, 0, 0, 0);
957 /* if file has grown, take user_size into account */
958 if (user_size && new_size > user_size)
959 new_size = user_size;
960 write_lock(&obj->oo_attr_lock);
961 if (obj->oo_attr.la_size < new_size) {
962 obj->oo_attr.la_size = new_size;
963 write_unlock(&obj->oo_attr_lock);
964 /* osd_object_sa_update() will be copying directly from
965 * oo_attr into dbuf. any update within a single txg will copy
967 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
968 &obj->oo_attr.la_size, 8, oh);
970 write_unlock(&obj->oo_attr_lock);
973 record_end_io(osd, WRITE, 0, iosize, npages);
978 static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
979 struct niobuf_local *lnb, int npages)
981 struct osd_object *obj = osd_dt_obj(dt);
985 LASSERT(dt_object_exists(dt));
988 read_lock(&obj->oo_attr_lock);
989 eof = obj->oo_attr.la_size;
990 read_unlock(&obj->oo_attr_lock);
992 for (i = 0; i < npages; i++) {
993 if (unlikely(lnb[i].lnb_rc < 0))
996 lnb[i].lnb_rc = lnb[i].lnb_len;
998 if (lnb[i].lnb_file_offset + lnb[i].lnb_len >= eof) {
999 /* send complete pages all the time */
1000 if (eof <= lnb[i].lnb_file_offset)
1003 /* all subsequent rc should be 0 */
1004 while (++i < npages)
1014 * Punch/truncate an object
1016 * IN: db - dmu_buf of the object to free data in.
1017 * off - start of section to free.
1018 * len - length of section to free (DMU_OBJECT_END => to EOF).
1020 * RETURN: 0 if success
1021 * error code if failure
1023 * The transaction passed to this routine must have
1024 * dmu_tx_hold_sa() and if off < size, dmu_tx_hold_free()
1025 * called and then assigned to a transaction group.
1027 static int __osd_object_punch(struct osd_object *obj, objset_t *os,
1028 dmu_tx_t *tx, uint64_t off, uint64_t len)
1030 dnode_t *dn = obj->oo_dn;
1031 uint64_t size = obj->oo_attr.la_size;
1034 /* Assert that the transaction has been assigned to a
1035 transaction group. */
1036 LASSERT(tx->tx_txg != 0);
1038 * Nothing to do if file already at desired length.
1040 if (len == DMU_OBJECT_END && size == off)
1043 /* if object holds encrypted content, we need to make sure we truncate
1044 * on an encryption unit boundary, or subsequent reads will get
1047 if (len != DMU_OBJECT_END)
1048 len -= LUSTRE_ENCRYPTION_UNIT_SIZE -
1049 (off & ~LUSTRE_ENCRYPTION_MASK);
1050 if (obj->oo_lma_flags & LUSTRE_ENCRYPT_FL &&
1051 off & ~LUSTRE_ENCRYPTION_MASK)
1052 off = (off & LUSTRE_ENCRYPTION_MASK) +
1053 LUSTRE_ENCRYPTION_UNIT_SIZE;
1056 /* XXX: dnode_free_range() can be used to save on dnode lookup */
1058 dmu_free_range(os, dn->dn_object, off, len, tx);
1063 static int osd_punch(const struct lu_env *env, struct dt_object *dt,
1064 __u64 start, __u64 end, struct thandle *th)
1066 struct osd_object *obj = osd_dt_obj(dt);
1067 struct osd_device *osd = osd_obj2dev(obj);
1068 struct osd_thandle *oh;
1073 LASSERT(dt_object_exists(dt));
1074 LASSERT(osd_invariant(obj));
1076 LASSERT(th != NULL);
1077 oh = container_of(th, struct osd_thandle, ot_super);
1079 write_lock(&obj->oo_attr_lock);
1081 if (end == OBD_OBJECT_EOF || end >= obj->oo_attr.la_size)
1082 len = DMU_OBJECT_END;
1085 write_unlock(&obj->oo_attr_lock);
1087 rc = __osd_object_punch(obj, osd->od_os, oh->ot_tx, start, len);
1090 if (len == DMU_OBJECT_END) {
1091 write_lock(&obj->oo_attr_lock);
1092 obj->oo_attr.la_size = start;
1093 write_unlock(&obj->oo_attr_lock);
1094 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
1095 &obj->oo_attr.la_size, 8, oh);
1100 static int osd_declare_punch(const struct lu_env *env, struct dt_object *dt,
1101 __u64 start, __u64 end, struct thandle *handle)
1103 struct osd_object *obj = osd_dt_obj(dt);
1104 struct osd_device *osd = osd_obj2dev(obj);
1105 struct osd_thandle *oh;
1109 oh = container_of(handle, struct osd_thandle, ot_super);
1111 read_lock(&obj->oo_attr_lock);
1112 if (end == OBD_OBJECT_EOF || end >= obj->oo_attr.la_size)
1113 len = DMU_OBJECT_END;
1117 /* declare we'll free some blocks ... */
1118 /* if object holds encrypted content, we need to make sure we truncate
1119 * on an encryption unit boundary, or subsequent reads will get
1122 if (obj->oo_lma_flags & LUSTRE_ENCRYPT_FL &&
1123 start & ~LUSTRE_ENCRYPTION_MASK)
1124 start = (start & LUSTRE_ENCRYPTION_MASK) +
1125 LUSTRE_ENCRYPTION_UNIT_SIZE;
1126 if (start < obj->oo_attr.la_size) {
1127 read_unlock(&obj->oo_attr_lock);
1128 dmu_tx_mark_netfree(oh->ot_tx);
1129 dmu_tx_hold_free(oh->ot_tx, obj->oo_dn->dn_object, start, len);
1131 read_unlock(&obj->oo_attr_lock);
1134 RETURN(osd_declare_quota(env, osd, obj->oo_attr.la_uid,
1135 obj->oo_attr.la_gid, obj->oo_attr.la_projid,
1136 0, oh, NULL, OSD_QID_BLK));
1139 static int osd_ladvise(const struct lu_env *env, struct dt_object *dt,
1140 __u64 start, __u64 end, enum lu_ladvise_type advice)
1154 static int osd_fallocate(const struct lu_env *env, struct dt_object *dt,
1155 __u64 start, __u64 end, int mode, struct thandle *th)
1157 int rc = -EOPNOTSUPP;
1161 * space preallocation is not supported for ZFS
1162 * Returns -EOPNOTSUPP for now
1167 static int osd_declare_fallocate(const struct lu_env *env,
1168 struct dt_object *dt, struct thandle *th)
1170 int rc = -EOPNOTSUPP;
1174 * space preallocation is not supported for ZFS
1175 * Returns -EOPNOTSUPP for now
1180 struct dt_body_operations osd_body_ops = {
1181 .dbo_read = osd_read,
1182 .dbo_declare_write = osd_declare_write,
1183 .dbo_write = osd_write,
1184 .dbo_bufs_get = osd_bufs_get,
1185 .dbo_bufs_put = osd_bufs_put,
1186 .dbo_write_prep = osd_write_prep,
1187 .dbo_declare_write_commit = osd_declare_write_commit,
1188 .dbo_write_commit = osd_write_commit,
1189 .dbo_read_prep = osd_read_prep,
1190 .dbo_declare_punch = osd_declare_punch,
1191 .dbo_punch = osd_punch,
1192 .dbo_ladvise = osd_ladvise,
1193 .dbo_declare_fallocate = osd_declare_fallocate,
1194 .dbo_fallocate = osd_fallocate,
1197 struct dt_body_operations osd_body_scrub_ops = {
1198 .dbo_read = osd_read_no_record,
1199 .dbo_declare_write = osd_declare_write,
1200 .dbo_write = osd_write,