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