FreeBSD ZFS
The Zettabyte File System
|
00001 /* 00002 * CDDL HEADER START 00003 * 00004 * The contents of this file are subject to the terms of the 00005 * Common Development and Distribution License (the "License"). 00006 * You may not use this file except in compliance with the License. 00007 * 00008 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 00009 * or http://www.opensolaris.org/os/licensing. 00010 * See the License for the specific language governing permissions 00011 * and limitations under the License. 00012 * 00013 * When distributing Covered Code, include this CDDL HEADER in each 00014 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 00015 * If applicable, add the following below this CDDL HEADER, with the 00016 * fields enclosed by brackets "[]" replaced with your own identifying 00017 * information: Portions Copyright [yyyy] [name of copyright owner] 00018 * 00019 * CDDL HEADER END 00020 */ 00021 /* 00022 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 00023 * Copyright (c) 2012 by Delphix. All rights reserved. 00024 */ 00025 00026 /* Portions Copyright 2010 Robert Milkowski */ 00027 00028 #include <sys/zfs_context.h> 00029 #include <sys/spa.h> 00030 #include <sys/dmu.h> 00031 #include <sys/zap.h> 00032 #include <sys/arc.h> 00033 #include <sys/stat.h> 00034 #include <sys/resource.h> 00035 #include <sys/zil.h> 00036 #include <sys/zil_impl.h> 00037 #include <sys/dsl_dataset.h> 00038 #include <sys/vdev_impl.h> 00039 #include <sys/dmu_tx.h> 00040 #include <sys/dsl_pool.h> 00041 00078 int zil_replay_disable = 0; 00079 SYSCTL_DECL(_vfs_zfs); 00080 TUNABLE_INT("vfs.zfs.zil_replay_disable", &zil_replay_disable); 00081 SYSCTL_INT(_vfs_zfs, OID_AUTO, zil_replay_disable, CTLFLAG_RW, 00082 &zil_replay_disable, 0, "Disable intent logging replay"); 00083 00091 boolean_t zfs_nocacheflush = B_FALSE; 00092 TUNABLE_INT("vfs.zfs.cache_flush_disable", &zfs_nocacheflush); 00093 SYSCTL_INT(_vfs_zfs, OID_AUTO, cache_flush_disable, CTLFLAG_RDTUN, 00094 &zfs_nocacheflush, 0, "Disable cache flush"); 00095 boolean_t zfs_notrim = B_TRUE; 00096 TUNABLE_INT("vfs.zfs.trim_disable", &zfs_notrim); 00097 SYSCTL_INT(_vfs_zfs, OID_AUTO, trim_disable, CTLFLAG_RDTUN, &zfs_notrim, 0, 00098 "Disable trim"); 00099 00100 static kmem_cache_t *zil_lwb_cache; 00101 00102 static void zil_async_to_sync(zilog_t *zilog, uint64_t foid); 00103 00104 #define LWB_EMPTY(lwb) ((BP_GET_LSIZE(&lwb->lwb_blk) - \ 00105 sizeof (zil_chain_t)) == (lwb->lwb_sz - lwb->lwb_nused)) 00106 00107 00115 #define ZILTEST_TXG (UINT64_MAX - TXG_CONCURRENT_STATES) 00116 00117 static int 00118 zil_bp_compare(const void *x1, const void *x2) 00119 { 00120 const dva_t *dva1 = &((zil_bp_node_t *)x1)->zn_dva; 00121 const dva_t *dva2 = &((zil_bp_node_t *)x2)->zn_dva; 00122 00123 if (DVA_GET_VDEV(dva1) < DVA_GET_VDEV(dva2)) 00124 return (-1); 00125 if (DVA_GET_VDEV(dva1) > DVA_GET_VDEV(dva2)) 00126 return (1); 00127 00128 if (DVA_GET_OFFSET(dva1) < DVA_GET_OFFSET(dva2)) 00129 return (-1); 00130 if (DVA_GET_OFFSET(dva1) > DVA_GET_OFFSET(dva2)) 00131 return (1); 00132 00133 return (0); 00134 } 00135 00136 static void 00137 zil_bp_tree_init(zilog_t *zilog) 00138 { 00139 avl_create(&zilog->zl_bp_tree, zil_bp_compare, 00140 sizeof (zil_bp_node_t), offsetof(zil_bp_node_t, zn_node)); 00141 } 00142 00143 static void 00144 zil_bp_tree_fini(zilog_t *zilog) 00145 { 00146 avl_tree_t *t = &zilog->zl_bp_tree; 00147 zil_bp_node_t *zn; 00148 void *cookie = NULL; 00149 00150 while ((zn = avl_destroy_nodes(t, &cookie)) != NULL) 00151 kmem_free(zn, sizeof (zil_bp_node_t)); 00152 00153 avl_destroy(t); 00154 } 00155 00156 int 00157 zil_bp_tree_add(zilog_t *zilog, const blkptr_t *bp) 00158 { 00159 avl_tree_t *t = &zilog->zl_bp_tree; 00160 const dva_t *dva = BP_IDENTITY(bp); 00161 zil_bp_node_t *zn; 00162 avl_index_t where; 00163 00164 if (avl_find(t, dva, &where) != NULL) 00165 return (EEXIST); 00166 00167 zn = kmem_alloc(sizeof (zil_bp_node_t), KM_SLEEP); 00168 zn->zn_dva = *dva; 00169 avl_insert(t, zn, where); 00170 00171 return (0); 00172 } 00173 00174 static zil_header_t * 00175 zil_header_in_syncing_context(zilog_t *zilog) 00176 { 00177 return ((zil_header_t *)zilog->zl_header); 00178 } 00179 00180 static void 00181 zil_init_log_chain(zilog_t *zilog, blkptr_t *bp) 00182 { 00183 zio_cksum_t *zc = &bp->blk_cksum; 00184 00185 zc->zc_word[ZIL_ZC_GUID_0] = spa_get_random(-1ULL); 00186 zc->zc_word[ZIL_ZC_GUID_1] = spa_get_random(-1ULL); 00187 zc->zc_word[ZIL_ZC_OBJSET] = dmu_objset_id(zilog->zl_os); 00188 zc->zc_word[ZIL_ZC_SEQ] = 1ULL; 00189 } 00190 00194 static int 00195 zil_read_log_block(zilog_t *zilog, const blkptr_t *bp, blkptr_t *nbp, void *dst, 00196 char **end) 00197 { 00198 enum zio_flag zio_flags = ZIO_FLAG_CANFAIL; 00199 uint32_t aflags = ARC_WAIT; 00200 arc_buf_t *abuf = NULL; 00201 zbookmark_t zb; 00202 int error; 00203 00204 if (zilog->zl_header->zh_claim_txg == 0) 00205 zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB; 00206 00207 if (!(zilog->zl_header->zh_flags & ZIL_CLAIM_LR_SEQ_VALID)) 00208 zio_flags |= ZIO_FLAG_SPECULATIVE; 00209 00210 SET_BOOKMARK(&zb, bp->blk_cksum.zc_word[ZIL_ZC_OBJSET], 00211 ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]); 00212 00213 error = dsl_read_nolock(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf, 00214 ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb); 00215 00216 if (error == 0) { 00217 zio_cksum_t cksum = bp->blk_cksum; 00218 00219 /* 00220 * Validate the checksummed log block. 00221 * 00222 * Sequence numbers should be... sequential. The checksum 00223 * verifier for the next block should be bp's checksum plus 1. 00224 * 00225 * Also check the log chain linkage and size used. 00226 */ 00227 cksum.zc_word[ZIL_ZC_SEQ]++; 00228 00229 if (BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_ZILOG2) { 00230 zil_chain_t *zilc = abuf->b_data; 00231 char *lr = (char *)(zilc + 1); 00232 uint64_t len = zilc->zc_nused - sizeof (zil_chain_t); 00233 00234 if (bcmp(&cksum, &zilc->zc_next_blk.blk_cksum, 00235 sizeof (cksum)) || BP_IS_HOLE(&zilc->zc_next_blk)) { 00236 error = ECKSUM; 00237 } else { 00238 bcopy(lr, dst, len); 00239 *end = (char *)dst + len; 00240 *nbp = zilc->zc_next_blk; 00241 } 00242 } else { 00243 char *lr = abuf->b_data; 00244 uint64_t size = BP_GET_LSIZE(bp); 00245 zil_chain_t *zilc = (zil_chain_t *)(lr + size) - 1; 00246 00247 if (bcmp(&cksum, &zilc->zc_next_blk.blk_cksum, 00248 sizeof (cksum)) || BP_IS_HOLE(&zilc->zc_next_blk) || 00249 (zilc->zc_nused > (size - sizeof (*zilc)))) { 00250 error = ECKSUM; 00251 } else { 00252 bcopy(lr, dst, zilc->zc_nused); 00253 *end = (char *)dst + zilc->zc_nused; 00254 *nbp = zilc->zc_next_blk; 00255 } 00256 } 00257 00258 VERIFY(arc_buf_remove_ref(abuf, &abuf) == 1); 00259 } 00260 00261 return (error); 00262 } 00263 00267 static int 00268 zil_read_log_data(zilog_t *zilog, const lr_write_t *lr, void *wbuf) 00269 { 00270 enum zio_flag zio_flags = ZIO_FLAG_CANFAIL; 00271 const blkptr_t *bp = &lr->lr_blkptr; 00272 uint32_t aflags = ARC_WAIT; 00273 arc_buf_t *abuf = NULL; 00274 zbookmark_t zb; 00275 int error; 00276 00277 if (BP_IS_HOLE(bp)) { 00278 if (wbuf != NULL) 00279 bzero(wbuf, MAX(BP_GET_LSIZE(bp), lr->lr_length)); 00280 return (0); 00281 } 00282 00283 if (zilog->zl_header->zh_claim_txg == 0) 00284 zio_flags |= ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB; 00285 00286 SET_BOOKMARK(&zb, dmu_objset_id(zilog->zl_os), lr->lr_foid, 00287 ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp)); 00288 00289 error = arc_read_nolock(NULL, zilog->zl_spa, bp, arc_getbuf_func, &abuf, 00290 ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb); 00291 00292 if (error == 0) { 00293 if (wbuf != NULL) 00294 bcopy(abuf->b_data, wbuf, arc_buf_size(abuf)); 00295 (void) arc_buf_remove_ref(abuf, &abuf); 00296 } 00297 00298 return (error); 00299 } 00300 00304 int 00305 zil_parse(zilog_t *zilog, zil_parse_blk_func_t *parse_blk_func, 00306 zil_parse_lr_func_t *parse_lr_func, void *arg, uint64_t txg) 00307 { 00308 const zil_header_t *zh = zilog->zl_header; 00309 boolean_t claimed = !!zh->zh_claim_txg; 00310 uint64_t claim_blk_seq = claimed ? zh->zh_claim_blk_seq : UINT64_MAX; 00311 uint64_t claim_lr_seq = claimed ? zh->zh_claim_lr_seq : UINT64_MAX; 00312 uint64_t max_blk_seq = 0; 00313 uint64_t max_lr_seq = 0; 00314 uint64_t blk_count = 0; 00315 uint64_t lr_count = 0; 00316 blkptr_t blk, next_blk; 00317 char *lrbuf, *lrp; 00318 int error = 0; 00319 00320 /* 00321 * Old logs didn't record the maximum zh_claim_lr_seq. 00322 */ 00323 if (!(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID)) 00324 claim_lr_seq = UINT64_MAX; 00325 00326 /* 00327 * Starting at the block pointed to by zh_log we read the log chain. 00328 * For each block in the chain we strongly check that block to 00329 * ensure its validity. We stop when an invalid block is found. 00330 * For each block pointer in the chain we call parse_blk_func(). 00331 * For each record in each valid block we call parse_lr_func(). 00332 * If the log has been claimed, stop if we encounter a sequence 00333 * number greater than the highest claimed sequence number. 00334 */ 00335 lrbuf = zio_buf_alloc(SPA_MAXBLOCKSIZE); 00336 zil_bp_tree_init(zilog); 00337 00338 for (blk = zh->zh_log; !BP_IS_HOLE(&blk); blk = next_blk) { 00339 uint64_t blk_seq = blk.blk_cksum.zc_word[ZIL_ZC_SEQ]; 00340 int reclen; 00341 char *end; 00342 00343 if (blk_seq > claim_blk_seq) 00344 break; 00345 if ((error = parse_blk_func(zilog, &blk, arg, txg)) != 0) 00346 break; 00347 ASSERT3U(max_blk_seq, <, blk_seq); 00348 max_blk_seq = blk_seq; 00349 blk_count++; 00350 00351 if (max_lr_seq == claim_lr_seq && max_blk_seq == claim_blk_seq) 00352 break; 00353 00354 error = zil_read_log_block(zilog, &blk, &next_blk, lrbuf, &end); 00355 if (error) 00356 break; 00357 00358 for (lrp = lrbuf; lrp < end; lrp += reclen) { 00359 lr_t *lr = (lr_t *)lrp; 00360 reclen = lr->lrc_reclen; 00361 ASSERT3U(reclen, >=, sizeof (lr_t)); 00362 if (lr->lrc_seq > claim_lr_seq) 00363 goto done; 00364 if ((error = parse_lr_func(zilog, lr, arg, txg)) != 0) 00365 goto done; 00366 ASSERT3U(max_lr_seq, <, lr->lrc_seq); 00367 max_lr_seq = lr->lrc_seq; 00368 lr_count++; 00369 } 00370 } 00371 done: 00372 zilog->zl_parse_error = error; 00373 zilog->zl_parse_blk_seq = max_blk_seq; 00374 zilog->zl_parse_lr_seq = max_lr_seq; 00375 zilog->zl_parse_blk_count = blk_count; 00376 zilog->zl_parse_lr_count = lr_count; 00377 00378 ASSERT(!claimed || !(zh->zh_flags & ZIL_CLAIM_LR_SEQ_VALID) || 00379 (max_blk_seq == claim_blk_seq && max_lr_seq == claim_lr_seq)); 00380 00381 zil_bp_tree_fini(zilog); 00382 zio_buf_free(lrbuf, SPA_MAXBLOCKSIZE); 00383 00384 return (error); 00385 } 00386 00387 static int 00388 zil_claim_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t first_txg) 00389 { 00390 /* 00391 * Claim log block if not already committed and not already claimed. 00392 * If tx == NULL, just verify that the block is claimable. 00393 */ 00394 if (bp->blk_birth < first_txg || zil_bp_tree_add(zilog, bp) != 0) 00395 return (0); 00396 00397 return (zio_wait(zio_claim(NULL, zilog->zl_spa, 00398 tx == NULL ? 0 : first_txg, bp, spa_claim_notify, NULL, 00399 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SCRUB))); 00400 } 00401 00402 static int 00403 zil_claim_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t first_txg) 00404 { 00405 lr_write_t *lr = (lr_write_t *)lrc; 00406 int error; 00407 00408 if (lrc->lrc_txtype != TX_WRITE) 00409 return (0); 00410 00411 /* 00412 * If the block is not readable, don't claim it. This can happen 00413 * in normal operation when a log block is written to disk before 00414 * some of the dmu_sync() blocks it points to. In this case, the 00415 * transaction cannot have been committed to anyone (we would have 00416 * waited for all writes to be stable first), so it is semantically 00417 * correct to declare this the end of the log. 00418 */ 00419 if (lr->lr_blkptr.blk_birth >= first_txg && 00420 (error = zil_read_log_data(zilog, lr, NULL)) != 0) 00421 return (error); 00422 return (zil_claim_log_block(zilog, &lr->lr_blkptr, tx, first_txg)); 00423 } 00424 00425 /* ARGSUSED */ 00426 static int 00427 zil_free_log_block(zilog_t *zilog, blkptr_t *bp, void *tx, uint64_t claim_txg) 00428 { 00429 zio_free_zil(zilog->zl_spa, dmu_tx_get_txg(tx), bp); 00430 00431 return (0); 00432 } 00433 00434 static int 00435 zil_free_log_record(zilog_t *zilog, lr_t *lrc, void *tx, uint64_t claim_txg) 00436 { 00437 lr_write_t *lr = (lr_write_t *)lrc; 00438 blkptr_t *bp = &lr->lr_blkptr; 00439 00440 /* 00441 * If we previously claimed it, we need to free it. 00442 */ 00443 if (claim_txg != 0 && lrc->lrc_txtype == TX_WRITE && 00444 bp->blk_birth >= claim_txg && zil_bp_tree_add(zilog, bp) == 0) 00445 zio_free(zilog->zl_spa, dmu_tx_get_txg(tx), bp); 00446 00447 return (0); 00448 } 00449 00450 static lwb_t * 00451 zil_alloc_lwb(zilog_t *zilog, blkptr_t *bp, uint64_t txg) 00452 { 00453 lwb_t *lwb; 00454 00455 lwb = kmem_cache_alloc(zil_lwb_cache, KM_SLEEP); 00456 lwb->lwb_zilog = zilog; 00457 lwb->lwb_blk = *bp; 00458 lwb->lwb_buf = zio_buf_alloc(BP_GET_LSIZE(bp)); 00459 lwb->lwb_max_txg = txg; 00460 lwb->lwb_zio = NULL; 00461 lwb->lwb_tx = NULL; 00462 if (BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_ZILOG2) { 00463 lwb->lwb_nused = sizeof (zil_chain_t); 00464 lwb->lwb_sz = BP_GET_LSIZE(bp); 00465 } else { 00466 lwb->lwb_nused = 0; 00467 lwb->lwb_sz = BP_GET_LSIZE(bp) - sizeof (zil_chain_t); 00468 } 00469 00470 mutex_enter(&zilog->zl_lock); 00471 list_insert_tail(&zilog->zl_lwb_list, lwb); 00472 mutex_exit(&zilog->zl_lock); 00473 00474 return (lwb); 00475 } 00476 00481 void 00482 zilog_dirty(zilog_t *zilog, uint64_t txg) 00483 { 00484 dsl_pool_t *dp = zilog->zl_dmu_pool; 00485 dsl_dataset_t *ds = dmu_objset_ds(zilog->zl_os); 00486 00487 if (dsl_dataset_is_snapshot(ds)) 00488 panic("dirtying snapshot!"); 00489 00490 if (txg_list_add(&dp->dp_dirty_zilogs, zilog, txg) == 0) { 00491 /* up the hold count until we can be written out */ 00492 dmu_buf_add_ref(ds->ds_dbuf, zilog); 00493 } 00494 } 00495 00496 boolean_t 00497 zilog_is_dirty(zilog_t *zilog) 00498 { 00499 dsl_pool_t *dp = zilog->zl_dmu_pool; 00500 00501 for (int t = 0; t < TXG_SIZE; t++) { 00502 if (txg_list_member(&dp->dp_dirty_zilogs, zilog, t)) 00503 return (B_TRUE); 00504 } 00505 return (B_FALSE); 00506 } 00507 00511 static lwb_t * 00512 zil_create(zilog_t *zilog) 00513 { 00514 const zil_header_t *zh = zilog->zl_header; 00515 lwb_t *lwb = NULL; 00516 uint64_t txg = 0; 00517 dmu_tx_t *tx = NULL; 00518 blkptr_t blk; 00519 int error = 0; 00520 00521 /* 00522 * Wait for any previous destroy to complete. 00523 */ 00524 txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 00525 00526 ASSERT(zh->zh_claim_txg == 0); 00527 ASSERT(zh->zh_replay_seq == 0); 00528 00529 blk = zh->zh_log; 00530 00531 /* 00532 * Allocate an initial log block if: 00533 * - there isn't one already 00534 * - the existing block is the wrong endianess 00535 */ 00536 if (BP_IS_HOLE(&blk) || BP_SHOULD_BYTESWAP(&blk)) { 00537 tx = dmu_tx_create(zilog->zl_os); 00538 VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0); 00539 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 00540 txg = dmu_tx_get_txg(tx); 00541 00542 if (!BP_IS_HOLE(&blk)) { 00543 zio_free_zil(zilog->zl_spa, txg, &blk); 00544 BP_ZERO(&blk); 00545 } 00546 00547 error = zio_alloc_zil(zilog->zl_spa, txg, &blk, NULL, 00548 ZIL_MIN_BLKSZ, zilog->zl_logbias == ZFS_LOGBIAS_LATENCY); 00549 00550 if (error == 0) 00551 zil_init_log_chain(zilog, &blk); 00552 } 00553 00554 /* 00555 * Allocate a log write buffer (lwb) for the first log block. 00556 */ 00557 if (error == 0) 00558 lwb = zil_alloc_lwb(zilog, &blk, txg); 00559 00560 /* 00561 * If we just allocated the first log block, commit our transaction 00562 * and wait for zil_sync() to stuff the block poiner into zh_log. 00563 * (zh is part of the MOS, so we cannot modify it in open context.) 00564 */ 00565 if (tx != NULL) { 00566 dmu_tx_commit(tx); 00567 txg_wait_synced(zilog->zl_dmu_pool, txg); 00568 } 00569 00570 ASSERT(bcmp(&blk, &zh->zh_log, sizeof (blk)) == 0); 00571 00572 return (lwb); 00573 } 00574 00584 void 00585 zil_destroy(zilog_t *zilog, boolean_t keep_first) 00586 { 00587 const zil_header_t *zh = zilog->zl_header; 00588 lwb_t *lwb; 00589 dmu_tx_t *tx; 00590 uint64_t txg; 00591 00592 /* 00593 * Wait for any previous destroy to complete. 00594 */ 00595 txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 00596 00597 zilog->zl_old_header = *zh; /* debugging aid */ 00598 00599 if (BP_IS_HOLE(&zh->zh_log)) 00600 return; 00601 00602 tx = dmu_tx_create(zilog->zl_os); 00603 VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0); 00604 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 00605 txg = dmu_tx_get_txg(tx); 00606 00607 mutex_enter(&zilog->zl_lock); 00608 00609 ASSERT3U(zilog->zl_destroy_txg, <, txg); 00610 zilog->zl_destroy_txg = txg; 00611 zilog->zl_keep_first = keep_first; 00612 00613 if (!list_is_empty(&zilog->zl_lwb_list)) { 00614 ASSERT(zh->zh_claim_txg == 0); 00615 VERIFY(!keep_first); 00616 while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 00617 list_remove(&zilog->zl_lwb_list, lwb); 00618 if (lwb->lwb_buf != NULL) 00619 zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 00620 zio_free_zil(zilog->zl_spa, txg, &lwb->lwb_blk); 00621 kmem_cache_free(zil_lwb_cache, lwb); 00622 } 00623 } else if (!keep_first) { 00624 zil_destroy_sync(zilog, tx); 00625 } 00626 mutex_exit(&zilog->zl_lock); 00627 00628 dmu_tx_commit(tx); 00629 } 00630 00631 void 00632 zil_destroy_sync(zilog_t *zilog, dmu_tx_t *tx) 00633 { 00634 ASSERT(list_is_empty(&zilog->zl_lwb_list)); 00635 (void) zil_parse(zilog, zil_free_log_block, 00636 zil_free_log_record, tx, zilog->zl_header->zh_claim_txg); 00637 } 00638 00639 int 00640 zil_claim(const char *osname, void *txarg) 00641 { 00642 dmu_tx_t *tx = txarg; 00643 uint64_t first_txg = dmu_tx_get_txg(tx); 00644 zilog_t *zilog; 00645 zil_header_t *zh; 00646 objset_t *os; 00647 int error; 00648 00649 error = dmu_objset_hold(osname, FTAG, &os); 00650 if (error) { 00651 cmn_err(CE_WARN, "can't open objset for %s", osname); 00652 return (0); 00653 } 00654 00655 zilog = dmu_objset_zil(os); 00656 zh = zil_header_in_syncing_context(zilog); 00657 00658 if (spa_get_log_state(zilog->zl_spa) == SPA_LOG_CLEAR) { 00659 if (!BP_IS_HOLE(&zh->zh_log)) 00660 zio_free_zil(zilog->zl_spa, first_txg, &zh->zh_log); 00661 BP_ZERO(&zh->zh_log); 00662 dsl_dataset_dirty(dmu_objset_ds(os), tx); 00663 dmu_objset_rele(os, FTAG); 00664 return (0); 00665 } 00666 00667 /* 00668 * Claim all log blocks if we haven't already done so, and remember 00669 * the highest claimed sequence number. This ensures that if we can 00670 * read only part of the log now (e.g. due to a missing device), 00671 * but we can read the entire log later, we will not try to replay 00672 * or destroy beyond the last block we successfully claimed. 00673 */ 00674 ASSERT3U(zh->zh_claim_txg, <=, first_txg); 00675 if (zh->zh_claim_txg == 0 && !BP_IS_HOLE(&zh->zh_log)) { 00676 (void) zil_parse(zilog, zil_claim_log_block, 00677 zil_claim_log_record, tx, first_txg); 00678 zh->zh_claim_txg = first_txg; 00679 zh->zh_claim_blk_seq = zilog->zl_parse_blk_seq; 00680 zh->zh_claim_lr_seq = zilog->zl_parse_lr_seq; 00681 if (zilog->zl_parse_lr_count || zilog->zl_parse_blk_count > 1) 00682 zh->zh_flags |= ZIL_REPLAY_NEEDED; 00683 zh->zh_flags |= ZIL_CLAIM_LR_SEQ_VALID; 00684 dsl_dataset_dirty(dmu_objset_ds(os), tx); 00685 } 00686 00687 ASSERT3U(first_txg, ==, (spa_last_synced_txg(zilog->zl_spa) + 1)); 00688 dmu_objset_rele(os, FTAG); 00689 return (0); 00690 } 00691 00697 int 00698 zil_check_log_chain(const char *osname, void *tx) 00699 { 00700 zilog_t *zilog; 00701 objset_t *os; 00702 blkptr_t *bp; 00703 int error; 00704 00705 ASSERT(tx == NULL); 00706 00707 error = dmu_objset_hold(osname, FTAG, &os); 00708 if (error) { 00709 cmn_err(CE_WARN, "can't open objset for %s", osname); 00710 return (0); 00711 } 00712 00713 zilog = dmu_objset_zil(os); 00714 bp = (blkptr_t *)&zilog->zl_header->zh_log; 00715 00716 /* 00717 * Check the first block and determine if it's on a log device 00718 * which may have been removed or faulted prior to loading this 00719 * pool. If so, there's no point in checking the rest of the log 00720 * as its content should have already been synced to the pool. 00721 */ 00722 if (!BP_IS_HOLE(bp)) { 00723 vdev_t *vd; 00724 boolean_t valid = B_TRUE; 00725 00726 spa_config_enter(os->os_spa, SCL_STATE, FTAG, RW_READER); 00727 vd = vdev_lookup_top(os->os_spa, DVA_GET_VDEV(&bp->blk_dva[0])); 00728 if (vd->vdev_islog && vdev_is_dead(vd)) 00729 valid = vdev_log_state_valid(vd); 00730 spa_config_exit(os->os_spa, SCL_STATE, FTAG); 00731 00732 if (!valid) { 00733 dmu_objset_rele(os, FTAG); 00734 return (0); 00735 } 00736 } 00737 00738 /* 00739 * Because tx == NULL, zil_claim_log_block() will not actually claim 00740 * any blocks, but just determine whether it is possible to do so. 00741 * In addition to checking the log chain, zil_claim_log_block() 00742 * will invoke zio_claim() with a done func of spa_claim_notify(), 00743 * which will update spa_max_claim_txg. See spa_load() for details. 00744 */ 00745 error = zil_parse(zilog, zil_claim_log_block, zil_claim_log_record, tx, 00746 zilog->zl_header->zh_claim_txg ? -1ULL : spa_first_txg(os->os_spa)); 00747 00748 dmu_objset_rele(os, FTAG); 00749 00750 return ((error == ECKSUM || error == ENOENT) ? 0 : error); 00751 } 00752 00753 static int 00754 zil_vdev_compare(const void *x1, const void *x2) 00755 { 00756 const uint64_t v1 = ((zil_vdev_node_t *)x1)->zv_vdev; 00757 const uint64_t v2 = ((zil_vdev_node_t *)x2)->zv_vdev; 00758 00759 if (v1 < v2) 00760 return (-1); 00761 if (v1 > v2) 00762 return (1); 00763 00764 return (0); 00765 } 00766 00767 void 00768 zil_add_block(zilog_t *zilog, const blkptr_t *bp) 00769 { 00770 avl_tree_t *t = &zilog->zl_vdev_tree; 00771 avl_index_t where; 00772 zil_vdev_node_t *zv, zvsearch; 00773 int ndvas = BP_GET_NDVAS(bp); 00774 int i; 00775 00776 if (zfs_nocacheflush) 00777 return; 00778 00779 ASSERT(zilog->zl_writer); 00780 00781 /* 00782 * Even though we're zl_writer, we still need a lock because the 00783 * zl_get_data() callbacks may have dmu_sync() done callbacks 00784 * that will run concurrently. 00785 */ 00786 mutex_enter(&zilog->zl_vdev_lock); 00787 for (i = 0; i < ndvas; i++) { 00788 zvsearch.zv_vdev = DVA_GET_VDEV(&bp->blk_dva[i]); 00789 if (avl_find(t, &zvsearch, &where) == NULL) { 00790 zv = kmem_alloc(sizeof (*zv), KM_SLEEP); 00791 zv->zv_vdev = zvsearch.zv_vdev; 00792 avl_insert(t, zv, where); 00793 } 00794 } 00795 mutex_exit(&zilog->zl_vdev_lock); 00796 } 00797 00798 static void 00799 zil_flush_vdevs(zilog_t *zilog) 00800 { 00801 spa_t *spa = zilog->zl_spa; 00802 avl_tree_t *t = &zilog->zl_vdev_tree; 00803 void *cookie = NULL; 00804 zil_vdev_node_t *zv; 00805 zio_t *zio; 00806 00807 ASSERT(zilog->zl_writer); 00808 00809 /* 00810 * We don't need zl_vdev_lock here because we're the zl_writer, 00811 * and all zl_get_data() callbacks are done. 00812 */ 00813 if (avl_numnodes(t) == 0) 00814 return; 00815 00816 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 00817 00818 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); 00819 00820 while ((zv = avl_destroy_nodes(t, &cookie)) != NULL) { 00821 vdev_t *vd = vdev_lookup_top(spa, zv->zv_vdev); 00822 if (vd != NULL) 00823 zio_flush(zio, vd); 00824 kmem_free(zv, sizeof (*zv)); 00825 } 00826 00827 /* 00828 * Wait for all the flushes to complete. Not all devices actually 00829 * support the DKIOCFLUSHWRITECACHE ioctl, so it's OK if it fails. 00830 */ 00831 (void) zio_wait(zio); 00832 00833 spa_config_exit(spa, SCL_STATE, FTAG); 00834 } 00835 00839 static void 00840 zil_lwb_write_done(zio_t *zio) 00841 { 00842 lwb_t *lwb = zio->io_private; 00843 zilog_t *zilog = lwb->lwb_zilog; 00844 dmu_tx_t *tx = lwb->lwb_tx; 00845 00846 ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF); 00847 ASSERT(BP_GET_TYPE(zio->io_bp) == DMU_OT_INTENT_LOG); 00848 ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 00849 ASSERT(BP_GET_BYTEORDER(zio->io_bp) == ZFS_HOST_BYTEORDER); 00850 ASSERT(!BP_IS_GANG(zio->io_bp)); 00851 ASSERT(!BP_IS_HOLE(zio->io_bp)); 00852 ASSERT(zio->io_bp->blk_fill == 0); 00853 00854 /* 00855 * Ensure the lwb buffer pointer is cleared before releasing 00856 * the txg. If we have had an allocation failure and 00857 * the txg is waiting to sync then we want want zil_sync() 00858 * to remove the lwb so that it's not picked up as the next new 00859 * one in zil_commit_writer(). zil_sync() will only remove 00860 * the lwb if lwb_buf is null. 00861 */ 00862 zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 00863 mutex_enter(&zilog->zl_lock); 00864 lwb->lwb_buf = NULL; 00865 lwb->lwb_tx = NULL; 00866 mutex_exit(&zilog->zl_lock); 00867 00868 /* 00869 * Now that we've written this log block, we have a stable pointer 00870 * to the next block in the chain, so it's OK to let the txg in 00871 * which we allocated the next block sync. 00872 */ 00873 dmu_tx_commit(tx); 00874 } 00875 00879 static void 00880 zil_lwb_write_init(zilog_t *zilog, lwb_t *lwb) 00881 { 00882 zbookmark_t zb; 00883 00884 SET_BOOKMARK(&zb, lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_OBJSET], 00885 ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, 00886 lwb->lwb_blk.blk_cksum.zc_word[ZIL_ZC_SEQ]); 00887 00888 if (zilog->zl_root_zio == NULL) { 00889 zilog->zl_root_zio = zio_root(zilog->zl_spa, NULL, NULL, 00890 ZIO_FLAG_CANFAIL); 00891 } 00892 if (lwb->lwb_zio == NULL) { 00893 lwb->lwb_zio = zio_rewrite(zilog->zl_root_zio, zilog->zl_spa, 00894 0, &lwb->lwb_blk, lwb->lwb_buf, BP_GET_LSIZE(&lwb->lwb_blk), 00895 zil_lwb_write_done, lwb, ZIO_PRIORITY_LOG_WRITE, 00896 ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE, &zb); 00897 } 00898 } 00899 00907 uint64_t zil_block_buckets[] = { 00908 4096, /* non TX_WRITE */ 00909 8192+4096, /* data base */ 00910 32*1024 + 4096, /* NFS writes */ 00911 UINT64_MAX 00912 }; 00913 00919 uint64_t zil_slog_limit = 1024 * 1024; 00920 #define USE_SLOG(zilog) (((zilog)->zl_logbias == ZFS_LOGBIAS_LATENCY) && \ 00921 (((zilog)->zl_cur_used < zil_slog_limit) || \ 00922 ((zilog)->zl_itx_list_sz < (zil_slog_limit << 1)))) 00923 00928 static lwb_t * 00929 zil_lwb_write_start(zilog_t *zilog, lwb_t *lwb) 00930 { 00931 lwb_t *nlwb = NULL; 00932 zil_chain_t *zilc; 00933 spa_t *spa = zilog->zl_spa; 00934 blkptr_t *bp; 00935 dmu_tx_t *tx; 00936 uint64_t txg; 00937 uint64_t zil_blksz, wsz; 00938 int i, error; 00939 00940 if (BP_GET_CHECKSUM(&lwb->lwb_blk) == ZIO_CHECKSUM_ZILOG2) { 00941 zilc = (zil_chain_t *)lwb->lwb_buf; 00942 bp = &zilc->zc_next_blk; 00943 } else { 00944 zilc = (zil_chain_t *)(lwb->lwb_buf + lwb->lwb_sz); 00945 bp = &zilc->zc_next_blk; 00946 } 00947 00948 ASSERT(lwb->lwb_nused <= lwb->lwb_sz); 00949 00950 /* 00951 * Allocate the next block and save its address in this block 00952 * before writing it in order to establish the log chain. 00953 * Note that if the allocation of nlwb synced before we wrote 00954 * the block that points at it (lwb), we'd leak it if we crashed. 00955 * Therefore, we don't do dmu_tx_commit() until zil_lwb_write_done(). 00956 * We dirty the dataset to ensure that zil_sync() will be called 00957 * to clean up in the event of allocation failure or I/O failure. 00958 */ 00959 tx = dmu_tx_create(zilog->zl_os); 00960 VERIFY(dmu_tx_assign(tx, TXG_WAIT) == 0); 00961 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 00962 txg = dmu_tx_get_txg(tx); 00963 00964 lwb->lwb_tx = tx; 00965 00966 /* 00967 * Log blocks are pre-allocated. Here we select the size of the next 00968 * block, based on size used in the last block. 00969 * - first find the smallest bucket that will fit the block from a 00970 * limited set of block sizes. This is because it's faster to write 00971 * blocks allocated from the same metaslab as they are adjacent or 00972 * close. 00973 * - next find the maximum from the new suggested size and an array of 00974 * previous sizes. This lessens a picket fence effect of wrongly 00975 * guesssing the size if we have a stream of say 2k, 64k, 2k, 64k 00976 * requests. 00977 * 00978 * Note we only write what is used, but we can't just allocate 00979 * the maximum block size because we can exhaust the available 00980 * pool log space. 00981 */ 00982 zil_blksz = zilog->zl_cur_used + sizeof (zil_chain_t); 00983 for (i = 0; zil_blksz > zil_block_buckets[i]; i++) 00984 continue; 00985 zil_blksz = zil_block_buckets[i]; 00986 if (zil_blksz == UINT64_MAX) 00987 zil_blksz = SPA_MAXBLOCKSIZE; 00988 zilog->zl_prev_blks[zilog->zl_prev_rotor] = zil_blksz; 00989 for (i = 0; i < ZIL_PREV_BLKS; i++) 00990 zil_blksz = MAX(zil_blksz, zilog->zl_prev_blks[i]); 00991 zilog->zl_prev_rotor = (zilog->zl_prev_rotor + 1) & (ZIL_PREV_BLKS - 1); 00992 00993 BP_ZERO(bp); 00994 /* pass the old blkptr in order to spread log blocks across devs */ 00995 error = zio_alloc_zil(spa, txg, bp, &lwb->lwb_blk, zil_blksz, 00996 USE_SLOG(zilog)); 00997 if (!error) { 00998 ASSERT3U(bp->blk_birth, ==, txg); 00999 bp->blk_cksum = lwb->lwb_blk.blk_cksum; 01000 bp->blk_cksum.zc_word[ZIL_ZC_SEQ]++; 01001 01002 /* 01003 * Allocate a new log write buffer (lwb). 01004 */ 01005 nlwb = zil_alloc_lwb(zilog, bp, txg); 01006 01007 /* Record the block for later vdev flushing */ 01008 zil_add_block(zilog, &lwb->lwb_blk); 01009 } 01010 01011 if (BP_GET_CHECKSUM(&lwb->lwb_blk) == ZIO_CHECKSUM_ZILOG2) { 01012 /* For Slim ZIL only write what is used. */ 01013 wsz = P2ROUNDUP_TYPED(lwb->lwb_nused, ZIL_MIN_BLKSZ, uint64_t); 01014 ASSERT3U(wsz, <=, lwb->lwb_sz); 01015 zio_shrink(lwb->lwb_zio, wsz); 01016 01017 } else { 01018 wsz = lwb->lwb_sz; 01019 } 01020 01021 zilc->zc_pad = 0; 01022 zilc->zc_nused = lwb->lwb_nused; 01023 zilc->zc_eck.zec_cksum = lwb->lwb_blk.blk_cksum; 01024 01025 /* 01026 * clear unused data for security 01027 */ 01028 bzero(lwb->lwb_buf + lwb->lwb_nused, wsz - lwb->lwb_nused); 01029 01030 zio_nowait(lwb->lwb_zio); /* Kick off the write for the old log block */ 01031 01032 /* 01033 * If there was an allocation failure then nlwb will be null which 01034 * forces a txg_wait_synced(). 01035 */ 01036 return (nlwb); 01037 } 01038 01039 static lwb_t * 01040 zil_lwb_commit(zilog_t *zilog, itx_t *itx, lwb_t *lwb) 01041 { 01042 lr_t *lrc = &itx->itx_lr; /* common log record */ 01043 lr_write_t *lrw = (lr_write_t *)lrc; 01044 char *lr_buf; 01045 uint64_t txg = lrc->lrc_txg; 01046 uint64_t reclen = lrc->lrc_reclen; 01047 uint64_t dlen = 0; 01048 01049 if (lwb == NULL) 01050 return (NULL); 01051 01052 ASSERT(lwb->lwb_buf != NULL); 01053 ASSERT(zilog_is_dirty(zilog) || 01054 spa_freeze_txg(zilog->zl_spa) != UINT64_MAX); 01055 01056 if (lrc->lrc_txtype == TX_WRITE && itx->itx_wr_state == WR_NEED_COPY) 01057 dlen = P2ROUNDUP_TYPED( 01058 lrw->lr_length, sizeof (uint64_t), uint64_t); 01059 01060 zilog->zl_cur_used += (reclen + dlen); 01061 01062 zil_lwb_write_init(zilog, lwb); 01063 01064 /* 01065 * If this record won't fit in the current log block, start a new one. 01066 */ 01067 if (lwb->lwb_nused + reclen + dlen > lwb->lwb_sz) { 01068 lwb = zil_lwb_write_start(zilog, lwb); 01069 if (lwb == NULL) 01070 return (NULL); 01071 zil_lwb_write_init(zilog, lwb); 01072 ASSERT(LWB_EMPTY(lwb)); 01073 if (lwb->lwb_nused + reclen + dlen > lwb->lwb_sz) { 01074 txg_wait_synced(zilog->zl_dmu_pool, txg); 01075 return (lwb); 01076 } 01077 } 01078 01079 lr_buf = lwb->lwb_buf + lwb->lwb_nused; 01080 bcopy(lrc, lr_buf, reclen); 01081 lrc = (lr_t *)lr_buf; 01082 lrw = (lr_write_t *)lrc; 01083 01084 /* 01085 * If it's a write, fetch the data or get its blkptr as appropriate. 01086 */ 01087 if (lrc->lrc_txtype == TX_WRITE) { 01088 if (txg > spa_freeze_txg(zilog->zl_spa)) 01089 txg_wait_synced(zilog->zl_dmu_pool, txg); 01090 if (itx->itx_wr_state != WR_COPIED) { 01091 char *dbuf; 01092 int error; 01093 01094 if (dlen) { 01095 ASSERT(itx->itx_wr_state == WR_NEED_COPY); 01096 dbuf = lr_buf + reclen; 01097 lrw->lr_common.lrc_reclen += dlen; 01098 } else { 01099 ASSERT(itx->itx_wr_state == WR_INDIRECT); 01100 dbuf = NULL; 01101 } 01102 error = zilog->zl_get_data( 01103 itx->itx_private, lrw, dbuf, lwb->lwb_zio); 01104 if (error == EIO) { 01105 txg_wait_synced(zilog->zl_dmu_pool, txg); 01106 return (lwb); 01107 } 01108 if (error) { 01109 ASSERT(error == ENOENT || error == EEXIST || 01110 error == EALREADY); 01111 return (lwb); 01112 } 01113 } 01114 } 01115 01116 /* 01117 * We're actually making an entry, so update lrc_seq to be the 01118 * log record sequence number. Note that this is generally not 01119 * equal to the itx sequence number because not all transactions 01120 * are synchronous, and sometimes spa_sync() gets there first. 01121 */ 01122 lrc->lrc_seq = ++zilog->zl_lr_seq; /* we are single threaded */ 01123 lwb->lwb_nused += reclen + dlen; 01124 lwb->lwb_max_txg = MAX(lwb->lwb_max_txg, txg); 01125 ASSERT3U(lwb->lwb_nused, <=, lwb->lwb_sz); 01126 ASSERT0(P2PHASE(lwb->lwb_nused, sizeof (uint64_t))); 01127 01128 return (lwb); 01129 } 01130 01131 itx_t * 01132 zil_itx_create(uint64_t txtype, size_t lrsize) 01133 { 01134 itx_t *itx; 01135 01136 lrsize = P2ROUNDUP_TYPED(lrsize, sizeof (uint64_t), size_t); 01137 01138 itx = kmem_alloc(offsetof(itx_t, itx_lr) + lrsize, KM_SLEEP); 01139 itx->itx_lr.lrc_txtype = txtype; 01140 itx->itx_lr.lrc_reclen = lrsize; 01141 itx->itx_sod = lrsize; /* if write & WR_NEED_COPY will be increased */ 01142 itx->itx_lr.lrc_seq = 0; /* defensive */ 01143 itx->itx_sync = B_TRUE; /* default is synchronous */ 01144 01145 return (itx); 01146 } 01147 01148 void 01149 zil_itx_destroy(itx_t *itx) 01150 { 01151 kmem_free(itx, offsetof(itx_t, itx_lr) + itx->itx_lr.lrc_reclen); 01152 } 01153 01158 static void 01159 zil_itxg_clean(itxs_t *itxs) 01160 { 01161 itx_t *itx; 01162 list_t *list; 01163 avl_tree_t *t; 01164 void *cookie; 01165 itx_async_node_t *ian; 01166 01167 list = &itxs->i_sync_list; 01168 while ((itx = list_head(list)) != NULL) { 01169 list_remove(list, itx); 01170 kmem_free(itx, offsetof(itx_t, itx_lr) + 01171 itx->itx_lr.lrc_reclen); 01172 } 01173 01174 cookie = NULL; 01175 t = &itxs->i_async_tree; 01176 while ((ian = avl_destroy_nodes(t, &cookie)) != NULL) { 01177 list = &ian->ia_list; 01178 while ((itx = list_head(list)) != NULL) { 01179 list_remove(list, itx); 01180 kmem_free(itx, offsetof(itx_t, itx_lr) + 01181 itx->itx_lr.lrc_reclen); 01182 } 01183 list_destroy(list); 01184 kmem_free(ian, sizeof (itx_async_node_t)); 01185 } 01186 avl_destroy(t); 01187 01188 kmem_free(itxs, sizeof (itxs_t)); 01189 } 01190 01191 static int 01192 zil_aitx_compare(const void *x1, const void *x2) 01193 { 01194 const uint64_t o1 = ((itx_async_node_t *)x1)->ia_foid; 01195 const uint64_t o2 = ((itx_async_node_t *)x2)->ia_foid; 01196 01197 if (o1 < o2) 01198 return (-1); 01199 if (o1 > o2) 01200 return (1); 01201 01202 return (0); 01203 } 01204 01208 static void 01209 zil_remove_async(zilog_t *zilog, uint64_t oid) 01210 { 01211 uint64_t otxg, txg; 01212 itx_async_node_t *ian; 01213 avl_tree_t *t; 01214 avl_index_t where; 01215 list_t clean_list; 01216 itx_t *itx; 01217 01218 ASSERT(oid != 0); 01219 list_create(&clean_list, sizeof (itx_t), offsetof(itx_t, itx_node)); 01220 01221 if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */ 01222 otxg = ZILTEST_TXG; 01223 else 01224 otxg = spa_last_synced_txg(zilog->zl_spa) + 1; 01225 01226 for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) { 01227 itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK]; 01228 01229 mutex_enter(&itxg->itxg_lock); 01230 if (itxg->itxg_txg != txg) { 01231 mutex_exit(&itxg->itxg_lock); 01232 continue; 01233 } 01234 01235 /* 01236 * Locate the object node and append its list. 01237 */ 01238 t = &itxg->itxg_itxs->i_async_tree; 01239 ian = avl_find(t, &oid, &where); 01240 if (ian != NULL) 01241 list_move_tail(&clean_list, &ian->ia_list); 01242 mutex_exit(&itxg->itxg_lock); 01243 } 01244 while ((itx = list_head(&clean_list)) != NULL) { 01245 list_remove(&clean_list, itx); 01246 kmem_free(itx, offsetof(itx_t, itx_lr) + 01247 itx->itx_lr.lrc_reclen); 01248 } 01249 list_destroy(&clean_list); 01250 } 01251 01252 void 01253 zil_itx_assign(zilog_t *zilog, itx_t *itx, dmu_tx_t *tx) 01254 { 01255 uint64_t txg; 01256 itxg_t *itxg; 01257 itxs_t *itxs, *clean = NULL; 01258 01259 /* 01260 * Object ids can be re-instantiated in the next txg so 01261 * remove any async transactions to avoid future leaks. 01262 * This can happen if a fsync occurs on the re-instantiated 01263 * object for a WR_INDIRECT or WR_NEED_COPY write, which gets 01264 * the new file data and flushes a write record for the old object. 01265 */ 01266 if ((itx->itx_lr.lrc_txtype & ~TX_CI) == TX_REMOVE) 01267 zil_remove_async(zilog, itx->itx_oid); 01268 01269 /* 01270 * Ensure the data of a renamed file is committed before the rename. 01271 */ 01272 if ((itx->itx_lr.lrc_txtype & ~TX_CI) == TX_RENAME) 01273 zil_async_to_sync(zilog, itx->itx_oid); 01274 01275 if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) 01276 txg = ZILTEST_TXG; 01277 else 01278 txg = dmu_tx_get_txg(tx); 01279 01280 itxg = &zilog->zl_itxg[txg & TXG_MASK]; 01281 mutex_enter(&itxg->itxg_lock); 01282 itxs = itxg->itxg_itxs; 01283 if (itxg->itxg_txg != txg) { 01284 if (itxs != NULL) { 01285 /* 01286 * The zil_clean callback hasn't got around to cleaning 01287 * this itxg. Save the itxs for release below. 01288 * This should be rare. 01289 */ 01290 atomic_add_64(&zilog->zl_itx_list_sz, -itxg->itxg_sod); 01291 itxg->itxg_sod = 0; 01292 clean = itxg->itxg_itxs; 01293 } 01294 ASSERT(itxg->itxg_sod == 0); 01295 itxg->itxg_txg = txg; 01296 itxs = itxg->itxg_itxs = kmem_zalloc(sizeof (itxs_t), KM_SLEEP); 01297 01298 list_create(&itxs->i_sync_list, sizeof (itx_t), 01299 offsetof(itx_t, itx_node)); 01300 avl_create(&itxs->i_async_tree, zil_aitx_compare, 01301 sizeof (itx_async_node_t), 01302 offsetof(itx_async_node_t, ia_node)); 01303 } 01304 if (itx->itx_sync) { 01305 list_insert_tail(&itxs->i_sync_list, itx); 01306 atomic_add_64(&zilog->zl_itx_list_sz, itx->itx_sod); 01307 itxg->itxg_sod += itx->itx_sod; 01308 } else { 01309 avl_tree_t *t = &itxs->i_async_tree; 01310 uint64_t foid = ((lr_ooo_t *)&itx->itx_lr)->lr_foid; 01311 itx_async_node_t *ian; 01312 avl_index_t where; 01313 01314 ian = avl_find(t, &foid, &where); 01315 if (ian == NULL) { 01316 ian = kmem_alloc(sizeof (itx_async_node_t), KM_SLEEP); 01317 list_create(&ian->ia_list, sizeof (itx_t), 01318 offsetof(itx_t, itx_node)); 01319 ian->ia_foid = foid; 01320 avl_insert(t, ian, where); 01321 } 01322 list_insert_tail(&ian->ia_list, itx); 01323 } 01324 01325 itx->itx_lr.lrc_txg = dmu_tx_get_txg(tx); 01326 zilog_dirty(zilog, txg); 01327 mutex_exit(&itxg->itxg_lock); 01328 01329 /* Release the old itxs now we've dropped the lock */ 01330 if (clean != NULL) 01331 zil_itxg_clean(clean); 01332 } 01333 01341 void 01342 zil_clean(zilog_t *zilog, uint64_t synced_txg) 01343 { 01344 itxg_t *itxg = &zilog->zl_itxg[synced_txg & TXG_MASK]; 01345 itxs_t *clean_me; 01346 01347 mutex_enter(&itxg->itxg_lock); 01348 if (itxg->itxg_itxs == NULL || itxg->itxg_txg == ZILTEST_TXG) { 01349 mutex_exit(&itxg->itxg_lock); 01350 return; 01351 } 01352 ASSERT3U(itxg->itxg_txg, <=, synced_txg); 01353 ASSERT(itxg->itxg_txg != 0); 01354 ASSERT(zilog->zl_clean_taskq != NULL); 01355 atomic_add_64(&zilog->zl_itx_list_sz, -itxg->itxg_sod); 01356 itxg->itxg_sod = 0; 01357 clean_me = itxg->itxg_itxs; 01358 itxg->itxg_itxs = NULL; 01359 itxg->itxg_txg = 0; 01360 mutex_exit(&itxg->itxg_lock); 01361 /* 01362 * Preferably start a task queue to free up the old itxs but 01363 * if taskq_dispatch can't allocate resources to do that then 01364 * free it in-line. This should be rare. Note, using TQ_SLEEP 01365 * created a bad performance problem. 01366 */ 01367 if (taskq_dispatch(zilog->zl_clean_taskq, 01368 (void (*)(void *))zil_itxg_clean, clean_me, TQ_NOSLEEP) == 0) 01369 zil_itxg_clean(clean_me); 01370 } 01371 01375 static void 01376 zil_get_commit_list(zilog_t *zilog) 01377 { 01378 uint64_t otxg, txg; 01379 list_t *commit_list = &zilog->zl_itx_commit_list; 01380 uint64_t push_sod = 0; 01381 01382 if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */ 01383 otxg = ZILTEST_TXG; 01384 else 01385 otxg = spa_last_synced_txg(zilog->zl_spa) + 1; 01386 01387 for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) { 01388 itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK]; 01389 01390 mutex_enter(&itxg->itxg_lock); 01391 if (itxg->itxg_txg != txg) { 01392 mutex_exit(&itxg->itxg_lock); 01393 continue; 01394 } 01395 01396 list_move_tail(commit_list, &itxg->itxg_itxs->i_sync_list); 01397 push_sod += itxg->itxg_sod; 01398 itxg->itxg_sod = 0; 01399 01400 mutex_exit(&itxg->itxg_lock); 01401 } 01402 atomic_add_64(&zilog->zl_itx_list_sz, -push_sod); 01403 } 01404 01408 static void 01409 zil_async_to_sync(zilog_t *zilog, uint64_t foid) 01410 { 01411 uint64_t otxg, txg; 01412 itx_async_node_t *ian; 01413 avl_tree_t *t; 01414 avl_index_t where; 01415 01416 if (spa_freeze_txg(zilog->zl_spa) != UINT64_MAX) /* ziltest support */ 01417 otxg = ZILTEST_TXG; 01418 else 01419 otxg = spa_last_synced_txg(zilog->zl_spa) + 1; 01420 01421 for (txg = otxg; txg < (otxg + TXG_CONCURRENT_STATES); txg++) { 01422 itxg_t *itxg = &zilog->zl_itxg[txg & TXG_MASK]; 01423 01424 mutex_enter(&itxg->itxg_lock); 01425 if (itxg->itxg_txg != txg) { 01426 mutex_exit(&itxg->itxg_lock); 01427 continue; 01428 } 01429 01430 /* 01431 * If a foid is specified then find that node and append its 01432 * list. Otherwise walk the tree appending all the lists 01433 * to the sync list. We add to the end rather than the 01434 * beginning to ensure the create has happened. 01435 */ 01436 t = &itxg->itxg_itxs->i_async_tree; 01437 if (foid != 0) { 01438 ian = avl_find(t, &foid, &where); 01439 if (ian != NULL) { 01440 list_move_tail(&itxg->itxg_itxs->i_sync_list, 01441 &ian->ia_list); 01442 } 01443 } else { 01444 void *cookie = NULL; 01445 01446 while ((ian = avl_destroy_nodes(t, &cookie)) != NULL) { 01447 list_move_tail(&itxg->itxg_itxs->i_sync_list, 01448 &ian->ia_list); 01449 list_destroy(&ian->ia_list); 01450 kmem_free(ian, sizeof (itx_async_node_t)); 01451 } 01452 } 01453 mutex_exit(&itxg->itxg_lock); 01454 } 01455 } 01456 01457 static void 01458 zil_commit_writer(zilog_t *zilog) 01459 { 01460 uint64_t txg; 01461 itx_t *itx; 01462 lwb_t *lwb; 01463 spa_t *spa = zilog->zl_spa; 01464 int error = 0; 01465 01466 ASSERT(zilog->zl_root_zio == NULL); 01467 01468 mutex_exit(&zilog->zl_lock); 01469 01470 zil_get_commit_list(zilog); 01471 01472 /* 01473 * Return if there's nothing to commit before we dirty the fs by 01474 * calling zil_create(). 01475 */ 01476 if (list_head(&zilog->zl_itx_commit_list) == NULL) { 01477 mutex_enter(&zilog->zl_lock); 01478 return; 01479 } 01480 01481 if (zilog->zl_suspend) { 01482 lwb = NULL; 01483 } else { 01484 lwb = list_tail(&zilog->zl_lwb_list); 01485 if (lwb == NULL) 01486 lwb = zil_create(zilog); 01487 } 01488 01489 DTRACE_PROBE1(zil__cw1, zilog_t *, zilog); 01490 while (itx = list_head(&zilog->zl_itx_commit_list)) { 01491 txg = itx->itx_lr.lrc_txg; 01492 ASSERT(txg); 01493 01494 if (txg > spa_last_synced_txg(spa) || txg > spa_freeze_txg(spa)) 01495 lwb = zil_lwb_commit(zilog, itx, lwb); 01496 list_remove(&zilog->zl_itx_commit_list, itx); 01497 kmem_free(itx, offsetof(itx_t, itx_lr) 01498 + itx->itx_lr.lrc_reclen); 01499 } 01500 DTRACE_PROBE1(zil__cw2, zilog_t *, zilog); 01501 01502 /* write the last block out */ 01503 if (lwb != NULL && lwb->lwb_zio != NULL) 01504 lwb = zil_lwb_write_start(zilog, lwb); 01505 01506 zilog->zl_cur_used = 0; 01507 01508 /* 01509 * Wait if necessary for the log blocks to be on stable storage. 01510 */ 01511 if (zilog->zl_root_zio) { 01512 error = zio_wait(zilog->zl_root_zio); 01513 zilog->zl_root_zio = NULL; 01514 zil_flush_vdevs(zilog); 01515 } 01516 01517 if (error || lwb == NULL) 01518 txg_wait_synced(zilog->zl_dmu_pool, 0); 01519 01520 mutex_enter(&zilog->zl_lock); 01521 01522 /* 01523 * Remember the highest committed log sequence number for ztest. 01524 * We only update this value when all the log writes succeeded, 01525 * because ztest wants to ASSERT that it got the whole log chain. 01526 */ 01527 if (error == 0 && lwb != NULL) 01528 zilog->zl_commit_lr_seq = zilog->zl_lr_seq; 01529 } 01530 01556 void 01557 zil_commit(zilog_t *zilog, uint64_t foid) 01558 { 01559 uint64_t mybatch; 01560 01561 if (zilog->zl_sync == ZFS_SYNC_DISABLED) 01562 return; 01563 01564 /* move the async itxs for the foid to the sync queues */ 01565 zil_async_to_sync(zilog, foid); 01566 01567 mutex_enter(&zilog->zl_lock); 01568 mybatch = zilog->zl_next_batch; 01569 while (zilog->zl_writer) { 01570 cv_wait(&zilog->zl_cv_batch[mybatch & 1], &zilog->zl_lock); 01571 if (mybatch <= zilog->zl_com_batch) { 01572 mutex_exit(&zilog->zl_lock); 01573 return; 01574 } 01575 } 01576 01577 zilog->zl_next_batch++; 01578 zilog->zl_writer = B_TRUE; 01579 zil_commit_writer(zilog); 01580 zilog->zl_com_batch = mybatch; 01581 zilog->zl_writer = B_FALSE; 01582 mutex_exit(&zilog->zl_lock); 01583 01584 /* wake up one thread to become the next writer */ 01585 cv_signal(&zilog->zl_cv_batch[(mybatch+1) & 1]); 01586 01587 /* wake up all threads waiting for this batch to be committed */ 01588 cv_broadcast(&zilog->zl_cv_batch[mybatch & 1]); 01589 } 01590 01594 void 01595 zil_sync(zilog_t *zilog, dmu_tx_t *tx) 01596 { 01597 zil_header_t *zh = zil_header_in_syncing_context(zilog); 01598 uint64_t txg = dmu_tx_get_txg(tx); 01599 spa_t *spa = zilog->zl_spa; 01600 uint64_t *replayed_seq = &zilog->zl_replayed_seq[txg & TXG_MASK]; 01601 lwb_t *lwb; 01602 01603 /* 01604 * We don't zero out zl_destroy_txg, so make sure we don't try 01605 * to destroy it twice. 01606 */ 01607 if (spa_sync_pass(spa) != 1) 01608 return; 01609 01610 mutex_enter(&zilog->zl_lock); 01611 01612 ASSERT(zilog->zl_stop_sync == 0); 01613 01614 if (*replayed_seq != 0) { 01615 ASSERT(zh->zh_replay_seq < *replayed_seq); 01616 zh->zh_replay_seq = *replayed_seq; 01617 *replayed_seq = 0; 01618 } 01619 01620 if (zilog->zl_destroy_txg == txg) { 01621 blkptr_t blk = zh->zh_log; 01622 01623 ASSERT(list_head(&zilog->zl_lwb_list) == NULL); 01624 01625 bzero(zh, sizeof (zil_header_t)); 01626 bzero(zilog->zl_replayed_seq, sizeof (zilog->zl_replayed_seq)); 01627 01628 if (zilog->zl_keep_first) { 01629 /* 01630 * If this block was part of log chain that couldn't 01631 * be claimed because a device was missing during 01632 * zil_claim(), but that device later returns, 01633 * then this block could erroneously appear valid. 01634 * To guard against this, assign a new GUID to the new 01635 * log chain so it doesn't matter what blk points to. 01636 */ 01637 zil_init_log_chain(zilog, &blk); 01638 zh->zh_log = blk; 01639 } 01640 } 01641 01642 while ((lwb = list_head(&zilog->zl_lwb_list)) != NULL) { 01643 zh->zh_log = lwb->lwb_blk; 01644 if (lwb->lwb_buf != NULL || lwb->lwb_max_txg > txg) 01645 break; 01646 list_remove(&zilog->zl_lwb_list, lwb); 01647 zio_free_zil(spa, txg, &lwb->lwb_blk); 01648 kmem_cache_free(zil_lwb_cache, lwb); 01649 01650 /* 01651 * If we don't have anything left in the lwb list then 01652 * we've had an allocation failure and we need to zero 01653 * out the zil_header blkptr so that we don't end 01654 * up freeing the same block twice. 01655 */ 01656 if (list_head(&zilog->zl_lwb_list) == NULL) 01657 BP_ZERO(&zh->zh_log); 01658 } 01659 mutex_exit(&zilog->zl_lock); 01660 } 01661 01662 void 01663 zil_init(void) 01664 { 01665 zil_lwb_cache = kmem_cache_create("zil_lwb_cache", 01666 sizeof (struct lwb), 0, NULL, NULL, NULL, NULL, NULL, 0); 01667 } 01668 01669 void 01670 zil_fini(void) 01671 { 01672 kmem_cache_destroy(zil_lwb_cache); 01673 } 01674 01675 void 01676 zil_set_sync(zilog_t *zilog, uint64_t sync) 01677 { 01678 zilog->zl_sync = sync; 01679 } 01680 01681 void 01682 zil_set_logbias(zilog_t *zilog, uint64_t logbias) 01683 { 01684 zilog->zl_logbias = logbias; 01685 } 01686 01687 zilog_t * 01688 zil_alloc(objset_t *os, zil_header_t *zh_phys) 01689 { 01690 zilog_t *zilog; 01691 01692 zilog = kmem_zalloc(sizeof (zilog_t), KM_SLEEP); 01693 01694 zilog->zl_header = zh_phys; 01695 zilog->zl_os = os; 01696 zilog->zl_spa = dmu_objset_spa(os); 01697 zilog->zl_dmu_pool = dmu_objset_pool(os); 01698 zilog->zl_destroy_txg = TXG_INITIAL - 1; 01699 zilog->zl_logbias = dmu_objset_logbias(os); 01700 zilog->zl_sync = dmu_objset_syncprop(os); 01701 zilog->zl_next_batch = 1; 01702 01703 mutex_init(&zilog->zl_lock, NULL, MUTEX_DEFAULT, NULL); 01704 01705 for (int i = 0; i < TXG_SIZE; i++) { 01706 mutex_init(&zilog->zl_itxg[i].itxg_lock, NULL, 01707 MUTEX_DEFAULT, NULL); 01708 } 01709 01710 list_create(&zilog->zl_lwb_list, sizeof (lwb_t), 01711 offsetof(lwb_t, lwb_node)); 01712 01713 list_create(&zilog->zl_itx_commit_list, sizeof (itx_t), 01714 offsetof(itx_t, itx_node)); 01715 01716 mutex_init(&zilog->zl_vdev_lock, NULL, MUTEX_DEFAULT, NULL); 01717 01718 avl_create(&zilog->zl_vdev_tree, zil_vdev_compare, 01719 sizeof (zil_vdev_node_t), offsetof(zil_vdev_node_t, zv_node)); 01720 01721 cv_init(&zilog->zl_cv_writer, NULL, CV_DEFAULT, NULL); 01722 cv_init(&zilog->zl_cv_suspend, NULL, CV_DEFAULT, NULL); 01723 cv_init(&zilog->zl_cv_batch[0], NULL, CV_DEFAULT, NULL); 01724 cv_init(&zilog->zl_cv_batch[1], NULL, CV_DEFAULT, NULL); 01725 01726 return (zilog); 01727 } 01728 01729 void 01730 zil_free(zilog_t *zilog) 01731 { 01732 zilog->zl_stop_sync = 1; 01733 01734 ASSERT(list_is_empty(&zilog->zl_lwb_list)); 01735 list_destroy(&zilog->zl_lwb_list); 01736 01737 avl_destroy(&zilog->zl_vdev_tree); 01738 mutex_destroy(&zilog->zl_vdev_lock); 01739 01740 ASSERT(list_is_empty(&zilog->zl_itx_commit_list)); 01741 list_destroy(&zilog->zl_itx_commit_list); 01742 01743 for (int i = 0; i < TXG_SIZE; i++) { 01744 /* 01745 * It's possible for an itx to be generated that doesn't dirty 01746 * a txg (e.g. ztest TX_TRUNCATE). So there's no zil_clean() 01747 * callback to remove the entry. We remove those here. 01748 * 01749 * Also free up the ziltest itxs. 01750 */ 01751 if (zilog->zl_itxg[i].itxg_itxs) 01752 zil_itxg_clean(zilog->zl_itxg[i].itxg_itxs); 01753 mutex_destroy(&zilog->zl_itxg[i].itxg_lock); 01754 } 01755 01756 mutex_destroy(&zilog->zl_lock); 01757 01758 cv_destroy(&zilog->zl_cv_writer); 01759 cv_destroy(&zilog->zl_cv_suspend); 01760 cv_destroy(&zilog->zl_cv_batch[0]); 01761 cv_destroy(&zilog->zl_cv_batch[1]); 01762 01763 kmem_free(zilog, sizeof (zilog_t)); 01764 } 01765 01769 zilog_t * 01770 zil_open(objset_t *os, zil_get_data_t *get_data) 01771 { 01772 zilog_t *zilog = dmu_objset_zil(os); 01773 01774 ASSERT(zilog->zl_clean_taskq == NULL); 01775 ASSERT(zilog->zl_get_data == NULL); 01776 ASSERT(list_is_empty(&zilog->zl_lwb_list)); 01777 01778 zilog->zl_get_data = get_data; 01779 zilog->zl_clean_taskq = taskq_create("zil_clean", 1, minclsyspri, 01780 2, 2, TASKQ_PREPOPULATE); 01781 01782 return (zilog); 01783 } 01784 01788 void 01789 zil_close(zilog_t *zilog) 01790 { 01791 lwb_t *lwb; 01792 uint64_t txg = 0; 01793 01794 zil_commit(zilog, 0); /* commit all itx */ 01795 01796 /* 01797 * The lwb_max_txg for the stubby lwb will reflect the last activity 01798 * for the zil. After a txg_wait_synced() on the txg we know all the 01799 * callbacks have occurred that may clean the zil. Only then can we 01800 * destroy the zl_clean_taskq. 01801 */ 01802 mutex_enter(&zilog->zl_lock); 01803 lwb = list_tail(&zilog->zl_lwb_list); 01804 if (lwb != NULL) 01805 txg = lwb->lwb_max_txg; 01806 mutex_exit(&zilog->zl_lock); 01807 if (txg) 01808 txg_wait_synced(zilog->zl_dmu_pool, txg); 01809 ASSERT(!zilog_is_dirty(zilog)); 01810 01811 taskq_destroy(zilog->zl_clean_taskq); 01812 zilog->zl_clean_taskq = NULL; 01813 zilog->zl_get_data = NULL; 01814 01815 /* 01816 * We should have only one LWB left on the list; remove it now. 01817 */ 01818 mutex_enter(&zilog->zl_lock); 01819 lwb = list_head(&zilog->zl_lwb_list); 01820 if (lwb != NULL) { 01821 ASSERT(lwb == list_tail(&zilog->zl_lwb_list)); 01822 list_remove(&zilog->zl_lwb_list, lwb); 01823 zio_buf_free(lwb->lwb_buf, lwb->lwb_sz); 01824 kmem_cache_free(zil_lwb_cache, lwb); 01825 } 01826 mutex_exit(&zilog->zl_lock); 01827 } 01828 01835 int 01836 zil_suspend(zilog_t *zilog) 01837 { 01838 const zil_header_t *zh = zilog->zl_header; 01839 01840 mutex_enter(&zilog->zl_lock); 01841 if (zh->zh_flags & ZIL_REPLAY_NEEDED) { /* unplayed log */ 01842 mutex_exit(&zilog->zl_lock); 01843 return (EBUSY); 01844 } 01845 if (zilog->zl_suspend++ != 0) { 01846 /* 01847 * Someone else already began a suspend. 01848 * Just wait for them to finish. 01849 */ 01850 while (zilog->zl_suspending) 01851 cv_wait(&zilog->zl_cv_suspend, &zilog->zl_lock); 01852 mutex_exit(&zilog->zl_lock); 01853 return (0); 01854 } 01855 zilog->zl_suspending = B_TRUE; 01856 mutex_exit(&zilog->zl_lock); 01857 01858 zil_commit(zilog, 0); 01859 01860 zil_destroy(zilog, B_FALSE); 01861 01862 mutex_enter(&zilog->zl_lock); 01863 zilog->zl_suspending = B_FALSE; 01864 cv_broadcast(&zilog->zl_cv_suspend); 01865 mutex_exit(&zilog->zl_lock); 01866 01867 return (0); 01868 } 01869 01870 void 01871 zil_resume(zilog_t *zilog) 01872 { 01873 mutex_enter(&zilog->zl_lock); 01874 ASSERT(zilog->zl_suspend != 0); 01875 zilog->zl_suspend--; 01876 mutex_exit(&zilog->zl_lock); 01877 } 01878 01879 typedef struct zil_replay_arg { 01880 zil_replay_func_t **zr_replay; 01881 void *zr_arg; 01882 boolean_t zr_byteswap; 01883 char *zr_lr; 01884 } zil_replay_arg_t; 01885 01886 static int 01887 zil_replay_error(zilog_t *zilog, lr_t *lr, int error) 01888 { 01889 char name[MAXNAMELEN]; 01890 01891 zilog->zl_replaying_seq--; /* didn't actually replay this one */ 01892 01893 dmu_objset_name(zilog->zl_os, name); 01894 01895 cmn_err(CE_WARN, "ZFS replay transaction error %d, " 01896 "dataset %s, seq 0x%llx, txtype %llu %s\n", error, name, 01897 (u_longlong_t)lr->lrc_seq, 01898 (u_longlong_t)(lr->lrc_txtype & ~TX_CI), 01899 (lr->lrc_txtype & TX_CI) ? "CI" : ""); 01900 01901 return (error); 01902 } 01903 01904 static int 01905 zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg) 01906 { 01907 zil_replay_arg_t *zr = zra; 01908 const zil_header_t *zh = zilog->zl_header; 01909 uint64_t reclen = lr->lrc_reclen; 01910 uint64_t txtype = lr->lrc_txtype; 01911 int error = 0; 01912 01913 zilog->zl_replaying_seq = lr->lrc_seq; 01914 01915 if (lr->lrc_seq <= zh->zh_replay_seq) /* already replayed */ 01916 return (0); 01917 01918 if (lr->lrc_txg < claim_txg) /* already committed */ 01919 return (0); 01920 01921 /* Strip case-insensitive bit, still present in log record */ 01922 txtype &= ~TX_CI; 01923 01924 if (txtype == 0 || txtype >= TX_MAX_TYPE) 01925 return (zil_replay_error(zilog, lr, EINVAL)); 01926 01927 /* 01928 * If this record type can be logged out of order, the object 01929 * (lr_foid) may no longer exist. That's legitimate, not an error. 01930 */ 01931 if (TX_OOO(txtype)) { 01932 error = dmu_object_info(zilog->zl_os, 01933 ((lr_ooo_t *)lr)->lr_foid, NULL); 01934 if (error == ENOENT || error == EEXIST) 01935 return (0); 01936 } 01937 01938 /* 01939 * Make a copy of the data so we can revise and extend it. 01940 */ 01941 bcopy(lr, zr->zr_lr, reclen); 01942 01943 /* 01944 * If this is a TX_WRITE with a blkptr, suck in the data. 01945 */ 01946 if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) { 01947 error = zil_read_log_data(zilog, (lr_write_t *)lr, 01948 zr->zr_lr + reclen); 01949 if (error) 01950 return (zil_replay_error(zilog, lr, error)); 01951 } 01952 01953 /* 01954 * The log block containing this lr may have been byteswapped 01955 * so that we can easily examine common fields like lrc_txtype. 01956 * However, the log is a mix of different record types, and only the 01957 * replay vectors know how to byteswap their records. Therefore, if 01958 * the lr was byteswapped, undo it before invoking the replay vector. 01959 */ 01960 if (zr->zr_byteswap) 01961 byteswap_uint64_array(zr->zr_lr, reclen); 01962 01963 /* 01964 * We must now do two things atomically: replay this log record, 01965 * and update the log header sequence number to reflect the fact that 01966 * we did so. At the end of each replay function the sequence number 01967 * is updated if we are in replay mode. 01968 */ 01969 error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, zr->zr_byteswap); 01970 if (error) { 01971 /* 01972 * The DMU's dnode layer doesn't see removes until the txg 01973 * commits, so a subsequent claim can spuriously fail with 01974 * EEXIST. So if we receive any error we try syncing out 01975 * any removes then retry the transaction. Note that we 01976 * specify B_FALSE for byteswap now, so we don't do it twice. 01977 */ 01978 txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0); 01979 error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lr, B_FALSE); 01980 if (error) 01981 return (zil_replay_error(zilog, lr, error)); 01982 } 01983 return (0); 01984 } 01985 01986 /* ARGSUSED */ 01987 static int 01988 zil_incr_blks(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg) 01989 { 01990 zilog->zl_replay_blks++; 01991 01992 return (0); 01993 } 01994 01998 void 01999 zil_replay(objset_t *os, void *arg, zil_replay_func_t *replay_func[TX_MAX_TYPE]) 02000 { 02001 zilog_t *zilog = dmu_objset_zil(os); 02002 const zil_header_t *zh = zilog->zl_header; 02003 zil_replay_arg_t zr; 02004 02005 if ((zh->zh_flags & ZIL_REPLAY_NEEDED) == 0) { 02006 zil_destroy(zilog, B_TRUE); 02007 return; 02008 } 02009 //printf("ZFS: Replaying ZIL on %s...\n", os->os->os_spa->spa_name); 02010 02011 zr.zr_replay = replay_func; 02012 zr.zr_arg = arg; 02013 zr.zr_byteswap = BP_SHOULD_BYTESWAP(&zh->zh_log); 02014 zr.zr_lr = kmem_alloc(2 * SPA_MAXBLOCKSIZE, KM_SLEEP); 02015 02016 /* 02017 * Wait for in-progress removes to sync before starting replay. 02018 */ 02019 txg_wait_synced(zilog->zl_dmu_pool, 0); 02020 02021 zilog->zl_replay = B_TRUE; 02022 zilog->zl_replay_time = ddi_get_lbolt(); 02023 ASSERT(zilog->zl_replay_blks == 0); 02024 (void) zil_parse(zilog, zil_incr_blks, zil_replay_log_record, &zr, 02025 zh->zh_claim_txg); 02026 kmem_free(zr.zr_lr, 2 * SPA_MAXBLOCKSIZE); 02027 02028 zil_destroy(zilog, B_FALSE); 02029 txg_wait_synced(zilog->zl_dmu_pool, zilog->zl_destroy_txg); 02030 zilog->zl_replay = B_FALSE; 02031 //printf("ZFS: Replay of ZIL on %s finished.\n", os->os->os_spa->spa_name); 02032 } 02033 02034 boolean_t 02035 zil_replaying(zilog_t *zilog, dmu_tx_t *tx) 02036 { 02037 if (zilog->zl_sync == ZFS_SYNC_DISABLED) 02038 return (B_TRUE); 02039 02040 if (zilog->zl_replay) { 02041 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); 02042 zilog->zl_replayed_seq[dmu_tx_get_txg(tx) & TXG_MASK] = 02043 zilog->zl_replaying_seq; 02044 return (B_TRUE); 02045 } 02046 02047 return (B_FALSE); 02048 } 02049 02050 /* ARGSUSED */ 02051 int 02052 zil_vdev_offline(const char *osname, void *arg) 02053 { 02054 objset_t *os; 02055 zilog_t *zilog; 02056 int error; 02057 02058 error = dmu_objset_hold(osname, FTAG, &os); 02059 if (error) 02060 return (error); 02061 02062 zilog = dmu_objset_zil(os); 02063 if (zil_suspend(zilog) != 0) 02064 error = EEXIST; 02065 else 02066 zil_resume(zilog); 02067 dmu_objset_rele(os, FTAG); 02068 return (error); 02069 }