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 /* 2213f5297eSperrin * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23fa9e4066Sahrens * Use is subject to license terms. 24fa9e4066Sahrens */ 25fa9e4066Sahrens 26fa9e4066Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27fa9e4066Sahrens 28fa9e4066Sahrens #include <sys/zfs_context.h> 29fa9e4066Sahrens #include <sys/spa.h> 30fa9e4066Sahrens #include <sys/dmu.h> 31fa9e4066Sahrens #include <sys/zap.h> 32fa9e4066Sahrens #include <sys/arc.h> 33fa9e4066Sahrens #include <sys/stat.h> 34fa9e4066Sahrens #include <sys/resource.h> 35fa9e4066Sahrens #include <sys/zil.h> 36fa9e4066Sahrens #include <sys/zil_impl.h> 37fa9e4066Sahrens #include <sys/dsl_dataset.h> 38fa9e4066Sahrens #include <sys/vdev.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 /* 67fa9e4066Sahrens * These global ZIL switches affect all pools 68fa9e4066Sahrens */ 69fa9e4066Sahrens int zil_disable = 0; /* disable intent logging */ 70fa9e4066Sahrens int zil_noflush = 0; /* don't flush write cache buffers on disks */ 71fa9e4066Sahrens 72fa9e4066Sahrens static kmem_cache_t *zil_lwb_cache; 73fa9e4066Sahrens 74fa9e4066Sahrens static int 75fa9e4066Sahrens zil_dva_compare(const void *x1, const void *x2) 76fa9e4066Sahrens { 77fa9e4066Sahrens const dva_t *dva1 = x1; 78fa9e4066Sahrens const dva_t *dva2 = x2; 79fa9e4066Sahrens 80fa9e4066Sahrens if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2)) 81fa9e4066Sahrens return (-1); 82fa9e4066Sahrens if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2)) 83fa9e4066Sahrens return (1); 84fa9e4066Sahrens 85fa9e4066Sahrens if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2)) 86fa9e4066Sahrens return (-1); 87fa9e4066Sahrens if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2)) 88fa9e4066Sahrens return (1); 89fa9e4066Sahrens 90fa9e4066Sahrens return (0); 91fa9e4066Sahrens } 92fa9e4066Sahrens 93fa9e4066Sahrens static void 94fa9e4066Sahrens zil_dva_tree_init(avl_tree_t *t) 95fa9e4066Sahrens { 96fa9e4066Sahrens avl_create(t, zil_dva_compare, sizeof (zil_dva_node_t), 97fa9e4066Sahrens offsetof(zil_dva_node_t, zn_node)); 98fa9e4066Sahrens } 99fa9e4066Sahrens 100fa9e4066Sahrens static void 101fa9e4066Sahrens zil_dva_tree_fini(avl_tree_t *t) 102fa9e4066Sahrens { 103fa9e4066Sahrens zil_dva_node_t *zn; 104fa9e4066Sahrens void *cookie = NULL; 105fa9e4066Sahrens 106fa9e4066Sahrens while ((zn = avl_destroy_nodes(t, &cookie)) != NULL) 107fa9e4066Sahrens kmem_free(zn, sizeof (zil_dva_node_t)); 108fa9e4066Sahrens 109fa9e4066Sahrens avl_destroy(t); 110fa9e4066Sahrens } 111fa9e4066Sahrens 112fa9e4066Sahrens static int 113fa9e4066Sahrens zil_dva_tree_add(avl_tree_t *t, dva_t *dva) 114fa9e4066Sahrens { 115fa9e4066Sahrens zil_dva_node_t *zn; 116fa9e4066Sahrens avl_index_t where; 117fa9e4066Sahrens 118fa9e4066Sahrens if (avl_find(t, dva, &where) != NULL) 119fa9e4066Sahrens return (EEXIST); 120fa9e4066Sahrens 121fa9e4066Sahrens zn = kmem_alloc(sizeof (zil_dva_node_t), KM_SLEEP); 122fa9e4066Sahrens zn->zn_dva = *dva; 123fa9e4066Sahrens avl_insert(t, zn, where); 124fa9e4066Sahrens 125fa9e4066Sahrens return (0); 126fa9e4066Sahrens } 127fa9e4066Sahrens 128d80c45e0Sbonwick static zil_header_t * 129d80c45e0Sbonwick zil_header_in_syncing_context(zilog_t *zilog) 130d80c45e0Sbonwick { 131d80c45e0Sbonwick return ((zil_header_t *)zilog->zl_header); 132d80c45e0Sbonwick } 133d80c45e0Sbonwick 134d80c45e0Sbonwick static void 135d80c45e0Sbonwick zil_init_log_chain(zilog_t *zilog, blkptr_t *bp) 136d80c45e0Sbonwick { 137d80c45e0Sbonwick zio_cksum_t *zc = &bp->blk_cksum; 138d80c45e0Sbonwick 139d80c45e0Sbonwick zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL); 140d80c45e0Sbonwick zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL); 141d80c45e0Sbonwick zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os); 142d80c45e0Sbonwick zc->zc_word[ZIL_ZC_SEQ] = 1ULL; 143d80c45e0Sbonwick } 144d80c45e0Sbonwick 145fa9e4066Sahrens /* 146fa9e4066Sahrens * Read a log block, make sure it's valid, and byteswap it if necessary. 147fa9e4066Sahrens */ 148fa9e4066Sahrens static int 149d80c45e0Sbonwick zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, arc_buf_t **abufpp) 150fa9e4066Sahrens { 151d80c45e0Sbonwick blkptr_t blk = *bp; 152ea8dc4b6Seschrock zbookmark_t zb; 15313506d1eSmaybee uint32_t aflags = ARC_WAIT; 154fa9e4066Sahrens int error; 155fa9e4066Sahrens 156d80c45e0Sbonwick zb.zb_objset = bp->blk_cksum.zc_word[ZIL_ZC_OBJSET]; 157ea8dc4b6Seschrock zb.zb_object = 0; 158ea8dc4b6Seschrock zb.zb_level = -1; 159d80c45e0Sbonwick zb.zb_blkid = bp->blk_cksum.zc_word[ZIL_ZC_SEQ]; 160ea8dc4b6Seschrock 161d80c45e0Sbonwick *abufpp = NULL; 162fa9e4066Sahrens 163d80c45e0Sbonwick error = arc_read(NULL, zilog->zl_spa, &blk, byteswap_uint64_array, 164d80c45e0Sbonwick arc_getbuf_func, abufpp, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL | 16513506d1eSmaybee ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB, &aflags, &zb); 166fa9e4066Sahrens 167d80c45e0Sbonwick if (error == 0) { 168d80c45e0Sbonwick char *data = (*abufpp)->b_data; 169d80c45e0Sbonwick uint64_t blksz = BP_GET_LSIZE(bp); 170d80c45e0Sbonwick zil_trailer_t *ztp = (zil_trailer_t *)(data + blksz) - 1; 171d80c45e0Sbonwick zio_cksum_t cksum = bp->blk_cksum; 172fa9e4066Sahrens 173d80c45e0Sbonwick /* 174d80c45e0Sbonwick * Sequence numbers should be... sequential. The checksum 175d80c45e0Sbonwick * verifier for the next block should be bp's checksum plus 1. 176d80c45e0Sbonwick */ 177d80c45e0Sbonwick cksum.zc_word[ZIL_ZC_SEQ]++; 178d80c45e0Sbonwick 179d80c45e0Sbonwick if (bcmp(&cksum, &ztp->zit_next_blk.blk_cksum, sizeof (cksum))) 180d80c45e0Sbonwick error = ESTALE; 181d80c45e0Sbonwick else if (BP_IS_HOLE(&ztp->zit_next_blk)) 182d80c45e0Sbonwick error = ENOENT; 183d80c45e0Sbonwick else if (ztp->zit_nused > (blksz - sizeof (zil_trailer_t))) 184d80c45e0Sbonwick error = EOVERFLOW; 185fa9e4066Sahrens 186d80c45e0Sbonwick if (error) { 187d80c45e0Sbonwick VERIFY(arc_buf_remove_ref(*abufpp, abufpp) == 1); 188d80c45e0Sbonwick *abufpp = NULL; 189d80c45e0Sbonwick } 190fa9e4066Sahrens } 191fa9e4066Sahrens 192d80c45e0Sbonwick dprintf("error %d on %llu:%llu\n", error, zb.zb_objset, zb.zb_blkid); 193fa9e4066Sahrens 194d80c45e0Sbonwick return (error); 195fa9e4066Sahrens } 196fa9e4066Sahrens 197fa9e4066Sahrens /* 198fa9e4066Sahrens * Parse the intent log, and call parse_func for each valid record within. 199d80c45e0Sbonwick * Return the highest sequence number. 200fa9e4066Sahrens */ 201d80c45e0Sbonwick uint64_t 202fa9e4066Sahrens zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func, 203fa9e4066Sahrens zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg) 204fa9e4066Sahrens { 205d80c45e0Sbonwick const zil_header_t *zh = zilog->zl_header; 206d80c45e0Sbonwick uint64_t claim_seq = zh->zh_claim_seq; 207d80c45e0Sbonwick uint64_t seq = 0; 208d80c45e0Sbonwick uint64_t max_seq = 0; 209d80c45e0Sbonwick blkptr_t blk = zh->zh_log; 210d80c45e0Sbonwick arc_buf_t *abuf; 211fa9e4066Sahrens char *lrbuf, *lrp; 212fa9e4066Sahrens zil_trailer_t *ztp; 213fa9e4066Sahrens int reclen, error; 214fa9e4066Sahrens 215fa9e4066Sahrens if (BP_IS_HOLE(&blk)) 216d80c45e0Sbonwick return (max_seq); 217fa9e4066Sahrens 218fa9e4066Sahrens /* 219fa9e4066Sahrens * Starting at the block pointed to by zh_log we read the log chain. 220fa9e4066Sahrens * For each block in the chain we strongly check that block to 221fa9e4066Sahrens * ensure its validity. We stop when an invalid block is found. 222fa9e4066Sahrens * For each block pointer in the chain we call parse_blk_func(). 223fa9e4066Sahrens * For each record in each valid block we call parse_lr_func(). 224d80c45e0Sbonwick * If the log has been claimed, stop if we encounter a sequence 225d80c45e0Sbonwick * number greater than the highest claimed sequence number. 226fa9e4066Sahrens */ 227fa9e4066Sahrens zil_dva_tree_init(&zilog->zl_dva_tree); 228fa9e4066Sahrens for (;;) { 229d80c45e0Sbonwick seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 230d80c45e0Sbonwick 231d80c45e0Sbonwick if (claim_seq != 0 && seq > claim_seq) 232d80c45e0Sbonwick break; 233d80c45e0Sbonwick 234d80c45e0Sbonwick ASSERT(max_seq < seq); 235d80c45e0Sbonwick max_seq = seq; 236d80c45e0Sbonwick 237d80c45e0Sbonwick error = zil_read_log_block(zilog, &blk, &abuf); 238fa9e4066Sahrens 239fa9e4066Sahrens if (parse_blk_func != NULL) 240fa9e4066Sahrens parse_blk_func(zilog, &blk, arg, txg); 241fa9e4066Sahrens 242fa9e4066Sahrens if (error) 243fa9e4066Sahrens break; 244fa9e4066Sahrens 245d80c45e0Sbonwick lrbuf = abuf->b_data; 246fa9e4066Sahrens ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1; 247fa9e4066Sahrens blk = ztp->zit_next_blk; 248fa9e4066Sahrens 249d80c45e0Sbonwick if (parse_lr_func == NULL) { 250d80c45e0Sbonwick VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 251fa9e4066Sahrens continue; 252d80c45e0Sbonwick } 253fa9e4066Sahrens 254fa9e4066Sahrens for (lrp = lrbuf; lrp < lrbuf + ztp->zit_nused; lrp += reclen) { 255fa9e4066Sahrens lr_t *lr = (lr_t *)lrp; 256fa9e4066Sahrens reclen = lr->lrc_reclen; 257fa9e4066Sahrens ASSERT3U(reclen, >=, sizeof (lr_t)); 258fa9e4066Sahrens parse_lr_func(zilog, lr, arg, txg); 259fa9e4066Sahrens } 260d80c45e0Sbonwick VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 261fa9e4066Sahrens } 262fa9e4066Sahrens zil_dva_tree_fini(&zilog->zl_dva_tree); 263d80c45e0Sbonwick 264d80c45e0Sbonwick return (max_seq); 265fa9e4066Sahrens } 266fa9e4066Sahrens 267fa9e4066Sahrens /* ARGSUSED */ 268fa9e4066Sahrens static void 269fa9e4066Sahrens zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg) 270fa9e4066Sahrens { 271fa9e4066Sahrens spa_t *spa = zilog->zl_spa; 272fa9e4066Sahrens int err; 273fa9e4066Sahrens 274fa9e4066Sahrens /* 275fa9e4066Sahrens * Claim log block if not already committed and not already claimed. 276fa9e4066Sahrens */ 277fa9e4066Sahrens if (bp->blk_birth >= first_txg && 278fa9e4066Sahrens zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp)) == 0) { 279fa9e4066Sahrens err = zio_wait(zio_claim(NULL, spa, first_txg, bp, NULL, NULL)); 280fa9e4066Sahrens ASSERT(err == 0); 281fa9e4066Sahrens } 282fa9e4066Sahrens } 283fa9e4066Sahrens 284fa9e4066Sahrens static void 285fa9e4066Sahrens zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg) 286fa9e4066Sahrens { 287fa9e4066Sahrens if (lrc->lrc_txtype == TX_WRITE) { 288fa9e4066Sahrens lr_write_t *lr = (lr_write_t *)lrc; 289fa9e4066Sahrens zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg); 290fa9e4066Sahrens } 291fa9e4066Sahrens } 292fa9e4066Sahrens 293fa9e4066Sahrens /* ARGSUSED */ 294fa9e4066Sahrens static void 295fa9e4066Sahrens zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg) 296fa9e4066Sahrens { 297fa9e4066Sahrens zio_free_blk(zilog->zl_spa, bp, dmu_tx_get_txg(tx)); 298fa9e4066Sahrens } 299fa9e4066Sahrens 300fa9e4066Sahrens static void 301fa9e4066Sahrens zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg) 302fa9e4066Sahrens { 303fa9e4066Sahrens /* 304fa9e4066Sahrens * If we previously claimed it, we need to free it. 305fa9e4066Sahrens */ 306fa9e4066Sahrens if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE) { 307fa9e4066Sahrens lr_write_t *lr = (lr_write_t *)lrc; 308fa9e4066Sahrens blkptr_t *bp = &lr->lr_blkptr; 309fa9e4066Sahrens if (bp->blk_birth >= claim_txg && 310fa9e4066Sahrens !zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp))) { 311fa9e4066Sahrens (void) arc_free(NULL, zilog->zl_spa, 312fa9e4066Sahrens dmu_tx_get_txg(tx), bp, NULL, NULL, ARC_WAIT); 313fa9e4066Sahrens } 314fa9e4066Sahrens } 315fa9e4066Sahrens } 316fa9e4066Sahrens 317fa9e4066Sahrens /* 318fa9e4066Sahrens * Create an on-disk intent log. 319fa9e4066Sahrens */ 320fa9e4066Sahrens static void 321fa9e4066Sahrens zil_create(zilog_t *zilog) 322fa9e4066Sahrens { 323d80c45e0Sbonwick const zil_header_t *zh = zilog->zl_header; 324fa9e4066Sahrens lwb_t *lwb; 325d80c45e0Sbonwick uint64_t txg = 0; 326d80c45e0Sbonwick dmu_tx_t *tx = NULL; 327fa9e4066Sahrens blkptr_t blk; 328d80c45e0Sbonwick int error = 0; 329fa9e4066Sahrens 330fa9e4066Sahrens /* 331d80c45e0Sbonwick * Wait for any previous destroy to complete. 332fa9e4066Sahrens */ 333d80c45e0Sbonwick txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 334d80c45e0Sbonwick 335d80c45e0Sbonwick ASSERT(zh->zh_claim_txg == 0); 336d80c45e0Sbonwick ASSERT(zh->zh_replay_seq == 0); 337d80c45e0Sbonwick 338d80c45e0Sbonwick blk = zh->zh_log; 339fa9e4066Sahrens 340fa9e4066Sahrens /* 341d80c45e0Sbonwick * If we don't already have an initial log block, allocate one now. 342fa9e4066Sahrens */ 343d80c45e0Sbonwick if (BP_IS_HOLE(&blk)) { 344d80c45e0Sbonwick tx = dmu_tx_create(zilog->zl_os); 345d80c45e0Sbonwick (void) dmu_tx_assign(tx, TXG_WAIT); 346d80c45e0Sbonwick dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 347d80c45e0Sbonwick txg = dmu_tx_get_txg(tx); 348d80c45e0Sbonwick 349d80c45e0Sbonwick error = zio_alloc_blk(zilog->zl_spa, ZIL_MIN_BLKSZ, &blk, txg); 350d80c45e0Sbonwick 351d80c45e0Sbonwick if (error == 0) 352d80c45e0Sbonwick zil_init_log_chain(zilog, &blk); 35313f5297eSperrin } 354fa9e4066Sahrens 355d80c45e0Sbonwick /* 356d80c45e0Sbonwick * Allocate a log write buffer (lwb) for the first log block. 357d80c45e0Sbonwick */ 358d80c45e0Sbonwick if (error == 0) { 359fa9e4066Sahrens lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 360fa9e4066Sahrens lwb->lwb_zilog = zilog; 361fa9e4066Sahrens lwb->lwb_blk = blk; 362fa9e4066Sahrens lwb->lwb_nused = 0; 363fa9e4066Sahrens lwb->lwb_sz = BP_GET_LSIZE(&lwb->lwb_blk); 364fa9e4066Sahrens lwb->lwb_buf = zio_buf_alloc(lwb->lwb_sz); 365fa9e4066Sahrens lwb->lwb_max_txg = txg; 366c5c6ffa0Smaybee lwb->lwb_zio = NULL; 367c5c6ffa0Smaybee 368fa9e4066Sahrens mutex_enter(&zilog->zl_lock); 369fa9e4066Sahrens list_insert_tail(&zilog->zl_lwb_list, lwb); 370fa9e4066Sahrens mutex_exit(&zilog->zl_lock); 371fa9e4066Sahrens } 372fa9e4066Sahrens 373d80c45e0Sbonwick /* 374d80c45e0Sbonwick * If we just allocated the first log block, commit our transaction 375d80c45e0Sbonwick * and wait for zil_sync() to stuff the block poiner into zh_log. 376d80c45e0Sbonwick * (zh is part of the MOS, so we cannot modify it in open context.) 377d80c45e0Sbonwick */ 378d80c45e0Sbonwick if (tx != NULL) { 379d80c45e0Sbonwick dmu_tx_commit(tx); 38013f5297eSperrin txg_wait_synced(zilog->zl_dmu_pool, txg); 381d80c45e0Sbonwick } 382d80c45e0Sbonwick 383d80c45e0Sbonwick ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0); 384fa9e4066Sahrens } 385fa9e4066Sahrens 386fa9e4066Sahrens /* 387fa9e4066Sahrens * In one tx, free all log blocks and clear the log header. 388d80c45e0Sbonwick * If keep_first is set, then we're replaying a log with no content. 389d80c45e0Sbonwick * We want to keep the first block, however, so that the first 390d80c45e0Sbonwick * synchronous transaction doesn't require a txg_wait_synced() 391d80c45e0Sbonwick * in zil_create(). We don't need to txg_wait_synced() here either 392d80c45e0Sbonwick * when keep_first is set, because both zil_create() and zil_destroy() 393d80c45e0Sbonwick * will wait for any in-progress destroys to complete. 394fa9e4066Sahrens */ 395fa9e4066Sahrens void 396d80c45e0Sbonwick zil_destroy(zilog_t *zilog, boolean_t keep_first) 397fa9e4066Sahrens { 398d80c45e0Sbonwick const zil_header_t *zh = zilog->zl_header; 399d80c45e0Sbonwick lwb_t *lwb; 400fa9e4066Sahrens dmu_tx_t *tx; 401fa9e4066Sahrens uint64_t txg; 402fa9e4066Sahrens 403d80c45e0Sbonwick /* 404d80c45e0Sbonwick * Wait for any previous destroy to complete. 405d80c45e0Sbonwick */ 406d80c45e0Sbonwick txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 407fa9e4066Sahrens 408d80c45e0Sbonwick if (BP_IS_HOLE(&zh->zh_log)) 409fa9e4066Sahrens return; 410fa9e4066Sahrens 411fa9e4066Sahrens tx = dmu_tx_create(zilog->zl_os); 412fa9e4066Sahrens (void) dmu_tx_assign(tx, TXG_WAIT); 413fa9e4066Sahrens dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 414fa9e4066Sahrens txg = dmu_tx_get_txg(tx); 415fa9e4066Sahrens 416d80c45e0Sbonwick mutex_enter(&zilog->zl_lock); 417d80c45e0Sbonwick 418d80c45e0Sbonwick ASSERT3U(zilog->zl_destroy_txg, <, txg); 419fa9e4066Sahrens zilog->zl_destroy_txg = txg; 420d80c45e0Sbonwick zilog->zl_keep_first = keep_first; 421d80c45e0Sbonwick 422d80c45e0Sbonwick if (!list_is_empty(&zilog->zl_lwb_list)) { 423d80c45e0Sbonwick ASSERT(zh->zh_claim_txg == 0); 424d80c45e0Sbonwick ASSERT(!keep_first); 425d80c45e0Sbonwick while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 426d80c45e0Sbonwick list_remove(&zilog->zl_lwb_list, lwb); 427d80c45e0Sbonwick if (lwb->lwb_buf != NULL) 428d80c45e0Sbonwick zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 429d80c45e0Sbonwick zio_free_blk(zilog->zl_spa, &lwb->lwb_blk, txg); 430d80c45e0Sbonwick kmem_cache_free(zil_lwb_cache, lwb); 431d80c45e0Sbonwick } 432d80c45e0Sbonwick } else { 433d80c45e0Sbonwick if (!keep_first) { 434d80c45e0Sbonwick (void) zil_parse(zilog, zil_free_log_block, 435d80c45e0Sbonwick zil_free_log_record, tx, zh->zh_claim_txg); 436d80c45e0Sbonwick } 437d80c45e0Sbonwick } 438*b19a79ecSperrin mutex_exit(&zilog->zl_lock); 439fa9e4066Sahrens 440fa9e4066Sahrens dmu_tx_commit(tx); 441fa9e4066Sahrens 442d80c45e0Sbonwick if (keep_first) /* no need to wait in this case */ 443d80c45e0Sbonwick return; 444d80c45e0Sbonwick 445d80c45e0Sbonwick txg_wait_synced(zilog->zl_dmu_pool, txg); 446d80c45e0Sbonwick ASSERT(BP_IS_HOLE(&zh->zh_log)); 447fa9e4066Sahrens } 448fa9e4066Sahrens 4491d452cf5Sahrens int 450fa9e4066Sahrens zil_claim(char *osname, void *txarg) 451fa9e4066Sahrens { 452fa9e4066Sahrens dmu_tx_t *tx = txarg; 453fa9e4066Sahrens uint64_t first_txg = dmu_tx_get_txg(tx); 454fa9e4066Sahrens zilog_t *zilog; 455fa9e4066Sahrens zil_header_t *zh; 456fa9e4066Sahrens objset_t *os; 457fa9e4066Sahrens int error; 458fa9e4066Sahrens 459fa9e4066Sahrens error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_STANDARD, &os); 460fa9e4066Sahrens if (error) { 461fa9e4066Sahrens cmn_err(CE_WARN, "can't process intent log for %s", osname); 4621d452cf5Sahrens return (0); 463fa9e4066Sahrens } 464fa9e4066Sahrens 465fa9e4066Sahrens zilog = dmu_objset_zil(os); 466d80c45e0Sbonwick zh = zil_header_in_syncing_context(zilog); 467fa9e4066Sahrens 468fa9e4066Sahrens /* 469d80c45e0Sbonwick * Claim all log blocks if we haven't already done so, and remember 470d80c45e0Sbonwick * the highest claimed sequence number. This ensures that if we can 471d80c45e0Sbonwick * read only part of the log now (e.g. due to a missing device), 472d80c45e0Sbonwick * but we can read the entire log later, we will not try to replay 473d80c45e0Sbonwick * or destroy beyond the last block we successfully claimed. 474fa9e4066Sahrens */ 475fa9e4066Sahrens ASSERT3U(zh->zh_claim_txg, <=, first_txg); 476fa9e4066Sahrens if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) { 477fa9e4066Sahrens zh->zh_claim_txg = first_txg; 478d80c45e0Sbonwick zh->zh_claim_seq = zil_parse(zilog, zil_claim_log_block, 479d80c45e0Sbonwick zil_claim_log_record, tx, first_txg); 480fa9e4066Sahrens dsl_dataset_dirty(dmu_objset_ds(os), tx); 481fa9e4066Sahrens } 482d80c45e0Sbonwick 483fa9e4066Sahrens ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1)); 484fa9e4066Sahrens dmu_objset_close(os); 4851d452cf5Sahrens return (0); 486fa9e4066Sahrens } 487fa9e4066Sahrens 488fa9e4066Sahrens void 489*b19a79ecSperrin zil_add_vdev(zilog_t *zilog, uint64_t vdev) 490fa9e4066Sahrens { 491fa9e4066Sahrens zil_vdev_t *zv; 492fa9e4066Sahrens 493fa9e4066Sahrens if (zil_noflush) 494fa9e4066Sahrens return; 495fa9e4066Sahrens 496fa9e4066Sahrens ASSERT(MUTEX_HELD(&zilog->zl_lock)); 497fa9e4066Sahrens zv = kmem_alloc(sizeof (zil_vdev_t), KM_SLEEP); 498fa9e4066Sahrens zv->vdev = vdev; 499fa9e4066Sahrens list_insert_tail(&zilog->zl_vdev_list, zv); 500fa9e4066Sahrens } 501fa9e4066Sahrens 502fa9e4066Sahrens void 503*b19a79ecSperrin zil_flush_vdevs(zilog_t *zilog) 504fa9e4066Sahrens { 505fa9e4066Sahrens vdev_t *vd; 506fa9e4066Sahrens zil_vdev_t *zv, *zv2; 507fa9e4066Sahrens zio_t *zio; 508fa9e4066Sahrens spa_t *spa; 509fa9e4066Sahrens uint64_t vdev; 510fa9e4066Sahrens 511fa9e4066Sahrens if (zil_noflush) 512fa9e4066Sahrens return; 513fa9e4066Sahrens 514fa9e4066Sahrens ASSERT(MUTEX_HELD(&zilog->zl_lock)); 515fa9e4066Sahrens 516fa9e4066Sahrens spa = zilog->zl_spa; 517fa9e4066Sahrens zio = NULL; 518fa9e4066Sahrens 519*b19a79ecSperrin while ((zv = list_head(&zilog->zl_vdev_list)) != NULL) { 520fa9e4066Sahrens vdev = zv->vdev; 521fa9e4066Sahrens list_remove(&zilog->zl_vdev_list, zv); 522fa9e4066Sahrens kmem_free(zv, sizeof (zil_vdev_t)); 523fa9e4066Sahrens 524fa9e4066Sahrens /* 525*b19a79ecSperrin * remove all chained entries with same vdev 526fa9e4066Sahrens */ 527fa9e4066Sahrens zv = list_head(&zilog->zl_vdev_list); 528*b19a79ecSperrin while (zv) { 529fa9e4066Sahrens zv2 = list_next(&zilog->zl_vdev_list, zv); 530fa9e4066Sahrens if (zv->vdev == vdev) { 531fa9e4066Sahrens list_remove(&zilog->zl_vdev_list, zv); 532fa9e4066Sahrens kmem_free(zv, sizeof (zil_vdev_t)); 533fa9e4066Sahrens } 534fa9e4066Sahrens zv = zv2; 535fa9e4066Sahrens } 536fa9e4066Sahrens 537fa9e4066Sahrens /* flush the write cache for this vdev */ 538fa9e4066Sahrens mutex_exit(&zilog->zl_lock); 539fa9e4066Sahrens if (zio == NULL) 540fa9e4066Sahrens zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); 541fa9e4066Sahrens vd = vdev_lookup_top(spa, vdev); 542fa9e4066Sahrens ASSERT(vd); 543fa9e4066Sahrens (void) zio_nowait(zio_ioctl(zio, spa, vd, DKIOCFLUSHWRITECACHE, 544fa9e4066Sahrens NULL, NULL, ZIO_PRIORITY_NOW, 545fa9e4066Sahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY)); 546fa9e4066Sahrens mutex_enter(&zilog->zl_lock); 547fa9e4066Sahrens } 548fa9e4066Sahrens 549fa9e4066Sahrens /* 550fa9e4066Sahrens * Wait for all the flushes to complete. Not all devices actually 551fa9e4066Sahrens * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails. 552fa9e4066Sahrens */ 55322ac5be4Sperrin if (zio != NULL) { 55422ac5be4Sperrin mutex_exit(&zilog->zl_lock); 555fa9e4066Sahrens (void) zio_wait(zio); 55622ac5be4Sperrin mutex_enter(&zilog->zl_lock); 55722ac5be4Sperrin } 558fa9e4066Sahrens } 559fa9e4066Sahrens 560fa9e4066Sahrens /* 561fa9e4066Sahrens * Function called when a log block write completes 562fa9e4066Sahrens */ 563fa9e4066Sahrens static void 564fa9e4066Sahrens zil_lwb_write_done(zio_t *zio) 565fa9e4066Sahrens { 566fa9e4066Sahrens lwb_t *lwb = zio->io_private; 567fa9e4066Sahrens zilog_t *zilog = lwb->lwb_zilog; 568fa9e4066Sahrens 569fa9e4066Sahrens /* 570fa9e4066Sahrens * Now that we've written this log block, we have a stable pointer 571fa9e4066Sahrens * to the next block in the chain, so it's OK to let the txg in 572fa9e4066Sahrens * which we allocated the next block sync. 573fa9e4066Sahrens */ 574fa9e4066Sahrens txg_rele_to_sync(&lwb->lwb_txgh); 575fa9e4066Sahrens 576fa9e4066Sahrens zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 577fa9e4066Sahrens mutex_enter(&zilog->zl_lock); 578fa9e4066Sahrens lwb->lwb_buf = NULL; 579fa9e4066Sahrens if (zio->io_error) { 580fa9e4066Sahrens zilog->zl_log_error = B_TRUE; 581fa9e4066Sahrens mutex_exit(&zilog->zl_lock); 582fa9e4066Sahrens return; 583fa9e4066Sahrens } 584fa9e4066Sahrens mutex_exit(&zilog->zl_lock); 585fa9e4066Sahrens } 586fa9e4066Sahrens 587c5c6ffa0Smaybee /* 588c5c6ffa0Smaybee * Initialize the io for a log block. 589c5c6ffa0Smaybee * 590c5c6ffa0Smaybee * Note, we should not initialize the IO until we are about 591c5c6ffa0Smaybee * to use it, since zio_rewrite() does a spa_config_enter(). 592c5c6ffa0Smaybee */ 593c5c6ffa0Smaybee static void 594c5c6ffa0Smaybee zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb) 595c5c6ffa0Smaybee { 596c5c6ffa0Smaybee zbookmark_t zb; 597c5c6ffa0Smaybee 598c5c6ffa0Smaybee zb.zb_objset = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET]; 599c5c6ffa0Smaybee zb.zb_object = 0; 600c5c6ffa0Smaybee zb.zb_level = -1; 601c5c6ffa0Smaybee zb.zb_blkid = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 602c5c6ffa0Smaybee 603*b19a79ecSperrin if (zilog->zl_root_zio == NULL) { 604*b19a79ecSperrin zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL, 605*b19a79ecSperrin ZIO_FLAG_CANFAIL); 606*b19a79ecSperrin } 607*b19a79ecSperrin lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa, 608c5c6ffa0Smaybee ZIO_CHECKSUM_ZILOG, 0, &lwb->lwb_blk, lwb->lwb_buf, 609c5c6ffa0Smaybee lwb->lwb_sz, zil_lwb_write_done, lwb, 610c5c6ffa0Smaybee ZIO_PRIORITY_LOG_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb); 611c5c6ffa0Smaybee } 612c5c6ffa0Smaybee 613fa9e4066Sahrens /* 614fa9e4066Sahrens * Start a log block write and advance to the next log block. 615fa9e4066Sahrens * Calls are serialized. 616fa9e4066Sahrens */ 617fa9e4066Sahrens static lwb_t * 618fa9e4066Sahrens zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb) 619fa9e4066Sahrens { 620fa9e4066Sahrens lwb_t *nlwb; 621fa9e4066Sahrens zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1; 622d80c45e0Sbonwick spa_t *spa = zilog->zl_spa; 623d80c45e0Sbonwick blkptr_t *bp = &ztp->zit_next_blk; 624fa9e4066Sahrens uint64_t txg; 625fa9e4066Sahrens uint64_t zil_blksz; 626fa9e4066Sahrens int error; 627fa9e4066Sahrens 628fa9e4066Sahrens ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb)); 629fa9e4066Sahrens 630fa9e4066Sahrens /* 631fa9e4066Sahrens * Allocate the next block and save its address in this block 632fa9e4066Sahrens * before writing it in order to establish the log chain. 633fa9e4066Sahrens * Note that if the allocation of nlwb synced before we wrote 634fa9e4066Sahrens * the block that points at it (lwb), we'd leak it if we crashed. 635fa9e4066Sahrens * Therefore, we don't do txg_rele_to_sync() until zil_lwb_write_done(). 636fa9e4066Sahrens */ 637fa9e4066Sahrens txg = txg_hold_open(zilog->zl_dmu_pool, &lwb->lwb_txgh); 638fa9e4066Sahrens txg_rele_to_quiesce(&lwb->lwb_txgh); 639fa9e4066Sahrens 640fa9e4066Sahrens /* 64122ac5be4Sperrin * Pick a ZIL blocksize. We request a size that is the 64222ac5be4Sperrin * maximum of the previous used size, the current used size and 64322ac5be4Sperrin * the amount waiting in the queue. 644fa9e4066Sahrens */ 645c5c6ffa0Smaybee zil_blksz = MAX(zilog->zl_prev_used, 646c5c6ffa0Smaybee zilog->zl_cur_used + sizeof (*ztp)); 64722ac5be4Sperrin zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp)); 648b4d654b0Sperrin zil_blksz = P2ROUNDUP_TYPED(zil_blksz, ZIL_MIN_BLKSZ, uint64_t); 64922ac5be4Sperrin if (zil_blksz > ZIL_MAX_BLKSZ) 65022ac5be4Sperrin zil_blksz = ZIL_MAX_BLKSZ; 651fa9e4066Sahrens 652d80c45e0Sbonwick error = zio_alloc_blk(spa, zil_blksz, bp, txg); 653fa9e4066Sahrens if (error) { 654ea8dc4b6Seschrock /* 655ea8dc4b6Seschrock * Reinitialise the lwb. 656ea8dc4b6Seschrock * By returning NULL the caller will call tx_wait_synced() 657ea8dc4b6Seschrock */ 658ea8dc4b6Seschrock mutex_enter(&zilog->zl_lock); 659ea8dc4b6Seschrock lwb->lwb_nused = 0; 660ea8dc4b6Seschrock mutex_exit(&zilog->zl_lock); 661fa9e4066Sahrens txg_rele_to_sync(&lwb->lwb_txgh); 662fa9e4066Sahrens return (NULL); 663fa9e4066Sahrens } 664fa9e4066Sahrens 665d80c45e0Sbonwick ASSERT3U(bp->blk_birth, ==, txg); 666ea8dc4b6Seschrock ztp->zit_pad = 0; 667fa9e4066Sahrens ztp->zit_nused = lwb->lwb_nused; 668fa9e4066Sahrens ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum; 669d80c45e0Sbonwick bp->blk_cksum = lwb->lwb_blk.blk_cksum; 670d80c45e0Sbonwick bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++; 671fa9e4066Sahrens 672fa9e4066Sahrens /* 673fa9e4066Sahrens * Allocate a new log write buffer (lwb). 674fa9e4066Sahrens */ 675fa9e4066Sahrens nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 676fa9e4066Sahrens 677fa9e4066Sahrens nlwb->lwb_zilog = zilog; 678d80c45e0Sbonwick nlwb->lwb_blk = *bp; 679fa9e4066Sahrens nlwb->lwb_nused = 0; 680fa9e4066Sahrens nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk); 681fa9e4066Sahrens nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz); 682fa9e4066Sahrens nlwb->lwb_max_txg = txg; 683c5c6ffa0Smaybee nlwb->lwb_zio = NULL; 684fa9e4066Sahrens 685fa9e4066Sahrens /* 686fa9e4066Sahrens * Put new lwb at the end of the log chain, 687fa9e4066Sahrens * and record the vdev for later flushing 688fa9e4066Sahrens */ 689fa9e4066Sahrens mutex_enter(&zilog->zl_lock); 690fa9e4066Sahrens list_insert_tail(&zilog->zl_lwb_list, nlwb); 691*b19a79ecSperrin zil_add_vdev(zilog, DVA_GET_VDEV(BP_IDENTITY(&(lwb->lwb_blk)))); 692fa9e4066Sahrens mutex_exit(&zilog->zl_lock); 693fa9e4066Sahrens 694fa9e4066Sahrens /* 695c5c6ffa0Smaybee * kick off the write for the old log block 696fa9e4066Sahrens */ 697c5c6ffa0Smaybee dprintf_bp(&lwb->lwb_blk, "lwb %p txg %llu: ", lwb, txg); 698*b19a79ecSperrin if (lwb->lwb_zio == NULL) 699c5c6ffa0Smaybee zil_lwb_write_init(zilog, lwb); 700c5c6ffa0Smaybee zio_nowait(lwb->lwb_zio); 701fa9e4066Sahrens 702fa9e4066Sahrens return (nlwb); 703fa9e4066Sahrens } 704fa9e4066Sahrens 705fa9e4066Sahrens static lwb_t * 706fa9e4066Sahrens zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb) 707fa9e4066Sahrens { 708fa9e4066Sahrens lr_t *lrc = &itx->itx_lr; /* common log record */ 709c5c6ffa0Smaybee lr_write_t *lr = (lr_write_t *)lrc; 710fa9e4066Sahrens uint64_t txg = lrc->lrc_txg; 711fa9e4066Sahrens uint64_t reclen = lrc->lrc_reclen; 712c5c6ffa0Smaybee uint64_t dlen; 713fa9e4066Sahrens 714fa9e4066Sahrens if (lwb == NULL) 715fa9e4066Sahrens return (NULL); 716fa9e4066Sahrens ASSERT(lwb->lwb_buf != NULL); 717fa9e4066Sahrens 718c5c6ffa0Smaybee if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY) 719c5c6ffa0Smaybee dlen = P2ROUNDUP_TYPED( 720c5c6ffa0Smaybee lr->lr_length, sizeof (uint64_t), uint64_t); 721c5c6ffa0Smaybee else 722c5c6ffa0Smaybee dlen = 0; 723fa9e4066Sahrens 724104e2ed7Sperrin zilog->zl_cur_used += (reclen + dlen); 72522ac5be4Sperrin 726fa9e4066Sahrens /* 727fa9e4066Sahrens * If this record won't fit in the current log block, start a new one. 728fa9e4066Sahrens */ 729104e2ed7Sperrin if (lwb->lwb_nused + reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) { 730fa9e4066Sahrens lwb = zil_lwb_write_start(zilog, lwb); 731c5c6ffa0Smaybee if (lwb == NULL) 732fa9e4066Sahrens return (NULL); 733ea8dc4b6Seschrock ASSERT(lwb->lwb_nused == 0); 734104e2ed7Sperrin if (reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) { 735fa9e4066Sahrens txg_wait_synced(zilog->zl_dmu_pool, txg); 736fa9e4066Sahrens return (lwb); 737fa9e4066Sahrens } 738fa9e4066Sahrens } 739fa9e4066Sahrens 740*b19a79ecSperrin /* 741*b19a79ecSperrin * Update the lrc_seq, to be log record sequence number. See zil.h 742*b19a79ecSperrin * Then copy the record to the log buffer. 743*b19a79ecSperrin */ 744*b19a79ecSperrin lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */ 745fa9e4066Sahrens bcopy(lrc, lwb->lwb_buf + lwb->lwb_nused, reclen); 746c5c6ffa0Smaybee 747c5c6ffa0Smaybee /* 748c5c6ffa0Smaybee * If it's a write, fetch the data or get its blkptr as appropriate. 749c5c6ffa0Smaybee */ 750c5c6ffa0Smaybee if (lrc->lrc_txtype == TX_WRITE) { 751c5c6ffa0Smaybee if (txg > spa_freeze_txg(zilog->zl_spa)) 752c5c6ffa0Smaybee txg_wait_synced(zilog->zl_dmu_pool, txg); 753c5c6ffa0Smaybee if (itx->itx_wr_state != WR_COPIED) { 754c5c6ffa0Smaybee char *dbuf; 755c5c6ffa0Smaybee int error; 756c5c6ffa0Smaybee 757c5c6ffa0Smaybee /* alignment is guaranteed */ 758c5c6ffa0Smaybee lr = (lr_write_t *)(lwb->lwb_buf + lwb->lwb_nused); 759c5c6ffa0Smaybee if (dlen) { 760c5c6ffa0Smaybee ASSERT(itx->itx_wr_state == WR_NEED_COPY); 761c5c6ffa0Smaybee dbuf = lwb->lwb_buf + lwb->lwb_nused + reclen; 762c5c6ffa0Smaybee lr->lr_common.lrc_reclen += dlen; 763c5c6ffa0Smaybee } else { 764c5c6ffa0Smaybee ASSERT(itx->itx_wr_state == WR_INDIRECT); 765c5c6ffa0Smaybee dbuf = NULL; 766c5c6ffa0Smaybee } 767c5c6ffa0Smaybee error = zilog->zl_get_data( 768c5c6ffa0Smaybee itx->itx_private, lr, dbuf, lwb->lwb_zio); 769c5c6ffa0Smaybee if (error) { 770c5c6ffa0Smaybee ASSERT(error == ENOENT || error == EEXIST || 771c5c6ffa0Smaybee error == EALREADY); 772c5c6ffa0Smaybee return (lwb); 773c5c6ffa0Smaybee } 774c5c6ffa0Smaybee } 775104e2ed7Sperrin } 776c5c6ffa0Smaybee 777c5c6ffa0Smaybee lwb->lwb_nused += reclen + dlen; 778fa9e4066Sahrens lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg); 779fa9e4066Sahrens ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb)); 780fa9e4066Sahrens ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0); 781fa9e4066Sahrens 782fa9e4066Sahrens return (lwb); 783fa9e4066Sahrens } 784fa9e4066Sahrens 785fa9e4066Sahrens itx_t * 786fa9e4066Sahrens zil_itx_create(int txtype, size_t lrsize) 787fa9e4066Sahrens { 788fa9e4066Sahrens itx_t *itx; 789fa9e4066Sahrens 790b4d654b0Sperrin lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t); 791fa9e4066Sahrens 792fa9e4066Sahrens itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP); 793fa9e4066Sahrens itx->itx_lr.lrc_txtype = txtype; 794fa9e4066Sahrens itx->itx_lr.lrc_reclen = lrsize; 795fa9e4066Sahrens itx->itx_lr.lrc_seq = 0; /* defensive */ 796fa9e4066Sahrens 797fa9e4066Sahrens return (itx); 798fa9e4066Sahrens } 799fa9e4066Sahrens 800fa9e4066Sahrens uint64_t 801fa9e4066Sahrens zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx) 802fa9e4066Sahrens { 803fa9e4066Sahrens uint64_t seq; 804fa9e4066Sahrens 805fa9e4066Sahrens ASSERT(itx->itx_lr.lrc_seq == 0); 806fa9e4066Sahrens 807fa9e4066Sahrens mutex_enter(&zilog->zl_lock); 808fa9e4066Sahrens list_insert_tail(&zilog->zl_itx_list, itx); 809fa9e4066Sahrens zilog->zl_itx_list_sz += itx->itx_lr.lrc_reclen; 810fa9e4066Sahrens itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx); 811fa9e4066Sahrens itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq; 812fa9e4066Sahrens mutex_exit(&zilog->zl_lock); 813fa9e4066Sahrens 814fa9e4066Sahrens return (seq); 815fa9e4066Sahrens } 816fa9e4066Sahrens 817fa9e4066Sahrens /* 818fa9e4066Sahrens * Free up all in-memory intent log transactions that have now been synced. 819fa9e4066Sahrens */ 820fa9e4066Sahrens static void 821fa9e4066Sahrens zil_itx_clean(zilog_t *zilog) 822fa9e4066Sahrens { 823fa9e4066Sahrens uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa); 824fa9e4066Sahrens uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa); 825fa9e4066Sahrens itx_t *itx; 826fa9e4066Sahrens 827fa9e4066Sahrens mutex_enter(&zilog->zl_lock); 828*b19a79ecSperrin /* wait for a log writer to finish walking list */ 829*b19a79ecSperrin while (zilog->zl_writer) { 830*b19a79ecSperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 831*b19a79ecSperrin } 832*b19a79ecSperrin /* no need to set zl_writer as we never drop zl_lock */ 833fa9e4066Sahrens while ((itx = list_head(&zilog->zl_itx_list)) != NULL && 834fa9e4066Sahrens itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) { 835fa9e4066Sahrens list_remove(&zilog->zl_itx_list, itx); 836fa9e4066Sahrens zilog->zl_itx_list_sz -= itx->itx_lr.lrc_reclen; 837fa9e4066Sahrens kmem_free(itx, offsetof(itx_t, itx_lr) 838fa9e4066Sahrens + itx->itx_lr.lrc_reclen); 839fa9e4066Sahrens } 840fa9e4066Sahrens mutex_exit(&zilog->zl_lock); 841fa9e4066Sahrens } 842fa9e4066Sahrens 843*b19a79ecSperrin /* 844*b19a79ecSperrin * If there are in-memory intent log transactions then 845*b19a79ecSperrin * start up a taskq to free up any that have now been synced. 846*b19a79ecSperrin */ 847fa9e4066Sahrens void 848fa9e4066Sahrens zil_clean(zilog_t *zilog) 849fa9e4066Sahrens { 850fa9e4066Sahrens mutex_enter(&zilog->zl_lock); 851fa9e4066Sahrens if (list_head(&zilog->zl_itx_list) != NULL) 852fa9e4066Sahrens (void) taskq_dispatch(zilog->zl_clean_taskq, 853fa9e4066Sahrens (void (*)(void *))zil_itx_clean, zilog, TQ_NOSLEEP); 854fa9e4066Sahrens mutex_exit(&zilog->zl_lock); 855fa9e4066Sahrens } 856fa9e4066Sahrens 857fa9e4066Sahrens void 858*b19a79ecSperrin zil_commit_writer(zilog_t *zilog, uint64_t seq, uint64_t foid) 859fa9e4066Sahrens { 860fa9e4066Sahrens uint64_t txg; 861fa9e4066Sahrens uint64_t reclen; 862*b19a79ecSperrin itx_t *itx, *itx_next = (itx_t *)-1; 863fa9e4066Sahrens lwb_t *lwb; 864fa9e4066Sahrens spa_t *spa; 865fa9e4066Sahrens 866fa9e4066Sahrens zilog->zl_writer = B_TRUE; 867*b19a79ecSperrin zilog->zl_root_zio = NULL; 868*b19a79ecSperrin spa = zilog->zl_spa; 869fa9e4066Sahrens 870fa9e4066Sahrens if (zilog->zl_suspend) { 871fa9e4066Sahrens lwb = NULL; 872fa9e4066Sahrens } else { 873fa9e4066Sahrens lwb = list_tail(&zilog->zl_lwb_list); 874fa9e4066Sahrens if (lwb == NULL) { 875*b19a79ecSperrin /* 876*b19a79ecSperrin * Return if there's nothing to flush before we 877*b19a79ecSperrin * dirty the fs by calling zil_create() 878*b19a79ecSperrin */ 879*b19a79ecSperrin if (list_is_empty(&zilog->zl_itx_list)) { 880*b19a79ecSperrin /* wake up others waiting to start a write */ 881*b19a79ecSperrin zilog->zl_writer = B_FALSE; 882*b19a79ecSperrin cv_broadcast(&zilog->zl_cv_writer); 883*b19a79ecSperrin mutex_exit(&zilog->zl_lock); 884*b19a79ecSperrin return; 885*b19a79ecSperrin } 886*b19a79ecSperrin 887fa9e4066Sahrens mutex_exit(&zilog->zl_lock); 888fa9e4066Sahrens zil_create(zilog); 889fa9e4066Sahrens mutex_enter(&zilog->zl_lock); 890fa9e4066Sahrens lwb = list_tail(&zilog->zl_lwb_list); 891fa9e4066Sahrens } 892fa9e4066Sahrens } 893fa9e4066Sahrens 894fa9e4066Sahrens /* 895fa9e4066Sahrens * Loop through in-memory log transactions filling log blocks, 896fa9e4066Sahrens * until we reach the given sequence number and there's no more 897fa9e4066Sahrens * room in the write buffer. 898fa9e4066Sahrens */ 899*b19a79ecSperrin DTRACE_PROBE1(zil__cw1, zilog_t *, zilog); 900fa9e4066Sahrens for (;;) { 901*b19a79ecSperrin /* 902*b19a79ecSperrin * Find the next itx to push: 903*b19a79ecSperrin * Push all transactions related to specified foid and all 904*b19a79ecSperrin * other transactions except TX_WRITE, TX_TRUNCATE, 905*b19a79ecSperrin * TX_SETATTR and TX_ACL for all other files. 906*b19a79ecSperrin */ 907*b19a79ecSperrin if (itx_next != (itx_t *)-1) 908*b19a79ecSperrin itx = itx_next; 909*b19a79ecSperrin else 910*b19a79ecSperrin itx = list_head(&zilog->zl_itx_list); 911*b19a79ecSperrin for (; itx != NULL; itx = list_next(&zilog->zl_itx_list, itx)) { 912*b19a79ecSperrin if (foid == 0) /* push all foids? */ 913*b19a79ecSperrin break; 914*b19a79ecSperrin switch (itx->itx_lr.lrc_txtype) { 915*b19a79ecSperrin case TX_SETATTR: 916*b19a79ecSperrin case TX_WRITE: 917*b19a79ecSperrin case TX_TRUNCATE: 918*b19a79ecSperrin case TX_ACL: 919*b19a79ecSperrin /* lr_foid is same offset for these records */ 920*b19a79ecSperrin if (((lr_write_t *)&itx->itx_lr)->lr_foid 921*b19a79ecSperrin != foid) { 922*b19a79ecSperrin continue; /* skip this record */ 923*b19a79ecSperrin } 924*b19a79ecSperrin } 925*b19a79ecSperrin break; 926*b19a79ecSperrin } 927fa9e4066Sahrens if (itx == NULL) 928fa9e4066Sahrens break; 929fa9e4066Sahrens 930fa9e4066Sahrens reclen = itx->itx_lr.lrc_reclen; 931fa9e4066Sahrens if ((itx->itx_lr.lrc_seq > seq) && 932*b19a79ecSperrin ((lwb == NULL) || (lwb->lwb_nused == 0) || 933*b19a79ecSperrin (lwb->lwb_nused + reclen > ZIL_BLK_DATA_SZ(lwb)))) 934fa9e4066Sahrens break; 935fa9e4066Sahrens 936*b19a79ecSperrin /* 937*b19a79ecSperrin * Save the next pointer. Even though we soon drop 938*b19a79ecSperrin * zl_lock all threads that may change the list 939*b19a79ecSperrin * (another writer or zil_itx_clean) can't do so until 940*b19a79ecSperrin * they have zl_writer. 941*b19a79ecSperrin */ 942*b19a79ecSperrin itx_next = list_next(&zilog->zl_itx_list, itx); 943fa9e4066Sahrens list_remove(&zilog->zl_itx_list, itx); 944fa9e4066Sahrens txg = itx->itx_lr.lrc_txg; 945fa9e4066Sahrens ASSERT(txg); 946fa9e4066Sahrens 947fa9e4066Sahrens mutex_exit(&zilog->zl_lock); 948fa9e4066Sahrens if (txg > spa_last_synced_txg(spa) || 949fa9e4066Sahrens txg > spa_freeze_txg(spa)) 950fa9e4066Sahrens lwb = zil_lwb_commit(zilog, itx, lwb); 951fa9e4066Sahrens kmem_free(itx, offsetof(itx_t, itx_lr) 952fa9e4066Sahrens + itx->itx_lr.lrc_reclen); 953fa9e4066Sahrens mutex_enter(&zilog->zl_lock); 954fa9e4066Sahrens zilog->zl_itx_list_sz -= reclen; 955fa9e4066Sahrens } 956*b19a79ecSperrin DTRACE_PROBE1(zil__cw2, zilog_t *, zilog); 957fa9e4066Sahrens mutex_exit(&zilog->zl_lock); 958fa9e4066Sahrens 959fa9e4066Sahrens /* write the last block out */ 960fa9e4066Sahrens if (lwb != NULL && lwb->lwb_nused != 0) 961fa9e4066Sahrens lwb = zil_lwb_write_start(zilog, lwb); 962fa9e4066Sahrens 96322ac5be4Sperrin zilog->zl_prev_used = zilog->zl_cur_used; 96422ac5be4Sperrin zilog->zl_cur_used = 0; 965fa9e4066Sahrens 966fa9e4066Sahrens /* 967*b19a79ecSperrin * Wait if necessary for the log blocks to be on stable storage. 968fa9e4066Sahrens */ 969*b19a79ecSperrin mutex_enter(&zilog->zl_lock); 970*b19a79ecSperrin if (zilog->zl_root_zio) { 971*b19a79ecSperrin mutex_exit(&zilog->zl_lock); 972*b19a79ecSperrin DTRACE_PROBE1(zil__cw3, zilog_t *, zilog); 973*b19a79ecSperrin (void) zio_wait(zilog->zl_root_zio); 974*b19a79ecSperrin DTRACE_PROBE1(zil__cw4, zilog_t *, zilog); 975*b19a79ecSperrin mutex_enter(&zilog->zl_lock); 976*b19a79ecSperrin zil_flush_vdevs(zilog); 977fa9e4066Sahrens } 97822ac5be4Sperrin 979fa9e4066Sahrens if (zilog->zl_log_error || lwb == NULL) { 980fa9e4066Sahrens zilog->zl_log_error = 0; 981fa9e4066Sahrens mutex_exit(&zilog->zl_lock); 982fa9e4066Sahrens txg_wait_synced(zilog->zl_dmu_pool, 0); 983fa9e4066Sahrens mutex_enter(&zilog->zl_lock); 984fa9e4066Sahrens } 98522ac5be4Sperrin /* wake up others waiting to start a write */ 98622ac5be4Sperrin zilog->zl_writer = B_FALSE; 987*b19a79ecSperrin cv_broadcast(&zilog->zl_cv_writer); 988fa9e4066Sahrens mutex_exit(&zilog->zl_lock); 989*b19a79ecSperrin } 990*b19a79ecSperrin 991*b19a79ecSperrin /* 992*b19a79ecSperrin * Push zfs transactions to stable storage up to the supplied sequence number. 993*b19a79ecSperrin * If foid is 0 push out all transactions, otherwise push only those 994*b19a79ecSperrin * for that file or might have been used to create that file. 995*b19a79ecSperrin */ 996*b19a79ecSperrin void 997*b19a79ecSperrin zil_commit(zilog_t *zilog, uint64_t seq, uint64_t foid) 998*b19a79ecSperrin { 999*b19a79ecSperrin if (zilog == NULL || seq == 0) 1000*b19a79ecSperrin return; 1001*b19a79ecSperrin 1002*b19a79ecSperrin mutex_enter(&zilog->zl_lock); 1003*b19a79ecSperrin 1004*b19a79ecSperrin seq = MIN(seq, zilog->zl_itx_seq); /* cap seq at largest itx seq */ 1005*b19a79ecSperrin 1006*b19a79ecSperrin while (zilog->zl_writer) 1007*b19a79ecSperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 1008*b19a79ecSperrin zil_commit_writer(zilog, seq, foid); /* drops zl_lock */ 1009fa9e4066Sahrens } 1010fa9e4066Sahrens 1011fa9e4066Sahrens /* 1012fa9e4066Sahrens * Called in syncing context to free committed log blocks and update log header. 1013fa9e4066Sahrens */ 1014fa9e4066Sahrens void 1015fa9e4066Sahrens zil_sync(zilog_t *zilog, dmu_tx_t *tx) 1016fa9e4066Sahrens { 1017d80c45e0Sbonwick zil_header_t *zh = zil_header_in_syncing_context(zilog); 1018fa9e4066Sahrens uint64_t txg = dmu_tx_get_txg(tx); 1019fa9e4066Sahrens spa_t *spa = zilog->zl_spa; 1020fa9e4066Sahrens lwb_t *lwb; 1021fa9e4066Sahrens 1022d80c45e0Sbonwick mutex_enter(&zilog->zl_lock); 1023d80c45e0Sbonwick 1024fa9e4066Sahrens ASSERT(zilog->zl_stop_sync == 0); 1025fa9e4066Sahrens 1026d80c45e0Sbonwick zh->zh_replay_seq = zilog->zl_replay_seq[txg & TXG_MASK]; 1027fa9e4066Sahrens 1028fa9e4066Sahrens if (zilog->zl_destroy_txg == txg) { 1029d80c45e0Sbonwick blkptr_t blk = zh->zh_log; 1030d80c45e0Sbonwick 1031d80c45e0Sbonwick ASSERT(list_head(&zilog->zl_lwb_list) == NULL); 1032d80c45e0Sbonwick ASSERT(spa_sync_pass(spa) == 1); 1033d80c45e0Sbonwick 1034d80c45e0Sbonwick bzero(zh, sizeof (zil_header_t)); 1035fa9e4066Sahrens bzero(zilog->zl_replay_seq, sizeof (zilog->zl_replay_seq)); 1036d80c45e0Sbonwick 1037d80c45e0Sbonwick if (zilog->zl_keep_first) { 1038d80c45e0Sbonwick /* 1039d80c45e0Sbonwick * If this block was part of log chain that couldn't 1040d80c45e0Sbonwick * be claimed because a device was missing during 1041d80c45e0Sbonwick * zil_claim(), but that device later returns, 1042d80c45e0Sbonwick * then this block could erroneously appear valid. 1043d80c45e0Sbonwick * To guard against this, assign a new GUID to the new 1044d80c45e0Sbonwick * log chain so it doesn't matter what blk points to. 1045d80c45e0Sbonwick */ 1046d80c45e0Sbonwick zil_init_log_chain(zilog, &blk); 1047d80c45e0Sbonwick zh->zh_log = blk; 1048d80c45e0Sbonwick } 1049fa9e4066Sahrens } 1050fa9e4066Sahrens 1051fa9e4066Sahrens for (;;) { 1052fa9e4066Sahrens lwb = list_head(&zilog->zl_lwb_list); 1053fa9e4066Sahrens if (lwb == NULL) { 1054fa9e4066Sahrens mutex_exit(&zilog->zl_lock); 1055fa9e4066Sahrens return; 1056fa9e4066Sahrens } 1057*b19a79ecSperrin zh->zh_log = lwb->lwb_blk; 1058fa9e4066Sahrens if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg) 1059fa9e4066Sahrens break; 1060fa9e4066Sahrens list_remove(&zilog->zl_lwb_list, lwb); 1061fa9e4066Sahrens zio_free_blk(spa, &lwb->lwb_blk, txg); 1062fa9e4066Sahrens kmem_cache_free(zil_lwb_cache, lwb); 1063fa9e4066Sahrens } 1064fa9e4066Sahrens mutex_exit(&zilog->zl_lock); 1065fa9e4066Sahrens } 1066fa9e4066Sahrens 1067fa9e4066Sahrens void 1068fa9e4066Sahrens zil_init(void) 1069fa9e4066Sahrens { 1070fa9e4066Sahrens zil_lwb_cache = kmem_cache_create("zil_lwb_cache", 1071fa9e4066Sahrens sizeof (struct lwb), NULL, NULL, NULL, NULL, NULL, NULL, 0); 1072fa9e4066Sahrens } 1073fa9e4066Sahrens 1074fa9e4066Sahrens void 1075fa9e4066Sahrens zil_fini(void) 1076fa9e4066Sahrens { 1077fa9e4066Sahrens kmem_cache_destroy(zil_lwb_cache); 1078fa9e4066Sahrens } 1079fa9e4066Sahrens 1080fa9e4066Sahrens zilog_t * 1081fa9e4066Sahrens zil_alloc(objset_t *os, zil_header_t *zh_phys) 1082fa9e4066Sahrens { 1083fa9e4066Sahrens zilog_t *zilog; 1084fa9e4066Sahrens 1085fa9e4066Sahrens zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP); 1086fa9e4066Sahrens 1087fa9e4066Sahrens zilog->zl_header = zh_phys; 1088fa9e4066Sahrens zilog->zl_os = os; 1089fa9e4066Sahrens zilog->zl_spa = dmu_objset_spa(os); 1090fa9e4066Sahrens zilog->zl_dmu_pool = dmu_objset_pool(os); 1091d80c45e0Sbonwick zilog->zl_destroy_txg = TXG_INITIAL - 1; 1092fa9e4066Sahrens 1093fa9e4066Sahrens list_create(&zilog->zl_itx_list, sizeof (itx_t), 1094fa9e4066Sahrens offsetof(itx_t, itx_node)); 1095fa9e4066Sahrens 1096fa9e4066Sahrens list_create(&zilog->zl_lwb_list, sizeof (lwb_t), 1097fa9e4066Sahrens offsetof(lwb_t, lwb_node)); 1098fa9e4066Sahrens 1099fa9e4066Sahrens list_create(&zilog->zl_vdev_list, sizeof (zil_vdev_t), 1100fa9e4066Sahrens offsetof(zil_vdev_t, vdev_seq_node)); 1101fa9e4066Sahrens 1102fa9e4066Sahrens return (zilog); 1103fa9e4066Sahrens } 1104fa9e4066Sahrens 1105fa9e4066Sahrens void 1106fa9e4066Sahrens zil_free(zilog_t *zilog) 1107fa9e4066Sahrens { 1108fa9e4066Sahrens lwb_t *lwb; 1109fa9e4066Sahrens zil_vdev_t *zv; 1110fa9e4066Sahrens 1111fa9e4066Sahrens zilog->zl_stop_sync = 1; 1112fa9e4066Sahrens 1113fa9e4066Sahrens while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 1114fa9e4066Sahrens list_remove(&zilog->zl_lwb_list, lwb); 1115fa9e4066Sahrens if (lwb->lwb_buf != NULL) 1116fa9e4066Sahrens zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 1117fa9e4066Sahrens kmem_cache_free(zil_lwb_cache, lwb); 1118fa9e4066Sahrens } 1119fa9e4066Sahrens list_destroy(&zilog->zl_lwb_list); 1120fa9e4066Sahrens 1121fa9e4066Sahrens while ((zv = list_head(&zilog->zl_vdev_list)) != NULL) { 1122fa9e4066Sahrens list_remove(&zilog->zl_vdev_list, zv); 1123fa9e4066Sahrens kmem_free(zv, sizeof (zil_vdev_t)); 1124fa9e4066Sahrens } 1125fa9e4066Sahrens list_destroy(&zilog->zl_vdev_list); 1126fa9e4066Sahrens 1127fa9e4066Sahrens ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1128fa9e4066Sahrens list_destroy(&zilog->zl_itx_list); 1129fa9e4066Sahrens 1130fa9e4066Sahrens kmem_free(zilog, sizeof (zilog_t)); 1131fa9e4066Sahrens } 1132fa9e4066Sahrens 113313f5297eSperrin /* 1134436b2950Sperrin * return true if the initial log block is not valid 113513f5297eSperrin */ 113613f5297eSperrin static int 113713f5297eSperrin zil_empty(zilog_t *zilog) 113813f5297eSperrin { 1139d80c45e0Sbonwick const zil_header_t *zh = zilog->zl_header; 1140d80c45e0Sbonwick arc_buf_t *abuf = NULL; 114113f5297eSperrin 1142d80c45e0Sbonwick if (BP_IS_HOLE(&zh->zh_log)) 114313f5297eSperrin return (1); 114413f5297eSperrin 1145d80c45e0Sbonwick if (zil_read_log_block(zilog, &zh->zh_log, &abuf) != 0) 1146d80c45e0Sbonwick return (1); 1147d80c45e0Sbonwick 1148d80c45e0Sbonwick VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 1149d80c45e0Sbonwick return (0); 115013f5297eSperrin } 115113f5297eSperrin 1152fa9e4066Sahrens /* 1153fa9e4066Sahrens * Open an intent log. 1154fa9e4066Sahrens */ 1155fa9e4066Sahrens zilog_t * 1156fa9e4066Sahrens zil_open(objset_t *os, zil_get_data_t *get_data) 1157fa9e4066Sahrens { 1158fa9e4066Sahrens zilog_t *zilog = dmu_objset_zil(os); 1159fa9e4066Sahrens 1160fa9e4066Sahrens zilog->zl_get_data = get_data; 1161fa9e4066Sahrens zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri, 1162fa9e4066Sahrens 2, 2, TASKQ_PREPOPULATE); 1163fa9e4066Sahrens 1164fa9e4066Sahrens return (zilog); 1165fa9e4066Sahrens } 1166fa9e4066Sahrens 1167fa9e4066Sahrens /* 1168fa9e4066Sahrens * Close an intent log. 1169fa9e4066Sahrens */ 1170fa9e4066Sahrens void 1171fa9e4066Sahrens zil_close(zilog_t *zilog) 1172fa9e4066Sahrens { 1173d80c45e0Sbonwick /* 1174d80c45e0Sbonwick * If the log isn't already committed, mark the objset dirty 1175d80c45e0Sbonwick * (so zil_sync() will be called) and wait for that txg to sync. 1176d80c45e0Sbonwick */ 1177d80c45e0Sbonwick if (!zil_is_committed(zilog)) { 1178d80c45e0Sbonwick uint64_t txg; 1179d80c45e0Sbonwick dmu_tx_t *tx = dmu_tx_create(zilog->zl_os); 1180d80c45e0Sbonwick (void) dmu_tx_assign(tx, TXG_WAIT); 1181d80c45e0Sbonwick dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 1182d80c45e0Sbonwick txg = dmu_tx_get_txg(tx); 1183d80c45e0Sbonwick dmu_tx_commit(tx); 1184d80c45e0Sbonwick txg_wait_synced(zilog->zl_dmu_pool, txg); 1185d80c45e0Sbonwick } 1186d80c45e0Sbonwick 1187fa9e4066Sahrens taskq_destroy(zilog->zl_clean_taskq); 1188fa9e4066Sahrens zilog->zl_clean_taskq = NULL; 1189fa9e4066Sahrens zilog->zl_get_data = NULL; 1190fa9e4066Sahrens 1191fa9e4066Sahrens zil_itx_clean(zilog); 1192fa9e4066Sahrens ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1193fa9e4066Sahrens } 1194fa9e4066Sahrens 1195fa9e4066Sahrens /* 1196fa9e4066Sahrens * Suspend an intent log. While in suspended mode, we still honor 1197fa9e4066Sahrens * synchronous semantics, but we rely on txg_wait_synced() to do it. 1198fa9e4066Sahrens * We suspend the log briefly when taking a snapshot so that the snapshot 1199fa9e4066Sahrens * contains all the data it's supposed to, and has an empty intent log. 1200fa9e4066Sahrens */ 1201fa9e4066Sahrens int 1202fa9e4066Sahrens zil_suspend(zilog_t *zilog) 1203fa9e4066Sahrens { 1204d80c45e0Sbonwick const zil_header_t *zh = zilog->zl_header; 1205fa9e4066Sahrens 1206fa9e4066Sahrens mutex_enter(&zilog->zl_lock); 1207d80c45e0Sbonwick if (zh->zh_claim_txg != 0) { /* unplayed log */ 1208fa9e4066Sahrens mutex_exit(&zilog->zl_lock); 1209fa9e4066Sahrens return (EBUSY); 1210fa9e4066Sahrens } 1211d80c45e0Sbonwick if (zilog->zl_suspend++ != 0) { 1212d80c45e0Sbonwick /* 1213d80c45e0Sbonwick * Someone else already began a suspend. 1214d80c45e0Sbonwick * Just wait for them to finish. 1215d80c45e0Sbonwick */ 1216d80c45e0Sbonwick while (zilog->zl_suspending) 1217d80c45e0Sbonwick cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock); 1218d80c45e0Sbonwick ASSERT(BP_IS_HOLE(&zh->zh_log)); 1219d80c45e0Sbonwick mutex_exit(&zilog->zl_lock); 1220d80c45e0Sbonwick return (0); 1221d80c45e0Sbonwick } 1222d80c45e0Sbonwick zilog->zl_suspending = B_TRUE; 1223fa9e4066Sahrens mutex_exit(&zilog->zl_lock); 1224fa9e4066Sahrens 1225*b19a79ecSperrin zil_commit(zilog, UINT64_MAX, 0); 1226fa9e4066Sahrens 1227*b19a79ecSperrin /* 1228*b19a79ecSperrin * Wait for any in-flight log writes to complete. 1229*b19a79ecSperrin */ 1230fa9e4066Sahrens mutex_enter(&zilog->zl_lock); 1231*b19a79ecSperrin while (zilog->zl_writer) 1232*b19a79ecSperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 1233fa9e4066Sahrens mutex_exit(&zilog->zl_lock); 1234fa9e4066Sahrens 1235d80c45e0Sbonwick zil_destroy(zilog, B_FALSE); 1236d80c45e0Sbonwick 1237d80c45e0Sbonwick mutex_enter(&zilog->zl_lock); 1238d80c45e0Sbonwick ASSERT(BP_IS_HOLE(&zh->zh_log)); 1239d80c45e0Sbonwick zilog->zl_suspending = B_FALSE; 1240d80c45e0Sbonwick cv_broadcast(&zilog->zl_cv_suspend); 1241d80c45e0Sbonwick mutex_exit(&zilog->zl_lock); 1242fa9e4066Sahrens 1243fa9e4066Sahrens return (0); 1244fa9e4066Sahrens } 1245fa9e4066Sahrens 1246fa9e4066Sahrens void 1247fa9e4066Sahrens zil_resume(zilog_t *zilog) 1248fa9e4066Sahrens { 1249fa9e4066Sahrens mutex_enter(&zilog->zl_lock); 1250fa9e4066Sahrens ASSERT(zilog->zl_suspend != 0); 1251fa9e4066Sahrens zilog->zl_suspend--; 1252fa9e4066Sahrens mutex_exit(&zilog->zl_lock); 1253fa9e4066Sahrens } 1254fa9e4066Sahrens 1255fa9e4066Sahrens typedef struct zil_replay_arg { 1256fa9e4066Sahrens objset_t *zr_os; 1257fa9e4066Sahrens zil_replay_func_t **zr_replay; 1258fa9e4066Sahrens void *zr_arg; 1259fa9e4066Sahrens void (*zr_rm_sync)(void *arg); 1260fa9e4066Sahrens uint64_t *zr_txgp; 1261fa9e4066Sahrens boolean_t zr_byteswap; 1262fa9e4066Sahrens char *zr_lrbuf; 1263fa9e4066Sahrens } zil_replay_arg_t; 1264fa9e4066Sahrens 1265fa9e4066Sahrens static void 1266fa9e4066Sahrens zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg) 1267fa9e4066Sahrens { 1268fa9e4066Sahrens zil_replay_arg_t *zr = zra; 1269d80c45e0Sbonwick const zil_header_t *zh = zilog->zl_header; 1270fa9e4066Sahrens uint64_t reclen = lr->lrc_reclen; 1271fa9e4066Sahrens uint64_t txtype = lr->lrc_txtype; 1272fa9e4066Sahrens int pass, error; 1273fa9e4066Sahrens 1274fa9e4066Sahrens if (zilog->zl_stop_replay) 1275fa9e4066Sahrens return; 1276fa9e4066Sahrens 1277fa9e4066Sahrens if (lr->lrc_txg < claim_txg) /* already committed */ 1278fa9e4066Sahrens return; 1279fa9e4066Sahrens 1280fa9e4066Sahrens if (lr->lrc_seq <= zh->zh_replay_seq) /* already replayed */ 1281fa9e4066Sahrens return; 1282fa9e4066Sahrens 1283fa9e4066Sahrens /* 1284fa9e4066Sahrens * Make a copy of the data so we can revise and extend it. 1285fa9e4066Sahrens */ 1286fa9e4066Sahrens bcopy(lr, zr->zr_lrbuf, reclen); 1287fa9e4066Sahrens 1288fa9e4066Sahrens /* 1289fa9e4066Sahrens * The log block containing this lr may have been byteswapped 1290fa9e4066Sahrens * so that we can easily examine common fields like lrc_txtype. 1291fa9e4066Sahrens * However, the log is a mix of different data types, and only the 1292fa9e4066Sahrens * replay vectors know how to byteswap their records. Therefore, if 1293fa9e4066Sahrens * the lr was byteswapped, undo it before invoking the replay vector. 1294fa9e4066Sahrens */ 1295fa9e4066Sahrens if (zr->zr_byteswap) 1296fa9e4066Sahrens byteswap_uint64_array(zr->zr_lrbuf, reclen); 1297fa9e4066Sahrens 1298fa9e4066Sahrens /* 1299fa9e4066Sahrens * If this is a TX_WRITE with a blkptr, suck in the data. 1300fa9e4066Sahrens */ 1301fa9e4066Sahrens if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) { 1302fa9e4066Sahrens lr_write_t *lrw = (lr_write_t *)lr; 1303fa9e4066Sahrens blkptr_t *wbp = &lrw->lr_blkptr; 1304fa9e4066Sahrens uint64_t wlen = lrw->lr_length; 1305fa9e4066Sahrens char *wbuf = zr->zr_lrbuf + reclen; 1306fa9e4066Sahrens 1307fa9e4066Sahrens if (BP_IS_HOLE(wbp)) { /* compressed to a hole */ 1308fa9e4066Sahrens bzero(wbuf, wlen); 1309fa9e4066Sahrens } else { 1310fa9e4066Sahrens /* 1311fa9e4066Sahrens * A subsequent write may have overwritten this block, 1312fa9e4066Sahrens * in which case wbp may have been been freed and 1313fa9e4066Sahrens * reallocated, and our read of wbp may fail with a 1314fa9e4066Sahrens * checksum error. We can safely ignore this because 1315fa9e4066Sahrens * the later write will provide the correct data. 1316fa9e4066Sahrens */ 1317ea8dc4b6Seschrock zbookmark_t zb; 1318ea8dc4b6Seschrock 1319ea8dc4b6Seschrock zb.zb_objset = dmu_objset_id(zilog->zl_os); 1320ea8dc4b6Seschrock zb.zb_object = lrw->lr_foid; 1321ea8dc4b6Seschrock zb.zb_level = -1; 1322ea8dc4b6Seschrock zb.zb_blkid = lrw->lr_offset / BP_GET_LSIZE(wbp); 1323ea8dc4b6Seschrock 1324fa9e4066Sahrens (void) zio_wait(zio_read(NULL, zilog->zl_spa, 1325fa9e4066Sahrens wbp, wbuf, BP_GET_LSIZE(wbp), NULL, NULL, 1326fa9e4066Sahrens ZIO_PRIORITY_SYNC_READ, 1327ea8dc4b6Seschrock ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb)); 1328fa9e4066Sahrens (void) memmove(wbuf, wbuf + lrw->lr_blkoff, wlen); 1329fa9e4066Sahrens } 1330fa9e4066Sahrens } 1331fa9e4066Sahrens 1332fa9e4066Sahrens /* 1333fa9e4066Sahrens * We must now do two things atomically: replay this log record, 1334fa9e4066Sahrens * and update the log header to reflect the fact that we did so. 1335fa9e4066Sahrens * We use the DMU's ability to assign into a specific txg to do this. 1336fa9e4066Sahrens */ 1337fa9e4066Sahrens for (pass = 1; /* CONSTANTCONDITION */; pass++) { 1338fa9e4066Sahrens uint64_t replay_txg; 1339fa9e4066Sahrens dmu_tx_t *replay_tx; 1340fa9e4066Sahrens 1341fa9e4066Sahrens replay_tx = dmu_tx_create(zr->zr_os); 1342fa9e4066Sahrens error = dmu_tx_assign(replay_tx, TXG_WAIT); 1343fa9e4066Sahrens if (error) { 1344fa9e4066Sahrens dmu_tx_abort(replay_tx); 1345fa9e4066Sahrens break; 1346fa9e4066Sahrens } 1347fa9e4066Sahrens 1348fa9e4066Sahrens replay_txg = dmu_tx_get_txg(replay_tx); 1349fa9e4066Sahrens 1350fa9e4066Sahrens if (txtype == 0 || txtype >= TX_MAX_TYPE) { 1351fa9e4066Sahrens error = EINVAL; 1352fa9e4066Sahrens } else { 1353fa9e4066Sahrens /* 1354fa9e4066Sahrens * On the first pass, arrange for the replay vector 1355fa9e4066Sahrens * to fail its dmu_tx_assign(). That's the only way 1356fa9e4066Sahrens * to ensure that those code paths remain well tested. 1357fa9e4066Sahrens */ 1358fa9e4066Sahrens *zr->zr_txgp = replay_txg - (pass == 1); 1359fa9e4066Sahrens error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lrbuf, 1360fa9e4066Sahrens zr->zr_byteswap); 1361fa9e4066Sahrens *zr->zr_txgp = TXG_NOWAIT; 1362fa9e4066Sahrens } 1363fa9e4066Sahrens 1364fa9e4066Sahrens if (error == 0) { 1365fa9e4066Sahrens dsl_dataset_dirty(dmu_objset_ds(zr->zr_os), replay_tx); 1366fa9e4066Sahrens zilog->zl_replay_seq[replay_txg & TXG_MASK] = 1367fa9e4066Sahrens lr->lrc_seq; 1368fa9e4066Sahrens } 1369fa9e4066Sahrens 1370fa9e4066Sahrens dmu_tx_commit(replay_tx); 1371fa9e4066Sahrens 1372fa9e4066Sahrens if (error != ERESTART) 1373fa9e4066Sahrens break; 1374fa9e4066Sahrens 1375fa9e4066Sahrens if (pass != 1) 1376fa9e4066Sahrens txg_wait_open(spa_get_dsl(zilog->zl_spa), 1377fa9e4066Sahrens replay_txg + 1); 1378fa9e4066Sahrens 1379fa9e4066Sahrens dprintf("pass %d, retrying\n", pass); 1380fa9e4066Sahrens } 1381fa9e4066Sahrens 1382fa9e4066Sahrens if (error) { 1383fa9e4066Sahrens char *name = kmem_alloc(MAXNAMELEN, KM_SLEEP); 1384fa9e4066Sahrens dmu_objset_name(zr->zr_os, name); 1385fa9e4066Sahrens cmn_err(CE_WARN, "ZFS replay transaction error %d, " 1386fa9e4066Sahrens "dataset %s, seq 0x%llx, txtype %llu\n", 1387fa9e4066Sahrens error, name, 1388fa9e4066Sahrens (u_longlong_t)lr->lrc_seq, (u_longlong_t)txtype); 1389fa9e4066Sahrens zilog->zl_stop_replay = 1; 1390fa9e4066Sahrens kmem_free(name, MAXNAMELEN); 1391fa9e4066Sahrens } 1392fa9e4066Sahrens 1393fa9e4066Sahrens /* 1394fa9e4066Sahrens * The DMU's dnode layer doesn't see removes until the txg commits, 1395fa9e4066Sahrens * so a subsequent claim can spuriously fail with EEXIST. 1396fa9e4066Sahrens * To prevent this, if we might have removed an object, 1397fa9e4066Sahrens * wait for the delete thread to delete it, and then 1398fa9e4066Sahrens * wait for the transaction group to sync. 1399fa9e4066Sahrens */ 1400fa9e4066Sahrens if (txtype == TX_REMOVE || txtype == TX_RMDIR || txtype == TX_RENAME) { 1401fa9e4066Sahrens if (zr->zr_rm_sync != NULL) 1402fa9e4066Sahrens zr->zr_rm_sync(zr->zr_arg); 1403fa9e4066Sahrens txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0); 1404fa9e4066Sahrens } 1405fa9e4066Sahrens } 1406fa9e4066Sahrens 1407fa9e4066Sahrens /* 140813f5297eSperrin * If this dataset has a non-empty intent log, replay it and destroy it. 1409fa9e4066Sahrens */ 1410fa9e4066Sahrens void 1411fa9e4066Sahrens zil_replay(objset_t *os, void *arg, uint64_t *txgp, 1412fa9e4066Sahrens zil_replay_func_t *replay_func[TX_MAX_TYPE], void (*rm_sync)(void *arg)) 1413fa9e4066Sahrens { 1414fa9e4066Sahrens zilog_t *zilog = dmu_objset_zil(os); 1415d80c45e0Sbonwick const zil_header_t *zh = zilog->zl_header; 1416d80c45e0Sbonwick zil_replay_arg_t zr; 141713f5297eSperrin 141813f5297eSperrin if (zil_empty(zilog)) { 1419d80c45e0Sbonwick zil_destroy(zilog, B_TRUE); 142013f5297eSperrin return; 142113f5297eSperrin } 1422fa9e4066Sahrens 1423fa9e4066Sahrens zr.zr_os = os; 1424fa9e4066Sahrens zr.zr_replay = replay_func; 1425fa9e4066Sahrens zr.zr_arg = arg; 1426fa9e4066Sahrens zr.zr_rm_sync = rm_sync; 1427fa9e4066Sahrens zr.zr_txgp = txgp; 1428d80c45e0Sbonwick zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log); 1429fa9e4066Sahrens zr.zr_lrbuf = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP); 1430fa9e4066Sahrens 1431fa9e4066Sahrens /* 1432fa9e4066Sahrens * Wait for in-progress removes to sync before starting replay. 1433fa9e4066Sahrens */ 1434fa9e4066Sahrens if (rm_sync != NULL) 1435fa9e4066Sahrens rm_sync(arg); 1436fa9e4066Sahrens txg_wait_synced(zilog->zl_dmu_pool, 0); 1437fa9e4066Sahrens 1438fa9e4066Sahrens zilog->zl_stop_replay = 0; 1439d80c45e0Sbonwick (void) zil_parse(zilog, NULL, zil_replay_log_record, &zr, 1440d80c45e0Sbonwick zh->zh_claim_txg); 1441fa9e4066Sahrens kmem_free(zr.zr_lrbuf, 2 * SPA_MAXBLOCKSIZE); 1442fa9e4066Sahrens 1443d80c45e0Sbonwick zil_destroy(zilog, B_FALSE); 1444fa9e4066Sahrens } 1445436b2950Sperrin 1446436b2950Sperrin /* 1447436b2950Sperrin * Report whether all transactions are committed 1448436b2950Sperrin */ 1449436b2950Sperrin int 1450436b2950Sperrin zil_is_committed(zilog_t *zilog) 1451436b2950Sperrin { 1452436b2950Sperrin lwb_t *lwb; 1453*b19a79ecSperrin int ret; 1454*b19a79ecSperrin 1455*b19a79ecSperrin mutex_enter(&zilog->zl_lock); 1456*b19a79ecSperrin while (zilog->zl_writer) 1457*b19a79ecSperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 1458*b19a79ecSperrin 1459*b19a79ecSperrin /* recent unpushed intent log transactions? */ 1460*b19a79ecSperrin if (!list_is_empty(&zilog->zl_itx_list)) { 1461*b19a79ecSperrin ret = B_FALSE; 1462*b19a79ecSperrin goto out; 1463*b19a79ecSperrin } 1464436b2950Sperrin 1465*b19a79ecSperrin /* intent log never used? */ 1466*b19a79ecSperrin lwb = list_head(&zilog->zl_lwb_list); 1467*b19a79ecSperrin if (lwb == NULL) { 1468*b19a79ecSperrin ret = B_TRUE; 1469*b19a79ecSperrin goto out; 1470*b19a79ecSperrin } 1471436b2950Sperrin 1472436b2950Sperrin /* 1473*b19a79ecSperrin * more than 1 log buffer means zil_sync() hasn't yet freed 1474*b19a79ecSperrin * entries after a txg has committed 1475436b2950Sperrin */ 1476*b19a79ecSperrin if (list_next(&zilog->zl_lwb_list, lwb)) { 1477*b19a79ecSperrin ret = B_FALSE; 1478*b19a79ecSperrin goto out; 1479*b19a79ecSperrin } 1480*b19a79ecSperrin 1481436b2950Sperrin ASSERT(zil_empty(zilog)); 1482*b19a79ecSperrin ret = B_TRUE; 1483*b19a79ecSperrin out: 1484*b19a79ecSperrin cv_broadcast(&zilog->zl_cv_writer); 1485*b19a79ecSperrin mutex_exit(&zilog->zl_lock); 1486*b19a79ecSperrin return (ret); 1487436b2950Sperrin } 1488