1189251Ssam/* 2189251Ssam * CDDL HEADER START 3189251Ssam * 4189251Ssam * The contents of this file are subject to the terms of the 5252726Srpaulo * Common Development and Distribution License (the "License"). 6252726Srpaulo * You may not use this file except in compliance with the License. 7189251Ssam * 8189251Ssam * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9189251Ssam * or https://opensource.org/licenses/CDDL-1.0. 10189251Ssam * See the License for the specific language governing permissions 11189251Ssam * and limitations under the License. 12189251Ssam * 13189251Ssam * When distributing Covered Code, include this CDDL HEADER in each 14189251Ssam * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15189251Ssam * If applicable, add the following below this CDDL HEADER, with the 16189251Ssam * fields enclosed by brackets "[]" replaced with your own identifying 17189251Ssam * information: Portions Copyright [yyyy] [name of copyright owner] 18189251Ssam * 19189251Ssam * CDDL HEADER END 20189251Ssam */ 21189251Ssam/* 22189251Ssam * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23189251Ssam * Copyright (c) 2012, 2018 by Delphix. All rights reserved. 24189251Ssam */ 25189251Ssam 26189251Ssam#include <sys/zfs_context.h> 27189251Ssam#include <sys/dmu_objset.h> 28189251Ssam#include <sys/dmu_traverse.h> 29189251Ssam#include <sys/dsl_dataset.h> 30189251Ssam#include <sys/dsl_dir.h> 31189251Ssam#include <sys/dsl_pool.h> 32189251Ssam#include <sys/dnode.h> 33189251Ssam#include <sys/spa.h> 34189251Ssam#include <sys/spa_impl.h> 35189251Ssam#include <sys/zio.h> 36189251Ssam#include <sys/dmu_impl.h> 37189251Ssam#include <sys/sa.h> 38189251Ssam#include <sys/sa_impl.h> 39189251Ssam#include <sys/callb.h> 40189251Ssam#include <sys/zfeature.h> 41189251Ssam 42189251Ssamstatic int32_t zfs_pd_bytes_max = 50 * 1024 * 1024; /* 50MB */ 43189251Ssamstatic int32_t send_holes_without_birth_time = 1; 44189251Ssamstatic uint_t zfs_traverse_indirect_prefetch_limit = 32; 45189251Ssam 46189251Ssamtypedef struct prefetch_data { 47189251Ssam kmutex_t pd_mtx; 48189251Ssam kcondvar_t pd_cv; 49189251Ssam int32_t pd_bytes_fetched; 50189251Ssam int pd_flags; 51189251Ssam boolean_t pd_cancel; 52189251Ssam boolean_t pd_exited; 53189251Ssam zbookmark_phys_t pd_resume; 54189251Ssam} prefetch_data_t; 55189251Ssam 56189251Ssamtypedef struct traverse_data { 57189251Ssam spa_t *td_spa; 58189251Ssam uint64_t td_objset; 59189251Ssam blkptr_t *td_rootbp; 60189251Ssam uint64_t td_min_txg; 61189251Ssam zbookmark_phys_t *td_resume; 62189251Ssam int td_flags; 63189251Ssam prefetch_data_t *td_pfd; 64189251Ssam boolean_t td_paused; 65189251Ssam uint64_t td_hole_birth_enabled_txg; 66189251Ssam blkptr_cb_t *td_func; 67189251Ssam void *td_arg; 68189251Ssam boolean_t td_realloc_possible; 69189251Ssam} traverse_data_t; 70189251Ssam 71189251Ssamstatic int traverse_dnode(traverse_data_t *td, const blkptr_t *bp, 72189251Ssam const dnode_phys_t *dnp, uint64_t objset, uint64_t object); 73189251Ssamstatic void prefetch_dnode_metadata(traverse_data_t *td, const dnode_phys_t *, 74189251Ssam uint64_t objset, uint64_t object); 75189251Ssam 76189251Ssamstatic int 77189251Ssamtraverse_zil_block(zilog_t *zilog, const blkptr_t *bp, void *arg, 78189251Ssam uint64_t claim_txg) 79214734Srpaulo{ 80189251Ssam traverse_data_t *td = arg; 81189251Ssam zbookmark_phys_t zb; 82189251Ssam 83189251Ssam if (BP_IS_HOLE(bp)) 84189251Ssam return (0); 85189251Ssam 86189251Ssam if (claim_txg == 0 && 87214734Srpaulo BP_GET_LOGICAL_BIRTH(bp) >= spa_min_claim_txg(td->td_spa)) 88214734Srpaulo return (-1); 89189251Ssam 90189251Ssam SET_BOOKMARK(&zb, td->td_objset, ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, 91189251Ssam bp->blk_cksum.zc_word[ZIL_ZC_SEQ]); 92189251Ssam 93189251Ssam (void) td->td_func(td->td_spa, zilog, bp, &zb, NULL, td->td_arg); 94189251Ssam 95189251Ssam return (0); 96189251Ssam} 97189251Ssam 98189251Ssamstatic int 99189251Ssamtraverse_zil_record(zilog_t *zilog, const lr_t *lrc, void *arg, 100189251Ssam uint64_t claim_txg) 101189251Ssam{ 102189251Ssam traverse_data_t *td = arg; 103189251Ssam 104189251Ssam if (lrc->lrc_txtype == TX_WRITE) { 105189251Ssam lr_write_t *lr = (lr_write_t *)lrc; 106189251Ssam blkptr_t *bp = &lr->lr_blkptr; 107189251Ssam zbookmark_phys_t zb; 108189251Ssam 109189251Ssam if (BP_IS_HOLE(bp)) 110189251Ssam return (0); 111189251Ssam 112189251Ssam if (claim_txg == 0 || BP_GET_LOGICAL_BIRTH(bp) < claim_txg) 113189251Ssam return (0); 114189251Ssam 115189251Ssam ASSERT3U(BP_GET_LSIZE(bp), !=, 0); 116189251Ssam SET_BOOKMARK(&zb, td->td_objset, lr->lr_foid, 117189251Ssam ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp)); 118189251Ssam 119189251Ssam (void) td->td_func(td->td_spa, zilog, bp, &zb, NULL, 120189251Ssam td->td_arg); 121189251Ssam } 122189251Ssam return (0); 123214734Srpaulo} 124214734Srpaulo 125214734Srpaulostatic void 126214734Srpaulotraverse_zil(traverse_data_t *td, zil_header_t *zh) 127214734Srpaulo{ 128214734Srpaulo uint64_t claim_txg = zh->zh_claim_txg; 129214734Srpaulo 130214734Srpaulo /* 131214734Srpaulo * We only want to visit blocks that have been claimed but not yet 132214734Srpaulo * replayed; plus blocks that are already stable in read-only mode. 133214734Srpaulo */ 134214734Srpaulo if (claim_txg == 0 && spa_writeable(td->td_spa)) 135214734Srpaulo return; 136214734Srpaulo 137214734Srpaulo zilog_t *zilog = zil_alloc(spa_get_dsl(td->td_spa)->dp_meta_objset, zh); 138214734Srpaulo (void) zil_parse(zilog, traverse_zil_block, traverse_zil_record, td, 139214734Srpaulo claim_txg, !(td->td_flags & TRAVERSE_NO_DECRYPT)); 140214734Srpaulo zil_free(zilog); 141214734Srpaulo} 142214734Srpaulo 143189251Ssamtypedef enum resume_skip { 144189251Ssam RESUME_SKIP_ALL, 145189251Ssam RESUME_SKIP_NONE, 146189251Ssam RESUME_SKIP_CHILDREN 147189251Ssam} resume_skip_t; 148189251Ssam 149189251Ssam/* 150189251Ssam * Returns RESUME_SKIP_ALL if td indicates that we are resuming a traversal and 151189251Ssam * the block indicated by zb does not need to be visited at all. Returns 152189251Ssam * RESUME_SKIP_CHILDREN if we are resuming a post traversal and we reach the 153189251Ssam * resume point. This indicates that this block should be visited but not its 154189251Ssam * children (since they must have been visited in a previous traversal). 155189251Ssam * Otherwise returns RESUME_SKIP_NONE. 156189251Ssam */ 157189251Ssamstatic resume_skip_t 158189251Ssamresume_skip_check(const traverse_data_t *td, const dnode_phys_t *dnp, 159189251Ssam const zbookmark_phys_t *zb) 160189251Ssam{ 161189251Ssam if (td->td_resume != NULL) { 162189251Ssam /* 163189251Ssam * If we already visited this bp & everything below, 164189251Ssam * don't bother doing it again. 165189251Ssam */ 166189251Ssam if (zbookmark_subtree_completed(dnp, zb, td->td_resume)) 167189251Ssam return (RESUME_SKIP_ALL); 168189251Ssam 169189251Ssam if (memcmp(zb, td->td_resume, sizeof (*zb)) == 0) { 170189251Ssam if (td->td_flags & TRAVERSE_POST) 171189251Ssam return (RESUME_SKIP_CHILDREN); 172189251Ssam } 173189251Ssam } 174189251Ssam return (RESUME_SKIP_NONE); 175189251Ssam} 176189251Ssam 177189251Ssam/* 178189251Ssam * Returns B_TRUE, if prefetch read is issued, otherwise B_FALSE. 179189251Ssam */ 180189251Ssamstatic boolean_t 181189251Ssamtraverse_prefetch_metadata(traverse_data_t *td, const dnode_phys_t *dnp, 182189251Ssam const blkptr_t *bp, const zbookmark_phys_t *zb) 183189251Ssam{ 184189251Ssam arc_flags_t flags = ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH | 185189251Ssam ARC_FLAG_PRESCIENT_PREFETCH; 186189251Ssam int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE; 187189251Ssam 188189251Ssam if (!(td->td_flags & TRAVERSE_PREFETCH_METADATA)) 189189251Ssam return (B_FALSE); 190189251Ssam /* 191189251Ssam * If this bp is before the resume point, it may have already been 192189251Ssam * freed. 193189251Ssam */ 194189251Ssam if (resume_skip_check(td, dnp, zb) != RESUME_SKIP_NONE) 195189251Ssam return (B_FALSE); 196189251Ssam if (BP_IS_HOLE(bp) || BP_GET_LOGICAL_BIRTH(bp) <= td->td_min_txg) 197189251Ssam return (B_FALSE); 198189251Ssam if (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_DNODE) 199189251Ssam return (B_FALSE); 200189251Ssam ASSERT(!BP_IS_REDACTED(bp)); 201189251Ssam 202189251Ssam if ((td->td_flags & TRAVERSE_NO_DECRYPT) && BP_IS_PROTECTED(bp)) 203189251Ssam zio_flags |= ZIO_FLAG_RAW; 204189251Ssam 205189251Ssam (void) arc_read(NULL, td->td_spa, bp, NULL, NULL, 206189251Ssam ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb); 207 return (B_TRUE); 208} 209 210static boolean_t 211prefetch_needed(prefetch_data_t *pfd, const blkptr_t *bp) 212{ 213 ASSERT(pfd->pd_flags & TRAVERSE_PREFETCH_DATA); 214 if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp) || 215 BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG || BP_IS_REDACTED(bp)) 216 return (B_FALSE); 217 return (B_TRUE); 218} 219 220static int 221traverse_visitbp(traverse_data_t *td, const dnode_phys_t *dnp, 222 const blkptr_t *bp, const zbookmark_phys_t *zb) 223{ 224 int err = 0; 225 arc_buf_t *buf = NULL; 226 prefetch_data_t *pd = td->td_pfd; 227 228 switch (resume_skip_check(td, dnp, zb)) { 229 case RESUME_SKIP_ALL: 230 return (0); 231 case RESUME_SKIP_CHILDREN: 232 goto post; 233 case RESUME_SKIP_NONE: 234 break; 235 default: 236 ASSERT(0); 237 } 238 239 if (BP_GET_LOGICAL_BIRTH(bp) == 0) { 240 /* 241 * Since this block has a birth time of 0 it must be one of 242 * two things: a hole created before the 243 * SPA_FEATURE_HOLE_BIRTH feature was enabled, or a hole 244 * which has always been a hole in an object. 245 * 246 * If a file is written sparsely, then the unwritten parts of 247 * the file were "always holes" -- that is, they have been 248 * holes since this object was allocated. However, we (and 249 * our callers) can not necessarily tell when an object was 250 * allocated. Therefore, if it's possible that this object 251 * was freed and then its object number reused, we need to 252 * visit all the holes with birth==0. 253 * 254 * If it isn't possible that the object number was reused, 255 * then if SPA_FEATURE_HOLE_BIRTH was enabled before we wrote 256 * all the blocks we will visit as part of this traversal, 257 * then this hole must have always existed, so we can skip 258 * it. We visit blocks born after (exclusive) td_min_txg. 259 * 260 * Note that the meta-dnode cannot be reallocated. 261 */ 262 if (!send_holes_without_birth_time && 263 (!td->td_realloc_possible || 264 zb->zb_object == DMU_META_DNODE_OBJECT) && 265 td->td_hole_birth_enabled_txg <= td->td_min_txg) 266 return (0); 267 } else if (BP_GET_LOGICAL_BIRTH(bp) <= td->td_min_txg) { 268 return (0); 269 } 270 271 if (pd != NULL && !pd->pd_exited && prefetch_needed(pd, bp)) { 272 uint64_t size = BP_GET_LSIZE(bp); 273 mutex_enter(&pd->pd_mtx); 274 ASSERT(pd->pd_bytes_fetched >= 0); 275 while (pd->pd_bytes_fetched < size && !pd->pd_exited) 276 cv_wait_sig(&pd->pd_cv, &pd->pd_mtx); 277 pd->pd_bytes_fetched -= size; 278 cv_broadcast(&pd->pd_cv); 279 mutex_exit(&pd->pd_mtx); 280 } 281 282 if (BP_IS_HOLE(bp) || BP_IS_REDACTED(bp)) { 283 err = td->td_func(td->td_spa, NULL, bp, zb, dnp, td->td_arg); 284 if (err != 0) 285 goto post; 286 return (0); 287 } 288 289 if (td->td_flags & TRAVERSE_PRE) { 290 err = td->td_func(td->td_spa, NULL, bp, zb, dnp, 291 td->td_arg); 292 if (err == TRAVERSE_VISIT_NO_CHILDREN) 293 return (0); 294 if (err != 0) 295 goto post; 296 } 297 298 if (BP_GET_LEVEL(bp) > 0) { 299 uint32_t flags = ARC_FLAG_WAIT; 300 int32_t i, ptidx, pidx; 301 uint32_t prefetchlimit; 302 int32_t epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT; 303 zbookmark_phys_t *czb; 304 305 ASSERT(!BP_IS_PROTECTED(bp)); 306 307 err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf, 308 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb); 309 if (err != 0) 310 goto post; 311 312 czb = kmem_alloc(sizeof (zbookmark_phys_t), KM_SLEEP); 313 314 /* 315 * When performing a traversal it is beneficial to 316 * asynchronously read-ahead the upcoming indirect 317 * blocks since they will be needed shortly. However, 318 * since a 128k indirect (non-L0) block may contain up 319 * to 1024 128-byte block pointers, its preferable to not 320 * prefetch them all at once. Issuing a large number of 321 * async reads may effect performance, and the earlier 322 * the indirect blocks are prefetched the less likely 323 * they are to still be resident in the ARC when needed. 324 * Therefore, prefetching indirect blocks is limited to 325 * zfs_traverse_indirect_prefetch_limit=32 blocks by 326 * default. 327 * 328 * pidx: Index for which next prefetch to be issued. 329 * ptidx: Index at which next prefetch to be triggered. 330 */ 331 ptidx = 0; 332 pidx = 1; 333 prefetchlimit = zfs_traverse_indirect_prefetch_limit; 334 for (i = 0; i < epb; i++) { 335 if (prefetchlimit && i == ptidx) { 336 ASSERT3S(ptidx, <=, pidx); 337 for (uint32_t prefetched = 0; pidx < epb && 338 prefetched < prefetchlimit; pidx++) { 339 SET_BOOKMARK(czb, zb->zb_objset, 340 zb->zb_object, zb->zb_level - 1, 341 zb->zb_blkid * epb + pidx); 342 if (traverse_prefetch_metadata(td, dnp, 343 &((blkptr_t *)buf->b_data)[pidx], 344 czb) == B_TRUE) { 345 prefetched++; 346 if (prefetched == 347 MAX(prefetchlimit / 2, 1)) 348 ptidx = pidx; 349 } 350 } 351 } 352 353 /* recursively visitbp() blocks below this */ 354 SET_BOOKMARK(czb, zb->zb_objset, zb->zb_object, 355 zb->zb_level - 1, 356 zb->zb_blkid * epb + i); 357 err = traverse_visitbp(td, dnp, 358 &((blkptr_t *)buf->b_data)[i], czb); 359 if (err != 0) 360 break; 361 } 362 363 kmem_free(czb, sizeof (zbookmark_phys_t)); 364 365 } else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) { 366 uint32_t flags = ARC_FLAG_WAIT; 367 uint32_t zio_flags = ZIO_FLAG_CANFAIL; 368 int32_t i; 369 int32_t epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT; 370 dnode_phys_t *child_dnp; 371 372 /* 373 * dnode blocks might have their bonus buffers encrypted, so 374 * we must be careful to honor TRAVERSE_NO_DECRYPT 375 */ 376 if ((td->td_flags & TRAVERSE_NO_DECRYPT) && BP_IS_PROTECTED(bp)) 377 zio_flags |= ZIO_FLAG_RAW; 378 379 err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf, 380 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb); 381 if (err != 0) 382 goto post; 383 384 child_dnp = buf->b_data; 385 386 for (i = 0; i < epb; i += child_dnp[i].dn_extra_slots + 1) { 387 prefetch_dnode_metadata(td, &child_dnp[i], 388 zb->zb_objset, zb->zb_blkid * epb + i); 389 } 390 391 /* recursively visitbp() blocks below this */ 392 for (i = 0; i < epb; i += child_dnp[i].dn_extra_slots + 1) { 393 err = traverse_dnode(td, bp, &child_dnp[i], 394 zb->zb_objset, zb->zb_blkid * epb + i); 395 if (err != 0) 396 break; 397 } 398 } else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) { 399 uint32_t zio_flags = ZIO_FLAG_CANFAIL; 400 arc_flags_t flags = ARC_FLAG_WAIT; 401 objset_phys_t *osp; 402 403 if ((td->td_flags & TRAVERSE_NO_DECRYPT) && BP_IS_PROTECTED(bp)) 404 zio_flags |= ZIO_FLAG_RAW; 405 406 err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf, 407 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb); 408 if (err != 0) 409 goto post; 410 411 osp = buf->b_data; 412 prefetch_dnode_metadata(td, &osp->os_meta_dnode, zb->zb_objset, 413 DMU_META_DNODE_OBJECT); 414 /* 415 * See the block comment above for the goal of this variable. 416 * If the maxblkid of the meta-dnode is 0, then we know that 417 * we've never had more than DNODES_PER_BLOCK objects in the 418 * dataset, which means we can't have reused any object ids. 419 */ 420 if (osp->os_meta_dnode.dn_maxblkid == 0) 421 td->td_realloc_possible = B_FALSE; 422 423 if (OBJSET_BUF_HAS_USERUSED(buf)) { 424 if (OBJSET_BUF_HAS_PROJECTUSED(buf)) 425 prefetch_dnode_metadata(td, 426 &osp->os_projectused_dnode, 427 zb->zb_objset, DMU_PROJECTUSED_OBJECT); 428 prefetch_dnode_metadata(td, &osp->os_groupused_dnode, 429 zb->zb_objset, DMU_GROUPUSED_OBJECT); 430 prefetch_dnode_metadata(td, &osp->os_userused_dnode, 431 zb->zb_objset, DMU_USERUSED_OBJECT); 432 } 433 434 err = traverse_dnode(td, bp, &osp->os_meta_dnode, zb->zb_objset, 435 DMU_META_DNODE_OBJECT); 436 if (err == 0 && OBJSET_BUF_HAS_USERUSED(buf)) { 437 if (OBJSET_BUF_HAS_PROJECTUSED(buf)) 438 err = traverse_dnode(td, bp, 439 &osp->os_projectused_dnode, zb->zb_objset, 440 DMU_PROJECTUSED_OBJECT); 441 if (err == 0) 442 err = traverse_dnode(td, bp, 443 &osp->os_groupused_dnode, zb->zb_objset, 444 DMU_GROUPUSED_OBJECT); 445 if (err == 0) 446 err = traverse_dnode(td, bp, 447 &osp->os_userused_dnode, zb->zb_objset, 448 DMU_USERUSED_OBJECT); 449 } 450 } 451 452 if (buf) 453 arc_buf_destroy(buf, &buf); 454 455post: 456 if (err == 0 && (td->td_flags & TRAVERSE_POST)) 457 err = td->td_func(td->td_spa, NULL, bp, zb, dnp, td->td_arg); 458 459 if ((td->td_flags & TRAVERSE_HARD) && (err == EIO || err == ECKSUM)) { 460 /* 461 * Ignore this disk error as requested by the HARD flag, 462 * and continue traversal. 463 */ 464 err = 0; 465 } 466 467 /* 468 * If we are stopping here, set td_resume. 469 */ 470 if (td->td_resume != NULL && err != 0 && !td->td_paused) { 471 td->td_resume->zb_objset = zb->zb_objset; 472 td->td_resume->zb_object = zb->zb_object; 473 td->td_resume->zb_level = 0; 474 /* 475 * If we have stopped on an indirect block (e.g. due to 476 * i/o error), we have not visited anything below it. 477 * Set the bookmark to the first level-0 block that we need 478 * to visit. This way, the resuming code does not need to 479 * deal with resuming from indirect blocks. 480 * 481 * Note, if zb_level <= 0, dnp may be NULL, so we don't want 482 * to dereference it. 483 */ 484 td->td_resume->zb_blkid = zb->zb_blkid; 485 if (zb->zb_level > 0) { 486 td->td_resume->zb_blkid <<= zb->zb_level * 487 (dnp->dn_indblkshift - SPA_BLKPTRSHIFT); 488 } 489 td->td_paused = B_TRUE; 490 } 491 492 return (err); 493} 494 495static void 496prefetch_dnode_metadata(traverse_data_t *td, const dnode_phys_t *dnp, 497 uint64_t objset, uint64_t object) 498{ 499 int j; 500 zbookmark_phys_t czb; 501 502 for (j = 0; j < dnp->dn_nblkptr; j++) { 503 SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j); 504 traverse_prefetch_metadata(td, dnp, &dnp->dn_blkptr[j], &czb); 505 } 506 507 if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) { 508 SET_BOOKMARK(&czb, objset, object, 0, DMU_SPILL_BLKID); 509 traverse_prefetch_metadata(td, dnp, DN_SPILL_BLKPTR(dnp), &czb); 510 } 511} 512 513static int 514traverse_dnode(traverse_data_t *td, const blkptr_t *bp, const dnode_phys_t *dnp, 515 uint64_t objset, uint64_t object) 516{ 517 int j, err = 0; 518 zbookmark_phys_t czb; 519 520 if (object != DMU_META_DNODE_OBJECT && td->td_resume != NULL && 521 object < td->td_resume->zb_object) 522 return (0); 523 524 if (td->td_flags & TRAVERSE_PRE) { 525 SET_BOOKMARK(&czb, objset, object, ZB_DNODE_LEVEL, 526 ZB_DNODE_BLKID); 527 err = td->td_func(td->td_spa, NULL, bp, &czb, dnp, 528 td->td_arg); 529 if (err == TRAVERSE_VISIT_NO_CHILDREN) 530 return (0); 531 if (err != 0) 532 return (err); 533 } 534 535 for (j = 0; j < dnp->dn_nblkptr; j++) { 536 SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j); 537 err = traverse_visitbp(td, dnp, &dnp->dn_blkptr[j], &czb); 538 if (err != 0) 539 break; 540 } 541 542 if (err == 0 && (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) { 543 SET_BOOKMARK(&czb, objset, object, 0, DMU_SPILL_BLKID); 544 err = traverse_visitbp(td, dnp, DN_SPILL_BLKPTR(dnp), &czb); 545 } 546 547 if (err == 0 && (td->td_flags & TRAVERSE_POST)) { 548 SET_BOOKMARK(&czb, objset, object, ZB_DNODE_LEVEL, 549 ZB_DNODE_BLKID); 550 err = td->td_func(td->td_spa, NULL, bp, &czb, dnp, 551 td->td_arg); 552 if (err == TRAVERSE_VISIT_NO_CHILDREN) 553 return (0); 554 if (err != 0) 555 return (err); 556 } 557 return (err); 558} 559 560static int 561traverse_prefetcher(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 562 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) 563{ 564 (void) zilog, (void) dnp; 565 prefetch_data_t *pfd = arg; 566 int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE; 567 arc_flags_t aflags = ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH | 568 ARC_FLAG_PRESCIENT_PREFETCH; 569 570 ASSERT(pfd->pd_bytes_fetched >= 0); 571 if (zb->zb_level == ZB_DNODE_LEVEL) 572 return (0); 573 if (pfd->pd_cancel) 574 return (SET_ERROR(EINTR)); 575 576 if (!prefetch_needed(pfd, bp)) 577 return (0); 578 579 mutex_enter(&pfd->pd_mtx); 580 while (!pfd->pd_cancel && pfd->pd_bytes_fetched >= zfs_pd_bytes_max) 581 cv_wait_sig(&pfd->pd_cv, &pfd->pd_mtx); 582 pfd->pd_bytes_fetched += BP_GET_LSIZE(bp); 583 cv_broadcast(&pfd->pd_cv); 584 mutex_exit(&pfd->pd_mtx); 585 586 if ((pfd->pd_flags & TRAVERSE_NO_DECRYPT) && BP_IS_PROTECTED(bp)) 587 zio_flags |= ZIO_FLAG_RAW; 588 589 (void) arc_read(NULL, spa, bp, NULL, NULL, ZIO_PRIORITY_ASYNC_READ, 590 zio_flags, &aflags, zb); 591 592 return (0); 593} 594 595static void 596traverse_prefetch_thread(void *arg) 597{ 598 traverse_data_t *td_main = arg; 599 traverse_data_t td = *td_main; 600 zbookmark_phys_t czb; 601 fstrans_cookie_t cookie = spl_fstrans_mark(); 602 603 td.td_func = traverse_prefetcher; 604 td.td_arg = td_main->td_pfd; 605 td.td_pfd = NULL; 606 td.td_resume = &td_main->td_pfd->pd_resume; 607 608 SET_BOOKMARK(&czb, td.td_objset, 609 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 610 (void) traverse_visitbp(&td, NULL, td.td_rootbp, &czb); 611 612 mutex_enter(&td_main->td_pfd->pd_mtx); 613 td_main->td_pfd->pd_exited = B_TRUE; 614 cv_broadcast(&td_main->td_pfd->pd_cv); 615 mutex_exit(&td_main->td_pfd->pd_mtx); 616 spl_fstrans_unmark(cookie); 617} 618 619/* 620 * NB: dataset must not be changing on-disk (eg, is a snapshot or we are 621 * in syncing context). 622 */ 623static int 624traverse_impl(spa_t *spa, dsl_dataset_t *ds, uint64_t objset, blkptr_t *rootbp, 625 uint64_t txg_start, zbookmark_phys_t *resume, int flags, 626 blkptr_cb_t func, void *arg) 627{ 628 traverse_data_t *td; 629 prefetch_data_t *pd; 630 zbookmark_phys_t *czb; 631 int err; 632 633 ASSERT(ds == NULL || objset == ds->ds_object); 634 ASSERT(!(flags & TRAVERSE_PRE) || !(flags & TRAVERSE_POST)); 635 636 td = kmem_alloc(sizeof (traverse_data_t), KM_SLEEP); 637 pd = kmem_zalloc(sizeof (prefetch_data_t), KM_SLEEP); 638 czb = kmem_alloc(sizeof (zbookmark_phys_t), KM_SLEEP); 639 640 td->td_spa = spa; 641 td->td_objset = objset; 642 td->td_rootbp = rootbp; 643 td->td_min_txg = txg_start; 644 td->td_resume = resume; 645 td->td_func = func; 646 td->td_arg = arg; 647 td->td_pfd = pd; 648 td->td_flags = flags; 649 td->td_paused = B_FALSE; 650 td->td_realloc_possible = (txg_start == 0 ? B_FALSE : B_TRUE); 651 652 if (spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) { 653 VERIFY(spa_feature_enabled_txg(spa, 654 SPA_FEATURE_HOLE_BIRTH, &td->td_hole_birth_enabled_txg)); 655 } else { 656 td->td_hole_birth_enabled_txg = UINT64_MAX; 657 } 658 659 pd->pd_flags = flags; 660 if (resume != NULL) 661 pd->pd_resume = *resume; 662 mutex_init(&pd->pd_mtx, NULL, MUTEX_DEFAULT, NULL); 663 cv_init(&pd->pd_cv, NULL, CV_DEFAULT, NULL); 664 665 SET_BOOKMARK(czb, td->td_objset, 666 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 667 668 /* See comment on ZIL traversal in dsl_scan_visitds. */ 669 if (ds != NULL && !ds->ds_is_snapshot && !BP_IS_HOLE(rootbp)) { 670 zio_flag_t zio_flags = ZIO_FLAG_CANFAIL; 671 uint32_t flags = ARC_FLAG_WAIT; 672 objset_phys_t *osp; 673 arc_buf_t *buf; 674 ASSERT(!BP_IS_REDACTED(rootbp)); 675 676 if ((td->td_flags & TRAVERSE_NO_DECRYPT) && 677 BP_IS_PROTECTED(rootbp)) 678 zio_flags |= ZIO_FLAG_RAW; 679 680 err = arc_read(NULL, td->td_spa, rootbp, arc_getbuf_func, 681 &buf, ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, czb); 682 if (err != 0) { 683 /* 684 * If both TRAVERSE_HARD and TRAVERSE_PRE are set, 685 * continue to visitbp so that td_func can be called 686 * in pre stage, and err will reset to zero. 687 */ 688 if (!(td->td_flags & TRAVERSE_HARD) || 689 !(td->td_flags & TRAVERSE_PRE)) 690 goto out; 691 } else { 692 osp = buf->b_data; 693 traverse_zil(td, &osp->os_zil_header); 694 arc_buf_destroy(buf, &buf); 695 } 696 } 697 698 if (!(flags & TRAVERSE_PREFETCH_DATA) || 699 taskq_dispatch(spa->spa_prefetch_taskq, traverse_prefetch_thread, 700 td, TQ_NOQUEUE) == TASKQID_INVALID) 701 pd->pd_exited = B_TRUE; 702 703 err = traverse_visitbp(td, NULL, rootbp, czb); 704 705 mutex_enter(&pd->pd_mtx); 706 pd->pd_cancel = B_TRUE; 707 cv_broadcast(&pd->pd_cv); 708 while (!pd->pd_exited) 709 cv_wait_sig(&pd->pd_cv, &pd->pd_mtx); 710 mutex_exit(&pd->pd_mtx); 711out: 712 mutex_destroy(&pd->pd_mtx); 713 cv_destroy(&pd->pd_cv); 714 715 kmem_free(czb, sizeof (zbookmark_phys_t)); 716 kmem_free(pd, sizeof (struct prefetch_data)); 717 kmem_free(td, sizeof (struct traverse_data)); 718 719 return (err); 720} 721 722/* 723 * NB: dataset must not be changing on-disk (eg, is a snapshot or we are 724 * in syncing context). 725 */ 726int 727traverse_dataset_resume(dsl_dataset_t *ds, uint64_t txg_start, 728 zbookmark_phys_t *resume, 729 int flags, blkptr_cb_t func, void *arg) 730{ 731 return (traverse_impl(ds->ds_dir->dd_pool->dp_spa, ds, ds->ds_object, 732 &dsl_dataset_phys(ds)->ds_bp, txg_start, resume, flags, func, arg)); 733} 734 735int 736traverse_dataset(dsl_dataset_t *ds, uint64_t txg_start, 737 int flags, blkptr_cb_t func, void *arg) 738{ 739 return (traverse_dataset_resume(ds, txg_start, NULL, flags, func, arg)); 740} 741 742int 743traverse_dataset_destroyed(spa_t *spa, blkptr_t *blkptr, 744 uint64_t txg_start, zbookmark_phys_t *resume, int flags, 745 blkptr_cb_t func, void *arg) 746{ 747 return (traverse_impl(spa, NULL, ZB_DESTROYED_OBJSET, 748 blkptr, txg_start, resume, flags, func, arg)); 749} 750 751/* 752 * NB: pool must not be changing on-disk (eg, from zdb or sync context). 753 */ 754int 755traverse_pool(spa_t *spa, uint64_t txg_start, int flags, 756 blkptr_cb_t func, void *arg) 757{ 758 int err; 759 dsl_pool_t *dp = spa_get_dsl(spa); 760 objset_t *mos = dp->dp_meta_objset; 761 boolean_t hard = (flags & TRAVERSE_HARD); 762 763 /* visit the MOS */ 764 err = traverse_impl(spa, NULL, 0, spa_get_rootblkptr(spa), 765 txg_start, NULL, flags, func, arg); 766 if (err != 0) 767 return (err); 768 769 /* visit each dataset */ 770 for (uint64_t obj = 1; err == 0; 771 err = dmu_object_next(mos, &obj, B_FALSE, txg_start)) { 772 dmu_object_info_t doi; 773 774 err = dmu_object_info(mos, obj, &doi); 775 if (err != 0) { 776 if (hard) 777 continue; 778 break; 779 } 780 781 if (doi.doi_bonus_type == DMU_OT_DSL_DATASET) { 782 dsl_dataset_t *ds; 783 uint64_t txg = txg_start; 784 785 dsl_pool_config_enter(dp, FTAG); 786 err = dsl_dataset_hold_obj(dp, obj, FTAG, &ds); 787 dsl_pool_config_exit(dp, FTAG); 788 if (err != 0) { 789 if (hard) 790 continue; 791 break; 792 } 793 if (dsl_dataset_phys(ds)->ds_prev_snap_txg > txg) 794 txg = dsl_dataset_phys(ds)->ds_prev_snap_txg; 795 err = traverse_dataset(ds, txg, flags, func, arg); 796 dsl_dataset_rele(ds, FTAG); 797 if (err != 0) 798 break; 799 } 800 } 801 if (err == ESRCH) 802 err = 0; 803 return (err); 804} 805 806EXPORT_SYMBOL(traverse_dataset); 807EXPORT_SYMBOL(traverse_pool); 808 809ZFS_MODULE_PARAM(zfs, zfs_, pd_bytes_max, INT, ZMOD_RW, 810 "Max number of bytes to prefetch"); 811 812ZFS_MODULE_PARAM(zfs, zfs_, traverse_indirect_prefetch_limit, UINT, ZMOD_RW, 813 "Traverse prefetch number of blocks pointed by indirect block"); 814 815#if defined(_KERNEL) 816module_param_named(ignore_hole_birth, send_holes_without_birth_time, int, 0644); 817MODULE_PARM_DESC(ignore_hole_birth, 818 "Alias for send_holes_without_birth_time"); 819#endif 820 821/* CSTYLED */ 822ZFS_MODULE_PARAM(zfs, , send_holes_without_birth_time, INT, ZMOD_RW, 823 "Ignore hole_birth txg for zfs send"); 824