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);
99 lprocfs_oh_tally_log2(&h[BRW_R_PAGES], npages);
101 lprocfs_oh_tally_log2(&h[BRW_R_DISK_IOSIZE], disksize);
103 lprocfs_oh_tally_log2(&h[BRW_R_IO_TIME], elapsed);
106 atomic_dec(&osd->od_w_in_flight);
107 lprocfs_oh_tally_log2(&h[BRW_W_PAGES], npages);
109 lprocfs_oh_tally_log2(&h[BRW_W_DISK_IOSIZE], disksize);
111 lprocfs_oh_tally_log2(&h[BRW_W_IO_TIME], elapsed);
115 static ssize_t __osd_read(const struct lu_env *env, struct dt_object *dt,
116 struct lu_buf *buf, loff_t *pos, size_t *size)
118 struct osd_object *obj = osd_dt_obj(dt);
122 LASSERT(dt_object_exists(dt));
125 read_lock(&obj->oo_attr_lock);
126 old_size = obj->oo_attr.la_size;
127 read_unlock(&obj->oo_attr_lock);
129 if (*pos + *size > old_size) {
133 *size = old_size - *pos;
136 rc = osd_dmu_read(osd_obj2dev(obj), obj->oo_dn, *pos, *size,
137 buf->lb_buf, DMU_READ_PREFETCH);
146 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
147 struct lu_buf *buf, loff_t *pos)
149 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
150 size_t size = buf->lb_len;
156 record_start_io(osd, READ, 0);
157 rc = __osd_read(env, dt, buf, pos, &size);
158 delta_ms = ktime_ms_delta(ktime_get(), start);
159 record_end_io(osd, READ, delta_ms, size, size >> PAGE_SHIFT);
164 static inline ssize_t osd_read_no_record(const struct lu_env *env,
165 struct dt_object *dt,
166 struct lu_buf *buf, loff_t *pos)
168 size_t size = buf->lb_len;
170 return __osd_read(env, dt, buf, pos, &size);
173 static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
174 const struct lu_buf *buf, loff_t pos,
177 struct osd_object *obj = osd_dt_obj(dt);
178 struct osd_device *osd = osd_obj2dev(obj);
179 struct osd_thandle *oh;
183 oh = container_of0(th, struct osd_thandle, ot_super);
185 /* in some cases declare can race with creation (e.g. llog)
186 * and we need to wait till object is initialized. notice
187 * LOHA_EXISTs is supposed to be the last step in the
190 /* size change (in dnode) will be declared by dmu_tx_hold_write() */
191 if (dt_object_exists(dt))
192 oid = obj->oo_dn->dn_object;
194 oid = DMU_NEW_OBJECT;
196 /* XXX: we still miss for append declaration support in ZFS
197 * -1 means append which is used by llog mostly, llog
198 * can grow upto LLOG_MIN_CHUNK_SIZE*8 records */
200 pos = max_t(loff_t, 256 * 8 * LLOG_MIN_CHUNK_SIZE,
201 obj->oo_attr.la_size + (2 << 20));
202 osd_tx_hold_write(oh->ot_tx, oid, obj->oo_dn, pos, buf->lb_len);
204 /* dt_declare_write() is usually called for system objects, such
205 * as llog or last_rcvd files. We needn't enforce quota on those
206 * objects, so always set the lqi_space as 0. */
207 RETURN(osd_declare_quota(env, osd, obj->oo_attr.la_uid,
208 obj->oo_attr.la_gid, obj->oo_attr.la_projid,
209 0, oh, NULL, OSD_QID_BLK));
212 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
213 const struct lu_buf *buf, loff_t *pos,
216 struct osd_object *obj = osd_dt_obj(dt);
217 struct osd_device *osd = osd_obj2dev(obj);
218 struct osd_thandle *oh;
219 uint64_t offset = *pos;
224 LASSERT(dt_object_exists(dt));
228 oh = container_of0(th, struct osd_thandle, ot_super);
230 osd_dmu_write(osd, obj->oo_dn, offset, (uint64_t)buf->lb_len,
231 buf->lb_buf, oh->ot_tx);
232 write_lock(&obj->oo_attr_lock);
233 if (obj->oo_attr.la_size < offset + buf->lb_len) {
234 obj->oo_attr.la_size = offset + buf->lb_len;
235 write_unlock(&obj->oo_attr_lock);
236 /* osd_object_sa_update() will be copying directly from oo_attr
237 * into dbuf. any update within a single txg will copy the
239 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
240 &obj->oo_attr.la_size, 8, oh);
244 write_unlock(&obj->oo_attr_lock);
255 * XXX: for the moment I don't want to use lnb_flags for osd-internal
256 * purposes as it's not very well defined ...
257 * instead I use the lowest bit of the address so that:
258 * arc buffer: .lnb_data = abuf (arc we loan for write)
259 * dbuf buffer: .lnb_data = dbuf | 1 (dbuf we get for read)
260 * copy buffer: .lnb_page->mapping = obj (page we allocate for write)
264 static int osd_bufs_put(const struct lu_env *env, struct dt_object *dt,
265 struct niobuf_local *lnb, int npages)
267 struct osd_object *obj = osd_dt_obj(dt);
268 struct osd_device *osd = osd_obj2dev(obj);
272 LASSERT(dt_object_exists(dt));
275 for (i = 0; i < npages; i++) {
276 if (lnb[i].lnb_page == NULL)
278 if (lnb[i].lnb_page->mapping == (void *)obj) {
279 /* this is anonymous page allocated for copy-write */
280 lnb[i].lnb_page->mapping = NULL;
281 __free_page(lnb[i].lnb_page);
282 atomic_dec(&osd->od_zerocopy_alloc);
284 /* see comment in osd_bufs_get_read() */
285 ptr = (unsigned long)lnb[i].lnb_data;
288 dmu_buf_rele((void *)ptr, osd_0copy_tag);
289 atomic_dec(&osd->od_zerocopy_pin);
290 } else if (lnb[i].lnb_data != NULL) {
291 int j, apages, abufsz;
292 abufsz = arc_buf_size(lnb[i].lnb_data);
293 apages = abufsz / PAGE_SIZE;
294 /* these references to pages must be invalidated
295 * to prevent access in osd_bufs_put() */
296 for (j = 0; j < apages; j++)
297 lnb[i + j].lnb_page = NULL;
298 dmu_return_arcbuf(lnb[i].lnb_data);
299 atomic_dec(&osd->od_zerocopy_loan);
302 lnb[i].lnb_page = NULL;
303 lnb[i].lnb_data = NULL;
309 static inline struct page *kmem_to_page(void *addr)
311 LASSERT(!((unsigned long)addr & ~PAGE_MASK));
312 if (is_vmalloc_addr(addr))
313 return vmalloc_to_page(addr);
315 return virt_to_page(addr);
319 * Prepare buffers for read.
321 * The function maps the range described by \a off and \a len to \a lnb array.
322 * dmu_buf_hold_array_by_bonus() finds/creates appropriate ARC buffers, then
323 * we fill \a lnb array with the pages storing ARC buffers. Notice the current
324 * implementationt passes TRUE to dmu_buf_hold_array_by_bonus() to fill ARC
325 * buffers with actual data, I/O is done in the conext of osd_bufs_get_read().
326 * A better implementation would just return the buffers (potentially unfilled)
327 * and subsequent osd_read_prep() would do I/O for many ranges concurrently.
329 * \param[in] env environment
330 * \param[in] obj object
331 * \param[in] off offset in bytes
332 * \param[in] len the number of bytes to access
333 * \param[out] lnb array of local niobufs pointing to the buffers with data
335 * \retval 0 for success
336 * \retval negative error number of failure
338 static int osd_bufs_get_read(const struct lu_env *env, struct osd_object *obj,
339 loff_t off, ssize_t len, struct niobuf_local *lnb)
341 struct osd_device *osd = osd_obj2dev(obj);
342 int rc, i, numbufs, npages = 0, drop_cache = 0;
343 ktime_t start = ktime_get();
348 record_start_io(osd, READ, 0);
350 if (obj->oo_attr.la_size >= osd->od_readcache_max_filesize)
353 /* grab buffers for read:
354 * OSD API let us to grab buffers first, then initiate IO(s)
355 * so that all required IOs will be done in parallel, but at the
356 * moment DMU doesn't provide us with a method to grab buffers.
357 * If we discover this is a vital for good performance we
358 * can get own replacement for dmu_buf_hold_array_by_bonus().
361 (obj->oo_dn->dn_datablkshift != 0 ||
362 off < obj->oo_dn->dn_datablksz)) {
363 if (obj->oo_dn->dn_datablkshift == 0 &&
364 off + len > obj->oo_dn->dn_datablksz)
365 len = obj->oo_dn->dn_datablksz - off;
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 thispage = PAGE_SIZE;
390 thispage -= bufoff & (PAGE_SIZE - 1);
391 thispage = min(tocpy, thispage);
394 lnb->lnb_file_offset = off;
395 lnb->lnb_page_offset = bufoff & ~PAGE_MASK;
396 lnb->lnb_len = thispage;
397 lnb->lnb_page = kmem_to_page(dbp[i]->db_data +
399 /* mark just a single slot: we need this
400 * reference to dbuf to be released once */
414 dbuf_set_pending_evict(dbp[i]);
416 /* steal dbuf so dmu_buf_rele_array() can't release
421 dmu_buf_rele_array(dbp, numbufs, osd_0copy_tag);
424 delta_ms = ktime_ms_delta(ktime_get(), start);
425 record_end_io(osd, READ, delta_ms, npages * PAGE_SIZE, npages);
431 osd_bufs_put(env, &obj->oo_dt, lnb - npages, npages);
435 static inline arc_buf_t *osd_request_arcbuf(dnode_t *dn, size_t bs)
439 abuf = dmu_request_arcbuf(&dn->dn_bonus->db, bs);
441 return ERR_PTR(-ENOMEM);
443 #if ZFS_VERSION_CODE < OBD_OCD_VERSION(0, 7, 0, 0)
445 * ZFS prior to 0.7.0 doesn't guarantee PAGE_SIZE alignment for zio
446 * blocks smaller than (PAGE_SIZE << 2). This poses a problem of
447 * setting up page array for RDMA transfer. See LU-9305.
449 if ((unsigned long)abuf->b_data & ~PAGE_MASK) {
450 dmu_return_arcbuf(abuf);
458 static int osd_bufs_get_write(const struct lu_env *env, struct osd_object *obj,
459 loff_t off, ssize_t len, struct niobuf_local *lnb)
461 struct osd_device *osd = osd_obj2dev(obj);
462 int poff, plen, off_in_block, sz_in_block;
463 int rc, i = 0, npages = 0;
464 dnode_t *dn = obj->oo_dn;
466 uint32_t bs = dn->dn_datablksz;
470 * currently only full blocks are subject to zerocopy approach:
471 * so that we're sure nobody is trying to update the same block
474 LASSERT(npages < PTLRPC_MAX_BRW_PAGES);
476 off_in_block = off & (bs - 1);
477 sz_in_block = min_t(int, bs - off_in_block, len);
480 if (sz_in_block == bs) {
481 /* full block, try to use zerocopy */
482 abuf = osd_request_arcbuf(dn, bs);
483 if (unlikely(IS_ERR(abuf)))
484 GOTO(out_err, rc = PTR_ERR(abuf));
488 atomic_inc(&osd->od_zerocopy_loan);
490 /* go over pages arcbuf contains, put them as
491 * local niobufs for ptlrpc's bulks */
492 while (sz_in_block > 0) {
493 plen = min_t(int, sz_in_block, PAGE_SIZE);
495 lnb[i].lnb_file_offset = off;
496 lnb[i].lnb_page_offset = 0;
497 lnb[i].lnb_len = plen;
499 if (sz_in_block == bs)
500 lnb[i].lnb_data = abuf;
502 lnb[i].lnb_data = NULL;
504 /* this one is not supposed to fail */
505 lnb[i].lnb_page = kmem_to_page(abuf->b_data +
507 LASSERT(lnb[i].lnb_page);
509 lprocfs_counter_add(osd->od_stats,
510 LPROC_OSD_ZEROCOPY_IO, 1);
515 off_in_block += plen;
520 if (off_in_block == 0 && len < bs &&
521 off + len >= obj->oo_attr.la_size)
522 lprocfs_counter_add(osd->od_stats,
523 LPROC_OSD_TAIL_IO, 1);
525 /* can't use zerocopy, allocate temp. buffers */
526 poff = off & (PAGE_SIZE - 1);
527 while (sz_in_block > 0) {
528 plen = min_t(int, poff + sz_in_block,
532 lnb[i].lnb_file_offset = off;
533 lnb[i].lnb_page_offset = poff;
536 lnb[i].lnb_len = plen;
538 lnb[i].lnb_data = NULL;
540 lnb[i].lnb_page = alloc_page(OSD_GFP_IO);
541 if (unlikely(lnb[i].lnb_page == NULL))
542 GOTO(out_err, rc = -ENOMEM);
544 LASSERT(lnb[i].lnb_page->mapping == NULL);
545 lnb[i].lnb_page->mapping = (void *)obj;
547 atomic_inc(&osd->od_zerocopy_alloc);
548 lprocfs_counter_add(osd->od_stats,
549 LPROC_OSD_COPY_IO, 1);
563 osd_bufs_put(env, &obj->oo_dt, lnb, npages);
567 static int osd_bufs_get(const struct lu_env *env, struct dt_object *dt,
568 loff_t offset, ssize_t len, struct niobuf_local *lnb,
569 enum dt_bufs_type rw)
571 struct osd_object *obj = osd_dt_obj(dt);
574 LASSERT(dt_object_exists(dt));
577 if (rw & DT_BUFS_TYPE_WRITE)
578 rc = osd_bufs_get_write(env, obj, offset, len, lnb);
580 rc = osd_bufs_get_read(env, obj, offset, len, lnb);
585 static int osd_write_prep(const struct lu_env *env, struct dt_object *dt,
586 struct niobuf_local *lnb, int npages)
588 struct osd_object *obj = osd_dt_obj(dt);
590 LASSERT(dt_object_exists(dt));
596 static inline uint64_t osd_roundup2blocksz(uint64_t size,
602 size += offset % blksz;
604 if (likely(is_power_of_2(blksz)))
605 return PO2_ROUNDUP_TYPED(size, blksz, uint64_t);
612 static int osd_declare_write_commit(const struct lu_env *env,
613 struct dt_object *dt,
614 struct niobuf_local *lnb, int npages,
617 struct osd_object *obj = osd_dt_obj(dt);
618 struct osd_device *osd = osd_obj2dev(obj);
619 struct osd_thandle *oh;
622 uint32_t blksz = obj->oo_dn->dn_datablksz;
623 int i, rc, flags = 0;
626 struct page *last_page = NULL;
627 unsigned long discont_pages = 0;
628 enum osd_qid_declare_flags declare_flags = OSD_QID_BLK;
631 LASSERT(dt_object_exists(dt));
637 oh = container_of0(th, struct osd_thandle, ot_super);
639 for (i = 0; i < npages; i++) {
640 if (last_page && lnb[i].lnb_page->index != (last_page->index + 1))
642 last_page = lnb[i].lnb_page;
644 /* ENOSPC, network RPC error, etc.
645 * We don't want to book space for pages which will be
646 * skipped in osd_write_commit(). Hence we skip pages
647 * with lnb_rc != 0 here too */
649 /* ignore quota for the whole request if any page is from
650 * client cache or written by root.
652 * XXX once we drop the 1.8 client support, the checking
653 * for whether page is from cache can be simplified as:
654 * !(lnb[i].flags & OBD_BRW_SYNC)
656 * XXX we could handle this on per-lnb basis as done by
658 if ((lnb[i].lnb_flags & OBD_BRW_NOQUOTA) ||
659 (lnb[i].lnb_flags & (OBD_BRW_FROM_GRANT | OBD_BRW_SYNC)) ==
661 declare_flags |= OSD_QID_FORCE;
664 /* first valid lnb */
665 offset = lnb[i].lnb_file_offset;
666 size = lnb[i].lnb_len;
669 if (offset + size == lnb[i].lnb_file_offset) {
670 /* this lnb is contiguous to the previous one */
671 size += lnb[i].lnb_len;
675 osd_tx_hold_write(oh->ot_tx, obj->oo_dn->dn_object,
676 obj->oo_dn, offset, size);
677 /* Estimating space to be consumed by a write is rather
678 * complicated with ZFS. As a consequence, we don't account for
679 * indirect blocks and just use as a rough estimate the worse
680 * case where the old space is being held by a snapshot. Quota
681 * overrun will be adjusted once the operation is committed, if
683 space += osd_roundup2blocksz(size, offset, blksz);
685 offset = lnb[i].lnb_file_offset;
686 size = lnb[i].lnb_len;
690 osd_tx_hold_write(oh->ot_tx, obj->oo_dn->dn_object, obj->oo_dn,
692 space += osd_roundup2blocksz(size, offset, blksz);
695 /* backend zfs filesystem might be configured to store multiple data
697 space *= osd->od_os->os_copies;
699 CDEBUG(D_QUOTA, "writing %d pages, reserving %lldK of quota space\n",
702 record_start_io(osd, WRITE, discont_pages);
704 /* acquire quota space if needed */
705 rc = osd_declare_quota(env, osd, obj->oo_attr.la_uid,
706 obj->oo_attr.la_gid, obj->oo_attr.la_projid,
707 space, oh, &flags, declare_flags);
709 if (!synced && rc == -EDQUOT && (flags & QUOTA_FL_SYNC) != 0) {
710 dt_sync(env, th->th_dev);
712 CDEBUG(D_QUOTA, "retry after sync\n");
717 /* we need only to store the overquota flags in the first lnb for
718 * now, once we support multiple objects BRW, this code needs be
720 if (flags & QUOTA_FL_OVER_USRQUOTA)
721 lnb[0].lnb_flags |= OBD_BRW_OVER_USRQUOTA;
722 if (flags & QUOTA_FL_OVER_GRPQUOTA)
723 lnb[0].lnb_flags |= OBD_BRW_OVER_GRPQUOTA;
724 #ifdef ZFS_PROJINHERIT
725 if (flags & QUOTA_FL_OVER_PRJQUOTA)
726 lnb[0].lnb_flags |= OBD_BRW_OVER_PRJQUOTA;
733 * Policy to grow ZFS block size by write pattern.
734 * For sequential write, it grows block size gradually until it reaches the
735 * maximum blocksize the dataset can support. Otherwise, it will pick a
736 * a block size by the writing region of this I/O.
738 static int osd_grow_blocksize(struct osd_object *obj, struct osd_thandle *oh,
739 uint64_t start, uint64_t end)
741 struct osd_device *osd = osd_obj2dev(obj);
742 dnode_t *dn = obj->oo_dn;
748 if (dn->dn_maxblkid > 0) /* can't change block size */
751 if (dn->dn_datablksz >= osd->od_max_blksz)
754 down_write(&obj->oo_guard);
756 blksz = dn->dn_datablksz;
757 if (blksz >= osd->od_max_blksz) /* check again after grabbing lock */
758 GOTO(out_unlock, rc);
760 /* now ZFS can support up to 16MB block size, and if the write
761 * is sequential, it just increases the block size gradually */
762 if (start <= blksz) { /* sequential */
763 blksz = (uint32_t)min_t(uint64_t, osd->od_max_blksz, end);
764 } else { /* sparse, pick a block size by write region */
765 blksz = (uint32_t)min_t(uint64_t, osd->od_max_blksz,
769 if (!is_power_of_2(blksz))
770 blksz = size_roundup_power2(blksz);
772 if (blksz > dn->dn_datablksz) {
773 rc = -dmu_object_set_blocksize(osd->od_os, dn->dn_object,
774 blksz, 0, oh->ot_tx);
775 LASSERT(ergo(rc == 0, dn->dn_datablksz >= blksz));
777 CDEBUG(D_INODE, "object "DFID": change block size"
778 "%u -> %u error rc = %d\n",
779 PFID(lu_object_fid(&obj->oo_dt.do_lu)),
780 dn->dn_datablksz, blksz, rc);
784 up_write(&obj->oo_guard);
789 static void osd_evict_dbufs_after_write(struct osd_object *obj,
790 loff_t off, ssize_t len)
795 rc = -dmu_buf_hold_array_by_bonus(&obj->oo_dn->dn_bonus->db, off, len,
796 TRUE, osd_0copy_tag, &numbufs, &dbp);
800 for (i = 0; i < numbufs; i++)
801 dbuf_set_pending_evict(dbp[i]);
803 dmu_buf_rele_array(dbp, numbufs, osd_0copy_tag);
806 static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
807 struct niobuf_local *lnb, int npages,
810 struct osd_object *obj = osd_dt_obj(dt);
811 struct osd_device *osd = osd_obj2dev(obj);
812 struct osd_thandle *oh;
813 uint64_t new_size = 0;
814 int i, abufsz, rc = 0, drop_cache = 0;
815 unsigned long iosize = 0;
818 LASSERT(dt_object_exists(dt));
822 oh = container_of0(th, struct osd_thandle, ot_super);
824 /* adjust block size. Assume the buffers are sorted. */
825 (void)osd_grow_blocksize(obj, oh, lnb[0].lnb_file_offset,
826 lnb[npages - 1].lnb_file_offset +
827 lnb[npages - 1].lnb_len);
829 if (obj->oo_attr.la_size >= osd->od_readcache_max_filesize ||
830 lnb[npages - 1].lnb_file_offset + lnb[npages - 1].lnb_len >=
831 osd->od_readcache_max_filesize)
834 if (OBD_FAIL_CHECK(OBD_FAIL_OST_MAPBLK_ENOSPC))
837 /* LU-8791: take oo_guard to avoid the deadlock that changing block
838 * size and assigning arcbuf take place at the same time.
842 * -> osd_grow_blocksize() with osd_object::oo_guard held
843 * -> dmu_object_set_blocksize()
844 * -> dnode_set_blksz(), with dnode_t::dn_struct_rwlock
847 * -> dmu_buf_will_dirty()
849 * -> wait for the dbuf state to change
852 * -> dmu_assign_arcbuf()
853 * -> dbuf_assign_arcbuf(), set dbuf state to DB_FILL
855 * -> try to hold the read lock of dnode_t::dn_struct_rwlock
857 * By taking the read lock, it can avoid thread 2 to enter into the
858 * critical section of assigning the arcbuf, while thread 1 is
859 * changing the block size.
861 down_read(&obj->oo_guard);
862 for (i = 0; i < npages; i++) {
863 CDEBUG(D_INODE, "write %u bytes at %u\n",
864 (unsigned) lnb[i].lnb_len,
865 (unsigned) lnb[i].lnb_file_offset);
868 /* ENOSPC, network RPC error, etc.
869 * Unlike ldiskfs, zfs allocates new blocks on rewrite,
870 * so we skip this page if lnb_rc is set to -ENOSPC */
871 CDEBUG(D_INODE, "obj "DFID": skipping lnb[%u]: rc=%d\n",
872 PFID(lu_object_fid(&dt->do_lu)), i,
877 if (new_size < lnb[i].lnb_file_offset + lnb[i].lnb_len)
878 new_size = lnb[i].lnb_file_offset + lnb[i].lnb_len;
879 if (lnb[i].lnb_page == NULL)
882 if (lnb[i].lnb_page->mapping == (void *)obj) {
883 osd_dmu_write(osd, obj->oo_dn, lnb[i].lnb_file_offset,
884 lnb[i].lnb_len, kmap(lnb[i].lnb_page),
886 kunmap(lnb[i].lnb_page);
887 iosize += lnb[i].lnb_len;
888 abufsz = lnb[i].lnb_len; /* to drop cache below */
889 } else if (lnb[i].lnb_data) {
891 LASSERT(((unsigned long)lnb[i].lnb_data & 1) == 0);
892 /* buffer loaned for zerocopy, try to use it.
893 * notice that dmu_assign_arcbuf() is smart
894 * enough to recognize changed blocksize
895 * in this case it fallbacks to dmu_write() */
896 abufsz = arc_buf_size(lnb[i].lnb_data);
897 LASSERT(abufsz & PAGE_MASK);
898 apages = abufsz / PAGE_SIZE;
899 LASSERT(i + apages <= npages);
900 /* these references to pages must be invalidated
901 * to prevent access in osd_bufs_put() */
902 for (j = 0; j < apages; j++)
903 lnb[i + j].lnb_page = NULL;
904 dmu_assign_arcbuf(&obj->oo_dn->dn_bonus->db,
905 lnb[i].lnb_file_offset,
906 lnb[i].lnb_data, oh->ot_tx);
907 /* drop the reference, otherwise osd_put_bufs()
908 * will be releasing it - bad! */
909 lnb[i].lnb_data = NULL;
910 atomic_dec(&osd->od_zerocopy_loan);
913 /* we don't want to deal with cache if nothing
914 * has been send to ZFS at this step */
921 /* we have to mark dbufs for eviction here because
922 * dmu_assign_arcbuf() may create a new dbuf for
924 osd_evict_dbufs_after_write(obj, lnb[i].lnb_file_offset,
927 up_read(&obj->oo_guard);
929 if (unlikely(new_size == 0)) {
930 /* no pages to write, no transno is needed */
932 /* it is important to return 0 even when all lnb_rc == -ENOSPC
933 * since ofd_commitrw_write() retries several times on ENOSPC */
934 record_end_io(osd, WRITE, 0, 0, 0);
938 write_lock(&obj->oo_attr_lock);
939 if (obj->oo_attr.la_size < new_size) {
940 obj->oo_attr.la_size = new_size;
941 write_unlock(&obj->oo_attr_lock);
942 /* osd_object_sa_update() will be copying directly from
943 * oo_attr into dbuf. any update within a single txg will copy
945 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
946 &obj->oo_attr.la_size, 8, oh);
948 write_unlock(&obj->oo_attr_lock);
951 record_end_io(osd, WRITE, 0, iosize, npages);
956 static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
957 struct niobuf_local *lnb, int npages)
959 struct osd_object *obj = osd_dt_obj(dt);
963 LASSERT(dt_object_exists(dt));
966 read_lock(&obj->oo_attr_lock);
967 eof = obj->oo_attr.la_size;
968 read_unlock(&obj->oo_attr_lock);
970 for (i = 0; i < npages; i++) {
971 if (unlikely(lnb[i].lnb_rc < 0))
974 lnb[i].lnb_rc = lnb[i].lnb_len;
976 if (lnb[i].lnb_file_offset + lnb[i].lnb_len >= eof) {
977 if (eof <= lnb[i].lnb_file_offset)
980 lnb[i].lnb_rc = eof - lnb[i].lnb_file_offset;
982 /* all subsequent rc should be 0 */
993 * Punch/truncate an object
995 * IN: db - dmu_buf of the object to free data in.
996 * off - start of section to free.
997 * len - length of section to free (DMU_OBJECT_END => to EOF).
999 * RETURN: 0 if success
1000 * error code if failure
1002 * The transaction passed to this routine must have
1003 * dmu_tx_hold_sa() and if off < size, dmu_tx_hold_free()
1004 * called and then assigned to a transaction group.
1006 static int __osd_object_punch(objset_t *os, dnode_t *dn, dmu_tx_t *tx,
1007 uint64_t size, uint64_t off, uint64_t len)
1011 /* Assert that the transaction has been assigned to a
1012 transaction group. */
1013 LASSERT(tx->tx_txg != 0);
1015 * Nothing to do if file already at desired length.
1017 if (len == DMU_OBJECT_END && size == off)
1020 /* XXX: dnode_free_range() can be used to save on dnode lookup */
1022 dmu_free_range(os, dn->dn_object, off, len, tx);
1027 static int osd_punch(const struct lu_env *env, struct dt_object *dt,
1028 __u64 start, __u64 end, struct thandle *th)
1030 struct osd_object *obj = osd_dt_obj(dt);
1031 struct osd_device *osd = osd_obj2dev(obj);
1032 struct osd_thandle *oh;
1037 LASSERT(dt_object_exists(dt));
1038 LASSERT(osd_invariant(obj));
1040 LASSERT(th != NULL);
1041 oh = container_of0(th, struct osd_thandle, ot_super);
1043 write_lock(&obj->oo_attr_lock);
1045 if (end == OBD_OBJECT_EOF || end >= obj->oo_attr.la_size)
1046 len = DMU_OBJECT_END;
1049 write_unlock(&obj->oo_attr_lock);
1051 rc = __osd_object_punch(osd->od_os, obj->oo_dn, oh->ot_tx,
1052 obj->oo_attr.la_size, start, len);
1054 if (len == DMU_OBJECT_END) {
1055 write_lock(&obj->oo_attr_lock);
1056 obj->oo_attr.la_size = start;
1057 write_unlock(&obj->oo_attr_lock);
1058 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
1059 &obj->oo_attr.la_size, 8, oh);
1064 static int osd_declare_punch(const struct lu_env *env, struct dt_object *dt,
1065 __u64 start, __u64 end, struct thandle *handle)
1067 struct osd_object *obj = osd_dt_obj(dt);
1068 struct osd_device *osd = osd_obj2dev(obj);
1069 struct osd_thandle *oh;
1073 oh = container_of0(handle, struct osd_thandle, ot_super);
1075 read_lock(&obj->oo_attr_lock);
1076 if (end == OBD_OBJECT_EOF || end >= obj->oo_attr.la_size)
1077 len = DMU_OBJECT_END;
1081 /* declare we'll free some blocks ... */
1082 if (start < obj->oo_attr.la_size) {
1083 read_unlock(&obj->oo_attr_lock);
1084 dmu_tx_mark_netfree(oh->ot_tx);
1085 dmu_tx_hold_free(oh->ot_tx, obj->oo_dn->dn_object, start, len);
1087 read_unlock(&obj->oo_attr_lock);
1090 RETURN(osd_declare_quota(env, osd, obj->oo_attr.la_uid,
1091 obj->oo_attr.la_gid, obj->oo_attr.la_projid,
1092 0, oh, NULL, OSD_QID_BLK));
1095 static int osd_ladvise(const struct lu_env *env, struct dt_object *dt,
1096 __u64 start, __u64 end, enum lu_ladvise_type advice)
1110 struct dt_body_operations osd_body_ops = {
1111 .dbo_read = osd_read,
1112 .dbo_declare_write = osd_declare_write,
1113 .dbo_write = osd_write,
1114 .dbo_bufs_get = osd_bufs_get,
1115 .dbo_bufs_put = osd_bufs_put,
1116 .dbo_write_prep = osd_write_prep,
1117 .dbo_declare_write_commit = osd_declare_write_commit,
1118 .dbo_write_commit = osd_write_commit,
1119 .dbo_read_prep = osd_read_prep,
1120 .dbo_declare_punch = osd_declare_punch,
1121 .dbo_punch = osd_punch,
1122 .dbo_ladvise = osd_ladvise,
1125 struct dt_body_operations osd_body_scrub_ops = {
1126 .dbo_read = osd_read_no_record,
1127 .dbo_declare_write = osd_declare_write,
1128 .dbo_write = osd_write,