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 /* 228746SMatthew.Ahrens@Sun.COM * Copyright 2009 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> 289701SGeorge.Wilson@Sun.COM #include <sys/spa_impl.h> 29789Sahrens #include <sys/dmu.h> 30789Sahrens #include <sys/zap.h> 31789Sahrens #include <sys/arc.h> 32789Sahrens #include <sys/stat.h> 33789Sahrens #include <sys/resource.h> 34789Sahrens #include <sys/zil.h> 35789Sahrens #include <sys/zil_impl.h> 36789Sahrens #include <sys/dsl_dataset.h> 37789Sahrens #include <sys/vdev.h> 383668Sgw25295 #include <sys/dmu_tx.h> 39789Sahrens 40789Sahrens /* 41789Sahrens * The zfs intent log (ZIL) saves transaction records of system calls 42789Sahrens * that change the file system in memory with enough information 43789Sahrens * to be able to replay them. These are stored in memory until 44789Sahrens * either the DMU transaction group (txg) commits them to the stable pool 45789Sahrens * and they can be discarded, or they are flushed to the stable log 46789Sahrens * (also in the pool) due to a fsync, O_DSYNC or other synchronous 47789Sahrens * requirement. In the event of a panic or power fail then those log 48789Sahrens * records (transactions) are replayed. 49789Sahrens * 50789Sahrens * There is one ZIL per file system. Its on-disk (pool) format consists 51789Sahrens * of 3 parts: 52789Sahrens * 53789Sahrens * - ZIL header 54789Sahrens * - ZIL blocks 55789Sahrens * - ZIL records 56789Sahrens * 57789Sahrens * A log record holds a system call transaction. Log blocks can 58789Sahrens * hold many log records and the blocks are chained together. 59789Sahrens * Each ZIL block contains a block pointer (blkptr_t) to the next 60789Sahrens * ZIL block in the chain. The ZIL header points to the first 61789Sahrens * block in the chain. Note there is not a fixed place in the pool 62789Sahrens * to hold blocks. They are dynamically allocated and freed as 63789Sahrens * needed from the blocks available. Figure X shows the ZIL structure: 64789Sahrens */ 65789Sahrens 66789Sahrens /* 672986Sek110237 * This global ZIL switch affects all pools 68789Sahrens */ 69789Sahrens int zil_disable = 0; /* disable intent logging */ 702986Sek110237 712986Sek110237 /* 722986Sek110237 * Tunable parameter for debugging or performance analysis. Setting 732986Sek110237 * zfs_nocacheflush will cause corruption on power loss if a volatile 742986Sek110237 * out-of-order write cache is enabled. 752986Sek110237 */ 762986Sek110237 boolean_t zfs_nocacheflush = B_FALSE; 77789Sahrens 78789Sahrens static kmem_cache_t *zil_lwb_cache; 79789Sahrens 80789Sahrens static int 81789Sahrens zil_dva_compare(const void *x1, const void *x2) 82789Sahrens { 83789Sahrens const dva_t *dva1 = x1; 84789Sahrens const dva_t *dva2 = x2; 85789Sahrens 86789Sahrens if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2)) 87789Sahrens return (-1); 88789Sahrens if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2)) 89789Sahrens return (1); 90789Sahrens 91789Sahrens if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2)) 92789Sahrens return (-1); 93789Sahrens if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2)) 94789Sahrens return (1); 95789Sahrens 96789Sahrens return (0); 97789Sahrens } 98789Sahrens 99789Sahrens static void 100789Sahrens zil_dva_tree_init(avl_tree_t *t) 101789Sahrens { 102789Sahrens avl_create(t, zil_dva_compare, sizeof (zil_dva_node_t), 103789Sahrens offsetof(zil_dva_node_t, zn_node)); 104789Sahrens } 105789Sahrens 106789Sahrens static void 107789Sahrens zil_dva_tree_fini(avl_tree_t *t) 108789Sahrens { 109789Sahrens zil_dva_node_t *zn; 110789Sahrens void *cookie = NULL; 111789Sahrens 112789Sahrens while ((zn = avl_destroy_nodes(t, &cookie)) != NULL) 113789Sahrens kmem_free(zn, sizeof (zil_dva_node_t)); 114789Sahrens 115789Sahrens avl_destroy(t); 116789Sahrens } 117789Sahrens 118789Sahrens static int 119789Sahrens zil_dva_tree_add(avl_tree_t *t, dva_t *dva) 120789Sahrens { 121789Sahrens zil_dva_node_t *zn; 122789Sahrens avl_index_t where; 123789Sahrens 124789Sahrens if (avl_find(t, dva, &where) != NULL) 125789Sahrens return (EEXIST); 126789Sahrens 127789Sahrens zn = kmem_alloc(sizeof (zil_dva_node_t), KM_SLEEP); 128789Sahrens zn->zn_dva = *dva; 129789Sahrens avl_insert(t, zn, where); 130789Sahrens 131789Sahrens return (0); 132789Sahrens } 133789Sahrens 1341807Sbonwick static zil_header_t * 1351807Sbonwick zil_header_in_syncing_context(zilog_t *zilog) 1361807Sbonwick { 1371807Sbonwick return ((zil_header_t *)zilog->zl_header); 1381807Sbonwick } 1391807Sbonwick 1401807Sbonwick static void 1411807Sbonwick zil_init_log_chain(zilog_t *zilog, blkptr_t *bp) 1421807Sbonwick { 1431807Sbonwick zio_cksum_t *zc = &bp->blk_cksum; 1441807Sbonwick 1451807Sbonwick zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL); 1461807Sbonwick zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL); 1471807Sbonwick zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os); 1481807Sbonwick zc->zc_word[ZIL_ZC_SEQ] = 1ULL; 1491807Sbonwick } 1501807Sbonwick 151789Sahrens /* 152789Sahrens * Read a log block, make sure it's valid, and byteswap it if necessary. 153789Sahrens */ 154789Sahrens static int 1551807Sbonwick zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, arc_buf_t **abufpp) 156789Sahrens { 1571807Sbonwick blkptr_t blk = *bp; 1581544Seschrock zbookmark_t zb; 1592391Smaybee uint32_t aflags = ARC_WAIT; 160789Sahrens int error; 161789Sahrens 1621807Sbonwick zb.zb_objset = bp->blk_cksum.zc_word[ZIL_ZC_OBJSET]; 1631544Seschrock zb.zb_object = 0; 1641544Seschrock zb.zb_level = -1; 1651807Sbonwick zb.zb_blkid = bp->blk_cksum.zc_word[ZIL_ZC_SEQ]; 1661807Sbonwick 1671807Sbonwick *abufpp = NULL; 1681807Sbonwick 1697046Sahrens /* 1707046Sahrens * We shouldn't be doing any scrubbing while we're doing log 1717046Sahrens * replay, it's OK to not lock. 1727046Sahrens */ 1737046Sahrens error = arc_read_nolock(NULL, zilog->zl_spa, &blk, 1741807Sbonwick arc_getbuf_func, abufpp, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL | 1752391Smaybee ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB, &aflags, &zb); 1761807Sbonwick 1771807Sbonwick if (error == 0) { 1781807Sbonwick char *data = (*abufpp)->b_data; 1791807Sbonwick uint64_t blksz = BP_GET_LSIZE(bp); 1801807Sbonwick zil_trailer_t *ztp = (zil_trailer_t *)(data + blksz) - 1; 1811807Sbonwick zio_cksum_t cksum = bp->blk_cksum; 1821544Seschrock 1831807Sbonwick /* 1847522SNeil.Perrin@Sun.COM * Validate the checksummed log block. 1857522SNeil.Perrin@Sun.COM * 1861807Sbonwick * Sequence numbers should be... sequential. The checksum 1871807Sbonwick * verifier for the next block should be bp's checksum plus 1. 1887522SNeil.Perrin@Sun.COM * 1897522SNeil.Perrin@Sun.COM * Also check the log chain linkage and size used. 1901807Sbonwick */ 1911807Sbonwick cksum.zc_word[ZIL_ZC_SEQ]++; 1921807Sbonwick 1937522SNeil.Perrin@Sun.COM if (bcmp(&cksum, &ztp->zit_next_blk.blk_cksum, 1947522SNeil.Perrin@Sun.COM sizeof (cksum)) || BP_IS_HOLE(&ztp->zit_next_blk) || 1957522SNeil.Perrin@Sun.COM (ztp->zit_nused > (blksz - sizeof (zil_trailer_t)))) { 1967522SNeil.Perrin@Sun.COM error = ECKSUM; 1977522SNeil.Perrin@Sun.COM } 1981807Sbonwick 1991807Sbonwick if (error) { 2001807Sbonwick VERIFY(arc_buf_remove_ref(*abufpp, abufpp) == 1); 2011807Sbonwick *abufpp = NULL; 2021807Sbonwick } 203789Sahrens } 204789Sahrens 2051807Sbonwick dprintf("error %d on %llu:%llu\n", error, zb.zb_objset, zb.zb_blkid); 206789Sahrens 2071807Sbonwick return (error); 208789Sahrens } 209789Sahrens 210789Sahrens /* 211789Sahrens * Parse the intent log, and call parse_func for each valid record within. 2121807Sbonwick * Return the highest sequence number. 213789Sahrens */ 2141807Sbonwick uint64_t 215789Sahrens zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func, 216789Sahrens zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg) 217789Sahrens { 2181807Sbonwick const zil_header_t *zh = zilog->zl_header; 2191807Sbonwick uint64_t claim_seq = zh->zh_claim_seq; 2201807Sbonwick uint64_t seq = 0; 2211807Sbonwick uint64_t max_seq = 0; 2221807Sbonwick blkptr_t blk = zh->zh_log; 2231807Sbonwick arc_buf_t *abuf; 224789Sahrens char *lrbuf, *lrp; 225789Sahrens zil_trailer_t *ztp; 226789Sahrens int reclen, error; 227789Sahrens 228789Sahrens if (BP_IS_HOLE(&blk)) 2291807Sbonwick return (max_seq); 230789Sahrens 231789Sahrens /* 232789Sahrens * Starting at the block pointed to by zh_log we read the log chain. 233789Sahrens * For each block in the chain we strongly check that block to 234789Sahrens * ensure its validity. We stop when an invalid block is found. 235789Sahrens * For each block pointer in the chain we call parse_blk_func(). 236789Sahrens * For each record in each valid block we call parse_lr_func(). 2371807Sbonwick * If the log has been claimed, stop if we encounter a sequence 2381807Sbonwick * number greater than the highest claimed sequence number. 239789Sahrens */ 240789Sahrens zil_dva_tree_init(&zilog->zl_dva_tree); 241789Sahrens for (;;) { 2421807Sbonwick seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 2431807Sbonwick 2441807Sbonwick if (claim_seq != 0 && seq > claim_seq) 2451807Sbonwick break; 2461807Sbonwick 2471807Sbonwick ASSERT(max_seq < seq); 2481807Sbonwick max_seq = seq; 2491807Sbonwick 2501807Sbonwick error = zil_read_log_block(zilog, &blk, &abuf); 251789Sahrens 252789Sahrens if (parse_blk_func != NULL) 253789Sahrens parse_blk_func(zilog, &blk, arg, txg); 254789Sahrens 255789Sahrens if (error) 256789Sahrens break; 257789Sahrens 2581807Sbonwick lrbuf = abuf->b_data; 259789Sahrens ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1; 260789Sahrens blk = ztp->zit_next_blk; 261789Sahrens 2621807Sbonwick if (parse_lr_func == NULL) { 2631807Sbonwick VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 264789Sahrens continue; 2651807Sbonwick } 266789Sahrens 267789Sahrens for (lrp = lrbuf; lrp < lrbuf + ztp->zit_nused; lrp += reclen) { 268789Sahrens lr_t *lr = (lr_t *)lrp; 269789Sahrens reclen = lr->lrc_reclen; 270789Sahrens ASSERT3U(reclen, >=, sizeof (lr_t)); 271789Sahrens parse_lr_func(zilog, lr, arg, txg); 272789Sahrens } 2731807Sbonwick VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 274789Sahrens } 275789Sahrens zil_dva_tree_fini(&zilog->zl_dva_tree); 2761807Sbonwick 2771807Sbonwick return (max_seq); 278789Sahrens } 279789Sahrens 280789Sahrens /* ARGSUSED */ 281789Sahrens static void 282789Sahrens zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg) 283789Sahrens { 284789Sahrens spa_t *spa = zilog->zl_spa; 285789Sahrens int err; 286789Sahrens 287789Sahrens /* 288789Sahrens * Claim log block if not already committed and not already claimed. 289789Sahrens */ 290789Sahrens if (bp->blk_birth >= first_txg && 291789Sahrens zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp)) == 0) { 2927754SJeff.Bonwick@Sun.COM err = zio_wait(zio_claim(NULL, spa, first_txg, bp, NULL, NULL, 2937754SJeff.Bonwick@Sun.COM ZIO_FLAG_MUSTSUCCEED)); 294789Sahrens ASSERT(err == 0); 295789Sahrens } 296789Sahrens } 297789Sahrens 298789Sahrens static void 299789Sahrens zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg) 300789Sahrens { 301789Sahrens if (lrc->lrc_txtype == TX_WRITE) { 302789Sahrens lr_write_t *lr = (lr_write_t *)lrc; 303789Sahrens zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg); 304789Sahrens } 305789Sahrens } 306789Sahrens 307789Sahrens /* ARGSUSED */ 308789Sahrens static void 309789Sahrens zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg) 310789Sahrens { 311789Sahrens zio_free_blk(zilog->zl_spa, bp, dmu_tx_get_txg(tx)); 312789Sahrens } 313789Sahrens 314789Sahrens static void 315789Sahrens zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg) 316789Sahrens { 317789Sahrens /* 318789Sahrens * If we previously claimed it, we need to free it. 319789Sahrens */ 320789Sahrens if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE) { 321789Sahrens lr_write_t *lr = (lr_write_t *)lrc; 322789Sahrens blkptr_t *bp = &lr->lr_blkptr; 323789Sahrens if (bp->blk_birth >= claim_txg && 324789Sahrens !zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp))) { 325789Sahrens (void) arc_free(NULL, zilog->zl_spa, 326789Sahrens dmu_tx_get_txg(tx), bp, NULL, NULL, ARC_WAIT); 327789Sahrens } 328789Sahrens } 329789Sahrens } 330789Sahrens 331789Sahrens /* 332789Sahrens * Create an on-disk intent log. 333789Sahrens */ 334789Sahrens static void 335789Sahrens zil_create(zilog_t *zilog) 336789Sahrens { 3371807Sbonwick const zil_header_t *zh = zilog->zl_header; 338789Sahrens lwb_t *lwb; 3391807Sbonwick uint64_t txg = 0; 3401807Sbonwick dmu_tx_t *tx = NULL; 341789Sahrens blkptr_t blk; 3421807Sbonwick int error = 0; 343789Sahrens 344789Sahrens /* 3451807Sbonwick * Wait for any previous destroy to complete. 346789Sahrens */ 3471807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 3481807Sbonwick 3491807Sbonwick ASSERT(zh->zh_claim_txg == 0); 3501807Sbonwick ASSERT(zh->zh_replay_seq == 0); 3511807Sbonwick 3521807Sbonwick blk = zh->zh_log; 353789Sahrens 354789Sahrens /* 3558109SNeil.Perrin@Sun.COM * If we don't already have an initial log block or we have one 3568109SNeil.Perrin@Sun.COM * but it's the wrong endianness then allocate one. 357789Sahrens */ 3588109SNeil.Perrin@Sun.COM if (BP_IS_HOLE(&blk) || BP_SHOULD_BYTESWAP(&blk)) { 3591807Sbonwick tx = dmu_tx_create(zilog->zl_os); 3601807Sbonwick (void) dmu_tx_assign(tx, TXG_WAIT); 3611807Sbonwick dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 3621807Sbonwick txg = dmu_tx_get_txg(tx); 3631807Sbonwick 3648109SNeil.Perrin@Sun.COM if (!BP_IS_HOLE(&blk)) { 3658109SNeil.Perrin@Sun.COM zio_free_blk(zilog->zl_spa, &blk, txg); 3668109SNeil.Perrin@Sun.COM BP_ZERO(&blk); 3678109SNeil.Perrin@Sun.COM } 3688109SNeil.Perrin@Sun.COM 3693063Sperrin error = zio_alloc_blk(zilog->zl_spa, ZIL_MIN_BLKSZ, &blk, 3703063Sperrin NULL, txg); 3711807Sbonwick 3721807Sbonwick if (error == 0) 3731807Sbonwick zil_init_log_chain(zilog, &blk); 3741362Sperrin } 3751807Sbonwick 3761807Sbonwick /* 3771807Sbonwick * Allocate a log write buffer (lwb) for the first log block. 3781807Sbonwick */ 379789Sahrens if (error == 0) { 380789Sahrens lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 381789Sahrens lwb->lwb_zilog = zilog; 382789Sahrens lwb->lwb_blk = blk; 383789Sahrens lwb->lwb_nused = 0; 384789Sahrens lwb->lwb_sz = BP_GET_LSIZE(&lwb->lwb_blk); 385789Sahrens lwb->lwb_buf = zio_buf_alloc(lwb->lwb_sz); 386789Sahrens lwb->lwb_max_txg = txg; 3872237Smaybee lwb->lwb_zio = NULL; 3882237Smaybee 389789Sahrens mutex_enter(&zilog->zl_lock); 390789Sahrens list_insert_tail(&zilog->zl_lwb_list, lwb); 391789Sahrens mutex_exit(&zilog->zl_lock); 392789Sahrens } 393789Sahrens 3941807Sbonwick /* 3951807Sbonwick * If we just allocated the first log block, commit our transaction 3961807Sbonwick * and wait for zil_sync() to stuff the block poiner into zh_log. 3971807Sbonwick * (zh is part of the MOS, so we cannot modify it in open context.) 3981807Sbonwick */ 3991807Sbonwick if (tx != NULL) { 4001807Sbonwick dmu_tx_commit(tx); 4011362Sperrin txg_wait_synced(zilog->zl_dmu_pool, txg); 4021807Sbonwick } 4031807Sbonwick 4041807Sbonwick ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0); 405789Sahrens } 406789Sahrens 407789Sahrens /* 408789Sahrens * In one tx, free all log blocks and clear the log header. 4091807Sbonwick * If keep_first is set, then we're replaying a log with no content. 4101807Sbonwick * We want to keep the first block, however, so that the first 4111807Sbonwick * synchronous transaction doesn't require a txg_wait_synced() 4121807Sbonwick * in zil_create(). We don't need to txg_wait_synced() here either 4131807Sbonwick * when keep_first is set, because both zil_create() and zil_destroy() 4141807Sbonwick * will wait for any in-progress destroys to complete. 415789Sahrens */ 416789Sahrens void 4171807Sbonwick zil_destroy(zilog_t *zilog, boolean_t keep_first) 418789Sahrens { 4191807Sbonwick const zil_header_t *zh = zilog->zl_header; 4201807Sbonwick lwb_t *lwb; 421789Sahrens dmu_tx_t *tx; 422789Sahrens uint64_t txg; 423789Sahrens 4241807Sbonwick /* 4251807Sbonwick * Wait for any previous destroy to complete. 4261807Sbonwick */ 4271807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 428789Sahrens 4291807Sbonwick if (BP_IS_HOLE(&zh->zh_log)) 430789Sahrens return; 431789Sahrens 432789Sahrens tx = dmu_tx_create(zilog->zl_os); 433789Sahrens (void) dmu_tx_assign(tx, TXG_WAIT); 434789Sahrens dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 435789Sahrens txg = dmu_tx_get_txg(tx); 436789Sahrens 4371807Sbonwick mutex_enter(&zilog->zl_lock); 4381807Sbonwick 4395223Sperrin /* 4405223Sperrin * It is possible for the ZIL to get the previously mounted zilog 4415223Sperrin * structure of the same dataset if quickly remounted and the dbuf 4425223Sperrin * eviction has not completed. In this case we can see a non 4435223Sperrin * empty lwb list and keep_first will be set. We fix this by 4445223Sperrin * clearing the keep_first. This will be slower but it's very rare. 4455223Sperrin */ 4465223Sperrin if (!list_is_empty(&zilog->zl_lwb_list) && keep_first) 4475223Sperrin keep_first = B_FALSE; 4485223Sperrin 4491807Sbonwick ASSERT3U(zilog->zl_destroy_txg, <, txg); 450789Sahrens zilog->zl_destroy_txg = txg; 4511807Sbonwick zilog->zl_keep_first = keep_first; 4521807Sbonwick 4531807Sbonwick if (!list_is_empty(&zilog->zl_lwb_list)) { 4541807Sbonwick ASSERT(zh->zh_claim_txg == 0); 4551807Sbonwick ASSERT(!keep_first); 4561807Sbonwick while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 4571807Sbonwick list_remove(&zilog->zl_lwb_list, lwb); 4581807Sbonwick if (lwb->lwb_buf != NULL) 4591807Sbonwick zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 4601807Sbonwick zio_free_blk(zilog->zl_spa, &lwb->lwb_blk, txg); 4611807Sbonwick kmem_cache_free(zil_lwb_cache, lwb); 4621807Sbonwick } 4631807Sbonwick } else { 4641807Sbonwick if (!keep_first) { 4651807Sbonwick (void) zil_parse(zilog, zil_free_log_block, 4661807Sbonwick zil_free_log_record, tx, zh->zh_claim_txg); 4671807Sbonwick } 4681807Sbonwick } 4692638Sperrin mutex_exit(&zilog->zl_lock); 470789Sahrens 471789Sahrens dmu_tx_commit(tx); 472789Sahrens } 473789Sahrens 4748989SNeil.Perrin@Sun.COM /* 4758989SNeil.Perrin@Sun.COM * return true if the initial log block is not valid 4768989SNeil.Perrin@Sun.COM */ 4778989SNeil.Perrin@Sun.COM static boolean_t 4788989SNeil.Perrin@Sun.COM zil_empty(zilog_t *zilog) 4798989SNeil.Perrin@Sun.COM { 4808989SNeil.Perrin@Sun.COM const zil_header_t *zh = zilog->zl_header; 4818989SNeil.Perrin@Sun.COM arc_buf_t *abuf = NULL; 4828989SNeil.Perrin@Sun.COM 4838989SNeil.Perrin@Sun.COM if (BP_IS_HOLE(&zh->zh_log)) 4848989SNeil.Perrin@Sun.COM return (B_TRUE); 4858989SNeil.Perrin@Sun.COM 4868989SNeil.Perrin@Sun.COM if (zil_read_log_block(zilog, &zh->zh_log, &abuf) != 0) 4878989SNeil.Perrin@Sun.COM return (B_TRUE); 4888989SNeil.Perrin@Sun.COM 4898989SNeil.Perrin@Sun.COM VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 4908989SNeil.Perrin@Sun.COM return (B_FALSE); 4918989SNeil.Perrin@Sun.COM } 4928989SNeil.Perrin@Sun.COM 4932199Sahrens int 494789Sahrens zil_claim(char *osname, void *txarg) 495789Sahrens { 496789Sahrens dmu_tx_t *tx = txarg; 497789Sahrens uint64_t first_txg = dmu_tx_get_txg(tx); 498789Sahrens zilog_t *zilog; 499789Sahrens zil_header_t *zh; 500789Sahrens objset_t *os; 501789Sahrens int error; 502789Sahrens 5036689Smaybee error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_USER, &os); 504789Sahrens if (error) { 5057294Sperrin cmn_err(CE_WARN, "can't open objset for %s", osname); 5062199Sahrens return (0); 507789Sahrens } 508789Sahrens 509789Sahrens zilog = dmu_objset_zil(os); 5101807Sbonwick zh = zil_header_in_syncing_context(zilog); 511789Sahrens 5129701SGeorge.Wilson@Sun.COM if (zilog->zl_spa->spa_log_state == SPA_LOG_CLEAR) { 5139701SGeorge.Wilson@Sun.COM if (!BP_IS_HOLE(&zh->zh_log)) 5149701SGeorge.Wilson@Sun.COM zio_free_blk(zilog->zl_spa, &zh->zh_log, first_txg); 5159701SGeorge.Wilson@Sun.COM BP_ZERO(&zh->zh_log); 5169701SGeorge.Wilson@Sun.COM dsl_dataset_dirty(dmu_objset_ds(os), tx); 5179701SGeorge.Wilson@Sun.COM } 5189701SGeorge.Wilson@Sun.COM 519789Sahrens /* 5208989SNeil.Perrin@Sun.COM * Record here whether the zil has any records to replay. 5218989SNeil.Perrin@Sun.COM * If the header block pointer is null or the block points 5228989SNeil.Perrin@Sun.COM * to the stubby then we know there are no valid log records. 5238989SNeil.Perrin@Sun.COM * We use the header to store this state as the the zilog gets 5248989SNeil.Perrin@Sun.COM * freed later in dmu_objset_close(). 5258989SNeil.Perrin@Sun.COM * The flags (and the rest of the header fields) are cleared in 5268989SNeil.Perrin@Sun.COM * zil_sync() as a result of a zil_destroy(), after replaying the log. 5278989SNeil.Perrin@Sun.COM * 5288989SNeil.Perrin@Sun.COM * Note, the intent log can be empty but still need the 5298989SNeil.Perrin@Sun.COM * stubby to be claimed. 5308989SNeil.Perrin@Sun.COM */ 5319701SGeorge.Wilson@Sun.COM if (!zil_empty(zilog)) { 5328989SNeil.Perrin@Sun.COM zh->zh_flags |= ZIL_REPLAY_NEEDED; 5339701SGeorge.Wilson@Sun.COM dsl_dataset_dirty(dmu_objset_ds(os), tx); 5349701SGeorge.Wilson@Sun.COM } 5358989SNeil.Perrin@Sun.COM 5368989SNeil.Perrin@Sun.COM /* 5371807Sbonwick * Claim all log blocks if we haven't already done so, and remember 5381807Sbonwick * the highest claimed sequence number. This ensures that if we can 5391807Sbonwick * read only part of the log now (e.g. due to a missing device), 5401807Sbonwick * but we can read the entire log later, we will not try to replay 5411807Sbonwick * or destroy beyond the last block we successfully claimed. 542789Sahrens */ 543789Sahrens ASSERT3U(zh->zh_claim_txg, <=, first_txg); 544789Sahrens if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) { 545789Sahrens zh->zh_claim_txg = first_txg; 5461807Sbonwick zh->zh_claim_seq = zil_parse(zilog, zil_claim_log_block, 5471807Sbonwick zil_claim_log_record, tx, first_txg); 548789Sahrens dsl_dataset_dirty(dmu_objset_ds(os), tx); 549789Sahrens } 5501807Sbonwick 551789Sahrens ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1)); 552789Sahrens dmu_objset_close(os); 5532199Sahrens return (0); 554789Sahrens } 555789Sahrens 5567294Sperrin /* 5577294Sperrin * Check the log by walking the log chain. 5587294Sperrin * Checksum errors are ok as they indicate the end of the chain. 5597294Sperrin * Any other error (no device or read failure) returns an error. 5607294Sperrin */ 5617294Sperrin /* ARGSUSED */ 5627294Sperrin int 5637294Sperrin zil_check_log_chain(char *osname, void *txarg) 5647294Sperrin { 5657294Sperrin zilog_t *zilog; 5667294Sperrin zil_header_t *zh; 5677294Sperrin blkptr_t blk; 5687294Sperrin arc_buf_t *abuf; 5697294Sperrin objset_t *os; 5707294Sperrin char *lrbuf; 5717294Sperrin zil_trailer_t *ztp; 5727294Sperrin int error; 5737294Sperrin 5747294Sperrin error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_USER, &os); 5757294Sperrin if (error) { 5767294Sperrin cmn_err(CE_WARN, "can't open objset for %s", osname); 5777294Sperrin return (0); 5787294Sperrin } 5797294Sperrin 5807294Sperrin zilog = dmu_objset_zil(os); 5817294Sperrin zh = zil_header_in_syncing_context(zilog); 5827294Sperrin blk = zh->zh_log; 5837294Sperrin if (BP_IS_HOLE(&blk)) { 5847294Sperrin dmu_objset_close(os); 5857294Sperrin return (0); /* no chain */ 5867294Sperrin } 5877294Sperrin 5887294Sperrin for (;;) { 5897294Sperrin error = zil_read_log_block(zilog, &blk, &abuf); 5907294Sperrin if (error) 5917294Sperrin break; 5927294Sperrin lrbuf = abuf->b_data; 5937294Sperrin ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1; 5947294Sperrin blk = ztp->zit_next_blk; 5957294Sperrin VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 5967294Sperrin } 5977294Sperrin dmu_objset_close(os); 5987294Sperrin if (error == ECKSUM) 5997294Sperrin return (0); /* normal end of chain */ 6007294Sperrin return (error); 6017294Sperrin } 6027294Sperrin 6035688Sbonwick static int 6045688Sbonwick zil_vdev_compare(const void *x1, const void *x2) 605789Sahrens { 6065875Sperrin uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev; 6075875Sperrin uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev; 6085688Sbonwick 6095688Sbonwick if (v1 < v2) 6105688Sbonwick return (-1); 6115688Sbonwick if (v1 > v2) 6125688Sbonwick return (1); 6135688Sbonwick 6145688Sbonwick return (0); 6155688Sbonwick } 6165688Sbonwick 6175688Sbonwick void 6185688Sbonwick zil_add_block(zilog_t *zilog, blkptr_t *bp) 6195688Sbonwick { 6205688Sbonwick avl_tree_t *t = &zilog->zl_vdev_tree; 6215688Sbonwick avl_index_t where; 6225688Sbonwick zil_vdev_node_t *zv, zvsearch; 6235688Sbonwick int ndvas = BP_GET_NDVAS(bp); 6245688Sbonwick int i; 625789Sahrens 6262986Sek110237 if (zfs_nocacheflush) 627789Sahrens return; 628789Sahrens 6295688Sbonwick ASSERT(zilog->zl_writer); 6305688Sbonwick 6315688Sbonwick /* 6325688Sbonwick * Even though we're zl_writer, we still need a lock because the 6335688Sbonwick * zl_get_data() callbacks may have dmu_sync() done callbacks 6345688Sbonwick * that will run concurrently. 6355688Sbonwick */ 6365688Sbonwick mutex_enter(&zilog->zl_vdev_lock); 6375688Sbonwick for (i = 0; i < ndvas; i++) { 6385688Sbonwick zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]); 6395688Sbonwick if (avl_find(t, &zvsearch, &where) == NULL) { 6405688Sbonwick zv = kmem_alloc(sizeof (*zv), KM_SLEEP); 6415688Sbonwick zv->zv_vdev = zvsearch.zv_vdev; 6425688Sbonwick avl_insert(t, zv, where); 6433063Sperrin } 6443063Sperrin } 6455688Sbonwick mutex_exit(&zilog->zl_vdev_lock); 6463063Sperrin } 6473063Sperrin 648789Sahrens void 6492638Sperrin zil_flush_vdevs(zilog_t *zilog) 650789Sahrens { 6513063Sperrin spa_t *spa = zilog->zl_spa; 6525688Sbonwick avl_tree_t *t = &zilog->zl_vdev_tree; 6535688Sbonwick void *cookie = NULL; 6545688Sbonwick zil_vdev_node_t *zv; 6555688Sbonwick zio_t *zio; 6563063Sperrin 6573063Sperrin ASSERT(zilog->zl_writer); 658789Sahrens 6595688Sbonwick /* 6605688Sbonwick * We don't need zl_vdev_lock here because we're the zl_writer, 6615688Sbonwick * and all zl_get_data() callbacks are done. 6625688Sbonwick */ 6635688Sbonwick if (avl_numnodes(t) == 0) 6645688Sbonwick return; 6655688Sbonwick 6667754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 6675688Sbonwick 6687754SJeff.Bonwick@Sun.COM zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); 6695688Sbonwick 6705688Sbonwick while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) { 6715688Sbonwick vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev); 6725688Sbonwick if (vd != NULL) 6735688Sbonwick zio_flush(zio, vd); 6745688Sbonwick kmem_free(zv, sizeof (*zv)); 6753063Sperrin } 676789Sahrens 677789Sahrens /* 678789Sahrens * Wait for all the flushes to complete. Not all devices actually 679789Sahrens * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails. 680789Sahrens */ 6815688Sbonwick (void) zio_wait(zio); 6825688Sbonwick 6837754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 684789Sahrens } 685789Sahrens 686789Sahrens /* 687789Sahrens * Function called when a log block write completes 688789Sahrens */ 689789Sahrens static void 690789Sahrens zil_lwb_write_done(zio_t *zio) 691789Sahrens { 692789Sahrens lwb_t *lwb = zio->io_private; 693789Sahrens zilog_t *zilog = lwb->lwb_zilog; 694789Sahrens 6957754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF); 6967754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_CHECKSUM(zio->io_bp) == ZIO_CHECKSUM_ZILOG); 6977754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_TYPE(zio->io_bp) == DMU_OT_INTENT_LOG); 6987754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 6997754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_BYTEORDER(zio->io_bp) == ZFS_HOST_BYTEORDER); 7007754SJeff.Bonwick@Sun.COM ASSERT(!BP_IS_GANG(zio->io_bp)); 7017754SJeff.Bonwick@Sun.COM ASSERT(!BP_IS_HOLE(zio->io_bp)); 7027754SJeff.Bonwick@Sun.COM ASSERT(zio->io_bp->blk_fill == 0); 7037754SJeff.Bonwick@Sun.COM 704789Sahrens /* 7059493SNeil.Perrin@Sun.COM * Ensure the lwb buffer pointer is cleared before releasing 7069493SNeil.Perrin@Sun.COM * the txg. If we have had an allocation failure and 7079493SNeil.Perrin@Sun.COM * the txg is waiting to sync then we want want zil_sync() 7089493SNeil.Perrin@Sun.COM * to remove the lwb so that it's not picked up as the next new 7099493SNeil.Perrin@Sun.COM * one in zil_commit_writer(). zil_sync() will only remove 7109493SNeil.Perrin@Sun.COM * the lwb if lwb_buf is null. 711789Sahrens */ 712789Sahrens zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 713789Sahrens mutex_enter(&zilog->zl_lock); 714789Sahrens lwb->lwb_buf = NULL; 7154527Sperrin if (zio->io_error) 716789Sahrens zilog->zl_log_error = B_TRUE; 7179493SNeil.Perrin@Sun.COM 7189493SNeil.Perrin@Sun.COM /* 7199493SNeil.Perrin@Sun.COM * Now that we've written this log block, we have a stable pointer 7209493SNeil.Perrin@Sun.COM * to the next block in the chain, so it's OK to let the txg in 721*9904SNeil.Perrin@Sun.COM * which we allocated the next block sync. We still have the 722*9904SNeil.Perrin@Sun.COM * zl_lock to ensure zil_sync doesn't kmem free the lwb. 7239493SNeil.Perrin@Sun.COM */ 7249493SNeil.Perrin@Sun.COM txg_rele_to_sync(&lwb->lwb_txgh); 725*9904SNeil.Perrin@Sun.COM mutex_exit(&zilog->zl_lock); 726789Sahrens } 727789Sahrens 728789Sahrens /* 7292237Smaybee * Initialize the io for a log block. 7302237Smaybee */ 7312237Smaybee static void 7322237Smaybee zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb) 7332237Smaybee { 7342237Smaybee zbookmark_t zb; 7352237Smaybee 7362237Smaybee zb.zb_objset = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET]; 7372237Smaybee zb.zb_object = 0; 7382237Smaybee zb.zb_level = -1; 7392237Smaybee zb.zb_blkid = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 7402237Smaybee 7412638Sperrin if (zilog->zl_root_zio == NULL) { 7422638Sperrin zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL, 7432638Sperrin ZIO_FLAG_CANFAIL); 7442638Sperrin } 7453063Sperrin if (lwb->lwb_zio == NULL) { 7463063Sperrin lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa, 7479701SGeorge.Wilson@Sun.COM 0, &lwb->lwb_blk, lwb->lwb_buf, lwb->lwb_sz, 7489701SGeorge.Wilson@Sun.COM zil_lwb_write_done, lwb, ZIO_PRIORITY_LOG_WRITE, 7499701SGeorge.Wilson@Sun.COM ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb); 7503063Sperrin } 7512237Smaybee } 7522237Smaybee 7532237Smaybee /* 754789Sahrens * Start a log block write and advance to the next log block. 755789Sahrens * Calls are serialized. 756789Sahrens */ 757789Sahrens static lwb_t * 758789Sahrens zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb) 759789Sahrens { 760789Sahrens lwb_t *nlwb; 761789Sahrens zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1; 7621807Sbonwick spa_t *spa = zilog->zl_spa; 7631807Sbonwick blkptr_t *bp = &ztp->zit_next_blk; 764789Sahrens uint64_t txg; 765789Sahrens uint64_t zil_blksz; 766789Sahrens int error; 767789Sahrens 768789Sahrens ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb)); 769789Sahrens 770789Sahrens /* 771789Sahrens * Allocate the next block and save its address in this block 772789Sahrens * before writing it in order to establish the log chain. 773789Sahrens * Note that if the allocation of nlwb synced before we wrote 774789Sahrens * the block that points at it (lwb), we'd leak it if we crashed. 775789Sahrens * Therefore, we don't do txg_rele_to_sync() until zil_lwb_write_done(). 776789Sahrens */ 777789Sahrens txg = txg_hold_open(zilog->zl_dmu_pool, &lwb->lwb_txgh); 778789Sahrens txg_rele_to_quiesce(&lwb->lwb_txgh); 779789Sahrens 780789Sahrens /* 7811141Sperrin * Pick a ZIL blocksize. We request a size that is the 7821141Sperrin * maximum of the previous used size, the current used size and 7831141Sperrin * the amount waiting in the queue. 784789Sahrens */ 7852237Smaybee zil_blksz = MAX(zilog->zl_prev_used, 7862237Smaybee zilog->zl_cur_used + sizeof (*ztp)); 7871141Sperrin zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp)); 7881842Sperrin zil_blksz = P2ROUNDUP_TYPED(zil_blksz, ZIL_MIN_BLKSZ, uint64_t); 7891141Sperrin if (zil_blksz > ZIL_MAX_BLKSZ) 7901141Sperrin zil_blksz = ZIL_MAX_BLKSZ; 791789Sahrens 7923063Sperrin BP_ZERO(bp); 7933063Sperrin /* pass the old blkptr in order to spread log blocks across devs */ 7943063Sperrin error = zio_alloc_blk(spa, zil_blksz, bp, &lwb->lwb_blk, txg); 795789Sahrens if (error) { 7963668Sgw25295 dmu_tx_t *tx = dmu_tx_create_assigned(zilog->zl_dmu_pool, txg); 7973668Sgw25295 7981544Seschrock /* 7993668Sgw25295 * We dirty the dataset to ensure that zil_sync() will 8003668Sgw25295 * be called to remove this lwb from our zl_lwb_list. 8013668Sgw25295 * Failing to do so, may leave an lwb with a NULL lwb_buf 8023668Sgw25295 * hanging around on the zl_lwb_list. 8033668Sgw25295 */ 8043668Sgw25295 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 8053848Sgw25295 dmu_tx_commit(tx); 8063668Sgw25295 8073668Sgw25295 /* 8083668Sgw25295 * Since we've just experienced an allocation failure so we 8093668Sgw25295 * terminate the current lwb and send it on its way. 8103668Sgw25295 */ 8113668Sgw25295 ztp->zit_pad = 0; 8123668Sgw25295 ztp->zit_nused = lwb->lwb_nused; 8133668Sgw25295 ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum; 8143668Sgw25295 zio_nowait(lwb->lwb_zio); 8153668Sgw25295 8163668Sgw25295 /* 8171544Seschrock * By returning NULL the caller will call tx_wait_synced() 8181544Seschrock */ 819789Sahrens return (NULL); 820789Sahrens } 821789Sahrens 8221807Sbonwick ASSERT3U(bp->blk_birth, ==, txg); 8231544Seschrock ztp->zit_pad = 0; 824789Sahrens ztp->zit_nused = lwb->lwb_nused; 825789Sahrens ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum; 8261807Sbonwick bp->blk_cksum = lwb->lwb_blk.blk_cksum; 8271807Sbonwick bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++; 828789Sahrens 829789Sahrens /* 830789Sahrens * Allocate a new log write buffer (lwb). 831789Sahrens */ 832789Sahrens nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 833789Sahrens 834789Sahrens nlwb->lwb_zilog = zilog; 8351807Sbonwick nlwb->lwb_blk = *bp; 836789Sahrens nlwb->lwb_nused = 0; 837789Sahrens nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk); 838789Sahrens nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz); 839789Sahrens nlwb->lwb_max_txg = txg; 8402237Smaybee nlwb->lwb_zio = NULL; 841789Sahrens 842789Sahrens /* 8433063Sperrin * Put new lwb at the end of the log chain 844789Sahrens */ 845789Sahrens mutex_enter(&zilog->zl_lock); 846789Sahrens list_insert_tail(&zilog->zl_lwb_list, nlwb); 8473063Sperrin mutex_exit(&zilog->zl_lock); 8483063Sperrin 8495688Sbonwick /* Record the block for later vdev flushing */ 8505688Sbonwick zil_add_block(zilog, &lwb->lwb_blk); 851789Sahrens 852789Sahrens /* 8532237Smaybee * kick off the write for the old log block 854789Sahrens */ 8552237Smaybee dprintf_bp(&lwb->lwb_blk, "lwb %p txg %llu: ", lwb, txg); 8563063Sperrin ASSERT(lwb->lwb_zio); 8572237Smaybee zio_nowait(lwb->lwb_zio); 858789Sahrens 859789Sahrens return (nlwb); 860789Sahrens } 861789Sahrens 862789Sahrens static lwb_t * 863789Sahrens zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb) 864789Sahrens { 865789Sahrens lr_t *lrc = &itx->itx_lr; /* common log record */ 8662237Smaybee lr_write_t *lr = (lr_write_t *)lrc; 867789Sahrens uint64_t txg = lrc->lrc_txg; 868789Sahrens uint64_t reclen = lrc->lrc_reclen; 8692237Smaybee uint64_t dlen; 870789Sahrens 871789Sahrens if (lwb == NULL) 872789Sahrens return (NULL); 873789Sahrens ASSERT(lwb->lwb_buf != NULL); 874789Sahrens 8752237Smaybee if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY) 8762237Smaybee dlen = P2ROUNDUP_TYPED( 8772237Smaybee lr->lr_length, sizeof (uint64_t), uint64_t); 8782237Smaybee else 8792237Smaybee dlen = 0; 8801669Sperrin 8811669Sperrin zilog->zl_cur_used += (reclen + dlen); 8821669Sperrin 8833063Sperrin zil_lwb_write_init(zilog, lwb); 8843063Sperrin 8851669Sperrin /* 8861669Sperrin * If this record won't fit in the current log block, start a new one. 8871669Sperrin */ 8881669Sperrin if (lwb->lwb_nused + reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) { 8891669Sperrin lwb = zil_lwb_write_start(zilog, lwb); 8902237Smaybee if (lwb == NULL) 8911669Sperrin return (NULL); 8923063Sperrin zil_lwb_write_init(zilog, lwb); 8931669Sperrin ASSERT(lwb->lwb_nused == 0); 8941669Sperrin if (reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) { 8951669Sperrin txg_wait_synced(zilog->zl_dmu_pool, txg); 896789Sahrens return (lwb); 897789Sahrens } 898789Sahrens } 899789Sahrens 9002638Sperrin /* 9012638Sperrin * Update the lrc_seq, to be log record sequence number. See zil.h 9022638Sperrin * Then copy the record to the log buffer. 9032638Sperrin */ 9042638Sperrin lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */ 905789Sahrens bcopy(lrc, lwb->lwb_buf + lwb->lwb_nused, reclen); 9062237Smaybee 9072237Smaybee /* 9082237Smaybee * If it's a write, fetch the data or get its blkptr as appropriate. 9092237Smaybee */ 9102237Smaybee if (lrc->lrc_txtype == TX_WRITE) { 9112237Smaybee if (txg > spa_freeze_txg(zilog->zl_spa)) 9122237Smaybee txg_wait_synced(zilog->zl_dmu_pool, txg); 9132237Smaybee if (itx->itx_wr_state != WR_COPIED) { 9142237Smaybee char *dbuf; 9152237Smaybee int error; 9162237Smaybee 9172237Smaybee /* alignment is guaranteed */ 9182237Smaybee lr = (lr_write_t *)(lwb->lwb_buf + lwb->lwb_nused); 9192237Smaybee if (dlen) { 9202237Smaybee ASSERT(itx->itx_wr_state == WR_NEED_COPY); 9212237Smaybee dbuf = lwb->lwb_buf + lwb->lwb_nused + reclen; 9222237Smaybee lr->lr_common.lrc_reclen += dlen; 9232237Smaybee } else { 9242237Smaybee ASSERT(itx->itx_wr_state == WR_INDIRECT); 9252237Smaybee dbuf = NULL; 9262237Smaybee } 9272237Smaybee error = zilog->zl_get_data( 9282237Smaybee itx->itx_private, lr, dbuf, lwb->lwb_zio); 9292237Smaybee if (error) { 9302237Smaybee ASSERT(error == ENOENT || error == EEXIST || 9312237Smaybee error == EALREADY); 9322237Smaybee return (lwb); 9332237Smaybee } 9342237Smaybee } 9351669Sperrin } 9362237Smaybee 9372237Smaybee lwb->lwb_nused += reclen + dlen; 938789Sahrens lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg); 939789Sahrens ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb)); 940789Sahrens ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0); 941789Sahrens 942789Sahrens return (lwb); 943789Sahrens } 944789Sahrens 945789Sahrens itx_t * 9465331Samw zil_itx_create(uint64_t txtype, size_t lrsize) 947789Sahrens { 948789Sahrens itx_t *itx; 949789Sahrens 9501842Sperrin lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t); 951789Sahrens 952789Sahrens itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP); 953789Sahrens itx->itx_lr.lrc_txtype = txtype; 954789Sahrens itx->itx_lr.lrc_reclen = lrsize; 9556101Sperrin itx->itx_sod = lrsize; /* if write & WR_NEED_COPY will be increased */ 956789Sahrens itx->itx_lr.lrc_seq = 0; /* defensive */ 957789Sahrens 958789Sahrens return (itx); 959789Sahrens } 960789Sahrens 961789Sahrens uint64_t 962789Sahrens zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx) 963789Sahrens { 964789Sahrens uint64_t seq; 965789Sahrens 966789Sahrens ASSERT(itx->itx_lr.lrc_seq == 0); 967789Sahrens 968789Sahrens mutex_enter(&zilog->zl_lock); 969789Sahrens list_insert_tail(&zilog->zl_itx_list, itx); 9706101Sperrin zilog->zl_itx_list_sz += itx->itx_sod; 971789Sahrens itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx); 972789Sahrens itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq; 973789Sahrens mutex_exit(&zilog->zl_lock); 974789Sahrens 975789Sahrens return (seq); 976789Sahrens } 977789Sahrens 978789Sahrens /* 979789Sahrens * Free up all in-memory intent log transactions that have now been synced. 980789Sahrens */ 981789Sahrens static void 982789Sahrens zil_itx_clean(zilog_t *zilog) 983789Sahrens { 984789Sahrens uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa); 985789Sahrens uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa); 9863778Sjohansen list_t clean_list; 987789Sahrens itx_t *itx; 988789Sahrens 9893778Sjohansen list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node)); 9903778Sjohansen 991789Sahrens mutex_enter(&zilog->zl_lock); 9922638Sperrin /* wait for a log writer to finish walking list */ 9932638Sperrin while (zilog->zl_writer) { 9942638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 9952638Sperrin } 9963778Sjohansen 9973778Sjohansen /* 9983778Sjohansen * Move the sync'd log transactions to a separate list so we can call 9993778Sjohansen * kmem_free without holding the zl_lock. 10003778Sjohansen * 10013778Sjohansen * There is no need to set zl_writer as we don't drop zl_lock here 10023778Sjohansen */ 1003789Sahrens while ((itx = list_head(&zilog->zl_itx_list)) != NULL && 1004789Sahrens itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) { 1005789Sahrens list_remove(&zilog->zl_itx_list, itx); 10066101Sperrin zilog->zl_itx_list_sz -= itx->itx_sod; 10073778Sjohansen list_insert_tail(&clean_list, itx); 10083778Sjohansen } 10093778Sjohansen cv_broadcast(&zilog->zl_cv_writer); 10103778Sjohansen mutex_exit(&zilog->zl_lock); 10113778Sjohansen 10123778Sjohansen /* destroy sync'd log transactions */ 10133778Sjohansen while ((itx = list_head(&clean_list)) != NULL) { 10143778Sjohansen list_remove(&clean_list, itx); 1015789Sahrens kmem_free(itx, offsetof(itx_t, itx_lr) 1016789Sahrens + itx->itx_lr.lrc_reclen); 1017789Sahrens } 10183778Sjohansen list_destroy(&clean_list); 1019789Sahrens } 1020789Sahrens 10212638Sperrin /* 10223063Sperrin * If there are any in-memory intent log transactions which have now been 10233063Sperrin * synced then start up a taskq to free them. 10242638Sperrin */ 1025789Sahrens void 1026789Sahrens zil_clean(zilog_t *zilog) 1027789Sahrens { 10283063Sperrin itx_t *itx; 10293063Sperrin 1030789Sahrens mutex_enter(&zilog->zl_lock); 10313063Sperrin itx = list_head(&zilog->zl_itx_list); 10323063Sperrin if ((itx != NULL) && 10333063Sperrin (itx->itx_lr.lrc_txg <= spa_last_synced_txg(zilog->zl_spa))) { 1034789Sahrens (void) taskq_dispatch(zilog->zl_clean_taskq, 10359321SNeil.Perrin@Sun.COM (task_func_t *)zil_itx_clean, zilog, TQ_SLEEP); 10363063Sperrin } 1037789Sahrens mutex_exit(&zilog->zl_lock); 1038789Sahrens } 1039789Sahrens 10407754SJeff.Bonwick@Sun.COM static void 10412638Sperrin zil_commit_writer(zilog_t *zilog, uint64_t seq, uint64_t foid) 1042789Sahrens { 1043789Sahrens uint64_t txg; 10443063Sperrin uint64_t commit_seq = 0; 10452638Sperrin itx_t *itx, *itx_next = (itx_t *)-1; 1046789Sahrens lwb_t *lwb; 1047789Sahrens spa_t *spa; 1048789Sahrens 10492638Sperrin zilog->zl_writer = B_TRUE; 10507754SJeff.Bonwick@Sun.COM ASSERT(zilog->zl_root_zio == NULL); 1051789Sahrens spa = zilog->zl_spa; 1052789Sahrens 1053789Sahrens if (zilog->zl_suspend) { 1054789Sahrens lwb = NULL; 1055789Sahrens } else { 1056789Sahrens lwb = list_tail(&zilog->zl_lwb_list); 1057789Sahrens if (lwb == NULL) { 10582638Sperrin /* 10592638Sperrin * Return if there's nothing to flush before we 10602638Sperrin * dirty the fs by calling zil_create() 10612638Sperrin */ 10622638Sperrin if (list_is_empty(&zilog->zl_itx_list)) { 10632638Sperrin zilog->zl_writer = B_FALSE; 10642638Sperrin return; 10652638Sperrin } 1066789Sahrens mutex_exit(&zilog->zl_lock); 1067789Sahrens zil_create(zilog); 1068789Sahrens mutex_enter(&zilog->zl_lock); 1069789Sahrens lwb = list_tail(&zilog->zl_lwb_list); 1070789Sahrens } 1071789Sahrens } 1072789Sahrens 10733063Sperrin /* Loop through in-memory log transactions filling log blocks. */ 10742638Sperrin DTRACE_PROBE1(zil__cw1, zilog_t *, zilog); 1075789Sahrens for (;;) { 10762638Sperrin /* 10772638Sperrin * Find the next itx to push: 10782638Sperrin * Push all transactions related to specified foid and all 10792638Sperrin * other transactions except TX_WRITE, TX_TRUNCATE, 10802638Sperrin * TX_SETATTR and TX_ACL for all other files. 10812638Sperrin */ 10822638Sperrin if (itx_next != (itx_t *)-1) 10832638Sperrin itx = itx_next; 10842638Sperrin else 10852638Sperrin itx = list_head(&zilog->zl_itx_list); 10862638Sperrin for (; itx != NULL; itx = list_next(&zilog->zl_itx_list, itx)) { 10872638Sperrin if (foid == 0) /* push all foids? */ 10882638Sperrin break; 10893063Sperrin if (itx->itx_sync) /* push all O_[D]SYNC */ 10903063Sperrin break; 10912638Sperrin switch (itx->itx_lr.lrc_txtype) { 10922638Sperrin case TX_SETATTR: 10932638Sperrin case TX_WRITE: 10942638Sperrin case TX_TRUNCATE: 10952638Sperrin case TX_ACL: 10962638Sperrin /* lr_foid is same offset for these records */ 10972638Sperrin if (((lr_write_t *)&itx->itx_lr)->lr_foid 10982638Sperrin != foid) { 10992638Sperrin continue; /* skip this record */ 11002638Sperrin } 11012638Sperrin } 11022638Sperrin break; 11032638Sperrin } 1104789Sahrens if (itx == NULL) 1105789Sahrens break; 1106789Sahrens 1107789Sahrens if ((itx->itx_lr.lrc_seq > seq) && 11082638Sperrin ((lwb == NULL) || (lwb->lwb_nused == 0) || 11096101Sperrin (lwb->lwb_nused + itx->itx_sod > ZIL_BLK_DATA_SZ(lwb)))) { 1110789Sahrens break; 11113063Sperrin } 1112789Sahrens 11132638Sperrin /* 11142638Sperrin * Save the next pointer. Even though we soon drop 11152638Sperrin * zl_lock all threads that may change the list 11162638Sperrin * (another writer or zil_itx_clean) can't do so until 11172638Sperrin * they have zl_writer. 11182638Sperrin */ 11192638Sperrin itx_next = list_next(&zilog->zl_itx_list, itx); 1120789Sahrens list_remove(&zilog->zl_itx_list, itx); 11216101Sperrin zilog->zl_itx_list_sz -= itx->itx_sod; 11223063Sperrin mutex_exit(&zilog->zl_lock); 1123789Sahrens txg = itx->itx_lr.lrc_txg; 1124789Sahrens ASSERT(txg); 1125789Sahrens 1126789Sahrens if (txg > spa_last_synced_txg(spa) || 1127789Sahrens txg > spa_freeze_txg(spa)) 1128789Sahrens lwb = zil_lwb_commit(zilog, itx, lwb); 1129789Sahrens kmem_free(itx, offsetof(itx_t, itx_lr) 1130789Sahrens + itx->itx_lr.lrc_reclen); 1131789Sahrens mutex_enter(&zilog->zl_lock); 1132789Sahrens } 11332638Sperrin DTRACE_PROBE1(zil__cw2, zilog_t *, zilog); 11343063Sperrin /* determine commit sequence number */ 11353063Sperrin itx = list_head(&zilog->zl_itx_list); 11363063Sperrin if (itx) 11373063Sperrin commit_seq = itx->itx_lr.lrc_seq; 11383063Sperrin else 11393063Sperrin commit_seq = zilog->zl_itx_seq; 1140789Sahrens mutex_exit(&zilog->zl_lock); 1141789Sahrens 1142789Sahrens /* write the last block out */ 11433063Sperrin if (lwb != NULL && lwb->lwb_zio != NULL) 1144789Sahrens lwb = zil_lwb_write_start(zilog, lwb); 1145789Sahrens 11461141Sperrin zilog->zl_prev_used = zilog->zl_cur_used; 11471141Sperrin zilog->zl_cur_used = 0; 11481141Sperrin 11492638Sperrin /* 11502638Sperrin * Wait if necessary for the log blocks to be on stable storage. 11512638Sperrin */ 11522638Sperrin if (zilog->zl_root_zio) { 11532638Sperrin DTRACE_PROBE1(zil__cw3, zilog_t *, zilog); 11542638Sperrin (void) zio_wait(zilog->zl_root_zio); 11557754SJeff.Bonwick@Sun.COM zilog->zl_root_zio = NULL; 11562638Sperrin DTRACE_PROBE1(zil__cw4, zilog_t *, zilog); 11575688Sbonwick zil_flush_vdevs(zilog); 1158789Sahrens } 11591141Sperrin 1160789Sahrens if (zilog->zl_log_error || lwb == NULL) { 1161789Sahrens zilog->zl_log_error = 0; 1162789Sahrens txg_wait_synced(zilog->zl_dmu_pool, 0); 1163789Sahrens } 11643063Sperrin 11653063Sperrin mutex_enter(&zilog->zl_lock); 11661141Sperrin zilog->zl_writer = B_FALSE; 11673063Sperrin 11683063Sperrin ASSERT3U(commit_seq, >=, zilog->zl_commit_seq); 11693063Sperrin zilog->zl_commit_seq = commit_seq; 11702638Sperrin } 11712638Sperrin 11722638Sperrin /* 11732638Sperrin * Push zfs transactions to stable storage up to the supplied sequence number. 11742638Sperrin * If foid is 0 push out all transactions, otherwise push only those 11752638Sperrin * for that file or might have been used to create that file. 11762638Sperrin */ 11772638Sperrin void 11782638Sperrin zil_commit(zilog_t *zilog, uint64_t seq, uint64_t foid) 11792638Sperrin { 11802638Sperrin if (zilog == NULL || seq == 0) 11812638Sperrin return; 11822638Sperrin 11832638Sperrin mutex_enter(&zilog->zl_lock); 11842638Sperrin 11852638Sperrin seq = MIN(seq, zilog->zl_itx_seq); /* cap seq at largest itx seq */ 11862638Sperrin 11873063Sperrin while (zilog->zl_writer) { 11882638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 11893063Sperrin if (seq < zilog->zl_commit_seq) { 11903063Sperrin mutex_exit(&zilog->zl_lock); 11913063Sperrin return; 11923063Sperrin } 11933063Sperrin } 11942638Sperrin zil_commit_writer(zilog, seq, foid); /* drops zl_lock */ 11953063Sperrin /* wake up others waiting on the commit */ 11963063Sperrin cv_broadcast(&zilog->zl_cv_writer); 11973063Sperrin mutex_exit(&zilog->zl_lock); 1198789Sahrens } 1199789Sahrens 1200789Sahrens /* 1201789Sahrens * Called in syncing context to free committed log blocks and update log header. 1202789Sahrens */ 1203789Sahrens void 1204789Sahrens zil_sync(zilog_t *zilog, dmu_tx_t *tx) 1205789Sahrens { 12061807Sbonwick zil_header_t *zh = zil_header_in_syncing_context(zilog); 1207789Sahrens uint64_t txg = dmu_tx_get_txg(tx); 1208789Sahrens spa_t *spa = zilog->zl_spa; 1209789Sahrens lwb_t *lwb; 1210789Sahrens 12119396SMatthew.Ahrens@Sun.COM /* 12129396SMatthew.Ahrens@Sun.COM * We don't zero out zl_destroy_txg, so make sure we don't try 12139396SMatthew.Ahrens@Sun.COM * to destroy it twice. 12149396SMatthew.Ahrens@Sun.COM */ 12159396SMatthew.Ahrens@Sun.COM if (spa_sync_pass(spa) != 1) 12169396SMatthew.Ahrens@Sun.COM return; 12179396SMatthew.Ahrens@Sun.COM 12181807Sbonwick mutex_enter(&zilog->zl_lock); 12191807Sbonwick 1220789Sahrens ASSERT(zilog->zl_stop_sync == 0); 1221789Sahrens 12228227SNeil.Perrin@Sun.COM zh->zh_replay_seq = zilog->zl_replayed_seq[txg & TXG_MASK]; 1223789Sahrens 1224789Sahrens if (zilog->zl_destroy_txg == txg) { 12251807Sbonwick blkptr_t blk = zh->zh_log; 12261807Sbonwick 12271807Sbonwick ASSERT(list_head(&zilog->zl_lwb_list) == NULL); 12281807Sbonwick 12291807Sbonwick bzero(zh, sizeof (zil_header_t)); 12308227SNeil.Perrin@Sun.COM bzero(zilog->zl_replayed_seq, sizeof (zilog->zl_replayed_seq)); 12311807Sbonwick 12321807Sbonwick if (zilog->zl_keep_first) { 12331807Sbonwick /* 12341807Sbonwick * If this block was part of log chain that couldn't 12351807Sbonwick * be claimed because a device was missing during 12361807Sbonwick * zil_claim(), but that device later returns, 12371807Sbonwick * then this block could erroneously appear valid. 12381807Sbonwick * To guard against this, assign a new GUID to the new 12391807Sbonwick * log chain so it doesn't matter what blk points to. 12401807Sbonwick */ 12411807Sbonwick zil_init_log_chain(zilog, &blk); 12421807Sbonwick zh->zh_log = blk; 12431807Sbonwick } 1244789Sahrens } 1245789Sahrens 12469701SGeorge.Wilson@Sun.COM while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 12472638Sperrin zh->zh_log = lwb->lwb_blk; 1248789Sahrens if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg) 1249789Sahrens break; 1250789Sahrens list_remove(&zilog->zl_lwb_list, lwb); 1251789Sahrens zio_free_blk(spa, &lwb->lwb_blk, txg); 1252789Sahrens kmem_cache_free(zil_lwb_cache, lwb); 12533668Sgw25295 12543668Sgw25295 /* 12553668Sgw25295 * If we don't have anything left in the lwb list then 12563668Sgw25295 * we've had an allocation failure and we need to zero 12573668Sgw25295 * out the zil_header blkptr so that we don't end 12583668Sgw25295 * up freeing the same block twice. 12593668Sgw25295 */ 12603668Sgw25295 if (list_head(&zilog->zl_lwb_list) == NULL) 12613668Sgw25295 BP_ZERO(&zh->zh_log); 1262789Sahrens } 1263789Sahrens mutex_exit(&zilog->zl_lock); 1264789Sahrens } 1265789Sahrens 1266789Sahrens void 1267789Sahrens zil_init(void) 1268789Sahrens { 1269789Sahrens zil_lwb_cache = kmem_cache_create("zil_lwb_cache", 12702856Snd150628 sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0); 1271789Sahrens } 1272789Sahrens 1273789Sahrens void 1274789Sahrens zil_fini(void) 1275789Sahrens { 1276789Sahrens kmem_cache_destroy(zil_lwb_cache); 1277789Sahrens } 1278789Sahrens 1279789Sahrens zilog_t * 1280789Sahrens zil_alloc(objset_t *os, zil_header_t *zh_phys) 1281789Sahrens { 1282789Sahrens zilog_t *zilog; 1283789Sahrens 1284789Sahrens zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP); 1285789Sahrens 1286789Sahrens zilog->zl_header = zh_phys; 1287789Sahrens zilog->zl_os = os; 1288789Sahrens zilog->zl_spa = dmu_objset_spa(os); 1289789Sahrens zilog->zl_dmu_pool = dmu_objset_pool(os); 12901807Sbonwick zilog->zl_destroy_txg = TXG_INITIAL - 1; 1291789Sahrens 12922856Snd150628 mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL); 12932856Snd150628 1294789Sahrens list_create(&zilog->zl_itx_list, sizeof (itx_t), 1295789Sahrens offsetof(itx_t, itx_node)); 1296789Sahrens 1297789Sahrens list_create(&zilog->zl_lwb_list, sizeof (lwb_t), 1298789Sahrens offsetof(lwb_t, lwb_node)); 1299789Sahrens 13005688Sbonwick mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL); 13015688Sbonwick 13025688Sbonwick avl_create(&zilog->zl_vdev_tree, zil_vdev_compare, 13035688Sbonwick sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node)); 1304789Sahrens 13055913Sperrin cv_init(&zilog->zl_cv_writer, NULL, CV_DEFAULT, NULL); 13065913Sperrin cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL); 13075913Sperrin 1308789Sahrens return (zilog); 1309789Sahrens } 1310789Sahrens 1311789Sahrens void 1312789Sahrens zil_free(zilog_t *zilog) 1313789Sahrens { 1314789Sahrens lwb_t *lwb; 1315789Sahrens 1316789Sahrens zilog->zl_stop_sync = 1; 1317789Sahrens 1318789Sahrens while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 1319789Sahrens list_remove(&zilog->zl_lwb_list, lwb); 1320789Sahrens if (lwb->lwb_buf != NULL) 1321789Sahrens zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 1322789Sahrens kmem_cache_free(zil_lwb_cache, lwb); 1323789Sahrens } 1324789Sahrens list_destroy(&zilog->zl_lwb_list); 1325789Sahrens 13265688Sbonwick avl_destroy(&zilog->zl_vdev_tree); 13275688Sbonwick mutex_destroy(&zilog->zl_vdev_lock); 1328789Sahrens 1329789Sahrens ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1330789Sahrens list_destroy(&zilog->zl_itx_list); 13312856Snd150628 mutex_destroy(&zilog->zl_lock); 1332789Sahrens 13335913Sperrin cv_destroy(&zilog->zl_cv_writer); 13345913Sperrin cv_destroy(&zilog->zl_cv_suspend); 13355913Sperrin 1336789Sahrens kmem_free(zilog, sizeof (zilog_t)); 1337789Sahrens } 1338789Sahrens 1339789Sahrens /* 1340789Sahrens * Open an intent log. 1341789Sahrens */ 1342789Sahrens zilog_t * 1343789Sahrens zil_open(objset_t *os, zil_get_data_t *get_data) 1344789Sahrens { 1345789Sahrens zilog_t *zilog = dmu_objset_zil(os); 1346789Sahrens 1347789Sahrens zilog->zl_get_data = get_data; 1348789Sahrens zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri, 1349789Sahrens 2, 2, TASKQ_PREPOPULATE); 1350789Sahrens 1351789Sahrens return (zilog); 1352789Sahrens } 1353789Sahrens 1354789Sahrens /* 1355789Sahrens * Close an intent log. 1356789Sahrens */ 1357789Sahrens void 1358789Sahrens zil_close(zilog_t *zilog) 1359789Sahrens { 13601807Sbonwick /* 13611807Sbonwick * If the log isn't already committed, mark the objset dirty 13621807Sbonwick * (so zil_sync() will be called) and wait for that txg to sync. 13631807Sbonwick */ 13641807Sbonwick if (!zil_is_committed(zilog)) { 13651807Sbonwick uint64_t txg; 13661807Sbonwick dmu_tx_t *tx = dmu_tx_create(zilog->zl_os); 13671807Sbonwick (void) dmu_tx_assign(tx, TXG_WAIT); 13681807Sbonwick dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 13691807Sbonwick txg = dmu_tx_get_txg(tx); 13701807Sbonwick dmu_tx_commit(tx); 13711807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, txg); 13721807Sbonwick } 13731807Sbonwick 1374789Sahrens taskq_destroy(zilog->zl_clean_taskq); 1375789Sahrens zilog->zl_clean_taskq = NULL; 1376789Sahrens zilog->zl_get_data = NULL; 1377789Sahrens 1378789Sahrens zil_itx_clean(zilog); 1379789Sahrens ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1380789Sahrens } 1381789Sahrens 1382789Sahrens /* 1383789Sahrens * Suspend an intent log. While in suspended mode, we still honor 1384789Sahrens * synchronous semantics, but we rely on txg_wait_synced() to do it. 1385789Sahrens * We suspend the log briefly when taking a snapshot so that the snapshot 1386789Sahrens * contains all the data it's supposed to, and has an empty intent log. 1387789Sahrens */ 1388789Sahrens int 1389789Sahrens zil_suspend(zilog_t *zilog) 1390789Sahrens { 13911807Sbonwick const zil_header_t *zh = zilog->zl_header; 1392789Sahrens 1393789Sahrens mutex_enter(&zilog->zl_lock); 13948989SNeil.Perrin@Sun.COM if (zh->zh_flags & ZIL_REPLAY_NEEDED) { /* unplayed log */ 1395789Sahrens mutex_exit(&zilog->zl_lock); 1396789Sahrens return (EBUSY); 1397789Sahrens } 13981807Sbonwick if (zilog->zl_suspend++ != 0) { 13991807Sbonwick /* 14001807Sbonwick * Someone else already began a suspend. 14011807Sbonwick * Just wait for them to finish. 14021807Sbonwick */ 14031807Sbonwick while (zilog->zl_suspending) 14041807Sbonwick cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock); 14051807Sbonwick mutex_exit(&zilog->zl_lock); 14061807Sbonwick return (0); 14071807Sbonwick } 14081807Sbonwick zilog->zl_suspending = B_TRUE; 1409789Sahrens mutex_exit(&zilog->zl_lock); 1410789Sahrens 14112638Sperrin zil_commit(zilog, UINT64_MAX, 0); 1412789Sahrens 14132638Sperrin /* 14142638Sperrin * Wait for any in-flight log writes to complete. 14152638Sperrin */ 1416789Sahrens mutex_enter(&zilog->zl_lock); 14172638Sperrin while (zilog->zl_writer) 14182638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 1419789Sahrens mutex_exit(&zilog->zl_lock); 1420789Sahrens 14211807Sbonwick zil_destroy(zilog, B_FALSE); 14221807Sbonwick 14231807Sbonwick mutex_enter(&zilog->zl_lock); 14241807Sbonwick zilog->zl_suspending = B_FALSE; 14251807Sbonwick cv_broadcast(&zilog->zl_cv_suspend); 14261807Sbonwick mutex_exit(&zilog->zl_lock); 1427789Sahrens 1428789Sahrens return (0); 1429789Sahrens } 1430789Sahrens 1431789Sahrens void 1432789Sahrens zil_resume(zilog_t *zilog) 1433789Sahrens { 1434789Sahrens mutex_enter(&zilog->zl_lock); 1435789Sahrens ASSERT(zilog->zl_suspend != 0); 1436789Sahrens zilog->zl_suspend--; 1437789Sahrens mutex_exit(&zilog->zl_lock); 1438789Sahrens } 1439789Sahrens 1440789Sahrens typedef struct zil_replay_arg { 1441789Sahrens objset_t *zr_os; 1442789Sahrens zil_replay_func_t **zr_replay; 1443789Sahrens void *zr_arg; 1444789Sahrens boolean_t zr_byteswap; 1445789Sahrens char *zr_lrbuf; 1446789Sahrens } zil_replay_arg_t; 1447789Sahrens 1448789Sahrens static void 1449789Sahrens zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg) 1450789Sahrens { 1451789Sahrens zil_replay_arg_t *zr = zra; 14521807Sbonwick const zil_header_t *zh = zilog->zl_header; 1453789Sahrens uint64_t reclen = lr->lrc_reclen; 1454789Sahrens uint64_t txtype = lr->lrc_txtype; 14553063Sperrin char *name; 14568227SNeil.Perrin@Sun.COM int pass, error; 1457789Sahrens 14588227SNeil.Perrin@Sun.COM if (!zilog->zl_replay) /* giving up */ 1459789Sahrens return; 1460789Sahrens 1461789Sahrens if (lr->lrc_txg < claim_txg) /* already committed */ 1462789Sahrens return; 1463789Sahrens 1464789Sahrens if (lr->lrc_seq <= zh->zh_replay_seq) /* already replayed */ 1465789Sahrens return; 1466789Sahrens 14675331Samw /* Strip case-insensitive bit, still present in log record */ 14685331Samw txtype &= ~TX_CI; 14695331Samw 14708227SNeil.Perrin@Sun.COM if (txtype == 0 || txtype >= TX_MAX_TYPE) { 14718227SNeil.Perrin@Sun.COM error = EINVAL; 14728227SNeil.Perrin@Sun.COM goto bad; 14738227SNeil.Perrin@Sun.COM } 14748227SNeil.Perrin@Sun.COM 1475789Sahrens /* 1476789Sahrens * Make a copy of the data so we can revise and extend it. 1477789Sahrens */ 1478789Sahrens bcopy(lr, zr->zr_lrbuf, reclen); 1479789Sahrens 1480789Sahrens /* 1481789Sahrens * The log block containing this lr may have been byteswapped 1482789Sahrens * so that we can easily examine common fields like lrc_txtype. 1483789Sahrens * However, the log is a mix of different data types, and only the 1484789Sahrens * replay vectors know how to byteswap their records. Therefore, if 1485789Sahrens * the lr was byteswapped, undo it before invoking the replay vector. 1486789Sahrens */ 1487789Sahrens if (zr->zr_byteswap) 1488789Sahrens byteswap_uint64_array(zr->zr_lrbuf, reclen); 1489789Sahrens 1490789Sahrens /* 1491789Sahrens * If this is a TX_WRITE with a blkptr, suck in the data. 1492789Sahrens */ 1493789Sahrens if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) { 1494789Sahrens lr_write_t *lrw = (lr_write_t *)lr; 1495789Sahrens blkptr_t *wbp = &lrw->lr_blkptr; 1496789Sahrens uint64_t wlen = lrw->lr_length; 1497789Sahrens char *wbuf = zr->zr_lrbuf + reclen; 1498789Sahrens 1499789Sahrens if (BP_IS_HOLE(wbp)) { /* compressed to a hole */ 1500789Sahrens bzero(wbuf, wlen); 1501789Sahrens } else { 1502789Sahrens /* 1503789Sahrens * A subsequent write may have overwritten this block, 1504789Sahrens * in which case wbp may have been been freed and 1505789Sahrens * reallocated, and our read of wbp may fail with a 1506789Sahrens * checksum error. We can safely ignore this because 1507789Sahrens * the later write will provide the correct data. 1508789Sahrens */ 15091544Seschrock zbookmark_t zb; 15101544Seschrock 15111544Seschrock zb.zb_objset = dmu_objset_id(zilog->zl_os); 15121544Seschrock zb.zb_object = lrw->lr_foid; 15131544Seschrock zb.zb_level = -1; 15141544Seschrock zb.zb_blkid = lrw->lr_offset / BP_GET_LSIZE(wbp); 15151544Seschrock 1516789Sahrens (void) zio_wait(zio_read(NULL, zilog->zl_spa, 1517789Sahrens wbp, wbuf, BP_GET_LSIZE(wbp), NULL, NULL, 1518789Sahrens ZIO_PRIORITY_SYNC_READ, 15191544Seschrock ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb)); 1520789Sahrens (void) memmove(wbuf, wbuf + lrw->lr_blkoff, wlen); 1521789Sahrens } 1522789Sahrens } 1523789Sahrens 1524789Sahrens /* 1525789Sahrens * We must now do two things atomically: replay this log record, 15268227SNeil.Perrin@Sun.COM * and update the log header sequence number to reflect the fact that 15278227SNeil.Perrin@Sun.COM * we did so. At the end of each replay function the sequence number 15288227SNeil.Perrin@Sun.COM * is updated if we are in replay mode. 1529789Sahrens */ 15308227SNeil.Perrin@Sun.COM for (pass = 1; pass <= 2; pass++) { 15318227SNeil.Perrin@Sun.COM zilog->zl_replaying_seq = lr->lrc_seq; 15328227SNeil.Perrin@Sun.COM /* Only byteswap (if needed) on the 1st pass. */ 15338227SNeil.Perrin@Sun.COM error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lrbuf, 15348227SNeil.Perrin@Sun.COM zr->zr_byteswap && pass == 1); 1535789Sahrens 15363063Sperrin if (!error) 15373063Sperrin return; 15383063Sperrin 15393063Sperrin /* 15403063Sperrin * The DMU's dnode layer doesn't see removes until the txg 15413063Sperrin * commits, so a subsequent claim can spuriously fail with 15428227SNeil.Perrin@Sun.COM * EEXIST. So if we receive any error we try syncing out 15438227SNeil.Perrin@Sun.COM * any removes then retry the transaction. 15443063Sperrin */ 15458227SNeil.Perrin@Sun.COM if (pass == 1) 15463063Sperrin txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0); 1547789Sahrens } 1548789Sahrens 15497904SNeil.Perrin@Sun.COM bad: 15508227SNeil.Perrin@Sun.COM ASSERT(error); 15513063Sperrin name = kmem_alloc(MAXNAMELEN, KM_SLEEP); 15523063Sperrin dmu_objset_name(zr->zr_os, name); 15533063Sperrin cmn_err(CE_WARN, "ZFS replay transaction error %d, " 15545331Samw "dataset %s, seq 0x%llx, txtype %llu %s\n", 15555331Samw error, name, (u_longlong_t)lr->lrc_seq, (u_longlong_t)txtype, 15565331Samw (lr->lrc_txtype & TX_CI) ? "CI" : ""); 15578227SNeil.Perrin@Sun.COM zilog->zl_replay = B_FALSE; 15583063Sperrin kmem_free(name, MAXNAMELEN); 15593063Sperrin } 1560789Sahrens 15613063Sperrin /* ARGSUSED */ 15623063Sperrin static void 15633063Sperrin zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg) 15643063Sperrin { 15653063Sperrin zilog->zl_replay_blks++; 1566789Sahrens } 1567789Sahrens 1568789Sahrens /* 15691362Sperrin * If this dataset has a non-empty intent log, replay it and destroy it. 1570789Sahrens */ 1571789Sahrens void 15728227SNeil.Perrin@Sun.COM zil_replay(objset_t *os, void *arg, zil_replay_func_t *replay_func[TX_MAX_TYPE]) 1573789Sahrens { 1574789Sahrens zilog_t *zilog = dmu_objset_zil(os); 15751807Sbonwick const zil_header_t *zh = zilog->zl_header; 15761807Sbonwick zil_replay_arg_t zr; 15771362Sperrin 15788989SNeil.Perrin@Sun.COM if ((zh->zh_flags & ZIL_REPLAY_NEEDED) == 0) { 15791807Sbonwick zil_destroy(zilog, B_TRUE); 15801362Sperrin return; 15811362Sperrin } 1582789Sahrens 1583789Sahrens zr.zr_os = os; 1584789Sahrens zr.zr_replay = replay_func; 1585789Sahrens zr.zr_arg = arg; 15861807Sbonwick zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log); 1587789Sahrens zr.zr_lrbuf = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP); 1588789Sahrens 1589789Sahrens /* 1590789Sahrens * Wait for in-progress removes to sync before starting replay. 1591789Sahrens */ 1592789Sahrens txg_wait_synced(zilog->zl_dmu_pool, 0); 1593789Sahrens 15948227SNeil.Perrin@Sun.COM zilog->zl_replay = B_TRUE; 15953063Sperrin zilog->zl_replay_time = lbolt; 15963063Sperrin ASSERT(zilog->zl_replay_blks == 0); 15973063Sperrin (void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr, 15981807Sbonwick zh->zh_claim_txg); 1599789Sahrens kmem_free(zr.zr_lrbuf, 2 * SPA_MAXBLOCKSIZE); 1600789Sahrens 16011807Sbonwick zil_destroy(zilog, B_FALSE); 16025712Sahrens txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 16038227SNeil.Perrin@Sun.COM zilog->zl_replay = B_FALSE; 1604789Sahrens } 16051646Sperrin 16061646Sperrin /* 16071646Sperrin * Report whether all transactions are committed 16081646Sperrin */ 16091646Sperrin int 16101646Sperrin zil_is_committed(zilog_t *zilog) 16111646Sperrin { 16121646Sperrin lwb_t *lwb; 16132638Sperrin int ret; 16141646Sperrin 16152638Sperrin mutex_enter(&zilog->zl_lock); 16162638Sperrin while (zilog->zl_writer) 16172638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 16182638Sperrin 16192638Sperrin /* recent unpushed intent log transactions? */ 16202638Sperrin if (!list_is_empty(&zilog->zl_itx_list)) { 16212638Sperrin ret = B_FALSE; 16222638Sperrin goto out; 16232638Sperrin } 16242638Sperrin 16252638Sperrin /* intent log never used? */ 16262638Sperrin lwb = list_head(&zilog->zl_lwb_list); 16272638Sperrin if (lwb == NULL) { 16282638Sperrin ret = B_TRUE; 16292638Sperrin goto out; 16302638Sperrin } 16311646Sperrin 16321646Sperrin /* 16332638Sperrin * more than 1 log buffer means zil_sync() hasn't yet freed 16342638Sperrin * entries after a txg has committed 16351646Sperrin */ 16362638Sperrin if (list_next(&zilog->zl_lwb_list, lwb)) { 16372638Sperrin ret = B_FALSE; 16382638Sperrin goto out; 16392638Sperrin } 16402638Sperrin 16411646Sperrin ASSERT(zil_empty(zilog)); 16422638Sperrin ret = B_TRUE; 16432638Sperrin out: 16442638Sperrin cv_broadcast(&zilog->zl_cv_writer); 16452638Sperrin mutex_exit(&zilog->zl_lock); 16462638Sperrin return (ret); 16471646Sperrin } 16489701SGeorge.Wilson@Sun.COM 16499701SGeorge.Wilson@Sun.COM /* ARGSUSED */ 16509701SGeorge.Wilson@Sun.COM int 16519701SGeorge.Wilson@Sun.COM zil_vdev_offline(char *osname, void *arg) 16529701SGeorge.Wilson@Sun.COM { 16539701SGeorge.Wilson@Sun.COM objset_t *os; 16549701SGeorge.Wilson@Sun.COM zilog_t *zilog; 16559701SGeorge.Wilson@Sun.COM int error; 16569701SGeorge.Wilson@Sun.COM 16579701SGeorge.Wilson@Sun.COM error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_USER, &os); 16589701SGeorge.Wilson@Sun.COM if (error) 16599701SGeorge.Wilson@Sun.COM return (error); 16609701SGeorge.Wilson@Sun.COM 16619701SGeorge.Wilson@Sun.COM zilog = dmu_objset_zil(os); 16629701SGeorge.Wilson@Sun.COM if (zil_suspend(zilog) != 0) 16639701SGeorge.Wilson@Sun.COM error = EEXIST; 16649701SGeorge.Wilson@Sun.COM else 16659701SGeorge.Wilson@Sun.COM zil_resume(zilog); 16669701SGeorge.Wilson@Sun.COM dmu_objset_close(os); 16679701SGeorge.Wilson@Sun.COM return (error); 16689701SGeorge.Wilson@Sun.COM } 1669