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.
2306315b79SMatthew Ahrens * Copyright (c) 2012, 2014 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>
3943466aaeSMax Grossman #include <sys/zfeature.h>
40fa9e4066Sahrens
4134d7ce05SGeorge Wilson int32_t zfs_pd_bytes_max = 50 * 1024 * 1024; /* 50MB */
4244f92b7dSChris Kirby
436e0cbcaaSMatthew Ahrens typedef struct prefetch_data {
4488b7b0f2SMatthew Ahrens kmutex_t pd_mtx;
4588b7b0f2SMatthew Ahrens kcondvar_t pd_cv;
4634d7ce05SGeorge Wilson int32_t pd_bytes_fetched;
4788b7b0f2SMatthew Ahrens int pd_flags;
4888b7b0f2SMatthew Ahrens boolean_t pd_cancel;
4988b7b0f2SMatthew Ahrens boolean_t pd_exited;
50c1a50c7eSMatthew Ahrens zbookmark_phys_t pd_resume;
516e0cbcaaSMatthew Ahrens } prefetch_data_t;
52fa9e4066Sahrens
536e0cbcaaSMatthew Ahrens typedef struct traverse_data {
5488b7b0f2SMatthew Ahrens spa_t *td_spa;
5588b7b0f2SMatthew Ahrens uint64_t td_objset;
5688b7b0f2SMatthew Ahrens blkptr_t *td_rootbp;
5788b7b0f2SMatthew Ahrens uint64_t td_min_txg;
587802d7bfSMatthew Ahrens zbookmark_phys_t *td_resume;
5988b7b0f2SMatthew Ahrens int td_flags;
606e0cbcaaSMatthew Ahrens prefetch_data_t *td_pfd;
617fd05ac4SMatthew Ahrens boolean_t td_paused;
62f7950bf1SMatthew Ahrens uint64_t td_hole_birth_enabled_txg;
6388b7b0f2SMatthew Ahrens blkptr_cb_t *td_func;
6488b7b0f2SMatthew Ahrens void *td_arg;
65*ee88b7a2SPaul Dagnelie boolean_t td_realloc_possible;
666e0cbcaaSMatthew Ahrens } traverse_data_t;
67fa9e4066Sahrens
686e0cbcaaSMatthew Ahrens static int traverse_dnode(traverse_data_t *td, const dnode_phys_t *dnp,
691b912ec7SGeorge Wilson uint64_t objset, uint64_t object);
70b4709335SMatthew Ahrens static void prefetch_dnode_metadata(traverse_data_t *td, const dnode_phys_t *,
711b912ec7SGeorge Wilson uint64_t objset, uint64_t object);
7214843421SMatthew Ahrens
73b24ab676SJeff Bonwick static int
traverse_zil_block(zilog_t * zilog,blkptr_t * bp,void * arg,uint64_t claim_txg)745dabedeeSbonwick traverse_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
75ea8dc4b6Seschrock {
766e0cbcaaSMatthew Ahrens traverse_data_t *td = arg;
777802d7bfSMatthew Ahrens zbookmark_phys_t zb;
78ea8dc4b6Seschrock
7943466aaeSMax Grossman if (BP_IS_HOLE(bp))
80b24ab676SJeff Bonwick return (0);
815dabedeeSbonwick
8288b7b0f2SMatthew Ahrens if (claim_txg == 0 && bp->blk_birth >= spa_first_txg(td->td_spa))
83b24ab676SJeff Bonwick return (0);
8488b7b0f2SMatthew Ahrens
85b24ab676SJeff Bonwick SET_BOOKMARK(&zb, td->td_objset, ZB_ZIL_OBJECT, ZB_ZIL_LEVEL,
86b24ab676SJeff Bonwick bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
87b24ab676SJeff Bonwick
881b912ec7SGeorge Wilson (void) td->td_func(td->td_spa, zilog, bp, &zb, NULL, td->td_arg);
89b24ab676SJeff Bonwick
90b24ab676SJeff Bonwick return (0);
91ea8dc4b6Seschrock }
92ea8dc4b6Seschrock
93b24ab676SJeff Bonwick static int
traverse_zil_record(zilog_t * zilog,lr_t * lrc,void * arg,uint64_t claim_txg)945dabedeeSbonwick traverse_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg)
95ea8dc4b6Seschrock {
966e0cbcaaSMatthew Ahrens traverse_data_t *td = arg;
97ea8dc4b6Seschrock
98ea8dc4b6Seschrock if (lrc->lrc_txtype == TX_WRITE) {
99ea8dc4b6Seschrock lr_write_t *lr = (lr_write_t *)lrc;
100ea8dc4b6Seschrock blkptr_t *bp = &lr->lr_blkptr;
1017802d7bfSMatthew Ahrens zbookmark_phys_t zb;
102ea8dc4b6Seschrock
10343466aaeSMax Grossman if (BP_IS_HOLE(bp))
104b24ab676SJeff Bonwick return (0);
1055dabedeeSbonwick
10688b7b0f2SMatthew Ahrens if (claim_txg == 0 || bp->blk_birth < claim_txg)
107b24ab676SJeff Bonwick return (0);
10888b7b0f2SMatthew Ahrens
1096e0cbcaaSMatthew Ahrens SET_BOOKMARK(&zb, td->td_objset, lr->lr_foid,
1106e0cbcaaSMatthew Ahrens ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp));
111b24ab676SJeff Bonwick
1121b912ec7SGeorge Wilson (void) td->td_func(td->td_spa, zilog, bp, &zb, NULL,
113b24ab676SJeff Bonwick td->td_arg);
114ea8dc4b6Seschrock }
115b24ab676SJeff Bonwick return (0);
116ea8dc4b6Seschrock }
117ea8dc4b6Seschrock
118ea8dc4b6Seschrock static void
traverse_zil(traverse_data_t * td,zil_header_t * zh)1196e0cbcaaSMatthew Ahrens traverse_zil(traverse_data_t *td, zil_header_t *zh)
120ea8dc4b6Seschrock {
1215dabedeeSbonwick uint64_t claim_txg = zh->zh_claim_txg;
122ea8dc4b6Seschrock zilog_t *zilog;
123ea8dc4b6Seschrock
1245dabedeeSbonwick /*
1255dabedeeSbonwick * We only want to visit blocks that have been claimed but not yet
126b24ab676SJeff Bonwick * replayed; plus, in read-only mode, blocks that are already stable.
1275dabedeeSbonwick */
1288ad4d6ddSJeff Bonwick if (claim_txg == 0 && spa_writeable(td->td_spa))
1295dabedeeSbonwick return;
1305dabedeeSbonwick
13188b7b0f2SMatthew Ahrens zilog = zil_alloc(spa_get_dsl(td->td_spa)->dp_meta_objset, zh);
132ea8dc4b6Seschrock
13388b7b0f2SMatthew Ahrens (void) zil_parse(zilog, traverse_zil_block, traverse_zil_record, td,
1345dabedeeSbonwick claim_txg);
135ea8dc4b6Seschrock
136ea8dc4b6Seschrock zil_free(zilog);
137ea8dc4b6Seschrock }
138ea8dc4b6Seschrock
139ad135b5dSChristopher Siden typedef enum resume_skip {
140ad135b5dSChristopher Siden RESUME_SKIP_ALL,
141ad135b5dSChristopher Siden RESUME_SKIP_NONE,
142ad135b5dSChristopher Siden RESUME_SKIP_CHILDREN
143ad135b5dSChristopher Siden } resume_skip_t;
144ad135b5dSChristopher Siden
145ad135b5dSChristopher Siden /*
146ad135b5dSChristopher Siden * Returns RESUME_SKIP_ALL if td indicates that we are resuming a traversal and
147ad135b5dSChristopher Siden * the block indicated by zb does not need to be visited at all. Returns
148ad135b5dSChristopher Siden * RESUME_SKIP_CHILDREN if we are resuming a post traversal and we reach the
149ad135b5dSChristopher Siden * resume point. This indicates that this block should be visited but not its
150ad135b5dSChristopher Siden * children (since they must have been visited in a previous traversal).
151ad135b5dSChristopher Siden * Otherwise returns RESUME_SKIP_NONE.
152ad135b5dSChristopher Siden */
153ad135b5dSChristopher Siden static resume_skip_t
resume_skip_check(traverse_data_t * td,const dnode_phys_t * dnp,const zbookmark_phys_t * zb)154ad135b5dSChristopher Siden resume_skip_check(traverse_data_t *td, const dnode_phys_t *dnp,
1557802d7bfSMatthew Ahrens const zbookmark_phys_t *zb)
156ad135b5dSChristopher Siden {
157ad135b5dSChristopher Siden if (td->td_resume != NULL && !ZB_IS_ZERO(td->td_resume)) {
158ad135b5dSChristopher Siden /*
159ad135b5dSChristopher Siden * If we already visited this bp & everything below,
160ad135b5dSChristopher Siden * don't bother doing it again.
161ad135b5dSChristopher Siden */
162a2cdcdd2SPaul Dagnelie if (zbookmark_subtree_completed(dnp, zb, td->td_resume))
163ad135b5dSChristopher Siden return (RESUME_SKIP_ALL);
164ad135b5dSChristopher Siden
165ad135b5dSChristopher Siden /*
166ad135b5dSChristopher Siden * If we found the block we're trying to resume from, zero
167ad135b5dSChristopher Siden * the bookmark out to indicate that we have resumed.
168ad135b5dSChristopher Siden */
169ad135b5dSChristopher Siden if (bcmp(zb, td->td_resume, sizeof (*zb)) == 0) {
170ad135b5dSChristopher Siden bzero(td->td_resume, sizeof (*zb));
171ad135b5dSChristopher Siden if (td->td_flags & TRAVERSE_POST)
172ad135b5dSChristopher Siden return (RESUME_SKIP_CHILDREN);
173ad135b5dSChristopher Siden }
174ad135b5dSChristopher Siden }
175ad135b5dSChristopher Siden return (RESUME_SKIP_NONE);
176ad135b5dSChristopher Siden }
177ad135b5dSChristopher Siden
178ad135b5dSChristopher Siden static void
traverse_prefetch_metadata(traverse_data_t * td,const blkptr_t * bp,const zbookmark_phys_t * zb)179b4709335SMatthew Ahrens traverse_prefetch_metadata(traverse_data_t *td,
1807802d7bfSMatthew Ahrens const blkptr_t *bp, const zbookmark_phys_t *zb)
181b4709335SMatthew Ahrens {
1827adb730bSGeorge Wilson arc_flags_t flags = ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH;
183b4709335SMatthew Ahrens
184b4709335SMatthew Ahrens if (!(td->td_flags & TRAVERSE_PREFETCH_METADATA))
185b4709335SMatthew Ahrens return;
186b4709335SMatthew Ahrens /*
187b4709335SMatthew Ahrens * If we are in the process of resuming, don't prefetch, because
188b4709335SMatthew Ahrens * some children will not be needed (and in fact may have already
189b4709335SMatthew Ahrens * been freed).
190b4709335SMatthew Ahrens */
191b4709335SMatthew Ahrens if (td->td_resume != NULL && !ZB_IS_ZERO(td->td_resume))
192b4709335SMatthew Ahrens return;
193b4709335SMatthew Ahrens if (BP_IS_HOLE(bp) || bp->blk_birth <= td->td_min_txg)
194b4709335SMatthew Ahrens return;
195b4709335SMatthew Ahrens if (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_DNODE)
196b4709335SMatthew Ahrens return;
197b4709335SMatthew Ahrens
1981b912ec7SGeorge Wilson (void) arc_read(NULL, td->td_spa, bp, NULL, NULL,
1991b912ec7SGeorge Wilson ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
200b4709335SMatthew Ahrens }
201b4709335SMatthew Ahrens
20206315b79SMatthew Ahrens static boolean_t
prefetch_needed(prefetch_data_t * pfd,const blkptr_t * bp)20306315b79SMatthew Ahrens prefetch_needed(prefetch_data_t *pfd, const blkptr_t *bp)
20406315b79SMatthew Ahrens {
20506315b79SMatthew Ahrens ASSERT(pfd->pd_flags & TRAVERSE_PREFETCH_DATA);
20606315b79SMatthew Ahrens if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp) ||
20706315b79SMatthew Ahrens BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG)
20806315b79SMatthew Ahrens return (B_FALSE);
20906315b79SMatthew Ahrens return (B_TRUE);
21006315b79SMatthew Ahrens }
21106315b79SMatthew Ahrens
212fa9e4066Sahrens static int
traverse_visitbp(traverse_data_t * td,const dnode_phys_t * dnp,const blkptr_t * bp,const zbookmark_phys_t * zb)2136e0cbcaaSMatthew Ahrens traverse_visitbp(traverse_data_t *td, const dnode_phys_t *dnp,
2147802d7bfSMatthew Ahrens const blkptr_t *bp, const zbookmark_phys_t *zb)
215fa9e4066Sahrens {
2167802d7bfSMatthew Ahrens zbookmark_phys_t czb;
2177fd05ac4SMatthew Ahrens int err = 0;
21888b7b0f2SMatthew Ahrens arc_buf_t *buf = NULL;
2196e0cbcaaSMatthew Ahrens prefetch_data_t *pd = td->td_pfd;
220cd088ea4SVictor Latushkin boolean_t hard = td->td_flags & TRAVERSE_HARD;
221fa9e4066Sahrens
222ad135b5dSChristopher Siden switch (resume_skip_check(td, dnp, zb)) {
223ad135b5dSChristopher Siden case RESUME_SKIP_ALL:
224ad135b5dSChristopher Siden return (0);
225ad135b5dSChristopher Siden case RESUME_SKIP_CHILDREN:
226ad135b5dSChristopher Siden goto post;
227ad135b5dSChristopher Siden case RESUME_SKIP_NONE:
228ad135b5dSChristopher Siden break;
229ad135b5dSChristopher Siden default:
230ad135b5dSChristopher Siden ASSERT(0);
231ad135b5dSChristopher Siden }
232ad135b5dSChristopher Siden
233*ee88b7a2SPaul Dagnelie if (bp->blk_birth == 0 && (!td->td_realloc_possible ||
234*ee88b7a2SPaul Dagnelie zb->zb_object == DMU_META_DNODE_OBJECT)) {
23543466aaeSMax Grossman /*
236*ee88b7a2SPaul Dagnelie * Since this block has a birth time of 0 it must be one of two
237*ee88b7a2SPaul Dagnelie * things: a hole created before the SPA_FEATURE_HOLE_BIRTH
238*ee88b7a2SPaul Dagnelie * feature was enabled, or a hole which has always been a hole
239*ee88b7a2SPaul Dagnelie * in an object. This occurs when an object is created and then
240*ee88b7a2SPaul Dagnelie * a write is performed that leaves part of the file as zeroes.
241*ee88b7a2SPaul Dagnelie * Normally we don't care about this case, but if this object
242*ee88b7a2SPaul Dagnelie * has the same object number as an object that has been
243*ee88b7a2SPaul Dagnelie * destroyed, callers of dmu_traverse may not be able to tell
244*ee88b7a2SPaul Dagnelie * that the object has been recreated. Thus, we must give them
245*ee88b7a2SPaul Dagnelie * all of the holes with birth == 0 in any objects that may have
246*ee88b7a2SPaul Dagnelie * been recreated. Thus, we cannot skip the block if it is
247*ee88b7a2SPaul Dagnelie * possible the object has been recreated. If it isn't, then if
248*ee88b7a2SPaul Dagnelie * SPA_FEATURE_HOLE_BIRTH was enabled before the min_txg for
249*ee88b7a2SPaul Dagnelie * this traveral we know the hole must have been created before
250*ee88b7a2SPaul Dagnelie * the min_txg for this traveral, so we can skip it. If
251*ee88b7a2SPaul Dagnelie * SPA_FEATURE_HOLE_BIRTH was enabled after the min_txg for this
252*ee88b7a2SPaul Dagnelie * traveral we cannot tell if the hole was created before or
253*ee88b7a2SPaul Dagnelie * after the min_txg for this traversal, so we cannot skip it.
254*ee88b7a2SPaul Dagnelie * Note that the meta-dnode cannot be reallocated, so we needn't
255*ee88b7a2SPaul Dagnelie * worry about that case.
25643466aaeSMax Grossman */
257*ee88b7a2SPaul Dagnelie if (td->td_hole_birth_enabled_txg <= td->td_min_txg)
25843466aaeSMax Grossman return (0);
25943466aaeSMax Grossman } else if (bp->blk_birth <= td->td_min_txg) {
26043466aaeSMax Grossman return (0);
261fa9e4066Sahrens }
262fa9e4066Sahrens
26306315b79SMatthew Ahrens if (pd != NULL && !pd->pd_exited && prefetch_needed(pd, bp)) {
26434d7ce05SGeorge Wilson uint64_t size = BP_GET_LSIZE(bp);
26588b7b0f2SMatthew Ahrens mutex_enter(&pd->pd_mtx);
26634d7ce05SGeorge Wilson ASSERT(pd->pd_bytes_fetched >= 0);
26734d7ce05SGeorge Wilson while (pd->pd_bytes_fetched < size && !pd->pd_exited)
26888b7b0f2SMatthew Ahrens cv_wait(&pd->pd_cv, &pd->pd_mtx);
26934d7ce05SGeorge Wilson pd->pd_bytes_fetched -= size;
27088b7b0f2SMatthew Ahrens cv_broadcast(&pd->pd_cv);
27188b7b0f2SMatthew Ahrens mutex_exit(&pd->pd_mtx);
272fa9e4066Sahrens }
273fa9e4066Sahrens
27406315b79SMatthew Ahrens if (BP_IS_HOLE(bp)) {
27506315b79SMatthew Ahrens err = td->td_func(td->td_spa, NULL, bp, zb, dnp, td->td_arg);
27606315b79SMatthew Ahrens if (err != 0)
27706315b79SMatthew Ahrens goto post;
27806315b79SMatthew Ahrens return (0);
27906315b79SMatthew Ahrens }
28006315b79SMatthew Ahrens
28188b7b0f2SMatthew Ahrens if (td->td_flags & TRAVERSE_PRE) {
2821b912ec7SGeorge Wilson err = td->td_func(td->td_spa, NULL, bp, zb, dnp,
2833f9d6ad7SLin Ling td->td_arg);
28499d5e173STim Haley if (err == TRAVERSE_VISIT_NO_CHILDREN)
28599d5e173STim Haley return (0);
286ad135b5dSChristopher Siden if (err != 0)
287ad135b5dSChristopher Siden goto post;
28888b7b0f2SMatthew Ahrens }
28988b7b0f2SMatthew Ahrens
29088b7b0f2SMatthew Ahrens if (BP_GET_LEVEL(bp) > 0) {
2917adb730bSGeorge Wilson arc_flags_t flags = ARC_FLAG_WAIT;
29288b7b0f2SMatthew Ahrens int i;
29388b7b0f2SMatthew Ahrens blkptr_t *cbp;
29488b7b0f2SMatthew Ahrens int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
29588b7b0f2SMatthew Ahrens
2961b912ec7SGeorge Wilson err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf,
29788b7b0f2SMatthew Ahrens ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
2983b2aab18SMatthew Ahrens if (err != 0)
2997fd05ac4SMatthew Ahrens goto post;
30088b7b0f2SMatthew Ahrens cbp = buf->b_data;
301b4709335SMatthew Ahrens
302b4709335SMatthew Ahrens for (i = 0; i < epb; i++) {
30388b7b0f2SMatthew Ahrens SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
30488b7b0f2SMatthew Ahrens zb->zb_level - 1,
30588b7b0f2SMatthew Ahrens zb->zb_blkid * epb + i);
3061b912ec7SGeorge Wilson traverse_prefetch_metadata(td, &cbp[i], &czb);
307b4709335SMatthew Ahrens }
308b4709335SMatthew Ahrens
309b4709335SMatthew Ahrens /* recursively visitbp() blocks below this */
310b4709335SMatthew Ahrens for (i = 0; i < epb; i++) {
311b4709335SMatthew Ahrens SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
312b4709335SMatthew Ahrens zb->zb_level - 1,
313b4709335SMatthew Ahrens zb->zb_blkid * epb + i);
3141b912ec7SGeorge Wilson err = traverse_visitbp(td, dnp, &cbp[i], &czb);
3157fd05ac4SMatthew Ahrens if (err != 0)
31688b7b0f2SMatthew Ahrens break;
31788b7b0f2SMatthew Ahrens }
31888b7b0f2SMatthew Ahrens } else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) {
3197adb730bSGeorge Wilson arc_flags_t flags = ARC_FLAG_WAIT;
32014843421SMatthew Ahrens int i;
32188b7b0f2SMatthew Ahrens int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
32288b7b0f2SMatthew Ahrens
3231b912ec7SGeorge Wilson err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf,
32488b7b0f2SMatthew Ahrens ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
3253b2aab18SMatthew Ahrens if (err != 0)
3267fd05ac4SMatthew Ahrens goto post;
327c1a50c7eSMatthew Ahrens dnode_phys_t *child_dnp = buf->b_data;
328b4709335SMatthew Ahrens
329b4709335SMatthew Ahrens for (i = 0; i < epb; i++) {
330c1a50c7eSMatthew Ahrens prefetch_dnode_metadata(td, &child_dnp[i],
331c1a50c7eSMatthew Ahrens zb->zb_objset, zb->zb_blkid * epb + i);
332b4709335SMatthew Ahrens }
33388b7b0f2SMatthew Ahrens
33488b7b0f2SMatthew Ahrens /* recursively visitbp() blocks below this */
335b4709335SMatthew Ahrens for (i = 0; i < epb; i++) {
336c1a50c7eSMatthew Ahrens err = traverse_dnode(td, &child_dnp[i],
337c1a50c7eSMatthew Ahrens zb->zb_objset, zb->zb_blkid * epb + i);
3387fd05ac4SMatthew Ahrens if (err != 0)
33988b7b0f2SMatthew Ahrens break;
34088b7b0f2SMatthew Ahrens }
34188b7b0f2SMatthew Ahrens } else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
3427adb730bSGeorge Wilson arc_flags_t flags = ARC_FLAG_WAIT;
34388b7b0f2SMatthew Ahrens
3441b912ec7SGeorge Wilson err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf,
34588b7b0f2SMatthew Ahrens ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
3463b2aab18SMatthew Ahrens if (err != 0)
3477fd05ac4SMatthew Ahrens goto post;
34888b7b0f2SMatthew Ahrens
349c1a50c7eSMatthew Ahrens objset_phys_t *osp = buf->b_data;
350c1a50c7eSMatthew Ahrens prefetch_dnode_metadata(td, &osp->os_meta_dnode, zb->zb_objset,
351b4709335SMatthew Ahrens DMU_META_DNODE_OBJECT);
352*ee88b7a2SPaul Dagnelie /*
353*ee88b7a2SPaul Dagnelie * See the block comment above for the goal of this variable.
354*ee88b7a2SPaul Dagnelie * If the maxblkid of the meta-dnode is 0, then we know that
355*ee88b7a2SPaul Dagnelie * we've never had more than DNODES_PER_BLOCK objects in the
356*ee88b7a2SPaul Dagnelie * dataset, which means we can't have reused any object ids.
357*ee88b7a2SPaul Dagnelie */
358*ee88b7a2SPaul Dagnelie if (osp->os_meta_dnode.dn_maxblkid == 0)
359*ee88b7a2SPaul Dagnelie td->td_realloc_possible = B_FALSE;
360*ee88b7a2SPaul Dagnelie
361b4709335SMatthew Ahrens if (arc_buf_size(buf) >= sizeof (objset_phys_t)) {
362b4709335SMatthew Ahrens prefetch_dnode_metadata(td, &osp->os_groupused_dnode,
36348f1b90eSMatthew Ahrens zb->zb_objset, DMU_GROUPUSED_OBJECT);
36448f1b90eSMatthew Ahrens prefetch_dnode_metadata(td, &osp->os_userused_dnode,
3651b912ec7SGeorge Wilson zb->zb_objset, DMU_USERUSED_OBJECT);
366b4709335SMatthew Ahrens }
367b4709335SMatthew Ahrens
368c1a50c7eSMatthew Ahrens err = traverse_dnode(td, &osp->os_meta_dnode, zb->zb_objset,
369b24ab676SJeff Bonwick DMU_META_DNODE_OBJECT);
37014843421SMatthew Ahrens if (err == 0 && arc_buf_size(buf) >= sizeof (objset_phys_t)) {
371c1a50c7eSMatthew Ahrens err = traverse_dnode(td, &osp->os_groupused_dnode,
372c1a50c7eSMatthew Ahrens zb->zb_objset, DMU_GROUPUSED_OBJECT);
37314843421SMatthew Ahrens }
37414843421SMatthew Ahrens if (err == 0 && arc_buf_size(buf) >= sizeof (objset_phys_t)) {
375c1a50c7eSMatthew Ahrens err = traverse_dnode(td, &osp->os_userused_dnode,
376c1a50c7eSMatthew Ahrens zb->zb_objset, DMU_USERUSED_OBJECT);
37788b7b0f2SMatthew Ahrens }
37888b7b0f2SMatthew Ahrens }
37988b7b0f2SMatthew Ahrens
38088b7b0f2SMatthew Ahrens if (buf)
38188b7b0f2SMatthew Ahrens (void) arc_buf_remove_ref(buf, &buf);
38288b7b0f2SMatthew Ahrens
383ad135b5dSChristopher Siden post:
3847fd05ac4SMatthew Ahrens if (err == 0 && (td->td_flags & TRAVERSE_POST))
3851b912ec7SGeorge Wilson err = td->td_func(td->td_spa, NULL, bp, zb, dnp, td->td_arg);
3867fd05ac4SMatthew Ahrens
3877fd05ac4SMatthew Ahrens if (hard && (err == EIO || err == ECKSUM)) {
3887fd05ac4SMatthew Ahrens /*
3897fd05ac4SMatthew Ahrens * Ignore this disk error as requested by the HARD flag,
3907fd05ac4SMatthew Ahrens * and continue traversal.
3917fd05ac4SMatthew Ahrens */
3927fd05ac4SMatthew Ahrens err = 0;
393ad135b5dSChristopher Siden }
394ad135b5dSChristopher Siden
3957fd05ac4SMatthew Ahrens /*
3967fd05ac4SMatthew Ahrens * If we are stopping here, set td_resume.
3977fd05ac4SMatthew Ahrens */
3987fd05ac4SMatthew Ahrens if (td->td_resume != NULL && err != 0 && !td->td_paused) {
3997fd05ac4SMatthew Ahrens td->td_resume->zb_objset = zb->zb_objset;
4007fd05ac4SMatthew Ahrens td->td_resume->zb_object = zb->zb_object;
4017fd05ac4SMatthew Ahrens td->td_resume->zb_level = 0;
4027fd05ac4SMatthew Ahrens /*
4037fd05ac4SMatthew Ahrens * If we have stopped on an indirect block (e.g. due to
4047fd05ac4SMatthew Ahrens * i/o error), we have not visited anything below it.
4057fd05ac4SMatthew Ahrens * Set the bookmark to the first level-0 block that we need
4067fd05ac4SMatthew Ahrens * to visit. This way, the resuming code does not need to
4077fd05ac4SMatthew Ahrens * deal with resuming from indirect blocks.
408c1a50c7eSMatthew Ahrens *
409c1a50c7eSMatthew Ahrens * Note, if zb_level <= 0, dnp may be NULL, so we don't want
410c1a50c7eSMatthew Ahrens * to dereference it.
4117fd05ac4SMatthew Ahrens */
412c1a50c7eSMatthew Ahrens td->td_resume->zb_blkid = zb->zb_blkid;
413c1a50c7eSMatthew Ahrens if (zb->zb_level > 0) {
414c1a50c7eSMatthew Ahrens td->td_resume->zb_blkid <<= zb->zb_level *
415c1a50c7eSMatthew Ahrens (dnp->dn_indblkshift - SPA_BLKPTRSHIFT);
416c1a50c7eSMatthew Ahrens }
4177fd05ac4SMatthew Ahrens td->td_paused = B_TRUE;
4183f9d6ad7SLin Ling }
41988b7b0f2SMatthew Ahrens
4207fd05ac4SMatthew Ahrens return (err);
42188b7b0f2SMatthew Ahrens }
42288b7b0f2SMatthew Ahrens
423b4709335SMatthew Ahrens static void
prefetch_dnode_metadata(traverse_data_t * td,const dnode_phys_t * dnp,uint64_t objset,uint64_t object)424b4709335SMatthew Ahrens prefetch_dnode_metadata(traverse_data_t *td, const dnode_phys_t *dnp,
4251b912ec7SGeorge Wilson uint64_t objset, uint64_t object)
426b4709335SMatthew Ahrens {
427b4709335SMatthew Ahrens int j;
4287802d7bfSMatthew Ahrens zbookmark_phys_t czb;
429b4709335SMatthew Ahrens
430b4709335SMatthew Ahrens for (j = 0; j < dnp->dn_nblkptr; j++) {
431b4709335SMatthew Ahrens SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j);
4321b912ec7SGeorge Wilson traverse_prefetch_metadata(td, &dnp->dn_blkptr[j], &czb);
433b4709335SMatthew Ahrens }
434b4709335SMatthew Ahrens
435b4709335SMatthew Ahrens if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
436b4709335SMatthew Ahrens SET_BOOKMARK(&czb, objset, object, 0, DMU_SPILL_BLKID);
4371b912ec7SGeorge Wilson traverse_prefetch_metadata(td, &dnp->dn_spill, &czb);
438b4709335SMatthew Ahrens }
439b4709335SMatthew Ahrens }
440b4709335SMatthew Ahrens
44114843421SMatthew Ahrens static int
traverse_dnode(traverse_data_t * td,const dnode_phys_t * dnp,uint64_t objset,uint64_t object)4426e0cbcaaSMatthew Ahrens traverse_dnode(traverse_data_t *td, const dnode_phys_t *dnp,
4431b912ec7SGeorge Wilson uint64_t objset, uint64_t object)
44414843421SMatthew Ahrens {
4457fd05ac4SMatthew Ahrens int j, err = 0;
4467802d7bfSMatthew Ahrens zbookmark_phys_t czb;
44714843421SMatthew Ahrens
448c1a50c7eSMatthew Ahrens if (object != DMU_META_DNODE_OBJECT && td->td_resume != NULL &&
449c1a50c7eSMatthew Ahrens object < td->td_resume->zb_object)
450c1a50c7eSMatthew Ahrens return (0);
451c1a50c7eSMatthew Ahrens
452a2cdcdd2SPaul Dagnelie if (td->td_flags & TRAVERSE_PRE) {
453a2cdcdd2SPaul Dagnelie SET_BOOKMARK(&czb, objset, object, ZB_DNODE_LEVEL,
454a2cdcdd2SPaul Dagnelie ZB_DNODE_BLKID);
455a2cdcdd2SPaul Dagnelie err = td->td_func(td->td_spa, NULL, NULL, &czb, dnp,
456a2cdcdd2SPaul Dagnelie td->td_arg);
457a2cdcdd2SPaul Dagnelie if (err == TRAVERSE_VISIT_NO_CHILDREN)
458a2cdcdd2SPaul Dagnelie return (0);
459a2cdcdd2SPaul Dagnelie if (err != 0)
460a2cdcdd2SPaul Dagnelie return (err);
461a2cdcdd2SPaul Dagnelie }
462a2cdcdd2SPaul Dagnelie
46314843421SMatthew Ahrens for (j = 0; j < dnp->dn_nblkptr; j++) {
46414843421SMatthew Ahrens SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j);
4651b912ec7SGeorge Wilson err = traverse_visitbp(td, dnp, &dnp->dn_blkptr[j], &czb);
4667fd05ac4SMatthew Ahrens if (err != 0)
46714843421SMatthew Ahrens break;
4683f9d6ad7SLin Ling }
4693f9d6ad7SLin Ling
470a2cdcdd2SPaul Dagnelie if (err == 0 && (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) {
471b4709335SMatthew Ahrens SET_BOOKMARK(&czb, objset, object, 0, DMU_SPILL_BLKID);
4721b912ec7SGeorge Wilson err = traverse_visitbp(td, dnp, &dnp->dn_spill, &czb);
4737fd05ac4SMatthew Ahrens }
474a2cdcdd2SPaul Dagnelie
475a2cdcdd2SPaul Dagnelie if (err == 0 && (td->td_flags & TRAVERSE_POST)) {
476a2cdcdd2SPaul Dagnelie SET_BOOKMARK(&czb, objset, object, ZB_DNODE_LEVEL,
477a2cdcdd2SPaul Dagnelie ZB_DNODE_BLKID);
478a2cdcdd2SPaul Dagnelie err = td->td_func(td->td_spa, NULL, NULL, &czb, dnp,
479a2cdcdd2SPaul Dagnelie td->td_arg);
480a2cdcdd2SPaul Dagnelie if (err == TRAVERSE_VISIT_NO_CHILDREN)
481a2cdcdd2SPaul Dagnelie return (0);
482a2cdcdd2SPaul Dagnelie if (err != 0)
483a2cdcdd2SPaul Dagnelie return (err);
484a2cdcdd2SPaul Dagnelie }
4853f9d6ad7SLin Ling return (err);
48614843421SMatthew Ahrens }
48714843421SMatthew Ahrens
48888b7b0f2SMatthew Ahrens /* ARGSUSED */
48988b7b0f2SMatthew Ahrens static int
traverse_prefetcher(spa_t * spa,zilog_t * zilog,const blkptr_t * bp,const zbookmark_phys_t * zb,const dnode_phys_t * dnp,void * arg)490b24ab676SJeff Bonwick traverse_prefetcher(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
4917802d7bfSMatthew Ahrens const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
49288b7b0f2SMatthew Ahrens {
4936e0cbcaaSMatthew Ahrens prefetch_data_t *pfd = arg;
4947adb730bSGeorge Wilson arc_flags_t aflags = ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH;
49588b7b0f2SMatthew Ahrens
49634d7ce05SGeorge Wilson ASSERT(pfd->pd_bytes_fetched >= 0);
497a2cdcdd2SPaul Dagnelie if (bp == NULL)
498a2cdcdd2SPaul Dagnelie return (0);
49988b7b0f2SMatthew Ahrens if (pfd->pd_cancel)
500be6fd75aSMatthew Ahrens return (SET_ERROR(EINTR));
50188b7b0f2SMatthew Ahrens
50206315b79SMatthew Ahrens if (!prefetch_needed(pfd, bp))
50388b7b0f2SMatthew Ahrens return (0);
50488b7b0f2SMatthew Ahrens
50588b7b0f2SMatthew Ahrens mutex_enter(&pfd->pd_mtx);
50634d7ce05SGeorge Wilson while (!pfd->pd_cancel && pfd->pd_bytes_fetched >= zfs_pd_bytes_max)
50788b7b0f2SMatthew Ahrens cv_wait(&pfd->pd_cv, &pfd->pd_mtx);
50834d7ce05SGeorge Wilson pfd->pd_bytes_fetched += BP_GET_LSIZE(bp);
50988b7b0f2SMatthew Ahrens cv_broadcast(&pfd->pd_cv);
51088b7b0f2SMatthew Ahrens mutex_exit(&pfd->pd_mtx);
51188b7b0f2SMatthew Ahrens
5121b912ec7SGeorge Wilson (void) arc_read(NULL, spa, bp, NULL, NULL, ZIO_PRIORITY_ASYNC_READ,
5131b912ec7SGeorge Wilson ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &aflags, zb);
51488b7b0f2SMatthew Ahrens
51588b7b0f2SMatthew Ahrens return (0);
51688b7b0f2SMatthew Ahrens }
51788b7b0f2SMatthew Ahrens
51888b7b0f2SMatthew Ahrens static void
traverse_prefetch_thread(void * arg)51988b7b0f2SMatthew Ahrens traverse_prefetch_thread(void *arg)
52088b7b0f2SMatthew Ahrens {
5216e0cbcaaSMatthew Ahrens traverse_data_t *td_main = arg;
5226e0cbcaaSMatthew Ahrens traverse_data_t td = *td_main;
5237802d7bfSMatthew Ahrens zbookmark_phys_t czb;
52488b7b0f2SMatthew Ahrens
52588b7b0f2SMatthew Ahrens td.td_func = traverse_prefetcher;
52688b7b0f2SMatthew Ahrens td.td_arg = td_main->td_pfd;
52788b7b0f2SMatthew Ahrens td.td_pfd = NULL;
528c1a50c7eSMatthew Ahrens td.td_resume = &td_main->td_pfd->pd_resume;
52988b7b0f2SMatthew Ahrens
530b24ab676SJeff Bonwick SET_BOOKMARK(&czb, td.td_objset,
531b24ab676SJeff Bonwick ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
5321b912ec7SGeorge Wilson (void) traverse_visitbp(&td, NULL, td.td_rootbp, &czb);
53388b7b0f2SMatthew Ahrens
53488b7b0f2SMatthew Ahrens mutex_enter(&td_main->td_pfd->pd_mtx);
53588b7b0f2SMatthew Ahrens td_main->td_pfd->pd_exited = B_TRUE;
53688b7b0f2SMatthew Ahrens cv_broadcast(&td_main->td_pfd->pd_cv);
53788b7b0f2SMatthew Ahrens mutex_exit(&td_main->td_pfd->pd_mtx);
538fa9e4066Sahrens }
539fa9e4066Sahrens
540fa9e4066Sahrens /*
54188b7b0f2SMatthew Ahrens * NB: dataset must not be changing on-disk (eg, is a snapshot or we are
54288b7b0f2SMatthew Ahrens * in syncing context).
543fa9e4066Sahrens */
54488b7b0f2SMatthew Ahrens static int
traverse_impl(spa_t * spa,dsl_dataset_t * ds,uint64_t objset,blkptr_t * rootbp,uint64_t txg_start,zbookmark_phys_t * resume,int flags,blkptr_cb_t func,void * arg)545ad135b5dSChristopher Siden traverse_impl(spa_t *spa, dsl_dataset_t *ds, uint64_t objset, blkptr_t *rootbp,
5467802d7bfSMatthew Ahrens uint64_t txg_start, zbookmark_phys_t *resume, int flags,
547ad135b5dSChristopher Siden blkptr_cb_t func, void *arg)
548fa9e4066Sahrens {
5496e0cbcaaSMatthew Ahrens traverse_data_t td;
5506e0cbcaaSMatthew Ahrens prefetch_data_t pd = { 0 };
5517802d7bfSMatthew Ahrens zbookmark_phys_t czb;
55288b7b0f2SMatthew Ahrens int err;
553fa9e4066Sahrens
554ad135b5dSChristopher Siden ASSERT(ds == NULL || objset == ds->ds_object);
555ad135b5dSChristopher Siden ASSERT(!(flags & TRAVERSE_PRE) || !(flags & TRAVERSE_POST));
556ad135b5dSChristopher Siden
55788b7b0f2SMatthew Ahrens td.td_spa = spa;
558ad135b5dSChristopher Siden td.td_objset = objset;
55988b7b0f2SMatthew Ahrens td.td_rootbp = rootbp;
56088b7b0f2SMatthew Ahrens td.td_min_txg = txg_start;
561ad135b5dSChristopher Siden td.td_resume = resume;
56288b7b0f2SMatthew Ahrens td.td_func = func;
56388b7b0f2SMatthew Ahrens td.td_arg = arg;
56488b7b0f2SMatthew Ahrens td.td_pfd = &pd;
56588b7b0f2SMatthew Ahrens td.td_flags = flags;
5667fd05ac4SMatthew Ahrens td.td_paused = B_FALSE;
567*ee88b7a2SPaul Dagnelie td.td_realloc_possible = (txg_start == 0 ? B_FALSE : B_TRUE);
568fa9e4066Sahrens
569f7950bf1SMatthew Ahrens if (spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) {
570f7950bf1SMatthew Ahrens VERIFY(spa_feature_enabled_txg(spa,
571f7950bf1SMatthew Ahrens SPA_FEATURE_HOLE_BIRTH, &td.td_hole_birth_enabled_txg));
572f7950bf1SMatthew Ahrens } else {
573*ee88b7a2SPaul Dagnelie td.td_hole_birth_enabled_txg = UINT64_MAX;
574f7950bf1SMatthew Ahrens }
575f7950bf1SMatthew Ahrens
57688b7b0f2SMatthew Ahrens pd.pd_flags = flags;
577c1a50c7eSMatthew Ahrens if (resume != NULL)
578c1a50c7eSMatthew Ahrens pd.pd_resume = *resume;
57988b7b0f2SMatthew Ahrens mutex_init(&pd.pd_mtx, NULL, MUTEX_DEFAULT, NULL);
58088b7b0f2SMatthew Ahrens cv_init(&pd.pd_cv, NULL, CV_DEFAULT, NULL);
581fa9e4066Sahrens
5826e0cbcaaSMatthew Ahrens /* See comment on ZIL traversal in dsl_scan_visitds. */
583bc9014e6SJustin Gibbs if (ds != NULL && !ds->ds_is_snapshot && !BP_IS_HOLE(rootbp)) {
5847adb730bSGeorge Wilson arc_flags_t flags = ARC_FLAG_WAIT;
5853b2aab18SMatthew Ahrens objset_phys_t *osp;
5863b2aab18SMatthew Ahrens arc_buf_t *buf;
5876e0cbcaaSMatthew Ahrens
5883b2aab18SMatthew Ahrens err = arc_read(NULL, td.td_spa, rootbp,
5893b2aab18SMatthew Ahrens arc_getbuf_func, &buf,
5903b2aab18SMatthew Ahrens ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, NULL);
5913b2aab18SMatthew Ahrens if (err != 0)
5926e0cbcaaSMatthew Ahrens return (err);
5936e0cbcaaSMatthew Ahrens
5943b2aab18SMatthew Ahrens osp = buf->b_data;
5953b2aab18SMatthew Ahrens traverse_zil(&td, &osp->os_zil_header);
5963b2aab18SMatthew Ahrens (void) arc_buf_remove_ref(buf, &buf);
5976e0cbcaaSMatthew Ahrens }
5986e0cbcaaSMatthew Ahrens
599b4709335SMatthew Ahrens if (!(flags & TRAVERSE_PREFETCH_DATA) ||
60088b7b0f2SMatthew Ahrens 0 == taskq_dispatch(system_taskq, traverse_prefetch_thread,
60188b7b0f2SMatthew Ahrens &td, TQ_NOQUEUE))
60288b7b0f2SMatthew Ahrens pd.pd_exited = B_TRUE;
603fa9e4066Sahrens
6046e0cbcaaSMatthew Ahrens SET_BOOKMARK(&czb, td.td_objset,
605b24ab676SJeff Bonwick ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
6061b912ec7SGeorge Wilson err = traverse_visitbp(&td, NULL, rootbp, &czb);
607fa9e4066Sahrens
60888b7b0f2SMatthew Ahrens mutex_enter(&pd.pd_mtx);
60988b7b0f2SMatthew Ahrens pd.pd_cancel = B_TRUE;
61088b7b0f2SMatthew Ahrens cv_broadcast(&pd.pd_cv);
61188b7b0f2SMatthew Ahrens while (!pd.pd_exited)
61288b7b0f2SMatthew Ahrens cv_wait(&pd.pd_cv, &pd.pd_mtx);
61388b7b0f2SMatthew Ahrens mutex_exit(&pd.pd_mtx);
61488b7b0f2SMatthew Ahrens
61588b7b0f2SMatthew Ahrens mutex_destroy(&pd.pd_mtx);
61688b7b0f2SMatthew Ahrens cv_destroy(&pd.pd_cv);
61788b7b0f2SMatthew Ahrens
61888b7b0f2SMatthew Ahrens return (err);
619fa9e4066Sahrens }
620fa9e4066Sahrens
62188b7b0f2SMatthew Ahrens /*
62288b7b0f2SMatthew Ahrens * NB: dataset must not be changing on-disk (eg, is a snapshot or we are
62388b7b0f2SMatthew Ahrens * in syncing context).
62488b7b0f2SMatthew Ahrens */
62588b7b0f2SMatthew Ahrens int
traverse_dataset_resume(dsl_dataset_t * ds,uint64_t txg_start,zbookmark_phys_t * resume,int flags,blkptr_cb_t func,void * arg)626c1a50c7eSMatthew Ahrens traverse_dataset_resume(dsl_dataset_t *ds, uint64_t txg_start,
627c1a50c7eSMatthew Ahrens zbookmark_phys_t *resume,
628c1a50c7eSMatthew Ahrens int flags, blkptr_cb_t func, void *arg)
629fa9e4066Sahrens {
630ad135b5dSChristopher Siden return (traverse_impl(ds->ds_dir->dd_pool->dp_spa, ds, ds->ds_object,
631c1a50c7eSMatthew Ahrens &dsl_dataset_phys(ds)->ds_bp, txg_start, resume, flags, func, arg));
632c1a50c7eSMatthew Ahrens }
633c1a50c7eSMatthew Ahrens
634c1a50c7eSMatthew Ahrens int
traverse_dataset(dsl_dataset_t * ds,uint64_t txg_start,int flags,blkptr_cb_t func,void * arg)635c1a50c7eSMatthew Ahrens traverse_dataset(dsl_dataset_t *ds, uint64_t txg_start,
636c1a50c7eSMatthew Ahrens int flags, blkptr_cb_t func, void *arg)
637c1a50c7eSMatthew Ahrens {
638c1a50c7eSMatthew Ahrens return (traverse_dataset_resume(ds, txg_start, NULL, flags, func, arg));
639ad135b5dSChristopher Siden }
640ad135b5dSChristopher Siden
641ad135b5dSChristopher Siden int
traverse_dataset_destroyed(spa_t * spa,blkptr_t * blkptr,uint64_t txg_start,zbookmark_phys_t * resume,int flags,blkptr_cb_t func,void * arg)642ad135b5dSChristopher Siden traverse_dataset_destroyed(spa_t *spa, blkptr_t *blkptr,
6437802d7bfSMatthew Ahrens uint64_t txg_start, zbookmark_phys_t *resume, int flags,
644ad135b5dSChristopher Siden blkptr_cb_t func, void *arg)
645ad135b5dSChristopher Siden {
646ad135b5dSChristopher Siden return (traverse_impl(spa, NULL, ZB_DESTROYED_OBJSET,
647ad135b5dSChristopher Siden blkptr, txg_start, resume, flags, func, arg));
648fa9e4066Sahrens }
649fa9e4066Sahrens
65088b7b0f2SMatthew Ahrens /*
65188b7b0f2SMatthew Ahrens * NB: pool must not be changing on-disk (eg, from zdb or sync context).
65288b7b0f2SMatthew Ahrens */
65388b7b0f2SMatthew Ahrens int
traverse_pool(spa_t * spa,uint64_t txg_start,int flags,blkptr_cb_t func,void * arg)654bbfd46c4SJeff Bonwick traverse_pool(spa_t *spa, uint64_t txg_start, int flags,
655bbfd46c4SJeff Bonwick blkptr_cb_t func, void *arg)
656fa9e4066Sahrens {
6577fd05ac4SMatthew Ahrens int err;
65888b7b0f2SMatthew Ahrens dsl_pool_t *dp = spa_get_dsl(spa);
65988b7b0f2SMatthew Ahrens objset_t *mos = dp->dp_meta_objset;
660cd088ea4SVictor Latushkin boolean_t hard = (flags & TRAVERSE_HARD);
661fa9e4066Sahrens
66288b7b0f2SMatthew Ahrens /* visit the MOS */
663ad135b5dSChristopher Siden err = traverse_impl(spa, NULL, 0, spa_get_rootblkptr(spa),
664ad135b5dSChristopher Siden txg_start, NULL, flags, func, arg);
6653b2aab18SMatthew Ahrens if (err != 0)
66688b7b0f2SMatthew Ahrens return (err);
667fa9e4066Sahrens
66888b7b0f2SMatthew Ahrens /* visit each dataset */
669c1a50c7eSMatthew Ahrens for (uint64_t obj = 1; err == 0;
670c1a50c7eSMatthew Ahrens err = dmu_object_next(mos, &obj, B_FALSE, txg_start)) {
67188b7b0f2SMatthew Ahrens dmu_object_info_t doi;
672fa9e4066Sahrens
67388b7b0f2SMatthew Ahrens err = dmu_object_info(mos, obj, &doi);
6743b2aab18SMatthew Ahrens if (err != 0) {
6757fd05ac4SMatthew Ahrens if (hard)
676cd088ea4SVictor Latushkin continue;
6777fd05ac4SMatthew Ahrens break;
678cd088ea4SVictor Latushkin }
679fa9e4066Sahrens
6802acef22dSMatthew Ahrens if (doi.doi_bonus_type == DMU_OT_DSL_DATASET) {
68188b7b0f2SMatthew Ahrens dsl_dataset_t *ds;
682468c413aSTim Haley uint64_t txg = txg_start;
683468c413aSTim Haley
6843b2aab18SMatthew Ahrens dsl_pool_config_enter(dp, FTAG);
68588b7b0f2SMatthew Ahrens err = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
6863b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG);
6873b2aab18SMatthew Ahrens if (err != 0) {
6887fd05ac4SMatthew Ahrens if (hard)
689cd088ea4SVictor Latushkin continue;
6907fd05ac4SMatthew Ahrens break;
691cd088ea4SVictor Latushkin }
692c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_txg > txg)
693c1379625SJustin T. Gibbs txg = dsl_dataset_phys(ds)->ds_prev_snap_txg;
694bbfd46c4SJeff Bonwick err = traverse_dataset(ds, txg, flags, func, arg);
69588b7b0f2SMatthew Ahrens dsl_dataset_rele(ds, FTAG);
6967fd05ac4SMatthew Ahrens if (err != 0)
6977fd05ac4SMatthew Ahrens break;
698fa9e4066Sahrens }
699fa9e4066Sahrens }
70088b7b0f2SMatthew Ahrens if (err == ESRCH)
70188b7b0f2SMatthew Ahrens err = 0;
7027fd05ac4SMatthew Ahrens return (err);
703fa9e4066Sahrens }
704