4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 #include <sys/dmu_impl.h>
28 #include <sys/dmu_tx.h>
30 #include <sys/dnode.h>
31 #include <sys/zfs_context.h>
32 #include <sys/dmu_objset.h>
33 #include <sys/dmu_traverse.h>
34 #include <sys/dsl_dataset.h>
35 #include <sys/dsl_dir.h>
36 #include <sys/dsl_pool.h>
37 #include <sys/dsl_synctask.h>
38 #include <sys/dsl_prop.h>
39 #include <sys/dmu_zfetch.h>
40 #include <sys/zfs_ioctl.h>
42 #include <sys/zio_checksum.h>
43 #include <sys/zfs_znode.h>
45 const dmu_object_type_info_t dmu_ot[DMU_OT_NUMTYPES] = {
46 { byteswap_uint8_array, TRUE, "unallocated" },
47 { zap_byteswap, TRUE, "object directory" },
48 { byteswap_uint64_array, TRUE, "object array" },
49 { byteswap_uint8_array, TRUE, "packed nvlist" },
50 { byteswap_uint64_array, TRUE, "packed nvlist size" },
51 { byteswap_uint64_array, TRUE, "bplist" },
52 { byteswap_uint64_array, TRUE, "bplist header" },
53 { byteswap_uint64_array, TRUE, "SPA space map header" },
54 { byteswap_uint64_array, TRUE, "SPA space map" },
55 { byteswap_uint64_array, TRUE, "ZIL intent log" },
56 { dnode_buf_byteswap, TRUE, "DMU dnode" },
57 { dmu_objset_byteswap, TRUE, "DMU objset" },
58 { byteswap_uint64_array, TRUE, "DSL directory" },
59 { zap_byteswap, TRUE, "DSL directory child map"},
60 { zap_byteswap, TRUE, "DSL dataset snap map" },
61 { zap_byteswap, TRUE, "DSL props" },
62 { byteswap_uint64_array, TRUE, "DSL dataset" },
63 { zfs_znode_byteswap, TRUE, "ZFS znode" },
64 { zfs_oldacl_byteswap, TRUE, "ZFS V0 ACL" },
65 { byteswap_uint8_array, FALSE, "ZFS plain file" },
66 { zap_byteswap, TRUE, "ZFS directory" },
67 { zap_byteswap, TRUE, "ZFS master node" },
68 { zap_byteswap, TRUE, "ZFS delete queue" },
69 { byteswap_uint8_array, FALSE, "zvol object" },
70 { zap_byteswap, TRUE, "zvol prop" },
71 { byteswap_uint8_array, FALSE, "other uint8[]" },
72 { byteswap_uint64_array, FALSE, "other uint64[]" },
73 { zap_byteswap, TRUE, "other ZAP" },
74 { zap_byteswap, TRUE, "persistent error log" },
75 { byteswap_uint8_array, TRUE, "SPA history" },
76 { byteswap_uint64_array, TRUE, "SPA history offsets" },
77 { zap_byteswap, TRUE, "Pool properties" },
78 { zap_byteswap, TRUE, "DSL permissions" },
79 { zfs_acl_byteswap, TRUE, "ZFS ACL" },
80 { byteswap_uint8_array, TRUE, "ZFS SYSACL" },
81 { byteswap_uint8_array, TRUE, "FUID table" },
82 { byteswap_uint64_array, TRUE, "FUID table size" },
83 { zap_byteswap, TRUE, "DSL dataset next clones"},
84 { zap_byteswap, TRUE, "scrub work queue" },
88 dmu_buf_hold(objset_t *os, uint64_t object, uint64_t offset,
89 void *tag, dmu_buf_t **dbp)
96 err = dnode_hold(os->os, object, FTAG, &dn);
99 blkid = dbuf_whichblock(dn, offset);
100 rw_enter(&dn->dn_struct_rwlock, RW_READER);
101 db = dbuf_hold(dn, blkid, tag);
102 rw_exit(&dn->dn_struct_rwlock);
106 err = dbuf_read(db, NULL, DB_RF_CANFAIL);
113 dnode_rele(dn, FTAG);
121 return (DN_MAX_BONUSLEN);
125 dmu_set_bonus(dmu_buf_t *db, int newsize, dmu_tx_t *tx)
127 dnode_t *dn = ((dmu_buf_impl_t *)db)->db_dnode;
129 if (dn->dn_bonus != (dmu_buf_impl_t *)db)
131 if (newsize < 0 || newsize > db->db_size)
133 dnode_setbonuslen(dn, newsize, tx);
138 * returns ENOENT, EIO, or 0.
141 dmu_bonus_hold(objset_t *os, uint64_t object, void *tag, dmu_buf_t **dbp)
147 error = dnode_hold(os->os, object, FTAG, &dn);
151 rw_enter(&dn->dn_struct_rwlock, RW_READER);
152 if (dn->dn_bonus == NULL) {
153 rw_exit(&dn->dn_struct_rwlock);
154 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
155 if (dn->dn_bonus == NULL)
156 dbuf_create_bonus(dn);
159 rw_exit(&dn->dn_struct_rwlock);
161 /* as long as the bonus buf is held, the dnode will be held */
162 if (refcount_add(&db->db_holds, tag) == 1)
163 VERIFY(dnode_add_ref(dn, db));
165 dnode_rele(dn, FTAG);
167 VERIFY(0 == dbuf_read(db, NULL, DB_RF_MUST_SUCCEED));
174 * Note: longer-term, we should modify all of the dmu_buf_*() interfaces
175 * to take a held dnode rather than <os, object> -- the lookup is wasteful,
176 * and can induce severe lock contention when writing to several files
177 * whose dnodes are in the same block.
180 dmu_buf_hold_array_by_dnode(dnode_t *dn, uint64_t offset,
181 uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp)
183 dsl_pool_t *dp = NULL;
185 uint64_t blkid, nblks, i;
191 ASSERT(length <= DMU_MAX_ACCESS);
193 flags = DB_RF_CANFAIL | DB_RF_NEVERWAIT;
194 if (length > zfetch_array_rd_sz)
195 flags |= DB_RF_NOPREFETCH;
197 rw_enter(&dn->dn_struct_rwlock, RW_READER);
198 if (dn->dn_datablkshift) {
199 int blkshift = dn->dn_datablkshift;
200 nblks = (P2ROUNDUP(offset+length, 1ULL<<blkshift) -
201 P2ALIGN(offset, 1ULL<<blkshift)) >> blkshift;
203 if (offset + length > dn->dn_datablksz) {
204 zfs_panic_recover("zfs: accessing past end of object "
205 "%llx/%llx (size=%u access=%llu+%llu)",
206 (longlong_t)dn->dn_objset->
207 os_dsl_dataset->ds_object,
208 (longlong_t)dn->dn_object, dn->dn_datablksz,
209 (longlong_t)offset, (longlong_t)length);
214 dbp = kmem_zalloc(sizeof (dmu_buf_t *) * nblks, KM_SLEEP);
216 if (dn->dn_objset->os_dsl_dataset)
217 dp = dn->dn_objset->os_dsl_dataset->ds_dir->dd_pool;
218 if (dp && dsl_pool_sync_context(dp))
220 zio = zio_root(dn->dn_objset->os_spa, NULL, NULL, ZIO_FLAG_CANFAIL);
221 blkid = dbuf_whichblock(dn, offset);
222 for (i = 0; i < nblks; i++) {
223 dmu_buf_impl_t *db = dbuf_hold(dn, blkid+i, tag);
225 rw_exit(&dn->dn_struct_rwlock);
226 dmu_buf_rele_array(dbp, nblks, tag);
230 /* initiate async i/o */
232 rw_exit(&dn->dn_struct_rwlock);
233 (void) dbuf_read(db, zio, flags);
234 rw_enter(&dn->dn_struct_rwlock, RW_READER);
238 rw_exit(&dn->dn_struct_rwlock);
240 /* wait for async i/o */
242 /* track read overhead when we are in sync context */
243 if (dp && dsl_pool_sync_context(dp))
244 dp->dp_read_overhead += gethrtime() - start;
246 dmu_buf_rele_array(dbp, nblks, tag);
250 /* wait for other io to complete */
252 for (i = 0; i < nblks; i++) {
253 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbp[i];
254 mutex_enter(&db->db_mtx);
255 while (db->db_state == DB_READ ||
256 db->db_state == DB_FILL)
257 cv_wait(&db->db_changed, &db->db_mtx);
258 if (db->db_state == DB_UNCACHED)
260 mutex_exit(&db->db_mtx);
262 dmu_buf_rele_array(dbp, nblks, tag);
274 dmu_buf_hold_array(objset_t *os, uint64_t object, uint64_t offset,
275 uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp)
280 err = dnode_hold(os->os, object, FTAG, &dn);
284 err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag,
287 dnode_rele(dn, FTAG);
293 dmu_buf_hold_array_by_bonus(dmu_buf_t *db, uint64_t offset,
294 uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp)
296 dnode_t *dn = ((dmu_buf_impl_t *)db)->db_dnode;
299 err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag,
306 dmu_buf_rele_array(dmu_buf_t **dbp_fake, int numbufs, void *tag)
309 dmu_buf_impl_t **dbp = (dmu_buf_impl_t **)dbp_fake;
314 for (i = 0; i < numbufs; i++) {
316 dbuf_rele(dbp[i], tag);
319 kmem_free(dbp, sizeof (dmu_buf_t *) * numbufs);
323 dmu_prefetch(objset_t *os, uint64_t object, uint64_t offset, uint64_t len)
329 if (zfs_prefetch_disable)
332 if (len == 0) { /* they're interested in the bonus buffer */
333 dn = os->os->os_meta_dnode;
335 if (object == 0 || object >= DN_MAX_OBJECT)
338 rw_enter(&dn->dn_struct_rwlock, RW_READER);
339 blkid = dbuf_whichblock(dn, object * sizeof (dnode_phys_t));
340 dbuf_prefetch(dn, blkid);
341 rw_exit(&dn->dn_struct_rwlock);
346 * XXX - Note, if the dnode for the requested object is not
347 * already cached, we will do a *synchronous* read in the
348 * dnode_hold() call. The same is true for any indirects.
350 err = dnode_hold(os->os, object, FTAG, &dn);
354 rw_enter(&dn->dn_struct_rwlock, RW_READER);
355 if (dn->dn_datablkshift) {
356 int blkshift = dn->dn_datablkshift;
357 nblks = (P2ROUNDUP(offset+len, 1<<blkshift) -
358 P2ALIGN(offset, 1<<blkshift)) >> blkshift;
360 nblks = (offset < dn->dn_datablksz);
364 blkid = dbuf_whichblock(dn, offset);
365 for (i = 0; i < nblks; i++)
366 dbuf_prefetch(dn, blkid+i);
369 rw_exit(&dn->dn_struct_rwlock);
371 dnode_rele(dn, FTAG);
375 get_next_chunk(dnode_t *dn, uint64_t *offset, uint64_t limit)
377 uint64_t len = *offset - limit;
378 uint64_t chunk_len = dn->dn_datablksz * DMU_MAX_DELETEBLKCNT;
380 dn->dn_datablksz * EPB(dn->dn_indblkshift, SPA_BLKPTRSHIFT);
382 ASSERT(limit <= *offset);
384 if (len <= chunk_len) {
389 ASSERT(ISP2(subchunk));
391 while (*offset > limit) {
392 uint64_t initial_offset = P2ROUNDUP(*offset, subchunk);
396 /* skip over allocated data */
397 err = dnode_next_offset(dn,
398 DNODE_FIND_HOLE|DNODE_FIND_BACKWARDS, offset, 1, 1, 0);
404 ASSERT3U(*offset, <=, initial_offset);
405 *offset = P2ALIGN(*offset, subchunk);
406 delta = initial_offset - *offset;
407 if (delta >= chunk_len) {
408 *offset += delta - chunk_len;
413 /* skip over unallocated data */
414 err = dnode_next_offset(dn,
415 DNODE_FIND_BACKWARDS, offset, 1, 1, 0);
423 ASSERT3U(*offset, <, initial_offset);
429 dmu_free_long_range_impl(objset_t *os, dnode_t *dn, uint64_t offset,
430 uint64_t length, boolean_t free_dnode)
433 uint64_t object_size, start, end, len;
434 boolean_t trunc = (length == DMU_OBJECT_END);
437 align = 1 << dn->dn_datablkshift;
439 object_size = align == 1 ? dn->dn_datablksz :
440 (dn->dn_maxblkid + 1) << dn->dn_datablkshift;
442 if (trunc || (end = offset + length) > object_size)
446 length = end - offset;
450 err = get_next_chunk(dn, &start, offset);
453 len = trunc ? DMU_OBJECT_END : end - start;
455 tx = dmu_tx_create(os);
456 dmu_tx_hold_free(tx, dn->dn_object, start, len);
457 err = dmu_tx_assign(tx, TXG_WAIT);
463 dnode_free_range(dn, start, trunc ? -1 : len, tx);
465 if (start == 0 && free_dnode) {
470 length -= end - start;
479 dmu_free_long_range(objset_t *os, uint64_t object,
480 uint64_t offset, uint64_t length)
485 err = dnode_hold(os->os, object, FTAG, &dn);
488 err = dmu_free_long_range_impl(os, dn, offset, length, FALSE);
489 dnode_rele(dn, FTAG);
494 dmu_free_object(objset_t *os, uint64_t object)
500 err = dnode_hold_impl(os->os, object, DNODE_MUST_BE_ALLOCATED,
504 if (dn->dn_nlevels == 1) {
505 tx = dmu_tx_create(os);
506 dmu_tx_hold_bonus(tx, object);
507 dmu_tx_hold_free(tx, dn->dn_object, 0, DMU_OBJECT_END);
508 err = dmu_tx_assign(tx, TXG_WAIT);
510 dnode_free_range(dn, 0, DMU_OBJECT_END, tx);
517 err = dmu_free_long_range_impl(os, dn, 0, DMU_OBJECT_END, TRUE);
519 dnode_rele(dn, FTAG);
524 dmu_free_range(objset_t *os, uint64_t object, uint64_t offset,
525 uint64_t size, dmu_tx_t *tx)
528 int err = dnode_hold(os->os, object, FTAG, &dn);
531 ASSERT(offset < UINT64_MAX);
532 ASSERT(size == -1ULL || size <= UINT64_MAX - offset);
533 dnode_free_range(dn, offset, size, tx);
534 dnode_rele(dn, FTAG);
539 dmu_read(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
546 err = dnode_hold(os->os, object, FTAG, &dn);
551 * Deal with odd block sizes, where there can't be data past the first
552 * block. If we ever do the tail block optimization, we will need to
553 * handle that here as well.
555 if (dn->dn_datablkshift == 0) {
556 int newsz = offset > dn->dn_datablksz ? 0 :
557 MIN(size, dn->dn_datablksz - offset);
558 bzero((char *)buf + newsz, size - newsz);
563 uint64_t mylen = MIN(size, DMU_MAX_ACCESS / 2);
566 * NB: we could do this block-at-a-time, but it's nice
567 * to be reading in parallel.
569 err = dmu_buf_hold_array_by_dnode(dn, offset, mylen,
570 TRUE, FTAG, &numbufs, &dbp);
574 for (i = 0; i < numbufs; i++) {
577 dmu_buf_t *db = dbp[i];
581 bufoff = offset - db->db_offset;
582 tocpy = (int)MIN(db->db_size - bufoff, size);
584 bcopy((char *)db->db_data + bufoff, buf, tocpy);
588 buf = (char *)buf + tocpy;
590 dmu_buf_rele_array(dbp, numbufs, FTAG);
592 dnode_rele(dn, FTAG);
597 dmu_write(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
598 const void *buf, dmu_tx_t *tx)
606 VERIFY(0 == dmu_buf_hold_array(os, object, offset, size,
607 FALSE, FTAG, &numbufs, &dbp));
609 for (i = 0; i < numbufs; i++) {
612 dmu_buf_t *db = dbp[i];
616 bufoff = offset - db->db_offset;
617 tocpy = (int)MIN(db->db_size - bufoff, size);
619 ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
621 if (tocpy == db->db_size)
622 dmu_buf_will_fill(db, tx);
624 dmu_buf_will_dirty(db, tx);
626 bcopy(buf, (char *)db->db_data + bufoff, tocpy);
628 if (tocpy == db->db_size)
629 dmu_buf_fill_done(db, tx);
633 buf = (char *)buf + tocpy;
635 dmu_buf_rele_array(dbp, numbufs, FTAG);
640 dmu_read_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size)
646 * NB: we could do this block-at-a-time, but it's nice
647 * to be reading in parallel.
649 err = dmu_buf_hold_array(os, object, uio->uio_loffset, size, TRUE, FTAG,
654 for (i = 0; i < numbufs; i++) {
657 dmu_buf_t *db = dbp[i];
661 bufoff = uio->uio_loffset - db->db_offset;
662 tocpy = (int)MIN(db->db_size - bufoff, size);
664 err = uiomove((char *)db->db_data + bufoff, tocpy,
671 dmu_buf_rele_array(dbp, numbufs, FTAG);
677 dmu_write_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size,
687 err = dmu_buf_hold_array(os, object, uio->uio_loffset, size,
688 FALSE, FTAG, &numbufs, &dbp);
692 for (i = 0; i < numbufs; i++) {
695 dmu_buf_t *db = dbp[i];
699 bufoff = uio->uio_loffset - db->db_offset;
700 tocpy = (int)MIN(db->db_size - bufoff, size);
702 ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
704 if (tocpy == db->db_size)
705 dmu_buf_will_fill(db, tx);
707 dmu_buf_will_dirty(db, tx);
710 * XXX uiomove could block forever (eg. nfs-backed
711 * pages). There needs to be a uiolockdown() function
712 * to lock the pages in memory, so that uiomove won't
715 err = uiomove((char *)db->db_data + bufoff, tocpy,
718 if (tocpy == db->db_size)
719 dmu_buf_fill_done(db, tx);
726 dmu_buf_rele_array(dbp, numbufs, FTAG);
732 dmu_write_pages(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
733 page_t *pp, dmu_tx_t *tx)
742 err = dmu_buf_hold_array(os, object, offset, size,
743 FALSE, FTAG, &numbufs, &dbp);
747 for (i = 0; i < numbufs; i++) {
748 int tocpy, copied, thiscpy;
750 dmu_buf_t *db = dbp[i];
754 ASSERT3U(db->db_size, >=, PAGESIZE);
756 bufoff = offset - db->db_offset;
757 tocpy = (int)MIN(db->db_size - bufoff, size);
759 ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
761 if (tocpy == db->db_size)
762 dmu_buf_will_fill(db, tx);
764 dmu_buf_will_dirty(db, tx);
766 for (copied = 0; copied < tocpy; copied += PAGESIZE) {
767 ASSERT3U(pp->p_offset, ==, db->db_offset + bufoff);
768 thiscpy = MIN(PAGESIZE, tocpy - copied);
769 va = zfs_map_page(pp, S_READ);
770 bcopy(va, (char *)db->db_data + bufoff, thiscpy);
771 zfs_unmap_page(pp, va);
776 if (tocpy == db->db_size)
777 dmu_buf_fill_done(db, tx);
785 dmu_buf_rele_array(dbp, numbufs, FTAG);
788 #endif /* !__FreeBSD__ */
792 dbuf_dirty_record_t *dr;
799 dmu_sync_ready(zio_t *zio, arc_buf_t *buf, void *varg)
801 blkptr_t *bp = zio->io_bp;
803 if (!BP_IS_HOLE(bp)) {
804 dmu_sync_arg_t *in = varg;
805 dbuf_dirty_record_t *dr = in->dr;
806 dmu_buf_impl_t *db = dr->dr_dbuf;
807 ASSERT(BP_GET_TYPE(bp) == db->db_dnode->dn_type);
808 ASSERT(BP_GET_LEVEL(bp) == 0);
815 dmu_sync_done(zio_t *zio, arc_buf_t *buf, void *varg)
817 dmu_sync_arg_t *in = varg;
818 dbuf_dirty_record_t *dr = in->dr;
819 dmu_buf_impl_t *db = dr->dr_dbuf;
820 dmu_sync_cb_t *done = in->done;
822 mutex_enter(&db->db_mtx);
823 ASSERT(dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC);
824 dr->dt.dl.dr_overridden_by = *zio->io_bp; /* structure assignment */
825 dr->dt.dl.dr_override_state = DR_OVERRIDDEN;
826 cv_broadcast(&db->db_changed);
827 mutex_exit(&db->db_mtx);
830 done(&(db->db), in->arg);
832 kmem_free(in, sizeof (dmu_sync_arg_t));
836 * Intent log support: sync the block associated with db to disk.
837 * N.B. and XXX: the caller is responsible for making sure that the
838 * data isn't changing while dmu_sync() is writing it.
842 * EEXIST: this txg has already been synced, so there's nothing to to.
843 * The caller should not log the write.
845 * ENOENT: the block was dbuf_free_range()'d, so there's nothing to do.
846 * The caller should not log the write.
848 * EALREADY: this block is already in the process of being synced.
849 * The caller should track its progress (somehow).
851 * EINPROGRESS: the IO has been initiated.
852 * The caller should log this blkptr in the callback.
854 * 0: completed. Sets *bp to the blkptr just written.
855 * The caller should log this blkptr immediately.
858 dmu_sync(zio_t *pio, dmu_buf_t *db_fake,
859 blkptr_t *bp, uint64_t txg, dmu_sync_cb_t *done, void *arg)
861 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
862 objset_impl_t *os = db->db_objset;
863 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
864 tx_state_t *tx = &dp->dp_tx;
865 dbuf_dirty_record_t *dr;
868 writeprops_t wp = { 0 };
872 ASSERT(BP_IS_HOLE(bp));
875 dprintf("dmu_sync txg=%llu, s,o,q %llu %llu %llu\n",
876 txg, tx->tx_synced_txg, tx->tx_open_txg, tx->tx_quiesced_txg);
879 * XXX - would be nice if we could do this without suspending...
884 * If this txg already synced, there's nothing to do.
886 if (txg <= tx->tx_synced_txg) {
889 * If we're running ziltest, we need the blkptr regardless.
891 if (txg > spa_freeze_txg(dp->dp_spa)) {
892 /* if db_blkptr == NULL, this was an empty write */
894 *bp = *db->db_blkptr; /* structure assignment */
900 mutex_enter(&db->db_mtx);
902 if (txg == tx->tx_syncing_txg) {
903 while (db->db_data_pending) {
905 * IO is in-progress. Wait for it to finish.
906 * XXX - would be nice to be able to somehow "attach"
907 * this zio to the parent zio passed in.
909 cv_wait(&db->db_changed, &db->db_mtx);
910 if (!db->db_data_pending &&
911 db->db_blkptr && BP_IS_HOLE(db->db_blkptr)) {
913 * IO was compressed away
915 *bp = *db->db_blkptr; /* structure assignment */
916 mutex_exit(&db->db_mtx);
920 ASSERT(db->db_data_pending ||
921 (db->db_blkptr && db->db_blkptr->blk_birth == txg));
924 if (db->db_blkptr && db->db_blkptr->blk_birth == txg) {
926 * IO is already completed.
928 *bp = *db->db_blkptr; /* structure assignment */
929 mutex_exit(&db->db_mtx);
935 dr = db->db_last_dirty;
936 while (dr && dr->dr_txg > txg)
938 if (dr == NULL || dr->dr_txg < txg) {
940 * This dbuf isn't dirty, must have been free_range'd.
941 * There's no need to log writes to freed blocks, so we're done.
943 mutex_exit(&db->db_mtx);
948 ASSERT(dr->dr_txg == txg);
949 if (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
951 * We have already issued a sync write for this buffer.
953 mutex_exit(&db->db_mtx);
956 } else if (dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
958 * This buffer has already been synced. It could not
959 * have been dirtied since, or we would have cleared the state.
961 *bp = dr->dt.dl.dr_overridden_by; /* structure assignment */
962 mutex_exit(&db->db_mtx);
967 dr->dt.dl.dr_override_state = DR_IN_DMU_SYNC;
968 in = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP);
972 mutex_exit(&db->db_mtx);
975 zb.zb_objset = os->os_dsl_dataset->ds_object;
976 zb.zb_object = db->db.db_object;
977 zb.zb_level = db->db_level;
978 zb.zb_blkid = db->db_blkid;
980 wp.wp_type = db->db_dnode->dn_type;
981 wp.wp_level = db->db_level;
982 wp.wp_copies = os->os_copies;
983 wp.wp_dnchecksum = db->db_dnode->dn_checksum;
984 wp.wp_oschecksum = os->os_checksum;
985 wp.wp_dncompress = db->db_dnode->dn_compress;
986 wp.wp_oscompress = os->os_compress;
988 ASSERT(BP_IS_HOLE(bp));
990 zio = arc_write(pio, os->os_spa, &wp, DBUF_IS_L2CACHEABLE(db),
991 txg, bp, dr->dt.dl.dr_data, dmu_sync_ready, dmu_sync_done, in,
992 ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
1005 dmu_object_set_blocksize(objset_t *os, uint64_t object, uint64_t size, int ibs,
1011 err = dnode_hold(os->os, object, FTAG, &dn);
1014 err = dnode_set_blksz(dn, size, ibs, tx);
1015 dnode_rele(dn, FTAG);
1020 dmu_object_set_checksum(objset_t *os, uint64_t object, uint8_t checksum,
1025 /* XXX assumes dnode_hold will not get an i/o error */
1026 (void) dnode_hold(os->os, object, FTAG, &dn);
1027 ASSERT(checksum < ZIO_CHECKSUM_FUNCTIONS);
1028 dn->dn_checksum = checksum;
1029 dnode_setdirty(dn, tx);
1030 dnode_rele(dn, FTAG);
1034 dmu_object_set_compress(objset_t *os, uint64_t object, uint8_t compress,
1039 /* XXX assumes dnode_hold will not get an i/o error */
1040 (void) dnode_hold(os->os, object, FTAG, &dn);
1041 ASSERT(compress < ZIO_COMPRESS_FUNCTIONS);
1042 dn->dn_compress = compress;
1043 dnode_setdirty(dn, tx);
1044 dnode_rele(dn, FTAG);
1048 dmu_offset_next(objset_t *os, uint64_t object, boolean_t hole, uint64_t *off)
1053 err = dnode_hold(os->os, object, FTAG, &dn);
1057 * Sync any current changes before
1058 * we go trundling through the block pointers.
1060 for (i = 0; i < TXG_SIZE; i++) {
1061 if (list_link_active(&dn->dn_dirty_link[i]))
1064 if (i != TXG_SIZE) {
1065 dnode_rele(dn, FTAG);
1066 txg_wait_synced(dmu_objset_pool(os), 0);
1067 err = dnode_hold(os->os, object, FTAG, &dn);
1072 err = dnode_next_offset(dn, (hole ? DNODE_FIND_HOLE : 0), off, 1, 1, 0);
1073 dnode_rele(dn, FTAG);
1079 dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi)
1081 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1082 mutex_enter(&dn->dn_mtx);
1084 doi->doi_data_block_size = dn->dn_datablksz;
1085 doi->doi_metadata_block_size = dn->dn_indblkshift ?
1086 1ULL << dn->dn_indblkshift : 0;
1087 doi->doi_indirection = dn->dn_nlevels;
1088 doi->doi_checksum = dn->dn_checksum;
1089 doi->doi_compress = dn->dn_compress;
1090 doi->doi_physical_blks = (DN_USED_BYTES(dn->dn_phys) +
1091 SPA_MINBLOCKSIZE/2) >> SPA_MINBLOCKSHIFT;
1092 doi->doi_max_block_offset = dn->dn_phys->dn_maxblkid;
1093 doi->doi_type = dn->dn_type;
1094 doi->doi_bonus_size = dn->dn_bonuslen;
1095 doi->doi_bonus_type = dn->dn_bonustype;
1097 mutex_exit(&dn->dn_mtx);
1098 rw_exit(&dn->dn_struct_rwlock);
1102 * Get information on a DMU object.
1103 * If doi is NULL, just indicates whether the object exists.
1106 dmu_object_info(objset_t *os, uint64_t object, dmu_object_info_t *doi)
1109 int err = dnode_hold(os->os, object, FTAG, &dn);
1115 dmu_object_info_from_dnode(dn, doi);
1117 dnode_rele(dn, FTAG);
1122 * As above, but faster; can be used when you have a held dbuf in hand.
1125 dmu_object_info_from_db(dmu_buf_t *db, dmu_object_info_t *doi)
1127 dmu_object_info_from_dnode(((dmu_buf_impl_t *)db)->db_dnode, doi);
1131 * Faster still when you only care about the size.
1132 * This is specifically optimized for zfs_getattr().
1135 dmu_object_size_from_db(dmu_buf_t *db, uint32_t *blksize, u_longlong_t *nblk512)
1137 dnode_t *dn = ((dmu_buf_impl_t *)db)->db_dnode;
1139 *blksize = dn->dn_datablksz;
1140 /* add 1 for dnode space */
1141 *nblk512 = ((DN_USED_BYTES(dn->dn_phys) + SPA_MINBLOCKSIZE/2) >>
1142 SPA_MINBLOCKSHIFT) + 1;
1146 byteswap_uint64_array(void *vbuf, size_t size)
1148 uint64_t *buf = vbuf;
1149 size_t count = size >> 3;
1152 ASSERT((size & 7) == 0);
1154 for (i = 0; i < count; i++)
1155 buf[i] = BSWAP_64(buf[i]);
1159 byteswap_uint32_array(void *vbuf, size_t size)
1161 uint32_t *buf = vbuf;
1162 size_t count = size >> 2;
1165 ASSERT((size & 3) == 0);
1167 for (i = 0; i < count; i++)
1168 buf[i] = BSWAP_32(buf[i]);
1172 byteswap_uint16_array(void *vbuf, size_t size)
1174 uint16_t *buf = vbuf;
1175 size_t count = size >> 1;
1178 ASSERT((size & 1) == 0);
1180 for (i = 0; i < count; i++)
1181 buf[i] = BSWAP_16(buf[i]);
1186 byteswap_uint8_array(void *vbuf, size_t size)