xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_traverse.c (revision b24ab6762772a3f6a89393947930c7fa61306783)
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 /*
22ab69d62fSMatthew Ahrens  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
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>
3688b7b0f2SMatthew Ahrens #include <sys/callb.h>
3788b7b0f2SMatthew Ahrens 
3888b7b0f2SMatthew Ahrens struct prefetch_data {
3988b7b0f2SMatthew Ahrens 	kmutex_t pd_mtx;
4088b7b0f2SMatthew Ahrens 	kcondvar_t pd_cv;
4188b7b0f2SMatthew Ahrens 	int pd_blks_max;
4288b7b0f2SMatthew Ahrens 	int pd_blks_fetched;
4388b7b0f2SMatthew Ahrens 	int pd_flags;
4488b7b0f2SMatthew Ahrens 	boolean_t pd_cancel;
4588b7b0f2SMatthew Ahrens 	boolean_t pd_exited;
4688b7b0f2SMatthew Ahrens };
4788b7b0f2SMatthew Ahrens 
4888b7b0f2SMatthew Ahrens struct traverse_data {
4988b7b0f2SMatthew Ahrens 	spa_t *td_spa;
5088b7b0f2SMatthew Ahrens 	uint64_t td_objset;
5188b7b0f2SMatthew Ahrens 	blkptr_t *td_rootbp;
5288b7b0f2SMatthew Ahrens 	uint64_t td_min_txg;
5388b7b0f2SMatthew Ahrens 	int td_flags;
5488b7b0f2SMatthew Ahrens 	struct prefetch_data *td_pfd;
5588b7b0f2SMatthew Ahrens 	blkptr_cb_t *td_func;
5688b7b0f2SMatthew Ahrens 	void *td_arg;
5788b7b0f2SMatthew Ahrens };
58fa9e4066Sahrens 
5914843421SMatthew Ahrens static int traverse_dnode(struct traverse_data *td, const dnode_phys_t *dnp,
6014843421SMatthew Ahrens     arc_buf_t *buf, uint64_t objset, uint64_t object);
6114843421SMatthew Ahrens 
62ea8dc4b6Seschrock /* ARGSUSED */
63*b24ab676SJeff Bonwick static int
645dabedeeSbonwick traverse_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
65ea8dc4b6Seschrock {
6688b7b0f2SMatthew Ahrens 	struct traverse_data *td = arg;
6788b7b0f2SMatthew Ahrens 	zbookmark_t zb;
68ea8dc4b6Seschrock 
6988b7b0f2SMatthew Ahrens 	if (bp->blk_birth == 0)
70*b24ab676SJeff Bonwick 		return (0);
715dabedeeSbonwick 
7288b7b0f2SMatthew Ahrens 	if (claim_txg == 0 && bp->blk_birth >= spa_first_txg(td->td_spa))
73*b24ab676SJeff Bonwick 		return (0);
7488b7b0f2SMatthew Ahrens 
75*b24ab676SJeff Bonwick 	SET_BOOKMARK(&zb, td->td_objset, ZB_ZIL_OBJECT, ZB_ZIL_LEVEL,
76*b24ab676SJeff Bonwick 	    bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
77*b24ab676SJeff Bonwick 
78*b24ab676SJeff Bonwick 	(void) td->td_func(td->td_spa, zilog, bp, &zb, NULL, td->td_arg);
79*b24ab676SJeff Bonwick 
80*b24ab676SJeff Bonwick 	return (0);
81ea8dc4b6Seschrock }
82ea8dc4b6Seschrock 
83ea8dc4b6Seschrock /* ARGSUSED */
84*b24ab676SJeff Bonwick static int
855dabedeeSbonwick traverse_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg)
86ea8dc4b6Seschrock {
8788b7b0f2SMatthew Ahrens 	struct traverse_data *td = arg;
88ea8dc4b6Seschrock 
89ea8dc4b6Seschrock 	if (lrc->lrc_txtype == TX_WRITE) {
90ea8dc4b6Seschrock 		lr_write_t *lr = (lr_write_t *)lrc;
91ea8dc4b6Seschrock 		blkptr_t *bp = &lr->lr_blkptr;
9288b7b0f2SMatthew Ahrens 		zbookmark_t zb;
93ea8dc4b6Seschrock 
9488b7b0f2SMatthew Ahrens 		if (bp->blk_birth == 0)
95*b24ab676SJeff Bonwick 			return (0);
965dabedeeSbonwick 
9788b7b0f2SMatthew Ahrens 		if (claim_txg == 0 || bp->blk_birth < claim_txg)
98*b24ab676SJeff Bonwick 			return (0);
99*b24ab676SJeff Bonwick 
100*b24ab676SJeff Bonwick 		SET_BOOKMARK(&zb, td->td_objset, lr->lr_foid, ZB_ZIL_LEVEL,
101*b24ab676SJeff Bonwick 		    lr->lr_offset / BP_GET_LSIZE(bp));
10288b7b0f2SMatthew Ahrens 
103*b24ab676SJeff Bonwick 		(void) td->td_func(td->td_spa, zilog, bp, &zb, NULL,
104*b24ab676SJeff Bonwick 		    td->td_arg);
105ea8dc4b6Seschrock 	}
106*b24ab676SJeff Bonwick 	return (0);
107ea8dc4b6Seschrock }
108ea8dc4b6Seschrock 
109ea8dc4b6Seschrock static void
11088b7b0f2SMatthew Ahrens traverse_zil(struct traverse_data *td, zil_header_t *zh)
111ea8dc4b6Seschrock {
1125dabedeeSbonwick 	uint64_t claim_txg = zh->zh_claim_txg;
113ea8dc4b6Seschrock 	zilog_t *zilog;
114ea8dc4b6Seschrock 
1155dabedeeSbonwick 	/*
1165dabedeeSbonwick 	 * We only want to visit blocks that have been claimed but not yet
117*b24ab676SJeff Bonwick 	 * replayed; plus, in read-only mode, blocks that are already stable.
1185dabedeeSbonwick 	 */
1198ad4d6ddSJeff Bonwick 	if (claim_txg == 0 && spa_writeable(td->td_spa))
1205dabedeeSbonwick 		return;
1215dabedeeSbonwick 
12288b7b0f2SMatthew Ahrens 	zilog = zil_alloc(spa_get_dsl(td->td_spa)->dp_meta_objset, zh);
123ea8dc4b6Seschrock 
12488b7b0f2SMatthew Ahrens 	(void) zil_parse(zilog, traverse_zil_block, traverse_zil_record, td,
1255dabedeeSbonwick 	    claim_txg);
126ea8dc4b6Seschrock 
127ea8dc4b6Seschrock 	zil_free(zilog);
128ea8dc4b6Seschrock }
129ea8dc4b6Seschrock 
130fa9e4066Sahrens static int
13188b7b0f2SMatthew Ahrens traverse_visitbp(struct traverse_data *td, const dnode_phys_t *dnp,
13288b7b0f2SMatthew Ahrens     arc_buf_t *pbuf, blkptr_t *bp, const zbookmark_t *zb)
133fa9e4066Sahrens {
1346a0f0066SEric Taylor 	zbookmark_t czb;
13588b7b0f2SMatthew Ahrens 	int err = 0;
13688b7b0f2SMatthew Ahrens 	arc_buf_t *buf = NULL;
13788b7b0f2SMatthew Ahrens 	struct prefetch_data *pd = td->td_pfd;
138fa9e4066Sahrens 
13988b7b0f2SMatthew Ahrens 	if (bp->blk_birth == 0) {
140*b24ab676SJeff Bonwick 		err = td->td_func(td->td_spa, NULL, NULL, zb, dnp, td->td_arg);
14188b7b0f2SMatthew Ahrens 		return (err);
142fa9e4066Sahrens 	}
143fa9e4066Sahrens 
14488b7b0f2SMatthew Ahrens 	if (bp->blk_birth <= td->td_min_txg)
14588b7b0f2SMatthew Ahrens 		return (0);
146fa9e4066Sahrens 
14788b7b0f2SMatthew Ahrens 	if (pd && !pd->pd_exited &&
14888b7b0f2SMatthew Ahrens 	    ((pd->pd_flags & TRAVERSE_PREFETCH_DATA) ||
14988b7b0f2SMatthew Ahrens 	    BP_GET_TYPE(bp) == DMU_OT_DNODE || BP_GET_LEVEL(bp) > 0)) {
15088b7b0f2SMatthew Ahrens 		mutex_enter(&pd->pd_mtx);
15188b7b0f2SMatthew Ahrens 		ASSERT(pd->pd_blks_fetched >= 0);
15288b7b0f2SMatthew Ahrens 		while (pd->pd_blks_fetched == 0 && !pd->pd_exited)
15388b7b0f2SMatthew Ahrens 			cv_wait(&pd->pd_cv, &pd->pd_mtx);
15488b7b0f2SMatthew Ahrens 		pd->pd_blks_fetched--;
15588b7b0f2SMatthew Ahrens 		cv_broadcast(&pd->pd_cv);
15688b7b0f2SMatthew Ahrens 		mutex_exit(&pd->pd_mtx);
157fa9e4066Sahrens 	}
158fa9e4066Sahrens 
15988b7b0f2SMatthew Ahrens 	if (td->td_flags & TRAVERSE_PRE) {
160*b24ab676SJeff Bonwick 		err = td->td_func(td->td_spa, NULL, bp, zb, dnp, td->td_arg);
16188b7b0f2SMatthew Ahrens 		if (err)
16288b7b0f2SMatthew Ahrens 			return (err);
163fa9e4066Sahrens 	}
164fa9e4066Sahrens 
16588b7b0f2SMatthew Ahrens 	if (BP_GET_LEVEL(bp) > 0) {
16688b7b0f2SMatthew Ahrens 		uint32_t flags = ARC_WAIT;
16788b7b0f2SMatthew Ahrens 		int i;
16888b7b0f2SMatthew Ahrens 		blkptr_t *cbp;
16988b7b0f2SMatthew Ahrens 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
17088b7b0f2SMatthew Ahrens 
17188b7b0f2SMatthew Ahrens 		err = arc_read(NULL, td->td_spa, bp, pbuf,
17288b7b0f2SMatthew Ahrens 		    arc_getbuf_func, &buf,
17388b7b0f2SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
17488b7b0f2SMatthew Ahrens 		if (err)
17588b7b0f2SMatthew Ahrens 			return (err);
17688b7b0f2SMatthew Ahrens 
17788b7b0f2SMatthew Ahrens 		/* recursively visitbp() blocks below this */
17888b7b0f2SMatthew Ahrens 		cbp = buf->b_data;
17988b7b0f2SMatthew Ahrens 		for (i = 0; i < epb; i++, cbp++) {
18088b7b0f2SMatthew Ahrens 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
18188b7b0f2SMatthew Ahrens 			    zb->zb_level - 1,
18288b7b0f2SMatthew Ahrens 			    zb->zb_blkid * epb + i);
18388b7b0f2SMatthew Ahrens 			err = traverse_visitbp(td, dnp, buf, cbp, &czb);
18488b7b0f2SMatthew Ahrens 			if (err)
18588b7b0f2SMatthew Ahrens 				break;
18688b7b0f2SMatthew Ahrens 		}
18788b7b0f2SMatthew Ahrens 	} else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) {
18888b7b0f2SMatthew Ahrens 		uint32_t flags = ARC_WAIT;
18914843421SMatthew Ahrens 		int i;
19088b7b0f2SMatthew Ahrens 		int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
19188b7b0f2SMatthew Ahrens 
19288b7b0f2SMatthew Ahrens 		err = arc_read(NULL, td->td_spa, bp, pbuf,
19388b7b0f2SMatthew Ahrens 		    arc_getbuf_func, &buf,
19488b7b0f2SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
19588b7b0f2SMatthew Ahrens 		if (err)
19688b7b0f2SMatthew Ahrens 			return (err);
19788b7b0f2SMatthew Ahrens 
19888b7b0f2SMatthew Ahrens 		/* recursively visitbp() blocks below this */
19988b7b0f2SMatthew Ahrens 		dnp = buf->b_data;
20088b7b0f2SMatthew Ahrens 		for (i = 0; i < epb && err == 0; i++, dnp++) {
20114843421SMatthew Ahrens 			err = traverse_dnode(td, dnp, buf, zb->zb_objset,
20214843421SMatthew Ahrens 			    zb->zb_blkid * epb + i);
20314843421SMatthew Ahrens 			if (err)
20414843421SMatthew Ahrens 				break;
205fa9e4066Sahrens 		}
20688b7b0f2SMatthew Ahrens 	} else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
20788b7b0f2SMatthew Ahrens 		uint32_t flags = ARC_WAIT;
20888b7b0f2SMatthew Ahrens 		objset_phys_t *osp;
20914843421SMatthew Ahrens 		dnode_phys_t *dnp;
21088b7b0f2SMatthew Ahrens 
21188b7b0f2SMatthew Ahrens 		err = arc_read_nolock(NULL, td->td_spa, bp,
21288b7b0f2SMatthew Ahrens 		    arc_getbuf_func, &buf,
21388b7b0f2SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
21488b7b0f2SMatthew Ahrens 		if (err)
21588b7b0f2SMatthew Ahrens 			return (err);
21688b7b0f2SMatthew Ahrens 
21788b7b0f2SMatthew Ahrens 		osp = buf->b_data;
21888b7b0f2SMatthew Ahrens 		traverse_zil(td, &osp->os_zil_header);
219fa9e4066Sahrens 
22014843421SMatthew Ahrens 		dnp = &osp->os_meta_dnode;
221*b24ab676SJeff Bonwick 		err = traverse_dnode(td, dnp, buf, zb->zb_objset,
222*b24ab676SJeff Bonwick 		    DMU_META_DNODE_OBJECT);
22314843421SMatthew Ahrens 		if (err == 0 && arc_buf_size(buf) >= sizeof (objset_phys_t)) {
22414843421SMatthew Ahrens 			dnp = &osp->os_userused_dnode;
22514843421SMatthew Ahrens 			err = traverse_dnode(td, dnp, buf, zb->zb_objset,
22614843421SMatthew Ahrens 			    DMU_USERUSED_OBJECT);
22714843421SMatthew Ahrens 		}
22814843421SMatthew Ahrens 		if (err == 0 && arc_buf_size(buf) >= sizeof (objset_phys_t)) {
22914843421SMatthew Ahrens 			dnp = &osp->os_groupused_dnode;
23014843421SMatthew Ahrens 			err = traverse_dnode(td, dnp, buf, zb->zb_objset,
23114843421SMatthew Ahrens 			    DMU_GROUPUSED_OBJECT);
23288b7b0f2SMatthew Ahrens 		}
23388b7b0f2SMatthew Ahrens 	}
234fa9e4066Sahrens 
23588b7b0f2SMatthew Ahrens 	if (buf)
23688b7b0f2SMatthew Ahrens 		(void) arc_buf_remove_ref(buf, &buf);
237fa9e4066Sahrens 
23888b7b0f2SMatthew Ahrens 	if (err == 0 && (td->td_flags & TRAVERSE_POST))
239*b24ab676SJeff Bonwick 		err = td->td_func(td->td_spa, NULL, bp, zb, dnp, td->td_arg);
240fa9e4066Sahrens 
241fa9e4066Sahrens 	return (err);
242fa9e4066Sahrens }
243fa9e4066Sahrens 
24414843421SMatthew Ahrens static int
24514843421SMatthew Ahrens traverse_dnode(struct traverse_data *td, const dnode_phys_t *dnp,
24614843421SMatthew Ahrens     arc_buf_t *buf, uint64_t objset, uint64_t object)
24714843421SMatthew Ahrens {
24814843421SMatthew Ahrens 	int j, err = 0;
24914843421SMatthew Ahrens 	zbookmark_t czb;
25014843421SMatthew Ahrens 
25114843421SMatthew Ahrens 	for (j = 0; j < dnp->dn_nblkptr; j++) {
25214843421SMatthew Ahrens 		SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j);
25314843421SMatthew Ahrens 		err = traverse_visitbp(td, dnp, buf,
25414843421SMatthew Ahrens 		    (blkptr_t *)&dnp->dn_blkptr[j], &czb);
25514843421SMatthew Ahrens 		if (err)
25614843421SMatthew Ahrens 			break;
25714843421SMatthew Ahrens 	}
25814843421SMatthew Ahrens 	return (err);
25914843421SMatthew Ahrens }
26014843421SMatthew Ahrens 
26188b7b0f2SMatthew Ahrens /* ARGSUSED */
26288b7b0f2SMatthew Ahrens static int
263*b24ab676SJeff Bonwick traverse_prefetcher(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
264*b24ab676SJeff Bonwick     const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
265e7cbe64fSgw {
26688b7b0f2SMatthew Ahrens 	struct prefetch_data *pfd = arg;
26788b7b0f2SMatthew Ahrens 	uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
268e7cbe64fSgw 
26988b7b0f2SMatthew Ahrens 	ASSERT(pfd->pd_blks_fetched >= 0);
27088b7b0f2SMatthew Ahrens 	if (pfd->pd_cancel)
27188b7b0f2SMatthew Ahrens 		return (EINTR);
272e7cbe64fSgw 
27388b7b0f2SMatthew Ahrens 	if (bp == NULL || !((pfd->pd_flags & TRAVERSE_PREFETCH_DATA) ||
27488b7b0f2SMatthew Ahrens 	    BP_GET_TYPE(bp) == DMU_OT_DNODE || BP_GET_LEVEL(bp) > 0))
275fa9e4066Sahrens 		return (0);
276fa9e4066Sahrens 
27788b7b0f2SMatthew Ahrens 	mutex_enter(&pfd->pd_mtx);
27888b7b0f2SMatthew Ahrens 	while (!pfd->pd_cancel && pfd->pd_blks_fetched >= pfd->pd_blks_max)
27988b7b0f2SMatthew Ahrens 		cv_wait(&pfd->pd_cv, &pfd->pd_mtx);
28088b7b0f2SMatthew Ahrens 	pfd->pd_blks_fetched++;
28188b7b0f2SMatthew Ahrens 	cv_broadcast(&pfd->pd_cv);
28288b7b0f2SMatthew Ahrens 	mutex_exit(&pfd->pd_mtx);
283fa9e4066Sahrens 
28488b7b0f2SMatthew Ahrens 	(void) arc_read_nolock(NULL, spa, bp, NULL, NULL,
28588b7b0f2SMatthew Ahrens 	    ZIO_PRIORITY_ASYNC_READ,
28688b7b0f2SMatthew Ahrens 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
28788b7b0f2SMatthew Ahrens 	    &aflags, zb);
288fa9e4066Sahrens 
28988b7b0f2SMatthew Ahrens 	return (0);
290fa9e4066Sahrens }
291fa9e4066Sahrens 
292fa9e4066Sahrens static void
29388b7b0f2SMatthew Ahrens traverse_prefetch_thread(void *arg)
294fa9e4066Sahrens {
29588b7b0f2SMatthew Ahrens 	struct traverse_data *td_main = arg;
29688b7b0f2SMatthew Ahrens 	struct traverse_data td = *td_main;
29788b7b0f2SMatthew Ahrens 	zbookmark_t czb;
298fa9e4066Sahrens 
29988b7b0f2SMatthew Ahrens 	td.td_func = traverse_prefetcher;
30088b7b0f2SMatthew Ahrens 	td.td_arg = td_main->td_pfd;
30188b7b0f2SMatthew Ahrens 	td.td_pfd = NULL;
302fa9e4066Sahrens 
303*b24ab676SJeff Bonwick 	SET_BOOKMARK(&czb, td.td_objset,
304*b24ab676SJeff Bonwick 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
30588b7b0f2SMatthew Ahrens 	(void) traverse_visitbp(&td, NULL, NULL, td.td_rootbp, &czb);
306fa9e4066Sahrens 
30788b7b0f2SMatthew Ahrens 	mutex_enter(&td_main->td_pfd->pd_mtx);
30888b7b0f2SMatthew Ahrens 	td_main->td_pfd->pd_exited = B_TRUE;
30988b7b0f2SMatthew Ahrens 	cv_broadcast(&td_main->td_pfd->pd_cv);
31088b7b0f2SMatthew Ahrens 	mutex_exit(&td_main->td_pfd->pd_mtx);
311fa9e4066Sahrens }
312fa9e4066Sahrens 
31388b7b0f2SMatthew Ahrens /*
31488b7b0f2SMatthew Ahrens  * NB: dataset must not be changing on-disk (eg, is a snapshot or we are
31588b7b0f2SMatthew Ahrens  * in syncing context).
31688b7b0f2SMatthew Ahrens  */
31788b7b0f2SMatthew Ahrens static int
31888b7b0f2SMatthew Ahrens traverse_impl(spa_t *spa, uint64_t objset, blkptr_t *rootbp,
31988b7b0f2SMatthew Ahrens     uint64_t txg_start, int flags, blkptr_cb_t func, void *arg)
320fa9e4066Sahrens {
32188b7b0f2SMatthew Ahrens 	struct traverse_data td;
32288b7b0f2SMatthew Ahrens 	struct prefetch_data pd = { 0 };
32388b7b0f2SMatthew Ahrens 	zbookmark_t czb;
32488b7b0f2SMatthew Ahrens 	int err;
325fa9e4066Sahrens 
32688b7b0f2SMatthew Ahrens 	td.td_spa = spa;
32788b7b0f2SMatthew Ahrens 	td.td_objset = objset;
32888b7b0f2SMatthew Ahrens 	td.td_rootbp = rootbp;
32988b7b0f2SMatthew Ahrens 	td.td_min_txg = txg_start;
33088b7b0f2SMatthew Ahrens 	td.td_func = func;
33188b7b0f2SMatthew Ahrens 	td.td_arg = arg;
33288b7b0f2SMatthew Ahrens 	td.td_pfd = &pd;
33388b7b0f2SMatthew Ahrens 	td.td_flags = flags;
33488b7b0f2SMatthew Ahrens 
33588b7b0f2SMatthew Ahrens 	pd.pd_blks_max = 100;
33688b7b0f2SMatthew Ahrens 	pd.pd_flags = flags;
33788b7b0f2SMatthew Ahrens 	mutex_init(&pd.pd_mtx, NULL, MUTEX_DEFAULT, NULL);
33888b7b0f2SMatthew Ahrens 	cv_init(&pd.pd_cv, NULL, CV_DEFAULT, NULL);
33988b7b0f2SMatthew Ahrens 
34088b7b0f2SMatthew Ahrens 	if (!(flags & TRAVERSE_PREFETCH) ||
34188b7b0f2SMatthew Ahrens 	    0 == taskq_dispatch(system_taskq, traverse_prefetch_thread,
34288b7b0f2SMatthew Ahrens 	    &td, TQ_NOQUEUE))
34388b7b0f2SMatthew Ahrens 		pd.pd_exited = B_TRUE;
34488b7b0f2SMatthew Ahrens 
345*b24ab676SJeff Bonwick 	SET_BOOKMARK(&czb, objset,
346*b24ab676SJeff Bonwick 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
34788b7b0f2SMatthew Ahrens 	err = traverse_visitbp(&td, NULL, NULL, rootbp, &czb);
34888b7b0f2SMatthew Ahrens 
34988b7b0f2SMatthew Ahrens 	mutex_enter(&pd.pd_mtx);
35088b7b0f2SMatthew Ahrens 	pd.pd_cancel = B_TRUE;
35188b7b0f2SMatthew Ahrens 	cv_broadcast(&pd.pd_cv);
35288b7b0f2SMatthew Ahrens 	while (!pd.pd_exited)
35388b7b0f2SMatthew Ahrens 		cv_wait(&pd.pd_cv, &pd.pd_mtx);
35488b7b0f2SMatthew Ahrens 	mutex_exit(&pd.pd_mtx);
35588b7b0f2SMatthew Ahrens 
35688b7b0f2SMatthew Ahrens 	mutex_destroy(&pd.pd_mtx);
35788b7b0f2SMatthew Ahrens 	cv_destroy(&pd.pd_cv);
358fa9e4066Sahrens 
35988b7b0f2SMatthew Ahrens 	return (err);
360fa9e4066Sahrens }
361fa9e4066Sahrens 
36288b7b0f2SMatthew Ahrens /*
36388b7b0f2SMatthew Ahrens  * NB: dataset must not be changing on-disk (eg, is a snapshot or we are
36488b7b0f2SMatthew Ahrens  * in syncing context).
36588b7b0f2SMatthew Ahrens  */
36688b7b0f2SMatthew Ahrens int
36788b7b0f2SMatthew Ahrens traverse_dataset(dsl_dataset_t *ds, uint64_t txg_start, int flags,
36888b7b0f2SMatthew Ahrens     blkptr_cb_t func, void *arg)
369fa9e4066Sahrens {
37088b7b0f2SMatthew Ahrens 	return (traverse_impl(ds->ds_dir->dd_pool->dp_spa, ds->ds_object,
37188b7b0f2SMatthew Ahrens 	    &ds->ds_phys->ds_bp, txg_start, flags, func, arg));
372fa9e4066Sahrens }
373fa9e4066Sahrens 
37488b7b0f2SMatthew Ahrens /*
37588b7b0f2SMatthew Ahrens  * NB: pool must not be changing on-disk (eg, from zdb or sync context).
37688b7b0f2SMatthew Ahrens  */
37788b7b0f2SMatthew Ahrens int
378468c413aSTim Haley traverse_pool(spa_t *spa, blkptr_cb_t func, void *arg, uint64_t txg_start)
379fa9e4066Sahrens {
38088b7b0f2SMatthew Ahrens 	int err;
38188b7b0f2SMatthew Ahrens 	uint64_t obj;
38288b7b0f2SMatthew Ahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
38388b7b0f2SMatthew Ahrens 	objset_t *mos = dp->dp_meta_objset;
38488b7b0f2SMatthew Ahrens 
38588b7b0f2SMatthew Ahrens 	/* visit the MOS */
38688b7b0f2SMatthew Ahrens 	err = traverse_impl(spa, 0, spa_get_rootblkptr(spa),
387468c413aSTim Haley 	    txg_start, TRAVERSE_PRE | TRAVERSE_PREFETCH, func, arg);
38888b7b0f2SMatthew Ahrens 	if (err)
38988b7b0f2SMatthew Ahrens 		return (err);
39088b7b0f2SMatthew Ahrens 
39188b7b0f2SMatthew Ahrens 	/* visit each dataset */
392468c413aSTim Haley 	for (obj = 1; err == 0; err = dmu_object_next(mos, &obj, FALSE,
393468c413aSTim Haley 	    txg_start)) {
39488b7b0f2SMatthew Ahrens 		dmu_object_info_t doi;
39588b7b0f2SMatthew Ahrens 
39688b7b0f2SMatthew Ahrens 		err = dmu_object_info(mos, obj, &doi);
39788b7b0f2SMatthew Ahrens 		if (err)
39888b7b0f2SMatthew Ahrens 			return (err);
39988b7b0f2SMatthew Ahrens 
40088b7b0f2SMatthew Ahrens 		if (doi.doi_type == DMU_OT_DSL_DATASET) {
40188b7b0f2SMatthew Ahrens 			dsl_dataset_t *ds;
402468c413aSTim Haley 			uint64_t txg = txg_start;
403468c413aSTim Haley 
40488b7b0f2SMatthew Ahrens 			rw_enter(&dp->dp_config_rwlock, RW_READER);
40588b7b0f2SMatthew Ahrens 			err = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
40688b7b0f2SMatthew Ahrens 			rw_exit(&dp->dp_config_rwlock);
40788b7b0f2SMatthew Ahrens 			if (err)
40888b7b0f2SMatthew Ahrens 				return (err);
409468c413aSTim Haley 			if (ds->ds_phys->ds_prev_snap_txg > txg)
410468c413aSTim Haley 				txg = ds->ds_phys->ds_prev_snap_txg;
411468c413aSTim Haley 			err = traverse_dataset(ds, txg,
412468c413aSTim Haley 			    TRAVERSE_PRE | TRAVERSE_PREFETCH, func, arg);
41388b7b0f2SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
41488b7b0f2SMatthew Ahrens 			if (err)
41588b7b0f2SMatthew Ahrens 				return (err);
41688b7b0f2SMatthew Ahrens 		}
417fa9e4066Sahrens 	}
41888b7b0f2SMatthew Ahrens 	if (err == ESRCH)
41988b7b0f2SMatthew Ahrens 		err = 0;
42088b7b0f2SMatthew Ahrens 	return (err);
421fa9e4066Sahrens }
422