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 the next "chunk" of file data to free. We traverse the file from
376 * the end so that the file gets shorter over time (if we crashes in the
377 * middle, this will leave us in a better state). We find allocated file
378 * data by simply searching the allocated level 1 indirects.
381 get_next_chunk(dnode_t *dn, uint64_t *start, uint64_t limit)
383 uint64_t len = *start - limit;
385 uint64_t maxblks = DMU_MAX_ACCESS / (1ULL << (dn->dn_indblkshift + 1));
387 dn->dn_datablksz * EPB(dn->dn_indblkshift, SPA_BLKPTRSHIFT);
389 ASSERT(limit <= *start);
391 if (len <= iblkrange * maxblks) {
395 ASSERT(ISP2(iblkrange));
397 while (*start > limit && blkcnt < maxblks) {
400 /* find next allocated L1 indirect */
401 err = dnode_next_offset(dn,
402 DNODE_FIND_BACKWARDS, start, 2, 1, 0);
404 /* if there are no more, then we are done */
413 /* reset offset to end of "next" block back */
414 *start = P2ALIGN(*start, iblkrange);
424 dmu_free_long_range_impl(objset_t *os, dnode_t *dn, uint64_t offset,
425 uint64_t length, boolean_t free_dnode)
428 uint64_t object_size, start, end, len;
429 boolean_t trunc = (length == DMU_OBJECT_END);
432 align = 1 << dn->dn_datablkshift;
434 object_size = align == 1 ? dn->dn_datablksz :
435 (dn->dn_maxblkid + 1) << dn->dn_datablkshift;
437 if (trunc || (end = offset + length) > object_size)
441 length = end - offset;
445 err = get_next_chunk(dn, &start, offset);
448 len = trunc ? DMU_OBJECT_END : end - start;
450 tx = dmu_tx_create(os);
451 dmu_tx_hold_free(tx, dn->dn_object, start, len);
452 err = dmu_tx_assign(tx, TXG_WAIT);
458 dnode_free_range(dn, start, trunc ? -1 : len, tx);
460 if (start == 0 && free_dnode) {
465 length -= end - start;
474 dmu_free_long_range(objset_t *os, uint64_t object,
475 uint64_t offset, uint64_t length)
480 err = dnode_hold(os->os, object, FTAG, &dn);
483 err = dmu_free_long_range_impl(os, dn, offset, length, FALSE);
484 dnode_rele(dn, FTAG);
489 dmu_free_object(objset_t *os, uint64_t object)
495 err = dnode_hold_impl(os->os, object, DNODE_MUST_BE_ALLOCATED,
499 if (dn->dn_nlevels == 1) {
500 tx = dmu_tx_create(os);
501 dmu_tx_hold_bonus(tx, object);
502 dmu_tx_hold_free(tx, dn->dn_object, 0, DMU_OBJECT_END);
503 err = dmu_tx_assign(tx, TXG_WAIT);
505 dnode_free_range(dn, 0, DMU_OBJECT_END, tx);
512 err = dmu_free_long_range_impl(os, dn, 0, DMU_OBJECT_END, TRUE);
514 dnode_rele(dn, FTAG);
519 dmu_free_range(objset_t *os, uint64_t object, uint64_t offset,
520 uint64_t size, dmu_tx_t *tx)
523 int err = dnode_hold(os->os, object, FTAG, &dn);
526 ASSERT(offset < UINT64_MAX);
527 ASSERT(size == -1ULL || size <= UINT64_MAX - offset);
528 dnode_free_range(dn, offset, size, tx);
529 dnode_rele(dn, FTAG);
534 dmu_read(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
541 err = dnode_hold(os->os, object, FTAG, &dn);
546 * Deal with odd block sizes, where there can't be data past the first
547 * block. If we ever do the tail block optimization, we will need to
548 * handle that here as well.
550 if (dn->dn_datablkshift == 0) {
551 int newsz = offset > dn->dn_datablksz ? 0 :
552 MIN(size, dn->dn_datablksz - offset);
553 bzero((char *)buf + newsz, size - newsz);
558 uint64_t mylen = MIN(size, DMU_MAX_ACCESS / 2);
561 * NB: we could do this block-at-a-time, but it's nice
562 * to be reading in parallel.
564 err = dmu_buf_hold_array_by_dnode(dn, offset, mylen,
565 TRUE, FTAG, &numbufs, &dbp);
569 for (i = 0; i < numbufs; i++) {
572 dmu_buf_t *db = dbp[i];
576 bufoff = offset - db->db_offset;
577 tocpy = (int)MIN(db->db_size - bufoff, size);
579 bcopy((char *)db->db_data + bufoff, buf, tocpy);
583 buf = (char *)buf + tocpy;
585 dmu_buf_rele_array(dbp, numbufs, FTAG);
587 dnode_rele(dn, FTAG);
592 dmu_write(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
593 const void *buf, dmu_tx_t *tx)
601 VERIFY(0 == dmu_buf_hold_array(os, object, offset, size,
602 FALSE, FTAG, &numbufs, &dbp));
604 for (i = 0; i < numbufs; i++) {
607 dmu_buf_t *db = dbp[i];
611 bufoff = offset - db->db_offset;
612 tocpy = (int)MIN(db->db_size - bufoff, size);
614 ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
616 if (tocpy == db->db_size)
617 dmu_buf_will_fill(db, tx);
619 dmu_buf_will_dirty(db, tx);
621 bcopy(buf, (char *)db->db_data + bufoff, tocpy);
623 if (tocpy == db->db_size)
624 dmu_buf_fill_done(db, tx);
628 buf = (char *)buf + tocpy;
630 dmu_buf_rele_array(dbp, numbufs, FTAG);
635 dmu_read_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size)
641 * NB: we could do this block-at-a-time, but it's nice
642 * to be reading in parallel.
644 err = dmu_buf_hold_array(os, object, uio->uio_loffset, size, TRUE, FTAG,
649 for (i = 0; i < numbufs; i++) {
652 dmu_buf_t *db = dbp[i];
656 bufoff = uio->uio_loffset - db->db_offset;
657 tocpy = (int)MIN(db->db_size - bufoff, size);
659 err = uiomove((char *)db->db_data + bufoff, tocpy,
666 dmu_buf_rele_array(dbp, numbufs, FTAG);
672 dmu_write_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size,
682 err = dmu_buf_hold_array(os, object, uio->uio_loffset, size,
683 FALSE, FTAG, &numbufs, &dbp);
687 for (i = 0; i < numbufs; i++) {
690 dmu_buf_t *db = dbp[i];
694 bufoff = uio->uio_loffset - db->db_offset;
695 tocpy = (int)MIN(db->db_size - bufoff, size);
697 ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
699 if (tocpy == db->db_size)
700 dmu_buf_will_fill(db, tx);
702 dmu_buf_will_dirty(db, tx);
705 * XXX uiomove could block forever (eg. nfs-backed
706 * pages). There needs to be a uiolockdown() function
707 * to lock the pages in memory, so that uiomove won't
710 err = uiomove((char *)db->db_data + bufoff, tocpy,
713 if (tocpy == db->db_size)
714 dmu_buf_fill_done(db, tx);
721 dmu_buf_rele_array(dbp, numbufs, FTAG);
727 dmu_write_pages(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
728 page_t *pp, dmu_tx_t *tx)
737 err = dmu_buf_hold_array(os, object, offset, size,
738 FALSE, FTAG, &numbufs, &dbp);
742 for (i = 0; i < numbufs; i++) {
743 int tocpy, copied, thiscpy;
745 dmu_buf_t *db = dbp[i];
749 ASSERT3U(db->db_size, >=, PAGESIZE);
751 bufoff = offset - db->db_offset;
752 tocpy = (int)MIN(db->db_size - bufoff, size);
754 ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
756 if (tocpy == db->db_size)
757 dmu_buf_will_fill(db, tx);
759 dmu_buf_will_dirty(db, tx);
761 for (copied = 0; copied < tocpy; copied += PAGESIZE) {
762 ASSERT3U(pp->p_offset, ==, db->db_offset + bufoff);
763 thiscpy = MIN(PAGESIZE, tocpy - copied);
764 va = zfs_map_page(pp, S_READ);
765 bcopy(va, (char *)db->db_data + bufoff, thiscpy);
766 zfs_unmap_page(pp, va);
771 if (tocpy == db->db_size)
772 dmu_buf_fill_done(db, tx);
780 dmu_buf_rele_array(dbp, numbufs, FTAG);
783 #endif /* !__FreeBSD__ */
787 dbuf_dirty_record_t *dr;
794 dmu_sync_ready(zio_t *zio, arc_buf_t *buf, void *varg)
796 blkptr_t *bp = zio->io_bp;
798 if (!BP_IS_HOLE(bp)) {
799 dmu_sync_arg_t *in = varg;
800 dbuf_dirty_record_t *dr = in->dr;
801 dmu_buf_impl_t *db = dr->dr_dbuf;
802 ASSERT(BP_GET_TYPE(bp) == db->db_dnode->dn_type);
803 ASSERT(BP_GET_LEVEL(bp) == 0);
810 dmu_sync_done(zio_t *zio, arc_buf_t *buf, void *varg)
812 dmu_sync_arg_t *in = varg;
813 dbuf_dirty_record_t *dr = in->dr;
814 dmu_buf_impl_t *db = dr->dr_dbuf;
815 dmu_sync_cb_t *done = in->done;
817 mutex_enter(&db->db_mtx);
818 ASSERT(dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC);
819 dr->dt.dl.dr_overridden_by = *zio->io_bp; /* structure assignment */
820 dr->dt.dl.dr_override_state = DR_OVERRIDDEN;
821 cv_broadcast(&db->db_changed);
822 mutex_exit(&db->db_mtx);
825 done(&(db->db), in->arg);
827 kmem_free(in, sizeof (dmu_sync_arg_t));
831 * Intent log support: sync the block associated with db to disk.
832 * N.B. and XXX: the caller is responsible for making sure that the
833 * data isn't changing while dmu_sync() is writing it.
837 * EEXIST: this txg has already been synced, so there's nothing to to.
838 * The caller should not log the write.
840 * ENOENT: the block was dbuf_free_range()'d, so there's nothing to do.
841 * The caller should not log the write.
843 * EALREADY: this block is already in the process of being synced.
844 * The caller should track its progress (somehow).
846 * EINPROGRESS: the IO has been initiated.
847 * The caller should log this blkptr in the callback.
849 * 0: completed. Sets *bp to the blkptr just written.
850 * The caller should log this blkptr immediately.
853 dmu_sync(zio_t *pio, dmu_buf_t *db_fake,
854 blkptr_t *bp, uint64_t txg, dmu_sync_cb_t *done, void *arg)
856 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
857 objset_impl_t *os = db->db_objset;
858 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
859 tx_state_t *tx = &dp->dp_tx;
860 dbuf_dirty_record_t *dr;
863 writeprops_t wp = { 0 };
867 ASSERT(BP_IS_HOLE(bp));
870 dprintf("dmu_sync txg=%llu, s,o,q %llu %llu %llu\n",
871 txg, tx->tx_synced_txg, tx->tx_open_txg, tx->tx_quiesced_txg);
874 * XXX - would be nice if we could do this without suspending...
879 * If this txg already synced, there's nothing to do.
881 if (txg <= tx->tx_synced_txg) {
884 * If we're running ziltest, we need the blkptr regardless.
886 if (txg > spa_freeze_txg(dp->dp_spa)) {
887 /* if db_blkptr == NULL, this was an empty write */
889 *bp = *db->db_blkptr; /* structure assignment */
895 mutex_enter(&db->db_mtx);
897 if (txg == tx->tx_syncing_txg) {
898 while (db->db_data_pending) {
900 * IO is in-progress. Wait for it to finish.
901 * XXX - would be nice to be able to somehow "attach"
902 * this zio to the parent zio passed in.
904 cv_wait(&db->db_changed, &db->db_mtx);
905 if (!db->db_data_pending &&
906 db->db_blkptr && BP_IS_HOLE(db->db_blkptr)) {
908 * IO was compressed away
910 *bp = *db->db_blkptr; /* structure assignment */
911 mutex_exit(&db->db_mtx);
915 ASSERT(db->db_data_pending ||
916 (db->db_blkptr && db->db_blkptr->blk_birth == txg));
919 if (db->db_blkptr && db->db_blkptr->blk_birth == txg) {
921 * IO is already completed.
923 *bp = *db->db_blkptr; /* structure assignment */
924 mutex_exit(&db->db_mtx);
930 dr = db->db_last_dirty;
931 while (dr && dr->dr_txg > txg)
933 if (dr == NULL || dr->dr_txg < txg) {
935 * This dbuf isn't dirty, must have been free_range'd.
936 * There's no need to log writes to freed blocks, so we're done.
938 mutex_exit(&db->db_mtx);
943 ASSERT(dr->dr_txg == txg);
944 if (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
946 * We have already issued a sync write for this buffer.
948 mutex_exit(&db->db_mtx);
951 } else if (dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
953 * This buffer has already been synced. It could not
954 * have been dirtied since, or we would have cleared the state.
956 *bp = dr->dt.dl.dr_overridden_by; /* structure assignment */
957 mutex_exit(&db->db_mtx);
962 dr->dt.dl.dr_override_state = DR_IN_DMU_SYNC;
963 in = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP);
967 mutex_exit(&db->db_mtx);
970 zb.zb_objset = os->os_dsl_dataset->ds_object;
971 zb.zb_object = db->db.db_object;
972 zb.zb_level = db->db_level;
973 zb.zb_blkid = db->db_blkid;
975 wp.wp_type = db->db_dnode->dn_type;
976 wp.wp_level = db->db_level;
977 wp.wp_copies = os->os_copies;
978 wp.wp_dnchecksum = db->db_dnode->dn_checksum;
979 wp.wp_oschecksum = os->os_checksum;
980 wp.wp_dncompress = db->db_dnode->dn_compress;
981 wp.wp_oscompress = os->os_compress;
983 ASSERT(BP_IS_HOLE(bp));
985 zio = arc_write(pio, os->os_spa, &wp, DBUF_IS_L2CACHEABLE(db),
986 txg, bp, dr->dt.dl.dr_data, dmu_sync_ready, dmu_sync_done, in,
987 ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
1000 dmu_object_set_blocksize(objset_t *os, uint64_t object, uint64_t size, int ibs,
1006 err = dnode_hold(os->os, object, FTAG, &dn);
1009 err = dnode_set_blksz(dn, size, ibs, tx);
1010 dnode_rele(dn, FTAG);
1015 dmu_object_set_checksum(objset_t *os, uint64_t object, uint8_t checksum,
1020 /* XXX assumes dnode_hold will not get an i/o error */
1021 (void) dnode_hold(os->os, object, FTAG, &dn);
1022 ASSERT(checksum < ZIO_CHECKSUM_FUNCTIONS);
1023 dn->dn_checksum = checksum;
1024 dnode_setdirty(dn, tx);
1025 dnode_rele(dn, FTAG);
1029 dmu_object_set_compress(objset_t *os, uint64_t object, uint8_t compress,
1034 /* XXX assumes dnode_hold will not get an i/o error */
1035 (void) dnode_hold(os->os, object, FTAG, &dn);
1036 ASSERT(compress < ZIO_COMPRESS_FUNCTIONS);
1037 dn->dn_compress = compress;
1038 dnode_setdirty(dn, tx);
1039 dnode_rele(dn, FTAG);
1043 dmu_offset_next(objset_t *os, uint64_t object, boolean_t hole, uint64_t *off)
1048 err = dnode_hold(os->os, object, FTAG, &dn);
1052 * Sync any current changes before
1053 * we go trundling through the block pointers.
1055 for (i = 0; i < TXG_SIZE; i++) {
1056 if (list_link_active(&dn->dn_dirty_link[i]))
1059 if (i != TXG_SIZE) {
1060 dnode_rele(dn, FTAG);
1061 txg_wait_synced(dmu_objset_pool(os), 0);
1062 err = dnode_hold(os->os, object, FTAG, &dn);
1067 err = dnode_next_offset(dn, (hole ? DNODE_FIND_HOLE : 0), off, 1, 1, 0);
1068 dnode_rele(dn, FTAG);
1074 dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi)
1076 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1077 mutex_enter(&dn->dn_mtx);
1079 doi->doi_data_block_size = dn->dn_datablksz;
1080 doi->doi_metadata_block_size = dn->dn_indblkshift ?
1081 1ULL << dn->dn_indblkshift : 0;
1082 doi->doi_indirection = dn->dn_nlevels;
1083 doi->doi_checksum = dn->dn_checksum;
1084 doi->doi_compress = dn->dn_compress;
1085 doi->doi_physical_blks = (DN_USED_BYTES(dn->dn_phys) +
1086 SPA_MINBLOCKSIZE/2) >> SPA_MINBLOCKSHIFT;
1087 doi->doi_max_block_offset = dn->dn_phys->dn_maxblkid;
1088 doi->doi_type = dn->dn_type;
1089 doi->doi_bonus_size = dn->dn_bonuslen;
1090 doi->doi_bonus_type = dn->dn_bonustype;
1092 mutex_exit(&dn->dn_mtx);
1093 rw_exit(&dn->dn_struct_rwlock);
1097 * Get information on a DMU object.
1098 * If doi is NULL, just indicates whether the object exists.
1101 dmu_object_info(objset_t *os, uint64_t object, dmu_object_info_t *doi)
1104 int err = dnode_hold(os->os, object, FTAG, &dn);
1110 dmu_object_info_from_dnode(dn, doi);
1112 dnode_rele(dn, FTAG);
1117 * As above, but faster; can be used when you have a held dbuf in hand.
1120 dmu_object_info_from_db(dmu_buf_t *db, dmu_object_info_t *doi)
1122 dmu_object_info_from_dnode(((dmu_buf_impl_t *)db)->db_dnode, doi);
1126 * Faster still when you only care about the size.
1127 * This is specifically optimized for zfs_getattr().
1130 dmu_object_size_from_db(dmu_buf_t *db, uint32_t *blksize, u_longlong_t *nblk512)
1132 dnode_t *dn = ((dmu_buf_impl_t *)db)->db_dnode;
1134 *blksize = dn->dn_datablksz;
1135 /* add 1 for dnode space */
1136 *nblk512 = ((DN_USED_BYTES(dn->dn_phys) + SPA_MINBLOCKSIZE/2) >>
1137 SPA_MINBLOCKSHIFT) + 1;
1141 byteswap_uint64_array(void *vbuf, size_t size)
1143 uint64_t *buf = vbuf;
1144 size_t count = size >> 3;
1147 ASSERT((size & 7) == 0);
1149 for (i = 0; i < count; i++)
1150 buf[i] = BSWAP_64(buf[i]);
1154 byteswap_uint32_array(void *vbuf, size_t size)
1156 uint32_t *buf = vbuf;
1157 size_t count = size >> 2;
1160 ASSERT((size & 3) == 0);
1162 for (i = 0; i < count; i++)
1163 buf[i] = BSWAP_32(buf[i]);
1167 byteswap_uint16_array(void *vbuf, size_t size)
1169 uint16_t *buf = vbuf;
1170 size_t count = size >> 1;
1173 ASSERT((size & 1) == 0);
1175 for (i = 0; i < count; i++)
1176 buf[i] = BSWAP_16(buf[i]);
1181 byteswap_uint8_array(void *vbuf, size_t size)