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