xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_traverse.c (revision dcbf3bd6a1f1360fc1afcee9e22c6dcff7844bf2)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
223f9d6ad7SLin Ling  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23286ef713SPaul Dagnelie  * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #include <sys/zfs_context.h>
27fa9e4066Sahrens #include <sys/dmu_objset.h>
28fa9e4066Sahrens #include <sys/dmu_traverse.h>
29fa9e4066Sahrens #include <sys/dsl_dataset.h>
30fa9e4066Sahrens #include <sys/dsl_dir.h>
31fa9e4066Sahrens #include <sys/dsl_pool.h>
32fa9e4066Sahrens #include <sys/dnode.h>
33fa9e4066Sahrens #include <sys/spa.h>
34fa9e4066Sahrens #include <sys/zio.h>
35fa9e4066Sahrens #include <sys/dmu_impl.h>
360a586ceaSMark Shellenbaum #include <sys/sa.h>
370a586ceaSMark Shellenbaum #include <sys/sa_impl.h>
3888b7b0f2SMatthew Ahrens #include <sys/callb.h>
3943466aaeSMax Grossman #include <sys/zfeature.h>
4088b7b0f2SMatthew Ahrens 
4134d7ce05SGeorge Wilson int32_t zfs_pd_bytes_max = 50 * 1024 * 1024;	/* 50MB */
4244f92b7dSChris Kirby 
436e0cbcaaSMatthew Ahrens typedef struct prefetch_data {
4488b7b0f2SMatthew Ahrens 	kmutex_t pd_mtx;
4588b7b0f2SMatthew Ahrens 	kcondvar_t pd_cv;
4634d7ce05SGeorge Wilson 	int32_t pd_bytes_fetched;
4788b7b0f2SMatthew Ahrens 	int pd_flags;
4888b7b0f2SMatthew Ahrens 	boolean_t pd_cancel;
4988b7b0f2SMatthew Ahrens 	boolean_t pd_exited;
509c3fd121SMatthew Ahrens 	zbookmark_phys_t pd_resume;
516e0cbcaaSMatthew Ahrens } prefetch_data_t;
5288b7b0f2SMatthew Ahrens 
536e0cbcaaSMatthew Ahrens typedef struct traverse_data {
5488b7b0f2SMatthew Ahrens 	spa_t *td_spa;
5588b7b0f2SMatthew Ahrens 	uint64_t td_objset;
5688b7b0f2SMatthew Ahrens 	blkptr_t *td_rootbp;
5788b7b0f2SMatthew Ahrens 	uint64_t td_min_txg;
587802d7bfSMatthew Ahrens 	zbookmark_phys_t *td_resume;
5988b7b0f2SMatthew Ahrens 	int td_flags;
606e0cbcaaSMatthew Ahrens 	prefetch_data_t *td_pfd;
617fd05ac4SMatthew Ahrens 	boolean_t td_paused;
62f7950bf1SMatthew Ahrens 	uint64_t td_hole_birth_enabled_txg;
6388b7b0f2SMatthew Ahrens 	blkptr_cb_t *td_func;
6488b7b0f2SMatthew Ahrens 	void *td_arg;
65286ef713SPaul Dagnelie 	boolean_t td_realloc_possible;
666e0cbcaaSMatthew Ahrens } traverse_data_t;
67fa9e4066Sahrens 
686e0cbcaaSMatthew Ahrens static int traverse_dnode(traverse_data_t *td, const dnode_phys_t *dnp,
691b912ec7SGeorge Wilson     uint64_t objset, uint64_t object);
70b4709335SMatthew Ahrens static void prefetch_dnode_metadata(traverse_data_t *td, const dnode_phys_t *,
711b912ec7SGeorge Wilson     uint64_t objset, uint64_t object);
7214843421SMatthew Ahrens 
73b24ab676SJeff Bonwick static int
745dabedeeSbonwick traverse_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
75ea8dc4b6Seschrock {
766e0cbcaaSMatthew Ahrens 	traverse_data_t *td = arg;
777802d7bfSMatthew Ahrens 	zbookmark_phys_t zb;
78ea8dc4b6Seschrock 
7943466aaeSMax Grossman 	if (BP_IS_HOLE(bp))
80b24ab676SJeff Bonwick 		return (0);
815dabedeeSbonwick 
8288b7b0f2SMatthew Ahrens 	if (claim_txg == 0 && bp->blk_birth >= spa_first_txg(td->td_spa))
83b24ab676SJeff Bonwick 		return (0);
8488b7b0f2SMatthew Ahrens 
85b24ab676SJeff Bonwick 	SET_BOOKMARK(&zb, td->td_objset, ZB_ZIL_OBJECT, ZB_ZIL_LEVEL,
86b24ab676SJeff Bonwick 	    bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
87b24ab676SJeff Bonwick 
881b912ec7SGeorge Wilson 	(void) td->td_func(td->td_spa, zilog, bp, &zb, NULL, td->td_arg);
89b24ab676SJeff Bonwick 
90b24ab676SJeff Bonwick 	return (0);
91ea8dc4b6Seschrock }
92ea8dc4b6Seschrock 
93b24ab676SJeff Bonwick static int
945dabedeeSbonwick traverse_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg)
95ea8dc4b6Seschrock {
966e0cbcaaSMatthew Ahrens 	traverse_data_t *td = arg;
97ea8dc4b6Seschrock 
98ea8dc4b6Seschrock 	if (lrc->lrc_txtype == TX_WRITE) {
99ea8dc4b6Seschrock 		lr_write_t *lr = (lr_write_t *)lrc;
100ea8dc4b6Seschrock 		blkptr_t *bp = &lr->lr_blkptr;
1017802d7bfSMatthew Ahrens 		zbookmark_phys_t zb;
102ea8dc4b6Seschrock 
10343466aaeSMax Grossman 		if (BP_IS_HOLE(bp))
104b24ab676SJeff Bonwick 			return (0);
1055dabedeeSbonwick 
10688b7b0f2SMatthew Ahrens 		if (claim_txg == 0 || bp->blk_birth < claim_txg)
107b24ab676SJeff Bonwick 			return (0);
108b24ab676SJeff Bonwick 
1096e0cbcaaSMatthew Ahrens 		SET_BOOKMARK(&zb, td->td_objset, lr->lr_foid,
1106e0cbcaaSMatthew Ahrens 		    ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp));
11188b7b0f2SMatthew Ahrens 
1121b912ec7SGeorge Wilson 		(void) td->td_func(td->td_spa, zilog, bp, &zb, NULL,
113b24ab676SJeff Bonwick 		    td->td_arg);
114ea8dc4b6Seschrock 	}
115b24ab676SJeff Bonwick 	return (0);
116ea8dc4b6Seschrock }
117ea8dc4b6Seschrock 
118ea8dc4b6Seschrock static void
1196e0cbcaaSMatthew Ahrens traverse_zil(traverse_data_t *td, zil_header_t *zh)
120ea8dc4b6Seschrock {
1215dabedeeSbonwick 	uint64_t claim_txg = zh->zh_claim_txg;
122ea8dc4b6Seschrock 	zilog_t *zilog;
123ea8dc4b6Seschrock 
1245dabedeeSbonwick 	/*
1255dabedeeSbonwick 	 * We only want to visit blocks that have been claimed but not yet
126b24ab676SJeff Bonwick 	 * replayed; plus, in read-only mode, blocks that are already stable.
1275dabedeeSbonwick 	 */
1288ad4d6ddSJeff Bonwick 	if (claim_txg == 0 && spa_writeable(td->td_spa))
1295dabedeeSbonwick 		return;
1305dabedeeSbonwick 
13188b7b0f2SMatthew Ahrens 	zilog = zil_alloc(spa_get_dsl(td->td_spa)->dp_meta_objset, zh);
132ea8dc4b6Seschrock 
13388b7b0f2SMatthew Ahrens 	(void) zil_parse(zilog, traverse_zil_block, traverse_zil_record, td,
1345dabedeeSbonwick 	    claim_txg);
135ea8dc4b6Seschrock 
136ea8dc4b6Seschrock 	zil_free(zilog);
137ea8dc4b6Seschrock }
138ea8dc4b6Seschrock 
139ad135b5dSChristopher Siden typedef enum resume_skip {
140ad135b5dSChristopher Siden 	RESUME_SKIP_ALL,
141ad135b5dSChristopher Siden 	RESUME_SKIP_NONE,
142ad135b5dSChristopher Siden 	RESUME_SKIP_CHILDREN
143ad135b5dSChristopher Siden } resume_skip_t;
144ad135b5dSChristopher Siden 
145ad135b5dSChristopher Siden /*
146ad135b5dSChristopher Siden  * Returns RESUME_SKIP_ALL if td indicates that we are resuming a traversal and
147ad135b5dSChristopher Siden  * the block indicated by zb does not need to be visited at all. Returns
148ad135b5dSChristopher Siden  * RESUME_SKIP_CHILDREN if we are resuming a post traversal and we reach the
149ad135b5dSChristopher Siden  * resume point. This indicates that this block should be visited but not its
150ad135b5dSChristopher Siden  * children (since they must have been visited in a previous traversal).
151ad135b5dSChristopher Siden  * Otherwise returns RESUME_SKIP_NONE.
152ad135b5dSChristopher Siden  */
153ad135b5dSChristopher Siden static resume_skip_t
154ad135b5dSChristopher Siden resume_skip_check(traverse_data_t *td, const dnode_phys_t *dnp,
1557802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb)
156ad135b5dSChristopher Siden {
157ad135b5dSChristopher Siden 	if (td->td_resume != NULL && !ZB_IS_ZERO(td->td_resume)) {
158ad135b5dSChristopher Siden 		/*
159ad135b5dSChristopher Siden 		 * If we already visited this bp & everything below,
160ad135b5dSChristopher Siden 		 * don't bother doing it again.
161ad135b5dSChristopher Siden 		 */
162a2cdcdd2SPaul Dagnelie 		if (zbookmark_subtree_completed(dnp, zb, td->td_resume))
163ad135b5dSChristopher Siden 			return (RESUME_SKIP_ALL);
164ad135b5dSChristopher Siden 
165ad135b5dSChristopher Siden 		/*
166ad135b5dSChristopher Siden 		 * If we found the block we're trying to resume from, zero
167ad135b5dSChristopher Siden 		 * the bookmark out to indicate that we have resumed.
168ad135b5dSChristopher Siden 		 */
169ad135b5dSChristopher Siden 		if (bcmp(zb, td->td_resume, sizeof (*zb)) == 0) {
170ad135b5dSChristopher Siden 			bzero(td->td_resume, sizeof (*zb));
171ad135b5dSChristopher Siden 			if (td->td_flags & TRAVERSE_POST)
172ad135b5dSChristopher Siden 				return (RESUME_SKIP_CHILDREN);
173ad135b5dSChristopher Siden 		}
174ad135b5dSChristopher Siden 	}
175ad135b5dSChristopher Siden 	return (RESUME_SKIP_NONE);
176ad135b5dSChristopher Siden }
177ad135b5dSChristopher Siden 
178b4709335SMatthew Ahrens static void
179b4709335SMatthew Ahrens traverse_prefetch_metadata(traverse_data_t *td,
1807802d7bfSMatthew Ahrens     const blkptr_t *bp, const zbookmark_phys_t *zb)
181b4709335SMatthew Ahrens {
1827adb730bSGeorge Wilson 	arc_flags_t flags = ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH;
183b4709335SMatthew Ahrens 
184b4709335SMatthew Ahrens 	if (!(td->td_flags & TRAVERSE_PREFETCH_METADATA))
185b4709335SMatthew Ahrens 		return;
186b4709335SMatthew Ahrens 	/*
187b4709335SMatthew Ahrens 	 * If we are in the process of resuming, don't prefetch, because
188b4709335SMatthew Ahrens 	 * some children will not be needed (and in fact may have already
189b4709335SMatthew Ahrens 	 * been freed).
190b4709335SMatthew Ahrens 	 */
191b4709335SMatthew Ahrens 	if (td->td_resume != NULL && !ZB_IS_ZERO(td->td_resume))
192b4709335SMatthew Ahrens 		return;
193b4709335SMatthew Ahrens 	if (BP_IS_HOLE(bp) || bp->blk_birth <= td->td_min_txg)
194b4709335SMatthew Ahrens 		return;
195b4709335SMatthew Ahrens 	if (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_DNODE)
196b4709335SMatthew Ahrens 		return;
197b4709335SMatthew Ahrens 
1981b912ec7SGeorge Wilson 	(void) arc_read(NULL, td->td_spa, bp, NULL, NULL,
1991b912ec7SGeorge Wilson 	    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
200b4709335SMatthew Ahrens }
201b4709335SMatthew Ahrens 
20206315b79SMatthew Ahrens static boolean_t
20306315b79SMatthew Ahrens prefetch_needed(prefetch_data_t *pfd, const blkptr_t *bp)
20406315b79SMatthew Ahrens {
20506315b79SMatthew Ahrens 	ASSERT(pfd->pd_flags & TRAVERSE_PREFETCH_DATA);
20606315b79SMatthew Ahrens 	if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp) ||
20706315b79SMatthew Ahrens 	    BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG)
20806315b79SMatthew Ahrens 		return (B_FALSE);
20906315b79SMatthew Ahrens 	return (B_TRUE);
21006315b79SMatthew Ahrens }
21106315b79SMatthew Ahrens 
212fa9e4066Sahrens static int
2136e0cbcaaSMatthew Ahrens traverse_visitbp(traverse_data_t *td, const dnode_phys_t *dnp,
2147802d7bfSMatthew Ahrens     const blkptr_t *bp, const zbookmark_phys_t *zb)
215fa9e4066Sahrens {
2167802d7bfSMatthew Ahrens 	zbookmark_phys_t czb;
2177fd05ac4SMatthew Ahrens 	int err = 0;
21888b7b0f2SMatthew Ahrens 	arc_buf_t *buf = NULL;
2196e0cbcaaSMatthew Ahrens 	prefetch_data_t *pd = td->td_pfd;
220cd088ea4SVictor Latushkin 	boolean_t hard = td->td_flags & TRAVERSE_HARD;
221ad135b5dSChristopher Siden 
222ad135b5dSChristopher Siden 	switch (resume_skip_check(td, dnp, zb)) {
223ad135b5dSChristopher Siden 	case RESUME_SKIP_ALL:
224ad135b5dSChristopher Siden 		return (0);
225ad135b5dSChristopher Siden 	case RESUME_SKIP_CHILDREN:
226ad135b5dSChristopher Siden 		goto post;
227ad135b5dSChristopher Siden 	case RESUME_SKIP_NONE:
228ad135b5dSChristopher Siden 		break;
229ad135b5dSChristopher Siden 	default:
230ad135b5dSChristopher Siden 		ASSERT(0);
231ad135b5dSChristopher Siden 	}
232fa9e4066Sahrens 
23343466aaeSMax Grossman 	if (bp->blk_birth == 0) {
234f7950bf1SMatthew Ahrens 		/*
235286ef713SPaul Dagnelie 		 * Since this block has a birth time of 0 it must be one of
236286ef713SPaul Dagnelie 		 * two things: a hole created before the
237286ef713SPaul Dagnelie 		 * SPA_FEATURE_HOLE_BIRTH feature was enabled, or a hole
238286ef713SPaul Dagnelie 		 * which has always been a hole in an object.
239286ef713SPaul Dagnelie 		 *
240286ef713SPaul Dagnelie 		 * If a file is written sparsely, then the unwritten parts of
241286ef713SPaul Dagnelie 		 * the file were "always holes" -- that is, they have been
242286ef713SPaul Dagnelie 		 * holes since this object was allocated.  However, we (and
243286ef713SPaul Dagnelie 		 * our callers) can not necessarily tell when an object was
244286ef713SPaul Dagnelie 		 * allocated.  Therefore, if it's possible that this object
245286ef713SPaul Dagnelie 		 * was freed and then its object number reused, we need to
246286ef713SPaul Dagnelie 		 * visit all the holes with birth==0.
247286ef713SPaul Dagnelie 		 *
248286ef713SPaul Dagnelie 		 * If it isn't possible that the object number was reused,
249286ef713SPaul Dagnelie 		 * then if SPA_FEATURE_HOLE_BIRTH was enabled before we wrote
250286ef713SPaul Dagnelie 		 * all the blocks we will visit as part of this traversal,
251286ef713SPaul Dagnelie 		 * then this hole must have always existed, so we can skip
252286ef713SPaul Dagnelie 		 * it.  We visit blocks born after (exclusive) td_min_txg.
253286ef713SPaul Dagnelie 		 *
254286ef713SPaul Dagnelie 		 * Note that the meta-dnode cannot be reallocated.
255f7950bf1SMatthew Ahrens 		 */
256286ef713SPaul Dagnelie 		if ((!td->td_realloc_possible ||
257286ef713SPaul Dagnelie 		    zb->zb_object == DMU_META_DNODE_OBJECT) &&
258286ef713SPaul Dagnelie 		    td->td_hole_birth_enabled_txg <= td->td_min_txg)
259f7950bf1SMatthew Ahrens 			return (0);
26043466aaeSMax Grossman 	} else if (bp->blk_birth <= td->td_min_txg) {
26143466aaeSMax Grossman 		return (0);
26243466aaeSMax Grossman 	}
26343466aaeSMax Grossman 
26406315b79SMatthew Ahrens 	if (pd != NULL && !pd->pd_exited && prefetch_needed(pd, bp)) {
26534d7ce05SGeorge Wilson 		uint64_t size = BP_GET_LSIZE(bp);
26688b7b0f2SMatthew Ahrens 		mutex_enter(&pd->pd_mtx);
26734d7ce05SGeorge Wilson 		ASSERT(pd->pd_bytes_fetched >= 0);
26834d7ce05SGeorge Wilson 		while (pd->pd_bytes_fetched < size && !pd->pd_exited)
26988b7b0f2SMatthew Ahrens 			cv_wait(&pd->pd_cv, &pd->pd_mtx);
27034d7ce05SGeorge Wilson 		pd->pd_bytes_fetched -= size;
27188b7b0f2SMatthew Ahrens 		cv_broadcast(&pd->pd_cv);
27288b7b0f2SMatthew Ahrens 		mutex_exit(&pd->pd_mtx);
273fa9e4066Sahrens 	}
274fa9e4066Sahrens 
27506315b79SMatthew Ahrens 	if (BP_IS_HOLE(bp)) {
27606315b79SMatthew Ahrens 		err = td->td_func(td->td_spa, NULL, bp, zb, dnp, td->td_arg);
27706315b79SMatthew Ahrens 		if (err != 0)
27806315b79SMatthew Ahrens 			goto post;
27906315b79SMatthew Ahrens 		return (0);
28006315b79SMatthew Ahrens 	}
28106315b79SMatthew Ahrens 
28288b7b0f2SMatthew Ahrens 	if (td->td_flags & TRAVERSE_PRE) {
2831b912ec7SGeorge Wilson 		err = td->td_func(td->td_spa, NULL, bp, zb, dnp,
2843f9d6ad7SLin Ling 		    td->td_arg);
28599d5e173STim Haley 		if (err == TRAVERSE_VISIT_NO_CHILDREN)
28699d5e173STim Haley 			return (0);
287ad135b5dSChristopher Siden 		if (err != 0)
288ad135b5dSChristopher Siden 			goto post;
289fa9e4066Sahrens 	}
290fa9e4066Sahrens 
29188b7b0f2SMatthew Ahrens 	if (BP_GET_LEVEL(bp) > 0) {
2927adb730bSGeorge Wilson 		arc_flags_t flags = ARC_FLAG_WAIT;
29388b7b0f2SMatthew Ahrens 		int i;
29488b7b0f2SMatthew Ahrens 		blkptr_t *cbp;
29588b7b0f2SMatthew Ahrens 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
29688b7b0f2SMatthew Ahrens 
2971b912ec7SGeorge Wilson 		err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf,
29888b7b0f2SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
2993b2aab18SMatthew Ahrens 		if (err != 0)
3007fd05ac4SMatthew Ahrens 			goto post;
301b4709335SMatthew Ahrens 		cbp = buf->b_data;
302b4709335SMatthew Ahrens 
303b4709335SMatthew Ahrens 		for (i = 0; i < epb; i++) {
304b4709335SMatthew Ahrens 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
305b4709335SMatthew Ahrens 			    zb->zb_level - 1,
306b4709335SMatthew Ahrens 			    zb->zb_blkid * epb + i);
3071b912ec7SGeorge Wilson 			traverse_prefetch_metadata(td, &cbp[i], &czb);
308b4709335SMatthew Ahrens 		}
30988b7b0f2SMatthew Ahrens 
31088b7b0f2SMatthew Ahrens 		/* recursively visitbp() blocks below this */
311b4709335SMatthew Ahrens 		for (i = 0; i < epb; i++) {
31288b7b0f2SMatthew Ahrens 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
31388b7b0f2SMatthew Ahrens 			    zb->zb_level - 1,
31488b7b0f2SMatthew Ahrens 			    zb->zb_blkid * epb + i);
3151b912ec7SGeorge Wilson 			err = traverse_visitbp(td, dnp, &cbp[i], &czb);
3167fd05ac4SMatthew Ahrens 			if (err != 0)
3177fd05ac4SMatthew Ahrens 				break;
31888b7b0f2SMatthew Ahrens 		}
31988b7b0f2SMatthew Ahrens 	} else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) {
3207adb730bSGeorge Wilson 		arc_flags_t flags = ARC_FLAG_WAIT;
32114843421SMatthew Ahrens 		int i;
32288b7b0f2SMatthew Ahrens 		int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
32388b7b0f2SMatthew Ahrens 
3241b912ec7SGeorge Wilson 		err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf,
32588b7b0f2SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
3263b2aab18SMatthew Ahrens 		if (err != 0)
3277fd05ac4SMatthew Ahrens 			goto post;
3289c3fd121SMatthew Ahrens 		dnode_phys_t *child_dnp = buf->b_data;
329b4709335SMatthew Ahrens 
330b4709335SMatthew Ahrens 		for (i = 0; i < epb; i++) {
3319c3fd121SMatthew Ahrens 			prefetch_dnode_metadata(td, &child_dnp[i],
3329c3fd121SMatthew Ahrens 			    zb->zb_objset, zb->zb_blkid * epb + i);
333b4709335SMatthew Ahrens 		}
33488b7b0f2SMatthew Ahrens 
33588b7b0f2SMatthew Ahrens 		/* recursively visitbp() blocks below this */
336b4709335SMatthew Ahrens 		for (i = 0; i < epb; i++) {
3379c3fd121SMatthew Ahrens 			err = traverse_dnode(td, &child_dnp[i],
3389c3fd121SMatthew Ahrens 			    zb->zb_objset, zb->zb_blkid * epb + i);
3397fd05ac4SMatthew Ahrens 			if (err != 0)
3407fd05ac4SMatthew Ahrens 				break;
341fa9e4066Sahrens 		}
34288b7b0f2SMatthew Ahrens 	} else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
3437adb730bSGeorge Wilson 		arc_flags_t flags = ARC_FLAG_WAIT;
34488b7b0f2SMatthew Ahrens 
3451b912ec7SGeorge Wilson 		err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf,
34688b7b0f2SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
3473b2aab18SMatthew Ahrens 		if (err != 0)
3487fd05ac4SMatthew Ahrens 			goto post;
34988b7b0f2SMatthew Ahrens 
3509c3fd121SMatthew Ahrens 		objset_phys_t *osp = buf->b_data;
3519c3fd121SMatthew Ahrens 		prefetch_dnode_metadata(td, &osp->os_meta_dnode, zb->zb_objset,
352b4709335SMatthew Ahrens 		    DMU_META_DNODE_OBJECT);
353286ef713SPaul Dagnelie 		/*
354286ef713SPaul Dagnelie 		 * See the block comment above for the goal of this variable.
355286ef713SPaul Dagnelie 		 * If the maxblkid of the meta-dnode is 0, then we know that
356286ef713SPaul Dagnelie 		 * we've never had more than DNODES_PER_BLOCK objects in the
357286ef713SPaul Dagnelie 		 * dataset, which means we can't have reused any object ids.
358286ef713SPaul Dagnelie 		 */
359286ef713SPaul Dagnelie 		if (osp->os_meta_dnode.dn_maxblkid == 0)
360286ef713SPaul Dagnelie 			td->td_realloc_possible = B_FALSE;
361286ef713SPaul Dagnelie 
362b4709335SMatthew Ahrens 		if (arc_buf_size(buf) >= sizeof (objset_phys_t)) {
363b4709335SMatthew Ahrens 			prefetch_dnode_metadata(td, &osp->os_groupused_dnode,
36448f1b90eSMatthew Ahrens 			    zb->zb_objset, DMU_GROUPUSED_OBJECT);
36548f1b90eSMatthew Ahrens 			prefetch_dnode_metadata(td, &osp->os_userused_dnode,
3661b912ec7SGeorge Wilson 			    zb->zb_objset, DMU_USERUSED_OBJECT);
367b4709335SMatthew Ahrens 		}
368b4709335SMatthew Ahrens 
3699c3fd121SMatthew Ahrens 		err = traverse_dnode(td, &osp->os_meta_dnode, zb->zb_objset,
370b24ab676SJeff Bonwick 		    DMU_META_DNODE_OBJECT);
37114843421SMatthew Ahrens 		if (err == 0 && arc_buf_size(buf) >= sizeof (objset_phys_t)) {
3729c3fd121SMatthew Ahrens 			err = traverse_dnode(td, &osp->os_groupused_dnode,
3739c3fd121SMatthew Ahrens 			    zb->zb_objset, DMU_GROUPUSED_OBJECT);
37414843421SMatthew Ahrens 		}
37514843421SMatthew Ahrens 		if (err == 0 && arc_buf_size(buf) >= sizeof (objset_phys_t)) {
3769c3fd121SMatthew Ahrens 			err = traverse_dnode(td, &osp->os_userused_dnode,
3779c3fd121SMatthew Ahrens 			    zb->zb_objset, DMU_USERUSED_OBJECT);
37888b7b0f2SMatthew Ahrens 		}
37988b7b0f2SMatthew Ahrens 	}
380fa9e4066Sahrens 
38188b7b0f2SMatthew Ahrens 	if (buf)
382*dcbf3bd6SGeorge Wilson 		arc_buf_destroy(buf, &buf);
383fa9e4066Sahrens 
384ad135b5dSChristopher Siden post:
3857fd05ac4SMatthew Ahrens 	if (err == 0 && (td->td_flags & TRAVERSE_POST))
3861b912ec7SGeorge Wilson 		err = td->td_func(td->td_spa, NULL, bp, zb, dnp, td->td_arg);
3877fd05ac4SMatthew Ahrens 
3887fd05ac4SMatthew Ahrens 	if (hard && (err == EIO || err == ECKSUM)) {
3897fd05ac4SMatthew Ahrens 		/*
3907fd05ac4SMatthew Ahrens 		 * Ignore this disk error as requested by the HARD flag,
3917fd05ac4SMatthew Ahrens 		 * and continue traversal.
3927fd05ac4SMatthew Ahrens 		 */
3937fd05ac4SMatthew Ahrens 		err = 0;
394ad135b5dSChristopher Siden 	}
395ad135b5dSChristopher Siden 
3967fd05ac4SMatthew Ahrens 	/*
3977fd05ac4SMatthew Ahrens 	 * If we are stopping here, set td_resume.
3987fd05ac4SMatthew Ahrens 	 */
3997fd05ac4SMatthew Ahrens 	if (td->td_resume != NULL && err != 0 && !td->td_paused) {
4007fd05ac4SMatthew Ahrens 		td->td_resume->zb_objset = zb->zb_objset;
4017fd05ac4SMatthew Ahrens 		td->td_resume->zb_object = zb->zb_object;
4027fd05ac4SMatthew Ahrens 		td->td_resume->zb_level = 0;
4037fd05ac4SMatthew Ahrens 		/*
4047fd05ac4SMatthew Ahrens 		 * If we have stopped on an indirect block (e.g. due to
4057fd05ac4SMatthew Ahrens 		 * i/o error), we have not visited anything below it.
4067fd05ac4SMatthew Ahrens 		 * Set the bookmark to the first level-0 block that we need
4077fd05ac4SMatthew Ahrens 		 * to visit.  This way, the resuming code does not need to
4087fd05ac4SMatthew Ahrens 		 * deal with resuming from indirect blocks.
4099c3fd121SMatthew Ahrens 		 *
4109c3fd121SMatthew Ahrens 		 * Note, if zb_level <= 0, dnp may be NULL, so we don't want
4119c3fd121SMatthew Ahrens 		 * to dereference it.
4127fd05ac4SMatthew Ahrens 		 */
4139c3fd121SMatthew Ahrens 		td->td_resume->zb_blkid = zb->zb_blkid;
4149c3fd121SMatthew Ahrens 		if (zb->zb_level > 0) {
4159c3fd121SMatthew Ahrens 			td->td_resume->zb_blkid <<= zb->zb_level *
4169c3fd121SMatthew Ahrens 			    (dnp->dn_indblkshift - SPA_BLKPTRSHIFT);
4179c3fd121SMatthew Ahrens 		}
4187fd05ac4SMatthew Ahrens 		td->td_paused = B_TRUE;
4193f9d6ad7SLin Ling 	}
420fa9e4066Sahrens 
4217fd05ac4SMatthew Ahrens 	return (err);
422fa9e4066Sahrens }
423fa9e4066Sahrens 
424b4709335SMatthew Ahrens static void
425b4709335SMatthew Ahrens prefetch_dnode_metadata(traverse_data_t *td, const dnode_phys_t *dnp,
4261b912ec7SGeorge Wilson     uint64_t objset, uint64_t object)
427b4709335SMatthew Ahrens {
428b4709335SMatthew Ahrens 	int j;
4297802d7bfSMatthew Ahrens 	zbookmark_phys_t czb;
430b4709335SMatthew Ahrens 
431b4709335SMatthew Ahrens 	for (j = 0; j < dnp->dn_nblkptr; j++) {
432b4709335SMatthew Ahrens 		SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j);
4331b912ec7SGeorge Wilson 		traverse_prefetch_metadata(td, &dnp->dn_blkptr[j], &czb);
434b4709335SMatthew Ahrens 	}
435b4709335SMatthew Ahrens 
436b4709335SMatthew Ahrens 	if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
437b4709335SMatthew Ahrens 		SET_BOOKMARK(&czb, objset, object, 0, DMU_SPILL_BLKID);
4381b912ec7SGeorge Wilson 		traverse_prefetch_metadata(td, &dnp->dn_spill, &czb);
439b4709335SMatthew Ahrens 	}
440b4709335SMatthew Ahrens }
441b4709335SMatthew Ahrens 
44214843421SMatthew Ahrens static int
4436e0cbcaaSMatthew Ahrens traverse_dnode(traverse_data_t *td, const dnode_phys_t *dnp,
4441b912ec7SGeorge Wilson     uint64_t objset, uint64_t object)
44514843421SMatthew Ahrens {
4467fd05ac4SMatthew Ahrens 	int j, err = 0;
4477802d7bfSMatthew Ahrens 	zbookmark_phys_t czb;
44814843421SMatthew Ahrens 
4499c3fd121SMatthew Ahrens 	if (object != DMU_META_DNODE_OBJECT && td->td_resume != NULL &&
4509c3fd121SMatthew Ahrens 	    object < td->td_resume->zb_object)
4519c3fd121SMatthew Ahrens 		return (0);
4529c3fd121SMatthew Ahrens 
453a2cdcdd2SPaul Dagnelie 	if (td->td_flags & TRAVERSE_PRE) {
454a2cdcdd2SPaul Dagnelie 		SET_BOOKMARK(&czb, objset, object, ZB_DNODE_LEVEL,
455a2cdcdd2SPaul Dagnelie 		    ZB_DNODE_BLKID);
456a2cdcdd2SPaul Dagnelie 		err = td->td_func(td->td_spa, NULL, NULL, &czb, dnp,
457a2cdcdd2SPaul Dagnelie 		    td->td_arg);
458a2cdcdd2SPaul Dagnelie 		if (err == TRAVERSE_VISIT_NO_CHILDREN)
459a2cdcdd2SPaul Dagnelie 			return (0);
460a2cdcdd2SPaul Dagnelie 		if (err != 0)
461a2cdcdd2SPaul Dagnelie 			return (err);
462a2cdcdd2SPaul Dagnelie 	}
463a2cdcdd2SPaul Dagnelie 
46414843421SMatthew Ahrens 	for (j = 0; j < dnp->dn_nblkptr; j++) {
46514843421SMatthew Ahrens 		SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j);
4661b912ec7SGeorge Wilson 		err = traverse_visitbp(td, dnp, &dnp->dn_blkptr[j], &czb);
4677fd05ac4SMatthew Ahrens 		if (err != 0)
4687fd05ac4SMatthew Ahrens 			break;
4693f9d6ad7SLin Ling 	}
4703f9d6ad7SLin Ling 
471a2cdcdd2SPaul Dagnelie 	if (err == 0 && (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) {
472b4709335SMatthew Ahrens 		SET_BOOKMARK(&czb, objset, object, 0, DMU_SPILL_BLKID);
4731b912ec7SGeorge Wilson 		err = traverse_visitbp(td, dnp, &dnp->dn_spill, &czb);
47414843421SMatthew Ahrens 	}
475a2cdcdd2SPaul Dagnelie 
476a2cdcdd2SPaul Dagnelie 	if (err == 0 && (td->td_flags & TRAVERSE_POST)) {
477a2cdcdd2SPaul Dagnelie 		SET_BOOKMARK(&czb, objset, object, ZB_DNODE_LEVEL,
478a2cdcdd2SPaul Dagnelie 		    ZB_DNODE_BLKID);
479a2cdcdd2SPaul Dagnelie 		err = td->td_func(td->td_spa, NULL, NULL, &czb, dnp,
480a2cdcdd2SPaul Dagnelie 		    td->td_arg);
481a2cdcdd2SPaul Dagnelie 		if (err == TRAVERSE_VISIT_NO_CHILDREN)
482a2cdcdd2SPaul Dagnelie 			return (0);
483a2cdcdd2SPaul Dagnelie 		if (err != 0)
484a2cdcdd2SPaul Dagnelie 			return (err);
485a2cdcdd2SPaul Dagnelie 	}
4867fd05ac4SMatthew Ahrens 	return (err);
48714843421SMatthew Ahrens }
48814843421SMatthew Ahrens 
48988b7b0f2SMatthew Ahrens /* ARGSUSED */
49088b7b0f2SMatthew Ahrens static int
491b24ab676SJeff Bonwick traverse_prefetcher(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
4927802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
493e7cbe64fSgw {
4946e0cbcaaSMatthew Ahrens 	prefetch_data_t *pfd = arg;
4957adb730bSGeorge Wilson 	arc_flags_t aflags = ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH;
496e7cbe64fSgw 
49734d7ce05SGeorge Wilson 	ASSERT(pfd->pd_bytes_fetched >= 0);
498a2cdcdd2SPaul Dagnelie 	if (bp == NULL)
499a2cdcdd2SPaul Dagnelie 		return (0);
50088b7b0f2SMatthew Ahrens 	if (pfd->pd_cancel)
501be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
502e7cbe64fSgw 
50306315b79SMatthew Ahrens 	if (!prefetch_needed(pfd, bp))
504fa9e4066Sahrens 		return (0);
505fa9e4066Sahrens 
50688b7b0f2SMatthew Ahrens 	mutex_enter(&pfd->pd_mtx);
50734d7ce05SGeorge Wilson 	while (!pfd->pd_cancel && pfd->pd_bytes_fetched >= zfs_pd_bytes_max)
50888b7b0f2SMatthew Ahrens 		cv_wait(&pfd->pd_cv, &pfd->pd_mtx);
50934d7ce05SGeorge Wilson 	pfd->pd_bytes_fetched += BP_GET_LSIZE(bp);
51088b7b0f2SMatthew Ahrens 	cv_broadcast(&pfd->pd_cv);
51188b7b0f2SMatthew Ahrens 	mutex_exit(&pfd->pd_mtx);
512fa9e4066Sahrens 
5131b912ec7SGeorge Wilson 	(void) arc_read(NULL, spa, bp, NULL, NULL, ZIO_PRIORITY_ASYNC_READ,
5141b912ec7SGeorge Wilson 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &aflags, zb);
515fa9e4066Sahrens 
51688b7b0f2SMatthew Ahrens 	return (0);
517fa9e4066Sahrens }
518fa9e4066Sahrens 
519fa9e4066Sahrens static void
52088b7b0f2SMatthew Ahrens traverse_prefetch_thread(void *arg)
521fa9e4066Sahrens {
5226e0cbcaaSMatthew Ahrens 	traverse_data_t *td_main = arg;
5236e0cbcaaSMatthew Ahrens 	traverse_data_t td = *td_main;
5247802d7bfSMatthew Ahrens 	zbookmark_phys_t czb;
525fa9e4066Sahrens 
52688b7b0f2SMatthew Ahrens 	td.td_func = traverse_prefetcher;
52788b7b0f2SMatthew Ahrens 	td.td_arg = td_main->td_pfd;
52888b7b0f2SMatthew Ahrens 	td.td_pfd = NULL;
5299c3fd121SMatthew Ahrens 	td.td_resume = &td_main->td_pfd->pd_resume;
530fa9e4066Sahrens 
531b24ab676SJeff Bonwick 	SET_BOOKMARK(&czb, td.td_objset,
532b24ab676SJeff Bonwick 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
5331b912ec7SGeorge Wilson 	(void) traverse_visitbp(&td, NULL, td.td_rootbp, &czb);
534fa9e4066Sahrens 
53588b7b0f2SMatthew Ahrens 	mutex_enter(&td_main->td_pfd->pd_mtx);
53688b7b0f2SMatthew Ahrens 	td_main->td_pfd->pd_exited = B_TRUE;
53788b7b0f2SMatthew Ahrens 	cv_broadcast(&td_main->td_pfd->pd_cv);
53888b7b0f2SMatthew Ahrens 	mutex_exit(&td_main->td_pfd->pd_mtx);
539fa9e4066Sahrens }
540fa9e4066Sahrens 
54188b7b0f2SMatthew Ahrens /*
54288b7b0f2SMatthew Ahrens  * NB: dataset must not be changing on-disk (eg, is a snapshot or we are
54388b7b0f2SMatthew Ahrens  * in syncing context).
54488b7b0f2SMatthew Ahrens  */
54588b7b0f2SMatthew Ahrens static int
546ad135b5dSChristopher Siden traverse_impl(spa_t *spa, dsl_dataset_t *ds, uint64_t objset, blkptr_t *rootbp,
5477802d7bfSMatthew Ahrens     uint64_t txg_start, zbookmark_phys_t *resume, int flags,
548ad135b5dSChristopher Siden     blkptr_cb_t func, void *arg)
549fa9e4066Sahrens {
5506e0cbcaaSMatthew Ahrens 	traverse_data_t td;
5516e0cbcaaSMatthew Ahrens 	prefetch_data_t pd = { 0 };
5527802d7bfSMatthew Ahrens 	zbookmark_phys_t czb;
55388b7b0f2SMatthew Ahrens 	int err;
554fa9e4066Sahrens 
555ad135b5dSChristopher Siden 	ASSERT(ds == NULL || objset == ds->ds_object);
556ad135b5dSChristopher Siden 	ASSERT(!(flags & TRAVERSE_PRE) || !(flags & TRAVERSE_POST));
557ad135b5dSChristopher Siden 
55888b7b0f2SMatthew Ahrens 	td.td_spa = spa;
559ad135b5dSChristopher Siden 	td.td_objset = objset;
56088b7b0f2SMatthew Ahrens 	td.td_rootbp = rootbp;
56188b7b0f2SMatthew Ahrens 	td.td_min_txg = txg_start;
562ad135b5dSChristopher Siden 	td.td_resume = resume;
56388b7b0f2SMatthew Ahrens 	td.td_func = func;
56488b7b0f2SMatthew Ahrens 	td.td_arg = arg;
56588b7b0f2SMatthew Ahrens 	td.td_pfd = &pd;
56688b7b0f2SMatthew Ahrens 	td.td_flags = flags;
5677fd05ac4SMatthew Ahrens 	td.td_paused = B_FALSE;
568286ef713SPaul Dagnelie 	td.td_realloc_possible = (txg_start == 0 ? B_FALSE : B_TRUE);
56988b7b0f2SMatthew Ahrens 
570f7950bf1SMatthew Ahrens 	if (spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) {
571f7950bf1SMatthew Ahrens 		VERIFY(spa_feature_enabled_txg(spa,
572f7950bf1SMatthew Ahrens 		    SPA_FEATURE_HOLE_BIRTH, &td.td_hole_birth_enabled_txg));
573f7950bf1SMatthew Ahrens 	} else {
574286ef713SPaul Dagnelie 		td.td_hole_birth_enabled_txg = UINT64_MAX;
575f7950bf1SMatthew Ahrens 	}
576f7950bf1SMatthew Ahrens 
57788b7b0f2SMatthew Ahrens 	pd.pd_flags = flags;
5789c3fd121SMatthew Ahrens 	if (resume != NULL)
5799c3fd121SMatthew Ahrens 		pd.pd_resume = *resume;
58088b7b0f2SMatthew Ahrens 	mutex_init(&pd.pd_mtx, NULL, MUTEX_DEFAULT, NULL);
58188b7b0f2SMatthew Ahrens 	cv_init(&pd.pd_cv, NULL, CV_DEFAULT, NULL);
58288b7b0f2SMatthew Ahrens 
5836e0cbcaaSMatthew Ahrens 	/* See comment on ZIL traversal in dsl_scan_visitds. */
584bc9014e6SJustin Gibbs 	if (ds != NULL && !ds->ds_is_snapshot && !BP_IS_HOLE(rootbp)) {
5857adb730bSGeorge Wilson 		arc_flags_t flags = ARC_FLAG_WAIT;
5863b2aab18SMatthew Ahrens 		objset_phys_t *osp;
5873b2aab18SMatthew Ahrens 		arc_buf_t *buf;
5886e0cbcaaSMatthew Ahrens 
5893b2aab18SMatthew Ahrens 		err = arc_read(NULL, td.td_spa, rootbp,
5903b2aab18SMatthew Ahrens 		    arc_getbuf_func, &buf,
5913b2aab18SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, NULL);
5923b2aab18SMatthew Ahrens 		if (err != 0)
5936e0cbcaaSMatthew Ahrens 			return (err);
5946e0cbcaaSMatthew Ahrens 
5953b2aab18SMatthew Ahrens 		osp = buf->b_data;
5963b2aab18SMatthew Ahrens 		traverse_zil(&td, &osp->os_zil_header);
597*dcbf3bd6SGeorge Wilson 		arc_buf_destroy(buf, &buf);
5986e0cbcaaSMatthew Ahrens 	}
5996e0cbcaaSMatthew Ahrens 
600b4709335SMatthew Ahrens 	if (!(flags & TRAVERSE_PREFETCH_DATA) ||
60188b7b0f2SMatthew Ahrens 	    0 == taskq_dispatch(system_taskq, traverse_prefetch_thread,
60288b7b0f2SMatthew Ahrens 	    &td, TQ_NOQUEUE))
60388b7b0f2SMatthew Ahrens 		pd.pd_exited = B_TRUE;
60488b7b0f2SMatthew Ahrens 
6056e0cbcaaSMatthew Ahrens 	SET_BOOKMARK(&czb, td.td_objset,
606b24ab676SJeff Bonwick 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
6071b912ec7SGeorge Wilson 	err = traverse_visitbp(&td, NULL, rootbp, &czb);
60888b7b0f2SMatthew Ahrens 
60988b7b0f2SMatthew Ahrens 	mutex_enter(&pd.pd_mtx);
61088b7b0f2SMatthew Ahrens 	pd.pd_cancel = B_TRUE;
61188b7b0f2SMatthew Ahrens 	cv_broadcast(&pd.pd_cv);
61288b7b0f2SMatthew Ahrens 	while (!pd.pd_exited)
61388b7b0f2SMatthew Ahrens 		cv_wait(&pd.pd_cv, &pd.pd_mtx);
61488b7b0f2SMatthew Ahrens 	mutex_exit(&pd.pd_mtx);
61588b7b0f2SMatthew Ahrens 
61688b7b0f2SMatthew Ahrens 	mutex_destroy(&pd.pd_mtx);
61788b7b0f2SMatthew Ahrens 	cv_destroy(&pd.pd_cv);
618fa9e4066Sahrens 
61988b7b0f2SMatthew Ahrens 	return (err);
620fa9e4066Sahrens }
621fa9e4066Sahrens 
62288b7b0f2SMatthew Ahrens /*
62388b7b0f2SMatthew Ahrens  * NB: dataset must not be changing on-disk (eg, is a snapshot or we are
62488b7b0f2SMatthew Ahrens  * in syncing context).
62588b7b0f2SMatthew Ahrens  */
62688b7b0f2SMatthew Ahrens int
6279c3fd121SMatthew Ahrens traverse_dataset_resume(dsl_dataset_t *ds, uint64_t txg_start,
6289c3fd121SMatthew Ahrens     zbookmark_phys_t *resume,
6299c3fd121SMatthew Ahrens     int flags, blkptr_cb_t func, void *arg)
630fa9e4066Sahrens {
631ad135b5dSChristopher Siden 	return (traverse_impl(ds->ds_dir->dd_pool->dp_spa, ds, ds->ds_object,
6329c3fd121SMatthew Ahrens 	    &dsl_dataset_phys(ds)->ds_bp, txg_start, resume, flags, func, arg));
6339c3fd121SMatthew Ahrens }
6349c3fd121SMatthew Ahrens 
6359c3fd121SMatthew Ahrens int
6369c3fd121SMatthew Ahrens traverse_dataset(dsl_dataset_t *ds, uint64_t txg_start,
6379c3fd121SMatthew Ahrens     int flags, blkptr_cb_t func, void *arg)
6389c3fd121SMatthew Ahrens {
6399c3fd121SMatthew Ahrens 	return (traverse_dataset_resume(ds, txg_start, NULL, flags, func, arg));
640ad135b5dSChristopher Siden }
641ad135b5dSChristopher Siden 
642ad135b5dSChristopher Siden int
643ad135b5dSChristopher Siden traverse_dataset_destroyed(spa_t *spa, blkptr_t *blkptr,
6447802d7bfSMatthew Ahrens     uint64_t txg_start, zbookmark_phys_t *resume, int flags,
645ad135b5dSChristopher Siden     blkptr_cb_t func, void *arg)
646ad135b5dSChristopher Siden {
647ad135b5dSChristopher Siden 	return (traverse_impl(spa, NULL, ZB_DESTROYED_OBJSET,
648ad135b5dSChristopher Siden 	    blkptr, txg_start, resume, flags, func, arg));
649fa9e4066Sahrens }
650fa9e4066Sahrens 
65188b7b0f2SMatthew Ahrens /*
65288b7b0f2SMatthew Ahrens  * NB: pool must not be changing on-disk (eg, from zdb or sync context).
65388b7b0f2SMatthew Ahrens  */
65488b7b0f2SMatthew Ahrens int
655bbfd46c4SJeff Bonwick traverse_pool(spa_t *spa, uint64_t txg_start, int flags,
656bbfd46c4SJeff Bonwick     blkptr_cb_t func, void *arg)
657fa9e4066Sahrens {
6587fd05ac4SMatthew Ahrens 	int err;
65988b7b0f2SMatthew Ahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
66088b7b0f2SMatthew Ahrens 	objset_t *mos = dp->dp_meta_objset;
661cd088ea4SVictor Latushkin 	boolean_t hard = (flags & TRAVERSE_HARD);
66288b7b0f2SMatthew Ahrens 
66388b7b0f2SMatthew Ahrens 	/* visit the MOS */
664ad135b5dSChristopher Siden 	err = traverse_impl(spa, NULL, 0, spa_get_rootblkptr(spa),
665ad135b5dSChristopher Siden 	    txg_start, NULL, flags, func, arg);
6663b2aab18SMatthew Ahrens 	if (err != 0)
66788b7b0f2SMatthew Ahrens 		return (err);
66888b7b0f2SMatthew Ahrens 
66988b7b0f2SMatthew Ahrens 	/* visit each dataset */
6709c3fd121SMatthew Ahrens 	for (uint64_t obj = 1; err == 0;
6719c3fd121SMatthew Ahrens 	    err = dmu_object_next(mos, &obj, B_FALSE, txg_start)) {
67288b7b0f2SMatthew Ahrens 		dmu_object_info_t doi;
67388b7b0f2SMatthew Ahrens 
67488b7b0f2SMatthew Ahrens 		err = dmu_object_info(mos, obj, &doi);
6753b2aab18SMatthew Ahrens 		if (err != 0) {
6767fd05ac4SMatthew Ahrens 			if (hard)
6777fd05ac4SMatthew Ahrens 				continue;
6787fd05ac4SMatthew Ahrens 			break;
679cd088ea4SVictor Latushkin 		}
68088b7b0f2SMatthew Ahrens 
6812acef22dSMatthew Ahrens 		if (doi.doi_bonus_type == DMU_OT_DSL_DATASET) {
68288b7b0f2SMatthew Ahrens 			dsl_dataset_t *ds;
683468c413aSTim Haley 			uint64_t txg = txg_start;
684468c413aSTim Haley 
6853b2aab18SMatthew Ahrens 			dsl_pool_config_enter(dp, FTAG);
68688b7b0f2SMatthew Ahrens 			err = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
6873b2aab18SMatthew Ahrens 			dsl_pool_config_exit(dp, FTAG);
6883b2aab18SMatthew Ahrens 			if (err != 0) {
6897fd05ac4SMatthew Ahrens 				if (hard)
6907fd05ac4SMatthew Ahrens 					continue;
6917fd05ac4SMatthew Ahrens 				break;
692cd088ea4SVictor Latushkin 			}
693c1379625SJustin T. Gibbs 			if (dsl_dataset_phys(ds)->ds_prev_snap_txg > txg)
694c1379625SJustin T. Gibbs 				txg = dsl_dataset_phys(ds)->ds_prev_snap_txg;
695bbfd46c4SJeff Bonwick 			err = traverse_dataset(ds, txg, flags, func, arg);
69688b7b0f2SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
6977fd05ac4SMatthew Ahrens 			if (err != 0)
6987fd05ac4SMatthew Ahrens 				break;
69988b7b0f2SMatthew Ahrens 		}
700fa9e4066Sahrens 	}
70188b7b0f2SMatthew Ahrens 	if (err == ESRCH)
70288b7b0f2SMatthew Ahrens 		err = 0;
7037fd05ac4SMatthew Ahrens 	return (err);
704fa9e4066Sahrens }
705