1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5fe9cf88cSperrin * Common Development and Distribution License (the "License"). 6fe9cf88cSperrin * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 22ab69d62fSMatthew Ahrens * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23fa9e4066Sahrens * Use is subject to license terms. 24fa9e4066Sahrens */ 25fa9e4066Sahrens 26fa9e4066Sahrens #include <sys/zfs_context.h> 27fa9e4066Sahrens #include <sys/spa.h> 28e6ca193dSGeorge Wilson #include <sys/spa_impl.h> 29fa9e4066Sahrens #include <sys/dmu.h> 30fa9e4066Sahrens #include <sys/zap.h> 31fa9e4066Sahrens #include <sys/arc.h> 32fa9e4066Sahrens #include <sys/stat.h> 33fa9e4066Sahrens #include <sys/resource.h> 34fa9e4066Sahrens #include <sys/zil.h> 35fa9e4066Sahrens #include <sys/zil_impl.h> 36fa9e4066Sahrens #include <sys/dsl_dataset.h> 37fa9e4066Sahrens #include <sys/vdev.h> 38d63d470bSgw #include <sys/dmu_tx.h> 39fa9e4066Sahrens 40fa9e4066Sahrens /* 41fa9e4066Sahrens * The zfs intent log (ZIL) saves transaction records of system calls 42fa9e4066Sahrens * that change the file system in memory with enough information 43fa9e4066Sahrens * to be able to replay them. These are stored in memory until 44fa9e4066Sahrens * either the DMU transaction group (txg) commits them to the stable pool 45fa9e4066Sahrens * and they can be discarded, or they are flushed to the stable log 46fa9e4066Sahrens * (also in the pool) due to a fsync, O_DSYNC or other synchronous 47fa9e4066Sahrens * requirement. In the event of a panic or power fail then those log 48fa9e4066Sahrens * records (transactions) are replayed. 49fa9e4066Sahrens * 50fa9e4066Sahrens * There is one ZIL per file system. Its on-disk (pool) format consists 51fa9e4066Sahrens * of 3 parts: 52fa9e4066Sahrens * 53fa9e4066Sahrens * - ZIL header 54fa9e4066Sahrens * - ZIL blocks 55fa9e4066Sahrens * - ZIL records 56fa9e4066Sahrens * 57fa9e4066Sahrens * A log record holds a system call transaction. Log blocks can 58fa9e4066Sahrens * hold many log records and the blocks are chained together. 59fa9e4066Sahrens * Each ZIL block contains a block pointer (blkptr_t) to the next 60fa9e4066Sahrens * ZIL block in the chain. The ZIL header points to the first 61fa9e4066Sahrens * block in the chain. Note there is not a fixed place in the pool 62fa9e4066Sahrens * to hold blocks. They are dynamically allocated and freed as 63fa9e4066Sahrens * needed from the blocks available. Figure X shows the ZIL structure: 64fa9e4066Sahrens */ 65fa9e4066Sahrens 66fa9e4066Sahrens /* 67416e0cd8Sek * This global ZIL switch affects all pools 68fa9e4066Sahrens */ 69fa9e4066Sahrens int zil_disable = 0; /* disable intent logging */ 70416e0cd8Sek 71416e0cd8Sek /* 72416e0cd8Sek * Tunable parameter for debugging or performance analysis. Setting 73416e0cd8Sek * zfs_nocacheflush will cause corruption on power loss if a volatile 74416e0cd8Sek * out-of-order write cache is enabled. 75416e0cd8Sek */ 76416e0cd8Sek boolean_t zfs_nocacheflush = B_FALSE; 77fa9e4066Sahrens 78fa9e4066Sahrens static kmem_cache_t *zil_lwb_cache; 79fa9e4066Sahrens 808f18d1faSGeorge Wilson static boolean_t zil_empty(zilog_t *zilog); 818f18d1faSGeorge Wilson 82fa9e4066Sahrens static int 83fa9e4066Sahrens zil_dva_compare(const void *x1, const void *x2) 84fa9e4066Sahrens { 85fa9e4066Sahrens const dva_t *dva1 = x1; 86fa9e4066Sahrens const dva_t *dva2 = x2; 87fa9e4066Sahrens 88fa9e4066Sahrens if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2)) 89fa9e4066Sahrens return (-1); 90fa9e4066Sahrens if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2)) 91fa9e4066Sahrens return (1); 92fa9e4066Sahrens 93fa9e4066Sahrens if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2)) 94fa9e4066Sahrens return (-1); 95fa9e4066Sahrens if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2)) 96fa9e4066Sahrens return (1); 97fa9e4066Sahrens 98fa9e4066Sahrens return (0); 99fa9e4066Sahrens } 100fa9e4066Sahrens 101fa9e4066Sahrens static void 102fa9e4066Sahrens zil_dva_tree_init(avl_tree_t *t) 103fa9e4066Sahrens { 104fa9e4066Sahrens avl_create(t, zil_dva_compare, sizeof (zil_dva_node_t), 105fa9e4066Sahrens offsetof(zil_dva_node_t, zn_node)); 106fa9e4066Sahrens } 107fa9e4066Sahrens 108fa9e4066Sahrens static void 109fa9e4066Sahrens zil_dva_tree_fini(avl_tree_t *t) 110fa9e4066Sahrens { 111fa9e4066Sahrens zil_dva_node_t *zn; 112fa9e4066Sahrens void *cookie = NULL; 113fa9e4066Sahrens 114fa9e4066Sahrens while ((zn = avl_destroy_nodes(t, &cookie)) != NULL) 115fa9e4066Sahrens kmem_free(zn, sizeof (zil_dva_node_t)); 116fa9e4066Sahrens 117fa9e4066Sahrens avl_destroy(t); 118fa9e4066Sahrens } 119fa9e4066Sahrens 120fa9e4066Sahrens static int 121fa9e4066Sahrens zil_dva_tree_add(avl_tree_t *t, dva_t *dva) 122fa9e4066Sahrens { 123fa9e4066Sahrens zil_dva_node_t *zn; 124fa9e4066Sahrens avl_index_t where; 125fa9e4066Sahrens 126fa9e4066Sahrens if (avl_find(t, dva, &where) != NULL) 127fa9e4066Sahrens return (EEXIST); 128fa9e4066Sahrens 129fa9e4066Sahrens zn = kmem_alloc(sizeof (zil_dva_node_t), KM_SLEEP); 130fa9e4066Sahrens zn->zn_dva = *dva; 131fa9e4066Sahrens avl_insert(t, zn, where); 132fa9e4066Sahrens 133fa9e4066Sahrens return (0); 134fa9e4066Sahrens } 135fa9e4066Sahrens 136d80c45e0Sbonwick static zil_header_t * 137d80c45e0Sbonwick zil_header_in_syncing_context(zilog_t *zilog) 138d80c45e0Sbonwick { 139d80c45e0Sbonwick return ((zil_header_t *)zilog->zl_header); 140d80c45e0Sbonwick } 141d80c45e0Sbonwick 142d80c45e0Sbonwick static void 143d80c45e0Sbonwick zil_init_log_chain(zilog_t *zilog, blkptr_t *bp) 144d80c45e0Sbonwick { 145d80c45e0Sbonwick zio_cksum_t *zc = &bp->blk_cksum; 146d80c45e0Sbonwick 147d80c45e0Sbonwick zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL); 148d80c45e0Sbonwick zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL); 149d80c45e0Sbonwick zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os); 150d80c45e0Sbonwick zc->zc_word[ZIL_ZC_SEQ] = 1ULL; 151d80c45e0Sbonwick } 152d80c45e0Sbonwick 153fa9e4066Sahrens /* 154fa9e4066Sahrens * Read a log block, make sure it's valid, and byteswap it if necessary. 155fa9e4066Sahrens */ 156fa9e4066Sahrens static int 157d80c45e0Sbonwick zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, arc_buf_t **abufpp) 158fa9e4066Sahrens { 159d80c45e0Sbonwick blkptr_t blk = *bp; 160ea8dc4b6Seschrock zbookmark_t zb; 16113506d1eSmaybee uint32_t aflags = ARC_WAIT; 162fa9e4066Sahrens int error; 163fa9e4066Sahrens 164d80c45e0Sbonwick zb.zb_objset = bp->blk_cksum.zc_word[ZIL_ZC_OBJSET]; 165ea8dc4b6Seschrock zb.zb_object = 0; 166ea8dc4b6Seschrock zb.zb_level = -1; 167d80c45e0Sbonwick zb.zb_blkid = bp->blk_cksum.zc_word[ZIL_ZC_SEQ]; 168ea8dc4b6Seschrock 169d80c45e0Sbonwick *abufpp = NULL; 170fa9e4066Sahrens 171088f3894Sahrens /* 172088f3894Sahrens * We shouldn't be doing any scrubbing while we're doing log 173088f3894Sahrens * replay, it's OK to not lock. 174088f3894Sahrens */ 175088f3894Sahrens error = arc_read_nolock(NULL, zilog->zl_spa, &blk, 176d80c45e0Sbonwick arc_getbuf_func, abufpp, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL | 17713506d1eSmaybee ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB, &aflags, &zb); 178fa9e4066Sahrens 179d80c45e0Sbonwick if (error == 0) { 180d80c45e0Sbonwick char *data = (*abufpp)->b_data; 181d80c45e0Sbonwick uint64_t blksz = BP_GET_LSIZE(bp); 182d80c45e0Sbonwick zil_trailer_t *ztp = (zil_trailer_t *)(data + blksz) - 1; 183d80c45e0Sbonwick zio_cksum_t cksum = bp->blk_cksum; 184fa9e4066Sahrens 185d80c45e0Sbonwick /* 186f5e6e722SNeil Perrin * Validate the checksummed log block. 187f5e6e722SNeil Perrin * 188d80c45e0Sbonwick * Sequence numbers should be... sequential. The checksum 189d80c45e0Sbonwick * verifier for the next block should be bp's checksum plus 1. 190f5e6e722SNeil Perrin * 191f5e6e722SNeil Perrin * Also check the log chain linkage and size used. 192d80c45e0Sbonwick */ 193d80c45e0Sbonwick cksum.zc_word[ZIL_ZC_SEQ]++; 194d80c45e0Sbonwick 195f5e6e722SNeil Perrin if (bcmp(&cksum, &ztp->zit_next_blk.blk_cksum, 196f5e6e722SNeil Perrin sizeof (cksum)) || BP_IS_HOLE(&ztp->zit_next_blk) || 197f5e6e722SNeil Perrin (ztp->zit_nused > (blksz - sizeof (zil_trailer_t)))) { 198f5e6e722SNeil Perrin error = ECKSUM; 199f5e6e722SNeil Perrin } 200fa9e4066Sahrens 201d80c45e0Sbonwick if (error) { 202d80c45e0Sbonwick VERIFY(arc_buf_remove_ref(*abufpp, abufpp) == 1); 203d80c45e0Sbonwick *abufpp = NULL; 204d80c45e0Sbonwick } 205fa9e4066Sahrens } 206fa9e4066Sahrens 207d80c45e0Sbonwick dprintf("error %d on %llu:%llu\n", error, zb.zb_objset, zb.zb_blkid); 208fa9e4066Sahrens 209d80c45e0Sbonwick return (error); 210fa9e4066Sahrens } 211fa9e4066Sahrens 212fa9e4066Sahrens /* 213fa9e4066Sahrens * Parse the intent log, and call parse_func for each valid record within. 214d80c45e0Sbonwick * Return the highest sequence number. 215fa9e4066Sahrens */ 216d80c45e0Sbonwick uint64_t 217fa9e4066Sahrens zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func, 218fa9e4066Sahrens zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg) 219fa9e4066Sahrens { 220d80c45e0Sbonwick const zil_header_t *zh = zilog->zl_header; 221d80c45e0Sbonwick uint64_t claim_seq = zh->zh_claim_seq; 222d80c45e0Sbonwick uint64_t seq = 0; 223d80c45e0Sbonwick uint64_t max_seq = 0; 224d80c45e0Sbonwick blkptr_t blk = zh->zh_log; 225d80c45e0Sbonwick arc_buf_t *abuf; 226fa9e4066Sahrens char *lrbuf, *lrp; 227fa9e4066Sahrens zil_trailer_t *ztp; 228fa9e4066Sahrens int reclen, error; 229fa9e4066Sahrens 230fa9e4066Sahrens if (BP_IS_HOLE(&blk)) 231d80c45e0Sbonwick return (max_seq); 232fa9e4066Sahrens 233fa9e4066Sahrens /* 234fa9e4066Sahrens * Starting at the block pointed to by zh_log we read the log chain. 235fa9e4066Sahrens * For each block in the chain we strongly check that block to 236fa9e4066Sahrens * ensure its validity. We stop when an invalid block is found. 237fa9e4066Sahrens * For each block pointer in the chain we call parse_blk_func(). 238fa9e4066Sahrens * For each record in each valid block we call parse_lr_func(). 239d80c45e0Sbonwick * If the log has been claimed, stop if we encounter a sequence 240d80c45e0Sbonwick * number greater than the highest claimed sequence number. 241fa9e4066Sahrens */ 242fa9e4066Sahrens zil_dva_tree_init(&zilog->zl_dva_tree); 243fa9e4066Sahrens for (;;) { 244d80c45e0Sbonwick seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 245d80c45e0Sbonwick 246d80c45e0Sbonwick if (claim_seq != 0 && seq > claim_seq) 247d80c45e0Sbonwick break; 248d80c45e0Sbonwick 249d80c45e0Sbonwick ASSERT(max_seq < seq); 250d80c45e0Sbonwick max_seq = seq; 251d80c45e0Sbonwick 252d80c45e0Sbonwick error = zil_read_log_block(zilog, &blk, &abuf); 253fa9e4066Sahrens 254fa9e4066Sahrens if (parse_blk_func != NULL) 255fa9e4066Sahrens parse_blk_func(zilog, &blk, arg, txg); 256fa9e4066Sahrens 257fa9e4066Sahrens if (error) 258fa9e4066Sahrens break; 259fa9e4066Sahrens 260d80c45e0Sbonwick lrbuf = abuf->b_data; 261fa9e4066Sahrens ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1; 262fa9e4066Sahrens blk = ztp->zit_next_blk; 263fa9e4066Sahrens 264d80c45e0Sbonwick if (parse_lr_func == NULL) { 265d80c45e0Sbonwick VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 266fa9e4066Sahrens continue; 267d80c45e0Sbonwick } 268fa9e4066Sahrens 269fa9e4066Sahrens for (lrp = lrbuf; lrp < lrbuf + ztp->zit_nused; lrp += reclen) { 270fa9e4066Sahrens lr_t *lr = (lr_t *)lrp; 271fa9e4066Sahrens reclen = lr->lrc_reclen; 272fa9e4066Sahrens ASSERT3U(reclen, >=, sizeof (lr_t)); 273fa9e4066Sahrens parse_lr_func(zilog, lr, arg, txg); 274fa9e4066Sahrens } 275d80c45e0Sbonwick VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 276fa9e4066Sahrens } 277fa9e4066Sahrens zil_dva_tree_fini(&zilog->zl_dva_tree); 278d80c45e0Sbonwick 279d80c45e0Sbonwick return (max_seq); 280fa9e4066Sahrens } 281fa9e4066Sahrens 282fa9e4066Sahrens /* ARGSUSED */ 283fa9e4066Sahrens static void 284fa9e4066Sahrens zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg) 285fa9e4066Sahrens { 286fa9e4066Sahrens spa_t *spa = zilog->zl_spa; 287fa9e4066Sahrens int err; 288fa9e4066Sahrens 289fa9e4066Sahrens /* 290fa9e4066Sahrens * Claim log block if not already committed and not already claimed. 291fa9e4066Sahrens */ 292fa9e4066Sahrens if (bp->blk_birth >= first_txg && 293fa9e4066Sahrens zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp)) == 0) { 294e14bb325SJeff Bonwick err = zio_wait(zio_claim(NULL, spa, first_txg, bp, NULL, NULL, 295e14bb325SJeff Bonwick ZIO_FLAG_MUSTSUCCEED)); 296fa9e4066Sahrens ASSERT(err == 0); 297fa9e4066Sahrens } 298fa9e4066Sahrens } 299fa9e4066Sahrens 300fa9e4066Sahrens static void 301fa9e4066Sahrens zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg) 302fa9e4066Sahrens { 303fa9e4066Sahrens if (lrc->lrc_txtype == TX_WRITE) { 304fa9e4066Sahrens lr_write_t *lr = (lr_write_t *)lrc; 305fa9e4066Sahrens zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg); 306fa9e4066Sahrens } 307fa9e4066Sahrens } 308fa9e4066Sahrens 309fa9e4066Sahrens /* ARGSUSED */ 310fa9e4066Sahrens static void 311fa9e4066Sahrens zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg) 312fa9e4066Sahrens { 313fa9e4066Sahrens zio_free_blk(zilog->zl_spa, bp, dmu_tx_get_txg(tx)); 314fa9e4066Sahrens } 315fa9e4066Sahrens 316fa9e4066Sahrens static void 317fa9e4066Sahrens zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg) 318fa9e4066Sahrens { 319fa9e4066Sahrens /* 320fa9e4066Sahrens * If we previously claimed it, we need to free it. 321fa9e4066Sahrens */ 322fa9e4066Sahrens if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE) { 323fa9e4066Sahrens lr_write_t *lr = (lr_write_t *)lrc; 324fa9e4066Sahrens blkptr_t *bp = &lr->lr_blkptr; 325fa9e4066Sahrens if (bp->blk_birth >= claim_txg && 326fa9e4066Sahrens !zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp))) { 327fa9e4066Sahrens (void) arc_free(NULL, zilog->zl_spa, 328fa9e4066Sahrens dmu_tx_get_txg(tx), bp, NULL, NULL, ARC_WAIT); 329fa9e4066Sahrens } 330fa9e4066Sahrens } 331fa9e4066Sahrens } 332fa9e4066Sahrens 333fa9e4066Sahrens /* 334fa9e4066Sahrens * Create an on-disk intent log. 335fa9e4066Sahrens */ 336fa9e4066Sahrens static void 337fa9e4066Sahrens zil_create(zilog_t *zilog) 338fa9e4066Sahrens { 339d80c45e0Sbonwick const zil_header_t *zh = zilog->zl_header; 340fa9e4066Sahrens lwb_t *lwb; 341d80c45e0Sbonwick uint64_t txg = 0; 342d80c45e0Sbonwick dmu_tx_t *tx = NULL; 343fa9e4066Sahrens blkptr_t blk; 344d80c45e0Sbonwick int error = 0; 345fa9e4066Sahrens 346fa9e4066Sahrens /* 347d80c45e0Sbonwick * Wait for any previous destroy to complete. 348fa9e4066Sahrens */ 349d80c45e0Sbonwick txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 350d80c45e0Sbonwick 351d80c45e0Sbonwick ASSERT(zh->zh_claim_txg == 0); 352d80c45e0Sbonwick ASSERT(zh->zh_replay_seq == 0); 353d80c45e0Sbonwick 354d80c45e0Sbonwick blk = zh->zh_log; 355fa9e4066Sahrens 356fa9e4066Sahrens /* 357899217ddSNeil Perrin * If we don't already have an initial log block or we have one 358899217ddSNeil Perrin * but it's the wrong endianness then allocate one. 359fa9e4066Sahrens */ 360899217ddSNeil Perrin if (BP_IS_HOLE(&blk) || BP_SHOULD_BYTESWAP(&blk)) { 361d80c45e0Sbonwick tx = dmu_tx_create(zilog->zl_os); 362d80c45e0Sbonwick (void) dmu_tx_assign(tx, TXG_WAIT); 363d80c45e0Sbonwick dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 364d80c45e0Sbonwick txg = dmu_tx_get_txg(tx); 365d80c45e0Sbonwick 366899217ddSNeil Perrin if (!BP_IS_HOLE(&blk)) { 367899217ddSNeil Perrin zio_free_blk(zilog->zl_spa, &blk, txg); 368899217ddSNeil Perrin BP_ZERO(&blk); 369899217ddSNeil Perrin } 370899217ddSNeil Perrin 37167bd71c6Sperrin error = zio_alloc_blk(zilog->zl_spa, ZIL_MIN_BLKSZ, &blk, 372d48e086fSNeil Perrin NULL, txg, zilog->zl_logbias == ZFS_LOGBIAS_LATENCY); 373d80c45e0Sbonwick 374d80c45e0Sbonwick if (error == 0) 375d80c45e0Sbonwick zil_init_log_chain(zilog, &blk); 37613f5297eSperrin } 377fa9e4066Sahrens 378d80c45e0Sbonwick /* 379d80c45e0Sbonwick * Allocate a log write buffer (lwb) for the first log block. 380d80c45e0Sbonwick */ 381d80c45e0Sbonwick if (error == 0) { 382fa9e4066Sahrens lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 383fa9e4066Sahrens lwb->lwb_zilog = zilog; 384fa9e4066Sahrens lwb->lwb_blk = blk; 385fa9e4066Sahrens lwb->lwb_nused = 0; 386fa9e4066Sahrens lwb->lwb_sz = BP_GET_LSIZE(&lwb->lwb_blk); 387fa9e4066Sahrens lwb->lwb_buf = zio_buf_alloc(lwb->lwb_sz); 388fa9e4066Sahrens lwb->lwb_max_txg = txg; 389c5c6ffa0Smaybee lwb->lwb_zio = NULL; 390c5c6ffa0Smaybee 391fa9e4066Sahrens mutex_enter(&zilog->zl_lock); 392fa9e4066Sahrens list_insert_tail(&zilog->zl_lwb_list, lwb); 393fa9e4066Sahrens mutex_exit(&zilog->zl_lock); 394fa9e4066Sahrens } 395fa9e4066Sahrens 396d80c45e0Sbonwick /* 397d80c45e0Sbonwick * If we just allocated the first log block, commit our transaction 398d80c45e0Sbonwick * and wait for zil_sync() to stuff the block poiner into zh_log. 399d80c45e0Sbonwick * (zh is part of the MOS, so we cannot modify it in open context.) 400d80c45e0Sbonwick */ 401d80c45e0Sbonwick if (tx != NULL) { 402d80c45e0Sbonwick dmu_tx_commit(tx); 40313f5297eSperrin txg_wait_synced(zilog->zl_dmu_pool, txg); 404d80c45e0Sbonwick } 405d80c45e0Sbonwick 406d80c45e0Sbonwick ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0); 407fa9e4066Sahrens } 408fa9e4066Sahrens 409fa9e4066Sahrens /* 410fa9e4066Sahrens * In one tx, free all log blocks and clear the log header. 411d80c45e0Sbonwick * If keep_first is set, then we're replaying a log with no content. 412d80c45e0Sbonwick * We want to keep the first block, however, so that the first 413d80c45e0Sbonwick * synchronous transaction doesn't require a txg_wait_synced() 414d80c45e0Sbonwick * in zil_create(). We don't need to txg_wait_synced() here either 415d80c45e0Sbonwick * when keep_first is set, because both zil_create() and zil_destroy() 416d80c45e0Sbonwick * will wait for any in-progress destroys to complete. 417fa9e4066Sahrens */ 418fa9e4066Sahrens void 419d80c45e0Sbonwick zil_destroy(zilog_t *zilog, boolean_t keep_first) 420fa9e4066Sahrens { 421d80c45e0Sbonwick const zil_header_t *zh = zilog->zl_header; 422d80c45e0Sbonwick lwb_t *lwb; 423fa9e4066Sahrens dmu_tx_t *tx; 424fa9e4066Sahrens uint64_t txg; 425fa9e4066Sahrens 426d80c45e0Sbonwick /* 427d80c45e0Sbonwick * Wait for any previous destroy to complete. 428d80c45e0Sbonwick */ 429d80c45e0Sbonwick txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 430fa9e4066Sahrens 431d80c45e0Sbonwick if (BP_IS_HOLE(&zh->zh_log)) 432fa9e4066Sahrens return; 433fa9e4066Sahrens 434fa9e4066Sahrens tx = dmu_tx_create(zilog->zl_os); 435fa9e4066Sahrens (void) dmu_tx_assign(tx, TXG_WAIT); 436fa9e4066Sahrens dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 437fa9e4066Sahrens txg = dmu_tx_get_txg(tx); 438fa9e4066Sahrens 439d80c45e0Sbonwick mutex_enter(&zilog->zl_lock); 440d80c45e0Sbonwick 441d80c45e0Sbonwick ASSERT3U(zilog->zl_destroy_txg, <, txg); 442fa9e4066Sahrens zilog->zl_destroy_txg = txg; 443d80c45e0Sbonwick 444d80c45e0Sbonwick if (!list_is_empty(&zilog->zl_lwb_list)) { 445d80c45e0Sbonwick ASSERT(zh->zh_claim_txg == 0); 4468f18d1faSGeorge Wilson zilog->zl_keep_first = B_FALSE; 447d80c45e0Sbonwick while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 448d80c45e0Sbonwick list_remove(&zilog->zl_lwb_list, lwb); 449d80c45e0Sbonwick if (lwb->lwb_buf != NULL) 450d80c45e0Sbonwick zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 451d80c45e0Sbonwick zio_free_blk(zilog->zl_spa, &lwb->lwb_blk, txg); 452d80c45e0Sbonwick kmem_cache_free(zil_lwb_cache, lwb); 453d80c45e0Sbonwick } 454d80c45e0Sbonwick } else { 4558f18d1faSGeorge Wilson zilog->zl_keep_first = keep_first; 4568f18d1faSGeorge Wilson if (zh->zh_flags & ZIL_REPLAY_NEEDED) { 4578f18d1faSGeorge Wilson ASSERT(!keep_first); 458d80c45e0Sbonwick (void) zil_parse(zilog, zil_free_log_block, 459d80c45e0Sbonwick zil_free_log_record, tx, zh->zh_claim_txg); 4608f18d1faSGeorge Wilson } else { 4618f18d1faSGeorge Wilson /* 4628f18d1faSGeorge Wilson * Would like to assert zil_empty() but that 4638f18d1faSGeorge Wilson * would force us to read the log chain which 4648f18d1faSGeorge Wilson * requires us to do I/O to the log. This is 4658f18d1faSGeorge Wilson * overkill since we really just want to destroy 4668f18d1faSGeorge Wilson * the chain anyway. 4678f18d1faSGeorge Wilson */ 4688f18d1faSGeorge Wilson if (!keep_first) { 4698f18d1faSGeorge Wilson blkptr_t bp = zh->zh_log; 4708f18d1faSGeorge Wilson zio_free_blk(zilog->zl_spa, &bp, txg); 4718f18d1faSGeorge Wilson } 472d80c45e0Sbonwick } 473d80c45e0Sbonwick } 474b19a79ecSperrin mutex_exit(&zilog->zl_lock); 475fa9e4066Sahrens 476fa9e4066Sahrens dmu_tx_commit(tx); 477fa9e4066Sahrens } 478fa9e4066Sahrens 4793589c4f0SNeil Perrin /* 4803589c4f0SNeil Perrin * return true if the initial log block is not valid 4813589c4f0SNeil Perrin */ 4823589c4f0SNeil Perrin static boolean_t 4833589c4f0SNeil Perrin zil_empty(zilog_t *zilog) 4843589c4f0SNeil Perrin { 4853589c4f0SNeil Perrin const zil_header_t *zh = zilog->zl_header; 4863589c4f0SNeil Perrin arc_buf_t *abuf = NULL; 4873589c4f0SNeil Perrin 4883589c4f0SNeil Perrin if (BP_IS_HOLE(&zh->zh_log)) 4893589c4f0SNeil Perrin return (B_TRUE); 4903589c4f0SNeil Perrin 4913589c4f0SNeil Perrin if (zil_read_log_block(zilog, &zh->zh_log, &abuf) != 0) 4923589c4f0SNeil Perrin return (B_TRUE); 4933589c4f0SNeil Perrin 4943589c4f0SNeil Perrin VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 4953589c4f0SNeil Perrin return (B_FALSE); 4963589c4f0SNeil Perrin } 4973589c4f0SNeil Perrin 4981d452cf5Sahrens int 499fa9e4066Sahrens zil_claim(char *osname, void *txarg) 500fa9e4066Sahrens { 501fa9e4066Sahrens dmu_tx_t *tx = txarg; 502fa9e4066Sahrens uint64_t first_txg = dmu_tx_get_txg(tx); 503fa9e4066Sahrens zilog_t *zilog; 504fa9e4066Sahrens zil_header_t *zh; 505fa9e4066Sahrens objset_t *os; 506fa9e4066Sahrens int error; 507fa9e4066Sahrens 508503ad85cSMatthew Ahrens error = dmu_objset_hold(osname, FTAG, &os); 509fa9e4066Sahrens if (error) { 510b87f3af3Sperrin cmn_err(CE_WARN, "can't open objset for %s", osname); 5111d452cf5Sahrens return (0); 512fa9e4066Sahrens } 513fa9e4066Sahrens 514fa9e4066Sahrens zilog = dmu_objset_zil(os); 515d80c45e0Sbonwick zh = zil_header_in_syncing_context(zilog); 516fa9e4066Sahrens 517e6ca193dSGeorge Wilson if (zilog->zl_spa->spa_log_state == SPA_LOG_CLEAR) { 518e6ca193dSGeorge Wilson if (!BP_IS_HOLE(&zh->zh_log)) 519e6ca193dSGeorge Wilson zio_free_blk(zilog->zl_spa, &zh->zh_log, first_txg); 520e6ca193dSGeorge Wilson BP_ZERO(&zh->zh_log); 521e6ca193dSGeorge Wilson dsl_dataset_dirty(dmu_objset_ds(os), tx); 522*468c413aSTim Haley dmu_objset_rele(os, FTAG); 523*468c413aSTim Haley return (0); 524e6ca193dSGeorge Wilson } 525e6ca193dSGeorge Wilson 5263589c4f0SNeil Perrin /* 5273589c4f0SNeil Perrin * Record here whether the zil has any records to replay. 5283589c4f0SNeil Perrin * If the header block pointer is null or the block points 5293589c4f0SNeil Perrin * to the stubby then we know there are no valid log records. 5303589c4f0SNeil Perrin * We use the header to store this state as the the zilog gets 5313589c4f0SNeil Perrin * freed later in dmu_objset_close(). 5323589c4f0SNeil Perrin * The flags (and the rest of the header fields) are cleared in 5333589c4f0SNeil Perrin * zil_sync() as a result of a zil_destroy(), after replaying the log. 5343589c4f0SNeil Perrin * 5353589c4f0SNeil Perrin * Note, the intent log can be empty but still need the 5363589c4f0SNeil Perrin * stubby to be claimed. 5373589c4f0SNeil Perrin */ 538e6ca193dSGeorge Wilson if (!zil_empty(zilog)) { 5393589c4f0SNeil Perrin zh->zh_flags |= ZIL_REPLAY_NEEDED; 540e6ca193dSGeorge Wilson dsl_dataset_dirty(dmu_objset_ds(os), tx); 541e6ca193dSGeorge Wilson } 5423589c4f0SNeil Perrin 543fa9e4066Sahrens /* 544d80c45e0Sbonwick * Claim all log blocks if we haven't already done so, and remember 545d80c45e0Sbonwick * the highest claimed sequence number. This ensures that if we can 546d80c45e0Sbonwick * read only part of the log now (e.g. due to a missing device), 547d80c45e0Sbonwick * but we can read the entire log later, we will not try to replay 548d80c45e0Sbonwick * or destroy beyond the last block we successfully claimed. 549fa9e4066Sahrens */ 550fa9e4066Sahrens ASSERT3U(zh->zh_claim_txg, <=, first_txg); 551fa9e4066Sahrens if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) { 552fa9e4066Sahrens zh->zh_claim_txg = first_txg; 553d80c45e0Sbonwick zh->zh_claim_seq = zil_parse(zilog, zil_claim_log_block, 554d80c45e0Sbonwick zil_claim_log_record, tx, first_txg); 555fa9e4066Sahrens dsl_dataset_dirty(dmu_objset_ds(os), tx); 556fa9e4066Sahrens } 557d80c45e0Sbonwick 558fa9e4066Sahrens ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1)); 559503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 5601d452cf5Sahrens return (0); 561b87f3af3Sperrin } 562b87f3af3Sperrin 563b87f3af3Sperrin /* 564b87f3af3Sperrin * Check the log by walking the log chain. 565b87f3af3Sperrin * Checksum errors are ok as they indicate the end of the chain. 566b87f3af3Sperrin * Any other error (no device or read failure) returns an error. 567b87f3af3Sperrin */ 568b87f3af3Sperrin /* ARGSUSED */ 569b87f3af3Sperrin int 570b87f3af3Sperrin zil_check_log_chain(char *osname, void *txarg) 571b87f3af3Sperrin { 572b87f3af3Sperrin zilog_t *zilog; 573b87f3af3Sperrin zil_header_t *zh; 574b87f3af3Sperrin blkptr_t blk; 575b87f3af3Sperrin arc_buf_t *abuf; 576b87f3af3Sperrin objset_t *os; 577b87f3af3Sperrin char *lrbuf; 578b87f3af3Sperrin zil_trailer_t *ztp; 579b87f3af3Sperrin int error; 580b87f3af3Sperrin 581503ad85cSMatthew Ahrens error = dmu_objset_hold(osname, FTAG, &os); 582b87f3af3Sperrin if (error) { 583b87f3af3Sperrin cmn_err(CE_WARN, "can't open objset for %s", osname); 584b87f3af3Sperrin return (0); 585b87f3af3Sperrin } 586b87f3af3Sperrin 587b87f3af3Sperrin zilog = dmu_objset_zil(os); 588b87f3af3Sperrin zh = zil_header_in_syncing_context(zilog); 589b87f3af3Sperrin blk = zh->zh_log; 590b87f3af3Sperrin if (BP_IS_HOLE(&blk)) { 591503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 592b87f3af3Sperrin return (0); /* no chain */ 593b87f3af3Sperrin } 594b87f3af3Sperrin 595b87f3af3Sperrin for (;;) { 596b87f3af3Sperrin error = zil_read_log_block(zilog, &blk, &abuf); 597b87f3af3Sperrin if (error) 598b87f3af3Sperrin break; 599b87f3af3Sperrin lrbuf = abuf->b_data; 600b87f3af3Sperrin ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1; 601b87f3af3Sperrin blk = ztp->zit_next_blk; 602b87f3af3Sperrin VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 603b87f3af3Sperrin } 604503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 605b87f3af3Sperrin if (error == ECKSUM) 606b87f3af3Sperrin return (0); /* normal end of chain */ 607b87f3af3Sperrin return (error); 608b87f3af3Sperrin } 609b87f3af3Sperrin 61017f17c2dSbonwick static int 61117f17c2dSbonwick zil_vdev_compare(const void *x1, const void *x2) 61217f17c2dSbonwick { 613c0d19b33Sperrin uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev; 614c0d19b33Sperrin uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev; 61517f17c2dSbonwick 61617f17c2dSbonwick if (v1 < v2) 61717f17c2dSbonwick return (-1); 61817f17c2dSbonwick if (v1 > v2) 61917f17c2dSbonwick return (1); 62017f17c2dSbonwick 62117f17c2dSbonwick return (0); 62217f17c2dSbonwick } 62317f17c2dSbonwick 624fa9e4066Sahrens void 62517f17c2dSbonwick zil_add_block(zilog_t *zilog, blkptr_t *bp) 626fa9e4066Sahrens { 62717f17c2dSbonwick avl_tree_t *t = &zilog->zl_vdev_tree; 62817f17c2dSbonwick avl_index_t where; 62917f17c2dSbonwick zil_vdev_node_t *zv, zvsearch; 63017f17c2dSbonwick int ndvas = BP_GET_NDVAS(bp); 63117f17c2dSbonwick int i; 632fa9e4066Sahrens 633416e0cd8Sek if (zfs_nocacheflush) 634fa9e4066Sahrens return; 635fa9e4066Sahrens 63617f17c2dSbonwick ASSERT(zilog->zl_writer); 63717f17c2dSbonwick 63817f17c2dSbonwick /* 63917f17c2dSbonwick * Even though we're zl_writer, we still need a lock because the 64017f17c2dSbonwick * zl_get_data() callbacks may have dmu_sync() done callbacks 64117f17c2dSbonwick * that will run concurrently. 64217f17c2dSbonwick */ 64317f17c2dSbonwick mutex_enter(&zilog->zl_vdev_lock); 64417f17c2dSbonwick for (i = 0; i < ndvas; i++) { 64517f17c2dSbonwick zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]); 64617f17c2dSbonwick if (avl_find(t, &zvsearch, &where) == NULL) { 64717f17c2dSbonwick zv = kmem_alloc(sizeof (*zv), KM_SLEEP); 64817f17c2dSbonwick zv->zv_vdev = zvsearch.zv_vdev; 64917f17c2dSbonwick avl_insert(t, zv, where); 65067bd71c6Sperrin } 65167bd71c6Sperrin } 65217f17c2dSbonwick mutex_exit(&zilog->zl_vdev_lock); 653fa9e4066Sahrens } 654fa9e4066Sahrens 65567bd71c6Sperrin void 65667bd71c6Sperrin zil_flush_vdevs(zilog_t *zilog) 65767bd71c6Sperrin { 65867bd71c6Sperrin spa_t *spa = zilog->zl_spa; 65917f17c2dSbonwick avl_tree_t *t = &zilog->zl_vdev_tree; 66017f17c2dSbonwick void *cookie = NULL; 66117f17c2dSbonwick zil_vdev_node_t *zv; 66217f17c2dSbonwick zio_t *zio; 663fa9e4066Sahrens 66467bd71c6Sperrin ASSERT(zilog->zl_writer); 66567bd71c6Sperrin 66617f17c2dSbonwick /* 66717f17c2dSbonwick * We don't need zl_vdev_lock here because we're the zl_writer, 66817f17c2dSbonwick * and all zl_get_data() callbacks are done. 66917f17c2dSbonwick */ 67017f17c2dSbonwick if (avl_numnodes(t) == 0) 67117f17c2dSbonwick return; 67217f17c2dSbonwick 673e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 67417f17c2dSbonwick 675e14bb325SJeff Bonwick zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); 676fa9e4066Sahrens 67717f17c2dSbonwick while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) { 67817f17c2dSbonwick vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev); 67917f17c2dSbonwick if (vd != NULL) 68017f17c2dSbonwick zio_flush(zio, vd); 68117f17c2dSbonwick kmem_free(zv, sizeof (*zv)); 68267bd71c6Sperrin } 68317f17c2dSbonwick 684fa9e4066Sahrens /* 685fa9e4066Sahrens * Wait for all the flushes to complete. Not all devices actually 686fa9e4066Sahrens * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails. 687fa9e4066Sahrens */ 68817f17c2dSbonwick (void) zio_wait(zio); 68917f17c2dSbonwick 690e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG); 691fa9e4066Sahrens } 692fa9e4066Sahrens 693fa9e4066Sahrens /* 694fa9e4066Sahrens * Function called when a log block write completes 695fa9e4066Sahrens */ 696fa9e4066Sahrens static void 697fa9e4066Sahrens zil_lwb_write_done(zio_t *zio) 698fa9e4066Sahrens { 699fa9e4066Sahrens lwb_t *lwb = zio->io_private; 700fa9e4066Sahrens zilog_t *zilog = lwb->lwb_zilog; 701fa9e4066Sahrens 702e14bb325SJeff Bonwick ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF); 703e14bb325SJeff Bonwick ASSERT(BP_GET_CHECKSUM(zio->io_bp) == ZIO_CHECKSUM_ZILOG); 704e14bb325SJeff Bonwick ASSERT(BP_GET_TYPE(zio->io_bp) == DMU_OT_INTENT_LOG); 705e14bb325SJeff Bonwick ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 706e14bb325SJeff Bonwick ASSERT(BP_GET_BYTEORDER(zio->io_bp) == ZFS_HOST_BYTEORDER); 707e14bb325SJeff Bonwick ASSERT(!BP_IS_GANG(zio->io_bp)); 708e14bb325SJeff Bonwick ASSERT(!BP_IS_HOLE(zio->io_bp)); 709e14bb325SJeff Bonwick ASSERT(zio->io_bp->blk_fill == 0); 710e14bb325SJeff Bonwick 711fa9e4066Sahrens /* 712ef0d8e11SNeil Perrin * Ensure the lwb buffer pointer is cleared before releasing 713ef0d8e11SNeil Perrin * the txg. If we have had an allocation failure and 714ef0d8e11SNeil Perrin * the txg is waiting to sync then we want want zil_sync() 715ef0d8e11SNeil Perrin * to remove the lwb so that it's not picked up as the next new 716ef0d8e11SNeil Perrin * one in zil_commit_writer(). zil_sync() will only remove 717ef0d8e11SNeil Perrin * the lwb if lwb_buf is null. 718fa9e4066Sahrens */ 719fa9e4066Sahrens zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 720fa9e4066Sahrens mutex_enter(&zilog->zl_lock); 721fa9e4066Sahrens lwb->lwb_buf = NULL; 7228654d025Sperrin if (zio->io_error) 723fa9e4066Sahrens zilog->zl_log_error = B_TRUE; 724ef0d8e11SNeil Perrin 725ef0d8e11SNeil Perrin /* 726ef0d8e11SNeil Perrin * Now that we've written this log block, we have a stable pointer 727ef0d8e11SNeil Perrin * to the next block in the chain, so it's OK to let the txg in 7282672fd86SNeil Perrin * which we allocated the next block sync. We still have the 7292672fd86SNeil Perrin * zl_lock to ensure zil_sync doesn't kmem free the lwb. 730ef0d8e11SNeil Perrin */ 731ef0d8e11SNeil Perrin txg_rele_to_sync(&lwb->lwb_txgh); 7322672fd86SNeil Perrin mutex_exit(&zilog->zl_lock); 733fa9e4066Sahrens } 734fa9e4066Sahrens 735c5c6ffa0Smaybee /* 736c5c6ffa0Smaybee * Initialize the io for a log block. 737c5c6ffa0Smaybee */ 738c5c6ffa0Smaybee static void 739c5c6ffa0Smaybee zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb) 740c5c6ffa0Smaybee { 741c5c6ffa0Smaybee zbookmark_t zb; 742c5c6ffa0Smaybee 743c5c6ffa0Smaybee zb.zb_objset = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET]; 744c5c6ffa0Smaybee zb.zb_object = 0; 745c5c6ffa0Smaybee zb.zb_level = -1; 746c5c6ffa0Smaybee zb.zb_blkid = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 747c5c6ffa0Smaybee 748b19a79ecSperrin if (zilog->zl_root_zio == NULL) { 749b19a79ecSperrin zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL, 750b19a79ecSperrin ZIO_FLAG_CANFAIL); 751b19a79ecSperrin } 75267bd71c6Sperrin if (lwb->lwb_zio == NULL) { 75367bd71c6Sperrin lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa, 754e6ca193dSGeorge Wilson 0, &lwb->lwb_blk, lwb->lwb_buf, lwb->lwb_sz, 755e6ca193dSGeorge Wilson zil_lwb_write_done, lwb, ZIO_PRIORITY_LOG_WRITE, 7568f18d1faSGeorge Wilson ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE, &zb); 75767bd71c6Sperrin } 758c5c6ffa0Smaybee } 759c5c6ffa0Smaybee 760d48e086fSNeil Perrin /* 761d48e086fSNeil Perrin * Use the slog as long as the logbias is 'latency' and the current commit size 762d48e086fSNeil Perrin * is less than the limit or the total list size is less than 2X the limit. 763d48e086fSNeil Perrin * Limit checking is disabled by setting zil_slog_limit to UINT64_MAX. 764d48e086fSNeil Perrin */ 765d48e086fSNeil Perrin uint64_t zil_slog_limit = 1024 * 1024; 766d48e086fSNeil Perrin #define USE_SLOG(zilog) (((zilog)->zl_logbias == ZFS_LOGBIAS_LATENCY) && \ 767d48e086fSNeil Perrin (((zilog)->zl_cur_used < zil_slog_limit) || \ 768d48e086fSNeil Perrin ((zilog)->zl_itx_list_sz < (zil_slog_limit << 1)))) 769d48e086fSNeil Perrin 770fa9e4066Sahrens /* 771fa9e4066Sahrens * Start a log block write and advance to the next log block. 772fa9e4066Sahrens * Calls are serialized. 773fa9e4066Sahrens */ 774fa9e4066Sahrens static lwb_t * 775fa9e4066Sahrens zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb) 776fa9e4066Sahrens { 777fa9e4066Sahrens lwb_t *nlwb; 778fa9e4066Sahrens zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1; 779d80c45e0Sbonwick spa_t *spa = zilog->zl_spa; 780d80c45e0Sbonwick blkptr_t *bp = &ztp->zit_next_blk; 781fa9e4066Sahrens uint64_t txg; 782fa9e4066Sahrens uint64_t zil_blksz; 783fa9e4066Sahrens int error; 784fa9e4066Sahrens 785fa9e4066Sahrens ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb)); 786fa9e4066Sahrens 787fa9e4066Sahrens /* 788fa9e4066Sahrens * Allocate the next block and save its address in this block 789fa9e4066Sahrens * before writing it in order to establish the log chain. 790fa9e4066Sahrens * Note that if the allocation of nlwb synced before we wrote 791fa9e4066Sahrens * the block that points at it (lwb), we'd leak it if we crashed. 792fa9e4066Sahrens * Therefore, we don't do txg_rele_to_sync() until zil_lwb_write_done(). 793fa9e4066Sahrens */ 794fa9e4066Sahrens txg = txg_hold_open(zilog->zl_dmu_pool, &lwb->lwb_txgh); 795fa9e4066Sahrens txg_rele_to_quiesce(&lwb->lwb_txgh); 796fa9e4066Sahrens 797fa9e4066Sahrens /* 79822ac5be4Sperrin * Pick a ZIL blocksize. We request a size that is the 79922ac5be4Sperrin * maximum of the previous used size, the current used size and 80022ac5be4Sperrin * the amount waiting in the queue. 801fa9e4066Sahrens */ 802c5c6ffa0Smaybee zil_blksz = MAX(zilog->zl_prev_used, 803c5c6ffa0Smaybee zilog->zl_cur_used + sizeof (*ztp)); 80422ac5be4Sperrin zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp)); 805b4d654b0Sperrin zil_blksz = P2ROUNDUP_TYPED(zil_blksz, ZIL_MIN_BLKSZ, uint64_t); 80622ac5be4Sperrin if (zil_blksz > ZIL_MAX_BLKSZ) 80722ac5be4Sperrin zil_blksz = ZIL_MAX_BLKSZ; 808fa9e4066Sahrens 80967bd71c6Sperrin BP_ZERO(bp); 81067bd71c6Sperrin /* pass the old blkptr in order to spread log blocks across devs */ 811e09fa4daSNeil Perrin error = zio_alloc_blk(spa, zil_blksz, bp, &lwb->lwb_blk, txg, 812d48e086fSNeil Perrin USE_SLOG(zilog)); 813fa9e4066Sahrens if (error) { 814d63d470bSgw dmu_tx_t *tx = dmu_tx_create_assigned(zilog->zl_dmu_pool, txg); 815d63d470bSgw 816d63d470bSgw /* 817d63d470bSgw * We dirty the dataset to ensure that zil_sync() will 818d63d470bSgw * be called to remove this lwb from our zl_lwb_list. 819d63d470bSgw * Failing to do so, may leave an lwb with a NULL lwb_buf 820d63d470bSgw * hanging around on the zl_lwb_list. 821d63d470bSgw */ 822d63d470bSgw dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 823aeb1c1b6Sgw dmu_tx_commit(tx); 824d63d470bSgw 825d63d470bSgw /* 826d63d470bSgw * Since we've just experienced an allocation failure so we 827d63d470bSgw * terminate the current lwb and send it on its way. 828d63d470bSgw */ 829d63d470bSgw ztp->zit_pad = 0; 830d63d470bSgw ztp->zit_nused = lwb->lwb_nused; 831d63d470bSgw ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum; 832d63d470bSgw zio_nowait(lwb->lwb_zio); 833d63d470bSgw 834ea8dc4b6Seschrock /* 835ea8dc4b6Seschrock * By returning NULL the caller will call tx_wait_synced() 836ea8dc4b6Seschrock */ 837fa9e4066Sahrens return (NULL); 838fa9e4066Sahrens } 839fa9e4066Sahrens 840d80c45e0Sbonwick ASSERT3U(bp->blk_birth, ==, txg); 841ea8dc4b6Seschrock ztp->zit_pad = 0; 842fa9e4066Sahrens ztp->zit_nused = lwb->lwb_nused; 843fa9e4066Sahrens ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum; 844d80c45e0Sbonwick bp->blk_cksum = lwb->lwb_blk.blk_cksum; 845d80c45e0Sbonwick bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++; 846fa9e4066Sahrens 847fa9e4066Sahrens /* 848fa9e4066Sahrens * Allocate a new log write buffer (lwb). 849fa9e4066Sahrens */ 850fa9e4066Sahrens nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 851fa9e4066Sahrens 852fa9e4066Sahrens nlwb->lwb_zilog = zilog; 853d80c45e0Sbonwick nlwb->lwb_blk = *bp; 854fa9e4066Sahrens nlwb->lwb_nused = 0; 855fa9e4066Sahrens nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk); 856fa9e4066Sahrens nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz); 857fa9e4066Sahrens nlwb->lwb_max_txg = txg; 858c5c6ffa0Smaybee nlwb->lwb_zio = NULL; 859fa9e4066Sahrens 860fa9e4066Sahrens /* 86167bd71c6Sperrin * Put new lwb at the end of the log chain 862fa9e4066Sahrens */ 863fa9e4066Sahrens mutex_enter(&zilog->zl_lock); 864fa9e4066Sahrens list_insert_tail(&zilog->zl_lwb_list, nlwb); 865fa9e4066Sahrens mutex_exit(&zilog->zl_lock); 866fa9e4066Sahrens 86717f17c2dSbonwick /* Record the block for later vdev flushing */ 86817f17c2dSbonwick zil_add_block(zilog, &lwb->lwb_blk); 86967bd71c6Sperrin 870fa9e4066Sahrens /* 871c5c6ffa0Smaybee * kick off the write for the old log block 872fa9e4066Sahrens */ 873c5c6ffa0Smaybee dprintf_bp(&lwb->lwb_blk, "lwb %p txg %llu: ", lwb, txg); 87467bd71c6Sperrin ASSERT(lwb->lwb_zio); 875c5c6ffa0Smaybee zio_nowait(lwb->lwb_zio); 876fa9e4066Sahrens 877fa9e4066Sahrens return (nlwb); 878fa9e4066Sahrens } 879fa9e4066Sahrens 880fa9e4066Sahrens static lwb_t * 881fa9e4066Sahrens zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb) 882fa9e4066Sahrens { 883fa9e4066Sahrens lr_t *lrc = &itx->itx_lr; /* common log record */ 884c5c6ffa0Smaybee lr_write_t *lr = (lr_write_t *)lrc; 885fa9e4066Sahrens uint64_t txg = lrc->lrc_txg; 886fa9e4066Sahrens uint64_t reclen = lrc->lrc_reclen; 887c5c6ffa0Smaybee uint64_t dlen; 888fa9e4066Sahrens 889fa9e4066Sahrens if (lwb == NULL) 890fa9e4066Sahrens return (NULL); 891fa9e4066Sahrens ASSERT(lwb->lwb_buf != NULL); 892fa9e4066Sahrens 893c5c6ffa0Smaybee if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY) 894c5c6ffa0Smaybee dlen = P2ROUNDUP_TYPED( 895c5c6ffa0Smaybee lr->lr_length, sizeof (uint64_t), uint64_t); 896c5c6ffa0Smaybee else 897c5c6ffa0Smaybee dlen = 0; 898fa9e4066Sahrens 899104e2ed7Sperrin zilog->zl_cur_used += (reclen + dlen); 90022ac5be4Sperrin 90167bd71c6Sperrin zil_lwb_write_init(zilog, lwb); 90267bd71c6Sperrin 903fa9e4066Sahrens /* 904fa9e4066Sahrens * If this record won't fit in the current log block, start a new one. 905fa9e4066Sahrens */ 906104e2ed7Sperrin if (lwb->lwb_nused + reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) { 907fa9e4066Sahrens lwb = zil_lwb_write_start(zilog, lwb); 908c5c6ffa0Smaybee if (lwb == NULL) 909fa9e4066Sahrens return (NULL); 91067bd71c6Sperrin zil_lwb_write_init(zilog, lwb); 911ea8dc4b6Seschrock ASSERT(lwb->lwb_nused == 0); 912104e2ed7Sperrin if (reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) { 913fa9e4066Sahrens txg_wait_synced(zilog->zl_dmu_pool, txg); 914fa9e4066Sahrens return (lwb); 915fa9e4066Sahrens } 916fa9e4066Sahrens } 917fa9e4066Sahrens 918b19a79ecSperrin /* 919b19a79ecSperrin * Update the lrc_seq, to be log record sequence number. See zil.h 920b19a79ecSperrin * Then copy the record to the log buffer. 921b19a79ecSperrin */ 922b19a79ecSperrin lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */ 923fa9e4066Sahrens bcopy(lrc, lwb->lwb_buf + lwb->lwb_nused, reclen); 924c5c6ffa0Smaybee 925c5c6ffa0Smaybee /* 926c5c6ffa0Smaybee * If it's a write, fetch the data or get its blkptr as appropriate. 927c5c6ffa0Smaybee */ 928c5c6ffa0Smaybee if (lrc->lrc_txtype == TX_WRITE) { 929c5c6ffa0Smaybee if (txg > spa_freeze_txg(zilog->zl_spa)) 930c5c6ffa0Smaybee txg_wait_synced(zilog->zl_dmu_pool, txg); 931c5c6ffa0Smaybee if (itx->itx_wr_state != WR_COPIED) { 932c5c6ffa0Smaybee char *dbuf; 933c5c6ffa0Smaybee int error; 934c5c6ffa0Smaybee 935c5c6ffa0Smaybee /* alignment is guaranteed */ 936c5c6ffa0Smaybee lr = (lr_write_t *)(lwb->lwb_buf + lwb->lwb_nused); 937c5c6ffa0Smaybee if (dlen) { 938c5c6ffa0Smaybee ASSERT(itx->itx_wr_state == WR_NEED_COPY); 939c5c6ffa0Smaybee dbuf = lwb->lwb_buf + lwb->lwb_nused + reclen; 940c5c6ffa0Smaybee lr->lr_common.lrc_reclen += dlen; 941c5c6ffa0Smaybee } else { 942c5c6ffa0Smaybee ASSERT(itx->itx_wr_state == WR_INDIRECT); 943c5c6ffa0Smaybee dbuf = NULL; 944c5c6ffa0Smaybee } 945c5c6ffa0Smaybee error = zilog->zl_get_data( 946c5c6ffa0Smaybee itx->itx_private, lr, dbuf, lwb->lwb_zio); 947c87b8fc5SMark J Musante if (error == EIO) { 948c87b8fc5SMark J Musante txg_wait_synced(zilog->zl_dmu_pool, txg); 949c87b8fc5SMark J Musante return (lwb); 950c87b8fc5SMark J Musante } 951c5c6ffa0Smaybee if (error) { 952c5c6ffa0Smaybee ASSERT(error == ENOENT || error == EEXIST || 953c5c6ffa0Smaybee error == EALREADY); 954c5c6ffa0Smaybee return (lwb); 955c5c6ffa0Smaybee } 956c5c6ffa0Smaybee } 957104e2ed7Sperrin } 958c5c6ffa0Smaybee 959c5c6ffa0Smaybee lwb->lwb_nused += reclen + dlen; 960fa9e4066Sahrens lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg); 961fa9e4066Sahrens ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb)); 962fa9e4066Sahrens ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0); 963fa9e4066Sahrens 964fa9e4066Sahrens return (lwb); 965fa9e4066Sahrens } 966fa9e4066Sahrens 967fa9e4066Sahrens itx_t * 968da6c28aaSamw zil_itx_create(uint64_t txtype, size_t lrsize) 969fa9e4066Sahrens { 970fa9e4066Sahrens itx_t *itx; 971fa9e4066Sahrens 972b4d654b0Sperrin lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t); 973fa9e4066Sahrens 974fa9e4066Sahrens itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP); 975fa9e4066Sahrens itx->itx_lr.lrc_txtype = txtype; 976fa9e4066Sahrens itx->itx_lr.lrc_reclen = lrsize; 977abf76b6eSperrin itx->itx_sod = lrsize; /* if write & WR_NEED_COPY will be increased */ 978fa9e4066Sahrens itx->itx_lr.lrc_seq = 0; /* defensive */ 979fa9e4066Sahrens 980fa9e4066Sahrens return (itx); 981fa9e4066Sahrens } 982fa9e4066Sahrens 983fa9e4066Sahrens uint64_t 984fa9e4066Sahrens zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx) 985fa9e4066Sahrens { 986fa9e4066Sahrens uint64_t seq; 987fa9e4066Sahrens 988fa9e4066Sahrens ASSERT(itx->itx_lr.lrc_seq == 0); 989fa9e4066Sahrens 990fa9e4066Sahrens mutex_enter(&zilog->zl_lock); 991fa9e4066Sahrens list_insert_tail(&zilog->zl_itx_list, itx); 992abf76b6eSperrin zilog->zl_itx_list_sz += itx->itx_sod; 993fa9e4066Sahrens itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx); 994fa9e4066Sahrens itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq; 995fa9e4066Sahrens mutex_exit(&zilog->zl_lock); 996fa9e4066Sahrens 997fa9e4066Sahrens return (seq); 998fa9e4066Sahrens } 999fa9e4066Sahrens 1000fa9e4066Sahrens /* 1001fa9e4066Sahrens * Free up all in-memory intent log transactions that have now been synced. 1002fa9e4066Sahrens */ 1003fa9e4066Sahrens static void 1004fa9e4066Sahrens zil_itx_clean(zilog_t *zilog) 1005fa9e4066Sahrens { 1006fa9e4066Sahrens uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa); 1007fa9e4066Sahrens uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa); 1008a584ef65Sjohansen list_t clean_list; 1009fa9e4066Sahrens itx_t *itx; 1010fa9e4066Sahrens 1011a584ef65Sjohansen list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node)); 1012a584ef65Sjohansen 1013fa9e4066Sahrens mutex_enter(&zilog->zl_lock); 1014b19a79ecSperrin /* wait for a log writer to finish walking list */ 1015b19a79ecSperrin while (zilog->zl_writer) { 1016b19a79ecSperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 1017b19a79ecSperrin } 1018a584ef65Sjohansen 1019a584ef65Sjohansen /* 1020a584ef65Sjohansen * Move the sync'd log transactions to a separate list so we can call 1021a584ef65Sjohansen * kmem_free without holding the zl_lock. 1022a584ef65Sjohansen * 1023a584ef65Sjohansen * There is no need to set zl_writer as we don't drop zl_lock here 1024a584ef65Sjohansen */ 1025fa9e4066Sahrens while ((itx = list_head(&zilog->zl_itx_list)) != NULL && 1026fa9e4066Sahrens itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) { 1027fa9e4066Sahrens list_remove(&zilog->zl_itx_list, itx); 1028abf76b6eSperrin zilog->zl_itx_list_sz -= itx->itx_sod; 1029a584ef65Sjohansen list_insert_tail(&clean_list, itx); 1030fa9e4066Sahrens } 103167bd71c6Sperrin cv_broadcast(&zilog->zl_cv_writer); 1032fa9e4066Sahrens mutex_exit(&zilog->zl_lock); 1033a584ef65Sjohansen 1034a584ef65Sjohansen /* destroy sync'd log transactions */ 1035a584ef65Sjohansen while ((itx = list_head(&clean_list)) != NULL) { 1036a584ef65Sjohansen list_remove(&clean_list, itx); 1037a584ef65Sjohansen kmem_free(itx, offsetof(itx_t, itx_lr) 1038a584ef65Sjohansen + itx->itx_lr.lrc_reclen); 1039a584ef65Sjohansen } 1040a584ef65Sjohansen list_destroy(&clean_list); 1041fa9e4066Sahrens } 1042fa9e4066Sahrens 1043b19a79ecSperrin /* 104467bd71c6Sperrin * If there are any in-memory intent log transactions which have now been 104567bd71c6Sperrin * synced then start up a taskq to free them. 1046b19a79ecSperrin */ 1047fa9e4066Sahrens void 1048fa9e4066Sahrens zil_clean(zilog_t *zilog) 1049fa9e4066Sahrens { 105067bd71c6Sperrin itx_t *itx; 105167bd71c6Sperrin 1052fa9e4066Sahrens mutex_enter(&zilog->zl_lock); 105367bd71c6Sperrin itx = list_head(&zilog->zl_itx_list); 105467bd71c6Sperrin if ((itx != NULL) && 105567bd71c6Sperrin (itx->itx_lr.lrc_txg <= spa_last_synced_txg(zilog->zl_spa))) { 1056fa9e4066Sahrens (void) taskq_dispatch(zilog->zl_clean_taskq, 1057d48e086fSNeil Perrin (task_func_t *)zil_itx_clean, zilog, TQ_NOSLEEP); 105867bd71c6Sperrin } 1059fa9e4066Sahrens mutex_exit(&zilog->zl_lock); 1060fa9e4066Sahrens } 1061fa9e4066Sahrens 1062e14bb325SJeff Bonwick static void 1063b19a79ecSperrin zil_commit_writer(zilog_t *zilog, uint64_t seq, uint64_t foid) 1064fa9e4066Sahrens { 1065fa9e4066Sahrens uint64_t txg; 106667bd71c6Sperrin uint64_t commit_seq = 0; 1067b19a79ecSperrin itx_t *itx, *itx_next = (itx_t *)-1; 1068fa9e4066Sahrens lwb_t *lwb; 1069fa9e4066Sahrens spa_t *spa; 1070fa9e4066Sahrens 1071fa9e4066Sahrens zilog->zl_writer = B_TRUE; 1072e14bb325SJeff Bonwick ASSERT(zilog->zl_root_zio == NULL); 1073b19a79ecSperrin spa = zilog->zl_spa; 1074fa9e4066Sahrens 1075fa9e4066Sahrens if (zilog->zl_suspend) { 1076fa9e4066Sahrens lwb = NULL; 1077fa9e4066Sahrens } else { 1078fa9e4066Sahrens lwb = list_tail(&zilog->zl_lwb_list); 1079fa9e4066Sahrens if (lwb == NULL) { 1080b19a79ecSperrin /* 1081b19a79ecSperrin * Return if there's nothing to flush before we 1082b19a79ecSperrin * dirty the fs by calling zil_create() 1083b19a79ecSperrin */ 1084b19a79ecSperrin if (list_is_empty(&zilog->zl_itx_list)) { 1085b19a79ecSperrin zilog->zl_writer = B_FALSE; 1086b19a79ecSperrin return; 1087b19a79ecSperrin } 1088fa9e4066Sahrens mutex_exit(&zilog->zl_lock); 1089fa9e4066Sahrens zil_create(zilog); 1090fa9e4066Sahrens mutex_enter(&zilog->zl_lock); 1091fa9e4066Sahrens lwb = list_tail(&zilog->zl_lwb_list); 1092fa9e4066Sahrens } 1093fa9e4066Sahrens } 1094fa9e4066Sahrens 109567bd71c6Sperrin /* Loop through in-memory log transactions filling log blocks. */ 1096b19a79ecSperrin DTRACE_PROBE1(zil__cw1, zilog_t *, zilog); 1097fa9e4066Sahrens for (;;) { 1098b19a79ecSperrin /* 1099b19a79ecSperrin * Find the next itx to push: 1100b19a79ecSperrin * Push all transactions related to specified foid and all 1101b19a79ecSperrin * other transactions except TX_WRITE, TX_TRUNCATE, 1102b19a79ecSperrin * TX_SETATTR and TX_ACL for all other files. 1103b19a79ecSperrin */ 1104b19a79ecSperrin if (itx_next != (itx_t *)-1) 1105b19a79ecSperrin itx = itx_next; 1106b19a79ecSperrin else 1107b19a79ecSperrin itx = list_head(&zilog->zl_itx_list); 1108b19a79ecSperrin for (; itx != NULL; itx = list_next(&zilog->zl_itx_list, itx)) { 1109b19a79ecSperrin if (foid == 0) /* push all foids? */ 1110b19a79ecSperrin break; 111167bd71c6Sperrin if (itx->itx_sync) /* push all O_[D]SYNC */ 111267bd71c6Sperrin break; 1113b19a79ecSperrin switch (itx->itx_lr.lrc_txtype) { 1114b19a79ecSperrin case TX_SETATTR: 1115b19a79ecSperrin case TX_WRITE: 1116b19a79ecSperrin case TX_TRUNCATE: 1117b19a79ecSperrin case TX_ACL: 1118b19a79ecSperrin /* lr_foid is same offset for these records */ 1119b19a79ecSperrin if (((lr_write_t *)&itx->itx_lr)->lr_foid 1120b19a79ecSperrin != foid) { 1121b19a79ecSperrin continue; /* skip this record */ 1122b19a79ecSperrin } 1123b19a79ecSperrin } 1124b19a79ecSperrin break; 1125b19a79ecSperrin } 1126fa9e4066Sahrens if (itx == NULL) 1127fa9e4066Sahrens break; 1128fa9e4066Sahrens 1129fa9e4066Sahrens if ((itx->itx_lr.lrc_seq > seq) && 1130b19a79ecSperrin ((lwb == NULL) || (lwb->lwb_nused == 0) || 1131abf76b6eSperrin (lwb->lwb_nused + itx->itx_sod > ZIL_BLK_DATA_SZ(lwb)))) { 1132fa9e4066Sahrens break; 113367bd71c6Sperrin } 1134fa9e4066Sahrens 1135b19a79ecSperrin /* 1136b19a79ecSperrin * Save the next pointer. Even though we soon drop 1137b19a79ecSperrin * zl_lock all threads that may change the list 1138b19a79ecSperrin * (another writer or zil_itx_clean) can't do so until 1139b19a79ecSperrin * they have zl_writer. 1140b19a79ecSperrin */ 1141b19a79ecSperrin itx_next = list_next(&zilog->zl_itx_list, itx); 1142fa9e4066Sahrens list_remove(&zilog->zl_itx_list, itx); 1143abf76b6eSperrin zilog->zl_itx_list_sz -= itx->itx_sod; 114467bd71c6Sperrin mutex_exit(&zilog->zl_lock); 1145fa9e4066Sahrens txg = itx->itx_lr.lrc_txg; 1146fa9e4066Sahrens ASSERT(txg); 1147fa9e4066Sahrens 1148fa9e4066Sahrens if (txg > spa_last_synced_txg(spa) || 1149fa9e4066Sahrens txg > spa_freeze_txg(spa)) 1150fa9e4066Sahrens lwb = zil_lwb_commit(zilog, itx, lwb); 1151fa9e4066Sahrens kmem_free(itx, offsetof(itx_t, itx_lr) 1152fa9e4066Sahrens + itx->itx_lr.lrc_reclen); 1153fa9e4066Sahrens mutex_enter(&zilog->zl_lock); 1154fa9e4066Sahrens } 1155b19a79ecSperrin DTRACE_PROBE1(zil__cw2, zilog_t *, zilog); 115667bd71c6Sperrin /* determine commit sequence number */ 115767bd71c6Sperrin itx = list_head(&zilog->zl_itx_list); 115867bd71c6Sperrin if (itx) 115967bd71c6Sperrin commit_seq = itx->itx_lr.lrc_seq; 116067bd71c6Sperrin else 116167bd71c6Sperrin commit_seq = zilog->zl_itx_seq; 1162fa9e4066Sahrens mutex_exit(&zilog->zl_lock); 1163fa9e4066Sahrens 1164fa9e4066Sahrens /* write the last block out */ 116567bd71c6Sperrin if (lwb != NULL && lwb->lwb_zio != NULL) 1166fa9e4066Sahrens lwb = zil_lwb_write_start(zilog, lwb); 1167fa9e4066Sahrens 116822ac5be4Sperrin zilog->zl_prev_used = zilog->zl_cur_used; 116922ac5be4Sperrin zilog->zl_cur_used = 0; 1170fa9e4066Sahrens 1171fa9e4066Sahrens /* 1172b19a79ecSperrin * Wait if necessary for the log blocks to be on stable storage. 1173fa9e4066Sahrens */ 1174b19a79ecSperrin if (zilog->zl_root_zio) { 1175b19a79ecSperrin DTRACE_PROBE1(zil__cw3, zilog_t *, zilog); 1176b19a79ecSperrin (void) zio_wait(zilog->zl_root_zio); 1177e14bb325SJeff Bonwick zilog->zl_root_zio = NULL; 1178b19a79ecSperrin DTRACE_PROBE1(zil__cw4, zilog_t *, zilog); 117917f17c2dSbonwick zil_flush_vdevs(zilog); 1180fa9e4066Sahrens } 118122ac5be4Sperrin 1182fa9e4066Sahrens if (zilog->zl_log_error || lwb == NULL) { 1183fa9e4066Sahrens zilog->zl_log_error = 0; 1184fa9e4066Sahrens txg_wait_synced(zilog->zl_dmu_pool, 0); 1185fa9e4066Sahrens } 118667bd71c6Sperrin 118767bd71c6Sperrin mutex_enter(&zilog->zl_lock); 118822ac5be4Sperrin zilog->zl_writer = B_FALSE; 118967bd71c6Sperrin 119067bd71c6Sperrin ASSERT3U(commit_seq, >=, zilog->zl_commit_seq); 119167bd71c6Sperrin zilog->zl_commit_seq = commit_seq; 1192b19a79ecSperrin } 1193b19a79ecSperrin 1194b19a79ecSperrin /* 1195b19a79ecSperrin * Push zfs transactions to stable storage up to the supplied sequence number. 1196b19a79ecSperrin * If foid is 0 push out all transactions, otherwise push only those 1197b19a79ecSperrin * for that file or might have been used to create that file. 1198b19a79ecSperrin */ 1199b19a79ecSperrin void 1200b19a79ecSperrin zil_commit(zilog_t *zilog, uint64_t seq, uint64_t foid) 1201b19a79ecSperrin { 1202b19a79ecSperrin if (zilog == NULL || seq == 0) 1203b19a79ecSperrin return; 1204b19a79ecSperrin 1205b19a79ecSperrin mutex_enter(&zilog->zl_lock); 1206b19a79ecSperrin 1207b19a79ecSperrin seq = MIN(seq, zilog->zl_itx_seq); /* cap seq at largest itx seq */ 1208b19a79ecSperrin 120967bd71c6Sperrin while (zilog->zl_writer) { 1210b19a79ecSperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 121167bd71c6Sperrin if (seq < zilog->zl_commit_seq) { 121267bd71c6Sperrin mutex_exit(&zilog->zl_lock); 121367bd71c6Sperrin return; 121467bd71c6Sperrin } 121567bd71c6Sperrin } 1216b19a79ecSperrin zil_commit_writer(zilog, seq, foid); /* drops zl_lock */ 121767bd71c6Sperrin /* wake up others waiting on the commit */ 121867bd71c6Sperrin cv_broadcast(&zilog->zl_cv_writer); 121967bd71c6Sperrin mutex_exit(&zilog->zl_lock); 1220fa9e4066Sahrens } 1221fa9e4066Sahrens 1222fa9e4066Sahrens /* 1223fa9e4066Sahrens * Called in syncing context to free committed log blocks and update log header. 1224fa9e4066Sahrens */ 1225fa9e4066Sahrens void 1226fa9e4066Sahrens zil_sync(zilog_t *zilog, dmu_tx_t *tx) 1227fa9e4066Sahrens { 1228d80c45e0Sbonwick zil_header_t *zh = zil_header_in_syncing_context(zilog); 1229fa9e4066Sahrens uint64_t txg = dmu_tx_get_txg(tx); 1230fa9e4066Sahrens spa_t *spa = zilog->zl_spa; 1231fa9e4066Sahrens lwb_t *lwb; 1232fa9e4066Sahrens 123314843421SMatthew Ahrens /* 123414843421SMatthew Ahrens * We don't zero out zl_destroy_txg, so make sure we don't try 123514843421SMatthew Ahrens * to destroy it twice. 123614843421SMatthew Ahrens */ 123714843421SMatthew Ahrens if (spa_sync_pass(spa) != 1) 123814843421SMatthew Ahrens return; 123914843421SMatthew Ahrens 1240d80c45e0Sbonwick mutex_enter(&zilog->zl_lock); 1241d80c45e0Sbonwick 1242fa9e4066Sahrens ASSERT(zilog->zl_stop_sync == 0); 1243fa9e4066Sahrens 12441209a471SNeil Perrin zh->zh_replay_seq = zilog->zl_replayed_seq[txg & TXG_MASK]; 1245fa9e4066Sahrens 1246fa9e4066Sahrens if (zilog->zl_destroy_txg == txg) { 1247d80c45e0Sbonwick blkptr_t blk = zh->zh_log; 1248d80c45e0Sbonwick 1249d80c45e0Sbonwick ASSERT(list_head(&zilog->zl_lwb_list) == NULL); 1250d80c45e0Sbonwick 1251d80c45e0Sbonwick bzero(zh, sizeof (zil_header_t)); 12521209a471SNeil Perrin bzero(zilog->zl_replayed_seq, sizeof (zilog->zl_replayed_seq)); 1253d80c45e0Sbonwick 1254d80c45e0Sbonwick if (zilog->zl_keep_first) { 1255d80c45e0Sbonwick /* 1256d80c45e0Sbonwick * If this block was part of log chain that couldn't 1257d80c45e0Sbonwick * be claimed because a device was missing during 1258d80c45e0Sbonwick * zil_claim(), but that device later returns, 1259d80c45e0Sbonwick * then this block could erroneously appear valid. 1260d80c45e0Sbonwick * To guard against this, assign a new GUID to the new 1261d80c45e0Sbonwick * log chain so it doesn't matter what blk points to. 1262d80c45e0Sbonwick */ 1263d80c45e0Sbonwick zil_init_log_chain(zilog, &blk); 1264d80c45e0Sbonwick zh->zh_log = blk; 1265d80c45e0Sbonwick } 1266fa9e4066Sahrens } 1267fa9e4066Sahrens 1268e6ca193dSGeorge Wilson while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 1269b19a79ecSperrin zh->zh_log = lwb->lwb_blk; 1270fa9e4066Sahrens if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg) 1271fa9e4066Sahrens break; 1272fa9e4066Sahrens list_remove(&zilog->zl_lwb_list, lwb); 1273fa9e4066Sahrens zio_free_blk(spa, &lwb->lwb_blk, txg); 1274fa9e4066Sahrens kmem_cache_free(zil_lwb_cache, lwb); 1275d63d470bSgw 1276d63d470bSgw /* 1277d63d470bSgw * If we don't have anything left in the lwb list then 1278d63d470bSgw * we've had an allocation failure and we need to zero 1279d63d470bSgw * out the zil_header blkptr so that we don't end 1280d63d470bSgw * up freeing the same block twice. 1281d63d470bSgw */ 1282d63d470bSgw if (list_head(&zilog->zl_lwb_list) == NULL) 1283d63d470bSgw BP_ZERO(&zh->zh_log); 1284fa9e4066Sahrens } 1285fa9e4066Sahrens mutex_exit(&zilog->zl_lock); 1286fa9e4066Sahrens } 1287fa9e4066Sahrens 1288fa9e4066Sahrens void 1289fa9e4066Sahrens zil_init(void) 1290fa9e4066Sahrens { 1291fa9e4066Sahrens zil_lwb_cache = kmem_cache_create("zil_lwb_cache", 12925ad82045Snd sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0); 1293fa9e4066Sahrens } 1294fa9e4066Sahrens 1295fa9e4066Sahrens void 1296fa9e4066Sahrens zil_fini(void) 1297fa9e4066Sahrens { 1298fa9e4066Sahrens kmem_cache_destroy(zil_lwb_cache); 1299fa9e4066Sahrens } 1300fa9e4066Sahrens 1301e09fa4daSNeil Perrin void 1302e09fa4daSNeil Perrin zil_set_logbias(zilog_t *zilog, uint64_t logbias) 1303e09fa4daSNeil Perrin { 1304e09fa4daSNeil Perrin zilog->zl_logbias = logbias; 1305e09fa4daSNeil Perrin } 1306e09fa4daSNeil Perrin 1307fa9e4066Sahrens zilog_t * 1308fa9e4066Sahrens zil_alloc(objset_t *os, zil_header_t *zh_phys) 1309fa9e4066Sahrens { 1310fa9e4066Sahrens zilog_t *zilog; 1311fa9e4066Sahrens 1312fa9e4066Sahrens zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP); 1313fa9e4066Sahrens 1314fa9e4066Sahrens zilog->zl_header = zh_phys; 1315fa9e4066Sahrens zilog->zl_os = os; 1316fa9e4066Sahrens zilog->zl_spa = dmu_objset_spa(os); 1317fa9e4066Sahrens zilog->zl_dmu_pool = dmu_objset_pool(os); 1318d80c45e0Sbonwick zilog->zl_destroy_txg = TXG_INITIAL - 1; 1319e09fa4daSNeil Perrin zilog->zl_logbias = dmu_objset_logbias(os); 1320fa9e4066Sahrens 13215ad82045Snd mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL); 13225ad82045Snd 1323fa9e4066Sahrens list_create(&zilog->zl_itx_list, sizeof (itx_t), 1324fa9e4066Sahrens offsetof(itx_t, itx_node)); 1325fa9e4066Sahrens 1326fa9e4066Sahrens list_create(&zilog->zl_lwb_list, sizeof (lwb_t), 1327fa9e4066Sahrens offsetof(lwb_t, lwb_node)); 1328fa9e4066Sahrens 132917f17c2dSbonwick mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL); 133017f17c2dSbonwick 133117f17c2dSbonwick avl_create(&zilog->zl_vdev_tree, zil_vdev_compare, 133217f17c2dSbonwick sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node)); 1333fa9e4066Sahrens 1334b7b97454Sperrin cv_init(&zilog->zl_cv_writer, NULL, CV_DEFAULT, NULL); 1335b7b97454Sperrin cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL); 1336b7b97454Sperrin 1337fa9e4066Sahrens return (zilog); 1338fa9e4066Sahrens } 1339fa9e4066Sahrens 1340fa9e4066Sahrens void 1341fa9e4066Sahrens zil_free(zilog_t *zilog) 1342fa9e4066Sahrens { 1343fa9e4066Sahrens lwb_t *lwb; 1344fa9e4066Sahrens 1345fa9e4066Sahrens zilog->zl_stop_sync = 1; 1346fa9e4066Sahrens 1347fa9e4066Sahrens while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 1348fa9e4066Sahrens list_remove(&zilog->zl_lwb_list, lwb); 1349fa9e4066Sahrens if (lwb->lwb_buf != NULL) 1350fa9e4066Sahrens zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 1351fa9e4066Sahrens kmem_cache_free(zil_lwb_cache, lwb); 1352fa9e4066Sahrens } 1353fa9e4066Sahrens list_destroy(&zilog->zl_lwb_list); 1354fa9e4066Sahrens 135517f17c2dSbonwick avl_destroy(&zilog->zl_vdev_tree); 135617f17c2dSbonwick mutex_destroy(&zilog->zl_vdev_lock); 1357fa9e4066Sahrens 1358fa9e4066Sahrens ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1359fa9e4066Sahrens list_destroy(&zilog->zl_itx_list); 13605ad82045Snd mutex_destroy(&zilog->zl_lock); 1361fa9e4066Sahrens 1362b7b97454Sperrin cv_destroy(&zilog->zl_cv_writer); 1363b7b97454Sperrin cv_destroy(&zilog->zl_cv_suspend); 1364b7b97454Sperrin 1365fa9e4066Sahrens kmem_free(zilog, sizeof (zilog_t)); 1366fa9e4066Sahrens } 1367fa9e4066Sahrens 1368fa9e4066Sahrens /* 1369fa9e4066Sahrens * Open an intent log. 1370fa9e4066Sahrens */ 1371fa9e4066Sahrens zilog_t * 1372fa9e4066Sahrens zil_open(objset_t *os, zil_get_data_t *get_data) 1373fa9e4066Sahrens { 1374fa9e4066Sahrens zilog_t *zilog = dmu_objset_zil(os); 1375fa9e4066Sahrens 1376fa9e4066Sahrens zilog->zl_get_data = get_data; 1377fa9e4066Sahrens zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri, 1378fa9e4066Sahrens 2, 2, TASKQ_PREPOPULATE); 1379fa9e4066Sahrens 1380fa9e4066Sahrens return (zilog); 1381fa9e4066Sahrens } 1382fa9e4066Sahrens 1383fa9e4066Sahrens /* 1384fa9e4066Sahrens * Close an intent log. 1385fa9e4066Sahrens */ 1386fa9e4066Sahrens void 1387fa9e4066Sahrens zil_close(zilog_t *zilog) 1388fa9e4066Sahrens { 1389d80c45e0Sbonwick /* 1390d80c45e0Sbonwick * If the log isn't already committed, mark the objset dirty 1391d80c45e0Sbonwick * (so zil_sync() will be called) and wait for that txg to sync. 1392d80c45e0Sbonwick */ 1393d80c45e0Sbonwick if (!zil_is_committed(zilog)) { 1394d80c45e0Sbonwick uint64_t txg; 1395d80c45e0Sbonwick dmu_tx_t *tx = dmu_tx_create(zilog->zl_os); 1396d80c45e0Sbonwick (void) dmu_tx_assign(tx, TXG_WAIT); 1397d80c45e0Sbonwick dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 1398d80c45e0Sbonwick txg = dmu_tx_get_txg(tx); 1399d80c45e0Sbonwick dmu_tx_commit(tx); 1400d80c45e0Sbonwick txg_wait_synced(zilog->zl_dmu_pool, txg); 1401d80c45e0Sbonwick } 1402d80c45e0Sbonwick 1403fa9e4066Sahrens taskq_destroy(zilog->zl_clean_taskq); 1404fa9e4066Sahrens zilog->zl_clean_taskq = NULL; 1405fa9e4066Sahrens zilog->zl_get_data = NULL; 1406fa9e4066Sahrens 1407fa9e4066Sahrens zil_itx_clean(zilog); 1408fa9e4066Sahrens ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1409fa9e4066Sahrens } 1410fa9e4066Sahrens 1411fa9e4066Sahrens /* 1412fa9e4066Sahrens * Suspend an intent log. While in suspended mode, we still honor 1413fa9e4066Sahrens * synchronous semantics, but we rely on txg_wait_synced() to do it. 1414fa9e4066Sahrens * We suspend the log briefly when taking a snapshot so that the snapshot 1415fa9e4066Sahrens * contains all the data it's supposed to, and has an empty intent log. 1416fa9e4066Sahrens */ 1417fa9e4066Sahrens int 1418fa9e4066Sahrens zil_suspend(zilog_t *zilog) 1419fa9e4066Sahrens { 1420d80c45e0Sbonwick const zil_header_t *zh = zilog->zl_header; 1421fa9e4066Sahrens 1422fa9e4066Sahrens mutex_enter(&zilog->zl_lock); 14233589c4f0SNeil Perrin if (zh->zh_flags & ZIL_REPLAY_NEEDED) { /* unplayed log */ 1424fa9e4066Sahrens mutex_exit(&zilog->zl_lock); 1425fa9e4066Sahrens return (EBUSY); 1426fa9e4066Sahrens } 1427d80c45e0Sbonwick if (zilog->zl_suspend++ != 0) { 1428d80c45e0Sbonwick /* 1429d80c45e0Sbonwick * Someone else already began a suspend. 1430d80c45e0Sbonwick * Just wait for them to finish. 1431d80c45e0Sbonwick */ 1432d80c45e0Sbonwick while (zilog->zl_suspending) 1433d80c45e0Sbonwick cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock); 1434d80c45e0Sbonwick mutex_exit(&zilog->zl_lock); 1435d80c45e0Sbonwick return (0); 1436d80c45e0Sbonwick } 1437d80c45e0Sbonwick zilog->zl_suspending = B_TRUE; 1438fa9e4066Sahrens mutex_exit(&zilog->zl_lock); 1439fa9e4066Sahrens 1440b19a79ecSperrin zil_commit(zilog, UINT64_MAX, 0); 1441fa9e4066Sahrens 1442b19a79ecSperrin /* 1443b19a79ecSperrin * Wait for any in-flight log writes to complete. 1444b19a79ecSperrin */ 1445fa9e4066Sahrens mutex_enter(&zilog->zl_lock); 1446b19a79ecSperrin while (zilog->zl_writer) 1447b19a79ecSperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 1448fa9e4066Sahrens mutex_exit(&zilog->zl_lock); 1449fa9e4066Sahrens 1450d80c45e0Sbonwick zil_destroy(zilog, B_FALSE); 1451d80c45e0Sbonwick 1452d80c45e0Sbonwick mutex_enter(&zilog->zl_lock); 1453d80c45e0Sbonwick zilog->zl_suspending = B_FALSE; 1454d80c45e0Sbonwick cv_broadcast(&zilog->zl_cv_suspend); 1455d80c45e0Sbonwick mutex_exit(&zilog->zl_lock); 1456fa9e4066Sahrens 1457fa9e4066Sahrens return (0); 1458fa9e4066Sahrens } 1459fa9e4066Sahrens 1460fa9e4066Sahrens void 1461fa9e4066Sahrens zil_resume(zilog_t *zilog) 1462fa9e4066Sahrens { 1463fa9e4066Sahrens mutex_enter(&zilog->zl_lock); 1464fa9e4066Sahrens ASSERT(zilog->zl_suspend != 0); 1465fa9e4066Sahrens zilog->zl_suspend--; 1466fa9e4066Sahrens mutex_exit(&zilog->zl_lock); 1467fa9e4066Sahrens } 1468fa9e4066Sahrens 1469975c32a0SNeil Perrin /* 1470975c32a0SNeil Perrin * Read in the data for the dmu_sync()ed block, and change the log 1471975c32a0SNeil Perrin * record to write this whole block. 1472975c32a0SNeil Perrin */ 1473975c32a0SNeil Perrin void 1474975c32a0SNeil Perrin zil_get_replay_data(zilog_t *zilog, lr_write_t *lr) 1475975c32a0SNeil Perrin { 1476975c32a0SNeil Perrin blkptr_t *wbp = &lr->lr_blkptr; 1477975c32a0SNeil Perrin char *wbuf = (char *)(lr + 1); /* data follows lr_write_t */ 1478975c32a0SNeil Perrin uint64_t blksz; 1479975c32a0SNeil Perrin 1480975c32a0SNeil Perrin if (BP_IS_HOLE(wbp)) { /* compressed to a hole */ 1481975c32a0SNeil Perrin blksz = BP_GET_LSIZE(&lr->lr_blkptr); 1482975c32a0SNeil Perrin /* 1483975c32a0SNeil Perrin * If the blksz is zero then we must be replaying a log 1484975c32a0SNeil Perrin * from an version prior to setting the blksize of null blocks. 1485975c32a0SNeil Perrin * So we just zero the actual write size reqeusted. 1486975c32a0SNeil Perrin */ 1487975c32a0SNeil Perrin if (blksz == 0) { 1488975c32a0SNeil Perrin bzero(wbuf, lr->lr_length); 1489975c32a0SNeil Perrin return; 1490975c32a0SNeil Perrin } 1491975c32a0SNeil Perrin bzero(wbuf, blksz); 1492975c32a0SNeil Perrin } else { 1493975c32a0SNeil Perrin /* 1494975c32a0SNeil Perrin * A subsequent write may have overwritten this block, in which 1495975c32a0SNeil Perrin * case wbp may have been been freed and reallocated, and our 1496975c32a0SNeil Perrin * read of wbp may fail with a checksum error. We can safely 1497975c32a0SNeil Perrin * ignore this because the later write will provide the 1498975c32a0SNeil Perrin * correct data. 1499975c32a0SNeil Perrin */ 1500975c32a0SNeil Perrin zbookmark_t zb; 1501975c32a0SNeil Perrin 1502975c32a0SNeil Perrin zb.zb_objset = dmu_objset_id(zilog->zl_os); 1503975c32a0SNeil Perrin zb.zb_object = lr->lr_foid; 1504975c32a0SNeil Perrin zb.zb_level = 0; 1505975c32a0SNeil Perrin zb.zb_blkid = -1; /* unknown */ 1506975c32a0SNeil Perrin 1507975c32a0SNeil Perrin blksz = BP_GET_LSIZE(&lr->lr_blkptr); 1508975c32a0SNeil Perrin (void) zio_wait(zio_read(NULL, zilog->zl_spa, wbp, wbuf, blksz, 1509975c32a0SNeil Perrin NULL, NULL, ZIO_PRIORITY_SYNC_READ, 1510975c32a0SNeil Perrin ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb)); 1511975c32a0SNeil Perrin } 1512975c32a0SNeil Perrin lr->lr_offset -= lr->lr_offset % blksz; 1513975c32a0SNeil Perrin lr->lr_length = blksz; 1514975c32a0SNeil Perrin } 1515975c32a0SNeil Perrin 1516fa9e4066Sahrens typedef struct zil_replay_arg { 1517fa9e4066Sahrens objset_t *zr_os; 1518fa9e4066Sahrens zil_replay_func_t **zr_replay; 1519fa9e4066Sahrens void *zr_arg; 1520fa9e4066Sahrens boolean_t zr_byteswap; 1521fa9e4066Sahrens char *zr_lrbuf; 1522fa9e4066Sahrens } zil_replay_arg_t; 1523fa9e4066Sahrens 1524fa9e4066Sahrens static void 1525fa9e4066Sahrens zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg) 1526fa9e4066Sahrens { 1527fa9e4066Sahrens zil_replay_arg_t *zr = zra; 1528d80c45e0Sbonwick const zil_header_t *zh = zilog->zl_header; 1529fa9e4066Sahrens uint64_t reclen = lr->lrc_reclen; 1530fa9e4066Sahrens uint64_t txtype = lr->lrc_txtype; 153167bd71c6Sperrin char *name; 15321209a471SNeil Perrin int pass, error; 1533fa9e4066Sahrens 15341209a471SNeil Perrin if (!zilog->zl_replay) /* giving up */ 1535fa9e4066Sahrens return; 1536fa9e4066Sahrens 1537fa9e4066Sahrens if (lr->lrc_txg < claim_txg) /* already committed */ 1538fa9e4066Sahrens return; 1539fa9e4066Sahrens 1540fa9e4066Sahrens if (lr->lrc_seq <= zh->zh_replay_seq) /* already replayed */ 1541fa9e4066Sahrens return; 1542fa9e4066Sahrens 1543da6c28aaSamw /* Strip case-insensitive bit, still present in log record */ 1544da6c28aaSamw txtype &= ~TX_CI; 1545da6c28aaSamw 15461209a471SNeil Perrin if (txtype == 0 || txtype >= TX_MAX_TYPE) { 15471209a471SNeil Perrin error = EINVAL; 15481209a471SNeil Perrin goto bad; 15491209a471SNeil Perrin } 15501209a471SNeil Perrin 1551fa9e4066Sahrens /* 1552fa9e4066Sahrens * Make a copy of the data so we can revise and extend it. 1553fa9e4066Sahrens */ 1554fa9e4066Sahrens bcopy(lr, zr->zr_lrbuf, reclen); 1555fa9e4066Sahrens 1556fa9e4066Sahrens /* 1557fa9e4066Sahrens * The log block containing this lr may have been byteswapped 1558fa9e4066Sahrens * so that we can easily examine common fields like lrc_txtype. 1559fa9e4066Sahrens * However, the log is a mix of different data types, and only the 1560fa9e4066Sahrens * replay vectors know how to byteswap their records. Therefore, if 1561fa9e4066Sahrens * the lr was byteswapped, undo it before invoking the replay vector. 1562fa9e4066Sahrens */ 1563fa9e4066Sahrens if (zr->zr_byteswap) 1564fa9e4066Sahrens byteswap_uint64_array(zr->zr_lrbuf, reclen); 1565fa9e4066Sahrens 1566fa9e4066Sahrens /* 1567fa9e4066Sahrens * We must now do two things atomically: replay this log record, 15681209a471SNeil Perrin * and update the log header sequence number to reflect the fact that 15691209a471SNeil Perrin * we did so. At the end of each replay function the sequence number 15701209a471SNeil Perrin * is updated if we are in replay mode. 1571fa9e4066Sahrens */ 15721209a471SNeil Perrin for (pass = 1; pass <= 2; pass++) { 15731209a471SNeil Perrin zilog->zl_replaying_seq = lr->lrc_seq; 15741209a471SNeil Perrin /* Only byteswap (if needed) on the 1st pass. */ 15751209a471SNeil Perrin error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lrbuf, 15761209a471SNeil Perrin zr->zr_byteswap && pass == 1); 1577fa9e4066Sahrens 157867bd71c6Sperrin if (!error) 157967bd71c6Sperrin return; 158067bd71c6Sperrin 158167bd71c6Sperrin /* 158267bd71c6Sperrin * The DMU's dnode layer doesn't see removes until the txg 158367bd71c6Sperrin * commits, so a subsequent claim can spuriously fail with 15841209a471SNeil Perrin * EEXIST. So if we receive any error we try syncing out 15851209a471SNeil Perrin * any removes then retry the transaction. 158667bd71c6Sperrin */ 15871209a471SNeil Perrin if (pass == 1) 158867bd71c6Sperrin txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0); 1589fa9e4066Sahrens } 1590fa9e4066Sahrens 1591d30e856aSNeil Perrin bad: 15921209a471SNeil Perrin ASSERT(error); 159367bd71c6Sperrin name = kmem_alloc(MAXNAMELEN, KM_SLEEP); 159467bd71c6Sperrin dmu_objset_name(zr->zr_os, name); 159567bd71c6Sperrin cmn_err(CE_WARN, "ZFS replay transaction error %d, " 1596da6c28aaSamw "dataset %s, seq 0x%llx, txtype %llu %s\n", 1597da6c28aaSamw error, name, (u_longlong_t)lr->lrc_seq, (u_longlong_t)txtype, 1598da6c28aaSamw (lr->lrc_txtype & TX_CI) ? "CI" : ""); 15991209a471SNeil Perrin zilog->zl_replay = B_FALSE; 160067bd71c6Sperrin kmem_free(name, MAXNAMELEN); 160167bd71c6Sperrin } 1602fa9e4066Sahrens 160367bd71c6Sperrin /* ARGSUSED */ 160467bd71c6Sperrin static void 160567bd71c6Sperrin zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg) 160667bd71c6Sperrin { 160767bd71c6Sperrin zilog->zl_replay_blks++; 1608fa9e4066Sahrens } 1609fa9e4066Sahrens 1610fa9e4066Sahrens /* 161113f5297eSperrin * If this dataset has a non-empty intent log, replay it and destroy it. 1612fa9e4066Sahrens */ 1613fa9e4066Sahrens void 16141209a471SNeil Perrin zil_replay(objset_t *os, void *arg, zil_replay_func_t *replay_func[TX_MAX_TYPE]) 1615fa9e4066Sahrens { 1616fa9e4066Sahrens zilog_t *zilog = dmu_objset_zil(os); 1617d80c45e0Sbonwick const zil_header_t *zh = zilog->zl_header; 1618d80c45e0Sbonwick zil_replay_arg_t zr; 161913f5297eSperrin 16203589c4f0SNeil Perrin if ((zh->zh_flags & ZIL_REPLAY_NEEDED) == 0) { 1621d80c45e0Sbonwick zil_destroy(zilog, B_TRUE); 162213f5297eSperrin return; 162313f5297eSperrin } 1624fa9e4066Sahrens 1625fa9e4066Sahrens zr.zr_os = os; 1626fa9e4066Sahrens zr.zr_replay = replay_func; 1627fa9e4066Sahrens zr.zr_arg = arg; 1628d80c45e0Sbonwick zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log); 1629fa9e4066Sahrens zr.zr_lrbuf = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP); 1630fa9e4066Sahrens 1631fa9e4066Sahrens /* 1632fa9e4066Sahrens * Wait for in-progress removes to sync before starting replay. 1633fa9e4066Sahrens */ 1634fa9e4066Sahrens txg_wait_synced(zilog->zl_dmu_pool, 0); 1635fa9e4066Sahrens 16361209a471SNeil Perrin zilog->zl_replay = B_TRUE; 163767bd71c6Sperrin zilog->zl_replay_time = lbolt; 163867bd71c6Sperrin ASSERT(zilog->zl_replay_blks == 0); 163967bd71c6Sperrin (void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr, 1640d80c45e0Sbonwick zh->zh_claim_txg); 1641fa9e4066Sahrens kmem_free(zr.zr_lrbuf, 2 * SPA_MAXBLOCKSIZE); 1642fa9e4066Sahrens 1643d80c45e0Sbonwick zil_destroy(zilog, B_FALSE); 1644a4611edeSahrens txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 16451209a471SNeil Perrin zilog->zl_replay = B_FALSE; 1646fa9e4066Sahrens } 1647436b2950Sperrin 1648436b2950Sperrin /* 1649436b2950Sperrin * Report whether all transactions are committed 1650436b2950Sperrin */ 1651436b2950Sperrin int 1652436b2950Sperrin zil_is_committed(zilog_t *zilog) 1653436b2950Sperrin { 1654436b2950Sperrin lwb_t *lwb; 1655b19a79ecSperrin int ret; 1656b19a79ecSperrin 1657b19a79ecSperrin mutex_enter(&zilog->zl_lock); 1658b19a79ecSperrin while (zilog->zl_writer) 1659b19a79ecSperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 1660b19a79ecSperrin 1661b19a79ecSperrin /* recent unpushed intent log transactions? */ 1662b19a79ecSperrin if (!list_is_empty(&zilog->zl_itx_list)) { 1663b19a79ecSperrin ret = B_FALSE; 1664b19a79ecSperrin goto out; 1665b19a79ecSperrin } 1666436b2950Sperrin 1667b19a79ecSperrin /* intent log never used? */ 1668b19a79ecSperrin lwb = list_head(&zilog->zl_lwb_list); 1669b19a79ecSperrin if (lwb == NULL) { 1670b19a79ecSperrin ret = B_TRUE; 1671b19a79ecSperrin goto out; 1672b19a79ecSperrin } 1673436b2950Sperrin 1674436b2950Sperrin /* 1675b19a79ecSperrin * more than 1 log buffer means zil_sync() hasn't yet freed 1676b19a79ecSperrin * entries after a txg has committed 1677436b2950Sperrin */ 1678b19a79ecSperrin if (list_next(&zilog->zl_lwb_list, lwb)) { 1679b19a79ecSperrin ret = B_FALSE; 1680b19a79ecSperrin goto out; 1681b19a79ecSperrin } 1682b19a79ecSperrin 1683436b2950Sperrin ASSERT(zil_empty(zilog)); 1684b19a79ecSperrin ret = B_TRUE; 1685b19a79ecSperrin out: 1686b19a79ecSperrin cv_broadcast(&zilog->zl_cv_writer); 1687b19a79ecSperrin mutex_exit(&zilog->zl_lock); 1688b19a79ecSperrin return (ret); 1689436b2950Sperrin } 1690e6ca193dSGeorge Wilson 1691e6ca193dSGeorge Wilson /* ARGSUSED */ 1692e6ca193dSGeorge Wilson int 1693e6ca193dSGeorge Wilson zil_vdev_offline(char *osname, void *arg) 1694e6ca193dSGeorge Wilson { 1695e6ca193dSGeorge Wilson objset_t *os; 1696e6ca193dSGeorge Wilson zilog_t *zilog; 1697e6ca193dSGeorge Wilson int error; 1698e6ca193dSGeorge Wilson 1699503ad85cSMatthew Ahrens error = dmu_objset_hold(osname, FTAG, &os); 1700e6ca193dSGeorge Wilson if (error) 1701e6ca193dSGeorge Wilson return (error); 1702e6ca193dSGeorge Wilson 1703e6ca193dSGeorge Wilson zilog = dmu_objset_zil(os); 1704e6ca193dSGeorge Wilson if (zil_suspend(zilog) != 0) 1705e6ca193dSGeorge Wilson error = EEXIST; 1706e6ca193dSGeorge Wilson else 1707e6ca193dSGeorge Wilson zil_resume(zilog); 1708503ad85cSMatthew Ahrens dmu_objset_rele(os, FTAG); 1709e6ca193dSGeorge Wilson return (error); 1710e6ca193dSGeorge Wilson } 1711