xref: /freebsd/sys/contrib/openzfs/module/zfs/dmu_traverse.c (revision 15f0b8c309dea1dcb14d3e374686576ff68ac43f)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy /*
22eda14cbcSMatt Macy  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23eda14cbcSMatt Macy  * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
24eda14cbcSMatt Macy  */
25eda14cbcSMatt Macy 
26eda14cbcSMatt Macy #include <sys/zfs_context.h>
27eda14cbcSMatt Macy #include <sys/dmu_objset.h>
28eda14cbcSMatt Macy #include <sys/dmu_traverse.h>
29eda14cbcSMatt Macy #include <sys/dsl_dataset.h>
30eda14cbcSMatt Macy #include <sys/dsl_dir.h>
31eda14cbcSMatt Macy #include <sys/dsl_pool.h>
32eda14cbcSMatt Macy #include <sys/dnode.h>
33eda14cbcSMatt Macy #include <sys/spa.h>
34eda14cbcSMatt Macy #include <sys/spa_impl.h>
35eda14cbcSMatt Macy #include <sys/zio.h>
36eda14cbcSMatt Macy #include <sys/dmu_impl.h>
37eda14cbcSMatt Macy #include <sys/sa.h>
38eda14cbcSMatt Macy #include <sys/sa_impl.h>
39eda14cbcSMatt Macy #include <sys/callb.h>
40eda14cbcSMatt Macy #include <sys/zfeature.h>
41eda14cbcSMatt Macy 
42e92ffd9bSMartin Matuska static int32_t zfs_pd_bytes_max = 50 * 1024 * 1024;	/* 50MB */
43e92ffd9bSMartin Matuska static int32_t send_holes_without_birth_time = 1;
44be181ee2SMartin Matuska static uint_t zfs_traverse_indirect_prefetch_limit = 32;
45eda14cbcSMatt Macy 
46eda14cbcSMatt Macy typedef struct prefetch_data {
47eda14cbcSMatt Macy 	kmutex_t pd_mtx;
48eda14cbcSMatt Macy 	kcondvar_t pd_cv;
49eda14cbcSMatt Macy 	int32_t pd_bytes_fetched;
50eda14cbcSMatt Macy 	int pd_flags;
51eda14cbcSMatt Macy 	boolean_t pd_cancel;
52eda14cbcSMatt Macy 	boolean_t pd_exited;
53eda14cbcSMatt Macy 	zbookmark_phys_t pd_resume;
54eda14cbcSMatt Macy } prefetch_data_t;
55eda14cbcSMatt Macy 
56eda14cbcSMatt Macy typedef struct traverse_data {
57eda14cbcSMatt Macy 	spa_t *td_spa;
58eda14cbcSMatt Macy 	uint64_t td_objset;
59eda14cbcSMatt Macy 	blkptr_t *td_rootbp;
60eda14cbcSMatt Macy 	uint64_t td_min_txg;
61eda14cbcSMatt Macy 	zbookmark_phys_t *td_resume;
62eda14cbcSMatt Macy 	int td_flags;
63eda14cbcSMatt Macy 	prefetch_data_t *td_pfd;
64eda14cbcSMatt Macy 	boolean_t td_paused;
65eda14cbcSMatt Macy 	uint64_t td_hole_birth_enabled_txg;
66eda14cbcSMatt Macy 	blkptr_cb_t *td_func;
67eda14cbcSMatt Macy 	void *td_arg;
68eda14cbcSMatt Macy 	boolean_t td_realloc_possible;
69eda14cbcSMatt Macy } traverse_data_t;
70eda14cbcSMatt Macy 
71eda14cbcSMatt Macy static int traverse_dnode(traverse_data_t *td, const blkptr_t *bp,
72eda14cbcSMatt Macy     const dnode_phys_t *dnp, uint64_t objset, uint64_t object);
73eda14cbcSMatt Macy static void prefetch_dnode_metadata(traverse_data_t *td, const dnode_phys_t *,
74eda14cbcSMatt Macy     uint64_t objset, uint64_t object);
75eda14cbcSMatt Macy 
76eda14cbcSMatt Macy static int
77180f8225SMatt Macy traverse_zil_block(zilog_t *zilog, const blkptr_t *bp, void *arg,
78180f8225SMatt Macy     uint64_t claim_txg)
79eda14cbcSMatt Macy {
80eda14cbcSMatt Macy 	traverse_data_t *td = arg;
81eda14cbcSMatt Macy 	zbookmark_phys_t zb;
82eda14cbcSMatt Macy 
83eda14cbcSMatt Macy 	if (BP_IS_HOLE(bp))
84eda14cbcSMatt Macy 		return (0);
85eda14cbcSMatt Macy 
86eda14cbcSMatt Macy 	if (claim_txg == 0 && bp->blk_birth >= spa_min_claim_txg(td->td_spa))
87eda14cbcSMatt Macy 		return (-1);
88eda14cbcSMatt Macy 
89eda14cbcSMatt Macy 	SET_BOOKMARK(&zb, td->td_objset, ZB_ZIL_OBJECT, ZB_ZIL_LEVEL,
90eda14cbcSMatt Macy 	    bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
91eda14cbcSMatt Macy 
92eda14cbcSMatt Macy 	(void) td->td_func(td->td_spa, zilog, bp, &zb, NULL, td->td_arg);
93eda14cbcSMatt Macy 
94eda14cbcSMatt Macy 	return (0);
95eda14cbcSMatt Macy }
96eda14cbcSMatt Macy 
97eda14cbcSMatt Macy static int
98180f8225SMatt Macy traverse_zil_record(zilog_t *zilog, const lr_t *lrc, void *arg,
99180f8225SMatt Macy     uint64_t claim_txg)
100eda14cbcSMatt Macy {
101eda14cbcSMatt Macy 	traverse_data_t *td = arg;
102eda14cbcSMatt Macy 
103eda14cbcSMatt Macy 	if (lrc->lrc_txtype == TX_WRITE) {
104eda14cbcSMatt Macy 		lr_write_t *lr = (lr_write_t *)lrc;
105eda14cbcSMatt Macy 		blkptr_t *bp = &lr->lr_blkptr;
106eda14cbcSMatt Macy 		zbookmark_phys_t zb;
107eda14cbcSMatt Macy 
108eda14cbcSMatt Macy 		if (BP_IS_HOLE(bp))
109eda14cbcSMatt Macy 			return (0);
110eda14cbcSMatt Macy 
111eda14cbcSMatt Macy 		if (claim_txg == 0 || bp->blk_birth < claim_txg)
112eda14cbcSMatt Macy 			return (0);
113eda14cbcSMatt Macy 
114dbd5678dSMartin Matuska 		ASSERT3U(BP_GET_LSIZE(bp), !=, 0);
115eda14cbcSMatt Macy 		SET_BOOKMARK(&zb, td->td_objset, lr->lr_foid,
116eda14cbcSMatt Macy 		    ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp));
117eda14cbcSMatt Macy 
118eda14cbcSMatt Macy 		(void) td->td_func(td->td_spa, zilog, bp, &zb, NULL,
119eda14cbcSMatt Macy 		    td->td_arg);
120eda14cbcSMatt Macy 	}
121eda14cbcSMatt Macy 	return (0);
122eda14cbcSMatt Macy }
123eda14cbcSMatt Macy 
124eda14cbcSMatt Macy static void
125eda14cbcSMatt Macy traverse_zil(traverse_data_t *td, zil_header_t *zh)
126eda14cbcSMatt Macy {
127eda14cbcSMatt Macy 	uint64_t claim_txg = zh->zh_claim_txg;
128eda14cbcSMatt Macy 
129eda14cbcSMatt Macy 	/*
130eda14cbcSMatt Macy 	 * We only want to visit blocks that have been claimed but not yet
131eda14cbcSMatt Macy 	 * replayed; plus blocks that are already stable in read-only mode.
132eda14cbcSMatt Macy 	 */
133eda14cbcSMatt Macy 	if (claim_txg == 0 && spa_writeable(td->td_spa))
134eda14cbcSMatt Macy 		return;
135eda14cbcSMatt Macy 
136eda14cbcSMatt Macy 	zilog_t *zilog = zil_alloc(spa_get_dsl(td->td_spa)->dp_meta_objset, zh);
137eda14cbcSMatt Macy 	(void) zil_parse(zilog, traverse_zil_block, traverse_zil_record, td,
138eda14cbcSMatt Macy 	    claim_txg, !(td->td_flags & TRAVERSE_NO_DECRYPT));
139eda14cbcSMatt Macy 	zil_free(zilog);
140eda14cbcSMatt Macy }
141eda14cbcSMatt Macy 
142eda14cbcSMatt Macy typedef enum resume_skip {
143eda14cbcSMatt Macy 	RESUME_SKIP_ALL,
144eda14cbcSMatt Macy 	RESUME_SKIP_NONE,
145eda14cbcSMatt Macy 	RESUME_SKIP_CHILDREN
146eda14cbcSMatt Macy } resume_skip_t;
147eda14cbcSMatt Macy 
148eda14cbcSMatt Macy /*
149eda14cbcSMatt Macy  * Returns RESUME_SKIP_ALL if td indicates that we are resuming a traversal and
150eda14cbcSMatt Macy  * the block indicated by zb does not need to be visited at all. Returns
151eda14cbcSMatt Macy  * RESUME_SKIP_CHILDREN if we are resuming a post traversal and we reach the
152eda14cbcSMatt Macy  * resume point. This indicates that this block should be visited but not its
153eda14cbcSMatt Macy  * children (since they must have been visited in a previous traversal).
154eda14cbcSMatt Macy  * Otherwise returns RESUME_SKIP_NONE.
155eda14cbcSMatt Macy  */
156eda14cbcSMatt Macy static resume_skip_t
157eda14cbcSMatt Macy resume_skip_check(traverse_data_t *td, const dnode_phys_t *dnp,
158eda14cbcSMatt Macy     const zbookmark_phys_t *zb)
159eda14cbcSMatt Macy {
160eda14cbcSMatt Macy 	if (td->td_resume != NULL && !ZB_IS_ZERO(td->td_resume)) {
161eda14cbcSMatt Macy 		/*
162eda14cbcSMatt Macy 		 * If we already visited this bp & everything below,
163eda14cbcSMatt Macy 		 * don't bother doing it again.
164eda14cbcSMatt Macy 		 */
165eda14cbcSMatt Macy 		if (zbookmark_subtree_completed(dnp, zb, td->td_resume))
166eda14cbcSMatt Macy 			return (RESUME_SKIP_ALL);
167eda14cbcSMatt Macy 
168eda14cbcSMatt Macy 		/*
169eda14cbcSMatt Macy 		 * If we found the block we're trying to resume from, zero
170eda14cbcSMatt Macy 		 * the bookmark out to indicate that we have resumed.
171eda14cbcSMatt Macy 		 */
172da5137abSMartin Matuska 		if (memcmp(zb, td->td_resume, sizeof (*zb)) == 0) {
173da5137abSMartin Matuska 			memset(td->td_resume, 0, sizeof (*zb));
174eda14cbcSMatt Macy 			if (td->td_flags & TRAVERSE_POST)
175eda14cbcSMatt Macy 				return (RESUME_SKIP_CHILDREN);
176eda14cbcSMatt Macy 		}
177eda14cbcSMatt Macy 	}
178eda14cbcSMatt Macy 	return (RESUME_SKIP_NONE);
179eda14cbcSMatt Macy }
180eda14cbcSMatt Macy 
18116038816SMartin Matuska /*
18216038816SMartin Matuska  * Returns B_TRUE, if prefetch read is issued, otherwise B_FALSE.
18316038816SMartin Matuska  */
18416038816SMartin Matuska static boolean_t
185eda14cbcSMatt Macy traverse_prefetch_metadata(traverse_data_t *td,
186eda14cbcSMatt Macy     const blkptr_t *bp, const zbookmark_phys_t *zb)
187eda14cbcSMatt Macy {
188*15f0b8c3SMartin Matuska 	arc_flags_t flags = ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH |
189*15f0b8c3SMartin Matuska 	    ARC_FLAG_PRESCIENT_PREFETCH;
190eda14cbcSMatt Macy 	int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE;
191eda14cbcSMatt Macy 
192eda14cbcSMatt Macy 	if (!(td->td_flags & TRAVERSE_PREFETCH_METADATA))
19316038816SMartin Matuska 		return (B_FALSE);
194eda14cbcSMatt Macy 	/*
195eda14cbcSMatt Macy 	 * If we are in the process of resuming, don't prefetch, because
196eda14cbcSMatt Macy 	 * some children will not be needed (and in fact may have already
197eda14cbcSMatt Macy 	 * been freed).
198eda14cbcSMatt Macy 	 */
199eda14cbcSMatt Macy 	if (td->td_resume != NULL && !ZB_IS_ZERO(td->td_resume))
20016038816SMartin Matuska 		return (B_FALSE);
201eda14cbcSMatt Macy 	if (BP_IS_HOLE(bp) || bp->blk_birth <= td->td_min_txg)
20216038816SMartin Matuska 		return (B_FALSE);
203eda14cbcSMatt Macy 	if (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_DNODE)
20416038816SMartin Matuska 		return (B_FALSE);
205eda14cbcSMatt Macy 	ASSERT(!BP_IS_REDACTED(bp));
206eda14cbcSMatt Macy 
207eda14cbcSMatt Macy 	if ((td->td_flags & TRAVERSE_NO_DECRYPT) && BP_IS_PROTECTED(bp))
208eda14cbcSMatt Macy 		zio_flags |= ZIO_FLAG_RAW;
209eda14cbcSMatt Macy 
210eda14cbcSMatt Macy 	(void) arc_read(NULL, td->td_spa, bp, NULL, NULL,
211eda14cbcSMatt Macy 	    ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
21216038816SMartin Matuska 	return (B_TRUE);
213eda14cbcSMatt Macy }
214eda14cbcSMatt Macy 
215eda14cbcSMatt Macy static boolean_t
216eda14cbcSMatt Macy prefetch_needed(prefetch_data_t *pfd, const blkptr_t *bp)
217eda14cbcSMatt Macy {
218eda14cbcSMatt Macy 	ASSERT(pfd->pd_flags & TRAVERSE_PREFETCH_DATA);
219eda14cbcSMatt Macy 	if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp) ||
220eda14cbcSMatt Macy 	    BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG || BP_IS_REDACTED(bp))
221eda14cbcSMatt Macy 		return (B_FALSE);
222eda14cbcSMatt Macy 	return (B_TRUE);
223eda14cbcSMatt Macy }
224eda14cbcSMatt Macy 
225eda14cbcSMatt Macy static int
226eda14cbcSMatt Macy traverse_visitbp(traverse_data_t *td, const dnode_phys_t *dnp,
227eda14cbcSMatt Macy     const blkptr_t *bp, const zbookmark_phys_t *zb)
228eda14cbcSMatt Macy {
229eda14cbcSMatt Macy 	int err = 0;
230eda14cbcSMatt Macy 	arc_buf_t *buf = NULL;
231eda14cbcSMatt Macy 	prefetch_data_t *pd = td->td_pfd;
232eda14cbcSMatt Macy 
233eda14cbcSMatt Macy 	switch (resume_skip_check(td, dnp, zb)) {
234eda14cbcSMatt Macy 	case RESUME_SKIP_ALL:
235eda14cbcSMatt Macy 		return (0);
236eda14cbcSMatt Macy 	case RESUME_SKIP_CHILDREN:
237eda14cbcSMatt Macy 		goto post;
238eda14cbcSMatt Macy 	case RESUME_SKIP_NONE:
239eda14cbcSMatt Macy 		break;
240eda14cbcSMatt Macy 	default:
241eda14cbcSMatt Macy 		ASSERT(0);
242eda14cbcSMatt Macy 	}
243eda14cbcSMatt Macy 
244eda14cbcSMatt Macy 	if (bp->blk_birth == 0) {
245eda14cbcSMatt Macy 		/*
246eda14cbcSMatt Macy 		 * Since this block has a birth time of 0 it must be one of
247eda14cbcSMatt Macy 		 * two things: a hole created before the
248eda14cbcSMatt Macy 		 * SPA_FEATURE_HOLE_BIRTH feature was enabled, or a hole
249eda14cbcSMatt Macy 		 * which has always been a hole in an object.
250eda14cbcSMatt Macy 		 *
251eda14cbcSMatt Macy 		 * If a file is written sparsely, then the unwritten parts of
252eda14cbcSMatt Macy 		 * the file were "always holes" -- that is, they have been
253eda14cbcSMatt Macy 		 * holes since this object was allocated.  However, we (and
254eda14cbcSMatt Macy 		 * our callers) can not necessarily tell when an object was
255eda14cbcSMatt Macy 		 * allocated.  Therefore, if it's possible that this object
256eda14cbcSMatt Macy 		 * was freed and then its object number reused, we need to
257eda14cbcSMatt Macy 		 * visit all the holes with birth==0.
258eda14cbcSMatt Macy 		 *
259eda14cbcSMatt Macy 		 * If it isn't possible that the object number was reused,
260eda14cbcSMatt Macy 		 * then if SPA_FEATURE_HOLE_BIRTH was enabled before we wrote
261eda14cbcSMatt Macy 		 * all the blocks we will visit as part of this traversal,
262eda14cbcSMatt Macy 		 * then this hole must have always existed, so we can skip
263eda14cbcSMatt Macy 		 * it.  We visit blocks born after (exclusive) td_min_txg.
264eda14cbcSMatt Macy 		 *
265eda14cbcSMatt Macy 		 * Note that the meta-dnode cannot be reallocated.
266eda14cbcSMatt Macy 		 */
267eda14cbcSMatt Macy 		if (!send_holes_without_birth_time &&
268eda14cbcSMatt Macy 		    (!td->td_realloc_possible ||
269eda14cbcSMatt Macy 		    zb->zb_object == DMU_META_DNODE_OBJECT) &&
270eda14cbcSMatt Macy 		    td->td_hole_birth_enabled_txg <= td->td_min_txg)
271eda14cbcSMatt Macy 			return (0);
272eda14cbcSMatt Macy 	} else if (bp->blk_birth <= td->td_min_txg) {
273eda14cbcSMatt Macy 		return (0);
274eda14cbcSMatt Macy 	}
275eda14cbcSMatt Macy 
276eda14cbcSMatt Macy 	if (pd != NULL && !pd->pd_exited && prefetch_needed(pd, bp)) {
277eda14cbcSMatt Macy 		uint64_t size = BP_GET_LSIZE(bp);
278eda14cbcSMatt Macy 		mutex_enter(&pd->pd_mtx);
279eda14cbcSMatt Macy 		ASSERT(pd->pd_bytes_fetched >= 0);
280eda14cbcSMatt Macy 		while (pd->pd_bytes_fetched < size && !pd->pd_exited)
281eda14cbcSMatt Macy 			cv_wait_sig(&pd->pd_cv, &pd->pd_mtx);
282eda14cbcSMatt Macy 		pd->pd_bytes_fetched -= size;
283eda14cbcSMatt Macy 		cv_broadcast(&pd->pd_cv);
284eda14cbcSMatt Macy 		mutex_exit(&pd->pd_mtx);
285eda14cbcSMatt Macy 	}
286eda14cbcSMatt Macy 
287eda14cbcSMatt Macy 	if (BP_IS_HOLE(bp) || BP_IS_REDACTED(bp)) {
288eda14cbcSMatt Macy 		err = td->td_func(td->td_spa, NULL, bp, zb, dnp, td->td_arg);
289eda14cbcSMatt Macy 		if (err != 0)
290eda14cbcSMatt Macy 			goto post;
291eda14cbcSMatt Macy 		return (0);
292eda14cbcSMatt Macy 	}
293eda14cbcSMatt Macy 
294eda14cbcSMatt Macy 	if (td->td_flags & TRAVERSE_PRE) {
295eda14cbcSMatt Macy 		err = td->td_func(td->td_spa, NULL, bp, zb, dnp,
296eda14cbcSMatt Macy 		    td->td_arg);
297eda14cbcSMatt Macy 		if (err == TRAVERSE_VISIT_NO_CHILDREN)
298eda14cbcSMatt Macy 			return (0);
299eda14cbcSMatt Macy 		if (err != 0)
300eda14cbcSMatt Macy 			goto post;
301eda14cbcSMatt Macy 	}
302eda14cbcSMatt Macy 
303eda14cbcSMatt Macy 	if (BP_GET_LEVEL(bp) > 0) {
304eda14cbcSMatt Macy 		uint32_t flags = ARC_FLAG_WAIT;
30516038816SMartin Matuska 		int32_t i, ptidx, pidx;
30616038816SMartin Matuska 		uint32_t prefetchlimit;
307eda14cbcSMatt Macy 		int32_t epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
308eda14cbcSMatt Macy 		zbookmark_phys_t *czb;
309eda14cbcSMatt Macy 
310eda14cbcSMatt Macy 		ASSERT(!BP_IS_PROTECTED(bp));
311eda14cbcSMatt Macy 
312eda14cbcSMatt Macy 		err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf,
313eda14cbcSMatt Macy 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
314eda14cbcSMatt Macy 		if (err != 0)
315eda14cbcSMatt Macy 			goto post;
316eda14cbcSMatt Macy 
317eda14cbcSMatt Macy 		czb = kmem_alloc(sizeof (zbookmark_phys_t), KM_SLEEP);
318eda14cbcSMatt Macy 
31916038816SMartin Matuska 		/*
32016038816SMartin Matuska 		 * When performing a traversal it is beneficial to
32116038816SMartin Matuska 		 * asynchronously read-ahead the upcoming indirect
32216038816SMartin Matuska 		 * blocks since they will be needed shortly. However,
32316038816SMartin Matuska 		 * since a 128k indirect (non-L0) block may contain up
32416038816SMartin Matuska 		 * to 1024 128-byte block pointers, its preferable to not
32516038816SMartin Matuska 		 * prefetch them all at once. Issuing a large number of
32616038816SMartin Matuska 		 * async reads may effect performance, and the earlier
32716038816SMartin Matuska 		 * the indirect blocks are prefetched the less likely
32816038816SMartin Matuska 		 * they are to still be resident in the ARC when needed.
32916038816SMartin Matuska 		 * Therefore, prefetching indirect blocks is limited to
33016038816SMartin Matuska 		 * zfs_traverse_indirect_prefetch_limit=32 blocks by
33116038816SMartin Matuska 		 * default.
33216038816SMartin Matuska 		 *
33316038816SMartin Matuska 		 * pidx: Index for which next prefetch to be issued.
33416038816SMartin Matuska 		 * ptidx: Index at which next prefetch to be triggered.
33516038816SMartin Matuska 		 */
33616038816SMartin Matuska 		ptidx = 0;
33716038816SMartin Matuska 		pidx = 1;
33816038816SMartin Matuska 		prefetchlimit = zfs_traverse_indirect_prefetch_limit;
339eda14cbcSMatt Macy 		for (i = 0; i < epb; i++) {
34016038816SMartin Matuska 			if (prefetchlimit && i == ptidx) {
34116038816SMartin Matuska 				ASSERT3S(ptidx, <=, pidx);
34216038816SMartin Matuska 				for (uint32_t  prefetched = 0; pidx < epb &&
34316038816SMartin Matuska 				    prefetched < prefetchlimit; pidx++) {
34416038816SMartin Matuska 					SET_BOOKMARK(czb, zb->zb_objset,
34516038816SMartin Matuska 					    zb->zb_object, zb->zb_level - 1,
34616038816SMartin Matuska 					    zb->zb_blkid * epb + pidx);
34716038816SMartin Matuska 					if (traverse_prefetch_metadata(td,
34816038816SMartin Matuska 					    &((blkptr_t *)buf->b_data)[pidx],
34916038816SMartin Matuska 					    czb) == B_TRUE) {
35016038816SMartin Matuska 						prefetched++;
35116038816SMartin Matuska 						if (prefetched ==
35216038816SMartin Matuska 						    MAX(prefetchlimit / 2, 1))
35316038816SMartin Matuska 							ptidx = pidx;
35416038816SMartin Matuska 					}
35516038816SMartin Matuska 				}
356eda14cbcSMatt Macy 			}
357eda14cbcSMatt Macy 
358eda14cbcSMatt Macy 			/* recursively visitbp() blocks below this */
359eda14cbcSMatt Macy 			SET_BOOKMARK(czb, zb->zb_objset, zb->zb_object,
360eda14cbcSMatt Macy 			    zb->zb_level - 1,
361eda14cbcSMatt Macy 			    zb->zb_blkid * epb + i);
362eda14cbcSMatt Macy 			err = traverse_visitbp(td, dnp,
363eda14cbcSMatt Macy 			    &((blkptr_t *)buf->b_data)[i], czb);
364eda14cbcSMatt Macy 			if (err != 0)
365eda14cbcSMatt Macy 				break;
366eda14cbcSMatt Macy 		}
367eda14cbcSMatt Macy 
368eda14cbcSMatt Macy 		kmem_free(czb, sizeof (zbookmark_phys_t));
369eda14cbcSMatt Macy 
370eda14cbcSMatt Macy 	} else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) {
371eda14cbcSMatt Macy 		uint32_t flags = ARC_FLAG_WAIT;
372eda14cbcSMatt Macy 		uint32_t zio_flags = ZIO_FLAG_CANFAIL;
373eda14cbcSMatt Macy 		int32_t i;
374eda14cbcSMatt Macy 		int32_t epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
375eda14cbcSMatt Macy 		dnode_phys_t *child_dnp;
376eda14cbcSMatt Macy 
377eda14cbcSMatt Macy 		/*
378eda14cbcSMatt Macy 		 * dnode blocks might have their bonus buffers encrypted, so
379eda14cbcSMatt Macy 		 * we must be careful to honor TRAVERSE_NO_DECRYPT
380eda14cbcSMatt Macy 		 */
381eda14cbcSMatt Macy 		if ((td->td_flags & TRAVERSE_NO_DECRYPT) && BP_IS_PROTECTED(bp))
382eda14cbcSMatt Macy 			zio_flags |= ZIO_FLAG_RAW;
383eda14cbcSMatt Macy 
384eda14cbcSMatt Macy 		err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf,
385eda14cbcSMatt Macy 		    ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
386eda14cbcSMatt Macy 		if (err != 0)
387eda14cbcSMatt Macy 			goto post;
388eda14cbcSMatt Macy 
389eda14cbcSMatt Macy 		child_dnp = buf->b_data;
390eda14cbcSMatt Macy 
391eda14cbcSMatt Macy 		for (i = 0; i < epb; i += child_dnp[i].dn_extra_slots + 1) {
392eda14cbcSMatt Macy 			prefetch_dnode_metadata(td, &child_dnp[i],
393eda14cbcSMatt Macy 			    zb->zb_objset, zb->zb_blkid * epb + i);
394eda14cbcSMatt Macy 		}
395eda14cbcSMatt Macy 
396eda14cbcSMatt Macy 		/* recursively visitbp() blocks below this */
397eda14cbcSMatt Macy 		for (i = 0; i < epb; i += child_dnp[i].dn_extra_slots + 1) {
398eda14cbcSMatt Macy 			err = traverse_dnode(td, bp, &child_dnp[i],
399eda14cbcSMatt Macy 			    zb->zb_objset, zb->zb_blkid * epb + i);
400eda14cbcSMatt Macy 			if (err != 0)
401eda14cbcSMatt Macy 				break;
402eda14cbcSMatt Macy 		}
403eda14cbcSMatt Macy 	} else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
404eda14cbcSMatt Macy 		uint32_t zio_flags = ZIO_FLAG_CANFAIL;
405eda14cbcSMatt Macy 		arc_flags_t flags = ARC_FLAG_WAIT;
406eda14cbcSMatt Macy 		objset_phys_t *osp;
407eda14cbcSMatt Macy 
408eda14cbcSMatt Macy 		if ((td->td_flags & TRAVERSE_NO_DECRYPT) && BP_IS_PROTECTED(bp))
409eda14cbcSMatt Macy 			zio_flags |= ZIO_FLAG_RAW;
410eda14cbcSMatt Macy 
411eda14cbcSMatt Macy 		err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf,
412eda14cbcSMatt Macy 		    ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
413eda14cbcSMatt Macy 		if (err != 0)
414eda14cbcSMatt Macy 			goto post;
415eda14cbcSMatt Macy 
416eda14cbcSMatt Macy 		osp = buf->b_data;
417eda14cbcSMatt Macy 		prefetch_dnode_metadata(td, &osp->os_meta_dnode, zb->zb_objset,
418eda14cbcSMatt Macy 		    DMU_META_DNODE_OBJECT);
419eda14cbcSMatt Macy 		/*
420eda14cbcSMatt Macy 		 * See the block comment above for the goal of this variable.
421eda14cbcSMatt Macy 		 * If the maxblkid of the meta-dnode is 0, then we know that
422eda14cbcSMatt Macy 		 * we've never had more than DNODES_PER_BLOCK objects in the
423eda14cbcSMatt Macy 		 * dataset, which means we can't have reused any object ids.
424eda14cbcSMatt Macy 		 */
425eda14cbcSMatt Macy 		if (osp->os_meta_dnode.dn_maxblkid == 0)
426eda14cbcSMatt Macy 			td->td_realloc_possible = B_FALSE;
427eda14cbcSMatt Macy 
428eda14cbcSMatt Macy 		if (OBJSET_BUF_HAS_USERUSED(buf)) {
429eda14cbcSMatt Macy 			if (OBJSET_BUF_HAS_PROJECTUSED(buf))
430eda14cbcSMatt Macy 				prefetch_dnode_metadata(td,
431eda14cbcSMatt Macy 				    &osp->os_projectused_dnode,
432eda14cbcSMatt Macy 				    zb->zb_objset, DMU_PROJECTUSED_OBJECT);
433eda14cbcSMatt Macy 			prefetch_dnode_metadata(td, &osp->os_groupused_dnode,
434eda14cbcSMatt Macy 			    zb->zb_objset, DMU_GROUPUSED_OBJECT);
435eda14cbcSMatt Macy 			prefetch_dnode_metadata(td, &osp->os_userused_dnode,
436eda14cbcSMatt Macy 			    zb->zb_objset, DMU_USERUSED_OBJECT);
437eda14cbcSMatt Macy 		}
438eda14cbcSMatt Macy 
439eda14cbcSMatt Macy 		err = traverse_dnode(td, bp, &osp->os_meta_dnode, zb->zb_objset,
440eda14cbcSMatt Macy 		    DMU_META_DNODE_OBJECT);
441eda14cbcSMatt Macy 		if (err == 0 && OBJSET_BUF_HAS_USERUSED(buf)) {
442eda14cbcSMatt Macy 			if (OBJSET_BUF_HAS_PROJECTUSED(buf))
443eda14cbcSMatt Macy 				err = traverse_dnode(td, bp,
444eda14cbcSMatt Macy 				    &osp->os_projectused_dnode, zb->zb_objset,
445eda14cbcSMatt Macy 				    DMU_PROJECTUSED_OBJECT);
446eda14cbcSMatt Macy 			if (err == 0)
447eda14cbcSMatt Macy 				err = traverse_dnode(td, bp,
448eda14cbcSMatt Macy 				    &osp->os_groupused_dnode, zb->zb_objset,
449eda14cbcSMatt Macy 				    DMU_GROUPUSED_OBJECT);
450eda14cbcSMatt Macy 			if (err == 0)
451eda14cbcSMatt Macy 				err = traverse_dnode(td, bp,
452eda14cbcSMatt Macy 				    &osp->os_userused_dnode, zb->zb_objset,
453eda14cbcSMatt Macy 				    DMU_USERUSED_OBJECT);
454eda14cbcSMatt Macy 		}
455eda14cbcSMatt Macy 	}
456eda14cbcSMatt Macy 
457eda14cbcSMatt Macy 	if (buf)
458eda14cbcSMatt Macy 		arc_buf_destroy(buf, &buf);
459eda14cbcSMatt Macy 
460eda14cbcSMatt Macy post:
461eda14cbcSMatt Macy 	if (err == 0 && (td->td_flags & TRAVERSE_POST))
462eda14cbcSMatt Macy 		err = td->td_func(td->td_spa, NULL, bp, zb, dnp, td->td_arg);
463eda14cbcSMatt Macy 
464eda14cbcSMatt Macy 	if ((td->td_flags & TRAVERSE_HARD) && (err == EIO || err == ECKSUM)) {
465eda14cbcSMatt Macy 		/*
466eda14cbcSMatt Macy 		 * Ignore this disk error as requested by the HARD flag,
467eda14cbcSMatt Macy 		 * and continue traversal.
468eda14cbcSMatt Macy 		 */
469eda14cbcSMatt Macy 		err = 0;
470eda14cbcSMatt Macy 	}
471eda14cbcSMatt Macy 
472eda14cbcSMatt Macy 	/*
473eda14cbcSMatt Macy 	 * If we are stopping here, set td_resume.
474eda14cbcSMatt Macy 	 */
475eda14cbcSMatt Macy 	if (td->td_resume != NULL && err != 0 && !td->td_paused) {
476eda14cbcSMatt Macy 		td->td_resume->zb_objset = zb->zb_objset;
477eda14cbcSMatt Macy 		td->td_resume->zb_object = zb->zb_object;
478eda14cbcSMatt Macy 		td->td_resume->zb_level = 0;
479eda14cbcSMatt Macy 		/*
480eda14cbcSMatt Macy 		 * If we have stopped on an indirect block (e.g. due to
481eda14cbcSMatt Macy 		 * i/o error), we have not visited anything below it.
482eda14cbcSMatt Macy 		 * Set the bookmark to the first level-0 block that we need
483eda14cbcSMatt Macy 		 * to visit.  This way, the resuming code does not need to
484eda14cbcSMatt Macy 		 * deal with resuming from indirect blocks.
485eda14cbcSMatt Macy 		 *
486eda14cbcSMatt Macy 		 * Note, if zb_level <= 0, dnp may be NULL, so we don't want
487eda14cbcSMatt Macy 		 * to dereference it.
488eda14cbcSMatt Macy 		 */
489eda14cbcSMatt Macy 		td->td_resume->zb_blkid = zb->zb_blkid;
490eda14cbcSMatt Macy 		if (zb->zb_level > 0) {
491eda14cbcSMatt Macy 			td->td_resume->zb_blkid <<= zb->zb_level *
492eda14cbcSMatt Macy 			    (dnp->dn_indblkshift - SPA_BLKPTRSHIFT);
493eda14cbcSMatt Macy 		}
494eda14cbcSMatt Macy 		td->td_paused = B_TRUE;
495eda14cbcSMatt Macy 	}
496eda14cbcSMatt Macy 
497eda14cbcSMatt Macy 	return (err);
498eda14cbcSMatt Macy }
499eda14cbcSMatt Macy 
500eda14cbcSMatt Macy static void
501eda14cbcSMatt Macy prefetch_dnode_metadata(traverse_data_t *td, const dnode_phys_t *dnp,
502eda14cbcSMatt Macy     uint64_t objset, uint64_t object)
503eda14cbcSMatt Macy {
504eda14cbcSMatt Macy 	int j;
505eda14cbcSMatt Macy 	zbookmark_phys_t czb;
506eda14cbcSMatt Macy 
507eda14cbcSMatt Macy 	for (j = 0; j < dnp->dn_nblkptr; j++) {
508eda14cbcSMatt Macy 		SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j);
509eda14cbcSMatt Macy 		traverse_prefetch_metadata(td, &dnp->dn_blkptr[j], &czb);
510eda14cbcSMatt Macy 	}
511eda14cbcSMatt Macy 
512eda14cbcSMatt Macy 	if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
513eda14cbcSMatt Macy 		SET_BOOKMARK(&czb, objset, object, 0, DMU_SPILL_BLKID);
514eda14cbcSMatt Macy 		traverse_prefetch_metadata(td, DN_SPILL_BLKPTR(dnp), &czb);
515eda14cbcSMatt Macy 	}
516eda14cbcSMatt Macy }
517eda14cbcSMatt Macy 
518eda14cbcSMatt Macy static int
519eda14cbcSMatt Macy traverse_dnode(traverse_data_t *td, const blkptr_t *bp, const dnode_phys_t *dnp,
520eda14cbcSMatt Macy     uint64_t objset, uint64_t object)
521eda14cbcSMatt Macy {
522eda14cbcSMatt Macy 	int j, err = 0;
523eda14cbcSMatt Macy 	zbookmark_phys_t czb;
524eda14cbcSMatt Macy 
525eda14cbcSMatt Macy 	if (object != DMU_META_DNODE_OBJECT && td->td_resume != NULL &&
526eda14cbcSMatt Macy 	    object < td->td_resume->zb_object)
527eda14cbcSMatt Macy 		return (0);
528eda14cbcSMatt Macy 
529eda14cbcSMatt Macy 	if (td->td_flags & TRAVERSE_PRE) {
530eda14cbcSMatt Macy 		SET_BOOKMARK(&czb, objset, object, ZB_DNODE_LEVEL,
531eda14cbcSMatt Macy 		    ZB_DNODE_BLKID);
532eda14cbcSMatt Macy 		err = td->td_func(td->td_spa, NULL, bp, &czb, dnp,
533eda14cbcSMatt Macy 		    td->td_arg);
534eda14cbcSMatt Macy 		if (err == TRAVERSE_VISIT_NO_CHILDREN)
535eda14cbcSMatt Macy 			return (0);
536eda14cbcSMatt Macy 		if (err != 0)
537eda14cbcSMatt Macy 			return (err);
538eda14cbcSMatt Macy 	}
539eda14cbcSMatt Macy 
540eda14cbcSMatt Macy 	for (j = 0; j < dnp->dn_nblkptr; j++) {
541eda14cbcSMatt Macy 		SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j);
542eda14cbcSMatt Macy 		err = traverse_visitbp(td, dnp, &dnp->dn_blkptr[j], &czb);
543eda14cbcSMatt Macy 		if (err != 0)
544eda14cbcSMatt Macy 			break;
545eda14cbcSMatt Macy 	}
546eda14cbcSMatt Macy 
547eda14cbcSMatt Macy 	if (err == 0 && (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) {
548eda14cbcSMatt Macy 		SET_BOOKMARK(&czb, objset, object, 0, DMU_SPILL_BLKID);
549eda14cbcSMatt Macy 		err = traverse_visitbp(td, dnp, DN_SPILL_BLKPTR(dnp), &czb);
550eda14cbcSMatt Macy 	}
551eda14cbcSMatt Macy 
552eda14cbcSMatt Macy 	if (err == 0 && (td->td_flags & TRAVERSE_POST)) {
553eda14cbcSMatt Macy 		SET_BOOKMARK(&czb, objset, object, ZB_DNODE_LEVEL,
554eda14cbcSMatt Macy 		    ZB_DNODE_BLKID);
555eda14cbcSMatt Macy 		err = td->td_func(td->td_spa, NULL, bp, &czb, dnp,
556eda14cbcSMatt Macy 		    td->td_arg);
557eda14cbcSMatt Macy 		if (err == TRAVERSE_VISIT_NO_CHILDREN)
558eda14cbcSMatt Macy 			return (0);
559eda14cbcSMatt Macy 		if (err != 0)
560eda14cbcSMatt Macy 			return (err);
561eda14cbcSMatt Macy 	}
562eda14cbcSMatt Macy 	return (err);
563eda14cbcSMatt Macy }
564eda14cbcSMatt Macy 
565eda14cbcSMatt Macy static int
566eda14cbcSMatt Macy traverse_prefetcher(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
567eda14cbcSMatt Macy     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
568eda14cbcSMatt Macy {
569e92ffd9bSMartin Matuska 	(void) zilog, (void) dnp;
570eda14cbcSMatt Macy 	prefetch_data_t *pfd = arg;
571eda14cbcSMatt Macy 	int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE;
572eda14cbcSMatt Macy 	arc_flags_t aflags = ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH |
573eda14cbcSMatt Macy 	    ARC_FLAG_PRESCIENT_PREFETCH;
574eda14cbcSMatt Macy 
575eda14cbcSMatt Macy 	ASSERT(pfd->pd_bytes_fetched >= 0);
576eda14cbcSMatt Macy 	if (zb->zb_level == ZB_DNODE_LEVEL)
577eda14cbcSMatt Macy 		return (0);
578eda14cbcSMatt Macy 	if (pfd->pd_cancel)
579eda14cbcSMatt Macy 		return (SET_ERROR(EINTR));
580eda14cbcSMatt Macy 
581eda14cbcSMatt Macy 	if (!prefetch_needed(pfd, bp))
582eda14cbcSMatt Macy 		return (0);
583eda14cbcSMatt Macy 
584eda14cbcSMatt Macy 	mutex_enter(&pfd->pd_mtx);
585eda14cbcSMatt Macy 	while (!pfd->pd_cancel && pfd->pd_bytes_fetched >= zfs_pd_bytes_max)
586eda14cbcSMatt Macy 		cv_wait_sig(&pfd->pd_cv, &pfd->pd_mtx);
587eda14cbcSMatt Macy 	pfd->pd_bytes_fetched += BP_GET_LSIZE(bp);
588eda14cbcSMatt Macy 	cv_broadcast(&pfd->pd_cv);
589eda14cbcSMatt Macy 	mutex_exit(&pfd->pd_mtx);
590eda14cbcSMatt Macy 
591eda14cbcSMatt Macy 	if ((pfd->pd_flags & TRAVERSE_NO_DECRYPT) && BP_IS_PROTECTED(bp))
592eda14cbcSMatt Macy 		zio_flags |= ZIO_FLAG_RAW;
593eda14cbcSMatt Macy 
594eda14cbcSMatt Macy 	(void) arc_read(NULL, spa, bp, NULL, NULL, ZIO_PRIORITY_ASYNC_READ,
595eda14cbcSMatt Macy 	    zio_flags, &aflags, zb);
596eda14cbcSMatt Macy 
597eda14cbcSMatt Macy 	return (0);
598eda14cbcSMatt Macy }
599eda14cbcSMatt Macy 
600eda14cbcSMatt Macy static void
601eda14cbcSMatt Macy traverse_prefetch_thread(void *arg)
602eda14cbcSMatt Macy {
603eda14cbcSMatt Macy 	traverse_data_t *td_main = arg;
604eda14cbcSMatt Macy 	traverse_data_t td = *td_main;
605eda14cbcSMatt Macy 	zbookmark_phys_t czb;
606eda14cbcSMatt Macy 	fstrans_cookie_t cookie = spl_fstrans_mark();
607eda14cbcSMatt Macy 
608eda14cbcSMatt Macy 	td.td_func = traverse_prefetcher;
609eda14cbcSMatt Macy 	td.td_arg = td_main->td_pfd;
610eda14cbcSMatt Macy 	td.td_pfd = NULL;
611eda14cbcSMatt Macy 	td.td_resume = &td_main->td_pfd->pd_resume;
612eda14cbcSMatt Macy 
613eda14cbcSMatt Macy 	SET_BOOKMARK(&czb, td.td_objset,
614eda14cbcSMatt Macy 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
615eda14cbcSMatt Macy 	(void) traverse_visitbp(&td, NULL, td.td_rootbp, &czb);
616eda14cbcSMatt Macy 
617eda14cbcSMatt Macy 	mutex_enter(&td_main->td_pfd->pd_mtx);
618eda14cbcSMatt Macy 	td_main->td_pfd->pd_exited = B_TRUE;
619eda14cbcSMatt Macy 	cv_broadcast(&td_main->td_pfd->pd_cv);
620eda14cbcSMatt Macy 	mutex_exit(&td_main->td_pfd->pd_mtx);
621eda14cbcSMatt Macy 	spl_fstrans_unmark(cookie);
622eda14cbcSMatt Macy }
623eda14cbcSMatt Macy 
624eda14cbcSMatt Macy /*
625eda14cbcSMatt Macy  * NB: dataset must not be changing on-disk (eg, is a snapshot or we are
626eda14cbcSMatt Macy  * in syncing context).
627eda14cbcSMatt Macy  */
628eda14cbcSMatt Macy static int
629eda14cbcSMatt Macy traverse_impl(spa_t *spa, dsl_dataset_t *ds, uint64_t objset, blkptr_t *rootbp,
630eda14cbcSMatt Macy     uint64_t txg_start, zbookmark_phys_t *resume, int flags,
631eda14cbcSMatt Macy     blkptr_cb_t func, void *arg)
632eda14cbcSMatt Macy {
633eda14cbcSMatt Macy 	traverse_data_t *td;
634eda14cbcSMatt Macy 	prefetch_data_t *pd;
635eda14cbcSMatt Macy 	zbookmark_phys_t *czb;
636eda14cbcSMatt Macy 	int err;
637eda14cbcSMatt Macy 
638eda14cbcSMatt Macy 	ASSERT(ds == NULL || objset == ds->ds_object);
639eda14cbcSMatt Macy 	ASSERT(!(flags & TRAVERSE_PRE) || !(flags & TRAVERSE_POST));
640eda14cbcSMatt Macy 
641eda14cbcSMatt Macy 	td = kmem_alloc(sizeof (traverse_data_t), KM_SLEEP);
642eda14cbcSMatt Macy 	pd = kmem_zalloc(sizeof (prefetch_data_t), KM_SLEEP);
643eda14cbcSMatt Macy 	czb = kmem_alloc(sizeof (zbookmark_phys_t), KM_SLEEP);
644eda14cbcSMatt Macy 
645eda14cbcSMatt Macy 	td->td_spa = spa;
646eda14cbcSMatt Macy 	td->td_objset = objset;
647eda14cbcSMatt Macy 	td->td_rootbp = rootbp;
648eda14cbcSMatt Macy 	td->td_min_txg = txg_start;
649eda14cbcSMatt Macy 	td->td_resume = resume;
650eda14cbcSMatt Macy 	td->td_func = func;
651eda14cbcSMatt Macy 	td->td_arg = arg;
652eda14cbcSMatt Macy 	td->td_pfd = pd;
653eda14cbcSMatt Macy 	td->td_flags = flags;
654eda14cbcSMatt Macy 	td->td_paused = B_FALSE;
655eda14cbcSMatt Macy 	td->td_realloc_possible = (txg_start == 0 ? B_FALSE : B_TRUE);
656eda14cbcSMatt Macy 
657eda14cbcSMatt Macy 	if (spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) {
658eda14cbcSMatt Macy 		VERIFY(spa_feature_enabled_txg(spa,
659eda14cbcSMatt Macy 		    SPA_FEATURE_HOLE_BIRTH, &td->td_hole_birth_enabled_txg));
660eda14cbcSMatt Macy 	} else {
661eda14cbcSMatt Macy 		td->td_hole_birth_enabled_txg = UINT64_MAX;
662eda14cbcSMatt Macy 	}
663eda14cbcSMatt Macy 
664eda14cbcSMatt Macy 	pd->pd_flags = flags;
665eda14cbcSMatt Macy 	if (resume != NULL)
666eda14cbcSMatt Macy 		pd->pd_resume = *resume;
667eda14cbcSMatt Macy 	mutex_init(&pd->pd_mtx, NULL, MUTEX_DEFAULT, NULL);
668eda14cbcSMatt Macy 	cv_init(&pd->pd_cv, NULL, CV_DEFAULT, NULL);
669eda14cbcSMatt Macy 
670eda14cbcSMatt Macy 	SET_BOOKMARK(czb, td->td_objset,
671eda14cbcSMatt Macy 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
672eda14cbcSMatt Macy 
673eda14cbcSMatt Macy 	/* See comment on ZIL traversal in dsl_scan_visitds. */
674eda14cbcSMatt Macy 	if (ds != NULL && !ds->ds_is_snapshot && !BP_IS_HOLE(rootbp)) {
675dbd5678dSMartin Matuska 		zio_flag_t zio_flags = ZIO_FLAG_CANFAIL;
676eda14cbcSMatt Macy 		uint32_t flags = ARC_FLAG_WAIT;
677eda14cbcSMatt Macy 		objset_phys_t *osp;
678eda14cbcSMatt Macy 		arc_buf_t *buf;
679eda14cbcSMatt Macy 		ASSERT(!BP_IS_REDACTED(rootbp));
680eda14cbcSMatt Macy 
681eda14cbcSMatt Macy 		if ((td->td_flags & TRAVERSE_NO_DECRYPT) &&
682eda14cbcSMatt Macy 		    BP_IS_PROTECTED(rootbp))
683eda14cbcSMatt Macy 			zio_flags |= ZIO_FLAG_RAW;
684eda14cbcSMatt Macy 
685eda14cbcSMatt Macy 		err = arc_read(NULL, td->td_spa, rootbp, arc_getbuf_func,
686eda14cbcSMatt Macy 		    &buf, ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, czb);
687eda14cbcSMatt Macy 		if (err != 0) {
688eda14cbcSMatt Macy 			/*
689eda14cbcSMatt Macy 			 * If both TRAVERSE_HARD and TRAVERSE_PRE are set,
690eda14cbcSMatt Macy 			 * continue to visitbp so that td_func can be called
691eda14cbcSMatt Macy 			 * in pre stage, and err will reset to zero.
692eda14cbcSMatt Macy 			 */
693eda14cbcSMatt Macy 			if (!(td->td_flags & TRAVERSE_HARD) ||
694eda14cbcSMatt Macy 			    !(td->td_flags & TRAVERSE_PRE))
695eda14cbcSMatt Macy 				goto out;
696eda14cbcSMatt Macy 		} else {
697eda14cbcSMatt Macy 			osp = buf->b_data;
698eda14cbcSMatt Macy 			traverse_zil(td, &osp->os_zil_header);
699eda14cbcSMatt Macy 			arc_buf_destroy(buf, &buf);
700eda14cbcSMatt Macy 		}
701eda14cbcSMatt Macy 	}
702eda14cbcSMatt Macy 
703eda14cbcSMatt Macy 	if (!(flags & TRAVERSE_PREFETCH_DATA) ||
704eda14cbcSMatt Macy 	    taskq_dispatch(spa->spa_prefetch_taskq, traverse_prefetch_thread,
705eda14cbcSMatt Macy 	    td, TQ_NOQUEUE) == TASKQID_INVALID)
706eda14cbcSMatt Macy 		pd->pd_exited = B_TRUE;
707eda14cbcSMatt Macy 
708eda14cbcSMatt Macy 	err = traverse_visitbp(td, NULL, rootbp, czb);
709eda14cbcSMatt Macy 
710eda14cbcSMatt Macy 	mutex_enter(&pd->pd_mtx);
711eda14cbcSMatt Macy 	pd->pd_cancel = B_TRUE;
712eda14cbcSMatt Macy 	cv_broadcast(&pd->pd_cv);
713eda14cbcSMatt Macy 	while (!pd->pd_exited)
714eda14cbcSMatt Macy 		cv_wait_sig(&pd->pd_cv, &pd->pd_mtx);
715eda14cbcSMatt Macy 	mutex_exit(&pd->pd_mtx);
716eda14cbcSMatt Macy out:
717eda14cbcSMatt Macy 	mutex_destroy(&pd->pd_mtx);
718eda14cbcSMatt Macy 	cv_destroy(&pd->pd_cv);
719eda14cbcSMatt Macy 
720eda14cbcSMatt Macy 	kmem_free(czb, sizeof (zbookmark_phys_t));
721eda14cbcSMatt Macy 	kmem_free(pd, sizeof (struct prefetch_data));
722eda14cbcSMatt Macy 	kmem_free(td, sizeof (struct traverse_data));
723eda14cbcSMatt Macy 
724eda14cbcSMatt Macy 	return (err);
725eda14cbcSMatt Macy }
726eda14cbcSMatt Macy 
727eda14cbcSMatt Macy /*
728eda14cbcSMatt Macy  * NB: dataset must not be changing on-disk (eg, is a snapshot or we are
729eda14cbcSMatt Macy  * in syncing context).
730eda14cbcSMatt Macy  */
731eda14cbcSMatt Macy int
732eda14cbcSMatt Macy traverse_dataset_resume(dsl_dataset_t *ds, uint64_t txg_start,
733eda14cbcSMatt Macy     zbookmark_phys_t *resume,
734eda14cbcSMatt Macy     int flags, blkptr_cb_t func, void *arg)
735eda14cbcSMatt Macy {
736eda14cbcSMatt Macy 	return (traverse_impl(ds->ds_dir->dd_pool->dp_spa, ds, ds->ds_object,
737eda14cbcSMatt Macy 	    &dsl_dataset_phys(ds)->ds_bp, txg_start, resume, flags, func, arg));
738eda14cbcSMatt Macy }
739eda14cbcSMatt Macy 
740eda14cbcSMatt Macy int
741eda14cbcSMatt Macy traverse_dataset(dsl_dataset_t *ds, uint64_t txg_start,
742eda14cbcSMatt Macy     int flags, blkptr_cb_t func, void *arg)
743eda14cbcSMatt Macy {
744eda14cbcSMatt Macy 	return (traverse_dataset_resume(ds, txg_start, NULL, flags, func, arg));
745eda14cbcSMatt Macy }
746eda14cbcSMatt Macy 
747eda14cbcSMatt Macy int
748eda14cbcSMatt Macy traverse_dataset_destroyed(spa_t *spa, blkptr_t *blkptr,
749eda14cbcSMatt Macy     uint64_t txg_start, zbookmark_phys_t *resume, int flags,
750eda14cbcSMatt Macy     blkptr_cb_t func, void *arg)
751eda14cbcSMatt Macy {
752eda14cbcSMatt Macy 	return (traverse_impl(spa, NULL, ZB_DESTROYED_OBJSET,
753eda14cbcSMatt Macy 	    blkptr, txg_start, resume, flags, func, arg));
754eda14cbcSMatt Macy }
755eda14cbcSMatt Macy 
756eda14cbcSMatt Macy /*
757eda14cbcSMatt Macy  * NB: pool must not be changing on-disk (eg, from zdb or sync context).
758eda14cbcSMatt Macy  */
759eda14cbcSMatt Macy int
760eda14cbcSMatt Macy traverse_pool(spa_t *spa, uint64_t txg_start, int flags,
761eda14cbcSMatt Macy     blkptr_cb_t func, void *arg)
762eda14cbcSMatt Macy {
763eda14cbcSMatt Macy 	int err;
764eda14cbcSMatt Macy 	dsl_pool_t *dp = spa_get_dsl(spa);
765eda14cbcSMatt Macy 	objset_t *mos = dp->dp_meta_objset;
766eda14cbcSMatt Macy 	boolean_t hard = (flags & TRAVERSE_HARD);
767eda14cbcSMatt Macy 
768eda14cbcSMatt Macy 	/* visit the MOS */
769eda14cbcSMatt Macy 	err = traverse_impl(spa, NULL, 0, spa_get_rootblkptr(spa),
770eda14cbcSMatt Macy 	    txg_start, NULL, flags, func, arg);
771eda14cbcSMatt Macy 	if (err != 0)
772eda14cbcSMatt Macy 		return (err);
773eda14cbcSMatt Macy 
774eda14cbcSMatt Macy 	/* visit each dataset */
775eda14cbcSMatt Macy 	for (uint64_t obj = 1; err == 0;
776eda14cbcSMatt Macy 	    err = dmu_object_next(mos, &obj, B_FALSE, txg_start)) {
777eda14cbcSMatt Macy 		dmu_object_info_t doi;
778eda14cbcSMatt Macy 
779eda14cbcSMatt Macy 		err = dmu_object_info(mos, obj, &doi);
780eda14cbcSMatt Macy 		if (err != 0) {
781eda14cbcSMatt Macy 			if (hard)
782eda14cbcSMatt Macy 				continue;
783eda14cbcSMatt Macy 			break;
784eda14cbcSMatt Macy 		}
785eda14cbcSMatt Macy 
786eda14cbcSMatt Macy 		if (doi.doi_bonus_type == DMU_OT_DSL_DATASET) {
787eda14cbcSMatt Macy 			dsl_dataset_t *ds;
788eda14cbcSMatt Macy 			uint64_t txg = txg_start;
789eda14cbcSMatt Macy 
790eda14cbcSMatt Macy 			dsl_pool_config_enter(dp, FTAG);
791eda14cbcSMatt Macy 			err = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
792eda14cbcSMatt Macy 			dsl_pool_config_exit(dp, FTAG);
793eda14cbcSMatt Macy 			if (err != 0) {
794eda14cbcSMatt Macy 				if (hard)
795eda14cbcSMatt Macy 					continue;
796eda14cbcSMatt Macy 				break;
797eda14cbcSMatt Macy 			}
798eda14cbcSMatt Macy 			if (dsl_dataset_phys(ds)->ds_prev_snap_txg > txg)
799eda14cbcSMatt Macy 				txg = dsl_dataset_phys(ds)->ds_prev_snap_txg;
800eda14cbcSMatt Macy 			err = traverse_dataset(ds, txg, flags, func, arg);
801eda14cbcSMatt Macy 			dsl_dataset_rele(ds, FTAG);
802eda14cbcSMatt Macy 			if (err != 0)
803eda14cbcSMatt Macy 				break;
804eda14cbcSMatt Macy 		}
805eda14cbcSMatt Macy 	}
806eda14cbcSMatt Macy 	if (err == ESRCH)
807eda14cbcSMatt Macy 		err = 0;
808eda14cbcSMatt Macy 	return (err);
809eda14cbcSMatt Macy }
810eda14cbcSMatt Macy 
811eda14cbcSMatt Macy EXPORT_SYMBOL(traverse_dataset);
812eda14cbcSMatt Macy EXPORT_SYMBOL(traverse_pool);
813eda14cbcSMatt Macy 
814eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs, zfs_, pd_bytes_max, INT, ZMOD_RW,
815eda14cbcSMatt Macy 	"Max number of bytes to prefetch");
816eda14cbcSMatt Macy 
817be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs, zfs_, traverse_indirect_prefetch_limit, UINT, ZMOD_RW,
81816038816SMartin Matuska 	"Traverse prefetch number of blocks pointed by indirect block");
81916038816SMartin Matuska 
820eda14cbcSMatt Macy #if defined(_KERNEL)
821eda14cbcSMatt Macy module_param_named(ignore_hole_birth, send_holes_without_birth_time, int, 0644);
822eda14cbcSMatt Macy MODULE_PARM_DESC(ignore_hole_birth,
823eda14cbcSMatt Macy 	"Alias for send_holes_without_birth_time");
824eda14cbcSMatt Macy #endif
825eda14cbcSMatt Macy 
826c03c5b1cSMartin Matuska /* CSTYLED */
827eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs, , send_holes_without_birth_time, INT, ZMOD_RW,
828eda14cbcSMatt Macy 	"Ignore hole_birth txg for zfs send");
829