1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5f65e61c0Sahrens * Common Development and Distribution License (the "License"). 6f65e61c0Sahrens * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 2201025c89SJohn Harres * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 239dccfd2aSAlbert Lee * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 24*ad135b5dSChristopher Siden * Copyright (c) 2012 by Delphix. All rights reserved. 259dccfd2aSAlbert Lee */ 26fa9e4066Sahrens 27fa9e4066Sahrens #include <sys/dmu.h> 28fa9e4066Sahrens #include <sys/dmu_impl.h> 29fa9e4066Sahrens #include <sys/dbuf.h> 30fa9e4066Sahrens #include <sys/dmu_tx.h> 31fa9e4066Sahrens #include <sys/dmu_objset.h> 32fa9e4066Sahrens #include <sys/dsl_dataset.h> /* for dsl_dataset_block_freeable() */ 33fa9e4066Sahrens #include <sys/dsl_dir.h> /* for dsl_dir_tempreserve_*() */ 34fa9e4066Sahrens #include <sys/dsl_pool.h> 358a2f1b91Sahrens #include <sys/zap_impl.h> /* for fzap_default_block_shift */ 36fa9e4066Sahrens #include <sys/spa.h> 370a586ceaSMark Shellenbaum #include <sys/sa.h> 380a586ceaSMark Shellenbaum #include <sys/sa_impl.h> 39fa9e4066Sahrens #include <sys/zfs_context.h> 400a586ceaSMark Shellenbaum #include <sys/varargs.h> 41fa9e4066Sahrens 42ea8dc4b6Seschrock typedef void (*dmu_tx_hold_func_t)(dmu_tx_t *tx, struct dnode *dn, 43ea8dc4b6Seschrock uint64_t arg1, uint64_t arg2); 44ea8dc4b6Seschrock 45fa9e4066Sahrens 46fa9e4066Sahrens dmu_tx_t * 471d452cf5Sahrens dmu_tx_create_dd(dsl_dir_t *dd) 48fa9e4066Sahrens { 49fa9e4066Sahrens dmu_tx_t *tx = kmem_zalloc(sizeof (dmu_tx_t), KM_SLEEP); 50fa9e4066Sahrens tx->tx_dir = dd; 51fa9e4066Sahrens if (dd) 52fa9e4066Sahrens tx->tx_pool = dd->dd_pool; 53fa9e4066Sahrens list_create(&tx->tx_holds, sizeof (dmu_tx_hold_t), 548a2f1b91Sahrens offsetof(dmu_tx_hold_t, txh_node)); 55d20e665cSRicardo M. Correia list_create(&tx->tx_callbacks, sizeof (dmu_tx_callback_t), 56d20e665cSRicardo M. Correia offsetof(dmu_tx_callback_t, dcb_node)); 578a2f1b91Sahrens #ifdef ZFS_DEBUG 58fa9e4066Sahrens refcount_create(&tx->tx_space_written); 59fa9e4066Sahrens refcount_create(&tx->tx_space_freed); 608a2f1b91Sahrens #endif 61fa9e4066Sahrens return (tx); 62fa9e4066Sahrens } 63fa9e4066Sahrens 64fa9e4066Sahrens dmu_tx_t * 65fa9e4066Sahrens dmu_tx_create(objset_t *os) 66fa9e4066Sahrens { 67503ad85cSMatthew Ahrens dmu_tx_t *tx = dmu_tx_create_dd(os->os_dsl_dataset->ds_dir); 68fa9e4066Sahrens tx->tx_objset = os; 69503ad85cSMatthew Ahrens tx->tx_lastsnap_txg = dsl_dataset_prev_snap_txg(os->os_dsl_dataset); 70fa9e4066Sahrens return (tx); 71fa9e4066Sahrens } 72fa9e4066Sahrens 73fa9e4066Sahrens dmu_tx_t * 74fa9e4066Sahrens dmu_tx_create_assigned(struct dsl_pool *dp, uint64_t txg) 75fa9e4066Sahrens { 761d452cf5Sahrens dmu_tx_t *tx = dmu_tx_create_dd(NULL); 77fa9e4066Sahrens 78fa9e4066Sahrens ASSERT3U(txg, <=, dp->dp_tx.tx_open_txg); 79fa9e4066Sahrens tx->tx_pool = dp; 80fa9e4066Sahrens tx->tx_txg = txg; 81fa9e4066Sahrens tx->tx_anyobj = TRUE; 82fa9e4066Sahrens 83fa9e4066Sahrens return (tx); 84fa9e4066Sahrens } 85fa9e4066Sahrens 86fa9e4066Sahrens int 87fa9e4066Sahrens dmu_tx_is_syncing(dmu_tx_t *tx) 88fa9e4066Sahrens { 89fa9e4066Sahrens return (tx->tx_anyobj); 90fa9e4066Sahrens } 91fa9e4066Sahrens 92fa9e4066Sahrens int 93fa9e4066Sahrens dmu_tx_private_ok(dmu_tx_t *tx) 94fa9e4066Sahrens { 95ea8dc4b6Seschrock return (tx->tx_anyobj); 96fa9e4066Sahrens } 97fa9e4066Sahrens 988a2f1b91Sahrens static dmu_tx_hold_t * 99fa9e4066Sahrens dmu_tx_hold_object_impl(dmu_tx_t *tx, objset_t *os, uint64_t object, 1008a2f1b91Sahrens enum dmu_tx_hold_type type, uint64_t arg1, uint64_t arg2) 101fa9e4066Sahrens { 1028a2f1b91Sahrens dmu_tx_hold_t *txh; 103fa9e4066Sahrens dnode_t *dn = NULL; 104ea8dc4b6Seschrock int err; 105fa9e4066Sahrens 106fa9e4066Sahrens if (object != DMU_NEW_OBJECT) { 107503ad85cSMatthew Ahrens err = dnode_hold(os, object, tx, &dn); 108ea8dc4b6Seschrock if (err) { 109ea8dc4b6Seschrock tx->tx_err = err; 1108a2f1b91Sahrens return (NULL); 111ea8dc4b6Seschrock } 112fa9e4066Sahrens 113ea8dc4b6Seschrock if (err == 0 && tx->tx_txg != 0) { 114fa9e4066Sahrens mutex_enter(&dn->dn_mtx); 115fa9e4066Sahrens /* 116fa9e4066Sahrens * dn->dn_assigned_txg == tx->tx_txg doesn't pose a 117fa9e4066Sahrens * problem, but there's no way for it to happen (for 118fa9e4066Sahrens * now, at least). 119fa9e4066Sahrens */ 120fa9e4066Sahrens ASSERT(dn->dn_assigned_txg == 0); 121fa9e4066Sahrens dn->dn_assigned_txg = tx->tx_txg; 122fa9e4066Sahrens (void) refcount_add(&dn->dn_tx_holds, tx); 123fa9e4066Sahrens mutex_exit(&dn->dn_mtx); 124fa9e4066Sahrens } 125fa9e4066Sahrens } 126fa9e4066Sahrens 1278a2f1b91Sahrens txh = kmem_zalloc(sizeof (dmu_tx_hold_t), KM_SLEEP); 1288a2f1b91Sahrens txh->txh_tx = tx; 1298a2f1b91Sahrens txh->txh_dnode = dn; 1308a2f1b91Sahrens #ifdef ZFS_DEBUG 1318a2f1b91Sahrens txh->txh_type = type; 1328a2f1b91Sahrens txh->txh_arg1 = arg1; 1338a2f1b91Sahrens txh->txh_arg2 = arg2; 1348a2f1b91Sahrens #endif 1358a2f1b91Sahrens list_insert_tail(&tx->tx_holds, txh); 136ea8dc4b6Seschrock 1378a2f1b91Sahrens return (txh); 138fa9e4066Sahrens } 139fa9e4066Sahrens 140fa9e4066Sahrens void 141fa9e4066Sahrens dmu_tx_add_new_object(dmu_tx_t *tx, objset_t *os, uint64_t object) 142fa9e4066Sahrens { 143fa9e4066Sahrens /* 144fa9e4066Sahrens * If we're syncing, they can manipulate any object anyhow, and 145fa9e4066Sahrens * the hold on the dnode_t can cause problems. 146fa9e4066Sahrens */ 147fa9e4066Sahrens if (!dmu_tx_is_syncing(tx)) { 1488a2f1b91Sahrens (void) dmu_tx_hold_object_impl(tx, os, 1498a2f1b91Sahrens object, THT_NEWOBJECT, 0, 0); 150fa9e4066Sahrens } 151fa9e4066Sahrens } 152fa9e4066Sahrens 153ea8dc4b6Seschrock static int 154ea8dc4b6Seschrock dmu_tx_check_ioerr(zio_t *zio, dnode_t *dn, int level, uint64_t blkid) 155ea8dc4b6Seschrock { 156ea8dc4b6Seschrock int err; 157ea8dc4b6Seschrock dmu_buf_impl_t *db; 158ea8dc4b6Seschrock 159ea8dc4b6Seschrock rw_enter(&dn->dn_struct_rwlock, RW_READER); 160ea8dc4b6Seschrock db = dbuf_hold_level(dn, level, blkid, FTAG); 161ea8dc4b6Seschrock rw_exit(&dn->dn_struct_rwlock); 162ea8dc4b6Seschrock if (db == NULL) 163ea8dc4b6Seschrock return (EIO); 1641ab7f2deSmaybee err = dbuf_read(db, zio, DB_RF_CANFAIL | DB_RF_NOPREFETCH); 165ea8dc4b6Seschrock dbuf_rele(db, FTAG); 166ea8dc4b6Seschrock return (err); 167ea8dc4b6Seschrock } 168ea8dc4b6Seschrock 1694a7f2a75SMark Maybee static void 170b24ab676SJeff Bonwick dmu_tx_count_twig(dmu_tx_hold_t *txh, dnode_t *dn, dmu_buf_impl_t *db, 171b24ab676SJeff Bonwick int level, uint64_t blkid, boolean_t freeable, uint64_t *history) 1724a7f2a75SMark Maybee { 173b24ab676SJeff Bonwick objset_t *os = dn->dn_objset; 174b24ab676SJeff Bonwick dsl_dataset_t *ds = os->os_dsl_dataset; 175b24ab676SJeff Bonwick int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 176b24ab676SJeff Bonwick dmu_buf_impl_t *parent = NULL; 177b24ab676SJeff Bonwick blkptr_t *bp = NULL; 178b24ab676SJeff Bonwick uint64_t space; 179b24ab676SJeff Bonwick 180b24ab676SJeff Bonwick if (level >= dn->dn_nlevels || history[level] == blkid) 1814a7f2a75SMark Maybee return; 1824a7f2a75SMark Maybee 183b24ab676SJeff Bonwick history[level] = blkid; 1844a7f2a75SMark Maybee 185b24ab676SJeff Bonwick space = (level == 0) ? dn->dn_datablksz : (1ULL << dn->dn_indblkshift); 1864a7f2a75SMark Maybee 187b24ab676SJeff Bonwick if (db == NULL || db == dn->dn_dbuf) { 188b24ab676SJeff Bonwick ASSERT(level != 0); 189b24ab676SJeff Bonwick db = NULL; 190b24ab676SJeff Bonwick } else { 191744947dcSTom Erickson ASSERT(DB_DNODE(db) == dn); 192b24ab676SJeff Bonwick ASSERT(db->db_level == level); 193b24ab676SJeff Bonwick ASSERT(db->db.db_size == space); 194b24ab676SJeff Bonwick ASSERT(db->db_blkid == blkid); 195b24ab676SJeff Bonwick bp = db->db_blkptr; 196b24ab676SJeff Bonwick parent = db->db_parent; 1974a7f2a75SMark Maybee } 198b24ab676SJeff Bonwick 199b24ab676SJeff Bonwick freeable = (bp && (freeable || 200c7cd2421SGeorge Wilson dsl_dataset_block_freeable(ds, bp, bp->blk_birth))); 201b24ab676SJeff Bonwick 202b24ab676SJeff Bonwick if (freeable) 203b24ab676SJeff Bonwick txh->txh_space_tooverwrite += space; 204b24ab676SJeff Bonwick else 205b24ab676SJeff Bonwick txh->txh_space_towrite += space; 206b24ab676SJeff Bonwick if (bp) 207b24ab676SJeff Bonwick txh->txh_space_tounref += bp_get_dsize(os->os_spa, bp); 208b24ab676SJeff Bonwick 209b24ab676SJeff Bonwick dmu_tx_count_twig(txh, dn, parent, level + 1, 210b24ab676SJeff Bonwick blkid >> epbs, freeable, history); 2114a7f2a75SMark Maybee } 2124a7f2a75SMark Maybee 213fa9e4066Sahrens /* ARGSUSED */ 214fa9e4066Sahrens static void 2158a2f1b91Sahrens dmu_tx_count_write(dmu_tx_hold_t *txh, uint64_t off, uint64_t len) 216fa9e4066Sahrens { 2178a2f1b91Sahrens dnode_t *dn = txh->txh_dnode; 2188a2f1b91Sahrens uint64_t start, end, i; 219fa9e4066Sahrens int min_bs, max_bs, min_ibs, max_ibs, epbs, bits; 2208a2f1b91Sahrens int err = 0; 221fa9e4066Sahrens 222fa9e4066Sahrens if (len == 0) 223fa9e4066Sahrens return; 224fa9e4066Sahrens 225fa9e4066Sahrens min_bs = SPA_MINBLOCKSHIFT; 226fa9e4066Sahrens max_bs = SPA_MAXBLOCKSHIFT; 227fa9e4066Sahrens min_ibs = DN_MIN_INDBLKSHIFT; 228fa9e4066Sahrens max_ibs = DN_MAX_INDBLKSHIFT; 229fa9e4066Sahrens 2308a2f1b91Sahrens if (dn) { 231b24ab676SJeff Bonwick uint64_t history[DN_MAX_LEVELS]; 2324a7f2a75SMark Maybee int nlvls = dn->dn_nlevels; 2334a7f2a75SMark Maybee int delta; 2344a7f2a75SMark Maybee 2354a7f2a75SMark Maybee /* 2364a7f2a75SMark Maybee * For i/o error checking, read the first and last level-0 2374a7f2a75SMark Maybee * blocks (if they are not aligned), and all the level-1 blocks. 2384a7f2a75SMark Maybee */ 239ea8dc4b6Seschrock if (dn->dn_maxblkid == 0) { 2404a7f2a75SMark Maybee delta = dn->dn_datablksz; 2414a7f2a75SMark Maybee start = (off < dn->dn_datablksz) ? 0 : 1; 2424a7f2a75SMark Maybee end = (off+len <= dn->dn_datablksz) ? 0 : 1; 2434a7f2a75SMark Maybee if (start == 0 && (off > 0 || len < dn->dn_datablksz)) { 24482c9918fSTim Haley err = dmu_tx_check_ioerr(NULL, dn, 0, 0); 24582c9918fSTim Haley if (err) 24682c9918fSTim Haley goto out; 2474a7f2a75SMark Maybee delta -= off; 24882c9918fSTim Haley } 249ea8dc4b6Seschrock } else { 2508a2f1b91Sahrens zio_t *zio = zio_root(dn->dn_objset->os_spa, 251ea8dc4b6Seschrock NULL, NULL, ZIO_FLAG_CANFAIL); 252ea8dc4b6Seschrock 253ea8dc4b6Seschrock /* first level-0 block */ 25499653d4eSeschrock start = off >> dn->dn_datablkshift; 25599653d4eSeschrock if (P2PHASE(off, dn->dn_datablksz) || 25699653d4eSeschrock len < dn->dn_datablksz) { 25799653d4eSeschrock err = dmu_tx_check_ioerr(zio, dn, 0, start); 2588a2f1b91Sahrens if (err) 2598a2f1b91Sahrens goto out; 260ea8dc4b6Seschrock } 261ea8dc4b6Seschrock 262ea8dc4b6Seschrock /* last level-0 block */ 26399653d4eSeschrock end = (off+len-1) >> dn->dn_datablkshift; 26482c9918fSTim Haley if (end != start && end <= dn->dn_maxblkid && 26599653d4eSeschrock P2PHASE(off+len, dn->dn_datablksz)) { 266ea8dc4b6Seschrock err = dmu_tx_check_ioerr(zio, dn, 0, end); 2678a2f1b91Sahrens if (err) 2688a2f1b91Sahrens goto out; 269ea8dc4b6Seschrock } 270ea8dc4b6Seschrock 271ea8dc4b6Seschrock /* level-1 blocks */ 2724a7f2a75SMark Maybee if (nlvls > 1) { 2734a7f2a75SMark Maybee int shft = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 2744a7f2a75SMark Maybee for (i = (start>>shft)+1; i < end>>shft; i++) { 275ea8dc4b6Seschrock err = dmu_tx_check_ioerr(zio, dn, 1, i); 2768a2f1b91Sahrens if (err) 2778a2f1b91Sahrens goto out; 278ea8dc4b6Seschrock } 279ea8dc4b6Seschrock } 280ea8dc4b6Seschrock 281ea8dc4b6Seschrock err = zio_wait(zio); 2828a2f1b91Sahrens if (err) 2838a2f1b91Sahrens goto out; 2844a7f2a75SMark Maybee delta = P2NPHASE(off, dn->dn_datablksz); 285ea8dc4b6Seschrock } 286ea8dc4b6Seschrock 2874a7f2a75SMark Maybee if (dn->dn_maxblkid > 0) { 2884a7f2a75SMark Maybee /* 2894a7f2a75SMark Maybee * The blocksize can't change, 2904a7f2a75SMark Maybee * so we can make a more precise estimate. 2914a7f2a75SMark Maybee */ 2924a7f2a75SMark Maybee ASSERT(dn->dn_datablkshift != 0); 293fa9e4066Sahrens min_bs = max_bs = dn->dn_datablkshift; 2944a7f2a75SMark Maybee min_ibs = max_ibs = dn->dn_indblkshift; 2954a7f2a75SMark Maybee } else if (dn->dn_indblkshift > max_ibs) { 2964a7f2a75SMark Maybee /* 2974a7f2a75SMark Maybee * This ensures that if we reduce DN_MAX_INDBLKSHIFT, 2984a7f2a75SMark Maybee * the code will still work correctly on older pools. 2994a7f2a75SMark Maybee */ 3004a7f2a75SMark Maybee min_ibs = max_ibs = dn->dn_indblkshift; 3014a7f2a75SMark Maybee } 3024a7f2a75SMark Maybee 3034a7f2a75SMark Maybee /* 3044a7f2a75SMark Maybee * If this write is not off the end of the file 3054a7f2a75SMark Maybee * we need to account for overwrites/unref. 3064a7f2a75SMark Maybee */ 307b24ab676SJeff Bonwick if (start <= dn->dn_maxblkid) { 308b24ab676SJeff Bonwick for (int l = 0; l < DN_MAX_LEVELS; l++) 309b24ab676SJeff Bonwick history[l] = -1ULL; 310b24ab676SJeff Bonwick } 3114a7f2a75SMark Maybee while (start <= dn->dn_maxblkid) { 3124a7f2a75SMark Maybee dmu_buf_impl_t *db; 3134a7f2a75SMark Maybee 3144a7f2a75SMark Maybee rw_enter(&dn->dn_struct_rwlock, RW_READER); 31501025c89SJohn Harres err = dbuf_hold_impl(dn, 0, start, FALSE, FTAG, &db); 3164a7f2a75SMark Maybee rw_exit(&dn->dn_struct_rwlock); 31701025c89SJohn Harres 31801025c89SJohn Harres if (err) { 31901025c89SJohn Harres txh->txh_tx->tx_err = err; 32001025c89SJohn Harres return; 32101025c89SJohn Harres } 32201025c89SJohn Harres 323b24ab676SJeff Bonwick dmu_tx_count_twig(txh, dn, db, 0, start, B_FALSE, 324b24ab676SJeff Bonwick history); 3254a7f2a75SMark Maybee dbuf_rele(db, FTAG); 3264a7f2a75SMark Maybee if (++start > end) { 3274a7f2a75SMark Maybee /* 3284a7f2a75SMark Maybee * Account for new indirects appearing 3294a7f2a75SMark Maybee * before this IO gets assigned into a txg. 3304a7f2a75SMark Maybee */ 3314a7f2a75SMark Maybee bits = 64 - min_bs; 3324a7f2a75SMark Maybee epbs = min_ibs - SPA_BLKPTRSHIFT; 3334a7f2a75SMark Maybee for (bits -= epbs * (nlvls - 1); 3344a7f2a75SMark Maybee bits >= 0; bits -= epbs) 3354a7f2a75SMark Maybee txh->txh_fudge += 1ULL << max_ibs; 3364a7f2a75SMark Maybee goto out; 3374a7f2a75SMark Maybee } 3384a7f2a75SMark Maybee off += delta; 3394a7f2a75SMark Maybee if (len >= delta) 3404a7f2a75SMark Maybee len -= delta; 3414a7f2a75SMark Maybee delta = dn->dn_datablksz; 3424a7f2a75SMark Maybee } 343fa9e4066Sahrens } 344fa9e4066Sahrens 345fa9e4066Sahrens /* 346fa9e4066Sahrens * 'end' is the last thing we will access, not one past. 347fa9e4066Sahrens * This way we won't overflow when accessing the last byte. 348fa9e4066Sahrens */ 349fa9e4066Sahrens start = P2ALIGN(off, 1ULL << max_bs); 350fa9e4066Sahrens end = P2ROUNDUP(off + len, 1ULL << max_bs) - 1; 3518a2f1b91Sahrens txh->txh_space_towrite += end - start + 1; 352fa9e4066Sahrens 353fa9e4066Sahrens start >>= min_bs; 354fa9e4066Sahrens end >>= min_bs; 355fa9e4066Sahrens 356fa9e4066Sahrens epbs = min_ibs - SPA_BLKPTRSHIFT; 357fa9e4066Sahrens 358fa9e4066Sahrens /* 359fa9e4066Sahrens * The object contains at most 2^(64 - min_bs) blocks, 360fa9e4066Sahrens * and each indirect level maps 2^epbs. 361fa9e4066Sahrens */ 362fa9e4066Sahrens for (bits = 64 - min_bs; bits >= 0; bits -= epbs) { 363fa9e4066Sahrens start >>= epbs; 364fa9e4066Sahrens end >>= epbs; 3654a7f2a75SMark Maybee ASSERT3U(end, >=, start); 3668a2f1b91Sahrens txh->txh_space_towrite += (end - start + 1) << max_ibs; 3674a7f2a75SMark Maybee if (start != 0) { 3684a7f2a75SMark Maybee /* 3694a7f2a75SMark Maybee * We also need a new blkid=0 indirect block 3704a7f2a75SMark Maybee * to reference any existing file data. 3714a7f2a75SMark Maybee */ 3724a7f2a75SMark Maybee txh->txh_space_towrite += 1ULL << max_ibs; 3734a7f2a75SMark Maybee } 374fa9e4066Sahrens } 375fa9e4066Sahrens 3768a2f1b91Sahrens out: 3774a7f2a75SMark Maybee if (txh->txh_space_towrite + txh->txh_space_tooverwrite > 3784a7f2a75SMark Maybee 2 * DMU_MAX_ACCESS) 3794a7f2a75SMark Maybee err = EFBIG; 3804a7f2a75SMark Maybee 3818a2f1b91Sahrens if (err) 3828a2f1b91Sahrens txh->txh_tx->tx_err = err; 383fa9e4066Sahrens } 384fa9e4066Sahrens 385fa9e4066Sahrens static void 3868a2f1b91Sahrens dmu_tx_count_dnode(dmu_tx_hold_t *txh) 387fa9e4066Sahrens { 3888a2f1b91Sahrens dnode_t *dn = txh->txh_dnode; 389744947dcSTom Erickson dnode_t *mdn = DMU_META_DNODE(txh->txh_tx->tx_objset); 3908a2f1b91Sahrens uint64_t space = mdn->dn_datablksz + 3918a2f1b91Sahrens ((mdn->dn_nlevels-1) << mdn->dn_indblkshift); 392fa9e4066Sahrens 393fa9e4066Sahrens if (dn && dn->dn_dbuf->db_blkptr && 394fa9e4066Sahrens dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset, 395c7cd2421SGeorge Wilson dn->dn_dbuf->db_blkptr, dn->dn_dbuf->db_blkptr->blk_birth)) { 3968a2f1b91Sahrens txh->txh_space_tooverwrite += space; 3974a7f2a75SMark Maybee txh->txh_space_tounref += space; 3988a2f1b91Sahrens } else { 3998a2f1b91Sahrens txh->txh_space_towrite += space; 400a9799022Sck if (dn && dn->dn_dbuf->db_blkptr) 401a9799022Sck txh->txh_space_tounref += space; 402fa9e4066Sahrens } 403fa9e4066Sahrens } 404fa9e4066Sahrens 405fa9e4066Sahrens void 406fa9e4066Sahrens dmu_tx_hold_write(dmu_tx_t *tx, uint64_t object, uint64_t off, int len) 407fa9e4066Sahrens { 4088a2f1b91Sahrens dmu_tx_hold_t *txh; 4098a2f1b91Sahrens 410fa9e4066Sahrens ASSERT(tx->tx_txg == 0); 411ea8dc4b6Seschrock ASSERT(len < DMU_MAX_ACCESS); 412dd6ef538Smaybee ASSERT(len == 0 || UINT64_MAX - off >= len - 1); 413fa9e4066Sahrens 4148a2f1b91Sahrens txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, 4158a2f1b91Sahrens object, THT_WRITE, off, len); 4168a2f1b91Sahrens if (txh == NULL) 4178a2f1b91Sahrens return; 4188a2f1b91Sahrens 4198a2f1b91Sahrens dmu_tx_count_write(txh, off, len); 4208a2f1b91Sahrens dmu_tx_count_dnode(txh); 421fa9e4066Sahrens } 422fa9e4066Sahrens 423fa9e4066Sahrens static void 4248a2f1b91Sahrens dmu_tx_count_free(dmu_tx_hold_t *txh, uint64_t off, uint64_t len) 425fa9e4066Sahrens { 426cdb0ab79Smaybee uint64_t blkid, nblks, lastblk; 427cdb0ab79Smaybee uint64_t space = 0, unref = 0, skipped = 0; 4288a2f1b91Sahrens dnode_t *dn = txh->txh_dnode; 429fa9e4066Sahrens dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset; 4308a2f1b91Sahrens spa_t *spa = txh->txh_tx->tx_pool->dp_spa; 431cdb0ab79Smaybee int epbs; 432fa9e4066Sahrens 433cdb0ab79Smaybee if (dn->dn_nlevels == 0) 434fa9e4066Sahrens return; 435c543ec06Sahrens 436fa9e4066Sahrens /* 437cdb0ab79Smaybee * The struct_rwlock protects us against dn_nlevels 438c543ec06Sahrens * changing, in case (against all odds) we manage to dirty & 439c543ec06Sahrens * sync out the changes after we check for being dirty. 44001025c89SJohn Harres * Also, dbuf_hold_impl() wants us to have the struct_rwlock. 441fa9e4066Sahrens */ 442fa9e4066Sahrens rw_enter(&dn->dn_struct_rwlock, RW_READER); 443cdb0ab79Smaybee epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 444cdb0ab79Smaybee if (dn->dn_maxblkid == 0) { 445c543ec06Sahrens if (off == 0 && len >= dn->dn_datablksz) { 446c543ec06Sahrens blkid = 0; 447c543ec06Sahrens nblks = 1; 448c543ec06Sahrens } else { 449c543ec06Sahrens rw_exit(&dn->dn_struct_rwlock); 450c543ec06Sahrens return; 451c543ec06Sahrens } 452c543ec06Sahrens } else { 453c543ec06Sahrens blkid = off >> dn->dn_datablkshift; 454cdb0ab79Smaybee nblks = (len + dn->dn_datablksz - 1) >> dn->dn_datablkshift; 455fa9e4066Sahrens 456cdb0ab79Smaybee if (blkid >= dn->dn_maxblkid) { 457c543ec06Sahrens rw_exit(&dn->dn_struct_rwlock); 458c543ec06Sahrens return; 459c543ec06Sahrens } 460cdb0ab79Smaybee if (blkid + nblks > dn->dn_maxblkid) 461cdb0ab79Smaybee nblks = dn->dn_maxblkid - blkid; 462fa9e4066Sahrens 463c543ec06Sahrens } 464cdb0ab79Smaybee if (dn->dn_nlevels == 1) { 465fa9e4066Sahrens int i; 466fa9e4066Sahrens for (i = 0; i < nblks; i++) { 467fa9e4066Sahrens blkptr_t *bp = dn->dn_phys->dn_blkptr; 468cdb0ab79Smaybee ASSERT3U(blkid + i, <, dn->dn_nblkptr); 469fa9e4066Sahrens bp += blkid + i; 470c7cd2421SGeorge Wilson if (dsl_dataset_block_freeable(ds, bp, bp->blk_birth)) { 471fa9e4066Sahrens dprintf_bp(bp, "can free old%s", ""); 472b24ab676SJeff Bonwick space += bp_get_dsize(spa, bp); 473fa9e4066Sahrens } 474a9799022Sck unref += BP_GET_ASIZE(bp); 475fa9e4066Sahrens } 476ea8dc4b6Seschrock nblks = 0; 477fa9e4066Sahrens } 478fa9e4066Sahrens 479cdb0ab79Smaybee /* 480715614a4Smaybee * Add in memory requirements of higher-level indirects. 481715614a4Smaybee * This assumes a worst-possible scenario for dn_nlevels. 482cdb0ab79Smaybee */ 483715614a4Smaybee { 484cdb0ab79Smaybee uint64_t blkcnt = 1 + ((nblks >> epbs) >> epbs); 485b7e50089Smaybee int level = (dn->dn_nlevels > 1) ? 2 : 1; 486cdb0ab79Smaybee 487715614a4Smaybee while (level++ < DN_MAX_LEVELS) { 488cdb0ab79Smaybee txh->txh_memory_tohold += blkcnt << dn->dn_indblkshift; 489cdb0ab79Smaybee blkcnt = 1 + (blkcnt >> epbs); 490cdb0ab79Smaybee } 491cdb0ab79Smaybee ASSERT(blkcnt <= dn->dn_nblkptr); 492cdb0ab79Smaybee } 493cdb0ab79Smaybee 494cdb0ab79Smaybee lastblk = blkid + nblks - 1; 495fa9e4066Sahrens while (nblks) { 496fa9e4066Sahrens dmu_buf_impl_t *dbuf; 497cdb0ab79Smaybee uint64_t ibyte, new_blkid; 498cdb0ab79Smaybee int epb = 1 << epbs; 499cdb0ab79Smaybee int err, i, blkoff, tochk; 500cdb0ab79Smaybee blkptr_t *bp; 501cdb0ab79Smaybee 502cdb0ab79Smaybee ibyte = blkid << dn->dn_datablkshift; 503cdb0ab79Smaybee err = dnode_next_offset(dn, 504cdb0ab79Smaybee DNODE_FIND_HAVELOCK, &ibyte, 2, 1, 0); 505cdb0ab79Smaybee new_blkid = ibyte >> dn->dn_datablkshift; 506b7e50089Smaybee if (err == ESRCH) { 507b7e50089Smaybee skipped += (lastblk >> epbs) - (blkid >> epbs) + 1; 508cdb0ab79Smaybee break; 509b7e50089Smaybee } 510cdb0ab79Smaybee if (err) { 511cdb0ab79Smaybee txh->txh_tx->tx_err = err; 512cdb0ab79Smaybee break; 513cdb0ab79Smaybee } 514b7e50089Smaybee if (new_blkid > lastblk) { 515b7e50089Smaybee skipped += (lastblk >> epbs) - (blkid >> epbs) + 1; 516cdb0ab79Smaybee break; 517b7e50089Smaybee } 518fa9e4066Sahrens 519cdb0ab79Smaybee if (new_blkid > blkid) { 520b7e50089Smaybee ASSERT((new_blkid >> epbs) > (blkid >> epbs)); 521b7e50089Smaybee skipped += (new_blkid >> epbs) - (blkid >> epbs) - 1; 522cdb0ab79Smaybee nblks -= new_blkid - blkid; 523cdb0ab79Smaybee blkid = new_blkid; 524cdb0ab79Smaybee } 525cdb0ab79Smaybee blkoff = P2PHASE(blkid, epb); 526cdb0ab79Smaybee tochk = MIN(epb - blkoff, nblks); 527fa9e4066Sahrens 52801025c89SJohn Harres err = dbuf_hold_impl(dn, 1, blkid >> epbs, FALSE, FTAG, &dbuf); 52901025c89SJohn Harres if (err) { 53001025c89SJohn Harres txh->txh_tx->tx_err = err; 53101025c89SJohn Harres break; 53201025c89SJohn Harres } 533cdb0ab79Smaybee 534cdb0ab79Smaybee txh->txh_memory_tohold += dbuf->db.db_size; 53577179d12SLori Alt 53677179d12SLori Alt /* 53777179d12SLori Alt * We don't check memory_tohold against DMU_MAX_ACCESS because 53877179d12SLori Alt * memory_tohold is an over-estimation (especially the >L1 53977179d12SLori Alt * indirect blocks), so it could fail. Callers should have 54077179d12SLori Alt * already verified that they will not be holding too much 54177179d12SLori Alt * memory. 54277179d12SLori Alt */ 54377179d12SLori Alt 544cdb0ab79Smaybee err = dbuf_read(dbuf, NULL, DB_RF_HAVESTRUCT | DB_RF_CANFAIL); 545cdb0ab79Smaybee if (err != 0) { 5468a2f1b91Sahrens txh->txh_tx->tx_err = err; 547cdb0ab79Smaybee dbuf_rele(dbuf, FTAG); 548c543ec06Sahrens break; 549fa9e4066Sahrens } 550fa9e4066Sahrens 551cdb0ab79Smaybee bp = dbuf->db.db_data; 552cdb0ab79Smaybee bp += blkoff; 553cdb0ab79Smaybee 554cdb0ab79Smaybee for (i = 0; i < tochk; i++) { 555c7cd2421SGeorge Wilson if (dsl_dataset_block_freeable(ds, &bp[i], 556c7cd2421SGeorge Wilson bp[i].blk_birth)) { 557cdb0ab79Smaybee dprintf_bp(&bp[i], "can free old%s", ""); 558b24ab676SJeff Bonwick space += bp_get_dsize(spa, &bp[i]); 559cdb0ab79Smaybee } 560cdb0ab79Smaybee unref += BP_GET_ASIZE(bp); 561cdb0ab79Smaybee } 562cdb0ab79Smaybee dbuf_rele(dbuf, FTAG); 563cdb0ab79Smaybee 564fa9e4066Sahrens blkid += tochk; 565fa9e4066Sahrens nblks -= tochk; 566fa9e4066Sahrens } 567fa9e4066Sahrens rw_exit(&dn->dn_struct_rwlock); 568fa9e4066Sahrens 569cdb0ab79Smaybee /* account for new level 1 indirect blocks that might show up */ 570b7e50089Smaybee if (skipped > 0) { 571715614a4Smaybee txh->txh_fudge += skipped << dn->dn_indblkshift; 572cdb0ab79Smaybee skipped = MIN(skipped, DMU_MAX_DELETEBLKCNT >> epbs); 573cdb0ab79Smaybee txh->txh_memory_tohold += skipped << dn->dn_indblkshift; 574cdb0ab79Smaybee } 5758a2f1b91Sahrens txh->txh_space_tofree += space; 576a9799022Sck txh->txh_space_tounref += unref; 577fa9e4066Sahrens } 578fa9e4066Sahrens 5798a2f1b91Sahrens void 5808a2f1b91Sahrens dmu_tx_hold_free(dmu_tx_t *tx, uint64_t object, uint64_t off, uint64_t len) 581fa9e4066Sahrens { 5828a2f1b91Sahrens dmu_tx_hold_t *txh; 5838a2f1b91Sahrens dnode_t *dn; 584ea8dc4b6Seschrock uint64_t start, end, i; 585c543ec06Sahrens int err, shift; 586ea8dc4b6Seschrock zio_t *zio; 587fa9e4066Sahrens 5888a2f1b91Sahrens ASSERT(tx->tx_txg == 0); 5898a2f1b91Sahrens 5908a2f1b91Sahrens txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, 5918a2f1b91Sahrens object, THT_FREE, off, len); 5928a2f1b91Sahrens if (txh == NULL) 5938a2f1b91Sahrens return; 5948a2f1b91Sahrens dn = txh->txh_dnode; 5958a2f1b91Sahrens 596fa9e4066Sahrens /* first block */ 59798572ac1Sahrens if (off != 0) 5988a2f1b91Sahrens dmu_tx_count_write(txh, off, 1); 599fa9e4066Sahrens /* last block */ 600fa9e4066Sahrens if (len != DMU_OBJECT_END) 6018a2f1b91Sahrens dmu_tx_count_write(txh, off+len, 1); 602fa9e4066Sahrens 603b24ab676SJeff Bonwick dmu_tx_count_dnode(txh); 604b24ab676SJeff Bonwick 605fa9e4066Sahrens if (off >= (dn->dn_maxblkid+1) * dn->dn_datablksz) 606fa9e4066Sahrens return; 607fa9e4066Sahrens if (len == DMU_OBJECT_END) 608fa9e4066Sahrens len = (dn->dn_maxblkid+1) * dn->dn_datablksz - off; 609fa9e4066Sahrens 610ea8dc4b6Seschrock /* 611ea8dc4b6Seschrock * For i/o error checking, read the first and last level-0 612ea8dc4b6Seschrock * blocks, and all the level-1 blocks. The above count_write's 613cdb0ab79Smaybee * have already taken care of the level-0 blocks. 614ea8dc4b6Seschrock */ 61598572ac1Sahrens if (dn->dn_nlevels > 1) { 61698572ac1Sahrens shift = dn->dn_datablkshift + dn->dn_indblkshift - 61798572ac1Sahrens SPA_BLKPTRSHIFT; 61898572ac1Sahrens start = off >> shift; 61998572ac1Sahrens end = dn->dn_datablkshift ? ((off+len) >> shift) : 0; 62098572ac1Sahrens 62198572ac1Sahrens zio = zio_root(tx->tx_pool->dp_spa, 62298572ac1Sahrens NULL, NULL, ZIO_FLAG_CANFAIL); 62398572ac1Sahrens for (i = start; i <= end; i++) { 62498572ac1Sahrens uint64_t ibyte = i << shift; 625cdb0ab79Smaybee err = dnode_next_offset(dn, 0, &ibyte, 2, 1, 0); 62698572ac1Sahrens i = ibyte >> shift; 62798572ac1Sahrens if (err == ESRCH) 62898572ac1Sahrens break; 62998572ac1Sahrens if (err) { 63098572ac1Sahrens tx->tx_err = err; 63198572ac1Sahrens return; 63298572ac1Sahrens } 633ea8dc4b6Seschrock 63498572ac1Sahrens err = dmu_tx_check_ioerr(zio, dn, 1, i); 63598572ac1Sahrens if (err) { 63698572ac1Sahrens tx->tx_err = err; 63798572ac1Sahrens return; 63898572ac1Sahrens } 63998572ac1Sahrens } 64098572ac1Sahrens err = zio_wait(zio); 641ea8dc4b6Seschrock if (err) { 642ea8dc4b6Seschrock tx->tx_err = err; 643ea8dc4b6Seschrock return; 644ea8dc4b6Seschrock } 645ea8dc4b6Seschrock } 646ea8dc4b6Seschrock 6478a2f1b91Sahrens dmu_tx_count_free(txh, off, len); 648fa9e4066Sahrens } 649fa9e4066Sahrens 650fa9e4066Sahrens void 65114843421SMatthew Ahrens dmu_tx_hold_zap(dmu_tx_t *tx, uint64_t object, int add, const char *name) 652fa9e4066Sahrens { 6538a2f1b91Sahrens dmu_tx_hold_t *txh; 6548a2f1b91Sahrens dnode_t *dn; 655fa9e4066Sahrens uint64_t nblocks; 656ea8dc4b6Seschrock int epbs, err; 657fa9e4066Sahrens 6588a2f1b91Sahrens ASSERT(tx->tx_txg == 0); 6598a2f1b91Sahrens 6608a2f1b91Sahrens txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, 6618a2f1b91Sahrens object, THT_ZAP, add, (uintptr_t)name); 6628a2f1b91Sahrens if (txh == NULL) 6638a2f1b91Sahrens return; 6648a2f1b91Sahrens dn = txh->txh_dnode; 6658a2f1b91Sahrens 6668a2f1b91Sahrens dmu_tx_count_dnode(txh); 667fa9e4066Sahrens 668fa9e4066Sahrens if (dn == NULL) { 669fa9e4066Sahrens /* 670ea8dc4b6Seschrock * We will be able to fit a new object's entries into one leaf 671fa9e4066Sahrens * block. So there will be at most 2 blocks total, 672fa9e4066Sahrens * including the header block. 673fa9e4066Sahrens */ 6748a2f1b91Sahrens dmu_tx_count_write(txh, 0, 2 << fzap_default_block_shift); 675fa9e4066Sahrens return; 676fa9e4066Sahrens } 677fa9e4066Sahrens 678*ad135b5dSChristopher Siden ASSERT3P(DMU_OT_BYTESWAP(dn->dn_type), ==, DMU_BSWAP_ZAP); 679fa9e4066Sahrens 680ea8dc4b6Seschrock if (dn->dn_maxblkid == 0 && !add) { 6819dccfd2aSAlbert Lee blkptr_t *bp; 6829dccfd2aSAlbert Lee 683fa9e4066Sahrens /* 684fa9e4066Sahrens * If there is only one block (i.e. this is a micro-zap) 685ea8dc4b6Seschrock * and we are not adding anything, the accounting is simple. 686fa9e4066Sahrens */ 687ea8dc4b6Seschrock err = dmu_tx_check_ioerr(NULL, dn, 0, 0); 688ea8dc4b6Seschrock if (err) { 689ea8dc4b6Seschrock tx->tx_err = err; 690ea8dc4b6Seschrock return; 691ea8dc4b6Seschrock } 692ea8dc4b6Seschrock 693b6130eadSmaybee /* 694b6130eadSmaybee * Use max block size here, since we don't know how much 695b6130eadSmaybee * the size will change between now and the dbuf dirty call. 696b6130eadSmaybee */ 6979dccfd2aSAlbert Lee bp = &dn->dn_phys->dn_blkptr[0]; 698fa9e4066Sahrens if (dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset, 6999dccfd2aSAlbert Lee bp, bp->blk_birth)) 700b6130eadSmaybee txh->txh_space_tooverwrite += SPA_MAXBLOCKSIZE; 7019dccfd2aSAlbert Lee else 702b6130eadSmaybee txh->txh_space_towrite += SPA_MAXBLOCKSIZE; 7039dccfd2aSAlbert Lee if (!BP_IS_HOLE(bp)) 704f878aa38SChris Kirby txh->txh_space_tounref += SPA_MAXBLOCKSIZE; 705fa9e4066Sahrens return; 706fa9e4066Sahrens } 707fa9e4066Sahrens 708ea8dc4b6Seschrock if (dn->dn_maxblkid > 0 && name) { 709ea8dc4b6Seschrock /* 710ea8dc4b6Seschrock * access the name in this fat-zap so that we'll check 711ea8dc4b6Seschrock * for i/o errors to the leaf blocks, etc. 712ea8dc4b6Seschrock */ 713503ad85cSMatthew Ahrens err = zap_lookup(dn->dn_objset, dn->dn_object, name, 714ea8dc4b6Seschrock 8, 0, NULL); 715ea8dc4b6Seschrock if (err == EIO) { 716ea8dc4b6Seschrock tx->tx_err = err; 717ea8dc4b6Seschrock return; 718ea8dc4b6Seschrock } 719ea8dc4b6Seschrock } 720ea8dc4b6Seschrock 721503ad85cSMatthew Ahrens err = zap_count_write(dn->dn_objset, dn->dn_object, name, add, 722720d1aa1SSanjeev Bagewadi &txh->txh_space_towrite, &txh->txh_space_tooverwrite); 723fa9e4066Sahrens 724fa9e4066Sahrens /* 725fa9e4066Sahrens * If the modified blocks are scattered to the four winds, 726fa9e4066Sahrens * we'll have to modify an indirect twig for each. 727fa9e4066Sahrens */ 728fa9e4066Sahrens epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 729fa9e4066Sahrens for (nblocks = dn->dn_maxblkid >> epbs; nblocks != 0; nblocks >>= epbs) 7303d692628SSanjeev Bagewadi if (dn->dn_objset->os_dsl_dataset->ds_phys->ds_prev_snap_obj) 7313d692628SSanjeev Bagewadi txh->txh_space_towrite += 3 << dn->dn_indblkshift; 7323d692628SSanjeev Bagewadi else 7333d692628SSanjeev Bagewadi txh->txh_space_tooverwrite += 3 << dn->dn_indblkshift; 734fa9e4066Sahrens } 735fa9e4066Sahrens 736fa9e4066Sahrens void 737fa9e4066Sahrens dmu_tx_hold_bonus(dmu_tx_t *tx, uint64_t object) 738fa9e4066Sahrens { 7398a2f1b91Sahrens dmu_tx_hold_t *txh; 740fa9e4066Sahrens 7418a2f1b91Sahrens ASSERT(tx->tx_txg == 0); 742fa9e4066Sahrens 7438a2f1b91Sahrens txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, 7448a2f1b91Sahrens object, THT_BONUS, 0, 0); 7458a2f1b91Sahrens if (txh) 7468a2f1b91Sahrens dmu_tx_count_dnode(txh); 747fa9e4066Sahrens } 748fa9e4066Sahrens 749fa9e4066Sahrens void 750fa9e4066Sahrens dmu_tx_hold_space(dmu_tx_t *tx, uint64_t space) 751fa9e4066Sahrens { 7528a2f1b91Sahrens dmu_tx_hold_t *txh; 753fa9e4066Sahrens ASSERT(tx->tx_txg == 0); 754fa9e4066Sahrens 7558a2f1b91Sahrens txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, 7568a2f1b91Sahrens DMU_NEW_OBJECT, THT_SPACE, space, 0); 7578a2f1b91Sahrens 7588a2f1b91Sahrens txh->txh_space_towrite += space; 759fa9e4066Sahrens } 760fa9e4066Sahrens 761fa9e4066Sahrens int 762fa9e4066Sahrens dmu_tx_holds(dmu_tx_t *tx, uint64_t object) 763fa9e4066Sahrens { 7648a2f1b91Sahrens dmu_tx_hold_t *txh; 765fa9e4066Sahrens int holds = 0; 766fa9e4066Sahrens 767fa9e4066Sahrens /* 768fa9e4066Sahrens * By asserting that the tx is assigned, we're counting the 769fa9e4066Sahrens * number of dn_tx_holds, which is the same as the number of 770fa9e4066Sahrens * dn_holds. Otherwise, we'd be counting dn_holds, but 771fa9e4066Sahrens * dn_tx_holds could be 0. 772fa9e4066Sahrens */ 773fa9e4066Sahrens ASSERT(tx->tx_txg != 0); 774fa9e4066Sahrens 775fa9e4066Sahrens /* if (tx->tx_anyobj == TRUE) */ 776fa9e4066Sahrens /* return (0); */ 777fa9e4066Sahrens 7788a2f1b91Sahrens for (txh = list_head(&tx->tx_holds); txh; 7798a2f1b91Sahrens txh = list_next(&tx->tx_holds, txh)) { 7808a2f1b91Sahrens if (txh->txh_dnode && txh->txh_dnode->dn_object == object) 781fa9e4066Sahrens holds++; 782fa9e4066Sahrens } 783fa9e4066Sahrens 784fa9e4066Sahrens return (holds); 785fa9e4066Sahrens } 786fa9e4066Sahrens 7879c9dc39aSek #ifdef ZFS_DEBUG 788fa9e4066Sahrens void 789fa9e4066Sahrens dmu_tx_dirty_buf(dmu_tx_t *tx, dmu_buf_impl_t *db) 790fa9e4066Sahrens { 7918a2f1b91Sahrens dmu_tx_hold_t *txh; 792fa9e4066Sahrens int match_object = FALSE, match_offset = FALSE; 793744947dcSTom Erickson dnode_t *dn; 794fa9e4066Sahrens 795744947dcSTom Erickson DB_DNODE_ENTER(db); 796744947dcSTom Erickson dn = DB_DNODE(db); 797fa9e4066Sahrens ASSERT(tx->tx_txg != 0); 798503ad85cSMatthew Ahrens ASSERT(tx->tx_objset == NULL || dn->dn_objset == tx->tx_objset); 799fa9e4066Sahrens ASSERT3U(dn->dn_object, ==, db->db.db_object); 800fa9e4066Sahrens 801744947dcSTom Erickson if (tx->tx_anyobj) { 802744947dcSTom Erickson DB_DNODE_EXIT(db); 803fa9e4066Sahrens return; 804744947dcSTom Erickson } 805fa9e4066Sahrens 806fa9e4066Sahrens /* XXX No checking on the meta dnode for now */ 807744947dcSTom Erickson if (db->db.db_object == DMU_META_DNODE_OBJECT) { 808744947dcSTom Erickson DB_DNODE_EXIT(db); 809fa9e4066Sahrens return; 810744947dcSTom Erickson } 811fa9e4066Sahrens 8128a2f1b91Sahrens for (txh = list_head(&tx->tx_holds); txh; 8138a2f1b91Sahrens txh = list_next(&tx->tx_holds, txh)) { 814fa9e4066Sahrens ASSERT(dn == NULL || dn->dn_assigned_txg == tx->tx_txg); 8158a2f1b91Sahrens if (txh->txh_dnode == dn && txh->txh_type != THT_NEWOBJECT) 816fa9e4066Sahrens match_object = TRUE; 8178a2f1b91Sahrens if (txh->txh_dnode == NULL || txh->txh_dnode == dn) { 818fa9e4066Sahrens int datablkshift = dn->dn_datablkshift ? 819fa9e4066Sahrens dn->dn_datablkshift : SPA_MAXBLOCKSHIFT; 820fa9e4066Sahrens int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 821fa9e4066Sahrens int shift = datablkshift + epbs * db->db_level; 822fa9e4066Sahrens uint64_t beginblk = shift >= 64 ? 0 : 8238a2f1b91Sahrens (txh->txh_arg1 >> shift); 824fa9e4066Sahrens uint64_t endblk = shift >= 64 ? 0 : 8258a2f1b91Sahrens ((txh->txh_arg1 + txh->txh_arg2 - 1) >> shift); 826fa9e4066Sahrens uint64_t blkid = db->db_blkid; 827fa9e4066Sahrens 8288a2f1b91Sahrens /* XXX txh_arg2 better not be zero... */ 829fa9e4066Sahrens 8308a2f1b91Sahrens dprintf("found txh type %x beginblk=%llx endblk=%llx\n", 8318a2f1b91Sahrens txh->txh_type, beginblk, endblk); 832fa9e4066Sahrens 8338a2f1b91Sahrens switch (txh->txh_type) { 834fa9e4066Sahrens case THT_WRITE: 835fa9e4066Sahrens if (blkid >= beginblk && blkid <= endblk) 836fa9e4066Sahrens match_offset = TRUE; 837fa9e4066Sahrens /* 838fa9e4066Sahrens * We will let this hold work for the bonus 8390a586ceaSMark Shellenbaum * or spill buffer so that we don't need to 8400a586ceaSMark Shellenbaum * hold it when creating a new object. 841fa9e4066Sahrens */ 8420a586ceaSMark Shellenbaum if (blkid == DMU_BONUS_BLKID || 8430a586ceaSMark Shellenbaum blkid == DMU_SPILL_BLKID) 844fa9e4066Sahrens match_offset = TRUE; 845fa9e4066Sahrens /* 846fa9e4066Sahrens * They might have to increase nlevels, 847fa9e4066Sahrens * thus dirtying the new TLIBs. Or the 848fa9e4066Sahrens * might have to change the block size, 849fa9e4066Sahrens * thus dirying the new lvl=0 blk=0. 850fa9e4066Sahrens */ 851fa9e4066Sahrens if (blkid == 0) 852fa9e4066Sahrens match_offset = TRUE; 853fa9e4066Sahrens break; 854fa9e4066Sahrens case THT_FREE: 855cdb0ab79Smaybee /* 856cdb0ab79Smaybee * We will dirty all the level 1 blocks in 857cdb0ab79Smaybee * the free range and perhaps the first and 858cdb0ab79Smaybee * last level 0 block. 859cdb0ab79Smaybee */ 860cdb0ab79Smaybee if (blkid >= beginblk && (blkid <= endblk || 861cdb0ab79Smaybee txh->txh_arg2 == DMU_OBJECT_END)) 862fa9e4066Sahrens match_offset = TRUE; 863fa9e4066Sahrens break; 8640a586ceaSMark Shellenbaum case THT_SPILL: 8650a586ceaSMark Shellenbaum if (blkid == DMU_SPILL_BLKID) 8660a586ceaSMark Shellenbaum match_offset = TRUE; 8670a586ceaSMark Shellenbaum break; 868fa9e4066Sahrens case THT_BONUS: 8690a586ceaSMark Shellenbaum if (blkid == DMU_BONUS_BLKID) 870fa9e4066Sahrens match_offset = TRUE; 871fa9e4066Sahrens break; 872fa9e4066Sahrens case THT_ZAP: 873fa9e4066Sahrens match_offset = TRUE; 874fa9e4066Sahrens break; 875fa9e4066Sahrens case THT_NEWOBJECT: 876fa9e4066Sahrens match_object = TRUE; 877fa9e4066Sahrens break; 878fa9e4066Sahrens default: 8798a2f1b91Sahrens ASSERT(!"bad txh_type"); 880fa9e4066Sahrens } 881fa9e4066Sahrens } 882744947dcSTom Erickson if (match_object && match_offset) { 883744947dcSTom Erickson DB_DNODE_EXIT(db); 884fa9e4066Sahrens return; 885744947dcSTom Erickson } 886fa9e4066Sahrens } 887744947dcSTom Erickson DB_DNODE_EXIT(db); 888fa9e4066Sahrens panic("dirtying dbuf obj=%llx lvl=%u blkid=%llx but not tx_held\n", 889fa9e4066Sahrens (u_longlong_t)db->db.db_object, db->db_level, 890fa9e4066Sahrens (u_longlong_t)db->db_blkid); 891fa9e4066Sahrens } 8929c9dc39aSek #endif 893fa9e4066Sahrens 894fa9e4066Sahrens static int 8958a2f1b91Sahrens dmu_tx_try_assign(dmu_tx_t *tx, uint64_t txg_how) 896fa9e4066Sahrens { 8978a2f1b91Sahrens dmu_tx_hold_t *txh; 8980a4e9518Sgw spa_t *spa = tx->tx_pool->dp_spa; 899cdb0ab79Smaybee uint64_t memory, asize, fsize, usize; 900715614a4Smaybee uint64_t towrite, tofree, tooverwrite, tounref, tohold, fudge; 901fa9e4066Sahrens 9028a2f1b91Sahrens ASSERT3U(tx->tx_txg, ==, 0); 9030a4e9518Sgw 9048a2f1b91Sahrens if (tx->tx_err) 9058a2f1b91Sahrens return (tx->tx_err); 906fa9e4066Sahrens 907e14bb325SJeff Bonwick if (spa_suspended(spa)) { 9080a4e9518Sgw /* 9090a4e9518Sgw * If the user has indicated a blocking failure mode 9100a4e9518Sgw * then return ERESTART which will block in dmu_tx_wait(). 9110a4e9518Sgw * Otherwise, return EIO so that an error can get 9120a4e9518Sgw * propagated back to the VOP calls. 9130a4e9518Sgw * 9140a4e9518Sgw * Note that we always honor the txg_how flag regardless 9150a4e9518Sgw * of the failuremode setting. 9160a4e9518Sgw */ 9170a4e9518Sgw if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE && 9180a4e9518Sgw txg_how != TXG_WAIT) 9190a4e9518Sgw return (EIO); 9200a4e9518Sgw 9210a4e9518Sgw return (ERESTART); 9220a4e9518Sgw } 9230a4e9518Sgw 924fa9e4066Sahrens tx->tx_txg = txg_hold_open(tx->tx_pool, &tx->tx_txgh); 9258a2f1b91Sahrens tx->tx_needassign_txh = NULL; 926fa9e4066Sahrens 9278a2f1b91Sahrens /* 9288a2f1b91Sahrens * NB: No error returns are allowed after txg_hold_open, but 9298a2f1b91Sahrens * before processing the dnode holds, due to the 9308a2f1b91Sahrens * dmu_tx_unassign() logic. 9318a2f1b91Sahrens */ 932fa9e4066Sahrens 933715614a4Smaybee towrite = tofree = tooverwrite = tounref = tohold = fudge = 0; 9348a2f1b91Sahrens for (txh = list_head(&tx->tx_holds); txh; 9358a2f1b91Sahrens txh = list_next(&tx->tx_holds, txh)) { 9368a2f1b91Sahrens dnode_t *dn = txh->txh_dnode; 937fa9e4066Sahrens if (dn != NULL) { 938fa9e4066Sahrens mutex_enter(&dn->dn_mtx); 9398a2f1b91Sahrens if (dn->dn_assigned_txg == tx->tx_txg - 1) { 9408a2f1b91Sahrens mutex_exit(&dn->dn_mtx); 9418a2f1b91Sahrens tx->tx_needassign_txh = txh; 9428a2f1b91Sahrens return (ERESTART); 943fa9e4066Sahrens } 9448a2f1b91Sahrens if (dn->dn_assigned_txg == 0) 945fa9e4066Sahrens dn->dn_assigned_txg = tx->tx_txg; 9468a2f1b91Sahrens ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg); 947fa9e4066Sahrens (void) refcount_add(&dn->dn_tx_holds, tx); 948fa9e4066Sahrens mutex_exit(&dn->dn_mtx); 949fa9e4066Sahrens } 9508a2f1b91Sahrens towrite += txh->txh_space_towrite; 9518a2f1b91Sahrens tofree += txh->txh_space_tofree; 9528a2f1b91Sahrens tooverwrite += txh->txh_space_tooverwrite; 953a9799022Sck tounref += txh->txh_space_tounref; 954cdb0ab79Smaybee tohold += txh->txh_memory_tohold; 955715614a4Smaybee fudge += txh->txh_fudge; 956ea8dc4b6Seschrock } 957ea8dc4b6Seschrock 9588a2f1b91Sahrens /* 9598a2f1b91Sahrens * NB: This check must be after we've held the dnodes, so that 9608a2f1b91Sahrens * the dmu_tx_unassign() logic will work properly 9618a2f1b91Sahrens */ 9628a2f1b91Sahrens if (txg_how >= TXG_INITIAL && txg_how != tx->tx_txg) 9638a2f1b91Sahrens return (ERESTART); 9648a2f1b91Sahrens 965ea8dc4b6Seschrock /* 966ea8dc4b6Seschrock * If a snapshot has been taken since we made our estimates, 967ea8dc4b6Seschrock * assume that we won't be able to free or overwrite anything. 968ea8dc4b6Seschrock */ 969ea8dc4b6Seschrock if (tx->tx_objset && 970503ad85cSMatthew Ahrens dsl_dataset_prev_snap_txg(tx->tx_objset->os_dsl_dataset) > 971ea8dc4b6Seschrock tx->tx_lastsnap_txg) { 9728a2f1b91Sahrens towrite += tooverwrite; 9738a2f1b91Sahrens tooverwrite = tofree = 0; 974fa9e4066Sahrens } 975fa9e4066Sahrens 976cdb0ab79Smaybee /* needed allocation: worst-case estimate of write space */ 977cdb0ab79Smaybee asize = spa_get_asize(tx->tx_pool->dp_spa, towrite + tooverwrite); 978cdb0ab79Smaybee /* freed space estimate: worst-case overwrite + free estimate */ 9798a2f1b91Sahrens fsize = spa_get_asize(tx->tx_pool->dp_spa, tooverwrite) + tofree; 980cdb0ab79Smaybee /* convert unrefd space to worst-case estimate */ 981a9799022Sck usize = spa_get_asize(tx->tx_pool->dp_spa, tounref); 982cdb0ab79Smaybee /* calculate memory footprint estimate */ 983cdb0ab79Smaybee memory = towrite + tooverwrite + tohold; 9848a2f1b91Sahrens 9858a2f1b91Sahrens #ifdef ZFS_DEBUG 986715614a4Smaybee /* 987715614a4Smaybee * Add in 'tohold' to account for our dirty holds on this memory 988715614a4Smaybee * XXX - the "fudge" factor is to account for skipped blocks that 989715614a4Smaybee * we missed because dnode_next_offset() misses in-core-only blocks. 990715614a4Smaybee */ 991cdb0ab79Smaybee tx->tx_space_towrite = asize + 992715614a4Smaybee spa_get_asize(tx->tx_pool->dp_spa, tohold + fudge); 9938a2f1b91Sahrens tx->tx_space_tofree = tofree; 9948a2f1b91Sahrens tx->tx_space_tooverwrite = tooverwrite; 995a9799022Sck tx->tx_space_tounref = tounref; 9968a2f1b91Sahrens #endif 997fa9e4066Sahrens 998fa9e4066Sahrens if (tx->tx_dir && asize != 0) { 999cdb0ab79Smaybee int err = dsl_dir_tempreserve_space(tx->tx_dir, memory, 1000cdb0ab79Smaybee asize, fsize, usize, &tx->tx_tempreserve_cookie, tx); 10018a2f1b91Sahrens if (err) 1002fa9e4066Sahrens return (err); 1003fa9e4066Sahrens } 1004fa9e4066Sahrens 1005fa9e4066Sahrens return (0); 1006fa9e4066Sahrens } 1007fa9e4066Sahrens 10088a2f1b91Sahrens static void 10098a2f1b91Sahrens dmu_tx_unassign(dmu_tx_t *tx) 1010fa9e4066Sahrens { 10118a2f1b91Sahrens dmu_tx_hold_t *txh; 1012fa9e4066Sahrens 10138a2f1b91Sahrens if (tx->tx_txg == 0) 10148a2f1b91Sahrens return; 1015fa9e4066Sahrens 1016fa9e4066Sahrens txg_rele_to_quiesce(&tx->tx_txgh); 1017fa9e4066Sahrens 10188a2f1b91Sahrens for (txh = list_head(&tx->tx_holds); txh != tx->tx_needassign_txh; 10198a2f1b91Sahrens txh = list_next(&tx->tx_holds, txh)) { 10208a2f1b91Sahrens dnode_t *dn = txh->txh_dnode; 1021fa9e4066Sahrens 1022fa9e4066Sahrens if (dn == NULL) 1023fa9e4066Sahrens continue; 1024fa9e4066Sahrens mutex_enter(&dn->dn_mtx); 10258a2f1b91Sahrens ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg); 1026fa9e4066Sahrens 1027fa9e4066Sahrens if (refcount_remove(&dn->dn_tx_holds, tx) == 0) { 1028fa9e4066Sahrens dn->dn_assigned_txg = 0; 1029fa9e4066Sahrens cv_broadcast(&dn->dn_notxholds); 1030fa9e4066Sahrens } 1031fa9e4066Sahrens mutex_exit(&dn->dn_mtx); 1032fa9e4066Sahrens } 1033fa9e4066Sahrens 1034fa9e4066Sahrens txg_rele_to_sync(&tx->tx_txgh); 1035fa9e4066Sahrens 10368a2f1b91Sahrens tx->tx_lasttried_txg = tx->tx_txg; 1037fa9e4066Sahrens tx->tx_txg = 0; 1038fa9e4066Sahrens } 1039fa9e4066Sahrens 1040fa9e4066Sahrens /* 1041fa9e4066Sahrens * Assign tx to a transaction group. txg_how can be one of: 1042fa9e4066Sahrens * 1043fa9e4066Sahrens * (1) TXG_WAIT. If the current open txg is full, waits until there's 1044fa9e4066Sahrens * a new one. This should be used when you're not holding locks. 1045fa9e4066Sahrens * If will only fail if we're truly out of space (or over quota). 1046fa9e4066Sahrens * 1047fa9e4066Sahrens * (2) TXG_NOWAIT. If we can't assign into the current open txg without 1048fa9e4066Sahrens * blocking, returns immediately with ERESTART. This should be used 1049fa9e4066Sahrens * whenever you're holding locks. On an ERESTART error, the caller 10508a2f1b91Sahrens * should drop locks, do a dmu_tx_wait(tx), and try again. 1051fa9e4066Sahrens * 1052fa9e4066Sahrens * (3) A specific txg. Use this if you need to ensure that multiple 1053fa9e4066Sahrens * transactions all sync in the same txg. Like TXG_NOWAIT, it 1054fa9e4066Sahrens * returns ERESTART if it can't assign you into the requested txg. 1055fa9e4066Sahrens */ 1056fa9e4066Sahrens int 1057fa9e4066Sahrens dmu_tx_assign(dmu_tx_t *tx, uint64_t txg_how) 1058fa9e4066Sahrens { 1059fa9e4066Sahrens int err; 1060fa9e4066Sahrens 1061fa9e4066Sahrens ASSERT(tx->tx_txg == 0); 1062fa9e4066Sahrens ASSERT(txg_how != 0); 1063fa9e4066Sahrens ASSERT(!dsl_pool_sync_context(tx->tx_pool)); 1064fa9e4066Sahrens 10658a2f1b91Sahrens while ((err = dmu_tx_try_assign(tx, txg_how)) != 0) { 10668a2f1b91Sahrens dmu_tx_unassign(tx); 1067fa9e4066Sahrens 1068fa9e4066Sahrens if (err != ERESTART || txg_how != TXG_WAIT) 1069fa9e4066Sahrens return (err); 1070fa9e4066Sahrens 10718a2f1b91Sahrens dmu_tx_wait(tx); 1072fa9e4066Sahrens } 1073fa9e4066Sahrens 1074fa9e4066Sahrens txg_rele_to_quiesce(&tx->tx_txgh); 1075fa9e4066Sahrens 1076fa9e4066Sahrens return (0); 1077fa9e4066Sahrens } 1078fa9e4066Sahrens 10798a2f1b91Sahrens void 10808a2f1b91Sahrens dmu_tx_wait(dmu_tx_t *tx) 10818a2f1b91Sahrens { 10820a4e9518Sgw spa_t *spa = tx->tx_pool->dp_spa; 10830a4e9518Sgw 10848a2f1b91Sahrens ASSERT(tx->tx_txg == 0); 10858a2f1b91Sahrens 10860a4e9518Sgw /* 10870a4e9518Sgw * It's possible that the pool has become active after this thread 10880a4e9518Sgw * has tried to obtain a tx. If that's the case then his 10890a4e9518Sgw * tx_lasttried_txg would not have been assigned. 10900a4e9518Sgw */ 1091e14bb325SJeff Bonwick if (spa_suspended(spa) || tx->tx_lasttried_txg == 0) { 10920a4e9518Sgw txg_wait_synced(tx->tx_pool, spa_last_synced_txg(spa) + 1); 10930a4e9518Sgw } else if (tx->tx_needassign_txh) { 10948a2f1b91Sahrens dnode_t *dn = tx->tx_needassign_txh->txh_dnode; 10958a2f1b91Sahrens 10968a2f1b91Sahrens mutex_enter(&dn->dn_mtx); 10978a2f1b91Sahrens while (dn->dn_assigned_txg == tx->tx_lasttried_txg - 1) 10988a2f1b91Sahrens cv_wait(&dn->dn_notxholds, &dn->dn_mtx); 10998a2f1b91Sahrens mutex_exit(&dn->dn_mtx); 11008a2f1b91Sahrens tx->tx_needassign_txh = NULL; 11018a2f1b91Sahrens } else { 11028a2f1b91Sahrens txg_wait_open(tx->tx_pool, tx->tx_lasttried_txg + 1); 11038a2f1b91Sahrens } 11048a2f1b91Sahrens } 11058a2f1b91Sahrens 1106fa9e4066Sahrens void 1107fa9e4066Sahrens dmu_tx_willuse_space(dmu_tx_t *tx, int64_t delta) 1108fa9e4066Sahrens { 11098a2f1b91Sahrens #ifdef ZFS_DEBUG 1110fa9e4066Sahrens if (tx->tx_dir == NULL || delta == 0) 1111fa9e4066Sahrens return; 1112fa9e4066Sahrens 1113fa9e4066Sahrens if (delta > 0) { 1114fa9e4066Sahrens ASSERT3U(refcount_count(&tx->tx_space_written) + delta, <=, 1115fa9e4066Sahrens tx->tx_space_towrite); 1116fa9e4066Sahrens (void) refcount_add_many(&tx->tx_space_written, delta, NULL); 1117fa9e4066Sahrens } else { 1118fa9e4066Sahrens (void) refcount_add_many(&tx->tx_space_freed, -delta, NULL); 1119fa9e4066Sahrens } 11208a2f1b91Sahrens #endif 1121fa9e4066Sahrens } 1122fa9e4066Sahrens 1123fa9e4066Sahrens void 1124fa9e4066Sahrens dmu_tx_commit(dmu_tx_t *tx) 1125fa9e4066Sahrens { 11268a2f1b91Sahrens dmu_tx_hold_t *txh; 1127fa9e4066Sahrens 1128fa9e4066Sahrens ASSERT(tx->tx_txg != 0); 1129fa9e4066Sahrens 11308a2f1b91Sahrens while (txh = list_head(&tx->tx_holds)) { 11318a2f1b91Sahrens dnode_t *dn = txh->txh_dnode; 1132fa9e4066Sahrens 11338a2f1b91Sahrens list_remove(&tx->tx_holds, txh); 11348a2f1b91Sahrens kmem_free(txh, sizeof (dmu_tx_hold_t)); 1135fa9e4066Sahrens if (dn == NULL) 1136fa9e4066Sahrens continue; 1137fa9e4066Sahrens mutex_enter(&dn->dn_mtx); 1138fa9e4066Sahrens ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg); 1139fa9e4066Sahrens 1140fa9e4066Sahrens if (refcount_remove(&dn->dn_tx_holds, tx) == 0) { 1141fa9e4066Sahrens dn->dn_assigned_txg = 0; 1142fa9e4066Sahrens cv_broadcast(&dn->dn_notxholds); 1143fa9e4066Sahrens } 1144fa9e4066Sahrens mutex_exit(&dn->dn_mtx); 1145fa9e4066Sahrens dnode_rele(dn, tx); 1146fa9e4066Sahrens } 1147fa9e4066Sahrens 11488a2f1b91Sahrens if (tx->tx_tempreserve_cookie) 1149fa9e4066Sahrens dsl_dir_tempreserve_clear(tx->tx_tempreserve_cookie, tx); 1150fa9e4066Sahrens 1151d20e665cSRicardo M. Correia if (!list_is_empty(&tx->tx_callbacks)) 1152d20e665cSRicardo M. Correia txg_register_callbacks(&tx->tx_txgh, &tx->tx_callbacks); 1153d20e665cSRicardo M. Correia 1154fa9e4066Sahrens if (tx->tx_anyobj == FALSE) 1155fa9e4066Sahrens txg_rele_to_sync(&tx->tx_txgh); 1156d20e665cSRicardo M. Correia 1157d20e665cSRicardo M. Correia list_destroy(&tx->tx_callbacks); 11588f38d419Sek list_destroy(&tx->tx_holds); 11598a2f1b91Sahrens #ifdef ZFS_DEBUG 1160fa9e4066Sahrens dprintf("towrite=%llu written=%llu tofree=%llu freed=%llu\n", 1161fa9e4066Sahrens tx->tx_space_towrite, refcount_count(&tx->tx_space_written), 1162fa9e4066Sahrens tx->tx_space_tofree, refcount_count(&tx->tx_space_freed)); 1163fa9e4066Sahrens refcount_destroy_many(&tx->tx_space_written, 1164fa9e4066Sahrens refcount_count(&tx->tx_space_written)); 1165fa9e4066Sahrens refcount_destroy_many(&tx->tx_space_freed, 1166fa9e4066Sahrens refcount_count(&tx->tx_space_freed)); 1167fa9e4066Sahrens #endif 1168fa9e4066Sahrens kmem_free(tx, sizeof (dmu_tx_t)); 1169fa9e4066Sahrens } 1170fa9e4066Sahrens 1171fa9e4066Sahrens void 1172fa9e4066Sahrens dmu_tx_abort(dmu_tx_t *tx) 1173fa9e4066Sahrens { 11748a2f1b91Sahrens dmu_tx_hold_t *txh; 1175fa9e4066Sahrens 1176fa9e4066Sahrens ASSERT(tx->tx_txg == 0); 1177fa9e4066Sahrens 11788a2f1b91Sahrens while (txh = list_head(&tx->tx_holds)) { 11798a2f1b91Sahrens dnode_t *dn = txh->txh_dnode; 1180fa9e4066Sahrens 11818a2f1b91Sahrens list_remove(&tx->tx_holds, txh); 11828a2f1b91Sahrens kmem_free(txh, sizeof (dmu_tx_hold_t)); 1183fa9e4066Sahrens if (dn != NULL) 1184fa9e4066Sahrens dnode_rele(dn, tx); 1185fa9e4066Sahrens } 1186d20e665cSRicardo M. Correia 1187d20e665cSRicardo M. Correia /* 1188d20e665cSRicardo M. Correia * Call any registered callbacks with an error code. 1189d20e665cSRicardo M. Correia */ 1190d20e665cSRicardo M. Correia if (!list_is_empty(&tx->tx_callbacks)) 1191d20e665cSRicardo M. Correia dmu_tx_do_callbacks(&tx->tx_callbacks, ECANCELED); 1192d20e665cSRicardo M. Correia 1193d20e665cSRicardo M. Correia list_destroy(&tx->tx_callbacks); 11948f38d419Sek list_destroy(&tx->tx_holds); 11958a2f1b91Sahrens #ifdef ZFS_DEBUG 1196fa9e4066Sahrens refcount_destroy_many(&tx->tx_space_written, 1197fa9e4066Sahrens refcount_count(&tx->tx_space_written)); 1198fa9e4066Sahrens refcount_destroy_many(&tx->tx_space_freed, 1199fa9e4066Sahrens refcount_count(&tx->tx_space_freed)); 1200fa9e4066Sahrens #endif 1201fa9e4066Sahrens kmem_free(tx, sizeof (dmu_tx_t)); 1202fa9e4066Sahrens } 1203fa9e4066Sahrens 1204fa9e4066Sahrens uint64_t 1205fa9e4066Sahrens dmu_tx_get_txg(dmu_tx_t *tx) 1206fa9e4066Sahrens { 1207fa9e4066Sahrens ASSERT(tx->tx_txg != 0); 1208fa9e4066Sahrens return (tx->tx_txg); 1209fa9e4066Sahrens } 1210d20e665cSRicardo M. Correia 1211d20e665cSRicardo M. Correia void 1212d20e665cSRicardo M. Correia dmu_tx_callback_register(dmu_tx_t *tx, dmu_tx_callback_func_t *func, void *data) 1213d20e665cSRicardo M. Correia { 1214d20e665cSRicardo M. Correia dmu_tx_callback_t *dcb; 1215d20e665cSRicardo M. Correia 1216d20e665cSRicardo M. Correia dcb = kmem_alloc(sizeof (dmu_tx_callback_t), KM_SLEEP); 1217d20e665cSRicardo M. Correia 1218d20e665cSRicardo M. Correia dcb->dcb_func = func; 1219d20e665cSRicardo M. Correia dcb->dcb_data = data; 1220d20e665cSRicardo M. Correia 1221d20e665cSRicardo M. Correia list_insert_tail(&tx->tx_callbacks, dcb); 1222d20e665cSRicardo M. Correia } 1223d20e665cSRicardo M. Correia 1224d20e665cSRicardo M. Correia /* 1225d20e665cSRicardo M. Correia * Call all the commit callbacks on a list, with a given error code. 1226d20e665cSRicardo M. Correia */ 1227d20e665cSRicardo M. Correia void 1228d20e665cSRicardo M. Correia dmu_tx_do_callbacks(list_t *cb_list, int error) 1229d20e665cSRicardo M. Correia { 1230d20e665cSRicardo M. Correia dmu_tx_callback_t *dcb; 1231d20e665cSRicardo M. Correia 1232d20e665cSRicardo M. Correia while (dcb = list_head(cb_list)) { 1233d20e665cSRicardo M. Correia list_remove(cb_list, dcb); 1234d20e665cSRicardo M. Correia dcb->dcb_func(dcb->dcb_data, error); 1235d20e665cSRicardo M. Correia kmem_free(dcb, sizeof (dmu_tx_callback_t)); 1236d20e665cSRicardo M. Correia } 1237d20e665cSRicardo M. Correia } 12380a586ceaSMark Shellenbaum 12390a586ceaSMark Shellenbaum /* 12400a586ceaSMark Shellenbaum * Interface to hold a bunch of attributes. 12410a586ceaSMark Shellenbaum * used for creating new files. 12420a586ceaSMark Shellenbaum * attrsize is the total size of all attributes 12430a586ceaSMark Shellenbaum * to be added during object creation 12440a586ceaSMark Shellenbaum * 12450a586ceaSMark Shellenbaum * For updating/adding a single attribute dmu_tx_hold_sa() should be used. 12460a586ceaSMark Shellenbaum */ 12470a586ceaSMark Shellenbaum 12480a586ceaSMark Shellenbaum /* 12490a586ceaSMark Shellenbaum * hold necessary attribute name for attribute registration. 12500a586ceaSMark Shellenbaum * should be a very rare case where this is needed. If it does 12510a586ceaSMark Shellenbaum * happen it would only happen on the first write to the file system. 12520a586ceaSMark Shellenbaum */ 12530a586ceaSMark Shellenbaum static void 12540a586ceaSMark Shellenbaum dmu_tx_sa_registration_hold(sa_os_t *sa, dmu_tx_t *tx) 12550a586ceaSMark Shellenbaum { 12560a586ceaSMark Shellenbaum int i; 12570a586ceaSMark Shellenbaum 12580a586ceaSMark Shellenbaum if (!sa->sa_need_attr_registration) 12590a586ceaSMark Shellenbaum return; 12600a586ceaSMark Shellenbaum 12610a586ceaSMark Shellenbaum for (i = 0; i != sa->sa_num_attrs; i++) { 12620a586ceaSMark Shellenbaum if (!sa->sa_attr_table[i].sa_registered) { 12630a586ceaSMark Shellenbaum if (sa->sa_reg_attr_obj) 12640a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, sa->sa_reg_attr_obj, 12650a586ceaSMark Shellenbaum B_TRUE, sa->sa_attr_table[i].sa_name); 12660a586ceaSMark Shellenbaum else 12670a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, 12680a586ceaSMark Shellenbaum B_TRUE, sa->sa_attr_table[i].sa_name); 12690a586ceaSMark Shellenbaum } 12700a586ceaSMark Shellenbaum } 12710a586ceaSMark Shellenbaum } 12720a586ceaSMark Shellenbaum 12730a586ceaSMark Shellenbaum 12740a586ceaSMark Shellenbaum void 12750a586ceaSMark Shellenbaum dmu_tx_hold_spill(dmu_tx_t *tx, uint64_t object) 12760a586ceaSMark Shellenbaum { 12770a586ceaSMark Shellenbaum dnode_t *dn; 12780a586ceaSMark Shellenbaum dmu_tx_hold_t *txh; 12790a586ceaSMark Shellenbaum 12800a586ceaSMark Shellenbaum txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, object, 12810a586ceaSMark Shellenbaum THT_SPILL, 0, 0); 12820a586ceaSMark Shellenbaum 12830a586ceaSMark Shellenbaum dn = txh->txh_dnode; 12840a586ceaSMark Shellenbaum 12850a586ceaSMark Shellenbaum if (dn == NULL) 12860a586ceaSMark Shellenbaum return; 12870a586ceaSMark Shellenbaum 12880a586ceaSMark Shellenbaum /* If blkptr doesn't exist then add space to towrite */ 12899dccfd2aSAlbert Lee if (!(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) { 12900a586ceaSMark Shellenbaum txh->txh_space_towrite += SPA_MAXBLOCKSIZE; 12910a586ceaSMark Shellenbaum } else { 12929dccfd2aSAlbert Lee blkptr_t *bp; 12939dccfd2aSAlbert Lee 12949dccfd2aSAlbert Lee bp = &dn->dn_phys->dn_spill; 12950a586ceaSMark Shellenbaum if (dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset, 1296c7cd2421SGeorge Wilson bp, bp->blk_birth)) 12970a586ceaSMark Shellenbaum txh->txh_space_tooverwrite += SPA_MAXBLOCKSIZE; 12980a586ceaSMark Shellenbaum else 12990a586ceaSMark Shellenbaum txh->txh_space_towrite += SPA_MAXBLOCKSIZE; 13009dccfd2aSAlbert Lee if (!BP_IS_HOLE(bp)) 13010a586ceaSMark Shellenbaum txh->txh_space_tounref += SPA_MAXBLOCKSIZE; 13020a586ceaSMark Shellenbaum } 13030a586ceaSMark Shellenbaum } 13040a586ceaSMark Shellenbaum 13050a586ceaSMark Shellenbaum void 13060a586ceaSMark Shellenbaum dmu_tx_hold_sa_create(dmu_tx_t *tx, int attrsize) 13070a586ceaSMark Shellenbaum { 13080a586ceaSMark Shellenbaum sa_os_t *sa = tx->tx_objset->os_sa; 13090a586ceaSMark Shellenbaum 13100a586ceaSMark Shellenbaum dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 13110a586ceaSMark Shellenbaum 13120a586ceaSMark Shellenbaum if (tx->tx_objset->os_sa->sa_master_obj == 0) 13130a586ceaSMark Shellenbaum return; 13140a586ceaSMark Shellenbaum 13150a586ceaSMark Shellenbaum if (tx->tx_objset->os_sa->sa_layout_attr_obj) 13160a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, sa->sa_layout_attr_obj, B_TRUE, NULL); 13170a586ceaSMark Shellenbaum else { 13180a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_LAYOUTS); 13190a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_REGISTRY); 13200a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL); 13210a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL); 13220a586ceaSMark Shellenbaum } 13230a586ceaSMark Shellenbaum 13240a586ceaSMark Shellenbaum dmu_tx_sa_registration_hold(sa, tx); 13250a586ceaSMark Shellenbaum 13260a586ceaSMark Shellenbaum if (attrsize <= DN_MAX_BONUSLEN && !sa->sa_force_spill) 13270a586ceaSMark Shellenbaum return; 13280a586ceaSMark Shellenbaum 13290a586ceaSMark Shellenbaum (void) dmu_tx_hold_object_impl(tx, tx->tx_objset, DMU_NEW_OBJECT, 13300a586ceaSMark Shellenbaum THT_SPILL, 0, 0); 13310a586ceaSMark Shellenbaum } 13320a586ceaSMark Shellenbaum 13330a586ceaSMark Shellenbaum /* 13340a586ceaSMark Shellenbaum * Hold SA attribute 13350a586ceaSMark Shellenbaum * 13360a586ceaSMark Shellenbaum * dmu_tx_hold_sa(dmu_tx_t *tx, sa_handle_t *, attribute, add, size) 13370a586ceaSMark Shellenbaum * 13380a586ceaSMark Shellenbaum * variable_size is the total size of all variable sized attributes 13390a586ceaSMark Shellenbaum * passed to this function. It is not the total size of all 13400a586ceaSMark Shellenbaum * variable size attributes that *may* exist on this object. 13410a586ceaSMark Shellenbaum */ 13420a586ceaSMark Shellenbaum void 13430a586ceaSMark Shellenbaum dmu_tx_hold_sa(dmu_tx_t *tx, sa_handle_t *hdl, boolean_t may_grow) 13440a586ceaSMark Shellenbaum { 13450a586ceaSMark Shellenbaum uint64_t object; 13460a586ceaSMark Shellenbaum sa_os_t *sa = tx->tx_objset->os_sa; 13470a586ceaSMark Shellenbaum 13480a586ceaSMark Shellenbaum ASSERT(hdl != NULL); 13490a586ceaSMark Shellenbaum 13500a586ceaSMark Shellenbaum object = sa_handle_object(hdl); 13510a586ceaSMark Shellenbaum 13520a586ceaSMark Shellenbaum dmu_tx_hold_bonus(tx, object); 13530a586ceaSMark Shellenbaum 13540a586ceaSMark Shellenbaum if (tx->tx_objset->os_sa->sa_master_obj == 0) 13550a586ceaSMark Shellenbaum return; 13560a586ceaSMark Shellenbaum 13570a586ceaSMark Shellenbaum if (tx->tx_objset->os_sa->sa_reg_attr_obj == 0 || 13580a586ceaSMark Shellenbaum tx->tx_objset->os_sa->sa_layout_attr_obj == 0) { 13590a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_LAYOUTS); 13600a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_REGISTRY); 13610a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL); 13620a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL); 13630a586ceaSMark Shellenbaum } 13640a586ceaSMark Shellenbaum 13650a586ceaSMark Shellenbaum dmu_tx_sa_registration_hold(sa, tx); 13660a586ceaSMark Shellenbaum 13670a586ceaSMark Shellenbaum if (may_grow && tx->tx_objset->os_sa->sa_layout_attr_obj) 13680a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, sa->sa_layout_attr_obj, B_TRUE, NULL); 13690a586ceaSMark Shellenbaum 1370744947dcSTom Erickson if (sa->sa_force_spill || may_grow || hdl->sa_spill) { 13710a586ceaSMark Shellenbaum ASSERT(tx->tx_txg == 0); 13720a586ceaSMark Shellenbaum dmu_tx_hold_spill(tx, object); 1373744947dcSTom Erickson } else { 1374744947dcSTom Erickson dmu_buf_impl_t *db = (dmu_buf_impl_t *)hdl->sa_bonus; 1375744947dcSTom Erickson dnode_t *dn; 1376744947dcSTom Erickson 1377744947dcSTom Erickson DB_DNODE_ENTER(db); 1378744947dcSTom Erickson dn = DB_DNODE(db); 1379744947dcSTom Erickson if (dn->dn_have_spill) { 1380744947dcSTom Erickson ASSERT(tx->tx_txg == 0); 1381744947dcSTom Erickson dmu_tx_hold_spill(tx, object); 1382744947dcSTom Erickson } 1383744947dcSTom Erickson DB_DNODE_EXIT(db); 13840a586ceaSMark Shellenbaum } 13850a586ceaSMark Shellenbaum } 1386