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 https://opensource.org/licenses/CDDL-1.0. 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, 2018 by Delphix. All rights reserved. 24 * Copyright (c) 2014 Integros [integros.com] 25 * Copyright (c) 2018 Datto Inc. 26 */ 27 28 /* Portions Copyright 2010 Robert Milkowski */ 29 30 #include <sys/zfs_context.h> 31 #include <sys/spa.h> 32 #include <sys/spa_impl.h> 33 #include <sys/dmu.h> 34 #include <sys/zap.h> 35 #include <sys/arc.h> 36 #include <sys/stat.h> 37 #include <sys/zil.h> 38 #include <sys/zil_impl.h> 39 #include <sys/dsl_dataset.h> 40 #include <sys/vdev_impl.h> 41 #include <sys/dmu_tx.h> 42 #include <sys/dsl_pool.h> 43 #include <sys/metaslab.h> 44 #include <sys/trace_zfs.h> 45 #include <sys/abd.h> 46 #include <sys/brt.h> 47 #include <sys/wmsum.h> 48 49 /* 50 * The ZFS Intent Log (ZIL) saves "transaction records" (itxs) of system 51 * calls that change the file system. Each itx has enough information to 52 * be able to replay them after a system crash, power loss, or 53 * equivalent failure mode. These are stored in memory until either: 54 * 55 * 1. they are committed to the pool by the DMU transaction group 56 * (txg), at which point they can be discarded; or 57 * 2. they are committed to the on-disk ZIL for the dataset being 58 * modified (e.g. due to an fsync, O_DSYNC, or other synchronous 59 * requirement). 60 * 61 * In the event of a crash or power loss, the itxs contained by each 62 * dataset's on-disk ZIL will be replayed when that dataset is first 63 * instantiated (e.g. if the dataset is a normal filesystem, when it is 64 * first mounted). 65 * 66 * As hinted at above, there is one ZIL per dataset (both the in-memory 67 * representation, and the on-disk representation). The on-disk format 68 * consists of 3 parts: 69 * 70 * - a single, per-dataset, ZIL header; which points to a chain of 71 * - zero or more ZIL blocks; each of which contains 72 * - zero or more ZIL records 73 * 74 * A ZIL record holds the information necessary to replay a single 75 * system call transaction. A ZIL block can hold many ZIL records, and 76 * the blocks are chained together, similarly to a singly linked list. 77 * 78 * Each ZIL block contains a block pointer (blkptr_t) to the next ZIL 79 * block in the chain, and the ZIL header points to the first block in 80 * the chain. 81 * 82 * Note, there is not a fixed place in the pool to hold these ZIL 83 * blocks; they are dynamically allocated and freed as needed from the 84 * blocks available on the pool, though they can be preferentially 85 * allocated from a dedicated "log" vdev. 86 */ 87 88 /* 89 * This controls the amount of time that a ZIL block (lwb) will remain 90 * "open" when it isn't "full", and it has a thread waiting for it to be 91 * committed to stable storage. Please refer to the zil_commit_waiter() 92 * function (and the comments within it) for more details. 93 */ 94 static uint_t zfs_commit_timeout_pct = 10; 95 96 /* 97 * See zil.h for more information about these fields. 98 */ 99 static zil_kstat_values_t zil_stats = { 100 { "zil_commit_count", KSTAT_DATA_UINT64 }, 101 { "zil_commit_writer_count", KSTAT_DATA_UINT64 }, 102 { "zil_itx_count", KSTAT_DATA_UINT64 }, 103 { "zil_itx_indirect_count", KSTAT_DATA_UINT64 }, 104 { "zil_itx_indirect_bytes", KSTAT_DATA_UINT64 }, 105 { "zil_itx_copied_count", KSTAT_DATA_UINT64 }, 106 { "zil_itx_copied_bytes", KSTAT_DATA_UINT64 }, 107 { "zil_itx_needcopy_count", KSTAT_DATA_UINT64 }, 108 { "zil_itx_needcopy_bytes", KSTAT_DATA_UINT64 }, 109 { "zil_itx_metaslab_normal_count", KSTAT_DATA_UINT64 }, 110 { "zil_itx_metaslab_normal_bytes", KSTAT_DATA_UINT64 }, 111 { "zil_itx_metaslab_normal_write", KSTAT_DATA_UINT64 }, 112 { "zil_itx_metaslab_normal_alloc", KSTAT_DATA_UINT64 }, 113 { "zil_itx_metaslab_slog_count", KSTAT_DATA_UINT64 }, 114 { "zil_itx_metaslab_slog_bytes", KSTAT_DATA_UINT64 }, 115 { "zil_itx_metaslab_slog_write", KSTAT_DATA_UINT64 }, 116 { "zil_itx_metaslab_slog_alloc", KSTAT_DATA_UINT64 }, 117 }; 118 119 static zil_sums_t zil_sums_global; 120 static kstat_t *zil_kstats_global; 121 122 /* 123 * Disable intent logging replay. This global ZIL switch affects all pools. 124 */ 125 int zil_replay_disable = 0; 126 127 /* 128 * Disable the DKIOCFLUSHWRITECACHE commands that are normally sent to 129 * the disk(s) by the ZIL after an LWB write has completed. Setting this 130 * will cause ZIL corruption on power loss if a volatile out-of-order 131 * write cache is enabled. 132 */ 133 static int zil_nocacheflush = 0; 134 135 /* 136 * Limit SLOG write size per commit executed with synchronous priority. 137 * Any writes above that will be executed with lower (asynchronous) priority 138 * to limit potential SLOG device abuse by single active ZIL writer. 139 */ 140 static uint64_t zil_slog_bulk = 64 * 1024 * 1024; 141 142 static kmem_cache_t *zil_lwb_cache; 143 static kmem_cache_t *zil_zcw_cache; 144 145 static void zil_lwb_commit(zilog_t *zilog, lwb_t *lwb, itx_t *itx); 146 static itx_t *zil_itx_clone(itx_t *oitx); 147 148 static int 149 zil_bp_compare(const void *x1, const void *x2) 150 { 151 const dva_t *dva1 = &((zil_bp_node_t *)x1)->zn_dva; 152 const dva_t *dva2 = &((zil_bp_node_t *)x2)->zn_dva; 153 154 int cmp = TREE_CMP(DVA_GET_VDEV(dva1), DVA_GET_VDEV(dva2)); 155 if (likely(cmp)) 156 return (cmp); 157 158 return (TREE_CMP(DVA_GET_OFFSET(dva1), DVA_GET_OFFSET(dva2))); 159 } 160 161 static void 162 zil_bp_tree_init(zilog_t *zilog) 163 { 164 avl_create(&zilog->zl_bp_tree, zil_bp_compare, 165 sizeof (zil_bp_node_t), offsetof(zil_bp_node_t, zn_node)); 166 } 167 168 static void 169 zil_bp_tree_fini(zilog_t *zilog) 170 { 171 avl_tree_t *t = &zilog->zl_bp_tree; 172 zil_bp_node_t *zn; 173 void *cookie = NULL; 174 175 while ((zn = avl_destroy_nodes(t, &cookie)) != NULL) 176 kmem_free(zn, sizeof (zil_bp_node_t)); 177 178 avl_destroy(t); 179 } 180 181 int 182 zil_bp_tree_add(zilog_t *zilog, const blkptr_t *bp) 183 { 184 avl_tree_t *t = &zilog->zl_bp_tree; 185 const dva_t *dva; 186 zil_bp_node_t *zn; 187 avl_index_t where; 188 189 if (BP_IS_EMBEDDED(bp)) 190 return (0); 191 192 dva = BP_IDENTITY(bp); 193 194 if (avl_find(t, dva, &where) != NULL) 195 return (SET_ERROR(EEXIST)); 196 197 zn = kmem_alloc(sizeof (zil_bp_node_t), KM_SLEEP); 198 zn->zn_dva = *dva; 199 avl_insert(t, zn, where); 200 201 return (0); 202 } 203 204 static zil_header_t * 205 zil_header_in_syncing_context(zilog_t *zilog) 206 { 207 return ((zil_header_t *)zilog->zl_header); 208 } 209 210 static void 211 zil_init_log_chain(zilog_t *zilog, blkptr_t *bp) 212 { 213 zio_cksum_t *zc = &bp->blk_cksum; 214 215 (void) random_get_pseudo_bytes((void *)&zc->zc_word[ZIL_ZC_GUID_0], 216 sizeof (zc->zc_word[ZIL_ZC_GUID_0])); 217 (void) random_get_pseudo_bytes((void *)&zc->zc_word[ZIL_ZC_GUID_1], 218 sizeof (zc->zc_word[ZIL_ZC_GUID_1])); 219 zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os); 220 zc->zc_word[ZIL_ZC_SEQ] = 1ULL; 221 } 222 223 static int 224 zil_kstats_global_update(kstat_t *ksp, int rw) 225 { 226 zil_kstat_values_t *zs = ksp->ks_data; 227 ASSERT3P(&zil_stats, ==, zs); 228 229 if (rw == KSTAT_WRITE) { 230 return (SET_ERROR(EACCES)); 231 } 232 233 zil_kstat_values_update(zs, &zil_sums_global); 234 235 return (0); 236 } 237 238 /* 239 * Read a log block and make sure it's valid. 240 */ 241 static int 242 zil_read_log_block(zilog_t *zilog, boolean_t decrypt, const blkptr_t *bp, 243 blkptr_t *nbp, char **begin, char **end, arc_buf_t **abuf) 244 { 245 zio_flag_t zio_flags = ZIO_FLAG_CANFAIL; 246 arc_flags_t aflags = ARC_FLAG_WAIT; 247 zbookmark_phys_t zb; 248 int error; 249 250 if (zilog->zl_header->zh_claim_txg == 0) 251 zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB; 252 253 if (!(zilog->zl_header->zh_flags & ZIL_CLAIM_LR_SEQ_VALID)) 254 zio_flags |= ZIO_FLAG_SPECULATIVE; 255 256 if (!decrypt) 257 zio_flags |= ZIO_FLAG_RAW; 258 259 SET_BOOKMARK(&zb, bp->blk_cksum.zc_word[ZIL_ZC_OBJSET], 260 ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]); 261 262 error = arc_read(NULL, zilog->zl_spa, bp, arc_getbuf_func, 263 abuf, ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb); 264 265 if (error == 0) { 266 zio_cksum_t cksum = bp->blk_cksum; 267 268 /* 269 * Validate the checksummed log block. 270 * 271 * Sequence numbers should be... sequential. The checksum 272 * verifier for the next block should be bp's checksum plus 1. 273 * 274 * Also check the log chain linkage and size used. 275 */ 276 cksum.zc_word[ZIL_ZC_SEQ]++; 277 278 uint64_t size = BP_GET_LSIZE(bp); 279 if (BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_ZILOG2) { 280 zil_chain_t *zilc = (*abuf)->b_data; 281 char *lr = (char *)(zilc + 1); 282 283 if (memcmp(&cksum, &zilc->zc_next_blk.blk_cksum, 284 sizeof (cksum)) || 285 zilc->zc_nused < sizeof (*zilc) || 286 zilc->zc_nused > size) { 287 error = SET_ERROR(ECKSUM); 288 } else { 289 *begin = lr; 290 *end = lr + zilc->zc_nused - sizeof (*zilc); 291 *nbp = zilc->zc_next_blk; 292 } 293 } else { 294 char *lr = (*abuf)->b_data; 295 zil_chain_t *zilc = (zil_chain_t *)(lr + size) - 1; 296 297 if (memcmp(&cksum, &zilc->zc_next_blk.blk_cksum, 298 sizeof (cksum)) || 299 (zilc->zc_nused > (size - sizeof (*zilc)))) { 300 error = SET_ERROR(ECKSUM); 301 } else { 302 *begin = lr; 303 *end = lr + zilc->zc_nused; 304 *nbp = zilc->zc_next_blk; 305 } 306 } 307 } 308 309 return (error); 310 } 311 312 /* 313 * Read a TX_WRITE log data block. 314 */ 315 static int 316 zil_read_log_data(zilog_t *zilog, const lr_write_t *lr, void *wbuf) 317 { 318 zio_flag_t zio_flags = ZIO_FLAG_CANFAIL; 319 const blkptr_t *bp = &lr->lr_blkptr; 320 arc_flags_t aflags = ARC_FLAG_WAIT; 321 arc_buf_t *abuf = NULL; 322 zbookmark_phys_t zb; 323 int error; 324 325 if (BP_IS_HOLE(bp)) { 326 if (wbuf != NULL) 327 memset(wbuf, 0, MAX(BP_GET_LSIZE(bp), lr->lr_length)); 328 return (0); 329 } 330 331 if (zilog->zl_header->zh_claim_txg == 0) 332 zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB; 333 334 /* 335 * If we are not using the resulting data, we are just checking that 336 * it hasn't been corrupted so we don't need to waste CPU time 337 * decompressing and decrypting it. 338 */ 339 if (wbuf == NULL) 340 zio_flags |= ZIO_FLAG_RAW; 341 342 ASSERT3U(BP_GET_LSIZE(bp), !=, 0); 343 SET_BOOKMARK(&zb, dmu_objset_id(zilog->zl_os), lr->lr_foid, 344 ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp)); 345 346 error = arc_read(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf, 347 ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb); 348 349 if (error == 0) { 350 if (wbuf != NULL) 351 memcpy(wbuf, abuf->b_data, arc_buf_size(abuf)); 352 arc_buf_destroy(abuf, &abuf); 353 } 354 355 return (error); 356 } 357 358 void 359 zil_sums_init(zil_sums_t *zs) 360 { 361 wmsum_init(&zs->zil_commit_count, 0); 362 wmsum_init(&zs->zil_commit_writer_count, 0); 363 wmsum_init(&zs->zil_itx_count, 0); 364 wmsum_init(&zs->zil_itx_indirect_count, 0); 365 wmsum_init(&zs->zil_itx_indirect_bytes, 0); 366 wmsum_init(&zs->zil_itx_copied_count, 0); 367 wmsum_init(&zs->zil_itx_copied_bytes, 0); 368 wmsum_init(&zs->zil_itx_needcopy_count, 0); 369 wmsum_init(&zs->zil_itx_needcopy_bytes, 0); 370 wmsum_init(&zs->zil_itx_metaslab_normal_count, 0); 371 wmsum_init(&zs->zil_itx_metaslab_normal_bytes, 0); 372 wmsum_init(&zs->zil_itx_metaslab_normal_write, 0); 373 wmsum_init(&zs->zil_itx_metaslab_normal_alloc, 0); 374 wmsum_init(&zs->zil_itx_metaslab_slog_count, 0); 375 wmsum_init(&zs->zil_itx_metaslab_slog_bytes, 0); 376 wmsum_init(&zs->zil_itx_metaslab_slog_write, 0); 377 wmsum_init(&zs->zil_itx_metaslab_slog_alloc, 0); 378 } 379 380 void 381 zil_sums_fini(zil_sums_t *zs) 382 { 383 wmsum_fini(&zs->zil_commit_count); 384 wmsum_fini(&zs->zil_commit_writer_count); 385 wmsum_fini(&zs->zil_itx_count); 386 wmsum_fini(&zs->zil_itx_indirect_count); 387 wmsum_fini(&zs->zil_itx_indirect_bytes); 388 wmsum_fini(&zs->zil_itx_copied_count); 389 wmsum_fini(&zs->zil_itx_copied_bytes); 390 wmsum_fini(&zs->zil_itx_needcopy_count); 391 wmsum_fini(&zs->zil_itx_needcopy_bytes); 392 wmsum_fini(&zs->zil_itx_metaslab_normal_count); 393 wmsum_fini(&zs->zil_itx_metaslab_normal_bytes); 394 wmsum_fini(&zs->zil_itx_metaslab_normal_write); 395 wmsum_fini(&zs->zil_itx_metaslab_normal_alloc); 396 wmsum_fini(&zs->zil_itx_metaslab_slog_count); 397 wmsum_fini(&zs->zil_itx_metaslab_slog_bytes); 398 wmsum_fini(&zs->zil_itx_metaslab_slog_write); 399 wmsum_fini(&zs->zil_itx_metaslab_slog_alloc); 400 } 401 402 void 403 zil_kstat_values_update(zil_kstat_values_t *zs, zil_sums_t *zil_sums) 404 { 405 zs->zil_commit_count.value.ui64 = 406 wmsum_value(&zil_sums->zil_commit_count); 407 zs->zil_commit_writer_count.value.ui64 = 408 wmsum_value(&zil_sums->zil_commit_writer_count); 409 zs->zil_itx_count.value.ui64 = 410 wmsum_value(&zil_sums->zil_itx_count); 411 zs->zil_itx_indirect_count.value.ui64 = 412 wmsum_value(&zil_sums->zil_itx_indirect_count); 413 zs->zil_itx_indirect_bytes.value.ui64 = 414 wmsum_value(&zil_sums->zil_itx_indirect_bytes); 415 zs->zil_itx_copied_count.value.ui64 = 416 wmsum_value(&zil_sums->zil_itx_copied_count); 417 zs->zil_itx_copied_bytes.value.ui64 = 418 wmsum_value(&zil_sums->zil_itx_copied_bytes); 419 zs->zil_itx_needcopy_count.value.ui64 = 420 wmsum_value(&zil_sums->zil_itx_needcopy_count); 421 zs->zil_itx_needcopy_bytes.value.ui64 = 422 wmsum_value(&zil_sums->zil_itx_needcopy_bytes); 423 zs->zil_itx_metaslab_normal_count.value.ui64 = 424 wmsum_value(&zil_sums->zil_itx_metaslab_normal_count); 425 zs->zil_itx_metaslab_normal_bytes.value.ui64 = 426 wmsum_value(&zil_sums->zil_itx_metaslab_normal_bytes); 427 zs->zil_itx_metaslab_normal_write.value.ui64 = 428 wmsum_value(&zil_sums->zil_itx_metaslab_normal_write); 429 zs->zil_itx_metaslab_normal_alloc.value.ui64 = 430 wmsum_value(&zil_sums->zil_itx_metaslab_normal_alloc); 431 zs->zil_itx_metaslab_slog_count.value.ui64 = 432 wmsum_value(&zil_sums->zil_itx_metaslab_slog_count); 433 zs->zil_itx_metaslab_slog_bytes.value.ui64 = 434 wmsum_value(&zil_sums->zil_itx_metaslab_slog_bytes); 435 zs->zil_itx_metaslab_slog_write.value.ui64 = 436 wmsum_value(&zil_sums->zil_itx_metaslab_slog_write); 437 zs->zil_itx_metaslab_slog_alloc.value.ui64 = 438 wmsum_value(&zil_sums->zil_itx_metaslab_slog_alloc); 439 } 440 441 /* 442 * Parse the intent log, and call parse_func for each valid record within. 443 */ 444 int 445 zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func, 446 zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg, 447 boolean_t decrypt) 448 { 449 const zil_header_t *zh = zilog->zl_header; 450 boolean_t claimed = !!zh->zh_claim_txg; 451 uint64_t claim_blk_seq = claimed ? zh->zh_claim_blk_seq : UINT64_MAX; 452 uint64_t claim_lr_seq = claimed ? zh->zh_claim_lr_seq : UINT64_MAX; 453 uint64_t max_blk_seq = 0; 454 uint64_t max_lr_seq = 0; 455 uint64_t blk_count = 0; 456 uint64_t lr_count = 0; 457 blkptr_t blk, next_blk = {{{{0}}}}; 458 int error = 0; 459 460 /* 461 * Old logs didn't record the maximum zh_claim_lr_seq. 462 */ 463 if (!(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID)) 464 claim_lr_seq = UINT64_MAX; 465 466 /* 467 * Starting at the block pointed to by zh_log we read the log chain. 468 * For each block in the chain we strongly check that block to 469 * ensure its validity. We stop when an invalid block is found. 470 * For each block pointer in the chain we call parse_blk_func(). 471 * For each record in each valid block we call parse_lr_func(). 472 * If the log has been claimed, stop if we encounter a sequence 473 * number greater than the highest claimed sequence number. 474 */ 475 zil_bp_tree_init(zilog); 476 477 for (blk = zh->zh_log; !BP_IS_HOLE(&blk); blk = next_blk) { 478 uint64_t blk_seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 479 int reclen; 480 char *lrp, *end; 481 arc_buf_t *abuf = NULL; 482 483 if (blk_seq > claim_blk_seq) 484 break; 485 486 error = parse_blk_func(zilog, &blk, arg, txg); 487 if (error != 0) 488 break; 489 ASSERT3U(max_blk_seq, <, blk_seq); 490 max_blk_seq = blk_seq; 491 blk_count++; 492 493 if (max_lr_seq == claim_lr_seq && max_blk_seq == claim_blk_seq) 494 break; 495 496 error = zil_read_log_block(zilog, decrypt, &blk, &next_blk, 497 &lrp, &end, &abuf); 498 if (error != 0) { 499 if (abuf) 500 arc_buf_destroy(abuf, &abuf); 501 if (claimed) { 502 char name[ZFS_MAX_DATASET_NAME_LEN]; 503 504 dmu_objset_name(zilog->zl_os, name); 505 506 cmn_err(CE_WARN, "ZFS read log block error %d, " 507 "dataset %s, seq 0x%llx\n", error, name, 508 (u_longlong_t)blk_seq); 509 } 510 break; 511 } 512 513 for (; lrp < end; lrp += reclen) { 514 lr_t *lr = (lr_t *)lrp; 515 reclen = lr->lrc_reclen; 516 ASSERT3U(reclen, >=, sizeof (lr_t)); 517 ASSERT3U(reclen, <=, end - lrp); 518 if (lr->lrc_seq > claim_lr_seq) { 519 arc_buf_destroy(abuf, &abuf); 520 goto done; 521 } 522 523 error = parse_lr_func(zilog, lr, arg, txg); 524 if (error != 0) { 525 arc_buf_destroy(abuf, &abuf); 526 goto done; 527 } 528 ASSERT3U(max_lr_seq, <, lr->lrc_seq); 529 max_lr_seq = lr->lrc_seq; 530 lr_count++; 531 } 532 arc_buf_destroy(abuf, &abuf); 533 } 534 done: 535 zilog->zl_parse_error = error; 536 zilog->zl_parse_blk_seq = max_blk_seq; 537 zilog->zl_parse_lr_seq = max_lr_seq; 538 zilog->zl_parse_blk_count = blk_count; 539 zilog->zl_parse_lr_count = lr_count; 540 541 zil_bp_tree_fini(zilog); 542 543 return (error); 544 } 545 546 static int 547 zil_clear_log_block(zilog_t *zilog, const blkptr_t *bp, void *tx, 548 uint64_t first_txg) 549 { 550 (void) tx; 551 ASSERT(!BP_IS_HOLE(bp)); 552 553 /* 554 * As we call this function from the context of a rewind to a 555 * checkpoint, each ZIL block whose txg is later than the txg 556 * that we rewind to is invalid. Thus, we return -1 so 557 * zil_parse() doesn't attempt to read it. 558 */ 559 if (bp->blk_birth >= first_txg) 560 return (-1); 561 562 if (zil_bp_tree_add(zilog, bp) != 0) 563 return (0); 564 565 zio_free(zilog->zl_spa, first_txg, bp); 566 return (0); 567 } 568 569 static int 570 zil_noop_log_record(zilog_t *zilog, const lr_t *lrc, void *tx, 571 uint64_t first_txg) 572 { 573 (void) zilog, (void) lrc, (void) tx, (void) first_txg; 574 return (0); 575 } 576 577 static int 578 zil_claim_log_block(zilog_t *zilog, const blkptr_t *bp, void *tx, 579 uint64_t first_txg) 580 { 581 /* 582 * Claim log block if not already committed and not already claimed. 583 * If tx == NULL, just verify that the block is claimable. 584 */ 585 if (BP_IS_HOLE(bp) || bp->blk_birth < first_txg || 586 zil_bp_tree_add(zilog, bp) != 0) 587 return (0); 588 589 return (zio_wait(zio_claim(NULL, zilog->zl_spa, 590 tx == NULL ? 0 : first_txg, bp, spa_claim_notify, NULL, 591 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB))); 592 } 593 594 static int 595 zil_claim_write(zilog_t *zilog, const lr_t *lrc, void *tx, uint64_t first_txg) 596 { 597 lr_write_t *lr = (lr_write_t *)lrc; 598 int error; 599 600 ASSERT3U(lrc->lrc_reclen, >=, sizeof (*lr)); 601 602 /* 603 * If the block is not readable, don't claim it. This can happen 604 * in normal operation when a log block is written to disk before 605 * some of the dmu_sync() blocks it points to. In this case, the 606 * transaction cannot have been committed to anyone (we would have 607 * waited for all writes to be stable first), so it is semantically 608 * correct to declare this the end of the log. 609 */ 610 if (lr->lr_blkptr.blk_birth >= first_txg) { 611 error = zil_read_log_data(zilog, lr, NULL); 612 if (error != 0) 613 return (error); 614 } 615 616 return (zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg)); 617 } 618 619 static int 620 zil_claim_clone_range(zilog_t *zilog, const lr_t *lrc, void *tx) 621 { 622 const lr_clone_range_t *lr = (const lr_clone_range_t *)lrc; 623 const blkptr_t *bp; 624 spa_t *spa; 625 uint_t ii; 626 627 ASSERT3U(lrc->lrc_reclen, >=, sizeof (*lr)); 628 ASSERT3U(lrc->lrc_reclen, >=, offsetof(lr_clone_range_t, 629 lr_bps[lr->lr_nbps])); 630 631 if (tx == NULL) { 632 return (0); 633 } 634 635 /* 636 * XXX: Do we need to byteswap lr? 637 */ 638 639 spa = zilog->zl_spa; 640 641 for (ii = 0; ii < lr->lr_nbps; ii++) { 642 bp = &lr->lr_bps[ii]; 643 644 /* 645 * When data in embedded into BP there is no need to create 646 * BRT entry as there is no data block. Just copy the BP as 647 * it contains the data. 648 */ 649 if (!BP_IS_HOLE(bp) && !BP_IS_EMBEDDED(bp)) { 650 brt_pending_add(spa, bp, tx); 651 } 652 } 653 654 return (0); 655 } 656 657 static int 658 zil_claim_log_record(zilog_t *zilog, const lr_t *lrc, void *tx, 659 uint64_t first_txg) 660 { 661 662 switch (lrc->lrc_txtype) { 663 case TX_WRITE: 664 return (zil_claim_write(zilog, lrc, tx, first_txg)); 665 case TX_CLONE_RANGE: 666 return (zil_claim_clone_range(zilog, lrc, tx)); 667 default: 668 return (0); 669 } 670 } 671 672 static int 673 zil_free_log_block(zilog_t *zilog, const blkptr_t *bp, void *tx, 674 uint64_t claim_txg) 675 { 676 (void) claim_txg; 677 678 zio_free(zilog->zl_spa, dmu_tx_get_txg(tx), bp); 679 680 return (0); 681 } 682 683 static int 684 zil_free_write(zilog_t *zilog, const lr_t *lrc, void *tx, uint64_t claim_txg) 685 { 686 lr_write_t *lr = (lr_write_t *)lrc; 687 blkptr_t *bp = &lr->lr_blkptr; 688 689 ASSERT3U(lrc->lrc_reclen, >=, sizeof (*lr)); 690 691 /* 692 * If we previously claimed it, we need to free it. 693 */ 694 if (bp->blk_birth >= claim_txg && zil_bp_tree_add(zilog, bp) == 0 && 695 !BP_IS_HOLE(bp)) { 696 zio_free(zilog->zl_spa, dmu_tx_get_txg(tx), bp); 697 } 698 699 return (0); 700 } 701 702 static int 703 zil_free_clone_range(zilog_t *zilog, const lr_t *lrc, void *tx) 704 { 705 const lr_clone_range_t *lr = (const lr_clone_range_t *)lrc; 706 const blkptr_t *bp; 707 spa_t *spa; 708 uint_t ii; 709 710 ASSERT3U(lrc->lrc_reclen, >=, sizeof (*lr)); 711 ASSERT3U(lrc->lrc_reclen, >=, offsetof(lr_clone_range_t, 712 lr_bps[lr->lr_nbps])); 713 714 if (tx == NULL) { 715 return (0); 716 } 717 718 spa = zilog->zl_spa; 719 720 for (ii = 0; ii < lr->lr_nbps; ii++) { 721 bp = &lr->lr_bps[ii]; 722 723 if (!BP_IS_HOLE(bp)) { 724 zio_free(spa, dmu_tx_get_txg(tx), bp); 725 } 726 } 727 728 return (0); 729 } 730 731 static int 732 zil_free_log_record(zilog_t *zilog, const lr_t *lrc, void *tx, 733 uint64_t claim_txg) 734 { 735 736 if (claim_txg == 0) { 737 return (0); 738 } 739 740 switch (lrc->lrc_txtype) { 741 case TX_WRITE: 742 return (zil_free_write(zilog, lrc, tx, claim_txg)); 743 case TX_CLONE_RANGE: 744 return (zil_free_clone_range(zilog, lrc, tx)); 745 default: 746 return (0); 747 } 748 } 749 750 static int 751 zil_lwb_vdev_compare(const void *x1, const void *x2) 752 { 753 const uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev; 754 const uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev; 755 756 return (TREE_CMP(v1, v2)); 757 } 758 759 /* 760 * Allocate a new lwb. We may already have a block pointer for it, in which 761 * case we get size and version from there. Or we may not yet, in which case 762 * we choose them here and later make the block allocation match. 763 */ 764 static lwb_t * 765 zil_alloc_lwb(zilog_t *zilog, int sz, blkptr_t *bp, boolean_t slog, 766 uint64_t txg, lwb_state_t state) 767 { 768 lwb_t *lwb; 769 770 lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 771 lwb->lwb_zilog = zilog; 772 if (bp) { 773 lwb->lwb_blk = *bp; 774 lwb->lwb_slim = (BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_ZILOG2); 775 sz = BP_GET_LSIZE(bp); 776 } else { 777 BP_ZERO(&lwb->lwb_blk); 778 lwb->lwb_slim = (spa_version(zilog->zl_spa) >= 779 SPA_VERSION_SLIM_ZIL); 780 } 781 lwb->lwb_slog = slog; 782 lwb->lwb_error = 0; 783 if (lwb->lwb_slim) { 784 lwb->lwb_nmax = sz; 785 lwb->lwb_nused = lwb->lwb_nfilled = sizeof (zil_chain_t); 786 } else { 787 lwb->lwb_nmax = sz - sizeof (zil_chain_t); 788 lwb->lwb_nused = lwb->lwb_nfilled = 0; 789 } 790 lwb->lwb_sz = sz; 791 lwb->lwb_state = state; 792 lwb->lwb_buf = zio_buf_alloc(sz); 793 lwb->lwb_child_zio = NULL; 794 lwb->lwb_write_zio = NULL; 795 lwb->lwb_root_zio = NULL; 796 lwb->lwb_issued_timestamp = 0; 797 lwb->lwb_issued_txg = 0; 798 lwb->lwb_alloc_txg = txg; 799 lwb->lwb_max_txg = 0; 800 801 mutex_enter(&zilog->zl_lock); 802 list_insert_tail(&zilog->zl_lwb_list, lwb); 803 if (state != LWB_STATE_NEW) 804 zilog->zl_last_lwb_opened = lwb; 805 mutex_exit(&zilog->zl_lock); 806 807 return (lwb); 808 } 809 810 static void 811 zil_free_lwb(zilog_t *zilog, lwb_t *lwb) 812 { 813 ASSERT(MUTEX_HELD(&zilog->zl_lock)); 814 ASSERT(lwb->lwb_state == LWB_STATE_NEW || 815 lwb->lwb_state == LWB_STATE_FLUSH_DONE); 816 ASSERT3P(lwb->lwb_child_zio, ==, NULL); 817 ASSERT3P(lwb->lwb_write_zio, ==, NULL); 818 ASSERT3P(lwb->lwb_root_zio, ==, NULL); 819 ASSERT3U(lwb->lwb_alloc_txg, <=, spa_syncing_txg(zilog->zl_spa)); 820 ASSERT3U(lwb->lwb_max_txg, <=, spa_syncing_txg(zilog->zl_spa)); 821 VERIFY(list_is_empty(&lwb->lwb_itxs)); 822 VERIFY(list_is_empty(&lwb->lwb_waiters)); 823 ASSERT(avl_is_empty(&lwb->lwb_vdev_tree)); 824 ASSERT(!MUTEX_HELD(&lwb->lwb_vdev_lock)); 825 826 /* 827 * Clear the zilog's field to indicate this lwb is no longer 828 * valid, and prevent use-after-free errors. 829 */ 830 if (zilog->zl_last_lwb_opened == lwb) 831 zilog->zl_last_lwb_opened = NULL; 832 833 kmem_cache_free(zil_lwb_cache, lwb); 834 } 835 836 /* 837 * Called when we create in-memory log transactions so that we know 838 * to cleanup the itxs at the end of spa_sync(). 839 */ 840 static void 841 zilog_dirty(zilog_t *zilog, uint64_t txg) 842 { 843 dsl_pool_t *dp = zilog->zl_dmu_pool; 844 dsl_dataset_t *ds = dmu_objset_ds(zilog->zl_os); 845 846 ASSERT(spa_writeable(zilog->zl_spa)); 847 848 if (ds->ds_is_snapshot) 849 panic("dirtying snapshot!"); 850 851 if (txg_list_add(&dp->dp_dirty_zilogs, zilog, txg)) { 852 /* up the hold count until we can be written out */ 853 dmu_buf_add_ref(ds->ds_dbuf, zilog); 854 855 zilog->zl_dirty_max_txg = MAX(txg, zilog->zl_dirty_max_txg); 856 } 857 } 858 859 /* 860 * Determine if the zil is dirty in the specified txg. Callers wanting to 861 * ensure that the dirty state does not change must hold the itxg_lock for 862 * the specified txg. Holding the lock will ensure that the zil cannot be 863 * dirtied (zil_itx_assign) or cleaned (zil_clean) while we check its current 864 * state. 865 */ 866 static boolean_t __maybe_unused 867 zilog_is_dirty_in_txg(zilog_t *zilog, uint64_t txg) 868 { 869 dsl_pool_t *dp = zilog->zl_dmu_pool; 870 871 if (txg_list_member(&dp->dp_dirty_zilogs, zilog, txg & TXG_MASK)) 872 return (B_TRUE); 873 return (B_FALSE); 874 } 875 876 /* 877 * Determine if the zil is dirty. The zil is considered dirty if it has 878 * any pending itx records that have not been cleaned by zil_clean(). 879 */ 880 static boolean_t 881 zilog_is_dirty(zilog_t *zilog) 882 { 883 dsl_pool_t *dp = zilog->zl_dmu_pool; 884 885 for (int t = 0; t < TXG_SIZE; t++) { 886 if (txg_list_member(&dp->dp_dirty_zilogs, zilog, t)) 887 return (B_TRUE); 888 } 889 return (B_FALSE); 890 } 891 892 /* 893 * Its called in zil_commit context (zil_process_commit_list()/zil_create()). 894 * It activates SPA_FEATURE_ZILSAXATTR feature, if its enabled. 895 * Check dsl_dataset_feature_is_active to avoid txg_wait_synced() on every 896 * zil_commit. 897 */ 898 static void 899 zil_commit_activate_saxattr_feature(zilog_t *zilog) 900 { 901 dsl_dataset_t *ds = dmu_objset_ds(zilog->zl_os); 902 uint64_t txg = 0; 903 dmu_tx_t *tx = NULL; 904 905 if (spa_feature_is_enabled(zilog->zl_spa, SPA_FEATURE_ZILSAXATTR) && 906 dmu_objset_type(zilog->zl_os) != DMU_OST_ZVOL && 907 !dsl_dataset_feature_is_active(ds, SPA_FEATURE_ZILSAXATTR)) { 908 tx = dmu_tx_create(zilog->zl_os); 909 VERIFY0(dmu_tx_assign(tx, TXG_WAIT)); 910 dsl_dataset_dirty(ds, tx); 911 txg = dmu_tx_get_txg(tx); 912 913 mutex_enter(&ds->ds_lock); 914 ds->ds_feature_activation[SPA_FEATURE_ZILSAXATTR] = 915 (void *)B_TRUE; 916 mutex_exit(&ds->ds_lock); 917 dmu_tx_commit(tx); 918 txg_wait_synced(zilog->zl_dmu_pool, txg); 919 } 920 } 921 922 /* 923 * Create an on-disk intent log. 924 */ 925 static lwb_t * 926 zil_create(zilog_t *zilog) 927 { 928 const zil_header_t *zh = zilog->zl_header; 929 lwb_t *lwb = NULL; 930 uint64_t txg = 0; 931 dmu_tx_t *tx = NULL; 932 blkptr_t blk; 933 int error = 0; 934 boolean_t slog = FALSE; 935 dsl_dataset_t *ds = dmu_objset_ds(zilog->zl_os); 936 937 938 /* 939 * Wait for any previous destroy to complete. 940 */ 941 txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 942 943 ASSERT(zh->zh_claim_txg == 0); 944 ASSERT(zh->zh_replay_seq == 0); 945 946 blk = zh->zh_log; 947 948 /* 949 * Allocate an initial log block if: 950 * - there isn't one already 951 * - the existing block is the wrong endianness 952 */ 953 if (BP_IS_HOLE(&blk) || BP_SHOULD_BYTESWAP(&blk)) { 954 tx = dmu_tx_create(zilog->zl_os); 955 VERIFY0(dmu_tx_assign(tx, TXG_WAIT)); 956 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 957 txg = dmu_tx_get_txg(tx); 958 959 if (!BP_IS_HOLE(&blk)) { 960 zio_free(zilog->zl_spa, txg, &blk); 961 BP_ZERO(&blk); 962 } 963 964 error = zio_alloc_zil(zilog->zl_spa, zilog->zl_os, txg, &blk, 965 ZIL_MIN_BLKSZ, &slog); 966 if (error == 0) 967 zil_init_log_chain(zilog, &blk); 968 } 969 970 /* 971 * Allocate a log write block (lwb) for the first log block. 972 */ 973 if (error == 0) 974 lwb = zil_alloc_lwb(zilog, 0, &blk, slog, txg, LWB_STATE_NEW); 975 976 /* 977 * If we just allocated the first log block, commit our transaction 978 * and wait for zil_sync() to stuff the block pointer into zh_log. 979 * (zh is part of the MOS, so we cannot modify it in open context.) 980 */ 981 if (tx != NULL) { 982 /* 983 * If "zilsaxattr" feature is enabled on zpool, then activate 984 * it now when we're creating the ZIL chain. We can't wait with 985 * this until we write the first xattr log record because we 986 * need to wait for the feature activation to sync out. 987 */ 988 if (spa_feature_is_enabled(zilog->zl_spa, 989 SPA_FEATURE_ZILSAXATTR) && dmu_objset_type(zilog->zl_os) != 990 DMU_OST_ZVOL) { 991 mutex_enter(&ds->ds_lock); 992 ds->ds_feature_activation[SPA_FEATURE_ZILSAXATTR] = 993 (void *)B_TRUE; 994 mutex_exit(&ds->ds_lock); 995 } 996 997 dmu_tx_commit(tx); 998 txg_wait_synced(zilog->zl_dmu_pool, txg); 999 } else { 1000 /* 1001 * This branch covers the case where we enable the feature on a 1002 * zpool that has existing ZIL headers. 1003 */ 1004 zil_commit_activate_saxattr_feature(zilog); 1005 } 1006 IMPLY(spa_feature_is_enabled(zilog->zl_spa, SPA_FEATURE_ZILSAXATTR) && 1007 dmu_objset_type(zilog->zl_os) != DMU_OST_ZVOL, 1008 dsl_dataset_feature_is_active(ds, SPA_FEATURE_ZILSAXATTR)); 1009 1010 ASSERT(error != 0 || memcmp(&blk, &zh->zh_log, sizeof (blk)) == 0); 1011 IMPLY(error == 0, lwb != NULL); 1012 1013 return (lwb); 1014 } 1015 1016 /* 1017 * In one tx, free all log blocks and clear the log header. If keep_first 1018 * is set, then we're replaying a log with no content. We want to keep the 1019 * first block, however, so that the first synchronous transaction doesn't 1020 * require a txg_wait_synced() in zil_create(). We don't need to 1021 * txg_wait_synced() here either when keep_first is set, because both 1022 * zil_create() and zil_destroy() will wait for any in-progress destroys 1023 * to complete. 1024 * Return B_TRUE if there were any entries to replay. 1025 */ 1026 boolean_t 1027 zil_destroy(zilog_t *zilog, boolean_t keep_first) 1028 { 1029 const zil_header_t *zh = zilog->zl_header; 1030 lwb_t *lwb; 1031 dmu_tx_t *tx; 1032 uint64_t txg; 1033 1034 /* 1035 * Wait for any previous destroy to complete. 1036 */ 1037 txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 1038 1039 zilog->zl_old_header = *zh; /* debugging aid */ 1040 1041 if (BP_IS_HOLE(&zh->zh_log)) 1042 return (B_FALSE); 1043 1044 tx = dmu_tx_create(zilog->zl_os); 1045 VERIFY0(dmu_tx_assign(tx, TXG_WAIT)); 1046 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 1047 txg = dmu_tx_get_txg(tx); 1048 1049 mutex_enter(&zilog->zl_lock); 1050 1051 ASSERT3U(zilog->zl_destroy_txg, <, txg); 1052 zilog->zl_destroy_txg = txg; 1053 zilog->zl_keep_first = keep_first; 1054 1055 if (!list_is_empty(&zilog->zl_lwb_list)) { 1056 ASSERT(zh->zh_claim_txg == 0); 1057 VERIFY(!keep_first); 1058 while ((lwb = list_remove_head(&zilog->zl_lwb_list)) != NULL) { 1059 if (lwb->lwb_buf != NULL) 1060 zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 1061 if (!BP_IS_HOLE(&lwb->lwb_blk)) 1062 zio_free(zilog->zl_spa, txg, &lwb->lwb_blk); 1063 zil_free_lwb(zilog, lwb); 1064 } 1065 } else if (!keep_first) { 1066 zil_destroy_sync(zilog, tx); 1067 } 1068 mutex_exit(&zilog->zl_lock); 1069 1070 dmu_tx_commit(tx); 1071 1072 return (B_TRUE); 1073 } 1074 1075 void 1076 zil_destroy_sync(zilog_t *zilog, dmu_tx_t *tx) 1077 { 1078 ASSERT(list_is_empty(&zilog->zl_lwb_list)); 1079 (void) zil_parse(zilog, zil_free_log_block, 1080 zil_free_log_record, tx, zilog->zl_header->zh_claim_txg, B_FALSE); 1081 } 1082 1083 int 1084 zil_claim(dsl_pool_t *dp, dsl_dataset_t *ds, void *txarg) 1085 { 1086 dmu_tx_t *tx = txarg; 1087 zilog_t *zilog; 1088 uint64_t first_txg; 1089 zil_header_t *zh; 1090 objset_t *os; 1091 int error; 1092 1093 error = dmu_objset_own_obj(dp, ds->ds_object, 1094 DMU_OST_ANY, B_FALSE, B_FALSE, FTAG, &os); 1095 if (error != 0) { 1096 /* 1097 * EBUSY indicates that the objset is inconsistent, in which 1098 * case it can not have a ZIL. 1099 */ 1100 if (error != EBUSY) { 1101 cmn_err(CE_WARN, "can't open objset for %llu, error %u", 1102 (unsigned long long)ds->ds_object, error); 1103 } 1104 1105 return (0); 1106 } 1107 1108 zilog = dmu_objset_zil(os); 1109 zh = zil_header_in_syncing_context(zilog); 1110 ASSERT3U(tx->tx_txg, ==, spa_first_txg(zilog->zl_spa)); 1111 first_txg = spa_min_claim_txg(zilog->zl_spa); 1112 1113 /* 1114 * If the spa_log_state is not set to be cleared, check whether 1115 * the current uberblock is a checkpoint one and if the current 1116 * header has been claimed before moving on. 1117 * 1118 * If the current uberblock is a checkpointed uberblock then 1119 * one of the following scenarios took place: 1120 * 1121 * 1] We are currently rewinding to the checkpoint of the pool. 1122 * 2] We crashed in the middle of a checkpoint rewind but we 1123 * did manage to write the checkpointed uberblock to the 1124 * vdev labels, so when we tried to import the pool again 1125 * the checkpointed uberblock was selected from the import 1126 * procedure. 1127 * 1128 * In both cases we want to zero out all the ZIL blocks, except 1129 * the ones that have been claimed at the time of the checkpoint 1130 * (their zh_claim_txg != 0). The reason is that these blocks 1131 * may be corrupted since we may have reused their locations on 1132 * disk after we took the checkpoint. 1133 * 1134 * We could try to set spa_log_state to SPA_LOG_CLEAR earlier 1135 * when we first figure out whether the current uberblock is 1136 * checkpointed or not. Unfortunately, that would discard all 1137 * the logs, including the ones that are claimed, and we would 1138 * leak space. 1139 */ 1140 if (spa_get_log_state(zilog->zl_spa) == SPA_LOG_CLEAR || 1141 (zilog->zl_spa->spa_uberblock.ub_checkpoint_txg != 0 && 1142 zh->zh_claim_txg == 0)) { 1143 if (!BP_IS_HOLE(&zh->zh_log)) { 1144 (void) zil_parse(zilog, zil_clear_log_block, 1145 zil_noop_log_record, tx, first_txg, B_FALSE); 1146 } 1147 BP_ZERO(&zh->zh_log); 1148 if (os->os_encrypted) 1149 os->os_next_write_raw[tx->tx_txg & TXG_MASK] = B_TRUE; 1150 dsl_dataset_dirty(dmu_objset_ds(os), tx); 1151 dmu_objset_disown(os, B_FALSE, FTAG); 1152 return (0); 1153 } 1154 1155 /* 1156 * If we are not rewinding and opening the pool normally, then 1157 * the min_claim_txg should be equal to the first txg of the pool. 1158 */ 1159 ASSERT3U(first_txg, ==, spa_first_txg(zilog->zl_spa)); 1160 1161 /* 1162 * Claim all log blocks if we haven't already done so, and remember 1163 * the highest claimed sequence number. This ensures that if we can 1164 * read only part of the log now (e.g. due to a missing device), 1165 * but we can read the entire log later, we will not try to replay 1166 * or destroy beyond the last block we successfully claimed. 1167 */ 1168 ASSERT3U(zh->zh_claim_txg, <=, first_txg); 1169 if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) { 1170 (void) zil_parse(zilog, zil_claim_log_block, 1171 zil_claim_log_record, tx, first_txg, B_FALSE); 1172 zh->zh_claim_txg = first_txg; 1173 zh->zh_claim_blk_seq = zilog->zl_parse_blk_seq; 1174 zh->zh_claim_lr_seq = zilog->zl_parse_lr_seq; 1175 if (zilog->zl_parse_lr_count || zilog->zl_parse_blk_count > 1) 1176 zh->zh_flags |= ZIL_REPLAY_NEEDED; 1177 zh->zh_flags |= ZIL_CLAIM_LR_SEQ_VALID; 1178 if (os->os_encrypted) 1179 os->os_next_write_raw[tx->tx_txg & TXG_MASK] = B_TRUE; 1180 dsl_dataset_dirty(dmu_objset_ds(os), tx); 1181 } 1182 1183 ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1)); 1184 dmu_objset_disown(os, B_FALSE, FTAG); 1185 return (0); 1186 } 1187 1188 /* 1189 * Check the log by walking the log chain. 1190 * Checksum errors are ok as they indicate the end of the chain. 1191 * Any other error (no device or read failure) returns an error. 1192 */ 1193 int 1194 zil_check_log_chain(dsl_pool_t *dp, dsl_dataset_t *ds, void *tx) 1195 { 1196 (void) dp; 1197 zilog_t *zilog; 1198 objset_t *os; 1199 blkptr_t *bp; 1200 int error; 1201 1202 ASSERT(tx == NULL); 1203 1204 error = dmu_objset_from_ds(ds, &os); 1205 if (error != 0) { 1206 cmn_err(CE_WARN, "can't open objset %llu, error %d", 1207 (unsigned long long)ds->ds_object, error); 1208 return (0); 1209 } 1210 1211 zilog = dmu_objset_zil(os); 1212 bp = (blkptr_t *)&zilog->zl_header->zh_log; 1213 1214 if (!BP_IS_HOLE(bp)) { 1215 vdev_t *vd; 1216 boolean_t valid = B_TRUE; 1217 1218 /* 1219 * Check the first block and determine if it's on a log device 1220 * which may have been removed or faulted prior to loading this 1221 * pool. If so, there's no point in checking the rest of the 1222 * log as its content should have already been synced to the 1223 * pool. 1224 */ 1225 spa_config_enter(os->os_spa, SCL_STATE, FTAG, RW_READER); 1226 vd = vdev_lookup_top(os->os_spa, DVA_GET_VDEV(&bp->blk_dva[0])); 1227 if (vd->vdev_islog && vdev_is_dead(vd)) 1228 valid = vdev_log_state_valid(vd); 1229 spa_config_exit(os->os_spa, SCL_STATE, FTAG); 1230 1231 if (!valid) 1232 return (0); 1233 1234 /* 1235 * Check whether the current uberblock is checkpointed (e.g. 1236 * we are rewinding) and whether the current header has been 1237 * claimed or not. If it hasn't then skip verifying it. We 1238 * do this because its ZIL blocks may be part of the pool's 1239 * state before the rewind, which is no longer valid. 1240 */ 1241 zil_header_t *zh = zil_header_in_syncing_context(zilog); 1242 if (zilog->zl_spa->spa_uberblock.ub_checkpoint_txg != 0 && 1243 zh->zh_claim_txg == 0) 1244 return (0); 1245 } 1246 1247 /* 1248 * Because tx == NULL, zil_claim_log_block() will not actually claim 1249 * any blocks, but just determine whether it is possible to do so. 1250 * In addition to checking the log chain, zil_claim_log_block() 1251 * will invoke zio_claim() with a done func of spa_claim_notify(), 1252 * which will update spa_max_claim_txg. See spa_load() for details. 1253 */ 1254 error = zil_parse(zilog, zil_claim_log_block, zil_claim_log_record, tx, 1255 zilog->zl_header->zh_claim_txg ? -1ULL : 1256 spa_min_claim_txg(os->os_spa), B_FALSE); 1257 1258 return ((error == ECKSUM || error == ENOENT) ? 0 : error); 1259 } 1260 1261 /* 1262 * When an itx is "skipped", this function is used to properly mark the 1263 * waiter as "done, and signal any thread(s) waiting on it. An itx can 1264 * be skipped (and not committed to an lwb) for a variety of reasons, 1265 * one of them being that the itx was committed via spa_sync(), prior to 1266 * it being committed to an lwb; this can happen if a thread calling 1267 * zil_commit() is racing with spa_sync(). 1268 */ 1269 static void 1270 zil_commit_waiter_skip(zil_commit_waiter_t *zcw) 1271 { 1272 mutex_enter(&zcw->zcw_lock); 1273 ASSERT3B(zcw->zcw_done, ==, B_FALSE); 1274 zcw->zcw_done = B_TRUE; 1275 cv_broadcast(&zcw->zcw_cv); 1276 mutex_exit(&zcw->zcw_lock); 1277 } 1278 1279 /* 1280 * This function is used when the given waiter is to be linked into an 1281 * lwb's "lwb_waiter" list; i.e. when the itx is committed to the lwb. 1282 * At this point, the waiter will no longer be referenced by the itx, 1283 * and instead, will be referenced by the lwb. 1284 */ 1285 static void 1286 zil_commit_waiter_link_lwb(zil_commit_waiter_t *zcw, lwb_t *lwb) 1287 { 1288 /* 1289 * The lwb_waiters field of the lwb is protected by the zilog's 1290 * zl_issuer_lock while the lwb is open and zl_lock otherwise. 1291 * zl_issuer_lock also protects leaving the open state. 1292 * zcw_lwb setting is protected by zl_issuer_lock and state != 1293 * flush_done, which transition is protected by zl_lock. 1294 */ 1295 ASSERT(MUTEX_HELD(&lwb->lwb_zilog->zl_issuer_lock)); 1296 IMPLY(lwb->lwb_state != LWB_STATE_OPENED, 1297 MUTEX_HELD(&lwb->lwb_zilog->zl_lock)); 1298 ASSERT3S(lwb->lwb_state, !=, LWB_STATE_NEW); 1299 ASSERT3S(lwb->lwb_state, !=, LWB_STATE_FLUSH_DONE); 1300 1301 ASSERT(!list_link_active(&zcw->zcw_node)); 1302 list_insert_tail(&lwb->lwb_waiters, zcw); 1303 ASSERT3P(zcw->zcw_lwb, ==, NULL); 1304 zcw->zcw_lwb = lwb; 1305 } 1306 1307 /* 1308 * This function is used when zio_alloc_zil() fails to allocate a ZIL 1309 * block, and the given waiter must be linked to the "nolwb waiters" 1310 * list inside of zil_process_commit_list(). 1311 */ 1312 static void 1313 zil_commit_waiter_link_nolwb(zil_commit_waiter_t *zcw, list_t *nolwb) 1314 { 1315 ASSERT(!list_link_active(&zcw->zcw_node)); 1316 list_insert_tail(nolwb, zcw); 1317 ASSERT3P(zcw->zcw_lwb, ==, NULL); 1318 } 1319 1320 void 1321 zil_lwb_add_block(lwb_t *lwb, const blkptr_t *bp) 1322 { 1323 avl_tree_t *t = &lwb->lwb_vdev_tree; 1324 avl_index_t where; 1325 zil_vdev_node_t *zv, zvsearch; 1326 int ndvas = BP_GET_NDVAS(bp); 1327 int i; 1328 1329 ASSERT3S(lwb->lwb_state, !=, LWB_STATE_WRITE_DONE); 1330 ASSERT3S(lwb->lwb_state, !=, LWB_STATE_FLUSH_DONE); 1331 1332 if (zil_nocacheflush) 1333 return; 1334 1335 mutex_enter(&lwb->lwb_vdev_lock); 1336 for (i = 0; i < ndvas; i++) { 1337 zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]); 1338 if (avl_find(t, &zvsearch, &where) == NULL) { 1339 zv = kmem_alloc(sizeof (*zv), KM_SLEEP); 1340 zv->zv_vdev = zvsearch.zv_vdev; 1341 avl_insert(t, zv, where); 1342 } 1343 } 1344 mutex_exit(&lwb->lwb_vdev_lock); 1345 } 1346 1347 static void 1348 zil_lwb_flush_defer(lwb_t *lwb, lwb_t *nlwb) 1349 { 1350 avl_tree_t *src = &lwb->lwb_vdev_tree; 1351 avl_tree_t *dst = &nlwb->lwb_vdev_tree; 1352 void *cookie = NULL; 1353 zil_vdev_node_t *zv; 1354 1355 ASSERT3S(lwb->lwb_state, ==, LWB_STATE_WRITE_DONE); 1356 ASSERT3S(nlwb->lwb_state, !=, LWB_STATE_WRITE_DONE); 1357 ASSERT3S(nlwb->lwb_state, !=, LWB_STATE_FLUSH_DONE); 1358 1359 /* 1360 * While 'lwb' is at a point in its lifetime where lwb_vdev_tree does 1361 * not need the protection of lwb_vdev_lock (it will only be modified 1362 * while holding zilog->zl_lock) as its writes and those of its 1363 * children have all completed. The younger 'nlwb' may be waiting on 1364 * future writes to additional vdevs. 1365 */ 1366 mutex_enter(&nlwb->lwb_vdev_lock); 1367 /* 1368 * Tear down the 'lwb' vdev tree, ensuring that entries which do not 1369 * exist in 'nlwb' are moved to it, freeing any would-be duplicates. 1370 */ 1371 while ((zv = avl_destroy_nodes(src, &cookie)) != NULL) { 1372 avl_index_t where; 1373 1374 if (avl_find(dst, zv, &where) == NULL) { 1375 avl_insert(dst, zv, where); 1376 } else { 1377 kmem_free(zv, sizeof (*zv)); 1378 } 1379 } 1380 mutex_exit(&nlwb->lwb_vdev_lock); 1381 } 1382 1383 void 1384 zil_lwb_add_txg(lwb_t *lwb, uint64_t txg) 1385 { 1386 lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg); 1387 } 1388 1389 /* 1390 * This function is a called after all vdevs associated with a given lwb 1391 * write have completed their DKIOCFLUSHWRITECACHE command; or as soon 1392 * as the lwb write completes, if "zil_nocacheflush" is set. Further, 1393 * all "previous" lwb's will have completed before this function is 1394 * called; i.e. this function is called for all previous lwbs before 1395 * it's called for "this" lwb (enforced via zio the dependencies 1396 * configured in zil_lwb_set_zio_dependency()). 1397 * 1398 * The intention is for this function to be called as soon as the 1399 * contents of an lwb are considered "stable" on disk, and will survive 1400 * any sudden loss of power. At this point, any threads waiting for the 1401 * lwb to reach this state are signalled, and the "waiter" structures 1402 * are marked "done". 1403 */ 1404 static void 1405 zil_lwb_flush_vdevs_done(zio_t *zio) 1406 { 1407 lwb_t *lwb = zio->io_private; 1408 zilog_t *zilog = lwb->lwb_zilog; 1409 zil_commit_waiter_t *zcw; 1410 itx_t *itx; 1411 1412 spa_config_exit(zilog->zl_spa, SCL_STATE, lwb); 1413 1414 hrtime_t t = gethrtime() - lwb->lwb_issued_timestamp; 1415 1416 mutex_enter(&zilog->zl_lock); 1417 1418 zilog->zl_last_lwb_latency = (zilog->zl_last_lwb_latency * 7 + t) / 8; 1419 1420 lwb->lwb_root_zio = NULL; 1421 1422 ASSERT3S(lwb->lwb_state, ==, LWB_STATE_WRITE_DONE); 1423 lwb->lwb_state = LWB_STATE_FLUSH_DONE; 1424 1425 if (zilog->zl_last_lwb_opened == lwb) { 1426 /* 1427 * Remember the highest committed log sequence number 1428 * for ztest. We only update this value when all the log 1429 * writes succeeded, because ztest wants to ASSERT that 1430 * it got the whole log chain. 1431 */ 1432 zilog->zl_commit_lr_seq = zilog->zl_lr_seq; 1433 } 1434 1435 while ((itx = list_remove_head(&lwb->lwb_itxs)) != NULL) 1436 zil_itx_destroy(itx); 1437 1438 while ((zcw = list_remove_head(&lwb->lwb_waiters)) != NULL) { 1439 mutex_enter(&zcw->zcw_lock); 1440 1441 ASSERT3P(zcw->zcw_lwb, ==, lwb); 1442 zcw->zcw_lwb = NULL; 1443 /* 1444 * We expect any ZIO errors from child ZIOs to have been 1445 * propagated "up" to this specific LWB's root ZIO, in 1446 * order for this error handling to work correctly. This 1447 * includes ZIO errors from either this LWB's write or 1448 * flush, as well as any errors from other dependent LWBs 1449 * (e.g. a root LWB ZIO that might be a child of this LWB). 1450 * 1451 * With that said, it's important to note that LWB flush 1452 * errors are not propagated up to the LWB root ZIO. 1453 * This is incorrect behavior, and results in VDEV flush 1454 * errors not being handled correctly here. See the 1455 * comment above the call to "zio_flush" for details. 1456 */ 1457 1458 zcw->zcw_zio_error = zio->io_error; 1459 1460 ASSERT3B(zcw->zcw_done, ==, B_FALSE); 1461 zcw->zcw_done = B_TRUE; 1462 cv_broadcast(&zcw->zcw_cv); 1463 1464 mutex_exit(&zcw->zcw_lock); 1465 } 1466 1467 uint64_t txg = lwb->lwb_issued_txg; 1468 1469 /* Once we drop the lock, lwb may be freed by zil_sync(). */ 1470 mutex_exit(&zilog->zl_lock); 1471 1472 mutex_enter(&zilog->zl_lwb_io_lock); 1473 ASSERT3U(zilog->zl_lwb_inflight[txg & TXG_MASK], >, 0); 1474 zilog->zl_lwb_inflight[txg & TXG_MASK]--; 1475 if (zilog->zl_lwb_inflight[txg & TXG_MASK] == 0) 1476 cv_broadcast(&zilog->zl_lwb_io_cv); 1477 mutex_exit(&zilog->zl_lwb_io_lock); 1478 } 1479 1480 /* 1481 * Wait for the completion of all issued write/flush of that txg provided. 1482 * It guarantees zil_lwb_flush_vdevs_done() is called and returned. 1483 */ 1484 static void 1485 zil_lwb_flush_wait_all(zilog_t *zilog, uint64_t txg) 1486 { 1487 ASSERT3U(txg, ==, spa_syncing_txg(zilog->zl_spa)); 1488 1489 mutex_enter(&zilog->zl_lwb_io_lock); 1490 while (zilog->zl_lwb_inflight[txg & TXG_MASK] > 0) 1491 cv_wait(&zilog->zl_lwb_io_cv, &zilog->zl_lwb_io_lock); 1492 mutex_exit(&zilog->zl_lwb_io_lock); 1493 1494 #ifdef ZFS_DEBUG 1495 mutex_enter(&zilog->zl_lock); 1496 mutex_enter(&zilog->zl_lwb_io_lock); 1497 lwb_t *lwb = list_head(&zilog->zl_lwb_list); 1498 while (lwb != NULL) { 1499 if (lwb->lwb_issued_txg <= txg) { 1500 ASSERT(lwb->lwb_state != LWB_STATE_ISSUED); 1501 ASSERT(lwb->lwb_state != LWB_STATE_WRITE_DONE); 1502 IMPLY(lwb->lwb_issued_txg > 0, 1503 lwb->lwb_state == LWB_STATE_FLUSH_DONE); 1504 } 1505 IMPLY(lwb->lwb_state == LWB_STATE_WRITE_DONE || 1506 lwb->lwb_state == LWB_STATE_FLUSH_DONE, 1507 lwb->lwb_buf == NULL); 1508 lwb = list_next(&zilog->zl_lwb_list, lwb); 1509 } 1510 mutex_exit(&zilog->zl_lwb_io_lock); 1511 mutex_exit(&zilog->zl_lock); 1512 #endif 1513 } 1514 1515 /* 1516 * This is called when an lwb's write zio completes. The callback's 1517 * purpose is to issue the DKIOCFLUSHWRITECACHE commands for the vdevs 1518 * in the lwb's lwb_vdev_tree. The tree will contain the vdevs involved 1519 * in writing out this specific lwb's data, and in the case that cache 1520 * flushes have been deferred, vdevs involved in writing the data for 1521 * previous lwbs. The writes corresponding to all the vdevs in the 1522 * lwb_vdev_tree will have completed by the time this is called, due to 1523 * the zio dependencies configured in zil_lwb_set_zio_dependency(), 1524 * which takes deferred flushes into account. The lwb will be "done" 1525 * once zil_lwb_flush_vdevs_done() is called, which occurs in the zio 1526 * completion callback for the lwb's root zio. 1527 */ 1528 static void 1529 zil_lwb_write_done(zio_t *zio) 1530 { 1531 lwb_t *lwb = zio->io_private; 1532 spa_t *spa = zio->io_spa; 1533 zilog_t *zilog = lwb->lwb_zilog; 1534 avl_tree_t *t = &lwb->lwb_vdev_tree; 1535 void *cookie = NULL; 1536 zil_vdev_node_t *zv; 1537 lwb_t *nlwb; 1538 1539 ASSERT3S(spa_config_held(spa, SCL_STATE, RW_READER), !=, 0); 1540 1541 abd_free(zio->io_abd); 1542 zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 1543 lwb->lwb_buf = NULL; 1544 1545 mutex_enter(&zilog->zl_lock); 1546 ASSERT3S(lwb->lwb_state, ==, LWB_STATE_ISSUED); 1547 lwb->lwb_state = LWB_STATE_WRITE_DONE; 1548 lwb->lwb_child_zio = NULL; 1549 lwb->lwb_write_zio = NULL; 1550 1551 /* 1552 * If nlwb is not yet issued, zil_lwb_set_zio_dependency() is not 1553 * called for it yet, and when it will be, it won't be able to make 1554 * its write ZIO a parent this ZIO. In such case we can not defer 1555 * our flushes or below may be a race between the done callbacks. 1556 */ 1557 nlwb = list_next(&zilog->zl_lwb_list, lwb); 1558 if (nlwb && nlwb->lwb_state != LWB_STATE_ISSUED) 1559 nlwb = NULL; 1560 mutex_exit(&zilog->zl_lock); 1561 1562 if (avl_numnodes(t) == 0) 1563 return; 1564 1565 /* 1566 * If there was an IO error, we're not going to call zio_flush() 1567 * on these vdevs, so we simply empty the tree and free the 1568 * nodes. We avoid calling zio_flush() since there isn't any 1569 * good reason for doing so, after the lwb block failed to be 1570 * written out. 1571 * 1572 * Additionally, we don't perform any further error handling at 1573 * this point (e.g. setting "zcw_zio_error" appropriately), as 1574 * we expect that to occur in "zil_lwb_flush_vdevs_done" (thus, 1575 * we expect any error seen here, to have been propagated to 1576 * that function). 1577 */ 1578 if (zio->io_error != 0) { 1579 while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) 1580 kmem_free(zv, sizeof (*zv)); 1581 return; 1582 } 1583 1584 /* 1585 * If this lwb does not have any threads waiting for it to 1586 * complete, we want to defer issuing the DKIOCFLUSHWRITECACHE 1587 * command to the vdevs written to by "this" lwb, and instead 1588 * rely on the "next" lwb to handle the DKIOCFLUSHWRITECACHE 1589 * command for those vdevs. Thus, we merge the vdev tree of 1590 * "this" lwb with the vdev tree of the "next" lwb in the list, 1591 * and assume the "next" lwb will handle flushing the vdevs (or 1592 * deferring the flush(s) again). 1593 * 1594 * This is a useful performance optimization, especially for 1595 * workloads with lots of async write activity and few sync 1596 * write and/or fsync activity, as it has the potential to 1597 * coalesce multiple flush commands to a vdev into one. 1598 */ 1599 if (list_is_empty(&lwb->lwb_waiters) && nlwb != NULL) { 1600 zil_lwb_flush_defer(lwb, nlwb); 1601 ASSERT(avl_is_empty(&lwb->lwb_vdev_tree)); 1602 return; 1603 } 1604 1605 while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) { 1606 vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev); 1607 if (vd != NULL) { 1608 /* 1609 * The "ZIO_FLAG_DONT_PROPAGATE" is currently 1610 * always used within "zio_flush". This means, 1611 * any errors when flushing the vdev(s), will 1612 * (unfortunately) not be handled correctly, 1613 * since these "zio_flush" errors will not be 1614 * propagated up to "zil_lwb_flush_vdevs_done". 1615 */ 1616 zio_flush(lwb->lwb_root_zio, vd); 1617 } 1618 kmem_free(zv, sizeof (*zv)); 1619 } 1620 } 1621 1622 /* 1623 * Build the zio dependency chain, which is used to preserve the ordering of 1624 * lwb completions that is required by the semantics of the ZIL. Each new lwb 1625 * zio becomes a parent of the previous lwb zio, such that the new lwb's zio 1626 * cannot complete until the previous lwb's zio completes. 1627 * 1628 * This is required by the semantics of zil_commit(): the commit waiters 1629 * attached to the lwbs will be woken in the lwb zio's completion callback, 1630 * so this zio dependency graph ensures the waiters are woken in the correct 1631 * order (the same order the lwbs were created). 1632 */ 1633 static void 1634 zil_lwb_set_zio_dependency(zilog_t *zilog, lwb_t *lwb) 1635 { 1636 ASSERT(MUTEX_HELD(&zilog->zl_lock)); 1637 1638 lwb_t *prev_lwb = list_prev(&zilog->zl_lwb_list, lwb); 1639 if (prev_lwb == NULL || 1640 prev_lwb->lwb_state == LWB_STATE_FLUSH_DONE) 1641 return; 1642 1643 /* 1644 * If the previous lwb's write hasn't already completed, we also want 1645 * to order the completion of the lwb write zios (above, we only order 1646 * the completion of the lwb root zios). This is required because of 1647 * how we can defer the DKIOCFLUSHWRITECACHE commands for each lwb. 1648 * 1649 * When the DKIOCFLUSHWRITECACHE commands are deferred, the previous 1650 * lwb will rely on this lwb to flush the vdevs written to by that 1651 * previous lwb. Thus, we need to ensure this lwb doesn't issue the 1652 * flush until after the previous lwb's write completes. We ensure 1653 * this ordering by setting the zio parent/child relationship here. 1654 * 1655 * Without this relationship on the lwb's write zio, it's possible 1656 * for this lwb's write to complete prior to the previous lwb's write 1657 * completing; and thus, the vdevs for the previous lwb would be 1658 * flushed prior to that lwb's data being written to those vdevs (the 1659 * vdevs are flushed in the lwb write zio's completion handler, 1660 * zil_lwb_write_done()). 1661 */ 1662 if (prev_lwb->lwb_state == LWB_STATE_ISSUED) { 1663 ASSERT3P(prev_lwb->lwb_write_zio, !=, NULL); 1664 zio_add_child(lwb->lwb_write_zio, prev_lwb->lwb_write_zio); 1665 } else { 1666 ASSERT3S(prev_lwb->lwb_state, ==, LWB_STATE_WRITE_DONE); 1667 } 1668 1669 ASSERT3P(prev_lwb->lwb_root_zio, !=, NULL); 1670 zio_add_child(lwb->lwb_root_zio, prev_lwb->lwb_root_zio); 1671 } 1672 1673 1674 /* 1675 * This function's purpose is to "open" an lwb such that it is ready to 1676 * accept new itxs being committed to it. This function is idempotent; if 1677 * the passed in lwb has already been opened, it is essentially a no-op. 1678 */ 1679 static void 1680 zil_lwb_write_open(zilog_t *zilog, lwb_t *lwb) 1681 { 1682 ASSERT(MUTEX_HELD(&zilog->zl_issuer_lock)); 1683 1684 if (lwb->lwb_state != LWB_STATE_NEW) { 1685 ASSERT3S(lwb->lwb_state, ==, LWB_STATE_OPENED); 1686 return; 1687 } 1688 1689 mutex_enter(&zilog->zl_lock); 1690 lwb->lwb_state = LWB_STATE_OPENED; 1691 zilog->zl_last_lwb_opened = lwb; 1692 mutex_exit(&zilog->zl_lock); 1693 } 1694 1695 /* 1696 * Define a limited set of intent log block sizes. 1697 * 1698 * These must be a multiple of 4KB. Note only the amount used (again 1699 * aligned to 4KB) actually gets written. However, we can't always just 1700 * allocate SPA_OLD_MAXBLOCKSIZE as the slog space could be exhausted. 1701 */ 1702 static const struct { 1703 uint64_t limit; 1704 uint64_t blksz; 1705 } zil_block_buckets[] = { 1706 { 4096, 4096 }, /* non TX_WRITE */ 1707 { 8192 + 4096, 8192 + 4096 }, /* database */ 1708 { 32768 + 4096, 32768 + 4096 }, /* NFS writes */ 1709 { 65536 + 4096, 65536 + 4096 }, /* 64KB writes */ 1710 { 131072, 131072 }, /* < 128KB writes */ 1711 { 131072 +4096, 65536 + 4096 }, /* 128KB writes */ 1712 { UINT64_MAX, SPA_OLD_MAXBLOCKSIZE}, /* > 128KB writes */ 1713 }; 1714 1715 /* 1716 * Maximum block size used by the ZIL. This is picked up when the ZIL is 1717 * initialized. Otherwise this should not be used directly; see 1718 * zl_max_block_size instead. 1719 */ 1720 static uint_t zil_maxblocksize = SPA_OLD_MAXBLOCKSIZE; 1721 1722 /* 1723 * Close the log block for being issued and allocate the next one. 1724 * Has to be called under zl_issuer_lock to chain more lwbs. 1725 */ 1726 static lwb_t * 1727 zil_lwb_write_close(zilog_t *zilog, lwb_t *lwb, lwb_state_t state) 1728 { 1729 int i; 1730 1731 ASSERT(MUTEX_HELD(&zilog->zl_issuer_lock)); 1732 ASSERT3S(lwb->lwb_state, ==, LWB_STATE_OPENED); 1733 lwb->lwb_state = LWB_STATE_CLOSED; 1734 1735 /* 1736 * If there was an allocation failure then returned NULL will trigger 1737 * zil_commit_writer_stall() at the caller. This is inherently racy, 1738 * since allocation may not have happened yet. 1739 */ 1740 if (lwb->lwb_error != 0) 1741 return (NULL); 1742 1743 /* 1744 * Log blocks are pre-allocated. Here we select the size of the next 1745 * block, based on size used in the last block. 1746 * - first find the smallest bucket that will fit the block from a 1747 * limited set of block sizes. This is because it's faster to write 1748 * blocks allocated from the same metaslab as they are adjacent or 1749 * close. 1750 * - next find the maximum from the new suggested size and an array of 1751 * previous sizes. This lessens a picket fence effect of wrongly 1752 * guessing the size if we have a stream of say 2k, 64k, 2k, 64k 1753 * requests. 1754 * 1755 * Note we only write what is used, but we can't just allocate 1756 * the maximum block size because we can exhaust the available 1757 * pool log space. 1758 */ 1759 uint64_t zil_blksz = zilog->zl_cur_used + sizeof (zil_chain_t); 1760 for (i = 0; zil_blksz > zil_block_buckets[i].limit; i++) 1761 continue; 1762 zil_blksz = MIN(zil_block_buckets[i].blksz, zilog->zl_max_block_size); 1763 zilog->zl_prev_blks[zilog->zl_prev_rotor] = zil_blksz; 1764 for (i = 0; i < ZIL_PREV_BLKS; i++) 1765 zil_blksz = MAX(zil_blksz, zilog->zl_prev_blks[i]); 1766 DTRACE_PROBE3(zil__block__size, zilog_t *, zilog, 1767 uint64_t, zil_blksz, 1768 uint64_t, zilog->zl_prev_blks[zilog->zl_prev_rotor]); 1769 zilog->zl_prev_rotor = (zilog->zl_prev_rotor + 1) & (ZIL_PREV_BLKS - 1); 1770 1771 return (zil_alloc_lwb(zilog, zil_blksz, NULL, 0, 0, state)); 1772 } 1773 1774 /* 1775 * Finalize previously closed block and issue the write zio. 1776 */ 1777 static void 1778 zil_lwb_write_issue(zilog_t *zilog, lwb_t *lwb) 1779 { 1780 spa_t *spa = zilog->zl_spa; 1781 zil_chain_t *zilc; 1782 boolean_t slog; 1783 zbookmark_phys_t zb; 1784 zio_priority_t prio; 1785 int error; 1786 1787 ASSERT3S(lwb->lwb_state, ==, LWB_STATE_CLOSED); 1788 1789 /* Actually fill the lwb with the data. */ 1790 for (itx_t *itx = list_head(&lwb->lwb_itxs); itx; 1791 itx = list_next(&lwb->lwb_itxs, itx)) 1792 zil_lwb_commit(zilog, lwb, itx); 1793 lwb->lwb_nused = lwb->lwb_nfilled; 1794 ASSERT3U(lwb->lwb_nused, <=, lwb->lwb_nmax); 1795 1796 lwb->lwb_root_zio = zio_root(spa, zil_lwb_flush_vdevs_done, lwb, 1797 ZIO_FLAG_CANFAIL); 1798 1799 /* 1800 * The lwb is now ready to be issued, but it can be only if it already 1801 * got its block pointer allocated or the allocation has failed. 1802 * Otherwise leave it as-is, relying on some other thread to issue it 1803 * after allocating its block pointer via calling zil_lwb_write_issue() 1804 * for the previous lwb(s) in the chain. 1805 */ 1806 mutex_enter(&zilog->zl_lock); 1807 lwb->lwb_state = LWB_STATE_READY; 1808 if (BP_IS_HOLE(&lwb->lwb_blk) && lwb->lwb_error == 0) { 1809 mutex_exit(&zilog->zl_lock); 1810 return; 1811 } 1812 mutex_exit(&zilog->zl_lock); 1813 1814 next_lwb: 1815 if (lwb->lwb_slim) 1816 zilc = (zil_chain_t *)lwb->lwb_buf; 1817 else 1818 zilc = (zil_chain_t *)(lwb->lwb_buf + lwb->lwb_nmax); 1819 int wsz = lwb->lwb_sz; 1820 if (lwb->lwb_error == 0) { 1821 abd_t *lwb_abd = abd_get_from_buf(lwb->lwb_buf, lwb->lwb_sz); 1822 if (!lwb->lwb_slog || zilog->zl_cur_used <= zil_slog_bulk) 1823 prio = ZIO_PRIORITY_SYNC_WRITE; 1824 else 1825 prio = ZIO_PRIORITY_ASYNC_WRITE; 1826 SET_BOOKMARK(&zb, lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET], 1827 ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, 1828 lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ]); 1829 lwb->lwb_write_zio = zio_rewrite(lwb->lwb_root_zio, spa, 0, 1830 &lwb->lwb_blk, lwb_abd, lwb->lwb_sz, zil_lwb_write_done, 1831 lwb, prio, ZIO_FLAG_CANFAIL, &zb); 1832 zil_lwb_add_block(lwb, &lwb->lwb_blk); 1833 1834 if (lwb->lwb_slim) { 1835 /* For Slim ZIL only write what is used. */ 1836 wsz = P2ROUNDUP_TYPED(lwb->lwb_nused, ZIL_MIN_BLKSZ, 1837 int); 1838 ASSERT3S(wsz, <=, lwb->lwb_sz); 1839 zio_shrink(lwb->lwb_write_zio, wsz); 1840 wsz = lwb->lwb_write_zio->io_size; 1841 } 1842 memset(lwb->lwb_buf + lwb->lwb_nused, 0, wsz - lwb->lwb_nused); 1843 zilc->zc_pad = 0; 1844 zilc->zc_nused = lwb->lwb_nused; 1845 zilc->zc_eck.zec_cksum = lwb->lwb_blk.blk_cksum; 1846 } else { 1847 /* 1848 * We can't write the lwb if there was an allocation failure, 1849 * so create a null zio instead just to maintain dependencies. 1850 */ 1851 lwb->lwb_write_zio = zio_null(lwb->lwb_root_zio, spa, NULL, 1852 zil_lwb_write_done, lwb, ZIO_FLAG_CANFAIL); 1853 lwb->lwb_write_zio->io_error = lwb->lwb_error; 1854 } 1855 if (lwb->lwb_child_zio) 1856 zio_add_child(lwb->lwb_write_zio, lwb->lwb_child_zio); 1857 1858 /* 1859 * Open transaction to allocate the next block pointer. 1860 */ 1861 dmu_tx_t *tx = dmu_tx_create(zilog->zl_os); 1862 VERIFY0(dmu_tx_assign(tx, TXG_WAIT | TXG_NOTHROTTLE)); 1863 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 1864 uint64_t txg = dmu_tx_get_txg(tx); 1865 1866 /* 1867 * Allocate next the block pointer unless we are already in error. 1868 */ 1869 lwb_t *nlwb = list_next(&zilog->zl_lwb_list, lwb); 1870 blkptr_t *bp = &zilc->zc_next_blk; 1871 BP_ZERO(bp); 1872 error = lwb->lwb_error; 1873 if (error == 0) { 1874 error = zio_alloc_zil(spa, zilog->zl_os, txg, bp, nlwb->lwb_sz, 1875 &slog); 1876 } 1877 if (error == 0) { 1878 ASSERT3U(bp->blk_birth, ==, txg); 1879 BP_SET_CHECKSUM(bp, nlwb->lwb_slim ? ZIO_CHECKSUM_ZILOG2 : 1880 ZIO_CHECKSUM_ZILOG); 1881 bp->blk_cksum = lwb->lwb_blk.blk_cksum; 1882 bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++; 1883 } 1884 1885 /* 1886 * Reduce TXG open time by incrementing inflight counter and committing 1887 * the transaciton. zil_sync() will wait for it to return to zero. 1888 */ 1889 mutex_enter(&zilog->zl_lwb_io_lock); 1890 lwb->lwb_issued_txg = txg; 1891 zilog->zl_lwb_inflight[txg & TXG_MASK]++; 1892 zilog->zl_lwb_max_issued_txg = MAX(txg, zilog->zl_lwb_max_issued_txg); 1893 mutex_exit(&zilog->zl_lwb_io_lock); 1894 dmu_tx_commit(tx); 1895 1896 spa_config_enter(spa, SCL_STATE, lwb, RW_READER); 1897 1898 /* 1899 * We've completed all potentially blocking operations. Update the 1900 * nlwb and allow it proceed without possible lock order reversals. 1901 */ 1902 mutex_enter(&zilog->zl_lock); 1903 zil_lwb_set_zio_dependency(zilog, lwb); 1904 lwb->lwb_state = LWB_STATE_ISSUED; 1905 1906 if (nlwb) { 1907 nlwb->lwb_blk = *bp; 1908 nlwb->lwb_error = error; 1909 nlwb->lwb_slog = slog; 1910 nlwb->lwb_alloc_txg = txg; 1911 if (nlwb->lwb_state != LWB_STATE_READY) 1912 nlwb = NULL; 1913 } 1914 mutex_exit(&zilog->zl_lock); 1915 1916 if (lwb->lwb_slog) { 1917 ZIL_STAT_BUMP(zilog, zil_itx_metaslab_slog_count); 1918 ZIL_STAT_INCR(zilog, zil_itx_metaslab_slog_bytes, 1919 lwb->lwb_nused); 1920 ZIL_STAT_INCR(zilog, zil_itx_metaslab_slog_write, 1921 wsz); 1922 ZIL_STAT_INCR(zilog, zil_itx_metaslab_slog_alloc, 1923 BP_GET_LSIZE(&lwb->lwb_blk)); 1924 } else { 1925 ZIL_STAT_BUMP(zilog, zil_itx_metaslab_normal_count); 1926 ZIL_STAT_INCR(zilog, zil_itx_metaslab_normal_bytes, 1927 lwb->lwb_nused); 1928 ZIL_STAT_INCR(zilog, zil_itx_metaslab_normal_write, 1929 wsz); 1930 ZIL_STAT_INCR(zilog, zil_itx_metaslab_normal_alloc, 1931 BP_GET_LSIZE(&lwb->lwb_blk)); 1932 } 1933 lwb->lwb_issued_timestamp = gethrtime(); 1934 if (lwb->lwb_child_zio) 1935 zio_nowait(lwb->lwb_child_zio); 1936 zio_nowait(lwb->lwb_write_zio); 1937 zio_nowait(lwb->lwb_root_zio); 1938 1939 /* 1940 * If nlwb was ready when we gave it the block pointer, 1941 * it is on us to issue it and possibly following ones. 1942 */ 1943 lwb = nlwb; 1944 if (lwb) 1945 goto next_lwb; 1946 } 1947 1948 /* 1949 * Maximum amount of data that can be put into single log block. 1950 */ 1951 uint64_t 1952 zil_max_log_data(zilog_t *zilog, size_t hdrsize) 1953 { 1954 return (zilog->zl_max_block_size - sizeof (zil_chain_t) - hdrsize); 1955 } 1956 1957 /* 1958 * Maximum amount of log space we agree to waste to reduce number of 1959 * WR_NEED_COPY chunks to reduce zl_get_data() overhead (~6%). 1960 */ 1961 static inline uint64_t 1962 zil_max_waste_space(zilog_t *zilog) 1963 { 1964 return (zil_max_log_data(zilog, sizeof (lr_write_t)) / 16); 1965 } 1966 1967 /* 1968 * Maximum amount of write data for WR_COPIED. For correctness, consumers 1969 * must fall back to WR_NEED_COPY if we can't fit the entire record into one 1970 * maximum sized log block, because each WR_COPIED record must fit in a 1971 * single log block. Below that it is a tradeoff of additional memory copy 1972 * and possibly worse log space efficiency vs additional range lock/unlock. 1973 */ 1974 static uint_t zil_maxcopied = 7680; 1975 1976 uint64_t 1977 zil_max_copied_data(zilog_t *zilog) 1978 { 1979 uint64_t max_data = zil_max_log_data(zilog, sizeof (lr_write_t)); 1980 return (MIN(max_data, zil_maxcopied)); 1981 } 1982 1983 /* 1984 * Estimate space needed in the lwb for the itx. Allocate more lwbs or 1985 * split the itx as needed, but don't touch the actual transaction data. 1986 * Has to be called under zl_issuer_lock to call zil_lwb_write_close() 1987 * to chain more lwbs. 1988 */ 1989 static lwb_t * 1990 zil_lwb_assign(zilog_t *zilog, lwb_t *lwb, itx_t *itx, list_t *ilwbs) 1991 { 1992 itx_t *citx; 1993 lr_t *lr, *clr; 1994 lr_write_t *lrw; 1995 uint64_t dlen, dnow, lwb_sp, reclen, max_log_data; 1996 1997 ASSERT(MUTEX_HELD(&zilog->zl_issuer_lock)); 1998 ASSERT3P(lwb, !=, NULL); 1999 ASSERT3P(lwb->lwb_buf, !=, NULL); 2000 2001 zil_lwb_write_open(zilog, lwb); 2002 2003 lr = &itx->itx_lr; 2004 lrw = (lr_write_t *)lr; 2005 2006 /* 2007 * A commit itx doesn't represent any on-disk state; instead 2008 * it's simply used as a place holder on the commit list, and 2009 * provides a mechanism for attaching a "commit waiter" onto the 2010 * correct lwb (such that the waiter can be signalled upon 2011 * completion of that lwb). Thus, we don't process this itx's 2012 * log record if it's a commit itx (these itx's don't have log 2013 * records), and instead link the itx's waiter onto the lwb's 2014 * list of waiters. 2015 * 2016 * For more details, see the comment above zil_commit(). 2017 */ 2018 if (lr->lrc_txtype == TX_COMMIT) { 2019 zil_commit_waiter_link_lwb(itx->itx_private, lwb); 2020 list_insert_tail(&lwb->lwb_itxs, itx); 2021 return (lwb); 2022 } 2023 2024 reclen = lr->lrc_reclen; 2025 if (lr->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY) { 2026 ASSERT3U(reclen, ==, sizeof (lr_write_t)); 2027 dlen = P2ROUNDUP_TYPED( 2028 lrw->lr_length, sizeof (uint64_t), uint64_t); 2029 } else { 2030 ASSERT3U(reclen, >=, sizeof (lr_t)); 2031 dlen = 0; 2032 } 2033 ASSERT3U(reclen, <=, zil_max_log_data(zilog, 0)); 2034 zilog->zl_cur_used += (reclen + dlen); 2035 2036 cont: 2037 /* 2038 * If this record won't fit in the current log block, start a new one. 2039 * For WR_NEED_COPY optimize layout for minimal number of chunks. 2040 */ 2041 lwb_sp = lwb->lwb_nmax - lwb->lwb_nused; 2042 max_log_data = zil_max_log_data(zilog, sizeof (lr_write_t)); 2043 if (reclen > lwb_sp || (reclen + dlen > lwb_sp && 2044 lwb_sp < zil_max_waste_space(zilog) && 2045 (dlen % max_log_data == 0 || 2046 lwb_sp < reclen + dlen % max_log_data))) { 2047 list_insert_tail(ilwbs, lwb); 2048 lwb = zil_lwb_write_close(zilog, lwb, LWB_STATE_OPENED); 2049 if (lwb == NULL) 2050 return (NULL); 2051 lwb_sp = lwb->lwb_nmax - lwb->lwb_nused; 2052 } 2053 2054 /* 2055 * There must be enough space in the log block to hold reclen. 2056 * For WR_COPIED, we need to fit the whole record in one block, 2057 * and reclen is the write record header size + the data size. 2058 * For WR_NEED_COPY, we can create multiple records, splitting 2059 * the data into multiple blocks, so we only need to fit one 2060 * word of data per block; in this case reclen is just the header 2061 * size (no data). 2062 */ 2063 ASSERT3U(reclen + MIN(dlen, sizeof (uint64_t)), <=, lwb_sp); 2064 2065 dnow = MIN(dlen, lwb_sp - reclen); 2066 if (dlen > dnow) { 2067 ASSERT3U(lr->lrc_txtype, ==, TX_WRITE); 2068 ASSERT3U(itx->itx_wr_state, ==, WR_NEED_COPY); 2069 citx = zil_itx_clone(itx); 2070 clr = &citx->itx_lr; 2071 lr_write_t *clrw = (lr_write_t *)clr; 2072 clrw->lr_length = dnow; 2073 lrw->lr_offset += dnow; 2074 lrw->lr_length -= dnow; 2075 } else { 2076 citx = itx; 2077 clr = lr; 2078 } 2079 2080 /* 2081 * We're actually making an entry, so update lrc_seq to be the 2082 * log record sequence number. Note that this is generally not 2083 * equal to the itx sequence number because not all transactions 2084 * are synchronous, and sometimes spa_sync() gets there first. 2085 */ 2086 clr->lrc_seq = ++zilog->zl_lr_seq; 2087 2088 lwb->lwb_nused += reclen + dnow; 2089 ASSERT3U(lwb->lwb_nused, <=, lwb->lwb_nmax); 2090 ASSERT0(P2PHASE(lwb->lwb_nused, sizeof (uint64_t))); 2091 2092 zil_lwb_add_txg(lwb, lr->lrc_txg); 2093 list_insert_tail(&lwb->lwb_itxs, citx); 2094 2095 dlen -= dnow; 2096 if (dlen > 0) { 2097 zilog->zl_cur_used += reclen; 2098 goto cont; 2099 } 2100 2101 if (lr->lrc_txtype == TX_WRITE && 2102 lr->lrc_txg > spa_freeze_txg(zilog->zl_spa)) 2103 txg_wait_synced(zilog->zl_dmu_pool, lr->lrc_txg); 2104 2105 return (lwb); 2106 } 2107 2108 /* 2109 * Fill the actual transaction data into the lwb, following zil_lwb_assign(). 2110 * Does not require locking. 2111 */ 2112 static void 2113 zil_lwb_commit(zilog_t *zilog, lwb_t *lwb, itx_t *itx) 2114 { 2115 lr_t *lr, *lrb; 2116 lr_write_t *lrw, *lrwb; 2117 char *lr_buf; 2118 uint64_t dlen, reclen; 2119 2120 lr = &itx->itx_lr; 2121 lrw = (lr_write_t *)lr; 2122 2123 if (lr->lrc_txtype == TX_COMMIT) 2124 return; 2125 2126 if (lr->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY) { 2127 dlen = P2ROUNDUP_TYPED( 2128 lrw->lr_length, sizeof (uint64_t), uint64_t); 2129 } else { 2130 dlen = 0; 2131 } 2132 reclen = lr->lrc_reclen; 2133 ASSERT3U(reclen + dlen, <=, lwb->lwb_nused - lwb->lwb_nfilled); 2134 2135 lr_buf = lwb->lwb_buf + lwb->lwb_nfilled; 2136 memcpy(lr_buf, lr, reclen); 2137 lrb = (lr_t *)lr_buf; /* Like lr, but inside lwb. */ 2138 lrwb = (lr_write_t *)lrb; /* Like lrw, but inside lwb. */ 2139 2140 ZIL_STAT_BUMP(zilog, zil_itx_count); 2141 2142 /* 2143 * If it's a write, fetch the data or get its blkptr as appropriate. 2144 */ 2145 if (lr->lrc_txtype == TX_WRITE) { 2146 if (itx->itx_wr_state == WR_COPIED) { 2147 ZIL_STAT_BUMP(zilog, zil_itx_copied_count); 2148 ZIL_STAT_INCR(zilog, zil_itx_copied_bytes, 2149 lrw->lr_length); 2150 } else { 2151 char *dbuf; 2152 int error; 2153 2154 if (itx->itx_wr_state == WR_NEED_COPY) { 2155 dbuf = lr_buf + reclen; 2156 lrb->lrc_reclen += dlen; 2157 ZIL_STAT_BUMP(zilog, zil_itx_needcopy_count); 2158 ZIL_STAT_INCR(zilog, zil_itx_needcopy_bytes, 2159 dlen); 2160 } else { 2161 ASSERT3S(itx->itx_wr_state, ==, WR_INDIRECT); 2162 dbuf = NULL; 2163 ZIL_STAT_BUMP(zilog, zil_itx_indirect_count); 2164 ZIL_STAT_INCR(zilog, zil_itx_indirect_bytes, 2165 lrw->lr_length); 2166 if (lwb->lwb_child_zio == NULL) { 2167 lwb->lwb_child_zio = zio_null(NULL, 2168 zilog->zl_spa, NULL, NULL, NULL, 2169 ZIO_FLAG_CANFAIL); 2170 } 2171 } 2172 2173 /* 2174 * The "lwb_child_zio" we pass in will become a child of 2175 * "lwb_write_zio", when one is created, so one will be 2176 * a parent of any zio's created by the "zl_get_data". 2177 * This way "lwb_write_zio" will first wait for children 2178 * block pointers before own writing, and then for their 2179 * writing completion before the vdev cache flushing. 2180 */ 2181 error = zilog->zl_get_data(itx->itx_private, 2182 itx->itx_gen, lrwb, dbuf, lwb, 2183 lwb->lwb_child_zio); 2184 if (dbuf != NULL && error == 0) { 2185 /* Zero any padding bytes in the last block. */ 2186 memset((char *)dbuf + lrwb->lr_length, 0, 2187 dlen - lrwb->lr_length); 2188 } 2189 2190 /* 2191 * Typically, the only return values we should see from 2192 * ->zl_get_data() are 0, EIO, ENOENT, EEXIST or 2193 * EALREADY. However, it is also possible to see other 2194 * error values such as ENOSPC or EINVAL from 2195 * dmu_read() -> dnode_hold() -> dnode_hold_impl() or 2196 * ENXIO as well as a multitude of others from the 2197 * block layer through dmu_buf_hold() -> dbuf_read() 2198 * -> zio_wait(), as well as through dmu_read() -> 2199 * dnode_hold() -> dnode_hold_impl() -> dbuf_read() -> 2200 * zio_wait(). When these errors happen, we can assume 2201 * that neither an immediate write nor an indirect 2202 * write occurred, so we need to fall back to 2203 * txg_wait_synced(). This is unusual, so we print to 2204 * dmesg whenever one of these errors occurs. 2205 */ 2206 switch (error) { 2207 case 0: 2208 break; 2209 default: 2210 cmn_err(CE_WARN, "zil_lwb_commit() received " 2211 "unexpected error %d from ->zl_get_data()" 2212 ". Falling back to txg_wait_synced().", 2213 error); 2214 zfs_fallthrough; 2215 case EIO: 2216 txg_wait_synced(zilog->zl_dmu_pool, 2217 lr->lrc_txg); 2218 zfs_fallthrough; 2219 case ENOENT: 2220 zfs_fallthrough; 2221 case EEXIST: 2222 zfs_fallthrough; 2223 case EALREADY: 2224 return; 2225 } 2226 } 2227 } 2228 2229 lwb->lwb_nfilled += reclen + dlen; 2230 ASSERT3S(lwb->lwb_nfilled, <=, lwb->lwb_nused); 2231 ASSERT0(P2PHASE(lwb->lwb_nfilled, sizeof (uint64_t))); 2232 } 2233 2234 itx_t * 2235 zil_itx_create(uint64_t txtype, size_t olrsize) 2236 { 2237 size_t itxsize, lrsize; 2238 itx_t *itx; 2239 2240 ASSERT3U(olrsize, >=, sizeof (lr_t)); 2241 lrsize = P2ROUNDUP_TYPED(olrsize, sizeof (uint64_t), size_t); 2242 ASSERT3U(lrsize, >=, olrsize); 2243 itxsize = offsetof(itx_t, itx_lr) + lrsize; 2244 2245 itx = zio_data_buf_alloc(itxsize); 2246 itx->itx_lr.lrc_txtype = txtype; 2247 itx->itx_lr.lrc_reclen = lrsize; 2248 itx->itx_lr.lrc_seq = 0; /* defensive */ 2249 memset((char *)&itx->itx_lr + olrsize, 0, lrsize - olrsize); 2250 itx->itx_sync = B_TRUE; /* default is synchronous */ 2251 itx->itx_callback = NULL; 2252 itx->itx_callback_data = NULL; 2253 itx->itx_size = itxsize; 2254 2255 return (itx); 2256 } 2257 2258 static itx_t * 2259 zil_itx_clone(itx_t *oitx) 2260 { 2261 ASSERT3U(oitx->itx_size, >=, sizeof (itx_t)); 2262 ASSERT3U(oitx->itx_size, ==, 2263 offsetof(itx_t, itx_lr) + oitx->itx_lr.lrc_reclen); 2264 2265 itx_t *itx = zio_data_buf_alloc(oitx->itx_size); 2266 memcpy(itx, oitx, oitx->itx_size); 2267 itx->itx_callback = NULL; 2268 itx->itx_callback_data = NULL; 2269 return (itx); 2270 } 2271 2272 void 2273 zil_itx_destroy(itx_t *itx) 2274 { 2275 ASSERT3U(itx->itx_size, >=, sizeof (itx_t)); 2276 ASSERT3U(itx->itx_lr.lrc_reclen, ==, 2277 itx->itx_size - offsetof(itx_t, itx_lr)); 2278 IMPLY(itx->itx_lr.lrc_txtype == TX_COMMIT, itx->itx_callback == NULL); 2279 IMPLY(itx->itx_callback != NULL, itx->itx_lr.lrc_txtype != TX_COMMIT); 2280 2281 if (itx->itx_callback != NULL) 2282 itx->itx_callback(itx->itx_callback_data); 2283 2284 zio_data_buf_free(itx, itx->itx_size); 2285 } 2286 2287 /* 2288 * Free up the sync and async itxs. The itxs_t has already been detached 2289 * so no locks are needed. 2290 */ 2291 static void 2292 zil_itxg_clean(void *arg) 2293 { 2294 itx_t *itx; 2295 list_t *list; 2296 avl_tree_t *t; 2297 void *cookie; 2298 itxs_t *itxs = arg; 2299 itx_async_node_t *ian; 2300 2301 list = &itxs->i_sync_list; 2302 while ((itx = list_remove_head(list)) != NULL) { 2303 /* 2304 * In the general case, commit itxs will not be found 2305 * here, as they'll be committed to an lwb via 2306 * zil_lwb_assign(), and free'd in that function. Having 2307 * said that, it is still possible for commit itxs to be 2308 * found here, due to the following race: 2309 * 2310 * - a thread calls zil_commit() which assigns the 2311 * commit itx to a per-txg i_sync_list 2312 * - zil_itxg_clean() is called (e.g. via spa_sync()) 2313 * while the waiter is still on the i_sync_list 2314 * 2315 * There's nothing to prevent syncing the txg while the 2316 * waiter is on the i_sync_list. This normally doesn't 2317 * happen because spa_sync() is slower than zil_commit(), 2318 * but if zil_commit() calls txg_wait_synced() (e.g. 2319 * because zil_create() or zil_commit_writer_stall() is 2320 * called) we will hit this case. 2321 */ 2322 if (itx->itx_lr.lrc_txtype == TX_COMMIT) 2323 zil_commit_waiter_skip(itx->itx_private); 2324 2325 zil_itx_destroy(itx); 2326 } 2327 2328 cookie = NULL; 2329 t = &itxs->i_async_tree; 2330 while ((ian = avl_destroy_nodes(t, &cookie)) != NULL) { 2331 list = &ian->ia_list; 2332 while ((itx = list_remove_head(list)) != NULL) { 2333 /* commit itxs should never be on the async lists. */ 2334 ASSERT3U(itx->itx_lr.lrc_txtype, !=, TX_COMMIT); 2335 zil_itx_destroy(itx); 2336 } 2337 list_destroy(list); 2338 kmem_free(ian, sizeof (itx_async_node_t)); 2339 } 2340 avl_destroy(t); 2341 2342 kmem_free(itxs, sizeof (itxs_t)); 2343 } 2344 2345 static int 2346 zil_aitx_compare(const void *x1, const void *x2) 2347 { 2348 const uint64_t o1 = ((itx_async_node_t *)x1)->ia_foid; 2349 const uint64_t o2 = ((itx_async_node_t *)x2)->ia_foid; 2350 2351 return (TREE_CMP(o1, o2)); 2352 } 2353 2354 /* 2355 * Remove all async itx with the given oid. 2356 */ 2357 void 2358 zil_remove_async(zilog_t *zilog, uint64_t oid) 2359 { 2360 uint64_t otxg, txg; 2361 itx_async_node_t *ian, ian_search; 2362 avl_tree_t *t; 2363 avl_index_t where; 2364 list_t clean_list; 2365 itx_t *itx; 2366 2367 ASSERT(oid != 0); 2368 list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node)); 2369 2370 if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */ 2371 otxg = ZILTEST_TXG; 2372 else 2373 otxg = spa_last_synced_txg(zilog->zl_spa) + 1; 2374 2375 for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) { 2376 itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK]; 2377 2378 mutex_enter(&itxg->itxg_lock); 2379 if (itxg->itxg_txg != txg) { 2380 mutex_exit(&itxg->itxg_lock); 2381 continue; 2382 } 2383 2384 /* 2385 * Locate the object node and append its list. 2386 */ 2387 t = &itxg->itxg_itxs->i_async_tree; 2388 ian_search.ia_foid = oid; 2389 ian = avl_find(t, &ian_search, &where); 2390 if (ian != NULL) 2391 list_move_tail(&clean_list, &ian->ia_list); 2392 mutex_exit(&itxg->itxg_lock); 2393 } 2394 while ((itx = list_remove_head(&clean_list)) != NULL) { 2395 /* commit itxs should never be on the async lists. */ 2396 ASSERT3U(itx->itx_lr.lrc_txtype, !=, TX_COMMIT); 2397 zil_itx_destroy(itx); 2398 } 2399 list_destroy(&clean_list); 2400 } 2401 2402 void 2403 zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx) 2404 { 2405 uint64_t txg; 2406 itxg_t *itxg; 2407 itxs_t *itxs, *clean = NULL; 2408 2409 /* 2410 * Ensure the data of a renamed file is committed before the rename. 2411 */ 2412 if ((itx->itx_lr.lrc_txtype & ~TX_CI) == TX_RENAME) 2413 zil_async_to_sync(zilog, itx->itx_oid); 2414 2415 if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) 2416 txg = ZILTEST_TXG; 2417 else 2418 txg = dmu_tx_get_txg(tx); 2419 2420 itxg = &zilog->zl_itxg[txg & TXG_MASK]; 2421 mutex_enter(&itxg->itxg_lock); 2422 itxs = itxg->itxg_itxs; 2423 if (itxg->itxg_txg != txg) { 2424 if (itxs != NULL) { 2425 /* 2426 * The zil_clean callback hasn't got around to cleaning 2427 * this itxg. Save the itxs for release below. 2428 * This should be rare. 2429 */ 2430 zfs_dbgmsg("zil_itx_assign: missed itx cleanup for " 2431 "txg %llu", (u_longlong_t)itxg->itxg_txg); 2432 clean = itxg->itxg_itxs; 2433 } 2434 itxg->itxg_txg = txg; 2435 itxs = itxg->itxg_itxs = kmem_zalloc(sizeof (itxs_t), 2436 KM_SLEEP); 2437 2438 list_create(&itxs->i_sync_list, sizeof (itx_t), 2439 offsetof(itx_t, itx_node)); 2440 avl_create(&itxs->i_async_tree, zil_aitx_compare, 2441 sizeof (itx_async_node_t), 2442 offsetof(itx_async_node_t, ia_node)); 2443 } 2444 if (itx->itx_sync) { 2445 list_insert_tail(&itxs->i_sync_list, itx); 2446 } else { 2447 avl_tree_t *t = &itxs->i_async_tree; 2448 uint64_t foid = 2449 LR_FOID_GET_OBJ(((lr_ooo_t *)&itx->itx_lr)->lr_foid); 2450 itx_async_node_t *ian; 2451 avl_index_t where; 2452 2453 ian = avl_find(t, &foid, &where); 2454 if (ian == NULL) { 2455 ian = kmem_alloc(sizeof (itx_async_node_t), 2456 KM_SLEEP); 2457 list_create(&ian->ia_list, sizeof (itx_t), 2458 offsetof(itx_t, itx_node)); 2459 ian->ia_foid = foid; 2460 avl_insert(t, ian, where); 2461 } 2462 list_insert_tail(&ian->ia_list, itx); 2463 } 2464 2465 itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx); 2466 2467 /* 2468 * We don't want to dirty the ZIL using ZILTEST_TXG, because 2469 * zil_clean() will never be called using ZILTEST_TXG. Thus, we 2470 * need to be careful to always dirty the ZIL using the "real" 2471 * TXG (not itxg_txg) even when the SPA is frozen. 2472 */ 2473 zilog_dirty(zilog, dmu_tx_get_txg(tx)); 2474 mutex_exit(&itxg->itxg_lock); 2475 2476 /* Release the old itxs now we've dropped the lock */ 2477 if (clean != NULL) 2478 zil_itxg_clean(clean); 2479 } 2480 2481 /* 2482 * If there are any in-memory intent log transactions which have now been 2483 * synced then start up a taskq to free them. We should only do this after we 2484 * have written out the uberblocks (i.e. txg has been committed) so that 2485 * don't inadvertently clean out in-memory log records that would be required 2486 * by zil_commit(). 2487 */ 2488 void 2489 zil_clean(zilog_t *zilog, uint64_t synced_txg) 2490 { 2491 itxg_t *itxg = &zilog->zl_itxg[synced_txg & TXG_MASK]; 2492 itxs_t *clean_me; 2493 2494 ASSERT3U(synced_txg, <, ZILTEST_TXG); 2495 2496 mutex_enter(&itxg->itxg_lock); 2497 if (itxg->itxg_itxs == NULL || itxg->itxg_txg == ZILTEST_TXG) { 2498 mutex_exit(&itxg->itxg_lock); 2499 return; 2500 } 2501 ASSERT3U(itxg->itxg_txg, <=, synced_txg); 2502 ASSERT3U(itxg->itxg_txg, !=, 0); 2503 clean_me = itxg->itxg_itxs; 2504 itxg->itxg_itxs = NULL; 2505 itxg->itxg_txg = 0; 2506 mutex_exit(&itxg->itxg_lock); 2507 /* 2508 * Preferably start a task queue to free up the old itxs but 2509 * if taskq_dispatch can't allocate resources to do that then 2510 * free it in-line. This should be rare. Note, using TQ_SLEEP 2511 * created a bad performance problem. 2512 */ 2513 ASSERT3P(zilog->zl_dmu_pool, !=, NULL); 2514 ASSERT3P(zilog->zl_dmu_pool->dp_zil_clean_taskq, !=, NULL); 2515 taskqid_t id = taskq_dispatch(zilog->zl_dmu_pool->dp_zil_clean_taskq, 2516 zil_itxg_clean, clean_me, TQ_NOSLEEP); 2517 if (id == TASKQID_INVALID) 2518 zil_itxg_clean(clean_me); 2519 } 2520 2521 /* 2522 * This function will traverse the queue of itxs that need to be 2523 * committed, and move them onto the ZIL's zl_itx_commit_list. 2524 */ 2525 static uint64_t 2526 zil_get_commit_list(zilog_t *zilog) 2527 { 2528 uint64_t otxg, txg, wtxg = 0; 2529 list_t *commit_list = &zilog->zl_itx_commit_list; 2530 2531 ASSERT(MUTEX_HELD(&zilog->zl_issuer_lock)); 2532 2533 if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */ 2534 otxg = ZILTEST_TXG; 2535 else 2536 otxg = spa_last_synced_txg(zilog->zl_spa) + 1; 2537 2538 /* 2539 * This is inherently racy, since there is nothing to prevent 2540 * the last synced txg from changing. That's okay since we'll 2541 * only commit things in the future. 2542 */ 2543 for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) { 2544 itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK]; 2545 2546 mutex_enter(&itxg->itxg_lock); 2547 if (itxg->itxg_txg != txg) { 2548 mutex_exit(&itxg->itxg_lock); 2549 continue; 2550 } 2551 2552 /* 2553 * If we're adding itx records to the zl_itx_commit_list, 2554 * then the zil better be dirty in this "txg". We can assert 2555 * that here since we're holding the itxg_lock which will 2556 * prevent spa_sync from cleaning it. Once we add the itxs 2557 * to the zl_itx_commit_list we must commit it to disk even 2558 * if it's unnecessary (i.e. the txg was synced). 2559 */ 2560 ASSERT(zilog_is_dirty_in_txg(zilog, txg) || 2561 spa_freeze_txg(zilog->zl_spa) != UINT64_MAX); 2562 list_t *sync_list = &itxg->itxg_itxs->i_sync_list; 2563 if (unlikely(zilog->zl_suspend > 0)) { 2564 /* 2565 * ZIL was just suspended, but we lost the race. 2566 * Allow all earlier itxs to be committed, but ask 2567 * caller to do txg_wait_synced(txg) for any new. 2568 */ 2569 if (!list_is_empty(sync_list)) 2570 wtxg = MAX(wtxg, txg); 2571 } else { 2572 list_move_tail(commit_list, sync_list); 2573 } 2574 2575 mutex_exit(&itxg->itxg_lock); 2576 } 2577 return (wtxg); 2578 } 2579 2580 /* 2581 * Move the async itxs for a specified object to commit into sync lists. 2582 */ 2583 void 2584 zil_async_to_sync(zilog_t *zilog, uint64_t foid) 2585 { 2586 uint64_t otxg, txg; 2587 itx_async_node_t *ian, ian_search; 2588 avl_tree_t *t; 2589 avl_index_t where; 2590 2591 if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */ 2592 otxg = ZILTEST_TXG; 2593 else 2594 otxg = spa_last_synced_txg(zilog->zl_spa) + 1; 2595 2596 /* 2597 * This is inherently racy, since there is nothing to prevent 2598 * the last synced txg from changing. 2599 */ 2600 for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) { 2601 itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK]; 2602 2603 mutex_enter(&itxg->itxg_lock); 2604 if (itxg->itxg_txg != txg) { 2605 mutex_exit(&itxg->itxg_lock); 2606 continue; 2607 } 2608 2609 /* 2610 * If a foid is specified then find that node and append its 2611 * list. Otherwise walk the tree appending all the lists 2612 * to the sync list. We add to the end rather than the 2613 * beginning to ensure the create has happened. 2614 */ 2615 t = &itxg->itxg_itxs->i_async_tree; 2616 if (foid != 0) { 2617 ian_search.ia_foid = foid; 2618 ian = avl_find(t, &ian_search, &where); 2619 if (ian != NULL) { 2620 list_move_tail(&itxg->itxg_itxs->i_sync_list, 2621 &ian->ia_list); 2622 } 2623 } else { 2624 void *cookie = NULL; 2625 2626 while ((ian = avl_destroy_nodes(t, &cookie)) != NULL) { 2627 list_move_tail(&itxg->itxg_itxs->i_sync_list, 2628 &ian->ia_list); 2629 list_destroy(&ian->ia_list); 2630 kmem_free(ian, sizeof (itx_async_node_t)); 2631 } 2632 } 2633 mutex_exit(&itxg->itxg_lock); 2634 } 2635 } 2636 2637 /* 2638 * This function will prune commit itxs that are at the head of the 2639 * commit list (it won't prune past the first non-commit itx), and 2640 * either: a) attach them to the last lwb that's still pending 2641 * completion, or b) skip them altogether. 2642 * 2643 * This is used as a performance optimization to prevent commit itxs 2644 * from generating new lwbs when it's unnecessary to do so. 2645 */ 2646 static void 2647 zil_prune_commit_list(zilog_t *zilog) 2648 { 2649 itx_t *itx; 2650 2651 ASSERT(MUTEX_HELD(&zilog->zl_issuer_lock)); 2652 2653 while ((itx = list_head(&zilog->zl_itx_commit_list)) != NULL) { 2654 lr_t *lrc = &itx->itx_lr; 2655 if (lrc->lrc_txtype != TX_COMMIT) 2656 break; 2657 2658 mutex_enter(&zilog->zl_lock); 2659 2660 lwb_t *last_lwb = zilog->zl_last_lwb_opened; 2661 if (last_lwb == NULL || 2662 last_lwb->lwb_state == LWB_STATE_FLUSH_DONE) { 2663 /* 2664 * All of the itxs this waiter was waiting on 2665 * must have already completed (or there were 2666 * never any itx's for it to wait on), so it's 2667 * safe to skip this waiter and mark it done. 2668 */ 2669 zil_commit_waiter_skip(itx->itx_private); 2670 } else { 2671 zil_commit_waiter_link_lwb(itx->itx_private, last_lwb); 2672 } 2673 2674 mutex_exit(&zilog->zl_lock); 2675 2676 list_remove(&zilog->zl_itx_commit_list, itx); 2677 zil_itx_destroy(itx); 2678 } 2679 2680 IMPLY(itx != NULL, itx->itx_lr.lrc_txtype != TX_COMMIT); 2681 } 2682 2683 static void 2684 zil_commit_writer_stall(zilog_t *zilog) 2685 { 2686 /* 2687 * When zio_alloc_zil() fails to allocate the next lwb block on 2688 * disk, we must call txg_wait_synced() to ensure all of the 2689 * lwbs in the zilog's zl_lwb_list are synced and then freed (in 2690 * zil_sync()), such that any subsequent ZIL writer (i.e. a call 2691 * to zil_process_commit_list()) will have to call zil_create(), 2692 * and start a new ZIL chain. 2693 * 2694 * Since zil_alloc_zil() failed, the lwb that was previously 2695 * issued does not have a pointer to the "next" lwb on disk. 2696 * Thus, if another ZIL writer thread was to allocate the "next" 2697 * on-disk lwb, that block could be leaked in the event of a 2698 * crash (because the previous lwb on-disk would not point to 2699 * it). 2700 * 2701 * We must hold the zilog's zl_issuer_lock while we do this, to 2702 * ensure no new threads enter zil_process_commit_list() until 2703 * all lwb's in the zl_lwb_list have been synced and freed 2704 * (which is achieved via the txg_wait_synced() call). 2705 */ 2706 ASSERT(MUTEX_HELD(&zilog->zl_issuer_lock)); 2707 txg_wait_synced(zilog->zl_dmu_pool, 0); 2708 ASSERT(list_is_empty(&zilog->zl_lwb_list)); 2709 } 2710 2711 static void 2712 zil_burst_done(zilog_t *zilog) 2713 { 2714 if (!list_is_empty(&zilog->zl_itx_commit_list) || 2715 zilog->zl_cur_used == 0) 2716 return; 2717 2718 if (zilog->zl_parallel) 2719 zilog->zl_parallel--; 2720 2721 zilog->zl_cur_used = 0; 2722 } 2723 2724 /* 2725 * This function will traverse the commit list, creating new lwbs as 2726 * needed, and committing the itxs from the commit list to these newly 2727 * created lwbs. Additionally, as a new lwb is created, the previous 2728 * lwb will be issued to the zio layer to be written to disk. 2729 */ 2730 static void 2731 zil_process_commit_list(zilog_t *zilog, zil_commit_waiter_t *zcw, list_t *ilwbs) 2732 { 2733 spa_t *spa = zilog->zl_spa; 2734 list_t nolwb_itxs; 2735 list_t nolwb_waiters; 2736 lwb_t *lwb, *plwb; 2737 itx_t *itx; 2738 2739 ASSERT(MUTEX_HELD(&zilog->zl_issuer_lock)); 2740 2741 /* 2742 * Return if there's nothing to commit before we dirty the fs by 2743 * calling zil_create(). 2744 */ 2745 if (list_is_empty(&zilog->zl_itx_commit_list)) 2746 return; 2747 2748 list_create(&nolwb_itxs, sizeof (itx_t), offsetof(itx_t, itx_node)); 2749 list_create(&nolwb_waiters, sizeof (zil_commit_waiter_t), 2750 offsetof(zil_commit_waiter_t, zcw_node)); 2751 2752 lwb = list_tail(&zilog->zl_lwb_list); 2753 if (lwb == NULL) { 2754 lwb = zil_create(zilog); 2755 } else { 2756 /* 2757 * Activate SPA_FEATURE_ZILSAXATTR for the cases where ZIL will 2758 * have already been created (zl_lwb_list not empty). 2759 */ 2760 zil_commit_activate_saxattr_feature(zilog); 2761 ASSERT(lwb->lwb_state == LWB_STATE_NEW || 2762 lwb->lwb_state == LWB_STATE_OPENED); 2763 2764 /* 2765 * If the lwb is still opened, it means the workload is really 2766 * multi-threaded and we won the chance of write aggregation. 2767 * If it is not opened yet, but previous lwb is still not 2768 * flushed, it still means the workload is multi-threaded, but 2769 * there was too much time between the commits to aggregate, so 2770 * we try aggregation next times, but without too much hopes. 2771 */ 2772 if (lwb->lwb_state == LWB_STATE_OPENED) { 2773 zilog->zl_parallel = ZIL_BURSTS; 2774 } else if ((plwb = list_prev(&zilog->zl_lwb_list, lwb)) 2775 != NULL && plwb->lwb_state != LWB_STATE_FLUSH_DONE) { 2776 zilog->zl_parallel = MAX(zilog->zl_parallel, 2777 ZIL_BURSTS / 2); 2778 } 2779 } 2780 2781 while ((itx = list_remove_head(&zilog->zl_itx_commit_list)) != NULL) { 2782 lr_t *lrc = &itx->itx_lr; 2783 uint64_t txg = lrc->lrc_txg; 2784 2785 ASSERT3U(txg, !=, 0); 2786 2787 if (lrc->lrc_txtype == TX_COMMIT) { 2788 DTRACE_PROBE2(zil__process__commit__itx, 2789 zilog_t *, zilog, itx_t *, itx); 2790 } else { 2791 DTRACE_PROBE2(zil__process__normal__itx, 2792 zilog_t *, zilog, itx_t *, itx); 2793 } 2794 2795 boolean_t synced = txg <= spa_last_synced_txg(spa); 2796 boolean_t frozen = txg > spa_freeze_txg(spa); 2797 2798 /* 2799 * If the txg of this itx has already been synced out, then 2800 * we don't need to commit this itx to an lwb. This is 2801 * because the data of this itx will have already been 2802 * written to the main pool. This is inherently racy, and 2803 * it's still ok to commit an itx whose txg has already 2804 * been synced; this will result in a write that's 2805 * unnecessary, but will do no harm. 2806 * 2807 * With that said, we always want to commit TX_COMMIT itxs 2808 * to an lwb, regardless of whether or not that itx's txg 2809 * has been synced out. We do this to ensure any OPENED lwb 2810 * will always have at least one zil_commit_waiter_t linked 2811 * to the lwb. 2812 * 2813 * As a counter-example, if we skipped TX_COMMIT itx's 2814 * whose txg had already been synced, the following 2815 * situation could occur if we happened to be racing with 2816 * spa_sync: 2817 * 2818 * 1. We commit a non-TX_COMMIT itx to an lwb, where the 2819 * itx's txg is 10 and the last synced txg is 9. 2820 * 2. spa_sync finishes syncing out txg 10. 2821 * 3. We move to the next itx in the list, it's a TX_COMMIT 2822 * whose txg is 10, so we skip it rather than committing 2823 * it to the lwb used in (1). 2824 * 2825 * If the itx that is skipped in (3) is the last TX_COMMIT 2826 * itx in the commit list, than it's possible for the lwb 2827 * used in (1) to remain in the OPENED state indefinitely. 2828 * 2829 * To prevent the above scenario from occurring, ensuring 2830 * that once an lwb is OPENED it will transition to ISSUED 2831 * and eventually DONE, we always commit TX_COMMIT itx's to 2832 * an lwb here, even if that itx's txg has already been 2833 * synced. 2834 * 2835 * Finally, if the pool is frozen, we _always_ commit the 2836 * itx. The point of freezing the pool is to prevent data 2837 * from being written to the main pool via spa_sync, and 2838 * instead rely solely on the ZIL to persistently store the 2839 * data; i.e. when the pool is frozen, the last synced txg 2840 * value can't be trusted. 2841 */ 2842 if (frozen || !synced || lrc->lrc_txtype == TX_COMMIT) { 2843 if (lwb != NULL) { 2844 lwb = zil_lwb_assign(zilog, lwb, itx, ilwbs); 2845 if (lwb == NULL) { 2846 list_insert_tail(&nolwb_itxs, itx); 2847 } else if ((zcw->zcw_lwb != NULL && 2848 zcw->zcw_lwb != lwb) || zcw->zcw_done) { 2849 /* 2850 * Our lwb is done, leave the rest of 2851 * itx list to somebody else who care. 2852 */ 2853 zilog->zl_parallel = ZIL_BURSTS; 2854 break; 2855 } 2856 } else { 2857 if (lrc->lrc_txtype == TX_COMMIT) { 2858 zil_commit_waiter_link_nolwb( 2859 itx->itx_private, &nolwb_waiters); 2860 } 2861 list_insert_tail(&nolwb_itxs, itx); 2862 } 2863 } else { 2864 ASSERT3S(lrc->lrc_txtype, !=, TX_COMMIT); 2865 zil_itx_destroy(itx); 2866 } 2867 } 2868 2869 if (lwb == NULL) { 2870 /* 2871 * This indicates zio_alloc_zil() failed to allocate the 2872 * "next" lwb on-disk. When this happens, we must stall 2873 * the ZIL write pipeline; see the comment within 2874 * zil_commit_writer_stall() for more details. 2875 */ 2876 while ((lwb = list_remove_head(ilwbs)) != NULL) 2877 zil_lwb_write_issue(zilog, lwb); 2878 zil_commit_writer_stall(zilog); 2879 2880 /* 2881 * Additionally, we have to signal and mark the "nolwb" 2882 * waiters as "done" here, since without an lwb, we 2883 * can't do this via zil_lwb_flush_vdevs_done() like 2884 * normal. 2885 */ 2886 zil_commit_waiter_t *zcw; 2887 while ((zcw = list_remove_head(&nolwb_waiters)) != NULL) 2888 zil_commit_waiter_skip(zcw); 2889 2890 /* 2891 * And finally, we have to destroy the itx's that 2892 * couldn't be committed to an lwb; this will also call 2893 * the itx's callback if one exists for the itx. 2894 */ 2895 while ((itx = list_remove_head(&nolwb_itxs)) != NULL) 2896 zil_itx_destroy(itx); 2897 } else { 2898 ASSERT(list_is_empty(&nolwb_waiters)); 2899 ASSERT3P(lwb, !=, NULL); 2900 ASSERT(lwb->lwb_state == LWB_STATE_NEW || 2901 lwb->lwb_state == LWB_STATE_OPENED); 2902 2903 /* 2904 * At this point, the ZIL block pointed at by the "lwb" 2905 * variable is in "new" or "opened" state. 2906 * 2907 * If it's "new", then no itxs have been committed to it, so 2908 * there's no point in issuing its zio (i.e. it's "empty"). 2909 * 2910 * If it's "opened", then it contains one or more itxs that 2911 * eventually need to be committed to stable storage. In 2912 * this case we intentionally do not issue the lwb's zio 2913 * to disk yet, and instead rely on one of the following 2914 * two mechanisms for issuing the zio: 2915 * 2916 * 1. Ideally, there will be more ZIL activity occurring on 2917 * the system, such that this function will be immediately 2918 * called again by different thread and this lwb will be 2919 * closed by zil_lwb_assign(). This way, the lwb will be 2920 * "full" when it is issued to disk, and we'll make use of 2921 * the lwb's size the best we can. 2922 * 2923 * 2. If there isn't sufficient ZIL activity occurring on 2924 * the system, zil_commit_waiter() will close it and issue 2925 * the zio. If this occurs, the lwb is not guaranteed 2926 * to be "full" by the time its zio is issued, and means 2927 * the size of the lwb was "too large" given the amount 2928 * of ZIL activity occurring on the system at that time. 2929 * 2930 * We do this for a couple of reasons: 2931 * 2932 * 1. To try and reduce the number of IOPs needed to 2933 * write the same number of itxs. If an lwb has space 2934 * available in its buffer for more itxs, and more itxs 2935 * will be committed relatively soon (relative to the 2936 * latency of performing a write), then it's beneficial 2937 * to wait for these "next" itxs. This way, more itxs 2938 * can be committed to stable storage with fewer writes. 2939 * 2940 * 2. To try and use the largest lwb block size that the 2941 * incoming rate of itxs can support. Again, this is to 2942 * try and pack as many itxs into as few lwbs as 2943 * possible, without significantly impacting the latency 2944 * of each individual itx. 2945 */ 2946 if (lwb->lwb_state == LWB_STATE_OPENED && !zilog->zl_parallel) { 2947 list_insert_tail(ilwbs, lwb); 2948 lwb = zil_lwb_write_close(zilog, lwb, LWB_STATE_NEW); 2949 zil_burst_done(zilog); 2950 if (lwb == NULL) { 2951 while ((lwb = list_remove_head(ilwbs)) != NULL) 2952 zil_lwb_write_issue(zilog, lwb); 2953 zil_commit_writer_stall(zilog); 2954 } 2955 } 2956 } 2957 } 2958 2959 /* 2960 * This function is responsible for ensuring the passed in commit waiter 2961 * (and associated commit itx) is committed to an lwb. If the waiter is 2962 * not already committed to an lwb, all itxs in the zilog's queue of 2963 * itxs will be processed. The assumption is the passed in waiter's 2964 * commit itx will found in the queue just like the other non-commit 2965 * itxs, such that when the entire queue is processed, the waiter will 2966 * have been committed to an lwb. 2967 * 2968 * The lwb associated with the passed in waiter is not guaranteed to 2969 * have been issued by the time this function completes. If the lwb is 2970 * not issued, we rely on future calls to zil_commit_writer() to issue 2971 * the lwb, or the timeout mechanism found in zil_commit_waiter(). 2972 */ 2973 static uint64_t 2974 zil_commit_writer(zilog_t *zilog, zil_commit_waiter_t *zcw) 2975 { 2976 list_t ilwbs; 2977 lwb_t *lwb; 2978 uint64_t wtxg = 0; 2979 2980 ASSERT(!MUTEX_HELD(&zilog->zl_lock)); 2981 ASSERT(spa_writeable(zilog->zl_spa)); 2982 2983 list_create(&ilwbs, sizeof (lwb_t), offsetof(lwb_t, lwb_issue_node)); 2984 mutex_enter(&zilog->zl_issuer_lock); 2985 2986 if (zcw->zcw_lwb != NULL || zcw->zcw_done) { 2987 /* 2988 * It's possible that, while we were waiting to acquire 2989 * the "zl_issuer_lock", another thread committed this 2990 * waiter to an lwb. If that occurs, we bail out early, 2991 * without processing any of the zilog's queue of itxs. 2992 * 2993 * On certain workloads and system configurations, the 2994 * "zl_issuer_lock" can become highly contended. In an 2995 * attempt to reduce this contention, we immediately drop 2996 * the lock if the waiter has already been processed. 2997 * 2998 * We've measured this optimization to reduce CPU spent 2999 * contending on this lock by up to 5%, using a system 3000 * with 32 CPUs, low latency storage (~50 usec writes), 3001 * and 1024 threads performing sync writes. 3002 */ 3003 goto out; 3004 } 3005 3006 ZIL_STAT_BUMP(zilog, zil_commit_writer_count); 3007 3008 wtxg = zil_get_commit_list(zilog); 3009 zil_prune_commit_list(zilog); 3010 zil_process_commit_list(zilog, zcw, &ilwbs); 3011 3012 out: 3013 mutex_exit(&zilog->zl_issuer_lock); 3014 while ((lwb = list_remove_head(&ilwbs)) != NULL) 3015 zil_lwb_write_issue(zilog, lwb); 3016 list_destroy(&ilwbs); 3017 return (wtxg); 3018 } 3019 3020 static void 3021 zil_commit_waiter_timeout(zilog_t *zilog, zil_commit_waiter_t *zcw) 3022 { 3023 ASSERT(!MUTEX_HELD(&zilog->zl_issuer_lock)); 3024 ASSERT(MUTEX_HELD(&zcw->zcw_lock)); 3025 ASSERT3B(zcw->zcw_done, ==, B_FALSE); 3026 3027 lwb_t *lwb = zcw->zcw_lwb; 3028 ASSERT3P(lwb, !=, NULL); 3029 ASSERT3S(lwb->lwb_state, !=, LWB_STATE_NEW); 3030 3031 /* 3032 * If the lwb has already been issued by another thread, we can 3033 * immediately return since there's no work to be done (the 3034 * point of this function is to issue the lwb). Additionally, we 3035 * do this prior to acquiring the zl_issuer_lock, to avoid 3036 * acquiring it when it's not necessary to do so. 3037 */ 3038 if (lwb->lwb_state != LWB_STATE_OPENED) 3039 return; 3040 3041 /* 3042 * In order to call zil_lwb_write_close() we must hold the 3043 * zilog's "zl_issuer_lock". We can't simply acquire that lock, 3044 * since we're already holding the commit waiter's "zcw_lock", 3045 * and those two locks are acquired in the opposite order 3046 * elsewhere. 3047 */ 3048 mutex_exit(&zcw->zcw_lock); 3049 mutex_enter(&zilog->zl_issuer_lock); 3050 mutex_enter(&zcw->zcw_lock); 3051 3052 /* 3053 * Since we just dropped and re-acquired the commit waiter's 3054 * lock, we have to re-check to see if the waiter was marked 3055 * "done" during that process. If the waiter was marked "done", 3056 * the "lwb" pointer is no longer valid (it can be free'd after 3057 * the waiter is marked "done"), so without this check we could 3058 * wind up with a use-after-free error below. 3059 */ 3060 if (zcw->zcw_done) { 3061 mutex_exit(&zilog->zl_issuer_lock); 3062 return; 3063 } 3064 3065 ASSERT3P(lwb, ==, zcw->zcw_lwb); 3066 3067 /* 3068 * We've already checked this above, but since we hadn't acquired 3069 * the zilog's zl_issuer_lock, we have to perform this check a 3070 * second time while holding the lock. 3071 * 3072 * We don't need to hold the zl_lock since the lwb cannot transition 3073 * from OPENED to CLOSED while we hold the zl_issuer_lock. The lwb 3074 * _can_ transition from CLOSED to DONE, but it's OK to race with 3075 * that transition since we treat the lwb the same, whether it's in 3076 * the CLOSED, ISSUED or DONE states. 3077 * 3078 * The important thing, is we treat the lwb differently depending on 3079 * if it's OPENED or CLOSED, and block any other threads that might 3080 * attempt to close/issue this lwb. For that reason we hold the 3081 * zl_issuer_lock when checking the lwb_state; we must not call 3082 * zil_lwb_write_close() if the lwb had already been closed/issued. 3083 * 3084 * See the comment above the lwb_state_t structure definition for 3085 * more details on the lwb states, and locking requirements. 3086 */ 3087 if (lwb->lwb_state != LWB_STATE_OPENED) { 3088 mutex_exit(&zilog->zl_issuer_lock); 3089 return; 3090 } 3091 3092 /* 3093 * We do not need zcw_lock once we hold zl_issuer_lock and know lwb 3094 * is still open. But we have to drop it to avoid a deadlock in case 3095 * callback of zio issued by zil_lwb_write_issue() try to get it, 3096 * while zil_lwb_write_issue() is blocked on attempt to issue next 3097 * lwb it found in LWB_STATE_READY state. 3098 */ 3099 mutex_exit(&zcw->zcw_lock); 3100 3101 /* 3102 * As described in the comments above zil_commit_waiter() and 3103 * zil_process_commit_list(), we need to issue this lwb's zio 3104 * since we've reached the commit waiter's timeout and it still 3105 * hasn't been issued. 3106 */ 3107 lwb_t *nlwb = zil_lwb_write_close(zilog, lwb, LWB_STATE_NEW); 3108 3109 ASSERT3S(lwb->lwb_state, ==, LWB_STATE_CLOSED); 3110 3111 zil_burst_done(zilog); 3112 3113 if (nlwb == NULL) { 3114 /* 3115 * When zil_lwb_write_close() returns NULL, this 3116 * indicates zio_alloc_zil() failed to allocate the 3117 * "next" lwb on-disk. When this occurs, the ZIL write 3118 * pipeline must be stalled; see the comment within the 3119 * zil_commit_writer_stall() function for more details. 3120 */ 3121 zil_lwb_write_issue(zilog, lwb); 3122 zil_commit_writer_stall(zilog); 3123 mutex_exit(&zilog->zl_issuer_lock); 3124 } else { 3125 mutex_exit(&zilog->zl_issuer_lock); 3126 zil_lwb_write_issue(zilog, lwb); 3127 } 3128 mutex_enter(&zcw->zcw_lock); 3129 } 3130 3131 /* 3132 * This function is responsible for performing the following two tasks: 3133 * 3134 * 1. its primary responsibility is to block until the given "commit 3135 * waiter" is considered "done". 3136 * 3137 * 2. its secondary responsibility is to issue the zio for the lwb that 3138 * the given "commit waiter" is waiting on, if this function has 3139 * waited "long enough" and the lwb is still in the "open" state. 3140 * 3141 * Given a sufficient amount of itxs being generated and written using 3142 * the ZIL, the lwb's zio will be issued via the zil_lwb_assign() 3143 * function. If this does not occur, this secondary responsibility will 3144 * ensure the lwb is issued even if there is not other synchronous 3145 * activity on the system. 3146 * 3147 * For more details, see zil_process_commit_list(); more specifically, 3148 * the comment at the bottom of that function. 3149 */ 3150 static void 3151 zil_commit_waiter(zilog_t *zilog, zil_commit_waiter_t *zcw) 3152 { 3153 ASSERT(!MUTEX_HELD(&zilog->zl_lock)); 3154 ASSERT(!MUTEX_HELD(&zilog->zl_issuer_lock)); 3155 ASSERT(spa_writeable(zilog->zl_spa)); 3156 3157 mutex_enter(&zcw->zcw_lock); 3158 3159 /* 3160 * The timeout is scaled based on the lwb latency to avoid 3161 * significantly impacting the latency of each individual itx. 3162 * For more details, see the comment at the bottom of the 3163 * zil_process_commit_list() function. 3164 */ 3165 int pct = MAX(zfs_commit_timeout_pct, 1); 3166 hrtime_t sleep = (zilog->zl_last_lwb_latency * pct) / 100; 3167 hrtime_t wakeup = gethrtime() + sleep; 3168 boolean_t timedout = B_FALSE; 3169 3170 while (!zcw->zcw_done) { 3171 ASSERT(MUTEX_HELD(&zcw->zcw_lock)); 3172 3173 lwb_t *lwb = zcw->zcw_lwb; 3174 3175 /* 3176 * Usually, the waiter will have a non-NULL lwb field here, 3177 * but it's possible for it to be NULL as a result of 3178 * zil_commit() racing with spa_sync(). 3179 * 3180 * When zil_clean() is called, it's possible for the itxg 3181 * list (which may be cleaned via a taskq) to contain 3182 * commit itxs. When this occurs, the commit waiters linked 3183 * off of these commit itxs will not be committed to an 3184 * lwb. Additionally, these commit waiters will not be 3185 * marked done until zil_commit_waiter_skip() is called via 3186 * zil_itxg_clean(). 3187 * 3188 * Thus, it's possible for this commit waiter (i.e. the 3189 * "zcw" variable) to be found in this "in between" state; 3190 * where it's "zcw_lwb" field is NULL, and it hasn't yet 3191 * been skipped, so it's "zcw_done" field is still B_FALSE. 3192 */ 3193 IMPLY(lwb != NULL, lwb->lwb_state != LWB_STATE_NEW); 3194 3195 if (lwb != NULL && lwb->lwb_state == LWB_STATE_OPENED) { 3196 ASSERT3B(timedout, ==, B_FALSE); 3197 3198 /* 3199 * If the lwb hasn't been issued yet, then we 3200 * need to wait with a timeout, in case this 3201 * function needs to issue the lwb after the 3202 * timeout is reached; responsibility (2) from 3203 * the comment above this function. 3204 */ 3205 int rc = cv_timedwait_hires(&zcw->zcw_cv, 3206 &zcw->zcw_lock, wakeup, USEC2NSEC(1), 3207 CALLOUT_FLAG_ABSOLUTE); 3208 3209 if (rc != -1 || zcw->zcw_done) 3210 continue; 3211 3212 timedout = B_TRUE; 3213 zil_commit_waiter_timeout(zilog, zcw); 3214 3215 if (!zcw->zcw_done) { 3216 /* 3217 * If the commit waiter has already been 3218 * marked "done", it's possible for the 3219 * waiter's lwb structure to have already 3220 * been freed. Thus, we can only reliably 3221 * make these assertions if the waiter 3222 * isn't done. 3223 */ 3224 ASSERT3P(lwb, ==, zcw->zcw_lwb); 3225 ASSERT3S(lwb->lwb_state, !=, LWB_STATE_OPENED); 3226 } 3227 } else { 3228 /* 3229 * If the lwb isn't open, then it must have already 3230 * been issued. In that case, there's no need to 3231 * use a timeout when waiting for the lwb to 3232 * complete. 3233 * 3234 * Additionally, if the lwb is NULL, the waiter 3235 * will soon be signaled and marked done via 3236 * zil_clean() and zil_itxg_clean(), so no timeout 3237 * is required. 3238 */ 3239 3240 IMPLY(lwb != NULL, 3241 lwb->lwb_state == LWB_STATE_CLOSED || 3242 lwb->lwb_state == LWB_STATE_READY || 3243 lwb->lwb_state == LWB_STATE_ISSUED || 3244 lwb->lwb_state == LWB_STATE_WRITE_DONE || 3245 lwb->lwb_state == LWB_STATE_FLUSH_DONE); 3246 cv_wait(&zcw->zcw_cv, &zcw->zcw_lock); 3247 } 3248 } 3249 3250 mutex_exit(&zcw->zcw_lock); 3251 } 3252 3253 static zil_commit_waiter_t * 3254 zil_alloc_commit_waiter(void) 3255 { 3256 zil_commit_waiter_t *zcw = kmem_cache_alloc(zil_zcw_cache, KM_SLEEP); 3257 3258 cv_init(&zcw->zcw_cv, NULL, CV_DEFAULT, NULL); 3259 mutex_init(&zcw->zcw_lock, NULL, MUTEX_DEFAULT, NULL); 3260 list_link_init(&zcw->zcw_node); 3261 zcw->zcw_lwb = NULL; 3262 zcw->zcw_done = B_FALSE; 3263 zcw->zcw_zio_error = 0; 3264 3265 return (zcw); 3266 } 3267 3268 static void 3269 zil_free_commit_waiter(zil_commit_waiter_t *zcw) 3270 { 3271 ASSERT(!list_link_active(&zcw->zcw_node)); 3272 ASSERT3P(zcw->zcw_lwb, ==, NULL); 3273 ASSERT3B(zcw->zcw_done, ==, B_TRUE); 3274 mutex_destroy(&zcw->zcw_lock); 3275 cv_destroy(&zcw->zcw_cv); 3276 kmem_cache_free(zil_zcw_cache, zcw); 3277 } 3278 3279 /* 3280 * This function is used to create a TX_COMMIT itx and assign it. This 3281 * way, it will be linked into the ZIL's list of synchronous itxs, and 3282 * then later committed to an lwb (or skipped) when 3283 * zil_process_commit_list() is called. 3284 */ 3285 static void 3286 zil_commit_itx_assign(zilog_t *zilog, zil_commit_waiter_t *zcw) 3287 { 3288 dmu_tx_t *tx = dmu_tx_create(zilog->zl_os); 3289 3290 /* 3291 * Since we are not going to create any new dirty data, and we 3292 * can even help with clearing the existing dirty data, we 3293 * should not be subject to the dirty data based delays. We 3294 * use TXG_NOTHROTTLE to bypass the delay mechanism. 3295 */ 3296 VERIFY0(dmu_tx_assign(tx, TXG_WAIT | TXG_NOTHROTTLE)); 3297 3298 itx_t *itx = zil_itx_create(TX_COMMIT, sizeof (lr_t)); 3299 itx->itx_sync = B_TRUE; 3300 itx->itx_private = zcw; 3301 3302 zil_itx_assign(zilog, itx, tx); 3303 3304 dmu_tx_commit(tx); 3305 } 3306 3307 /* 3308 * Commit ZFS Intent Log transactions (itxs) to stable storage. 3309 * 3310 * When writing ZIL transactions to the on-disk representation of the 3311 * ZIL, the itxs are committed to a Log Write Block (lwb). Multiple 3312 * itxs can be committed to a single lwb. Once a lwb is written and 3313 * committed to stable storage (i.e. the lwb is written, and vdevs have 3314 * been flushed), each itx that was committed to that lwb is also 3315 * considered to be committed to stable storage. 3316 * 3317 * When an itx is committed to an lwb, the log record (lr_t) contained 3318 * by the itx is copied into the lwb's zio buffer, and once this buffer 3319 * is written to disk, it becomes an on-disk ZIL block. 3320 * 3321 * As itxs are generated, they're inserted into the ZIL's queue of 3322 * uncommitted itxs. The semantics of zil_commit() are such that it will 3323 * block until all itxs that were in the queue when it was called, are 3324 * committed to stable storage. 3325 * 3326 * If "foid" is zero, this means all "synchronous" and "asynchronous" 3327 * itxs, for all objects in the dataset, will be committed to stable 3328 * storage prior to zil_commit() returning. If "foid" is non-zero, all 3329 * "synchronous" itxs for all objects, but only "asynchronous" itxs 3330 * that correspond to the foid passed in, will be committed to stable 3331 * storage prior to zil_commit() returning. 3332 * 3333 * Generally speaking, when zil_commit() is called, the consumer doesn't 3334 * actually care about _all_ of the uncommitted itxs. Instead, they're 3335 * simply trying to waiting for a specific itx to be committed to disk, 3336 * but the interface(s) for interacting with the ZIL don't allow such 3337 * fine-grained communication. A better interface would allow a consumer 3338 * to create and assign an itx, and then pass a reference to this itx to 3339 * zil_commit(); such that zil_commit() would return as soon as that 3340 * specific itx was committed to disk (instead of waiting for _all_ 3341 * itxs to be committed). 3342 * 3343 * When a thread calls zil_commit() a special "commit itx" will be 3344 * generated, along with a corresponding "waiter" for this commit itx. 3345 * zil_commit() will wait on this waiter's CV, such that when the waiter 3346 * is marked done, and signaled, zil_commit() will return. 3347 * 3348 * This commit itx is inserted into the queue of uncommitted itxs. This 3349 * provides an easy mechanism for determining which itxs were in the 3350 * queue prior to zil_commit() having been called, and which itxs were 3351 * added after zil_commit() was called. 3352 * 3353 * The commit itx is special; it doesn't have any on-disk representation. 3354 * When a commit itx is "committed" to an lwb, the waiter associated 3355 * with it is linked onto the lwb's list of waiters. Then, when that lwb 3356 * completes, each waiter on the lwb's list is marked done and signaled 3357 * -- allowing the thread waiting on the waiter to return from zil_commit(). 3358 * 3359 * It's important to point out a few critical factors that allow us 3360 * to make use of the commit itxs, commit waiters, per-lwb lists of 3361 * commit waiters, and zio completion callbacks like we're doing: 3362 * 3363 * 1. The list of waiters for each lwb is traversed, and each commit 3364 * waiter is marked "done" and signaled, in the zio completion 3365 * callback of the lwb's zio[*]. 3366 * 3367 * * Actually, the waiters are signaled in the zio completion 3368 * callback of the root zio for the DKIOCFLUSHWRITECACHE commands 3369 * that are sent to the vdevs upon completion of the lwb zio. 3370 * 3371 * 2. When the itxs are inserted into the ZIL's queue of uncommitted 3372 * itxs, the order in which they are inserted is preserved[*]; as 3373 * itxs are added to the queue, they are added to the tail of 3374 * in-memory linked lists. 3375 * 3376 * When committing the itxs to lwbs (to be written to disk), they 3377 * are committed in the same order in which the itxs were added to 3378 * the uncommitted queue's linked list(s); i.e. the linked list of 3379 * itxs to commit is traversed from head to tail, and each itx is 3380 * committed to an lwb in that order. 3381 * 3382 * * To clarify: 3383 * 3384 * - the order of "sync" itxs is preserved w.r.t. other 3385 * "sync" itxs, regardless of the corresponding objects. 3386 * - the order of "async" itxs is preserved w.r.t. other 3387 * "async" itxs corresponding to the same object. 3388 * - the order of "async" itxs is *not* preserved w.r.t. other 3389 * "async" itxs corresponding to different objects. 3390 * - the order of "sync" itxs w.r.t. "async" itxs (or vice 3391 * versa) is *not* preserved, even for itxs that correspond 3392 * to the same object. 3393 * 3394 * For more details, see: zil_itx_assign(), zil_async_to_sync(), 3395 * zil_get_commit_list(), and zil_process_commit_list(). 3396 * 3397 * 3. The lwbs represent a linked list of blocks on disk. Thus, any 3398 * lwb cannot be considered committed to stable storage, until its 3399 * "previous" lwb is also committed to stable storage. This fact, 3400 * coupled with the fact described above, means that itxs are 3401 * committed in (roughly) the order in which they were generated. 3402 * This is essential because itxs are dependent on prior itxs. 3403 * Thus, we *must not* deem an itx as being committed to stable 3404 * storage, until *all* prior itxs have also been committed to 3405 * stable storage. 3406 * 3407 * To enforce this ordering of lwb zio's, while still leveraging as 3408 * much of the underlying storage performance as possible, we rely 3409 * on two fundamental concepts: 3410 * 3411 * 1. The creation and issuance of lwb zio's is protected by 3412 * the zilog's "zl_issuer_lock", which ensures only a single 3413 * thread is creating and/or issuing lwb's at a time 3414 * 2. The "previous" lwb is a child of the "current" lwb 3415 * (leveraging the zio parent-child dependency graph) 3416 * 3417 * By relying on this parent-child zio relationship, we can have 3418 * many lwb zio's concurrently issued to the underlying storage, 3419 * but the order in which they complete will be the same order in 3420 * which they were created. 3421 */ 3422 void 3423 zil_commit(zilog_t *zilog, uint64_t foid) 3424 { 3425 /* 3426 * We should never attempt to call zil_commit on a snapshot for 3427 * a couple of reasons: 3428 * 3429 * 1. A snapshot may never be modified, thus it cannot have any 3430 * in-flight itxs that would have modified the dataset. 3431 * 3432 * 2. By design, when zil_commit() is called, a commit itx will 3433 * be assigned to this zilog; as a result, the zilog will be 3434 * dirtied. We must not dirty the zilog of a snapshot; there's 3435 * checks in the code that enforce this invariant, and will 3436 * cause a panic if it's not upheld. 3437 */ 3438 ASSERT3B(dmu_objset_is_snapshot(zilog->zl_os), ==, B_FALSE); 3439 3440 if (zilog->zl_sync == ZFS_SYNC_DISABLED) 3441 return; 3442 3443 if (!spa_writeable(zilog->zl_spa)) { 3444 /* 3445 * If the SPA is not writable, there should never be any 3446 * pending itxs waiting to be committed to disk. If that 3447 * weren't true, we'd skip writing those itxs out, and 3448 * would break the semantics of zil_commit(); thus, we're 3449 * verifying that truth before we return to the caller. 3450 */ 3451 ASSERT(list_is_empty(&zilog->zl_lwb_list)); 3452 ASSERT3P(zilog->zl_last_lwb_opened, ==, NULL); 3453 for (int i = 0; i < TXG_SIZE; i++) 3454 ASSERT3P(zilog->zl_itxg[i].itxg_itxs, ==, NULL); 3455 return; 3456 } 3457 3458 /* 3459 * If the ZIL is suspended, we don't want to dirty it by calling 3460 * zil_commit_itx_assign() below, nor can we write out 3461 * lwbs like would be done in zil_commit_write(). Thus, we 3462 * simply rely on txg_wait_synced() to maintain the necessary 3463 * semantics, and avoid calling those functions altogether. 3464 */ 3465 if (zilog->zl_suspend > 0) { 3466 txg_wait_synced(zilog->zl_dmu_pool, 0); 3467 return; 3468 } 3469 3470 zil_commit_impl(zilog, foid); 3471 } 3472 3473 void 3474 zil_commit_impl(zilog_t *zilog, uint64_t foid) 3475 { 3476 ZIL_STAT_BUMP(zilog, zil_commit_count); 3477 3478 /* 3479 * Move the "async" itxs for the specified foid to the "sync" 3480 * queues, such that they will be later committed (or skipped) 3481 * to an lwb when zil_process_commit_list() is called. 3482 * 3483 * Since these "async" itxs must be committed prior to this 3484 * call to zil_commit returning, we must perform this operation 3485 * before we call zil_commit_itx_assign(). 3486 */ 3487 zil_async_to_sync(zilog, foid); 3488 3489 /* 3490 * We allocate a new "waiter" structure which will initially be 3491 * linked to the commit itx using the itx's "itx_private" field. 3492 * Since the commit itx doesn't represent any on-disk state, 3493 * when it's committed to an lwb, rather than copying the its 3494 * lr_t into the lwb's buffer, the commit itx's "waiter" will be 3495 * added to the lwb's list of waiters. Then, when the lwb is 3496 * committed to stable storage, each waiter in the lwb's list of 3497 * waiters will be marked "done", and signalled. 3498 * 3499 * We must create the waiter and assign the commit itx prior to 3500 * calling zil_commit_writer(), or else our specific commit itx 3501 * is not guaranteed to be committed to an lwb prior to calling 3502 * zil_commit_waiter(). 3503 */ 3504 zil_commit_waiter_t *zcw = zil_alloc_commit_waiter(); 3505 zil_commit_itx_assign(zilog, zcw); 3506 3507 uint64_t wtxg = zil_commit_writer(zilog, zcw); 3508 zil_commit_waiter(zilog, zcw); 3509 3510 if (zcw->zcw_zio_error != 0) { 3511 /* 3512 * If there was an error writing out the ZIL blocks that 3513 * this thread is waiting on, then we fallback to 3514 * relying on spa_sync() to write out the data this 3515 * thread is waiting on. Obviously this has performance 3516 * implications, but the expectation is for this to be 3517 * an exceptional case, and shouldn't occur often. 3518 */ 3519 DTRACE_PROBE2(zil__commit__io__error, 3520 zilog_t *, zilog, zil_commit_waiter_t *, zcw); 3521 txg_wait_synced(zilog->zl_dmu_pool, 0); 3522 } else if (wtxg != 0) { 3523 txg_wait_synced(zilog->zl_dmu_pool, wtxg); 3524 } 3525 3526 zil_free_commit_waiter(zcw); 3527 } 3528 3529 /* 3530 * Called in syncing context to free committed log blocks and update log header. 3531 */ 3532 void 3533 zil_sync(zilog_t *zilog, dmu_tx_t *tx) 3534 { 3535 zil_header_t *zh = zil_header_in_syncing_context(zilog); 3536 uint64_t txg = dmu_tx_get_txg(tx); 3537 spa_t *spa = zilog->zl_spa; 3538 uint64_t *replayed_seq = &zilog->zl_replayed_seq[txg & TXG_MASK]; 3539 lwb_t *lwb; 3540 3541 /* 3542 * We don't zero out zl_destroy_txg, so make sure we don't try 3543 * to destroy it twice. 3544 */ 3545 if (spa_sync_pass(spa) != 1) 3546 return; 3547 3548 zil_lwb_flush_wait_all(zilog, txg); 3549 3550 mutex_enter(&zilog->zl_lock); 3551 3552 ASSERT(zilog->zl_stop_sync == 0); 3553 3554 if (*replayed_seq != 0) { 3555 ASSERT(zh->zh_replay_seq < *replayed_seq); 3556 zh->zh_replay_seq = *replayed_seq; 3557 *replayed_seq = 0; 3558 } 3559 3560 if (zilog->zl_destroy_txg == txg) { 3561 blkptr_t blk = zh->zh_log; 3562 dsl_dataset_t *ds = dmu_objset_ds(zilog->zl_os); 3563 3564 ASSERT(list_is_empty(&zilog->zl_lwb_list)); 3565 3566 memset(zh, 0, sizeof (zil_header_t)); 3567 memset(zilog->zl_replayed_seq, 0, 3568 sizeof (zilog->zl_replayed_seq)); 3569 3570 if (zilog->zl_keep_first) { 3571 /* 3572 * If this block was part of log chain that couldn't 3573 * be claimed because a device was missing during 3574 * zil_claim(), but that device later returns, 3575 * then this block could erroneously appear valid. 3576 * To guard against this, assign a new GUID to the new 3577 * log chain so it doesn't matter what blk points to. 3578 */ 3579 zil_init_log_chain(zilog, &blk); 3580 zh->zh_log = blk; 3581 } else { 3582 /* 3583 * A destroyed ZIL chain can't contain any TX_SETSAXATTR 3584 * records. So, deactivate the feature for this dataset. 3585 * We activate it again when we start a new ZIL chain. 3586 */ 3587 if (dsl_dataset_feature_is_active(ds, 3588 SPA_FEATURE_ZILSAXATTR)) 3589 dsl_dataset_deactivate_feature(ds, 3590 SPA_FEATURE_ZILSAXATTR, tx); 3591 } 3592 } 3593 3594 while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 3595 zh->zh_log = lwb->lwb_blk; 3596 if (lwb->lwb_state != LWB_STATE_FLUSH_DONE || 3597 lwb->lwb_alloc_txg > txg || lwb->lwb_max_txg > txg) 3598 break; 3599 list_remove(&zilog->zl_lwb_list, lwb); 3600 if (!BP_IS_HOLE(&lwb->lwb_blk)) 3601 zio_free(spa, txg, &lwb->lwb_blk); 3602 zil_free_lwb(zilog, lwb); 3603 3604 /* 3605 * If we don't have anything left in the lwb list then 3606 * we've had an allocation failure and we need to zero 3607 * out the zil_header blkptr so that we don't end 3608 * up freeing the same block twice. 3609 */ 3610 if (list_is_empty(&zilog->zl_lwb_list)) 3611 BP_ZERO(&zh->zh_log); 3612 } 3613 3614 mutex_exit(&zilog->zl_lock); 3615 } 3616 3617 static int 3618 zil_lwb_cons(void *vbuf, void *unused, int kmflag) 3619 { 3620 (void) unused, (void) kmflag; 3621 lwb_t *lwb = vbuf; 3622 list_create(&lwb->lwb_itxs, sizeof (itx_t), offsetof(itx_t, itx_node)); 3623 list_create(&lwb->lwb_waiters, sizeof (zil_commit_waiter_t), 3624 offsetof(zil_commit_waiter_t, zcw_node)); 3625 avl_create(&lwb->lwb_vdev_tree, zil_lwb_vdev_compare, 3626 sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node)); 3627 mutex_init(&lwb->lwb_vdev_lock, NULL, MUTEX_DEFAULT, NULL); 3628 return (0); 3629 } 3630 3631 static void 3632 zil_lwb_dest(void *vbuf, void *unused) 3633 { 3634 (void) unused; 3635 lwb_t *lwb = vbuf; 3636 mutex_destroy(&lwb->lwb_vdev_lock); 3637 avl_destroy(&lwb->lwb_vdev_tree); 3638 list_destroy(&lwb->lwb_waiters); 3639 list_destroy(&lwb->lwb_itxs); 3640 } 3641 3642 void 3643 zil_init(void) 3644 { 3645 zil_lwb_cache = kmem_cache_create("zil_lwb_cache", 3646 sizeof (lwb_t), 0, zil_lwb_cons, zil_lwb_dest, NULL, NULL, NULL, 0); 3647 3648 zil_zcw_cache = kmem_cache_create("zil_zcw_cache", 3649 sizeof (zil_commit_waiter_t), 0, NULL, NULL, NULL, NULL, NULL, 0); 3650 3651 zil_sums_init(&zil_sums_global); 3652 zil_kstats_global = kstat_create("zfs", 0, "zil", "misc", 3653 KSTAT_TYPE_NAMED, sizeof (zil_stats) / sizeof (kstat_named_t), 3654 KSTAT_FLAG_VIRTUAL); 3655 3656 if (zil_kstats_global != NULL) { 3657 zil_kstats_global->ks_data = &zil_stats; 3658 zil_kstats_global->ks_update = zil_kstats_global_update; 3659 zil_kstats_global->ks_private = NULL; 3660 kstat_install(zil_kstats_global); 3661 } 3662 } 3663 3664 void 3665 zil_fini(void) 3666 { 3667 kmem_cache_destroy(zil_zcw_cache); 3668 kmem_cache_destroy(zil_lwb_cache); 3669 3670 if (zil_kstats_global != NULL) { 3671 kstat_delete(zil_kstats_global); 3672 zil_kstats_global = NULL; 3673 } 3674 3675 zil_sums_fini(&zil_sums_global); 3676 } 3677 3678 void 3679 zil_set_sync(zilog_t *zilog, uint64_t sync) 3680 { 3681 zilog->zl_sync = sync; 3682 } 3683 3684 void 3685 zil_set_logbias(zilog_t *zilog, uint64_t logbias) 3686 { 3687 zilog->zl_logbias = logbias; 3688 } 3689 3690 zilog_t * 3691 zil_alloc(objset_t *os, zil_header_t *zh_phys) 3692 { 3693 zilog_t *zilog; 3694 3695 zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP); 3696 3697 zilog->zl_header = zh_phys; 3698 zilog->zl_os = os; 3699 zilog->zl_spa = dmu_objset_spa(os); 3700 zilog->zl_dmu_pool = dmu_objset_pool(os); 3701 zilog->zl_destroy_txg = TXG_INITIAL - 1; 3702 zilog->zl_logbias = dmu_objset_logbias(os); 3703 zilog->zl_sync = dmu_objset_syncprop(os); 3704 zilog->zl_dirty_max_txg = 0; 3705 zilog->zl_last_lwb_opened = NULL; 3706 zilog->zl_last_lwb_latency = 0; 3707 zilog->zl_max_block_size = zil_maxblocksize; 3708 3709 mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL); 3710 mutex_init(&zilog->zl_issuer_lock, NULL, MUTEX_DEFAULT, NULL); 3711 mutex_init(&zilog->zl_lwb_io_lock, NULL, MUTEX_DEFAULT, NULL); 3712 3713 for (int i = 0; i < TXG_SIZE; i++) { 3714 mutex_init(&zilog->zl_itxg[i].itxg_lock, NULL, 3715 MUTEX_DEFAULT, NULL); 3716 } 3717 3718 list_create(&zilog->zl_lwb_list, sizeof (lwb_t), 3719 offsetof(lwb_t, lwb_node)); 3720 3721 list_create(&zilog->zl_itx_commit_list, sizeof (itx_t), 3722 offsetof(itx_t, itx_node)); 3723 3724 cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL); 3725 cv_init(&zilog->zl_lwb_io_cv, NULL, CV_DEFAULT, NULL); 3726 3727 return (zilog); 3728 } 3729 3730 void 3731 zil_free(zilog_t *zilog) 3732 { 3733 int i; 3734 3735 zilog->zl_stop_sync = 1; 3736 3737 ASSERT0(zilog->zl_suspend); 3738 ASSERT0(zilog->zl_suspending); 3739 3740 ASSERT(list_is_empty(&zilog->zl_lwb_list)); 3741 list_destroy(&zilog->zl_lwb_list); 3742 3743 ASSERT(list_is_empty(&zilog->zl_itx_commit_list)); 3744 list_destroy(&zilog->zl_itx_commit_list); 3745 3746 for (i = 0; i < TXG_SIZE; i++) { 3747 /* 3748 * It's possible for an itx to be generated that doesn't dirty 3749 * a txg (e.g. ztest TX_TRUNCATE). So there's no zil_clean() 3750 * callback to remove the entry. We remove those here. 3751 * 3752 * Also free up the ziltest itxs. 3753 */ 3754 if (zilog->zl_itxg[i].itxg_itxs) 3755 zil_itxg_clean(zilog->zl_itxg[i].itxg_itxs); 3756 mutex_destroy(&zilog->zl_itxg[i].itxg_lock); 3757 } 3758 3759 mutex_destroy(&zilog->zl_issuer_lock); 3760 mutex_destroy(&zilog->zl_lock); 3761 mutex_destroy(&zilog->zl_lwb_io_lock); 3762 3763 cv_destroy(&zilog->zl_cv_suspend); 3764 cv_destroy(&zilog->zl_lwb_io_cv); 3765 3766 kmem_free(zilog, sizeof (zilog_t)); 3767 } 3768 3769 /* 3770 * Open an intent log. 3771 */ 3772 zilog_t * 3773 zil_open(objset_t *os, zil_get_data_t *get_data, zil_sums_t *zil_sums) 3774 { 3775 zilog_t *zilog = dmu_objset_zil(os); 3776 3777 ASSERT3P(zilog->zl_get_data, ==, NULL); 3778 ASSERT3P(zilog->zl_last_lwb_opened, ==, NULL); 3779 ASSERT(list_is_empty(&zilog->zl_lwb_list)); 3780 3781 zilog->zl_get_data = get_data; 3782 zilog->zl_sums = zil_sums; 3783 3784 return (zilog); 3785 } 3786 3787 /* 3788 * Close an intent log. 3789 */ 3790 void 3791 zil_close(zilog_t *zilog) 3792 { 3793 lwb_t *lwb; 3794 uint64_t txg; 3795 3796 if (!dmu_objset_is_snapshot(zilog->zl_os)) { 3797 zil_commit(zilog, 0); 3798 } else { 3799 ASSERT(list_is_empty(&zilog->zl_lwb_list)); 3800 ASSERT0(zilog->zl_dirty_max_txg); 3801 ASSERT3B(zilog_is_dirty(zilog), ==, B_FALSE); 3802 } 3803 3804 mutex_enter(&zilog->zl_lock); 3805 txg = zilog->zl_dirty_max_txg; 3806 lwb = list_tail(&zilog->zl_lwb_list); 3807 if (lwb != NULL) { 3808 txg = MAX(txg, lwb->lwb_alloc_txg); 3809 txg = MAX(txg, lwb->lwb_max_txg); 3810 } 3811 mutex_exit(&zilog->zl_lock); 3812 3813 /* 3814 * zl_lwb_max_issued_txg may be larger than lwb_max_txg. It depends 3815 * on the time when the dmu_tx transaction is assigned in 3816 * zil_lwb_write_issue(). 3817 */ 3818 mutex_enter(&zilog->zl_lwb_io_lock); 3819 txg = MAX(zilog->zl_lwb_max_issued_txg, txg); 3820 mutex_exit(&zilog->zl_lwb_io_lock); 3821 3822 /* 3823 * We need to use txg_wait_synced() to wait until that txg is synced. 3824 * zil_sync() will guarantee all lwbs up to that txg have been 3825 * written out, flushed, and cleaned. 3826 */ 3827 if (txg != 0) 3828 txg_wait_synced(zilog->zl_dmu_pool, txg); 3829 3830 if (zilog_is_dirty(zilog)) 3831 zfs_dbgmsg("zil (%px) is dirty, txg %llu", zilog, 3832 (u_longlong_t)txg); 3833 if (txg < spa_freeze_txg(zilog->zl_spa)) 3834 VERIFY(!zilog_is_dirty(zilog)); 3835 3836 zilog->zl_get_data = NULL; 3837 3838 /* 3839 * We should have only one lwb left on the list; remove it now. 3840 */ 3841 mutex_enter(&zilog->zl_lock); 3842 lwb = list_remove_head(&zilog->zl_lwb_list); 3843 if (lwb != NULL) { 3844 ASSERT(list_is_empty(&zilog->zl_lwb_list)); 3845 ASSERT3S(lwb->lwb_state, ==, LWB_STATE_NEW); 3846 zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 3847 zil_free_lwb(zilog, lwb); 3848 } 3849 mutex_exit(&zilog->zl_lock); 3850 } 3851 3852 static const char *suspend_tag = "zil suspending"; 3853 3854 /* 3855 * Suspend an intent log. While in suspended mode, we still honor 3856 * synchronous semantics, but we rely on txg_wait_synced() to do it. 3857 * On old version pools, we suspend the log briefly when taking a 3858 * snapshot so that it will have an empty intent log. 3859 * 3860 * Long holds are not really intended to be used the way we do here -- 3861 * held for such a short time. A concurrent caller of dsl_dataset_long_held() 3862 * could fail. Therefore we take pains to only put a long hold if it is 3863 * actually necessary. Fortunately, it will only be necessary if the 3864 * objset is currently mounted (or the ZVOL equivalent). In that case it 3865 * will already have a long hold, so we are not really making things any worse. 3866 * 3867 * Ideally, we would locate the existing long-holder (i.e. the zfsvfs_t or 3868 * zvol_state_t), and use their mechanism to prevent their hold from being 3869 * dropped (e.g. VFS_HOLD()). However, that would be even more pain for 3870 * very little gain. 3871 * 3872 * if cookiep == NULL, this does both the suspend & resume. 3873 * Otherwise, it returns with the dataset "long held", and the cookie 3874 * should be passed into zil_resume(). 3875 */ 3876 int 3877 zil_suspend(const char *osname, void **cookiep) 3878 { 3879 objset_t *os; 3880 zilog_t *zilog; 3881 const zil_header_t *zh; 3882 int error; 3883 3884 error = dmu_objset_hold(osname, suspend_tag, &os); 3885 if (error != 0) 3886 return (error); 3887 zilog = dmu_objset_zil(os); 3888 3889 mutex_enter(&zilog->zl_lock); 3890 zh = zilog->zl_header; 3891 3892 if (zh->zh_flags & ZIL_REPLAY_NEEDED) { /* unplayed log */ 3893 mutex_exit(&zilog->zl_lock); 3894 dmu_objset_rele(os, suspend_tag); 3895 return (SET_ERROR(EBUSY)); 3896 } 3897 3898 /* 3899 * Don't put a long hold in the cases where we can avoid it. This 3900 * is when there is no cookie so we are doing a suspend & resume 3901 * (i.e. called from zil_vdev_offline()), and there's nothing to do 3902 * for the suspend because it's already suspended, or there's no ZIL. 3903 */ 3904 if (cookiep == NULL && !zilog->zl_suspending && 3905 (zilog->zl_suspend > 0 || BP_IS_HOLE(&zh->zh_log))) { 3906 mutex_exit(&zilog->zl_lock); 3907 dmu_objset_rele(os, suspend_tag); 3908 return (0); 3909 } 3910 3911 dsl_dataset_long_hold(dmu_objset_ds(os), suspend_tag); 3912 dsl_pool_rele(dmu_objset_pool(os), suspend_tag); 3913 3914 zilog->zl_suspend++; 3915 3916 if (zilog->zl_suspend > 1) { 3917 /* 3918 * Someone else is already suspending it. 3919 * Just wait for them to finish. 3920 */ 3921 3922 while (zilog->zl_suspending) 3923 cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock); 3924 mutex_exit(&zilog->zl_lock); 3925 3926 if (cookiep == NULL) 3927 zil_resume(os); 3928 else 3929 *cookiep = os; 3930 return (0); 3931 } 3932 3933 /* 3934 * If there is no pointer to an on-disk block, this ZIL must not 3935 * be active (e.g. filesystem not mounted), so there's nothing 3936 * to clean up. 3937 */ 3938 if (BP_IS_HOLE(&zh->zh_log)) { 3939 ASSERT(cookiep != NULL); /* fast path already handled */ 3940 3941 *cookiep = os; 3942 mutex_exit(&zilog->zl_lock); 3943 return (0); 3944 } 3945 3946 /* 3947 * The ZIL has work to do. Ensure that the associated encryption 3948 * key will remain mapped while we are committing the log by 3949 * grabbing a reference to it. If the key isn't loaded we have no 3950 * choice but to return an error until the wrapping key is loaded. 3951 */ 3952 if (os->os_encrypted && 3953 dsl_dataset_create_key_mapping(dmu_objset_ds(os)) != 0) { 3954 zilog->zl_suspend--; 3955 mutex_exit(&zilog->zl_lock); 3956 dsl_dataset_long_rele(dmu_objset_ds(os), suspend_tag); 3957 dsl_dataset_rele(dmu_objset_ds(os), suspend_tag); 3958 return (SET_ERROR(EACCES)); 3959 } 3960 3961 zilog->zl_suspending = B_TRUE; 3962 mutex_exit(&zilog->zl_lock); 3963 3964 /* 3965 * We need to use zil_commit_impl to ensure we wait for all 3966 * LWB_STATE_OPENED, _CLOSED and _READY lwbs to be committed 3967 * to disk before proceeding. If we used zil_commit instead, it 3968 * would just call txg_wait_synced(), because zl_suspend is set. 3969 * txg_wait_synced() doesn't wait for these lwb's to be 3970 * LWB_STATE_FLUSH_DONE before returning. 3971 */ 3972 zil_commit_impl(zilog, 0); 3973 3974 /* 3975 * Now that we've ensured all lwb's are LWB_STATE_FLUSH_DONE, we 3976 * use txg_wait_synced() to ensure the data from the zilog has 3977 * migrated to the main pool before calling zil_destroy(). 3978 */ 3979 txg_wait_synced(zilog->zl_dmu_pool, 0); 3980 3981 zil_destroy(zilog, B_FALSE); 3982 3983 mutex_enter(&zilog->zl_lock); 3984 zilog->zl_suspending = B_FALSE; 3985 cv_broadcast(&zilog->zl_cv_suspend); 3986 mutex_exit(&zilog->zl_lock); 3987 3988 if (os->os_encrypted) 3989 dsl_dataset_remove_key_mapping(dmu_objset_ds(os)); 3990 3991 if (cookiep == NULL) 3992 zil_resume(os); 3993 else 3994 *cookiep = os; 3995 return (0); 3996 } 3997 3998 void 3999 zil_resume(void *cookie) 4000 { 4001 objset_t *os = cookie; 4002 zilog_t *zilog = dmu_objset_zil(os); 4003 4004 mutex_enter(&zilog->zl_lock); 4005 ASSERT(zilog->zl_suspend != 0); 4006 zilog->zl_suspend--; 4007 mutex_exit(&zilog->zl_lock); 4008 dsl_dataset_long_rele(dmu_objset_ds(os), suspend_tag); 4009 dsl_dataset_rele(dmu_objset_ds(os), suspend_tag); 4010 } 4011 4012 typedef struct zil_replay_arg { 4013 zil_replay_func_t *const *zr_replay; 4014 void *zr_arg; 4015 boolean_t zr_byteswap; 4016 char *zr_lr; 4017 } zil_replay_arg_t; 4018 4019 static int 4020 zil_replay_error(zilog_t *zilog, const lr_t *lr, int error) 4021 { 4022 char name[ZFS_MAX_DATASET_NAME_LEN]; 4023 4024 zilog->zl_replaying_seq--; /* didn't actually replay this one */ 4025 4026 dmu_objset_name(zilog->zl_os, name); 4027 4028 cmn_err(CE_WARN, "ZFS replay transaction error %d, " 4029 "dataset %s, seq 0x%llx, txtype %llu %s\n", error, name, 4030 (u_longlong_t)lr->lrc_seq, 4031 (u_longlong_t)(lr->lrc_txtype & ~TX_CI), 4032 (lr->lrc_txtype & TX_CI) ? "CI" : ""); 4033 4034 return (error); 4035 } 4036 4037 static int 4038 zil_replay_log_record(zilog_t *zilog, const lr_t *lr, void *zra, 4039 uint64_t claim_txg) 4040 { 4041 zil_replay_arg_t *zr = zra; 4042 const zil_header_t *zh = zilog->zl_header; 4043 uint64_t reclen = lr->lrc_reclen; 4044 uint64_t txtype = lr->lrc_txtype; 4045 int error = 0; 4046 4047 zilog->zl_replaying_seq = lr->lrc_seq; 4048 4049 if (lr->lrc_seq <= zh->zh_replay_seq) /* already replayed */ 4050 return (0); 4051 4052 if (lr->lrc_txg < claim_txg) /* already committed */ 4053 return (0); 4054 4055 /* Strip case-insensitive bit, still present in log record */ 4056 txtype &= ~TX_CI; 4057 4058 if (txtype == 0 || txtype >= TX_MAX_TYPE) 4059 return (zil_replay_error(zilog, lr, EINVAL)); 4060 4061 /* 4062 * If this record type can be logged out of order, the object 4063 * (lr_foid) may no longer exist. That's legitimate, not an error. 4064 */ 4065 if (TX_OOO(txtype)) { 4066 error = dmu_object_info(zilog->zl_os, 4067 LR_FOID_GET_OBJ(((lr_ooo_t *)lr)->lr_foid), NULL); 4068 if (error == ENOENT || error == EEXIST) 4069 return (0); 4070 } 4071 4072 /* 4073 * Make a copy of the data so we can revise and extend it. 4074 */ 4075 memcpy(zr->zr_lr, lr, reclen); 4076 4077 /* 4078 * If this is a TX_WRITE with a blkptr, suck in the data. 4079 */ 4080 if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) { 4081 error = zil_read_log_data(zilog, (lr_write_t *)lr, 4082 zr->zr_lr + reclen); 4083 if (error != 0) 4084 return (zil_replay_error(zilog, lr, error)); 4085 } 4086 4087 /* 4088 * The log block containing this lr may have been byteswapped 4089 * so that we can easily examine common fields like lrc_txtype. 4090 * However, the log is a mix of different record types, and only the 4091 * replay vectors know how to byteswap their records. Therefore, if 4092 * the lr was byteswapped, undo it before invoking the replay vector. 4093 */ 4094 if (zr->zr_byteswap) 4095 byteswap_uint64_array(zr->zr_lr, reclen); 4096 4097 /* 4098 * We must now do two things atomically: replay this log record, 4099 * and update the log header sequence number to reflect the fact that 4100 * we did so. At the end of each replay function the sequence number 4101 * is updated if we are in replay mode. 4102 */ 4103 error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, zr->zr_byteswap); 4104 if (error != 0) { 4105 /* 4106 * The DMU's dnode layer doesn't see removes until the txg 4107 * commits, so a subsequent claim can spuriously fail with 4108 * EEXIST. So if we receive any error we try syncing out 4109 * any removes then retry the transaction. Note that we 4110 * specify B_FALSE for byteswap now, so we don't do it twice. 4111 */ 4112 txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0); 4113 error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, B_FALSE); 4114 if (error != 0) 4115 return (zil_replay_error(zilog, lr, error)); 4116 } 4117 return (0); 4118 } 4119 4120 static int 4121 zil_incr_blks(zilog_t *zilog, const blkptr_t *bp, void *arg, uint64_t claim_txg) 4122 { 4123 (void) bp, (void) arg, (void) claim_txg; 4124 4125 zilog->zl_replay_blks++; 4126 4127 return (0); 4128 } 4129 4130 /* 4131 * If this dataset has a non-empty intent log, replay it and destroy it. 4132 * Return B_TRUE if there were any entries to replay. 4133 */ 4134 boolean_t 4135 zil_replay(objset_t *os, void *arg, 4136 zil_replay_func_t *const replay_func[TX_MAX_TYPE]) 4137 { 4138 zilog_t *zilog = dmu_objset_zil(os); 4139 const zil_header_t *zh = zilog->zl_header; 4140 zil_replay_arg_t zr; 4141 4142 if ((zh->zh_flags & ZIL_REPLAY_NEEDED) == 0) { 4143 return (zil_destroy(zilog, B_TRUE)); 4144 } 4145 4146 zr.zr_replay = replay_func; 4147 zr.zr_arg = arg; 4148 zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log); 4149 zr.zr_lr = vmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP); 4150 4151 /* 4152 * Wait for in-progress removes to sync before starting replay. 4153 */ 4154 txg_wait_synced(zilog->zl_dmu_pool, 0); 4155 4156 zilog->zl_replay = B_TRUE; 4157 zilog->zl_replay_time = ddi_get_lbolt(); 4158 ASSERT(zilog->zl_replay_blks == 0); 4159 (void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr, 4160 zh->zh_claim_txg, B_TRUE); 4161 vmem_free(zr.zr_lr, 2 * SPA_MAXBLOCKSIZE); 4162 4163 zil_destroy(zilog, B_FALSE); 4164 txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 4165 zilog->zl_replay = B_FALSE; 4166 4167 return (B_TRUE); 4168 } 4169 4170 boolean_t 4171 zil_replaying(zilog_t *zilog, dmu_tx_t *tx) 4172 { 4173 if (zilog->zl_sync == ZFS_SYNC_DISABLED) 4174 return (B_TRUE); 4175 4176 if (zilog->zl_replay) { 4177 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 4178 zilog->zl_replayed_seq[dmu_tx_get_txg(tx) & TXG_MASK] = 4179 zilog->zl_replaying_seq; 4180 return (B_TRUE); 4181 } 4182 4183 return (B_FALSE); 4184 } 4185 4186 int 4187 zil_reset(const char *osname, void *arg) 4188 { 4189 (void) arg; 4190 4191 int error = zil_suspend(osname, NULL); 4192 /* EACCES means crypto key not loaded */ 4193 if ((error == EACCES) || (error == EBUSY)) 4194 return (SET_ERROR(error)); 4195 if (error != 0) 4196 return (SET_ERROR(EEXIST)); 4197 return (0); 4198 } 4199 4200 EXPORT_SYMBOL(zil_alloc); 4201 EXPORT_SYMBOL(zil_free); 4202 EXPORT_SYMBOL(zil_open); 4203 EXPORT_SYMBOL(zil_close); 4204 EXPORT_SYMBOL(zil_replay); 4205 EXPORT_SYMBOL(zil_replaying); 4206 EXPORT_SYMBOL(zil_destroy); 4207 EXPORT_SYMBOL(zil_destroy_sync); 4208 EXPORT_SYMBOL(zil_itx_create); 4209 EXPORT_SYMBOL(zil_itx_destroy); 4210 EXPORT_SYMBOL(zil_itx_assign); 4211 EXPORT_SYMBOL(zil_commit); 4212 EXPORT_SYMBOL(zil_claim); 4213 EXPORT_SYMBOL(zil_check_log_chain); 4214 EXPORT_SYMBOL(zil_sync); 4215 EXPORT_SYMBOL(zil_clean); 4216 EXPORT_SYMBOL(zil_suspend); 4217 EXPORT_SYMBOL(zil_resume); 4218 EXPORT_SYMBOL(zil_lwb_add_block); 4219 EXPORT_SYMBOL(zil_bp_tree_add); 4220 EXPORT_SYMBOL(zil_set_sync); 4221 EXPORT_SYMBOL(zil_set_logbias); 4222 EXPORT_SYMBOL(zil_sums_init); 4223 EXPORT_SYMBOL(zil_sums_fini); 4224 EXPORT_SYMBOL(zil_kstat_values_update); 4225 4226 ZFS_MODULE_PARAM(zfs, zfs_, commit_timeout_pct, UINT, ZMOD_RW, 4227 "ZIL block open timeout percentage"); 4228 4229 ZFS_MODULE_PARAM(zfs_zil, zil_, replay_disable, INT, ZMOD_RW, 4230 "Disable intent logging replay"); 4231 4232 ZFS_MODULE_PARAM(zfs_zil, zil_, nocacheflush, INT, ZMOD_RW, 4233 "Disable ZIL cache flushes"); 4234 4235 ZFS_MODULE_PARAM(zfs_zil, zil_, slog_bulk, U64, ZMOD_RW, 4236 "Limit in bytes slog sync writes per commit"); 4237 4238 ZFS_MODULE_PARAM(zfs_zil, zil_, maxblocksize, UINT, ZMOD_RW, 4239 "Limit in bytes of ZIL log block size"); 4240 4241 ZFS_MODULE_PARAM(zfs_zil, zil_, maxcopied, UINT, ZMOD_RW, 4242 "Limit in bytes WR_COPIED size"); 4243