zil.c revision 268657
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/* 22219089Spjd * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23268657Sdelphij * Copyright (c) 2011, 2014 by Delphix. All rights reserved. 24168404Spjd */ 25168404Spjd 26219089Spjd/* Portions Copyright 2010 Robert Milkowski */ 27219089Spjd 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> 38219089Spjd#include <sys/vdev_impl.h> 39168404Spjd#include <sys/dmu_tx.h> 40219089Spjd#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/* 69251631Sdelphij * Disable intent logging replay. This global ZIL switch affects all pools. 70168404Spjd */ 71251631Sdelphijint zil_replay_disable = 0; 72168404SpjdSYSCTL_DECL(_vfs_zfs); 73219089SpjdTUNABLE_INT("vfs.zfs.zil_replay_disable", &zil_replay_disable); 74219089SpjdSYSCTL_INT(_vfs_zfs, OID_AUTO, zil_replay_disable, CTLFLAG_RW, 75219089Spjd &zil_replay_disable, 0, "Disable intent logging replay"); 76168404Spjd 77168404Spjd/* 78168404Spjd * Tunable parameter for debugging or performance analysis. Setting 79168404Spjd * zfs_nocacheflush will cause corruption on power loss if a volatile 80168404Spjd * out-of-order write cache is enabled. 81168404Spjd */ 82168404Spjdboolean_t zfs_nocacheflush = B_FALSE; 83168404SpjdTUNABLE_INT("vfs.zfs.cache_flush_disable", &zfs_nocacheflush); 84168404SpjdSYSCTL_INT(_vfs_zfs, OID_AUTO, cache_flush_disable, CTLFLAG_RDTUN, 85168404Spjd &zfs_nocacheflush, 0, "Disable cache flush"); 86249921Ssmhboolean_t zfs_trim_enabled = B_TRUE; 87249921SsmhSYSCTL_DECL(_vfs_zfs_trim); 88249921SsmhTUNABLE_INT("vfs.zfs.trim.enabled", &zfs_trim_enabled); 89249921SsmhSYSCTL_INT(_vfs_zfs_trim, OID_AUTO, enabled, CTLFLAG_RDTUN, &zfs_trim_enabled, 0, 90249921Ssmh "Enable ZFS TRIM"); 91168404Spjd 92168404Spjdstatic kmem_cache_t *zil_lwb_cache; 93168404Spjd 94219089Spjdstatic void zil_async_to_sync(zilog_t *zilog, uint64_t foid); 95219089Spjd 96219089Spjd#define LWB_EMPTY(lwb) ((BP_GET_LSIZE(&lwb->lwb_blk) - \ 97219089Spjd sizeof (zil_chain_t)) == (lwb->lwb_sz - lwb->lwb_nused)) 98219089Spjd 99219089Spjd 100219089Spjd/* 101219089Spjd * ziltest is by and large an ugly hack, but very useful in 102219089Spjd * checking replay without tedious work. 103219089Spjd * When running ziltest we want to keep all itx's and so maintain 104219089Spjd * a single list in the zl_itxg[] that uses a high txg: ZILTEST_TXG 105219089Spjd * We subtract TXG_CONCURRENT_STATES to allow for common code. 106219089Spjd */ 107219089Spjd#define ZILTEST_TXG (UINT64_MAX - TXG_CONCURRENT_STATES) 108219089Spjd 109168404Spjdstatic int 110219089Spjdzil_bp_compare(const void *x1, const void *x2) 111168404Spjd{ 112219089Spjd const dva_t *dva1 = &((zil_bp_node_t *)x1)->zn_dva; 113219089Spjd const dva_t *dva2 = &((zil_bp_node_t *)x2)->zn_dva; 114168404Spjd 115168404Spjd if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2)) 116168404Spjd return (-1); 117168404Spjd if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2)) 118168404Spjd return (1); 119168404Spjd 120168404Spjd if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2)) 121168404Spjd return (-1); 122168404Spjd if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2)) 123168404Spjd return (1); 124168404Spjd 125168404Spjd return (0); 126168404Spjd} 127168404Spjd 128168404Spjdstatic void 129219089Spjdzil_bp_tree_init(zilog_t *zilog) 130168404Spjd{ 131219089Spjd avl_create(&zilog->zl_bp_tree, zil_bp_compare, 132219089Spjd sizeof (zil_bp_node_t), offsetof(zil_bp_node_t, zn_node)); 133168404Spjd} 134168404Spjd 135168404Spjdstatic void 136219089Spjdzil_bp_tree_fini(zilog_t *zilog) 137168404Spjd{ 138219089Spjd avl_tree_t *t = &zilog->zl_bp_tree; 139219089Spjd zil_bp_node_t *zn; 140168404Spjd void *cookie = NULL; 141168404Spjd 142168404Spjd while ((zn = avl_destroy_nodes(t, &cookie)) != NULL) 143219089Spjd kmem_free(zn, sizeof (zil_bp_node_t)); 144168404Spjd 145168404Spjd avl_destroy(t); 146168404Spjd} 147168404Spjd 148219089Spjdint 149219089Spjdzil_bp_tree_add(zilog_t *zilog, const blkptr_t *bp) 150168404Spjd{ 151219089Spjd avl_tree_t *t = &zilog->zl_bp_tree; 152268649Sdelphij const dva_t *dva; 153219089Spjd zil_bp_node_t *zn; 154168404Spjd avl_index_t where; 155168404Spjd 156268649Sdelphij if (BP_IS_EMBEDDED(bp)) 157268649Sdelphij return (0); 158268649Sdelphij 159268649Sdelphij dva = BP_IDENTITY(bp); 160268649Sdelphij 161168404Spjd if (avl_find(t, dva, &where) != NULL) 162249195Smm return (SET_ERROR(EEXIST)); 163168404Spjd 164219089Spjd zn = kmem_alloc(sizeof (zil_bp_node_t), KM_SLEEP); 165168404Spjd zn->zn_dva = *dva; 166168404Spjd avl_insert(t, zn, where); 167168404Spjd 168168404Spjd return (0); 169168404Spjd} 170168404Spjd 171168404Spjdstatic zil_header_t * 172168404Spjdzil_header_in_syncing_context(zilog_t *zilog) 173168404Spjd{ 174168404Spjd return ((zil_header_t *)zilog->zl_header); 175168404Spjd} 176168404Spjd 177168404Spjdstatic void 178168404Spjdzil_init_log_chain(zilog_t *zilog, blkptr_t *bp) 179168404Spjd{ 180168404Spjd zio_cksum_t *zc = &bp->blk_cksum; 181168404Spjd 182168404Spjd zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL); 183168404Spjd zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL); 184168404Spjd zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os); 185168404Spjd zc->zc_word[ZIL_ZC_SEQ] = 1ULL; 186168404Spjd} 187168404Spjd 188168404Spjd/* 189219089Spjd * Read a log block and make sure it's valid. 190168404Spjd */ 191168404Spjdstatic int 192219089Spjdzil_read_log_block(zilog_t *zilog, const blkptr_t *bp, blkptr_t *nbp, void *dst, 193219089Spjd char **end) 194168404Spjd{ 195219089Spjd enum zio_flag zio_flags = ZIO_FLAG_CANFAIL; 196219089Spjd uint32_t aflags = ARC_WAIT; 197219089Spjd arc_buf_t *abuf = NULL; 198268657Sdelphij zbookmark_phys_t zb; 199168404Spjd int error; 200168404Spjd 201219089Spjd if (zilog->zl_header->zh_claim_txg == 0) 202219089Spjd zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB; 203168404Spjd 204219089Spjd if (!(zilog->zl_header->zh_flags & ZIL_CLAIM_LR_SEQ_VALID)) 205219089Spjd zio_flags |= ZIO_FLAG_SPECULATIVE; 206168404Spjd 207219089Spjd SET_BOOKMARK(&zb, bp->blk_cksum.zc_word[ZIL_ZC_OBJSET], 208219089Spjd ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]); 209168404Spjd 210246666Smm error = arc_read(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf, 211219089Spjd ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb); 212219089Spjd 213168404Spjd if (error == 0) { 214168404Spjd zio_cksum_t cksum = bp->blk_cksum; 215168404Spjd 216168404Spjd /* 217185029Spjd * Validate the checksummed log block. 218185029Spjd * 219168404Spjd * Sequence numbers should be... sequential. The checksum 220168404Spjd * verifier for the next block should be bp's checksum plus 1. 221185029Spjd * 222185029Spjd * Also check the log chain linkage and size used. 223168404Spjd */ 224168404Spjd cksum.zc_word[ZIL_ZC_SEQ]++; 225168404Spjd 226219089Spjd if (BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_ZILOG2) { 227219089Spjd zil_chain_t *zilc = abuf->b_data; 228219089Spjd char *lr = (char *)(zilc + 1); 229219089Spjd uint64_t len = zilc->zc_nused - sizeof (zil_chain_t); 230219089Spjd 231219089Spjd if (bcmp(&cksum, &zilc->zc_next_blk.blk_cksum, 232219089Spjd sizeof (cksum)) || BP_IS_HOLE(&zilc->zc_next_blk)) { 233249195Smm error = SET_ERROR(ECKSUM); 234219089Spjd } else { 235219089Spjd bcopy(lr, dst, len); 236219089Spjd *end = (char *)dst + len; 237219089Spjd *nbp = zilc->zc_next_blk; 238219089Spjd } 239219089Spjd } else { 240219089Spjd char *lr = abuf->b_data; 241219089Spjd uint64_t size = BP_GET_LSIZE(bp); 242219089Spjd zil_chain_t *zilc = (zil_chain_t *)(lr + size) - 1; 243219089Spjd 244219089Spjd if (bcmp(&cksum, &zilc->zc_next_blk.blk_cksum, 245219089Spjd sizeof (cksum)) || BP_IS_HOLE(&zilc->zc_next_blk) || 246219089Spjd (zilc->zc_nused > (size - sizeof (*zilc)))) { 247249195Smm error = SET_ERROR(ECKSUM); 248219089Spjd } else { 249219089Spjd bcopy(lr, dst, zilc->zc_nused); 250219089Spjd *end = (char *)dst + zilc->zc_nused; 251219089Spjd *nbp = zilc->zc_next_blk; 252219089Spjd } 253185029Spjd } 254168404Spjd 255248571Smm VERIFY(arc_buf_remove_ref(abuf, &abuf)); 256168404Spjd } 257168404Spjd 258219089Spjd return (error); 259219089Spjd} 260168404Spjd 261219089Spjd/* 262219089Spjd * Read a TX_WRITE log data block. 263219089Spjd */ 264219089Spjdstatic int 265219089Spjdzil_read_log_data(zilog_t *zilog, const lr_write_t *lr, void *wbuf) 266219089Spjd{ 267219089Spjd enum zio_flag zio_flags = ZIO_FLAG_CANFAIL; 268219089Spjd const blkptr_t *bp = &lr->lr_blkptr; 269219089Spjd uint32_t aflags = ARC_WAIT; 270219089Spjd arc_buf_t *abuf = NULL; 271268657Sdelphij zbookmark_phys_t zb; 272219089Spjd int error; 273219089Spjd 274219089Spjd if (BP_IS_HOLE(bp)) { 275219089Spjd if (wbuf != NULL) 276219089Spjd bzero(wbuf, MAX(BP_GET_LSIZE(bp), lr->lr_length)); 277219089Spjd return (0); 278219089Spjd } 279219089Spjd 280219089Spjd if (zilog->zl_header->zh_claim_txg == 0) 281219089Spjd zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB; 282219089Spjd 283219089Spjd SET_BOOKMARK(&zb, dmu_objset_id(zilog->zl_os), lr->lr_foid, 284219089Spjd ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp)); 285219089Spjd 286246666Smm error = arc_read(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf, 287219089Spjd ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb); 288219089Spjd 289219089Spjd if (error == 0) { 290219089Spjd if (wbuf != NULL) 291219089Spjd bcopy(abuf->b_data, wbuf, arc_buf_size(abuf)); 292219089Spjd (void) arc_buf_remove_ref(abuf, &abuf); 293219089Spjd } 294219089Spjd 295168404Spjd return (error); 296168404Spjd} 297168404Spjd 298168404Spjd/* 299168404Spjd * Parse the intent log, and call parse_func for each valid record within. 300168404Spjd */ 301219089Spjdint 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; 306219089Spjd boolean_t claimed = !!zh->zh_claim_txg; 307219089Spjd uint64_t claim_blk_seq = claimed ? zh->zh_claim_blk_seq : UINT64_MAX; 308219089Spjd uint64_t claim_lr_seq = claimed ? zh->zh_claim_lr_seq : UINT64_MAX; 309219089Spjd uint64_t max_blk_seq = 0; 310219089Spjd uint64_t max_lr_seq = 0; 311219089Spjd uint64_t blk_count = 0; 312219089Spjd uint64_t lr_count = 0; 313219089Spjd blkptr_t blk, next_blk; 314168404Spjd char *lrbuf, *lrp; 315219089Spjd int error = 0; 316168404Spjd 317219089Spjd /* 318219089Spjd * Old logs didn't record the maximum zh_claim_lr_seq. 319219089Spjd */ 320219089Spjd if (!(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID)) 321219089Spjd 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 */ 332219089Spjd lrbuf = zio_buf_alloc(SPA_MAXBLOCKSIZE); 333219089Spjd zil_bp_tree_init(zilog); 334168404Spjd 335219089Spjd for (blk = zh->zh_log; !BP_IS_HOLE(&blk); blk = next_blk) { 336219089Spjd uint64_t blk_seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 337219089Spjd int reclen; 338219089Spjd char *end; 339219089Spjd 340219089Spjd if (blk_seq > claim_blk_seq) 341168404Spjd break; 342219089Spjd if ((error = parse_blk_func(zilog, &blk, arg, txg)) != 0) 343219089Spjd break; 344219089Spjd ASSERT3U(max_blk_seq, <, blk_seq); 345219089Spjd max_blk_seq = blk_seq; 346219089Spjd blk_count++; 347168404Spjd 348219089Spjd if (max_lr_seq == claim_lr_seq && max_blk_seq == claim_blk_seq) 349219089Spjd break; 350168404Spjd 351219089Spjd error = zil_read_log_block(zilog, &blk, &next_blk, lrbuf, &end); 352248571Smm if (error != 0) 353168404Spjd break; 354168404Spjd 355219089Spjd 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)); 359219089Spjd if (lr->lrc_seq > claim_lr_seq) 360219089Spjd goto done; 361219089Spjd if ((error = parse_lr_func(zilog, lr, arg, txg)) != 0) 362219089Spjd goto done; 363219089Spjd ASSERT3U(max_lr_seq, <, lr->lrc_seq); 364219089Spjd max_lr_seq = lr->lrc_seq; 365219089Spjd lr_count++; 366168404Spjd } 367168404Spjd } 368219089Spjddone: 369219089Spjd zilog->zl_parse_error = error; 370219089Spjd zilog->zl_parse_blk_seq = max_blk_seq; 371219089Spjd zilog->zl_parse_lr_seq = max_lr_seq; 372219089Spjd zilog->zl_parse_blk_count = blk_count; 373219089Spjd zilog->zl_parse_lr_count = lr_count; 374168404Spjd 375219089Spjd ASSERT(!claimed || !(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID) || 376219089Spjd (max_blk_seq == claim_blk_seq && max_lr_seq == claim_lr_seq)); 377219089Spjd 378219089Spjd zil_bp_tree_fini(zilog); 379219089Spjd zio_buf_free(lrbuf, SPA_MAXBLOCKSIZE); 380219089Spjd 381219089Spjd return (error); 382168404Spjd} 383168404Spjd 384219089Spjdstatic 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. 389219089Spjd * If tx == NULL, just verify that the block is claimable. 390168404Spjd */ 391263397Sdelphij if (BP_IS_HOLE(bp) || bp->blk_birth < first_txg || 392263397Sdelphij zil_bp_tree_add(zilog, bp) != 0) 393219089Spjd return (0); 394219089Spjd 395219089Spjd return (zio_wait(zio_claim(NULL, zilog->zl_spa, 396219089Spjd tx == NULL ? 0 : first_txg, bp, spa_claim_notify, NULL, 397219089Spjd ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB))); 398168404Spjd} 399168404Spjd 400219089Spjdstatic int 401168404Spjdzil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg) 402168404Spjd{ 403219089Spjd lr_write_t *lr = (lr_write_t *)lrc; 404219089Spjd int error; 405219089Spjd 406219089Spjd if (lrc->lrc_txtype != TX_WRITE) 407219089Spjd return (0); 408219089Spjd 409219089Spjd /* 410219089Spjd * If the block is not readable, don't claim it. This can happen 411219089Spjd * in normal operation when a log block is written to disk before 412219089Spjd * some of the dmu_sync() blocks it points to. In this case, the 413219089Spjd * transaction cannot have been committed to anyone (we would have 414219089Spjd * waited for all writes to be stable first), so it is semantically 415219089Spjd * correct to declare this the end of the log. 416219089Spjd */ 417219089Spjd if (lr->lr_blkptr.blk_birth >= first_txg && 418219089Spjd (error = zil_read_log_data(zilog, lr, NULL)) != 0) 419219089Spjd return (error); 420219089Spjd return (zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg)); 421168404Spjd} 422168404Spjd 423168404Spjd/* ARGSUSED */ 424219089Spjdstatic int 425168404Spjdzil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg) 426168404Spjd{ 427219089Spjd zio_free_zil(zilog->zl_spa, dmu_tx_get_txg(tx), bp); 428219089Spjd 429219089Spjd return (0); 430168404Spjd} 431168404Spjd 432219089Spjdstatic int 433168404Spjdzil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg) 434168404Spjd{ 435219089Spjd lr_write_t *lr = (lr_write_t *)lrc; 436219089Spjd blkptr_t *bp = &lr->lr_blkptr; 437219089Spjd 438168404Spjd /* 439168404Spjd * If we previously claimed it, we need to free it. 440168404Spjd */ 441219089Spjd if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE && 442263397Sdelphij bp->blk_birth >= claim_txg && zil_bp_tree_add(zilog, bp) == 0 && 443263397Sdelphij !BP_IS_HOLE(bp)) 444219089Spjd zio_free(zilog->zl_spa, dmu_tx_get_txg(tx), bp); 445219089Spjd 446219089Spjd return (0); 447219089Spjd} 448219089Spjd 449219089Spjdstatic lwb_t * 450219089Spjdzil_alloc_lwb(zilog_t *zilog, blkptr_t *bp, uint64_t txg) 451219089Spjd{ 452219089Spjd lwb_t *lwb; 453219089Spjd 454219089Spjd lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 455219089Spjd lwb->lwb_zilog = zilog; 456219089Spjd lwb->lwb_blk = *bp; 457219089Spjd lwb->lwb_buf = zio_buf_alloc(BP_GET_LSIZE(bp)); 458219089Spjd lwb->lwb_max_txg = txg; 459219089Spjd lwb->lwb_zio = NULL; 460219089Spjd lwb->lwb_tx = NULL; 461219089Spjd if (BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_ZILOG2) { 462219089Spjd lwb->lwb_nused = sizeof (zil_chain_t); 463219089Spjd lwb->lwb_sz = BP_GET_LSIZE(bp); 464219089Spjd } else { 465219089Spjd lwb->lwb_nused = 0; 466219089Spjd lwb->lwb_sz = BP_GET_LSIZE(bp) - sizeof (zil_chain_t); 467168404Spjd } 468219089Spjd 469219089Spjd mutex_enter(&zilog->zl_lock); 470219089Spjd list_insert_tail(&zilog->zl_lwb_list, lwb); 471219089Spjd mutex_exit(&zilog->zl_lock); 472219089Spjd 473219089Spjd return (lwb); 474168404Spjd} 475168404Spjd 476168404Spjd/* 477239620Smm * Called when we create in-memory log transactions so that we know 478239620Smm * to cleanup the itxs at the end of spa_sync(). 479239620Smm */ 480239620Smmvoid 481239620Smmzilog_dirty(zilog_t *zilog, uint64_t txg) 482239620Smm{ 483239620Smm dsl_pool_t *dp = zilog->zl_dmu_pool; 484239620Smm dsl_dataset_t *ds = dmu_objset_ds(zilog->zl_os); 485239620Smm 486239620Smm if (dsl_dataset_is_snapshot(ds)) 487239620Smm panic("dirtying snapshot!"); 488239620Smm 489248571Smm if (txg_list_add(&dp->dp_dirty_zilogs, zilog, txg)) { 490239620Smm /* up the hold count until we can be written out */ 491239620Smm dmu_buf_add_ref(ds->ds_dbuf, zilog); 492239620Smm } 493239620Smm} 494239620Smm 495239620Smmboolean_t 496239620Smmzilog_is_dirty(zilog_t *zilog) 497239620Smm{ 498239620Smm dsl_pool_t *dp = zilog->zl_dmu_pool; 499239620Smm 500239620Smm for (int t = 0; t < TXG_SIZE; t++) { 501239620Smm if (txg_list_member(&dp->dp_dirty_zilogs, zilog, t)) 502239620Smm return (B_TRUE); 503239620Smm } 504239620Smm return (B_FALSE); 505239620Smm} 506239620Smm 507239620Smm/* 508168404Spjd * Create an on-disk intent log. 509168404Spjd */ 510219089Spjdstatic lwb_t * 511168404Spjdzil_create(zilog_t *zilog) 512168404Spjd{ 513168404Spjd const zil_header_t *zh = zilog->zl_header; 514219089Spjd lwb_t *lwb = NULL; 515168404Spjd uint64_t txg = 0; 516168404Spjd dmu_tx_t *tx = NULL; 517168404Spjd blkptr_t blk; 518168404Spjd int error = 0; 519168404Spjd 520168404Spjd /* 521168404Spjd * Wait for any previous destroy to complete. 522168404Spjd */ 523168404Spjd 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 /* 531219089Spjd * Allocate an initial log block if: 532219089Spjd * - there isn't one already 533219089Spjd * - the existing block is the wrong endianess 534168404Spjd */ 535207908Smm if (BP_IS_HOLE(&blk) || BP_SHOULD_BYTESWAP(&blk)) { 536168404Spjd tx = dmu_tx_create(zilog->zl_os); 537219089Spjd 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 541207908Smm if (!BP_IS_HOLE(&blk)) { 542219089Spjd zio_free_zil(zilog->zl_spa, txg, &blk); 543207908Smm BP_ZERO(&blk); 544207908Smm } 545207908Smm 546219089Spjd error = zio_alloc_zil(zilog->zl_spa, txg, &blk, NULL, 547219089Spjd ZIL_MIN_BLKSZ, zilog->zl_logbias == ZFS_LOGBIAS_LATENCY); 548168404Spjd 549168404Spjd if (error == 0) 550168404Spjd zil_init_log_chain(zilog, &blk); 551168404Spjd } 552168404Spjd 553168404Spjd /* 554168404Spjd * Allocate a log write buffer (lwb) for the first log block. 555168404Spjd */ 556219089Spjd if (error == 0) 557219089Spjd lwb = zil_alloc_lwb(zilog, &blk, txg); 558168404Spjd 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); 570219089Spjd 571219089Spjd 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 578168404Spjd * synchronous transaction doesn't require a txg_wait_synced() 579168404Spjd * in zil_create(). We don't need to txg_wait_synced() here either 580168404Spjd * when keep_first is set, because both zil_create() and zil_destroy() 581168404Spjd * will wait for any in-progress destroys to complete. 582168404Spjd */ 583168404Spjdvoid 584168404Spjdzil_destroy(zilog_t *zilog, boolean_t keep_first) 585168404Spjd{ 586168404Spjd const zil_header_t *zh = zilog->zl_header; 587168404Spjd lwb_t *lwb; 588168404Spjd dmu_tx_t *tx; 589168404Spjd uint64_t txg; 590168404Spjd 591168404Spjd /* 592168404Spjd * Wait for any previous destroy to complete. 593168404Spjd */ 594168404Spjd txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 595168404Spjd 596219089Spjd zilog->zl_old_header = *zh; /* debugging aid */ 597219089Spjd 598168404Spjd if (BP_IS_HOLE(&zh->zh_log)) 599168404Spjd return; 600168404Spjd 601168404Spjd tx = dmu_tx_create(zilog->zl_os); 602219089Spjd VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0); 603168404Spjd dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 604168404Spjd txg = dmu_tx_get_txg(tx); 605168404Spjd 606168404Spjd mutex_enter(&zilog->zl_lock); 607168404Spjd 608168404Spjd ASSERT3U(zilog->zl_destroy_txg, <, txg); 609168404Spjd zilog->zl_destroy_txg = txg; 610168404Spjd zilog->zl_keep_first = keep_first; 611168404Spjd 612168404Spjd if (!list_is_empty(&zilog->zl_lwb_list)) { 613168404Spjd ASSERT(zh->zh_claim_txg == 0); 614224526Smm VERIFY(!keep_first); 615168404Spjd while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 616168404Spjd list_remove(&zilog->zl_lwb_list, lwb); 617168404Spjd if (lwb->lwb_buf != NULL) 618168404Spjd zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 619219089Spjd zio_free_zil(zilog->zl_spa, txg, &lwb->lwb_blk); 620168404Spjd kmem_cache_free(zil_lwb_cache, lwb); 621168404Spjd } 622219089Spjd } else if (!keep_first) { 623239620Smm zil_destroy_sync(zilog, tx); 624168404Spjd } 625168404Spjd mutex_exit(&zilog->zl_lock); 626168404Spjd 627168404Spjd dmu_tx_commit(tx); 628185029Spjd} 629168404Spjd 630239620Smmvoid 631239620Smmzil_destroy_sync(zilog_t *zilog, dmu_tx_t *tx) 632239620Smm{ 633239620Smm ASSERT(list_is_empty(&zilog->zl_lwb_list)); 634239620Smm (void) zil_parse(zilog, zil_free_log_block, 635239620Smm zil_free_log_record, tx, zilog->zl_header->zh_claim_txg); 636239620Smm} 637239620Smm 638168404Spjdint 639219089Spjdzil_claim(const char *osname, void *txarg) 640168404Spjd{ 641168404Spjd dmu_tx_t *tx = txarg; 642168404Spjd uint64_t first_txg = dmu_tx_get_txg(tx); 643168404Spjd zilog_t *zilog; 644168404Spjd zil_header_t *zh; 645168404Spjd objset_t *os; 646168404Spjd int error; 647168404Spjd 648248571Smm error = dmu_objset_own(osname, DMU_OST_ANY, B_FALSE, FTAG, &os); 649248571Smm if (error != 0) { 650185029Spjd cmn_err(CE_WARN, "can't open objset for %s", osname); 651168404Spjd return (0); 652168404Spjd } 653168404Spjd 654168404Spjd zilog = dmu_objset_zil(os); 655168404Spjd zh = zil_header_in_syncing_context(zilog); 656168404Spjd 657219089Spjd if (spa_get_log_state(zilog->zl_spa) == SPA_LOG_CLEAR) { 658213197Smm if (!BP_IS_HOLE(&zh->zh_log)) 659219089Spjd zio_free_zil(zilog->zl_spa, first_txg, &zh->zh_log); 660213197Smm BP_ZERO(&zh->zh_log); 661213197Smm dsl_dataset_dirty(dmu_objset_ds(os), tx); 662248571Smm dmu_objset_disown(os, FTAG); 663219089Spjd return (0); 664213197Smm } 665213197Smm 666168404Spjd /* 667168404Spjd * Claim all log blocks if we haven't already done so, and remember 668168404Spjd * the highest claimed sequence number. This ensures that if we can 669168404Spjd * read only part of the log now (e.g. due to a missing device), 670168404Spjd * but we can read the entire log later, we will not try to replay 671168404Spjd * or destroy beyond the last block we successfully claimed. 672168404Spjd */ 673168404Spjd ASSERT3U(zh->zh_claim_txg, <=, first_txg); 674168404Spjd if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) { 675219089Spjd (void) zil_parse(zilog, zil_claim_log_block, 676219089Spjd zil_claim_log_record, tx, first_txg); 677168404Spjd zh->zh_claim_txg = first_txg; 678219089Spjd zh->zh_claim_blk_seq = zilog->zl_parse_blk_seq; 679219089Spjd zh->zh_claim_lr_seq = zilog->zl_parse_lr_seq; 680219089Spjd if (zilog->zl_parse_lr_count || zilog->zl_parse_blk_count > 1) 681219089Spjd zh->zh_flags |= ZIL_REPLAY_NEEDED; 682219089Spjd zh->zh_flags |= ZIL_CLAIM_LR_SEQ_VALID; 683168404Spjd dsl_dataset_dirty(dmu_objset_ds(os), tx); 684168404Spjd } 685168404Spjd 686168404Spjd ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1)); 687248571Smm dmu_objset_disown(os, FTAG); 688168404Spjd return (0); 689168404Spjd} 690168404Spjd 691185029Spjd/* 692185029Spjd * Check the log by walking the log chain. 693185029Spjd * Checksum errors are ok as they indicate the end of the chain. 694185029Spjd * Any other error (no device or read failure) returns an error. 695185029Spjd */ 696185029Spjdint 697219089Spjdzil_check_log_chain(const char *osname, void *tx) 698168404Spjd{ 699185029Spjd zilog_t *zilog; 700185029Spjd objset_t *os; 701219089Spjd blkptr_t *bp; 702185029Spjd int error; 703168404Spjd 704219089Spjd ASSERT(tx == NULL); 705219089Spjd 706219089Spjd error = dmu_objset_hold(osname, FTAG, &os); 707248571Smm if (error != 0) { 708185029Spjd cmn_err(CE_WARN, "can't open objset for %s", osname); 709185029Spjd return (0); 710185029Spjd } 711168404Spjd 712185029Spjd zilog = dmu_objset_zil(os); 713219089Spjd bp = (blkptr_t *)&zilog->zl_header->zh_log; 714219089Spjd 715219089Spjd /* 716219089Spjd * Check the first block and determine if it's on a log device 717219089Spjd * which may have been removed or faulted prior to loading this 718219089Spjd * pool. If so, there's no point in checking the rest of the log 719219089Spjd * as its content should have already been synced to the pool. 720219089Spjd */ 721219089Spjd if (!BP_IS_HOLE(bp)) { 722219089Spjd vdev_t *vd; 723219089Spjd boolean_t valid = B_TRUE; 724219089Spjd 725219089Spjd spa_config_enter(os->os_spa, SCL_STATE, FTAG, RW_READER); 726219089Spjd vd = vdev_lookup_top(os->os_spa, DVA_GET_VDEV(&bp->blk_dva[0])); 727219089Spjd if (vd->vdev_islog && vdev_is_dead(vd)) 728219089Spjd valid = vdev_log_state_valid(vd); 729219089Spjd spa_config_exit(os->os_spa, SCL_STATE, FTAG); 730219089Spjd 731219089Spjd if (!valid) { 732219089Spjd dmu_objset_rele(os, FTAG); 733219089Spjd return (0); 734219089Spjd } 735168404Spjd } 736185029Spjd 737219089Spjd /* 738219089Spjd * Because tx == NULL, zil_claim_log_block() will not actually claim 739219089Spjd * any blocks, but just determine whether it is possible to do so. 740219089Spjd * In addition to checking the log chain, zil_claim_log_block() 741219089Spjd * will invoke zio_claim() with a done func of spa_claim_notify(), 742219089Spjd * which will update spa_max_claim_txg. See spa_load() for details. 743219089Spjd */ 744219089Spjd error = zil_parse(zilog, zil_claim_log_block, zil_claim_log_record, tx, 745219089Spjd zilog->zl_header->zh_claim_txg ? -1ULL : spa_first_txg(os->os_spa)); 746219089Spjd 747219089Spjd dmu_objset_rele(os, FTAG); 748219089Spjd 749219089Spjd return ((error == ECKSUM || error == ENOENT) ? 0 : error); 750168404Spjd} 751168404Spjd 752185029Spjdstatic int 753185029Spjdzil_vdev_compare(const void *x1, const void *x2) 754185029Spjd{ 755219089Spjd const uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev; 756219089Spjd const uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev; 757185029Spjd 758185029Spjd if (v1 < v2) 759185029Spjd return (-1); 760185029Spjd if (v1 > v2) 761185029Spjd return (1); 762185029Spjd 763185029Spjd return (0); 764185029Spjd} 765185029Spjd 766168404Spjdvoid 767219089Spjdzil_add_block(zilog_t *zilog, const blkptr_t *bp) 768168404Spjd{ 769185029Spjd avl_tree_t *t = &zilog->zl_vdev_tree; 770185029Spjd avl_index_t where; 771185029Spjd zil_vdev_node_t *zv, zvsearch; 772185029Spjd int ndvas = BP_GET_NDVAS(bp); 773185029Spjd int i; 774168404Spjd 775185029Spjd if (zfs_nocacheflush) 776185029Spjd return; 777168404Spjd 778185029Spjd ASSERT(zilog->zl_writer); 779168404Spjd 780185029Spjd /* 781185029Spjd * Even though we're zl_writer, we still need a lock because the 782185029Spjd * zl_get_data() callbacks may have dmu_sync() done callbacks 783185029Spjd * that will run concurrently. 784185029Spjd */ 785185029Spjd mutex_enter(&zilog->zl_vdev_lock); 786185029Spjd for (i = 0; i < ndvas; i++) { 787185029Spjd zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]); 788185029Spjd if (avl_find(t, &zvsearch, &where) == NULL) { 789185029Spjd zv = kmem_alloc(sizeof (*zv), KM_SLEEP); 790185029Spjd zv->zv_vdev = zvsearch.zv_vdev; 791185029Spjd avl_insert(t, zv, where); 792185029Spjd } 793185029Spjd } 794185029Spjd mutex_exit(&zilog->zl_vdev_lock); 795168404Spjd} 796168404Spjd 797219089Spjdstatic void 798168404Spjdzil_flush_vdevs(zilog_t *zilog) 799168404Spjd{ 800168404Spjd spa_t *spa = zilog->zl_spa; 801185029Spjd avl_tree_t *t = &zilog->zl_vdev_tree; 802185029Spjd void *cookie = NULL; 803185029Spjd zil_vdev_node_t *zv; 804185029Spjd zio_t *zio; 805168404Spjd 806168404Spjd ASSERT(zilog->zl_writer); 807168404Spjd 808185029Spjd /* 809185029Spjd * We don't need zl_vdev_lock here because we're the zl_writer, 810185029Spjd * and all zl_get_data() callbacks are done. 811185029Spjd */ 812185029Spjd if (avl_numnodes(t) == 0) 813185029Spjd return; 814185029Spjd 815185029Spjd spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 816185029Spjd 817185029Spjd zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); 818185029Spjd 819185029Spjd while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) { 820185029Spjd vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev); 821185029Spjd if (vd != NULL) 822185029Spjd zio_flush(zio, vd); 823185029Spjd kmem_free(zv, sizeof (*zv)); 824168404Spjd } 825168404Spjd 826168404Spjd /* 827168404Spjd * Wait for all the flushes to complete. Not all devices actually 828168404Spjd * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails. 829168404Spjd */ 830185029Spjd (void) zio_wait(zio); 831185029Spjd 832185029Spjd spa_config_exit(spa, SCL_STATE, FTAG); 833168404Spjd} 834168404Spjd 835168404Spjd/* 836168404Spjd * Function called when a log block write completes 837168404Spjd */ 838168404Spjdstatic void 839168404Spjdzil_lwb_write_done(zio_t *zio) 840168404Spjd{ 841168404Spjd lwb_t *lwb = zio->io_private; 842168404Spjd zilog_t *zilog = lwb->lwb_zilog; 843219089Spjd dmu_tx_t *tx = lwb->lwb_tx; 844168404Spjd 845185029Spjd ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF); 846185029Spjd ASSERT(BP_GET_TYPE(zio->io_bp) == DMU_OT_INTENT_LOG); 847185029Spjd ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 848185029Spjd ASSERT(BP_GET_BYTEORDER(zio->io_bp) == ZFS_HOST_BYTEORDER); 849185029Spjd ASSERT(!BP_IS_GANG(zio->io_bp)); 850185029Spjd ASSERT(!BP_IS_HOLE(zio->io_bp)); 851268649Sdelphij ASSERT(BP_GET_FILL(zio->io_bp) == 0); 852185029Spjd 853168404Spjd /* 854209962Smm * Ensure the lwb buffer pointer is cleared before releasing 855209962Smm * the txg. If we have had an allocation failure and 856209962Smm * the txg is waiting to sync then we want want zil_sync() 857209962Smm * to remove the lwb so that it's not picked up as the next new 858209962Smm * one in zil_commit_writer(). zil_sync() will only remove 859209962Smm * the lwb if lwb_buf is null. 860168404Spjd */ 861168404Spjd zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 862168404Spjd mutex_enter(&zilog->zl_lock); 863168404Spjd lwb->lwb_buf = NULL; 864219089Spjd lwb->lwb_tx = NULL; 865219089Spjd mutex_exit(&zilog->zl_lock); 866209962Smm 867209962Smm /* 868209962Smm * Now that we've written this log block, we have a stable pointer 869209962Smm * to the next block in the chain, so it's OK to let the txg in 870219089Spjd * which we allocated the next block sync. 871209962Smm */ 872219089Spjd dmu_tx_commit(tx); 873168404Spjd} 874168404Spjd 875168404Spjd/* 876168404Spjd * Initialize the io for a log block. 877168404Spjd */ 878168404Spjdstatic void 879168404Spjdzil_lwb_write_init(zilog_t *zilog, lwb_t *lwb) 880168404Spjd{ 881268657Sdelphij zbookmark_phys_t zb; 882168404Spjd 883219089Spjd SET_BOOKMARK(&zb, lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET], 884219089Spjd ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, 885219089Spjd lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ]); 886168404Spjd 887168404Spjd if (zilog->zl_root_zio == NULL) { 888168404Spjd zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL, 889168404Spjd ZIO_FLAG_CANFAIL); 890168404Spjd } 891168404Spjd if (lwb->lwb_zio == NULL) { 892168404Spjd lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa, 893219089Spjd 0, &lwb->lwb_blk, lwb->lwb_buf, BP_GET_LSIZE(&lwb->lwb_blk), 894260763Savg zil_lwb_write_done, lwb, ZIO_PRIORITY_SYNC_WRITE, 895219089Spjd ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE, &zb); 896168404Spjd } 897168404Spjd} 898168404Spjd 899168404Spjd/* 900219089Spjd * Define a limited set of intent log block sizes. 901251631Sdelphij * 902219089Spjd * These must be a multiple of 4KB. Note only the amount used (again 903219089Spjd * aligned to 4KB) actually gets written. However, we can't always just 904219089Spjd * allocate SPA_MAXBLOCKSIZE as the slog space could be exhausted. 905219089Spjd */ 906219089Spjduint64_t zil_block_buckets[] = { 907219089Spjd 4096, /* non TX_WRITE */ 908219089Spjd 8192+4096, /* data base */ 909219089Spjd 32*1024 + 4096, /* NFS writes */ 910219089Spjd UINT64_MAX 911219089Spjd}; 912219089Spjd 913219089Spjd/* 914219089Spjd * Use the slog as long as the logbias is 'latency' and the current commit size 915219089Spjd * is less than the limit or the total list size is less than 2X the limit. 916219089Spjd * Limit checking is disabled by setting zil_slog_limit to UINT64_MAX. 917219089Spjd */ 918219089Spjduint64_t zil_slog_limit = 1024 * 1024; 919219089Spjd#define USE_SLOG(zilog) (((zilog)->zl_logbias == ZFS_LOGBIAS_LATENCY) && \ 920219089Spjd (((zilog)->zl_cur_used < zil_slog_limit) || \ 921219089Spjd ((zilog)->zl_itx_list_sz < (zil_slog_limit << 1)))) 922219089Spjd 923219089Spjd/* 924168404Spjd * Start a log block write and advance to the next log block. 925168404Spjd * Calls are serialized. 926168404Spjd */ 927168404Spjdstatic lwb_t * 928168404Spjdzil_lwb_write_start(zilog_t *zilog, lwb_t *lwb) 929168404Spjd{ 930219089Spjd lwb_t *nlwb = NULL; 931219089Spjd zil_chain_t *zilc; 932168404Spjd spa_t *spa = zilog->zl_spa; 933219089Spjd blkptr_t *bp; 934219089Spjd dmu_tx_t *tx; 935168404Spjd uint64_t txg; 936219089Spjd uint64_t zil_blksz, wsz; 937219089Spjd int i, error; 938168404Spjd 939219089Spjd if (BP_GET_CHECKSUM(&lwb->lwb_blk) == ZIO_CHECKSUM_ZILOG2) { 940219089Spjd zilc = (zil_chain_t *)lwb->lwb_buf; 941219089Spjd bp = &zilc->zc_next_blk; 942219089Spjd } else { 943219089Spjd zilc = (zil_chain_t *)(lwb->lwb_buf + lwb->lwb_sz); 944219089Spjd bp = &zilc->zc_next_blk; 945219089Spjd } 946168404Spjd 947219089Spjd ASSERT(lwb->lwb_nused <= lwb->lwb_sz); 948219089Spjd 949168404Spjd /* 950168404Spjd * Allocate the next block and save its address in this block 951168404Spjd * before writing it in order to establish the log chain. 952168404Spjd * Note that if the allocation of nlwb synced before we wrote 953168404Spjd * the block that points at it (lwb), we'd leak it if we crashed. 954219089Spjd * Therefore, we don't do dmu_tx_commit() until zil_lwb_write_done(). 955219089Spjd * We dirty the dataset to ensure that zil_sync() will be called 956219089Spjd * to clean up in the event of allocation failure or I/O failure. 957168404Spjd */ 958219089Spjd tx = dmu_tx_create(zilog->zl_os); 959219089Spjd VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0); 960219089Spjd dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 961219089Spjd txg = dmu_tx_get_txg(tx); 962168404Spjd 963219089Spjd lwb->lwb_tx = tx; 964219089Spjd 965168404Spjd /* 966219089Spjd * Log blocks are pre-allocated. Here we select the size of the next 967219089Spjd * block, based on size used in the last block. 968219089Spjd * - first find the smallest bucket that will fit the block from a 969219089Spjd * limited set of block sizes. This is because it's faster to write 970219089Spjd * blocks allocated from the same metaslab as they are adjacent or 971219089Spjd * close. 972219089Spjd * - next find the maximum from the new suggested size and an array of 973219089Spjd * previous sizes. This lessens a picket fence effect of wrongly 974219089Spjd * guesssing the size if we have a stream of say 2k, 64k, 2k, 64k 975219089Spjd * requests. 976219089Spjd * 977219089Spjd * Note we only write what is used, but we can't just allocate 978219089Spjd * the maximum block size because we can exhaust the available 979219089Spjd * pool log space. 980168404Spjd */ 981219089Spjd zil_blksz = zilog->zl_cur_used + sizeof (zil_chain_t); 982219089Spjd for (i = 0; zil_blksz > zil_block_buckets[i]; i++) 983219089Spjd continue; 984219089Spjd zil_blksz = zil_block_buckets[i]; 985219089Spjd if (zil_blksz == UINT64_MAX) 986219089Spjd zil_blksz = SPA_MAXBLOCKSIZE; 987219089Spjd zilog->zl_prev_blks[zilog->zl_prev_rotor] = zil_blksz; 988219089Spjd for (i = 0; i < ZIL_PREV_BLKS; i++) 989219089Spjd zil_blksz = MAX(zil_blksz, zilog->zl_prev_blks[i]); 990219089Spjd zilog->zl_prev_rotor = (zilog->zl_prev_rotor + 1) & (ZIL_PREV_BLKS - 1); 991168404Spjd 992168404Spjd BP_ZERO(bp); 993168404Spjd /* pass the old blkptr in order to spread log blocks across devs */ 994219089Spjd error = zio_alloc_zil(spa, txg, bp, &lwb->lwb_blk, zil_blksz, 995219089Spjd USE_SLOG(zilog)); 996248571Smm if (error == 0) { 997219089Spjd ASSERT3U(bp->blk_birth, ==, txg); 998219089Spjd bp->blk_cksum = lwb->lwb_blk.blk_cksum; 999219089Spjd bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++; 1000168404Spjd 1001168404Spjd /* 1002219089Spjd * Allocate a new log write buffer (lwb). 1003168404Spjd */ 1004219089Spjd nlwb = zil_alloc_lwb(zilog, bp, txg); 1005168404Spjd 1006219089Spjd /* Record the block for later vdev flushing */ 1007219089Spjd zil_add_block(zilog, &lwb->lwb_blk); 1008168404Spjd } 1009168404Spjd 1010219089Spjd if (BP_GET_CHECKSUM(&lwb->lwb_blk) == ZIO_CHECKSUM_ZILOG2) { 1011219089Spjd /* For Slim ZIL only write what is used. */ 1012219089Spjd wsz = P2ROUNDUP_TYPED(lwb->lwb_nused, ZIL_MIN_BLKSZ, uint64_t); 1013219089Spjd ASSERT3U(wsz, <=, lwb->lwb_sz); 1014219089Spjd zio_shrink(lwb->lwb_zio, wsz); 1015168404Spjd 1016219089Spjd } else { 1017219089Spjd wsz = lwb->lwb_sz; 1018219089Spjd } 1019168404Spjd 1020219089Spjd zilc->zc_pad = 0; 1021219089Spjd zilc->zc_nused = lwb->lwb_nused; 1022219089Spjd zilc->zc_eck.zec_cksum = lwb->lwb_blk.blk_cksum; 1023168404Spjd 1024168404Spjd /* 1025219089Spjd * clear unused data for security 1026168404Spjd */ 1027219089Spjd bzero(lwb->lwb_buf + lwb->lwb_nused, wsz - lwb->lwb_nused); 1028168404Spjd 1029219089Spjd zio_nowait(lwb->lwb_zio); /* Kick off the write for the old log block */ 1030168404Spjd 1031168404Spjd /* 1032219089Spjd * If there was an allocation failure then nlwb will be null which 1033219089Spjd * forces a txg_wait_synced(). 1034168404Spjd */ 1035168404Spjd return (nlwb); 1036168404Spjd} 1037168404Spjd 1038168404Spjdstatic lwb_t * 1039168404Spjdzil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb) 1040168404Spjd{ 1041168404Spjd lr_t *lrc = &itx->itx_lr; /* common log record */ 1042219089Spjd lr_write_t *lrw = (lr_write_t *)lrc; 1043219089Spjd char *lr_buf; 1044168404Spjd uint64_t txg = lrc->lrc_txg; 1045168404Spjd uint64_t reclen = lrc->lrc_reclen; 1046219089Spjd uint64_t dlen = 0; 1047168404Spjd 1048168404Spjd if (lwb == NULL) 1049168404Spjd return (NULL); 1050219089Spjd 1051168404Spjd ASSERT(lwb->lwb_buf != NULL); 1052239620Smm ASSERT(zilog_is_dirty(zilog) || 1053239620Smm spa_freeze_txg(zilog->zl_spa) != UINT64_MAX); 1054168404Spjd 1055168404Spjd if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY) 1056168404Spjd dlen = P2ROUNDUP_TYPED( 1057219089Spjd lrw->lr_length, sizeof (uint64_t), uint64_t); 1058168404Spjd 1059168404Spjd zilog->zl_cur_used += (reclen + dlen); 1060168404Spjd 1061168404Spjd zil_lwb_write_init(zilog, lwb); 1062168404Spjd 1063168404Spjd /* 1064168404Spjd * If this record won't fit in the current log block, start a new one. 1065168404Spjd */ 1066219089Spjd if (lwb->lwb_nused + reclen + dlen > lwb->lwb_sz) { 1067168404Spjd lwb = zil_lwb_write_start(zilog, lwb); 1068168404Spjd if (lwb == NULL) 1069168404Spjd return (NULL); 1070168404Spjd zil_lwb_write_init(zilog, lwb); 1071219089Spjd ASSERT(LWB_EMPTY(lwb)); 1072219089Spjd if (lwb->lwb_nused + reclen + dlen > lwb->lwb_sz) { 1073168404Spjd txg_wait_synced(zilog->zl_dmu_pool, txg); 1074168404Spjd return (lwb); 1075168404Spjd } 1076168404Spjd } 1077168404Spjd 1078219089Spjd lr_buf = lwb->lwb_buf + lwb->lwb_nused; 1079219089Spjd bcopy(lrc, lr_buf, reclen); 1080219089Spjd lrc = (lr_t *)lr_buf; 1081219089Spjd lrw = (lr_write_t *)lrc; 1082168404Spjd 1083168404Spjd /* 1084168404Spjd * If it's a write, fetch the data or get its blkptr as appropriate. 1085168404Spjd */ 1086168404Spjd if (lrc->lrc_txtype == TX_WRITE) { 1087168404Spjd if (txg > spa_freeze_txg(zilog->zl_spa)) 1088168404Spjd txg_wait_synced(zilog->zl_dmu_pool, txg); 1089168404Spjd if (itx->itx_wr_state != WR_COPIED) { 1090168404Spjd char *dbuf; 1091168404Spjd int error; 1092168404Spjd 1093168404Spjd if (dlen) { 1094168404Spjd ASSERT(itx->itx_wr_state == WR_NEED_COPY); 1095219089Spjd dbuf = lr_buf + reclen; 1096219089Spjd lrw->lr_common.lrc_reclen += dlen; 1097168404Spjd } else { 1098168404Spjd ASSERT(itx->itx_wr_state == WR_INDIRECT); 1099168404Spjd dbuf = NULL; 1100168404Spjd } 1101168404Spjd error = zilog->zl_get_data( 1102219089Spjd itx->itx_private, lrw, dbuf, lwb->lwb_zio); 1103214378Smm if (error == EIO) { 1104214378Smm txg_wait_synced(zilog->zl_dmu_pool, txg); 1105214378Smm return (lwb); 1106214378Smm } 1107248571Smm if (error != 0) { 1108168404Spjd ASSERT(error == ENOENT || error == EEXIST || 1109168404Spjd error == EALREADY); 1110168404Spjd return (lwb); 1111168404Spjd } 1112168404Spjd } 1113168404Spjd } 1114168404Spjd 1115219089Spjd /* 1116219089Spjd * We're actually making an entry, so update lrc_seq to be the 1117219089Spjd * log record sequence number. Note that this is generally not 1118219089Spjd * equal to the itx sequence number because not all transactions 1119219089Spjd * are synchronous, and sometimes spa_sync() gets there first. 1120219089Spjd */ 1121219089Spjd lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */ 1122168404Spjd lwb->lwb_nused += reclen + dlen; 1123168404Spjd lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg); 1124219089Spjd ASSERT3U(lwb->lwb_nused, <=, lwb->lwb_sz); 1125240415Smm ASSERT0(P2PHASE(lwb->lwb_nused, sizeof (uint64_t))); 1126168404Spjd 1127168404Spjd return (lwb); 1128168404Spjd} 1129168404Spjd 1130168404Spjditx_t * 1131185029Spjdzil_itx_create(uint64_t txtype, size_t lrsize) 1132168404Spjd{ 1133168404Spjd itx_t *itx; 1134168404Spjd 1135168404Spjd lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t); 1136168404Spjd 1137168404Spjd itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP); 1138168404Spjd itx->itx_lr.lrc_txtype = txtype; 1139168404Spjd itx->itx_lr.lrc_reclen = lrsize; 1140185029Spjd itx->itx_sod = lrsize; /* if write & WR_NEED_COPY will be increased */ 1141168404Spjd itx->itx_lr.lrc_seq = 0; /* defensive */ 1142219089Spjd itx->itx_sync = B_TRUE; /* default is synchronous */ 1143168404Spjd 1144168404Spjd return (itx); 1145168404Spjd} 1146168404Spjd 1147219089Spjdvoid 1148219089Spjdzil_itx_destroy(itx_t *itx) 1149168404Spjd{ 1150219089Spjd kmem_free(itx, offsetof(itx_t, itx_lr) + itx->itx_lr.lrc_reclen); 1151219089Spjd} 1152168404Spjd 1153219089Spjd/* 1154219089Spjd * Free up the sync and async itxs. The itxs_t has already been detached 1155219089Spjd * so no locks are needed. 1156219089Spjd */ 1157219089Spjdstatic void 1158219089Spjdzil_itxg_clean(itxs_t *itxs) 1159219089Spjd{ 1160219089Spjd itx_t *itx; 1161219089Spjd list_t *list; 1162219089Spjd avl_tree_t *t; 1163219089Spjd void *cookie; 1164219089Spjd itx_async_node_t *ian; 1165168404Spjd 1166219089Spjd list = &itxs->i_sync_list; 1167219089Spjd while ((itx = list_head(list)) != NULL) { 1168219089Spjd list_remove(list, itx); 1169219089Spjd kmem_free(itx, offsetof(itx_t, itx_lr) + 1170219089Spjd itx->itx_lr.lrc_reclen); 1171219089Spjd } 1172168404Spjd 1173219089Spjd cookie = NULL; 1174219089Spjd t = &itxs->i_async_tree; 1175219089Spjd while ((ian = avl_destroy_nodes(t, &cookie)) != NULL) { 1176219089Spjd list = &ian->ia_list; 1177219089Spjd while ((itx = list_head(list)) != NULL) { 1178219089Spjd list_remove(list, itx); 1179219089Spjd kmem_free(itx, offsetof(itx_t, itx_lr) + 1180219089Spjd itx->itx_lr.lrc_reclen); 1181219089Spjd } 1182219089Spjd list_destroy(list); 1183219089Spjd kmem_free(ian, sizeof (itx_async_node_t)); 1184219089Spjd } 1185219089Spjd avl_destroy(t); 1186219089Spjd 1187219089Spjd kmem_free(itxs, sizeof (itxs_t)); 1188168404Spjd} 1189168404Spjd 1190219089Spjdstatic int 1191219089Spjdzil_aitx_compare(const void *x1, const void *x2) 1192219089Spjd{ 1193219089Spjd const uint64_t o1 = ((itx_async_node_t *)x1)->ia_foid; 1194219089Spjd const uint64_t o2 = ((itx_async_node_t *)x2)->ia_foid; 1195219089Spjd 1196219089Spjd if (o1 < o2) 1197219089Spjd return (-1); 1198219089Spjd if (o1 > o2) 1199219089Spjd return (1); 1200219089Spjd 1201219089Spjd return (0); 1202219089Spjd} 1203219089Spjd 1204168404Spjd/* 1205219089Spjd * Remove all async itx with the given oid. 1206168404Spjd */ 1207168404Spjdstatic void 1208219089Spjdzil_remove_async(zilog_t *zilog, uint64_t oid) 1209168404Spjd{ 1210219089Spjd uint64_t otxg, txg; 1211219089Spjd itx_async_node_t *ian; 1212219089Spjd avl_tree_t *t; 1213219089Spjd avl_index_t where; 1214168404Spjd list_t clean_list; 1215168404Spjd itx_t *itx; 1216168404Spjd 1217219089Spjd ASSERT(oid != 0); 1218168404Spjd list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node)); 1219168404Spjd 1220219089Spjd if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */ 1221219089Spjd otxg = ZILTEST_TXG; 1222219089Spjd else 1223219089Spjd otxg = spa_last_synced_txg(zilog->zl_spa) + 1; 1224219089Spjd 1225219089Spjd for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) { 1226219089Spjd itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK]; 1227219089Spjd 1228219089Spjd mutex_enter(&itxg->itxg_lock); 1229219089Spjd if (itxg->itxg_txg != txg) { 1230219089Spjd mutex_exit(&itxg->itxg_lock); 1231219089Spjd continue; 1232219089Spjd } 1233219089Spjd 1234219089Spjd /* 1235219089Spjd * Locate the object node and append its list. 1236219089Spjd */ 1237219089Spjd t = &itxg->itxg_itxs->i_async_tree; 1238219089Spjd ian = avl_find(t, &oid, &where); 1239219089Spjd if (ian != NULL) 1240219089Spjd list_move_tail(&clean_list, &ian->ia_list); 1241219089Spjd mutex_exit(&itxg->itxg_lock); 1242168404Spjd } 1243219089Spjd while ((itx = list_head(&clean_list)) != NULL) { 1244219089Spjd list_remove(&clean_list, itx); 1245219089Spjd kmem_free(itx, offsetof(itx_t, itx_lr) + 1246219089Spjd itx->itx_lr.lrc_reclen); 1247219089Spjd } 1248219089Spjd list_destroy(&clean_list); 1249219089Spjd} 1250168404Spjd 1251219089Spjdvoid 1252219089Spjdzil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx) 1253219089Spjd{ 1254219089Spjd uint64_t txg; 1255219089Spjd itxg_t *itxg; 1256219089Spjd itxs_t *itxs, *clean = NULL; 1257219089Spjd 1258168404Spjd /* 1259219089Spjd * Object ids can be re-instantiated in the next txg so 1260219089Spjd * remove any async transactions to avoid future leaks. 1261219089Spjd * This can happen if a fsync occurs on the re-instantiated 1262219089Spjd * object for a WR_INDIRECT or WR_NEED_COPY write, which gets 1263219089Spjd * the new file data and flushes a write record for the old object. 1264168404Spjd */ 1265219089Spjd if ((itx->itx_lr.lrc_txtype & ~TX_CI) == TX_REMOVE) 1266219089Spjd zil_remove_async(zilog, itx->itx_oid); 1267219089Spjd 1268219089Spjd /* 1269219089Spjd * Ensure the data of a renamed file is committed before the rename. 1270219089Spjd */ 1271219089Spjd if ((itx->itx_lr.lrc_txtype & ~TX_CI) == TX_RENAME) 1272219089Spjd zil_async_to_sync(zilog, itx->itx_oid); 1273219089Spjd 1274239620Smm if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) 1275219089Spjd txg = ZILTEST_TXG; 1276219089Spjd else 1277219089Spjd txg = dmu_tx_get_txg(tx); 1278219089Spjd 1279219089Spjd itxg = &zilog->zl_itxg[txg & TXG_MASK]; 1280219089Spjd mutex_enter(&itxg->itxg_lock); 1281219089Spjd itxs = itxg->itxg_itxs; 1282219089Spjd if (itxg->itxg_txg != txg) { 1283219089Spjd if (itxs != NULL) { 1284219089Spjd /* 1285219089Spjd * The zil_clean callback hasn't got around to cleaning 1286219089Spjd * this itxg. Save the itxs for release below. 1287219089Spjd * This should be rare. 1288219089Spjd */ 1289219089Spjd atomic_add_64(&zilog->zl_itx_list_sz, -itxg->itxg_sod); 1290219089Spjd itxg->itxg_sod = 0; 1291219089Spjd clean = itxg->itxg_itxs; 1292219089Spjd } 1293219089Spjd ASSERT(itxg->itxg_sod == 0); 1294219089Spjd itxg->itxg_txg = txg; 1295219089Spjd itxs = itxg->itxg_itxs = kmem_zalloc(sizeof (itxs_t), KM_SLEEP); 1296219089Spjd 1297219089Spjd list_create(&itxs->i_sync_list, sizeof (itx_t), 1298219089Spjd offsetof(itx_t, itx_node)); 1299219089Spjd avl_create(&itxs->i_async_tree, zil_aitx_compare, 1300219089Spjd sizeof (itx_async_node_t), 1301219089Spjd offsetof(itx_async_node_t, ia_node)); 1302168404Spjd } 1303219089Spjd if (itx->itx_sync) { 1304219089Spjd list_insert_tail(&itxs->i_sync_list, itx); 1305219089Spjd atomic_add_64(&zilog->zl_itx_list_sz, itx->itx_sod); 1306219089Spjd itxg->itxg_sod += itx->itx_sod; 1307219089Spjd } else { 1308219089Spjd avl_tree_t *t = &itxs->i_async_tree; 1309219089Spjd uint64_t foid = ((lr_ooo_t *)&itx->itx_lr)->lr_foid; 1310219089Spjd itx_async_node_t *ian; 1311219089Spjd avl_index_t where; 1312168404Spjd 1313219089Spjd ian = avl_find(t, &foid, &where); 1314219089Spjd if (ian == NULL) { 1315219089Spjd ian = kmem_alloc(sizeof (itx_async_node_t), KM_SLEEP); 1316219089Spjd list_create(&ian->ia_list, sizeof (itx_t), 1317219089Spjd offsetof(itx_t, itx_node)); 1318219089Spjd ian->ia_foid = foid; 1319219089Spjd avl_insert(t, ian, where); 1320219089Spjd } 1321219089Spjd list_insert_tail(&ian->ia_list, itx); 1322168404Spjd } 1323219089Spjd 1324219089Spjd itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx); 1325239620Smm zilog_dirty(zilog, txg); 1326219089Spjd mutex_exit(&itxg->itxg_lock); 1327219089Spjd 1328219089Spjd /* Release the old itxs now we've dropped the lock */ 1329219089Spjd if (clean != NULL) 1330219089Spjd zil_itxg_clean(clean); 1331168404Spjd} 1332168404Spjd 1333168404Spjd/* 1334168404Spjd * If there are any in-memory intent log transactions which have now been 1335239620Smm * synced then start up a taskq to free them. We should only do this after we 1336239620Smm * have written out the uberblocks (i.e. txg has been comitted) so that 1337239620Smm * don't inadvertently clean out in-memory log records that would be required 1338239620Smm * by zil_commit(). 1339168404Spjd */ 1340168404Spjdvoid 1341219089Spjdzil_clean(zilog_t *zilog, uint64_t synced_txg) 1342168404Spjd{ 1343219089Spjd itxg_t *itxg = &zilog->zl_itxg[synced_txg & TXG_MASK]; 1344219089Spjd itxs_t *clean_me; 1345168404Spjd 1346219089Spjd mutex_enter(&itxg->itxg_lock); 1347219089Spjd if (itxg->itxg_itxs == NULL || itxg->itxg_txg == ZILTEST_TXG) { 1348219089Spjd mutex_exit(&itxg->itxg_lock); 1349219089Spjd return; 1350168404Spjd } 1351219089Spjd ASSERT3U(itxg->itxg_txg, <=, synced_txg); 1352219089Spjd ASSERT(itxg->itxg_txg != 0); 1353219089Spjd ASSERT(zilog->zl_clean_taskq != NULL); 1354219089Spjd atomic_add_64(&zilog->zl_itx_list_sz, -itxg->itxg_sod); 1355219089Spjd itxg->itxg_sod = 0; 1356219089Spjd clean_me = itxg->itxg_itxs; 1357219089Spjd itxg->itxg_itxs = NULL; 1358219089Spjd itxg->itxg_txg = 0; 1359219089Spjd mutex_exit(&itxg->itxg_lock); 1360219089Spjd /* 1361219089Spjd * Preferably start a task queue to free up the old itxs but 1362219089Spjd * if taskq_dispatch can't allocate resources to do that then 1363219089Spjd * free it in-line. This should be rare. Note, using TQ_SLEEP 1364219089Spjd * created a bad performance problem. 1365219089Spjd */ 1366219089Spjd if (taskq_dispatch(zilog->zl_clean_taskq, 1367219089Spjd (void (*)(void *))zil_itxg_clean, clean_me, TQ_NOSLEEP) == 0) 1368219089Spjd zil_itxg_clean(clean_me); 1369168404Spjd} 1370168404Spjd 1371219089Spjd/* 1372219089Spjd * Get the list of itxs to commit into zl_itx_commit_list. 1373219089Spjd */ 1374185029Spjdstatic void 1375219089Spjdzil_get_commit_list(zilog_t *zilog) 1376168404Spjd{ 1377219089Spjd uint64_t otxg, txg; 1378219089Spjd list_t *commit_list = &zilog->zl_itx_commit_list; 1379219089Spjd uint64_t push_sod = 0; 1380219089Spjd 1381219089Spjd if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */ 1382219089Spjd otxg = ZILTEST_TXG; 1383219089Spjd else 1384219089Spjd otxg = spa_last_synced_txg(zilog->zl_spa) + 1; 1385219089Spjd 1386219089Spjd for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) { 1387219089Spjd itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK]; 1388219089Spjd 1389219089Spjd mutex_enter(&itxg->itxg_lock); 1390219089Spjd if (itxg->itxg_txg != txg) { 1391219089Spjd mutex_exit(&itxg->itxg_lock); 1392219089Spjd continue; 1393219089Spjd } 1394219089Spjd 1395219089Spjd list_move_tail(commit_list, &itxg->itxg_itxs->i_sync_list); 1396219089Spjd push_sod += itxg->itxg_sod; 1397219089Spjd itxg->itxg_sod = 0; 1398219089Spjd 1399219089Spjd mutex_exit(&itxg->itxg_lock); 1400219089Spjd } 1401219089Spjd atomic_add_64(&zilog->zl_itx_list_sz, -push_sod); 1402219089Spjd} 1403219089Spjd 1404219089Spjd/* 1405219089Spjd * Move the async itxs for a specified object to commit into sync lists. 1406219089Spjd */ 1407219089Spjdstatic void 1408219089Spjdzil_async_to_sync(zilog_t *zilog, uint64_t foid) 1409219089Spjd{ 1410219089Spjd uint64_t otxg, txg; 1411219089Spjd itx_async_node_t *ian; 1412219089Spjd avl_tree_t *t; 1413219089Spjd avl_index_t where; 1414219089Spjd 1415219089Spjd if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */ 1416219089Spjd otxg = ZILTEST_TXG; 1417219089Spjd else 1418219089Spjd otxg = spa_last_synced_txg(zilog->zl_spa) + 1; 1419219089Spjd 1420219089Spjd for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) { 1421219089Spjd itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK]; 1422219089Spjd 1423219089Spjd mutex_enter(&itxg->itxg_lock); 1424219089Spjd if (itxg->itxg_txg != txg) { 1425219089Spjd mutex_exit(&itxg->itxg_lock); 1426219089Spjd continue; 1427219089Spjd } 1428219089Spjd 1429219089Spjd /* 1430219089Spjd * If a foid is specified then find that node and append its 1431219089Spjd * list. Otherwise walk the tree appending all the lists 1432219089Spjd * to the sync list. We add to the end rather than the 1433219089Spjd * beginning to ensure the create has happened. 1434219089Spjd */ 1435219089Spjd t = &itxg->itxg_itxs->i_async_tree; 1436219089Spjd if (foid != 0) { 1437219089Spjd ian = avl_find(t, &foid, &where); 1438219089Spjd if (ian != NULL) { 1439219089Spjd list_move_tail(&itxg->itxg_itxs->i_sync_list, 1440219089Spjd &ian->ia_list); 1441219089Spjd } 1442219089Spjd } else { 1443219089Spjd void *cookie = NULL; 1444219089Spjd 1445219089Spjd while ((ian = avl_destroy_nodes(t, &cookie)) != NULL) { 1446219089Spjd list_move_tail(&itxg->itxg_itxs->i_sync_list, 1447219089Spjd &ian->ia_list); 1448219089Spjd list_destroy(&ian->ia_list); 1449219089Spjd kmem_free(ian, sizeof (itx_async_node_t)); 1450219089Spjd } 1451219089Spjd } 1452219089Spjd mutex_exit(&itxg->itxg_lock); 1453219089Spjd } 1454219089Spjd} 1455219089Spjd 1456219089Spjdstatic void 1457219089Spjdzil_commit_writer(zilog_t *zilog) 1458219089Spjd{ 1459168404Spjd uint64_t txg; 1460219089Spjd itx_t *itx; 1461168404Spjd lwb_t *lwb; 1462219089Spjd spa_t *spa = zilog->zl_spa; 1463219089Spjd int error = 0; 1464168404Spjd 1465185029Spjd ASSERT(zilog->zl_root_zio == NULL); 1466168404Spjd 1467219089Spjd mutex_exit(&zilog->zl_lock); 1468219089Spjd 1469219089Spjd zil_get_commit_list(zilog); 1470219089Spjd 1471219089Spjd /* 1472219089Spjd * Return if there's nothing to commit before we dirty the fs by 1473219089Spjd * calling zil_create(). 1474219089Spjd */ 1475219089Spjd if (list_head(&zilog->zl_itx_commit_list) == NULL) { 1476219089Spjd mutex_enter(&zilog->zl_lock); 1477219089Spjd return; 1478219089Spjd } 1479219089Spjd 1480168404Spjd if (zilog->zl_suspend) { 1481168404Spjd lwb = NULL; 1482168404Spjd } else { 1483168404Spjd lwb = list_tail(&zilog->zl_lwb_list); 1484219089Spjd if (lwb == NULL) 1485219089Spjd lwb = zil_create(zilog); 1486168404Spjd } 1487168404Spjd 1488168404Spjd DTRACE_PROBE1(zil__cw1, zilog_t *, zilog); 1489219089Spjd while (itx = list_head(&zilog->zl_itx_commit_list)) { 1490168404Spjd txg = itx->itx_lr.lrc_txg; 1491168404Spjd ASSERT(txg); 1492168404Spjd 1493219089Spjd if (txg > spa_last_synced_txg(spa) || txg > spa_freeze_txg(spa)) 1494168404Spjd lwb = zil_lwb_commit(zilog, itx, lwb); 1495219089Spjd list_remove(&zilog->zl_itx_commit_list, itx); 1496168404Spjd kmem_free(itx, offsetof(itx_t, itx_lr) 1497168404Spjd + itx->itx_lr.lrc_reclen); 1498168404Spjd } 1499168404Spjd DTRACE_PROBE1(zil__cw2, zilog_t *, zilog); 1500168404Spjd 1501168404Spjd /* write the last block out */ 1502168404Spjd if (lwb != NULL && lwb->lwb_zio != NULL) 1503168404Spjd lwb = zil_lwb_write_start(zilog, lwb); 1504168404Spjd 1505168404Spjd zilog->zl_cur_used = 0; 1506168404Spjd 1507168404Spjd /* 1508168404Spjd * Wait if necessary for the log blocks to be on stable storage. 1509168404Spjd */ 1510168404Spjd if (zilog->zl_root_zio) { 1511219089Spjd error = zio_wait(zilog->zl_root_zio); 1512185029Spjd zilog->zl_root_zio = NULL; 1513185029Spjd zil_flush_vdevs(zilog); 1514168404Spjd } 1515168404Spjd 1516219089Spjd if (error || lwb == NULL) 1517168404Spjd txg_wait_synced(zilog->zl_dmu_pool, 0); 1518168404Spjd 1519168404Spjd mutex_enter(&zilog->zl_lock); 1520168404Spjd 1521219089Spjd /* 1522219089Spjd * Remember the highest committed log sequence number for ztest. 1523219089Spjd * We only update this value when all the log writes succeeded, 1524219089Spjd * because ztest wants to ASSERT that it got the whole log chain. 1525219089Spjd */ 1526219089Spjd if (error == 0 && lwb != NULL) 1527219089Spjd zilog->zl_commit_lr_seq = zilog->zl_lr_seq; 1528168404Spjd} 1529168404Spjd 1530168404Spjd/* 1531219089Spjd * Commit zfs transactions to stable storage. 1532168404Spjd * If foid is 0 push out all transactions, otherwise push only those 1533219089Spjd * for that object or might reference that object. 1534219089Spjd * 1535219089Spjd * itxs are committed in batches. In a heavily stressed zil there will be 1536219089Spjd * a commit writer thread who is writing out a bunch of itxs to the log 1537219089Spjd * for a set of committing threads (cthreads) in the same batch as the writer. 1538219089Spjd * Those cthreads are all waiting on the same cv for that batch. 1539219089Spjd * 1540219089Spjd * There will also be a different and growing batch of threads that are 1541219089Spjd * waiting to commit (qthreads). When the committing batch completes 1542219089Spjd * a transition occurs such that the cthreads exit and the qthreads become 1543219089Spjd * cthreads. One of the new cthreads becomes the writer thread for the 1544219089Spjd * batch. Any new threads arriving become new qthreads. 1545219089Spjd * 1546219089Spjd * Only 2 condition variables are needed and there's no transition 1547219089Spjd * between the two cvs needed. They just flip-flop between qthreads 1548219089Spjd * and cthreads. 1549219089Spjd * 1550219089Spjd * Using this scheme we can efficiently wakeup up only those threads 1551219089Spjd * that have been committed. 1552168404Spjd */ 1553168404Spjdvoid 1554219089Spjdzil_commit(zilog_t *zilog, uint64_t foid) 1555168404Spjd{ 1556219089Spjd uint64_t mybatch; 1557219089Spjd 1558219089Spjd if (zilog->zl_sync == ZFS_SYNC_DISABLED) 1559168404Spjd return; 1560168404Spjd 1561219089Spjd /* move the async itxs for the foid to the sync queues */ 1562219089Spjd zil_async_to_sync(zilog, foid); 1563219089Spjd 1564168404Spjd mutex_enter(&zilog->zl_lock); 1565219089Spjd mybatch = zilog->zl_next_batch; 1566168404Spjd while (zilog->zl_writer) { 1567219089Spjd cv_wait(&zilog->zl_cv_batch[mybatch & 1], &zilog->zl_lock); 1568219089Spjd if (mybatch <= zilog->zl_com_batch) { 1569168404Spjd mutex_exit(&zilog->zl_lock); 1570168404Spjd return; 1571168404Spjd } 1572168404Spjd } 1573219089Spjd 1574219089Spjd zilog->zl_next_batch++; 1575219089Spjd zilog->zl_writer = B_TRUE; 1576219089Spjd zil_commit_writer(zilog); 1577219089Spjd zilog->zl_com_batch = mybatch; 1578219089Spjd zilog->zl_writer = B_FALSE; 1579168404Spjd mutex_exit(&zilog->zl_lock); 1580219089Spjd 1581219089Spjd /* wake up one thread to become the next writer */ 1582219089Spjd cv_signal(&zilog->zl_cv_batch[(mybatch+1) & 1]); 1583219089Spjd 1584219089Spjd /* wake up all threads waiting for this batch to be committed */ 1585219089Spjd cv_broadcast(&zilog->zl_cv_batch[mybatch & 1]); 1586168404Spjd} 1587168404Spjd 1588168404Spjd/* 1589168404Spjd * Called in syncing context to free committed log blocks and update log header. 1590168404Spjd */ 1591168404Spjdvoid 1592168404Spjdzil_sync(zilog_t *zilog, dmu_tx_t *tx) 1593168404Spjd{ 1594168404Spjd zil_header_t *zh = zil_header_in_syncing_context(zilog); 1595168404Spjd uint64_t txg = dmu_tx_get_txg(tx); 1596168404Spjd spa_t *spa = zilog->zl_spa; 1597219089Spjd uint64_t *replayed_seq = &zilog->zl_replayed_seq[txg & TXG_MASK]; 1598168404Spjd lwb_t *lwb; 1599168404Spjd 1600209962Smm /* 1601209962Smm * We don't zero out zl_destroy_txg, so make sure we don't try 1602209962Smm * to destroy it twice. 1603209962Smm */ 1604209962Smm if (spa_sync_pass(spa) != 1) 1605209962Smm return; 1606209962Smm 1607168404Spjd mutex_enter(&zilog->zl_lock); 1608168404Spjd 1609168404Spjd ASSERT(zilog->zl_stop_sync == 0); 1610168404Spjd 1611219089Spjd if (*replayed_seq != 0) { 1612219089Spjd ASSERT(zh->zh_replay_seq < *replayed_seq); 1613219089Spjd zh->zh_replay_seq = *replayed_seq; 1614219089Spjd *replayed_seq = 0; 1615219089Spjd } 1616168404Spjd 1617168404Spjd if (zilog->zl_destroy_txg == txg) { 1618168404Spjd blkptr_t blk = zh->zh_log; 1619168404Spjd 1620168404Spjd ASSERT(list_head(&zilog->zl_lwb_list) == NULL); 1621168404Spjd 1622168404Spjd bzero(zh, sizeof (zil_header_t)); 1623209962Smm bzero(zilog->zl_replayed_seq, sizeof (zilog->zl_replayed_seq)); 1624168404Spjd 1625168404Spjd if (zilog->zl_keep_first) { 1626168404Spjd /* 1627168404Spjd * If this block was part of log chain that couldn't 1628168404Spjd * be claimed because a device was missing during 1629168404Spjd * zil_claim(), but that device later returns, 1630168404Spjd * then this block could erroneously appear valid. 1631168404Spjd * To guard against this, assign a new GUID to the new 1632168404Spjd * log chain so it doesn't matter what blk points to. 1633168404Spjd */ 1634168404Spjd zil_init_log_chain(zilog, &blk); 1635168404Spjd zh->zh_log = blk; 1636168404Spjd } 1637168404Spjd } 1638168404Spjd 1639213197Smm while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 1640168404Spjd zh->zh_log = lwb->lwb_blk; 1641168404Spjd if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg) 1642168404Spjd break; 1643168404Spjd list_remove(&zilog->zl_lwb_list, lwb); 1644219089Spjd zio_free_zil(spa, txg, &lwb->lwb_blk); 1645168404Spjd kmem_cache_free(zil_lwb_cache, lwb); 1646168404Spjd 1647168404Spjd /* 1648168404Spjd * If we don't have anything left in the lwb list then 1649168404Spjd * we've had an allocation failure and we need to zero 1650168404Spjd * out the zil_header blkptr so that we don't end 1651168404Spjd * up freeing the same block twice. 1652168404Spjd */ 1653168404Spjd if (list_head(&zilog->zl_lwb_list) == NULL) 1654168404Spjd BP_ZERO(&zh->zh_log); 1655168404Spjd } 1656168404Spjd mutex_exit(&zilog->zl_lock); 1657168404Spjd} 1658168404Spjd 1659168404Spjdvoid 1660168404Spjdzil_init(void) 1661168404Spjd{ 1662168404Spjd zil_lwb_cache = kmem_cache_create("zil_lwb_cache", 1663168404Spjd sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0); 1664168404Spjd} 1665168404Spjd 1666168404Spjdvoid 1667168404Spjdzil_fini(void) 1668168404Spjd{ 1669168404Spjd kmem_cache_destroy(zil_lwb_cache); 1670168404Spjd} 1671168404Spjd 1672219089Spjdvoid 1673219089Spjdzil_set_sync(zilog_t *zilog, uint64_t sync) 1674219089Spjd{ 1675219089Spjd zilog->zl_sync = sync; 1676219089Spjd} 1677219089Spjd 1678219089Spjdvoid 1679219089Spjdzil_set_logbias(zilog_t *zilog, uint64_t logbias) 1680219089Spjd{ 1681219089Spjd zilog->zl_logbias = logbias; 1682219089Spjd} 1683219089Spjd 1684168404Spjdzilog_t * 1685168404Spjdzil_alloc(objset_t *os, zil_header_t *zh_phys) 1686168404Spjd{ 1687168404Spjd zilog_t *zilog; 1688168404Spjd 1689168404Spjd zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP); 1690168404Spjd 1691168404Spjd zilog->zl_header = zh_phys; 1692168404Spjd zilog->zl_os = os; 1693168404Spjd zilog->zl_spa = dmu_objset_spa(os); 1694168404Spjd zilog->zl_dmu_pool = dmu_objset_pool(os); 1695168404Spjd zilog->zl_destroy_txg = TXG_INITIAL - 1; 1696219089Spjd zilog->zl_logbias = dmu_objset_logbias(os); 1697219089Spjd zilog->zl_sync = dmu_objset_syncprop(os); 1698219089Spjd zilog->zl_next_batch = 1; 1699168404Spjd 1700168404Spjd mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL); 1701168404Spjd 1702219089Spjd for (int i = 0; i < TXG_SIZE; i++) { 1703219089Spjd mutex_init(&zilog->zl_itxg[i].itxg_lock, NULL, 1704219089Spjd MUTEX_DEFAULT, NULL); 1705219089Spjd } 1706168404Spjd 1707168404Spjd list_create(&zilog->zl_lwb_list, sizeof (lwb_t), 1708168404Spjd offsetof(lwb_t, lwb_node)); 1709168404Spjd 1710219089Spjd list_create(&zilog->zl_itx_commit_list, sizeof (itx_t), 1711219089Spjd offsetof(itx_t, itx_node)); 1712219089Spjd 1713185029Spjd mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL); 1714168404Spjd 1715185029Spjd avl_create(&zilog->zl_vdev_tree, zil_vdev_compare, 1716185029Spjd sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node)); 1717185029Spjd 1718185029Spjd cv_init(&zilog->zl_cv_writer, NULL, CV_DEFAULT, NULL); 1719185029Spjd cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL); 1720219089Spjd cv_init(&zilog->zl_cv_batch[0], NULL, CV_DEFAULT, NULL); 1721219089Spjd cv_init(&zilog->zl_cv_batch[1], NULL, CV_DEFAULT, NULL); 1722185029Spjd 1723168404Spjd return (zilog); 1724168404Spjd} 1725168404Spjd 1726168404Spjdvoid 1727168404Spjdzil_free(zilog_t *zilog) 1728168404Spjd{ 1729168404Spjd zilog->zl_stop_sync = 1; 1730168404Spjd 1731248571Smm ASSERT0(zilog->zl_suspend); 1732248571Smm ASSERT0(zilog->zl_suspending); 1733248571Smm 1734224526Smm ASSERT(list_is_empty(&zilog->zl_lwb_list)); 1735168404Spjd list_destroy(&zilog->zl_lwb_list); 1736168404Spjd 1737185029Spjd avl_destroy(&zilog->zl_vdev_tree); 1738185029Spjd mutex_destroy(&zilog->zl_vdev_lock); 1739168404Spjd 1740219089Spjd ASSERT(list_is_empty(&zilog->zl_itx_commit_list)); 1741219089Spjd list_destroy(&zilog->zl_itx_commit_list); 1742219089Spjd 1743219089Spjd for (int i = 0; i < TXG_SIZE; i++) { 1744219089Spjd /* 1745219089Spjd * It's possible for an itx to be generated that doesn't dirty 1746219089Spjd * a txg (e.g. ztest TX_TRUNCATE). So there's no zil_clean() 1747219089Spjd * callback to remove the entry. We remove those here. 1748219089Spjd * 1749219089Spjd * Also free up the ziltest itxs. 1750219089Spjd */ 1751219089Spjd if (zilog->zl_itxg[i].itxg_itxs) 1752219089Spjd zil_itxg_clean(zilog->zl_itxg[i].itxg_itxs); 1753219089Spjd mutex_destroy(&zilog->zl_itxg[i].itxg_lock); 1754219089Spjd } 1755219089Spjd 1756168404Spjd mutex_destroy(&zilog->zl_lock); 1757168404Spjd 1758185029Spjd cv_destroy(&zilog->zl_cv_writer); 1759185029Spjd cv_destroy(&zilog->zl_cv_suspend); 1760219089Spjd cv_destroy(&zilog->zl_cv_batch[0]); 1761219089Spjd cv_destroy(&zilog->zl_cv_batch[1]); 1762185029Spjd 1763168404Spjd kmem_free(zilog, sizeof (zilog_t)); 1764168404Spjd} 1765168404Spjd 1766168404Spjd/* 1767168404Spjd * Open an intent log. 1768168404Spjd */ 1769168404Spjdzilog_t * 1770168404Spjdzil_open(objset_t *os, zil_get_data_t *get_data) 1771168404Spjd{ 1772168404Spjd zilog_t *zilog = dmu_objset_zil(os); 1773168404Spjd 1774224526Smm ASSERT(zilog->zl_clean_taskq == NULL); 1775224526Smm ASSERT(zilog->zl_get_data == NULL); 1776224526Smm ASSERT(list_is_empty(&zilog->zl_lwb_list)); 1777224526Smm 1778168404Spjd zilog->zl_get_data = get_data; 1779168404Spjd zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri, 1780168404Spjd 2, 2, TASKQ_PREPOPULATE); 1781168404Spjd 1782168404Spjd return (zilog); 1783168404Spjd} 1784168404Spjd 1785168404Spjd/* 1786168404Spjd * Close an intent log. 1787168404Spjd */ 1788168404Spjdvoid 1789168404Spjdzil_close(zilog_t *zilog) 1790168404Spjd{ 1791224526Smm lwb_t *lwb; 1792219089Spjd uint64_t txg = 0; 1793219089Spjd 1794219089Spjd zil_commit(zilog, 0); /* commit all itx */ 1795219089Spjd 1796168404Spjd /* 1797219089Spjd * The lwb_max_txg for the stubby lwb will reflect the last activity 1798219089Spjd * for the zil. After a txg_wait_synced() on the txg we know all the 1799219089Spjd * callbacks have occurred that may clean the zil. Only then can we 1800219089Spjd * destroy the zl_clean_taskq. 1801168404Spjd */ 1802219089Spjd mutex_enter(&zilog->zl_lock); 1803224526Smm lwb = list_tail(&zilog->zl_lwb_list); 1804224526Smm if (lwb != NULL) 1805224526Smm txg = lwb->lwb_max_txg; 1806219089Spjd mutex_exit(&zilog->zl_lock); 1807219089Spjd if (txg) 1808168404Spjd txg_wait_synced(zilog->zl_dmu_pool, txg); 1809239620Smm ASSERT(!zilog_is_dirty(zilog)); 1810168404Spjd 1811168404Spjd taskq_destroy(zilog->zl_clean_taskq); 1812168404Spjd zilog->zl_clean_taskq = NULL; 1813168404Spjd zilog->zl_get_data = NULL; 1814224526Smm 1815224526Smm /* 1816224526Smm * We should have only one LWB left on the list; remove it now. 1817224526Smm */ 1818224526Smm mutex_enter(&zilog->zl_lock); 1819224526Smm lwb = list_head(&zilog->zl_lwb_list); 1820224526Smm if (lwb != NULL) { 1821224526Smm ASSERT(lwb == list_tail(&zilog->zl_lwb_list)); 1822224526Smm list_remove(&zilog->zl_lwb_list, lwb); 1823224526Smm zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 1824224526Smm kmem_cache_free(zil_lwb_cache, lwb); 1825224526Smm } 1826224526Smm mutex_exit(&zilog->zl_lock); 1827168404Spjd} 1828168404Spjd 1829248571Smmstatic char *suspend_tag = "zil suspending"; 1830248571Smm 1831168404Spjd/* 1832168404Spjd * Suspend an intent log. While in suspended mode, we still honor 1833168404Spjd * synchronous semantics, but we rely on txg_wait_synced() to do it. 1834248571Smm * On old version pools, we suspend the log briefly when taking a 1835248571Smm * snapshot so that it will have an empty intent log. 1836248571Smm * 1837248571Smm * Long holds are not really intended to be used the way we do here -- 1838248571Smm * held for such a short time. A concurrent caller of dsl_dataset_long_held() 1839248571Smm * could fail. Therefore we take pains to only put a long hold if it is 1840248571Smm * actually necessary. Fortunately, it will only be necessary if the 1841248571Smm * objset is currently mounted (or the ZVOL equivalent). In that case it 1842248571Smm * will already have a long hold, so we are not really making things any worse. 1843248571Smm * 1844248571Smm * Ideally, we would locate the existing long-holder (i.e. the zfsvfs_t or 1845248571Smm * zvol_state_t), and use their mechanism to prevent their hold from being 1846248571Smm * dropped (e.g. VFS_HOLD()). However, that would be even more pain for 1847248571Smm * very little gain. 1848248571Smm * 1849248571Smm * if cookiep == NULL, this does both the suspend & resume. 1850248571Smm * Otherwise, it returns with the dataset "long held", and the cookie 1851248571Smm * should be passed into zil_resume(). 1852168404Spjd */ 1853168404Spjdint 1854248571Smmzil_suspend(const char *osname, void **cookiep) 1855168404Spjd{ 1856248571Smm objset_t *os; 1857248571Smm zilog_t *zilog; 1858248571Smm const zil_header_t *zh; 1859248571Smm int error; 1860168404Spjd 1861248571Smm error = dmu_objset_hold(osname, suspend_tag, &os); 1862248571Smm if (error != 0) 1863248571Smm return (error); 1864248571Smm zilog = dmu_objset_zil(os); 1865248571Smm 1866168404Spjd mutex_enter(&zilog->zl_lock); 1867248571Smm zh = zilog->zl_header; 1868248571Smm 1869200724Sdelphij if (zh->zh_flags & ZIL_REPLAY_NEEDED) { /* unplayed log */ 1870168404Spjd mutex_exit(&zilog->zl_lock); 1871248571Smm dmu_objset_rele(os, suspend_tag); 1872249195Smm return (SET_ERROR(EBUSY)); 1873168404Spjd } 1874248571Smm 1875248571Smm /* 1876248571Smm * Don't put a long hold in the cases where we can avoid it. This 1877248571Smm * is when there is no cookie so we are doing a suspend & resume 1878248571Smm * (i.e. called from zil_vdev_offline()), and there's nothing to do 1879248571Smm * for the suspend because it's already suspended, or there's no ZIL. 1880248571Smm */ 1881248571Smm if (cookiep == NULL && !zilog->zl_suspending && 1882248571Smm (zilog->zl_suspend > 0 || BP_IS_HOLE(&zh->zh_log))) { 1883248571Smm mutex_exit(&zilog->zl_lock); 1884248571Smm dmu_objset_rele(os, suspend_tag); 1885248571Smm return (0); 1886248571Smm } 1887248571Smm 1888248571Smm dsl_dataset_long_hold(dmu_objset_ds(os), suspend_tag); 1889248571Smm dsl_pool_rele(dmu_objset_pool(os), suspend_tag); 1890248571Smm 1891248571Smm zilog->zl_suspend++; 1892248571Smm 1893248571Smm if (zilog->zl_suspend > 1) { 1894168404Spjd /* 1895248571Smm * Someone else is already suspending it. 1896168404Spjd * Just wait for them to finish. 1897168404Spjd */ 1898248571Smm 1899168404Spjd while (zilog->zl_suspending) 1900168404Spjd cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock); 1901168404Spjd mutex_exit(&zilog->zl_lock); 1902248571Smm 1903248571Smm if (cookiep == NULL) 1904248571Smm zil_resume(os); 1905248571Smm else 1906248571Smm *cookiep = os; 1907168404Spjd return (0); 1908168404Spjd } 1909248571Smm 1910248571Smm /* 1911248571Smm * If there is no pointer to an on-disk block, this ZIL must not 1912248571Smm * be active (e.g. filesystem not mounted), so there's nothing 1913248571Smm * to clean up. 1914248571Smm */ 1915248571Smm if (BP_IS_HOLE(&zh->zh_log)) { 1916248571Smm ASSERT(cookiep != NULL); /* fast path already handled */ 1917248571Smm 1918248571Smm *cookiep = os; 1919248571Smm mutex_exit(&zilog->zl_lock); 1920248571Smm return (0); 1921248571Smm } 1922248571Smm 1923168404Spjd zilog->zl_suspending = B_TRUE; 1924168404Spjd mutex_exit(&zilog->zl_lock); 1925168404Spjd 1926219089Spjd zil_commit(zilog, 0); 1927168404Spjd 1928168404Spjd zil_destroy(zilog, B_FALSE); 1929168404Spjd 1930168404Spjd mutex_enter(&zilog->zl_lock); 1931168404Spjd zilog->zl_suspending = B_FALSE; 1932168404Spjd cv_broadcast(&zilog->zl_cv_suspend); 1933168404Spjd mutex_exit(&zilog->zl_lock); 1934168404Spjd 1935248571Smm if (cookiep == NULL) 1936248571Smm zil_resume(os); 1937248571Smm else 1938248571Smm *cookiep = os; 1939168404Spjd return (0); 1940168404Spjd} 1941168404Spjd 1942168404Spjdvoid 1943248571Smmzil_resume(void *cookie) 1944168404Spjd{ 1945248571Smm objset_t *os = cookie; 1946248571Smm zilog_t *zilog = dmu_objset_zil(os); 1947248571Smm 1948168404Spjd mutex_enter(&zilog->zl_lock); 1949168404Spjd ASSERT(zilog->zl_suspend != 0); 1950168404Spjd zilog->zl_suspend--; 1951168404Spjd mutex_exit(&zilog->zl_lock); 1952248571Smm dsl_dataset_long_rele(dmu_objset_ds(os), suspend_tag); 1953248571Smm dsl_dataset_rele(dmu_objset_ds(os), suspend_tag); 1954168404Spjd} 1955168404Spjd 1956219089Spjdtypedef struct zil_replay_arg { 1957219089Spjd zil_replay_func_t **zr_replay; 1958219089Spjd void *zr_arg; 1959219089Spjd boolean_t zr_byteswap; 1960219089Spjd char *zr_lr; 1961219089Spjd} zil_replay_arg_t; 1962219089Spjd 1963219089Spjdstatic int 1964219089Spjdzil_replay_error(zilog_t *zilog, lr_t *lr, int error) 1965209962Smm{ 1966219089Spjd char name[MAXNAMELEN]; 1967209962Smm 1968219089Spjd zilog->zl_replaying_seq--; /* didn't actually replay this one */ 1969209962Smm 1970219089Spjd dmu_objset_name(zilog->zl_os, name); 1971209962Smm 1972219089Spjd cmn_err(CE_WARN, "ZFS replay transaction error %d, " 1973219089Spjd "dataset %s, seq 0x%llx, txtype %llu %s\n", error, name, 1974219089Spjd (u_longlong_t)lr->lrc_seq, 1975219089Spjd (u_longlong_t)(lr->lrc_txtype & ~TX_CI), 1976219089Spjd (lr->lrc_txtype & TX_CI) ? "CI" : ""); 1977219089Spjd 1978219089Spjd return (error); 1979209962Smm} 1980209962Smm 1981219089Spjdstatic int 1982168404Spjdzil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg) 1983168404Spjd{ 1984168404Spjd zil_replay_arg_t *zr = zra; 1985168404Spjd const zil_header_t *zh = zilog->zl_header; 1986168404Spjd uint64_t reclen = lr->lrc_reclen; 1987168404Spjd uint64_t txtype = lr->lrc_txtype; 1988219089Spjd int error = 0; 1989168404Spjd 1990219089Spjd zilog->zl_replaying_seq = lr->lrc_seq; 1991168404Spjd 1992219089Spjd if (lr->lrc_seq <= zh->zh_replay_seq) /* already replayed */ 1993219089Spjd return (0); 1994219089Spjd 1995168404Spjd if (lr->lrc_txg < claim_txg) /* already committed */ 1996219089Spjd return (0); 1997168404Spjd 1998185029Spjd /* Strip case-insensitive bit, still present in log record */ 1999185029Spjd txtype &= ~TX_CI; 2000185029Spjd 2001219089Spjd if (txtype == 0 || txtype >= TX_MAX_TYPE) 2002219089Spjd return (zil_replay_error(zilog, lr, EINVAL)); 2003219089Spjd 2004219089Spjd /* 2005219089Spjd * If this record type can be logged out of order, the object 2006219089Spjd * (lr_foid) may no longer exist. That's legitimate, not an error. 2007219089Spjd */ 2008219089Spjd if (TX_OOO(txtype)) { 2009219089Spjd error = dmu_object_info(zilog->zl_os, 2010219089Spjd ((lr_ooo_t *)lr)->lr_foid, NULL); 2011219089Spjd if (error == ENOENT || error == EEXIST) 2012219089Spjd return (0); 2013209962Smm } 2014209962Smm 2015168404Spjd /* 2016168404Spjd * Make a copy of the data so we can revise and extend it. 2017168404Spjd */ 2018219089Spjd bcopy(lr, zr->zr_lr, reclen); 2019168404Spjd 2020168404Spjd /* 2021219089Spjd * If this is a TX_WRITE with a blkptr, suck in the data. 2022219089Spjd */ 2023219089Spjd if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) { 2024219089Spjd error = zil_read_log_data(zilog, (lr_write_t *)lr, 2025219089Spjd zr->zr_lr + reclen); 2026248571Smm if (error != 0) 2027219089Spjd return (zil_replay_error(zilog, lr, error)); 2028219089Spjd } 2029219089Spjd 2030219089Spjd /* 2031168404Spjd * The log block containing this lr may have been byteswapped 2032168404Spjd * so that we can easily examine common fields like lrc_txtype. 2033219089Spjd * However, the log is a mix of different record types, and only the 2034168404Spjd * replay vectors know how to byteswap their records. Therefore, if 2035168404Spjd * the lr was byteswapped, undo it before invoking the replay vector. 2036168404Spjd */ 2037168404Spjd if (zr->zr_byteswap) 2038219089Spjd byteswap_uint64_array(zr->zr_lr, reclen); 2039168404Spjd 2040168404Spjd /* 2041168404Spjd * We must now do two things atomically: replay this log record, 2042209962Smm * and update the log header sequence number to reflect the fact that 2043209962Smm * we did so. At the end of each replay function the sequence number 2044209962Smm * is updated if we are in replay mode. 2045168404Spjd */ 2046219089Spjd error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, zr->zr_byteswap); 2047248571Smm if (error != 0) { 2048168404Spjd /* 2049168404Spjd * The DMU's dnode layer doesn't see removes until the txg 2050168404Spjd * commits, so a subsequent claim can spuriously fail with 2051209962Smm * EEXIST. So if we receive any error we try syncing out 2052219089Spjd * any removes then retry the transaction. Note that we 2053219089Spjd * specify B_FALSE for byteswap now, so we don't do it twice. 2054168404Spjd */ 2055219089Spjd txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0); 2056219089Spjd error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, B_FALSE); 2057248571Smm if (error != 0) 2058219089Spjd return (zil_replay_error(zilog, lr, error)); 2059168404Spjd } 2060219089Spjd return (0); 2061168404Spjd} 2062168404Spjd 2063168404Spjd/* ARGSUSED */ 2064219089Spjdstatic int 2065168404Spjdzil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg) 2066168404Spjd{ 2067168404Spjd zilog->zl_replay_blks++; 2068219089Spjd 2069219089Spjd return (0); 2070168404Spjd} 2071168404Spjd 2072168404Spjd/* 2073168404Spjd * If this dataset has a non-empty intent log, replay it and destroy it. 2074168404Spjd */ 2075168404Spjdvoid 2076209962Smmzil_replay(objset_t *os, void *arg, zil_replay_func_t *replay_func[TX_MAX_TYPE]) 2077168404Spjd{ 2078168404Spjd zilog_t *zilog = dmu_objset_zil(os); 2079168404Spjd const zil_header_t *zh = zilog->zl_header; 2080168404Spjd zil_replay_arg_t zr; 2081168404Spjd 2082200724Sdelphij if ((zh->zh_flags & ZIL_REPLAY_NEEDED) == 0) { 2083168404Spjd zil_destroy(zilog, B_TRUE); 2084168404Spjd return; 2085168404Spjd } 2086168404Spjd //printf("ZFS: Replaying ZIL on %s...\n", os->os->os_spa->spa_name); 2087168404Spjd 2088168404Spjd zr.zr_replay = replay_func; 2089168404Spjd zr.zr_arg = arg; 2090168404Spjd zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log); 2091219089Spjd zr.zr_lr = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP); 2092168404Spjd 2093168404Spjd /* 2094168404Spjd * Wait for in-progress removes to sync before starting replay. 2095168404Spjd */ 2096168404Spjd txg_wait_synced(zilog->zl_dmu_pool, 0); 2097168404Spjd 2098209962Smm zilog->zl_replay = B_TRUE; 2099219089Spjd zilog->zl_replay_time = ddi_get_lbolt(); 2100168404Spjd ASSERT(zilog->zl_replay_blks == 0); 2101168404Spjd (void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr, 2102168404Spjd zh->zh_claim_txg); 2103219089Spjd kmem_free(zr.zr_lr, 2 * SPA_MAXBLOCKSIZE); 2104168404Spjd 2105168404Spjd zil_destroy(zilog, B_FALSE); 2106185029Spjd txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 2107209962Smm zilog->zl_replay = B_FALSE; 2108168404Spjd //printf("ZFS: Replay of ZIL on %s finished.\n", os->os->os_spa->spa_name); 2109168404Spjd} 2110168404Spjd 2111219089Spjdboolean_t 2112219089Spjdzil_replaying(zilog_t *zilog, dmu_tx_t *tx) 2113168404Spjd{ 2114219089Spjd if (zilog->zl_sync == ZFS_SYNC_DISABLED) 2115219089Spjd return (B_TRUE); 2116168404Spjd 2117219089Spjd if (zilog->zl_replay) { 2118219089Spjd dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 2119219089Spjd zilog->zl_replayed_seq[dmu_tx_get_txg(tx) & TXG_MASK] = 2120219089Spjd zilog->zl_replaying_seq; 2121219089Spjd return (B_TRUE); 2122168404Spjd } 2123168404Spjd 2124219089Spjd return (B_FALSE); 2125168404Spjd} 2126213197Smm 2127213197Smm/* ARGSUSED */ 2128213197Smmint 2129219089Spjdzil_vdev_offline(const char *osname, void *arg) 2130213197Smm{ 2131213197Smm int error; 2132213197Smm 2133248571Smm error = zil_suspend(osname, NULL); 2134248571Smm if (error != 0) 2135249195Smm return (SET_ERROR(EEXIST)); 2136248571Smm return (0); 2137213197Smm} 2138