dmu_tx.c revision 8f38d41910063e19709864b025684a228961299f
fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER START
fa9e4066f08beec538e775443c5be79dd423fcabahrens * The contents of this file are subject to the terms of the
f65e61c04bc28ffd6bda04619c84330b420450b5ahrens * Common Development and Distribution License (the "License").
f65e61c04bc28ffd6bda04619c84330b420450b5ahrens * You may not use this file except in compliance with the License.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fa9e4066f08beec538e775443c5be79dd423fcabahrens * See the License for the specific language governing permissions
fa9e4066f08beec538e775443c5be79dd423fcabahrens * and limitations under the License.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * When distributing Covered Code, include this CDDL HEADER in each
fa9e4066f08beec538e775443c5be79dd423fcabahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If applicable, add the following below this CDDL HEADER, with the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * fields enclosed by brackets "[]" replaced with your own identifying
fa9e4066f08beec538e775443c5be79dd423fcabahrens * information: Portions Copyright [yyyy] [name of copyright owner]
fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER END
8f38d41910063e19709864b025684a228961299fek * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Use is subject to license terms.
fa9e4066f08beec538e775443c5be79dd423fcabahrens#pragma ident "%Z%%M% %I% %E% SMI"
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/dsl_dataset.h> /* for dsl_dataset_block_freeable() */
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/dsl_dir.h> /* for dsl_dir_tempreserve_*() */
8a2f1b9190d1dc288470a1fd2776d79ce82cb129ahrens#include <sys/zap_impl.h> /* for fzap_default_block_shift */
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrocktypedef void (*dmu_tx_hold_func_t)(dmu_tx_t *tx, struct dnode *dn,
fa9e4066f08beec538e775443c5be79dd423fcabahrens dmu_tx_t *tx = kmem_zalloc(sizeof (dmu_tx_t), KM_SLEEP);
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens dmu_tx_t *tx = dmu_tx_create_dd(os->os->os_dsl_dataset->ds_dir);
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock tx->tx_lastsnap_txg = dsl_dataset_prev_snap_txg(os->os->os_dsl_dataset);
fa9e4066f08beec538e775443c5be79dd423fcabahrensdmu_tx_create_assigned(struct dsl_pool *dp, uint64_t txg)
fa9e4066f08beec538e775443c5be79dd423fcabahrensdmu_tx_hold_object_impl(dmu_tx_t *tx, objset_t *os, uint64_t object,
8a2f1b9190d1dc288470a1fd2776d79ce82cb129ahrens enum dmu_tx_hold_type type, uint64_t arg1, uint64_t arg2)
fa9e4066f08beec538e775443c5be79dd423fcabahrens * dn->dn_assigned_txg == tx->tx_txg doesn't pose a
fa9e4066f08beec538e775443c5be79dd423fcabahrens * problem, but there's no way for it to happen (for
fa9e4066f08beec538e775443c5be79dd423fcabahrens * now, at least).
fa9e4066f08beec538e775443c5be79dd423fcabahrensdmu_tx_add_new_object(dmu_tx_t *tx, objset_t *os, uint64_t object)
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If we're syncing, they can manipulate any object anyhow, and
fa9e4066f08beec538e775443c5be79dd423fcabahrens * the hold on the dnode_t can cause problems.
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrockdmu_tx_check_ioerr(zio_t *zio, dnode_t *dn, int level, uint64_t blkid)
fa9e4066f08beec538e775443c5be79dd423fcabahrens/* ARGSUSED */
8a2f1b9190d1dc288470a1fd2776d79ce82cb129ahrensdmu_tx_count_write(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * For i/o error checking, read the first and last level-0
99653d4ee642c6528e88224f12409a5f23060994eschrock * blocks (if they are not aligned), and all the level-1 blocks.
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock /* first level-0 block */
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock /* last level-0 block */
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock /* level-1 blocks */
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If there's more than one block, the blocksize can't change,
fa9e4066f08beec538e775443c5be79dd423fcabahrens * so we can make a more precise estimate. Alternatively,
fa9e4066f08beec538e775443c5be79dd423fcabahrens * if the dnode's ibs is larger than max_ibs, always use that.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * This ensures that if we reduce DN_MAX_INDBLKSHIFT,
fa9e4066f08beec538e775443c5be79dd423fcabahrens * the code will still work correctly on existing pools.
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (dn && (dn->dn_maxblkid != 0 || dn->dn_indblkshift > max_ibs)) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens * 'end' is the last thing we will access, not one past.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * This way we won't overflow when accessing the last byte.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * The object contains at most 2^(64 - min_bs) blocks,
fa9e4066f08beec538e775443c5be79dd423fcabahrens * and each indirect level maps 2^epbs.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If we increase the number of levels of indirection,
fa9e4066f08beec538e775443c5be79dd423fcabahrens * we'll need new blkid=0 indirect blocks. If start == 0,
fa9e4066f08beec538e775443c5be79dd423fcabahrens * we're already accounting for that blocks; and if end == 0,
fa9e4066f08beec538e775443c5be79dd423fcabahrens * we can't increase the number of levels beyond that.
8a2f1b9190d1dc288470a1fd2776d79ce82cb129ahrens txh->txh_space_towrite += (end - start + 1) << max_ibs;
8a2f1b9190d1dc288470a1fd2776d79ce82cb129ahrens dnode_t *mdn = txh->txh_tx->tx_objset->os->os_meta_dnode;
fa9e4066f08beec538e775443c5be79dd423fcabahrens dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset,
fa9e4066f08beec538e775443c5be79dd423fcabahrensdmu_tx_hold_write(dmu_tx_t *tx, uint64_t object, uint64_t off, int len)
8a2f1b9190d1dc288470a1fd2776d79ce82cb129ahrensdmu_tx_count_free(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
347a31bcb38b51837caee115d3979d3a981cc099ahrens * We don't need to use any locking to check for dirtyness
347a31bcb38b51837caee115d3979d3a981cc099ahrens * because it's OK if we get stale data -- the dnode may become
31fd60d36d9ae794bbedd5e834b8be6d412a853fahrens * dirty immediately after our check anyway. This is just a
31fd60d36d9ae794bbedd5e834b8be6d412a853fahrens * means to avoid the expensive count when we aren't sure we
347a31bcb38b51837caee115d3979d3a981cc099ahrens * need it. We need to be able to deal with a dirty dnode.
8a2f1b9190d1dc288470a1fd2776d79ce82cb129ahrens if (dirty || dn->dn_assigned_txg || dn->dn_phys->dn_nlevels == 0)
c543ec060d1359f6c8a9507242521f344a2ac3efahrens * the struct_rwlock protects us against dn_phys->dn_nlevels
c543ec060d1359f6c8a9507242521f344a2ac3efahrens * changing, in case (against all odds) we manage to dirty &
c543ec060d1359f6c8a9507242521f344a2ac3efahrens * sync out the changes after we check for being dirty.
c543ec060d1359f6c8a9507242521f344a2ac3efahrens * also, dbuf_hold_impl() wants us to have the struct_rwlock.
c543ec060d1359f6c8a9507242521f344a2ac3efahrens * It's fine to use dn_datablkshift rather than the dn_phys
c543ec060d1359f6c8a9507242521f344a2ac3efahrens * equivalent because if it is changing, maxblkid==0 and we will
c543ec060d1359f6c8a9507242521f344a2ac3efahrens /* don't bother after 128,000 blocks */
fa9e4066f08beec538e775443c5be79dd423fcabahrens for (i = 0; i < nblks; i++) {
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock if (dsl_dataset_block_freeable(ds, bp->blk_birth)) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens err = dbuf_hold_impl(dn, 1, blkid >> epbs, TRUE, FTAG, &dbuf);
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (err == 0) {
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock if (err != 0) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens for (i = 0; i < tochk; i++) {
8a2f1b9190d1dc288470a1fd2776d79ce82cb129ahrensdmu_tx_hold_free(dmu_tx_t *tx, uint64_t object, uint64_t off, uint64_t len)
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* first block */
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* last block */
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * For i/o error checking, read the first and last level-0
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * blocks, and all the level-1 blocks. The above count_write's
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * will take care of the level-0 blocks.
98572ac1e6b4d1bdba6ab2df94e0a0c37df56337ahrens end = dn->dn_datablkshift ? ((off+len) >> shift) : 0;
8a2f1b9190d1dc288470a1fd2776d79ce82cb129ahrensdmu_tx_hold_zap(dmu_tx_t *tx, uint64_t object, int add, char *name)
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * We will be able to fit a new object's entries into one leaf
fa9e4066f08beec538e775443c5be79dd423fcabahrens * block. So there will be at most 2 blocks total,
fa9e4066f08beec538e775443c5be79dd423fcabahrens * including the header block.
8a2f1b9190d1dc288470a1fd2776d79ce82cb129ahrens dmu_tx_count_write(txh, 0, 2 << fzap_default_block_shift);
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT3P(dmu_ot[dn->dn_type].ot_byteswap, ==, zap_byteswap);
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If there is only one block (i.e. this is a micro-zap)
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * and we are not adding anything, the accounting is simple.
b6130ead041e56a0331b89b333bdddab8afb9b1cmaybee * Use max block size here, since we don't know how much
b6130ead041e56a0331b89b333bdddab8afb9b1cmaybee * the size will change between now and the dbuf dirty call.
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset,
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * access the name in this fat-zap so that we'll check
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * for i/o errors to the leaf blocks, etc.
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock err = zap_lookup(&dn->dn_objset->os, dn->dn_object, name,
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * 3 blocks overwritten: target leaf, ptrtbl block, header block
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * 3 new blocks written if adding: new split leaf, 2 grown ptrtbl blocks
8a2f1b9190d1dc288470a1fd2776d79ce82cb129ahrens dmu_tx_count_write(txh, dn->dn_maxblkid * dn->dn_datablksz,
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If the modified blocks are scattered to the four winds,
fa9e4066f08beec538e775443c5be79dd423fcabahrens * we'll have to modify an indirect twig for each.
fa9e4066f08beec538e775443c5be79dd423fcabahrens for (nblocks = dn->dn_maxblkid >> epbs; nblocks != 0; nblocks >>= epbs)
fa9e4066f08beec538e775443c5be79dd423fcabahrens * By asserting that the tx is assigned, we're counting the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * number of dn_tx_holds, which is the same as the number of
fa9e4066f08beec538e775443c5be79dd423fcabahrens * dn_holds. Otherwise, we'd be counting dn_holds, but
fa9e4066f08beec538e775443c5be79dd423fcabahrens * dn_tx_holds could be 0.
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* if (tx->tx_anyobj == TRUE) */
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* return (0); */
8a2f1b9190d1dc288470a1fd2776d79ce82cb129ahrens if (txh->txh_dnode && txh->txh_dnode->dn_object == object)
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT(tx->tx_objset == NULL || dn->dn_objset == tx->tx_objset->os);
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* XXX No checking on the meta dnode for now */
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT(dn == NULL || dn->dn_assigned_txg == tx->tx_txg);
8a2f1b9190d1dc288470a1fd2776d79ce82cb129ahrens if (txh->txh_dnode == dn && txh->txh_type != THT_NEWOBJECT)
8a2f1b9190d1dc288470a1fd2776d79ce82cb129ahrens if (txh->txh_dnode == NULL || txh->txh_dnode == dn) {
8a2f1b9190d1dc288470a1fd2776d79ce82cb129ahrens /* XXX txh_arg2 better not be zero... */
8a2f1b9190d1dc288470a1fd2776d79ce82cb129ahrens dprintf("found txh type %x beginblk=%llx endblk=%llx\n",
fa9e4066f08beec538e775443c5be79dd423fcabahrens * We will let this hold work for the bonus
fa9e4066f08beec538e775443c5be79dd423fcabahrens * buffer so that we don't need to hold it
fa9e4066f08beec538e775443c5be79dd423fcabahrens * when creating a new object.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * They might have to increase nlevels,
fa9e4066f08beec538e775443c5be79dd423fcabahrens * thus dirtying the new TLIBs. Or the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * might have to change the block size,
fa9e4066f08beec538e775443c5be79dd423fcabahrens * thus dirying the new lvl=0 blk=0.
fa9e4066f08beec538e775443c5be79dd423fcabahrens panic("dirtying dbuf obj=%llx lvl=%u blkid=%llx but not tx_held\n",
0a4e9518a44f226be6d39383330b5b1792d2f184gw * If the user has indicated a blocking failure mode
0a4e9518a44f226be6d39383330b5b1792d2f184gw * then return ERESTART which will block in dmu_tx_wait().
0a4e9518a44f226be6d39383330b5b1792d2f184gw * Otherwise, return EIO so that an error can get
0a4e9518a44f226be6d39383330b5b1792d2f184gw * propagated back to the VOP calls.
0a4e9518a44f226be6d39383330b5b1792d2f184gw * Note that we always honor the txg_how flag regardless
0a4e9518a44f226be6d39383330b5b1792d2f184gw * of the failuremode setting.
0a4e9518a44f226be6d39383330b5b1792d2f184gw if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE &&
fa9e4066f08beec538e775443c5be79dd423fcabahrens tx->tx_txg = txg_hold_open(tx->tx_pool, &tx->tx_txgh);
8a2f1b9190d1dc288470a1fd2776d79ce82cb129ahrens * NB: No error returns are allowed after txg_hold_open, but
8a2f1b9190d1dc288470a1fd2776d79ce82cb129ahrens * before processing the dnode holds, due to the
8a2f1b9190d1dc288470a1fd2776d79ce82cb129ahrens * dmu_tx_unassign() logic.
8a2f1b9190d1dc288470a1fd2776d79ce82cb129ahrens * NB: This check must be after we've held the dnodes, so that
8a2f1b9190d1dc288470a1fd2776d79ce82cb129ahrens * the dmu_tx_unassign() logic will work properly
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * If a snapshot has been taken since we made our estimates,
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * assume that we won't be able to free or overwrite anything.
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock dsl_dataset_prev_snap_txg(tx->tx_objset->os->os_dsl_dataset) >
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Convert logical size to worst-case allocated size.
8a2f1b9190d1dc288470a1fd2776d79ce82cb129ahrens fsize = spa_get_asize(tx->tx_pool->dp_spa, tooverwrite) + tofree;
a9799022bd90b13722204e80112efaa5bf573099ck lsize, asize, fsize, usize, &tx->tx_tempreserve_cookie, tx);
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (0);
8a2f1b9190d1dc288470a1fd2776d79ce82cb129ahrens for (txh = list_head(&tx->tx_holds); txh != tx->tx_needassign_txh;
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Assign tx to a transaction group. txg_how can be one of:
fa9e4066f08beec538e775443c5be79dd423fcabahrens * (1) TXG_WAIT. If the current open txg is full, waits until there's
fa9e4066f08beec538e775443c5be79dd423fcabahrens * a new one. This should be used when you're not holding locks.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If will only fail if we're truly out of space (or over quota).
fa9e4066f08beec538e775443c5be79dd423fcabahrens * (2) TXG_NOWAIT. If we can't assign into the current open txg without
fa9e4066f08beec538e775443c5be79dd423fcabahrens * blocking, returns immediately with ERESTART. This should be used
fa9e4066f08beec538e775443c5be79dd423fcabahrens * whenever you're holding locks. On an ERESTART error, the caller
8a2f1b9190d1dc288470a1fd2776d79ce82cb129ahrens * should drop locks, do a dmu_tx_wait(tx), and try again.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * (3) A specific txg. Use this if you need to ensure that multiple
fa9e4066f08beec538e775443c5be79dd423fcabahrens * transactions all sync in the same txg. Like TXG_NOWAIT, it
fa9e4066f08beec538e775443c5be79dd423fcabahrens * returns ERESTART if it can't assign you into the requested txg.
8a2f1b9190d1dc288470a1fd2776d79ce82cb129ahrens while ((err = dmu_tx_try_assign(tx, txg_how)) != 0) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (0);
0a4e9518a44f226be6d39383330b5b1792d2f184gw * It's possible that the pool has become active after this thread
0a4e9518a44f226be6d39383330b5b1792d2f184gw * has tried to obtain a tx. If that's the case then his
0a4e9518a44f226be6d39383330b5b1792d2f184gw * tx_lasttried_txg would not have been assigned.
0a4e9518a44f226be6d39383330b5b1792d2f184gw txg_wait_synced(tx->tx_pool, spa_last_synced_txg(spa) + 1);
8a2f1b9190d1dc288470a1fd2776d79ce82cb129ahrens while (dn->dn_assigned_txg == tx->tx_lasttried_txg - 1)
8a2f1b9190d1dc288470a1fd2776d79ce82cb129ahrens txg_wait_open(tx->tx_pool, tx->tx_lasttried_txg + 1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT3U(refcount_count(&tx->tx_space_written) + delta, <=,
fa9e4066f08beec538e775443c5be79dd423fcabahrens (void) refcount_add_many(&tx->tx_space_written, delta, NULL);
fa9e4066f08beec538e775443c5be79dd423fcabahrens (void) refcount_add_many(&tx->tx_space_freed, -delta, NULL);
fa9e4066f08beec538e775443c5be79dd423fcabahrens dsl_dir_tempreserve_clear(tx->tx_tempreserve_cookie, tx);
fa9e4066f08beec538e775443c5be79dd423fcabahrens dprintf("towrite=%llu written=%llu tofree=%llu freed=%llu\n",
fa9e4066f08beec538e775443c5be79dd423fcabahrens tx->tx_space_towrite, refcount_count(&tx->tx_space_written),