1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <sys/zfs_context.h> 29 #include <sys/dmu_objset.h> 30 #include <sys/dmu_traverse.h> 31 #include <sys/dsl_dataset.h> 32 #include <sys/dsl_dir.h> 33 #include <sys/dsl_pool.h> 34 #include <sys/dnode.h> 35 #include <sys/spa.h> 36 #include <sys/zio.h> 37 #include <sys/dmu_impl.h> 38 39 #define BP_SPAN_SHIFT(level, width) ((level) * (width)) 40 41 #define BP_EQUAL(b1, b2) \ 42 (DVA_EQUAL(BP_IDENTITY(b1), BP_IDENTITY(b2)) && \ 43 (b1)->blk_birth == (b2)->blk_birth) 44 45 /* 46 * Compare two bookmarks. 47 * 48 * For ADVANCE_PRE, the visitation order is: 49 * 50 * objset 0, 1, 2, ..., ZB_MAXOBJSET. 51 * object 0, 1, 2, ..., ZB_MAXOBJECT. 52 * blkoff 0, 1, 2, ... 53 * level ZB_MAXLEVEL, ..., 2, 1, 0. 54 * 55 * where blkoff = blkid << BP_SPAN_SHIFT(level, width), and thus a valid 56 * ordering vector is: 57 * 58 * < objset, object, blkoff, -level > 59 * 60 * For ADVANCE_POST, the starting offsets aren't sequential but ending 61 * offsets [blkoff = (blkid + 1) << BP_SPAN_SHIFT(level, width)] are. 62 * The visitation order is: 63 * 64 * objset 1, 2, ..., ZB_MAXOBJSET, 0. 65 * object 1, 2, ..., ZB_MAXOBJECT, 0. 66 * blkoff 1, 2, ... 67 * level 0, 1, 2, ..., ZB_MAXLEVEL. 68 * 69 * and thus a valid ordering vector is: 70 * 71 * < objset - 1, object - 1, blkoff, level > 72 * 73 * Both orderings can be expressed as: 74 * 75 * < objset + bias, object + bias, blkoff, level ^ bias > 76 * 77 * where 'bias' is either 0 or -1 (for ADVANCE_PRE or ADVANCE_POST) 78 * and 'blkoff' is (blkid - bias) << BP_SPAN_SHIFT(level, wshift). 79 * 80 * Special case: an objset's osphys is represented as level -1 of object 0. 81 * It is always either the very first or very last block we visit in an objset. 82 * Therefore, if either bookmark's level is -1, level alone determines order. 83 */ 84 static int 85 compare_bookmark(zbookmark_t *szb, zbookmark_t *ezb, dnode_phys_t *dnp, 86 int advance) 87 { 88 int bias = (advance & ADVANCE_PRE) ? 0 : -1; 89 uint64_t sblkoff, eblkoff; 90 int slevel, elevel, wshift; 91 92 if (szb->zb_objset + bias < ezb->zb_objset + bias) 93 return (-1); 94 95 if (szb->zb_objset + bias > ezb->zb_objset + bias) 96 return (1); 97 98 slevel = szb->zb_level; 99 elevel = ezb->zb_level; 100 101 if ((slevel | elevel) < 0) 102 return ((slevel ^ bias) - (elevel ^ bias)); 103 104 if (szb->zb_object + bias < ezb->zb_object + bias) 105 return (-1); 106 107 if (szb->zb_object + bias > ezb->zb_object + bias) 108 return (1); 109 110 if (dnp == NULL) 111 return (0); 112 113 wshift = dnp->dn_indblkshift - SPA_BLKPTRSHIFT; 114 115 sblkoff = (szb->zb_blkid - bias) << BP_SPAN_SHIFT(slevel, wshift); 116 eblkoff = (ezb->zb_blkid - bias) << BP_SPAN_SHIFT(elevel, wshift); 117 118 if (sblkoff < eblkoff) 119 return (-1); 120 121 if (sblkoff > eblkoff) 122 return (1); 123 124 return ((elevel ^ bias) - (slevel ^ bias)); 125 } 126 127 #define SET_BOOKMARK(zb, objset, object, level, blkid) \ 128 { \ 129 (zb)->zb_objset = objset; \ 130 (zb)->zb_object = object; \ 131 (zb)->zb_level = level; \ 132 (zb)->zb_blkid = blkid; \ 133 } 134 135 #define SET_BOOKMARK_LB(zb, level, blkid) \ 136 { \ 137 (zb)->zb_level = level; \ 138 (zb)->zb_blkid = blkid; \ 139 } 140 141 static int 142 advance_objset(zseg_t *zseg, uint64_t objset, int advance) 143 { 144 zbookmark_t *zb = &zseg->seg_start; 145 146 if (advance & ADVANCE_PRE) { 147 if (objset >= ZB_MAXOBJSET) 148 return (ERANGE); 149 SET_BOOKMARK(zb, objset, 0, -1, 0); 150 } else { 151 if (objset >= ZB_MAXOBJSET) 152 objset = 0; 153 SET_BOOKMARK(zb, objset, 1, 0, 0); 154 } 155 156 if (compare_bookmark(zb, &zseg->seg_end, NULL, advance) > 0) 157 return (ERANGE); 158 159 return (EAGAIN); 160 } 161 162 static int 163 advance_object(zseg_t *zseg, uint64_t object, int advance) 164 { 165 zbookmark_t *zb = &zseg->seg_start; 166 167 if (advance & ADVANCE_PRE) { 168 if (object >= ZB_MAXOBJECT) { 169 SET_BOOKMARK(zb, zb->zb_objset + 1, 0, -1, 0); 170 } else { 171 SET_BOOKMARK(zb, zb->zb_objset, object, ZB_MAXLEVEL, 0); 172 } 173 } else { 174 if (zb->zb_object == 0) { 175 SET_BOOKMARK(zb, zb->zb_objset, 0, -1, 0); 176 } else { 177 if (object >= ZB_MAXOBJECT) 178 object = 0; 179 SET_BOOKMARK(zb, zb->zb_objset, object, 0, 0); 180 } 181 } 182 183 if (compare_bookmark(zb, &zseg->seg_end, NULL, advance) > 0) 184 return (ERANGE); 185 186 return (EAGAIN); 187 } 188 189 static int 190 advance_from_osphys(zseg_t *zseg, int advance) 191 { 192 zbookmark_t *zb = &zseg->seg_start; 193 194 ASSERT(zb->zb_object == 0); 195 ASSERT(zb->zb_level == -1); 196 ASSERT(zb->zb_blkid == 0); 197 198 if (advance & ADVANCE_PRE) { 199 SET_BOOKMARK_LB(zb, ZB_MAXLEVEL, 0); 200 } else { 201 if (zb->zb_objset == 0) 202 return (ERANGE); 203 SET_BOOKMARK(zb, zb->zb_objset + 1, 1, 0, 0); 204 } 205 206 if (compare_bookmark(zb, &zseg->seg_end, NULL, advance) > 0) 207 return (ERANGE); 208 209 return (EAGAIN); 210 } 211 212 static int 213 advance_block(zseg_t *zseg, dnode_phys_t *dnp, int rc, int advance) 214 { 215 zbookmark_t *zb = &zseg->seg_start; 216 int wshift = dnp->dn_indblkshift - SPA_BLKPTRSHIFT; 217 int maxlevel = dnp->dn_nlevels - 1; 218 int level = zb->zb_level; 219 uint64_t blkid = zb->zb_blkid; 220 221 if (advance & ADVANCE_PRE) { 222 if (level > 0 && rc == 0) { 223 level--; 224 blkid <<= wshift; 225 } else { 226 blkid++; 227 228 if ((blkid << BP_SPAN_SHIFT(level, wshift)) > 229 dnp->dn_maxblkid) 230 return (ERANGE); 231 232 while (level < maxlevel) { 233 if (P2PHASE(blkid, 1ULL << wshift)) 234 break; 235 blkid >>= wshift; 236 level++; 237 } 238 } 239 } else { 240 if (level >= maxlevel || P2PHASE(blkid + 1, 1ULL << wshift)) { 241 blkid = (blkid + 1) << BP_SPAN_SHIFT(level, wshift); 242 level = 0; 243 } else { 244 blkid >>= wshift; 245 level++; 246 } 247 248 while ((blkid << BP_SPAN_SHIFT(level, wshift)) > 249 dnp->dn_maxblkid) { 250 if (level == maxlevel) 251 return (ERANGE); 252 blkid >>= wshift; 253 level++; 254 } 255 } 256 SET_BOOKMARK_LB(zb, level, blkid); 257 258 if (compare_bookmark(zb, &zseg->seg_end, dnp, advance) > 0) 259 return (ERANGE); 260 261 return (EAGAIN); 262 } 263 264 static int 265 traverse_callback(traverse_handle_t *th, zseg_t *zseg, traverse_blk_cache_t *bc) 266 { 267 /* 268 * Before we issue the callback, prune against maxtxg. 269 * 270 * We prune against mintxg before we get here because it's a big win. 271 * If a given block was born in txg 37, then we know that the entire 272 * subtree below that block must have been born in txg 37 or earlier. 273 * We can therefore lop off huge branches of the tree as we go. 274 * 275 * There's no corresponding optimization for maxtxg because knowing 276 * that bp->blk_birth >= maxtxg doesn't imply anything about the bp's 277 * children. In fact, the copy-on-write design of ZFS ensures that 278 * top-level blocks will pretty much always be new. 279 * 280 * Therefore, in the name of simplicity we don't prune against 281 * maxtxg until the last possible moment -- that being right now. 282 */ 283 if (bc->bc_errno == 0 && bc->bc_blkptr.blk_birth >= zseg->seg_maxtxg) 284 return (0); 285 286 if (bc->bc_errno == 0) { 287 zbookmark_t *zb = &bc->bc_bookmark; 288 zbookmark_t *szb = &zseg->seg_start; 289 zbookmark_t *ezb = &zseg->seg_end; 290 zbookmark_t *lzb = &th->th_lastcb; 291 dnode_phys_t *dnp = bc->bc_dnode; 292 293 /* 294 * Debugging: verify that the order we visit things 295 * agrees with the order defined by compare_bookmark(). 296 */ 297 ASSERT(compare_bookmark(zb, ezb, dnp, th->th_advance) <= 0); 298 ASSERT(compare_bookmark(zb, szb, dnp, th->th_advance) == 0); 299 ASSERT(compare_bookmark(lzb, zb, dnp, th->th_advance) < 0 || 300 lzb->zb_level == ZB_NO_LEVEL); 301 *lzb = *zb; 302 } 303 304 th->th_callbacks++; 305 return (th->th_func(bc, th->th_spa, th->th_arg)); 306 } 307 308 static int 309 traverse_read(traverse_handle_t *th, traverse_blk_cache_t *bc, blkptr_t *bp, 310 dnode_phys_t *dnp) 311 { 312 zbookmark_t *zb = &bc->bc_bookmark; 313 int error; 314 315 th->th_hits++; 316 317 bc->bc_dnode = dnp; 318 bc->bc_errno = 0; 319 320 if (BP_EQUAL(&bc->bc_blkptr, bp)) 321 return (0); 322 323 bc->bc_blkptr = *bp; 324 325 if (bc->bc_data == NULL) 326 return (0); 327 328 if (BP_IS_HOLE(bp)) { 329 ASSERT(th->th_advance & ADVANCE_HOLES); 330 return (0); 331 } 332 333 if (compare_bookmark(zb, &th->th_noread, dnp, 0) == 0) { 334 error = EIO; 335 } else if (arc_tryread(th->th_spa, bp, bc->bc_data) == 0) { 336 error = 0; 337 th->th_arc_hits++; 338 } else { 339 error = zio_wait(zio_read(NULL, th->th_spa, bp, bc->bc_data, 340 BP_GET_LSIZE(bp), NULL, NULL, ZIO_PRIORITY_SYNC_READ, 341 th->th_zio_flags | ZIO_FLAG_DONT_CACHE, zb)); 342 343 if (BP_SHOULD_BYTESWAP(bp) && error == 0) 344 (zb->zb_level > 0 ? byteswap_uint64_array : 345 dmu_ot[BP_GET_TYPE(bp)].ot_byteswap)(bc->bc_data, 346 BP_GET_LSIZE(bp)); 347 th->th_reads++; 348 } 349 350 if (error) { 351 bc->bc_errno = error; 352 error = traverse_callback(th, NULL, bc); 353 ASSERT(error == EAGAIN || error == EINTR || error == ERESTART); 354 bc->bc_blkptr.blk_birth = -1ULL; 355 } 356 357 dprintf("cache %02x error %d <%llu, %llu, %d, %llx>\n", 358 bc - &th->th_cache[0][0], error, 359 zb->zb_objset, zb->zb_object, zb->zb_level, zb->zb_blkid); 360 361 return (error); 362 } 363 364 static int 365 find_block(traverse_handle_t *th, zseg_t *zseg, dnode_phys_t *dnp, int depth) 366 { 367 zbookmark_t *zb = &zseg->seg_start; 368 traverse_blk_cache_t *bc; 369 blkptr_t *bp = dnp->dn_blkptr; 370 int i, first, level; 371 int nbp = dnp->dn_nblkptr; 372 int minlevel = zb->zb_level; 373 int maxlevel = dnp->dn_nlevels - 1; 374 int wshift = dnp->dn_indblkshift - SPA_BLKPTRSHIFT; 375 int bp_shift = BP_SPAN_SHIFT(maxlevel - minlevel, wshift); 376 uint64_t blkid = zb->zb_blkid >> bp_shift; 377 int do_holes = (th->th_advance & ADVANCE_HOLES) && depth == ZB_DN_CACHE; 378 int rc; 379 380 if (minlevel > maxlevel || blkid >= nbp) 381 return (ERANGE); 382 383 for (level = maxlevel; level >= minlevel; level--) { 384 first = P2PHASE(blkid, 1ULL << wshift); 385 386 for (i = first; i < nbp; i++) 387 if (bp[i].blk_birth > zseg->seg_mintxg || 388 BP_IS_HOLE(&bp[i]) && do_holes) 389 break; 390 391 if (i != first) { 392 i--; 393 SET_BOOKMARK_LB(zb, level, blkid + (i - first)); 394 return (ENOTBLK); 395 } 396 397 bc = &th->th_cache[depth][level]; 398 399 SET_BOOKMARK(&bc->bc_bookmark, zb->zb_objset, zb->zb_object, 400 level, blkid); 401 402 if (rc = traverse_read(th, bc, bp + i, dnp)) { 403 if (rc != EAGAIN) { 404 SET_BOOKMARK_LB(zb, level, blkid); 405 } 406 return (rc); 407 } 408 409 if (BP_IS_HOLE(&bp[i])) { 410 SET_BOOKMARK_LB(zb, level, blkid); 411 th->th_lastcb.zb_level = ZB_NO_LEVEL; 412 return (0); 413 } 414 415 nbp = 1 << wshift; 416 bp = bc->bc_data; 417 bp_shift -= wshift; 418 blkid = zb->zb_blkid >> bp_shift; 419 } 420 421 return (0); 422 } 423 424 static int 425 get_dnode(traverse_handle_t *th, uint64_t objset, dnode_phys_t *mdn, 426 uint64_t *objectp, dnode_phys_t **dnpp, uint64_t txg, int type, int depth) 427 { 428 zseg_t zseg; 429 zbookmark_t *zb = &zseg.seg_start; 430 uint64_t object = *objectp; 431 int i, rc; 432 433 SET_BOOKMARK(zb, objset, 0, 0, object / DNODES_PER_BLOCK); 434 SET_BOOKMARK(&zseg.seg_end, objset, 0, 0, ZB_MAXBLKID); 435 436 zseg.seg_mintxg = txg; 437 zseg.seg_maxtxg = -1ULL; 438 439 for (;;) { 440 rc = find_block(th, &zseg, mdn, depth); 441 442 if (rc == EAGAIN || rc == EINTR || rc == ERANGE) 443 break; 444 445 if (rc == 0 && zb->zb_level == 0) { 446 dnode_phys_t *dnp = th->th_cache[depth][0].bc_data; 447 for (i = 0; i < DNODES_PER_BLOCK; i++) { 448 object = (zb->zb_blkid * DNODES_PER_BLOCK) + i; 449 if (object >= *objectp && 450 dnp[i].dn_type != DMU_OT_NONE && 451 (type == -1 || dnp[i].dn_type == type)) { 452 *objectp = object; 453 *dnpp = &dnp[i]; 454 return (0); 455 } 456 } 457 } 458 459 rc = advance_block(&zseg, mdn, rc, ADVANCE_PRE); 460 461 if (rc == ERANGE) 462 break; 463 } 464 465 if (rc == ERANGE) 466 *objectp = ZB_MAXOBJECT; 467 468 return (rc); 469 } 470 471 /* ARGSUSED */ 472 static void 473 traverse_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg) 474 { 475 traverse_handle_t *th = arg; 476 traverse_blk_cache_t *bc = &th->th_zil_cache; 477 zbookmark_t *zb = &bc->bc_bookmark; 478 zseg_t *zseg = list_head(&th->th_seglist); 479 480 if (bp->blk_birth <= zseg->seg_mintxg || 481 bp->blk_birth >= zseg->seg_maxtxg) 482 return; 483 484 if (claim_txg != 0 || bp->blk_birth < spa_first_txg(th->th_spa)) { 485 zb->zb_object = 0; 486 zb->zb_blkid = bp->blk_cksum.zc_word[3]; 487 bc->bc_blkptr = *bp; 488 (void) th->th_func(bc, th->th_spa, th->th_arg); 489 } 490 } 491 492 /* ARGSUSED */ 493 static void 494 traverse_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg) 495 { 496 traverse_handle_t *th = arg; 497 traverse_blk_cache_t *bc = &th->th_zil_cache; 498 zbookmark_t *zb = &bc->bc_bookmark; 499 zseg_t *zseg = list_head(&th->th_seglist); 500 501 if (lrc->lrc_txtype == TX_WRITE) { 502 lr_write_t *lr = (lr_write_t *)lrc; 503 blkptr_t *bp = &lr->lr_blkptr; 504 505 if (bp->blk_birth <= zseg->seg_mintxg || 506 bp->blk_birth >= zseg->seg_maxtxg) 507 return; 508 509 if (claim_txg != 0 && bp->blk_birth >= claim_txg) { 510 zb->zb_object = lr->lr_foid; 511 zb->zb_blkid = lr->lr_offset / BP_GET_LSIZE(bp); 512 bc->bc_blkptr = *bp; 513 (void) th->th_func(bc, th->th_spa, th->th_arg); 514 } 515 } 516 } 517 518 static void 519 traverse_zil(traverse_handle_t *th, traverse_blk_cache_t *bc) 520 { 521 spa_t *spa = th->th_spa; 522 dsl_pool_t *dp = spa_get_dsl(spa); 523 objset_phys_t *osphys = bc->bc_data; 524 zil_header_t *zh = &osphys->os_zil_header; 525 uint64_t claim_txg = zh->zh_claim_txg; 526 zilog_t *zilog; 527 528 ASSERT(bc == &th->th_cache[ZB_MDN_CACHE][ZB_MAXLEVEL - 1]); 529 ASSERT(bc->bc_bookmark.zb_level == -1); 530 531 /* 532 * We only want to visit blocks that have been claimed but not yet 533 * replayed (or, in read-only mode, blocks that *would* be claimed). 534 */ 535 if (claim_txg == 0 && (spa_mode & FWRITE)) 536 return; 537 538 th->th_zil_cache.bc_bookmark = bc->bc_bookmark; 539 540 zilog = zil_alloc(dp->dp_meta_objset, zh); 541 542 zil_parse(zilog, traverse_zil_block, traverse_zil_record, th, 543 claim_txg); 544 545 zil_free(zilog); 546 } 547 548 static int 549 traverse_segment(traverse_handle_t *th, zseg_t *zseg, blkptr_t *mosbp) 550 { 551 zbookmark_t *zb = &zseg->seg_start; 552 traverse_blk_cache_t *bc; 553 dnode_phys_t *dn, *dn_tmp; 554 int worklimit = 100; 555 int rc; 556 557 dprintf("<%llu, %llu, %d, %llx>\n", 558 zb->zb_objset, zb->zb_object, zb->zb_level, zb->zb_blkid); 559 560 bc = &th->th_cache[ZB_MOS_CACHE][ZB_MAXLEVEL - 1]; 561 dn = &((objset_phys_t *)bc->bc_data)->os_meta_dnode; 562 563 SET_BOOKMARK(&bc->bc_bookmark, 0, 0, -1, 0); 564 565 rc = traverse_read(th, bc, mosbp, dn); 566 567 if (rc) /* If we get ERESTART, we've got nowhere left to go */ 568 return (rc == ERESTART ? EINTR : rc); 569 570 ASSERT(dn->dn_nlevels < ZB_MAXLEVEL); 571 572 if (zb->zb_objset != 0) { 573 uint64_t objset = zb->zb_objset; 574 dsl_dataset_phys_t *dsp; 575 576 rc = get_dnode(th, 0, dn, &objset, &dn_tmp, 0, 577 DMU_OT_DSL_DATASET, ZB_MOS_CACHE); 578 579 if (objset != zb->zb_objset) 580 rc = advance_objset(zseg, objset, th->th_advance); 581 582 if (rc != 0) 583 return (rc); 584 585 dsp = DN_BONUS(dn_tmp); 586 587 bc = &th->th_cache[ZB_MDN_CACHE][ZB_MAXLEVEL - 1]; 588 dn = &((objset_phys_t *)bc->bc_data)->os_meta_dnode; 589 590 SET_BOOKMARK(&bc->bc_bookmark, objset, 0, -1, 0); 591 592 rc = traverse_read(th, bc, &dsp->ds_bp, dn); 593 594 if (rc != 0) { 595 if (rc == ERESTART) 596 rc = advance_objset(zseg, zb->zb_objset + 1, 597 th->th_advance); 598 return (rc); 599 } 600 601 if (th->th_advance & ADVANCE_PRUNE) 602 zseg->seg_mintxg = 603 MAX(zseg->seg_mintxg, dsp->ds_prev_snap_txg); 604 } 605 606 if (zb->zb_level == -1) { 607 ASSERT(zb->zb_object == 0); 608 ASSERT(zb->zb_blkid == 0); 609 ASSERT(BP_GET_TYPE(&bc->bc_blkptr) == DMU_OT_OBJSET); 610 611 if (bc->bc_blkptr.blk_birth > zseg->seg_mintxg) { 612 rc = traverse_callback(th, zseg, bc); 613 if (rc) { 614 ASSERT(rc == EINTR); 615 return (rc); 616 } 617 if ((th->th_advance & ADVANCE_ZIL) && 618 zb->zb_objset != 0) 619 traverse_zil(th, bc); 620 } 621 622 return (advance_from_osphys(zseg, th->th_advance)); 623 } 624 625 if (zb->zb_object != 0) { 626 uint64_t object = zb->zb_object; 627 628 rc = get_dnode(th, zb->zb_objset, dn, &object, &dn_tmp, 629 zseg->seg_mintxg, -1, ZB_MDN_CACHE); 630 631 if (object != zb->zb_object) 632 rc = advance_object(zseg, object, th->th_advance); 633 634 if (rc != 0) 635 return (rc); 636 637 dn = dn_tmp; 638 } 639 640 if (zb->zb_level == ZB_MAXLEVEL) 641 zb->zb_level = dn->dn_nlevels - 1; 642 643 for (;;) { 644 rc = find_block(th, zseg, dn, ZB_DN_CACHE); 645 646 if (rc == EAGAIN || rc == EINTR || rc == ERANGE) 647 break; 648 649 if (rc == 0) { 650 bc = &th->th_cache[ZB_DN_CACHE][zb->zb_level]; 651 ASSERT(bc->bc_dnode == dn); 652 ASSERT(bc->bc_blkptr.blk_birth <= mosbp->blk_birth); 653 rc = traverse_callback(th, zseg, bc); 654 if (rc) { 655 ASSERT(rc == EINTR); 656 return (rc); 657 } 658 if (BP_IS_HOLE(&bc->bc_blkptr)) { 659 ASSERT(th->th_advance & ADVANCE_HOLES); 660 rc = ENOTBLK; 661 } 662 } 663 664 rc = advance_block(zseg, dn, rc, th->th_advance); 665 666 if (rc == ERANGE) 667 break; 668 669 /* 670 * Give spa_sync() a chance to run. 671 */ 672 if (spa_traverse_wanted(th->th_spa)) { 673 th->th_syncs++; 674 return (EAGAIN); 675 } 676 677 if (--worklimit == 0) 678 return (EAGAIN); 679 } 680 681 if (rc == ERANGE) 682 rc = advance_object(zseg, zb->zb_object + 1, th->th_advance); 683 684 return (rc); 685 } 686 687 /* 688 * It is the caller's responsibility to ensure that the dsl_dataset_t 689 * doesn't go away during traversal. 690 */ 691 int 692 traverse_dsl_dataset(dsl_dataset_t *ds, uint64_t txg_start, int advance, 693 blkptr_cb_t func, void *arg) 694 { 695 spa_t *spa = ds->ds_dir->dd_pool->dp_spa; 696 traverse_handle_t *th; 697 int err; 698 699 th = traverse_init(spa, func, arg, advance, ZIO_FLAG_MUSTSUCCEED); 700 701 traverse_add_objset(th, txg_start, -1ULL, ds->ds_object); 702 703 while ((err = traverse_more(th)) == EAGAIN) 704 continue; 705 706 traverse_fini(th); 707 return (err); 708 } 709 710 int 711 traverse_more(traverse_handle_t *th) 712 { 713 zseg_t *zseg = list_head(&th->th_seglist); 714 uint64_t save_txg; /* XXX won't be necessary with real itinerary */ 715 krwlock_t *rw = spa_traverse_rwlock(th->th_spa); 716 blkptr_t *mosbp = spa_get_rootblkptr(th->th_spa); 717 int rc; 718 719 if (zseg == NULL) 720 return (0); 721 722 th->th_restarts++; 723 724 save_txg = zseg->seg_mintxg; 725 726 if (!(th->th_advance & ADVANCE_NOLOCK)) 727 rw_enter(rw, RW_READER); 728 729 rc = traverse_segment(th, zseg, mosbp); 730 ASSERT(rc == ERANGE || rc == EAGAIN || rc == EINTR); 731 732 if (!(th->th_advance & ADVANCE_NOLOCK)) 733 rw_exit(rw); 734 735 zseg->seg_mintxg = save_txg; 736 737 if (rc == ERANGE) { 738 list_remove(&th->th_seglist, zseg); 739 kmem_free(zseg, sizeof (*zseg)); 740 return (EAGAIN); 741 } 742 743 return (rc); 744 } 745 746 /* 747 * Note: (mintxg, maxtxg) is an open interval; mintxg and maxtxg themselves 748 * are not included. The blocks covered by this segment will all have 749 * mintxg < birth < maxtxg. 750 */ 751 static void 752 traverse_add_segment(traverse_handle_t *th, uint64_t mintxg, uint64_t maxtxg, 753 uint64_t sobjset, uint64_t sobject, int slevel, uint64_t sblkid, 754 uint64_t eobjset, uint64_t eobject, int elevel, uint64_t eblkid) 755 { 756 zseg_t *zseg; 757 758 zseg = kmem_alloc(sizeof (zseg_t), KM_SLEEP); 759 760 zseg->seg_mintxg = mintxg; 761 zseg->seg_maxtxg = maxtxg; 762 763 zseg->seg_start.zb_objset = sobjset; 764 zseg->seg_start.zb_object = sobject; 765 zseg->seg_start.zb_level = slevel; 766 zseg->seg_start.zb_blkid = sblkid; 767 768 zseg->seg_end.zb_objset = eobjset; 769 zseg->seg_end.zb_object = eobject; 770 zseg->seg_end.zb_level = elevel; 771 zseg->seg_end.zb_blkid = eblkid; 772 773 list_insert_tail(&th->th_seglist, zseg); 774 } 775 776 void 777 traverse_add_dnode(traverse_handle_t *th, uint64_t mintxg, uint64_t maxtxg, 778 uint64_t objset, uint64_t object) 779 { 780 if (th->th_advance & ADVANCE_PRE) 781 traverse_add_segment(th, mintxg, maxtxg, 782 objset, object, ZB_MAXLEVEL, 0, 783 objset, object, 0, ZB_MAXBLKID); 784 else 785 traverse_add_segment(th, mintxg, maxtxg, 786 objset, object, 0, 0, 787 objset, object, 0, ZB_MAXBLKID); 788 } 789 790 void 791 traverse_add_objset(traverse_handle_t *th, uint64_t mintxg, uint64_t maxtxg, 792 uint64_t objset) 793 { 794 if (th->th_advance & ADVANCE_PRE) 795 traverse_add_segment(th, mintxg, maxtxg, 796 objset, 0, -1, 0, 797 objset, ZB_MAXOBJECT, 0, ZB_MAXBLKID); 798 else 799 traverse_add_segment(th, mintxg, maxtxg, 800 objset, 1, 0, 0, 801 objset, 0, -1, 0); 802 } 803 804 void 805 traverse_add_pool(traverse_handle_t *th, uint64_t mintxg, uint64_t maxtxg) 806 { 807 if (th->th_advance & ADVANCE_PRE) 808 traverse_add_segment(th, mintxg, maxtxg, 809 0, 0, -1, 0, 810 ZB_MAXOBJSET, ZB_MAXOBJECT, 0, ZB_MAXBLKID); 811 else 812 traverse_add_segment(th, mintxg, maxtxg, 813 1, 1, 0, 0, 814 0, 0, -1, 0); 815 } 816 817 traverse_handle_t * 818 traverse_init(spa_t *spa, blkptr_cb_t func, void *arg, int advance, 819 int zio_flags) 820 { 821 traverse_handle_t *th; 822 int d, l; 823 824 th = kmem_zalloc(sizeof (*th), KM_SLEEP); 825 826 th->th_spa = spa; 827 th->th_func = func; 828 th->th_arg = arg; 829 th->th_advance = advance; 830 th->th_lastcb.zb_level = ZB_NO_LEVEL; 831 th->th_noread.zb_level = ZB_NO_LEVEL; 832 th->th_zio_flags = zio_flags; 833 834 list_create(&th->th_seglist, sizeof (zseg_t), 835 offsetof(zseg_t, seg_node)); 836 837 for (d = 0; d < ZB_DEPTH; d++) { 838 for (l = 0; l < ZB_MAXLEVEL; l++) { 839 if ((advance & ADVANCE_DATA) || 840 l != 0 || d != ZB_DN_CACHE) 841 th->th_cache[d][l].bc_data = 842 zio_buf_alloc(SPA_MAXBLOCKSIZE); 843 } 844 } 845 846 return (th); 847 } 848 849 void 850 traverse_fini(traverse_handle_t *th) 851 { 852 int d, l; 853 zseg_t *zseg; 854 855 for (d = 0; d < ZB_DEPTH; d++) 856 for (l = 0; l < ZB_MAXLEVEL; l++) 857 if (th->th_cache[d][l].bc_data != NULL) 858 zio_buf_free(th->th_cache[d][l].bc_data, 859 SPA_MAXBLOCKSIZE); 860 861 while ((zseg = list_head(&th->th_seglist)) != NULL) { 862 list_remove(&th->th_seglist, zseg); 863 kmem_free(zseg, sizeof (*zseg)); 864 } 865 866 list_destroy(&th->th_seglist); 867 868 dprintf("%llu hit, %llu ARC, %llu IO, %llu cb, %llu sync, %llu again\n", 869 th->th_hits, th->th_arc_hits, th->th_reads, th->th_callbacks, 870 th->th_syncs, th->th_restarts); 871 872 kmem_free(th, sizeof (*th)); 873 } 874