xref: /titanic_44/usr/src/uts/common/fs/zfs/dmu_traverse.c (revision ee88b7a2060ab0ecb98a928e0e0595b7274aec2a)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
24  */
25 
26 #include <sys/zfs_context.h>
27 #include <sys/dmu_objset.h>
28 #include <sys/dmu_traverse.h>
29 #include <sys/dsl_dataset.h>
30 #include <sys/dsl_dir.h>
31 #include <sys/dsl_pool.h>
32 #include <sys/dnode.h>
33 #include <sys/spa.h>
34 #include <sys/zio.h>
35 #include <sys/dmu_impl.h>
36 #include <sys/sa.h>
37 #include <sys/sa_impl.h>
38 #include <sys/callb.h>
39 #include <sys/zfeature.h>
40 
41 int32_t zfs_pd_bytes_max = 50 * 1024 * 1024;	/* 50MB */
42 
43 typedef struct prefetch_data {
44 	kmutex_t pd_mtx;
45 	kcondvar_t pd_cv;
46 	int32_t pd_bytes_fetched;
47 	int pd_flags;
48 	boolean_t pd_cancel;
49 	boolean_t pd_exited;
50 	zbookmark_phys_t pd_resume;
51 } prefetch_data_t;
52 
53 typedef struct traverse_data {
54 	spa_t *td_spa;
55 	uint64_t td_objset;
56 	blkptr_t *td_rootbp;
57 	uint64_t td_min_txg;
58 	zbookmark_phys_t *td_resume;
59 	int td_flags;
60 	prefetch_data_t *td_pfd;
61 	boolean_t td_paused;
62 	uint64_t td_hole_birth_enabled_txg;
63 	blkptr_cb_t *td_func;
64 	void *td_arg;
65 	boolean_t td_realloc_possible;
66 } traverse_data_t;
67 
68 static int traverse_dnode(traverse_data_t *td, const dnode_phys_t *dnp,
69     uint64_t objset, uint64_t object);
70 static void prefetch_dnode_metadata(traverse_data_t *td, const dnode_phys_t *,
71     uint64_t objset, uint64_t object);
72 
73 static int
traverse_zil_block(zilog_t * zilog,blkptr_t * bp,void * arg,uint64_t claim_txg)74 traverse_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
75 {
76 	traverse_data_t *td = arg;
77 	zbookmark_phys_t zb;
78 
79 	if (BP_IS_HOLE(bp))
80 		return (0);
81 
82 	if (claim_txg == 0 && bp->blk_birth >= spa_first_txg(td->td_spa))
83 		return (0);
84 
85 	SET_BOOKMARK(&zb, td->td_objset, ZB_ZIL_OBJECT, ZB_ZIL_LEVEL,
86 	    bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
87 
88 	(void) td->td_func(td->td_spa, zilog, bp, &zb, NULL, td->td_arg);
89 
90 	return (0);
91 }
92 
93 static int
traverse_zil_record(zilog_t * zilog,lr_t * lrc,void * arg,uint64_t claim_txg)94 traverse_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg)
95 {
96 	traverse_data_t *td = arg;
97 
98 	if (lrc->lrc_txtype == TX_WRITE) {
99 		lr_write_t *lr = (lr_write_t *)lrc;
100 		blkptr_t *bp = &lr->lr_blkptr;
101 		zbookmark_phys_t zb;
102 
103 		if (BP_IS_HOLE(bp))
104 			return (0);
105 
106 		if (claim_txg == 0 || bp->blk_birth < claim_txg)
107 			return (0);
108 
109 		SET_BOOKMARK(&zb, td->td_objset, lr->lr_foid,
110 		    ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp));
111 
112 		(void) td->td_func(td->td_spa, zilog, bp, &zb, NULL,
113 		    td->td_arg);
114 	}
115 	return (0);
116 }
117 
118 static void
traverse_zil(traverse_data_t * td,zil_header_t * zh)119 traverse_zil(traverse_data_t *td, zil_header_t *zh)
120 {
121 	uint64_t claim_txg = zh->zh_claim_txg;
122 	zilog_t *zilog;
123 
124 	/*
125 	 * We only want to visit blocks that have been claimed but not yet
126 	 * replayed; plus, in read-only mode, blocks that are already stable.
127 	 */
128 	if (claim_txg == 0 && spa_writeable(td->td_spa))
129 		return;
130 
131 	zilog = zil_alloc(spa_get_dsl(td->td_spa)->dp_meta_objset, zh);
132 
133 	(void) zil_parse(zilog, traverse_zil_block, traverse_zil_record, td,
134 	    claim_txg);
135 
136 	zil_free(zilog);
137 }
138 
139 typedef enum resume_skip {
140 	RESUME_SKIP_ALL,
141 	RESUME_SKIP_NONE,
142 	RESUME_SKIP_CHILDREN
143 } resume_skip_t;
144 
145 /*
146  * Returns RESUME_SKIP_ALL if td indicates that we are resuming a traversal and
147  * the block indicated by zb does not need to be visited at all. Returns
148  * RESUME_SKIP_CHILDREN if we are resuming a post traversal and we reach the
149  * resume point. This indicates that this block should be visited but not its
150  * children (since they must have been visited in a previous traversal).
151  * Otherwise returns RESUME_SKIP_NONE.
152  */
153 static resume_skip_t
resume_skip_check(traverse_data_t * td,const dnode_phys_t * dnp,const zbookmark_phys_t * zb)154 resume_skip_check(traverse_data_t *td, const dnode_phys_t *dnp,
155     const zbookmark_phys_t *zb)
156 {
157 	if (td->td_resume != NULL && !ZB_IS_ZERO(td->td_resume)) {
158 		/*
159 		 * If we already visited this bp & everything below,
160 		 * don't bother doing it again.
161 		 */
162 		if (zbookmark_subtree_completed(dnp, zb, td->td_resume))
163 			return (RESUME_SKIP_ALL);
164 
165 		/*
166 		 * If we found the block we're trying to resume from, zero
167 		 * the bookmark out to indicate that we have resumed.
168 		 */
169 		if (bcmp(zb, td->td_resume, sizeof (*zb)) == 0) {
170 			bzero(td->td_resume, sizeof (*zb));
171 			if (td->td_flags & TRAVERSE_POST)
172 				return (RESUME_SKIP_CHILDREN);
173 		}
174 	}
175 	return (RESUME_SKIP_NONE);
176 }
177 
178 static void
traverse_prefetch_metadata(traverse_data_t * td,const blkptr_t * bp,const zbookmark_phys_t * zb)179 traverse_prefetch_metadata(traverse_data_t *td,
180     const blkptr_t *bp, const zbookmark_phys_t *zb)
181 {
182 	arc_flags_t flags = ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH;
183 
184 	if (!(td->td_flags & TRAVERSE_PREFETCH_METADATA))
185 		return;
186 	/*
187 	 * If we are in the process of resuming, don't prefetch, because
188 	 * some children will not be needed (and in fact may have already
189 	 * been freed).
190 	 */
191 	if (td->td_resume != NULL && !ZB_IS_ZERO(td->td_resume))
192 		return;
193 	if (BP_IS_HOLE(bp) || bp->blk_birth <= td->td_min_txg)
194 		return;
195 	if (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_DNODE)
196 		return;
197 
198 	(void) arc_read(NULL, td->td_spa, bp, NULL, NULL,
199 	    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
200 }
201 
202 static boolean_t
prefetch_needed(prefetch_data_t * pfd,const blkptr_t * bp)203 prefetch_needed(prefetch_data_t *pfd, const blkptr_t *bp)
204 {
205 	ASSERT(pfd->pd_flags & TRAVERSE_PREFETCH_DATA);
206 	if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp) ||
207 	    BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG)
208 		return (B_FALSE);
209 	return (B_TRUE);
210 }
211 
212 static int
traverse_visitbp(traverse_data_t * td,const dnode_phys_t * dnp,const blkptr_t * bp,const zbookmark_phys_t * zb)213 traverse_visitbp(traverse_data_t *td, const dnode_phys_t *dnp,
214     const blkptr_t *bp, const zbookmark_phys_t *zb)
215 {
216 	zbookmark_phys_t czb;
217 	int err = 0;
218 	arc_buf_t *buf = NULL;
219 	prefetch_data_t *pd = td->td_pfd;
220 	boolean_t hard = td->td_flags & TRAVERSE_HARD;
221 
222 	switch (resume_skip_check(td, dnp, zb)) {
223 	case RESUME_SKIP_ALL:
224 		return (0);
225 	case RESUME_SKIP_CHILDREN:
226 		goto post;
227 	case RESUME_SKIP_NONE:
228 		break;
229 	default:
230 		ASSERT(0);
231 	}
232 
233 	if (bp->blk_birth == 0 && (!td->td_realloc_possible ||
234 	    zb->zb_object == DMU_META_DNODE_OBJECT)) {
235 		/*
236 		 * Since this block has a birth time of 0 it must be one of two
237 		 * things: a hole created before the SPA_FEATURE_HOLE_BIRTH
238 		 * feature was enabled, or a hole which has always been a hole
239 		 * in an object.  This occurs when an object is created and then
240 		 * a write is performed that leaves part of the file as zeroes.
241 		 * Normally we don't care about this case, but if this object
242 		 * has the same object number as an object that has been
243 		 * destroyed, callers of dmu_traverse may not be able to tell
244 		 * that the object has been recreated.  Thus, we must give them
245 		 * all of the holes with birth == 0 in any objects that may have
246 		 * been recreated. Thus, we cannot skip the block if it is
247 		 * possible the object has been recreated. If it isn't, then if
248 		 * SPA_FEATURE_HOLE_BIRTH was enabled before the min_txg for
249 		 * this traveral we know the hole must have been created before
250 		 * the min_txg for this traveral, so we can skip it. If
251 		 * SPA_FEATURE_HOLE_BIRTH was enabled after the min_txg for this
252 		 * traveral we cannot tell if the hole was created before or
253 		 * after the min_txg for this traversal, so we cannot skip it.
254 		 * Note that the meta-dnode cannot be reallocated, so we needn't
255 		 * worry about that case.
256 		 */
257 		if (td->td_hole_birth_enabled_txg <= td->td_min_txg)
258 			return (0);
259 	} else if (bp->blk_birth <= td->td_min_txg) {
260 		return (0);
261 	}
262 
263 	if (pd != NULL && !pd->pd_exited && prefetch_needed(pd, bp)) {
264 		uint64_t size = BP_GET_LSIZE(bp);
265 		mutex_enter(&pd->pd_mtx);
266 		ASSERT(pd->pd_bytes_fetched >= 0);
267 		while (pd->pd_bytes_fetched < size && !pd->pd_exited)
268 			cv_wait(&pd->pd_cv, &pd->pd_mtx);
269 		pd->pd_bytes_fetched -= size;
270 		cv_broadcast(&pd->pd_cv);
271 		mutex_exit(&pd->pd_mtx);
272 	}
273 
274 	if (BP_IS_HOLE(bp)) {
275 		err = td->td_func(td->td_spa, NULL, bp, zb, dnp, td->td_arg);
276 		if (err != 0)
277 			goto post;
278 		return (0);
279 	}
280 
281 	if (td->td_flags & TRAVERSE_PRE) {
282 		err = td->td_func(td->td_spa, NULL, bp, zb, dnp,
283 		    td->td_arg);
284 		if (err == TRAVERSE_VISIT_NO_CHILDREN)
285 			return (0);
286 		if (err != 0)
287 			goto post;
288 	}
289 
290 	if (BP_GET_LEVEL(bp) > 0) {
291 		arc_flags_t flags = ARC_FLAG_WAIT;
292 		int i;
293 		blkptr_t *cbp;
294 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
295 
296 		err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf,
297 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
298 		if (err != 0)
299 			goto post;
300 		cbp = buf->b_data;
301 
302 		for (i = 0; i < epb; i++) {
303 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
304 			    zb->zb_level - 1,
305 			    zb->zb_blkid * epb + i);
306 			traverse_prefetch_metadata(td, &cbp[i], &czb);
307 		}
308 
309 		/* recursively visitbp() blocks below this */
310 		for (i = 0; i < epb; i++) {
311 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
312 			    zb->zb_level - 1,
313 			    zb->zb_blkid * epb + i);
314 			err = traverse_visitbp(td, dnp, &cbp[i], &czb);
315 			if (err != 0)
316 				break;
317 		}
318 	} else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) {
319 		arc_flags_t flags = ARC_FLAG_WAIT;
320 		int i;
321 		int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
322 
323 		err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf,
324 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
325 		if (err != 0)
326 			goto post;
327 		dnode_phys_t *child_dnp = buf->b_data;
328 
329 		for (i = 0; i < epb; i++) {
330 			prefetch_dnode_metadata(td, &child_dnp[i],
331 			    zb->zb_objset, zb->zb_blkid * epb + i);
332 		}
333 
334 		/* recursively visitbp() blocks below this */
335 		for (i = 0; i < epb; i++) {
336 			err = traverse_dnode(td, &child_dnp[i],
337 			    zb->zb_objset, zb->zb_blkid * epb + i);
338 			if (err != 0)
339 				break;
340 		}
341 	} else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
342 		arc_flags_t flags = ARC_FLAG_WAIT;
343 
344 		err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf,
345 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
346 		if (err != 0)
347 			goto post;
348 
349 		objset_phys_t *osp = buf->b_data;
350 		prefetch_dnode_metadata(td, &osp->os_meta_dnode, zb->zb_objset,
351 		    DMU_META_DNODE_OBJECT);
352 		/*
353 		 * See the block comment above for the goal of this variable.
354 		 * If the maxblkid of the meta-dnode is 0, then we know that
355 		 * we've never had more than DNODES_PER_BLOCK objects in the
356 		 * dataset, which means we can't have reused any object ids.
357 		 */
358 		if (osp->os_meta_dnode.dn_maxblkid == 0)
359 			td->td_realloc_possible = B_FALSE;
360 
361 		if (arc_buf_size(buf) >= sizeof (objset_phys_t)) {
362 			prefetch_dnode_metadata(td, &osp->os_groupused_dnode,
363 			    zb->zb_objset, DMU_GROUPUSED_OBJECT);
364 			prefetch_dnode_metadata(td, &osp->os_userused_dnode,
365 			    zb->zb_objset, DMU_USERUSED_OBJECT);
366 		}
367 
368 		err = traverse_dnode(td, &osp->os_meta_dnode, zb->zb_objset,
369 		    DMU_META_DNODE_OBJECT);
370 		if (err == 0 && arc_buf_size(buf) >= sizeof (objset_phys_t)) {
371 			err = traverse_dnode(td, &osp->os_groupused_dnode,
372 			    zb->zb_objset, DMU_GROUPUSED_OBJECT);
373 		}
374 		if (err == 0 && arc_buf_size(buf) >= sizeof (objset_phys_t)) {
375 			err = traverse_dnode(td, &osp->os_userused_dnode,
376 			    zb->zb_objset, DMU_USERUSED_OBJECT);
377 		}
378 	}
379 
380 	if (buf)
381 		(void) arc_buf_remove_ref(buf, &buf);
382 
383 post:
384 	if (err == 0 && (td->td_flags & TRAVERSE_POST))
385 		err = td->td_func(td->td_spa, NULL, bp, zb, dnp, td->td_arg);
386 
387 	if (hard && (err == EIO || err == ECKSUM)) {
388 		/*
389 		 * Ignore this disk error as requested by the HARD flag,
390 		 * and continue traversal.
391 		 */
392 		err = 0;
393 	}
394 
395 	/*
396 	 * If we are stopping here, set td_resume.
397 	 */
398 	if (td->td_resume != NULL && err != 0 && !td->td_paused) {
399 		td->td_resume->zb_objset = zb->zb_objset;
400 		td->td_resume->zb_object = zb->zb_object;
401 		td->td_resume->zb_level = 0;
402 		/*
403 		 * If we have stopped on an indirect block (e.g. due to
404 		 * i/o error), we have not visited anything below it.
405 		 * Set the bookmark to the first level-0 block that we need
406 		 * to visit.  This way, the resuming code does not need to
407 		 * deal with resuming from indirect blocks.
408 		 *
409 		 * Note, if zb_level <= 0, dnp may be NULL, so we don't want
410 		 * to dereference it.
411 		 */
412 		td->td_resume->zb_blkid = zb->zb_blkid;
413 		if (zb->zb_level > 0) {
414 			td->td_resume->zb_blkid <<= zb->zb_level *
415 			    (dnp->dn_indblkshift - SPA_BLKPTRSHIFT);
416 		}
417 		td->td_paused = B_TRUE;
418 	}
419 
420 	return (err);
421 }
422 
423 static void
prefetch_dnode_metadata(traverse_data_t * td,const dnode_phys_t * dnp,uint64_t objset,uint64_t object)424 prefetch_dnode_metadata(traverse_data_t *td, const dnode_phys_t *dnp,
425     uint64_t objset, uint64_t object)
426 {
427 	int j;
428 	zbookmark_phys_t czb;
429 
430 	for (j = 0; j < dnp->dn_nblkptr; j++) {
431 		SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j);
432 		traverse_prefetch_metadata(td, &dnp->dn_blkptr[j], &czb);
433 	}
434 
435 	if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
436 		SET_BOOKMARK(&czb, objset, object, 0, DMU_SPILL_BLKID);
437 		traverse_prefetch_metadata(td, &dnp->dn_spill, &czb);
438 	}
439 }
440 
441 static int
traverse_dnode(traverse_data_t * td,const dnode_phys_t * dnp,uint64_t objset,uint64_t object)442 traverse_dnode(traverse_data_t *td, const dnode_phys_t *dnp,
443     uint64_t objset, uint64_t object)
444 {
445 	int j, err = 0;
446 	zbookmark_phys_t czb;
447 
448 	if (object != DMU_META_DNODE_OBJECT && td->td_resume != NULL &&
449 	    object < td->td_resume->zb_object)
450 		return (0);
451 
452 	if (td->td_flags & TRAVERSE_PRE) {
453 		SET_BOOKMARK(&czb, objset, object, ZB_DNODE_LEVEL,
454 		    ZB_DNODE_BLKID);
455 		err = td->td_func(td->td_spa, NULL, NULL, &czb, dnp,
456 		    td->td_arg);
457 		if (err == TRAVERSE_VISIT_NO_CHILDREN)
458 			return (0);
459 		if (err != 0)
460 			return (err);
461 	}
462 
463 	for (j = 0; j < dnp->dn_nblkptr; j++) {
464 		SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j);
465 		err = traverse_visitbp(td, dnp, &dnp->dn_blkptr[j], &czb);
466 		if (err != 0)
467 			break;
468 	}
469 
470 	if (err == 0 && (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) {
471 		SET_BOOKMARK(&czb, objset, object, 0, DMU_SPILL_BLKID);
472 		err = traverse_visitbp(td, dnp, &dnp->dn_spill, &czb);
473 	}
474 
475 	if (err == 0 && (td->td_flags & TRAVERSE_POST)) {
476 		SET_BOOKMARK(&czb, objset, object, ZB_DNODE_LEVEL,
477 		    ZB_DNODE_BLKID);
478 		err = td->td_func(td->td_spa, NULL, NULL, &czb, dnp,
479 		    td->td_arg);
480 		if (err == TRAVERSE_VISIT_NO_CHILDREN)
481 			return (0);
482 		if (err != 0)
483 			return (err);
484 	}
485 	return (err);
486 }
487 
488 /* ARGSUSED */
489 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)490 traverse_prefetcher(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
491     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
492 {
493 	prefetch_data_t *pfd = arg;
494 	arc_flags_t aflags = ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH;
495 
496 	ASSERT(pfd->pd_bytes_fetched >= 0);
497 	if (bp == NULL)
498 		return (0);
499 	if (pfd->pd_cancel)
500 		return (SET_ERROR(EINTR));
501 
502 	if (!prefetch_needed(pfd, bp))
503 		return (0);
504 
505 	mutex_enter(&pfd->pd_mtx);
506 	while (!pfd->pd_cancel && pfd->pd_bytes_fetched >= zfs_pd_bytes_max)
507 		cv_wait(&pfd->pd_cv, &pfd->pd_mtx);
508 	pfd->pd_bytes_fetched += BP_GET_LSIZE(bp);
509 	cv_broadcast(&pfd->pd_cv);
510 	mutex_exit(&pfd->pd_mtx);
511 
512 	(void) arc_read(NULL, spa, bp, NULL, NULL, ZIO_PRIORITY_ASYNC_READ,
513 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &aflags, zb);
514 
515 	return (0);
516 }
517 
518 static void
traverse_prefetch_thread(void * arg)519 traverse_prefetch_thread(void *arg)
520 {
521 	traverse_data_t *td_main = arg;
522 	traverse_data_t td = *td_main;
523 	zbookmark_phys_t czb;
524 
525 	td.td_func = traverse_prefetcher;
526 	td.td_arg = td_main->td_pfd;
527 	td.td_pfd = NULL;
528 	td.td_resume = &td_main->td_pfd->pd_resume;
529 
530 	SET_BOOKMARK(&czb, td.td_objset,
531 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
532 	(void) traverse_visitbp(&td, NULL, td.td_rootbp, &czb);
533 
534 	mutex_enter(&td_main->td_pfd->pd_mtx);
535 	td_main->td_pfd->pd_exited = B_TRUE;
536 	cv_broadcast(&td_main->td_pfd->pd_cv);
537 	mutex_exit(&td_main->td_pfd->pd_mtx);
538 }
539 
540 /*
541  * NB: dataset must not be changing on-disk (eg, is a snapshot or we are
542  * in syncing context).
543  */
544 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)545 traverse_impl(spa_t *spa, dsl_dataset_t *ds, uint64_t objset, blkptr_t *rootbp,
546     uint64_t txg_start, zbookmark_phys_t *resume, int flags,
547     blkptr_cb_t func, void *arg)
548 {
549 	traverse_data_t td;
550 	prefetch_data_t pd = { 0 };
551 	zbookmark_phys_t czb;
552 	int err;
553 
554 	ASSERT(ds == NULL || objset == ds->ds_object);
555 	ASSERT(!(flags & TRAVERSE_PRE) || !(flags & TRAVERSE_POST));
556 
557 	td.td_spa = spa;
558 	td.td_objset = objset;
559 	td.td_rootbp = rootbp;
560 	td.td_min_txg = txg_start;
561 	td.td_resume = resume;
562 	td.td_func = func;
563 	td.td_arg = arg;
564 	td.td_pfd = &pd;
565 	td.td_flags = flags;
566 	td.td_paused = B_FALSE;
567 	td.td_realloc_possible = (txg_start == 0 ? B_FALSE : B_TRUE);
568 
569 	if (spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) {
570 		VERIFY(spa_feature_enabled_txg(spa,
571 		    SPA_FEATURE_HOLE_BIRTH, &td.td_hole_birth_enabled_txg));
572 	} else {
573 		td.td_hole_birth_enabled_txg = UINT64_MAX;
574 	}
575 
576 	pd.pd_flags = flags;
577 	if (resume != NULL)
578 		pd.pd_resume = *resume;
579 	mutex_init(&pd.pd_mtx, NULL, MUTEX_DEFAULT, NULL);
580 	cv_init(&pd.pd_cv, NULL, CV_DEFAULT, NULL);
581 
582 	/* See comment on ZIL traversal in dsl_scan_visitds. */
583 	if (ds != NULL && !ds->ds_is_snapshot && !BP_IS_HOLE(rootbp)) {
584 		arc_flags_t flags = ARC_FLAG_WAIT;
585 		objset_phys_t *osp;
586 		arc_buf_t *buf;
587 
588 		err = arc_read(NULL, td.td_spa, rootbp,
589 		    arc_getbuf_func, &buf,
590 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, NULL);
591 		if (err != 0)
592 			return (err);
593 
594 		osp = buf->b_data;
595 		traverse_zil(&td, &osp->os_zil_header);
596 		(void) arc_buf_remove_ref(buf, &buf);
597 	}
598 
599 	if (!(flags & TRAVERSE_PREFETCH_DATA) ||
600 	    0 == taskq_dispatch(system_taskq, traverse_prefetch_thread,
601 	    &td, TQ_NOQUEUE))
602 		pd.pd_exited = B_TRUE;
603 
604 	SET_BOOKMARK(&czb, td.td_objset,
605 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
606 	err = traverse_visitbp(&td, NULL, rootbp, &czb);
607 
608 	mutex_enter(&pd.pd_mtx);
609 	pd.pd_cancel = B_TRUE;
610 	cv_broadcast(&pd.pd_cv);
611 	while (!pd.pd_exited)
612 		cv_wait(&pd.pd_cv, &pd.pd_mtx);
613 	mutex_exit(&pd.pd_mtx);
614 
615 	mutex_destroy(&pd.pd_mtx);
616 	cv_destroy(&pd.pd_cv);
617 
618 	return (err);
619 }
620 
621 /*
622  * NB: dataset must not be changing on-disk (eg, is a snapshot or we are
623  * in syncing context).
624  */
625 int
traverse_dataset_resume(dsl_dataset_t * ds,uint64_t txg_start,zbookmark_phys_t * resume,int flags,blkptr_cb_t func,void * arg)626 traverse_dataset_resume(dsl_dataset_t *ds, uint64_t txg_start,
627     zbookmark_phys_t *resume,
628     int flags, blkptr_cb_t func, void *arg)
629 {
630 	return (traverse_impl(ds->ds_dir->dd_pool->dp_spa, ds, ds->ds_object,
631 	    &dsl_dataset_phys(ds)->ds_bp, txg_start, resume, flags, func, arg));
632 }
633 
634 int
traverse_dataset(dsl_dataset_t * ds,uint64_t txg_start,int flags,blkptr_cb_t func,void * arg)635 traverse_dataset(dsl_dataset_t *ds, uint64_t txg_start,
636     int flags, blkptr_cb_t func, void *arg)
637 {
638 	return (traverse_dataset_resume(ds, txg_start, NULL, flags, func, arg));
639 }
640 
641 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)642 traverse_dataset_destroyed(spa_t *spa, blkptr_t *blkptr,
643     uint64_t txg_start, zbookmark_phys_t *resume, int flags,
644     blkptr_cb_t func, void *arg)
645 {
646 	return (traverse_impl(spa, NULL, ZB_DESTROYED_OBJSET,
647 	    blkptr, txg_start, resume, flags, func, arg));
648 }
649 
650 /*
651  * NB: pool must not be changing on-disk (eg, from zdb or sync context).
652  */
653 int
traverse_pool(spa_t * spa,uint64_t txg_start,int flags,blkptr_cb_t func,void * arg)654 traverse_pool(spa_t *spa, uint64_t txg_start, int flags,
655     blkptr_cb_t func, void *arg)
656 {
657 	int err;
658 	dsl_pool_t *dp = spa_get_dsl(spa);
659 	objset_t *mos = dp->dp_meta_objset;
660 	boolean_t hard = (flags & TRAVERSE_HARD);
661 
662 	/* visit the MOS */
663 	err = traverse_impl(spa, NULL, 0, spa_get_rootblkptr(spa),
664 	    txg_start, NULL, flags, func, arg);
665 	if (err != 0)
666 		return (err);
667 
668 	/* visit each dataset */
669 	for (uint64_t obj = 1; err == 0;
670 	    err = dmu_object_next(mos, &obj, B_FALSE, txg_start)) {
671 		dmu_object_info_t doi;
672 
673 		err = dmu_object_info(mos, obj, &doi);
674 		if (err != 0) {
675 			if (hard)
676 				continue;
677 			break;
678 		}
679 
680 		if (doi.doi_bonus_type == DMU_OT_DSL_DATASET) {
681 			dsl_dataset_t *ds;
682 			uint64_t txg = txg_start;
683 
684 			dsl_pool_config_enter(dp, FTAG);
685 			err = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
686 			dsl_pool_config_exit(dp, FTAG);
687 			if (err != 0) {
688 				if (hard)
689 					continue;
690 				break;
691 			}
692 			if (dsl_dataset_phys(ds)->ds_prev_snap_txg > txg)
693 				txg = dsl_dataset_phys(ds)->ds_prev_snap_txg;
694 			err = traverse_dataset(ds, txg, flags, func, arg);
695 			dsl_dataset_rele(ds, FTAG);
696 			if (err != 0)
697 				break;
698 		}
699 	}
700 	if (err == ESRCH)
701 		err = 0;
702 	return (err);
703 }
704