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.
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.
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]
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
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>
37 #include <sys/dmu_impl.h>
39 #define BP_SPAN_SHIFT(level, width) ((level) * (width))
41 #define BP_EQUAL(b1, b2) \
42 (DVA_EQUAL(BP_IDENTITY(b1), BP_IDENTITY(b2)) && \
43 (b1)->blk_birth == (b2)->blk_birth)
46 * Compare two bookmarks.
48 * For ADVANCE_PRE, the visitation order is:
50 * objset 0, 1, 2, ..., ZB_MAXOBJSET.
51 * object 0, 1, 2, ..., ZB_MAXOBJECT.
53 * level ZB_MAXLEVEL, ..., 2, 1, 0.
55 * where blkoff = blkid << BP_SPAN_SHIFT(level, width), and thus a valid
58 * < objset, object, blkoff, -level >
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:
64 * objset 1, 2, ..., ZB_MAXOBJSET, 0.
65 * object 1, 2, ..., ZB_MAXOBJECT, 0.
67 * level 0, 1, 2, ..., ZB_MAXLEVEL.
69 * and thus a valid ordering vector is:
71 * < objset - 1, object - 1, blkoff, level >
73 * Both orderings can be expressed as:
75 * < objset + bias, object + bias, blkoff, level ^ bias >
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).
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.
85 compare_bookmark(zbookmark_t *szb, zbookmark_t *ezb, dnode_phys_t *dnp,
88 int bias = (advance & ADVANCE_PRE) ? 0 : -1;
89 uint64_t sblkoff, eblkoff;
90 int slevel, elevel, wshift;
92 if (szb->zb_objset + bias < ezb->zb_objset + bias)
95 if (szb->zb_objset + bias > ezb->zb_objset + bias)
98 slevel = szb->zb_level;
99 elevel = ezb->zb_level;
101 if ((slevel | elevel) < 0)
102 return ((slevel ^ bias) - (elevel ^ bias));
104 if (szb->zb_object + bias < ezb->zb_object + bias)
107 if (szb->zb_object + bias > ezb->zb_object + bias)
113 wshift = dnp->dn_indblkshift - SPA_BLKPTRSHIFT;
115 sblkoff = (szb->zb_blkid - bias) << BP_SPAN_SHIFT(slevel, wshift);
116 eblkoff = (ezb->zb_blkid - bias) << BP_SPAN_SHIFT(elevel, wshift);
118 if (sblkoff < eblkoff)
121 if (sblkoff > eblkoff)
124 return ((elevel ^ bias) - (slevel ^ bias));
127 #define SET_BOOKMARK(zb, objset, object, level, blkid) \
129 (zb)->zb_objset = objset; \
130 (zb)->zb_object = object; \
131 (zb)->zb_level = level; \
132 (zb)->zb_blkid = blkid; \
135 #define SET_BOOKMARK_LB(zb, level, blkid) \
137 (zb)->zb_level = level; \
138 (zb)->zb_blkid = blkid; \
142 advance_objset(zseg_t *zseg, uint64_t objset, int advance)
144 zbookmark_t *zb = &zseg->seg_start;
146 if (advance & ADVANCE_PRE) {
147 if (objset >= ZB_MAXOBJSET)
149 SET_BOOKMARK(zb, objset, 0, -1, 0);
151 if (objset >= ZB_MAXOBJSET)
153 SET_BOOKMARK(zb, objset, 1, 0, 0);
156 if (compare_bookmark(zb, &zseg->seg_end, NULL, advance) > 0)
163 advance_object(zseg_t *zseg, uint64_t object, int advance)
165 zbookmark_t *zb = &zseg->seg_start;
167 if (advance & ADVANCE_PRE) {
168 if (object >= ZB_MAXOBJECT) {
169 SET_BOOKMARK(zb, zb->zb_objset + 1, 0, -1, 0);
171 SET_BOOKMARK(zb, zb->zb_objset, object, ZB_MAXLEVEL, 0);
174 if (zb->zb_object == 0) {
175 SET_BOOKMARK(zb, zb->zb_objset, 0, -1, 0);
177 if (object >= ZB_MAXOBJECT)
179 SET_BOOKMARK(zb, zb->zb_objset, object, 0, 0);
183 if (compare_bookmark(zb, &zseg->seg_end, NULL, advance) > 0)
190 advance_from_osphys(zseg_t *zseg, int advance)
192 zbookmark_t *zb = &zseg->seg_start;
194 ASSERT(zb->zb_object == 0);
195 ASSERT(zb->zb_level == -1);
196 ASSERT(zb->zb_blkid == 0);
198 if (advance & ADVANCE_PRE) {
199 SET_BOOKMARK_LB(zb, ZB_MAXLEVEL, 0);
201 if (zb->zb_objset == 0)
203 SET_BOOKMARK(zb, zb->zb_objset + 1, 1, 0, 0);
206 if (compare_bookmark(zb, &zseg->seg_end, NULL, advance) > 0)
213 advance_block(zseg_t *zseg, dnode_phys_t *dnp, int rc, int advance)
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;
221 if (advance & ADVANCE_PRE) {
222 if (level > 0 && rc == 0) {
228 if ((blkid << BP_SPAN_SHIFT(level, wshift)) >
232 while (level < maxlevel) {
233 if (P2PHASE(blkid, 1ULL << wshift))
240 if (level >= maxlevel || P2PHASE(blkid + 1, 1ULL << wshift)) {
241 blkid = (blkid + 1) << BP_SPAN_SHIFT(level, wshift);
248 while ((blkid << BP_SPAN_SHIFT(level, wshift)) >
250 if (level == maxlevel)
256 SET_BOOKMARK_LB(zb, level, blkid);
258 if (compare_bookmark(zb, &zseg->seg_end, dnp, advance) > 0)
265 traverse_callback(traverse_handle_t *th, zseg_t *zseg, traverse_blk_cache_t *bc)
268 * Before we issue the callback, prune against maxtxg.
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.
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.
280 * Therefore, in the name of simplicity we don't prune against
281 * maxtxg until the last possible moment -- that being right now.
283 if (bc->bc_errno == 0 && bc->bc_blkptr.blk_birth >= zseg->seg_maxtxg)
287 * Debugging: verify that the order we visit things agrees with the
288 * order defined by compare_bookmark(). We don't check this for
289 * log blocks because there's no defined ordering for them; they're
290 * always visited (or not) as part of visiting the objset_phys_t.
292 if (bc->bc_errno == 0 && bc != &th->th_zil_cache) {
293 zbookmark_t *zb = &bc->bc_bookmark;
294 zbookmark_t *szb = &zseg->seg_start;
295 zbookmark_t *ezb = &zseg->seg_end;
296 zbookmark_t *lzb = &th->th_lastcb;
297 dnode_phys_t *dnp = bc->bc_dnode;
299 ASSERT(compare_bookmark(zb, ezb, dnp, th->th_advance) <= 0);
300 ASSERT(compare_bookmark(zb, szb, dnp, th->th_advance) == 0);
301 ASSERT(compare_bookmark(lzb, zb, dnp, th->th_advance) < 0 ||
302 lzb->zb_level == ZB_NO_LEVEL);
307 return (th->th_func(bc, th->th_spa, th->th_arg));
311 traverse_read(traverse_handle_t *th, traverse_blk_cache_t *bc, blkptr_t *bp,
314 zbookmark_t *zb = &bc->bc_bookmark;
322 if (BP_EQUAL(&bc->bc_blkptr, bp))
327 if (bc->bc_data == NULL)
330 if (BP_IS_HOLE(bp)) {
331 ASSERT(th->th_advance & ADVANCE_HOLES);
335 if (compare_bookmark(zb, &th->th_noread, dnp, 0) == 0) {
337 } else if (arc_tryread(th->th_spa, bp, bc->bc_data) == 0) {
341 error = zio_wait(zio_read(NULL, th->th_spa, bp, bc->bc_data,
342 BP_GET_LSIZE(bp), NULL, NULL, ZIO_PRIORITY_SYNC_READ,
343 th->th_zio_flags | ZIO_FLAG_DONT_CACHE, zb));
345 if (BP_SHOULD_BYTESWAP(bp) && error == 0)
346 (zb->zb_level > 0 ? byteswap_uint64_array :
347 dmu_ot[BP_GET_TYPE(bp)].ot_byteswap)(bc->bc_data,
353 bc->bc_errno = error;
354 error = traverse_callback(th, NULL, bc);
355 ASSERT(error == EAGAIN || error == EINTR || error == ERESTART);
356 bc->bc_blkptr.blk_birth = -1ULL;
359 dprintf("cache %02x error %d <%llu, %llu, %d, %llx>\n",
360 bc - &th->th_cache[0][0], error,
361 zb->zb_objset, zb->zb_object, zb->zb_level, zb->zb_blkid);
367 find_block(traverse_handle_t *th, zseg_t *zseg, dnode_phys_t *dnp, int depth)
369 zbookmark_t *zb = &zseg->seg_start;
370 traverse_blk_cache_t *bc;
371 blkptr_t *bp = dnp->dn_blkptr;
373 int nbp = dnp->dn_nblkptr;
374 int minlevel = zb->zb_level;
375 int maxlevel = dnp->dn_nlevels - 1;
376 int wshift = dnp->dn_indblkshift - SPA_BLKPTRSHIFT;
377 int bp_shift = BP_SPAN_SHIFT(maxlevel - minlevel, wshift);
378 uint64_t blkid = zb->zb_blkid >> bp_shift;
379 int do_holes = (th->th_advance & ADVANCE_HOLES) && depth == ZB_DN_CACHE;
382 if (minlevel > maxlevel || blkid >= nbp)
385 for (level = maxlevel; level >= minlevel; level--) {
386 first = P2PHASE(blkid, 1ULL << wshift);
388 for (i = first; i < nbp; i++)
389 if (bp[i].blk_birth > zseg->seg_mintxg ||
390 BP_IS_HOLE(&bp[i]) && do_holes)
395 SET_BOOKMARK_LB(zb, level, blkid + (i - first));
399 bc = &th->th_cache[depth][level];
401 SET_BOOKMARK(&bc->bc_bookmark, zb->zb_objset, zb->zb_object,
404 if (rc = traverse_read(th, bc, bp + i, dnp)) {
406 SET_BOOKMARK_LB(zb, level, blkid);
411 if (BP_IS_HOLE(&bp[i])) {
412 SET_BOOKMARK_LB(zb, level, blkid);
413 th->th_lastcb.zb_level = ZB_NO_LEVEL;
420 blkid = zb->zb_blkid >> bp_shift;
427 get_dnode(traverse_handle_t *th, uint64_t objset, dnode_phys_t *mdn,
428 uint64_t *objectp, dnode_phys_t **dnpp, uint64_t txg, int type, int depth)
431 zbookmark_t *zb = &zseg.seg_start;
432 uint64_t object = *objectp;
435 SET_BOOKMARK(zb, objset, 0, 0, object / DNODES_PER_BLOCK);
436 SET_BOOKMARK(&zseg.seg_end, objset, 0, 0, ZB_MAXBLKID);
438 zseg.seg_mintxg = txg;
439 zseg.seg_maxtxg = -1ULL;
442 rc = find_block(th, &zseg, mdn, depth);
444 if (rc == EAGAIN || rc == EINTR || rc == ERANGE)
447 if (rc == 0 && zb->zb_level == 0) {
448 dnode_phys_t *dnp = th->th_cache[depth][0].bc_data;
449 for (i = 0; i < DNODES_PER_BLOCK; i++) {
450 object = (zb->zb_blkid * DNODES_PER_BLOCK) + i;
451 if (object >= *objectp &&
452 dnp[i].dn_type != DMU_OT_NONE &&
453 (type == -1 || dnp[i].dn_type == type)) {
461 rc = advance_block(&zseg, mdn, rc, ADVANCE_PRE);
468 *objectp = ZB_MAXOBJECT;
475 traverse_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
477 traverse_handle_t *th = arg;
478 traverse_blk_cache_t *bc = &th->th_zil_cache;
479 zbookmark_t *zb = &bc->bc_bookmark;
480 zseg_t *zseg = list_head(&th->th_seglist);
482 if (bp->blk_birth <= zseg->seg_mintxg)
485 if (claim_txg != 0 || bp->blk_birth < spa_first_txg(th->th_spa)) {
487 zb->zb_blkid = bp->blk_cksum.zc_word[ZIL_ZC_SEQ];
489 (void) traverse_callback(th, zseg, bc);
495 traverse_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg)
497 traverse_handle_t *th = arg;
498 traverse_blk_cache_t *bc = &th->th_zil_cache;
499 zbookmark_t *zb = &bc->bc_bookmark;
500 zseg_t *zseg = list_head(&th->th_seglist);
502 if (lrc->lrc_txtype == TX_WRITE) {
503 lr_write_t *lr = (lr_write_t *)lrc;
504 blkptr_t *bp = &lr->lr_blkptr;
506 if (bp->blk_birth <= zseg->seg_mintxg)
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);
513 (void) traverse_callback(th, zseg, bc);
519 traverse_zil(traverse_handle_t *th, traverse_blk_cache_t *bc)
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;
528 ASSERT(bc == &th->th_cache[ZB_MDN_CACHE][ZB_MAXLEVEL - 1]);
529 ASSERT(bc->bc_bookmark.zb_level == -1);
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).
535 if (claim_txg == 0 && (spa_mode & FWRITE))
538 th->th_zil_cache.bc_bookmark = bc->bc_bookmark;
540 zilog = zil_alloc(dp->dp_meta_objset, zh);
542 (void) zil_parse(zilog, traverse_zil_block, traverse_zil_record, th,
549 traverse_segment(traverse_handle_t *th, zseg_t *zseg, blkptr_t *mosbp)
551 zbookmark_t *zb = &zseg->seg_start;
552 traverse_blk_cache_t *bc;
553 dnode_phys_t *dn, *dn_tmp;
557 dprintf("<%llu, %llu, %d, %llx>\n",
558 zb->zb_objset, zb->zb_object, zb->zb_level, zb->zb_blkid);
560 bc = &th->th_cache[ZB_MOS_CACHE][ZB_MAXLEVEL - 1];
561 dn = &((objset_phys_t *)bc->bc_data)->os_meta_dnode;
563 SET_BOOKMARK(&bc->bc_bookmark, 0, 0, -1, 0);
565 rc = traverse_read(th, bc, mosbp, dn);
567 if (rc) /* If we get ERESTART, we've got nowhere left to go */
568 return (rc == ERESTART ? EINTR : rc);
570 ASSERT(dn->dn_nlevels < ZB_MAXLEVEL);
572 if (zb->zb_objset != 0) {
573 uint64_t objset = zb->zb_objset;
574 dsl_dataset_phys_t *dsp;
576 rc = get_dnode(th, 0, dn, &objset, &dn_tmp, 0,
577 DMU_OT_DSL_DATASET, ZB_MOS_CACHE);
579 if (objset != zb->zb_objset)
580 rc = advance_objset(zseg, objset, th->th_advance);
585 dsp = DN_BONUS(dn_tmp);
587 bc = &th->th_cache[ZB_MDN_CACHE][ZB_MAXLEVEL - 1];
588 dn = &((objset_phys_t *)bc->bc_data)->os_meta_dnode;
590 SET_BOOKMARK(&bc->bc_bookmark, objset, 0, -1, 0);
593 * If we're traversing an open snapshot, we know that it
594 * can't be deleted (because it's open) and it can't change
595 * (because it's a snapshot). Therefore, once we've gotten
596 * from the uberblock down to the snapshot's objset_phys_t,
597 * we no longer need to synchronize with spa_sync(); we're
598 * traversing a completely static block tree from here on.
600 if (th->th_advance & ADVANCE_NOLOCK) {
601 ASSERT(th->th_locked);
602 rw_exit(spa_traverse_rwlock(th->th_spa));
606 rc = traverse_read(th, bc, &dsp->ds_bp, dn);
610 rc = advance_objset(zseg, zb->zb_objset + 1,
615 if (th->th_advance & ADVANCE_PRUNE)
617 MAX(zseg->seg_mintxg, dsp->ds_prev_snap_txg);
620 if (zb->zb_level == -1) {
621 ASSERT(zb->zb_object == 0);
622 ASSERT(zb->zb_blkid == 0);
623 ASSERT(BP_GET_TYPE(&bc->bc_blkptr) == DMU_OT_OBJSET);
625 if (bc->bc_blkptr.blk_birth > zseg->seg_mintxg) {
626 rc = traverse_callback(th, zseg, bc);
631 if ((th->th_advance & ADVANCE_ZIL) &&
633 traverse_zil(th, bc);
636 return (advance_from_osphys(zseg, th->th_advance));
639 if (zb->zb_object != 0) {
640 uint64_t object = zb->zb_object;
642 rc = get_dnode(th, zb->zb_objset, dn, &object, &dn_tmp,
643 zseg->seg_mintxg, -1, ZB_MDN_CACHE);
645 if (object != zb->zb_object)
646 rc = advance_object(zseg, object, th->th_advance);
654 if (zb->zb_level == ZB_MAXLEVEL)
655 zb->zb_level = dn->dn_nlevels - 1;
658 rc = find_block(th, zseg, dn, ZB_DN_CACHE);
660 if (rc == EAGAIN || rc == EINTR || rc == ERANGE)
664 bc = &th->th_cache[ZB_DN_CACHE][zb->zb_level];
665 ASSERT(bc->bc_dnode == dn);
666 ASSERT(bc->bc_blkptr.blk_birth <= mosbp->blk_birth);
667 rc = traverse_callback(th, zseg, bc);
672 if (BP_IS_HOLE(&bc->bc_blkptr)) {
673 ASSERT(th->th_advance & ADVANCE_HOLES);
678 rc = advance_block(zseg, dn, rc, th->th_advance);
684 * Give spa_sync() a chance to run.
686 if (th->th_locked && spa_traverse_wanted(th->th_spa)) {
691 if (--worklimit == 0)
696 rc = advance_object(zseg, zb->zb_object + 1, th->th_advance);
702 * It is the caller's responsibility to ensure that the dsl_dataset_t
703 * doesn't go away during traversal.
706 traverse_dsl_dataset(dsl_dataset_t *ds, uint64_t txg_start, int advance,
707 blkptr_cb_t func, void *arg)
709 spa_t *spa = ds->ds_dir->dd_pool->dp_spa;
710 traverse_handle_t *th;
713 th = traverse_init(spa, func, arg, advance, ZIO_FLAG_MUSTSUCCEED);
715 traverse_add_objset(th, txg_start, -1ULL, ds->ds_object);
717 while ((err = traverse_more(th)) == EAGAIN)
725 traverse_more(traverse_handle_t *th)
727 zseg_t *zseg = list_head(&th->th_seglist);
728 uint64_t save_txg; /* XXX won't be necessary with real itinerary */
729 krwlock_t *rw = spa_traverse_rwlock(th->th_spa);
730 blkptr_t *mosbp = spa_get_rootblkptr(th->th_spa);
738 save_txg = zseg->seg_mintxg;
740 rw_enter(rw, RW_READER);
743 rc = traverse_segment(th, zseg, mosbp);
744 ASSERT(rc == ERANGE || rc == EAGAIN || rc == EINTR);
750 zseg->seg_mintxg = save_txg;
753 list_remove(&th->th_seglist, zseg);
754 kmem_free(zseg, sizeof (*zseg));
762 * Note: (mintxg, maxtxg) is an open interval; mintxg and maxtxg themselves
763 * are not included. The blocks covered by this segment will all have
764 * mintxg < birth < maxtxg.
767 traverse_add_segment(traverse_handle_t *th, uint64_t mintxg, uint64_t maxtxg,
768 uint64_t sobjset, uint64_t sobject, int slevel, uint64_t sblkid,
769 uint64_t eobjset, uint64_t eobject, int elevel, uint64_t eblkid)
773 zseg = kmem_alloc(sizeof (zseg_t), KM_SLEEP);
775 zseg->seg_mintxg = mintxg;
776 zseg->seg_maxtxg = maxtxg;
778 zseg->seg_start.zb_objset = sobjset;
779 zseg->seg_start.zb_object = sobject;
780 zseg->seg_start.zb_level = slevel;
781 zseg->seg_start.zb_blkid = sblkid;
783 zseg->seg_end.zb_objset = eobjset;
784 zseg->seg_end.zb_object = eobject;
785 zseg->seg_end.zb_level = elevel;
786 zseg->seg_end.zb_blkid = eblkid;
788 list_insert_tail(&th->th_seglist, zseg);
792 traverse_add_dnode(traverse_handle_t *th, uint64_t mintxg, uint64_t maxtxg,
793 uint64_t objset, uint64_t object)
795 if (th->th_advance & ADVANCE_PRE)
796 traverse_add_segment(th, mintxg, maxtxg,
797 objset, object, ZB_MAXLEVEL, 0,
798 objset, object, 0, ZB_MAXBLKID);
800 traverse_add_segment(th, mintxg, maxtxg,
801 objset, object, 0, 0,
802 objset, object, 0, ZB_MAXBLKID);
806 traverse_add_objset(traverse_handle_t *th, uint64_t mintxg, uint64_t maxtxg,
809 if (th->th_advance & ADVANCE_PRE)
810 traverse_add_segment(th, mintxg, maxtxg,
812 objset, ZB_MAXOBJECT, 0, ZB_MAXBLKID);
814 traverse_add_segment(th, mintxg, maxtxg,
820 traverse_add_pool(traverse_handle_t *th, uint64_t mintxg, uint64_t maxtxg)
822 if (th->th_advance & ADVANCE_PRE)
823 traverse_add_segment(th, mintxg, maxtxg,
825 ZB_MAXOBJSET, ZB_MAXOBJECT, 0, ZB_MAXBLKID);
827 traverse_add_segment(th, mintxg, maxtxg,
833 traverse_init(spa_t *spa, blkptr_cb_t func, void *arg, int advance,
836 traverse_handle_t *th;
839 th = kmem_zalloc(sizeof (*th), KM_SLEEP);
844 th->th_advance = advance;
845 th->th_lastcb.zb_level = ZB_NO_LEVEL;
846 th->th_noread.zb_level = ZB_NO_LEVEL;
847 th->th_zio_flags = zio_flags;
849 list_create(&th->th_seglist, sizeof (zseg_t),
850 offsetof(zseg_t, seg_node));
852 for (d = 0; d < ZB_DEPTH; d++) {
853 for (l = 0; l < ZB_MAXLEVEL; l++) {
854 if ((advance & ADVANCE_DATA) ||
855 l != 0 || d != ZB_DN_CACHE)
856 th->th_cache[d][l].bc_data =
857 zio_buf_alloc(SPA_MAXBLOCKSIZE);
865 traverse_fini(traverse_handle_t *th)
870 for (d = 0; d < ZB_DEPTH; d++)
871 for (l = 0; l < ZB_MAXLEVEL; l++)
872 if (th->th_cache[d][l].bc_data != NULL)
873 zio_buf_free(th->th_cache[d][l].bc_data,
876 while ((zseg = list_head(&th->th_seglist)) != NULL) {
877 list_remove(&th->th_seglist, zseg);
878 kmem_free(zseg, sizeof (*zseg));
881 list_destroy(&th->th_seglist);
883 dprintf("%llu hit, %llu ARC, %llu IO, %llu cb, %llu sync, %llu again\n",
884 th->th_hits, th->th_arc_hits, th->th_reads, th->th_callbacks,
885 th->th_syncs, th->th_restarts);
887 kmem_free(th, sizeof (*th));