1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51472Sperrin * Common Development and Distribution License (the "License"). 61472Sperrin * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 225809Sperrin * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27789Sahrens 28789Sahrens #include <sys/zfs_context.h> 29789Sahrens #include <sys/spa.h> 30789Sahrens #include <sys/dmu.h> 31789Sahrens #include <sys/zap.h> 32789Sahrens #include <sys/arc.h> 33789Sahrens #include <sys/stat.h> 34789Sahrens #include <sys/resource.h> 35789Sahrens #include <sys/zil.h> 36789Sahrens #include <sys/zil_impl.h> 37789Sahrens #include <sys/dsl_dataset.h> 38789Sahrens #include <sys/vdev.h> 393668Sgw25295 #include <sys/dmu_tx.h> 40789Sahrens 41789Sahrens /* 42789Sahrens * The zfs intent log (ZIL) saves transaction records of system calls 43789Sahrens * that change the file system in memory with enough information 44789Sahrens * to be able to replay them. These are stored in memory until 45789Sahrens * either the DMU transaction group (txg) commits them to the stable pool 46789Sahrens * and they can be discarded, or they are flushed to the stable log 47789Sahrens * (also in the pool) due to a fsync, O_DSYNC or other synchronous 48789Sahrens * requirement. In the event of a panic or power fail then those log 49789Sahrens * records (transactions) are replayed. 50789Sahrens * 51789Sahrens * There is one ZIL per file system. Its on-disk (pool) format consists 52789Sahrens * of 3 parts: 53789Sahrens * 54789Sahrens * - ZIL header 55789Sahrens * - ZIL blocks 56789Sahrens * - ZIL records 57789Sahrens * 58789Sahrens * A log record holds a system call transaction. Log blocks can 59789Sahrens * hold many log records and the blocks are chained together. 60789Sahrens * Each ZIL block contains a block pointer (blkptr_t) to the next 61789Sahrens * ZIL block in the chain. The ZIL header points to the first 62789Sahrens * block in the chain. Note there is not a fixed place in the pool 63789Sahrens * to hold blocks. They are dynamically allocated and freed as 64789Sahrens * needed from the blocks available. Figure X shows the ZIL structure: 65789Sahrens */ 66789Sahrens 67789Sahrens /* 682986Sek110237 * This global ZIL switch affects all pools 69789Sahrens */ 70789Sahrens int zil_disable = 0; /* disable intent logging */ 712986Sek110237 722986Sek110237 /* 732986Sek110237 * Tunable parameter for debugging or performance analysis. Setting 742986Sek110237 * zfs_nocacheflush will cause corruption on power loss if a volatile 752986Sek110237 * out-of-order write cache is enabled. 762986Sek110237 */ 772986Sek110237 boolean_t zfs_nocacheflush = B_FALSE; 78789Sahrens 79789Sahrens static kmem_cache_t *zil_lwb_cache; 80789Sahrens 81789Sahrens static int 82789Sahrens zil_dva_compare(const void *x1, const void *x2) 83789Sahrens { 84789Sahrens const dva_t *dva1 = x1; 85789Sahrens const dva_t *dva2 = x2; 86789Sahrens 87789Sahrens if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2)) 88789Sahrens return (-1); 89789Sahrens if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2)) 90789Sahrens return (1); 91789Sahrens 92789Sahrens if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2)) 93789Sahrens return (-1); 94789Sahrens if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2)) 95789Sahrens return (1); 96789Sahrens 97789Sahrens return (0); 98789Sahrens } 99789Sahrens 100789Sahrens static void 101789Sahrens zil_dva_tree_init(avl_tree_t *t) 102789Sahrens { 103789Sahrens avl_create(t, zil_dva_compare, sizeof (zil_dva_node_t), 104789Sahrens offsetof(zil_dva_node_t, zn_node)); 105789Sahrens } 106789Sahrens 107789Sahrens static void 108789Sahrens zil_dva_tree_fini(avl_tree_t *t) 109789Sahrens { 110789Sahrens zil_dva_node_t *zn; 111789Sahrens void *cookie = NULL; 112789Sahrens 113789Sahrens while ((zn = avl_destroy_nodes(t, &cookie)) != NULL) 114789Sahrens kmem_free(zn, sizeof (zil_dva_node_t)); 115789Sahrens 116789Sahrens avl_destroy(t); 117789Sahrens } 118789Sahrens 119789Sahrens static int 120789Sahrens zil_dva_tree_add(avl_tree_t *t, dva_t *dva) 121789Sahrens { 122789Sahrens zil_dva_node_t *zn; 123789Sahrens avl_index_t where; 124789Sahrens 125789Sahrens if (avl_find(t, dva, &where) != NULL) 126789Sahrens return (EEXIST); 127789Sahrens 128789Sahrens zn = kmem_alloc(sizeof (zil_dva_node_t), KM_SLEEP); 129789Sahrens zn->zn_dva = *dva; 130789Sahrens avl_insert(t, zn, where); 131789Sahrens 132789Sahrens return (0); 133789Sahrens } 134789Sahrens 1351807Sbonwick static zil_header_t * 1361807Sbonwick zil_header_in_syncing_context(zilog_t *zilog) 1371807Sbonwick { 1381807Sbonwick return ((zil_header_t *)zilog->zl_header); 1391807Sbonwick } 1401807Sbonwick 1411807Sbonwick static void 1421807Sbonwick zil_init_log_chain(zilog_t *zilog, blkptr_t *bp) 1431807Sbonwick { 1441807Sbonwick zio_cksum_t *zc = &bp->blk_cksum; 1451807Sbonwick 1461807Sbonwick zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL); 1471807Sbonwick zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL); 1481807Sbonwick zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os); 1491807Sbonwick zc->zc_word[ZIL_ZC_SEQ] = 1ULL; 1501807Sbonwick } 1511807Sbonwick 152789Sahrens /* 153789Sahrens * Read a log block, make sure it's valid, and byteswap it if necessary. 154789Sahrens */ 155789Sahrens static int 1561807Sbonwick zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, arc_buf_t **abufpp) 157789Sahrens { 1581807Sbonwick blkptr_t blk = *bp; 1591544Seschrock zbookmark_t zb; 1602391Smaybee uint32_t aflags = ARC_WAIT; 161789Sahrens int error; 162789Sahrens 1631807Sbonwick zb.zb_objset = bp->blk_cksum.zc_word[ZIL_ZC_OBJSET]; 1641544Seschrock zb.zb_object = 0; 1651544Seschrock zb.zb_level = -1; 1661807Sbonwick zb.zb_blkid = bp->blk_cksum.zc_word[ZIL_ZC_SEQ]; 1671807Sbonwick 1681807Sbonwick *abufpp = NULL; 1691807Sbonwick 1707046Sahrens /* 1717046Sahrens * We shouldn't be doing any scrubbing while we're doing log 1727046Sahrens * replay, it's OK to not lock. 1737046Sahrens */ 1747046Sahrens error = arc_read_nolock(NULL, zilog->zl_spa, &blk, 1751807Sbonwick arc_getbuf_func, abufpp, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL | 1762391Smaybee ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB, &aflags, &zb); 1771807Sbonwick 1781807Sbonwick if (error == 0) { 1791807Sbonwick char *data = (*abufpp)->b_data; 1801807Sbonwick uint64_t blksz = BP_GET_LSIZE(bp); 1811807Sbonwick zil_trailer_t *ztp = (zil_trailer_t *)(data + blksz) - 1; 1821807Sbonwick zio_cksum_t cksum = bp->blk_cksum; 1831544Seschrock 1841807Sbonwick /* 1851807Sbonwick * Sequence numbers should be... sequential. The checksum 1861807Sbonwick * verifier for the next block should be bp's checksum plus 1. 1871807Sbonwick */ 1881807Sbonwick cksum.zc_word[ZIL_ZC_SEQ]++; 1891807Sbonwick 1901807Sbonwick if (bcmp(&cksum, &ztp->zit_next_blk.blk_cksum, sizeof (cksum))) 1911807Sbonwick error = ESTALE; 1921807Sbonwick else if (BP_IS_HOLE(&ztp->zit_next_blk)) 1931807Sbonwick error = ENOENT; 1941807Sbonwick else if (ztp->zit_nused > (blksz - sizeof (zil_trailer_t))) 1951807Sbonwick error = EOVERFLOW; 1961807Sbonwick 1971807Sbonwick if (error) { 1981807Sbonwick VERIFY(arc_buf_remove_ref(*abufpp, abufpp) == 1); 1991807Sbonwick *abufpp = NULL; 2001807Sbonwick } 201789Sahrens } 202789Sahrens 2031807Sbonwick dprintf("error %d on %llu:%llu\n", error, zb.zb_objset, zb.zb_blkid); 204789Sahrens 2051807Sbonwick return (error); 206789Sahrens } 207789Sahrens 208789Sahrens /* 209789Sahrens * Parse the intent log, and call parse_func for each valid record within. 2101807Sbonwick * Return the highest sequence number. 211789Sahrens */ 2121807Sbonwick uint64_t 213789Sahrens zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func, 214789Sahrens zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg) 215789Sahrens { 2161807Sbonwick const zil_header_t *zh = zilog->zl_header; 2171807Sbonwick uint64_t claim_seq = zh->zh_claim_seq; 2181807Sbonwick uint64_t seq = 0; 2191807Sbonwick uint64_t max_seq = 0; 2201807Sbonwick blkptr_t blk = zh->zh_log; 2211807Sbonwick arc_buf_t *abuf; 222789Sahrens char *lrbuf, *lrp; 223789Sahrens zil_trailer_t *ztp; 224789Sahrens int reclen, error; 225789Sahrens 226789Sahrens if (BP_IS_HOLE(&blk)) 2271807Sbonwick return (max_seq); 228789Sahrens 229789Sahrens /* 230789Sahrens * Starting at the block pointed to by zh_log we read the log chain. 231789Sahrens * For each block in the chain we strongly check that block to 232789Sahrens * ensure its validity. We stop when an invalid block is found. 233789Sahrens * For each block pointer in the chain we call parse_blk_func(). 234789Sahrens * For each record in each valid block we call parse_lr_func(). 2351807Sbonwick * If the log has been claimed, stop if we encounter a sequence 2361807Sbonwick * number greater than the highest claimed sequence number. 237789Sahrens */ 238789Sahrens zil_dva_tree_init(&zilog->zl_dva_tree); 239789Sahrens for (;;) { 2401807Sbonwick seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 2411807Sbonwick 2421807Sbonwick if (claim_seq != 0 && seq > claim_seq) 2431807Sbonwick break; 2441807Sbonwick 2451807Sbonwick ASSERT(max_seq < seq); 2461807Sbonwick max_seq = seq; 2471807Sbonwick 2481807Sbonwick error = zil_read_log_block(zilog, &blk, &abuf); 249789Sahrens 250789Sahrens if (parse_blk_func != NULL) 251789Sahrens parse_blk_func(zilog, &blk, arg, txg); 252789Sahrens 253789Sahrens if (error) 254789Sahrens break; 255789Sahrens 2561807Sbonwick lrbuf = abuf->b_data; 257789Sahrens ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1; 258789Sahrens blk = ztp->zit_next_blk; 259789Sahrens 2601807Sbonwick if (parse_lr_func == NULL) { 2611807Sbonwick VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 262789Sahrens continue; 2631807Sbonwick } 264789Sahrens 265789Sahrens for (lrp = lrbuf; lrp < lrbuf + ztp->zit_nused; lrp += reclen) { 266789Sahrens lr_t *lr = (lr_t *)lrp; 267789Sahrens reclen = lr->lrc_reclen; 268789Sahrens ASSERT3U(reclen, >=, sizeof (lr_t)); 269789Sahrens parse_lr_func(zilog, lr, arg, txg); 270789Sahrens } 2711807Sbonwick VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 272789Sahrens } 273789Sahrens zil_dva_tree_fini(&zilog->zl_dva_tree); 2741807Sbonwick 2751807Sbonwick return (max_seq); 276789Sahrens } 277789Sahrens 278789Sahrens /* ARGSUSED */ 279789Sahrens static void 280789Sahrens zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg) 281789Sahrens { 282789Sahrens spa_t *spa = zilog->zl_spa; 283789Sahrens int err; 284789Sahrens 285789Sahrens /* 286789Sahrens * Claim log block if not already committed and not already claimed. 287789Sahrens */ 288789Sahrens if (bp->blk_birth >= first_txg && 289789Sahrens zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp)) == 0) { 290789Sahrens err = zio_wait(zio_claim(NULL, spa, first_txg, bp, NULL, NULL)); 291789Sahrens ASSERT(err == 0); 292789Sahrens } 293789Sahrens } 294789Sahrens 295789Sahrens static void 296789Sahrens zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg) 297789Sahrens { 298789Sahrens if (lrc->lrc_txtype == TX_WRITE) { 299789Sahrens lr_write_t *lr = (lr_write_t *)lrc; 300789Sahrens zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg); 301789Sahrens } 302789Sahrens } 303789Sahrens 304789Sahrens /* ARGSUSED */ 305789Sahrens static void 306789Sahrens zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg) 307789Sahrens { 308789Sahrens zio_free_blk(zilog->zl_spa, bp, dmu_tx_get_txg(tx)); 309789Sahrens } 310789Sahrens 311789Sahrens static void 312789Sahrens zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg) 313789Sahrens { 314789Sahrens /* 315789Sahrens * If we previously claimed it, we need to free it. 316789Sahrens */ 317789Sahrens if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE) { 318789Sahrens lr_write_t *lr = (lr_write_t *)lrc; 319789Sahrens blkptr_t *bp = &lr->lr_blkptr; 320789Sahrens if (bp->blk_birth >= claim_txg && 321789Sahrens !zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp))) { 322789Sahrens (void) arc_free(NULL, zilog->zl_spa, 323789Sahrens dmu_tx_get_txg(tx), bp, NULL, NULL, ARC_WAIT); 324789Sahrens } 325789Sahrens } 326789Sahrens } 327789Sahrens 328789Sahrens /* 329789Sahrens * Create an on-disk intent log. 330789Sahrens */ 331789Sahrens static void 332789Sahrens zil_create(zilog_t *zilog) 333789Sahrens { 3341807Sbonwick const zil_header_t *zh = zilog->zl_header; 335789Sahrens lwb_t *lwb; 3361807Sbonwick uint64_t txg = 0; 3371807Sbonwick dmu_tx_t *tx = NULL; 338789Sahrens blkptr_t blk; 3391807Sbonwick int error = 0; 340789Sahrens 341789Sahrens /* 3421807Sbonwick * Wait for any previous destroy to complete. 343789Sahrens */ 3441807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 3451807Sbonwick 3461807Sbonwick ASSERT(zh->zh_claim_txg == 0); 3471807Sbonwick ASSERT(zh->zh_replay_seq == 0); 3481807Sbonwick 3491807Sbonwick blk = zh->zh_log; 350789Sahrens 351789Sahrens /* 3521807Sbonwick * If we don't already have an initial log block, allocate one now. 353789Sahrens */ 3541807Sbonwick if (BP_IS_HOLE(&blk)) { 3551807Sbonwick tx = dmu_tx_create(zilog->zl_os); 3561807Sbonwick (void) dmu_tx_assign(tx, TXG_WAIT); 3571807Sbonwick dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 3581807Sbonwick txg = dmu_tx_get_txg(tx); 3591807Sbonwick 3603063Sperrin error = zio_alloc_blk(zilog->zl_spa, ZIL_MIN_BLKSZ, &blk, 3613063Sperrin NULL, txg); 3621807Sbonwick 3631807Sbonwick if (error == 0) 3641807Sbonwick zil_init_log_chain(zilog, &blk); 3651362Sperrin } 3661807Sbonwick 3671807Sbonwick /* 3681807Sbonwick * Allocate a log write buffer (lwb) for the first log block. 3691807Sbonwick */ 370789Sahrens if (error == 0) { 371789Sahrens lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 372789Sahrens lwb->lwb_zilog = zilog; 373789Sahrens lwb->lwb_blk = blk; 374789Sahrens lwb->lwb_nused = 0; 375789Sahrens lwb->lwb_sz = BP_GET_LSIZE(&lwb->lwb_blk); 376789Sahrens lwb->lwb_buf = zio_buf_alloc(lwb->lwb_sz); 377789Sahrens lwb->lwb_max_txg = txg; 3782237Smaybee lwb->lwb_zio = NULL; 3792237Smaybee 380789Sahrens mutex_enter(&zilog->zl_lock); 381789Sahrens list_insert_tail(&zilog->zl_lwb_list, lwb); 382789Sahrens mutex_exit(&zilog->zl_lock); 383789Sahrens } 384789Sahrens 3851807Sbonwick /* 3861807Sbonwick * If we just allocated the first log block, commit our transaction 3871807Sbonwick * and wait for zil_sync() to stuff the block poiner into zh_log. 3881807Sbonwick * (zh is part of the MOS, so we cannot modify it in open context.) 3891807Sbonwick */ 3901807Sbonwick if (tx != NULL) { 3911807Sbonwick dmu_tx_commit(tx); 3921362Sperrin txg_wait_synced(zilog->zl_dmu_pool, txg); 3931807Sbonwick } 3941807Sbonwick 3951807Sbonwick ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0); 396789Sahrens } 397789Sahrens 398789Sahrens /* 399789Sahrens * In one tx, free all log blocks and clear the log header. 4001807Sbonwick * If keep_first is set, then we're replaying a log with no content. 4011807Sbonwick * We want to keep the first block, however, so that the first 4021807Sbonwick * synchronous transaction doesn't require a txg_wait_synced() 4031807Sbonwick * in zil_create(). We don't need to txg_wait_synced() here either 4041807Sbonwick * when keep_first is set, because both zil_create() and zil_destroy() 4051807Sbonwick * will wait for any in-progress destroys to complete. 406789Sahrens */ 407789Sahrens void 4081807Sbonwick zil_destroy(zilog_t *zilog, boolean_t keep_first) 409789Sahrens { 4101807Sbonwick const zil_header_t *zh = zilog->zl_header; 4111807Sbonwick lwb_t *lwb; 412789Sahrens dmu_tx_t *tx; 413789Sahrens uint64_t txg; 414789Sahrens 4151807Sbonwick /* 4161807Sbonwick * Wait for any previous destroy to complete. 4171807Sbonwick */ 4181807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 419789Sahrens 4201807Sbonwick if (BP_IS_HOLE(&zh->zh_log)) 421789Sahrens return; 422789Sahrens 423789Sahrens tx = dmu_tx_create(zilog->zl_os); 424789Sahrens (void) dmu_tx_assign(tx, TXG_WAIT); 425789Sahrens dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 426789Sahrens txg = dmu_tx_get_txg(tx); 427789Sahrens 4281807Sbonwick mutex_enter(&zilog->zl_lock); 4291807Sbonwick 4305223Sperrin /* 4315223Sperrin * It is possible for the ZIL to get the previously mounted zilog 4325223Sperrin * structure of the same dataset if quickly remounted and the dbuf 4335223Sperrin * eviction has not completed. In this case we can see a non 4345223Sperrin * empty lwb list and keep_first will be set. We fix this by 4355223Sperrin * clearing the keep_first. This will be slower but it's very rare. 4365223Sperrin */ 4375223Sperrin if (!list_is_empty(&zilog->zl_lwb_list) && keep_first) 4385223Sperrin keep_first = B_FALSE; 4395223Sperrin 4401807Sbonwick ASSERT3U(zilog->zl_destroy_txg, <, txg); 441789Sahrens zilog->zl_destroy_txg = txg; 4421807Sbonwick zilog->zl_keep_first = keep_first; 4431807Sbonwick 4441807Sbonwick if (!list_is_empty(&zilog->zl_lwb_list)) { 4451807Sbonwick ASSERT(zh->zh_claim_txg == 0); 4461807Sbonwick ASSERT(!keep_first); 4471807Sbonwick while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 4481807Sbonwick list_remove(&zilog->zl_lwb_list, lwb); 4491807Sbonwick if (lwb->lwb_buf != NULL) 4501807Sbonwick zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 4511807Sbonwick zio_free_blk(zilog->zl_spa, &lwb->lwb_blk, txg); 4521807Sbonwick kmem_cache_free(zil_lwb_cache, lwb); 4531807Sbonwick } 4541807Sbonwick } else { 4551807Sbonwick if (!keep_first) { 4561807Sbonwick (void) zil_parse(zilog, zil_free_log_block, 4571807Sbonwick zil_free_log_record, tx, zh->zh_claim_txg); 4581807Sbonwick } 4591807Sbonwick } 4602638Sperrin mutex_exit(&zilog->zl_lock); 461789Sahrens 462789Sahrens dmu_tx_commit(tx); 463789Sahrens } 464789Sahrens 4654935Sperrin /* 4664935Sperrin * zil_rollback_destroy() is only called by the rollback code. 4674935Sperrin * We already have a syncing tx. Rollback has exclusive access to the 4684935Sperrin * dataset, so we don't have to worry about concurrent zil access. 4694935Sperrin * The actual freeing of any log blocks occurs in zil_sync() later in 4704935Sperrin * this txg syncing phase. 4714935Sperrin */ 4724935Sperrin void 4734935Sperrin zil_rollback_destroy(zilog_t *zilog, dmu_tx_t *tx) 4744935Sperrin { 4754935Sperrin const zil_header_t *zh = zilog->zl_header; 4764935Sperrin uint64_t txg; 4774935Sperrin 4784935Sperrin if (BP_IS_HOLE(&zh->zh_log)) 4794935Sperrin return; 4804935Sperrin 4814935Sperrin txg = dmu_tx_get_txg(tx); 4824935Sperrin ASSERT3U(zilog->zl_destroy_txg, <, txg); 4834935Sperrin zilog->zl_destroy_txg = txg; 4844935Sperrin zilog->zl_keep_first = B_FALSE; 4854935Sperrin 4865809Sperrin /* 4875809Sperrin * Ensure there's no outstanding ZIL IO. No lwbs or just the 4885809Sperrin * unused one that allocated in advance is ok. 4895809Sperrin */ 4905809Sperrin ASSERT(zilog->zl_lwb_list.list_head.list_next == 4915809Sperrin zilog->zl_lwb_list.list_head.list_prev); 4924935Sperrin (void) zil_parse(zilog, zil_free_log_block, zil_free_log_record, 4934935Sperrin tx, zh->zh_claim_txg); 4944935Sperrin } 4954935Sperrin 4962199Sahrens int 497789Sahrens zil_claim(char *osname, void *txarg) 498789Sahrens { 499789Sahrens dmu_tx_t *tx = txarg; 500789Sahrens uint64_t first_txg = dmu_tx_get_txg(tx); 501789Sahrens zilog_t *zilog; 502789Sahrens zil_header_t *zh; 503789Sahrens objset_t *os; 504789Sahrens int error; 505789Sahrens 5066689Smaybee error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_USER, &os); 507789Sahrens if (error) { 508*7294Sperrin cmn_err(CE_WARN, "can't open objset for %s", osname); 5092199Sahrens return (0); 510789Sahrens } 511789Sahrens 512789Sahrens zilog = dmu_objset_zil(os); 5131807Sbonwick zh = zil_header_in_syncing_context(zilog); 514789Sahrens 515789Sahrens /* 5161807Sbonwick * Claim all log blocks if we haven't already done so, and remember 5171807Sbonwick * the highest claimed sequence number. This ensures that if we can 5181807Sbonwick * read only part of the log now (e.g. due to a missing device), 5191807Sbonwick * but we can read the entire log later, we will not try to replay 5201807Sbonwick * or destroy beyond the last block we successfully claimed. 521789Sahrens */ 522789Sahrens ASSERT3U(zh->zh_claim_txg, <=, first_txg); 523789Sahrens if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) { 524789Sahrens zh->zh_claim_txg = first_txg; 5251807Sbonwick zh->zh_claim_seq = zil_parse(zilog, zil_claim_log_block, 5261807Sbonwick zil_claim_log_record, tx, first_txg); 527789Sahrens dsl_dataset_dirty(dmu_objset_ds(os), tx); 528789Sahrens } 5291807Sbonwick 530789Sahrens ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1)); 531789Sahrens dmu_objset_close(os); 5322199Sahrens return (0); 533789Sahrens } 534789Sahrens 535*7294Sperrin /* 536*7294Sperrin * Check the log by walking the log chain. 537*7294Sperrin * Checksum errors are ok as they indicate the end of the chain. 538*7294Sperrin * Any other error (no device or read failure) returns an error. 539*7294Sperrin */ 540*7294Sperrin /* ARGSUSED */ 541*7294Sperrin int 542*7294Sperrin zil_check_log_chain(char *osname, void *txarg) 543*7294Sperrin { 544*7294Sperrin zilog_t *zilog; 545*7294Sperrin zil_header_t *zh; 546*7294Sperrin blkptr_t blk; 547*7294Sperrin arc_buf_t *abuf; 548*7294Sperrin objset_t *os; 549*7294Sperrin char *lrbuf; 550*7294Sperrin zil_trailer_t *ztp; 551*7294Sperrin int error; 552*7294Sperrin 553*7294Sperrin error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_USER, &os); 554*7294Sperrin if (error) { 555*7294Sperrin cmn_err(CE_WARN, "can't open objset for %s", osname); 556*7294Sperrin return (0); 557*7294Sperrin } 558*7294Sperrin 559*7294Sperrin zilog = dmu_objset_zil(os); 560*7294Sperrin zh = zil_header_in_syncing_context(zilog); 561*7294Sperrin blk = zh->zh_log; 562*7294Sperrin if (BP_IS_HOLE(&blk)) { 563*7294Sperrin dmu_objset_close(os); 564*7294Sperrin return (0); /* no chain */ 565*7294Sperrin } 566*7294Sperrin 567*7294Sperrin for (;;) { 568*7294Sperrin error = zil_read_log_block(zilog, &blk, &abuf); 569*7294Sperrin if (error) 570*7294Sperrin break; 571*7294Sperrin lrbuf = abuf->b_data; 572*7294Sperrin ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1; 573*7294Sperrin blk = ztp->zit_next_blk; 574*7294Sperrin VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 575*7294Sperrin } 576*7294Sperrin dmu_objset_close(os); 577*7294Sperrin if (error == ECKSUM) 578*7294Sperrin return (0); /* normal end of chain */ 579*7294Sperrin return (error); 580*7294Sperrin } 581*7294Sperrin 582*7294Sperrin /* 583*7294Sperrin * Clear a log chain 584*7294Sperrin */ 585*7294Sperrin /* ARGSUSED */ 586*7294Sperrin int 587*7294Sperrin zil_clear_log_chain(char *osname, void *txarg) 588*7294Sperrin { 589*7294Sperrin zilog_t *zilog; 590*7294Sperrin zil_header_t *zh; 591*7294Sperrin objset_t *os; 592*7294Sperrin dmu_tx_t *tx; 593*7294Sperrin int error; 594*7294Sperrin 595*7294Sperrin error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_USER, &os); 596*7294Sperrin if (error) { 597*7294Sperrin cmn_err(CE_WARN, "can't open objset for %s", osname); 598*7294Sperrin return (0); 599*7294Sperrin } 600*7294Sperrin 601*7294Sperrin zilog = dmu_objset_zil(os); 602*7294Sperrin tx = dmu_tx_create(zilog->zl_os); 603*7294Sperrin (void) dmu_tx_assign(tx, TXG_WAIT); 604*7294Sperrin zh = zil_header_in_syncing_context(zilog); 605*7294Sperrin BP_ZERO(&zh->zh_log); 606*7294Sperrin dsl_dataset_dirty(dmu_objset_ds(os), tx); 607*7294Sperrin dmu_tx_commit(tx); 608*7294Sperrin dmu_objset_close(os); 609*7294Sperrin return (0); 610*7294Sperrin } 611*7294Sperrin 6125688Sbonwick static int 6135688Sbonwick zil_vdev_compare(const void *x1, const void *x2) 614789Sahrens { 6155875Sperrin uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev; 6165875Sperrin uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev; 6175688Sbonwick 6185688Sbonwick if (v1 < v2) 6195688Sbonwick return (-1); 6205688Sbonwick if (v1 > v2) 6215688Sbonwick return (1); 6225688Sbonwick 6235688Sbonwick return (0); 6245688Sbonwick } 6255688Sbonwick 6265688Sbonwick void 6275688Sbonwick zil_add_block(zilog_t *zilog, blkptr_t *bp) 6285688Sbonwick { 6295688Sbonwick avl_tree_t *t = &zilog->zl_vdev_tree; 6305688Sbonwick avl_index_t where; 6315688Sbonwick zil_vdev_node_t *zv, zvsearch; 6325688Sbonwick int ndvas = BP_GET_NDVAS(bp); 6335688Sbonwick int i; 634789Sahrens 6352986Sek110237 if (zfs_nocacheflush) 636789Sahrens return; 637789Sahrens 6385688Sbonwick ASSERT(zilog->zl_writer); 6395688Sbonwick 6405688Sbonwick /* 6415688Sbonwick * Even though we're zl_writer, we still need a lock because the 6425688Sbonwick * zl_get_data() callbacks may have dmu_sync() done callbacks 6435688Sbonwick * that will run concurrently. 6445688Sbonwick */ 6455688Sbonwick mutex_enter(&zilog->zl_vdev_lock); 6465688Sbonwick for (i = 0; i < ndvas; i++) { 6475688Sbonwick zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]); 6485688Sbonwick if (avl_find(t, &zvsearch, &where) == NULL) { 6495688Sbonwick zv = kmem_alloc(sizeof (*zv), KM_SLEEP); 6505688Sbonwick zv->zv_vdev = zvsearch.zv_vdev; 6515688Sbonwick avl_insert(t, zv, where); 6523063Sperrin } 6533063Sperrin } 6545688Sbonwick mutex_exit(&zilog->zl_vdev_lock); 6553063Sperrin } 6563063Sperrin 657789Sahrens void 6582638Sperrin zil_flush_vdevs(zilog_t *zilog) 659789Sahrens { 6603063Sperrin spa_t *spa = zilog->zl_spa; 6615688Sbonwick avl_tree_t *t = &zilog->zl_vdev_tree; 6625688Sbonwick void *cookie = NULL; 6635688Sbonwick zil_vdev_node_t *zv; 6645688Sbonwick zio_t *zio; 6653063Sperrin 6663063Sperrin ASSERT(zilog->zl_writer); 667789Sahrens 6685688Sbonwick /* 6695688Sbonwick * We don't need zl_vdev_lock here because we're the zl_writer, 6705688Sbonwick * and all zl_get_data() callbacks are done. 6715688Sbonwick */ 6725688Sbonwick if (avl_numnodes(t) == 0) 6735688Sbonwick return; 6745688Sbonwick 6755688Sbonwick spa_config_enter(spa, RW_READER, FTAG); 6765688Sbonwick 6775688Sbonwick zio = zio_root(spa, NULL, NULL, 6785688Sbonwick ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL); 6795688Sbonwick 6805688Sbonwick while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) { 6815688Sbonwick vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev); 6825688Sbonwick if (vd != NULL) 6835688Sbonwick zio_flush(zio, vd); 6845688Sbonwick kmem_free(zv, sizeof (*zv)); 6853063Sperrin } 686789Sahrens 687789Sahrens /* 688789Sahrens * Wait for all the flushes to complete. Not all devices actually 689789Sahrens * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails. 690789Sahrens */ 6915688Sbonwick (void) zio_wait(zio); 6925688Sbonwick 6935688Sbonwick spa_config_exit(spa, FTAG); 694789Sahrens } 695789Sahrens 696789Sahrens /* 697789Sahrens * Function called when a log block write completes 698789Sahrens */ 699789Sahrens static void 700789Sahrens zil_lwb_write_done(zio_t *zio) 701789Sahrens { 702789Sahrens lwb_t *lwb = zio->io_private; 703789Sahrens zilog_t *zilog = lwb->lwb_zilog; 704789Sahrens 705789Sahrens /* 706789Sahrens * Now that we've written this log block, we have a stable pointer 707789Sahrens * to the next block in the chain, so it's OK to let the txg in 708789Sahrens * which we allocated the next block sync. 709789Sahrens */ 710789Sahrens txg_rele_to_sync(&lwb->lwb_txgh); 711789Sahrens 712789Sahrens zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 713789Sahrens mutex_enter(&zilog->zl_lock); 714789Sahrens lwb->lwb_buf = NULL; 7154527Sperrin if (zio->io_error) 716789Sahrens zilog->zl_log_error = B_TRUE; 717789Sahrens mutex_exit(&zilog->zl_lock); 718789Sahrens } 719789Sahrens 720789Sahrens /* 7212237Smaybee * Initialize the io for a log block. 7222237Smaybee * 7232237Smaybee * Note, we should not initialize the IO until we are about 7242237Smaybee * to use it, since zio_rewrite() does a spa_config_enter(). 7252237Smaybee */ 7262237Smaybee static void 7272237Smaybee zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb) 7282237Smaybee { 7292237Smaybee zbookmark_t zb; 7302237Smaybee 7312237Smaybee zb.zb_objset = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET]; 7322237Smaybee zb.zb_object = 0; 7332237Smaybee zb.zb_level = -1; 7342237Smaybee zb.zb_blkid = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 7352237Smaybee 7362638Sperrin if (zilog->zl_root_zio == NULL) { 7372638Sperrin zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL, 7382638Sperrin ZIO_FLAG_CANFAIL); 7392638Sperrin } 7403063Sperrin if (lwb->lwb_zio == NULL) { 7413063Sperrin lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa, 7427181Sperrin ZIO_CHECKSUM_ZILOG, 0, &lwb->lwb_blk, lwb->lwb_buf, 7433063Sperrin lwb->lwb_sz, zil_lwb_write_done, lwb, 7444527Sperrin ZIO_PRIORITY_LOG_WRITE, ZIO_FLAG_CANFAIL, &zb); 7453063Sperrin } 7462237Smaybee } 7472237Smaybee 7482237Smaybee /* 749789Sahrens * Start a log block write and advance to the next log block. 750789Sahrens * Calls are serialized. 751789Sahrens */ 752789Sahrens static lwb_t * 753789Sahrens zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb) 754789Sahrens { 755789Sahrens lwb_t *nlwb; 756789Sahrens zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1; 7571807Sbonwick spa_t *spa = zilog->zl_spa; 7581807Sbonwick blkptr_t *bp = &ztp->zit_next_blk; 759789Sahrens uint64_t txg; 760789Sahrens uint64_t zil_blksz; 761789Sahrens int error; 762789Sahrens 763789Sahrens ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb)); 764789Sahrens 765789Sahrens /* 766789Sahrens * Allocate the next block and save its address in this block 767789Sahrens * before writing it in order to establish the log chain. 768789Sahrens * Note that if the allocation of nlwb synced before we wrote 769789Sahrens * the block that points at it (lwb), we'd leak it if we crashed. 770789Sahrens * Therefore, we don't do txg_rele_to_sync() until zil_lwb_write_done(). 771789Sahrens */ 772789Sahrens txg = txg_hold_open(zilog->zl_dmu_pool, &lwb->lwb_txgh); 773789Sahrens txg_rele_to_quiesce(&lwb->lwb_txgh); 774789Sahrens 775789Sahrens /* 7761141Sperrin * Pick a ZIL blocksize. We request a size that is the 7771141Sperrin * maximum of the previous used size, the current used size and 7781141Sperrin * the amount waiting in the queue. 779789Sahrens */ 7802237Smaybee zil_blksz = MAX(zilog->zl_prev_used, 7812237Smaybee zilog->zl_cur_used + sizeof (*ztp)); 7821141Sperrin zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp)); 7831842Sperrin zil_blksz = P2ROUNDUP_TYPED(zil_blksz, ZIL_MIN_BLKSZ, uint64_t); 7841141Sperrin if (zil_blksz > ZIL_MAX_BLKSZ) 7851141Sperrin zil_blksz = ZIL_MAX_BLKSZ; 786789Sahrens 7873063Sperrin BP_ZERO(bp); 7883063Sperrin /* pass the old blkptr in order to spread log blocks across devs */ 7893063Sperrin error = zio_alloc_blk(spa, zil_blksz, bp, &lwb->lwb_blk, txg); 790789Sahrens if (error) { 7913668Sgw25295 dmu_tx_t *tx = dmu_tx_create_assigned(zilog->zl_dmu_pool, txg); 7923668Sgw25295 7931544Seschrock /* 7943668Sgw25295 * We dirty the dataset to ensure that zil_sync() will 7953668Sgw25295 * be called to remove this lwb from our zl_lwb_list. 7963668Sgw25295 * Failing to do so, may leave an lwb with a NULL lwb_buf 7973668Sgw25295 * hanging around on the zl_lwb_list. 7983668Sgw25295 */ 7993668Sgw25295 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 8003848Sgw25295 dmu_tx_commit(tx); 8013668Sgw25295 8023668Sgw25295 /* 8033668Sgw25295 * Since we've just experienced an allocation failure so we 8043668Sgw25295 * terminate the current lwb and send it on its way. 8053668Sgw25295 */ 8063668Sgw25295 ztp->zit_pad = 0; 8073668Sgw25295 ztp->zit_nused = lwb->lwb_nused; 8083668Sgw25295 ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum; 8093668Sgw25295 zio_nowait(lwb->lwb_zio); 8103668Sgw25295 8113668Sgw25295 /* 8121544Seschrock * By returning NULL the caller will call tx_wait_synced() 8131544Seschrock */ 814789Sahrens return (NULL); 815789Sahrens } 816789Sahrens 8171807Sbonwick ASSERT3U(bp->blk_birth, ==, txg); 8181544Seschrock ztp->zit_pad = 0; 819789Sahrens ztp->zit_nused = lwb->lwb_nused; 820789Sahrens ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum; 8211807Sbonwick bp->blk_cksum = lwb->lwb_blk.blk_cksum; 8221807Sbonwick bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++; 823789Sahrens 824789Sahrens /* 825789Sahrens * Allocate a new log write buffer (lwb). 826789Sahrens */ 827789Sahrens nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 828789Sahrens 829789Sahrens nlwb->lwb_zilog = zilog; 8301807Sbonwick nlwb->lwb_blk = *bp; 831789Sahrens nlwb->lwb_nused = 0; 832789Sahrens nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk); 833789Sahrens nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz); 834789Sahrens nlwb->lwb_max_txg = txg; 8352237Smaybee nlwb->lwb_zio = NULL; 836789Sahrens 837789Sahrens /* 8383063Sperrin * Put new lwb at the end of the log chain 839789Sahrens */ 840789Sahrens mutex_enter(&zilog->zl_lock); 841789Sahrens list_insert_tail(&zilog->zl_lwb_list, nlwb); 8423063Sperrin mutex_exit(&zilog->zl_lock); 8433063Sperrin 8445688Sbonwick /* Record the block for later vdev flushing */ 8455688Sbonwick zil_add_block(zilog, &lwb->lwb_blk); 846789Sahrens 847789Sahrens /* 8482237Smaybee * kick off the write for the old log block 849789Sahrens */ 8502237Smaybee dprintf_bp(&lwb->lwb_blk, "lwb %p txg %llu: ", lwb, txg); 8513063Sperrin ASSERT(lwb->lwb_zio); 8522237Smaybee zio_nowait(lwb->lwb_zio); 853789Sahrens 854789Sahrens return (nlwb); 855789Sahrens } 856789Sahrens 857789Sahrens static lwb_t * 858789Sahrens zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb) 859789Sahrens { 860789Sahrens lr_t *lrc = &itx->itx_lr; /* common log record */ 8612237Smaybee lr_write_t *lr = (lr_write_t *)lrc; 862789Sahrens uint64_t txg = lrc->lrc_txg; 863789Sahrens uint64_t reclen = lrc->lrc_reclen; 8642237Smaybee uint64_t dlen; 865789Sahrens 866789Sahrens if (lwb == NULL) 867789Sahrens return (NULL); 868789Sahrens ASSERT(lwb->lwb_buf != NULL); 869789Sahrens 8702237Smaybee if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY) 8712237Smaybee dlen = P2ROUNDUP_TYPED( 8722237Smaybee lr->lr_length, sizeof (uint64_t), uint64_t); 8732237Smaybee else 8742237Smaybee dlen = 0; 8751669Sperrin 8761669Sperrin zilog->zl_cur_used += (reclen + dlen); 8771669Sperrin 8783063Sperrin zil_lwb_write_init(zilog, lwb); 8793063Sperrin 8801669Sperrin /* 8811669Sperrin * If this record won't fit in the current log block, start a new one. 8821669Sperrin */ 8831669Sperrin if (lwb->lwb_nused + reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) { 8841669Sperrin lwb = zil_lwb_write_start(zilog, lwb); 8852237Smaybee if (lwb == NULL) 8861669Sperrin return (NULL); 8873063Sperrin zil_lwb_write_init(zilog, lwb); 8881669Sperrin ASSERT(lwb->lwb_nused == 0); 8891669Sperrin if (reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) { 8901669Sperrin txg_wait_synced(zilog->zl_dmu_pool, txg); 891789Sahrens return (lwb); 892789Sahrens } 893789Sahrens } 894789Sahrens 8952638Sperrin /* 8962638Sperrin * Update the lrc_seq, to be log record sequence number. See zil.h 8972638Sperrin * Then copy the record to the log buffer. 8982638Sperrin */ 8992638Sperrin lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */ 900789Sahrens bcopy(lrc, lwb->lwb_buf + lwb->lwb_nused, reclen); 9012237Smaybee 9022237Smaybee /* 9032237Smaybee * If it's a write, fetch the data or get its blkptr as appropriate. 9042237Smaybee */ 9052237Smaybee if (lrc->lrc_txtype == TX_WRITE) { 9062237Smaybee if (txg > spa_freeze_txg(zilog->zl_spa)) 9072237Smaybee txg_wait_synced(zilog->zl_dmu_pool, txg); 9082237Smaybee if (itx->itx_wr_state != WR_COPIED) { 9092237Smaybee char *dbuf; 9102237Smaybee int error; 9112237Smaybee 9122237Smaybee /* alignment is guaranteed */ 9132237Smaybee lr = (lr_write_t *)(lwb->lwb_buf + lwb->lwb_nused); 9142237Smaybee if (dlen) { 9152237Smaybee ASSERT(itx->itx_wr_state == WR_NEED_COPY); 9162237Smaybee dbuf = lwb->lwb_buf + lwb->lwb_nused + reclen; 9172237Smaybee lr->lr_common.lrc_reclen += dlen; 9182237Smaybee } else { 9192237Smaybee ASSERT(itx->itx_wr_state == WR_INDIRECT); 9202237Smaybee dbuf = NULL; 9212237Smaybee } 9222237Smaybee error = zilog->zl_get_data( 9232237Smaybee itx->itx_private, lr, dbuf, lwb->lwb_zio); 9242237Smaybee if (error) { 9252237Smaybee ASSERT(error == ENOENT || error == EEXIST || 9262237Smaybee error == EALREADY); 9272237Smaybee return (lwb); 9282237Smaybee } 9292237Smaybee } 9301669Sperrin } 9312237Smaybee 9322237Smaybee lwb->lwb_nused += reclen + dlen; 933789Sahrens lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg); 934789Sahrens ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb)); 935789Sahrens ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0); 936789Sahrens 937789Sahrens return (lwb); 938789Sahrens } 939789Sahrens 940789Sahrens itx_t * 9415331Samw zil_itx_create(uint64_t txtype, size_t lrsize) 942789Sahrens { 943789Sahrens itx_t *itx; 944789Sahrens 9451842Sperrin lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t); 946789Sahrens 947789Sahrens itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP); 948789Sahrens itx->itx_lr.lrc_txtype = txtype; 949789Sahrens itx->itx_lr.lrc_reclen = lrsize; 9506101Sperrin itx->itx_sod = lrsize; /* if write & WR_NEED_COPY will be increased */ 951789Sahrens itx->itx_lr.lrc_seq = 0; /* defensive */ 952789Sahrens 953789Sahrens return (itx); 954789Sahrens } 955789Sahrens 956789Sahrens uint64_t 957789Sahrens zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx) 958789Sahrens { 959789Sahrens uint64_t seq; 960789Sahrens 961789Sahrens ASSERT(itx->itx_lr.lrc_seq == 0); 962789Sahrens 963789Sahrens mutex_enter(&zilog->zl_lock); 964789Sahrens list_insert_tail(&zilog->zl_itx_list, itx); 9656101Sperrin zilog->zl_itx_list_sz += itx->itx_sod; 966789Sahrens itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx); 967789Sahrens itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq; 968789Sahrens mutex_exit(&zilog->zl_lock); 969789Sahrens 970789Sahrens return (seq); 971789Sahrens } 972789Sahrens 973789Sahrens /* 974789Sahrens * Free up all in-memory intent log transactions that have now been synced. 975789Sahrens */ 976789Sahrens static void 977789Sahrens zil_itx_clean(zilog_t *zilog) 978789Sahrens { 979789Sahrens uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa); 980789Sahrens uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa); 9813778Sjohansen list_t clean_list; 982789Sahrens itx_t *itx; 983789Sahrens 9843778Sjohansen list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node)); 9853778Sjohansen 986789Sahrens mutex_enter(&zilog->zl_lock); 9872638Sperrin /* wait for a log writer to finish walking list */ 9882638Sperrin while (zilog->zl_writer) { 9892638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 9902638Sperrin } 9913778Sjohansen 9923778Sjohansen /* 9933778Sjohansen * Move the sync'd log transactions to a separate list so we can call 9943778Sjohansen * kmem_free without holding the zl_lock. 9953778Sjohansen * 9963778Sjohansen * There is no need to set zl_writer as we don't drop zl_lock here 9973778Sjohansen */ 998789Sahrens while ((itx = list_head(&zilog->zl_itx_list)) != NULL && 999789Sahrens itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) { 1000789Sahrens list_remove(&zilog->zl_itx_list, itx); 10016101Sperrin zilog->zl_itx_list_sz -= itx->itx_sod; 10023778Sjohansen list_insert_tail(&clean_list, itx); 10033778Sjohansen } 10043778Sjohansen cv_broadcast(&zilog->zl_cv_writer); 10053778Sjohansen mutex_exit(&zilog->zl_lock); 10063778Sjohansen 10073778Sjohansen /* destroy sync'd log transactions */ 10083778Sjohansen while ((itx = list_head(&clean_list)) != NULL) { 10093778Sjohansen list_remove(&clean_list, itx); 1010789Sahrens kmem_free(itx, offsetof(itx_t, itx_lr) 1011789Sahrens + itx->itx_lr.lrc_reclen); 1012789Sahrens } 10133778Sjohansen list_destroy(&clean_list); 1014789Sahrens } 1015789Sahrens 10162638Sperrin /* 10173063Sperrin * If there are any in-memory intent log transactions which have now been 10183063Sperrin * synced then start up a taskq to free them. 10192638Sperrin */ 1020789Sahrens void 1021789Sahrens zil_clean(zilog_t *zilog) 1022789Sahrens { 10233063Sperrin itx_t *itx; 10243063Sperrin 1025789Sahrens mutex_enter(&zilog->zl_lock); 10263063Sperrin itx = list_head(&zilog->zl_itx_list); 10273063Sperrin if ((itx != NULL) && 10283063Sperrin (itx->itx_lr.lrc_txg <= spa_last_synced_txg(zilog->zl_spa))) { 1029789Sahrens (void) taskq_dispatch(zilog->zl_clean_taskq, 1030789Sahrens (void (*)(void *))zil_itx_clean, zilog, TQ_NOSLEEP); 10313063Sperrin } 1032789Sahrens mutex_exit(&zilog->zl_lock); 1033789Sahrens } 1034789Sahrens 1035789Sahrens void 10362638Sperrin zil_commit_writer(zilog_t *zilog, uint64_t seq, uint64_t foid) 1037789Sahrens { 1038789Sahrens uint64_t txg; 10393063Sperrin uint64_t commit_seq = 0; 10402638Sperrin itx_t *itx, *itx_next = (itx_t *)-1; 1041789Sahrens lwb_t *lwb; 1042789Sahrens spa_t *spa; 1043789Sahrens 10442638Sperrin zilog->zl_writer = B_TRUE; 10452638Sperrin zilog->zl_root_zio = NULL; 1046789Sahrens spa = zilog->zl_spa; 1047789Sahrens 1048789Sahrens if (zilog->zl_suspend) { 1049789Sahrens lwb = NULL; 1050789Sahrens } else { 1051789Sahrens lwb = list_tail(&zilog->zl_lwb_list); 1052789Sahrens if (lwb == NULL) { 10532638Sperrin /* 10542638Sperrin * Return if there's nothing to flush before we 10552638Sperrin * dirty the fs by calling zil_create() 10562638Sperrin */ 10572638Sperrin if (list_is_empty(&zilog->zl_itx_list)) { 10582638Sperrin zilog->zl_writer = B_FALSE; 10592638Sperrin return; 10602638Sperrin } 1061789Sahrens mutex_exit(&zilog->zl_lock); 1062789Sahrens zil_create(zilog); 1063789Sahrens mutex_enter(&zilog->zl_lock); 1064789Sahrens lwb = list_tail(&zilog->zl_lwb_list); 1065789Sahrens } 1066789Sahrens } 1067789Sahrens 10683063Sperrin /* Loop through in-memory log transactions filling log blocks. */ 10692638Sperrin DTRACE_PROBE1(zil__cw1, zilog_t *, zilog); 1070789Sahrens for (;;) { 10712638Sperrin /* 10722638Sperrin * Find the next itx to push: 10732638Sperrin * Push all transactions related to specified foid and all 10742638Sperrin * other transactions except TX_WRITE, TX_TRUNCATE, 10752638Sperrin * TX_SETATTR and TX_ACL for all other files. 10762638Sperrin */ 10772638Sperrin if (itx_next != (itx_t *)-1) 10782638Sperrin itx = itx_next; 10792638Sperrin else 10802638Sperrin itx = list_head(&zilog->zl_itx_list); 10812638Sperrin for (; itx != NULL; itx = list_next(&zilog->zl_itx_list, itx)) { 10822638Sperrin if (foid == 0) /* push all foids? */ 10832638Sperrin break; 10843063Sperrin if (itx->itx_sync) /* push all O_[D]SYNC */ 10853063Sperrin break; 10862638Sperrin switch (itx->itx_lr.lrc_txtype) { 10872638Sperrin case TX_SETATTR: 10882638Sperrin case TX_WRITE: 10892638Sperrin case TX_TRUNCATE: 10902638Sperrin case TX_ACL: 10912638Sperrin /* lr_foid is same offset for these records */ 10922638Sperrin if (((lr_write_t *)&itx->itx_lr)->lr_foid 10932638Sperrin != foid) { 10942638Sperrin continue; /* skip this record */ 10952638Sperrin } 10962638Sperrin } 10972638Sperrin break; 10982638Sperrin } 1099789Sahrens if (itx == NULL) 1100789Sahrens break; 1101789Sahrens 1102789Sahrens if ((itx->itx_lr.lrc_seq > seq) && 11032638Sperrin ((lwb == NULL) || (lwb->lwb_nused == 0) || 11046101Sperrin (lwb->lwb_nused + itx->itx_sod > ZIL_BLK_DATA_SZ(lwb)))) { 1105789Sahrens break; 11063063Sperrin } 1107789Sahrens 11082638Sperrin /* 11092638Sperrin * Save the next pointer. Even though we soon drop 11102638Sperrin * zl_lock all threads that may change the list 11112638Sperrin * (another writer or zil_itx_clean) can't do so until 11122638Sperrin * they have zl_writer. 11132638Sperrin */ 11142638Sperrin itx_next = list_next(&zilog->zl_itx_list, itx); 1115789Sahrens list_remove(&zilog->zl_itx_list, itx); 11166101Sperrin zilog->zl_itx_list_sz -= itx->itx_sod; 11173063Sperrin mutex_exit(&zilog->zl_lock); 1118789Sahrens txg = itx->itx_lr.lrc_txg; 1119789Sahrens ASSERT(txg); 1120789Sahrens 1121789Sahrens if (txg > spa_last_synced_txg(spa) || 1122789Sahrens txg > spa_freeze_txg(spa)) 1123789Sahrens lwb = zil_lwb_commit(zilog, itx, lwb); 1124789Sahrens kmem_free(itx, offsetof(itx_t, itx_lr) 1125789Sahrens + itx->itx_lr.lrc_reclen); 1126789Sahrens mutex_enter(&zilog->zl_lock); 1127789Sahrens } 11282638Sperrin DTRACE_PROBE1(zil__cw2, zilog_t *, zilog); 11293063Sperrin /* determine commit sequence number */ 11303063Sperrin itx = list_head(&zilog->zl_itx_list); 11313063Sperrin if (itx) 11323063Sperrin commit_seq = itx->itx_lr.lrc_seq; 11333063Sperrin else 11343063Sperrin commit_seq = zilog->zl_itx_seq; 1135789Sahrens mutex_exit(&zilog->zl_lock); 1136789Sahrens 1137789Sahrens /* write the last block out */ 11383063Sperrin if (lwb != NULL && lwb->lwb_zio != NULL) 1139789Sahrens lwb = zil_lwb_write_start(zilog, lwb); 1140789Sahrens 11411141Sperrin zilog->zl_prev_used = zilog->zl_cur_used; 11421141Sperrin zilog->zl_cur_used = 0; 11431141Sperrin 11442638Sperrin /* 11452638Sperrin * Wait if necessary for the log blocks to be on stable storage. 11462638Sperrin */ 11472638Sperrin if (zilog->zl_root_zio) { 11482638Sperrin DTRACE_PROBE1(zil__cw3, zilog_t *, zilog); 11492638Sperrin (void) zio_wait(zilog->zl_root_zio); 11502638Sperrin DTRACE_PROBE1(zil__cw4, zilog_t *, zilog); 11515688Sbonwick zil_flush_vdevs(zilog); 1152789Sahrens } 11531141Sperrin 1154789Sahrens if (zilog->zl_log_error || lwb == NULL) { 1155789Sahrens zilog->zl_log_error = 0; 1156789Sahrens txg_wait_synced(zilog->zl_dmu_pool, 0); 1157789Sahrens } 11583063Sperrin 11593063Sperrin mutex_enter(&zilog->zl_lock); 11601141Sperrin zilog->zl_writer = B_FALSE; 11613063Sperrin 11623063Sperrin ASSERT3U(commit_seq, >=, zilog->zl_commit_seq); 11633063Sperrin zilog->zl_commit_seq = commit_seq; 11642638Sperrin } 11652638Sperrin 11662638Sperrin /* 11672638Sperrin * Push zfs transactions to stable storage up to the supplied sequence number. 11682638Sperrin * If foid is 0 push out all transactions, otherwise push only those 11692638Sperrin * for that file or might have been used to create that file. 11702638Sperrin */ 11712638Sperrin void 11722638Sperrin zil_commit(zilog_t *zilog, uint64_t seq, uint64_t foid) 11732638Sperrin { 11742638Sperrin if (zilog == NULL || seq == 0) 11752638Sperrin return; 11762638Sperrin 11772638Sperrin mutex_enter(&zilog->zl_lock); 11782638Sperrin 11792638Sperrin seq = MIN(seq, zilog->zl_itx_seq); /* cap seq at largest itx seq */ 11802638Sperrin 11813063Sperrin while (zilog->zl_writer) { 11822638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 11833063Sperrin if (seq < zilog->zl_commit_seq) { 11843063Sperrin mutex_exit(&zilog->zl_lock); 11853063Sperrin return; 11863063Sperrin } 11873063Sperrin } 11882638Sperrin zil_commit_writer(zilog, seq, foid); /* drops zl_lock */ 11893063Sperrin /* wake up others waiting on the commit */ 11903063Sperrin cv_broadcast(&zilog->zl_cv_writer); 11913063Sperrin mutex_exit(&zilog->zl_lock); 1192789Sahrens } 1193789Sahrens 1194789Sahrens /* 1195789Sahrens * Called in syncing context to free committed log blocks and update log header. 1196789Sahrens */ 1197789Sahrens void 1198789Sahrens zil_sync(zilog_t *zilog, dmu_tx_t *tx) 1199789Sahrens { 12001807Sbonwick zil_header_t *zh = zil_header_in_syncing_context(zilog); 1201789Sahrens uint64_t txg = dmu_tx_get_txg(tx); 1202789Sahrens spa_t *spa = zilog->zl_spa; 1203789Sahrens lwb_t *lwb; 1204789Sahrens 12051807Sbonwick mutex_enter(&zilog->zl_lock); 12061807Sbonwick 1207789Sahrens ASSERT(zilog->zl_stop_sync == 0); 1208789Sahrens 12091807Sbonwick zh->zh_replay_seq = zilog->zl_replay_seq[txg & TXG_MASK]; 1210789Sahrens 1211789Sahrens if (zilog->zl_destroy_txg == txg) { 12121807Sbonwick blkptr_t blk = zh->zh_log; 12131807Sbonwick 12141807Sbonwick ASSERT(list_head(&zilog->zl_lwb_list) == NULL); 12151807Sbonwick ASSERT(spa_sync_pass(spa) == 1); 12161807Sbonwick 12171807Sbonwick bzero(zh, sizeof (zil_header_t)); 1218789Sahrens bzero(zilog->zl_replay_seq, sizeof (zilog->zl_replay_seq)); 12191807Sbonwick 12201807Sbonwick if (zilog->zl_keep_first) { 12211807Sbonwick /* 12221807Sbonwick * If this block was part of log chain that couldn't 12231807Sbonwick * be claimed because a device was missing during 12241807Sbonwick * zil_claim(), but that device later returns, 12251807Sbonwick * then this block could erroneously appear valid. 12261807Sbonwick * To guard against this, assign a new GUID to the new 12271807Sbonwick * log chain so it doesn't matter what blk points to. 12281807Sbonwick */ 12291807Sbonwick zil_init_log_chain(zilog, &blk); 12301807Sbonwick zh->zh_log = blk; 12311807Sbonwick } 1232789Sahrens } 1233789Sahrens 1234789Sahrens for (;;) { 1235789Sahrens lwb = list_head(&zilog->zl_lwb_list); 1236789Sahrens if (lwb == NULL) { 1237789Sahrens mutex_exit(&zilog->zl_lock); 1238789Sahrens return; 1239789Sahrens } 12402638Sperrin zh->zh_log = lwb->lwb_blk; 1241789Sahrens if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg) 1242789Sahrens break; 1243789Sahrens list_remove(&zilog->zl_lwb_list, lwb); 1244789Sahrens zio_free_blk(spa, &lwb->lwb_blk, txg); 1245789Sahrens kmem_cache_free(zil_lwb_cache, lwb); 12463668Sgw25295 12473668Sgw25295 /* 12483668Sgw25295 * If we don't have anything left in the lwb list then 12493668Sgw25295 * we've had an allocation failure and we need to zero 12503668Sgw25295 * out the zil_header blkptr so that we don't end 12513668Sgw25295 * up freeing the same block twice. 12523668Sgw25295 */ 12533668Sgw25295 if (list_head(&zilog->zl_lwb_list) == NULL) 12543668Sgw25295 BP_ZERO(&zh->zh_log); 1255789Sahrens } 1256789Sahrens mutex_exit(&zilog->zl_lock); 1257789Sahrens } 1258789Sahrens 1259789Sahrens void 1260789Sahrens zil_init(void) 1261789Sahrens { 1262789Sahrens zil_lwb_cache = kmem_cache_create("zil_lwb_cache", 12632856Snd150628 sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0); 1264789Sahrens } 1265789Sahrens 1266789Sahrens void 1267789Sahrens zil_fini(void) 1268789Sahrens { 1269789Sahrens kmem_cache_destroy(zil_lwb_cache); 1270789Sahrens } 1271789Sahrens 1272789Sahrens zilog_t * 1273789Sahrens zil_alloc(objset_t *os, zil_header_t *zh_phys) 1274789Sahrens { 1275789Sahrens zilog_t *zilog; 1276789Sahrens 1277789Sahrens zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP); 1278789Sahrens 1279789Sahrens zilog->zl_header = zh_phys; 1280789Sahrens zilog->zl_os = os; 1281789Sahrens zilog->zl_spa = dmu_objset_spa(os); 1282789Sahrens zilog->zl_dmu_pool = dmu_objset_pool(os); 12831807Sbonwick zilog->zl_destroy_txg = TXG_INITIAL - 1; 1284789Sahrens 12852856Snd150628 mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL); 12862856Snd150628 1287789Sahrens list_create(&zilog->zl_itx_list, sizeof (itx_t), 1288789Sahrens offsetof(itx_t, itx_node)); 1289789Sahrens 1290789Sahrens list_create(&zilog->zl_lwb_list, sizeof (lwb_t), 1291789Sahrens offsetof(lwb_t, lwb_node)); 1292789Sahrens 12935688Sbonwick mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL); 12945688Sbonwick 12955688Sbonwick avl_create(&zilog->zl_vdev_tree, zil_vdev_compare, 12965688Sbonwick sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node)); 1297789Sahrens 12985913Sperrin cv_init(&zilog->zl_cv_writer, NULL, CV_DEFAULT, NULL); 12995913Sperrin cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL); 13005913Sperrin 1301789Sahrens return (zilog); 1302789Sahrens } 1303789Sahrens 1304789Sahrens void 1305789Sahrens zil_free(zilog_t *zilog) 1306789Sahrens { 1307789Sahrens lwb_t *lwb; 1308789Sahrens 1309789Sahrens zilog->zl_stop_sync = 1; 1310789Sahrens 1311789Sahrens while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 1312789Sahrens list_remove(&zilog->zl_lwb_list, lwb); 1313789Sahrens if (lwb->lwb_buf != NULL) 1314789Sahrens zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 1315789Sahrens kmem_cache_free(zil_lwb_cache, lwb); 1316789Sahrens } 1317789Sahrens list_destroy(&zilog->zl_lwb_list); 1318789Sahrens 13195688Sbonwick avl_destroy(&zilog->zl_vdev_tree); 13205688Sbonwick mutex_destroy(&zilog->zl_vdev_lock); 1321789Sahrens 1322789Sahrens ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1323789Sahrens list_destroy(&zilog->zl_itx_list); 13242856Snd150628 mutex_destroy(&zilog->zl_lock); 1325789Sahrens 13265913Sperrin cv_destroy(&zilog->zl_cv_writer); 13275913Sperrin cv_destroy(&zilog->zl_cv_suspend); 13285913Sperrin 1329789Sahrens kmem_free(zilog, sizeof (zilog_t)); 1330789Sahrens } 1331789Sahrens 1332789Sahrens /* 13331646Sperrin * return true if the initial log block is not valid 13341362Sperrin */ 13351362Sperrin static int 13361362Sperrin zil_empty(zilog_t *zilog) 13371362Sperrin { 13381807Sbonwick const zil_header_t *zh = zilog->zl_header; 13391807Sbonwick arc_buf_t *abuf = NULL; 13401362Sperrin 13411807Sbonwick if (BP_IS_HOLE(&zh->zh_log)) 13421362Sperrin return (1); 13431362Sperrin 13441807Sbonwick if (zil_read_log_block(zilog, &zh->zh_log, &abuf) != 0) 13451807Sbonwick return (1); 13461807Sbonwick 13471807Sbonwick VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 13481807Sbonwick return (0); 13491362Sperrin } 13501362Sperrin 13511362Sperrin /* 1352789Sahrens * Open an intent log. 1353789Sahrens */ 1354789Sahrens zilog_t * 1355789Sahrens zil_open(objset_t *os, zil_get_data_t *get_data) 1356789Sahrens { 1357789Sahrens zilog_t *zilog = dmu_objset_zil(os); 1358789Sahrens 1359789Sahrens zilog->zl_get_data = get_data; 1360789Sahrens zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri, 1361789Sahrens 2, 2, TASKQ_PREPOPULATE); 1362789Sahrens 1363789Sahrens return (zilog); 1364789Sahrens } 1365789Sahrens 1366789Sahrens /* 1367789Sahrens * Close an intent log. 1368789Sahrens */ 1369789Sahrens void 1370789Sahrens zil_close(zilog_t *zilog) 1371789Sahrens { 13721807Sbonwick /* 13731807Sbonwick * If the log isn't already committed, mark the objset dirty 13741807Sbonwick * (so zil_sync() will be called) and wait for that txg to sync. 13751807Sbonwick */ 13761807Sbonwick if (!zil_is_committed(zilog)) { 13771807Sbonwick uint64_t txg; 13781807Sbonwick dmu_tx_t *tx = dmu_tx_create(zilog->zl_os); 13791807Sbonwick (void) dmu_tx_assign(tx, TXG_WAIT); 13801807Sbonwick dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 13811807Sbonwick txg = dmu_tx_get_txg(tx); 13821807Sbonwick dmu_tx_commit(tx); 13831807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, txg); 13841807Sbonwick } 13851807Sbonwick 1386789Sahrens taskq_destroy(zilog->zl_clean_taskq); 1387789Sahrens zilog->zl_clean_taskq = NULL; 1388789Sahrens zilog->zl_get_data = NULL; 1389789Sahrens 1390789Sahrens zil_itx_clean(zilog); 1391789Sahrens ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1392789Sahrens } 1393789Sahrens 1394789Sahrens /* 1395789Sahrens * Suspend an intent log. While in suspended mode, we still honor 1396789Sahrens * synchronous semantics, but we rely on txg_wait_synced() to do it. 1397789Sahrens * We suspend the log briefly when taking a snapshot so that the snapshot 1398789Sahrens * contains all the data it's supposed to, and has an empty intent log. 1399789Sahrens */ 1400789Sahrens int 1401789Sahrens zil_suspend(zilog_t *zilog) 1402789Sahrens { 14031807Sbonwick const zil_header_t *zh = zilog->zl_header; 1404789Sahrens 1405789Sahrens mutex_enter(&zilog->zl_lock); 14061807Sbonwick if (zh->zh_claim_txg != 0) { /* unplayed log */ 1407789Sahrens mutex_exit(&zilog->zl_lock); 1408789Sahrens return (EBUSY); 1409789Sahrens } 14101807Sbonwick if (zilog->zl_suspend++ != 0) { 14111807Sbonwick /* 14121807Sbonwick * Someone else already began a suspend. 14131807Sbonwick * Just wait for them to finish. 14141807Sbonwick */ 14151807Sbonwick while (zilog->zl_suspending) 14161807Sbonwick cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock); 14171807Sbonwick mutex_exit(&zilog->zl_lock); 14181807Sbonwick return (0); 14191807Sbonwick } 14201807Sbonwick zilog->zl_suspending = B_TRUE; 1421789Sahrens mutex_exit(&zilog->zl_lock); 1422789Sahrens 14232638Sperrin zil_commit(zilog, UINT64_MAX, 0); 1424789Sahrens 14252638Sperrin /* 14262638Sperrin * Wait for any in-flight log writes to complete. 14272638Sperrin */ 1428789Sahrens mutex_enter(&zilog->zl_lock); 14292638Sperrin while (zilog->zl_writer) 14302638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 1431789Sahrens mutex_exit(&zilog->zl_lock); 1432789Sahrens 14331807Sbonwick zil_destroy(zilog, B_FALSE); 14341807Sbonwick 14351807Sbonwick mutex_enter(&zilog->zl_lock); 14361807Sbonwick zilog->zl_suspending = B_FALSE; 14371807Sbonwick cv_broadcast(&zilog->zl_cv_suspend); 14381807Sbonwick mutex_exit(&zilog->zl_lock); 1439789Sahrens 1440789Sahrens return (0); 1441789Sahrens } 1442789Sahrens 1443789Sahrens void 1444789Sahrens zil_resume(zilog_t *zilog) 1445789Sahrens { 1446789Sahrens mutex_enter(&zilog->zl_lock); 1447789Sahrens ASSERT(zilog->zl_suspend != 0); 1448789Sahrens zilog->zl_suspend--; 1449789Sahrens mutex_exit(&zilog->zl_lock); 1450789Sahrens } 1451789Sahrens 1452789Sahrens typedef struct zil_replay_arg { 1453789Sahrens objset_t *zr_os; 1454789Sahrens zil_replay_func_t **zr_replay; 1455789Sahrens void *zr_arg; 1456789Sahrens uint64_t *zr_txgp; 1457789Sahrens boolean_t zr_byteswap; 1458789Sahrens char *zr_lrbuf; 1459789Sahrens } zil_replay_arg_t; 1460789Sahrens 1461789Sahrens static void 1462789Sahrens zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg) 1463789Sahrens { 1464789Sahrens zil_replay_arg_t *zr = zra; 14651807Sbonwick const zil_header_t *zh = zilog->zl_header; 1466789Sahrens uint64_t reclen = lr->lrc_reclen; 1467789Sahrens uint64_t txtype = lr->lrc_txtype; 14683063Sperrin char *name; 14693063Sperrin int pass, error, sunk; 1470789Sahrens 1471789Sahrens if (zilog->zl_stop_replay) 1472789Sahrens return; 1473789Sahrens 1474789Sahrens if (lr->lrc_txg < claim_txg) /* already committed */ 1475789Sahrens return; 1476789Sahrens 1477789Sahrens if (lr->lrc_seq <= zh->zh_replay_seq) /* already replayed */ 1478789Sahrens return; 1479789Sahrens 14805331Samw /* Strip case-insensitive bit, still present in log record */ 14815331Samw txtype &= ~TX_CI; 14825331Samw 1483789Sahrens /* 1484789Sahrens * Make a copy of the data so we can revise and extend it. 1485789Sahrens */ 1486789Sahrens bcopy(lr, zr->zr_lrbuf, reclen); 1487789Sahrens 1488789Sahrens /* 1489789Sahrens * The log block containing this lr may have been byteswapped 1490789Sahrens * so that we can easily examine common fields like lrc_txtype. 1491789Sahrens * However, the log is a mix of different data types, and only the 1492789Sahrens * replay vectors know how to byteswap their records. Therefore, if 1493789Sahrens * the lr was byteswapped, undo it before invoking the replay vector. 1494789Sahrens */ 1495789Sahrens if (zr->zr_byteswap) 1496789Sahrens byteswap_uint64_array(zr->zr_lrbuf, reclen); 1497789Sahrens 1498789Sahrens /* 1499789Sahrens * If this is a TX_WRITE with a blkptr, suck in the data. 1500789Sahrens */ 1501789Sahrens if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) { 1502789Sahrens lr_write_t *lrw = (lr_write_t *)lr; 1503789Sahrens blkptr_t *wbp = &lrw->lr_blkptr; 1504789Sahrens uint64_t wlen = lrw->lr_length; 1505789Sahrens char *wbuf = zr->zr_lrbuf + reclen; 1506789Sahrens 1507789Sahrens if (BP_IS_HOLE(wbp)) { /* compressed to a hole */ 1508789Sahrens bzero(wbuf, wlen); 1509789Sahrens } else { 1510789Sahrens /* 1511789Sahrens * A subsequent write may have overwritten this block, 1512789Sahrens * in which case wbp may have been been freed and 1513789Sahrens * reallocated, and our read of wbp may fail with a 1514789Sahrens * checksum error. We can safely ignore this because 1515789Sahrens * the later write will provide the correct data. 1516789Sahrens */ 15171544Seschrock zbookmark_t zb; 15181544Seschrock 15191544Seschrock zb.zb_objset = dmu_objset_id(zilog->zl_os); 15201544Seschrock zb.zb_object = lrw->lr_foid; 15211544Seschrock zb.zb_level = -1; 15221544Seschrock zb.zb_blkid = lrw->lr_offset / BP_GET_LSIZE(wbp); 15231544Seschrock 1524789Sahrens (void) zio_wait(zio_read(NULL, zilog->zl_spa, 1525789Sahrens wbp, wbuf, BP_GET_LSIZE(wbp), NULL, NULL, 1526789Sahrens ZIO_PRIORITY_SYNC_READ, 15271544Seschrock ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb)); 1528789Sahrens (void) memmove(wbuf, wbuf + lrw->lr_blkoff, wlen); 1529789Sahrens } 1530789Sahrens } 1531789Sahrens 1532789Sahrens /* 1533789Sahrens * We must now do two things atomically: replay this log record, 1534789Sahrens * and update the log header to reflect the fact that we did so. 1535789Sahrens * We use the DMU's ability to assign into a specific txg to do this. 1536789Sahrens */ 15373063Sperrin for (pass = 1, sunk = B_FALSE; /* CONSTANTCONDITION */; pass++) { 1538789Sahrens uint64_t replay_txg; 1539789Sahrens dmu_tx_t *replay_tx; 1540789Sahrens 1541789Sahrens replay_tx = dmu_tx_create(zr->zr_os); 1542789Sahrens error = dmu_tx_assign(replay_tx, TXG_WAIT); 1543789Sahrens if (error) { 1544789Sahrens dmu_tx_abort(replay_tx); 1545789Sahrens break; 1546789Sahrens } 1547789Sahrens 1548789Sahrens replay_txg = dmu_tx_get_txg(replay_tx); 1549789Sahrens 1550789Sahrens if (txtype == 0 || txtype >= TX_MAX_TYPE) { 1551789Sahrens error = EINVAL; 1552789Sahrens } else { 1553789Sahrens /* 1554789Sahrens * On the first pass, arrange for the replay vector 1555789Sahrens * to fail its dmu_tx_assign(). That's the only way 1556789Sahrens * to ensure that those code paths remain well tested. 15575676Sperrin * 15585676Sperrin * Only byteswap (if needed) on the 1st pass. 1559789Sahrens */ 1560789Sahrens *zr->zr_txgp = replay_txg - (pass == 1); 1561789Sahrens error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lrbuf, 15625676Sperrin zr->zr_byteswap && pass == 1); 1563789Sahrens *zr->zr_txgp = TXG_NOWAIT; 1564789Sahrens } 1565789Sahrens 1566789Sahrens if (error == 0) { 1567789Sahrens dsl_dataset_dirty(dmu_objset_ds(zr->zr_os), replay_tx); 1568789Sahrens zilog->zl_replay_seq[replay_txg & TXG_MASK] = 1569789Sahrens lr->lrc_seq; 1570789Sahrens } 1571789Sahrens 1572789Sahrens dmu_tx_commit(replay_tx); 1573789Sahrens 15743063Sperrin if (!error) 15753063Sperrin return; 15763063Sperrin 15773063Sperrin /* 15783063Sperrin * The DMU's dnode layer doesn't see removes until the txg 15793063Sperrin * commits, so a subsequent claim can spuriously fail with 15803063Sperrin * EEXIST. So if we receive any error other than ERESTART 15813063Sperrin * we try syncing out any removes then retrying the 15823063Sperrin * transaction. 15833063Sperrin */ 15843063Sperrin if (error != ERESTART && !sunk) { 15853063Sperrin txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0); 15863063Sperrin sunk = B_TRUE; 15873063Sperrin continue; /* retry */ 15883063Sperrin } 15893063Sperrin 1590789Sahrens if (error != ERESTART) 1591789Sahrens break; 1592789Sahrens 1593789Sahrens if (pass != 1) 1594789Sahrens txg_wait_open(spa_get_dsl(zilog->zl_spa), 1595789Sahrens replay_txg + 1); 1596789Sahrens 1597789Sahrens dprintf("pass %d, retrying\n", pass); 1598789Sahrens } 1599789Sahrens 16003063Sperrin ASSERT(error && error != ERESTART); 16013063Sperrin name = kmem_alloc(MAXNAMELEN, KM_SLEEP); 16023063Sperrin dmu_objset_name(zr->zr_os, name); 16033063Sperrin cmn_err(CE_WARN, "ZFS replay transaction error %d, " 16045331Samw "dataset %s, seq 0x%llx, txtype %llu %s\n", 16055331Samw error, name, (u_longlong_t)lr->lrc_seq, (u_longlong_t)txtype, 16065331Samw (lr->lrc_txtype & TX_CI) ? "CI" : ""); 16073063Sperrin zilog->zl_stop_replay = 1; 16083063Sperrin kmem_free(name, MAXNAMELEN); 16093063Sperrin } 1610789Sahrens 16113063Sperrin /* ARGSUSED */ 16123063Sperrin static void 16133063Sperrin zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg) 16143063Sperrin { 16153063Sperrin zilog->zl_replay_blks++; 1616789Sahrens } 1617789Sahrens 1618789Sahrens /* 16191362Sperrin * If this dataset has a non-empty intent log, replay it and destroy it. 1620789Sahrens */ 1621789Sahrens void 1622789Sahrens zil_replay(objset_t *os, void *arg, uint64_t *txgp, 16233461Sahrens zil_replay_func_t *replay_func[TX_MAX_TYPE]) 1624789Sahrens { 1625789Sahrens zilog_t *zilog = dmu_objset_zil(os); 16261807Sbonwick const zil_header_t *zh = zilog->zl_header; 16271807Sbonwick zil_replay_arg_t zr; 16281362Sperrin 16291362Sperrin if (zil_empty(zilog)) { 16301807Sbonwick zil_destroy(zilog, B_TRUE); 16311362Sperrin return; 16321362Sperrin } 1633789Sahrens 1634789Sahrens zr.zr_os = os; 1635789Sahrens zr.zr_replay = replay_func; 1636789Sahrens zr.zr_arg = arg; 1637789Sahrens zr.zr_txgp = txgp; 16381807Sbonwick zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log); 1639789Sahrens zr.zr_lrbuf = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP); 1640789Sahrens 1641789Sahrens /* 1642789Sahrens * Wait for in-progress removes to sync before starting replay. 1643789Sahrens */ 1644789Sahrens txg_wait_synced(zilog->zl_dmu_pool, 0); 1645789Sahrens 1646789Sahrens zilog->zl_stop_replay = 0; 16473063Sperrin zilog->zl_replay_time = lbolt; 16483063Sperrin ASSERT(zilog->zl_replay_blks == 0); 16493063Sperrin (void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr, 16501807Sbonwick zh->zh_claim_txg); 1651789Sahrens kmem_free(zr.zr_lrbuf, 2 * SPA_MAXBLOCKSIZE); 1652789Sahrens 16531807Sbonwick zil_destroy(zilog, B_FALSE); 16545712Sahrens txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 1655789Sahrens } 16561646Sperrin 16571646Sperrin /* 16581646Sperrin * Report whether all transactions are committed 16591646Sperrin */ 16601646Sperrin int 16611646Sperrin zil_is_committed(zilog_t *zilog) 16621646Sperrin { 16631646Sperrin lwb_t *lwb; 16642638Sperrin int ret; 16651646Sperrin 16662638Sperrin mutex_enter(&zilog->zl_lock); 16672638Sperrin while (zilog->zl_writer) 16682638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 16692638Sperrin 16702638Sperrin /* recent unpushed intent log transactions? */ 16712638Sperrin if (!list_is_empty(&zilog->zl_itx_list)) { 16722638Sperrin ret = B_FALSE; 16732638Sperrin goto out; 16742638Sperrin } 16752638Sperrin 16762638Sperrin /* intent log never used? */ 16772638Sperrin lwb = list_head(&zilog->zl_lwb_list); 16782638Sperrin if (lwb == NULL) { 16792638Sperrin ret = B_TRUE; 16802638Sperrin goto out; 16812638Sperrin } 16821646Sperrin 16831646Sperrin /* 16842638Sperrin * more than 1 log buffer means zil_sync() hasn't yet freed 16852638Sperrin * entries after a txg has committed 16861646Sperrin */ 16872638Sperrin if (list_next(&zilog->zl_lwb_list, lwb)) { 16882638Sperrin ret = B_FALSE; 16892638Sperrin goto out; 16902638Sperrin } 16912638Sperrin 16921646Sperrin ASSERT(zil_empty(zilog)); 16932638Sperrin ret = B_TRUE; 16942638Sperrin out: 16952638Sperrin cv_broadcast(&zilog->zl_cv_writer); 16962638Sperrin mutex_exit(&zilog->zl_lock); 16972638Sperrin return (ret); 16981646Sperrin } 1699