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 /* 725789Sahrens * Now that we've written this log block, we have a stable pointer 726789Sahrens * to the next block in the chain, so it's OK to let the txg in 727789Sahrens * which we allocated the next block sync. 728789Sahrens */ 729789Sahrens txg_rele_to_sync(&lwb->lwb_txgh); 730789Sahrens 731789Sahrens zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 732789Sahrens mutex_enter(&zilog->zl_lock); 733789Sahrens lwb->lwb_buf = NULL; 7344527Sperrin if (zio->io_error) 735789Sahrens zilog->zl_log_error = B_TRUE; 736789Sahrens mutex_exit(&zilog->zl_lock); 737789Sahrens } 738789Sahrens 739789Sahrens /* 7402237Smaybee * Initialize the io for a log block. 7412237Smaybee */ 7422237Smaybee static void 7432237Smaybee zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb) 7442237Smaybee { 7452237Smaybee zbookmark_t zb; 7462237Smaybee 7472237Smaybee zb.zb_objset = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET]; 7482237Smaybee zb.zb_object = 0; 7492237Smaybee zb.zb_level = -1; 7502237Smaybee zb.zb_blkid = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 7512237Smaybee 7522638Sperrin if (zilog->zl_root_zio == NULL) { 7532638Sperrin zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL, 7542638Sperrin ZIO_FLAG_CANFAIL); 7552638Sperrin } 7563063Sperrin if (lwb->lwb_zio == NULL) { 7573063Sperrin lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa, 7587754SJeff.Bonwick@Sun.COM 0, &lwb->lwb_blk, lwb->lwb_buf, 7593063Sperrin lwb->lwb_sz, zil_lwb_write_done, lwb, 7604527Sperrin ZIO_PRIORITY_LOG_WRITE, ZIO_FLAG_CANFAIL, &zb); 7613063Sperrin } 7622237Smaybee } 7632237Smaybee 7642237Smaybee /* 765789Sahrens * Start a log block write and advance to the next log block. 766789Sahrens * Calls are serialized. 767789Sahrens */ 768789Sahrens static lwb_t * 769789Sahrens zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb) 770789Sahrens { 771789Sahrens lwb_t *nlwb; 772789Sahrens zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1; 7731807Sbonwick spa_t *spa = zilog->zl_spa; 7741807Sbonwick blkptr_t *bp = &ztp->zit_next_blk; 775789Sahrens uint64_t txg; 776789Sahrens uint64_t zil_blksz; 777789Sahrens int error; 778789Sahrens 779789Sahrens ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb)); 780789Sahrens 781789Sahrens /* 782789Sahrens * Allocate the next block and save its address in this block 783789Sahrens * before writing it in order to establish the log chain. 784789Sahrens * Note that if the allocation of nlwb synced before we wrote 785789Sahrens * the block that points at it (lwb), we'd leak it if we crashed. 786789Sahrens * Therefore, we don't do txg_rele_to_sync() until zil_lwb_write_done(). 787789Sahrens */ 788789Sahrens txg = txg_hold_open(zilog->zl_dmu_pool, &lwb->lwb_txgh); 789789Sahrens txg_rele_to_quiesce(&lwb->lwb_txgh); 790789Sahrens 791789Sahrens /* 7921141Sperrin * Pick a ZIL blocksize. We request a size that is the 7931141Sperrin * maximum of the previous used size, the current used size and 7941141Sperrin * the amount waiting in the queue. 795789Sahrens */ 7962237Smaybee zil_blksz = MAX(zilog->zl_prev_used, 7972237Smaybee zilog->zl_cur_used + sizeof (*ztp)); 7981141Sperrin zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp)); 7991842Sperrin zil_blksz = P2ROUNDUP_TYPED(zil_blksz, ZIL_MIN_BLKSZ, uint64_t); 8001141Sperrin if (zil_blksz > ZIL_MAX_BLKSZ) 8011141Sperrin zil_blksz = ZIL_MAX_BLKSZ; 802789Sahrens 8033063Sperrin BP_ZERO(bp); 8043063Sperrin /* pass the old blkptr in order to spread log blocks across devs */ 8053063Sperrin error = zio_alloc_blk(spa, zil_blksz, bp, &lwb->lwb_blk, txg); 806789Sahrens if (error) { 8073668Sgw25295 dmu_tx_t *tx = dmu_tx_create_assigned(zilog->zl_dmu_pool, txg); 8083668Sgw25295 8091544Seschrock /* 8103668Sgw25295 * We dirty the dataset to ensure that zil_sync() will 8113668Sgw25295 * be called to remove this lwb from our zl_lwb_list. 8123668Sgw25295 * Failing to do so, may leave an lwb with a NULL lwb_buf 8133668Sgw25295 * hanging around on the zl_lwb_list. 8143668Sgw25295 */ 8153668Sgw25295 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 8163848Sgw25295 dmu_tx_commit(tx); 8173668Sgw25295 8183668Sgw25295 /* 8193668Sgw25295 * Since we've just experienced an allocation failure so we 8203668Sgw25295 * terminate the current lwb and send it on its way. 8213668Sgw25295 */ 8223668Sgw25295 ztp->zit_pad = 0; 8233668Sgw25295 ztp->zit_nused = lwb->lwb_nused; 8243668Sgw25295 ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum; 8253668Sgw25295 zio_nowait(lwb->lwb_zio); 8263668Sgw25295 8273668Sgw25295 /* 8281544Seschrock * By returning NULL the caller will call tx_wait_synced() 8291544Seschrock */ 830789Sahrens return (NULL); 831789Sahrens } 832789Sahrens 8331807Sbonwick ASSERT3U(bp->blk_birth, ==, txg); 8341544Seschrock ztp->zit_pad = 0; 835789Sahrens ztp->zit_nused = lwb->lwb_nused; 836789Sahrens ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum; 8371807Sbonwick bp->blk_cksum = lwb->lwb_blk.blk_cksum; 8381807Sbonwick bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++; 839789Sahrens 840789Sahrens /* 841789Sahrens * Allocate a new log write buffer (lwb). 842789Sahrens */ 843789Sahrens nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 844789Sahrens 845789Sahrens nlwb->lwb_zilog = zilog; 8461807Sbonwick nlwb->lwb_blk = *bp; 847789Sahrens nlwb->lwb_nused = 0; 848789Sahrens nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk); 849789Sahrens nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz); 850789Sahrens nlwb->lwb_max_txg = txg; 8512237Smaybee nlwb->lwb_zio = NULL; 852789Sahrens 853789Sahrens /* 8543063Sperrin * Put new lwb at the end of the log chain 855789Sahrens */ 856789Sahrens mutex_enter(&zilog->zl_lock); 857789Sahrens list_insert_tail(&zilog->zl_lwb_list, nlwb); 8583063Sperrin mutex_exit(&zilog->zl_lock); 8593063Sperrin 8605688Sbonwick /* Record the block for later vdev flushing */ 8615688Sbonwick zil_add_block(zilog, &lwb->lwb_blk); 862789Sahrens 863789Sahrens /* 8642237Smaybee * kick off the write for the old log block 865789Sahrens */ 8662237Smaybee dprintf_bp(&lwb->lwb_blk, "lwb %p txg %llu: ", lwb, txg); 8673063Sperrin ASSERT(lwb->lwb_zio); 8682237Smaybee zio_nowait(lwb->lwb_zio); 869789Sahrens 870789Sahrens return (nlwb); 871789Sahrens } 872789Sahrens 873789Sahrens static lwb_t * 874789Sahrens zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb) 875789Sahrens { 876789Sahrens lr_t *lrc = &itx->itx_lr; /* common log record */ 8772237Smaybee lr_write_t *lr = (lr_write_t *)lrc; 878789Sahrens uint64_t txg = lrc->lrc_txg; 879789Sahrens uint64_t reclen = lrc->lrc_reclen; 8802237Smaybee uint64_t dlen; 881789Sahrens 882789Sahrens if (lwb == NULL) 883789Sahrens return (NULL); 884789Sahrens ASSERT(lwb->lwb_buf != NULL); 885789Sahrens 8862237Smaybee if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY) 8872237Smaybee dlen = P2ROUNDUP_TYPED( 8882237Smaybee lr->lr_length, sizeof (uint64_t), uint64_t); 8892237Smaybee else 8902237Smaybee dlen = 0; 8911669Sperrin 8921669Sperrin zilog->zl_cur_used += (reclen + dlen); 8931669Sperrin 8943063Sperrin zil_lwb_write_init(zilog, lwb); 8953063Sperrin 8961669Sperrin /* 8971669Sperrin * If this record won't fit in the current log block, start a new one. 8981669Sperrin */ 8991669Sperrin if (lwb->lwb_nused + reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) { 9001669Sperrin lwb = zil_lwb_write_start(zilog, lwb); 9012237Smaybee if (lwb == NULL) 9021669Sperrin return (NULL); 9033063Sperrin zil_lwb_write_init(zilog, lwb); 9041669Sperrin ASSERT(lwb->lwb_nused == 0); 9051669Sperrin if (reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) { 9061669Sperrin txg_wait_synced(zilog->zl_dmu_pool, txg); 907789Sahrens return (lwb); 908789Sahrens } 909789Sahrens } 910789Sahrens 9112638Sperrin /* 9122638Sperrin * Update the lrc_seq, to be log record sequence number. See zil.h 9132638Sperrin * Then copy the record to the log buffer. 9142638Sperrin */ 9152638Sperrin lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */ 916789Sahrens bcopy(lrc, lwb->lwb_buf + lwb->lwb_nused, reclen); 9172237Smaybee 9182237Smaybee /* 9192237Smaybee * If it's a write, fetch the data or get its blkptr as appropriate. 9202237Smaybee */ 9212237Smaybee if (lrc->lrc_txtype == TX_WRITE) { 9222237Smaybee if (txg > spa_freeze_txg(zilog->zl_spa)) 9232237Smaybee txg_wait_synced(zilog->zl_dmu_pool, txg); 9242237Smaybee if (itx->itx_wr_state != WR_COPIED) { 9252237Smaybee char *dbuf; 9262237Smaybee int error; 9272237Smaybee 9282237Smaybee /* alignment is guaranteed */ 9292237Smaybee lr = (lr_write_t *)(lwb->lwb_buf + lwb->lwb_nused); 9302237Smaybee if (dlen) { 9312237Smaybee ASSERT(itx->itx_wr_state == WR_NEED_COPY); 9322237Smaybee dbuf = lwb->lwb_buf + lwb->lwb_nused + reclen; 9332237Smaybee lr->lr_common.lrc_reclen += dlen; 9342237Smaybee } else { 9352237Smaybee ASSERT(itx->itx_wr_state == WR_INDIRECT); 9362237Smaybee dbuf = NULL; 9372237Smaybee } 9382237Smaybee error = zilog->zl_get_data( 9392237Smaybee itx->itx_private, lr, dbuf, lwb->lwb_zio); 9402237Smaybee if (error) { 9412237Smaybee ASSERT(error == ENOENT || error == EEXIST || 9422237Smaybee error == EALREADY); 9432237Smaybee return (lwb); 9442237Smaybee } 9452237Smaybee } 9461669Sperrin } 9472237Smaybee 9482237Smaybee lwb->lwb_nused += reclen + dlen; 949789Sahrens lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg); 950789Sahrens ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb)); 951789Sahrens ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0); 952789Sahrens 953789Sahrens return (lwb); 954789Sahrens } 955789Sahrens 956789Sahrens itx_t * 9575331Samw zil_itx_create(uint64_t txtype, size_t lrsize) 958789Sahrens { 959789Sahrens itx_t *itx; 960789Sahrens 9611842Sperrin lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t); 962789Sahrens 963789Sahrens itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP); 964789Sahrens itx->itx_lr.lrc_txtype = txtype; 965789Sahrens itx->itx_lr.lrc_reclen = lrsize; 9666101Sperrin itx->itx_sod = lrsize; /* if write & WR_NEED_COPY will be increased */ 967789Sahrens itx->itx_lr.lrc_seq = 0; /* defensive */ 968789Sahrens 969789Sahrens return (itx); 970789Sahrens } 971789Sahrens 972789Sahrens uint64_t 973789Sahrens zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx) 974789Sahrens { 975789Sahrens uint64_t seq; 976789Sahrens 977789Sahrens ASSERT(itx->itx_lr.lrc_seq == 0); 978789Sahrens 979789Sahrens mutex_enter(&zilog->zl_lock); 980789Sahrens list_insert_tail(&zilog->zl_itx_list, itx); 9816101Sperrin zilog->zl_itx_list_sz += itx->itx_sod; 982789Sahrens itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx); 983789Sahrens itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq; 984789Sahrens mutex_exit(&zilog->zl_lock); 985789Sahrens 986789Sahrens return (seq); 987789Sahrens } 988789Sahrens 989789Sahrens /* 990789Sahrens * Free up all in-memory intent log transactions that have now been synced. 991789Sahrens */ 992789Sahrens static void 993789Sahrens zil_itx_clean(zilog_t *zilog) 994789Sahrens { 995789Sahrens uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa); 996789Sahrens uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa); 9973778Sjohansen list_t clean_list; 998789Sahrens itx_t *itx; 999789Sahrens 10003778Sjohansen list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node)); 10013778Sjohansen 1002789Sahrens mutex_enter(&zilog->zl_lock); 10032638Sperrin /* wait for a log writer to finish walking list */ 10042638Sperrin while (zilog->zl_writer) { 10052638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 10062638Sperrin } 10073778Sjohansen 10083778Sjohansen /* 10093778Sjohansen * Move the sync'd log transactions to a separate list so we can call 10103778Sjohansen * kmem_free without holding the zl_lock. 10113778Sjohansen * 10123778Sjohansen * There is no need to set zl_writer as we don't drop zl_lock here 10133778Sjohansen */ 1014789Sahrens while ((itx = list_head(&zilog->zl_itx_list)) != NULL && 1015789Sahrens itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) { 1016789Sahrens list_remove(&zilog->zl_itx_list, itx); 10176101Sperrin zilog->zl_itx_list_sz -= itx->itx_sod; 10183778Sjohansen list_insert_tail(&clean_list, itx); 10193778Sjohansen } 10203778Sjohansen cv_broadcast(&zilog->zl_cv_writer); 10213778Sjohansen mutex_exit(&zilog->zl_lock); 10223778Sjohansen 10233778Sjohansen /* destroy sync'd log transactions */ 10243778Sjohansen while ((itx = list_head(&clean_list)) != NULL) { 10253778Sjohansen list_remove(&clean_list, itx); 1026789Sahrens kmem_free(itx, offsetof(itx_t, itx_lr) 1027789Sahrens + itx->itx_lr.lrc_reclen); 1028789Sahrens } 10293778Sjohansen list_destroy(&clean_list); 1030789Sahrens } 1031789Sahrens 10322638Sperrin /* 10333063Sperrin * If there are any in-memory intent log transactions which have now been 10343063Sperrin * synced then start up a taskq to free them. 10352638Sperrin */ 1036789Sahrens void 1037789Sahrens zil_clean(zilog_t *zilog) 1038789Sahrens { 10393063Sperrin itx_t *itx; 10403063Sperrin 1041789Sahrens mutex_enter(&zilog->zl_lock); 10423063Sperrin itx = list_head(&zilog->zl_itx_list); 10433063Sperrin if ((itx != NULL) && 10443063Sperrin (itx->itx_lr.lrc_txg <= spa_last_synced_txg(zilog->zl_spa))) { 1045789Sahrens (void) taskq_dispatch(zilog->zl_clean_taskq, 10469321SNeil.Perrin@Sun.COM (task_func_t *)zil_itx_clean, zilog, TQ_SLEEP); 10473063Sperrin } 1048789Sahrens mutex_exit(&zilog->zl_lock); 1049789Sahrens } 1050789Sahrens 10517754SJeff.Bonwick@Sun.COM static void 10522638Sperrin zil_commit_writer(zilog_t *zilog, uint64_t seq, uint64_t foid) 1053789Sahrens { 1054789Sahrens uint64_t txg; 10553063Sperrin uint64_t commit_seq = 0; 10562638Sperrin itx_t *itx, *itx_next = (itx_t *)-1; 1057789Sahrens lwb_t *lwb; 1058789Sahrens spa_t *spa; 1059789Sahrens 10602638Sperrin zilog->zl_writer = B_TRUE; 10617754SJeff.Bonwick@Sun.COM ASSERT(zilog->zl_root_zio == NULL); 1062789Sahrens spa = zilog->zl_spa; 1063789Sahrens 1064789Sahrens if (zilog->zl_suspend) { 1065789Sahrens lwb = NULL; 1066789Sahrens } else { 1067789Sahrens lwb = list_tail(&zilog->zl_lwb_list); 1068789Sahrens if (lwb == NULL) { 10692638Sperrin /* 10702638Sperrin * Return if there's nothing to flush before we 10712638Sperrin * dirty the fs by calling zil_create() 10722638Sperrin */ 10732638Sperrin if (list_is_empty(&zilog->zl_itx_list)) { 10742638Sperrin zilog->zl_writer = B_FALSE; 10752638Sperrin return; 10762638Sperrin } 1077789Sahrens mutex_exit(&zilog->zl_lock); 1078789Sahrens zil_create(zilog); 1079789Sahrens mutex_enter(&zilog->zl_lock); 1080789Sahrens lwb = list_tail(&zilog->zl_lwb_list); 1081789Sahrens } 1082789Sahrens } 1083789Sahrens 10843063Sperrin /* Loop through in-memory log transactions filling log blocks. */ 10852638Sperrin DTRACE_PROBE1(zil__cw1, zilog_t *, zilog); 1086789Sahrens for (;;) { 10872638Sperrin /* 10882638Sperrin * Find the next itx to push: 10892638Sperrin * Push all transactions related to specified foid and all 10902638Sperrin * other transactions except TX_WRITE, TX_TRUNCATE, 10912638Sperrin * TX_SETATTR and TX_ACL for all other files. 10922638Sperrin */ 10932638Sperrin if (itx_next != (itx_t *)-1) 10942638Sperrin itx = itx_next; 10952638Sperrin else 10962638Sperrin itx = list_head(&zilog->zl_itx_list); 10972638Sperrin for (; itx != NULL; itx = list_next(&zilog->zl_itx_list, itx)) { 10982638Sperrin if (foid == 0) /* push all foids? */ 10992638Sperrin break; 11003063Sperrin if (itx->itx_sync) /* push all O_[D]SYNC */ 11013063Sperrin break; 11022638Sperrin switch (itx->itx_lr.lrc_txtype) { 11032638Sperrin case TX_SETATTR: 11042638Sperrin case TX_WRITE: 11052638Sperrin case TX_TRUNCATE: 11062638Sperrin case TX_ACL: 11072638Sperrin /* lr_foid is same offset for these records */ 11082638Sperrin if (((lr_write_t *)&itx->itx_lr)->lr_foid 11092638Sperrin != foid) { 11102638Sperrin continue; /* skip this record */ 11112638Sperrin } 11122638Sperrin } 11132638Sperrin break; 11142638Sperrin } 1115789Sahrens if (itx == NULL) 1116789Sahrens break; 1117789Sahrens 1118789Sahrens if ((itx->itx_lr.lrc_seq > seq) && 11192638Sperrin ((lwb == NULL) || (lwb->lwb_nused == 0) || 11206101Sperrin (lwb->lwb_nused + itx->itx_sod > ZIL_BLK_DATA_SZ(lwb)))) { 1121789Sahrens break; 11223063Sperrin } 1123789Sahrens 11242638Sperrin /* 11252638Sperrin * Save the next pointer. Even though we soon drop 11262638Sperrin * zl_lock all threads that may change the list 11272638Sperrin * (another writer or zil_itx_clean) can't do so until 11282638Sperrin * they have zl_writer. 11292638Sperrin */ 11302638Sperrin itx_next = list_next(&zilog->zl_itx_list, itx); 1131789Sahrens list_remove(&zilog->zl_itx_list, itx); 11326101Sperrin zilog->zl_itx_list_sz -= itx->itx_sod; 11333063Sperrin mutex_exit(&zilog->zl_lock); 1134789Sahrens txg = itx->itx_lr.lrc_txg; 1135789Sahrens ASSERT(txg); 1136789Sahrens 1137789Sahrens if (txg > spa_last_synced_txg(spa) || 1138789Sahrens txg > spa_freeze_txg(spa)) 1139789Sahrens lwb = zil_lwb_commit(zilog, itx, lwb); 1140789Sahrens kmem_free(itx, offsetof(itx_t, itx_lr) 1141789Sahrens + itx->itx_lr.lrc_reclen); 1142789Sahrens mutex_enter(&zilog->zl_lock); 1143789Sahrens } 11442638Sperrin DTRACE_PROBE1(zil__cw2, zilog_t *, zilog); 11453063Sperrin /* determine commit sequence number */ 11463063Sperrin itx = list_head(&zilog->zl_itx_list); 11473063Sperrin if (itx) 11483063Sperrin commit_seq = itx->itx_lr.lrc_seq; 11493063Sperrin else 11503063Sperrin commit_seq = zilog->zl_itx_seq; 1151789Sahrens mutex_exit(&zilog->zl_lock); 1152789Sahrens 1153789Sahrens /* write the last block out */ 11543063Sperrin if (lwb != NULL && lwb->lwb_zio != NULL) 1155789Sahrens lwb = zil_lwb_write_start(zilog, lwb); 1156789Sahrens 11571141Sperrin zilog->zl_prev_used = zilog->zl_cur_used; 11581141Sperrin zilog->zl_cur_used = 0; 11591141Sperrin 11602638Sperrin /* 11612638Sperrin * Wait if necessary for the log blocks to be on stable storage. 11622638Sperrin */ 11632638Sperrin if (zilog->zl_root_zio) { 11642638Sperrin DTRACE_PROBE1(zil__cw3, zilog_t *, zilog); 11652638Sperrin (void) zio_wait(zilog->zl_root_zio); 11667754SJeff.Bonwick@Sun.COM zilog->zl_root_zio = NULL; 11672638Sperrin DTRACE_PROBE1(zil__cw4, zilog_t *, zilog); 11685688Sbonwick zil_flush_vdevs(zilog); 1169789Sahrens } 11701141Sperrin 1171789Sahrens if (zilog->zl_log_error || lwb == NULL) { 1172789Sahrens zilog->zl_log_error = 0; 1173789Sahrens txg_wait_synced(zilog->zl_dmu_pool, 0); 1174789Sahrens } 11753063Sperrin 11763063Sperrin mutex_enter(&zilog->zl_lock); 11771141Sperrin zilog->zl_writer = B_FALSE; 11783063Sperrin 11793063Sperrin ASSERT3U(commit_seq, >=, zilog->zl_commit_seq); 11803063Sperrin zilog->zl_commit_seq = commit_seq; 11812638Sperrin } 11822638Sperrin 11832638Sperrin /* 11842638Sperrin * Push zfs transactions to stable storage up to the supplied sequence number. 11852638Sperrin * If foid is 0 push out all transactions, otherwise push only those 11862638Sperrin * for that file or might have been used to create that file. 11872638Sperrin */ 11882638Sperrin void 11892638Sperrin zil_commit(zilog_t *zilog, uint64_t seq, uint64_t foid) 11902638Sperrin { 11912638Sperrin if (zilog == NULL || seq == 0) 11922638Sperrin return; 11932638Sperrin 11942638Sperrin mutex_enter(&zilog->zl_lock); 11952638Sperrin 11962638Sperrin seq = MIN(seq, zilog->zl_itx_seq); /* cap seq at largest itx seq */ 11972638Sperrin 11983063Sperrin while (zilog->zl_writer) { 11992638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 12003063Sperrin if (seq < zilog->zl_commit_seq) { 12013063Sperrin mutex_exit(&zilog->zl_lock); 12023063Sperrin return; 12033063Sperrin } 12043063Sperrin } 12052638Sperrin zil_commit_writer(zilog, seq, foid); /* drops zl_lock */ 12063063Sperrin /* wake up others waiting on the commit */ 12073063Sperrin cv_broadcast(&zilog->zl_cv_writer); 12083063Sperrin mutex_exit(&zilog->zl_lock); 1209789Sahrens } 1210789Sahrens 1211789Sahrens /* 1212789Sahrens * Called in syncing context to free committed log blocks and update log header. 1213789Sahrens */ 1214789Sahrens void 1215789Sahrens zil_sync(zilog_t *zilog, dmu_tx_t *tx) 1216789Sahrens { 12171807Sbonwick zil_header_t *zh = zil_header_in_syncing_context(zilog); 1218789Sahrens uint64_t txg = dmu_tx_get_txg(tx); 1219789Sahrens spa_t *spa = zilog->zl_spa; 1220789Sahrens lwb_t *lwb; 1221789Sahrens 1222*9396SMatthew.Ahrens@Sun.COM /* 1223*9396SMatthew.Ahrens@Sun.COM * We don't zero out zl_destroy_txg, so make sure we don't try 1224*9396SMatthew.Ahrens@Sun.COM * to destroy it twice. 1225*9396SMatthew.Ahrens@Sun.COM */ 1226*9396SMatthew.Ahrens@Sun.COM if (spa_sync_pass(spa) != 1) 1227*9396SMatthew.Ahrens@Sun.COM return; 1228*9396SMatthew.Ahrens@Sun.COM 12291807Sbonwick mutex_enter(&zilog->zl_lock); 12301807Sbonwick 1231789Sahrens ASSERT(zilog->zl_stop_sync == 0); 1232789Sahrens 12338227SNeil.Perrin@Sun.COM zh->zh_replay_seq = zilog->zl_replayed_seq[txg & TXG_MASK]; 1234789Sahrens 1235789Sahrens if (zilog->zl_destroy_txg == txg) { 12361807Sbonwick blkptr_t blk = zh->zh_log; 12371807Sbonwick 12381807Sbonwick ASSERT(list_head(&zilog->zl_lwb_list) == NULL); 12391807Sbonwick 12401807Sbonwick bzero(zh, sizeof (zil_header_t)); 12418227SNeil.Perrin@Sun.COM bzero(zilog->zl_replayed_seq, sizeof (zilog->zl_replayed_seq)); 12421807Sbonwick 12431807Sbonwick if (zilog->zl_keep_first) { 12441807Sbonwick /* 12451807Sbonwick * If this block was part of log chain that couldn't 12461807Sbonwick * be claimed because a device was missing during 12471807Sbonwick * zil_claim(), but that device later returns, 12481807Sbonwick * then this block could erroneously appear valid. 12491807Sbonwick * To guard against this, assign a new GUID to the new 12501807Sbonwick * log chain so it doesn't matter what blk points to. 12511807Sbonwick */ 12521807Sbonwick zil_init_log_chain(zilog, &blk); 12531807Sbonwick zh->zh_log = blk; 12541807Sbonwick } 1255789Sahrens } 1256789Sahrens 1257789Sahrens for (;;) { 1258789Sahrens lwb = list_head(&zilog->zl_lwb_list); 1259789Sahrens if (lwb == NULL) { 1260789Sahrens mutex_exit(&zilog->zl_lock); 1261789Sahrens return; 1262789Sahrens } 12632638Sperrin zh->zh_log = lwb->lwb_blk; 1264789Sahrens if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg) 1265789Sahrens break; 1266789Sahrens list_remove(&zilog->zl_lwb_list, lwb); 1267789Sahrens zio_free_blk(spa, &lwb->lwb_blk, txg); 1268789Sahrens kmem_cache_free(zil_lwb_cache, lwb); 12693668Sgw25295 12703668Sgw25295 /* 12713668Sgw25295 * If we don't have anything left in the lwb list then 12723668Sgw25295 * we've had an allocation failure and we need to zero 12733668Sgw25295 * out the zil_header blkptr so that we don't end 12743668Sgw25295 * up freeing the same block twice. 12753668Sgw25295 */ 12763668Sgw25295 if (list_head(&zilog->zl_lwb_list) == NULL) 12773668Sgw25295 BP_ZERO(&zh->zh_log); 1278789Sahrens } 1279789Sahrens mutex_exit(&zilog->zl_lock); 1280789Sahrens } 1281789Sahrens 1282789Sahrens void 1283789Sahrens zil_init(void) 1284789Sahrens { 1285789Sahrens zil_lwb_cache = kmem_cache_create("zil_lwb_cache", 12862856Snd150628 sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0); 1287789Sahrens } 1288789Sahrens 1289789Sahrens void 1290789Sahrens zil_fini(void) 1291789Sahrens { 1292789Sahrens kmem_cache_destroy(zil_lwb_cache); 1293789Sahrens } 1294789Sahrens 1295789Sahrens zilog_t * 1296789Sahrens zil_alloc(objset_t *os, zil_header_t *zh_phys) 1297789Sahrens { 1298789Sahrens zilog_t *zilog; 1299789Sahrens 1300789Sahrens zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP); 1301789Sahrens 1302789Sahrens zilog->zl_header = zh_phys; 1303789Sahrens zilog->zl_os = os; 1304789Sahrens zilog->zl_spa = dmu_objset_spa(os); 1305789Sahrens zilog->zl_dmu_pool = dmu_objset_pool(os); 13061807Sbonwick zilog->zl_destroy_txg = TXG_INITIAL - 1; 1307789Sahrens 13082856Snd150628 mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL); 13092856Snd150628 1310789Sahrens list_create(&zilog->zl_itx_list, sizeof (itx_t), 1311789Sahrens offsetof(itx_t, itx_node)); 1312789Sahrens 1313789Sahrens list_create(&zilog->zl_lwb_list, sizeof (lwb_t), 1314789Sahrens offsetof(lwb_t, lwb_node)); 1315789Sahrens 13165688Sbonwick mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL); 13175688Sbonwick 13185688Sbonwick avl_create(&zilog->zl_vdev_tree, zil_vdev_compare, 13195688Sbonwick sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node)); 1320789Sahrens 13215913Sperrin cv_init(&zilog->zl_cv_writer, NULL, CV_DEFAULT, NULL); 13225913Sperrin cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL); 13235913Sperrin 1324789Sahrens return (zilog); 1325789Sahrens } 1326789Sahrens 1327789Sahrens void 1328789Sahrens zil_free(zilog_t *zilog) 1329789Sahrens { 1330789Sahrens lwb_t *lwb; 1331789Sahrens 1332789Sahrens zilog->zl_stop_sync = 1; 1333789Sahrens 1334789Sahrens while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 1335789Sahrens list_remove(&zilog->zl_lwb_list, lwb); 1336789Sahrens if (lwb->lwb_buf != NULL) 1337789Sahrens zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 1338789Sahrens kmem_cache_free(zil_lwb_cache, lwb); 1339789Sahrens } 1340789Sahrens list_destroy(&zilog->zl_lwb_list); 1341789Sahrens 13425688Sbonwick avl_destroy(&zilog->zl_vdev_tree); 13435688Sbonwick mutex_destroy(&zilog->zl_vdev_lock); 1344789Sahrens 1345789Sahrens ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1346789Sahrens list_destroy(&zilog->zl_itx_list); 13472856Snd150628 mutex_destroy(&zilog->zl_lock); 1348789Sahrens 13495913Sperrin cv_destroy(&zilog->zl_cv_writer); 13505913Sperrin cv_destroy(&zilog->zl_cv_suspend); 13515913Sperrin 1352789Sahrens kmem_free(zilog, sizeof (zilog_t)); 1353789Sahrens } 1354789Sahrens 1355789Sahrens /* 1356789Sahrens * Open an intent log. 1357789Sahrens */ 1358789Sahrens zilog_t * 1359789Sahrens zil_open(objset_t *os, zil_get_data_t *get_data) 1360789Sahrens { 1361789Sahrens zilog_t *zilog = dmu_objset_zil(os); 1362789Sahrens 1363789Sahrens zilog->zl_get_data = get_data; 1364789Sahrens zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri, 1365789Sahrens 2, 2, TASKQ_PREPOPULATE); 1366789Sahrens 1367789Sahrens return (zilog); 1368789Sahrens } 1369789Sahrens 1370789Sahrens /* 1371789Sahrens * Close an intent log. 1372789Sahrens */ 1373789Sahrens void 1374789Sahrens zil_close(zilog_t *zilog) 1375789Sahrens { 13761807Sbonwick /* 13771807Sbonwick * If the log isn't already committed, mark the objset dirty 13781807Sbonwick * (so zil_sync() will be called) and wait for that txg to sync. 13791807Sbonwick */ 13801807Sbonwick if (!zil_is_committed(zilog)) { 13811807Sbonwick uint64_t txg; 13821807Sbonwick dmu_tx_t *tx = dmu_tx_create(zilog->zl_os); 13831807Sbonwick (void) dmu_tx_assign(tx, TXG_WAIT); 13841807Sbonwick dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 13851807Sbonwick txg = dmu_tx_get_txg(tx); 13861807Sbonwick dmu_tx_commit(tx); 13871807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, txg); 13881807Sbonwick } 13891807Sbonwick 1390789Sahrens taskq_destroy(zilog->zl_clean_taskq); 1391789Sahrens zilog->zl_clean_taskq = NULL; 1392789Sahrens zilog->zl_get_data = NULL; 1393789Sahrens 1394789Sahrens zil_itx_clean(zilog); 1395789Sahrens ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1396789Sahrens } 1397789Sahrens 1398789Sahrens /* 1399789Sahrens * Suspend an intent log. While in suspended mode, we still honor 1400789Sahrens * synchronous semantics, but we rely on txg_wait_synced() to do it. 1401789Sahrens * We suspend the log briefly when taking a snapshot so that the snapshot 1402789Sahrens * contains all the data it's supposed to, and has an empty intent log. 1403789Sahrens */ 1404789Sahrens int 1405789Sahrens zil_suspend(zilog_t *zilog) 1406789Sahrens { 14071807Sbonwick const zil_header_t *zh = zilog->zl_header; 1408789Sahrens 1409789Sahrens mutex_enter(&zilog->zl_lock); 14108989SNeil.Perrin@Sun.COM if (zh->zh_flags & ZIL_REPLAY_NEEDED) { /* unplayed log */ 1411789Sahrens mutex_exit(&zilog->zl_lock); 1412789Sahrens return (EBUSY); 1413789Sahrens } 14141807Sbonwick if (zilog->zl_suspend++ != 0) { 14151807Sbonwick /* 14161807Sbonwick * Someone else already began a suspend. 14171807Sbonwick * Just wait for them to finish. 14181807Sbonwick */ 14191807Sbonwick while (zilog->zl_suspending) 14201807Sbonwick cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock); 14211807Sbonwick mutex_exit(&zilog->zl_lock); 14221807Sbonwick return (0); 14231807Sbonwick } 14241807Sbonwick zilog->zl_suspending = B_TRUE; 1425789Sahrens mutex_exit(&zilog->zl_lock); 1426789Sahrens 14272638Sperrin zil_commit(zilog, UINT64_MAX, 0); 1428789Sahrens 14292638Sperrin /* 14302638Sperrin * Wait for any in-flight log writes to complete. 14312638Sperrin */ 1432789Sahrens mutex_enter(&zilog->zl_lock); 14332638Sperrin while (zilog->zl_writer) 14342638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 1435789Sahrens mutex_exit(&zilog->zl_lock); 1436789Sahrens 14371807Sbonwick zil_destroy(zilog, B_FALSE); 14381807Sbonwick 14391807Sbonwick mutex_enter(&zilog->zl_lock); 14401807Sbonwick zilog->zl_suspending = B_FALSE; 14411807Sbonwick cv_broadcast(&zilog->zl_cv_suspend); 14421807Sbonwick mutex_exit(&zilog->zl_lock); 1443789Sahrens 1444789Sahrens return (0); 1445789Sahrens } 1446789Sahrens 1447789Sahrens void 1448789Sahrens zil_resume(zilog_t *zilog) 1449789Sahrens { 1450789Sahrens mutex_enter(&zilog->zl_lock); 1451789Sahrens ASSERT(zilog->zl_suspend != 0); 1452789Sahrens zilog->zl_suspend--; 1453789Sahrens mutex_exit(&zilog->zl_lock); 1454789Sahrens } 1455789Sahrens 1456789Sahrens typedef struct zil_replay_arg { 1457789Sahrens objset_t *zr_os; 1458789Sahrens zil_replay_func_t **zr_replay; 1459789Sahrens void *zr_arg; 1460789Sahrens boolean_t zr_byteswap; 1461789Sahrens char *zr_lrbuf; 1462789Sahrens } zil_replay_arg_t; 1463789Sahrens 1464789Sahrens static void 1465789Sahrens zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg) 1466789Sahrens { 1467789Sahrens zil_replay_arg_t *zr = zra; 14681807Sbonwick const zil_header_t *zh = zilog->zl_header; 1469789Sahrens uint64_t reclen = lr->lrc_reclen; 1470789Sahrens uint64_t txtype = lr->lrc_txtype; 14713063Sperrin char *name; 14728227SNeil.Perrin@Sun.COM int pass, error; 1473789Sahrens 14748227SNeil.Perrin@Sun.COM if (!zilog->zl_replay) /* giving up */ 1475789Sahrens return; 1476789Sahrens 1477789Sahrens if (lr->lrc_txg < claim_txg) /* already committed */ 1478789Sahrens return; 1479789Sahrens 1480789Sahrens if (lr->lrc_seq <= zh->zh_replay_seq) /* already replayed */ 1481789Sahrens return; 1482789Sahrens 14835331Samw /* Strip case-insensitive bit, still present in log record */ 14845331Samw txtype &= ~TX_CI; 14855331Samw 14868227SNeil.Perrin@Sun.COM if (txtype == 0 || txtype >= TX_MAX_TYPE) { 14878227SNeil.Perrin@Sun.COM error = EINVAL; 14888227SNeil.Perrin@Sun.COM goto bad; 14898227SNeil.Perrin@Sun.COM } 14908227SNeil.Perrin@Sun.COM 1491789Sahrens /* 1492789Sahrens * Make a copy of the data so we can revise and extend it. 1493789Sahrens */ 1494789Sahrens bcopy(lr, zr->zr_lrbuf, reclen); 1495789Sahrens 1496789Sahrens /* 1497789Sahrens * The log block containing this lr may have been byteswapped 1498789Sahrens * so that we can easily examine common fields like lrc_txtype. 1499789Sahrens * However, the log is a mix of different data types, and only the 1500789Sahrens * replay vectors know how to byteswap their records. Therefore, if 1501789Sahrens * the lr was byteswapped, undo it before invoking the replay vector. 1502789Sahrens */ 1503789Sahrens if (zr->zr_byteswap) 1504789Sahrens byteswap_uint64_array(zr->zr_lrbuf, reclen); 1505789Sahrens 1506789Sahrens /* 1507789Sahrens * If this is a TX_WRITE with a blkptr, suck in the data. 1508789Sahrens */ 1509789Sahrens if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) { 1510789Sahrens lr_write_t *lrw = (lr_write_t *)lr; 1511789Sahrens blkptr_t *wbp = &lrw->lr_blkptr; 1512789Sahrens uint64_t wlen = lrw->lr_length; 1513789Sahrens char *wbuf = zr->zr_lrbuf + reclen; 1514789Sahrens 1515789Sahrens if (BP_IS_HOLE(wbp)) { /* compressed to a hole */ 1516789Sahrens bzero(wbuf, wlen); 1517789Sahrens } else { 1518789Sahrens /* 1519789Sahrens * A subsequent write may have overwritten this block, 1520789Sahrens * in which case wbp may have been been freed and 1521789Sahrens * reallocated, and our read of wbp may fail with a 1522789Sahrens * checksum error. We can safely ignore this because 1523789Sahrens * the later write will provide the correct data. 1524789Sahrens */ 15251544Seschrock zbookmark_t zb; 15261544Seschrock 15271544Seschrock zb.zb_objset = dmu_objset_id(zilog->zl_os); 15281544Seschrock zb.zb_object = lrw->lr_foid; 15291544Seschrock zb.zb_level = -1; 15301544Seschrock zb.zb_blkid = lrw->lr_offset / BP_GET_LSIZE(wbp); 15311544Seschrock 1532789Sahrens (void) zio_wait(zio_read(NULL, zilog->zl_spa, 1533789Sahrens wbp, wbuf, BP_GET_LSIZE(wbp), NULL, NULL, 1534789Sahrens ZIO_PRIORITY_SYNC_READ, 15351544Seschrock ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb)); 1536789Sahrens (void) memmove(wbuf, wbuf + lrw->lr_blkoff, wlen); 1537789Sahrens } 1538789Sahrens } 1539789Sahrens 1540789Sahrens /* 1541789Sahrens * We must now do two things atomically: replay this log record, 15428227SNeil.Perrin@Sun.COM * and update the log header sequence number to reflect the fact that 15438227SNeil.Perrin@Sun.COM * we did so. At the end of each replay function the sequence number 15448227SNeil.Perrin@Sun.COM * is updated if we are in replay mode. 1545789Sahrens */ 15468227SNeil.Perrin@Sun.COM for (pass = 1; pass <= 2; pass++) { 15478227SNeil.Perrin@Sun.COM zilog->zl_replaying_seq = lr->lrc_seq; 15488227SNeil.Perrin@Sun.COM /* Only byteswap (if needed) on the 1st pass. */ 15498227SNeil.Perrin@Sun.COM error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lrbuf, 15508227SNeil.Perrin@Sun.COM zr->zr_byteswap && pass == 1); 1551789Sahrens 15523063Sperrin if (!error) 15533063Sperrin return; 15543063Sperrin 15553063Sperrin /* 15563063Sperrin * The DMU's dnode layer doesn't see removes until the txg 15573063Sperrin * commits, so a subsequent claim can spuriously fail with 15588227SNeil.Perrin@Sun.COM * EEXIST. So if we receive any error we try syncing out 15598227SNeil.Perrin@Sun.COM * any removes then retry the transaction. 15603063Sperrin */ 15618227SNeil.Perrin@Sun.COM if (pass == 1) 15623063Sperrin txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0); 1563789Sahrens } 1564789Sahrens 15657904SNeil.Perrin@Sun.COM bad: 15668227SNeil.Perrin@Sun.COM ASSERT(error); 15673063Sperrin name = kmem_alloc(MAXNAMELEN, KM_SLEEP); 15683063Sperrin dmu_objset_name(zr->zr_os, name); 15693063Sperrin cmn_err(CE_WARN, "ZFS replay transaction error %d, " 15705331Samw "dataset %s, seq 0x%llx, txtype %llu %s\n", 15715331Samw error, name, (u_longlong_t)lr->lrc_seq, (u_longlong_t)txtype, 15725331Samw (lr->lrc_txtype & TX_CI) ? "CI" : ""); 15738227SNeil.Perrin@Sun.COM zilog->zl_replay = B_FALSE; 15743063Sperrin kmem_free(name, MAXNAMELEN); 15753063Sperrin } 1576789Sahrens 15773063Sperrin /* ARGSUSED */ 15783063Sperrin static void 15793063Sperrin zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg) 15803063Sperrin { 15813063Sperrin zilog->zl_replay_blks++; 1582789Sahrens } 1583789Sahrens 1584789Sahrens /* 15851362Sperrin * If this dataset has a non-empty intent log, replay it and destroy it. 1586789Sahrens */ 1587789Sahrens void 15888227SNeil.Perrin@Sun.COM zil_replay(objset_t *os, void *arg, zil_replay_func_t *replay_func[TX_MAX_TYPE]) 1589789Sahrens { 1590789Sahrens zilog_t *zilog = dmu_objset_zil(os); 15911807Sbonwick const zil_header_t *zh = zilog->zl_header; 15921807Sbonwick zil_replay_arg_t zr; 15931362Sperrin 15948989SNeil.Perrin@Sun.COM if ((zh->zh_flags & ZIL_REPLAY_NEEDED) == 0) { 15951807Sbonwick zil_destroy(zilog, B_TRUE); 15961362Sperrin return; 15971362Sperrin } 1598789Sahrens 1599789Sahrens zr.zr_os = os; 1600789Sahrens zr.zr_replay = replay_func; 1601789Sahrens zr.zr_arg = arg; 16021807Sbonwick zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log); 1603789Sahrens zr.zr_lrbuf = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP); 1604789Sahrens 1605789Sahrens /* 1606789Sahrens * Wait for in-progress removes to sync before starting replay. 1607789Sahrens */ 1608789Sahrens txg_wait_synced(zilog->zl_dmu_pool, 0); 1609789Sahrens 16108227SNeil.Perrin@Sun.COM zilog->zl_replay = B_TRUE; 16113063Sperrin zilog->zl_replay_time = lbolt; 16123063Sperrin ASSERT(zilog->zl_replay_blks == 0); 16133063Sperrin (void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr, 16141807Sbonwick zh->zh_claim_txg); 1615789Sahrens kmem_free(zr.zr_lrbuf, 2 * SPA_MAXBLOCKSIZE); 1616789Sahrens 16171807Sbonwick zil_destroy(zilog, B_FALSE); 16185712Sahrens txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 16198227SNeil.Perrin@Sun.COM zilog->zl_replay = B_FALSE; 1620789Sahrens } 16211646Sperrin 16221646Sperrin /* 16231646Sperrin * Report whether all transactions are committed 16241646Sperrin */ 16251646Sperrin int 16261646Sperrin zil_is_committed(zilog_t *zilog) 16271646Sperrin { 16281646Sperrin lwb_t *lwb; 16292638Sperrin int ret; 16301646Sperrin 16312638Sperrin mutex_enter(&zilog->zl_lock); 16322638Sperrin while (zilog->zl_writer) 16332638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 16342638Sperrin 16352638Sperrin /* recent unpushed intent log transactions? */ 16362638Sperrin if (!list_is_empty(&zilog->zl_itx_list)) { 16372638Sperrin ret = B_FALSE; 16382638Sperrin goto out; 16392638Sperrin } 16402638Sperrin 16412638Sperrin /* intent log never used? */ 16422638Sperrin lwb = list_head(&zilog->zl_lwb_list); 16432638Sperrin if (lwb == NULL) { 16442638Sperrin ret = B_TRUE; 16452638Sperrin goto out; 16462638Sperrin } 16471646Sperrin 16481646Sperrin /* 16492638Sperrin * more than 1 log buffer means zil_sync() hasn't yet freed 16502638Sperrin * entries after a txg has committed 16511646Sperrin */ 16522638Sperrin if (list_next(&zilog->zl_lwb_list, lwb)) { 16532638Sperrin ret = B_FALSE; 16542638Sperrin goto out; 16552638Sperrin } 16562638Sperrin 16571646Sperrin ASSERT(zil_empty(zilog)); 16582638Sperrin ret = B_TRUE; 16592638Sperrin out: 16602638Sperrin cv_broadcast(&zilog->zl_cv_writer); 16612638Sperrin mutex_exit(&zilog->zl_lock); 16622638Sperrin return (ret); 16631646Sperrin } 1664