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. 24bda88194SGeorge Wilson * Copyright (c) 2013 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; 514445fffbSMatthew Ahrens if (dd != NULL) 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)); 5769962b56SMatthew Ahrens tx->tx_start = gethrtime(); 588a2f1b91Sahrens #ifdef ZFS_DEBUG 59fa9e4066Sahrens refcount_create(&tx->tx_space_written); 60fa9e4066Sahrens refcount_create(&tx->tx_space_freed); 618a2f1b91Sahrens #endif 62fa9e4066Sahrens return (tx); 63fa9e4066Sahrens } 64fa9e4066Sahrens 65fa9e4066Sahrens dmu_tx_t * 66fa9e4066Sahrens dmu_tx_create(objset_t *os) 67fa9e4066Sahrens { 68503ad85cSMatthew Ahrens dmu_tx_t *tx = dmu_tx_create_dd(os->os_dsl_dataset->ds_dir); 69fa9e4066Sahrens tx->tx_objset = os; 70503ad85cSMatthew Ahrens tx->tx_lastsnap_txg = dsl_dataset_prev_snap_txg(os->os_dsl_dataset); 71fa9e4066Sahrens return (tx); 72fa9e4066Sahrens } 73fa9e4066Sahrens 74fa9e4066Sahrens dmu_tx_t * 75fa9e4066Sahrens dmu_tx_create_assigned(struct dsl_pool *dp, uint64_t txg) 76fa9e4066Sahrens { 771d452cf5Sahrens dmu_tx_t *tx = dmu_tx_create_dd(NULL); 78fa9e4066Sahrens 79fa9e4066Sahrens ASSERT3U(txg, <=, dp->dp_tx.tx_open_txg); 80fa9e4066Sahrens tx->tx_pool = dp; 81fa9e4066Sahrens tx->tx_txg = txg; 82fa9e4066Sahrens tx->tx_anyobj = TRUE; 83fa9e4066Sahrens 84fa9e4066Sahrens return (tx); 85fa9e4066Sahrens } 86fa9e4066Sahrens 87fa9e4066Sahrens int 88fa9e4066Sahrens dmu_tx_is_syncing(dmu_tx_t *tx) 89fa9e4066Sahrens { 90fa9e4066Sahrens return (tx->tx_anyobj); 91fa9e4066Sahrens } 92fa9e4066Sahrens 93fa9e4066Sahrens int 94fa9e4066Sahrens dmu_tx_private_ok(dmu_tx_t *tx) 95fa9e4066Sahrens { 96ea8dc4b6Seschrock return (tx->tx_anyobj); 97fa9e4066Sahrens } 98fa9e4066Sahrens 998a2f1b91Sahrens static dmu_tx_hold_t * 100fa9e4066Sahrens dmu_tx_hold_object_impl(dmu_tx_t *tx, objset_t *os, uint64_t object, 1018a2f1b91Sahrens enum dmu_tx_hold_type type, uint64_t arg1, uint64_t arg2) 102fa9e4066Sahrens { 1038a2f1b91Sahrens dmu_tx_hold_t *txh; 104fa9e4066Sahrens dnode_t *dn = NULL; 105ea8dc4b6Seschrock int err; 106fa9e4066Sahrens 107fa9e4066Sahrens if (object != DMU_NEW_OBJECT) { 108503ad85cSMatthew Ahrens err = dnode_hold(os, object, tx, &dn); 109ea8dc4b6Seschrock if (err) { 110ea8dc4b6Seschrock tx->tx_err = err; 1118a2f1b91Sahrens return (NULL); 112ea8dc4b6Seschrock } 113fa9e4066Sahrens 114ea8dc4b6Seschrock if (err == 0 && tx->tx_txg != 0) { 115fa9e4066Sahrens mutex_enter(&dn->dn_mtx); 116fa9e4066Sahrens /* 117fa9e4066Sahrens * dn->dn_assigned_txg == tx->tx_txg doesn't pose a 118fa9e4066Sahrens * problem, but there's no way for it to happen (for 119fa9e4066Sahrens * now, at least). 120fa9e4066Sahrens */ 121fa9e4066Sahrens ASSERT(dn->dn_assigned_txg == 0); 122fa9e4066Sahrens dn->dn_assigned_txg = tx->tx_txg; 123fa9e4066Sahrens (void) refcount_add(&dn->dn_tx_holds, tx); 124fa9e4066Sahrens mutex_exit(&dn->dn_mtx); 125fa9e4066Sahrens } 126fa9e4066Sahrens } 127fa9e4066Sahrens 1288a2f1b91Sahrens txh = kmem_zalloc(sizeof (dmu_tx_hold_t), KM_SLEEP); 1298a2f1b91Sahrens txh->txh_tx = tx; 1308a2f1b91Sahrens txh->txh_dnode = dn; 1318a2f1b91Sahrens #ifdef ZFS_DEBUG 1328a2f1b91Sahrens txh->txh_type = type; 1338a2f1b91Sahrens txh->txh_arg1 = arg1; 1348a2f1b91Sahrens txh->txh_arg2 = arg2; 1358a2f1b91Sahrens #endif 1368a2f1b91Sahrens list_insert_tail(&tx->tx_holds, txh); 137ea8dc4b6Seschrock 1388a2f1b91Sahrens return (txh); 139fa9e4066Sahrens } 140fa9e4066Sahrens 141fa9e4066Sahrens void 142fa9e4066Sahrens dmu_tx_add_new_object(dmu_tx_t *tx, objset_t *os, uint64_t object) 143fa9e4066Sahrens { 144fa9e4066Sahrens /* 145fa9e4066Sahrens * If we're syncing, they can manipulate any object anyhow, and 146fa9e4066Sahrens * the hold on the dnode_t can cause problems. 147fa9e4066Sahrens */ 148fa9e4066Sahrens if (!dmu_tx_is_syncing(tx)) { 1498a2f1b91Sahrens (void) dmu_tx_hold_object_impl(tx, os, 1508a2f1b91Sahrens object, THT_NEWOBJECT, 0, 0); 151fa9e4066Sahrens } 152fa9e4066Sahrens } 153fa9e4066Sahrens 154ea8dc4b6Seschrock static int 155ea8dc4b6Seschrock dmu_tx_check_ioerr(zio_t *zio, dnode_t *dn, int level, uint64_t blkid) 156ea8dc4b6Seschrock { 157ea8dc4b6Seschrock int err; 158ea8dc4b6Seschrock dmu_buf_impl_t *db; 159ea8dc4b6Seschrock 160ea8dc4b6Seschrock rw_enter(&dn->dn_struct_rwlock, RW_READER); 161ea8dc4b6Seschrock db = dbuf_hold_level(dn, level, blkid, FTAG); 162ea8dc4b6Seschrock rw_exit(&dn->dn_struct_rwlock); 163ea8dc4b6Seschrock if (db == NULL) 164be6fd75aSMatthew Ahrens return (SET_ERROR(EIO)); 1651ab7f2deSmaybee err = dbuf_read(db, zio, DB_RF_CANFAIL | DB_RF_NOPREFETCH); 166ea8dc4b6Seschrock dbuf_rele(db, FTAG); 167ea8dc4b6Seschrock return (err); 168ea8dc4b6Seschrock } 169ea8dc4b6Seschrock 1704a7f2a75SMark Maybee static void 171b24ab676SJeff Bonwick dmu_tx_count_twig(dmu_tx_hold_t *txh, dnode_t *dn, dmu_buf_impl_t *db, 172b24ab676SJeff Bonwick int level, uint64_t blkid, boolean_t freeable, uint64_t *history) 1734a7f2a75SMark Maybee { 174b24ab676SJeff Bonwick objset_t *os = dn->dn_objset; 175b24ab676SJeff Bonwick dsl_dataset_t *ds = os->os_dsl_dataset; 176b24ab676SJeff Bonwick int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 177b24ab676SJeff Bonwick dmu_buf_impl_t *parent = NULL; 178b24ab676SJeff Bonwick blkptr_t *bp = NULL; 179b24ab676SJeff Bonwick uint64_t space; 180b24ab676SJeff Bonwick 181b24ab676SJeff Bonwick if (level >= dn->dn_nlevels || history[level] == blkid) 1824a7f2a75SMark Maybee return; 1834a7f2a75SMark Maybee 184b24ab676SJeff Bonwick history[level] = blkid; 1854a7f2a75SMark Maybee 186b24ab676SJeff Bonwick space = (level == 0) ? dn->dn_datablksz : (1ULL << dn->dn_indblkshift); 1874a7f2a75SMark Maybee 188b24ab676SJeff Bonwick if (db == NULL || db == dn->dn_dbuf) { 189b24ab676SJeff Bonwick ASSERT(level != 0); 190b24ab676SJeff Bonwick db = NULL; 191b24ab676SJeff Bonwick } else { 192744947dcSTom Erickson ASSERT(DB_DNODE(db) == dn); 193b24ab676SJeff Bonwick ASSERT(db->db_level == level); 194b24ab676SJeff Bonwick ASSERT(db->db.db_size == space); 195b24ab676SJeff Bonwick ASSERT(db->db_blkid == blkid); 196b24ab676SJeff Bonwick bp = db->db_blkptr; 197b24ab676SJeff Bonwick parent = db->db_parent; 1984a7f2a75SMark Maybee } 199b24ab676SJeff Bonwick 200b24ab676SJeff Bonwick freeable = (bp && (freeable || 201c7cd2421SGeorge Wilson dsl_dataset_block_freeable(ds, bp, bp->blk_birth))); 202b24ab676SJeff Bonwick 203b24ab676SJeff Bonwick if (freeable) 204b24ab676SJeff Bonwick txh->txh_space_tooverwrite += space; 205b24ab676SJeff Bonwick else 206b24ab676SJeff Bonwick txh->txh_space_towrite += space; 207b24ab676SJeff Bonwick if (bp) 208b24ab676SJeff Bonwick txh->txh_space_tounref += bp_get_dsize(os->os_spa, bp); 209b24ab676SJeff Bonwick 210b24ab676SJeff Bonwick dmu_tx_count_twig(txh, dn, parent, level + 1, 211b24ab676SJeff Bonwick blkid >> epbs, freeable, history); 2124a7f2a75SMark Maybee } 2134a7f2a75SMark Maybee 214fa9e4066Sahrens /* ARGSUSED */ 215fa9e4066Sahrens static void 2168a2f1b91Sahrens dmu_tx_count_write(dmu_tx_hold_t *txh, uint64_t off, uint64_t len) 217fa9e4066Sahrens { 2188a2f1b91Sahrens dnode_t *dn = txh->txh_dnode; 2198a2f1b91Sahrens uint64_t start, end, i; 220fa9e4066Sahrens int min_bs, max_bs, min_ibs, max_ibs, epbs, bits; 2218a2f1b91Sahrens int err = 0; 222fa9e4066Sahrens 223fa9e4066Sahrens if (len == 0) 224fa9e4066Sahrens return; 225fa9e4066Sahrens 226fa9e4066Sahrens min_bs = SPA_MINBLOCKSHIFT; 227fa9e4066Sahrens max_bs = SPA_MAXBLOCKSHIFT; 228fa9e4066Sahrens min_ibs = DN_MIN_INDBLKSHIFT; 229fa9e4066Sahrens max_ibs = DN_MAX_INDBLKSHIFT; 230fa9e4066Sahrens 2318a2f1b91Sahrens if (dn) { 232b24ab676SJeff Bonwick uint64_t history[DN_MAX_LEVELS]; 2334a7f2a75SMark Maybee int nlvls = dn->dn_nlevels; 2344a7f2a75SMark Maybee int delta; 2354a7f2a75SMark Maybee 2364a7f2a75SMark Maybee /* 2374a7f2a75SMark Maybee * For i/o error checking, read the first and last level-0 2384a7f2a75SMark Maybee * blocks (if they are not aligned), and all the level-1 blocks. 2394a7f2a75SMark Maybee */ 240ea8dc4b6Seschrock if (dn->dn_maxblkid == 0) { 2414a7f2a75SMark Maybee delta = dn->dn_datablksz; 2424a7f2a75SMark Maybee start = (off < dn->dn_datablksz) ? 0 : 1; 2434a7f2a75SMark Maybee end = (off+len <= dn->dn_datablksz) ? 0 : 1; 2444a7f2a75SMark Maybee if (start == 0 && (off > 0 || len < dn->dn_datablksz)) { 24582c9918fSTim Haley err = dmu_tx_check_ioerr(NULL, dn, 0, 0); 24682c9918fSTim Haley if (err) 24782c9918fSTim Haley goto out; 2484a7f2a75SMark Maybee delta -= off; 24982c9918fSTim Haley } 250ea8dc4b6Seschrock } else { 2518a2f1b91Sahrens zio_t *zio = zio_root(dn->dn_objset->os_spa, 252ea8dc4b6Seschrock NULL, NULL, ZIO_FLAG_CANFAIL); 253ea8dc4b6Seschrock 254ea8dc4b6Seschrock /* first level-0 block */ 25599653d4eSeschrock start = off >> dn->dn_datablkshift; 25699653d4eSeschrock if (P2PHASE(off, dn->dn_datablksz) || 25799653d4eSeschrock len < dn->dn_datablksz) { 25899653d4eSeschrock err = dmu_tx_check_ioerr(zio, dn, 0, start); 2598a2f1b91Sahrens if (err) 2608a2f1b91Sahrens goto out; 261ea8dc4b6Seschrock } 262ea8dc4b6Seschrock 263ea8dc4b6Seschrock /* last level-0 block */ 26499653d4eSeschrock end = (off+len-1) >> dn->dn_datablkshift; 26582c9918fSTim Haley if (end != start && end <= dn->dn_maxblkid && 26699653d4eSeschrock P2PHASE(off+len, dn->dn_datablksz)) { 267ea8dc4b6Seschrock err = dmu_tx_check_ioerr(zio, dn, 0, end); 2688a2f1b91Sahrens if (err) 2698a2f1b91Sahrens goto out; 270ea8dc4b6Seschrock } 271ea8dc4b6Seschrock 272ea8dc4b6Seschrock /* level-1 blocks */ 2734a7f2a75SMark Maybee if (nlvls > 1) { 2744a7f2a75SMark Maybee int shft = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 2754a7f2a75SMark Maybee for (i = (start>>shft)+1; i < end>>shft; i++) { 276ea8dc4b6Seschrock err = dmu_tx_check_ioerr(zio, dn, 1, i); 2778a2f1b91Sahrens if (err) 2788a2f1b91Sahrens goto out; 279ea8dc4b6Seschrock } 280ea8dc4b6Seschrock } 281ea8dc4b6Seschrock 282ea8dc4b6Seschrock err = zio_wait(zio); 2838a2f1b91Sahrens if (err) 2848a2f1b91Sahrens goto out; 2854a7f2a75SMark Maybee delta = P2NPHASE(off, dn->dn_datablksz); 286ea8dc4b6Seschrock } 287ea8dc4b6Seschrock 288bda88194SGeorge Wilson min_ibs = max_ibs = dn->dn_indblkshift; 2894a7f2a75SMark Maybee if (dn->dn_maxblkid > 0) { 2904a7f2a75SMark Maybee /* 2914a7f2a75SMark Maybee * The blocksize can't change, 2924a7f2a75SMark Maybee * so we can make a more precise estimate. 2934a7f2a75SMark Maybee */ 2944a7f2a75SMark Maybee ASSERT(dn->dn_datablkshift != 0); 295fa9e4066Sahrens min_bs = max_bs = dn->dn_datablkshift; 2964a7f2a75SMark Maybee } 2974a7f2a75SMark Maybee 2984a7f2a75SMark Maybee /* 2994a7f2a75SMark Maybee * If this write is not off the end of the file 3004a7f2a75SMark Maybee * we need to account for overwrites/unref. 3014a7f2a75SMark Maybee */ 302b24ab676SJeff Bonwick if (start <= dn->dn_maxblkid) { 303b24ab676SJeff Bonwick for (int l = 0; l < DN_MAX_LEVELS; l++) 304b24ab676SJeff Bonwick history[l] = -1ULL; 305b24ab676SJeff Bonwick } 3064a7f2a75SMark Maybee while (start <= dn->dn_maxblkid) { 3074a7f2a75SMark Maybee dmu_buf_impl_t *db; 3084a7f2a75SMark Maybee 3094a7f2a75SMark Maybee rw_enter(&dn->dn_struct_rwlock, RW_READER); 31001025c89SJohn Harres err = dbuf_hold_impl(dn, 0, start, FALSE, FTAG, &db); 3114a7f2a75SMark Maybee rw_exit(&dn->dn_struct_rwlock); 31201025c89SJohn Harres 31301025c89SJohn Harres if (err) { 31401025c89SJohn Harres txh->txh_tx->tx_err = err; 31501025c89SJohn Harres return; 31601025c89SJohn Harres } 31701025c89SJohn Harres 318b24ab676SJeff Bonwick dmu_tx_count_twig(txh, dn, db, 0, start, B_FALSE, 319b24ab676SJeff Bonwick history); 3204a7f2a75SMark Maybee dbuf_rele(db, FTAG); 3214a7f2a75SMark Maybee if (++start > end) { 3224a7f2a75SMark Maybee /* 3234a7f2a75SMark Maybee * Account for new indirects appearing 3244a7f2a75SMark Maybee * before this IO gets assigned into a txg. 3254a7f2a75SMark Maybee */ 3264a7f2a75SMark Maybee bits = 64 - min_bs; 3274a7f2a75SMark Maybee epbs = min_ibs - SPA_BLKPTRSHIFT; 3284a7f2a75SMark Maybee for (bits -= epbs * (nlvls - 1); 3294a7f2a75SMark Maybee bits >= 0; bits -= epbs) 3304a7f2a75SMark Maybee txh->txh_fudge += 1ULL << max_ibs; 3314a7f2a75SMark Maybee goto out; 3324a7f2a75SMark Maybee } 3334a7f2a75SMark Maybee off += delta; 3344a7f2a75SMark Maybee if (len >= delta) 3354a7f2a75SMark Maybee len -= delta; 3364a7f2a75SMark Maybee delta = dn->dn_datablksz; 3374a7f2a75SMark Maybee } 338fa9e4066Sahrens } 339fa9e4066Sahrens 340fa9e4066Sahrens /* 341fa9e4066Sahrens * 'end' is the last thing we will access, not one past. 342fa9e4066Sahrens * This way we won't overflow when accessing the last byte. 343fa9e4066Sahrens */ 344fa9e4066Sahrens start = P2ALIGN(off, 1ULL << max_bs); 345fa9e4066Sahrens end = P2ROUNDUP(off + len, 1ULL << max_bs) - 1; 3468a2f1b91Sahrens txh->txh_space_towrite += end - start + 1; 347fa9e4066Sahrens 348fa9e4066Sahrens start >>= min_bs; 349fa9e4066Sahrens end >>= min_bs; 350fa9e4066Sahrens 351fa9e4066Sahrens epbs = min_ibs - SPA_BLKPTRSHIFT; 352fa9e4066Sahrens 353fa9e4066Sahrens /* 354fa9e4066Sahrens * The object contains at most 2^(64 - min_bs) blocks, 355fa9e4066Sahrens * and each indirect level maps 2^epbs. 356fa9e4066Sahrens */ 357fa9e4066Sahrens for (bits = 64 - min_bs; bits >= 0; bits -= epbs) { 358fa9e4066Sahrens start >>= epbs; 359fa9e4066Sahrens end >>= epbs; 3604a7f2a75SMark Maybee ASSERT3U(end, >=, start); 3618a2f1b91Sahrens txh->txh_space_towrite += (end - start + 1) << max_ibs; 3624a7f2a75SMark Maybee if (start != 0) { 3634a7f2a75SMark Maybee /* 3644a7f2a75SMark Maybee * We also need a new blkid=0 indirect block 3654a7f2a75SMark Maybee * to reference any existing file data. 3664a7f2a75SMark Maybee */ 3674a7f2a75SMark Maybee txh->txh_space_towrite += 1ULL << max_ibs; 3684a7f2a75SMark Maybee } 369fa9e4066Sahrens } 370fa9e4066Sahrens 3718a2f1b91Sahrens out: 3724a7f2a75SMark Maybee if (txh->txh_space_towrite + txh->txh_space_tooverwrite > 3734a7f2a75SMark Maybee 2 * DMU_MAX_ACCESS) 374be6fd75aSMatthew Ahrens err = SET_ERROR(EFBIG); 3754a7f2a75SMark Maybee 3768a2f1b91Sahrens if (err) 3778a2f1b91Sahrens txh->txh_tx->tx_err = err; 378fa9e4066Sahrens } 379fa9e4066Sahrens 380fa9e4066Sahrens static void 3818a2f1b91Sahrens dmu_tx_count_dnode(dmu_tx_hold_t *txh) 382fa9e4066Sahrens { 3838a2f1b91Sahrens dnode_t *dn = txh->txh_dnode; 384744947dcSTom Erickson dnode_t *mdn = DMU_META_DNODE(txh->txh_tx->tx_objset); 3858a2f1b91Sahrens uint64_t space = mdn->dn_datablksz + 3868a2f1b91Sahrens ((mdn->dn_nlevels-1) << mdn->dn_indblkshift); 387fa9e4066Sahrens 388fa9e4066Sahrens if (dn && dn->dn_dbuf->db_blkptr && 389fa9e4066Sahrens dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset, 390c7cd2421SGeorge Wilson dn->dn_dbuf->db_blkptr, dn->dn_dbuf->db_blkptr->blk_birth)) { 3918a2f1b91Sahrens txh->txh_space_tooverwrite += space; 3924a7f2a75SMark Maybee txh->txh_space_tounref += space; 3938a2f1b91Sahrens } else { 3948a2f1b91Sahrens txh->txh_space_towrite += space; 395a9799022Sck if (dn && dn->dn_dbuf->db_blkptr) 396a9799022Sck txh->txh_space_tounref += space; 397fa9e4066Sahrens } 398fa9e4066Sahrens } 399fa9e4066Sahrens 400fa9e4066Sahrens void 401fa9e4066Sahrens dmu_tx_hold_write(dmu_tx_t *tx, uint64_t object, uint64_t off, int len) 402fa9e4066Sahrens { 4038a2f1b91Sahrens dmu_tx_hold_t *txh; 4048a2f1b91Sahrens 405fa9e4066Sahrens ASSERT(tx->tx_txg == 0); 406ea8dc4b6Seschrock ASSERT(len < DMU_MAX_ACCESS); 407dd6ef538Smaybee ASSERT(len == 0 || UINT64_MAX - off >= len - 1); 408fa9e4066Sahrens 4098a2f1b91Sahrens txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, 4108a2f1b91Sahrens object, THT_WRITE, off, len); 4118a2f1b91Sahrens if (txh == NULL) 4128a2f1b91Sahrens return; 4138a2f1b91Sahrens 4148a2f1b91Sahrens dmu_tx_count_write(txh, off, len); 4158a2f1b91Sahrens dmu_tx_count_dnode(txh); 416fa9e4066Sahrens } 417fa9e4066Sahrens 418fa9e4066Sahrens static void 4198a2f1b91Sahrens dmu_tx_count_free(dmu_tx_hold_t *txh, uint64_t off, uint64_t len) 420fa9e4066Sahrens { 421cdb0ab79Smaybee uint64_t blkid, nblks, lastblk; 422cdb0ab79Smaybee uint64_t space = 0, unref = 0, skipped = 0; 4238a2f1b91Sahrens dnode_t *dn = txh->txh_dnode; 424fa9e4066Sahrens dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset; 4258a2f1b91Sahrens spa_t *spa = txh->txh_tx->tx_pool->dp_spa; 426cdb0ab79Smaybee int epbs; 42731495a1eSArne Jansen uint64_t l0span = 0, nl1blks = 0; 428fa9e4066Sahrens 429cdb0ab79Smaybee if (dn->dn_nlevels == 0) 430fa9e4066Sahrens return; 431c543ec06Sahrens 432fa9e4066Sahrens /* 433cdb0ab79Smaybee * The struct_rwlock protects us against dn_nlevels 434c543ec06Sahrens * changing, in case (against all odds) we manage to dirty & 435c543ec06Sahrens * sync out the changes after we check for being dirty. 43601025c89SJohn Harres * Also, dbuf_hold_impl() wants us to have the struct_rwlock. 437fa9e4066Sahrens */ 438fa9e4066Sahrens rw_enter(&dn->dn_struct_rwlock, RW_READER); 439cdb0ab79Smaybee epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 440cdb0ab79Smaybee if (dn->dn_maxblkid == 0) { 441c543ec06Sahrens if (off == 0 && len >= dn->dn_datablksz) { 442c543ec06Sahrens blkid = 0; 443c543ec06Sahrens nblks = 1; 444c543ec06Sahrens } else { 445c543ec06Sahrens rw_exit(&dn->dn_struct_rwlock); 446c543ec06Sahrens return; 447c543ec06Sahrens } 448c543ec06Sahrens } else { 449c543ec06Sahrens blkid = off >> dn->dn_datablkshift; 450cdb0ab79Smaybee nblks = (len + dn->dn_datablksz - 1) >> dn->dn_datablkshift; 451fa9e4066Sahrens 452be9000ccSMatthew Ahrens if (blkid > dn->dn_maxblkid) { 453c543ec06Sahrens rw_exit(&dn->dn_struct_rwlock); 454c543ec06Sahrens return; 455c543ec06Sahrens } 456cdb0ab79Smaybee if (blkid + nblks > dn->dn_maxblkid) 457be9000ccSMatthew Ahrens nblks = dn->dn_maxblkid - blkid + 1; 458fa9e4066Sahrens 459c543ec06Sahrens } 46031495a1eSArne Jansen l0span = nblks; /* save for later use to calc level > 1 overhead */ 461cdb0ab79Smaybee if (dn->dn_nlevels == 1) { 462fa9e4066Sahrens int i; 463fa9e4066Sahrens for (i = 0; i < nblks; i++) { 464fa9e4066Sahrens blkptr_t *bp = dn->dn_phys->dn_blkptr; 465cdb0ab79Smaybee ASSERT3U(blkid + i, <, dn->dn_nblkptr); 466fa9e4066Sahrens bp += blkid + i; 467c7cd2421SGeorge Wilson if (dsl_dataset_block_freeable(ds, bp, bp->blk_birth)) { 468fa9e4066Sahrens dprintf_bp(bp, "can free old%s", ""); 469b24ab676SJeff Bonwick space += bp_get_dsize(spa, bp); 470fa9e4066Sahrens } 471a9799022Sck unref += BP_GET_ASIZE(bp); 472fa9e4066Sahrens } 47331495a1eSArne Jansen nl1blks = 1; 474ea8dc4b6Seschrock nblks = 0; 475fa9e4066Sahrens } 476fa9e4066Sahrens 477cdb0ab79Smaybee lastblk = blkid + nblks - 1; 478fa9e4066Sahrens while (nblks) { 479fa9e4066Sahrens dmu_buf_impl_t *dbuf; 480cdb0ab79Smaybee uint64_t ibyte, new_blkid; 481cdb0ab79Smaybee int epb = 1 << epbs; 482cdb0ab79Smaybee int err, i, blkoff, tochk; 483cdb0ab79Smaybee blkptr_t *bp; 484cdb0ab79Smaybee 485cdb0ab79Smaybee ibyte = blkid << dn->dn_datablkshift; 486cdb0ab79Smaybee err = dnode_next_offset(dn, 487cdb0ab79Smaybee DNODE_FIND_HAVELOCK, &ibyte, 2, 1, 0); 488cdb0ab79Smaybee new_blkid = ibyte >> dn->dn_datablkshift; 489b7e50089Smaybee if (err == ESRCH) { 490b7e50089Smaybee skipped += (lastblk >> epbs) - (blkid >> epbs) + 1; 491cdb0ab79Smaybee break; 492b7e50089Smaybee } 493cdb0ab79Smaybee if (err) { 494cdb0ab79Smaybee txh->txh_tx->tx_err = err; 495cdb0ab79Smaybee break; 496cdb0ab79Smaybee } 497b7e50089Smaybee if (new_blkid > lastblk) { 498b7e50089Smaybee skipped += (lastblk >> epbs) - (blkid >> epbs) + 1; 499cdb0ab79Smaybee break; 500b7e50089Smaybee } 501fa9e4066Sahrens 502cdb0ab79Smaybee if (new_blkid > blkid) { 503b7e50089Smaybee ASSERT((new_blkid >> epbs) > (blkid >> epbs)); 504b7e50089Smaybee skipped += (new_blkid >> epbs) - (blkid >> epbs) - 1; 505cdb0ab79Smaybee nblks -= new_blkid - blkid; 506cdb0ab79Smaybee blkid = new_blkid; 507cdb0ab79Smaybee } 508cdb0ab79Smaybee blkoff = P2PHASE(blkid, epb); 509cdb0ab79Smaybee tochk = MIN(epb - blkoff, nblks); 510fa9e4066Sahrens 51101025c89SJohn Harres err = dbuf_hold_impl(dn, 1, blkid >> epbs, FALSE, FTAG, &dbuf); 51201025c89SJohn Harres if (err) { 51301025c89SJohn Harres txh->txh_tx->tx_err = err; 51401025c89SJohn Harres break; 51501025c89SJohn Harres } 516cdb0ab79Smaybee 517cdb0ab79Smaybee txh->txh_memory_tohold += dbuf->db.db_size; 51877179d12SLori Alt 51977179d12SLori Alt /* 52077179d12SLori Alt * We don't check memory_tohold against DMU_MAX_ACCESS because 52177179d12SLori Alt * memory_tohold is an over-estimation (especially the >L1 52277179d12SLori Alt * indirect blocks), so it could fail. Callers should have 52377179d12SLori Alt * already verified that they will not be holding too much 52477179d12SLori Alt * memory. 52577179d12SLori Alt */ 52677179d12SLori Alt 527cdb0ab79Smaybee err = dbuf_read(dbuf, NULL, DB_RF_HAVESTRUCT | DB_RF_CANFAIL); 528cdb0ab79Smaybee if (err != 0) { 5298a2f1b91Sahrens txh->txh_tx->tx_err = err; 530cdb0ab79Smaybee dbuf_rele(dbuf, FTAG); 531c543ec06Sahrens break; 532fa9e4066Sahrens } 533fa9e4066Sahrens 534cdb0ab79Smaybee bp = dbuf->db.db_data; 535cdb0ab79Smaybee bp += blkoff; 536cdb0ab79Smaybee 537cdb0ab79Smaybee for (i = 0; i < tochk; i++) { 538c7cd2421SGeorge Wilson if (dsl_dataset_block_freeable(ds, &bp[i], 539c7cd2421SGeorge Wilson bp[i].blk_birth)) { 540cdb0ab79Smaybee dprintf_bp(&bp[i], "can free old%s", ""); 541b24ab676SJeff Bonwick space += bp_get_dsize(spa, &bp[i]); 542cdb0ab79Smaybee } 543cdb0ab79Smaybee unref += BP_GET_ASIZE(bp); 544cdb0ab79Smaybee } 545cdb0ab79Smaybee dbuf_rele(dbuf, FTAG); 546cdb0ab79Smaybee 54731495a1eSArne Jansen ++nl1blks; 548fa9e4066Sahrens blkid += tochk; 549fa9e4066Sahrens nblks -= tochk; 550fa9e4066Sahrens } 551fa9e4066Sahrens rw_exit(&dn->dn_struct_rwlock); 552fa9e4066Sahrens 55331495a1eSArne Jansen /* 55431495a1eSArne Jansen * Add in memory requirements of higher-level indirects. 55531495a1eSArne Jansen * This assumes a worst-possible scenario for dn_nlevels and a 55631495a1eSArne Jansen * worst-possible distribution of l1-blocks over the region to free. 55731495a1eSArne Jansen */ 55831495a1eSArne Jansen { 55931495a1eSArne Jansen uint64_t blkcnt = 1 + ((l0span >> epbs) >> epbs); 56031495a1eSArne Jansen int level = 2; 56131495a1eSArne Jansen /* 56231495a1eSArne Jansen * Here we don't use DN_MAX_LEVEL, but calculate it with the 56331495a1eSArne Jansen * given datablkshift and indblkshift. This makes the 56431495a1eSArne Jansen * difference between 19 and 8 on large files. 56531495a1eSArne Jansen */ 56631495a1eSArne Jansen int maxlevel = 2 + (DN_MAX_OFFSET_SHIFT - dn->dn_datablkshift) / 56731495a1eSArne Jansen (dn->dn_indblkshift - SPA_BLKPTRSHIFT); 56831495a1eSArne Jansen 56931495a1eSArne Jansen while (level++ < maxlevel) { 5708f0b538dSChristopher Siden txh->txh_memory_tohold += MAX(MIN(blkcnt, nl1blks), 1) 57131495a1eSArne Jansen << dn->dn_indblkshift; 57231495a1eSArne Jansen blkcnt = 1 + (blkcnt >> epbs); 57331495a1eSArne Jansen } 57431495a1eSArne Jansen } 57531495a1eSArne Jansen 576cdb0ab79Smaybee /* account for new level 1 indirect blocks that might show up */ 577b7e50089Smaybee if (skipped > 0) { 578715614a4Smaybee txh->txh_fudge += skipped << dn->dn_indblkshift; 579cdb0ab79Smaybee skipped = MIN(skipped, DMU_MAX_DELETEBLKCNT >> epbs); 580cdb0ab79Smaybee txh->txh_memory_tohold += skipped << dn->dn_indblkshift; 581cdb0ab79Smaybee } 5828a2f1b91Sahrens txh->txh_space_tofree += space; 583a9799022Sck txh->txh_space_tounref += unref; 584fa9e4066Sahrens } 585fa9e4066Sahrens 5868a2f1b91Sahrens void 5878a2f1b91Sahrens dmu_tx_hold_free(dmu_tx_t *tx, uint64_t object, uint64_t off, uint64_t len) 588fa9e4066Sahrens { 5898a2f1b91Sahrens dmu_tx_hold_t *txh; 5908a2f1b91Sahrens dnode_t *dn; 5912f3d8780SMatthew Ahrens int err; 592ea8dc4b6Seschrock zio_t *zio; 593fa9e4066Sahrens 5948a2f1b91Sahrens ASSERT(tx->tx_txg == 0); 5958a2f1b91Sahrens 5968a2f1b91Sahrens txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, 5978a2f1b91Sahrens object, THT_FREE, off, len); 5988a2f1b91Sahrens if (txh == NULL) 5998a2f1b91Sahrens return; 6008a2f1b91Sahrens dn = txh->txh_dnode; 60169962b56SMatthew Ahrens dmu_tx_count_dnode(txh); 6028a2f1b91Sahrens 603fa9e4066Sahrens if (off >= (dn->dn_maxblkid+1) * dn->dn_datablksz) 604fa9e4066Sahrens return; 605fa9e4066Sahrens if (len == DMU_OBJECT_END) 606fa9e4066Sahrens len = (dn->dn_maxblkid+1) * dn->dn_datablksz - off; 607fa9e4066Sahrens 608ea8dc4b6Seschrock /* 6092f3d8780SMatthew Ahrens * For i/o error checking, we read the first and last level-0 6102f3d8780SMatthew Ahrens * blocks if they are not aligned, and all the level-1 blocks. 6112f3d8780SMatthew Ahrens * 6122f3d8780SMatthew Ahrens * Note: dbuf_free_range() assumes that we have not instantiated 6132f3d8780SMatthew Ahrens * any level-0 dbufs that will be completely freed. Therefore we must 6142f3d8780SMatthew Ahrens * exercise care to not read or count the first and last blocks 6152f3d8780SMatthew Ahrens * if they are blocksize-aligned. 6162f3d8780SMatthew Ahrens */ 6172f3d8780SMatthew Ahrens if (dn->dn_datablkshift == 0) { 618713d6c20SMatthew Ahrens if (off != 0 || len < dn->dn_datablksz) 619*5253393bSMatthew Ahrens dmu_tx_count_write(txh, 0, dn->dn_datablksz); 6202f3d8780SMatthew Ahrens } else { 6212f3d8780SMatthew Ahrens /* first block will be modified if it is not aligned */ 6222f3d8780SMatthew Ahrens if (!IS_P2ALIGNED(off, 1 << dn->dn_datablkshift)) 6232f3d8780SMatthew Ahrens dmu_tx_count_write(txh, off, 1); 6242f3d8780SMatthew Ahrens /* last block will be modified if it is not aligned */ 6252f3d8780SMatthew Ahrens if (!IS_P2ALIGNED(off + len, 1 << dn->dn_datablkshift)) 6262f3d8780SMatthew Ahrens dmu_tx_count_write(txh, off+len, 1); 6272f3d8780SMatthew Ahrens } 6282f3d8780SMatthew Ahrens 6292f3d8780SMatthew Ahrens /* 6302f3d8780SMatthew Ahrens * Check level-1 blocks. 631ea8dc4b6Seschrock */ 63298572ac1Sahrens if (dn->dn_nlevels > 1) { 6332f3d8780SMatthew Ahrens int shift = dn->dn_datablkshift + dn->dn_indblkshift - 63498572ac1Sahrens SPA_BLKPTRSHIFT; 6352f3d8780SMatthew Ahrens uint64_t start = off >> shift; 6362f3d8780SMatthew Ahrens uint64_t end = (off + len) >> shift; 6372f3d8780SMatthew Ahrens 6382f3d8780SMatthew Ahrens ASSERT(dn->dn_datablkshift != 0); 6392f3d8780SMatthew Ahrens ASSERT(dn->dn_indblkshift != 0); 64098572ac1Sahrens 64198572ac1Sahrens zio = zio_root(tx->tx_pool->dp_spa, 64298572ac1Sahrens NULL, NULL, ZIO_FLAG_CANFAIL); 6432f3d8780SMatthew Ahrens for (uint64_t i = start; i <= end; i++) { 64498572ac1Sahrens uint64_t ibyte = i << shift; 645cdb0ab79Smaybee err = dnode_next_offset(dn, 0, &ibyte, 2, 1, 0); 64698572ac1Sahrens i = ibyte >> shift; 64798572ac1Sahrens if (err == ESRCH) 64898572ac1Sahrens break; 64998572ac1Sahrens if (err) { 65098572ac1Sahrens tx->tx_err = err; 65198572ac1Sahrens return; 65298572ac1Sahrens } 653ea8dc4b6Seschrock 65498572ac1Sahrens err = dmu_tx_check_ioerr(zio, dn, 1, i); 65598572ac1Sahrens if (err) { 65698572ac1Sahrens tx->tx_err = err; 65798572ac1Sahrens return; 65898572ac1Sahrens } 65998572ac1Sahrens } 66098572ac1Sahrens err = zio_wait(zio); 661ea8dc4b6Seschrock if (err) { 662ea8dc4b6Seschrock tx->tx_err = err; 663ea8dc4b6Seschrock return; 664ea8dc4b6Seschrock } 665ea8dc4b6Seschrock } 666ea8dc4b6Seschrock 6678a2f1b91Sahrens dmu_tx_count_free(txh, off, len); 668fa9e4066Sahrens } 669fa9e4066Sahrens 670fa9e4066Sahrens void 67114843421SMatthew Ahrens dmu_tx_hold_zap(dmu_tx_t *tx, uint64_t object, int add, const char *name) 672fa9e4066Sahrens { 6738a2f1b91Sahrens dmu_tx_hold_t *txh; 6748a2f1b91Sahrens dnode_t *dn; 675fa9e4066Sahrens uint64_t nblocks; 676ea8dc4b6Seschrock int epbs, err; 677fa9e4066Sahrens 6788a2f1b91Sahrens ASSERT(tx->tx_txg == 0); 6798a2f1b91Sahrens 6808a2f1b91Sahrens txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, 6818a2f1b91Sahrens object, THT_ZAP, add, (uintptr_t)name); 6828a2f1b91Sahrens if (txh == NULL) 6838a2f1b91Sahrens return; 6848a2f1b91Sahrens dn = txh->txh_dnode; 6858a2f1b91Sahrens 6868a2f1b91Sahrens dmu_tx_count_dnode(txh); 687fa9e4066Sahrens 688fa9e4066Sahrens if (dn == NULL) { 689fa9e4066Sahrens /* 690ea8dc4b6Seschrock * We will be able to fit a new object's entries into one leaf 691fa9e4066Sahrens * block. So there will be at most 2 blocks total, 692fa9e4066Sahrens * including the header block. 693fa9e4066Sahrens */ 6948a2f1b91Sahrens dmu_tx_count_write(txh, 0, 2 << fzap_default_block_shift); 695fa9e4066Sahrens return; 696fa9e4066Sahrens } 697fa9e4066Sahrens 698ad135b5dSChristopher Siden ASSERT3P(DMU_OT_BYTESWAP(dn->dn_type), ==, DMU_BSWAP_ZAP); 699fa9e4066Sahrens 700ea8dc4b6Seschrock if (dn->dn_maxblkid == 0 && !add) { 7019dccfd2aSAlbert Lee blkptr_t *bp; 7029dccfd2aSAlbert Lee 703fa9e4066Sahrens /* 704fa9e4066Sahrens * If there is only one block (i.e. this is a micro-zap) 705ea8dc4b6Seschrock * and we are not adding anything, the accounting is simple. 706fa9e4066Sahrens */ 707ea8dc4b6Seschrock err = dmu_tx_check_ioerr(NULL, dn, 0, 0); 708ea8dc4b6Seschrock if (err) { 709ea8dc4b6Seschrock tx->tx_err = err; 710ea8dc4b6Seschrock return; 711ea8dc4b6Seschrock } 712ea8dc4b6Seschrock 713b6130eadSmaybee /* 714b6130eadSmaybee * Use max block size here, since we don't know how much 715b6130eadSmaybee * the size will change between now and the dbuf dirty call. 716b6130eadSmaybee */ 7179dccfd2aSAlbert Lee bp = &dn->dn_phys->dn_blkptr[0]; 718fa9e4066Sahrens if (dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset, 7199dccfd2aSAlbert Lee bp, bp->blk_birth)) 720b6130eadSmaybee txh->txh_space_tooverwrite += SPA_MAXBLOCKSIZE; 7219dccfd2aSAlbert Lee else 722b6130eadSmaybee txh->txh_space_towrite += SPA_MAXBLOCKSIZE; 7239dccfd2aSAlbert Lee if (!BP_IS_HOLE(bp)) 724f878aa38SChris Kirby txh->txh_space_tounref += SPA_MAXBLOCKSIZE; 725fa9e4066Sahrens return; 726fa9e4066Sahrens } 727fa9e4066Sahrens 728ea8dc4b6Seschrock if (dn->dn_maxblkid > 0 && name) { 729ea8dc4b6Seschrock /* 730ea8dc4b6Seschrock * access the name in this fat-zap so that we'll check 731ea8dc4b6Seschrock * for i/o errors to the leaf blocks, etc. 732ea8dc4b6Seschrock */ 733503ad85cSMatthew Ahrens err = zap_lookup(dn->dn_objset, dn->dn_object, name, 734ea8dc4b6Seschrock 8, 0, NULL); 735ea8dc4b6Seschrock if (err == EIO) { 736ea8dc4b6Seschrock tx->tx_err = err; 737ea8dc4b6Seschrock return; 738ea8dc4b6Seschrock } 739ea8dc4b6Seschrock } 740ea8dc4b6Seschrock 741503ad85cSMatthew Ahrens err = zap_count_write(dn->dn_objset, dn->dn_object, name, add, 742720d1aa1SSanjeev Bagewadi &txh->txh_space_towrite, &txh->txh_space_tooverwrite); 743fa9e4066Sahrens 744fa9e4066Sahrens /* 745fa9e4066Sahrens * If the modified blocks are scattered to the four winds, 746fa9e4066Sahrens * we'll have to modify an indirect twig for each. 747fa9e4066Sahrens */ 748fa9e4066Sahrens epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 749fa9e4066Sahrens for (nblocks = dn->dn_maxblkid >> epbs; nblocks != 0; nblocks >>= epbs) 7503d692628SSanjeev Bagewadi if (dn->dn_objset->os_dsl_dataset->ds_phys->ds_prev_snap_obj) 7513d692628SSanjeev Bagewadi txh->txh_space_towrite += 3 << dn->dn_indblkshift; 7523d692628SSanjeev Bagewadi else 7533d692628SSanjeev Bagewadi txh->txh_space_tooverwrite += 3 << dn->dn_indblkshift; 754fa9e4066Sahrens } 755fa9e4066Sahrens 756fa9e4066Sahrens void 757fa9e4066Sahrens dmu_tx_hold_bonus(dmu_tx_t *tx, uint64_t object) 758fa9e4066Sahrens { 7598a2f1b91Sahrens dmu_tx_hold_t *txh; 760fa9e4066Sahrens 7618a2f1b91Sahrens ASSERT(tx->tx_txg == 0); 762fa9e4066Sahrens 7638a2f1b91Sahrens txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, 7648a2f1b91Sahrens object, THT_BONUS, 0, 0); 7658a2f1b91Sahrens if (txh) 7668a2f1b91Sahrens dmu_tx_count_dnode(txh); 767fa9e4066Sahrens } 768fa9e4066Sahrens 769fa9e4066Sahrens void 770fa9e4066Sahrens dmu_tx_hold_space(dmu_tx_t *tx, uint64_t space) 771fa9e4066Sahrens { 7728a2f1b91Sahrens dmu_tx_hold_t *txh; 773fa9e4066Sahrens ASSERT(tx->tx_txg == 0); 774fa9e4066Sahrens 7758a2f1b91Sahrens txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, 7768a2f1b91Sahrens DMU_NEW_OBJECT, THT_SPACE, space, 0); 7778a2f1b91Sahrens 7788a2f1b91Sahrens txh->txh_space_towrite += space; 779fa9e4066Sahrens } 780fa9e4066Sahrens 781fa9e4066Sahrens int 782fa9e4066Sahrens dmu_tx_holds(dmu_tx_t *tx, uint64_t object) 783fa9e4066Sahrens { 7848a2f1b91Sahrens dmu_tx_hold_t *txh; 785fa9e4066Sahrens int holds = 0; 786fa9e4066Sahrens 787fa9e4066Sahrens /* 788fa9e4066Sahrens * By asserting that the tx is assigned, we're counting the 789fa9e4066Sahrens * number of dn_tx_holds, which is the same as the number of 790fa9e4066Sahrens * dn_holds. Otherwise, we'd be counting dn_holds, but 791fa9e4066Sahrens * dn_tx_holds could be 0. 792fa9e4066Sahrens */ 793fa9e4066Sahrens ASSERT(tx->tx_txg != 0); 794fa9e4066Sahrens 795fa9e4066Sahrens /* if (tx->tx_anyobj == TRUE) */ 796fa9e4066Sahrens /* return (0); */ 797fa9e4066Sahrens 7988a2f1b91Sahrens for (txh = list_head(&tx->tx_holds); txh; 7998a2f1b91Sahrens txh = list_next(&tx->tx_holds, txh)) { 8008a2f1b91Sahrens if (txh->txh_dnode && txh->txh_dnode->dn_object == object) 801fa9e4066Sahrens holds++; 802fa9e4066Sahrens } 803fa9e4066Sahrens 804fa9e4066Sahrens return (holds); 805fa9e4066Sahrens } 806fa9e4066Sahrens 8079c9dc39aSek #ifdef ZFS_DEBUG 808fa9e4066Sahrens void 809fa9e4066Sahrens dmu_tx_dirty_buf(dmu_tx_t *tx, dmu_buf_impl_t *db) 810fa9e4066Sahrens { 8118a2f1b91Sahrens dmu_tx_hold_t *txh; 812fa9e4066Sahrens int match_object = FALSE, match_offset = FALSE; 813744947dcSTom Erickson dnode_t *dn; 814fa9e4066Sahrens 815744947dcSTom Erickson DB_DNODE_ENTER(db); 816744947dcSTom Erickson dn = DB_DNODE(db); 817fa9e4066Sahrens ASSERT(tx->tx_txg != 0); 818503ad85cSMatthew Ahrens ASSERT(tx->tx_objset == NULL || dn->dn_objset == tx->tx_objset); 819fa9e4066Sahrens ASSERT3U(dn->dn_object, ==, db->db.db_object); 820fa9e4066Sahrens 821744947dcSTom Erickson if (tx->tx_anyobj) { 822744947dcSTom Erickson DB_DNODE_EXIT(db); 823fa9e4066Sahrens return; 824744947dcSTom Erickson } 825fa9e4066Sahrens 826fa9e4066Sahrens /* XXX No checking on the meta dnode for now */ 827744947dcSTom Erickson if (db->db.db_object == DMU_META_DNODE_OBJECT) { 828744947dcSTom Erickson DB_DNODE_EXIT(db); 829fa9e4066Sahrens return; 830744947dcSTom Erickson } 831fa9e4066Sahrens 8328a2f1b91Sahrens for (txh = list_head(&tx->tx_holds); txh; 8338a2f1b91Sahrens txh = list_next(&tx->tx_holds, txh)) { 834fa9e4066Sahrens ASSERT(dn == NULL || dn->dn_assigned_txg == tx->tx_txg); 8358a2f1b91Sahrens if (txh->txh_dnode == dn && txh->txh_type != THT_NEWOBJECT) 836fa9e4066Sahrens match_object = TRUE; 8378a2f1b91Sahrens if (txh->txh_dnode == NULL || txh->txh_dnode == dn) { 838fa9e4066Sahrens int datablkshift = dn->dn_datablkshift ? 839fa9e4066Sahrens dn->dn_datablkshift : SPA_MAXBLOCKSHIFT; 840fa9e4066Sahrens int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 841fa9e4066Sahrens int shift = datablkshift + epbs * db->db_level; 842fa9e4066Sahrens uint64_t beginblk = shift >= 64 ? 0 : 8438a2f1b91Sahrens (txh->txh_arg1 >> shift); 844fa9e4066Sahrens uint64_t endblk = shift >= 64 ? 0 : 8458a2f1b91Sahrens ((txh->txh_arg1 + txh->txh_arg2 - 1) >> shift); 846fa9e4066Sahrens uint64_t blkid = db->db_blkid; 847fa9e4066Sahrens 8488a2f1b91Sahrens /* XXX txh_arg2 better not be zero... */ 849fa9e4066Sahrens 8508a2f1b91Sahrens dprintf("found txh type %x beginblk=%llx endblk=%llx\n", 8518a2f1b91Sahrens txh->txh_type, beginblk, endblk); 852fa9e4066Sahrens 8538a2f1b91Sahrens switch (txh->txh_type) { 854fa9e4066Sahrens case THT_WRITE: 855fa9e4066Sahrens if (blkid >= beginblk && blkid <= endblk) 856fa9e4066Sahrens match_offset = TRUE; 857fa9e4066Sahrens /* 858fa9e4066Sahrens * We will let this hold work for the bonus 8590a586ceaSMark Shellenbaum * or spill buffer so that we don't need to 8600a586ceaSMark Shellenbaum * hold it when creating a new object. 861fa9e4066Sahrens */ 8620a586ceaSMark Shellenbaum if (blkid == DMU_BONUS_BLKID || 8630a586ceaSMark Shellenbaum blkid == DMU_SPILL_BLKID) 864fa9e4066Sahrens match_offset = TRUE; 865fa9e4066Sahrens /* 866fa9e4066Sahrens * They might have to increase nlevels, 867fa9e4066Sahrens * thus dirtying the new TLIBs. Or the 868fa9e4066Sahrens * might have to change the block size, 869fa9e4066Sahrens * thus dirying the new lvl=0 blk=0. 870fa9e4066Sahrens */ 871fa9e4066Sahrens if (blkid == 0) 872fa9e4066Sahrens match_offset = TRUE; 873fa9e4066Sahrens break; 874fa9e4066Sahrens case THT_FREE: 875cdb0ab79Smaybee /* 876cdb0ab79Smaybee * We will dirty all the level 1 blocks in 877cdb0ab79Smaybee * the free range and perhaps the first and 878cdb0ab79Smaybee * last level 0 block. 879cdb0ab79Smaybee */ 880cdb0ab79Smaybee if (blkid >= beginblk && (blkid <= endblk || 881cdb0ab79Smaybee txh->txh_arg2 == DMU_OBJECT_END)) 882fa9e4066Sahrens match_offset = TRUE; 883fa9e4066Sahrens break; 8840a586ceaSMark Shellenbaum case THT_SPILL: 8850a586ceaSMark Shellenbaum if (blkid == DMU_SPILL_BLKID) 8860a586ceaSMark Shellenbaum match_offset = TRUE; 8870a586ceaSMark Shellenbaum break; 888fa9e4066Sahrens case THT_BONUS: 8890a586ceaSMark Shellenbaum if (blkid == DMU_BONUS_BLKID) 890fa9e4066Sahrens match_offset = TRUE; 891fa9e4066Sahrens break; 892fa9e4066Sahrens case THT_ZAP: 893fa9e4066Sahrens match_offset = TRUE; 894fa9e4066Sahrens break; 895fa9e4066Sahrens case THT_NEWOBJECT: 896fa9e4066Sahrens match_object = TRUE; 897fa9e4066Sahrens break; 898fa9e4066Sahrens default: 8998a2f1b91Sahrens ASSERT(!"bad txh_type"); 900fa9e4066Sahrens } 901fa9e4066Sahrens } 902744947dcSTom Erickson if (match_object && match_offset) { 903744947dcSTom Erickson DB_DNODE_EXIT(db); 904fa9e4066Sahrens return; 905744947dcSTom Erickson } 906fa9e4066Sahrens } 907744947dcSTom Erickson DB_DNODE_EXIT(db); 908fa9e4066Sahrens panic("dirtying dbuf obj=%llx lvl=%u blkid=%llx but not tx_held\n", 909fa9e4066Sahrens (u_longlong_t)db->db.db_object, db->db_level, 910fa9e4066Sahrens (u_longlong_t)db->db_blkid); 911fa9e4066Sahrens } 9129c9dc39aSek #endif 913fa9e4066Sahrens 91469962b56SMatthew Ahrens /* 91569962b56SMatthew Ahrens * If we can't do 10 iops, something is wrong. Let us go ahead 91669962b56SMatthew Ahrens * and hit zfs_dirty_data_max. 91769962b56SMatthew Ahrens */ 91869962b56SMatthew Ahrens hrtime_t zfs_delay_max_ns = MSEC2NSEC(100); 91969962b56SMatthew Ahrens int zfs_delay_resolution_ns = 100 * 1000; /* 100 microseconds */ 92069962b56SMatthew Ahrens 92169962b56SMatthew Ahrens /* 92269962b56SMatthew Ahrens * We delay transactions when we've determined that the backend storage 92369962b56SMatthew Ahrens * isn't able to accommodate the rate of incoming writes. 92469962b56SMatthew Ahrens * 92569962b56SMatthew Ahrens * If there is already a transaction waiting, we delay relative to when 92669962b56SMatthew Ahrens * that transaction finishes waiting. This way the calculated min_time 92769962b56SMatthew Ahrens * is independent of the number of threads concurrently executing 92869962b56SMatthew Ahrens * transactions. 92969962b56SMatthew Ahrens * 93069962b56SMatthew Ahrens * If we are the only waiter, wait relative to when the transaction 93169962b56SMatthew Ahrens * started, rather than the current time. This credits the transaction for 93269962b56SMatthew Ahrens * "time already served", e.g. reading indirect blocks. 93369962b56SMatthew Ahrens * 93469962b56SMatthew Ahrens * The minimum time for a transaction to take is calculated as: 93569962b56SMatthew Ahrens * min_time = scale * (dirty - min) / (max - dirty) 93669962b56SMatthew Ahrens * min_time is then capped at zfs_delay_max_ns. 93769962b56SMatthew Ahrens * 93869962b56SMatthew Ahrens * The delay has two degrees of freedom that can be adjusted via tunables. 93969962b56SMatthew Ahrens * The percentage of dirty data at which we start to delay is defined by 94069962b56SMatthew Ahrens * zfs_delay_min_dirty_percent. This should typically be at or above 94169962b56SMatthew Ahrens * zfs_vdev_async_write_active_max_dirty_percent so that we only start to 94269962b56SMatthew Ahrens * delay after writing at full speed has failed to keep up with the incoming 94369962b56SMatthew Ahrens * write rate. The scale of the curve is defined by zfs_delay_scale. Roughly 94469962b56SMatthew Ahrens * speaking, this variable determines the amount of delay at the midpoint of 94569962b56SMatthew Ahrens * the curve. 94669962b56SMatthew Ahrens * 94769962b56SMatthew Ahrens * delay 94869962b56SMatthew Ahrens * 10ms +-------------------------------------------------------------*+ 94969962b56SMatthew Ahrens * | *| 95069962b56SMatthew Ahrens * 9ms + *+ 95169962b56SMatthew Ahrens * | *| 95269962b56SMatthew Ahrens * 8ms + *+ 95369962b56SMatthew Ahrens * | * | 95469962b56SMatthew Ahrens * 7ms + * + 95569962b56SMatthew Ahrens * | * | 95669962b56SMatthew Ahrens * 6ms + * + 95769962b56SMatthew Ahrens * | * | 95869962b56SMatthew Ahrens * 5ms + * + 95969962b56SMatthew Ahrens * | * | 96069962b56SMatthew Ahrens * 4ms + * + 96169962b56SMatthew Ahrens * | * | 96269962b56SMatthew Ahrens * 3ms + * + 96369962b56SMatthew Ahrens * | * | 96469962b56SMatthew Ahrens * 2ms + (midpoint) * + 96569962b56SMatthew Ahrens * | | ** | 96669962b56SMatthew Ahrens * 1ms + v *** + 96769962b56SMatthew Ahrens * | zfs_delay_scale ----------> ******** | 96869962b56SMatthew Ahrens * 0 +-------------------------------------*********----------------+ 96969962b56SMatthew Ahrens * 0% <- zfs_dirty_data_max -> 100% 97069962b56SMatthew Ahrens * 97169962b56SMatthew Ahrens * Note that since the delay is added to the outstanding time remaining on the 97269962b56SMatthew Ahrens * most recent transaction, the delay is effectively the inverse of IOPS. 97369962b56SMatthew Ahrens * Here the midpoint of 500us translates to 2000 IOPS. The shape of the curve 97469962b56SMatthew Ahrens * was chosen such that small changes in the amount of accumulated dirty data 97569962b56SMatthew Ahrens * in the first 3/4 of the curve yield relatively small differences in the 97669962b56SMatthew Ahrens * amount of delay. 97769962b56SMatthew Ahrens * 97869962b56SMatthew Ahrens * The effects can be easier to understand when the amount of delay is 97969962b56SMatthew Ahrens * represented on a log scale: 98069962b56SMatthew Ahrens * 98169962b56SMatthew Ahrens * delay 98269962b56SMatthew Ahrens * 100ms +-------------------------------------------------------------++ 98369962b56SMatthew Ahrens * + + 98469962b56SMatthew Ahrens * | | 98569962b56SMatthew Ahrens * + *+ 98669962b56SMatthew Ahrens * 10ms + *+ 98769962b56SMatthew Ahrens * + ** + 98869962b56SMatthew Ahrens * | (midpoint) ** | 98969962b56SMatthew Ahrens * + | ** + 99069962b56SMatthew Ahrens * 1ms + v **** + 99169962b56SMatthew Ahrens * + zfs_delay_scale ----------> ***** + 99269962b56SMatthew Ahrens * | **** | 99369962b56SMatthew Ahrens * + **** + 99469962b56SMatthew Ahrens * 100us + ** + 99569962b56SMatthew Ahrens * + * + 99669962b56SMatthew Ahrens * | * | 99769962b56SMatthew Ahrens * + * + 99869962b56SMatthew Ahrens * 10us + * + 99969962b56SMatthew Ahrens * + + 100069962b56SMatthew Ahrens * | | 100169962b56SMatthew Ahrens * + + 100269962b56SMatthew Ahrens * +--------------------------------------------------------------+ 100369962b56SMatthew Ahrens * 0% <- zfs_dirty_data_max -> 100% 100469962b56SMatthew Ahrens * 100569962b56SMatthew Ahrens * Note here that only as the amount of dirty data approaches its limit does 100669962b56SMatthew Ahrens * the delay start to increase rapidly. The goal of a properly tuned system 100769962b56SMatthew Ahrens * should be to keep the amount of dirty data out of that range by first 100869962b56SMatthew Ahrens * ensuring that the appropriate limits are set for the I/O scheduler to reach 100969962b56SMatthew Ahrens * optimal throughput on the backend storage, and then by changing the value 101069962b56SMatthew Ahrens * of zfs_delay_scale to increase the steepness of the curve. 101169962b56SMatthew Ahrens */ 101269962b56SMatthew Ahrens static void 101369962b56SMatthew Ahrens dmu_tx_delay(dmu_tx_t *tx, uint64_t dirty) 101469962b56SMatthew Ahrens { 101569962b56SMatthew Ahrens dsl_pool_t *dp = tx->tx_pool; 101669962b56SMatthew Ahrens uint64_t delay_min_bytes = 101769962b56SMatthew Ahrens zfs_dirty_data_max * zfs_delay_min_dirty_percent / 100; 101869962b56SMatthew Ahrens hrtime_t wakeup, min_tx_time, now; 101969962b56SMatthew Ahrens 102069962b56SMatthew Ahrens if (dirty <= delay_min_bytes) 102169962b56SMatthew Ahrens return; 102269962b56SMatthew Ahrens 102369962b56SMatthew Ahrens /* 102469962b56SMatthew Ahrens * The caller has already waited until we are under the max. 102569962b56SMatthew Ahrens * We make them pass us the amount of dirty data so we don't 102669962b56SMatthew Ahrens * have to handle the case of it being >= the max, which could 102769962b56SMatthew Ahrens * cause a divide-by-zero if it's == the max. 102869962b56SMatthew Ahrens */ 102969962b56SMatthew Ahrens ASSERT3U(dirty, <, zfs_dirty_data_max); 103069962b56SMatthew Ahrens 103169962b56SMatthew Ahrens now = gethrtime(); 103269962b56SMatthew Ahrens min_tx_time = zfs_delay_scale * 103369962b56SMatthew Ahrens (dirty - delay_min_bytes) / (zfs_dirty_data_max - dirty); 103469962b56SMatthew Ahrens if (now > tx->tx_start + min_tx_time) 103569962b56SMatthew Ahrens return; 103669962b56SMatthew Ahrens 103769962b56SMatthew Ahrens min_tx_time = MIN(min_tx_time, zfs_delay_max_ns); 103869962b56SMatthew Ahrens 103969962b56SMatthew Ahrens DTRACE_PROBE3(delay__mintime, dmu_tx_t *, tx, uint64_t, dirty, 104069962b56SMatthew Ahrens uint64_t, min_tx_time); 104169962b56SMatthew Ahrens 104269962b56SMatthew Ahrens mutex_enter(&dp->dp_lock); 104369962b56SMatthew Ahrens wakeup = MAX(tx->tx_start + min_tx_time, 104469962b56SMatthew Ahrens dp->dp_last_wakeup + min_tx_time); 104569962b56SMatthew Ahrens dp->dp_last_wakeup = wakeup; 104669962b56SMatthew Ahrens mutex_exit(&dp->dp_lock); 104769962b56SMatthew Ahrens 104869962b56SMatthew Ahrens #ifdef _KERNEL 104969962b56SMatthew Ahrens mutex_enter(&curthread->t_delay_lock); 105069962b56SMatthew Ahrens while (cv_timedwait_hires(&curthread->t_delay_cv, 105169962b56SMatthew Ahrens &curthread->t_delay_lock, wakeup, zfs_delay_resolution_ns, 105269962b56SMatthew Ahrens CALLOUT_FLAG_ABSOLUTE | CALLOUT_FLAG_ROUNDUP) > 0) 105369962b56SMatthew Ahrens continue; 105469962b56SMatthew Ahrens mutex_exit(&curthread->t_delay_lock); 105569962b56SMatthew Ahrens #else 105669962b56SMatthew Ahrens hrtime_t delta = wakeup - gethrtime(); 105769962b56SMatthew Ahrens struct timespec ts; 105869962b56SMatthew Ahrens ts.tv_sec = delta / NANOSEC; 105969962b56SMatthew Ahrens ts.tv_nsec = delta % NANOSEC; 106069962b56SMatthew Ahrens (void) nanosleep(&ts, NULL); 106169962b56SMatthew Ahrens #endif 106269962b56SMatthew Ahrens } 106369962b56SMatthew Ahrens 1064fa9e4066Sahrens static int 10653b2aab18SMatthew Ahrens dmu_tx_try_assign(dmu_tx_t *tx, txg_how_t txg_how) 1066fa9e4066Sahrens { 10678a2f1b91Sahrens dmu_tx_hold_t *txh; 10680a4e9518Sgw spa_t *spa = tx->tx_pool->dp_spa; 1069cdb0ab79Smaybee uint64_t memory, asize, fsize, usize; 1070715614a4Smaybee uint64_t towrite, tofree, tooverwrite, tounref, tohold, fudge; 1071fa9e4066Sahrens 1072fb09f5aaSMadhav Suresh ASSERT0(tx->tx_txg); 10730a4e9518Sgw 10748a2f1b91Sahrens if (tx->tx_err) 10758a2f1b91Sahrens return (tx->tx_err); 1076fa9e4066Sahrens 1077e14bb325SJeff Bonwick if (spa_suspended(spa)) { 10780a4e9518Sgw /* 10790a4e9518Sgw * If the user has indicated a blocking failure mode 10800a4e9518Sgw * then return ERESTART which will block in dmu_tx_wait(). 10810a4e9518Sgw * Otherwise, return EIO so that an error can get 10820a4e9518Sgw * propagated back to the VOP calls. 10830a4e9518Sgw * 10840a4e9518Sgw * Note that we always honor the txg_how flag regardless 10850a4e9518Sgw * of the failuremode setting. 10860a4e9518Sgw */ 10870a4e9518Sgw if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE && 10880a4e9518Sgw txg_how != TXG_WAIT) 1089be6fd75aSMatthew Ahrens return (SET_ERROR(EIO)); 10900a4e9518Sgw 1091be6fd75aSMatthew Ahrens return (SET_ERROR(ERESTART)); 10920a4e9518Sgw } 10930a4e9518Sgw 109469962b56SMatthew Ahrens if (!tx->tx_waited && 109569962b56SMatthew Ahrens dsl_pool_need_dirty_delay(tx->tx_pool)) { 109669962b56SMatthew Ahrens tx->tx_wait_dirty = B_TRUE; 109769962b56SMatthew Ahrens return (SET_ERROR(ERESTART)); 109869962b56SMatthew Ahrens } 109969962b56SMatthew Ahrens 1100fa9e4066Sahrens tx->tx_txg = txg_hold_open(tx->tx_pool, &tx->tx_txgh); 11018a2f1b91Sahrens tx->tx_needassign_txh = NULL; 1102fa9e4066Sahrens 11038a2f1b91Sahrens /* 11048a2f1b91Sahrens * NB: No error returns are allowed after txg_hold_open, but 11058a2f1b91Sahrens * before processing the dnode holds, due to the 11068a2f1b91Sahrens * dmu_tx_unassign() logic. 11078a2f1b91Sahrens */ 1108fa9e4066Sahrens 1109715614a4Smaybee towrite = tofree = tooverwrite = tounref = tohold = fudge = 0; 11108a2f1b91Sahrens for (txh = list_head(&tx->tx_holds); txh; 11118a2f1b91Sahrens txh = list_next(&tx->tx_holds, txh)) { 11128a2f1b91Sahrens dnode_t *dn = txh->txh_dnode; 1113fa9e4066Sahrens if (dn != NULL) { 1114fa9e4066Sahrens mutex_enter(&dn->dn_mtx); 11158a2f1b91Sahrens if (dn->dn_assigned_txg == tx->tx_txg - 1) { 11168a2f1b91Sahrens mutex_exit(&dn->dn_mtx); 11178a2f1b91Sahrens tx->tx_needassign_txh = txh; 1118be6fd75aSMatthew Ahrens return (SET_ERROR(ERESTART)); 1119fa9e4066Sahrens } 11208a2f1b91Sahrens if (dn->dn_assigned_txg == 0) 1121fa9e4066Sahrens dn->dn_assigned_txg = tx->tx_txg; 11228a2f1b91Sahrens ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg); 1123fa9e4066Sahrens (void) refcount_add(&dn->dn_tx_holds, tx); 1124fa9e4066Sahrens mutex_exit(&dn->dn_mtx); 1125fa9e4066Sahrens } 11268a2f1b91Sahrens towrite += txh->txh_space_towrite; 11278a2f1b91Sahrens tofree += txh->txh_space_tofree; 11288a2f1b91Sahrens tooverwrite += txh->txh_space_tooverwrite; 1129a9799022Sck tounref += txh->txh_space_tounref; 1130cdb0ab79Smaybee tohold += txh->txh_memory_tohold; 1131715614a4Smaybee fudge += txh->txh_fudge; 1132ea8dc4b6Seschrock } 1133ea8dc4b6Seschrock 1134ea8dc4b6Seschrock /* 1135ea8dc4b6Seschrock * If a snapshot has been taken since we made our estimates, 1136ea8dc4b6Seschrock * assume that we won't be able to free or overwrite anything. 1137ea8dc4b6Seschrock */ 1138ea8dc4b6Seschrock if (tx->tx_objset && 1139503ad85cSMatthew Ahrens dsl_dataset_prev_snap_txg(tx->tx_objset->os_dsl_dataset) > 1140ea8dc4b6Seschrock tx->tx_lastsnap_txg) { 11418a2f1b91Sahrens towrite += tooverwrite; 11428a2f1b91Sahrens tooverwrite = tofree = 0; 1143fa9e4066Sahrens } 1144fa9e4066Sahrens 1145cdb0ab79Smaybee /* needed allocation: worst-case estimate of write space */ 1146cdb0ab79Smaybee asize = spa_get_asize(tx->tx_pool->dp_spa, towrite + tooverwrite); 1147cdb0ab79Smaybee /* freed space estimate: worst-case overwrite + free estimate */ 11488a2f1b91Sahrens fsize = spa_get_asize(tx->tx_pool->dp_spa, tooverwrite) + tofree; 1149cdb0ab79Smaybee /* convert unrefd space to worst-case estimate */ 1150a9799022Sck usize = spa_get_asize(tx->tx_pool->dp_spa, tounref); 1151cdb0ab79Smaybee /* calculate memory footprint estimate */ 1152cdb0ab79Smaybee memory = towrite + tooverwrite + tohold; 11538a2f1b91Sahrens 11548a2f1b91Sahrens #ifdef ZFS_DEBUG 1155715614a4Smaybee /* 1156715614a4Smaybee * Add in 'tohold' to account for our dirty holds on this memory 1157715614a4Smaybee * XXX - the "fudge" factor is to account for skipped blocks that 1158715614a4Smaybee * we missed because dnode_next_offset() misses in-core-only blocks. 1159715614a4Smaybee */ 1160cdb0ab79Smaybee tx->tx_space_towrite = asize + 1161715614a4Smaybee spa_get_asize(tx->tx_pool->dp_spa, tohold + fudge); 11628a2f1b91Sahrens tx->tx_space_tofree = tofree; 11638a2f1b91Sahrens tx->tx_space_tooverwrite = tooverwrite; 1164a9799022Sck tx->tx_space_tounref = tounref; 11658a2f1b91Sahrens #endif 1166fa9e4066Sahrens 1167fa9e4066Sahrens if (tx->tx_dir && asize != 0) { 1168cdb0ab79Smaybee int err = dsl_dir_tempreserve_space(tx->tx_dir, memory, 1169cdb0ab79Smaybee asize, fsize, usize, &tx->tx_tempreserve_cookie, tx); 11708a2f1b91Sahrens if (err) 1171fa9e4066Sahrens return (err); 1172fa9e4066Sahrens } 1173fa9e4066Sahrens 1174fa9e4066Sahrens return (0); 1175fa9e4066Sahrens } 1176fa9e4066Sahrens 11778a2f1b91Sahrens static void 11788a2f1b91Sahrens dmu_tx_unassign(dmu_tx_t *tx) 1179fa9e4066Sahrens { 11808a2f1b91Sahrens dmu_tx_hold_t *txh; 1181fa9e4066Sahrens 11828a2f1b91Sahrens if (tx->tx_txg == 0) 11838a2f1b91Sahrens return; 1184fa9e4066Sahrens 1185fa9e4066Sahrens txg_rele_to_quiesce(&tx->tx_txgh); 1186fa9e4066Sahrens 11873e30c24aSWill Andrews /* 11883e30c24aSWill Andrews * Walk the transaction's hold list, removing the hold on the 11893e30c24aSWill Andrews * associated dnode, and notifying waiters if the refcount drops to 0. 11903e30c24aSWill Andrews */ 11918a2f1b91Sahrens for (txh = list_head(&tx->tx_holds); txh != tx->tx_needassign_txh; 11928a2f1b91Sahrens txh = list_next(&tx->tx_holds, txh)) { 11938a2f1b91Sahrens dnode_t *dn = txh->txh_dnode; 1194fa9e4066Sahrens 1195fa9e4066Sahrens if (dn == NULL) 1196fa9e4066Sahrens continue; 1197fa9e4066Sahrens mutex_enter(&dn->dn_mtx); 11988a2f1b91Sahrens ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg); 1199fa9e4066Sahrens 1200fa9e4066Sahrens if (refcount_remove(&dn->dn_tx_holds, tx) == 0) { 1201fa9e4066Sahrens dn->dn_assigned_txg = 0; 1202fa9e4066Sahrens cv_broadcast(&dn->dn_notxholds); 1203fa9e4066Sahrens } 1204fa9e4066Sahrens mutex_exit(&dn->dn_mtx); 1205fa9e4066Sahrens } 1206fa9e4066Sahrens 1207fa9e4066Sahrens txg_rele_to_sync(&tx->tx_txgh); 1208fa9e4066Sahrens 12098a2f1b91Sahrens tx->tx_lasttried_txg = tx->tx_txg; 1210fa9e4066Sahrens tx->tx_txg = 0; 1211fa9e4066Sahrens } 1212fa9e4066Sahrens 1213fa9e4066Sahrens /* 1214fa9e4066Sahrens * Assign tx to a transaction group. txg_how can be one of: 1215fa9e4066Sahrens * 1216fa9e4066Sahrens * (1) TXG_WAIT. If the current open txg is full, waits until there's 1217fa9e4066Sahrens * a new one. This should be used when you're not holding locks. 12183b2aab18SMatthew Ahrens * It will only fail if we're truly out of space (or over quota). 1219fa9e4066Sahrens * 1220fa9e4066Sahrens * (2) TXG_NOWAIT. If we can't assign into the current open txg without 1221fa9e4066Sahrens * blocking, returns immediately with ERESTART. This should be used 1222fa9e4066Sahrens * whenever you're holding locks. On an ERESTART error, the caller 12238a2f1b91Sahrens * should drop locks, do a dmu_tx_wait(tx), and try again. 122469962b56SMatthew Ahrens * 122569962b56SMatthew Ahrens * (3) TXG_WAITED. Like TXG_NOWAIT, but indicates that dmu_tx_wait() 122669962b56SMatthew Ahrens * has already been called on behalf of this operation (though 122769962b56SMatthew Ahrens * most likely on a different tx). 1228fa9e4066Sahrens */ 1229fa9e4066Sahrens int 12303b2aab18SMatthew Ahrens dmu_tx_assign(dmu_tx_t *tx, txg_how_t txg_how) 1231fa9e4066Sahrens { 1232fa9e4066Sahrens int err; 1233fa9e4066Sahrens 1234fa9e4066Sahrens ASSERT(tx->tx_txg == 0); 123569962b56SMatthew Ahrens ASSERT(txg_how == TXG_WAIT || txg_how == TXG_NOWAIT || 123669962b56SMatthew Ahrens txg_how == TXG_WAITED); 1237fa9e4066Sahrens ASSERT(!dsl_pool_sync_context(tx->tx_pool)); 1238fa9e4066Sahrens 12393b2aab18SMatthew Ahrens /* If we might wait, we must not hold the config lock. */ 12403b2aab18SMatthew Ahrens ASSERT(txg_how != TXG_WAIT || !dsl_pool_config_held(tx->tx_pool)); 12413b2aab18SMatthew Ahrens 124269962b56SMatthew Ahrens if (txg_how == TXG_WAITED) 124369962b56SMatthew Ahrens tx->tx_waited = B_TRUE; 124469962b56SMatthew Ahrens 12458a2f1b91Sahrens while ((err = dmu_tx_try_assign(tx, txg_how)) != 0) { 12468a2f1b91Sahrens dmu_tx_unassign(tx); 1247fa9e4066Sahrens 1248fa9e4066Sahrens if (err != ERESTART || txg_how != TXG_WAIT) 1249fa9e4066Sahrens return (err); 1250fa9e4066Sahrens 12518a2f1b91Sahrens dmu_tx_wait(tx); 1252fa9e4066Sahrens } 1253fa9e4066Sahrens 1254fa9e4066Sahrens txg_rele_to_quiesce(&tx->tx_txgh); 1255fa9e4066Sahrens 1256fa9e4066Sahrens return (0); 1257fa9e4066Sahrens } 1258fa9e4066Sahrens 12598a2f1b91Sahrens void 12608a2f1b91Sahrens dmu_tx_wait(dmu_tx_t *tx) 12618a2f1b91Sahrens { 12620a4e9518Sgw spa_t *spa = tx->tx_pool->dp_spa; 126369962b56SMatthew Ahrens dsl_pool_t *dp = tx->tx_pool; 12640a4e9518Sgw 12658a2f1b91Sahrens ASSERT(tx->tx_txg == 0); 12663b2aab18SMatthew Ahrens ASSERT(!dsl_pool_config_held(tx->tx_pool)); 12678a2f1b91Sahrens 126869962b56SMatthew Ahrens if (tx->tx_wait_dirty) { 126969962b56SMatthew Ahrens /* 127069962b56SMatthew Ahrens * dmu_tx_try_assign() has determined that we need to wait 127169962b56SMatthew Ahrens * because we've consumed much or all of the dirty buffer 127269962b56SMatthew Ahrens * space. 127369962b56SMatthew Ahrens */ 127469962b56SMatthew Ahrens mutex_enter(&dp->dp_lock); 127569962b56SMatthew Ahrens while (dp->dp_dirty_total >= zfs_dirty_data_max) 127669962b56SMatthew Ahrens cv_wait(&dp->dp_spaceavail_cv, &dp->dp_lock); 127769962b56SMatthew Ahrens uint64_t dirty = dp->dp_dirty_total; 127869962b56SMatthew Ahrens mutex_exit(&dp->dp_lock); 127969962b56SMatthew Ahrens 128069962b56SMatthew Ahrens dmu_tx_delay(tx, dirty); 128169962b56SMatthew Ahrens 128269962b56SMatthew Ahrens tx->tx_wait_dirty = B_FALSE; 128369962b56SMatthew Ahrens 128469962b56SMatthew Ahrens /* 128569962b56SMatthew Ahrens * Note: setting tx_waited only has effect if the caller 128669962b56SMatthew Ahrens * used TX_WAIT. Otherwise they are going to destroy 128769962b56SMatthew Ahrens * this tx and try again. The common case, zfs_write(), 128869962b56SMatthew Ahrens * uses TX_WAIT. 128969962b56SMatthew Ahrens */ 129069962b56SMatthew Ahrens tx->tx_waited = B_TRUE; 129169962b56SMatthew Ahrens } else if (spa_suspended(spa) || tx->tx_lasttried_txg == 0) { 129269962b56SMatthew Ahrens /* 129369962b56SMatthew Ahrens * If the pool is suspended we need to wait until it 129469962b56SMatthew Ahrens * is resumed. Note that it's possible that the pool 129569962b56SMatthew Ahrens * has become active after this thread has tried to 129669962b56SMatthew Ahrens * obtain a tx. If that's the case then tx_lasttried_txg 129769962b56SMatthew Ahrens * would not have been set. 129869962b56SMatthew Ahrens */ 129969962b56SMatthew Ahrens txg_wait_synced(dp, spa_last_synced_txg(spa) + 1); 13000a4e9518Sgw } else if (tx->tx_needassign_txh) { 130169962b56SMatthew Ahrens /* 130269962b56SMatthew Ahrens * A dnode is assigned to the quiescing txg. Wait for its 130369962b56SMatthew Ahrens * transaction to complete. 130469962b56SMatthew Ahrens */ 13058a2f1b91Sahrens dnode_t *dn = tx->tx_needassign_txh->txh_dnode; 13068a2f1b91Sahrens 13078a2f1b91Sahrens mutex_enter(&dn->dn_mtx); 13088a2f1b91Sahrens while (dn->dn_assigned_txg == tx->tx_lasttried_txg - 1) 13098a2f1b91Sahrens cv_wait(&dn->dn_notxholds, &dn->dn_mtx); 13108a2f1b91Sahrens mutex_exit(&dn->dn_mtx); 13118a2f1b91Sahrens tx->tx_needassign_txh = NULL; 13128a2f1b91Sahrens } else { 13138a2f1b91Sahrens txg_wait_open(tx->tx_pool, tx->tx_lasttried_txg + 1); 13148a2f1b91Sahrens } 13158a2f1b91Sahrens } 13168a2f1b91Sahrens 1317fa9e4066Sahrens void 1318fa9e4066Sahrens dmu_tx_willuse_space(dmu_tx_t *tx, int64_t delta) 1319fa9e4066Sahrens { 13208a2f1b91Sahrens #ifdef ZFS_DEBUG 1321fa9e4066Sahrens if (tx->tx_dir == NULL || delta == 0) 1322fa9e4066Sahrens return; 1323fa9e4066Sahrens 1324fa9e4066Sahrens if (delta > 0) { 1325fa9e4066Sahrens ASSERT3U(refcount_count(&tx->tx_space_written) + delta, <=, 1326fa9e4066Sahrens tx->tx_space_towrite); 1327fa9e4066Sahrens (void) refcount_add_many(&tx->tx_space_written, delta, NULL); 1328fa9e4066Sahrens } else { 1329fa9e4066Sahrens (void) refcount_add_many(&tx->tx_space_freed, -delta, NULL); 1330fa9e4066Sahrens } 13318a2f1b91Sahrens #endif 1332fa9e4066Sahrens } 1333fa9e4066Sahrens 1334fa9e4066Sahrens void 1335fa9e4066Sahrens dmu_tx_commit(dmu_tx_t *tx) 1336fa9e4066Sahrens { 13378a2f1b91Sahrens dmu_tx_hold_t *txh; 1338fa9e4066Sahrens 1339fa9e4066Sahrens ASSERT(tx->tx_txg != 0); 1340fa9e4066Sahrens 13413e30c24aSWill Andrews /* 13423e30c24aSWill Andrews * Go through the transaction's hold list and remove holds on 13433e30c24aSWill Andrews * associated dnodes, notifying waiters if no holds remain. 13443e30c24aSWill Andrews */ 13458a2f1b91Sahrens while (txh = list_head(&tx->tx_holds)) { 13468a2f1b91Sahrens dnode_t *dn = txh->txh_dnode; 1347fa9e4066Sahrens 13488a2f1b91Sahrens list_remove(&tx->tx_holds, txh); 13498a2f1b91Sahrens kmem_free(txh, sizeof (dmu_tx_hold_t)); 1350fa9e4066Sahrens if (dn == NULL) 1351fa9e4066Sahrens continue; 1352fa9e4066Sahrens mutex_enter(&dn->dn_mtx); 1353fa9e4066Sahrens ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg); 1354fa9e4066Sahrens 1355fa9e4066Sahrens if (refcount_remove(&dn->dn_tx_holds, tx) == 0) { 1356fa9e4066Sahrens dn->dn_assigned_txg = 0; 1357fa9e4066Sahrens cv_broadcast(&dn->dn_notxholds); 1358fa9e4066Sahrens } 1359fa9e4066Sahrens mutex_exit(&dn->dn_mtx); 1360fa9e4066Sahrens dnode_rele(dn, tx); 1361fa9e4066Sahrens } 1362fa9e4066Sahrens 13638a2f1b91Sahrens if (tx->tx_tempreserve_cookie) 1364fa9e4066Sahrens dsl_dir_tempreserve_clear(tx->tx_tempreserve_cookie, tx); 1365fa9e4066Sahrens 1366d20e665cSRicardo M. Correia if (!list_is_empty(&tx->tx_callbacks)) 1367d20e665cSRicardo M. Correia txg_register_callbacks(&tx->tx_txgh, &tx->tx_callbacks); 1368d20e665cSRicardo M. Correia 1369fa9e4066Sahrens if (tx->tx_anyobj == FALSE) 1370fa9e4066Sahrens txg_rele_to_sync(&tx->tx_txgh); 1371d20e665cSRicardo M. Correia 1372d20e665cSRicardo M. Correia list_destroy(&tx->tx_callbacks); 13738f38d419Sek list_destroy(&tx->tx_holds); 13748a2f1b91Sahrens #ifdef ZFS_DEBUG 1375fa9e4066Sahrens dprintf("towrite=%llu written=%llu tofree=%llu freed=%llu\n", 1376fa9e4066Sahrens tx->tx_space_towrite, refcount_count(&tx->tx_space_written), 1377fa9e4066Sahrens tx->tx_space_tofree, refcount_count(&tx->tx_space_freed)); 1378fa9e4066Sahrens refcount_destroy_many(&tx->tx_space_written, 1379fa9e4066Sahrens refcount_count(&tx->tx_space_written)); 1380fa9e4066Sahrens refcount_destroy_many(&tx->tx_space_freed, 1381fa9e4066Sahrens refcount_count(&tx->tx_space_freed)); 1382fa9e4066Sahrens #endif 1383fa9e4066Sahrens kmem_free(tx, sizeof (dmu_tx_t)); 1384fa9e4066Sahrens } 1385fa9e4066Sahrens 1386fa9e4066Sahrens void 1387fa9e4066Sahrens dmu_tx_abort(dmu_tx_t *tx) 1388fa9e4066Sahrens { 13898a2f1b91Sahrens dmu_tx_hold_t *txh; 1390fa9e4066Sahrens 1391fa9e4066Sahrens ASSERT(tx->tx_txg == 0); 1392fa9e4066Sahrens 13938a2f1b91Sahrens while (txh = list_head(&tx->tx_holds)) { 13948a2f1b91Sahrens dnode_t *dn = txh->txh_dnode; 1395fa9e4066Sahrens 13968a2f1b91Sahrens list_remove(&tx->tx_holds, txh); 13978a2f1b91Sahrens kmem_free(txh, sizeof (dmu_tx_hold_t)); 1398fa9e4066Sahrens if (dn != NULL) 1399fa9e4066Sahrens dnode_rele(dn, tx); 1400fa9e4066Sahrens } 1401d20e665cSRicardo M. Correia 1402d20e665cSRicardo M. Correia /* 1403d20e665cSRicardo M. Correia * Call any registered callbacks with an error code. 1404d20e665cSRicardo M. Correia */ 1405d20e665cSRicardo M. Correia if (!list_is_empty(&tx->tx_callbacks)) 1406d20e665cSRicardo M. Correia dmu_tx_do_callbacks(&tx->tx_callbacks, ECANCELED); 1407d20e665cSRicardo M. Correia 1408d20e665cSRicardo M. Correia list_destroy(&tx->tx_callbacks); 14098f38d419Sek list_destroy(&tx->tx_holds); 14108a2f1b91Sahrens #ifdef ZFS_DEBUG 1411fa9e4066Sahrens refcount_destroy_many(&tx->tx_space_written, 1412fa9e4066Sahrens refcount_count(&tx->tx_space_written)); 1413fa9e4066Sahrens refcount_destroy_many(&tx->tx_space_freed, 1414fa9e4066Sahrens refcount_count(&tx->tx_space_freed)); 1415fa9e4066Sahrens #endif 1416fa9e4066Sahrens kmem_free(tx, sizeof (dmu_tx_t)); 1417fa9e4066Sahrens } 1418fa9e4066Sahrens 1419fa9e4066Sahrens uint64_t 1420fa9e4066Sahrens dmu_tx_get_txg(dmu_tx_t *tx) 1421fa9e4066Sahrens { 1422fa9e4066Sahrens ASSERT(tx->tx_txg != 0); 1423fa9e4066Sahrens return (tx->tx_txg); 1424fa9e4066Sahrens } 1425d20e665cSRicardo M. Correia 14263b2aab18SMatthew Ahrens dsl_pool_t * 14273b2aab18SMatthew Ahrens dmu_tx_pool(dmu_tx_t *tx) 14283b2aab18SMatthew Ahrens { 14293b2aab18SMatthew Ahrens ASSERT(tx->tx_pool != NULL); 14303b2aab18SMatthew Ahrens return (tx->tx_pool); 14313b2aab18SMatthew Ahrens } 14323b2aab18SMatthew Ahrens 14333b2aab18SMatthew Ahrens 1434d20e665cSRicardo M. Correia void 1435d20e665cSRicardo M. Correia dmu_tx_callback_register(dmu_tx_t *tx, dmu_tx_callback_func_t *func, void *data) 1436d20e665cSRicardo M. Correia { 1437d20e665cSRicardo M. Correia dmu_tx_callback_t *dcb; 1438d20e665cSRicardo M. Correia 1439d20e665cSRicardo M. Correia dcb = kmem_alloc(sizeof (dmu_tx_callback_t), KM_SLEEP); 1440d20e665cSRicardo M. Correia 1441d20e665cSRicardo M. Correia dcb->dcb_func = func; 1442d20e665cSRicardo M. Correia dcb->dcb_data = data; 1443d20e665cSRicardo M. Correia 1444d20e665cSRicardo M. Correia list_insert_tail(&tx->tx_callbacks, dcb); 1445d20e665cSRicardo M. Correia } 1446d20e665cSRicardo M. Correia 1447d20e665cSRicardo M. Correia /* 1448d20e665cSRicardo M. Correia * Call all the commit callbacks on a list, with a given error code. 1449d20e665cSRicardo M. Correia */ 1450d20e665cSRicardo M. Correia void 1451d20e665cSRicardo M. Correia dmu_tx_do_callbacks(list_t *cb_list, int error) 1452d20e665cSRicardo M. Correia { 1453d20e665cSRicardo M. Correia dmu_tx_callback_t *dcb; 1454d20e665cSRicardo M. Correia 1455d20e665cSRicardo M. Correia while (dcb = list_head(cb_list)) { 1456d20e665cSRicardo M. Correia list_remove(cb_list, dcb); 1457d20e665cSRicardo M. Correia dcb->dcb_func(dcb->dcb_data, error); 1458d20e665cSRicardo M. Correia kmem_free(dcb, sizeof (dmu_tx_callback_t)); 1459d20e665cSRicardo M. Correia } 1460d20e665cSRicardo M. Correia } 14610a586ceaSMark Shellenbaum 14620a586ceaSMark Shellenbaum /* 14630a586ceaSMark Shellenbaum * Interface to hold a bunch of attributes. 14640a586ceaSMark Shellenbaum * used for creating new files. 14650a586ceaSMark Shellenbaum * attrsize is the total size of all attributes 14660a586ceaSMark Shellenbaum * to be added during object creation 14670a586ceaSMark Shellenbaum * 14680a586ceaSMark Shellenbaum * For updating/adding a single attribute dmu_tx_hold_sa() should be used. 14690a586ceaSMark Shellenbaum */ 14700a586ceaSMark Shellenbaum 14710a586ceaSMark Shellenbaum /* 14720a586ceaSMark Shellenbaum * hold necessary attribute name for attribute registration. 14730a586ceaSMark Shellenbaum * should be a very rare case where this is needed. If it does 14740a586ceaSMark Shellenbaum * happen it would only happen on the first write to the file system. 14750a586ceaSMark Shellenbaum */ 14760a586ceaSMark Shellenbaum static void 14770a586ceaSMark Shellenbaum dmu_tx_sa_registration_hold(sa_os_t *sa, dmu_tx_t *tx) 14780a586ceaSMark Shellenbaum { 14790a586ceaSMark Shellenbaum int i; 14800a586ceaSMark Shellenbaum 14810a586ceaSMark Shellenbaum if (!sa->sa_need_attr_registration) 14820a586ceaSMark Shellenbaum return; 14830a586ceaSMark Shellenbaum 14840a586ceaSMark Shellenbaum for (i = 0; i != sa->sa_num_attrs; i++) { 14850a586ceaSMark Shellenbaum if (!sa->sa_attr_table[i].sa_registered) { 14860a586ceaSMark Shellenbaum if (sa->sa_reg_attr_obj) 14870a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, sa->sa_reg_attr_obj, 14880a586ceaSMark Shellenbaum B_TRUE, sa->sa_attr_table[i].sa_name); 14890a586ceaSMark Shellenbaum else 14900a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, 14910a586ceaSMark Shellenbaum B_TRUE, sa->sa_attr_table[i].sa_name); 14920a586ceaSMark Shellenbaum } 14930a586ceaSMark Shellenbaum } 14940a586ceaSMark Shellenbaum } 14950a586ceaSMark Shellenbaum 14960a586ceaSMark Shellenbaum 14970a586ceaSMark Shellenbaum void 14980a586ceaSMark Shellenbaum dmu_tx_hold_spill(dmu_tx_t *tx, uint64_t object) 14990a586ceaSMark Shellenbaum { 15000a586ceaSMark Shellenbaum dnode_t *dn; 15010a586ceaSMark Shellenbaum dmu_tx_hold_t *txh; 15020a586ceaSMark Shellenbaum 15030a586ceaSMark Shellenbaum txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, object, 15040a586ceaSMark Shellenbaum THT_SPILL, 0, 0); 15050a586ceaSMark Shellenbaum 15060a586ceaSMark Shellenbaum dn = txh->txh_dnode; 15070a586ceaSMark Shellenbaum 15080a586ceaSMark Shellenbaum if (dn == NULL) 15090a586ceaSMark Shellenbaum return; 15100a586ceaSMark Shellenbaum 15110a586ceaSMark Shellenbaum /* If blkptr doesn't exist then add space to towrite */ 15129dccfd2aSAlbert Lee if (!(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) { 15130a586ceaSMark Shellenbaum txh->txh_space_towrite += SPA_MAXBLOCKSIZE; 15140a586ceaSMark Shellenbaum } else { 15159dccfd2aSAlbert Lee blkptr_t *bp; 15169dccfd2aSAlbert Lee 15179dccfd2aSAlbert Lee bp = &dn->dn_phys->dn_spill; 15180a586ceaSMark Shellenbaum if (dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset, 1519c7cd2421SGeorge Wilson bp, bp->blk_birth)) 15200a586ceaSMark Shellenbaum txh->txh_space_tooverwrite += SPA_MAXBLOCKSIZE; 15210a586ceaSMark Shellenbaum else 15220a586ceaSMark Shellenbaum txh->txh_space_towrite += SPA_MAXBLOCKSIZE; 15239dccfd2aSAlbert Lee if (!BP_IS_HOLE(bp)) 15240a586ceaSMark Shellenbaum txh->txh_space_tounref += SPA_MAXBLOCKSIZE; 15250a586ceaSMark Shellenbaum } 15260a586ceaSMark Shellenbaum } 15270a586ceaSMark Shellenbaum 15280a586ceaSMark Shellenbaum void 15290a586ceaSMark Shellenbaum dmu_tx_hold_sa_create(dmu_tx_t *tx, int attrsize) 15300a586ceaSMark Shellenbaum { 15310a586ceaSMark Shellenbaum sa_os_t *sa = tx->tx_objset->os_sa; 15320a586ceaSMark Shellenbaum 15330a586ceaSMark Shellenbaum dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 15340a586ceaSMark Shellenbaum 15350a586ceaSMark Shellenbaum if (tx->tx_objset->os_sa->sa_master_obj == 0) 15360a586ceaSMark Shellenbaum return; 15370a586ceaSMark Shellenbaum 15380a586ceaSMark Shellenbaum if (tx->tx_objset->os_sa->sa_layout_attr_obj) 15390a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, sa->sa_layout_attr_obj, B_TRUE, NULL); 15400a586ceaSMark Shellenbaum else { 15410a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_LAYOUTS); 15420a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_REGISTRY); 15430a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL); 15440a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL); 15450a586ceaSMark Shellenbaum } 15460a586ceaSMark Shellenbaum 15470a586ceaSMark Shellenbaum dmu_tx_sa_registration_hold(sa, tx); 15480a586ceaSMark Shellenbaum 15490a586ceaSMark Shellenbaum if (attrsize <= DN_MAX_BONUSLEN && !sa->sa_force_spill) 15500a586ceaSMark Shellenbaum return; 15510a586ceaSMark Shellenbaum 15520a586ceaSMark Shellenbaum (void) dmu_tx_hold_object_impl(tx, tx->tx_objset, DMU_NEW_OBJECT, 15530a586ceaSMark Shellenbaum THT_SPILL, 0, 0); 15540a586ceaSMark Shellenbaum } 15550a586ceaSMark Shellenbaum 15560a586ceaSMark Shellenbaum /* 15570a586ceaSMark Shellenbaum * Hold SA attribute 15580a586ceaSMark Shellenbaum * 15590a586ceaSMark Shellenbaum * dmu_tx_hold_sa(dmu_tx_t *tx, sa_handle_t *, attribute, add, size) 15600a586ceaSMark Shellenbaum * 15610a586ceaSMark Shellenbaum * variable_size is the total size of all variable sized attributes 15620a586ceaSMark Shellenbaum * passed to this function. It is not the total size of all 15630a586ceaSMark Shellenbaum * variable size attributes that *may* exist on this object. 15640a586ceaSMark Shellenbaum */ 15650a586ceaSMark Shellenbaum void 15660a586ceaSMark Shellenbaum dmu_tx_hold_sa(dmu_tx_t *tx, sa_handle_t *hdl, boolean_t may_grow) 15670a586ceaSMark Shellenbaum { 15680a586ceaSMark Shellenbaum uint64_t object; 15690a586ceaSMark Shellenbaum sa_os_t *sa = tx->tx_objset->os_sa; 15700a586ceaSMark Shellenbaum 15710a586ceaSMark Shellenbaum ASSERT(hdl != NULL); 15720a586ceaSMark Shellenbaum 15730a586ceaSMark Shellenbaum object = sa_handle_object(hdl); 15740a586ceaSMark Shellenbaum 15750a586ceaSMark Shellenbaum dmu_tx_hold_bonus(tx, object); 15760a586ceaSMark Shellenbaum 15770a586ceaSMark Shellenbaum if (tx->tx_objset->os_sa->sa_master_obj == 0) 15780a586ceaSMark Shellenbaum return; 15790a586ceaSMark Shellenbaum 15800a586ceaSMark Shellenbaum if (tx->tx_objset->os_sa->sa_reg_attr_obj == 0 || 15810a586ceaSMark Shellenbaum tx->tx_objset->os_sa->sa_layout_attr_obj == 0) { 15820a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_LAYOUTS); 15830a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_REGISTRY); 15840a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL); 15850a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL); 15860a586ceaSMark Shellenbaum } 15870a586ceaSMark Shellenbaum 15880a586ceaSMark Shellenbaum dmu_tx_sa_registration_hold(sa, tx); 15890a586ceaSMark Shellenbaum 15900a586ceaSMark Shellenbaum if (may_grow && tx->tx_objset->os_sa->sa_layout_attr_obj) 15910a586ceaSMark Shellenbaum dmu_tx_hold_zap(tx, sa->sa_layout_attr_obj, B_TRUE, NULL); 15920a586ceaSMark Shellenbaum 1593744947dcSTom Erickson if (sa->sa_force_spill || may_grow || hdl->sa_spill) { 15940a586ceaSMark Shellenbaum ASSERT(tx->tx_txg == 0); 15950a586ceaSMark Shellenbaum dmu_tx_hold_spill(tx, object); 1596744947dcSTom Erickson } else { 1597744947dcSTom Erickson dmu_buf_impl_t *db = (dmu_buf_impl_t *)hdl->sa_bonus; 1598744947dcSTom Erickson dnode_t *dn; 1599744947dcSTom Erickson 1600744947dcSTom Erickson DB_DNODE_ENTER(db); 1601744947dcSTom Erickson dn = DB_DNODE(db); 1602744947dcSTom Erickson if (dn->dn_have_spill) { 1603744947dcSTom Erickson ASSERT(tx->tx_txg == 0); 1604744947dcSTom Erickson dmu_tx_hold_spill(tx, object); 1605744947dcSTom Erickson } 1606744947dcSTom Erickson DB_DNODE_EXIT(db); 16070a586ceaSMark Shellenbaum } 16080a586ceaSMark Shellenbaum } 1609