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