xref: /titanic_50/usr/src/uts/common/fs/zfs/dmu_traverse.c (revision 49f9b365248ee858ee91baa36eab27c5200f6dca)
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 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 
40 int zfs_pd_blks_max = 100;
41 
42 typedef struct prefetch_data {
43 	kmutex_t pd_mtx;
44 	kcondvar_t pd_cv;
45 	int pd_blks_max;
46 	int pd_blks_fetched;
47 	int pd_flags;
48 	boolean_t pd_cancel;
49 	boolean_t pd_exited;
50 } prefetch_data_t;
51 
52 typedef struct traverse_data {
53 	spa_t *td_spa;
54 	uint64_t td_objset;
55 	blkptr_t *td_rootbp;
56 	uint64_t td_min_txg;
57 	zbookmark_t *td_resume;
58 	int td_flags;
59 	prefetch_data_t *td_pfd;
60 	blkptr_cb_t *td_func;
61 	void *td_arg;
62 } traverse_data_t;
63 
64 static int traverse_dnode(traverse_data_t *td, const dnode_phys_t *dnp,
65     arc_buf_t *buf, uint64_t objset, uint64_t object);
66 static void prefetch_dnode_metadata(traverse_data_t *td, const dnode_phys_t *,
67     arc_buf_t *buf, uint64_t objset, uint64_t object);
68 
69 static int
70 traverse_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
71 {
72 	traverse_data_t *td = arg;
73 	zbookmark_t zb;
74 
75 	if (bp->blk_birth == 0)
76 		return (0);
77 
78 	if (claim_txg == 0 && bp->blk_birth >= spa_first_txg(td->td_spa))
79 		return (0);
80 
81 	SET_BOOKMARK(&zb, td->td_objset, ZB_ZIL_OBJECT, ZB_ZIL_LEVEL,
82 	    bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
83 
84 	(void) td->td_func(td->td_spa, zilog, bp, NULL, &zb, NULL, td->td_arg);
85 
86 	return (0);
87 }
88 
89 static int
90 traverse_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg)
91 {
92 	traverse_data_t *td = arg;
93 
94 	if (lrc->lrc_txtype == TX_WRITE) {
95 		lr_write_t *lr = (lr_write_t *)lrc;
96 		blkptr_t *bp = &lr->lr_blkptr;
97 		zbookmark_t zb;
98 
99 		if (bp->blk_birth == 0)
100 			return (0);
101 
102 		if (claim_txg == 0 || bp->blk_birth < claim_txg)
103 			return (0);
104 
105 		SET_BOOKMARK(&zb, td->td_objset, lr->lr_foid,
106 		    ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp));
107 
108 		(void) td->td_func(td->td_spa, zilog, bp, NULL, &zb, NULL,
109 		    td->td_arg);
110 	}
111 	return (0);
112 }
113 
114 static void
115 traverse_zil(traverse_data_t *td, zil_header_t *zh)
116 {
117 	uint64_t claim_txg = zh->zh_claim_txg;
118 	zilog_t *zilog;
119 
120 	/*
121 	 * We only want to visit blocks that have been claimed but not yet
122 	 * replayed; plus, in read-only mode, blocks that are already stable.
123 	 */
124 	if (claim_txg == 0 && spa_writeable(td->td_spa))
125 		return;
126 
127 	zilog = zil_alloc(spa_get_dsl(td->td_spa)->dp_meta_objset, zh);
128 
129 	(void) zil_parse(zilog, traverse_zil_block, traverse_zil_record, td,
130 	    claim_txg);
131 
132 	zil_free(zilog);
133 }
134 
135 typedef enum resume_skip {
136 	RESUME_SKIP_ALL,
137 	RESUME_SKIP_NONE,
138 	RESUME_SKIP_CHILDREN
139 } resume_skip_t;
140 
141 /*
142  * Returns RESUME_SKIP_ALL if td indicates that we are resuming a traversal and
143  * the block indicated by zb does not need to be visited at all. Returns
144  * RESUME_SKIP_CHILDREN if we are resuming a post traversal and we reach the
145  * resume point. This indicates that this block should be visited but not its
146  * children (since they must have been visited in a previous traversal).
147  * Otherwise returns RESUME_SKIP_NONE.
148  */
149 static resume_skip_t
150 resume_skip_check(traverse_data_t *td, const dnode_phys_t *dnp,
151     const zbookmark_t *zb)
152 {
153 	if (td->td_resume != NULL && !ZB_IS_ZERO(td->td_resume)) {
154 		/*
155 		 * If we already visited this bp & everything below,
156 		 * don't bother doing it again.
157 		 */
158 		if (zbookmark_is_before(dnp, zb, td->td_resume))
159 			return (RESUME_SKIP_ALL);
160 
161 		/*
162 		 * If we found the block we're trying to resume from, zero
163 		 * the bookmark out to indicate that we have resumed.
164 		 */
165 		ASSERT3U(zb->zb_object, <=, td->td_resume->zb_object);
166 		if (bcmp(zb, td->td_resume, sizeof (*zb)) == 0) {
167 			bzero(td->td_resume, sizeof (*zb));
168 			if (td->td_flags & TRAVERSE_POST)
169 				return (RESUME_SKIP_CHILDREN);
170 		}
171 	}
172 	return (RESUME_SKIP_NONE);
173 }
174 
175 static void
176 traverse_pause(traverse_data_t *td, const zbookmark_t *zb)
177 {
178 	ASSERT(td->td_resume != NULL);
179 	ASSERT0(zb->zb_level);
180 	bcopy(zb, td->td_resume, sizeof (*td->td_resume));
181 }
182 
183 static void
184 traverse_prefetch_metadata(traverse_data_t *td,
185     arc_buf_t *pbuf, const blkptr_t *bp, const zbookmark_t *zb)
186 {
187 	uint32_t flags = ARC_NOWAIT | ARC_PREFETCH;
188 
189 	if (!(td->td_flags & TRAVERSE_PREFETCH_METADATA))
190 		return;
191 	/*
192 	 * If we are in the process of resuming, don't prefetch, because
193 	 * some children will not be needed (and in fact may have already
194 	 * been freed).
195 	 */
196 	if (td->td_resume != NULL && !ZB_IS_ZERO(td->td_resume))
197 		return;
198 	if (BP_IS_HOLE(bp) || bp->blk_birth <= td->td_min_txg)
199 		return;
200 	if (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_DNODE)
201 		return;
202 
203 	(void) arc_read(NULL, td->td_spa, bp,
204 	    pbuf, NULL, NULL, ZIO_PRIORITY_ASYNC_READ,
205 	    ZIO_FLAG_CANFAIL, &flags, zb);
206 }
207 
208 static int
209 traverse_visitbp(traverse_data_t *td, const dnode_phys_t *dnp,
210     arc_buf_t *pbuf, const blkptr_t *bp, const zbookmark_t *zb)
211 {
212 	zbookmark_t czb;
213 	int err = 0, lasterr = 0;
214 	arc_buf_t *buf = NULL;
215 	prefetch_data_t *pd = td->td_pfd;
216 	boolean_t hard = td->td_flags & TRAVERSE_HARD;
217 	boolean_t pause = B_FALSE;
218 
219 	switch (resume_skip_check(td, dnp, zb)) {
220 	case RESUME_SKIP_ALL:
221 		return (0);
222 	case RESUME_SKIP_CHILDREN:
223 		goto post;
224 	case RESUME_SKIP_NONE:
225 		break;
226 	default:
227 		ASSERT(0);
228 	}
229 
230 	if (BP_IS_HOLE(bp)) {
231 		err = td->td_func(td->td_spa, NULL, NULL, pbuf, zb, dnp,
232 		    td->td_arg);
233 		return (err);
234 	}
235 
236 	if (bp->blk_birth <= td->td_min_txg)
237 		return (0);
238 
239 	if (pd && !pd->pd_exited &&
240 	    ((pd->pd_flags & TRAVERSE_PREFETCH_DATA) ||
241 	    BP_GET_TYPE(bp) == DMU_OT_DNODE || BP_GET_LEVEL(bp) > 0)) {
242 		mutex_enter(&pd->pd_mtx);
243 		ASSERT(pd->pd_blks_fetched >= 0);
244 		while (pd->pd_blks_fetched == 0 && !pd->pd_exited)
245 			cv_wait(&pd->pd_cv, &pd->pd_mtx);
246 		pd->pd_blks_fetched--;
247 		cv_broadcast(&pd->pd_cv);
248 		mutex_exit(&pd->pd_mtx);
249 	}
250 
251 	if (td->td_flags & TRAVERSE_PRE) {
252 		err = td->td_func(td->td_spa, NULL, bp, pbuf, zb, dnp,
253 		    td->td_arg);
254 		if (err == TRAVERSE_VISIT_NO_CHILDREN)
255 			return (0);
256 		if (err == ERESTART)
257 			pause = B_TRUE; /* handle pausing at a common point */
258 		if (err != 0)
259 			goto post;
260 	}
261 
262 	if (BP_GET_LEVEL(bp) > 0) {
263 		uint32_t flags = ARC_WAIT;
264 		int i;
265 		blkptr_t *cbp;
266 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
267 
268 		err = dsl_read(NULL, td->td_spa, bp, pbuf,
269 		    arc_getbuf_func, &buf,
270 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
271 		if (err)
272 			return (err);
273 		cbp = buf->b_data;
274 
275 		for (i = 0; i < epb; i++) {
276 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
277 			    zb->zb_level - 1,
278 			    zb->zb_blkid * epb + i);
279 			traverse_prefetch_metadata(td, buf, &cbp[i], &czb);
280 		}
281 
282 		/* recursively visitbp() blocks below this */
283 		for (i = 0; i < epb; i++) {
284 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
285 			    zb->zb_level - 1,
286 			    zb->zb_blkid * epb + i);
287 			err = traverse_visitbp(td, dnp, buf, &cbp[i], &czb);
288 			if (err) {
289 				if (!hard)
290 					break;
291 				lasterr = err;
292 			}
293 		}
294 	} else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) {
295 		uint32_t flags = ARC_WAIT;
296 		int i;
297 		int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
298 
299 		err = dsl_read(NULL, td->td_spa, bp, pbuf,
300 		    arc_getbuf_func, &buf,
301 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
302 		if (err)
303 			return (err);
304 		dnp = buf->b_data;
305 
306 		for (i = 0; i < epb; i++) {
307 			prefetch_dnode_metadata(td, &dnp[i], buf, zb->zb_objset,
308 			    zb->zb_blkid * epb + i);
309 		}
310 
311 		/* recursively visitbp() blocks below this */
312 		for (i = 0; i < epb; i++) {
313 			err = traverse_dnode(td, &dnp[i], buf, zb->zb_objset,
314 			    zb->zb_blkid * epb + i);
315 			if (err) {
316 				if (!hard)
317 					break;
318 				lasterr = err;
319 			}
320 		}
321 	} else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
322 		uint32_t flags = ARC_WAIT;
323 		objset_phys_t *osp;
324 		dnode_phys_t *dnp;
325 
326 		err = dsl_read_nolock(NULL, td->td_spa, bp,
327 		    arc_getbuf_func, &buf,
328 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
329 		if (err)
330 			return (err);
331 
332 		osp = buf->b_data;
333 		dnp = &osp->os_meta_dnode;
334 		prefetch_dnode_metadata(td, dnp, buf, zb->zb_objset,
335 		    DMU_META_DNODE_OBJECT);
336 		if (arc_buf_size(buf) >= sizeof (objset_phys_t)) {
337 			prefetch_dnode_metadata(td, &osp->os_userused_dnode,
338 			    buf, zb->zb_objset, DMU_USERUSED_OBJECT);
339 			prefetch_dnode_metadata(td, &osp->os_groupused_dnode,
340 			    buf, zb->zb_objset, DMU_USERUSED_OBJECT);
341 		}
342 
343 		err = traverse_dnode(td, dnp, buf, zb->zb_objset,
344 		    DMU_META_DNODE_OBJECT);
345 		if (err && hard) {
346 			lasterr = err;
347 			err = 0;
348 		}
349 		if (err == 0 && arc_buf_size(buf) >= sizeof (objset_phys_t)) {
350 			dnp = &osp->os_userused_dnode;
351 			err = traverse_dnode(td, dnp, buf, zb->zb_objset,
352 			    DMU_USERUSED_OBJECT);
353 		}
354 		if (err && hard) {
355 			lasterr = err;
356 			err = 0;
357 		}
358 		if (err == 0 && arc_buf_size(buf) >= sizeof (objset_phys_t)) {
359 			dnp = &osp->os_groupused_dnode;
360 			err = traverse_dnode(td, dnp, buf, zb->zb_objset,
361 			    DMU_GROUPUSED_OBJECT);
362 		}
363 	}
364 
365 	if (buf)
366 		(void) arc_buf_remove_ref(buf, &buf);
367 
368 post:
369 	if (err == 0 && lasterr == 0 && (td->td_flags & TRAVERSE_POST)) {
370 		err = td->td_func(td->td_spa, NULL, bp, pbuf, zb, dnp,
371 		    td->td_arg);
372 		if (err == ERESTART)
373 			pause = B_TRUE;
374 	}
375 
376 	if (pause && td->td_resume != NULL) {
377 		ASSERT3U(err, ==, ERESTART);
378 		ASSERT(!hard);
379 		traverse_pause(td, zb);
380 	}
381 
382 	return (err != 0 ? err : lasterr);
383 }
384 
385 static void
386 prefetch_dnode_metadata(traverse_data_t *td, const dnode_phys_t *dnp,
387     arc_buf_t *buf, uint64_t objset, uint64_t object)
388 {
389 	int j;
390 	zbookmark_t czb;
391 
392 	for (j = 0; j < dnp->dn_nblkptr; j++) {
393 		SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j);
394 		traverse_prefetch_metadata(td, buf, &dnp->dn_blkptr[j], &czb);
395 	}
396 
397 	if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
398 		SET_BOOKMARK(&czb, objset, object, 0, DMU_SPILL_BLKID);
399 		traverse_prefetch_metadata(td, buf, &dnp->dn_spill, &czb);
400 	}
401 }
402 
403 static int
404 traverse_dnode(traverse_data_t *td, const dnode_phys_t *dnp,
405     arc_buf_t *buf, uint64_t objset, uint64_t object)
406 {
407 	int j, err = 0, lasterr = 0;
408 	zbookmark_t czb;
409 	boolean_t hard = (td->td_flags & TRAVERSE_HARD);
410 
411 	for (j = 0; j < dnp->dn_nblkptr; j++) {
412 		SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j);
413 		err = traverse_visitbp(td, dnp, buf, &dnp->dn_blkptr[j], &czb);
414 		if (err) {
415 			if (!hard)
416 				break;
417 			lasterr = err;
418 		}
419 	}
420 
421 	if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
422 		SET_BOOKMARK(&czb, objset, object, 0, DMU_SPILL_BLKID);
423 		err = traverse_visitbp(td, dnp, buf, &dnp->dn_spill, &czb);
424 		if (err) {
425 			if (!hard)
426 				return (err);
427 			lasterr = err;
428 		}
429 	}
430 	return (err != 0 ? err : lasterr);
431 }
432 
433 /* ARGSUSED */
434 static int
435 traverse_prefetcher(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
436     arc_buf_t *pbuf, const zbookmark_t *zb, const dnode_phys_t *dnp,
437     void *arg)
438 {
439 	prefetch_data_t *pfd = arg;
440 	uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
441 
442 	ASSERT(pfd->pd_blks_fetched >= 0);
443 	if (pfd->pd_cancel)
444 		return (EINTR);
445 
446 	if (bp == NULL || !((pfd->pd_flags & TRAVERSE_PREFETCH_DATA) ||
447 	    BP_GET_TYPE(bp) == DMU_OT_DNODE || BP_GET_LEVEL(bp) > 0) ||
448 	    BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG)
449 		return (0);
450 
451 	mutex_enter(&pfd->pd_mtx);
452 	while (!pfd->pd_cancel && pfd->pd_blks_fetched >= pfd->pd_blks_max)
453 		cv_wait(&pfd->pd_cv, &pfd->pd_mtx);
454 	pfd->pd_blks_fetched++;
455 	cv_broadcast(&pfd->pd_cv);
456 	mutex_exit(&pfd->pd_mtx);
457 
458 	(void) dsl_read(NULL, spa, bp, pbuf, NULL, NULL,
459 	    ZIO_PRIORITY_ASYNC_READ,
460 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
461 	    &aflags, zb);
462 
463 	return (0);
464 }
465 
466 static void
467 traverse_prefetch_thread(void *arg)
468 {
469 	traverse_data_t *td_main = arg;
470 	traverse_data_t td = *td_main;
471 	zbookmark_t czb;
472 
473 	td.td_func = traverse_prefetcher;
474 	td.td_arg = td_main->td_pfd;
475 	td.td_pfd = NULL;
476 
477 	SET_BOOKMARK(&czb, td.td_objset,
478 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
479 	(void) traverse_visitbp(&td, NULL, NULL, td.td_rootbp, &czb);
480 
481 	mutex_enter(&td_main->td_pfd->pd_mtx);
482 	td_main->td_pfd->pd_exited = B_TRUE;
483 	cv_broadcast(&td_main->td_pfd->pd_cv);
484 	mutex_exit(&td_main->td_pfd->pd_mtx);
485 }
486 
487 /*
488  * NB: dataset must not be changing on-disk (eg, is a snapshot or we are
489  * in syncing context).
490  */
491 static int
492 traverse_impl(spa_t *spa, dsl_dataset_t *ds, uint64_t objset, blkptr_t *rootbp,
493     uint64_t txg_start, zbookmark_t *resume, int flags,
494     blkptr_cb_t func, void *arg)
495 {
496 	traverse_data_t td;
497 	prefetch_data_t pd = { 0 };
498 	zbookmark_t czb;
499 	int err;
500 
501 	ASSERT(ds == NULL || objset == ds->ds_object);
502 	ASSERT(!(flags & TRAVERSE_PRE) || !(flags & TRAVERSE_POST));
503 
504 	/*
505 	 * The data prefetching mechanism (the prefetch thread) is incompatible
506 	 * with resuming from a bookmark.
507 	 */
508 	ASSERT(resume == NULL || !(flags & TRAVERSE_PREFETCH_DATA));
509 
510 	td.td_spa = spa;
511 	td.td_objset = objset;
512 	td.td_rootbp = rootbp;
513 	td.td_min_txg = txg_start;
514 	td.td_resume = resume;
515 	td.td_func = func;
516 	td.td_arg = arg;
517 	td.td_pfd = &pd;
518 	td.td_flags = flags;
519 
520 	pd.pd_blks_max = zfs_pd_blks_max;
521 	pd.pd_flags = flags;
522 	mutex_init(&pd.pd_mtx, NULL, MUTEX_DEFAULT, NULL);
523 	cv_init(&pd.pd_cv, NULL, CV_DEFAULT, NULL);
524 
525 	/* See comment on ZIL traversal in dsl_scan_visitds. */
526 	if (ds != NULL && !dsl_dataset_is_snapshot(ds)) {
527 		objset_t *os;
528 
529 		err = dmu_objset_from_ds(ds, &os);
530 		if (err)
531 			return (err);
532 
533 		traverse_zil(&td, &os->os_zil_header);
534 	}
535 
536 	if (!(flags & TRAVERSE_PREFETCH_DATA) ||
537 	    0 == taskq_dispatch(system_taskq, traverse_prefetch_thread,
538 	    &td, TQ_NOQUEUE))
539 		pd.pd_exited = B_TRUE;
540 
541 	SET_BOOKMARK(&czb, td.td_objset,
542 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
543 	err = traverse_visitbp(&td, NULL, NULL, rootbp, &czb);
544 
545 	mutex_enter(&pd.pd_mtx);
546 	pd.pd_cancel = B_TRUE;
547 	cv_broadcast(&pd.pd_cv);
548 	while (!pd.pd_exited)
549 		cv_wait(&pd.pd_cv, &pd.pd_mtx);
550 	mutex_exit(&pd.pd_mtx);
551 
552 	mutex_destroy(&pd.pd_mtx);
553 	cv_destroy(&pd.pd_cv);
554 
555 	return (err);
556 }
557 
558 /*
559  * NB: dataset must not be changing on-disk (eg, is a snapshot or we are
560  * in syncing context).
561  */
562 int
563 traverse_dataset(dsl_dataset_t *ds, uint64_t txg_start, int flags,
564     blkptr_cb_t func, void *arg)
565 {
566 	return (traverse_impl(ds->ds_dir->dd_pool->dp_spa, ds, ds->ds_object,
567 	    &ds->ds_phys->ds_bp, txg_start, NULL, flags, func, arg));
568 }
569 
570 int
571 traverse_dataset_destroyed(spa_t *spa, blkptr_t *blkptr,
572     uint64_t txg_start, zbookmark_t *resume, int flags,
573     blkptr_cb_t func, void *arg)
574 {
575 	return (traverse_impl(spa, NULL, ZB_DESTROYED_OBJSET,
576 	    blkptr, txg_start, resume, flags, func, arg));
577 }
578 
579 /*
580  * NB: pool must not be changing on-disk (eg, from zdb or sync context).
581  */
582 int
583 traverse_pool(spa_t *spa, uint64_t txg_start, int flags,
584     blkptr_cb_t func, void *arg)
585 {
586 	int err, lasterr = 0;
587 	uint64_t obj;
588 	dsl_pool_t *dp = spa_get_dsl(spa);
589 	objset_t *mos = dp->dp_meta_objset;
590 	boolean_t hard = (flags & TRAVERSE_HARD);
591 
592 	/* visit the MOS */
593 	err = traverse_impl(spa, NULL, 0, spa_get_rootblkptr(spa),
594 	    txg_start, NULL, flags, func, arg);
595 	if (err)
596 		return (err);
597 
598 	/* visit each dataset */
599 	for (obj = 1; err == 0 || (err != ESRCH && hard);
600 	    err = dmu_object_next(mos, &obj, FALSE, txg_start)) {
601 		dmu_object_info_t doi;
602 
603 		err = dmu_object_info(mos, obj, &doi);
604 		if (err) {
605 			if (!hard)
606 				return (err);
607 			lasterr = err;
608 			continue;
609 		}
610 
611 		if (doi.doi_type == DMU_OT_DSL_DATASET) {
612 			dsl_dataset_t *ds;
613 			uint64_t txg = txg_start;
614 
615 			rw_enter(&dp->dp_config_rwlock, RW_READER);
616 			err = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
617 			rw_exit(&dp->dp_config_rwlock);
618 			if (err) {
619 				if (!hard)
620 					return (err);
621 				lasterr = err;
622 				continue;
623 			}
624 			if (ds->ds_phys->ds_prev_snap_txg > txg)
625 				txg = ds->ds_phys->ds_prev_snap_txg;
626 			err = traverse_dataset(ds, txg, flags, func, arg);
627 			dsl_dataset_rele(ds, FTAG);
628 			if (err) {
629 				if (!hard)
630 					return (err);
631 				lasterr = err;
632 			}
633 		}
634 	}
635 	if (err == ESRCH)
636 		err = 0;
637 	return (err != 0 ? err : lasterr);
638 }
639