1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51472Sperrin * Common Development and Distribution License (the "License"). 61472Sperrin * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 228746SMatthew.Ahrens@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #include <sys/zfs_context.h> 27789Sahrens #include <sys/spa.h> 289701SGeorge.Wilson@Sun.COM #include <sys/spa_impl.h> 29789Sahrens #include <sys/dmu.h> 30789Sahrens #include <sys/zap.h> 31789Sahrens #include <sys/arc.h> 32789Sahrens #include <sys/stat.h> 33789Sahrens #include <sys/resource.h> 34789Sahrens #include <sys/zil.h> 35789Sahrens #include <sys/zil_impl.h> 36789Sahrens #include <sys/dsl_dataset.h> 37789Sahrens #include <sys/vdev.h> 383668Sgw25295 #include <sys/dmu_tx.h> 39789Sahrens 40789Sahrens /* 41789Sahrens * The zfs intent log (ZIL) saves transaction records of system calls 42789Sahrens * that change the file system in memory with enough information 43789Sahrens * to be able to replay them. These are stored in memory until 44789Sahrens * either the DMU transaction group (txg) commits them to the stable pool 45789Sahrens * and they can be discarded, or they are flushed to the stable log 46789Sahrens * (also in the pool) due to a fsync, O_DSYNC or other synchronous 47789Sahrens * requirement. In the event of a panic or power fail then those log 48789Sahrens * records (transactions) are replayed. 49789Sahrens * 50789Sahrens * There is one ZIL per file system. Its on-disk (pool) format consists 51789Sahrens * of 3 parts: 52789Sahrens * 53789Sahrens * - ZIL header 54789Sahrens * - ZIL blocks 55789Sahrens * - ZIL records 56789Sahrens * 57789Sahrens * A log record holds a system call transaction. Log blocks can 58789Sahrens * hold many log records and the blocks are chained together. 59789Sahrens * Each ZIL block contains a block pointer (blkptr_t) to the next 60789Sahrens * ZIL block in the chain. The ZIL header points to the first 61789Sahrens * block in the chain. Note there is not a fixed place in the pool 62789Sahrens * to hold blocks. They are dynamically allocated and freed as 63789Sahrens * needed from the blocks available. Figure X shows the ZIL structure: 64789Sahrens */ 65789Sahrens 66789Sahrens /* 672986Sek110237 * This global ZIL switch affects all pools 68789Sahrens */ 69789Sahrens int zil_disable = 0; /* disable intent logging */ 702986Sek110237 712986Sek110237 /* 722986Sek110237 * Tunable parameter for debugging or performance analysis. Setting 732986Sek110237 * zfs_nocacheflush will cause corruption on power loss if a volatile 742986Sek110237 * out-of-order write cache is enabled. 752986Sek110237 */ 762986Sek110237 boolean_t zfs_nocacheflush = B_FALSE; 77789Sahrens 78789Sahrens static kmem_cache_t *zil_lwb_cache; 79789Sahrens 8010685SGeorge.Wilson@Sun.COM static boolean_t zil_empty(zilog_t *zilog); 8110685SGeorge.Wilson@Sun.COM 82789Sahrens static int 83789Sahrens zil_dva_compare(const void *x1, const void *x2) 84789Sahrens { 85789Sahrens const dva_t *dva1 = x1; 86789Sahrens const dva_t *dva2 = x2; 87789Sahrens 88789Sahrens if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2)) 89789Sahrens return (-1); 90789Sahrens if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2)) 91789Sahrens return (1); 92789Sahrens 93789Sahrens if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2)) 94789Sahrens return (-1); 95789Sahrens if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2)) 96789Sahrens return (1); 97789Sahrens 98789Sahrens return (0); 99789Sahrens } 100789Sahrens 101789Sahrens static void 102789Sahrens zil_dva_tree_init(avl_tree_t *t) 103789Sahrens { 104789Sahrens avl_create(t, zil_dva_compare, sizeof (zil_dva_node_t), 105789Sahrens offsetof(zil_dva_node_t, zn_node)); 106789Sahrens } 107789Sahrens 108789Sahrens static void 109789Sahrens zil_dva_tree_fini(avl_tree_t *t) 110789Sahrens { 111789Sahrens zil_dva_node_t *zn; 112789Sahrens void *cookie = NULL; 113789Sahrens 114789Sahrens while ((zn = avl_destroy_nodes(t, &cookie)) != NULL) 115789Sahrens kmem_free(zn, sizeof (zil_dva_node_t)); 116789Sahrens 117789Sahrens avl_destroy(t); 118789Sahrens } 119789Sahrens 120789Sahrens static int 121789Sahrens zil_dva_tree_add(avl_tree_t *t, dva_t *dva) 122789Sahrens { 123789Sahrens zil_dva_node_t *zn; 124789Sahrens avl_index_t where; 125789Sahrens 126789Sahrens if (avl_find(t, dva, &where) != NULL) 127789Sahrens return (EEXIST); 128789Sahrens 129789Sahrens zn = kmem_alloc(sizeof (zil_dva_node_t), KM_SLEEP); 130789Sahrens zn->zn_dva = *dva; 131789Sahrens avl_insert(t, zn, where); 132789Sahrens 133789Sahrens return (0); 134789Sahrens } 135789Sahrens 1361807Sbonwick static zil_header_t * 1371807Sbonwick zil_header_in_syncing_context(zilog_t *zilog) 1381807Sbonwick { 1391807Sbonwick return ((zil_header_t *)zilog->zl_header); 1401807Sbonwick } 1411807Sbonwick 1421807Sbonwick static void 1431807Sbonwick zil_init_log_chain(zilog_t *zilog, blkptr_t *bp) 1441807Sbonwick { 1451807Sbonwick zio_cksum_t *zc = &bp->blk_cksum; 1461807Sbonwick 1471807Sbonwick zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL); 1481807Sbonwick zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL); 1491807Sbonwick zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os); 1501807Sbonwick zc->zc_word[ZIL_ZC_SEQ] = 1ULL; 1511807Sbonwick } 1521807Sbonwick 153789Sahrens /* 154789Sahrens * Read a log block, make sure it's valid, and byteswap it if necessary. 155789Sahrens */ 156789Sahrens static int 1571807Sbonwick zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, arc_buf_t **abufpp) 158789Sahrens { 1591807Sbonwick blkptr_t blk = *bp; 1601544Seschrock zbookmark_t zb; 1612391Smaybee uint32_t aflags = ARC_WAIT; 162789Sahrens int error; 163789Sahrens 1641807Sbonwick zb.zb_objset = bp->blk_cksum.zc_word[ZIL_ZC_OBJSET]; 1651544Seschrock zb.zb_object = 0; 1661544Seschrock zb.zb_level = -1; 1671807Sbonwick zb.zb_blkid = bp->blk_cksum.zc_word[ZIL_ZC_SEQ]; 1681807Sbonwick 1691807Sbonwick *abufpp = NULL; 1701807Sbonwick 1717046Sahrens /* 1727046Sahrens * We shouldn't be doing any scrubbing while we're doing log 1737046Sahrens * replay, it's OK to not lock. 1747046Sahrens */ 1757046Sahrens error = arc_read_nolock(NULL, zilog->zl_spa, &blk, 1761807Sbonwick arc_getbuf_func, abufpp, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL | 1772391Smaybee ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB, &aflags, &zb); 1781807Sbonwick 1791807Sbonwick if (error == 0) { 1801807Sbonwick char *data = (*abufpp)->b_data; 1811807Sbonwick uint64_t blksz = BP_GET_LSIZE(bp); 1821807Sbonwick zil_trailer_t *ztp = (zil_trailer_t *)(data + blksz) - 1; 1831807Sbonwick zio_cksum_t cksum = bp->blk_cksum; 1841544Seschrock 1851807Sbonwick /* 1867522SNeil.Perrin@Sun.COM * Validate the checksummed log block. 1877522SNeil.Perrin@Sun.COM * 1881807Sbonwick * Sequence numbers should be... sequential. The checksum 1891807Sbonwick * verifier for the next block should be bp's checksum plus 1. 1907522SNeil.Perrin@Sun.COM * 1917522SNeil.Perrin@Sun.COM * Also check the log chain linkage and size used. 1921807Sbonwick */ 1931807Sbonwick cksum.zc_word[ZIL_ZC_SEQ]++; 1941807Sbonwick 1957522SNeil.Perrin@Sun.COM if (bcmp(&cksum, &ztp->zit_next_blk.blk_cksum, 1967522SNeil.Perrin@Sun.COM sizeof (cksum)) || BP_IS_HOLE(&ztp->zit_next_blk) || 1977522SNeil.Perrin@Sun.COM (ztp->zit_nused > (blksz - sizeof (zil_trailer_t)))) { 1987522SNeil.Perrin@Sun.COM error = ECKSUM; 1997522SNeil.Perrin@Sun.COM } 2001807Sbonwick 2011807Sbonwick if (error) { 2021807Sbonwick VERIFY(arc_buf_remove_ref(*abufpp, abufpp) == 1); 2031807Sbonwick *abufpp = NULL; 2041807Sbonwick } 205789Sahrens } 206789Sahrens 2071807Sbonwick dprintf("error %d on %llu:%llu\n", error, zb.zb_objset, zb.zb_blkid); 208789Sahrens 2091807Sbonwick return (error); 210789Sahrens } 211789Sahrens 212789Sahrens /* 213789Sahrens * Parse the intent log, and call parse_func for each valid record within. 2141807Sbonwick * Return the highest sequence number. 215789Sahrens */ 2161807Sbonwick uint64_t 217789Sahrens zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func, 218789Sahrens zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg) 219789Sahrens { 2201807Sbonwick const zil_header_t *zh = zilog->zl_header; 2211807Sbonwick uint64_t claim_seq = zh->zh_claim_seq; 2221807Sbonwick uint64_t seq = 0; 2231807Sbonwick uint64_t max_seq = 0; 2241807Sbonwick blkptr_t blk = zh->zh_log; 2251807Sbonwick arc_buf_t *abuf; 226789Sahrens char *lrbuf, *lrp; 227789Sahrens zil_trailer_t *ztp; 228789Sahrens int reclen, error; 229789Sahrens 230789Sahrens if (BP_IS_HOLE(&blk)) 2311807Sbonwick return (max_seq); 232789Sahrens 233789Sahrens /* 234789Sahrens * Starting at the block pointed to by zh_log we read the log chain. 235789Sahrens * For each block in the chain we strongly check that block to 236789Sahrens * ensure its validity. We stop when an invalid block is found. 237789Sahrens * For each block pointer in the chain we call parse_blk_func(). 238789Sahrens * For each record in each valid block we call parse_lr_func(). 2391807Sbonwick * If the log has been claimed, stop if we encounter a sequence 2401807Sbonwick * number greater than the highest claimed sequence number. 241789Sahrens */ 242789Sahrens zil_dva_tree_init(&zilog->zl_dva_tree); 243789Sahrens for (;;) { 2441807Sbonwick seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 2451807Sbonwick 2461807Sbonwick if (claim_seq != 0 && seq > claim_seq) 2471807Sbonwick break; 2481807Sbonwick 2491807Sbonwick ASSERT(max_seq < seq); 2501807Sbonwick max_seq = seq; 2511807Sbonwick 2521807Sbonwick error = zil_read_log_block(zilog, &blk, &abuf); 253789Sahrens 254789Sahrens if (parse_blk_func != NULL) 255789Sahrens parse_blk_func(zilog, &blk, arg, txg); 256789Sahrens 257789Sahrens if (error) 258789Sahrens break; 259789Sahrens 2601807Sbonwick lrbuf = abuf->b_data; 261789Sahrens ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1; 262789Sahrens blk = ztp->zit_next_blk; 263789Sahrens 2641807Sbonwick if (parse_lr_func == NULL) { 2651807Sbonwick VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 266789Sahrens continue; 2671807Sbonwick } 268789Sahrens 269789Sahrens for (lrp = lrbuf; lrp < lrbuf + ztp->zit_nused; lrp += reclen) { 270789Sahrens lr_t *lr = (lr_t *)lrp; 271789Sahrens reclen = lr->lrc_reclen; 272789Sahrens ASSERT3U(reclen, >=, sizeof (lr_t)); 273789Sahrens parse_lr_func(zilog, lr, arg, txg); 274789Sahrens } 2751807Sbonwick VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 276789Sahrens } 277789Sahrens zil_dva_tree_fini(&zilog->zl_dva_tree); 2781807Sbonwick 2791807Sbonwick return (max_seq); 280789Sahrens } 281789Sahrens 282789Sahrens /* ARGSUSED */ 283789Sahrens static void 284789Sahrens zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg) 285789Sahrens { 286789Sahrens spa_t *spa = zilog->zl_spa; 287789Sahrens int err; 288789Sahrens 289789Sahrens /* 290789Sahrens * Claim log block if not already committed and not already claimed. 291789Sahrens */ 292789Sahrens if (bp->blk_birth >= first_txg && 293789Sahrens zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp)) == 0) { 2947754SJeff.Bonwick@Sun.COM err = zio_wait(zio_claim(NULL, spa, first_txg, bp, NULL, NULL, 2957754SJeff.Bonwick@Sun.COM ZIO_FLAG_MUSTSUCCEED)); 296789Sahrens ASSERT(err == 0); 297789Sahrens } 298789Sahrens } 299789Sahrens 300789Sahrens static void 301789Sahrens zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg) 302789Sahrens { 303789Sahrens if (lrc->lrc_txtype == TX_WRITE) { 304789Sahrens lr_write_t *lr = (lr_write_t *)lrc; 305789Sahrens zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg); 306789Sahrens } 307789Sahrens } 308789Sahrens 309789Sahrens /* ARGSUSED */ 310789Sahrens static void 311789Sahrens zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg) 312789Sahrens { 313789Sahrens zio_free_blk(zilog->zl_spa, bp, dmu_tx_get_txg(tx)); 314789Sahrens } 315789Sahrens 316789Sahrens static void 317789Sahrens zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg) 318789Sahrens { 319789Sahrens /* 320789Sahrens * If we previously claimed it, we need to free it. 321789Sahrens */ 322789Sahrens if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE) { 323789Sahrens lr_write_t *lr = (lr_write_t *)lrc; 324789Sahrens blkptr_t *bp = &lr->lr_blkptr; 325789Sahrens if (bp->blk_birth >= claim_txg && 326789Sahrens !zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp))) { 327789Sahrens (void) arc_free(NULL, zilog->zl_spa, 328789Sahrens dmu_tx_get_txg(tx), bp, NULL, NULL, ARC_WAIT); 329789Sahrens } 330789Sahrens } 331789Sahrens } 332789Sahrens 333789Sahrens /* 334789Sahrens * Create an on-disk intent log. 335789Sahrens */ 336789Sahrens static void 337789Sahrens zil_create(zilog_t *zilog) 338789Sahrens { 3391807Sbonwick const zil_header_t *zh = zilog->zl_header; 340789Sahrens lwb_t *lwb; 3411807Sbonwick uint64_t txg = 0; 3421807Sbonwick dmu_tx_t *tx = NULL; 343789Sahrens blkptr_t blk; 3441807Sbonwick int error = 0; 345789Sahrens 346789Sahrens /* 3471807Sbonwick * Wait for any previous destroy to complete. 348789Sahrens */ 3491807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 3501807Sbonwick 3511807Sbonwick ASSERT(zh->zh_claim_txg == 0); 3521807Sbonwick ASSERT(zh->zh_replay_seq == 0); 3531807Sbonwick 3541807Sbonwick blk = zh->zh_log; 355789Sahrens 356789Sahrens /* 3578109SNeil.Perrin@Sun.COM * If we don't already have an initial log block or we have one 3588109SNeil.Perrin@Sun.COM * but it's the wrong endianness then allocate one. 359789Sahrens */ 3608109SNeil.Perrin@Sun.COM if (BP_IS_HOLE(&blk) || BP_SHOULD_BYTESWAP(&blk)) { 3611807Sbonwick tx = dmu_tx_create(zilog->zl_os); 3621807Sbonwick (void) dmu_tx_assign(tx, TXG_WAIT); 3631807Sbonwick dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 3641807Sbonwick txg = dmu_tx_get_txg(tx); 3651807Sbonwick 3668109SNeil.Perrin@Sun.COM if (!BP_IS_HOLE(&blk)) { 3678109SNeil.Perrin@Sun.COM zio_free_blk(zilog->zl_spa, &blk, txg); 3688109SNeil.Perrin@Sun.COM BP_ZERO(&blk); 3698109SNeil.Perrin@Sun.COM } 3708109SNeil.Perrin@Sun.COM 3713063Sperrin error = zio_alloc_blk(zilog->zl_spa, ZIL_MIN_BLKSZ, &blk, 37210879SNeil.Perrin@Sun.COM NULL, txg, zilog->zl_logbias == ZFS_LOGBIAS_LATENCY); 3731807Sbonwick 3741807Sbonwick if (error == 0) 3751807Sbonwick zil_init_log_chain(zilog, &blk); 3761362Sperrin } 3771807Sbonwick 3781807Sbonwick /* 3791807Sbonwick * Allocate a log write buffer (lwb) for the first log block. 3801807Sbonwick */ 381789Sahrens if (error == 0) { 382789Sahrens lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 383789Sahrens lwb->lwb_zilog = zilog; 384789Sahrens lwb->lwb_blk = blk; 385789Sahrens lwb->lwb_nused = 0; 386789Sahrens lwb->lwb_sz = BP_GET_LSIZE(&lwb->lwb_blk); 387789Sahrens lwb->lwb_buf = zio_buf_alloc(lwb->lwb_sz); 388789Sahrens lwb->lwb_max_txg = txg; 3892237Smaybee lwb->lwb_zio = NULL; 3902237Smaybee 391789Sahrens mutex_enter(&zilog->zl_lock); 392789Sahrens list_insert_tail(&zilog->zl_lwb_list, lwb); 393789Sahrens mutex_exit(&zilog->zl_lock); 394789Sahrens } 395789Sahrens 3961807Sbonwick /* 3971807Sbonwick * If we just allocated the first log block, commit our transaction 3981807Sbonwick * and wait for zil_sync() to stuff the block poiner into zh_log. 3991807Sbonwick * (zh is part of the MOS, so we cannot modify it in open context.) 4001807Sbonwick */ 4011807Sbonwick if (tx != NULL) { 4021807Sbonwick dmu_tx_commit(tx); 4031362Sperrin txg_wait_synced(zilog->zl_dmu_pool, txg); 4041807Sbonwick } 4051807Sbonwick 4061807Sbonwick ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0); 407789Sahrens } 408789Sahrens 409789Sahrens /* 410789Sahrens * In one tx, free all log blocks and clear the log header. 4111807Sbonwick * If keep_first is set, then we're replaying a log with no content. 4121807Sbonwick * We want to keep the first block, however, so that the first 4131807Sbonwick * synchronous transaction doesn't require a txg_wait_synced() 4141807Sbonwick * in zil_create(). We don't need to txg_wait_synced() here either 4151807Sbonwick * when keep_first is set, because both zil_create() and zil_destroy() 4161807Sbonwick * will wait for any in-progress destroys to complete. 417789Sahrens */ 418789Sahrens void 4191807Sbonwick zil_destroy(zilog_t *zilog, boolean_t keep_first) 420789Sahrens { 4211807Sbonwick const zil_header_t *zh = zilog->zl_header; 4221807Sbonwick lwb_t *lwb; 423789Sahrens dmu_tx_t *tx; 424789Sahrens uint64_t txg; 425789Sahrens 4261807Sbonwick /* 4271807Sbonwick * Wait for any previous destroy to complete. 4281807Sbonwick */ 4291807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 430789Sahrens 4311807Sbonwick if (BP_IS_HOLE(&zh->zh_log)) 432789Sahrens return; 433789Sahrens 434789Sahrens tx = dmu_tx_create(zilog->zl_os); 435789Sahrens (void) dmu_tx_assign(tx, TXG_WAIT); 436789Sahrens dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 437789Sahrens txg = dmu_tx_get_txg(tx); 438789Sahrens 4391807Sbonwick mutex_enter(&zilog->zl_lock); 4401807Sbonwick 4411807Sbonwick ASSERT3U(zilog->zl_destroy_txg, <, txg); 442789Sahrens zilog->zl_destroy_txg = txg; 4431807Sbonwick 4441807Sbonwick if (!list_is_empty(&zilog->zl_lwb_list)) { 4451807Sbonwick ASSERT(zh->zh_claim_txg == 0); 44610685SGeorge.Wilson@Sun.COM zilog->zl_keep_first = B_FALSE; 4471807Sbonwick while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 4481807Sbonwick list_remove(&zilog->zl_lwb_list, lwb); 4491807Sbonwick if (lwb->lwb_buf != NULL) 4501807Sbonwick zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 4511807Sbonwick zio_free_blk(zilog->zl_spa, &lwb->lwb_blk, txg); 4521807Sbonwick kmem_cache_free(zil_lwb_cache, lwb); 4531807Sbonwick } 4541807Sbonwick } else { 45510685SGeorge.Wilson@Sun.COM zilog->zl_keep_first = keep_first; 45610685SGeorge.Wilson@Sun.COM if (zh->zh_flags & ZIL_REPLAY_NEEDED) { 45710685SGeorge.Wilson@Sun.COM ASSERT(!keep_first); 4581807Sbonwick (void) zil_parse(zilog, zil_free_log_block, 4591807Sbonwick zil_free_log_record, tx, zh->zh_claim_txg); 46010685SGeorge.Wilson@Sun.COM } else { 46110685SGeorge.Wilson@Sun.COM /* 46210685SGeorge.Wilson@Sun.COM * Would like to assert zil_empty() but that 46310685SGeorge.Wilson@Sun.COM * would force us to read the log chain which 46410685SGeorge.Wilson@Sun.COM * requires us to do I/O to the log. This is 46510685SGeorge.Wilson@Sun.COM * overkill since we really just want to destroy 46610685SGeorge.Wilson@Sun.COM * the chain anyway. 46710685SGeorge.Wilson@Sun.COM */ 46810685SGeorge.Wilson@Sun.COM if (!keep_first) { 46910685SGeorge.Wilson@Sun.COM blkptr_t bp = zh->zh_log; 47010685SGeorge.Wilson@Sun.COM zio_free_blk(zilog->zl_spa, &bp, txg); 47110685SGeorge.Wilson@Sun.COM } 4721807Sbonwick } 4731807Sbonwick } 4742638Sperrin mutex_exit(&zilog->zl_lock); 475789Sahrens 476789Sahrens dmu_tx_commit(tx); 477789Sahrens } 478789Sahrens 4798989SNeil.Perrin@Sun.COM /* 4808989SNeil.Perrin@Sun.COM * return true if the initial log block is not valid 4818989SNeil.Perrin@Sun.COM */ 4828989SNeil.Perrin@Sun.COM static boolean_t 4838989SNeil.Perrin@Sun.COM zil_empty(zilog_t *zilog) 4848989SNeil.Perrin@Sun.COM { 4858989SNeil.Perrin@Sun.COM const zil_header_t *zh = zilog->zl_header; 4868989SNeil.Perrin@Sun.COM arc_buf_t *abuf = NULL; 4878989SNeil.Perrin@Sun.COM 4888989SNeil.Perrin@Sun.COM if (BP_IS_HOLE(&zh->zh_log)) 4898989SNeil.Perrin@Sun.COM return (B_TRUE); 4908989SNeil.Perrin@Sun.COM 4918989SNeil.Perrin@Sun.COM if (zil_read_log_block(zilog, &zh->zh_log, &abuf) != 0) 4928989SNeil.Perrin@Sun.COM return (B_TRUE); 4938989SNeil.Perrin@Sun.COM 4948989SNeil.Perrin@Sun.COM VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 4958989SNeil.Perrin@Sun.COM return (B_FALSE); 4968989SNeil.Perrin@Sun.COM } 4978989SNeil.Perrin@Sun.COM 4982199Sahrens int 499789Sahrens zil_claim(char *osname, void *txarg) 500789Sahrens { 501789Sahrens dmu_tx_t *tx = txarg; 502789Sahrens uint64_t first_txg = dmu_tx_get_txg(tx); 503789Sahrens zilog_t *zilog; 504789Sahrens zil_header_t *zh; 505789Sahrens objset_t *os; 506789Sahrens int error; 507789Sahrens 50810298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(osname, FTAG, &os); 509789Sahrens if (error) { 5107294Sperrin cmn_err(CE_WARN, "can't open objset for %s", osname); 5112199Sahrens return (0); 512789Sahrens } 513789Sahrens 514789Sahrens zilog = dmu_objset_zil(os); 5151807Sbonwick zh = zil_header_in_syncing_context(zilog); 516789Sahrens 5179701SGeorge.Wilson@Sun.COM if (zilog->zl_spa->spa_log_state == SPA_LOG_CLEAR) { 5189701SGeorge.Wilson@Sun.COM if (!BP_IS_HOLE(&zh->zh_log)) 5199701SGeorge.Wilson@Sun.COM zio_free_blk(zilog->zl_spa, &zh->zh_log, first_txg); 5209701SGeorge.Wilson@Sun.COM BP_ZERO(&zh->zh_log); 5219701SGeorge.Wilson@Sun.COM dsl_dataset_dirty(dmu_objset_ds(os), tx); 522*10921STim.Haley@Sun.COM dmu_objset_rele(os, FTAG); 523*10921STim.Haley@Sun.COM return (0); 5249701SGeorge.Wilson@Sun.COM } 5259701SGeorge.Wilson@Sun.COM 526789Sahrens /* 5278989SNeil.Perrin@Sun.COM * Record here whether the zil has any records to replay. 5288989SNeil.Perrin@Sun.COM * If the header block pointer is null or the block points 5298989SNeil.Perrin@Sun.COM * to the stubby then we know there are no valid log records. 5308989SNeil.Perrin@Sun.COM * We use the header to store this state as the the zilog gets 5318989SNeil.Perrin@Sun.COM * freed later in dmu_objset_close(). 5328989SNeil.Perrin@Sun.COM * The flags (and the rest of the header fields) are cleared in 5338989SNeil.Perrin@Sun.COM * zil_sync() as a result of a zil_destroy(), after replaying the log. 5348989SNeil.Perrin@Sun.COM * 5358989SNeil.Perrin@Sun.COM * Note, the intent log can be empty but still need the 5368989SNeil.Perrin@Sun.COM * stubby to be claimed. 5378989SNeil.Perrin@Sun.COM */ 5389701SGeorge.Wilson@Sun.COM if (!zil_empty(zilog)) { 5398989SNeil.Perrin@Sun.COM zh->zh_flags |= ZIL_REPLAY_NEEDED; 5409701SGeorge.Wilson@Sun.COM dsl_dataset_dirty(dmu_objset_ds(os), tx); 5419701SGeorge.Wilson@Sun.COM } 5428989SNeil.Perrin@Sun.COM 5438989SNeil.Perrin@Sun.COM /* 5441807Sbonwick * Claim all log blocks if we haven't already done so, and remember 5451807Sbonwick * the highest claimed sequence number. This ensures that if we can 5461807Sbonwick * read only part of the log now (e.g. due to a missing device), 5471807Sbonwick * but we can read the entire log later, we will not try to replay 5481807Sbonwick * or destroy beyond the last block we successfully claimed. 549789Sahrens */ 550789Sahrens ASSERT3U(zh->zh_claim_txg, <=, first_txg); 551789Sahrens if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) { 552789Sahrens zh->zh_claim_txg = first_txg; 5531807Sbonwick zh->zh_claim_seq = zil_parse(zilog, zil_claim_log_block, 5541807Sbonwick zil_claim_log_record, tx, first_txg); 555789Sahrens dsl_dataset_dirty(dmu_objset_ds(os), tx); 556789Sahrens } 5571807Sbonwick 558789Sahrens ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1)); 55910298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 5602199Sahrens return (0); 561789Sahrens } 562789Sahrens 5637294Sperrin /* 5647294Sperrin * Check the log by walking the log chain. 5657294Sperrin * Checksum errors are ok as they indicate the end of the chain. 5667294Sperrin * Any other error (no device or read failure) returns an error. 5677294Sperrin */ 5687294Sperrin /* ARGSUSED */ 5697294Sperrin int 5707294Sperrin zil_check_log_chain(char *osname, void *txarg) 5717294Sperrin { 5727294Sperrin zilog_t *zilog; 5737294Sperrin zil_header_t *zh; 5747294Sperrin blkptr_t blk; 5757294Sperrin arc_buf_t *abuf; 5767294Sperrin objset_t *os; 5777294Sperrin char *lrbuf; 5787294Sperrin zil_trailer_t *ztp; 5797294Sperrin int error; 5807294Sperrin 58110298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(osname, FTAG, &os); 5827294Sperrin if (error) { 5837294Sperrin cmn_err(CE_WARN, "can't open objset for %s", osname); 5847294Sperrin return (0); 5857294Sperrin } 5867294Sperrin 5877294Sperrin zilog = dmu_objset_zil(os); 5887294Sperrin zh = zil_header_in_syncing_context(zilog); 5897294Sperrin blk = zh->zh_log; 5907294Sperrin if (BP_IS_HOLE(&blk)) { 59110298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 5927294Sperrin return (0); /* no chain */ 5937294Sperrin } 5947294Sperrin 5957294Sperrin for (;;) { 5967294Sperrin error = zil_read_log_block(zilog, &blk, &abuf); 5977294Sperrin if (error) 5987294Sperrin break; 5997294Sperrin lrbuf = abuf->b_data; 6007294Sperrin ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1; 6017294Sperrin blk = ztp->zit_next_blk; 6027294Sperrin VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 6037294Sperrin } 60410298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 6057294Sperrin if (error == ECKSUM) 6067294Sperrin return (0); /* normal end of chain */ 6077294Sperrin return (error); 6087294Sperrin } 6097294Sperrin 6105688Sbonwick static int 6115688Sbonwick zil_vdev_compare(const void *x1, const void *x2) 612789Sahrens { 6135875Sperrin uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev; 6145875Sperrin uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev; 6155688Sbonwick 6165688Sbonwick if (v1 < v2) 6175688Sbonwick return (-1); 6185688Sbonwick if (v1 > v2) 6195688Sbonwick return (1); 6205688Sbonwick 6215688Sbonwick return (0); 6225688Sbonwick } 6235688Sbonwick 6245688Sbonwick void 6255688Sbonwick zil_add_block(zilog_t *zilog, blkptr_t *bp) 6265688Sbonwick { 6275688Sbonwick avl_tree_t *t = &zilog->zl_vdev_tree; 6285688Sbonwick avl_index_t where; 6295688Sbonwick zil_vdev_node_t *zv, zvsearch; 6305688Sbonwick int ndvas = BP_GET_NDVAS(bp); 6315688Sbonwick int i; 632789Sahrens 6332986Sek110237 if (zfs_nocacheflush) 634789Sahrens return; 635789Sahrens 6365688Sbonwick ASSERT(zilog->zl_writer); 6375688Sbonwick 6385688Sbonwick /* 6395688Sbonwick * Even though we're zl_writer, we still need a lock because the 6405688Sbonwick * zl_get_data() callbacks may have dmu_sync() done callbacks 6415688Sbonwick * that will run concurrently. 6425688Sbonwick */ 6435688Sbonwick mutex_enter(&zilog->zl_vdev_lock); 6445688Sbonwick for (i = 0; i < ndvas; i++) { 6455688Sbonwick zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]); 6465688Sbonwick if (avl_find(t, &zvsearch, &where) == NULL) { 6475688Sbonwick zv = kmem_alloc(sizeof (*zv), KM_SLEEP); 6485688Sbonwick zv->zv_vdev = zvsearch.zv_vdev; 6495688Sbonwick avl_insert(t, zv, where); 6503063Sperrin } 6513063Sperrin } 6525688Sbonwick mutex_exit(&zilog->zl_vdev_lock); 6533063Sperrin } 6543063Sperrin 655789Sahrens void 6562638Sperrin zil_flush_vdevs(zilog_t *zilog) 657789Sahrens { 6583063Sperrin spa_t *spa = zilog->zl_spa; 6595688Sbonwick avl_tree_t *t = &zilog->zl_vdev_tree; 6605688Sbonwick void *cookie = NULL; 6615688Sbonwick zil_vdev_node_t *zv; 6625688Sbonwick zio_t *zio; 6633063Sperrin 6643063Sperrin ASSERT(zilog->zl_writer); 665789Sahrens 6665688Sbonwick /* 6675688Sbonwick * We don't need zl_vdev_lock here because we're the zl_writer, 6685688Sbonwick * and all zl_get_data() callbacks are done. 6695688Sbonwick */ 6705688Sbonwick if (avl_numnodes(t) == 0) 6715688Sbonwick return; 6725688Sbonwick 6737754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 6745688Sbonwick 6757754SJeff.Bonwick@Sun.COM zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); 6765688Sbonwick 6775688Sbonwick while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) { 6785688Sbonwick vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev); 6795688Sbonwick if (vd != NULL) 6805688Sbonwick zio_flush(zio, vd); 6815688Sbonwick kmem_free(zv, sizeof (*zv)); 6823063Sperrin } 683789Sahrens 684789Sahrens /* 685789Sahrens * Wait for all the flushes to complete. Not all devices actually 686789Sahrens * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails. 687789Sahrens */ 6885688Sbonwick (void) zio_wait(zio); 6895688Sbonwick 6907754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 691789Sahrens } 692789Sahrens 693789Sahrens /* 694789Sahrens * Function called when a log block write completes 695789Sahrens */ 696789Sahrens static void 697789Sahrens zil_lwb_write_done(zio_t *zio) 698789Sahrens { 699789Sahrens lwb_t *lwb = zio->io_private; 700789Sahrens zilog_t *zilog = lwb->lwb_zilog; 701789Sahrens 7027754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF); 7037754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_CHECKSUM(zio->io_bp) == ZIO_CHECKSUM_ZILOG); 7047754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_TYPE(zio->io_bp) == DMU_OT_INTENT_LOG); 7057754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 7067754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_BYTEORDER(zio->io_bp) == ZFS_HOST_BYTEORDER); 7077754SJeff.Bonwick@Sun.COM ASSERT(!BP_IS_GANG(zio->io_bp)); 7087754SJeff.Bonwick@Sun.COM ASSERT(!BP_IS_HOLE(zio->io_bp)); 7097754SJeff.Bonwick@Sun.COM ASSERT(zio->io_bp->blk_fill == 0); 7107754SJeff.Bonwick@Sun.COM 711789Sahrens /* 7129493SNeil.Perrin@Sun.COM * Ensure the lwb buffer pointer is cleared before releasing 7139493SNeil.Perrin@Sun.COM * the txg. If we have had an allocation failure and 7149493SNeil.Perrin@Sun.COM * the txg is waiting to sync then we want want zil_sync() 7159493SNeil.Perrin@Sun.COM * to remove the lwb so that it's not picked up as the next new 7169493SNeil.Perrin@Sun.COM * one in zil_commit_writer(). zil_sync() will only remove 7179493SNeil.Perrin@Sun.COM * the lwb if lwb_buf is null. 718789Sahrens */ 719789Sahrens zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 720789Sahrens mutex_enter(&zilog->zl_lock); 721789Sahrens lwb->lwb_buf = NULL; 7224527Sperrin if (zio->io_error) 723789Sahrens zilog->zl_log_error = B_TRUE; 7249493SNeil.Perrin@Sun.COM 7259493SNeil.Perrin@Sun.COM /* 7269493SNeil.Perrin@Sun.COM * Now that we've written this log block, we have a stable pointer 7279493SNeil.Perrin@Sun.COM * to the next block in the chain, so it's OK to let the txg in 7289904SNeil.Perrin@Sun.COM * which we allocated the next block sync. We still have the 7299904SNeil.Perrin@Sun.COM * zl_lock to ensure zil_sync doesn't kmem free the lwb. 7309493SNeil.Perrin@Sun.COM */ 7319493SNeil.Perrin@Sun.COM txg_rele_to_sync(&lwb->lwb_txgh); 7329904SNeil.Perrin@Sun.COM mutex_exit(&zilog->zl_lock); 733789Sahrens } 734789Sahrens 735789Sahrens /* 7362237Smaybee * Initialize the io for a log block. 7372237Smaybee */ 7382237Smaybee static void 7392237Smaybee zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb) 7402237Smaybee { 7412237Smaybee zbookmark_t zb; 7422237Smaybee 7432237Smaybee zb.zb_objset = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET]; 7442237Smaybee zb.zb_object = 0; 7452237Smaybee zb.zb_level = -1; 7462237Smaybee zb.zb_blkid = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 7472237Smaybee 7482638Sperrin if (zilog->zl_root_zio == NULL) { 7492638Sperrin zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL, 7502638Sperrin ZIO_FLAG_CANFAIL); 7512638Sperrin } 7523063Sperrin if (lwb->lwb_zio == NULL) { 7533063Sperrin lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa, 7549701SGeorge.Wilson@Sun.COM 0, &lwb->lwb_blk, lwb->lwb_buf, lwb->lwb_sz, 7559701SGeorge.Wilson@Sun.COM zil_lwb_write_done, lwb, ZIO_PRIORITY_LOG_WRITE, 75610685SGeorge.Wilson@Sun.COM ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE, &zb); 7573063Sperrin } 7582237Smaybee } 7592237Smaybee 7602237Smaybee /* 76110879SNeil.Perrin@Sun.COM * Use the slog as long as the logbias is 'latency' and the current commit size 76210879SNeil.Perrin@Sun.COM * is less than the limit or the total list size is less than 2X the limit. 76310879SNeil.Perrin@Sun.COM * Limit checking is disabled by setting zil_slog_limit to UINT64_MAX. 76410879SNeil.Perrin@Sun.COM */ 76510879SNeil.Perrin@Sun.COM uint64_t zil_slog_limit = 1024 * 1024; 76610879SNeil.Perrin@Sun.COM #define USE_SLOG(zilog) (((zilog)->zl_logbias == ZFS_LOGBIAS_LATENCY) && \ 76710879SNeil.Perrin@Sun.COM (((zilog)->zl_cur_used < zil_slog_limit) || \ 76810879SNeil.Perrin@Sun.COM ((zilog)->zl_itx_list_sz < (zil_slog_limit << 1)))) 76910879SNeil.Perrin@Sun.COM 77010879SNeil.Perrin@Sun.COM /* 771789Sahrens * Start a log block write and advance to the next log block. 772789Sahrens * Calls are serialized. 773789Sahrens */ 774789Sahrens static lwb_t * 775789Sahrens zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb) 776789Sahrens { 777789Sahrens lwb_t *nlwb; 778789Sahrens zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1; 7791807Sbonwick spa_t *spa = zilog->zl_spa; 7801807Sbonwick blkptr_t *bp = &ztp->zit_next_blk; 781789Sahrens uint64_t txg; 782789Sahrens uint64_t zil_blksz; 783789Sahrens int error; 784789Sahrens 785789Sahrens ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb)); 786789Sahrens 787789Sahrens /* 788789Sahrens * Allocate the next block and save its address in this block 789789Sahrens * before writing it in order to establish the log chain. 790789Sahrens * Note that if the allocation of nlwb synced before we wrote 791789Sahrens * the block that points at it (lwb), we'd leak it if we crashed. 792789Sahrens * Therefore, we don't do txg_rele_to_sync() until zil_lwb_write_done(). 793789Sahrens */ 794789Sahrens txg = txg_hold_open(zilog->zl_dmu_pool, &lwb->lwb_txgh); 795789Sahrens txg_rele_to_quiesce(&lwb->lwb_txgh); 796789Sahrens 797789Sahrens /* 7981141Sperrin * Pick a ZIL blocksize. We request a size that is the 7991141Sperrin * maximum of the previous used size, the current used size and 8001141Sperrin * the amount waiting in the queue. 801789Sahrens */ 8022237Smaybee zil_blksz = MAX(zilog->zl_prev_used, 8032237Smaybee zilog->zl_cur_used + sizeof (*ztp)); 8041141Sperrin zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp)); 8051842Sperrin zil_blksz = P2ROUNDUP_TYPED(zil_blksz, ZIL_MIN_BLKSZ, uint64_t); 8061141Sperrin if (zil_blksz > ZIL_MAX_BLKSZ) 8071141Sperrin zil_blksz = ZIL_MAX_BLKSZ; 808789Sahrens 8093063Sperrin BP_ZERO(bp); 8103063Sperrin /* pass the old blkptr in order to spread log blocks across devs */ 81110310SNeil.Perrin@Sun.COM error = zio_alloc_blk(spa, zil_blksz, bp, &lwb->lwb_blk, txg, 81210879SNeil.Perrin@Sun.COM USE_SLOG(zilog)); 813789Sahrens if (error) { 8143668Sgw25295 dmu_tx_t *tx = dmu_tx_create_assigned(zilog->zl_dmu_pool, txg); 8153668Sgw25295 8161544Seschrock /* 8173668Sgw25295 * We dirty the dataset to ensure that zil_sync() will 8183668Sgw25295 * be called to remove this lwb from our zl_lwb_list. 8193668Sgw25295 * Failing to do so, may leave an lwb with a NULL lwb_buf 8203668Sgw25295 * hanging around on the zl_lwb_list. 8213668Sgw25295 */ 8223668Sgw25295 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 8233848Sgw25295 dmu_tx_commit(tx); 8243668Sgw25295 8253668Sgw25295 /* 8263668Sgw25295 * Since we've just experienced an allocation failure so we 8273668Sgw25295 * terminate the current lwb and send it on its way. 8283668Sgw25295 */ 8293668Sgw25295 ztp->zit_pad = 0; 8303668Sgw25295 ztp->zit_nused = lwb->lwb_nused; 8313668Sgw25295 ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum; 8323668Sgw25295 zio_nowait(lwb->lwb_zio); 8333668Sgw25295 8343668Sgw25295 /* 8351544Seschrock * By returning NULL the caller will call tx_wait_synced() 8361544Seschrock */ 837789Sahrens return (NULL); 838789Sahrens } 839789Sahrens 8401807Sbonwick ASSERT3U(bp->blk_birth, ==, txg); 8411544Seschrock ztp->zit_pad = 0; 842789Sahrens ztp->zit_nused = lwb->lwb_nused; 843789Sahrens ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum; 8441807Sbonwick bp->blk_cksum = lwb->lwb_blk.blk_cksum; 8451807Sbonwick bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++; 846789Sahrens 847789Sahrens /* 848789Sahrens * Allocate a new log write buffer (lwb). 849789Sahrens */ 850789Sahrens nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 851789Sahrens 852789Sahrens nlwb->lwb_zilog = zilog; 8531807Sbonwick nlwb->lwb_blk = *bp; 854789Sahrens nlwb->lwb_nused = 0; 855789Sahrens nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk); 856789Sahrens nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz); 857789Sahrens nlwb->lwb_max_txg = txg; 8582237Smaybee nlwb->lwb_zio = NULL; 859789Sahrens 860789Sahrens /* 8613063Sperrin * Put new lwb at the end of the log chain 862789Sahrens */ 863789Sahrens mutex_enter(&zilog->zl_lock); 864789Sahrens list_insert_tail(&zilog->zl_lwb_list, nlwb); 8653063Sperrin mutex_exit(&zilog->zl_lock); 8663063Sperrin 8675688Sbonwick /* Record the block for later vdev flushing */ 8685688Sbonwick zil_add_block(zilog, &lwb->lwb_blk); 869789Sahrens 870789Sahrens /* 8712237Smaybee * kick off the write for the old log block 872789Sahrens */ 8732237Smaybee dprintf_bp(&lwb->lwb_blk, "lwb %p txg %llu: ", lwb, txg); 8743063Sperrin ASSERT(lwb->lwb_zio); 8752237Smaybee zio_nowait(lwb->lwb_zio); 876789Sahrens 877789Sahrens return (nlwb); 878789Sahrens } 879789Sahrens 880789Sahrens static lwb_t * 881789Sahrens zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb) 882789Sahrens { 883789Sahrens lr_t *lrc = &itx->itx_lr; /* common log record */ 8842237Smaybee lr_write_t *lr = (lr_write_t *)lrc; 885789Sahrens uint64_t txg = lrc->lrc_txg; 886789Sahrens uint64_t reclen = lrc->lrc_reclen; 8872237Smaybee uint64_t dlen; 888789Sahrens 889789Sahrens if (lwb == NULL) 890789Sahrens return (NULL); 891789Sahrens ASSERT(lwb->lwb_buf != NULL); 892789Sahrens 8932237Smaybee if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY) 8942237Smaybee dlen = P2ROUNDUP_TYPED( 8952237Smaybee lr->lr_length, sizeof (uint64_t), uint64_t); 8962237Smaybee else 8972237Smaybee dlen = 0; 8981669Sperrin 8991669Sperrin zilog->zl_cur_used += (reclen + dlen); 9001669Sperrin 9013063Sperrin zil_lwb_write_init(zilog, lwb); 9023063Sperrin 9031669Sperrin /* 9041669Sperrin * If this record won't fit in the current log block, start a new one. 9051669Sperrin */ 9061669Sperrin if (lwb->lwb_nused + reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) { 9071669Sperrin lwb = zil_lwb_write_start(zilog, lwb); 9082237Smaybee if (lwb == NULL) 9091669Sperrin return (NULL); 9103063Sperrin zil_lwb_write_init(zilog, lwb); 9111669Sperrin ASSERT(lwb->lwb_nused == 0); 9121669Sperrin if (reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) { 9131669Sperrin txg_wait_synced(zilog->zl_dmu_pool, txg); 914789Sahrens return (lwb); 915789Sahrens } 916789Sahrens } 917789Sahrens 9182638Sperrin /* 9192638Sperrin * Update the lrc_seq, to be log record sequence number. See zil.h 9202638Sperrin * Then copy the record to the log buffer. 9212638Sperrin */ 9222638Sperrin lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */ 923789Sahrens bcopy(lrc, lwb->lwb_buf + lwb->lwb_nused, reclen); 9242237Smaybee 9252237Smaybee /* 9262237Smaybee * If it's a write, fetch the data or get its blkptr as appropriate. 9272237Smaybee */ 9282237Smaybee if (lrc->lrc_txtype == TX_WRITE) { 9292237Smaybee if (txg > spa_freeze_txg(zilog->zl_spa)) 9302237Smaybee txg_wait_synced(zilog->zl_dmu_pool, txg); 9312237Smaybee if (itx->itx_wr_state != WR_COPIED) { 9322237Smaybee char *dbuf; 9332237Smaybee int error; 9342237Smaybee 9352237Smaybee /* alignment is guaranteed */ 9362237Smaybee lr = (lr_write_t *)(lwb->lwb_buf + lwb->lwb_nused); 9372237Smaybee if (dlen) { 9382237Smaybee ASSERT(itx->itx_wr_state == WR_NEED_COPY); 9392237Smaybee dbuf = lwb->lwb_buf + lwb->lwb_nused + reclen; 9402237Smaybee lr->lr_common.lrc_reclen += dlen; 9412237Smaybee } else { 9422237Smaybee ASSERT(itx->itx_wr_state == WR_INDIRECT); 9432237Smaybee dbuf = NULL; 9442237Smaybee } 9452237Smaybee error = zilog->zl_get_data( 9462237Smaybee itx->itx_private, lr, dbuf, lwb->lwb_zio); 94710209SMark.Musante@Sun.COM if (error == EIO) { 94810209SMark.Musante@Sun.COM txg_wait_synced(zilog->zl_dmu_pool, txg); 94910209SMark.Musante@Sun.COM return (lwb); 95010209SMark.Musante@Sun.COM } 9512237Smaybee if (error) { 9522237Smaybee ASSERT(error == ENOENT || error == EEXIST || 9532237Smaybee error == EALREADY); 9542237Smaybee return (lwb); 9552237Smaybee } 9562237Smaybee } 9571669Sperrin } 9582237Smaybee 9592237Smaybee lwb->lwb_nused += reclen + dlen; 960789Sahrens lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg); 961789Sahrens ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb)); 962789Sahrens ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0); 963789Sahrens 964789Sahrens return (lwb); 965789Sahrens } 966789Sahrens 967789Sahrens itx_t * 9685331Samw zil_itx_create(uint64_t txtype, size_t lrsize) 969789Sahrens { 970789Sahrens itx_t *itx; 971789Sahrens 9721842Sperrin lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t); 973789Sahrens 974789Sahrens itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP); 975789Sahrens itx->itx_lr.lrc_txtype = txtype; 976789Sahrens itx->itx_lr.lrc_reclen = lrsize; 9776101Sperrin itx->itx_sod = lrsize; /* if write & WR_NEED_COPY will be increased */ 978789Sahrens itx->itx_lr.lrc_seq = 0; /* defensive */ 979789Sahrens 980789Sahrens return (itx); 981789Sahrens } 982789Sahrens 983789Sahrens uint64_t 984789Sahrens zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx) 985789Sahrens { 986789Sahrens uint64_t seq; 987789Sahrens 988789Sahrens ASSERT(itx->itx_lr.lrc_seq == 0); 989789Sahrens 990789Sahrens mutex_enter(&zilog->zl_lock); 991789Sahrens list_insert_tail(&zilog->zl_itx_list, itx); 9926101Sperrin zilog->zl_itx_list_sz += itx->itx_sod; 993789Sahrens itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx); 994789Sahrens itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq; 995789Sahrens mutex_exit(&zilog->zl_lock); 996789Sahrens 997789Sahrens return (seq); 998789Sahrens } 999789Sahrens 1000789Sahrens /* 1001789Sahrens * Free up all in-memory intent log transactions that have now been synced. 1002789Sahrens */ 1003789Sahrens static void 1004789Sahrens zil_itx_clean(zilog_t *zilog) 1005789Sahrens { 1006789Sahrens uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa); 1007789Sahrens uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa); 10083778Sjohansen list_t clean_list; 1009789Sahrens itx_t *itx; 1010789Sahrens 10113778Sjohansen list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node)); 10123778Sjohansen 1013789Sahrens mutex_enter(&zilog->zl_lock); 10142638Sperrin /* wait for a log writer to finish walking list */ 10152638Sperrin while (zilog->zl_writer) { 10162638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 10172638Sperrin } 10183778Sjohansen 10193778Sjohansen /* 10203778Sjohansen * Move the sync'd log transactions to a separate list so we can call 10213778Sjohansen * kmem_free without holding the zl_lock. 10223778Sjohansen * 10233778Sjohansen * There is no need to set zl_writer as we don't drop zl_lock here 10243778Sjohansen */ 1025789Sahrens while ((itx = list_head(&zilog->zl_itx_list)) != NULL && 1026789Sahrens itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) { 1027789Sahrens list_remove(&zilog->zl_itx_list, itx); 10286101Sperrin zilog->zl_itx_list_sz -= itx->itx_sod; 10293778Sjohansen list_insert_tail(&clean_list, itx); 10303778Sjohansen } 10313778Sjohansen cv_broadcast(&zilog->zl_cv_writer); 10323778Sjohansen mutex_exit(&zilog->zl_lock); 10333778Sjohansen 10343778Sjohansen /* destroy sync'd log transactions */ 10353778Sjohansen while ((itx = list_head(&clean_list)) != NULL) { 10363778Sjohansen list_remove(&clean_list, itx); 1037789Sahrens kmem_free(itx, offsetof(itx_t, itx_lr) 1038789Sahrens + itx->itx_lr.lrc_reclen); 1039789Sahrens } 10403778Sjohansen list_destroy(&clean_list); 1041789Sahrens } 1042789Sahrens 10432638Sperrin /* 10443063Sperrin * If there are any in-memory intent log transactions which have now been 10453063Sperrin * synced then start up a taskq to free them. 10462638Sperrin */ 1047789Sahrens void 1048789Sahrens zil_clean(zilog_t *zilog) 1049789Sahrens { 10503063Sperrin itx_t *itx; 10513063Sperrin 1052789Sahrens mutex_enter(&zilog->zl_lock); 10533063Sperrin itx = list_head(&zilog->zl_itx_list); 10543063Sperrin if ((itx != NULL) && 10553063Sperrin (itx->itx_lr.lrc_txg <= spa_last_synced_txg(zilog->zl_spa))) { 1056789Sahrens (void) taskq_dispatch(zilog->zl_clean_taskq, 105710879SNeil.Perrin@Sun.COM (task_func_t *)zil_itx_clean, zilog, TQ_NOSLEEP); 10583063Sperrin } 1059789Sahrens mutex_exit(&zilog->zl_lock); 1060789Sahrens } 1061789Sahrens 10627754SJeff.Bonwick@Sun.COM static void 10632638Sperrin zil_commit_writer(zilog_t *zilog, uint64_t seq, uint64_t foid) 1064789Sahrens { 1065789Sahrens uint64_t txg; 10663063Sperrin uint64_t commit_seq = 0; 10672638Sperrin itx_t *itx, *itx_next = (itx_t *)-1; 1068789Sahrens lwb_t *lwb; 1069789Sahrens spa_t *spa; 1070789Sahrens 10712638Sperrin zilog->zl_writer = B_TRUE; 10727754SJeff.Bonwick@Sun.COM ASSERT(zilog->zl_root_zio == NULL); 1073789Sahrens spa = zilog->zl_spa; 1074789Sahrens 1075789Sahrens if (zilog->zl_suspend) { 1076789Sahrens lwb = NULL; 1077789Sahrens } else { 1078789Sahrens lwb = list_tail(&zilog->zl_lwb_list); 1079789Sahrens if (lwb == NULL) { 10802638Sperrin /* 10812638Sperrin * Return if there's nothing to flush before we 10822638Sperrin * dirty the fs by calling zil_create() 10832638Sperrin */ 10842638Sperrin if (list_is_empty(&zilog->zl_itx_list)) { 10852638Sperrin zilog->zl_writer = B_FALSE; 10862638Sperrin return; 10872638Sperrin } 1088789Sahrens mutex_exit(&zilog->zl_lock); 1089789Sahrens zil_create(zilog); 1090789Sahrens mutex_enter(&zilog->zl_lock); 1091789Sahrens lwb = list_tail(&zilog->zl_lwb_list); 1092789Sahrens } 1093789Sahrens } 1094789Sahrens 10953063Sperrin /* Loop through in-memory log transactions filling log blocks. */ 10962638Sperrin DTRACE_PROBE1(zil__cw1, zilog_t *, zilog); 1097789Sahrens for (;;) { 10982638Sperrin /* 10992638Sperrin * Find the next itx to push: 11002638Sperrin * Push all transactions related to specified foid and all 11012638Sperrin * other transactions except TX_WRITE, TX_TRUNCATE, 11022638Sperrin * TX_SETATTR and TX_ACL for all other files. 11032638Sperrin */ 11042638Sperrin if (itx_next != (itx_t *)-1) 11052638Sperrin itx = itx_next; 11062638Sperrin else 11072638Sperrin itx = list_head(&zilog->zl_itx_list); 11082638Sperrin for (; itx != NULL; itx = list_next(&zilog->zl_itx_list, itx)) { 11092638Sperrin if (foid == 0) /* push all foids? */ 11102638Sperrin break; 11113063Sperrin if (itx->itx_sync) /* push all O_[D]SYNC */ 11123063Sperrin break; 11132638Sperrin switch (itx->itx_lr.lrc_txtype) { 11142638Sperrin case TX_SETATTR: 11152638Sperrin case TX_WRITE: 11162638Sperrin case TX_TRUNCATE: 11172638Sperrin case TX_ACL: 11182638Sperrin /* lr_foid is same offset for these records */ 11192638Sperrin if (((lr_write_t *)&itx->itx_lr)->lr_foid 11202638Sperrin != foid) { 11212638Sperrin continue; /* skip this record */ 11222638Sperrin } 11232638Sperrin } 11242638Sperrin break; 11252638Sperrin } 1126789Sahrens if (itx == NULL) 1127789Sahrens break; 1128789Sahrens 1129789Sahrens if ((itx->itx_lr.lrc_seq > seq) && 11302638Sperrin ((lwb == NULL) || (lwb->lwb_nused == 0) || 11316101Sperrin (lwb->lwb_nused + itx->itx_sod > ZIL_BLK_DATA_SZ(lwb)))) { 1132789Sahrens break; 11333063Sperrin } 1134789Sahrens 11352638Sperrin /* 11362638Sperrin * Save the next pointer. Even though we soon drop 11372638Sperrin * zl_lock all threads that may change the list 11382638Sperrin * (another writer or zil_itx_clean) can't do so until 11392638Sperrin * they have zl_writer. 11402638Sperrin */ 11412638Sperrin itx_next = list_next(&zilog->zl_itx_list, itx); 1142789Sahrens list_remove(&zilog->zl_itx_list, itx); 11436101Sperrin zilog->zl_itx_list_sz -= itx->itx_sod; 11443063Sperrin mutex_exit(&zilog->zl_lock); 1145789Sahrens txg = itx->itx_lr.lrc_txg; 1146789Sahrens ASSERT(txg); 1147789Sahrens 1148789Sahrens if (txg > spa_last_synced_txg(spa) || 1149789Sahrens txg > spa_freeze_txg(spa)) 1150789Sahrens lwb = zil_lwb_commit(zilog, itx, lwb); 1151789Sahrens kmem_free(itx, offsetof(itx_t, itx_lr) 1152789Sahrens + itx->itx_lr.lrc_reclen); 1153789Sahrens mutex_enter(&zilog->zl_lock); 1154789Sahrens } 11552638Sperrin DTRACE_PROBE1(zil__cw2, zilog_t *, zilog); 11563063Sperrin /* determine commit sequence number */ 11573063Sperrin itx = list_head(&zilog->zl_itx_list); 11583063Sperrin if (itx) 11593063Sperrin commit_seq = itx->itx_lr.lrc_seq; 11603063Sperrin else 11613063Sperrin commit_seq = zilog->zl_itx_seq; 1162789Sahrens mutex_exit(&zilog->zl_lock); 1163789Sahrens 1164789Sahrens /* write the last block out */ 11653063Sperrin if (lwb != NULL && lwb->lwb_zio != NULL) 1166789Sahrens lwb = zil_lwb_write_start(zilog, lwb); 1167789Sahrens 11681141Sperrin zilog->zl_prev_used = zilog->zl_cur_used; 11691141Sperrin zilog->zl_cur_used = 0; 11701141Sperrin 11712638Sperrin /* 11722638Sperrin * Wait if necessary for the log blocks to be on stable storage. 11732638Sperrin */ 11742638Sperrin if (zilog->zl_root_zio) { 11752638Sperrin DTRACE_PROBE1(zil__cw3, zilog_t *, zilog); 11762638Sperrin (void) zio_wait(zilog->zl_root_zio); 11777754SJeff.Bonwick@Sun.COM zilog->zl_root_zio = NULL; 11782638Sperrin DTRACE_PROBE1(zil__cw4, zilog_t *, zilog); 11795688Sbonwick zil_flush_vdevs(zilog); 1180789Sahrens } 11811141Sperrin 1182789Sahrens if (zilog->zl_log_error || lwb == NULL) { 1183789Sahrens zilog->zl_log_error = 0; 1184789Sahrens txg_wait_synced(zilog->zl_dmu_pool, 0); 1185789Sahrens } 11863063Sperrin 11873063Sperrin mutex_enter(&zilog->zl_lock); 11881141Sperrin zilog->zl_writer = B_FALSE; 11893063Sperrin 11903063Sperrin ASSERT3U(commit_seq, >=, zilog->zl_commit_seq); 11913063Sperrin zilog->zl_commit_seq = commit_seq; 11922638Sperrin } 11932638Sperrin 11942638Sperrin /* 11952638Sperrin * Push zfs transactions to stable storage up to the supplied sequence number. 11962638Sperrin * If foid is 0 push out all transactions, otherwise push only those 11972638Sperrin * for that file or might have been used to create that file. 11982638Sperrin */ 11992638Sperrin void 12002638Sperrin zil_commit(zilog_t *zilog, uint64_t seq, uint64_t foid) 12012638Sperrin { 12022638Sperrin if (zilog == NULL || seq == 0) 12032638Sperrin return; 12042638Sperrin 12052638Sperrin mutex_enter(&zilog->zl_lock); 12062638Sperrin 12072638Sperrin seq = MIN(seq, zilog->zl_itx_seq); /* cap seq at largest itx seq */ 12082638Sperrin 12093063Sperrin while (zilog->zl_writer) { 12102638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 12113063Sperrin if (seq < zilog->zl_commit_seq) { 12123063Sperrin mutex_exit(&zilog->zl_lock); 12133063Sperrin return; 12143063Sperrin } 12153063Sperrin } 12162638Sperrin zil_commit_writer(zilog, seq, foid); /* drops zl_lock */ 12173063Sperrin /* wake up others waiting on the commit */ 12183063Sperrin cv_broadcast(&zilog->zl_cv_writer); 12193063Sperrin mutex_exit(&zilog->zl_lock); 1220789Sahrens } 1221789Sahrens 1222789Sahrens /* 1223789Sahrens * Called in syncing context to free committed log blocks and update log header. 1224789Sahrens */ 1225789Sahrens void 1226789Sahrens zil_sync(zilog_t *zilog, dmu_tx_t *tx) 1227789Sahrens { 12281807Sbonwick zil_header_t *zh = zil_header_in_syncing_context(zilog); 1229789Sahrens uint64_t txg = dmu_tx_get_txg(tx); 1230789Sahrens spa_t *spa = zilog->zl_spa; 1231789Sahrens lwb_t *lwb; 1232789Sahrens 12339396SMatthew.Ahrens@Sun.COM /* 12349396SMatthew.Ahrens@Sun.COM * We don't zero out zl_destroy_txg, so make sure we don't try 12359396SMatthew.Ahrens@Sun.COM * to destroy it twice. 12369396SMatthew.Ahrens@Sun.COM */ 12379396SMatthew.Ahrens@Sun.COM if (spa_sync_pass(spa) != 1) 12389396SMatthew.Ahrens@Sun.COM return; 12399396SMatthew.Ahrens@Sun.COM 12401807Sbonwick mutex_enter(&zilog->zl_lock); 12411807Sbonwick 1242789Sahrens ASSERT(zilog->zl_stop_sync == 0); 1243789Sahrens 12448227SNeil.Perrin@Sun.COM zh->zh_replay_seq = zilog->zl_replayed_seq[txg & TXG_MASK]; 1245789Sahrens 1246789Sahrens if (zilog->zl_destroy_txg == txg) { 12471807Sbonwick blkptr_t blk = zh->zh_log; 12481807Sbonwick 12491807Sbonwick ASSERT(list_head(&zilog->zl_lwb_list) == NULL); 12501807Sbonwick 12511807Sbonwick bzero(zh, sizeof (zil_header_t)); 12528227SNeil.Perrin@Sun.COM bzero(zilog->zl_replayed_seq, sizeof (zilog->zl_replayed_seq)); 12531807Sbonwick 12541807Sbonwick if (zilog->zl_keep_first) { 12551807Sbonwick /* 12561807Sbonwick * If this block was part of log chain that couldn't 12571807Sbonwick * be claimed because a device was missing during 12581807Sbonwick * zil_claim(), but that device later returns, 12591807Sbonwick * then this block could erroneously appear valid. 12601807Sbonwick * To guard against this, assign a new GUID to the new 12611807Sbonwick * log chain so it doesn't matter what blk points to. 12621807Sbonwick */ 12631807Sbonwick zil_init_log_chain(zilog, &blk); 12641807Sbonwick zh->zh_log = blk; 12651807Sbonwick } 1266789Sahrens } 1267789Sahrens 12689701SGeorge.Wilson@Sun.COM while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 12692638Sperrin zh->zh_log = lwb->lwb_blk; 1270789Sahrens if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg) 1271789Sahrens break; 1272789Sahrens list_remove(&zilog->zl_lwb_list, lwb); 1273789Sahrens zio_free_blk(spa, &lwb->lwb_blk, txg); 1274789Sahrens kmem_cache_free(zil_lwb_cache, lwb); 12753668Sgw25295 12763668Sgw25295 /* 12773668Sgw25295 * If we don't have anything left in the lwb list then 12783668Sgw25295 * we've had an allocation failure and we need to zero 12793668Sgw25295 * out the zil_header blkptr so that we don't end 12803668Sgw25295 * up freeing the same block twice. 12813668Sgw25295 */ 12823668Sgw25295 if (list_head(&zilog->zl_lwb_list) == NULL) 12833668Sgw25295 BP_ZERO(&zh->zh_log); 1284789Sahrens } 1285789Sahrens mutex_exit(&zilog->zl_lock); 1286789Sahrens } 1287789Sahrens 1288789Sahrens void 1289789Sahrens zil_init(void) 1290789Sahrens { 1291789Sahrens zil_lwb_cache = kmem_cache_create("zil_lwb_cache", 12922856Snd150628 sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0); 1293789Sahrens } 1294789Sahrens 1295789Sahrens void 1296789Sahrens zil_fini(void) 1297789Sahrens { 1298789Sahrens kmem_cache_destroy(zil_lwb_cache); 1299789Sahrens } 1300789Sahrens 130110310SNeil.Perrin@Sun.COM void 130210310SNeil.Perrin@Sun.COM zil_set_logbias(zilog_t *zilog, uint64_t logbias) 130310310SNeil.Perrin@Sun.COM { 130410310SNeil.Perrin@Sun.COM zilog->zl_logbias = logbias; 130510310SNeil.Perrin@Sun.COM } 130610310SNeil.Perrin@Sun.COM 1307789Sahrens zilog_t * 1308789Sahrens zil_alloc(objset_t *os, zil_header_t *zh_phys) 1309789Sahrens { 1310789Sahrens zilog_t *zilog; 1311789Sahrens 1312789Sahrens zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP); 1313789Sahrens 1314789Sahrens zilog->zl_header = zh_phys; 1315789Sahrens zilog->zl_os = os; 1316789Sahrens zilog->zl_spa = dmu_objset_spa(os); 1317789Sahrens zilog->zl_dmu_pool = dmu_objset_pool(os); 13181807Sbonwick zilog->zl_destroy_txg = TXG_INITIAL - 1; 131910310SNeil.Perrin@Sun.COM zilog->zl_logbias = dmu_objset_logbias(os); 1320789Sahrens 13212856Snd150628 mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL); 13222856Snd150628 1323789Sahrens list_create(&zilog->zl_itx_list, sizeof (itx_t), 1324789Sahrens offsetof(itx_t, itx_node)); 1325789Sahrens 1326789Sahrens list_create(&zilog->zl_lwb_list, sizeof (lwb_t), 1327789Sahrens offsetof(lwb_t, lwb_node)); 1328789Sahrens 13295688Sbonwick mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL); 13305688Sbonwick 13315688Sbonwick avl_create(&zilog->zl_vdev_tree, zil_vdev_compare, 13325688Sbonwick sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node)); 1333789Sahrens 13345913Sperrin cv_init(&zilog->zl_cv_writer, NULL, CV_DEFAULT, NULL); 13355913Sperrin cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL); 13365913Sperrin 1337789Sahrens return (zilog); 1338789Sahrens } 1339789Sahrens 1340789Sahrens void 1341789Sahrens zil_free(zilog_t *zilog) 1342789Sahrens { 1343789Sahrens lwb_t *lwb; 1344789Sahrens 1345789Sahrens zilog->zl_stop_sync = 1; 1346789Sahrens 1347789Sahrens while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 1348789Sahrens list_remove(&zilog->zl_lwb_list, lwb); 1349789Sahrens if (lwb->lwb_buf != NULL) 1350789Sahrens zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 1351789Sahrens kmem_cache_free(zil_lwb_cache, lwb); 1352789Sahrens } 1353789Sahrens list_destroy(&zilog->zl_lwb_list); 1354789Sahrens 13555688Sbonwick avl_destroy(&zilog->zl_vdev_tree); 13565688Sbonwick mutex_destroy(&zilog->zl_vdev_lock); 1357789Sahrens 1358789Sahrens ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1359789Sahrens list_destroy(&zilog->zl_itx_list); 13602856Snd150628 mutex_destroy(&zilog->zl_lock); 1361789Sahrens 13625913Sperrin cv_destroy(&zilog->zl_cv_writer); 13635913Sperrin cv_destroy(&zilog->zl_cv_suspend); 13645913Sperrin 1365789Sahrens kmem_free(zilog, sizeof (zilog_t)); 1366789Sahrens } 1367789Sahrens 1368789Sahrens /* 1369789Sahrens * Open an intent log. 1370789Sahrens */ 1371789Sahrens zilog_t * 1372789Sahrens zil_open(objset_t *os, zil_get_data_t *get_data) 1373789Sahrens { 1374789Sahrens zilog_t *zilog = dmu_objset_zil(os); 1375789Sahrens 1376789Sahrens zilog->zl_get_data = get_data; 1377789Sahrens zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri, 1378789Sahrens 2, 2, TASKQ_PREPOPULATE); 1379789Sahrens 1380789Sahrens return (zilog); 1381789Sahrens } 1382789Sahrens 1383789Sahrens /* 1384789Sahrens * Close an intent log. 1385789Sahrens */ 1386789Sahrens void 1387789Sahrens zil_close(zilog_t *zilog) 1388789Sahrens { 13891807Sbonwick /* 13901807Sbonwick * If the log isn't already committed, mark the objset dirty 13911807Sbonwick * (so zil_sync() will be called) and wait for that txg to sync. 13921807Sbonwick */ 13931807Sbonwick if (!zil_is_committed(zilog)) { 13941807Sbonwick uint64_t txg; 13951807Sbonwick dmu_tx_t *tx = dmu_tx_create(zilog->zl_os); 13961807Sbonwick (void) dmu_tx_assign(tx, TXG_WAIT); 13971807Sbonwick dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 13981807Sbonwick txg = dmu_tx_get_txg(tx); 13991807Sbonwick dmu_tx_commit(tx); 14001807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, txg); 14011807Sbonwick } 14021807Sbonwick 1403789Sahrens taskq_destroy(zilog->zl_clean_taskq); 1404789Sahrens zilog->zl_clean_taskq = NULL; 1405789Sahrens zilog->zl_get_data = NULL; 1406789Sahrens 1407789Sahrens zil_itx_clean(zilog); 1408789Sahrens ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1409789Sahrens } 1410789Sahrens 1411789Sahrens /* 1412789Sahrens * Suspend an intent log. While in suspended mode, we still honor 1413789Sahrens * synchronous semantics, but we rely on txg_wait_synced() to do it. 1414789Sahrens * We suspend the log briefly when taking a snapshot so that the snapshot 1415789Sahrens * contains all the data it's supposed to, and has an empty intent log. 1416789Sahrens */ 1417789Sahrens int 1418789Sahrens zil_suspend(zilog_t *zilog) 1419789Sahrens { 14201807Sbonwick const zil_header_t *zh = zilog->zl_header; 1421789Sahrens 1422789Sahrens mutex_enter(&zilog->zl_lock); 14238989SNeil.Perrin@Sun.COM if (zh->zh_flags & ZIL_REPLAY_NEEDED) { /* unplayed log */ 1424789Sahrens mutex_exit(&zilog->zl_lock); 1425789Sahrens return (EBUSY); 1426789Sahrens } 14271807Sbonwick if (zilog->zl_suspend++ != 0) { 14281807Sbonwick /* 14291807Sbonwick * Someone else already began a suspend. 14301807Sbonwick * Just wait for them to finish. 14311807Sbonwick */ 14321807Sbonwick while (zilog->zl_suspending) 14331807Sbonwick cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock); 14341807Sbonwick mutex_exit(&zilog->zl_lock); 14351807Sbonwick return (0); 14361807Sbonwick } 14371807Sbonwick zilog->zl_suspending = B_TRUE; 1438789Sahrens mutex_exit(&zilog->zl_lock); 1439789Sahrens 14402638Sperrin zil_commit(zilog, UINT64_MAX, 0); 1441789Sahrens 14422638Sperrin /* 14432638Sperrin * Wait for any in-flight log writes to complete. 14442638Sperrin */ 1445789Sahrens mutex_enter(&zilog->zl_lock); 14462638Sperrin while (zilog->zl_writer) 14472638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 1448789Sahrens mutex_exit(&zilog->zl_lock); 1449789Sahrens 14501807Sbonwick zil_destroy(zilog, B_FALSE); 14511807Sbonwick 14521807Sbonwick mutex_enter(&zilog->zl_lock); 14531807Sbonwick zilog->zl_suspending = B_FALSE; 14541807Sbonwick cv_broadcast(&zilog->zl_cv_suspend); 14551807Sbonwick mutex_exit(&zilog->zl_lock); 1456789Sahrens 1457789Sahrens return (0); 1458789Sahrens } 1459789Sahrens 1460789Sahrens void 1461789Sahrens zil_resume(zilog_t *zilog) 1462789Sahrens { 1463789Sahrens mutex_enter(&zilog->zl_lock); 1464789Sahrens ASSERT(zilog->zl_suspend != 0); 1465789Sahrens zilog->zl_suspend--; 1466789Sahrens mutex_exit(&zilog->zl_lock); 1467789Sahrens } 1468789Sahrens 146910800SNeil.Perrin@Sun.COM /* 147010800SNeil.Perrin@Sun.COM * Read in the data for the dmu_sync()ed block, and change the log 147110800SNeil.Perrin@Sun.COM * record to write this whole block. 147210800SNeil.Perrin@Sun.COM */ 147310800SNeil.Perrin@Sun.COM void 147410800SNeil.Perrin@Sun.COM zil_get_replay_data(zilog_t *zilog, lr_write_t *lr) 147510800SNeil.Perrin@Sun.COM { 147610800SNeil.Perrin@Sun.COM blkptr_t *wbp = &lr->lr_blkptr; 147710800SNeil.Perrin@Sun.COM char *wbuf = (char *)(lr + 1); /* data follows lr_write_t */ 147810800SNeil.Perrin@Sun.COM uint64_t blksz; 147910800SNeil.Perrin@Sun.COM 148010800SNeil.Perrin@Sun.COM if (BP_IS_HOLE(wbp)) { /* compressed to a hole */ 148110800SNeil.Perrin@Sun.COM blksz = BP_GET_LSIZE(&lr->lr_blkptr); 148210800SNeil.Perrin@Sun.COM /* 148310800SNeil.Perrin@Sun.COM * If the blksz is zero then we must be replaying a log 148410800SNeil.Perrin@Sun.COM * from an version prior to setting the blksize of null blocks. 148510800SNeil.Perrin@Sun.COM * So we just zero the actual write size reqeusted. 148610800SNeil.Perrin@Sun.COM */ 148710800SNeil.Perrin@Sun.COM if (blksz == 0) { 148810800SNeil.Perrin@Sun.COM bzero(wbuf, lr->lr_length); 148910800SNeil.Perrin@Sun.COM return; 149010800SNeil.Perrin@Sun.COM } 149110800SNeil.Perrin@Sun.COM bzero(wbuf, blksz); 149210800SNeil.Perrin@Sun.COM } else { 149310800SNeil.Perrin@Sun.COM /* 149410800SNeil.Perrin@Sun.COM * A subsequent write may have overwritten this block, in which 149510800SNeil.Perrin@Sun.COM * case wbp may have been been freed and reallocated, and our 149610800SNeil.Perrin@Sun.COM * read of wbp may fail with a checksum error. We can safely 149710800SNeil.Perrin@Sun.COM * ignore this because the later write will provide the 149810800SNeil.Perrin@Sun.COM * correct data. 149910800SNeil.Perrin@Sun.COM */ 150010800SNeil.Perrin@Sun.COM zbookmark_t zb; 150110800SNeil.Perrin@Sun.COM 150210800SNeil.Perrin@Sun.COM zb.zb_objset = dmu_objset_id(zilog->zl_os); 150310800SNeil.Perrin@Sun.COM zb.zb_object = lr->lr_foid; 150410800SNeil.Perrin@Sun.COM zb.zb_level = 0; 150510800SNeil.Perrin@Sun.COM zb.zb_blkid = -1; /* unknown */ 150610800SNeil.Perrin@Sun.COM 150710800SNeil.Perrin@Sun.COM blksz = BP_GET_LSIZE(&lr->lr_blkptr); 150810800SNeil.Perrin@Sun.COM (void) zio_wait(zio_read(NULL, zilog->zl_spa, wbp, wbuf, blksz, 150910800SNeil.Perrin@Sun.COM NULL, NULL, ZIO_PRIORITY_SYNC_READ, 151010800SNeil.Perrin@Sun.COM ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb)); 151110800SNeil.Perrin@Sun.COM } 151210800SNeil.Perrin@Sun.COM lr->lr_offset -= lr->lr_offset % blksz; 151310800SNeil.Perrin@Sun.COM lr->lr_length = blksz; 151410800SNeil.Perrin@Sun.COM } 151510800SNeil.Perrin@Sun.COM 1516789Sahrens typedef struct zil_replay_arg { 1517789Sahrens objset_t *zr_os; 1518789Sahrens zil_replay_func_t **zr_replay; 1519789Sahrens void *zr_arg; 1520789Sahrens boolean_t zr_byteswap; 1521789Sahrens char *zr_lrbuf; 1522789Sahrens } zil_replay_arg_t; 1523789Sahrens 1524789Sahrens static void 1525789Sahrens zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg) 1526789Sahrens { 1527789Sahrens zil_replay_arg_t *zr = zra; 15281807Sbonwick const zil_header_t *zh = zilog->zl_header; 1529789Sahrens uint64_t reclen = lr->lrc_reclen; 1530789Sahrens uint64_t txtype = lr->lrc_txtype; 15313063Sperrin char *name; 15328227SNeil.Perrin@Sun.COM int pass, error; 1533789Sahrens 15348227SNeil.Perrin@Sun.COM if (!zilog->zl_replay) /* giving up */ 1535789Sahrens return; 1536789Sahrens 1537789Sahrens if (lr->lrc_txg < claim_txg) /* already committed */ 1538789Sahrens return; 1539789Sahrens 1540789Sahrens if (lr->lrc_seq <= zh->zh_replay_seq) /* already replayed */ 1541789Sahrens return; 1542789Sahrens 15435331Samw /* Strip case-insensitive bit, still present in log record */ 15445331Samw txtype &= ~TX_CI; 15455331Samw 15468227SNeil.Perrin@Sun.COM if (txtype == 0 || txtype >= TX_MAX_TYPE) { 15478227SNeil.Perrin@Sun.COM error = EINVAL; 15488227SNeil.Perrin@Sun.COM goto bad; 15498227SNeil.Perrin@Sun.COM } 15508227SNeil.Perrin@Sun.COM 1551789Sahrens /* 1552789Sahrens * Make a copy of the data so we can revise and extend it. 1553789Sahrens */ 1554789Sahrens bcopy(lr, zr->zr_lrbuf, reclen); 1555789Sahrens 1556789Sahrens /* 1557789Sahrens * The log block containing this lr may have been byteswapped 1558789Sahrens * so that we can easily examine common fields like lrc_txtype. 1559789Sahrens * However, the log is a mix of different data types, and only the 1560789Sahrens * replay vectors know how to byteswap their records. Therefore, if 1561789Sahrens * the lr was byteswapped, undo it before invoking the replay vector. 1562789Sahrens */ 1563789Sahrens if (zr->zr_byteswap) 1564789Sahrens byteswap_uint64_array(zr->zr_lrbuf, reclen); 1565789Sahrens 1566789Sahrens /* 1567789Sahrens * We must now do two things atomically: replay this log record, 15688227SNeil.Perrin@Sun.COM * and update the log header sequence number to reflect the fact that 15698227SNeil.Perrin@Sun.COM * we did so. At the end of each replay function the sequence number 15708227SNeil.Perrin@Sun.COM * is updated if we are in replay mode. 1571789Sahrens */ 15728227SNeil.Perrin@Sun.COM for (pass = 1; pass <= 2; pass++) { 15738227SNeil.Perrin@Sun.COM zilog->zl_replaying_seq = lr->lrc_seq; 15748227SNeil.Perrin@Sun.COM /* Only byteswap (if needed) on the 1st pass. */ 15758227SNeil.Perrin@Sun.COM error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lrbuf, 15768227SNeil.Perrin@Sun.COM zr->zr_byteswap && pass == 1); 1577789Sahrens 15783063Sperrin if (!error) 15793063Sperrin return; 15803063Sperrin 15813063Sperrin /* 15823063Sperrin * The DMU's dnode layer doesn't see removes until the txg 15833063Sperrin * commits, so a subsequent claim can spuriously fail with 15848227SNeil.Perrin@Sun.COM * EEXIST. So if we receive any error we try syncing out 15858227SNeil.Perrin@Sun.COM * any removes then retry the transaction. 15863063Sperrin */ 15878227SNeil.Perrin@Sun.COM if (pass == 1) 15883063Sperrin txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0); 1589789Sahrens } 1590789Sahrens 15917904SNeil.Perrin@Sun.COM bad: 15928227SNeil.Perrin@Sun.COM ASSERT(error); 15933063Sperrin name = kmem_alloc(MAXNAMELEN, KM_SLEEP); 15943063Sperrin dmu_objset_name(zr->zr_os, name); 15953063Sperrin cmn_err(CE_WARN, "ZFS replay transaction error %d, " 15965331Samw "dataset %s, seq 0x%llx, txtype %llu %s\n", 15975331Samw error, name, (u_longlong_t)lr->lrc_seq, (u_longlong_t)txtype, 15985331Samw (lr->lrc_txtype & TX_CI) ? "CI" : ""); 15998227SNeil.Perrin@Sun.COM zilog->zl_replay = B_FALSE; 16003063Sperrin kmem_free(name, MAXNAMELEN); 16013063Sperrin } 1602789Sahrens 16033063Sperrin /* ARGSUSED */ 16043063Sperrin static void 16053063Sperrin zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg) 16063063Sperrin { 16073063Sperrin zilog->zl_replay_blks++; 1608789Sahrens } 1609789Sahrens 1610789Sahrens /* 16111362Sperrin * If this dataset has a non-empty intent log, replay it and destroy it. 1612789Sahrens */ 1613789Sahrens void 16148227SNeil.Perrin@Sun.COM zil_replay(objset_t *os, void *arg, zil_replay_func_t *replay_func[TX_MAX_TYPE]) 1615789Sahrens { 1616789Sahrens zilog_t *zilog = dmu_objset_zil(os); 16171807Sbonwick const zil_header_t *zh = zilog->zl_header; 16181807Sbonwick zil_replay_arg_t zr; 16191362Sperrin 16208989SNeil.Perrin@Sun.COM if ((zh->zh_flags & ZIL_REPLAY_NEEDED) == 0) { 16211807Sbonwick zil_destroy(zilog, B_TRUE); 16221362Sperrin return; 16231362Sperrin } 1624789Sahrens 1625789Sahrens zr.zr_os = os; 1626789Sahrens zr.zr_replay = replay_func; 1627789Sahrens zr.zr_arg = arg; 16281807Sbonwick zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log); 1629789Sahrens zr.zr_lrbuf = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP); 1630789Sahrens 1631789Sahrens /* 1632789Sahrens * Wait for in-progress removes to sync before starting replay. 1633789Sahrens */ 1634789Sahrens txg_wait_synced(zilog->zl_dmu_pool, 0); 1635789Sahrens 16368227SNeil.Perrin@Sun.COM zilog->zl_replay = B_TRUE; 16373063Sperrin zilog->zl_replay_time = lbolt; 16383063Sperrin ASSERT(zilog->zl_replay_blks == 0); 16393063Sperrin (void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr, 16401807Sbonwick zh->zh_claim_txg); 1641789Sahrens kmem_free(zr.zr_lrbuf, 2 * SPA_MAXBLOCKSIZE); 1642789Sahrens 16431807Sbonwick zil_destroy(zilog, B_FALSE); 16445712Sahrens txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 16458227SNeil.Perrin@Sun.COM zilog->zl_replay = B_FALSE; 1646789Sahrens } 16471646Sperrin 16481646Sperrin /* 16491646Sperrin * Report whether all transactions are committed 16501646Sperrin */ 16511646Sperrin int 16521646Sperrin zil_is_committed(zilog_t *zilog) 16531646Sperrin { 16541646Sperrin lwb_t *lwb; 16552638Sperrin int ret; 16561646Sperrin 16572638Sperrin mutex_enter(&zilog->zl_lock); 16582638Sperrin while (zilog->zl_writer) 16592638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 16602638Sperrin 16612638Sperrin /* recent unpushed intent log transactions? */ 16622638Sperrin if (!list_is_empty(&zilog->zl_itx_list)) { 16632638Sperrin ret = B_FALSE; 16642638Sperrin goto out; 16652638Sperrin } 16662638Sperrin 16672638Sperrin /* intent log never used? */ 16682638Sperrin lwb = list_head(&zilog->zl_lwb_list); 16692638Sperrin if (lwb == NULL) { 16702638Sperrin ret = B_TRUE; 16712638Sperrin goto out; 16722638Sperrin } 16731646Sperrin 16741646Sperrin /* 16752638Sperrin * more than 1 log buffer means zil_sync() hasn't yet freed 16762638Sperrin * entries after a txg has committed 16771646Sperrin */ 16782638Sperrin if (list_next(&zilog->zl_lwb_list, lwb)) { 16792638Sperrin ret = B_FALSE; 16802638Sperrin goto out; 16812638Sperrin } 16822638Sperrin 16831646Sperrin ASSERT(zil_empty(zilog)); 16842638Sperrin ret = B_TRUE; 16852638Sperrin out: 16862638Sperrin cv_broadcast(&zilog->zl_cv_writer); 16872638Sperrin mutex_exit(&zilog->zl_lock); 16882638Sperrin return (ret); 16891646Sperrin } 16909701SGeorge.Wilson@Sun.COM 16919701SGeorge.Wilson@Sun.COM /* ARGSUSED */ 16929701SGeorge.Wilson@Sun.COM int 16939701SGeorge.Wilson@Sun.COM zil_vdev_offline(char *osname, void *arg) 16949701SGeorge.Wilson@Sun.COM { 16959701SGeorge.Wilson@Sun.COM objset_t *os; 16969701SGeorge.Wilson@Sun.COM zilog_t *zilog; 16979701SGeorge.Wilson@Sun.COM int error; 16989701SGeorge.Wilson@Sun.COM 169910298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(osname, FTAG, &os); 17009701SGeorge.Wilson@Sun.COM if (error) 17019701SGeorge.Wilson@Sun.COM return (error); 17029701SGeorge.Wilson@Sun.COM 17039701SGeorge.Wilson@Sun.COM zilog = dmu_objset_zil(os); 17049701SGeorge.Wilson@Sun.COM if (zil_suspend(zilog) != 0) 17059701SGeorge.Wilson@Sun.COM error = EEXIST; 17069701SGeorge.Wilson@Sun.COM else 17079701SGeorge.Wilson@Sun.COM zil_resume(zilog); 170810298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 17099701SGeorge.Wilson@Sun.COM return (error); 17109701SGeorge.Wilson@Sun.COM } 1711