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> 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) { 2917754SJeff.Bonwick@Sun.COM err = zio_wait(zio_claim(NULL, spa, first_txg, bp, NULL, NULL, 2927754SJeff.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 /* 3548109SNeil.Perrin@Sun.COM * If we don't already have an initial log block or we have one 3558109SNeil.Perrin@Sun.COM * but it's the wrong endianness then allocate one. 356789Sahrens */ 3578109SNeil.Perrin@Sun.COM if (BP_IS_HOLE(&blk) || BP_SHOULD_BYTESWAP(&blk)) { 3581807Sbonwick tx = dmu_tx_create(zilog->zl_os); 3591807Sbonwick (void) dmu_tx_assign(tx, TXG_WAIT); 3601807Sbonwick dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 3611807Sbonwick txg = dmu_tx_get_txg(tx); 3621807Sbonwick 3638109SNeil.Perrin@Sun.COM if (!BP_IS_HOLE(&blk)) { 3648109SNeil.Perrin@Sun.COM zio_free_blk(zilog->zl_spa, &blk, txg); 3658109SNeil.Perrin@Sun.COM BP_ZERO(&blk); 3668109SNeil.Perrin@Sun.COM } 3678109SNeil.Perrin@Sun.COM 3683063Sperrin error = zio_alloc_blk(zilog->zl_spa, ZIL_MIN_BLKSZ, &blk, 3693063Sperrin NULL, txg); 3701807Sbonwick 3711807Sbonwick if (error == 0) 3721807Sbonwick zil_init_log_chain(zilog, &blk); 3731362Sperrin } 3741807Sbonwick 3751807Sbonwick /* 3761807Sbonwick * Allocate a log write buffer (lwb) for the first log block. 3771807Sbonwick */ 378789Sahrens if (error == 0) { 379789Sahrens lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 380789Sahrens lwb->lwb_zilog = zilog; 381789Sahrens lwb->lwb_blk = blk; 382789Sahrens lwb->lwb_nused = 0; 383789Sahrens lwb->lwb_sz = BP_GET_LSIZE(&lwb->lwb_blk); 384789Sahrens lwb->lwb_buf = zio_buf_alloc(lwb->lwb_sz); 385789Sahrens lwb->lwb_max_txg = txg; 3862237Smaybee lwb->lwb_zio = NULL; 3872237Smaybee 388789Sahrens mutex_enter(&zilog->zl_lock); 389789Sahrens list_insert_tail(&zilog->zl_lwb_list, lwb); 390789Sahrens mutex_exit(&zilog->zl_lock); 391789Sahrens } 392789Sahrens 3931807Sbonwick /* 3941807Sbonwick * If we just allocated the first log block, commit our transaction 3951807Sbonwick * and wait for zil_sync() to stuff the block poiner into zh_log. 3961807Sbonwick * (zh is part of the MOS, so we cannot modify it in open context.) 3971807Sbonwick */ 3981807Sbonwick if (tx != NULL) { 3991807Sbonwick dmu_tx_commit(tx); 4001362Sperrin txg_wait_synced(zilog->zl_dmu_pool, txg); 4011807Sbonwick } 4021807Sbonwick 4031807Sbonwick ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0); 404789Sahrens } 405789Sahrens 406789Sahrens /* 407789Sahrens * In one tx, free all log blocks and clear the log header. 4081807Sbonwick * If keep_first is set, then we're replaying a log with no content. 4091807Sbonwick * We want to keep the first block, however, so that the first 4101807Sbonwick * synchronous transaction doesn't require a txg_wait_synced() 4111807Sbonwick * in zil_create(). We don't need to txg_wait_synced() here either 4121807Sbonwick * when keep_first is set, because both zil_create() and zil_destroy() 4131807Sbonwick * will wait for any in-progress destroys to complete. 414789Sahrens */ 415789Sahrens void 4161807Sbonwick zil_destroy(zilog_t *zilog, boolean_t keep_first) 417789Sahrens { 4181807Sbonwick const zil_header_t *zh = zilog->zl_header; 4191807Sbonwick lwb_t *lwb; 420789Sahrens dmu_tx_t *tx; 421789Sahrens uint64_t txg; 422789Sahrens 4231807Sbonwick /* 4241807Sbonwick * Wait for any previous destroy to complete. 4251807Sbonwick */ 4261807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 427789Sahrens 4281807Sbonwick if (BP_IS_HOLE(&zh->zh_log)) 429789Sahrens return; 430789Sahrens 431789Sahrens tx = dmu_tx_create(zilog->zl_os); 432789Sahrens (void) dmu_tx_assign(tx, TXG_WAIT); 433789Sahrens dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 434789Sahrens txg = dmu_tx_get_txg(tx); 435789Sahrens 4361807Sbonwick mutex_enter(&zilog->zl_lock); 4371807Sbonwick 4385223Sperrin /* 4395223Sperrin * It is possible for the ZIL to get the previously mounted zilog 4405223Sperrin * structure of the same dataset if quickly remounted and the dbuf 4415223Sperrin * eviction has not completed. In this case we can see a non 4425223Sperrin * empty lwb list and keep_first will be set. We fix this by 4435223Sperrin * clearing the keep_first. This will be slower but it's very rare. 4445223Sperrin */ 4455223Sperrin if (!list_is_empty(&zilog->zl_lwb_list) && keep_first) 4465223Sperrin keep_first = B_FALSE; 4475223Sperrin 4481807Sbonwick ASSERT3U(zilog->zl_destroy_txg, <, txg); 449789Sahrens zilog->zl_destroy_txg = txg; 4501807Sbonwick zilog->zl_keep_first = keep_first; 4511807Sbonwick 4521807Sbonwick if (!list_is_empty(&zilog->zl_lwb_list)) { 4531807Sbonwick ASSERT(zh->zh_claim_txg == 0); 4541807Sbonwick ASSERT(!keep_first); 4551807Sbonwick while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 4561807Sbonwick list_remove(&zilog->zl_lwb_list, lwb); 4571807Sbonwick if (lwb->lwb_buf != NULL) 4581807Sbonwick zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 4591807Sbonwick zio_free_blk(zilog->zl_spa, &lwb->lwb_blk, txg); 4601807Sbonwick kmem_cache_free(zil_lwb_cache, lwb); 4611807Sbonwick } 4621807Sbonwick } else { 4631807Sbonwick if (!keep_first) { 4641807Sbonwick (void) zil_parse(zilog, zil_free_log_block, 4651807Sbonwick zil_free_log_record, tx, zh->zh_claim_txg); 4661807Sbonwick } 4671807Sbonwick } 4682638Sperrin mutex_exit(&zilog->zl_lock); 469789Sahrens 470789Sahrens dmu_tx_commit(tx); 471789Sahrens } 472789Sahrens 4738989SNeil.Perrin@Sun.COM /* 4748989SNeil.Perrin@Sun.COM * return true if the initial log block is not valid 4758989SNeil.Perrin@Sun.COM */ 4768989SNeil.Perrin@Sun.COM static boolean_t 4778989SNeil.Perrin@Sun.COM zil_empty(zilog_t *zilog) 4788989SNeil.Perrin@Sun.COM { 4798989SNeil.Perrin@Sun.COM const zil_header_t *zh = zilog->zl_header; 4808989SNeil.Perrin@Sun.COM arc_buf_t *abuf = NULL; 4818989SNeil.Perrin@Sun.COM 4828989SNeil.Perrin@Sun.COM if (BP_IS_HOLE(&zh->zh_log)) 4838989SNeil.Perrin@Sun.COM return (B_TRUE); 4848989SNeil.Perrin@Sun.COM 4858989SNeil.Perrin@Sun.COM if (zil_read_log_block(zilog, &zh->zh_log, &abuf) != 0) 4868989SNeil.Perrin@Sun.COM return (B_TRUE); 4878989SNeil.Perrin@Sun.COM 4888989SNeil.Perrin@Sun.COM VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 4898989SNeil.Perrin@Sun.COM return (B_FALSE); 4908989SNeil.Perrin@Sun.COM } 4918989SNeil.Perrin@Sun.COM 4922199Sahrens int 493789Sahrens zil_claim(char *osname, void *txarg) 494789Sahrens { 495789Sahrens dmu_tx_t *tx = txarg; 496789Sahrens uint64_t first_txg = dmu_tx_get_txg(tx); 497789Sahrens zilog_t *zilog; 498789Sahrens zil_header_t *zh; 499789Sahrens objset_t *os; 500789Sahrens int error; 501789Sahrens 5026689Smaybee error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_USER, &os); 503789Sahrens if (error) { 5047294Sperrin cmn_err(CE_WARN, "can't open objset for %s", osname); 5052199Sahrens return (0); 506789Sahrens } 507789Sahrens 508789Sahrens zilog = dmu_objset_zil(os); 5091807Sbonwick zh = zil_header_in_syncing_context(zilog); 510789Sahrens 511789Sahrens /* 5128989SNeil.Perrin@Sun.COM * Record here whether the zil has any records to replay. 5138989SNeil.Perrin@Sun.COM * If the header block pointer is null or the block points 5148989SNeil.Perrin@Sun.COM * to the stubby then we know there are no valid log records. 5158989SNeil.Perrin@Sun.COM * We use the header to store this state as the the zilog gets 5168989SNeil.Perrin@Sun.COM * freed later in dmu_objset_close(). 5178989SNeil.Perrin@Sun.COM * The flags (and the rest of the header fields) are cleared in 5188989SNeil.Perrin@Sun.COM * zil_sync() as a result of a zil_destroy(), after replaying the log. 5198989SNeil.Perrin@Sun.COM * 5208989SNeil.Perrin@Sun.COM * Note, the intent log can be empty but still need the 5218989SNeil.Perrin@Sun.COM * stubby to be claimed. 5228989SNeil.Perrin@Sun.COM */ 5238989SNeil.Perrin@Sun.COM if (!zil_empty(zilog)) 5248989SNeil.Perrin@Sun.COM zh->zh_flags |= ZIL_REPLAY_NEEDED; 5258989SNeil.Perrin@Sun.COM 5268989SNeil.Perrin@Sun.COM /* 5271807Sbonwick * Claim all log blocks if we haven't already done so, and remember 5281807Sbonwick * the highest claimed sequence number. This ensures that if we can 5291807Sbonwick * read only part of the log now (e.g. due to a missing device), 5301807Sbonwick * but we can read the entire log later, we will not try to replay 5311807Sbonwick * or destroy beyond the last block we successfully claimed. 532789Sahrens */ 533789Sahrens ASSERT3U(zh->zh_claim_txg, <=, first_txg); 534789Sahrens if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) { 535789Sahrens zh->zh_claim_txg = first_txg; 5361807Sbonwick zh->zh_claim_seq = zil_parse(zilog, zil_claim_log_block, 5371807Sbonwick zil_claim_log_record, tx, first_txg); 538789Sahrens dsl_dataset_dirty(dmu_objset_ds(os), tx); 539789Sahrens } 5401807Sbonwick 541789Sahrens ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1)); 542789Sahrens dmu_objset_close(os); 5432199Sahrens return (0); 544789Sahrens } 545789Sahrens 5467294Sperrin /* 5477294Sperrin * Check the log by walking the log chain. 5487294Sperrin * Checksum errors are ok as they indicate the end of the chain. 5497294Sperrin * Any other error (no device or read failure) returns an error. 5507294Sperrin */ 5517294Sperrin /* ARGSUSED */ 5527294Sperrin int 5537294Sperrin zil_check_log_chain(char *osname, void *txarg) 5547294Sperrin { 5557294Sperrin zilog_t *zilog; 5567294Sperrin zil_header_t *zh; 5577294Sperrin blkptr_t blk; 5587294Sperrin arc_buf_t *abuf; 5597294Sperrin objset_t *os; 5607294Sperrin char *lrbuf; 5617294Sperrin zil_trailer_t *ztp; 5627294Sperrin int error; 5637294Sperrin 5647294Sperrin error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_USER, &os); 5657294Sperrin if (error) { 5667294Sperrin cmn_err(CE_WARN, "can't open objset for %s", osname); 5677294Sperrin return (0); 5687294Sperrin } 5697294Sperrin 5707294Sperrin zilog = dmu_objset_zil(os); 5717294Sperrin zh = zil_header_in_syncing_context(zilog); 5727294Sperrin blk = zh->zh_log; 5737294Sperrin if (BP_IS_HOLE(&blk)) { 5747294Sperrin dmu_objset_close(os); 5757294Sperrin return (0); /* no chain */ 5767294Sperrin } 5777294Sperrin 5787294Sperrin for (;;) { 5797294Sperrin error = zil_read_log_block(zilog, &blk, &abuf); 5807294Sperrin if (error) 5817294Sperrin break; 5827294Sperrin lrbuf = abuf->b_data; 5837294Sperrin ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1; 5847294Sperrin blk = ztp->zit_next_blk; 5857294Sperrin VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 5867294Sperrin } 5877294Sperrin dmu_objset_close(os); 5887294Sperrin if (error == ECKSUM) 5897294Sperrin return (0); /* normal end of chain */ 5907294Sperrin return (error); 5917294Sperrin } 5927294Sperrin 5937294Sperrin /* 5947294Sperrin * Clear a log chain 5957294Sperrin */ 5967294Sperrin /* ARGSUSED */ 5977294Sperrin int 5987294Sperrin zil_clear_log_chain(char *osname, void *txarg) 5997294Sperrin { 6007294Sperrin zilog_t *zilog; 6017294Sperrin zil_header_t *zh; 6027294Sperrin objset_t *os; 6037294Sperrin dmu_tx_t *tx; 6047294Sperrin int error; 6057294Sperrin 6067294Sperrin error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_USER, &os); 6077294Sperrin if (error) { 6087294Sperrin cmn_err(CE_WARN, "can't open objset for %s", osname); 6097294Sperrin return (0); 6107294Sperrin } 6117294Sperrin 6127294Sperrin zilog = dmu_objset_zil(os); 6137294Sperrin tx = dmu_tx_create(zilog->zl_os); 6147294Sperrin (void) dmu_tx_assign(tx, TXG_WAIT); 6157294Sperrin zh = zil_header_in_syncing_context(zilog); 6167294Sperrin BP_ZERO(&zh->zh_log); 6177294Sperrin dsl_dataset_dirty(dmu_objset_ds(os), tx); 6187294Sperrin dmu_tx_commit(tx); 6197294Sperrin dmu_objset_close(os); 6207294Sperrin return (0); 6217294Sperrin } 6227294Sperrin 6235688Sbonwick static int 6245688Sbonwick zil_vdev_compare(const void *x1, const void *x2) 625789Sahrens { 6265875Sperrin uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev; 6275875Sperrin uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev; 6285688Sbonwick 6295688Sbonwick if (v1 < v2) 6305688Sbonwick return (-1); 6315688Sbonwick if (v1 > v2) 6325688Sbonwick return (1); 6335688Sbonwick 6345688Sbonwick return (0); 6355688Sbonwick } 6365688Sbonwick 6375688Sbonwick void 6385688Sbonwick zil_add_block(zilog_t *zilog, blkptr_t *bp) 6395688Sbonwick { 6405688Sbonwick avl_tree_t *t = &zilog->zl_vdev_tree; 6415688Sbonwick avl_index_t where; 6425688Sbonwick zil_vdev_node_t *zv, zvsearch; 6435688Sbonwick int ndvas = BP_GET_NDVAS(bp); 6445688Sbonwick int i; 645789Sahrens 6462986Sek110237 if (zfs_nocacheflush) 647789Sahrens return; 648789Sahrens 6495688Sbonwick ASSERT(zilog->zl_writer); 6505688Sbonwick 6515688Sbonwick /* 6525688Sbonwick * Even though we're zl_writer, we still need a lock because the 6535688Sbonwick * zl_get_data() callbacks may have dmu_sync() done callbacks 6545688Sbonwick * that will run concurrently. 6555688Sbonwick */ 6565688Sbonwick mutex_enter(&zilog->zl_vdev_lock); 6575688Sbonwick for (i = 0; i < ndvas; i++) { 6585688Sbonwick zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]); 6595688Sbonwick if (avl_find(t, &zvsearch, &where) == NULL) { 6605688Sbonwick zv = kmem_alloc(sizeof (*zv), KM_SLEEP); 6615688Sbonwick zv->zv_vdev = zvsearch.zv_vdev; 6625688Sbonwick avl_insert(t, zv, where); 6633063Sperrin } 6643063Sperrin } 6655688Sbonwick mutex_exit(&zilog->zl_vdev_lock); 6663063Sperrin } 6673063Sperrin 668789Sahrens void 6692638Sperrin zil_flush_vdevs(zilog_t *zilog) 670789Sahrens { 6713063Sperrin spa_t *spa = zilog->zl_spa; 6725688Sbonwick avl_tree_t *t = &zilog->zl_vdev_tree; 6735688Sbonwick void *cookie = NULL; 6745688Sbonwick zil_vdev_node_t *zv; 6755688Sbonwick zio_t *zio; 6763063Sperrin 6773063Sperrin ASSERT(zilog->zl_writer); 678789Sahrens 6795688Sbonwick /* 6805688Sbonwick * We don't need zl_vdev_lock here because we're the zl_writer, 6815688Sbonwick * and all zl_get_data() callbacks are done. 6825688Sbonwick */ 6835688Sbonwick if (avl_numnodes(t) == 0) 6845688Sbonwick return; 6855688Sbonwick 6867754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 6875688Sbonwick 6887754SJeff.Bonwick@Sun.COM zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); 6895688Sbonwick 6905688Sbonwick while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) { 6915688Sbonwick vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev); 6925688Sbonwick if (vd != NULL) 6935688Sbonwick zio_flush(zio, vd); 6945688Sbonwick kmem_free(zv, sizeof (*zv)); 6953063Sperrin } 696789Sahrens 697789Sahrens /* 698789Sahrens * Wait for all the flushes to complete. Not all devices actually 699789Sahrens * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails. 700789Sahrens */ 7015688Sbonwick (void) zio_wait(zio); 7025688Sbonwick 7037754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 704789Sahrens } 705789Sahrens 706789Sahrens /* 707789Sahrens * Function called when a log block write completes 708789Sahrens */ 709789Sahrens static void 710789Sahrens zil_lwb_write_done(zio_t *zio) 711789Sahrens { 712789Sahrens lwb_t *lwb = zio->io_private; 713789Sahrens zilog_t *zilog = lwb->lwb_zilog; 714789Sahrens 7157754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF); 7167754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_CHECKSUM(zio->io_bp) == ZIO_CHECKSUM_ZILOG); 7177754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_TYPE(zio->io_bp) == DMU_OT_INTENT_LOG); 7187754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 7197754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_BYTEORDER(zio->io_bp) == ZFS_HOST_BYTEORDER); 7207754SJeff.Bonwick@Sun.COM ASSERT(!BP_IS_GANG(zio->io_bp)); 7217754SJeff.Bonwick@Sun.COM ASSERT(!BP_IS_HOLE(zio->io_bp)); 7227754SJeff.Bonwick@Sun.COM ASSERT(zio->io_bp->blk_fill == 0); 7237754SJeff.Bonwick@Sun.COM 724789Sahrens /* 725*9493SNeil.Perrin@Sun.COM * Ensure the lwb buffer pointer is cleared before releasing 726*9493SNeil.Perrin@Sun.COM * the txg. If we have had an allocation failure and 727*9493SNeil.Perrin@Sun.COM * the txg is waiting to sync then we want want zil_sync() 728*9493SNeil.Perrin@Sun.COM * to remove the lwb so that it's not picked up as the next new 729*9493SNeil.Perrin@Sun.COM * one in zil_commit_writer(). zil_sync() will only remove 730*9493SNeil.Perrin@Sun.COM * the lwb if lwb_buf is null. 731789Sahrens */ 732789Sahrens zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 733789Sahrens mutex_enter(&zilog->zl_lock); 734789Sahrens lwb->lwb_buf = NULL; 7354527Sperrin if (zio->io_error) 736789Sahrens zilog->zl_log_error = B_TRUE; 737789Sahrens mutex_exit(&zilog->zl_lock); 738*9493SNeil.Perrin@Sun.COM 739*9493SNeil.Perrin@Sun.COM /* 740*9493SNeil.Perrin@Sun.COM * Now that we've written this log block, we have a stable pointer 741*9493SNeil.Perrin@Sun.COM * to the next block in the chain, so it's OK to let the txg in 742*9493SNeil.Perrin@Sun.COM * which we allocated the next block sync. 743*9493SNeil.Perrin@Sun.COM */ 744*9493SNeil.Perrin@Sun.COM txg_rele_to_sync(&lwb->lwb_txgh); 745789Sahrens } 746789Sahrens 747789Sahrens /* 7482237Smaybee * Initialize the io for a log block. 7492237Smaybee */ 7502237Smaybee static void 7512237Smaybee zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb) 7522237Smaybee { 7532237Smaybee zbookmark_t zb; 7542237Smaybee 7552237Smaybee zb.zb_objset = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET]; 7562237Smaybee zb.zb_object = 0; 7572237Smaybee zb.zb_level = -1; 7582237Smaybee zb.zb_blkid = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 7592237Smaybee 7602638Sperrin if (zilog->zl_root_zio == NULL) { 7612638Sperrin zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL, 7622638Sperrin ZIO_FLAG_CANFAIL); 7632638Sperrin } 7643063Sperrin if (lwb->lwb_zio == NULL) { 7653063Sperrin lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa, 7667754SJeff.Bonwick@Sun.COM 0, &lwb->lwb_blk, lwb->lwb_buf, 7673063Sperrin lwb->lwb_sz, zil_lwb_write_done, lwb, 7684527Sperrin ZIO_PRIORITY_LOG_WRITE, ZIO_FLAG_CANFAIL, &zb); 7693063Sperrin } 7702237Smaybee } 7712237Smaybee 7722237Smaybee /* 773789Sahrens * Start a log block write and advance to the next log block. 774789Sahrens * Calls are serialized. 775789Sahrens */ 776789Sahrens static lwb_t * 777789Sahrens zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb) 778789Sahrens { 779789Sahrens lwb_t *nlwb; 780789Sahrens zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1; 7811807Sbonwick spa_t *spa = zilog->zl_spa; 7821807Sbonwick blkptr_t *bp = &ztp->zit_next_blk; 783789Sahrens uint64_t txg; 784789Sahrens uint64_t zil_blksz; 785789Sahrens int error; 786789Sahrens 787789Sahrens ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb)); 788789Sahrens 789789Sahrens /* 790789Sahrens * Allocate the next block and save its address in this block 791789Sahrens * before writing it in order to establish the log chain. 792789Sahrens * Note that if the allocation of nlwb synced before we wrote 793789Sahrens * the block that points at it (lwb), we'd leak it if we crashed. 794789Sahrens * Therefore, we don't do txg_rele_to_sync() until zil_lwb_write_done(). 795789Sahrens */ 796789Sahrens txg = txg_hold_open(zilog->zl_dmu_pool, &lwb->lwb_txgh); 797789Sahrens txg_rele_to_quiesce(&lwb->lwb_txgh); 798789Sahrens 799789Sahrens /* 8001141Sperrin * Pick a ZIL blocksize. We request a size that is the 8011141Sperrin * maximum of the previous used size, the current used size and 8021141Sperrin * the amount waiting in the queue. 803789Sahrens */ 8042237Smaybee zil_blksz = MAX(zilog->zl_prev_used, 8052237Smaybee zilog->zl_cur_used + sizeof (*ztp)); 8061141Sperrin zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp)); 8071842Sperrin zil_blksz = P2ROUNDUP_TYPED(zil_blksz, ZIL_MIN_BLKSZ, uint64_t); 8081141Sperrin if (zil_blksz > ZIL_MAX_BLKSZ) 8091141Sperrin zil_blksz = ZIL_MAX_BLKSZ; 810789Sahrens 8113063Sperrin BP_ZERO(bp); 8123063Sperrin /* pass the old blkptr in order to spread log blocks across devs */ 8133063Sperrin error = zio_alloc_blk(spa, zil_blksz, bp, &lwb->lwb_blk, txg); 814789Sahrens if (error) { 8153668Sgw25295 dmu_tx_t *tx = dmu_tx_create_assigned(zilog->zl_dmu_pool, txg); 8163668Sgw25295 8171544Seschrock /* 8183668Sgw25295 * We dirty the dataset to ensure that zil_sync() will 8193668Sgw25295 * be called to remove this lwb from our zl_lwb_list. 8203668Sgw25295 * Failing to do so, may leave an lwb with a NULL lwb_buf 8213668Sgw25295 * hanging around on the zl_lwb_list. 8223668Sgw25295 */ 8233668Sgw25295 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 8243848Sgw25295 dmu_tx_commit(tx); 8253668Sgw25295 8263668Sgw25295 /* 8273668Sgw25295 * Since we've just experienced an allocation failure so we 8283668Sgw25295 * terminate the current lwb and send it on its way. 8293668Sgw25295 */ 8303668Sgw25295 ztp->zit_pad = 0; 8313668Sgw25295 ztp->zit_nused = lwb->lwb_nused; 8323668Sgw25295 ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum; 8333668Sgw25295 zio_nowait(lwb->lwb_zio); 8343668Sgw25295 8353668Sgw25295 /* 8361544Seschrock * By returning NULL the caller will call tx_wait_synced() 8371544Seschrock */ 838789Sahrens return (NULL); 839789Sahrens } 840789Sahrens 8411807Sbonwick ASSERT3U(bp->blk_birth, ==, txg); 8421544Seschrock ztp->zit_pad = 0; 843789Sahrens ztp->zit_nused = lwb->lwb_nused; 844789Sahrens ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum; 8451807Sbonwick bp->blk_cksum = lwb->lwb_blk.blk_cksum; 8461807Sbonwick bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++; 847789Sahrens 848789Sahrens /* 849789Sahrens * Allocate a new log write buffer (lwb). 850789Sahrens */ 851789Sahrens nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 852789Sahrens 853789Sahrens nlwb->lwb_zilog = zilog; 8541807Sbonwick nlwb->lwb_blk = *bp; 855789Sahrens nlwb->lwb_nused = 0; 856789Sahrens nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk); 857789Sahrens nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz); 858789Sahrens nlwb->lwb_max_txg = txg; 8592237Smaybee nlwb->lwb_zio = NULL; 860789Sahrens 861789Sahrens /* 8623063Sperrin * Put new lwb at the end of the log chain 863789Sahrens */ 864789Sahrens mutex_enter(&zilog->zl_lock); 865789Sahrens list_insert_tail(&zilog->zl_lwb_list, nlwb); 8663063Sperrin mutex_exit(&zilog->zl_lock); 8673063Sperrin 8685688Sbonwick /* Record the block for later vdev flushing */ 8695688Sbonwick zil_add_block(zilog, &lwb->lwb_blk); 870789Sahrens 871789Sahrens /* 8722237Smaybee * kick off the write for the old log block 873789Sahrens */ 8742237Smaybee dprintf_bp(&lwb->lwb_blk, "lwb %p txg %llu: ", lwb, txg); 8753063Sperrin ASSERT(lwb->lwb_zio); 8762237Smaybee zio_nowait(lwb->lwb_zio); 877789Sahrens 878789Sahrens return (nlwb); 879789Sahrens } 880789Sahrens 881789Sahrens static lwb_t * 882789Sahrens zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb) 883789Sahrens { 884789Sahrens lr_t *lrc = &itx->itx_lr; /* common log record */ 8852237Smaybee lr_write_t *lr = (lr_write_t *)lrc; 886789Sahrens uint64_t txg = lrc->lrc_txg; 887789Sahrens uint64_t reclen = lrc->lrc_reclen; 8882237Smaybee uint64_t dlen; 889789Sahrens 890789Sahrens if (lwb == NULL) 891789Sahrens return (NULL); 892789Sahrens ASSERT(lwb->lwb_buf != NULL); 893789Sahrens 8942237Smaybee if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY) 8952237Smaybee dlen = P2ROUNDUP_TYPED( 8962237Smaybee lr->lr_length, sizeof (uint64_t), uint64_t); 8972237Smaybee else 8982237Smaybee dlen = 0; 8991669Sperrin 9001669Sperrin zilog->zl_cur_used += (reclen + dlen); 9011669Sperrin 9023063Sperrin zil_lwb_write_init(zilog, lwb); 9033063Sperrin 9041669Sperrin /* 9051669Sperrin * If this record won't fit in the current log block, start a new one. 9061669Sperrin */ 9071669Sperrin if (lwb->lwb_nused + reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) { 9081669Sperrin lwb = zil_lwb_write_start(zilog, lwb); 9092237Smaybee if (lwb == NULL) 9101669Sperrin return (NULL); 9113063Sperrin zil_lwb_write_init(zilog, lwb); 9121669Sperrin ASSERT(lwb->lwb_nused == 0); 9131669Sperrin if (reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) { 9141669Sperrin txg_wait_synced(zilog->zl_dmu_pool, txg); 915789Sahrens return (lwb); 916789Sahrens } 917789Sahrens } 918789Sahrens 9192638Sperrin /* 9202638Sperrin * Update the lrc_seq, to be log record sequence number. See zil.h 9212638Sperrin * Then copy the record to the log buffer. 9222638Sperrin */ 9232638Sperrin lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */ 924789Sahrens bcopy(lrc, lwb->lwb_buf + lwb->lwb_nused, reclen); 9252237Smaybee 9262237Smaybee /* 9272237Smaybee * If it's a write, fetch the data or get its blkptr as appropriate. 9282237Smaybee */ 9292237Smaybee if (lrc->lrc_txtype == TX_WRITE) { 9302237Smaybee if (txg > spa_freeze_txg(zilog->zl_spa)) 9312237Smaybee txg_wait_synced(zilog->zl_dmu_pool, txg); 9322237Smaybee if (itx->itx_wr_state != WR_COPIED) { 9332237Smaybee char *dbuf; 9342237Smaybee int error; 9352237Smaybee 9362237Smaybee /* alignment is guaranteed */ 9372237Smaybee lr = (lr_write_t *)(lwb->lwb_buf + lwb->lwb_nused); 9382237Smaybee if (dlen) { 9392237Smaybee ASSERT(itx->itx_wr_state == WR_NEED_COPY); 9402237Smaybee dbuf = lwb->lwb_buf + lwb->lwb_nused + reclen; 9412237Smaybee lr->lr_common.lrc_reclen += dlen; 9422237Smaybee } else { 9432237Smaybee ASSERT(itx->itx_wr_state == WR_INDIRECT); 9442237Smaybee dbuf = NULL; 9452237Smaybee } 9462237Smaybee error = zilog->zl_get_data( 9472237Smaybee itx->itx_private, lr, dbuf, lwb->lwb_zio); 9482237Smaybee if (error) { 9492237Smaybee ASSERT(error == ENOENT || error == EEXIST || 9502237Smaybee error == EALREADY); 9512237Smaybee return (lwb); 9522237Smaybee } 9532237Smaybee } 9541669Sperrin } 9552237Smaybee 9562237Smaybee lwb->lwb_nused += reclen + dlen; 957789Sahrens lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg); 958789Sahrens ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb)); 959789Sahrens ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0); 960789Sahrens 961789Sahrens return (lwb); 962789Sahrens } 963789Sahrens 964789Sahrens itx_t * 9655331Samw zil_itx_create(uint64_t txtype, size_t lrsize) 966789Sahrens { 967789Sahrens itx_t *itx; 968789Sahrens 9691842Sperrin lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t); 970789Sahrens 971789Sahrens itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP); 972789Sahrens itx->itx_lr.lrc_txtype = txtype; 973789Sahrens itx->itx_lr.lrc_reclen = lrsize; 9746101Sperrin itx->itx_sod = lrsize; /* if write & WR_NEED_COPY will be increased */ 975789Sahrens itx->itx_lr.lrc_seq = 0; /* defensive */ 976789Sahrens 977789Sahrens return (itx); 978789Sahrens } 979789Sahrens 980789Sahrens uint64_t 981789Sahrens zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx) 982789Sahrens { 983789Sahrens uint64_t seq; 984789Sahrens 985789Sahrens ASSERT(itx->itx_lr.lrc_seq == 0); 986789Sahrens 987789Sahrens mutex_enter(&zilog->zl_lock); 988789Sahrens list_insert_tail(&zilog->zl_itx_list, itx); 9896101Sperrin zilog->zl_itx_list_sz += itx->itx_sod; 990789Sahrens itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx); 991789Sahrens itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq; 992789Sahrens mutex_exit(&zilog->zl_lock); 993789Sahrens 994789Sahrens return (seq); 995789Sahrens } 996789Sahrens 997789Sahrens /* 998789Sahrens * Free up all in-memory intent log transactions that have now been synced. 999789Sahrens */ 1000789Sahrens static void 1001789Sahrens zil_itx_clean(zilog_t *zilog) 1002789Sahrens { 1003789Sahrens uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa); 1004789Sahrens uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa); 10053778Sjohansen list_t clean_list; 1006789Sahrens itx_t *itx; 1007789Sahrens 10083778Sjohansen list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node)); 10093778Sjohansen 1010789Sahrens mutex_enter(&zilog->zl_lock); 10112638Sperrin /* wait for a log writer to finish walking list */ 10122638Sperrin while (zilog->zl_writer) { 10132638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 10142638Sperrin } 10153778Sjohansen 10163778Sjohansen /* 10173778Sjohansen * Move the sync'd log transactions to a separate list so we can call 10183778Sjohansen * kmem_free without holding the zl_lock. 10193778Sjohansen * 10203778Sjohansen * There is no need to set zl_writer as we don't drop zl_lock here 10213778Sjohansen */ 1022789Sahrens while ((itx = list_head(&zilog->zl_itx_list)) != NULL && 1023789Sahrens itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) { 1024789Sahrens list_remove(&zilog->zl_itx_list, itx); 10256101Sperrin zilog->zl_itx_list_sz -= itx->itx_sod; 10263778Sjohansen list_insert_tail(&clean_list, itx); 10273778Sjohansen } 10283778Sjohansen cv_broadcast(&zilog->zl_cv_writer); 10293778Sjohansen mutex_exit(&zilog->zl_lock); 10303778Sjohansen 10313778Sjohansen /* destroy sync'd log transactions */ 10323778Sjohansen while ((itx = list_head(&clean_list)) != NULL) { 10333778Sjohansen list_remove(&clean_list, itx); 1034789Sahrens kmem_free(itx, offsetof(itx_t, itx_lr) 1035789Sahrens + itx->itx_lr.lrc_reclen); 1036789Sahrens } 10373778Sjohansen list_destroy(&clean_list); 1038789Sahrens } 1039789Sahrens 10402638Sperrin /* 10413063Sperrin * If there are any in-memory intent log transactions which have now been 10423063Sperrin * synced then start up a taskq to free them. 10432638Sperrin */ 1044789Sahrens void 1045789Sahrens zil_clean(zilog_t *zilog) 1046789Sahrens { 10473063Sperrin itx_t *itx; 10483063Sperrin 1049789Sahrens mutex_enter(&zilog->zl_lock); 10503063Sperrin itx = list_head(&zilog->zl_itx_list); 10513063Sperrin if ((itx != NULL) && 10523063Sperrin (itx->itx_lr.lrc_txg <= spa_last_synced_txg(zilog->zl_spa))) { 1053789Sahrens (void) taskq_dispatch(zilog->zl_clean_taskq, 10549321SNeil.Perrin@Sun.COM (task_func_t *)zil_itx_clean, zilog, TQ_SLEEP); 10553063Sperrin } 1056789Sahrens mutex_exit(&zilog->zl_lock); 1057789Sahrens } 1058789Sahrens 10597754SJeff.Bonwick@Sun.COM static void 10602638Sperrin zil_commit_writer(zilog_t *zilog, uint64_t seq, uint64_t foid) 1061789Sahrens { 1062789Sahrens uint64_t txg; 10633063Sperrin uint64_t commit_seq = 0; 10642638Sperrin itx_t *itx, *itx_next = (itx_t *)-1; 1065789Sahrens lwb_t *lwb; 1066789Sahrens spa_t *spa; 1067789Sahrens 10682638Sperrin zilog->zl_writer = B_TRUE; 10697754SJeff.Bonwick@Sun.COM ASSERT(zilog->zl_root_zio == NULL); 1070789Sahrens spa = zilog->zl_spa; 1071789Sahrens 1072789Sahrens if (zilog->zl_suspend) { 1073789Sahrens lwb = NULL; 1074789Sahrens } else { 1075789Sahrens lwb = list_tail(&zilog->zl_lwb_list); 1076789Sahrens if (lwb == NULL) { 10772638Sperrin /* 10782638Sperrin * Return if there's nothing to flush before we 10792638Sperrin * dirty the fs by calling zil_create() 10802638Sperrin */ 10812638Sperrin if (list_is_empty(&zilog->zl_itx_list)) { 10822638Sperrin zilog->zl_writer = B_FALSE; 10832638Sperrin return; 10842638Sperrin } 1085789Sahrens mutex_exit(&zilog->zl_lock); 1086789Sahrens zil_create(zilog); 1087789Sahrens mutex_enter(&zilog->zl_lock); 1088789Sahrens lwb = list_tail(&zilog->zl_lwb_list); 1089789Sahrens } 1090789Sahrens } 1091789Sahrens 10923063Sperrin /* Loop through in-memory log transactions filling log blocks. */ 10932638Sperrin DTRACE_PROBE1(zil__cw1, zilog_t *, zilog); 1094789Sahrens for (;;) { 10952638Sperrin /* 10962638Sperrin * Find the next itx to push: 10972638Sperrin * Push all transactions related to specified foid and all 10982638Sperrin * other transactions except TX_WRITE, TX_TRUNCATE, 10992638Sperrin * TX_SETATTR and TX_ACL for all other files. 11002638Sperrin */ 11012638Sperrin if (itx_next != (itx_t *)-1) 11022638Sperrin itx = itx_next; 11032638Sperrin else 11042638Sperrin itx = list_head(&zilog->zl_itx_list); 11052638Sperrin for (; itx != NULL; itx = list_next(&zilog->zl_itx_list, itx)) { 11062638Sperrin if (foid == 0) /* push all foids? */ 11072638Sperrin break; 11083063Sperrin if (itx->itx_sync) /* push all O_[D]SYNC */ 11093063Sperrin break; 11102638Sperrin switch (itx->itx_lr.lrc_txtype) { 11112638Sperrin case TX_SETATTR: 11122638Sperrin case TX_WRITE: 11132638Sperrin case TX_TRUNCATE: 11142638Sperrin case TX_ACL: 11152638Sperrin /* lr_foid is same offset for these records */ 11162638Sperrin if (((lr_write_t *)&itx->itx_lr)->lr_foid 11172638Sperrin != foid) { 11182638Sperrin continue; /* skip this record */ 11192638Sperrin } 11202638Sperrin } 11212638Sperrin break; 11222638Sperrin } 1123789Sahrens if (itx == NULL) 1124789Sahrens break; 1125789Sahrens 1126789Sahrens if ((itx->itx_lr.lrc_seq > seq) && 11272638Sperrin ((lwb == NULL) || (lwb->lwb_nused == 0) || 11286101Sperrin (lwb->lwb_nused + itx->itx_sod > ZIL_BLK_DATA_SZ(lwb)))) { 1129789Sahrens break; 11303063Sperrin } 1131789Sahrens 11322638Sperrin /* 11332638Sperrin * Save the next pointer. Even though we soon drop 11342638Sperrin * zl_lock all threads that may change the list 11352638Sperrin * (another writer or zil_itx_clean) can't do so until 11362638Sperrin * they have zl_writer. 11372638Sperrin */ 11382638Sperrin itx_next = list_next(&zilog->zl_itx_list, itx); 1139789Sahrens list_remove(&zilog->zl_itx_list, itx); 11406101Sperrin zilog->zl_itx_list_sz -= itx->itx_sod; 11413063Sperrin mutex_exit(&zilog->zl_lock); 1142789Sahrens txg = itx->itx_lr.lrc_txg; 1143789Sahrens ASSERT(txg); 1144789Sahrens 1145789Sahrens if (txg > spa_last_synced_txg(spa) || 1146789Sahrens txg > spa_freeze_txg(spa)) 1147789Sahrens lwb = zil_lwb_commit(zilog, itx, lwb); 1148789Sahrens kmem_free(itx, offsetof(itx_t, itx_lr) 1149789Sahrens + itx->itx_lr.lrc_reclen); 1150789Sahrens mutex_enter(&zilog->zl_lock); 1151789Sahrens } 11522638Sperrin DTRACE_PROBE1(zil__cw2, zilog_t *, zilog); 11533063Sperrin /* determine commit sequence number */ 11543063Sperrin itx = list_head(&zilog->zl_itx_list); 11553063Sperrin if (itx) 11563063Sperrin commit_seq = itx->itx_lr.lrc_seq; 11573063Sperrin else 11583063Sperrin commit_seq = zilog->zl_itx_seq; 1159789Sahrens mutex_exit(&zilog->zl_lock); 1160789Sahrens 1161789Sahrens /* write the last block out */ 11623063Sperrin if (lwb != NULL && lwb->lwb_zio != NULL) 1163789Sahrens lwb = zil_lwb_write_start(zilog, lwb); 1164789Sahrens 11651141Sperrin zilog->zl_prev_used = zilog->zl_cur_used; 11661141Sperrin zilog->zl_cur_used = 0; 11671141Sperrin 11682638Sperrin /* 11692638Sperrin * Wait if necessary for the log blocks to be on stable storage. 11702638Sperrin */ 11712638Sperrin if (zilog->zl_root_zio) { 11722638Sperrin DTRACE_PROBE1(zil__cw3, zilog_t *, zilog); 11732638Sperrin (void) zio_wait(zilog->zl_root_zio); 11747754SJeff.Bonwick@Sun.COM zilog->zl_root_zio = NULL; 11752638Sperrin DTRACE_PROBE1(zil__cw4, zilog_t *, zilog); 11765688Sbonwick zil_flush_vdevs(zilog); 1177789Sahrens } 11781141Sperrin 1179789Sahrens if (zilog->zl_log_error || lwb == NULL) { 1180789Sahrens zilog->zl_log_error = 0; 1181789Sahrens txg_wait_synced(zilog->zl_dmu_pool, 0); 1182789Sahrens } 11833063Sperrin 11843063Sperrin mutex_enter(&zilog->zl_lock); 11851141Sperrin zilog->zl_writer = B_FALSE; 11863063Sperrin 11873063Sperrin ASSERT3U(commit_seq, >=, zilog->zl_commit_seq); 11883063Sperrin zilog->zl_commit_seq = commit_seq; 11892638Sperrin } 11902638Sperrin 11912638Sperrin /* 11922638Sperrin * Push zfs transactions to stable storage up to the supplied sequence number. 11932638Sperrin * If foid is 0 push out all transactions, otherwise push only those 11942638Sperrin * for that file or might have been used to create that file. 11952638Sperrin */ 11962638Sperrin void 11972638Sperrin zil_commit(zilog_t *zilog, uint64_t seq, uint64_t foid) 11982638Sperrin { 11992638Sperrin if (zilog == NULL || seq == 0) 12002638Sperrin return; 12012638Sperrin 12022638Sperrin mutex_enter(&zilog->zl_lock); 12032638Sperrin 12042638Sperrin seq = MIN(seq, zilog->zl_itx_seq); /* cap seq at largest itx seq */ 12052638Sperrin 12063063Sperrin while (zilog->zl_writer) { 12072638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 12083063Sperrin if (seq < zilog->zl_commit_seq) { 12093063Sperrin mutex_exit(&zilog->zl_lock); 12103063Sperrin return; 12113063Sperrin } 12123063Sperrin } 12132638Sperrin zil_commit_writer(zilog, seq, foid); /* drops zl_lock */ 12143063Sperrin /* wake up others waiting on the commit */ 12153063Sperrin cv_broadcast(&zilog->zl_cv_writer); 12163063Sperrin mutex_exit(&zilog->zl_lock); 1217789Sahrens } 1218789Sahrens 1219789Sahrens /* 1220789Sahrens * Called in syncing context to free committed log blocks and update log header. 1221789Sahrens */ 1222789Sahrens void 1223789Sahrens zil_sync(zilog_t *zilog, dmu_tx_t *tx) 1224789Sahrens { 12251807Sbonwick zil_header_t *zh = zil_header_in_syncing_context(zilog); 1226789Sahrens uint64_t txg = dmu_tx_get_txg(tx); 1227789Sahrens spa_t *spa = zilog->zl_spa; 1228789Sahrens lwb_t *lwb; 1229789Sahrens 12309396SMatthew.Ahrens@Sun.COM /* 12319396SMatthew.Ahrens@Sun.COM * We don't zero out zl_destroy_txg, so make sure we don't try 12329396SMatthew.Ahrens@Sun.COM * to destroy it twice. 12339396SMatthew.Ahrens@Sun.COM */ 12349396SMatthew.Ahrens@Sun.COM if (spa_sync_pass(spa) != 1) 12359396SMatthew.Ahrens@Sun.COM return; 12369396SMatthew.Ahrens@Sun.COM 12371807Sbonwick mutex_enter(&zilog->zl_lock); 12381807Sbonwick 1239789Sahrens ASSERT(zilog->zl_stop_sync == 0); 1240789Sahrens 12418227SNeil.Perrin@Sun.COM zh->zh_replay_seq = zilog->zl_replayed_seq[txg & TXG_MASK]; 1242789Sahrens 1243789Sahrens if (zilog->zl_destroy_txg == txg) { 12441807Sbonwick blkptr_t blk = zh->zh_log; 12451807Sbonwick 12461807Sbonwick ASSERT(list_head(&zilog->zl_lwb_list) == NULL); 12471807Sbonwick 12481807Sbonwick bzero(zh, sizeof (zil_header_t)); 12498227SNeil.Perrin@Sun.COM bzero(zilog->zl_replayed_seq, sizeof (zilog->zl_replayed_seq)); 12501807Sbonwick 12511807Sbonwick if (zilog->zl_keep_first) { 12521807Sbonwick /* 12531807Sbonwick * If this block was part of log chain that couldn't 12541807Sbonwick * be claimed because a device was missing during 12551807Sbonwick * zil_claim(), but that device later returns, 12561807Sbonwick * then this block could erroneously appear valid. 12571807Sbonwick * To guard against this, assign a new GUID to the new 12581807Sbonwick * log chain so it doesn't matter what blk points to. 12591807Sbonwick */ 12601807Sbonwick zil_init_log_chain(zilog, &blk); 12611807Sbonwick zh->zh_log = blk; 12621807Sbonwick } 1263789Sahrens } 1264789Sahrens 1265789Sahrens for (;;) { 1266789Sahrens lwb = list_head(&zilog->zl_lwb_list); 1267789Sahrens if (lwb == NULL) { 1268789Sahrens mutex_exit(&zilog->zl_lock); 1269789Sahrens return; 1270789Sahrens } 12712638Sperrin zh->zh_log = lwb->lwb_blk; 1272789Sahrens if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg) 1273789Sahrens break; 1274789Sahrens list_remove(&zilog->zl_lwb_list, lwb); 1275789Sahrens zio_free_blk(spa, &lwb->lwb_blk, txg); 1276789Sahrens kmem_cache_free(zil_lwb_cache, lwb); 12773668Sgw25295 12783668Sgw25295 /* 12793668Sgw25295 * If we don't have anything left in the lwb list then 12803668Sgw25295 * we've had an allocation failure and we need to zero 12813668Sgw25295 * out the zil_header blkptr so that we don't end 12823668Sgw25295 * up freeing the same block twice. 12833668Sgw25295 */ 12843668Sgw25295 if (list_head(&zilog->zl_lwb_list) == NULL) 12853668Sgw25295 BP_ZERO(&zh->zh_log); 1286789Sahrens } 1287789Sahrens mutex_exit(&zilog->zl_lock); 1288789Sahrens } 1289789Sahrens 1290789Sahrens void 1291789Sahrens zil_init(void) 1292789Sahrens { 1293789Sahrens zil_lwb_cache = kmem_cache_create("zil_lwb_cache", 12942856Snd150628 sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0); 1295789Sahrens } 1296789Sahrens 1297789Sahrens void 1298789Sahrens zil_fini(void) 1299789Sahrens { 1300789Sahrens kmem_cache_destroy(zil_lwb_cache); 1301789Sahrens } 1302789Sahrens 1303789Sahrens zilog_t * 1304789Sahrens zil_alloc(objset_t *os, zil_header_t *zh_phys) 1305789Sahrens { 1306789Sahrens zilog_t *zilog; 1307789Sahrens 1308789Sahrens zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP); 1309789Sahrens 1310789Sahrens zilog->zl_header = zh_phys; 1311789Sahrens zilog->zl_os = os; 1312789Sahrens zilog->zl_spa = dmu_objset_spa(os); 1313789Sahrens zilog->zl_dmu_pool = dmu_objset_pool(os); 13141807Sbonwick zilog->zl_destroy_txg = TXG_INITIAL - 1; 1315789Sahrens 13162856Snd150628 mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL); 13172856Snd150628 1318789Sahrens list_create(&zilog->zl_itx_list, sizeof (itx_t), 1319789Sahrens offsetof(itx_t, itx_node)); 1320789Sahrens 1321789Sahrens list_create(&zilog->zl_lwb_list, sizeof (lwb_t), 1322789Sahrens offsetof(lwb_t, lwb_node)); 1323789Sahrens 13245688Sbonwick mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL); 13255688Sbonwick 13265688Sbonwick avl_create(&zilog->zl_vdev_tree, zil_vdev_compare, 13275688Sbonwick sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node)); 1328789Sahrens 13295913Sperrin cv_init(&zilog->zl_cv_writer, NULL, CV_DEFAULT, NULL); 13305913Sperrin cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL); 13315913Sperrin 1332789Sahrens return (zilog); 1333789Sahrens } 1334789Sahrens 1335789Sahrens void 1336789Sahrens zil_free(zilog_t *zilog) 1337789Sahrens { 1338789Sahrens lwb_t *lwb; 1339789Sahrens 1340789Sahrens zilog->zl_stop_sync = 1; 1341789Sahrens 1342789Sahrens while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 1343789Sahrens list_remove(&zilog->zl_lwb_list, lwb); 1344789Sahrens if (lwb->lwb_buf != NULL) 1345789Sahrens zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 1346789Sahrens kmem_cache_free(zil_lwb_cache, lwb); 1347789Sahrens } 1348789Sahrens list_destroy(&zilog->zl_lwb_list); 1349789Sahrens 13505688Sbonwick avl_destroy(&zilog->zl_vdev_tree); 13515688Sbonwick mutex_destroy(&zilog->zl_vdev_lock); 1352789Sahrens 1353789Sahrens ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1354789Sahrens list_destroy(&zilog->zl_itx_list); 13552856Snd150628 mutex_destroy(&zilog->zl_lock); 1356789Sahrens 13575913Sperrin cv_destroy(&zilog->zl_cv_writer); 13585913Sperrin cv_destroy(&zilog->zl_cv_suspend); 13595913Sperrin 1360789Sahrens kmem_free(zilog, sizeof (zilog_t)); 1361789Sahrens } 1362789Sahrens 1363789Sahrens /* 1364789Sahrens * Open an intent log. 1365789Sahrens */ 1366789Sahrens zilog_t * 1367789Sahrens zil_open(objset_t *os, zil_get_data_t *get_data) 1368789Sahrens { 1369789Sahrens zilog_t *zilog = dmu_objset_zil(os); 1370789Sahrens 1371789Sahrens zilog->zl_get_data = get_data; 1372789Sahrens zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri, 1373789Sahrens 2, 2, TASKQ_PREPOPULATE); 1374789Sahrens 1375789Sahrens return (zilog); 1376789Sahrens } 1377789Sahrens 1378789Sahrens /* 1379789Sahrens * Close an intent log. 1380789Sahrens */ 1381789Sahrens void 1382789Sahrens zil_close(zilog_t *zilog) 1383789Sahrens { 13841807Sbonwick /* 13851807Sbonwick * If the log isn't already committed, mark the objset dirty 13861807Sbonwick * (so zil_sync() will be called) and wait for that txg to sync. 13871807Sbonwick */ 13881807Sbonwick if (!zil_is_committed(zilog)) { 13891807Sbonwick uint64_t txg; 13901807Sbonwick dmu_tx_t *tx = dmu_tx_create(zilog->zl_os); 13911807Sbonwick (void) dmu_tx_assign(tx, TXG_WAIT); 13921807Sbonwick dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 13931807Sbonwick txg = dmu_tx_get_txg(tx); 13941807Sbonwick dmu_tx_commit(tx); 13951807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, txg); 13961807Sbonwick } 13971807Sbonwick 1398789Sahrens taskq_destroy(zilog->zl_clean_taskq); 1399789Sahrens zilog->zl_clean_taskq = NULL; 1400789Sahrens zilog->zl_get_data = NULL; 1401789Sahrens 1402789Sahrens zil_itx_clean(zilog); 1403789Sahrens ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1404789Sahrens } 1405789Sahrens 1406789Sahrens /* 1407789Sahrens * Suspend an intent log. While in suspended mode, we still honor 1408789Sahrens * synchronous semantics, but we rely on txg_wait_synced() to do it. 1409789Sahrens * We suspend the log briefly when taking a snapshot so that the snapshot 1410789Sahrens * contains all the data it's supposed to, and has an empty intent log. 1411789Sahrens */ 1412789Sahrens int 1413789Sahrens zil_suspend(zilog_t *zilog) 1414789Sahrens { 14151807Sbonwick const zil_header_t *zh = zilog->zl_header; 1416789Sahrens 1417789Sahrens mutex_enter(&zilog->zl_lock); 14188989SNeil.Perrin@Sun.COM if (zh->zh_flags & ZIL_REPLAY_NEEDED) { /* unplayed log */ 1419789Sahrens mutex_exit(&zilog->zl_lock); 1420789Sahrens return (EBUSY); 1421789Sahrens } 14221807Sbonwick if (zilog->zl_suspend++ != 0) { 14231807Sbonwick /* 14241807Sbonwick * Someone else already began a suspend. 14251807Sbonwick * Just wait for them to finish. 14261807Sbonwick */ 14271807Sbonwick while (zilog->zl_suspending) 14281807Sbonwick cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock); 14291807Sbonwick mutex_exit(&zilog->zl_lock); 14301807Sbonwick return (0); 14311807Sbonwick } 14321807Sbonwick zilog->zl_suspending = B_TRUE; 1433789Sahrens mutex_exit(&zilog->zl_lock); 1434789Sahrens 14352638Sperrin zil_commit(zilog, UINT64_MAX, 0); 1436789Sahrens 14372638Sperrin /* 14382638Sperrin * Wait for any in-flight log writes to complete. 14392638Sperrin */ 1440789Sahrens mutex_enter(&zilog->zl_lock); 14412638Sperrin while (zilog->zl_writer) 14422638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 1443789Sahrens mutex_exit(&zilog->zl_lock); 1444789Sahrens 14451807Sbonwick zil_destroy(zilog, B_FALSE); 14461807Sbonwick 14471807Sbonwick mutex_enter(&zilog->zl_lock); 14481807Sbonwick zilog->zl_suspending = B_FALSE; 14491807Sbonwick cv_broadcast(&zilog->zl_cv_suspend); 14501807Sbonwick mutex_exit(&zilog->zl_lock); 1451789Sahrens 1452789Sahrens return (0); 1453789Sahrens } 1454789Sahrens 1455789Sahrens void 1456789Sahrens zil_resume(zilog_t *zilog) 1457789Sahrens { 1458789Sahrens mutex_enter(&zilog->zl_lock); 1459789Sahrens ASSERT(zilog->zl_suspend != 0); 1460789Sahrens zilog->zl_suspend--; 1461789Sahrens mutex_exit(&zilog->zl_lock); 1462789Sahrens } 1463789Sahrens 1464789Sahrens typedef struct zil_replay_arg { 1465789Sahrens objset_t *zr_os; 1466789Sahrens zil_replay_func_t **zr_replay; 1467789Sahrens void *zr_arg; 1468789Sahrens boolean_t zr_byteswap; 1469789Sahrens char *zr_lrbuf; 1470789Sahrens } zil_replay_arg_t; 1471789Sahrens 1472789Sahrens static void 1473789Sahrens zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg) 1474789Sahrens { 1475789Sahrens zil_replay_arg_t *zr = zra; 14761807Sbonwick const zil_header_t *zh = zilog->zl_header; 1477789Sahrens uint64_t reclen = lr->lrc_reclen; 1478789Sahrens uint64_t txtype = lr->lrc_txtype; 14793063Sperrin char *name; 14808227SNeil.Perrin@Sun.COM int pass, error; 1481789Sahrens 14828227SNeil.Perrin@Sun.COM if (!zilog->zl_replay) /* giving up */ 1483789Sahrens return; 1484789Sahrens 1485789Sahrens if (lr->lrc_txg < claim_txg) /* already committed */ 1486789Sahrens return; 1487789Sahrens 1488789Sahrens if (lr->lrc_seq <= zh->zh_replay_seq) /* already replayed */ 1489789Sahrens return; 1490789Sahrens 14915331Samw /* Strip case-insensitive bit, still present in log record */ 14925331Samw txtype &= ~TX_CI; 14935331Samw 14948227SNeil.Perrin@Sun.COM if (txtype == 0 || txtype >= TX_MAX_TYPE) { 14958227SNeil.Perrin@Sun.COM error = EINVAL; 14968227SNeil.Perrin@Sun.COM goto bad; 14978227SNeil.Perrin@Sun.COM } 14988227SNeil.Perrin@Sun.COM 1499789Sahrens /* 1500789Sahrens * Make a copy of the data so we can revise and extend it. 1501789Sahrens */ 1502789Sahrens bcopy(lr, zr->zr_lrbuf, reclen); 1503789Sahrens 1504789Sahrens /* 1505789Sahrens * The log block containing this lr may have been byteswapped 1506789Sahrens * so that we can easily examine common fields like lrc_txtype. 1507789Sahrens * However, the log is a mix of different data types, and only the 1508789Sahrens * replay vectors know how to byteswap their records. Therefore, if 1509789Sahrens * the lr was byteswapped, undo it before invoking the replay vector. 1510789Sahrens */ 1511789Sahrens if (zr->zr_byteswap) 1512789Sahrens byteswap_uint64_array(zr->zr_lrbuf, reclen); 1513789Sahrens 1514789Sahrens /* 1515789Sahrens * If this is a TX_WRITE with a blkptr, suck in the data. 1516789Sahrens */ 1517789Sahrens if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) { 1518789Sahrens lr_write_t *lrw = (lr_write_t *)lr; 1519789Sahrens blkptr_t *wbp = &lrw->lr_blkptr; 1520789Sahrens uint64_t wlen = lrw->lr_length; 1521789Sahrens char *wbuf = zr->zr_lrbuf + reclen; 1522789Sahrens 1523789Sahrens if (BP_IS_HOLE(wbp)) { /* compressed to a hole */ 1524789Sahrens bzero(wbuf, wlen); 1525789Sahrens } else { 1526789Sahrens /* 1527789Sahrens * A subsequent write may have overwritten this block, 1528789Sahrens * in which case wbp may have been been freed and 1529789Sahrens * reallocated, and our read of wbp may fail with a 1530789Sahrens * checksum error. We can safely ignore this because 1531789Sahrens * the later write will provide the correct data. 1532789Sahrens */ 15331544Seschrock zbookmark_t zb; 15341544Seschrock 15351544Seschrock zb.zb_objset = dmu_objset_id(zilog->zl_os); 15361544Seschrock zb.zb_object = lrw->lr_foid; 15371544Seschrock zb.zb_level = -1; 15381544Seschrock zb.zb_blkid = lrw->lr_offset / BP_GET_LSIZE(wbp); 15391544Seschrock 1540789Sahrens (void) zio_wait(zio_read(NULL, zilog->zl_spa, 1541789Sahrens wbp, wbuf, BP_GET_LSIZE(wbp), NULL, NULL, 1542789Sahrens ZIO_PRIORITY_SYNC_READ, 15431544Seschrock ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb)); 1544789Sahrens (void) memmove(wbuf, wbuf + lrw->lr_blkoff, wlen); 1545789Sahrens } 1546789Sahrens } 1547789Sahrens 1548789Sahrens /* 1549789Sahrens * We must now do two things atomically: replay this log record, 15508227SNeil.Perrin@Sun.COM * and update the log header sequence number to reflect the fact that 15518227SNeil.Perrin@Sun.COM * we did so. At the end of each replay function the sequence number 15528227SNeil.Perrin@Sun.COM * is updated if we are in replay mode. 1553789Sahrens */ 15548227SNeil.Perrin@Sun.COM for (pass = 1; pass <= 2; pass++) { 15558227SNeil.Perrin@Sun.COM zilog->zl_replaying_seq = lr->lrc_seq; 15568227SNeil.Perrin@Sun.COM /* Only byteswap (if needed) on the 1st pass. */ 15578227SNeil.Perrin@Sun.COM error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lrbuf, 15588227SNeil.Perrin@Sun.COM zr->zr_byteswap && pass == 1); 1559789Sahrens 15603063Sperrin if (!error) 15613063Sperrin return; 15623063Sperrin 15633063Sperrin /* 15643063Sperrin * The DMU's dnode layer doesn't see removes until the txg 15653063Sperrin * commits, so a subsequent claim can spuriously fail with 15668227SNeil.Perrin@Sun.COM * EEXIST. So if we receive any error we try syncing out 15678227SNeil.Perrin@Sun.COM * any removes then retry the transaction. 15683063Sperrin */ 15698227SNeil.Perrin@Sun.COM if (pass == 1) 15703063Sperrin txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0); 1571789Sahrens } 1572789Sahrens 15737904SNeil.Perrin@Sun.COM bad: 15748227SNeil.Perrin@Sun.COM ASSERT(error); 15753063Sperrin name = kmem_alloc(MAXNAMELEN, KM_SLEEP); 15763063Sperrin dmu_objset_name(zr->zr_os, name); 15773063Sperrin cmn_err(CE_WARN, "ZFS replay transaction error %d, " 15785331Samw "dataset %s, seq 0x%llx, txtype %llu %s\n", 15795331Samw error, name, (u_longlong_t)lr->lrc_seq, (u_longlong_t)txtype, 15805331Samw (lr->lrc_txtype & TX_CI) ? "CI" : ""); 15818227SNeil.Perrin@Sun.COM zilog->zl_replay = B_FALSE; 15823063Sperrin kmem_free(name, MAXNAMELEN); 15833063Sperrin } 1584789Sahrens 15853063Sperrin /* ARGSUSED */ 15863063Sperrin static void 15873063Sperrin zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg) 15883063Sperrin { 15893063Sperrin zilog->zl_replay_blks++; 1590789Sahrens } 1591789Sahrens 1592789Sahrens /* 15931362Sperrin * If this dataset has a non-empty intent log, replay it and destroy it. 1594789Sahrens */ 1595789Sahrens void 15968227SNeil.Perrin@Sun.COM zil_replay(objset_t *os, void *arg, zil_replay_func_t *replay_func[TX_MAX_TYPE]) 1597789Sahrens { 1598789Sahrens zilog_t *zilog = dmu_objset_zil(os); 15991807Sbonwick const zil_header_t *zh = zilog->zl_header; 16001807Sbonwick zil_replay_arg_t zr; 16011362Sperrin 16028989SNeil.Perrin@Sun.COM if ((zh->zh_flags & ZIL_REPLAY_NEEDED) == 0) { 16031807Sbonwick zil_destroy(zilog, B_TRUE); 16041362Sperrin return; 16051362Sperrin } 1606789Sahrens 1607789Sahrens zr.zr_os = os; 1608789Sahrens zr.zr_replay = replay_func; 1609789Sahrens zr.zr_arg = arg; 16101807Sbonwick zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log); 1611789Sahrens zr.zr_lrbuf = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP); 1612789Sahrens 1613789Sahrens /* 1614789Sahrens * Wait for in-progress removes to sync before starting replay. 1615789Sahrens */ 1616789Sahrens txg_wait_synced(zilog->zl_dmu_pool, 0); 1617789Sahrens 16188227SNeil.Perrin@Sun.COM zilog->zl_replay = B_TRUE; 16193063Sperrin zilog->zl_replay_time = lbolt; 16203063Sperrin ASSERT(zilog->zl_replay_blks == 0); 16213063Sperrin (void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr, 16221807Sbonwick zh->zh_claim_txg); 1623789Sahrens kmem_free(zr.zr_lrbuf, 2 * SPA_MAXBLOCKSIZE); 1624789Sahrens 16251807Sbonwick zil_destroy(zilog, B_FALSE); 16265712Sahrens txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 16278227SNeil.Perrin@Sun.COM zilog->zl_replay = B_FALSE; 1628789Sahrens } 16291646Sperrin 16301646Sperrin /* 16311646Sperrin * Report whether all transactions are committed 16321646Sperrin */ 16331646Sperrin int 16341646Sperrin zil_is_committed(zilog_t *zilog) 16351646Sperrin { 16361646Sperrin lwb_t *lwb; 16372638Sperrin int ret; 16381646Sperrin 16392638Sperrin mutex_enter(&zilog->zl_lock); 16402638Sperrin while (zilog->zl_writer) 16412638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 16422638Sperrin 16432638Sperrin /* recent unpushed intent log transactions? */ 16442638Sperrin if (!list_is_empty(&zilog->zl_itx_list)) { 16452638Sperrin ret = B_FALSE; 16462638Sperrin goto out; 16472638Sperrin } 16482638Sperrin 16492638Sperrin /* intent log never used? */ 16502638Sperrin lwb = list_head(&zilog->zl_lwb_list); 16512638Sperrin if (lwb == NULL) { 16522638Sperrin ret = B_TRUE; 16532638Sperrin goto out; 16542638Sperrin } 16551646Sperrin 16561646Sperrin /* 16572638Sperrin * more than 1 log buffer means zil_sync() hasn't yet freed 16582638Sperrin * entries after a txg has committed 16591646Sperrin */ 16602638Sperrin if (list_next(&zilog->zl_lwb_list, lwb)) { 16612638Sperrin ret = B_FALSE; 16622638Sperrin goto out; 16632638Sperrin } 16642638Sperrin 16651646Sperrin ASSERT(zil_empty(zilog)); 16662638Sperrin ret = B_TRUE; 16672638Sperrin out: 16682638Sperrin cv_broadcast(&zilog->zl_cv_writer); 16692638Sperrin mutex_exit(&zilog->zl_lock); 16702638Sperrin return (ret); 16711646Sperrin } 1672