dmu_tx.c revision 253821
1168404Spjd/* 2168404Spjd * CDDL HEADER START 3168404Spjd * 4168404Spjd * The contents of this file are subject to the terms of the 5168404Spjd * Common Development and Distribution License (the "License"). 6168404Spjd * You may not use this file except in compliance with the License. 7168404Spjd * 8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9168404Spjd * or http://www.opensolaris.org/os/licensing. 10168404Spjd * See the License for the specific language governing permissions 11168404Spjd * and limitations under the License. 12168404Spjd * 13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each 14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15168404Spjd * If applicable, add the following below this CDDL HEADER, with the 16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying 17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner] 18168404Spjd * 19168404Spjd * CDDL HEADER END 20168404Spjd */ 21168404Spjd/* 22219089Spjd * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23226512Smm * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 24246631Smm * Copyright (c) 2013 by Delphix. All rights reserved. 25226512Smm */ 26168404Spjd 27168404Spjd#include <sys/dmu.h> 28168404Spjd#include <sys/dmu_impl.h> 29168404Spjd#include <sys/dbuf.h> 30168404Spjd#include <sys/dmu_tx.h> 31168404Spjd#include <sys/dmu_objset.h> 32168404Spjd#include <sys/dsl_dataset.h> /* for dsl_dataset_block_freeable() */ 33168404Spjd#include <sys/dsl_dir.h> /* for dsl_dir_tempreserve_*() */ 34168404Spjd#include <sys/dsl_pool.h> 35168404Spjd#include <sys/zap_impl.h> /* for fzap_default_block_shift */ 36168404Spjd#include <sys/spa.h> 37219089Spjd#include <sys/sa.h> 38219089Spjd#include <sys/sa_impl.h> 39168404Spjd#include <sys/zfs_context.h> 40219089Spjd#include <sys/varargs.h> 41168404Spjd 42168404Spjdtypedef void (*dmu_tx_hold_func_t)(dmu_tx_t *tx, struct dnode *dn, 43168404Spjd uint64_t arg1, uint64_t arg2); 44168404Spjd 45168404Spjd 46168404Spjddmu_tx_t * 47168404Spjddmu_tx_create_dd(dsl_dir_t *dd) 48168404Spjd{ 49168404Spjd dmu_tx_t *tx = kmem_zalloc(sizeof (dmu_tx_t), KM_SLEEP); 50168404Spjd tx->tx_dir = dd; 51248571Smm if (dd != NULL) 52168404Spjd tx->tx_pool = dd->dd_pool; 53168404Spjd list_create(&tx->tx_holds, sizeof (dmu_tx_hold_t), 54168404Spjd offsetof(dmu_tx_hold_t, txh_node)); 55219089Spjd list_create(&tx->tx_callbacks, sizeof (dmu_tx_callback_t), 56219089Spjd offsetof(dmu_tx_callback_t, dcb_node)); 57168404Spjd#ifdef ZFS_DEBUG 58168404Spjd refcount_create(&tx->tx_space_written); 59168404Spjd refcount_create(&tx->tx_space_freed); 60168404Spjd#endif 61168404Spjd return (tx); 62168404Spjd} 63168404Spjd 64168404Spjddmu_tx_t * 65168404Spjddmu_tx_create(objset_t *os) 66168404Spjd{ 67219089Spjd dmu_tx_t *tx = dmu_tx_create_dd(os->os_dsl_dataset->ds_dir); 68168404Spjd tx->tx_objset = os; 69219089Spjd tx->tx_lastsnap_txg = dsl_dataset_prev_snap_txg(os->os_dsl_dataset); 70168404Spjd return (tx); 71168404Spjd} 72168404Spjd 73168404Spjddmu_tx_t * 74168404Spjddmu_tx_create_assigned(struct dsl_pool *dp, uint64_t txg) 75168404Spjd{ 76168404Spjd dmu_tx_t *tx = dmu_tx_create_dd(NULL); 77168404Spjd 78168404Spjd ASSERT3U(txg, <=, dp->dp_tx.tx_open_txg); 79168404Spjd tx->tx_pool = dp; 80168404Spjd tx->tx_txg = txg; 81168404Spjd tx->tx_anyobj = TRUE; 82168404Spjd 83168404Spjd return (tx); 84168404Spjd} 85168404Spjd 86168404Spjdint 87168404Spjddmu_tx_is_syncing(dmu_tx_t *tx) 88168404Spjd{ 89168404Spjd return (tx->tx_anyobj); 90168404Spjd} 91168404Spjd 92168404Spjdint 93168404Spjddmu_tx_private_ok(dmu_tx_t *tx) 94168404Spjd{ 95168404Spjd return (tx->tx_anyobj); 96168404Spjd} 97168404Spjd 98168404Spjdstatic dmu_tx_hold_t * 99168404Spjddmu_tx_hold_object_impl(dmu_tx_t *tx, objset_t *os, uint64_t object, 100168404Spjd enum dmu_tx_hold_type type, uint64_t arg1, uint64_t arg2) 101168404Spjd{ 102168404Spjd dmu_tx_hold_t *txh; 103168404Spjd dnode_t *dn = NULL; 104168404Spjd int err; 105168404Spjd 106168404Spjd if (object != DMU_NEW_OBJECT) { 107219089Spjd err = dnode_hold(os, object, tx, &dn); 108168404Spjd if (err) { 109168404Spjd tx->tx_err = err; 110168404Spjd return (NULL); 111168404Spjd } 112168404Spjd 113168404Spjd if (err == 0 && tx->tx_txg != 0) { 114168404Spjd mutex_enter(&dn->dn_mtx); 115168404Spjd /* 116168404Spjd * dn->dn_assigned_txg == tx->tx_txg doesn't pose a 117168404Spjd * problem, but there's no way for it to happen (for 118168404Spjd * now, at least). 119168404Spjd */ 120168404Spjd ASSERT(dn->dn_assigned_txg == 0); 121168404Spjd dn->dn_assigned_txg = tx->tx_txg; 122168404Spjd (void) refcount_add(&dn->dn_tx_holds, tx); 123168404Spjd mutex_exit(&dn->dn_mtx); 124168404Spjd } 125168404Spjd } 126168404Spjd 127168404Spjd txh = kmem_zalloc(sizeof (dmu_tx_hold_t), KM_SLEEP); 128168404Spjd txh->txh_tx = tx; 129168404Spjd txh->txh_dnode = dn; 130168404Spjd#ifdef ZFS_DEBUG 131168404Spjd txh->txh_type = type; 132168404Spjd txh->txh_arg1 = arg1; 133168404Spjd txh->txh_arg2 = arg2; 134168404Spjd#endif 135168404Spjd list_insert_tail(&tx->tx_holds, txh); 136168404Spjd 137168404Spjd return (txh); 138168404Spjd} 139168404Spjd 140168404Spjdvoid 141168404Spjddmu_tx_add_new_object(dmu_tx_t *tx, objset_t *os, uint64_t object) 142168404Spjd{ 143168404Spjd /* 144168404Spjd * If we're syncing, they can manipulate any object anyhow, and 145168404Spjd * the hold on the dnode_t can cause problems. 146168404Spjd */ 147168404Spjd if (!dmu_tx_is_syncing(tx)) { 148168404Spjd (void) dmu_tx_hold_object_impl(tx, os, 149168404Spjd object, THT_NEWOBJECT, 0, 0); 150168404Spjd } 151168404Spjd} 152168404Spjd 153168404Spjdstatic int 154168404Spjddmu_tx_check_ioerr(zio_t *zio, dnode_t *dn, int level, uint64_t blkid) 155168404Spjd{ 156168404Spjd int err; 157168404Spjd dmu_buf_impl_t *db; 158168404Spjd 159168404Spjd rw_enter(&dn->dn_struct_rwlock, RW_READER); 160168404Spjd db = dbuf_hold_level(dn, level, blkid, FTAG); 161168404Spjd rw_exit(&dn->dn_struct_rwlock); 162168404Spjd if (db == NULL) 163249195Smm return (SET_ERROR(EIO)); 164185029Spjd err = dbuf_read(db, zio, DB_RF_CANFAIL | DB_RF_NOPREFETCH); 165168404Spjd dbuf_rele(db, FTAG); 166168404Spjd return (err); 167168404Spjd} 168168404Spjd 169209962Smmstatic void 170219089Spjddmu_tx_count_twig(dmu_tx_hold_t *txh, dnode_t *dn, dmu_buf_impl_t *db, 171219089Spjd int level, uint64_t blkid, boolean_t freeable, uint64_t *history) 172209962Smm{ 173219089Spjd objset_t *os = dn->dn_objset; 174219089Spjd dsl_dataset_t *ds = os->os_dsl_dataset; 175219089Spjd int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 176219089Spjd dmu_buf_impl_t *parent = NULL; 177219089Spjd blkptr_t *bp = NULL; 178219089Spjd uint64_t space; 179209962Smm 180219089Spjd if (level >= dn->dn_nlevels || history[level] == blkid) 181209962Smm return; 182209962Smm 183219089Spjd history[level] = blkid; 184209962Smm 185219089Spjd space = (level == 0) ? dn->dn_datablksz : (1ULL << dn->dn_indblkshift); 186219089Spjd 187219089Spjd if (db == NULL || db == dn->dn_dbuf) { 188219089Spjd ASSERT(level != 0); 189219089Spjd db = NULL; 190219089Spjd } else { 191219089Spjd ASSERT(DB_DNODE(db) == dn); 192219089Spjd ASSERT(db->db_level == level); 193219089Spjd ASSERT(db->db.db_size == space); 194219089Spjd ASSERT(db->db_blkid == blkid); 195219089Spjd bp = db->db_blkptr; 196219089Spjd parent = db->db_parent; 197209962Smm } 198209962Smm 199219089Spjd freeable = (bp && (freeable || 200219089Spjd dsl_dataset_block_freeable(ds, bp, bp->blk_birth))); 201209962Smm 202219089Spjd if (freeable) 203219089Spjd txh->txh_space_tooverwrite += space; 204219089Spjd else 205219089Spjd txh->txh_space_towrite += space; 206219089Spjd if (bp) 207219089Spjd txh->txh_space_tounref += bp_get_dsize(os->os_spa, bp); 208219089Spjd 209219089Spjd dmu_tx_count_twig(txh, dn, parent, level + 1, 210219089Spjd blkid >> epbs, freeable, history); 211209962Smm} 212209962Smm 213168404Spjd/* ARGSUSED */ 214168404Spjdstatic void 215168404Spjddmu_tx_count_write(dmu_tx_hold_t *txh, uint64_t off, uint64_t len) 216168404Spjd{ 217168404Spjd dnode_t *dn = txh->txh_dnode; 218168404Spjd uint64_t start, end, i; 219168404Spjd int min_bs, max_bs, min_ibs, max_ibs, epbs, bits; 220168404Spjd int err = 0; 221168404Spjd 222168404Spjd if (len == 0) 223168404Spjd return; 224168404Spjd 225168404Spjd min_bs = SPA_MINBLOCKSHIFT; 226168404Spjd max_bs = SPA_MAXBLOCKSHIFT; 227168404Spjd min_ibs = DN_MIN_INDBLKSHIFT; 228168404Spjd max_ibs = DN_MAX_INDBLKSHIFT; 229168404Spjd 230209962Smm if (dn) { 231219089Spjd uint64_t history[DN_MAX_LEVELS]; 232209962Smm int nlvls = dn->dn_nlevels; 233209962Smm int delta; 234168404Spjd 235209962Smm /* 236209962Smm * For i/o error checking, read the first and last level-0 237209962Smm * blocks (if they are not aligned), and all the level-1 blocks. 238209962Smm */ 239168404Spjd if (dn->dn_maxblkid == 0) { 240209962Smm delta = dn->dn_datablksz; 241209962Smm start = (off < dn->dn_datablksz) ? 0 : 1; 242209962Smm end = (off+len <= dn->dn_datablksz) ? 0 : 1; 243209962Smm if (start == 0 && (off > 0 || len < dn->dn_datablksz)) { 244209962Smm err = dmu_tx_check_ioerr(NULL, dn, 0, 0); 245209962Smm if (err) 246209962Smm goto out; 247209962Smm delta -= off; 248209962Smm } 249168404Spjd } else { 250168404Spjd zio_t *zio = zio_root(dn->dn_objset->os_spa, 251168404Spjd NULL, NULL, ZIO_FLAG_CANFAIL); 252168404Spjd 253168404Spjd /* first level-0 block */ 254168404Spjd start = off >> dn->dn_datablkshift; 255168404Spjd if (P2PHASE(off, dn->dn_datablksz) || 256168404Spjd len < dn->dn_datablksz) { 257168404Spjd err = dmu_tx_check_ioerr(zio, dn, 0, start); 258168404Spjd if (err) 259168404Spjd goto out; 260168404Spjd } 261168404Spjd 262168404Spjd /* last level-0 block */ 263168404Spjd end = (off+len-1) >> dn->dn_datablkshift; 264219089Spjd if (end != start && end <= dn->dn_maxblkid && 265168404Spjd P2PHASE(off+len, dn->dn_datablksz)) { 266168404Spjd err = dmu_tx_check_ioerr(zio, dn, 0, end); 267168404Spjd if (err) 268168404Spjd goto out; 269168404Spjd } 270168404Spjd 271168404Spjd /* level-1 blocks */ 272209962Smm if (nlvls > 1) { 273209962Smm int shft = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 274209962Smm for (i = (start>>shft)+1; i < end>>shft; i++) { 275168404Spjd err = dmu_tx_check_ioerr(zio, dn, 1, i); 276168404Spjd if (err) 277168404Spjd goto out; 278168404Spjd } 279168404Spjd } 280168404Spjd 281168404Spjd err = zio_wait(zio); 282168404Spjd if (err) 283168404Spjd goto out; 284209962Smm delta = P2NPHASE(off, dn->dn_datablksz); 285168404Spjd } 286168404Spjd 287246631Smm min_ibs = max_ibs = dn->dn_indblkshift; 288209962Smm if (dn->dn_maxblkid > 0) { 289209962Smm /* 290209962Smm * The blocksize can't change, 291209962Smm * so we can make a more precise estimate. 292209962Smm */ 293209962Smm ASSERT(dn->dn_datablkshift != 0); 294168404Spjd min_bs = max_bs = dn->dn_datablkshift; 295209962Smm } 296209962Smm 297209962Smm /* 298209962Smm * If this write is not off the end of the file 299209962Smm * we need to account for overwrites/unref. 300209962Smm */ 301219089Spjd if (start <= dn->dn_maxblkid) { 302219089Spjd for (int l = 0; l < DN_MAX_LEVELS; l++) 303219089Spjd history[l] = -1ULL; 304219089Spjd } 305209962Smm while (start <= dn->dn_maxblkid) { 306209962Smm dmu_buf_impl_t *db; 307209962Smm 308209962Smm rw_enter(&dn->dn_struct_rwlock, RW_READER); 309219089Spjd err = dbuf_hold_impl(dn, 0, start, FALSE, FTAG, &db); 310209962Smm rw_exit(&dn->dn_struct_rwlock); 311219089Spjd 312219089Spjd if (err) { 313219089Spjd txh->txh_tx->tx_err = err; 314219089Spjd return; 315209962Smm } 316219089Spjd 317219089Spjd dmu_tx_count_twig(txh, dn, db, 0, start, B_FALSE, 318219089Spjd history); 319209962Smm dbuf_rele(db, FTAG); 320209962Smm if (++start > end) { 321209962Smm /* 322209962Smm * Account for new indirects appearing 323209962Smm * before this IO gets assigned into a txg. 324209962Smm */ 325209962Smm bits = 64 - min_bs; 326209962Smm epbs = min_ibs - SPA_BLKPTRSHIFT; 327209962Smm for (bits -= epbs * (nlvls - 1); 328209962Smm bits >= 0; bits -= epbs) 329209962Smm txh->txh_fudge += 1ULL << max_ibs; 330209962Smm goto out; 331209962Smm } 332209962Smm off += delta; 333209962Smm if (len >= delta) 334209962Smm len -= delta; 335209962Smm delta = dn->dn_datablksz; 336209962Smm } 337168404Spjd } 338168404Spjd 339168404Spjd /* 340168404Spjd * 'end' is the last thing we will access, not one past. 341168404Spjd * This way we won't overflow when accessing the last byte. 342168404Spjd */ 343168404Spjd start = P2ALIGN(off, 1ULL << max_bs); 344168404Spjd end = P2ROUNDUP(off + len, 1ULL << max_bs) - 1; 345168404Spjd txh->txh_space_towrite += end - start + 1; 346168404Spjd 347168404Spjd start >>= min_bs; 348168404Spjd end >>= min_bs; 349168404Spjd 350168404Spjd epbs = min_ibs - SPA_BLKPTRSHIFT; 351168404Spjd 352168404Spjd /* 353168404Spjd * The object contains at most 2^(64 - min_bs) blocks, 354168404Spjd * and each indirect level maps 2^epbs. 355168404Spjd */ 356168404Spjd for (bits = 64 - min_bs; bits >= 0; bits -= epbs) { 357168404Spjd start >>= epbs; 358168404Spjd end >>= epbs; 359209962Smm ASSERT3U(end, >=, start); 360209962Smm txh->txh_space_towrite += (end - start + 1) << max_ibs; 361209962Smm if (start != 0) { 362209962Smm /* 363209962Smm * We also need a new blkid=0 indirect block 364209962Smm * to reference any existing file data. 365209962Smm */ 366168404Spjd txh->txh_space_towrite += 1ULL << max_ibs; 367209962Smm } 368168404Spjd } 369168404Spjd 370209962Smmout: 371209962Smm if (txh->txh_space_towrite + txh->txh_space_tooverwrite > 372209962Smm 2 * DMU_MAX_ACCESS) 373249195Smm err = SET_ERROR(EFBIG); 374168404Spjd 375168404Spjd if (err) 376168404Spjd txh->txh_tx->tx_err = err; 377168404Spjd} 378168404Spjd 379168404Spjdstatic void 380168404Spjddmu_tx_count_dnode(dmu_tx_hold_t *txh) 381168404Spjd{ 382168404Spjd dnode_t *dn = txh->txh_dnode; 383219089Spjd dnode_t *mdn = DMU_META_DNODE(txh->txh_tx->tx_objset); 384168404Spjd uint64_t space = mdn->dn_datablksz + 385168404Spjd ((mdn->dn_nlevels-1) << mdn->dn_indblkshift); 386168404Spjd 387168404Spjd if (dn && dn->dn_dbuf->db_blkptr && 388168404Spjd dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset, 389219089Spjd dn->dn_dbuf->db_blkptr, dn->dn_dbuf->db_blkptr->blk_birth)) { 390168404Spjd txh->txh_space_tooverwrite += space; 391209962Smm txh->txh_space_tounref += space; 392168404Spjd } else { 393168404Spjd txh->txh_space_towrite += space; 394185029Spjd if (dn && dn->dn_dbuf->db_blkptr) 395185029Spjd txh->txh_space_tounref += space; 396168404Spjd } 397168404Spjd} 398168404Spjd 399168404Spjdvoid 400168404Spjddmu_tx_hold_write(dmu_tx_t *tx, uint64_t object, uint64_t off, int len) 401168404Spjd{ 402168404Spjd dmu_tx_hold_t *txh; 403168404Spjd 404168404Spjd ASSERT(tx->tx_txg == 0); 405168404Spjd ASSERT(len < DMU_MAX_ACCESS); 406168404Spjd ASSERT(len == 0 || UINT64_MAX - off >= len - 1); 407168404Spjd 408168404Spjd txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, 409168404Spjd object, THT_WRITE, off, len); 410168404Spjd if (txh == NULL) 411168404Spjd return; 412168404Spjd 413168404Spjd dmu_tx_count_write(txh, off, len); 414168404Spjd dmu_tx_count_dnode(txh); 415168404Spjd} 416168404Spjd 417168404Spjdstatic void 418168404Spjddmu_tx_count_free(dmu_tx_hold_t *txh, uint64_t off, uint64_t len) 419168404Spjd{ 420185029Spjd uint64_t blkid, nblks, lastblk; 421185029Spjd uint64_t space = 0, unref = 0, skipped = 0; 422168404Spjd dnode_t *dn = txh->txh_dnode; 423168404Spjd dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset; 424168404Spjd spa_t *spa = txh->txh_tx->tx_pool->dp_spa; 425185029Spjd int epbs; 426240133Smm uint64_t l0span = 0, nl1blks = 0; 427168404Spjd 428185029Spjd if (dn->dn_nlevels == 0) 429168404Spjd return; 430168404Spjd 431168404Spjd /* 432185029Spjd * The struct_rwlock protects us against dn_nlevels 433168404Spjd * changing, in case (against all odds) we manage to dirty & 434168404Spjd * sync out the changes after we check for being dirty. 435219089Spjd * Also, dbuf_hold_impl() wants us to have the struct_rwlock. 436168404Spjd */ 437168404Spjd rw_enter(&dn->dn_struct_rwlock, RW_READER); 438185029Spjd epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 439185029Spjd if (dn->dn_maxblkid == 0) { 440168404Spjd if (off == 0 && len >= dn->dn_datablksz) { 441168404Spjd blkid = 0; 442168404Spjd nblks = 1; 443168404Spjd } else { 444168404Spjd rw_exit(&dn->dn_struct_rwlock); 445168404Spjd return; 446168404Spjd } 447168404Spjd } else { 448168404Spjd blkid = off >> dn->dn_datablkshift; 449185029Spjd nblks = (len + dn->dn_datablksz - 1) >> dn->dn_datablkshift; 450168404Spjd 451185029Spjd if (blkid >= dn->dn_maxblkid) { 452168404Spjd rw_exit(&dn->dn_struct_rwlock); 453168404Spjd return; 454168404Spjd } 455185029Spjd if (blkid + nblks > dn->dn_maxblkid) 456185029Spjd nblks = dn->dn_maxblkid - blkid; 457168404Spjd 458168404Spjd } 459240133Smm l0span = nblks; /* save for later use to calc level > 1 overhead */ 460185029Spjd if (dn->dn_nlevels == 1) { 461168404Spjd int i; 462168404Spjd for (i = 0; i < nblks; i++) { 463168404Spjd blkptr_t *bp = dn->dn_phys->dn_blkptr; 464185029Spjd ASSERT3U(blkid + i, <, dn->dn_nblkptr); 465168404Spjd bp += blkid + i; 466219089Spjd if (dsl_dataset_block_freeable(ds, bp, bp->blk_birth)) { 467168404Spjd dprintf_bp(bp, "can free old%s", ""); 468219089Spjd space += bp_get_dsize(spa, bp); 469168404Spjd } 470185029Spjd unref += BP_GET_ASIZE(bp); 471168404Spjd } 472240133Smm nl1blks = 1; 473168404Spjd nblks = 0; 474168404Spjd } 475168404Spjd 476185029Spjd lastblk = blkid + nblks - 1; 477168404Spjd while (nblks) { 478168404Spjd dmu_buf_impl_t *dbuf; 479185029Spjd uint64_t ibyte, new_blkid; 480185029Spjd int epb = 1 << epbs; 481185029Spjd int err, i, blkoff, tochk; 482185029Spjd blkptr_t *bp; 483168404Spjd 484185029Spjd ibyte = blkid << dn->dn_datablkshift; 485185029Spjd err = dnode_next_offset(dn, 486185029Spjd DNODE_FIND_HAVELOCK, &ibyte, 2, 1, 0); 487185029Spjd new_blkid = ibyte >> dn->dn_datablkshift; 488185029Spjd if (err == ESRCH) { 489185029Spjd skipped += (lastblk >> epbs) - (blkid >> epbs) + 1; 490185029Spjd break; 491185029Spjd } 492185029Spjd if (err) { 493185029Spjd txh->txh_tx->tx_err = err; 494185029Spjd break; 495185029Spjd } 496185029Spjd if (new_blkid > lastblk) { 497185029Spjd skipped += (lastblk >> epbs) - (blkid >> epbs) + 1; 498185029Spjd break; 499185029Spjd } 500168404Spjd 501185029Spjd if (new_blkid > blkid) { 502185029Spjd ASSERT((new_blkid >> epbs) > (blkid >> epbs)); 503185029Spjd skipped += (new_blkid >> epbs) - (blkid >> epbs) - 1; 504185029Spjd nblks -= new_blkid - blkid; 505185029Spjd blkid = new_blkid; 506185029Spjd } 507185029Spjd blkoff = P2PHASE(blkid, epb); 508185029Spjd tochk = MIN(epb - blkoff, nblks); 509168404Spjd 510219089Spjd err = dbuf_hold_impl(dn, 1, blkid >> epbs, FALSE, FTAG, &dbuf); 511219089Spjd if (err) { 512219089Spjd txh->txh_tx->tx_err = err; 513219089Spjd break; 514219089Spjd } 515168404Spjd 516185029Spjd txh->txh_memory_tohold += dbuf->db.db_size; 517219089Spjd 518219089Spjd /* 519219089Spjd * We don't check memory_tohold against DMU_MAX_ACCESS because 520219089Spjd * memory_tohold is an over-estimation (especially the >L1 521219089Spjd * indirect blocks), so it could fail. Callers should have 522219089Spjd * already verified that they will not be holding too much 523219089Spjd * memory. 524219089Spjd */ 525219089Spjd 526185029Spjd err = dbuf_read(dbuf, NULL, DB_RF_HAVESTRUCT | DB_RF_CANFAIL); 527185029Spjd if (err != 0) { 528168404Spjd txh->txh_tx->tx_err = err; 529185029Spjd dbuf_rele(dbuf, FTAG); 530168404Spjd break; 531168404Spjd } 532168404Spjd 533185029Spjd bp = dbuf->db.db_data; 534185029Spjd bp += blkoff; 535185029Spjd 536185029Spjd for (i = 0; i < tochk; i++) { 537219089Spjd if (dsl_dataset_block_freeable(ds, &bp[i], 538219089Spjd bp[i].blk_birth)) { 539185029Spjd dprintf_bp(&bp[i], "can free old%s", ""); 540219089Spjd space += bp_get_dsize(spa, &bp[i]); 541185029Spjd } 542185029Spjd unref += BP_GET_ASIZE(bp); 543185029Spjd } 544185029Spjd dbuf_rele(dbuf, FTAG); 545185029Spjd 546240133Smm ++nl1blks; 547168404Spjd blkid += tochk; 548168404Spjd nblks -= tochk; 549168404Spjd } 550168404Spjd rw_exit(&dn->dn_struct_rwlock); 551168404Spjd 552240133Smm /* 553240133Smm * Add in memory requirements of higher-level indirects. 554240133Smm * This assumes a worst-possible scenario for dn_nlevels and a 555240133Smm * worst-possible distribution of l1-blocks over the region to free. 556240133Smm */ 557240133Smm { 558240133Smm uint64_t blkcnt = 1 + ((l0span >> epbs) >> epbs); 559240133Smm int level = 2; 560240133Smm /* 561240133Smm * Here we don't use DN_MAX_LEVEL, but calculate it with the 562240133Smm * given datablkshift and indblkshift. This makes the 563240133Smm * difference between 19 and 8 on large files. 564240133Smm */ 565240133Smm int maxlevel = 2 + (DN_MAX_OFFSET_SHIFT - dn->dn_datablkshift) / 566240133Smm (dn->dn_indblkshift - SPA_BLKPTRSHIFT); 567240133Smm 568240133Smm while (level++ < maxlevel) { 569240955Smm txh->txh_memory_tohold += MAX(MIN(blkcnt, nl1blks), 1) 570240133Smm << dn->dn_indblkshift; 571240133Smm blkcnt = 1 + (blkcnt >> epbs); 572240133Smm } 573240133Smm } 574240133Smm 575185029Spjd /* account for new level 1 indirect blocks that might show up */ 576185029Spjd if (skipped > 0) { 577185029Spjd txh->txh_fudge += skipped << dn->dn_indblkshift; 578185029Spjd skipped = MIN(skipped, DMU_MAX_DELETEBLKCNT >> epbs); 579185029Spjd txh->txh_memory_tohold += skipped << dn->dn_indblkshift; 580185029Spjd } 581168404Spjd txh->txh_space_tofree += space; 582185029Spjd txh->txh_space_tounref += unref; 583168404Spjd} 584168404Spjd 585168404Spjdvoid 586168404Spjddmu_tx_hold_free(dmu_tx_t *tx, uint64_t object, uint64_t off, uint64_t len) 587168404Spjd{ 588168404Spjd dmu_tx_hold_t *txh; 589168404Spjd dnode_t *dn; 590253821Sdelphij int err; 591168404Spjd zio_t *zio; 592168404Spjd 593168404Spjd ASSERT(tx->tx_txg == 0); 594168404Spjd 595168404Spjd txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, 596168404Spjd object, THT_FREE, off, len); 597168404Spjd if (txh == NULL) 598168404Spjd return; 599168404Spjd dn = txh->txh_dnode; 600168404Spjd 601168404Spjd if (off >= (dn->dn_maxblkid+1) * dn->dn_datablksz) 602168404Spjd return; 603168404Spjd if (len == DMU_OBJECT_END) 604168404Spjd len = (dn->dn_maxblkid+1) * dn->dn_datablksz - off; 605168404Spjd 606253821Sdelphij dmu_tx_count_dnode(txh); 607253821Sdelphij 608168404Spjd /* 609253821Sdelphij * For i/o error checking, we read the first and last level-0 610253821Sdelphij * blocks if they are not aligned, and all the level-1 blocks. 611253821Sdelphij * 612253821Sdelphij * Note: dbuf_free_range() assumes that we have not instantiated 613253821Sdelphij * any level-0 dbufs that will be completely freed. Therefore we must 614253821Sdelphij * exercise care to not read or count the first and last blocks 615253821Sdelphij * if they are blocksize-aligned. 616168404Spjd */ 617253821Sdelphij if (dn->dn_datablkshift == 0) { 618253821Sdelphij dmu_tx_count_write(txh, off, len); 619253821Sdelphij } else { 620253821Sdelphij /* first block will be modified if it is not aligned */ 621253821Sdelphij if (!IS_P2ALIGNED(off, 1 << dn->dn_datablkshift)) 622253821Sdelphij dmu_tx_count_write(txh, off, 1); 623253821Sdelphij /* last block will be modified if it is not aligned */ 624253821Sdelphij if (!IS_P2ALIGNED(off + len, 1 << dn->dn_datablkshift)) 625253821Sdelphij dmu_tx_count_write(txh, off+len, 1); 626253821Sdelphij } 627253821Sdelphij 628253821Sdelphij /* 629253821Sdelphij * Check level-1 blocks. 630253821Sdelphij */ 631168404Spjd if (dn->dn_nlevels > 1) { 632253821Sdelphij int shift = dn->dn_datablkshift + dn->dn_indblkshift - 633168404Spjd SPA_BLKPTRSHIFT; 634253821Sdelphij uint64_t start = off >> shift; 635253821Sdelphij uint64_t end = (off + len) >> shift; 636168404Spjd 637253821Sdelphij ASSERT(dn->dn_datablkshift != 0); 638253821Sdelphij ASSERT(dn->dn_indblkshift != 0); 639253821Sdelphij 640168404Spjd zio = zio_root(tx->tx_pool->dp_spa, 641168404Spjd NULL, NULL, ZIO_FLAG_CANFAIL); 642253821Sdelphij for (uint64_t i = start; i <= end; i++) { 643168404Spjd uint64_t ibyte = i << shift; 644185029Spjd err = dnode_next_offset(dn, 0, &ibyte, 2, 1, 0); 645168404Spjd i = ibyte >> shift; 646168404Spjd if (err == ESRCH) 647168404Spjd break; 648168404Spjd if (err) { 649168404Spjd tx->tx_err = err; 650168404Spjd return; 651168404Spjd } 652168404Spjd 653168404Spjd err = dmu_tx_check_ioerr(zio, dn, 1, i); 654168404Spjd if (err) { 655168404Spjd tx->tx_err = err; 656168404Spjd return; 657168404Spjd } 658168404Spjd } 659168404Spjd err = zio_wait(zio); 660168404Spjd if (err) { 661168404Spjd tx->tx_err = err; 662168404Spjd return; 663168404Spjd } 664168404Spjd } 665168404Spjd 666168404Spjd dmu_tx_count_free(txh, off, len); 667168404Spjd} 668168404Spjd 669168404Spjdvoid 670209962Smmdmu_tx_hold_zap(dmu_tx_t *tx, uint64_t object, int add, const char *name) 671168404Spjd{ 672168404Spjd dmu_tx_hold_t *txh; 673168404Spjd dnode_t *dn; 674168404Spjd uint64_t nblocks; 675168404Spjd int epbs, err; 676168404Spjd 677168404Spjd ASSERT(tx->tx_txg == 0); 678168404Spjd 679168404Spjd txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, 680168404Spjd object, THT_ZAP, add, (uintptr_t)name); 681168404Spjd if (txh == NULL) 682168404Spjd return; 683168404Spjd dn = txh->txh_dnode; 684168404Spjd 685168404Spjd dmu_tx_count_dnode(txh); 686168404Spjd 687168404Spjd if (dn == NULL) { 688168404Spjd /* 689168404Spjd * We will be able to fit a new object's entries into one leaf 690168404Spjd * block. So there will be at most 2 blocks total, 691168404Spjd * including the header block. 692168404Spjd */ 693168404Spjd dmu_tx_count_write(txh, 0, 2 << fzap_default_block_shift); 694168404Spjd return; 695168404Spjd } 696168404Spjd 697236884Smm ASSERT3P(DMU_OT_BYTESWAP(dn->dn_type), ==, DMU_BSWAP_ZAP); 698168404Spjd 699168404Spjd if (dn->dn_maxblkid == 0 && !add) { 700226512Smm blkptr_t *bp; 701226512Smm 702168404Spjd /* 703168404Spjd * If there is only one block (i.e. this is a micro-zap) 704168404Spjd * and we are not adding anything, the accounting is simple. 705168404Spjd */ 706168404Spjd err = dmu_tx_check_ioerr(NULL, dn, 0, 0); 707168404Spjd if (err) { 708168404Spjd tx->tx_err = err; 709168404Spjd return; 710168404Spjd } 711168404Spjd 712168404Spjd /* 713168404Spjd * Use max block size here, since we don't know how much 714168404Spjd * the size will change between now and the dbuf dirty call. 715168404Spjd */ 716226512Smm bp = &dn->dn_phys->dn_blkptr[0]; 717168404Spjd if (dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset, 718226512Smm bp, bp->blk_birth)) 719168404Spjd txh->txh_space_tooverwrite += SPA_MAXBLOCKSIZE; 720226512Smm else 721168404Spjd txh->txh_space_towrite += SPA_MAXBLOCKSIZE; 722226512Smm if (!BP_IS_HOLE(bp)) 723209093Smm txh->txh_space_tounref += SPA_MAXBLOCKSIZE; 724168404Spjd return; 725168404Spjd } 726168404Spjd 727168404Spjd if (dn->dn_maxblkid > 0 && name) { 728168404Spjd /* 729168404Spjd * access the name in this fat-zap so that we'll check 730168404Spjd * for i/o errors to the leaf blocks, etc. 731168404Spjd */ 732219089Spjd err = zap_lookup(dn->dn_objset, dn->dn_object, name, 733168404Spjd 8, 0, NULL); 734168404Spjd if (err == EIO) { 735168404Spjd tx->tx_err = err; 736168404Spjd return; 737168404Spjd } 738168404Spjd } 739168404Spjd 740219089Spjd err = zap_count_write(dn->dn_objset, dn->dn_object, name, add, 741209962Smm &txh->txh_space_towrite, &txh->txh_space_tooverwrite); 742168404Spjd 743168404Spjd /* 744168404Spjd * If the modified blocks are scattered to the four winds, 745168404Spjd * we'll have to modify an indirect twig for each. 746168404Spjd */ 747168404Spjd epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 748168404Spjd for (nblocks = dn->dn_maxblkid >> epbs; nblocks != 0; nblocks >>= epbs) 749209962Smm if (dn->dn_objset->os_dsl_dataset->ds_phys->ds_prev_snap_obj) 750209962Smm txh->txh_space_towrite += 3 << dn->dn_indblkshift; 751209962Smm else 752209962Smm txh->txh_space_tooverwrite += 3 << dn->dn_indblkshift; 753168404Spjd} 754168404Spjd 755168404Spjdvoid 756168404Spjddmu_tx_hold_bonus(dmu_tx_t *tx, uint64_t object) 757168404Spjd{ 758168404Spjd dmu_tx_hold_t *txh; 759168404Spjd 760168404Spjd ASSERT(tx->tx_txg == 0); 761168404Spjd 762168404Spjd txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, 763168404Spjd object, THT_BONUS, 0, 0); 764168404Spjd if (txh) 765168404Spjd dmu_tx_count_dnode(txh); 766168404Spjd} 767168404Spjd 768168404Spjdvoid 769168404Spjddmu_tx_hold_space(dmu_tx_t *tx, uint64_t space) 770168404Spjd{ 771168404Spjd dmu_tx_hold_t *txh; 772168404Spjd ASSERT(tx->tx_txg == 0); 773168404Spjd 774168404Spjd txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, 775168404Spjd DMU_NEW_OBJECT, THT_SPACE, space, 0); 776168404Spjd 777168404Spjd txh->txh_space_towrite += space; 778168404Spjd} 779168404Spjd 780168404Spjdint 781168404Spjddmu_tx_holds(dmu_tx_t *tx, uint64_t object) 782168404Spjd{ 783168404Spjd dmu_tx_hold_t *txh; 784168404Spjd int holds = 0; 785168404Spjd 786168404Spjd /* 787168404Spjd * By asserting that the tx is assigned, we're counting the 788168404Spjd * number of dn_tx_holds, which is the same as the number of 789168404Spjd * dn_holds. Otherwise, we'd be counting dn_holds, but 790168404Spjd * dn_tx_holds could be 0. 791168404Spjd */ 792168404Spjd ASSERT(tx->tx_txg != 0); 793168404Spjd 794168404Spjd /* if (tx->tx_anyobj == TRUE) */ 795168404Spjd /* return (0); */ 796168404Spjd 797168404Spjd for (txh = list_head(&tx->tx_holds); txh; 798168404Spjd txh = list_next(&tx->tx_holds, txh)) { 799168404Spjd if (txh->txh_dnode && txh->txh_dnode->dn_object == object) 800168404Spjd holds++; 801168404Spjd } 802168404Spjd 803168404Spjd return (holds); 804168404Spjd} 805168404Spjd 806168404Spjd#ifdef ZFS_DEBUG 807168404Spjdvoid 808168404Spjddmu_tx_dirty_buf(dmu_tx_t *tx, dmu_buf_impl_t *db) 809168404Spjd{ 810168404Spjd dmu_tx_hold_t *txh; 811168404Spjd int match_object = FALSE, match_offset = FALSE; 812219089Spjd dnode_t *dn; 813168404Spjd 814219089Spjd DB_DNODE_ENTER(db); 815219089Spjd dn = DB_DNODE(db); 816168404Spjd ASSERT(tx->tx_txg != 0); 817219089Spjd ASSERT(tx->tx_objset == NULL || dn->dn_objset == tx->tx_objset); 818168404Spjd ASSERT3U(dn->dn_object, ==, db->db.db_object); 819168404Spjd 820219089Spjd if (tx->tx_anyobj) { 821219089Spjd DB_DNODE_EXIT(db); 822168404Spjd return; 823219089Spjd } 824168404Spjd 825168404Spjd /* XXX No checking on the meta dnode for now */ 826219089Spjd if (db->db.db_object == DMU_META_DNODE_OBJECT) { 827219089Spjd DB_DNODE_EXIT(db); 828168404Spjd return; 829219089Spjd } 830168404Spjd 831168404Spjd for (txh = list_head(&tx->tx_holds); txh; 832168404Spjd txh = list_next(&tx->tx_holds, txh)) { 833168404Spjd ASSERT(dn == NULL || dn->dn_assigned_txg == tx->tx_txg); 834168404Spjd if (txh->txh_dnode == dn && txh->txh_type != THT_NEWOBJECT) 835168404Spjd match_object = TRUE; 836168404Spjd if (txh->txh_dnode == NULL || txh->txh_dnode == dn) { 837168404Spjd int datablkshift = dn->dn_datablkshift ? 838168404Spjd dn->dn_datablkshift : SPA_MAXBLOCKSHIFT; 839168404Spjd int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 840168404Spjd int shift = datablkshift + epbs * db->db_level; 841168404Spjd uint64_t beginblk = shift >= 64 ? 0 : 842168404Spjd (txh->txh_arg1 >> shift); 843168404Spjd uint64_t endblk = shift >= 64 ? 0 : 844168404Spjd ((txh->txh_arg1 + txh->txh_arg2 - 1) >> shift); 845168404Spjd uint64_t blkid = db->db_blkid; 846168404Spjd 847168404Spjd /* XXX txh_arg2 better not be zero... */ 848168404Spjd 849168404Spjd dprintf("found txh type %x beginblk=%llx endblk=%llx\n", 850168404Spjd txh->txh_type, beginblk, endblk); 851168404Spjd 852168404Spjd switch (txh->txh_type) { 853168404Spjd case THT_WRITE: 854168404Spjd if (blkid >= beginblk && blkid <= endblk) 855168404Spjd match_offset = TRUE; 856168404Spjd /* 857168404Spjd * We will let this hold work for the bonus 858219089Spjd * or spill buffer so that we don't need to 859219089Spjd * hold it when creating a new object. 860168404Spjd */ 861219089Spjd if (blkid == DMU_BONUS_BLKID || 862219089Spjd blkid == DMU_SPILL_BLKID) 863168404Spjd match_offset = TRUE; 864168404Spjd /* 865168404Spjd * They might have to increase nlevels, 866168404Spjd * thus dirtying the new TLIBs. Or the 867168404Spjd * might have to change the block size, 868168404Spjd * thus dirying the new lvl=0 blk=0. 869168404Spjd */ 870168404Spjd if (blkid == 0) 871168404Spjd match_offset = TRUE; 872168404Spjd break; 873168404Spjd case THT_FREE: 874185029Spjd /* 875185029Spjd * We will dirty all the level 1 blocks in 876185029Spjd * the free range and perhaps the first and 877185029Spjd * last level 0 block. 878185029Spjd */ 879185029Spjd if (blkid >= beginblk && (blkid <= endblk || 880185029Spjd txh->txh_arg2 == DMU_OBJECT_END)) 881168404Spjd match_offset = TRUE; 882168404Spjd break; 883219089Spjd case THT_SPILL: 884219089Spjd if (blkid == DMU_SPILL_BLKID) 885219089Spjd match_offset = TRUE; 886219089Spjd break; 887168404Spjd case THT_BONUS: 888219089Spjd if (blkid == DMU_BONUS_BLKID) 889168404Spjd match_offset = TRUE; 890168404Spjd break; 891168404Spjd case THT_ZAP: 892168404Spjd match_offset = TRUE; 893168404Spjd break; 894168404Spjd case THT_NEWOBJECT: 895168404Spjd match_object = TRUE; 896168404Spjd break; 897168404Spjd default: 898168404Spjd ASSERT(!"bad txh_type"); 899168404Spjd } 900168404Spjd } 901219089Spjd if (match_object && match_offset) { 902219089Spjd DB_DNODE_EXIT(db); 903168404Spjd return; 904219089Spjd } 905168404Spjd } 906219089Spjd DB_DNODE_EXIT(db); 907168404Spjd panic("dirtying dbuf obj=%llx lvl=%u blkid=%llx but not tx_held\n", 908168404Spjd (u_longlong_t)db->db.db_object, db->db_level, 909168404Spjd (u_longlong_t)db->db_blkid); 910168404Spjd} 911168404Spjd#endif 912168404Spjd 913168404Spjdstatic int 914248571Smmdmu_tx_try_assign(dmu_tx_t *tx, txg_how_t txg_how) 915168404Spjd{ 916168404Spjd dmu_tx_hold_t *txh; 917185029Spjd spa_t *spa = tx->tx_pool->dp_spa; 918185029Spjd uint64_t memory, asize, fsize, usize; 919185029Spjd uint64_t towrite, tofree, tooverwrite, tounref, tohold, fudge; 920168404Spjd 921240415Smm ASSERT0(tx->tx_txg); 922185029Spjd 923168404Spjd if (tx->tx_err) 924168404Spjd return (tx->tx_err); 925168404Spjd 926185029Spjd if (spa_suspended(spa)) { 927185029Spjd /* 928185029Spjd * If the user has indicated a blocking failure mode 929185029Spjd * then return ERESTART which will block in dmu_tx_wait(). 930185029Spjd * Otherwise, return EIO so that an error can get 931185029Spjd * propagated back to the VOP calls. 932185029Spjd * 933185029Spjd * Note that we always honor the txg_how flag regardless 934185029Spjd * of the failuremode setting. 935185029Spjd */ 936185029Spjd if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE && 937185029Spjd txg_how != TXG_WAIT) 938249195Smm return (SET_ERROR(EIO)); 939185029Spjd 940249195Smm return (SET_ERROR(ERESTART)); 941185029Spjd } 942185029Spjd 943168404Spjd tx->tx_txg = txg_hold_open(tx->tx_pool, &tx->tx_txgh); 944168404Spjd tx->tx_needassign_txh = NULL; 945168404Spjd 946168404Spjd /* 947168404Spjd * NB: No error returns are allowed after txg_hold_open, but 948168404Spjd * before processing the dnode holds, due to the 949168404Spjd * dmu_tx_unassign() logic. 950168404Spjd */ 951168404Spjd 952185029Spjd towrite = tofree = tooverwrite = tounref = tohold = fudge = 0; 953168404Spjd for (txh = list_head(&tx->tx_holds); txh; 954168404Spjd txh = list_next(&tx->tx_holds, txh)) { 955168404Spjd dnode_t *dn = txh->txh_dnode; 956168404Spjd if (dn != NULL) { 957168404Spjd mutex_enter(&dn->dn_mtx); 958168404Spjd if (dn->dn_assigned_txg == tx->tx_txg - 1) { 959168404Spjd mutex_exit(&dn->dn_mtx); 960168404Spjd tx->tx_needassign_txh = txh; 961249195Smm return (SET_ERROR(ERESTART)); 962168404Spjd } 963168404Spjd if (dn->dn_assigned_txg == 0) 964168404Spjd dn->dn_assigned_txg = tx->tx_txg; 965168404Spjd ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg); 966168404Spjd (void) refcount_add(&dn->dn_tx_holds, tx); 967168404Spjd mutex_exit(&dn->dn_mtx); 968168404Spjd } 969168404Spjd towrite += txh->txh_space_towrite; 970168404Spjd tofree += txh->txh_space_tofree; 971168404Spjd tooverwrite += txh->txh_space_tooverwrite; 972185029Spjd tounref += txh->txh_space_tounref; 973185029Spjd tohold += txh->txh_memory_tohold; 974185029Spjd fudge += txh->txh_fudge; 975168404Spjd } 976168404Spjd 977168404Spjd /* 978168404Spjd * If a snapshot has been taken since we made our estimates, 979168404Spjd * assume that we won't be able to free or overwrite anything. 980168404Spjd */ 981168404Spjd if (tx->tx_objset && 982219089Spjd dsl_dataset_prev_snap_txg(tx->tx_objset->os_dsl_dataset) > 983168404Spjd tx->tx_lastsnap_txg) { 984168404Spjd towrite += tooverwrite; 985168404Spjd tooverwrite = tofree = 0; 986168404Spjd } 987168404Spjd 988185029Spjd /* needed allocation: worst-case estimate of write space */ 989185029Spjd asize = spa_get_asize(tx->tx_pool->dp_spa, towrite + tooverwrite); 990185029Spjd /* freed space estimate: worst-case overwrite + free estimate */ 991168404Spjd fsize = spa_get_asize(tx->tx_pool->dp_spa, tooverwrite) + tofree; 992185029Spjd /* convert unrefd space to worst-case estimate */ 993185029Spjd usize = spa_get_asize(tx->tx_pool->dp_spa, tounref); 994185029Spjd /* calculate memory footprint estimate */ 995185029Spjd memory = towrite + tooverwrite + tohold; 996168404Spjd 997168404Spjd#ifdef ZFS_DEBUG 998185029Spjd /* 999185029Spjd * Add in 'tohold' to account for our dirty holds on this memory 1000185029Spjd * XXX - the "fudge" factor is to account for skipped blocks that 1001185029Spjd * we missed because dnode_next_offset() misses in-core-only blocks. 1002185029Spjd */ 1003185029Spjd tx->tx_space_towrite = asize + 1004185029Spjd spa_get_asize(tx->tx_pool->dp_spa, tohold + fudge); 1005168404Spjd tx->tx_space_tofree = tofree; 1006168404Spjd tx->tx_space_tooverwrite = tooverwrite; 1007185029Spjd tx->tx_space_tounref = tounref; 1008168404Spjd#endif 1009168404Spjd 1010168404Spjd if (tx->tx_dir && asize != 0) { 1011185029Spjd int err = dsl_dir_tempreserve_space(tx->tx_dir, memory, 1012185029Spjd asize, fsize, usize, &tx->tx_tempreserve_cookie, tx); 1013168404Spjd if (err) 1014168404Spjd return (err); 1015168404Spjd } 1016168404Spjd 1017168404Spjd return (0); 1018168404Spjd} 1019168404Spjd 1020168404Spjdstatic void 1021168404Spjddmu_tx_unassign(dmu_tx_t *tx) 1022168404Spjd{ 1023168404Spjd dmu_tx_hold_t *txh; 1024168404Spjd 1025168404Spjd if (tx->tx_txg == 0) 1026168404Spjd return; 1027168404Spjd 1028168404Spjd txg_rele_to_quiesce(&tx->tx_txgh); 1029168404Spjd 1030251629Sdelphij /* 1031251629Sdelphij * Walk the transaction's hold list, removing the hold on the 1032251629Sdelphij * associated dnode, and notifying waiters if the refcount drops to 0. 1033251629Sdelphij */ 1034168404Spjd for (txh = list_head(&tx->tx_holds); txh != tx->tx_needassign_txh; 1035168404Spjd txh = list_next(&tx->tx_holds, txh)) { 1036168404Spjd dnode_t *dn = txh->txh_dnode; 1037168404Spjd 1038168404Spjd if (dn == NULL) 1039168404Spjd continue; 1040168404Spjd mutex_enter(&dn->dn_mtx); 1041168404Spjd ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg); 1042168404Spjd 1043168404Spjd if (refcount_remove(&dn->dn_tx_holds, tx) == 0) { 1044168404Spjd dn->dn_assigned_txg = 0; 1045168404Spjd cv_broadcast(&dn->dn_notxholds); 1046168404Spjd } 1047168404Spjd mutex_exit(&dn->dn_mtx); 1048168404Spjd } 1049168404Spjd 1050168404Spjd txg_rele_to_sync(&tx->tx_txgh); 1051168404Spjd 1052168404Spjd tx->tx_lasttried_txg = tx->tx_txg; 1053168404Spjd tx->tx_txg = 0; 1054168404Spjd} 1055168404Spjd 1056168404Spjd/* 1057168404Spjd * Assign tx to a transaction group. txg_how can be one of: 1058168404Spjd * 1059168404Spjd * (1) TXG_WAIT. If the current open txg is full, waits until there's 1060168404Spjd * a new one. This should be used when you're not holding locks. 1061248571Smm * It will only fail if we're truly out of space (or over quota). 1062168404Spjd * 1063168404Spjd * (2) TXG_NOWAIT. If we can't assign into the current open txg without 1064168404Spjd * blocking, returns immediately with ERESTART. This should be used 1065168404Spjd * whenever you're holding locks. On an ERESTART error, the caller 1066168404Spjd * should drop locks, do a dmu_tx_wait(tx), and try again. 1067168404Spjd */ 1068168404Spjdint 1069248571Smmdmu_tx_assign(dmu_tx_t *tx, txg_how_t txg_how) 1070168404Spjd{ 1071168404Spjd int err; 1072168404Spjd 1073168404Spjd ASSERT(tx->tx_txg == 0); 1074248571Smm ASSERT(txg_how == TXG_WAIT || txg_how == TXG_NOWAIT); 1075168404Spjd ASSERT(!dsl_pool_sync_context(tx->tx_pool)); 1076168404Spjd 1077248571Smm /* If we might wait, we must not hold the config lock. */ 1078248571Smm ASSERT(txg_how != TXG_WAIT || !dsl_pool_config_held(tx->tx_pool)); 1079248571Smm 1080168404Spjd while ((err = dmu_tx_try_assign(tx, txg_how)) != 0) { 1081168404Spjd dmu_tx_unassign(tx); 1082168404Spjd 1083168404Spjd if (err != ERESTART || txg_how != TXG_WAIT) 1084168404Spjd return (err); 1085168404Spjd 1086168404Spjd dmu_tx_wait(tx); 1087168404Spjd } 1088168404Spjd 1089168404Spjd txg_rele_to_quiesce(&tx->tx_txgh); 1090168404Spjd 1091168404Spjd return (0); 1092168404Spjd} 1093168404Spjd 1094168404Spjdvoid 1095168404Spjddmu_tx_wait(dmu_tx_t *tx) 1096168404Spjd{ 1097185029Spjd spa_t *spa = tx->tx_pool->dp_spa; 1098185029Spjd 1099168404Spjd ASSERT(tx->tx_txg == 0); 1100248571Smm ASSERT(!dsl_pool_config_held(tx->tx_pool)); 1101168404Spjd 1102185029Spjd /* 1103185029Spjd * It's possible that the pool has become active after this thread 1104185029Spjd * has tried to obtain a tx. If that's the case then his 1105185029Spjd * tx_lasttried_txg would not have been assigned. 1106185029Spjd */ 1107185029Spjd if (spa_suspended(spa) || tx->tx_lasttried_txg == 0) { 1108185029Spjd txg_wait_synced(tx->tx_pool, spa_last_synced_txg(spa) + 1); 1109185029Spjd } else if (tx->tx_needassign_txh) { 1110168404Spjd dnode_t *dn = tx->tx_needassign_txh->txh_dnode; 1111168404Spjd 1112168404Spjd mutex_enter(&dn->dn_mtx); 1113168404Spjd while (dn->dn_assigned_txg == tx->tx_lasttried_txg - 1) 1114168404Spjd cv_wait(&dn->dn_notxholds, &dn->dn_mtx); 1115168404Spjd mutex_exit(&dn->dn_mtx); 1116168404Spjd tx->tx_needassign_txh = NULL; 1117168404Spjd } else { 1118168404Spjd txg_wait_open(tx->tx_pool, tx->tx_lasttried_txg + 1); 1119168404Spjd } 1120168404Spjd} 1121168404Spjd 1122168404Spjdvoid 1123168404Spjddmu_tx_willuse_space(dmu_tx_t *tx, int64_t delta) 1124168404Spjd{ 1125168404Spjd#ifdef ZFS_DEBUG 1126168404Spjd if (tx->tx_dir == NULL || delta == 0) 1127168404Spjd return; 1128168404Spjd 1129168404Spjd if (delta > 0) { 1130168404Spjd ASSERT3U(refcount_count(&tx->tx_space_written) + delta, <=, 1131168404Spjd tx->tx_space_towrite); 1132168404Spjd (void) refcount_add_many(&tx->tx_space_written, delta, NULL); 1133168404Spjd } else { 1134168404Spjd (void) refcount_add_many(&tx->tx_space_freed, -delta, NULL); 1135168404Spjd } 1136168404Spjd#endif 1137168404Spjd} 1138168404Spjd 1139168404Spjdvoid 1140168404Spjddmu_tx_commit(dmu_tx_t *tx) 1141168404Spjd{ 1142168404Spjd dmu_tx_hold_t *txh; 1143168404Spjd 1144168404Spjd ASSERT(tx->tx_txg != 0); 1145168404Spjd 1146251629Sdelphij /* 1147251629Sdelphij * Go through the transaction's hold list and remove holds on 1148251629Sdelphij * associated dnodes, notifying waiters if no holds remain. 1149251629Sdelphij */ 1150168404Spjd while (txh = list_head(&tx->tx_holds)) { 1151168404Spjd dnode_t *dn = txh->txh_dnode; 1152168404Spjd 1153168404Spjd list_remove(&tx->tx_holds, txh); 1154168404Spjd kmem_free(txh, sizeof (dmu_tx_hold_t)); 1155168404Spjd if (dn == NULL) 1156168404Spjd continue; 1157168404Spjd mutex_enter(&dn->dn_mtx); 1158168404Spjd ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg); 1159168404Spjd 1160168404Spjd if (refcount_remove(&dn->dn_tx_holds, tx) == 0) { 1161168404Spjd dn->dn_assigned_txg = 0; 1162168404Spjd cv_broadcast(&dn->dn_notxholds); 1163168404Spjd } 1164168404Spjd mutex_exit(&dn->dn_mtx); 1165168404Spjd dnode_rele(dn, tx); 1166168404Spjd } 1167168404Spjd 1168168404Spjd if (tx->tx_tempreserve_cookie) 1169168404Spjd dsl_dir_tempreserve_clear(tx->tx_tempreserve_cookie, tx); 1170168404Spjd 1171219089Spjd if (!list_is_empty(&tx->tx_callbacks)) 1172219089Spjd txg_register_callbacks(&tx->tx_txgh, &tx->tx_callbacks); 1173219089Spjd 1174168404Spjd if (tx->tx_anyobj == FALSE) 1175168404Spjd txg_rele_to_sync(&tx->tx_txgh); 1176219089Spjd 1177219089Spjd list_destroy(&tx->tx_callbacks); 1178185029Spjd list_destroy(&tx->tx_holds); 1179168404Spjd#ifdef ZFS_DEBUG 1180168404Spjd dprintf("towrite=%llu written=%llu tofree=%llu freed=%llu\n", 1181168404Spjd tx->tx_space_towrite, refcount_count(&tx->tx_space_written), 1182168404Spjd tx->tx_space_tofree, refcount_count(&tx->tx_space_freed)); 1183168404Spjd refcount_destroy_many(&tx->tx_space_written, 1184168404Spjd refcount_count(&tx->tx_space_written)); 1185168404Spjd refcount_destroy_many(&tx->tx_space_freed, 1186168404Spjd refcount_count(&tx->tx_space_freed)); 1187168404Spjd#endif 1188168404Spjd kmem_free(tx, sizeof (dmu_tx_t)); 1189168404Spjd} 1190168404Spjd 1191168404Spjdvoid 1192168404Spjddmu_tx_abort(dmu_tx_t *tx) 1193168404Spjd{ 1194168404Spjd dmu_tx_hold_t *txh; 1195168404Spjd 1196168404Spjd ASSERT(tx->tx_txg == 0); 1197168404Spjd 1198168404Spjd while (txh = list_head(&tx->tx_holds)) { 1199168404Spjd dnode_t *dn = txh->txh_dnode; 1200168404Spjd 1201168404Spjd list_remove(&tx->tx_holds, txh); 1202168404Spjd kmem_free(txh, sizeof (dmu_tx_hold_t)); 1203168404Spjd if (dn != NULL) 1204168404Spjd dnode_rele(dn, tx); 1205168404Spjd } 1206219089Spjd 1207219089Spjd /* 1208219089Spjd * Call any registered callbacks with an error code. 1209219089Spjd */ 1210219089Spjd if (!list_is_empty(&tx->tx_callbacks)) 1211219089Spjd dmu_tx_do_callbacks(&tx->tx_callbacks, ECANCELED); 1212219089Spjd 1213219089Spjd list_destroy(&tx->tx_callbacks); 1214185029Spjd list_destroy(&tx->tx_holds); 1215168404Spjd#ifdef ZFS_DEBUG 1216168404Spjd refcount_destroy_many(&tx->tx_space_written, 1217168404Spjd refcount_count(&tx->tx_space_written)); 1218168404Spjd refcount_destroy_many(&tx->tx_space_freed, 1219168404Spjd refcount_count(&tx->tx_space_freed)); 1220168404Spjd#endif 1221168404Spjd kmem_free(tx, sizeof (dmu_tx_t)); 1222168404Spjd} 1223168404Spjd 1224168404Spjduint64_t 1225168404Spjddmu_tx_get_txg(dmu_tx_t *tx) 1226168404Spjd{ 1227168404Spjd ASSERT(tx->tx_txg != 0); 1228168404Spjd return (tx->tx_txg); 1229168404Spjd} 1230219089Spjd 1231248571Smmdsl_pool_t * 1232248571Smmdmu_tx_pool(dmu_tx_t *tx) 1233248571Smm{ 1234248571Smm ASSERT(tx->tx_pool != NULL); 1235248571Smm return (tx->tx_pool); 1236248571Smm} 1237248571Smm 1238248571Smm 1239219089Spjdvoid 1240219089Spjddmu_tx_callback_register(dmu_tx_t *tx, dmu_tx_callback_func_t *func, void *data) 1241219089Spjd{ 1242219089Spjd dmu_tx_callback_t *dcb; 1243219089Spjd 1244219089Spjd dcb = kmem_alloc(sizeof (dmu_tx_callback_t), KM_SLEEP); 1245219089Spjd 1246219089Spjd dcb->dcb_func = func; 1247219089Spjd dcb->dcb_data = data; 1248219089Spjd 1249219089Spjd list_insert_tail(&tx->tx_callbacks, dcb); 1250219089Spjd} 1251219089Spjd 1252219089Spjd/* 1253219089Spjd * Call all the commit callbacks on a list, with a given error code. 1254219089Spjd */ 1255219089Spjdvoid 1256219089Spjddmu_tx_do_callbacks(list_t *cb_list, int error) 1257219089Spjd{ 1258219089Spjd dmu_tx_callback_t *dcb; 1259219089Spjd 1260219089Spjd while (dcb = list_head(cb_list)) { 1261219089Spjd list_remove(cb_list, dcb); 1262219089Spjd dcb->dcb_func(dcb->dcb_data, error); 1263219089Spjd kmem_free(dcb, sizeof (dmu_tx_callback_t)); 1264219089Spjd } 1265219089Spjd} 1266219089Spjd 1267219089Spjd/* 1268219089Spjd * Interface to hold a bunch of attributes. 1269219089Spjd * used for creating new files. 1270219089Spjd * attrsize is the total size of all attributes 1271219089Spjd * to be added during object creation 1272219089Spjd * 1273219089Spjd * For updating/adding a single attribute dmu_tx_hold_sa() should be used. 1274219089Spjd */ 1275219089Spjd 1276219089Spjd/* 1277219089Spjd * hold necessary attribute name for attribute registration. 1278219089Spjd * should be a very rare case where this is needed. If it does 1279219089Spjd * happen it would only happen on the first write to the file system. 1280219089Spjd */ 1281219089Spjdstatic void 1282219089Spjddmu_tx_sa_registration_hold(sa_os_t *sa, dmu_tx_t *tx) 1283219089Spjd{ 1284219089Spjd int i; 1285219089Spjd 1286219089Spjd if (!sa->sa_need_attr_registration) 1287219089Spjd return; 1288219089Spjd 1289219089Spjd for (i = 0; i != sa->sa_num_attrs; i++) { 1290219089Spjd if (!sa->sa_attr_table[i].sa_registered) { 1291219089Spjd if (sa->sa_reg_attr_obj) 1292219089Spjd dmu_tx_hold_zap(tx, sa->sa_reg_attr_obj, 1293219089Spjd B_TRUE, sa->sa_attr_table[i].sa_name); 1294219089Spjd else 1295219089Spjd dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, 1296219089Spjd B_TRUE, sa->sa_attr_table[i].sa_name); 1297219089Spjd } 1298219089Spjd } 1299219089Spjd} 1300219089Spjd 1301219089Spjd 1302219089Spjdvoid 1303219089Spjddmu_tx_hold_spill(dmu_tx_t *tx, uint64_t object) 1304219089Spjd{ 1305219089Spjd dnode_t *dn; 1306219089Spjd dmu_tx_hold_t *txh; 1307219089Spjd 1308219089Spjd txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, object, 1309219089Spjd THT_SPILL, 0, 0); 1310219089Spjd 1311219089Spjd dn = txh->txh_dnode; 1312219089Spjd 1313219089Spjd if (dn == NULL) 1314219089Spjd return; 1315219089Spjd 1316219089Spjd /* If blkptr doesn't exist then add space to towrite */ 1317219089Spjd if (!(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) { 1318219089Spjd txh->txh_space_towrite += SPA_MAXBLOCKSIZE; 1319219089Spjd } else { 1320226512Smm blkptr_t *bp; 1321226512Smm 1322219089Spjd bp = &dn->dn_phys->dn_spill; 1323219089Spjd if (dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset, 1324219089Spjd bp, bp->blk_birth)) 1325219089Spjd txh->txh_space_tooverwrite += SPA_MAXBLOCKSIZE; 1326219089Spjd else 1327219089Spjd txh->txh_space_towrite += SPA_MAXBLOCKSIZE; 1328226512Smm if (!BP_IS_HOLE(bp)) 1329219089Spjd txh->txh_space_tounref += SPA_MAXBLOCKSIZE; 1330219089Spjd } 1331219089Spjd} 1332219089Spjd 1333219089Spjdvoid 1334219089Spjddmu_tx_hold_sa_create(dmu_tx_t *tx, int attrsize) 1335219089Spjd{ 1336219089Spjd sa_os_t *sa = tx->tx_objset->os_sa; 1337219089Spjd 1338219089Spjd dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 1339219089Spjd 1340219089Spjd if (tx->tx_objset->os_sa->sa_master_obj == 0) 1341219089Spjd return; 1342219089Spjd 1343219089Spjd if (tx->tx_objset->os_sa->sa_layout_attr_obj) 1344219089Spjd dmu_tx_hold_zap(tx, sa->sa_layout_attr_obj, B_TRUE, NULL); 1345219089Spjd else { 1346219089Spjd dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_LAYOUTS); 1347219089Spjd dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_REGISTRY); 1348219089Spjd dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL); 1349219089Spjd dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL); 1350219089Spjd } 1351219089Spjd 1352219089Spjd dmu_tx_sa_registration_hold(sa, tx); 1353219089Spjd 1354219089Spjd if (attrsize <= DN_MAX_BONUSLEN && !sa->sa_force_spill) 1355219089Spjd return; 1356219089Spjd 1357219089Spjd (void) dmu_tx_hold_object_impl(tx, tx->tx_objset, DMU_NEW_OBJECT, 1358219089Spjd THT_SPILL, 0, 0); 1359219089Spjd} 1360219089Spjd 1361219089Spjd/* 1362219089Spjd * Hold SA attribute 1363219089Spjd * 1364219089Spjd * dmu_tx_hold_sa(dmu_tx_t *tx, sa_handle_t *, attribute, add, size) 1365219089Spjd * 1366219089Spjd * variable_size is the total size of all variable sized attributes 1367219089Spjd * passed to this function. It is not the total size of all 1368219089Spjd * variable size attributes that *may* exist on this object. 1369219089Spjd */ 1370219089Spjdvoid 1371219089Spjddmu_tx_hold_sa(dmu_tx_t *tx, sa_handle_t *hdl, boolean_t may_grow) 1372219089Spjd{ 1373219089Spjd uint64_t object; 1374219089Spjd sa_os_t *sa = tx->tx_objset->os_sa; 1375219089Spjd 1376219089Spjd ASSERT(hdl != NULL); 1377219089Spjd 1378219089Spjd object = sa_handle_object(hdl); 1379219089Spjd 1380219089Spjd dmu_tx_hold_bonus(tx, object); 1381219089Spjd 1382219089Spjd if (tx->tx_objset->os_sa->sa_master_obj == 0) 1383219089Spjd return; 1384219089Spjd 1385219089Spjd if (tx->tx_objset->os_sa->sa_reg_attr_obj == 0 || 1386219089Spjd tx->tx_objset->os_sa->sa_layout_attr_obj == 0) { 1387219089Spjd dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_LAYOUTS); 1388219089Spjd dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_REGISTRY); 1389219089Spjd dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL); 1390219089Spjd dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL); 1391219089Spjd } 1392219089Spjd 1393219089Spjd dmu_tx_sa_registration_hold(sa, tx); 1394219089Spjd 1395219089Spjd if (may_grow && tx->tx_objset->os_sa->sa_layout_attr_obj) 1396219089Spjd dmu_tx_hold_zap(tx, sa->sa_layout_attr_obj, B_TRUE, NULL); 1397219089Spjd 1398219089Spjd if (sa->sa_force_spill || may_grow || hdl->sa_spill) { 1399219089Spjd ASSERT(tx->tx_txg == 0); 1400219089Spjd dmu_tx_hold_spill(tx, object); 1401219089Spjd } else { 1402219089Spjd dmu_buf_impl_t *db = (dmu_buf_impl_t *)hdl->sa_bonus; 1403219089Spjd dnode_t *dn; 1404219089Spjd 1405219089Spjd DB_DNODE_ENTER(db); 1406219089Spjd dn = DB_DNODE(db); 1407219089Spjd if (dn->dn_have_spill) { 1408219089Spjd ASSERT(tx->tx_txg == 0); 1409219089Spjd dmu_tx_hold_spill(tx, object); 1410219089Spjd } 1411219089Spjd DB_DNODE_EXIT(db); 1412219089Spjd } 1413219089Spjd} 1414