zil.c revision 208050
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 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26#include <sys/zfs_context.h> 27#include <sys/spa.h> 28#include <sys/dmu.h> 29#include <sys/zap.h> 30#include <sys/arc.h> 31#include <sys/stat.h> 32#include <sys/resource.h> 33#include <sys/zil.h> 34#include <sys/zil_impl.h> 35#include <sys/dsl_dataset.h> 36#include <sys/vdev.h> 37#include <sys/dmu_tx.h> 38 39/* 40 * The zfs intent log (ZIL) saves transaction records of system calls 41 * that change the file system in memory with enough information 42 * to be able to replay them. These are stored in memory until 43 * either the DMU transaction group (txg) commits them to the stable pool 44 * and they can be discarded, or they are flushed to the stable log 45 * (also in the pool) due to a fsync, O_DSYNC or other synchronous 46 * requirement. In the event of a panic or power fail then those log 47 * records (transactions) are replayed. 48 * 49 * There is one ZIL per file system. Its on-disk (pool) format consists 50 * of 3 parts: 51 * 52 * - ZIL header 53 * - ZIL blocks 54 * - ZIL records 55 * 56 * A log record holds a system call transaction. Log blocks can 57 * hold many log records and the blocks are chained together. 58 * Each ZIL block contains a block pointer (blkptr_t) to the next 59 * ZIL block in the chain. The ZIL header points to the first 60 * block in the chain. Note there is not a fixed place in the pool 61 * to hold blocks. They are dynamically allocated and freed as 62 * needed from the blocks available. Figure X shows the ZIL structure: 63 */ 64 65/* 66 * This global ZIL switch affects all pools 67 */ 68int zil_disable = 0; /* disable intent logging */ 69SYSCTL_DECL(_vfs_zfs); 70TUNABLE_INT("vfs.zfs.zil_disable", &zil_disable); 71SYSCTL_INT(_vfs_zfs, OID_AUTO, zil_disable, CTLFLAG_RW, &zil_disable, 0, 72 "Disable ZFS Intent Log (ZIL)"); 73 74/* 75 * Tunable parameter for debugging or performance analysis. Setting 76 * zfs_nocacheflush will cause corruption on power loss if a volatile 77 * out-of-order write cache is enabled. 78 */ 79boolean_t zfs_nocacheflush = B_FALSE; 80TUNABLE_INT("vfs.zfs.cache_flush_disable", &zfs_nocacheflush); 81SYSCTL_INT(_vfs_zfs, OID_AUTO, cache_flush_disable, CTLFLAG_RDTUN, 82 &zfs_nocacheflush, 0, "Disable cache flush"); 83 84static kmem_cache_t *zil_lwb_cache; 85 86static int 87zil_dva_compare(const void *x1, const void *x2) 88{ 89 const dva_t *dva1 = x1; 90 const dva_t *dva2 = x2; 91 92 if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2)) 93 return (-1); 94 if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2)) 95 return (1); 96 97 if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2)) 98 return (-1); 99 if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2)) 100 return (1); 101 102 return (0); 103} 104 105static void 106zil_dva_tree_init(avl_tree_t *t) 107{ 108 avl_create(t, zil_dva_compare, sizeof (zil_dva_node_t), 109 offsetof(zil_dva_node_t, zn_node)); 110} 111 112static void 113zil_dva_tree_fini(avl_tree_t *t) 114{ 115 zil_dva_node_t *zn; 116 void *cookie = NULL; 117 118 while ((zn = avl_destroy_nodes(t, &cookie)) != NULL) 119 kmem_free(zn, sizeof (zil_dva_node_t)); 120 121 avl_destroy(t); 122} 123 124static int 125zil_dva_tree_add(avl_tree_t *t, dva_t *dva) 126{ 127 zil_dva_node_t *zn; 128 avl_index_t where; 129 130 if (avl_find(t, dva, &where) != NULL) 131 return (EEXIST); 132 133 zn = kmem_alloc(sizeof (zil_dva_node_t), KM_SLEEP); 134 zn->zn_dva = *dva; 135 avl_insert(t, zn, where); 136 137 return (0); 138} 139 140static zil_header_t * 141zil_header_in_syncing_context(zilog_t *zilog) 142{ 143 return ((zil_header_t *)zilog->zl_header); 144} 145 146static void 147zil_init_log_chain(zilog_t *zilog, blkptr_t *bp) 148{ 149 zio_cksum_t *zc = &bp->blk_cksum; 150 151 zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL); 152 zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL); 153 zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os); 154 zc->zc_word[ZIL_ZC_SEQ] = 1ULL; 155} 156 157/* 158 * Read a log block, make sure it's valid, and byteswap it if necessary. 159 */ 160static int 161zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, arc_buf_t **abufpp) 162{ 163 blkptr_t blk = *bp; 164 zbookmark_t zb; 165 uint32_t aflags = ARC_WAIT; 166 int error; 167 168 zb.zb_objset = bp->blk_cksum.zc_word[ZIL_ZC_OBJSET]; 169 zb.zb_object = 0; 170 zb.zb_level = -1; 171 zb.zb_blkid = bp->blk_cksum.zc_word[ZIL_ZC_SEQ]; 172 173 *abufpp = NULL; 174 175 /* 176 * We shouldn't be doing any scrubbing while we're doing log 177 * replay, it's OK to not lock. 178 */ 179 error = arc_read_nolock(NULL, zilog->zl_spa, &blk, 180 arc_getbuf_func, abufpp, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL | 181 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB, &aflags, &zb); 182 183 if (error == 0) { 184 char *data = (*abufpp)->b_data; 185 uint64_t blksz = BP_GET_LSIZE(bp); 186 zil_trailer_t *ztp = (zil_trailer_t *)(data + blksz) - 1; 187 zio_cksum_t cksum = bp->blk_cksum; 188 189 /* 190 * Validate the checksummed log block. 191 * 192 * Sequence numbers should be... sequential. The checksum 193 * verifier for the next block should be bp's checksum plus 1. 194 * 195 * Also check the log chain linkage and size used. 196 */ 197 cksum.zc_word[ZIL_ZC_SEQ]++; 198 199 if (bcmp(&cksum, &ztp->zit_next_blk.blk_cksum, 200 sizeof (cksum)) || BP_IS_HOLE(&ztp->zit_next_blk) || 201 (ztp->zit_nused > (blksz - sizeof (zil_trailer_t)))) { 202 error = ECKSUM; 203 } 204 205 if (error) { 206 VERIFY(arc_buf_remove_ref(*abufpp, abufpp) == 1); 207 *abufpp = NULL; 208 } 209 } 210 211 dprintf("error %d on %llu:%llu\n", error, zb.zb_objset, zb.zb_blkid); 212 213 return (error); 214} 215 216/* 217 * Parse the intent log, and call parse_func for each valid record within. 218 * Return the highest sequence number. 219 */ 220uint64_t 221zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func, 222 zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg) 223{ 224 const zil_header_t *zh = zilog->zl_header; 225 uint64_t claim_seq = zh->zh_claim_seq; 226 uint64_t seq = 0; 227 uint64_t max_seq = 0; 228 blkptr_t blk = zh->zh_log; 229 arc_buf_t *abuf; 230 char *lrbuf, *lrp; 231 zil_trailer_t *ztp; 232 int reclen, error; 233 234 if (BP_IS_HOLE(&blk)) 235 return (max_seq); 236 237 /* 238 * Starting at the block pointed to by zh_log we read the log chain. 239 * For each block in the chain we strongly check that block to 240 * ensure its validity. We stop when an invalid block is found. 241 * For each block pointer in the chain we call parse_blk_func(). 242 * For each record in each valid block we call parse_lr_func(). 243 * If the log has been claimed, stop if we encounter a sequence 244 * number greater than the highest claimed sequence number. 245 */ 246 zil_dva_tree_init(&zilog->zl_dva_tree); 247 for (;;) { 248 seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 249 250 if (claim_seq != 0 && seq > claim_seq) 251 break; 252 253 ASSERT(max_seq < seq); 254 max_seq = seq; 255 256 error = zil_read_log_block(zilog, &blk, &abuf); 257 258 if (parse_blk_func != NULL) 259 parse_blk_func(zilog, &blk, arg, txg); 260 261 if (error) 262 break; 263 264 lrbuf = abuf->b_data; 265 ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1; 266 blk = ztp->zit_next_blk; 267 268 if (parse_lr_func == NULL) { 269 VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 270 continue; 271 } 272 273 for (lrp = lrbuf; lrp < lrbuf + ztp->zit_nused; lrp += reclen) { 274 lr_t *lr = (lr_t *)lrp; 275 reclen = lr->lrc_reclen; 276 ASSERT3U(reclen, >=, sizeof (lr_t)); 277 parse_lr_func(zilog, lr, arg, txg); 278 } 279 VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 280 } 281 zil_dva_tree_fini(&zilog->zl_dva_tree); 282 283 return (max_seq); 284} 285 286/* ARGSUSED */ 287static void 288zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg) 289{ 290 spa_t *spa = zilog->zl_spa; 291 int err; 292 293 /* 294 * Claim log block if not already committed and not already claimed. 295 */ 296 if (bp->blk_birth >= first_txg && 297 zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp)) == 0) { 298 err = zio_wait(zio_claim(NULL, spa, first_txg, bp, NULL, NULL, 299 ZIO_FLAG_MUSTSUCCEED)); 300 ASSERT(err == 0); 301 } 302} 303 304static void 305zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg) 306{ 307 if (lrc->lrc_txtype == TX_WRITE) { 308 lr_write_t *lr = (lr_write_t *)lrc; 309 zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg); 310 } 311} 312 313/* ARGSUSED */ 314static void 315zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg) 316{ 317 zio_free_blk(zilog->zl_spa, bp, dmu_tx_get_txg(tx)); 318} 319 320static void 321zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg) 322{ 323 /* 324 * If we previously claimed it, we need to free it. 325 */ 326 if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE) { 327 lr_write_t *lr = (lr_write_t *)lrc; 328 blkptr_t *bp = &lr->lr_blkptr; 329 if (bp->blk_birth >= claim_txg && 330 !zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp))) { 331 (void) arc_free(NULL, zilog->zl_spa, 332 dmu_tx_get_txg(tx), bp, NULL, NULL, ARC_WAIT); 333 } 334 } 335} 336 337/* 338 * Create an on-disk intent log. 339 */ 340static void 341zil_create(zilog_t *zilog) 342{ 343 const zil_header_t *zh = zilog->zl_header; 344 lwb_t *lwb; 345 uint64_t txg = 0; 346 dmu_tx_t *tx = NULL; 347 blkptr_t blk; 348 int error = 0; 349 350 /* 351 * Wait for any previous destroy to complete. 352 */ 353 txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 354 355 ASSERT(zh->zh_claim_txg == 0); 356 ASSERT(zh->zh_replay_seq == 0); 357 358 blk = zh->zh_log; 359 360 /* 361 * If we don't already have an initial log block or we have one 362 * but it's the wrong endianness then allocate one. 363 */ 364 if (BP_IS_HOLE(&blk) || BP_SHOULD_BYTESWAP(&blk)) { 365 tx = dmu_tx_create(zilog->zl_os); 366 (void) dmu_tx_assign(tx, TXG_WAIT); 367 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 368 txg = dmu_tx_get_txg(tx); 369 370 if (!BP_IS_HOLE(&blk)) { 371 zio_free_blk(zilog->zl_spa, &blk, txg); 372 BP_ZERO(&blk); 373 } 374 375 error = zio_alloc_blk(zilog->zl_spa, ZIL_MIN_BLKSZ, &blk, 376 NULL, txg); 377 378 if (error == 0) 379 zil_init_log_chain(zilog, &blk); 380 } 381 382 /* 383 * Allocate a log write buffer (lwb) for the first log block. 384 */ 385 if (error == 0) { 386 lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 387 lwb->lwb_zilog = zilog; 388 lwb->lwb_blk = blk; 389 lwb->lwb_nused = 0; 390 lwb->lwb_sz = BP_GET_LSIZE(&lwb->lwb_blk); 391 lwb->lwb_buf = zio_buf_alloc(lwb->lwb_sz); 392 lwb->lwb_max_txg = txg; 393 lwb->lwb_zio = NULL; 394 395 mutex_enter(&zilog->zl_lock); 396 list_insert_tail(&zilog->zl_lwb_list, lwb); 397 mutex_exit(&zilog->zl_lock); 398 } 399 400 /* 401 * If we just allocated the first log block, commit our transaction 402 * and wait for zil_sync() to stuff the block poiner into zh_log. 403 * (zh is part of the MOS, so we cannot modify it in open context.) 404 */ 405 if (tx != NULL) { 406 dmu_tx_commit(tx); 407 txg_wait_synced(zilog->zl_dmu_pool, txg); 408 } 409 410 ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0); 411} 412 413/* 414 * In one tx, free all log blocks and clear the log header. 415 * If keep_first is set, then we're replaying a log with no content. 416 * We want to keep the first block, however, so that the first 417 * synchronous transaction doesn't require a txg_wait_synced() 418 * in zil_create(). We don't need to txg_wait_synced() here either 419 * when keep_first is set, because both zil_create() and zil_destroy() 420 * will wait for any in-progress destroys to complete. 421 */ 422void 423zil_destroy(zilog_t *zilog, boolean_t keep_first) 424{ 425 const zil_header_t *zh = zilog->zl_header; 426 lwb_t *lwb; 427 dmu_tx_t *tx; 428 uint64_t txg; 429 430 /* 431 * Wait for any previous destroy to complete. 432 */ 433 txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 434 435 if (BP_IS_HOLE(&zh->zh_log)) 436 return; 437 438 tx = dmu_tx_create(zilog->zl_os); 439 (void) dmu_tx_assign(tx, TXG_WAIT); 440 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 441 txg = dmu_tx_get_txg(tx); 442 443 mutex_enter(&zilog->zl_lock); 444 445 /* 446 * It is possible for the ZIL to get the previously mounted zilog 447 * structure of the same dataset if quickly remounted and the dbuf 448 * eviction has not completed. In this case we can see a non 449 * empty lwb list and keep_first will be set. We fix this by 450 * clearing the keep_first. This will be slower but it's very rare. 451 */ 452 if (!list_is_empty(&zilog->zl_lwb_list) && keep_first) 453 keep_first = B_FALSE; 454 455 ASSERT3U(zilog->zl_destroy_txg, <, txg); 456 zilog->zl_destroy_txg = txg; 457 zilog->zl_keep_first = keep_first; 458 459 if (!list_is_empty(&zilog->zl_lwb_list)) { 460 ASSERT(zh->zh_claim_txg == 0); 461 ASSERT(!keep_first); 462 while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 463 list_remove(&zilog->zl_lwb_list, lwb); 464 if (lwb->lwb_buf != NULL) 465 zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 466 zio_free_blk(zilog->zl_spa, &lwb->lwb_blk, txg); 467 kmem_cache_free(zil_lwb_cache, lwb); 468 } 469 } else { 470 if (!keep_first) { 471 (void) zil_parse(zilog, zil_free_log_block, 472 zil_free_log_record, tx, zh->zh_claim_txg); 473 } 474 } 475 mutex_exit(&zilog->zl_lock); 476 477 dmu_tx_commit(tx); 478} 479 480/* 481 * return true if the initial log block is not valid 482 */ 483static boolean_t 484zil_empty(zilog_t *zilog) 485{ 486 const zil_header_t *zh = zilog->zl_header; 487 arc_buf_t *abuf = NULL; 488 489 if (BP_IS_HOLE(&zh->zh_log)) 490 return (B_TRUE); 491 492 if (zil_read_log_block(zilog, &zh->zh_log, &abuf) != 0) 493 return (B_TRUE); 494 495 VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 496 return (B_FALSE); 497} 498 499int 500zil_claim(char *osname, void *txarg) 501{ 502 dmu_tx_t *tx = txarg; 503 uint64_t first_txg = dmu_tx_get_txg(tx); 504 zilog_t *zilog; 505 zil_header_t *zh; 506 objset_t *os; 507 int error; 508 509 error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_USER, &os); 510 if (error) { 511 cmn_err(CE_WARN, "can't open objset for %s", osname); 512 return (0); 513 } 514 515 zilog = dmu_objset_zil(os); 516 zh = zil_header_in_syncing_context(zilog); 517 518 /* 519 * Record here whether the zil has any records to replay. 520 * If the header block pointer is null or the block points 521 * to the stubby then we know there are no valid log records. 522 * We use the header to store this state as the the zilog gets 523 * freed later in dmu_objset_close(). 524 * The flags (and the rest of the header fields) are cleared in 525 * zil_sync() as a result of a zil_destroy(), after replaying the log. 526 * 527 * Note, the intent log can be empty but still need the 528 * stubby to be claimed. 529 */ 530 if (!zil_empty(zilog)) 531 zh->zh_flags |= ZIL_REPLAY_NEEDED; 532 533 /* 534 * Claim all log blocks if we haven't already done so, and remember 535 * the highest claimed sequence number. This ensures that if we can 536 * read only part of the log now (e.g. due to a missing device), 537 * but we can read the entire log later, we will not try to replay 538 * or destroy beyond the last block we successfully claimed. 539 */ 540 ASSERT3U(zh->zh_claim_txg, <=, first_txg); 541 if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) { 542 zh->zh_claim_txg = first_txg; 543 zh->zh_claim_seq = zil_parse(zilog, zil_claim_log_block, 544 zil_claim_log_record, tx, first_txg); 545 dsl_dataset_dirty(dmu_objset_ds(os), tx); 546 } 547 548 ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1)); 549 dmu_objset_close(os); 550 return (0); 551} 552 553/* 554 * Check the log by walking the log chain. 555 * Checksum errors are ok as they indicate the end of the chain. 556 * Any other error (no device or read failure) returns an error. 557 */ 558/* ARGSUSED */ 559int 560zil_check_log_chain(char *osname, void *txarg) 561{ 562 zilog_t *zilog; 563 zil_header_t *zh; 564 blkptr_t blk; 565 arc_buf_t *abuf; 566 objset_t *os; 567 char *lrbuf; 568 zil_trailer_t *ztp; 569 int error; 570 571 error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_USER, &os); 572 if (error) { 573 cmn_err(CE_WARN, "can't open objset for %s", osname); 574 return (0); 575 } 576 577 zilog = dmu_objset_zil(os); 578 zh = zil_header_in_syncing_context(zilog); 579 blk = zh->zh_log; 580 if (BP_IS_HOLE(&blk)) { 581 dmu_objset_close(os); 582 return (0); /* no chain */ 583 } 584 585 for (;;) { 586 error = zil_read_log_block(zilog, &blk, &abuf); 587 if (error) 588 break; 589 lrbuf = abuf->b_data; 590 ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1; 591 blk = ztp->zit_next_blk; 592 VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 593 } 594 dmu_objset_close(os); 595 if (error == ECKSUM) 596 return (0); /* normal end of chain */ 597 return (error); 598} 599 600/* 601 * Clear a log chain 602 */ 603/* ARGSUSED */ 604int 605zil_clear_log_chain(char *osname, void *txarg) 606{ 607 zilog_t *zilog; 608 zil_header_t *zh; 609 objset_t *os; 610 dmu_tx_t *tx; 611 int error; 612 613 error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_USER, &os); 614 if (error) { 615 cmn_err(CE_WARN, "can't open objset for %s", osname); 616 return (0); 617 } 618 619 zilog = dmu_objset_zil(os); 620 tx = dmu_tx_create(zilog->zl_os); 621 (void) dmu_tx_assign(tx, TXG_WAIT); 622 zh = zil_header_in_syncing_context(zilog); 623 BP_ZERO(&zh->zh_log); 624 dsl_dataset_dirty(dmu_objset_ds(os), tx); 625 dmu_tx_commit(tx); 626 dmu_objset_close(os); 627 return (0); 628} 629 630static int 631zil_vdev_compare(const void *x1, const void *x2) 632{ 633 uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev; 634 uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev; 635 636 if (v1 < v2) 637 return (-1); 638 if (v1 > v2) 639 return (1); 640 641 return (0); 642} 643 644void 645zil_add_block(zilog_t *zilog, blkptr_t *bp) 646{ 647 avl_tree_t *t = &zilog->zl_vdev_tree; 648 avl_index_t where; 649 zil_vdev_node_t *zv, zvsearch; 650 int ndvas = BP_GET_NDVAS(bp); 651 int i; 652 653 if (zfs_nocacheflush) 654 return; 655 656 ASSERT(zilog->zl_writer); 657 658 /* 659 * Even though we're zl_writer, we still need a lock because the 660 * zl_get_data() callbacks may have dmu_sync() done callbacks 661 * that will run concurrently. 662 */ 663 mutex_enter(&zilog->zl_vdev_lock); 664 for (i = 0; i < ndvas; i++) { 665 zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]); 666 if (avl_find(t, &zvsearch, &where) == NULL) { 667 zv = kmem_alloc(sizeof (*zv), KM_SLEEP); 668 zv->zv_vdev = zvsearch.zv_vdev; 669 avl_insert(t, zv, where); 670 } 671 } 672 mutex_exit(&zilog->zl_vdev_lock); 673} 674 675void 676zil_flush_vdevs(zilog_t *zilog) 677{ 678 spa_t *spa = zilog->zl_spa; 679 avl_tree_t *t = &zilog->zl_vdev_tree; 680 void *cookie = NULL; 681 zil_vdev_node_t *zv; 682 zio_t *zio; 683 684 ASSERT(zilog->zl_writer); 685 686 /* 687 * We don't need zl_vdev_lock here because we're the zl_writer, 688 * and all zl_get_data() callbacks are done. 689 */ 690 if (avl_numnodes(t) == 0) 691 return; 692 693 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 694 695 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); 696 697 while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) { 698 vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev); 699 if (vd != NULL) 700 zio_flush(zio, vd); 701 kmem_free(zv, sizeof (*zv)); 702 } 703 704 /* 705 * Wait for all the flushes to complete. Not all devices actually 706 * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails. 707 */ 708 (void) zio_wait(zio); 709 710 spa_config_exit(spa, SCL_STATE, FTAG); 711} 712 713/* 714 * Function called when a log block write completes 715 */ 716static void 717zil_lwb_write_done(zio_t *zio) 718{ 719 lwb_t *lwb = zio->io_private; 720 zilog_t *zilog = lwb->lwb_zilog; 721 722 ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF); 723 ASSERT(BP_GET_CHECKSUM(zio->io_bp) == ZIO_CHECKSUM_ZILOG); 724 ASSERT(BP_GET_TYPE(zio->io_bp) == DMU_OT_INTENT_LOG); 725 ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 726 ASSERT(BP_GET_BYTEORDER(zio->io_bp) == ZFS_HOST_BYTEORDER); 727 ASSERT(!BP_IS_GANG(zio->io_bp)); 728 ASSERT(!BP_IS_HOLE(zio->io_bp)); 729 ASSERT(zio->io_bp->blk_fill == 0); 730 731 /* 732 * Now that we've written this log block, we have a stable pointer 733 * to the next block in the chain, so it's OK to let the txg in 734 * which we allocated the next block sync. 735 */ 736 txg_rele_to_sync(&lwb->lwb_txgh); 737 738 zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 739 mutex_enter(&zilog->zl_lock); 740 lwb->lwb_buf = NULL; 741 if (zio->io_error) 742 zilog->zl_log_error = B_TRUE; 743 mutex_exit(&zilog->zl_lock); 744} 745 746/* 747 * Initialize the io for a log block. 748 */ 749static void 750zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb) 751{ 752 zbookmark_t zb; 753 754 zb.zb_objset = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET]; 755 zb.zb_object = 0; 756 zb.zb_level = -1; 757 zb.zb_blkid = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 758 759 if (zilog->zl_root_zio == NULL) { 760 zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL, 761 ZIO_FLAG_CANFAIL); 762 } 763 if (lwb->lwb_zio == NULL) { 764 lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa, 765 0, &lwb->lwb_blk, lwb->lwb_buf, 766 lwb->lwb_sz, zil_lwb_write_done, lwb, 767 ZIO_PRIORITY_LOG_WRITE, ZIO_FLAG_CANFAIL, &zb); 768 } 769} 770 771/* 772 * Start a log block write and advance to the next log block. 773 * Calls are serialized. 774 */ 775static lwb_t * 776zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb) 777{ 778 lwb_t *nlwb; 779 zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1; 780 spa_t *spa = zilog->zl_spa; 781 blkptr_t *bp = &ztp->zit_next_blk; 782 uint64_t txg; 783 uint64_t zil_blksz; 784 int error; 785 786 ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb)); 787 788 /* 789 * Allocate the next block and save its address in this block 790 * before writing it in order to establish the log chain. 791 * Note that if the allocation of nlwb synced before we wrote 792 * the block that points at it (lwb), we'd leak it if we crashed. 793 * Therefore, we don't do txg_rele_to_sync() until zil_lwb_write_done(). 794 */ 795 txg = txg_hold_open(zilog->zl_dmu_pool, &lwb->lwb_txgh); 796 txg_rele_to_quiesce(&lwb->lwb_txgh); 797 798 /* 799 * Pick a ZIL blocksize. We request a size that is the 800 * maximum of the previous used size, the current used size and 801 * the amount waiting in the queue. 802 */ 803 zil_blksz = MAX(zilog->zl_prev_used, 804 zilog->zl_cur_used + sizeof (*ztp)); 805 zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp)); 806 zil_blksz = P2ROUNDUP_TYPED(zil_blksz, ZIL_MIN_BLKSZ, uint64_t); 807 if (zil_blksz > ZIL_MAX_BLKSZ) 808 zil_blksz = ZIL_MAX_BLKSZ; 809 810 BP_ZERO(bp); 811 /* pass the old blkptr in order to spread log blocks across devs */ 812 error = zio_alloc_blk(spa, zil_blksz, bp, &lwb->lwb_blk, txg); 813 if (error) { 814 dmu_tx_t *tx = dmu_tx_create_assigned(zilog->zl_dmu_pool, txg); 815 816 /* 817 * We dirty the dataset to ensure that zil_sync() will 818 * be called to remove this lwb from our zl_lwb_list. 819 * Failing to do so, may leave an lwb with a NULL lwb_buf 820 * hanging around on the zl_lwb_list. 821 */ 822 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 823 dmu_tx_commit(tx); 824 825 /* 826 * Since we've just experienced an allocation failure so we 827 * terminate the current lwb and send it on its way. 828 */ 829 ztp->zit_pad = 0; 830 ztp->zit_nused = lwb->lwb_nused; 831 ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum; 832 zio_nowait(lwb->lwb_zio); 833 834 /* 835 * By returning NULL the caller will call tx_wait_synced() 836 */ 837 return (NULL); 838 } 839 840 ASSERT3U(bp->blk_birth, ==, txg); 841 ztp->zit_pad = 0; 842 ztp->zit_nused = lwb->lwb_nused; 843 ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum; 844 bp->blk_cksum = lwb->lwb_blk.blk_cksum; 845 bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++; 846 847 /* 848 * Allocate a new log write buffer (lwb). 849 */ 850 nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 851 852 nlwb->lwb_zilog = zilog; 853 nlwb->lwb_blk = *bp; 854 nlwb->lwb_nused = 0; 855 nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk); 856 nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz); 857 nlwb->lwb_max_txg = txg; 858 nlwb->lwb_zio = NULL; 859 860 /* 861 * Put new lwb at the end of the log chain 862 */ 863 mutex_enter(&zilog->zl_lock); 864 list_insert_tail(&zilog->zl_lwb_list, nlwb); 865 mutex_exit(&zilog->zl_lock); 866 867 /* Record the block for later vdev flushing */ 868 zil_add_block(zilog, &lwb->lwb_blk); 869 870 /* 871 * kick off the write for the old log block 872 */ 873 dprintf_bp(&lwb->lwb_blk, "lwb %p txg %llu: ", lwb, txg); 874 ASSERT(lwb->lwb_zio); 875 zio_nowait(lwb->lwb_zio); 876 877 return (nlwb); 878} 879 880static lwb_t * 881zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb) 882{ 883 lr_t *lrc = &itx->itx_lr; /* common log record */ 884 lr_write_t *lr = (lr_write_t *)lrc; 885 uint64_t txg = lrc->lrc_txg; 886 uint64_t reclen = lrc->lrc_reclen; 887 uint64_t dlen; 888 889 if (lwb == NULL) 890 return (NULL); 891 ASSERT(lwb->lwb_buf != NULL); 892 893 if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY) 894 dlen = P2ROUNDUP_TYPED( 895 lr->lr_length, sizeof (uint64_t), uint64_t); 896 else 897 dlen = 0; 898 899 zilog->zl_cur_used += (reclen + dlen); 900 901 zil_lwb_write_init(zilog, lwb); 902 903 /* 904 * If this record won't fit in the current log block, start a new one. 905 */ 906 if (lwb->lwb_nused + reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) { 907 lwb = zil_lwb_write_start(zilog, lwb); 908 if (lwb == NULL) 909 return (NULL); 910 zil_lwb_write_init(zilog, lwb); 911 ASSERT(lwb->lwb_nused == 0); 912 if (reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) { 913 txg_wait_synced(zilog->zl_dmu_pool, txg); 914 return (lwb); 915 } 916 } 917 918 /* 919 * Update the lrc_seq, to be log record sequence number. See zil.h 920 * Then copy the record to the log buffer. 921 */ 922 lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */ 923 bcopy(lrc, lwb->lwb_buf + lwb->lwb_nused, reclen); 924 925 /* 926 * If it's a write, fetch the data or get its blkptr as appropriate. 927 */ 928 if (lrc->lrc_txtype == TX_WRITE) { 929 if (txg > spa_freeze_txg(zilog->zl_spa)) 930 txg_wait_synced(zilog->zl_dmu_pool, txg); 931 if (itx->itx_wr_state != WR_COPIED) { 932 char *dbuf; 933 int error; 934 935 /* alignment is guaranteed */ 936 lr = (lr_write_t *)(lwb->lwb_buf + lwb->lwb_nused); 937 if (dlen) { 938 ASSERT(itx->itx_wr_state == WR_NEED_COPY); 939 dbuf = lwb->lwb_buf + lwb->lwb_nused + reclen; 940 lr->lr_common.lrc_reclen += dlen; 941 } else { 942 ASSERT(itx->itx_wr_state == WR_INDIRECT); 943 dbuf = NULL; 944 } 945 error = zilog->zl_get_data( 946 itx->itx_private, lr, dbuf, lwb->lwb_zio); 947 if (error) { 948 ASSERT(error == ENOENT || error == EEXIST || 949 error == EALREADY); 950 return (lwb); 951 } 952 } 953 } 954 955 lwb->lwb_nused += reclen + dlen; 956 lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg); 957 ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb)); 958 ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0); 959 960 return (lwb); 961} 962 963itx_t * 964zil_itx_create(uint64_t txtype, size_t lrsize) 965{ 966 itx_t *itx; 967 968 lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t); 969 970 itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP); 971 itx->itx_lr.lrc_txtype = txtype; 972 itx->itx_lr.lrc_reclen = lrsize; 973 itx->itx_sod = lrsize; /* if write & WR_NEED_COPY will be increased */ 974 itx->itx_lr.lrc_seq = 0; /* defensive */ 975 976 return (itx); 977} 978 979uint64_t 980zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx) 981{ 982 uint64_t seq; 983 984 ASSERT(itx->itx_lr.lrc_seq == 0); 985 986 mutex_enter(&zilog->zl_lock); 987 list_insert_tail(&zilog->zl_itx_list, itx); 988 zilog->zl_itx_list_sz += itx->itx_sod; 989 itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx); 990 itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq; 991 mutex_exit(&zilog->zl_lock); 992 993 return (seq); 994} 995 996/* 997 * Free up all in-memory intent log transactions that have now been synced. 998 */ 999static void 1000zil_itx_clean(zilog_t *zilog) 1001{ 1002 uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa); 1003 uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa); 1004 list_t clean_list; 1005 itx_t *itx; 1006 1007 list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node)); 1008 1009 mutex_enter(&zilog->zl_lock); 1010 /* wait for a log writer to finish walking list */ 1011 while (zilog->zl_writer) { 1012 cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 1013 } 1014 1015 /* 1016 * Move the sync'd log transactions to a separate list so we can call 1017 * kmem_free without holding the zl_lock. 1018 * 1019 * There is no need to set zl_writer as we don't drop zl_lock here 1020 */ 1021 while ((itx = list_head(&zilog->zl_itx_list)) != NULL && 1022 itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) { 1023 list_remove(&zilog->zl_itx_list, itx); 1024 zilog->zl_itx_list_sz -= itx->itx_sod; 1025 list_insert_tail(&clean_list, itx); 1026 } 1027 cv_broadcast(&zilog->zl_cv_writer); 1028 mutex_exit(&zilog->zl_lock); 1029 1030 /* destroy sync'd log transactions */ 1031 while ((itx = list_head(&clean_list)) != NULL) { 1032 list_remove(&clean_list, itx); 1033 kmem_free(itx, offsetof(itx_t, itx_lr) 1034 + itx->itx_lr.lrc_reclen); 1035 } 1036 list_destroy(&clean_list); 1037} 1038 1039/* 1040 * If there are any in-memory intent log transactions which have now been 1041 * synced then start up a taskq to free them. 1042 */ 1043void 1044zil_clean(zilog_t *zilog) 1045{ 1046 itx_t *itx; 1047 1048 mutex_enter(&zilog->zl_lock); 1049 itx = list_head(&zilog->zl_itx_list); 1050 if ((itx != NULL) && 1051 (itx->itx_lr.lrc_txg <= spa_last_synced_txg(zilog->zl_spa))) { 1052 (void) taskq_dispatch(zilog->zl_clean_taskq, 1053 (task_func_t *)zil_itx_clean, zilog, TQ_SLEEP); 1054 } 1055 mutex_exit(&zilog->zl_lock); 1056} 1057 1058static void 1059zil_commit_writer(zilog_t *zilog, uint64_t seq, uint64_t foid) 1060{ 1061 uint64_t txg; 1062 uint64_t commit_seq = 0; 1063 itx_t *itx, *itx_next = (itx_t *)-1; 1064 lwb_t *lwb; 1065 spa_t *spa; 1066 1067 zilog->zl_writer = B_TRUE; 1068 ASSERT(zilog->zl_root_zio == NULL); 1069 spa = zilog->zl_spa; 1070 1071 if (zilog->zl_suspend) { 1072 lwb = NULL; 1073 } else { 1074 lwb = list_tail(&zilog->zl_lwb_list); 1075 if (lwb == NULL) { 1076 /* 1077 * Return if there's nothing to flush before we 1078 * dirty the fs by calling zil_create() 1079 */ 1080 if (list_is_empty(&zilog->zl_itx_list)) { 1081 zilog->zl_writer = B_FALSE; 1082 return; 1083 } 1084 mutex_exit(&zilog->zl_lock); 1085 zil_create(zilog); 1086 mutex_enter(&zilog->zl_lock); 1087 lwb = list_tail(&zilog->zl_lwb_list); 1088 } 1089 } 1090 1091 /* Loop through in-memory log transactions filling log blocks. */ 1092 DTRACE_PROBE1(zil__cw1, zilog_t *, zilog); 1093 for (;;) { 1094 /* 1095 * Find the next itx to push: 1096 * Push all transactions related to specified foid and all 1097 * other transactions except TX_WRITE, TX_TRUNCATE, 1098 * TX_SETATTR and TX_ACL for all other files. 1099 */ 1100 if (itx_next != (itx_t *)-1) 1101 itx = itx_next; 1102 else 1103 itx = list_head(&zilog->zl_itx_list); 1104 for (; itx != NULL; itx = list_next(&zilog->zl_itx_list, itx)) { 1105 if (foid == 0) /* push all foids? */ 1106 break; 1107 if (itx->itx_sync) /* push all O_[D]SYNC */ 1108 break; 1109 switch (itx->itx_lr.lrc_txtype) { 1110 case TX_SETATTR: 1111 case TX_WRITE: 1112 case TX_TRUNCATE: 1113 case TX_ACL: 1114 /* lr_foid is same offset for these records */ 1115 if (((lr_write_t *)&itx->itx_lr)->lr_foid 1116 != foid) { 1117 continue; /* skip this record */ 1118 } 1119 } 1120 break; 1121 } 1122 if (itx == NULL) 1123 break; 1124 1125 if ((itx->itx_lr.lrc_seq > seq) && 1126 ((lwb == NULL) || (lwb->lwb_nused == 0) || 1127 (lwb->lwb_nused + itx->itx_sod > ZIL_BLK_DATA_SZ(lwb)))) { 1128 break; 1129 } 1130 1131 /* 1132 * Save the next pointer. Even though we soon drop 1133 * zl_lock all threads that may change the list 1134 * (another writer or zil_itx_clean) can't do so until 1135 * they have zl_writer. 1136 */ 1137 itx_next = list_next(&zilog->zl_itx_list, itx); 1138 list_remove(&zilog->zl_itx_list, itx); 1139 zilog->zl_itx_list_sz -= itx->itx_sod; 1140 mutex_exit(&zilog->zl_lock); 1141 txg = itx->itx_lr.lrc_txg; 1142 ASSERT(txg); 1143 1144 if (txg > spa_last_synced_txg(spa) || 1145 txg > spa_freeze_txg(spa)) 1146 lwb = zil_lwb_commit(zilog, itx, lwb); 1147 kmem_free(itx, offsetof(itx_t, itx_lr) 1148 + itx->itx_lr.lrc_reclen); 1149 mutex_enter(&zilog->zl_lock); 1150 } 1151 DTRACE_PROBE1(zil__cw2, zilog_t *, zilog); 1152 /* determine commit sequence number */ 1153 itx = list_head(&zilog->zl_itx_list); 1154 if (itx) 1155 commit_seq = itx->itx_lr.lrc_seq; 1156 else 1157 commit_seq = zilog->zl_itx_seq; 1158 mutex_exit(&zilog->zl_lock); 1159 1160 /* write the last block out */ 1161 if (lwb != NULL && lwb->lwb_zio != NULL) 1162 lwb = zil_lwb_write_start(zilog, lwb); 1163 1164 zilog->zl_prev_used = zilog->zl_cur_used; 1165 zilog->zl_cur_used = 0; 1166 1167 /* 1168 * Wait if necessary for the log blocks to be on stable storage. 1169 */ 1170 if (zilog->zl_root_zio) { 1171 DTRACE_PROBE1(zil__cw3, zilog_t *, zilog); 1172 (void) zio_wait(zilog->zl_root_zio); 1173 zilog->zl_root_zio = NULL; 1174 DTRACE_PROBE1(zil__cw4, zilog_t *, zilog); 1175 zil_flush_vdevs(zilog); 1176 } 1177 1178 if (zilog->zl_log_error || lwb == NULL) { 1179 zilog->zl_log_error = 0; 1180 txg_wait_synced(zilog->zl_dmu_pool, 0); 1181 } 1182 1183 mutex_enter(&zilog->zl_lock); 1184 zilog->zl_writer = B_FALSE; 1185 1186 ASSERT3U(commit_seq, >=, zilog->zl_commit_seq); 1187 zilog->zl_commit_seq = commit_seq; 1188} 1189 1190/* 1191 * Push zfs transactions to stable storage up to the supplied sequence number. 1192 * If foid is 0 push out all transactions, otherwise push only those 1193 * for that file or might have been used to create that file. 1194 */ 1195void 1196zil_commit(zilog_t *zilog, uint64_t seq, uint64_t foid) 1197{ 1198 if (zilog == NULL || seq == 0) 1199 return; 1200 1201 mutex_enter(&zilog->zl_lock); 1202 1203 seq = MIN(seq, zilog->zl_itx_seq); /* cap seq at largest itx seq */ 1204 1205 while (zilog->zl_writer) { 1206 cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 1207 if (seq < zilog->zl_commit_seq) { 1208 mutex_exit(&zilog->zl_lock); 1209 return; 1210 } 1211 } 1212 zil_commit_writer(zilog, seq, foid); /* drops zl_lock */ 1213 /* wake up others waiting on the commit */ 1214 cv_broadcast(&zilog->zl_cv_writer); 1215 mutex_exit(&zilog->zl_lock); 1216} 1217 1218/* 1219 * Called in syncing context to free committed log blocks and update log header. 1220 */ 1221void 1222zil_sync(zilog_t *zilog, dmu_tx_t *tx) 1223{ 1224 zil_header_t *zh = zil_header_in_syncing_context(zilog); 1225 uint64_t txg = dmu_tx_get_txg(tx); 1226 spa_t *spa = zilog->zl_spa; 1227 lwb_t *lwb; 1228 1229 mutex_enter(&zilog->zl_lock); 1230 1231 ASSERT(zilog->zl_stop_sync == 0); 1232 1233 zh->zh_replay_seq = zilog->zl_replay_seq[txg & TXG_MASK]; 1234 1235 if (zilog->zl_destroy_txg == txg) { 1236 blkptr_t blk = zh->zh_log; 1237 1238 ASSERT(list_head(&zilog->zl_lwb_list) == NULL); 1239 ASSERT(spa_sync_pass(spa) == 1); 1240 1241 bzero(zh, sizeof (zil_header_t)); 1242 bzero(zilog->zl_replay_seq, sizeof (zilog->zl_replay_seq)); 1243 1244 if (zilog->zl_keep_first) { 1245 /* 1246 * If this block was part of log chain that couldn't 1247 * be claimed because a device was missing during 1248 * zil_claim(), but that device later returns, 1249 * then this block could erroneously appear valid. 1250 * To guard against this, assign a new GUID to the new 1251 * log chain so it doesn't matter what blk points to. 1252 */ 1253 zil_init_log_chain(zilog, &blk); 1254 zh->zh_log = blk; 1255 } 1256 } 1257 1258 for (;;) { 1259 lwb = list_head(&zilog->zl_lwb_list); 1260 if (lwb == NULL) { 1261 mutex_exit(&zilog->zl_lock); 1262 return; 1263 } 1264 zh->zh_log = lwb->lwb_blk; 1265 if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg) 1266 break; 1267 list_remove(&zilog->zl_lwb_list, lwb); 1268 zio_free_blk(spa, &lwb->lwb_blk, txg); 1269 kmem_cache_free(zil_lwb_cache, lwb); 1270 1271 /* 1272 * If we don't have anything left in the lwb list then 1273 * we've had an allocation failure and we need to zero 1274 * out the zil_header blkptr so that we don't end 1275 * up freeing the same block twice. 1276 */ 1277 if (list_head(&zilog->zl_lwb_list) == NULL) 1278 BP_ZERO(&zh->zh_log); 1279 } 1280 mutex_exit(&zilog->zl_lock); 1281} 1282 1283void 1284zil_init(void) 1285{ 1286 zil_lwb_cache = kmem_cache_create("zil_lwb_cache", 1287 sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0); 1288} 1289 1290void 1291zil_fini(void) 1292{ 1293 kmem_cache_destroy(zil_lwb_cache); 1294} 1295 1296zilog_t * 1297zil_alloc(objset_t *os, zil_header_t *zh_phys) 1298{ 1299 zilog_t *zilog; 1300 1301 zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP); 1302 1303 zilog->zl_header = zh_phys; 1304 zilog->zl_os = os; 1305 zilog->zl_spa = dmu_objset_spa(os); 1306 zilog->zl_dmu_pool = dmu_objset_pool(os); 1307 zilog->zl_destroy_txg = TXG_INITIAL - 1; 1308 1309 mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL); 1310 1311 list_create(&zilog->zl_itx_list, sizeof (itx_t), 1312 offsetof(itx_t, itx_node)); 1313 1314 list_create(&zilog->zl_lwb_list, sizeof (lwb_t), 1315 offsetof(lwb_t, lwb_node)); 1316 1317 mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL); 1318 1319 avl_create(&zilog->zl_vdev_tree, zil_vdev_compare, 1320 sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node)); 1321 1322 cv_init(&zilog->zl_cv_writer, NULL, CV_DEFAULT, NULL); 1323 cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL); 1324 1325 return (zilog); 1326} 1327 1328void 1329zil_free(zilog_t *zilog) 1330{ 1331 lwb_t *lwb; 1332 1333 zilog->zl_stop_sync = 1; 1334 1335 while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 1336 list_remove(&zilog->zl_lwb_list, lwb); 1337 if (lwb->lwb_buf != NULL) 1338 zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 1339 kmem_cache_free(zil_lwb_cache, lwb); 1340 } 1341 list_destroy(&zilog->zl_lwb_list); 1342 1343 avl_destroy(&zilog->zl_vdev_tree); 1344 mutex_destroy(&zilog->zl_vdev_lock); 1345 1346 ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1347 list_destroy(&zilog->zl_itx_list); 1348 mutex_destroy(&zilog->zl_lock); 1349 1350 cv_destroy(&zilog->zl_cv_writer); 1351 cv_destroy(&zilog->zl_cv_suspend); 1352 1353 kmem_free(zilog, sizeof (zilog_t)); 1354} 1355 1356/* 1357 * Open an intent log. 1358 */ 1359zilog_t * 1360zil_open(objset_t *os, zil_get_data_t *get_data) 1361{ 1362 zilog_t *zilog = dmu_objset_zil(os); 1363 1364 zilog->zl_get_data = get_data; 1365 zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri, 1366 2, 2, TASKQ_PREPOPULATE); 1367 1368 return (zilog); 1369} 1370 1371/* 1372 * Close an intent log. 1373 */ 1374void 1375zil_close(zilog_t *zilog) 1376{ 1377 /* 1378 * If the log isn't already committed, mark the objset dirty 1379 * (so zil_sync() will be called) and wait for that txg to sync. 1380 */ 1381 if (!zil_is_committed(zilog)) { 1382 uint64_t txg; 1383 dmu_tx_t *tx = dmu_tx_create(zilog->zl_os); 1384 (void) dmu_tx_assign(tx, TXG_WAIT); 1385 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 1386 txg = dmu_tx_get_txg(tx); 1387 dmu_tx_commit(tx); 1388 txg_wait_synced(zilog->zl_dmu_pool, txg); 1389 } 1390 1391 taskq_destroy(zilog->zl_clean_taskq); 1392 zilog->zl_clean_taskq = NULL; 1393 zilog->zl_get_data = NULL; 1394 1395 zil_itx_clean(zilog); 1396 ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1397} 1398 1399/* 1400 * Suspend an intent log. While in suspended mode, we still honor 1401 * synchronous semantics, but we rely on txg_wait_synced() to do it. 1402 * We suspend the log briefly when taking a snapshot so that the snapshot 1403 * contains all the data it's supposed to, and has an empty intent log. 1404 */ 1405int 1406zil_suspend(zilog_t *zilog) 1407{ 1408 const zil_header_t *zh = zilog->zl_header; 1409 1410 mutex_enter(&zilog->zl_lock); 1411 if (zh->zh_flags & ZIL_REPLAY_NEEDED) { /* unplayed log */ 1412 mutex_exit(&zilog->zl_lock); 1413 return (EBUSY); 1414 } 1415 if (zilog->zl_suspend++ != 0) { 1416 /* 1417 * Someone else already began a suspend. 1418 * Just wait for them to finish. 1419 */ 1420 while (zilog->zl_suspending) 1421 cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock); 1422 mutex_exit(&zilog->zl_lock); 1423 return (0); 1424 } 1425 zilog->zl_suspending = B_TRUE; 1426 mutex_exit(&zilog->zl_lock); 1427 1428 zil_commit(zilog, UINT64_MAX, 0); 1429 1430 /* 1431 * Wait for any in-flight log writes to complete. 1432 */ 1433 mutex_enter(&zilog->zl_lock); 1434 while (zilog->zl_writer) 1435 cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 1436 mutex_exit(&zilog->zl_lock); 1437 1438 zil_destroy(zilog, B_FALSE); 1439 1440 mutex_enter(&zilog->zl_lock); 1441 zilog->zl_suspending = B_FALSE; 1442 cv_broadcast(&zilog->zl_cv_suspend); 1443 mutex_exit(&zilog->zl_lock); 1444 1445 return (0); 1446} 1447 1448void 1449zil_resume(zilog_t *zilog) 1450{ 1451 mutex_enter(&zilog->zl_lock); 1452 ASSERT(zilog->zl_suspend != 0); 1453 zilog->zl_suspend--; 1454 mutex_exit(&zilog->zl_lock); 1455} 1456 1457typedef struct zil_replay_arg { 1458 objset_t *zr_os; 1459 zil_replay_func_t **zr_replay; 1460 zil_replay_cleaner_t *zr_replay_cleaner; 1461 void *zr_arg; 1462 uint64_t *zr_txgp; 1463 boolean_t zr_byteswap; 1464 char *zr_lrbuf; 1465} zil_replay_arg_t; 1466 1467static void 1468zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg) 1469{ 1470 zil_replay_arg_t *zr = zra; 1471 const zil_header_t *zh = zilog->zl_header; 1472 uint64_t reclen = lr->lrc_reclen; 1473 uint64_t txtype = lr->lrc_txtype; 1474 char *name; 1475 int pass, error, sunk; 1476 1477 if (zilog->zl_stop_replay) 1478 return; 1479 1480 if (lr->lrc_txg < claim_txg) /* already committed */ 1481 return; 1482 1483 if (lr->lrc_seq <= zh->zh_replay_seq) /* already replayed */ 1484 return; 1485 1486 /* Strip case-insensitive bit, still present in log record */ 1487 txtype &= ~TX_CI; 1488 1489 /* 1490 * Make a copy of the data so we can revise and extend it. 1491 */ 1492 bcopy(lr, zr->zr_lrbuf, reclen); 1493 1494 /* 1495 * The log block containing this lr may have been byteswapped 1496 * so that we can easily examine common fields like lrc_txtype. 1497 * However, the log is a mix of different data types, and only the 1498 * replay vectors know how to byteswap their records. Therefore, if 1499 * the lr was byteswapped, undo it before invoking the replay vector. 1500 */ 1501 if (zr->zr_byteswap) 1502 byteswap_uint64_array(zr->zr_lrbuf, reclen); 1503 1504 /* 1505 * If this is a TX_WRITE with a blkptr, suck in the data. 1506 */ 1507 if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) { 1508 lr_write_t *lrw = (lr_write_t *)lr; 1509 blkptr_t *wbp = &lrw->lr_blkptr; 1510 uint64_t wlen = lrw->lr_length; 1511 char *wbuf = zr->zr_lrbuf + reclen; 1512 1513 if (BP_IS_HOLE(wbp)) { /* compressed to a hole */ 1514 bzero(wbuf, wlen); 1515 } else { 1516 /* 1517 * A subsequent write may have overwritten this block, 1518 * in which case wbp may have been been freed and 1519 * reallocated, and our read of wbp may fail with a 1520 * checksum error. We can safely ignore this because 1521 * the later write will provide the correct data. 1522 */ 1523 zbookmark_t zb; 1524 1525 zb.zb_objset = dmu_objset_id(zilog->zl_os); 1526 zb.zb_object = lrw->lr_foid; 1527 zb.zb_level = -1; 1528 zb.zb_blkid = lrw->lr_offset / BP_GET_LSIZE(wbp); 1529 1530 (void) zio_wait(zio_read(NULL, zilog->zl_spa, 1531 wbp, wbuf, BP_GET_LSIZE(wbp), NULL, NULL, 1532 ZIO_PRIORITY_SYNC_READ, 1533 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb)); 1534 (void) memmove(wbuf, wbuf + lrw->lr_blkoff, wlen); 1535 } 1536 } 1537 1538 /* 1539 * Replay of large truncates can end up needing additional txs 1540 * and a different txg. If they are nested within the replay tx 1541 * as below then a hang is possible. So we do the truncate here 1542 * and redo the truncate later (a no-op) and update the sequence 1543 * number whilst in the replay tx. Fortunately, it's safe to repeat 1544 * a truncate if we crash and the truncate commits. A create over 1545 * an existing file will also come in as a TX_TRUNCATE record. 1546 * 1547 * Note, remove of large files and renames over large files is 1548 * handled by putting the deleted object on a stable list 1549 * and if necessary force deleting the object outside of the replay 1550 * transaction using the zr_replay_cleaner. 1551 */ 1552 if (txtype == TX_TRUNCATE) { 1553 *zr->zr_txgp = TXG_NOWAIT; 1554 error = zr->zr_replay[TX_TRUNCATE](zr->zr_arg, zr->zr_lrbuf, 1555 zr->zr_byteswap); 1556 if (error) 1557 goto bad; 1558 zr->zr_byteswap = 0; /* only byteswap once */ 1559 } 1560 1561 /* 1562 * We must now do two things atomically: replay this log record, 1563 * and update the log header to reflect the fact that we did so. 1564 * We use the DMU's ability to assign into a specific txg to do this. 1565 */ 1566 for (pass = 1, sunk = B_FALSE; /* CONSTANTCONDITION */; pass++) { 1567 uint64_t replay_txg; 1568 dmu_tx_t *replay_tx; 1569 1570 replay_tx = dmu_tx_create(zr->zr_os); 1571 error = dmu_tx_assign(replay_tx, TXG_WAIT); 1572 if (error) { 1573 dmu_tx_abort(replay_tx); 1574 break; 1575 } 1576 1577 replay_txg = dmu_tx_get_txg(replay_tx); 1578 1579 if (txtype == 0 || txtype >= TX_MAX_TYPE) { 1580 error = EINVAL; 1581 } else { 1582 /* 1583 * On the first pass, arrange for the replay vector 1584 * to fail its dmu_tx_assign(). That's the only way 1585 * to ensure that those code paths remain well tested. 1586 * 1587 * Only byteswap (if needed) on the 1st pass. 1588 */ 1589 *zr->zr_txgp = replay_txg - (pass == 1); 1590 error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lrbuf, 1591 zr->zr_byteswap && pass == 1); 1592 *zr->zr_txgp = TXG_NOWAIT; 1593 } 1594 1595 if (error == 0) { 1596 dsl_dataset_dirty(dmu_objset_ds(zr->zr_os), replay_tx); 1597 zilog->zl_replay_seq[replay_txg & TXG_MASK] = 1598 lr->lrc_seq; 1599 } 1600 1601 dmu_tx_commit(replay_tx); 1602 1603 if (!error) 1604 return; 1605 1606 /* 1607 * The DMU's dnode layer doesn't see removes until the txg 1608 * commits, so a subsequent claim can spuriously fail with 1609 * EEXIST. So if we receive any error other than ERESTART 1610 * we try syncing out any removes then retrying the 1611 * transaction. 1612 */ 1613 if (error != ERESTART && !sunk) { 1614 if (zr->zr_replay_cleaner) 1615 zr->zr_replay_cleaner(zr->zr_arg); 1616 txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0); 1617 sunk = B_TRUE; 1618 continue; /* retry */ 1619 } 1620 1621 if (error != ERESTART) 1622 break; 1623 1624 if (pass != 1) 1625 txg_wait_open(spa_get_dsl(zilog->zl_spa), 1626 replay_txg + 1); 1627 1628 dprintf("pass %d, retrying\n", pass); 1629 } 1630 1631bad: 1632 ASSERT(error && error != ERESTART); 1633 name = kmem_alloc(MAXNAMELEN, KM_SLEEP); 1634 dmu_objset_name(zr->zr_os, name); 1635 cmn_err(CE_WARN, "ZFS replay transaction error %d, " 1636 "dataset %s, seq 0x%llx, txtype %llu %s\n", 1637 error, name, (u_longlong_t)lr->lrc_seq, (u_longlong_t)txtype, 1638 (lr->lrc_txtype & TX_CI) ? "CI" : ""); 1639 zilog->zl_stop_replay = 1; 1640 kmem_free(name, MAXNAMELEN); 1641} 1642 1643/* ARGSUSED */ 1644static void 1645zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg) 1646{ 1647 zilog->zl_replay_blks++; 1648} 1649 1650/* 1651 * If this dataset has a non-empty intent log, replay it and destroy it. 1652 */ 1653void 1654zil_replay(objset_t *os, void *arg, uint64_t *txgp, 1655 zil_replay_func_t *replay_func[TX_MAX_TYPE], 1656 zil_replay_cleaner_t *replay_cleaner) 1657{ 1658 zilog_t *zilog = dmu_objset_zil(os); 1659 const zil_header_t *zh = zilog->zl_header; 1660 zil_replay_arg_t zr; 1661 1662 if ((zh->zh_flags & ZIL_REPLAY_NEEDED) == 0) { 1663 zil_destroy(zilog, B_TRUE); 1664 return; 1665 } 1666 //printf("ZFS: Replaying ZIL on %s...\n", os->os->os_spa->spa_name); 1667 1668 zr.zr_os = os; 1669 zr.zr_replay = replay_func; 1670 zr.zr_replay_cleaner = replay_cleaner; 1671 zr.zr_arg = arg; 1672 zr.zr_txgp = txgp; 1673 zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log); 1674 zr.zr_lrbuf = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP); 1675 1676 /* 1677 * Wait for in-progress removes to sync before starting replay. 1678 */ 1679 txg_wait_synced(zilog->zl_dmu_pool, 0); 1680 1681 zilog->zl_stop_replay = 0; 1682 zilog->zl_replay_time = LBOLT; 1683 ASSERT(zilog->zl_replay_blks == 0); 1684 (void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr, 1685 zh->zh_claim_txg); 1686 kmem_free(zr.zr_lrbuf, 2 * SPA_MAXBLOCKSIZE); 1687 1688 zil_destroy(zilog, B_FALSE); 1689 txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 1690 //printf("ZFS: Replay of ZIL on %s finished.\n", os->os->os_spa->spa_name); 1691} 1692 1693/* 1694 * Report whether all transactions are committed 1695 */ 1696int 1697zil_is_committed(zilog_t *zilog) 1698{ 1699 lwb_t *lwb; 1700 int ret; 1701 1702 mutex_enter(&zilog->zl_lock); 1703 while (zilog->zl_writer) 1704 cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 1705 1706 /* recent unpushed intent log transactions? */ 1707 if (!list_is_empty(&zilog->zl_itx_list)) { 1708 ret = B_FALSE; 1709 goto out; 1710 } 1711 1712 /* intent log never used? */ 1713 lwb = list_head(&zilog->zl_lwb_list); 1714 if (lwb == NULL) { 1715 ret = B_TRUE; 1716 goto out; 1717 } 1718 1719 /* 1720 * more than 1 log buffer means zil_sync() hasn't yet freed 1721 * entries after a txg has committed 1722 */ 1723 if (list_next(&zilog->zl_lwb_list, lwb)) { 1724 ret = B_FALSE; 1725 goto out; 1726 } 1727 1728 ASSERT(zil_empty(zilog)); 1729 ret = B_TRUE; 1730out: 1731 cv_broadcast(&zilog->zl_cv_writer); 1732 mutex_exit(&zilog->zl_lock); 1733 return (ret); 1734} 1735