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 /* 221362Sperrin * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27789Sahrens 28789Sahrens #include <sys/zfs_context.h> 29789Sahrens #include <sys/spa.h> 30789Sahrens #include <sys/dmu.h> 31789Sahrens #include <sys/zap.h> 32789Sahrens #include <sys/arc.h> 33789Sahrens #include <sys/stat.h> 34789Sahrens #include <sys/resource.h> 35789Sahrens #include <sys/zil.h> 36789Sahrens #include <sys/zil_impl.h> 37789Sahrens #include <sys/dsl_dataset.h> 38789Sahrens #include <sys/vdev.h> 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 80789Sahrens static int 81789Sahrens zil_dva_compare(const void *x1, const void *x2) 82789Sahrens { 83789Sahrens const dva_t *dva1 = x1; 84789Sahrens const dva_t *dva2 = x2; 85789Sahrens 86789Sahrens if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2)) 87789Sahrens return (-1); 88789Sahrens if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2)) 89789Sahrens return (1); 90789Sahrens 91789Sahrens if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2)) 92789Sahrens return (-1); 93789Sahrens if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2)) 94789Sahrens return (1); 95789Sahrens 96789Sahrens return (0); 97789Sahrens } 98789Sahrens 99789Sahrens static void 100789Sahrens zil_dva_tree_init(avl_tree_t *t) 101789Sahrens { 102789Sahrens avl_create(t, zil_dva_compare, sizeof (zil_dva_node_t), 103789Sahrens offsetof(zil_dva_node_t, zn_node)); 104789Sahrens } 105789Sahrens 106789Sahrens static void 107789Sahrens zil_dva_tree_fini(avl_tree_t *t) 108789Sahrens { 109789Sahrens zil_dva_node_t *zn; 110789Sahrens void *cookie = NULL; 111789Sahrens 112789Sahrens while ((zn = avl_destroy_nodes(t, &cookie)) != NULL) 113789Sahrens kmem_free(zn, sizeof (zil_dva_node_t)); 114789Sahrens 115789Sahrens avl_destroy(t); 116789Sahrens } 117789Sahrens 118789Sahrens static int 119789Sahrens zil_dva_tree_add(avl_tree_t *t, dva_t *dva) 120789Sahrens { 121789Sahrens zil_dva_node_t *zn; 122789Sahrens avl_index_t where; 123789Sahrens 124789Sahrens if (avl_find(t, dva, &where) != NULL) 125789Sahrens return (EEXIST); 126789Sahrens 127789Sahrens zn = kmem_alloc(sizeof (zil_dva_node_t), KM_SLEEP); 128789Sahrens zn->zn_dva = *dva; 129789Sahrens avl_insert(t, zn, where); 130789Sahrens 131789Sahrens return (0); 132789Sahrens } 133789Sahrens 1341807Sbonwick static zil_header_t * 1351807Sbonwick zil_header_in_syncing_context(zilog_t *zilog) 1361807Sbonwick { 1371807Sbonwick return ((zil_header_t *)zilog->zl_header); 1381807Sbonwick } 1391807Sbonwick 1401807Sbonwick static void 1411807Sbonwick zil_init_log_chain(zilog_t *zilog, blkptr_t *bp) 1421807Sbonwick { 1431807Sbonwick zio_cksum_t *zc = &bp->blk_cksum; 1441807Sbonwick 1451807Sbonwick zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL); 1461807Sbonwick zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL); 1471807Sbonwick zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os); 1481807Sbonwick zc->zc_word[ZIL_ZC_SEQ] = 1ULL; 1491807Sbonwick } 1501807Sbonwick 151789Sahrens /* 152789Sahrens * Read a log block, make sure it's valid, and byteswap it if necessary. 153789Sahrens */ 154789Sahrens static int 1551807Sbonwick zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, arc_buf_t **abufpp) 156789Sahrens { 1571807Sbonwick blkptr_t blk = *bp; 1581544Seschrock zbookmark_t zb; 1592391Smaybee uint32_t aflags = ARC_WAIT; 160789Sahrens int error; 161789Sahrens 1621807Sbonwick zb.zb_objset = bp->blk_cksum.zc_word[ZIL_ZC_OBJSET]; 1631544Seschrock zb.zb_object = 0; 1641544Seschrock zb.zb_level = -1; 1651807Sbonwick zb.zb_blkid = bp->blk_cksum.zc_word[ZIL_ZC_SEQ]; 1661807Sbonwick 1671807Sbonwick *abufpp = NULL; 1681807Sbonwick 1691807Sbonwick error = arc_read(NULL, zilog->zl_spa, &blk, byteswap_uint64_array, 1701807Sbonwick arc_getbuf_func, abufpp, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL | 1712391Smaybee ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB, &aflags, &zb); 1721807Sbonwick 1731807Sbonwick if (error == 0) { 1741807Sbonwick char *data = (*abufpp)->b_data; 1751807Sbonwick uint64_t blksz = BP_GET_LSIZE(bp); 1761807Sbonwick zil_trailer_t *ztp = (zil_trailer_t *)(data + blksz) - 1; 1771807Sbonwick zio_cksum_t cksum = bp->blk_cksum; 1781544Seschrock 1791807Sbonwick /* 1801807Sbonwick * Sequence numbers should be... sequential. The checksum 1811807Sbonwick * verifier for the next block should be bp's checksum plus 1. 1821807Sbonwick */ 1831807Sbonwick cksum.zc_word[ZIL_ZC_SEQ]++; 1841807Sbonwick 1851807Sbonwick if (bcmp(&cksum, &ztp->zit_next_blk.blk_cksum, sizeof (cksum))) 1861807Sbonwick error = ESTALE; 1871807Sbonwick else if (BP_IS_HOLE(&ztp->zit_next_blk)) 1881807Sbonwick error = ENOENT; 1891807Sbonwick else if (ztp->zit_nused > (blksz - sizeof (zil_trailer_t))) 1901807Sbonwick error = EOVERFLOW; 1911807Sbonwick 1921807Sbonwick if (error) { 1931807Sbonwick VERIFY(arc_buf_remove_ref(*abufpp, abufpp) == 1); 1941807Sbonwick *abufpp = NULL; 1951807Sbonwick } 196789Sahrens } 197789Sahrens 1981807Sbonwick dprintf("error %d on %llu:%llu\n", error, zb.zb_objset, zb.zb_blkid); 199789Sahrens 2001807Sbonwick return (error); 201789Sahrens } 202789Sahrens 203789Sahrens /* 204789Sahrens * Parse the intent log, and call parse_func for each valid record within. 2051807Sbonwick * Return the highest sequence number. 206789Sahrens */ 2071807Sbonwick uint64_t 208789Sahrens zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func, 209789Sahrens zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg) 210789Sahrens { 2111807Sbonwick const zil_header_t *zh = zilog->zl_header; 2121807Sbonwick uint64_t claim_seq = zh->zh_claim_seq; 2131807Sbonwick uint64_t seq = 0; 2141807Sbonwick uint64_t max_seq = 0; 2151807Sbonwick blkptr_t blk = zh->zh_log; 2161807Sbonwick arc_buf_t *abuf; 217789Sahrens char *lrbuf, *lrp; 218789Sahrens zil_trailer_t *ztp; 219789Sahrens int reclen, error; 220789Sahrens 221789Sahrens if (BP_IS_HOLE(&blk)) 2221807Sbonwick return (max_seq); 223789Sahrens 224789Sahrens /* 225789Sahrens * Starting at the block pointed to by zh_log we read the log chain. 226789Sahrens * For each block in the chain we strongly check that block to 227789Sahrens * ensure its validity. We stop when an invalid block is found. 228789Sahrens * For each block pointer in the chain we call parse_blk_func(). 229789Sahrens * For each record in each valid block we call parse_lr_func(). 2301807Sbonwick * If the log has been claimed, stop if we encounter a sequence 2311807Sbonwick * number greater than the highest claimed sequence number. 232789Sahrens */ 233789Sahrens zil_dva_tree_init(&zilog->zl_dva_tree); 234789Sahrens for (;;) { 2351807Sbonwick seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 2361807Sbonwick 2371807Sbonwick if (claim_seq != 0 && seq > claim_seq) 2381807Sbonwick break; 2391807Sbonwick 2401807Sbonwick ASSERT(max_seq < seq); 2411807Sbonwick max_seq = seq; 2421807Sbonwick 2431807Sbonwick error = zil_read_log_block(zilog, &blk, &abuf); 244789Sahrens 245789Sahrens if (parse_blk_func != NULL) 246789Sahrens parse_blk_func(zilog, &blk, arg, txg); 247789Sahrens 248789Sahrens if (error) 249789Sahrens break; 250789Sahrens 2511807Sbonwick lrbuf = abuf->b_data; 252789Sahrens ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1; 253789Sahrens blk = ztp->zit_next_blk; 254789Sahrens 2551807Sbonwick if (parse_lr_func == NULL) { 2561807Sbonwick VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 257789Sahrens continue; 2581807Sbonwick } 259789Sahrens 260789Sahrens for (lrp = lrbuf; lrp < lrbuf + ztp->zit_nused; lrp += reclen) { 261789Sahrens lr_t *lr = (lr_t *)lrp; 262789Sahrens reclen = lr->lrc_reclen; 263789Sahrens ASSERT3U(reclen, >=, sizeof (lr_t)); 264789Sahrens parse_lr_func(zilog, lr, arg, txg); 265789Sahrens } 2661807Sbonwick VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 267789Sahrens } 268789Sahrens zil_dva_tree_fini(&zilog->zl_dva_tree); 2691807Sbonwick 2701807Sbonwick return (max_seq); 271789Sahrens } 272789Sahrens 273789Sahrens /* ARGSUSED */ 274789Sahrens static void 275789Sahrens zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg) 276789Sahrens { 277789Sahrens spa_t *spa = zilog->zl_spa; 278789Sahrens int err; 279789Sahrens 280789Sahrens /* 281789Sahrens * Claim log block if not already committed and not already claimed. 282789Sahrens */ 283789Sahrens if (bp->blk_birth >= first_txg && 284789Sahrens zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp)) == 0) { 285789Sahrens err = zio_wait(zio_claim(NULL, spa, first_txg, bp, NULL, NULL)); 286789Sahrens ASSERT(err == 0); 287789Sahrens } 288789Sahrens } 289789Sahrens 290789Sahrens static void 291789Sahrens zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg) 292789Sahrens { 293789Sahrens if (lrc->lrc_txtype == TX_WRITE) { 294789Sahrens lr_write_t *lr = (lr_write_t *)lrc; 295789Sahrens zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg); 296789Sahrens } 297789Sahrens } 298789Sahrens 299789Sahrens /* ARGSUSED */ 300789Sahrens static void 301789Sahrens zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg) 302789Sahrens { 303789Sahrens zio_free_blk(zilog->zl_spa, bp, dmu_tx_get_txg(tx)); 304789Sahrens } 305789Sahrens 306789Sahrens static void 307789Sahrens zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg) 308789Sahrens { 309789Sahrens /* 310789Sahrens * If we previously claimed it, we need to free it. 311789Sahrens */ 312789Sahrens if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE) { 313789Sahrens lr_write_t *lr = (lr_write_t *)lrc; 314789Sahrens blkptr_t *bp = &lr->lr_blkptr; 315789Sahrens if (bp->blk_birth >= claim_txg && 316789Sahrens !zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp))) { 317789Sahrens (void) arc_free(NULL, zilog->zl_spa, 318789Sahrens dmu_tx_get_txg(tx), bp, NULL, NULL, ARC_WAIT); 319789Sahrens } 320789Sahrens } 321789Sahrens } 322789Sahrens 323789Sahrens /* 324789Sahrens * Create an on-disk intent log. 325789Sahrens */ 326789Sahrens static void 327789Sahrens zil_create(zilog_t *zilog) 328789Sahrens { 3291807Sbonwick const zil_header_t *zh = zilog->zl_header; 330789Sahrens lwb_t *lwb; 3311807Sbonwick uint64_t txg = 0; 3321807Sbonwick dmu_tx_t *tx = NULL; 333789Sahrens blkptr_t blk; 3341807Sbonwick int error = 0; 335789Sahrens 336789Sahrens /* 3371807Sbonwick * Wait for any previous destroy to complete. 338789Sahrens */ 3391807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 3401807Sbonwick 3411807Sbonwick ASSERT(zh->zh_claim_txg == 0); 3421807Sbonwick ASSERT(zh->zh_replay_seq == 0); 3431807Sbonwick 3441807Sbonwick blk = zh->zh_log; 345789Sahrens 346789Sahrens /* 3471807Sbonwick * If we don't already have an initial log block, allocate one now. 348789Sahrens */ 3491807Sbonwick if (BP_IS_HOLE(&blk)) { 3501807Sbonwick tx = dmu_tx_create(zilog->zl_os); 3511807Sbonwick (void) dmu_tx_assign(tx, TXG_WAIT); 3521807Sbonwick dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 3531807Sbonwick txg = dmu_tx_get_txg(tx); 3541807Sbonwick 355*3063Sperrin error = zio_alloc_blk(zilog->zl_spa, ZIL_MIN_BLKSZ, &blk, 356*3063Sperrin NULL, txg); 3571807Sbonwick 3581807Sbonwick if (error == 0) 3591807Sbonwick zil_init_log_chain(zilog, &blk); 3601362Sperrin } 3611807Sbonwick 3621807Sbonwick /* 3631807Sbonwick * Allocate a log write buffer (lwb) for the first log block. 3641807Sbonwick */ 365789Sahrens if (error == 0) { 366789Sahrens lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 367789Sahrens lwb->lwb_zilog = zilog; 368789Sahrens lwb->lwb_blk = blk; 369789Sahrens lwb->lwb_nused = 0; 370789Sahrens lwb->lwb_sz = BP_GET_LSIZE(&lwb->lwb_blk); 371789Sahrens lwb->lwb_buf = zio_buf_alloc(lwb->lwb_sz); 372789Sahrens lwb->lwb_max_txg = txg; 3732237Smaybee lwb->lwb_zio = NULL; 3742237Smaybee 375789Sahrens mutex_enter(&zilog->zl_lock); 376789Sahrens list_insert_tail(&zilog->zl_lwb_list, lwb); 377789Sahrens mutex_exit(&zilog->zl_lock); 378789Sahrens } 379789Sahrens 3801807Sbonwick /* 3811807Sbonwick * If we just allocated the first log block, commit our transaction 3821807Sbonwick * and wait for zil_sync() to stuff the block poiner into zh_log. 3831807Sbonwick * (zh is part of the MOS, so we cannot modify it in open context.) 3841807Sbonwick */ 3851807Sbonwick if (tx != NULL) { 3861807Sbonwick dmu_tx_commit(tx); 3871362Sperrin txg_wait_synced(zilog->zl_dmu_pool, txg); 3881807Sbonwick } 3891807Sbonwick 3901807Sbonwick ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0); 391789Sahrens } 392789Sahrens 393789Sahrens /* 394789Sahrens * In one tx, free all log blocks and clear the log header. 3951807Sbonwick * If keep_first is set, then we're replaying a log with no content. 3961807Sbonwick * We want to keep the first block, however, so that the first 3971807Sbonwick * synchronous transaction doesn't require a txg_wait_synced() 3981807Sbonwick * in zil_create(). We don't need to txg_wait_synced() here either 3991807Sbonwick * when keep_first is set, because both zil_create() and zil_destroy() 4001807Sbonwick * will wait for any in-progress destroys to complete. 401789Sahrens */ 402789Sahrens void 4031807Sbonwick zil_destroy(zilog_t *zilog, boolean_t keep_first) 404789Sahrens { 4051807Sbonwick const zil_header_t *zh = zilog->zl_header; 4061807Sbonwick lwb_t *lwb; 407789Sahrens dmu_tx_t *tx; 408789Sahrens uint64_t txg; 409789Sahrens 4101807Sbonwick /* 4111807Sbonwick * Wait for any previous destroy to complete. 4121807Sbonwick */ 4131807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 414789Sahrens 4151807Sbonwick if (BP_IS_HOLE(&zh->zh_log)) 416789Sahrens return; 417789Sahrens 418789Sahrens tx = dmu_tx_create(zilog->zl_os); 419789Sahrens (void) dmu_tx_assign(tx, TXG_WAIT); 420789Sahrens dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 421789Sahrens txg = dmu_tx_get_txg(tx); 422789Sahrens 4231807Sbonwick mutex_enter(&zilog->zl_lock); 4241807Sbonwick 4251807Sbonwick ASSERT3U(zilog->zl_destroy_txg, <, txg); 426789Sahrens zilog->zl_destroy_txg = txg; 4271807Sbonwick zilog->zl_keep_first = keep_first; 4281807Sbonwick 4291807Sbonwick if (!list_is_empty(&zilog->zl_lwb_list)) { 4301807Sbonwick ASSERT(zh->zh_claim_txg == 0); 4311807Sbonwick ASSERT(!keep_first); 4321807Sbonwick while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 4331807Sbonwick list_remove(&zilog->zl_lwb_list, lwb); 4341807Sbonwick if (lwb->lwb_buf != NULL) 4351807Sbonwick zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 4361807Sbonwick zio_free_blk(zilog->zl_spa, &lwb->lwb_blk, txg); 4371807Sbonwick kmem_cache_free(zil_lwb_cache, lwb); 4381807Sbonwick } 4391807Sbonwick } else { 4401807Sbonwick if (!keep_first) { 4411807Sbonwick (void) zil_parse(zilog, zil_free_log_block, 4421807Sbonwick zil_free_log_record, tx, zh->zh_claim_txg); 4431807Sbonwick } 4441807Sbonwick } 4452638Sperrin mutex_exit(&zilog->zl_lock); 446789Sahrens 447789Sahrens dmu_tx_commit(tx); 448789Sahrens 4491807Sbonwick if (keep_first) /* no need to wait in this case */ 4501807Sbonwick return; 4511807Sbonwick 4521807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, txg); 4531807Sbonwick ASSERT(BP_IS_HOLE(&zh->zh_log)); 454789Sahrens } 455789Sahrens 4562199Sahrens int 457789Sahrens zil_claim(char *osname, void *txarg) 458789Sahrens { 459789Sahrens dmu_tx_t *tx = txarg; 460789Sahrens uint64_t first_txg = dmu_tx_get_txg(tx); 461789Sahrens zilog_t *zilog; 462789Sahrens zil_header_t *zh; 463789Sahrens objset_t *os; 464789Sahrens int error; 465789Sahrens 466789Sahrens error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_STANDARD, &os); 467789Sahrens if (error) { 468789Sahrens cmn_err(CE_WARN, "can't process intent log for %s", osname); 4692199Sahrens return (0); 470789Sahrens } 471789Sahrens 472789Sahrens zilog = dmu_objset_zil(os); 4731807Sbonwick zh = zil_header_in_syncing_context(zilog); 474789Sahrens 475789Sahrens /* 4761807Sbonwick * Claim all log blocks if we haven't already done so, and remember 4771807Sbonwick * the highest claimed sequence number. This ensures that if we can 4781807Sbonwick * read only part of the log now (e.g. due to a missing device), 4791807Sbonwick * but we can read the entire log later, we will not try to replay 4801807Sbonwick * or destroy beyond the last block we successfully claimed. 481789Sahrens */ 482789Sahrens ASSERT3U(zh->zh_claim_txg, <=, first_txg); 483789Sahrens if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) { 484789Sahrens zh->zh_claim_txg = first_txg; 4851807Sbonwick zh->zh_claim_seq = zil_parse(zilog, zil_claim_log_block, 4861807Sbonwick zil_claim_log_record, tx, first_txg); 487789Sahrens dsl_dataset_dirty(dmu_objset_ds(os), tx); 488789Sahrens } 4891807Sbonwick 490789Sahrens ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1)); 491789Sahrens dmu_objset_close(os); 4922199Sahrens return (0); 493789Sahrens } 494789Sahrens 495789Sahrens void 4962638Sperrin zil_add_vdev(zilog_t *zilog, uint64_t vdev) 497789Sahrens { 498*3063Sperrin zil_vdev_t *zv, *new; 499*3063Sperrin uint64_t bmap_sz = sizeof (zilog->zl_vdev_bmap) << 3; 500*3063Sperrin uchar_t *cp; 501789Sahrens 5022986Sek110237 if (zfs_nocacheflush) 503789Sahrens return; 504789Sahrens 505*3063Sperrin if (vdev < bmap_sz) { 506*3063Sperrin cp = zilog->zl_vdev_bmap + (vdev / 8); 507*3063Sperrin atomic_or_8(cp, 1 << (vdev % 8)); 508*3063Sperrin } else { 509*3063Sperrin /* 510*3063Sperrin * insert into ordered list 511*3063Sperrin */ 512*3063Sperrin mutex_enter(&zilog->zl_lock); 513*3063Sperrin for (zv = list_head(&zilog->zl_vdev_list); zv != NULL; 514*3063Sperrin zv = list_next(&zilog->zl_vdev_list, zv)) { 515*3063Sperrin if (zv->vdev == vdev) { 516*3063Sperrin /* duplicate found - just return */ 517*3063Sperrin mutex_exit(&zilog->zl_lock); 518*3063Sperrin return; 519*3063Sperrin } 520*3063Sperrin if (zv->vdev > vdev) { 521*3063Sperrin /* insert before this entry */ 522*3063Sperrin new = kmem_alloc(sizeof (zil_vdev_t), 523*3063Sperrin KM_SLEEP); 524*3063Sperrin new->vdev = vdev; 525*3063Sperrin list_insert_before(&zilog->zl_vdev_list, 526*3063Sperrin zv, new); 527*3063Sperrin mutex_exit(&zilog->zl_lock); 528*3063Sperrin return; 529*3063Sperrin } 530*3063Sperrin } 531*3063Sperrin /* ran off end of list, insert at the end */ 532*3063Sperrin ASSERT(zv == NULL); 533*3063Sperrin new = kmem_alloc(sizeof (zil_vdev_t), KM_SLEEP); 534*3063Sperrin new->vdev = vdev; 535*3063Sperrin list_insert_tail(&zilog->zl_vdev_list, new); 536*3063Sperrin mutex_exit(&zilog->zl_lock); 537*3063Sperrin } 538*3063Sperrin } 539*3063Sperrin 540*3063Sperrin /* start an async flush of the write cache for this vdev */ 541*3063Sperrin void 542*3063Sperrin zil_flush_vdev(spa_t *spa, uint64_t vdev, zio_t **zio) 543*3063Sperrin { 544*3063Sperrin vdev_t *vd; 545*3063Sperrin 546*3063Sperrin if (*zio == NULL) 547*3063Sperrin *zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); 548*3063Sperrin 549*3063Sperrin vd = vdev_lookup_top(spa, vdev); 550*3063Sperrin ASSERT(vd); 551*3063Sperrin 552*3063Sperrin (void) zio_nowait(zio_ioctl(*zio, spa, vd, DKIOCFLUSHWRITECACHE, 553*3063Sperrin NULL, NULL, ZIO_PRIORITY_NOW, 554*3063Sperrin ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY)); 555789Sahrens } 556789Sahrens 557789Sahrens void 5582638Sperrin zil_flush_vdevs(zilog_t *zilog) 559789Sahrens { 560*3063Sperrin zil_vdev_t *zv; 561*3063Sperrin zio_t *zio = NULL; 562*3063Sperrin spa_t *spa = zilog->zl_spa; 563789Sahrens uint64_t vdev; 564*3063Sperrin uint8_t b; 565*3063Sperrin int i, j; 566*3063Sperrin 567*3063Sperrin ASSERT(zilog->zl_writer); 568789Sahrens 569*3063Sperrin for (i = 0; i < sizeof (zilog->zl_vdev_bmap); i++) { 570*3063Sperrin b = zilog->zl_vdev_bmap[i]; 571*3063Sperrin if (b == 0) 572*3063Sperrin continue; 573*3063Sperrin for (j = 0; j < 8; j++) { 574*3063Sperrin if (b & (1 << j)) { 575*3063Sperrin vdev = (i << 3) + j; 576*3063Sperrin zil_flush_vdev(spa, vdev, &zio); 577*3063Sperrin } 578*3063Sperrin } 579*3063Sperrin zilog->zl_vdev_bmap[i] = 0; 580*3063Sperrin } 581789Sahrens 5822638Sperrin while ((zv = list_head(&zilog->zl_vdev_list)) != NULL) { 583*3063Sperrin zil_flush_vdev(spa, zv->vdev, &zio); 584789Sahrens list_remove(&zilog->zl_vdev_list, zv); 585789Sahrens kmem_free(zv, sizeof (zil_vdev_t)); 586789Sahrens } 587789Sahrens /* 588789Sahrens * Wait for all the flushes to complete. Not all devices actually 589789Sahrens * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails. 590789Sahrens */ 591*3063Sperrin if (zio) 592789Sahrens (void) zio_wait(zio); 593789Sahrens } 594789Sahrens 595789Sahrens /* 596789Sahrens * Function called when a log block write completes 597789Sahrens */ 598789Sahrens static void 599789Sahrens zil_lwb_write_done(zio_t *zio) 600789Sahrens { 601789Sahrens lwb_t *lwb = zio->io_private; 602789Sahrens zilog_t *zilog = lwb->lwb_zilog; 603789Sahrens 604789Sahrens /* 605789Sahrens * Now that we've written this log block, we have a stable pointer 606789Sahrens * to the next block in the chain, so it's OK to let the txg in 607789Sahrens * which we allocated the next block sync. 608789Sahrens */ 609789Sahrens txg_rele_to_sync(&lwb->lwb_txgh); 610789Sahrens 611789Sahrens zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 612789Sahrens mutex_enter(&zilog->zl_lock); 613789Sahrens lwb->lwb_buf = NULL; 614789Sahrens if (zio->io_error) { 615789Sahrens zilog->zl_log_error = B_TRUE; 616789Sahrens mutex_exit(&zilog->zl_lock); 617789Sahrens return; 618789Sahrens } 619789Sahrens mutex_exit(&zilog->zl_lock); 620789Sahrens } 621789Sahrens 622789Sahrens /* 6232237Smaybee * Initialize the io for a log block. 6242237Smaybee * 6252237Smaybee * Note, we should not initialize the IO until we are about 6262237Smaybee * to use it, since zio_rewrite() does a spa_config_enter(). 6272237Smaybee */ 6282237Smaybee static void 6292237Smaybee zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb) 6302237Smaybee { 6312237Smaybee zbookmark_t zb; 6322237Smaybee 6332237Smaybee zb.zb_objset = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET]; 6342237Smaybee zb.zb_object = 0; 6352237Smaybee zb.zb_level = -1; 6362237Smaybee zb.zb_blkid = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 6372237Smaybee 6382638Sperrin if (zilog->zl_root_zio == NULL) { 6392638Sperrin zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL, 6402638Sperrin ZIO_FLAG_CANFAIL); 6412638Sperrin } 642*3063Sperrin if (lwb->lwb_zio == NULL) { 643*3063Sperrin lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa, 644*3063Sperrin ZIO_CHECKSUM_ZILOG, 0, &lwb->lwb_blk, lwb->lwb_buf, 645*3063Sperrin lwb->lwb_sz, zil_lwb_write_done, lwb, 646*3063Sperrin ZIO_PRIORITY_LOG_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb); 647*3063Sperrin } 6482237Smaybee } 6492237Smaybee 6502237Smaybee /* 651789Sahrens * Start a log block write and advance to the next log block. 652789Sahrens * Calls are serialized. 653789Sahrens */ 654789Sahrens static lwb_t * 655789Sahrens zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb) 656789Sahrens { 657789Sahrens lwb_t *nlwb; 658789Sahrens zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1; 6591807Sbonwick spa_t *spa = zilog->zl_spa; 6601807Sbonwick blkptr_t *bp = &ztp->zit_next_blk; 661789Sahrens uint64_t txg; 662789Sahrens uint64_t zil_blksz; 663789Sahrens int error; 664789Sahrens 665789Sahrens ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb)); 666789Sahrens 667789Sahrens /* 668789Sahrens * Allocate the next block and save its address in this block 669789Sahrens * before writing it in order to establish the log chain. 670789Sahrens * Note that if the allocation of nlwb synced before we wrote 671789Sahrens * the block that points at it (lwb), we'd leak it if we crashed. 672789Sahrens * Therefore, we don't do txg_rele_to_sync() until zil_lwb_write_done(). 673789Sahrens */ 674789Sahrens txg = txg_hold_open(zilog->zl_dmu_pool, &lwb->lwb_txgh); 675789Sahrens txg_rele_to_quiesce(&lwb->lwb_txgh); 676789Sahrens 677789Sahrens /* 6781141Sperrin * Pick a ZIL blocksize. We request a size that is the 6791141Sperrin * maximum of the previous used size, the current used size and 6801141Sperrin * the amount waiting in the queue. 681789Sahrens */ 6822237Smaybee zil_blksz = MAX(zilog->zl_prev_used, 6832237Smaybee zilog->zl_cur_used + sizeof (*ztp)); 6841141Sperrin zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp)); 6851842Sperrin zil_blksz = P2ROUNDUP_TYPED(zil_blksz, ZIL_MIN_BLKSZ, uint64_t); 6861141Sperrin if (zil_blksz > ZIL_MAX_BLKSZ) 6871141Sperrin zil_blksz = ZIL_MAX_BLKSZ; 688789Sahrens 689*3063Sperrin BP_ZERO(bp); 690*3063Sperrin /* pass the old blkptr in order to spread log blocks across devs */ 691*3063Sperrin error = zio_alloc_blk(spa, zil_blksz, bp, &lwb->lwb_blk, txg); 692789Sahrens if (error) { 6931544Seschrock /* 6941544Seschrock * Reinitialise the lwb. 6951544Seschrock * By returning NULL the caller will call tx_wait_synced() 6961544Seschrock */ 6971544Seschrock mutex_enter(&zilog->zl_lock); 6981544Seschrock lwb->lwb_nused = 0; 6991544Seschrock mutex_exit(&zilog->zl_lock); 700789Sahrens txg_rele_to_sync(&lwb->lwb_txgh); 701789Sahrens return (NULL); 702789Sahrens } 703789Sahrens 7041807Sbonwick ASSERT3U(bp->blk_birth, ==, txg); 7051544Seschrock ztp->zit_pad = 0; 706789Sahrens ztp->zit_nused = lwb->lwb_nused; 707789Sahrens ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum; 7081807Sbonwick bp->blk_cksum = lwb->lwb_blk.blk_cksum; 7091807Sbonwick bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++; 710789Sahrens 711789Sahrens /* 712789Sahrens * Allocate a new log write buffer (lwb). 713789Sahrens */ 714789Sahrens nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 715789Sahrens 716789Sahrens nlwb->lwb_zilog = zilog; 7171807Sbonwick nlwb->lwb_blk = *bp; 718789Sahrens nlwb->lwb_nused = 0; 719789Sahrens nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk); 720789Sahrens nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz); 721789Sahrens nlwb->lwb_max_txg = txg; 7222237Smaybee nlwb->lwb_zio = NULL; 723789Sahrens 724789Sahrens /* 725*3063Sperrin * Put new lwb at the end of the log chain 726789Sahrens */ 727789Sahrens mutex_enter(&zilog->zl_lock); 728789Sahrens list_insert_tail(&zilog->zl_lwb_list, nlwb); 729*3063Sperrin mutex_exit(&zilog->zl_lock); 730*3063Sperrin 731*3063Sperrin /* Record the vdev for later flushing */ 7322638Sperrin zil_add_vdev(zilog, DVA_GET_VDEV(BP_IDENTITY(&(lwb->lwb_blk)))); 733789Sahrens 734789Sahrens /* 7352237Smaybee * kick off the write for the old log block 736789Sahrens */ 7372237Smaybee dprintf_bp(&lwb->lwb_blk, "lwb %p txg %llu: ", lwb, txg); 738*3063Sperrin ASSERT(lwb->lwb_zio); 7392237Smaybee zio_nowait(lwb->lwb_zio); 740789Sahrens 741789Sahrens return (nlwb); 742789Sahrens } 743789Sahrens 744789Sahrens static lwb_t * 745789Sahrens zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb) 746789Sahrens { 747789Sahrens lr_t *lrc = &itx->itx_lr; /* common log record */ 7482237Smaybee lr_write_t *lr = (lr_write_t *)lrc; 749789Sahrens uint64_t txg = lrc->lrc_txg; 750789Sahrens uint64_t reclen = lrc->lrc_reclen; 7512237Smaybee uint64_t dlen; 752789Sahrens 753789Sahrens if (lwb == NULL) 754789Sahrens return (NULL); 755789Sahrens ASSERT(lwb->lwb_buf != NULL); 756789Sahrens 7572237Smaybee if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY) 7582237Smaybee dlen = P2ROUNDUP_TYPED( 7592237Smaybee lr->lr_length, sizeof (uint64_t), uint64_t); 7602237Smaybee else 7612237Smaybee dlen = 0; 7621669Sperrin 7631669Sperrin zilog->zl_cur_used += (reclen + dlen); 7641669Sperrin 765*3063Sperrin zil_lwb_write_init(zilog, lwb); 766*3063Sperrin 7671669Sperrin /* 7681669Sperrin * If this record won't fit in the current log block, start a new one. 7691669Sperrin */ 7701669Sperrin if (lwb->lwb_nused + reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) { 7711669Sperrin lwb = zil_lwb_write_start(zilog, lwb); 7722237Smaybee if (lwb == NULL) 7731669Sperrin return (NULL); 774*3063Sperrin zil_lwb_write_init(zilog, lwb); 7751669Sperrin ASSERT(lwb->lwb_nused == 0); 7761669Sperrin if (reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) { 7771669Sperrin txg_wait_synced(zilog->zl_dmu_pool, txg); 778789Sahrens return (lwb); 779789Sahrens } 780789Sahrens } 781789Sahrens 7822638Sperrin /* 7832638Sperrin * Update the lrc_seq, to be log record sequence number. See zil.h 7842638Sperrin * Then copy the record to the log buffer. 7852638Sperrin */ 7862638Sperrin lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */ 787789Sahrens bcopy(lrc, lwb->lwb_buf + lwb->lwb_nused, reclen); 7882237Smaybee 7892237Smaybee /* 7902237Smaybee * If it's a write, fetch the data or get its blkptr as appropriate. 7912237Smaybee */ 7922237Smaybee if (lrc->lrc_txtype == TX_WRITE) { 7932237Smaybee if (txg > spa_freeze_txg(zilog->zl_spa)) 7942237Smaybee txg_wait_synced(zilog->zl_dmu_pool, txg); 7952237Smaybee if (itx->itx_wr_state != WR_COPIED) { 7962237Smaybee char *dbuf; 7972237Smaybee int error; 7982237Smaybee 7992237Smaybee /* alignment is guaranteed */ 8002237Smaybee lr = (lr_write_t *)(lwb->lwb_buf + lwb->lwb_nused); 8012237Smaybee if (dlen) { 8022237Smaybee ASSERT(itx->itx_wr_state == WR_NEED_COPY); 8032237Smaybee dbuf = lwb->lwb_buf + lwb->lwb_nused + reclen; 8042237Smaybee lr->lr_common.lrc_reclen += dlen; 8052237Smaybee } else { 8062237Smaybee ASSERT(itx->itx_wr_state == WR_INDIRECT); 8072237Smaybee dbuf = NULL; 8082237Smaybee } 8092237Smaybee error = zilog->zl_get_data( 8102237Smaybee itx->itx_private, lr, dbuf, lwb->lwb_zio); 8112237Smaybee if (error) { 8122237Smaybee ASSERT(error == ENOENT || error == EEXIST || 8132237Smaybee error == EALREADY); 8142237Smaybee return (lwb); 8152237Smaybee } 8162237Smaybee } 8171669Sperrin } 8182237Smaybee 8192237Smaybee lwb->lwb_nused += reclen + dlen; 820789Sahrens lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg); 821789Sahrens ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb)); 822789Sahrens ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0); 823789Sahrens 824789Sahrens return (lwb); 825789Sahrens } 826789Sahrens 827789Sahrens itx_t * 828789Sahrens zil_itx_create(int txtype, size_t lrsize) 829789Sahrens { 830789Sahrens itx_t *itx; 831789Sahrens 8321842Sperrin lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t); 833789Sahrens 834789Sahrens itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP); 835789Sahrens itx->itx_lr.lrc_txtype = txtype; 836789Sahrens itx->itx_lr.lrc_reclen = lrsize; 837789Sahrens itx->itx_lr.lrc_seq = 0; /* defensive */ 838789Sahrens 839789Sahrens return (itx); 840789Sahrens } 841789Sahrens 842789Sahrens uint64_t 843789Sahrens zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx) 844789Sahrens { 845789Sahrens uint64_t seq; 846789Sahrens 847789Sahrens ASSERT(itx->itx_lr.lrc_seq == 0); 848789Sahrens 849789Sahrens mutex_enter(&zilog->zl_lock); 850789Sahrens list_insert_tail(&zilog->zl_itx_list, itx); 851789Sahrens zilog->zl_itx_list_sz += itx->itx_lr.lrc_reclen; 852789Sahrens itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx); 853789Sahrens itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq; 854789Sahrens mutex_exit(&zilog->zl_lock); 855789Sahrens 856789Sahrens return (seq); 857789Sahrens } 858789Sahrens 859789Sahrens /* 860789Sahrens * Free up all in-memory intent log transactions that have now been synced. 861789Sahrens */ 862789Sahrens static void 863789Sahrens zil_itx_clean(zilog_t *zilog) 864789Sahrens { 865789Sahrens uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa); 866789Sahrens uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa); 867789Sahrens itx_t *itx; 868789Sahrens 869789Sahrens mutex_enter(&zilog->zl_lock); 8702638Sperrin /* wait for a log writer to finish walking list */ 8712638Sperrin while (zilog->zl_writer) { 8722638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 8732638Sperrin } 8742638Sperrin /* no need to set zl_writer as we never drop zl_lock */ 875789Sahrens while ((itx = list_head(&zilog->zl_itx_list)) != NULL && 876789Sahrens itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) { 877789Sahrens list_remove(&zilog->zl_itx_list, itx); 878789Sahrens zilog->zl_itx_list_sz -= itx->itx_lr.lrc_reclen; 879789Sahrens kmem_free(itx, offsetof(itx_t, itx_lr) 880789Sahrens + itx->itx_lr.lrc_reclen); 881789Sahrens } 882*3063Sperrin cv_broadcast(&zilog->zl_cv_writer); 883789Sahrens mutex_exit(&zilog->zl_lock); 884789Sahrens } 885789Sahrens 8862638Sperrin /* 887*3063Sperrin * If there are any in-memory intent log transactions which have now been 888*3063Sperrin * synced then start up a taskq to free them. 8892638Sperrin */ 890789Sahrens void 891789Sahrens zil_clean(zilog_t *zilog) 892789Sahrens { 893*3063Sperrin itx_t *itx; 894*3063Sperrin 895789Sahrens mutex_enter(&zilog->zl_lock); 896*3063Sperrin itx = list_head(&zilog->zl_itx_list); 897*3063Sperrin if ((itx != NULL) && 898*3063Sperrin (itx->itx_lr.lrc_txg <= spa_last_synced_txg(zilog->zl_spa))) { 899789Sahrens (void) taskq_dispatch(zilog->zl_clean_taskq, 900789Sahrens (void (*)(void *))zil_itx_clean, zilog, TQ_NOSLEEP); 901*3063Sperrin } 902789Sahrens mutex_exit(&zilog->zl_lock); 903789Sahrens } 904789Sahrens 905789Sahrens void 9062638Sperrin zil_commit_writer(zilog_t *zilog, uint64_t seq, uint64_t foid) 907789Sahrens { 908789Sahrens uint64_t txg; 909789Sahrens uint64_t reclen; 910*3063Sperrin uint64_t commit_seq = 0; 9112638Sperrin itx_t *itx, *itx_next = (itx_t *)-1; 912789Sahrens lwb_t *lwb; 913789Sahrens spa_t *spa; 914789Sahrens 9152638Sperrin zilog->zl_writer = B_TRUE; 9162638Sperrin zilog->zl_root_zio = NULL; 917789Sahrens spa = zilog->zl_spa; 918789Sahrens 919789Sahrens if (zilog->zl_suspend) { 920789Sahrens lwb = NULL; 921789Sahrens } else { 922789Sahrens lwb = list_tail(&zilog->zl_lwb_list); 923789Sahrens if (lwb == NULL) { 9242638Sperrin /* 9252638Sperrin * Return if there's nothing to flush before we 9262638Sperrin * dirty the fs by calling zil_create() 9272638Sperrin */ 9282638Sperrin if (list_is_empty(&zilog->zl_itx_list)) { 9292638Sperrin zilog->zl_writer = B_FALSE; 9302638Sperrin return; 9312638Sperrin } 932789Sahrens mutex_exit(&zilog->zl_lock); 933789Sahrens zil_create(zilog); 934789Sahrens mutex_enter(&zilog->zl_lock); 935789Sahrens lwb = list_tail(&zilog->zl_lwb_list); 936789Sahrens } 937789Sahrens } 938789Sahrens 939*3063Sperrin /* Loop through in-memory log transactions filling log blocks. */ 9402638Sperrin DTRACE_PROBE1(zil__cw1, zilog_t *, zilog); 941789Sahrens for (;;) { 9422638Sperrin /* 9432638Sperrin * Find the next itx to push: 9442638Sperrin * Push all transactions related to specified foid and all 9452638Sperrin * other transactions except TX_WRITE, TX_TRUNCATE, 9462638Sperrin * TX_SETATTR and TX_ACL for all other files. 9472638Sperrin */ 9482638Sperrin if (itx_next != (itx_t *)-1) 9492638Sperrin itx = itx_next; 9502638Sperrin else 9512638Sperrin itx = list_head(&zilog->zl_itx_list); 9522638Sperrin for (; itx != NULL; itx = list_next(&zilog->zl_itx_list, itx)) { 9532638Sperrin if (foid == 0) /* push all foids? */ 9542638Sperrin break; 955*3063Sperrin if (itx->itx_sync) /* push all O_[D]SYNC */ 956*3063Sperrin break; 9572638Sperrin switch (itx->itx_lr.lrc_txtype) { 9582638Sperrin case TX_SETATTR: 9592638Sperrin case TX_WRITE: 9602638Sperrin case TX_TRUNCATE: 9612638Sperrin case TX_ACL: 9622638Sperrin /* lr_foid is same offset for these records */ 9632638Sperrin if (((lr_write_t *)&itx->itx_lr)->lr_foid 9642638Sperrin != foid) { 9652638Sperrin continue; /* skip this record */ 9662638Sperrin } 9672638Sperrin } 9682638Sperrin break; 9692638Sperrin } 970789Sahrens if (itx == NULL) 971789Sahrens break; 972789Sahrens 973789Sahrens reclen = itx->itx_lr.lrc_reclen; 974789Sahrens if ((itx->itx_lr.lrc_seq > seq) && 9752638Sperrin ((lwb == NULL) || (lwb->lwb_nused == 0) || 976*3063Sperrin (lwb->lwb_nused + reclen > ZIL_BLK_DATA_SZ(lwb)))) { 977789Sahrens break; 978*3063Sperrin } 979789Sahrens 9802638Sperrin /* 9812638Sperrin * Save the next pointer. Even though we soon drop 9822638Sperrin * zl_lock all threads that may change the list 9832638Sperrin * (another writer or zil_itx_clean) can't do so until 9842638Sperrin * they have zl_writer. 9852638Sperrin */ 9862638Sperrin itx_next = list_next(&zilog->zl_itx_list, itx); 987789Sahrens list_remove(&zilog->zl_itx_list, itx); 988*3063Sperrin mutex_exit(&zilog->zl_lock); 989789Sahrens txg = itx->itx_lr.lrc_txg; 990789Sahrens ASSERT(txg); 991789Sahrens 992789Sahrens if (txg > spa_last_synced_txg(spa) || 993789Sahrens txg > spa_freeze_txg(spa)) 994789Sahrens lwb = zil_lwb_commit(zilog, itx, lwb); 995789Sahrens kmem_free(itx, offsetof(itx_t, itx_lr) 996789Sahrens + itx->itx_lr.lrc_reclen); 997789Sahrens mutex_enter(&zilog->zl_lock); 998789Sahrens zilog->zl_itx_list_sz -= reclen; 999789Sahrens } 10002638Sperrin DTRACE_PROBE1(zil__cw2, zilog_t *, zilog); 1001*3063Sperrin /* determine commit sequence number */ 1002*3063Sperrin itx = list_head(&zilog->zl_itx_list); 1003*3063Sperrin if (itx) 1004*3063Sperrin commit_seq = itx->itx_lr.lrc_seq; 1005*3063Sperrin else 1006*3063Sperrin commit_seq = zilog->zl_itx_seq; 1007789Sahrens mutex_exit(&zilog->zl_lock); 1008789Sahrens 1009789Sahrens /* write the last block out */ 1010*3063Sperrin if (lwb != NULL && lwb->lwb_zio != NULL) 1011789Sahrens lwb = zil_lwb_write_start(zilog, lwb); 1012789Sahrens 10131141Sperrin zilog->zl_prev_used = zilog->zl_cur_used; 10141141Sperrin zilog->zl_cur_used = 0; 10151141Sperrin 10162638Sperrin /* 10172638Sperrin * Wait if necessary for the log blocks to be on stable storage. 10182638Sperrin */ 10192638Sperrin if (zilog->zl_root_zio) { 10202638Sperrin DTRACE_PROBE1(zil__cw3, zilog_t *, zilog); 10212638Sperrin (void) zio_wait(zilog->zl_root_zio); 10222638Sperrin DTRACE_PROBE1(zil__cw4, zilog_t *, zilog); 1023*3063Sperrin if (!zfs_nocacheflush) 1024*3063Sperrin zil_flush_vdevs(zilog); 1025789Sahrens } 10261141Sperrin 1027789Sahrens if (zilog->zl_log_error || lwb == NULL) { 1028789Sahrens zilog->zl_log_error = 0; 1029789Sahrens txg_wait_synced(zilog->zl_dmu_pool, 0); 1030789Sahrens } 1031*3063Sperrin 1032*3063Sperrin mutex_enter(&zilog->zl_lock); 10331141Sperrin zilog->zl_writer = B_FALSE; 1034*3063Sperrin 1035*3063Sperrin ASSERT3U(commit_seq, >=, zilog->zl_commit_seq); 1036*3063Sperrin zilog->zl_commit_seq = commit_seq; 10372638Sperrin } 10382638Sperrin 10392638Sperrin /* 10402638Sperrin * Push zfs transactions to stable storage up to the supplied sequence number. 10412638Sperrin * If foid is 0 push out all transactions, otherwise push only those 10422638Sperrin * for that file or might have been used to create that file. 10432638Sperrin */ 10442638Sperrin void 10452638Sperrin zil_commit(zilog_t *zilog, uint64_t seq, uint64_t foid) 10462638Sperrin { 10472638Sperrin if (zilog == NULL || seq == 0) 10482638Sperrin return; 10492638Sperrin 10502638Sperrin mutex_enter(&zilog->zl_lock); 10512638Sperrin 10522638Sperrin seq = MIN(seq, zilog->zl_itx_seq); /* cap seq at largest itx seq */ 10532638Sperrin 1054*3063Sperrin while (zilog->zl_writer) { 10552638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 1056*3063Sperrin if (seq < zilog->zl_commit_seq) { 1057*3063Sperrin mutex_exit(&zilog->zl_lock); 1058*3063Sperrin return; 1059*3063Sperrin } 1060*3063Sperrin } 10612638Sperrin zil_commit_writer(zilog, seq, foid); /* drops zl_lock */ 1062*3063Sperrin /* wake up others waiting on the commit */ 1063*3063Sperrin cv_broadcast(&zilog->zl_cv_writer); 1064*3063Sperrin mutex_exit(&zilog->zl_lock); 1065789Sahrens } 1066789Sahrens 1067789Sahrens /* 1068789Sahrens * Called in syncing context to free committed log blocks and update log header. 1069789Sahrens */ 1070789Sahrens void 1071789Sahrens zil_sync(zilog_t *zilog, dmu_tx_t *tx) 1072789Sahrens { 10731807Sbonwick zil_header_t *zh = zil_header_in_syncing_context(zilog); 1074789Sahrens uint64_t txg = dmu_tx_get_txg(tx); 1075789Sahrens spa_t *spa = zilog->zl_spa; 1076789Sahrens lwb_t *lwb; 1077789Sahrens 10781807Sbonwick mutex_enter(&zilog->zl_lock); 10791807Sbonwick 1080789Sahrens ASSERT(zilog->zl_stop_sync == 0); 1081789Sahrens 10821807Sbonwick zh->zh_replay_seq = zilog->zl_replay_seq[txg & TXG_MASK]; 1083789Sahrens 1084789Sahrens if (zilog->zl_destroy_txg == txg) { 10851807Sbonwick blkptr_t blk = zh->zh_log; 10861807Sbonwick 10871807Sbonwick ASSERT(list_head(&zilog->zl_lwb_list) == NULL); 10881807Sbonwick ASSERT(spa_sync_pass(spa) == 1); 10891807Sbonwick 10901807Sbonwick bzero(zh, sizeof (zil_header_t)); 1091789Sahrens bzero(zilog->zl_replay_seq, sizeof (zilog->zl_replay_seq)); 10921807Sbonwick 10931807Sbonwick if (zilog->zl_keep_first) { 10941807Sbonwick /* 10951807Sbonwick * If this block was part of log chain that couldn't 10961807Sbonwick * be claimed because a device was missing during 10971807Sbonwick * zil_claim(), but that device later returns, 10981807Sbonwick * then this block could erroneously appear valid. 10991807Sbonwick * To guard against this, assign a new GUID to the new 11001807Sbonwick * log chain so it doesn't matter what blk points to. 11011807Sbonwick */ 11021807Sbonwick zil_init_log_chain(zilog, &blk); 11031807Sbonwick zh->zh_log = blk; 11041807Sbonwick } 1105789Sahrens } 1106789Sahrens 1107789Sahrens for (;;) { 1108789Sahrens lwb = list_head(&zilog->zl_lwb_list); 1109789Sahrens if (lwb == NULL) { 1110789Sahrens mutex_exit(&zilog->zl_lock); 1111789Sahrens return; 1112789Sahrens } 11132638Sperrin zh->zh_log = lwb->lwb_blk; 1114789Sahrens if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg) 1115789Sahrens break; 1116789Sahrens list_remove(&zilog->zl_lwb_list, lwb); 1117789Sahrens zio_free_blk(spa, &lwb->lwb_blk, txg); 1118789Sahrens kmem_cache_free(zil_lwb_cache, lwb); 1119789Sahrens } 1120789Sahrens mutex_exit(&zilog->zl_lock); 1121789Sahrens } 1122789Sahrens 1123789Sahrens void 1124789Sahrens zil_init(void) 1125789Sahrens { 1126789Sahrens zil_lwb_cache = kmem_cache_create("zil_lwb_cache", 11272856Snd150628 sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0); 1128789Sahrens } 1129789Sahrens 1130789Sahrens void 1131789Sahrens zil_fini(void) 1132789Sahrens { 1133789Sahrens kmem_cache_destroy(zil_lwb_cache); 1134789Sahrens } 1135789Sahrens 1136789Sahrens zilog_t * 1137789Sahrens zil_alloc(objset_t *os, zil_header_t *zh_phys) 1138789Sahrens { 1139789Sahrens zilog_t *zilog; 1140789Sahrens 1141789Sahrens zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP); 1142789Sahrens 1143789Sahrens zilog->zl_header = zh_phys; 1144789Sahrens zilog->zl_os = os; 1145789Sahrens zilog->zl_spa = dmu_objset_spa(os); 1146789Sahrens zilog->zl_dmu_pool = dmu_objset_pool(os); 11471807Sbonwick zilog->zl_destroy_txg = TXG_INITIAL - 1; 1148789Sahrens 11492856Snd150628 mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL); 11502856Snd150628 1151789Sahrens list_create(&zilog->zl_itx_list, sizeof (itx_t), 1152789Sahrens offsetof(itx_t, itx_node)); 1153789Sahrens 1154789Sahrens list_create(&zilog->zl_lwb_list, sizeof (lwb_t), 1155789Sahrens offsetof(lwb_t, lwb_node)); 1156789Sahrens 1157789Sahrens list_create(&zilog->zl_vdev_list, sizeof (zil_vdev_t), 1158789Sahrens offsetof(zil_vdev_t, vdev_seq_node)); 1159789Sahrens 1160789Sahrens return (zilog); 1161789Sahrens } 1162789Sahrens 1163789Sahrens void 1164789Sahrens zil_free(zilog_t *zilog) 1165789Sahrens { 1166789Sahrens lwb_t *lwb; 1167789Sahrens zil_vdev_t *zv; 1168789Sahrens 1169789Sahrens zilog->zl_stop_sync = 1; 1170789Sahrens 1171789Sahrens while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 1172789Sahrens list_remove(&zilog->zl_lwb_list, lwb); 1173789Sahrens if (lwb->lwb_buf != NULL) 1174789Sahrens zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 1175789Sahrens kmem_cache_free(zil_lwb_cache, lwb); 1176789Sahrens } 1177789Sahrens list_destroy(&zilog->zl_lwb_list); 1178789Sahrens 1179789Sahrens while ((zv = list_head(&zilog->zl_vdev_list)) != NULL) { 1180789Sahrens list_remove(&zilog->zl_vdev_list, zv); 1181789Sahrens kmem_free(zv, sizeof (zil_vdev_t)); 1182789Sahrens } 1183789Sahrens list_destroy(&zilog->zl_vdev_list); 1184789Sahrens 1185789Sahrens ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1186789Sahrens list_destroy(&zilog->zl_itx_list); 11872856Snd150628 mutex_destroy(&zilog->zl_lock); 1188789Sahrens 1189789Sahrens kmem_free(zilog, sizeof (zilog_t)); 1190789Sahrens } 1191789Sahrens 1192789Sahrens /* 11931646Sperrin * return true if the initial log block is not valid 11941362Sperrin */ 11951362Sperrin static int 11961362Sperrin zil_empty(zilog_t *zilog) 11971362Sperrin { 11981807Sbonwick const zil_header_t *zh = zilog->zl_header; 11991807Sbonwick arc_buf_t *abuf = NULL; 12001362Sperrin 12011807Sbonwick if (BP_IS_HOLE(&zh->zh_log)) 12021362Sperrin return (1); 12031362Sperrin 12041807Sbonwick if (zil_read_log_block(zilog, &zh->zh_log, &abuf) != 0) 12051807Sbonwick return (1); 12061807Sbonwick 12071807Sbonwick VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 12081807Sbonwick return (0); 12091362Sperrin } 12101362Sperrin 12111362Sperrin /* 1212789Sahrens * Open an intent log. 1213789Sahrens */ 1214789Sahrens zilog_t * 1215789Sahrens zil_open(objset_t *os, zil_get_data_t *get_data) 1216789Sahrens { 1217789Sahrens zilog_t *zilog = dmu_objset_zil(os); 1218789Sahrens 1219789Sahrens zilog->zl_get_data = get_data; 1220789Sahrens zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri, 1221789Sahrens 2, 2, TASKQ_PREPOPULATE); 1222789Sahrens 1223789Sahrens return (zilog); 1224789Sahrens } 1225789Sahrens 1226789Sahrens /* 1227789Sahrens * Close an intent log. 1228789Sahrens */ 1229789Sahrens void 1230789Sahrens zil_close(zilog_t *zilog) 1231789Sahrens { 12321807Sbonwick /* 12331807Sbonwick * If the log isn't already committed, mark the objset dirty 12341807Sbonwick * (so zil_sync() will be called) and wait for that txg to sync. 12351807Sbonwick */ 12361807Sbonwick if (!zil_is_committed(zilog)) { 12371807Sbonwick uint64_t txg; 12381807Sbonwick dmu_tx_t *tx = dmu_tx_create(zilog->zl_os); 12391807Sbonwick (void) dmu_tx_assign(tx, TXG_WAIT); 12401807Sbonwick dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 12411807Sbonwick txg = dmu_tx_get_txg(tx); 12421807Sbonwick dmu_tx_commit(tx); 12431807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, txg); 12441807Sbonwick } 12451807Sbonwick 1246789Sahrens taskq_destroy(zilog->zl_clean_taskq); 1247789Sahrens zilog->zl_clean_taskq = NULL; 1248789Sahrens zilog->zl_get_data = NULL; 1249789Sahrens 1250789Sahrens zil_itx_clean(zilog); 1251789Sahrens ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1252789Sahrens } 1253789Sahrens 1254789Sahrens /* 1255789Sahrens * Suspend an intent log. While in suspended mode, we still honor 1256789Sahrens * synchronous semantics, but we rely on txg_wait_synced() to do it. 1257789Sahrens * We suspend the log briefly when taking a snapshot so that the snapshot 1258789Sahrens * contains all the data it's supposed to, and has an empty intent log. 1259789Sahrens */ 1260789Sahrens int 1261789Sahrens zil_suspend(zilog_t *zilog) 1262789Sahrens { 12631807Sbonwick const zil_header_t *zh = zilog->zl_header; 1264789Sahrens 1265789Sahrens mutex_enter(&zilog->zl_lock); 12661807Sbonwick if (zh->zh_claim_txg != 0) { /* unplayed log */ 1267789Sahrens mutex_exit(&zilog->zl_lock); 1268789Sahrens return (EBUSY); 1269789Sahrens } 12701807Sbonwick if (zilog->zl_suspend++ != 0) { 12711807Sbonwick /* 12721807Sbonwick * Someone else already began a suspend. 12731807Sbonwick * Just wait for them to finish. 12741807Sbonwick */ 12751807Sbonwick while (zilog->zl_suspending) 12761807Sbonwick cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock); 12771807Sbonwick ASSERT(BP_IS_HOLE(&zh->zh_log)); 12781807Sbonwick mutex_exit(&zilog->zl_lock); 12791807Sbonwick return (0); 12801807Sbonwick } 12811807Sbonwick zilog->zl_suspending = B_TRUE; 1282789Sahrens mutex_exit(&zilog->zl_lock); 1283789Sahrens 12842638Sperrin zil_commit(zilog, UINT64_MAX, 0); 1285789Sahrens 12862638Sperrin /* 12872638Sperrin * Wait for any in-flight log writes to complete. 12882638Sperrin */ 1289789Sahrens mutex_enter(&zilog->zl_lock); 12902638Sperrin while (zilog->zl_writer) 12912638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 1292789Sahrens mutex_exit(&zilog->zl_lock); 1293789Sahrens 12941807Sbonwick zil_destroy(zilog, B_FALSE); 12951807Sbonwick 12961807Sbonwick mutex_enter(&zilog->zl_lock); 12971807Sbonwick ASSERT(BP_IS_HOLE(&zh->zh_log)); 12981807Sbonwick zilog->zl_suspending = B_FALSE; 12991807Sbonwick cv_broadcast(&zilog->zl_cv_suspend); 13001807Sbonwick mutex_exit(&zilog->zl_lock); 1301789Sahrens 1302789Sahrens return (0); 1303789Sahrens } 1304789Sahrens 1305789Sahrens void 1306789Sahrens zil_resume(zilog_t *zilog) 1307789Sahrens { 1308789Sahrens mutex_enter(&zilog->zl_lock); 1309789Sahrens ASSERT(zilog->zl_suspend != 0); 1310789Sahrens zilog->zl_suspend--; 1311789Sahrens mutex_exit(&zilog->zl_lock); 1312789Sahrens } 1313789Sahrens 1314789Sahrens typedef struct zil_replay_arg { 1315789Sahrens objset_t *zr_os; 1316789Sahrens zil_replay_func_t **zr_replay; 1317789Sahrens void *zr_arg; 1318789Sahrens void (*zr_rm_sync)(void *arg); 1319789Sahrens uint64_t *zr_txgp; 1320789Sahrens boolean_t zr_byteswap; 1321789Sahrens char *zr_lrbuf; 1322789Sahrens } zil_replay_arg_t; 1323789Sahrens 1324789Sahrens static void 1325789Sahrens zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg) 1326789Sahrens { 1327789Sahrens zil_replay_arg_t *zr = zra; 13281807Sbonwick const zil_header_t *zh = zilog->zl_header; 1329789Sahrens uint64_t reclen = lr->lrc_reclen; 1330789Sahrens uint64_t txtype = lr->lrc_txtype; 1331*3063Sperrin char *name; 1332*3063Sperrin int pass, error, sunk; 1333789Sahrens 1334789Sahrens if (zilog->zl_stop_replay) 1335789Sahrens return; 1336789Sahrens 1337789Sahrens if (lr->lrc_txg < claim_txg) /* already committed */ 1338789Sahrens return; 1339789Sahrens 1340789Sahrens if (lr->lrc_seq <= zh->zh_replay_seq) /* already replayed */ 1341789Sahrens return; 1342789Sahrens 1343789Sahrens /* 1344789Sahrens * Make a copy of the data so we can revise and extend it. 1345789Sahrens */ 1346789Sahrens bcopy(lr, zr->zr_lrbuf, reclen); 1347789Sahrens 1348789Sahrens /* 1349789Sahrens * The log block containing this lr may have been byteswapped 1350789Sahrens * so that we can easily examine common fields like lrc_txtype. 1351789Sahrens * However, the log is a mix of different data types, and only the 1352789Sahrens * replay vectors know how to byteswap their records. Therefore, if 1353789Sahrens * the lr was byteswapped, undo it before invoking the replay vector. 1354789Sahrens */ 1355789Sahrens if (zr->zr_byteswap) 1356789Sahrens byteswap_uint64_array(zr->zr_lrbuf, reclen); 1357789Sahrens 1358789Sahrens /* 1359789Sahrens * If this is a TX_WRITE with a blkptr, suck in the data. 1360789Sahrens */ 1361789Sahrens if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) { 1362789Sahrens lr_write_t *lrw = (lr_write_t *)lr; 1363789Sahrens blkptr_t *wbp = &lrw->lr_blkptr; 1364789Sahrens uint64_t wlen = lrw->lr_length; 1365789Sahrens char *wbuf = zr->zr_lrbuf + reclen; 1366789Sahrens 1367789Sahrens if (BP_IS_HOLE(wbp)) { /* compressed to a hole */ 1368789Sahrens bzero(wbuf, wlen); 1369789Sahrens } else { 1370789Sahrens /* 1371789Sahrens * A subsequent write may have overwritten this block, 1372789Sahrens * in which case wbp may have been been freed and 1373789Sahrens * reallocated, and our read of wbp may fail with a 1374789Sahrens * checksum error. We can safely ignore this because 1375789Sahrens * the later write will provide the correct data. 1376789Sahrens */ 13771544Seschrock zbookmark_t zb; 13781544Seschrock 13791544Seschrock zb.zb_objset = dmu_objset_id(zilog->zl_os); 13801544Seschrock zb.zb_object = lrw->lr_foid; 13811544Seschrock zb.zb_level = -1; 13821544Seschrock zb.zb_blkid = lrw->lr_offset / BP_GET_LSIZE(wbp); 13831544Seschrock 1384789Sahrens (void) zio_wait(zio_read(NULL, zilog->zl_spa, 1385789Sahrens wbp, wbuf, BP_GET_LSIZE(wbp), NULL, NULL, 1386789Sahrens ZIO_PRIORITY_SYNC_READ, 13871544Seschrock ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb)); 1388789Sahrens (void) memmove(wbuf, wbuf + lrw->lr_blkoff, wlen); 1389789Sahrens } 1390789Sahrens } 1391789Sahrens 1392789Sahrens /* 1393789Sahrens * We must now do two things atomically: replay this log record, 1394789Sahrens * and update the log header to reflect the fact that we did so. 1395789Sahrens * We use the DMU's ability to assign into a specific txg to do this. 1396789Sahrens */ 1397*3063Sperrin for (pass = 1, sunk = B_FALSE; /* CONSTANTCONDITION */; pass++) { 1398789Sahrens uint64_t replay_txg; 1399789Sahrens dmu_tx_t *replay_tx; 1400789Sahrens 1401789Sahrens replay_tx = dmu_tx_create(zr->zr_os); 1402789Sahrens error = dmu_tx_assign(replay_tx, TXG_WAIT); 1403789Sahrens if (error) { 1404789Sahrens dmu_tx_abort(replay_tx); 1405789Sahrens break; 1406789Sahrens } 1407789Sahrens 1408789Sahrens replay_txg = dmu_tx_get_txg(replay_tx); 1409789Sahrens 1410789Sahrens if (txtype == 0 || txtype >= TX_MAX_TYPE) { 1411789Sahrens error = EINVAL; 1412789Sahrens } else { 1413789Sahrens /* 1414789Sahrens * On the first pass, arrange for the replay vector 1415789Sahrens * to fail its dmu_tx_assign(). That's the only way 1416789Sahrens * to ensure that those code paths remain well tested. 1417789Sahrens */ 1418789Sahrens *zr->zr_txgp = replay_txg - (pass == 1); 1419789Sahrens error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lrbuf, 1420789Sahrens zr->zr_byteswap); 1421789Sahrens *zr->zr_txgp = TXG_NOWAIT; 1422789Sahrens } 1423789Sahrens 1424789Sahrens if (error == 0) { 1425789Sahrens dsl_dataset_dirty(dmu_objset_ds(zr->zr_os), replay_tx); 1426789Sahrens zilog->zl_replay_seq[replay_txg & TXG_MASK] = 1427789Sahrens lr->lrc_seq; 1428789Sahrens } 1429789Sahrens 1430789Sahrens dmu_tx_commit(replay_tx); 1431789Sahrens 1432*3063Sperrin if (!error) 1433*3063Sperrin return; 1434*3063Sperrin 1435*3063Sperrin /* 1436*3063Sperrin * The DMU's dnode layer doesn't see removes until the txg 1437*3063Sperrin * commits, so a subsequent claim can spuriously fail with 1438*3063Sperrin * EEXIST. So if we receive any error other than ERESTART 1439*3063Sperrin * we try syncing out any removes then retrying the 1440*3063Sperrin * transaction. 1441*3063Sperrin */ 1442*3063Sperrin if (error != ERESTART && !sunk) { 1443*3063Sperrin if (zr->zr_rm_sync != NULL) 1444*3063Sperrin zr->zr_rm_sync(zr->zr_arg); 1445*3063Sperrin txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0); 1446*3063Sperrin sunk = B_TRUE; 1447*3063Sperrin continue; /* retry */ 1448*3063Sperrin } 1449*3063Sperrin 1450789Sahrens if (error != ERESTART) 1451789Sahrens break; 1452789Sahrens 1453789Sahrens if (pass != 1) 1454789Sahrens txg_wait_open(spa_get_dsl(zilog->zl_spa), 1455789Sahrens replay_txg + 1); 1456789Sahrens 1457789Sahrens dprintf("pass %d, retrying\n", pass); 1458789Sahrens } 1459789Sahrens 1460*3063Sperrin ASSERT(error && error != ERESTART); 1461*3063Sperrin name = kmem_alloc(MAXNAMELEN, KM_SLEEP); 1462*3063Sperrin dmu_objset_name(zr->zr_os, name); 1463*3063Sperrin cmn_err(CE_WARN, "ZFS replay transaction error %d, " 1464*3063Sperrin "dataset %s, seq 0x%llx, txtype %llu\n", 1465*3063Sperrin error, name, (u_longlong_t)lr->lrc_seq, (u_longlong_t)txtype); 1466*3063Sperrin zilog->zl_stop_replay = 1; 1467*3063Sperrin kmem_free(name, MAXNAMELEN); 1468*3063Sperrin } 1469789Sahrens 1470*3063Sperrin /* ARGSUSED */ 1471*3063Sperrin static void 1472*3063Sperrin zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg) 1473*3063Sperrin { 1474*3063Sperrin zilog->zl_replay_blks++; 1475789Sahrens } 1476789Sahrens 1477789Sahrens /* 14781362Sperrin * If this dataset has a non-empty intent log, replay it and destroy it. 1479789Sahrens */ 1480789Sahrens void 1481789Sahrens zil_replay(objset_t *os, void *arg, uint64_t *txgp, 1482789Sahrens zil_replay_func_t *replay_func[TX_MAX_TYPE], void (*rm_sync)(void *arg)) 1483789Sahrens { 1484789Sahrens zilog_t *zilog = dmu_objset_zil(os); 14851807Sbonwick const zil_header_t *zh = zilog->zl_header; 14861807Sbonwick zil_replay_arg_t zr; 14871362Sperrin 14881362Sperrin if (zil_empty(zilog)) { 14891807Sbonwick zil_destroy(zilog, B_TRUE); 14901362Sperrin return; 14911362Sperrin } 1492789Sahrens 1493789Sahrens zr.zr_os = os; 1494789Sahrens zr.zr_replay = replay_func; 1495789Sahrens zr.zr_arg = arg; 1496789Sahrens zr.zr_rm_sync = rm_sync; 1497789Sahrens zr.zr_txgp = txgp; 14981807Sbonwick zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log); 1499789Sahrens zr.zr_lrbuf = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP); 1500789Sahrens 1501789Sahrens /* 1502789Sahrens * Wait for in-progress removes to sync before starting replay. 1503789Sahrens */ 1504789Sahrens if (rm_sync != NULL) 1505789Sahrens rm_sync(arg); 1506789Sahrens txg_wait_synced(zilog->zl_dmu_pool, 0); 1507789Sahrens 1508789Sahrens zilog->zl_stop_replay = 0; 1509*3063Sperrin zilog->zl_replay_time = lbolt; 1510*3063Sperrin ASSERT(zilog->zl_replay_blks == 0); 1511*3063Sperrin (void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr, 15121807Sbonwick zh->zh_claim_txg); 1513789Sahrens kmem_free(zr.zr_lrbuf, 2 * SPA_MAXBLOCKSIZE); 1514789Sahrens 15151807Sbonwick zil_destroy(zilog, B_FALSE); 1516789Sahrens } 15171646Sperrin 15181646Sperrin /* 15191646Sperrin * Report whether all transactions are committed 15201646Sperrin */ 15211646Sperrin int 15221646Sperrin zil_is_committed(zilog_t *zilog) 15231646Sperrin { 15241646Sperrin lwb_t *lwb; 15252638Sperrin int ret; 15261646Sperrin 15272638Sperrin mutex_enter(&zilog->zl_lock); 15282638Sperrin while (zilog->zl_writer) 15292638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 15302638Sperrin 15312638Sperrin /* recent unpushed intent log transactions? */ 15322638Sperrin if (!list_is_empty(&zilog->zl_itx_list)) { 15332638Sperrin ret = B_FALSE; 15342638Sperrin goto out; 15352638Sperrin } 15362638Sperrin 15372638Sperrin /* intent log never used? */ 15382638Sperrin lwb = list_head(&zilog->zl_lwb_list); 15392638Sperrin if (lwb == NULL) { 15402638Sperrin ret = B_TRUE; 15412638Sperrin goto out; 15422638Sperrin } 15431646Sperrin 15441646Sperrin /* 15452638Sperrin * more than 1 log buffer means zil_sync() hasn't yet freed 15462638Sperrin * entries after a txg has committed 15471646Sperrin */ 15482638Sperrin if (list_next(&zilog->zl_lwb_list, lwb)) { 15492638Sperrin ret = B_FALSE; 15502638Sperrin goto out; 15512638Sperrin } 15522638Sperrin 15531646Sperrin ASSERT(zil_empty(zilog)); 15542638Sperrin ret = B_TRUE; 15552638Sperrin out: 15562638Sperrin cv_broadcast(&zilog->zl_cv_writer); 15572638Sperrin mutex_exit(&zilog->zl_lock); 15582638Sperrin return (ret); 15591646Sperrin } 1560