1 1.1 haad /* 2 1.1 haad * CDDL HEADER START 3 1.1 haad * 4 1.1 haad * The contents of this file are subject to the terms of the 5 1.1 haad * Common Development and Distribution License (the "License"). 6 1.1 haad * You may not use this file except in compliance with the License. 7 1.1 haad * 8 1.1 haad * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 1.1 haad * or http://www.opensolaris.org/os/licensing. 10 1.1 haad * See the License for the specific language governing permissions 11 1.1 haad * and limitations under the License. 12 1.1 haad * 13 1.1 haad * When distributing Covered Code, include this CDDL HEADER in each 14 1.1 haad * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 1.1 haad * If applicable, add the following below this CDDL HEADER, with the 16 1.1 haad * fields enclosed by brackets "[]" replaced with your own identifying 17 1.1 haad * information: Portions Copyright [yyyy] [name of copyright owner] 18 1.1 haad * 19 1.1 haad * CDDL HEADER END 20 1.1 haad */ 21 1.1 haad /* 22 1.1.1.3 chs * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23 1.1.1.3 chs * Copyright (c) 2011, 2016 by Delphix. All rights reserved. 24 1.1.1.3 chs * Copyright (c) 2014 Integros [integros.com] 25 1.1 haad */ 26 1.1 haad 27 1.1.1.3 chs /* Portions Copyright 2010 Robert Milkowski */ 28 1.1.1.3 chs 29 1.1 haad #include <sys/zfs_context.h> 30 1.1 haad #include <sys/spa.h> 31 1.1 haad #include <sys/dmu.h> 32 1.1 haad #include <sys/zap.h> 33 1.1 haad #include <sys/arc.h> 34 1.1 haad #include <sys/stat.h> 35 1.1 haad #include <sys/resource.h> 36 1.1 haad #include <sys/zil.h> 37 1.1 haad #include <sys/zil_impl.h> 38 1.1 haad #include <sys/dsl_dataset.h> 39 1.1.1.3 chs #include <sys/vdev_impl.h> 40 1.1 haad #include <sys/dmu_tx.h> 41 1.1.1.3 chs #include <sys/dsl_pool.h> 42 1.1 haad 43 1.1 haad /* 44 1.1 haad * The zfs intent log (ZIL) saves transaction records of system calls 45 1.1 haad * that change the file system in memory with enough information 46 1.1 haad * to be able to replay them. These are stored in memory until 47 1.1 haad * either the DMU transaction group (txg) commits them to the stable pool 48 1.1 haad * and they can be discarded, or they are flushed to the stable log 49 1.1 haad * (also in the pool) due to a fsync, O_DSYNC or other synchronous 50 1.1 haad * requirement. In the event of a panic or power fail then those log 51 1.1 haad * records (transactions) are replayed. 52 1.1 haad * 53 1.1 haad * There is one ZIL per file system. Its on-disk (pool) format consists 54 1.1 haad * of 3 parts: 55 1.1 haad * 56 1.1 haad * - ZIL header 57 1.1 haad * - ZIL blocks 58 1.1 haad * - ZIL records 59 1.1 haad * 60 1.1 haad * A log record holds a system call transaction. Log blocks can 61 1.1 haad * hold many log records and the blocks are chained together. 62 1.1 haad * Each ZIL block contains a block pointer (blkptr_t) to the next 63 1.1 haad * ZIL block in the chain. The ZIL header points to the first 64 1.1 haad * block in the chain. Note there is not a fixed place in the pool 65 1.1 haad * to hold blocks. They are dynamically allocated and freed as 66 1.1 haad * needed from the blocks available. Figure X shows the ZIL structure: 67 1.1 haad */ 68 1.1 haad 69 1.1 haad /* 70 1.1.1.3 chs * Disable intent logging replay. This global ZIL switch affects all pools. 71 1.1 haad */ 72 1.1.1.3 chs int zil_replay_disable = 0; 73 1.1.1.3 chs SYSCTL_DECL(_vfs_zfs); 74 1.1.1.3 chs SYSCTL_INT(_vfs_zfs, OID_AUTO, zil_replay_disable, CTLFLAG_RWTUN, 75 1.1.1.3 chs &zil_replay_disable, 0, "Disable intent logging replay"); 76 1.1 haad 77 1.1 haad /* 78 1.1 haad * Tunable parameter for debugging or performance analysis. Setting 79 1.1 haad * zfs_nocacheflush will cause corruption on power loss if a volatile 80 1.1 haad * out-of-order write cache is enabled. 81 1.1 haad */ 82 1.1 haad boolean_t zfs_nocacheflush = B_FALSE; 83 1.1.1.3 chs SYSCTL_INT(_vfs_zfs, OID_AUTO, cache_flush_disable, CTLFLAG_RDTUN, 84 1.1.1.3 chs &zfs_nocacheflush, 0, "Disable cache flush"); 85 1.1.1.3 chs boolean_t zfs_trim_enabled = B_TRUE; 86 1.1.1.3 chs SYSCTL_DECL(_vfs_zfs_trim); 87 1.1.1.3 chs SYSCTL_INT(_vfs_zfs_trim, OID_AUTO, enabled, CTLFLAG_RDTUN, &zfs_trim_enabled, 0, 88 1.1.1.3 chs "Enable ZFS TRIM"); 89 1.1 haad 90 1.1.1.3 chs /* 91 1.1.1.3 chs * Limit SLOG write size per commit executed with synchronous priority. 92 1.1.1.3 chs * Any writes above that executed with lower (asynchronous) priority to 93 1.1.1.3 chs * limit potential SLOG device abuse by single active ZIL writer. 94 1.1.1.3 chs */ 95 1.1.1.3 chs uint64_t zil_slog_limit = 768 * 1024; 96 1.1.1.3 chs SYSCTL_QUAD(_vfs_zfs, OID_AUTO, zil_slog_limit, CTLFLAG_RWTUN, 97 1.1.1.3 chs &zil_slog_limit, 0, "Maximal SLOG commit size with sync priority"); 98 1.1 haad 99 1.1.1.3 chs static kmem_cache_t *zil_lwb_cache; 100 1.1.1.2 haad 101 1.1.1.2 haad #define LWB_EMPTY(lwb) ((BP_GET_LSIZE(&lwb->lwb_blk) - \ 102 1.1.1.2 haad sizeof (zil_chain_t)) == (lwb->lwb_sz - lwb->lwb_nused)) 103 1.1.1.2 haad 104 1.1.1.2 haad 105 1.1.1.3 chs /* 106 1.1.1.3 chs * ziltest is by and large an ugly hack, but very useful in 107 1.1.1.3 chs * checking replay without tedious work. 108 1.1.1.3 chs * When running ziltest we want to keep all itx's and so maintain 109 1.1.1.3 chs * a single list in the zl_itxg[] that uses a high txg: ZILTEST_TXG 110 1.1.1.3 chs * We subtract TXG_CONCURRENT_STATES to allow for common code. 111 1.1.1.3 chs */ 112 1.1.1.3 chs #define ZILTEST_TXG (UINT64_MAX - TXG_CONCURRENT_STATES) 113 1.1.1.3 chs 114 1.1 haad static int 115 1.1.1.2 haad zil_bp_compare(const void *x1, const void *x2) 116 1.1 haad { 117 1.1.1.2 haad const dva_t *dva1 = &((zil_bp_node_t *)x1)->zn_dva; 118 1.1.1.2 haad const dva_t *dva2 = &((zil_bp_node_t *)x2)->zn_dva; 119 1.1 haad 120 1.1 haad if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2)) 121 1.1 haad return (-1); 122 1.1 haad if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2)) 123 1.1 haad return (1); 124 1.1 haad 125 1.1 haad if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2)) 126 1.1 haad return (-1); 127 1.1 haad if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2)) 128 1.1 haad return (1); 129 1.1 haad 130 1.1 haad return (0); 131 1.1 haad } 132 1.1 haad 133 1.1 haad static void 134 1.1.1.2 haad zil_bp_tree_init(zilog_t *zilog) 135 1.1 haad { 136 1.1.1.2 haad avl_create(&zilog->zl_bp_tree, zil_bp_compare, 137 1.1.1.2 haad sizeof (zil_bp_node_t), offsetof(zil_bp_node_t, zn_node)); 138 1.1 haad } 139 1.1 haad 140 1.1 haad static void 141 1.1.1.2 haad zil_bp_tree_fini(zilog_t *zilog) 142 1.1 haad { 143 1.1.1.2 haad avl_tree_t *t = &zilog->zl_bp_tree; 144 1.1.1.2 haad zil_bp_node_t *zn; 145 1.1 haad void *cookie = NULL; 146 1.1 haad 147 1.1 haad while ((zn = avl_destroy_nodes(t, &cookie)) != NULL) 148 1.1.1.2 haad kmem_free(zn, sizeof (zil_bp_node_t)); 149 1.1 haad 150 1.1 haad avl_destroy(t); 151 1.1 haad } 152 1.1 haad 153 1.1.1.2 haad int 154 1.1.1.2 haad zil_bp_tree_add(zilog_t *zilog, const blkptr_t *bp) 155 1.1 haad { 156 1.1.1.2 haad avl_tree_t *t = &zilog->zl_bp_tree; 157 1.1.1.3 chs const dva_t *dva; 158 1.1.1.2 haad zil_bp_node_t *zn; 159 1.1 haad avl_index_t where; 160 1.1 haad 161 1.1.1.3 chs if (BP_IS_EMBEDDED(bp)) 162 1.1.1.3 chs return (0); 163 1.1.1.3 chs 164 1.1.1.3 chs dva = BP_IDENTITY(bp); 165 1.1.1.3 chs 166 1.1 haad if (avl_find(t, dva, &where) != NULL) 167 1.1.1.3 chs return (SET_ERROR(EEXIST)); 168 1.1 haad 169 1.1.1.2 haad zn = kmem_alloc(sizeof (zil_bp_node_t), KM_SLEEP); 170 1.1 haad zn->zn_dva = *dva; 171 1.1 haad avl_insert(t, zn, where); 172 1.1 haad 173 1.1 haad return (0); 174 1.1 haad } 175 1.1 haad 176 1.1 haad static zil_header_t * 177 1.1 haad zil_header_in_syncing_context(zilog_t *zilog) 178 1.1 haad { 179 1.1 haad return ((zil_header_t *)zilog->zl_header); 180 1.1 haad } 181 1.1 haad 182 1.1 haad static void 183 1.1 haad zil_init_log_chain(zilog_t *zilog, blkptr_t *bp) 184 1.1 haad { 185 1.1 haad zio_cksum_t *zc = &bp->blk_cksum; 186 1.1 haad 187 1.1 haad zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL); 188 1.1 haad zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL); 189 1.1 haad zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os); 190 1.1 haad zc->zc_word[ZIL_ZC_SEQ] = 1ULL; 191 1.1 haad } 192 1.1 haad 193 1.1 haad /* 194 1.1.1.2 haad * Read a log block and make sure it's valid. 195 1.1 haad */ 196 1.1 haad static int 197 1.1.1.2 haad zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, blkptr_t *nbp, void *dst, 198 1.1.1.2 haad char **end) 199 1.1 haad { 200 1.1.1.2 haad enum zio_flag zio_flags = ZIO_FLAG_CANFAIL; 201 1.1.1.3 chs arc_flags_t aflags = ARC_FLAG_WAIT; 202 1.1.1.2 haad arc_buf_t *abuf = NULL; 203 1.1.1.3 chs zbookmark_phys_t zb; 204 1.1 haad int error; 205 1.1 haad 206 1.1.1.2 haad if (zilog->zl_header->zh_claim_txg == 0) 207 1.1.1.2 haad zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB; 208 1.1 haad 209 1.1.1.2 haad if (!(zilog->zl_header->zh_flags & ZIL_CLAIM_LR_SEQ_VALID)) 210 1.1.1.2 haad zio_flags |= ZIO_FLAG_SPECULATIVE; 211 1.1 haad 212 1.1.1.2 haad SET_BOOKMARK(&zb, bp->blk_cksum.zc_word[ZIL_ZC_OBJSET], 213 1.1.1.2 haad ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]); 214 1.1.1.2 haad 215 1.1.1.3 chs error = arc_read(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf, 216 1.1.1.2 haad ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb); 217 1.1 haad 218 1.1 haad if (error == 0) { 219 1.1 haad zio_cksum_t cksum = bp->blk_cksum; 220 1.1 haad 221 1.1 haad /* 222 1.1 haad * Validate the checksummed log block. 223 1.1 haad * 224 1.1 haad * Sequence numbers should be... sequential. The checksum 225 1.1 haad * verifier for the next block should be bp's checksum plus 1. 226 1.1 haad * 227 1.1 haad * Also check the log chain linkage and size used. 228 1.1 haad */ 229 1.1 haad cksum.zc_word[ZIL_ZC_SEQ]++; 230 1.1 haad 231 1.1.1.2 haad if (BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_ZILOG2) { 232 1.1.1.2 haad zil_chain_t *zilc = abuf->b_data; 233 1.1.1.2 haad char *lr = (char *)(zilc + 1); 234 1.1.1.2 haad uint64_t len = zilc->zc_nused - sizeof (zil_chain_t); 235 1.1.1.2 haad 236 1.1.1.2 haad if (bcmp(&cksum, &zilc->zc_next_blk.blk_cksum, 237 1.1.1.2 haad sizeof (cksum)) || BP_IS_HOLE(&zilc->zc_next_blk)) { 238 1.1.1.3 chs error = SET_ERROR(ECKSUM); 239 1.1.1.2 haad } else { 240 1.1.1.3 chs ASSERT3U(len, <=, SPA_OLD_MAXBLOCKSIZE); 241 1.1.1.2 haad bcopy(lr, dst, len); 242 1.1.1.2 haad *end = (char *)dst + len; 243 1.1.1.2 haad *nbp = zilc->zc_next_blk; 244 1.1.1.2 haad } 245 1.1.1.2 haad } else { 246 1.1.1.2 haad char *lr = abuf->b_data; 247 1.1.1.2 haad uint64_t size = BP_GET_LSIZE(bp); 248 1.1.1.2 haad zil_chain_t *zilc = (zil_chain_t *)(lr + size) - 1; 249 1.1.1.2 haad 250 1.1.1.2 haad if (bcmp(&cksum, &zilc->zc_next_blk.blk_cksum, 251 1.1.1.2 haad sizeof (cksum)) || BP_IS_HOLE(&zilc->zc_next_blk) || 252 1.1.1.2 haad (zilc->zc_nused > (size - sizeof (*zilc)))) { 253 1.1.1.3 chs error = SET_ERROR(ECKSUM); 254 1.1.1.2 haad } else { 255 1.1.1.3 chs ASSERT3U(zilc->zc_nused, <=, 256 1.1.1.3 chs SPA_OLD_MAXBLOCKSIZE); 257 1.1.1.2 haad bcopy(lr, dst, zilc->zc_nused); 258 1.1.1.2 haad *end = (char *)dst + zilc->zc_nused; 259 1.1.1.2 haad *nbp = zilc->zc_next_blk; 260 1.1.1.2 haad } 261 1.1 haad } 262 1.1 haad 263 1.1.1.3 chs arc_buf_destroy(abuf, &abuf); 264 1.1.1.2 haad } 265 1.1.1.2 haad 266 1.1.1.2 haad return (error); 267 1.1.1.2 haad } 268 1.1.1.2 haad 269 1.1.1.2 haad /* 270 1.1.1.2 haad * Read a TX_WRITE log data block. 271 1.1.1.2 haad */ 272 1.1.1.2 haad static int 273 1.1.1.2 haad zil_read_log_data(zilog_t *zilog, const lr_write_t *lr, void *wbuf) 274 1.1.1.2 haad { 275 1.1.1.2 haad enum zio_flag zio_flags = ZIO_FLAG_CANFAIL; 276 1.1.1.2 haad const blkptr_t *bp = &lr->lr_blkptr; 277 1.1.1.3 chs arc_flags_t aflags = ARC_FLAG_WAIT; 278 1.1.1.2 haad arc_buf_t *abuf = NULL; 279 1.1.1.3 chs zbookmark_phys_t zb; 280 1.1.1.2 haad int error; 281 1.1.1.2 haad 282 1.1.1.2 haad if (BP_IS_HOLE(bp)) { 283 1.1.1.2 haad if (wbuf != NULL) 284 1.1.1.2 haad bzero(wbuf, MAX(BP_GET_LSIZE(bp), lr->lr_length)); 285 1.1.1.2 haad return (0); 286 1.1 haad } 287 1.1 haad 288 1.1.1.2 haad if (zilog->zl_header->zh_claim_txg == 0) 289 1.1.1.2 haad zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB; 290 1.1.1.2 haad 291 1.1.1.2 haad SET_BOOKMARK(&zb, dmu_objset_id(zilog->zl_os), lr->lr_foid, 292 1.1.1.2 haad ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp)); 293 1.1.1.2 haad 294 1.1.1.3 chs error = arc_read(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf, 295 1.1.1.2 haad ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb); 296 1.1.1.2 haad 297 1.1.1.2 haad if (error == 0) { 298 1.1.1.2 haad if (wbuf != NULL) 299 1.1.1.2 haad bcopy(abuf->b_data, wbuf, arc_buf_size(abuf)); 300 1.1.1.3 chs arc_buf_destroy(abuf, &abuf); 301 1.1.1.2 haad } 302 1.1 haad 303 1.1 haad return (error); 304 1.1 haad } 305 1.1 haad 306 1.1 haad /* 307 1.1 haad * Parse the intent log, and call parse_func for each valid record within. 308 1.1 haad */ 309 1.1.1.2 haad int 310 1.1 haad zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func, 311 1.1 haad zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg) 312 1.1 haad { 313 1.1 haad const zil_header_t *zh = zilog->zl_header; 314 1.1.1.2 haad boolean_t claimed = !!zh->zh_claim_txg; 315 1.1.1.2 haad uint64_t claim_blk_seq = claimed ? zh->zh_claim_blk_seq : UINT64_MAX; 316 1.1.1.2 haad uint64_t claim_lr_seq = claimed ? zh->zh_claim_lr_seq : UINT64_MAX; 317 1.1.1.2 haad uint64_t max_blk_seq = 0; 318 1.1.1.2 haad uint64_t max_lr_seq = 0; 319 1.1.1.2 haad uint64_t blk_count = 0; 320 1.1.1.2 haad uint64_t lr_count = 0; 321 1.1.1.2 haad blkptr_t blk, next_blk; 322 1.1 haad char *lrbuf, *lrp; 323 1.1.1.2 haad int error = 0; 324 1.1 haad 325 1.1.1.2 haad /* 326 1.1.1.2 haad * Old logs didn't record the maximum zh_claim_lr_seq. 327 1.1.1.2 haad */ 328 1.1.1.2 haad if (!(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID)) 329 1.1.1.2 haad claim_lr_seq = UINT64_MAX; 330 1.1 haad 331 1.1 haad /* 332 1.1 haad * Starting at the block pointed to by zh_log we read the log chain. 333 1.1 haad * For each block in the chain we strongly check that block to 334 1.1 haad * ensure its validity. We stop when an invalid block is found. 335 1.1 haad * For each block pointer in the chain we call parse_blk_func(). 336 1.1 haad * For each record in each valid block we call parse_lr_func(). 337 1.1 haad * If the log has been claimed, stop if we encounter a sequence 338 1.1 haad * number greater than the highest claimed sequence number. 339 1.1 haad */ 340 1.1.1.3 chs lrbuf = zio_buf_alloc(SPA_OLD_MAXBLOCKSIZE); 341 1.1.1.2 haad zil_bp_tree_init(zilog); 342 1.1 haad 343 1.1.1.2 haad for (blk = zh->zh_log; !BP_IS_HOLE(&blk); blk = next_blk) { 344 1.1.1.2 haad uint64_t blk_seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 345 1.1.1.2 haad int reclen; 346 1.1.1.2 haad char *end; 347 1.1 haad 348 1.1.1.2 haad if (blk_seq > claim_blk_seq) 349 1.1.1.2 haad break; 350 1.1.1.2 haad if ((error = parse_blk_func(zilog, &blk, arg, txg)) != 0) 351 1.1.1.2 haad break; 352 1.1.1.2 haad ASSERT3U(max_blk_seq, <, blk_seq); 353 1.1.1.2 haad max_blk_seq = blk_seq; 354 1.1.1.2 haad blk_count++; 355 1.1 haad 356 1.1.1.2 haad if (max_lr_seq == claim_lr_seq && max_blk_seq == claim_blk_seq) 357 1.1.1.2 haad break; 358 1.1 haad 359 1.1.1.2 haad error = zil_read_log_block(zilog, &blk, &next_blk, lrbuf, &end); 360 1.1.1.3 chs if (error != 0) 361 1.1 haad break; 362 1.1 haad 363 1.1.1.2 haad for (lrp = lrbuf; lrp < end; lrp += reclen) { 364 1.1 haad lr_t *lr = (lr_t *)lrp; 365 1.1 haad reclen = lr->lrc_reclen; 366 1.1 haad ASSERT3U(reclen, >=, sizeof (lr_t)); 367 1.1.1.2 haad if (lr->lrc_seq > claim_lr_seq) 368 1.1.1.2 haad goto done; 369 1.1.1.2 haad if ((error = parse_lr_func(zilog, lr, arg, txg)) != 0) 370 1.1.1.2 haad goto done; 371 1.1.1.2 haad ASSERT3U(max_lr_seq, <, lr->lrc_seq); 372 1.1.1.2 haad max_lr_seq = lr->lrc_seq; 373 1.1.1.2 haad lr_count++; 374 1.1.1.2 haad } 375 1.1.1.2 haad } 376 1.1.1.2 haad done: 377 1.1.1.2 haad zilog->zl_parse_error = error; 378 1.1.1.2 haad zilog->zl_parse_blk_seq = max_blk_seq; 379 1.1.1.2 haad zilog->zl_parse_lr_seq = max_lr_seq; 380 1.1.1.2 haad zilog->zl_parse_blk_count = blk_count; 381 1.1.1.2 haad zilog->zl_parse_lr_count = lr_count; 382 1.1.1.2 haad 383 1.1.1.2 haad ASSERT(!claimed || !(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID) || 384 1.1.1.2 haad (max_blk_seq == claim_blk_seq && max_lr_seq == claim_lr_seq)); 385 1.1 haad 386 1.1.1.2 haad zil_bp_tree_fini(zilog); 387 1.1.1.3 chs zio_buf_free(lrbuf, SPA_OLD_MAXBLOCKSIZE); 388 1.1.1.2 haad 389 1.1.1.2 haad return (error); 390 1.1 haad } 391 1.1 haad 392 1.1.1.2 haad static int 393 1.1 haad zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg) 394 1.1 haad { 395 1.1 haad /* 396 1.1 haad * Claim log block if not already committed and not already claimed. 397 1.1.1.2 haad * If tx == NULL, just verify that the block is claimable. 398 1.1 haad */ 399 1.1.1.3 chs if (BP_IS_HOLE(bp) || bp->blk_birth < first_txg || 400 1.1.1.3 chs zil_bp_tree_add(zilog, bp) != 0) 401 1.1.1.2 haad return (0); 402 1.1.1.2 haad 403 1.1.1.2 haad return (zio_wait(zio_claim(NULL, zilog->zl_spa, 404 1.1.1.2 haad tx == NULL ? 0 : first_txg, bp, spa_claim_notify, NULL, 405 1.1.1.2 haad ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB))); 406 1.1 haad } 407 1.1 haad 408 1.1.1.2 haad static int 409 1.1 haad zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg) 410 1.1 haad { 411 1.1.1.2 haad lr_write_t *lr = (lr_write_t *)lrc; 412 1.1.1.2 haad int error; 413 1.1.1.2 haad 414 1.1.1.2 haad if (lrc->lrc_txtype != TX_WRITE) 415 1.1.1.2 haad return (0); 416 1.1.1.2 haad 417 1.1.1.2 haad /* 418 1.1.1.2 haad * If the block is not readable, don't claim it. This can happen 419 1.1.1.2 haad * in normal operation when a log block is written to disk before 420 1.1.1.2 haad * some of the dmu_sync() blocks it points to. In this case, the 421 1.1.1.2 haad * transaction cannot have been committed to anyone (we would have 422 1.1.1.2 haad * waited for all writes to be stable first), so it is semantically 423 1.1.1.2 haad * correct to declare this the end of the log. 424 1.1.1.2 haad */ 425 1.1.1.2 haad if (lr->lr_blkptr.blk_birth >= first_txg && 426 1.1.1.2 haad (error = zil_read_log_data(zilog, lr, NULL)) != 0) 427 1.1.1.2 haad return (error); 428 1.1.1.2 haad return (zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg)); 429 1.1 haad } 430 1.1 haad 431 1.1 haad /* ARGSUSED */ 432 1.1.1.2 haad static int 433 1.1 haad zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg) 434 1.1 haad { 435 1.1.1.2 haad zio_free_zil(zilog->zl_spa, dmu_tx_get_txg(tx), bp); 436 1.1.1.2 haad 437 1.1.1.2 haad return (0); 438 1.1 haad } 439 1.1 haad 440 1.1.1.2 haad static int 441 1.1 haad zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg) 442 1.1 haad { 443 1.1.1.2 haad lr_write_t *lr = (lr_write_t *)lrc; 444 1.1.1.2 haad blkptr_t *bp = &lr->lr_blkptr; 445 1.1.1.2 haad 446 1.1 haad /* 447 1.1 haad * If we previously claimed it, we need to free it. 448 1.1 haad */ 449 1.1.1.2 haad if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE && 450 1.1.1.3 chs bp->blk_birth >= claim_txg && zil_bp_tree_add(zilog, bp) == 0 && 451 1.1.1.3 chs !BP_IS_HOLE(bp)) 452 1.1.1.2 haad zio_free(zilog->zl_spa, dmu_tx_get_txg(tx), bp); 453 1.1.1.2 haad 454 1.1.1.2 haad return (0); 455 1.1.1.2 haad } 456 1.1.1.2 haad 457 1.1.1.2 haad static lwb_t * 458 1.1.1.3 chs zil_alloc_lwb(zilog_t *zilog, blkptr_t *bp, boolean_t slog, uint64_t txg) 459 1.1.1.2 haad { 460 1.1.1.2 haad lwb_t *lwb; 461 1.1.1.2 haad 462 1.1.1.2 haad lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 463 1.1.1.2 haad lwb->lwb_zilog = zilog; 464 1.1.1.2 haad lwb->lwb_blk = *bp; 465 1.1.1.3 chs lwb->lwb_slog = slog; 466 1.1.1.2 haad lwb->lwb_buf = zio_buf_alloc(BP_GET_LSIZE(bp)); 467 1.1.1.2 haad lwb->lwb_max_txg = txg; 468 1.1.1.2 haad lwb->lwb_zio = NULL; 469 1.1.1.2 haad lwb->lwb_tx = NULL; 470 1.1.1.2 haad if (BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_ZILOG2) { 471 1.1.1.2 haad lwb->lwb_nused = sizeof (zil_chain_t); 472 1.1.1.2 haad lwb->lwb_sz = BP_GET_LSIZE(bp); 473 1.1.1.2 haad } else { 474 1.1.1.2 haad lwb->lwb_nused = 0; 475 1.1.1.2 haad lwb->lwb_sz = BP_GET_LSIZE(bp) - sizeof (zil_chain_t); 476 1.1 haad } 477 1.1.1.2 haad 478 1.1.1.2 haad mutex_enter(&zilog->zl_lock); 479 1.1.1.2 haad list_insert_tail(&zilog->zl_lwb_list, lwb); 480 1.1.1.2 haad mutex_exit(&zilog->zl_lock); 481 1.1.1.2 haad 482 1.1.1.2 haad return (lwb); 483 1.1 haad } 484 1.1 haad 485 1.1 haad /* 486 1.1.1.3 chs * Called when we create in-memory log transactions so that we know 487 1.1.1.3 chs * to cleanup the itxs at the end of spa_sync(). 488 1.1.1.3 chs */ 489 1.1.1.3 chs void 490 1.1.1.3 chs zilog_dirty(zilog_t *zilog, uint64_t txg) 491 1.1.1.3 chs { 492 1.1.1.3 chs dsl_pool_t *dp = zilog->zl_dmu_pool; 493 1.1.1.3 chs dsl_dataset_t *ds = dmu_objset_ds(zilog->zl_os); 494 1.1.1.3 chs 495 1.1.1.3 chs if (ds->ds_is_snapshot) 496 1.1.1.3 chs panic("dirtying snapshot!"); 497 1.1.1.3 chs 498 1.1.1.3 chs if (txg_list_add(&dp->dp_dirty_zilogs, zilog, txg)) { 499 1.1.1.3 chs /* up the hold count until we can be written out */ 500 1.1.1.3 chs dmu_buf_add_ref(ds->ds_dbuf, zilog); 501 1.1.1.3 chs } 502 1.1.1.3 chs } 503 1.1.1.3 chs 504 1.1.1.3 chs /* 505 1.1.1.3 chs * Determine if the zil is dirty in the specified txg. Callers wanting to 506 1.1.1.3 chs * ensure that the dirty state does not change must hold the itxg_lock for 507 1.1.1.3 chs * the specified txg. Holding the lock will ensure that the zil cannot be 508 1.1.1.3 chs * dirtied (zil_itx_assign) or cleaned (zil_clean) while we check its current 509 1.1.1.3 chs * state. 510 1.1.1.3 chs */ 511 1.1.1.3 chs boolean_t 512 1.1.1.3 chs zilog_is_dirty_in_txg(zilog_t *zilog, uint64_t txg) 513 1.1.1.3 chs { 514 1.1.1.3 chs dsl_pool_t *dp = zilog->zl_dmu_pool; 515 1.1.1.3 chs 516 1.1.1.3 chs if (txg_list_member(&dp->dp_dirty_zilogs, zilog, txg & TXG_MASK)) 517 1.1.1.3 chs return (B_TRUE); 518 1.1.1.3 chs return (B_FALSE); 519 1.1.1.3 chs } 520 1.1.1.3 chs 521 1.1.1.3 chs /* 522 1.1.1.3 chs * Determine if the zil is dirty. The zil is considered dirty if it has 523 1.1.1.3 chs * any pending itx records that have not been cleaned by zil_clean(). 524 1.1.1.3 chs */ 525 1.1.1.3 chs boolean_t 526 1.1.1.3 chs zilog_is_dirty(zilog_t *zilog) 527 1.1.1.3 chs { 528 1.1.1.3 chs dsl_pool_t *dp = zilog->zl_dmu_pool; 529 1.1.1.3 chs 530 1.1.1.3 chs for (int t = 0; t < TXG_SIZE; t++) { 531 1.1.1.3 chs if (txg_list_member(&dp->dp_dirty_zilogs, zilog, t)) 532 1.1.1.3 chs return (B_TRUE); 533 1.1.1.3 chs } 534 1.1.1.3 chs return (B_FALSE); 535 1.1.1.3 chs } 536 1.1.1.3 chs 537 1.1.1.3 chs /* 538 1.1 haad * Create an on-disk intent log. 539 1.1 haad */ 540 1.1.1.2 haad static lwb_t * 541 1.1 haad zil_create(zilog_t *zilog) 542 1.1 haad { 543 1.1 haad const zil_header_t *zh = zilog->zl_header; 544 1.1.1.2 haad lwb_t *lwb = NULL; 545 1.1 haad uint64_t txg = 0; 546 1.1 haad dmu_tx_t *tx = NULL; 547 1.1 haad blkptr_t blk; 548 1.1 haad int error = 0; 549 1.1.1.3 chs boolean_t slog = FALSE; 550 1.1 haad 551 1.1 haad /* 552 1.1 haad * Wait for any previous destroy to complete. 553 1.1 haad */ 554 1.1 haad txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 555 1.1 haad 556 1.1 haad ASSERT(zh->zh_claim_txg == 0); 557 1.1 haad ASSERT(zh->zh_replay_seq == 0); 558 1.1 haad 559 1.1 haad blk = zh->zh_log; 560 1.1 haad 561 1.1 haad /* 562 1.1.1.2 haad * Allocate an initial log block if: 563 1.1.1.2 haad * - there isn't one already 564 1.1.1.2 haad * - the existing block is the wrong endianess 565 1.1 haad */ 566 1.1 haad if (BP_IS_HOLE(&blk) || BP_SHOULD_BYTESWAP(&blk)) { 567 1.1 haad tx = dmu_tx_create(zilog->zl_os); 568 1.1.1.2 haad VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0); 569 1.1 haad dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 570 1.1 haad txg = dmu_tx_get_txg(tx); 571 1.1 haad 572 1.1 haad if (!BP_IS_HOLE(&blk)) { 573 1.1.1.2 haad zio_free_zil(zilog->zl_spa, txg, &blk); 574 1.1 haad BP_ZERO(&blk); 575 1.1 haad } 576 1.1 haad 577 1.1.1.2 haad error = zio_alloc_zil(zilog->zl_spa, txg, &blk, NULL, 578 1.1.1.3 chs ZIL_MIN_BLKSZ, &slog); 579 1.1 haad 580 1.1 haad if (error == 0) 581 1.1 haad zil_init_log_chain(zilog, &blk); 582 1.1 haad } 583 1.1 haad 584 1.1 haad /* 585 1.1 haad * Allocate a log write buffer (lwb) for the first log block. 586 1.1 haad */ 587 1.1.1.2 haad if (error == 0) 588 1.1.1.3 chs lwb = zil_alloc_lwb(zilog, &blk, slog, txg); 589 1.1 haad 590 1.1 haad /* 591 1.1 haad * If we just allocated the first log block, commit our transaction 592 1.1 haad * and wait for zil_sync() to stuff the block poiner into zh_log. 593 1.1 haad * (zh is part of the MOS, so we cannot modify it in open context.) 594 1.1 haad */ 595 1.1 haad if (tx != NULL) { 596 1.1 haad dmu_tx_commit(tx); 597 1.1 haad txg_wait_synced(zilog->zl_dmu_pool, txg); 598 1.1 haad } 599 1.1 haad 600 1.1 haad ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0); 601 1.1.1.2 haad 602 1.1.1.2 haad return (lwb); 603 1.1 haad } 604 1.1 haad 605 1.1 haad /* 606 1.1 haad * In one tx, free all log blocks and clear the log header. 607 1.1 haad * If keep_first is set, then we're replaying a log with no content. 608 1.1 haad * We want to keep the first block, however, so that the first 609 1.1 haad * synchronous transaction doesn't require a txg_wait_synced() 610 1.1 haad * in zil_create(). We don't need to txg_wait_synced() here either 611 1.1 haad * when keep_first is set, because both zil_create() and zil_destroy() 612 1.1 haad * will wait for any in-progress destroys to complete. 613 1.1 haad */ 614 1.1 haad void 615 1.1 haad zil_destroy(zilog_t *zilog, boolean_t keep_first) 616 1.1 haad { 617 1.1 haad const zil_header_t *zh = zilog->zl_header; 618 1.1 haad lwb_t *lwb; 619 1.1 haad dmu_tx_t *tx; 620 1.1 haad uint64_t txg; 621 1.1 haad 622 1.1 haad /* 623 1.1 haad * Wait for any previous destroy to complete. 624 1.1 haad */ 625 1.1 haad txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 626 1.1 haad 627 1.1.1.2 haad zilog->zl_old_header = *zh; /* debugging aid */ 628 1.1.1.2 haad 629 1.1 haad if (BP_IS_HOLE(&zh->zh_log)) 630 1.1 haad return; 631 1.1 haad 632 1.1 haad tx = dmu_tx_create(zilog->zl_os); 633 1.1.1.2 haad VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0); 634 1.1 haad dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 635 1.1 haad txg = dmu_tx_get_txg(tx); 636 1.1 haad 637 1.1 haad mutex_enter(&zilog->zl_lock); 638 1.1 haad 639 1.1 haad ASSERT3U(zilog->zl_destroy_txg, <, txg); 640 1.1 haad zilog->zl_destroy_txg = txg; 641 1.1 haad zilog->zl_keep_first = keep_first; 642 1.1 haad 643 1.1 haad if (!list_is_empty(&zilog->zl_lwb_list)) { 644 1.1 haad ASSERT(zh->zh_claim_txg == 0); 645 1.1.1.3 chs VERIFY(!keep_first); 646 1.1 haad while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 647 1.1 haad list_remove(&zilog->zl_lwb_list, lwb); 648 1.1 haad if (lwb->lwb_buf != NULL) 649 1.1 haad zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 650 1.1.1.2 haad zio_free_zil(zilog->zl_spa, txg, &lwb->lwb_blk); 651 1.1 haad kmem_cache_free(zil_lwb_cache, lwb); 652 1.1 haad } 653 1.1.1.2 haad } else if (!keep_first) { 654 1.1.1.3 chs zil_destroy_sync(zilog, tx); 655 1.1 haad } 656 1.1 haad mutex_exit(&zilog->zl_lock); 657 1.1 haad 658 1.1 haad dmu_tx_commit(tx); 659 1.1 haad } 660 1.1 haad 661 1.1.1.3 chs void 662 1.1.1.3 chs zil_destroy_sync(zilog_t *zilog, dmu_tx_t *tx) 663 1.1.1.3 chs { 664 1.1.1.3 chs ASSERT(list_is_empty(&zilog->zl_lwb_list)); 665 1.1.1.3 chs (void) zil_parse(zilog, zil_free_log_block, 666 1.1.1.3 chs zil_free_log_record, tx, zilog->zl_header->zh_claim_txg); 667 1.1.1.3 chs } 668 1.1.1.3 chs 669 1.1 haad int 670 1.1.1.3 chs zil_claim(dsl_pool_t *dp, dsl_dataset_t *ds, void *txarg) 671 1.1 haad { 672 1.1 haad dmu_tx_t *tx = txarg; 673 1.1 haad uint64_t first_txg = dmu_tx_get_txg(tx); 674 1.1 haad zilog_t *zilog; 675 1.1 haad zil_header_t *zh; 676 1.1 haad objset_t *os; 677 1.1 haad int error; 678 1.1 haad 679 1.1.1.3 chs error = dmu_objset_own_obj(dp, ds->ds_object, 680 1.1.1.3 chs DMU_OST_ANY, B_FALSE, FTAG, &os); 681 1.1.1.3 chs if (error != 0) { 682 1.1.1.3 chs /* 683 1.1.1.3 chs * EBUSY indicates that the objset is inconsistent, in which 684 1.1.1.3 chs * case it can not have a ZIL. 685 1.1.1.3 chs */ 686 1.1.1.3 chs if (error != EBUSY) { 687 1.1.1.3 chs cmn_err(CE_WARN, "can't open objset for %llu, error %u", 688 1.1.1.3 chs (unsigned long long)ds->ds_object, error); 689 1.1.1.3 chs } 690 1.1 haad return (0); 691 1.1 haad } 692 1.1 haad 693 1.1 haad zilog = dmu_objset_zil(os); 694 1.1 haad zh = zil_header_in_syncing_context(zilog); 695 1.1 haad 696 1.1.1.2 haad if (spa_get_log_state(zilog->zl_spa) == SPA_LOG_CLEAR) { 697 1.1.1.2 haad if (!BP_IS_HOLE(&zh->zh_log)) 698 1.1.1.2 haad zio_free_zil(zilog->zl_spa, first_txg, &zh->zh_log); 699 1.1.1.2 haad BP_ZERO(&zh->zh_log); 700 1.1.1.2 haad dsl_dataset_dirty(dmu_objset_ds(os), tx); 701 1.1.1.3 chs dmu_objset_disown(os, FTAG); 702 1.1.1.2 haad return (0); 703 1.1.1.2 haad } 704 1.1.1.2 haad 705 1.1 haad /* 706 1.1 haad * Claim all log blocks if we haven't already done so, and remember 707 1.1 haad * the highest claimed sequence number. This ensures that if we can 708 1.1 haad * read only part of the log now (e.g. due to a missing device), 709 1.1 haad * but we can read the entire log later, we will not try to replay 710 1.1 haad * or destroy beyond the last block we successfully claimed. 711 1.1 haad */ 712 1.1 haad ASSERT3U(zh->zh_claim_txg, <=, first_txg); 713 1.1 haad if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) { 714 1.1.1.2 haad (void) zil_parse(zilog, zil_claim_log_block, 715 1.1 haad zil_claim_log_record, tx, first_txg); 716 1.1.1.2 haad zh->zh_claim_txg = first_txg; 717 1.1.1.2 haad zh->zh_claim_blk_seq = zilog->zl_parse_blk_seq; 718 1.1.1.2 haad zh->zh_claim_lr_seq = zilog->zl_parse_lr_seq; 719 1.1.1.2 haad if (zilog->zl_parse_lr_count || zilog->zl_parse_blk_count > 1) 720 1.1.1.2 haad zh->zh_flags |= ZIL_REPLAY_NEEDED; 721 1.1.1.2 haad zh->zh_flags |= ZIL_CLAIM_LR_SEQ_VALID; 722 1.1 haad dsl_dataset_dirty(dmu_objset_ds(os), tx); 723 1.1 haad } 724 1.1 haad 725 1.1 haad ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1)); 726 1.1.1.3 chs dmu_objset_disown(os, FTAG); 727 1.1 haad return (0); 728 1.1 haad } 729 1.1 haad 730 1.1 haad /* 731 1.1 haad * Check the log by walking the log chain. 732 1.1 haad * Checksum errors are ok as they indicate the end of the chain. 733 1.1 haad * Any other error (no device or read failure) returns an error. 734 1.1 haad */ 735 1.1.1.3 chs /* ARGSUSED */ 736 1.1 haad int 737 1.1.1.3 chs zil_check_log_chain(dsl_pool_t *dp, dsl_dataset_t *ds, void *tx) 738 1.1 haad { 739 1.1 haad zilog_t *zilog; 740 1.1 haad objset_t *os; 741 1.1.1.3 chs blkptr_t *bp; 742 1.1 haad int error; 743 1.1 haad 744 1.1.1.2 haad ASSERT(tx == NULL); 745 1.1.1.2 haad 746 1.1.1.3 chs error = dmu_objset_from_ds(ds, &os); 747 1.1.1.3 chs if (error != 0) { 748 1.1.1.3 chs cmn_err(CE_WARN, "can't open objset %llu, error %d", 749 1.1.1.3 chs (unsigned long long)ds->ds_object, error); 750 1.1 haad return (0); 751 1.1 haad } 752 1.1 haad 753 1.1 haad zilog = dmu_objset_zil(os); 754 1.1.1.3 chs bp = (blkptr_t *)&zilog->zl_header->zh_log; 755 1.1.1.3 chs 756 1.1.1.3 chs /* 757 1.1.1.3 chs * Check the first block and determine if it's on a log device 758 1.1.1.3 chs * which may have been removed or faulted prior to loading this 759 1.1.1.3 chs * pool. If so, there's no point in checking the rest of the log 760 1.1.1.3 chs * as its content should have already been synced to the pool. 761 1.1.1.3 chs */ 762 1.1.1.3 chs if (!BP_IS_HOLE(bp)) { 763 1.1.1.3 chs vdev_t *vd; 764 1.1.1.3 chs boolean_t valid = B_TRUE; 765 1.1.1.3 chs 766 1.1.1.3 chs spa_config_enter(os->os_spa, SCL_STATE, FTAG, RW_READER); 767 1.1.1.3 chs vd = vdev_lookup_top(os->os_spa, DVA_GET_VDEV(&bp->blk_dva[0])); 768 1.1.1.3 chs if (vd->vdev_islog && vdev_is_dead(vd)) 769 1.1.1.3 chs valid = vdev_log_state_valid(vd); 770 1.1.1.3 chs spa_config_exit(os->os_spa, SCL_STATE, FTAG); 771 1.1.1.3 chs 772 1.1.1.3 chs if (!valid) 773 1.1.1.3 chs return (0); 774 1.1.1.3 chs } 775 1.1 haad 776 1.1.1.2 haad /* 777 1.1.1.2 haad * Because tx == NULL, zil_claim_log_block() will not actually claim 778 1.1.1.2 haad * any blocks, but just determine whether it is possible to do so. 779 1.1.1.2 haad * In addition to checking the log chain, zil_claim_log_block() 780 1.1.1.2 haad * will invoke zio_claim() with a done func of spa_claim_notify(), 781 1.1.1.2 haad * which will update spa_max_claim_txg. See spa_load() for details. 782 1.1.1.2 haad */ 783 1.1.1.2 haad error = zil_parse(zilog, zil_claim_log_block, zil_claim_log_record, tx, 784 1.1.1.2 haad zilog->zl_header->zh_claim_txg ? -1ULL : spa_first_txg(os->os_spa)); 785 1.1 haad 786 1.1.1.2 haad return ((error == ECKSUM || error == ENOENT) ? 0 : error); 787 1.1 haad } 788 1.1 haad 789 1.1 haad static int 790 1.1 haad zil_vdev_compare(const void *x1, const void *x2) 791 1.1 haad { 792 1.1.1.3 chs const uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev; 793 1.1.1.3 chs const uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev; 794 1.1 haad 795 1.1 haad if (v1 < v2) 796 1.1 haad return (-1); 797 1.1 haad if (v1 > v2) 798 1.1 haad return (1); 799 1.1 haad 800 1.1 haad return (0); 801 1.1 haad } 802 1.1 haad 803 1.1 haad void 804 1.1.1.2 haad zil_add_block(zilog_t *zilog, const blkptr_t *bp) 805 1.1 haad { 806 1.1 haad avl_tree_t *t = &zilog->zl_vdev_tree; 807 1.1 haad avl_index_t where; 808 1.1 haad zil_vdev_node_t *zv, zvsearch; 809 1.1 haad int ndvas = BP_GET_NDVAS(bp); 810 1.1 haad int i; 811 1.1 haad 812 1.1 haad if (zfs_nocacheflush) 813 1.1 haad return; 814 1.1 haad 815 1.1 haad ASSERT(zilog->zl_writer); 816 1.1 haad 817 1.1 haad /* 818 1.1 haad * Even though we're zl_writer, we still need a lock because the 819 1.1 haad * zl_get_data() callbacks may have dmu_sync() done callbacks 820 1.1 haad * that will run concurrently. 821 1.1 haad */ 822 1.1 haad mutex_enter(&zilog->zl_vdev_lock); 823 1.1 haad for (i = 0; i < ndvas; i++) { 824 1.1 haad zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]); 825 1.1 haad if (avl_find(t, &zvsearch, &where) == NULL) { 826 1.1 haad zv = kmem_alloc(sizeof (*zv), KM_SLEEP); 827 1.1 haad zv->zv_vdev = zvsearch.zv_vdev; 828 1.1 haad avl_insert(t, zv, where); 829 1.1 haad } 830 1.1 haad } 831 1.1 haad mutex_exit(&zilog->zl_vdev_lock); 832 1.1 haad } 833 1.1 haad 834 1.1.1.3 chs static void 835 1.1 haad zil_flush_vdevs(zilog_t *zilog) 836 1.1 haad { 837 1.1 haad spa_t *spa = zilog->zl_spa; 838 1.1 haad avl_tree_t *t = &zilog->zl_vdev_tree; 839 1.1 haad void *cookie = NULL; 840 1.1 haad zil_vdev_node_t *zv; 841 1.1.1.3 chs zio_t *zio = NULL; 842 1.1 haad 843 1.1 haad ASSERT(zilog->zl_writer); 844 1.1 haad 845 1.1 haad /* 846 1.1 haad * We don't need zl_vdev_lock here because we're the zl_writer, 847 1.1 haad * and all zl_get_data() callbacks are done. 848 1.1 haad */ 849 1.1 haad if (avl_numnodes(t) == 0) 850 1.1 haad return; 851 1.1 haad 852 1.1 haad spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 853 1.1 haad 854 1.1 haad while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) { 855 1.1 haad vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev); 856 1.1.1.3 chs if (vd != NULL && !vd->vdev_nowritecache) { 857 1.1.1.3 chs if (zio == NULL) 858 1.1.1.3 chs zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); 859 1.1 haad zio_flush(zio, vd); 860 1.1.1.3 chs } 861 1.1 haad kmem_free(zv, sizeof (*zv)); 862 1.1 haad } 863 1.1 haad 864 1.1 haad /* 865 1.1 haad * Wait for all the flushes to complete. Not all devices actually 866 1.1 haad * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails. 867 1.1 haad */ 868 1.1.1.3 chs if (zio) 869 1.1.1.3 chs (void) zio_wait(zio); 870 1.1 haad 871 1.1 haad spa_config_exit(spa, SCL_STATE, FTAG); 872 1.1 haad } 873 1.1 haad 874 1.1 haad /* 875 1.1 haad * Function called when a log block write completes 876 1.1 haad */ 877 1.1 haad static void 878 1.1 haad zil_lwb_write_done(zio_t *zio) 879 1.1 haad { 880 1.1 haad lwb_t *lwb = zio->io_private; 881 1.1 haad zilog_t *zilog = lwb->lwb_zilog; 882 1.1.1.2 haad dmu_tx_t *tx = lwb->lwb_tx; 883 1.1 haad 884 1.1 haad ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF); 885 1.1 haad ASSERT(BP_GET_TYPE(zio->io_bp) == DMU_OT_INTENT_LOG); 886 1.1 haad ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 887 1.1 haad ASSERT(BP_GET_BYTEORDER(zio->io_bp) == ZFS_HOST_BYTEORDER); 888 1.1 haad ASSERT(!BP_IS_GANG(zio->io_bp)); 889 1.1 haad ASSERT(!BP_IS_HOLE(zio->io_bp)); 890 1.1.1.3 chs ASSERT(BP_GET_FILL(zio->io_bp) == 0); 891 1.1 haad 892 1.1 haad /* 893 1.1.1.2 haad * Ensure the lwb buffer pointer is cleared before releasing 894 1.1.1.2 haad * the txg. If we have had an allocation failure and 895 1.1.1.2 haad * the txg is waiting to sync then we want want zil_sync() 896 1.1.1.2 haad * to remove the lwb so that it's not picked up as the next new 897 1.1.1.2 haad * one in zil_commit_writer(). zil_sync() will only remove 898 1.1.1.2 haad * the lwb if lwb_buf is null. 899 1.1 haad */ 900 1.1 haad zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 901 1.1 haad mutex_enter(&zilog->zl_lock); 902 1.1 haad lwb->lwb_buf = NULL; 903 1.1.1.2 haad lwb->lwb_tx = NULL; 904 1.1 haad mutex_exit(&zilog->zl_lock); 905 1.1.1.2 haad 906 1.1.1.2 haad /* 907 1.1.1.2 haad * Now that we've written this log block, we have a stable pointer 908 1.1.1.2 haad * to the next block in the chain, so it's OK to let the txg in 909 1.1.1.2 haad * which we allocated the next block sync. 910 1.1.1.2 haad */ 911 1.1.1.2 haad dmu_tx_commit(tx); 912 1.1 haad } 913 1.1 haad 914 1.1 haad /* 915 1.1 haad * Initialize the io for a log block. 916 1.1 haad */ 917 1.1 haad static void 918 1.1 haad zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb) 919 1.1 haad { 920 1.1.1.3 chs zbookmark_phys_t zb; 921 1.1.1.3 chs zio_priority_t prio; 922 1.1 haad 923 1.1.1.2 haad SET_BOOKMARK(&zb, lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET], 924 1.1.1.2 haad ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, 925 1.1.1.2 haad lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ]); 926 1.1 haad 927 1.1 haad if (zilog->zl_root_zio == NULL) { 928 1.1 haad zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL, 929 1.1 haad ZIO_FLAG_CANFAIL); 930 1.1 haad } 931 1.1 haad if (lwb->lwb_zio == NULL) { 932 1.1.1.3 chs if (zilog->zl_cur_used <= zil_slog_limit || !lwb->lwb_slog) 933 1.1.1.3 chs prio = ZIO_PRIORITY_SYNC_WRITE; 934 1.1.1.3 chs else 935 1.1.1.3 chs prio = ZIO_PRIORITY_ASYNC_WRITE; 936 1.1 haad lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa, 937 1.1.1.2 haad 0, &lwb->lwb_blk, lwb->lwb_buf, BP_GET_LSIZE(&lwb->lwb_blk), 938 1.1.1.3 chs zil_lwb_write_done, lwb, prio, 939 1.1.1.2 haad ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE, &zb); 940 1.1 haad } 941 1.1 haad } 942 1.1 haad 943 1.1 haad /* 944 1.1.1.2 haad * Define a limited set of intent log block sizes. 945 1.1.1.3 chs * 946 1.1.1.2 haad * These must be a multiple of 4KB. Note only the amount used (again 947 1.1.1.2 haad * aligned to 4KB) actually gets written. However, we can't always just 948 1.1.1.3 chs * allocate SPA_OLD_MAXBLOCKSIZE as the slog space could be exhausted. 949 1.1.1.2 haad */ 950 1.1.1.2 haad uint64_t zil_block_buckets[] = { 951 1.1.1.2 haad 4096, /* non TX_WRITE */ 952 1.1.1.2 haad 8192+4096, /* data base */ 953 1.1.1.2 haad 32*1024 + 4096, /* NFS writes */ 954 1.1.1.2 haad UINT64_MAX 955 1.1.1.2 haad }; 956 1.1.1.2 haad 957 1.1.1.2 haad /* 958 1.1 haad * Start a log block write and advance to the next log block. 959 1.1 haad * Calls are serialized. 960 1.1 haad */ 961 1.1 haad static lwb_t * 962 1.1.1.3 chs zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb, boolean_t last) 963 1.1 haad { 964 1.1.1.2 haad lwb_t *nlwb = NULL; 965 1.1.1.2 haad zil_chain_t *zilc; 966 1.1 haad spa_t *spa = zilog->zl_spa; 967 1.1.1.2 haad blkptr_t *bp; 968 1.1.1.2 haad dmu_tx_t *tx; 969 1.1 haad uint64_t txg; 970 1.1.1.3 chs uint64_t zil_blksz, wsz; 971 1.1.1.2 haad int i, error; 972 1.1.1.3 chs boolean_t slog; 973 1.1 haad 974 1.1.1.2 haad if (BP_GET_CHECKSUM(&lwb->lwb_blk) == ZIO_CHECKSUM_ZILOG2) { 975 1.1.1.2 haad zilc = (zil_chain_t *)lwb->lwb_buf; 976 1.1.1.2 haad bp = &zilc->zc_next_blk; 977 1.1.1.2 haad } else { 978 1.1.1.2 haad zilc = (zil_chain_t *)(lwb->lwb_buf + lwb->lwb_sz); 979 1.1.1.2 haad bp = &zilc->zc_next_blk; 980 1.1.1.2 haad } 981 1.1.1.2 haad 982 1.1.1.2 haad ASSERT(lwb->lwb_nused <= lwb->lwb_sz); 983 1.1 haad 984 1.1 haad /* 985 1.1 haad * Allocate the next block and save its address in this block 986 1.1 haad * before writing it in order to establish the log chain. 987 1.1 haad * Note that if the allocation of nlwb synced before we wrote 988 1.1 haad * the block that points at it (lwb), we'd leak it if we crashed. 989 1.1.1.2 haad * Therefore, we don't do dmu_tx_commit() until zil_lwb_write_done(). 990 1.1.1.2 haad * We dirty the dataset to ensure that zil_sync() will be called 991 1.1.1.2 haad * to clean up in the event of allocation failure or I/O failure. 992 1.1 haad */ 993 1.1.1.2 haad tx = dmu_tx_create(zilog->zl_os); 994 1.1.1.2 haad VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0); 995 1.1.1.2 haad dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 996 1.1.1.2 haad txg = dmu_tx_get_txg(tx); 997 1.1.1.2 haad 998 1.1.1.2 haad lwb->lwb_tx = tx; 999 1.1 haad 1000 1.1 haad /* 1001 1.1.1.2 haad * Log blocks are pre-allocated. Here we select the size of the next 1002 1.1.1.2 haad * block, based on size used in the last block. 1003 1.1.1.2 haad * - first find the smallest bucket that will fit the block from a 1004 1.1.1.2 haad * limited set of block sizes. This is because it's faster to write 1005 1.1.1.2 haad * blocks allocated from the same metaslab as they are adjacent or 1006 1.1.1.2 haad * close. 1007 1.1.1.2 haad * - next find the maximum from the new suggested size and an array of 1008 1.1.1.2 haad * previous sizes. This lessens a picket fence effect of wrongly 1009 1.1.1.2 haad * guesssing the size if we have a stream of say 2k, 64k, 2k, 64k 1010 1.1.1.2 haad * requests. 1011 1.1.1.2 haad * 1012 1.1.1.2 haad * Note we only write what is used, but we can't just allocate 1013 1.1.1.2 haad * the maximum block size because we can exhaust the available 1014 1.1.1.2 haad * pool log space. 1015 1.1.1.2 haad */ 1016 1.1.1.2 haad zil_blksz = zilog->zl_cur_used + sizeof (zil_chain_t); 1017 1.1.1.2 haad for (i = 0; zil_blksz > zil_block_buckets[i]; i++) 1018 1.1.1.2 haad continue; 1019 1.1.1.2 haad zil_blksz = zil_block_buckets[i]; 1020 1.1.1.2 haad if (zil_blksz == UINT64_MAX) 1021 1.1.1.3 chs zil_blksz = SPA_OLD_MAXBLOCKSIZE; 1022 1.1.1.2 haad zilog->zl_prev_blks[zilog->zl_prev_rotor] = zil_blksz; 1023 1.1.1.2 haad for (i = 0; i < ZIL_PREV_BLKS; i++) 1024 1.1.1.2 haad zil_blksz = MAX(zil_blksz, zilog->zl_prev_blks[i]); 1025 1.1.1.2 haad zilog->zl_prev_rotor = (zilog->zl_prev_rotor + 1) & (ZIL_PREV_BLKS - 1); 1026 1.1 haad 1027 1.1 haad BP_ZERO(bp); 1028 1.1 haad /* pass the old blkptr in order to spread log blocks across devs */ 1029 1.1.1.3 chs error = zio_alloc_zil(spa, txg, bp, &lwb->lwb_blk, zil_blksz, &slog); 1030 1.1.1.3 chs if (error == 0) { 1031 1.1.1.2 haad ASSERT3U(bp->blk_birth, ==, txg); 1032 1.1.1.2 haad bp->blk_cksum = lwb->lwb_blk.blk_cksum; 1033 1.1.1.2 haad bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++; 1034 1.1 haad 1035 1.1 haad /* 1036 1.1.1.2 haad * Allocate a new log write buffer (lwb). 1037 1.1 haad */ 1038 1.1.1.3 chs nlwb = zil_alloc_lwb(zilog, bp, slog, txg); 1039 1.1 haad 1040 1.1.1.2 haad /* Record the block for later vdev flushing */ 1041 1.1.1.2 haad zil_add_block(zilog, &lwb->lwb_blk); 1042 1.1 haad } 1043 1.1 haad 1044 1.1.1.2 haad if (BP_GET_CHECKSUM(&lwb->lwb_blk) == ZIO_CHECKSUM_ZILOG2) { 1045 1.1.1.2 haad /* For Slim ZIL only write what is used. */ 1046 1.1.1.3 chs wsz = P2ROUNDUP_TYPED(lwb->lwb_nused, ZIL_MIN_BLKSZ, uint64_t); 1047 1.1.1.3 chs ASSERT3U(wsz, <=, lwb->lwb_sz); 1048 1.1.1.3 chs zio_shrink(lwb->lwb_zio, wsz); 1049 1.1 haad 1050 1.1.1.3 chs } else { 1051 1.1.1.3 chs wsz = lwb->lwb_sz; 1052 1.1.1.2 haad } 1053 1.1.1.3 chs 1054 1.1.1.2 haad zilc->zc_pad = 0; 1055 1.1.1.2 haad zilc->zc_nused = lwb->lwb_nused; 1056 1.1.1.2 haad zilc->zc_eck.zec_cksum = lwb->lwb_blk.blk_cksum; 1057 1.1 haad 1058 1.1.1.3 chs /* 1059 1.1.1.3 chs * clear unused data for security 1060 1.1.1.3 chs */ 1061 1.1.1.3 chs bzero(lwb->lwb_buf + lwb->lwb_nused, wsz - lwb->lwb_nused); 1062 1.1.1.3 chs 1063 1.1.1.3 chs if (last) 1064 1.1.1.3 chs lwb->lwb_zio->io_pipeline &= ~ZIO_STAGE_ISSUE_ASYNC; 1065 1.1.1.2 haad zio_nowait(lwb->lwb_zio); /* Kick off the write for the old log block */ 1066 1.1 haad 1067 1.1 haad /* 1068 1.1.1.2 haad * If there was an allocation failure then nlwb will be null which 1069 1.1.1.2 haad * forces a txg_wait_synced(). 1070 1.1 haad */ 1071 1.1 haad return (nlwb); 1072 1.1 haad } 1073 1.1 haad 1074 1.1 haad static lwb_t * 1075 1.1 haad zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb) 1076 1.1 haad { 1077 1.1.1.3 chs lr_t *lrcb, *lrc = &itx->itx_lr; /* common log record */ 1078 1.1.1.3 chs lr_write_t *lrwb, *lrw = (lr_write_t *)lrc; 1079 1.1.1.2 haad char *lr_buf; 1080 1.1 haad uint64_t txg = lrc->lrc_txg; 1081 1.1 haad uint64_t reclen = lrc->lrc_reclen; 1082 1.1.1.2 haad uint64_t dlen = 0; 1083 1.1.1.3 chs uint64_t dnow, lwb_sp; 1084 1.1 haad 1085 1.1 haad if (lwb == NULL) 1086 1.1 haad return (NULL); 1087 1.1.1.2 haad 1088 1.1 haad ASSERT(lwb->lwb_buf != NULL); 1089 1.1 haad 1090 1.1 haad if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY) 1091 1.1 haad dlen = P2ROUNDUP_TYPED( 1092 1.1.1.2 haad lrw->lr_length, sizeof (uint64_t), uint64_t); 1093 1.1 haad 1094 1.1 haad zilog->zl_cur_used += (reclen + dlen); 1095 1.1 haad 1096 1.1 haad zil_lwb_write_init(zilog, lwb); 1097 1.1 haad 1098 1.1.1.3 chs cont: 1099 1.1 haad /* 1100 1.1 haad * If this record won't fit in the current log block, start a new one. 1101 1.1.1.3 chs * For WR_NEED_COPY optimize layout for minimal number of chunks, but 1102 1.1.1.3 chs * try to keep wasted space withing reasonable range (12%). 1103 1.1 haad */ 1104 1.1.1.3 chs lwb_sp = lwb->lwb_sz - lwb->lwb_nused; 1105 1.1.1.3 chs if (reclen > lwb_sp || (reclen + dlen > lwb_sp && 1106 1.1.1.3 chs lwb_sp < ZIL_MAX_LOG_DATA / 8 && (dlen % ZIL_MAX_LOG_DATA == 0 || 1107 1.1.1.3 chs lwb_sp < reclen + dlen % ZIL_MAX_LOG_DATA))) { 1108 1.1.1.3 chs lwb = zil_lwb_write_start(zilog, lwb, B_FALSE); 1109 1.1 haad if (lwb == NULL) 1110 1.1 haad return (NULL); 1111 1.1 haad zil_lwb_write_init(zilog, lwb); 1112 1.1.1.2 haad ASSERT(LWB_EMPTY(lwb)); 1113 1.1.1.3 chs lwb_sp = lwb->lwb_sz - lwb->lwb_nused; 1114 1.1.1.3 chs ASSERT3U(reclen + MIN(dlen, sizeof(uint64_t)), <=, lwb_sp); 1115 1.1 haad } 1116 1.1 haad 1117 1.1.1.3 chs dnow = MIN(dlen, lwb_sp - reclen); 1118 1.1.1.2 haad lr_buf = lwb->lwb_buf + lwb->lwb_nused; 1119 1.1.1.2 haad bcopy(lrc, lr_buf, reclen); 1120 1.1.1.3 chs lrcb = (lr_t *)lr_buf; 1121 1.1.1.3 chs lrwb = (lr_write_t *)lrcb; 1122 1.1 haad 1123 1.1 haad /* 1124 1.1 haad * If it's a write, fetch the data or get its blkptr as appropriate. 1125 1.1 haad */ 1126 1.1 haad if (lrc->lrc_txtype == TX_WRITE) { 1127 1.1 haad if (txg > spa_freeze_txg(zilog->zl_spa)) 1128 1.1 haad txg_wait_synced(zilog->zl_dmu_pool, txg); 1129 1.1 haad if (itx->itx_wr_state != WR_COPIED) { 1130 1.1 haad char *dbuf; 1131 1.1 haad int error; 1132 1.1 haad 1133 1.1.1.3 chs if (itx->itx_wr_state == WR_NEED_COPY) { 1134 1.1.1.2 haad dbuf = lr_buf + reclen; 1135 1.1.1.3 chs lrcb->lrc_reclen += dnow; 1136 1.1.1.3 chs if (lrwb->lr_length > dnow) 1137 1.1.1.3 chs lrwb->lr_length = dnow; 1138 1.1.1.3 chs lrw->lr_offset += dnow; 1139 1.1.1.3 chs lrw->lr_length -= dnow; 1140 1.1 haad } else { 1141 1.1 haad ASSERT(itx->itx_wr_state == WR_INDIRECT); 1142 1.1 haad dbuf = NULL; 1143 1.1 haad } 1144 1.1 haad error = zilog->zl_get_data( 1145 1.1.1.3 chs itx->itx_private, lrwb, dbuf, lwb->lwb_zio); 1146 1.1.1.2 haad if (error == EIO) { 1147 1.1.1.2 haad txg_wait_synced(zilog->zl_dmu_pool, txg); 1148 1.1.1.2 haad return (lwb); 1149 1.1.1.2 haad } 1150 1.1.1.3 chs if (error != 0) { 1151 1.1 haad ASSERT(error == ENOENT || error == EEXIST || 1152 1.1 haad error == EALREADY); 1153 1.1 haad return (lwb); 1154 1.1 haad } 1155 1.1 haad } 1156 1.1 haad } 1157 1.1 haad 1158 1.1.1.2 haad /* 1159 1.1.1.2 haad * We're actually making an entry, so update lrc_seq to be the 1160 1.1.1.2 haad * log record sequence number. Note that this is generally not 1161 1.1.1.2 haad * equal to the itx sequence number because not all transactions 1162 1.1.1.2 haad * are synchronous, and sometimes spa_sync() gets there first. 1163 1.1.1.2 haad */ 1164 1.1.1.3 chs lrcb->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */ 1165 1.1.1.3 chs lwb->lwb_nused += reclen + dnow; 1166 1.1 haad lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg); 1167 1.1.1.2 haad ASSERT3U(lwb->lwb_nused, <=, lwb->lwb_sz); 1168 1.1.1.3 chs ASSERT0(P2PHASE(lwb->lwb_nused, sizeof (uint64_t))); 1169 1.1.1.3 chs 1170 1.1.1.3 chs dlen -= dnow; 1171 1.1.1.3 chs if (dlen > 0) { 1172 1.1.1.3 chs zilog->zl_cur_used += reclen; 1173 1.1.1.3 chs goto cont; 1174 1.1.1.3 chs } 1175 1.1 haad 1176 1.1 haad return (lwb); 1177 1.1 haad } 1178 1.1 haad 1179 1.1 haad itx_t * 1180 1.1 haad zil_itx_create(uint64_t txtype, size_t lrsize) 1181 1.1 haad { 1182 1.1 haad itx_t *itx; 1183 1.1 haad 1184 1.1 haad lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t); 1185 1.1 haad 1186 1.1 haad itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP); 1187 1.1 haad itx->itx_lr.lrc_txtype = txtype; 1188 1.1 haad itx->itx_lr.lrc_reclen = lrsize; 1189 1.1 haad itx->itx_lr.lrc_seq = 0; /* defensive */ 1190 1.1.1.3 chs itx->itx_sync = B_TRUE; /* default is synchronous */ 1191 1.1 haad 1192 1.1 haad return (itx); 1193 1.1 haad } 1194 1.1 haad 1195 1.1.1.2 haad void 1196 1.1.1.2 haad zil_itx_destroy(itx_t *itx) 1197 1.1.1.2 haad { 1198 1.1.1.2 haad kmem_free(itx, offsetof(itx_t, itx_lr) + itx->itx_lr.lrc_reclen); 1199 1.1.1.2 haad } 1200 1.1.1.2 haad 1201 1.1.1.3 chs /* 1202 1.1.1.3 chs * Free up the sync and async itxs. The itxs_t has already been detached 1203 1.1.1.3 chs * so no locks are needed. 1204 1.1.1.3 chs */ 1205 1.1.1.3 chs static void 1206 1.1.1.3 chs zil_itxg_clean(itxs_t *itxs) 1207 1.1 haad { 1208 1.1.1.3 chs itx_t *itx; 1209 1.1.1.3 chs list_t *list; 1210 1.1.1.3 chs avl_tree_t *t; 1211 1.1.1.3 chs void *cookie; 1212 1.1.1.3 chs itx_async_node_t *ian; 1213 1.1.1.3 chs 1214 1.1.1.3 chs list = &itxs->i_sync_list; 1215 1.1.1.3 chs while ((itx = list_head(list)) != NULL) { 1216 1.1.1.3 chs list_remove(list, itx); 1217 1.1.1.3 chs kmem_free(itx, offsetof(itx_t, itx_lr) + 1218 1.1.1.3 chs itx->itx_lr.lrc_reclen); 1219 1.1.1.3 chs } 1220 1.1.1.3 chs 1221 1.1.1.3 chs cookie = NULL; 1222 1.1.1.3 chs t = &itxs->i_async_tree; 1223 1.1.1.3 chs while ((ian = avl_destroy_nodes(t, &cookie)) != NULL) { 1224 1.1.1.3 chs list = &ian->ia_list; 1225 1.1.1.3 chs while ((itx = list_head(list)) != NULL) { 1226 1.1.1.3 chs list_remove(list, itx); 1227 1.1.1.3 chs kmem_free(itx, offsetof(itx_t, itx_lr) + 1228 1.1.1.3 chs itx->itx_lr.lrc_reclen); 1229 1.1.1.3 chs } 1230 1.1.1.3 chs list_destroy(list); 1231 1.1.1.3 chs kmem_free(ian, sizeof (itx_async_node_t)); 1232 1.1.1.3 chs } 1233 1.1.1.3 chs avl_destroy(t); 1234 1.1 haad 1235 1.1.1.3 chs kmem_free(itxs, sizeof (itxs_t)); 1236 1.1.1.3 chs } 1237 1.1 haad 1238 1.1.1.3 chs static int 1239 1.1.1.3 chs zil_aitx_compare(const void *x1, const void *x2) 1240 1.1.1.3 chs { 1241 1.1.1.3 chs const uint64_t o1 = ((itx_async_node_t *)x1)->ia_foid; 1242 1.1.1.3 chs const uint64_t o2 = ((itx_async_node_t *)x2)->ia_foid; 1243 1.1 haad 1244 1.1.1.3 chs if (o1 < o2) 1245 1.1.1.3 chs return (-1); 1246 1.1.1.3 chs if (o1 > o2) 1247 1.1.1.3 chs return (1); 1248 1.1.1.3 chs 1249 1.1.1.3 chs return (0); 1250 1.1 haad } 1251 1.1 haad 1252 1.1 haad /* 1253 1.1.1.3 chs * Remove all async itx with the given oid. 1254 1.1 haad */ 1255 1.1 haad static void 1256 1.1.1.3 chs zil_remove_async(zilog_t *zilog, uint64_t oid) 1257 1.1 haad { 1258 1.1.1.3 chs uint64_t otxg, txg; 1259 1.1.1.3 chs itx_async_node_t *ian; 1260 1.1.1.3 chs avl_tree_t *t; 1261 1.1.1.3 chs avl_index_t where; 1262 1.1 haad list_t clean_list; 1263 1.1 haad itx_t *itx; 1264 1.1 haad 1265 1.1.1.3 chs ASSERT(oid != 0); 1266 1.1 haad list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node)); 1267 1.1 haad 1268 1.1.1.3 chs if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */ 1269 1.1.1.3 chs otxg = ZILTEST_TXG; 1270 1.1.1.3 chs else 1271 1.1.1.3 chs otxg = spa_last_synced_txg(zilog->zl_spa) + 1; 1272 1.1 haad 1273 1.1.1.3 chs for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) { 1274 1.1.1.3 chs itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK]; 1275 1.1.1.3 chs 1276 1.1.1.3 chs mutex_enter(&itxg->itxg_lock); 1277 1.1.1.3 chs if (itxg->itxg_txg != txg) { 1278 1.1.1.3 chs mutex_exit(&itxg->itxg_lock); 1279 1.1.1.3 chs continue; 1280 1.1.1.3 chs } 1281 1.1 haad 1282 1.1.1.3 chs /* 1283 1.1.1.3 chs * Locate the object node and append its list. 1284 1.1.1.3 chs */ 1285 1.1.1.3 chs t = &itxg->itxg_itxs->i_async_tree; 1286 1.1.1.3 chs ian = avl_find(t, &oid, &where); 1287 1.1.1.3 chs if (ian != NULL) 1288 1.1.1.3 chs list_move_tail(&clean_list, &ian->ia_list); 1289 1.1.1.3 chs mutex_exit(&itxg->itxg_lock); 1290 1.1.1.3 chs } 1291 1.1 haad while ((itx = list_head(&clean_list)) != NULL) { 1292 1.1 haad list_remove(&clean_list, itx); 1293 1.1.1.3 chs kmem_free(itx, offsetof(itx_t, itx_lr) + 1294 1.1.1.3 chs itx->itx_lr.lrc_reclen); 1295 1.1 haad } 1296 1.1 haad list_destroy(&clean_list); 1297 1.1 haad } 1298 1.1 haad 1299 1.1.1.3 chs void 1300 1.1.1.3 chs zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx) 1301 1.1.1.3 chs { 1302 1.1.1.3 chs uint64_t txg; 1303 1.1.1.3 chs itxg_t *itxg; 1304 1.1.1.3 chs itxs_t *itxs, *clean = NULL; 1305 1.1.1.3 chs 1306 1.1.1.3 chs /* 1307 1.1.1.3 chs * Object ids can be re-instantiated in the next txg so 1308 1.1.1.3 chs * remove any async transactions to avoid future leaks. 1309 1.1.1.3 chs * This can happen if a fsync occurs on the re-instantiated 1310 1.1.1.3 chs * object for a WR_INDIRECT or WR_NEED_COPY write, which gets 1311 1.1.1.3 chs * the new file data and flushes a write record for the old object. 1312 1.1.1.3 chs */ 1313 1.1.1.3 chs if ((itx->itx_lr.lrc_txtype & ~TX_CI) == TX_REMOVE) 1314 1.1.1.3 chs zil_remove_async(zilog, itx->itx_oid); 1315 1.1.1.3 chs 1316 1.1.1.3 chs /* 1317 1.1.1.3 chs * Ensure the data of a renamed file is committed before the rename. 1318 1.1.1.3 chs */ 1319 1.1.1.3 chs if ((itx->itx_lr.lrc_txtype & ~TX_CI) == TX_RENAME) 1320 1.1.1.3 chs zil_async_to_sync(zilog, itx->itx_oid); 1321 1.1.1.3 chs 1322 1.1.1.3 chs if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) 1323 1.1.1.3 chs txg = ZILTEST_TXG; 1324 1.1.1.3 chs else 1325 1.1.1.3 chs txg = dmu_tx_get_txg(tx); 1326 1.1.1.3 chs 1327 1.1.1.3 chs itxg = &zilog->zl_itxg[txg & TXG_MASK]; 1328 1.1.1.3 chs mutex_enter(&itxg->itxg_lock); 1329 1.1.1.3 chs itxs = itxg->itxg_itxs; 1330 1.1.1.3 chs if (itxg->itxg_txg != txg) { 1331 1.1.1.3 chs if (itxs != NULL) { 1332 1.1.1.3 chs /* 1333 1.1.1.3 chs * The zil_clean callback hasn't got around to cleaning 1334 1.1.1.3 chs * this itxg. Save the itxs for release below. 1335 1.1.1.3 chs * This should be rare. 1336 1.1.1.3 chs */ 1337 1.1.1.3 chs clean = itxg->itxg_itxs; 1338 1.1.1.3 chs } 1339 1.1.1.3 chs itxg->itxg_txg = txg; 1340 1.1.1.3 chs itxs = itxg->itxg_itxs = kmem_zalloc(sizeof (itxs_t), KM_SLEEP); 1341 1.1.1.3 chs 1342 1.1.1.3 chs list_create(&itxs->i_sync_list, sizeof (itx_t), 1343 1.1.1.3 chs offsetof(itx_t, itx_node)); 1344 1.1.1.3 chs avl_create(&itxs->i_async_tree, zil_aitx_compare, 1345 1.1.1.3 chs sizeof (itx_async_node_t), 1346 1.1.1.3 chs offsetof(itx_async_node_t, ia_node)); 1347 1.1.1.3 chs } 1348 1.1.1.3 chs if (itx->itx_sync) { 1349 1.1.1.3 chs list_insert_tail(&itxs->i_sync_list, itx); 1350 1.1.1.3 chs } else { 1351 1.1.1.3 chs avl_tree_t *t = &itxs->i_async_tree; 1352 1.1.1.3 chs uint64_t foid = ((lr_ooo_t *)&itx->itx_lr)->lr_foid; 1353 1.1.1.3 chs itx_async_node_t *ian; 1354 1.1.1.3 chs avl_index_t where; 1355 1.1.1.3 chs 1356 1.1.1.3 chs ian = avl_find(t, &foid, &where); 1357 1.1.1.3 chs if (ian == NULL) { 1358 1.1.1.3 chs ian = kmem_alloc(sizeof (itx_async_node_t), KM_SLEEP); 1359 1.1.1.3 chs list_create(&ian->ia_list, sizeof (itx_t), 1360 1.1.1.3 chs offsetof(itx_t, itx_node)); 1361 1.1.1.3 chs ian->ia_foid = foid; 1362 1.1.1.3 chs avl_insert(t, ian, where); 1363 1.1.1.3 chs } 1364 1.1.1.3 chs list_insert_tail(&ian->ia_list, itx); 1365 1.1.1.3 chs } 1366 1.1.1.3 chs 1367 1.1.1.3 chs itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx); 1368 1.1.1.3 chs zilog_dirty(zilog, txg); 1369 1.1.1.3 chs mutex_exit(&itxg->itxg_lock); 1370 1.1.1.3 chs 1371 1.1.1.3 chs /* Release the old itxs now we've dropped the lock */ 1372 1.1.1.3 chs if (clean != NULL) 1373 1.1.1.3 chs zil_itxg_clean(clean); 1374 1.1.1.3 chs } 1375 1.1.1.3 chs 1376 1.1 haad /* 1377 1.1 haad * If there are any in-memory intent log transactions which have now been 1378 1.1.1.3 chs * synced then start up a taskq to free them. We should only do this after we 1379 1.1.1.3 chs * have written out the uberblocks (i.e. txg has been comitted) so that 1380 1.1.1.3 chs * don't inadvertently clean out in-memory log records that would be required 1381 1.1.1.3 chs * by zil_commit(). 1382 1.1 haad */ 1383 1.1 haad void 1384 1.1.1.3 chs zil_clean(zilog_t *zilog, uint64_t synced_txg) 1385 1.1 haad { 1386 1.1.1.3 chs itxg_t *itxg = &zilog->zl_itxg[synced_txg & TXG_MASK]; 1387 1.1.1.3 chs itxs_t *clean_me; 1388 1.1 haad 1389 1.1.1.3 chs mutex_enter(&itxg->itxg_lock); 1390 1.1.1.3 chs if (itxg->itxg_itxs == NULL || itxg->itxg_txg == ZILTEST_TXG) { 1391 1.1.1.3 chs mutex_exit(&itxg->itxg_lock); 1392 1.1.1.3 chs return; 1393 1.1.1.3 chs } 1394 1.1.1.3 chs ASSERT3U(itxg->itxg_txg, <=, synced_txg); 1395 1.1.1.3 chs ASSERT(itxg->itxg_txg != 0); 1396 1.1.1.3 chs ASSERT(zilog->zl_clean_taskq != NULL); 1397 1.1.1.3 chs clean_me = itxg->itxg_itxs; 1398 1.1.1.3 chs itxg->itxg_itxs = NULL; 1399 1.1.1.3 chs itxg->itxg_txg = 0; 1400 1.1.1.3 chs mutex_exit(&itxg->itxg_lock); 1401 1.1.1.3 chs /* 1402 1.1.1.3 chs * Preferably start a task queue to free up the old itxs but 1403 1.1.1.3 chs * if taskq_dispatch can't allocate resources to do that then 1404 1.1.1.3 chs * free it in-line. This should be rare. Note, using TQ_SLEEP 1405 1.1.1.3 chs * created a bad performance problem. 1406 1.1.1.3 chs */ 1407 1.1.1.3 chs if (taskq_dispatch(zilog->zl_clean_taskq, 1408 1.1.1.3 chs (void (*)(void *))zil_itxg_clean, clean_me, TQ_NOSLEEP) == 0) 1409 1.1.1.3 chs zil_itxg_clean(clean_me); 1410 1.1.1.3 chs } 1411 1.1.1.3 chs 1412 1.1.1.3 chs /* 1413 1.1.1.3 chs * Get the list of itxs to commit into zl_itx_commit_list. 1414 1.1.1.3 chs */ 1415 1.1.1.3 chs static void 1416 1.1.1.3 chs zil_get_commit_list(zilog_t *zilog) 1417 1.1.1.3 chs { 1418 1.1.1.3 chs uint64_t otxg, txg; 1419 1.1.1.3 chs list_t *commit_list = &zilog->zl_itx_commit_list; 1420 1.1.1.3 chs 1421 1.1.1.3 chs if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */ 1422 1.1.1.3 chs otxg = ZILTEST_TXG; 1423 1.1.1.3 chs else 1424 1.1.1.3 chs otxg = spa_last_synced_txg(zilog->zl_spa) + 1; 1425 1.1.1.3 chs 1426 1.1.1.3 chs /* 1427 1.1.1.3 chs * This is inherently racy, since there is nothing to prevent 1428 1.1.1.3 chs * the last synced txg from changing. That's okay since we'll 1429 1.1.1.3 chs * only commit things in the future. 1430 1.1.1.3 chs */ 1431 1.1.1.3 chs for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) { 1432 1.1.1.3 chs itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK]; 1433 1.1.1.3 chs 1434 1.1.1.3 chs mutex_enter(&itxg->itxg_lock); 1435 1.1.1.3 chs if (itxg->itxg_txg != txg) { 1436 1.1.1.3 chs mutex_exit(&itxg->itxg_lock); 1437 1.1.1.3 chs continue; 1438 1.1.1.3 chs } 1439 1.1.1.3 chs 1440 1.1.1.3 chs /* 1441 1.1.1.3 chs * If we're adding itx records to the zl_itx_commit_list, 1442 1.1.1.3 chs * then the zil better be dirty in this "txg". We can assert 1443 1.1.1.3 chs * that here since we're holding the itxg_lock which will 1444 1.1.1.3 chs * prevent spa_sync from cleaning it. Once we add the itxs 1445 1.1.1.3 chs * to the zl_itx_commit_list we must commit it to disk even 1446 1.1.1.3 chs * if it's unnecessary (i.e. the txg was synced). 1447 1.1.1.3 chs */ 1448 1.1.1.3 chs ASSERT(zilog_is_dirty_in_txg(zilog, txg) || 1449 1.1.1.3 chs spa_freeze_txg(zilog->zl_spa) != UINT64_MAX); 1450 1.1.1.3 chs list_move_tail(commit_list, &itxg->itxg_itxs->i_sync_list); 1451 1.1.1.3 chs 1452 1.1.1.3 chs mutex_exit(&itxg->itxg_lock); 1453 1.1.1.3 chs } 1454 1.1.1.3 chs } 1455 1.1.1.3 chs 1456 1.1.1.3 chs /* 1457 1.1.1.3 chs * Move the async itxs for a specified object to commit into sync lists. 1458 1.1.1.3 chs */ 1459 1.1.1.3 chs void 1460 1.1.1.3 chs zil_async_to_sync(zilog_t *zilog, uint64_t foid) 1461 1.1.1.3 chs { 1462 1.1.1.3 chs uint64_t otxg, txg; 1463 1.1.1.3 chs itx_async_node_t *ian; 1464 1.1.1.3 chs avl_tree_t *t; 1465 1.1.1.3 chs avl_index_t where; 1466 1.1.1.3 chs 1467 1.1.1.3 chs if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */ 1468 1.1.1.3 chs otxg = ZILTEST_TXG; 1469 1.1.1.3 chs else 1470 1.1.1.3 chs otxg = spa_last_synced_txg(zilog->zl_spa) + 1; 1471 1.1.1.3 chs 1472 1.1.1.3 chs /* 1473 1.1.1.3 chs * This is inherently racy, since there is nothing to prevent 1474 1.1.1.3 chs * the last synced txg from changing. 1475 1.1.1.3 chs */ 1476 1.1.1.3 chs for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) { 1477 1.1.1.3 chs itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK]; 1478 1.1.1.3 chs 1479 1.1.1.3 chs mutex_enter(&itxg->itxg_lock); 1480 1.1.1.3 chs if (itxg->itxg_txg != txg) { 1481 1.1.1.3 chs mutex_exit(&itxg->itxg_lock); 1482 1.1.1.3 chs continue; 1483 1.1.1.3 chs } 1484 1.1.1.3 chs 1485 1.1.1.3 chs /* 1486 1.1.1.3 chs * If a foid is specified then find that node and append its 1487 1.1.1.3 chs * list. Otherwise walk the tree appending all the lists 1488 1.1.1.3 chs * to the sync list. We add to the end rather than the 1489 1.1.1.3 chs * beginning to ensure the create has happened. 1490 1.1.1.3 chs */ 1491 1.1.1.3 chs t = &itxg->itxg_itxs->i_async_tree; 1492 1.1.1.3 chs if (foid != 0) { 1493 1.1.1.3 chs ian = avl_find(t, &foid, &where); 1494 1.1.1.3 chs if (ian != NULL) { 1495 1.1.1.3 chs list_move_tail(&itxg->itxg_itxs->i_sync_list, 1496 1.1.1.3 chs &ian->ia_list); 1497 1.1.1.3 chs } 1498 1.1.1.3 chs } else { 1499 1.1.1.3 chs void *cookie = NULL; 1500 1.1.1.3 chs 1501 1.1.1.3 chs while ((ian = avl_destroy_nodes(t, &cookie)) != NULL) { 1502 1.1.1.3 chs list_move_tail(&itxg->itxg_itxs->i_sync_list, 1503 1.1.1.3 chs &ian->ia_list); 1504 1.1.1.3 chs list_destroy(&ian->ia_list); 1505 1.1.1.3 chs kmem_free(ian, sizeof (itx_async_node_t)); 1506 1.1.1.3 chs } 1507 1.1.1.3 chs } 1508 1.1.1.3 chs mutex_exit(&itxg->itxg_lock); 1509 1.1 haad } 1510 1.1 haad } 1511 1.1 haad 1512 1.1 haad static void 1513 1.1.1.3 chs zil_commit_writer(zilog_t *zilog) 1514 1.1 haad { 1515 1.1 haad uint64_t txg; 1516 1.1.1.3 chs itx_t *itx; 1517 1.1 haad lwb_t *lwb; 1518 1.1.1.3 chs spa_t *spa = zilog->zl_spa; 1519 1.1.1.2 haad int error = 0; 1520 1.1 haad 1521 1.1 haad ASSERT(zilog->zl_root_zio == NULL); 1522 1.1.1.3 chs 1523 1.1.1.3 chs mutex_exit(&zilog->zl_lock); 1524 1.1.1.3 chs 1525 1.1.1.3 chs zil_get_commit_list(zilog); 1526 1.1.1.3 chs 1527 1.1.1.3 chs /* 1528 1.1.1.3 chs * Return if there's nothing to commit before we dirty the fs by 1529 1.1.1.3 chs * calling zil_create(). 1530 1.1.1.3 chs */ 1531 1.1.1.3 chs if (list_head(&zilog->zl_itx_commit_list) == NULL) { 1532 1.1.1.3 chs mutex_enter(&zilog->zl_lock); 1533 1.1.1.3 chs return; 1534 1.1.1.3 chs } 1535 1.1 haad 1536 1.1 haad if (zilog->zl_suspend) { 1537 1.1 haad lwb = NULL; 1538 1.1 haad } else { 1539 1.1 haad lwb = list_tail(&zilog->zl_lwb_list); 1540 1.1.1.3 chs if (lwb == NULL) 1541 1.1.1.2 haad lwb = zil_create(zilog); 1542 1.1 haad } 1543 1.1 haad 1544 1.1 haad DTRACE_PROBE1(zil__cw1, zilog_t *, zilog); 1545 1.1.1.3 chs while (itx = list_head(&zilog->zl_itx_commit_list)) { 1546 1.1.1.3 chs txg = itx->itx_lr.lrc_txg; 1547 1.1.1.3 chs ASSERT3U(txg, !=, 0); 1548 1.1.1.2 haad 1549 1.1.1.2 haad /* 1550 1.1.1.3 chs * This is inherently racy and may result in us writing 1551 1.1.1.3 chs * out a log block for a txg that was just synced. This is 1552 1.1.1.3 chs * ok since we'll end cleaning up that log block the next 1553 1.1.1.3 chs * time we call zil_sync(). 1554 1.1.1.2 haad */ 1555 1.1.1.3 chs if (txg > spa_last_synced_txg(spa) || txg > spa_freeze_txg(spa)) 1556 1.1 haad lwb = zil_lwb_commit(zilog, itx, lwb); 1557 1.1.1.3 chs list_remove(&zilog->zl_itx_commit_list, itx); 1558 1.1.1.3 chs kmem_free(itx, offsetof(itx_t, itx_lr) 1559 1.1.1.3 chs + itx->itx_lr.lrc_reclen); 1560 1.1 haad } 1561 1.1 haad DTRACE_PROBE1(zil__cw2, zilog_t *, zilog); 1562 1.1 haad 1563 1.1 haad /* write the last block out */ 1564 1.1 haad if (lwb != NULL && lwb->lwb_zio != NULL) 1565 1.1.1.3 chs lwb = zil_lwb_write_start(zilog, lwb, B_TRUE); 1566 1.1 haad 1567 1.1 haad zilog->zl_cur_used = 0; 1568 1.1 haad 1569 1.1 haad /* 1570 1.1 haad * Wait if necessary for the log blocks to be on stable storage. 1571 1.1 haad */ 1572 1.1 haad if (zilog->zl_root_zio) { 1573 1.1.1.2 haad error = zio_wait(zilog->zl_root_zio); 1574 1.1 haad zilog->zl_root_zio = NULL; 1575 1.1 haad zil_flush_vdevs(zilog); 1576 1.1 haad } 1577 1.1 haad 1578 1.1.1.2 haad if (error || lwb == NULL) 1579 1.1 haad txg_wait_synced(zilog->zl_dmu_pool, 0); 1580 1.1 haad 1581 1.1 haad mutex_enter(&zilog->zl_lock); 1582 1.1.1.2 haad 1583 1.1.1.2 haad /* 1584 1.1.1.2 haad * Remember the highest committed log sequence number for ztest. 1585 1.1.1.2 haad * We only update this value when all the log writes succeeded, 1586 1.1.1.2 haad * because ztest wants to ASSERT that it got the whole log chain. 1587 1.1.1.2 haad */ 1588 1.1.1.2 haad if (error == 0 && lwb != NULL) 1589 1.1.1.2 haad zilog->zl_commit_lr_seq = zilog->zl_lr_seq; 1590 1.1 haad } 1591 1.1 haad 1592 1.1 haad /* 1593 1.1.1.3 chs * Commit zfs transactions to stable storage. 1594 1.1 haad * If foid is 0 push out all transactions, otherwise push only those 1595 1.1.1.3 chs * for that object or might reference that object. 1596 1.1.1.3 chs * 1597 1.1.1.3 chs * itxs are committed in batches. In a heavily stressed zil there will be 1598 1.1.1.3 chs * a commit writer thread who is writing out a bunch of itxs to the log 1599 1.1.1.3 chs * for a set of committing threads (cthreads) in the same batch as the writer. 1600 1.1.1.3 chs * Those cthreads are all waiting on the same cv for that batch. 1601 1.1.1.3 chs * 1602 1.1.1.3 chs * There will also be a different and growing batch of threads that are 1603 1.1.1.3 chs * waiting to commit (qthreads). When the committing batch completes 1604 1.1.1.3 chs * a transition occurs such that the cthreads exit and the qthreads become 1605 1.1.1.3 chs * cthreads. One of the new cthreads becomes the writer thread for the 1606 1.1.1.3 chs * batch. Any new threads arriving become new qthreads. 1607 1.1.1.3 chs * 1608 1.1.1.3 chs * Only 2 condition variables are needed and there's no transition 1609 1.1.1.3 chs * between the two cvs needed. They just flip-flop between qthreads 1610 1.1.1.3 chs * and cthreads. 1611 1.1.1.3 chs * 1612 1.1.1.3 chs * Using this scheme we can efficiently wakeup up only those threads 1613 1.1.1.3 chs * that have been committed. 1614 1.1 haad */ 1615 1.1 haad void 1616 1.1.1.3 chs zil_commit(zilog_t *zilog, uint64_t foid) 1617 1.1 haad { 1618 1.1.1.3 chs uint64_t mybatch; 1619 1.1 haad 1620 1.1.1.3 chs if (zilog->zl_sync == ZFS_SYNC_DISABLED) 1621 1.1.1.3 chs return; 1622 1.1 haad 1623 1.1.1.3 chs /* move the async itxs for the foid to the sync queues */ 1624 1.1.1.3 chs zil_async_to_sync(zilog, foid); 1625 1.1 haad 1626 1.1.1.3 chs mutex_enter(&zilog->zl_lock); 1627 1.1.1.3 chs mybatch = zilog->zl_next_batch; 1628 1.1 haad while (zilog->zl_writer) { 1629 1.1.1.3 chs cv_wait(&zilog->zl_cv_batch[mybatch & 1], &zilog->zl_lock); 1630 1.1.1.3 chs if (mybatch <= zilog->zl_com_batch) { 1631 1.1 haad mutex_exit(&zilog->zl_lock); 1632 1.1 haad return; 1633 1.1 haad } 1634 1.1 haad } 1635 1.1.1.2 haad 1636 1.1.1.3 chs zilog->zl_next_batch++; 1637 1.1.1.3 chs zilog->zl_writer = B_TRUE; 1638 1.1.1.3 chs zil_commit_writer(zilog); 1639 1.1.1.3 chs zilog->zl_com_batch = mybatch; 1640 1.1.1.3 chs zilog->zl_writer = B_FALSE; 1641 1.1.1.3 chs mutex_exit(&zilog->zl_lock); 1642 1.1.1.2 haad 1643 1.1.1.3 chs /* wake up one thread to become the next writer */ 1644 1.1.1.3 chs cv_signal(&zilog->zl_cv_batch[(mybatch+1) & 1]); 1645 1.1.1.2 haad 1646 1.1.1.3 chs /* wake up all threads waiting for this batch to be committed */ 1647 1.1.1.3 chs cv_broadcast(&zilog->zl_cv_batch[mybatch & 1]); 1648 1.1.1.2 haad } 1649 1.1.1.2 haad 1650 1.1.1.2 haad /* 1651 1.1 haad * Called in syncing context to free committed log blocks and update log header. 1652 1.1 haad */ 1653 1.1 haad void 1654 1.1 haad zil_sync(zilog_t *zilog, dmu_tx_t *tx) 1655 1.1 haad { 1656 1.1 haad zil_header_t *zh = zil_header_in_syncing_context(zilog); 1657 1.1 haad uint64_t txg = dmu_tx_get_txg(tx); 1658 1.1 haad spa_t *spa = zilog->zl_spa; 1659 1.1.1.2 haad uint64_t *replayed_seq = &zilog->zl_replayed_seq[txg & TXG_MASK]; 1660 1.1 haad lwb_t *lwb; 1661 1.1 haad 1662 1.1.1.2 haad /* 1663 1.1.1.2 haad * We don't zero out zl_destroy_txg, so make sure we don't try 1664 1.1.1.2 haad * to destroy it twice. 1665 1.1.1.2 haad */ 1666 1.1.1.2 haad if (spa_sync_pass(spa) != 1) 1667 1.1.1.2 haad return; 1668 1.1.1.2 haad 1669 1.1 haad mutex_enter(&zilog->zl_lock); 1670 1.1 haad 1671 1.1 haad ASSERT(zilog->zl_stop_sync == 0); 1672 1.1 haad 1673 1.1.1.2 haad if (*replayed_seq != 0) { 1674 1.1.1.2 haad ASSERT(zh->zh_replay_seq < *replayed_seq); 1675 1.1.1.2 haad zh->zh_replay_seq = *replayed_seq; 1676 1.1.1.2 haad *replayed_seq = 0; 1677 1.1.1.2 haad } 1678 1.1 haad 1679 1.1 haad if (zilog->zl_destroy_txg == txg) { 1680 1.1 haad blkptr_t blk = zh->zh_log; 1681 1.1 haad 1682 1.1 haad ASSERT(list_head(&zilog->zl_lwb_list) == NULL); 1683 1.1 haad 1684 1.1 haad bzero(zh, sizeof (zil_header_t)); 1685 1.1.1.2 haad bzero(zilog->zl_replayed_seq, sizeof (zilog->zl_replayed_seq)); 1686 1.1 haad 1687 1.1 haad if (zilog->zl_keep_first) { 1688 1.1 haad /* 1689 1.1 haad * If this block was part of log chain that couldn't 1690 1.1 haad * be claimed because a device was missing during 1691 1.1 haad * zil_claim(), but that device later returns, 1692 1.1 haad * then this block could erroneously appear valid. 1693 1.1 haad * To guard against this, assign a new GUID to the new 1694 1.1 haad * log chain so it doesn't matter what blk points to. 1695 1.1 haad */ 1696 1.1 haad zil_init_log_chain(zilog, &blk); 1697 1.1 haad zh->zh_log = blk; 1698 1.1 haad } 1699 1.1 haad } 1700 1.1 haad 1701 1.1.1.2 haad while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 1702 1.1 haad zh->zh_log = lwb->lwb_blk; 1703 1.1 haad if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg) 1704 1.1 haad break; 1705 1.1 haad list_remove(&zilog->zl_lwb_list, lwb); 1706 1.1.1.2 haad zio_free_zil(spa, txg, &lwb->lwb_blk); 1707 1.1 haad kmem_cache_free(zil_lwb_cache, lwb); 1708 1.1 haad 1709 1.1 haad /* 1710 1.1 haad * If we don't have anything left in the lwb list then 1711 1.1 haad * we've had an allocation failure and we need to zero 1712 1.1 haad * out the zil_header blkptr so that we don't end 1713 1.1 haad * up freeing the same block twice. 1714 1.1 haad */ 1715 1.1 haad if (list_head(&zilog->zl_lwb_list) == NULL) 1716 1.1 haad BP_ZERO(&zh->zh_log); 1717 1.1 haad } 1718 1.1 haad mutex_exit(&zilog->zl_lock); 1719 1.1 haad } 1720 1.1 haad 1721 1.1 haad void 1722 1.1 haad zil_init(void) 1723 1.1 haad { 1724 1.1 haad zil_lwb_cache = kmem_cache_create("zil_lwb_cache", 1725 1.1 haad sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0); 1726 1.1 haad } 1727 1.1 haad 1728 1.1 haad void 1729 1.1 haad zil_fini(void) 1730 1.1 haad { 1731 1.1 haad kmem_cache_destroy(zil_lwb_cache); 1732 1.1 haad } 1733 1.1 haad 1734 1.1.1.2 haad void 1735 1.1.1.3 chs zil_set_sync(zilog_t *zilog, uint64_t sync) 1736 1.1.1.3 chs { 1737 1.1.1.3 chs zilog->zl_sync = sync; 1738 1.1.1.3 chs } 1739 1.1.1.3 chs 1740 1.1.1.3 chs void 1741 1.1.1.2 haad zil_set_logbias(zilog_t *zilog, uint64_t logbias) 1742 1.1.1.2 haad { 1743 1.1.1.2 haad zilog->zl_logbias = logbias; 1744 1.1.1.2 haad } 1745 1.1.1.2 haad 1746 1.1 haad zilog_t * 1747 1.1 haad zil_alloc(objset_t *os, zil_header_t *zh_phys) 1748 1.1 haad { 1749 1.1 haad zilog_t *zilog; 1750 1.1 haad 1751 1.1 haad zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP); 1752 1.1 haad 1753 1.1 haad zilog->zl_header = zh_phys; 1754 1.1 haad zilog->zl_os = os; 1755 1.1 haad zilog->zl_spa = dmu_objset_spa(os); 1756 1.1 haad zilog->zl_dmu_pool = dmu_objset_pool(os); 1757 1.1 haad zilog->zl_destroy_txg = TXG_INITIAL - 1; 1758 1.1.1.2 haad zilog->zl_logbias = dmu_objset_logbias(os); 1759 1.1.1.3 chs zilog->zl_sync = dmu_objset_syncprop(os); 1760 1.1.1.3 chs zilog->zl_next_batch = 1; 1761 1.1 haad 1762 1.1 haad mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL); 1763 1.1 haad 1764 1.1.1.3 chs for (int i = 0; i < TXG_SIZE; i++) { 1765 1.1.1.3 chs mutex_init(&zilog->zl_itxg[i].itxg_lock, NULL, 1766 1.1.1.3 chs MUTEX_DEFAULT, NULL); 1767 1.1.1.3 chs } 1768 1.1 haad 1769 1.1 haad list_create(&zilog->zl_lwb_list, sizeof (lwb_t), 1770 1.1 haad offsetof(lwb_t, lwb_node)); 1771 1.1 haad 1772 1.1.1.3 chs list_create(&zilog->zl_itx_commit_list, sizeof (itx_t), 1773 1.1.1.3 chs offsetof(itx_t, itx_node)); 1774 1.1.1.3 chs 1775 1.1 haad mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL); 1776 1.1 haad 1777 1.1 haad avl_create(&zilog->zl_vdev_tree, zil_vdev_compare, 1778 1.1 haad sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node)); 1779 1.1 haad 1780 1.1 haad cv_init(&zilog->zl_cv_writer, NULL, CV_DEFAULT, NULL); 1781 1.1 haad cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL); 1782 1.1.1.3 chs cv_init(&zilog->zl_cv_batch[0], NULL, CV_DEFAULT, NULL); 1783 1.1.1.3 chs cv_init(&zilog->zl_cv_batch[1], NULL, CV_DEFAULT, NULL); 1784 1.1 haad 1785 1.1 haad return (zilog); 1786 1.1 haad } 1787 1.1 haad 1788 1.1 haad void 1789 1.1 haad zil_free(zilog_t *zilog) 1790 1.1 haad { 1791 1.1 haad zilog->zl_stop_sync = 1; 1792 1.1 haad 1793 1.1.1.3 chs ASSERT0(zilog->zl_suspend); 1794 1.1.1.3 chs ASSERT0(zilog->zl_suspending); 1795 1.1.1.3 chs 1796 1.1.1.3 chs ASSERT(list_is_empty(&zilog->zl_lwb_list)); 1797 1.1 haad list_destroy(&zilog->zl_lwb_list); 1798 1.1 haad 1799 1.1 haad avl_destroy(&zilog->zl_vdev_tree); 1800 1.1 haad mutex_destroy(&zilog->zl_vdev_lock); 1801 1.1 haad 1802 1.1.1.3 chs ASSERT(list_is_empty(&zilog->zl_itx_commit_list)); 1803 1.1.1.3 chs list_destroy(&zilog->zl_itx_commit_list); 1804 1.1.1.3 chs 1805 1.1.1.3 chs for (int i = 0; i < TXG_SIZE; i++) { 1806 1.1.1.3 chs /* 1807 1.1.1.3 chs * It's possible for an itx to be generated that doesn't dirty 1808 1.1.1.3 chs * a txg (e.g. ztest TX_TRUNCATE). So there's no zil_clean() 1809 1.1.1.3 chs * callback to remove the entry. We remove those here. 1810 1.1.1.3 chs * 1811 1.1.1.3 chs * Also free up the ziltest itxs. 1812 1.1.1.3 chs */ 1813 1.1.1.3 chs if (zilog->zl_itxg[i].itxg_itxs) 1814 1.1.1.3 chs zil_itxg_clean(zilog->zl_itxg[i].itxg_itxs); 1815 1.1.1.3 chs mutex_destroy(&zilog->zl_itxg[i].itxg_lock); 1816 1.1.1.3 chs } 1817 1.1.1.3 chs 1818 1.1 haad mutex_destroy(&zilog->zl_lock); 1819 1.1 haad 1820 1.1 haad cv_destroy(&zilog->zl_cv_writer); 1821 1.1 haad cv_destroy(&zilog->zl_cv_suspend); 1822 1.1.1.3 chs cv_destroy(&zilog->zl_cv_batch[0]); 1823 1.1.1.3 chs cv_destroy(&zilog->zl_cv_batch[1]); 1824 1.1 haad 1825 1.1 haad kmem_free(zilog, sizeof (zilog_t)); 1826 1.1 haad } 1827 1.1 haad 1828 1.1 haad /* 1829 1.1 haad * Open an intent log. 1830 1.1 haad */ 1831 1.1 haad zilog_t * 1832 1.1 haad zil_open(objset_t *os, zil_get_data_t *get_data) 1833 1.1 haad { 1834 1.1 haad zilog_t *zilog = dmu_objset_zil(os); 1835 1.1 haad 1836 1.1.1.3 chs ASSERT(zilog->zl_clean_taskq == NULL); 1837 1.1.1.3 chs ASSERT(zilog->zl_get_data == NULL); 1838 1.1.1.3 chs ASSERT(list_is_empty(&zilog->zl_lwb_list)); 1839 1.1.1.3 chs 1840 1.1 haad zilog->zl_get_data = get_data; 1841 1.1 haad zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri, 1842 1.1 haad 2, 2, TASKQ_PREPOPULATE); 1843 1.1 haad 1844 1.1 haad return (zilog); 1845 1.1 haad } 1846 1.1 haad 1847 1.1 haad /* 1848 1.1 haad * Close an intent log. 1849 1.1 haad */ 1850 1.1 haad void 1851 1.1 haad zil_close(zilog_t *zilog) 1852 1.1 haad { 1853 1.1.1.3 chs lwb_t *lwb; 1854 1.1.1.3 chs uint64_t txg = 0; 1855 1.1.1.3 chs 1856 1.1.1.3 chs zil_commit(zilog, 0); /* commit all itx */ 1857 1.1.1.3 chs 1858 1.1 haad /* 1859 1.1.1.3 chs * The lwb_max_txg for the stubby lwb will reflect the last activity 1860 1.1.1.3 chs * for the zil. After a txg_wait_synced() on the txg we know all the 1861 1.1.1.3 chs * callbacks have occurred that may clean the zil. Only then can we 1862 1.1.1.3 chs * destroy the zl_clean_taskq. 1863 1.1 haad */ 1864 1.1.1.3 chs mutex_enter(&zilog->zl_lock); 1865 1.1.1.3 chs lwb = list_tail(&zilog->zl_lwb_list); 1866 1.1.1.3 chs if (lwb != NULL) 1867 1.1.1.3 chs txg = lwb->lwb_max_txg; 1868 1.1.1.3 chs mutex_exit(&zilog->zl_lock); 1869 1.1.1.3 chs if (txg) 1870 1.1 haad txg_wait_synced(zilog->zl_dmu_pool, txg); 1871 1.1.1.3 chs 1872 1.1.1.3 chs if (zilog_is_dirty(zilog)) 1873 1.1.1.3 chs zfs_dbgmsg("zil (%p) is dirty, txg %llu", zilog, txg); 1874 1.1.1.3 chs VERIFY(!zilog_is_dirty(zilog)); 1875 1.1 haad 1876 1.1 haad taskq_destroy(zilog->zl_clean_taskq); 1877 1.1 haad zilog->zl_clean_taskq = NULL; 1878 1.1 haad zilog->zl_get_data = NULL; 1879 1.1 haad 1880 1.1.1.3 chs /* 1881 1.1.1.3 chs * We should have only one LWB left on the list; remove it now. 1882 1.1.1.3 chs */ 1883 1.1.1.3 chs mutex_enter(&zilog->zl_lock); 1884 1.1.1.3 chs lwb = list_head(&zilog->zl_lwb_list); 1885 1.1.1.3 chs if (lwb != NULL) { 1886 1.1.1.3 chs ASSERT(lwb == list_tail(&zilog->zl_lwb_list)); 1887 1.1.1.3 chs list_remove(&zilog->zl_lwb_list, lwb); 1888 1.1.1.3 chs zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 1889 1.1.1.3 chs kmem_cache_free(zil_lwb_cache, lwb); 1890 1.1.1.3 chs } 1891 1.1.1.3 chs mutex_exit(&zilog->zl_lock); 1892 1.1 haad } 1893 1.1 haad 1894 1.1.1.3 chs static char *suspend_tag = "zil suspending"; 1895 1.1.1.3 chs 1896 1.1 haad /* 1897 1.1 haad * Suspend an intent log. While in suspended mode, we still honor 1898 1.1 haad * synchronous semantics, but we rely on txg_wait_synced() to do it. 1899 1.1.1.3 chs * On old version pools, we suspend the log briefly when taking a 1900 1.1.1.3 chs * snapshot so that it will have an empty intent log. 1901 1.1.1.3 chs * 1902 1.1.1.3 chs * Long holds are not really intended to be used the way we do here -- 1903 1.1.1.3 chs * held for such a short time. A concurrent caller of dsl_dataset_long_held() 1904 1.1.1.3 chs * could fail. Therefore we take pains to only put a long hold if it is 1905 1.1.1.3 chs * actually necessary. Fortunately, it will only be necessary if the 1906 1.1.1.3 chs * objset is currently mounted (or the ZVOL equivalent). In that case it 1907 1.1.1.3 chs * will already have a long hold, so we are not really making things any worse. 1908 1.1.1.3 chs * 1909 1.1.1.3 chs * Ideally, we would locate the existing long-holder (i.e. the zfsvfs_t or 1910 1.1.1.3 chs * zvol_state_t), and use their mechanism to prevent their hold from being 1911 1.1.1.3 chs * dropped (e.g. VFS_HOLD()). However, that would be even more pain for 1912 1.1.1.3 chs * very little gain. 1913 1.1.1.3 chs * 1914 1.1.1.3 chs * if cookiep == NULL, this does both the suspend & resume. 1915 1.1.1.3 chs * Otherwise, it returns with the dataset "long held", and the cookie 1916 1.1.1.3 chs * should be passed into zil_resume(). 1917 1.1 haad */ 1918 1.1 haad int 1919 1.1.1.3 chs zil_suspend(const char *osname, void **cookiep) 1920 1.1 haad { 1921 1.1.1.3 chs objset_t *os; 1922 1.1.1.3 chs zilog_t *zilog; 1923 1.1.1.3 chs const zil_header_t *zh; 1924 1.1.1.3 chs int error; 1925 1.1.1.3 chs 1926 1.1.1.3 chs error = dmu_objset_hold(osname, suspend_tag, &os); 1927 1.1.1.3 chs if (error != 0) 1928 1.1.1.3 chs return (error); 1929 1.1.1.3 chs zilog = dmu_objset_zil(os); 1930 1.1 haad 1931 1.1 haad mutex_enter(&zilog->zl_lock); 1932 1.1.1.3 chs zh = zilog->zl_header; 1933 1.1.1.3 chs 1934 1.1.1.2 haad if (zh->zh_flags & ZIL_REPLAY_NEEDED) { /* unplayed log */ 1935 1.1 haad mutex_exit(&zilog->zl_lock); 1936 1.1.1.3 chs dmu_objset_rele(os, suspend_tag); 1937 1.1.1.3 chs return (SET_ERROR(EBUSY)); 1938 1.1.1.3 chs } 1939 1.1.1.3 chs 1940 1.1.1.3 chs /* 1941 1.1.1.3 chs * Don't put a long hold in the cases where we can avoid it. This 1942 1.1.1.3 chs * is when there is no cookie so we are doing a suspend & resume 1943 1.1.1.3 chs * (i.e. called from zil_vdev_offline()), and there's nothing to do 1944 1.1.1.3 chs * for the suspend because it's already suspended, or there's no ZIL. 1945 1.1.1.3 chs */ 1946 1.1.1.3 chs if (cookiep == NULL && !zilog->zl_suspending && 1947 1.1.1.3 chs (zilog->zl_suspend > 0 || BP_IS_HOLE(&zh->zh_log))) { 1948 1.1.1.3 chs mutex_exit(&zilog->zl_lock); 1949 1.1.1.3 chs dmu_objset_rele(os, suspend_tag); 1950 1.1.1.3 chs return (0); 1951 1.1 haad } 1952 1.1.1.3 chs 1953 1.1.1.3 chs dsl_dataset_long_hold(dmu_objset_ds(os), suspend_tag); 1954 1.1.1.3 chs dsl_pool_rele(dmu_objset_pool(os), suspend_tag); 1955 1.1.1.3 chs 1956 1.1.1.3 chs zilog->zl_suspend++; 1957 1.1.1.3 chs 1958 1.1.1.3 chs if (zilog->zl_suspend > 1) { 1959 1.1 haad /* 1960 1.1.1.3 chs * Someone else is already suspending it. 1961 1.1 haad * Just wait for them to finish. 1962 1.1 haad */ 1963 1.1.1.3 chs 1964 1.1 haad while (zilog->zl_suspending) 1965 1.1 haad cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock); 1966 1.1 haad mutex_exit(&zilog->zl_lock); 1967 1.1.1.3 chs 1968 1.1.1.3 chs if (cookiep == NULL) 1969 1.1.1.3 chs zil_resume(os); 1970 1.1.1.3 chs else 1971 1.1.1.3 chs *cookiep = os; 1972 1.1 haad return (0); 1973 1.1 haad } 1974 1.1 haad 1975 1.1 haad /* 1976 1.1.1.3 chs * If there is no pointer to an on-disk block, this ZIL must not 1977 1.1.1.3 chs * be active (e.g. filesystem not mounted), so there's nothing 1978 1.1.1.3 chs * to clean up. 1979 1.1 haad */ 1980 1.1.1.3 chs if (BP_IS_HOLE(&zh->zh_log)) { 1981 1.1.1.3 chs ASSERT(cookiep != NULL); /* fast path already handled */ 1982 1.1.1.3 chs 1983 1.1.1.3 chs *cookiep = os; 1984 1.1.1.3 chs mutex_exit(&zilog->zl_lock); 1985 1.1.1.3 chs return (0); 1986 1.1.1.3 chs } 1987 1.1.1.3 chs 1988 1.1.1.3 chs zilog->zl_suspending = B_TRUE; 1989 1.1 haad mutex_exit(&zilog->zl_lock); 1990 1.1 haad 1991 1.1.1.3 chs zil_commit(zilog, 0); 1992 1.1.1.3 chs 1993 1.1 haad zil_destroy(zilog, B_FALSE); 1994 1.1 haad 1995 1.1 haad mutex_enter(&zilog->zl_lock); 1996 1.1 haad zilog->zl_suspending = B_FALSE; 1997 1.1 haad cv_broadcast(&zilog->zl_cv_suspend); 1998 1.1 haad mutex_exit(&zilog->zl_lock); 1999 1.1 haad 2000 1.1.1.3 chs if (cookiep == NULL) 2001 1.1.1.3 chs zil_resume(os); 2002 1.1.1.3 chs else 2003 1.1.1.3 chs *cookiep = os; 2004 1.1 haad return (0); 2005 1.1 haad } 2006 1.1 haad 2007 1.1 haad void 2008 1.1.1.3 chs zil_resume(void *cookie) 2009 1.1 haad { 2010 1.1.1.3 chs objset_t *os = cookie; 2011 1.1.1.3 chs zilog_t *zilog = dmu_objset_zil(os); 2012 1.1.1.3 chs 2013 1.1 haad mutex_enter(&zilog->zl_lock); 2014 1.1 haad ASSERT(zilog->zl_suspend != 0); 2015 1.1 haad zilog->zl_suspend--; 2016 1.1 haad mutex_exit(&zilog->zl_lock); 2017 1.1.1.3 chs dsl_dataset_long_rele(dmu_objset_ds(os), suspend_tag); 2018 1.1.1.3 chs dsl_dataset_rele(dmu_objset_ds(os), suspend_tag); 2019 1.1 haad } 2020 1.1 haad 2021 1.1 haad typedef struct zil_replay_arg { 2022 1.1 haad zil_replay_func_t **zr_replay; 2023 1.1 haad void *zr_arg; 2024 1.1 haad boolean_t zr_byteswap; 2025 1.1.1.2 haad char *zr_lr; 2026 1.1 haad } zil_replay_arg_t; 2027 1.1 haad 2028 1.1.1.2 haad static int 2029 1.1.1.2 haad zil_replay_error(zilog_t *zilog, lr_t *lr, int error) 2030 1.1.1.2 haad { 2031 1.1.1.3 chs char name[ZFS_MAX_DATASET_NAME_LEN]; 2032 1.1.1.2 haad 2033 1.1.1.2 haad zilog->zl_replaying_seq--; /* didn't actually replay this one */ 2034 1.1.1.2 haad 2035 1.1.1.2 haad dmu_objset_name(zilog->zl_os, name); 2036 1.1.1.2 haad 2037 1.1.1.2 haad cmn_err(CE_WARN, "ZFS replay transaction error %d, " 2038 1.1.1.2 haad "dataset %s, seq 0x%llx, txtype %llu %s\n", error, name, 2039 1.1.1.2 haad (u_longlong_t)lr->lrc_seq, 2040 1.1.1.2 haad (u_longlong_t)(lr->lrc_txtype & ~TX_CI), 2041 1.1.1.2 haad (lr->lrc_txtype & TX_CI) ? "CI" : ""); 2042 1.1.1.2 haad 2043 1.1.1.2 haad return (error); 2044 1.1.1.2 haad } 2045 1.1.1.2 haad 2046 1.1.1.2 haad static int 2047 1.1 haad zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg) 2048 1.1 haad { 2049 1.1 haad zil_replay_arg_t *zr = zra; 2050 1.1 haad const zil_header_t *zh = zilog->zl_header; 2051 1.1 haad uint64_t reclen = lr->lrc_reclen; 2052 1.1 haad uint64_t txtype = lr->lrc_txtype; 2053 1.1.1.2 haad int error = 0; 2054 1.1 haad 2055 1.1.1.2 haad zilog->zl_replaying_seq = lr->lrc_seq; 2056 1.1 haad 2057 1.1 haad if (lr->lrc_seq <= zh->zh_replay_seq) /* already replayed */ 2058 1.1.1.2 haad return (0); 2059 1.1.1.2 haad 2060 1.1.1.2 haad if (lr->lrc_txg < claim_txg) /* already committed */ 2061 1.1.1.2 haad return (0); 2062 1.1 haad 2063 1.1 haad /* Strip case-insensitive bit, still present in log record */ 2064 1.1 haad txtype &= ~TX_CI; 2065 1.1 haad 2066 1.1.1.2 haad if (txtype == 0 || txtype >= TX_MAX_TYPE) 2067 1.1.1.2 haad return (zil_replay_error(zilog, lr, EINVAL)); 2068 1.1.1.2 haad 2069 1.1 haad /* 2070 1.1.1.2 haad * If this record type can be logged out of order, the object 2071 1.1.1.2 haad * (lr_foid) may no longer exist. That's legitimate, not an error. 2072 1.1 haad */ 2073 1.1.1.2 haad if (TX_OOO(txtype)) { 2074 1.1.1.2 haad error = dmu_object_info(zilog->zl_os, 2075 1.1.1.2 haad ((lr_ooo_t *)lr)->lr_foid, NULL); 2076 1.1.1.2 haad if (error == ENOENT || error == EEXIST) 2077 1.1.1.2 haad return (0); 2078 1.1.1.2 haad } 2079 1.1 haad 2080 1.1 haad /* 2081 1.1.1.2 haad * Make a copy of the data so we can revise and extend it. 2082 1.1 haad */ 2083 1.1.1.2 haad bcopy(lr, zr->zr_lr, reclen); 2084 1.1 haad 2085 1.1 haad /* 2086 1.1 haad * If this is a TX_WRITE with a blkptr, suck in the data. 2087 1.1 haad */ 2088 1.1 haad if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) { 2089 1.1.1.2 haad error = zil_read_log_data(zilog, (lr_write_t *)lr, 2090 1.1.1.2 haad zr->zr_lr + reclen); 2091 1.1.1.3 chs if (error != 0) 2092 1.1.1.2 haad return (zil_replay_error(zilog, lr, error)); 2093 1.1 haad } 2094 1.1 haad 2095 1.1 haad /* 2096 1.1.1.2 haad * The log block containing this lr may have been byteswapped 2097 1.1.1.2 haad * so that we can easily examine common fields like lrc_txtype. 2098 1.1.1.2 haad * However, the log is a mix of different record types, and only the 2099 1.1.1.2 haad * replay vectors know how to byteswap their records. Therefore, if 2100 1.1.1.2 haad * the lr was byteswapped, undo it before invoking the replay vector. 2101 1.1.1.2 haad */ 2102 1.1.1.2 haad if (zr->zr_byteswap) 2103 1.1.1.2 haad byteswap_uint64_array(zr->zr_lr, reclen); 2104 1.1 haad 2105 1.1 haad /* 2106 1.1 haad * We must now do two things atomically: replay this log record, 2107 1.1.1.2 haad * and update the log header sequence number to reflect the fact that 2108 1.1.1.2 haad * we did so. At the end of each replay function the sequence number 2109 1.1.1.2 haad * is updated if we are in replay mode. 2110 1.1 haad */ 2111 1.1.1.2 haad error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, zr->zr_byteswap); 2112 1.1.1.3 chs if (error != 0) { 2113 1.1 haad /* 2114 1.1 haad * The DMU's dnode layer doesn't see removes until the txg 2115 1.1 haad * commits, so a subsequent claim can spuriously fail with 2116 1.1.1.2 haad * EEXIST. So if we receive any error we try syncing out 2117 1.1.1.2 haad * any removes then retry the transaction. Note that we 2118 1.1.1.2 haad * specify B_FALSE for byteswap now, so we don't do it twice. 2119 1.1 haad */ 2120 1.1.1.2 haad txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0); 2121 1.1.1.2 haad error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, B_FALSE); 2122 1.1.1.3 chs if (error != 0) 2123 1.1.1.2 haad return (zil_replay_error(zilog, lr, error)); 2124 1.1 haad } 2125 1.1.1.2 haad return (0); 2126 1.1 haad } 2127 1.1 haad 2128 1.1 haad /* ARGSUSED */ 2129 1.1.1.2 haad static int 2130 1.1 haad zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg) 2131 1.1 haad { 2132 1.1 haad zilog->zl_replay_blks++; 2133 1.1.1.2 haad 2134 1.1.1.2 haad return (0); 2135 1.1 haad } 2136 1.1 haad 2137 1.1 haad /* 2138 1.1 haad * If this dataset has a non-empty intent log, replay it and destroy it. 2139 1.1 haad */ 2140 1.1 haad void 2141 1.1.1.2 haad zil_replay(objset_t *os, void *arg, zil_replay_func_t *replay_func[TX_MAX_TYPE]) 2142 1.1 haad { 2143 1.1 haad zilog_t *zilog = dmu_objset_zil(os); 2144 1.1 haad const zil_header_t *zh = zilog->zl_header; 2145 1.1 haad zil_replay_arg_t zr; 2146 1.1 haad 2147 1.1.1.2 haad if ((zh->zh_flags & ZIL_REPLAY_NEEDED) == 0) { 2148 1.1 haad zil_destroy(zilog, B_TRUE); 2149 1.1 haad return; 2150 1.1 haad } 2151 1.1 haad 2152 1.1 haad zr.zr_replay = replay_func; 2153 1.1 haad zr.zr_arg = arg; 2154 1.1 haad zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log); 2155 1.1.1.2 haad zr.zr_lr = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP); 2156 1.1 haad 2157 1.1 haad /* 2158 1.1 haad * Wait for in-progress removes to sync before starting replay. 2159 1.1 haad */ 2160 1.1 haad txg_wait_synced(zilog->zl_dmu_pool, 0); 2161 1.1 haad 2162 1.1.1.2 haad zilog->zl_replay = B_TRUE; 2163 1.1.1.2 haad zilog->zl_replay_time = ddi_get_lbolt(); 2164 1.1 haad ASSERT(zilog->zl_replay_blks == 0); 2165 1.1 haad (void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr, 2166 1.1 haad zh->zh_claim_txg); 2167 1.1.1.2 haad kmem_free(zr.zr_lr, 2 * SPA_MAXBLOCKSIZE); 2168 1.1 haad 2169 1.1 haad zil_destroy(zilog, B_FALSE); 2170 1.1 haad txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 2171 1.1.1.2 haad zilog->zl_replay = B_FALSE; 2172 1.1 haad } 2173 1.1 haad 2174 1.1.1.2 haad boolean_t 2175 1.1.1.2 haad zil_replaying(zilog_t *zilog, dmu_tx_t *tx) 2176 1.1 haad { 2177 1.1.1.3 chs if (zilog->zl_sync == ZFS_SYNC_DISABLED) 2178 1.1.1.2 haad return (B_TRUE); 2179 1.1 haad 2180 1.1.1.2 haad if (zilog->zl_replay) { 2181 1.1.1.2 haad dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 2182 1.1.1.2 haad zilog->zl_replayed_seq[dmu_tx_get_txg(tx) & TXG_MASK] = 2183 1.1.1.2 haad zilog->zl_replaying_seq; 2184 1.1.1.2 haad return (B_TRUE); 2185 1.1 haad } 2186 1.1 haad 2187 1.1.1.2 haad return (B_FALSE); 2188 1.1.1.2 haad } 2189 1.1 haad 2190 1.1.1.2 haad /* ARGSUSED */ 2191 1.1.1.2 haad int 2192 1.1.1.2 haad zil_vdev_offline(const char *osname, void *arg) 2193 1.1.1.2 haad { 2194 1.1.1.2 haad int error; 2195 1.1 haad 2196 1.1.1.3 chs error = zil_suspend(osname, NULL); 2197 1.1.1.3 chs if (error != 0) 2198 1.1.1.3 chs return (SET_ERROR(EEXIST)); 2199 1.1.1.3 chs return (0); 2200 1.1 haad } 2201