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 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <sys/dmu_impl.h>
30 #include <sys/dmu_tx.h>
32 #include <sys/dnode.h>
33 #include <sys/zfs_context.h>
34 #include <sys/dmu_objset.h>
35 #include <sys/dmu_traverse.h>
36 #include <sys/dsl_dataset.h>
37 #include <sys/dsl_dir.h>
38 #include <sys/dsl_pool.h>
39 #include <sys/dsl_synctask.h>
40 #include <sys/dsl_prop.h>
41 #include <sys/dmu_zfetch.h>
42 #include <sys/zfs_ioctl.h>
44 #include <sys/zio_checksum.h>
46 const dmu_object_type_info_t dmu_ot[DMU_OT_NUMTYPES] = {
47 { byteswap_uint8_array, TRUE, "unallocated" },
48 { zap_byteswap, TRUE, "object directory" },
49 { byteswap_uint64_array, TRUE, "object array" },
50 { byteswap_uint8_array, TRUE, "packed nvlist" },
51 { byteswap_uint64_array, TRUE, "packed nvlist size" },
52 { byteswap_uint64_array, TRUE, "bplist" },
53 { byteswap_uint64_array, TRUE, "bplist header" },
54 { byteswap_uint64_array, TRUE, "SPA space map header" },
55 { byteswap_uint64_array, TRUE, "SPA space map" },
56 { byteswap_uint64_array, TRUE, "ZIL intent log" },
57 { dnode_buf_byteswap, TRUE, "DMU dnode" },
58 { dmu_objset_byteswap, TRUE, "DMU objset" },
59 { byteswap_uint64_array, TRUE, "DSL directory" },
60 { zap_byteswap, TRUE, "DSL directory child map"},
61 { zap_byteswap, TRUE, "DSL dataset snap map" },
62 { zap_byteswap, TRUE, "DSL props" },
63 { byteswap_uint64_array, TRUE, "DSL dataset" },
64 { zfs_znode_byteswap, TRUE, "ZFS znode" },
65 { zfs_acl_byteswap, TRUE, "ZFS ACL" },
66 { byteswap_uint8_array, FALSE, "ZFS plain file" },
67 { zap_byteswap, TRUE, "ZFS directory" },
68 { zap_byteswap, TRUE, "ZFS master node" },
69 { zap_byteswap, TRUE, "ZFS delete queue" },
70 { byteswap_uint8_array, FALSE, "zvol object" },
71 { zap_byteswap, TRUE, "zvol prop" },
72 { byteswap_uint8_array, FALSE, "other uint8[]" },
73 { byteswap_uint64_array, FALSE, "other uint64[]" },
74 { zap_byteswap, TRUE, "other ZAP" },
75 { zap_byteswap, TRUE, "persistent error log" },
76 { byteswap_uint8_array, TRUE, "SPA history" },
77 { byteswap_uint64_array, TRUE, "SPA history offsets" },
78 { zap_byteswap, TRUE, "Pool properties" },
82 dmu_buf_hold(objset_t *os, uint64_t object, uint64_t offset,
83 void *tag, dmu_buf_t **dbp)
90 err = dnode_hold(os->os, object, FTAG, &dn);
93 blkid = dbuf_whichblock(dn, offset);
94 rw_enter(&dn->dn_struct_rwlock, RW_READER);
95 db = dbuf_hold(dn, blkid, tag);
96 rw_exit(&dn->dn_struct_rwlock);
100 err = dbuf_read(db, NULL, DB_RF_CANFAIL);
107 dnode_rele(dn, FTAG);
115 return (DN_MAX_BONUSLEN);
119 * returns ENOENT, EIO, or 0.
122 dmu_bonus_hold(objset_t *os, uint64_t object, void *tag, dmu_buf_t **dbp)
128 err = dnode_hold(os->os, object, FTAG, &dn);
132 rw_enter(&dn->dn_struct_rwlock, RW_READER);
133 if (dn->dn_bonus == NULL) {
134 rw_exit(&dn->dn_struct_rwlock);
135 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
136 if (dn->dn_bonus == NULL)
137 dn->dn_bonus = dbuf_create_bonus(dn);
140 rw_exit(&dn->dn_struct_rwlock);
141 mutex_enter(&db->db_mtx);
142 count = refcount_add(&db->db_holds, tag);
143 mutex_exit(&db->db_mtx);
145 dnode_add_ref(dn, db);
146 dnode_rele(dn, FTAG);
148 VERIFY(0 == dbuf_read(db, NULL, DB_RF_MUST_SUCCEED));
155 * Note: longer-term, we should modify all of the dmu_buf_*() interfaces
156 * to take a held dnode rather than <os, object> -- the lookup is wasteful,
157 * and can induce severe lock contention when writing to several files
158 * whose dnodes are in the same block.
161 dmu_buf_hold_array_by_dnode(dnode_t *dn, uint64_t offset,
162 uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp)
165 uint64_t blkid, nblks, i;
170 ASSERT(length <= DMU_MAX_ACCESS);
172 flags = DB_RF_CANFAIL | DB_RF_NEVERWAIT;
173 if (length > zfetch_array_rd_sz)
174 flags |= DB_RF_NOPREFETCH;
176 rw_enter(&dn->dn_struct_rwlock, RW_READER);
177 if (dn->dn_datablkshift) {
178 int blkshift = dn->dn_datablkshift;
179 nblks = (P2ROUNDUP(offset+length, 1ULL<<blkshift) -
180 P2ALIGN(offset, 1ULL<<blkshift)) >> blkshift;
182 if (offset + length > dn->dn_datablksz) {
183 zfs_panic_recover("zfs: accessing past end of object "
184 "%llx/%llx (size=%u access=%llu+%llu)",
185 (longlong_t)dn->dn_objset->
186 os_dsl_dataset->ds_object,
187 (longlong_t)dn->dn_object, dn->dn_datablksz,
188 (longlong_t)offset, (longlong_t)length);
193 dbp = kmem_zalloc(sizeof (dmu_buf_t *) * nblks, KM_SLEEP);
195 zio = zio_root(dn->dn_objset->os_spa, NULL, NULL, TRUE);
196 blkid = dbuf_whichblock(dn, offset);
197 for (i = 0; i < nblks; i++) {
198 dmu_buf_impl_t *db = dbuf_hold(dn, blkid+i, tag);
200 rw_exit(&dn->dn_struct_rwlock);
201 dmu_buf_rele_array(dbp, nblks, tag);
205 /* initiate async i/o */
207 rw_exit(&dn->dn_struct_rwlock);
208 (void) dbuf_read(db, zio, flags);
209 rw_enter(&dn->dn_struct_rwlock, RW_READER);
213 rw_exit(&dn->dn_struct_rwlock);
215 /* wait for async i/o */
218 dmu_buf_rele_array(dbp, nblks, tag);
222 /* wait for other io to complete */
224 for (i = 0; i < nblks; i++) {
225 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbp[i];
226 mutex_enter(&db->db_mtx);
227 while (db->db_state == DB_READ ||
228 db->db_state == DB_FILL)
229 cv_wait(&db->db_changed, &db->db_mtx);
230 if (db->db_state == DB_UNCACHED)
232 mutex_exit(&db->db_mtx);
234 dmu_buf_rele_array(dbp, nblks, tag);
246 dmu_buf_hold_array(objset_t *os, uint64_t object, uint64_t offset,
247 uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp)
252 err = dnode_hold(os->os, object, FTAG, &dn);
256 err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag,
259 dnode_rele(dn, FTAG);
265 dmu_buf_hold_array_by_bonus(dmu_buf_t *db, uint64_t offset,
266 uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp)
268 dnode_t *dn = ((dmu_buf_impl_t *)db)->db_dnode;
271 err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag,
278 dmu_buf_rele_array(dmu_buf_t **dbp_fake, int numbufs, void *tag)
281 dmu_buf_impl_t **dbp = (dmu_buf_impl_t **)dbp_fake;
286 for (i = 0; i < numbufs; i++) {
288 dbuf_rele(dbp[i], tag);
291 kmem_free(dbp, sizeof (dmu_buf_t *) * numbufs);
295 dmu_prefetch(objset_t *os, uint64_t object, uint64_t offset, uint64_t len)
301 if (zfs_prefetch_disable)
304 if (len == 0) { /* they're interested in the bonus buffer */
305 dn = os->os->os_meta_dnode;
307 if (object == 0 || object >= DN_MAX_OBJECT)
310 rw_enter(&dn->dn_struct_rwlock, RW_READER);
311 blkid = dbuf_whichblock(dn, object * sizeof (dnode_phys_t));
312 dbuf_prefetch(dn, blkid);
313 rw_exit(&dn->dn_struct_rwlock);
318 * XXX - Note, if the dnode for the requested object is not
319 * already cached, we will do a *synchronous* read in the
320 * dnode_hold() call. The same is true for any indirects.
322 err = dnode_hold(os->os, object, FTAG, &dn);
326 rw_enter(&dn->dn_struct_rwlock, RW_READER);
327 if (dn->dn_datablkshift) {
328 int blkshift = dn->dn_datablkshift;
329 nblks = (P2ROUNDUP(offset+len, 1<<blkshift) -
330 P2ALIGN(offset, 1<<blkshift)) >> blkshift;
332 nblks = (offset < dn->dn_datablksz);
336 blkid = dbuf_whichblock(dn, offset);
337 for (i = 0; i < nblks; i++)
338 dbuf_prefetch(dn, blkid+i);
341 rw_exit(&dn->dn_struct_rwlock);
343 dnode_rele(dn, FTAG);
347 dmu_free_range(objset_t *os, uint64_t object, uint64_t offset,
348 uint64_t size, dmu_tx_t *tx)
351 int err = dnode_hold(os->os, object, FTAG, &dn);
354 ASSERT(offset < UINT64_MAX);
355 ASSERT(size == -1ULL || size <= UINT64_MAX - offset);
356 dnode_free_range(dn, offset, size, tx);
357 dnode_rele(dn, FTAG);
362 dmu_read(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
369 err = dnode_hold(os->os, object, FTAG, &dn);
374 * Deal with odd block sizes, where there can't be data past the first
375 * block. If we ever do the tail block optimization, we will need to
376 * handle that here as well.
378 if (dn->dn_datablkshift == 0) {
379 int newsz = offset > dn->dn_datablksz ? 0 :
380 MIN(size, dn->dn_datablksz - offset);
381 bzero((char *)buf + newsz, size - newsz);
386 uint64_t mylen = MIN(size, DMU_MAX_ACCESS / 2);
390 * NB: we could do this block-at-a-time, but it's nice
391 * to be reading in parallel.
393 err = dmu_buf_hold_array_by_dnode(dn, offset, mylen,
394 TRUE, FTAG, &numbufs, &dbp);
398 for (i = 0; i < numbufs; i++) {
401 dmu_buf_t *db = dbp[i];
405 bufoff = offset - db->db_offset;
406 tocpy = (int)MIN(db->db_size - bufoff, size);
408 bcopy((char *)db->db_data + bufoff, buf, tocpy);
412 buf = (char *)buf + tocpy;
414 dmu_buf_rele_array(dbp, numbufs, FTAG);
416 dnode_rele(dn, FTAG);
421 dmu_write(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
422 const void *buf, dmu_tx_t *tx)
430 VERIFY(0 == dmu_buf_hold_array(os, object, offset, size,
431 FALSE, FTAG, &numbufs, &dbp));
433 for (i = 0; i < numbufs; i++) {
436 dmu_buf_t *db = dbp[i];
440 bufoff = offset - db->db_offset;
441 tocpy = (int)MIN(db->db_size - bufoff, size);
443 ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
445 if (tocpy == db->db_size)
446 dmu_buf_will_fill(db, tx);
448 dmu_buf_will_dirty(db, tx);
450 bcopy(buf, (char *)db->db_data + bufoff, tocpy);
452 if (tocpy == db->db_size)
453 dmu_buf_fill_done(db, tx);
457 buf = (char *)buf + tocpy;
459 dmu_buf_rele_array(dbp, numbufs, FTAG);
464 dmu_read_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size)
470 * NB: we could do this block-at-a-time, but it's nice
471 * to be reading in parallel.
473 err = dmu_buf_hold_array(os, object, uio->uio_loffset, size, TRUE, FTAG,
478 for (i = 0; i < numbufs; i++) {
481 dmu_buf_t *db = dbp[i];
485 bufoff = uio->uio_loffset - db->db_offset;
486 tocpy = (int)MIN(db->db_size - bufoff, size);
488 err = uiomove((char *)db->db_data + bufoff, tocpy,
495 dmu_buf_rele_array(dbp, numbufs, FTAG);
501 dmu_write_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size,
511 err = dmu_buf_hold_array(os, object, uio->uio_loffset, size,
512 FALSE, FTAG, &numbufs, &dbp);
516 for (i = 0; i < numbufs; i++) {
519 dmu_buf_t *db = dbp[i];
523 bufoff = uio->uio_loffset - db->db_offset;
524 tocpy = (int)MIN(db->db_size - bufoff, size);
526 ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
528 if (tocpy == db->db_size)
529 dmu_buf_will_fill(db, tx);
531 dmu_buf_will_dirty(db, tx);
534 * XXX uiomove could block forever (eg. nfs-backed
535 * pages). There needs to be a uiolockdown() function
536 * to lock the pages in memory, so that uiomove won't
539 err = uiomove((char *)db->db_data + bufoff, tocpy,
542 if (tocpy == db->db_size)
543 dmu_buf_fill_done(db, tx);
550 dmu_buf_rele_array(dbp, numbufs, FTAG);
556 dmu_write_pages(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
557 page_t *pp, dmu_tx_t *tx)
566 err = dmu_buf_hold_array(os, object, offset, size,
567 FALSE, FTAG, &numbufs, &dbp);
571 for (i = 0; i < numbufs; i++) {
572 int tocpy, copied, thiscpy;
574 dmu_buf_t *db = dbp[i];
578 ASSERT3U(db->db_size, >=, PAGESIZE);
580 bufoff = offset - db->db_offset;
581 tocpy = (int)MIN(db->db_size - bufoff, size);
583 ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
585 if (tocpy == db->db_size)
586 dmu_buf_will_fill(db, tx);
588 dmu_buf_will_dirty(db, tx);
590 for (copied = 0; copied < tocpy; copied += PAGESIZE) {
591 ASSERT3U(pp->p_offset, ==, db->db_offset + bufoff);
592 thiscpy = MIN(PAGESIZE, tocpy - copied);
593 va = ppmapin(pp, PROT_READ, (caddr_t)-1);
594 bcopy(va, (char *)db->db_data + bufoff, thiscpy);
600 if (tocpy == db->db_size)
601 dmu_buf_fill_done(db, tx);
609 dmu_buf_rele_array(dbp, numbufs, FTAG);
612 #endif /* !__FreeBSD__ */
616 dbuf_dirty_record_t *dr;
623 dmu_sync_done(zio_t *zio, arc_buf_t *buf, void *varg)
625 dmu_sync_arg_t *in = varg;
626 dbuf_dirty_record_t *dr = in->dr;
627 dmu_buf_impl_t *db = dr->dr_dbuf;
628 dmu_sync_cb_t *done = in->done;
630 if (!BP_IS_HOLE(zio->io_bp)) {
631 zio->io_bp->blk_fill = 1;
632 BP_SET_TYPE(zio->io_bp, db->db_dnode->dn_type);
633 BP_SET_LEVEL(zio->io_bp, 0);
636 mutex_enter(&db->db_mtx);
637 ASSERT(dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC);
638 dr->dt.dl.dr_overridden_by = *zio->io_bp; /* structure assignment */
639 dr->dt.dl.dr_override_state = DR_OVERRIDDEN;
640 cv_broadcast(&db->db_changed);
641 mutex_exit(&db->db_mtx);
644 done(&(db->db), in->arg);
646 kmem_free(in, sizeof (dmu_sync_arg_t));
650 * Intent log support: sync the block associated with db to disk.
651 * N.B. and XXX: the caller is responsible for making sure that the
652 * data isn't changing while dmu_sync() is writing it.
656 * EEXIST: this txg has already been synced, so there's nothing to to.
657 * The caller should not log the write.
659 * ENOENT: the block was dbuf_free_range()'d, so there's nothing to do.
660 * The caller should not log the write.
662 * EALREADY: this block is already in the process of being synced.
663 * The caller should track its progress (somehow).
665 * EINPROGRESS: the IO has been initiated.
666 * The caller should log this blkptr in the callback.
668 * 0: completed. Sets *bp to the blkptr just written.
669 * The caller should log this blkptr immediately.
672 dmu_sync(zio_t *pio, dmu_buf_t *db_fake,
673 blkptr_t *bp, uint64_t txg, dmu_sync_cb_t *done, void *arg)
675 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
676 objset_impl_t *os = db->db_objset;
677 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
678 tx_state_t *tx = &dp->dp_tx;
679 dbuf_dirty_record_t *dr;
686 ASSERT(BP_IS_HOLE(bp));
690 dprintf("dmu_sync txg=%llu, s,o,q %llu %llu %llu\n",
691 txg, tx->tx_synced_txg, tx->tx_open_txg, tx->tx_quiesced_txg);
694 * XXX - would be nice if we could do this without suspending...
699 * If this txg already synced, there's nothing to do.
701 if (txg <= tx->tx_synced_txg) {
704 * If we're running ziltest, we need the blkptr regardless.
706 if (txg > spa_freeze_txg(dp->dp_spa)) {
707 /* if db_blkptr == NULL, this was an empty write */
709 *bp = *db->db_blkptr; /* structure assignment */
715 mutex_enter(&db->db_mtx);
717 if (txg == tx->tx_syncing_txg) {
718 while (db->db_data_pending) {
720 * IO is in-progress. Wait for it to finish.
721 * XXX - would be nice to be able to somehow "attach"
722 * this zio to the parent zio passed in.
724 cv_wait(&db->db_changed, &db->db_mtx);
725 if (!db->db_data_pending &&
726 db->db_blkptr && BP_IS_HOLE(db->db_blkptr)) {
728 * IO was compressed away
730 *bp = *db->db_blkptr; /* structure assignment */
731 mutex_exit(&db->db_mtx);
735 ASSERT(db->db_data_pending ||
736 (db->db_blkptr && db->db_blkptr->blk_birth == txg));
739 if (db->db_blkptr && db->db_blkptr->blk_birth == txg) {
741 * IO is already completed.
743 *bp = *db->db_blkptr; /* structure assignment */
744 mutex_exit(&db->db_mtx);
750 dr = db->db_last_dirty;
751 while (dr && dr->dr_txg > txg)
753 if (dr == NULL || dr->dr_txg < txg) {
755 * This dbuf isn't dirty, must have been free_range'd.
756 * There's no need to log writes to freed blocks, so we're done.
758 mutex_exit(&db->db_mtx);
763 ASSERT(dr->dr_txg == txg);
764 if (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
766 * We have already issued a sync write for this buffer.
768 mutex_exit(&db->db_mtx);
771 } else if (dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
773 * This buffer has already been synced. It could not
774 * have been dirtied since, or we would have cleared the state.
776 *bp = dr->dt.dl.dr_overridden_by; /* structure assignment */
777 mutex_exit(&db->db_mtx);
782 dr->dt.dl.dr_override_state = DR_IN_DMU_SYNC;
783 in = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP);
787 mutex_exit(&db->db_mtx);
790 zb.zb_objset = os->os_dsl_dataset->ds_object;
791 zb.zb_object = db->db.db_object;
792 zb.zb_level = db->db_level;
793 zb.zb_blkid = db->db_blkid;
794 zio_flags = ZIO_FLAG_MUSTSUCCEED;
795 if (dmu_ot[db->db_dnode->dn_type].ot_metadata || zb.zb_level != 0)
796 zio_flags |= ZIO_FLAG_METADATA;
797 zio = arc_write(pio, os->os_spa,
798 zio_checksum_select(db->db_dnode->dn_checksum, os->os_checksum),
799 zio_compress_select(db->db_dnode->dn_compress, os->os_compress),
800 dmu_get_replication_level(os, &zb, db->db_dnode->dn_type),
801 txg, bp, dr->dt.dl.dr_data, NULL, dmu_sync_done, in,
802 ZIO_PRIORITY_SYNC_WRITE, zio_flags, &zb);
815 dmu_object_set_blocksize(objset_t *os, uint64_t object, uint64_t size, int ibs,
821 err = dnode_hold(os->os, object, FTAG, &dn);
824 err = dnode_set_blksz(dn, size, ibs, tx);
825 dnode_rele(dn, FTAG);
830 dmu_object_set_checksum(objset_t *os, uint64_t object, uint8_t checksum,
835 /* XXX assumes dnode_hold will not get an i/o error */
836 (void) dnode_hold(os->os, object, FTAG, &dn);
837 ASSERT(checksum < ZIO_CHECKSUM_FUNCTIONS);
838 dn->dn_checksum = checksum;
839 dnode_setdirty(dn, tx);
840 dnode_rele(dn, FTAG);
844 dmu_object_set_compress(objset_t *os, uint64_t object, uint8_t compress,
849 /* XXX assumes dnode_hold will not get an i/o error */
850 (void) dnode_hold(os->os, object, FTAG, &dn);
851 ASSERT(compress < ZIO_COMPRESS_FUNCTIONS);
852 dn->dn_compress = compress;
853 dnode_setdirty(dn, tx);
854 dnode_rele(dn, FTAG);
858 dmu_get_replication_level(objset_impl_t *os,
859 zbookmark_t *zb, dmu_object_type_t ot)
861 int ncopies = os->os_copies;
863 /* If it's the mos, it should have max copies set. */
864 ASSERT(zb->zb_objset != 0 ||
865 ncopies == spa_max_replication(os->os_spa));
867 if (dmu_ot[ot].ot_metadata || zb->zb_level != 0)
869 return (MIN(ncopies, spa_max_replication(os->os_spa)));
873 dmu_offset_next(objset_t *os, uint64_t object, boolean_t hole, uint64_t *off)
878 err = dnode_hold(os->os, object, FTAG, &dn);
882 * Sync any current changes before
883 * we go trundling through the block pointers.
885 for (i = 0; i < TXG_SIZE; i++) {
886 if (list_link_active(&dn->dn_dirty_link[i]))
890 dnode_rele(dn, FTAG);
891 txg_wait_synced(dmu_objset_pool(os), 0);
892 err = dnode_hold(os->os, object, FTAG, &dn);
897 err = dnode_next_offset(dn, hole, off, 1, 1, 0);
898 dnode_rele(dn, FTAG);
904 dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi)
906 rw_enter(&dn->dn_struct_rwlock, RW_READER);
907 mutex_enter(&dn->dn_mtx);
909 doi->doi_data_block_size = dn->dn_datablksz;
910 doi->doi_metadata_block_size = dn->dn_indblkshift ?
911 1ULL << dn->dn_indblkshift : 0;
912 doi->doi_indirection = dn->dn_nlevels;
913 doi->doi_checksum = dn->dn_checksum;
914 doi->doi_compress = dn->dn_compress;
915 doi->doi_physical_blks = (DN_USED_BYTES(dn->dn_phys) +
916 SPA_MINBLOCKSIZE/2) >> SPA_MINBLOCKSHIFT;
917 doi->doi_max_block_offset = dn->dn_phys->dn_maxblkid;
918 doi->doi_type = dn->dn_type;
919 doi->doi_bonus_size = dn->dn_bonuslen;
920 doi->doi_bonus_type = dn->dn_bonustype;
922 mutex_exit(&dn->dn_mtx);
923 rw_exit(&dn->dn_struct_rwlock);
927 * Get information on a DMU object.
928 * If doi is NULL, just indicates whether the object exists.
931 dmu_object_info(objset_t *os, uint64_t object, dmu_object_info_t *doi)
934 int err = dnode_hold(os->os, object, FTAG, &dn);
940 dmu_object_info_from_dnode(dn, doi);
942 dnode_rele(dn, FTAG);
947 * As above, but faster; can be used when you have a held dbuf in hand.
950 dmu_object_info_from_db(dmu_buf_t *db, dmu_object_info_t *doi)
952 dmu_object_info_from_dnode(((dmu_buf_impl_t *)db)->db_dnode, doi);
956 * Faster still when you only care about the size.
957 * This is specifically optimized for zfs_getattr().
960 dmu_object_size_from_db(dmu_buf_t *db, uint32_t *blksize, u_longlong_t *nblk512)
962 dnode_t *dn = ((dmu_buf_impl_t *)db)->db_dnode;
964 *blksize = dn->dn_datablksz;
965 /* add 1 for dnode space */
966 *nblk512 = ((DN_USED_BYTES(dn->dn_phys) + SPA_MINBLOCKSIZE/2) >>
967 SPA_MINBLOCKSHIFT) + 1;
971 byteswap_uint64_array(void *vbuf, size_t size)
973 uint64_t *buf = vbuf;
974 size_t count = size >> 3;
977 ASSERT((size & 7) == 0);
979 for (i = 0; i < count; i++)
980 buf[i] = BSWAP_64(buf[i]);
984 byteswap_uint32_array(void *vbuf, size_t size)
986 uint32_t *buf = vbuf;
987 size_t count = size >> 2;
990 ASSERT((size & 3) == 0);
992 for (i = 0; i < count; i++)
993 buf[i] = BSWAP_32(buf[i]);
997 byteswap_uint16_array(void *vbuf, size_t size)
999 uint16_t *buf = vbuf;
1000 size_t count = size >> 1;
1003 ASSERT((size & 1) == 0);
1005 for (i = 0; i < count; i++)
1006 buf[i] = BSWAP_16(buf[i]);
1011 byteswap_uint8_array(void *vbuf, size_t size)