1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51472Sperrin * Common Development and Distribution License (the "License"). 61472Sperrin * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 223461Sahrens * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27789Sahrens 28789Sahrens #include <sys/zfs_context.h> 29789Sahrens #include <sys/spa.h> 30789Sahrens #include <sys/dmu.h> 31789Sahrens #include <sys/zap.h> 32789Sahrens #include <sys/arc.h> 33789Sahrens #include <sys/stat.h> 34789Sahrens #include <sys/resource.h> 35789Sahrens #include <sys/zil.h> 36789Sahrens #include <sys/zil_impl.h> 37789Sahrens #include <sys/dsl_dataset.h> 38789Sahrens #include <sys/vdev.h> 393668Sgw25295 #include <sys/dmu_tx.h> 40789Sahrens 41789Sahrens /* 42789Sahrens * The zfs intent log (ZIL) saves transaction records of system calls 43789Sahrens * that change the file system in memory with enough information 44789Sahrens * to be able to replay them. These are stored in memory until 45789Sahrens * either the DMU transaction group (txg) commits them to the stable pool 46789Sahrens * and they can be discarded, or they are flushed to the stable log 47789Sahrens * (also in the pool) due to a fsync, O_DSYNC or other synchronous 48789Sahrens * requirement. In the event of a panic or power fail then those log 49789Sahrens * records (transactions) are replayed. 50789Sahrens * 51789Sahrens * There is one ZIL per file system. Its on-disk (pool) format consists 52789Sahrens * of 3 parts: 53789Sahrens * 54789Sahrens * - ZIL header 55789Sahrens * - ZIL blocks 56789Sahrens * - ZIL records 57789Sahrens * 58789Sahrens * A log record holds a system call transaction. Log blocks can 59789Sahrens * hold many log records and the blocks are chained together. 60789Sahrens * Each ZIL block contains a block pointer (blkptr_t) to the next 61789Sahrens * ZIL block in the chain. The ZIL header points to the first 62789Sahrens * block in the chain. Note there is not a fixed place in the pool 63789Sahrens * to hold blocks. They are dynamically allocated and freed as 64789Sahrens * needed from the blocks available. Figure X shows the ZIL structure: 65789Sahrens */ 66789Sahrens 67789Sahrens /* 682986Sek110237 * This global ZIL switch affects all pools 69789Sahrens */ 70789Sahrens int zil_disable = 0; /* disable intent logging */ 712986Sek110237 722986Sek110237 /* 732986Sek110237 * Tunable parameter for debugging or performance analysis. Setting 742986Sek110237 * zfs_nocacheflush will cause corruption on power loss if a volatile 752986Sek110237 * out-of-order write cache is enabled. 762986Sek110237 */ 772986Sek110237 boolean_t zfs_nocacheflush = B_FALSE; 78789Sahrens 79789Sahrens static kmem_cache_t *zil_lwb_cache; 80789Sahrens 81789Sahrens static int 82789Sahrens zil_dva_compare(const void *x1, const void *x2) 83789Sahrens { 84789Sahrens const dva_t *dva1 = x1; 85789Sahrens const dva_t *dva2 = x2; 86789Sahrens 87789Sahrens if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2)) 88789Sahrens return (-1); 89789Sahrens if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2)) 90789Sahrens return (1); 91789Sahrens 92789Sahrens if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2)) 93789Sahrens return (-1); 94789Sahrens if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2)) 95789Sahrens return (1); 96789Sahrens 97789Sahrens return (0); 98789Sahrens } 99789Sahrens 100789Sahrens static void 101789Sahrens zil_dva_tree_init(avl_tree_t *t) 102789Sahrens { 103789Sahrens avl_create(t, zil_dva_compare, sizeof (zil_dva_node_t), 104789Sahrens offsetof(zil_dva_node_t, zn_node)); 105789Sahrens } 106789Sahrens 107789Sahrens static void 108789Sahrens zil_dva_tree_fini(avl_tree_t *t) 109789Sahrens { 110789Sahrens zil_dva_node_t *zn; 111789Sahrens void *cookie = NULL; 112789Sahrens 113789Sahrens while ((zn = avl_destroy_nodes(t, &cookie)) != NULL) 114789Sahrens kmem_free(zn, sizeof (zil_dva_node_t)); 115789Sahrens 116789Sahrens avl_destroy(t); 117789Sahrens } 118789Sahrens 119789Sahrens static int 120789Sahrens zil_dva_tree_add(avl_tree_t *t, dva_t *dva) 121789Sahrens { 122789Sahrens zil_dva_node_t *zn; 123789Sahrens avl_index_t where; 124789Sahrens 125789Sahrens if (avl_find(t, dva, &where) != NULL) 126789Sahrens return (EEXIST); 127789Sahrens 128789Sahrens zn = kmem_alloc(sizeof (zil_dva_node_t), KM_SLEEP); 129789Sahrens zn->zn_dva = *dva; 130789Sahrens avl_insert(t, zn, where); 131789Sahrens 132789Sahrens return (0); 133789Sahrens } 134789Sahrens 1351807Sbonwick static zil_header_t * 1361807Sbonwick zil_header_in_syncing_context(zilog_t *zilog) 1371807Sbonwick { 1381807Sbonwick return ((zil_header_t *)zilog->zl_header); 1391807Sbonwick } 1401807Sbonwick 1411807Sbonwick static void 1421807Sbonwick zil_init_log_chain(zilog_t *zilog, blkptr_t *bp) 1431807Sbonwick { 1441807Sbonwick zio_cksum_t *zc = &bp->blk_cksum; 1451807Sbonwick 1461807Sbonwick zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL); 1471807Sbonwick zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL); 1481807Sbonwick zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os); 1491807Sbonwick zc->zc_word[ZIL_ZC_SEQ] = 1ULL; 1501807Sbonwick } 1511807Sbonwick 152789Sahrens /* 153789Sahrens * Read a log block, make sure it's valid, and byteswap it if necessary. 154789Sahrens */ 155789Sahrens static int 1561807Sbonwick zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, arc_buf_t **abufpp) 157789Sahrens { 1581807Sbonwick blkptr_t blk = *bp; 1591544Seschrock zbookmark_t zb; 1602391Smaybee uint32_t aflags = ARC_WAIT; 161789Sahrens int error; 162789Sahrens 1631807Sbonwick zb.zb_objset = bp->blk_cksum.zc_word[ZIL_ZC_OBJSET]; 1641544Seschrock zb.zb_object = 0; 1651544Seschrock zb.zb_level = -1; 1661807Sbonwick zb.zb_blkid = bp->blk_cksum.zc_word[ZIL_ZC_SEQ]; 1671807Sbonwick 1681807Sbonwick *abufpp = NULL; 1691807Sbonwick 1701807Sbonwick error = arc_read(NULL, zilog->zl_spa, &blk, byteswap_uint64_array, 1711807Sbonwick arc_getbuf_func, abufpp, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL | 1722391Smaybee ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB, &aflags, &zb); 1731807Sbonwick 1741807Sbonwick if (error == 0) { 1751807Sbonwick char *data = (*abufpp)->b_data; 1761807Sbonwick uint64_t blksz = BP_GET_LSIZE(bp); 1771807Sbonwick zil_trailer_t *ztp = (zil_trailer_t *)(data + blksz) - 1; 1781807Sbonwick zio_cksum_t cksum = bp->blk_cksum; 1791544Seschrock 1801807Sbonwick /* 1811807Sbonwick * Sequence numbers should be... sequential. The checksum 1821807Sbonwick * verifier for the next block should be bp's checksum plus 1. 1831807Sbonwick */ 1841807Sbonwick cksum.zc_word[ZIL_ZC_SEQ]++; 1851807Sbonwick 1861807Sbonwick if (bcmp(&cksum, &ztp->zit_next_blk.blk_cksum, sizeof (cksum))) 1871807Sbonwick error = ESTALE; 1881807Sbonwick else if (BP_IS_HOLE(&ztp->zit_next_blk)) 1891807Sbonwick error = ENOENT; 1901807Sbonwick else if (ztp->zit_nused > (blksz - sizeof (zil_trailer_t))) 1911807Sbonwick error = EOVERFLOW; 1921807Sbonwick 1931807Sbonwick if (error) { 1941807Sbonwick VERIFY(arc_buf_remove_ref(*abufpp, abufpp) == 1); 1951807Sbonwick *abufpp = NULL; 1961807Sbonwick } 197789Sahrens } 198789Sahrens 1991807Sbonwick dprintf("error %d on %llu:%llu\n", error, zb.zb_objset, zb.zb_blkid); 200789Sahrens 2011807Sbonwick return (error); 202789Sahrens } 203789Sahrens 204789Sahrens /* 205789Sahrens * Parse the intent log, and call parse_func for each valid record within. 2061807Sbonwick * Return the highest sequence number. 207789Sahrens */ 2081807Sbonwick uint64_t 209789Sahrens zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func, 210789Sahrens zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg) 211789Sahrens { 2121807Sbonwick const zil_header_t *zh = zilog->zl_header; 2131807Sbonwick uint64_t claim_seq = zh->zh_claim_seq; 2141807Sbonwick uint64_t seq = 0; 2151807Sbonwick uint64_t max_seq = 0; 2161807Sbonwick blkptr_t blk = zh->zh_log; 2171807Sbonwick arc_buf_t *abuf; 218789Sahrens char *lrbuf, *lrp; 219789Sahrens zil_trailer_t *ztp; 220789Sahrens int reclen, error; 221789Sahrens 222789Sahrens if (BP_IS_HOLE(&blk)) 2231807Sbonwick return (max_seq); 224789Sahrens 225789Sahrens /* 226789Sahrens * Starting at the block pointed to by zh_log we read the log chain. 227789Sahrens * For each block in the chain we strongly check that block to 228789Sahrens * ensure its validity. We stop when an invalid block is found. 229789Sahrens * For each block pointer in the chain we call parse_blk_func(). 230789Sahrens * For each record in each valid block we call parse_lr_func(). 2311807Sbonwick * If the log has been claimed, stop if we encounter a sequence 2321807Sbonwick * number greater than the highest claimed sequence number. 233789Sahrens */ 234789Sahrens zil_dva_tree_init(&zilog->zl_dva_tree); 235789Sahrens for (;;) { 2361807Sbonwick seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 2371807Sbonwick 2381807Sbonwick if (claim_seq != 0 && seq > claim_seq) 2391807Sbonwick break; 2401807Sbonwick 2411807Sbonwick ASSERT(max_seq < seq); 2421807Sbonwick max_seq = seq; 2431807Sbonwick 2441807Sbonwick error = zil_read_log_block(zilog, &blk, &abuf); 245789Sahrens 246789Sahrens if (parse_blk_func != NULL) 247789Sahrens parse_blk_func(zilog, &blk, arg, txg); 248789Sahrens 249789Sahrens if (error) 250789Sahrens break; 251789Sahrens 2521807Sbonwick lrbuf = abuf->b_data; 253789Sahrens ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1; 254789Sahrens blk = ztp->zit_next_blk; 255789Sahrens 2561807Sbonwick if (parse_lr_func == NULL) { 2571807Sbonwick VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 258789Sahrens continue; 2591807Sbonwick } 260789Sahrens 261789Sahrens for (lrp = lrbuf; lrp < lrbuf + ztp->zit_nused; lrp += reclen) { 262789Sahrens lr_t *lr = (lr_t *)lrp; 263789Sahrens reclen = lr->lrc_reclen; 264789Sahrens ASSERT3U(reclen, >=, sizeof (lr_t)); 265789Sahrens parse_lr_func(zilog, lr, arg, txg); 266789Sahrens } 2671807Sbonwick VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 268789Sahrens } 269789Sahrens zil_dva_tree_fini(&zilog->zl_dva_tree); 2701807Sbonwick 2711807Sbonwick return (max_seq); 272789Sahrens } 273789Sahrens 274789Sahrens /* ARGSUSED */ 275789Sahrens static void 276789Sahrens zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg) 277789Sahrens { 278789Sahrens spa_t *spa = zilog->zl_spa; 279789Sahrens int err; 280789Sahrens 281789Sahrens /* 282789Sahrens * Claim log block if not already committed and not already claimed. 283789Sahrens */ 284789Sahrens if (bp->blk_birth >= first_txg && 285789Sahrens zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp)) == 0) { 286789Sahrens err = zio_wait(zio_claim(NULL, spa, first_txg, bp, NULL, NULL)); 287789Sahrens ASSERT(err == 0); 288789Sahrens } 289789Sahrens } 290789Sahrens 291789Sahrens static void 292789Sahrens zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg) 293789Sahrens { 294789Sahrens if (lrc->lrc_txtype == TX_WRITE) { 295789Sahrens lr_write_t *lr = (lr_write_t *)lrc; 296789Sahrens zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg); 297789Sahrens } 298789Sahrens } 299789Sahrens 300789Sahrens /* ARGSUSED */ 301789Sahrens static void 302789Sahrens zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg) 303789Sahrens { 304789Sahrens zio_free_blk(zilog->zl_spa, bp, dmu_tx_get_txg(tx)); 305789Sahrens } 306789Sahrens 307789Sahrens static void 308789Sahrens zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg) 309789Sahrens { 310789Sahrens /* 311789Sahrens * If we previously claimed it, we need to free it. 312789Sahrens */ 313789Sahrens if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE) { 314789Sahrens lr_write_t *lr = (lr_write_t *)lrc; 315789Sahrens blkptr_t *bp = &lr->lr_blkptr; 316789Sahrens if (bp->blk_birth >= claim_txg && 317789Sahrens !zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp))) { 318789Sahrens (void) arc_free(NULL, zilog->zl_spa, 319789Sahrens dmu_tx_get_txg(tx), bp, NULL, NULL, ARC_WAIT); 320789Sahrens } 321789Sahrens } 322789Sahrens } 323789Sahrens 324789Sahrens /* 325789Sahrens * Create an on-disk intent log. 326789Sahrens */ 327789Sahrens static void 328789Sahrens zil_create(zilog_t *zilog) 329789Sahrens { 3301807Sbonwick const zil_header_t *zh = zilog->zl_header; 331789Sahrens lwb_t *lwb; 3321807Sbonwick uint64_t txg = 0; 3331807Sbonwick dmu_tx_t *tx = NULL; 334789Sahrens blkptr_t blk; 3351807Sbonwick int error = 0; 336789Sahrens 337789Sahrens /* 3381807Sbonwick * Wait for any previous destroy to complete. 339789Sahrens */ 3401807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 3411807Sbonwick 3421807Sbonwick ASSERT(zh->zh_claim_txg == 0); 3431807Sbonwick ASSERT(zh->zh_replay_seq == 0); 3441807Sbonwick 3451807Sbonwick blk = zh->zh_log; 346789Sahrens 347789Sahrens /* 3481807Sbonwick * If we don't already have an initial log block, allocate one now. 349789Sahrens */ 3501807Sbonwick if (BP_IS_HOLE(&blk)) { 3511807Sbonwick tx = dmu_tx_create(zilog->zl_os); 3521807Sbonwick (void) dmu_tx_assign(tx, TXG_WAIT); 3531807Sbonwick dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 3541807Sbonwick txg = dmu_tx_get_txg(tx); 3551807Sbonwick 3563063Sperrin error = zio_alloc_blk(zilog->zl_spa, ZIL_MIN_BLKSZ, &blk, 3573063Sperrin NULL, txg); 3581807Sbonwick 3591807Sbonwick if (error == 0) 3601807Sbonwick zil_init_log_chain(zilog, &blk); 3611362Sperrin } 3621807Sbonwick 3631807Sbonwick /* 3641807Sbonwick * Allocate a log write buffer (lwb) for the first log block. 3651807Sbonwick */ 366789Sahrens if (error == 0) { 367789Sahrens lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 368789Sahrens lwb->lwb_zilog = zilog; 369789Sahrens lwb->lwb_blk = blk; 370789Sahrens lwb->lwb_nused = 0; 371789Sahrens lwb->lwb_sz = BP_GET_LSIZE(&lwb->lwb_blk); 372789Sahrens lwb->lwb_buf = zio_buf_alloc(lwb->lwb_sz); 373789Sahrens lwb->lwb_max_txg = txg; 3742237Smaybee lwb->lwb_zio = NULL; 3752237Smaybee 376789Sahrens mutex_enter(&zilog->zl_lock); 377789Sahrens list_insert_tail(&zilog->zl_lwb_list, lwb); 378789Sahrens mutex_exit(&zilog->zl_lock); 379789Sahrens } 380789Sahrens 3811807Sbonwick /* 3821807Sbonwick * If we just allocated the first log block, commit our transaction 3831807Sbonwick * and wait for zil_sync() to stuff the block poiner into zh_log. 3841807Sbonwick * (zh is part of the MOS, so we cannot modify it in open context.) 3851807Sbonwick */ 3861807Sbonwick if (tx != NULL) { 3871807Sbonwick dmu_tx_commit(tx); 3881362Sperrin txg_wait_synced(zilog->zl_dmu_pool, txg); 3891807Sbonwick } 3901807Sbonwick 3911807Sbonwick ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0); 392789Sahrens } 393789Sahrens 394789Sahrens /* 395789Sahrens * In one tx, free all log blocks and clear the log header. 3961807Sbonwick * If keep_first is set, then we're replaying a log with no content. 3971807Sbonwick * We want to keep the first block, however, so that the first 3981807Sbonwick * synchronous transaction doesn't require a txg_wait_synced() 3991807Sbonwick * in zil_create(). We don't need to txg_wait_synced() here either 4001807Sbonwick * when keep_first is set, because both zil_create() and zil_destroy() 4011807Sbonwick * will wait for any in-progress destroys to complete. 402789Sahrens */ 403789Sahrens void 4041807Sbonwick zil_destroy(zilog_t *zilog, boolean_t keep_first) 405789Sahrens { 4061807Sbonwick const zil_header_t *zh = zilog->zl_header; 4071807Sbonwick lwb_t *lwb; 408789Sahrens dmu_tx_t *tx; 409789Sahrens uint64_t txg; 410789Sahrens 4111807Sbonwick /* 4121807Sbonwick * Wait for any previous destroy to complete. 4131807Sbonwick */ 4141807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 415789Sahrens 4161807Sbonwick if (BP_IS_HOLE(&zh->zh_log)) 417789Sahrens return; 418789Sahrens 419789Sahrens tx = dmu_tx_create(zilog->zl_os); 420789Sahrens (void) dmu_tx_assign(tx, TXG_WAIT); 421789Sahrens dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 422789Sahrens txg = dmu_tx_get_txg(tx); 423789Sahrens 4241807Sbonwick mutex_enter(&zilog->zl_lock); 4251807Sbonwick 4261807Sbonwick ASSERT3U(zilog->zl_destroy_txg, <, txg); 427789Sahrens zilog->zl_destroy_txg = txg; 4281807Sbonwick zilog->zl_keep_first = keep_first; 4291807Sbonwick 4301807Sbonwick if (!list_is_empty(&zilog->zl_lwb_list)) { 4311807Sbonwick ASSERT(zh->zh_claim_txg == 0); 4321807Sbonwick ASSERT(!keep_first); 4331807Sbonwick while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 4341807Sbonwick list_remove(&zilog->zl_lwb_list, lwb); 4351807Sbonwick if (lwb->lwb_buf != NULL) 4361807Sbonwick zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 4371807Sbonwick zio_free_blk(zilog->zl_spa, &lwb->lwb_blk, txg); 4381807Sbonwick kmem_cache_free(zil_lwb_cache, lwb); 4391807Sbonwick } 4401807Sbonwick } else { 4411807Sbonwick if (!keep_first) { 4421807Sbonwick (void) zil_parse(zilog, zil_free_log_block, 4431807Sbonwick zil_free_log_record, tx, zh->zh_claim_txg); 4441807Sbonwick } 4451807Sbonwick } 4462638Sperrin mutex_exit(&zilog->zl_lock); 447789Sahrens 448789Sahrens dmu_tx_commit(tx); 449789Sahrens 4501807Sbonwick if (keep_first) /* no need to wait in this case */ 4511807Sbonwick return; 4521807Sbonwick 4531807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, txg); 4541807Sbonwick ASSERT(BP_IS_HOLE(&zh->zh_log)); 455789Sahrens } 456789Sahrens 4572199Sahrens int 458789Sahrens zil_claim(char *osname, void *txarg) 459789Sahrens { 460789Sahrens dmu_tx_t *tx = txarg; 461789Sahrens uint64_t first_txg = dmu_tx_get_txg(tx); 462789Sahrens zilog_t *zilog; 463789Sahrens zil_header_t *zh; 464789Sahrens objset_t *os; 465789Sahrens int error; 466789Sahrens 467789Sahrens error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_STANDARD, &os); 468789Sahrens if (error) { 469789Sahrens cmn_err(CE_WARN, "can't process intent log for %s", osname); 4702199Sahrens return (0); 471789Sahrens } 472789Sahrens 473789Sahrens zilog = dmu_objset_zil(os); 4741807Sbonwick zh = zil_header_in_syncing_context(zilog); 475789Sahrens 476789Sahrens /* 4771807Sbonwick * Claim all log blocks if we haven't already done so, and remember 4781807Sbonwick * the highest claimed sequence number. This ensures that if we can 4791807Sbonwick * read only part of the log now (e.g. due to a missing device), 4801807Sbonwick * but we can read the entire log later, we will not try to replay 4811807Sbonwick * or destroy beyond the last block we successfully claimed. 482789Sahrens */ 483789Sahrens ASSERT3U(zh->zh_claim_txg, <=, first_txg); 484789Sahrens if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) { 485789Sahrens zh->zh_claim_txg = first_txg; 4861807Sbonwick zh->zh_claim_seq = zil_parse(zilog, zil_claim_log_block, 4871807Sbonwick zil_claim_log_record, tx, first_txg); 488789Sahrens dsl_dataset_dirty(dmu_objset_ds(os), tx); 489789Sahrens } 4901807Sbonwick 491789Sahrens ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1)); 492789Sahrens dmu_objset_close(os); 4932199Sahrens return (0); 494789Sahrens } 495789Sahrens 496789Sahrens void 4972638Sperrin zil_add_vdev(zilog_t *zilog, uint64_t vdev) 498789Sahrens { 4993063Sperrin zil_vdev_t *zv, *new; 5003063Sperrin uint64_t bmap_sz = sizeof (zilog->zl_vdev_bmap) << 3; 5013063Sperrin uchar_t *cp; 502789Sahrens 5032986Sek110237 if (zfs_nocacheflush) 504789Sahrens return; 505789Sahrens 5063063Sperrin if (vdev < bmap_sz) { 5073063Sperrin cp = zilog->zl_vdev_bmap + (vdev / 8); 5083063Sperrin atomic_or_8(cp, 1 << (vdev % 8)); 5093063Sperrin } else { 5103063Sperrin /* 5113063Sperrin * insert into ordered list 5123063Sperrin */ 5133063Sperrin mutex_enter(&zilog->zl_lock); 5143063Sperrin for (zv = list_head(&zilog->zl_vdev_list); zv != NULL; 5153063Sperrin zv = list_next(&zilog->zl_vdev_list, zv)) { 5163063Sperrin if (zv->vdev == vdev) { 5173063Sperrin /* duplicate found - just return */ 5183063Sperrin mutex_exit(&zilog->zl_lock); 5193063Sperrin return; 5203063Sperrin } 5213063Sperrin if (zv->vdev > vdev) { 5223063Sperrin /* insert before this entry */ 5233063Sperrin new = kmem_alloc(sizeof (zil_vdev_t), 5243063Sperrin KM_SLEEP); 5253063Sperrin new->vdev = vdev; 5263063Sperrin list_insert_before(&zilog->zl_vdev_list, 5273063Sperrin zv, new); 5283063Sperrin mutex_exit(&zilog->zl_lock); 5293063Sperrin return; 5303063Sperrin } 5313063Sperrin } 5323063Sperrin /* ran off end of list, insert at the end */ 5333063Sperrin ASSERT(zv == NULL); 5343063Sperrin new = kmem_alloc(sizeof (zil_vdev_t), KM_SLEEP); 5353063Sperrin new->vdev = vdev; 5363063Sperrin list_insert_tail(&zilog->zl_vdev_list, new); 5373063Sperrin mutex_exit(&zilog->zl_lock); 5383063Sperrin } 5393063Sperrin } 5403063Sperrin 541789Sahrens void 5422638Sperrin zil_flush_vdevs(zilog_t *zilog) 543789Sahrens { 5443063Sperrin zil_vdev_t *zv; 5453063Sperrin zio_t *zio = NULL; 5463063Sperrin spa_t *spa = zilog->zl_spa; 547789Sahrens uint64_t vdev; 5483063Sperrin uint8_t b; 5493063Sperrin int i, j; 5503063Sperrin 5513063Sperrin ASSERT(zilog->zl_writer); 552789Sahrens 5533063Sperrin for (i = 0; i < sizeof (zilog->zl_vdev_bmap); i++) { 5543063Sperrin b = zilog->zl_vdev_bmap[i]; 5553063Sperrin if (b == 0) 5563063Sperrin continue; 5573063Sperrin for (j = 0; j < 8; j++) { 5583063Sperrin if (b & (1 << j)) { 5593063Sperrin vdev = (i << 3) + j; 560*4469Sperrin zio_flush_vdev(spa, vdev, &zio); 5613063Sperrin } 5623063Sperrin } 5633063Sperrin zilog->zl_vdev_bmap[i] = 0; 5643063Sperrin } 565789Sahrens 5662638Sperrin while ((zv = list_head(&zilog->zl_vdev_list)) != NULL) { 567*4469Sperrin zio_flush_vdev(spa, zv->vdev, &zio); 568789Sahrens list_remove(&zilog->zl_vdev_list, zv); 569789Sahrens kmem_free(zv, sizeof (zil_vdev_t)); 570789Sahrens } 571789Sahrens /* 572789Sahrens * Wait for all the flushes to complete. Not all devices actually 573789Sahrens * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails. 574789Sahrens */ 5753063Sperrin if (zio) 576789Sahrens (void) zio_wait(zio); 577789Sahrens } 578789Sahrens 579789Sahrens /* 580789Sahrens * Function called when a log block write completes 581789Sahrens */ 582789Sahrens static void 583789Sahrens zil_lwb_write_done(zio_t *zio) 584789Sahrens { 585789Sahrens lwb_t *lwb = zio->io_private; 586789Sahrens zilog_t *zilog = lwb->lwb_zilog; 587789Sahrens 588789Sahrens /* 589789Sahrens * Now that we've written this log block, we have a stable pointer 590789Sahrens * to the next block in the chain, so it's OK to let the txg in 591789Sahrens * which we allocated the next block sync. 592789Sahrens */ 593789Sahrens txg_rele_to_sync(&lwb->lwb_txgh); 594789Sahrens 595789Sahrens zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 596789Sahrens mutex_enter(&zilog->zl_lock); 597789Sahrens lwb->lwb_buf = NULL; 598789Sahrens if (zio->io_error) { 599789Sahrens zilog->zl_log_error = B_TRUE; 600789Sahrens mutex_exit(&zilog->zl_lock); 601789Sahrens return; 602789Sahrens } 603789Sahrens mutex_exit(&zilog->zl_lock); 604789Sahrens } 605789Sahrens 606789Sahrens /* 6072237Smaybee * Initialize the io for a log block. 6082237Smaybee * 6092237Smaybee * Note, we should not initialize the IO until we are about 6102237Smaybee * to use it, since zio_rewrite() does a spa_config_enter(). 6112237Smaybee */ 6122237Smaybee static void 6132237Smaybee zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb) 6142237Smaybee { 6152237Smaybee zbookmark_t zb; 6162237Smaybee 6172237Smaybee zb.zb_objset = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET]; 6182237Smaybee zb.zb_object = 0; 6192237Smaybee zb.zb_level = -1; 6202237Smaybee zb.zb_blkid = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 6212237Smaybee 6222638Sperrin if (zilog->zl_root_zio == NULL) { 6232638Sperrin zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL, 6242638Sperrin ZIO_FLAG_CANFAIL); 6252638Sperrin } 6263063Sperrin if (lwb->lwb_zio == NULL) { 6273063Sperrin lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa, 6283063Sperrin ZIO_CHECKSUM_ZILOG, 0, &lwb->lwb_blk, lwb->lwb_buf, 6293063Sperrin lwb->lwb_sz, zil_lwb_write_done, lwb, 6303063Sperrin ZIO_PRIORITY_LOG_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb); 6313063Sperrin } 6322237Smaybee } 6332237Smaybee 6342237Smaybee /* 635789Sahrens * Start a log block write and advance to the next log block. 636789Sahrens * Calls are serialized. 637789Sahrens */ 638789Sahrens static lwb_t * 639789Sahrens zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb) 640789Sahrens { 641789Sahrens lwb_t *nlwb; 642789Sahrens zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1; 6431807Sbonwick spa_t *spa = zilog->zl_spa; 6441807Sbonwick blkptr_t *bp = &ztp->zit_next_blk; 645789Sahrens uint64_t txg; 646789Sahrens uint64_t zil_blksz; 647789Sahrens int error; 648789Sahrens 649789Sahrens ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb)); 650789Sahrens 651789Sahrens /* 652789Sahrens * Allocate the next block and save its address in this block 653789Sahrens * before writing it in order to establish the log chain. 654789Sahrens * Note that if the allocation of nlwb synced before we wrote 655789Sahrens * the block that points at it (lwb), we'd leak it if we crashed. 656789Sahrens * Therefore, we don't do txg_rele_to_sync() until zil_lwb_write_done(). 657789Sahrens */ 658789Sahrens txg = txg_hold_open(zilog->zl_dmu_pool, &lwb->lwb_txgh); 659789Sahrens txg_rele_to_quiesce(&lwb->lwb_txgh); 660789Sahrens 661789Sahrens /* 6621141Sperrin * Pick a ZIL blocksize. We request a size that is the 6631141Sperrin * maximum of the previous used size, the current used size and 6641141Sperrin * the amount waiting in the queue. 665789Sahrens */ 6662237Smaybee zil_blksz = MAX(zilog->zl_prev_used, 6672237Smaybee zilog->zl_cur_used + sizeof (*ztp)); 6681141Sperrin zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp)); 6691842Sperrin zil_blksz = P2ROUNDUP_TYPED(zil_blksz, ZIL_MIN_BLKSZ, uint64_t); 6701141Sperrin if (zil_blksz > ZIL_MAX_BLKSZ) 6711141Sperrin zil_blksz = ZIL_MAX_BLKSZ; 672789Sahrens 6733063Sperrin BP_ZERO(bp); 6743063Sperrin /* pass the old blkptr in order to spread log blocks across devs */ 6753063Sperrin error = zio_alloc_blk(spa, zil_blksz, bp, &lwb->lwb_blk, txg); 676789Sahrens if (error) { 6773668Sgw25295 dmu_tx_t *tx = dmu_tx_create_assigned(zilog->zl_dmu_pool, txg); 6783668Sgw25295 6791544Seschrock /* 6803668Sgw25295 * We dirty the dataset to ensure that zil_sync() will 6813668Sgw25295 * be called to remove this lwb from our zl_lwb_list. 6823668Sgw25295 * Failing to do so, may leave an lwb with a NULL lwb_buf 6833668Sgw25295 * hanging around on the zl_lwb_list. 6843668Sgw25295 */ 6853668Sgw25295 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 6863848Sgw25295 dmu_tx_commit(tx); 6873668Sgw25295 6883668Sgw25295 /* 6893668Sgw25295 * Since we've just experienced an allocation failure so we 6903668Sgw25295 * terminate the current lwb and send it on its way. 6913668Sgw25295 */ 6923668Sgw25295 ztp->zit_pad = 0; 6933668Sgw25295 ztp->zit_nused = lwb->lwb_nused; 6943668Sgw25295 ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum; 6953668Sgw25295 zio_nowait(lwb->lwb_zio); 6963668Sgw25295 6973668Sgw25295 /* 6981544Seschrock * By returning NULL the caller will call tx_wait_synced() 6991544Seschrock */ 700789Sahrens return (NULL); 701789Sahrens } 702789Sahrens 7031807Sbonwick ASSERT3U(bp->blk_birth, ==, txg); 7041544Seschrock ztp->zit_pad = 0; 705789Sahrens ztp->zit_nused = lwb->lwb_nused; 706789Sahrens ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum; 7071807Sbonwick bp->blk_cksum = lwb->lwb_blk.blk_cksum; 7081807Sbonwick bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++; 709789Sahrens 710789Sahrens /* 711789Sahrens * Allocate a new log write buffer (lwb). 712789Sahrens */ 713789Sahrens nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 714789Sahrens 715789Sahrens nlwb->lwb_zilog = zilog; 7161807Sbonwick nlwb->lwb_blk = *bp; 717789Sahrens nlwb->lwb_nused = 0; 718789Sahrens nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk); 719789Sahrens nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz); 720789Sahrens nlwb->lwb_max_txg = txg; 7212237Smaybee nlwb->lwb_zio = NULL; 722789Sahrens 723789Sahrens /* 7243063Sperrin * Put new lwb at the end of the log chain 725789Sahrens */ 726789Sahrens mutex_enter(&zilog->zl_lock); 727789Sahrens list_insert_tail(&zilog->zl_lwb_list, nlwb); 7283063Sperrin mutex_exit(&zilog->zl_lock); 7293063Sperrin 7303063Sperrin /* Record the vdev for later flushing */ 7312638Sperrin zil_add_vdev(zilog, DVA_GET_VDEV(BP_IDENTITY(&(lwb->lwb_blk)))); 732789Sahrens 733789Sahrens /* 7342237Smaybee * kick off the write for the old log block 735789Sahrens */ 7362237Smaybee dprintf_bp(&lwb->lwb_blk, "lwb %p txg %llu: ", lwb, txg); 7373063Sperrin ASSERT(lwb->lwb_zio); 7382237Smaybee zio_nowait(lwb->lwb_zio); 739789Sahrens 740789Sahrens return (nlwb); 741789Sahrens } 742789Sahrens 743789Sahrens static lwb_t * 744789Sahrens zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb) 745789Sahrens { 746789Sahrens lr_t *lrc = &itx->itx_lr; /* common log record */ 7472237Smaybee lr_write_t *lr = (lr_write_t *)lrc; 748789Sahrens uint64_t txg = lrc->lrc_txg; 749789Sahrens uint64_t reclen = lrc->lrc_reclen; 7502237Smaybee uint64_t dlen; 751789Sahrens 752789Sahrens if (lwb == NULL) 753789Sahrens return (NULL); 754789Sahrens ASSERT(lwb->lwb_buf != NULL); 755789Sahrens 7562237Smaybee if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY) 7572237Smaybee dlen = P2ROUNDUP_TYPED( 7582237Smaybee lr->lr_length, sizeof (uint64_t), uint64_t); 7592237Smaybee else 7602237Smaybee dlen = 0; 7611669Sperrin 7621669Sperrin zilog->zl_cur_used += (reclen + dlen); 7631669Sperrin 7643063Sperrin zil_lwb_write_init(zilog, lwb); 7653063Sperrin 7661669Sperrin /* 7671669Sperrin * If this record won't fit in the current log block, start a new one. 7681669Sperrin */ 7691669Sperrin if (lwb->lwb_nused + reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) { 7701669Sperrin lwb = zil_lwb_write_start(zilog, lwb); 7712237Smaybee if (lwb == NULL) 7721669Sperrin return (NULL); 7733063Sperrin zil_lwb_write_init(zilog, lwb); 7741669Sperrin ASSERT(lwb->lwb_nused == 0); 7751669Sperrin if (reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) { 7761669Sperrin txg_wait_synced(zilog->zl_dmu_pool, txg); 777789Sahrens return (lwb); 778789Sahrens } 779789Sahrens } 780789Sahrens 7812638Sperrin /* 7822638Sperrin * Update the lrc_seq, to be log record sequence number. See zil.h 7832638Sperrin * Then copy the record to the log buffer. 7842638Sperrin */ 7852638Sperrin lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */ 786789Sahrens bcopy(lrc, lwb->lwb_buf + lwb->lwb_nused, reclen); 7872237Smaybee 7882237Smaybee /* 7892237Smaybee * If it's a write, fetch the data or get its blkptr as appropriate. 7902237Smaybee */ 7912237Smaybee if (lrc->lrc_txtype == TX_WRITE) { 7922237Smaybee if (txg > spa_freeze_txg(zilog->zl_spa)) 7932237Smaybee txg_wait_synced(zilog->zl_dmu_pool, txg); 7942237Smaybee if (itx->itx_wr_state != WR_COPIED) { 7952237Smaybee char *dbuf; 7962237Smaybee int error; 7972237Smaybee 7982237Smaybee /* alignment is guaranteed */ 7992237Smaybee lr = (lr_write_t *)(lwb->lwb_buf + lwb->lwb_nused); 8002237Smaybee if (dlen) { 8012237Smaybee ASSERT(itx->itx_wr_state == WR_NEED_COPY); 8022237Smaybee dbuf = lwb->lwb_buf + lwb->lwb_nused + reclen; 8032237Smaybee lr->lr_common.lrc_reclen += dlen; 8042237Smaybee } else { 8052237Smaybee ASSERT(itx->itx_wr_state == WR_INDIRECT); 8062237Smaybee dbuf = NULL; 8072237Smaybee } 8082237Smaybee error = zilog->zl_get_data( 8092237Smaybee itx->itx_private, lr, dbuf, lwb->lwb_zio); 8102237Smaybee if (error) { 8112237Smaybee ASSERT(error == ENOENT || error == EEXIST || 8122237Smaybee error == EALREADY); 8132237Smaybee return (lwb); 8142237Smaybee } 8152237Smaybee } 8161669Sperrin } 8172237Smaybee 8182237Smaybee lwb->lwb_nused += reclen + dlen; 819789Sahrens lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg); 820789Sahrens ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb)); 821789Sahrens ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0); 822789Sahrens 823789Sahrens return (lwb); 824789Sahrens } 825789Sahrens 826789Sahrens itx_t * 827789Sahrens zil_itx_create(int txtype, size_t lrsize) 828789Sahrens { 829789Sahrens itx_t *itx; 830789Sahrens 8311842Sperrin lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t); 832789Sahrens 833789Sahrens itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP); 834789Sahrens itx->itx_lr.lrc_txtype = txtype; 835789Sahrens itx->itx_lr.lrc_reclen = lrsize; 836789Sahrens itx->itx_lr.lrc_seq = 0; /* defensive */ 837789Sahrens 838789Sahrens return (itx); 839789Sahrens } 840789Sahrens 841789Sahrens uint64_t 842789Sahrens zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx) 843789Sahrens { 844789Sahrens uint64_t seq; 845789Sahrens 846789Sahrens ASSERT(itx->itx_lr.lrc_seq == 0); 847789Sahrens 848789Sahrens mutex_enter(&zilog->zl_lock); 849789Sahrens list_insert_tail(&zilog->zl_itx_list, itx); 850789Sahrens zilog->zl_itx_list_sz += itx->itx_lr.lrc_reclen; 851789Sahrens itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx); 852789Sahrens itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq; 853789Sahrens mutex_exit(&zilog->zl_lock); 854789Sahrens 855789Sahrens return (seq); 856789Sahrens } 857789Sahrens 858789Sahrens /* 859789Sahrens * Free up all in-memory intent log transactions that have now been synced. 860789Sahrens */ 861789Sahrens static void 862789Sahrens zil_itx_clean(zilog_t *zilog) 863789Sahrens { 864789Sahrens uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa); 865789Sahrens uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa); 8663778Sjohansen list_t clean_list; 867789Sahrens itx_t *itx; 868789Sahrens 8693778Sjohansen list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node)); 8703778Sjohansen 871789Sahrens mutex_enter(&zilog->zl_lock); 8722638Sperrin /* wait for a log writer to finish walking list */ 8732638Sperrin while (zilog->zl_writer) { 8742638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 8752638Sperrin } 8763778Sjohansen 8773778Sjohansen /* 8783778Sjohansen * Move the sync'd log transactions to a separate list so we can call 8793778Sjohansen * kmem_free without holding the zl_lock. 8803778Sjohansen * 8813778Sjohansen * There is no need to set zl_writer as we don't drop zl_lock here 8823778Sjohansen */ 883789Sahrens while ((itx = list_head(&zilog->zl_itx_list)) != NULL && 884789Sahrens itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) { 885789Sahrens list_remove(&zilog->zl_itx_list, itx); 886789Sahrens zilog->zl_itx_list_sz -= itx->itx_lr.lrc_reclen; 8873778Sjohansen list_insert_tail(&clean_list, itx); 8883778Sjohansen } 8893778Sjohansen cv_broadcast(&zilog->zl_cv_writer); 8903778Sjohansen mutex_exit(&zilog->zl_lock); 8913778Sjohansen 8923778Sjohansen /* destroy sync'd log transactions */ 8933778Sjohansen while ((itx = list_head(&clean_list)) != NULL) { 8943778Sjohansen list_remove(&clean_list, itx); 895789Sahrens kmem_free(itx, offsetof(itx_t, itx_lr) 896789Sahrens + itx->itx_lr.lrc_reclen); 897789Sahrens } 8983778Sjohansen list_destroy(&clean_list); 899789Sahrens } 900789Sahrens 9012638Sperrin /* 9023063Sperrin * If there are any in-memory intent log transactions which have now been 9033063Sperrin * synced then start up a taskq to free them. 9042638Sperrin */ 905789Sahrens void 906789Sahrens zil_clean(zilog_t *zilog) 907789Sahrens { 9083063Sperrin itx_t *itx; 9093063Sperrin 910789Sahrens mutex_enter(&zilog->zl_lock); 9113063Sperrin itx = list_head(&zilog->zl_itx_list); 9123063Sperrin if ((itx != NULL) && 9133063Sperrin (itx->itx_lr.lrc_txg <= spa_last_synced_txg(zilog->zl_spa))) { 914789Sahrens (void) taskq_dispatch(zilog->zl_clean_taskq, 915789Sahrens (void (*)(void *))zil_itx_clean, zilog, TQ_NOSLEEP); 9163063Sperrin } 917789Sahrens mutex_exit(&zilog->zl_lock); 918789Sahrens } 919789Sahrens 920789Sahrens void 9212638Sperrin zil_commit_writer(zilog_t *zilog, uint64_t seq, uint64_t foid) 922789Sahrens { 923789Sahrens uint64_t txg; 924789Sahrens uint64_t reclen; 9253063Sperrin uint64_t commit_seq = 0; 9262638Sperrin itx_t *itx, *itx_next = (itx_t *)-1; 927789Sahrens lwb_t *lwb; 928789Sahrens spa_t *spa; 929789Sahrens 9302638Sperrin zilog->zl_writer = B_TRUE; 9312638Sperrin zilog->zl_root_zio = NULL; 932789Sahrens spa = zilog->zl_spa; 933789Sahrens 934789Sahrens if (zilog->zl_suspend) { 935789Sahrens lwb = NULL; 936789Sahrens } else { 937789Sahrens lwb = list_tail(&zilog->zl_lwb_list); 938789Sahrens if (lwb == NULL) { 9392638Sperrin /* 9402638Sperrin * Return if there's nothing to flush before we 9412638Sperrin * dirty the fs by calling zil_create() 9422638Sperrin */ 9432638Sperrin if (list_is_empty(&zilog->zl_itx_list)) { 9442638Sperrin zilog->zl_writer = B_FALSE; 9452638Sperrin return; 9462638Sperrin } 947789Sahrens mutex_exit(&zilog->zl_lock); 948789Sahrens zil_create(zilog); 949789Sahrens mutex_enter(&zilog->zl_lock); 950789Sahrens lwb = list_tail(&zilog->zl_lwb_list); 951789Sahrens } 952789Sahrens } 953789Sahrens 9543063Sperrin /* Loop through in-memory log transactions filling log blocks. */ 9552638Sperrin DTRACE_PROBE1(zil__cw1, zilog_t *, zilog); 956789Sahrens for (;;) { 9572638Sperrin /* 9582638Sperrin * Find the next itx to push: 9592638Sperrin * Push all transactions related to specified foid and all 9602638Sperrin * other transactions except TX_WRITE, TX_TRUNCATE, 9612638Sperrin * TX_SETATTR and TX_ACL for all other files. 9622638Sperrin */ 9632638Sperrin if (itx_next != (itx_t *)-1) 9642638Sperrin itx = itx_next; 9652638Sperrin else 9662638Sperrin itx = list_head(&zilog->zl_itx_list); 9672638Sperrin for (; itx != NULL; itx = list_next(&zilog->zl_itx_list, itx)) { 9682638Sperrin if (foid == 0) /* push all foids? */ 9692638Sperrin break; 9703063Sperrin if (itx->itx_sync) /* push all O_[D]SYNC */ 9713063Sperrin break; 9722638Sperrin switch (itx->itx_lr.lrc_txtype) { 9732638Sperrin case TX_SETATTR: 9742638Sperrin case TX_WRITE: 9752638Sperrin case TX_TRUNCATE: 9762638Sperrin case TX_ACL: 9772638Sperrin /* lr_foid is same offset for these records */ 9782638Sperrin if (((lr_write_t *)&itx->itx_lr)->lr_foid 9792638Sperrin != foid) { 9802638Sperrin continue; /* skip this record */ 9812638Sperrin } 9822638Sperrin } 9832638Sperrin break; 9842638Sperrin } 985789Sahrens if (itx == NULL) 986789Sahrens break; 987789Sahrens 988789Sahrens reclen = itx->itx_lr.lrc_reclen; 989789Sahrens if ((itx->itx_lr.lrc_seq > seq) && 9902638Sperrin ((lwb == NULL) || (lwb->lwb_nused == 0) || 9913063Sperrin (lwb->lwb_nused + reclen > ZIL_BLK_DATA_SZ(lwb)))) { 992789Sahrens break; 9933063Sperrin } 994789Sahrens 9952638Sperrin /* 9962638Sperrin * Save the next pointer. Even though we soon drop 9972638Sperrin * zl_lock all threads that may change the list 9982638Sperrin * (another writer or zil_itx_clean) can't do so until 9992638Sperrin * they have zl_writer. 10002638Sperrin */ 10012638Sperrin itx_next = list_next(&zilog->zl_itx_list, itx); 1002789Sahrens list_remove(&zilog->zl_itx_list, itx); 10033063Sperrin mutex_exit(&zilog->zl_lock); 1004789Sahrens txg = itx->itx_lr.lrc_txg; 1005789Sahrens ASSERT(txg); 1006789Sahrens 1007789Sahrens if (txg > spa_last_synced_txg(spa) || 1008789Sahrens txg > spa_freeze_txg(spa)) 1009789Sahrens lwb = zil_lwb_commit(zilog, itx, lwb); 1010789Sahrens kmem_free(itx, offsetof(itx_t, itx_lr) 1011789Sahrens + itx->itx_lr.lrc_reclen); 1012789Sahrens mutex_enter(&zilog->zl_lock); 1013789Sahrens zilog->zl_itx_list_sz -= reclen; 1014789Sahrens } 10152638Sperrin DTRACE_PROBE1(zil__cw2, zilog_t *, zilog); 10163063Sperrin /* determine commit sequence number */ 10173063Sperrin itx = list_head(&zilog->zl_itx_list); 10183063Sperrin if (itx) 10193063Sperrin commit_seq = itx->itx_lr.lrc_seq; 10203063Sperrin else 10213063Sperrin commit_seq = zilog->zl_itx_seq; 1022789Sahrens mutex_exit(&zilog->zl_lock); 1023789Sahrens 1024789Sahrens /* write the last block out */ 10253063Sperrin if (lwb != NULL && lwb->lwb_zio != NULL) 1026789Sahrens lwb = zil_lwb_write_start(zilog, lwb); 1027789Sahrens 10281141Sperrin zilog->zl_prev_used = zilog->zl_cur_used; 10291141Sperrin zilog->zl_cur_used = 0; 10301141Sperrin 10312638Sperrin /* 10322638Sperrin * Wait if necessary for the log blocks to be on stable storage. 10332638Sperrin */ 10342638Sperrin if (zilog->zl_root_zio) { 10352638Sperrin DTRACE_PROBE1(zil__cw3, zilog_t *, zilog); 10362638Sperrin (void) zio_wait(zilog->zl_root_zio); 10372638Sperrin DTRACE_PROBE1(zil__cw4, zilog_t *, zilog); 10383063Sperrin if (!zfs_nocacheflush) 10393063Sperrin zil_flush_vdevs(zilog); 1040789Sahrens } 10411141Sperrin 1042789Sahrens if (zilog->zl_log_error || lwb == NULL) { 1043789Sahrens zilog->zl_log_error = 0; 1044789Sahrens txg_wait_synced(zilog->zl_dmu_pool, 0); 1045789Sahrens } 10463063Sperrin 10473063Sperrin mutex_enter(&zilog->zl_lock); 10481141Sperrin zilog->zl_writer = B_FALSE; 10493063Sperrin 10503063Sperrin ASSERT3U(commit_seq, >=, zilog->zl_commit_seq); 10513063Sperrin zilog->zl_commit_seq = commit_seq; 10522638Sperrin } 10532638Sperrin 10542638Sperrin /* 10552638Sperrin * Push zfs transactions to stable storage up to the supplied sequence number. 10562638Sperrin * If foid is 0 push out all transactions, otherwise push only those 10572638Sperrin * for that file or might have been used to create that file. 10582638Sperrin */ 10592638Sperrin void 10602638Sperrin zil_commit(zilog_t *zilog, uint64_t seq, uint64_t foid) 10612638Sperrin { 10622638Sperrin if (zilog == NULL || seq == 0) 10632638Sperrin return; 10642638Sperrin 10652638Sperrin mutex_enter(&zilog->zl_lock); 10662638Sperrin 10672638Sperrin seq = MIN(seq, zilog->zl_itx_seq); /* cap seq at largest itx seq */ 10682638Sperrin 10693063Sperrin while (zilog->zl_writer) { 10702638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 10713063Sperrin if (seq < zilog->zl_commit_seq) { 10723063Sperrin mutex_exit(&zilog->zl_lock); 10733063Sperrin return; 10743063Sperrin } 10753063Sperrin } 10762638Sperrin zil_commit_writer(zilog, seq, foid); /* drops zl_lock */ 10773063Sperrin /* wake up others waiting on the commit */ 10783063Sperrin cv_broadcast(&zilog->zl_cv_writer); 10793063Sperrin mutex_exit(&zilog->zl_lock); 1080789Sahrens } 1081789Sahrens 1082789Sahrens /* 1083789Sahrens * Called in syncing context to free committed log blocks and update log header. 1084789Sahrens */ 1085789Sahrens void 1086789Sahrens zil_sync(zilog_t *zilog, dmu_tx_t *tx) 1087789Sahrens { 10881807Sbonwick zil_header_t *zh = zil_header_in_syncing_context(zilog); 1089789Sahrens uint64_t txg = dmu_tx_get_txg(tx); 1090789Sahrens spa_t *spa = zilog->zl_spa; 1091789Sahrens lwb_t *lwb; 1092789Sahrens 10931807Sbonwick mutex_enter(&zilog->zl_lock); 10941807Sbonwick 1095789Sahrens ASSERT(zilog->zl_stop_sync == 0); 1096789Sahrens 10971807Sbonwick zh->zh_replay_seq = zilog->zl_replay_seq[txg & TXG_MASK]; 1098789Sahrens 1099789Sahrens if (zilog->zl_destroy_txg == txg) { 11001807Sbonwick blkptr_t blk = zh->zh_log; 11011807Sbonwick 11021807Sbonwick ASSERT(list_head(&zilog->zl_lwb_list) == NULL); 11031807Sbonwick ASSERT(spa_sync_pass(spa) == 1); 11041807Sbonwick 11051807Sbonwick bzero(zh, sizeof (zil_header_t)); 1106789Sahrens bzero(zilog->zl_replay_seq, sizeof (zilog->zl_replay_seq)); 11071807Sbonwick 11081807Sbonwick if (zilog->zl_keep_first) { 11091807Sbonwick /* 11101807Sbonwick * If this block was part of log chain that couldn't 11111807Sbonwick * be claimed because a device was missing during 11121807Sbonwick * zil_claim(), but that device later returns, 11131807Sbonwick * then this block could erroneously appear valid. 11141807Sbonwick * To guard against this, assign a new GUID to the new 11151807Sbonwick * log chain so it doesn't matter what blk points to. 11161807Sbonwick */ 11171807Sbonwick zil_init_log_chain(zilog, &blk); 11181807Sbonwick zh->zh_log = blk; 11191807Sbonwick } 1120789Sahrens } 1121789Sahrens 1122789Sahrens for (;;) { 1123789Sahrens lwb = list_head(&zilog->zl_lwb_list); 1124789Sahrens if (lwb == NULL) { 1125789Sahrens mutex_exit(&zilog->zl_lock); 1126789Sahrens return; 1127789Sahrens } 11282638Sperrin zh->zh_log = lwb->lwb_blk; 1129789Sahrens if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg) 1130789Sahrens break; 1131789Sahrens list_remove(&zilog->zl_lwb_list, lwb); 1132789Sahrens zio_free_blk(spa, &lwb->lwb_blk, txg); 1133789Sahrens kmem_cache_free(zil_lwb_cache, lwb); 11343668Sgw25295 11353668Sgw25295 /* 11363668Sgw25295 * If we don't have anything left in the lwb list then 11373668Sgw25295 * we've had an allocation failure and we need to zero 11383668Sgw25295 * out the zil_header blkptr so that we don't end 11393668Sgw25295 * up freeing the same block twice. 11403668Sgw25295 */ 11413668Sgw25295 if (list_head(&zilog->zl_lwb_list) == NULL) 11423668Sgw25295 BP_ZERO(&zh->zh_log); 1143789Sahrens } 1144789Sahrens mutex_exit(&zilog->zl_lock); 1145789Sahrens } 1146789Sahrens 1147789Sahrens void 1148789Sahrens zil_init(void) 1149789Sahrens { 1150789Sahrens zil_lwb_cache = kmem_cache_create("zil_lwb_cache", 11512856Snd150628 sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0); 1152789Sahrens } 1153789Sahrens 1154789Sahrens void 1155789Sahrens zil_fini(void) 1156789Sahrens { 1157789Sahrens kmem_cache_destroy(zil_lwb_cache); 1158789Sahrens } 1159789Sahrens 1160789Sahrens zilog_t * 1161789Sahrens zil_alloc(objset_t *os, zil_header_t *zh_phys) 1162789Sahrens { 1163789Sahrens zilog_t *zilog; 1164789Sahrens 1165789Sahrens zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP); 1166789Sahrens 1167789Sahrens zilog->zl_header = zh_phys; 1168789Sahrens zilog->zl_os = os; 1169789Sahrens zilog->zl_spa = dmu_objset_spa(os); 1170789Sahrens zilog->zl_dmu_pool = dmu_objset_pool(os); 11711807Sbonwick zilog->zl_destroy_txg = TXG_INITIAL - 1; 1172789Sahrens 11732856Snd150628 mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL); 11742856Snd150628 1175789Sahrens list_create(&zilog->zl_itx_list, sizeof (itx_t), 1176789Sahrens offsetof(itx_t, itx_node)); 1177789Sahrens 1178789Sahrens list_create(&zilog->zl_lwb_list, sizeof (lwb_t), 1179789Sahrens offsetof(lwb_t, lwb_node)); 1180789Sahrens 1181789Sahrens list_create(&zilog->zl_vdev_list, sizeof (zil_vdev_t), 1182789Sahrens offsetof(zil_vdev_t, vdev_seq_node)); 1183789Sahrens 1184789Sahrens return (zilog); 1185789Sahrens } 1186789Sahrens 1187789Sahrens void 1188789Sahrens zil_free(zilog_t *zilog) 1189789Sahrens { 1190789Sahrens lwb_t *lwb; 1191789Sahrens zil_vdev_t *zv; 1192789Sahrens 1193789Sahrens zilog->zl_stop_sync = 1; 1194789Sahrens 1195789Sahrens while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 1196789Sahrens list_remove(&zilog->zl_lwb_list, lwb); 1197789Sahrens if (lwb->lwb_buf != NULL) 1198789Sahrens zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 1199789Sahrens kmem_cache_free(zil_lwb_cache, lwb); 1200789Sahrens } 1201789Sahrens list_destroy(&zilog->zl_lwb_list); 1202789Sahrens 1203789Sahrens while ((zv = list_head(&zilog->zl_vdev_list)) != NULL) { 1204789Sahrens list_remove(&zilog->zl_vdev_list, zv); 1205789Sahrens kmem_free(zv, sizeof (zil_vdev_t)); 1206789Sahrens } 1207789Sahrens list_destroy(&zilog->zl_vdev_list); 1208789Sahrens 1209789Sahrens ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1210789Sahrens list_destroy(&zilog->zl_itx_list); 12112856Snd150628 mutex_destroy(&zilog->zl_lock); 1212789Sahrens 1213789Sahrens kmem_free(zilog, sizeof (zilog_t)); 1214789Sahrens } 1215789Sahrens 1216789Sahrens /* 12171646Sperrin * return true if the initial log block is not valid 12181362Sperrin */ 12191362Sperrin static int 12201362Sperrin zil_empty(zilog_t *zilog) 12211362Sperrin { 12221807Sbonwick const zil_header_t *zh = zilog->zl_header; 12231807Sbonwick arc_buf_t *abuf = NULL; 12241362Sperrin 12251807Sbonwick if (BP_IS_HOLE(&zh->zh_log)) 12261362Sperrin return (1); 12271362Sperrin 12281807Sbonwick if (zil_read_log_block(zilog, &zh->zh_log, &abuf) != 0) 12291807Sbonwick return (1); 12301807Sbonwick 12311807Sbonwick VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 12321807Sbonwick return (0); 12331362Sperrin } 12341362Sperrin 12351362Sperrin /* 1236789Sahrens * Open an intent log. 1237789Sahrens */ 1238789Sahrens zilog_t * 1239789Sahrens zil_open(objset_t *os, zil_get_data_t *get_data) 1240789Sahrens { 1241789Sahrens zilog_t *zilog = dmu_objset_zil(os); 1242789Sahrens 1243789Sahrens zilog->zl_get_data = get_data; 1244789Sahrens zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri, 1245789Sahrens 2, 2, TASKQ_PREPOPULATE); 1246789Sahrens 1247789Sahrens return (zilog); 1248789Sahrens } 1249789Sahrens 1250789Sahrens /* 1251789Sahrens * Close an intent log. 1252789Sahrens */ 1253789Sahrens void 1254789Sahrens zil_close(zilog_t *zilog) 1255789Sahrens { 12561807Sbonwick /* 12571807Sbonwick * If the log isn't already committed, mark the objset dirty 12581807Sbonwick * (so zil_sync() will be called) and wait for that txg to sync. 12591807Sbonwick */ 12601807Sbonwick if (!zil_is_committed(zilog)) { 12611807Sbonwick uint64_t txg; 12621807Sbonwick dmu_tx_t *tx = dmu_tx_create(zilog->zl_os); 12631807Sbonwick (void) dmu_tx_assign(tx, TXG_WAIT); 12641807Sbonwick dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 12651807Sbonwick txg = dmu_tx_get_txg(tx); 12661807Sbonwick dmu_tx_commit(tx); 12671807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, txg); 12681807Sbonwick } 12691807Sbonwick 1270789Sahrens taskq_destroy(zilog->zl_clean_taskq); 1271789Sahrens zilog->zl_clean_taskq = NULL; 1272789Sahrens zilog->zl_get_data = NULL; 1273789Sahrens 1274789Sahrens zil_itx_clean(zilog); 1275789Sahrens ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1276789Sahrens } 1277789Sahrens 1278789Sahrens /* 1279789Sahrens * Suspend an intent log. While in suspended mode, we still honor 1280789Sahrens * synchronous semantics, but we rely on txg_wait_synced() to do it. 1281789Sahrens * We suspend the log briefly when taking a snapshot so that the snapshot 1282789Sahrens * contains all the data it's supposed to, and has an empty intent log. 1283789Sahrens */ 1284789Sahrens int 1285789Sahrens zil_suspend(zilog_t *zilog) 1286789Sahrens { 12871807Sbonwick const zil_header_t *zh = zilog->zl_header; 1288789Sahrens 1289789Sahrens mutex_enter(&zilog->zl_lock); 12901807Sbonwick if (zh->zh_claim_txg != 0) { /* unplayed log */ 1291789Sahrens mutex_exit(&zilog->zl_lock); 1292789Sahrens return (EBUSY); 1293789Sahrens } 12941807Sbonwick if (zilog->zl_suspend++ != 0) { 12951807Sbonwick /* 12961807Sbonwick * Someone else already began a suspend. 12971807Sbonwick * Just wait for them to finish. 12981807Sbonwick */ 12991807Sbonwick while (zilog->zl_suspending) 13001807Sbonwick cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock); 13011807Sbonwick ASSERT(BP_IS_HOLE(&zh->zh_log)); 13021807Sbonwick mutex_exit(&zilog->zl_lock); 13031807Sbonwick return (0); 13041807Sbonwick } 13051807Sbonwick zilog->zl_suspending = B_TRUE; 1306789Sahrens mutex_exit(&zilog->zl_lock); 1307789Sahrens 13082638Sperrin zil_commit(zilog, UINT64_MAX, 0); 1309789Sahrens 13102638Sperrin /* 13112638Sperrin * Wait for any in-flight log writes to complete. 13122638Sperrin */ 1313789Sahrens mutex_enter(&zilog->zl_lock); 13142638Sperrin while (zilog->zl_writer) 13152638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 1316789Sahrens mutex_exit(&zilog->zl_lock); 1317789Sahrens 13181807Sbonwick zil_destroy(zilog, B_FALSE); 13191807Sbonwick 13201807Sbonwick mutex_enter(&zilog->zl_lock); 13211807Sbonwick ASSERT(BP_IS_HOLE(&zh->zh_log)); 13221807Sbonwick zilog->zl_suspending = B_FALSE; 13231807Sbonwick cv_broadcast(&zilog->zl_cv_suspend); 13241807Sbonwick mutex_exit(&zilog->zl_lock); 1325789Sahrens 1326789Sahrens return (0); 1327789Sahrens } 1328789Sahrens 1329789Sahrens void 1330789Sahrens zil_resume(zilog_t *zilog) 1331789Sahrens { 1332789Sahrens mutex_enter(&zilog->zl_lock); 1333789Sahrens ASSERT(zilog->zl_suspend != 0); 1334789Sahrens zilog->zl_suspend--; 1335789Sahrens mutex_exit(&zilog->zl_lock); 1336789Sahrens } 1337789Sahrens 1338789Sahrens typedef struct zil_replay_arg { 1339789Sahrens objset_t *zr_os; 1340789Sahrens zil_replay_func_t **zr_replay; 1341789Sahrens void *zr_arg; 1342789Sahrens uint64_t *zr_txgp; 1343789Sahrens boolean_t zr_byteswap; 1344789Sahrens char *zr_lrbuf; 1345789Sahrens } zil_replay_arg_t; 1346789Sahrens 1347789Sahrens static void 1348789Sahrens zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg) 1349789Sahrens { 1350789Sahrens zil_replay_arg_t *zr = zra; 13511807Sbonwick const zil_header_t *zh = zilog->zl_header; 1352789Sahrens uint64_t reclen = lr->lrc_reclen; 1353789Sahrens uint64_t txtype = lr->lrc_txtype; 13543063Sperrin char *name; 13553063Sperrin int pass, error, sunk; 1356789Sahrens 1357789Sahrens if (zilog->zl_stop_replay) 1358789Sahrens return; 1359789Sahrens 1360789Sahrens if (lr->lrc_txg < claim_txg) /* already committed */ 1361789Sahrens return; 1362789Sahrens 1363789Sahrens if (lr->lrc_seq <= zh->zh_replay_seq) /* already replayed */ 1364789Sahrens return; 1365789Sahrens 1366789Sahrens /* 1367789Sahrens * Make a copy of the data so we can revise and extend it. 1368789Sahrens */ 1369789Sahrens bcopy(lr, zr->zr_lrbuf, reclen); 1370789Sahrens 1371789Sahrens /* 1372789Sahrens * The log block containing this lr may have been byteswapped 1373789Sahrens * so that we can easily examine common fields like lrc_txtype. 1374789Sahrens * However, the log is a mix of different data types, and only the 1375789Sahrens * replay vectors know how to byteswap their records. Therefore, if 1376789Sahrens * the lr was byteswapped, undo it before invoking the replay vector. 1377789Sahrens */ 1378789Sahrens if (zr->zr_byteswap) 1379789Sahrens byteswap_uint64_array(zr->zr_lrbuf, reclen); 1380789Sahrens 1381789Sahrens /* 1382789Sahrens * If this is a TX_WRITE with a blkptr, suck in the data. 1383789Sahrens */ 1384789Sahrens if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) { 1385789Sahrens lr_write_t *lrw = (lr_write_t *)lr; 1386789Sahrens blkptr_t *wbp = &lrw->lr_blkptr; 1387789Sahrens uint64_t wlen = lrw->lr_length; 1388789Sahrens char *wbuf = zr->zr_lrbuf + reclen; 1389789Sahrens 1390789Sahrens if (BP_IS_HOLE(wbp)) { /* compressed to a hole */ 1391789Sahrens bzero(wbuf, wlen); 1392789Sahrens } else { 1393789Sahrens /* 1394789Sahrens * A subsequent write may have overwritten this block, 1395789Sahrens * in which case wbp may have been been freed and 1396789Sahrens * reallocated, and our read of wbp may fail with a 1397789Sahrens * checksum error. We can safely ignore this because 1398789Sahrens * the later write will provide the correct data. 1399789Sahrens */ 14001544Seschrock zbookmark_t zb; 14011544Seschrock 14021544Seschrock zb.zb_objset = dmu_objset_id(zilog->zl_os); 14031544Seschrock zb.zb_object = lrw->lr_foid; 14041544Seschrock zb.zb_level = -1; 14051544Seschrock zb.zb_blkid = lrw->lr_offset / BP_GET_LSIZE(wbp); 14061544Seschrock 1407789Sahrens (void) zio_wait(zio_read(NULL, zilog->zl_spa, 1408789Sahrens wbp, wbuf, BP_GET_LSIZE(wbp), NULL, NULL, 1409789Sahrens ZIO_PRIORITY_SYNC_READ, 14101544Seschrock ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb)); 1411789Sahrens (void) memmove(wbuf, wbuf + lrw->lr_blkoff, wlen); 1412789Sahrens } 1413789Sahrens } 1414789Sahrens 1415789Sahrens /* 1416789Sahrens * We must now do two things atomically: replay this log record, 1417789Sahrens * and update the log header to reflect the fact that we did so. 1418789Sahrens * We use the DMU's ability to assign into a specific txg to do this. 1419789Sahrens */ 14203063Sperrin for (pass = 1, sunk = B_FALSE; /* CONSTANTCONDITION */; pass++) { 1421789Sahrens uint64_t replay_txg; 1422789Sahrens dmu_tx_t *replay_tx; 1423789Sahrens 1424789Sahrens replay_tx = dmu_tx_create(zr->zr_os); 1425789Sahrens error = dmu_tx_assign(replay_tx, TXG_WAIT); 1426789Sahrens if (error) { 1427789Sahrens dmu_tx_abort(replay_tx); 1428789Sahrens break; 1429789Sahrens } 1430789Sahrens 1431789Sahrens replay_txg = dmu_tx_get_txg(replay_tx); 1432789Sahrens 1433789Sahrens if (txtype == 0 || txtype >= TX_MAX_TYPE) { 1434789Sahrens error = EINVAL; 1435789Sahrens } else { 1436789Sahrens /* 1437789Sahrens * On the first pass, arrange for the replay vector 1438789Sahrens * to fail its dmu_tx_assign(). That's the only way 1439789Sahrens * to ensure that those code paths remain well tested. 1440789Sahrens */ 1441789Sahrens *zr->zr_txgp = replay_txg - (pass == 1); 1442789Sahrens error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lrbuf, 1443789Sahrens zr->zr_byteswap); 1444789Sahrens *zr->zr_txgp = TXG_NOWAIT; 1445789Sahrens } 1446789Sahrens 1447789Sahrens if (error == 0) { 1448789Sahrens dsl_dataset_dirty(dmu_objset_ds(zr->zr_os), replay_tx); 1449789Sahrens zilog->zl_replay_seq[replay_txg & TXG_MASK] = 1450789Sahrens lr->lrc_seq; 1451789Sahrens } 1452789Sahrens 1453789Sahrens dmu_tx_commit(replay_tx); 1454789Sahrens 14553063Sperrin if (!error) 14563063Sperrin return; 14573063Sperrin 14583063Sperrin /* 14593063Sperrin * The DMU's dnode layer doesn't see removes until the txg 14603063Sperrin * commits, so a subsequent claim can spuriously fail with 14613063Sperrin * EEXIST. So if we receive any error other than ERESTART 14623063Sperrin * we try syncing out any removes then retrying the 14633063Sperrin * transaction. 14643063Sperrin */ 14653063Sperrin if (error != ERESTART && !sunk) { 14663063Sperrin txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0); 14673063Sperrin sunk = B_TRUE; 14683063Sperrin continue; /* retry */ 14693063Sperrin } 14703063Sperrin 1471789Sahrens if (error != ERESTART) 1472789Sahrens break; 1473789Sahrens 1474789Sahrens if (pass != 1) 1475789Sahrens txg_wait_open(spa_get_dsl(zilog->zl_spa), 1476789Sahrens replay_txg + 1); 1477789Sahrens 1478789Sahrens dprintf("pass %d, retrying\n", pass); 1479789Sahrens } 1480789Sahrens 14813063Sperrin ASSERT(error && error != ERESTART); 14823063Sperrin name = kmem_alloc(MAXNAMELEN, KM_SLEEP); 14833063Sperrin dmu_objset_name(zr->zr_os, name); 14843063Sperrin cmn_err(CE_WARN, "ZFS replay transaction error %d, " 14853063Sperrin "dataset %s, seq 0x%llx, txtype %llu\n", 14863063Sperrin error, name, (u_longlong_t)lr->lrc_seq, (u_longlong_t)txtype); 14873063Sperrin zilog->zl_stop_replay = 1; 14883063Sperrin kmem_free(name, MAXNAMELEN); 14893063Sperrin } 1490789Sahrens 14913063Sperrin /* ARGSUSED */ 14923063Sperrin static void 14933063Sperrin zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg) 14943063Sperrin { 14953063Sperrin zilog->zl_replay_blks++; 1496789Sahrens } 1497789Sahrens 1498789Sahrens /* 14991362Sperrin * If this dataset has a non-empty intent log, replay it and destroy it. 1500789Sahrens */ 1501789Sahrens void 1502789Sahrens zil_replay(objset_t *os, void *arg, uint64_t *txgp, 15033461Sahrens zil_replay_func_t *replay_func[TX_MAX_TYPE]) 1504789Sahrens { 1505789Sahrens zilog_t *zilog = dmu_objset_zil(os); 15061807Sbonwick const zil_header_t *zh = zilog->zl_header; 15071807Sbonwick zil_replay_arg_t zr; 15081362Sperrin 15091362Sperrin if (zil_empty(zilog)) { 15101807Sbonwick zil_destroy(zilog, B_TRUE); 15111362Sperrin return; 15121362Sperrin } 1513789Sahrens 1514789Sahrens zr.zr_os = os; 1515789Sahrens zr.zr_replay = replay_func; 1516789Sahrens zr.zr_arg = arg; 1517789Sahrens zr.zr_txgp = txgp; 15181807Sbonwick zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log); 1519789Sahrens zr.zr_lrbuf = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP); 1520789Sahrens 1521789Sahrens /* 1522789Sahrens * Wait for in-progress removes to sync before starting replay. 1523789Sahrens */ 1524789Sahrens txg_wait_synced(zilog->zl_dmu_pool, 0); 1525789Sahrens 1526789Sahrens zilog->zl_stop_replay = 0; 15273063Sperrin zilog->zl_replay_time = lbolt; 15283063Sperrin ASSERT(zilog->zl_replay_blks == 0); 15293063Sperrin (void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr, 15301807Sbonwick zh->zh_claim_txg); 1531789Sahrens kmem_free(zr.zr_lrbuf, 2 * SPA_MAXBLOCKSIZE); 1532789Sahrens 15331807Sbonwick zil_destroy(zilog, B_FALSE); 1534789Sahrens } 15351646Sperrin 15361646Sperrin /* 15371646Sperrin * Report whether all transactions are committed 15381646Sperrin */ 15391646Sperrin int 15401646Sperrin zil_is_committed(zilog_t *zilog) 15411646Sperrin { 15421646Sperrin lwb_t *lwb; 15432638Sperrin int ret; 15441646Sperrin 15452638Sperrin mutex_enter(&zilog->zl_lock); 15462638Sperrin while (zilog->zl_writer) 15472638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 15482638Sperrin 15492638Sperrin /* recent unpushed intent log transactions? */ 15502638Sperrin if (!list_is_empty(&zilog->zl_itx_list)) { 15512638Sperrin ret = B_FALSE; 15522638Sperrin goto out; 15532638Sperrin } 15542638Sperrin 15552638Sperrin /* intent log never used? */ 15562638Sperrin lwb = list_head(&zilog->zl_lwb_list); 15572638Sperrin if (lwb == NULL) { 15582638Sperrin ret = B_TRUE; 15592638Sperrin goto out; 15602638Sperrin } 15611646Sperrin 15621646Sperrin /* 15632638Sperrin * more than 1 log buffer means zil_sync() hasn't yet freed 15642638Sperrin * entries after a txg has committed 15651646Sperrin */ 15662638Sperrin if (list_next(&zilog->zl_lwb_list, lwb)) { 15672638Sperrin ret = B_FALSE; 15682638Sperrin goto out; 15692638Sperrin } 15702638Sperrin 15711646Sperrin ASSERT(zil_empty(zilog)); 15722638Sperrin ret = B_TRUE; 15732638Sperrin out: 15742638Sperrin cv_broadcast(&zilog->zl_cv_writer); 15752638Sperrin mutex_exit(&zilog->zl_lock); 15762638Sperrin return (ret); 15771646Sperrin } 1578