dmu_tx.c revision 347a31bcb38b51837caee115d3979d3a981cc099
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
f65e61c04bc28ffd6bda04619c84330b420450b5ahrens * Copyright 2006 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_*() */
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);
fa9e4066f08beec538e775443c5be79dd423fcabahrens dmu_tx_t *tx = dmu_tx_create_ds(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,
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.
fa9e4066f08beec538e775443c5be79dd423fcabahrens dmu_tx_hold_object_impl(tx, os, object, THT_NEWOBJECT,
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrockdmu_tx_check_ioerr(zio_t *zio, dnode_t *dn, int level, uint64_t blkid)
fa9e4066f08beec538e775443c5be79dd423fcabahrens/* ARGSUSED */
fa9e4066f08beec538e775443c5be79dd423fcabahrensdmu_tx_count_write(dmu_tx_t *tx, dnode_t *dn, uint64_t off, uint64_t len)
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * For i/o error checking, read the first and last level-0
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * blocks, and all the level-1 blocks. We needn't do this on
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * the meta-dnode, because we've already read it in.
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock if (dn && dn->dn_object != DMU_META_DNODE_OBJECT) {
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.
fa9e4066f08beec538e775443c5be79dd423fcabahrens uint64_t object = dn ? dn->dn_object : DN_MAX_OBJECT - 1;
fa9e4066f08beec538e775443c5be79dd423fcabahrens dmu_tx_count_write(tx, mdn, object << DNODE_SHIFT, 1 << DNODE_SHIFT);
fa9e4066f08beec538e775443c5be79dd423fcabahrens dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset,
fa9e4066f08beec538e775443c5be79dd423fcabahrens/* ARGSUSED */
fa9e4066f08beec538e775443c5be79dd423fcabahrensdmu_tx_hold_write_impl(dmu_tx_t *tx, dnode_t *dn, uint64_t off, uint64_t len)
fa9e4066f08beec538e775443c5be79dd423fcabahrensdmu_tx_hold_write(dmu_tx_t *tx, uint64_t object, uint64_t off, int len)
fa9e4066f08beec538e775443c5be79dd423fcabahrens dmu_tx_hold_object_impl(tx, tx->tx_objset, object, THT_WRITE,
fa9e4066f08beec538e775443c5be79dd423fcabahrensdmu_tx_count_free(dmu_tx_t *tx, dnode_t *dn, 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
347a31bcb38b51837caee115d3979d3a981cc099ahrens * dirty immediately after our check anyway. However, we need
347a31bcb38b51837caee115d3979d3a981cc099ahrens * the lock to ensure that the link isn't changing while we call
347a31bcb38b51837caee115d3979d3a981cc099ahrens * list_link_active(), to satisfy its assertions. This is just
347a31bcb38b51837caee115d3979d3a981cc099ahrens * a 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.
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++) {
fa9e4066f08beec538e775443c5be79dd423fcabahrensdmu_tx_hold_free_impl(dmu_tx_t *tx, dnode_t *dn, 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.
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock shift = dn->dn_datablkshift + dn->dn_indblkshift - SPA_BLKPTRSHIFT;
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock end = dn->dn_datablkshift ? ((off+len) >> shift) : 0;
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock zio = zio_root(tx->tx_pool->dp_spa, NULL, NULL, ZIO_FLAG_CANFAIL);
fa9e4066f08beec538e775443c5be79dd423fcabahrensdmu_tx_hold_free(dmu_tx_t *tx, uint64_t object, uint64_t off, uint64_t len)
fa9e4066f08beec538e775443c5be79dd423fcabahrens dmu_tx_hold_object_impl(tx, tx->tx_objset, object, THT_FREE,
fa9e4066f08beec538e775443c5be79dd423fcabahrens/* ARGSUSED */
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrockdmu_tx_hold_zap_impl(dmu_tx_t *tx, dnode_t *dn, uint64_t add, uint64_t iname)
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.
f65e61c04bc28ffd6bda04619c84330b420450b5ahrens dmu_tx_count_write(tx, dn, 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.
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
fa9e4066f08beec538e775443c5be79dd423fcabahrens dmu_tx_count_write(tx, dn, 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)
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrockdmu_tx_hold_zap(dmu_tx_t *tx, uint64_t object, int add, char *name)
fa9e4066f08beec538e775443c5be79dd423fcabahrens dmu_tx_hold_object_impl(tx, tx->tx_objset, object, THT_ZAP,
fa9e4066f08beec538e775443c5be79dd423fcabahrens dmu_tx_hold_object_impl(tx, tx->tx_objset, object, THT_BONUS,
fa9e4066f08beec538e775443c5be79dd423fcabahrens/* ARGSUSED */
fa9e4066f08beec538e775443c5be79dd423fcabahrens dmu_tx_hold_object_impl(tx, tx->tx_objset, DMU_NEW_OBJECT, THT_SPACE,
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); */
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (dth->dth_dnode && dth->dth_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);
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (dth->dth_dnode == dn && dth->dth_type != THT_NEWOBJECT)
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (dth->dth_dnode == NULL || dth->dth_dnode == dn) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* XXX dth_arg2 better not be zero... */
fa9e4066f08beec538e775443c5be79dd423fcabahrens dprintf("found dth 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",
fa9e4066f08beec538e775443c5be79dd423fcabahrensdmu_tx_try_assign(dmu_tx_t *tx, uint64_t txg_how, dmu_tx_hold_t **last_dth)
fa9e4066f08beec538e775443c5be79dd423fcabahrens tx->tx_txg = txg_hold_open(tx->tx_pool, &tx->tx_txgh);
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.
fa9e4066f08beec538e775443c5be79dd423fcabahrens fsize = spa_get_asize(tx->tx_pool->dp_spa, tx->tx_space_tooverwrite) +
fa9e4066f08beec538e775443c5be79dd423fcabahrens lsize = tx->tx_space_towrite + tx->tx_space_tooverwrite;
fa9e4066f08beec538e775443c5be79dd423fcabahrens lsize, asize, fsize, &tx->tx_tempreserve_cookie, tx);
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (0);
fa9e4066f08beec538e775443c5be79dd423fcabahrensdmu_tx_unassign(dmu_tx_t *tx, dmu_tx_hold_t *last_dth)
fa9e4066f08beec538e775443c5be79dd423fcabahrens for (dth = last_dth; dth; dth = list_prev(&tx->tx_holds, dth)) {
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
fa9e4066f08beec538e775443c5be79dd423fcabahrens * should drop locks, do a txg_wait_open(dp, 0), 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.
fa9e4066f08beec538e775443c5be79dd423fcabahrens while ((err = dmu_tx_try_assign(tx, txg_how, &last_dth)) != 0) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (0);
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),