1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 24 * Copyright (c) 2012, 2017 by Delphix. All rights reserved. 25 */ 26 27 #include <sys/dmu.h> 28 #include <sys/dmu_impl.h> 29 #include <sys/dbuf.h> 30 #include <sys/dmu_tx.h> 31 #include <sys/dmu_objset.h> 32 #include <sys/dsl_dataset.h> 33 #include <sys/dsl_dir.h> 34 #include <sys/dsl_pool.h> 35 #include <sys/zap_impl.h> 36 #include <sys/spa.h> 37 #include <sys/sa.h> 38 #include <sys/sa_impl.h> 39 #include <sys/zfs_context.h> 40 #include <sys/trace_zfs.h> 41 42 typedef void (*dmu_tx_hold_func_t)(dmu_tx_t *tx, struct dnode *dn, 43 uint64_t arg1, uint64_t arg2); 44 45 dmu_tx_stats_t dmu_tx_stats = { 46 { "dmu_tx_assigned", KSTAT_DATA_UINT64 }, 47 { "dmu_tx_delay", KSTAT_DATA_UINT64 }, 48 { "dmu_tx_error", KSTAT_DATA_UINT64 }, 49 { "dmu_tx_suspended", KSTAT_DATA_UINT64 }, 50 { "dmu_tx_group", KSTAT_DATA_UINT64 }, 51 { "dmu_tx_memory_reserve", KSTAT_DATA_UINT64 }, 52 { "dmu_tx_memory_reclaim", KSTAT_DATA_UINT64 }, 53 { "dmu_tx_dirty_throttle", KSTAT_DATA_UINT64 }, 54 { "dmu_tx_dirty_delay", KSTAT_DATA_UINT64 }, 55 { "dmu_tx_dirty_over_max", KSTAT_DATA_UINT64 }, 56 { "dmu_tx_wrlog_over_max", KSTAT_DATA_UINT64 }, 57 { "dmu_tx_dirty_frees_delay", KSTAT_DATA_UINT64 }, 58 { "dmu_tx_quota", KSTAT_DATA_UINT64 }, 59 }; 60 61 static kstat_t *dmu_tx_ksp; 62 63 dmu_tx_t * 64 dmu_tx_create_dd(dsl_dir_t *dd) 65 { 66 dmu_tx_t *tx = kmem_zalloc(sizeof (dmu_tx_t), KM_SLEEP); 67 tx->tx_dir = dd; 68 if (dd != NULL) 69 tx->tx_pool = dd->dd_pool; 70 list_create(&tx->tx_holds, sizeof (dmu_tx_hold_t), 71 offsetof(dmu_tx_hold_t, txh_node)); 72 list_create(&tx->tx_callbacks, sizeof (dmu_tx_callback_t), 73 offsetof(dmu_tx_callback_t, dcb_node)); 74 tx->tx_start = gethrtime(); 75 return (tx); 76 } 77 78 dmu_tx_t * 79 dmu_tx_create(objset_t *os) 80 { 81 dmu_tx_t *tx = dmu_tx_create_dd(os->os_dsl_dataset->ds_dir); 82 tx->tx_objset = os; 83 return (tx); 84 } 85 86 dmu_tx_t * 87 dmu_tx_create_assigned(struct dsl_pool *dp, uint64_t txg) 88 { 89 dmu_tx_t *tx = dmu_tx_create_dd(NULL); 90 91 TXG_VERIFY(dp->dp_spa, txg); 92 tx->tx_pool = dp; 93 tx->tx_txg = txg; 94 tx->tx_anyobj = TRUE; 95 96 return (tx); 97 } 98 99 int 100 dmu_tx_is_syncing(dmu_tx_t *tx) 101 { 102 return (tx->tx_anyobj); 103 } 104 105 int 106 dmu_tx_private_ok(dmu_tx_t *tx) 107 { 108 return (tx->tx_anyobj); 109 } 110 111 static dmu_tx_hold_t * 112 dmu_tx_hold_dnode_impl(dmu_tx_t *tx, dnode_t *dn, enum dmu_tx_hold_type type, 113 uint64_t arg1, uint64_t arg2) 114 { 115 dmu_tx_hold_t *txh; 116 117 if (dn != NULL) { 118 (void) zfs_refcount_add(&dn->dn_holds, tx); 119 if (tx->tx_txg != 0) { 120 mutex_enter(&dn->dn_mtx); 121 /* 122 * dn->dn_assigned_txg == tx->tx_txg doesn't pose a 123 * problem, but there's no way for it to happen (for 124 * now, at least). 125 */ 126 ASSERT(dn->dn_assigned_txg == 0); 127 dn->dn_assigned_txg = tx->tx_txg; 128 (void) zfs_refcount_add(&dn->dn_tx_holds, tx); 129 mutex_exit(&dn->dn_mtx); 130 } 131 } 132 133 txh = kmem_zalloc(sizeof (dmu_tx_hold_t), KM_SLEEP); 134 txh->txh_tx = tx; 135 txh->txh_dnode = dn; 136 zfs_refcount_create(&txh->txh_space_towrite); 137 zfs_refcount_create(&txh->txh_memory_tohold); 138 txh->txh_type = type; 139 txh->txh_arg1 = arg1; 140 txh->txh_arg2 = arg2; 141 list_insert_tail(&tx->tx_holds, txh); 142 143 return (txh); 144 } 145 146 static dmu_tx_hold_t * 147 dmu_tx_hold_object_impl(dmu_tx_t *tx, objset_t *os, uint64_t object, 148 enum dmu_tx_hold_type type, uint64_t arg1, uint64_t arg2) 149 { 150 dnode_t *dn = NULL; 151 dmu_tx_hold_t *txh; 152 int err; 153 154 if (object != DMU_NEW_OBJECT) { 155 err = dnode_hold(os, object, FTAG, &dn); 156 if (err != 0) { 157 tx->tx_err = err; 158 return (NULL); 159 } 160 } 161 txh = dmu_tx_hold_dnode_impl(tx, dn, type, arg1, arg2); 162 if (dn != NULL) 163 dnode_rele(dn, FTAG); 164 return (txh); 165 } 166 167 void 168 dmu_tx_add_new_object(dmu_tx_t *tx, dnode_t *dn) 169 { 170 /* 171 * If we're syncing, they can manipulate any object anyhow, and 172 * the hold on the dnode_t can cause problems. 173 */ 174 if (!dmu_tx_is_syncing(tx)) 175 (void) dmu_tx_hold_dnode_impl(tx, dn, THT_NEWOBJECT, 0, 0); 176 } 177 178 /* 179 * This function reads specified data from disk. The specified data will 180 * be needed to perform the transaction -- i.e, it will be read after 181 * we do dmu_tx_assign(). There are two reasons that we read the data now 182 * (before dmu_tx_assign()): 183 * 184 * 1. Reading it now has potentially better performance. The transaction 185 * has not yet been assigned, so the TXG is not held open, and also the 186 * caller typically has less locks held when calling dmu_tx_hold_*() than 187 * after the transaction has been assigned. This reduces the lock (and txg) 188 * hold times, thus reducing lock contention. 189 * 190 * 2. It is easier for callers (primarily the ZPL) to handle i/o errors 191 * that are detected before they start making changes to the DMU state 192 * (i.e. now). Once the transaction has been assigned, and some DMU 193 * state has been changed, it can be difficult to recover from an i/o 194 * error (e.g. to undo the changes already made in memory at the DMU 195 * layer). Typically code to do so does not exist in the caller -- it 196 * assumes that the data has already been cached and thus i/o errors are 197 * not possible. 198 * 199 * It has been observed that the i/o initiated here can be a performance 200 * problem, and it appears to be optional, because we don't look at the 201 * data which is read. However, removing this read would only serve to 202 * move the work elsewhere (after the dmu_tx_assign()), where it may 203 * have a greater impact on performance (in addition to the impact on 204 * fault tolerance noted above). 205 */ 206 static int 207 dmu_tx_check_ioerr(zio_t *zio, dnode_t *dn, int level, uint64_t blkid) 208 { 209 int err; 210 dmu_buf_impl_t *db; 211 212 rw_enter(&dn->dn_struct_rwlock, RW_READER); 213 db = dbuf_hold_level(dn, level, blkid, FTAG); 214 rw_exit(&dn->dn_struct_rwlock); 215 if (db == NULL) 216 return (SET_ERROR(EIO)); 217 err = dbuf_read(db, zio, DB_RF_CANFAIL | DB_RF_NOPREFETCH); 218 dbuf_rele(db, FTAG); 219 return (err); 220 } 221 222 /* ARGSUSED */ 223 static void 224 dmu_tx_count_write(dmu_tx_hold_t *txh, uint64_t off, uint64_t len) 225 { 226 dnode_t *dn = txh->txh_dnode; 227 int err = 0; 228 229 if (len == 0) 230 return; 231 232 (void) zfs_refcount_add_many(&txh->txh_space_towrite, len, FTAG); 233 234 if (dn == NULL) 235 return; 236 237 /* 238 * For i/o error checking, read the blocks that will be needed 239 * to perform the write: the first and last level-0 blocks (if 240 * they are not aligned, i.e. if they are partial-block writes), 241 * and all the level-1 blocks. 242 */ 243 if (dn->dn_maxblkid == 0) { 244 if (off < dn->dn_datablksz && 245 (off > 0 || len < dn->dn_datablksz)) { 246 err = dmu_tx_check_ioerr(NULL, dn, 0, 0); 247 if (err != 0) { 248 txh->txh_tx->tx_err = err; 249 } 250 } 251 } else { 252 zio_t *zio = zio_root(dn->dn_objset->os_spa, 253 NULL, NULL, ZIO_FLAG_CANFAIL); 254 255 /* first level-0 block */ 256 uint64_t start = off >> dn->dn_datablkshift; 257 if (P2PHASE(off, dn->dn_datablksz) || len < dn->dn_datablksz) { 258 err = dmu_tx_check_ioerr(zio, dn, 0, start); 259 if (err != 0) { 260 txh->txh_tx->tx_err = err; 261 } 262 } 263 264 /* last level-0 block */ 265 uint64_t end = (off + len - 1) >> dn->dn_datablkshift; 266 if (end != start && end <= dn->dn_maxblkid && 267 P2PHASE(off + len, dn->dn_datablksz)) { 268 err = dmu_tx_check_ioerr(zio, dn, 0, end); 269 if (err != 0) { 270 txh->txh_tx->tx_err = err; 271 } 272 } 273 274 /* level-1 blocks */ 275 if (dn->dn_nlevels > 1) { 276 int shft = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 277 for (uint64_t i = (start >> shft) + 1; 278 i < end >> shft; i++) { 279 err = dmu_tx_check_ioerr(zio, dn, 1, i); 280 if (err != 0) { 281 txh->txh_tx->tx_err = err; 282 } 283 } 284 } 285 286 err = zio_wait(zio); 287 if (err != 0) { 288 txh->txh_tx->tx_err = err; 289 } 290 } 291 } 292 293 static void 294 dmu_tx_count_dnode(dmu_tx_hold_t *txh) 295 { 296 (void) zfs_refcount_add_many(&txh->txh_space_towrite, 297 DNODE_MIN_SIZE, FTAG); 298 } 299 300 void 301 dmu_tx_hold_write(dmu_tx_t *tx, uint64_t object, uint64_t off, int len) 302 { 303 dmu_tx_hold_t *txh; 304 305 ASSERT0(tx->tx_txg); 306 ASSERT3U(len, <=, DMU_MAX_ACCESS); 307 ASSERT(len == 0 || UINT64_MAX - off >= len - 1); 308 309 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, 310 object, THT_WRITE, off, len); 311 if (txh != NULL) { 312 dmu_tx_count_write(txh, off, len); 313 dmu_tx_count_dnode(txh); 314 } 315 } 316 317 void 318 dmu_tx_hold_write_by_dnode(dmu_tx_t *tx, dnode_t *dn, uint64_t off, int len) 319 { 320 dmu_tx_hold_t *txh; 321 322 ASSERT0(tx->tx_txg); 323 ASSERT3U(len, <=, DMU_MAX_ACCESS); 324 ASSERT(len == 0 || UINT64_MAX - off >= len - 1); 325 326 txh = dmu_tx_hold_dnode_impl(tx, dn, THT_WRITE, off, len); 327 if (txh != NULL) { 328 dmu_tx_count_write(txh, off, len); 329 dmu_tx_count_dnode(txh); 330 } 331 } 332 333 /* 334 * This function marks the transaction as being a "net free". The end 335 * result is that refquotas will be disabled for this transaction, and 336 * this transaction will be able to use half of the pool space overhead 337 * (see dsl_pool_adjustedsize()). Therefore this function should only 338 * be called for transactions that we expect will not cause a net increase 339 * in the amount of space used (but it's OK if that is occasionally not true). 340 */ 341 void 342 dmu_tx_mark_netfree(dmu_tx_t *tx) 343 { 344 tx->tx_netfree = B_TRUE; 345 } 346 347 static void 348 dmu_tx_hold_free_impl(dmu_tx_hold_t *txh, uint64_t off, uint64_t len) 349 { 350 dmu_tx_t *tx = txh->txh_tx; 351 dnode_t *dn = txh->txh_dnode; 352 int err; 353 354 ASSERT(tx->tx_txg == 0); 355 356 dmu_tx_count_dnode(txh); 357 358 if (off >= (dn->dn_maxblkid + 1) * dn->dn_datablksz) 359 return; 360 if (len == DMU_OBJECT_END) 361 len = (dn->dn_maxblkid + 1) * dn->dn_datablksz - off; 362 363 dmu_tx_count_dnode(txh); 364 365 /* 366 * For i/o error checking, we read the first and last level-0 367 * blocks if they are not aligned, and all the level-1 blocks. 368 * 369 * Note: dbuf_free_range() assumes that we have not instantiated 370 * any level-0 dbufs that will be completely freed. Therefore we must 371 * exercise care to not read or count the first and last blocks 372 * if they are blocksize-aligned. 373 */ 374 if (dn->dn_datablkshift == 0) { 375 if (off != 0 || len < dn->dn_datablksz) 376 dmu_tx_count_write(txh, 0, dn->dn_datablksz); 377 } else { 378 /* first block will be modified if it is not aligned */ 379 if (!IS_P2ALIGNED(off, 1 << dn->dn_datablkshift)) 380 dmu_tx_count_write(txh, off, 1); 381 /* last block will be modified if it is not aligned */ 382 if (!IS_P2ALIGNED(off + len, 1 << dn->dn_datablkshift)) 383 dmu_tx_count_write(txh, off + len, 1); 384 } 385 386 /* 387 * Check level-1 blocks. 388 */ 389 if (dn->dn_nlevels > 1) { 390 int shift = dn->dn_datablkshift + dn->dn_indblkshift - 391 SPA_BLKPTRSHIFT; 392 uint64_t start = off >> shift; 393 uint64_t end = (off + len) >> shift; 394 395 ASSERT(dn->dn_indblkshift != 0); 396 397 /* 398 * dnode_reallocate() can result in an object with indirect 399 * blocks having an odd data block size. In this case, 400 * just check the single block. 401 */ 402 if (dn->dn_datablkshift == 0) 403 start = end = 0; 404 405 zio_t *zio = zio_root(tx->tx_pool->dp_spa, 406 NULL, NULL, ZIO_FLAG_CANFAIL); 407 for (uint64_t i = start; i <= end; i++) { 408 uint64_t ibyte = i << shift; 409 err = dnode_next_offset(dn, 0, &ibyte, 2, 1, 0); 410 i = ibyte >> shift; 411 if (err == ESRCH || i > end) 412 break; 413 if (err != 0) { 414 tx->tx_err = err; 415 (void) zio_wait(zio); 416 return; 417 } 418 419 (void) zfs_refcount_add_many(&txh->txh_memory_tohold, 420 1 << dn->dn_indblkshift, FTAG); 421 422 err = dmu_tx_check_ioerr(zio, dn, 1, i); 423 if (err != 0) { 424 tx->tx_err = err; 425 (void) zio_wait(zio); 426 return; 427 } 428 } 429 err = zio_wait(zio); 430 if (err != 0) { 431 tx->tx_err = err; 432 return; 433 } 434 } 435 } 436 437 void 438 dmu_tx_hold_free(dmu_tx_t *tx, uint64_t object, uint64_t off, uint64_t len) 439 { 440 dmu_tx_hold_t *txh; 441 442 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, 443 object, THT_FREE, off, len); 444 if (txh != NULL) 445 (void) dmu_tx_hold_free_impl(txh, off, len); 446 } 447 448 void 449 dmu_tx_hold_free_by_dnode(dmu_tx_t *tx, dnode_t *dn, uint64_t off, uint64_t len) 450 { 451 dmu_tx_hold_t *txh; 452 453 txh = dmu_tx_hold_dnode_impl(tx, dn, THT_FREE, off, len); 454 if (txh != NULL) 455 (void) dmu_tx_hold_free_impl(txh, off, len); 456 } 457 458 static void 459 dmu_tx_hold_zap_impl(dmu_tx_hold_t *txh, const char *name) 460 { 461 dmu_tx_t *tx = txh->txh_tx; 462 dnode_t *dn = txh->txh_dnode; 463 int err; 464 465 ASSERT(tx->tx_txg == 0); 466 467 dmu_tx_count_dnode(txh); 468 469 /* 470 * Modifying a almost-full microzap is around the worst case (128KB) 471 * 472 * If it is a fat zap, the worst case would be 7*16KB=112KB: 473 * - 3 blocks overwritten: target leaf, ptrtbl block, header block 474 * - 4 new blocks written if adding: 475 * - 2 blocks for possibly split leaves, 476 * - 2 grown ptrtbl blocks 477 */ 478 (void) zfs_refcount_add_many(&txh->txh_space_towrite, 479 MZAP_MAX_BLKSZ, FTAG); 480 481 if (dn == NULL) 482 return; 483 484 ASSERT3U(DMU_OT_BYTESWAP(dn->dn_type), ==, DMU_BSWAP_ZAP); 485 486 if (dn->dn_maxblkid == 0 || name == NULL) { 487 /* 488 * This is a microzap (only one block), or we don't know 489 * the name. Check the first block for i/o errors. 490 */ 491 err = dmu_tx_check_ioerr(NULL, dn, 0, 0); 492 if (err != 0) { 493 tx->tx_err = err; 494 } 495 } else { 496 /* 497 * Access the name so that we'll check for i/o errors to 498 * the leaf blocks, etc. We ignore ENOENT, as this name 499 * may not yet exist. 500 */ 501 err = zap_lookup_by_dnode(dn, name, 8, 0, NULL); 502 if (err == EIO || err == ECKSUM || err == ENXIO) { 503 tx->tx_err = err; 504 } 505 } 506 } 507 508 void 509 dmu_tx_hold_zap(dmu_tx_t *tx, uint64_t object, int add, const char *name) 510 { 511 dmu_tx_hold_t *txh; 512 513 ASSERT0(tx->tx_txg); 514 515 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, 516 object, THT_ZAP, add, (uintptr_t)name); 517 if (txh != NULL) 518 dmu_tx_hold_zap_impl(txh, name); 519 } 520 521 void 522 dmu_tx_hold_zap_by_dnode(dmu_tx_t *tx, dnode_t *dn, int add, const char *name) 523 { 524 dmu_tx_hold_t *txh; 525 526 ASSERT0(tx->tx_txg); 527 ASSERT(dn != NULL); 528 529 txh = dmu_tx_hold_dnode_impl(tx, dn, THT_ZAP, add, (uintptr_t)name); 530 if (txh != NULL) 531 dmu_tx_hold_zap_impl(txh, name); 532 } 533 534 void 535 dmu_tx_hold_bonus(dmu_tx_t *tx, uint64_t object) 536 { 537 dmu_tx_hold_t *txh; 538 539 ASSERT(tx->tx_txg == 0); 540 541 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, 542 object, THT_BONUS, 0, 0); 543 if (txh) 544 dmu_tx_count_dnode(txh); 545 } 546 547 void 548 dmu_tx_hold_bonus_by_dnode(dmu_tx_t *tx, dnode_t *dn) 549 { 550 dmu_tx_hold_t *txh; 551 552 ASSERT0(tx->tx_txg); 553 554 txh = dmu_tx_hold_dnode_impl(tx, dn, THT_BONUS, 0, 0); 555 if (txh) 556 dmu_tx_count_dnode(txh); 557 } 558 559 void 560 dmu_tx_hold_space(dmu_tx_t *tx, uint64_t space) 561 { 562 dmu_tx_hold_t *txh; 563 564 ASSERT(tx->tx_txg == 0); 565 566 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, 567 DMU_NEW_OBJECT, THT_SPACE, space, 0); 568 if (txh) { 569 (void) zfs_refcount_add_many( 570 &txh->txh_space_towrite, space, FTAG); 571 } 572 } 573 574 #ifdef ZFS_DEBUG 575 void 576 dmu_tx_dirty_buf(dmu_tx_t *tx, dmu_buf_impl_t *db) 577 { 578 boolean_t match_object = B_FALSE; 579 boolean_t match_offset = B_FALSE; 580 581 DB_DNODE_ENTER(db); 582 dnode_t *dn = DB_DNODE(db); 583 ASSERT(tx->tx_txg != 0); 584 ASSERT(tx->tx_objset == NULL || dn->dn_objset == tx->tx_objset); 585 ASSERT3U(dn->dn_object, ==, db->db.db_object); 586 587 if (tx->tx_anyobj) { 588 DB_DNODE_EXIT(db); 589 return; 590 } 591 592 /* XXX No checking on the meta dnode for now */ 593 if (db->db.db_object == DMU_META_DNODE_OBJECT) { 594 DB_DNODE_EXIT(db); 595 return; 596 } 597 598 for (dmu_tx_hold_t *txh = list_head(&tx->tx_holds); txh != NULL; 599 txh = list_next(&tx->tx_holds, txh)) { 600 ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg); 601 if (txh->txh_dnode == dn && txh->txh_type != THT_NEWOBJECT) 602 match_object = TRUE; 603 if (txh->txh_dnode == NULL || txh->txh_dnode == dn) { 604 int datablkshift = dn->dn_datablkshift ? 605 dn->dn_datablkshift : SPA_MAXBLOCKSHIFT; 606 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 607 int shift = datablkshift + epbs * db->db_level; 608 uint64_t beginblk = shift >= 64 ? 0 : 609 (txh->txh_arg1 >> shift); 610 uint64_t endblk = shift >= 64 ? 0 : 611 ((txh->txh_arg1 + txh->txh_arg2 - 1) >> shift); 612 uint64_t blkid = db->db_blkid; 613 614 /* XXX txh_arg2 better not be zero... */ 615 616 dprintf("found txh type %x beginblk=%llx endblk=%llx\n", 617 txh->txh_type, (u_longlong_t)beginblk, 618 (u_longlong_t)endblk); 619 620 switch (txh->txh_type) { 621 case THT_WRITE: 622 if (blkid >= beginblk && blkid <= endblk) 623 match_offset = TRUE; 624 /* 625 * We will let this hold work for the bonus 626 * or spill buffer so that we don't need to 627 * hold it when creating a new object. 628 */ 629 if (blkid == DMU_BONUS_BLKID || 630 blkid == DMU_SPILL_BLKID) 631 match_offset = TRUE; 632 /* 633 * They might have to increase nlevels, 634 * thus dirtying the new TLIBs. Or the 635 * might have to change the block size, 636 * thus dirying the new lvl=0 blk=0. 637 */ 638 if (blkid == 0) 639 match_offset = TRUE; 640 break; 641 case THT_FREE: 642 /* 643 * We will dirty all the level 1 blocks in 644 * the free range and perhaps the first and 645 * last level 0 block. 646 */ 647 if (blkid >= beginblk && (blkid <= endblk || 648 txh->txh_arg2 == DMU_OBJECT_END)) 649 match_offset = TRUE; 650 break; 651 case THT_SPILL: 652 if (blkid == DMU_SPILL_BLKID) 653 match_offset = TRUE; 654 break; 655 case THT_BONUS: 656 if (blkid == DMU_BONUS_BLKID) 657 match_offset = TRUE; 658 break; 659 case THT_ZAP: 660 match_offset = TRUE; 661 break; 662 case THT_NEWOBJECT: 663 match_object = TRUE; 664 break; 665 default: 666 cmn_err(CE_PANIC, "bad txh_type %d", 667 txh->txh_type); 668 } 669 } 670 if (match_object && match_offset) { 671 DB_DNODE_EXIT(db); 672 return; 673 } 674 } 675 DB_DNODE_EXIT(db); 676 panic("dirtying dbuf obj=%llx lvl=%u blkid=%llx but not tx_held\n", 677 (u_longlong_t)db->db.db_object, db->db_level, 678 (u_longlong_t)db->db_blkid); 679 } 680 #endif 681 682 /* 683 * If we can't do 10 iops, something is wrong. Let us go ahead 684 * and hit zfs_dirty_data_max. 685 */ 686 hrtime_t zfs_delay_max_ns = 100 * MICROSEC; /* 100 milliseconds */ 687 int zfs_delay_resolution_ns = 100 * 1000; /* 100 microseconds */ 688 689 /* 690 * We delay transactions when we've determined that the backend storage 691 * isn't able to accommodate the rate of incoming writes. 692 * 693 * If there is already a transaction waiting, we delay relative to when 694 * that transaction finishes waiting. This way the calculated min_time 695 * is independent of the number of threads concurrently executing 696 * transactions. 697 * 698 * If we are the only waiter, wait relative to when the transaction 699 * started, rather than the current time. This credits the transaction for 700 * "time already served", e.g. reading indirect blocks. 701 * 702 * The minimum time for a transaction to take is calculated as: 703 * min_time = scale * (dirty - min) / (max - dirty) 704 * min_time is then capped at zfs_delay_max_ns. 705 * 706 * The delay has two degrees of freedom that can be adjusted via tunables. 707 * The percentage of dirty data at which we start to delay is defined by 708 * zfs_delay_min_dirty_percent. This should typically be at or above 709 * zfs_vdev_async_write_active_max_dirty_percent so that we only start to 710 * delay after writing at full speed has failed to keep up with the incoming 711 * write rate. The scale of the curve is defined by zfs_delay_scale. Roughly 712 * speaking, this variable determines the amount of delay at the midpoint of 713 * the curve. 714 * 715 * delay 716 * 10ms +-------------------------------------------------------------*+ 717 * | *| 718 * 9ms + *+ 719 * | *| 720 * 8ms + *+ 721 * | * | 722 * 7ms + * + 723 * | * | 724 * 6ms + * + 725 * | * | 726 * 5ms + * + 727 * | * | 728 * 4ms + * + 729 * | * | 730 * 3ms + * + 731 * | * | 732 * 2ms + (midpoint) * + 733 * | | ** | 734 * 1ms + v *** + 735 * | zfs_delay_scale ----------> ******** | 736 * 0 +-------------------------------------*********----------------+ 737 * 0% <- zfs_dirty_data_max -> 100% 738 * 739 * Note that since the delay is added to the outstanding time remaining on the 740 * most recent transaction, the delay is effectively the inverse of IOPS. 741 * Here the midpoint of 500us translates to 2000 IOPS. The shape of the curve 742 * was chosen such that small changes in the amount of accumulated dirty data 743 * in the first 3/4 of the curve yield relatively small differences in the 744 * amount of delay. 745 * 746 * The effects can be easier to understand when the amount of delay is 747 * represented on a log scale: 748 * 749 * delay 750 * 100ms +-------------------------------------------------------------++ 751 * + + 752 * | | 753 * + *+ 754 * 10ms + *+ 755 * + ** + 756 * | (midpoint) ** | 757 * + | ** + 758 * 1ms + v **** + 759 * + zfs_delay_scale ----------> ***** + 760 * | **** | 761 * + **** + 762 * 100us + ** + 763 * + * + 764 * | * | 765 * + * + 766 * 10us + * + 767 * + + 768 * | | 769 * + + 770 * +--------------------------------------------------------------+ 771 * 0% <- zfs_dirty_data_max -> 100% 772 * 773 * Note here that only as the amount of dirty data approaches its limit does 774 * the delay start to increase rapidly. The goal of a properly tuned system 775 * should be to keep the amount of dirty data out of that range by first 776 * ensuring that the appropriate limits are set for the I/O scheduler to reach 777 * optimal throughput on the backend storage, and then by changing the value 778 * of zfs_delay_scale to increase the steepness of the curve. 779 */ 780 static void 781 dmu_tx_delay(dmu_tx_t *tx, uint64_t dirty) 782 { 783 dsl_pool_t *dp = tx->tx_pool; 784 uint64_t delay_min_bytes = 785 zfs_dirty_data_max * zfs_delay_min_dirty_percent / 100; 786 hrtime_t wakeup, min_tx_time, now; 787 788 if (dirty <= delay_min_bytes) 789 return; 790 791 /* 792 * The caller has already waited until we are under the max. 793 * We make them pass us the amount of dirty data so we don't 794 * have to handle the case of it being >= the max, which could 795 * cause a divide-by-zero if it's == the max. 796 */ 797 ASSERT3U(dirty, <, zfs_dirty_data_max); 798 799 now = gethrtime(); 800 min_tx_time = zfs_delay_scale * 801 (dirty - delay_min_bytes) / (zfs_dirty_data_max - dirty); 802 min_tx_time = MIN(min_tx_time, zfs_delay_max_ns); 803 if (now > tx->tx_start + min_tx_time) 804 return; 805 806 DTRACE_PROBE3(delay__mintime, dmu_tx_t *, tx, uint64_t, dirty, 807 uint64_t, min_tx_time); 808 809 mutex_enter(&dp->dp_lock); 810 wakeup = MAX(tx->tx_start + min_tx_time, 811 dp->dp_last_wakeup + min_tx_time); 812 dp->dp_last_wakeup = wakeup; 813 mutex_exit(&dp->dp_lock); 814 815 zfs_sleep_until(wakeup); 816 } 817 818 /* 819 * This routine attempts to assign the transaction to a transaction group. 820 * To do so, we must determine if there is sufficient free space on disk. 821 * 822 * If this is a "netfree" transaction (i.e. we called dmu_tx_mark_netfree() 823 * on it), then it is assumed that there is sufficient free space, 824 * unless there's insufficient slop space in the pool (see the comment 825 * above spa_slop_shift in spa_misc.c). 826 * 827 * If it is not a "netfree" transaction, then if the data already on disk 828 * is over the allowed usage (e.g. quota), this will fail with EDQUOT or 829 * ENOSPC. Otherwise, if the current rough estimate of pending changes, 830 * plus the rough estimate of this transaction's changes, may exceed the 831 * allowed usage, then this will fail with ERESTART, which will cause the 832 * caller to wait for the pending changes to be written to disk (by waiting 833 * for the next TXG to open), and then check the space usage again. 834 * 835 * The rough estimate of pending changes is comprised of the sum of: 836 * 837 * - this transaction's holds' txh_space_towrite 838 * 839 * - dd_tempreserved[], which is the sum of in-flight transactions' 840 * holds' txh_space_towrite (i.e. those transactions that have called 841 * dmu_tx_assign() but not yet called dmu_tx_commit()). 842 * 843 * - dd_space_towrite[], which is the amount of dirtied dbufs. 844 * 845 * Note that all of these values are inflated by spa_get_worst_case_asize(), 846 * which means that we may get ERESTART well before we are actually in danger 847 * of running out of space, but this also mitigates any small inaccuracies 848 * in the rough estimate (e.g. txh_space_towrite doesn't take into account 849 * indirect blocks, and dd_space_towrite[] doesn't take into account changes 850 * to the MOS). 851 * 852 * Note that due to this algorithm, it is possible to exceed the allowed 853 * usage by one transaction. Also, as we approach the allowed usage, 854 * we will allow a very limited amount of changes into each TXG, thus 855 * decreasing performance. 856 */ 857 static int 858 dmu_tx_try_assign(dmu_tx_t *tx, uint64_t txg_how) 859 { 860 spa_t *spa = tx->tx_pool->dp_spa; 861 862 ASSERT0(tx->tx_txg); 863 864 if (tx->tx_err) { 865 DMU_TX_STAT_BUMP(dmu_tx_error); 866 return (tx->tx_err); 867 } 868 869 if (spa_suspended(spa)) { 870 DMU_TX_STAT_BUMP(dmu_tx_suspended); 871 872 /* 873 * If the user has indicated a blocking failure mode 874 * then return ERESTART which will block in dmu_tx_wait(). 875 * Otherwise, return EIO so that an error can get 876 * propagated back to the VOP calls. 877 * 878 * Note that we always honor the txg_how flag regardless 879 * of the failuremode setting. 880 */ 881 if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE && 882 !(txg_how & TXG_WAIT)) 883 return (SET_ERROR(EIO)); 884 885 return (SET_ERROR(ERESTART)); 886 } 887 888 if (!tx->tx_dirty_delayed && 889 dsl_pool_wrlog_over_max(tx->tx_pool)) { 890 DMU_TX_STAT_BUMP(dmu_tx_wrlog_over_max); 891 return (SET_ERROR(ERESTART)); 892 } 893 894 if (!tx->tx_dirty_delayed && 895 dsl_pool_need_dirty_delay(tx->tx_pool)) { 896 tx->tx_wait_dirty = B_TRUE; 897 DMU_TX_STAT_BUMP(dmu_tx_dirty_delay); 898 return (SET_ERROR(ERESTART)); 899 } 900 901 tx->tx_txg = txg_hold_open(tx->tx_pool, &tx->tx_txgh); 902 tx->tx_needassign_txh = NULL; 903 904 /* 905 * NB: No error returns are allowed after txg_hold_open, but 906 * before processing the dnode holds, due to the 907 * dmu_tx_unassign() logic. 908 */ 909 910 uint64_t towrite = 0; 911 uint64_t tohold = 0; 912 for (dmu_tx_hold_t *txh = list_head(&tx->tx_holds); txh != NULL; 913 txh = list_next(&tx->tx_holds, txh)) { 914 dnode_t *dn = txh->txh_dnode; 915 if (dn != NULL) { 916 /* 917 * This thread can't hold the dn_struct_rwlock 918 * while assigning the tx, because this can lead to 919 * deadlock. Specifically, if this dnode is already 920 * assigned to an earlier txg, this thread may need 921 * to wait for that txg to sync (the ERESTART case 922 * below). The other thread that has assigned this 923 * dnode to an earlier txg prevents this txg from 924 * syncing until its tx can complete (calling 925 * dmu_tx_commit()), but it may need to acquire the 926 * dn_struct_rwlock to do so (e.g. via 927 * dmu_buf_hold*()). 928 * 929 * Note that this thread can't hold the lock for 930 * read either, but the rwlock doesn't record 931 * enough information to make that assertion. 932 */ 933 ASSERT(!RW_WRITE_HELD(&dn->dn_struct_rwlock)); 934 935 mutex_enter(&dn->dn_mtx); 936 if (dn->dn_assigned_txg == tx->tx_txg - 1) { 937 mutex_exit(&dn->dn_mtx); 938 tx->tx_needassign_txh = txh; 939 DMU_TX_STAT_BUMP(dmu_tx_group); 940 return (SET_ERROR(ERESTART)); 941 } 942 if (dn->dn_assigned_txg == 0) 943 dn->dn_assigned_txg = tx->tx_txg; 944 ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg); 945 (void) zfs_refcount_add(&dn->dn_tx_holds, tx); 946 mutex_exit(&dn->dn_mtx); 947 } 948 towrite += zfs_refcount_count(&txh->txh_space_towrite); 949 tohold += zfs_refcount_count(&txh->txh_memory_tohold); 950 } 951 952 /* needed allocation: worst-case estimate of write space */ 953 uint64_t asize = spa_get_worst_case_asize(tx->tx_pool->dp_spa, towrite); 954 /* calculate memory footprint estimate */ 955 uint64_t memory = towrite + tohold; 956 957 if (tx->tx_dir != NULL && asize != 0) { 958 int err = dsl_dir_tempreserve_space(tx->tx_dir, memory, 959 asize, tx->tx_netfree, &tx->tx_tempreserve_cookie, tx); 960 if (err != 0) 961 return (err); 962 } 963 964 DMU_TX_STAT_BUMP(dmu_tx_assigned); 965 966 return (0); 967 } 968 969 static void 970 dmu_tx_unassign(dmu_tx_t *tx) 971 { 972 if (tx->tx_txg == 0) 973 return; 974 975 txg_rele_to_quiesce(&tx->tx_txgh); 976 977 /* 978 * Walk the transaction's hold list, removing the hold on the 979 * associated dnode, and notifying waiters if the refcount drops to 0. 980 */ 981 for (dmu_tx_hold_t *txh = list_head(&tx->tx_holds); 982 txh && txh != tx->tx_needassign_txh; 983 txh = list_next(&tx->tx_holds, txh)) { 984 dnode_t *dn = txh->txh_dnode; 985 986 if (dn == NULL) 987 continue; 988 mutex_enter(&dn->dn_mtx); 989 ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg); 990 991 if (zfs_refcount_remove(&dn->dn_tx_holds, tx) == 0) { 992 dn->dn_assigned_txg = 0; 993 cv_broadcast(&dn->dn_notxholds); 994 } 995 mutex_exit(&dn->dn_mtx); 996 } 997 998 txg_rele_to_sync(&tx->tx_txgh); 999 1000 tx->tx_lasttried_txg = tx->tx_txg; 1001 tx->tx_txg = 0; 1002 } 1003 1004 /* 1005 * Assign tx to a transaction group; txg_how is a bitmask: 1006 * 1007 * If TXG_WAIT is set and the currently open txg is full, this function 1008 * will wait until there's a new txg. This should be used when no locks 1009 * are being held. With this bit set, this function will only fail if 1010 * we're truly out of space (or over quota). 1011 * 1012 * If TXG_WAIT is *not* set and we can't assign into the currently open 1013 * txg without blocking, this function will return immediately with 1014 * ERESTART. This should be used whenever locks are being held. On an 1015 * ERESTART error, the caller should drop all locks, call dmu_tx_wait(), 1016 * and try again. 1017 * 1018 * If TXG_NOTHROTTLE is set, this indicates that this tx should not be 1019 * delayed due on the ZFS Write Throttle (see comments in dsl_pool.c for 1020 * details on the throttle). This is used by the VFS operations, after 1021 * they have already called dmu_tx_wait() (though most likely on a 1022 * different tx). 1023 * 1024 * It is guaranteed that subsequent successful calls to dmu_tx_assign() 1025 * will assign the tx to monotonically increasing txgs. Of course this is 1026 * not strong monotonicity, because the same txg can be returned multiple 1027 * times in a row. This guarantee holds both for subsequent calls from 1028 * one thread and for multiple threads. For example, it is impossible to 1029 * observe the following sequence of events: 1030 * 1031 * Thread 1 Thread 2 1032 * 1033 * dmu_tx_assign(T1, ...) 1034 * 1 <- dmu_tx_get_txg(T1) 1035 * dmu_tx_assign(T2, ...) 1036 * 2 <- dmu_tx_get_txg(T2) 1037 * dmu_tx_assign(T3, ...) 1038 * 1 <- dmu_tx_get_txg(T3) 1039 */ 1040 int 1041 dmu_tx_assign(dmu_tx_t *tx, uint64_t txg_how) 1042 { 1043 int err; 1044 1045 ASSERT(tx->tx_txg == 0); 1046 ASSERT0(txg_how & ~(TXG_WAIT | TXG_NOTHROTTLE)); 1047 ASSERT(!dsl_pool_sync_context(tx->tx_pool)); 1048 1049 /* If we might wait, we must not hold the config lock. */ 1050 IMPLY((txg_how & TXG_WAIT), !dsl_pool_config_held(tx->tx_pool)); 1051 1052 if ((txg_how & TXG_NOTHROTTLE)) 1053 tx->tx_dirty_delayed = B_TRUE; 1054 1055 while ((err = dmu_tx_try_assign(tx, txg_how)) != 0) { 1056 dmu_tx_unassign(tx); 1057 1058 if (err != ERESTART || !(txg_how & TXG_WAIT)) 1059 return (err); 1060 1061 dmu_tx_wait(tx); 1062 } 1063 1064 txg_rele_to_quiesce(&tx->tx_txgh); 1065 1066 return (0); 1067 } 1068 1069 void 1070 dmu_tx_wait(dmu_tx_t *tx) 1071 { 1072 spa_t *spa = tx->tx_pool->dp_spa; 1073 dsl_pool_t *dp = tx->tx_pool; 1074 hrtime_t before; 1075 1076 ASSERT(tx->tx_txg == 0); 1077 ASSERT(!dsl_pool_config_held(tx->tx_pool)); 1078 1079 before = gethrtime(); 1080 1081 if (tx->tx_wait_dirty) { 1082 uint64_t dirty; 1083 1084 /* 1085 * dmu_tx_try_assign() has determined that we need to wait 1086 * because we've consumed much or all of the dirty buffer 1087 * space. 1088 */ 1089 mutex_enter(&dp->dp_lock); 1090 if (dp->dp_dirty_total >= zfs_dirty_data_max) 1091 DMU_TX_STAT_BUMP(dmu_tx_dirty_over_max); 1092 while (dp->dp_dirty_total >= zfs_dirty_data_max) 1093 cv_wait(&dp->dp_spaceavail_cv, &dp->dp_lock); 1094 dirty = dp->dp_dirty_total; 1095 mutex_exit(&dp->dp_lock); 1096 1097 dmu_tx_delay(tx, dirty); 1098 1099 tx->tx_wait_dirty = B_FALSE; 1100 1101 /* 1102 * Note: setting tx_dirty_delayed only has effect if the 1103 * caller used TX_WAIT. Otherwise they are going to 1104 * destroy this tx and try again. The common case, 1105 * zfs_write(), uses TX_WAIT. 1106 */ 1107 tx->tx_dirty_delayed = B_TRUE; 1108 } else if (spa_suspended(spa) || tx->tx_lasttried_txg == 0) { 1109 /* 1110 * If the pool is suspended we need to wait until it 1111 * is resumed. Note that it's possible that the pool 1112 * has become active after this thread has tried to 1113 * obtain a tx. If that's the case then tx_lasttried_txg 1114 * would not have been set. 1115 */ 1116 txg_wait_synced(dp, spa_last_synced_txg(spa) + 1); 1117 } else if (tx->tx_needassign_txh) { 1118 dnode_t *dn = tx->tx_needassign_txh->txh_dnode; 1119 1120 mutex_enter(&dn->dn_mtx); 1121 while (dn->dn_assigned_txg == tx->tx_lasttried_txg - 1) 1122 cv_wait(&dn->dn_notxholds, &dn->dn_mtx); 1123 mutex_exit(&dn->dn_mtx); 1124 tx->tx_needassign_txh = NULL; 1125 } else { 1126 /* 1127 * If we have a lot of dirty data just wait until we sync 1128 * out a TXG at which point we'll hopefully have synced 1129 * a portion of the changes. 1130 */ 1131 txg_wait_synced(dp, spa_last_synced_txg(spa) + 1); 1132 } 1133 1134 spa_tx_assign_add_nsecs(spa, gethrtime() - before); 1135 } 1136 1137 static void 1138 dmu_tx_destroy(dmu_tx_t *tx) 1139 { 1140 dmu_tx_hold_t *txh; 1141 1142 while ((txh = list_head(&tx->tx_holds)) != NULL) { 1143 dnode_t *dn = txh->txh_dnode; 1144 1145 list_remove(&tx->tx_holds, txh); 1146 zfs_refcount_destroy_many(&txh->txh_space_towrite, 1147 zfs_refcount_count(&txh->txh_space_towrite)); 1148 zfs_refcount_destroy_many(&txh->txh_memory_tohold, 1149 zfs_refcount_count(&txh->txh_memory_tohold)); 1150 kmem_free(txh, sizeof (dmu_tx_hold_t)); 1151 if (dn != NULL) 1152 dnode_rele(dn, tx); 1153 } 1154 1155 list_destroy(&tx->tx_callbacks); 1156 list_destroy(&tx->tx_holds); 1157 kmem_free(tx, sizeof (dmu_tx_t)); 1158 } 1159 1160 void 1161 dmu_tx_commit(dmu_tx_t *tx) 1162 { 1163 ASSERT(tx->tx_txg != 0); 1164 1165 /* 1166 * Go through the transaction's hold list and remove holds on 1167 * associated dnodes, notifying waiters if no holds remain. 1168 */ 1169 for (dmu_tx_hold_t *txh = list_head(&tx->tx_holds); txh != NULL; 1170 txh = list_next(&tx->tx_holds, txh)) { 1171 dnode_t *dn = txh->txh_dnode; 1172 1173 if (dn == NULL) 1174 continue; 1175 1176 mutex_enter(&dn->dn_mtx); 1177 ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg); 1178 1179 if (zfs_refcount_remove(&dn->dn_tx_holds, tx) == 0) { 1180 dn->dn_assigned_txg = 0; 1181 cv_broadcast(&dn->dn_notxholds); 1182 } 1183 mutex_exit(&dn->dn_mtx); 1184 } 1185 1186 if (tx->tx_tempreserve_cookie) 1187 dsl_dir_tempreserve_clear(tx->tx_tempreserve_cookie, tx); 1188 1189 if (!list_is_empty(&tx->tx_callbacks)) 1190 txg_register_callbacks(&tx->tx_txgh, &tx->tx_callbacks); 1191 1192 if (tx->tx_anyobj == FALSE) 1193 txg_rele_to_sync(&tx->tx_txgh); 1194 1195 dmu_tx_destroy(tx); 1196 } 1197 1198 void 1199 dmu_tx_abort(dmu_tx_t *tx) 1200 { 1201 ASSERT(tx->tx_txg == 0); 1202 1203 /* 1204 * Call any registered callbacks with an error code. 1205 */ 1206 if (!list_is_empty(&tx->tx_callbacks)) 1207 dmu_tx_do_callbacks(&tx->tx_callbacks, SET_ERROR(ECANCELED)); 1208 1209 dmu_tx_destroy(tx); 1210 } 1211 1212 uint64_t 1213 dmu_tx_get_txg(dmu_tx_t *tx) 1214 { 1215 ASSERT(tx->tx_txg != 0); 1216 return (tx->tx_txg); 1217 } 1218 1219 dsl_pool_t * 1220 dmu_tx_pool(dmu_tx_t *tx) 1221 { 1222 ASSERT(tx->tx_pool != NULL); 1223 return (tx->tx_pool); 1224 } 1225 1226 void 1227 dmu_tx_callback_register(dmu_tx_t *tx, dmu_tx_callback_func_t *func, void *data) 1228 { 1229 dmu_tx_callback_t *dcb; 1230 1231 dcb = kmem_alloc(sizeof (dmu_tx_callback_t), KM_SLEEP); 1232 1233 dcb->dcb_func = func; 1234 dcb->dcb_data = data; 1235 1236 list_insert_tail(&tx->tx_callbacks, dcb); 1237 } 1238 1239 /* 1240 * Call all the commit callbacks on a list, with a given error code. 1241 */ 1242 void 1243 dmu_tx_do_callbacks(list_t *cb_list, int error) 1244 { 1245 dmu_tx_callback_t *dcb; 1246 1247 while ((dcb = list_tail(cb_list)) != NULL) { 1248 list_remove(cb_list, dcb); 1249 dcb->dcb_func(dcb->dcb_data, error); 1250 kmem_free(dcb, sizeof (dmu_tx_callback_t)); 1251 } 1252 } 1253 1254 /* 1255 * Interface to hold a bunch of attributes. 1256 * used for creating new files. 1257 * attrsize is the total size of all attributes 1258 * to be added during object creation 1259 * 1260 * For updating/adding a single attribute dmu_tx_hold_sa() should be used. 1261 */ 1262 1263 /* 1264 * hold necessary attribute name for attribute registration. 1265 * should be a very rare case where this is needed. If it does 1266 * happen it would only happen on the first write to the file system. 1267 */ 1268 static void 1269 dmu_tx_sa_registration_hold(sa_os_t *sa, dmu_tx_t *tx) 1270 { 1271 if (!sa->sa_need_attr_registration) 1272 return; 1273 1274 for (int i = 0; i != sa->sa_num_attrs; i++) { 1275 if (!sa->sa_attr_table[i].sa_registered) { 1276 if (sa->sa_reg_attr_obj) 1277 dmu_tx_hold_zap(tx, sa->sa_reg_attr_obj, 1278 B_TRUE, sa->sa_attr_table[i].sa_name); 1279 else 1280 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, 1281 B_TRUE, sa->sa_attr_table[i].sa_name); 1282 } 1283 } 1284 } 1285 1286 void 1287 dmu_tx_hold_spill(dmu_tx_t *tx, uint64_t object) 1288 { 1289 dmu_tx_hold_t *txh; 1290 1291 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, object, 1292 THT_SPILL, 0, 0); 1293 if (txh != NULL) 1294 (void) zfs_refcount_add_many(&txh->txh_space_towrite, 1295 SPA_OLD_MAXBLOCKSIZE, FTAG); 1296 } 1297 1298 void 1299 dmu_tx_hold_sa_create(dmu_tx_t *tx, int attrsize) 1300 { 1301 sa_os_t *sa = tx->tx_objset->os_sa; 1302 1303 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 1304 1305 if (tx->tx_objset->os_sa->sa_master_obj == 0) 1306 return; 1307 1308 if (tx->tx_objset->os_sa->sa_layout_attr_obj) { 1309 dmu_tx_hold_zap(tx, sa->sa_layout_attr_obj, B_TRUE, NULL); 1310 } else { 1311 dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_LAYOUTS); 1312 dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_REGISTRY); 1313 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL); 1314 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL); 1315 } 1316 1317 dmu_tx_sa_registration_hold(sa, tx); 1318 1319 if (attrsize <= DN_OLD_MAX_BONUSLEN && !sa->sa_force_spill) 1320 return; 1321 1322 (void) dmu_tx_hold_object_impl(tx, tx->tx_objset, DMU_NEW_OBJECT, 1323 THT_SPILL, 0, 0); 1324 } 1325 1326 /* 1327 * Hold SA attribute 1328 * 1329 * dmu_tx_hold_sa(dmu_tx_t *tx, sa_handle_t *, attribute, add, size) 1330 * 1331 * variable_size is the total size of all variable sized attributes 1332 * passed to this function. It is not the total size of all 1333 * variable size attributes that *may* exist on this object. 1334 */ 1335 void 1336 dmu_tx_hold_sa(dmu_tx_t *tx, sa_handle_t *hdl, boolean_t may_grow) 1337 { 1338 uint64_t object; 1339 sa_os_t *sa = tx->tx_objset->os_sa; 1340 1341 ASSERT(hdl != NULL); 1342 1343 object = sa_handle_object(hdl); 1344 1345 dmu_buf_impl_t *db = (dmu_buf_impl_t *)hdl->sa_bonus; 1346 DB_DNODE_ENTER(db); 1347 dmu_tx_hold_bonus_by_dnode(tx, DB_DNODE(db)); 1348 DB_DNODE_EXIT(db); 1349 1350 if (tx->tx_objset->os_sa->sa_master_obj == 0) 1351 return; 1352 1353 if (tx->tx_objset->os_sa->sa_reg_attr_obj == 0 || 1354 tx->tx_objset->os_sa->sa_layout_attr_obj == 0) { 1355 dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_LAYOUTS); 1356 dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_REGISTRY); 1357 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL); 1358 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL); 1359 } 1360 1361 dmu_tx_sa_registration_hold(sa, tx); 1362 1363 if (may_grow && tx->tx_objset->os_sa->sa_layout_attr_obj) 1364 dmu_tx_hold_zap(tx, sa->sa_layout_attr_obj, B_TRUE, NULL); 1365 1366 if (sa->sa_force_spill || may_grow || hdl->sa_spill) { 1367 ASSERT(tx->tx_txg == 0); 1368 dmu_tx_hold_spill(tx, object); 1369 } else { 1370 dnode_t *dn; 1371 1372 DB_DNODE_ENTER(db); 1373 dn = DB_DNODE(db); 1374 if (dn->dn_have_spill) { 1375 ASSERT(tx->tx_txg == 0); 1376 dmu_tx_hold_spill(tx, object); 1377 } 1378 DB_DNODE_EXIT(db); 1379 } 1380 } 1381 1382 void 1383 dmu_tx_init(void) 1384 { 1385 dmu_tx_ksp = kstat_create("zfs", 0, "dmu_tx", "misc", 1386 KSTAT_TYPE_NAMED, sizeof (dmu_tx_stats) / sizeof (kstat_named_t), 1387 KSTAT_FLAG_VIRTUAL); 1388 1389 if (dmu_tx_ksp != NULL) { 1390 dmu_tx_ksp->ks_data = &dmu_tx_stats; 1391 kstat_install(dmu_tx_ksp); 1392 } 1393 } 1394 1395 void 1396 dmu_tx_fini(void) 1397 { 1398 if (dmu_tx_ksp != NULL) { 1399 kstat_delete(dmu_tx_ksp); 1400 dmu_tx_ksp = NULL; 1401 } 1402 } 1403 1404 #if defined(_KERNEL) 1405 EXPORT_SYMBOL(dmu_tx_create); 1406 EXPORT_SYMBOL(dmu_tx_hold_write); 1407 EXPORT_SYMBOL(dmu_tx_hold_write_by_dnode); 1408 EXPORT_SYMBOL(dmu_tx_hold_free); 1409 EXPORT_SYMBOL(dmu_tx_hold_free_by_dnode); 1410 EXPORT_SYMBOL(dmu_tx_hold_zap); 1411 EXPORT_SYMBOL(dmu_tx_hold_zap_by_dnode); 1412 EXPORT_SYMBOL(dmu_tx_hold_bonus); 1413 EXPORT_SYMBOL(dmu_tx_hold_bonus_by_dnode); 1414 EXPORT_SYMBOL(dmu_tx_abort); 1415 EXPORT_SYMBOL(dmu_tx_assign); 1416 EXPORT_SYMBOL(dmu_tx_wait); 1417 EXPORT_SYMBOL(dmu_tx_commit); 1418 EXPORT_SYMBOL(dmu_tx_mark_netfree); 1419 EXPORT_SYMBOL(dmu_tx_get_txg); 1420 EXPORT_SYMBOL(dmu_tx_callback_register); 1421 EXPORT_SYMBOL(dmu_tx_do_callbacks); 1422 EXPORT_SYMBOL(dmu_tx_hold_spill); 1423 EXPORT_SYMBOL(dmu_tx_hold_sa_create); 1424 EXPORT_SYMBOL(dmu_tx_hold_sa); 1425 #endif 1426