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> 28789Sahrens #include <sys/dmu.h> 29789Sahrens #include <sys/zap.h> 30789Sahrens #include <sys/arc.h> 31789Sahrens #include <sys/stat.h> 32789Sahrens #include <sys/resource.h> 33789Sahrens #include <sys/zil.h> 34789Sahrens #include <sys/zil_impl.h> 35789Sahrens #include <sys/dsl_dataset.h> 36789Sahrens #include <sys/vdev.h> 373668Sgw25295 #include <sys/dmu_tx.h> 38789Sahrens 39789Sahrens /* 40789Sahrens * The zfs intent log (ZIL) saves transaction records of system calls 41789Sahrens * that change the file system in memory with enough information 42789Sahrens * to be able to replay them. These are stored in memory until 43789Sahrens * either the DMU transaction group (txg) commits them to the stable pool 44789Sahrens * and they can be discarded, or they are flushed to the stable log 45789Sahrens * (also in the pool) due to a fsync, O_DSYNC or other synchronous 46789Sahrens * requirement. In the event of a panic or power fail then those log 47789Sahrens * records (transactions) are replayed. 48789Sahrens * 49789Sahrens * There is one ZIL per file system. Its on-disk (pool) format consists 50789Sahrens * of 3 parts: 51789Sahrens * 52789Sahrens * - ZIL header 53789Sahrens * - ZIL blocks 54789Sahrens * - ZIL records 55789Sahrens * 56789Sahrens * A log record holds a system call transaction. Log blocks can 57789Sahrens * hold many log records and the blocks are chained together. 58789Sahrens * Each ZIL block contains a block pointer (blkptr_t) to the next 59789Sahrens * ZIL block in the chain. The ZIL header points to the first 60789Sahrens * block in the chain. Note there is not a fixed place in the pool 61789Sahrens * to hold blocks. They are dynamically allocated and freed as 62789Sahrens * needed from the blocks available. Figure X shows the ZIL structure: 63789Sahrens */ 64789Sahrens 65789Sahrens /* 662986Sek110237 * This global ZIL switch affects all pools 67789Sahrens */ 68789Sahrens int zil_disable = 0; /* disable intent logging */ 692986Sek110237 702986Sek110237 /* 712986Sek110237 * Tunable parameter for debugging or performance analysis. Setting 722986Sek110237 * zfs_nocacheflush will cause corruption on power loss if a volatile 732986Sek110237 * out-of-order write cache is enabled. 742986Sek110237 */ 752986Sek110237 boolean_t zfs_nocacheflush = B_FALSE; 76789Sahrens 77789Sahrens static kmem_cache_t *zil_lwb_cache; 78789Sahrens 7910685SGeorge.Wilson@Sun.COM static boolean_t zil_empty(zilog_t *zilog); 8010685SGeorge.Wilson@Sun.COM 81789Sahrens static int 8210922SJeff.Bonwick@Sun.COM zil_bp_compare(const void *x1, const void *x2) 83789Sahrens { 8410922SJeff.Bonwick@Sun.COM const dva_t *dva1 = &((zil_bp_node_t *)x1)->zn_dva; 8510922SJeff.Bonwick@Sun.COM const dva_t *dva2 = &((zil_bp_node_t *)x2)->zn_dva; 86789Sahrens 87789Sahrens if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2)) 88789Sahrens return (-1); 89789Sahrens if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2)) 90789Sahrens return (1); 91789Sahrens 92789Sahrens if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2)) 93789Sahrens return (-1); 94789Sahrens if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2)) 95789Sahrens return (1); 96789Sahrens 97789Sahrens return (0); 98789Sahrens } 99789Sahrens 100789Sahrens static void 10110922SJeff.Bonwick@Sun.COM zil_bp_tree_init(zilog_t *zilog) 102789Sahrens { 10310922SJeff.Bonwick@Sun.COM avl_create(&zilog->zl_bp_tree, zil_bp_compare, 10410922SJeff.Bonwick@Sun.COM sizeof (zil_bp_node_t), offsetof(zil_bp_node_t, zn_node)); 105789Sahrens } 106789Sahrens 107789Sahrens static void 10810922SJeff.Bonwick@Sun.COM zil_bp_tree_fini(zilog_t *zilog) 109789Sahrens { 11010922SJeff.Bonwick@Sun.COM avl_tree_t *t = &zilog->zl_bp_tree; 11110922SJeff.Bonwick@Sun.COM zil_bp_node_t *zn; 112789Sahrens void *cookie = NULL; 113789Sahrens 114789Sahrens while ((zn = avl_destroy_nodes(t, &cookie)) != NULL) 11510922SJeff.Bonwick@Sun.COM kmem_free(zn, sizeof (zil_bp_node_t)); 116789Sahrens 117789Sahrens avl_destroy(t); 118789Sahrens } 119789Sahrens 12010922SJeff.Bonwick@Sun.COM int 12110922SJeff.Bonwick@Sun.COM zil_bp_tree_add(zilog_t *zilog, const blkptr_t *bp) 122789Sahrens { 12310922SJeff.Bonwick@Sun.COM avl_tree_t *t = &zilog->zl_bp_tree; 12410922SJeff.Bonwick@Sun.COM const dva_t *dva = BP_IDENTITY(bp); 12510922SJeff.Bonwick@Sun.COM zil_bp_node_t *zn; 126789Sahrens avl_index_t where; 127789Sahrens 128789Sahrens if (avl_find(t, dva, &where) != NULL) 129789Sahrens return (EEXIST); 130789Sahrens 13110922SJeff.Bonwick@Sun.COM zn = kmem_alloc(sizeof (zil_bp_node_t), KM_SLEEP); 132789Sahrens zn->zn_dva = *dva; 133789Sahrens avl_insert(t, zn, where); 134789Sahrens 135789Sahrens return (0); 136789Sahrens } 137789Sahrens 1381807Sbonwick static zil_header_t * 1391807Sbonwick zil_header_in_syncing_context(zilog_t *zilog) 1401807Sbonwick { 1411807Sbonwick return ((zil_header_t *)zilog->zl_header); 1421807Sbonwick } 1431807Sbonwick 1441807Sbonwick static void 1451807Sbonwick zil_init_log_chain(zilog_t *zilog, blkptr_t *bp) 1461807Sbonwick { 1471807Sbonwick zio_cksum_t *zc = &bp->blk_cksum; 1481807Sbonwick 1491807Sbonwick zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL); 1501807Sbonwick zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL); 1511807Sbonwick zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os); 1521807Sbonwick zc->zc_word[ZIL_ZC_SEQ] = 1ULL; 1531807Sbonwick } 1541807Sbonwick 155789Sahrens /* 15610922SJeff.Bonwick@Sun.COM * Read a log block and make sure it's valid. 157789Sahrens */ 158789Sahrens static int 15910922SJeff.Bonwick@Sun.COM zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, blkptr_t *nbp, void *dst) 160789Sahrens { 16110922SJeff.Bonwick@Sun.COM enum zio_flag zio_flags = ZIO_FLAG_CANFAIL; 16210922SJeff.Bonwick@Sun.COM uint32_t aflags = ARC_WAIT; 16310922SJeff.Bonwick@Sun.COM arc_buf_t *abuf = NULL; 1641544Seschrock zbookmark_t zb; 165789Sahrens int error; 166789Sahrens 16710922SJeff.Bonwick@Sun.COM if (zilog->zl_header->zh_claim_txg == 0) 16810922SJeff.Bonwick@Sun.COM zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB; 1691807Sbonwick 17010922SJeff.Bonwick@Sun.COM if (!(zilog->zl_header->zh_flags & ZIL_CLAIM_LR_SEQ_VALID)) 17110922SJeff.Bonwick@Sun.COM zio_flags |= ZIO_FLAG_SPECULATIVE; 1721807Sbonwick 17310922SJeff.Bonwick@Sun.COM SET_BOOKMARK(&zb, bp->blk_cksum.zc_word[ZIL_ZC_OBJSET], 17410922SJeff.Bonwick@Sun.COM ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]); 17510922SJeff.Bonwick@Sun.COM 17610922SJeff.Bonwick@Sun.COM error = arc_read_nolock(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf, 17710922SJeff.Bonwick@Sun.COM ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb); 1781807Sbonwick 1791807Sbonwick if (error == 0) { 18010922SJeff.Bonwick@Sun.COM char *data = abuf->b_data; 18110922SJeff.Bonwick@Sun.COM uint64_t size = BP_GET_LSIZE(bp); 18210922SJeff.Bonwick@Sun.COM zil_trailer_t *ztp = (zil_trailer_t *)(data + size) - 1; 1831807Sbonwick zio_cksum_t cksum = bp->blk_cksum; 1841544Seschrock 18510922SJeff.Bonwick@Sun.COM bcopy(data, dst, size); 18610922SJeff.Bonwick@Sun.COM *nbp = ztp->zit_next_blk; 18710922SJeff.Bonwick@Sun.COM 1881807Sbonwick /* 1897522SNeil.Perrin@Sun.COM * Validate the checksummed log block. 1907522SNeil.Perrin@Sun.COM * 1911807Sbonwick * Sequence numbers should be... sequential. The checksum 1921807Sbonwick * verifier for the next block should be bp's checksum plus 1. 1937522SNeil.Perrin@Sun.COM * 1947522SNeil.Perrin@Sun.COM * Also check the log chain linkage and size used. 1951807Sbonwick */ 1961807Sbonwick cksum.zc_word[ZIL_ZC_SEQ]++; 1971807Sbonwick 1987522SNeil.Perrin@Sun.COM if (bcmp(&cksum, &ztp->zit_next_blk.blk_cksum, 1997522SNeil.Perrin@Sun.COM sizeof (cksum)) || BP_IS_HOLE(&ztp->zit_next_blk) || 20010922SJeff.Bonwick@Sun.COM (ztp->zit_nused > (size - sizeof (zil_trailer_t)))) 2017522SNeil.Perrin@Sun.COM error = ECKSUM; 2021807Sbonwick 20310922SJeff.Bonwick@Sun.COM VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 204789Sahrens } 205789Sahrens 20610922SJeff.Bonwick@Sun.COM return (error); 20710922SJeff.Bonwick@Sun.COM } 20810922SJeff.Bonwick@Sun.COM 20910922SJeff.Bonwick@Sun.COM /* 21010922SJeff.Bonwick@Sun.COM * Read a TX_WRITE log data block. 21110922SJeff.Bonwick@Sun.COM */ 21210922SJeff.Bonwick@Sun.COM static int 21310922SJeff.Bonwick@Sun.COM zil_read_log_data(zilog_t *zilog, const lr_write_t *lr, void *wbuf) 21410922SJeff.Bonwick@Sun.COM { 21510922SJeff.Bonwick@Sun.COM enum zio_flag zio_flags = ZIO_FLAG_CANFAIL; 21610922SJeff.Bonwick@Sun.COM const blkptr_t *bp = &lr->lr_blkptr; 21710922SJeff.Bonwick@Sun.COM uint32_t aflags = ARC_WAIT; 21810922SJeff.Bonwick@Sun.COM arc_buf_t *abuf = NULL; 21910922SJeff.Bonwick@Sun.COM zbookmark_t zb; 22010922SJeff.Bonwick@Sun.COM int error; 22110922SJeff.Bonwick@Sun.COM 22210922SJeff.Bonwick@Sun.COM if (BP_IS_HOLE(bp)) { 22310922SJeff.Bonwick@Sun.COM if (wbuf != NULL) 22410922SJeff.Bonwick@Sun.COM bzero(wbuf, MAX(BP_GET_LSIZE(bp), lr->lr_length)); 22510922SJeff.Bonwick@Sun.COM return (0); 22610922SJeff.Bonwick@Sun.COM } 22710922SJeff.Bonwick@Sun.COM 22810922SJeff.Bonwick@Sun.COM if (zilog->zl_header->zh_claim_txg == 0) 22910922SJeff.Bonwick@Sun.COM zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB; 23010922SJeff.Bonwick@Sun.COM 23110922SJeff.Bonwick@Sun.COM SET_BOOKMARK(&zb, dmu_objset_id(zilog->zl_os), lr->lr_foid, 23210922SJeff.Bonwick@Sun.COM ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp)); 23310922SJeff.Bonwick@Sun.COM 23410922SJeff.Bonwick@Sun.COM error = arc_read_nolock(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf, 23510922SJeff.Bonwick@Sun.COM ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb); 23610922SJeff.Bonwick@Sun.COM 23710922SJeff.Bonwick@Sun.COM if (error == 0) { 23810922SJeff.Bonwick@Sun.COM if (wbuf != NULL) 23910922SJeff.Bonwick@Sun.COM bcopy(abuf->b_data, wbuf, arc_buf_size(abuf)); 24010922SJeff.Bonwick@Sun.COM (void) arc_buf_remove_ref(abuf, &abuf); 24110922SJeff.Bonwick@Sun.COM } 242789Sahrens 2431807Sbonwick return (error); 244789Sahrens } 245789Sahrens 246789Sahrens /* 247789Sahrens * Parse the intent log, and call parse_func for each valid record within. 248789Sahrens */ 24910922SJeff.Bonwick@Sun.COM int 250789Sahrens zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func, 251789Sahrens zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg) 252789Sahrens { 2531807Sbonwick const zil_header_t *zh = zilog->zl_header; 25410922SJeff.Bonwick@Sun.COM boolean_t claimed = !!zh->zh_claim_txg; 25510922SJeff.Bonwick@Sun.COM uint64_t claim_blk_seq = claimed ? zh->zh_claim_blk_seq : UINT64_MAX; 25610922SJeff.Bonwick@Sun.COM uint64_t claim_lr_seq = claimed ? zh->zh_claim_lr_seq : UINT64_MAX; 25710922SJeff.Bonwick@Sun.COM uint64_t max_blk_seq = 0; 25810922SJeff.Bonwick@Sun.COM uint64_t max_lr_seq = 0; 25910922SJeff.Bonwick@Sun.COM uint64_t blk_count = 0; 26010922SJeff.Bonwick@Sun.COM uint64_t lr_count = 0; 26110922SJeff.Bonwick@Sun.COM blkptr_t blk, next_blk; 262789Sahrens char *lrbuf, *lrp; 26310922SJeff.Bonwick@Sun.COM int error = 0; 264789Sahrens 26510922SJeff.Bonwick@Sun.COM /* 26610922SJeff.Bonwick@Sun.COM * Old logs didn't record the maximum zh_claim_lr_seq. 26710922SJeff.Bonwick@Sun.COM */ 26810922SJeff.Bonwick@Sun.COM if (!(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID)) 26910922SJeff.Bonwick@Sun.COM claim_lr_seq = UINT64_MAX; 270789Sahrens 271789Sahrens /* 272789Sahrens * Starting at the block pointed to by zh_log we read the log chain. 273789Sahrens * For each block in the chain we strongly check that block to 274789Sahrens * ensure its validity. We stop when an invalid block is found. 275789Sahrens * For each block pointer in the chain we call parse_blk_func(). 276789Sahrens * For each record in each valid block we call parse_lr_func(). 2771807Sbonwick * If the log has been claimed, stop if we encounter a sequence 2781807Sbonwick * number greater than the highest claimed sequence number. 279789Sahrens */ 28010922SJeff.Bonwick@Sun.COM lrbuf = zio_buf_alloc(SPA_MAXBLOCKSIZE); 28110922SJeff.Bonwick@Sun.COM zil_bp_tree_init(zilog); 28210922SJeff.Bonwick@Sun.COM 28310922SJeff.Bonwick@Sun.COM for (blk = zh->zh_log; !BP_IS_HOLE(&blk); blk = next_blk) { 28410922SJeff.Bonwick@Sun.COM zil_trailer_t *ztp = 28510922SJeff.Bonwick@Sun.COM (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1; 28610922SJeff.Bonwick@Sun.COM uint64_t blk_seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 28710922SJeff.Bonwick@Sun.COM int reclen; 2881807Sbonwick 28910922SJeff.Bonwick@Sun.COM if (blk_seq > claim_blk_seq) 29010922SJeff.Bonwick@Sun.COM break; 29110922SJeff.Bonwick@Sun.COM if ((error = parse_blk_func(zilog, &blk, arg, txg)) != 0) 29210922SJeff.Bonwick@Sun.COM break; 29310922SJeff.Bonwick@Sun.COM ASSERT(max_blk_seq < blk_seq); 29410922SJeff.Bonwick@Sun.COM max_blk_seq = blk_seq; 29510922SJeff.Bonwick@Sun.COM blk_count++; 29610922SJeff.Bonwick@Sun.COM 29710922SJeff.Bonwick@Sun.COM if (max_lr_seq == claim_lr_seq && max_blk_seq == claim_blk_seq) 2981807Sbonwick break; 2991807Sbonwick 30010922SJeff.Bonwick@Sun.COM error = zil_read_log_block(zilog, &blk, &next_blk, lrbuf); 301789Sahrens if (error) 302789Sahrens break; 303789Sahrens 304789Sahrens for (lrp = lrbuf; lrp < lrbuf + ztp->zit_nused; lrp += reclen) { 305789Sahrens lr_t *lr = (lr_t *)lrp; 306789Sahrens reclen = lr->lrc_reclen; 307789Sahrens ASSERT3U(reclen, >=, sizeof (lr_t)); 30810922SJeff.Bonwick@Sun.COM if (lr->lrc_seq > claim_lr_seq) 30910922SJeff.Bonwick@Sun.COM goto done; 31010922SJeff.Bonwick@Sun.COM if ((error = parse_lr_func(zilog, lr, arg, txg)) != 0) 31110922SJeff.Bonwick@Sun.COM goto done; 31210922SJeff.Bonwick@Sun.COM ASSERT(max_lr_seq < lr->lrc_seq); 31310922SJeff.Bonwick@Sun.COM max_lr_seq = lr->lrc_seq; 31410922SJeff.Bonwick@Sun.COM lr_count++; 315789Sahrens } 316789Sahrens } 31710922SJeff.Bonwick@Sun.COM done: 31810922SJeff.Bonwick@Sun.COM zilog->zl_parse_error = error; 31910922SJeff.Bonwick@Sun.COM zilog->zl_parse_blk_seq = max_blk_seq; 32010922SJeff.Bonwick@Sun.COM zilog->zl_parse_lr_seq = max_lr_seq; 32110922SJeff.Bonwick@Sun.COM zilog->zl_parse_blk_count = blk_count; 32210922SJeff.Bonwick@Sun.COM zilog->zl_parse_lr_count = lr_count; 32310922SJeff.Bonwick@Sun.COM 32410922SJeff.Bonwick@Sun.COM ASSERT(!claimed || !(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID) || 32510922SJeff.Bonwick@Sun.COM (max_blk_seq == claim_blk_seq && max_lr_seq == claim_lr_seq)); 32610922SJeff.Bonwick@Sun.COM 32710922SJeff.Bonwick@Sun.COM zil_bp_tree_fini(zilog); 32810922SJeff.Bonwick@Sun.COM zio_buf_free(lrbuf, SPA_MAXBLOCKSIZE); 32910922SJeff.Bonwick@Sun.COM 33010922SJeff.Bonwick@Sun.COM return (error); 33110922SJeff.Bonwick@Sun.COM } 33210922SJeff.Bonwick@Sun.COM 33310922SJeff.Bonwick@Sun.COM static int 33410922SJeff.Bonwick@Sun.COM zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg) 33510922SJeff.Bonwick@Sun.COM { 33610922SJeff.Bonwick@Sun.COM /* 33710922SJeff.Bonwick@Sun.COM * Claim log block if not already committed and not already claimed. 33810922SJeff.Bonwick@Sun.COM * If tx == NULL, just verify that the block is claimable. 33910922SJeff.Bonwick@Sun.COM */ 34010922SJeff.Bonwick@Sun.COM if (bp->blk_birth < first_txg || zil_bp_tree_add(zilog, bp) != 0) 34110922SJeff.Bonwick@Sun.COM return (0); 3421807Sbonwick 34310922SJeff.Bonwick@Sun.COM return (zio_wait(zio_claim(NULL, zilog->zl_spa, 34410922SJeff.Bonwick@Sun.COM tx == NULL ? 0 : first_txg, bp, spa_claim_notify, NULL, 34510922SJeff.Bonwick@Sun.COM ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB))); 34610922SJeff.Bonwick@Sun.COM } 34710922SJeff.Bonwick@Sun.COM 34810922SJeff.Bonwick@Sun.COM static int 34910922SJeff.Bonwick@Sun.COM zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg) 35010922SJeff.Bonwick@Sun.COM { 35110922SJeff.Bonwick@Sun.COM lr_write_t *lr = (lr_write_t *)lrc; 35210922SJeff.Bonwick@Sun.COM int error; 35310922SJeff.Bonwick@Sun.COM 35410922SJeff.Bonwick@Sun.COM if (lrc->lrc_txtype != TX_WRITE) 35510922SJeff.Bonwick@Sun.COM return (0); 35610922SJeff.Bonwick@Sun.COM 35710922SJeff.Bonwick@Sun.COM /* 35810922SJeff.Bonwick@Sun.COM * If the block is not readable, don't claim it. This can happen 35910922SJeff.Bonwick@Sun.COM * in normal operation when a log block is written to disk before 36010922SJeff.Bonwick@Sun.COM * some of the dmu_sync() blocks it points to. In this case, the 36110922SJeff.Bonwick@Sun.COM * transaction cannot have been committed to anyone (we would have 36210922SJeff.Bonwick@Sun.COM * waited for all writes to be stable first), so it is semantically 36310922SJeff.Bonwick@Sun.COM * correct to declare this the end of the log. 36410922SJeff.Bonwick@Sun.COM */ 36510922SJeff.Bonwick@Sun.COM if (lr->lr_blkptr.blk_birth >= first_txg && 36610922SJeff.Bonwick@Sun.COM (error = zil_read_log_data(zilog, lr, NULL)) != 0) 36710922SJeff.Bonwick@Sun.COM return (error); 36810922SJeff.Bonwick@Sun.COM 36910922SJeff.Bonwick@Sun.COM return (zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg)); 370789Sahrens } 371789Sahrens 372789Sahrens /* ARGSUSED */ 37310922SJeff.Bonwick@Sun.COM static int 37410922SJeff.Bonwick@Sun.COM zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg) 375789Sahrens { 37610922SJeff.Bonwick@Sun.COM zio_free_zil(zilog->zl_spa, dmu_tx_get_txg(tx), bp); 377789Sahrens 37810922SJeff.Bonwick@Sun.COM return (0); 379789Sahrens } 380789Sahrens 38110922SJeff.Bonwick@Sun.COM static int 382789Sahrens zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg) 383789Sahrens { 38410922SJeff.Bonwick@Sun.COM lr_write_t *lr = (lr_write_t *)lrc; 38510922SJeff.Bonwick@Sun.COM blkptr_t *bp = &lr->lr_blkptr; 38610922SJeff.Bonwick@Sun.COM 387789Sahrens /* 388789Sahrens * If we previously claimed it, we need to free it. 389789Sahrens */ 39010922SJeff.Bonwick@Sun.COM if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE && 39110922SJeff.Bonwick@Sun.COM bp->blk_birth >= claim_txg && zil_bp_tree_add(zilog, bp) == 0) 39210922SJeff.Bonwick@Sun.COM zio_free(zilog->zl_spa, dmu_tx_get_txg(tx), bp); 39310922SJeff.Bonwick@Sun.COM 39410922SJeff.Bonwick@Sun.COM return (0); 395789Sahrens } 396789Sahrens 397789Sahrens /* 398789Sahrens * Create an on-disk intent log. 399789Sahrens */ 400789Sahrens static void 401789Sahrens zil_create(zilog_t *zilog) 402789Sahrens { 4031807Sbonwick const zil_header_t *zh = zilog->zl_header; 404789Sahrens lwb_t *lwb; 4051807Sbonwick uint64_t txg = 0; 4061807Sbonwick dmu_tx_t *tx = NULL; 407789Sahrens blkptr_t blk; 4081807Sbonwick int error = 0; 409789Sahrens 410789Sahrens /* 4111807Sbonwick * Wait for any previous destroy to complete. 412789Sahrens */ 4131807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 4141807Sbonwick 4151807Sbonwick ASSERT(zh->zh_claim_txg == 0); 4161807Sbonwick ASSERT(zh->zh_replay_seq == 0); 4171807Sbonwick 4181807Sbonwick blk = zh->zh_log; 419789Sahrens 420789Sahrens /* 4218109SNeil.Perrin@Sun.COM * If we don't already have an initial log block or we have one 4228109SNeil.Perrin@Sun.COM * but it's the wrong endianness then allocate one. 423789Sahrens */ 4248109SNeil.Perrin@Sun.COM if (BP_IS_HOLE(&blk) || BP_SHOULD_BYTESWAP(&blk)) { 4251807Sbonwick tx = dmu_tx_create(zilog->zl_os); 42610922SJeff.Bonwick@Sun.COM VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0); 4271807Sbonwick dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 4281807Sbonwick txg = dmu_tx_get_txg(tx); 4291807Sbonwick 4308109SNeil.Perrin@Sun.COM if (!BP_IS_HOLE(&blk)) { 43110922SJeff.Bonwick@Sun.COM zio_free_zil(zilog->zl_spa, txg, &blk); 4328109SNeil.Perrin@Sun.COM BP_ZERO(&blk); 4338109SNeil.Perrin@Sun.COM } 4348109SNeil.Perrin@Sun.COM 43510922SJeff.Bonwick@Sun.COM error = zio_alloc_zil(zilog->zl_spa, txg, &blk, NULL, 43610922SJeff.Bonwick@Sun.COM ZIL_MIN_BLKSZ, zilog->zl_logbias == ZFS_LOGBIAS_LATENCY); 4371807Sbonwick 4381807Sbonwick if (error == 0) 4391807Sbonwick zil_init_log_chain(zilog, &blk); 4401362Sperrin } 4411807Sbonwick 4421807Sbonwick /* 4431807Sbonwick * Allocate a log write buffer (lwb) for the first log block. 4441807Sbonwick */ 445789Sahrens if (error == 0) { 446789Sahrens lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 447789Sahrens lwb->lwb_zilog = zilog; 448789Sahrens lwb->lwb_blk = blk; 449789Sahrens lwb->lwb_nused = 0; 450789Sahrens lwb->lwb_sz = BP_GET_LSIZE(&lwb->lwb_blk); 451789Sahrens lwb->lwb_buf = zio_buf_alloc(lwb->lwb_sz); 452789Sahrens lwb->lwb_max_txg = txg; 4532237Smaybee lwb->lwb_zio = NULL; 45410922SJeff.Bonwick@Sun.COM lwb->lwb_tx = NULL; 4552237Smaybee 456789Sahrens mutex_enter(&zilog->zl_lock); 457789Sahrens list_insert_tail(&zilog->zl_lwb_list, lwb); 458789Sahrens mutex_exit(&zilog->zl_lock); 459789Sahrens } 460789Sahrens 4611807Sbonwick /* 4621807Sbonwick * If we just allocated the first log block, commit our transaction 4631807Sbonwick * and wait for zil_sync() to stuff the block poiner into zh_log. 4641807Sbonwick * (zh is part of the MOS, so we cannot modify it in open context.) 4651807Sbonwick */ 4661807Sbonwick if (tx != NULL) { 4671807Sbonwick dmu_tx_commit(tx); 4681362Sperrin txg_wait_synced(zilog->zl_dmu_pool, txg); 4691807Sbonwick } 4701807Sbonwick 4711807Sbonwick ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0); 472789Sahrens } 473789Sahrens 474789Sahrens /* 475789Sahrens * In one tx, free all log blocks and clear the log header. 4761807Sbonwick * If keep_first is set, then we're replaying a log with no content. 4771807Sbonwick * We want to keep the first block, however, so that the first 4781807Sbonwick * synchronous transaction doesn't require a txg_wait_synced() 4791807Sbonwick * in zil_create(). We don't need to txg_wait_synced() here either 4801807Sbonwick * when keep_first is set, because both zil_create() and zil_destroy() 4811807Sbonwick * will wait for any in-progress destroys to complete. 482789Sahrens */ 483789Sahrens void 4841807Sbonwick zil_destroy(zilog_t *zilog, boolean_t keep_first) 485789Sahrens { 4861807Sbonwick const zil_header_t *zh = zilog->zl_header; 4871807Sbonwick lwb_t *lwb; 488789Sahrens dmu_tx_t *tx; 489789Sahrens uint64_t txg; 490789Sahrens 4911807Sbonwick /* 4921807Sbonwick * Wait for any previous destroy to complete. 4931807Sbonwick */ 4941807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 495789Sahrens 49610922SJeff.Bonwick@Sun.COM zilog->zl_old_header = *zh; /* debugging aid */ 49710922SJeff.Bonwick@Sun.COM 4981807Sbonwick if (BP_IS_HOLE(&zh->zh_log)) 499789Sahrens return; 500789Sahrens 501789Sahrens tx = dmu_tx_create(zilog->zl_os); 50210922SJeff.Bonwick@Sun.COM VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0); 503789Sahrens dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 504789Sahrens txg = dmu_tx_get_txg(tx); 505789Sahrens 5061807Sbonwick mutex_enter(&zilog->zl_lock); 5071807Sbonwick 5081807Sbonwick ASSERT3U(zilog->zl_destroy_txg, <, txg); 509789Sahrens zilog->zl_destroy_txg = txg; 51010922SJeff.Bonwick@Sun.COM zilog->zl_keep_first = keep_first; 5111807Sbonwick 5121807Sbonwick if (!list_is_empty(&zilog->zl_lwb_list)) { 5131807Sbonwick ASSERT(zh->zh_claim_txg == 0); 51410922SJeff.Bonwick@Sun.COM ASSERT(!keep_first); 5151807Sbonwick while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 5161807Sbonwick list_remove(&zilog->zl_lwb_list, lwb); 5171807Sbonwick if (lwb->lwb_buf != NULL) 5181807Sbonwick zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 51910922SJeff.Bonwick@Sun.COM zio_free_zil(zilog->zl_spa, txg, &lwb->lwb_blk); 5201807Sbonwick kmem_cache_free(zil_lwb_cache, lwb); 5211807Sbonwick } 52210922SJeff.Bonwick@Sun.COM } else if (!keep_first) { 52310922SJeff.Bonwick@Sun.COM (void) zil_parse(zilog, zil_free_log_block, 52410922SJeff.Bonwick@Sun.COM zil_free_log_record, tx, zh->zh_claim_txg); 5251807Sbonwick } 5262638Sperrin mutex_exit(&zilog->zl_lock); 527789Sahrens 528789Sahrens dmu_tx_commit(tx); 529789Sahrens } 530789Sahrens 5312199Sahrens int 532*11209SMatthew.Ahrens@Sun.COM zil_claim(const char *osname, void *txarg) 533789Sahrens { 534789Sahrens dmu_tx_t *tx = txarg; 535789Sahrens uint64_t first_txg = dmu_tx_get_txg(tx); 536789Sahrens zilog_t *zilog; 537789Sahrens zil_header_t *zh; 538789Sahrens objset_t *os; 539789Sahrens int error; 540789Sahrens 54110298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(osname, FTAG, &os); 542789Sahrens if (error) { 5437294Sperrin cmn_err(CE_WARN, "can't open objset for %s", osname); 5442199Sahrens return (0); 545789Sahrens } 546789Sahrens 547789Sahrens zilog = dmu_objset_zil(os); 5481807Sbonwick zh = zil_header_in_syncing_context(zilog); 549789Sahrens 55010922SJeff.Bonwick@Sun.COM if (spa_get_log_state(zilog->zl_spa) == SPA_LOG_CLEAR) { 5519701SGeorge.Wilson@Sun.COM if (!BP_IS_HOLE(&zh->zh_log)) 55210922SJeff.Bonwick@Sun.COM zio_free_zil(zilog->zl_spa, first_txg, &zh->zh_log); 5539701SGeorge.Wilson@Sun.COM BP_ZERO(&zh->zh_log); 5549701SGeorge.Wilson@Sun.COM dsl_dataset_dirty(dmu_objset_ds(os), tx); 55510921STim.Haley@Sun.COM dmu_objset_rele(os, FTAG); 55610921STim.Haley@Sun.COM return (0); 5579701SGeorge.Wilson@Sun.COM } 5589701SGeorge.Wilson@Sun.COM 559789Sahrens /* 5601807Sbonwick * Claim all log blocks if we haven't already done so, and remember 5611807Sbonwick * the highest claimed sequence number. This ensures that if we can 5621807Sbonwick * read only part of the log now (e.g. due to a missing device), 5631807Sbonwick * but we can read the entire log later, we will not try to replay 5641807Sbonwick * or destroy beyond the last block we successfully claimed. 565789Sahrens */ 566789Sahrens ASSERT3U(zh->zh_claim_txg, <=, first_txg); 567789Sahrens if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) { 56810922SJeff.Bonwick@Sun.COM (void) zil_parse(zilog, zil_claim_log_block, 56910922SJeff.Bonwick@Sun.COM zil_claim_log_record, tx, first_txg); 570789Sahrens zh->zh_claim_txg = first_txg; 57110922SJeff.Bonwick@Sun.COM zh->zh_claim_blk_seq = zilog->zl_parse_blk_seq; 57210922SJeff.Bonwick@Sun.COM zh->zh_claim_lr_seq = zilog->zl_parse_lr_seq; 57310922SJeff.Bonwick@Sun.COM if (zilog->zl_parse_lr_count || zilog->zl_parse_blk_count > 1) 57410922SJeff.Bonwick@Sun.COM zh->zh_flags |= ZIL_REPLAY_NEEDED; 57510922SJeff.Bonwick@Sun.COM zh->zh_flags |= ZIL_CLAIM_LR_SEQ_VALID; 576789Sahrens dsl_dataset_dirty(dmu_objset_ds(os), tx); 577789Sahrens } 5781807Sbonwick 579789Sahrens ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1)); 58010298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 5812199Sahrens return (0); 582789Sahrens } 583789Sahrens 5847294Sperrin /* 5857294Sperrin * Check the log by walking the log chain. 5867294Sperrin * Checksum errors are ok as they indicate the end of the chain. 5877294Sperrin * Any other error (no device or read failure) returns an error. 5887294Sperrin */ 5897294Sperrin int 590*11209SMatthew.Ahrens@Sun.COM zil_check_log_chain(const char *osname, void *tx) 5917294Sperrin { 5927294Sperrin zilog_t *zilog; 5937294Sperrin objset_t *os; 5947294Sperrin int error; 5957294Sperrin 59610922SJeff.Bonwick@Sun.COM ASSERT(tx == NULL); 59710922SJeff.Bonwick@Sun.COM 59810298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(osname, FTAG, &os); 5997294Sperrin if (error) { 6007294Sperrin cmn_err(CE_WARN, "can't open objset for %s", osname); 6017294Sperrin return (0); 6027294Sperrin } 6037294Sperrin 6047294Sperrin zilog = dmu_objset_zil(os); 6057294Sperrin 60610922SJeff.Bonwick@Sun.COM /* 60710922SJeff.Bonwick@Sun.COM * Because tx == NULL, zil_claim_log_block() will not actually claim 60810922SJeff.Bonwick@Sun.COM * any blocks, but just determine whether it is possible to do so. 60910922SJeff.Bonwick@Sun.COM * In addition to checking the log chain, zil_claim_log_block() 61010922SJeff.Bonwick@Sun.COM * will invoke zio_claim() with a done func of spa_claim_notify(), 61110922SJeff.Bonwick@Sun.COM * which will update spa_max_claim_txg. See spa_load() for details. 61210922SJeff.Bonwick@Sun.COM */ 61310922SJeff.Bonwick@Sun.COM error = zil_parse(zilog, zil_claim_log_block, zil_claim_log_record, tx, 61410922SJeff.Bonwick@Sun.COM zilog->zl_header->zh_claim_txg ? -1ULL : spa_first_txg(os->os_spa)); 61510922SJeff.Bonwick@Sun.COM 61610298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 61710922SJeff.Bonwick@Sun.COM 61810922SJeff.Bonwick@Sun.COM return ((error == ECKSUM || error == ENOENT) ? 0 : error); 6197294Sperrin } 6207294Sperrin 6215688Sbonwick static int 6225688Sbonwick zil_vdev_compare(const void *x1, const void *x2) 623789Sahrens { 6245875Sperrin uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev; 6255875Sperrin uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev; 6265688Sbonwick 6275688Sbonwick if (v1 < v2) 6285688Sbonwick return (-1); 6295688Sbonwick if (v1 > v2) 6305688Sbonwick return (1); 6315688Sbonwick 6325688Sbonwick return (0); 6335688Sbonwick } 6345688Sbonwick 6355688Sbonwick void 63610922SJeff.Bonwick@Sun.COM zil_add_block(zilog_t *zilog, const blkptr_t *bp) 6375688Sbonwick { 6385688Sbonwick avl_tree_t *t = &zilog->zl_vdev_tree; 6395688Sbonwick avl_index_t where; 6405688Sbonwick zil_vdev_node_t *zv, zvsearch; 6415688Sbonwick int ndvas = BP_GET_NDVAS(bp); 6425688Sbonwick int i; 643789Sahrens 6442986Sek110237 if (zfs_nocacheflush) 645789Sahrens return; 646789Sahrens 6475688Sbonwick ASSERT(zilog->zl_writer); 6485688Sbonwick 6495688Sbonwick /* 6505688Sbonwick * Even though we're zl_writer, we still need a lock because the 6515688Sbonwick * zl_get_data() callbacks may have dmu_sync() done callbacks 6525688Sbonwick * that will run concurrently. 6535688Sbonwick */ 6545688Sbonwick mutex_enter(&zilog->zl_vdev_lock); 6555688Sbonwick for (i = 0; i < ndvas; i++) { 6565688Sbonwick zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]); 6575688Sbonwick if (avl_find(t, &zvsearch, &where) == NULL) { 6585688Sbonwick zv = kmem_alloc(sizeof (*zv), KM_SLEEP); 6595688Sbonwick zv->zv_vdev = zvsearch.zv_vdev; 6605688Sbonwick avl_insert(t, zv, where); 6613063Sperrin } 6623063Sperrin } 6635688Sbonwick mutex_exit(&zilog->zl_vdev_lock); 6643063Sperrin } 6653063Sperrin 666789Sahrens void 6672638Sperrin zil_flush_vdevs(zilog_t *zilog) 668789Sahrens { 6693063Sperrin spa_t *spa = zilog->zl_spa; 6705688Sbonwick avl_tree_t *t = &zilog->zl_vdev_tree; 6715688Sbonwick void *cookie = NULL; 6725688Sbonwick zil_vdev_node_t *zv; 6735688Sbonwick zio_t *zio; 6743063Sperrin 6753063Sperrin ASSERT(zilog->zl_writer); 676789Sahrens 6775688Sbonwick /* 6785688Sbonwick * We don't need zl_vdev_lock here because we're the zl_writer, 6795688Sbonwick * and all zl_get_data() callbacks are done. 6805688Sbonwick */ 6815688Sbonwick if (avl_numnodes(t) == 0) 6825688Sbonwick return; 6835688Sbonwick 6847754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 6855688Sbonwick 6867754SJeff.Bonwick@Sun.COM zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); 6875688Sbonwick 6885688Sbonwick while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) { 6895688Sbonwick vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev); 6905688Sbonwick if (vd != NULL) 6915688Sbonwick zio_flush(zio, vd); 6925688Sbonwick kmem_free(zv, sizeof (*zv)); 6933063Sperrin } 694789Sahrens 695789Sahrens /* 696789Sahrens * Wait for all the flushes to complete. Not all devices actually 697789Sahrens * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails. 698789Sahrens */ 6995688Sbonwick (void) zio_wait(zio); 7005688Sbonwick 7017754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_STATE, FTAG); 702789Sahrens } 703789Sahrens 704789Sahrens /* 705789Sahrens * Function called when a log block write completes 706789Sahrens */ 707789Sahrens static void 708789Sahrens zil_lwb_write_done(zio_t *zio) 709789Sahrens { 710789Sahrens lwb_t *lwb = zio->io_private; 711789Sahrens zilog_t *zilog = lwb->lwb_zilog; 71210922SJeff.Bonwick@Sun.COM dmu_tx_t *tx = lwb->lwb_tx; 713789Sahrens 7147754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF); 7157754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_CHECKSUM(zio->io_bp) == ZIO_CHECKSUM_ZILOG); 7167754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_TYPE(zio->io_bp) == DMU_OT_INTENT_LOG); 7177754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 7187754SJeff.Bonwick@Sun.COM ASSERT(BP_GET_BYTEORDER(zio->io_bp) == ZFS_HOST_BYTEORDER); 7197754SJeff.Bonwick@Sun.COM ASSERT(!BP_IS_GANG(zio->io_bp)); 7207754SJeff.Bonwick@Sun.COM ASSERT(!BP_IS_HOLE(zio->io_bp)); 7217754SJeff.Bonwick@Sun.COM ASSERT(zio->io_bp->blk_fill == 0); 7227754SJeff.Bonwick@Sun.COM 723789Sahrens /* 7249493SNeil.Perrin@Sun.COM * Ensure the lwb buffer pointer is cleared before releasing 7259493SNeil.Perrin@Sun.COM * the txg. If we have had an allocation failure and 7269493SNeil.Perrin@Sun.COM * the txg is waiting to sync then we want want zil_sync() 7279493SNeil.Perrin@Sun.COM * to remove the lwb so that it's not picked up as the next new 7289493SNeil.Perrin@Sun.COM * one in zil_commit_writer(). zil_sync() will only remove 7299493SNeil.Perrin@Sun.COM * the lwb if lwb_buf is null. 730789Sahrens */ 731789Sahrens zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 732789Sahrens mutex_enter(&zilog->zl_lock); 733789Sahrens lwb->lwb_buf = NULL; 73410922SJeff.Bonwick@Sun.COM lwb->lwb_tx = NULL; 73510922SJeff.Bonwick@Sun.COM mutex_exit(&zilog->zl_lock); 7369493SNeil.Perrin@Sun.COM 7379493SNeil.Perrin@Sun.COM /* 7389493SNeil.Perrin@Sun.COM * Now that we've written this log block, we have a stable pointer 7399493SNeil.Perrin@Sun.COM * to the next block in the chain, so it's OK to let the txg in 74010922SJeff.Bonwick@Sun.COM * which we allocated the next block sync. 7419493SNeil.Perrin@Sun.COM */ 74210922SJeff.Bonwick@Sun.COM dmu_tx_commit(tx); 743789Sahrens } 744789Sahrens 745789Sahrens /* 7462237Smaybee * Initialize the io for a log block. 7472237Smaybee */ 7482237Smaybee static void 7492237Smaybee zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb) 7502237Smaybee { 7512237Smaybee zbookmark_t zb; 7522237Smaybee 75310922SJeff.Bonwick@Sun.COM SET_BOOKMARK(&zb, lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET], 75410922SJeff.Bonwick@Sun.COM ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, 75510922SJeff.Bonwick@Sun.COM lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ]); 7562237Smaybee 7572638Sperrin if (zilog->zl_root_zio == NULL) { 7582638Sperrin zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL, 7592638Sperrin ZIO_FLAG_CANFAIL); 7602638Sperrin } 7613063Sperrin if (lwb->lwb_zio == NULL) { 7623063Sperrin lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa, 7639701SGeorge.Wilson@Sun.COM 0, &lwb->lwb_blk, lwb->lwb_buf, lwb->lwb_sz, 7649701SGeorge.Wilson@Sun.COM zil_lwb_write_done, lwb, ZIO_PRIORITY_LOG_WRITE, 76510685SGeorge.Wilson@Sun.COM ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE, &zb); 7663063Sperrin } 7672237Smaybee } 7682237Smaybee 7692237Smaybee /* 77010879SNeil.Perrin@Sun.COM * Use the slog as long as the logbias is 'latency' and the current commit size 77110879SNeil.Perrin@Sun.COM * is less than the limit or the total list size is less than 2X the limit. 77210879SNeil.Perrin@Sun.COM * Limit checking is disabled by setting zil_slog_limit to UINT64_MAX. 77310879SNeil.Perrin@Sun.COM */ 77410879SNeil.Perrin@Sun.COM uint64_t zil_slog_limit = 1024 * 1024; 77510879SNeil.Perrin@Sun.COM #define USE_SLOG(zilog) (((zilog)->zl_logbias == ZFS_LOGBIAS_LATENCY) && \ 77610879SNeil.Perrin@Sun.COM (((zilog)->zl_cur_used < zil_slog_limit) || \ 77710879SNeil.Perrin@Sun.COM ((zilog)->zl_itx_list_sz < (zil_slog_limit << 1)))) 77810879SNeil.Perrin@Sun.COM 77910879SNeil.Perrin@Sun.COM /* 780789Sahrens * Start a log block write and advance to the next log block. 781789Sahrens * Calls are serialized. 782789Sahrens */ 783789Sahrens static lwb_t * 784789Sahrens zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb) 785789Sahrens { 786789Sahrens lwb_t *nlwb; 787789Sahrens zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1; 7881807Sbonwick spa_t *spa = zilog->zl_spa; 7891807Sbonwick blkptr_t *bp = &ztp->zit_next_blk; 79010922SJeff.Bonwick@Sun.COM dmu_tx_t *tx; 791789Sahrens uint64_t txg; 792789Sahrens uint64_t zil_blksz; 793789Sahrens int error; 794789Sahrens 795789Sahrens ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb)); 796789Sahrens 797789Sahrens /* 798789Sahrens * Allocate the next block and save its address in this block 799789Sahrens * before writing it in order to establish the log chain. 800789Sahrens * Note that if the allocation of nlwb synced before we wrote 801789Sahrens * the block that points at it (lwb), we'd leak it if we crashed. 80210922SJeff.Bonwick@Sun.COM * Therefore, we don't do dmu_tx_commit() until zil_lwb_write_done(). 80310922SJeff.Bonwick@Sun.COM * We dirty the dataset to ensure that zil_sync() will be called 80410922SJeff.Bonwick@Sun.COM * to clean up in the event of allocation failure or I/O failure. 805789Sahrens */ 80610922SJeff.Bonwick@Sun.COM tx = dmu_tx_create(zilog->zl_os); 80710922SJeff.Bonwick@Sun.COM VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0); 80810922SJeff.Bonwick@Sun.COM dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 80910922SJeff.Bonwick@Sun.COM txg = dmu_tx_get_txg(tx); 81010922SJeff.Bonwick@Sun.COM 81110922SJeff.Bonwick@Sun.COM lwb->lwb_tx = tx; 812789Sahrens 813789Sahrens /* 8141141Sperrin * Pick a ZIL blocksize. We request a size that is the 8151141Sperrin * maximum of the previous used size, the current used size and 8161141Sperrin * the amount waiting in the queue. 817789Sahrens */ 8182237Smaybee zil_blksz = MAX(zilog->zl_prev_used, 8192237Smaybee zilog->zl_cur_used + sizeof (*ztp)); 8201141Sperrin zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp)); 8211842Sperrin zil_blksz = P2ROUNDUP_TYPED(zil_blksz, ZIL_MIN_BLKSZ, uint64_t); 8221141Sperrin if (zil_blksz > ZIL_MAX_BLKSZ) 8231141Sperrin zil_blksz = ZIL_MAX_BLKSZ; 824789Sahrens 8253063Sperrin BP_ZERO(bp); 8263063Sperrin /* pass the old blkptr in order to spread log blocks across devs */ 82710922SJeff.Bonwick@Sun.COM error = zio_alloc_zil(spa, txg, bp, &lwb->lwb_blk, zil_blksz, 82810879SNeil.Perrin@Sun.COM USE_SLOG(zilog)); 829789Sahrens if (error) { 8301544Seschrock /* 83110922SJeff.Bonwick@Sun.COM * Since we've just experienced an allocation failure, 8323668Sgw25295 * terminate the current lwb and send it on its way. 8333668Sgw25295 */ 8343668Sgw25295 ztp->zit_pad = 0; 8353668Sgw25295 ztp->zit_nused = lwb->lwb_nused; 8363668Sgw25295 ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum; 8373668Sgw25295 zio_nowait(lwb->lwb_zio); 8383668Sgw25295 8393668Sgw25295 /* 8401544Seschrock * By returning NULL the caller will call tx_wait_synced() 8411544Seschrock */ 842789Sahrens return (NULL); 843789Sahrens } 844789Sahrens 8451807Sbonwick ASSERT3U(bp->blk_birth, ==, txg); 8461544Seschrock ztp->zit_pad = 0; 847789Sahrens ztp->zit_nused = lwb->lwb_nused; 848789Sahrens ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum; 8491807Sbonwick bp->blk_cksum = lwb->lwb_blk.blk_cksum; 8501807Sbonwick bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++; 851789Sahrens 852789Sahrens /* 853789Sahrens * Allocate a new log write buffer (lwb). 854789Sahrens */ 855789Sahrens nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 856789Sahrens nlwb->lwb_zilog = zilog; 8571807Sbonwick nlwb->lwb_blk = *bp; 858789Sahrens nlwb->lwb_nused = 0; 859789Sahrens nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk); 860789Sahrens nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz); 861789Sahrens nlwb->lwb_max_txg = txg; 8622237Smaybee nlwb->lwb_zio = NULL; 86310922SJeff.Bonwick@Sun.COM nlwb->lwb_tx = NULL; 864789Sahrens 865789Sahrens /* 8663063Sperrin * Put new lwb at the end of the log chain 867789Sahrens */ 868789Sahrens mutex_enter(&zilog->zl_lock); 869789Sahrens list_insert_tail(&zilog->zl_lwb_list, nlwb); 8703063Sperrin mutex_exit(&zilog->zl_lock); 8713063Sperrin 8725688Sbonwick /* Record the block for later vdev flushing */ 8735688Sbonwick zil_add_block(zilog, &lwb->lwb_blk); 874789Sahrens 875789Sahrens /* 8762237Smaybee * kick off the write for the old log block 877789Sahrens */ 8783063Sperrin ASSERT(lwb->lwb_zio); 8792237Smaybee zio_nowait(lwb->lwb_zio); 880789Sahrens 881789Sahrens return (nlwb); 882789Sahrens } 883789Sahrens 884789Sahrens static lwb_t * 885789Sahrens zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb) 886789Sahrens { 887789Sahrens lr_t *lrc = &itx->itx_lr; /* common log record */ 88810922SJeff.Bonwick@Sun.COM lr_write_t *lrw = (lr_write_t *)lrc; 88910922SJeff.Bonwick@Sun.COM char *lr_buf; 890789Sahrens uint64_t txg = lrc->lrc_txg; 891789Sahrens uint64_t reclen = lrc->lrc_reclen; 89210922SJeff.Bonwick@Sun.COM uint64_t dlen = 0; 893789Sahrens 894789Sahrens if (lwb == NULL) 895789Sahrens return (NULL); 89610922SJeff.Bonwick@Sun.COM 897789Sahrens ASSERT(lwb->lwb_buf != NULL); 898789Sahrens 8992237Smaybee if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY) 9002237Smaybee dlen = P2ROUNDUP_TYPED( 90110922SJeff.Bonwick@Sun.COM lrw->lr_length, sizeof (uint64_t), uint64_t); 9021669Sperrin 9031669Sperrin zilog->zl_cur_used += (reclen + dlen); 9041669Sperrin 9053063Sperrin zil_lwb_write_init(zilog, lwb); 9063063Sperrin 9071669Sperrin /* 9081669Sperrin * If this record won't fit in the current log block, start a new one. 9091669Sperrin */ 9101669Sperrin if (lwb->lwb_nused + reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) { 9111669Sperrin lwb = zil_lwb_write_start(zilog, lwb); 9122237Smaybee if (lwb == NULL) 9131669Sperrin return (NULL); 9143063Sperrin zil_lwb_write_init(zilog, lwb); 9151669Sperrin ASSERT(lwb->lwb_nused == 0); 9161669Sperrin if (reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) { 9171669Sperrin txg_wait_synced(zilog->zl_dmu_pool, txg); 918789Sahrens return (lwb); 919789Sahrens } 920789Sahrens } 921789Sahrens 92210922SJeff.Bonwick@Sun.COM lr_buf = lwb->lwb_buf + lwb->lwb_nused; 92310922SJeff.Bonwick@Sun.COM bcopy(lrc, lr_buf, reclen); 92410922SJeff.Bonwick@Sun.COM lrc = (lr_t *)lr_buf; 92510922SJeff.Bonwick@Sun.COM lrw = (lr_write_t *)lrc; 9262237Smaybee 9272237Smaybee /* 9282237Smaybee * If it's a write, fetch the data or get its blkptr as appropriate. 9292237Smaybee */ 9302237Smaybee if (lrc->lrc_txtype == TX_WRITE) { 9312237Smaybee if (txg > spa_freeze_txg(zilog->zl_spa)) 9322237Smaybee txg_wait_synced(zilog->zl_dmu_pool, txg); 9332237Smaybee if (itx->itx_wr_state != WR_COPIED) { 9342237Smaybee char *dbuf; 9352237Smaybee int error; 9362237Smaybee 9372237Smaybee if (dlen) { 9382237Smaybee ASSERT(itx->itx_wr_state == WR_NEED_COPY); 93910922SJeff.Bonwick@Sun.COM dbuf = lr_buf + reclen; 94010922SJeff.Bonwick@Sun.COM lrw->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( 94610922SJeff.Bonwick@Sun.COM itx->itx_private, lrw, 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 95910922SJeff.Bonwick@Sun.COM /* 96010922SJeff.Bonwick@Sun.COM * We're actually making an entry, so update lrc_seq to be the 96110922SJeff.Bonwick@Sun.COM * log record sequence number. Note that this is generally not 96210922SJeff.Bonwick@Sun.COM * equal to the itx sequence number because not all transactions 96310922SJeff.Bonwick@Sun.COM * are synchronous, and sometimes spa_sync() gets there first. 96410922SJeff.Bonwick@Sun.COM */ 96510922SJeff.Bonwick@Sun.COM lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */ 9662237Smaybee lwb->lwb_nused += reclen + dlen; 967789Sahrens lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg); 968789Sahrens ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb)); 969789Sahrens ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0); 970789Sahrens 971789Sahrens return (lwb); 972789Sahrens } 973789Sahrens 974789Sahrens itx_t * 9755331Samw zil_itx_create(uint64_t txtype, size_t lrsize) 976789Sahrens { 977789Sahrens itx_t *itx; 978789Sahrens 9791842Sperrin lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t); 980789Sahrens 981789Sahrens itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP); 982789Sahrens itx->itx_lr.lrc_txtype = txtype; 983789Sahrens itx->itx_lr.lrc_reclen = lrsize; 9846101Sperrin itx->itx_sod = lrsize; /* if write & WR_NEED_COPY will be increased */ 985789Sahrens itx->itx_lr.lrc_seq = 0; /* defensive */ 986789Sahrens 987789Sahrens return (itx); 988789Sahrens } 989789Sahrens 99010922SJeff.Bonwick@Sun.COM void 99110922SJeff.Bonwick@Sun.COM zil_itx_destroy(itx_t *itx) 99210922SJeff.Bonwick@Sun.COM { 99310922SJeff.Bonwick@Sun.COM kmem_free(itx, offsetof(itx_t, itx_lr) + itx->itx_lr.lrc_reclen); 99410922SJeff.Bonwick@Sun.COM } 99510922SJeff.Bonwick@Sun.COM 996789Sahrens uint64_t 997789Sahrens zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx) 998789Sahrens { 999789Sahrens uint64_t seq; 1000789Sahrens 1001789Sahrens ASSERT(itx->itx_lr.lrc_seq == 0); 100210922SJeff.Bonwick@Sun.COM ASSERT(!zilog->zl_replay); 1003789Sahrens 1004789Sahrens mutex_enter(&zilog->zl_lock); 1005789Sahrens list_insert_tail(&zilog->zl_itx_list, itx); 10066101Sperrin zilog->zl_itx_list_sz += itx->itx_sod; 1007789Sahrens itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx); 1008789Sahrens itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq; 1009789Sahrens mutex_exit(&zilog->zl_lock); 1010789Sahrens 1011789Sahrens return (seq); 1012789Sahrens } 1013789Sahrens 1014789Sahrens /* 1015789Sahrens * Free up all in-memory intent log transactions that have now been synced. 1016789Sahrens */ 1017789Sahrens static void 1018789Sahrens zil_itx_clean(zilog_t *zilog) 1019789Sahrens { 1020789Sahrens uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa); 1021789Sahrens uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa); 10223778Sjohansen list_t clean_list; 1023789Sahrens itx_t *itx; 1024789Sahrens 10253778Sjohansen list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node)); 10263778Sjohansen 1027789Sahrens mutex_enter(&zilog->zl_lock); 10282638Sperrin /* wait for a log writer to finish walking list */ 10292638Sperrin while (zilog->zl_writer) { 10302638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 10312638Sperrin } 10323778Sjohansen 10333778Sjohansen /* 10343778Sjohansen * Move the sync'd log transactions to a separate list so we can call 10353778Sjohansen * kmem_free without holding the zl_lock. 10363778Sjohansen * 10373778Sjohansen * There is no need to set zl_writer as we don't drop zl_lock here 10383778Sjohansen */ 1039789Sahrens while ((itx = list_head(&zilog->zl_itx_list)) != NULL && 1040789Sahrens itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) { 1041789Sahrens list_remove(&zilog->zl_itx_list, itx); 10426101Sperrin zilog->zl_itx_list_sz -= itx->itx_sod; 10433778Sjohansen list_insert_tail(&clean_list, itx); 10443778Sjohansen } 10453778Sjohansen cv_broadcast(&zilog->zl_cv_writer); 10463778Sjohansen mutex_exit(&zilog->zl_lock); 10473778Sjohansen 10483778Sjohansen /* destroy sync'd log transactions */ 10493778Sjohansen while ((itx = list_head(&clean_list)) != NULL) { 10503778Sjohansen list_remove(&clean_list, itx); 105110922SJeff.Bonwick@Sun.COM zil_itx_destroy(itx); 1052789Sahrens } 10533778Sjohansen list_destroy(&clean_list); 1054789Sahrens } 1055789Sahrens 10562638Sperrin /* 10573063Sperrin * If there are any in-memory intent log transactions which have now been 10583063Sperrin * synced then start up a taskq to free them. 10592638Sperrin */ 1060789Sahrens void 1061789Sahrens zil_clean(zilog_t *zilog) 1062789Sahrens { 10633063Sperrin itx_t *itx; 10643063Sperrin 1065789Sahrens mutex_enter(&zilog->zl_lock); 10663063Sperrin itx = list_head(&zilog->zl_itx_list); 10673063Sperrin if ((itx != NULL) && 10683063Sperrin (itx->itx_lr.lrc_txg <= spa_last_synced_txg(zilog->zl_spa))) { 1069789Sahrens (void) taskq_dispatch(zilog->zl_clean_taskq, 107010879SNeil.Perrin@Sun.COM (task_func_t *)zil_itx_clean, zilog, TQ_NOSLEEP); 10713063Sperrin } 1072789Sahrens mutex_exit(&zilog->zl_lock); 1073789Sahrens } 1074789Sahrens 10757754SJeff.Bonwick@Sun.COM static void 10762638Sperrin zil_commit_writer(zilog_t *zilog, uint64_t seq, uint64_t foid) 1077789Sahrens { 1078789Sahrens uint64_t txg; 10793063Sperrin uint64_t commit_seq = 0; 108010922SJeff.Bonwick@Sun.COM itx_t *itx, *itx_next; 1081789Sahrens lwb_t *lwb; 1082789Sahrens spa_t *spa; 108310922SJeff.Bonwick@Sun.COM int error = 0; 1084789Sahrens 10852638Sperrin zilog->zl_writer = B_TRUE; 10867754SJeff.Bonwick@Sun.COM ASSERT(zilog->zl_root_zio == NULL); 1087789Sahrens spa = zilog->zl_spa; 1088789Sahrens 1089789Sahrens if (zilog->zl_suspend) { 1090789Sahrens lwb = NULL; 1091789Sahrens } else { 1092789Sahrens lwb = list_tail(&zilog->zl_lwb_list); 1093789Sahrens if (lwb == NULL) { 10942638Sperrin /* 10952638Sperrin * Return if there's nothing to flush before we 10962638Sperrin * dirty the fs by calling zil_create() 10972638Sperrin */ 10982638Sperrin if (list_is_empty(&zilog->zl_itx_list)) { 10992638Sperrin zilog->zl_writer = B_FALSE; 11002638Sperrin return; 11012638Sperrin } 1102789Sahrens mutex_exit(&zilog->zl_lock); 1103789Sahrens zil_create(zilog); 1104789Sahrens mutex_enter(&zilog->zl_lock); 1105789Sahrens lwb = list_tail(&zilog->zl_lwb_list); 1106789Sahrens } 1107789Sahrens } 1108789Sahrens 11093063Sperrin /* Loop through in-memory log transactions filling log blocks. */ 11102638Sperrin DTRACE_PROBE1(zil__cw1, zilog_t *, zilog); 111110922SJeff.Bonwick@Sun.COM 111210922SJeff.Bonwick@Sun.COM for (itx = list_head(&zilog->zl_itx_list); itx; itx = itx_next) { 11132638Sperrin /* 111410922SJeff.Bonwick@Sun.COM * Save the next pointer. Even though we drop zl_lock below, 111510922SJeff.Bonwick@Sun.COM * all threads that can remove itx list entries (other writers 111610922SJeff.Bonwick@Sun.COM * and zil_itx_clean()) can't do so until they have zl_writer. 11172638Sperrin */ 111810922SJeff.Bonwick@Sun.COM itx_next = list_next(&zilog->zl_itx_list, itx); 111910922SJeff.Bonwick@Sun.COM 112010922SJeff.Bonwick@Sun.COM /* 112110922SJeff.Bonwick@Sun.COM * Determine whether to push this itx. 112210922SJeff.Bonwick@Sun.COM * Push all transactions related to specified foid and 112310922SJeff.Bonwick@Sun.COM * all other transactions except those that can be logged 112410922SJeff.Bonwick@Sun.COM * out of order (TX_WRITE, TX_TRUNCATE, TX_SETATTR, TX_ACL) 112510922SJeff.Bonwick@Sun.COM * for all other files. 112610922SJeff.Bonwick@Sun.COM * 112710922SJeff.Bonwick@Sun.COM * If foid == 0 (meaning "push all foids") or 112810922SJeff.Bonwick@Sun.COM * itx->itx_sync is set (meaning O_[D]SYNC), push regardless. 112910922SJeff.Bonwick@Sun.COM */ 113010922SJeff.Bonwick@Sun.COM if (foid != 0 && !itx->itx_sync && 113110922SJeff.Bonwick@Sun.COM TX_OOO(itx->itx_lr.lrc_txtype) && 113210922SJeff.Bonwick@Sun.COM ((lr_ooo_t *)&itx->itx_lr)->lr_foid != foid) 113310922SJeff.Bonwick@Sun.COM continue; /* skip this record */ 1134789Sahrens 1135789Sahrens if ((itx->itx_lr.lrc_seq > seq) && 11362638Sperrin ((lwb == NULL) || (lwb->lwb_nused == 0) || 113710922SJeff.Bonwick@Sun.COM (lwb->lwb_nused + itx->itx_sod > ZIL_BLK_DATA_SZ(lwb)))) 1138789Sahrens break; 1139789Sahrens 1140789Sahrens list_remove(&zilog->zl_itx_list, itx); 11416101Sperrin zilog->zl_itx_list_sz -= itx->itx_sod; 114210922SJeff.Bonwick@Sun.COM 11433063Sperrin mutex_exit(&zilog->zl_lock); 114410922SJeff.Bonwick@Sun.COM 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); 115110922SJeff.Bonwick@Sun.COM 115210922SJeff.Bonwick@Sun.COM zil_itx_destroy(itx); 115310922SJeff.Bonwick@Sun.COM 1154789Sahrens mutex_enter(&zilog->zl_lock); 1155789Sahrens } 11562638Sperrin DTRACE_PROBE1(zil__cw2, zilog_t *, zilog); 11573063Sperrin /* determine commit sequence number */ 11583063Sperrin itx = list_head(&zilog->zl_itx_list); 11593063Sperrin if (itx) 116010922SJeff.Bonwick@Sun.COM commit_seq = itx->itx_lr.lrc_seq - 1; 11613063Sperrin else 11623063Sperrin commit_seq = zilog->zl_itx_seq; 1163789Sahrens mutex_exit(&zilog->zl_lock); 1164789Sahrens 1165789Sahrens /* write the last block out */ 11663063Sperrin if (lwb != NULL && lwb->lwb_zio != NULL) 1167789Sahrens lwb = zil_lwb_write_start(zilog, lwb); 1168789Sahrens 11691141Sperrin zilog->zl_prev_used = zilog->zl_cur_used; 11701141Sperrin zilog->zl_cur_used = 0; 11711141Sperrin 11722638Sperrin /* 11732638Sperrin * Wait if necessary for the log blocks to be on stable storage. 11742638Sperrin */ 11752638Sperrin if (zilog->zl_root_zio) { 11762638Sperrin DTRACE_PROBE1(zil__cw3, zilog_t *, zilog); 117710922SJeff.Bonwick@Sun.COM error = zio_wait(zilog->zl_root_zio); 11787754SJeff.Bonwick@Sun.COM zilog->zl_root_zio = NULL; 11792638Sperrin DTRACE_PROBE1(zil__cw4, zilog_t *, zilog); 11805688Sbonwick zil_flush_vdevs(zilog); 1181789Sahrens } 11821141Sperrin 118310922SJeff.Bonwick@Sun.COM if (error || lwb == NULL) 1184789Sahrens txg_wait_synced(zilog->zl_dmu_pool, 0); 11853063Sperrin 11863063Sperrin mutex_enter(&zilog->zl_lock); 11871141Sperrin zilog->zl_writer = B_FALSE; 11883063Sperrin 11893063Sperrin ASSERT3U(commit_seq, >=, zilog->zl_commit_seq); 11903063Sperrin zilog->zl_commit_seq = commit_seq; 119110922SJeff.Bonwick@Sun.COM 119210922SJeff.Bonwick@Sun.COM /* 119310922SJeff.Bonwick@Sun.COM * Remember the highest committed log sequence number for ztest. 119410922SJeff.Bonwick@Sun.COM * We only update this value when all the log writes succeeded, 119510922SJeff.Bonwick@Sun.COM * because ztest wants to ASSERT that it got the whole log chain. 119610922SJeff.Bonwick@Sun.COM */ 119710922SJeff.Bonwick@Sun.COM if (error == 0 && lwb != NULL) 119810922SJeff.Bonwick@Sun.COM zilog->zl_commit_lr_seq = zilog->zl_lr_seq; 11992638Sperrin } 12002638Sperrin 12012638Sperrin /* 12022638Sperrin * Push zfs transactions to stable storage up to the supplied sequence number. 12032638Sperrin * If foid is 0 push out all transactions, otherwise push only those 12042638Sperrin * for that file or might have been used to create that file. 12052638Sperrin */ 12062638Sperrin void 12072638Sperrin zil_commit(zilog_t *zilog, uint64_t seq, uint64_t foid) 12082638Sperrin { 12092638Sperrin if (zilog == NULL || seq == 0) 12102638Sperrin return; 12112638Sperrin 12122638Sperrin mutex_enter(&zilog->zl_lock); 12132638Sperrin 12142638Sperrin seq = MIN(seq, zilog->zl_itx_seq); /* cap seq at largest itx seq */ 12152638Sperrin 12163063Sperrin while (zilog->zl_writer) { 12172638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 121810922SJeff.Bonwick@Sun.COM if (seq <= zilog->zl_commit_seq) { 12193063Sperrin mutex_exit(&zilog->zl_lock); 12203063Sperrin return; 12213063Sperrin } 12223063Sperrin } 12232638Sperrin zil_commit_writer(zilog, seq, foid); /* drops zl_lock */ 12243063Sperrin /* wake up others waiting on the commit */ 12253063Sperrin cv_broadcast(&zilog->zl_cv_writer); 12263063Sperrin mutex_exit(&zilog->zl_lock); 1227789Sahrens } 1228789Sahrens 1229789Sahrens /* 123010922SJeff.Bonwick@Sun.COM * Report whether all transactions are committed. 123110922SJeff.Bonwick@Sun.COM */ 123210922SJeff.Bonwick@Sun.COM static boolean_t 123310922SJeff.Bonwick@Sun.COM zil_is_committed(zilog_t *zilog) 123410922SJeff.Bonwick@Sun.COM { 123510922SJeff.Bonwick@Sun.COM lwb_t *lwb; 123610922SJeff.Bonwick@Sun.COM boolean_t committed; 123710922SJeff.Bonwick@Sun.COM 123810922SJeff.Bonwick@Sun.COM mutex_enter(&zilog->zl_lock); 123910922SJeff.Bonwick@Sun.COM 124010922SJeff.Bonwick@Sun.COM while (zilog->zl_writer) 124110922SJeff.Bonwick@Sun.COM cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 124210922SJeff.Bonwick@Sun.COM 124310922SJeff.Bonwick@Sun.COM if (!list_is_empty(&zilog->zl_itx_list)) 124410922SJeff.Bonwick@Sun.COM committed = B_FALSE; /* unpushed transactions */ 124510922SJeff.Bonwick@Sun.COM else if ((lwb = list_head(&zilog->zl_lwb_list)) == NULL) 124610922SJeff.Bonwick@Sun.COM committed = B_TRUE; /* intent log never used */ 124710922SJeff.Bonwick@Sun.COM else if (list_next(&zilog->zl_lwb_list, lwb) != NULL) 124810922SJeff.Bonwick@Sun.COM committed = B_FALSE; /* zil_sync() not done yet */ 124910922SJeff.Bonwick@Sun.COM else 125010922SJeff.Bonwick@Sun.COM committed = B_TRUE; /* everything synced */ 125110922SJeff.Bonwick@Sun.COM 125210922SJeff.Bonwick@Sun.COM mutex_exit(&zilog->zl_lock); 125310922SJeff.Bonwick@Sun.COM return (committed); 125410922SJeff.Bonwick@Sun.COM } 125510922SJeff.Bonwick@Sun.COM 125610922SJeff.Bonwick@Sun.COM /* 1257789Sahrens * Called in syncing context to free committed log blocks and update log header. 1258789Sahrens */ 1259789Sahrens void 1260789Sahrens zil_sync(zilog_t *zilog, dmu_tx_t *tx) 1261789Sahrens { 12621807Sbonwick zil_header_t *zh = zil_header_in_syncing_context(zilog); 1263789Sahrens uint64_t txg = dmu_tx_get_txg(tx); 1264789Sahrens spa_t *spa = zilog->zl_spa; 126510922SJeff.Bonwick@Sun.COM uint64_t *replayed_seq = &zilog->zl_replayed_seq[txg & TXG_MASK]; 1266789Sahrens lwb_t *lwb; 1267789Sahrens 12689396SMatthew.Ahrens@Sun.COM /* 12699396SMatthew.Ahrens@Sun.COM * We don't zero out zl_destroy_txg, so make sure we don't try 12709396SMatthew.Ahrens@Sun.COM * to destroy it twice. 12719396SMatthew.Ahrens@Sun.COM */ 12729396SMatthew.Ahrens@Sun.COM if (spa_sync_pass(spa) != 1) 12739396SMatthew.Ahrens@Sun.COM return; 12749396SMatthew.Ahrens@Sun.COM 12751807Sbonwick mutex_enter(&zilog->zl_lock); 12761807Sbonwick 1277789Sahrens ASSERT(zilog->zl_stop_sync == 0); 1278789Sahrens 127910922SJeff.Bonwick@Sun.COM if (*replayed_seq != 0) { 128010922SJeff.Bonwick@Sun.COM ASSERT(zh->zh_replay_seq < *replayed_seq); 128110922SJeff.Bonwick@Sun.COM zh->zh_replay_seq = *replayed_seq; 128210922SJeff.Bonwick@Sun.COM *replayed_seq = 0; 128310922SJeff.Bonwick@Sun.COM } 1284789Sahrens 1285789Sahrens if (zilog->zl_destroy_txg == txg) { 12861807Sbonwick blkptr_t blk = zh->zh_log; 12871807Sbonwick 12881807Sbonwick ASSERT(list_head(&zilog->zl_lwb_list) == NULL); 12891807Sbonwick 12901807Sbonwick bzero(zh, sizeof (zil_header_t)); 12918227SNeil.Perrin@Sun.COM bzero(zilog->zl_replayed_seq, sizeof (zilog->zl_replayed_seq)); 12921807Sbonwick 12931807Sbonwick if (zilog->zl_keep_first) { 12941807Sbonwick /* 12951807Sbonwick * If this block was part of log chain that couldn't 12961807Sbonwick * be claimed because a device was missing during 12971807Sbonwick * zil_claim(), but that device later returns, 12981807Sbonwick * then this block could erroneously appear valid. 12991807Sbonwick * To guard against this, assign a new GUID to the new 13001807Sbonwick * log chain so it doesn't matter what blk points to. 13011807Sbonwick */ 13021807Sbonwick zil_init_log_chain(zilog, &blk); 13031807Sbonwick zh->zh_log = blk; 13041807Sbonwick } 1305789Sahrens } 1306789Sahrens 13079701SGeorge.Wilson@Sun.COM while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 13082638Sperrin zh->zh_log = lwb->lwb_blk; 1309789Sahrens if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg) 1310789Sahrens break; 1311789Sahrens list_remove(&zilog->zl_lwb_list, lwb); 131210922SJeff.Bonwick@Sun.COM zio_free_zil(spa, txg, &lwb->lwb_blk); 1313789Sahrens kmem_cache_free(zil_lwb_cache, lwb); 13143668Sgw25295 13153668Sgw25295 /* 13163668Sgw25295 * If we don't have anything left in the lwb list then 13173668Sgw25295 * we've had an allocation failure and we need to zero 13183668Sgw25295 * out the zil_header blkptr so that we don't end 13193668Sgw25295 * up freeing the same block twice. 13203668Sgw25295 */ 13213668Sgw25295 if (list_head(&zilog->zl_lwb_list) == NULL) 13223668Sgw25295 BP_ZERO(&zh->zh_log); 1323789Sahrens } 1324789Sahrens mutex_exit(&zilog->zl_lock); 1325789Sahrens } 1326789Sahrens 1327789Sahrens void 1328789Sahrens zil_init(void) 1329789Sahrens { 1330789Sahrens zil_lwb_cache = kmem_cache_create("zil_lwb_cache", 13312856Snd150628 sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0); 1332789Sahrens } 1333789Sahrens 1334789Sahrens void 1335789Sahrens zil_fini(void) 1336789Sahrens { 1337789Sahrens kmem_cache_destroy(zil_lwb_cache); 1338789Sahrens } 1339789Sahrens 134010310SNeil.Perrin@Sun.COM void 134110310SNeil.Perrin@Sun.COM zil_set_logbias(zilog_t *zilog, uint64_t logbias) 134210310SNeil.Perrin@Sun.COM { 134310310SNeil.Perrin@Sun.COM zilog->zl_logbias = logbias; 134410310SNeil.Perrin@Sun.COM } 134510310SNeil.Perrin@Sun.COM 1346789Sahrens zilog_t * 1347789Sahrens zil_alloc(objset_t *os, zil_header_t *zh_phys) 1348789Sahrens { 1349789Sahrens zilog_t *zilog; 1350789Sahrens 1351789Sahrens zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP); 1352789Sahrens 1353789Sahrens zilog->zl_header = zh_phys; 1354789Sahrens zilog->zl_os = os; 1355789Sahrens zilog->zl_spa = dmu_objset_spa(os); 1356789Sahrens zilog->zl_dmu_pool = dmu_objset_pool(os); 13571807Sbonwick zilog->zl_destroy_txg = TXG_INITIAL - 1; 135810310SNeil.Perrin@Sun.COM zilog->zl_logbias = dmu_objset_logbias(os); 1359789Sahrens 13602856Snd150628 mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL); 13612856Snd150628 1362789Sahrens list_create(&zilog->zl_itx_list, sizeof (itx_t), 1363789Sahrens offsetof(itx_t, itx_node)); 1364789Sahrens 1365789Sahrens list_create(&zilog->zl_lwb_list, sizeof (lwb_t), 1366789Sahrens offsetof(lwb_t, lwb_node)); 1367789Sahrens 13685688Sbonwick mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL); 13695688Sbonwick 13705688Sbonwick avl_create(&zilog->zl_vdev_tree, zil_vdev_compare, 13715688Sbonwick sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node)); 1372789Sahrens 13735913Sperrin cv_init(&zilog->zl_cv_writer, NULL, CV_DEFAULT, NULL); 13745913Sperrin cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL); 13755913Sperrin 1376789Sahrens return (zilog); 1377789Sahrens } 1378789Sahrens 1379789Sahrens void 1380789Sahrens zil_free(zilog_t *zilog) 1381789Sahrens { 1382789Sahrens lwb_t *lwb; 1383789Sahrens 1384789Sahrens zilog->zl_stop_sync = 1; 1385789Sahrens 1386789Sahrens while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 1387789Sahrens list_remove(&zilog->zl_lwb_list, lwb); 1388789Sahrens if (lwb->lwb_buf != NULL) 1389789Sahrens zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 1390789Sahrens kmem_cache_free(zil_lwb_cache, lwb); 1391789Sahrens } 1392789Sahrens list_destroy(&zilog->zl_lwb_list); 1393789Sahrens 13945688Sbonwick avl_destroy(&zilog->zl_vdev_tree); 13955688Sbonwick mutex_destroy(&zilog->zl_vdev_lock); 1396789Sahrens 1397789Sahrens ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1398789Sahrens list_destroy(&zilog->zl_itx_list); 13992856Snd150628 mutex_destroy(&zilog->zl_lock); 1400789Sahrens 14015913Sperrin cv_destroy(&zilog->zl_cv_writer); 14025913Sperrin cv_destroy(&zilog->zl_cv_suspend); 14035913Sperrin 1404789Sahrens kmem_free(zilog, sizeof (zilog_t)); 1405789Sahrens } 1406789Sahrens 1407789Sahrens /* 1408789Sahrens * Open an intent log. 1409789Sahrens */ 1410789Sahrens zilog_t * 1411789Sahrens zil_open(objset_t *os, zil_get_data_t *get_data) 1412789Sahrens { 1413789Sahrens zilog_t *zilog = dmu_objset_zil(os); 1414789Sahrens 1415789Sahrens zilog->zl_get_data = get_data; 1416789Sahrens zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri, 1417789Sahrens 2, 2, TASKQ_PREPOPULATE); 1418789Sahrens 1419789Sahrens return (zilog); 1420789Sahrens } 1421789Sahrens 1422789Sahrens /* 1423789Sahrens * Close an intent log. 1424789Sahrens */ 1425789Sahrens void 1426789Sahrens zil_close(zilog_t *zilog) 1427789Sahrens { 14281807Sbonwick /* 14291807Sbonwick * If the log isn't already committed, mark the objset dirty 14301807Sbonwick * (so zil_sync() will be called) and wait for that txg to sync. 14311807Sbonwick */ 14321807Sbonwick if (!zil_is_committed(zilog)) { 14331807Sbonwick uint64_t txg; 14341807Sbonwick dmu_tx_t *tx = dmu_tx_create(zilog->zl_os); 143510922SJeff.Bonwick@Sun.COM VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0); 14361807Sbonwick dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 14371807Sbonwick txg = dmu_tx_get_txg(tx); 14381807Sbonwick dmu_tx_commit(tx); 14391807Sbonwick txg_wait_synced(zilog->zl_dmu_pool, txg); 14401807Sbonwick } 14411807Sbonwick 1442789Sahrens taskq_destroy(zilog->zl_clean_taskq); 1443789Sahrens zilog->zl_clean_taskq = NULL; 1444789Sahrens zilog->zl_get_data = NULL; 1445789Sahrens 1446789Sahrens zil_itx_clean(zilog); 1447789Sahrens ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1448789Sahrens } 1449789Sahrens 1450789Sahrens /* 1451789Sahrens * Suspend an intent log. While in suspended mode, we still honor 1452789Sahrens * synchronous semantics, but we rely on txg_wait_synced() to do it. 1453789Sahrens * We suspend the log briefly when taking a snapshot so that the snapshot 1454789Sahrens * contains all the data it's supposed to, and has an empty intent log. 1455789Sahrens */ 1456789Sahrens int 1457789Sahrens zil_suspend(zilog_t *zilog) 1458789Sahrens { 14591807Sbonwick const zil_header_t *zh = zilog->zl_header; 1460789Sahrens 1461789Sahrens mutex_enter(&zilog->zl_lock); 14628989SNeil.Perrin@Sun.COM if (zh->zh_flags & ZIL_REPLAY_NEEDED) { /* unplayed log */ 1463789Sahrens mutex_exit(&zilog->zl_lock); 1464789Sahrens return (EBUSY); 1465789Sahrens } 14661807Sbonwick if (zilog->zl_suspend++ != 0) { 14671807Sbonwick /* 14681807Sbonwick * Someone else already began a suspend. 14691807Sbonwick * Just wait for them to finish. 14701807Sbonwick */ 14711807Sbonwick while (zilog->zl_suspending) 14721807Sbonwick cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock); 14731807Sbonwick mutex_exit(&zilog->zl_lock); 14741807Sbonwick return (0); 14751807Sbonwick } 14761807Sbonwick zilog->zl_suspending = B_TRUE; 1477789Sahrens mutex_exit(&zilog->zl_lock); 1478789Sahrens 14792638Sperrin zil_commit(zilog, UINT64_MAX, 0); 1480789Sahrens 14812638Sperrin /* 14822638Sperrin * Wait for any in-flight log writes to complete. 14832638Sperrin */ 1484789Sahrens mutex_enter(&zilog->zl_lock); 14852638Sperrin while (zilog->zl_writer) 14862638Sperrin cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 1487789Sahrens mutex_exit(&zilog->zl_lock); 1488789Sahrens 14891807Sbonwick zil_destroy(zilog, B_FALSE); 14901807Sbonwick 14911807Sbonwick mutex_enter(&zilog->zl_lock); 14921807Sbonwick zilog->zl_suspending = B_FALSE; 14931807Sbonwick cv_broadcast(&zilog->zl_cv_suspend); 14941807Sbonwick mutex_exit(&zilog->zl_lock); 1495789Sahrens 1496789Sahrens return (0); 1497789Sahrens } 1498789Sahrens 1499789Sahrens void 1500789Sahrens zil_resume(zilog_t *zilog) 1501789Sahrens { 1502789Sahrens mutex_enter(&zilog->zl_lock); 1503789Sahrens ASSERT(zilog->zl_suspend != 0); 1504789Sahrens zilog->zl_suspend--; 1505789Sahrens mutex_exit(&zilog->zl_lock); 1506789Sahrens } 1507789Sahrens 1508789Sahrens typedef struct zil_replay_arg { 1509789Sahrens zil_replay_func_t **zr_replay; 1510789Sahrens void *zr_arg; 1511789Sahrens boolean_t zr_byteswap; 151210922SJeff.Bonwick@Sun.COM char *zr_lr; 1513789Sahrens } zil_replay_arg_t; 1514789Sahrens 151510922SJeff.Bonwick@Sun.COM static int 151610922SJeff.Bonwick@Sun.COM zil_replay_error(zilog_t *zilog, lr_t *lr, int error) 151710922SJeff.Bonwick@Sun.COM { 151810922SJeff.Bonwick@Sun.COM char name[MAXNAMELEN]; 151910922SJeff.Bonwick@Sun.COM 152010922SJeff.Bonwick@Sun.COM zilog->zl_replaying_seq--; /* didn't actually replay this one */ 152110922SJeff.Bonwick@Sun.COM 152210922SJeff.Bonwick@Sun.COM dmu_objset_name(zilog->zl_os, name); 152310922SJeff.Bonwick@Sun.COM 152410922SJeff.Bonwick@Sun.COM cmn_err(CE_WARN, "ZFS replay transaction error %d, " 152510922SJeff.Bonwick@Sun.COM "dataset %s, seq 0x%llx, txtype %llu %s\n", error, name, 152610922SJeff.Bonwick@Sun.COM (u_longlong_t)lr->lrc_seq, 152710922SJeff.Bonwick@Sun.COM (u_longlong_t)(lr->lrc_txtype & ~TX_CI), 152810922SJeff.Bonwick@Sun.COM (lr->lrc_txtype & TX_CI) ? "CI" : ""); 152910922SJeff.Bonwick@Sun.COM 153010922SJeff.Bonwick@Sun.COM return (error); 153110922SJeff.Bonwick@Sun.COM } 153210922SJeff.Bonwick@Sun.COM 153310922SJeff.Bonwick@Sun.COM static int 1534789Sahrens zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg) 1535789Sahrens { 1536789Sahrens zil_replay_arg_t *zr = zra; 15371807Sbonwick const zil_header_t *zh = zilog->zl_header; 1538789Sahrens uint64_t reclen = lr->lrc_reclen; 1539789Sahrens uint64_t txtype = lr->lrc_txtype; 154010922SJeff.Bonwick@Sun.COM int error = 0; 1541789Sahrens 154210922SJeff.Bonwick@Sun.COM zilog->zl_replaying_seq = lr->lrc_seq; 154310922SJeff.Bonwick@Sun.COM 154410922SJeff.Bonwick@Sun.COM if (lr->lrc_seq <= zh->zh_replay_seq) /* already replayed */ 154510922SJeff.Bonwick@Sun.COM return (0); 1546789Sahrens 1547789Sahrens if (lr->lrc_txg < claim_txg) /* already committed */ 154810922SJeff.Bonwick@Sun.COM return (0); 1549789Sahrens 15505331Samw /* Strip case-insensitive bit, still present in log record */ 15515331Samw txtype &= ~TX_CI; 15525331Samw 155310922SJeff.Bonwick@Sun.COM if (txtype == 0 || txtype >= TX_MAX_TYPE) 155410922SJeff.Bonwick@Sun.COM return (zil_replay_error(zilog, lr, EINVAL)); 155510922SJeff.Bonwick@Sun.COM 155610922SJeff.Bonwick@Sun.COM /* 155710922SJeff.Bonwick@Sun.COM * If this record type can be logged out of order, the object 155810922SJeff.Bonwick@Sun.COM * (lr_foid) may no longer exist. That's legitimate, not an error. 155910922SJeff.Bonwick@Sun.COM */ 156010922SJeff.Bonwick@Sun.COM if (TX_OOO(txtype)) { 156110922SJeff.Bonwick@Sun.COM error = dmu_object_info(zilog->zl_os, 156210922SJeff.Bonwick@Sun.COM ((lr_ooo_t *)lr)->lr_foid, NULL); 156310922SJeff.Bonwick@Sun.COM if (error == ENOENT || error == EEXIST) 156410922SJeff.Bonwick@Sun.COM return (0); 15658227SNeil.Perrin@Sun.COM } 15668227SNeil.Perrin@Sun.COM 1567789Sahrens /* 1568789Sahrens * Make a copy of the data so we can revise and extend it. 1569789Sahrens */ 157010922SJeff.Bonwick@Sun.COM bcopy(lr, zr->zr_lr, reclen); 157110922SJeff.Bonwick@Sun.COM 157210922SJeff.Bonwick@Sun.COM /* 157310922SJeff.Bonwick@Sun.COM * If this is a TX_WRITE with a blkptr, suck in the data. 157410922SJeff.Bonwick@Sun.COM */ 157510922SJeff.Bonwick@Sun.COM if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) { 157610922SJeff.Bonwick@Sun.COM error = zil_read_log_data(zilog, (lr_write_t *)lr, 157710922SJeff.Bonwick@Sun.COM zr->zr_lr + reclen); 157810922SJeff.Bonwick@Sun.COM if (error) 157910922SJeff.Bonwick@Sun.COM return (zil_replay_error(zilog, lr, error)); 158010922SJeff.Bonwick@Sun.COM } 1581789Sahrens 1582789Sahrens /* 1583789Sahrens * The log block containing this lr may have been byteswapped 1584789Sahrens * so that we can easily examine common fields like lrc_txtype. 158510922SJeff.Bonwick@Sun.COM * However, the log is a mix of different record types, and only the 1586789Sahrens * replay vectors know how to byteswap their records. Therefore, if 1587789Sahrens * the lr was byteswapped, undo it before invoking the replay vector. 1588789Sahrens */ 1589789Sahrens if (zr->zr_byteswap) 159010922SJeff.Bonwick@Sun.COM byteswap_uint64_array(zr->zr_lr, reclen); 1591789Sahrens 1592789Sahrens /* 1593789Sahrens * We must now do two things atomically: replay this log record, 15948227SNeil.Perrin@Sun.COM * and update the log header sequence number to reflect the fact that 15958227SNeil.Perrin@Sun.COM * we did so. At the end of each replay function the sequence number 15968227SNeil.Perrin@Sun.COM * is updated if we are in replay mode. 1597789Sahrens */ 159810922SJeff.Bonwick@Sun.COM error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, zr->zr_byteswap); 159910922SJeff.Bonwick@Sun.COM if (error) { 16003063Sperrin /* 16013063Sperrin * The DMU's dnode layer doesn't see removes until the txg 16023063Sperrin * commits, so a subsequent claim can spuriously fail with 16038227SNeil.Perrin@Sun.COM * EEXIST. So if we receive any error we try syncing out 160410922SJeff.Bonwick@Sun.COM * any removes then retry the transaction. Note that we 160510922SJeff.Bonwick@Sun.COM * specify B_FALSE for byteswap now, so we don't do it twice. 16063063Sperrin */ 160710922SJeff.Bonwick@Sun.COM txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0); 160810922SJeff.Bonwick@Sun.COM error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, B_FALSE); 160910922SJeff.Bonwick@Sun.COM if (error) 161010922SJeff.Bonwick@Sun.COM return (zil_replay_error(zilog, lr, error)); 1611789Sahrens } 161210922SJeff.Bonwick@Sun.COM return (0); 16133063Sperrin } 1614789Sahrens 16153063Sperrin /* ARGSUSED */ 161610922SJeff.Bonwick@Sun.COM static int 16173063Sperrin zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg) 16183063Sperrin { 16193063Sperrin zilog->zl_replay_blks++; 162010922SJeff.Bonwick@Sun.COM 162110922SJeff.Bonwick@Sun.COM return (0); 1622789Sahrens } 1623789Sahrens 1624789Sahrens /* 16251362Sperrin * If this dataset has a non-empty intent log, replay it and destroy it. 1626789Sahrens */ 1627789Sahrens void 16288227SNeil.Perrin@Sun.COM zil_replay(objset_t *os, void *arg, zil_replay_func_t *replay_func[TX_MAX_TYPE]) 1629789Sahrens { 1630789Sahrens zilog_t *zilog = dmu_objset_zil(os); 16311807Sbonwick const zil_header_t *zh = zilog->zl_header; 16321807Sbonwick zil_replay_arg_t zr; 16331362Sperrin 16348989SNeil.Perrin@Sun.COM if ((zh->zh_flags & ZIL_REPLAY_NEEDED) == 0) { 16351807Sbonwick zil_destroy(zilog, B_TRUE); 16361362Sperrin return; 16371362Sperrin } 1638789Sahrens 1639789Sahrens zr.zr_replay = replay_func; 1640789Sahrens zr.zr_arg = arg; 16411807Sbonwick zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log); 164210922SJeff.Bonwick@Sun.COM zr.zr_lr = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP); 1643789Sahrens 1644789Sahrens /* 1645789Sahrens * Wait for in-progress removes to sync before starting replay. 1646789Sahrens */ 1647789Sahrens txg_wait_synced(zilog->zl_dmu_pool, 0); 1648789Sahrens 16498227SNeil.Perrin@Sun.COM zilog->zl_replay = B_TRUE; 165011066Srafael.vanoni@sun.com zilog->zl_replay_time = ddi_get_lbolt(); 16513063Sperrin ASSERT(zilog->zl_replay_blks == 0); 16523063Sperrin (void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr, 16531807Sbonwick zh->zh_claim_txg); 165410922SJeff.Bonwick@Sun.COM kmem_free(zr.zr_lr, 2 * SPA_MAXBLOCKSIZE); 1655789Sahrens 16561807Sbonwick zil_destroy(zilog, B_FALSE); 16575712Sahrens txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 16588227SNeil.Perrin@Sun.COM zilog->zl_replay = B_FALSE; 1659789Sahrens } 16601646Sperrin 166110922SJeff.Bonwick@Sun.COM boolean_t 166210922SJeff.Bonwick@Sun.COM zil_replaying(zilog_t *zilog, dmu_tx_t *tx) 16631646Sperrin { 166410922SJeff.Bonwick@Sun.COM if (zilog == NULL) 166510922SJeff.Bonwick@Sun.COM return (B_TRUE); 16661646Sperrin 166710922SJeff.Bonwick@Sun.COM if (zilog->zl_replay) { 166810922SJeff.Bonwick@Sun.COM dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 166910922SJeff.Bonwick@Sun.COM zilog->zl_replayed_seq[dmu_tx_get_txg(tx) & TXG_MASK] = 167010922SJeff.Bonwick@Sun.COM zilog->zl_replaying_seq; 167110922SJeff.Bonwick@Sun.COM return (B_TRUE); 16722638Sperrin } 16732638Sperrin 167410922SJeff.Bonwick@Sun.COM return (B_FALSE); 16751646Sperrin } 16769701SGeorge.Wilson@Sun.COM 16779701SGeorge.Wilson@Sun.COM /* ARGSUSED */ 16789701SGeorge.Wilson@Sun.COM int 1679*11209SMatthew.Ahrens@Sun.COM zil_vdev_offline(const char *osname, void *arg) 16809701SGeorge.Wilson@Sun.COM { 16819701SGeorge.Wilson@Sun.COM objset_t *os; 16829701SGeorge.Wilson@Sun.COM zilog_t *zilog; 16839701SGeorge.Wilson@Sun.COM int error; 16849701SGeorge.Wilson@Sun.COM 168510298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(osname, FTAG, &os); 16869701SGeorge.Wilson@Sun.COM if (error) 16879701SGeorge.Wilson@Sun.COM return (error); 16889701SGeorge.Wilson@Sun.COM 16899701SGeorge.Wilson@Sun.COM zilog = dmu_objset_zil(os); 16909701SGeorge.Wilson@Sun.COM if (zil_suspend(zilog) != 0) 16919701SGeorge.Wilson@Sun.COM error = EEXIST; 16929701SGeorge.Wilson@Sun.COM else 16939701SGeorge.Wilson@Sun.COM zil_resume(zilog); 169410298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 16959701SGeorge.Wilson@Sun.COM return (error); 16969701SGeorge.Wilson@Sun.COM } 1697