dmu_tx.c revision 715614a4137a6d2100ff60821e21cabc22d06088
9ec65cbae2f760e485a1c54df5b19853688d5c91wrowe/*
19d8729755d7f4d9503029a628dacbbdabcd2264wrowe * CDDL HEADER START
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding *
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding * The contents of this file are subject to the terms of the
8cc01204a878dc5f39bba11aa279564b81b15803wrowe * Common Development and Distribution License (the "License").
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding * You may not use this file except in compliance with the License.
9ec65cbae2f760e485a1c54df5b19853688d5c91wrowe *
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding * or http://www.opensolaris.org/os/licensing.
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding * See the License for the specific language governing permissions
9ec65cbae2f760e485a1c54df5b19853688d5c91wrowe * and limitations under the License.
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding *
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding * When distributing Covered Code, include this CDDL HEADER in each
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding * If applicable, add the following below this CDDL HEADER, with the
9ec65cbae2f760e485a1c54df5b19853688d5c91wrowe * fields enclosed by brackets "[]" replaced with your own identifying
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding * information: Portions Copyright [yyyy] [name of copyright owner]
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding *
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding * CDDL HEADER END
9ec65cbae2f760e485a1c54df5b19853688d5c91wrowe */
9ec65cbae2f760e485a1c54df5b19853688d5c91wrowe/*
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding * Use is subject to license terms.
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding */
19d8729755d7f4d9503029a628dacbbdabcd2264wrowe
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding#pragma ident "%Z%%M% %I% %E% SMI"
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding#include <sys/dmu.h>
577d813165ace257dd6f3bee143e5413ea1a16acstoddard#include <sys/dmu_impl.h>
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding#include <sys/dbuf.h>
9ec65cbae2f760e485a1c54df5b19853688d5c91wrowe#include <sys/dmu_tx.h>
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding#include <sys/dmu_objset.h>
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding#include <sys/dsl_dataset.h> /* for dsl_dataset_block_freeable() */
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding#include <sys/dsl_dir.h> /* for dsl_dir_tempreserve_*() */
8cc01204a878dc5f39bba11aa279564b81b15803wrowe#include <sys/dsl_pool.h>
8cc01204a878dc5f39bba11aa279564b81b15803wrowe#include <sys/zap_impl.h> /* for fzap_default_block_shift */
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding#include <sys/spa.h>
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding#include <sys/zfs_context.h>
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding
8cc01204a878dc5f39bba11aa279564b81b15803wrowetypedef void (*dmu_tx_hold_func_t)(dmu_tx_t *tx, struct dnode *dn,
8cc01204a878dc5f39bba11aa279564b81b15803wrowe uint64_t arg1, uint64_t arg2);
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding
9ec65cbae2f760e485a1c54df5b19853688d5c91wrowe
9ec65cbae2f760e485a1c54df5b19853688d5c91wrowedmu_tx_t *
577d813165ace257dd6f3bee143e5413ea1a16acstoddarddmu_tx_create_dd(dsl_dir_t *dd)
577d813165ace257dd6f3bee143e5413ea1a16acstoddard{
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding dmu_tx_t *tx = kmem_zalloc(sizeof (dmu_tx_t), KM_SLEEP);
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding tx->tx_dir = dd;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding if (dd)
8cc01204a878dc5f39bba11aa279564b81b15803wrowe tx->tx_pool = dd->dd_pool;
ee72e79784a32861da1bef9d7e75f09bcbb9de5cwrowe list_create(&tx->tx_holds, sizeof (dmu_tx_hold_t),
ee72e79784a32861da1bef9d7e75f09bcbb9de5cwrowe offsetof(dmu_tx_hold_t, txh_node));
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding#ifdef ZFS_DEBUG
9ec65cbae2f760e485a1c54df5b19853688d5c91wrowe refcount_create(&tx->tx_space_written);
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding refcount_create(&tx->tx_space_freed);
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding#endif
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding return (tx);
8cc01204a878dc5f39bba11aa279564b81b15803wrowe}
8cc01204a878dc5f39bba11aa279564b81b15803wrowe
f3220f54126b25e1cf93cc26c17177b7aef850fdfieldingdmu_tx_t *
f3220f54126b25e1cf93cc26c17177b7aef850fdfieldingdmu_tx_create(objset_t *os)
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding{
8cc01204a878dc5f39bba11aa279564b81b15803wrowe dmu_tx_t *tx = dmu_tx_create_dd(os->os->os_dsl_dataset->ds_dir);
8cc01204a878dc5f39bba11aa279564b81b15803wrowe tx->tx_objset = os;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding tx->tx_lastsnap_txg = dsl_dataset_prev_snap_txg(os->os->os_dsl_dataset);
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding return (tx);
19d8729755d7f4d9503029a628dacbbdabcd2264wrowe}
19d8729755d7f4d9503029a628dacbbdabcd2264wrowe
577d813165ace257dd6f3bee143e5413ea1a16acstoddarddmu_tx_t *
577d813165ace257dd6f3bee143e5413ea1a16acstoddarddmu_tx_create_assigned(struct dsl_pool *dp, uint64_t txg)
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding{
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding dmu_tx_t *tx = dmu_tx_create_dd(NULL);
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding
8cc01204a878dc5f39bba11aa279564b81b15803wrowe ASSERT3U(txg, <=, dp->dp_tx.tx_open_txg);
ee72e79784a32861da1bef9d7e75f09bcbb9de5cwrowe tx->tx_pool = dp;
ee72e79784a32861da1bef9d7e75f09bcbb9de5cwrowe tx->tx_txg = txg;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding tx->tx_anyobj = TRUE;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding return (tx);
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding}
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding
9ec65cbae2f760e485a1c54df5b19853688d5c91wroweint
9ec65cbae2f760e485a1c54df5b19853688d5c91wrowedmu_tx_is_syncing(dmu_tx_t *tx)
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe{
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding return (tx->tx_anyobj);
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding}
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding
f3220f54126b25e1cf93cc26c17177b7aef850fdfieldingint
4099f9c1ec0ad7d2423208089d87592c2846486dwrowedmu_tx_private_ok(dmu_tx_t *tx)
be8e3c3ad9c5d87951a803b632dccde0a4aba35cwrowe{
be8e3c3ad9c5d87951a803b632dccde0a4aba35cwrowe return (tx->tx_anyobj);
be8e3c3ad9c5d87951a803b632dccde0a4aba35cwrowe}
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe
37e3f7ccfb0862f3a7626681d0b1d01a3650162bstoddardstatic dmu_tx_hold_t *
37e3f7ccfb0862f3a7626681d0b1d01a3650162bstoddarddmu_tx_hold_object_impl(dmu_tx_t *tx, objset_t *os, uint64_t object,
37e3f7ccfb0862f3a7626681d0b1d01a3650162bstoddard enum dmu_tx_hold_type type, uint64_t arg1, uint64_t arg2)
9ec65cbae2f760e485a1c54df5b19853688d5c91wrowe{
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe dmu_tx_hold_t *txh;
37e3f7ccfb0862f3a7626681d0b1d01a3650162bstoddard dnode_t *dn = NULL;
37e3f7ccfb0862f3a7626681d0b1d01a3650162bstoddard int err;
37e3f7ccfb0862f3a7626681d0b1d01a3650162bstoddard
f8550f1fac4de016784cd90d480d2afa910eca53stoddard if (object != DMU_NEW_OBJECT) {
42004749288a35c7d87ff664855138469c3c399bstoddard err = dnode_hold(os->os, object, tx, &dn);
42004749288a35c7d87ff664855138469c3c399bstoddard if (err) {
42004749288a35c7d87ff664855138469c3c399bstoddard tx->tx_err = err;
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe return (NULL);
38a99ca79b5af0b58ec887f4a449dbb76f900fafwrowe }
38a99ca79b5af0b58ec887f4a449dbb76f900fafwrowe
38a99ca79b5af0b58ec887f4a449dbb76f900fafwrowe if (err == 0 && tx->tx_txg != 0) {
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe mutex_enter(&dn->dn_mtx);
37e3f7ccfb0862f3a7626681d0b1d01a3650162bstoddard /*
37e3f7ccfb0862f3a7626681d0b1d01a3650162bstoddard * dn->dn_assigned_txg == tx->tx_txg doesn't pose a
37e3f7ccfb0862f3a7626681d0b1d01a3650162bstoddard * problem, but there's no way for it to happen (for
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe * now, at least).
37e3f7ccfb0862f3a7626681d0b1d01a3650162bstoddard */
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe ASSERT(dn->dn_assigned_txg == 0);
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe dn->dn_assigned_txg = tx->tx_txg;
37e3f7ccfb0862f3a7626681d0b1d01a3650162bstoddard (void) refcount_add(&dn->dn_tx_holds, tx);
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe mutex_exit(&dn->dn_mtx);
dcd2219e426b64c69306a609cd8261f11283f9e9stoddard }
dcd2219e426b64c69306a609cd8261f11283f9e9stoddard }
990e1969a428b8844e07aad088df41340cd009d4wrowe
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe txh = kmem_zalloc(sizeof (dmu_tx_hold_t), KM_SLEEP);
9ec65cbae2f760e485a1c54df5b19853688d5c91wrowe txh->txh_tx = tx;
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe txh->txh_dnode = dn;
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe#ifdef ZFS_DEBUG
990e1969a428b8844e07aad088df41340cd009d4wrowe txh->txh_type = type;
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe txh->txh_arg1 = arg1;
990e1969a428b8844e07aad088df41340cd009d4wrowe txh->txh_arg2 = arg2;
990e1969a428b8844e07aad088df41340cd009d4wrowe#endif
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe list_insert_tail(&tx->tx_holds, txh);
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe return (txh);
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe}
9ec65cbae2f760e485a1c54df5b19853688d5c91wrowe
4099f9c1ec0ad7d2423208089d87592c2846486dwrowevoid
4099f9c1ec0ad7d2423208089d87592c2846486dwrowedmu_tx_add_new_object(dmu_tx_t *tx, objset_t *os, uint64_t object)
990e1969a428b8844e07aad088df41340cd009d4wrowe{
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe /*
990e1969a428b8844e07aad088df41340cd009d4wrowe * If we're syncing, they can manipulate any object anyhow, and
990e1969a428b8844e07aad088df41340cd009d4wrowe * the hold on the dnode_t can cause problems.
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe */
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe if (!dmu_tx_is_syncing(tx)) {
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe (void) dmu_tx_hold_object_impl(tx, os,
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe object, THT_NEWOBJECT, 0, 0);
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe }
38a99ca79b5af0b58ec887f4a449dbb76f900fafwrowe}
8ad0beaa526f5ebc7a44779020a82814f76e9617stoddard
8ad0beaa526f5ebc7a44779020a82814f76e9617stoddardstatic int
8ad0beaa526f5ebc7a44779020a82814f76e9617stoddarddmu_tx_check_ioerr(zio_t *zio, dnode_t *dn, int level, uint64_t blkid)
990e1969a428b8844e07aad088df41340cd009d4wrowe{
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe int err;
9ec65cbae2f760e485a1c54df5b19853688d5c91wrowe dmu_buf_impl_t *db;
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe rw_enter(&dn->dn_struct_rwlock, RW_READER);
990e1969a428b8844e07aad088df41340cd009d4wrowe db = dbuf_hold_level(dn, level, blkid, FTAG);
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe rw_exit(&dn->dn_struct_rwlock);
990e1969a428b8844e07aad088df41340cd009d4wrowe if (db == NULL)
990e1969a428b8844e07aad088df41340cd009d4wrowe return (EIO);
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe err = dbuf_read(db, zio, DB_RF_CANFAIL | DB_RF_NOPREFETCH);
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe dbuf_rele(db, FTAG);
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe return (err);
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe}
9ec65cbae2f760e485a1c54df5b19853688d5c91wrowe
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe/* ARGSUSED */
4099f9c1ec0ad7d2423208089d87592c2846486dwrowestatic void
990e1969a428b8844e07aad088df41340cd009d4wrowedmu_tx_count_write(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe{
990e1969a428b8844e07aad088df41340cd009d4wrowe dnode_t *dn = txh->txh_dnode;
990e1969a428b8844e07aad088df41340cd009d4wrowe uint64_t start, end, i;
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe int min_bs, max_bs, min_ibs, max_ibs, epbs, bits;
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe int err = 0;
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe if (len == 0)
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe return;
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe
37e3f7ccfb0862f3a7626681d0b1d01a3650162bstoddard min_bs = SPA_MINBLOCKSHIFT;
37e3f7ccfb0862f3a7626681d0b1d01a3650162bstoddard max_bs = SPA_MAXBLOCKSHIFT;
37e3f7ccfb0862f3a7626681d0b1d01a3650162bstoddard min_ibs = DN_MIN_INDBLKSHIFT;
990e1969a428b8844e07aad088df41340cd009d4wrowe max_ibs = DN_MAX_INDBLKSHIFT;
a9b90da93a259d06a414e51ec41632c28c71aa8awrowe
a9b90da93a259d06a414e51ec41632c28c71aa8awrowe
a9b90da93a259d06a414e51ec41632c28c71aa8awrowe /*
990e1969a428b8844e07aad088df41340cd009d4wrowe * For i/o error checking, read the first and last level-0
be8e3c3ad9c5d87951a803b632dccde0a4aba35cwrowe * blocks (if they are not aligned), and all the level-1 blocks.
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe */
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe if (dn) {
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe if (dn->dn_maxblkid == 0) {
e4a29c6dfdf566d49377e2a27595af02fb460ff4stoddard err = dmu_tx_check_ioerr(NULL, dn, 0, 0);
e4a29c6dfdf566d49377e2a27595af02fb460ff4stoddard if (err)
b0714718069df29b62da56faaffd2ed7c1287862wrowe goto out;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding } else {
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding zio_t *zio = zio_root(dn->dn_objset->os_spa,
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding NULL, NULL, ZIO_FLAG_CANFAIL);
b0714718069df29b62da56faaffd2ed7c1287862wrowe
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding /* first level-0 block */
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding start = off >> dn->dn_datablkshift;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding if (P2PHASE(off, dn->dn_datablksz) ||
b0714718069df29b62da56faaffd2ed7c1287862wrowe len < dn->dn_datablksz) {
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding err = dmu_tx_check_ioerr(zio, dn, 0, start);
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding if (err)
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding goto out;
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe }
f6bd1c0924641e60dcc56cbaba27d3cb3ce4917erbb
f6bd1c0924641e60dcc56cbaba27d3cb3ce4917erbb /* last level-0 block */
f6bd1c0924641e60dcc56cbaba27d3cb3ce4917erbb end = (off+len-1) >> dn->dn_datablkshift;
577d813165ace257dd6f3bee143e5413ea1a16acstoddard if (end != start &&
db920ff940978fe01db8d4de8c2a9d507bc0bbcdstoddard P2PHASE(off+len, dn->dn_datablksz)) {
db920ff940978fe01db8d4de8c2a9d507bc0bbcdstoddard err = dmu_tx_check_ioerr(zio, dn, 0, end);
db920ff940978fe01db8d4de8c2a9d507bc0bbcdstoddard if (err)
577d813165ace257dd6f3bee143e5413ea1a16acstoddard goto out;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding }
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding /* level-1 blocks */
577d813165ace257dd6f3bee143e5413ea1a16acstoddard if (dn->dn_nlevels > 1) {
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding start >>= dn->dn_indblkshift - SPA_BLKPTRSHIFT;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding end >>= dn->dn_indblkshift - SPA_BLKPTRSHIFT;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding for (i = start+1; i < end; i++) {
577d813165ace257dd6f3bee143e5413ea1a16acstoddard err = dmu_tx_check_ioerr(zio, dn, 1, i);
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding if (err)
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding goto out;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding }
577d813165ace257dd6f3bee143e5413ea1a16acstoddard }
f6bd1c0924641e60dcc56cbaba27d3cb3ce4917erbb
f6bd1c0924641e60dcc56cbaba27d3cb3ce4917erbb err = zio_wait(zio);
f6bd1c0924641e60dcc56cbaba27d3cb3ce4917erbb if (err)
577d813165ace257dd6f3bee143e5413ea1a16acstoddard goto out;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding }
577d813165ace257dd6f3bee143e5413ea1a16acstoddard }
577d813165ace257dd6f3bee143e5413ea1a16acstoddard
577d813165ace257dd6f3bee143e5413ea1a16acstoddard /*
577d813165ace257dd6f3bee143e5413ea1a16acstoddard * If there's more than one block, the blocksize can't change,
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding * so we can make a more precise estimate. Alternatively,
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding * if the dnode's ibs is larger than max_ibs, always use that.
577d813165ace257dd6f3bee143e5413ea1a16acstoddard * This ensures that if we reduce DN_MAX_INDBLKSHIFT,
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding * the code will still work correctly on existing pools.
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding */
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding if (dn && (dn->dn_maxblkid != 0 || dn->dn_indblkshift > max_ibs)) {
577d813165ace257dd6f3bee143e5413ea1a16acstoddard min_ibs = max_ibs = dn->dn_indblkshift;
8cc01204a878dc5f39bba11aa279564b81b15803wrowe if (dn->dn_datablkshift != 0)
8cc01204a878dc5f39bba11aa279564b81b15803wrowe min_bs = max_bs = dn->dn_datablkshift;
8cc01204a878dc5f39bba11aa279564b81b15803wrowe }
577d813165ace257dd6f3bee143e5413ea1a16acstoddard
bcdc8e0b30a73eb30244d66010dbe3233ac755aewrowe /*
bcdc8e0b30a73eb30244d66010dbe3233ac755aewrowe * 'end' is the last thing we will access, not one past.
bcdc8e0b30a73eb30244d66010dbe3233ac755aewrowe * This way we won't overflow when accessing the last byte.
577d813165ace257dd6f3bee143e5413ea1a16acstoddard */
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding start = P2ALIGN(off, 1ULL << max_bs);
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding end = P2ROUNDUP(off + len, 1ULL << max_bs) - 1;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding txh->txh_space_towrite += end - start + 1;
577d813165ace257dd6f3bee143e5413ea1a16acstoddard
b0714718069df29b62da56faaffd2ed7c1287862wrowe start >>= min_bs;
b0714718069df29b62da56faaffd2ed7c1287862wrowe end >>= min_bs;
b0714718069df29b62da56faaffd2ed7c1287862wrowe
577d813165ace257dd6f3bee143e5413ea1a16acstoddard epbs = min_ibs - SPA_BLKPTRSHIFT;
bcdc8e0b30a73eb30244d66010dbe3233ac755aewrowe
bcdc8e0b30a73eb30244d66010dbe3233ac755aewrowe /*
bcdc8e0b30a73eb30244d66010dbe3233ac755aewrowe * The object contains at most 2^(64 - min_bs) blocks,
990e1969a428b8844e07aad088df41340cd009d4wrowe * and each indirect level maps 2^epbs.
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding */
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding for (bits = 64 - min_bs; bits >= 0; bits -= epbs) {
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding start >>= epbs;
990e1969a428b8844e07aad088df41340cd009d4wrowe end >>= epbs;
f86735dccc1da7eaaa4b32739701b6ed98ad6bf1stoddard /*
f86735dccc1da7eaaa4b32739701b6ed98ad6bf1stoddard * If we increase the number of levels of indirection,
f86735dccc1da7eaaa4b32739701b6ed98ad6bf1stoddard * we'll need new blkid=0 indirect blocks. If start == 0,
990e1969a428b8844e07aad088df41340cd009d4wrowe * we're already accounting for that blocks; and if end == 0,
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding * we can't increase the number of levels beyond that.
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding */
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding if (start != 0 && end != 0)
990e1969a428b8844e07aad088df41340cd009d4wrowe txh->txh_space_towrite += 1ULL << max_ibs;
37e3f7ccfb0862f3a7626681d0b1d01a3650162bstoddard txh->txh_space_towrite += (end - start + 1) << max_ibs;
37e3f7ccfb0862f3a7626681d0b1d01a3650162bstoddard }
37e3f7ccfb0862f3a7626681d0b1d01a3650162bstoddard
990e1969a428b8844e07aad088df41340cd009d4wrowe ASSERT(txh->txh_space_towrite < 2 * DMU_MAX_ACCESS);
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding
f3220f54126b25e1cf93cc26c17177b7aef850fdfieldingout:
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding if (err)
990e1969a428b8844e07aad088df41340cd009d4wrowe txh->txh_tx->tx_err = err;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding}
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding
f3220f54126b25e1cf93cc26c17177b7aef850fdfieldingstatic void
990e1969a428b8844e07aad088df41340cd009d4wrowedmu_tx_count_dnode(dmu_tx_hold_t *txh)
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding{
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding dnode_t *dn = txh->txh_dnode;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding dnode_t *mdn = txh->txh_tx->tx_objset->os->os_meta_dnode;
990e1969a428b8844e07aad088df41340cd009d4wrowe uint64_t space = mdn->dn_datablksz +
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding ((mdn->dn_nlevels-1) << mdn->dn_indblkshift);
7e522b9ec579f96ee29120bc7f5e1979177de745stoddard
7e522b9ec579f96ee29120bc7f5e1979177de745stoddard if (dn && dn->dn_dbuf->db_blkptr &&
990e1969a428b8844e07aad088df41340cd009d4wrowe dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset,
7e522b9ec579f96ee29120bc7f5e1979177de745stoddard dn->dn_dbuf->db_blkptr->blk_birth)) {
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding txh->txh_space_tooverwrite += space;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding } else {
990e1969a428b8844e07aad088df41340cd009d4wrowe txh->txh_space_towrite += space;
be8e3c3ad9c5d87951a803b632dccde0a4aba35cwrowe if (dn && dn->dn_dbuf->db_blkptr)
be8e3c3ad9c5d87951a803b632dccde0a4aba35cwrowe txh->txh_space_tounref += space;
be8e3c3ad9c5d87951a803b632dccde0a4aba35cwrowe }
990e1969a428b8844e07aad088df41340cd009d4wrowe}
dcd2219e426b64c69306a609cd8261f11283f9e9stoddard
dcd2219e426b64c69306a609cd8261f11283f9e9stoddardvoid
dcd2219e426b64c69306a609cd8261f11283f9e9stoddarddmu_tx_hold_write(dmu_tx_t *tx, uint64_t object, uint64_t off, int len)
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe{
be8e3c3ad9c5d87951a803b632dccde0a4aba35cwrowe dmu_tx_hold_t *txh;
be8e3c3ad9c5d87951a803b632dccde0a4aba35cwrowe
be8e3c3ad9c5d87951a803b632dccde0a4aba35cwrowe ASSERT(tx->tx_txg == 0);
990e1969a428b8844e07aad088df41340cd009d4wrowe ASSERT(len < DMU_MAX_ACCESS);
be8e3c3ad9c5d87951a803b632dccde0a4aba35cwrowe ASSERT(len == 0 || UINT64_MAX - off >= len - 1);
be8e3c3ad9c5d87951a803b632dccde0a4aba35cwrowe
be8e3c3ad9c5d87951a803b632dccde0a4aba35cwrowe txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
990e1969a428b8844e07aad088df41340cd009d4wrowe object, THT_WRITE, off, len);
83bee7f569bee7534741915e173e6a7de9ddae81stoddard if (txh == NULL)
83bee7f569bee7534741915e173e6a7de9ddae81stoddard return;
83bee7f569bee7534741915e173e6a7de9ddae81stoddard
990e1969a428b8844e07aad088df41340cd009d4wrowe dmu_tx_count_write(txh, off, len);
83bee7f569bee7534741915e173e6a7de9ddae81stoddard dmu_tx_count_dnode(txh);
83bee7f569bee7534741915e173e6a7de9ddae81stoddard}
83bee7f569bee7534741915e173e6a7de9ddae81stoddard
990e1969a428b8844e07aad088df41340cd009d4wrowestatic void
83bee7f569bee7534741915e173e6a7de9ddae81stoddarddmu_tx_count_free(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
83bee7f569bee7534741915e173e6a7de9ddae81stoddard{
83bee7f569bee7534741915e173e6a7de9ddae81stoddard uint64_t blkid, nblks, lastblk;
990e1969a428b8844e07aad088df41340cd009d4wrowe uint64_t space = 0, unref = 0, skipped = 0;
be8e3c3ad9c5d87951a803b632dccde0a4aba35cwrowe dnode_t *dn = txh->txh_dnode;
be8e3c3ad9c5d87951a803b632dccde0a4aba35cwrowe dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
be8e3c3ad9c5d87951a803b632dccde0a4aba35cwrowe spa_t *spa = txh->txh_tx->tx_pool->dp_spa;
990e1969a428b8844e07aad088df41340cd009d4wrowe int epbs;
be8e3c3ad9c5d87951a803b632dccde0a4aba35cwrowe
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe if (dn->dn_nlevels == 0)
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe return;
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe /*
be8e3c3ad9c5d87951a803b632dccde0a4aba35cwrowe * The struct_rwlock protects us against dn_nlevels
be8e3c3ad9c5d87951a803b632dccde0a4aba35cwrowe * changing, in case (against all odds) we manage to dirty &
990e1969a428b8844e07aad088df41340cd009d4wrowe * sync out the changes after we check for being dirty.
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding * Also, dbuf_hold_level() wants us to have the struct_rwlock.
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding */
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding rw_enter(&dn->dn_struct_rwlock, RW_READER);
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding if (dn->dn_maxblkid == 0) {
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding if (off == 0 && len >= dn->dn_datablksz) {
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding blkid = 0;
990e1969a428b8844e07aad088df41340cd009d4wrowe nblks = 1;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding } else {
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding rw_exit(&dn->dn_struct_rwlock);
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding return;
990e1969a428b8844e07aad088df41340cd009d4wrowe }
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding } else {
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding blkid = off >> dn->dn_datablkshift;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding nblks = (len + dn->dn_datablksz - 1) >> dn->dn_datablkshift;
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding if (blkid >= dn->dn_maxblkid) {
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding rw_exit(&dn->dn_struct_rwlock);
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding return;
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe }
83bee7f569bee7534741915e173e6a7de9ddae81stoddard if (blkid + nblks > dn->dn_maxblkid)
83bee7f569bee7534741915e173e6a7de9ddae81stoddard nblks = dn->dn_maxblkid - blkid;
83bee7f569bee7534741915e173e6a7de9ddae81stoddard
990e1969a428b8844e07aad088df41340cd009d4wrowe }
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding if (dn->dn_nlevels == 1) {
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding int i;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding for (i = 0; i < nblks; i++) {
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe blkptr_t *bp = dn->dn_phys->dn_blkptr;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding ASSERT3U(blkid + i, <, dn->dn_nblkptr);
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding bp += blkid + i;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding if (dsl_dataset_block_freeable(ds, bp->blk_birth)) {
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe dprintf_bp(bp, "can free old%s", "");
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding space += bp_get_dasize(spa, bp);
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding }
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding unref += BP_GET_ASIZE(bp);
990e1969a428b8844e07aad088df41340cd009d4wrowe }
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding nblks = 0;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding }
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe /*
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding * Add in memory requirements of higher-level indirects.
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding * This assumes a worst-possible scenario for dn_nlevels.
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding */
990e1969a428b8844e07aad088df41340cd009d4wrowe {
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding uint64_t blkcnt = 1 + ((nblks >> epbs) >> epbs);
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding int level = dn->dn_nlevels > 1 ? 2 : 1;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe while (level++ < DN_MAX_LEVELS) {
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding txh->txh_memory_tohold += blkcnt << dn->dn_indblkshift;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding blkcnt = 1 + (blkcnt >> epbs);
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding }
990e1969a428b8844e07aad088df41340cd009d4wrowe ASSERT(blkcnt <= dn->dn_nblkptr);
83bee7f569bee7534741915e173e6a7de9ddae81stoddard }
83bee7f569bee7534741915e173e6a7de9ddae81stoddard
83bee7f569bee7534741915e173e6a7de9ddae81stoddard lastblk = blkid + nblks - 1;
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe while (nblks) {
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding dmu_buf_impl_t *dbuf;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding uint64_t ibyte, new_blkid;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding int epb = 1 << epbs;
990e1969a428b8844e07aad088df41340cd009d4wrowe int err, i, blkoff, tochk;
10a4cdd68ef1ca0e54af296fe1d08ac00150c90bwrowe blkptr_t *bp;
10a4cdd68ef1ca0e54af296fe1d08ac00150c90bwrowe
10a4cdd68ef1ca0e54af296fe1d08ac00150c90bwrowe ibyte = blkid << dn->dn_datablkshift;
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe err = dnode_next_offset(dn,
10a4cdd68ef1ca0e54af296fe1d08ac00150c90bwrowe DNODE_FIND_HAVELOCK, &ibyte, 2, 1, 0);
10a4cdd68ef1ca0e54af296fe1d08ac00150c90bwrowe new_blkid = ibyte >> dn->dn_datablkshift;
10a4cdd68ef1ca0e54af296fe1d08ac00150c90bwrowe if (err == ESRCH)
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe break;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding if (err) {
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding txh->txh_tx->tx_err = err;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding break;
990e1969a428b8844e07aad088df41340cd009d4wrowe }
10a4cdd68ef1ca0e54af296fe1d08ac00150c90bwrowe if (new_blkid > lastblk)
10a4cdd68ef1ca0e54af296fe1d08ac00150c90bwrowe break;
10a4cdd68ef1ca0e54af296fe1d08ac00150c90bwrowe
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe if (new_blkid > blkid) {
8cc01204a878dc5f39bba11aa279564b81b15803wrowe skipped += new_blkid - blkid - 1;
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe nblks -= new_blkid - blkid;
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe blkid = new_blkid;
8cc01204a878dc5f39bba11aa279564b81b15803wrowe }
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe blkoff = P2PHASE(blkid, epb);
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding tochk = MIN(epb - blkoff, nblks);
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe dbuf = dbuf_hold_level(dn, 1, blkid >> epbs, FTAG);
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding txh->txh_memory_tohold += dbuf->db.db_size;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding if (txh->txh_memory_tohold > DMU_MAX_ACCESS) {
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe txh->txh_tx->tx_err = E2BIG;
f86735dccc1da7eaaa4b32739701b6ed98ad6bf1stoddard dbuf_rele(dbuf, FTAG);
f86735dccc1da7eaaa4b32739701b6ed98ad6bf1stoddard break;
f86735dccc1da7eaaa4b32739701b6ed98ad6bf1stoddard }
990e1969a428b8844e07aad088df41340cd009d4wrowe err = dbuf_read(dbuf, NULL, DB_RF_HAVESTRUCT | DB_RF_CANFAIL);
10a4cdd68ef1ca0e54af296fe1d08ac00150c90bwrowe if (err != 0) {
10a4cdd68ef1ca0e54af296fe1d08ac00150c90bwrowe txh->txh_tx->tx_err = err;
10a4cdd68ef1ca0e54af296fe1d08ac00150c90bwrowe dbuf_rele(dbuf, FTAG);
990e1969a428b8844e07aad088df41340cd009d4wrowe break;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding }
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding bp = dbuf->db.db_data;
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe bp += blkoff;
10a4cdd68ef1ca0e54af296fe1d08ac00150c90bwrowe
10a4cdd68ef1ca0e54af296fe1d08ac00150c90bwrowe for (i = 0; i < tochk; i++) {
10a4cdd68ef1ca0e54af296fe1d08ac00150c90bwrowe if (dsl_dataset_block_freeable(ds, bp[i].blk_birth)) {
990e1969a428b8844e07aad088df41340cd009d4wrowe dprintf_bp(&bp[i], "can free old%s", "");
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding space += bp_get_dasize(spa, &bp[i]);
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding }
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding unref += BP_GET_ASIZE(bp);
4099f9c1ec0ad7d2423208089d87592c2846486dwrowe }
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding dbuf_rele(dbuf, FTAG);
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding blkid += tochk;
990e1969a428b8844e07aad088df41340cd009d4wrowe nblks -= tochk;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding }
594c336c5ff3a53eb8de499c1070dc99577f8442wrowe rw_exit(&dn->dn_struct_rwlock);
594c336c5ff3a53eb8de499c1070dc99577f8442wrowe
990e1969a428b8844e07aad088df41340cd009d4wrowe /* account for new level 1 indirect blocks that might show up */
594c336c5ff3a53eb8de499c1070dc99577f8442wrowe if (skipped) {
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding txh->txh_fudge += skipped << dn->dn_indblkshift;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding skipped = MIN(skipped, DMU_MAX_DELETEBLKCNT >> epbs);
990e1969a428b8844e07aad088df41340cd009d4wrowe txh->txh_memory_tohold += skipped << dn->dn_indblkshift;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding }
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding txh->txh_space_tofree += space;
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding txh->txh_space_tounref += unref;
990e1969a428b8844e07aad088df41340cd009d4wrowe}
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding
f3220f54126b25e1cf93cc26c17177b7aef850fdfieldingvoid
f3220f54126b25e1cf93cc26c17177b7aef850fdfieldingdmu_tx_hold_free(dmu_tx_t *tx, uint64_t object, uint64_t off, uint64_t len)
f3220f54126b25e1cf93cc26c17177b7aef850fdfielding{
dmu_tx_hold_t *txh;
dnode_t *dn;
uint64_t start, end, i;
int err, shift;
zio_t *zio;
ASSERT(tx->tx_txg == 0);
txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
object, THT_FREE, off, len);
if (txh == NULL)
return;
dn = txh->txh_dnode;
/* first block */
if (off != 0)
dmu_tx_count_write(txh, off, 1);
/* last block */
if (len != DMU_OBJECT_END)
dmu_tx_count_write(txh, off+len, 1);
if (off >= (dn->dn_maxblkid+1) * dn->dn_datablksz)
return;
if (len == DMU_OBJECT_END)
len = (dn->dn_maxblkid+1) * dn->dn_datablksz - off;
/*
* For i/o error checking, read the first and last level-0
* blocks, and all the level-1 blocks. The above count_write's
* have already taken care of the level-0 blocks.
*/
if (dn->dn_nlevels > 1) {
shift = dn->dn_datablkshift + dn->dn_indblkshift -
SPA_BLKPTRSHIFT;
start = off >> shift;
end = dn->dn_datablkshift ? ((off+len) >> shift) : 0;
zio = zio_root(tx->tx_pool->dp_spa,
NULL, NULL, ZIO_FLAG_CANFAIL);
for (i = start; i <= end; i++) {
uint64_t ibyte = i << shift;
err = dnode_next_offset(dn, 0, &ibyte, 2, 1, 0);
i = ibyte >> shift;
if (err == ESRCH)
break;
if (err) {
tx->tx_err = err;
return;
}
err = dmu_tx_check_ioerr(zio, dn, 1, i);
if (err) {
tx->tx_err = err;
return;
}
}
err = zio_wait(zio);
if (err) {
tx->tx_err = err;
return;
}
}
dmu_tx_count_dnode(txh);
dmu_tx_count_free(txh, off, len);
}
void
dmu_tx_hold_zap(dmu_tx_t *tx, uint64_t object, int add, char *name)
{
dmu_tx_hold_t *txh;
dnode_t *dn;
uint64_t nblocks;
int epbs, err;
ASSERT(tx->tx_txg == 0);
txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
object, THT_ZAP, add, (uintptr_t)name);
if (txh == NULL)
return;
dn = txh->txh_dnode;
dmu_tx_count_dnode(txh);
if (dn == NULL) {
/*
* We will be able to fit a new object's entries into one leaf
* block. So there will be at most 2 blocks total,
* including the header block.
*/
dmu_tx_count_write(txh, 0, 2 << fzap_default_block_shift);
return;
}
ASSERT3P(dmu_ot[dn->dn_type].ot_byteswap, ==, zap_byteswap);
if (dn->dn_maxblkid == 0 && !add) {
/*
* If there is only one block (i.e. this is a micro-zap)
* and we are not adding anything, the accounting is simple.
*/
err = dmu_tx_check_ioerr(NULL, dn, 0, 0);
if (err) {
tx->tx_err = err;
return;
}
/*
* Use max block size here, since we don't know how much
* the size will change between now and the dbuf dirty call.
*/
if (dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset,
dn->dn_phys->dn_blkptr[0].blk_birth)) {
txh->txh_space_tooverwrite += SPA_MAXBLOCKSIZE;
} else {
txh->txh_space_towrite += SPA_MAXBLOCKSIZE;
txh->txh_space_tounref +=
BP_GET_ASIZE(dn->dn_phys->dn_blkptr);
}
return;
}
if (dn->dn_maxblkid > 0 && name) {
/*
* access the name in this fat-zap so that we'll check
* for i/o errors to the leaf blocks, etc.
*/
err = zap_lookup(&dn->dn_objset->os, dn->dn_object, name,
8, 0, NULL);
if (err == EIO) {
tx->tx_err = err;
return;
}
}
/*
* 3 blocks overwritten: target leaf, ptrtbl block, header block
* 3 new blocks written if adding: new split leaf, 2 grown ptrtbl blocks
*/
dmu_tx_count_write(txh, dn->dn_maxblkid * dn->dn_datablksz,
(3 + add ? 3 : 0) << dn->dn_datablkshift);
/*
* If the modified blocks are scattered to the four winds,
* we'll have to modify an indirect twig for each.
*/
epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
for (nblocks = dn->dn_maxblkid >> epbs; nblocks != 0; nblocks >>= epbs)
txh->txh_space_towrite += 3 << dn->dn_indblkshift;
}
void
dmu_tx_hold_bonus(dmu_tx_t *tx, uint64_t object)
{
dmu_tx_hold_t *txh;
ASSERT(tx->tx_txg == 0);
txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
object, THT_BONUS, 0, 0);
if (txh)
dmu_tx_count_dnode(txh);
}
void
dmu_tx_hold_space(dmu_tx_t *tx, uint64_t space)
{
dmu_tx_hold_t *txh;
ASSERT(tx->tx_txg == 0);
txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
DMU_NEW_OBJECT, THT_SPACE, space, 0);
txh->txh_space_towrite += space;
}
int
dmu_tx_holds(dmu_tx_t *tx, uint64_t object)
{
dmu_tx_hold_t *txh;
int holds = 0;
/*
* By asserting that the tx is assigned, we're counting the
* number of dn_tx_holds, which is the same as the number of
* dn_holds. Otherwise, we'd be counting dn_holds, but
* dn_tx_holds could be 0.
*/
ASSERT(tx->tx_txg != 0);
/* if (tx->tx_anyobj == TRUE) */
/* return (0); */
for (txh = list_head(&tx->tx_holds); txh;
txh = list_next(&tx->tx_holds, txh)) {
if (txh->txh_dnode && txh->txh_dnode->dn_object == object)
holds++;
}
return (holds);
}
#ifdef ZFS_DEBUG
void
dmu_tx_dirty_buf(dmu_tx_t *tx, dmu_buf_impl_t *db)
{
dmu_tx_hold_t *txh;
int match_object = FALSE, match_offset = FALSE;
dnode_t *dn = db->db_dnode;
ASSERT(tx->tx_txg != 0);
ASSERT(tx->tx_objset == NULL || dn->dn_objset == tx->tx_objset->os);
ASSERT3U(dn->dn_object, ==, db->db.db_object);
if (tx->tx_anyobj)
return;
/* XXX No checking on the meta dnode for now */
if (db->db.db_object == DMU_META_DNODE_OBJECT)
return;
for (txh = list_head(&tx->tx_holds); txh;
txh = list_next(&tx->tx_holds, txh)) {
ASSERT(dn == NULL || dn->dn_assigned_txg == tx->tx_txg);
if (txh->txh_dnode == dn && txh->txh_type != THT_NEWOBJECT)
match_object = TRUE;
if (txh->txh_dnode == NULL || txh->txh_dnode == dn) {
int datablkshift = dn->dn_datablkshift ?
dn->dn_datablkshift : SPA_MAXBLOCKSHIFT;
int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
int shift = datablkshift + epbs * db->db_level;
uint64_t beginblk = shift >= 64 ? 0 :
(txh->txh_arg1 >> shift);
uint64_t endblk = shift >= 64 ? 0 :
((txh->txh_arg1 + txh->txh_arg2 - 1) >> shift);
uint64_t blkid = db->db_blkid;
/* XXX txh_arg2 better not be zero... */
dprintf("found txh type %x beginblk=%llx endblk=%llx\n",
txh->txh_type, beginblk, endblk);
switch (txh->txh_type) {
case THT_WRITE:
if (blkid >= beginblk && blkid <= endblk)
match_offset = TRUE;
/*
* We will let this hold work for the bonus
* buffer so that we don't need to hold it
* when creating a new object.
*/
if (blkid == DB_BONUS_BLKID)
match_offset = TRUE;
/*
* They might have to increase nlevels,
* thus dirtying the new TLIBs. Or the
* might have to change the block size,
* thus dirying the new lvl=0 blk=0.
*/
if (blkid == 0)
match_offset = TRUE;
break;
case THT_FREE:
/*
* We will dirty all the level 1 blocks in
* the free range and perhaps the first and
* last level 0 block.
*/
if (blkid >= beginblk && (blkid <= endblk ||
txh->txh_arg2 == DMU_OBJECT_END))
match_offset = TRUE;
break;
case THT_BONUS:
if (blkid == DB_BONUS_BLKID)
match_offset = TRUE;
break;
case THT_ZAP:
match_offset = TRUE;
break;
case THT_NEWOBJECT:
match_object = TRUE;
break;
default:
ASSERT(!"bad txh_type");
}
}
if (match_object && match_offset)
return;
}
panic("dirtying dbuf obj=%llx lvl=%u blkid=%llx but not tx_held\n",
(u_longlong_t)db->db.db_object, db->db_level,
(u_longlong_t)db->db_blkid);
}
#endif
static int
dmu_tx_try_assign(dmu_tx_t *tx, uint64_t txg_how)
{
dmu_tx_hold_t *txh;
spa_t *spa = tx->tx_pool->dp_spa;
uint64_t memory, asize, fsize, usize;
uint64_t towrite, tofree, tooverwrite, tounref, tohold, fudge;
ASSERT3U(tx->tx_txg, ==, 0);
if (tx->tx_err)
return (tx->tx_err);
if (spa_state(spa) == POOL_STATE_IO_FAILURE) {
/*
* If the user has indicated a blocking failure mode
* then return ERESTART which will block in dmu_tx_wait().
* Otherwise, return EIO so that an error can get
* propagated back to the VOP calls.
*
* Note that we always honor the txg_how flag regardless
* of the failuremode setting.
*/
if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE &&
txg_how != TXG_WAIT)
return (EIO);
return (ERESTART);
}
tx->tx_txg = txg_hold_open(tx->tx_pool, &tx->tx_txgh);
tx->tx_needassign_txh = NULL;
/*
* NB: No error returns are allowed after txg_hold_open, but
* before processing the dnode holds, due to the
* dmu_tx_unassign() logic.
*/
towrite = tofree = tooverwrite = tounref = tohold = fudge = 0;
for (txh = list_head(&tx->tx_holds); txh;
txh = list_next(&tx->tx_holds, txh)) {
dnode_t *dn = txh->txh_dnode;
if (dn != NULL) {
mutex_enter(&dn->dn_mtx);
if (dn->dn_assigned_txg == tx->tx_txg - 1) {
mutex_exit(&dn->dn_mtx);
tx->tx_needassign_txh = txh;
return (ERESTART);
}
if (dn->dn_assigned_txg == 0)
dn->dn_assigned_txg = tx->tx_txg;
ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
(void) refcount_add(&dn->dn_tx_holds, tx);
mutex_exit(&dn->dn_mtx);
}
towrite += txh->txh_space_towrite;
tofree += txh->txh_space_tofree;
tooverwrite += txh->txh_space_tooverwrite;
tounref += txh->txh_space_tounref;
tohold += txh->txh_memory_tohold;
fudge += txh->txh_fudge;
}
/*
* NB: This check must be after we've held the dnodes, so that
* the dmu_tx_unassign() logic will work properly
*/
if (txg_how >= TXG_INITIAL && txg_how != tx->tx_txg)
return (ERESTART);
/*
* If a snapshot has been taken since we made our estimates,
* assume that we won't be able to free or overwrite anything.
*/
if (tx->tx_objset &&
dsl_dataset_prev_snap_txg(tx->tx_objset->os->os_dsl_dataset) >
tx->tx_lastsnap_txg) {
towrite += tooverwrite;
tooverwrite = tofree = 0;
}
/* needed allocation: worst-case estimate of write space */
asize = spa_get_asize(tx->tx_pool->dp_spa, towrite + tooverwrite);
/* freed space estimate: worst-case overwrite + free estimate */
fsize = spa_get_asize(tx->tx_pool->dp_spa, tooverwrite) + tofree;
/* convert unrefd space to worst-case estimate */
usize = spa_get_asize(tx->tx_pool->dp_spa, tounref);
/* calculate memory footprint estimate */
memory = towrite + tooverwrite + tohold;
#ifdef ZFS_DEBUG
/*
* Add in 'tohold' to account for our dirty holds on this memory
* XXX - the "fudge" factor is to account for skipped blocks that
* we missed because dnode_next_offset() misses in-core-only blocks.
*/
tx->tx_space_towrite = asize +
spa_get_asize(tx->tx_pool->dp_spa, tohold + fudge);
tx->tx_space_tofree = tofree;
tx->tx_space_tooverwrite = tooverwrite;
tx->tx_space_tounref = tounref;
#endif
if (tx->tx_dir && asize != 0) {
int err = dsl_dir_tempreserve_space(tx->tx_dir, memory,
asize, fsize, usize, &tx->tx_tempreserve_cookie, tx);
if (err)
return (err);
}
return (0);
}
static void
dmu_tx_unassign(dmu_tx_t *tx)
{
dmu_tx_hold_t *txh;
if (tx->tx_txg == 0)
return;
txg_rele_to_quiesce(&tx->tx_txgh);
for (txh = list_head(&tx->tx_holds); txh != tx->tx_needassign_txh;
txh = list_next(&tx->tx_holds, txh)) {
dnode_t *dn = txh->txh_dnode;
if (dn == NULL)
continue;
mutex_enter(&dn->dn_mtx);
ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
if (refcount_remove(&dn->dn_tx_holds, tx) == 0) {
dn->dn_assigned_txg = 0;
cv_broadcast(&dn->dn_notxholds);
}
mutex_exit(&dn->dn_mtx);
}
txg_rele_to_sync(&tx->tx_txgh);
tx->tx_lasttried_txg = tx->tx_txg;
tx->tx_txg = 0;
}
/*
* Assign tx to a transaction group. txg_how can be one of:
*
* (1) TXG_WAIT. If the current open txg is full, waits until there's
* a new one. This should be used when you're not holding locks.
* If will only fail if we're truly out of space (or over quota).
*
* (2) TXG_NOWAIT. If we can't assign into the current open txg without
* blocking, returns immediately with ERESTART. This should be used
* whenever you're holding locks. On an ERESTART error, the caller
* should drop locks, do a dmu_tx_wait(tx), and try again.
*
* (3) A specific txg. Use this if you need to ensure that multiple
* transactions all sync in the same txg. Like TXG_NOWAIT, it
* returns ERESTART if it can't assign you into the requested txg.
*/
int
dmu_tx_assign(dmu_tx_t *tx, uint64_t txg_how)
{
int err;
ASSERT(tx->tx_txg == 0);
ASSERT(txg_how != 0);
ASSERT(!dsl_pool_sync_context(tx->tx_pool));
while ((err = dmu_tx_try_assign(tx, txg_how)) != 0) {
dmu_tx_unassign(tx);
if (err != ERESTART || txg_how != TXG_WAIT)
return (err);
dmu_tx_wait(tx);
}
txg_rele_to_quiesce(&tx->tx_txgh);
return (0);
}
void
dmu_tx_wait(dmu_tx_t *tx)
{
spa_t *spa = tx->tx_pool->dp_spa;
ASSERT(tx->tx_txg == 0);
/*
* It's possible that the pool has become active after this thread
* has tried to obtain a tx. If that's the case then his
* tx_lasttried_txg would not have been assigned.
*/
if (spa_state(spa) == POOL_STATE_IO_FAILURE ||
tx->tx_lasttried_txg == 0) {
txg_wait_synced(tx->tx_pool, spa_last_synced_txg(spa) + 1);
} else if (tx->tx_needassign_txh) {
dnode_t *dn = tx->tx_needassign_txh->txh_dnode;
mutex_enter(&dn->dn_mtx);
while (dn->dn_assigned_txg == tx->tx_lasttried_txg - 1)
cv_wait(&dn->dn_notxholds, &dn->dn_mtx);
mutex_exit(&dn->dn_mtx);
tx->tx_needassign_txh = NULL;
} else {
txg_wait_open(tx->tx_pool, tx->tx_lasttried_txg + 1);
}
}
void
dmu_tx_willuse_space(dmu_tx_t *tx, int64_t delta)
{
#ifdef ZFS_DEBUG
if (tx->tx_dir == NULL || delta == 0)
return;
if (delta > 0) {
ASSERT3U(refcount_count(&tx->tx_space_written) + delta, <=,
tx->tx_space_towrite);
(void) refcount_add_many(&tx->tx_space_written, delta, NULL);
} else {
(void) refcount_add_many(&tx->tx_space_freed, -delta, NULL);
}
#endif
}
void
dmu_tx_commit(dmu_tx_t *tx)
{
dmu_tx_hold_t *txh;
ASSERT(tx->tx_txg != 0);
while (txh = list_head(&tx->tx_holds)) {
dnode_t *dn = txh->txh_dnode;
list_remove(&tx->tx_holds, txh);
kmem_free(txh, sizeof (dmu_tx_hold_t));
if (dn == NULL)
continue;
mutex_enter(&dn->dn_mtx);
ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
if (refcount_remove(&dn->dn_tx_holds, tx) == 0) {
dn->dn_assigned_txg = 0;
cv_broadcast(&dn->dn_notxholds);
}
mutex_exit(&dn->dn_mtx);
dnode_rele(dn, tx);
}
if (tx->tx_tempreserve_cookie)
dsl_dir_tempreserve_clear(tx->tx_tempreserve_cookie, tx);
if (tx->tx_anyobj == FALSE)
txg_rele_to_sync(&tx->tx_txgh);
list_destroy(&tx->tx_holds);
#ifdef ZFS_DEBUG
dprintf("towrite=%llu written=%llu tofree=%llu freed=%llu\n",
tx->tx_space_towrite, refcount_count(&tx->tx_space_written),
tx->tx_space_tofree, refcount_count(&tx->tx_space_freed));
refcount_destroy_many(&tx->tx_space_written,
refcount_count(&tx->tx_space_written));
refcount_destroy_many(&tx->tx_space_freed,
refcount_count(&tx->tx_space_freed));
#endif
kmem_free(tx, sizeof (dmu_tx_t));
}
void
dmu_tx_abort(dmu_tx_t *tx)
{
dmu_tx_hold_t *txh;
ASSERT(tx->tx_txg == 0);
while (txh = list_head(&tx->tx_holds)) {
dnode_t *dn = txh->txh_dnode;
list_remove(&tx->tx_holds, txh);
kmem_free(txh, sizeof (dmu_tx_hold_t));
if (dn != NULL)
dnode_rele(dn, tx);
}
list_destroy(&tx->tx_holds);
#ifdef ZFS_DEBUG
refcount_destroy_many(&tx->tx_space_written,
refcount_count(&tx->tx_space_written));
refcount_destroy_many(&tx->tx_space_freed,
refcount_count(&tx->tx_space_freed));
#endif
kmem_free(tx, sizeof (dmu_tx_t));
}
uint64_t
dmu_tx_get_txg(dmu_tx_t *tx)
{
ASSERT(tx->tx_txg != 0);
return (tx->tx_txg);
}