zil.c revision 286575
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/* 22185029Spjd * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23168404Spjd * Copyright (c) 2011, 2014 by Delphix. All rights reserved. 24168404Spjd */ 25168404Spjd 26168404Spjd/* Portions Copyright 2010 Robert Milkowski */ 27168404Spjd 28168404Spjd#include <sys/zfs_context.h> 29168404Spjd#include <sys/spa.h> 30168404Spjd#include <sys/dmu.h> 31168404Spjd#include <sys/zap.h> 32168404Spjd#include <sys/arc.h> 33168404Spjd#include <sys/stat.h> 34168404Spjd#include <sys/resource.h> 35168404Spjd#include <sys/zil.h> 36168404Spjd#include <sys/zil_impl.h> 37168404Spjd#include <sys/dsl_dataset.h> 38168404Spjd#include <sys/vdev_impl.h> 39168404Spjd#include <sys/dmu_tx.h> 40168404Spjd#include <sys/dsl_pool.h> 41168404Spjd 42168404Spjd/* 43168404Spjd * The zfs intent log (ZIL) saves transaction records of system calls 44168404Spjd * that change the file system in memory with enough information 45168404Spjd * to be able to replay them. These are stored in memory until 46168404Spjd * either the DMU transaction group (txg) commits them to the stable pool 47168404Spjd * and they can be discarded, or they are flushed to the stable log 48168404Spjd * (also in the pool) due to a fsync, O_DSYNC or other synchronous 49168404Spjd * requirement. In the event of a panic or power fail then those log 50168404Spjd * records (transactions) are replayed. 51168404Spjd * 52168404Spjd * There is one ZIL per file system. Its on-disk (pool) format consists 53168404Spjd * of 3 parts: 54168404Spjd * 55168404Spjd * - ZIL header 56168404Spjd * - ZIL blocks 57168404Spjd * - ZIL records 58168404Spjd * 59168404Spjd * A log record holds a system call transaction. Log blocks can 60168404Spjd * hold many log records and the blocks are chained together. 61168404Spjd * Each ZIL block contains a block pointer (blkptr_t) to the next 62168404Spjd * ZIL block in the chain. The ZIL header points to the first 63168404Spjd * block in the chain. Note there is not a fixed place in the pool 64168404Spjd * to hold blocks. They are dynamically allocated and freed as 65168404Spjd * needed from the blocks available. Figure X shows the ZIL structure: 66168404Spjd */ 67168404Spjd 68168404Spjd/* 69168404Spjd * Disable intent logging replay. This global ZIL switch affects all pools. 70168404Spjd */ 71169028Spjdint zil_replay_disable = 0; 72168404SpjdSYSCTL_DECL(_vfs_zfs); 73168404SpjdSYSCTL_INT(_vfs_zfs, OID_AUTO, zil_replay_disable, CTLFLAG_RWTUN, 74168404Spjd &zil_replay_disable, 0, "Disable intent logging replay"); 75168404Spjd 76168404Spjd/* 77168404Spjd * Tunable parameter for debugging or performance analysis. Setting 78168404Spjd * zfs_nocacheflush will cause corruption on power loss if a volatile 79168404Spjd * out-of-order write cache is enabled. 80168404Spjd */ 81168404Spjdboolean_t zfs_nocacheflush = B_FALSE; 82168404SpjdSYSCTL_INT(_vfs_zfs, OID_AUTO, cache_flush_disable, CTLFLAG_RDTUN, 83168404Spjd &zfs_nocacheflush, 0, "Disable cache flush"); 84168404Spjdboolean_t zfs_trim_enabled = B_TRUE; 85168404SpjdSYSCTL_DECL(_vfs_zfs_trim); 86168404SpjdSYSCTL_INT(_vfs_zfs_trim, OID_AUTO, enabled, CTLFLAG_RDTUN, &zfs_trim_enabled, 0, 87168404Spjd "Enable ZFS TRIM"); 88168404Spjd 89168404Spjdstatic kmem_cache_t *zil_lwb_cache; 90168404Spjd 91168404Spjdstatic void zil_async_to_sync(zilog_t *zilog, uint64_t foid); 92168404Spjd 93168404Spjd#define LWB_EMPTY(lwb) ((BP_GET_LSIZE(&lwb->lwb_blk) - \ 94168404Spjd sizeof (zil_chain_t)) == (lwb->lwb_sz - lwb->lwb_nused)) 95168404Spjd 96168404Spjd 97168404Spjd/* 98168404Spjd * ziltest is by and large an ugly hack, but very useful in 99168404Spjd * checking replay without tedious work. 100168404Spjd * When running ziltest we want to keep all itx's and so maintain 101168404Spjd * a single list in the zl_itxg[] that uses a high txg: ZILTEST_TXG 102168404Spjd * We subtract TXG_CONCURRENT_STATES to allow for common code. 103168404Spjd */ 104168404Spjd#define ZILTEST_TXG (UINT64_MAX - TXG_CONCURRENT_STATES) 105168404Spjd 106168404Spjdstatic int 107168404Spjdzil_bp_compare(const void *x1, const void *x2) 108168404Spjd{ 109168404Spjd const dva_t *dva1 = &((zil_bp_node_t *)x1)->zn_dva; 110168404Spjd const dva_t *dva2 = &((zil_bp_node_t *)x2)->zn_dva; 111168404Spjd 112168404Spjd if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2)) 113168404Spjd return (-1); 114168404Spjd if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2)) 115168404Spjd return (1); 116168404Spjd 117168404Spjd if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2)) 118168404Spjd return (-1); 119168404Spjd if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2)) 120168404Spjd return (1); 121168404Spjd 122168404Spjd return (0); 123168404Spjd} 124168404Spjd 125168404Spjdstatic void 126168404Spjdzil_bp_tree_init(zilog_t *zilog) 127168404Spjd{ 128168404Spjd avl_create(&zilog->zl_bp_tree, zil_bp_compare, 129168404Spjd sizeof (zil_bp_node_t), offsetof(zil_bp_node_t, zn_node)); 130168404Spjd} 131168404Spjd 132168404Spjdstatic void 133168404Spjdzil_bp_tree_fini(zilog_t *zilog) 134168404Spjd{ 135168404Spjd avl_tree_t *t = &zilog->zl_bp_tree; 136168404Spjd zil_bp_node_t *zn; 137168404Spjd void *cookie = NULL; 138168404Spjd 139168404Spjd while ((zn = avl_destroy_nodes(t, &cookie)) != NULL) 140168404Spjd kmem_free(zn, sizeof (zil_bp_node_t)); 141168404Spjd 142168404Spjd avl_destroy(t); 143168404Spjd} 144168404Spjd 145168404Spjdint 146168404Spjdzil_bp_tree_add(zilog_t *zilog, const blkptr_t *bp) 147168404Spjd{ 148168404Spjd avl_tree_t *t = &zilog->zl_bp_tree; 149168404Spjd const dva_t *dva; 150168404Spjd zil_bp_node_t *zn; 151168404Spjd avl_index_t where; 152168404Spjd 153168404Spjd if (BP_IS_EMBEDDED(bp)) 154168404Spjd return (0); 155168404Spjd 156168404Spjd dva = BP_IDENTITY(bp); 157168404Spjd 158168404Spjd if (avl_find(t, dva, &where) != NULL) 159168404Spjd return (SET_ERROR(EEXIST)); 160168404Spjd 161168404Spjd zn = kmem_alloc(sizeof (zil_bp_node_t), KM_SLEEP); 162168404Spjd zn->zn_dva = *dva; 163168404Spjd avl_insert(t, zn, where); 164168404Spjd 165168404Spjd return (0); 166168404Spjd} 167168404Spjd 168168404Spjdstatic zil_header_t * 169168404Spjdzil_header_in_syncing_context(zilog_t *zilog) 170168404Spjd{ 171168404Spjd return ((zil_header_t *)zilog->zl_header); 172168404Spjd} 173168404Spjd 174168404Spjdstatic void 175185029Spjdzil_init_log_chain(zilog_t *zilog, blkptr_t *bp) 176185029Spjd{ 177185029Spjd zio_cksum_t *zc = &bp->blk_cksum; 178185029Spjd 179185029Spjd zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL); 180168404Spjd zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL); 181168404Spjd zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os); 182168404Spjd zc->zc_word[ZIL_ZC_SEQ] = 1ULL; 183168404Spjd} 184168404Spjd 185168404Spjd/* 186168404Spjd * Read a log block and make sure it's valid. 187168404Spjd */ 188168404Spjdstatic int 189168404Spjdzil_read_log_block(zilog_t *zilog, const blkptr_t *bp, blkptr_t *nbp, void *dst, 190185029Spjd char **end) 191185029Spjd{ 192168404Spjd enum zio_flag zio_flags = ZIO_FLAG_CANFAIL; 193168404Spjd arc_flags_t aflags = ARC_FLAG_WAIT; 194185029Spjd arc_buf_t *abuf = NULL; 195185029Spjd zbookmark_phys_t zb; 196168404Spjd int error; 197168404Spjd 198168404Spjd if (zilog->zl_header->zh_claim_txg == 0) 199185029Spjd zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB; 200185029Spjd 201185029Spjd if (!(zilog->zl_header->zh_flags & ZIL_CLAIM_LR_SEQ_VALID)) 202185029Spjd zio_flags |= ZIO_FLAG_SPECULATIVE; 203185029Spjd 204168404Spjd SET_BOOKMARK(&zb, bp->blk_cksum.zc_word[ZIL_ZC_OBJSET], 205168404Spjd ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]); 206168404Spjd 207168404Spjd error = arc_read(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf, 208168404Spjd ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb); 209168404Spjd 210168404Spjd if (error == 0) { 211168404Spjd zio_cksum_t cksum = bp->blk_cksum; 212168404Spjd 213168404Spjd /* 214168404Spjd * Validate the checksummed log block. 215168404Spjd * 216168404Spjd * Sequence numbers should be... sequential. The checksum 217168404Spjd * verifier for the next block should be bp's checksum plus 1. 218168404Spjd * 219168404Spjd * Also check the log chain linkage and size used. 220168404Spjd */ 221168404Spjd cksum.zc_word[ZIL_ZC_SEQ]++; 222168404Spjd 223168404Spjd if (BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_ZILOG2) { 224168404Spjd zil_chain_t *zilc = abuf->b_data; 225168404Spjd char *lr = (char *)(zilc + 1); 226168404Spjd uint64_t len = zilc->zc_nused - sizeof (zil_chain_t); 227168404Spjd 228168404Spjd if (bcmp(&cksum, &zilc->zc_next_blk.blk_cksum, 229168404Spjd sizeof (cksum)) || BP_IS_HOLE(&zilc->zc_next_blk)) { 230168404Spjd error = SET_ERROR(ECKSUM); 231168404Spjd } else { 232168404Spjd ASSERT3U(len, <=, SPA_OLD_MAXBLOCKSIZE); 233168404Spjd bcopy(lr, dst, len); 234168404Spjd *end = (char *)dst + len; 235168404Spjd *nbp = zilc->zc_next_blk; 236168404Spjd } 237168404Spjd } else { 238168404Spjd char *lr = abuf->b_data; 239168404Spjd uint64_t size = BP_GET_LSIZE(bp); 240168404Spjd zil_chain_t *zilc = (zil_chain_t *)(lr + size) - 1; 241168404Spjd 242168404Spjd if (bcmp(&cksum, &zilc->zc_next_blk.blk_cksum, 243168404Spjd sizeof (cksum)) || BP_IS_HOLE(&zilc->zc_next_blk) || 244168404Spjd (zilc->zc_nused > (size - sizeof (*zilc)))) { 245168404Spjd error = SET_ERROR(ECKSUM); 246168404Spjd } else { 247168404Spjd ASSERT3U(zilc->zc_nused, <=, 248168404Spjd SPA_OLD_MAXBLOCKSIZE); 249168404Spjd bcopy(lr, dst, zilc->zc_nused); 250168404Spjd *end = (char *)dst + zilc->zc_nused; 251168404Spjd *nbp = zilc->zc_next_blk; 252168404Spjd } 253168404Spjd } 254168404Spjd 255168404Spjd VERIFY(arc_buf_remove_ref(abuf, &abuf)); 256168404Spjd } 257168404Spjd 258168404Spjd return (error); 259168404Spjd} 260168404Spjd 261168404Spjd/* 262168404Spjd * Read a TX_WRITE log data block. 263168404Spjd */ 264168404Spjdstatic int 265168404Spjdzil_read_log_data(zilog_t *zilog, const lr_write_t *lr, void *wbuf) 266168404Spjd{ 267168404Spjd enum zio_flag zio_flags = ZIO_FLAG_CANFAIL; 268168404Spjd const blkptr_t *bp = &lr->lr_blkptr; 269168404Spjd arc_flags_t aflags = ARC_FLAG_WAIT; 270168404Spjd arc_buf_t *abuf = NULL; 271168404Spjd zbookmark_phys_t zb; 272168404Spjd int error; 273168404Spjd 274168404Spjd if (BP_IS_HOLE(bp)) { 275168404Spjd if (wbuf != NULL) 276168404Spjd bzero(wbuf, MAX(BP_GET_LSIZE(bp), lr->lr_length)); 277168404Spjd return (0); 278168404Spjd } 279168404Spjd 280168404Spjd if (zilog->zl_header->zh_claim_txg == 0) 281168404Spjd zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB; 282168404Spjd 283168404Spjd SET_BOOKMARK(&zb, dmu_objset_id(zilog->zl_os), lr->lr_foid, 284168404Spjd ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp)); 285168404Spjd 286168404Spjd error = arc_read(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf, 287168404Spjd ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb); 288168404Spjd 289168404Spjd if (error == 0) { 290168404Spjd if (wbuf != NULL) 291168404Spjd bcopy(abuf->b_data, wbuf, arc_buf_size(abuf)); 292168404Spjd (void) arc_buf_remove_ref(abuf, &abuf); 293168404Spjd } 294168404Spjd 295168404Spjd return (error); 296168404Spjd} 297168404Spjd 298185029Spjd/* 299185029Spjd * Parse the intent log, and call parse_func for each valid record within. 300168404Spjd */ 301168404Spjdint 302168404Spjdzil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func, 303168404Spjd zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg) 304168404Spjd{ 305168404Spjd const zil_header_t *zh = zilog->zl_header; 306168404Spjd boolean_t claimed = !!zh->zh_claim_txg; 307168404Spjd uint64_t claim_blk_seq = claimed ? zh->zh_claim_blk_seq : UINT64_MAX; 308168404Spjd uint64_t claim_lr_seq = claimed ? zh->zh_claim_lr_seq : UINT64_MAX; 309168404Spjd uint64_t max_blk_seq = 0; 310168404Spjd uint64_t max_lr_seq = 0; 311168404Spjd uint64_t blk_count = 0; 312168404Spjd uint64_t lr_count = 0; 313168404Spjd blkptr_t blk, next_blk; 314168404Spjd char *lrbuf, *lrp; 315168404Spjd int error = 0; 316168404Spjd 317168404Spjd /* 318168404Spjd * Old logs didn't record the maximum zh_claim_lr_seq. 319168404Spjd */ 320168404Spjd if (!(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID)) 321168404Spjd claim_lr_seq = UINT64_MAX; 322168404Spjd 323168404Spjd /* 324168404Spjd * Starting at the block pointed to by zh_log we read the log chain. 325168404Spjd * For each block in the chain we strongly check that block to 326168404Spjd * ensure its validity. We stop when an invalid block is found. 327168404Spjd * For each block pointer in the chain we call parse_blk_func(). 328168404Spjd * For each record in each valid block we call parse_lr_func(). 329168404Spjd * If the log has been claimed, stop if we encounter a sequence 330168404Spjd * number greater than the highest claimed sequence number. 331168404Spjd */ 332168404Spjd lrbuf = zio_buf_alloc(SPA_OLD_MAXBLOCKSIZE); 333168404Spjd zil_bp_tree_init(zilog); 334168404Spjd 335168404Spjd for (blk = zh->zh_log; !BP_IS_HOLE(&blk); blk = next_blk) { 336168404Spjd uint64_t blk_seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 337168404Spjd int reclen; 338168404Spjd char *end; 339168404Spjd 340168404Spjd if (blk_seq > claim_blk_seq) 341168404Spjd break; 342168404Spjd if ((error = parse_blk_func(zilog, &blk, arg, txg)) != 0) 343168404Spjd break; 344168404Spjd ASSERT3U(max_blk_seq, <, blk_seq); 345168404Spjd max_blk_seq = blk_seq; 346168404Spjd blk_count++; 347168404Spjd 348168404Spjd if (max_lr_seq == claim_lr_seq && max_blk_seq == claim_blk_seq) 349168404Spjd break; 350168404Spjd 351168404Spjd error = zil_read_log_block(zilog, &blk, &next_blk, lrbuf, &end); 352168404Spjd if (error != 0) 353168404Spjd break; 354168404Spjd 355168404Spjd for (lrp = lrbuf; lrp < end; lrp += reclen) { 356168404Spjd lr_t *lr = (lr_t *)lrp; 357168404Spjd reclen = lr->lrc_reclen; 358168404Spjd ASSERT3U(reclen, >=, sizeof (lr_t)); 359168404Spjd if (lr->lrc_seq > claim_lr_seq) 360168404Spjd goto done; 361168404Spjd if ((error = parse_lr_func(zilog, lr, arg, txg)) != 0) 362168404Spjd goto done; 363168404Spjd ASSERT3U(max_lr_seq, <, lr->lrc_seq); 364168404Spjd max_lr_seq = lr->lrc_seq; 365168404Spjd lr_count++; 366168404Spjd } 367168404Spjd } 368168404Spjddone: 369168404Spjd zilog->zl_parse_error = error; 370168404Spjd zilog->zl_parse_blk_seq = max_blk_seq; 371168404Spjd zilog->zl_parse_lr_seq = max_lr_seq; 372168404Spjd zilog->zl_parse_blk_count = blk_count; 373168404Spjd zilog->zl_parse_lr_count = lr_count; 374168404Spjd 375168404Spjd ASSERT(!claimed || !(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID) || 376168404Spjd (max_blk_seq == claim_blk_seq && max_lr_seq == claim_lr_seq)); 377168404Spjd 378168404Spjd zil_bp_tree_fini(zilog); 379168404Spjd zio_buf_free(lrbuf, SPA_OLD_MAXBLOCKSIZE); 380168404Spjd 381168404Spjd return (error); 382168404Spjd} 383168404Spjd 384168404Spjdstatic int 385168404Spjdzil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg) 386168404Spjd{ 387168404Spjd /* 388168404Spjd * Claim log block if not already committed and not already claimed. 389168404Spjd * If tx == NULL, just verify that the block is claimable. 390168404Spjd */ 391168404Spjd if (BP_IS_HOLE(bp) || bp->blk_birth < first_txg || 392168404Spjd zil_bp_tree_add(zilog, bp) != 0) 393168404Spjd return (0); 394168404Spjd 395168404Spjd return (zio_wait(zio_claim(NULL, zilog->zl_spa, 396168404Spjd tx == NULL ? 0 : first_txg, bp, spa_claim_notify, NULL, 397168404Spjd ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB))); 398168404Spjd} 399168404Spjd 400168404Spjdstatic int 401168404Spjdzil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg) 402168404Spjd{ 403168404Spjd lr_write_t *lr = (lr_write_t *)lrc; 404168404Spjd int error; 405168404Spjd 406168404Spjd if (lrc->lrc_txtype != TX_WRITE) 407168404Spjd return (0); 408168404Spjd 409168404Spjd /* 410168404Spjd * If the block is not readable, don't claim it. This can happen 411168404Spjd * in normal operation when a log block is written to disk before 412168404Spjd * some of the dmu_sync() blocks it points to. In this case, the 413168404Spjd * transaction cannot have been committed to anyone (we would have 414168404Spjd * waited for all writes to be stable first), so it is semantically 415168404Spjd * correct to declare this the end of the log. 416168404Spjd */ 417168404Spjd if (lr->lr_blkptr.blk_birth >= first_txg && 418168404Spjd (error = zil_read_log_data(zilog, lr, NULL)) != 0) 419168404Spjd return (error); 420168404Spjd return (zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg)); 421168404Spjd} 422168404Spjd 423168404Spjd/* ARGSUSED */ 424168404Spjdstatic int 425168404Spjdzil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg) 426168404Spjd{ 427168404Spjd zio_free_zil(zilog->zl_spa, dmu_tx_get_txg(tx), bp); 428168404Spjd 429168404Spjd return (0); 430168404Spjd} 431168404Spjd 432168404Spjdstatic int 433168404Spjdzil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg) 434168404Spjd{ 435168404Spjd lr_write_t *lr = (lr_write_t *)lrc; 436168404Spjd blkptr_t *bp = &lr->lr_blkptr; 437168404Spjd 438168404Spjd /* 439185029Spjd * If we previously claimed it, we need to free it. 440185029Spjd */ 441185029Spjd if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE && 442185029Spjd bp->blk_birth >= claim_txg && zil_bp_tree_add(zilog, bp) == 0 && 443185029Spjd !BP_IS_HOLE(bp)) 444185029Spjd zio_free(zilog->zl_spa, dmu_tx_get_txg(tx), bp); 445185029Spjd 446185029Spjd return (0); 447185029Spjd} 448185029Spjd 449168404Spjdstatic lwb_t * 450168404Spjdzil_alloc_lwb(zilog_t *zilog, blkptr_t *bp, uint64_t txg) 451168404Spjd{ 452168404Spjd lwb_t *lwb; 453168404Spjd 454168404Spjd lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 455168404Spjd lwb->lwb_zilog = zilog; 456168404Spjd lwb->lwb_blk = *bp; 457168404Spjd lwb->lwb_buf = zio_buf_alloc(BP_GET_LSIZE(bp)); 458168404Spjd lwb->lwb_max_txg = txg; 459168404Spjd lwb->lwb_zio = NULL; 460168404Spjd lwb->lwb_tx = NULL; 461168404Spjd if (BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_ZILOG2) { 462168404Spjd lwb->lwb_nused = sizeof (zil_chain_t); 463168404Spjd lwb->lwb_sz = BP_GET_LSIZE(bp); 464168404Spjd } else { 465168404Spjd lwb->lwb_nused = 0; 466168404Spjd lwb->lwb_sz = BP_GET_LSIZE(bp) - sizeof (zil_chain_t); 467168404Spjd } 468168404Spjd 469168404Spjd mutex_enter(&zilog->zl_lock); 470168404Spjd list_insert_tail(&zilog->zl_lwb_list, lwb); 471168404Spjd mutex_exit(&zilog->zl_lock); 472185029Spjd 473168404Spjd return (lwb); 474185029Spjd} 475185029Spjd 476185029Spjd/* 477185029Spjd * Called when we create in-memory log transactions so that we know 478185029Spjd * to cleanup the itxs at the end of spa_sync(). 479185029Spjd */ 480185029Spjdvoid 481185029Spjdzilog_dirty(zilog_t *zilog, uint64_t txg) 482185029Spjd{ 483185029Spjd dsl_pool_t *dp = zilog->zl_dmu_pool; 484185029Spjd dsl_dataset_t *ds = dmu_objset_ds(zilog->zl_os); 485185029Spjd 486185029Spjd if (ds->ds_is_snapshot) 487185029Spjd panic("dirtying snapshot!"); 488168404Spjd 489168404Spjd if (txg_list_add(&dp->dp_dirty_zilogs, zilog, txg)) { 490185029Spjd /* up the hold count until we can be written out */ 491185029Spjd dmu_buf_add_ref(ds->ds_dbuf, zilog); 492185029Spjd } 493185029Spjd} 494185029Spjd 495185029Spjdboolean_t 496185029Spjdzilog_is_dirty(zilog_t *zilog) 497185029Spjd{ 498185029Spjd dsl_pool_t *dp = zilog->zl_dmu_pool; 499185029Spjd 500185029Spjd for (int t = 0; t < TXG_SIZE; t++) { 501185029Spjd if (txg_list_member(&dp->dp_dirty_zilogs, zilog, t)) 502185029Spjd return (B_TRUE); 503168404Spjd } 504168404Spjd return (B_FALSE); 505200724Sdelphij} 506200724Sdelphij 507200724Sdelphij/* 508200724Sdelphij * Create an on-disk intent log. 509200724Sdelphij */ 510200724Sdelphijstatic lwb_t * 511200724Sdelphijzil_create(zilog_t *zilog) 512200724Sdelphij{ 513200724Sdelphij const zil_header_t *zh = zilog->zl_header; 514200724Sdelphij lwb_t *lwb = NULL; 515200724Sdelphij uint64_t txg = 0; 516200724Sdelphij dmu_tx_t *tx = NULL; 517200724Sdelphij blkptr_t blk; 518200724Sdelphij int error = 0; 519200724Sdelphij 520200724Sdelphij /* 521200724Sdelphij * Wait for any previous destroy to complete. 522200724Sdelphij */ 523200724Sdelphij txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 524168404Spjd 525168404Spjd ASSERT(zh->zh_claim_txg == 0); 526168404Spjd ASSERT(zh->zh_replay_seq == 0); 527168404Spjd 528168404Spjd blk = zh->zh_log; 529168404Spjd 530168404Spjd /* 531168404Spjd * Allocate an initial log block if: 532168404Spjd * - there isn't one already 533168404Spjd * - the existing block is the wrong endianess 534185029Spjd */ 535168404Spjd if (BP_IS_HOLE(&blk) || BP_SHOULD_BYTESWAP(&blk)) { 536185029Spjd tx = dmu_tx_create(zilog->zl_os); 537168404Spjd VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0); 538168404Spjd dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 539168404Spjd txg = dmu_tx_get_txg(tx); 540168404Spjd 541168404Spjd if (!BP_IS_HOLE(&blk)) { 542168404Spjd zio_free_zil(zilog->zl_spa, txg, &blk); 543168404Spjd BP_ZERO(&blk); 544200724Sdelphij } 545200724Sdelphij 546200724Sdelphij error = zio_alloc_zil(zilog->zl_spa, txg, &blk, NULL, 547200724Sdelphij ZIL_MIN_BLKSZ, zilog->zl_logbias == ZFS_LOGBIAS_LATENCY); 548200724Sdelphij 549200724Sdelphij if (error == 0) 550200724Sdelphij zil_init_log_chain(zilog, &blk); 551200724Sdelphij } 552200724Sdelphij 553200724Sdelphij /* 554200724Sdelphij * Allocate a log write buffer (lwb) for the first log block. 555200724Sdelphij */ 556200724Sdelphij if (error == 0) 557200724Sdelphij lwb = zil_alloc_lwb(zilog, &blk, txg); 558200724Sdelphij 559168404Spjd /* 560168404Spjd * If we just allocated the first log block, commit our transaction 561168404Spjd * and wait for zil_sync() to stuff the block poiner into zh_log. 562168404Spjd * (zh is part of the MOS, so we cannot modify it in open context.) 563168404Spjd */ 564168404Spjd if (tx != NULL) { 565168404Spjd dmu_tx_commit(tx); 566168404Spjd txg_wait_synced(zilog->zl_dmu_pool, txg); 567168404Spjd } 568168404Spjd 569168404Spjd ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0); 570168404Spjd 571168404Spjd return (lwb); 572168404Spjd} 573168404Spjd 574168404Spjd/* 575168404Spjd * In one tx, free all log blocks and clear the log header. 576168404Spjd * If keep_first is set, then we're replaying a log with no content. 577168404Spjd * We want to keep the first block, however, so that the first 578185029Spjd * synchronous transaction doesn't require a txg_wait_synced() 579185029Spjd * in zil_create(). We don't need to txg_wait_synced() here either 580185029Spjd * when keep_first is set, because both zil_create() and zil_destroy() 581185029Spjd * will wait for any in-progress destroys to complete. 582185029Spjd */ 583185029Spjdvoid 584185029Spjdzil_destroy(zilog_t *zilog, boolean_t keep_first) 585185029Spjd{ 586168404Spjd const zil_header_t *zh = zilog->zl_header; 587185029Spjd lwb_t *lwb; 588185029Spjd dmu_tx_t *tx; 589185029Spjd uint64_t txg; 590185029Spjd 591185029Spjd /* 592185029Spjd * Wait for any previous destroy to complete. 593185029Spjd */ 594185029Spjd txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 595168404Spjd 596185029Spjd zilog->zl_old_header = *zh; /* debugging aid */ 597185029Spjd 598185029Spjd if (BP_IS_HOLE(&zh->zh_log)) 599185029Spjd return; 600185029Spjd 601168404Spjd tx = dmu_tx_create(zilog->zl_os); 602185029Spjd VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0); 603185029Spjd dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 604185029Spjd txg = dmu_tx_get_txg(tx); 605185029Spjd 606185029Spjd mutex_enter(&zilog->zl_lock); 607185029Spjd 608168404Spjd ASSERT3U(zilog->zl_destroy_txg, <, txg); 609185029Spjd zilog->zl_destroy_txg = txg; 610185029Spjd zilog->zl_keep_first = keep_first; 611185029Spjd 612185029Spjd if (!list_is_empty(&zilog->zl_lwb_list)) { 613185029Spjd ASSERT(zh->zh_claim_txg == 0); 614185029Spjd VERIFY(!keep_first); 615185029Spjd while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 616185029Spjd list_remove(&zilog->zl_lwb_list, lwb); 617185029Spjd if (lwb->lwb_buf != NULL) 618185029Spjd zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 619185029Spjd zio_free_zil(zilog->zl_spa, txg, &lwb->lwb_blk); 620185029Spjd kmem_cache_free(zil_lwb_cache, lwb); 621185029Spjd } 622185029Spjd } else if (!keep_first) { 623168404Spjd zil_destroy_sync(zilog, tx); 624168404Spjd } 625185029Spjd mutex_exit(&zilog->zl_lock); 626185029Spjd 627185029Spjd dmu_tx_commit(tx); 628185029Spjd} 629185029Spjd 630185029Spjdvoid 631185029Spjdzil_destroy_sync(zilog_t *zilog, dmu_tx_t *tx) 632185029Spjd{ 633185029Spjd ASSERT(list_is_empty(&zilog->zl_lwb_list)); 634185029Spjd (void) zil_parse(zilog, zil_free_log_block, 635185029Spjd zil_free_log_record, tx, zilog->zl_header->zh_claim_txg); 636185029Spjd} 637185029Spjd 638185029Spjdint 639185029Spjdzil_claim(const char *osname, void *txarg) 640185029Spjd{ 641185029Spjd dmu_tx_t *tx = txarg; 642185029Spjd uint64_t first_txg = dmu_tx_get_txg(tx); 643185029Spjd zilog_t *zilog; 644185029Spjd zil_header_t *zh; 645185029Spjd objset_t *os; 646185029Spjd int error; 647185029Spjd 648185029Spjd error = dmu_objset_own(osname, DMU_OST_ANY, B_FALSE, FTAG, &os); 649185029Spjd if (error != 0) { 650185029Spjd /* 651185029Spjd * EBUSY indicates that the objset is inconsistent, in which 652185029Spjd * case it can not have a ZIL. 653185029Spjd */ 654185029Spjd if (error != EBUSY) { 655185029Spjd cmn_err(CE_WARN, "can't open objset for %s, error %u", 656185029Spjd osname, error); 657185029Spjd } 658185029Spjd return (0); 659185029Spjd } 660185029Spjd 661185029Spjd zilog = dmu_objset_zil(os); 662185029Spjd zh = zil_header_in_syncing_context(zilog); 663185029Spjd 664185029Spjd if (spa_get_log_state(zilog->zl_spa) == SPA_LOG_CLEAR) { 665185029Spjd if (!BP_IS_HOLE(&zh->zh_log)) 666185029Spjd zio_free_zil(zilog->zl_spa, first_txg, &zh->zh_log); 667185029Spjd BP_ZERO(&zh->zh_log); 668185029Spjd dsl_dataset_dirty(dmu_objset_ds(os), tx); 669168404Spjd dmu_objset_disown(os, FTAG); 670185029Spjd return (0); 671168404Spjd } 672185029Spjd 673185029Spjd /* 674185029Spjd * Claim all log blocks if we haven't already done so, and remember 675185029Spjd * the highest claimed sequence number. This ensures that if we can 676185029Spjd * read only part of the log now (e.g. due to a missing device), 677168404Spjd * but we can read the entire log later, we will not try to replay 678185029Spjd * or destroy beyond the last block we successfully claimed. 679185029Spjd */ 680168404Spjd ASSERT3U(zh->zh_claim_txg, <=, first_txg); 681185029Spjd if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) { 682168404Spjd (void) zil_parse(zilog, zil_claim_log_block, 683185029Spjd zil_claim_log_record, tx, first_txg); 684185029Spjd zh->zh_claim_txg = first_txg; 685185029Spjd zh->zh_claim_blk_seq = zilog->zl_parse_blk_seq; 686185029Spjd zh->zh_claim_lr_seq = zilog->zl_parse_lr_seq; 687185029Spjd if (zilog->zl_parse_lr_count || zilog->zl_parse_blk_count > 1) 688185029Spjd zh->zh_flags |= ZIL_REPLAY_NEEDED; 689185029Spjd zh->zh_flags |= ZIL_CLAIM_LR_SEQ_VALID; 690185029Spjd dsl_dataset_dirty(dmu_objset_ds(os), tx); 691185029Spjd } 692185029Spjd 693185029Spjd ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1)); 694185029Spjd dmu_objset_disown(os, FTAG); 695185029Spjd return (0); 696185029Spjd} 697185029Spjd 698168404Spjd/* 699168404Spjd * Check the log by walking the log chain. 700168404Spjd * Checksum errors are ok as they indicate the end of the chain. 701168404Spjd * Any other error (no device or read failure) returns an error. 702168404Spjd */ 703168404Spjdint 704185029Spjdzil_check_log_chain(const char *osname, void *tx) 705185029Spjd{ 706185029Spjd zilog_t *zilog; 707185029Spjd objset_t *os; 708168404Spjd blkptr_t *bp; 709168404Spjd int error; 710168404Spjd 711185029Spjd ASSERT(tx == NULL); 712185029Spjd 713185029Spjd error = dmu_objset_hold(osname, FTAG, &os); 714185029Spjd if (error != 0) { 715185029Spjd cmn_err(CE_WARN, "can't open objset for %s", osname); 716185029Spjd return (0); 717185029Spjd } 718185029Spjd 719185029Spjd zilog = dmu_objset_zil(os); 720185029Spjd bp = (blkptr_t *)&zilog->zl_header->zh_log; 721185029Spjd 722185029Spjd /* 723185029Spjd * Check the first block and determine if it's on a log device 724185029Spjd * which may have been removed or faulted prior to loading this 725185029Spjd * pool. If so, there's no point in checking the rest of the log 726185029Spjd * as its content should have already been synced to the pool. 727168404Spjd */ 728168404Spjd if (!BP_IS_HOLE(bp)) { 729168404Spjd vdev_t *vd; 730168404Spjd boolean_t valid = B_TRUE; 731168404Spjd 732168404Spjd spa_config_enter(os->os_spa, SCL_STATE, FTAG, RW_READER); 733185029Spjd vd = vdev_lookup_top(os->os_spa, DVA_GET_VDEV(&bp->blk_dva[0])); 734185029Spjd if (vd->vdev_islog && vdev_is_dead(vd)) 735185029Spjd valid = vdev_log_state_valid(vd); 736168404Spjd spa_config_exit(os->os_spa, SCL_STATE, FTAG); 737168404Spjd 738168404Spjd if (!valid) { 739168404Spjd dmu_objset_rele(os, FTAG); 740168404Spjd return (0); 741168404Spjd } 742168404Spjd } 743168404Spjd 744168404Spjd /* 745168404Spjd * Because tx == NULL, zil_claim_log_block() will not actually claim 746168404Spjd * any blocks, but just determine whether it is possible to do so. 747185029Spjd * In addition to checking the log chain, zil_claim_log_block() 748185029Spjd * will invoke zio_claim() with a done func of spa_claim_notify(), 749185029Spjd * which will update spa_max_claim_txg. See spa_load() for details. 750185029Spjd */ 751185029Spjd error = zil_parse(zilog, zil_claim_log_block, zil_claim_log_record, tx, 752185029Spjd zilog->zl_header->zh_claim_txg ? -1ULL : spa_first_txg(os->os_spa)); 753185029Spjd 754185029Spjd dmu_objset_rele(os, FTAG); 755185029Spjd 756168404Spjd return ((error == ECKSUM || error == ENOENT) ? 0 : error); 757168404Spjd} 758168404Spjd 759168404Spjdstatic int 760168404Spjdzil_vdev_compare(const void *x1, const void *x2) 761168404Spjd{ 762168404Spjd const uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev; 763168404Spjd const uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev; 764168404Spjd 765168404Spjd if (v1 < v2) 766185029Spjd return (-1); 767168404Spjd if (v1 > v2) 768168404Spjd return (1); 769168404Spjd 770168404Spjd return (0); 771168404Spjd} 772168404Spjd 773168404Spjdvoid 774168404Spjdzil_add_block(zilog_t *zilog, const blkptr_t *bp) 775168404Spjd{ 776168404Spjd avl_tree_t *t = &zilog->zl_vdev_tree; 777168404Spjd avl_index_t where; 778168404Spjd zil_vdev_node_t *zv, zvsearch; 779168404Spjd int ndvas = BP_GET_NDVAS(bp); 780168404Spjd int i; 781168404Spjd 782168404Spjd if (zfs_nocacheflush) 783168404Spjd return; 784168404Spjd 785168404Spjd ASSERT(zilog->zl_writer); 786168404Spjd 787168404Spjd /* 788168404Spjd * Even though we're zl_writer, we still need a lock because the 789168404Spjd * zl_get_data() callbacks may have dmu_sync() done callbacks 790185029Spjd * that will run concurrently. 791168404Spjd */ 792185029Spjd mutex_enter(&zilog->zl_vdev_lock); 793168404Spjd for (i = 0; i < ndvas; i++) { 794168404Spjd zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]); 795168404Spjd if (avl_find(t, &zvsearch, &where) == NULL) { 796168404Spjd zv = kmem_alloc(sizeof (*zv), KM_SLEEP); 797168404Spjd zv->zv_vdev = zvsearch.zv_vdev; 798168404Spjd avl_insert(t, zv, where); 799168404Spjd } 800168404Spjd } 801168404Spjd mutex_exit(&zilog->zl_vdev_lock); 802168404Spjd} 803168404Spjd 804168404Spjdstatic void 805168404Spjdzil_flush_vdevs(zilog_t *zilog) 806168404Spjd{ 807168404Spjd spa_t *spa = zilog->zl_spa; 808168404Spjd avl_tree_t *t = &zilog->zl_vdev_tree; 809168404Spjd void *cookie = NULL; 810168404Spjd zil_vdev_node_t *zv; 811168404Spjd zio_t *zio; 812168404Spjd 813168404Spjd ASSERT(zilog->zl_writer); 814168404Spjd 815168404Spjd /* 816168404Spjd * We don't need zl_vdev_lock here because we're the zl_writer, 817168404Spjd * and all zl_get_data() callbacks are done. 818168404Spjd */ 819168404Spjd if (avl_numnodes(t) == 0) 820168404Spjd return; 821168404Spjd 822168404Spjd spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 823168404Spjd 824168404Spjd zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); 825168404Spjd 826168404Spjd while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) { 827168404Spjd vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev); 828168404Spjd if (vd != NULL) 829168404Spjd zio_flush(zio, vd); 830168404Spjd kmem_free(zv, sizeof (*zv)); 831168404Spjd } 832168404Spjd 833168404Spjd /* 834168404Spjd * Wait for all the flushes to complete. Not all devices actually 835168404Spjd * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails. 836168404Spjd */ 837168404Spjd (void) zio_wait(zio); 838168404Spjd 839168404Spjd spa_config_exit(spa, SCL_STATE, FTAG); 840168404Spjd} 841168404Spjd 842168404Spjd/* 843168404Spjd * Function called when a log block write completes 844168404Spjd */ 845168404Spjdstatic void 846168404Spjdzil_lwb_write_done(zio_t *zio) 847168404Spjd{ 848168404Spjd lwb_t *lwb = zio->io_private; 849168404Spjd zilog_t *zilog = lwb->lwb_zilog; 850168404Spjd dmu_tx_t *tx = lwb->lwb_tx; 851168404Spjd 852168404Spjd ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF); 853168404Spjd ASSERT(BP_GET_TYPE(zio->io_bp) == DMU_OT_INTENT_LOG); 854168404Spjd ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 855168404Spjd ASSERT(BP_GET_BYTEORDER(zio->io_bp) == ZFS_HOST_BYTEORDER); 856168404Spjd ASSERT(!BP_IS_GANG(zio->io_bp)); 857168404Spjd ASSERT(!BP_IS_HOLE(zio->io_bp)); 858168404Spjd ASSERT(BP_GET_FILL(zio->io_bp) == 0); 859168404Spjd 860168404Spjd /* 861168404Spjd * Ensure the lwb buffer pointer is cleared before releasing 862168404Spjd * the txg. If we have had an allocation failure and 863168404Spjd * the txg is waiting to sync then we want want zil_sync() 864168404Spjd * to remove the lwb so that it's not picked up as the next new 865168404Spjd * one in zil_commit_writer(). zil_sync() will only remove 866168404Spjd * the lwb if lwb_buf is null. 867168404Spjd */ 868168404Spjd zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 869168404Spjd mutex_enter(&zilog->zl_lock); 870168404Spjd lwb->lwb_buf = NULL; 871168404Spjd lwb->lwb_tx = NULL; 872168404Spjd mutex_exit(&zilog->zl_lock); 873168404Spjd 874168404Spjd /* 875168404Spjd * Now that we've written this log block, we have a stable pointer 876168404Spjd * to the next block in the chain, so it's OK to let the txg in 877168404Spjd * which we allocated the next block sync. 878168404Spjd */ 879168404Spjd dmu_tx_commit(tx); 880168404Spjd} 881168404Spjd 882168404Spjd/* 883168404Spjd * Initialize the io for a log block. 884168404Spjd */ 885168404Spjdstatic void 886168404Spjdzil_lwb_write_init(zilog_t *zilog, lwb_t *lwb) 887168404Spjd{ 888168404Spjd zbookmark_phys_t zb; 889168404Spjd 890168404Spjd SET_BOOKMARK(&zb, lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET], 891168404Spjd ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, 892185029Spjd lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ]); 893185029Spjd 894168404Spjd if (zilog->zl_root_zio == NULL) { 895168404Spjd zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL, 896168404Spjd ZIO_FLAG_CANFAIL); 897168404Spjd } 898168404Spjd if (lwb->lwb_zio == NULL) { 899168404Spjd lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa, 900168404Spjd 0, &lwb->lwb_blk, lwb->lwb_buf, BP_GET_LSIZE(&lwb->lwb_blk), 901168404Spjd zil_lwb_write_done, lwb, ZIO_PRIORITY_SYNC_WRITE, 902168404Spjd ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE, &zb); 903168404Spjd } 904168404Spjd} 905168404Spjd 906168404Spjd/* 907168404Spjd * Define a limited set of intent log block sizes. 908168404Spjd * 909168404Spjd * These must be a multiple of 4KB. Note only the amount used (again 910168404Spjd * aligned to 4KB) actually gets written. However, we can't always just 911168404Spjd * allocate SPA_OLD_MAXBLOCKSIZE as the slog space could be exhausted. 912168404Spjd */ 913168404Spjduint64_t zil_block_buckets[] = { 914168404Spjd 4096, /* non TX_WRITE */ 915168404Spjd 8192+4096, /* data base */ 916168404Spjd 32*1024 + 4096, /* NFS writes */ 917168404Spjd UINT64_MAX 918168404Spjd}; 919168404Spjd 920168404Spjd/* 921168404Spjd * Use the slog as long as the logbias is 'latency' and the current commit size 922168404Spjd * is less than the limit or the total list size is less than 2X the limit. 923168404Spjd * Limit checking is disabled by setting zil_slog_limit to UINT64_MAX. 924168404Spjd */ 925168404Spjduint64_t zil_slog_limit = 1024 * 1024; 926168404Spjd#define USE_SLOG(zilog) (((zilog)->zl_logbias == ZFS_LOGBIAS_LATENCY) && \ 927168404Spjd (((zilog)->zl_cur_used < zil_slog_limit) || \ 928168404Spjd ((zilog)->zl_itx_list_sz < (zil_slog_limit << 1)))) 929168404Spjd 930168404Spjd/* 931168404Spjd * Start a log block write and advance to the next log block. 932168404Spjd * Calls are serialized. 933168404Spjd */ 934168404Spjdstatic lwb_t * 935168404Spjdzil_lwb_write_start(zilog_t *zilog, lwb_t *lwb) 936168404Spjd{ 937168404Spjd lwb_t *nlwb = NULL; 938168404Spjd zil_chain_t *zilc; 939168404Spjd spa_t *spa = zilog->zl_spa; 940168404Spjd blkptr_t *bp; 941168404Spjd dmu_tx_t *tx; 942168404Spjd uint64_t txg; 943168404Spjd uint64_t zil_blksz, wsz; 944168404Spjd int i, error; 945168404Spjd 946168404Spjd if (BP_GET_CHECKSUM(&lwb->lwb_blk) == ZIO_CHECKSUM_ZILOG2) { 947168404Spjd zilc = (zil_chain_t *)lwb->lwb_buf; 948168404Spjd bp = &zilc->zc_next_blk; 949168404Spjd } else { 950168404Spjd zilc = (zil_chain_t *)(lwb->lwb_buf + lwb->lwb_sz); 951168404Spjd bp = &zilc->zc_next_blk; 952168404Spjd } 953168404Spjd 954168404Spjd ASSERT(lwb->lwb_nused <= lwb->lwb_sz); 955168404Spjd 956168404Spjd /* 957168404Spjd * Allocate the next block and save its address in this block 958168404Spjd * before writing it in order to establish the log chain. 959168404Spjd * Note that if the allocation of nlwb synced before we wrote 960168404Spjd * the block that points at it (lwb), we'd leak it if we crashed. 961168404Spjd * Therefore, we don't do dmu_tx_commit() until zil_lwb_write_done(). 962168404Spjd * We dirty the dataset to ensure that zil_sync() will be called 963168404Spjd * to clean up in the event of allocation failure or I/O failure. 964168404Spjd */ 965168404Spjd tx = dmu_tx_create(zilog->zl_os); 966168404Spjd VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0); 967168404Spjd dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 968168404Spjd txg = dmu_tx_get_txg(tx); 969168404Spjd 970168404Spjd lwb->lwb_tx = tx; 971168404Spjd 972168404Spjd /* 973168404Spjd * Log blocks are pre-allocated. Here we select the size of the next 974168404Spjd * block, based on size used in the last block. 975168404Spjd * - first find the smallest bucket that will fit the block from a 976168404Spjd * limited set of block sizes. This is because it's faster to write 977168404Spjd * blocks allocated from the same metaslab as they are adjacent or 978168404Spjd * close. 979168404Spjd * - next find the maximum from the new suggested size and an array of 980168404Spjd * previous sizes. This lessens a picket fence effect of wrongly 981168404Spjd * guesssing the size if we have a stream of say 2k, 64k, 2k, 64k 982168404Spjd * requests. 983168404Spjd * 984168404Spjd * Note we only write what is used, but we can't just allocate 985168404Spjd * the maximum block size because we can exhaust the available 986168404Spjd * pool log space. 987168404Spjd */ 988168404Spjd zil_blksz = zilog->zl_cur_used + sizeof (zil_chain_t); 989185029Spjd for (i = 0; zil_blksz > zil_block_buckets[i]; i++) 990168404Spjd continue; 991168404Spjd zil_blksz = zil_block_buckets[i]; 992168404Spjd if (zil_blksz == UINT64_MAX) 993168404Spjd zil_blksz = SPA_OLD_MAXBLOCKSIZE; 994168404Spjd zilog->zl_prev_blks[zilog->zl_prev_rotor] = zil_blksz; 995168404Spjd for (i = 0; i < ZIL_PREV_BLKS; i++) 996168404Spjd zil_blksz = MAX(zil_blksz, zilog->zl_prev_blks[i]); 997168404Spjd zilog->zl_prev_rotor = (zilog->zl_prev_rotor + 1) & (ZIL_PREV_BLKS - 1); 998185029Spjd 999168404Spjd BP_ZERO(bp); 1000168404Spjd /* pass the old blkptr in order to spread log blocks across devs */ 1001168404Spjd error = zio_alloc_zil(spa, txg, bp, &lwb->lwb_blk, zil_blksz, 1002168404Spjd USE_SLOG(zilog)); 1003168404Spjd if (error == 0) { 1004168404Spjd ASSERT3U(bp->blk_birth, ==, txg); 1005168404Spjd bp->blk_cksum = lwb->lwb_blk.blk_cksum; 1006168404Spjd bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++; 1007168404Spjd 1008168404Spjd /* 1009168404Spjd * Allocate a new log write buffer (lwb). 1010168404Spjd */ 1011168404Spjd nlwb = zil_alloc_lwb(zilog, bp, txg); 1012168404Spjd 1013185029Spjd /* Record the block for later vdev flushing */ 1014168404Spjd zil_add_block(zilog, &lwb->lwb_blk); 1015168404Spjd } 1016168404Spjd 1017168404Spjd if (BP_GET_CHECKSUM(&lwb->lwb_blk) == ZIO_CHECKSUM_ZILOG2) { 1018168404Spjd /* For Slim ZIL only write what is used. */ 1019168404Spjd wsz = P2ROUNDUP_TYPED(lwb->lwb_nused, ZIL_MIN_BLKSZ, uint64_t); 1020168404Spjd ASSERT3U(wsz, <=, lwb->lwb_sz); 1021168404Spjd zio_shrink(lwb->lwb_zio, wsz); 1022168404Spjd 1023168404Spjd } else { 1024168404Spjd wsz = lwb->lwb_sz; 1025168404Spjd } 1026168404Spjd 1027168404Spjd zilc->zc_pad = 0; 1028168404Spjd zilc->zc_nused = lwb->lwb_nused; 1029168404Spjd zilc->zc_eck.zec_cksum = lwb->lwb_blk.blk_cksum; 1030168404Spjd 1031168404Spjd /* 1032168404Spjd * clear unused data for security 1033168404Spjd */ 1034168404Spjd bzero(lwb->lwb_buf + lwb->lwb_nused, wsz - lwb->lwb_nused); 1035168404Spjd 1036168404Spjd zio_nowait(lwb->lwb_zio); /* Kick off the write for the old log block */ 1037168404Spjd 1038168404Spjd /* 1039168404Spjd * If there was an allocation failure then nlwb will be null which 1040168404Spjd * forces a txg_wait_synced(). 1041168404Spjd */ 1042168404Spjd return (nlwb); 1043168404Spjd} 1044168404Spjd 1045168404Spjdstatic lwb_t * 1046168404Spjdzil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb) 1047168404Spjd{ 1048168404Spjd lr_t *lrc = &itx->itx_lr; /* common log record */ 1049185029Spjd lr_write_t *lrw = (lr_write_t *)lrc; 1050168404Spjd char *lr_buf; 1051168404Spjd uint64_t txg = lrc->lrc_txg; 1052168404Spjd uint64_t reclen = lrc->lrc_reclen; 1053168404Spjd uint64_t dlen = 0; 1054168404Spjd 1055168404Spjd if (lwb == NULL) 1056168404Spjd return (NULL); 1057168404Spjd 1058168404Spjd ASSERT(lwb->lwb_buf != NULL); 1059168404Spjd ASSERT(zilog_is_dirty(zilog) || 1060168404Spjd spa_freeze_txg(zilog->zl_spa) != UINT64_MAX); 1061168404Spjd 1062168404Spjd if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY) 1063168404Spjd dlen = P2ROUNDUP_TYPED( 1064168404Spjd lrw->lr_length, sizeof (uint64_t), uint64_t); 1065168404Spjd 1066168404Spjd zilog->zl_cur_used += (reclen + dlen); 1067168404Spjd 1068168404Spjd zil_lwb_write_init(zilog, lwb); 1069168404Spjd 1070168404Spjd /* 1071168404Spjd * If this record won't fit in the current log block, start a new one. 1072168404Spjd */ 1073168404Spjd if (lwb->lwb_nused + reclen + dlen > lwb->lwb_sz) { 1074168404Spjd lwb = zil_lwb_write_start(zilog, lwb); 1075168404Spjd if (lwb == NULL) 1076168404Spjd return (NULL); 1077168404Spjd zil_lwb_write_init(zilog, lwb); 1078191900Skmacy ASSERT(LWB_EMPTY(lwb)); 1079168404Spjd if (lwb->lwb_nused + reclen + dlen > lwb->lwb_sz) { 1080168404Spjd txg_wait_synced(zilog->zl_dmu_pool, txg); 1081168404Spjd return (lwb); 1082168404Spjd } 1083185029Spjd } 1084168404Spjd 1085168404Spjd lr_buf = lwb->lwb_buf + lwb->lwb_nused; 1086168404Spjd bcopy(lrc, lr_buf, reclen); 1087168404Spjd lrc = (lr_t *)lr_buf; 1088168404Spjd lrw = (lr_write_t *)lrc; 1089168404Spjd 1090168404Spjd /* 1091168404Spjd * If it's a write, fetch the data or get its blkptr as appropriate. 1092168404Spjd */ 1093185029Spjd if (lrc->lrc_txtype == TX_WRITE) { 1094168404Spjd if (txg > spa_freeze_txg(zilog->zl_spa)) 1095168404Spjd txg_wait_synced(zilog->zl_dmu_pool, txg); 1096168404Spjd if (itx->itx_wr_state != WR_COPIED) { 1097168404Spjd char *dbuf; 1098168404Spjd int error; 1099168404Spjd 1100168404Spjd if (dlen) { 1101168404Spjd ASSERT(itx->itx_wr_state == WR_NEED_COPY); 1102168404Spjd dbuf = lr_buf + reclen; 1103168404Spjd lrw->lr_common.lrc_reclen += dlen; 1104168404Spjd } else { 1105168404Spjd ASSERT(itx->itx_wr_state == WR_INDIRECT); 1106168404Spjd dbuf = NULL; 1107168404Spjd } 1108168404Spjd error = zilog->zl_get_data( 1109168404Spjd itx->itx_private, lrw, dbuf, lwb->lwb_zio); 1110168404Spjd if (error == EIO) { 1111168404Spjd txg_wait_synced(zilog->zl_dmu_pool, txg); 1112168404Spjd return (lwb); 1113168404Spjd } 1114168404Spjd if (error != 0) { 1115168404Spjd ASSERT(error == ENOENT || error == EEXIST || 1116168404Spjd error == EALREADY); 1117168404Spjd return (lwb); 1118168404Spjd } 1119168404Spjd } 1120168404Spjd } 1121168404Spjd 1122168404Spjd /* 1123168404Spjd * We're actually making an entry, so update lrc_seq to be the 1124168404Spjd * log record sequence number. Note that this is generally not 1125168404Spjd * equal to the itx sequence number because not all transactions 1126168404Spjd * are synchronous, and sometimes spa_sync() gets there first. 1127168404Spjd */ 1128168404Spjd lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */ 1129168404Spjd lwb->lwb_nused += reclen + dlen; 1130168404Spjd lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg); 1131168404Spjd ASSERT3U(lwb->lwb_nused, <=, lwb->lwb_sz); 1132168404Spjd ASSERT0(P2PHASE(lwb->lwb_nused, sizeof (uint64_t))); 1133168404Spjd 1134168404Spjd return (lwb); 1135168404Spjd} 1136168404Spjd 1137168404Spjditx_t * 1138168404Spjdzil_itx_create(uint64_t txtype, size_t lrsize) 1139168404Spjd{ 1140168404Spjd itx_t *itx; 1141168404Spjd 1142168404Spjd lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t); 1143168404Spjd 1144168404Spjd itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP); 1145168404Spjd itx->itx_lr.lrc_txtype = txtype; 1146168404Spjd itx->itx_lr.lrc_reclen = lrsize; 1147168404Spjd itx->itx_sod = lrsize; /* if write & WR_NEED_COPY will be increased */ 1148168404Spjd itx->itx_lr.lrc_seq = 0; /* defensive */ 1149168404Spjd itx->itx_sync = B_TRUE; /* default is synchronous */ 1150168404Spjd 1151168404Spjd return (itx); 1152185029Spjd} 1153168404Spjd 1154168404Spjdvoid 1155168404Spjdzil_itx_destroy(itx_t *itx) 1156168404Spjd{ 1157168404Spjd kmem_free(itx, offsetof(itx_t, itx_lr) + itx->itx_lr.lrc_reclen); 1158168404Spjd} 1159168404Spjd 1160168404Spjd/* 1161168404Spjd * Free up the sync and async itxs. The itxs_t has already been detached 1162168404Spjd * so no locks are needed. 1163168404Spjd */ 1164185029Spjdstatic void 1165168404Spjdzil_itxg_clean(itxs_t *itxs) 1166168404Spjd{ 1167168404Spjd itx_t *itx; 1168168404Spjd list_t *list; 1169168404Spjd avl_tree_t *t; 1170168404Spjd void *cookie; 1171168404Spjd itx_async_node_t *ian; 1172168404Spjd 1173168404Spjd list = &itxs->i_sync_list; 1174168404Spjd while ((itx = list_head(list)) != NULL) { 1175168404Spjd list_remove(list, itx); 1176168404Spjd kmem_free(itx, offsetof(itx_t, itx_lr) + 1177168404Spjd itx->itx_lr.lrc_reclen); 1178168404Spjd } 1179168404Spjd 1180168404Spjd cookie = NULL; 1181168404Spjd t = &itxs->i_async_tree; 1182168404Spjd while ((ian = avl_destroy_nodes(t, &cookie)) != NULL) { 1183168404Spjd list = &ian->ia_list; 1184168404Spjd while ((itx = list_head(list)) != NULL) { 1185168404Spjd list_remove(list, itx); 1186168404Spjd kmem_free(itx, offsetof(itx_t, itx_lr) + 1187168404Spjd itx->itx_lr.lrc_reclen); 1188168404Spjd } 1189168404Spjd list_destroy(list); 1190168404Spjd kmem_free(ian, sizeof (itx_async_node_t)); 1191168404Spjd } 1192168404Spjd avl_destroy(t); 1193168404Spjd 1194168404Spjd kmem_free(itxs, sizeof (itxs_t)); 1195168404Spjd} 1196168404Spjd 1197168404Spjdstatic int 1198185029Spjdzil_aitx_compare(const void *x1, const void *x2) 1199168404Spjd{ 1200185029Spjd const uint64_t o1 = ((itx_async_node_t *)x1)->ia_foid; 1201168404Spjd const uint64_t o2 = ((itx_async_node_t *)x2)->ia_foid; 1202168404Spjd 1203168404Spjd if (o1 < o2) 1204168404Spjd return (-1); 1205168404Spjd if (o1 > o2) 1206168404Spjd return (1); 1207168404Spjd 1208168404Spjd return (0); 1209168404Spjd} 1210168404Spjd 1211168404Spjd/* 1212168404Spjd * Remove all async itx with the given oid. 1213168404Spjd */ 1214168404Spjdstatic void 1215168404Spjdzil_remove_async(zilog_t *zilog, uint64_t oid) 1216168404Spjd{ 1217168404Spjd uint64_t otxg, txg; 1218168404Spjd itx_async_node_t *ian; 1219168404Spjd avl_tree_t *t; 1220168404Spjd avl_index_t where; 1221168404Spjd list_t clean_list; 1222168404Spjd itx_t *itx; 1223168404Spjd 1224168404Spjd ASSERT(oid != 0); 1225168404Spjd list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node)); 1226168404Spjd 1227168404Spjd if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */ 1228168404Spjd otxg = ZILTEST_TXG; 1229168404Spjd else 1230168404Spjd otxg = spa_last_synced_txg(zilog->zl_spa) + 1; 1231168404Spjd 1232168404Spjd for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) { 1233168404Spjd itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK]; 1234168404Spjd 1235168404Spjd mutex_enter(&itxg->itxg_lock); 1236168404Spjd if (itxg->itxg_txg != txg) { 1237168404Spjd mutex_exit(&itxg->itxg_lock); 1238168404Spjd continue; 1239168404Spjd } 1240168404Spjd 1241168404Spjd /* 1242168404Spjd * Locate the object node and append its list. 1243168404Spjd */ 1244168404Spjd t = &itxg->itxg_itxs->i_async_tree; 1245168404Spjd ian = avl_find(t, &oid, &where); 1246168404Spjd if (ian != NULL) 1247168404Spjd list_move_tail(&clean_list, &ian->ia_list); 1248168404Spjd mutex_exit(&itxg->itxg_lock); 1249168404Spjd } 1250168404Spjd while ((itx = list_head(&clean_list)) != NULL) { 1251168404Spjd list_remove(&clean_list, itx); 1252168404Spjd kmem_free(itx, offsetof(itx_t, itx_lr) + 1253168404Spjd itx->itx_lr.lrc_reclen); 1254168404Spjd } 1255168404Spjd list_destroy(&clean_list); 1256168404Spjd} 1257168404Spjd 1258168404Spjdvoid 1259168404Spjdzil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx) 1260168404Spjd{ 1261168404Spjd uint64_t txg; 1262168404Spjd itxg_t *itxg; 1263168404Spjd itxs_t *itxs, *clean = NULL; 1264168404Spjd 1265168404Spjd /* 1266168404Spjd * Object ids can be re-instantiated in the next txg so 1267168404Spjd * remove any async transactions to avoid future leaks. 1268168404Spjd * This can happen if a fsync occurs on the re-instantiated 1269168404Spjd * object for a WR_INDIRECT or WR_NEED_COPY write, which gets 1270168404Spjd * the new file data and flushes a write record for the old object. 1271168404Spjd */ 1272168404Spjd if ((itx->itx_lr.lrc_txtype & ~TX_CI) == TX_REMOVE) 1273168404Spjd zil_remove_async(zilog, itx->itx_oid); 1274168404Spjd 1275168404Spjd /* 1276168404Spjd * Ensure the data of a renamed file is committed before the rename. 1277168404Spjd */ 1278168404Spjd if ((itx->itx_lr.lrc_txtype & ~TX_CI) == TX_RENAME) 1279168404Spjd zil_async_to_sync(zilog, itx->itx_oid); 1280168404Spjd 1281168404Spjd if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) 1282168404Spjd txg = ZILTEST_TXG; 1283168404Spjd else 1284168404Spjd txg = dmu_tx_get_txg(tx); 1285168404Spjd 1286168404Spjd itxg = &zilog->zl_itxg[txg & TXG_MASK]; 1287168404Spjd mutex_enter(&itxg->itxg_lock); 1288168404Spjd itxs = itxg->itxg_itxs; 1289168404Spjd if (itxg->itxg_txg != txg) { 1290168404Spjd if (itxs != NULL) { 1291168404Spjd /* 1292168404Spjd * The zil_clean callback hasn't got around to cleaning 1293168404Spjd * this itxg. Save the itxs for release below. 1294168404Spjd * This should be rare. 1295168404Spjd */ 1296168404Spjd atomic_add_64(&zilog->zl_itx_list_sz, -itxg->itxg_sod); 1297168404Spjd itxg->itxg_sod = 0; 1298168404Spjd clean = itxg->itxg_itxs; 1299168404Spjd } 1300168404Spjd ASSERT(itxg->itxg_sod == 0); 1301168404Spjd itxg->itxg_txg = txg; 1302168404Spjd itxs = itxg->itxg_itxs = kmem_zalloc(sizeof (itxs_t), KM_SLEEP); 1303168404Spjd 1304168404Spjd list_create(&itxs->i_sync_list, sizeof (itx_t), 1305168404Spjd offsetof(itx_t, itx_node)); 1306168404Spjd avl_create(&itxs->i_async_tree, zil_aitx_compare, 1307168404Spjd sizeof (itx_async_node_t), 1308168404Spjd offsetof(itx_async_node_t, ia_node)); 1309168404Spjd } 1310168404Spjd if (itx->itx_sync) { 1311168404Spjd list_insert_tail(&itxs->i_sync_list, itx); 1312168404Spjd atomic_add_64(&zilog->zl_itx_list_sz, itx->itx_sod); 1313168404Spjd itxg->itxg_sod += itx->itx_sod; 1314168404Spjd } else { 1315168404Spjd avl_tree_t *t = &itxs->i_async_tree; 1316168404Spjd uint64_t foid = ((lr_ooo_t *)&itx->itx_lr)->lr_foid; 1317168404Spjd itx_async_node_t *ian; 1318168404Spjd avl_index_t where; 1319168404Spjd 1320168404Spjd ian = avl_find(t, &foid, &where); 1321168404Spjd if (ian == NULL) { 1322168404Spjd ian = kmem_alloc(sizeof (itx_async_node_t), KM_SLEEP); 1323168404Spjd list_create(&ian->ia_list, sizeof (itx_t), 1324168404Spjd offsetof(itx_t, itx_node)); 1325168404Spjd ian->ia_foid = foid; 1326168404Spjd avl_insert(t, ian, where); 1327168404Spjd } 1328168404Spjd list_insert_tail(&ian->ia_list, itx); 1329168404Spjd } 1330168404Spjd 1331168404Spjd itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx); 1332168404Spjd zilog_dirty(zilog, txg); 1333168404Spjd mutex_exit(&itxg->itxg_lock); 1334168404Spjd 1335168404Spjd /* Release the old itxs now we've dropped the lock */ 1336168404Spjd if (clean != NULL) 1337168404Spjd zil_itxg_clean(clean); 1338168404Spjd} 1339168404Spjd 1340168404Spjd/* 1341168404Spjd * If there are any in-memory intent log transactions which have now been 1342185029Spjd * synced then start up a taskq to free them. We should only do this after we 1343168404Spjd * have written out the uberblocks (i.e. txg has been comitted) so that 1344185029Spjd * don't inadvertently clean out in-memory log records that would be required 1345185029Spjd * by zil_commit(). 1346185029Spjd */ 1347185029Spjdvoid 1348185029Spjdzil_clean(zilog_t *zilog, uint64_t synced_txg) 1349185029Spjd{ 1350168404Spjd itxg_t *itxg = &zilog->zl_itxg[synced_txg & TXG_MASK]; 1351168404Spjd itxs_t *clean_me; 1352168404Spjd 1353168404Spjd mutex_enter(&itxg->itxg_lock); 1354168404Spjd if (itxg->itxg_itxs == NULL || itxg->itxg_txg == ZILTEST_TXG) { 1355168404Spjd mutex_exit(&itxg->itxg_lock); 1356168404Spjd return; 1357168404Spjd } 1358168404Spjd ASSERT3U(itxg->itxg_txg, <=, synced_txg); 1359168404Spjd ASSERT(itxg->itxg_txg != 0); 1360168404Spjd ASSERT(zilog->zl_clean_taskq != NULL); 1361168404Spjd atomic_add_64(&zilog->zl_itx_list_sz, -itxg->itxg_sod); 1362168404Spjd itxg->itxg_sod = 0; 1363168404Spjd clean_me = itxg->itxg_itxs; 1364168404Spjd itxg->itxg_itxs = NULL; 1365168404Spjd itxg->itxg_txg = 0; 1366168404Spjd mutex_exit(&itxg->itxg_lock); 1367168404Spjd /* 1368185029Spjd * Preferably start a task queue to free up the old itxs but 1369185029Spjd * if taskq_dispatch can't allocate resources to do that then 1370168404Spjd * free it in-line. This should be rare. Note, using TQ_SLEEP 1371168404Spjd * created a bad performance problem. 1372168404Spjd */ 1373168404Spjd if (taskq_dispatch(zilog->zl_clean_taskq, 1374168404Spjd (void (*)(void *))zil_itxg_clean, clean_me, TQ_NOSLEEP) == 0) 1375185029Spjd zil_itxg_clean(clean_me); 1376185029Spjd} 1377185029Spjd 1378168404Spjd/* 1379168404Spjd * Get the list of itxs to commit into zl_itx_commit_list. 1380168404Spjd */ 1381168404Spjdstatic void 1382168404Spjdzil_get_commit_list(zilog_t *zilog) 1383168404Spjd{ 1384168404Spjd uint64_t otxg, txg; 1385168404Spjd list_t *commit_list = &zilog->zl_itx_commit_list; 1386168404Spjd uint64_t push_sod = 0; 1387168404Spjd 1388168404Spjd if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */ 1389168404Spjd otxg = ZILTEST_TXG; 1390168404Spjd else 1391168404Spjd otxg = spa_last_synced_txg(zilog->zl_spa) + 1; 1392168404Spjd 1393168404Spjd for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) { 1394168404Spjd itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK]; 1395168404Spjd 1396168404Spjd mutex_enter(&itxg->itxg_lock); 1397168404Spjd if (itxg->itxg_txg != txg) { 1398168404Spjd mutex_exit(&itxg->itxg_lock); 1399168404Spjd continue; 1400168404Spjd } 1401168404Spjd 1402168404Spjd list_move_tail(commit_list, &itxg->itxg_itxs->i_sync_list); 1403168404Spjd push_sod += itxg->itxg_sod; 1404168404Spjd itxg->itxg_sod = 0; 1405168404Spjd 1406168404Spjd mutex_exit(&itxg->itxg_lock); 1407168404Spjd } 1408168404Spjd atomic_add_64(&zilog->zl_itx_list_sz, -push_sod); 1409168404Spjd} 1410168404Spjd 1411168404Spjd/* 1412168404Spjd * Move the async itxs for a specified object to commit into sync lists. 1413168404Spjd */ 1414168404Spjdstatic void 1415168404Spjdzil_async_to_sync(zilog_t *zilog, uint64_t foid) 1416168404Spjd{ 1417168404Spjd uint64_t otxg, txg; 1418168404Spjd itx_async_node_t *ian; 1419168404Spjd avl_tree_t *t; 1420168404Spjd avl_index_t where; 1421168404Spjd 1422168404Spjd if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */ 1423168404Spjd otxg = ZILTEST_TXG; 1424168404Spjd else 1425168404Spjd otxg = spa_last_synced_txg(zilog->zl_spa) + 1; 1426168404Spjd 1427168404Spjd for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) { 1428168404Spjd itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK]; 1429168404Spjd 1430168404Spjd mutex_enter(&itxg->itxg_lock); 1431168404Spjd if (itxg->itxg_txg != txg) { 1432168404Spjd mutex_exit(&itxg->itxg_lock); 1433168404Spjd continue; 1434168404Spjd } 1435168404Spjd 1436200724Sdelphij /* 1437168404Spjd * If a foid is specified then find that node and append its 1438168404Spjd * list. Otherwise walk the tree appending all the lists 1439168404Spjd * to the sync list. We add to the end rather than the 1440168404Spjd * beginning to ensure the create has happened. 1441168404Spjd */ 1442168404Spjd t = &itxg->itxg_itxs->i_async_tree; 1443168404Spjd if (foid != 0) { 1444168404Spjd ian = avl_find(t, &foid, &where); 1445168404Spjd if (ian != NULL) { 1446168404Spjd list_move_tail(&itxg->itxg_itxs->i_sync_list, 1447168404Spjd &ian->ia_list); 1448168404Spjd } 1449168404Spjd } else { 1450168404Spjd void *cookie = NULL; 1451168404Spjd 1452168404Spjd while ((ian = avl_destroy_nodes(t, &cookie)) != NULL) { 1453168404Spjd list_move_tail(&itxg->itxg_itxs->i_sync_list, 1454168404Spjd &ian->ia_list); 1455168404Spjd list_destroy(&ian->ia_list); 1456168404Spjd kmem_free(ian, sizeof (itx_async_node_t)); 1457168404Spjd } 1458168404Spjd } 1459168404Spjd mutex_exit(&itxg->itxg_lock); 1460168404Spjd } 1461168404Spjd} 1462168404Spjd 1463168404Spjdstatic void 1464168404Spjdzil_commit_writer(zilog_t *zilog) 1465168404Spjd{ 1466168404Spjd uint64_t txg; 1467168404Spjd itx_t *itx; 1468168404Spjd lwb_t *lwb; 1469168404Spjd spa_t *spa = zilog->zl_spa; 1470168404Spjd int error = 0; 1471168404Spjd 1472168404Spjd ASSERT(zilog->zl_root_zio == NULL); 1473168404Spjd 1474168404Spjd mutex_exit(&zilog->zl_lock); 1475168404Spjd 1476168404Spjd zil_get_commit_list(zilog); 1477168404Spjd 1478168404Spjd /* 1479168404Spjd * Return if there's nothing to commit before we dirty the fs by 1480168404Spjd * calling zil_create(). 1481168404Spjd */ 1482168404Spjd if (list_head(&zilog->zl_itx_commit_list) == NULL) { 1483168404Spjd mutex_enter(&zilog->zl_lock); 1484168404Spjd return; 1485185029Spjd } 1486168404Spjd 1487168404Spjd if (zilog->zl_suspend) { 1488168404Spjd lwb = NULL; 1489168404Spjd } else { 1490168404Spjd lwb = list_tail(&zilog->zl_lwb_list); 1491168404Spjd if (lwb == NULL) 1492168404Spjd lwb = zil_create(zilog); 1493168404Spjd } 1494168404Spjd 1495168404Spjd DTRACE_PROBE1(zil__cw1, zilog_t *, zilog); 1496168404Spjd while (itx = list_head(&zilog->zl_itx_commit_list)) { 1497168404Spjd txg = itx->itx_lr.lrc_txg; 1498168404Spjd ASSERT(txg); 1499168404Spjd 1500168404Spjd if (txg > spa_last_synced_txg(spa) || txg > spa_freeze_txg(spa)) 1501168404Spjd lwb = zil_lwb_commit(zilog, itx, lwb); 1502168404Spjd list_remove(&zilog->zl_itx_commit_list, itx); 1503168404Spjd kmem_free(itx, offsetof(itx_t, itx_lr) 1504168404Spjd + itx->itx_lr.lrc_reclen); 1505168404Spjd } 1506168404Spjd DTRACE_PROBE1(zil__cw2, zilog_t *, zilog); 1507168404Spjd 1508168404Spjd /* write the last block out */ 1509168404Spjd if (lwb != NULL && lwb->lwb_zio != NULL) 1510168404Spjd lwb = zil_lwb_write_start(zilog, lwb); 1511185029Spjd 1512185029Spjd zilog->zl_cur_used = 0; 1513185029Spjd 1514168404Spjd /* 1515168404Spjd * Wait if necessary for the log blocks to be on stable storage. 1516168404Spjd */ 1517168404Spjd if (zilog->zl_root_zio) { 1518168404Spjd error = zio_wait(zilog->zl_root_zio); 1519168404Spjd zilog->zl_root_zio = NULL; 1520168404Spjd zil_flush_vdevs(zilog); 1521168404Spjd } 1522168404Spjd 1523168404Spjd if (error || lwb == NULL) 1524168404Spjd txg_wait_synced(zilog->zl_dmu_pool, 0); 1525168404Spjd 1526168404Spjd mutex_enter(&zilog->zl_lock); 1527168404Spjd 1528168404Spjd /* 1529168404Spjd * Remember the highest committed log sequence number for ztest. 1530168404Spjd * We only update this value when all the log writes succeeded, 1531168404Spjd * because ztest wants to ASSERT that it got the whole log chain. 1532168404Spjd */ 1533168404Spjd if (error == 0 && lwb != NULL) 1534168404Spjd zilog->zl_commit_lr_seq = zilog->zl_lr_seq; 1535168404Spjd} 1536168404Spjd 1537168404Spjd/* 1538168404Spjd * Commit zfs transactions to stable storage. 1539168404Spjd * If foid is 0 push out all transactions, otherwise push only those 1540168404Spjd * for that object or might reference that object. 1541168404Spjd * 1542168404Spjd * itxs are committed in batches. In a heavily stressed zil there will be 1543168404Spjd * a commit writer thread who is writing out a bunch of itxs to the log 1544168404Spjd * for a set of committing threads (cthreads) in the same batch as the writer. 1545168404Spjd * Those cthreads are all waiting on the same cv for that batch. 1546168404Spjd * 1547168404Spjd * There will also be a different and growing batch of threads that are 1548168404Spjd * waiting to commit (qthreads). When the committing batch completes 1549168404Spjd * a transition occurs such that the cthreads exit and the qthreads become 1550168404Spjd * cthreads. One of the new cthreads becomes the writer thread for the 1551168404Spjd * batch. Any new threads arriving become new qthreads. 1552168404Spjd * 1553168404Spjd * Only 2 condition variables are needed and there's no transition 1554168404Spjd * between the two cvs needed. They just flip-flop between qthreads 1555168404Spjd * and cthreads. 1556168404Spjd * 1557168404Spjd * Using this scheme we can efficiently wakeup up only those threads 1558168404Spjd * that have been committed. 1559168404Spjd */ 1560168404Spjdvoid 1561168404Spjdzil_commit(zilog_t *zilog, uint64_t foid) 1562168404Spjd{ 1563168404Spjd uint64_t mybatch; 1564168404Spjd 1565168404Spjd if (zilog->zl_sync == ZFS_SYNC_DISABLED) 1566168404Spjd return; 1567168404Spjd 1568168404Spjd /* move the async itxs for the foid to the sync queues */ 1569168404Spjd zil_async_to_sync(zilog, foid); 1570168404Spjd 1571168404Spjd mutex_enter(&zilog->zl_lock); 1572168404Spjd mybatch = zilog->zl_next_batch; 1573168404Spjd while (zilog->zl_writer) { 1574168404Spjd cv_wait(&zilog->zl_cv_batch[mybatch & 1], &zilog->zl_lock); 1575168404Spjd if (mybatch <= zilog->zl_com_batch) { 1576168404Spjd mutex_exit(&zilog->zl_lock); 1577168404Spjd return; 1578168404Spjd } 1579168404Spjd } 1580168404Spjd 1581168404Spjd zilog->zl_next_batch++; 1582168404Spjd zilog->zl_writer = B_TRUE; 1583168404Spjd zil_commit_writer(zilog); 1584168404Spjd zilog->zl_com_batch = mybatch; 1585168404Spjd zilog->zl_writer = B_FALSE; 1586168404Spjd mutex_exit(&zilog->zl_lock); 1587168404Spjd 1588185029Spjd /* wake up one thread to become the next writer */ 1589185029Spjd cv_signal(&zilog->zl_cv_batch[(mybatch+1) & 1]); 1590168404Spjd 1591168404Spjd /* wake up all threads waiting for this batch to be committed */ 1592168404Spjd cv_broadcast(&zilog->zl_cv_batch[mybatch & 1]); 1593185029Spjd} 1594168404Spjd 1595168404Spjd/* 1596168404Spjd * Called in syncing context to free committed log blocks and update log header. 1597168404Spjd */ 1598168404Spjdvoid 1599168404Spjdzil_sync(zilog_t *zilog, dmu_tx_t *tx) 1600168404Spjd{ 1601168404Spjd zil_header_t *zh = zil_header_in_syncing_context(zilog); 1602168404Spjd uint64_t txg = dmu_tx_get_txg(tx); 1603168404Spjd spa_t *spa = zilog->zl_spa; 1604168404Spjd uint64_t *replayed_seq = &zilog->zl_replayed_seq[txg & TXG_MASK]; 1605168404Spjd lwb_t *lwb; 1606168404Spjd 1607168404Spjd /* 1608168404Spjd * We don't zero out zl_destroy_txg, so make sure we don't try 1609168404Spjd * to destroy it twice. 1610168404Spjd */ 1611168404Spjd if (spa_sync_pass(spa) != 1) 1612168404Spjd return; 1613168404Spjd 1614168404Spjd mutex_enter(&zilog->zl_lock); 1615168404Spjd 1616185029Spjd ASSERT(zilog->zl_stop_sync == 0); 1617185029Spjd 1618168404Spjd if (*replayed_seq != 0) { 1619168404Spjd ASSERT(zh->zh_replay_seq < *replayed_seq); 1620168404Spjd zh->zh_replay_seq = *replayed_seq; 1621168404Spjd *replayed_seq = 0; 1622168404Spjd } 1623168404Spjd 1624168404Spjd if (zilog->zl_destroy_txg == txg) { 1625168404Spjd blkptr_t blk = zh->zh_log; 1626168404Spjd 1627168404Spjd ASSERT(list_head(&zilog->zl_lwb_list) == NULL); 1628168404Spjd 1629168404Spjd bzero(zh, sizeof (zil_header_t)); 1630168404Spjd bzero(zilog->zl_replayed_seq, sizeof (zilog->zl_replayed_seq)); 1631168404Spjd 1632168404Spjd if (zilog->zl_keep_first) { 1633168404Spjd /* 1634168404Spjd * If this block was part of log chain that couldn't 1635168404Spjd * be claimed because a device was missing during 1636168404Spjd * zil_claim(), but that device later returns, 1637185029Spjd * then this block could erroneously appear valid. 1638185029Spjd * To guard against this, assign a new GUID to the new 1639185029Spjd * log chain so it doesn't matter what blk points to. 1640168404Spjd */ 1641168404Spjd zil_init_log_chain(zilog, &blk); 1642168404Spjd zh->zh_log = blk; 1643168404Spjd } 1644168404Spjd } 1645168404Spjd 1646168404Spjd while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 1647168404Spjd zh->zh_log = lwb->lwb_blk; 1648168404Spjd if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg) 1649168404Spjd break; 1650168404Spjd list_remove(&zilog->zl_lwb_list, lwb); 1651168404Spjd zio_free_zil(spa, txg, &lwb->lwb_blk); 1652168404Spjd kmem_cache_free(zil_lwb_cache, lwb); 1653168404Spjd 1654168404Spjd /* 1655168404Spjd * If we don't have anything left in the lwb list then 1656185029Spjd * we've had an allocation failure and we need to zero 1657185029Spjd * out the zil_header blkptr so that we don't end 1658168404Spjd * up freeing the same block twice. 1659168404Spjd */ 1660168404Spjd if (list_head(&zilog->zl_lwb_list) == NULL) 1661168404Spjd BP_ZERO(&zh->zh_log); 1662168404Spjd } 1663200724Sdelphij mutex_exit(&zilog->zl_lock); 1664168404Spjd} 1665168404Spjd 1666168404Spjdvoid 1667168404Spjdzil_init(void) 1668168404Spjd{ 1669168404Spjd zil_lwb_cache = kmem_cache_create("zil_lwb_cache", 1670168404Spjd sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0); 1671185029Spjd} 1672168404Spjd 1673168404Spjdvoid 1674168404Spjdzil_fini(void) 1675168404Spjd{ 1676168404Spjd kmem_cache_destroy(zil_lwb_cache); 1677168404Spjd} 1678168404Spjd 1679168404Spjdvoid 1680168404Spjdzil_set_sync(zilog_t *zilog, uint64_t sync) 1681168404Spjd{ 1682168404Spjd zilog->zl_sync = sync; 1683174049Sjb} 1684168404Spjd 1685168404Spjdvoid 1686168404Spjdzil_set_logbias(zilog_t *zilog, uint64_t logbias) 1687168404Spjd{ 1688168404Spjd zilog->zl_logbias = logbias; 1689168404Spjd} 1690185029Spjd 1691168404Spjdzilog_t * 1692168404Spjdzil_alloc(objset_t *os, zil_header_t *zh_phys) 1693168404Spjd{ 1694168404Spjd zilog_t *zilog; 1695168404Spjd 1696168404Spjd zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP); 1697168404Spjd 1698168404Spjd zilog->zl_header = zh_phys; 1699168404Spjd zilog->zl_os = os; 1700168404Spjd zilog->zl_spa = dmu_objset_spa(os); 1701168404Spjd zilog->zl_dmu_pool = dmu_objset_pool(os); 1702168404Spjd zilog->zl_destroy_txg = TXG_INITIAL - 1; 1703168404Spjd zilog->zl_logbias = dmu_objset_logbias(os); 1704168404Spjd zilog->zl_sync = dmu_objset_syncprop(os); 1705168404Spjd zilog->zl_next_batch = 1; 1706168404Spjd 1707168404Spjd mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL); 1708168404Spjd 1709168404Spjd for (int i = 0; i < TXG_SIZE; i++) { 1710168404Spjd mutex_init(&zilog->zl_itxg[i].itxg_lock, NULL, 1711168404Spjd MUTEX_DEFAULT, NULL); 1712168404Spjd } 1713168404Spjd 1714168404Spjd list_create(&zilog->zl_lwb_list, sizeof (lwb_t), 1715168404Spjd offsetof(lwb_t, lwb_node)); 1716168404Spjd 1717168404Spjd list_create(&zilog->zl_itx_commit_list, sizeof (itx_t), 1718168404Spjd offsetof(itx_t, itx_node)); 1719168404Spjd 1720168404Spjd mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL); 1721168404Spjd 1722168404Spjd avl_create(&zilog->zl_vdev_tree, zil_vdev_compare, 1723168404Spjd sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node)); 1724168404Spjd 1725168404Spjd cv_init(&zilog->zl_cv_writer, NULL, CV_DEFAULT, NULL); 1726168404Spjd cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL); 1727168404Spjd cv_init(&zilog->zl_cv_batch[0], NULL, CV_DEFAULT, NULL); 1728168404Spjd cv_init(&zilog->zl_cv_batch[1], NULL, CV_DEFAULT, NULL); 1729168404Spjd 1730168404Spjd return (zilog); 1731168404Spjd} 1732168404Spjd 1733168404Spjdvoid 1734168404Spjdzil_free(zilog_t *zilog) 1735168404Spjd{ 1736 zilog->zl_stop_sync = 1; 1737 1738 ASSERT0(zilog->zl_suspend); 1739 ASSERT0(zilog->zl_suspending); 1740 1741 ASSERT(list_is_empty(&zilog->zl_lwb_list)); 1742 list_destroy(&zilog->zl_lwb_list); 1743 1744 avl_destroy(&zilog->zl_vdev_tree); 1745 mutex_destroy(&zilog->zl_vdev_lock); 1746 1747 ASSERT(list_is_empty(&zilog->zl_itx_commit_list)); 1748 list_destroy(&zilog->zl_itx_commit_list); 1749 1750 for (int i = 0; i < TXG_SIZE; i++) { 1751 /* 1752 * It's possible for an itx to be generated that doesn't dirty 1753 * a txg (e.g. ztest TX_TRUNCATE). So there's no zil_clean() 1754 * callback to remove the entry. We remove those here. 1755 * 1756 * Also free up the ziltest itxs. 1757 */ 1758 if (zilog->zl_itxg[i].itxg_itxs) 1759 zil_itxg_clean(zilog->zl_itxg[i].itxg_itxs); 1760 mutex_destroy(&zilog->zl_itxg[i].itxg_lock); 1761 } 1762 1763 mutex_destroy(&zilog->zl_lock); 1764 1765 cv_destroy(&zilog->zl_cv_writer); 1766 cv_destroy(&zilog->zl_cv_suspend); 1767 cv_destroy(&zilog->zl_cv_batch[0]); 1768 cv_destroy(&zilog->zl_cv_batch[1]); 1769 1770 kmem_free(zilog, sizeof (zilog_t)); 1771} 1772 1773/* 1774 * Open an intent log. 1775 */ 1776zilog_t * 1777zil_open(objset_t *os, zil_get_data_t *get_data) 1778{ 1779 zilog_t *zilog = dmu_objset_zil(os); 1780 1781 ASSERT(zilog->zl_clean_taskq == NULL); 1782 ASSERT(zilog->zl_get_data == NULL); 1783 ASSERT(list_is_empty(&zilog->zl_lwb_list)); 1784 1785 zilog->zl_get_data = get_data; 1786 zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri, 1787 2, 2, TASKQ_PREPOPULATE); 1788 1789 return (zilog); 1790} 1791 1792/* 1793 * Close an intent log. 1794 */ 1795void 1796zil_close(zilog_t *zilog) 1797{ 1798 lwb_t *lwb; 1799 uint64_t txg = 0; 1800 1801 zil_commit(zilog, 0); /* commit all itx */ 1802 1803 /* 1804 * The lwb_max_txg for the stubby lwb will reflect the last activity 1805 * for the zil. After a txg_wait_synced() on the txg we know all the 1806 * callbacks have occurred that may clean the zil. Only then can we 1807 * destroy the zl_clean_taskq. 1808 */ 1809 mutex_enter(&zilog->zl_lock); 1810 lwb = list_tail(&zilog->zl_lwb_list); 1811 if (lwb != NULL) 1812 txg = lwb->lwb_max_txg; 1813 mutex_exit(&zilog->zl_lock); 1814 if (txg) 1815 txg_wait_synced(zilog->zl_dmu_pool, txg); 1816 ASSERT(!zilog_is_dirty(zilog)); 1817 1818 taskq_destroy(zilog->zl_clean_taskq); 1819 zilog->zl_clean_taskq = NULL; 1820 zilog->zl_get_data = NULL; 1821 1822 /* 1823 * We should have only one LWB left on the list; remove it now. 1824 */ 1825 mutex_enter(&zilog->zl_lock); 1826 lwb = list_head(&zilog->zl_lwb_list); 1827 if (lwb != NULL) { 1828 ASSERT(lwb == list_tail(&zilog->zl_lwb_list)); 1829 list_remove(&zilog->zl_lwb_list, lwb); 1830 zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 1831 kmem_cache_free(zil_lwb_cache, lwb); 1832 } 1833 mutex_exit(&zilog->zl_lock); 1834} 1835 1836static char *suspend_tag = "zil suspending"; 1837 1838/* 1839 * Suspend an intent log. While in suspended mode, we still honor 1840 * synchronous semantics, but we rely on txg_wait_synced() to do it. 1841 * On old version pools, we suspend the log briefly when taking a 1842 * snapshot so that it will have an empty intent log. 1843 * 1844 * Long holds are not really intended to be used the way we do here -- 1845 * held for such a short time. A concurrent caller of dsl_dataset_long_held() 1846 * could fail. Therefore we take pains to only put a long hold if it is 1847 * actually necessary. Fortunately, it will only be necessary if the 1848 * objset is currently mounted (or the ZVOL equivalent). In that case it 1849 * will already have a long hold, so we are not really making things any worse. 1850 * 1851 * Ideally, we would locate the existing long-holder (i.e. the zfsvfs_t or 1852 * zvol_state_t), and use their mechanism to prevent their hold from being 1853 * dropped (e.g. VFS_HOLD()). However, that would be even more pain for 1854 * very little gain. 1855 * 1856 * if cookiep == NULL, this does both the suspend & resume. 1857 * Otherwise, it returns with the dataset "long held", and the cookie 1858 * should be passed into zil_resume(). 1859 */ 1860int 1861zil_suspend(const char *osname, void **cookiep) 1862{ 1863 objset_t *os; 1864 zilog_t *zilog; 1865 const zil_header_t *zh; 1866 int error; 1867 1868 error = dmu_objset_hold(osname, suspend_tag, &os); 1869 if (error != 0) 1870 return (error); 1871 zilog = dmu_objset_zil(os); 1872 1873 mutex_enter(&zilog->zl_lock); 1874 zh = zilog->zl_header; 1875 1876 if (zh->zh_flags & ZIL_REPLAY_NEEDED) { /* unplayed log */ 1877 mutex_exit(&zilog->zl_lock); 1878 dmu_objset_rele(os, suspend_tag); 1879 return (SET_ERROR(EBUSY)); 1880 } 1881 1882 /* 1883 * Don't put a long hold in the cases where we can avoid it. This 1884 * is when there is no cookie so we are doing a suspend & resume 1885 * (i.e. called from zil_vdev_offline()), and there's nothing to do 1886 * for the suspend because it's already suspended, or there's no ZIL. 1887 */ 1888 if (cookiep == NULL && !zilog->zl_suspending && 1889 (zilog->zl_suspend > 0 || BP_IS_HOLE(&zh->zh_log))) { 1890 mutex_exit(&zilog->zl_lock); 1891 dmu_objset_rele(os, suspend_tag); 1892 return (0); 1893 } 1894 1895 dsl_dataset_long_hold(dmu_objset_ds(os), suspend_tag); 1896 dsl_pool_rele(dmu_objset_pool(os), suspend_tag); 1897 1898 zilog->zl_suspend++; 1899 1900 if (zilog->zl_suspend > 1) { 1901 /* 1902 * Someone else is already suspending it. 1903 * Just wait for them to finish. 1904 */ 1905 1906 while (zilog->zl_suspending) 1907 cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock); 1908 mutex_exit(&zilog->zl_lock); 1909 1910 if (cookiep == NULL) 1911 zil_resume(os); 1912 else 1913 *cookiep = os; 1914 return (0); 1915 } 1916 1917 /* 1918 * If there is no pointer to an on-disk block, this ZIL must not 1919 * be active (e.g. filesystem not mounted), so there's nothing 1920 * to clean up. 1921 */ 1922 if (BP_IS_HOLE(&zh->zh_log)) { 1923 ASSERT(cookiep != NULL); /* fast path already handled */ 1924 1925 *cookiep = os; 1926 mutex_exit(&zilog->zl_lock); 1927 return (0); 1928 } 1929 1930 zilog->zl_suspending = B_TRUE; 1931 mutex_exit(&zilog->zl_lock); 1932 1933 zil_commit(zilog, 0); 1934 1935 zil_destroy(zilog, B_FALSE); 1936 1937 mutex_enter(&zilog->zl_lock); 1938 zilog->zl_suspending = B_FALSE; 1939 cv_broadcast(&zilog->zl_cv_suspend); 1940 mutex_exit(&zilog->zl_lock); 1941 1942 if (cookiep == NULL) 1943 zil_resume(os); 1944 else 1945 *cookiep = os; 1946 return (0); 1947} 1948 1949void 1950zil_resume(void *cookie) 1951{ 1952 objset_t *os = cookie; 1953 zilog_t *zilog = dmu_objset_zil(os); 1954 1955 mutex_enter(&zilog->zl_lock); 1956 ASSERT(zilog->zl_suspend != 0); 1957 zilog->zl_suspend--; 1958 mutex_exit(&zilog->zl_lock); 1959 dsl_dataset_long_rele(dmu_objset_ds(os), suspend_tag); 1960 dsl_dataset_rele(dmu_objset_ds(os), suspend_tag); 1961} 1962 1963typedef struct zil_replay_arg { 1964 zil_replay_func_t **zr_replay; 1965 void *zr_arg; 1966 boolean_t zr_byteswap; 1967 char *zr_lr; 1968} zil_replay_arg_t; 1969 1970static int 1971zil_replay_error(zilog_t *zilog, lr_t *lr, int error) 1972{ 1973 char name[MAXNAMELEN]; 1974 1975 zilog->zl_replaying_seq--; /* didn't actually replay this one */ 1976 1977 dmu_objset_name(zilog->zl_os, name); 1978 1979 cmn_err(CE_WARN, "ZFS replay transaction error %d, " 1980 "dataset %s, seq 0x%llx, txtype %llu %s\n", error, name, 1981 (u_longlong_t)lr->lrc_seq, 1982 (u_longlong_t)(lr->lrc_txtype & ~TX_CI), 1983 (lr->lrc_txtype & TX_CI) ? "CI" : ""); 1984 1985 return (error); 1986} 1987 1988static int 1989zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg) 1990{ 1991 zil_replay_arg_t *zr = zra; 1992 const zil_header_t *zh = zilog->zl_header; 1993 uint64_t reclen = lr->lrc_reclen; 1994 uint64_t txtype = lr->lrc_txtype; 1995 int error = 0; 1996 1997 zilog->zl_replaying_seq = lr->lrc_seq; 1998 1999 if (lr->lrc_seq <= zh->zh_replay_seq) /* already replayed */ 2000 return (0); 2001 2002 if (lr->lrc_txg < claim_txg) /* already committed */ 2003 return (0); 2004 2005 /* Strip case-insensitive bit, still present in log record */ 2006 txtype &= ~TX_CI; 2007 2008 if (txtype == 0 || txtype >= TX_MAX_TYPE) 2009 return (zil_replay_error(zilog, lr, EINVAL)); 2010 2011 /* 2012 * If this record type can be logged out of order, the object 2013 * (lr_foid) may no longer exist. That's legitimate, not an error. 2014 */ 2015 if (TX_OOO(txtype)) { 2016 error = dmu_object_info(zilog->zl_os, 2017 ((lr_ooo_t *)lr)->lr_foid, NULL); 2018 if (error == ENOENT || error == EEXIST) 2019 return (0); 2020 } 2021 2022 /* 2023 * Make a copy of the data so we can revise and extend it. 2024 */ 2025 bcopy(lr, zr->zr_lr, reclen); 2026 2027 /* 2028 * If this is a TX_WRITE with a blkptr, suck in the data. 2029 */ 2030 if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) { 2031 error = zil_read_log_data(zilog, (lr_write_t *)lr, 2032 zr->zr_lr + reclen); 2033 if (error != 0) 2034 return (zil_replay_error(zilog, lr, error)); 2035 } 2036 2037 /* 2038 * The log block containing this lr may have been byteswapped 2039 * so that we can easily examine common fields like lrc_txtype. 2040 * However, the log is a mix of different record types, and only the 2041 * replay vectors know how to byteswap their records. Therefore, if 2042 * the lr was byteswapped, undo it before invoking the replay vector. 2043 */ 2044 if (zr->zr_byteswap) 2045 byteswap_uint64_array(zr->zr_lr, reclen); 2046 2047 /* 2048 * We must now do two things atomically: replay this log record, 2049 * and update the log header sequence number to reflect the fact that 2050 * we did so. At the end of each replay function the sequence number 2051 * is updated if we are in replay mode. 2052 */ 2053 error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, zr->zr_byteswap); 2054 if (error != 0) { 2055 /* 2056 * The DMU's dnode layer doesn't see removes until the txg 2057 * commits, so a subsequent claim can spuriously fail with 2058 * EEXIST. So if we receive any error we try syncing out 2059 * any removes then retry the transaction. Note that we 2060 * specify B_FALSE for byteswap now, so we don't do it twice. 2061 */ 2062 txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0); 2063 error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, B_FALSE); 2064 if (error != 0) 2065 return (zil_replay_error(zilog, lr, error)); 2066 } 2067 return (0); 2068} 2069 2070/* ARGSUSED */ 2071static int 2072zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg) 2073{ 2074 zilog->zl_replay_blks++; 2075 2076 return (0); 2077} 2078 2079/* 2080 * If this dataset has a non-empty intent log, replay it and destroy it. 2081 */ 2082void 2083zil_replay(objset_t *os, void *arg, zil_replay_func_t *replay_func[TX_MAX_TYPE]) 2084{ 2085 zilog_t *zilog = dmu_objset_zil(os); 2086 const zil_header_t *zh = zilog->zl_header; 2087 zil_replay_arg_t zr; 2088 2089 if ((zh->zh_flags & ZIL_REPLAY_NEEDED) == 0) { 2090 zil_destroy(zilog, B_TRUE); 2091 return; 2092 } 2093 2094 zr.zr_replay = replay_func; 2095 zr.zr_arg = arg; 2096 zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log); 2097 zr.zr_lr = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP); 2098 2099 /* 2100 * Wait for in-progress removes to sync before starting replay. 2101 */ 2102 txg_wait_synced(zilog->zl_dmu_pool, 0); 2103 2104 zilog->zl_replay = B_TRUE; 2105 zilog->zl_replay_time = ddi_get_lbolt(); 2106 ASSERT(zilog->zl_replay_blks == 0); 2107 (void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr, 2108 zh->zh_claim_txg); 2109 kmem_free(zr.zr_lr, 2 * SPA_MAXBLOCKSIZE); 2110 2111 zil_destroy(zilog, B_FALSE); 2112 txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 2113 zilog->zl_replay = B_FALSE; 2114} 2115 2116boolean_t 2117zil_replaying(zilog_t *zilog, dmu_tx_t *tx) 2118{ 2119 if (zilog->zl_sync == ZFS_SYNC_DISABLED) 2120 return (B_TRUE); 2121 2122 if (zilog->zl_replay) { 2123 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 2124 zilog->zl_replayed_seq[dmu_tx_get_txg(tx) & TXG_MASK] = 2125 zilog->zl_replaying_seq; 2126 return (B_TRUE); 2127 } 2128 2129 return (B_FALSE); 2130} 2131 2132/* ARGSUSED */ 2133int 2134zil_vdev_offline(const char *osname, void *arg) 2135{ 2136 int error; 2137 2138 error = zil_suspend(osname, NULL); 2139 if (error != 0) 2140 return (SET_ERROR(EEXIST)); 2141 return (0); 2142} 2143