xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_traverse.c (revision b4709335aa83dcbfd0dba33c9be21fcabebd28e4)
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.
23ad135b5dSChristopher Siden  * Copyright (c) 2012 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>
3988b7b0f2SMatthew Ahrens 
4044f92b7dSChris Kirby int zfs_pd_blks_max = 100;
4144f92b7dSChris Kirby 
426e0cbcaaSMatthew Ahrens typedef struct prefetch_data {
4388b7b0f2SMatthew Ahrens 	kmutex_t pd_mtx;
4488b7b0f2SMatthew Ahrens 	kcondvar_t pd_cv;
4588b7b0f2SMatthew Ahrens 	int pd_blks_max;
4688b7b0f2SMatthew Ahrens 	int pd_blks_fetched;
4788b7b0f2SMatthew Ahrens 	int pd_flags;
4888b7b0f2SMatthew Ahrens 	boolean_t pd_cancel;
4988b7b0f2SMatthew Ahrens 	boolean_t pd_exited;
506e0cbcaaSMatthew Ahrens } prefetch_data_t;
5188b7b0f2SMatthew Ahrens 
526e0cbcaaSMatthew Ahrens typedef struct traverse_data {
5388b7b0f2SMatthew Ahrens 	spa_t *td_spa;
5488b7b0f2SMatthew Ahrens 	uint64_t td_objset;
5588b7b0f2SMatthew Ahrens 	blkptr_t *td_rootbp;
5688b7b0f2SMatthew Ahrens 	uint64_t td_min_txg;
57ad135b5dSChristopher Siden 	zbookmark_t *td_resume;
5888b7b0f2SMatthew Ahrens 	int td_flags;
596e0cbcaaSMatthew Ahrens 	prefetch_data_t *td_pfd;
6088b7b0f2SMatthew Ahrens 	blkptr_cb_t *td_func;
6188b7b0f2SMatthew Ahrens 	void *td_arg;
626e0cbcaaSMatthew Ahrens } traverse_data_t;
63fa9e4066Sahrens 
646e0cbcaaSMatthew Ahrens static int traverse_dnode(traverse_data_t *td, const dnode_phys_t *dnp,
6514843421SMatthew Ahrens     arc_buf_t *buf, uint64_t objset, uint64_t object);
66*b4709335SMatthew Ahrens static void prefetch_dnode_metadata(traverse_data_t *td, const dnode_phys_t *,
67*b4709335SMatthew Ahrens     arc_buf_t *buf, uint64_t objset, uint64_t object);
6814843421SMatthew Ahrens 
69b24ab676SJeff Bonwick static int
705dabedeeSbonwick traverse_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
71ea8dc4b6Seschrock {
726e0cbcaaSMatthew Ahrens 	traverse_data_t *td = arg;
7388b7b0f2SMatthew Ahrens 	zbookmark_t zb;
74ea8dc4b6Seschrock 
7588b7b0f2SMatthew Ahrens 	if (bp->blk_birth == 0)
76b24ab676SJeff Bonwick 		return (0);
775dabedeeSbonwick 
7888b7b0f2SMatthew Ahrens 	if (claim_txg == 0 && bp->blk_birth >= spa_first_txg(td->td_spa))
79b24ab676SJeff Bonwick 		return (0);
8088b7b0f2SMatthew Ahrens 
81b24ab676SJeff Bonwick 	SET_BOOKMARK(&zb, td->td_objset, ZB_ZIL_OBJECT, ZB_ZIL_LEVEL,
82b24ab676SJeff Bonwick 	    bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
83b24ab676SJeff Bonwick 
843f9d6ad7SLin Ling 	(void) td->td_func(td->td_spa, zilog, bp, NULL, &zb, NULL, td->td_arg);
85b24ab676SJeff Bonwick 
86b24ab676SJeff Bonwick 	return (0);
87ea8dc4b6Seschrock }
88ea8dc4b6Seschrock 
89b24ab676SJeff Bonwick static int
905dabedeeSbonwick traverse_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg)
91ea8dc4b6Seschrock {
926e0cbcaaSMatthew Ahrens 	traverse_data_t *td = arg;
93ea8dc4b6Seschrock 
94ea8dc4b6Seschrock 	if (lrc->lrc_txtype == TX_WRITE) {
95ea8dc4b6Seschrock 		lr_write_t *lr = (lr_write_t *)lrc;
96ea8dc4b6Seschrock 		blkptr_t *bp = &lr->lr_blkptr;
9788b7b0f2SMatthew Ahrens 		zbookmark_t zb;
98ea8dc4b6Seschrock 
9988b7b0f2SMatthew Ahrens 		if (bp->blk_birth == 0)
100b24ab676SJeff Bonwick 			return (0);
1015dabedeeSbonwick 
10288b7b0f2SMatthew Ahrens 		if (claim_txg == 0 || bp->blk_birth < claim_txg)
103b24ab676SJeff Bonwick 			return (0);
104b24ab676SJeff Bonwick 
1056e0cbcaaSMatthew Ahrens 		SET_BOOKMARK(&zb, td->td_objset, lr->lr_foid,
1066e0cbcaaSMatthew Ahrens 		    ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp));
10788b7b0f2SMatthew Ahrens 
1083f9d6ad7SLin Ling 		(void) td->td_func(td->td_spa, zilog, bp, NULL, &zb, NULL,
109b24ab676SJeff Bonwick 		    td->td_arg);
110ea8dc4b6Seschrock 	}
111b24ab676SJeff Bonwick 	return (0);
112ea8dc4b6Seschrock }
113ea8dc4b6Seschrock 
114ea8dc4b6Seschrock static void
1156e0cbcaaSMatthew Ahrens traverse_zil(traverse_data_t *td, zil_header_t *zh)
116ea8dc4b6Seschrock {
1175dabedeeSbonwick 	uint64_t claim_txg = zh->zh_claim_txg;
118ea8dc4b6Seschrock 	zilog_t *zilog;
119ea8dc4b6Seschrock 
1205dabedeeSbonwick 	/*
1215dabedeeSbonwick 	 * We only want to visit blocks that have been claimed but not yet
122b24ab676SJeff Bonwick 	 * replayed; plus, in read-only mode, blocks that are already stable.
1235dabedeeSbonwick 	 */
1248ad4d6ddSJeff Bonwick 	if (claim_txg == 0 && spa_writeable(td->td_spa))
1255dabedeeSbonwick 		return;
1265dabedeeSbonwick 
12788b7b0f2SMatthew Ahrens 	zilog = zil_alloc(spa_get_dsl(td->td_spa)->dp_meta_objset, zh);
128ea8dc4b6Seschrock 
12988b7b0f2SMatthew Ahrens 	(void) zil_parse(zilog, traverse_zil_block, traverse_zil_record, td,
1305dabedeeSbonwick 	    claim_txg);
131ea8dc4b6Seschrock 
132ea8dc4b6Seschrock 	zil_free(zilog);
133ea8dc4b6Seschrock }
134ea8dc4b6Seschrock 
135ad135b5dSChristopher Siden typedef enum resume_skip {
136ad135b5dSChristopher Siden 	RESUME_SKIP_ALL,
137ad135b5dSChristopher Siden 	RESUME_SKIP_NONE,
138ad135b5dSChristopher Siden 	RESUME_SKIP_CHILDREN
139ad135b5dSChristopher Siden } resume_skip_t;
140ad135b5dSChristopher Siden 
141ad135b5dSChristopher Siden /*
142ad135b5dSChristopher Siden  * Returns RESUME_SKIP_ALL if td indicates that we are resuming a traversal and
143ad135b5dSChristopher Siden  * the block indicated by zb does not need to be visited at all. Returns
144ad135b5dSChristopher Siden  * RESUME_SKIP_CHILDREN if we are resuming a post traversal and we reach the
145ad135b5dSChristopher Siden  * resume point. This indicates that this block should be visited but not its
146ad135b5dSChristopher Siden  * children (since they must have been visited in a previous traversal).
147ad135b5dSChristopher Siden  * Otherwise returns RESUME_SKIP_NONE.
148ad135b5dSChristopher Siden  */
149ad135b5dSChristopher Siden static resume_skip_t
150ad135b5dSChristopher Siden resume_skip_check(traverse_data_t *td, const dnode_phys_t *dnp,
151ad135b5dSChristopher Siden     const zbookmark_t *zb)
152ad135b5dSChristopher Siden {
153ad135b5dSChristopher Siden 	if (td->td_resume != NULL && !ZB_IS_ZERO(td->td_resume)) {
154ad135b5dSChristopher Siden 		/*
155ad135b5dSChristopher Siden 		 * If we already visited this bp & everything below,
156ad135b5dSChristopher Siden 		 * don't bother doing it again.
157ad135b5dSChristopher Siden 		 */
158ad135b5dSChristopher Siden 		if (zbookmark_is_before(dnp, zb, td->td_resume))
159ad135b5dSChristopher Siden 			return (RESUME_SKIP_ALL);
160ad135b5dSChristopher Siden 
161ad135b5dSChristopher Siden 		/*
162ad135b5dSChristopher Siden 		 * If we found the block we're trying to resume from, zero
163ad135b5dSChristopher Siden 		 * the bookmark out to indicate that we have resumed.
164ad135b5dSChristopher Siden 		 */
165ad135b5dSChristopher Siden 		ASSERT3U(zb->zb_object, <=, td->td_resume->zb_object);
166ad135b5dSChristopher Siden 		if (bcmp(zb, td->td_resume, sizeof (*zb)) == 0) {
167ad135b5dSChristopher Siden 			bzero(td->td_resume, sizeof (*zb));
168ad135b5dSChristopher Siden 			if (td->td_flags & TRAVERSE_POST)
169ad135b5dSChristopher Siden 				return (RESUME_SKIP_CHILDREN);
170ad135b5dSChristopher Siden 		}
171ad135b5dSChristopher Siden 	}
172ad135b5dSChristopher Siden 	return (RESUME_SKIP_NONE);
173ad135b5dSChristopher Siden }
174ad135b5dSChristopher Siden 
175ad135b5dSChristopher Siden static void
176ad135b5dSChristopher Siden traverse_pause(traverse_data_t *td, const zbookmark_t *zb)
177ad135b5dSChristopher Siden {
178ad135b5dSChristopher Siden 	ASSERT(td->td_resume != NULL);
179b420f3adSRichard Lowe 	ASSERT3U(zb->zb_level, ==, 0);
180ad135b5dSChristopher Siden 	bcopy(zb, td->td_resume, sizeof (*td->td_resume));
181ad135b5dSChristopher Siden }
182ad135b5dSChristopher Siden 
183*b4709335SMatthew Ahrens static void
184*b4709335SMatthew Ahrens traverse_prefetch_metadata(traverse_data_t *td,
185*b4709335SMatthew Ahrens     arc_buf_t *pbuf, const blkptr_t *bp, const zbookmark_t *zb)
186*b4709335SMatthew Ahrens {
187*b4709335SMatthew Ahrens 	uint32_t flags = ARC_NOWAIT | ARC_PREFETCH;
188*b4709335SMatthew Ahrens 
189*b4709335SMatthew Ahrens 	if (!(td->td_flags & TRAVERSE_PREFETCH_METADATA))
190*b4709335SMatthew Ahrens 		return;
191*b4709335SMatthew Ahrens 	/*
192*b4709335SMatthew Ahrens 	 * If we are in the process of resuming, don't prefetch, because
193*b4709335SMatthew Ahrens 	 * some children will not be needed (and in fact may have already
194*b4709335SMatthew Ahrens 	 * been freed).
195*b4709335SMatthew Ahrens 	 */
196*b4709335SMatthew Ahrens 	if (td->td_resume != NULL && !ZB_IS_ZERO(td->td_resume))
197*b4709335SMatthew Ahrens 		return;
198*b4709335SMatthew Ahrens 	if (BP_IS_HOLE(bp) || bp->blk_birth <= td->td_min_txg)
199*b4709335SMatthew Ahrens 		return;
200*b4709335SMatthew Ahrens 	if (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_DNODE)
201*b4709335SMatthew Ahrens 		return;
202*b4709335SMatthew Ahrens 
203*b4709335SMatthew Ahrens 	(void) arc_read(NULL, td->td_spa, bp,
204*b4709335SMatthew Ahrens 	    pbuf, NULL, NULL, ZIO_PRIORITY_ASYNC_READ,
205*b4709335SMatthew Ahrens 	    ZIO_FLAG_CANFAIL, &flags, zb);
206*b4709335SMatthew Ahrens }
207*b4709335SMatthew Ahrens 
208fa9e4066Sahrens static int
2096e0cbcaaSMatthew Ahrens traverse_visitbp(traverse_data_t *td, const dnode_phys_t *dnp,
210*b4709335SMatthew Ahrens     arc_buf_t *pbuf, const blkptr_t *bp, const zbookmark_t *zb)
211fa9e4066Sahrens {
2126a0f0066SEric Taylor 	zbookmark_t czb;
213cd088ea4SVictor Latushkin 	int err = 0, lasterr = 0;
21488b7b0f2SMatthew Ahrens 	arc_buf_t *buf = NULL;
2156e0cbcaaSMatthew Ahrens 	prefetch_data_t *pd = td->td_pfd;
216cd088ea4SVictor Latushkin 	boolean_t hard = td->td_flags & TRAVERSE_HARD;
217ad135b5dSChristopher Siden 	boolean_t pause = B_FALSE;
218ad135b5dSChristopher Siden 
219ad135b5dSChristopher Siden 	switch (resume_skip_check(td, dnp, zb)) {
220ad135b5dSChristopher Siden 	case RESUME_SKIP_ALL:
221ad135b5dSChristopher Siden 		return (0);
222ad135b5dSChristopher Siden 	case RESUME_SKIP_CHILDREN:
223ad135b5dSChristopher Siden 		goto post;
224ad135b5dSChristopher Siden 	case RESUME_SKIP_NONE:
225ad135b5dSChristopher Siden 		break;
226ad135b5dSChristopher Siden 	default:
227ad135b5dSChristopher Siden 		ASSERT(0);
228ad135b5dSChristopher Siden 	}
229fa9e4066Sahrens 
230ad135b5dSChristopher Siden 	if (BP_IS_HOLE(bp)) {
2313f9d6ad7SLin Ling 		err = td->td_func(td->td_spa, NULL, NULL, pbuf, zb, dnp,
2323f9d6ad7SLin Ling 		    td->td_arg);
23388b7b0f2SMatthew Ahrens 		return (err);
234fa9e4066Sahrens 	}
235fa9e4066Sahrens 
23688b7b0f2SMatthew Ahrens 	if (bp->blk_birth <= td->td_min_txg)
23788b7b0f2SMatthew Ahrens 		return (0);
238fa9e4066Sahrens 
23988b7b0f2SMatthew Ahrens 	if (pd && !pd->pd_exited &&
24088b7b0f2SMatthew Ahrens 	    ((pd->pd_flags & TRAVERSE_PREFETCH_DATA) ||
24188b7b0f2SMatthew Ahrens 	    BP_GET_TYPE(bp) == DMU_OT_DNODE || BP_GET_LEVEL(bp) > 0)) {
24288b7b0f2SMatthew Ahrens 		mutex_enter(&pd->pd_mtx);
24388b7b0f2SMatthew Ahrens 		ASSERT(pd->pd_blks_fetched >= 0);
24488b7b0f2SMatthew Ahrens 		while (pd->pd_blks_fetched == 0 && !pd->pd_exited)
24588b7b0f2SMatthew Ahrens 			cv_wait(&pd->pd_cv, &pd->pd_mtx);
24688b7b0f2SMatthew Ahrens 		pd->pd_blks_fetched--;
24788b7b0f2SMatthew Ahrens 		cv_broadcast(&pd->pd_cv);
24888b7b0f2SMatthew Ahrens 		mutex_exit(&pd->pd_mtx);
249fa9e4066Sahrens 	}
250fa9e4066Sahrens 
25188b7b0f2SMatthew Ahrens 	if (td->td_flags & TRAVERSE_PRE) {
2523f9d6ad7SLin Ling 		err = td->td_func(td->td_spa, NULL, bp, pbuf, zb, dnp,
2533f9d6ad7SLin Ling 		    td->td_arg);
25499d5e173STim Haley 		if (err == TRAVERSE_VISIT_NO_CHILDREN)
25599d5e173STim Haley 			return (0);
256ad135b5dSChristopher Siden 		if (err == ERESTART)
257ad135b5dSChristopher Siden 			pause = B_TRUE; /* handle pausing at a common point */
258ad135b5dSChristopher Siden 		if (err != 0)
259ad135b5dSChristopher Siden 			goto post;
260fa9e4066Sahrens 	}
261fa9e4066Sahrens 
26288b7b0f2SMatthew Ahrens 	if (BP_GET_LEVEL(bp) > 0) {
26388b7b0f2SMatthew Ahrens 		uint32_t flags = ARC_WAIT;
26488b7b0f2SMatthew Ahrens 		int i;
26588b7b0f2SMatthew Ahrens 		blkptr_t *cbp;
26688b7b0f2SMatthew Ahrens 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
26788b7b0f2SMatthew Ahrens 
2683f9d6ad7SLin Ling 		err = dsl_read(NULL, td->td_spa, bp, pbuf,
26988b7b0f2SMatthew Ahrens 		    arc_getbuf_func, &buf,
27088b7b0f2SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
27188b7b0f2SMatthew Ahrens 		if (err)
27288b7b0f2SMatthew Ahrens 			return (err);
273*b4709335SMatthew Ahrens 		cbp = buf->b_data;
274*b4709335SMatthew Ahrens 
275*b4709335SMatthew Ahrens 		for (i = 0; i < epb; i++) {
276*b4709335SMatthew Ahrens 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
277*b4709335SMatthew Ahrens 			    zb->zb_level - 1,
278*b4709335SMatthew Ahrens 			    zb->zb_blkid * epb + i);
279*b4709335SMatthew Ahrens 			traverse_prefetch_metadata(td, buf, &cbp[i], &czb);
280*b4709335SMatthew Ahrens 		}
28188b7b0f2SMatthew Ahrens 
28288b7b0f2SMatthew Ahrens 		/* recursively visitbp() blocks below this */
283*b4709335SMatthew Ahrens 		for (i = 0; i < epb; i++) {
28488b7b0f2SMatthew Ahrens 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
28588b7b0f2SMatthew Ahrens 			    zb->zb_level - 1,
28688b7b0f2SMatthew Ahrens 			    zb->zb_blkid * epb + i);
287*b4709335SMatthew Ahrens 			err = traverse_visitbp(td, dnp, buf, &cbp[i], &czb);
288cd088ea4SVictor Latushkin 			if (err) {
289cd088ea4SVictor Latushkin 				if (!hard)
290cd088ea4SVictor Latushkin 					break;
291cd088ea4SVictor Latushkin 				lasterr = err;
292cd088ea4SVictor Latushkin 			}
29388b7b0f2SMatthew Ahrens 		}
29488b7b0f2SMatthew Ahrens 	} else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) {
29588b7b0f2SMatthew Ahrens 		uint32_t flags = ARC_WAIT;
29614843421SMatthew Ahrens 		int i;
29788b7b0f2SMatthew Ahrens 		int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
29888b7b0f2SMatthew Ahrens 
2993f9d6ad7SLin Ling 		err = dsl_read(NULL, td->td_spa, bp, pbuf,
30088b7b0f2SMatthew Ahrens 		    arc_getbuf_func, &buf,
30188b7b0f2SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
30288b7b0f2SMatthew Ahrens 		if (err)
30388b7b0f2SMatthew Ahrens 			return (err);
304*b4709335SMatthew Ahrens 		dnp = buf->b_data;
305*b4709335SMatthew Ahrens 
306*b4709335SMatthew Ahrens 		for (i = 0; i < epb; i++) {
307*b4709335SMatthew Ahrens 			prefetch_dnode_metadata(td, &dnp[i], buf, zb->zb_objset,
308*b4709335SMatthew Ahrens 			    zb->zb_blkid * epb + i);
309*b4709335SMatthew Ahrens 		}
31088b7b0f2SMatthew Ahrens 
31188b7b0f2SMatthew Ahrens 		/* recursively visitbp() blocks below this */
312*b4709335SMatthew Ahrens 		for (i = 0; i < epb; i++) {
313*b4709335SMatthew Ahrens 			err = traverse_dnode(td, &dnp[i], buf, zb->zb_objset,
31414843421SMatthew Ahrens 			    zb->zb_blkid * epb + i);
315cd088ea4SVictor Latushkin 			if (err) {
316cd088ea4SVictor Latushkin 				if (!hard)
317cd088ea4SVictor Latushkin 					break;
318cd088ea4SVictor Latushkin 				lasterr = err;
319cd088ea4SVictor Latushkin 			}
320fa9e4066Sahrens 		}
32188b7b0f2SMatthew Ahrens 	} else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
32288b7b0f2SMatthew Ahrens 		uint32_t flags = ARC_WAIT;
32388b7b0f2SMatthew Ahrens 		objset_phys_t *osp;
32414843421SMatthew Ahrens 		dnode_phys_t *dnp;
32588b7b0f2SMatthew Ahrens 
3263f9d6ad7SLin Ling 		err = dsl_read_nolock(NULL, td->td_spa, bp,
32788b7b0f2SMatthew Ahrens 		    arc_getbuf_func, &buf,
32888b7b0f2SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
32988b7b0f2SMatthew Ahrens 		if (err)
33088b7b0f2SMatthew Ahrens 			return (err);
33188b7b0f2SMatthew Ahrens 
33288b7b0f2SMatthew Ahrens 		osp = buf->b_data;
33314843421SMatthew Ahrens 		dnp = &osp->os_meta_dnode;
334*b4709335SMatthew Ahrens 		prefetch_dnode_metadata(td, dnp, buf, zb->zb_objset,
335*b4709335SMatthew Ahrens 		    DMU_META_DNODE_OBJECT);
336*b4709335SMatthew Ahrens 		if (arc_buf_size(buf) >= sizeof (objset_phys_t)) {
337*b4709335SMatthew Ahrens 			prefetch_dnode_metadata(td, &osp->os_userused_dnode,
338*b4709335SMatthew Ahrens 			    buf, zb->zb_objset, DMU_USERUSED_OBJECT);
339*b4709335SMatthew Ahrens 			prefetch_dnode_metadata(td, &osp->os_groupused_dnode,
340*b4709335SMatthew Ahrens 			    buf, zb->zb_objset, DMU_USERUSED_OBJECT);
341*b4709335SMatthew Ahrens 		}
342*b4709335SMatthew Ahrens 
343b24ab676SJeff Bonwick 		err = traverse_dnode(td, dnp, buf, zb->zb_objset,
344b24ab676SJeff Bonwick 		    DMU_META_DNODE_OBJECT);
345cd088ea4SVictor Latushkin 		if (err && hard) {
346cd088ea4SVictor Latushkin 			lasterr = err;
347cd088ea4SVictor Latushkin 			err = 0;
348cd088ea4SVictor Latushkin 		}
34914843421SMatthew Ahrens 		if (err == 0 && arc_buf_size(buf) >= sizeof (objset_phys_t)) {
35014843421SMatthew Ahrens 			dnp = &osp->os_userused_dnode;
35114843421SMatthew Ahrens 			err = traverse_dnode(td, dnp, buf, zb->zb_objset,
35214843421SMatthew Ahrens 			    DMU_USERUSED_OBJECT);
35314843421SMatthew Ahrens 		}
354cd088ea4SVictor Latushkin 		if (err && hard) {
355cd088ea4SVictor Latushkin 			lasterr = err;
356cd088ea4SVictor Latushkin 			err = 0;
357cd088ea4SVictor Latushkin 		}
35814843421SMatthew Ahrens 		if (err == 0 && arc_buf_size(buf) >= sizeof (objset_phys_t)) {
35914843421SMatthew Ahrens 			dnp = &osp->os_groupused_dnode;
36014843421SMatthew Ahrens 			err = traverse_dnode(td, dnp, buf, zb->zb_objset,
36114843421SMatthew Ahrens 			    DMU_GROUPUSED_OBJECT);
36288b7b0f2SMatthew Ahrens 		}
36388b7b0f2SMatthew Ahrens 	}
364fa9e4066Sahrens 
36588b7b0f2SMatthew Ahrens 	if (buf)
36688b7b0f2SMatthew Ahrens 		(void) arc_buf_remove_ref(buf, &buf);
367fa9e4066Sahrens 
368ad135b5dSChristopher Siden post:
3693f9d6ad7SLin Ling 	if (err == 0 && lasterr == 0 && (td->td_flags & TRAVERSE_POST)) {
3703f9d6ad7SLin Ling 		err = td->td_func(td->td_spa, NULL, bp, pbuf, zb, dnp,
3713f9d6ad7SLin Ling 		    td->td_arg);
372ad135b5dSChristopher Siden 		if (err == ERESTART)
373ad135b5dSChristopher Siden 			pause = B_TRUE;
374ad135b5dSChristopher Siden 	}
375ad135b5dSChristopher Siden 
376ad135b5dSChristopher Siden 	if (pause && td->td_resume != NULL) {
377ad135b5dSChristopher Siden 		ASSERT3U(err, ==, ERESTART);
378ad135b5dSChristopher Siden 		ASSERT(!hard);
379ad135b5dSChristopher Siden 		traverse_pause(td, zb);
3803f9d6ad7SLin Ling 	}
381fa9e4066Sahrens 
382cd088ea4SVictor Latushkin 	return (err != 0 ? err : lasterr);
383fa9e4066Sahrens }
384fa9e4066Sahrens 
385*b4709335SMatthew Ahrens static void
386*b4709335SMatthew Ahrens prefetch_dnode_metadata(traverse_data_t *td, const dnode_phys_t *dnp,
387*b4709335SMatthew Ahrens     arc_buf_t *buf, uint64_t objset, uint64_t object)
388*b4709335SMatthew Ahrens {
389*b4709335SMatthew Ahrens 	int j;
390*b4709335SMatthew Ahrens 	zbookmark_t czb;
391*b4709335SMatthew Ahrens 
392*b4709335SMatthew Ahrens 	for (j = 0; j < dnp->dn_nblkptr; j++) {
393*b4709335SMatthew Ahrens 		SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j);
394*b4709335SMatthew Ahrens 		traverse_prefetch_metadata(td, buf, &dnp->dn_blkptr[j], &czb);
395*b4709335SMatthew Ahrens 	}
396*b4709335SMatthew Ahrens 
397*b4709335SMatthew Ahrens 	if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
398*b4709335SMatthew Ahrens 		SET_BOOKMARK(&czb, objset, object, 0, DMU_SPILL_BLKID);
399*b4709335SMatthew Ahrens 		traverse_prefetch_metadata(td, buf, &dnp->dn_spill, &czb);
400*b4709335SMatthew Ahrens 	}
401*b4709335SMatthew Ahrens }
402*b4709335SMatthew Ahrens 
40314843421SMatthew Ahrens static int
4046e0cbcaaSMatthew Ahrens traverse_dnode(traverse_data_t *td, const dnode_phys_t *dnp,
40514843421SMatthew Ahrens     arc_buf_t *buf, uint64_t objset, uint64_t object)
40614843421SMatthew Ahrens {
407cd088ea4SVictor Latushkin 	int j, err = 0, lasterr = 0;
40814843421SMatthew Ahrens 	zbookmark_t czb;
409cd088ea4SVictor Latushkin 	boolean_t hard = (td->td_flags & TRAVERSE_HARD);
41014843421SMatthew Ahrens 
41114843421SMatthew Ahrens 	for (j = 0; j < dnp->dn_nblkptr; j++) {
41214843421SMatthew Ahrens 		SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j);
413*b4709335SMatthew Ahrens 		err = traverse_visitbp(td, dnp, buf, &dnp->dn_blkptr[j], &czb);
414cd088ea4SVictor Latushkin 		if (err) {
415cd088ea4SVictor Latushkin 			if (!hard)
416cd088ea4SVictor Latushkin 				break;
417cd088ea4SVictor Latushkin 			lasterr = err;
418cd088ea4SVictor Latushkin 		}
4193f9d6ad7SLin Ling 	}
4203f9d6ad7SLin Ling 
4213f9d6ad7SLin Ling 	if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
422*b4709335SMatthew Ahrens 		SET_BOOKMARK(&czb, objset, object, 0, DMU_SPILL_BLKID);
423*b4709335SMatthew Ahrens 		err = traverse_visitbp(td, dnp, buf, &dnp->dn_spill, &czb);
4243f9d6ad7SLin Ling 		if (err) {
4253f9d6ad7SLin Ling 			if (!hard)
4263f9d6ad7SLin Ling 				return (err);
4273f9d6ad7SLin Ling 			lasterr = err;
4280a586ceaSMark Shellenbaum 		}
42914843421SMatthew Ahrens 	}
430cd088ea4SVictor Latushkin 	return (err != 0 ? err : lasterr);
43114843421SMatthew Ahrens }
43214843421SMatthew Ahrens 
43388b7b0f2SMatthew Ahrens /* ARGSUSED */
43488b7b0f2SMatthew Ahrens static int
435b24ab676SJeff Bonwick traverse_prefetcher(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
4363f9d6ad7SLin Ling     arc_buf_t *pbuf, const zbookmark_t *zb, const dnode_phys_t *dnp,
4373f9d6ad7SLin Ling     void *arg)
438e7cbe64fSgw {
4396e0cbcaaSMatthew Ahrens 	prefetch_data_t *pfd = arg;
44088b7b0f2SMatthew Ahrens 	uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
441e7cbe64fSgw 
44288b7b0f2SMatthew Ahrens 	ASSERT(pfd->pd_blks_fetched >= 0);
44388b7b0f2SMatthew Ahrens 	if (pfd->pd_cancel)
44488b7b0f2SMatthew Ahrens 		return (EINTR);
445e7cbe64fSgw 
44688b7b0f2SMatthew Ahrens 	if (bp == NULL || !((pfd->pd_flags & TRAVERSE_PREFETCH_DATA) ||
4476e1f5caaSNeil Perrin 	    BP_GET_TYPE(bp) == DMU_OT_DNODE || BP_GET_LEVEL(bp) > 0) ||
4486e1f5caaSNeil Perrin 	    BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG)
449fa9e4066Sahrens 		return (0);
450fa9e4066Sahrens 
45188b7b0f2SMatthew Ahrens 	mutex_enter(&pfd->pd_mtx);
45288b7b0f2SMatthew Ahrens 	while (!pfd->pd_cancel && pfd->pd_blks_fetched >= pfd->pd_blks_max)
45388b7b0f2SMatthew Ahrens 		cv_wait(&pfd->pd_cv, &pfd->pd_mtx);
45488b7b0f2SMatthew Ahrens 	pfd->pd_blks_fetched++;
45588b7b0f2SMatthew Ahrens 	cv_broadcast(&pfd->pd_cv);
45688b7b0f2SMatthew Ahrens 	mutex_exit(&pfd->pd_mtx);
457fa9e4066Sahrens 
4583f9d6ad7SLin Ling 	(void) dsl_read(NULL, spa, bp, pbuf, NULL, NULL,
45988b7b0f2SMatthew Ahrens 	    ZIO_PRIORITY_ASYNC_READ,
46088b7b0f2SMatthew Ahrens 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
46188b7b0f2SMatthew Ahrens 	    &aflags, zb);
462fa9e4066Sahrens 
46388b7b0f2SMatthew Ahrens 	return (0);
464fa9e4066Sahrens }
465fa9e4066Sahrens 
466fa9e4066Sahrens static void
46788b7b0f2SMatthew Ahrens traverse_prefetch_thread(void *arg)
468fa9e4066Sahrens {
4696e0cbcaaSMatthew Ahrens 	traverse_data_t *td_main = arg;
4706e0cbcaaSMatthew Ahrens 	traverse_data_t td = *td_main;
47188b7b0f2SMatthew Ahrens 	zbookmark_t czb;
472fa9e4066Sahrens 
47388b7b0f2SMatthew Ahrens 	td.td_func = traverse_prefetcher;
47488b7b0f2SMatthew Ahrens 	td.td_arg = td_main->td_pfd;
47588b7b0f2SMatthew Ahrens 	td.td_pfd = NULL;
476fa9e4066Sahrens 
477b24ab676SJeff Bonwick 	SET_BOOKMARK(&czb, td.td_objset,
478b24ab676SJeff Bonwick 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
47988b7b0f2SMatthew Ahrens 	(void) traverse_visitbp(&td, NULL, NULL, td.td_rootbp, &czb);
480fa9e4066Sahrens 
48188b7b0f2SMatthew Ahrens 	mutex_enter(&td_main->td_pfd->pd_mtx);
48288b7b0f2SMatthew Ahrens 	td_main->td_pfd->pd_exited = B_TRUE;
48388b7b0f2SMatthew Ahrens 	cv_broadcast(&td_main->td_pfd->pd_cv);
48488b7b0f2SMatthew Ahrens 	mutex_exit(&td_main->td_pfd->pd_mtx);
485fa9e4066Sahrens }
486fa9e4066Sahrens 
48788b7b0f2SMatthew Ahrens /*
48888b7b0f2SMatthew Ahrens  * NB: dataset must not be changing on-disk (eg, is a snapshot or we are
48988b7b0f2SMatthew Ahrens  * in syncing context).
49088b7b0f2SMatthew Ahrens  */
49188b7b0f2SMatthew Ahrens static int
492ad135b5dSChristopher Siden traverse_impl(spa_t *spa, dsl_dataset_t *ds, uint64_t objset, blkptr_t *rootbp,
493ad135b5dSChristopher Siden     uint64_t txg_start, zbookmark_t *resume, int flags,
494ad135b5dSChristopher Siden     blkptr_cb_t func, void *arg)
495fa9e4066Sahrens {
4966e0cbcaaSMatthew Ahrens 	traverse_data_t td;
4976e0cbcaaSMatthew Ahrens 	prefetch_data_t pd = { 0 };
49888b7b0f2SMatthew Ahrens 	zbookmark_t czb;
49988b7b0f2SMatthew Ahrens 	int err;
500fa9e4066Sahrens 
501ad135b5dSChristopher Siden 	ASSERT(ds == NULL || objset == ds->ds_object);
502ad135b5dSChristopher Siden 	ASSERT(!(flags & TRAVERSE_PRE) || !(flags & TRAVERSE_POST));
503ad135b5dSChristopher Siden 
504*b4709335SMatthew Ahrens 	/*
505*b4709335SMatthew Ahrens 	 * The data prefetching mechanism (the prefetch thread) is incompatible
506*b4709335SMatthew Ahrens 	 * with resuming from a bookmark.
507*b4709335SMatthew Ahrens 	 */
508*b4709335SMatthew Ahrens 	ASSERT(resume == NULL || !(flags & TRAVERSE_PREFETCH_DATA));
509*b4709335SMatthew Ahrens 
51088b7b0f2SMatthew Ahrens 	td.td_spa = spa;
511ad135b5dSChristopher Siden 	td.td_objset = objset;
51288b7b0f2SMatthew Ahrens 	td.td_rootbp = rootbp;
51388b7b0f2SMatthew Ahrens 	td.td_min_txg = txg_start;
514ad135b5dSChristopher Siden 	td.td_resume = resume;
51588b7b0f2SMatthew Ahrens 	td.td_func = func;
51688b7b0f2SMatthew Ahrens 	td.td_arg = arg;
51788b7b0f2SMatthew Ahrens 	td.td_pfd = &pd;
51888b7b0f2SMatthew Ahrens 	td.td_flags = flags;
51988b7b0f2SMatthew Ahrens 
52044f92b7dSChris Kirby 	pd.pd_blks_max = zfs_pd_blks_max;
52188b7b0f2SMatthew Ahrens 	pd.pd_flags = flags;
52288b7b0f2SMatthew Ahrens 	mutex_init(&pd.pd_mtx, NULL, MUTEX_DEFAULT, NULL);
52388b7b0f2SMatthew Ahrens 	cv_init(&pd.pd_cv, NULL, CV_DEFAULT, NULL);
52488b7b0f2SMatthew Ahrens 
5256e0cbcaaSMatthew Ahrens 	/* See comment on ZIL traversal in dsl_scan_visitds. */
5266e0cbcaaSMatthew Ahrens 	if (ds != NULL && !dsl_dataset_is_snapshot(ds)) {
5276e0cbcaaSMatthew Ahrens 		objset_t *os;
5286e0cbcaaSMatthew Ahrens 
5296e0cbcaaSMatthew Ahrens 		err = dmu_objset_from_ds(ds, &os);
5306e0cbcaaSMatthew Ahrens 		if (err)
5316e0cbcaaSMatthew Ahrens 			return (err);
5326e0cbcaaSMatthew Ahrens 
5336e0cbcaaSMatthew Ahrens 		traverse_zil(&td, &os->os_zil_header);
5346e0cbcaaSMatthew Ahrens 	}
5356e0cbcaaSMatthew Ahrens 
536*b4709335SMatthew Ahrens 	if (!(flags & TRAVERSE_PREFETCH_DATA) ||
53788b7b0f2SMatthew Ahrens 	    0 == taskq_dispatch(system_taskq, traverse_prefetch_thread,
53888b7b0f2SMatthew Ahrens 	    &td, TQ_NOQUEUE))
53988b7b0f2SMatthew Ahrens 		pd.pd_exited = B_TRUE;
54088b7b0f2SMatthew Ahrens 
5416e0cbcaaSMatthew Ahrens 	SET_BOOKMARK(&czb, td.td_objset,
542b24ab676SJeff Bonwick 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
54388b7b0f2SMatthew Ahrens 	err = traverse_visitbp(&td, NULL, NULL, rootbp, &czb);
54488b7b0f2SMatthew Ahrens 
54588b7b0f2SMatthew Ahrens 	mutex_enter(&pd.pd_mtx);
54688b7b0f2SMatthew Ahrens 	pd.pd_cancel = B_TRUE;
54788b7b0f2SMatthew Ahrens 	cv_broadcast(&pd.pd_cv);
54888b7b0f2SMatthew Ahrens 	while (!pd.pd_exited)
54988b7b0f2SMatthew Ahrens 		cv_wait(&pd.pd_cv, &pd.pd_mtx);
55088b7b0f2SMatthew Ahrens 	mutex_exit(&pd.pd_mtx);
55188b7b0f2SMatthew Ahrens 
55288b7b0f2SMatthew Ahrens 	mutex_destroy(&pd.pd_mtx);
55388b7b0f2SMatthew Ahrens 	cv_destroy(&pd.pd_cv);
554fa9e4066Sahrens 
55588b7b0f2SMatthew Ahrens 	return (err);
556fa9e4066Sahrens }
557fa9e4066Sahrens 
55888b7b0f2SMatthew Ahrens /*
55988b7b0f2SMatthew Ahrens  * NB: dataset must not be changing on-disk (eg, is a snapshot or we are
56088b7b0f2SMatthew Ahrens  * in syncing context).
56188b7b0f2SMatthew Ahrens  */
56288b7b0f2SMatthew Ahrens int
56388b7b0f2SMatthew Ahrens traverse_dataset(dsl_dataset_t *ds, uint64_t txg_start, int flags,
56488b7b0f2SMatthew Ahrens     blkptr_cb_t func, void *arg)
565fa9e4066Sahrens {
566ad135b5dSChristopher Siden 	return (traverse_impl(ds->ds_dir->dd_pool->dp_spa, ds, ds->ds_object,
567ad135b5dSChristopher Siden 	    &ds->ds_phys->ds_bp, txg_start, NULL, flags, func, arg));
568ad135b5dSChristopher Siden }
569ad135b5dSChristopher Siden 
570ad135b5dSChristopher Siden int
571ad135b5dSChristopher Siden traverse_dataset_destroyed(spa_t *spa, blkptr_t *blkptr,
572ad135b5dSChristopher Siden     uint64_t txg_start, zbookmark_t *resume, int flags,
573ad135b5dSChristopher Siden     blkptr_cb_t func, void *arg)
574ad135b5dSChristopher Siden {
575ad135b5dSChristopher Siden 	return (traverse_impl(spa, NULL, ZB_DESTROYED_OBJSET,
576ad135b5dSChristopher Siden 	    blkptr, txg_start, resume, flags, func, arg));
577fa9e4066Sahrens }
578fa9e4066Sahrens 
57988b7b0f2SMatthew Ahrens /*
58088b7b0f2SMatthew Ahrens  * NB: pool must not be changing on-disk (eg, from zdb or sync context).
58188b7b0f2SMatthew Ahrens  */
58288b7b0f2SMatthew Ahrens int
583bbfd46c4SJeff Bonwick traverse_pool(spa_t *spa, uint64_t txg_start, int flags,
584bbfd46c4SJeff Bonwick     blkptr_cb_t func, void *arg)
585fa9e4066Sahrens {
586cd088ea4SVictor Latushkin 	int err, lasterr = 0;
58788b7b0f2SMatthew Ahrens 	uint64_t obj;
58888b7b0f2SMatthew Ahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
58988b7b0f2SMatthew Ahrens 	objset_t *mos = dp->dp_meta_objset;
590cd088ea4SVictor Latushkin 	boolean_t hard = (flags & TRAVERSE_HARD);
59188b7b0f2SMatthew Ahrens 
59288b7b0f2SMatthew Ahrens 	/* visit the MOS */
593ad135b5dSChristopher Siden 	err = traverse_impl(spa, NULL, 0, spa_get_rootblkptr(spa),
594ad135b5dSChristopher Siden 	    txg_start, NULL, flags, func, arg);
59588b7b0f2SMatthew Ahrens 	if (err)
59688b7b0f2SMatthew Ahrens 		return (err);
59788b7b0f2SMatthew Ahrens 
59888b7b0f2SMatthew Ahrens 	/* visit each dataset */
599cd088ea4SVictor Latushkin 	for (obj = 1; err == 0 || (err != ESRCH && hard);
600cd088ea4SVictor Latushkin 	    err = dmu_object_next(mos, &obj, FALSE, txg_start)) {
60188b7b0f2SMatthew Ahrens 		dmu_object_info_t doi;
60288b7b0f2SMatthew Ahrens 
60388b7b0f2SMatthew Ahrens 		err = dmu_object_info(mos, obj, &doi);
604cd088ea4SVictor Latushkin 		if (err) {
605cd088ea4SVictor Latushkin 			if (!hard)
606cd088ea4SVictor Latushkin 				return (err);
607cd088ea4SVictor Latushkin 			lasterr = err;
608cd088ea4SVictor Latushkin 			continue;
609cd088ea4SVictor Latushkin 		}
61088b7b0f2SMatthew Ahrens 
61188b7b0f2SMatthew Ahrens 		if (doi.doi_type == DMU_OT_DSL_DATASET) {
61288b7b0f2SMatthew Ahrens 			dsl_dataset_t *ds;
613468c413aSTim Haley 			uint64_t txg = txg_start;
614468c413aSTim Haley 
61588b7b0f2SMatthew Ahrens 			rw_enter(&dp->dp_config_rwlock, RW_READER);
61688b7b0f2SMatthew Ahrens 			err = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
61788b7b0f2SMatthew Ahrens 			rw_exit(&dp->dp_config_rwlock);
618cd088ea4SVictor Latushkin 			if (err) {
619cd088ea4SVictor Latushkin 				if (!hard)
620cd088ea4SVictor Latushkin 					return (err);
621cd088ea4SVictor Latushkin 				lasterr = err;
622cd088ea4SVictor Latushkin 				continue;
623cd088ea4SVictor Latushkin 			}
624468c413aSTim Haley 			if (ds->ds_phys->ds_prev_snap_txg > txg)
625468c413aSTim Haley 				txg = ds->ds_phys->ds_prev_snap_txg;
626bbfd46c4SJeff Bonwick 			err = traverse_dataset(ds, txg, flags, func, arg);
62788b7b0f2SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
628cd088ea4SVictor Latushkin 			if (err) {
629cd088ea4SVictor Latushkin 				if (!hard)
630cd088ea4SVictor Latushkin 					return (err);
631cd088ea4SVictor Latushkin 				lasterr = err;
632cd088ea4SVictor Latushkin 			}
63388b7b0f2SMatthew Ahrens 		}
634fa9e4066Sahrens 	}
63588b7b0f2SMatthew Ahrens 	if (err == ESRCH)
63688b7b0f2SMatthew Ahrens 		err = 0;
637cd088ea4SVictor Latushkin 	return (err != 0 ? err : lasterr);
638fa9e4066Sahrens }
639