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.
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.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
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>
35 #include <sys/dmu_impl.h>
37 #include <sys/sa_impl.h>
38 #include <sys/callb.h>
39 #include <sys/zfeature.h>
41 int32_t zfs_pd_bytes_max = 50 * 1024 * 1024; /* 50MB */
43 typedef struct prefetch_data {
46 int32_t pd_bytes_fetched;
52 typedef struct traverse_data {
57 zbookmark_phys_t *td_resume;
59 prefetch_data_t *td_pfd;
61 uint64_t td_hole_birth_enabled_txg;
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);
72 traverse_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
74 traverse_data_t *td = arg;
80 if (claim_txg == 0 && bp->blk_birth >= spa_first_txg(td->td_spa))
83 SET_BOOKMARK(&zb, td->td_objset, ZB_ZIL_OBJECT, ZB_ZIL_LEVEL,
84 bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
86 (void) td->td_func(td->td_spa, zilog, bp, &zb, NULL, td->td_arg);
92 traverse_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg)
94 traverse_data_t *td = arg;
96 if (lrc->lrc_txtype == TX_WRITE) {
97 lr_write_t *lr = (lr_write_t *)lrc;
98 blkptr_t *bp = &lr->lr_blkptr;
104 if (claim_txg == 0 || bp->blk_birth < claim_txg)
107 SET_BOOKMARK(&zb, td->td_objset, lr->lr_foid,
108 ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp));
110 (void) td->td_func(td->td_spa, zilog, bp, &zb, NULL,
117 traverse_zil(traverse_data_t *td, zil_header_t *zh)
119 uint64_t claim_txg = zh->zh_claim_txg;
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.
126 if (claim_txg == 0 && spa_writeable(td->td_spa))
129 zilog = zil_alloc(spa_get_dsl(td->td_spa)->dp_meta_objset, zh);
131 (void) zil_parse(zilog, traverse_zil_block, traverse_zil_record, td,
137 typedef enum resume_skip {
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.
152 resume_skip_check(traverse_data_t *td, const dnode_phys_t *dnp,
153 const zbookmark_phys_t *zb)
155 if (td->td_resume != NULL && !ZB_IS_ZERO(td->td_resume)) {
157 * If we already visited this bp & everything below,
158 * don't bother doing it again.
160 if (zbookmark_is_before(dnp, zb, td->td_resume))
161 return (RESUME_SKIP_ALL);
164 * If we found the block we're trying to resume from, zero
165 * the bookmark out to indicate that we have resumed.
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);
173 return (RESUME_SKIP_NONE);
177 traverse_prefetch_metadata(traverse_data_t *td,
178 const blkptr_t *bp, const zbookmark_phys_t *zb)
180 uint32_t flags = ARC_NOWAIT | ARC_PREFETCH;
182 if (!(td->td_flags & TRAVERSE_PREFETCH_METADATA))
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
189 if (td->td_resume != NULL && !ZB_IS_ZERO(td->td_resume))
191 if (BP_IS_HOLE(bp) || bp->blk_birth <= td->td_min_txg)
193 if (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_DNODE)
196 (void) arc_read(NULL, td->td_spa, bp, NULL, NULL,
197 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
201 prefetch_needed(prefetch_data_t *pfd, const blkptr_t *bp)
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)
211 traverse_visitbp(traverse_data_t *td, const dnode_phys_t *dnp,
212 const blkptr_t *bp, const zbookmark_phys_t *zb)
215 arc_buf_t *buf = NULL;
216 prefetch_data_t *pd = td->td_pfd;
218 switch (resume_skip_check(td, dnp, zb)) {
219 case RESUME_SKIP_ALL:
221 case RESUME_SKIP_CHILDREN:
223 case RESUME_SKIP_NONE:
229 if (bp->blk_birth == 0) {
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.
242 if (td->td_hole_birth_enabled_txg < td->td_min_txg)
244 } else if (bp->blk_birth <= td->td_min_txg) {
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);
259 if (BP_IS_HOLE(bp)) {
260 err = td->td_func(td->td_spa, NULL, bp, zb, dnp, td->td_arg);
266 if (td->td_flags & TRAVERSE_PRE) {
267 err = td->td_func(td->td_spa, NULL, bp, zb, dnp,
269 if (err == TRAVERSE_VISIT_NO_CHILDREN)
275 if (BP_GET_LEVEL(bp) > 0) {
276 uint32_t flags = ARC_WAIT;
278 int32_t epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
279 zbookmark_phys_t *czb;
281 err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf,
282 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
286 czb = kmem_alloc(sizeof (zbookmark_phys_t), KM_SLEEP);
288 for (i = 0; i < epb; i++) {
289 SET_BOOKMARK(czb, zb->zb_objset, zb->zb_object,
291 zb->zb_blkid * epb + i);
292 traverse_prefetch_metadata(td,
293 &((blkptr_t *)buf->b_data)[i], czb);
296 /* recursively visitbp() blocks below this */
297 for (i = 0; i < epb; i++) {
298 SET_BOOKMARK(czb, zb->zb_objset, zb->zb_object,
300 zb->zb_blkid * epb + i);
301 err = traverse_visitbp(td, dnp,
302 &((blkptr_t *)buf->b_data)[i], czb);
307 kmem_free(czb, sizeof (zbookmark_phys_t));
309 } else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) {
310 uint32_t flags = ARC_WAIT;
312 int32_t epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
315 err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf,
316 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
321 for (i = 0; i < epb; i++) {
322 prefetch_dnode_metadata(td, &cdnp[i], zb->zb_objset,
323 zb->zb_blkid * epb + i);
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);
333 } else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
334 uint32_t flags = ARC_WAIT;
336 dnode_phys_t *mdnp, *gdnp, *udnp;
338 err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf,
339 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
344 mdnp = &osp->os_meta_dnode;
345 gdnp = &osp->os_groupused_dnode;
346 udnp = &osp->os_userused_dnode;
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);
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);
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);
370 (void) arc_buf_remove_ref(buf, &buf);
373 if (err == 0 && (td->td_flags & TRAVERSE_POST))
374 err = td->td_func(td->td_spa, NULL, bp, zb, dnp, td->td_arg);
376 if ((td->td_flags & TRAVERSE_HARD) && (err == EIO || err == ECKSUM)) {
378 * Ignore this disk error as requested by the HARD flag,
379 * and continue traversal.
385 * If we are stopping here, set td_resume.
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;
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.
398 td->td_resume->zb_blkid = zb->zb_blkid <<
399 (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT));
400 td->td_paused = B_TRUE;
407 prefetch_dnode_metadata(traverse_data_t *td, const dnode_phys_t *dnp,
408 uint64_t objset, uint64_t object)
411 zbookmark_phys_t czb;
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);
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);
425 traverse_dnode(traverse_data_t *td, const dnode_phys_t *dnp,
426 uint64_t objset, uint64_t object)
429 zbookmark_phys_t czb;
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);
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);
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)
450 prefetch_data_t *pfd = arg;
451 uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
453 ASSERT(pfd->pd_bytes_fetched >= 0);
455 return (SET_ERROR(EINTR));
457 if (!prefetch_needed(pfd, bp))
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);
467 (void) arc_read(NULL, spa, bp, NULL, NULL, ZIO_PRIORITY_ASYNC_READ,
468 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &aflags, zb);
474 traverse_prefetch_thread(void *arg)
476 traverse_data_t *td_main = arg;
477 traverse_data_t td = *td_main;
478 zbookmark_phys_t czb;
480 td.td_func = traverse_prefetcher;
481 td.td_arg = td_main->td_pfd;
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);
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);
495 * NB: dataset must not be changing on-disk (eg, is a snapshot or we are
496 * in syncing context).
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)
505 zbookmark_phys_t *czb;
508 ASSERT(ds == NULL || objset == ds->ds_object);
509 ASSERT(!(flags & TRAVERSE_PRE) || !(flags & TRAVERSE_POST));
512 * The data prefetching mechanism (the prefetch thread) is incompatible
513 * with resuming from a bookmark.
515 ASSERT(resume == NULL || !(flags & TRAVERSE_PREFETCH_DATA));
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);
522 td->td_objset = objset;
523 td->td_rootbp = rootbp;
524 td->td_min_txg = txg_start;
525 td->td_resume = resume;
529 td->td_flags = flags;
530 td->td_paused = B_FALSE;
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));
536 td->td_hole_birth_enabled_txg = 0;
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);
543 SET_BOOKMARK(czb, td->td_objset,
544 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
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;
552 err = arc_read(NULL, td->td_spa, rootbp,
553 arc_getbuf_func, &buf,
554 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, czb);
559 traverse_zil(td, &osp->os_zil_header);
560 (void) arc_buf_remove_ref(buf, &buf);
563 if (!(flags & TRAVERSE_PREFETCH_DATA) ||
564 0 == taskq_dispatch(system_taskq, traverse_prefetch_thread,
566 pd->pd_exited = B_TRUE;
568 err = traverse_visitbp(td, NULL, rootbp, czb);
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);
577 mutex_destroy(&pd->pd_mtx);
578 cv_destroy(&pd->pd_cv);
580 kmem_free(czb, sizeof (zbookmark_phys_t));
581 kmem_free(pd, sizeof (struct prefetch_data));
582 kmem_free(td, sizeof (struct traverse_data));
588 * NB: dataset must not be changing on-disk (eg, is a snapshot or we are
589 * in syncing context).
592 traverse_dataset(dsl_dataset_t *ds, uint64_t txg_start, int flags,
593 blkptr_cb_t func, void *arg)
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));
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)
604 return (traverse_impl(spa, NULL, ZB_DESTROYED_OBJSET,
605 blkptr, txg_start, resume, flags, func, arg));
609 * NB: pool must not be changing on-disk (eg, from zdb or sync context).
612 traverse_pool(spa_t *spa, uint64_t txg_start, int flags,
613 blkptr_cb_t func, void *arg)
617 dsl_pool_t *dp = spa_get_dsl(spa);
618 objset_t *mos = dp->dp_meta_objset;
619 boolean_t hard = (flags & TRAVERSE_HARD);
622 err = traverse_impl(spa, NULL, 0, spa_get_rootblkptr(spa),
623 txg_start, NULL, flags, func, arg);
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;
632 err = dmu_object_info(mos, obj, &doi);
639 if (doi.doi_bonus_type == DMU_OT_DSL_DATASET) {
641 uint64_t txg = txg_start;
643 dsl_pool_config_enter(dp, FTAG);
644 err = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
645 dsl_pool_config_exit(dp, FTAG);
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);
664 #if defined(_KERNEL) && defined(HAVE_SPL)
665 EXPORT_SYMBOL(traverse_dataset);
666 EXPORT_SYMBOL(traverse_pool);
668 module_param(zfs_pd_bytes_max, int, 0644);
669 MODULE_PARM_DESC(zfs_pd_bytes_max, "Max number of bytes to prefetch");