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 /* 225809Sperrin * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #include <sys/zfs_context.h> 27789Sahrens #include <sys/spa.h> 28789Sahrens #include <sys/dmu.h> 29789Sahrens #include <sys/zap.h> 30789Sahrens #include <sys/arc.h> 31789Sahrens #include <sys/stat.h> 32789Sahrens #include <sys/resource.h> 33789Sahrens #include <sys/zil.h> 34789Sahrens #include <sys/zil_impl.h> 35789Sahrens #include <sys/dsl_dataset.h> 36789Sahrens #include <sys/vdev.h> 373668Sgw25295 #include <sys/dmu_tx.h> 38789Sahrens 39789Sahrens /* 40789Sahrens * The zfs intent log (ZIL) saves transaction records of system calls 41789Sahrens * that change the file system in memory with enough information 42789Sahrens * to be able to replay them. These are stored in memory until 43789Sahrens * either the DMU transaction group (txg) commits them to the stable pool 44789Sahrens * and they can be discarded, or they are flushed to the stable log 45789Sahrens * (also in the pool) due to a fsync, O_DSYNC or other synchronous 46789Sahrens * requirement. In the event of a panic or power fail then those log 47789Sahrens * records (transactions) are replayed. 48789Sahrens * 49789Sahrens * There is one ZIL per file system. Its on-disk (pool) format consists 50789Sahrens * of 3 parts: 51789Sahrens * 52789Sahrens * - ZIL header 53789Sahrens * - ZIL blocks 54789Sahrens * - ZIL records 55789Sahrens * 56789Sahrens * A log record holds a system call transaction. Log blocks can 57789Sahrens * hold many log records and the blocks are chained together. 58789Sahrens * Each ZIL block contains a block pointer (blkptr_t) to the next 59789Sahrens * ZIL block in the chain. The ZIL header points to the first 60789Sahrens * block in the chain. Note there is not a fixed place in the pool 61789Sahrens * to hold blocks. They are dynamically allocated and freed as 62789Sahrens * needed from the blocks available. Figure X shows the ZIL structure: 63789Sahrens */ 64789Sahrens 65789Sahrens /* 662986Sek110237 * This global ZIL switch affects all pools 67789Sahrens */ 68789Sahrens int zil_disable = 0; /* disable intent logging */ 692986Sek110237 702986Sek110237 /* 712986Sek110237 * Tunable parameter for debugging or performance analysis. Setting 722986Sek110237 * zfs_nocacheflush will cause corruption on power loss if a volatile 732986Sek110237 * out-of-order write cache is enabled. 742986Sek110237 */ 752986Sek110237 boolean_t zfs_nocacheflush = B_FALSE; 76789Sahrens 77789Sahrens static kmem_cache_t *zil_lwb_cache; 78789Sahrens 79789Sahrens static int 80789Sahrens zil_dva_compare(const void *x1, const void *x2) 81789Sahrens { 82789Sahrens const dva_t *dva1 = x1; 83789Sahrens const dva_t *dva2 = x2; 84789Sahrens 85789Sahrens if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2)) 86789Sahrens return (-1); 87789Sahrens if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2)) 88789Sahrens return (1); 89789Sahrens 90789Sahrens if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2)) 91789Sahrens return (-1); 92789Sahrens if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2)) 93789Sahrens return (1); 94789Sahrens 95789Sahrens return (0); 96789Sahrens } 97789Sahrens 98789Sahrens static void 99789Sahrens zil_dva_tree_init(avl_tree_t *t) 100789Sahrens { 101789Sahrens avl_create(t, zil_dva_compare, sizeof (zil_dva_node_t), 102789Sahrens offsetof(zil_dva_node_t, zn_node)); 103789Sahrens } 104789Sahrens 105789Sahrens static void 106789Sahrens zil_dva_tree_fini(avl_tree_t *t) 107789Sahrens { 108789Sahrens zil_dva_node_t *zn; 109789Sahrens void *cookie = NULL; 110789Sahrens 111789Sahrens while ((zn = avl_destroy_nodes(t, &cookie)) != NULL) 112789Sahrens kmem_free(zn, sizeof (zil_dva_node_t)); 113789Sahrens 114789Sahrens avl_destroy(t); 115789Sahrens } 116789Sahrens 117789Sahrens static int 118789Sahrens zil_dva_tree_add(avl_tree_t *t, dva_t *dva) 119789Sahrens { 120789Sahrens zil_dva_node_t *zn; 121789Sahrens avl_index_t where; 122789Sahrens 123789Sahrens if (avl_find(t, dva, &where) != NULL) 124789Sahrens return (EEXIST); 125789Sahrens 126789Sahrens zn = kmem_alloc(sizeof (zil_dva_node_t), KM_SLEEP); 127789Sahrens zn->zn_dva = *dva; 128789Sahrens avl_insert(t, zn, where); 129789Sahrens 130789Sahrens return (0); 131789Sahrens } 132789Sahrens 1331807Sbonwick static zil_header_t * 1341807Sbonwick zil_header_in_syncing_context(zilog_t *zilog) 1351807Sbonwick { 1361807Sbonwick return ((zil_header_t *)zilog->zl_header); 1371807Sbonwick } 1381807Sbonwick 1391807Sbonwick static void 1401807Sbonwick zil_init_log_chain(zilog_t *zilog, blkptr_t *bp) 1411807Sbonwick { 1421807Sbonwick zio_cksum_t *zc = &bp->blk_cksum; 1431807Sbonwick 1441807Sbonwick zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL); 1451807Sbonwick zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL); 1461807Sbonwick zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os); 1471807Sbonwick zc->zc_word[ZIL_ZC_SEQ] = 1ULL; 1481807Sbonwick } 1491807Sbonwick 150789Sahrens /* 151789Sahrens * Read a log block, make sure it's valid, and byteswap it if necessary. 152789Sahrens */ 153789Sahrens static int 1541807Sbonwick zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, arc_buf_t **abufpp) 155789Sahrens { 1561807Sbonwick blkptr_t blk = *bp; 1571544Seschrock zbookmark_t zb; 1582391Smaybee uint32_t aflags = ARC_WAIT; 159789Sahrens int error; 160789Sahrens 1611807Sbonwick zb.zb_objset = bp->blk_cksum.zc_word[ZIL_ZC_OBJSET]; 1621544Seschrock zb.zb_object = 0; 1631544Seschrock zb.zb_level = -1; 1641807Sbonwick zb.zb_blkid = bp->blk_cksum.zc_word[ZIL_ZC_SEQ]; 1651807Sbonwick 1661807Sbonwick *abufpp = NULL; 1671807Sbonwick 1687046Sahrens /* 1697046Sahrens * We shouldn't be doing any scrubbing while we're doing log 1707046Sahrens * replay, it's OK to not lock. 1717046Sahrens */ 1727046Sahrens error = arc_read_nolock(NULL, zilog->zl_spa, &blk, 1731807Sbonwick arc_getbuf_func, abufpp, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL | 1742391Smaybee ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB, &aflags, &zb); 1751807Sbonwick 1761807Sbonwick if (error == 0) { 1771807Sbonwick char *data = (*abufpp)->b_data; 1781807Sbonwick uint64_t blksz = BP_GET_LSIZE(bp); 1791807Sbonwick zil_trailer_t *ztp = (zil_trailer_t *)(data + blksz) - 1; 1801807Sbonwick zio_cksum_t cksum = bp->blk_cksum; 1811544Seschrock 1821807Sbonwick /* 1837522SNeil.Perrin@Sun.COM * Validate the checksummed log block. 1847522SNeil.Perrin@Sun.COM * 1851807Sbonwick * Sequence numbers should be... sequential. The checksum 1861807Sbonwick * verifier for the next block should be bp's checksum plus 1. 1877522SNeil.Perrin@Sun.COM * 1887522SNeil.Perrin@Sun.COM * Also check the log chain linkage and size used. 1891807Sbonwick */ 1901807Sbonwick cksum.zc_word[ZIL_ZC_SEQ]++; 1911807Sbonwick 1927522SNeil.Perrin@Sun.COM if (bcmp(&cksum, &ztp->zit_next_blk.blk_cksum, 1937522SNeil.Perrin@Sun.COM sizeof (cksum)) || BP_IS_HOLE(&ztp->zit_next_blk) || 1947522SNeil.Perrin@Sun.COM (ztp->zit_nused > (blksz - sizeof (zil_trailer_t)))) { 1957522SNeil.Perrin@Sun.COM error = ECKSUM; 1967522SNeil.Perrin@Sun.COM } 1971807Sbonwick 1981807Sbonwick if (error) { 1991807Sbonwick VERIFY(arc_buf_remove_ref(*abufpp, abufpp) == 1); 2001807Sbonwick *abufpp = NULL; 2011807Sbonwick } 202789Sahrens } 203789Sahrens 2041807Sbonwick dprintf("error %d on %llu:%llu\n", error, zb.zb_objset, zb.zb_blkid); 205789Sahrens 2061807Sbonwick return (error); 207789Sahrens } 208789Sahrens 209789Sahrens /* 210789Sahrens * Parse the intent log, and call parse_func for each valid record within. 2111807Sbonwick * Return the highest sequence number. 212789Sahrens */ 2131807Sbonwick uint64_t 214789Sahrens zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func, 215789Sahrens zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg) 216789Sahrens { 2171807Sbonwick const zil_header_t *zh = zilog->zl_header; 2181807Sbonwick uint64_t claim_seq = zh->zh_claim_seq; 2191807Sbonwick uint64_t seq = 0; 2201807Sbonwick uint64_t max_seq = 0; 2211807Sbonwick blkptr_t blk = zh->zh_log; 2221807Sbonwick arc_buf_t *abuf; 223789Sahrens char *lrbuf, *lrp; 224789Sahrens zil_trailer_t *ztp; 225789Sahrens int reclen, error; 226789Sahrens 227789Sahrens if (BP_IS_HOLE(&blk)) 2281807Sbonwick return (max_seq); 229789Sahrens 230789Sahrens /* 231789Sahrens * Starting at the block pointed to by zh_log we read the log chain. 232789Sahrens * For each block in the chain we strongly check that block to 233789Sahrens * ensure its validity. We stop when an invalid block is found. 234789Sahrens * For each block pointer in the chain we call parse_blk_func(). 235789Sahrens * For each record in each valid block we call parse_lr_func(). 2361807Sbonwick * If the log has been claimed, stop if we encounter a sequence 2371807Sbonwick * number greater than the highest claimed sequence number. 238789Sahrens */ 239789Sahrens zil_dva_tree_init(&zilog->zl_dva_tree); 240789Sahrens for (;;) { 2411807Sbonwick seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 2421807Sbonwick 2431807Sbonwick if (claim_seq != 0 && seq > claim_seq) 2441807Sbonwick break; 2451807Sbonwick 2461807Sbonwick ASSERT(max_seq < seq); 2471807Sbonwick max_seq = seq; 2481807Sbonwick 2491807Sbonwick error = zil_read_log_block(zilog, &blk, &abuf); 250789Sahrens 251789Sahrens if (parse_blk_func != NULL) 252789Sahrens parse_blk_func(zilog, &blk, arg, txg); 253789Sahrens 254789Sahrens if (error) 255789Sahrens break; 256789Sahrens 2571807Sbonwick lrbuf = abuf->b_data; 258789Sahrens ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1; 259789Sahrens blk = ztp->zit_next_blk; 260789Sahrens 2611807Sbonwick if (parse_lr_func == NULL) { 2621807Sbonwick VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 263789Sahrens continue; 2641807Sbonwick } 265789Sahrens 266789Sahrens for (lrp = lrbuf; lrp < lrbuf + ztp->zit_nused; lrp += reclen) { 267789Sahrens lr_t *lr = (lr_t *)lrp; 268789Sahrens reclen = lr->lrc_reclen; 269789Sahrens ASSERT3U(reclen, >=, sizeof (lr_t)); 270789Sahrens parse_lr_func(zilog, lr, arg, txg); 271789Sahrens } 2721807Sbonwick VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 273789Sahrens } 274789Sahrens zil_dva_tree_fini(&zilog->zl_dva_tree); 2751807Sbonwick 2761807Sbonwick return (max_seq); 277789Sahrens } 278789Sahrens 279789Sahrens /* ARGSUSED */ 280789Sahrens static void 281789Sahrens zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg) 282789Sahrens { 283789Sahrens spa_t *spa = zilog->zl_spa; 284789Sahrens int err; 285789Sahrens 286789Sahrens /* 287789Sahrens * Claim log block if not already committed and not already claimed. 288789Sahrens */ 289789Sahrens if (bp->blk_birth >= first_txg && 290789Sahrens zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp)) == 0) { 291*7754SJeff.Bonwick@Sun.COM err = zio_wait(zio_claim(NULL, spa, first_txg, bp, NULL, NULL, 292*7754SJeff.Bonwick@Sun.COM ZIO_FLAG_MUSTSUCCEED)); 293789Sahrens ASSERT(err == 0); 294789Sahrens } 295789Sahrens } 296789Sahrens 297789Sahrens static void 298789Sahrens zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg) 299789Sahrens { 300789Sahrens if (lrc->lrc_txtype == TX_WRITE) { 301789Sahrens lr_write_t *lr = (lr_write_t *)lrc; 302789Sahrens zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg); 303789Sahrens } 304789Sahrens } 305789Sahrens 306789Sahrens /* ARGSUSED */ 307789Sahrens static void 308789Sahrens zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg) 309789Sahrens { 310789Sahrens zio_free_blk(zilog->zl_spa, bp, dmu_tx_get_txg(tx)); 311789Sahrens } 312789Sahrens 313789Sahrens static void 314789Sahrens zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg) 315789Sahrens { 316789Sahrens /* 317789Sahrens * If we previously claimed it, we need to free it. 318789Sahrens */ 319789Sahrens if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE) { 320789Sahrens lr_write_t *lr = (lr_write_t *)lrc; 321789Sahrens blkptr_t *bp = &lr->lr_blkptr; 322789Sahrens if (bp->blk_birth >= claim_txg && 323789Sahrens !zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp))) { 324789Sahrens (void) arc_free(NULL, zilog->zl_spa, 325789Sahrens dmu_tx_get_txg(tx), bp, NULL, NULL, ARC_WAIT); 326789Sahrens } 327789Sahrens } 328789Sahrens } 329789Sahrens 330789Sahrens /* 331789Sahrens * Create an on-disk intent log. 332789Sahrens */ 333789Sahrens static void 334789Sahrens zil_create(zilog_t *zilog) 335789Sahrens { 3361807Sbonwick const zil_header_t *zh = zilog->zl_header; 337789Sahrens lwb_t *lwb; 3381807Sbonwick uint64_t txg = 0; 3391807Sbonwick dmu_tx_t *tx = NULL; 340789Sahrens blkptr_t blk; 3411807Sbonwick int error = 0; 342789Sahrens 343789Sahrens /* 3441807Sbonwick * Wait for any previous destroy to complete. 345789Sahrens */ 3461807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 3471807Sbonwick 3481807Sbonwick ASSERT(zh->zh_claim_txg == 0); 3491807Sbonwick ASSERT(zh->zh_replay_seq == 0); 3501807Sbonwick 3511807Sbonwick blk = zh->zh_log; 352789Sahrens 353789Sahrens /* 3541807Sbonwick * If we don't already have an initial log block, allocate one now. 355789Sahrens */ 3561807Sbonwick if (BP_IS_HOLE(&blk)) { 3571807Sbonwick tx = dmu_tx_create(zilog->zl_os); 3581807Sbonwick (void) dmu_tx_assign(tx, TXG_WAIT); 3591807Sbonwick dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 3601807Sbonwick txg = dmu_tx_get_txg(tx); 3611807Sbonwick 3623063Sperrin error = zio_alloc_blk(zilog->zl_spa, ZIL_MIN_BLKSZ, &blk, 3633063Sperrin NULL, txg); 3641807Sbonwick 3651807Sbonwick if (error == 0) 3661807Sbonwick zil_init_log_chain(zilog, &blk); 3671362Sperrin } 3681807Sbonwick 3691807Sbonwick /* 3701807Sbonwick * Allocate a log write buffer (lwb) for the first log block. 3711807Sbonwick */ 372789Sahrens if (error == 0) { 373789Sahrens lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 374789Sahrens lwb->lwb_zilog = zilog; 375789Sahrens lwb->lwb_blk = blk; 376789Sahrens lwb->lwb_nused = 0; 377789Sahrens lwb->lwb_sz = BP_GET_LSIZE(&lwb->lwb_blk); 378789Sahrens lwb->lwb_buf = zio_buf_alloc(lwb->lwb_sz); 379789Sahrens lwb->lwb_max_txg = txg; 3802237Smaybee lwb->lwb_zio = NULL; 3812237Smaybee 382789Sahrens mutex_enter(&zilog->zl_lock); 383789Sahrens list_insert_tail(&zilog->zl_lwb_list, lwb); 384789Sahrens mutex_exit(&zilog->zl_lock); 385789Sahrens } 386789Sahrens 3871807Sbonwick /* 3881807Sbonwick * If we just allocated the first log block, commit our transaction 3891807Sbonwick * and wait for zil_sync() to stuff the block poiner into zh_log. 3901807Sbonwick * (zh is part of the MOS, so we cannot modify it in open context.) 3911807Sbonwick */ 3921807Sbonwick if (tx != NULL) { 3931807Sbonwick dmu_tx_commit(tx); 3941362Sperrin txg_wait_synced(zilog->zl_dmu_pool, txg); 3951807Sbonwick } 3961807Sbonwick 3971807Sbonwick ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0); 398789Sahrens } 399789Sahrens 400789Sahrens /* 401789Sahrens * In one tx, free all log blocks and clear the log header. 4021807Sbonwick * If keep_first is set, then we're replaying a log with no content. 4031807Sbonwick * We want to keep the first block, however, so that the first 4041807Sbonwick * synchronous transaction doesn't require a txg_wait_synced() 4051807Sbonwick * in zil_create(). We don't need to txg_wait_synced() here either 4061807Sbonwick * when keep_first is set, because both zil_create() and zil_destroy() 4071807Sbonwick * will wait for any in-progress destroys to complete. 408789Sahrens */ 409789Sahrens void 4101807Sbonwick zil_destroy(zilog_t *zilog, boolean_t keep_first) 411789Sahrens { 4121807Sbonwick const zil_header_t *zh = zilog->zl_header; 4131807Sbonwick lwb_t *lwb; 414789Sahrens dmu_tx_t *tx; 415789Sahrens uint64_t txg; 416789Sahrens 4171807Sbonwick /* 4181807Sbonwick * Wait for any previous destroy to complete. 4191807Sbonwick */ 4201807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 421789Sahrens 4221807Sbonwick if (BP_IS_HOLE(&zh->zh_log)) 423789Sahrens return; 424789Sahrens 425789Sahrens tx = dmu_tx_create(zilog->zl_os); 426789Sahrens (void) dmu_tx_assign(tx, TXG_WAIT); 427789Sahrens dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 428789Sahrens txg = dmu_tx_get_txg(tx); 429789Sahrens 4301807Sbonwick mutex_enter(&zilog->zl_lock); 4311807Sbonwick 4325223Sperrin /* 4335223Sperrin * It is possible for the ZIL to get the previously mounted zilog 4345223Sperrin * structure of the same dataset if quickly remounted and the dbuf 4355223Sperrin * eviction has not completed. In this case we can see a non 4365223Sperrin * empty lwb list and keep_first will be set. We fix this by 4375223Sperrin * clearing the keep_first. This will be slower but it's very rare. 4385223Sperrin */ 4395223Sperrin if (!list_is_empty(&zilog->zl_lwb_list) && keep_first) 4405223Sperrin keep_first = B_FALSE; 4415223Sperrin 4421807Sbonwick ASSERT3U(zilog->zl_destroy_txg, <, txg); 443789Sahrens zilog->zl_destroy_txg = txg; 4441807Sbonwick zilog->zl_keep_first = keep_first; 4451807Sbonwick 4461807Sbonwick if (!list_is_empty(&zilog->zl_lwb_list)) { 4471807Sbonwick ASSERT(zh->zh_claim_txg == 0); 4481807Sbonwick ASSERT(!keep_first); 4491807Sbonwick while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 4501807Sbonwick list_remove(&zilog->zl_lwb_list, lwb); 4511807Sbonwick if (lwb->lwb_buf != NULL) 4521807Sbonwick zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 4531807Sbonwick zio_free_blk(zilog->zl_spa, &lwb->lwb_blk, txg); 4541807Sbonwick kmem_cache_free(zil_lwb_cache, lwb); 4551807Sbonwick } 4561807Sbonwick } else { 4571807Sbonwick if (!keep_first) { 4581807Sbonwick (void) zil_parse(zilog, zil_free_log_block, 4591807Sbonwick zil_free_log_record, tx, zh->zh_claim_txg); 4601807Sbonwick } 4611807Sbonwick } 4622638Sperrin mutex_exit(&zilog->zl_lock); 463789Sahrens 464789Sahrens dmu_tx_commit(tx); 465789Sahrens } 466789Sahrens 4674935Sperrin /* 4684935Sperrin * zil_rollback_destroy() is only called by the rollback code. 4694935Sperrin * We already have a syncing tx. Rollback has exclusive access to the 4704935Sperrin * dataset, so we don't have to worry about concurrent zil access. 4714935Sperrin * The actual freeing of any log blocks occurs in zil_sync() later in 4724935Sperrin * this txg syncing phase. 4734935Sperrin */ 4744935Sperrin void 4754935Sperrin zil_rollback_destroy(zilog_t *zilog, dmu_tx_t *tx) 4764935Sperrin { 4774935Sperrin const zil_header_t *zh = zilog->zl_header; 4784935Sperrin uint64_t txg; 4794935Sperrin 4804935Sperrin if (BP_IS_HOLE(&zh->zh_log)) 4814935Sperrin return; 4824935Sperrin 4834935Sperrin txg = dmu_tx_get_txg(tx); 4844935Sperrin ASSERT3U(zilog->zl_destroy_txg, <, txg); 4854935Sperrin zilog->zl_destroy_txg = txg; 4864935Sperrin zilog->zl_keep_first = B_FALSE; 4874935Sperrin 4885809Sperrin /* 4895809Sperrin * Ensure there's no outstanding ZIL IO. No lwbs or just the 4905809Sperrin * unused one that allocated in advance is ok. 4915809Sperrin */ 4925809Sperrin ASSERT(zilog->zl_lwb_list.list_head.list_next == 4935809Sperrin zilog->zl_lwb_list.list_head.list_prev); 4944935Sperrin (void) zil_parse(zilog, zil_free_log_block, zil_free_log_record, 4954935Sperrin tx, zh->zh_claim_txg); 4964935Sperrin } 4974935Sperrin 4982199Sahrens int 499789Sahrens zil_claim(char *osname, void *txarg) 500789Sahrens { 501789Sahrens dmu_tx_t *tx = txarg; 502789Sahrens uint64_t first_txg = dmu_tx_get_txg(tx); 503789Sahrens zilog_t *zilog; 504789Sahrens zil_header_t *zh; 505789Sahrens objset_t *os; 506789Sahrens int error; 507789Sahrens 5086689Smaybee error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_USER, &os); 509789Sahrens if (error) { 5107294Sperrin cmn_err(CE_WARN, "can't open objset for %s", osname); 5112199Sahrens return (0); 512789Sahrens } 513789Sahrens 514789Sahrens zilog = dmu_objset_zil(os); 5151807Sbonwick zh = zil_header_in_syncing_context(zilog); 516789Sahrens 517789Sahrens /* 5181807Sbonwick * Claim all log blocks if we haven't already done so, and remember 5191807Sbonwick * the highest claimed sequence number. This ensures that if we can 5201807Sbonwick * read only part of the log now (e.g. due to a missing device), 5211807Sbonwick * but we can read the entire log later, we will not try to replay 5221807Sbonwick * or destroy beyond the last block we successfully claimed. 523789Sahrens */ 524789Sahrens ASSERT3U(zh->zh_claim_txg, <=, first_txg); 525789Sahrens if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) { 526789Sahrens zh->zh_claim_txg = first_txg; 5271807Sbonwick zh->zh_claim_seq = zil_parse(zilog, zil_claim_log_block, 5281807Sbonwick zil_claim_log_record, tx, first_txg); 529789Sahrens dsl_dataset_dirty(dmu_objset_ds(os), tx); 530789Sahrens } 5311807Sbonwick 532789Sahrens ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1)); 533789Sahrens dmu_objset_close(os); 5342199Sahrens return (0); 535789Sahrens } 536789Sahrens 5377294Sperrin /* 5387294Sperrin * Check the log by walking the log chain. 5397294Sperrin * Checksum errors are ok as they indicate the end of the chain. 5407294Sperrin * Any other error (no device or read failure) returns an error. 5417294Sperrin */ 5427294Sperrin /* ARGSUSED */ 5437294Sperrin int 5447294Sperrin zil_check_log_chain(char *osname, void *txarg) 5457294Sperrin { 5467294Sperrin zilog_t *zilog; 5477294Sperrin zil_header_t *zh; 5487294Sperrin blkptr_t blk; 5497294Sperrin arc_buf_t *abuf; 5507294Sperrin objset_t *os; 5517294Sperrin char *lrbuf; 5527294Sperrin zil_trailer_t *ztp; 5537294Sperrin int error; 5547294Sperrin 5557294Sperrin error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_USER, &os); 5567294Sperrin if (error) { 5577294Sperrin cmn_err(CE_WARN, "can't open objset for %s", osname); 5587294Sperrin return (0); 5597294Sperrin } 5607294Sperrin 5617294Sperrin zilog = dmu_objset_zil(os); 5627294Sperrin zh = zil_header_in_syncing_context(zilog); 5637294Sperrin blk = zh->zh_log; 5647294Sperrin if (BP_IS_HOLE(&blk)) { 5657294Sperrin dmu_objset_close(os); 5667294Sperrin return (0); /* no chain */ 5677294Sperrin } 5687294Sperrin 5697294Sperrin for (;;) { 5707294Sperrin error = zil_read_log_block(zilog, &blk, &abuf); 5717294Sperrin if (error) 5727294Sperrin break; 5737294Sperrin lrbuf = abuf->b_data; 5747294Sperrin ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1; 5757294Sperrin blk = ztp->zit_next_blk; 5767294Sperrin VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 5777294Sperrin } 5787294Sperrin dmu_objset_close(os); 5797294Sperrin if (error == ECKSUM) 5807294Sperrin return (0); /* normal end of chain */ 5817294Sperrin return (error); 5827294Sperrin } 5837294Sperrin 5847294Sperrin /* 5857294Sperrin * Clear a log chain 5867294Sperrin */ 5877294Sperrin /* ARGSUSED */ 5887294Sperrin int 5897294Sperrin zil_clear_log_chain(char *osname, void *txarg) 5907294Sperrin { 5917294Sperrin zilog_t *zilog; 5927294Sperrin zil_header_t *zh; 5937294Sperrin objset_t *os; 5947294Sperrin dmu_tx_t *tx; 5957294Sperrin int error; 5967294Sperrin 5977294Sperrin error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_USER, &os); 5987294Sperrin if (error) { 5997294Sperrin cmn_err(CE_WARN, "can't open objset for %s", osname); 6007294Sperrin return (0); 6017294Sperrin } 6027294Sperrin 6037294Sperrin zilog = dmu_objset_zil(os); 6047294Sperrin tx = dmu_tx_create(zilog->zl_os); 6057294Sperrin (void) dmu_tx_assign(tx, TXG_WAIT); 6067294Sperrin zh = zil_header_in_syncing_context(zilog); 6077294Sperrin BP_ZERO(&zh->zh_log); 6087294Sperrin dsl_dataset_dirty(dmu_objset_ds(os), tx); 6097294Sperrin dmu_tx_commit(tx); 6107294Sperrin dmu_objset_close(os); 6117294Sperrin return (0); 6127294Sperrin } 6137294Sperrin 6145688Sbonwick static int 6155688Sbonwick zil_vdev_compare(const void *x1, const void *x2) 616789Sahrens { 6175875Sperrin uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev; 6185875Sperrin uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev; 6195688Sbonwick 6205688Sbonwick if (v1 < v2) 6215688Sbonwick return (-1); 6225688Sbonwick if (v1 > v2) 6235688Sbonwick return (1); 6245688Sbonwick 6255688Sbonwick return (0); 6265688Sbonwick } 6275688Sbonwick 6285688Sbonwick void 6295688Sbonwick zil_add_block(zilog_t *zilog, blkptr_t *bp) 6305688Sbonwick { 6315688Sbonwick avl_tree_t *t = &zilog->zl_vdev_tree; 6325688Sbonwick avl_index_t where; 6335688Sbonwick zil_vdev_node_t *zv, zvsearch; 6345688Sbonwick int ndvas = BP_GET_NDVAS(bp); 6355688Sbonwick int i; 636789Sahrens 6372986Sek110237 if (zfs_nocacheflush) 638789Sahrens return; 639789Sahrens 6405688Sbonwick ASSERT(zilog->zl_writer); 6415688Sbonwick 6425688Sbonwick /* 6435688Sbonwick * Even though we're zl_writer, we still need a lock because the 6445688Sbonwick * zl_get_data() callbacks may have dmu_sync() done callbacks 6455688Sbonwick * that will run concurrently. 6465688Sbonwick */ 6475688Sbonwick mutex_enter(&zilog->zl_vdev_lock); 6485688Sbonwick for (i = 0; i < ndvas; i++) { 6495688Sbonwick zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]); 6505688Sbonwick if (avl_find(t, &zvsearch, &where) == NULL) { 6515688Sbonwick zv = kmem_alloc(sizeof (*zv), KM_SLEEP); 6525688Sbonwick zv->zv_vdev = zvsearch.zv_vdev; 6535688Sbonwick avl_insert(t, zv, where); 6543063Sperrin } 6553063Sperrin } 6565688Sbonwick mutex_exit(&zilog->zl_vdev_lock); 6573063Sperrin } 6583063Sperrin 659789Sahrens void 6602638Sperrin zil_flush_vdevs(zilog_t *zilog) 661789Sahrens { 6623063Sperrin spa_t *spa = zilog->zl_spa; 6635688Sbonwick avl_tree_t *t = &zilog->zl_vdev_tree; 6645688Sbonwick void *cookie = NULL; 6655688Sbonwick zil_vdev_node_t *zv; 6665688Sbonwick zio_t *zio; 6673063Sperrin 6683063Sperrin ASSERT(zilog->zl_writer); 669789Sahrens 6705688Sbonwick /* 6715688Sbonwick * We don't need zl_vdev_lock here because we're the zl_writer, 6725688Sbonwick * and all zl_get_data() callbacks are done. 6735688Sbonwick */ 6745688Sbonwick if (avl_numnodes(t) == 0) 6755688Sbonwick return; 6765688Sbonwick 677*7754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 6785688Sbonwick 679*7754SJeff.Bonwick@Sun.COM zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); 6805688Sbonwick 6815688Sbonwick while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) { 6825688Sbonwick vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev); 6835688Sbonwick if (vd != NULL) 6845688Sbonwick zio_flush(zio, vd); 6855688Sbonwick kmem_free(zv, sizeof (*zv)); 6863063Sperrin } 687789Sahrens 688789Sahrens /* 689789Sahrens * Wait for all the flushes to complete. Not all devices actually 690789Sahrens * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails. 691789Sahrens */ 6925688Sbonwick (void) zio_wait(zio); 6935688Sbonwick 694*7754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 695789Sahrens } 696789Sahrens 697789Sahrens /* 698789Sahrens * Function called when a log block write completes 699789Sahrens */ 700789Sahrens static void 701789Sahrens zil_lwb_write_done(zio_t *zio) 702789Sahrens { 703789Sahrens lwb_t *lwb = zio->io_private; 704789Sahrens zilog_t *zilog = lwb->lwb_zilog; 705789Sahrens 706*7754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF); 707*7754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_CHECKSUM(zio->io_bp) == ZIO_CHECKSUM_ZILOG); 708*7754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_TYPE(zio->io_bp) == DMU_OT_INTENT_LOG); 709*7754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 710*7754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_BYTEORDER(zio->io_bp) == ZFS_HOST_BYTEORDER); 711*7754SJeff.Bonwick@Sun.COM ASSERT(!BP_IS_GANG(zio->io_bp)); 712*7754SJeff.Bonwick@Sun.COM ASSERT(!BP_IS_HOLE(zio->io_bp)); 713*7754SJeff.Bonwick@Sun.COM ASSERT(zio->io_bp->blk_fill == 0); 714*7754SJeff.Bonwick@Sun.COM 715789Sahrens /* 716789Sahrens * Now that we've written this log block, we have a stable pointer 717789Sahrens * to the next block in the chain, so it's OK to let the txg in 718789Sahrens * which we allocated the next block sync. 719789Sahrens */ 720789Sahrens txg_rele_to_sync(&lwb->lwb_txgh); 721789Sahrens 722789Sahrens zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 723789Sahrens mutex_enter(&zilog->zl_lock); 724789Sahrens lwb->lwb_buf = NULL; 7254527Sperrin if (zio->io_error) 726789Sahrens zilog->zl_log_error = B_TRUE; 727789Sahrens mutex_exit(&zilog->zl_lock); 728789Sahrens } 729789Sahrens 730789Sahrens /* 7312237Smaybee * Initialize the io for a log block. 7322237Smaybee */ 7332237Smaybee static void 7342237Smaybee zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb) 7352237Smaybee { 7362237Smaybee zbookmark_t zb; 7372237Smaybee 7382237Smaybee zb.zb_objset = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET]; 7392237Smaybee zb.zb_object = 0; 7402237Smaybee zb.zb_level = -1; 7412237Smaybee zb.zb_blkid = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 7422237Smaybee 7432638Sperrin if (zilog->zl_root_zio == NULL) { 7442638Sperrin zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL, 7452638Sperrin ZIO_FLAG_CANFAIL); 7462638Sperrin } 7473063Sperrin if (lwb->lwb_zio == NULL) { 7483063Sperrin lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa, 749*7754SJeff.Bonwick@Sun.COM 0, &lwb->lwb_blk, lwb->lwb_buf, 7503063Sperrin lwb->lwb_sz, zil_lwb_write_done, lwb, 7514527Sperrin ZIO_PRIORITY_LOG_WRITE, ZIO_FLAG_CANFAIL, &zb); 7523063Sperrin } 7532237Smaybee } 7542237Smaybee 7552237Smaybee /* 756789Sahrens * Start a log block write and advance to the next log block. 757789Sahrens * Calls are serialized. 758789Sahrens */ 759789Sahrens static lwb_t * 760789Sahrens zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb) 761789Sahrens { 762789Sahrens lwb_t *nlwb; 763789Sahrens zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1; 7641807Sbonwick spa_t *spa = zilog->zl_spa; 7651807Sbonwick blkptr_t *bp = &ztp->zit_next_blk; 766789Sahrens uint64_t txg; 767789Sahrens uint64_t zil_blksz; 768789Sahrens int error; 769789Sahrens 770789Sahrens ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb)); 771789Sahrens 772789Sahrens /* 773789Sahrens * Allocate the next block and save its address in this block 774789Sahrens * before writing it in order to establish the log chain. 775789Sahrens * Note that if the allocation of nlwb synced before we wrote 776789Sahrens * the block that points at it (lwb), we'd leak it if we crashed. 777789Sahrens * Therefore, we don't do txg_rele_to_sync() until zil_lwb_write_done(). 778789Sahrens */ 779789Sahrens txg = txg_hold_open(zilog->zl_dmu_pool, &lwb->lwb_txgh); 780789Sahrens txg_rele_to_quiesce(&lwb->lwb_txgh); 781789Sahrens 782789Sahrens /* 7831141Sperrin * Pick a ZIL blocksize. We request a size that is the 7841141Sperrin * maximum of the previous used size, the current used size and 7851141Sperrin * the amount waiting in the queue. 786789Sahrens */ 7872237Smaybee zil_blksz = MAX(zilog->zl_prev_used, 7882237Smaybee zilog->zl_cur_used + sizeof (*ztp)); 7891141Sperrin zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp)); 7901842Sperrin zil_blksz = P2ROUNDUP_TYPED(zil_blksz, ZIL_MIN_BLKSZ, uint64_t); 7911141Sperrin if (zil_blksz > ZIL_MAX_BLKSZ) 7921141Sperrin zil_blksz = ZIL_MAX_BLKSZ; 793789Sahrens 7943063Sperrin BP_ZERO(bp); 7953063Sperrin /* pass the old blkptr in order to spread log blocks across devs */ 7963063Sperrin error = zio_alloc_blk(spa, zil_blksz, bp, &lwb->lwb_blk, txg); 797789Sahrens if (error) { 7983668Sgw25295 dmu_tx_t *tx = dmu_tx_create_assigned(zilog->zl_dmu_pool, txg); 7993668Sgw25295 8001544Seschrock /* 8013668Sgw25295 * We dirty the dataset to ensure that zil_sync() will 8023668Sgw25295 * be called to remove this lwb from our zl_lwb_list. 8033668Sgw25295 * Failing to do so, may leave an lwb with a NULL lwb_buf 8043668Sgw25295 * hanging around on the zl_lwb_list. 8053668Sgw25295 */ 8063668Sgw25295 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 8073848Sgw25295 dmu_tx_commit(tx); 8083668Sgw25295 8093668Sgw25295 /* 8103668Sgw25295 * Since we've just experienced an allocation failure so we 8113668Sgw25295 * terminate the current lwb and send it on its way. 8123668Sgw25295 */ 8133668Sgw25295 ztp->zit_pad = 0; 8143668Sgw25295 ztp->zit_nused = lwb->lwb_nused; 8153668Sgw25295 ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum; 8163668Sgw25295 zio_nowait(lwb->lwb_zio); 8173668Sgw25295 8183668Sgw25295 /* 8191544Seschrock * By returning NULL the caller will call tx_wait_synced() 8201544Seschrock */ 821789Sahrens return (NULL); 822789Sahrens } 823789Sahrens 8241807Sbonwick ASSERT3U(bp->blk_birth, ==, txg); 8251544Seschrock ztp->zit_pad = 0; 826789Sahrens ztp->zit_nused = lwb->lwb_nused; 827789Sahrens ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum; 8281807Sbonwick bp->blk_cksum = lwb->lwb_blk.blk_cksum; 8291807Sbonwick bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++; 830789Sahrens 831789Sahrens /* 832789Sahrens * Allocate a new log write buffer (lwb). 833789Sahrens */ 834789Sahrens nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 835789Sahrens 836789Sahrens nlwb->lwb_zilog = zilog; 8371807Sbonwick nlwb->lwb_blk = *bp; 838789Sahrens nlwb->lwb_nused = 0; 839789Sahrens nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk); 840789Sahrens nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz); 841789Sahrens nlwb->lwb_max_txg = txg; 8422237Smaybee nlwb->lwb_zio = NULL; 843789Sahrens 844789Sahrens /* 8453063Sperrin * Put new lwb at the end of the log chain 846789Sahrens */ 847789Sahrens mutex_enter(&zilog->zl_lock); 848789Sahrens list_insert_tail(&zilog->zl_lwb_list, nlwb); 8493063Sperrin mutex_exit(&zilog->zl_lock); 8503063Sperrin 8515688Sbonwick /* Record the block for later vdev flushing */ 8525688Sbonwick zil_add_block(zilog, &lwb->lwb_blk); 853789Sahrens 854789Sahrens /* 8552237Smaybee * kick off the write for the old log block 856789Sahrens */ 8572237Smaybee dprintf_bp(&lwb->lwb_blk, "lwb %p txg %llu: ", lwb, txg); 8583063Sperrin ASSERT(lwb->lwb_zio); 8592237Smaybee zio_nowait(lwb->lwb_zio); 860789Sahrens 861789Sahrens return (nlwb); 862789Sahrens } 863789Sahrens 864789Sahrens static lwb_t * 865789Sahrens zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb) 866789Sahrens { 867789Sahrens lr_t *lrc = &itx->itx_lr; /* common log record */ 8682237Smaybee lr_write_t *lr = (lr_write_t *)lrc; 869789Sahrens uint64_t txg = lrc->lrc_txg; 870789Sahrens uint64_t reclen = lrc->lrc_reclen; 8712237Smaybee uint64_t dlen; 872789Sahrens 873789Sahrens if (lwb == NULL) 874789Sahrens return (NULL); 875789Sahrens ASSERT(lwb->lwb_buf != NULL); 876789Sahrens 8772237Smaybee if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY) 8782237Smaybee dlen = P2ROUNDUP_TYPED( 8792237Smaybee lr->lr_length, sizeof (uint64_t), uint64_t); 8802237Smaybee else 8812237Smaybee dlen = 0; 8821669Sperrin 8831669Sperrin zilog->zl_cur_used += (reclen + dlen); 8841669Sperrin 8853063Sperrin zil_lwb_write_init(zilog, lwb); 8863063Sperrin 8871669Sperrin /* 8881669Sperrin * If this record won't fit in the current log block, start a new one. 8891669Sperrin */ 8901669Sperrin if (lwb->lwb_nused + reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) { 8911669Sperrin lwb = zil_lwb_write_start(zilog, lwb); 8922237Smaybee if (lwb == NULL) 8931669Sperrin return (NULL); 8943063Sperrin zil_lwb_write_init(zilog, lwb); 8951669Sperrin ASSERT(lwb->lwb_nused == 0); 8961669Sperrin if (reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) { 8971669Sperrin txg_wait_synced(zilog->zl_dmu_pool, txg); 898789Sahrens return (lwb); 899789Sahrens } 900789Sahrens } 901789Sahrens 9022638Sperrin /* 9032638Sperrin * Update the lrc_seq, to be log record sequence number. See zil.h 9042638Sperrin * Then copy the record to the log buffer. 9052638Sperrin */ 9062638Sperrin lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */ 907789Sahrens bcopy(lrc, lwb->lwb_buf + lwb->lwb_nused, reclen); 9082237Smaybee 9092237Smaybee /* 9102237Smaybee * If it's a write, fetch the data or get its blkptr as appropriate. 9112237Smaybee */ 9122237Smaybee if (lrc->lrc_txtype == TX_WRITE) { 9132237Smaybee if (txg > spa_freeze_txg(zilog->zl_spa)) 9142237Smaybee txg_wait_synced(zilog->zl_dmu_pool, txg); 9152237Smaybee if (itx->itx_wr_state != WR_COPIED) { 9162237Smaybee char *dbuf; 9172237Smaybee int error; 9182237Smaybee 9192237Smaybee /* alignment is guaranteed */ 9202237Smaybee lr = (lr_write_t *)(lwb->lwb_buf + lwb->lwb_nused); 9212237Smaybee if (dlen) { 9222237Smaybee ASSERT(itx->itx_wr_state == WR_NEED_COPY); 9232237Smaybee dbuf = lwb->lwb_buf + lwb->lwb_nused + reclen; 9242237Smaybee lr->lr_common.lrc_reclen += dlen; 9252237Smaybee } else { 9262237Smaybee ASSERT(itx->itx_wr_state == WR_INDIRECT); 9272237Smaybee dbuf = NULL; 9282237Smaybee } 9292237Smaybee error = zilog->zl_get_data( 9302237Smaybee itx->itx_private, lr, dbuf, lwb->lwb_zio); 9312237Smaybee if (error) { 9322237Smaybee ASSERT(error == ENOENT || error == EEXIST || 9332237Smaybee error == EALREADY); 9342237Smaybee return (lwb); 9352237Smaybee } 9362237Smaybee } 9371669Sperrin } 9382237Smaybee 9392237Smaybee lwb->lwb_nused += reclen + dlen; 940789Sahrens lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg); 941789Sahrens ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb)); 942789Sahrens ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0); 943789Sahrens 944789Sahrens return (lwb); 945789Sahrens } 946789Sahrens 947789Sahrens itx_t * 9485331Samw zil_itx_create(uint64_t txtype, size_t lrsize) 949789Sahrens { 950789Sahrens itx_t *itx; 951789Sahrens 9521842Sperrin lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t); 953789Sahrens 954789Sahrens itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP); 955789Sahrens itx->itx_lr.lrc_txtype = txtype; 956789Sahrens itx->itx_lr.lrc_reclen = lrsize; 9576101Sperrin itx->itx_sod = lrsize; /* if write & WR_NEED_COPY will be increased */ 958789Sahrens itx->itx_lr.lrc_seq = 0; /* defensive */ 959789Sahrens 960789Sahrens return (itx); 961789Sahrens } 962789Sahrens 963789Sahrens uint64_t 964789Sahrens zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx) 965789Sahrens { 966789Sahrens uint64_t seq; 967789Sahrens 968789Sahrens ASSERT(itx->itx_lr.lrc_seq == 0); 969789Sahrens 970789Sahrens mutex_enter(&zilog->zl_lock); 971789Sahrens list_insert_tail(&zilog->zl_itx_list, itx); 9726101Sperrin zilog->zl_itx_list_sz += itx->itx_sod; 973789Sahrens itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx); 974789Sahrens itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq; 975789Sahrens mutex_exit(&zilog->zl_lock); 976789Sahrens 977789Sahrens return (seq); 978789Sahrens } 979789Sahrens 980789Sahrens /* 981789Sahrens * Free up all in-memory intent log transactions that have now been synced. 982789Sahrens */ 983789Sahrens static void 984789Sahrens zil_itx_clean(zilog_t *zilog) 985789Sahrens { 986789Sahrens uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa); 987789Sahrens uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa); 9883778Sjohansen list_t clean_list; 989789Sahrens itx_t *itx; 990789Sahrens 9913778Sjohansen list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node)); 9923778Sjohansen 993789Sahrens mutex_enter(&zilog->zl_lock); 9942638Sperrin /* wait for a log writer to finish walking list */ 9952638Sperrin while (zilog->zl_writer) { 9962638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 9972638Sperrin } 9983778Sjohansen 9993778Sjohansen /* 10003778Sjohansen * Move the sync'd log transactions to a separate list so we can call 10013778Sjohansen * kmem_free without holding the zl_lock. 10023778Sjohansen * 10033778Sjohansen * There is no need to set zl_writer as we don't drop zl_lock here 10043778Sjohansen */ 1005789Sahrens while ((itx = list_head(&zilog->zl_itx_list)) != NULL && 1006789Sahrens itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) { 1007789Sahrens list_remove(&zilog->zl_itx_list, itx); 10086101Sperrin zilog->zl_itx_list_sz -= itx->itx_sod; 10093778Sjohansen list_insert_tail(&clean_list, itx); 10103778Sjohansen } 10113778Sjohansen cv_broadcast(&zilog->zl_cv_writer); 10123778Sjohansen mutex_exit(&zilog->zl_lock); 10133778Sjohansen 10143778Sjohansen /* destroy sync'd log transactions */ 10153778Sjohansen while ((itx = list_head(&clean_list)) != NULL) { 10163778Sjohansen list_remove(&clean_list, itx); 1017789Sahrens kmem_free(itx, offsetof(itx_t, itx_lr) 1018789Sahrens + itx->itx_lr.lrc_reclen); 1019789Sahrens } 10203778Sjohansen list_destroy(&clean_list); 1021789Sahrens } 1022789Sahrens 10232638Sperrin /* 10243063Sperrin * If there are any in-memory intent log transactions which have now been 10253063Sperrin * synced then start up a taskq to free them. 10262638Sperrin */ 1027789Sahrens void 1028789Sahrens zil_clean(zilog_t *zilog) 1029789Sahrens { 10303063Sperrin itx_t *itx; 10313063Sperrin 1032789Sahrens mutex_enter(&zilog->zl_lock); 10333063Sperrin itx = list_head(&zilog->zl_itx_list); 10343063Sperrin if ((itx != NULL) && 10353063Sperrin (itx->itx_lr.lrc_txg <= spa_last_synced_txg(zilog->zl_spa))) { 1036789Sahrens (void) taskq_dispatch(zilog->zl_clean_taskq, 1037789Sahrens (void (*)(void *))zil_itx_clean, zilog, TQ_NOSLEEP); 10383063Sperrin } 1039789Sahrens mutex_exit(&zilog->zl_lock); 1040789Sahrens } 1041789Sahrens 1042*7754SJeff.Bonwick@Sun.COM static void 10432638Sperrin zil_commit_writer(zilog_t *zilog, uint64_t seq, uint64_t foid) 1044789Sahrens { 1045789Sahrens uint64_t txg; 10463063Sperrin uint64_t commit_seq = 0; 10472638Sperrin itx_t *itx, *itx_next = (itx_t *)-1; 1048789Sahrens lwb_t *lwb; 1049789Sahrens spa_t *spa; 1050789Sahrens 10512638Sperrin zilog->zl_writer = B_TRUE; 1052*7754SJeff.Bonwick@Sun.COM ASSERT(zilog->zl_root_zio == NULL); 1053789Sahrens spa = zilog->zl_spa; 1054789Sahrens 1055789Sahrens if (zilog->zl_suspend) { 1056789Sahrens lwb = NULL; 1057789Sahrens } else { 1058789Sahrens lwb = list_tail(&zilog->zl_lwb_list); 1059789Sahrens if (lwb == NULL) { 10602638Sperrin /* 10612638Sperrin * Return if there's nothing to flush before we 10622638Sperrin * dirty the fs by calling zil_create() 10632638Sperrin */ 10642638Sperrin if (list_is_empty(&zilog->zl_itx_list)) { 10652638Sperrin zilog->zl_writer = B_FALSE; 10662638Sperrin return; 10672638Sperrin } 1068789Sahrens mutex_exit(&zilog->zl_lock); 1069789Sahrens zil_create(zilog); 1070789Sahrens mutex_enter(&zilog->zl_lock); 1071789Sahrens lwb = list_tail(&zilog->zl_lwb_list); 1072789Sahrens } 1073789Sahrens } 1074789Sahrens 10753063Sperrin /* Loop through in-memory log transactions filling log blocks. */ 10762638Sperrin DTRACE_PROBE1(zil__cw1, zilog_t *, zilog); 1077789Sahrens for (;;) { 10782638Sperrin /* 10792638Sperrin * Find the next itx to push: 10802638Sperrin * Push all transactions related to specified foid and all 10812638Sperrin * other transactions except TX_WRITE, TX_TRUNCATE, 10822638Sperrin * TX_SETATTR and TX_ACL for all other files. 10832638Sperrin */ 10842638Sperrin if (itx_next != (itx_t *)-1) 10852638Sperrin itx = itx_next; 10862638Sperrin else 10872638Sperrin itx = list_head(&zilog->zl_itx_list); 10882638Sperrin for (; itx != NULL; itx = list_next(&zilog->zl_itx_list, itx)) { 10892638Sperrin if (foid == 0) /* push all foids? */ 10902638Sperrin break; 10913063Sperrin if (itx->itx_sync) /* push all O_[D]SYNC */ 10923063Sperrin break; 10932638Sperrin switch (itx->itx_lr.lrc_txtype) { 10942638Sperrin case TX_SETATTR: 10952638Sperrin case TX_WRITE: 10962638Sperrin case TX_TRUNCATE: 10972638Sperrin case TX_ACL: 10982638Sperrin /* lr_foid is same offset for these records */ 10992638Sperrin if (((lr_write_t *)&itx->itx_lr)->lr_foid 11002638Sperrin != foid) { 11012638Sperrin continue; /* skip this record */ 11022638Sperrin } 11032638Sperrin } 11042638Sperrin break; 11052638Sperrin } 1106789Sahrens if (itx == NULL) 1107789Sahrens break; 1108789Sahrens 1109789Sahrens if ((itx->itx_lr.lrc_seq > seq) && 11102638Sperrin ((lwb == NULL) || (lwb->lwb_nused == 0) || 11116101Sperrin (lwb->lwb_nused + itx->itx_sod > ZIL_BLK_DATA_SZ(lwb)))) { 1112789Sahrens break; 11133063Sperrin } 1114789Sahrens 11152638Sperrin /* 11162638Sperrin * Save the next pointer. Even though we soon drop 11172638Sperrin * zl_lock all threads that may change the list 11182638Sperrin * (another writer or zil_itx_clean) can't do so until 11192638Sperrin * they have zl_writer. 11202638Sperrin */ 11212638Sperrin itx_next = list_next(&zilog->zl_itx_list, itx); 1122789Sahrens list_remove(&zilog->zl_itx_list, itx); 11236101Sperrin zilog->zl_itx_list_sz -= itx->itx_sod; 11243063Sperrin mutex_exit(&zilog->zl_lock); 1125789Sahrens txg = itx->itx_lr.lrc_txg; 1126789Sahrens ASSERT(txg); 1127789Sahrens 1128789Sahrens if (txg > spa_last_synced_txg(spa) || 1129789Sahrens txg > spa_freeze_txg(spa)) 1130789Sahrens lwb = zil_lwb_commit(zilog, itx, lwb); 1131789Sahrens kmem_free(itx, offsetof(itx_t, itx_lr) 1132789Sahrens + itx->itx_lr.lrc_reclen); 1133789Sahrens mutex_enter(&zilog->zl_lock); 1134789Sahrens } 11352638Sperrin DTRACE_PROBE1(zil__cw2, zilog_t *, zilog); 11363063Sperrin /* determine commit sequence number */ 11373063Sperrin itx = list_head(&zilog->zl_itx_list); 11383063Sperrin if (itx) 11393063Sperrin commit_seq = itx->itx_lr.lrc_seq; 11403063Sperrin else 11413063Sperrin commit_seq = zilog->zl_itx_seq; 1142789Sahrens mutex_exit(&zilog->zl_lock); 1143789Sahrens 1144789Sahrens /* write the last block out */ 11453063Sperrin if (lwb != NULL && lwb->lwb_zio != NULL) 1146789Sahrens lwb = zil_lwb_write_start(zilog, lwb); 1147789Sahrens 11481141Sperrin zilog->zl_prev_used = zilog->zl_cur_used; 11491141Sperrin zilog->zl_cur_used = 0; 11501141Sperrin 11512638Sperrin /* 11522638Sperrin * Wait if necessary for the log blocks to be on stable storage. 11532638Sperrin */ 11542638Sperrin if (zilog->zl_root_zio) { 11552638Sperrin DTRACE_PROBE1(zil__cw3, zilog_t *, zilog); 11562638Sperrin (void) zio_wait(zilog->zl_root_zio); 1157*7754SJeff.Bonwick@Sun.COM zilog->zl_root_zio = NULL; 11582638Sperrin DTRACE_PROBE1(zil__cw4, zilog_t *, zilog); 11595688Sbonwick zil_flush_vdevs(zilog); 1160789Sahrens } 11611141Sperrin 1162789Sahrens if (zilog->zl_log_error || lwb == NULL) { 1163789Sahrens zilog->zl_log_error = 0; 1164789Sahrens txg_wait_synced(zilog->zl_dmu_pool, 0); 1165789Sahrens } 11663063Sperrin 11673063Sperrin mutex_enter(&zilog->zl_lock); 11681141Sperrin zilog->zl_writer = B_FALSE; 11693063Sperrin 11703063Sperrin ASSERT3U(commit_seq, >=, zilog->zl_commit_seq); 11713063Sperrin zilog->zl_commit_seq = commit_seq; 11722638Sperrin } 11732638Sperrin 11742638Sperrin /* 11752638Sperrin * Push zfs transactions to stable storage up to the supplied sequence number. 11762638Sperrin * If foid is 0 push out all transactions, otherwise push only those 11772638Sperrin * for that file or might have been used to create that file. 11782638Sperrin */ 11792638Sperrin void 11802638Sperrin zil_commit(zilog_t *zilog, uint64_t seq, uint64_t foid) 11812638Sperrin { 11822638Sperrin if (zilog == NULL || seq == 0) 11832638Sperrin return; 11842638Sperrin 11852638Sperrin mutex_enter(&zilog->zl_lock); 11862638Sperrin 11872638Sperrin seq = MIN(seq, zilog->zl_itx_seq); /* cap seq at largest itx seq */ 11882638Sperrin 11893063Sperrin while (zilog->zl_writer) { 11902638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 11913063Sperrin if (seq < zilog->zl_commit_seq) { 11923063Sperrin mutex_exit(&zilog->zl_lock); 11933063Sperrin return; 11943063Sperrin } 11953063Sperrin } 11962638Sperrin zil_commit_writer(zilog, seq, foid); /* drops zl_lock */ 11973063Sperrin /* wake up others waiting on the commit */ 11983063Sperrin cv_broadcast(&zilog->zl_cv_writer); 11993063Sperrin mutex_exit(&zilog->zl_lock); 1200789Sahrens } 1201789Sahrens 1202789Sahrens /* 1203789Sahrens * Called in syncing context to free committed log blocks and update log header. 1204789Sahrens */ 1205789Sahrens void 1206789Sahrens zil_sync(zilog_t *zilog, dmu_tx_t *tx) 1207789Sahrens { 12081807Sbonwick zil_header_t *zh = zil_header_in_syncing_context(zilog); 1209789Sahrens uint64_t txg = dmu_tx_get_txg(tx); 1210789Sahrens spa_t *spa = zilog->zl_spa; 1211789Sahrens lwb_t *lwb; 1212789Sahrens 12131807Sbonwick mutex_enter(&zilog->zl_lock); 12141807Sbonwick 1215789Sahrens ASSERT(zilog->zl_stop_sync == 0); 1216789Sahrens 12171807Sbonwick zh->zh_replay_seq = zilog->zl_replay_seq[txg & TXG_MASK]; 1218789Sahrens 1219789Sahrens if (zilog->zl_destroy_txg == txg) { 12201807Sbonwick blkptr_t blk = zh->zh_log; 12211807Sbonwick 12221807Sbonwick ASSERT(list_head(&zilog->zl_lwb_list) == NULL); 12231807Sbonwick ASSERT(spa_sync_pass(spa) == 1); 12241807Sbonwick 12251807Sbonwick bzero(zh, sizeof (zil_header_t)); 1226789Sahrens bzero(zilog->zl_replay_seq, sizeof (zilog->zl_replay_seq)); 12271807Sbonwick 12281807Sbonwick if (zilog->zl_keep_first) { 12291807Sbonwick /* 12301807Sbonwick * If this block was part of log chain that couldn't 12311807Sbonwick * be claimed because a device was missing during 12321807Sbonwick * zil_claim(), but that device later returns, 12331807Sbonwick * then this block could erroneously appear valid. 12341807Sbonwick * To guard against this, assign a new GUID to the new 12351807Sbonwick * log chain so it doesn't matter what blk points to. 12361807Sbonwick */ 12371807Sbonwick zil_init_log_chain(zilog, &blk); 12381807Sbonwick zh->zh_log = blk; 12391807Sbonwick } 1240789Sahrens } 1241789Sahrens 1242789Sahrens for (;;) { 1243789Sahrens lwb = list_head(&zilog->zl_lwb_list); 1244789Sahrens if (lwb == NULL) { 1245789Sahrens mutex_exit(&zilog->zl_lock); 1246789Sahrens return; 1247789Sahrens } 12482638Sperrin zh->zh_log = lwb->lwb_blk; 1249789Sahrens if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg) 1250789Sahrens break; 1251789Sahrens list_remove(&zilog->zl_lwb_list, lwb); 1252789Sahrens zio_free_blk(spa, &lwb->lwb_blk, txg); 1253789Sahrens kmem_cache_free(zil_lwb_cache, lwb); 12543668Sgw25295 12553668Sgw25295 /* 12563668Sgw25295 * If we don't have anything left in the lwb list then 12573668Sgw25295 * we've had an allocation failure and we need to zero 12583668Sgw25295 * out the zil_header blkptr so that we don't end 12593668Sgw25295 * up freeing the same block twice. 12603668Sgw25295 */ 12613668Sgw25295 if (list_head(&zilog->zl_lwb_list) == NULL) 12623668Sgw25295 BP_ZERO(&zh->zh_log); 1263789Sahrens } 1264789Sahrens mutex_exit(&zilog->zl_lock); 1265789Sahrens } 1266789Sahrens 1267789Sahrens void 1268789Sahrens zil_init(void) 1269789Sahrens { 1270789Sahrens zil_lwb_cache = kmem_cache_create("zil_lwb_cache", 12712856Snd150628 sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0); 1272789Sahrens } 1273789Sahrens 1274789Sahrens void 1275789Sahrens zil_fini(void) 1276789Sahrens { 1277789Sahrens kmem_cache_destroy(zil_lwb_cache); 1278789Sahrens } 1279789Sahrens 1280789Sahrens zilog_t * 1281789Sahrens zil_alloc(objset_t *os, zil_header_t *zh_phys) 1282789Sahrens { 1283789Sahrens zilog_t *zilog; 1284789Sahrens 1285789Sahrens zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP); 1286789Sahrens 1287789Sahrens zilog->zl_header = zh_phys; 1288789Sahrens zilog->zl_os = os; 1289789Sahrens zilog->zl_spa = dmu_objset_spa(os); 1290789Sahrens zilog->zl_dmu_pool = dmu_objset_pool(os); 12911807Sbonwick zilog->zl_destroy_txg = TXG_INITIAL - 1; 1292789Sahrens 12932856Snd150628 mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL); 12942856Snd150628 1295789Sahrens list_create(&zilog->zl_itx_list, sizeof (itx_t), 1296789Sahrens offsetof(itx_t, itx_node)); 1297789Sahrens 1298789Sahrens list_create(&zilog->zl_lwb_list, sizeof (lwb_t), 1299789Sahrens offsetof(lwb_t, lwb_node)); 1300789Sahrens 13015688Sbonwick mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL); 13025688Sbonwick 13035688Sbonwick avl_create(&zilog->zl_vdev_tree, zil_vdev_compare, 13045688Sbonwick sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node)); 1305789Sahrens 13065913Sperrin cv_init(&zilog->zl_cv_writer, NULL, CV_DEFAULT, NULL); 13075913Sperrin cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL); 13085913Sperrin 1309789Sahrens return (zilog); 1310789Sahrens } 1311789Sahrens 1312789Sahrens void 1313789Sahrens zil_free(zilog_t *zilog) 1314789Sahrens { 1315789Sahrens lwb_t *lwb; 1316789Sahrens 1317789Sahrens zilog->zl_stop_sync = 1; 1318789Sahrens 1319789Sahrens while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 1320789Sahrens list_remove(&zilog->zl_lwb_list, lwb); 1321789Sahrens if (lwb->lwb_buf != NULL) 1322789Sahrens zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 1323789Sahrens kmem_cache_free(zil_lwb_cache, lwb); 1324789Sahrens } 1325789Sahrens list_destroy(&zilog->zl_lwb_list); 1326789Sahrens 13275688Sbonwick avl_destroy(&zilog->zl_vdev_tree); 13285688Sbonwick mutex_destroy(&zilog->zl_vdev_lock); 1329789Sahrens 1330789Sahrens ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1331789Sahrens list_destroy(&zilog->zl_itx_list); 13322856Snd150628 mutex_destroy(&zilog->zl_lock); 1333789Sahrens 13345913Sperrin cv_destroy(&zilog->zl_cv_writer); 13355913Sperrin cv_destroy(&zilog->zl_cv_suspend); 13365913Sperrin 1337789Sahrens kmem_free(zilog, sizeof (zilog_t)); 1338789Sahrens } 1339789Sahrens 1340789Sahrens /* 13411646Sperrin * return true if the initial log block is not valid 13421362Sperrin */ 13437522SNeil.Perrin@Sun.COM static boolean_t 13441362Sperrin zil_empty(zilog_t *zilog) 13451362Sperrin { 13461807Sbonwick const zil_header_t *zh = zilog->zl_header; 13471807Sbonwick arc_buf_t *abuf = NULL; 13481362Sperrin 13491807Sbonwick if (BP_IS_HOLE(&zh->zh_log)) 13507522SNeil.Perrin@Sun.COM return (B_TRUE); 13511362Sperrin 13521807Sbonwick if (zil_read_log_block(zilog, &zh->zh_log, &abuf) != 0) 13537522SNeil.Perrin@Sun.COM return (B_TRUE); 13541807Sbonwick 13551807Sbonwick VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 13567522SNeil.Perrin@Sun.COM return (B_FALSE); 13571362Sperrin } 13581362Sperrin 13591362Sperrin /* 1360789Sahrens * Open an intent log. 1361789Sahrens */ 1362789Sahrens zilog_t * 1363789Sahrens zil_open(objset_t *os, zil_get_data_t *get_data) 1364789Sahrens { 1365789Sahrens zilog_t *zilog = dmu_objset_zil(os); 1366789Sahrens 1367789Sahrens zilog->zl_get_data = get_data; 1368789Sahrens zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri, 1369789Sahrens 2, 2, TASKQ_PREPOPULATE); 1370789Sahrens 1371789Sahrens return (zilog); 1372789Sahrens } 1373789Sahrens 1374789Sahrens /* 1375789Sahrens * Close an intent log. 1376789Sahrens */ 1377789Sahrens void 1378789Sahrens zil_close(zilog_t *zilog) 1379789Sahrens { 13801807Sbonwick /* 13811807Sbonwick * If the log isn't already committed, mark the objset dirty 13821807Sbonwick * (so zil_sync() will be called) and wait for that txg to sync. 13831807Sbonwick */ 13841807Sbonwick if (!zil_is_committed(zilog)) { 13851807Sbonwick uint64_t txg; 13861807Sbonwick dmu_tx_t *tx = dmu_tx_create(zilog->zl_os); 13871807Sbonwick (void) dmu_tx_assign(tx, TXG_WAIT); 13881807Sbonwick dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 13891807Sbonwick txg = dmu_tx_get_txg(tx); 13901807Sbonwick dmu_tx_commit(tx); 13911807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, txg); 13921807Sbonwick } 13931807Sbonwick 1394789Sahrens taskq_destroy(zilog->zl_clean_taskq); 1395789Sahrens zilog->zl_clean_taskq = NULL; 1396789Sahrens zilog->zl_get_data = NULL; 1397789Sahrens 1398789Sahrens zil_itx_clean(zilog); 1399789Sahrens ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1400789Sahrens } 1401789Sahrens 1402789Sahrens /* 1403789Sahrens * Suspend an intent log. While in suspended mode, we still honor 1404789Sahrens * synchronous semantics, but we rely on txg_wait_synced() to do it. 1405789Sahrens * We suspend the log briefly when taking a snapshot so that the snapshot 1406789Sahrens * contains all the data it's supposed to, and has an empty intent log. 1407789Sahrens */ 1408789Sahrens int 1409789Sahrens zil_suspend(zilog_t *zilog) 1410789Sahrens { 14111807Sbonwick const zil_header_t *zh = zilog->zl_header; 1412789Sahrens 1413789Sahrens mutex_enter(&zilog->zl_lock); 14141807Sbonwick if (zh->zh_claim_txg != 0) { /* unplayed log */ 1415789Sahrens mutex_exit(&zilog->zl_lock); 1416789Sahrens return (EBUSY); 1417789Sahrens } 14181807Sbonwick if (zilog->zl_suspend++ != 0) { 14191807Sbonwick /* 14201807Sbonwick * Someone else already began a suspend. 14211807Sbonwick * Just wait for them to finish. 14221807Sbonwick */ 14231807Sbonwick while (zilog->zl_suspending) 14241807Sbonwick cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock); 14251807Sbonwick mutex_exit(&zilog->zl_lock); 14261807Sbonwick return (0); 14271807Sbonwick } 14281807Sbonwick zilog->zl_suspending = B_TRUE; 1429789Sahrens mutex_exit(&zilog->zl_lock); 1430789Sahrens 14312638Sperrin zil_commit(zilog, UINT64_MAX, 0); 1432789Sahrens 14332638Sperrin /* 14342638Sperrin * Wait for any in-flight log writes to complete. 14352638Sperrin */ 1436789Sahrens mutex_enter(&zilog->zl_lock); 14372638Sperrin while (zilog->zl_writer) 14382638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 1439789Sahrens mutex_exit(&zilog->zl_lock); 1440789Sahrens 14411807Sbonwick zil_destroy(zilog, B_FALSE); 14421807Sbonwick 14431807Sbonwick mutex_enter(&zilog->zl_lock); 14441807Sbonwick zilog->zl_suspending = B_FALSE; 14451807Sbonwick cv_broadcast(&zilog->zl_cv_suspend); 14461807Sbonwick mutex_exit(&zilog->zl_lock); 1447789Sahrens 1448789Sahrens return (0); 1449789Sahrens } 1450789Sahrens 1451789Sahrens void 1452789Sahrens zil_resume(zilog_t *zilog) 1453789Sahrens { 1454789Sahrens mutex_enter(&zilog->zl_lock); 1455789Sahrens ASSERT(zilog->zl_suspend != 0); 1456789Sahrens zilog->zl_suspend--; 1457789Sahrens mutex_exit(&zilog->zl_lock); 1458789Sahrens } 1459789Sahrens 1460789Sahrens typedef struct zil_replay_arg { 1461789Sahrens objset_t *zr_os; 1462789Sahrens zil_replay_func_t **zr_replay; 14637638SNeil.Perrin@Sun.COM zil_replay_cleaner_t *zr_replay_cleaner; 1464789Sahrens void *zr_arg; 1465789Sahrens uint64_t *zr_txgp; 1466789Sahrens boolean_t zr_byteswap; 1467789Sahrens char *zr_lrbuf; 1468789Sahrens } zil_replay_arg_t; 1469789Sahrens 1470789Sahrens static void 1471789Sahrens zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg) 1472789Sahrens { 1473789Sahrens zil_replay_arg_t *zr = zra; 14741807Sbonwick const zil_header_t *zh = zilog->zl_header; 1475789Sahrens uint64_t reclen = lr->lrc_reclen; 1476789Sahrens uint64_t txtype = lr->lrc_txtype; 14773063Sperrin char *name; 14783063Sperrin int pass, error, sunk; 1479789Sahrens 1480789Sahrens if (zilog->zl_stop_replay) 1481789Sahrens return; 1482789Sahrens 1483789Sahrens if (lr->lrc_txg < claim_txg) /* already committed */ 1484789Sahrens return; 1485789Sahrens 1486789Sahrens if (lr->lrc_seq <= zh->zh_replay_seq) /* already replayed */ 1487789Sahrens return; 1488789Sahrens 14895331Samw /* Strip case-insensitive bit, still present in log record */ 14905331Samw txtype &= ~TX_CI; 14915331Samw 1492789Sahrens /* 1493789Sahrens * Make a copy of the data so we can revise and extend it. 1494789Sahrens */ 1495789Sahrens bcopy(lr, zr->zr_lrbuf, reclen); 1496789Sahrens 1497789Sahrens /* 1498789Sahrens * The log block containing this lr may have been byteswapped 1499789Sahrens * so that we can easily examine common fields like lrc_txtype. 1500789Sahrens * However, the log is a mix of different data types, and only the 1501789Sahrens * replay vectors know how to byteswap their records. Therefore, if 1502789Sahrens * the lr was byteswapped, undo it before invoking the replay vector. 1503789Sahrens */ 1504789Sahrens if (zr->zr_byteswap) 1505789Sahrens byteswap_uint64_array(zr->zr_lrbuf, reclen); 1506789Sahrens 1507789Sahrens /* 1508789Sahrens * If this is a TX_WRITE with a blkptr, suck in the data. 1509789Sahrens */ 1510789Sahrens if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) { 1511789Sahrens lr_write_t *lrw = (lr_write_t *)lr; 1512789Sahrens blkptr_t *wbp = &lrw->lr_blkptr; 1513789Sahrens uint64_t wlen = lrw->lr_length; 1514789Sahrens char *wbuf = zr->zr_lrbuf + reclen; 1515789Sahrens 1516789Sahrens if (BP_IS_HOLE(wbp)) { /* compressed to a hole */ 1517789Sahrens bzero(wbuf, wlen); 1518789Sahrens } else { 1519789Sahrens /* 1520789Sahrens * A subsequent write may have overwritten this block, 1521789Sahrens * in which case wbp may have been been freed and 1522789Sahrens * reallocated, and our read of wbp may fail with a 1523789Sahrens * checksum error. We can safely ignore this because 1524789Sahrens * the later write will provide the correct data. 1525789Sahrens */ 15261544Seschrock zbookmark_t zb; 15271544Seschrock 15281544Seschrock zb.zb_objset = dmu_objset_id(zilog->zl_os); 15291544Seschrock zb.zb_object = lrw->lr_foid; 15301544Seschrock zb.zb_level = -1; 15311544Seschrock zb.zb_blkid = lrw->lr_offset / BP_GET_LSIZE(wbp); 15321544Seschrock 1533789Sahrens (void) zio_wait(zio_read(NULL, zilog->zl_spa, 1534789Sahrens wbp, wbuf, BP_GET_LSIZE(wbp), NULL, NULL, 1535789Sahrens ZIO_PRIORITY_SYNC_READ, 15361544Seschrock ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb)); 1537789Sahrens (void) memmove(wbuf, wbuf + lrw->lr_blkoff, wlen); 1538789Sahrens } 1539789Sahrens } 1540789Sahrens 1541789Sahrens /* 1542789Sahrens * We must now do two things atomically: replay this log record, 1543789Sahrens * and update the log header to reflect the fact that we did so. 1544789Sahrens * We use the DMU's ability to assign into a specific txg to do this. 1545789Sahrens */ 15463063Sperrin for (pass = 1, sunk = B_FALSE; /* CONSTANTCONDITION */; pass++) { 1547789Sahrens uint64_t replay_txg; 1548789Sahrens dmu_tx_t *replay_tx; 1549789Sahrens 1550789Sahrens replay_tx = dmu_tx_create(zr->zr_os); 1551789Sahrens error = dmu_tx_assign(replay_tx, TXG_WAIT); 1552789Sahrens if (error) { 1553789Sahrens dmu_tx_abort(replay_tx); 1554789Sahrens break; 1555789Sahrens } 1556789Sahrens 1557789Sahrens replay_txg = dmu_tx_get_txg(replay_tx); 1558789Sahrens 1559789Sahrens if (txtype == 0 || txtype >= TX_MAX_TYPE) { 1560789Sahrens error = EINVAL; 1561789Sahrens } else { 1562789Sahrens /* 1563789Sahrens * On the first pass, arrange for the replay vector 1564789Sahrens * to fail its dmu_tx_assign(). That's the only way 1565789Sahrens * to ensure that those code paths remain well tested. 15665676Sperrin * 15675676Sperrin * Only byteswap (if needed) on the 1st pass. 1568789Sahrens */ 1569789Sahrens *zr->zr_txgp = replay_txg - (pass == 1); 1570789Sahrens error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lrbuf, 15715676Sperrin zr->zr_byteswap && pass == 1); 1572789Sahrens *zr->zr_txgp = TXG_NOWAIT; 1573789Sahrens } 1574789Sahrens 1575789Sahrens if (error == 0) { 1576789Sahrens dsl_dataset_dirty(dmu_objset_ds(zr->zr_os), replay_tx); 1577789Sahrens zilog->zl_replay_seq[replay_txg & TXG_MASK] = 1578789Sahrens lr->lrc_seq; 1579789Sahrens } 1580789Sahrens 1581789Sahrens dmu_tx_commit(replay_tx); 1582789Sahrens 15833063Sperrin if (!error) 15843063Sperrin return; 15853063Sperrin 15863063Sperrin /* 15873063Sperrin * The DMU's dnode layer doesn't see removes until the txg 15883063Sperrin * commits, so a subsequent claim can spuriously fail with 15893063Sperrin * EEXIST. So if we receive any error other than ERESTART 15903063Sperrin * we try syncing out any removes then retrying the 15913063Sperrin * transaction. 15923063Sperrin */ 15933063Sperrin if (error != ERESTART && !sunk) { 15947638SNeil.Perrin@Sun.COM if (zr->zr_replay_cleaner) 15957638SNeil.Perrin@Sun.COM zr->zr_replay_cleaner(zr->zr_arg); 15963063Sperrin txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0); 15973063Sperrin sunk = B_TRUE; 15983063Sperrin continue; /* retry */ 15993063Sperrin } 16003063Sperrin 1601789Sahrens if (error != ERESTART) 1602789Sahrens break; 1603789Sahrens 1604789Sahrens if (pass != 1) 1605789Sahrens txg_wait_open(spa_get_dsl(zilog->zl_spa), 1606789Sahrens replay_txg + 1); 1607789Sahrens 1608789Sahrens dprintf("pass %d, retrying\n", pass); 1609789Sahrens } 1610789Sahrens 16113063Sperrin ASSERT(error && error != ERESTART); 16123063Sperrin name = kmem_alloc(MAXNAMELEN, KM_SLEEP); 16133063Sperrin dmu_objset_name(zr->zr_os, name); 16143063Sperrin cmn_err(CE_WARN, "ZFS replay transaction error %d, " 16155331Samw "dataset %s, seq 0x%llx, txtype %llu %s\n", 16165331Samw error, name, (u_longlong_t)lr->lrc_seq, (u_longlong_t)txtype, 16175331Samw (lr->lrc_txtype & TX_CI) ? "CI" : ""); 16183063Sperrin zilog->zl_stop_replay = 1; 16193063Sperrin kmem_free(name, MAXNAMELEN); 16203063Sperrin } 1621789Sahrens 16223063Sperrin /* ARGSUSED */ 16233063Sperrin static void 16243063Sperrin zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg) 16253063Sperrin { 16263063Sperrin zilog->zl_replay_blks++; 1627789Sahrens } 1628789Sahrens 1629789Sahrens /* 16301362Sperrin * If this dataset has a non-empty intent log, replay it and destroy it. 1631789Sahrens */ 1632789Sahrens void 1633789Sahrens zil_replay(objset_t *os, void *arg, uint64_t *txgp, 16347638SNeil.Perrin@Sun.COM zil_replay_func_t *replay_func[TX_MAX_TYPE], 16357638SNeil.Perrin@Sun.COM zil_replay_cleaner_t *replay_cleaner) 1636789Sahrens { 1637789Sahrens zilog_t *zilog = dmu_objset_zil(os); 16381807Sbonwick const zil_header_t *zh = zilog->zl_header; 16391807Sbonwick zil_replay_arg_t zr; 16401362Sperrin 16411362Sperrin if (zil_empty(zilog)) { 16421807Sbonwick zil_destroy(zilog, B_TRUE); 16431362Sperrin return; 16441362Sperrin } 1645789Sahrens 1646789Sahrens zr.zr_os = os; 1647789Sahrens zr.zr_replay = replay_func; 16487638SNeil.Perrin@Sun.COM zr.zr_replay_cleaner = replay_cleaner; 1649789Sahrens zr.zr_arg = arg; 1650789Sahrens zr.zr_txgp = txgp; 16511807Sbonwick zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log); 1652789Sahrens zr.zr_lrbuf = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP); 1653789Sahrens 1654789Sahrens /* 1655789Sahrens * Wait for in-progress removes to sync before starting replay. 1656789Sahrens */ 1657789Sahrens txg_wait_synced(zilog->zl_dmu_pool, 0); 1658789Sahrens 1659789Sahrens zilog->zl_stop_replay = 0; 16603063Sperrin zilog->zl_replay_time = lbolt; 16613063Sperrin ASSERT(zilog->zl_replay_blks == 0); 16623063Sperrin (void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr, 16631807Sbonwick zh->zh_claim_txg); 1664789Sahrens kmem_free(zr.zr_lrbuf, 2 * SPA_MAXBLOCKSIZE); 1665789Sahrens 16661807Sbonwick zil_destroy(zilog, B_FALSE); 16675712Sahrens txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 1668789Sahrens } 16691646Sperrin 16701646Sperrin /* 16711646Sperrin * Report whether all transactions are committed 16721646Sperrin */ 16731646Sperrin int 16741646Sperrin zil_is_committed(zilog_t *zilog) 16751646Sperrin { 16761646Sperrin lwb_t *lwb; 16772638Sperrin int ret; 16781646Sperrin 16792638Sperrin mutex_enter(&zilog->zl_lock); 16802638Sperrin while (zilog->zl_writer) 16812638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 16822638Sperrin 16832638Sperrin /* recent unpushed intent log transactions? */ 16842638Sperrin if (!list_is_empty(&zilog->zl_itx_list)) { 16852638Sperrin ret = B_FALSE; 16862638Sperrin goto out; 16872638Sperrin } 16882638Sperrin 16892638Sperrin /* intent log never used? */ 16902638Sperrin lwb = list_head(&zilog->zl_lwb_list); 16912638Sperrin if (lwb == NULL) { 16922638Sperrin ret = B_TRUE; 16932638Sperrin goto out; 16942638Sperrin } 16951646Sperrin 16961646Sperrin /* 16972638Sperrin * more than 1 log buffer means zil_sync() hasn't yet freed 16982638Sperrin * entries after a txg has committed 16991646Sperrin */ 17002638Sperrin if (list_next(&zilog->zl_lwb_list, lwb)) { 17012638Sperrin ret = B_FALSE; 17022638Sperrin goto out; 17032638Sperrin } 17042638Sperrin 17051646Sperrin ASSERT(zil_empty(zilog)); 17062638Sperrin ret = B_TRUE; 17072638Sperrin out: 17082638Sperrin cv_broadcast(&zilog->zl_cv_writer); 17092638Sperrin mutex_exit(&zilog->zl_lock); 17102638Sperrin return (ret); 17111646Sperrin } 1712