zil.c revision 209962
1168404Spjd/* 2168404Spjd * CDDL HEADER START 3168404Spjd * 4168404Spjd * The contents of this file are subject to the terms of the 5168404Spjd * Common Development and Distribution License (the "License"). 6168404Spjd * You may not use this file except in compliance with the License. 7168404Spjd * 8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9168404Spjd * or http://www.opensolaris.org/os/licensing. 10168404Spjd * See the License for the specific language governing permissions 11168404Spjd * and limitations under the License. 12168404Spjd * 13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each 14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15168404Spjd * If applicable, add the following below this CDDL HEADER, with the 16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying 17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner] 18168404Spjd * 19168404Spjd * CDDL HEADER END 20168404Spjd */ 21168404Spjd/* 22208050Smm * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23168404Spjd * Use is subject to license terms. 24168404Spjd */ 25168404Spjd 26168404Spjd#include <sys/zfs_context.h> 27168404Spjd#include <sys/spa.h> 28168404Spjd#include <sys/dmu.h> 29168404Spjd#include <sys/zap.h> 30168404Spjd#include <sys/arc.h> 31168404Spjd#include <sys/stat.h> 32168404Spjd#include <sys/resource.h> 33168404Spjd#include <sys/zil.h> 34168404Spjd#include <sys/zil_impl.h> 35168404Spjd#include <sys/dsl_dataset.h> 36168404Spjd#include <sys/vdev.h> 37168404Spjd#include <sys/dmu_tx.h> 38168404Spjd 39168404Spjd/* 40168404Spjd * The zfs intent log (ZIL) saves transaction records of system calls 41168404Spjd * that change the file system in memory with enough information 42168404Spjd * to be able to replay them. These are stored in memory until 43168404Spjd * either the DMU transaction group (txg) commits them to the stable pool 44168404Spjd * and they can be discarded, or they are flushed to the stable log 45168404Spjd * (also in the pool) due to a fsync, O_DSYNC or other synchronous 46168404Spjd * requirement. In the event of a panic or power fail then those log 47168404Spjd * records (transactions) are replayed. 48168404Spjd * 49168404Spjd * There is one ZIL per file system. Its on-disk (pool) format consists 50168404Spjd * of 3 parts: 51168404Spjd * 52168404Spjd * - ZIL header 53168404Spjd * - ZIL blocks 54168404Spjd * - ZIL records 55168404Spjd * 56168404Spjd * A log record holds a system call transaction. Log blocks can 57168404Spjd * hold many log records and the blocks are chained together. 58168404Spjd * Each ZIL block contains a block pointer (blkptr_t) to the next 59168404Spjd * ZIL block in the chain. The ZIL header points to the first 60168404Spjd * block in the chain. Note there is not a fixed place in the pool 61168404Spjd * to hold blocks. They are dynamically allocated and freed as 62168404Spjd * needed from the blocks available. Figure X shows the ZIL structure: 63168404Spjd */ 64168404Spjd 65168404Spjd/* 66168404Spjd * This global ZIL switch affects all pools 67168404Spjd */ 68168404Spjdint zil_disable = 0; /* disable intent logging */ 69168404SpjdSYSCTL_DECL(_vfs_zfs); 70168404SpjdTUNABLE_INT("vfs.zfs.zil_disable", &zil_disable); 71169028SpjdSYSCTL_INT(_vfs_zfs, OID_AUTO, zil_disable, CTLFLAG_RW, &zil_disable, 0, 72168404Spjd "Disable ZFS Intent Log (ZIL)"); 73168404Spjd 74168404Spjd/* 75168404Spjd * Tunable parameter for debugging or performance analysis. Setting 76168404Spjd * zfs_nocacheflush will cause corruption on power loss if a volatile 77168404Spjd * out-of-order write cache is enabled. 78168404Spjd */ 79168404Spjdboolean_t zfs_nocacheflush = B_FALSE; 80168404SpjdTUNABLE_INT("vfs.zfs.cache_flush_disable", &zfs_nocacheflush); 81168404SpjdSYSCTL_INT(_vfs_zfs, OID_AUTO, cache_flush_disable, CTLFLAG_RDTUN, 82168404Spjd &zfs_nocacheflush, 0, "Disable cache flush"); 83168404Spjd 84168404Spjdstatic kmem_cache_t *zil_lwb_cache; 85168404Spjd 86168404Spjdstatic int 87168404Spjdzil_dva_compare(const void *x1, const void *x2) 88168404Spjd{ 89168404Spjd const dva_t *dva1 = x1; 90168404Spjd const dva_t *dva2 = x2; 91168404Spjd 92168404Spjd if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2)) 93168404Spjd return (-1); 94168404Spjd if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2)) 95168404Spjd return (1); 96168404Spjd 97168404Spjd if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2)) 98168404Spjd return (-1); 99168404Spjd if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2)) 100168404Spjd return (1); 101168404Spjd 102168404Spjd return (0); 103168404Spjd} 104168404Spjd 105168404Spjdstatic void 106168404Spjdzil_dva_tree_init(avl_tree_t *t) 107168404Spjd{ 108168404Spjd avl_create(t, zil_dva_compare, sizeof (zil_dva_node_t), 109168404Spjd offsetof(zil_dva_node_t, zn_node)); 110168404Spjd} 111168404Spjd 112168404Spjdstatic void 113168404Spjdzil_dva_tree_fini(avl_tree_t *t) 114168404Spjd{ 115168404Spjd zil_dva_node_t *zn; 116168404Spjd void *cookie = NULL; 117168404Spjd 118168404Spjd while ((zn = avl_destroy_nodes(t, &cookie)) != NULL) 119168404Spjd kmem_free(zn, sizeof (zil_dva_node_t)); 120168404Spjd 121168404Spjd avl_destroy(t); 122168404Spjd} 123168404Spjd 124168404Spjdstatic int 125168404Spjdzil_dva_tree_add(avl_tree_t *t, dva_t *dva) 126168404Spjd{ 127168404Spjd zil_dva_node_t *zn; 128168404Spjd avl_index_t where; 129168404Spjd 130168404Spjd if (avl_find(t, dva, &where) != NULL) 131168404Spjd return (EEXIST); 132168404Spjd 133168404Spjd zn = kmem_alloc(sizeof (zil_dva_node_t), KM_SLEEP); 134168404Spjd zn->zn_dva = *dva; 135168404Spjd avl_insert(t, zn, where); 136168404Spjd 137168404Spjd return (0); 138168404Spjd} 139168404Spjd 140168404Spjdstatic zil_header_t * 141168404Spjdzil_header_in_syncing_context(zilog_t *zilog) 142168404Spjd{ 143168404Spjd return ((zil_header_t *)zilog->zl_header); 144168404Spjd} 145168404Spjd 146168404Spjdstatic void 147168404Spjdzil_init_log_chain(zilog_t *zilog, blkptr_t *bp) 148168404Spjd{ 149168404Spjd zio_cksum_t *zc = &bp->blk_cksum; 150168404Spjd 151168404Spjd zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL); 152168404Spjd zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL); 153168404Spjd zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os); 154168404Spjd zc->zc_word[ZIL_ZC_SEQ] = 1ULL; 155168404Spjd} 156168404Spjd 157168404Spjd/* 158168404Spjd * Read a log block, make sure it's valid, and byteswap it if necessary. 159168404Spjd */ 160168404Spjdstatic int 161168404Spjdzil_read_log_block(zilog_t *zilog, const blkptr_t *bp, arc_buf_t **abufpp) 162168404Spjd{ 163168404Spjd blkptr_t blk = *bp; 164168404Spjd zbookmark_t zb; 165168404Spjd uint32_t aflags = ARC_WAIT; 166168404Spjd int error; 167168404Spjd 168168404Spjd zb.zb_objset = bp->blk_cksum.zc_word[ZIL_ZC_OBJSET]; 169168404Spjd zb.zb_object = 0; 170168404Spjd zb.zb_level = -1; 171168404Spjd zb.zb_blkid = bp->blk_cksum.zc_word[ZIL_ZC_SEQ]; 172168404Spjd 173168404Spjd *abufpp = NULL; 174168404Spjd 175185029Spjd /* 176185029Spjd * We shouldn't be doing any scrubbing while we're doing log 177185029Spjd * replay, it's OK to not lock. 178185029Spjd */ 179185029Spjd error = arc_read_nolock(NULL, zilog->zl_spa, &blk, 180168404Spjd arc_getbuf_func, abufpp, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL | 181168404Spjd ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB, &aflags, &zb); 182168404Spjd 183168404Spjd if (error == 0) { 184168404Spjd char *data = (*abufpp)->b_data; 185168404Spjd uint64_t blksz = BP_GET_LSIZE(bp); 186168404Spjd zil_trailer_t *ztp = (zil_trailer_t *)(data + blksz) - 1; 187168404Spjd zio_cksum_t cksum = bp->blk_cksum; 188168404Spjd 189168404Spjd /* 190185029Spjd * Validate the checksummed log block. 191185029Spjd * 192168404Spjd * Sequence numbers should be... sequential. The checksum 193168404Spjd * verifier for the next block should be bp's checksum plus 1. 194185029Spjd * 195185029Spjd * Also check the log chain linkage and size used. 196168404Spjd */ 197168404Spjd cksum.zc_word[ZIL_ZC_SEQ]++; 198168404Spjd 199185029Spjd if (bcmp(&cksum, &ztp->zit_next_blk.blk_cksum, 200185029Spjd sizeof (cksum)) || BP_IS_HOLE(&ztp->zit_next_blk) || 201185029Spjd (ztp->zit_nused > (blksz - sizeof (zil_trailer_t)))) { 202185029Spjd error = ECKSUM; 203185029Spjd } 204168404Spjd 205168404Spjd if (error) { 206168404Spjd VERIFY(arc_buf_remove_ref(*abufpp, abufpp) == 1); 207168404Spjd *abufpp = NULL; 208168404Spjd } 209168404Spjd } 210168404Spjd 211168404Spjd dprintf("error %d on %llu:%llu\n", error, zb.zb_objset, zb.zb_blkid); 212168404Spjd 213168404Spjd return (error); 214168404Spjd} 215168404Spjd 216168404Spjd/* 217168404Spjd * Parse the intent log, and call parse_func for each valid record within. 218168404Spjd * Return the highest sequence number. 219168404Spjd */ 220168404Spjduint64_t 221168404Spjdzil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func, 222168404Spjd zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg) 223168404Spjd{ 224168404Spjd const zil_header_t *zh = zilog->zl_header; 225168404Spjd uint64_t claim_seq = zh->zh_claim_seq; 226168404Spjd uint64_t seq = 0; 227168404Spjd uint64_t max_seq = 0; 228168404Spjd blkptr_t blk = zh->zh_log; 229168404Spjd arc_buf_t *abuf; 230168404Spjd char *lrbuf, *lrp; 231168404Spjd zil_trailer_t *ztp; 232168404Spjd int reclen, error; 233168404Spjd 234168404Spjd if (BP_IS_HOLE(&blk)) 235168404Spjd return (max_seq); 236168404Spjd 237168404Spjd /* 238168404Spjd * Starting at the block pointed to by zh_log we read the log chain. 239168404Spjd * For each block in the chain we strongly check that block to 240168404Spjd * ensure its validity. We stop when an invalid block is found. 241168404Spjd * For each block pointer in the chain we call parse_blk_func(). 242168404Spjd * For each record in each valid block we call parse_lr_func(). 243168404Spjd * If the log has been claimed, stop if we encounter a sequence 244168404Spjd * number greater than the highest claimed sequence number. 245168404Spjd */ 246168404Spjd zil_dva_tree_init(&zilog->zl_dva_tree); 247168404Spjd for (;;) { 248168404Spjd seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 249168404Spjd 250168404Spjd if (claim_seq != 0 && seq > claim_seq) 251168404Spjd break; 252168404Spjd 253168404Spjd ASSERT(max_seq < seq); 254168404Spjd max_seq = seq; 255168404Spjd 256168404Spjd error = zil_read_log_block(zilog, &blk, &abuf); 257168404Spjd 258168404Spjd if (parse_blk_func != NULL) 259168404Spjd parse_blk_func(zilog, &blk, arg, txg); 260168404Spjd 261168404Spjd if (error) 262168404Spjd break; 263168404Spjd 264168404Spjd lrbuf = abuf->b_data; 265168404Spjd ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1; 266168404Spjd blk = ztp->zit_next_blk; 267168404Spjd 268168404Spjd if (parse_lr_func == NULL) { 269168404Spjd VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 270168404Spjd continue; 271168404Spjd } 272168404Spjd 273168404Spjd for (lrp = lrbuf; lrp < lrbuf + ztp->zit_nused; lrp += reclen) { 274168404Spjd lr_t *lr = (lr_t *)lrp; 275168404Spjd reclen = lr->lrc_reclen; 276168404Spjd ASSERT3U(reclen, >=, sizeof (lr_t)); 277168404Spjd parse_lr_func(zilog, lr, arg, txg); 278168404Spjd } 279168404Spjd VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 280168404Spjd } 281168404Spjd zil_dva_tree_fini(&zilog->zl_dva_tree); 282168404Spjd 283168404Spjd return (max_seq); 284168404Spjd} 285168404Spjd 286168404Spjd/* ARGSUSED */ 287168404Spjdstatic void 288168404Spjdzil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg) 289168404Spjd{ 290168404Spjd spa_t *spa = zilog->zl_spa; 291168404Spjd int err; 292168404Spjd 293168404Spjd /* 294168404Spjd * Claim log block if not already committed and not already claimed. 295168404Spjd */ 296168404Spjd if (bp->blk_birth >= first_txg && 297168404Spjd zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp)) == 0) { 298185029Spjd err = zio_wait(zio_claim(NULL, spa, first_txg, bp, NULL, NULL, 299185029Spjd ZIO_FLAG_MUSTSUCCEED)); 300168404Spjd ASSERT(err == 0); 301168404Spjd } 302168404Spjd} 303168404Spjd 304168404Spjdstatic void 305168404Spjdzil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg) 306168404Spjd{ 307168404Spjd if (lrc->lrc_txtype == TX_WRITE) { 308168404Spjd lr_write_t *lr = (lr_write_t *)lrc; 309168404Spjd zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg); 310168404Spjd } 311168404Spjd} 312168404Spjd 313168404Spjd/* ARGSUSED */ 314168404Spjdstatic void 315168404Spjdzil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg) 316168404Spjd{ 317168404Spjd zio_free_blk(zilog->zl_spa, bp, dmu_tx_get_txg(tx)); 318168404Spjd} 319168404Spjd 320168404Spjdstatic void 321168404Spjdzil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg) 322168404Spjd{ 323168404Spjd /* 324168404Spjd * If we previously claimed it, we need to free it. 325168404Spjd */ 326168404Spjd if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE) { 327168404Spjd lr_write_t *lr = (lr_write_t *)lrc; 328168404Spjd blkptr_t *bp = &lr->lr_blkptr; 329168404Spjd if (bp->blk_birth >= claim_txg && 330168404Spjd !zil_dva_tree_add(&zilog->zl_dva_tree, BP_IDENTITY(bp))) { 331168404Spjd (void) arc_free(NULL, zilog->zl_spa, 332168404Spjd dmu_tx_get_txg(tx), bp, NULL, NULL, ARC_WAIT); 333168404Spjd } 334168404Spjd } 335168404Spjd} 336168404Spjd 337168404Spjd/* 338168404Spjd * Create an on-disk intent log. 339168404Spjd */ 340168404Spjdstatic void 341168404Spjdzil_create(zilog_t *zilog) 342168404Spjd{ 343168404Spjd const zil_header_t *zh = zilog->zl_header; 344168404Spjd lwb_t *lwb; 345168404Spjd uint64_t txg = 0; 346168404Spjd dmu_tx_t *tx = NULL; 347168404Spjd blkptr_t blk; 348168404Spjd int error = 0; 349168404Spjd 350168404Spjd /* 351168404Spjd * Wait for any previous destroy to complete. 352168404Spjd */ 353168404Spjd txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 354168404Spjd 355168404Spjd ASSERT(zh->zh_claim_txg == 0); 356168404Spjd ASSERT(zh->zh_replay_seq == 0); 357168404Spjd 358168404Spjd blk = zh->zh_log; 359168404Spjd 360168404Spjd /* 361207908Smm * If we don't already have an initial log block or we have one 362207908Smm * but it's the wrong endianness then allocate one. 363168404Spjd */ 364207908Smm if (BP_IS_HOLE(&blk) || BP_SHOULD_BYTESWAP(&blk)) { 365168404Spjd tx = dmu_tx_create(zilog->zl_os); 366168404Spjd (void) dmu_tx_assign(tx, TXG_WAIT); 367168404Spjd dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 368168404Spjd txg = dmu_tx_get_txg(tx); 369168404Spjd 370207908Smm if (!BP_IS_HOLE(&blk)) { 371207908Smm zio_free_blk(zilog->zl_spa, &blk, txg); 372207908Smm BP_ZERO(&blk); 373207908Smm } 374207908Smm 375168404Spjd error = zio_alloc_blk(zilog->zl_spa, ZIL_MIN_BLKSZ, &blk, 376168404Spjd NULL, txg); 377168404Spjd 378168404Spjd if (error == 0) 379168404Spjd zil_init_log_chain(zilog, &blk); 380168404Spjd } 381168404Spjd 382168404Spjd /* 383168404Spjd * Allocate a log write buffer (lwb) for the first log block. 384168404Spjd */ 385168404Spjd if (error == 0) { 386168404Spjd lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 387168404Spjd lwb->lwb_zilog = zilog; 388168404Spjd lwb->lwb_blk = blk; 389168404Spjd lwb->lwb_nused = 0; 390168404Spjd lwb->lwb_sz = BP_GET_LSIZE(&lwb->lwb_blk); 391168404Spjd lwb->lwb_buf = zio_buf_alloc(lwb->lwb_sz); 392168404Spjd lwb->lwb_max_txg = txg; 393168404Spjd lwb->lwb_zio = NULL; 394168404Spjd 395168404Spjd mutex_enter(&zilog->zl_lock); 396168404Spjd list_insert_tail(&zilog->zl_lwb_list, lwb); 397168404Spjd mutex_exit(&zilog->zl_lock); 398168404Spjd } 399168404Spjd 400168404Spjd /* 401168404Spjd * If we just allocated the first log block, commit our transaction 402168404Spjd * and wait for zil_sync() to stuff the block poiner into zh_log. 403168404Spjd * (zh is part of the MOS, so we cannot modify it in open context.) 404168404Spjd */ 405168404Spjd if (tx != NULL) { 406168404Spjd dmu_tx_commit(tx); 407168404Spjd txg_wait_synced(zilog->zl_dmu_pool, txg); 408168404Spjd } 409168404Spjd 410168404Spjd ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0); 411168404Spjd} 412168404Spjd 413168404Spjd/* 414168404Spjd * In one tx, free all log blocks and clear the log header. 415168404Spjd * If keep_first is set, then we're replaying a log with no content. 416168404Spjd * We want to keep the first block, however, so that the first 417168404Spjd * synchronous transaction doesn't require a txg_wait_synced() 418168404Spjd * in zil_create(). We don't need to txg_wait_synced() here either 419168404Spjd * when keep_first is set, because both zil_create() and zil_destroy() 420168404Spjd * will wait for any in-progress destroys to complete. 421168404Spjd */ 422168404Spjdvoid 423168404Spjdzil_destroy(zilog_t *zilog, boolean_t keep_first) 424168404Spjd{ 425168404Spjd const zil_header_t *zh = zilog->zl_header; 426168404Spjd lwb_t *lwb; 427168404Spjd dmu_tx_t *tx; 428168404Spjd uint64_t txg; 429168404Spjd 430168404Spjd /* 431168404Spjd * Wait for any previous destroy to complete. 432168404Spjd */ 433168404Spjd txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 434168404Spjd 435168404Spjd if (BP_IS_HOLE(&zh->zh_log)) 436168404Spjd return; 437168404Spjd 438168404Spjd tx = dmu_tx_create(zilog->zl_os); 439168404Spjd (void) dmu_tx_assign(tx, TXG_WAIT); 440168404Spjd dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 441168404Spjd txg = dmu_tx_get_txg(tx); 442168404Spjd 443168404Spjd mutex_enter(&zilog->zl_lock); 444168404Spjd 445185029Spjd /* 446185029Spjd * It is possible for the ZIL to get the previously mounted zilog 447185029Spjd * structure of the same dataset if quickly remounted and the dbuf 448185029Spjd * eviction has not completed. In this case we can see a non 449185029Spjd * empty lwb list and keep_first will be set. We fix this by 450185029Spjd * clearing the keep_first. This will be slower but it's very rare. 451185029Spjd */ 452185029Spjd if (!list_is_empty(&zilog->zl_lwb_list) && keep_first) 453185029Spjd keep_first = B_FALSE; 454185029Spjd 455168404Spjd ASSERT3U(zilog->zl_destroy_txg, <, txg); 456168404Spjd zilog->zl_destroy_txg = txg; 457168404Spjd zilog->zl_keep_first = keep_first; 458168404Spjd 459168404Spjd if (!list_is_empty(&zilog->zl_lwb_list)) { 460168404Spjd ASSERT(zh->zh_claim_txg == 0); 461168404Spjd ASSERT(!keep_first); 462168404Spjd while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 463168404Spjd list_remove(&zilog->zl_lwb_list, lwb); 464168404Spjd if (lwb->lwb_buf != NULL) 465168404Spjd zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 466168404Spjd zio_free_blk(zilog->zl_spa, &lwb->lwb_blk, txg); 467168404Spjd kmem_cache_free(zil_lwb_cache, lwb); 468168404Spjd } 469168404Spjd } else { 470168404Spjd if (!keep_first) { 471168404Spjd (void) zil_parse(zilog, zil_free_log_block, 472168404Spjd zil_free_log_record, tx, zh->zh_claim_txg); 473168404Spjd } 474168404Spjd } 475168404Spjd mutex_exit(&zilog->zl_lock); 476168404Spjd 477168404Spjd dmu_tx_commit(tx); 478185029Spjd} 479168404Spjd 480185029Spjd/* 481200724Sdelphij * return true if the initial log block is not valid 482200724Sdelphij */ 483200724Sdelphijstatic boolean_t 484200724Sdelphijzil_empty(zilog_t *zilog) 485200724Sdelphij{ 486200724Sdelphij const zil_header_t *zh = zilog->zl_header; 487200724Sdelphij arc_buf_t *abuf = NULL; 488200724Sdelphij 489200724Sdelphij if (BP_IS_HOLE(&zh->zh_log)) 490200724Sdelphij return (B_TRUE); 491200724Sdelphij 492200724Sdelphij if (zil_read_log_block(zilog, &zh->zh_log, &abuf) != 0) 493200724Sdelphij return (B_TRUE); 494200724Sdelphij 495200724Sdelphij VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 496200724Sdelphij return (B_FALSE); 497200724Sdelphij} 498200724Sdelphij 499168404Spjdint 500168404Spjdzil_claim(char *osname, void *txarg) 501168404Spjd{ 502168404Spjd dmu_tx_t *tx = txarg; 503168404Spjd uint64_t first_txg = dmu_tx_get_txg(tx); 504168404Spjd zilog_t *zilog; 505168404Spjd zil_header_t *zh; 506168404Spjd objset_t *os; 507168404Spjd int error; 508168404Spjd 509185029Spjd error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_USER, &os); 510168404Spjd if (error) { 511185029Spjd cmn_err(CE_WARN, "can't open objset for %s", osname); 512168404Spjd return (0); 513168404Spjd } 514168404Spjd 515168404Spjd zilog = dmu_objset_zil(os); 516168404Spjd zh = zil_header_in_syncing_context(zilog); 517168404Spjd 518168404Spjd /* 519200724Sdelphij * Record here whether the zil has any records to replay. 520200724Sdelphij * If the header block pointer is null or the block points 521200724Sdelphij * to the stubby then we know there are no valid log records. 522200724Sdelphij * We use the header to store this state as the the zilog gets 523200724Sdelphij * freed later in dmu_objset_close(). 524200724Sdelphij * The flags (and the rest of the header fields) are cleared in 525200724Sdelphij * zil_sync() as a result of a zil_destroy(), after replaying the log. 526200724Sdelphij * 527200724Sdelphij * Note, the intent log can be empty but still need the 528200724Sdelphij * stubby to be claimed. 529200724Sdelphij */ 530200724Sdelphij if (!zil_empty(zilog)) 531200724Sdelphij zh->zh_flags |= ZIL_REPLAY_NEEDED; 532200724Sdelphij 533200724Sdelphij /* 534168404Spjd * Claim all log blocks if we haven't already done so, and remember 535168404Spjd * the highest claimed sequence number. This ensures that if we can 536168404Spjd * read only part of the log now (e.g. due to a missing device), 537168404Spjd * but we can read the entire log later, we will not try to replay 538168404Spjd * or destroy beyond the last block we successfully claimed. 539168404Spjd */ 540168404Spjd ASSERT3U(zh->zh_claim_txg, <=, first_txg); 541168404Spjd if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) { 542168404Spjd zh->zh_claim_txg = first_txg; 543168404Spjd zh->zh_claim_seq = zil_parse(zilog, zil_claim_log_block, 544168404Spjd zil_claim_log_record, tx, first_txg); 545168404Spjd dsl_dataset_dirty(dmu_objset_ds(os), tx); 546168404Spjd } 547168404Spjd 548168404Spjd ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1)); 549168404Spjd dmu_objset_close(os); 550168404Spjd return (0); 551168404Spjd} 552168404Spjd 553185029Spjd/* 554185029Spjd * Check the log by walking the log chain. 555185029Spjd * Checksum errors are ok as they indicate the end of the chain. 556185029Spjd * Any other error (no device or read failure) returns an error. 557185029Spjd */ 558185029Spjd/* ARGSUSED */ 559185029Spjdint 560185029Spjdzil_check_log_chain(char *osname, void *txarg) 561168404Spjd{ 562185029Spjd zilog_t *zilog; 563185029Spjd zil_header_t *zh; 564185029Spjd blkptr_t blk; 565185029Spjd arc_buf_t *abuf; 566185029Spjd objset_t *os; 567185029Spjd char *lrbuf; 568185029Spjd zil_trailer_t *ztp; 569185029Spjd int error; 570168404Spjd 571185029Spjd error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_USER, &os); 572185029Spjd if (error) { 573185029Spjd cmn_err(CE_WARN, "can't open objset for %s", osname); 574185029Spjd return (0); 575185029Spjd } 576168404Spjd 577185029Spjd zilog = dmu_objset_zil(os); 578185029Spjd zh = zil_header_in_syncing_context(zilog); 579185029Spjd blk = zh->zh_log; 580185029Spjd if (BP_IS_HOLE(&blk)) { 581185029Spjd dmu_objset_close(os); 582185029Spjd return (0); /* no chain */ 583168404Spjd } 584185029Spjd 585185029Spjd for (;;) { 586185029Spjd error = zil_read_log_block(zilog, &blk, &abuf); 587185029Spjd if (error) 588185029Spjd break; 589185029Spjd lrbuf = abuf->b_data; 590185029Spjd ztp = (zil_trailer_t *)(lrbuf + BP_GET_LSIZE(&blk)) - 1; 591185029Spjd blk = ztp->zit_next_blk; 592185029Spjd VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 593185029Spjd } 594185029Spjd dmu_objset_close(os); 595185029Spjd if (error == ECKSUM) 596185029Spjd return (0); /* normal end of chain */ 597185029Spjd return (error); 598168404Spjd} 599168404Spjd 600185029Spjd/* 601185029Spjd * Clear a log chain 602185029Spjd */ 603185029Spjd/* ARGSUSED */ 604185029Spjdint 605185029Spjdzil_clear_log_chain(char *osname, void *txarg) 606185029Spjd{ 607185029Spjd zilog_t *zilog; 608185029Spjd zil_header_t *zh; 609185029Spjd objset_t *os; 610185029Spjd dmu_tx_t *tx; 611185029Spjd int error; 612185029Spjd 613185029Spjd error = dmu_objset_open(osname, DMU_OST_ANY, DS_MODE_USER, &os); 614185029Spjd if (error) { 615185029Spjd cmn_err(CE_WARN, "can't open objset for %s", osname); 616185029Spjd return (0); 617185029Spjd } 618185029Spjd 619185029Spjd zilog = dmu_objset_zil(os); 620185029Spjd tx = dmu_tx_create(zilog->zl_os); 621185029Spjd (void) dmu_tx_assign(tx, TXG_WAIT); 622185029Spjd zh = zil_header_in_syncing_context(zilog); 623185029Spjd BP_ZERO(&zh->zh_log); 624185029Spjd dsl_dataset_dirty(dmu_objset_ds(os), tx); 625185029Spjd dmu_tx_commit(tx); 626185029Spjd dmu_objset_close(os); 627185029Spjd return (0); 628185029Spjd} 629185029Spjd 630185029Spjdstatic int 631185029Spjdzil_vdev_compare(const void *x1, const void *x2) 632185029Spjd{ 633185029Spjd uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev; 634185029Spjd uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev; 635185029Spjd 636185029Spjd if (v1 < v2) 637185029Spjd return (-1); 638185029Spjd if (v1 > v2) 639185029Spjd return (1); 640185029Spjd 641185029Spjd return (0); 642185029Spjd} 643185029Spjd 644168404Spjdvoid 645185029Spjdzil_add_block(zilog_t *zilog, blkptr_t *bp) 646168404Spjd{ 647185029Spjd avl_tree_t *t = &zilog->zl_vdev_tree; 648185029Spjd avl_index_t where; 649185029Spjd zil_vdev_node_t *zv, zvsearch; 650185029Spjd int ndvas = BP_GET_NDVAS(bp); 651185029Spjd int i; 652168404Spjd 653185029Spjd if (zfs_nocacheflush) 654185029Spjd return; 655168404Spjd 656185029Spjd ASSERT(zilog->zl_writer); 657168404Spjd 658185029Spjd /* 659185029Spjd * Even though we're zl_writer, we still need a lock because the 660185029Spjd * zl_get_data() callbacks may have dmu_sync() done callbacks 661185029Spjd * that will run concurrently. 662185029Spjd */ 663185029Spjd mutex_enter(&zilog->zl_vdev_lock); 664185029Spjd for (i = 0; i < ndvas; i++) { 665185029Spjd zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]); 666185029Spjd if (avl_find(t, &zvsearch, &where) == NULL) { 667185029Spjd zv = kmem_alloc(sizeof (*zv), KM_SLEEP); 668185029Spjd zv->zv_vdev = zvsearch.zv_vdev; 669185029Spjd avl_insert(t, zv, where); 670185029Spjd } 671185029Spjd } 672185029Spjd mutex_exit(&zilog->zl_vdev_lock); 673168404Spjd} 674168404Spjd 675168404Spjdvoid 676168404Spjdzil_flush_vdevs(zilog_t *zilog) 677168404Spjd{ 678168404Spjd spa_t *spa = zilog->zl_spa; 679185029Spjd avl_tree_t *t = &zilog->zl_vdev_tree; 680185029Spjd void *cookie = NULL; 681185029Spjd zil_vdev_node_t *zv; 682185029Spjd zio_t *zio; 683168404Spjd 684168404Spjd ASSERT(zilog->zl_writer); 685168404Spjd 686185029Spjd /* 687185029Spjd * We don't need zl_vdev_lock here because we're the zl_writer, 688185029Spjd * and all zl_get_data() callbacks are done. 689185029Spjd */ 690185029Spjd if (avl_numnodes(t) == 0) 691185029Spjd return; 692185029Spjd 693185029Spjd spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 694185029Spjd 695185029Spjd zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); 696185029Spjd 697185029Spjd while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) { 698185029Spjd vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev); 699185029Spjd if (vd != NULL) 700185029Spjd zio_flush(zio, vd); 701185029Spjd kmem_free(zv, sizeof (*zv)); 702168404Spjd } 703168404Spjd 704168404Spjd /* 705168404Spjd * Wait for all the flushes to complete. Not all devices actually 706168404Spjd * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails. 707168404Spjd */ 708185029Spjd (void) zio_wait(zio); 709185029Spjd 710185029Spjd spa_config_exit(spa, SCL_STATE, FTAG); 711168404Spjd} 712168404Spjd 713168404Spjd/* 714168404Spjd * Function called when a log block write completes 715168404Spjd */ 716168404Spjdstatic void 717168404Spjdzil_lwb_write_done(zio_t *zio) 718168404Spjd{ 719168404Spjd lwb_t *lwb = zio->io_private; 720168404Spjd zilog_t *zilog = lwb->lwb_zilog; 721168404Spjd 722185029Spjd ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF); 723185029Spjd ASSERT(BP_GET_CHECKSUM(zio->io_bp) == ZIO_CHECKSUM_ZILOG); 724185029Spjd ASSERT(BP_GET_TYPE(zio->io_bp) == DMU_OT_INTENT_LOG); 725185029Spjd ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 726185029Spjd ASSERT(BP_GET_BYTEORDER(zio->io_bp) == ZFS_HOST_BYTEORDER); 727185029Spjd ASSERT(!BP_IS_GANG(zio->io_bp)); 728185029Spjd ASSERT(!BP_IS_HOLE(zio->io_bp)); 729185029Spjd ASSERT(zio->io_bp->blk_fill == 0); 730185029Spjd 731168404Spjd /* 732209962Smm * Ensure the lwb buffer pointer is cleared before releasing 733209962Smm * the txg. If we have had an allocation failure and 734209962Smm * the txg is waiting to sync then we want want zil_sync() 735209962Smm * to remove the lwb so that it's not picked up as the next new 736209962Smm * one in zil_commit_writer(). zil_sync() will only remove 737209962Smm * the lwb if lwb_buf is null. 738168404Spjd */ 739168404Spjd zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 740168404Spjd mutex_enter(&zilog->zl_lock); 741168404Spjd lwb->lwb_buf = NULL; 742185029Spjd if (zio->io_error) 743168404Spjd zilog->zl_log_error = B_TRUE; 744209962Smm 745209962Smm /* 746209962Smm * Now that we've written this log block, we have a stable pointer 747209962Smm * to the next block in the chain, so it's OK to let the txg in 748209962Smm * which we allocated the next block sync. We still have the 749209962Smm * zl_lock to ensure zil_sync doesn't kmem free the lwb. 750209962Smm */ 751209962Smm txg_rele_to_sync(&lwb->lwb_txgh); 752168404Spjd mutex_exit(&zilog->zl_lock); 753168404Spjd} 754168404Spjd 755168404Spjd/* 756168404Spjd * Initialize the io for a log block. 757168404Spjd */ 758168404Spjdstatic void 759168404Spjdzil_lwb_write_init(zilog_t *zilog, lwb_t *lwb) 760168404Spjd{ 761168404Spjd zbookmark_t zb; 762168404Spjd 763168404Spjd zb.zb_objset = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET]; 764168404Spjd zb.zb_object = 0; 765168404Spjd zb.zb_level = -1; 766168404Spjd zb.zb_blkid = lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 767168404Spjd 768168404Spjd if (zilog->zl_root_zio == NULL) { 769168404Spjd zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL, 770168404Spjd ZIO_FLAG_CANFAIL); 771168404Spjd } 772168404Spjd if (lwb->lwb_zio == NULL) { 773168404Spjd lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa, 774185029Spjd 0, &lwb->lwb_blk, lwb->lwb_buf, 775168404Spjd lwb->lwb_sz, zil_lwb_write_done, lwb, 776185029Spjd ZIO_PRIORITY_LOG_WRITE, ZIO_FLAG_CANFAIL, &zb); 777168404Spjd } 778168404Spjd} 779168404Spjd 780168404Spjd/* 781168404Spjd * Start a log block write and advance to the next log block. 782168404Spjd * Calls are serialized. 783168404Spjd */ 784168404Spjdstatic lwb_t * 785168404Spjdzil_lwb_write_start(zilog_t *zilog, lwb_t *lwb) 786168404Spjd{ 787168404Spjd lwb_t *nlwb; 788168404Spjd zil_trailer_t *ztp = (zil_trailer_t *)(lwb->lwb_buf + lwb->lwb_sz) - 1; 789168404Spjd spa_t *spa = zilog->zl_spa; 790168404Spjd blkptr_t *bp = &ztp->zit_next_blk; 791168404Spjd uint64_t txg; 792168404Spjd uint64_t zil_blksz; 793168404Spjd int error; 794168404Spjd 795168404Spjd ASSERT(lwb->lwb_nused <= ZIL_BLK_DATA_SZ(lwb)); 796168404Spjd 797168404Spjd /* 798168404Spjd * Allocate the next block and save its address in this block 799168404Spjd * before writing it in order to establish the log chain. 800168404Spjd * Note that if the allocation of nlwb synced before we wrote 801168404Spjd * the block that points at it (lwb), we'd leak it if we crashed. 802168404Spjd * Therefore, we don't do txg_rele_to_sync() until zil_lwb_write_done(). 803168404Spjd */ 804168404Spjd txg = txg_hold_open(zilog->zl_dmu_pool, &lwb->lwb_txgh); 805168404Spjd txg_rele_to_quiesce(&lwb->lwb_txgh); 806168404Spjd 807168404Spjd /* 808168404Spjd * Pick a ZIL blocksize. We request a size that is the 809168404Spjd * maximum of the previous used size, the current used size and 810168404Spjd * the amount waiting in the queue. 811168404Spjd */ 812168404Spjd zil_blksz = MAX(zilog->zl_prev_used, 813168404Spjd zilog->zl_cur_used + sizeof (*ztp)); 814168404Spjd zil_blksz = MAX(zil_blksz, zilog->zl_itx_list_sz + sizeof (*ztp)); 815168404Spjd zil_blksz = P2ROUNDUP_TYPED(zil_blksz, ZIL_MIN_BLKSZ, uint64_t); 816168404Spjd if (zil_blksz > ZIL_MAX_BLKSZ) 817168404Spjd zil_blksz = ZIL_MAX_BLKSZ; 818168404Spjd 819168404Spjd BP_ZERO(bp); 820168404Spjd /* pass the old blkptr in order to spread log blocks across devs */ 821168404Spjd error = zio_alloc_blk(spa, zil_blksz, bp, &lwb->lwb_blk, txg); 822168404Spjd if (error) { 823168404Spjd dmu_tx_t *tx = dmu_tx_create_assigned(zilog->zl_dmu_pool, txg); 824168404Spjd 825168404Spjd /* 826168404Spjd * We dirty the dataset to ensure that zil_sync() will 827168404Spjd * be called to remove this lwb from our zl_lwb_list. 828168404Spjd * Failing to do so, may leave an lwb with a NULL lwb_buf 829168404Spjd * hanging around on the zl_lwb_list. 830168404Spjd */ 831168404Spjd dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 832168404Spjd dmu_tx_commit(tx); 833168404Spjd 834168404Spjd /* 835168404Spjd * Since we've just experienced an allocation failure so we 836168404Spjd * terminate the current lwb and send it on its way. 837168404Spjd */ 838168404Spjd ztp->zit_pad = 0; 839168404Spjd ztp->zit_nused = lwb->lwb_nused; 840168404Spjd ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum; 841168404Spjd zio_nowait(lwb->lwb_zio); 842168404Spjd 843168404Spjd /* 844168404Spjd * By returning NULL the caller will call tx_wait_synced() 845168404Spjd */ 846168404Spjd return (NULL); 847168404Spjd } 848168404Spjd 849168404Spjd ASSERT3U(bp->blk_birth, ==, txg); 850168404Spjd ztp->zit_pad = 0; 851168404Spjd ztp->zit_nused = lwb->lwb_nused; 852168404Spjd ztp->zit_bt.zbt_cksum = lwb->lwb_blk.blk_cksum; 853168404Spjd bp->blk_cksum = lwb->lwb_blk.blk_cksum; 854168404Spjd bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++; 855168404Spjd 856168404Spjd /* 857168404Spjd * Allocate a new log write buffer (lwb). 858168404Spjd */ 859168404Spjd nlwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 860168404Spjd 861168404Spjd nlwb->lwb_zilog = zilog; 862168404Spjd nlwb->lwb_blk = *bp; 863168404Spjd nlwb->lwb_nused = 0; 864168404Spjd nlwb->lwb_sz = BP_GET_LSIZE(&nlwb->lwb_blk); 865168404Spjd nlwb->lwb_buf = zio_buf_alloc(nlwb->lwb_sz); 866168404Spjd nlwb->lwb_max_txg = txg; 867168404Spjd nlwb->lwb_zio = NULL; 868168404Spjd 869168404Spjd /* 870168404Spjd * Put new lwb at the end of the log chain 871168404Spjd */ 872168404Spjd mutex_enter(&zilog->zl_lock); 873168404Spjd list_insert_tail(&zilog->zl_lwb_list, nlwb); 874168404Spjd mutex_exit(&zilog->zl_lock); 875168404Spjd 876185029Spjd /* Record the block for later vdev flushing */ 877185029Spjd zil_add_block(zilog, &lwb->lwb_blk); 878168404Spjd 879168404Spjd /* 880168404Spjd * kick off the write for the old log block 881168404Spjd */ 882168404Spjd dprintf_bp(&lwb->lwb_blk, "lwb %p txg %llu: ", lwb, txg); 883168404Spjd ASSERT(lwb->lwb_zio); 884168404Spjd zio_nowait(lwb->lwb_zio); 885168404Spjd 886168404Spjd return (nlwb); 887168404Spjd} 888168404Spjd 889168404Spjdstatic lwb_t * 890168404Spjdzil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb) 891168404Spjd{ 892168404Spjd lr_t *lrc = &itx->itx_lr; /* common log record */ 893168404Spjd lr_write_t *lr = (lr_write_t *)lrc; 894168404Spjd uint64_t txg = lrc->lrc_txg; 895168404Spjd uint64_t reclen = lrc->lrc_reclen; 896168404Spjd uint64_t dlen; 897168404Spjd 898168404Spjd if (lwb == NULL) 899168404Spjd return (NULL); 900168404Spjd ASSERT(lwb->lwb_buf != NULL); 901168404Spjd 902168404Spjd if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY) 903168404Spjd dlen = P2ROUNDUP_TYPED( 904168404Spjd lr->lr_length, sizeof (uint64_t), uint64_t); 905168404Spjd else 906168404Spjd dlen = 0; 907168404Spjd 908168404Spjd zilog->zl_cur_used += (reclen + dlen); 909168404Spjd 910168404Spjd zil_lwb_write_init(zilog, lwb); 911168404Spjd 912168404Spjd /* 913168404Spjd * If this record won't fit in the current log block, start a new one. 914168404Spjd */ 915168404Spjd if (lwb->lwb_nused + reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) { 916168404Spjd lwb = zil_lwb_write_start(zilog, lwb); 917168404Spjd if (lwb == NULL) 918168404Spjd return (NULL); 919168404Spjd zil_lwb_write_init(zilog, lwb); 920168404Spjd ASSERT(lwb->lwb_nused == 0); 921168404Spjd if (reclen + dlen > ZIL_BLK_DATA_SZ(lwb)) { 922168404Spjd txg_wait_synced(zilog->zl_dmu_pool, txg); 923168404Spjd return (lwb); 924168404Spjd } 925168404Spjd } 926168404Spjd 927168404Spjd /* 928168404Spjd * Update the lrc_seq, to be log record sequence number. See zil.h 929168404Spjd * Then copy the record to the log buffer. 930168404Spjd */ 931168404Spjd lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */ 932168404Spjd bcopy(lrc, lwb->lwb_buf + lwb->lwb_nused, reclen); 933168404Spjd 934168404Spjd /* 935168404Spjd * If it's a write, fetch the data or get its blkptr as appropriate. 936168404Spjd */ 937168404Spjd if (lrc->lrc_txtype == TX_WRITE) { 938168404Spjd if (txg > spa_freeze_txg(zilog->zl_spa)) 939168404Spjd txg_wait_synced(zilog->zl_dmu_pool, txg); 940168404Spjd if (itx->itx_wr_state != WR_COPIED) { 941168404Spjd char *dbuf; 942168404Spjd int error; 943168404Spjd 944168404Spjd /* alignment is guaranteed */ 945168404Spjd lr = (lr_write_t *)(lwb->lwb_buf + lwb->lwb_nused); 946168404Spjd if (dlen) { 947168404Spjd ASSERT(itx->itx_wr_state == WR_NEED_COPY); 948168404Spjd dbuf = lwb->lwb_buf + lwb->lwb_nused + reclen; 949168404Spjd lr->lr_common.lrc_reclen += dlen; 950168404Spjd } else { 951168404Spjd ASSERT(itx->itx_wr_state == WR_INDIRECT); 952168404Spjd dbuf = NULL; 953168404Spjd } 954168404Spjd error = zilog->zl_get_data( 955168404Spjd itx->itx_private, lr, dbuf, lwb->lwb_zio); 956168404Spjd if (error) { 957168404Spjd ASSERT(error == ENOENT || error == EEXIST || 958168404Spjd error == EALREADY); 959168404Spjd return (lwb); 960168404Spjd } 961168404Spjd } 962168404Spjd } 963168404Spjd 964168404Spjd lwb->lwb_nused += reclen + dlen; 965168404Spjd lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg); 966168404Spjd ASSERT3U(lwb->lwb_nused, <=, ZIL_BLK_DATA_SZ(lwb)); 967168404Spjd ASSERT3U(P2PHASE(lwb->lwb_nused, sizeof (uint64_t)), ==, 0); 968168404Spjd 969168404Spjd return (lwb); 970168404Spjd} 971168404Spjd 972168404Spjditx_t * 973185029Spjdzil_itx_create(uint64_t txtype, size_t lrsize) 974168404Spjd{ 975168404Spjd itx_t *itx; 976168404Spjd 977168404Spjd lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t); 978168404Spjd 979168404Spjd itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP); 980168404Spjd itx->itx_lr.lrc_txtype = txtype; 981168404Spjd itx->itx_lr.lrc_reclen = lrsize; 982185029Spjd itx->itx_sod = lrsize; /* if write & WR_NEED_COPY will be increased */ 983168404Spjd itx->itx_lr.lrc_seq = 0; /* defensive */ 984168404Spjd 985168404Spjd return (itx); 986168404Spjd} 987168404Spjd 988168404Spjduint64_t 989168404Spjdzil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx) 990168404Spjd{ 991168404Spjd uint64_t seq; 992168404Spjd 993168404Spjd ASSERT(itx->itx_lr.lrc_seq == 0); 994168404Spjd 995168404Spjd mutex_enter(&zilog->zl_lock); 996168404Spjd list_insert_tail(&zilog->zl_itx_list, itx); 997185029Spjd zilog->zl_itx_list_sz += itx->itx_sod; 998168404Spjd itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx); 999168404Spjd itx->itx_lr.lrc_seq = seq = ++zilog->zl_itx_seq; 1000168404Spjd mutex_exit(&zilog->zl_lock); 1001168404Spjd 1002168404Spjd return (seq); 1003168404Spjd} 1004168404Spjd 1005168404Spjd/* 1006168404Spjd * Free up all in-memory intent log transactions that have now been synced. 1007168404Spjd */ 1008168404Spjdstatic void 1009168404Spjdzil_itx_clean(zilog_t *zilog) 1010168404Spjd{ 1011168404Spjd uint64_t synced_txg = spa_last_synced_txg(zilog->zl_spa); 1012168404Spjd uint64_t freeze_txg = spa_freeze_txg(zilog->zl_spa); 1013168404Spjd list_t clean_list; 1014168404Spjd itx_t *itx; 1015168404Spjd 1016168404Spjd list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node)); 1017168404Spjd 1018168404Spjd mutex_enter(&zilog->zl_lock); 1019168404Spjd /* wait for a log writer to finish walking list */ 1020168404Spjd while (zilog->zl_writer) { 1021168404Spjd cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 1022168404Spjd } 1023168404Spjd 1024168404Spjd /* 1025168404Spjd * Move the sync'd log transactions to a separate list so we can call 1026168404Spjd * kmem_free without holding the zl_lock. 1027168404Spjd * 1028168404Spjd * There is no need to set zl_writer as we don't drop zl_lock here 1029168404Spjd */ 1030168404Spjd while ((itx = list_head(&zilog->zl_itx_list)) != NULL && 1031168404Spjd itx->itx_lr.lrc_txg <= MIN(synced_txg, freeze_txg)) { 1032168404Spjd list_remove(&zilog->zl_itx_list, itx); 1033185029Spjd zilog->zl_itx_list_sz -= itx->itx_sod; 1034168404Spjd list_insert_tail(&clean_list, itx); 1035168404Spjd } 1036168404Spjd cv_broadcast(&zilog->zl_cv_writer); 1037168404Spjd mutex_exit(&zilog->zl_lock); 1038168404Spjd 1039168404Spjd /* destroy sync'd log transactions */ 1040168404Spjd while ((itx = list_head(&clean_list)) != NULL) { 1041168404Spjd list_remove(&clean_list, itx); 1042168404Spjd kmem_free(itx, offsetof(itx_t, itx_lr) 1043168404Spjd + itx->itx_lr.lrc_reclen); 1044168404Spjd } 1045168404Spjd list_destroy(&clean_list); 1046168404Spjd} 1047168404Spjd 1048168404Spjd/* 1049168404Spjd * If there are any in-memory intent log transactions which have now been 1050168404Spjd * synced then start up a taskq to free them. 1051168404Spjd */ 1052168404Spjdvoid 1053168404Spjdzil_clean(zilog_t *zilog) 1054168404Spjd{ 1055168404Spjd itx_t *itx; 1056168404Spjd 1057168404Spjd mutex_enter(&zilog->zl_lock); 1058168404Spjd itx = list_head(&zilog->zl_itx_list); 1059168404Spjd if ((itx != NULL) && 1060168404Spjd (itx->itx_lr.lrc_txg <= spa_last_synced_txg(zilog->zl_spa))) { 1061168404Spjd (void) taskq_dispatch(zilog->zl_clean_taskq, 1062191900Skmacy (task_func_t *)zil_itx_clean, zilog, TQ_SLEEP); 1063168404Spjd } 1064168404Spjd mutex_exit(&zilog->zl_lock); 1065168404Spjd} 1066168404Spjd 1067185029Spjdstatic void 1068168404Spjdzil_commit_writer(zilog_t *zilog, uint64_t seq, uint64_t foid) 1069168404Spjd{ 1070168404Spjd uint64_t txg; 1071168404Spjd uint64_t commit_seq = 0; 1072168404Spjd itx_t *itx, *itx_next = (itx_t *)-1; 1073168404Spjd lwb_t *lwb; 1074168404Spjd spa_t *spa; 1075168404Spjd 1076168404Spjd zilog->zl_writer = B_TRUE; 1077185029Spjd ASSERT(zilog->zl_root_zio == NULL); 1078168404Spjd spa = zilog->zl_spa; 1079168404Spjd 1080168404Spjd if (zilog->zl_suspend) { 1081168404Spjd lwb = NULL; 1082168404Spjd } else { 1083168404Spjd lwb = list_tail(&zilog->zl_lwb_list); 1084168404Spjd if (lwb == NULL) { 1085168404Spjd /* 1086168404Spjd * Return if there's nothing to flush before we 1087168404Spjd * dirty the fs by calling zil_create() 1088168404Spjd */ 1089168404Spjd if (list_is_empty(&zilog->zl_itx_list)) { 1090168404Spjd zilog->zl_writer = B_FALSE; 1091168404Spjd return; 1092168404Spjd } 1093168404Spjd mutex_exit(&zilog->zl_lock); 1094168404Spjd zil_create(zilog); 1095168404Spjd mutex_enter(&zilog->zl_lock); 1096168404Spjd lwb = list_tail(&zilog->zl_lwb_list); 1097168404Spjd } 1098168404Spjd } 1099168404Spjd 1100168404Spjd /* Loop through in-memory log transactions filling log blocks. */ 1101168404Spjd DTRACE_PROBE1(zil__cw1, zilog_t *, zilog); 1102168404Spjd for (;;) { 1103168404Spjd /* 1104168404Spjd * Find the next itx to push: 1105168404Spjd * Push all transactions related to specified foid and all 1106168404Spjd * other transactions except TX_WRITE, TX_TRUNCATE, 1107168404Spjd * TX_SETATTR and TX_ACL for all other files. 1108168404Spjd */ 1109168404Spjd if (itx_next != (itx_t *)-1) 1110168404Spjd itx = itx_next; 1111168404Spjd else 1112168404Spjd itx = list_head(&zilog->zl_itx_list); 1113168404Spjd for (; itx != NULL; itx = list_next(&zilog->zl_itx_list, itx)) { 1114168404Spjd if (foid == 0) /* push all foids? */ 1115168404Spjd break; 1116168404Spjd if (itx->itx_sync) /* push all O_[D]SYNC */ 1117168404Spjd break; 1118168404Spjd switch (itx->itx_lr.lrc_txtype) { 1119168404Spjd case TX_SETATTR: 1120168404Spjd case TX_WRITE: 1121168404Spjd case TX_TRUNCATE: 1122168404Spjd case TX_ACL: 1123168404Spjd /* lr_foid is same offset for these records */ 1124168404Spjd if (((lr_write_t *)&itx->itx_lr)->lr_foid 1125168404Spjd != foid) { 1126168404Spjd continue; /* skip this record */ 1127168404Spjd } 1128168404Spjd } 1129168404Spjd break; 1130168404Spjd } 1131168404Spjd if (itx == NULL) 1132168404Spjd break; 1133168404Spjd 1134168404Spjd if ((itx->itx_lr.lrc_seq > seq) && 1135168404Spjd ((lwb == NULL) || (lwb->lwb_nused == 0) || 1136185029Spjd (lwb->lwb_nused + itx->itx_sod > ZIL_BLK_DATA_SZ(lwb)))) { 1137168404Spjd break; 1138168404Spjd } 1139168404Spjd 1140168404Spjd /* 1141168404Spjd * Save the next pointer. Even though we soon drop 1142168404Spjd * zl_lock all threads that may change the list 1143168404Spjd * (another writer or zil_itx_clean) can't do so until 1144168404Spjd * they have zl_writer. 1145168404Spjd */ 1146168404Spjd itx_next = list_next(&zilog->zl_itx_list, itx); 1147168404Spjd list_remove(&zilog->zl_itx_list, itx); 1148185029Spjd zilog->zl_itx_list_sz -= itx->itx_sod; 1149168404Spjd mutex_exit(&zilog->zl_lock); 1150168404Spjd txg = itx->itx_lr.lrc_txg; 1151168404Spjd ASSERT(txg); 1152168404Spjd 1153168404Spjd if (txg > spa_last_synced_txg(spa) || 1154168404Spjd txg > spa_freeze_txg(spa)) 1155168404Spjd lwb = zil_lwb_commit(zilog, itx, lwb); 1156168404Spjd kmem_free(itx, offsetof(itx_t, itx_lr) 1157168404Spjd + itx->itx_lr.lrc_reclen); 1158168404Spjd mutex_enter(&zilog->zl_lock); 1159168404Spjd } 1160168404Spjd DTRACE_PROBE1(zil__cw2, zilog_t *, zilog); 1161168404Spjd /* determine commit sequence number */ 1162168404Spjd itx = list_head(&zilog->zl_itx_list); 1163168404Spjd if (itx) 1164168404Spjd commit_seq = itx->itx_lr.lrc_seq; 1165168404Spjd else 1166168404Spjd commit_seq = zilog->zl_itx_seq; 1167168404Spjd mutex_exit(&zilog->zl_lock); 1168168404Spjd 1169168404Spjd /* write the last block out */ 1170168404Spjd if (lwb != NULL && lwb->lwb_zio != NULL) 1171168404Spjd lwb = zil_lwb_write_start(zilog, lwb); 1172168404Spjd 1173168404Spjd zilog->zl_prev_used = zilog->zl_cur_used; 1174168404Spjd zilog->zl_cur_used = 0; 1175168404Spjd 1176168404Spjd /* 1177168404Spjd * Wait if necessary for the log blocks to be on stable storage. 1178168404Spjd */ 1179168404Spjd if (zilog->zl_root_zio) { 1180168404Spjd DTRACE_PROBE1(zil__cw3, zilog_t *, zilog); 1181168404Spjd (void) zio_wait(zilog->zl_root_zio); 1182185029Spjd zilog->zl_root_zio = NULL; 1183168404Spjd DTRACE_PROBE1(zil__cw4, zilog_t *, zilog); 1184185029Spjd zil_flush_vdevs(zilog); 1185168404Spjd } 1186168404Spjd 1187168404Spjd if (zilog->zl_log_error || lwb == NULL) { 1188168404Spjd zilog->zl_log_error = 0; 1189168404Spjd txg_wait_synced(zilog->zl_dmu_pool, 0); 1190168404Spjd } 1191168404Spjd 1192168404Spjd mutex_enter(&zilog->zl_lock); 1193168404Spjd zilog->zl_writer = B_FALSE; 1194168404Spjd 1195168404Spjd ASSERT3U(commit_seq, >=, zilog->zl_commit_seq); 1196168404Spjd zilog->zl_commit_seq = commit_seq; 1197168404Spjd} 1198168404Spjd 1199168404Spjd/* 1200168404Spjd * Push zfs transactions to stable storage up to the supplied sequence number. 1201168404Spjd * If foid is 0 push out all transactions, otherwise push only those 1202168404Spjd * for that file or might have been used to create that file. 1203168404Spjd */ 1204168404Spjdvoid 1205168404Spjdzil_commit(zilog_t *zilog, uint64_t seq, uint64_t foid) 1206168404Spjd{ 1207168404Spjd if (zilog == NULL || seq == 0) 1208168404Spjd return; 1209168404Spjd 1210168404Spjd mutex_enter(&zilog->zl_lock); 1211168404Spjd 1212168404Spjd seq = MIN(seq, zilog->zl_itx_seq); /* cap seq at largest itx seq */ 1213168404Spjd 1214168404Spjd while (zilog->zl_writer) { 1215168404Spjd cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 1216168404Spjd if (seq < zilog->zl_commit_seq) { 1217168404Spjd mutex_exit(&zilog->zl_lock); 1218168404Spjd return; 1219168404Spjd } 1220168404Spjd } 1221168404Spjd zil_commit_writer(zilog, seq, foid); /* drops zl_lock */ 1222168404Spjd /* wake up others waiting on the commit */ 1223168404Spjd cv_broadcast(&zilog->zl_cv_writer); 1224168404Spjd mutex_exit(&zilog->zl_lock); 1225168404Spjd} 1226168404Spjd 1227168404Spjd/* 1228168404Spjd * Called in syncing context to free committed log blocks and update log header. 1229168404Spjd */ 1230168404Spjdvoid 1231168404Spjdzil_sync(zilog_t *zilog, dmu_tx_t *tx) 1232168404Spjd{ 1233168404Spjd zil_header_t *zh = zil_header_in_syncing_context(zilog); 1234168404Spjd uint64_t txg = dmu_tx_get_txg(tx); 1235168404Spjd spa_t *spa = zilog->zl_spa; 1236168404Spjd lwb_t *lwb; 1237168404Spjd 1238209962Smm /* 1239209962Smm * We don't zero out zl_destroy_txg, so make sure we don't try 1240209962Smm * to destroy it twice. 1241209962Smm */ 1242209962Smm if (spa_sync_pass(spa) != 1) 1243209962Smm return; 1244209962Smm 1245168404Spjd mutex_enter(&zilog->zl_lock); 1246168404Spjd 1247168404Spjd ASSERT(zilog->zl_stop_sync == 0); 1248168404Spjd 1249209962Smm zh->zh_replay_seq = zilog->zl_replayed_seq[txg & TXG_MASK]; 1250168404Spjd 1251168404Spjd if (zilog->zl_destroy_txg == txg) { 1252168404Spjd blkptr_t blk = zh->zh_log; 1253168404Spjd 1254168404Spjd ASSERT(list_head(&zilog->zl_lwb_list) == NULL); 1255168404Spjd 1256168404Spjd bzero(zh, sizeof (zil_header_t)); 1257209962Smm bzero(zilog->zl_replayed_seq, sizeof (zilog->zl_replayed_seq)); 1258168404Spjd 1259168404Spjd if (zilog->zl_keep_first) { 1260168404Spjd /* 1261168404Spjd * If this block was part of log chain that couldn't 1262168404Spjd * be claimed because a device was missing during 1263168404Spjd * zil_claim(), but that device later returns, 1264168404Spjd * then this block could erroneously appear valid. 1265168404Spjd * To guard against this, assign a new GUID to the new 1266168404Spjd * log chain so it doesn't matter what blk points to. 1267168404Spjd */ 1268168404Spjd zil_init_log_chain(zilog, &blk); 1269168404Spjd zh->zh_log = blk; 1270168404Spjd } 1271168404Spjd } 1272168404Spjd 1273168404Spjd for (;;) { 1274168404Spjd lwb = list_head(&zilog->zl_lwb_list); 1275168404Spjd if (lwb == NULL) { 1276168404Spjd mutex_exit(&zilog->zl_lock); 1277168404Spjd return; 1278168404Spjd } 1279168404Spjd zh->zh_log = lwb->lwb_blk; 1280168404Spjd if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg) 1281168404Spjd break; 1282168404Spjd list_remove(&zilog->zl_lwb_list, lwb); 1283168404Spjd zio_free_blk(spa, &lwb->lwb_blk, txg); 1284168404Spjd kmem_cache_free(zil_lwb_cache, lwb); 1285168404Spjd 1286168404Spjd /* 1287168404Spjd * If we don't have anything left in the lwb list then 1288168404Spjd * we've had an allocation failure and we need to zero 1289168404Spjd * out the zil_header blkptr so that we don't end 1290168404Spjd * up freeing the same block twice. 1291168404Spjd */ 1292168404Spjd if (list_head(&zilog->zl_lwb_list) == NULL) 1293168404Spjd BP_ZERO(&zh->zh_log); 1294168404Spjd } 1295168404Spjd mutex_exit(&zilog->zl_lock); 1296168404Spjd} 1297168404Spjd 1298168404Spjdvoid 1299168404Spjdzil_init(void) 1300168404Spjd{ 1301168404Spjd zil_lwb_cache = kmem_cache_create("zil_lwb_cache", 1302168404Spjd sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0); 1303168404Spjd} 1304168404Spjd 1305168404Spjdvoid 1306168404Spjdzil_fini(void) 1307168404Spjd{ 1308168404Spjd kmem_cache_destroy(zil_lwb_cache); 1309168404Spjd} 1310168404Spjd 1311168404Spjdzilog_t * 1312168404Spjdzil_alloc(objset_t *os, zil_header_t *zh_phys) 1313168404Spjd{ 1314168404Spjd zilog_t *zilog; 1315168404Spjd 1316168404Spjd zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP); 1317168404Spjd 1318168404Spjd zilog->zl_header = zh_phys; 1319168404Spjd zilog->zl_os = os; 1320168404Spjd zilog->zl_spa = dmu_objset_spa(os); 1321168404Spjd zilog->zl_dmu_pool = dmu_objset_pool(os); 1322168404Spjd zilog->zl_destroy_txg = TXG_INITIAL - 1; 1323168404Spjd 1324168404Spjd mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL); 1325168404Spjd 1326168404Spjd list_create(&zilog->zl_itx_list, sizeof (itx_t), 1327168404Spjd offsetof(itx_t, itx_node)); 1328168404Spjd 1329168404Spjd list_create(&zilog->zl_lwb_list, sizeof (lwb_t), 1330168404Spjd offsetof(lwb_t, lwb_node)); 1331168404Spjd 1332185029Spjd mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL); 1333168404Spjd 1334185029Spjd avl_create(&zilog->zl_vdev_tree, zil_vdev_compare, 1335185029Spjd sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node)); 1336185029Spjd 1337185029Spjd cv_init(&zilog->zl_cv_writer, NULL, CV_DEFAULT, NULL); 1338185029Spjd cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL); 1339185029Spjd 1340168404Spjd return (zilog); 1341168404Spjd} 1342168404Spjd 1343168404Spjdvoid 1344168404Spjdzil_free(zilog_t *zilog) 1345168404Spjd{ 1346168404Spjd lwb_t *lwb; 1347168404Spjd 1348168404Spjd zilog->zl_stop_sync = 1; 1349168404Spjd 1350168404Spjd while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 1351168404Spjd list_remove(&zilog->zl_lwb_list, lwb); 1352168404Spjd if (lwb->lwb_buf != NULL) 1353168404Spjd zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 1354168404Spjd kmem_cache_free(zil_lwb_cache, lwb); 1355168404Spjd } 1356168404Spjd list_destroy(&zilog->zl_lwb_list); 1357168404Spjd 1358185029Spjd avl_destroy(&zilog->zl_vdev_tree); 1359185029Spjd mutex_destroy(&zilog->zl_vdev_lock); 1360168404Spjd 1361168404Spjd ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1362168404Spjd list_destroy(&zilog->zl_itx_list); 1363168404Spjd mutex_destroy(&zilog->zl_lock); 1364168404Spjd 1365185029Spjd cv_destroy(&zilog->zl_cv_writer); 1366185029Spjd cv_destroy(&zilog->zl_cv_suspend); 1367185029Spjd 1368168404Spjd kmem_free(zilog, sizeof (zilog_t)); 1369168404Spjd} 1370168404Spjd 1371168404Spjd/* 1372168404Spjd * Open an intent log. 1373168404Spjd */ 1374168404Spjdzilog_t * 1375168404Spjdzil_open(objset_t *os, zil_get_data_t *get_data) 1376168404Spjd{ 1377168404Spjd zilog_t *zilog = dmu_objset_zil(os); 1378168404Spjd 1379168404Spjd zilog->zl_get_data = get_data; 1380168404Spjd zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri, 1381168404Spjd 2, 2, TASKQ_PREPOPULATE); 1382168404Spjd 1383168404Spjd return (zilog); 1384168404Spjd} 1385168404Spjd 1386168404Spjd/* 1387168404Spjd * Close an intent log. 1388168404Spjd */ 1389168404Spjdvoid 1390168404Spjdzil_close(zilog_t *zilog) 1391168404Spjd{ 1392168404Spjd /* 1393168404Spjd * If the log isn't already committed, mark the objset dirty 1394168404Spjd * (so zil_sync() will be called) and wait for that txg to sync. 1395168404Spjd */ 1396168404Spjd if (!zil_is_committed(zilog)) { 1397168404Spjd uint64_t txg; 1398168404Spjd dmu_tx_t *tx = dmu_tx_create(zilog->zl_os); 1399168404Spjd (void) dmu_tx_assign(tx, TXG_WAIT); 1400168404Spjd dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 1401168404Spjd txg = dmu_tx_get_txg(tx); 1402168404Spjd dmu_tx_commit(tx); 1403168404Spjd txg_wait_synced(zilog->zl_dmu_pool, txg); 1404168404Spjd } 1405168404Spjd 1406168404Spjd taskq_destroy(zilog->zl_clean_taskq); 1407168404Spjd zilog->zl_clean_taskq = NULL; 1408168404Spjd zilog->zl_get_data = NULL; 1409168404Spjd 1410168404Spjd zil_itx_clean(zilog); 1411168404Spjd ASSERT(list_head(&zilog->zl_itx_list) == NULL); 1412168404Spjd} 1413168404Spjd 1414168404Spjd/* 1415168404Spjd * Suspend an intent log. While in suspended mode, we still honor 1416168404Spjd * synchronous semantics, but we rely on txg_wait_synced() to do it. 1417168404Spjd * We suspend the log briefly when taking a snapshot so that the snapshot 1418168404Spjd * contains all the data it's supposed to, and has an empty intent log. 1419168404Spjd */ 1420168404Spjdint 1421168404Spjdzil_suspend(zilog_t *zilog) 1422168404Spjd{ 1423168404Spjd const zil_header_t *zh = zilog->zl_header; 1424168404Spjd 1425168404Spjd mutex_enter(&zilog->zl_lock); 1426200724Sdelphij if (zh->zh_flags & ZIL_REPLAY_NEEDED) { /* unplayed log */ 1427168404Spjd mutex_exit(&zilog->zl_lock); 1428168404Spjd return (EBUSY); 1429168404Spjd } 1430168404Spjd if (zilog->zl_suspend++ != 0) { 1431168404Spjd /* 1432168404Spjd * Someone else already began a suspend. 1433168404Spjd * Just wait for them to finish. 1434168404Spjd */ 1435168404Spjd while (zilog->zl_suspending) 1436168404Spjd cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock); 1437168404Spjd mutex_exit(&zilog->zl_lock); 1438168404Spjd return (0); 1439168404Spjd } 1440168404Spjd zilog->zl_suspending = B_TRUE; 1441168404Spjd mutex_exit(&zilog->zl_lock); 1442168404Spjd 1443168404Spjd zil_commit(zilog, UINT64_MAX, 0); 1444168404Spjd 1445168404Spjd /* 1446168404Spjd * Wait for any in-flight log writes to complete. 1447168404Spjd */ 1448168404Spjd mutex_enter(&zilog->zl_lock); 1449168404Spjd while (zilog->zl_writer) 1450168404Spjd cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 1451168404Spjd mutex_exit(&zilog->zl_lock); 1452168404Spjd 1453168404Spjd zil_destroy(zilog, B_FALSE); 1454168404Spjd 1455168404Spjd mutex_enter(&zilog->zl_lock); 1456168404Spjd zilog->zl_suspending = B_FALSE; 1457168404Spjd cv_broadcast(&zilog->zl_cv_suspend); 1458168404Spjd mutex_exit(&zilog->zl_lock); 1459168404Spjd 1460168404Spjd return (0); 1461168404Spjd} 1462168404Spjd 1463168404Spjdvoid 1464168404Spjdzil_resume(zilog_t *zilog) 1465168404Spjd{ 1466168404Spjd mutex_enter(&zilog->zl_lock); 1467168404Spjd ASSERT(zilog->zl_suspend != 0); 1468168404Spjd zilog->zl_suspend--; 1469168404Spjd mutex_exit(&zilog->zl_lock); 1470168404Spjd} 1471168404Spjd 1472209962Smm/* 1473209962Smm * Read in the data for the dmu_sync()ed block, and change the log 1474209962Smm * record to write this whole block. 1475209962Smm */ 1476209962Smmvoid 1477209962Smmzil_get_replay_data(zilog_t *zilog, lr_write_t *lr) 1478209962Smm{ 1479209962Smm blkptr_t *wbp = &lr->lr_blkptr; 1480209962Smm char *wbuf = (char *)(lr + 1); /* data follows lr_write_t */ 1481209962Smm uint64_t blksz; 1482209962Smm 1483209962Smm if (BP_IS_HOLE(wbp)) { /* compressed to a hole */ 1484209962Smm blksz = BP_GET_LSIZE(&lr->lr_blkptr); 1485209962Smm /* 1486209962Smm * If the blksz is zero then we must be replaying a log 1487209962Smm * from an version prior to setting the blksize of null blocks. 1488209962Smm * So we just zero the actual write size reqeusted. 1489209962Smm */ 1490209962Smm if (blksz == 0) { 1491209962Smm bzero(wbuf, lr->lr_length); 1492209962Smm return; 1493209962Smm } 1494209962Smm bzero(wbuf, blksz); 1495209962Smm } else { 1496209962Smm /* 1497209962Smm * A subsequent write may have overwritten this block, in which 1498209962Smm * case wbp may have been been freed and reallocated, and our 1499209962Smm * read of wbp may fail with a checksum error. We can safely 1500209962Smm * ignore this because the later write will provide the 1501209962Smm * correct data. 1502209962Smm */ 1503209962Smm zbookmark_t zb; 1504209962Smm 1505209962Smm zb.zb_objset = dmu_objset_id(zilog->zl_os); 1506209962Smm zb.zb_object = lr->lr_foid; 1507209962Smm zb.zb_level = 0; 1508209962Smm zb.zb_blkid = -1; /* unknown */ 1509209962Smm 1510209962Smm blksz = BP_GET_LSIZE(&lr->lr_blkptr); 1511209962Smm (void) zio_wait(zio_read(NULL, zilog->zl_spa, wbp, wbuf, blksz, 1512209962Smm NULL, NULL, ZIO_PRIORITY_SYNC_READ, 1513209962Smm ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb)); 1514209962Smm } 1515209962Smm lr->lr_offset -= lr->lr_offset % blksz; 1516209962Smm lr->lr_length = blksz; 1517209962Smm} 1518209962Smm 1519168404Spjdtypedef struct zil_replay_arg { 1520168404Spjd objset_t *zr_os; 1521168404Spjd zil_replay_func_t **zr_replay; 1522168404Spjd void *zr_arg; 1523168404Spjd boolean_t zr_byteswap; 1524168404Spjd char *zr_lrbuf; 1525168404Spjd} zil_replay_arg_t; 1526168404Spjd 1527168404Spjdstatic void 1528168404Spjdzil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg) 1529168404Spjd{ 1530168404Spjd zil_replay_arg_t *zr = zra; 1531168404Spjd const zil_header_t *zh = zilog->zl_header; 1532168404Spjd uint64_t reclen = lr->lrc_reclen; 1533168404Spjd uint64_t txtype = lr->lrc_txtype; 1534168404Spjd char *name; 1535209962Smm int pass, error; 1536168404Spjd 1537209962Smm if (!zilog->zl_replay) /* giving up */ 1538168404Spjd return; 1539168404Spjd 1540168404Spjd if (lr->lrc_txg < claim_txg) /* already committed */ 1541168404Spjd return; 1542168404Spjd 1543168404Spjd if (lr->lrc_seq <= zh->zh_replay_seq) /* already replayed */ 1544168404Spjd return; 1545168404Spjd 1546185029Spjd /* Strip case-insensitive bit, still present in log record */ 1547185029Spjd txtype &= ~TX_CI; 1548185029Spjd 1549209962Smm if (txtype == 0 || txtype >= TX_MAX_TYPE) { 1550209962Smm error = EINVAL; 1551209962Smm goto bad; 1552209962Smm } 1553209962Smm 1554168404Spjd /* 1555168404Spjd * Make a copy of the data so we can revise and extend it. 1556168404Spjd */ 1557168404Spjd bcopy(lr, zr->zr_lrbuf, reclen); 1558168404Spjd 1559168404Spjd /* 1560168404Spjd * The log block containing this lr may have been byteswapped 1561168404Spjd * so that we can easily examine common fields like lrc_txtype. 1562168404Spjd * However, the log is a mix of different data types, and only the 1563168404Spjd * replay vectors know how to byteswap their records. Therefore, if 1564168404Spjd * the lr was byteswapped, undo it before invoking the replay vector. 1565168404Spjd */ 1566168404Spjd if (zr->zr_byteswap) 1567168404Spjd byteswap_uint64_array(zr->zr_lrbuf, reclen); 1568168404Spjd 1569168404Spjd /* 1570168404Spjd * We must now do two things atomically: replay this log record, 1571209962Smm * and update the log header sequence number to reflect the fact that 1572209962Smm * we did so. At the end of each replay function the sequence number 1573209962Smm * is updated if we are in replay mode. 1574168404Spjd */ 1575209962Smm for (pass = 1; pass <= 2; pass++) { 1576209962Smm zilog->zl_replaying_seq = lr->lrc_seq; 1577209962Smm /* Only byteswap (if needed) on the 1st pass. */ 1578209962Smm error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lrbuf, 1579209962Smm zr->zr_byteswap && pass == 1); 1580168404Spjd 1581168404Spjd if (!error) 1582168404Spjd return; 1583168404Spjd 1584168404Spjd /* 1585168404Spjd * The DMU's dnode layer doesn't see removes until the txg 1586168404Spjd * commits, so a subsequent claim can spuriously fail with 1587209962Smm * EEXIST. So if we receive any error we try syncing out 1588209962Smm * any removes then retry the transaction. 1589168404Spjd */ 1590209962Smm if (pass == 1) 1591168404Spjd txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0); 1592168404Spjd } 1593168404Spjd 1594207956Smmbad: 1595209962Smm ASSERT(error); 1596168404Spjd name = kmem_alloc(MAXNAMELEN, KM_SLEEP); 1597168404Spjd dmu_objset_name(zr->zr_os, name); 1598168404Spjd cmn_err(CE_WARN, "ZFS replay transaction error %d, " 1599185029Spjd "dataset %s, seq 0x%llx, txtype %llu %s\n", 1600185029Spjd error, name, (u_longlong_t)lr->lrc_seq, (u_longlong_t)txtype, 1601185029Spjd (lr->lrc_txtype & TX_CI) ? "CI" : ""); 1602209962Smm zilog->zl_replay = B_FALSE; 1603168404Spjd kmem_free(name, MAXNAMELEN); 1604168404Spjd} 1605168404Spjd 1606168404Spjd/* ARGSUSED */ 1607168404Spjdstatic void 1608168404Spjdzil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg) 1609168404Spjd{ 1610168404Spjd zilog->zl_replay_blks++; 1611168404Spjd} 1612168404Spjd 1613168404Spjd/* 1614168404Spjd * If this dataset has a non-empty intent log, replay it and destroy it. 1615168404Spjd */ 1616168404Spjdvoid 1617209962Smmzil_replay(objset_t *os, void *arg, zil_replay_func_t *replay_func[TX_MAX_TYPE]) 1618168404Spjd{ 1619168404Spjd zilog_t *zilog = dmu_objset_zil(os); 1620168404Spjd const zil_header_t *zh = zilog->zl_header; 1621168404Spjd zil_replay_arg_t zr; 1622168404Spjd 1623200724Sdelphij if ((zh->zh_flags & ZIL_REPLAY_NEEDED) == 0) { 1624168404Spjd zil_destroy(zilog, B_TRUE); 1625168404Spjd return; 1626168404Spjd } 1627168404Spjd //printf("ZFS: Replaying ZIL on %s...\n", os->os->os_spa->spa_name); 1628168404Spjd 1629168404Spjd zr.zr_os = os; 1630168404Spjd zr.zr_replay = replay_func; 1631168404Spjd zr.zr_arg = arg; 1632168404Spjd zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log); 1633168404Spjd zr.zr_lrbuf = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP); 1634168404Spjd 1635168404Spjd /* 1636168404Spjd * Wait for in-progress removes to sync before starting replay. 1637168404Spjd */ 1638168404Spjd txg_wait_synced(zilog->zl_dmu_pool, 0); 1639168404Spjd 1640209962Smm zilog->zl_replay = B_TRUE; 1641174049Sjb zilog->zl_replay_time = LBOLT; 1642168404Spjd ASSERT(zilog->zl_replay_blks == 0); 1643168404Spjd (void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr, 1644168404Spjd zh->zh_claim_txg); 1645168404Spjd kmem_free(zr.zr_lrbuf, 2 * SPA_MAXBLOCKSIZE); 1646168404Spjd 1647168404Spjd zil_destroy(zilog, B_FALSE); 1648185029Spjd txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 1649209962Smm zilog->zl_replay = B_FALSE; 1650168404Spjd //printf("ZFS: Replay of ZIL on %s finished.\n", os->os->os_spa->spa_name); 1651168404Spjd} 1652168404Spjd 1653168404Spjd/* 1654168404Spjd * Report whether all transactions are committed 1655168404Spjd */ 1656168404Spjdint 1657168404Spjdzil_is_committed(zilog_t *zilog) 1658168404Spjd{ 1659168404Spjd lwb_t *lwb; 1660168404Spjd int ret; 1661168404Spjd 1662168404Spjd mutex_enter(&zilog->zl_lock); 1663168404Spjd while (zilog->zl_writer) 1664168404Spjd cv_wait(&zilog->zl_cv_writer, &zilog->zl_lock); 1665168404Spjd 1666168404Spjd /* recent unpushed intent log transactions? */ 1667168404Spjd if (!list_is_empty(&zilog->zl_itx_list)) { 1668168404Spjd ret = B_FALSE; 1669168404Spjd goto out; 1670168404Spjd } 1671168404Spjd 1672168404Spjd /* intent log never used? */ 1673168404Spjd lwb = list_head(&zilog->zl_lwb_list); 1674168404Spjd if (lwb == NULL) { 1675168404Spjd ret = B_TRUE; 1676168404Spjd goto out; 1677168404Spjd } 1678168404Spjd 1679168404Spjd /* 1680168404Spjd * more than 1 log buffer means zil_sync() hasn't yet freed 1681168404Spjd * entries after a txg has committed 1682168404Spjd */ 1683168404Spjd if (list_next(&zilog->zl_lwb_list, lwb)) { 1684168404Spjd ret = B_FALSE; 1685168404Spjd goto out; 1686168404Spjd } 1687168404Spjd 1688168404Spjd ASSERT(zil_empty(zilog)); 1689168404Spjd ret = B_TRUE; 1690168404Spjdout: 1691168404Spjd cv_broadcast(&zilog->zl_cv_writer); 1692168404Spjd mutex_exit(&zilog->zl_lock); 1693168404Spjd return (ret); 1694168404Spjd} 1695