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*5809Sperrin * 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 1701807Sbonwick error = arc_read(NULL, zilog->zl_spa, &blk, byteswap_uint64_array, 1711807Sbonwick arc_getbuf_func, abufpp, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL | 1722391Smaybee ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB, &aflags, &zb); 1731807Sbonwick 1741807Sbonwick if (error == 0) { 1751807Sbonwick char *data = (*abufpp)->b_data; 1761807Sbonwick uint64_t blksz = BP_GET_LSIZE(bp); 1771807Sbonwick zil_trailer_t *ztp = (zil_trailer_t *)(data + blksz) - 1; 1781807Sbonwick zio_cksum_t cksum = bp->blk_cksum; 1791544Seschrock 1801807Sbonwick /* 1811807Sbonwick * Sequence numbers should be... sequential. The checksum 1821807Sbonwick * verifier for the next block should be bp's checksum plus 1. 1831807Sbonwick */ 1841807Sbonwick cksum.zc_word[ZIL_ZC_SEQ]++; 1851807Sbonwick 1861807Sbonwick if (bcmp(&cksum, &ztp->zit_next_blk.blk_cksum, sizeof (cksum))) 1871807Sbonwick error = ESTALE; 1881807Sbonwick else if (BP_IS_HOLE(&ztp->zit_next_blk)) 1891807Sbonwick error = ENOENT; 1901807Sbonwick else if (ztp->zit_nused > (blksz - sizeof (zil_trailer_t))) 1911807Sbonwick error = EOVERFLOW; 1921807Sbonwick 1931807Sbonwick if (error) { 1941807Sbonwick VERIFY(arc_buf_remove_ref(*abufpp, abufpp) == 1); 1951807Sbonwick *abufpp = NULL; 1961807Sbonwick } 197789Sahrens } 198789Sahrens 1991807Sbonwick dprintf("error %d on %llu:%llu\n", error, zb.zb_objset, zb.zb_blkid); 200789Sahrens 2011807Sbonwick return (error); 202789Sahrens } 203789Sahrens 204789Sahrens /* 205789Sahrens * Parse the intent log, and call parse_func for each valid record within. 2061807Sbonwick * Return the highest sequence number. 207789Sahrens */ 2081807Sbonwick uint64_t 209789Sahrens zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func, 210789Sahrens zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg) 211789Sahrens { 2121807Sbonwick const zil_header_t *zh = zilog->zl_header; 2131807Sbonwick uint64_t claim_seq = zh->zh_claim_seq; 2141807Sbonwick uint64_t seq = 0; 2151807Sbonwick uint64_t max_seq = 0; 2161807Sbonwick blkptr_t blk = zh->zh_log; 2171807Sbonwick arc_buf_t *abuf; 218789Sahrens char *lrbuf, *lrp; 219789Sahrens zil_trailer_t *ztp; 220789Sahrens int reclen, error; 221789Sahrens 222789Sahrens if (BP_IS_HOLE(&blk)) 2231807Sbonwick return (max_seq); 224789Sahrens 225789Sahrens /* 226789Sahrens * Starting at the block pointed to by zh_log we read the log chain. 227789Sahrens * For each block in the chain we strongly check that block to 228789Sahrens * ensure its validity. We stop when an invalid block is found. 229789Sahrens * For each block pointer in the chain we call parse_blk_func(). 230789Sahrens * For each record in each valid block we call parse_lr_func(). 2311807Sbonwick * If the log has been claimed, stop if we encounter a sequence 2321807Sbonwick * number greater than the highest claimed sequence number. 233789Sahrens */ 234789Sahrens zil_dva_tree_init(&zilog->zl_dva_tree); 235789Sahrens for (;;) { 2361807Sbonwick seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 2371807Sbonwick 2381807Sbonwick if (claim_seq != 0 && seq > claim_seq) 2391807Sbonwick break; 2401807Sbonwick 2411807Sbonwick ASSERT(max_seq < seq); 2421807Sbonwick max_seq = seq; 2431807Sbonwick 2441807Sbonwick error = zil_read_log_block(zilog, &blk, &abuf); 245789Sahrens 246789Sahrens if (parse_blk_func != NULL) 247789Sahrens parse_blk_func(zilog, &blk, arg, txg); 248789Sahrens 249789Sahrens if (error) 250789Sahrens break; 251789Sahrens 2521807Sbonwick lrbuf = abuf->b_data; 253789Sahrens ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1; 254789Sahrens blk = ztp->zit_next_blk; 255789Sahrens 2561807Sbonwick if (parse_lr_func == NULL) { 2571807Sbonwick VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 258789Sahrens continue; 2591807Sbonwick } 260789Sahrens 261789Sahrens for (lrp = lrbuf; lrp < lrbuf + ztp->zit_nused; lrp += reclen) { 262789Sahrens lr_t *lr = (lr_t *)lrp; 263789Sahrens reclen = lr->lrc_reclen; 264789Sahrens ASSERT3U(reclen, >=, sizeof (lr_t)); 265789Sahrens parse_lr_func(zilog, lr, arg, txg); 266789Sahrens } 2671807Sbonwick VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 268789Sahrens } 269789Sahrens zil_dva_tree_fini(&zilog->zl_dva_tree); 2701807Sbonwick 2711807Sbonwick return (max_seq); 272789Sahrens } 273789Sahrens 274789Sahrens /* ARGSUSED */ 275789Sahrens static void 276789Sahrens zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg) 277789Sahrens { 278789Sahrens spa_t *spa = zilog->zl_spa; 279789Sahrens int err; 280789Sahrens 281789Sahrens /* 282789Sahrens * Claim log block if not already committed and not already claimed. 283789Sahrens */ 284789Sahrens if (bp->blk_birth >= first_txg && 285789Sahrens zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp)) == 0) { 286789Sahrens err = zio_wait(zio_claim(NULL, spa, first_txg, bp, NULL, NULL)); 287789Sahrens ASSERT(err == 0); 288789Sahrens } 289789Sahrens } 290789Sahrens 291789Sahrens static void 292789Sahrens zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg) 293789Sahrens { 294789Sahrens if (lrc->lrc_txtype == TX_WRITE) { 295789Sahrens lr_write_t *lr = (lr_write_t *)lrc; 296789Sahrens zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg); 297789Sahrens } 298789Sahrens } 299789Sahrens 300789Sahrens /* ARGSUSED */ 301789Sahrens static void 302789Sahrens zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg) 303789Sahrens { 304789Sahrens zio_free_blk(zilog->zl_spa, bp, dmu_tx_get_txg(tx)); 305789Sahrens } 306789Sahrens 307789Sahrens static void 308789Sahrens zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg) 309789Sahrens { 310789Sahrens /* 311789Sahrens * If we previously claimed it, we need to free it. 312789Sahrens */ 313789Sahrens if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE) { 314789Sahrens lr_write_t *lr = (lr_write_t *)lrc; 315789Sahrens blkptr_t *bp = &lr->lr_blkptr; 316789Sahrens if (bp->blk_birth >= claim_txg && 317789Sahrens !zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp))) { 318789Sahrens (void) arc_free(NULL, zilog->zl_spa, 319789Sahrens dmu_tx_get_txg(tx), bp, NULL, NULL, ARC_WAIT); 320789Sahrens } 321789Sahrens } 322789Sahrens } 323789Sahrens 324789Sahrens /* 325789Sahrens * Create an on-disk intent log. 326789Sahrens */ 327789Sahrens static void 328789Sahrens zil_create(zilog_t *zilog) 329789Sahrens { 3301807Sbonwick const zil_header_t *zh = zilog->zl_header; 331789Sahrens lwb_t *lwb; 3321807Sbonwick uint64_t txg = 0; 3331807Sbonwick dmu_tx_t *tx = NULL; 334789Sahrens blkptr_t blk; 3351807Sbonwick int error = 0; 336789Sahrens 337789Sahrens /* 3381807Sbonwick * Wait for any previous destroy to complete. 339789Sahrens */ 3401807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 3411807Sbonwick 3421807Sbonwick ASSERT(zh->zh_claim_txg == 0); 3431807Sbonwick ASSERT(zh->zh_replay_seq == 0); 3441807Sbonwick 3451807Sbonwick blk = zh->zh_log; 346789Sahrens 347789Sahrens /* 3481807Sbonwick * If we don't already have an initial log block, allocate one now. 349789Sahrens */ 3501807Sbonwick if (BP_IS_HOLE(&blk)) { 3511807Sbonwick tx = dmu_tx_create(zilog->zl_os); 3521807Sbonwick (void) dmu_tx_assign(tx, TXG_WAIT); 3531807Sbonwick dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 3541807Sbonwick txg = dmu_tx_get_txg(tx); 3551807Sbonwick 3563063Sperrin error = zio_alloc_blk(zilog->zl_spa, ZIL_MIN_BLKSZ, &blk, 3573063Sperrin NULL, txg); 3581807Sbonwick 3591807Sbonwick if (error == 0) 3601807Sbonwick zil_init_log_chain(zilog, &blk); 3611362Sperrin } 3621807Sbonwick 3631807Sbonwick /* 3641807Sbonwick * Allocate a log write buffer (lwb) for the first log block. 3651807Sbonwick */ 366789Sahrens if (error == 0) { 367789Sahrens lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 368789Sahrens lwb->lwb_zilog = zilog; 369789Sahrens lwb->lwb_blk = blk; 370789Sahrens lwb->lwb_nused = 0; 371789Sahrens lwb->lwb_sz = BP_GET_LSIZE(&lwb->lwb_blk); 372789Sahrens lwb->lwb_buf = zio_buf_alloc(lwb->lwb_sz); 373789Sahrens lwb->lwb_max_txg = txg; 3742237Smaybee lwb->lwb_zio = NULL; 3752237Smaybee 376789Sahrens mutex_enter(&zilog->zl_lock); 377789Sahrens list_insert_tail(&zilog->zl_lwb_list, lwb); 378789Sahrens mutex_exit(&zilog->zl_lock); 379789Sahrens } 380789Sahrens 3811807Sbonwick /* 3821807Sbonwick * If we just allocated the first log block, commit our transaction 3831807Sbonwick * and wait for zil_sync() to stuff the block poiner into zh_log. 3841807Sbonwick * (zh is part of the MOS, so we cannot modify it in open context.) 3851807Sbonwick */ 3861807Sbonwick if (tx != NULL) { 3871807Sbonwick dmu_tx_commit(tx); 3881362Sperrin txg_wait_synced(zilog->zl_dmu_pool, txg); 3891807Sbonwick } 3901807Sbonwick 3911807Sbonwick ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0); 392789Sahrens } 393789Sahrens 394789Sahrens /* 395789Sahrens * In one tx, free all log blocks and clear the log header. 3961807Sbonwick * If keep_first is set, then we're replaying a log with no content. 3971807Sbonwick * We want to keep the first block, however, so that the first 3981807Sbonwick * synchronous transaction doesn't require a txg_wait_synced() 3991807Sbonwick * in zil_create(). We don't need to txg_wait_synced() here either 4001807Sbonwick * when keep_first is set, because both zil_create() and zil_destroy() 4011807Sbonwick * will wait for any in-progress destroys to complete. 402789Sahrens */ 403789Sahrens void 4041807Sbonwick zil_destroy(zilog_t *zilog, boolean_t keep_first) 405789Sahrens { 4061807Sbonwick const zil_header_t *zh = zilog->zl_header; 4071807Sbonwick lwb_t *lwb; 408789Sahrens dmu_tx_t *tx; 409789Sahrens uint64_t txg; 410789Sahrens 4111807Sbonwick /* 4121807Sbonwick * Wait for any previous destroy to complete. 4131807Sbonwick */ 4141807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 415789Sahrens 4161807Sbonwick if (BP_IS_HOLE(&zh->zh_log)) 417789Sahrens return; 418789Sahrens 419789Sahrens tx = dmu_tx_create(zilog->zl_os); 420789Sahrens (void) dmu_tx_assign(tx, TXG_WAIT); 421789Sahrens dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 422789Sahrens txg = dmu_tx_get_txg(tx); 423789Sahrens 4241807Sbonwick mutex_enter(&zilog->zl_lock); 4251807Sbonwick 4265223Sperrin /* 4275223Sperrin * It is possible for the ZIL to get the previously mounted zilog 4285223Sperrin * structure of the same dataset if quickly remounted and the dbuf 4295223Sperrin * eviction has not completed. In this case we can see a non 4305223Sperrin * empty lwb list and keep_first will be set. We fix this by 4315223Sperrin * clearing the keep_first. This will be slower but it's very rare. 4325223Sperrin */ 4335223Sperrin if (!list_is_empty(&zilog->zl_lwb_list) && keep_first) 4345223Sperrin keep_first = B_FALSE; 4355223Sperrin 4361807Sbonwick ASSERT3U(zilog->zl_destroy_txg, <, txg); 437789Sahrens zilog->zl_destroy_txg = txg; 4381807Sbonwick zilog->zl_keep_first = keep_first; 4391807Sbonwick 4401807Sbonwick if (!list_is_empty(&zilog->zl_lwb_list)) { 4411807Sbonwick ASSERT(zh->zh_claim_txg == 0); 4421807Sbonwick ASSERT(!keep_first); 4431807Sbonwick while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 4441807Sbonwick list_remove(&zilog->zl_lwb_list, lwb); 4451807Sbonwick if (lwb->lwb_buf != NULL) 4461807Sbonwick zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 4471807Sbonwick zio_free_blk(zilog->zl_spa, &lwb->lwb_blk, txg); 4481807Sbonwick kmem_cache_free(zil_lwb_cache, lwb); 4491807Sbonwick } 4501807Sbonwick } else { 4511807Sbonwick if (!keep_first) { 4521807Sbonwick (void) zil_parse(zilog, zil_free_log_block, 4531807Sbonwick zil_free_log_record, tx, zh->zh_claim_txg); 4541807Sbonwick } 4551807Sbonwick } 4562638Sperrin mutex_exit(&zilog->zl_lock); 457789Sahrens 458789Sahrens dmu_tx_commit(tx); 459789Sahrens } 460789Sahrens 4614935Sperrin /* 4624935Sperrin * zil_rollback_destroy() is only called by the rollback code. 4634935Sperrin * We already have a syncing tx. Rollback has exclusive access to the 4644935Sperrin * dataset, so we don't have to worry about concurrent zil access. 4654935Sperrin * The actual freeing of any log blocks occurs in zil_sync() later in 4664935Sperrin * this txg syncing phase. 4674935Sperrin */ 4684935Sperrin void 4694935Sperrin zil_rollback_destroy(zilog_t *zilog, dmu_tx_t *tx) 4704935Sperrin { 4714935Sperrin const zil_header_t *zh = zilog->zl_header; 4724935Sperrin uint64_t txg; 4734935Sperrin 4744935Sperrin if (BP_IS_HOLE(&zh->zh_log)) 4754935Sperrin return; 4764935Sperrin 4774935Sperrin txg = dmu_tx_get_txg(tx); 4784935Sperrin ASSERT3U(zilog->zl_destroy_txg, <, txg); 4794935Sperrin zilog->zl_destroy_txg = txg; 4804935Sperrin zilog->zl_keep_first = B_FALSE; 4814935Sperrin 482*5809Sperrin /* 483*5809Sperrin * Ensure there's no outstanding ZIL IO. No lwbs or just the 484*5809Sperrin * unused one that allocated in advance is ok. 485*5809Sperrin */ 486*5809Sperrin ASSERT(zilog->zl_lwb_list.list_head.list_next == 487*5809Sperrin zilog->zl_lwb_list.list_head.list_prev); 4884935Sperrin (void) zil_parse(zilog, zil_free_log_block, zil_free_log_record, 4894935Sperrin tx, zh->zh_claim_txg); 4904935Sperrin } 4914935Sperrin 4922199Sahrens int 493789Sahrens zil_claim(char *osname, void *txarg) 494789Sahrens { 495789Sahrens dmu_tx_t *tx = txarg; 496789Sahrens uint64_t first_txg = dmu_tx_get_txg(tx); 497789Sahrens zilog_t *zilog; 498789Sahrens zil_header_t *zh; 499789Sahrens objset_t *os; 500789Sahrens int error; 501789Sahrens 502789Sahrens error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_STANDARD, &os); 503789Sahrens if (error) { 504789Sahrens cmn_err(CE_WARN, "can't process intent log for %s", osname); 5052199Sahrens return (0); 506789Sahrens } 507789Sahrens 508789Sahrens zilog = dmu_objset_zil(os); 5091807Sbonwick zh = zil_header_in_syncing_context(zilog); 510789Sahrens 511789Sahrens /* 5121807Sbonwick * Claim all log blocks if we haven't already done so, and remember 5131807Sbonwick * the highest claimed sequence number. This ensures that if we can 5141807Sbonwick * read only part of the log now (e.g. due to a missing device), 5151807Sbonwick * but we can read the entire log later, we will not try to replay 5161807Sbonwick * or destroy beyond the last block we successfully claimed. 517789Sahrens */ 518789Sahrens ASSERT3U(zh->zh_claim_txg, <=, first_txg); 519789Sahrens if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) { 520789Sahrens zh->zh_claim_txg = first_txg; 5211807Sbonwick zh->zh_claim_seq = zil_parse(zilog, zil_claim_log_block, 5221807Sbonwick zil_claim_log_record, tx, first_txg); 523789Sahrens dsl_dataset_dirty(dmu_objset_ds(os), tx); 524789Sahrens } 5251807Sbonwick 526789Sahrens ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1)); 527789Sahrens dmu_objset_close(os); 5282199Sahrens return (0); 529789Sahrens } 530789Sahrens 5315688Sbonwick static int 5325688Sbonwick zil_vdev_compare(const void *x1, const void *x2) 533789Sahrens { 5345688Sbonwick const uint64_t *v1 = x1; 5355688Sbonwick const uint64_t *v2 = x2; 5365688Sbonwick 5375688Sbonwick if (v1 < v2) 5385688Sbonwick return (-1); 5395688Sbonwick if (v1 > v2) 5405688Sbonwick return (1); 5415688Sbonwick 5425688Sbonwick return (0); 5435688Sbonwick } 5445688Sbonwick 5455688Sbonwick void 5465688Sbonwick zil_add_block(zilog_t *zilog, blkptr_t *bp) 5475688Sbonwick { 5485688Sbonwick avl_tree_t *t = &zilog->zl_vdev_tree; 5495688Sbonwick avl_index_t where; 5505688Sbonwick zil_vdev_node_t *zv, zvsearch; 5515688Sbonwick int ndvas = BP_GET_NDVAS(bp); 5525688Sbonwick int i; 553789Sahrens 5542986Sek110237 if (zfs_nocacheflush) 555789Sahrens return; 556789Sahrens 5575688Sbonwick ASSERT(zilog->zl_writer); 5585688Sbonwick 5595688Sbonwick /* 5605688Sbonwick * Even though we're zl_writer, we still need a lock because the 5615688Sbonwick * zl_get_data() callbacks may have dmu_sync() done callbacks 5625688Sbonwick * that will run concurrently. 5635688Sbonwick */ 5645688Sbonwick mutex_enter(&zilog->zl_vdev_lock); 5655688Sbonwick for (i = 0; i < ndvas; i++) { 5665688Sbonwick zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]); 5675688Sbonwick if (avl_find(t, &zvsearch, &where) == NULL) { 5685688Sbonwick zv = kmem_alloc(sizeof (*zv), KM_SLEEP); 5695688Sbonwick zv->zv_vdev = zvsearch.zv_vdev; 5705688Sbonwick avl_insert(t, zv, where); 5713063Sperrin } 5723063Sperrin } 5735688Sbonwick mutex_exit(&zilog->zl_vdev_lock); 5743063Sperrin } 5753063Sperrin 576789Sahrens void 5772638Sperrin zil_flush_vdevs(zilog_t *zilog) 578789Sahrens { 5793063Sperrin spa_t *spa = zilog->zl_spa; 5805688Sbonwick avl_tree_t *t = &zilog->zl_vdev_tree; 5815688Sbonwick void *cookie = NULL; 5825688Sbonwick zil_vdev_node_t *zv; 5835688Sbonwick zio_t *zio; 5843063Sperrin 5853063Sperrin ASSERT(zilog->zl_writer); 586789Sahrens 5875688Sbonwick /* 5885688Sbonwick * We don't need zl_vdev_lock here because we're the zl_writer, 5895688Sbonwick * and all zl_get_data() callbacks are done. 5905688Sbonwick */ 5915688Sbonwick if (avl_numnodes(t) == 0) 5925688Sbonwick return; 5935688Sbonwick 5945688Sbonwick spa_config_enter(spa, RW_READER, FTAG); 5955688Sbonwick 5965688Sbonwick zio = zio_root(spa, NULL, NULL, 5975688Sbonwick ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL); 5985688Sbonwick 5995688Sbonwick while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) { 6005688Sbonwick vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev); 6015688Sbonwick if (vd != NULL) 6025688Sbonwick zio_flush(zio, vd); 6035688Sbonwick kmem_free(zv, sizeof (*zv)); 6043063Sperrin } 605789Sahrens 606789Sahrens /* 607789Sahrens * Wait for all the flushes to complete. Not all devices actually 608789Sahrens * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails. 609789Sahrens */ 6105688Sbonwick (void) zio_wait(zio); 6115688Sbonwick 6125688Sbonwick spa_config_exit(spa, FTAG); 613789Sahrens } 614789Sahrens 615789Sahrens /* 616789Sahrens * Function called when a log block write completes 617789Sahrens */ 618789Sahrens static void 619789Sahrens zil_lwb_write_done(zio_t *zio) 620789Sahrens { 621789Sahrens lwb_t *lwb = zio->io_private; 622789Sahrens zilog_t *zilog = lwb->lwb_zilog; 623789Sahrens 624789Sahrens /* 625789Sahrens * Now that we've written this log block, we have a stable pointer 626789Sahrens * to the next block in the chain, so it's OK to let the txg in 627789Sahrens * which we allocated the next block sync. 628789Sahrens */ 629789Sahrens txg_rele_to_sync(&lwb->lwb_txgh); 630789Sahrens 631789Sahrens zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 632789Sahrens mutex_enter(&zilog->zl_lock); 633789Sahrens lwb->lwb_buf = NULL; 6344527Sperrin if (zio->io_error) 635789Sahrens zilog->zl_log_error = B_TRUE; 636789Sahrens mutex_exit(&zilog->zl_lock); 637789Sahrens } 638789Sahrens 639789Sahrens /* 6402237Smaybee * Initialize the io for a log block. 6412237Smaybee * 6422237Smaybee * Note, we should not initialize the IO until we are about 6432237Smaybee * to use it, since zio_rewrite() does a spa_config_enter(). 6442237Smaybee */ 6452237Smaybee static void 6462237Smaybee zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb) 6472237Smaybee { 6482237Smaybee zbookmark_t zb; 6492237Smaybee 6502237Smaybee zb.zb_objset = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET]; 6512237Smaybee zb.zb_object = 0; 6522237Smaybee zb.zb_level = -1; 6532237Smaybee zb.zb_blkid = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 6542237Smaybee 6552638Sperrin if (zilog->zl_root_zio == NULL) { 6562638Sperrin zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL, 6572638Sperrin ZIO_FLAG_CANFAIL); 6582638Sperrin } 6593063Sperrin if (lwb->lwb_zio == NULL) { 6603063Sperrin lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa, 6613063Sperrin ZIO_CHECKSUM_ZILOG, 0, &lwb->lwb_blk, lwb->lwb_buf, 6623063Sperrin lwb->lwb_sz, zil_lwb_write_done, lwb, 6634527Sperrin ZIO_PRIORITY_LOG_WRITE, ZIO_FLAG_CANFAIL, &zb); 6643063Sperrin } 6652237Smaybee } 6662237Smaybee 6672237Smaybee /* 668789Sahrens * Start a log block write and advance to the next log block. 669789Sahrens * Calls are serialized. 670789Sahrens */ 671789Sahrens static lwb_t * 672789Sahrens zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb) 673789Sahrens { 674789Sahrens lwb_t *nlwb; 675789Sahrens zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1; 6761807Sbonwick spa_t *spa = zilog->zl_spa; 6771807Sbonwick blkptr_t *bp = &ztp->zit_next_blk; 678789Sahrens uint64_t txg; 679789Sahrens uint64_t zil_blksz; 680789Sahrens int error; 681789Sahrens 682789Sahrens ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb)); 683789Sahrens 684789Sahrens /* 685789Sahrens * Allocate the next block and save its address in this block 686789Sahrens * before writing it in order to establish the log chain. 687789Sahrens * Note that if the allocation of nlwb synced before we wrote 688789Sahrens * the block that points at it (lwb), we'd leak it if we crashed. 689789Sahrens * Therefore, we don't do txg_rele_to_sync() until zil_lwb_write_done(). 690789Sahrens */ 691789Sahrens txg = txg_hold_open(zilog->zl_dmu_pool, &lwb->lwb_txgh); 692789Sahrens txg_rele_to_quiesce(&lwb->lwb_txgh); 693789Sahrens 694789Sahrens /* 6951141Sperrin * Pick a ZIL blocksize. We request a size that is the 6961141Sperrin * maximum of the previous used size, the current used size and 6971141Sperrin * the amount waiting in the queue. 698789Sahrens */ 6992237Smaybee zil_blksz = MAX(zilog->zl_prev_used, 7002237Smaybee zilog->zl_cur_used + sizeof (*ztp)); 7011141Sperrin zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp)); 7021842Sperrin zil_blksz = P2ROUNDUP_TYPED(zil_blksz, ZIL_MIN_BLKSZ, uint64_t); 7031141Sperrin if (zil_blksz > ZIL_MAX_BLKSZ) 7041141Sperrin zil_blksz = ZIL_MAX_BLKSZ; 705789Sahrens 7063063Sperrin BP_ZERO(bp); 7073063Sperrin /* pass the old blkptr in order to spread log blocks across devs */ 7083063Sperrin error = zio_alloc_blk(spa, zil_blksz, bp, &lwb->lwb_blk, txg); 709789Sahrens if (error) { 7103668Sgw25295 dmu_tx_t *tx = dmu_tx_create_assigned(zilog->zl_dmu_pool, txg); 7113668Sgw25295 7121544Seschrock /* 7133668Sgw25295 * We dirty the dataset to ensure that zil_sync() will 7143668Sgw25295 * be called to remove this lwb from our zl_lwb_list. 7153668Sgw25295 * Failing to do so, may leave an lwb with a NULL lwb_buf 7163668Sgw25295 * hanging around on the zl_lwb_list. 7173668Sgw25295 */ 7183668Sgw25295 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 7193848Sgw25295 dmu_tx_commit(tx); 7203668Sgw25295 7213668Sgw25295 /* 7223668Sgw25295 * Since we've just experienced an allocation failure so we 7233668Sgw25295 * terminate the current lwb and send it on its way. 7243668Sgw25295 */ 7253668Sgw25295 ztp->zit_pad = 0; 7263668Sgw25295 ztp->zit_nused = lwb->lwb_nused; 7273668Sgw25295 ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum; 7283668Sgw25295 zio_nowait(lwb->lwb_zio); 7293668Sgw25295 7303668Sgw25295 /* 7311544Seschrock * By returning NULL the caller will call tx_wait_synced() 7321544Seschrock */ 733789Sahrens return (NULL); 734789Sahrens } 735789Sahrens 7361807Sbonwick ASSERT3U(bp->blk_birth, ==, txg); 7371544Seschrock ztp->zit_pad = 0; 738789Sahrens ztp->zit_nused = lwb->lwb_nused; 739789Sahrens ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum; 7401807Sbonwick bp->blk_cksum = lwb->lwb_blk.blk_cksum; 7411807Sbonwick bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++; 742789Sahrens 743789Sahrens /* 744789Sahrens * Allocate a new log write buffer (lwb). 745789Sahrens */ 746789Sahrens nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 747789Sahrens 748789Sahrens nlwb->lwb_zilog = zilog; 7491807Sbonwick nlwb->lwb_blk = *bp; 750789Sahrens nlwb->lwb_nused = 0; 751789Sahrens nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk); 752789Sahrens nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz); 753789Sahrens nlwb->lwb_max_txg = txg; 7542237Smaybee nlwb->lwb_zio = NULL; 755789Sahrens 756789Sahrens /* 7573063Sperrin * Put new lwb at the end of the log chain 758789Sahrens */ 759789Sahrens mutex_enter(&zilog->zl_lock); 760789Sahrens list_insert_tail(&zilog->zl_lwb_list, nlwb); 7613063Sperrin mutex_exit(&zilog->zl_lock); 7623063Sperrin 7635688Sbonwick /* Record the block for later vdev flushing */ 7645688Sbonwick zil_add_block(zilog, &lwb->lwb_blk); 765789Sahrens 766789Sahrens /* 7672237Smaybee * kick off the write for the old log block 768789Sahrens */ 7692237Smaybee dprintf_bp(&lwb->lwb_blk, "lwb %p txg %llu: ", lwb, txg); 7703063Sperrin ASSERT(lwb->lwb_zio); 7712237Smaybee zio_nowait(lwb->lwb_zio); 772789Sahrens 773789Sahrens return (nlwb); 774789Sahrens } 775789Sahrens 776789Sahrens static lwb_t * 777789Sahrens zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb) 778789Sahrens { 779789Sahrens lr_t *lrc = &itx->itx_lr; /* common log record */ 7802237Smaybee lr_write_t *lr = (lr_write_t *)lrc; 781789Sahrens uint64_t txg = lrc->lrc_txg; 782789Sahrens uint64_t reclen = lrc->lrc_reclen; 7832237Smaybee uint64_t dlen; 784789Sahrens 785789Sahrens if (lwb == NULL) 786789Sahrens return (NULL); 787789Sahrens ASSERT(lwb->lwb_buf != NULL); 788789Sahrens 7892237Smaybee if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY) 7902237Smaybee dlen = P2ROUNDUP_TYPED( 7912237Smaybee lr->lr_length, sizeof (uint64_t), uint64_t); 7922237Smaybee else 7932237Smaybee dlen = 0; 7941669Sperrin 7951669Sperrin zilog->zl_cur_used += (reclen + dlen); 7961669Sperrin 7973063Sperrin zil_lwb_write_init(zilog, lwb); 7983063Sperrin 7991669Sperrin /* 8001669Sperrin * If this record won't fit in the current log block, start a new one. 8011669Sperrin */ 8021669Sperrin if (lwb->lwb_nused + reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) { 8031669Sperrin lwb = zil_lwb_write_start(zilog, lwb); 8042237Smaybee if (lwb == NULL) 8051669Sperrin return (NULL); 8063063Sperrin zil_lwb_write_init(zilog, lwb); 8071669Sperrin ASSERT(lwb->lwb_nused == 0); 8081669Sperrin if (reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) { 8091669Sperrin txg_wait_synced(zilog->zl_dmu_pool, txg); 810789Sahrens return (lwb); 811789Sahrens } 812789Sahrens } 813789Sahrens 8142638Sperrin /* 8152638Sperrin * Update the lrc_seq, to be log record sequence number. See zil.h 8162638Sperrin * Then copy the record to the log buffer. 8172638Sperrin */ 8182638Sperrin lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */ 819789Sahrens bcopy(lrc, lwb->lwb_buf + lwb->lwb_nused, reclen); 8202237Smaybee 8212237Smaybee /* 8222237Smaybee * If it's a write, fetch the data or get its blkptr as appropriate. 8232237Smaybee */ 8242237Smaybee if (lrc->lrc_txtype == TX_WRITE) { 8252237Smaybee if (txg > spa_freeze_txg(zilog->zl_spa)) 8262237Smaybee txg_wait_synced(zilog->zl_dmu_pool, txg); 8272237Smaybee if (itx->itx_wr_state != WR_COPIED) { 8282237Smaybee char *dbuf; 8292237Smaybee int error; 8302237Smaybee 8312237Smaybee /* alignment is guaranteed */ 8322237Smaybee lr = (lr_write_t *)(lwb->lwb_buf + lwb->lwb_nused); 8332237Smaybee if (dlen) { 8342237Smaybee ASSERT(itx->itx_wr_state == WR_NEED_COPY); 8352237Smaybee dbuf = lwb->lwb_buf + lwb->lwb_nused + reclen; 8362237Smaybee lr->lr_common.lrc_reclen += dlen; 8372237Smaybee } else { 8382237Smaybee ASSERT(itx->itx_wr_state == WR_INDIRECT); 8392237Smaybee dbuf = NULL; 8402237Smaybee } 8412237Smaybee error = zilog->zl_get_data( 8422237Smaybee itx->itx_private, lr, dbuf, lwb->lwb_zio); 8432237Smaybee if (error) { 8442237Smaybee ASSERT(error == ENOENT || error == EEXIST || 8452237Smaybee error == EALREADY); 8462237Smaybee return (lwb); 8472237Smaybee } 8482237Smaybee } 8491669Sperrin } 8502237Smaybee 8512237Smaybee lwb->lwb_nused += reclen + dlen; 852789Sahrens lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg); 853789Sahrens ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb)); 854789Sahrens ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0); 855789Sahrens 856789Sahrens return (lwb); 857789Sahrens } 858789Sahrens 859789Sahrens itx_t * 8605331Samw zil_itx_create(uint64_t txtype, size_t lrsize) 861789Sahrens { 862789Sahrens itx_t *itx; 863789Sahrens 8641842Sperrin lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t); 865789Sahrens 866789Sahrens itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP); 867789Sahrens itx->itx_lr.lrc_txtype = txtype; 868789Sahrens itx->itx_lr.lrc_reclen = lrsize; 869789Sahrens itx->itx_lr.lrc_seq = 0; /* defensive */ 870789Sahrens 871789Sahrens return (itx); 872789Sahrens } 873789Sahrens 874789Sahrens uint64_t 875789Sahrens zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx) 876789Sahrens { 877789Sahrens uint64_t seq; 878789Sahrens 879789Sahrens ASSERT(itx->itx_lr.lrc_seq == 0); 880789Sahrens 881789Sahrens mutex_enter(&zilog->zl_lock); 882789Sahrens list_insert_tail(&zilog->zl_itx_list, itx); 883789Sahrens zilog->zl_itx_list_sz += itx->itx_lr.lrc_reclen; 884789Sahrens itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx); 885789Sahrens itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq; 886789Sahrens mutex_exit(&zilog->zl_lock); 887789Sahrens 888789Sahrens return (seq); 889789Sahrens } 890789Sahrens 891789Sahrens /* 892789Sahrens * Free up all in-memory intent log transactions that have now been synced. 893789Sahrens */ 894789Sahrens static void 895789Sahrens zil_itx_clean(zilog_t *zilog) 896789Sahrens { 897789Sahrens uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa); 898789Sahrens uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa); 8993778Sjohansen list_t clean_list; 900789Sahrens itx_t *itx; 901789Sahrens 9023778Sjohansen list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node)); 9033778Sjohansen 904789Sahrens mutex_enter(&zilog->zl_lock); 9052638Sperrin /* wait for a log writer to finish walking list */ 9062638Sperrin while (zilog->zl_writer) { 9072638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 9082638Sperrin } 9093778Sjohansen 9103778Sjohansen /* 9113778Sjohansen * Move the sync'd log transactions to a separate list so we can call 9123778Sjohansen * kmem_free without holding the zl_lock. 9133778Sjohansen * 9143778Sjohansen * There is no need to set zl_writer as we don't drop zl_lock here 9153778Sjohansen */ 916789Sahrens while ((itx = list_head(&zilog->zl_itx_list)) != NULL && 917789Sahrens itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) { 918789Sahrens list_remove(&zilog->zl_itx_list, itx); 919789Sahrens zilog->zl_itx_list_sz -= itx->itx_lr.lrc_reclen; 9203778Sjohansen list_insert_tail(&clean_list, itx); 9213778Sjohansen } 9223778Sjohansen cv_broadcast(&zilog->zl_cv_writer); 9233778Sjohansen mutex_exit(&zilog->zl_lock); 9243778Sjohansen 9253778Sjohansen /* destroy sync'd log transactions */ 9263778Sjohansen while ((itx = list_head(&clean_list)) != NULL) { 9273778Sjohansen list_remove(&clean_list, itx); 928789Sahrens kmem_free(itx, offsetof(itx_t, itx_lr) 929789Sahrens + itx->itx_lr.lrc_reclen); 930789Sahrens } 9313778Sjohansen list_destroy(&clean_list); 932789Sahrens } 933789Sahrens 9342638Sperrin /* 9353063Sperrin * If there are any in-memory intent log transactions which have now been 9363063Sperrin * synced then start up a taskq to free them. 9372638Sperrin */ 938789Sahrens void 939789Sahrens zil_clean(zilog_t *zilog) 940789Sahrens { 9413063Sperrin itx_t *itx; 9423063Sperrin 943789Sahrens mutex_enter(&zilog->zl_lock); 9443063Sperrin itx = list_head(&zilog->zl_itx_list); 9453063Sperrin if ((itx != NULL) && 9463063Sperrin (itx->itx_lr.lrc_txg <= spa_last_synced_txg(zilog->zl_spa))) { 947789Sahrens (void) taskq_dispatch(zilog->zl_clean_taskq, 948789Sahrens (void (*)(void *))zil_itx_clean, zilog, TQ_NOSLEEP); 9493063Sperrin } 950789Sahrens mutex_exit(&zilog->zl_lock); 951789Sahrens } 952789Sahrens 953789Sahrens void 9542638Sperrin zil_commit_writer(zilog_t *zilog, uint64_t seq, uint64_t foid) 955789Sahrens { 956789Sahrens uint64_t txg; 957789Sahrens uint64_t reclen; 9583063Sperrin uint64_t commit_seq = 0; 9592638Sperrin itx_t *itx, *itx_next = (itx_t *)-1; 960789Sahrens lwb_t *lwb; 961789Sahrens spa_t *spa; 962789Sahrens 9632638Sperrin zilog->zl_writer = B_TRUE; 9642638Sperrin zilog->zl_root_zio = NULL; 965789Sahrens spa = zilog->zl_spa; 966789Sahrens 967789Sahrens if (zilog->zl_suspend) { 968789Sahrens lwb = NULL; 969789Sahrens } else { 970789Sahrens lwb = list_tail(&zilog->zl_lwb_list); 971789Sahrens if (lwb == NULL) { 9722638Sperrin /* 9732638Sperrin * Return if there's nothing to flush before we 9742638Sperrin * dirty the fs by calling zil_create() 9752638Sperrin */ 9762638Sperrin if (list_is_empty(&zilog->zl_itx_list)) { 9772638Sperrin zilog->zl_writer = B_FALSE; 9782638Sperrin return; 9792638Sperrin } 980789Sahrens mutex_exit(&zilog->zl_lock); 981789Sahrens zil_create(zilog); 982789Sahrens mutex_enter(&zilog->zl_lock); 983789Sahrens lwb = list_tail(&zilog->zl_lwb_list); 984789Sahrens } 985789Sahrens } 986789Sahrens 9873063Sperrin /* Loop through in-memory log transactions filling log blocks. */ 9882638Sperrin DTRACE_PROBE1(zil__cw1, zilog_t *, zilog); 989789Sahrens for (;;) { 9902638Sperrin /* 9912638Sperrin * Find the next itx to push: 9922638Sperrin * Push all transactions related to specified foid and all 9932638Sperrin * other transactions except TX_WRITE, TX_TRUNCATE, 9942638Sperrin * TX_SETATTR and TX_ACL for all other files. 9952638Sperrin */ 9962638Sperrin if (itx_next != (itx_t *)-1) 9972638Sperrin itx = itx_next; 9982638Sperrin else 9992638Sperrin itx = list_head(&zilog->zl_itx_list); 10002638Sperrin for (; itx != NULL; itx = list_next(&zilog->zl_itx_list, itx)) { 10012638Sperrin if (foid == 0) /* push all foids? */ 10022638Sperrin break; 10033063Sperrin if (itx->itx_sync) /* push all O_[D]SYNC */ 10043063Sperrin break; 10052638Sperrin switch (itx->itx_lr.lrc_txtype) { 10062638Sperrin case TX_SETATTR: 10072638Sperrin case TX_WRITE: 10082638Sperrin case TX_TRUNCATE: 10092638Sperrin case TX_ACL: 10102638Sperrin /* lr_foid is same offset for these records */ 10112638Sperrin if (((lr_write_t *)&itx->itx_lr)->lr_foid 10122638Sperrin != foid) { 10132638Sperrin continue; /* skip this record */ 10142638Sperrin } 10152638Sperrin } 10162638Sperrin break; 10172638Sperrin } 1018789Sahrens if (itx == NULL) 1019789Sahrens break; 1020789Sahrens 1021789Sahrens reclen = itx->itx_lr.lrc_reclen; 1022789Sahrens if ((itx->itx_lr.lrc_seq > seq) && 10232638Sperrin ((lwb == NULL) || (lwb->lwb_nused == 0) || 10243063Sperrin (lwb->lwb_nused + reclen > ZIL_BLK_DATA_SZ(lwb)))) { 1025789Sahrens break; 10263063Sperrin } 1027789Sahrens 10282638Sperrin /* 10292638Sperrin * Save the next pointer. Even though we soon drop 10302638Sperrin * zl_lock all threads that may change the list 10312638Sperrin * (another writer or zil_itx_clean) can't do so until 10322638Sperrin * they have zl_writer. 10332638Sperrin */ 10342638Sperrin itx_next = list_next(&zilog->zl_itx_list, itx); 1035789Sahrens list_remove(&zilog->zl_itx_list, itx); 10363063Sperrin mutex_exit(&zilog->zl_lock); 1037789Sahrens txg = itx->itx_lr.lrc_txg; 1038789Sahrens ASSERT(txg); 1039789Sahrens 1040789Sahrens if (txg > spa_last_synced_txg(spa) || 1041789Sahrens txg > spa_freeze_txg(spa)) 1042789Sahrens lwb = zil_lwb_commit(zilog, itx, lwb); 1043789Sahrens kmem_free(itx, offsetof(itx_t, itx_lr) 1044789Sahrens + itx->itx_lr.lrc_reclen); 1045789Sahrens mutex_enter(&zilog->zl_lock); 1046789Sahrens zilog->zl_itx_list_sz -= reclen; 1047789Sahrens } 10482638Sperrin DTRACE_PROBE1(zil__cw2, zilog_t *, zilog); 10493063Sperrin /* determine commit sequence number */ 10503063Sperrin itx = list_head(&zilog->zl_itx_list); 10513063Sperrin if (itx) 10523063Sperrin commit_seq = itx->itx_lr.lrc_seq; 10533063Sperrin else 10543063Sperrin commit_seq = zilog->zl_itx_seq; 1055789Sahrens mutex_exit(&zilog->zl_lock); 1056789Sahrens 1057789Sahrens /* write the last block out */ 10583063Sperrin if (lwb != NULL && lwb->lwb_zio != NULL) 1059789Sahrens lwb = zil_lwb_write_start(zilog, lwb); 1060789Sahrens 10611141Sperrin zilog->zl_prev_used = zilog->zl_cur_used; 10621141Sperrin zilog->zl_cur_used = 0; 10631141Sperrin 10642638Sperrin /* 10652638Sperrin * Wait if necessary for the log blocks to be on stable storage. 10662638Sperrin */ 10672638Sperrin if (zilog->zl_root_zio) { 10682638Sperrin DTRACE_PROBE1(zil__cw3, zilog_t *, zilog); 10692638Sperrin (void) zio_wait(zilog->zl_root_zio); 10702638Sperrin DTRACE_PROBE1(zil__cw4, zilog_t *, zilog); 10715688Sbonwick zil_flush_vdevs(zilog); 1072789Sahrens } 10731141Sperrin 1074789Sahrens if (zilog->zl_log_error || lwb == NULL) { 1075789Sahrens zilog->zl_log_error = 0; 1076789Sahrens txg_wait_synced(zilog->zl_dmu_pool, 0); 1077789Sahrens } 10783063Sperrin 10793063Sperrin mutex_enter(&zilog->zl_lock); 10801141Sperrin zilog->zl_writer = B_FALSE; 10813063Sperrin 10823063Sperrin ASSERT3U(commit_seq, >=, zilog->zl_commit_seq); 10833063Sperrin zilog->zl_commit_seq = commit_seq; 10842638Sperrin } 10852638Sperrin 10862638Sperrin /* 10872638Sperrin * Push zfs transactions to stable storage up to the supplied sequence number. 10882638Sperrin * If foid is 0 push out all transactions, otherwise push only those 10892638Sperrin * for that file or might have been used to create that file. 10902638Sperrin */ 10912638Sperrin void 10922638Sperrin zil_commit(zilog_t *zilog, uint64_t seq, uint64_t foid) 10932638Sperrin { 10942638Sperrin if (zilog == NULL || seq == 0) 10952638Sperrin return; 10962638Sperrin 10972638Sperrin mutex_enter(&zilog->zl_lock); 10982638Sperrin 10992638Sperrin seq = MIN(seq, zilog->zl_itx_seq); /* cap seq at largest itx seq */ 11002638Sperrin 11013063Sperrin while (zilog->zl_writer) { 11022638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 11033063Sperrin if (seq < zilog->zl_commit_seq) { 11043063Sperrin mutex_exit(&zilog->zl_lock); 11053063Sperrin return; 11063063Sperrin } 11073063Sperrin } 11082638Sperrin zil_commit_writer(zilog, seq, foid); /* drops zl_lock */ 11093063Sperrin /* wake up others waiting on the commit */ 11103063Sperrin cv_broadcast(&zilog->zl_cv_writer); 11113063Sperrin mutex_exit(&zilog->zl_lock); 1112789Sahrens } 1113789Sahrens 1114789Sahrens /* 1115789Sahrens * Called in syncing context to free committed log blocks and update log header. 1116789Sahrens */ 1117789Sahrens void 1118789Sahrens zil_sync(zilog_t *zilog, dmu_tx_t *tx) 1119789Sahrens { 11201807Sbonwick zil_header_t *zh = zil_header_in_syncing_context(zilog); 1121789Sahrens uint64_t txg = dmu_tx_get_txg(tx); 1122789Sahrens spa_t *spa = zilog->zl_spa; 1123789Sahrens lwb_t *lwb; 1124789Sahrens 11251807Sbonwick mutex_enter(&zilog->zl_lock); 11261807Sbonwick 1127789Sahrens ASSERT(zilog->zl_stop_sync == 0); 1128789Sahrens 11291807Sbonwick zh->zh_replay_seq = zilog->zl_replay_seq[txg & TXG_MASK]; 1130789Sahrens 1131789Sahrens if (zilog->zl_destroy_txg == txg) { 11321807Sbonwick blkptr_t blk = zh->zh_log; 11331807Sbonwick 11341807Sbonwick ASSERT(list_head(&zilog->zl_lwb_list) == NULL); 11351807Sbonwick ASSERT(spa_sync_pass(spa) == 1); 11361807Sbonwick 11371807Sbonwick bzero(zh, sizeof (zil_header_t)); 1138789Sahrens bzero(zilog->zl_replay_seq, sizeof (zilog->zl_replay_seq)); 11391807Sbonwick 11401807Sbonwick if (zilog->zl_keep_first) { 11411807Sbonwick /* 11421807Sbonwick * If this block was part of log chain that couldn't 11431807Sbonwick * be claimed because a device was missing during 11441807Sbonwick * zil_claim(), but that device later returns, 11451807Sbonwick * then this block could erroneously appear valid. 11461807Sbonwick * To guard against this, assign a new GUID to the new 11471807Sbonwick * log chain so it doesn't matter what blk points to. 11481807Sbonwick */ 11491807Sbonwick zil_init_log_chain(zilog, &blk); 11501807Sbonwick zh->zh_log = blk; 11511807Sbonwick } 1152789Sahrens } 1153789Sahrens 1154789Sahrens for (;;) { 1155789Sahrens lwb = list_head(&zilog->zl_lwb_list); 1156789Sahrens if (lwb == NULL) { 1157789Sahrens mutex_exit(&zilog->zl_lock); 1158789Sahrens return; 1159789Sahrens } 11602638Sperrin zh->zh_log = lwb->lwb_blk; 1161789Sahrens if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg) 1162789Sahrens break; 1163789Sahrens list_remove(&zilog->zl_lwb_list, lwb); 1164789Sahrens zio_free_blk(spa, &lwb->lwb_blk, txg); 1165789Sahrens kmem_cache_free(zil_lwb_cache, lwb); 11663668Sgw25295 11673668Sgw25295 /* 11683668Sgw25295 * If we don't have anything left in the lwb list then 11693668Sgw25295 * we've had an allocation failure and we need to zero 11703668Sgw25295 * out the zil_header blkptr so that we don't end 11713668Sgw25295 * up freeing the same block twice. 11723668Sgw25295 */ 11733668Sgw25295 if (list_head(&zilog->zl_lwb_list) == NULL) 11743668Sgw25295 BP_ZERO(&zh->zh_log); 1175789Sahrens } 1176789Sahrens mutex_exit(&zilog->zl_lock); 1177789Sahrens } 1178789Sahrens 1179789Sahrens void 1180789Sahrens zil_init(void) 1181789Sahrens { 1182789Sahrens zil_lwb_cache = kmem_cache_create("zil_lwb_cache", 11832856Snd150628 sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0); 1184789Sahrens } 1185789Sahrens 1186789Sahrens void 1187789Sahrens zil_fini(void) 1188789Sahrens { 1189789Sahrens kmem_cache_destroy(zil_lwb_cache); 1190789Sahrens } 1191789Sahrens 1192789Sahrens zilog_t * 1193789Sahrens zil_alloc(objset_t *os, zil_header_t *zh_phys) 1194789Sahrens { 1195789Sahrens zilog_t *zilog; 1196789Sahrens 1197789Sahrens zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP); 1198789Sahrens 1199789Sahrens zilog->zl_header = zh_phys; 1200789Sahrens zilog->zl_os = os; 1201789Sahrens zilog->zl_spa = dmu_objset_spa(os); 1202789Sahrens zilog->zl_dmu_pool = dmu_objset_pool(os); 12031807Sbonwick zilog->zl_destroy_txg = TXG_INITIAL - 1; 1204789Sahrens 12052856Snd150628 mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL); 12062856Snd150628 1207789Sahrens list_create(&zilog->zl_itx_list, sizeof (itx_t), 1208789Sahrens offsetof(itx_t, itx_node)); 1209789Sahrens 1210789Sahrens list_create(&zilog->zl_lwb_list, sizeof (lwb_t), 1211789Sahrens offsetof(lwb_t, lwb_node)); 1212789Sahrens 12135688Sbonwick mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL); 12145688Sbonwick 12155688Sbonwick avl_create(&zilog->zl_vdev_tree, zil_vdev_compare, 12165688Sbonwick sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node)); 1217789Sahrens 1218789Sahrens return (zilog); 1219789Sahrens } 1220789Sahrens 1221789Sahrens void 1222789Sahrens zil_free(zilog_t *zilog) 1223789Sahrens { 1224789Sahrens lwb_t *lwb; 1225789Sahrens 1226789Sahrens zilog->zl_stop_sync = 1; 1227789Sahrens 1228789Sahrens while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 1229789Sahrens list_remove(&zilog->zl_lwb_list, lwb); 1230789Sahrens if (lwb->lwb_buf != NULL) 1231789Sahrens zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 1232789Sahrens kmem_cache_free(zil_lwb_cache, lwb); 1233789Sahrens } 1234789Sahrens list_destroy(&zilog->zl_lwb_list); 1235789Sahrens 12365688Sbonwick avl_destroy(&zilog->zl_vdev_tree); 12375688Sbonwick mutex_destroy(&zilog->zl_vdev_lock); 1238789Sahrens 1239789Sahrens ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1240789Sahrens list_destroy(&zilog->zl_itx_list); 12412856Snd150628 mutex_destroy(&zilog->zl_lock); 1242789Sahrens 1243789Sahrens kmem_free(zilog, sizeof (zilog_t)); 1244789Sahrens } 1245789Sahrens 1246789Sahrens /* 12471646Sperrin * return true if the initial log block is not valid 12481362Sperrin */ 12491362Sperrin static int 12501362Sperrin zil_empty(zilog_t *zilog) 12511362Sperrin { 12521807Sbonwick const zil_header_t *zh = zilog->zl_header; 12531807Sbonwick arc_buf_t *abuf = NULL; 12541362Sperrin 12551807Sbonwick if (BP_IS_HOLE(&zh->zh_log)) 12561362Sperrin return (1); 12571362Sperrin 12581807Sbonwick if (zil_read_log_block(zilog, &zh->zh_log, &abuf) != 0) 12591807Sbonwick return (1); 12601807Sbonwick 12611807Sbonwick VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 12621807Sbonwick return (0); 12631362Sperrin } 12641362Sperrin 12651362Sperrin /* 1266789Sahrens * Open an intent log. 1267789Sahrens */ 1268789Sahrens zilog_t * 1269789Sahrens zil_open(objset_t *os, zil_get_data_t *get_data) 1270789Sahrens { 1271789Sahrens zilog_t *zilog = dmu_objset_zil(os); 1272789Sahrens 1273789Sahrens zilog->zl_get_data = get_data; 1274789Sahrens zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri, 1275789Sahrens 2, 2, TASKQ_PREPOPULATE); 1276789Sahrens 1277789Sahrens return (zilog); 1278789Sahrens } 1279789Sahrens 1280789Sahrens /* 1281789Sahrens * Close an intent log. 1282789Sahrens */ 1283789Sahrens void 1284789Sahrens zil_close(zilog_t *zilog) 1285789Sahrens { 12861807Sbonwick /* 12871807Sbonwick * If the log isn't already committed, mark the objset dirty 12881807Sbonwick * (so zil_sync() will be called) and wait for that txg to sync. 12891807Sbonwick */ 12901807Sbonwick if (!zil_is_committed(zilog)) { 12911807Sbonwick uint64_t txg; 12921807Sbonwick dmu_tx_t *tx = dmu_tx_create(zilog->zl_os); 12931807Sbonwick (void) dmu_tx_assign(tx, TXG_WAIT); 12941807Sbonwick dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 12951807Sbonwick txg = dmu_tx_get_txg(tx); 12961807Sbonwick dmu_tx_commit(tx); 12971807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, txg); 12981807Sbonwick } 12991807Sbonwick 1300789Sahrens taskq_destroy(zilog->zl_clean_taskq); 1301789Sahrens zilog->zl_clean_taskq = NULL; 1302789Sahrens zilog->zl_get_data = NULL; 1303789Sahrens 1304789Sahrens zil_itx_clean(zilog); 1305789Sahrens ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1306789Sahrens } 1307789Sahrens 1308789Sahrens /* 1309789Sahrens * Suspend an intent log. While in suspended mode, we still honor 1310789Sahrens * synchronous semantics, but we rely on txg_wait_synced() to do it. 1311789Sahrens * We suspend the log briefly when taking a snapshot so that the snapshot 1312789Sahrens * contains all the data it's supposed to, and has an empty intent log. 1313789Sahrens */ 1314789Sahrens int 1315789Sahrens zil_suspend(zilog_t *zilog) 1316789Sahrens { 13171807Sbonwick const zil_header_t *zh = zilog->zl_header; 1318789Sahrens 1319789Sahrens mutex_enter(&zilog->zl_lock); 13201807Sbonwick if (zh->zh_claim_txg != 0) { /* unplayed log */ 1321789Sahrens mutex_exit(&zilog->zl_lock); 1322789Sahrens return (EBUSY); 1323789Sahrens } 13241807Sbonwick if (zilog->zl_suspend++ != 0) { 13251807Sbonwick /* 13261807Sbonwick * Someone else already began a suspend. 13271807Sbonwick * Just wait for them to finish. 13281807Sbonwick */ 13291807Sbonwick while (zilog->zl_suspending) 13301807Sbonwick cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock); 13311807Sbonwick ASSERT(BP_IS_HOLE(&zh->zh_log)); 13321807Sbonwick mutex_exit(&zilog->zl_lock); 13331807Sbonwick return (0); 13341807Sbonwick } 13351807Sbonwick zilog->zl_suspending = B_TRUE; 1336789Sahrens mutex_exit(&zilog->zl_lock); 1337789Sahrens 13382638Sperrin zil_commit(zilog, UINT64_MAX, 0); 1339789Sahrens 13402638Sperrin /* 13412638Sperrin * Wait for any in-flight log writes to complete. 13422638Sperrin */ 1343789Sahrens mutex_enter(&zilog->zl_lock); 13442638Sperrin while (zilog->zl_writer) 13452638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 1346789Sahrens mutex_exit(&zilog->zl_lock); 1347789Sahrens 13481807Sbonwick zil_destroy(zilog, B_FALSE); 13491807Sbonwick 13501807Sbonwick mutex_enter(&zilog->zl_lock); 13511807Sbonwick zilog->zl_suspending = B_FALSE; 13521807Sbonwick cv_broadcast(&zilog->zl_cv_suspend); 13531807Sbonwick mutex_exit(&zilog->zl_lock); 1354789Sahrens 1355789Sahrens return (0); 1356789Sahrens } 1357789Sahrens 1358789Sahrens void 1359789Sahrens zil_resume(zilog_t *zilog) 1360789Sahrens { 1361789Sahrens mutex_enter(&zilog->zl_lock); 1362789Sahrens ASSERT(zilog->zl_suspend != 0); 1363789Sahrens zilog->zl_suspend--; 1364789Sahrens mutex_exit(&zilog->zl_lock); 1365789Sahrens } 1366789Sahrens 1367789Sahrens typedef struct zil_replay_arg { 1368789Sahrens objset_t *zr_os; 1369789Sahrens zil_replay_func_t **zr_replay; 1370789Sahrens void *zr_arg; 1371789Sahrens uint64_t *zr_txgp; 1372789Sahrens boolean_t zr_byteswap; 1373789Sahrens char *zr_lrbuf; 1374789Sahrens } zil_replay_arg_t; 1375789Sahrens 1376789Sahrens static void 1377789Sahrens zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg) 1378789Sahrens { 1379789Sahrens zil_replay_arg_t *zr = zra; 13801807Sbonwick const zil_header_t *zh = zilog->zl_header; 1381789Sahrens uint64_t reclen = lr->lrc_reclen; 1382789Sahrens uint64_t txtype = lr->lrc_txtype; 13833063Sperrin char *name; 13843063Sperrin int pass, error, sunk; 1385789Sahrens 1386789Sahrens if (zilog->zl_stop_replay) 1387789Sahrens return; 1388789Sahrens 1389789Sahrens if (lr->lrc_txg < claim_txg) /* already committed */ 1390789Sahrens return; 1391789Sahrens 1392789Sahrens if (lr->lrc_seq <= zh->zh_replay_seq) /* already replayed */ 1393789Sahrens return; 1394789Sahrens 13955331Samw /* Strip case-insensitive bit, still present in log record */ 13965331Samw txtype &= ~TX_CI; 13975331Samw 1398789Sahrens /* 1399789Sahrens * Make a copy of the data so we can revise and extend it. 1400789Sahrens */ 1401789Sahrens bcopy(lr, zr->zr_lrbuf, reclen); 1402789Sahrens 1403789Sahrens /* 1404789Sahrens * The log block containing this lr may have been byteswapped 1405789Sahrens * so that we can easily examine common fields like lrc_txtype. 1406789Sahrens * However, the log is a mix of different data types, and only the 1407789Sahrens * replay vectors know how to byteswap their records. Therefore, if 1408789Sahrens * the lr was byteswapped, undo it before invoking the replay vector. 1409789Sahrens */ 1410789Sahrens if (zr->zr_byteswap) 1411789Sahrens byteswap_uint64_array(zr->zr_lrbuf, reclen); 1412789Sahrens 1413789Sahrens /* 1414789Sahrens * If this is a TX_WRITE with a blkptr, suck in the data. 1415789Sahrens */ 1416789Sahrens if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) { 1417789Sahrens lr_write_t *lrw = (lr_write_t *)lr; 1418789Sahrens blkptr_t *wbp = &lrw->lr_blkptr; 1419789Sahrens uint64_t wlen = lrw->lr_length; 1420789Sahrens char *wbuf = zr->zr_lrbuf + reclen; 1421789Sahrens 1422789Sahrens if (BP_IS_HOLE(wbp)) { /* compressed to a hole */ 1423789Sahrens bzero(wbuf, wlen); 1424789Sahrens } else { 1425789Sahrens /* 1426789Sahrens * A subsequent write may have overwritten this block, 1427789Sahrens * in which case wbp may have been been freed and 1428789Sahrens * reallocated, and our read of wbp may fail with a 1429789Sahrens * checksum error. We can safely ignore this because 1430789Sahrens * the later write will provide the correct data. 1431789Sahrens */ 14321544Seschrock zbookmark_t zb; 14331544Seschrock 14341544Seschrock zb.zb_objset = dmu_objset_id(zilog->zl_os); 14351544Seschrock zb.zb_object = lrw->lr_foid; 14361544Seschrock zb.zb_level = -1; 14371544Seschrock zb.zb_blkid = lrw->lr_offset / BP_GET_LSIZE(wbp); 14381544Seschrock 1439789Sahrens (void) zio_wait(zio_read(NULL, zilog->zl_spa, 1440789Sahrens wbp, wbuf, BP_GET_LSIZE(wbp), NULL, NULL, 1441789Sahrens ZIO_PRIORITY_SYNC_READ, 14421544Seschrock ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb)); 1443789Sahrens (void) memmove(wbuf, wbuf + lrw->lr_blkoff, wlen); 1444789Sahrens } 1445789Sahrens } 1446789Sahrens 1447789Sahrens /* 1448789Sahrens * We must now do two things atomically: replay this log record, 1449789Sahrens * and update the log header to reflect the fact that we did so. 1450789Sahrens * We use the DMU's ability to assign into a specific txg to do this. 1451789Sahrens */ 14523063Sperrin for (pass = 1, sunk = B_FALSE; /* CONSTANTCONDITION */; pass++) { 1453789Sahrens uint64_t replay_txg; 1454789Sahrens dmu_tx_t *replay_tx; 1455789Sahrens 1456789Sahrens replay_tx = dmu_tx_create(zr->zr_os); 1457789Sahrens error = dmu_tx_assign(replay_tx, TXG_WAIT); 1458789Sahrens if (error) { 1459789Sahrens dmu_tx_abort(replay_tx); 1460789Sahrens break; 1461789Sahrens } 1462789Sahrens 1463789Sahrens replay_txg = dmu_tx_get_txg(replay_tx); 1464789Sahrens 1465789Sahrens if (txtype == 0 || txtype >= TX_MAX_TYPE) { 1466789Sahrens error = EINVAL; 1467789Sahrens } else { 1468789Sahrens /* 1469789Sahrens * On the first pass, arrange for the replay vector 1470789Sahrens * to fail its dmu_tx_assign(). That's the only way 1471789Sahrens * to ensure that those code paths remain well tested. 14725676Sperrin * 14735676Sperrin * Only byteswap (if needed) on the 1st pass. 1474789Sahrens */ 1475789Sahrens *zr->zr_txgp = replay_txg - (pass == 1); 1476789Sahrens error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lrbuf, 14775676Sperrin zr->zr_byteswap && pass == 1); 1478789Sahrens *zr->zr_txgp = TXG_NOWAIT; 1479789Sahrens } 1480789Sahrens 1481789Sahrens if (error == 0) { 1482789Sahrens dsl_dataset_dirty(dmu_objset_ds(zr->zr_os), replay_tx); 1483789Sahrens zilog->zl_replay_seq[replay_txg & TXG_MASK] = 1484789Sahrens lr->lrc_seq; 1485789Sahrens } 1486789Sahrens 1487789Sahrens dmu_tx_commit(replay_tx); 1488789Sahrens 14893063Sperrin if (!error) 14903063Sperrin return; 14913063Sperrin 14923063Sperrin /* 14933063Sperrin * The DMU's dnode layer doesn't see removes until the txg 14943063Sperrin * commits, so a subsequent claim can spuriously fail with 14953063Sperrin * EEXIST. So if we receive any error other than ERESTART 14963063Sperrin * we try syncing out any removes then retrying the 14973063Sperrin * transaction. 14983063Sperrin */ 14993063Sperrin if (error != ERESTART && !sunk) { 15003063Sperrin txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0); 15013063Sperrin sunk = B_TRUE; 15023063Sperrin continue; /* retry */ 15033063Sperrin } 15043063Sperrin 1505789Sahrens if (error != ERESTART) 1506789Sahrens break; 1507789Sahrens 1508789Sahrens if (pass != 1) 1509789Sahrens txg_wait_open(spa_get_dsl(zilog->zl_spa), 1510789Sahrens replay_txg + 1); 1511789Sahrens 1512789Sahrens dprintf("pass %d, retrying\n", pass); 1513789Sahrens } 1514789Sahrens 15153063Sperrin ASSERT(error && error != ERESTART); 15163063Sperrin name = kmem_alloc(MAXNAMELEN, KM_SLEEP); 15173063Sperrin dmu_objset_name(zr->zr_os, name); 15183063Sperrin cmn_err(CE_WARN, "ZFS replay transaction error %d, " 15195331Samw "dataset %s, seq 0x%llx, txtype %llu %s\n", 15205331Samw error, name, (u_longlong_t)lr->lrc_seq, (u_longlong_t)txtype, 15215331Samw (lr->lrc_txtype & TX_CI) ? "CI" : ""); 15223063Sperrin zilog->zl_stop_replay = 1; 15233063Sperrin kmem_free(name, MAXNAMELEN); 15243063Sperrin } 1525789Sahrens 15263063Sperrin /* ARGSUSED */ 15273063Sperrin static void 15283063Sperrin zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg) 15293063Sperrin { 15303063Sperrin zilog->zl_replay_blks++; 1531789Sahrens } 1532789Sahrens 1533789Sahrens /* 15341362Sperrin * If this dataset has a non-empty intent log, replay it and destroy it. 1535789Sahrens */ 1536789Sahrens void 1537789Sahrens zil_replay(objset_t *os, void *arg, uint64_t *txgp, 15383461Sahrens zil_replay_func_t *replay_func[TX_MAX_TYPE]) 1539789Sahrens { 1540789Sahrens zilog_t *zilog = dmu_objset_zil(os); 15411807Sbonwick const zil_header_t *zh = zilog->zl_header; 15421807Sbonwick zil_replay_arg_t zr; 15431362Sperrin 15441362Sperrin if (zil_empty(zilog)) { 15451807Sbonwick zil_destroy(zilog, B_TRUE); 15461362Sperrin return; 15471362Sperrin } 1548789Sahrens 1549789Sahrens zr.zr_os = os; 1550789Sahrens zr.zr_replay = replay_func; 1551789Sahrens zr.zr_arg = arg; 1552789Sahrens zr.zr_txgp = txgp; 15531807Sbonwick zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log); 1554789Sahrens zr.zr_lrbuf = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP); 1555789Sahrens 1556789Sahrens /* 1557789Sahrens * Wait for in-progress removes to sync before starting replay. 1558789Sahrens */ 1559789Sahrens txg_wait_synced(zilog->zl_dmu_pool, 0); 1560789Sahrens 1561789Sahrens zilog->zl_stop_replay = 0; 15623063Sperrin zilog->zl_replay_time = lbolt; 15633063Sperrin ASSERT(zilog->zl_replay_blks == 0); 15643063Sperrin (void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr, 15651807Sbonwick zh->zh_claim_txg); 1566789Sahrens kmem_free(zr.zr_lrbuf, 2 * SPA_MAXBLOCKSIZE); 1567789Sahrens 15681807Sbonwick zil_destroy(zilog, B_FALSE); 15695712Sahrens txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 1570789Sahrens } 15711646Sperrin 15721646Sperrin /* 15731646Sperrin * Report whether all transactions are committed 15741646Sperrin */ 15751646Sperrin int 15761646Sperrin zil_is_committed(zilog_t *zilog) 15771646Sperrin { 15781646Sperrin lwb_t *lwb; 15792638Sperrin int ret; 15801646Sperrin 15812638Sperrin mutex_enter(&zilog->zl_lock); 15822638Sperrin while (zilog->zl_writer) 15832638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 15842638Sperrin 15852638Sperrin /* recent unpushed intent log transactions? */ 15862638Sperrin if (!list_is_empty(&zilog->zl_itx_list)) { 15872638Sperrin ret = B_FALSE; 15882638Sperrin goto out; 15892638Sperrin } 15902638Sperrin 15912638Sperrin /* intent log never used? */ 15922638Sperrin lwb = list_head(&zilog->zl_lwb_list); 15932638Sperrin if (lwb == NULL) { 15942638Sperrin ret = B_TRUE; 15952638Sperrin goto out; 15962638Sperrin } 15971646Sperrin 15981646Sperrin /* 15992638Sperrin * more than 1 log buffer means zil_sync() hasn't yet freed 16002638Sperrin * entries after a txg has committed 16011646Sperrin */ 16022638Sperrin if (list_next(&zilog->zl_lwb_list, lwb)) { 16032638Sperrin ret = B_FALSE; 16042638Sperrin goto out; 16052638Sperrin } 16062638Sperrin 16071646Sperrin ASSERT(zil_empty(zilog)); 16082638Sperrin ret = B_TRUE; 16092638Sperrin out: 16102638Sperrin cv_broadcast(&zilog->zl_cv_writer); 16112638Sperrin mutex_exit(&zilog->zl_lock); 16122638Sperrin return (ret); 16131646Sperrin } 1614