xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_traverse.c (revision f76886de6cd6914424d9f6c25bd9d93d87889269)
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 */
42*f76886deSPaul Dagnelie boolean_t send_holes_without_birth_time = B_TRUE;
4344f92b7dSChris Kirby 
446e0cbcaaSMatthew Ahrens typedef struct prefetch_data {
4588b7b0f2SMatthew Ahrens 	kmutex_t pd_mtx;
4688b7b0f2SMatthew Ahrens 	kcondvar_t pd_cv;
4734d7ce05SGeorge Wilson 	int32_t pd_bytes_fetched;
4888b7b0f2SMatthew Ahrens 	int pd_flags;
4988b7b0f2SMatthew Ahrens 	boolean_t pd_cancel;
5088b7b0f2SMatthew Ahrens 	boolean_t pd_exited;
519c3fd121SMatthew Ahrens 	zbookmark_phys_t pd_resume;
526e0cbcaaSMatthew Ahrens } prefetch_data_t;
5388b7b0f2SMatthew Ahrens 
546e0cbcaaSMatthew Ahrens typedef struct traverse_data {
5588b7b0f2SMatthew Ahrens 	spa_t *td_spa;
5688b7b0f2SMatthew Ahrens 	uint64_t td_objset;
5788b7b0f2SMatthew Ahrens 	blkptr_t *td_rootbp;
5888b7b0f2SMatthew Ahrens 	uint64_t td_min_txg;
597802d7bfSMatthew Ahrens 	zbookmark_phys_t *td_resume;
6088b7b0f2SMatthew Ahrens 	int td_flags;
616e0cbcaaSMatthew Ahrens 	prefetch_data_t *td_pfd;
627fd05ac4SMatthew Ahrens 	boolean_t td_paused;
63f7950bf1SMatthew Ahrens 	uint64_t td_hole_birth_enabled_txg;
6488b7b0f2SMatthew Ahrens 	blkptr_cb_t *td_func;
6588b7b0f2SMatthew Ahrens 	void *td_arg;
66286ef713SPaul Dagnelie 	boolean_t td_realloc_possible;
676e0cbcaaSMatthew Ahrens } traverse_data_t;
68fa9e4066Sahrens 
696e0cbcaaSMatthew Ahrens static int traverse_dnode(traverse_data_t *td, const dnode_phys_t *dnp,
701b912ec7SGeorge Wilson     uint64_t objset, uint64_t object);
71b4709335SMatthew Ahrens static void prefetch_dnode_metadata(traverse_data_t *td, const dnode_phys_t *,
721b912ec7SGeorge Wilson     uint64_t objset, uint64_t object);
7314843421SMatthew Ahrens 
74b24ab676SJeff Bonwick static int
755dabedeeSbonwick traverse_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
76ea8dc4b6Seschrock {
776e0cbcaaSMatthew Ahrens 	traverse_data_t *td = arg;
787802d7bfSMatthew Ahrens 	zbookmark_phys_t zb;
79ea8dc4b6Seschrock 
8043466aaeSMax Grossman 	if (BP_IS_HOLE(bp))
81b24ab676SJeff Bonwick 		return (0);
825dabedeeSbonwick 
8388b7b0f2SMatthew Ahrens 	if (claim_txg == 0 && bp->blk_birth >= spa_first_txg(td->td_spa))
84b24ab676SJeff Bonwick 		return (0);
8588b7b0f2SMatthew Ahrens 
86b24ab676SJeff Bonwick 	SET_BOOKMARK(&zb, td->td_objset, ZB_ZIL_OBJECT, ZB_ZIL_LEVEL,
87b24ab676SJeff Bonwick 	    bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
88b24ab676SJeff Bonwick 
891b912ec7SGeorge Wilson 	(void) td->td_func(td->td_spa, zilog, bp, &zb, NULL, td->td_arg);
90b24ab676SJeff Bonwick 
91b24ab676SJeff Bonwick 	return (0);
92ea8dc4b6Seschrock }
93ea8dc4b6Seschrock 
94b24ab676SJeff Bonwick static int
955dabedeeSbonwick traverse_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg)
96ea8dc4b6Seschrock {
976e0cbcaaSMatthew Ahrens 	traverse_data_t *td = arg;
98ea8dc4b6Seschrock 
99ea8dc4b6Seschrock 	if (lrc->lrc_txtype == TX_WRITE) {
100ea8dc4b6Seschrock 		lr_write_t *lr = (lr_write_t *)lrc;
101ea8dc4b6Seschrock 		blkptr_t *bp = &lr->lr_blkptr;
1027802d7bfSMatthew Ahrens 		zbookmark_phys_t zb;
103ea8dc4b6Seschrock 
10443466aaeSMax Grossman 		if (BP_IS_HOLE(bp))
105b24ab676SJeff Bonwick 			return (0);
1065dabedeeSbonwick 
10788b7b0f2SMatthew Ahrens 		if (claim_txg == 0 || bp->blk_birth < claim_txg)
108b24ab676SJeff Bonwick 			return (0);
109b24ab676SJeff Bonwick 
1106e0cbcaaSMatthew Ahrens 		SET_BOOKMARK(&zb, td->td_objset, lr->lr_foid,
1116e0cbcaaSMatthew Ahrens 		    ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp));
11288b7b0f2SMatthew Ahrens 
1131b912ec7SGeorge Wilson 		(void) td->td_func(td->td_spa, zilog, bp, &zb, NULL,
114b24ab676SJeff Bonwick 		    td->td_arg);
115ea8dc4b6Seschrock 	}
116b24ab676SJeff Bonwick 	return (0);
117ea8dc4b6Seschrock }
118ea8dc4b6Seschrock 
119ea8dc4b6Seschrock static void
1206e0cbcaaSMatthew Ahrens traverse_zil(traverse_data_t *td, zil_header_t *zh)
121ea8dc4b6Seschrock {
1225dabedeeSbonwick 	uint64_t claim_txg = zh->zh_claim_txg;
123ea8dc4b6Seschrock 	zilog_t *zilog;
124ea8dc4b6Seschrock 
1255dabedeeSbonwick 	/*
1265dabedeeSbonwick 	 * We only want to visit blocks that have been claimed but not yet
127b24ab676SJeff Bonwick 	 * replayed; plus, in read-only mode, blocks that are already stable.
1285dabedeeSbonwick 	 */
1298ad4d6ddSJeff Bonwick 	if (claim_txg == 0 && spa_writeable(td->td_spa))
1305dabedeeSbonwick 		return;
1315dabedeeSbonwick 
13288b7b0f2SMatthew Ahrens 	zilog = zil_alloc(spa_get_dsl(td->td_spa)->dp_meta_objset, zh);
133ea8dc4b6Seschrock 
13488b7b0f2SMatthew Ahrens 	(void) zil_parse(zilog, traverse_zil_block, traverse_zil_record, td,
1355dabedeeSbonwick 	    claim_txg);
136ea8dc4b6Seschrock 
137ea8dc4b6Seschrock 	zil_free(zilog);
138ea8dc4b6Seschrock }
139ea8dc4b6Seschrock 
140ad135b5dSChristopher Siden typedef enum resume_skip {
141ad135b5dSChristopher Siden 	RESUME_SKIP_ALL,
142ad135b5dSChristopher Siden 	RESUME_SKIP_NONE,
143ad135b5dSChristopher Siden 	RESUME_SKIP_CHILDREN
144ad135b5dSChristopher Siden } resume_skip_t;
145ad135b5dSChristopher Siden 
146ad135b5dSChristopher Siden /*
147ad135b5dSChristopher Siden  * Returns RESUME_SKIP_ALL if td indicates that we are resuming a traversal and
148ad135b5dSChristopher Siden  * the block indicated by zb does not need to be visited at all. Returns
149ad135b5dSChristopher Siden  * RESUME_SKIP_CHILDREN if we are resuming a post traversal and we reach the
150ad135b5dSChristopher Siden  * resume point. This indicates that this block should be visited but not its
151ad135b5dSChristopher Siden  * children (since they must have been visited in a previous traversal).
152ad135b5dSChristopher Siden  * Otherwise returns RESUME_SKIP_NONE.
153ad135b5dSChristopher Siden  */
154ad135b5dSChristopher Siden static resume_skip_t
155ad135b5dSChristopher Siden resume_skip_check(traverse_data_t *td, const dnode_phys_t *dnp,
1567802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb)
157ad135b5dSChristopher Siden {
158ad135b5dSChristopher Siden 	if (td->td_resume != NULL && !ZB_IS_ZERO(td->td_resume)) {
159ad135b5dSChristopher Siden 		/*
160ad135b5dSChristopher Siden 		 * If we already visited this bp & everything below,
161ad135b5dSChristopher Siden 		 * don't bother doing it again.
162ad135b5dSChristopher Siden 		 */
163a2cdcdd2SPaul Dagnelie 		if (zbookmark_subtree_completed(dnp, zb, td->td_resume))
164ad135b5dSChristopher Siden 			return (RESUME_SKIP_ALL);
165ad135b5dSChristopher Siden 
166ad135b5dSChristopher Siden 		/*
167ad135b5dSChristopher Siden 		 * If we found the block we're trying to resume from, zero
168ad135b5dSChristopher Siden 		 * the bookmark out to indicate that we have resumed.
169ad135b5dSChristopher Siden 		 */
170ad135b5dSChristopher Siden 		if (bcmp(zb, td->td_resume, sizeof (*zb)) == 0) {
171ad135b5dSChristopher Siden 			bzero(td->td_resume, sizeof (*zb));
172ad135b5dSChristopher Siden 			if (td->td_flags & TRAVERSE_POST)
173ad135b5dSChristopher Siden 				return (RESUME_SKIP_CHILDREN);
174ad135b5dSChristopher Siden 		}
175ad135b5dSChristopher Siden 	}
176ad135b5dSChristopher Siden 	return (RESUME_SKIP_NONE);
177ad135b5dSChristopher Siden }
178ad135b5dSChristopher Siden 
179b4709335SMatthew Ahrens static void
180b4709335SMatthew Ahrens traverse_prefetch_metadata(traverse_data_t *td,
1817802d7bfSMatthew Ahrens     const blkptr_t *bp, const zbookmark_phys_t *zb)
182b4709335SMatthew Ahrens {
1837adb730bSGeorge Wilson 	arc_flags_t flags = ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH;
184b4709335SMatthew Ahrens 
185b4709335SMatthew Ahrens 	if (!(td->td_flags & TRAVERSE_PREFETCH_METADATA))
186b4709335SMatthew Ahrens 		return;
187b4709335SMatthew Ahrens 	/*
188b4709335SMatthew Ahrens 	 * If we are in the process of resuming, don't prefetch, because
189b4709335SMatthew Ahrens 	 * some children will not be needed (and in fact may have already
190b4709335SMatthew Ahrens 	 * been freed).
191b4709335SMatthew Ahrens 	 */
192b4709335SMatthew Ahrens 	if (td->td_resume != NULL && !ZB_IS_ZERO(td->td_resume))
193b4709335SMatthew Ahrens 		return;
194b4709335SMatthew Ahrens 	if (BP_IS_HOLE(bp) || bp->blk_birth <= td->td_min_txg)
195b4709335SMatthew Ahrens 		return;
196b4709335SMatthew Ahrens 	if (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_DNODE)
197b4709335SMatthew Ahrens 		return;
198b4709335SMatthew Ahrens 
1991b912ec7SGeorge Wilson 	(void) arc_read(NULL, td->td_spa, bp, NULL, NULL,
2001b912ec7SGeorge Wilson 	    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
201b4709335SMatthew Ahrens }
202b4709335SMatthew Ahrens 
20306315b79SMatthew Ahrens static boolean_t
20406315b79SMatthew Ahrens prefetch_needed(prefetch_data_t *pfd, const blkptr_t *bp)
20506315b79SMatthew Ahrens {
20606315b79SMatthew Ahrens 	ASSERT(pfd->pd_flags & TRAVERSE_PREFETCH_DATA);
20706315b79SMatthew Ahrens 	if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp) ||
20806315b79SMatthew Ahrens 	    BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG)
20906315b79SMatthew Ahrens 		return (B_FALSE);
21006315b79SMatthew Ahrens 	return (B_TRUE);
21106315b79SMatthew Ahrens }
21206315b79SMatthew Ahrens 
213fa9e4066Sahrens static int
2146e0cbcaaSMatthew Ahrens traverse_visitbp(traverse_data_t *td, const dnode_phys_t *dnp,
2157802d7bfSMatthew Ahrens     const blkptr_t *bp, const zbookmark_phys_t *zb)
216fa9e4066Sahrens {
2177802d7bfSMatthew Ahrens 	zbookmark_phys_t czb;
2187fd05ac4SMatthew Ahrens 	int err = 0;
21988b7b0f2SMatthew Ahrens 	arc_buf_t *buf = NULL;
2206e0cbcaaSMatthew Ahrens 	prefetch_data_t *pd = td->td_pfd;
221cd088ea4SVictor Latushkin 	boolean_t hard = td->td_flags & TRAVERSE_HARD;
222ad135b5dSChristopher Siden 
223ad135b5dSChristopher Siden 	switch (resume_skip_check(td, dnp, zb)) {
224ad135b5dSChristopher Siden 	case RESUME_SKIP_ALL:
225ad135b5dSChristopher Siden 		return (0);
226ad135b5dSChristopher Siden 	case RESUME_SKIP_CHILDREN:
227ad135b5dSChristopher Siden 		goto post;
228ad135b5dSChristopher Siden 	case RESUME_SKIP_NONE:
229ad135b5dSChristopher Siden 		break;
230ad135b5dSChristopher Siden 	default:
231ad135b5dSChristopher Siden 		ASSERT(0);
232ad135b5dSChristopher Siden 	}
233fa9e4066Sahrens 
23443466aaeSMax Grossman 	if (bp->blk_birth == 0) {
235f7950bf1SMatthew Ahrens 		/*
236286ef713SPaul Dagnelie 		 * Since this block has a birth time of 0 it must be one of
237286ef713SPaul Dagnelie 		 * two things: a hole created before the
238286ef713SPaul Dagnelie 		 * SPA_FEATURE_HOLE_BIRTH feature was enabled, or a hole
239286ef713SPaul Dagnelie 		 * which has always been a hole in an object.
240286ef713SPaul Dagnelie 		 *
241286ef713SPaul Dagnelie 		 * If a file is written sparsely, then the unwritten parts of
242286ef713SPaul Dagnelie 		 * the file were "always holes" -- that is, they have been
243286ef713SPaul Dagnelie 		 * holes since this object was allocated.  However, we (and
244286ef713SPaul Dagnelie 		 * our callers) can not necessarily tell when an object was
245286ef713SPaul Dagnelie 		 * allocated.  Therefore, if it's possible that this object
246286ef713SPaul Dagnelie 		 * was freed and then its object number reused, we need to
247286ef713SPaul Dagnelie 		 * visit all the holes with birth==0.
248286ef713SPaul Dagnelie 		 *
249286ef713SPaul Dagnelie 		 * If it isn't possible that the object number was reused,
250286ef713SPaul Dagnelie 		 * then if SPA_FEATURE_HOLE_BIRTH was enabled before we wrote
251286ef713SPaul Dagnelie 		 * all the blocks we will visit as part of this traversal,
252286ef713SPaul Dagnelie 		 * then this hole must have always existed, so we can skip
253286ef713SPaul Dagnelie 		 * it.  We visit blocks born after (exclusive) td_min_txg.
254286ef713SPaul Dagnelie 		 *
255286ef713SPaul Dagnelie 		 * Note that the meta-dnode cannot be reallocated.
256f7950bf1SMatthew Ahrens 		 */
257*f76886deSPaul Dagnelie 		if (!send_holes_without_birth_time &&
258*f76886deSPaul Dagnelie 		    (!td->td_realloc_possible ||
259286ef713SPaul Dagnelie 		    zb->zb_object == DMU_META_DNODE_OBJECT) &&
260286ef713SPaul Dagnelie 		    td->td_hole_birth_enabled_txg <= td->td_min_txg)
261f7950bf1SMatthew Ahrens 			return (0);
26243466aaeSMax Grossman 	} else if (bp->blk_birth <= td->td_min_txg) {
26343466aaeSMax Grossman 		return (0);
26443466aaeSMax Grossman 	}
26543466aaeSMax Grossman 
26606315b79SMatthew Ahrens 	if (pd != NULL && !pd->pd_exited && prefetch_needed(pd, bp)) {
26734d7ce05SGeorge Wilson 		uint64_t size = BP_GET_LSIZE(bp);
26888b7b0f2SMatthew Ahrens 		mutex_enter(&pd->pd_mtx);
26934d7ce05SGeorge Wilson 		ASSERT(pd->pd_bytes_fetched >= 0);
27034d7ce05SGeorge Wilson 		while (pd->pd_bytes_fetched < size && !pd->pd_exited)
27188b7b0f2SMatthew Ahrens 			cv_wait(&pd->pd_cv, &pd->pd_mtx);
27234d7ce05SGeorge Wilson 		pd->pd_bytes_fetched -= size;
27388b7b0f2SMatthew Ahrens 		cv_broadcast(&pd->pd_cv);
27488b7b0f2SMatthew Ahrens 		mutex_exit(&pd->pd_mtx);
275fa9e4066Sahrens 	}
276fa9e4066Sahrens 
27706315b79SMatthew Ahrens 	if (BP_IS_HOLE(bp)) {
27806315b79SMatthew Ahrens 		err = td->td_func(td->td_spa, NULL, bp, zb, dnp, td->td_arg);
27906315b79SMatthew Ahrens 		if (err != 0)
28006315b79SMatthew Ahrens 			goto post;
28106315b79SMatthew Ahrens 		return (0);
28206315b79SMatthew Ahrens 	}
28306315b79SMatthew Ahrens 
28488b7b0f2SMatthew Ahrens 	if (td->td_flags & TRAVERSE_PRE) {
2851b912ec7SGeorge Wilson 		err = td->td_func(td->td_spa, NULL, bp, zb, dnp,
2863f9d6ad7SLin Ling 		    td->td_arg);
28799d5e173STim Haley 		if (err == TRAVERSE_VISIT_NO_CHILDREN)
28899d5e173STim Haley 			return (0);
289ad135b5dSChristopher Siden 		if (err != 0)
290ad135b5dSChristopher Siden 			goto post;
291fa9e4066Sahrens 	}
292fa9e4066Sahrens 
29388b7b0f2SMatthew Ahrens 	if (BP_GET_LEVEL(bp) > 0) {
2947adb730bSGeorge Wilson 		arc_flags_t flags = ARC_FLAG_WAIT;
29588b7b0f2SMatthew Ahrens 		int i;
29688b7b0f2SMatthew Ahrens 		blkptr_t *cbp;
29788b7b0f2SMatthew Ahrens 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
29888b7b0f2SMatthew Ahrens 
2991b912ec7SGeorge Wilson 		err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf,
30088b7b0f2SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
3013b2aab18SMatthew Ahrens 		if (err != 0)
3027fd05ac4SMatthew Ahrens 			goto post;
303b4709335SMatthew Ahrens 		cbp = buf->b_data;
304b4709335SMatthew Ahrens 
305b4709335SMatthew Ahrens 		for (i = 0; i < epb; i++) {
306b4709335SMatthew Ahrens 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
307b4709335SMatthew Ahrens 			    zb->zb_level - 1,
308b4709335SMatthew Ahrens 			    zb->zb_blkid * epb + i);
3091b912ec7SGeorge Wilson 			traverse_prefetch_metadata(td, &cbp[i], &czb);
310b4709335SMatthew Ahrens 		}
31188b7b0f2SMatthew Ahrens 
31288b7b0f2SMatthew Ahrens 		/* recursively visitbp() blocks below this */
313b4709335SMatthew Ahrens 		for (i = 0; i < epb; i++) {
31488b7b0f2SMatthew Ahrens 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
31588b7b0f2SMatthew Ahrens 			    zb->zb_level - 1,
31688b7b0f2SMatthew Ahrens 			    zb->zb_blkid * epb + i);
3171b912ec7SGeorge Wilson 			err = traverse_visitbp(td, dnp, &cbp[i], &czb);
3187fd05ac4SMatthew Ahrens 			if (err != 0)
3197fd05ac4SMatthew Ahrens 				break;
32088b7b0f2SMatthew Ahrens 		}
32188b7b0f2SMatthew Ahrens 	} else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) {
3227adb730bSGeorge Wilson 		arc_flags_t flags = ARC_FLAG_WAIT;
32314843421SMatthew Ahrens 		int i;
32488b7b0f2SMatthew Ahrens 		int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
32588b7b0f2SMatthew Ahrens 
3261b912ec7SGeorge Wilson 		err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf,
32788b7b0f2SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
3283b2aab18SMatthew Ahrens 		if (err != 0)
3297fd05ac4SMatthew Ahrens 			goto post;
3309c3fd121SMatthew Ahrens 		dnode_phys_t *child_dnp = buf->b_data;
331b4709335SMatthew Ahrens 
332b4709335SMatthew Ahrens 		for (i = 0; i < epb; i++) {
3339c3fd121SMatthew Ahrens 			prefetch_dnode_metadata(td, &child_dnp[i],
3349c3fd121SMatthew Ahrens 			    zb->zb_objset, zb->zb_blkid * epb + i);
335b4709335SMatthew Ahrens 		}
33688b7b0f2SMatthew Ahrens 
33788b7b0f2SMatthew Ahrens 		/* recursively visitbp() blocks below this */
338b4709335SMatthew Ahrens 		for (i = 0; i < epb; i++) {
3399c3fd121SMatthew Ahrens 			err = traverse_dnode(td, &child_dnp[i],
3409c3fd121SMatthew Ahrens 			    zb->zb_objset, zb->zb_blkid * epb + i);
3417fd05ac4SMatthew Ahrens 			if (err != 0)
3427fd05ac4SMatthew Ahrens 				break;
343fa9e4066Sahrens 		}
34488b7b0f2SMatthew Ahrens 	} else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
3457adb730bSGeorge Wilson 		arc_flags_t flags = ARC_FLAG_WAIT;
34688b7b0f2SMatthew Ahrens 
3471b912ec7SGeorge Wilson 		err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf,
34888b7b0f2SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
3493b2aab18SMatthew Ahrens 		if (err != 0)
3507fd05ac4SMatthew Ahrens 			goto post;
35188b7b0f2SMatthew Ahrens 
3529c3fd121SMatthew Ahrens 		objset_phys_t *osp = buf->b_data;
3539c3fd121SMatthew Ahrens 		prefetch_dnode_metadata(td, &osp->os_meta_dnode, zb->zb_objset,
354b4709335SMatthew Ahrens 		    DMU_META_DNODE_OBJECT);
355286ef713SPaul Dagnelie 		/*
356286ef713SPaul Dagnelie 		 * See the block comment above for the goal of this variable.
357286ef713SPaul Dagnelie 		 * If the maxblkid of the meta-dnode is 0, then we know that
358286ef713SPaul Dagnelie 		 * we've never had more than DNODES_PER_BLOCK objects in the
359286ef713SPaul Dagnelie 		 * dataset, which means we can't have reused any object ids.
360286ef713SPaul Dagnelie 		 */
361286ef713SPaul Dagnelie 		if (osp->os_meta_dnode.dn_maxblkid == 0)
362286ef713SPaul Dagnelie 			td->td_realloc_possible = B_FALSE;
363286ef713SPaul Dagnelie 
364b4709335SMatthew Ahrens 		if (arc_buf_size(buf) >= sizeof (objset_phys_t)) {
365b4709335SMatthew Ahrens 			prefetch_dnode_metadata(td, &osp->os_groupused_dnode,
36648f1b90eSMatthew Ahrens 			    zb->zb_objset, DMU_GROUPUSED_OBJECT);
36748f1b90eSMatthew Ahrens 			prefetch_dnode_metadata(td, &osp->os_userused_dnode,
3681b912ec7SGeorge Wilson 			    zb->zb_objset, DMU_USERUSED_OBJECT);
369b4709335SMatthew Ahrens 		}
370b4709335SMatthew Ahrens 
3719c3fd121SMatthew Ahrens 		err = traverse_dnode(td, &osp->os_meta_dnode, zb->zb_objset,
372b24ab676SJeff Bonwick 		    DMU_META_DNODE_OBJECT);
37314843421SMatthew Ahrens 		if (err == 0 && arc_buf_size(buf) >= sizeof (objset_phys_t)) {
3749c3fd121SMatthew Ahrens 			err = traverse_dnode(td, &osp->os_groupused_dnode,
3759c3fd121SMatthew Ahrens 			    zb->zb_objset, DMU_GROUPUSED_OBJECT);
37614843421SMatthew Ahrens 		}
37714843421SMatthew Ahrens 		if (err == 0 && arc_buf_size(buf) >= sizeof (objset_phys_t)) {
3789c3fd121SMatthew Ahrens 			err = traverse_dnode(td, &osp->os_userused_dnode,
3799c3fd121SMatthew Ahrens 			    zb->zb_objset, DMU_USERUSED_OBJECT);
38088b7b0f2SMatthew Ahrens 		}
38188b7b0f2SMatthew Ahrens 	}
382fa9e4066Sahrens 
38388b7b0f2SMatthew Ahrens 	if (buf)
384dcbf3bd6SGeorge Wilson 		arc_buf_destroy(buf, &buf);
385fa9e4066Sahrens 
386ad135b5dSChristopher Siden post:
3877fd05ac4SMatthew Ahrens 	if (err == 0 && (td->td_flags & TRAVERSE_POST))
3881b912ec7SGeorge Wilson 		err = td->td_func(td->td_spa, NULL, bp, zb, dnp, td->td_arg);
3897fd05ac4SMatthew Ahrens 
3907fd05ac4SMatthew Ahrens 	if (hard && (err == EIO || err == ECKSUM)) {
3917fd05ac4SMatthew Ahrens 		/*
3927fd05ac4SMatthew Ahrens 		 * Ignore this disk error as requested by the HARD flag,
3937fd05ac4SMatthew Ahrens 		 * and continue traversal.
3947fd05ac4SMatthew Ahrens 		 */
3957fd05ac4SMatthew Ahrens 		err = 0;
396ad135b5dSChristopher Siden 	}
397ad135b5dSChristopher Siden 
3987fd05ac4SMatthew Ahrens 	/*
3997fd05ac4SMatthew Ahrens 	 * If we are stopping here, set td_resume.
4007fd05ac4SMatthew Ahrens 	 */
4017fd05ac4SMatthew Ahrens 	if (td->td_resume != NULL && err != 0 && !td->td_paused) {
4027fd05ac4SMatthew Ahrens 		td->td_resume->zb_objset = zb->zb_objset;
4037fd05ac4SMatthew Ahrens 		td->td_resume->zb_object = zb->zb_object;
4047fd05ac4SMatthew Ahrens 		td->td_resume->zb_level = 0;
4057fd05ac4SMatthew Ahrens 		/*
4067fd05ac4SMatthew Ahrens 		 * If we have stopped on an indirect block (e.g. due to
4077fd05ac4SMatthew Ahrens 		 * i/o error), we have not visited anything below it.
4087fd05ac4SMatthew Ahrens 		 * Set the bookmark to the first level-0 block that we need
4097fd05ac4SMatthew Ahrens 		 * to visit.  This way, the resuming code does not need to
4107fd05ac4SMatthew Ahrens 		 * deal with resuming from indirect blocks.
4119c3fd121SMatthew Ahrens 		 *
4129c3fd121SMatthew Ahrens 		 * Note, if zb_level <= 0, dnp may be NULL, so we don't want
4139c3fd121SMatthew Ahrens 		 * to dereference it.
4147fd05ac4SMatthew Ahrens 		 */
4159c3fd121SMatthew Ahrens 		td->td_resume->zb_blkid = zb->zb_blkid;
4169c3fd121SMatthew Ahrens 		if (zb->zb_level > 0) {
4179c3fd121SMatthew Ahrens 			td->td_resume->zb_blkid <<= zb->zb_level *
4189c3fd121SMatthew Ahrens 			    (dnp->dn_indblkshift - SPA_BLKPTRSHIFT);
4199c3fd121SMatthew Ahrens 		}
4207fd05ac4SMatthew Ahrens 		td->td_paused = B_TRUE;
4213f9d6ad7SLin Ling 	}
422fa9e4066Sahrens 
4237fd05ac4SMatthew Ahrens 	return (err);
424fa9e4066Sahrens }
425fa9e4066Sahrens 
426b4709335SMatthew Ahrens static void
427b4709335SMatthew Ahrens prefetch_dnode_metadata(traverse_data_t *td, const dnode_phys_t *dnp,
4281b912ec7SGeorge Wilson     uint64_t objset, uint64_t object)
429b4709335SMatthew Ahrens {
430b4709335SMatthew Ahrens 	int j;
4317802d7bfSMatthew Ahrens 	zbookmark_phys_t czb;
432b4709335SMatthew Ahrens 
433b4709335SMatthew Ahrens 	for (j = 0; j < dnp->dn_nblkptr; j++) {
434b4709335SMatthew Ahrens 		SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j);
4351b912ec7SGeorge Wilson 		traverse_prefetch_metadata(td, &dnp->dn_blkptr[j], &czb);
436b4709335SMatthew Ahrens 	}
437b4709335SMatthew Ahrens 
438b4709335SMatthew Ahrens 	if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
439b4709335SMatthew Ahrens 		SET_BOOKMARK(&czb, objset, object, 0, DMU_SPILL_BLKID);
4401b912ec7SGeorge Wilson 		traverse_prefetch_metadata(td, &dnp->dn_spill, &czb);
441b4709335SMatthew Ahrens 	}
442b4709335SMatthew Ahrens }
443b4709335SMatthew Ahrens 
44414843421SMatthew Ahrens static int
4456e0cbcaaSMatthew Ahrens traverse_dnode(traverse_data_t *td, const dnode_phys_t *dnp,
4461b912ec7SGeorge Wilson     uint64_t objset, uint64_t object)
44714843421SMatthew Ahrens {
4487fd05ac4SMatthew Ahrens 	int j, err = 0;
4497802d7bfSMatthew Ahrens 	zbookmark_phys_t czb;
45014843421SMatthew Ahrens 
4519c3fd121SMatthew Ahrens 	if (object != DMU_META_DNODE_OBJECT && td->td_resume != NULL &&
4529c3fd121SMatthew Ahrens 	    object < td->td_resume->zb_object)
4539c3fd121SMatthew Ahrens 		return (0);
4549c3fd121SMatthew Ahrens 
455a2cdcdd2SPaul Dagnelie 	if (td->td_flags & TRAVERSE_PRE) {
456a2cdcdd2SPaul Dagnelie 		SET_BOOKMARK(&czb, objset, object, ZB_DNODE_LEVEL,
457a2cdcdd2SPaul Dagnelie 		    ZB_DNODE_BLKID);
458a2cdcdd2SPaul Dagnelie 		err = td->td_func(td->td_spa, NULL, NULL, &czb, dnp,
459a2cdcdd2SPaul Dagnelie 		    td->td_arg);
460a2cdcdd2SPaul Dagnelie 		if (err == TRAVERSE_VISIT_NO_CHILDREN)
461a2cdcdd2SPaul Dagnelie 			return (0);
462a2cdcdd2SPaul Dagnelie 		if (err != 0)
463a2cdcdd2SPaul Dagnelie 			return (err);
464a2cdcdd2SPaul Dagnelie 	}
465a2cdcdd2SPaul Dagnelie 
46614843421SMatthew Ahrens 	for (j = 0; j < dnp->dn_nblkptr; j++) {
46714843421SMatthew Ahrens 		SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j);
4681b912ec7SGeorge Wilson 		err = traverse_visitbp(td, dnp, &dnp->dn_blkptr[j], &czb);
4697fd05ac4SMatthew Ahrens 		if (err != 0)
4707fd05ac4SMatthew Ahrens 			break;
4713f9d6ad7SLin Ling 	}
4723f9d6ad7SLin Ling 
473a2cdcdd2SPaul Dagnelie 	if (err == 0 && (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) {
474b4709335SMatthew Ahrens 		SET_BOOKMARK(&czb, objset, object, 0, DMU_SPILL_BLKID);
4751b912ec7SGeorge Wilson 		err = traverse_visitbp(td, dnp, &dnp->dn_spill, &czb);
47614843421SMatthew Ahrens 	}
477a2cdcdd2SPaul Dagnelie 
478a2cdcdd2SPaul Dagnelie 	if (err == 0 && (td->td_flags & TRAVERSE_POST)) {
479a2cdcdd2SPaul Dagnelie 		SET_BOOKMARK(&czb, objset, object, ZB_DNODE_LEVEL,
480a2cdcdd2SPaul Dagnelie 		    ZB_DNODE_BLKID);
481a2cdcdd2SPaul Dagnelie 		err = td->td_func(td->td_spa, NULL, NULL, &czb, dnp,
482a2cdcdd2SPaul Dagnelie 		    td->td_arg);
483a2cdcdd2SPaul Dagnelie 		if (err == TRAVERSE_VISIT_NO_CHILDREN)
484a2cdcdd2SPaul Dagnelie 			return (0);
485a2cdcdd2SPaul Dagnelie 		if (err != 0)
486a2cdcdd2SPaul Dagnelie 			return (err);
487a2cdcdd2SPaul Dagnelie 	}
4887fd05ac4SMatthew Ahrens 	return (err);
48914843421SMatthew Ahrens }
49014843421SMatthew Ahrens 
49188b7b0f2SMatthew Ahrens /* ARGSUSED */
49288b7b0f2SMatthew Ahrens static int
493b24ab676SJeff Bonwick traverse_prefetcher(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
4947802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
495e7cbe64fSgw {
4966e0cbcaaSMatthew Ahrens 	prefetch_data_t *pfd = arg;
4977adb730bSGeorge Wilson 	arc_flags_t aflags = ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH;
498e7cbe64fSgw 
49934d7ce05SGeorge Wilson 	ASSERT(pfd->pd_bytes_fetched >= 0);
500a2cdcdd2SPaul Dagnelie 	if (bp == NULL)
501a2cdcdd2SPaul Dagnelie 		return (0);
50288b7b0f2SMatthew Ahrens 	if (pfd->pd_cancel)
503be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
504e7cbe64fSgw 
50506315b79SMatthew Ahrens 	if (!prefetch_needed(pfd, bp))
506fa9e4066Sahrens 		return (0);
507fa9e4066Sahrens 
50888b7b0f2SMatthew Ahrens 	mutex_enter(&pfd->pd_mtx);
50934d7ce05SGeorge Wilson 	while (!pfd->pd_cancel && pfd->pd_bytes_fetched >= zfs_pd_bytes_max)
51088b7b0f2SMatthew Ahrens 		cv_wait(&pfd->pd_cv, &pfd->pd_mtx);
51134d7ce05SGeorge Wilson 	pfd->pd_bytes_fetched += BP_GET_LSIZE(bp);
51288b7b0f2SMatthew Ahrens 	cv_broadcast(&pfd->pd_cv);
51388b7b0f2SMatthew Ahrens 	mutex_exit(&pfd->pd_mtx);
514fa9e4066Sahrens 
5151b912ec7SGeorge Wilson 	(void) arc_read(NULL, spa, bp, NULL, NULL, ZIO_PRIORITY_ASYNC_READ,
5161b912ec7SGeorge Wilson 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &aflags, zb);
517fa9e4066Sahrens 
51888b7b0f2SMatthew Ahrens 	return (0);
519fa9e4066Sahrens }
520fa9e4066Sahrens 
521fa9e4066Sahrens static void
52288b7b0f2SMatthew Ahrens traverse_prefetch_thread(void *arg)
523fa9e4066Sahrens {
5246e0cbcaaSMatthew Ahrens 	traverse_data_t *td_main = arg;
5256e0cbcaaSMatthew Ahrens 	traverse_data_t td = *td_main;
5267802d7bfSMatthew Ahrens 	zbookmark_phys_t czb;
527fa9e4066Sahrens 
52888b7b0f2SMatthew Ahrens 	td.td_func = traverse_prefetcher;
52988b7b0f2SMatthew Ahrens 	td.td_arg = td_main->td_pfd;
53088b7b0f2SMatthew Ahrens 	td.td_pfd = NULL;
5319c3fd121SMatthew Ahrens 	td.td_resume = &td_main->td_pfd->pd_resume;
532fa9e4066Sahrens 
533b24ab676SJeff Bonwick 	SET_BOOKMARK(&czb, td.td_objset,
534b24ab676SJeff Bonwick 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
5351b912ec7SGeorge Wilson 	(void) traverse_visitbp(&td, NULL, td.td_rootbp, &czb);
536fa9e4066Sahrens 
53788b7b0f2SMatthew Ahrens 	mutex_enter(&td_main->td_pfd->pd_mtx);
53888b7b0f2SMatthew Ahrens 	td_main->td_pfd->pd_exited = B_TRUE;
53988b7b0f2SMatthew Ahrens 	cv_broadcast(&td_main->td_pfd->pd_cv);
54088b7b0f2SMatthew Ahrens 	mutex_exit(&td_main->td_pfd->pd_mtx);
541fa9e4066Sahrens }
542fa9e4066Sahrens 
54388b7b0f2SMatthew Ahrens /*
54488b7b0f2SMatthew Ahrens  * NB: dataset must not be changing on-disk (eg, is a snapshot or we are
54588b7b0f2SMatthew Ahrens  * in syncing context).
54688b7b0f2SMatthew Ahrens  */
54788b7b0f2SMatthew Ahrens static int
548ad135b5dSChristopher Siden traverse_impl(spa_t *spa, dsl_dataset_t *ds, uint64_t objset, blkptr_t *rootbp,
5497802d7bfSMatthew Ahrens     uint64_t txg_start, zbookmark_phys_t *resume, int flags,
550ad135b5dSChristopher Siden     blkptr_cb_t func, void *arg)
551fa9e4066Sahrens {
5526e0cbcaaSMatthew Ahrens 	traverse_data_t td;
5536e0cbcaaSMatthew Ahrens 	prefetch_data_t pd = { 0 };
5547802d7bfSMatthew Ahrens 	zbookmark_phys_t czb;
55588b7b0f2SMatthew Ahrens 	int err;
556fa9e4066Sahrens 
557ad135b5dSChristopher Siden 	ASSERT(ds == NULL || objset == ds->ds_object);
558ad135b5dSChristopher Siden 	ASSERT(!(flags & TRAVERSE_PRE) || !(flags & TRAVERSE_POST));
559ad135b5dSChristopher Siden 
56088b7b0f2SMatthew Ahrens 	td.td_spa = spa;
561ad135b5dSChristopher Siden 	td.td_objset = objset;
56288b7b0f2SMatthew Ahrens 	td.td_rootbp = rootbp;
56388b7b0f2SMatthew Ahrens 	td.td_min_txg = txg_start;
564ad135b5dSChristopher Siden 	td.td_resume = resume;
56588b7b0f2SMatthew Ahrens 	td.td_func = func;
56688b7b0f2SMatthew Ahrens 	td.td_arg = arg;
56788b7b0f2SMatthew Ahrens 	td.td_pfd = &pd;
56888b7b0f2SMatthew Ahrens 	td.td_flags = flags;
5697fd05ac4SMatthew Ahrens 	td.td_paused = B_FALSE;
570286ef713SPaul Dagnelie 	td.td_realloc_possible = (txg_start == 0 ? B_FALSE : B_TRUE);
57188b7b0f2SMatthew Ahrens 
572f7950bf1SMatthew Ahrens 	if (spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) {
573f7950bf1SMatthew Ahrens 		VERIFY(spa_feature_enabled_txg(spa,
574f7950bf1SMatthew Ahrens 		    SPA_FEATURE_HOLE_BIRTH, &td.td_hole_birth_enabled_txg));
575f7950bf1SMatthew Ahrens 	} else {
576286ef713SPaul Dagnelie 		td.td_hole_birth_enabled_txg = UINT64_MAX;
577f7950bf1SMatthew Ahrens 	}
578f7950bf1SMatthew Ahrens 
57988b7b0f2SMatthew Ahrens 	pd.pd_flags = flags;
5809c3fd121SMatthew Ahrens 	if (resume != NULL)
5819c3fd121SMatthew Ahrens 		pd.pd_resume = *resume;
58288b7b0f2SMatthew Ahrens 	mutex_init(&pd.pd_mtx, NULL, MUTEX_DEFAULT, NULL);
58388b7b0f2SMatthew Ahrens 	cv_init(&pd.pd_cv, NULL, CV_DEFAULT, NULL);
58488b7b0f2SMatthew Ahrens 
5856e0cbcaaSMatthew Ahrens 	/* See comment on ZIL traversal in dsl_scan_visitds. */
586bc9014e6SJustin Gibbs 	if (ds != NULL && !ds->ds_is_snapshot && !BP_IS_HOLE(rootbp)) {
5877adb730bSGeorge Wilson 		arc_flags_t flags = ARC_FLAG_WAIT;
5883b2aab18SMatthew Ahrens 		objset_phys_t *osp;
5893b2aab18SMatthew Ahrens 		arc_buf_t *buf;
5906e0cbcaaSMatthew Ahrens 
5913b2aab18SMatthew Ahrens 		err = arc_read(NULL, td.td_spa, rootbp,
5923b2aab18SMatthew Ahrens 		    arc_getbuf_func, &buf,
5933b2aab18SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, NULL);
5943b2aab18SMatthew Ahrens 		if (err != 0)
5956e0cbcaaSMatthew Ahrens 			return (err);
5966e0cbcaaSMatthew Ahrens 
5973b2aab18SMatthew Ahrens 		osp = buf->b_data;
5983b2aab18SMatthew Ahrens 		traverse_zil(&td, &osp->os_zil_header);
599dcbf3bd6SGeorge Wilson 		arc_buf_destroy(buf, &buf);
6006e0cbcaaSMatthew Ahrens 	}
6016e0cbcaaSMatthew Ahrens 
602b4709335SMatthew Ahrens 	if (!(flags & TRAVERSE_PREFETCH_DATA) ||
60388b7b0f2SMatthew Ahrens 	    0 == taskq_dispatch(system_taskq, traverse_prefetch_thread,
60488b7b0f2SMatthew Ahrens 	    &td, TQ_NOQUEUE))
60588b7b0f2SMatthew Ahrens 		pd.pd_exited = B_TRUE;
60688b7b0f2SMatthew Ahrens 
6076e0cbcaaSMatthew Ahrens 	SET_BOOKMARK(&czb, td.td_objset,
608b24ab676SJeff Bonwick 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
6091b912ec7SGeorge Wilson 	err = traverse_visitbp(&td, NULL, rootbp, &czb);
61088b7b0f2SMatthew Ahrens 
61188b7b0f2SMatthew Ahrens 	mutex_enter(&pd.pd_mtx);
61288b7b0f2SMatthew Ahrens 	pd.pd_cancel = B_TRUE;
61388b7b0f2SMatthew Ahrens 	cv_broadcast(&pd.pd_cv);
61488b7b0f2SMatthew Ahrens 	while (!pd.pd_exited)
61588b7b0f2SMatthew Ahrens 		cv_wait(&pd.pd_cv, &pd.pd_mtx);
61688b7b0f2SMatthew Ahrens 	mutex_exit(&pd.pd_mtx);
61788b7b0f2SMatthew Ahrens 
61888b7b0f2SMatthew Ahrens 	mutex_destroy(&pd.pd_mtx);
61988b7b0f2SMatthew Ahrens 	cv_destroy(&pd.pd_cv);
620fa9e4066Sahrens 
62188b7b0f2SMatthew Ahrens 	return (err);
622fa9e4066Sahrens }
623fa9e4066Sahrens 
62488b7b0f2SMatthew Ahrens /*
62588b7b0f2SMatthew Ahrens  * NB: dataset must not be changing on-disk (eg, is a snapshot or we are
62688b7b0f2SMatthew Ahrens  * in syncing context).
62788b7b0f2SMatthew Ahrens  */
62888b7b0f2SMatthew Ahrens int
6299c3fd121SMatthew Ahrens traverse_dataset_resume(dsl_dataset_t *ds, uint64_t txg_start,
6309c3fd121SMatthew Ahrens     zbookmark_phys_t *resume,
6319c3fd121SMatthew Ahrens     int flags, blkptr_cb_t func, void *arg)
632fa9e4066Sahrens {
633ad135b5dSChristopher Siden 	return (traverse_impl(ds->ds_dir->dd_pool->dp_spa, ds, ds->ds_object,
6349c3fd121SMatthew Ahrens 	    &dsl_dataset_phys(ds)->ds_bp, txg_start, resume, flags, func, arg));
6359c3fd121SMatthew Ahrens }
6369c3fd121SMatthew Ahrens 
6379c3fd121SMatthew Ahrens int
6389c3fd121SMatthew Ahrens traverse_dataset(dsl_dataset_t *ds, uint64_t txg_start,
6399c3fd121SMatthew Ahrens     int flags, blkptr_cb_t func, void *arg)
6409c3fd121SMatthew Ahrens {
6419c3fd121SMatthew Ahrens 	return (traverse_dataset_resume(ds, txg_start, NULL, flags, func, arg));
642ad135b5dSChristopher Siden }
643ad135b5dSChristopher Siden 
644ad135b5dSChristopher Siden int
645ad135b5dSChristopher Siden traverse_dataset_destroyed(spa_t *spa, blkptr_t *blkptr,
6467802d7bfSMatthew Ahrens     uint64_t txg_start, zbookmark_phys_t *resume, int flags,
647ad135b5dSChristopher Siden     blkptr_cb_t func, void *arg)
648ad135b5dSChristopher Siden {
649ad135b5dSChristopher Siden 	return (traverse_impl(spa, NULL, ZB_DESTROYED_OBJSET,
650ad135b5dSChristopher Siden 	    blkptr, txg_start, resume, flags, func, arg));
651fa9e4066Sahrens }
652fa9e4066Sahrens 
65388b7b0f2SMatthew Ahrens /*
65488b7b0f2SMatthew Ahrens  * NB: pool must not be changing on-disk (eg, from zdb or sync context).
65588b7b0f2SMatthew Ahrens  */
65688b7b0f2SMatthew Ahrens int
657bbfd46c4SJeff Bonwick traverse_pool(spa_t *spa, uint64_t txg_start, int flags,
658bbfd46c4SJeff Bonwick     blkptr_cb_t func, void *arg)
659fa9e4066Sahrens {
6607fd05ac4SMatthew Ahrens 	int err;
66188b7b0f2SMatthew Ahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
66288b7b0f2SMatthew Ahrens 	objset_t *mos = dp->dp_meta_objset;
663cd088ea4SVictor Latushkin 	boolean_t hard = (flags & TRAVERSE_HARD);
66488b7b0f2SMatthew Ahrens 
66588b7b0f2SMatthew Ahrens 	/* visit the MOS */
666ad135b5dSChristopher Siden 	err = traverse_impl(spa, NULL, 0, spa_get_rootblkptr(spa),
667ad135b5dSChristopher Siden 	    txg_start, NULL, flags, func, arg);
6683b2aab18SMatthew Ahrens 	if (err != 0)
66988b7b0f2SMatthew Ahrens 		return (err);
67088b7b0f2SMatthew Ahrens 
67188b7b0f2SMatthew Ahrens 	/* visit each dataset */
6729c3fd121SMatthew Ahrens 	for (uint64_t obj = 1; err == 0;
6739c3fd121SMatthew Ahrens 	    err = dmu_object_next(mos, &obj, B_FALSE, txg_start)) {
67488b7b0f2SMatthew Ahrens 		dmu_object_info_t doi;
67588b7b0f2SMatthew Ahrens 
67688b7b0f2SMatthew Ahrens 		err = dmu_object_info(mos, obj, &doi);
6773b2aab18SMatthew Ahrens 		if (err != 0) {
6787fd05ac4SMatthew Ahrens 			if (hard)
6797fd05ac4SMatthew Ahrens 				continue;
6807fd05ac4SMatthew Ahrens 			break;
681cd088ea4SVictor Latushkin 		}
68288b7b0f2SMatthew Ahrens 
6832acef22dSMatthew Ahrens 		if (doi.doi_bonus_type == DMU_OT_DSL_DATASET) {
68488b7b0f2SMatthew Ahrens 			dsl_dataset_t *ds;
685468c413aSTim Haley 			uint64_t txg = txg_start;
686468c413aSTim Haley 
6873b2aab18SMatthew Ahrens 			dsl_pool_config_enter(dp, FTAG);
68888b7b0f2SMatthew Ahrens 			err = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
6893b2aab18SMatthew Ahrens 			dsl_pool_config_exit(dp, FTAG);
6903b2aab18SMatthew Ahrens 			if (err != 0) {
6917fd05ac4SMatthew Ahrens 				if (hard)
6927fd05ac4SMatthew Ahrens 					continue;
6937fd05ac4SMatthew Ahrens 				break;
694cd088ea4SVictor Latushkin 			}
695c1379625SJustin T. Gibbs 			if (dsl_dataset_phys(ds)->ds_prev_snap_txg > txg)
696c1379625SJustin T. Gibbs 				txg = dsl_dataset_phys(ds)->ds_prev_snap_txg;
697bbfd46c4SJeff Bonwick 			err = traverse_dataset(ds, txg, flags, func, arg);
69888b7b0f2SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
6997fd05ac4SMatthew Ahrens 			if (err != 0)
7007fd05ac4SMatthew Ahrens 				break;
70188b7b0f2SMatthew Ahrens 		}
702fa9e4066Sahrens 	}
70388b7b0f2SMatthew Ahrens 	if (err == ESRCH)
70488b7b0f2SMatthew Ahrens 		err = 0;
7057fd05ac4SMatthew Ahrens 	return (err);
706fa9e4066Sahrens }
707