xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_traverse.c (revision 99d5e173470cf967aa87653364ed614299e7b511)
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.
23fa9e4066Sahrens  */
24fa9e4066Sahrens 
25fa9e4066Sahrens #include <sys/zfs_context.h>
26fa9e4066Sahrens #include <sys/dmu_objset.h>
27fa9e4066Sahrens #include <sys/dmu_traverse.h>
28fa9e4066Sahrens #include <sys/dsl_dataset.h>
29fa9e4066Sahrens #include <sys/dsl_dir.h>
30fa9e4066Sahrens #include <sys/dsl_pool.h>
31fa9e4066Sahrens #include <sys/dnode.h>
32fa9e4066Sahrens #include <sys/spa.h>
33fa9e4066Sahrens #include <sys/zio.h>
34fa9e4066Sahrens #include <sys/dmu_impl.h>
350a586ceaSMark Shellenbaum #include <sys/sa.h>
360a586ceaSMark Shellenbaum #include <sys/sa_impl.h>
3788b7b0f2SMatthew Ahrens #include <sys/callb.h>
3888b7b0f2SMatthew Ahrens 
3944f92b7dSChris Kirby int zfs_pd_blks_max = 100;
4044f92b7dSChris Kirby 
416e0cbcaaSMatthew Ahrens typedef struct prefetch_data {
4288b7b0f2SMatthew Ahrens 	kmutex_t pd_mtx;
4388b7b0f2SMatthew Ahrens 	kcondvar_t pd_cv;
4488b7b0f2SMatthew Ahrens 	int pd_blks_max;
4588b7b0f2SMatthew Ahrens 	int pd_blks_fetched;
4688b7b0f2SMatthew Ahrens 	int pd_flags;
4788b7b0f2SMatthew Ahrens 	boolean_t pd_cancel;
4888b7b0f2SMatthew Ahrens 	boolean_t pd_exited;
496e0cbcaaSMatthew Ahrens } prefetch_data_t;
5088b7b0f2SMatthew Ahrens 
516e0cbcaaSMatthew Ahrens typedef struct traverse_data {
5288b7b0f2SMatthew Ahrens 	spa_t *td_spa;
5388b7b0f2SMatthew Ahrens 	uint64_t td_objset;
5488b7b0f2SMatthew Ahrens 	blkptr_t *td_rootbp;
5588b7b0f2SMatthew Ahrens 	uint64_t td_min_txg;
5688b7b0f2SMatthew Ahrens 	int td_flags;
576e0cbcaaSMatthew Ahrens 	prefetch_data_t *td_pfd;
5888b7b0f2SMatthew Ahrens 	blkptr_cb_t *td_func;
5988b7b0f2SMatthew Ahrens 	void *td_arg;
606e0cbcaaSMatthew Ahrens } traverse_data_t;
61fa9e4066Sahrens 
626e0cbcaaSMatthew Ahrens static int traverse_dnode(traverse_data_t *td, const dnode_phys_t *dnp,
6314843421SMatthew Ahrens     arc_buf_t *buf, uint64_t objset, uint64_t object);
6414843421SMatthew Ahrens 
65b24ab676SJeff Bonwick static int
665dabedeeSbonwick traverse_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
67ea8dc4b6Seschrock {
686e0cbcaaSMatthew Ahrens 	traverse_data_t *td = arg;
6988b7b0f2SMatthew Ahrens 	zbookmark_t zb;
70ea8dc4b6Seschrock 
7188b7b0f2SMatthew Ahrens 	if (bp->blk_birth == 0)
72b24ab676SJeff Bonwick 		return (0);
735dabedeeSbonwick 
7488b7b0f2SMatthew Ahrens 	if (claim_txg == 0 && bp->blk_birth >= spa_first_txg(td->td_spa))
75b24ab676SJeff Bonwick 		return (0);
7688b7b0f2SMatthew Ahrens 
77b24ab676SJeff Bonwick 	SET_BOOKMARK(&zb, td->td_objset, ZB_ZIL_OBJECT, ZB_ZIL_LEVEL,
78b24ab676SJeff Bonwick 	    bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
79b24ab676SJeff Bonwick 
803f9d6ad7SLin Ling 	(void) td->td_func(td->td_spa, zilog, bp, NULL, &zb, NULL, td->td_arg);
81b24ab676SJeff Bonwick 
82b24ab676SJeff Bonwick 	return (0);
83ea8dc4b6Seschrock }
84ea8dc4b6Seschrock 
85b24ab676SJeff Bonwick static int
865dabedeeSbonwick traverse_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg)
87ea8dc4b6Seschrock {
886e0cbcaaSMatthew Ahrens 	traverse_data_t *td = arg;
89ea8dc4b6Seschrock 
90ea8dc4b6Seschrock 	if (lrc->lrc_txtype == TX_WRITE) {
91ea8dc4b6Seschrock 		lr_write_t *lr = (lr_write_t *)lrc;
92ea8dc4b6Seschrock 		blkptr_t *bp = &lr->lr_blkptr;
9388b7b0f2SMatthew Ahrens 		zbookmark_t zb;
94ea8dc4b6Seschrock 
9588b7b0f2SMatthew Ahrens 		if (bp->blk_birth == 0)
96b24ab676SJeff Bonwick 			return (0);
975dabedeeSbonwick 
9888b7b0f2SMatthew Ahrens 		if (claim_txg == 0 || bp->blk_birth < claim_txg)
99b24ab676SJeff Bonwick 			return (0);
100b24ab676SJeff Bonwick 
1016e0cbcaaSMatthew Ahrens 		SET_BOOKMARK(&zb, td->td_objset, lr->lr_foid,
1026e0cbcaaSMatthew Ahrens 		    ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp));
10388b7b0f2SMatthew Ahrens 
1043f9d6ad7SLin Ling 		(void) td->td_func(td->td_spa, zilog, bp, NULL, &zb, NULL,
105b24ab676SJeff Bonwick 		    td->td_arg);
106ea8dc4b6Seschrock 	}
107b24ab676SJeff Bonwick 	return (0);
108ea8dc4b6Seschrock }
109ea8dc4b6Seschrock 
110ea8dc4b6Seschrock static void
1116e0cbcaaSMatthew Ahrens traverse_zil(traverse_data_t *td, zil_header_t *zh)
112ea8dc4b6Seschrock {
1135dabedeeSbonwick 	uint64_t claim_txg = zh->zh_claim_txg;
114ea8dc4b6Seschrock 	zilog_t *zilog;
115ea8dc4b6Seschrock 
1165dabedeeSbonwick 	/*
1175dabedeeSbonwick 	 * We only want to visit blocks that have been claimed but not yet
118b24ab676SJeff Bonwick 	 * replayed; plus, in read-only mode, blocks that are already stable.
1195dabedeeSbonwick 	 */
1208ad4d6ddSJeff Bonwick 	if (claim_txg == 0 && spa_writeable(td->td_spa))
1215dabedeeSbonwick 		return;
1225dabedeeSbonwick 
12388b7b0f2SMatthew Ahrens 	zilog = zil_alloc(spa_get_dsl(td->td_spa)->dp_meta_objset, zh);
124ea8dc4b6Seschrock 
12588b7b0f2SMatthew Ahrens 	(void) zil_parse(zilog, traverse_zil_block, traverse_zil_record, td,
1265dabedeeSbonwick 	    claim_txg);
127ea8dc4b6Seschrock 
128ea8dc4b6Seschrock 	zil_free(zilog);
129ea8dc4b6Seschrock }
130ea8dc4b6Seschrock 
131fa9e4066Sahrens static int
1326e0cbcaaSMatthew Ahrens traverse_visitbp(traverse_data_t *td, const dnode_phys_t *dnp,
13388b7b0f2SMatthew Ahrens     arc_buf_t *pbuf, blkptr_t *bp, const zbookmark_t *zb)
134fa9e4066Sahrens {
1356a0f0066SEric Taylor 	zbookmark_t czb;
136cd088ea4SVictor Latushkin 	int err = 0, lasterr = 0;
13788b7b0f2SMatthew Ahrens 	arc_buf_t *buf = NULL;
1386e0cbcaaSMatthew Ahrens 	prefetch_data_t *pd = td->td_pfd;
139cd088ea4SVictor Latushkin 	boolean_t hard = td->td_flags & TRAVERSE_HARD;
140fa9e4066Sahrens 
14188b7b0f2SMatthew Ahrens 	if (bp->blk_birth == 0) {
1423f9d6ad7SLin Ling 		err = td->td_func(td->td_spa, NULL, NULL, pbuf, zb, dnp,
1433f9d6ad7SLin Ling 		    td->td_arg);
14488b7b0f2SMatthew Ahrens 		return (err);
145fa9e4066Sahrens 	}
146fa9e4066Sahrens 
14788b7b0f2SMatthew Ahrens 	if (bp->blk_birth <= td->td_min_txg)
14888b7b0f2SMatthew Ahrens 		return (0);
149fa9e4066Sahrens 
15088b7b0f2SMatthew Ahrens 	if (pd && !pd->pd_exited &&
15188b7b0f2SMatthew Ahrens 	    ((pd->pd_flags & TRAVERSE_PREFETCH_DATA) ||
15288b7b0f2SMatthew Ahrens 	    BP_GET_TYPE(bp) == DMU_OT_DNODE || BP_GET_LEVEL(bp) > 0)) {
15388b7b0f2SMatthew Ahrens 		mutex_enter(&pd->pd_mtx);
15488b7b0f2SMatthew Ahrens 		ASSERT(pd->pd_blks_fetched >= 0);
15588b7b0f2SMatthew Ahrens 		while (pd->pd_blks_fetched == 0 && !pd->pd_exited)
15688b7b0f2SMatthew Ahrens 			cv_wait(&pd->pd_cv, &pd->pd_mtx);
15788b7b0f2SMatthew Ahrens 		pd->pd_blks_fetched--;
15888b7b0f2SMatthew Ahrens 		cv_broadcast(&pd->pd_cv);
15988b7b0f2SMatthew Ahrens 		mutex_exit(&pd->pd_mtx);
160fa9e4066Sahrens 	}
161fa9e4066Sahrens 
16288b7b0f2SMatthew Ahrens 	if (td->td_flags & TRAVERSE_PRE) {
1633f9d6ad7SLin Ling 		err = td->td_func(td->td_spa, NULL, bp, pbuf, zb, dnp,
1643f9d6ad7SLin Ling 		    td->td_arg);
165*99d5e173STim Haley 		if (err == TRAVERSE_VISIT_NO_CHILDREN)
166*99d5e173STim Haley 			return (0);
16788b7b0f2SMatthew Ahrens 		if (err)
16888b7b0f2SMatthew Ahrens 			return (err);
169fa9e4066Sahrens 	}
170fa9e4066Sahrens 
17188b7b0f2SMatthew Ahrens 	if (BP_GET_LEVEL(bp) > 0) {
17288b7b0f2SMatthew Ahrens 		uint32_t flags = ARC_WAIT;
17388b7b0f2SMatthew Ahrens 		int i;
17488b7b0f2SMatthew Ahrens 		blkptr_t *cbp;
17588b7b0f2SMatthew Ahrens 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
17688b7b0f2SMatthew Ahrens 
1773f9d6ad7SLin Ling 		err = dsl_read(NULL, td->td_spa, bp, pbuf,
17888b7b0f2SMatthew Ahrens 		    arc_getbuf_func, &buf,
17988b7b0f2SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
18088b7b0f2SMatthew Ahrens 		if (err)
18188b7b0f2SMatthew Ahrens 			return (err);
18288b7b0f2SMatthew Ahrens 
18388b7b0f2SMatthew Ahrens 		/* recursively visitbp() blocks below this */
18488b7b0f2SMatthew Ahrens 		cbp = buf->b_data;
18588b7b0f2SMatthew Ahrens 		for (i = 0; i < epb; i++, cbp++) {
18688b7b0f2SMatthew Ahrens 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
18788b7b0f2SMatthew Ahrens 			    zb->zb_level - 1,
18888b7b0f2SMatthew Ahrens 			    zb->zb_blkid * epb + i);
18988b7b0f2SMatthew Ahrens 			err = traverse_visitbp(td, dnp, buf, cbp, &czb);
190cd088ea4SVictor Latushkin 			if (err) {
191cd088ea4SVictor Latushkin 				if (!hard)
192cd088ea4SVictor Latushkin 					break;
193cd088ea4SVictor Latushkin 				lasterr = err;
194cd088ea4SVictor Latushkin 			}
19588b7b0f2SMatthew Ahrens 		}
19688b7b0f2SMatthew Ahrens 	} else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) {
19788b7b0f2SMatthew Ahrens 		uint32_t flags = ARC_WAIT;
19814843421SMatthew Ahrens 		int i;
19988b7b0f2SMatthew Ahrens 		int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
20088b7b0f2SMatthew Ahrens 
2013f9d6ad7SLin Ling 		err = dsl_read(NULL, td->td_spa, bp, pbuf,
20288b7b0f2SMatthew Ahrens 		    arc_getbuf_func, &buf,
20388b7b0f2SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
20488b7b0f2SMatthew Ahrens 		if (err)
20588b7b0f2SMatthew Ahrens 			return (err);
20688b7b0f2SMatthew Ahrens 
20788b7b0f2SMatthew Ahrens 		/* recursively visitbp() blocks below this */
20888b7b0f2SMatthew Ahrens 		dnp = buf->b_data;
209cd088ea4SVictor Latushkin 		for (i = 0; i < epb; i++, dnp++) {
21014843421SMatthew Ahrens 			err = traverse_dnode(td, dnp, buf, zb->zb_objset,
21114843421SMatthew Ahrens 			    zb->zb_blkid * epb + i);
212cd088ea4SVictor Latushkin 			if (err) {
213cd088ea4SVictor Latushkin 				if (!hard)
214cd088ea4SVictor Latushkin 					break;
215cd088ea4SVictor Latushkin 				lasterr = err;
216cd088ea4SVictor Latushkin 			}
217fa9e4066Sahrens 		}
21888b7b0f2SMatthew Ahrens 	} else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
21988b7b0f2SMatthew Ahrens 		uint32_t flags = ARC_WAIT;
22088b7b0f2SMatthew Ahrens 		objset_phys_t *osp;
22114843421SMatthew Ahrens 		dnode_phys_t *dnp;
22288b7b0f2SMatthew Ahrens 
2233f9d6ad7SLin Ling 		err = dsl_read_nolock(NULL, td->td_spa, bp,
22488b7b0f2SMatthew Ahrens 		    arc_getbuf_func, &buf,
22588b7b0f2SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
22688b7b0f2SMatthew Ahrens 		if (err)
22788b7b0f2SMatthew Ahrens 			return (err);
22888b7b0f2SMatthew Ahrens 
22988b7b0f2SMatthew Ahrens 		osp = buf->b_data;
23014843421SMatthew Ahrens 		dnp = &osp->os_meta_dnode;
231b24ab676SJeff Bonwick 		err = traverse_dnode(td, dnp, buf, zb->zb_objset,
232b24ab676SJeff Bonwick 		    DMU_META_DNODE_OBJECT);
233cd088ea4SVictor Latushkin 		if (err && hard) {
234cd088ea4SVictor Latushkin 			lasterr = err;
235cd088ea4SVictor Latushkin 			err = 0;
236cd088ea4SVictor Latushkin 		}
23714843421SMatthew Ahrens 		if (err == 0 && arc_buf_size(buf) >= sizeof (objset_phys_t)) {
23814843421SMatthew Ahrens 			dnp = &osp->os_userused_dnode;
23914843421SMatthew Ahrens 			err = traverse_dnode(td, dnp, buf, zb->zb_objset,
24014843421SMatthew Ahrens 			    DMU_USERUSED_OBJECT);
24114843421SMatthew Ahrens 		}
242cd088ea4SVictor Latushkin 		if (err && hard) {
243cd088ea4SVictor Latushkin 			lasterr = err;
244cd088ea4SVictor Latushkin 			err = 0;
245cd088ea4SVictor Latushkin 		}
24614843421SMatthew Ahrens 		if (err == 0 && arc_buf_size(buf) >= sizeof (objset_phys_t)) {
24714843421SMatthew Ahrens 			dnp = &osp->os_groupused_dnode;
24814843421SMatthew Ahrens 			err = traverse_dnode(td, dnp, buf, zb->zb_objset,
24914843421SMatthew Ahrens 			    DMU_GROUPUSED_OBJECT);
25088b7b0f2SMatthew Ahrens 		}
25188b7b0f2SMatthew Ahrens 	}
252fa9e4066Sahrens 
25388b7b0f2SMatthew Ahrens 	if (buf)
25488b7b0f2SMatthew Ahrens 		(void) arc_buf_remove_ref(buf, &buf);
255fa9e4066Sahrens 
2563f9d6ad7SLin Ling 	if (err == 0 && lasterr == 0 && (td->td_flags & TRAVERSE_POST)) {
2573f9d6ad7SLin Ling 		err = td->td_func(td->td_spa, NULL, bp, pbuf, zb, dnp,
2583f9d6ad7SLin Ling 		    td->td_arg);
2593f9d6ad7SLin Ling 	}
260fa9e4066Sahrens 
261cd088ea4SVictor Latushkin 	return (err != 0 ? err : lasterr);
262fa9e4066Sahrens }
263fa9e4066Sahrens 
26414843421SMatthew Ahrens static int
2656e0cbcaaSMatthew Ahrens traverse_dnode(traverse_data_t *td, const dnode_phys_t *dnp,
26614843421SMatthew Ahrens     arc_buf_t *buf, uint64_t objset, uint64_t object)
26714843421SMatthew Ahrens {
268cd088ea4SVictor Latushkin 	int j, err = 0, lasterr = 0;
26914843421SMatthew Ahrens 	zbookmark_t czb;
270cd088ea4SVictor Latushkin 	boolean_t hard = (td->td_flags & TRAVERSE_HARD);
27114843421SMatthew Ahrens 
27214843421SMatthew Ahrens 	for (j = 0; j < dnp->dn_nblkptr; j++) {
27314843421SMatthew Ahrens 		SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j);
27414843421SMatthew Ahrens 		err = traverse_visitbp(td, dnp, buf,
27514843421SMatthew Ahrens 		    (blkptr_t *)&dnp->dn_blkptr[j], &czb);
276cd088ea4SVictor Latushkin 		if (err) {
277cd088ea4SVictor Latushkin 			if (!hard)
278cd088ea4SVictor Latushkin 				break;
279cd088ea4SVictor Latushkin 			lasterr = err;
280cd088ea4SVictor Latushkin 		}
2813f9d6ad7SLin Ling 	}
2823f9d6ad7SLin Ling 
2833f9d6ad7SLin Ling 	if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
2843f9d6ad7SLin Ling 		SET_BOOKMARK(&czb, objset,
2853f9d6ad7SLin Ling 		    object, 0, DMU_SPILL_BLKID);
2863f9d6ad7SLin Ling 		err = traverse_visitbp(td, dnp, buf,
2873f9d6ad7SLin Ling 		    (blkptr_t *)&dnp->dn_spill, &czb);
2883f9d6ad7SLin Ling 		if (err) {
2893f9d6ad7SLin Ling 			if (!hard)
2903f9d6ad7SLin Ling 				return (err);
2913f9d6ad7SLin Ling 			lasterr = err;
2920a586ceaSMark Shellenbaum 		}
29314843421SMatthew Ahrens 	}
294cd088ea4SVictor Latushkin 	return (err != 0 ? err : lasterr);
29514843421SMatthew Ahrens }
29614843421SMatthew Ahrens 
29788b7b0f2SMatthew Ahrens /* ARGSUSED */
29888b7b0f2SMatthew Ahrens static int
299b24ab676SJeff Bonwick traverse_prefetcher(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
3003f9d6ad7SLin Ling     arc_buf_t *pbuf, const zbookmark_t *zb, const dnode_phys_t *dnp,
3013f9d6ad7SLin Ling     void *arg)
302e7cbe64fSgw {
3036e0cbcaaSMatthew Ahrens 	prefetch_data_t *pfd = arg;
30488b7b0f2SMatthew Ahrens 	uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
305e7cbe64fSgw 
30688b7b0f2SMatthew Ahrens 	ASSERT(pfd->pd_blks_fetched >= 0);
30788b7b0f2SMatthew Ahrens 	if (pfd->pd_cancel)
30888b7b0f2SMatthew Ahrens 		return (EINTR);
309e7cbe64fSgw 
31088b7b0f2SMatthew Ahrens 	if (bp == NULL || !((pfd->pd_flags & TRAVERSE_PREFETCH_DATA) ||
3116e1f5caaSNeil Perrin 	    BP_GET_TYPE(bp) == DMU_OT_DNODE || BP_GET_LEVEL(bp) > 0) ||
3126e1f5caaSNeil Perrin 	    BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG)
313fa9e4066Sahrens 		return (0);
314fa9e4066Sahrens 
31588b7b0f2SMatthew Ahrens 	mutex_enter(&pfd->pd_mtx);
31688b7b0f2SMatthew Ahrens 	while (!pfd->pd_cancel && pfd->pd_blks_fetched >= pfd->pd_blks_max)
31788b7b0f2SMatthew Ahrens 		cv_wait(&pfd->pd_cv, &pfd->pd_mtx);
31888b7b0f2SMatthew Ahrens 	pfd->pd_blks_fetched++;
31988b7b0f2SMatthew Ahrens 	cv_broadcast(&pfd->pd_cv);
32088b7b0f2SMatthew Ahrens 	mutex_exit(&pfd->pd_mtx);
321fa9e4066Sahrens 
3223f9d6ad7SLin Ling 	(void) dsl_read(NULL, spa, bp, pbuf, NULL, NULL,
32388b7b0f2SMatthew Ahrens 	    ZIO_PRIORITY_ASYNC_READ,
32488b7b0f2SMatthew Ahrens 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
32588b7b0f2SMatthew Ahrens 	    &aflags, zb);
326fa9e4066Sahrens 
32788b7b0f2SMatthew Ahrens 	return (0);
328fa9e4066Sahrens }
329fa9e4066Sahrens 
330fa9e4066Sahrens static void
33188b7b0f2SMatthew Ahrens traverse_prefetch_thread(void *arg)
332fa9e4066Sahrens {
3336e0cbcaaSMatthew Ahrens 	traverse_data_t *td_main = arg;
3346e0cbcaaSMatthew Ahrens 	traverse_data_t td = *td_main;
33588b7b0f2SMatthew Ahrens 	zbookmark_t czb;
336fa9e4066Sahrens 
33788b7b0f2SMatthew Ahrens 	td.td_func = traverse_prefetcher;
33888b7b0f2SMatthew Ahrens 	td.td_arg = td_main->td_pfd;
33988b7b0f2SMatthew Ahrens 	td.td_pfd = NULL;
340fa9e4066Sahrens 
341b24ab676SJeff Bonwick 	SET_BOOKMARK(&czb, td.td_objset,
342b24ab676SJeff Bonwick 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
34388b7b0f2SMatthew Ahrens 	(void) traverse_visitbp(&td, NULL, NULL, td.td_rootbp, &czb);
344fa9e4066Sahrens 
34588b7b0f2SMatthew Ahrens 	mutex_enter(&td_main->td_pfd->pd_mtx);
34688b7b0f2SMatthew Ahrens 	td_main->td_pfd->pd_exited = B_TRUE;
34788b7b0f2SMatthew Ahrens 	cv_broadcast(&td_main->td_pfd->pd_cv);
34888b7b0f2SMatthew Ahrens 	mutex_exit(&td_main->td_pfd->pd_mtx);
349fa9e4066Sahrens }
350fa9e4066Sahrens 
35188b7b0f2SMatthew Ahrens /*
35288b7b0f2SMatthew Ahrens  * NB: dataset must not be changing on-disk (eg, is a snapshot or we are
35388b7b0f2SMatthew Ahrens  * in syncing context).
35488b7b0f2SMatthew Ahrens  */
35588b7b0f2SMatthew Ahrens static int
3566e0cbcaaSMatthew Ahrens traverse_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *rootbp,
35788b7b0f2SMatthew Ahrens     uint64_t txg_start, int flags, blkptr_cb_t func, void *arg)
358fa9e4066Sahrens {
3596e0cbcaaSMatthew Ahrens 	traverse_data_t td;
3606e0cbcaaSMatthew Ahrens 	prefetch_data_t pd = { 0 };
36188b7b0f2SMatthew Ahrens 	zbookmark_t czb;
36288b7b0f2SMatthew Ahrens 	int err;
363fa9e4066Sahrens 
36488b7b0f2SMatthew Ahrens 	td.td_spa = spa;
3656e0cbcaaSMatthew Ahrens 	td.td_objset = ds ? ds->ds_object : 0;
36688b7b0f2SMatthew Ahrens 	td.td_rootbp = rootbp;
36788b7b0f2SMatthew Ahrens 	td.td_min_txg = txg_start;
36888b7b0f2SMatthew Ahrens 	td.td_func = func;
36988b7b0f2SMatthew Ahrens 	td.td_arg = arg;
37088b7b0f2SMatthew Ahrens 	td.td_pfd = &pd;
37188b7b0f2SMatthew Ahrens 	td.td_flags = flags;
37288b7b0f2SMatthew Ahrens 
37344f92b7dSChris Kirby 	pd.pd_blks_max = zfs_pd_blks_max;
37488b7b0f2SMatthew Ahrens 	pd.pd_flags = flags;
37588b7b0f2SMatthew Ahrens 	mutex_init(&pd.pd_mtx, NULL, MUTEX_DEFAULT, NULL);
37688b7b0f2SMatthew Ahrens 	cv_init(&pd.pd_cv, NULL, CV_DEFAULT, NULL);
37788b7b0f2SMatthew Ahrens 
3786e0cbcaaSMatthew Ahrens 	/* See comment on ZIL traversal in dsl_scan_visitds. */
3796e0cbcaaSMatthew Ahrens 	if (ds != NULL && !dsl_dataset_is_snapshot(ds)) {
3806e0cbcaaSMatthew Ahrens 		objset_t *os;
3816e0cbcaaSMatthew Ahrens 
3826e0cbcaaSMatthew Ahrens 		err = dmu_objset_from_ds(ds, &os);
3836e0cbcaaSMatthew Ahrens 		if (err)
3846e0cbcaaSMatthew Ahrens 			return (err);
3856e0cbcaaSMatthew Ahrens 
3866e0cbcaaSMatthew Ahrens 		traverse_zil(&td, &os->os_zil_header);
3876e0cbcaaSMatthew Ahrens 	}
3886e0cbcaaSMatthew Ahrens 
38988b7b0f2SMatthew Ahrens 	if (!(flags & TRAVERSE_PREFETCH) ||
39088b7b0f2SMatthew Ahrens 	    0 == taskq_dispatch(system_taskq, traverse_prefetch_thread,
39188b7b0f2SMatthew Ahrens 	    &td, TQ_NOQUEUE))
39288b7b0f2SMatthew Ahrens 		pd.pd_exited = B_TRUE;
39388b7b0f2SMatthew Ahrens 
3946e0cbcaaSMatthew Ahrens 	SET_BOOKMARK(&czb, td.td_objset,
395b24ab676SJeff Bonwick 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
39688b7b0f2SMatthew Ahrens 	err = traverse_visitbp(&td, NULL, NULL, rootbp, &czb);
39788b7b0f2SMatthew Ahrens 
39888b7b0f2SMatthew Ahrens 	mutex_enter(&pd.pd_mtx);
39988b7b0f2SMatthew Ahrens 	pd.pd_cancel = B_TRUE;
40088b7b0f2SMatthew Ahrens 	cv_broadcast(&pd.pd_cv);
40188b7b0f2SMatthew Ahrens 	while (!pd.pd_exited)
40288b7b0f2SMatthew Ahrens 		cv_wait(&pd.pd_cv, &pd.pd_mtx);
40388b7b0f2SMatthew Ahrens 	mutex_exit(&pd.pd_mtx);
40488b7b0f2SMatthew Ahrens 
40588b7b0f2SMatthew Ahrens 	mutex_destroy(&pd.pd_mtx);
40688b7b0f2SMatthew Ahrens 	cv_destroy(&pd.pd_cv);
407fa9e4066Sahrens 
40888b7b0f2SMatthew Ahrens 	return (err);
409fa9e4066Sahrens }
410fa9e4066Sahrens 
41188b7b0f2SMatthew Ahrens /*
41288b7b0f2SMatthew Ahrens  * NB: dataset must not be changing on-disk (eg, is a snapshot or we are
41388b7b0f2SMatthew Ahrens  * in syncing context).
41488b7b0f2SMatthew Ahrens  */
41588b7b0f2SMatthew Ahrens int
41688b7b0f2SMatthew Ahrens traverse_dataset(dsl_dataset_t *ds, uint64_t txg_start, int flags,
41788b7b0f2SMatthew Ahrens     blkptr_cb_t func, void *arg)
418fa9e4066Sahrens {
4196e0cbcaaSMatthew Ahrens 	return (traverse_impl(ds->ds_dir->dd_pool->dp_spa, ds,
42088b7b0f2SMatthew Ahrens 	    &ds->ds_phys->ds_bp, txg_start, flags, func, arg));
421fa9e4066Sahrens }
422fa9e4066Sahrens 
42388b7b0f2SMatthew Ahrens /*
42488b7b0f2SMatthew Ahrens  * NB: pool must not be changing on-disk (eg, from zdb or sync context).
42588b7b0f2SMatthew Ahrens  */
42688b7b0f2SMatthew Ahrens int
427bbfd46c4SJeff Bonwick traverse_pool(spa_t *spa, uint64_t txg_start, int flags,
428bbfd46c4SJeff Bonwick     blkptr_cb_t func, void *arg)
429fa9e4066Sahrens {
430cd088ea4SVictor Latushkin 	int err, lasterr = 0;
43188b7b0f2SMatthew Ahrens 	uint64_t obj;
43288b7b0f2SMatthew Ahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
43388b7b0f2SMatthew Ahrens 	objset_t *mos = dp->dp_meta_objset;
434cd088ea4SVictor Latushkin 	boolean_t hard = (flags & TRAVERSE_HARD);
43588b7b0f2SMatthew Ahrens 
43688b7b0f2SMatthew Ahrens 	/* visit the MOS */
4376e0cbcaaSMatthew Ahrens 	err = traverse_impl(spa, NULL, spa_get_rootblkptr(spa),
438bbfd46c4SJeff Bonwick 	    txg_start, flags, func, arg);
43988b7b0f2SMatthew Ahrens 	if (err)
44088b7b0f2SMatthew Ahrens 		return (err);
44188b7b0f2SMatthew Ahrens 
44288b7b0f2SMatthew Ahrens 	/* visit each dataset */
443cd088ea4SVictor Latushkin 	for (obj = 1; err == 0 || (err != ESRCH && hard);
444cd088ea4SVictor Latushkin 	    err = dmu_object_next(mos, &obj, FALSE, txg_start)) {
44588b7b0f2SMatthew Ahrens 		dmu_object_info_t doi;
44688b7b0f2SMatthew Ahrens 
44788b7b0f2SMatthew Ahrens 		err = dmu_object_info(mos, obj, &doi);
448cd088ea4SVictor Latushkin 		if (err) {
449cd088ea4SVictor Latushkin 			if (!hard)
450cd088ea4SVictor Latushkin 				return (err);
451cd088ea4SVictor Latushkin 			lasterr = err;
452cd088ea4SVictor Latushkin 			continue;
453cd088ea4SVictor Latushkin 		}
45488b7b0f2SMatthew Ahrens 
45588b7b0f2SMatthew Ahrens 		if (doi.doi_type == DMU_OT_DSL_DATASET) {
45688b7b0f2SMatthew Ahrens 			dsl_dataset_t *ds;
457468c413aSTim Haley 			uint64_t txg = txg_start;
458468c413aSTim Haley 
45988b7b0f2SMatthew Ahrens 			rw_enter(&dp->dp_config_rwlock, RW_READER);
46088b7b0f2SMatthew Ahrens 			err = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
46188b7b0f2SMatthew Ahrens 			rw_exit(&dp->dp_config_rwlock);
462cd088ea4SVictor Latushkin 			if (err) {
463cd088ea4SVictor Latushkin 				if (!hard)
464cd088ea4SVictor Latushkin 					return (err);
465cd088ea4SVictor Latushkin 				lasterr = err;
466cd088ea4SVictor Latushkin 				continue;
467cd088ea4SVictor Latushkin 			}
468468c413aSTim Haley 			if (ds->ds_phys->ds_prev_snap_txg > txg)
469468c413aSTim Haley 				txg = ds->ds_phys->ds_prev_snap_txg;
470bbfd46c4SJeff Bonwick 			err = traverse_dataset(ds, txg, flags, func, arg);
47188b7b0f2SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
472cd088ea4SVictor Latushkin 			if (err) {
473cd088ea4SVictor Latushkin 				if (!hard)
474cd088ea4SVictor Latushkin 					return (err);
475cd088ea4SVictor Latushkin 				lasterr = err;
476cd088ea4SVictor Latushkin 			}
47788b7b0f2SMatthew Ahrens 		}
478fa9e4066Sahrens 	}
47988b7b0f2SMatthew Ahrens 	if (err == ESRCH)
48088b7b0f2SMatthew Ahrens 		err = 0;
481cd088ea4SVictor Latushkin 	return (err != 0 ? err : lasterr);
482fa9e4066Sahrens }
483