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 /* 22*8746SMatthew.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 4732199Sahrens int 474789Sahrens zil_claim(char *osname, void *txarg) 475789Sahrens { 476789Sahrens dmu_tx_t *tx = txarg; 477789Sahrens uint64_t first_txg = dmu_tx_get_txg(tx); 478789Sahrens zilog_t *zilog; 479789Sahrens zil_header_t *zh; 480789Sahrens objset_t *os; 481789Sahrens int error; 482789Sahrens 4836689Smaybee error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_USER, &os); 484789Sahrens if (error) { 4857294Sperrin cmn_err(CE_WARN, "can't open objset for %s", osname); 4862199Sahrens return (0); 487789Sahrens } 488789Sahrens 489789Sahrens zilog = dmu_objset_zil(os); 4901807Sbonwick zh = zil_header_in_syncing_context(zilog); 491789Sahrens 492789Sahrens /* 4931807Sbonwick * Claim all log blocks if we haven't already done so, and remember 4941807Sbonwick * the highest claimed sequence number. This ensures that if we can 4951807Sbonwick * read only part of the log now (e.g. due to a missing device), 4961807Sbonwick * but we can read the entire log later, we will not try to replay 4971807Sbonwick * or destroy beyond the last block we successfully claimed. 498789Sahrens */ 499789Sahrens ASSERT3U(zh->zh_claim_txg, <=, first_txg); 500789Sahrens if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) { 501789Sahrens zh->zh_claim_txg = first_txg; 5021807Sbonwick zh->zh_claim_seq = zil_parse(zilog, zil_claim_log_block, 5031807Sbonwick zil_claim_log_record, tx, first_txg); 504789Sahrens dsl_dataset_dirty(dmu_objset_ds(os), tx); 505789Sahrens } 5061807Sbonwick 507789Sahrens ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1)); 508789Sahrens dmu_objset_close(os); 5092199Sahrens return (0); 510789Sahrens } 511789Sahrens 5127294Sperrin /* 5137294Sperrin * Check the log by walking the log chain. 5147294Sperrin * Checksum errors are ok as they indicate the end of the chain. 5157294Sperrin * Any other error (no device or read failure) returns an error. 5167294Sperrin */ 5177294Sperrin /* ARGSUSED */ 5187294Sperrin int 5197294Sperrin zil_check_log_chain(char *osname, void *txarg) 5207294Sperrin { 5217294Sperrin zilog_t *zilog; 5227294Sperrin zil_header_t *zh; 5237294Sperrin blkptr_t blk; 5247294Sperrin arc_buf_t *abuf; 5257294Sperrin objset_t *os; 5267294Sperrin char *lrbuf; 5277294Sperrin zil_trailer_t *ztp; 5287294Sperrin int error; 5297294Sperrin 5307294Sperrin error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_USER, &os); 5317294Sperrin if (error) { 5327294Sperrin cmn_err(CE_WARN, "can't open objset for %s", osname); 5337294Sperrin return (0); 5347294Sperrin } 5357294Sperrin 5367294Sperrin zilog = dmu_objset_zil(os); 5377294Sperrin zh = zil_header_in_syncing_context(zilog); 5387294Sperrin blk = zh->zh_log; 5397294Sperrin if (BP_IS_HOLE(&blk)) { 5407294Sperrin dmu_objset_close(os); 5417294Sperrin return (0); /* no chain */ 5427294Sperrin } 5437294Sperrin 5447294Sperrin for (;;) { 5457294Sperrin error = zil_read_log_block(zilog, &blk, &abuf); 5467294Sperrin if (error) 5477294Sperrin break; 5487294Sperrin lrbuf = abuf->b_data; 5497294Sperrin ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1; 5507294Sperrin blk = ztp->zit_next_blk; 5517294Sperrin VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 5527294Sperrin } 5537294Sperrin dmu_objset_close(os); 5547294Sperrin if (error == ECKSUM) 5557294Sperrin return (0); /* normal end of chain */ 5567294Sperrin return (error); 5577294Sperrin } 5587294Sperrin 5597294Sperrin /* 5607294Sperrin * Clear a log chain 5617294Sperrin */ 5627294Sperrin /* ARGSUSED */ 5637294Sperrin int 5647294Sperrin zil_clear_log_chain(char *osname, void *txarg) 5657294Sperrin { 5667294Sperrin zilog_t *zilog; 5677294Sperrin zil_header_t *zh; 5687294Sperrin objset_t *os; 5697294Sperrin dmu_tx_t *tx; 5707294Sperrin int error; 5717294Sperrin 5727294Sperrin error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_USER, &os); 5737294Sperrin if (error) { 5747294Sperrin cmn_err(CE_WARN, "can't open objset for %s", osname); 5757294Sperrin return (0); 5767294Sperrin } 5777294Sperrin 5787294Sperrin zilog = dmu_objset_zil(os); 5797294Sperrin tx = dmu_tx_create(zilog->zl_os); 5807294Sperrin (void) dmu_tx_assign(tx, TXG_WAIT); 5817294Sperrin zh = zil_header_in_syncing_context(zilog); 5827294Sperrin BP_ZERO(&zh->zh_log); 5837294Sperrin dsl_dataset_dirty(dmu_objset_ds(os), tx); 5847294Sperrin dmu_tx_commit(tx); 5857294Sperrin dmu_objset_close(os); 5867294Sperrin return (0); 5877294Sperrin } 5887294Sperrin 5895688Sbonwick static int 5905688Sbonwick zil_vdev_compare(const void *x1, const void *x2) 591789Sahrens { 5925875Sperrin uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev; 5935875Sperrin uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev; 5945688Sbonwick 5955688Sbonwick if (v1 < v2) 5965688Sbonwick return (-1); 5975688Sbonwick if (v1 > v2) 5985688Sbonwick return (1); 5995688Sbonwick 6005688Sbonwick return (0); 6015688Sbonwick } 6025688Sbonwick 6035688Sbonwick void 6045688Sbonwick zil_add_block(zilog_t *zilog, blkptr_t *bp) 6055688Sbonwick { 6065688Sbonwick avl_tree_t *t = &zilog->zl_vdev_tree; 6075688Sbonwick avl_index_t where; 6085688Sbonwick zil_vdev_node_t *zv, zvsearch; 6095688Sbonwick int ndvas = BP_GET_NDVAS(bp); 6105688Sbonwick int i; 611789Sahrens 6122986Sek110237 if (zfs_nocacheflush) 613789Sahrens return; 614789Sahrens 6155688Sbonwick ASSERT(zilog->zl_writer); 6165688Sbonwick 6175688Sbonwick /* 6185688Sbonwick * Even though we're zl_writer, we still need a lock because the 6195688Sbonwick * zl_get_data() callbacks may have dmu_sync() done callbacks 6205688Sbonwick * that will run concurrently. 6215688Sbonwick */ 6225688Sbonwick mutex_enter(&zilog->zl_vdev_lock); 6235688Sbonwick for (i = 0; i < ndvas; i++) { 6245688Sbonwick zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]); 6255688Sbonwick if (avl_find(t, &zvsearch, &where) == NULL) { 6265688Sbonwick zv = kmem_alloc(sizeof (*zv), KM_SLEEP); 6275688Sbonwick zv->zv_vdev = zvsearch.zv_vdev; 6285688Sbonwick avl_insert(t, zv, where); 6293063Sperrin } 6303063Sperrin } 6315688Sbonwick mutex_exit(&zilog->zl_vdev_lock); 6323063Sperrin } 6333063Sperrin 634789Sahrens void 6352638Sperrin zil_flush_vdevs(zilog_t *zilog) 636789Sahrens { 6373063Sperrin spa_t *spa = zilog->zl_spa; 6385688Sbonwick avl_tree_t *t = &zilog->zl_vdev_tree; 6395688Sbonwick void *cookie = NULL; 6405688Sbonwick zil_vdev_node_t *zv; 6415688Sbonwick zio_t *zio; 6423063Sperrin 6433063Sperrin ASSERT(zilog->zl_writer); 644789Sahrens 6455688Sbonwick /* 6465688Sbonwick * We don't need zl_vdev_lock here because we're the zl_writer, 6475688Sbonwick * and all zl_get_data() callbacks are done. 6485688Sbonwick */ 6495688Sbonwick if (avl_numnodes(t) == 0) 6505688Sbonwick return; 6515688Sbonwick 6527754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 6535688Sbonwick 6547754SJeff.Bonwick@Sun.COM zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); 6555688Sbonwick 6565688Sbonwick while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) { 6575688Sbonwick vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev); 6585688Sbonwick if (vd != NULL) 6595688Sbonwick zio_flush(zio, vd); 6605688Sbonwick kmem_free(zv, sizeof (*zv)); 6613063Sperrin } 662789Sahrens 663789Sahrens /* 664789Sahrens * Wait for all the flushes to complete. Not all devices actually 665789Sahrens * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails. 666789Sahrens */ 6675688Sbonwick (void) zio_wait(zio); 6685688Sbonwick 6697754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 670789Sahrens } 671789Sahrens 672789Sahrens /* 673789Sahrens * Function called when a log block write completes 674789Sahrens */ 675789Sahrens static void 676789Sahrens zil_lwb_write_done(zio_t *zio) 677789Sahrens { 678789Sahrens lwb_t *lwb = zio->io_private; 679789Sahrens zilog_t *zilog = lwb->lwb_zilog; 680789Sahrens 6817754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF); 6827754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_CHECKSUM(zio->io_bp) == ZIO_CHECKSUM_ZILOG); 6837754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_TYPE(zio->io_bp) == DMU_OT_INTENT_LOG); 6847754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 6857754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_BYTEORDER(zio->io_bp) == ZFS_HOST_BYTEORDER); 6867754SJeff.Bonwick@Sun.COM ASSERT(!BP_IS_GANG(zio->io_bp)); 6877754SJeff.Bonwick@Sun.COM ASSERT(!BP_IS_HOLE(zio->io_bp)); 6887754SJeff.Bonwick@Sun.COM ASSERT(zio->io_bp->blk_fill == 0); 6897754SJeff.Bonwick@Sun.COM 690789Sahrens /* 691789Sahrens * Now that we've written this log block, we have a stable pointer 692789Sahrens * to the next block in the chain, so it's OK to let the txg in 693789Sahrens * which we allocated the next block sync. 694789Sahrens */ 695789Sahrens txg_rele_to_sync(&lwb->lwb_txgh); 696789Sahrens 697789Sahrens zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 698789Sahrens mutex_enter(&zilog->zl_lock); 699789Sahrens lwb->lwb_buf = NULL; 7004527Sperrin if (zio->io_error) 701789Sahrens zilog->zl_log_error = B_TRUE; 702789Sahrens mutex_exit(&zilog->zl_lock); 703789Sahrens } 704789Sahrens 705789Sahrens /* 7062237Smaybee * Initialize the io for a log block. 7072237Smaybee */ 7082237Smaybee static void 7092237Smaybee zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb) 7102237Smaybee { 7112237Smaybee zbookmark_t zb; 7122237Smaybee 7132237Smaybee zb.zb_objset = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET]; 7142237Smaybee zb.zb_object = 0; 7152237Smaybee zb.zb_level = -1; 7162237Smaybee zb.zb_blkid = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 7172237Smaybee 7182638Sperrin if (zilog->zl_root_zio == NULL) { 7192638Sperrin zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL, 7202638Sperrin ZIO_FLAG_CANFAIL); 7212638Sperrin } 7223063Sperrin if (lwb->lwb_zio == NULL) { 7233063Sperrin lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa, 7247754SJeff.Bonwick@Sun.COM 0, &lwb->lwb_blk, lwb->lwb_buf, 7253063Sperrin lwb->lwb_sz, zil_lwb_write_done, lwb, 7264527Sperrin ZIO_PRIORITY_LOG_WRITE, ZIO_FLAG_CANFAIL, &zb); 7273063Sperrin } 7282237Smaybee } 7292237Smaybee 7302237Smaybee /* 731789Sahrens * Start a log block write and advance to the next log block. 732789Sahrens * Calls are serialized. 733789Sahrens */ 734789Sahrens static lwb_t * 735789Sahrens zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb) 736789Sahrens { 737789Sahrens lwb_t *nlwb; 738789Sahrens zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1; 7391807Sbonwick spa_t *spa = zilog->zl_spa; 7401807Sbonwick blkptr_t *bp = &ztp->zit_next_blk; 741789Sahrens uint64_t txg; 742789Sahrens uint64_t zil_blksz; 743789Sahrens int error; 744789Sahrens 745789Sahrens ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb)); 746789Sahrens 747789Sahrens /* 748789Sahrens * Allocate the next block and save its address in this block 749789Sahrens * before writing it in order to establish the log chain. 750789Sahrens * Note that if the allocation of nlwb synced before we wrote 751789Sahrens * the block that points at it (lwb), we'd leak it if we crashed. 752789Sahrens * Therefore, we don't do txg_rele_to_sync() until zil_lwb_write_done(). 753789Sahrens */ 754789Sahrens txg = txg_hold_open(zilog->zl_dmu_pool, &lwb->lwb_txgh); 755789Sahrens txg_rele_to_quiesce(&lwb->lwb_txgh); 756789Sahrens 757789Sahrens /* 7581141Sperrin * Pick a ZIL blocksize. We request a size that is the 7591141Sperrin * maximum of the previous used size, the current used size and 7601141Sperrin * the amount waiting in the queue. 761789Sahrens */ 7622237Smaybee zil_blksz = MAX(zilog->zl_prev_used, 7632237Smaybee zilog->zl_cur_used + sizeof (*ztp)); 7641141Sperrin zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp)); 7651842Sperrin zil_blksz = P2ROUNDUP_TYPED(zil_blksz, ZIL_MIN_BLKSZ, uint64_t); 7661141Sperrin if (zil_blksz > ZIL_MAX_BLKSZ) 7671141Sperrin zil_blksz = ZIL_MAX_BLKSZ; 768789Sahrens 7693063Sperrin BP_ZERO(bp); 7703063Sperrin /* pass the old blkptr in order to spread log blocks across devs */ 7713063Sperrin error = zio_alloc_blk(spa, zil_blksz, bp, &lwb->lwb_blk, txg); 772789Sahrens if (error) { 7733668Sgw25295 dmu_tx_t *tx = dmu_tx_create_assigned(zilog->zl_dmu_pool, txg); 7743668Sgw25295 7751544Seschrock /* 7763668Sgw25295 * We dirty the dataset to ensure that zil_sync() will 7773668Sgw25295 * be called to remove this lwb from our zl_lwb_list. 7783668Sgw25295 * Failing to do so, may leave an lwb with a NULL lwb_buf 7793668Sgw25295 * hanging around on the zl_lwb_list. 7803668Sgw25295 */ 7813668Sgw25295 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 7823848Sgw25295 dmu_tx_commit(tx); 7833668Sgw25295 7843668Sgw25295 /* 7853668Sgw25295 * Since we've just experienced an allocation failure so we 7863668Sgw25295 * terminate the current lwb and send it on its way. 7873668Sgw25295 */ 7883668Sgw25295 ztp->zit_pad = 0; 7893668Sgw25295 ztp->zit_nused = lwb->lwb_nused; 7903668Sgw25295 ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum; 7913668Sgw25295 zio_nowait(lwb->lwb_zio); 7923668Sgw25295 7933668Sgw25295 /* 7941544Seschrock * By returning NULL the caller will call tx_wait_synced() 7951544Seschrock */ 796789Sahrens return (NULL); 797789Sahrens } 798789Sahrens 7991807Sbonwick ASSERT3U(bp->blk_birth, ==, txg); 8001544Seschrock ztp->zit_pad = 0; 801789Sahrens ztp->zit_nused = lwb->lwb_nused; 802789Sahrens ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum; 8031807Sbonwick bp->blk_cksum = lwb->lwb_blk.blk_cksum; 8041807Sbonwick bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++; 805789Sahrens 806789Sahrens /* 807789Sahrens * Allocate a new log write buffer (lwb). 808789Sahrens */ 809789Sahrens nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 810789Sahrens 811789Sahrens nlwb->lwb_zilog = zilog; 8121807Sbonwick nlwb->lwb_blk = *bp; 813789Sahrens nlwb->lwb_nused = 0; 814789Sahrens nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk); 815789Sahrens nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz); 816789Sahrens nlwb->lwb_max_txg = txg; 8172237Smaybee nlwb->lwb_zio = NULL; 818789Sahrens 819789Sahrens /* 8203063Sperrin * Put new lwb at the end of the log chain 821789Sahrens */ 822789Sahrens mutex_enter(&zilog->zl_lock); 823789Sahrens list_insert_tail(&zilog->zl_lwb_list, nlwb); 8243063Sperrin mutex_exit(&zilog->zl_lock); 8253063Sperrin 8265688Sbonwick /* Record the block for later vdev flushing */ 8275688Sbonwick zil_add_block(zilog, &lwb->lwb_blk); 828789Sahrens 829789Sahrens /* 8302237Smaybee * kick off the write for the old log block 831789Sahrens */ 8322237Smaybee dprintf_bp(&lwb->lwb_blk, "lwb %p txg %llu: ", lwb, txg); 8333063Sperrin ASSERT(lwb->lwb_zio); 8342237Smaybee zio_nowait(lwb->lwb_zio); 835789Sahrens 836789Sahrens return (nlwb); 837789Sahrens } 838789Sahrens 839789Sahrens static lwb_t * 840789Sahrens zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb) 841789Sahrens { 842789Sahrens lr_t *lrc = &itx->itx_lr; /* common log record */ 8432237Smaybee lr_write_t *lr = (lr_write_t *)lrc; 844789Sahrens uint64_t txg = lrc->lrc_txg; 845789Sahrens uint64_t reclen = lrc->lrc_reclen; 8462237Smaybee uint64_t dlen; 847789Sahrens 848789Sahrens if (lwb == NULL) 849789Sahrens return (NULL); 850789Sahrens ASSERT(lwb->lwb_buf != NULL); 851789Sahrens 8522237Smaybee if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY) 8532237Smaybee dlen = P2ROUNDUP_TYPED( 8542237Smaybee lr->lr_length, sizeof (uint64_t), uint64_t); 8552237Smaybee else 8562237Smaybee dlen = 0; 8571669Sperrin 8581669Sperrin zilog->zl_cur_used += (reclen + dlen); 8591669Sperrin 8603063Sperrin zil_lwb_write_init(zilog, lwb); 8613063Sperrin 8621669Sperrin /* 8631669Sperrin * If this record won't fit in the current log block, start a new one. 8641669Sperrin */ 8651669Sperrin if (lwb->lwb_nused + reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) { 8661669Sperrin lwb = zil_lwb_write_start(zilog, lwb); 8672237Smaybee if (lwb == NULL) 8681669Sperrin return (NULL); 8693063Sperrin zil_lwb_write_init(zilog, lwb); 8701669Sperrin ASSERT(lwb->lwb_nused == 0); 8711669Sperrin if (reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) { 8721669Sperrin txg_wait_synced(zilog->zl_dmu_pool, txg); 873789Sahrens return (lwb); 874789Sahrens } 875789Sahrens } 876789Sahrens 8772638Sperrin /* 8782638Sperrin * Update the lrc_seq, to be log record sequence number. See zil.h 8792638Sperrin * Then copy the record to the log buffer. 8802638Sperrin */ 8812638Sperrin lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */ 882789Sahrens bcopy(lrc, lwb->lwb_buf + lwb->lwb_nused, reclen); 8832237Smaybee 8842237Smaybee /* 8852237Smaybee * If it's a write, fetch the data or get its blkptr as appropriate. 8862237Smaybee */ 8872237Smaybee if (lrc->lrc_txtype == TX_WRITE) { 8882237Smaybee if (txg > spa_freeze_txg(zilog->zl_spa)) 8892237Smaybee txg_wait_synced(zilog->zl_dmu_pool, txg); 8902237Smaybee if (itx->itx_wr_state != WR_COPIED) { 8912237Smaybee char *dbuf; 8922237Smaybee int error; 8932237Smaybee 8942237Smaybee /* alignment is guaranteed */ 8952237Smaybee lr = (lr_write_t *)(lwb->lwb_buf + lwb->lwb_nused); 8962237Smaybee if (dlen) { 8972237Smaybee ASSERT(itx->itx_wr_state == WR_NEED_COPY); 8982237Smaybee dbuf = lwb->lwb_buf + lwb->lwb_nused + reclen; 8992237Smaybee lr->lr_common.lrc_reclen += dlen; 9002237Smaybee } else { 9012237Smaybee ASSERT(itx->itx_wr_state == WR_INDIRECT); 9022237Smaybee dbuf = NULL; 9032237Smaybee } 9042237Smaybee error = zilog->zl_get_data( 9052237Smaybee itx->itx_private, lr, dbuf, lwb->lwb_zio); 9062237Smaybee if (error) { 9072237Smaybee ASSERT(error == ENOENT || error == EEXIST || 9082237Smaybee error == EALREADY); 9092237Smaybee return (lwb); 9102237Smaybee } 9112237Smaybee } 9121669Sperrin } 9132237Smaybee 9142237Smaybee lwb->lwb_nused += reclen + dlen; 915789Sahrens lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg); 916789Sahrens ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb)); 917789Sahrens ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0); 918789Sahrens 919789Sahrens return (lwb); 920789Sahrens } 921789Sahrens 922789Sahrens itx_t * 9235331Samw zil_itx_create(uint64_t txtype, size_t lrsize) 924789Sahrens { 925789Sahrens itx_t *itx; 926789Sahrens 9271842Sperrin lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t); 928789Sahrens 929789Sahrens itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP); 930789Sahrens itx->itx_lr.lrc_txtype = txtype; 931789Sahrens itx->itx_lr.lrc_reclen = lrsize; 9326101Sperrin itx->itx_sod = lrsize; /* if write & WR_NEED_COPY will be increased */ 933789Sahrens itx->itx_lr.lrc_seq = 0; /* defensive */ 934789Sahrens 935789Sahrens return (itx); 936789Sahrens } 937789Sahrens 938789Sahrens uint64_t 939789Sahrens zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx) 940789Sahrens { 941789Sahrens uint64_t seq; 942789Sahrens 943789Sahrens ASSERT(itx->itx_lr.lrc_seq == 0); 944789Sahrens 945789Sahrens mutex_enter(&zilog->zl_lock); 946789Sahrens list_insert_tail(&zilog->zl_itx_list, itx); 9476101Sperrin zilog->zl_itx_list_sz += itx->itx_sod; 948789Sahrens itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx); 949789Sahrens itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq; 950789Sahrens mutex_exit(&zilog->zl_lock); 951789Sahrens 952789Sahrens return (seq); 953789Sahrens } 954789Sahrens 955789Sahrens /* 956789Sahrens * Free up all in-memory intent log transactions that have now been synced. 957789Sahrens */ 958789Sahrens static void 959789Sahrens zil_itx_clean(zilog_t *zilog) 960789Sahrens { 961789Sahrens uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa); 962789Sahrens uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa); 9633778Sjohansen list_t clean_list; 964789Sahrens itx_t *itx; 965789Sahrens 9663778Sjohansen list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node)); 9673778Sjohansen 968789Sahrens mutex_enter(&zilog->zl_lock); 9692638Sperrin /* wait for a log writer to finish walking list */ 9702638Sperrin while (zilog->zl_writer) { 9712638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 9722638Sperrin } 9733778Sjohansen 9743778Sjohansen /* 9753778Sjohansen * Move the sync'd log transactions to a separate list so we can call 9763778Sjohansen * kmem_free without holding the zl_lock. 9773778Sjohansen * 9783778Sjohansen * There is no need to set zl_writer as we don't drop zl_lock here 9793778Sjohansen */ 980789Sahrens while ((itx = list_head(&zilog->zl_itx_list)) != NULL && 981789Sahrens itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) { 982789Sahrens list_remove(&zilog->zl_itx_list, itx); 9836101Sperrin zilog->zl_itx_list_sz -= itx->itx_sod; 9843778Sjohansen list_insert_tail(&clean_list, itx); 9853778Sjohansen } 9863778Sjohansen cv_broadcast(&zilog->zl_cv_writer); 9873778Sjohansen mutex_exit(&zilog->zl_lock); 9883778Sjohansen 9893778Sjohansen /* destroy sync'd log transactions */ 9903778Sjohansen while ((itx = list_head(&clean_list)) != NULL) { 9913778Sjohansen list_remove(&clean_list, itx); 992789Sahrens kmem_free(itx, offsetof(itx_t, itx_lr) 993789Sahrens + itx->itx_lr.lrc_reclen); 994789Sahrens } 9953778Sjohansen list_destroy(&clean_list); 996789Sahrens } 997789Sahrens 9982638Sperrin /* 9993063Sperrin * If there are any in-memory intent log transactions which have now been 10003063Sperrin * synced then start up a taskq to free them. 10012638Sperrin */ 1002789Sahrens void 1003789Sahrens zil_clean(zilog_t *zilog) 1004789Sahrens { 10053063Sperrin itx_t *itx; 10063063Sperrin 1007789Sahrens mutex_enter(&zilog->zl_lock); 10083063Sperrin itx = list_head(&zilog->zl_itx_list); 10093063Sperrin if ((itx != NULL) && 10103063Sperrin (itx->itx_lr.lrc_txg <= spa_last_synced_txg(zilog->zl_spa))) { 1011789Sahrens (void) taskq_dispatch(zilog->zl_clean_taskq, 1012789Sahrens (void (*)(void *))zil_itx_clean, zilog, TQ_NOSLEEP); 10133063Sperrin } 1014789Sahrens mutex_exit(&zilog->zl_lock); 1015789Sahrens } 1016789Sahrens 10177754SJeff.Bonwick@Sun.COM static void 10182638Sperrin zil_commit_writer(zilog_t *zilog, uint64_t seq, uint64_t foid) 1019789Sahrens { 1020789Sahrens uint64_t txg; 10213063Sperrin uint64_t commit_seq = 0; 10222638Sperrin itx_t *itx, *itx_next = (itx_t *)-1; 1023789Sahrens lwb_t *lwb; 1024789Sahrens spa_t *spa; 1025789Sahrens 10262638Sperrin zilog->zl_writer = B_TRUE; 10277754SJeff.Bonwick@Sun.COM ASSERT(zilog->zl_root_zio == NULL); 1028789Sahrens spa = zilog->zl_spa; 1029789Sahrens 1030789Sahrens if (zilog->zl_suspend) { 1031789Sahrens lwb = NULL; 1032789Sahrens } else { 1033789Sahrens lwb = list_tail(&zilog->zl_lwb_list); 1034789Sahrens if (lwb == NULL) { 10352638Sperrin /* 10362638Sperrin * Return if there's nothing to flush before we 10372638Sperrin * dirty the fs by calling zil_create() 10382638Sperrin */ 10392638Sperrin if (list_is_empty(&zilog->zl_itx_list)) { 10402638Sperrin zilog->zl_writer = B_FALSE; 10412638Sperrin return; 10422638Sperrin } 1043789Sahrens mutex_exit(&zilog->zl_lock); 1044789Sahrens zil_create(zilog); 1045789Sahrens mutex_enter(&zilog->zl_lock); 1046789Sahrens lwb = list_tail(&zilog->zl_lwb_list); 1047789Sahrens } 1048789Sahrens } 1049789Sahrens 10503063Sperrin /* Loop through in-memory log transactions filling log blocks. */ 10512638Sperrin DTRACE_PROBE1(zil__cw1, zilog_t *, zilog); 1052789Sahrens for (;;) { 10532638Sperrin /* 10542638Sperrin * Find the next itx to push: 10552638Sperrin * Push all transactions related to specified foid and all 10562638Sperrin * other transactions except TX_WRITE, TX_TRUNCATE, 10572638Sperrin * TX_SETATTR and TX_ACL for all other files. 10582638Sperrin */ 10592638Sperrin if (itx_next != (itx_t *)-1) 10602638Sperrin itx = itx_next; 10612638Sperrin else 10622638Sperrin itx = list_head(&zilog->zl_itx_list); 10632638Sperrin for (; itx != NULL; itx = list_next(&zilog->zl_itx_list, itx)) { 10642638Sperrin if (foid == 0) /* push all foids? */ 10652638Sperrin break; 10663063Sperrin if (itx->itx_sync) /* push all O_[D]SYNC */ 10673063Sperrin break; 10682638Sperrin switch (itx->itx_lr.lrc_txtype) { 10692638Sperrin case TX_SETATTR: 10702638Sperrin case TX_WRITE: 10712638Sperrin case TX_TRUNCATE: 10722638Sperrin case TX_ACL: 10732638Sperrin /* lr_foid is same offset for these records */ 10742638Sperrin if (((lr_write_t *)&itx->itx_lr)->lr_foid 10752638Sperrin != foid) { 10762638Sperrin continue; /* skip this record */ 10772638Sperrin } 10782638Sperrin } 10792638Sperrin break; 10802638Sperrin } 1081789Sahrens if (itx == NULL) 1082789Sahrens break; 1083789Sahrens 1084789Sahrens if ((itx->itx_lr.lrc_seq > seq) && 10852638Sperrin ((lwb == NULL) || (lwb->lwb_nused == 0) || 10866101Sperrin (lwb->lwb_nused + itx->itx_sod > ZIL_BLK_DATA_SZ(lwb)))) { 1087789Sahrens break; 10883063Sperrin } 1089789Sahrens 10902638Sperrin /* 10912638Sperrin * Save the next pointer. Even though we soon drop 10922638Sperrin * zl_lock all threads that may change the list 10932638Sperrin * (another writer or zil_itx_clean) can't do so until 10942638Sperrin * they have zl_writer. 10952638Sperrin */ 10962638Sperrin itx_next = list_next(&zilog->zl_itx_list, itx); 1097789Sahrens list_remove(&zilog->zl_itx_list, itx); 10986101Sperrin zilog->zl_itx_list_sz -= itx->itx_sod; 10993063Sperrin mutex_exit(&zilog->zl_lock); 1100789Sahrens txg = itx->itx_lr.lrc_txg; 1101789Sahrens ASSERT(txg); 1102789Sahrens 1103789Sahrens if (txg > spa_last_synced_txg(spa) || 1104789Sahrens txg > spa_freeze_txg(spa)) 1105789Sahrens lwb = zil_lwb_commit(zilog, itx, lwb); 1106789Sahrens kmem_free(itx, offsetof(itx_t, itx_lr) 1107789Sahrens + itx->itx_lr.lrc_reclen); 1108789Sahrens mutex_enter(&zilog->zl_lock); 1109789Sahrens } 11102638Sperrin DTRACE_PROBE1(zil__cw2, zilog_t *, zilog); 11113063Sperrin /* determine commit sequence number */ 11123063Sperrin itx = list_head(&zilog->zl_itx_list); 11133063Sperrin if (itx) 11143063Sperrin commit_seq = itx->itx_lr.lrc_seq; 11153063Sperrin else 11163063Sperrin commit_seq = zilog->zl_itx_seq; 1117789Sahrens mutex_exit(&zilog->zl_lock); 1118789Sahrens 1119789Sahrens /* write the last block out */ 11203063Sperrin if (lwb != NULL && lwb->lwb_zio != NULL) 1121789Sahrens lwb = zil_lwb_write_start(zilog, lwb); 1122789Sahrens 11231141Sperrin zilog->zl_prev_used = zilog->zl_cur_used; 11241141Sperrin zilog->zl_cur_used = 0; 11251141Sperrin 11262638Sperrin /* 11272638Sperrin * Wait if necessary for the log blocks to be on stable storage. 11282638Sperrin */ 11292638Sperrin if (zilog->zl_root_zio) { 11302638Sperrin DTRACE_PROBE1(zil__cw3, zilog_t *, zilog); 11312638Sperrin (void) zio_wait(zilog->zl_root_zio); 11327754SJeff.Bonwick@Sun.COM zilog->zl_root_zio = NULL; 11332638Sperrin DTRACE_PROBE1(zil__cw4, zilog_t *, zilog); 11345688Sbonwick zil_flush_vdevs(zilog); 1135789Sahrens } 11361141Sperrin 1137789Sahrens if (zilog->zl_log_error || lwb == NULL) { 1138789Sahrens zilog->zl_log_error = 0; 1139789Sahrens txg_wait_synced(zilog->zl_dmu_pool, 0); 1140789Sahrens } 11413063Sperrin 11423063Sperrin mutex_enter(&zilog->zl_lock); 11431141Sperrin zilog->zl_writer = B_FALSE; 11443063Sperrin 11453063Sperrin ASSERT3U(commit_seq, >=, zilog->zl_commit_seq); 11463063Sperrin zilog->zl_commit_seq = commit_seq; 11472638Sperrin } 11482638Sperrin 11492638Sperrin /* 11502638Sperrin * Push zfs transactions to stable storage up to the supplied sequence number. 11512638Sperrin * If foid is 0 push out all transactions, otherwise push only those 11522638Sperrin * for that file or might have been used to create that file. 11532638Sperrin */ 11542638Sperrin void 11552638Sperrin zil_commit(zilog_t *zilog, uint64_t seq, uint64_t foid) 11562638Sperrin { 11572638Sperrin if (zilog == NULL || seq == 0) 11582638Sperrin return; 11592638Sperrin 11602638Sperrin mutex_enter(&zilog->zl_lock); 11612638Sperrin 11622638Sperrin seq = MIN(seq, zilog->zl_itx_seq); /* cap seq at largest itx seq */ 11632638Sperrin 11643063Sperrin while (zilog->zl_writer) { 11652638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 11663063Sperrin if (seq < zilog->zl_commit_seq) { 11673063Sperrin mutex_exit(&zilog->zl_lock); 11683063Sperrin return; 11693063Sperrin } 11703063Sperrin } 11712638Sperrin zil_commit_writer(zilog, seq, foid); /* drops zl_lock */ 11723063Sperrin /* wake up others waiting on the commit */ 11733063Sperrin cv_broadcast(&zilog->zl_cv_writer); 11743063Sperrin mutex_exit(&zilog->zl_lock); 1175789Sahrens } 1176789Sahrens 1177789Sahrens /* 1178789Sahrens * Called in syncing context to free committed log blocks and update log header. 1179789Sahrens */ 1180789Sahrens void 1181789Sahrens zil_sync(zilog_t *zilog, dmu_tx_t *tx) 1182789Sahrens { 11831807Sbonwick zil_header_t *zh = zil_header_in_syncing_context(zilog); 1184789Sahrens uint64_t txg = dmu_tx_get_txg(tx); 1185789Sahrens spa_t *spa = zilog->zl_spa; 1186789Sahrens lwb_t *lwb; 1187789Sahrens 11881807Sbonwick mutex_enter(&zilog->zl_lock); 11891807Sbonwick 1190789Sahrens ASSERT(zilog->zl_stop_sync == 0); 1191789Sahrens 11928227SNeil.Perrin@Sun.COM zh->zh_replay_seq = zilog->zl_replayed_seq[txg & TXG_MASK]; 1193789Sahrens 1194789Sahrens if (zilog->zl_destroy_txg == txg) { 11951807Sbonwick blkptr_t blk = zh->zh_log; 11961807Sbonwick 11971807Sbonwick ASSERT(list_head(&zilog->zl_lwb_list) == NULL); 11981807Sbonwick ASSERT(spa_sync_pass(spa) == 1); 11991807Sbonwick 12001807Sbonwick bzero(zh, sizeof (zil_header_t)); 12018227SNeil.Perrin@Sun.COM bzero(zilog->zl_replayed_seq, sizeof (zilog->zl_replayed_seq)); 12021807Sbonwick 12031807Sbonwick if (zilog->zl_keep_first) { 12041807Sbonwick /* 12051807Sbonwick * If this block was part of log chain that couldn't 12061807Sbonwick * be claimed because a device was missing during 12071807Sbonwick * zil_claim(), but that device later returns, 12081807Sbonwick * then this block could erroneously appear valid. 12091807Sbonwick * To guard against this, assign a new GUID to the new 12101807Sbonwick * log chain so it doesn't matter what blk points to. 12111807Sbonwick */ 12121807Sbonwick zil_init_log_chain(zilog, &blk); 12131807Sbonwick zh->zh_log = blk; 12141807Sbonwick } 1215789Sahrens } 1216789Sahrens 1217789Sahrens for (;;) { 1218789Sahrens lwb = list_head(&zilog->zl_lwb_list); 1219789Sahrens if (lwb == NULL) { 1220789Sahrens mutex_exit(&zilog->zl_lock); 1221789Sahrens return; 1222789Sahrens } 12232638Sperrin zh->zh_log = lwb->lwb_blk; 1224789Sahrens if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg) 1225789Sahrens break; 1226789Sahrens list_remove(&zilog->zl_lwb_list, lwb); 1227789Sahrens zio_free_blk(spa, &lwb->lwb_blk, txg); 1228789Sahrens kmem_cache_free(zil_lwb_cache, lwb); 12293668Sgw25295 12303668Sgw25295 /* 12313668Sgw25295 * If we don't have anything left in the lwb list then 12323668Sgw25295 * we've had an allocation failure and we need to zero 12333668Sgw25295 * out the zil_header blkptr so that we don't end 12343668Sgw25295 * up freeing the same block twice. 12353668Sgw25295 */ 12363668Sgw25295 if (list_head(&zilog->zl_lwb_list) == NULL) 12373668Sgw25295 BP_ZERO(&zh->zh_log); 1238789Sahrens } 1239789Sahrens mutex_exit(&zilog->zl_lock); 1240789Sahrens } 1241789Sahrens 1242789Sahrens void 1243789Sahrens zil_init(void) 1244789Sahrens { 1245789Sahrens zil_lwb_cache = kmem_cache_create("zil_lwb_cache", 12462856Snd150628 sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0); 1247789Sahrens } 1248789Sahrens 1249789Sahrens void 1250789Sahrens zil_fini(void) 1251789Sahrens { 1252789Sahrens kmem_cache_destroy(zil_lwb_cache); 1253789Sahrens } 1254789Sahrens 1255789Sahrens zilog_t * 1256789Sahrens zil_alloc(objset_t *os, zil_header_t *zh_phys) 1257789Sahrens { 1258789Sahrens zilog_t *zilog; 1259789Sahrens 1260789Sahrens zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP); 1261789Sahrens 1262789Sahrens zilog->zl_header = zh_phys; 1263789Sahrens zilog->zl_os = os; 1264789Sahrens zilog->zl_spa = dmu_objset_spa(os); 1265789Sahrens zilog->zl_dmu_pool = dmu_objset_pool(os); 12661807Sbonwick zilog->zl_destroy_txg = TXG_INITIAL - 1; 1267789Sahrens 12682856Snd150628 mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL); 12692856Snd150628 1270789Sahrens list_create(&zilog->zl_itx_list, sizeof (itx_t), 1271789Sahrens offsetof(itx_t, itx_node)); 1272789Sahrens 1273789Sahrens list_create(&zilog->zl_lwb_list, sizeof (lwb_t), 1274789Sahrens offsetof(lwb_t, lwb_node)); 1275789Sahrens 12765688Sbonwick mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL); 12775688Sbonwick 12785688Sbonwick avl_create(&zilog->zl_vdev_tree, zil_vdev_compare, 12795688Sbonwick sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node)); 1280789Sahrens 12815913Sperrin cv_init(&zilog->zl_cv_writer, NULL, CV_DEFAULT, NULL); 12825913Sperrin cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL); 12835913Sperrin 1284789Sahrens return (zilog); 1285789Sahrens } 1286789Sahrens 1287789Sahrens void 1288789Sahrens zil_free(zilog_t *zilog) 1289789Sahrens { 1290789Sahrens lwb_t *lwb; 1291789Sahrens 1292789Sahrens zilog->zl_stop_sync = 1; 1293789Sahrens 1294789Sahrens while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 1295789Sahrens list_remove(&zilog->zl_lwb_list, lwb); 1296789Sahrens if (lwb->lwb_buf != NULL) 1297789Sahrens zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 1298789Sahrens kmem_cache_free(zil_lwb_cache, lwb); 1299789Sahrens } 1300789Sahrens list_destroy(&zilog->zl_lwb_list); 1301789Sahrens 13025688Sbonwick avl_destroy(&zilog->zl_vdev_tree); 13035688Sbonwick mutex_destroy(&zilog->zl_vdev_lock); 1304789Sahrens 1305789Sahrens ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1306789Sahrens list_destroy(&zilog->zl_itx_list); 13072856Snd150628 mutex_destroy(&zilog->zl_lock); 1308789Sahrens 13095913Sperrin cv_destroy(&zilog->zl_cv_writer); 13105913Sperrin cv_destroy(&zilog->zl_cv_suspend); 13115913Sperrin 1312789Sahrens kmem_free(zilog, sizeof (zilog_t)); 1313789Sahrens } 1314789Sahrens 1315789Sahrens /* 13161646Sperrin * return true if the initial log block is not valid 13171362Sperrin */ 13187522SNeil.Perrin@Sun.COM static boolean_t 13191362Sperrin zil_empty(zilog_t *zilog) 13201362Sperrin { 13211807Sbonwick const zil_header_t *zh = zilog->zl_header; 13221807Sbonwick arc_buf_t *abuf = NULL; 13231362Sperrin 13241807Sbonwick if (BP_IS_HOLE(&zh->zh_log)) 13257522SNeil.Perrin@Sun.COM return (B_TRUE); 13261362Sperrin 13271807Sbonwick if (zil_read_log_block(zilog, &zh->zh_log, &abuf) != 0) 13287522SNeil.Perrin@Sun.COM return (B_TRUE); 13291807Sbonwick 13301807Sbonwick VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 13317522SNeil.Perrin@Sun.COM return (B_FALSE); 13321362Sperrin } 13331362Sperrin 13341362Sperrin /* 1335789Sahrens * Open an intent log. 1336789Sahrens */ 1337789Sahrens zilog_t * 1338789Sahrens zil_open(objset_t *os, zil_get_data_t *get_data) 1339789Sahrens { 1340789Sahrens zilog_t *zilog = dmu_objset_zil(os); 1341789Sahrens 1342789Sahrens zilog->zl_get_data = get_data; 1343789Sahrens zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri, 1344789Sahrens 2, 2, TASKQ_PREPOPULATE); 1345789Sahrens 1346789Sahrens return (zilog); 1347789Sahrens } 1348789Sahrens 1349789Sahrens /* 1350789Sahrens * Close an intent log. 1351789Sahrens */ 1352789Sahrens void 1353789Sahrens zil_close(zilog_t *zilog) 1354789Sahrens { 13551807Sbonwick /* 13561807Sbonwick * If the log isn't already committed, mark the objset dirty 13571807Sbonwick * (so zil_sync() will be called) and wait for that txg to sync. 13581807Sbonwick */ 13591807Sbonwick if (!zil_is_committed(zilog)) { 13601807Sbonwick uint64_t txg; 13611807Sbonwick dmu_tx_t *tx = dmu_tx_create(zilog->zl_os); 13621807Sbonwick (void) dmu_tx_assign(tx, TXG_WAIT); 13631807Sbonwick dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 13641807Sbonwick txg = dmu_tx_get_txg(tx); 13651807Sbonwick dmu_tx_commit(tx); 13661807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, txg); 13671807Sbonwick } 13681807Sbonwick 1369789Sahrens taskq_destroy(zilog->zl_clean_taskq); 1370789Sahrens zilog->zl_clean_taskq = NULL; 1371789Sahrens zilog->zl_get_data = NULL; 1372789Sahrens 1373789Sahrens zil_itx_clean(zilog); 1374789Sahrens ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1375789Sahrens } 1376789Sahrens 1377789Sahrens /* 1378789Sahrens * Suspend an intent log. While in suspended mode, we still honor 1379789Sahrens * synchronous semantics, but we rely on txg_wait_synced() to do it. 1380789Sahrens * We suspend the log briefly when taking a snapshot so that the snapshot 1381789Sahrens * contains all the data it's supposed to, and has an empty intent log. 1382789Sahrens */ 1383789Sahrens int 1384789Sahrens zil_suspend(zilog_t *zilog) 1385789Sahrens { 13861807Sbonwick const zil_header_t *zh = zilog->zl_header; 1387789Sahrens 1388789Sahrens mutex_enter(&zilog->zl_lock); 13891807Sbonwick if (zh->zh_claim_txg != 0) { /* unplayed log */ 1390789Sahrens mutex_exit(&zilog->zl_lock); 1391789Sahrens return (EBUSY); 1392789Sahrens } 13931807Sbonwick if (zilog->zl_suspend++ != 0) { 13941807Sbonwick /* 13951807Sbonwick * Someone else already began a suspend. 13961807Sbonwick * Just wait for them to finish. 13971807Sbonwick */ 13981807Sbonwick while (zilog->zl_suspending) 13991807Sbonwick cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock); 14001807Sbonwick mutex_exit(&zilog->zl_lock); 14011807Sbonwick return (0); 14021807Sbonwick } 14031807Sbonwick zilog->zl_suspending = B_TRUE; 1404789Sahrens mutex_exit(&zilog->zl_lock); 1405789Sahrens 14062638Sperrin zil_commit(zilog, UINT64_MAX, 0); 1407789Sahrens 14082638Sperrin /* 14092638Sperrin * Wait for any in-flight log writes to complete. 14102638Sperrin */ 1411789Sahrens mutex_enter(&zilog->zl_lock); 14122638Sperrin while (zilog->zl_writer) 14132638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 1414789Sahrens mutex_exit(&zilog->zl_lock); 1415789Sahrens 14161807Sbonwick zil_destroy(zilog, B_FALSE); 14171807Sbonwick 14181807Sbonwick mutex_enter(&zilog->zl_lock); 14191807Sbonwick zilog->zl_suspending = B_FALSE; 14201807Sbonwick cv_broadcast(&zilog->zl_cv_suspend); 14211807Sbonwick mutex_exit(&zilog->zl_lock); 1422789Sahrens 1423789Sahrens return (0); 1424789Sahrens } 1425789Sahrens 1426789Sahrens void 1427789Sahrens zil_resume(zilog_t *zilog) 1428789Sahrens { 1429789Sahrens mutex_enter(&zilog->zl_lock); 1430789Sahrens ASSERT(zilog->zl_suspend != 0); 1431789Sahrens zilog->zl_suspend--; 1432789Sahrens mutex_exit(&zilog->zl_lock); 1433789Sahrens } 1434789Sahrens 1435789Sahrens typedef struct zil_replay_arg { 1436789Sahrens objset_t *zr_os; 1437789Sahrens zil_replay_func_t **zr_replay; 1438789Sahrens void *zr_arg; 1439789Sahrens boolean_t zr_byteswap; 1440789Sahrens char *zr_lrbuf; 1441789Sahrens } zil_replay_arg_t; 1442789Sahrens 1443789Sahrens static void 1444789Sahrens zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg) 1445789Sahrens { 1446789Sahrens zil_replay_arg_t *zr = zra; 14471807Sbonwick const zil_header_t *zh = zilog->zl_header; 1448789Sahrens uint64_t reclen = lr->lrc_reclen; 1449789Sahrens uint64_t txtype = lr->lrc_txtype; 14503063Sperrin char *name; 14518227SNeil.Perrin@Sun.COM int pass, error; 1452789Sahrens 14538227SNeil.Perrin@Sun.COM if (!zilog->zl_replay) /* giving up */ 1454789Sahrens return; 1455789Sahrens 1456789Sahrens if (lr->lrc_txg < claim_txg) /* already committed */ 1457789Sahrens return; 1458789Sahrens 1459789Sahrens if (lr->lrc_seq <= zh->zh_replay_seq) /* already replayed */ 1460789Sahrens return; 1461789Sahrens 14625331Samw /* Strip case-insensitive bit, still present in log record */ 14635331Samw txtype &= ~TX_CI; 14645331Samw 14658227SNeil.Perrin@Sun.COM if (txtype == 0 || txtype >= TX_MAX_TYPE) { 14668227SNeil.Perrin@Sun.COM error = EINVAL; 14678227SNeil.Perrin@Sun.COM goto bad; 14688227SNeil.Perrin@Sun.COM } 14698227SNeil.Perrin@Sun.COM 1470789Sahrens /* 1471789Sahrens * Make a copy of the data so we can revise and extend it. 1472789Sahrens */ 1473789Sahrens bcopy(lr, zr->zr_lrbuf, reclen); 1474789Sahrens 1475789Sahrens /* 1476789Sahrens * The log block containing this lr may have been byteswapped 1477789Sahrens * so that we can easily examine common fields like lrc_txtype. 1478789Sahrens * However, the log is a mix of different data types, and only the 1479789Sahrens * replay vectors know how to byteswap their records. Therefore, if 1480789Sahrens * the lr was byteswapped, undo it before invoking the replay vector. 1481789Sahrens */ 1482789Sahrens if (zr->zr_byteswap) 1483789Sahrens byteswap_uint64_array(zr->zr_lrbuf, reclen); 1484789Sahrens 1485789Sahrens /* 1486789Sahrens * If this is a TX_WRITE with a blkptr, suck in the data. 1487789Sahrens */ 1488789Sahrens if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) { 1489789Sahrens lr_write_t *lrw = (lr_write_t *)lr; 1490789Sahrens blkptr_t *wbp = &lrw->lr_blkptr; 1491789Sahrens uint64_t wlen = lrw->lr_length; 1492789Sahrens char *wbuf = zr->zr_lrbuf + reclen; 1493789Sahrens 1494789Sahrens if (BP_IS_HOLE(wbp)) { /* compressed to a hole */ 1495789Sahrens bzero(wbuf, wlen); 1496789Sahrens } else { 1497789Sahrens /* 1498789Sahrens * A subsequent write may have overwritten this block, 1499789Sahrens * in which case wbp may have been been freed and 1500789Sahrens * reallocated, and our read of wbp may fail with a 1501789Sahrens * checksum error. We can safely ignore this because 1502789Sahrens * the later write will provide the correct data. 1503789Sahrens */ 15041544Seschrock zbookmark_t zb; 15051544Seschrock 15061544Seschrock zb.zb_objset = dmu_objset_id(zilog->zl_os); 15071544Seschrock zb.zb_object = lrw->lr_foid; 15081544Seschrock zb.zb_level = -1; 15091544Seschrock zb.zb_blkid = lrw->lr_offset / BP_GET_LSIZE(wbp); 15101544Seschrock 1511789Sahrens (void) zio_wait(zio_read(NULL, zilog->zl_spa, 1512789Sahrens wbp, wbuf, BP_GET_LSIZE(wbp), NULL, NULL, 1513789Sahrens ZIO_PRIORITY_SYNC_READ, 15141544Seschrock ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb)); 1515789Sahrens (void) memmove(wbuf, wbuf + lrw->lr_blkoff, wlen); 1516789Sahrens } 1517789Sahrens } 1518789Sahrens 1519789Sahrens /* 1520789Sahrens * We must now do two things atomically: replay this log record, 15218227SNeil.Perrin@Sun.COM * and update the log header sequence number to reflect the fact that 15228227SNeil.Perrin@Sun.COM * we did so. At the end of each replay function the sequence number 15238227SNeil.Perrin@Sun.COM * is updated if we are in replay mode. 1524789Sahrens */ 15258227SNeil.Perrin@Sun.COM for (pass = 1; pass <= 2; pass++) { 15268227SNeil.Perrin@Sun.COM zilog->zl_replaying_seq = lr->lrc_seq; 15278227SNeil.Perrin@Sun.COM /* Only byteswap (if needed) on the 1st pass. */ 15288227SNeil.Perrin@Sun.COM error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lrbuf, 15298227SNeil.Perrin@Sun.COM zr->zr_byteswap && pass == 1); 1530789Sahrens 15313063Sperrin if (!error) 15323063Sperrin return; 15333063Sperrin 15343063Sperrin /* 15353063Sperrin * The DMU's dnode layer doesn't see removes until the txg 15363063Sperrin * commits, so a subsequent claim can spuriously fail with 15378227SNeil.Perrin@Sun.COM * EEXIST. So if we receive any error we try syncing out 15388227SNeil.Perrin@Sun.COM * any removes then retry the transaction. 15393063Sperrin */ 15408227SNeil.Perrin@Sun.COM if (pass == 1) 15413063Sperrin txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0); 1542789Sahrens } 1543789Sahrens 15447904SNeil.Perrin@Sun.COM bad: 15458227SNeil.Perrin@Sun.COM ASSERT(error); 15463063Sperrin name = kmem_alloc(MAXNAMELEN, KM_SLEEP); 15473063Sperrin dmu_objset_name(zr->zr_os, name); 15483063Sperrin cmn_err(CE_WARN, "ZFS replay transaction error %d, " 15495331Samw "dataset %s, seq 0x%llx, txtype %llu %s\n", 15505331Samw error, name, (u_longlong_t)lr->lrc_seq, (u_longlong_t)txtype, 15515331Samw (lr->lrc_txtype & TX_CI) ? "CI" : ""); 15528227SNeil.Perrin@Sun.COM zilog->zl_replay = B_FALSE; 15533063Sperrin kmem_free(name, MAXNAMELEN); 15543063Sperrin } 1555789Sahrens 15563063Sperrin /* ARGSUSED */ 15573063Sperrin static void 15583063Sperrin zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg) 15593063Sperrin { 15603063Sperrin zilog->zl_replay_blks++; 1561789Sahrens } 1562789Sahrens 1563789Sahrens /* 15641362Sperrin * If this dataset has a non-empty intent log, replay it and destroy it. 1565789Sahrens */ 1566789Sahrens void 15678227SNeil.Perrin@Sun.COM zil_replay(objset_t *os, void *arg, zil_replay_func_t *replay_func[TX_MAX_TYPE]) 1568789Sahrens { 1569789Sahrens zilog_t *zilog = dmu_objset_zil(os); 15701807Sbonwick const zil_header_t *zh = zilog->zl_header; 15711807Sbonwick zil_replay_arg_t zr; 15721362Sperrin 15731362Sperrin if (zil_empty(zilog)) { 15741807Sbonwick zil_destroy(zilog, B_TRUE); 15751362Sperrin return; 15761362Sperrin } 1577789Sahrens 1578789Sahrens zr.zr_os = os; 1579789Sahrens zr.zr_replay = replay_func; 1580789Sahrens zr.zr_arg = arg; 15811807Sbonwick zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log); 1582789Sahrens zr.zr_lrbuf = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP); 1583789Sahrens 1584789Sahrens /* 1585789Sahrens * Wait for in-progress removes to sync before starting replay. 1586789Sahrens */ 1587789Sahrens txg_wait_synced(zilog->zl_dmu_pool, 0); 1588789Sahrens 15898227SNeil.Perrin@Sun.COM zilog->zl_replay = B_TRUE; 15903063Sperrin zilog->zl_replay_time = lbolt; 15913063Sperrin ASSERT(zilog->zl_replay_blks == 0); 15923063Sperrin (void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr, 15931807Sbonwick zh->zh_claim_txg); 1594789Sahrens kmem_free(zr.zr_lrbuf, 2 * SPA_MAXBLOCKSIZE); 1595789Sahrens 15961807Sbonwick zil_destroy(zilog, B_FALSE); 15975712Sahrens txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 15988227SNeil.Perrin@Sun.COM zilog->zl_replay = B_FALSE; 1599789Sahrens } 16001646Sperrin 16011646Sperrin /* 16021646Sperrin * Report whether all transactions are committed 16031646Sperrin */ 16041646Sperrin int 16051646Sperrin zil_is_committed(zilog_t *zilog) 16061646Sperrin { 16071646Sperrin lwb_t *lwb; 16082638Sperrin int ret; 16091646Sperrin 16102638Sperrin mutex_enter(&zilog->zl_lock); 16112638Sperrin while (zilog->zl_writer) 16122638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 16132638Sperrin 16142638Sperrin /* recent unpushed intent log transactions? */ 16152638Sperrin if (!list_is_empty(&zilog->zl_itx_list)) { 16162638Sperrin ret = B_FALSE; 16172638Sperrin goto out; 16182638Sperrin } 16192638Sperrin 16202638Sperrin /* intent log never used? */ 16212638Sperrin lwb = list_head(&zilog->zl_lwb_list); 16222638Sperrin if (lwb == NULL) { 16232638Sperrin ret = B_TRUE; 16242638Sperrin goto out; 16252638Sperrin } 16261646Sperrin 16271646Sperrin /* 16282638Sperrin * more than 1 log buffer means zil_sync() hasn't yet freed 16292638Sperrin * entries after a txg has committed 16301646Sperrin */ 16312638Sperrin if (list_next(&zilog->zl_lwb_list, lwb)) { 16322638Sperrin ret = B_FALSE; 16332638Sperrin goto out; 16342638Sperrin } 16352638Sperrin 16361646Sperrin ASSERT(zil_empty(zilog)); 16372638Sperrin ret = B_TRUE; 16382638Sperrin out: 16392638Sperrin cv_broadcast(&zilog->zl_cv_writer); 16402638Sperrin mutex_exit(&zilog->zl_lock); 16412638Sperrin return (ret); 16421646Sperrin } 1643