zil.c revision 328235
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 (c) 2011, 2017 by Delphix. All rights reserved. 24 * Copyright (c) 2014 Integros [integros.com] 25 */ 26 27/* Portions Copyright 2010 Robert Milkowski */ 28 29#include <sys/zfs_context.h> 30#include <sys/spa.h> 31#include <sys/dmu.h> 32#include <sys/zap.h> 33#include <sys/arc.h> 34#include <sys/stat.h> 35#include <sys/resource.h> 36#include <sys/zil.h> 37#include <sys/zil_impl.h> 38#include <sys/dsl_dataset.h> 39#include <sys/vdev_impl.h> 40#include <sys/dmu_tx.h> 41#include <sys/dsl_pool.h> 42#include <sys/abd.h> 43 44/* 45 * The ZFS Intent Log (ZIL) saves "transaction records" (itxs) of system 46 * calls that change the file system. Each itx has enough information to 47 * be able to replay them after a system crash, power loss, or 48 * equivalent failure mode. These are stored in memory until either: 49 * 50 * 1. they are committed to the pool by the DMU transaction group 51 * (txg), at which point they can be discarded; or 52 * 2. they are committed to the on-disk ZIL for the dataset being 53 * modified (e.g. due to an fsync, O_DSYNC, or other synchronous 54 * requirement). 55 * 56 * In the event of a crash or power loss, the itxs contained by each 57 * dataset's on-disk ZIL will be replayed when that dataset is first 58 * instantianted (e.g. if the dataset is a normal fileystem, when it is 59 * first mounted). 60 * 61 * As hinted at above, there is one ZIL per dataset (both the in-memory 62 * representation, and the on-disk representation). The on-disk format 63 * consists of 3 parts: 64 * 65 * - a single, per-dataset, ZIL header; which points to a chain of 66 * - zero or more ZIL blocks; each of which contains 67 * - zero or more ZIL records 68 * 69 * A ZIL record holds the information necessary to replay a single 70 * system call transaction. A ZIL block can hold many ZIL records, and 71 * the blocks are chained together, similarly to a singly linked list. 72 * 73 * Each ZIL block contains a block pointer (blkptr_t) to the next ZIL 74 * block in the chain, and the ZIL header points to the first block in 75 * the chain. 76 * 77 * Note, there is not a fixed place in the pool to hold these ZIL 78 * blocks; they are dynamically allocated and freed as needed from the 79 * blocks available on the pool, though they can be preferentially 80 * allocated from a dedicated "log" vdev. 81 */ 82 83/* 84 * This controls the amount of time that a ZIL block (lwb) will remain 85 * "open" when it isn't "full", and it has a thread waiting for it to be 86 * committed to stable storage. Please refer to the zil_commit_waiter() 87 * function (and the comments within it) for more details. 88 */ 89int zfs_commit_timeout_pct = 5; 90 91/* 92 * Disable intent logging replay. This global ZIL switch affects all pools. 93 */ 94int zil_replay_disable = 0; 95SYSCTL_DECL(_vfs_zfs); 96SYSCTL_INT(_vfs_zfs, OID_AUTO, zil_replay_disable, CTLFLAG_RWTUN, 97 &zil_replay_disable, 0, "Disable intent logging replay"); 98 99/* 100 * Tunable parameter for debugging or performance analysis. Setting 101 * zfs_nocacheflush will cause corruption on power loss if a volatile 102 * out-of-order write cache is enabled. 103 */ 104boolean_t zfs_nocacheflush = B_FALSE; 105SYSCTL_INT(_vfs_zfs, OID_AUTO, cache_flush_disable, CTLFLAG_RDTUN, 106 &zfs_nocacheflush, 0, "Disable cache flush"); 107boolean_t zfs_trim_enabled = B_TRUE; 108SYSCTL_DECL(_vfs_zfs_trim); 109SYSCTL_INT(_vfs_zfs_trim, OID_AUTO, enabled, CTLFLAG_RDTUN, &zfs_trim_enabled, 0, 110 "Enable ZFS TRIM"); 111 112/* 113 * Limit SLOG write size per commit executed with synchronous priority. 114 * Any writes above that will be executed with lower (asynchronous) priority 115 * to limit potential SLOG device abuse by single active ZIL writer. 116 */ 117uint64_t zil_slog_bulk = 768 * 1024; 118SYSCTL_QUAD(_vfs_zfs, OID_AUTO, zil_slog_bulk, CTLFLAG_RWTUN, 119 &zil_slog_bulk, 0, "Maximal SLOG commit size with sync priority"); 120 121static kmem_cache_t *zil_lwb_cache; 122static kmem_cache_t *zil_zcw_cache; 123 124#define LWB_EMPTY(lwb) ((BP_GET_LSIZE(&lwb->lwb_blk) - \ 125 sizeof (zil_chain_t)) == (lwb->lwb_sz - lwb->lwb_nused)) 126 127static int 128zil_bp_compare(const void *x1, const void *x2) 129{ 130 const dva_t *dva1 = &((zil_bp_node_t *)x1)->zn_dva; 131 const dva_t *dva2 = &((zil_bp_node_t *)x2)->zn_dva; 132 133 if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2)) 134 return (-1); 135 if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2)) 136 return (1); 137 138 if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2)) 139 return (-1); 140 if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2)) 141 return (1); 142 143 return (0); 144} 145 146static void 147zil_bp_tree_init(zilog_t *zilog) 148{ 149 avl_create(&zilog->zl_bp_tree, zil_bp_compare, 150 sizeof (zil_bp_node_t), offsetof(zil_bp_node_t, zn_node)); 151} 152 153static void 154zil_bp_tree_fini(zilog_t *zilog) 155{ 156 avl_tree_t *t = &zilog->zl_bp_tree; 157 zil_bp_node_t *zn; 158 void *cookie = NULL; 159 160 while ((zn = avl_destroy_nodes(t, &cookie)) != NULL) 161 kmem_free(zn, sizeof (zil_bp_node_t)); 162 163 avl_destroy(t); 164} 165 166int 167zil_bp_tree_add(zilog_t *zilog, const blkptr_t *bp) 168{ 169 avl_tree_t *t = &zilog->zl_bp_tree; 170 const dva_t *dva; 171 zil_bp_node_t *zn; 172 avl_index_t where; 173 174 if (BP_IS_EMBEDDED(bp)) 175 return (0); 176 177 dva = BP_IDENTITY(bp); 178 179 if (avl_find(t, dva, &where) != NULL) 180 return (SET_ERROR(EEXIST)); 181 182 zn = kmem_alloc(sizeof (zil_bp_node_t), KM_SLEEP); 183 zn->zn_dva = *dva; 184 avl_insert(t, zn, where); 185 186 return (0); 187} 188 189static zil_header_t * 190zil_header_in_syncing_context(zilog_t *zilog) 191{ 192 return ((zil_header_t *)zilog->zl_header); 193} 194 195static void 196zil_init_log_chain(zilog_t *zilog, blkptr_t *bp) 197{ 198 zio_cksum_t *zc = &bp->blk_cksum; 199 200 zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL); 201 zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL); 202 zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os); 203 zc->zc_word[ZIL_ZC_SEQ] = 1ULL; 204} 205 206/* 207 * Read a log block and make sure it's valid. 208 */ 209static int 210zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, blkptr_t *nbp, void *dst, 211 char **end) 212{ 213 enum zio_flag zio_flags = ZIO_FLAG_CANFAIL; 214 arc_flags_t aflags = ARC_FLAG_WAIT; 215 arc_buf_t *abuf = NULL; 216 zbookmark_phys_t zb; 217 int error; 218 219 if (zilog->zl_header->zh_claim_txg == 0) 220 zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB; 221 222 if (!(zilog->zl_header->zh_flags & ZIL_CLAIM_LR_SEQ_VALID)) 223 zio_flags |= ZIO_FLAG_SPECULATIVE; 224 225 SET_BOOKMARK(&zb, bp->blk_cksum.zc_word[ZIL_ZC_OBJSET], 226 ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]); 227 228 error = arc_read(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf, 229 ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb); 230 231 if (error == 0) { 232 zio_cksum_t cksum = bp->blk_cksum; 233 234 /* 235 * Validate the checksummed log block. 236 * 237 * Sequence numbers should be... sequential. The checksum 238 * verifier for the next block should be bp's checksum plus 1. 239 * 240 * Also check the log chain linkage and size used. 241 */ 242 cksum.zc_word[ZIL_ZC_SEQ]++; 243 244 if (BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_ZILOG2) { 245 zil_chain_t *zilc = abuf->b_data; 246 char *lr = (char *)(zilc + 1); 247 uint64_t len = zilc->zc_nused - sizeof (zil_chain_t); 248 249 if (bcmp(&cksum, &zilc->zc_next_blk.blk_cksum, 250 sizeof (cksum)) || BP_IS_HOLE(&zilc->zc_next_blk)) { 251 error = SET_ERROR(ECKSUM); 252 } else { 253 ASSERT3U(len, <=, SPA_OLD_MAXBLOCKSIZE); 254 bcopy(lr, dst, len); 255 *end = (char *)dst + len; 256 *nbp = zilc->zc_next_blk; 257 } 258 } else { 259 char *lr = abuf->b_data; 260 uint64_t size = BP_GET_LSIZE(bp); 261 zil_chain_t *zilc = (zil_chain_t *)(lr + size) - 1; 262 263 if (bcmp(&cksum, &zilc->zc_next_blk.blk_cksum, 264 sizeof (cksum)) || BP_IS_HOLE(&zilc->zc_next_blk) || 265 (zilc->zc_nused > (size - sizeof (*zilc)))) { 266 error = SET_ERROR(ECKSUM); 267 } else { 268 ASSERT3U(zilc->zc_nused, <=, 269 SPA_OLD_MAXBLOCKSIZE); 270 bcopy(lr, dst, zilc->zc_nused); 271 *end = (char *)dst + zilc->zc_nused; 272 *nbp = zilc->zc_next_blk; 273 } 274 } 275 276 arc_buf_destroy(abuf, &abuf); 277 } 278 279 return (error); 280} 281 282/* 283 * Read a TX_WRITE log data block. 284 */ 285static int 286zil_read_log_data(zilog_t *zilog, const lr_write_t *lr, void *wbuf) 287{ 288 enum zio_flag zio_flags = ZIO_FLAG_CANFAIL; 289 const blkptr_t *bp = &lr->lr_blkptr; 290 arc_flags_t aflags = ARC_FLAG_WAIT; 291 arc_buf_t *abuf = NULL; 292 zbookmark_phys_t zb; 293 int error; 294 295 if (BP_IS_HOLE(bp)) { 296 if (wbuf != NULL) 297 bzero(wbuf, MAX(BP_GET_LSIZE(bp), lr->lr_length)); 298 return (0); 299 } 300 301 if (zilog->zl_header->zh_claim_txg == 0) 302 zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB; 303 304 SET_BOOKMARK(&zb, dmu_objset_id(zilog->zl_os), lr->lr_foid, 305 ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp)); 306 307 error = arc_read(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf, 308 ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb); 309 310 if (error == 0) { 311 if (wbuf != NULL) 312 bcopy(abuf->b_data, wbuf, arc_buf_size(abuf)); 313 arc_buf_destroy(abuf, &abuf); 314 } 315 316 return (error); 317} 318 319/* 320 * Parse the intent log, and call parse_func for each valid record within. 321 */ 322int 323zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func, 324 zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg) 325{ 326 const zil_header_t *zh = zilog->zl_header; 327 boolean_t claimed = !!zh->zh_claim_txg; 328 uint64_t claim_blk_seq = claimed ? zh->zh_claim_blk_seq : UINT64_MAX; 329 uint64_t claim_lr_seq = claimed ? zh->zh_claim_lr_seq : UINT64_MAX; 330 uint64_t max_blk_seq = 0; 331 uint64_t max_lr_seq = 0; 332 uint64_t blk_count = 0; 333 uint64_t lr_count = 0; 334 blkptr_t blk, next_blk; 335 char *lrbuf, *lrp; 336 int error = 0; 337 338 /* 339 * Old logs didn't record the maximum zh_claim_lr_seq. 340 */ 341 if (!(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID)) 342 claim_lr_seq = UINT64_MAX; 343 344 /* 345 * Starting at the block pointed to by zh_log we read the log chain. 346 * For each block in the chain we strongly check that block to 347 * ensure its validity. We stop when an invalid block is found. 348 * For each block pointer in the chain we call parse_blk_func(). 349 * For each record in each valid block we call parse_lr_func(). 350 * If the log has been claimed, stop if we encounter a sequence 351 * number greater than the highest claimed sequence number. 352 */ 353 lrbuf = zio_buf_alloc(SPA_OLD_MAXBLOCKSIZE); 354 zil_bp_tree_init(zilog); 355 356 for (blk = zh->zh_log; !BP_IS_HOLE(&blk); blk = next_blk) { 357 uint64_t blk_seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 358 int reclen; 359 char *end; 360 361 if (blk_seq > claim_blk_seq) 362 break; 363 if ((error = parse_blk_func(zilog, &blk, arg, txg)) != 0) 364 break; 365 ASSERT3U(max_blk_seq, <, blk_seq); 366 max_blk_seq = blk_seq; 367 blk_count++; 368 369 if (max_lr_seq == claim_lr_seq && max_blk_seq == claim_blk_seq) 370 break; 371 372 error = zil_read_log_block(zilog, &blk, &next_blk, lrbuf, &end); 373 if (error != 0) 374 break; 375 376 for (lrp = lrbuf; lrp < end; lrp += reclen) { 377 lr_t *lr = (lr_t *)lrp; 378 reclen = lr->lrc_reclen; 379 ASSERT3U(reclen, >=, sizeof (lr_t)); 380 if (lr->lrc_seq > claim_lr_seq) 381 goto done; 382 if ((error = parse_lr_func(zilog, lr, arg, txg)) != 0) 383 goto done; 384 ASSERT3U(max_lr_seq, <, lr->lrc_seq); 385 max_lr_seq = lr->lrc_seq; 386 lr_count++; 387 } 388 } 389done: 390 zilog->zl_parse_error = error; 391 zilog->zl_parse_blk_seq = max_blk_seq; 392 zilog->zl_parse_lr_seq = max_lr_seq; 393 zilog->zl_parse_blk_count = blk_count; 394 zilog->zl_parse_lr_count = lr_count; 395 396 ASSERT(!claimed || !(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID) || 397 (max_blk_seq == claim_blk_seq && max_lr_seq == claim_lr_seq)); 398 399 zil_bp_tree_fini(zilog); 400 zio_buf_free(lrbuf, SPA_OLD_MAXBLOCKSIZE); 401 402 return (error); 403} 404 405static int 406zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg) 407{ 408 /* 409 * Claim log block if not already committed and not already claimed. 410 * If tx == NULL, just verify that the block is claimable. 411 */ 412 if (BP_IS_HOLE(bp) || bp->blk_birth < first_txg || 413 zil_bp_tree_add(zilog, bp) != 0) 414 return (0); 415 416 return (zio_wait(zio_claim(NULL, zilog->zl_spa, 417 tx == NULL ? 0 : first_txg, bp, spa_claim_notify, NULL, 418 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB))); 419} 420 421static int 422zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg) 423{ 424 lr_write_t *lr = (lr_write_t *)lrc; 425 int error; 426 427 if (lrc->lrc_txtype != TX_WRITE) 428 return (0); 429 430 /* 431 * If the block is not readable, don't claim it. This can happen 432 * in normal operation when a log block is written to disk before 433 * some of the dmu_sync() blocks it points to. In this case, the 434 * transaction cannot have been committed to anyone (we would have 435 * waited for all writes to be stable first), so it is semantically 436 * correct to declare this the end of the log. 437 */ 438 if (lr->lr_blkptr.blk_birth >= first_txg && 439 (error = zil_read_log_data(zilog, lr, NULL)) != 0) 440 return (error); 441 return (zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg)); 442} 443 444/* ARGSUSED */ 445static int 446zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg) 447{ 448 zio_free_zil(zilog->zl_spa, dmu_tx_get_txg(tx), bp); 449 450 return (0); 451} 452 453static int 454zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg) 455{ 456 lr_write_t *lr = (lr_write_t *)lrc; 457 blkptr_t *bp = &lr->lr_blkptr; 458 459 /* 460 * If we previously claimed it, we need to free it. 461 */ 462 if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE && 463 bp->blk_birth >= claim_txg && zil_bp_tree_add(zilog, bp) == 0 && 464 !BP_IS_HOLE(bp)) 465 zio_free(zilog->zl_spa, dmu_tx_get_txg(tx), bp); 466 467 return (0); 468} 469 470static int 471zil_lwb_vdev_compare(const void *x1, const void *x2) 472{ 473 const uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev; 474 const uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev; 475 476 if (v1 < v2) 477 return (-1); 478 if (v1 > v2) 479 return (1); 480 481 return (0); 482} 483 484static lwb_t * 485zil_alloc_lwb(zilog_t *zilog, blkptr_t *bp, boolean_t slog, uint64_t txg) 486{ 487 lwb_t *lwb; 488 489 lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 490 lwb->lwb_zilog = zilog; 491 lwb->lwb_blk = *bp; 492 lwb->lwb_slog = slog; 493 lwb->lwb_state = LWB_STATE_CLOSED; 494 lwb->lwb_buf = zio_buf_alloc(BP_GET_LSIZE(bp)); 495 lwb->lwb_max_txg = txg; 496 lwb->lwb_write_zio = NULL; 497 lwb->lwb_root_zio = NULL; 498 lwb->lwb_tx = NULL; 499 lwb->lwb_issued_timestamp = 0; 500 if (BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_ZILOG2) { 501 lwb->lwb_nused = sizeof (zil_chain_t); 502 lwb->lwb_sz = BP_GET_LSIZE(bp); 503 } else { 504 lwb->lwb_nused = 0; 505 lwb->lwb_sz = BP_GET_LSIZE(bp) - sizeof (zil_chain_t); 506 } 507 508 mutex_enter(&zilog->zl_lock); 509 list_insert_tail(&zilog->zl_lwb_list, lwb); 510 mutex_exit(&zilog->zl_lock); 511 512 ASSERT(!MUTEX_HELD(&lwb->lwb_vdev_lock)); 513 ASSERT(avl_is_empty(&lwb->lwb_vdev_tree)); 514 ASSERT(list_is_empty(&lwb->lwb_waiters)); 515 516 return (lwb); 517} 518 519static void 520zil_free_lwb(zilog_t *zilog, lwb_t *lwb) 521{ 522 ASSERT(MUTEX_HELD(&zilog->zl_lock)); 523 ASSERT(!MUTEX_HELD(&lwb->lwb_vdev_lock)); 524 ASSERT(list_is_empty(&lwb->lwb_waiters)); 525 526 if (lwb->lwb_state == LWB_STATE_OPENED) { 527 avl_tree_t *t = &lwb->lwb_vdev_tree; 528 void *cookie = NULL; 529 zil_vdev_node_t *zv; 530 531 while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) 532 kmem_free(zv, sizeof (*zv)); 533 534 ASSERT3P(lwb->lwb_root_zio, !=, NULL); 535 ASSERT3P(lwb->lwb_write_zio, !=, NULL); 536 537 zio_cancel(lwb->lwb_root_zio); 538 zio_cancel(lwb->lwb_write_zio); 539 540 lwb->lwb_root_zio = NULL; 541 lwb->lwb_write_zio = NULL; 542 } else { 543 ASSERT3S(lwb->lwb_state, !=, LWB_STATE_ISSUED); 544 } 545 546 ASSERT(avl_is_empty(&lwb->lwb_vdev_tree)); 547 ASSERT3P(lwb->lwb_write_zio, ==, NULL); 548 ASSERT3P(lwb->lwb_root_zio, ==, NULL); 549 550 /* 551 * Clear the zilog's field to indicate this lwb is no longer 552 * valid, and prevent use-after-free errors. 553 */ 554 if (zilog->zl_last_lwb_opened == lwb) 555 zilog->zl_last_lwb_opened = NULL; 556 557 kmem_cache_free(zil_lwb_cache, lwb); 558} 559 560/* 561 * Called when we create in-memory log transactions so that we know 562 * to cleanup the itxs at the end of spa_sync(). 563 */ 564void 565zilog_dirty(zilog_t *zilog, uint64_t txg) 566{ 567 dsl_pool_t *dp = zilog->zl_dmu_pool; 568 dsl_dataset_t *ds = dmu_objset_ds(zilog->zl_os); 569 570 ASSERT(spa_writeable(zilog->zl_spa)); 571 572 if (ds->ds_is_snapshot) 573 panic("dirtying snapshot!"); 574 575 if (txg_list_add(&dp->dp_dirty_zilogs, zilog, txg)) { 576 /* up the hold count until we can be written out */ 577 dmu_buf_add_ref(ds->ds_dbuf, zilog); 578 579 zilog->zl_dirty_max_txg = MAX(txg, zilog->zl_dirty_max_txg); 580 } 581} 582 583/* 584 * Determine if the zil is dirty in the specified txg. Callers wanting to 585 * ensure that the dirty state does not change must hold the itxg_lock for 586 * the specified txg. Holding the lock will ensure that the zil cannot be 587 * dirtied (zil_itx_assign) or cleaned (zil_clean) while we check its current 588 * state. 589 */ 590boolean_t 591zilog_is_dirty_in_txg(zilog_t *zilog, uint64_t txg) 592{ 593 dsl_pool_t *dp = zilog->zl_dmu_pool; 594 595 if (txg_list_member(&dp->dp_dirty_zilogs, zilog, txg & TXG_MASK)) 596 return (B_TRUE); 597 return (B_FALSE); 598} 599 600/* 601 * Determine if the zil is dirty. The zil is considered dirty if it has 602 * any pending itx records that have not been cleaned by zil_clean(). 603 */ 604boolean_t 605zilog_is_dirty(zilog_t *zilog) 606{ 607 dsl_pool_t *dp = zilog->zl_dmu_pool; 608 609 for (int t = 0; t < TXG_SIZE; t++) { 610 if (txg_list_member(&dp->dp_dirty_zilogs, zilog, t)) 611 return (B_TRUE); 612 } 613 return (B_FALSE); 614} 615 616/* 617 * Create an on-disk intent log. 618 */ 619static lwb_t * 620zil_create(zilog_t *zilog) 621{ 622 const zil_header_t *zh = zilog->zl_header; 623 lwb_t *lwb = NULL; 624 uint64_t txg = 0; 625 dmu_tx_t *tx = NULL; 626 blkptr_t blk; 627 int error = 0; 628 boolean_t slog = FALSE; 629 630 /* 631 * Wait for any previous destroy to complete. 632 */ 633 txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 634 635 ASSERT(zh->zh_claim_txg == 0); 636 ASSERT(zh->zh_replay_seq == 0); 637 638 blk = zh->zh_log; 639 640 /* 641 * Allocate an initial log block if: 642 * - there isn't one already 643 * - the existing block is the wrong endianess 644 */ 645 if (BP_IS_HOLE(&blk) || BP_SHOULD_BYTESWAP(&blk)) { 646 tx = dmu_tx_create(zilog->zl_os); 647 VERIFY0(dmu_tx_assign(tx, TXG_WAIT)); 648 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 649 txg = dmu_tx_get_txg(tx); 650 651 if (!BP_IS_HOLE(&blk)) { 652 zio_free_zil(zilog->zl_spa, txg, &blk); 653 BP_ZERO(&blk); 654 } 655 656 error = zio_alloc_zil(zilog->zl_spa, txg, &blk, NULL, 657 ZIL_MIN_BLKSZ, &slog); 658 659 if (error == 0) 660 zil_init_log_chain(zilog, &blk); 661 } 662 663 /* 664 * Allocate a log write block (lwb) for the first log block. 665 */ 666 if (error == 0) 667 lwb = zil_alloc_lwb(zilog, &blk, slog, txg); 668 669 /* 670 * If we just allocated the first log block, commit our transaction 671 * and wait for zil_sync() to stuff the block poiner into zh_log. 672 * (zh is part of the MOS, so we cannot modify it in open context.) 673 */ 674 if (tx != NULL) { 675 dmu_tx_commit(tx); 676 txg_wait_synced(zilog->zl_dmu_pool, txg); 677 } 678 679 ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0); 680 681 return (lwb); 682} 683 684/* 685 * In one tx, free all log blocks and clear the log header. If keep_first 686 * is set, then we're replaying a log with no content. We want to keep the 687 * first block, however, so that the first synchronous transaction doesn't 688 * require a txg_wait_synced() in zil_create(). We don't need to 689 * txg_wait_synced() here either when keep_first is set, because both 690 * zil_create() and zil_destroy() will wait for any in-progress destroys 691 * to complete. 692 */ 693void 694zil_destroy(zilog_t *zilog, boolean_t keep_first) 695{ 696 const zil_header_t *zh = zilog->zl_header; 697 lwb_t *lwb; 698 dmu_tx_t *tx; 699 uint64_t txg; 700 701 /* 702 * Wait for any previous destroy to complete. 703 */ 704 txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 705 706 zilog->zl_old_header = *zh; /* debugging aid */ 707 708 if (BP_IS_HOLE(&zh->zh_log)) 709 return; 710 711 tx = dmu_tx_create(zilog->zl_os); 712 VERIFY0(dmu_tx_assign(tx, TXG_WAIT)); 713 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 714 txg = dmu_tx_get_txg(tx); 715 716 mutex_enter(&zilog->zl_lock); 717 718 ASSERT3U(zilog->zl_destroy_txg, <, txg); 719 zilog->zl_destroy_txg = txg; 720 zilog->zl_keep_first = keep_first; 721 722 if (!list_is_empty(&zilog->zl_lwb_list)) { 723 ASSERT(zh->zh_claim_txg == 0); 724 VERIFY(!keep_first); 725 while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 726 list_remove(&zilog->zl_lwb_list, lwb); 727 if (lwb->lwb_buf != NULL) 728 zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 729 zio_free(zilog->zl_spa, txg, &lwb->lwb_blk); 730 zil_free_lwb(zilog, lwb); 731 } 732 } else if (!keep_first) { 733 zil_destroy_sync(zilog, tx); 734 } 735 mutex_exit(&zilog->zl_lock); 736 737 dmu_tx_commit(tx); 738} 739 740void 741zil_destroy_sync(zilog_t *zilog, dmu_tx_t *tx) 742{ 743 ASSERT(list_is_empty(&zilog->zl_lwb_list)); 744 (void) zil_parse(zilog, zil_free_log_block, 745 zil_free_log_record, tx, zilog->zl_header->zh_claim_txg); 746} 747 748int 749zil_claim(dsl_pool_t *dp, dsl_dataset_t *ds, void *txarg) 750{ 751 dmu_tx_t *tx = txarg; 752 uint64_t first_txg = dmu_tx_get_txg(tx); 753 zilog_t *zilog; 754 zil_header_t *zh; 755 objset_t *os; 756 int error; 757 758 error = dmu_objset_own_obj(dp, ds->ds_object, 759 DMU_OST_ANY, B_FALSE, FTAG, &os); 760 if (error != 0) { 761 /* 762 * EBUSY indicates that the objset is inconsistent, in which 763 * case it can not have a ZIL. 764 */ 765 if (error != EBUSY) { 766 cmn_err(CE_WARN, "can't open objset for %llu, error %u", 767 (unsigned long long)ds->ds_object, error); 768 } 769 return (0); 770 } 771 772 zilog = dmu_objset_zil(os); 773 zh = zil_header_in_syncing_context(zilog); 774 775 if (spa_get_log_state(zilog->zl_spa) == SPA_LOG_CLEAR) { 776 if (!BP_IS_HOLE(&zh->zh_log)) 777 zio_free_zil(zilog->zl_spa, first_txg, &zh->zh_log); 778 BP_ZERO(&zh->zh_log); 779 dsl_dataset_dirty(dmu_objset_ds(os), tx); 780 dmu_objset_disown(os, FTAG); 781 return (0); 782 } 783 784 /* 785 * Claim all log blocks if we haven't already done so, and remember 786 * the highest claimed sequence number. This ensures that if we can 787 * read only part of the log now (e.g. due to a missing device), 788 * but we can read the entire log later, we will not try to replay 789 * or destroy beyond the last block we successfully claimed. 790 */ 791 ASSERT3U(zh->zh_claim_txg, <=, first_txg); 792 if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) { 793 (void) zil_parse(zilog, zil_claim_log_block, 794 zil_claim_log_record, tx, first_txg); 795 zh->zh_claim_txg = first_txg; 796 zh->zh_claim_blk_seq = zilog->zl_parse_blk_seq; 797 zh->zh_claim_lr_seq = zilog->zl_parse_lr_seq; 798 if (zilog->zl_parse_lr_count || zilog->zl_parse_blk_count > 1) 799 zh->zh_flags |= ZIL_REPLAY_NEEDED; 800 zh->zh_flags |= ZIL_CLAIM_LR_SEQ_VALID; 801 dsl_dataset_dirty(dmu_objset_ds(os), tx); 802 } 803 804 ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1)); 805 dmu_objset_disown(os, FTAG); 806 return (0); 807} 808 809/* 810 * Check the log by walking the log chain. 811 * Checksum errors are ok as they indicate the end of the chain. 812 * Any other error (no device or read failure) returns an error. 813 */ 814/* ARGSUSED */ 815int 816zil_check_log_chain(dsl_pool_t *dp, dsl_dataset_t *ds, void *tx) 817{ 818 zilog_t *zilog; 819 objset_t *os; 820 blkptr_t *bp; 821 int error; 822 823 ASSERT(tx == NULL); 824 825 error = dmu_objset_from_ds(ds, &os); 826 if (error != 0) { 827 cmn_err(CE_WARN, "can't open objset %llu, error %d", 828 (unsigned long long)ds->ds_object, error); 829 return (0); 830 } 831 832 zilog = dmu_objset_zil(os); 833 bp = (blkptr_t *)&zilog->zl_header->zh_log; 834 835 /* 836 * Check the first block and determine if it's on a log device 837 * which may have been removed or faulted prior to loading this 838 * pool. If so, there's no point in checking the rest of the log 839 * as its content should have already been synced to the pool. 840 */ 841 if (!BP_IS_HOLE(bp)) { 842 vdev_t *vd; 843 boolean_t valid = B_TRUE; 844 845 spa_config_enter(os->os_spa, SCL_STATE, FTAG, RW_READER); 846 vd = vdev_lookup_top(os->os_spa, DVA_GET_VDEV(&bp->blk_dva[0])); 847 if (vd->vdev_islog && vdev_is_dead(vd)) 848 valid = vdev_log_state_valid(vd); 849 spa_config_exit(os->os_spa, SCL_STATE, FTAG); 850 851 if (!valid) 852 return (0); 853 } 854 855 /* 856 * Because tx == NULL, zil_claim_log_block() will not actually claim 857 * any blocks, but just determine whether it is possible to do so. 858 * In addition to checking the log chain, zil_claim_log_block() 859 * will invoke zio_claim() with a done func of spa_claim_notify(), 860 * which will update spa_max_claim_txg. See spa_load() for details. 861 */ 862 error = zil_parse(zilog, zil_claim_log_block, zil_claim_log_record, tx, 863 zilog->zl_header->zh_claim_txg ? -1ULL : spa_first_txg(os->os_spa)); 864 865 return ((error == ECKSUM || error == ENOENT) ? 0 : error); 866} 867 868/* 869 * When an itx is "skipped", this function is used to properly mark the 870 * waiter as "done, and signal any thread(s) waiting on it. An itx can 871 * be skipped (and not committed to an lwb) for a variety of reasons, 872 * one of them being that the itx was committed via spa_sync(), prior to 873 * it being committed to an lwb; this can happen if a thread calling 874 * zil_commit() is racing with spa_sync(). 875 */ 876static void 877zil_commit_waiter_skip(zil_commit_waiter_t *zcw) 878{ 879 mutex_enter(&zcw->zcw_lock); 880 ASSERT3B(zcw->zcw_done, ==, B_FALSE); 881 zcw->zcw_done = B_TRUE; 882 cv_broadcast(&zcw->zcw_cv); 883 mutex_exit(&zcw->zcw_lock); 884} 885 886/* 887 * This function is used when the given waiter is to be linked into an 888 * lwb's "lwb_waiter" list; i.e. when the itx is committed to the lwb. 889 * At this point, the waiter will no longer be referenced by the itx, 890 * and instead, will be referenced by the lwb. 891 */ 892static void 893zil_commit_waiter_link_lwb(zil_commit_waiter_t *zcw, lwb_t *lwb) 894{ 895 mutex_enter(&zcw->zcw_lock); 896 ASSERT(!list_link_active(&zcw->zcw_node)); 897 ASSERT3P(zcw->zcw_lwb, ==, NULL); 898 ASSERT3P(lwb, !=, NULL); 899 ASSERT(lwb->lwb_state == LWB_STATE_OPENED || 900 lwb->lwb_state == LWB_STATE_ISSUED); 901 902 list_insert_tail(&lwb->lwb_waiters, zcw); 903 zcw->zcw_lwb = lwb; 904 mutex_exit(&zcw->zcw_lock); 905} 906 907/* 908 * This function is used when zio_alloc_zil() fails to allocate a ZIL 909 * block, and the given waiter must be linked to the "nolwb waiters" 910 * list inside of zil_process_commit_list(). 911 */ 912static void 913zil_commit_waiter_link_nolwb(zil_commit_waiter_t *zcw, list_t *nolwb) 914{ 915 mutex_enter(&zcw->zcw_lock); 916 ASSERT(!list_link_active(&zcw->zcw_node)); 917 ASSERT3P(zcw->zcw_lwb, ==, NULL); 918 list_insert_tail(nolwb, zcw); 919 mutex_exit(&zcw->zcw_lock); 920} 921 922void 923zil_lwb_add_block(lwb_t *lwb, const blkptr_t *bp) 924{ 925 avl_tree_t *t = &lwb->lwb_vdev_tree; 926 avl_index_t where; 927 zil_vdev_node_t *zv, zvsearch; 928 int ndvas = BP_GET_NDVAS(bp); 929 int i; 930 931 if (zfs_nocacheflush) 932 return; 933 934 mutex_enter(&lwb->lwb_vdev_lock); 935 for (i = 0; i < ndvas; i++) { 936 zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]); 937 if (avl_find(t, &zvsearch, &where) == NULL) { 938 zv = kmem_alloc(sizeof (*zv), KM_SLEEP); 939 zv->zv_vdev = zvsearch.zv_vdev; 940 avl_insert(t, zv, where); 941 } 942 } 943 mutex_exit(&lwb->lwb_vdev_lock); 944} 945 946void 947zil_lwb_add_txg(lwb_t *lwb, uint64_t txg) 948{ 949 lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg); 950} 951 952/* 953 * This function is a called after all VDEVs associated with a given lwb 954 * write have completed their DKIOCFLUSHWRITECACHE command; or as soon 955 * as the lwb write completes, if "zfs_nocacheflush" is set. 956 * 957 * The intention is for this function to be called as soon as the 958 * contents of an lwb are considered "stable" on disk, and will survive 959 * any sudden loss of power. At this point, any threads waiting for the 960 * lwb to reach this state are signalled, and the "waiter" structures 961 * are marked "done". 962 */ 963static void 964zil_lwb_flush_vdevs_done(zio_t *zio) 965{ 966 lwb_t *lwb = zio->io_private; 967 zilog_t *zilog = lwb->lwb_zilog; 968 dmu_tx_t *tx = lwb->lwb_tx; 969 zil_commit_waiter_t *zcw; 970 971 spa_config_exit(zilog->zl_spa, SCL_STATE, lwb); 972 973 zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 974 975 mutex_enter(&zilog->zl_lock); 976 977 /* 978 * Ensure the lwb buffer pointer is cleared before releasing the 979 * txg. If we have had an allocation failure and the txg is 980 * waiting to sync then we want zil_sync() to remove the lwb so 981 * that it's not picked up as the next new one in 982 * zil_process_commit_list(). zil_sync() will only remove the 983 * lwb if lwb_buf is null. 984 */ 985 lwb->lwb_buf = NULL; 986 lwb->lwb_tx = NULL; 987 988 ASSERT3U(lwb->lwb_issued_timestamp, >, 0); 989 zilog->zl_last_lwb_latency = gethrtime() - lwb->lwb_issued_timestamp; 990 991 lwb->lwb_root_zio = NULL; 992 lwb->lwb_state = LWB_STATE_DONE; 993 994 if (zilog->zl_last_lwb_opened == lwb) { 995 /* 996 * Remember the highest committed log sequence number 997 * for ztest. We only update this value when all the log 998 * writes succeeded, because ztest wants to ASSERT that 999 * it got the whole log chain. 1000 */ 1001 zilog->zl_commit_lr_seq = zilog->zl_lr_seq; 1002 } 1003 1004 while ((zcw = list_head(&lwb->lwb_waiters)) != NULL) { 1005 mutex_enter(&zcw->zcw_lock); 1006 1007 ASSERT(list_link_active(&zcw->zcw_node)); 1008 list_remove(&lwb->lwb_waiters, zcw); 1009 1010 ASSERT3P(zcw->zcw_lwb, ==, lwb); 1011 zcw->zcw_lwb = NULL; 1012 1013 zcw->zcw_zio_error = zio->io_error; 1014 1015 ASSERT3B(zcw->zcw_done, ==, B_FALSE); 1016 zcw->zcw_done = B_TRUE; 1017 cv_broadcast(&zcw->zcw_cv); 1018 1019 mutex_exit(&zcw->zcw_lock); 1020 } 1021 1022 mutex_exit(&zilog->zl_lock); 1023 1024 /* 1025 * Now that we've written this log block, we have a stable pointer 1026 * to the next block in the chain, so it's OK to let the txg in 1027 * which we allocated the next block sync. 1028 */ 1029 dmu_tx_commit(tx); 1030} 1031 1032/* 1033 * This is called when an lwb write completes. This means, this specific 1034 * lwb was written to disk, and all dependent lwb have also been 1035 * written to disk. 1036 * 1037 * At this point, a DKIOCFLUSHWRITECACHE command hasn't been issued to 1038 * the VDEVs involved in writing out this specific lwb. The lwb will be 1039 * "done" once zil_lwb_flush_vdevs_done() is called, which occurs in the 1040 * zio completion callback for the lwb's root zio. 1041 */ 1042static void 1043zil_lwb_write_done(zio_t *zio) 1044{ 1045 lwb_t *lwb = zio->io_private; 1046 spa_t *spa = zio->io_spa; 1047 zilog_t *zilog = lwb->lwb_zilog; 1048 avl_tree_t *t = &lwb->lwb_vdev_tree; 1049 void *cookie = NULL; 1050 zil_vdev_node_t *zv; 1051 1052 ASSERT3S(spa_config_held(spa, SCL_STATE, RW_READER), !=, 0); 1053 1054 ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF); 1055 ASSERT(BP_GET_TYPE(zio->io_bp) == DMU_OT_INTENT_LOG); 1056 ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 1057 ASSERT(BP_GET_BYTEORDER(zio->io_bp) == ZFS_HOST_BYTEORDER); 1058 ASSERT(!BP_IS_GANG(zio->io_bp)); 1059 ASSERT(!BP_IS_HOLE(zio->io_bp)); 1060 ASSERT(BP_GET_FILL(zio->io_bp) == 0); 1061 1062 abd_put(zio->io_abd); 1063 1064 ASSERT3S(lwb->lwb_state, ==, LWB_STATE_ISSUED); 1065 1066 mutex_enter(&zilog->zl_lock); 1067 lwb->lwb_write_zio = NULL; 1068 mutex_exit(&zilog->zl_lock); 1069 1070 if (avl_numnodes(t) == 0) 1071 return; 1072 1073 /* 1074 * If there was an IO error, we're not going to call zio_flush() 1075 * on these vdevs, so we simply empty the tree and free the 1076 * nodes. We avoid calling zio_flush() since there isn't any 1077 * good reason for doing so, after the lwb block failed to be 1078 * written out. 1079 */ 1080 if (zio->io_error != 0) { 1081 while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) 1082 kmem_free(zv, sizeof (*zv)); 1083 return; 1084 } 1085 1086 while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) { 1087 vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev); 1088 if (vd != NULL) 1089 zio_flush(lwb->lwb_root_zio, vd); 1090 kmem_free(zv, sizeof (*zv)); 1091 } 1092} 1093 1094/* 1095 * This function's purpose is to "open" an lwb such that it is ready to 1096 * accept new itxs being committed to it. To do this, the lwb's zio 1097 * structures are created, and linked to the lwb. This function is 1098 * idempotent; if the passed in lwb has already been opened, this 1099 * function is essentially a no-op. 1100 */ 1101static void 1102zil_lwb_write_open(zilog_t *zilog, lwb_t *lwb) 1103{ 1104 zbookmark_phys_t zb; 1105 zio_priority_t prio; 1106 1107 ASSERT(MUTEX_HELD(&zilog->zl_writer_lock)); 1108 ASSERT3P(lwb, !=, NULL); 1109 EQUIV(lwb->lwb_root_zio == NULL, lwb->lwb_state == LWB_STATE_CLOSED); 1110 EQUIV(lwb->lwb_root_zio != NULL, lwb->lwb_state == LWB_STATE_OPENED); 1111 1112 SET_BOOKMARK(&zb, lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET], 1113 ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, 1114 lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ]); 1115 1116 if (lwb->lwb_root_zio == NULL) { 1117 abd_t *lwb_abd = abd_get_from_buf(lwb->lwb_buf, 1118 BP_GET_LSIZE(&lwb->lwb_blk)); 1119 1120 if (!lwb->lwb_slog || zilog->zl_cur_used <= zil_slog_bulk) 1121 prio = ZIO_PRIORITY_SYNC_WRITE; 1122 else 1123 prio = ZIO_PRIORITY_ASYNC_WRITE; 1124 1125 lwb->lwb_root_zio = zio_root(zilog->zl_spa, 1126 zil_lwb_flush_vdevs_done, lwb, ZIO_FLAG_CANFAIL); 1127 ASSERT3P(lwb->lwb_root_zio, !=, NULL); 1128 1129 lwb->lwb_write_zio = zio_rewrite(lwb->lwb_root_zio, 1130 zilog->zl_spa, 0, &lwb->lwb_blk, lwb_abd, 1131 BP_GET_LSIZE(&lwb->lwb_blk), zil_lwb_write_done, lwb, 1132 prio, ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE, &zb); 1133 ASSERT3P(lwb->lwb_write_zio, !=, NULL); 1134 1135 lwb->lwb_state = LWB_STATE_OPENED; 1136 1137 mutex_enter(&zilog->zl_lock); 1138 1139 /* 1140 * The zilog's "zl_last_lwb_opened" field is used to 1141 * build the lwb/zio dependency chain, which is used to 1142 * preserve the ordering of lwb completions that is 1143 * required by the semantics of the ZIL. Each new lwb 1144 * zio becomes a parent of the "previous" lwb zio, such 1145 * that the new lwb's zio cannot complete until the 1146 * "previous" lwb's zio completes. 1147 * 1148 * This is required by the semantics of zil_commit(); 1149 * the commit waiters attached to the lwbs will be woken 1150 * in the lwb zio's completion callback, so this zio 1151 * dependency graph ensures the waiters are woken in the 1152 * correct order (the same order the lwbs were created). 1153 */ 1154 lwb_t *last_lwb_opened = zilog->zl_last_lwb_opened; 1155 if (last_lwb_opened != NULL && 1156 last_lwb_opened->lwb_state != LWB_STATE_DONE) { 1157 ASSERT(last_lwb_opened->lwb_state == LWB_STATE_OPENED || 1158 last_lwb_opened->lwb_state == LWB_STATE_ISSUED); 1159 ASSERT3P(last_lwb_opened->lwb_root_zio, !=, NULL); 1160 zio_add_child(lwb->lwb_root_zio, 1161 last_lwb_opened->lwb_root_zio); 1162 } 1163 zilog->zl_last_lwb_opened = lwb; 1164 1165 mutex_exit(&zilog->zl_lock); 1166 } 1167 1168 ASSERT3P(lwb->lwb_root_zio, !=, NULL); 1169 ASSERT3P(lwb->lwb_write_zio, !=, NULL); 1170 ASSERT3S(lwb->lwb_state, ==, LWB_STATE_OPENED); 1171} 1172 1173/* 1174 * Define a limited set of intent log block sizes. 1175 * 1176 * These must be a multiple of 4KB. Note only the amount used (again 1177 * aligned to 4KB) actually gets written. However, we can't always just 1178 * allocate SPA_OLD_MAXBLOCKSIZE as the slog space could be exhausted. 1179 */ 1180uint64_t zil_block_buckets[] = { 1181 4096, /* non TX_WRITE */ 1182 8192+4096, /* data base */ 1183 32*1024 + 4096, /* NFS writes */ 1184 UINT64_MAX 1185}; 1186 1187/* 1188 * Start a log block write and advance to the next log block. 1189 * Calls are serialized. 1190 */ 1191static lwb_t * 1192zil_lwb_write_issue(zilog_t *zilog, lwb_t *lwb) 1193{ 1194 lwb_t *nlwb = NULL; 1195 zil_chain_t *zilc; 1196 spa_t *spa = zilog->zl_spa; 1197 blkptr_t *bp; 1198 dmu_tx_t *tx; 1199 uint64_t txg; 1200 uint64_t zil_blksz, wsz; 1201 int i, error; 1202 boolean_t slog; 1203 1204 ASSERT(MUTEX_HELD(&zilog->zl_writer_lock)); 1205 ASSERT3P(lwb->lwb_root_zio, !=, NULL); 1206 ASSERT3P(lwb->lwb_write_zio, !=, NULL); 1207 ASSERT3S(lwb->lwb_state, ==, LWB_STATE_OPENED); 1208 1209 if (BP_GET_CHECKSUM(&lwb->lwb_blk) == ZIO_CHECKSUM_ZILOG2) { 1210 zilc = (zil_chain_t *)lwb->lwb_buf; 1211 bp = &zilc->zc_next_blk; 1212 } else { 1213 zilc = (zil_chain_t *)(lwb->lwb_buf + lwb->lwb_sz); 1214 bp = &zilc->zc_next_blk; 1215 } 1216 1217 ASSERT(lwb->lwb_nused <= lwb->lwb_sz); 1218 1219 /* 1220 * Allocate the next block and save its address in this block 1221 * before writing it in order to establish the log chain. 1222 * Note that if the allocation of nlwb synced before we wrote 1223 * the block that points at it (lwb), we'd leak it if we crashed. 1224 * Therefore, we don't do dmu_tx_commit() until zil_lwb_write_done(). 1225 * We dirty the dataset to ensure that zil_sync() will be called 1226 * to clean up in the event of allocation failure or I/O failure. 1227 */ 1228 1229 tx = dmu_tx_create(zilog->zl_os); 1230 1231 /* 1232 * Since we are not going to create any new dirty data and we can even 1233 * help with clearing the existing dirty data, we should not be subject 1234 * to the dirty data based delays. 1235 * We (ab)use TXG_WAITED to bypass the delay mechanism. 1236 * One side effect from using TXG_WAITED is that dmu_tx_assign() can 1237 * fail if the pool is suspended. Those are dramatic circumstances, 1238 * so we return NULL to signal that the normal ZIL processing is not 1239 * possible and txg_wait_synced() should be used to ensure that the data 1240 * is on disk. 1241 */ 1242 error = dmu_tx_assign(tx, TXG_WAITED); 1243 if (error != 0) { 1244 ASSERT3S(error, ==, EIO); 1245 dmu_tx_abort(tx); 1246 return (NULL); 1247 } 1248 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 1249 txg = dmu_tx_get_txg(tx); 1250 1251 lwb->lwb_tx = tx; 1252 1253 /* 1254 * Log blocks are pre-allocated. Here we select the size of the next 1255 * block, based on size used in the last block. 1256 * - first find the smallest bucket that will fit the block from a 1257 * limited set of block sizes. This is because it's faster to write 1258 * blocks allocated from the same metaslab as they are adjacent or 1259 * close. 1260 * - next find the maximum from the new suggested size and an array of 1261 * previous sizes. This lessens a picket fence effect of wrongly 1262 * guesssing the size if we have a stream of say 2k, 64k, 2k, 64k 1263 * requests. 1264 * 1265 * Note we only write what is used, but we can't just allocate 1266 * the maximum block size because we can exhaust the available 1267 * pool log space. 1268 */ 1269 zil_blksz = zilog->zl_cur_used + sizeof (zil_chain_t); 1270 for (i = 0; zil_blksz > zil_block_buckets[i]; i++) 1271 continue; 1272 zil_blksz = zil_block_buckets[i]; 1273 if (zil_blksz == UINT64_MAX) 1274 zil_blksz = SPA_OLD_MAXBLOCKSIZE; 1275 zilog->zl_prev_blks[zilog->zl_prev_rotor] = zil_blksz; 1276 for (i = 0; i < ZIL_PREV_BLKS; i++) 1277 zil_blksz = MAX(zil_blksz, zilog->zl_prev_blks[i]); 1278 zilog->zl_prev_rotor = (zilog->zl_prev_rotor + 1) & (ZIL_PREV_BLKS - 1); 1279 1280 BP_ZERO(bp); 1281 1282 /* pass the old blkptr in order to spread log blocks across devs */ 1283 error = zio_alloc_zil(spa, txg, bp, &lwb->lwb_blk, zil_blksz, &slog); 1284 if (error == 0) { 1285 ASSERT3U(bp->blk_birth, ==, txg); 1286 bp->blk_cksum = lwb->lwb_blk.blk_cksum; 1287 bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++; 1288 1289 /* 1290 * Allocate a new log write block (lwb). 1291 */ 1292 nlwb = zil_alloc_lwb(zilog, bp, slog, txg); 1293 } 1294 1295 if (BP_GET_CHECKSUM(&lwb->lwb_blk) == ZIO_CHECKSUM_ZILOG2) { 1296 /* For Slim ZIL only write what is used. */ 1297 wsz = P2ROUNDUP_TYPED(lwb->lwb_nused, ZIL_MIN_BLKSZ, uint64_t); 1298 ASSERT3U(wsz, <=, lwb->lwb_sz); 1299 zio_shrink(lwb->lwb_write_zio, wsz); 1300 1301 } else { 1302 wsz = lwb->lwb_sz; 1303 } 1304 1305 zilc->zc_pad = 0; 1306 zilc->zc_nused = lwb->lwb_nused; 1307 zilc->zc_eck.zec_cksum = lwb->lwb_blk.blk_cksum; 1308 1309 /* 1310 * clear unused data for security 1311 */ 1312 bzero(lwb->lwb_buf + lwb->lwb_nused, wsz - lwb->lwb_nused); 1313 1314 spa_config_enter(zilog->zl_spa, SCL_STATE, lwb, RW_READER); 1315 1316 zil_lwb_add_block(lwb, &lwb->lwb_blk); 1317 lwb->lwb_issued_timestamp = gethrtime(); 1318 lwb->lwb_state = LWB_STATE_ISSUED; 1319 1320 zio_nowait(lwb->lwb_root_zio); 1321 zio_nowait(lwb->lwb_write_zio); 1322 1323 /* 1324 * If there was an allocation failure then nlwb will be null which 1325 * forces a txg_wait_synced(). 1326 */ 1327 return (nlwb); 1328} 1329 1330static lwb_t * 1331zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb) 1332{ 1333 lr_t *lrcb, *lrc; 1334 lr_write_t *lrwb, *lrw; 1335 char *lr_buf; 1336 uint64_t dlen, dnow, lwb_sp, reclen, txg; 1337 1338 ASSERT(MUTEX_HELD(&zilog->zl_writer_lock)); 1339 ASSERT3P(lwb, !=, NULL); 1340 ASSERT3P(lwb->lwb_buf, !=, NULL); 1341 1342 zil_lwb_write_open(zilog, lwb); 1343 1344 lrc = &itx->itx_lr; 1345 lrw = (lr_write_t *)lrc; 1346 1347 /* 1348 * A commit itx doesn't represent any on-disk state; instead 1349 * it's simply used as a place holder on the commit list, and 1350 * provides a mechanism for attaching a "commit waiter" onto the 1351 * correct lwb (such that the waiter can be signalled upon 1352 * completion of that lwb). Thus, we don't process this itx's 1353 * log record if it's a commit itx (these itx's don't have log 1354 * records), and instead link the itx's waiter onto the lwb's 1355 * list of waiters. 1356 * 1357 * For more details, see the comment above zil_commit(). 1358 */ 1359 if (lrc->lrc_txtype == TX_COMMIT) { 1360 zil_commit_waiter_link_lwb(itx->itx_private, lwb); 1361 itx->itx_private = NULL; 1362 return (lwb); 1363 } 1364 1365 if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY) { 1366 dlen = P2ROUNDUP_TYPED( 1367 lrw->lr_length, sizeof (uint64_t), uint64_t); 1368 } else { 1369 dlen = 0; 1370 } 1371 reclen = lrc->lrc_reclen; 1372 zilog->zl_cur_used += (reclen + dlen); 1373 txg = lrc->lrc_txg; 1374 1375 ASSERT3U(zilog->zl_cur_used, <, UINT64_MAX - (reclen + dlen)); 1376 1377cont: 1378 /* 1379 * If this record won't fit in the current log block, start a new one. 1380 * For WR_NEED_COPY optimize layout for minimal number of chunks. 1381 */ 1382 lwb_sp = lwb->lwb_sz - lwb->lwb_nused; 1383 if (reclen > lwb_sp || (reclen + dlen > lwb_sp && 1384 lwb_sp < ZIL_MAX_WASTE_SPACE && (dlen % ZIL_MAX_LOG_DATA == 0 || 1385 lwb_sp < reclen + dlen % ZIL_MAX_LOG_DATA))) { 1386 lwb = zil_lwb_write_issue(zilog, lwb); 1387 if (lwb == NULL) 1388 return (NULL); 1389 zil_lwb_write_open(zilog, lwb); 1390 ASSERT(LWB_EMPTY(lwb)); 1391 lwb_sp = lwb->lwb_sz - lwb->lwb_nused; 1392 ASSERT3U(reclen + MIN(dlen, sizeof (uint64_t)), <=, lwb_sp); 1393 } 1394 1395 dnow = MIN(dlen, lwb_sp - reclen); 1396 lr_buf = lwb->lwb_buf + lwb->lwb_nused; 1397 bcopy(lrc, lr_buf, reclen); 1398 lrcb = (lr_t *)lr_buf; /* Like lrc, but inside lwb. */ 1399 lrwb = (lr_write_t *)lrcb; /* Like lrw, but inside lwb. */ 1400 1401 /* 1402 * If it's a write, fetch the data or get its blkptr as appropriate. 1403 */ 1404 if (lrc->lrc_txtype == TX_WRITE) { 1405 if (txg > spa_freeze_txg(zilog->zl_spa)) 1406 txg_wait_synced(zilog->zl_dmu_pool, txg); 1407 if (itx->itx_wr_state != WR_COPIED) { 1408 char *dbuf; 1409 int error; 1410 1411 if (itx->itx_wr_state == WR_NEED_COPY) { 1412 dbuf = lr_buf + reclen; 1413 lrcb->lrc_reclen += dnow; 1414 if (lrwb->lr_length > dnow) 1415 lrwb->lr_length = dnow; 1416 lrw->lr_offset += dnow; 1417 lrw->lr_length -= dnow; 1418 } else { 1419 ASSERT(itx->itx_wr_state == WR_INDIRECT); 1420 dbuf = NULL; 1421 } 1422 1423 /* 1424 * We pass in the "lwb_write_zio" rather than 1425 * "lwb_root_zio" so that the "lwb_write_zio" 1426 * becomes the parent of any zio's created by 1427 * the "zl_get_data" callback. The vdevs are 1428 * flushed after the "lwb_write_zio" completes, 1429 * so we want to make sure that completion 1430 * callback waits for these additional zio's, 1431 * such that the vdevs used by those zio's will 1432 * be included in the lwb's vdev tree, and those 1433 * vdevs will be properly flushed. If we passed 1434 * in "lwb_root_zio" here, then these additional 1435 * vdevs may not be flushed; e.g. if these zio's 1436 * completed after "lwb_write_zio" completed. 1437 */ 1438 error = zilog->zl_get_data(itx->itx_private, 1439 lrwb, dbuf, lwb, lwb->lwb_write_zio); 1440 1441 if (error == EIO) { 1442 txg_wait_synced(zilog->zl_dmu_pool, txg); 1443 return (lwb); 1444 } 1445 if (error != 0) { 1446 ASSERT(error == ENOENT || error == EEXIST || 1447 error == EALREADY); 1448 return (lwb); 1449 } 1450 } 1451 } 1452 1453 /* 1454 * We're actually making an entry, so update lrc_seq to be the 1455 * log record sequence number. Note that this is generally not 1456 * equal to the itx sequence number because not all transactions 1457 * are synchronous, and sometimes spa_sync() gets there first. 1458 */ 1459 lrcb->lrc_seq = ++zilog->zl_lr_seq; 1460 lwb->lwb_nused += reclen + dnow; 1461 1462 zil_lwb_add_txg(lwb, txg); 1463 1464 ASSERT3U(lwb->lwb_nused, <=, lwb->lwb_sz); 1465 ASSERT0(P2PHASE(lwb->lwb_nused, sizeof (uint64_t))); 1466 1467 dlen -= dnow; 1468 if (dlen > 0) { 1469 zilog->zl_cur_used += reclen; 1470 goto cont; 1471 } 1472 1473 return (lwb); 1474} 1475 1476itx_t * 1477zil_itx_create(uint64_t txtype, size_t lrsize) 1478{ 1479 itx_t *itx; 1480 1481 lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t); 1482 1483 itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP); 1484 itx->itx_lr.lrc_txtype = txtype; 1485 itx->itx_lr.lrc_reclen = lrsize; 1486 itx->itx_lr.lrc_seq = 0; /* defensive */ 1487 itx->itx_sync = B_TRUE; /* default is synchronous */ 1488 1489 return (itx); 1490} 1491 1492void 1493zil_itx_destroy(itx_t *itx) 1494{ 1495 kmem_free(itx, offsetof(itx_t, itx_lr) + itx->itx_lr.lrc_reclen); 1496} 1497 1498/* 1499 * Free up the sync and async itxs. The itxs_t has already been detached 1500 * so no locks are needed. 1501 */ 1502static void 1503zil_itxg_clean(itxs_t *itxs) 1504{ 1505 itx_t *itx; 1506 list_t *list; 1507 avl_tree_t *t; 1508 void *cookie; 1509 itx_async_node_t *ian; 1510 1511 list = &itxs->i_sync_list; 1512 while ((itx = list_head(list)) != NULL) { 1513 /* 1514 * In the general case, commit itxs will not be found 1515 * here, as they'll be committed to an lwb via 1516 * zil_lwb_commit(), and free'd in that function. Having 1517 * said that, it is still possible for commit itxs to be 1518 * found here, due to the following race: 1519 * 1520 * - a thread calls zil_commit() which assigns the 1521 * commit itx to a per-txg i_sync_list 1522 * - zil_itxg_clean() is called (e.g. via spa_sync()) 1523 * while the waiter is still on the i_sync_list 1524 * 1525 * There's nothing to prevent syncing the txg while the 1526 * waiter is on the i_sync_list. This normally doesn't 1527 * happen because spa_sync() is slower than zil_commit(), 1528 * but if zil_commit() calls txg_wait_synced() (e.g. 1529 * because zil_create() or zil_commit_writer_stall() is 1530 * called) we will hit this case. 1531 */ 1532 if (itx->itx_lr.lrc_txtype == TX_COMMIT) 1533 zil_commit_waiter_skip(itx->itx_private); 1534 1535 list_remove(list, itx); 1536 zil_itx_destroy(itx); 1537 } 1538 1539 cookie = NULL; 1540 t = &itxs->i_async_tree; 1541 while ((ian = avl_destroy_nodes(t, &cookie)) != NULL) { 1542 list = &ian->ia_list; 1543 while ((itx = list_head(list)) != NULL) { 1544 list_remove(list, itx); 1545 /* commit itxs should never be on the async lists. */ 1546 ASSERT3U(itx->itx_lr.lrc_txtype, !=, TX_COMMIT); 1547 zil_itx_destroy(itx); 1548 } 1549 list_destroy(list); 1550 kmem_free(ian, sizeof (itx_async_node_t)); 1551 } 1552 avl_destroy(t); 1553 1554 kmem_free(itxs, sizeof (itxs_t)); 1555} 1556 1557static int 1558zil_aitx_compare(const void *x1, const void *x2) 1559{ 1560 const uint64_t o1 = ((itx_async_node_t *)x1)->ia_foid; 1561 const uint64_t o2 = ((itx_async_node_t *)x2)->ia_foid; 1562 1563 if (o1 < o2) 1564 return (-1); 1565 if (o1 > o2) 1566 return (1); 1567 1568 return (0); 1569} 1570 1571/* 1572 * Remove all async itx with the given oid. 1573 */ 1574static void 1575zil_remove_async(zilog_t *zilog, uint64_t oid) 1576{ 1577 uint64_t otxg, txg; 1578 itx_async_node_t *ian; 1579 avl_tree_t *t; 1580 avl_index_t where; 1581 list_t clean_list; 1582 itx_t *itx; 1583 1584 ASSERT(oid != 0); 1585 list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node)); 1586 1587 if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */ 1588 otxg = ZILTEST_TXG; 1589 else 1590 otxg = spa_last_synced_txg(zilog->zl_spa) + 1; 1591 1592 for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) { 1593 itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK]; 1594 1595 mutex_enter(&itxg->itxg_lock); 1596 if (itxg->itxg_txg != txg) { 1597 mutex_exit(&itxg->itxg_lock); 1598 continue; 1599 } 1600 1601 /* 1602 * Locate the object node and append its list. 1603 */ 1604 t = &itxg->itxg_itxs->i_async_tree; 1605 ian = avl_find(t, &oid, &where); 1606 if (ian != NULL) 1607 list_move_tail(&clean_list, &ian->ia_list); 1608 mutex_exit(&itxg->itxg_lock); 1609 } 1610 while ((itx = list_head(&clean_list)) != NULL) { 1611 list_remove(&clean_list, itx); 1612 /* commit itxs should never be on the async lists. */ 1613 ASSERT3U(itx->itx_lr.lrc_txtype, !=, TX_COMMIT); 1614 zil_itx_destroy(itx); 1615 } 1616 list_destroy(&clean_list); 1617} 1618 1619void 1620zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx) 1621{ 1622 uint64_t txg; 1623 itxg_t *itxg; 1624 itxs_t *itxs, *clean = NULL; 1625 1626 /* 1627 * Object ids can be re-instantiated in the next txg so 1628 * remove any async transactions to avoid future leaks. 1629 * This can happen if a fsync occurs on the re-instantiated 1630 * object for a WR_INDIRECT or WR_NEED_COPY write, which gets 1631 * the new file data and flushes a write record for the old object. 1632 */ 1633 if ((itx->itx_lr.lrc_txtype & ~TX_CI) == TX_REMOVE) 1634 zil_remove_async(zilog, itx->itx_oid); 1635 1636 /* 1637 * Ensure the data of a renamed file is committed before the rename. 1638 */ 1639 if ((itx->itx_lr.lrc_txtype & ~TX_CI) == TX_RENAME) 1640 zil_async_to_sync(zilog, itx->itx_oid); 1641 1642 if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) 1643 txg = ZILTEST_TXG; 1644 else 1645 txg = dmu_tx_get_txg(tx); 1646 1647 itxg = &zilog->zl_itxg[txg & TXG_MASK]; 1648 mutex_enter(&itxg->itxg_lock); 1649 itxs = itxg->itxg_itxs; 1650 if (itxg->itxg_txg != txg) { 1651 if (itxs != NULL) { 1652 /* 1653 * The zil_clean callback hasn't got around to cleaning 1654 * this itxg. Save the itxs for release below. 1655 * This should be rare. 1656 */ 1657 zfs_dbgmsg("zil_itx_assign: missed itx cleanup for " 1658 "txg %llu", itxg->itxg_txg); 1659 clean = itxg->itxg_itxs; 1660 } 1661 itxg->itxg_txg = txg; 1662 itxs = itxg->itxg_itxs = kmem_zalloc(sizeof (itxs_t), KM_SLEEP); 1663 1664 list_create(&itxs->i_sync_list, sizeof (itx_t), 1665 offsetof(itx_t, itx_node)); 1666 avl_create(&itxs->i_async_tree, zil_aitx_compare, 1667 sizeof (itx_async_node_t), 1668 offsetof(itx_async_node_t, ia_node)); 1669 } 1670 if (itx->itx_sync) { 1671 list_insert_tail(&itxs->i_sync_list, itx); 1672 } else { 1673 avl_tree_t *t = &itxs->i_async_tree; 1674 uint64_t foid = ((lr_ooo_t *)&itx->itx_lr)->lr_foid; 1675 itx_async_node_t *ian; 1676 avl_index_t where; 1677 1678 ian = avl_find(t, &foid, &where); 1679 if (ian == NULL) { 1680 ian = kmem_alloc(sizeof (itx_async_node_t), KM_SLEEP); 1681 list_create(&ian->ia_list, sizeof (itx_t), 1682 offsetof(itx_t, itx_node)); 1683 ian->ia_foid = foid; 1684 avl_insert(t, ian, where); 1685 } 1686 list_insert_tail(&ian->ia_list, itx); 1687 } 1688 1689 itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx); 1690 1691 /* 1692 * We don't want to dirty the ZIL using ZILTEST_TXG, because 1693 * zil_clean() will never be called using ZILTEST_TXG. Thus, we 1694 * need to be careful to always dirty the ZIL using the "real" 1695 * TXG (not itxg_txg) even when the SPA is frozen. 1696 */ 1697 zilog_dirty(zilog, dmu_tx_get_txg(tx)); 1698 mutex_exit(&itxg->itxg_lock); 1699 1700 /* Release the old itxs now we've dropped the lock */ 1701 if (clean != NULL) 1702 zil_itxg_clean(clean); 1703} 1704 1705/* 1706 * If there are any in-memory intent log transactions which have now been 1707 * synced then start up a taskq to free them. We should only do this after we 1708 * have written out the uberblocks (i.e. txg has been comitted) so that 1709 * don't inadvertently clean out in-memory log records that would be required 1710 * by zil_commit(). 1711 */ 1712void 1713zil_clean(zilog_t *zilog, uint64_t synced_txg) 1714{ 1715 itxg_t *itxg = &zilog->zl_itxg[synced_txg & TXG_MASK]; 1716 itxs_t *clean_me; 1717 1718 ASSERT3U(synced_txg, <, ZILTEST_TXG); 1719 1720 mutex_enter(&itxg->itxg_lock); 1721 if (itxg->itxg_itxs == NULL || itxg->itxg_txg == ZILTEST_TXG) { 1722 mutex_exit(&itxg->itxg_lock); 1723 return; 1724 } 1725 ASSERT3U(itxg->itxg_txg, <=, synced_txg); 1726 ASSERT3U(itxg->itxg_txg, !=, 0); 1727 clean_me = itxg->itxg_itxs; 1728 itxg->itxg_itxs = NULL; 1729 itxg->itxg_txg = 0; 1730 mutex_exit(&itxg->itxg_lock); 1731 /* 1732 * Preferably start a task queue to free up the old itxs but 1733 * if taskq_dispatch can't allocate resources to do that then 1734 * free it in-line. This should be rare. Note, using TQ_SLEEP 1735 * created a bad performance problem. 1736 */ 1737 ASSERT3P(zilog->zl_dmu_pool, !=, NULL); 1738 ASSERT3P(zilog->zl_dmu_pool->dp_zil_clean_taskq, !=, NULL); 1739 if (taskq_dispatch(zilog->zl_dmu_pool->dp_zil_clean_taskq, 1740 (void (*)(void *))zil_itxg_clean, clean_me, TQ_NOSLEEP) == 0) 1741 zil_itxg_clean(clean_me); 1742} 1743 1744/* 1745 * This function will traverse the queue of itxs that need to be 1746 * committed, and move them onto the ZIL's zl_itx_commit_list. 1747 */ 1748static void 1749zil_get_commit_list(zilog_t *zilog) 1750{ 1751 uint64_t otxg, txg; 1752 list_t *commit_list = &zilog->zl_itx_commit_list; 1753 1754 ASSERT(MUTEX_HELD(&zilog->zl_writer_lock)); 1755 1756 if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */ 1757 otxg = ZILTEST_TXG; 1758 else 1759 otxg = spa_last_synced_txg(zilog->zl_spa) + 1; 1760 1761 /* 1762 * This is inherently racy, since there is nothing to prevent 1763 * the last synced txg from changing. That's okay since we'll 1764 * only commit things in the future. 1765 */ 1766 for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) { 1767 itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK]; 1768 1769 mutex_enter(&itxg->itxg_lock); 1770 if (itxg->itxg_txg != txg) { 1771 mutex_exit(&itxg->itxg_lock); 1772 continue; 1773 } 1774 1775 /* 1776 * If we're adding itx records to the zl_itx_commit_list, 1777 * then the zil better be dirty in this "txg". We can assert 1778 * that here since we're holding the itxg_lock which will 1779 * prevent spa_sync from cleaning it. Once we add the itxs 1780 * to the zl_itx_commit_list we must commit it to disk even 1781 * if it's unnecessary (i.e. the txg was synced). 1782 */ 1783 ASSERT(zilog_is_dirty_in_txg(zilog, txg) || 1784 spa_freeze_txg(zilog->zl_spa) != UINT64_MAX); 1785 list_move_tail(commit_list, &itxg->itxg_itxs->i_sync_list); 1786 1787 mutex_exit(&itxg->itxg_lock); 1788 } 1789} 1790 1791/* 1792 * Move the async itxs for a specified object to commit into sync lists. 1793 */ 1794void 1795zil_async_to_sync(zilog_t *zilog, uint64_t foid) 1796{ 1797 uint64_t otxg, txg; 1798 itx_async_node_t *ian; 1799 avl_tree_t *t; 1800 avl_index_t where; 1801 1802 if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */ 1803 otxg = ZILTEST_TXG; 1804 else 1805 otxg = spa_last_synced_txg(zilog->zl_spa) + 1; 1806 1807 /* 1808 * This is inherently racy, since there is nothing to prevent 1809 * the last synced txg from changing. 1810 */ 1811 for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) { 1812 itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK]; 1813 1814 mutex_enter(&itxg->itxg_lock); 1815 if (itxg->itxg_txg != txg) { 1816 mutex_exit(&itxg->itxg_lock); 1817 continue; 1818 } 1819 1820 /* 1821 * If a foid is specified then find that node and append its 1822 * list. Otherwise walk the tree appending all the lists 1823 * to the sync list. We add to the end rather than the 1824 * beginning to ensure the create has happened. 1825 */ 1826 t = &itxg->itxg_itxs->i_async_tree; 1827 if (foid != 0) { 1828 ian = avl_find(t, &foid, &where); 1829 if (ian != NULL) { 1830 list_move_tail(&itxg->itxg_itxs->i_sync_list, 1831 &ian->ia_list); 1832 } 1833 } else { 1834 void *cookie = NULL; 1835 1836 while ((ian = avl_destroy_nodes(t, &cookie)) != NULL) { 1837 list_move_tail(&itxg->itxg_itxs->i_sync_list, 1838 &ian->ia_list); 1839 list_destroy(&ian->ia_list); 1840 kmem_free(ian, sizeof (itx_async_node_t)); 1841 } 1842 } 1843 mutex_exit(&itxg->itxg_lock); 1844 } 1845} 1846 1847/* 1848 * This function will prune commit itxs that are at the head of the 1849 * commit list (it won't prune past the first non-commit itx), and 1850 * either: a) attach them to the last lwb that's still pending 1851 * completion, or b) skip them altogether. 1852 * 1853 * This is used as a performance optimization to prevent commit itxs 1854 * from generating new lwbs when it's unnecessary to do so. 1855 */ 1856static void 1857zil_prune_commit_list(zilog_t *zilog) 1858{ 1859 itx_t *itx; 1860 1861 ASSERT(MUTEX_HELD(&zilog->zl_writer_lock)); 1862 1863 while (itx = list_head(&zilog->zl_itx_commit_list)) { 1864 lr_t *lrc = &itx->itx_lr; 1865 if (lrc->lrc_txtype != TX_COMMIT) 1866 break; 1867 1868 mutex_enter(&zilog->zl_lock); 1869 1870 lwb_t *last_lwb = zilog->zl_last_lwb_opened; 1871 if (last_lwb == NULL || last_lwb->lwb_state == LWB_STATE_DONE) { 1872 /* 1873 * All of the itxs this waiter was waiting on 1874 * must have already completed (or there were 1875 * never any itx's for it to wait on), so it's 1876 * safe to skip this waiter and mark it done. 1877 */ 1878 zil_commit_waiter_skip(itx->itx_private); 1879 } else { 1880 zil_commit_waiter_link_lwb(itx->itx_private, last_lwb); 1881 itx->itx_private = NULL; 1882 } 1883 1884 mutex_exit(&zilog->zl_lock); 1885 1886 list_remove(&zilog->zl_itx_commit_list, itx); 1887 zil_itx_destroy(itx); 1888 } 1889 1890 IMPLY(itx != NULL, itx->itx_lr.lrc_txtype != TX_COMMIT); 1891} 1892 1893static void 1894zil_commit_writer_stall(zilog_t *zilog) 1895{ 1896 /* 1897 * When zio_alloc_zil() fails to allocate the next lwb block on 1898 * disk, we must call txg_wait_synced() to ensure all of the 1899 * lwbs in the zilog's zl_lwb_list are synced and then freed (in 1900 * zil_sync()), such that any subsequent ZIL writer (i.e. a call 1901 * to zil_process_commit_list()) will have to call zil_create(), 1902 * and start a new ZIL chain. 1903 * 1904 * Since zil_alloc_zil() failed, the lwb that was previously 1905 * issued does not have a pointer to the "next" lwb on disk. 1906 * Thus, if another ZIL writer thread was to allocate the "next" 1907 * on-disk lwb, that block could be leaked in the event of a 1908 * crash (because the previous lwb on-disk would not point to 1909 * it). 1910 * 1911 * We must hold the zilog's zl_writer_lock while we do this, to 1912 * ensure no new threads enter zil_process_commit_list() until 1913 * all lwb's in the zl_lwb_list have been synced and freed 1914 * (which is achieved via the txg_wait_synced() call). 1915 */ 1916 ASSERT(MUTEX_HELD(&zilog->zl_writer_lock)); 1917 txg_wait_synced(zilog->zl_dmu_pool, 0); 1918 ASSERT3P(list_tail(&zilog->zl_lwb_list), ==, NULL); 1919} 1920 1921/* 1922 * This function will traverse the commit list, creating new lwbs as 1923 * needed, and committing the itxs from the commit list to these newly 1924 * created lwbs. Additionally, as a new lwb is created, the previous 1925 * lwb will be issued to the zio layer to be written to disk. 1926 */ 1927static void 1928zil_process_commit_list(zilog_t *zilog) 1929{ 1930 spa_t *spa = zilog->zl_spa; 1931 list_t nolwb_waiters; 1932 lwb_t *lwb; 1933 itx_t *itx; 1934 1935 ASSERT(MUTEX_HELD(&zilog->zl_writer_lock)); 1936 1937 /* 1938 * Return if there's nothing to commit before we dirty the fs by 1939 * calling zil_create(). 1940 */ 1941 if (list_head(&zilog->zl_itx_commit_list) == NULL) 1942 return; 1943 1944 list_create(&nolwb_waiters, sizeof (zil_commit_waiter_t), 1945 offsetof(zil_commit_waiter_t, zcw_node)); 1946 1947 lwb = list_tail(&zilog->zl_lwb_list); 1948 if (lwb == NULL) { 1949 lwb = zil_create(zilog); 1950 } else { 1951 ASSERT3S(lwb->lwb_state, !=, LWB_STATE_ISSUED); 1952 ASSERT3S(lwb->lwb_state, !=, LWB_STATE_DONE); 1953 } 1954 1955 while (itx = list_head(&zilog->zl_itx_commit_list)) { 1956 lr_t *lrc = &itx->itx_lr; 1957 uint64_t txg = lrc->lrc_txg; 1958 1959 ASSERT3U(txg, !=, 0); 1960 1961 if (lrc->lrc_txtype == TX_COMMIT) { 1962 DTRACE_PROBE2(zil__process__commit__itx, 1963 zilog_t *, zilog, itx_t *, itx); 1964 } else { 1965 DTRACE_PROBE2(zil__process__normal__itx, 1966 zilog_t *, zilog, itx_t *, itx); 1967 } 1968 1969 /* 1970 * This is inherently racy and may result in us writing 1971 * out a log block for a txg that was just synced. This 1972 * is ok since we'll end cleaning up that log block the 1973 * next time we call zil_sync(). 1974 */ 1975 boolean_t synced = txg <= spa_last_synced_txg(spa); 1976 boolean_t frozen = txg > spa_freeze_txg(spa); 1977 1978 if (!synced || frozen) { 1979 if (lwb != NULL) { 1980 lwb = zil_lwb_commit(zilog, itx, lwb); 1981 } else if (lrc->lrc_txtype == TX_COMMIT) { 1982 ASSERT3P(lwb, ==, NULL); 1983 zil_commit_waiter_link_nolwb( 1984 itx->itx_private, &nolwb_waiters); 1985 } 1986 } else if (lrc->lrc_txtype == TX_COMMIT) { 1987 ASSERT3B(synced, ==, B_TRUE); 1988 ASSERT3B(frozen, ==, B_FALSE); 1989 1990 /* 1991 * If this is a commit itx, then there will be a 1992 * thread that is either: already waiting for 1993 * it, or soon will be waiting. 1994 * 1995 * This itx has already been committed to disk 1996 * via spa_sync() so we don't bother committing 1997 * it to an lwb. As a result, we cannot use the 1998 * lwb zio callback to signal the waiter and 1999 * mark it as done, so we must do that here. 2000 */ 2001 zil_commit_waiter_skip(itx->itx_private); 2002 } 2003 2004 list_remove(&zilog->zl_itx_commit_list, itx); 2005 zil_itx_destroy(itx); 2006 } 2007 2008 if (lwb == NULL) { 2009 /* 2010 * This indicates zio_alloc_zil() failed to allocate the 2011 * "next" lwb on-disk. When this happens, we must stall 2012 * the ZIL write pipeline; see the comment within 2013 * zil_commit_writer_stall() for more details. 2014 */ 2015 zil_commit_writer_stall(zilog); 2016 2017 /* 2018 * Additionally, we have to signal and mark the "nolwb" 2019 * waiters as "done" here, since without an lwb, we 2020 * can't do this via zil_lwb_flush_vdevs_done() like 2021 * normal. 2022 */ 2023 zil_commit_waiter_t *zcw; 2024 while (zcw = list_head(&nolwb_waiters)) { 2025 zil_commit_waiter_skip(zcw); 2026 list_remove(&nolwb_waiters, zcw); 2027 } 2028 } else { 2029 ASSERT(list_is_empty(&nolwb_waiters)); 2030 ASSERT3P(lwb, !=, NULL); 2031 ASSERT3S(lwb->lwb_state, !=, LWB_STATE_ISSUED); 2032 ASSERT3S(lwb->lwb_state, !=, LWB_STATE_DONE); 2033 2034 /* 2035 * At this point, the ZIL block pointed at by the "lwb" 2036 * variable is in one of the following states: "closed" 2037 * or "open". 2038 * 2039 * If its "closed", then no itxs have been committed to 2040 * it, so there's no point in issuing its zio (i.e. 2041 * it's "empty"). 2042 * 2043 * If its "open" state, then it contains one or more 2044 * itxs that eventually need to be committed to stable 2045 * storage. In this case we intentionally do not issue 2046 * the lwb's zio to disk yet, and instead rely on one of 2047 * the following two mechanisms for issuing the zio: 2048 * 2049 * 1. Ideally, there will be more ZIL activity occuring 2050 * on the system, such that this function will be 2051 * immediately called again (not necessarily by the same 2052 * thread) and this lwb's zio will be issued via 2053 * zil_lwb_commit(). This way, the lwb is guaranteed to 2054 * be "full" when it is issued to disk, and we'll make 2055 * use of the lwb's size the best we can. 2056 * 2057 * 2. If there isn't sufficient ZIL activity occuring on 2058 * the system, such that this lwb's zio isn't issued via 2059 * zil_lwb_commit(), zil_commit_waiter() will issue the 2060 * lwb's zio. If this occurs, the lwb is not guaranteed 2061 * to be "full" by the time its zio is issued, and means 2062 * the size of the lwb was "too large" given the amount 2063 * of ZIL activity occuring on the system at that time. 2064 * 2065 * We do this for a couple of reasons: 2066 * 2067 * 1. To try and reduce the number of IOPs needed to 2068 * write the same number of itxs. If an lwb has space 2069 * available in it's buffer for more itxs, and more itxs 2070 * will be committed relatively soon (relative to the 2071 * latency of performing a write), then it's beneficial 2072 * to wait for these "next" itxs. This way, more itxs 2073 * can be committed to stable storage with fewer writes. 2074 * 2075 * 2. To try and use the largest lwb block size that the 2076 * incoming rate of itxs can support. Again, this is to 2077 * try and pack as many itxs into as few lwbs as 2078 * possible, without significantly impacting the latency 2079 * of each individual itx. 2080 */ 2081 } 2082} 2083 2084/* 2085 * This function is responsible for ensuring the passed in commit waiter 2086 * (and associated commit itx) is committed to an lwb. If the waiter is 2087 * not already committed to an lwb, all itxs in the zilog's queue of 2088 * itxs will be processed. The assumption is the passed in waiter's 2089 * commit itx will found in the queue just like the other non-commit 2090 * itxs, such that when the entire queue is processed, the waiter will 2091 * have been commited to an lwb. 2092 * 2093 * The lwb associated with the passed in waiter is not guaranteed to 2094 * have been issued by the time this function completes. If the lwb is 2095 * not issued, we rely on future calls to zil_commit_writer() to issue 2096 * the lwb, or the timeout mechanism found in zil_commit_waiter(). 2097 */ 2098static void 2099zil_commit_writer(zilog_t *zilog, zil_commit_waiter_t *zcw) 2100{ 2101 ASSERT(!MUTEX_HELD(&zilog->zl_lock)); 2102 ASSERT(spa_writeable(zilog->zl_spa)); 2103 ASSERT0(zilog->zl_suspend); 2104 2105 mutex_enter(&zilog->zl_writer_lock); 2106 2107 if (zcw->zcw_lwb != NULL || zcw->zcw_done) { 2108 /* 2109 * It's possible that, while we were waiting to acquire 2110 * the "zl_writer_lock", another thread committed this 2111 * waiter to an lwb. If that occurs, we bail out early, 2112 * without processing any of the zilog's queue of itxs. 2113 * 2114 * On certain workloads and system configurations, the 2115 * "zl_writer_lock" can become highly contended. In an 2116 * attempt to reduce this contention, we immediately drop 2117 * the lock if the waiter has already been processed. 2118 * 2119 * We've measured this optimization to reduce CPU spent 2120 * contending on this lock by up to 5%, using a system 2121 * with 32 CPUs, low latency storage (~50 usec writes), 2122 * and 1024 threads performing sync writes. 2123 */ 2124 goto out; 2125 } 2126 2127 zil_get_commit_list(zilog); 2128 zil_prune_commit_list(zilog); 2129 zil_process_commit_list(zilog); 2130 2131out: 2132 mutex_exit(&zilog->zl_writer_lock); 2133} 2134 2135static void 2136zil_commit_waiter_timeout(zilog_t *zilog, zil_commit_waiter_t *zcw) 2137{ 2138 ASSERT(!MUTEX_HELD(&zilog->zl_writer_lock)); 2139 ASSERT(MUTEX_HELD(&zcw->zcw_lock)); 2140 ASSERT3B(zcw->zcw_done, ==, B_FALSE); 2141 2142 lwb_t *lwb = zcw->zcw_lwb; 2143 ASSERT3P(lwb, !=, NULL); 2144 ASSERT3S(lwb->lwb_state, !=, LWB_STATE_CLOSED); 2145 2146 /* 2147 * If the lwb has already been issued by another thread, we can 2148 * immediately return since there's no work to be done (the 2149 * point of this function is to issue the lwb). Additionally, we 2150 * do this prior to acquiring the zl_writer_lock, to avoid 2151 * acquiring it when it's not necessary to do so. 2152 */ 2153 if (lwb->lwb_state == LWB_STATE_ISSUED || 2154 lwb->lwb_state == LWB_STATE_DONE) 2155 return; 2156 2157 /* 2158 * In order to call zil_lwb_write_issue() we must hold the 2159 * zilog's "zl_writer_lock". We can't simply acquire that lock, 2160 * since we're already holding the commit waiter's "zcw_lock", 2161 * and those two locks are aquired in the opposite order 2162 * elsewhere. 2163 */ 2164 mutex_exit(&zcw->zcw_lock); 2165 mutex_enter(&zilog->zl_writer_lock); 2166 mutex_enter(&zcw->zcw_lock); 2167 2168 /* 2169 * Since we just dropped and re-acquired the commit waiter's 2170 * lock, we have to re-check to see if the waiter was marked 2171 * "done" during that process. If the waiter was marked "done", 2172 * the "lwb" pointer is no longer valid (it can be free'd after 2173 * the waiter is marked "done"), so without this check we could 2174 * wind up with a use-after-free error below. 2175 */ 2176 if (zcw->zcw_done) 2177 goto out; 2178 2179 ASSERT3P(lwb, ==, zcw->zcw_lwb); 2180 2181 /* 2182 * We've already checked this above, but since we hadn't 2183 * acquired the zilog's zl_writer_lock, we have to perform this 2184 * check a second time while holding the lock. We can't call 2185 * zil_lwb_write_issue() if the lwb had already been issued. 2186 */ 2187 if (lwb->lwb_state == LWB_STATE_ISSUED || 2188 lwb->lwb_state == LWB_STATE_DONE) 2189 goto out; 2190 2191 ASSERT3S(lwb->lwb_state, ==, LWB_STATE_OPENED); 2192 2193 /* 2194 * As described in the comments above zil_commit_waiter() and 2195 * zil_process_commit_list(), we need to issue this lwb's zio 2196 * since we've reached the commit waiter's timeout and it still 2197 * hasn't been issued. 2198 */ 2199 lwb_t *nlwb = zil_lwb_write_issue(zilog, lwb); 2200 2201 ASSERT3S(lwb->lwb_state, !=, LWB_STATE_OPENED); 2202 2203 /* 2204 * Since the lwb's zio hadn't been issued by the time this thread 2205 * reached its timeout, we reset the zilog's "zl_cur_used" field 2206 * to influence the zil block size selection algorithm. 2207 * 2208 * By having to issue the lwb's zio here, it means the size of the 2209 * lwb was too large, given the incoming throughput of itxs. By 2210 * setting "zl_cur_used" to zero, we communicate this fact to the 2211 * block size selection algorithm, so it can take this informaiton 2212 * into account, and potentially select a smaller size for the 2213 * next lwb block that is allocated. 2214 */ 2215 zilog->zl_cur_used = 0; 2216 2217 if (nlwb == NULL) { 2218 /* 2219 * When zil_lwb_write_issue() returns NULL, this 2220 * indicates zio_alloc_zil() failed to allocate the 2221 * "next" lwb on-disk. When this occurs, the ZIL write 2222 * pipeline must be stalled; see the comment within the 2223 * zil_commit_writer_stall() function for more details. 2224 * 2225 * We must drop the commit waiter's lock prior to 2226 * calling zil_commit_writer_stall() or else we can wind 2227 * up with the following deadlock: 2228 * 2229 * - This thread is waiting for the txg to sync while 2230 * holding the waiter's lock; txg_wait_synced() is 2231 * used within txg_commit_writer_stall(). 2232 * 2233 * - The txg can't sync because it is waiting for this 2234 * lwb's zio callback to call dmu_tx_commit(). 2235 * 2236 * - The lwb's zio callback can't call dmu_tx_commit() 2237 * because it's blocked trying to acquire the waiter's 2238 * lock, which occurs prior to calling dmu_tx_commit() 2239 */ 2240 mutex_exit(&zcw->zcw_lock); 2241 zil_commit_writer_stall(zilog); 2242 mutex_enter(&zcw->zcw_lock); 2243 } 2244 2245out: 2246 mutex_exit(&zilog->zl_writer_lock); 2247 ASSERT(MUTEX_HELD(&zcw->zcw_lock)); 2248} 2249 2250/* 2251 * This function is responsible for performing the following two tasks: 2252 * 2253 * 1. its primary responsibility is to block until the given "commit 2254 * waiter" is considered "done". 2255 * 2256 * 2. its secondary responsibility is to issue the zio for the lwb that 2257 * the given "commit waiter" is waiting on, if this function has 2258 * waited "long enough" and the lwb is still in the "open" state. 2259 * 2260 * Given a sufficient amount of itxs being generated and written using 2261 * the ZIL, the lwb's zio will be issued via the zil_lwb_commit() 2262 * function. If this does not occur, this secondary responsibility will 2263 * ensure the lwb is issued even if there is not other synchronous 2264 * activity on the system. 2265 * 2266 * For more details, see zil_process_commit_list(); more specifically, 2267 * the comment at the bottom of that function. 2268 */ 2269static void 2270zil_commit_waiter(zilog_t *zilog, zil_commit_waiter_t *zcw) 2271{ 2272 ASSERT(!MUTEX_HELD(&zilog->zl_lock)); 2273 ASSERT(!MUTEX_HELD(&zilog->zl_writer_lock)); 2274 ASSERT(spa_writeable(zilog->zl_spa)); 2275 ASSERT0(zilog->zl_suspend); 2276 2277 mutex_enter(&zcw->zcw_lock); 2278 2279 /* 2280 * The timeout is scaled based on the lwb latency to avoid 2281 * significantly impacting the latency of each individual itx. 2282 * For more details, see the comment at the bottom of the 2283 * zil_process_commit_list() function. 2284 */ 2285 int pct = MAX(zfs_commit_timeout_pct, 1); 2286#if defined(illumos) || !defined(_KERNEL) 2287 hrtime_t sleep = (zilog->zl_last_lwb_latency * pct) / 100; 2288 hrtime_t wakeup = gethrtime() + sleep; 2289#else 2290 sbintime_t sleep = nstosbt((zilog->zl_last_lwb_latency * pct) / 100); 2291 sbintime_t wakeup = getsbinuptime() + sleep; 2292#endif 2293 boolean_t timedout = B_FALSE; 2294 2295 while (!zcw->zcw_done) { 2296 ASSERT(MUTEX_HELD(&zcw->zcw_lock)); 2297 2298 lwb_t *lwb = zcw->zcw_lwb; 2299 2300 /* 2301 * Usually, the waiter will have a non-NULL lwb field here, 2302 * but it's possible for it to be NULL as a result of 2303 * zil_commit() racing with spa_sync(). 2304 * 2305 * When zil_clean() is called, it's possible for the itxg 2306 * list (which may be cleaned via a taskq) to contain 2307 * commit itxs. When this occurs, the commit waiters linked 2308 * off of these commit itxs will not be committed to an 2309 * lwb. Additionally, these commit waiters will not be 2310 * marked done until zil_commit_waiter_skip() is called via 2311 * zil_itxg_clean(). 2312 * 2313 * Thus, it's possible for this commit waiter (i.e. the 2314 * "zcw" variable) to be found in this "in between" state; 2315 * where it's "zcw_lwb" field is NULL, and it hasn't yet 2316 * been skipped, so it's "zcw_done" field is still B_FALSE. 2317 */ 2318 IMPLY(lwb != NULL, lwb->lwb_state != LWB_STATE_CLOSED); 2319 2320 if (lwb != NULL && lwb->lwb_state == LWB_STATE_OPENED) { 2321 ASSERT3B(timedout, ==, B_FALSE); 2322 2323 /* 2324 * If the lwb hasn't been issued yet, then we 2325 * need to wait with a timeout, in case this 2326 * function needs to issue the lwb after the 2327 * timeout is reached; responsibility (2) from 2328 * the comment above this function. 2329 */ 2330#if defined(illumos) || !defined(_KERNEL) 2331 clock_t timeleft = cv_timedwait_hires(&zcw->zcw_cv, 2332 &zcw->zcw_lock, wakeup, USEC2NSEC(1), 2333 CALLOUT_FLAG_ABSOLUTE); 2334 2335 if (timeleft >= 0 || zcw->zcw_done) 2336 continue; 2337#else 2338 int wait_err = cv_timedwait_sbt(&zcw->zcw_cv, 2339 &zcw->zcw_lock, wakeup, SBT_1NS, C_ABSOLUTE); 2340 if (wait_err != EWOULDBLOCK || zcw->zcw_done) 2341 continue; 2342#endif 2343 2344 timedout = B_TRUE; 2345 zil_commit_waiter_timeout(zilog, zcw); 2346 2347 if (!zcw->zcw_done) { 2348 /* 2349 * If the commit waiter has already been 2350 * marked "done", it's possible for the 2351 * waiter's lwb structure to have already 2352 * been freed. Thus, we can only reliably 2353 * make these assertions if the waiter 2354 * isn't done. 2355 */ 2356 ASSERT3P(lwb, ==, zcw->zcw_lwb); 2357 ASSERT3S(lwb->lwb_state, !=, LWB_STATE_OPENED); 2358 } 2359 } else { 2360 /* 2361 * If the lwb isn't open, then it must have already 2362 * been issued. In that case, there's no need to 2363 * use a timeout when waiting for the lwb to 2364 * complete. 2365 * 2366 * Additionally, if the lwb is NULL, the waiter 2367 * will soon be signalled and marked done via 2368 * zil_clean() and zil_itxg_clean(), so no timeout 2369 * is required. 2370 */ 2371 2372 IMPLY(lwb != NULL, 2373 lwb->lwb_state == LWB_STATE_ISSUED || 2374 lwb->lwb_state == LWB_STATE_DONE); 2375 cv_wait(&zcw->zcw_cv, &zcw->zcw_lock); 2376 } 2377 } 2378 2379 mutex_exit(&zcw->zcw_lock); 2380} 2381 2382static zil_commit_waiter_t * 2383zil_alloc_commit_waiter() 2384{ 2385 zil_commit_waiter_t *zcw = kmem_cache_alloc(zil_zcw_cache, KM_SLEEP); 2386 2387 cv_init(&zcw->zcw_cv, NULL, CV_DEFAULT, NULL); 2388 mutex_init(&zcw->zcw_lock, NULL, MUTEX_DEFAULT, NULL); 2389 list_link_init(&zcw->zcw_node); 2390 zcw->zcw_lwb = NULL; 2391 zcw->zcw_done = B_FALSE; 2392 zcw->zcw_zio_error = 0; 2393 2394 return (zcw); 2395} 2396 2397static void 2398zil_free_commit_waiter(zil_commit_waiter_t *zcw) 2399{ 2400 ASSERT(!list_link_active(&zcw->zcw_node)); 2401 ASSERT3P(zcw->zcw_lwb, ==, NULL); 2402 ASSERT3B(zcw->zcw_done, ==, B_TRUE); 2403 mutex_destroy(&zcw->zcw_lock); 2404 cv_destroy(&zcw->zcw_cv); 2405 kmem_cache_free(zil_zcw_cache, zcw); 2406} 2407 2408/* 2409 * This function is used to create a TX_COMMIT itx and assign it. This 2410 * way, it will be linked into the ZIL's list of synchronous itxs, and 2411 * then later committed to an lwb (or skipped) when 2412 * zil_process_commit_list() is called. 2413 */ 2414static void 2415zil_commit_itx_assign(zilog_t *zilog, zil_commit_waiter_t *zcw) 2416{ 2417 dmu_tx_t *tx = dmu_tx_create(zilog->zl_os); 2418 VERIFY0(dmu_tx_assign(tx, TXG_WAIT)); 2419 2420 itx_t *itx = zil_itx_create(TX_COMMIT, sizeof (lr_t)); 2421 itx->itx_sync = B_TRUE; 2422 itx->itx_private = zcw; 2423 2424 zil_itx_assign(zilog, itx, tx); 2425 2426 dmu_tx_commit(tx); 2427} 2428 2429/* 2430 * Commit ZFS Intent Log transactions (itxs) to stable storage. 2431 * 2432 * When writing ZIL transactions to the on-disk representation of the 2433 * ZIL, the itxs are committed to a Log Write Block (lwb). Multiple 2434 * itxs can be committed to a single lwb. Once a lwb is written and 2435 * committed to stable storage (i.e. the lwb is written, and vdevs have 2436 * been flushed), each itx that was committed to that lwb is also 2437 * considered to be committed to stable storage. 2438 * 2439 * When an itx is committed to an lwb, the log record (lr_t) contained 2440 * by the itx is copied into the lwb's zio buffer, and once this buffer 2441 * is written to disk, it becomes an on-disk ZIL block. 2442 * 2443 * As itxs are generated, they're inserted into the ZIL's queue of 2444 * uncommitted itxs. The semantics of zil_commit() are such that it will 2445 * block until all itxs that were in the queue when it was called, are 2446 * committed to stable storage. 2447 * 2448 * If "foid" is zero, this means all "synchronous" and "asynchronous" 2449 * itxs, for all objects in the dataset, will be committed to stable 2450 * storage prior to zil_commit() returning. If "foid" is non-zero, all 2451 * "synchronous" itxs for all objects, but only "asynchronous" itxs 2452 * that correspond to the foid passed in, will be committed to stable 2453 * storage prior to zil_commit() returning. 2454 * 2455 * Generally speaking, when zil_commit() is called, the consumer doesn't 2456 * actually care about _all_ of the uncommitted itxs. Instead, they're 2457 * simply trying to waiting for a specific itx to be committed to disk, 2458 * but the interface(s) for interacting with the ZIL don't allow such 2459 * fine-grained communication. A better interface would allow a consumer 2460 * to create and assign an itx, and then pass a reference to this itx to 2461 * zil_commit(); such that zil_commit() would return as soon as that 2462 * specific itx was committed to disk (instead of waiting for _all_ 2463 * itxs to be committed). 2464 * 2465 * When a thread calls zil_commit() a special "commit itx" will be 2466 * generated, along with a corresponding "waiter" for this commit itx. 2467 * zil_commit() will wait on this waiter's CV, such that when the waiter 2468 * is marked done, and signalled, zil_commit() will return. 2469 * 2470 * This commit itx is inserted into the queue of uncommitted itxs. This 2471 * provides an easy mechanism for determining which itxs were in the 2472 * queue prior to zil_commit() having been called, and which itxs were 2473 * added after zil_commit() was called. 2474 * 2475 * The commit it is special; it doesn't have any on-disk representation. 2476 * When a commit itx is "committed" to an lwb, the waiter associated 2477 * with it is linked onto the lwb's list of waiters. Then, when that lwb 2478 * completes, each waiter on the lwb's list is marked done and signalled 2479 * -- allowing the thread waiting on the waiter to return from zil_commit(). 2480 * 2481 * It's important to point out a few critical factors that allow us 2482 * to make use of the commit itxs, commit waiters, per-lwb lists of 2483 * commit waiters, and zio completion callbacks like we're doing: 2484 * 2485 * 1. The list of waiters for each lwb is traversed, and each commit 2486 * waiter is marked "done" and signalled, in the zio completion 2487 * callback of the lwb's zio[*]. 2488 * 2489 * * Actually, the waiters are signalled in the zio completion 2490 * callback of the root zio for the DKIOCFLUSHWRITECACHE commands 2491 * that are sent to the vdevs upon completion of the lwb zio. 2492 * 2493 * 2. When the itxs are inserted into the ZIL's queue of uncommitted 2494 * itxs, the order in which they are inserted is preserved[*]; as 2495 * itxs are added to the queue, they are added to the tail of 2496 * in-memory linked lists. 2497 * 2498 * When committing the itxs to lwbs (to be written to disk), they 2499 * are committed in the same order in which the itxs were added to 2500 * the uncommitted queue's linked list(s); i.e. the linked list of 2501 * itxs to commit is traversed from head to tail, and each itx is 2502 * committed to an lwb in that order. 2503 * 2504 * * To clarify: 2505 * 2506 * - the order of "sync" itxs is preserved w.r.t. other 2507 * "sync" itxs, regardless of the corresponding objects. 2508 * - the order of "async" itxs is preserved w.r.t. other 2509 * "async" itxs corresponding to the same object. 2510 * - the order of "async" itxs is *not* preserved w.r.t. other 2511 * "async" itxs corresponding to different objects. 2512 * - the order of "sync" itxs w.r.t. "async" itxs (or vice 2513 * versa) is *not* preserved, even for itxs that correspond 2514 * to the same object. 2515 * 2516 * For more details, see: zil_itx_assign(), zil_async_to_sync(), 2517 * zil_get_commit_list(), and zil_process_commit_list(). 2518 * 2519 * 3. The lwbs represent a linked list of blocks on disk. Thus, any 2520 * lwb cannot be considered committed to stable storage, until its 2521 * "previous" lwb is also committed to stable storage. This fact, 2522 * coupled with the fact described above, means that itxs are 2523 * committed in (roughly) the order in which they were generated. 2524 * This is essential because itxs are dependent on prior itxs. 2525 * Thus, we *must not* deem an itx as being committed to stable 2526 * storage, until *all* prior itxs have also been committed to 2527 * stable storage. 2528 * 2529 * To enforce this ordering of lwb zio's, while still leveraging as 2530 * much of the underlying storage performance as possible, we rely 2531 * on two fundamental concepts: 2532 * 2533 * 1. The creation and issuance of lwb zio's is protected by 2534 * the zilog's "zl_writer_lock", which ensures only a single 2535 * thread is creating and/or issuing lwb's at a time 2536 * 2. The "previous" lwb is a child of the "current" lwb 2537 * (leveraging the zio parent-child depenency graph) 2538 * 2539 * By relying on this parent-child zio relationship, we can have 2540 * many lwb zio's concurrently issued to the underlying storage, 2541 * but the order in which they complete will be the same order in 2542 * which they were created. 2543 */ 2544void 2545zil_commit(zilog_t *zilog, uint64_t foid) 2546{ 2547 /* 2548 * We should never attempt to call zil_commit on a snapshot for 2549 * a couple of reasons: 2550 * 2551 * 1. A snapshot may never be modified, thus it cannot have any 2552 * in-flight itxs that would have modified the dataset. 2553 * 2554 * 2. By design, when zil_commit() is called, a commit itx will 2555 * be assigned to this zilog; as a result, the zilog will be 2556 * dirtied. We must not dirty the zilog of a snapshot; there's 2557 * checks in the code that enforce this invariant, and will 2558 * cause a panic if it's not upheld. 2559 */ 2560 ASSERT3B(dmu_objset_is_snapshot(zilog->zl_os), ==, B_FALSE); 2561 2562 if (zilog->zl_sync == ZFS_SYNC_DISABLED) 2563 return; 2564 2565 if (!spa_writeable(zilog->zl_spa)) { 2566 /* 2567 * If the SPA is not writable, there should never be any 2568 * pending itxs waiting to be committed to disk. If that 2569 * weren't true, we'd skip writing those itxs out, and 2570 * would break the sematics of zil_commit(); thus, we're 2571 * verifying that truth before we return to the caller. 2572 */ 2573 ASSERT(list_is_empty(&zilog->zl_lwb_list)); 2574 ASSERT3P(zilog->zl_last_lwb_opened, ==, NULL); 2575 for (int i = 0; i < TXG_SIZE; i++) 2576 ASSERT3P(zilog->zl_itxg[i].itxg_itxs, ==, NULL); 2577 return; 2578 } 2579 2580 /* 2581 * If the ZIL is suspended, we don't want to dirty it by calling 2582 * zil_commit_itx_assign() below, nor can we write out 2583 * lwbs like would be done in zil_commit_write(). Thus, we 2584 * simply rely on txg_wait_synced() to maintain the necessary 2585 * semantics, and avoid calling those functions altogether. 2586 */ 2587 if (zilog->zl_suspend > 0) { 2588 txg_wait_synced(zilog->zl_dmu_pool, 0); 2589 return; 2590 } 2591 2592 /* 2593 * Move the "async" itxs for the specified foid to the "sync" 2594 * queues, such that they will be later committed (or skipped) 2595 * to an lwb when zil_process_commit_list() is called. 2596 * 2597 * Since these "async" itxs must be committed prior to this 2598 * call to zil_commit returning, we must perform this operation 2599 * before we call zil_commit_itx_assign(). 2600 */ 2601 zil_async_to_sync(zilog, foid); 2602 2603 /* 2604 * We allocate a new "waiter" structure which will initially be 2605 * linked to the commit itx using the itx's "itx_private" field. 2606 * Since the commit itx doesn't represent any on-disk state, 2607 * when it's committed to an lwb, rather than copying the its 2608 * lr_t into the lwb's buffer, the commit itx's "waiter" will be 2609 * added to the lwb's list of waiters. Then, when the lwb is 2610 * committed to stable storage, each waiter in the lwb's list of 2611 * waiters will be marked "done", and signalled. 2612 * 2613 * We must create the waiter and assign the commit itx prior to 2614 * calling zil_commit_writer(), or else our specific commit itx 2615 * is not guaranteed to be committed to an lwb prior to calling 2616 * zil_commit_waiter(). 2617 */ 2618 zil_commit_waiter_t *zcw = zil_alloc_commit_waiter(); 2619 zil_commit_itx_assign(zilog, zcw); 2620 2621 zil_commit_writer(zilog, zcw); 2622 zil_commit_waiter(zilog, zcw); 2623 2624 if (zcw->zcw_zio_error != 0) { 2625 /* 2626 * If there was an error writing out the ZIL blocks that 2627 * this thread is waiting on, then we fallback to 2628 * relying on spa_sync() to write out the data this 2629 * thread is waiting on. Obviously this has performance 2630 * implications, but the expectation is for this to be 2631 * an exceptional case, and shouldn't occur often. 2632 */ 2633 DTRACE_PROBE2(zil__commit__io__error, 2634 zilog_t *, zilog, zil_commit_waiter_t *, zcw); 2635 txg_wait_synced(zilog->zl_dmu_pool, 0); 2636 } 2637 2638 zil_free_commit_waiter(zcw); 2639} 2640 2641/* 2642 * Called in syncing context to free committed log blocks and update log header. 2643 */ 2644void 2645zil_sync(zilog_t *zilog, dmu_tx_t *tx) 2646{ 2647 zil_header_t *zh = zil_header_in_syncing_context(zilog); 2648 uint64_t txg = dmu_tx_get_txg(tx); 2649 spa_t *spa = zilog->zl_spa; 2650 uint64_t *replayed_seq = &zilog->zl_replayed_seq[txg & TXG_MASK]; 2651 lwb_t *lwb; 2652 2653 /* 2654 * We don't zero out zl_destroy_txg, so make sure we don't try 2655 * to destroy it twice. 2656 */ 2657 if (spa_sync_pass(spa) != 1) 2658 return; 2659 2660 mutex_enter(&zilog->zl_lock); 2661 2662 ASSERT(zilog->zl_stop_sync == 0); 2663 2664 if (*replayed_seq != 0) { 2665 ASSERT(zh->zh_replay_seq < *replayed_seq); 2666 zh->zh_replay_seq = *replayed_seq; 2667 *replayed_seq = 0; 2668 } 2669 2670 if (zilog->zl_destroy_txg == txg) { 2671 blkptr_t blk = zh->zh_log; 2672 2673 ASSERT(list_head(&zilog->zl_lwb_list) == NULL); 2674 2675 bzero(zh, sizeof (zil_header_t)); 2676 bzero(zilog->zl_replayed_seq, sizeof (zilog->zl_replayed_seq)); 2677 2678 if (zilog->zl_keep_first) { 2679 /* 2680 * If this block was part of log chain that couldn't 2681 * be claimed because a device was missing during 2682 * zil_claim(), but that device later returns, 2683 * then this block could erroneously appear valid. 2684 * To guard against this, assign a new GUID to the new 2685 * log chain so it doesn't matter what blk points to. 2686 */ 2687 zil_init_log_chain(zilog, &blk); 2688 zh->zh_log = blk; 2689 } 2690 } 2691 2692 while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 2693 zh->zh_log = lwb->lwb_blk; 2694 if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg) 2695 break; 2696 list_remove(&zilog->zl_lwb_list, lwb); 2697 zio_free(spa, txg, &lwb->lwb_blk); 2698 zil_free_lwb(zilog, lwb); 2699 2700 /* 2701 * If we don't have anything left in the lwb list then 2702 * we've had an allocation failure and we need to zero 2703 * out the zil_header blkptr so that we don't end 2704 * up freeing the same block twice. 2705 */ 2706 if (list_head(&zilog->zl_lwb_list) == NULL) 2707 BP_ZERO(&zh->zh_log); 2708 } 2709 mutex_exit(&zilog->zl_lock); 2710} 2711 2712/* ARGSUSED */ 2713static int 2714zil_lwb_cons(void *vbuf, void *unused, int kmflag) 2715{ 2716 lwb_t *lwb = vbuf; 2717 list_create(&lwb->lwb_waiters, sizeof (zil_commit_waiter_t), 2718 offsetof(zil_commit_waiter_t, zcw_node)); 2719 avl_create(&lwb->lwb_vdev_tree, zil_lwb_vdev_compare, 2720 sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node)); 2721 mutex_init(&lwb->lwb_vdev_lock, NULL, MUTEX_DEFAULT, NULL); 2722 return (0); 2723} 2724 2725/* ARGSUSED */ 2726static void 2727zil_lwb_dest(void *vbuf, void *unused) 2728{ 2729 lwb_t *lwb = vbuf; 2730 mutex_destroy(&lwb->lwb_vdev_lock); 2731 avl_destroy(&lwb->lwb_vdev_tree); 2732 list_destroy(&lwb->lwb_waiters); 2733} 2734 2735void 2736zil_init(void) 2737{ 2738 zil_lwb_cache = kmem_cache_create("zil_lwb_cache", 2739 sizeof (lwb_t), 0, zil_lwb_cons, zil_lwb_dest, NULL, NULL, NULL, 0); 2740 2741 zil_zcw_cache = kmem_cache_create("zil_zcw_cache", 2742 sizeof (zil_commit_waiter_t), 0, NULL, NULL, NULL, NULL, NULL, 0); 2743} 2744 2745void 2746zil_fini(void) 2747{ 2748 kmem_cache_destroy(zil_zcw_cache); 2749 kmem_cache_destroy(zil_lwb_cache); 2750} 2751 2752void 2753zil_set_sync(zilog_t *zilog, uint64_t sync) 2754{ 2755 zilog->zl_sync = sync; 2756} 2757 2758void 2759zil_set_logbias(zilog_t *zilog, uint64_t logbias) 2760{ 2761 zilog->zl_logbias = logbias; 2762} 2763 2764zilog_t * 2765zil_alloc(objset_t *os, zil_header_t *zh_phys) 2766{ 2767 zilog_t *zilog; 2768 2769 zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP); 2770 2771 zilog->zl_header = zh_phys; 2772 zilog->zl_os = os; 2773 zilog->zl_spa = dmu_objset_spa(os); 2774 zilog->zl_dmu_pool = dmu_objset_pool(os); 2775 zilog->zl_destroy_txg = TXG_INITIAL - 1; 2776 zilog->zl_logbias = dmu_objset_logbias(os); 2777 zilog->zl_sync = dmu_objset_syncprop(os); 2778 zilog->zl_dirty_max_txg = 0; 2779 zilog->zl_last_lwb_opened = NULL; 2780 zilog->zl_last_lwb_latency = 0; 2781 2782 mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL); 2783 mutex_init(&zilog->zl_writer_lock, NULL, MUTEX_DEFAULT, NULL); 2784 2785 for (int i = 0; i < TXG_SIZE; i++) { 2786 mutex_init(&zilog->zl_itxg[i].itxg_lock, NULL, 2787 MUTEX_DEFAULT, NULL); 2788 } 2789 2790 list_create(&zilog->zl_lwb_list, sizeof (lwb_t), 2791 offsetof(lwb_t, lwb_node)); 2792 2793 list_create(&zilog->zl_itx_commit_list, sizeof (itx_t), 2794 offsetof(itx_t, itx_node)); 2795 2796 cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL); 2797 2798 return (zilog); 2799} 2800 2801void 2802zil_free(zilog_t *zilog) 2803{ 2804 zilog->zl_stop_sync = 1; 2805 2806 ASSERT0(zilog->zl_suspend); 2807 ASSERT0(zilog->zl_suspending); 2808 2809 ASSERT(list_is_empty(&zilog->zl_lwb_list)); 2810 list_destroy(&zilog->zl_lwb_list); 2811 2812 ASSERT(list_is_empty(&zilog->zl_itx_commit_list)); 2813 list_destroy(&zilog->zl_itx_commit_list); 2814 2815 for (int i = 0; i < TXG_SIZE; i++) { 2816 /* 2817 * It's possible for an itx to be generated that doesn't dirty 2818 * a txg (e.g. ztest TX_TRUNCATE). So there's no zil_clean() 2819 * callback to remove the entry. We remove those here. 2820 * 2821 * Also free up the ziltest itxs. 2822 */ 2823 if (zilog->zl_itxg[i].itxg_itxs) 2824 zil_itxg_clean(zilog->zl_itxg[i].itxg_itxs); 2825 mutex_destroy(&zilog->zl_itxg[i].itxg_lock); 2826 } 2827 2828 mutex_destroy(&zilog->zl_writer_lock); 2829 mutex_destroy(&zilog->zl_lock); 2830 2831 cv_destroy(&zilog->zl_cv_suspend); 2832 2833 kmem_free(zilog, sizeof (zilog_t)); 2834} 2835 2836/* 2837 * Open an intent log. 2838 */ 2839zilog_t * 2840zil_open(objset_t *os, zil_get_data_t *get_data) 2841{ 2842 zilog_t *zilog = dmu_objset_zil(os); 2843 2844 ASSERT3P(zilog->zl_get_data, ==, NULL); 2845 ASSERT3P(zilog->zl_last_lwb_opened, ==, NULL); 2846 ASSERT(list_is_empty(&zilog->zl_lwb_list)); 2847 2848 zilog->zl_get_data = get_data; 2849 2850 return (zilog); 2851} 2852 2853/* 2854 * Close an intent log. 2855 */ 2856void 2857zil_close(zilog_t *zilog) 2858{ 2859 lwb_t *lwb; 2860 uint64_t txg; 2861 2862 if (!dmu_objset_is_snapshot(zilog->zl_os)) { 2863 zil_commit(zilog, 0); 2864 } else { 2865 ASSERT3P(list_tail(&zilog->zl_lwb_list), ==, NULL); 2866 ASSERT0(zilog->zl_dirty_max_txg); 2867 ASSERT3B(zilog_is_dirty(zilog), ==, B_FALSE); 2868 } 2869 2870 mutex_enter(&zilog->zl_lock); 2871 lwb = list_tail(&zilog->zl_lwb_list); 2872 if (lwb == NULL) 2873 txg = zilog->zl_dirty_max_txg; 2874 else 2875 txg = MAX(zilog->zl_dirty_max_txg, lwb->lwb_max_txg); 2876 mutex_exit(&zilog->zl_lock); 2877 2878 /* 2879 * We need to use txg_wait_synced() to wait long enough for the 2880 * ZIL to be clean, and to wait for all pending lwbs to be 2881 * written out. 2882 */ 2883 if (txg != 0) 2884 txg_wait_synced(zilog->zl_dmu_pool, txg); 2885 2886 if (zilog_is_dirty(zilog)) 2887 zfs_dbgmsg("zil (%p) is dirty, txg %llu", zilog, txg); 2888 VERIFY(!zilog_is_dirty(zilog)); 2889 2890 zilog->zl_get_data = NULL; 2891 2892 /* 2893 * We should have only one lwb left on the list; remove it now. 2894 */ 2895 mutex_enter(&zilog->zl_lock); 2896 lwb = list_head(&zilog->zl_lwb_list); 2897 if (lwb != NULL) { 2898 ASSERT3P(lwb, ==, list_tail(&zilog->zl_lwb_list)); 2899 ASSERT3S(lwb->lwb_state, !=, LWB_STATE_ISSUED); 2900 list_remove(&zilog->zl_lwb_list, lwb); 2901 zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 2902 zil_free_lwb(zilog, lwb); 2903 } 2904 mutex_exit(&zilog->zl_lock); 2905} 2906 2907static char *suspend_tag = "zil suspending"; 2908 2909/* 2910 * Suspend an intent log. While in suspended mode, we still honor 2911 * synchronous semantics, but we rely on txg_wait_synced() to do it. 2912 * On old version pools, we suspend the log briefly when taking a 2913 * snapshot so that it will have an empty intent log. 2914 * 2915 * Long holds are not really intended to be used the way we do here -- 2916 * held for such a short time. A concurrent caller of dsl_dataset_long_held() 2917 * could fail. Therefore we take pains to only put a long hold if it is 2918 * actually necessary. Fortunately, it will only be necessary if the 2919 * objset is currently mounted (or the ZVOL equivalent). In that case it 2920 * will already have a long hold, so we are not really making things any worse. 2921 * 2922 * Ideally, we would locate the existing long-holder (i.e. the zfsvfs_t or 2923 * zvol_state_t), and use their mechanism to prevent their hold from being 2924 * dropped (e.g. VFS_HOLD()). However, that would be even more pain for 2925 * very little gain. 2926 * 2927 * if cookiep == NULL, this does both the suspend & resume. 2928 * Otherwise, it returns with the dataset "long held", and the cookie 2929 * should be passed into zil_resume(). 2930 */ 2931int 2932zil_suspend(const char *osname, void **cookiep) 2933{ 2934 objset_t *os; 2935 zilog_t *zilog; 2936 const zil_header_t *zh; 2937 int error; 2938 2939 error = dmu_objset_hold(osname, suspend_tag, &os); 2940 if (error != 0) 2941 return (error); 2942 zilog = dmu_objset_zil(os); 2943 2944 mutex_enter(&zilog->zl_lock); 2945 zh = zilog->zl_header; 2946 2947 if (zh->zh_flags & ZIL_REPLAY_NEEDED) { /* unplayed log */ 2948 mutex_exit(&zilog->zl_lock); 2949 dmu_objset_rele(os, suspend_tag); 2950 return (SET_ERROR(EBUSY)); 2951 } 2952 2953 /* 2954 * Don't put a long hold in the cases where we can avoid it. This 2955 * is when there is no cookie so we are doing a suspend & resume 2956 * (i.e. called from zil_vdev_offline()), and there's nothing to do 2957 * for the suspend because it's already suspended, or there's no ZIL. 2958 */ 2959 if (cookiep == NULL && !zilog->zl_suspending && 2960 (zilog->zl_suspend > 0 || BP_IS_HOLE(&zh->zh_log))) { 2961 mutex_exit(&zilog->zl_lock); 2962 dmu_objset_rele(os, suspend_tag); 2963 return (0); 2964 } 2965 2966 dsl_dataset_long_hold(dmu_objset_ds(os), suspend_tag); 2967 dsl_pool_rele(dmu_objset_pool(os), suspend_tag); 2968 2969 zilog->zl_suspend++; 2970 2971 if (zilog->zl_suspend > 1) { 2972 /* 2973 * Someone else is already suspending it. 2974 * Just wait for them to finish. 2975 */ 2976 2977 while (zilog->zl_suspending) 2978 cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock); 2979 mutex_exit(&zilog->zl_lock); 2980 2981 if (cookiep == NULL) 2982 zil_resume(os); 2983 else 2984 *cookiep = os; 2985 return (0); 2986 } 2987 2988 /* 2989 * If there is no pointer to an on-disk block, this ZIL must not 2990 * be active (e.g. filesystem not mounted), so there's nothing 2991 * to clean up. 2992 */ 2993 if (BP_IS_HOLE(&zh->zh_log)) { 2994 ASSERT(cookiep != NULL); /* fast path already handled */ 2995 2996 *cookiep = os; 2997 mutex_exit(&zilog->zl_lock); 2998 return (0); 2999 } 3000 3001 zilog->zl_suspending = B_TRUE; 3002 mutex_exit(&zilog->zl_lock); 3003 3004 zil_commit(zilog, 0); 3005 3006 zil_destroy(zilog, B_FALSE); 3007 3008 mutex_enter(&zilog->zl_lock); 3009 zilog->zl_suspending = B_FALSE; 3010 cv_broadcast(&zilog->zl_cv_suspend); 3011 mutex_exit(&zilog->zl_lock); 3012 3013 if (cookiep == NULL) 3014 zil_resume(os); 3015 else 3016 *cookiep = os; 3017 return (0); 3018} 3019 3020void 3021zil_resume(void *cookie) 3022{ 3023 objset_t *os = cookie; 3024 zilog_t *zilog = dmu_objset_zil(os); 3025 3026 mutex_enter(&zilog->zl_lock); 3027 ASSERT(zilog->zl_suspend != 0); 3028 zilog->zl_suspend--; 3029 mutex_exit(&zilog->zl_lock); 3030 dsl_dataset_long_rele(dmu_objset_ds(os), suspend_tag); 3031 dsl_dataset_rele(dmu_objset_ds(os), suspend_tag); 3032} 3033 3034typedef struct zil_replay_arg { 3035 zil_replay_func_t **zr_replay; 3036 void *zr_arg; 3037 boolean_t zr_byteswap; 3038 char *zr_lr; 3039} zil_replay_arg_t; 3040 3041static int 3042zil_replay_error(zilog_t *zilog, lr_t *lr, int error) 3043{ 3044 char name[ZFS_MAX_DATASET_NAME_LEN]; 3045 3046 zilog->zl_replaying_seq--; /* didn't actually replay this one */ 3047 3048 dmu_objset_name(zilog->zl_os, name); 3049 3050 cmn_err(CE_WARN, "ZFS replay transaction error %d, " 3051 "dataset %s, seq 0x%llx, txtype %llu %s\n", error, name, 3052 (u_longlong_t)lr->lrc_seq, 3053 (u_longlong_t)(lr->lrc_txtype & ~TX_CI), 3054 (lr->lrc_txtype & TX_CI) ? "CI" : ""); 3055 3056 return (error); 3057} 3058 3059static int 3060zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg) 3061{ 3062 zil_replay_arg_t *zr = zra; 3063 const zil_header_t *zh = zilog->zl_header; 3064 uint64_t reclen = lr->lrc_reclen; 3065 uint64_t txtype = lr->lrc_txtype; 3066 int error = 0; 3067 3068 zilog->zl_replaying_seq = lr->lrc_seq; 3069 3070 if (lr->lrc_seq <= zh->zh_replay_seq) /* already replayed */ 3071 return (0); 3072 3073 if (lr->lrc_txg < claim_txg) /* already committed */ 3074 return (0); 3075 3076 /* Strip case-insensitive bit, still present in log record */ 3077 txtype &= ~TX_CI; 3078 3079 if (txtype == 0 || txtype >= TX_MAX_TYPE) 3080 return (zil_replay_error(zilog, lr, EINVAL)); 3081 3082 /* 3083 * If this record type can be logged out of order, the object 3084 * (lr_foid) may no longer exist. That's legitimate, not an error. 3085 */ 3086 if (TX_OOO(txtype)) { 3087 error = dmu_object_info(zilog->zl_os, 3088 ((lr_ooo_t *)lr)->lr_foid, NULL); 3089 if (error == ENOENT || error == EEXIST) 3090 return (0); 3091 } 3092 3093 /* 3094 * Make a copy of the data so we can revise and extend it. 3095 */ 3096 bcopy(lr, zr->zr_lr, reclen); 3097 3098 /* 3099 * If this is a TX_WRITE with a blkptr, suck in the data. 3100 */ 3101 if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) { 3102 error = zil_read_log_data(zilog, (lr_write_t *)lr, 3103 zr->zr_lr + reclen); 3104 if (error != 0) 3105 return (zil_replay_error(zilog, lr, error)); 3106 } 3107 3108 /* 3109 * The log block containing this lr may have been byteswapped 3110 * so that we can easily examine common fields like lrc_txtype. 3111 * However, the log is a mix of different record types, and only the 3112 * replay vectors know how to byteswap their records. Therefore, if 3113 * the lr was byteswapped, undo it before invoking the replay vector. 3114 */ 3115 if (zr->zr_byteswap) 3116 byteswap_uint64_array(zr->zr_lr, reclen); 3117 3118 /* 3119 * We must now do two things atomically: replay this log record, 3120 * and update the log header sequence number to reflect the fact that 3121 * we did so. At the end of each replay function the sequence number 3122 * is updated if we are in replay mode. 3123 */ 3124 error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, zr->zr_byteswap); 3125 if (error != 0) { 3126 /* 3127 * The DMU's dnode layer doesn't see removes until the txg 3128 * commits, so a subsequent claim can spuriously fail with 3129 * EEXIST. So if we receive any error we try syncing out 3130 * any removes then retry the transaction. Note that we 3131 * specify B_FALSE for byteswap now, so we don't do it twice. 3132 */ 3133 txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0); 3134 error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, B_FALSE); 3135 if (error != 0) 3136 return (zil_replay_error(zilog, lr, error)); 3137 } 3138 return (0); 3139} 3140 3141/* ARGSUSED */ 3142static int 3143zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg) 3144{ 3145 zilog->zl_replay_blks++; 3146 3147 return (0); 3148} 3149 3150/* 3151 * If this dataset has a non-empty intent log, replay it and destroy it. 3152 */ 3153void 3154zil_replay(objset_t *os, void *arg, zil_replay_func_t *replay_func[TX_MAX_TYPE]) 3155{ 3156 zilog_t *zilog = dmu_objset_zil(os); 3157 const zil_header_t *zh = zilog->zl_header; 3158 zil_replay_arg_t zr; 3159 3160 if ((zh->zh_flags & ZIL_REPLAY_NEEDED) == 0) { 3161 zil_destroy(zilog, B_TRUE); 3162 return; 3163 } 3164 3165 zr.zr_replay = replay_func; 3166 zr.zr_arg = arg; 3167 zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log); 3168 zr.zr_lr = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP); 3169 3170 /* 3171 * Wait for in-progress removes to sync before starting replay. 3172 */ 3173 txg_wait_synced(zilog->zl_dmu_pool, 0); 3174 3175 zilog->zl_replay = B_TRUE; 3176 zilog->zl_replay_time = ddi_get_lbolt(); 3177 ASSERT(zilog->zl_replay_blks == 0); 3178 (void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr, 3179 zh->zh_claim_txg); 3180 kmem_free(zr.zr_lr, 2 * SPA_MAXBLOCKSIZE); 3181 3182 zil_destroy(zilog, B_FALSE); 3183 txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 3184 zilog->zl_replay = B_FALSE; 3185} 3186 3187boolean_t 3188zil_replaying(zilog_t *zilog, dmu_tx_t *tx) 3189{ 3190 if (zilog->zl_sync == ZFS_SYNC_DISABLED) 3191 return (B_TRUE); 3192 3193 if (zilog->zl_replay) { 3194 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 3195 zilog->zl_replayed_seq[dmu_tx_get_txg(tx) & TXG_MASK] = 3196 zilog->zl_replaying_seq; 3197 return (B_TRUE); 3198 } 3199 3200 return (B_FALSE); 3201} 3202 3203/* ARGSUSED */ 3204int 3205zil_vdev_offline(const char *osname, void *arg) 3206{ 3207 int error; 3208 3209 error = zil_suspend(osname, NULL); 3210 if (error != 0) 3211 return (SET_ERROR(EEXIST)); 3212 return (0); 3213} 3214