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 #include <sys/zfs_context.h> 27789Sahrens #include <sys/spa.h> 28789Sahrens #include <sys/dmu.h> 29789Sahrens #include <sys/zap.h> 30789Sahrens #include <sys/arc.h> 31789Sahrens #include <sys/stat.h> 32789Sahrens #include <sys/resource.h> 33789Sahrens #include <sys/zil.h> 34789Sahrens #include <sys/zil_impl.h> 35789Sahrens #include <sys/dsl_dataset.h> 36789Sahrens #include <sys/vdev.h> 373668Sgw25295 #include <sys/dmu_tx.h> 38789Sahrens 39789Sahrens /* 40789Sahrens * The zfs intent log (ZIL) saves transaction records of system calls 41789Sahrens * that change the file system in memory with enough information 42789Sahrens * to be able to replay them. These are stored in memory until 43789Sahrens * either the DMU transaction group (txg) commits them to the stable pool 44789Sahrens * and they can be discarded, or they are flushed to the stable log 45789Sahrens * (also in the pool) due to a fsync, O_DSYNC or other synchronous 46789Sahrens * requirement. In the event of a panic or power fail then those log 47789Sahrens * records (transactions) are replayed. 48789Sahrens * 49789Sahrens * There is one ZIL per file system. Its on-disk (pool) format consists 50789Sahrens * of 3 parts: 51789Sahrens * 52789Sahrens * - ZIL header 53789Sahrens * - ZIL blocks 54789Sahrens * - ZIL records 55789Sahrens * 56789Sahrens * A log record holds a system call transaction. Log blocks can 57789Sahrens * hold many log records and the blocks are chained together. 58789Sahrens * Each ZIL block contains a block pointer (blkptr_t) to the next 59789Sahrens * ZIL block in the chain. The ZIL header points to the first 60789Sahrens * block in the chain. Note there is not a fixed place in the pool 61789Sahrens * to hold blocks. They are dynamically allocated and freed as 62789Sahrens * needed from the blocks available. Figure X shows the ZIL structure: 63789Sahrens */ 64789Sahrens 65789Sahrens /* 662986Sek110237 * This global ZIL switch affects all pools 67789Sahrens */ 68789Sahrens int zil_disable = 0; /* disable intent logging */ 692986Sek110237 702986Sek110237 /* 712986Sek110237 * Tunable parameter for debugging or performance analysis. Setting 722986Sek110237 * zfs_nocacheflush will cause corruption on power loss if a volatile 732986Sek110237 * out-of-order write cache is enabled. 742986Sek110237 */ 752986Sek110237 boolean_t zfs_nocacheflush = B_FALSE; 76789Sahrens 77789Sahrens static kmem_cache_t *zil_lwb_cache; 78789Sahrens 79789Sahrens static int 80789Sahrens zil_dva_compare(const void *x1, const void *x2) 81789Sahrens { 82789Sahrens const dva_t *dva1 = x1; 83789Sahrens const dva_t *dva2 = x2; 84789Sahrens 85789Sahrens if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2)) 86789Sahrens return (-1); 87789Sahrens if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2)) 88789Sahrens return (1); 89789Sahrens 90789Sahrens if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2)) 91789Sahrens return (-1); 92789Sahrens if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2)) 93789Sahrens return (1); 94789Sahrens 95789Sahrens return (0); 96789Sahrens } 97789Sahrens 98789Sahrens static void 99789Sahrens zil_dva_tree_init(avl_tree_t *t) 100789Sahrens { 101789Sahrens avl_create(t, zil_dva_compare, sizeof (zil_dva_node_t), 102789Sahrens offsetof(zil_dva_node_t, zn_node)); 103789Sahrens } 104789Sahrens 105789Sahrens static void 106789Sahrens zil_dva_tree_fini(avl_tree_t *t) 107789Sahrens { 108789Sahrens zil_dva_node_t *zn; 109789Sahrens void *cookie = NULL; 110789Sahrens 111789Sahrens while ((zn = avl_destroy_nodes(t, &cookie)) != NULL) 112789Sahrens kmem_free(zn, sizeof (zil_dva_node_t)); 113789Sahrens 114789Sahrens avl_destroy(t); 115789Sahrens } 116789Sahrens 117789Sahrens static int 118789Sahrens zil_dva_tree_add(avl_tree_t *t, dva_t *dva) 119789Sahrens { 120789Sahrens zil_dva_node_t *zn; 121789Sahrens avl_index_t where; 122789Sahrens 123789Sahrens if (avl_find(t, dva, &where) != NULL) 124789Sahrens return (EEXIST); 125789Sahrens 126789Sahrens zn = kmem_alloc(sizeof (zil_dva_node_t), KM_SLEEP); 127789Sahrens zn->zn_dva = *dva; 128789Sahrens avl_insert(t, zn, where); 129789Sahrens 130789Sahrens return (0); 131789Sahrens } 132789Sahrens 1331807Sbonwick static zil_header_t * 1341807Sbonwick zil_header_in_syncing_context(zilog_t *zilog) 1351807Sbonwick { 1361807Sbonwick return ((zil_header_t *)zilog->zl_header); 1371807Sbonwick } 1381807Sbonwick 1391807Sbonwick static void 1401807Sbonwick zil_init_log_chain(zilog_t *zilog, blkptr_t *bp) 1411807Sbonwick { 1421807Sbonwick zio_cksum_t *zc = &bp->blk_cksum; 1431807Sbonwick 1441807Sbonwick zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL); 1451807Sbonwick zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL); 1461807Sbonwick zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os); 1471807Sbonwick zc->zc_word[ZIL_ZC_SEQ] = 1ULL; 1481807Sbonwick } 1491807Sbonwick 150789Sahrens /* 151789Sahrens * Read a log block, make sure it's valid, and byteswap it if necessary. 152789Sahrens */ 153789Sahrens static int 1541807Sbonwick zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, arc_buf_t **abufpp) 155789Sahrens { 1561807Sbonwick blkptr_t blk = *bp; 1571544Seschrock zbookmark_t zb; 1582391Smaybee uint32_t aflags = ARC_WAIT; 159789Sahrens int error; 160789Sahrens 1611807Sbonwick zb.zb_objset = bp->blk_cksum.zc_word[ZIL_ZC_OBJSET]; 1621544Seschrock zb.zb_object = 0; 1631544Seschrock zb.zb_level = -1; 1641807Sbonwick zb.zb_blkid = bp->blk_cksum.zc_word[ZIL_ZC_SEQ]; 1651807Sbonwick 1661807Sbonwick *abufpp = NULL; 1671807Sbonwick 1687046Sahrens /* 1697046Sahrens * We shouldn't be doing any scrubbing while we're doing log 1707046Sahrens * replay, it's OK to not lock. 1717046Sahrens */ 1727046Sahrens error = arc_read_nolock(NULL, zilog->zl_spa, &blk, 1731807Sbonwick arc_getbuf_func, abufpp, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL | 1742391Smaybee ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB, &aflags, &zb); 1751807Sbonwick 1761807Sbonwick if (error == 0) { 1771807Sbonwick char *data = (*abufpp)->b_data; 1781807Sbonwick uint64_t blksz = BP_GET_LSIZE(bp); 1791807Sbonwick zil_trailer_t *ztp = (zil_trailer_t *)(data + blksz) - 1; 1801807Sbonwick zio_cksum_t cksum = bp->blk_cksum; 1811544Seschrock 1821807Sbonwick /* 1837522SNeil.Perrin@Sun.COM * Validate the checksummed log block. 1847522SNeil.Perrin@Sun.COM * 1851807Sbonwick * Sequence numbers should be... sequential. The checksum 1861807Sbonwick * verifier for the next block should be bp's checksum plus 1. 1877522SNeil.Perrin@Sun.COM * 1887522SNeil.Perrin@Sun.COM * Also check the log chain linkage and size used. 1891807Sbonwick */ 1901807Sbonwick cksum.zc_word[ZIL_ZC_SEQ]++; 1911807Sbonwick 1927522SNeil.Perrin@Sun.COM if (bcmp(&cksum, &ztp->zit_next_blk.blk_cksum, 1937522SNeil.Perrin@Sun.COM sizeof (cksum)) || BP_IS_HOLE(&ztp->zit_next_blk) || 1947522SNeil.Perrin@Sun.COM (ztp->zit_nused > (blksz - sizeof (zil_trailer_t)))) { 1957522SNeil.Perrin@Sun.COM error = ECKSUM; 1967522SNeil.Perrin@Sun.COM } 1971807Sbonwick 1981807Sbonwick if (error) { 1991807Sbonwick VERIFY(arc_buf_remove_ref(*abufpp, abufpp) == 1); 2001807Sbonwick *abufpp = NULL; 2011807Sbonwick } 202789Sahrens } 203789Sahrens 2041807Sbonwick dprintf("error %d on %llu:%llu\n", error, zb.zb_objset, zb.zb_blkid); 205789Sahrens 2061807Sbonwick return (error); 207789Sahrens } 208789Sahrens 209789Sahrens /* 210789Sahrens * Parse the intent log, and call parse_func for each valid record within. 2111807Sbonwick * Return the highest sequence number. 212789Sahrens */ 2131807Sbonwick uint64_t 214789Sahrens zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func, 215789Sahrens zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg) 216789Sahrens { 2171807Sbonwick const zil_header_t *zh = zilog->zl_header; 2181807Sbonwick uint64_t claim_seq = zh->zh_claim_seq; 2191807Sbonwick uint64_t seq = 0; 2201807Sbonwick uint64_t max_seq = 0; 2211807Sbonwick blkptr_t blk = zh->zh_log; 2221807Sbonwick arc_buf_t *abuf; 223789Sahrens char *lrbuf, *lrp; 224789Sahrens zil_trailer_t *ztp; 225789Sahrens int reclen, error; 226789Sahrens 227789Sahrens if (BP_IS_HOLE(&blk)) 2281807Sbonwick return (max_seq); 229789Sahrens 230789Sahrens /* 231789Sahrens * Starting at the block pointed to by zh_log we read the log chain. 232789Sahrens * For each block in the chain we strongly check that block to 233789Sahrens * ensure its validity. We stop when an invalid block is found. 234789Sahrens * For each block pointer in the chain we call parse_blk_func(). 235789Sahrens * For each record in each valid block we call parse_lr_func(). 2361807Sbonwick * If the log has been claimed, stop if we encounter a sequence 2371807Sbonwick * number greater than the highest claimed sequence number. 238789Sahrens */ 239789Sahrens zil_dva_tree_init(&zilog->zl_dva_tree); 240789Sahrens for (;;) { 2411807Sbonwick seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 2421807Sbonwick 2431807Sbonwick if (claim_seq != 0 && seq > claim_seq) 2441807Sbonwick break; 2451807Sbonwick 2461807Sbonwick ASSERT(max_seq < seq); 2471807Sbonwick max_seq = seq; 2481807Sbonwick 2491807Sbonwick error = zil_read_log_block(zilog, &blk, &abuf); 250789Sahrens 251789Sahrens if (parse_blk_func != NULL) 252789Sahrens parse_blk_func(zilog, &blk, arg, txg); 253789Sahrens 254789Sahrens if (error) 255789Sahrens break; 256789Sahrens 2571807Sbonwick lrbuf = abuf->b_data; 258789Sahrens ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1; 259789Sahrens blk = ztp->zit_next_blk; 260789Sahrens 2611807Sbonwick if (parse_lr_func == NULL) { 2621807Sbonwick VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 263789Sahrens continue; 2641807Sbonwick } 265789Sahrens 266789Sahrens for (lrp = lrbuf; lrp < lrbuf + ztp->zit_nused; lrp += reclen) { 267789Sahrens lr_t *lr = (lr_t *)lrp; 268789Sahrens reclen = lr->lrc_reclen; 269789Sahrens ASSERT3U(reclen, >=, sizeof (lr_t)); 270789Sahrens parse_lr_func(zilog, lr, arg, txg); 271789Sahrens } 2721807Sbonwick VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 273789Sahrens } 274789Sahrens zil_dva_tree_fini(&zilog->zl_dva_tree); 2751807Sbonwick 2761807Sbonwick return (max_seq); 277789Sahrens } 278789Sahrens 279789Sahrens /* ARGSUSED */ 280789Sahrens static void 281789Sahrens zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg) 282789Sahrens { 283789Sahrens spa_t *spa = zilog->zl_spa; 284789Sahrens int err; 285789Sahrens 286789Sahrens /* 287789Sahrens * Claim log block if not already committed and not already claimed. 288789Sahrens */ 289789Sahrens if (bp->blk_birth >= first_txg && 290789Sahrens zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp)) == 0) { 291789Sahrens err = zio_wait(zio_claim(NULL, spa, first_txg, bp, NULL, NULL)); 292789Sahrens ASSERT(err == 0); 293789Sahrens } 294789Sahrens } 295789Sahrens 296789Sahrens static void 297789Sahrens zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg) 298789Sahrens { 299789Sahrens if (lrc->lrc_txtype == TX_WRITE) { 300789Sahrens lr_write_t *lr = (lr_write_t *)lrc; 301789Sahrens zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg); 302789Sahrens } 303789Sahrens } 304789Sahrens 305789Sahrens /* ARGSUSED */ 306789Sahrens static void 307789Sahrens zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg) 308789Sahrens { 309789Sahrens zio_free_blk(zilog->zl_spa, bp, dmu_tx_get_txg(tx)); 310789Sahrens } 311789Sahrens 312789Sahrens static void 313789Sahrens zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg) 314789Sahrens { 315789Sahrens /* 316789Sahrens * If we previously claimed it, we need to free it. 317789Sahrens */ 318789Sahrens if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE) { 319789Sahrens lr_write_t *lr = (lr_write_t *)lrc; 320789Sahrens blkptr_t *bp = &lr->lr_blkptr; 321789Sahrens if (bp->blk_birth >= claim_txg && 322789Sahrens !zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp))) { 323789Sahrens (void) arc_free(NULL, zilog->zl_spa, 324789Sahrens dmu_tx_get_txg(tx), bp, NULL, NULL, ARC_WAIT); 325789Sahrens } 326789Sahrens } 327789Sahrens } 328789Sahrens 329789Sahrens /* 330789Sahrens * Create an on-disk intent log. 331789Sahrens */ 332789Sahrens static void 333789Sahrens zil_create(zilog_t *zilog) 334789Sahrens { 3351807Sbonwick const zil_header_t *zh = zilog->zl_header; 336789Sahrens lwb_t *lwb; 3371807Sbonwick uint64_t txg = 0; 3381807Sbonwick dmu_tx_t *tx = NULL; 339789Sahrens blkptr_t blk; 3401807Sbonwick int error = 0; 341789Sahrens 342789Sahrens /* 3431807Sbonwick * Wait for any previous destroy to complete. 344789Sahrens */ 3451807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 3461807Sbonwick 3471807Sbonwick ASSERT(zh->zh_claim_txg == 0); 3481807Sbonwick ASSERT(zh->zh_replay_seq == 0); 3491807Sbonwick 3501807Sbonwick blk = zh->zh_log; 351789Sahrens 352789Sahrens /* 3531807Sbonwick * If we don't already have an initial log block, allocate one now. 354789Sahrens */ 3551807Sbonwick if (BP_IS_HOLE(&blk)) { 3561807Sbonwick tx = dmu_tx_create(zilog->zl_os); 3571807Sbonwick (void) dmu_tx_assign(tx, TXG_WAIT); 3581807Sbonwick dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 3591807Sbonwick txg = dmu_tx_get_txg(tx); 3601807Sbonwick 3613063Sperrin error = zio_alloc_blk(zilog->zl_spa, ZIL_MIN_BLKSZ, &blk, 3623063Sperrin NULL, txg); 3631807Sbonwick 3641807Sbonwick if (error == 0) 3651807Sbonwick zil_init_log_chain(zilog, &blk); 3661362Sperrin } 3671807Sbonwick 3681807Sbonwick /* 3691807Sbonwick * Allocate a log write buffer (lwb) for the first log block. 3701807Sbonwick */ 371789Sahrens if (error == 0) { 372789Sahrens lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 373789Sahrens lwb->lwb_zilog = zilog; 374789Sahrens lwb->lwb_blk = blk; 375789Sahrens lwb->lwb_nused = 0; 376789Sahrens lwb->lwb_sz = BP_GET_LSIZE(&lwb->lwb_blk); 377789Sahrens lwb->lwb_buf = zio_buf_alloc(lwb->lwb_sz); 378789Sahrens lwb->lwb_max_txg = txg; 3792237Smaybee lwb->lwb_zio = NULL; 3802237Smaybee 381789Sahrens mutex_enter(&zilog->zl_lock); 382789Sahrens list_insert_tail(&zilog->zl_lwb_list, lwb); 383789Sahrens mutex_exit(&zilog->zl_lock); 384789Sahrens } 385789Sahrens 3861807Sbonwick /* 3871807Sbonwick * If we just allocated the first log block, commit our transaction 3881807Sbonwick * and wait for zil_sync() to stuff the block poiner into zh_log. 3891807Sbonwick * (zh is part of the MOS, so we cannot modify it in open context.) 3901807Sbonwick */ 3911807Sbonwick if (tx != NULL) { 3921807Sbonwick dmu_tx_commit(tx); 3931362Sperrin txg_wait_synced(zilog->zl_dmu_pool, txg); 3941807Sbonwick } 3951807Sbonwick 3961807Sbonwick ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0); 397789Sahrens } 398789Sahrens 399789Sahrens /* 400789Sahrens * In one tx, free all log blocks and clear the log header. 4011807Sbonwick * If keep_first is set, then we're replaying a log with no content. 4021807Sbonwick * We want to keep the first block, however, so that the first 4031807Sbonwick * synchronous transaction doesn't require a txg_wait_synced() 4041807Sbonwick * in zil_create(). We don't need to txg_wait_synced() here either 4051807Sbonwick * when keep_first is set, because both zil_create() and zil_destroy() 4061807Sbonwick * will wait for any in-progress destroys to complete. 407789Sahrens */ 408789Sahrens void 4091807Sbonwick zil_destroy(zilog_t *zilog, boolean_t keep_first) 410789Sahrens { 4111807Sbonwick const zil_header_t *zh = zilog->zl_header; 4121807Sbonwick lwb_t *lwb; 413789Sahrens dmu_tx_t *tx; 414789Sahrens uint64_t txg; 415789Sahrens 4161807Sbonwick /* 4171807Sbonwick * Wait for any previous destroy to complete. 4181807Sbonwick */ 4191807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 420789Sahrens 4211807Sbonwick if (BP_IS_HOLE(&zh->zh_log)) 422789Sahrens return; 423789Sahrens 424789Sahrens tx = dmu_tx_create(zilog->zl_os); 425789Sahrens (void) dmu_tx_assign(tx, TXG_WAIT); 426789Sahrens dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 427789Sahrens txg = dmu_tx_get_txg(tx); 428789Sahrens 4291807Sbonwick mutex_enter(&zilog->zl_lock); 4301807Sbonwick 4315223Sperrin /* 4325223Sperrin * It is possible for the ZIL to get the previously mounted zilog 4335223Sperrin * structure of the same dataset if quickly remounted and the dbuf 4345223Sperrin * eviction has not completed. In this case we can see a non 4355223Sperrin * empty lwb list and keep_first will be set. We fix this by 4365223Sperrin * clearing the keep_first. This will be slower but it's very rare. 4375223Sperrin */ 4385223Sperrin if (!list_is_empty(&zilog->zl_lwb_list) && keep_first) 4395223Sperrin keep_first = B_FALSE; 4405223Sperrin 4411807Sbonwick ASSERT3U(zilog->zl_destroy_txg, <, txg); 442789Sahrens zilog->zl_destroy_txg = txg; 4431807Sbonwick zilog->zl_keep_first = keep_first; 4441807Sbonwick 4451807Sbonwick if (!list_is_empty(&zilog->zl_lwb_list)) { 4461807Sbonwick ASSERT(zh->zh_claim_txg == 0); 4471807Sbonwick ASSERT(!keep_first); 4481807Sbonwick while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 4491807Sbonwick list_remove(&zilog->zl_lwb_list, lwb); 4501807Sbonwick if (lwb->lwb_buf != NULL) 4511807Sbonwick zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 4521807Sbonwick zio_free_blk(zilog->zl_spa, &lwb->lwb_blk, txg); 4531807Sbonwick kmem_cache_free(zil_lwb_cache, lwb); 4541807Sbonwick } 4551807Sbonwick } else { 4561807Sbonwick if (!keep_first) { 4571807Sbonwick (void) zil_parse(zilog, zil_free_log_block, 4581807Sbonwick zil_free_log_record, tx, zh->zh_claim_txg); 4591807Sbonwick } 4601807Sbonwick } 4612638Sperrin mutex_exit(&zilog->zl_lock); 462789Sahrens 463789Sahrens dmu_tx_commit(tx); 464789Sahrens } 465789Sahrens 4664935Sperrin /* 4674935Sperrin * zil_rollback_destroy() is only called by the rollback code. 4684935Sperrin * We already have a syncing tx. Rollback has exclusive access to the 4694935Sperrin * dataset, so we don't have to worry about concurrent zil access. 4704935Sperrin * The actual freeing of any log blocks occurs in zil_sync() later in 4714935Sperrin * this txg syncing phase. 4724935Sperrin */ 4734935Sperrin void 4744935Sperrin zil_rollback_destroy(zilog_t *zilog, dmu_tx_t *tx) 4754935Sperrin { 4764935Sperrin const zil_header_t *zh = zilog->zl_header; 4774935Sperrin uint64_t txg; 4784935Sperrin 4794935Sperrin if (BP_IS_HOLE(&zh->zh_log)) 4804935Sperrin return; 4814935Sperrin 4824935Sperrin txg = dmu_tx_get_txg(tx); 4834935Sperrin ASSERT3U(zilog->zl_destroy_txg, <, txg); 4844935Sperrin zilog->zl_destroy_txg = txg; 4854935Sperrin zilog->zl_keep_first = B_FALSE; 4864935Sperrin 4875809Sperrin /* 4885809Sperrin * Ensure there's no outstanding ZIL IO. No lwbs or just the 4895809Sperrin * unused one that allocated in advance is ok. 4905809Sperrin */ 4915809Sperrin ASSERT(zilog->zl_lwb_list.list_head.list_next == 4925809Sperrin zilog->zl_lwb_list.list_head.list_prev); 4934935Sperrin (void) zil_parse(zilog, zil_free_log_block, zil_free_log_record, 4944935Sperrin tx, zh->zh_claim_txg); 4954935Sperrin } 4964935Sperrin 4972199Sahrens int 498789Sahrens zil_claim(char *osname, void *txarg) 499789Sahrens { 500789Sahrens dmu_tx_t *tx = txarg; 501789Sahrens uint64_t first_txg = dmu_tx_get_txg(tx); 502789Sahrens zilog_t *zilog; 503789Sahrens zil_header_t *zh; 504789Sahrens objset_t *os; 505789Sahrens int error; 506789Sahrens 5076689Smaybee error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_USER, &os); 508789Sahrens if (error) { 5097294Sperrin cmn_err(CE_WARN, "can't open objset for %s", osname); 5102199Sahrens return (0); 511789Sahrens } 512789Sahrens 513789Sahrens zilog = dmu_objset_zil(os); 5141807Sbonwick zh = zil_header_in_syncing_context(zilog); 515789Sahrens 516789Sahrens /* 5171807Sbonwick * Claim all log blocks if we haven't already done so, and remember 5181807Sbonwick * the highest claimed sequence number. This ensures that if we can 5191807Sbonwick * read only part of the log now (e.g. due to a missing device), 5201807Sbonwick * but we can read the entire log later, we will not try to replay 5211807Sbonwick * or destroy beyond the last block we successfully claimed. 522789Sahrens */ 523789Sahrens ASSERT3U(zh->zh_claim_txg, <=, first_txg); 524789Sahrens if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) { 525789Sahrens zh->zh_claim_txg = first_txg; 5261807Sbonwick zh->zh_claim_seq = zil_parse(zilog, zil_claim_log_block, 5271807Sbonwick zil_claim_log_record, tx, first_txg); 528789Sahrens dsl_dataset_dirty(dmu_objset_ds(os), tx); 529789Sahrens } 5301807Sbonwick 531789Sahrens ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1)); 532789Sahrens dmu_objset_close(os); 5332199Sahrens return (0); 534789Sahrens } 535789Sahrens 5367294Sperrin /* 5377294Sperrin * Check the log by walking the log chain. 5387294Sperrin * Checksum errors are ok as they indicate the end of the chain. 5397294Sperrin * Any other error (no device or read failure) returns an error. 5407294Sperrin */ 5417294Sperrin /* ARGSUSED */ 5427294Sperrin int 5437294Sperrin zil_check_log_chain(char *osname, void *txarg) 5447294Sperrin { 5457294Sperrin zilog_t *zilog; 5467294Sperrin zil_header_t *zh; 5477294Sperrin blkptr_t blk; 5487294Sperrin arc_buf_t *abuf; 5497294Sperrin objset_t *os; 5507294Sperrin char *lrbuf; 5517294Sperrin zil_trailer_t *ztp; 5527294Sperrin int error; 5537294Sperrin 5547294Sperrin error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_USER, &os); 5557294Sperrin if (error) { 5567294Sperrin cmn_err(CE_WARN, "can't open objset for %s", osname); 5577294Sperrin return (0); 5587294Sperrin } 5597294Sperrin 5607294Sperrin zilog = dmu_objset_zil(os); 5617294Sperrin zh = zil_header_in_syncing_context(zilog); 5627294Sperrin blk = zh->zh_log; 5637294Sperrin if (BP_IS_HOLE(&blk)) { 5647294Sperrin dmu_objset_close(os); 5657294Sperrin return (0); /* no chain */ 5667294Sperrin } 5677294Sperrin 5687294Sperrin for (;;) { 5697294Sperrin error = zil_read_log_block(zilog, &blk, &abuf); 5707294Sperrin if (error) 5717294Sperrin break; 5727294Sperrin lrbuf = abuf->b_data; 5737294Sperrin ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1; 5747294Sperrin blk = ztp->zit_next_blk; 5757294Sperrin VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 5767294Sperrin } 5777294Sperrin dmu_objset_close(os); 5787294Sperrin if (error == ECKSUM) 5797294Sperrin return (0); /* normal end of chain */ 5807294Sperrin return (error); 5817294Sperrin } 5827294Sperrin 5837294Sperrin /* 5847294Sperrin * Clear a log chain 5857294Sperrin */ 5867294Sperrin /* ARGSUSED */ 5877294Sperrin int 5887294Sperrin zil_clear_log_chain(char *osname, void *txarg) 5897294Sperrin { 5907294Sperrin zilog_t *zilog; 5917294Sperrin zil_header_t *zh; 5927294Sperrin objset_t *os; 5937294Sperrin dmu_tx_t *tx; 5947294Sperrin int error; 5957294Sperrin 5967294Sperrin error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_USER, &os); 5977294Sperrin if (error) { 5987294Sperrin cmn_err(CE_WARN, "can't open objset for %s", osname); 5997294Sperrin return (0); 6007294Sperrin } 6017294Sperrin 6027294Sperrin zilog = dmu_objset_zil(os); 6037294Sperrin tx = dmu_tx_create(zilog->zl_os); 6047294Sperrin (void) dmu_tx_assign(tx, TXG_WAIT); 6057294Sperrin zh = zil_header_in_syncing_context(zilog); 6067294Sperrin BP_ZERO(&zh->zh_log); 6077294Sperrin dsl_dataset_dirty(dmu_objset_ds(os), tx); 6087294Sperrin dmu_tx_commit(tx); 6097294Sperrin dmu_objset_close(os); 6107294Sperrin return (0); 6117294Sperrin } 6127294Sperrin 6135688Sbonwick static int 6145688Sbonwick zil_vdev_compare(const void *x1, const void *x2) 615789Sahrens { 6165875Sperrin uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev; 6175875Sperrin uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev; 6185688Sbonwick 6195688Sbonwick if (v1 < v2) 6205688Sbonwick return (-1); 6215688Sbonwick if (v1 > v2) 6225688Sbonwick return (1); 6235688Sbonwick 6245688Sbonwick return (0); 6255688Sbonwick } 6265688Sbonwick 6275688Sbonwick void 6285688Sbonwick zil_add_block(zilog_t *zilog, blkptr_t *bp) 6295688Sbonwick { 6305688Sbonwick avl_tree_t *t = &zilog->zl_vdev_tree; 6315688Sbonwick avl_index_t where; 6325688Sbonwick zil_vdev_node_t *zv, zvsearch; 6335688Sbonwick int ndvas = BP_GET_NDVAS(bp); 6345688Sbonwick int i; 635789Sahrens 6362986Sek110237 if (zfs_nocacheflush) 637789Sahrens return; 638789Sahrens 6395688Sbonwick ASSERT(zilog->zl_writer); 6405688Sbonwick 6415688Sbonwick /* 6425688Sbonwick * Even though we're zl_writer, we still need a lock because the 6435688Sbonwick * zl_get_data() callbacks may have dmu_sync() done callbacks 6445688Sbonwick * that will run concurrently. 6455688Sbonwick */ 6465688Sbonwick mutex_enter(&zilog->zl_vdev_lock); 6475688Sbonwick for (i = 0; i < ndvas; i++) { 6485688Sbonwick zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]); 6495688Sbonwick if (avl_find(t, &zvsearch, &where) == NULL) { 6505688Sbonwick zv = kmem_alloc(sizeof (*zv), KM_SLEEP); 6515688Sbonwick zv->zv_vdev = zvsearch.zv_vdev; 6525688Sbonwick avl_insert(t, zv, where); 6533063Sperrin } 6543063Sperrin } 6555688Sbonwick mutex_exit(&zilog->zl_vdev_lock); 6563063Sperrin } 6573063Sperrin 658789Sahrens void 6592638Sperrin zil_flush_vdevs(zilog_t *zilog) 660789Sahrens { 6613063Sperrin spa_t *spa = zilog->zl_spa; 6625688Sbonwick avl_tree_t *t = &zilog->zl_vdev_tree; 6635688Sbonwick void *cookie = NULL; 6645688Sbonwick zil_vdev_node_t *zv; 6655688Sbonwick zio_t *zio; 6663063Sperrin 6673063Sperrin ASSERT(zilog->zl_writer); 668789Sahrens 6695688Sbonwick /* 6705688Sbonwick * We don't need zl_vdev_lock here because we're the zl_writer, 6715688Sbonwick * and all zl_get_data() callbacks are done. 6725688Sbonwick */ 6735688Sbonwick if (avl_numnodes(t) == 0) 6745688Sbonwick return; 6755688Sbonwick 6765688Sbonwick spa_config_enter(spa, RW_READER, FTAG); 6775688Sbonwick 6785688Sbonwick zio = zio_root(spa, NULL, NULL, 6795688Sbonwick ZIO_FLAG_CONFIG_HELD | ZIO_FLAG_CANFAIL); 6805688Sbonwick 6815688Sbonwick while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) { 6825688Sbonwick vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev); 6835688Sbonwick if (vd != NULL) 6845688Sbonwick zio_flush(zio, vd); 6855688Sbonwick kmem_free(zv, sizeof (*zv)); 6863063Sperrin } 687789Sahrens 688789Sahrens /* 689789Sahrens * Wait for all the flushes to complete. Not all devices actually 690789Sahrens * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails. 691789Sahrens */ 6925688Sbonwick (void) zio_wait(zio); 6935688Sbonwick 6945688Sbonwick spa_config_exit(spa, FTAG); 695789Sahrens } 696789Sahrens 697789Sahrens /* 698789Sahrens * Function called when a log block write completes 699789Sahrens */ 700789Sahrens static void 701789Sahrens zil_lwb_write_done(zio_t *zio) 702789Sahrens { 703789Sahrens lwb_t *lwb = zio->io_private; 704789Sahrens zilog_t *zilog = lwb->lwb_zilog; 705789Sahrens 706789Sahrens /* 707789Sahrens * Now that we've written this log block, we have a stable pointer 708789Sahrens * to the next block in the chain, so it's OK to let the txg in 709789Sahrens * which we allocated the next block sync. 710789Sahrens */ 711789Sahrens txg_rele_to_sync(&lwb->lwb_txgh); 712789Sahrens 713789Sahrens zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 714789Sahrens mutex_enter(&zilog->zl_lock); 715789Sahrens lwb->lwb_buf = NULL; 7164527Sperrin if (zio->io_error) 717789Sahrens zilog->zl_log_error = B_TRUE; 718789Sahrens mutex_exit(&zilog->zl_lock); 719789Sahrens } 720789Sahrens 721789Sahrens /* 7222237Smaybee * Initialize the io for a log block. 7232237Smaybee * 7242237Smaybee * Note, we should not initialize the IO until we are about 7252237Smaybee * to use it, since zio_rewrite() does a spa_config_enter(). 7262237Smaybee */ 7272237Smaybee static void 7282237Smaybee zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb) 7292237Smaybee { 7302237Smaybee zbookmark_t zb; 7312237Smaybee 7322237Smaybee zb.zb_objset = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET]; 7332237Smaybee zb.zb_object = 0; 7342237Smaybee zb.zb_level = -1; 7352237Smaybee zb.zb_blkid = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 7362237Smaybee 7372638Sperrin if (zilog->zl_root_zio == NULL) { 7382638Sperrin zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL, 7392638Sperrin ZIO_FLAG_CANFAIL); 7402638Sperrin } 7413063Sperrin if (lwb->lwb_zio == NULL) { 7423063Sperrin lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa, 7437181Sperrin ZIO_CHECKSUM_ZILOG, 0, &lwb->lwb_blk, lwb->lwb_buf, 7443063Sperrin lwb->lwb_sz, zil_lwb_write_done, lwb, 7454527Sperrin ZIO_PRIORITY_LOG_WRITE, ZIO_FLAG_CANFAIL, &zb); 7463063Sperrin } 7472237Smaybee } 7482237Smaybee 7492237Smaybee /* 750789Sahrens * Start a log block write and advance to the next log block. 751789Sahrens * Calls are serialized. 752789Sahrens */ 753789Sahrens static lwb_t * 754789Sahrens zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb) 755789Sahrens { 756789Sahrens lwb_t *nlwb; 757789Sahrens zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1; 7581807Sbonwick spa_t *spa = zilog->zl_spa; 7591807Sbonwick blkptr_t *bp = &ztp->zit_next_blk; 760789Sahrens uint64_t txg; 761789Sahrens uint64_t zil_blksz; 762789Sahrens int error; 763789Sahrens 764789Sahrens ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb)); 765789Sahrens 766789Sahrens /* 767789Sahrens * Allocate the next block and save its address in this block 768789Sahrens * before writing it in order to establish the log chain. 769789Sahrens * Note that if the allocation of nlwb synced before we wrote 770789Sahrens * the block that points at it (lwb), we'd leak it if we crashed. 771789Sahrens * Therefore, we don't do txg_rele_to_sync() until zil_lwb_write_done(). 772789Sahrens */ 773789Sahrens txg = txg_hold_open(zilog->zl_dmu_pool, &lwb->lwb_txgh); 774789Sahrens txg_rele_to_quiesce(&lwb->lwb_txgh); 775789Sahrens 776789Sahrens /* 7771141Sperrin * Pick a ZIL blocksize. We request a size that is the 7781141Sperrin * maximum of the previous used size, the current used size and 7791141Sperrin * the amount waiting in the queue. 780789Sahrens */ 7812237Smaybee zil_blksz = MAX(zilog->zl_prev_used, 7822237Smaybee zilog->zl_cur_used + sizeof (*ztp)); 7831141Sperrin zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp)); 7841842Sperrin zil_blksz = P2ROUNDUP_TYPED(zil_blksz, ZIL_MIN_BLKSZ, uint64_t); 7851141Sperrin if (zil_blksz > ZIL_MAX_BLKSZ) 7861141Sperrin zil_blksz = ZIL_MAX_BLKSZ; 787789Sahrens 7883063Sperrin BP_ZERO(bp); 7893063Sperrin /* pass the old blkptr in order to spread log blocks across devs */ 7903063Sperrin error = zio_alloc_blk(spa, zil_blksz, bp, &lwb->lwb_blk, txg); 791789Sahrens if (error) { 7923668Sgw25295 dmu_tx_t *tx = dmu_tx_create_assigned(zilog->zl_dmu_pool, txg); 7933668Sgw25295 7941544Seschrock /* 7953668Sgw25295 * We dirty the dataset to ensure that zil_sync() will 7963668Sgw25295 * be called to remove this lwb from our zl_lwb_list. 7973668Sgw25295 * Failing to do so, may leave an lwb with a NULL lwb_buf 7983668Sgw25295 * hanging around on the zl_lwb_list. 7993668Sgw25295 */ 8003668Sgw25295 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 8013848Sgw25295 dmu_tx_commit(tx); 8023668Sgw25295 8033668Sgw25295 /* 8043668Sgw25295 * Since we've just experienced an allocation failure so we 8053668Sgw25295 * terminate the current lwb and send it on its way. 8063668Sgw25295 */ 8073668Sgw25295 ztp->zit_pad = 0; 8083668Sgw25295 ztp->zit_nused = lwb->lwb_nused; 8093668Sgw25295 ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum; 8103668Sgw25295 zio_nowait(lwb->lwb_zio); 8113668Sgw25295 8123668Sgw25295 /* 8131544Seschrock * By returning NULL the caller will call tx_wait_synced() 8141544Seschrock */ 815789Sahrens return (NULL); 816789Sahrens } 817789Sahrens 8181807Sbonwick ASSERT3U(bp->blk_birth, ==, txg); 8191544Seschrock ztp->zit_pad = 0; 820789Sahrens ztp->zit_nused = lwb->lwb_nused; 821789Sahrens ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum; 8221807Sbonwick bp->blk_cksum = lwb->lwb_blk.blk_cksum; 8231807Sbonwick bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++; 824789Sahrens 825789Sahrens /* 826789Sahrens * Allocate a new log write buffer (lwb). 827789Sahrens */ 828789Sahrens nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 829789Sahrens 830789Sahrens nlwb->lwb_zilog = zilog; 8311807Sbonwick nlwb->lwb_blk = *bp; 832789Sahrens nlwb->lwb_nused = 0; 833789Sahrens nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk); 834789Sahrens nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz); 835789Sahrens nlwb->lwb_max_txg = txg; 8362237Smaybee nlwb->lwb_zio = NULL; 837789Sahrens 838789Sahrens /* 8393063Sperrin * Put new lwb at the end of the log chain 840789Sahrens */ 841789Sahrens mutex_enter(&zilog->zl_lock); 842789Sahrens list_insert_tail(&zilog->zl_lwb_list, nlwb); 8433063Sperrin mutex_exit(&zilog->zl_lock); 8443063Sperrin 8455688Sbonwick /* Record the block for later vdev flushing */ 8465688Sbonwick zil_add_block(zilog, &lwb->lwb_blk); 847789Sahrens 848789Sahrens /* 8492237Smaybee * kick off the write for the old log block 850789Sahrens */ 8512237Smaybee dprintf_bp(&lwb->lwb_blk, "lwb %p txg %llu: ", lwb, txg); 8523063Sperrin ASSERT(lwb->lwb_zio); 8532237Smaybee zio_nowait(lwb->lwb_zio); 854789Sahrens 855789Sahrens return (nlwb); 856789Sahrens } 857789Sahrens 858789Sahrens static lwb_t * 859789Sahrens zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb) 860789Sahrens { 861789Sahrens lr_t *lrc = &itx->itx_lr; /* common log record */ 8622237Smaybee lr_write_t *lr = (lr_write_t *)lrc; 863789Sahrens uint64_t txg = lrc->lrc_txg; 864789Sahrens uint64_t reclen = lrc->lrc_reclen; 8652237Smaybee uint64_t dlen; 866789Sahrens 867789Sahrens if (lwb == NULL) 868789Sahrens return (NULL); 869789Sahrens ASSERT(lwb->lwb_buf != NULL); 870789Sahrens 8712237Smaybee if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY) 8722237Smaybee dlen = P2ROUNDUP_TYPED( 8732237Smaybee lr->lr_length, sizeof (uint64_t), uint64_t); 8742237Smaybee else 8752237Smaybee dlen = 0; 8761669Sperrin 8771669Sperrin zilog->zl_cur_used += (reclen + dlen); 8781669Sperrin 8793063Sperrin zil_lwb_write_init(zilog, lwb); 8803063Sperrin 8811669Sperrin /* 8821669Sperrin * If this record won't fit in the current log block, start a new one. 8831669Sperrin */ 8841669Sperrin if (lwb->lwb_nused + reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) { 8851669Sperrin lwb = zil_lwb_write_start(zilog, lwb); 8862237Smaybee if (lwb == NULL) 8871669Sperrin return (NULL); 8883063Sperrin zil_lwb_write_init(zilog, lwb); 8891669Sperrin ASSERT(lwb->lwb_nused == 0); 8901669Sperrin if (reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) { 8911669Sperrin txg_wait_synced(zilog->zl_dmu_pool, txg); 892789Sahrens return (lwb); 893789Sahrens } 894789Sahrens } 895789Sahrens 8962638Sperrin /* 8972638Sperrin * Update the lrc_seq, to be log record sequence number. See zil.h 8982638Sperrin * Then copy the record to the log buffer. 8992638Sperrin */ 9002638Sperrin lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */ 901789Sahrens bcopy(lrc, lwb->lwb_buf + lwb->lwb_nused, reclen); 9022237Smaybee 9032237Smaybee /* 9042237Smaybee * If it's a write, fetch the data or get its blkptr as appropriate. 9052237Smaybee */ 9062237Smaybee if (lrc->lrc_txtype == TX_WRITE) { 9072237Smaybee if (txg > spa_freeze_txg(zilog->zl_spa)) 9082237Smaybee txg_wait_synced(zilog->zl_dmu_pool, txg); 9092237Smaybee if (itx->itx_wr_state != WR_COPIED) { 9102237Smaybee char *dbuf; 9112237Smaybee int error; 9122237Smaybee 9132237Smaybee /* alignment is guaranteed */ 9142237Smaybee lr = (lr_write_t *)(lwb->lwb_buf + lwb->lwb_nused); 9152237Smaybee if (dlen) { 9162237Smaybee ASSERT(itx->itx_wr_state == WR_NEED_COPY); 9172237Smaybee dbuf = lwb->lwb_buf + lwb->lwb_nused + reclen; 9182237Smaybee lr->lr_common.lrc_reclen += dlen; 9192237Smaybee } else { 9202237Smaybee ASSERT(itx->itx_wr_state == WR_INDIRECT); 9212237Smaybee dbuf = NULL; 9222237Smaybee } 9232237Smaybee error = zilog->zl_get_data( 9242237Smaybee itx->itx_private, lr, dbuf, lwb->lwb_zio); 9252237Smaybee if (error) { 9262237Smaybee ASSERT(error == ENOENT || error == EEXIST || 9272237Smaybee error == EALREADY); 9282237Smaybee return (lwb); 9292237Smaybee } 9302237Smaybee } 9311669Sperrin } 9322237Smaybee 9332237Smaybee lwb->lwb_nused += reclen + dlen; 934789Sahrens lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg); 935789Sahrens ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb)); 936789Sahrens ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0); 937789Sahrens 938789Sahrens return (lwb); 939789Sahrens } 940789Sahrens 941789Sahrens itx_t * 9425331Samw zil_itx_create(uint64_t txtype, size_t lrsize) 943789Sahrens { 944789Sahrens itx_t *itx; 945789Sahrens 9461842Sperrin lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t); 947789Sahrens 948789Sahrens itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP); 949789Sahrens itx->itx_lr.lrc_txtype = txtype; 950789Sahrens itx->itx_lr.lrc_reclen = lrsize; 9516101Sperrin itx->itx_sod = lrsize; /* if write & WR_NEED_COPY will be increased */ 952789Sahrens itx->itx_lr.lrc_seq = 0; /* defensive */ 953789Sahrens 954789Sahrens return (itx); 955789Sahrens } 956789Sahrens 957789Sahrens uint64_t 958789Sahrens zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx) 959789Sahrens { 960789Sahrens uint64_t seq; 961789Sahrens 962789Sahrens ASSERT(itx->itx_lr.lrc_seq == 0); 963789Sahrens 964789Sahrens mutex_enter(&zilog->zl_lock); 965789Sahrens list_insert_tail(&zilog->zl_itx_list, itx); 9666101Sperrin zilog->zl_itx_list_sz += itx->itx_sod; 967789Sahrens itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx); 968789Sahrens itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq; 969789Sahrens mutex_exit(&zilog->zl_lock); 970789Sahrens 971789Sahrens return (seq); 972789Sahrens } 973789Sahrens 974789Sahrens /* 975789Sahrens * Free up all in-memory intent log transactions that have now been synced. 976789Sahrens */ 977789Sahrens static void 978789Sahrens zil_itx_clean(zilog_t *zilog) 979789Sahrens { 980789Sahrens uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa); 981789Sahrens uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa); 9823778Sjohansen list_t clean_list; 983789Sahrens itx_t *itx; 984789Sahrens 9853778Sjohansen list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node)); 9863778Sjohansen 987789Sahrens mutex_enter(&zilog->zl_lock); 9882638Sperrin /* wait for a log writer to finish walking list */ 9892638Sperrin while (zilog->zl_writer) { 9902638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 9912638Sperrin } 9923778Sjohansen 9933778Sjohansen /* 9943778Sjohansen * Move the sync'd log transactions to a separate list so we can call 9953778Sjohansen * kmem_free without holding the zl_lock. 9963778Sjohansen * 9973778Sjohansen * There is no need to set zl_writer as we don't drop zl_lock here 9983778Sjohansen */ 999789Sahrens while ((itx = list_head(&zilog->zl_itx_list)) != NULL && 1000789Sahrens itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) { 1001789Sahrens list_remove(&zilog->zl_itx_list, itx); 10026101Sperrin zilog->zl_itx_list_sz -= itx->itx_sod; 10033778Sjohansen list_insert_tail(&clean_list, itx); 10043778Sjohansen } 10053778Sjohansen cv_broadcast(&zilog->zl_cv_writer); 10063778Sjohansen mutex_exit(&zilog->zl_lock); 10073778Sjohansen 10083778Sjohansen /* destroy sync'd log transactions */ 10093778Sjohansen while ((itx = list_head(&clean_list)) != NULL) { 10103778Sjohansen list_remove(&clean_list, itx); 1011789Sahrens kmem_free(itx, offsetof(itx_t, itx_lr) 1012789Sahrens + itx->itx_lr.lrc_reclen); 1013789Sahrens } 10143778Sjohansen list_destroy(&clean_list); 1015789Sahrens } 1016789Sahrens 10172638Sperrin /* 10183063Sperrin * If there are any in-memory intent log transactions which have now been 10193063Sperrin * synced then start up a taskq to free them. 10202638Sperrin */ 1021789Sahrens void 1022789Sahrens zil_clean(zilog_t *zilog) 1023789Sahrens { 10243063Sperrin itx_t *itx; 10253063Sperrin 1026789Sahrens mutex_enter(&zilog->zl_lock); 10273063Sperrin itx = list_head(&zilog->zl_itx_list); 10283063Sperrin if ((itx != NULL) && 10293063Sperrin (itx->itx_lr.lrc_txg <= spa_last_synced_txg(zilog->zl_spa))) { 1030789Sahrens (void) taskq_dispatch(zilog->zl_clean_taskq, 1031789Sahrens (void (*)(void *))zil_itx_clean, zilog, TQ_NOSLEEP); 10323063Sperrin } 1033789Sahrens mutex_exit(&zilog->zl_lock); 1034789Sahrens } 1035789Sahrens 1036789Sahrens void 10372638Sperrin zil_commit_writer(zilog_t *zilog, uint64_t seq, uint64_t foid) 1038789Sahrens { 1039789Sahrens uint64_t txg; 10403063Sperrin uint64_t commit_seq = 0; 10412638Sperrin itx_t *itx, *itx_next = (itx_t *)-1; 1042789Sahrens lwb_t *lwb; 1043789Sahrens spa_t *spa; 1044789Sahrens 10452638Sperrin zilog->zl_writer = B_TRUE; 10462638Sperrin zilog->zl_root_zio = NULL; 1047789Sahrens spa = zilog->zl_spa; 1048789Sahrens 1049789Sahrens if (zilog->zl_suspend) { 1050789Sahrens lwb = NULL; 1051789Sahrens } else { 1052789Sahrens lwb = list_tail(&zilog->zl_lwb_list); 1053789Sahrens if (lwb == NULL) { 10542638Sperrin /* 10552638Sperrin * Return if there's nothing to flush before we 10562638Sperrin * dirty the fs by calling zil_create() 10572638Sperrin */ 10582638Sperrin if (list_is_empty(&zilog->zl_itx_list)) { 10592638Sperrin zilog->zl_writer = B_FALSE; 10602638Sperrin return; 10612638Sperrin } 1062789Sahrens mutex_exit(&zilog->zl_lock); 1063789Sahrens zil_create(zilog); 1064789Sahrens mutex_enter(&zilog->zl_lock); 1065789Sahrens lwb = list_tail(&zilog->zl_lwb_list); 1066789Sahrens } 1067789Sahrens } 1068789Sahrens 10693063Sperrin /* Loop through in-memory log transactions filling log blocks. */ 10702638Sperrin DTRACE_PROBE1(zil__cw1, zilog_t *, zilog); 1071789Sahrens for (;;) { 10722638Sperrin /* 10732638Sperrin * Find the next itx to push: 10742638Sperrin * Push all transactions related to specified foid and all 10752638Sperrin * other transactions except TX_WRITE, TX_TRUNCATE, 10762638Sperrin * TX_SETATTR and TX_ACL for all other files. 10772638Sperrin */ 10782638Sperrin if (itx_next != (itx_t *)-1) 10792638Sperrin itx = itx_next; 10802638Sperrin else 10812638Sperrin itx = list_head(&zilog->zl_itx_list); 10822638Sperrin for (; itx != NULL; itx = list_next(&zilog->zl_itx_list, itx)) { 10832638Sperrin if (foid == 0) /* push all foids? */ 10842638Sperrin break; 10853063Sperrin if (itx->itx_sync) /* push all O_[D]SYNC */ 10863063Sperrin break; 10872638Sperrin switch (itx->itx_lr.lrc_txtype) { 10882638Sperrin case TX_SETATTR: 10892638Sperrin case TX_WRITE: 10902638Sperrin case TX_TRUNCATE: 10912638Sperrin case TX_ACL: 10922638Sperrin /* lr_foid is same offset for these records */ 10932638Sperrin if (((lr_write_t *)&itx->itx_lr)->lr_foid 10942638Sperrin != foid) { 10952638Sperrin continue; /* skip this record */ 10962638Sperrin } 10972638Sperrin } 10982638Sperrin break; 10992638Sperrin } 1100789Sahrens if (itx == NULL) 1101789Sahrens break; 1102789Sahrens 1103789Sahrens if ((itx->itx_lr.lrc_seq > seq) && 11042638Sperrin ((lwb == NULL) || (lwb->lwb_nused == 0) || 11056101Sperrin (lwb->lwb_nused + itx->itx_sod > ZIL_BLK_DATA_SZ(lwb)))) { 1106789Sahrens break; 11073063Sperrin } 1108789Sahrens 11092638Sperrin /* 11102638Sperrin * Save the next pointer. Even though we soon drop 11112638Sperrin * zl_lock all threads that may change the list 11122638Sperrin * (another writer or zil_itx_clean) can't do so until 11132638Sperrin * they have zl_writer. 11142638Sperrin */ 11152638Sperrin itx_next = list_next(&zilog->zl_itx_list, itx); 1116789Sahrens list_remove(&zilog->zl_itx_list, itx); 11176101Sperrin zilog->zl_itx_list_sz -= itx->itx_sod; 11183063Sperrin mutex_exit(&zilog->zl_lock); 1119789Sahrens txg = itx->itx_lr.lrc_txg; 1120789Sahrens ASSERT(txg); 1121789Sahrens 1122789Sahrens if (txg > spa_last_synced_txg(spa) || 1123789Sahrens txg > spa_freeze_txg(spa)) 1124789Sahrens lwb = zil_lwb_commit(zilog, itx, lwb); 1125789Sahrens kmem_free(itx, offsetof(itx_t, itx_lr) 1126789Sahrens + itx->itx_lr.lrc_reclen); 1127789Sahrens mutex_enter(&zilog->zl_lock); 1128789Sahrens } 11292638Sperrin DTRACE_PROBE1(zil__cw2, zilog_t *, zilog); 11303063Sperrin /* determine commit sequence number */ 11313063Sperrin itx = list_head(&zilog->zl_itx_list); 11323063Sperrin if (itx) 11333063Sperrin commit_seq = itx->itx_lr.lrc_seq; 11343063Sperrin else 11353063Sperrin commit_seq = zilog->zl_itx_seq; 1136789Sahrens mutex_exit(&zilog->zl_lock); 1137789Sahrens 1138789Sahrens /* write the last block out */ 11393063Sperrin if (lwb != NULL && lwb->lwb_zio != NULL) 1140789Sahrens lwb = zil_lwb_write_start(zilog, lwb); 1141789Sahrens 11421141Sperrin zilog->zl_prev_used = zilog->zl_cur_used; 11431141Sperrin zilog->zl_cur_used = 0; 11441141Sperrin 11452638Sperrin /* 11462638Sperrin * Wait if necessary for the log blocks to be on stable storage. 11472638Sperrin */ 11482638Sperrin if (zilog->zl_root_zio) { 11492638Sperrin DTRACE_PROBE1(zil__cw3, zilog_t *, zilog); 11502638Sperrin (void) zio_wait(zilog->zl_root_zio); 11512638Sperrin DTRACE_PROBE1(zil__cw4, zilog_t *, zilog); 11525688Sbonwick zil_flush_vdevs(zilog); 1153789Sahrens } 11541141Sperrin 1155789Sahrens if (zilog->zl_log_error || lwb == NULL) { 1156789Sahrens zilog->zl_log_error = 0; 1157789Sahrens txg_wait_synced(zilog->zl_dmu_pool, 0); 1158789Sahrens } 11593063Sperrin 11603063Sperrin mutex_enter(&zilog->zl_lock); 11611141Sperrin zilog->zl_writer = B_FALSE; 11623063Sperrin 11633063Sperrin ASSERT3U(commit_seq, >=, zilog->zl_commit_seq); 11643063Sperrin zilog->zl_commit_seq = commit_seq; 11652638Sperrin } 11662638Sperrin 11672638Sperrin /* 11682638Sperrin * Push zfs transactions to stable storage up to the supplied sequence number. 11692638Sperrin * If foid is 0 push out all transactions, otherwise push only those 11702638Sperrin * for that file or might have been used to create that file. 11712638Sperrin */ 11722638Sperrin void 11732638Sperrin zil_commit(zilog_t *zilog, uint64_t seq, uint64_t foid) 11742638Sperrin { 11752638Sperrin if (zilog == NULL || seq == 0) 11762638Sperrin return; 11772638Sperrin 11782638Sperrin mutex_enter(&zilog->zl_lock); 11792638Sperrin 11802638Sperrin seq = MIN(seq, zilog->zl_itx_seq); /* cap seq at largest itx seq */ 11812638Sperrin 11823063Sperrin while (zilog->zl_writer) { 11832638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 11843063Sperrin if (seq < zilog->zl_commit_seq) { 11853063Sperrin mutex_exit(&zilog->zl_lock); 11863063Sperrin return; 11873063Sperrin } 11883063Sperrin } 11892638Sperrin zil_commit_writer(zilog, seq, foid); /* drops zl_lock */ 11903063Sperrin /* wake up others waiting on the commit */ 11913063Sperrin cv_broadcast(&zilog->zl_cv_writer); 11923063Sperrin mutex_exit(&zilog->zl_lock); 1193789Sahrens } 1194789Sahrens 1195789Sahrens /* 1196789Sahrens * Called in syncing context to free committed log blocks and update log header. 1197789Sahrens */ 1198789Sahrens void 1199789Sahrens zil_sync(zilog_t *zilog, dmu_tx_t *tx) 1200789Sahrens { 12011807Sbonwick zil_header_t *zh = zil_header_in_syncing_context(zilog); 1202789Sahrens uint64_t txg = dmu_tx_get_txg(tx); 1203789Sahrens spa_t *spa = zilog->zl_spa; 1204789Sahrens lwb_t *lwb; 1205789Sahrens 12061807Sbonwick mutex_enter(&zilog->zl_lock); 12071807Sbonwick 1208789Sahrens ASSERT(zilog->zl_stop_sync == 0); 1209789Sahrens 12101807Sbonwick zh->zh_replay_seq = zilog->zl_replay_seq[txg & TXG_MASK]; 1211789Sahrens 1212789Sahrens if (zilog->zl_destroy_txg == txg) { 12131807Sbonwick blkptr_t blk = zh->zh_log; 12141807Sbonwick 12151807Sbonwick ASSERT(list_head(&zilog->zl_lwb_list) == NULL); 12161807Sbonwick ASSERT(spa_sync_pass(spa) == 1); 12171807Sbonwick 12181807Sbonwick bzero(zh, sizeof (zil_header_t)); 1219789Sahrens bzero(zilog->zl_replay_seq, sizeof (zilog->zl_replay_seq)); 12201807Sbonwick 12211807Sbonwick if (zilog->zl_keep_first) { 12221807Sbonwick /* 12231807Sbonwick * If this block was part of log chain that couldn't 12241807Sbonwick * be claimed because a device was missing during 12251807Sbonwick * zil_claim(), but that device later returns, 12261807Sbonwick * then this block could erroneously appear valid. 12271807Sbonwick * To guard against this, assign a new GUID to the new 12281807Sbonwick * log chain so it doesn't matter what blk points to. 12291807Sbonwick */ 12301807Sbonwick zil_init_log_chain(zilog, &blk); 12311807Sbonwick zh->zh_log = blk; 12321807Sbonwick } 1233789Sahrens } 1234789Sahrens 1235789Sahrens for (;;) { 1236789Sahrens lwb = list_head(&zilog->zl_lwb_list); 1237789Sahrens if (lwb == NULL) { 1238789Sahrens mutex_exit(&zilog->zl_lock); 1239789Sahrens return; 1240789Sahrens } 12412638Sperrin zh->zh_log = lwb->lwb_blk; 1242789Sahrens if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg) 1243789Sahrens break; 1244789Sahrens list_remove(&zilog->zl_lwb_list, lwb); 1245789Sahrens zio_free_blk(spa, &lwb->lwb_blk, txg); 1246789Sahrens kmem_cache_free(zil_lwb_cache, lwb); 12473668Sgw25295 12483668Sgw25295 /* 12493668Sgw25295 * If we don't have anything left in the lwb list then 12503668Sgw25295 * we've had an allocation failure and we need to zero 12513668Sgw25295 * out the zil_header blkptr so that we don't end 12523668Sgw25295 * up freeing the same block twice. 12533668Sgw25295 */ 12543668Sgw25295 if (list_head(&zilog->zl_lwb_list) == NULL) 12553668Sgw25295 BP_ZERO(&zh->zh_log); 1256789Sahrens } 1257789Sahrens mutex_exit(&zilog->zl_lock); 1258789Sahrens } 1259789Sahrens 1260789Sahrens void 1261789Sahrens zil_init(void) 1262789Sahrens { 1263789Sahrens zil_lwb_cache = kmem_cache_create("zil_lwb_cache", 12642856Snd150628 sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0); 1265789Sahrens } 1266789Sahrens 1267789Sahrens void 1268789Sahrens zil_fini(void) 1269789Sahrens { 1270789Sahrens kmem_cache_destroy(zil_lwb_cache); 1271789Sahrens } 1272789Sahrens 1273789Sahrens zilog_t * 1274789Sahrens zil_alloc(objset_t *os, zil_header_t *zh_phys) 1275789Sahrens { 1276789Sahrens zilog_t *zilog; 1277789Sahrens 1278789Sahrens zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP); 1279789Sahrens 1280789Sahrens zilog->zl_header = zh_phys; 1281789Sahrens zilog->zl_os = os; 1282789Sahrens zilog->zl_spa = dmu_objset_spa(os); 1283789Sahrens zilog->zl_dmu_pool = dmu_objset_pool(os); 12841807Sbonwick zilog->zl_destroy_txg = TXG_INITIAL - 1; 1285789Sahrens 12862856Snd150628 mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL); 12872856Snd150628 1288789Sahrens list_create(&zilog->zl_itx_list, sizeof (itx_t), 1289789Sahrens offsetof(itx_t, itx_node)); 1290789Sahrens 1291789Sahrens list_create(&zilog->zl_lwb_list, sizeof (lwb_t), 1292789Sahrens offsetof(lwb_t, lwb_node)); 1293789Sahrens 12945688Sbonwick mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL); 12955688Sbonwick 12965688Sbonwick avl_create(&zilog->zl_vdev_tree, zil_vdev_compare, 12975688Sbonwick sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node)); 1298789Sahrens 12995913Sperrin cv_init(&zilog->zl_cv_writer, NULL, CV_DEFAULT, NULL); 13005913Sperrin cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL); 13015913Sperrin 1302789Sahrens return (zilog); 1303789Sahrens } 1304789Sahrens 1305789Sahrens void 1306789Sahrens zil_free(zilog_t *zilog) 1307789Sahrens { 1308789Sahrens lwb_t *lwb; 1309789Sahrens 1310789Sahrens zilog->zl_stop_sync = 1; 1311789Sahrens 1312789Sahrens while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 1313789Sahrens list_remove(&zilog->zl_lwb_list, lwb); 1314789Sahrens if (lwb->lwb_buf != NULL) 1315789Sahrens zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 1316789Sahrens kmem_cache_free(zil_lwb_cache, lwb); 1317789Sahrens } 1318789Sahrens list_destroy(&zilog->zl_lwb_list); 1319789Sahrens 13205688Sbonwick avl_destroy(&zilog->zl_vdev_tree); 13215688Sbonwick mutex_destroy(&zilog->zl_vdev_lock); 1322789Sahrens 1323789Sahrens ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1324789Sahrens list_destroy(&zilog->zl_itx_list); 13252856Snd150628 mutex_destroy(&zilog->zl_lock); 1326789Sahrens 13275913Sperrin cv_destroy(&zilog->zl_cv_writer); 13285913Sperrin cv_destroy(&zilog->zl_cv_suspend); 13295913Sperrin 1330789Sahrens kmem_free(zilog, sizeof (zilog_t)); 1331789Sahrens } 1332789Sahrens 1333789Sahrens /* 13341646Sperrin * return true if the initial log block is not valid 13351362Sperrin */ 13367522SNeil.Perrin@Sun.COM static boolean_t 13371362Sperrin zil_empty(zilog_t *zilog) 13381362Sperrin { 13391807Sbonwick const zil_header_t *zh = zilog->zl_header; 13401807Sbonwick arc_buf_t *abuf = NULL; 13411362Sperrin 13421807Sbonwick if (BP_IS_HOLE(&zh->zh_log)) 13437522SNeil.Perrin@Sun.COM return (B_TRUE); 13441362Sperrin 13451807Sbonwick if (zil_read_log_block(zilog, &zh->zh_log, &abuf) != 0) 13467522SNeil.Perrin@Sun.COM return (B_TRUE); 13471807Sbonwick 13481807Sbonwick VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 13497522SNeil.Perrin@Sun.COM return (B_FALSE); 13501362Sperrin } 13511362Sperrin 13521362Sperrin /* 1353789Sahrens * Open an intent log. 1354789Sahrens */ 1355789Sahrens zilog_t * 1356789Sahrens zil_open(objset_t *os, zil_get_data_t *get_data) 1357789Sahrens { 1358789Sahrens zilog_t *zilog = dmu_objset_zil(os); 1359789Sahrens 1360789Sahrens zilog->zl_get_data = get_data; 1361789Sahrens zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri, 1362789Sahrens 2, 2, TASKQ_PREPOPULATE); 1363789Sahrens 1364789Sahrens return (zilog); 1365789Sahrens } 1366789Sahrens 1367789Sahrens /* 1368789Sahrens * Close an intent log. 1369789Sahrens */ 1370789Sahrens void 1371789Sahrens zil_close(zilog_t *zilog) 1372789Sahrens { 13731807Sbonwick /* 13741807Sbonwick * If the log isn't already committed, mark the objset dirty 13751807Sbonwick * (so zil_sync() will be called) and wait for that txg to sync. 13761807Sbonwick */ 13771807Sbonwick if (!zil_is_committed(zilog)) { 13781807Sbonwick uint64_t txg; 13791807Sbonwick dmu_tx_t *tx = dmu_tx_create(zilog->zl_os); 13801807Sbonwick (void) dmu_tx_assign(tx, TXG_WAIT); 13811807Sbonwick dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 13821807Sbonwick txg = dmu_tx_get_txg(tx); 13831807Sbonwick dmu_tx_commit(tx); 13841807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, txg); 13851807Sbonwick } 13861807Sbonwick 1387789Sahrens taskq_destroy(zilog->zl_clean_taskq); 1388789Sahrens zilog->zl_clean_taskq = NULL; 1389789Sahrens zilog->zl_get_data = NULL; 1390789Sahrens 1391789Sahrens zil_itx_clean(zilog); 1392789Sahrens ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1393789Sahrens } 1394789Sahrens 1395789Sahrens /* 1396789Sahrens * Suspend an intent log. While in suspended mode, we still honor 1397789Sahrens * synchronous semantics, but we rely on txg_wait_synced() to do it. 1398789Sahrens * We suspend the log briefly when taking a snapshot so that the snapshot 1399789Sahrens * contains all the data it's supposed to, and has an empty intent log. 1400789Sahrens */ 1401789Sahrens int 1402789Sahrens zil_suspend(zilog_t *zilog) 1403789Sahrens { 14041807Sbonwick const zil_header_t *zh = zilog->zl_header; 1405789Sahrens 1406789Sahrens mutex_enter(&zilog->zl_lock); 14071807Sbonwick if (zh->zh_claim_txg != 0) { /* unplayed log */ 1408789Sahrens mutex_exit(&zilog->zl_lock); 1409789Sahrens return (EBUSY); 1410789Sahrens } 14111807Sbonwick if (zilog->zl_suspend++ != 0) { 14121807Sbonwick /* 14131807Sbonwick * Someone else already began a suspend. 14141807Sbonwick * Just wait for them to finish. 14151807Sbonwick */ 14161807Sbonwick while (zilog->zl_suspending) 14171807Sbonwick cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock); 14181807Sbonwick mutex_exit(&zilog->zl_lock); 14191807Sbonwick return (0); 14201807Sbonwick } 14211807Sbonwick zilog->zl_suspending = B_TRUE; 1422789Sahrens mutex_exit(&zilog->zl_lock); 1423789Sahrens 14242638Sperrin zil_commit(zilog, UINT64_MAX, 0); 1425789Sahrens 14262638Sperrin /* 14272638Sperrin * Wait for any in-flight log writes to complete. 14282638Sperrin */ 1429789Sahrens mutex_enter(&zilog->zl_lock); 14302638Sperrin while (zilog->zl_writer) 14312638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 1432789Sahrens mutex_exit(&zilog->zl_lock); 1433789Sahrens 14341807Sbonwick zil_destroy(zilog, B_FALSE); 14351807Sbonwick 14361807Sbonwick mutex_enter(&zilog->zl_lock); 14371807Sbonwick zilog->zl_suspending = B_FALSE; 14381807Sbonwick cv_broadcast(&zilog->zl_cv_suspend); 14391807Sbonwick mutex_exit(&zilog->zl_lock); 1440789Sahrens 1441789Sahrens return (0); 1442789Sahrens } 1443789Sahrens 1444789Sahrens void 1445789Sahrens zil_resume(zilog_t *zilog) 1446789Sahrens { 1447789Sahrens mutex_enter(&zilog->zl_lock); 1448789Sahrens ASSERT(zilog->zl_suspend != 0); 1449789Sahrens zilog->zl_suspend--; 1450789Sahrens mutex_exit(&zilog->zl_lock); 1451789Sahrens } 1452789Sahrens 1453789Sahrens typedef struct zil_replay_arg { 1454789Sahrens objset_t *zr_os; 1455789Sahrens zil_replay_func_t **zr_replay; 1456*7638SNeil.Perrin@Sun.COM zil_replay_cleaner_t *zr_replay_cleaner; 1457789Sahrens void *zr_arg; 1458789Sahrens uint64_t *zr_txgp; 1459789Sahrens boolean_t zr_byteswap; 1460789Sahrens char *zr_lrbuf; 1461789Sahrens } zil_replay_arg_t; 1462789Sahrens 1463789Sahrens static void 1464789Sahrens zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg) 1465789Sahrens { 1466789Sahrens zil_replay_arg_t *zr = zra; 14671807Sbonwick const zil_header_t *zh = zilog->zl_header; 1468789Sahrens uint64_t reclen = lr->lrc_reclen; 1469789Sahrens uint64_t txtype = lr->lrc_txtype; 14703063Sperrin char *name; 14713063Sperrin int pass, error, sunk; 1472789Sahrens 1473789Sahrens if (zilog->zl_stop_replay) 1474789Sahrens return; 1475789Sahrens 1476789Sahrens if (lr->lrc_txg < claim_txg) /* already committed */ 1477789Sahrens return; 1478789Sahrens 1479789Sahrens if (lr->lrc_seq <= zh->zh_replay_seq) /* already replayed */ 1480789Sahrens return; 1481789Sahrens 14825331Samw /* Strip case-insensitive bit, still present in log record */ 14835331Samw txtype &= ~TX_CI; 14845331Samw 1485789Sahrens /* 1486789Sahrens * Make a copy of the data so we can revise and extend it. 1487789Sahrens */ 1488789Sahrens bcopy(lr, zr->zr_lrbuf, reclen); 1489789Sahrens 1490789Sahrens /* 1491789Sahrens * The log block containing this lr may have been byteswapped 1492789Sahrens * so that we can easily examine common fields like lrc_txtype. 1493789Sahrens * However, the log is a mix of different data types, and only the 1494789Sahrens * replay vectors know how to byteswap their records. Therefore, if 1495789Sahrens * the lr was byteswapped, undo it before invoking the replay vector. 1496789Sahrens */ 1497789Sahrens if (zr->zr_byteswap) 1498789Sahrens byteswap_uint64_array(zr->zr_lrbuf, reclen); 1499789Sahrens 1500789Sahrens /* 1501789Sahrens * If this is a TX_WRITE with a blkptr, suck in the data. 1502789Sahrens */ 1503789Sahrens if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) { 1504789Sahrens lr_write_t *lrw = (lr_write_t *)lr; 1505789Sahrens blkptr_t *wbp = &lrw->lr_blkptr; 1506789Sahrens uint64_t wlen = lrw->lr_length; 1507789Sahrens char *wbuf = zr->zr_lrbuf + reclen; 1508789Sahrens 1509789Sahrens if (BP_IS_HOLE(wbp)) { /* compressed to a hole */ 1510789Sahrens bzero(wbuf, wlen); 1511789Sahrens } else { 1512789Sahrens /* 1513789Sahrens * A subsequent write may have overwritten this block, 1514789Sahrens * in which case wbp may have been been freed and 1515789Sahrens * reallocated, and our read of wbp may fail with a 1516789Sahrens * checksum error. We can safely ignore this because 1517789Sahrens * the later write will provide the correct data. 1518789Sahrens */ 15191544Seschrock zbookmark_t zb; 15201544Seschrock 15211544Seschrock zb.zb_objset = dmu_objset_id(zilog->zl_os); 15221544Seschrock zb.zb_object = lrw->lr_foid; 15231544Seschrock zb.zb_level = -1; 15241544Seschrock zb.zb_blkid = lrw->lr_offset / BP_GET_LSIZE(wbp); 15251544Seschrock 1526789Sahrens (void) zio_wait(zio_read(NULL, zilog->zl_spa, 1527789Sahrens wbp, wbuf, BP_GET_LSIZE(wbp), NULL, NULL, 1528789Sahrens ZIO_PRIORITY_SYNC_READ, 15291544Seschrock ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb)); 1530789Sahrens (void) memmove(wbuf, wbuf + lrw->lr_blkoff, wlen); 1531789Sahrens } 1532789Sahrens } 1533789Sahrens 1534789Sahrens /* 1535789Sahrens * We must now do two things atomically: replay this log record, 1536789Sahrens * and update the log header to reflect the fact that we did so. 1537789Sahrens * We use the DMU's ability to assign into a specific txg to do this. 1538789Sahrens */ 15393063Sperrin for (pass = 1, sunk = B_FALSE; /* CONSTANTCONDITION */; pass++) { 1540789Sahrens uint64_t replay_txg; 1541789Sahrens dmu_tx_t *replay_tx; 1542789Sahrens 1543789Sahrens replay_tx = dmu_tx_create(zr->zr_os); 1544789Sahrens error = dmu_tx_assign(replay_tx, TXG_WAIT); 1545789Sahrens if (error) { 1546789Sahrens dmu_tx_abort(replay_tx); 1547789Sahrens break; 1548789Sahrens } 1549789Sahrens 1550789Sahrens replay_txg = dmu_tx_get_txg(replay_tx); 1551789Sahrens 1552789Sahrens if (txtype == 0 || txtype >= TX_MAX_TYPE) { 1553789Sahrens error = EINVAL; 1554789Sahrens } else { 1555789Sahrens /* 1556789Sahrens * On the first pass, arrange for the replay vector 1557789Sahrens * to fail its dmu_tx_assign(). That's the only way 1558789Sahrens * to ensure that those code paths remain well tested. 15595676Sperrin * 15605676Sperrin * Only byteswap (if needed) on the 1st pass. 1561789Sahrens */ 1562789Sahrens *zr->zr_txgp = replay_txg - (pass == 1); 1563789Sahrens error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lrbuf, 15645676Sperrin zr->zr_byteswap && pass == 1); 1565789Sahrens *zr->zr_txgp = TXG_NOWAIT; 1566789Sahrens } 1567789Sahrens 1568789Sahrens if (error == 0) { 1569789Sahrens dsl_dataset_dirty(dmu_objset_ds(zr->zr_os), replay_tx); 1570789Sahrens zilog->zl_replay_seq[replay_txg & TXG_MASK] = 1571789Sahrens lr->lrc_seq; 1572789Sahrens } 1573789Sahrens 1574789Sahrens dmu_tx_commit(replay_tx); 1575789Sahrens 15763063Sperrin if (!error) 15773063Sperrin return; 15783063Sperrin 15793063Sperrin /* 15803063Sperrin * The DMU's dnode layer doesn't see removes until the txg 15813063Sperrin * commits, so a subsequent claim can spuriously fail with 15823063Sperrin * EEXIST. So if we receive any error other than ERESTART 15833063Sperrin * we try syncing out any removes then retrying the 15843063Sperrin * transaction. 15853063Sperrin */ 15863063Sperrin if (error != ERESTART && !sunk) { 1587*7638SNeil.Perrin@Sun.COM if (zr->zr_replay_cleaner) 1588*7638SNeil.Perrin@Sun.COM zr->zr_replay_cleaner(zr->zr_arg); 15893063Sperrin txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0); 15903063Sperrin sunk = B_TRUE; 15913063Sperrin continue; /* retry */ 15923063Sperrin } 15933063Sperrin 1594789Sahrens if (error != ERESTART) 1595789Sahrens break; 1596789Sahrens 1597789Sahrens if (pass != 1) 1598789Sahrens txg_wait_open(spa_get_dsl(zilog->zl_spa), 1599789Sahrens replay_txg + 1); 1600789Sahrens 1601789Sahrens dprintf("pass %d, retrying\n", pass); 1602789Sahrens } 1603789Sahrens 16043063Sperrin ASSERT(error && error != ERESTART); 16053063Sperrin name = kmem_alloc(MAXNAMELEN, KM_SLEEP); 16063063Sperrin dmu_objset_name(zr->zr_os, name); 16073063Sperrin cmn_err(CE_WARN, "ZFS replay transaction error %d, " 16085331Samw "dataset %s, seq 0x%llx, txtype %llu %s\n", 16095331Samw error, name, (u_longlong_t)lr->lrc_seq, (u_longlong_t)txtype, 16105331Samw (lr->lrc_txtype & TX_CI) ? "CI" : ""); 16113063Sperrin zilog->zl_stop_replay = 1; 16123063Sperrin kmem_free(name, MAXNAMELEN); 16133063Sperrin } 1614789Sahrens 16153063Sperrin /* ARGSUSED */ 16163063Sperrin static void 16173063Sperrin zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg) 16183063Sperrin { 16193063Sperrin zilog->zl_replay_blks++; 1620789Sahrens } 1621789Sahrens 1622789Sahrens /* 16231362Sperrin * If this dataset has a non-empty intent log, replay it and destroy it. 1624789Sahrens */ 1625789Sahrens void 1626789Sahrens zil_replay(objset_t *os, void *arg, uint64_t *txgp, 1627*7638SNeil.Perrin@Sun.COM zil_replay_func_t *replay_func[TX_MAX_TYPE], 1628*7638SNeil.Perrin@Sun.COM zil_replay_cleaner_t *replay_cleaner) 1629789Sahrens { 1630789Sahrens zilog_t *zilog = dmu_objset_zil(os); 16311807Sbonwick const zil_header_t *zh = zilog->zl_header; 16321807Sbonwick zil_replay_arg_t zr; 16331362Sperrin 16341362Sperrin if (zil_empty(zilog)) { 16351807Sbonwick zil_destroy(zilog, B_TRUE); 16361362Sperrin return; 16371362Sperrin } 1638789Sahrens 1639789Sahrens zr.zr_os = os; 1640789Sahrens zr.zr_replay = replay_func; 1641*7638SNeil.Perrin@Sun.COM zr.zr_replay_cleaner = replay_cleaner; 1642789Sahrens zr.zr_arg = arg; 1643789Sahrens zr.zr_txgp = txgp; 16441807Sbonwick zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log); 1645789Sahrens zr.zr_lrbuf = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP); 1646789Sahrens 1647789Sahrens /* 1648789Sahrens * Wait for in-progress removes to sync before starting replay. 1649789Sahrens */ 1650789Sahrens txg_wait_synced(zilog->zl_dmu_pool, 0); 1651789Sahrens 1652789Sahrens zilog->zl_stop_replay = 0; 16533063Sperrin zilog->zl_replay_time = lbolt; 16543063Sperrin ASSERT(zilog->zl_replay_blks == 0); 16553063Sperrin (void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr, 16561807Sbonwick zh->zh_claim_txg); 1657789Sahrens kmem_free(zr.zr_lrbuf, 2 * SPA_MAXBLOCKSIZE); 1658789Sahrens 16591807Sbonwick zil_destroy(zilog, B_FALSE); 16605712Sahrens txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 1661789Sahrens } 16621646Sperrin 16631646Sperrin /* 16641646Sperrin * Report whether all transactions are committed 16651646Sperrin */ 16661646Sperrin int 16671646Sperrin zil_is_committed(zilog_t *zilog) 16681646Sperrin { 16691646Sperrin lwb_t *lwb; 16702638Sperrin int ret; 16711646Sperrin 16722638Sperrin mutex_enter(&zilog->zl_lock); 16732638Sperrin while (zilog->zl_writer) 16742638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 16752638Sperrin 16762638Sperrin /* recent unpushed intent log transactions? */ 16772638Sperrin if (!list_is_empty(&zilog->zl_itx_list)) { 16782638Sperrin ret = B_FALSE; 16792638Sperrin goto out; 16802638Sperrin } 16812638Sperrin 16822638Sperrin /* intent log never used? */ 16832638Sperrin lwb = list_head(&zilog->zl_lwb_list); 16842638Sperrin if (lwb == NULL) { 16852638Sperrin ret = B_TRUE; 16862638Sperrin goto out; 16872638Sperrin } 16881646Sperrin 16891646Sperrin /* 16902638Sperrin * more than 1 log buffer means zil_sync() hasn't yet freed 16912638Sperrin * entries after a txg has committed 16921646Sperrin */ 16932638Sperrin if (list_next(&zilog->zl_lwb_list, lwb)) { 16942638Sperrin ret = B_FALSE; 16952638Sperrin goto out; 16962638Sperrin } 16972638Sperrin 16981646Sperrin ASSERT(zil_empty(zilog)); 16992638Sperrin ret = B_TRUE; 17002638Sperrin out: 17012638Sperrin cv_broadcast(&zilog->zl_cv_writer); 17022638Sperrin mutex_exit(&zilog->zl_lock); 17032638Sperrin return (ret); 17041646Sperrin } 1705