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.
2386714001SSerapheim Dimitropoulos  * Copyright (c) 2012, 2018 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>
3486714001SSerapheim Dimitropoulos #include <sys/spa_impl.h>
35fa9e4066Sahrens #include <sys/zio.h>
36fa9e4066Sahrens #include <sys/dmu_impl.h>
370a586ceaSMark Shellenbaum #include <sys/sa.h>
380a586ceaSMark Shellenbaum #include <sys/sa_impl.h>
3988b7b0f2SMatthew Ahrens #include <sys/callb.h>
4043466aaeSMax Grossman #include <sys/zfeature.h>
4188b7b0f2SMatthew Ahrens 
4234d7ce05SGeorge Wilson int32_t zfs_pd_bytes_max = 50 * 1024 * 1024;	/* 50MB */
43f76886deSPaul Dagnelie boolean_t send_holes_without_birth_time = B_TRUE;
4444f92b7dSChris Kirby 
456e0cbcaaSMatthew Ahrens typedef struct prefetch_data {
4688b7b0f2SMatthew Ahrens 	kmutex_t pd_mtx;
4788b7b0f2SMatthew Ahrens 	kcondvar_t pd_cv;
4834d7ce05SGeorge Wilson 	int32_t pd_bytes_fetched;
4988b7b0f2SMatthew Ahrens 	int pd_flags;
5088b7b0f2SMatthew Ahrens 	boolean_t pd_cancel;
5188b7b0f2SMatthew Ahrens 	boolean_t pd_exited;
529c3fd121SMatthew Ahrens 	zbookmark_phys_t pd_resume;
536e0cbcaaSMatthew Ahrens } prefetch_data_t;
5488b7b0f2SMatthew Ahrens 
556e0cbcaaSMatthew Ahrens typedef struct traverse_data {
5688b7b0f2SMatthew Ahrens 	spa_t *td_spa;
5788b7b0f2SMatthew Ahrens 	uint64_t td_objset;
5888b7b0f2SMatthew Ahrens 	blkptr_t *td_rootbp;
5988b7b0f2SMatthew Ahrens 	uint64_t td_min_txg;
607802d7bfSMatthew Ahrens 	zbookmark_phys_t *td_resume;
6188b7b0f2SMatthew Ahrens 	int td_flags;
626e0cbcaaSMatthew Ahrens 	prefetch_data_t *td_pfd;
637fd05ac4SMatthew Ahrens 	boolean_t td_paused;
64f7950bf1SMatthew Ahrens 	uint64_t td_hole_birth_enabled_txg;
6588b7b0f2SMatthew Ahrens 	blkptr_cb_t *td_func;
6688b7b0f2SMatthew Ahrens 	void *td_arg;
67286ef713SPaul Dagnelie 	boolean_t td_realloc_possible;
686e0cbcaaSMatthew Ahrens } traverse_data_t;
69fa9e4066Sahrens 
706e0cbcaaSMatthew Ahrens static int traverse_dnode(traverse_data_t *td, const dnode_phys_t *dnp,
711b912ec7SGeorge Wilson     uint64_t objset, uint64_t object);
72b4709335SMatthew Ahrens static void prefetch_dnode_metadata(traverse_data_t *td, const dnode_phys_t *,
731b912ec7SGeorge Wilson     uint64_t objset, uint64_t object);
7414843421SMatthew Ahrens 
75b24ab676SJeff Bonwick static int
traverse_zil_block(zilog_t * zilog,blkptr_t * bp,void * arg,uint64_t claim_txg)765dabedeeSbonwick traverse_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
77ea8dc4b6Seschrock {
786e0cbcaaSMatthew Ahrens 	traverse_data_t *td = arg;
797802d7bfSMatthew Ahrens 	zbookmark_phys_t zb;
80ea8dc4b6Seschrock 
8143466aaeSMax Grossman 	if (BP_IS_HOLE(bp))
82b24ab676SJeff Bonwick 		return (0);
835dabedeeSbonwick 
8486714001SSerapheim Dimitropoulos 	if (claim_txg == 0 && bp->blk_birth >= spa_min_claim_txg(td->td_spa))
8586714001SSerapheim Dimitropoulos 		return (-1);
8688b7b0f2SMatthew Ahrens 
87b24ab676SJeff Bonwick 	SET_BOOKMARK(&zb, td->td_objset, ZB_ZIL_OBJECT, ZB_ZIL_LEVEL,
88b24ab676SJeff Bonwick 	    bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
89b24ab676SJeff Bonwick 
901b912ec7SGeorge Wilson 	(void) td->td_func(td->td_spa, zilog, bp, &zb, NULL, td->td_arg);
91b24ab676SJeff Bonwick 
92b24ab676SJeff Bonwick 	return (0);
93ea8dc4b6Seschrock }
94ea8dc4b6Seschrock 
95b24ab676SJeff Bonwick static int
traverse_zil_record(zilog_t * zilog,lr_t * lrc,void * arg,uint64_t claim_txg)965dabedeeSbonwick traverse_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg)
97ea8dc4b6Seschrock {
986e0cbcaaSMatthew Ahrens 	traverse_data_t *td = arg;
99ea8dc4b6Seschrock 
100ea8dc4b6Seschrock 	if (lrc->lrc_txtype == TX_WRITE) {
101ea8dc4b6Seschrock 		lr_write_t *lr = (lr_write_t *)lrc;
102ea8dc4b6Seschrock 		blkptr_t *bp = &lr->lr_blkptr;
1037802d7bfSMatthew Ahrens 		zbookmark_phys_t zb;
104ea8dc4b6Seschrock 
10543466aaeSMax Grossman 		if (BP_IS_HOLE(bp))
106b24ab676SJeff Bonwick 			return (0);
1075dabedeeSbonwick 
10888b7b0f2SMatthew Ahrens 		if (claim_txg == 0 || bp->blk_birth < claim_txg)
109b24ab676SJeff Bonwick 			return (0);
110b24ab676SJeff Bonwick 
1116e0cbcaaSMatthew Ahrens 		SET_BOOKMARK(&zb, td->td_objset, lr->lr_foid,
1126e0cbcaaSMatthew Ahrens 		    ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp));
11388b7b0f2SMatthew Ahrens 
1141b912ec7SGeorge Wilson 		(void) td->td_func(td->td_spa, zilog, bp, &zb, NULL,
115b24ab676SJeff Bonwick 		    td->td_arg);
116ea8dc4b6Seschrock 	}
117b24ab676SJeff Bonwick 	return (0);
118ea8dc4b6Seschrock }
119ea8dc4b6Seschrock 
120ea8dc4b6Seschrock static void
traverse_zil(traverse_data_t * td,zil_header_t * zh)1216e0cbcaaSMatthew Ahrens traverse_zil(traverse_data_t *td, zil_header_t *zh)
122ea8dc4b6Seschrock {
1235dabedeeSbonwick 	uint64_t claim_txg = zh->zh_claim_txg;
124ea8dc4b6Seschrock 
1255dabedeeSbonwick 	/*
1265dabedeeSbonwick 	 * We only want to visit blocks that have been claimed but not yet
12786714001SSerapheim Dimitropoulos 	 * replayed; plus blocks that are already stable in read-only mode.
1285dabedeeSbonwick 	 */
1298ad4d6ddSJeff Bonwick 	if (claim_txg == 0 && spa_writeable(td->td_spa))
1305dabedeeSbonwick 		return;
1315dabedeeSbonwick 
13286714001SSerapheim Dimitropoulos 	zilog_t *zilog = zil_alloc(spa_get_dsl(td->td_spa)->dp_meta_objset, zh);
13388b7b0f2SMatthew Ahrens 	(void) zil_parse(zilog, traverse_zil_block, traverse_zil_record, td,
134eb633035STom Caputi 	    claim_txg, !(td->td_flags & TRAVERSE_NO_DECRYPT));
135ea8dc4b6Seschrock 	zil_free(zilog);
136ea8dc4b6Seschrock }
137ea8dc4b6Seschrock 
138ad135b5dSChristopher Siden typedef enum resume_skip {
139ad135b5dSChristopher Siden 	RESUME_SKIP_ALL,
140ad135b5dSChristopher Siden 	RESUME_SKIP_NONE,
141ad135b5dSChristopher Siden 	RESUME_SKIP_CHILDREN
142ad135b5dSChristopher Siden } resume_skip_t;
143ad135b5dSChristopher Siden 
144ad135b5dSChristopher Siden /*
145ad135b5dSChristopher Siden  * Returns RESUME_SKIP_ALL if td indicates that we are resuming a traversal and
146ad135b5dSChristopher Siden  * the block indicated by zb does not need to be visited at all. Returns
147ad135b5dSChristopher Siden  * RESUME_SKIP_CHILDREN if we are resuming a post traversal and we reach the
148ad135b5dSChristopher Siden  * resume point. This indicates that this block should be visited but not its
149ad135b5dSChristopher Siden  * children (since they must have been visited in a previous traversal).
150ad135b5dSChristopher Siden  * Otherwise returns RESUME_SKIP_NONE.
151ad135b5dSChristopher Siden  */
152ad135b5dSChristopher Siden static resume_skip_t
resume_skip_check(traverse_data_t * td,const dnode_phys_t * dnp,const zbookmark_phys_t * zb)153ad135b5dSChristopher Siden resume_skip_check(traverse_data_t *td, const dnode_phys_t *dnp,
1547802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb)
155ad135b5dSChristopher Siden {
156ad135b5dSChristopher Siden 	if (td->td_resume != NULL && !ZB_IS_ZERO(td->td_resume)) {
157ad135b5dSChristopher Siden 		/*
158ad135b5dSChristopher Siden 		 * If we already visited this bp & everything below,
159ad135b5dSChristopher Siden 		 * don't bother doing it again.
160ad135b5dSChristopher Siden 		 */
161a2cdcdd2SPaul Dagnelie 		if (zbookmark_subtree_completed(dnp, zb, td->td_resume))
162ad135b5dSChristopher Siden 			return (RESUME_SKIP_ALL);
163ad135b5dSChristopher Siden 
164ad135b5dSChristopher Siden 		/*
165ad135b5dSChristopher Siden 		 * If we found the block we're trying to resume from, zero
166ad135b5dSChristopher Siden 		 * the bookmark out to indicate that we have resumed.
167ad135b5dSChristopher Siden 		 */
168ad135b5dSChristopher Siden 		if (bcmp(zb, td->td_resume, sizeof (*zb)) == 0) {
169ad135b5dSChristopher Siden 			bzero(td->td_resume, sizeof (*zb));
170ad135b5dSChristopher Siden 			if (td->td_flags & TRAVERSE_POST)
171ad135b5dSChristopher Siden 				return (RESUME_SKIP_CHILDREN);
172ad135b5dSChristopher Siden 		}
173ad135b5dSChristopher Siden 	}
174ad135b5dSChristopher Siden 	return (RESUME_SKIP_NONE);
175ad135b5dSChristopher Siden }
176ad135b5dSChristopher Siden 
177b4709335SMatthew Ahrens static void
traverse_prefetch_metadata(traverse_data_t * td,const blkptr_t * bp,const zbookmark_phys_t * zb)178b4709335SMatthew Ahrens traverse_prefetch_metadata(traverse_data_t *td,
1797802d7bfSMatthew Ahrens     const blkptr_t *bp, const zbookmark_phys_t *zb)
180b4709335SMatthew Ahrens {
1817adb730bSGeorge Wilson 	arc_flags_t flags = ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH;
182eb633035STom Caputi 	int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE;
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 
198eb633035STom Caputi 	if ((td->td_flags & TRAVERSE_NO_DECRYPT) && BP_IS_PROTECTED(bp))
199eb633035STom Caputi 		zio_flags |= ZIO_FLAG_RAW;
200eb633035STom Caputi 
2011b912ec7SGeorge Wilson 	(void) arc_read(NULL, td->td_spa, bp, NULL, NULL,
202eb633035STom Caputi 	    ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
203b4709335SMatthew Ahrens }
204b4709335SMatthew Ahrens 
20506315b79SMatthew Ahrens static boolean_t
prefetch_needed(prefetch_data_t * pfd,const blkptr_t * bp)20606315b79SMatthew Ahrens prefetch_needed(prefetch_data_t *pfd, const blkptr_t *bp)
20706315b79SMatthew Ahrens {
20806315b79SMatthew Ahrens 	ASSERT(pfd->pd_flags & TRAVERSE_PREFETCH_DATA);
20906315b79SMatthew Ahrens 	if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp) ||
21006315b79SMatthew Ahrens 	    BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG)
21106315b79SMatthew Ahrens 		return (B_FALSE);
21206315b79SMatthew Ahrens 	return (B_TRUE);
21306315b79SMatthew Ahrens }
21406315b79SMatthew Ahrens 
215fa9e4066Sahrens static int
traverse_visitbp(traverse_data_t * td,const dnode_phys_t * dnp,const blkptr_t * bp,const zbookmark_phys_t * zb)2166e0cbcaaSMatthew Ahrens traverse_visitbp(traverse_data_t *td, const dnode_phys_t *dnp,
2177802d7bfSMatthew Ahrens     const blkptr_t *bp, const zbookmark_phys_t *zb)
218fa9e4066Sahrens {
2197802d7bfSMatthew Ahrens 	zbookmark_phys_t czb;
2207fd05ac4SMatthew Ahrens 	int err = 0;
22188b7b0f2SMatthew Ahrens 	arc_buf_t *buf = NULL;
2226e0cbcaaSMatthew Ahrens 	prefetch_data_t *pd = td->td_pfd;
223cd088ea4SVictor Latushkin 	boolean_t hard = td->td_flags & TRAVERSE_HARD;
224ad135b5dSChristopher Siden 
225ad135b5dSChristopher Siden 	switch (resume_skip_check(td, dnp, zb)) {
226ad135b5dSChristopher Siden 	case RESUME_SKIP_ALL:
227ad135b5dSChristopher Siden 		return (0);
228ad135b5dSChristopher Siden 	case RESUME_SKIP_CHILDREN:
229ad135b5dSChristopher Siden 		goto post;
230ad135b5dSChristopher Siden 	case RESUME_SKIP_NONE:
231ad135b5dSChristopher Siden 		break;
232ad135b5dSChristopher Siden 	default:
233ad135b5dSChristopher Siden 		ASSERT(0);
234ad135b5dSChristopher Siden 	}
235fa9e4066Sahrens 
23643466aaeSMax Grossman 	if (bp->blk_birth == 0) {
237f7950bf1SMatthew Ahrens 		/*
238286ef713SPaul Dagnelie 		 * Since this block has a birth time of 0 it must be one of
239286ef713SPaul Dagnelie 		 * two things: a hole created before the
240286ef713SPaul Dagnelie 		 * SPA_FEATURE_HOLE_BIRTH feature was enabled, or a hole
241286ef713SPaul Dagnelie 		 * which has always been a hole in an object.
242286ef713SPaul Dagnelie 		 *
243286ef713SPaul Dagnelie 		 * If a file is written sparsely, then the unwritten parts of
244286ef713SPaul Dagnelie 		 * the file were "always holes" -- that is, they have been
245286ef713SPaul Dagnelie 		 * holes since this object was allocated.  However, we (and
246286ef713SPaul Dagnelie 		 * our callers) can not necessarily tell when an object was
247286ef713SPaul Dagnelie 		 * allocated.  Therefore, if it's possible that this object
248286ef713SPaul Dagnelie 		 * was freed and then its object number reused, we need to
249286ef713SPaul Dagnelie 		 * visit all the holes with birth==0.
250286ef713SPaul Dagnelie 		 *
251286ef713SPaul Dagnelie 		 * If it isn't possible that the object number was reused,
252286ef713SPaul Dagnelie 		 * then if SPA_FEATURE_HOLE_BIRTH was enabled before we wrote
253286ef713SPaul Dagnelie 		 * all the blocks we will visit as part of this traversal,
254286ef713SPaul Dagnelie 		 * then this hole must have always existed, so we can skip
255286ef713SPaul Dagnelie 		 * it.  We visit blocks born after (exclusive) td_min_txg.
256286ef713SPaul Dagnelie 		 *
257286ef713SPaul Dagnelie 		 * Note that the meta-dnode cannot be reallocated.
258f7950bf1SMatthew Ahrens 		 */
259f76886deSPaul Dagnelie 		if (!send_holes_without_birth_time &&
260f76886deSPaul Dagnelie 		    (!td->td_realloc_possible ||
261286ef713SPaul Dagnelie 		    zb->zb_object == DMU_META_DNODE_OBJECT) &&
262286ef713SPaul Dagnelie 		    td->td_hole_birth_enabled_txg <= td->td_min_txg)
263f7950bf1SMatthew Ahrens 			return (0);
26443466aaeSMax Grossman 	} else if (bp->blk_birth <= td->td_min_txg) {
26543466aaeSMax Grossman 		return (0);
26643466aaeSMax Grossman 	}
26743466aaeSMax Grossman 
26806315b79SMatthew Ahrens 	if (pd != NULL && !pd->pd_exited && prefetch_needed(pd, bp)) {
26934d7ce05SGeorge Wilson 		uint64_t size = BP_GET_LSIZE(bp);
27088b7b0f2SMatthew Ahrens 		mutex_enter(&pd->pd_mtx);
27134d7ce05SGeorge Wilson 		ASSERT(pd->pd_bytes_fetched >= 0);
27234d7ce05SGeorge Wilson 		while (pd->pd_bytes_fetched < size && !pd->pd_exited)
27388b7b0f2SMatthew Ahrens 			cv_wait(&pd->pd_cv, &pd->pd_mtx);
27434d7ce05SGeorge Wilson 		pd->pd_bytes_fetched -= size;
27588b7b0f2SMatthew Ahrens 		cv_broadcast(&pd->pd_cv);
27688b7b0f2SMatthew Ahrens 		mutex_exit(&pd->pd_mtx);
277fa9e4066Sahrens 	}
278fa9e4066Sahrens 
27906315b79SMatthew Ahrens 	if (BP_IS_HOLE(bp)) {
28006315b79SMatthew Ahrens 		err = td->td_func(td->td_spa, NULL, bp, zb, dnp, td->td_arg);
28106315b79SMatthew Ahrens 		if (err != 0)
28206315b79SMatthew Ahrens 			goto post;
28306315b79SMatthew Ahrens 		return (0);
28406315b79SMatthew Ahrens 	}
28506315b79SMatthew Ahrens 
28688b7b0f2SMatthew Ahrens 	if (td->td_flags & TRAVERSE_PRE) {
2871b912ec7SGeorge Wilson 		err = td->td_func(td->td_spa, NULL, bp, zb, dnp,
2883f9d6ad7SLin Ling 		    td->td_arg);
28999d5e173STim Haley 		if (err == TRAVERSE_VISIT_NO_CHILDREN)
29099d5e173STim Haley 			return (0);
291ad135b5dSChristopher Siden 		if (err != 0)
292ad135b5dSChristopher Siden 			goto post;
293fa9e4066Sahrens 	}
294fa9e4066Sahrens 
29588b7b0f2SMatthew Ahrens 	if (BP_GET_LEVEL(bp) > 0) {
2967adb730bSGeorge Wilson 		arc_flags_t flags = ARC_FLAG_WAIT;
29788b7b0f2SMatthew Ahrens 		int i;
29888b7b0f2SMatthew Ahrens 		blkptr_t *cbp;
29988b7b0f2SMatthew Ahrens 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
30088b7b0f2SMatthew Ahrens 
301eb633035STom Caputi 		ASSERT(!BP_IS_PROTECTED(bp));
302eb633035STom Caputi 
3031b912ec7SGeorge Wilson 		err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf,
30488b7b0f2SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
3053b2aab18SMatthew Ahrens 		if (err != 0)
3067fd05ac4SMatthew Ahrens 			goto post;
307b4709335SMatthew Ahrens 		cbp = buf->b_data;
308b4709335SMatthew Ahrens 
309b4709335SMatthew Ahrens 		for (i = 0; i < epb; i++) {
310b4709335SMatthew Ahrens 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
311b4709335SMatthew Ahrens 			    zb->zb_level - 1,
312b4709335SMatthew Ahrens 			    zb->zb_blkid * epb + i);
3131b912ec7SGeorge Wilson 			traverse_prefetch_metadata(td, &cbp[i], &czb);
314b4709335SMatthew Ahrens 		}
31588b7b0f2SMatthew Ahrens 
31688b7b0f2SMatthew Ahrens 		/* recursively visitbp() blocks below this */
317b4709335SMatthew Ahrens 		for (i = 0; i < epb; i++) {
31888b7b0f2SMatthew Ahrens 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
31988b7b0f2SMatthew Ahrens 			    zb->zb_level - 1,
32088b7b0f2SMatthew Ahrens 			    zb->zb_blkid * epb + i);
3211b912ec7SGeorge Wilson 			err = traverse_visitbp(td, dnp, &cbp[i], &czb);
3227fd05ac4SMatthew Ahrens 			if (err != 0)
3237fd05ac4SMatthew Ahrens 				break;
32488b7b0f2SMatthew Ahrens 		}
32588b7b0f2SMatthew Ahrens 	} else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) {
3267adb730bSGeorge Wilson 		arc_flags_t flags = ARC_FLAG_WAIT;
327eb633035STom Caputi 		uint32_t zio_flags = ZIO_FLAG_CANFAIL;
32814843421SMatthew Ahrens 		int i;
32988b7b0f2SMatthew Ahrens 		int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
33088b7b0f2SMatthew Ahrens 
331eb633035STom Caputi 		/*
332eb633035STom Caputi 		 * dnode blocks might have their bonus buffers encrypted, so
333eb633035STom Caputi 		 * we must be careful to honor TRAVERSE_NO_DECRYPT
334eb633035STom Caputi 		 */
335eb633035STom Caputi 		if ((td->td_flags & TRAVERSE_NO_DECRYPT) && BP_IS_PROTECTED(bp))
336eb633035STom Caputi 			zio_flags |= ZIO_FLAG_RAW;
3371b912ec7SGeorge Wilson 		err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf,
338eb633035STom Caputi 		    ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
3393b2aab18SMatthew Ahrens 		if (err != 0)
3407fd05ac4SMatthew Ahrens 			goto post;
3419c3fd121SMatthew Ahrens 		dnode_phys_t *child_dnp = buf->b_data;
342b4709335SMatthew Ahrens 
34354811da5SToomas Soome 		for (i = 0; i < epb; i += child_dnp[i].dn_extra_slots + 1) {
3449c3fd121SMatthew Ahrens 			prefetch_dnode_metadata(td, &child_dnp[i],
3459c3fd121SMatthew Ahrens 			    zb->zb_objset, zb->zb_blkid * epb + i);
346b4709335SMatthew Ahrens 		}
34788b7b0f2SMatthew Ahrens 
34888b7b0f2SMatthew Ahrens 		/* recursively visitbp() blocks below this */
34954811da5SToomas Soome 		for (i = 0; i < epb; i += child_dnp[i].dn_extra_slots + 1) {
3509c3fd121SMatthew Ahrens 			err = traverse_dnode(td, &child_dnp[i],
3519c3fd121SMatthew Ahrens 			    zb->zb_objset, zb->zb_blkid * epb + i);
3527fd05ac4SMatthew Ahrens 			if (err != 0)
3537fd05ac4SMatthew Ahrens 				break;
354fa9e4066Sahrens 		}
35588b7b0f2SMatthew Ahrens 	} else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
356eb633035STom Caputi 		uint32_t zio_flags = ZIO_FLAG_CANFAIL;
3577adb730bSGeorge Wilson 		arc_flags_t flags = ARC_FLAG_WAIT;
35888b7b0f2SMatthew Ahrens 
359eb633035STom Caputi 		if ((td->td_flags & TRAVERSE_NO_DECRYPT) && BP_IS_PROTECTED(bp))
360eb633035STom Caputi 			zio_flags |= ZIO_FLAG_RAW;
361eb633035STom Caputi 
3621b912ec7SGeorge Wilson 		err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf,
363eb633035STom Caputi 		    ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
3643b2aab18SMatthew Ahrens 		if (err != 0)
3657fd05ac4SMatthew Ahrens 			goto post;
36688b7b0f2SMatthew Ahrens 
3679c3fd121SMatthew Ahrens 		objset_phys_t *osp = buf->b_data;
3689c3fd121SMatthew Ahrens 		prefetch_dnode_metadata(td, &osp->os_meta_dnode, zb->zb_objset,
369b4709335SMatthew Ahrens 		    DMU_META_DNODE_OBJECT);
370286ef713SPaul Dagnelie 		/*
371286ef713SPaul Dagnelie 		 * See the block comment above for the goal of this variable.
372286ef713SPaul Dagnelie 		 * If the maxblkid of the meta-dnode is 0, then we know that
373286ef713SPaul Dagnelie 		 * we've never had more than DNODES_PER_BLOCK objects in the
374286ef713SPaul Dagnelie 		 * dataset, which means we can't have reused any object ids.
375286ef713SPaul Dagnelie 		 */
376286ef713SPaul Dagnelie 		if (osp->os_meta_dnode.dn_maxblkid == 0)
377286ef713SPaul Dagnelie 			td->td_realloc_possible = B_FALSE;
378286ef713SPaul Dagnelie 
379*f67950b2SNasf-Fan 		if (OBJSET_BUF_HAS_USERUSED(buf)) {
380*f67950b2SNasf-Fan 			if (OBJSET_BUF_HAS_PROJECTUSED(buf))
381*f67950b2SNasf-Fan 				prefetch_dnode_metadata(td,
382*f67950b2SNasf-Fan 				    &osp->os_projectused_dnode,
383*f67950b2SNasf-Fan 				    zb->zb_objset, DMU_PROJECTUSED_OBJECT);
384b4709335SMatthew Ahrens 			prefetch_dnode_metadata(td, &osp->os_groupused_dnode,
38548f1b90eSMatthew Ahrens 			    zb->zb_objset, DMU_GROUPUSED_OBJECT);
38648f1b90eSMatthew Ahrens 			prefetch_dnode_metadata(td, &osp->os_userused_dnode,
3871b912ec7SGeorge Wilson 			    zb->zb_objset, DMU_USERUSED_OBJECT);
388b4709335SMatthew Ahrens 		}
389b4709335SMatthew Ahrens 
3909c3fd121SMatthew Ahrens 		err = traverse_dnode(td, &osp->os_meta_dnode, zb->zb_objset,
391b24ab676SJeff Bonwick 		    DMU_META_DNODE_OBJECT);
392*f67950b2SNasf-Fan 		if (err == 0 && OBJSET_BUF_HAS_USERUSED(buf)) {
393*f67950b2SNasf-Fan 			if (OBJSET_BUF_HAS_PROJECTUSED(buf))
394*f67950b2SNasf-Fan 				err = traverse_dnode(td,
395*f67950b2SNasf-Fan 				    &osp->os_projectused_dnode, zb->zb_objset,
396*f67950b2SNasf-Fan 				    DMU_PROJECTUSED_OBJECT);
397*f67950b2SNasf-Fan 			if (err == 0)
398*f67950b2SNasf-Fan 				err = traverse_dnode(td,
399*f67950b2SNasf-Fan 				    &osp->os_groupused_dnode, zb->zb_objset,
400*f67950b2SNasf-Fan 				    DMU_GROUPUSED_OBJECT);
401*f67950b2SNasf-Fan 			if (err == 0)
402*f67950b2SNasf-Fan 				err = traverse_dnode(td,
403*f67950b2SNasf-Fan 				    &osp->os_userused_dnode, zb->zb_objset,
404*f67950b2SNasf-Fan 				    DMU_USERUSED_OBJECT);
40588b7b0f2SMatthew Ahrens 		}
40688b7b0f2SMatthew Ahrens 	}
407fa9e4066Sahrens 
40888b7b0f2SMatthew Ahrens 	if (buf)
409dcbf3bd6SGeorge Wilson 		arc_buf_destroy(buf, &buf);
410fa9e4066Sahrens 
411ad135b5dSChristopher Siden post:
4127fd05ac4SMatthew Ahrens 	if (err == 0 && (td->td_flags & TRAVERSE_POST))
4131b912ec7SGeorge Wilson 		err = td->td_func(td->td_spa, NULL, bp, zb, dnp, td->td_arg);
4147fd05ac4SMatthew Ahrens 
4157fd05ac4SMatthew Ahrens 	if (hard && (err == EIO || err == ECKSUM)) {
4167fd05ac4SMatthew Ahrens 		/*
4177fd05ac4SMatthew Ahrens 		 * Ignore this disk error as requested by the HARD flag,
4187fd05ac4SMatthew Ahrens 		 * and continue traversal.
4197fd05ac4SMatthew Ahrens 		 */
4207fd05ac4SMatthew Ahrens 		err = 0;
421ad135b5dSChristopher Siden 	}
422ad135b5dSChristopher Siden 
4237fd05ac4SMatthew Ahrens 	/*
4247fd05ac4SMatthew Ahrens 	 * If we are stopping here, set td_resume.
4257fd05ac4SMatthew Ahrens 	 */
4267fd05ac4SMatthew Ahrens 	if (td->td_resume != NULL && err != 0 && !td->td_paused) {
4277fd05ac4SMatthew Ahrens 		td->td_resume->zb_objset = zb->zb_objset;
4287fd05ac4SMatthew Ahrens 		td->td_resume->zb_object = zb->zb_object;
4297fd05ac4SMatthew Ahrens 		td->td_resume->zb_level = 0;
4307fd05ac4SMatthew Ahrens 		/*
4317fd05ac4SMatthew Ahrens 		 * If we have stopped on an indirect block (e.g. due to
4327fd05ac4SMatthew Ahrens 		 * i/o error), we have not visited anything below it.
4337fd05ac4SMatthew Ahrens 		 * Set the bookmark to the first level-0 block that we need
4347fd05ac4SMatthew Ahrens 		 * to visit.  This way, the resuming code does not need to
4357fd05ac4SMatthew Ahrens 		 * deal with resuming from indirect blocks.
4369c3fd121SMatthew Ahrens 		 *
4379c3fd121SMatthew Ahrens 		 * Note, if zb_level <= 0, dnp may be NULL, so we don't want
4389c3fd121SMatthew Ahrens 		 * to dereference it.
4397fd05ac4SMatthew Ahrens 		 */
4409c3fd121SMatthew Ahrens 		td->td_resume->zb_blkid = zb->zb_blkid;
4419c3fd121SMatthew Ahrens 		if (zb->zb_level > 0) {
4429c3fd121SMatthew Ahrens 			td->td_resume->zb_blkid <<= zb->zb_level *
4439c3fd121SMatthew Ahrens 			    (dnp->dn_indblkshift - SPA_BLKPTRSHIFT);
4449c3fd121SMatthew Ahrens 		}
4457fd05ac4SMatthew Ahrens 		td->td_paused = B_TRUE;
4463f9d6ad7SLin Ling 	}
447fa9e4066Sahrens 
4487fd05ac4SMatthew Ahrens 	return (err);
449fa9e4066Sahrens }
450fa9e4066Sahrens 
451b4709335SMatthew Ahrens static void
prefetch_dnode_metadata(traverse_data_t * td,const dnode_phys_t * dnp,uint64_t objset,uint64_t object)452b4709335SMatthew Ahrens prefetch_dnode_metadata(traverse_data_t *td, const dnode_phys_t *dnp,
4531b912ec7SGeorge Wilson     uint64_t objset, uint64_t object)
454b4709335SMatthew Ahrens {
455b4709335SMatthew Ahrens 	int j;
4567802d7bfSMatthew Ahrens 	zbookmark_phys_t czb;
457b4709335SMatthew Ahrens 
458b4709335SMatthew Ahrens 	for (j = 0; j < dnp->dn_nblkptr; j++) {
459b4709335SMatthew Ahrens 		SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j);
4601b912ec7SGeorge Wilson 		traverse_prefetch_metadata(td, &dnp->dn_blkptr[j], &czb);
461b4709335SMatthew Ahrens 	}
462b4709335SMatthew Ahrens 
463b4709335SMatthew Ahrens 	if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
464b4709335SMatthew Ahrens 		SET_BOOKMARK(&czb, objset, object, 0, DMU_SPILL_BLKID);
46554811da5SToomas Soome 		traverse_prefetch_metadata(td, DN_SPILL_BLKPTR(dnp), &czb);
466b4709335SMatthew Ahrens 	}
467b4709335SMatthew Ahrens }
468b4709335SMatthew Ahrens 
46914843421SMatthew Ahrens static int
traverse_dnode(traverse_data_t * td,const dnode_phys_t * dnp,uint64_t objset,uint64_t object)4706e0cbcaaSMatthew Ahrens traverse_dnode(traverse_data_t *td, const dnode_phys_t *dnp,
4711b912ec7SGeorge Wilson     uint64_t objset, uint64_t object)
47214843421SMatthew Ahrens {
4737fd05ac4SMatthew Ahrens 	int j, err = 0;
4747802d7bfSMatthew Ahrens 	zbookmark_phys_t czb;
47514843421SMatthew Ahrens 
4769c3fd121SMatthew Ahrens 	if (object != DMU_META_DNODE_OBJECT && td->td_resume != NULL &&
4779c3fd121SMatthew Ahrens 	    object < td->td_resume->zb_object)
4789c3fd121SMatthew Ahrens 		return (0);
4799c3fd121SMatthew Ahrens 
480a2cdcdd2SPaul Dagnelie 	if (td->td_flags & TRAVERSE_PRE) {
481a2cdcdd2SPaul Dagnelie 		SET_BOOKMARK(&czb, objset, object, ZB_DNODE_LEVEL,
482a2cdcdd2SPaul Dagnelie 		    ZB_DNODE_BLKID);
483a2cdcdd2SPaul Dagnelie 		err = td->td_func(td->td_spa, NULL, NULL, &czb, dnp,
484a2cdcdd2SPaul Dagnelie 		    td->td_arg);
485a2cdcdd2SPaul Dagnelie 		if (err == TRAVERSE_VISIT_NO_CHILDREN)
486a2cdcdd2SPaul Dagnelie 			return (0);
487a2cdcdd2SPaul Dagnelie 		if (err != 0)
488a2cdcdd2SPaul Dagnelie 			return (err);
489a2cdcdd2SPaul Dagnelie 	}
490a2cdcdd2SPaul Dagnelie 
49114843421SMatthew Ahrens 	for (j = 0; j < dnp->dn_nblkptr; j++) {
49214843421SMatthew Ahrens 		SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j);
4931b912ec7SGeorge Wilson 		err = traverse_visitbp(td, dnp, &dnp->dn_blkptr[j], &czb);
4947fd05ac4SMatthew Ahrens 		if (err != 0)
4957fd05ac4SMatthew Ahrens 			break;
4963f9d6ad7SLin Ling 	}
4973f9d6ad7SLin Ling 
498a2cdcdd2SPaul Dagnelie 	if (err == 0 && (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) {
499b4709335SMatthew Ahrens 		SET_BOOKMARK(&czb, objset, object, 0, DMU_SPILL_BLKID);
50054811da5SToomas Soome 		err = traverse_visitbp(td, dnp, DN_SPILL_BLKPTR(dnp), &czb);
50114843421SMatthew Ahrens 	}
502a2cdcdd2SPaul Dagnelie 
503a2cdcdd2SPaul Dagnelie 	if (err == 0 && (td->td_flags & TRAVERSE_POST)) {
504a2cdcdd2SPaul Dagnelie 		SET_BOOKMARK(&czb, objset, object, ZB_DNODE_LEVEL,
505a2cdcdd2SPaul Dagnelie 		    ZB_DNODE_BLKID);
506a2cdcdd2SPaul Dagnelie 		err = td->td_func(td->td_spa, NULL, NULL, &czb, dnp,
507a2cdcdd2SPaul Dagnelie 		    td->td_arg);
508a2cdcdd2SPaul Dagnelie 		if (err == TRAVERSE_VISIT_NO_CHILDREN)
509a2cdcdd2SPaul Dagnelie 			return (0);
510a2cdcdd2SPaul Dagnelie 		if (err != 0)
511a2cdcdd2SPaul Dagnelie 			return (err);
512a2cdcdd2SPaul Dagnelie 	}
5137fd05ac4SMatthew Ahrens 	return (err);
51414843421SMatthew Ahrens }
51514843421SMatthew Ahrens 
51688b7b0f2SMatthew Ahrens /* ARGSUSED */
51788b7b0f2SMatthew Ahrens static int
traverse_prefetcher(spa_t * spa,zilog_t * zilog,const blkptr_t * bp,const zbookmark_phys_t * zb,const dnode_phys_t * dnp,void * arg)518b24ab676SJeff Bonwick traverse_prefetcher(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
5197802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
520e7cbe64fSgw {
5216e0cbcaaSMatthew Ahrens 	prefetch_data_t *pfd = arg;
522eb633035STom Caputi 	int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE;
523a3874b8bSToomas Soome 	arc_flags_t aflags = ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH |
524a3874b8bSToomas Soome 	    ARC_FLAG_PRESCIENT_PREFETCH;
525e7cbe64fSgw 
52634d7ce05SGeorge Wilson 	ASSERT(pfd->pd_bytes_fetched >= 0);
527a2cdcdd2SPaul Dagnelie 	if (bp == NULL)
528a2cdcdd2SPaul Dagnelie 		return (0);
52988b7b0f2SMatthew Ahrens 	if (pfd->pd_cancel)
530be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
531e7cbe64fSgw 
53206315b79SMatthew Ahrens 	if (!prefetch_needed(pfd, bp))
533fa9e4066Sahrens 		return (0);
534fa9e4066Sahrens 
53588b7b0f2SMatthew Ahrens 	mutex_enter(&pfd->pd_mtx);
53634d7ce05SGeorge Wilson 	while (!pfd->pd_cancel && pfd->pd_bytes_fetched >= zfs_pd_bytes_max)
53788b7b0f2SMatthew Ahrens 		cv_wait(&pfd->pd_cv, &pfd->pd_mtx);
53834d7ce05SGeorge Wilson 	pfd->pd_bytes_fetched += BP_GET_LSIZE(bp);
53988b7b0f2SMatthew Ahrens 	cv_broadcast(&pfd->pd_cv);
54088b7b0f2SMatthew Ahrens 	mutex_exit(&pfd->pd_mtx);
541fa9e4066Sahrens 
542eb633035STom Caputi 	if ((pfd->pd_flags & TRAVERSE_NO_DECRYPT) && BP_IS_PROTECTED(bp))
543eb633035STom Caputi 		zio_flags |= ZIO_FLAG_RAW;
544eb633035STom Caputi 
5451b912ec7SGeorge Wilson 	(void) arc_read(NULL, spa, bp, NULL, NULL, ZIO_PRIORITY_ASYNC_READ,
546eb633035STom Caputi 	    zio_flags, &aflags, zb);
547fa9e4066Sahrens 
54888b7b0f2SMatthew Ahrens 	return (0);
549fa9e4066Sahrens }
550fa9e4066Sahrens 
551fa9e4066Sahrens static void
traverse_prefetch_thread(void * arg)55288b7b0f2SMatthew Ahrens traverse_prefetch_thread(void *arg)
553fa9e4066Sahrens {
5546e0cbcaaSMatthew Ahrens 	traverse_data_t *td_main = arg;
5556e0cbcaaSMatthew Ahrens 	traverse_data_t td = *td_main;
5567802d7bfSMatthew Ahrens 	zbookmark_phys_t czb;
557fa9e4066Sahrens 
55888b7b0f2SMatthew Ahrens 	td.td_func = traverse_prefetcher;
55988b7b0f2SMatthew Ahrens 	td.td_arg = td_main->td_pfd;
56088b7b0f2SMatthew Ahrens 	td.td_pfd = NULL;
5619c3fd121SMatthew Ahrens 	td.td_resume = &td_main->td_pfd->pd_resume;
562fa9e4066Sahrens 
563b24ab676SJeff Bonwick 	SET_BOOKMARK(&czb, td.td_objset,
564b24ab676SJeff Bonwick 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
5651b912ec7SGeorge Wilson 	(void) traverse_visitbp(&td, NULL, td.td_rootbp, &czb);
566fa9e4066Sahrens 
56788b7b0f2SMatthew Ahrens 	mutex_enter(&td_main->td_pfd->pd_mtx);
56888b7b0f2SMatthew Ahrens 	td_main->td_pfd->pd_exited = B_TRUE;
56988b7b0f2SMatthew Ahrens 	cv_broadcast(&td_main->td_pfd->pd_cv);
57088b7b0f2SMatthew Ahrens 	mutex_exit(&td_main->td_pfd->pd_mtx);
571fa9e4066Sahrens }
572fa9e4066Sahrens 
57388b7b0f2SMatthew Ahrens /*
57488b7b0f2SMatthew Ahrens  * NB: dataset must not be changing on-disk (eg, is a snapshot or we are
57588b7b0f2SMatthew Ahrens  * in syncing context).
57688b7b0f2SMatthew Ahrens  */
57788b7b0f2SMatthew Ahrens static int
traverse_impl(spa_t * spa,dsl_dataset_t * ds,uint64_t objset,blkptr_t * rootbp,uint64_t txg_start,zbookmark_phys_t * resume,int flags,blkptr_cb_t func,void * arg)578ad135b5dSChristopher Siden traverse_impl(spa_t *spa, dsl_dataset_t *ds, uint64_t objset, blkptr_t *rootbp,
5797802d7bfSMatthew Ahrens     uint64_t txg_start, zbookmark_phys_t *resume, int flags,
580ad135b5dSChristopher Siden     blkptr_cb_t func, void *arg)
581fa9e4066Sahrens {
5826e0cbcaaSMatthew Ahrens 	traverse_data_t td;
5836e0cbcaaSMatthew Ahrens 	prefetch_data_t pd = { 0 };
5847802d7bfSMatthew Ahrens 	zbookmark_phys_t czb;
58588b7b0f2SMatthew Ahrens 	int err;
586fa9e4066Sahrens 
587ad135b5dSChristopher Siden 	ASSERT(ds == NULL || objset == ds->ds_object);
588ad135b5dSChristopher Siden 	ASSERT(!(flags & TRAVERSE_PRE) || !(flags & TRAVERSE_POST));
589ad135b5dSChristopher Siden 
59088b7b0f2SMatthew Ahrens 	td.td_spa = spa;
591ad135b5dSChristopher Siden 	td.td_objset = objset;
59288b7b0f2SMatthew Ahrens 	td.td_rootbp = rootbp;
59388b7b0f2SMatthew Ahrens 	td.td_min_txg = txg_start;
594ad135b5dSChristopher Siden 	td.td_resume = resume;
59588b7b0f2SMatthew Ahrens 	td.td_func = func;
59688b7b0f2SMatthew Ahrens 	td.td_arg = arg;
59788b7b0f2SMatthew Ahrens 	td.td_pfd = &pd;
59888b7b0f2SMatthew Ahrens 	td.td_flags = flags;
5997fd05ac4SMatthew Ahrens 	td.td_paused = B_FALSE;
600286ef713SPaul Dagnelie 	td.td_realloc_possible = (txg_start == 0 ? B_FALSE : B_TRUE);
60188b7b0f2SMatthew Ahrens 
602f7950bf1SMatthew Ahrens 	if (spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) {
603f7950bf1SMatthew Ahrens 		VERIFY(spa_feature_enabled_txg(spa,
604f7950bf1SMatthew Ahrens 		    SPA_FEATURE_HOLE_BIRTH, &td.td_hole_birth_enabled_txg));
605f7950bf1SMatthew Ahrens 	} else {
606286ef713SPaul Dagnelie 		td.td_hole_birth_enabled_txg = UINT64_MAX;
607f7950bf1SMatthew Ahrens 	}
608f7950bf1SMatthew Ahrens 
60988b7b0f2SMatthew Ahrens 	pd.pd_flags = flags;
6109c3fd121SMatthew Ahrens 	if (resume != NULL)
6119c3fd121SMatthew Ahrens 		pd.pd_resume = *resume;
61288b7b0f2SMatthew Ahrens 	mutex_init(&pd.pd_mtx, NULL, MUTEX_DEFAULT, NULL);
61388b7b0f2SMatthew Ahrens 	cv_init(&pd.pd_cv, NULL, CV_DEFAULT, NULL);
61488b7b0f2SMatthew Ahrens 
615eb633035STom Caputi 	SET_BOOKMARK(&czb, td.td_objset,
616eb633035STom Caputi 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
617eb633035STom Caputi 
6186e0cbcaaSMatthew Ahrens 	/* See comment on ZIL traversal in dsl_scan_visitds. */
619bc9014e6SJustin Gibbs 	if (ds != NULL && !ds->ds_is_snapshot && !BP_IS_HOLE(rootbp)) {
620eb633035STom Caputi 		enum zio_flag zio_flags = ZIO_FLAG_CANFAIL;
6217adb730bSGeorge Wilson 		arc_flags_t flags = ARC_FLAG_WAIT;
6223b2aab18SMatthew Ahrens 		objset_phys_t *osp;
6233b2aab18SMatthew Ahrens 		arc_buf_t *buf;
6246e0cbcaaSMatthew Ahrens 
625eb633035STom Caputi 		if ((td.td_flags & TRAVERSE_NO_DECRYPT) &&
626eb633035STom Caputi 		    BP_IS_PROTECTED(rootbp))
627eb633035STom Caputi 			zio_flags |= ZIO_FLAG_RAW;
628eb633035STom Caputi 
629eb633035STom Caputi 		err = arc_read(NULL, td.td_spa, rootbp, arc_getbuf_func,
630eb633035STom Caputi 		    &buf, ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, &czb);
6313b2aab18SMatthew Ahrens 		if (err != 0)
6326e0cbcaaSMatthew Ahrens 			return (err);
6336e0cbcaaSMatthew Ahrens 
6343b2aab18SMatthew Ahrens 		osp = buf->b_data;
6353b2aab18SMatthew Ahrens 		traverse_zil(&td, &osp->os_zil_header);
636dcbf3bd6SGeorge Wilson 		arc_buf_destroy(buf, &buf);
6376e0cbcaaSMatthew Ahrens 	}
6386e0cbcaaSMatthew Ahrens 
639b4709335SMatthew Ahrens 	if (!(flags & TRAVERSE_PREFETCH_DATA) ||
640fc8ae2ecSToomas Soome 	    taskq_dispatch(system_taskq, traverse_prefetch_thread,
641fc8ae2ecSToomas Soome 	    &td, TQ_NOQUEUE) == TASKQID_INVALID)
64288b7b0f2SMatthew Ahrens 		pd.pd_exited = B_TRUE;
64388b7b0f2SMatthew Ahrens 
6441b912ec7SGeorge Wilson 	err = traverse_visitbp(&td, NULL, rootbp, &czb);
64588b7b0f2SMatthew Ahrens 
64688b7b0f2SMatthew Ahrens 	mutex_enter(&pd.pd_mtx);
64788b7b0f2SMatthew Ahrens 	pd.pd_cancel = B_TRUE;
64888b7b0f2SMatthew Ahrens 	cv_broadcast(&pd.pd_cv);
64988b7b0f2SMatthew Ahrens 	while (!pd.pd_exited)
65088b7b0f2SMatthew Ahrens 		cv_wait(&pd.pd_cv, &pd.pd_mtx);
65188b7b0f2SMatthew Ahrens 	mutex_exit(&pd.pd_mtx);
65288b7b0f2SMatthew Ahrens 
65388b7b0f2SMatthew Ahrens 	mutex_destroy(&pd.pd_mtx);
65488b7b0f2SMatthew Ahrens 	cv_destroy(&pd.pd_cv);
655fa9e4066Sahrens 
65688b7b0f2SMatthew Ahrens 	return (err);
657fa9e4066Sahrens }
658fa9e4066Sahrens 
65988b7b0f2SMatthew Ahrens /*
66088b7b0f2SMatthew Ahrens  * NB: dataset must not be changing on-disk (eg, is a snapshot or we are
66188b7b0f2SMatthew Ahrens  * in syncing context).
66288b7b0f2SMatthew Ahrens  */
66388b7b0f2SMatthew Ahrens int
traverse_dataset_resume(dsl_dataset_t * ds,uint64_t txg_start,zbookmark_phys_t * resume,int flags,blkptr_cb_t func,void * arg)6649c3fd121SMatthew Ahrens traverse_dataset_resume(dsl_dataset_t *ds, uint64_t txg_start,
6659c3fd121SMatthew Ahrens     zbookmark_phys_t *resume,
6669c3fd121SMatthew Ahrens     int flags, blkptr_cb_t func, void *arg)
667fa9e4066Sahrens {
668ad135b5dSChristopher Siden 	return (traverse_impl(ds->ds_dir->dd_pool->dp_spa, ds, ds->ds_object,
6699c3fd121SMatthew Ahrens 	    &dsl_dataset_phys(ds)->ds_bp, txg_start, resume, flags, func, arg));
6709c3fd121SMatthew Ahrens }
6719c3fd121SMatthew Ahrens 
6729c3fd121SMatthew Ahrens int
traverse_dataset(dsl_dataset_t * ds,uint64_t txg_start,int flags,blkptr_cb_t func,void * arg)6739c3fd121SMatthew Ahrens traverse_dataset(dsl_dataset_t *ds, uint64_t txg_start,
6749c3fd121SMatthew Ahrens     int flags, blkptr_cb_t func, void *arg)
6759c3fd121SMatthew Ahrens {
6769c3fd121SMatthew Ahrens 	return (traverse_dataset_resume(ds, txg_start, NULL, flags, func, arg));
677ad135b5dSChristopher Siden }
678ad135b5dSChristopher Siden 
679ad135b5dSChristopher Siden int
traverse_dataset_destroyed(spa_t * spa,blkptr_t * blkptr,uint64_t txg_start,zbookmark_phys_t * resume,int flags,blkptr_cb_t func,void * arg)680ad135b5dSChristopher Siden traverse_dataset_destroyed(spa_t *spa, blkptr_t *blkptr,
6817802d7bfSMatthew Ahrens     uint64_t txg_start, zbookmark_phys_t *resume, int flags,
682ad135b5dSChristopher Siden     blkptr_cb_t func, void *arg)
683ad135b5dSChristopher Siden {
684ad135b5dSChristopher Siden 	return (traverse_impl(spa, NULL, ZB_DESTROYED_OBJSET,
685ad135b5dSChristopher Siden 	    blkptr, txg_start, resume, flags, func, arg));
686fa9e4066Sahrens }
687fa9e4066Sahrens 
68888b7b0f2SMatthew Ahrens /*
68988b7b0f2SMatthew Ahrens  * NB: pool must not be changing on-disk (eg, from zdb or sync context).
69088b7b0f2SMatthew Ahrens  */
69188b7b0f2SMatthew Ahrens int
traverse_pool(spa_t * spa,uint64_t txg_start,int flags,blkptr_cb_t func,void * arg)692bbfd46c4SJeff Bonwick traverse_pool(spa_t *spa, uint64_t txg_start, int flags,
693bbfd46c4SJeff Bonwick     blkptr_cb_t func, void *arg)
694fa9e4066Sahrens {
6957fd05ac4SMatthew Ahrens 	int err;
69688b7b0f2SMatthew Ahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
69788b7b0f2SMatthew Ahrens 	objset_t *mos = dp->dp_meta_objset;
698cd088ea4SVictor Latushkin 	boolean_t hard = (flags & TRAVERSE_HARD);
69988b7b0f2SMatthew Ahrens 
70088b7b0f2SMatthew Ahrens 	/* visit the MOS */
701ad135b5dSChristopher Siden 	err = traverse_impl(spa, NULL, 0, spa_get_rootblkptr(spa),
702ad135b5dSChristopher Siden 	    txg_start, NULL, flags, func, arg);
7033b2aab18SMatthew Ahrens 	if (err != 0)
70488b7b0f2SMatthew Ahrens 		return (err);
70588b7b0f2SMatthew Ahrens 
70688b7b0f2SMatthew Ahrens 	/* visit each dataset */
7079c3fd121SMatthew Ahrens 	for (uint64_t obj = 1; err == 0;
7089c3fd121SMatthew Ahrens 	    err = dmu_object_next(mos, &obj, B_FALSE, txg_start)) {
70988b7b0f2SMatthew Ahrens 		dmu_object_info_t doi;
71088b7b0f2SMatthew Ahrens 
71188b7b0f2SMatthew Ahrens 		err = dmu_object_info(mos, obj, &doi);
7123b2aab18SMatthew Ahrens 		if (err != 0) {
7137fd05ac4SMatthew Ahrens 			if (hard)
7147fd05ac4SMatthew Ahrens 				continue;
7157fd05ac4SMatthew Ahrens 			break;
716cd088ea4SVictor Latushkin 		}
71788b7b0f2SMatthew Ahrens 
7182acef22dSMatthew Ahrens 		if (doi.doi_bonus_type == DMU_OT_DSL_DATASET) {
71988b7b0f2SMatthew Ahrens 			dsl_dataset_t *ds;
720468c413aSTim Haley 			uint64_t txg = txg_start;
721468c413aSTim Haley 
7223b2aab18SMatthew Ahrens 			dsl_pool_config_enter(dp, FTAG);
72388b7b0f2SMatthew Ahrens 			err = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
7243b2aab18SMatthew Ahrens 			dsl_pool_config_exit(dp, FTAG);
7253b2aab18SMatthew Ahrens 			if (err != 0) {
7267fd05ac4SMatthew Ahrens 				if (hard)
7277fd05ac4SMatthew Ahrens 					continue;
7287fd05ac4SMatthew Ahrens 				break;
729cd088ea4SVictor Latushkin 			}
730c1379625SJustin T. Gibbs 			if (dsl_dataset_phys(ds)->ds_prev_snap_txg > txg)
731c1379625SJustin T. Gibbs 				txg = dsl_dataset_phys(ds)->ds_prev_snap_txg;
732bbfd46c4SJeff Bonwick 			err = traverse_dataset(ds, txg, flags, func, arg);
73388b7b0f2SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
7347fd05ac4SMatthew Ahrens 			if (err != 0)
7357fd05ac4SMatthew Ahrens 				break;
73688b7b0f2SMatthew Ahrens 		}
737fa9e4066Sahrens 	}
73888b7b0f2SMatthew Ahrens 	if (err == ESRCH)
73988b7b0f2SMatthew Ahrens 		err = 0;
7407fd05ac4SMatthew Ahrens 	return (err);
741fa9e4066Sahrens }
742