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> 4088b7b0f2SMatthew Ahrens 4144f92b7dSChris Kirby int zfs_pd_blks_max = 100; 4244f92b7dSChris Kirby 436e0cbcaaSMatthew Ahrens typedef struct prefetch_data { 4488b7b0f2SMatthew Ahrens kmutex_t pd_mtx; 4588b7b0f2SMatthew Ahrens kcondvar_t pd_cv; 4688b7b0f2SMatthew Ahrens int pd_blks_max; 4788b7b0f2SMatthew Ahrens int pd_blks_fetched; 4888b7b0f2SMatthew Ahrens int pd_flags; 4988b7b0f2SMatthew Ahrens boolean_t pd_cancel; 5088b7b0f2SMatthew Ahrens boolean_t pd_exited; 516e0cbcaaSMatthew Ahrens } prefetch_data_t; 5288b7b0f2SMatthew Ahrens 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; 58*7802d7bfSMatthew Ahrens zbookmark_phys_t *td_resume; 5988b7b0f2SMatthew Ahrens int td_flags; 606e0cbcaaSMatthew Ahrens prefetch_data_t *td_pfd; 617fd05ac4SMatthew Ahrens boolean_t td_paused; 6288b7b0f2SMatthew Ahrens blkptr_cb_t *td_func; 6388b7b0f2SMatthew Ahrens void *td_arg; 646e0cbcaaSMatthew Ahrens } traverse_data_t; 65fa9e4066Sahrens 666e0cbcaaSMatthew Ahrens static int traverse_dnode(traverse_data_t *td, const dnode_phys_t *dnp, 671b912ec7SGeorge Wilson uint64_t objset, uint64_t object); 68b4709335SMatthew Ahrens static void prefetch_dnode_metadata(traverse_data_t *td, const dnode_phys_t *, 691b912ec7SGeorge Wilson uint64_t objset, uint64_t object); 7014843421SMatthew Ahrens 71b24ab676SJeff Bonwick static int 725dabedeeSbonwick traverse_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg) 73ea8dc4b6Seschrock { 746e0cbcaaSMatthew Ahrens traverse_data_t *td = arg; 75*7802d7bfSMatthew Ahrens zbookmark_phys_t zb; 76ea8dc4b6Seschrock 7743466aaeSMax Grossman if (BP_IS_HOLE(bp)) 78b24ab676SJeff Bonwick return (0); 795dabedeeSbonwick 8088b7b0f2SMatthew Ahrens if (claim_txg == 0 && bp->blk_birth >= spa_first_txg(td->td_spa)) 81b24ab676SJeff Bonwick return (0); 8288b7b0f2SMatthew Ahrens 83b24ab676SJeff Bonwick SET_BOOKMARK(&zb, td->td_objset, ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, 84b24ab676SJeff Bonwick bp->blk_cksum.zc_word[ZIL_ZC_SEQ]); 85b24ab676SJeff Bonwick 861b912ec7SGeorge Wilson (void) td->td_func(td->td_spa, zilog, bp, &zb, NULL, td->td_arg); 87b24ab676SJeff Bonwick 88b24ab676SJeff Bonwick return (0); 89ea8dc4b6Seschrock } 90ea8dc4b6Seschrock 91b24ab676SJeff Bonwick static int 925dabedeeSbonwick traverse_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg) 93ea8dc4b6Seschrock { 946e0cbcaaSMatthew Ahrens traverse_data_t *td = arg; 95ea8dc4b6Seschrock 96ea8dc4b6Seschrock if (lrc->lrc_txtype == TX_WRITE) { 97ea8dc4b6Seschrock lr_write_t *lr = (lr_write_t *)lrc; 98ea8dc4b6Seschrock blkptr_t *bp = &lr->lr_blkptr; 99*7802d7bfSMatthew Ahrens zbookmark_phys_t zb; 100ea8dc4b6Seschrock 10143466aaeSMax Grossman if (BP_IS_HOLE(bp)) 102b24ab676SJeff Bonwick return (0); 1035dabedeeSbonwick 10488b7b0f2SMatthew Ahrens if (claim_txg == 0 || bp->blk_birth < claim_txg) 105b24ab676SJeff Bonwick return (0); 106b24ab676SJeff Bonwick 1076e0cbcaaSMatthew Ahrens SET_BOOKMARK(&zb, td->td_objset, lr->lr_foid, 1086e0cbcaaSMatthew Ahrens ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp)); 10988b7b0f2SMatthew Ahrens 1101b912ec7SGeorge Wilson (void) td->td_func(td->td_spa, zilog, bp, &zb, NULL, 111b24ab676SJeff Bonwick td->td_arg); 112ea8dc4b6Seschrock } 113b24ab676SJeff Bonwick return (0); 114ea8dc4b6Seschrock } 115ea8dc4b6Seschrock 116ea8dc4b6Seschrock static void 1176e0cbcaaSMatthew Ahrens traverse_zil(traverse_data_t *td, zil_header_t *zh) 118ea8dc4b6Seschrock { 1195dabedeeSbonwick uint64_t claim_txg = zh->zh_claim_txg; 120ea8dc4b6Seschrock zilog_t *zilog; 121ea8dc4b6Seschrock 1225dabedeeSbonwick /* 1235dabedeeSbonwick * We only want to visit blocks that have been claimed but not yet 124b24ab676SJeff Bonwick * replayed; plus, in read-only mode, blocks that are already stable. 1255dabedeeSbonwick */ 1268ad4d6ddSJeff Bonwick if (claim_txg == 0 && spa_writeable(td->td_spa)) 1275dabedeeSbonwick return; 1285dabedeeSbonwick 12988b7b0f2SMatthew Ahrens zilog = zil_alloc(spa_get_dsl(td->td_spa)->dp_meta_objset, zh); 130ea8dc4b6Seschrock 13188b7b0f2SMatthew Ahrens (void) zil_parse(zilog, traverse_zil_block, traverse_zil_record, td, 1325dabedeeSbonwick claim_txg); 133ea8dc4b6Seschrock 134ea8dc4b6Seschrock zil_free(zilog); 135ea8dc4b6Seschrock } 136ea8dc4b6Seschrock 137ad135b5dSChristopher Siden typedef enum resume_skip { 138ad135b5dSChristopher Siden RESUME_SKIP_ALL, 139ad135b5dSChristopher Siden RESUME_SKIP_NONE, 140ad135b5dSChristopher Siden RESUME_SKIP_CHILDREN 141ad135b5dSChristopher Siden } resume_skip_t; 142ad135b5dSChristopher Siden 143ad135b5dSChristopher Siden /* 144ad135b5dSChristopher Siden * Returns RESUME_SKIP_ALL if td indicates that we are resuming a traversal and 145ad135b5dSChristopher Siden * the block indicated by zb does not need to be visited at all. Returns 146ad135b5dSChristopher Siden * RESUME_SKIP_CHILDREN if we are resuming a post traversal and we reach the 147ad135b5dSChristopher Siden * resume point. This indicates that this block should be visited but not its 148ad135b5dSChristopher Siden * children (since they must have been visited in a previous traversal). 149ad135b5dSChristopher Siden * Otherwise returns RESUME_SKIP_NONE. 150ad135b5dSChristopher Siden */ 151ad135b5dSChristopher Siden static resume_skip_t 152ad135b5dSChristopher Siden resume_skip_check(traverse_data_t *td, const dnode_phys_t *dnp, 153*7802d7bfSMatthew Ahrens const zbookmark_phys_t *zb) 154ad135b5dSChristopher Siden { 155ad135b5dSChristopher Siden if (td->td_resume != NULL && !ZB_IS_ZERO(td->td_resume)) { 156ad135b5dSChristopher Siden /* 157ad135b5dSChristopher Siden * If we already visited this bp & everything below, 158ad135b5dSChristopher Siden * don't bother doing it again. 159ad135b5dSChristopher Siden */ 160ad135b5dSChristopher Siden if (zbookmark_is_before(dnp, zb, td->td_resume)) 161ad135b5dSChristopher Siden return (RESUME_SKIP_ALL); 162ad135b5dSChristopher Siden 163ad135b5dSChristopher Siden /* 164ad135b5dSChristopher Siden * If we found the block we're trying to resume from, zero 165ad135b5dSChristopher Siden * the bookmark out to indicate that we have resumed. 166ad135b5dSChristopher Siden */ 167ad135b5dSChristopher Siden if (bcmp(zb, td->td_resume, sizeof (*zb)) == 0) { 168ad135b5dSChristopher Siden bzero(td->td_resume, sizeof (*zb)); 169ad135b5dSChristopher Siden if (td->td_flags & TRAVERSE_POST) 170ad135b5dSChristopher Siden return (RESUME_SKIP_CHILDREN); 171ad135b5dSChristopher Siden } 172ad135b5dSChristopher Siden } 173ad135b5dSChristopher Siden return (RESUME_SKIP_NONE); 174ad135b5dSChristopher Siden } 175ad135b5dSChristopher Siden 176b4709335SMatthew Ahrens static void 177b4709335SMatthew Ahrens traverse_prefetch_metadata(traverse_data_t *td, 178*7802d7bfSMatthew Ahrens const blkptr_t *bp, const zbookmark_phys_t *zb) 179b4709335SMatthew Ahrens { 180b4709335SMatthew Ahrens uint32_t flags = ARC_NOWAIT | ARC_PREFETCH; 181b4709335SMatthew Ahrens 182b4709335SMatthew Ahrens if (!(td->td_flags & TRAVERSE_PREFETCH_METADATA)) 183b4709335SMatthew Ahrens return; 184b4709335SMatthew Ahrens /* 185b4709335SMatthew Ahrens * If we are in the process of resuming, don't prefetch, because 186b4709335SMatthew Ahrens * some children will not be needed (and in fact may have already 187b4709335SMatthew Ahrens * been freed). 188b4709335SMatthew Ahrens */ 189b4709335SMatthew Ahrens if (td->td_resume != NULL && !ZB_IS_ZERO(td->td_resume)) 190b4709335SMatthew Ahrens return; 191b4709335SMatthew Ahrens if (BP_IS_HOLE(bp) || bp->blk_birth <= td->td_min_txg) 192b4709335SMatthew Ahrens return; 193b4709335SMatthew Ahrens if (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_DNODE) 194b4709335SMatthew Ahrens return; 195b4709335SMatthew Ahrens 1961b912ec7SGeorge Wilson (void) arc_read(NULL, td->td_spa, bp, NULL, NULL, 1971b912ec7SGeorge Wilson ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb); 198b4709335SMatthew Ahrens } 199b4709335SMatthew Ahrens 20006315b79SMatthew Ahrens static boolean_t 20106315b79SMatthew Ahrens prefetch_needed(prefetch_data_t *pfd, const blkptr_t *bp) 20206315b79SMatthew Ahrens { 20306315b79SMatthew Ahrens ASSERT(pfd->pd_flags & TRAVERSE_PREFETCH_DATA); 20406315b79SMatthew Ahrens if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp) || 20506315b79SMatthew Ahrens BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG) 20606315b79SMatthew Ahrens return (B_FALSE); 20706315b79SMatthew Ahrens return (B_TRUE); 20806315b79SMatthew Ahrens } 20906315b79SMatthew Ahrens 210fa9e4066Sahrens static int 2116e0cbcaaSMatthew Ahrens traverse_visitbp(traverse_data_t *td, const dnode_phys_t *dnp, 212*7802d7bfSMatthew Ahrens const blkptr_t *bp, const zbookmark_phys_t *zb) 213fa9e4066Sahrens { 214*7802d7bfSMatthew Ahrens zbookmark_phys_t czb; 2157fd05ac4SMatthew Ahrens int err = 0; 21688b7b0f2SMatthew Ahrens arc_buf_t *buf = NULL; 2176e0cbcaaSMatthew Ahrens prefetch_data_t *pd = td->td_pfd; 218cd088ea4SVictor Latushkin boolean_t hard = td->td_flags & TRAVERSE_HARD; 219ad135b5dSChristopher Siden 220ad135b5dSChristopher Siden switch (resume_skip_check(td, dnp, zb)) { 221ad135b5dSChristopher Siden case RESUME_SKIP_ALL: 222ad135b5dSChristopher Siden return (0); 223ad135b5dSChristopher Siden case RESUME_SKIP_CHILDREN: 224ad135b5dSChristopher Siden goto post; 225ad135b5dSChristopher Siden case RESUME_SKIP_NONE: 226ad135b5dSChristopher Siden break; 227ad135b5dSChristopher Siden default: 228ad135b5dSChristopher Siden ASSERT(0); 229ad135b5dSChristopher Siden } 230fa9e4066Sahrens 23143466aaeSMax Grossman if (bp->blk_birth == 0) { 23243466aaeSMax Grossman if (spa_feature_is_active(td->td_spa, SPA_FEATURE_HOLE_BIRTH)) { 23343466aaeSMax Grossman /* 23443466aaeSMax Grossman * Since this block has a birth time of 0 it must be a 23543466aaeSMax Grossman * hole created before the SPA_FEATURE_HOLE_BIRTH 23643466aaeSMax Grossman * feature was enabled. If SPA_FEATURE_HOLE_BIRTH 23743466aaeSMax Grossman * was enabled before the min_txg for this traveral we 23843466aaeSMax Grossman * know the hole must have been created before the 23943466aaeSMax Grossman * min_txg for this traveral, so we can skip it. If 24043466aaeSMax Grossman * SPA_FEATURE_HOLE_BIRTH was enabled after the min_txg 24143466aaeSMax Grossman * for this traveral we cannot tell if the hole was 24243466aaeSMax Grossman * created before or after the min_txg for this 24343466aaeSMax Grossman * traversal, so we cannot skip it. 24443466aaeSMax Grossman */ 24543466aaeSMax Grossman uint64_t hole_birth_enabled_txg; 24643466aaeSMax Grossman VERIFY(spa_feature_enabled_txg(td->td_spa, 24743466aaeSMax Grossman SPA_FEATURE_HOLE_BIRTH, &hole_birth_enabled_txg)); 24843466aaeSMax Grossman if (hole_birth_enabled_txg < td->td_min_txg) 24943466aaeSMax Grossman return (0); 25043466aaeSMax Grossman } 25143466aaeSMax Grossman } else if (bp->blk_birth <= td->td_min_txg) { 25243466aaeSMax Grossman return (0); 25343466aaeSMax Grossman } 25443466aaeSMax Grossman 25506315b79SMatthew Ahrens if (pd != NULL && !pd->pd_exited && prefetch_needed(pd, bp)) { 25688b7b0f2SMatthew Ahrens mutex_enter(&pd->pd_mtx); 25788b7b0f2SMatthew Ahrens ASSERT(pd->pd_blks_fetched >= 0); 25888b7b0f2SMatthew Ahrens while (pd->pd_blks_fetched == 0 && !pd->pd_exited) 25988b7b0f2SMatthew Ahrens cv_wait(&pd->pd_cv, &pd->pd_mtx); 26088b7b0f2SMatthew Ahrens pd->pd_blks_fetched--; 26188b7b0f2SMatthew Ahrens cv_broadcast(&pd->pd_cv); 26288b7b0f2SMatthew Ahrens mutex_exit(&pd->pd_mtx); 263fa9e4066Sahrens } 264fa9e4066Sahrens 26506315b79SMatthew Ahrens if (BP_IS_HOLE(bp)) { 26606315b79SMatthew Ahrens err = td->td_func(td->td_spa, NULL, bp, zb, dnp, td->td_arg); 26706315b79SMatthew Ahrens if (err != 0) 26806315b79SMatthew Ahrens goto post; 26906315b79SMatthew Ahrens return (0); 27006315b79SMatthew Ahrens } 27106315b79SMatthew Ahrens 27288b7b0f2SMatthew Ahrens if (td->td_flags & TRAVERSE_PRE) { 2731b912ec7SGeorge Wilson err = td->td_func(td->td_spa, NULL, bp, zb, dnp, 2743f9d6ad7SLin Ling td->td_arg); 27599d5e173STim Haley if (err == TRAVERSE_VISIT_NO_CHILDREN) 27699d5e173STim Haley return (0); 277ad135b5dSChristopher Siden if (err != 0) 278ad135b5dSChristopher Siden goto post; 279fa9e4066Sahrens } 280fa9e4066Sahrens 28188b7b0f2SMatthew Ahrens if (BP_GET_LEVEL(bp) > 0) { 28288b7b0f2SMatthew Ahrens uint32_t flags = ARC_WAIT; 28388b7b0f2SMatthew Ahrens int i; 28488b7b0f2SMatthew Ahrens blkptr_t *cbp; 28588b7b0f2SMatthew Ahrens int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT; 28688b7b0f2SMatthew Ahrens 2871b912ec7SGeorge Wilson err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf, 28888b7b0f2SMatthew Ahrens ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb); 2893b2aab18SMatthew Ahrens if (err != 0) 2907fd05ac4SMatthew Ahrens goto post; 291b4709335SMatthew Ahrens cbp = buf->b_data; 292b4709335SMatthew Ahrens 293b4709335SMatthew Ahrens for (i = 0; i < epb; i++) { 294b4709335SMatthew Ahrens SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object, 295b4709335SMatthew Ahrens zb->zb_level - 1, 296b4709335SMatthew Ahrens zb->zb_blkid * epb + i); 2971b912ec7SGeorge Wilson traverse_prefetch_metadata(td, &cbp[i], &czb); 298b4709335SMatthew Ahrens } 29988b7b0f2SMatthew Ahrens 30088b7b0f2SMatthew Ahrens /* recursively visitbp() blocks below this */ 301b4709335SMatthew Ahrens for (i = 0; i < epb; i++) { 30288b7b0f2SMatthew Ahrens SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object, 30388b7b0f2SMatthew Ahrens zb->zb_level - 1, 30488b7b0f2SMatthew Ahrens zb->zb_blkid * epb + i); 3051b912ec7SGeorge Wilson err = traverse_visitbp(td, dnp, &cbp[i], &czb); 3067fd05ac4SMatthew Ahrens if (err != 0) 3077fd05ac4SMatthew Ahrens break; 30888b7b0f2SMatthew Ahrens } 30988b7b0f2SMatthew Ahrens } else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) { 31088b7b0f2SMatthew Ahrens uint32_t flags = ARC_WAIT; 31114843421SMatthew Ahrens int i; 31288b7b0f2SMatthew Ahrens int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT; 31388b7b0f2SMatthew Ahrens 3141b912ec7SGeorge Wilson err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf, 31588b7b0f2SMatthew Ahrens ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb); 3163b2aab18SMatthew Ahrens if (err != 0) 3177fd05ac4SMatthew Ahrens goto post; 318b4709335SMatthew Ahrens dnp = buf->b_data; 319b4709335SMatthew Ahrens 320b4709335SMatthew Ahrens for (i = 0; i < epb; i++) { 3211b912ec7SGeorge Wilson prefetch_dnode_metadata(td, &dnp[i], zb->zb_objset, 322b4709335SMatthew Ahrens zb->zb_blkid * epb + i); 323b4709335SMatthew Ahrens } 32488b7b0f2SMatthew Ahrens 32588b7b0f2SMatthew Ahrens /* recursively visitbp() blocks below this */ 326b4709335SMatthew Ahrens for (i = 0; i < epb; i++) { 3271b912ec7SGeorge Wilson err = traverse_dnode(td, &dnp[i], zb->zb_objset, 32814843421SMatthew Ahrens zb->zb_blkid * epb + i); 3297fd05ac4SMatthew Ahrens if (err != 0) 3307fd05ac4SMatthew Ahrens break; 331fa9e4066Sahrens } 33288b7b0f2SMatthew Ahrens } else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) { 33388b7b0f2SMatthew Ahrens uint32_t flags = ARC_WAIT; 33488b7b0f2SMatthew Ahrens objset_phys_t *osp; 33514843421SMatthew Ahrens dnode_phys_t *dnp; 33688b7b0f2SMatthew Ahrens 3371b912ec7SGeorge Wilson err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf, 33888b7b0f2SMatthew Ahrens ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb); 3393b2aab18SMatthew Ahrens if (err != 0) 3407fd05ac4SMatthew Ahrens goto post; 34188b7b0f2SMatthew Ahrens 34288b7b0f2SMatthew Ahrens osp = buf->b_data; 34314843421SMatthew Ahrens dnp = &osp->os_meta_dnode; 3441b912ec7SGeorge Wilson prefetch_dnode_metadata(td, dnp, zb->zb_objset, 345b4709335SMatthew Ahrens DMU_META_DNODE_OBJECT); 346b4709335SMatthew Ahrens if (arc_buf_size(buf) >= sizeof (objset_phys_t)) { 347b4709335SMatthew Ahrens prefetch_dnode_metadata(td, &osp->os_groupused_dnode, 34848f1b90eSMatthew Ahrens zb->zb_objset, DMU_GROUPUSED_OBJECT); 34948f1b90eSMatthew Ahrens prefetch_dnode_metadata(td, &osp->os_userused_dnode, 3501b912ec7SGeorge Wilson zb->zb_objset, DMU_USERUSED_OBJECT); 351b4709335SMatthew Ahrens } 352b4709335SMatthew Ahrens 3531b912ec7SGeorge Wilson err = traverse_dnode(td, dnp, zb->zb_objset, 354b24ab676SJeff Bonwick DMU_META_DNODE_OBJECT); 35514843421SMatthew Ahrens if (err == 0 && arc_buf_size(buf) >= sizeof (objset_phys_t)) { 35648f1b90eSMatthew Ahrens dnp = &osp->os_groupused_dnode; 3571b912ec7SGeorge Wilson err = traverse_dnode(td, dnp, zb->zb_objset, 35848f1b90eSMatthew Ahrens DMU_GROUPUSED_OBJECT); 35914843421SMatthew Ahrens } 36014843421SMatthew Ahrens if (err == 0 && arc_buf_size(buf) >= sizeof (objset_phys_t)) { 36148f1b90eSMatthew Ahrens dnp = &osp->os_userused_dnode; 3621b912ec7SGeorge Wilson err = traverse_dnode(td, dnp, zb->zb_objset, 36348f1b90eSMatthew Ahrens DMU_USERUSED_OBJECT); 36488b7b0f2SMatthew Ahrens } 36588b7b0f2SMatthew Ahrens } 366fa9e4066Sahrens 36788b7b0f2SMatthew Ahrens if (buf) 36888b7b0f2SMatthew Ahrens (void) arc_buf_remove_ref(buf, &buf); 369fa9e4066Sahrens 370ad135b5dSChristopher Siden post: 3717fd05ac4SMatthew Ahrens if (err == 0 && (td->td_flags & TRAVERSE_POST)) 3721b912ec7SGeorge Wilson err = td->td_func(td->td_spa, NULL, bp, zb, dnp, td->td_arg); 3737fd05ac4SMatthew Ahrens 3747fd05ac4SMatthew Ahrens if (hard && (err == EIO || err == ECKSUM)) { 3757fd05ac4SMatthew Ahrens /* 3767fd05ac4SMatthew Ahrens * Ignore this disk error as requested by the HARD flag, 3777fd05ac4SMatthew Ahrens * and continue traversal. 3787fd05ac4SMatthew Ahrens */ 3797fd05ac4SMatthew Ahrens err = 0; 380ad135b5dSChristopher Siden } 381ad135b5dSChristopher Siden 3827fd05ac4SMatthew Ahrens /* 3837fd05ac4SMatthew Ahrens * If we are stopping here, set td_resume. 3847fd05ac4SMatthew Ahrens */ 3857fd05ac4SMatthew Ahrens if (td->td_resume != NULL && err != 0 && !td->td_paused) { 3867fd05ac4SMatthew Ahrens td->td_resume->zb_objset = zb->zb_objset; 3877fd05ac4SMatthew Ahrens td->td_resume->zb_object = zb->zb_object; 3887fd05ac4SMatthew Ahrens td->td_resume->zb_level = 0; 3897fd05ac4SMatthew Ahrens /* 3907fd05ac4SMatthew Ahrens * If we have stopped on an indirect block (e.g. due to 3917fd05ac4SMatthew Ahrens * i/o error), we have not visited anything below it. 3927fd05ac4SMatthew Ahrens * Set the bookmark to the first level-0 block that we need 3937fd05ac4SMatthew Ahrens * to visit. This way, the resuming code does not need to 3947fd05ac4SMatthew Ahrens * deal with resuming from indirect blocks. 3957fd05ac4SMatthew Ahrens */ 3967fd05ac4SMatthew Ahrens td->td_resume->zb_blkid = zb->zb_blkid << 3977fd05ac4SMatthew Ahrens (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT)); 3987fd05ac4SMatthew Ahrens td->td_paused = B_TRUE; 3993f9d6ad7SLin Ling } 400fa9e4066Sahrens 4017fd05ac4SMatthew Ahrens return (err); 402fa9e4066Sahrens } 403fa9e4066Sahrens 404b4709335SMatthew Ahrens static void 405b4709335SMatthew Ahrens prefetch_dnode_metadata(traverse_data_t *td, const dnode_phys_t *dnp, 4061b912ec7SGeorge Wilson uint64_t objset, uint64_t object) 407b4709335SMatthew Ahrens { 408b4709335SMatthew Ahrens int j; 409*7802d7bfSMatthew Ahrens zbookmark_phys_t czb; 410b4709335SMatthew Ahrens 411b4709335SMatthew Ahrens for (j = 0; j < dnp->dn_nblkptr; j++) { 412b4709335SMatthew Ahrens SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j); 4131b912ec7SGeorge Wilson traverse_prefetch_metadata(td, &dnp->dn_blkptr[j], &czb); 414b4709335SMatthew Ahrens } 415b4709335SMatthew Ahrens 416b4709335SMatthew Ahrens if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) { 417b4709335SMatthew Ahrens SET_BOOKMARK(&czb, objset, object, 0, DMU_SPILL_BLKID); 4181b912ec7SGeorge Wilson traverse_prefetch_metadata(td, &dnp->dn_spill, &czb); 419b4709335SMatthew Ahrens } 420b4709335SMatthew Ahrens } 421b4709335SMatthew Ahrens 42214843421SMatthew Ahrens static int 4236e0cbcaaSMatthew Ahrens traverse_dnode(traverse_data_t *td, const dnode_phys_t *dnp, 4241b912ec7SGeorge Wilson uint64_t objset, uint64_t object) 42514843421SMatthew Ahrens { 4267fd05ac4SMatthew Ahrens int j, err = 0; 427*7802d7bfSMatthew Ahrens zbookmark_phys_t czb; 42814843421SMatthew Ahrens 42914843421SMatthew Ahrens for (j = 0; j < dnp->dn_nblkptr; j++) { 43014843421SMatthew Ahrens SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j); 4311b912ec7SGeorge Wilson err = traverse_visitbp(td, dnp, &dnp->dn_blkptr[j], &czb); 4327fd05ac4SMatthew Ahrens if (err != 0) 4337fd05ac4SMatthew Ahrens break; 4343f9d6ad7SLin Ling } 4353f9d6ad7SLin Ling 4363f9d6ad7SLin Ling if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) { 437b4709335SMatthew Ahrens SET_BOOKMARK(&czb, objset, object, 0, DMU_SPILL_BLKID); 4381b912ec7SGeorge Wilson err = traverse_visitbp(td, dnp, &dnp->dn_spill, &czb); 43914843421SMatthew Ahrens } 4407fd05ac4SMatthew Ahrens return (err); 44114843421SMatthew Ahrens } 44214843421SMatthew Ahrens 44388b7b0f2SMatthew Ahrens /* ARGSUSED */ 44488b7b0f2SMatthew Ahrens static int 445b24ab676SJeff Bonwick traverse_prefetcher(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 446*7802d7bfSMatthew Ahrens const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) 447e7cbe64fSgw { 4486e0cbcaaSMatthew Ahrens prefetch_data_t *pfd = arg; 44988b7b0f2SMatthew Ahrens uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH; 450e7cbe64fSgw 45188b7b0f2SMatthew Ahrens ASSERT(pfd->pd_blks_fetched >= 0); 45288b7b0f2SMatthew Ahrens if (pfd->pd_cancel) 453be6fd75aSMatthew Ahrens return (SET_ERROR(EINTR)); 454e7cbe64fSgw 45506315b79SMatthew Ahrens if (!prefetch_needed(pfd, bp)) 456fa9e4066Sahrens return (0); 457fa9e4066Sahrens 45888b7b0f2SMatthew Ahrens mutex_enter(&pfd->pd_mtx); 45988b7b0f2SMatthew Ahrens while (!pfd->pd_cancel && pfd->pd_blks_fetched >= pfd->pd_blks_max) 46088b7b0f2SMatthew Ahrens cv_wait(&pfd->pd_cv, &pfd->pd_mtx); 46188b7b0f2SMatthew Ahrens pfd->pd_blks_fetched++; 46288b7b0f2SMatthew Ahrens cv_broadcast(&pfd->pd_cv); 46388b7b0f2SMatthew Ahrens mutex_exit(&pfd->pd_mtx); 464fa9e4066Sahrens 4651b912ec7SGeorge Wilson (void) arc_read(NULL, spa, bp, NULL, NULL, ZIO_PRIORITY_ASYNC_READ, 4661b912ec7SGeorge Wilson ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &aflags, zb); 467fa9e4066Sahrens 46888b7b0f2SMatthew Ahrens return (0); 469fa9e4066Sahrens } 470fa9e4066Sahrens 471fa9e4066Sahrens static void 47288b7b0f2SMatthew Ahrens traverse_prefetch_thread(void *arg) 473fa9e4066Sahrens { 4746e0cbcaaSMatthew Ahrens traverse_data_t *td_main = arg; 4756e0cbcaaSMatthew Ahrens traverse_data_t td = *td_main; 476*7802d7bfSMatthew Ahrens zbookmark_phys_t czb; 477fa9e4066Sahrens 47888b7b0f2SMatthew Ahrens td.td_func = traverse_prefetcher; 47988b7b0f2SMatthew Ahrens td.td_arg = td_main->td_pfd; 48088b7b0f2SMatthew Ahrens td.td_pfd = NULL; 481fa9e4066Sahrens 482b24ab676SJeff Bonwick SET_BOOKMARK(&czb, td.td_objset, 483b24ab676SJeff Bonwick ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 4841b912ec7SGeorge Wilson (void) traverse_visitbp(&td, NULL, td.td_rootbp, &czb); 485fa9e4066Sahrens 48688b7b0f2SMatthew Ahrens mutex_enter(&td_main->td_pfd->pd_mtx); 48788b7b0f2SMatthew Ahrens td_main->td_pfd->pd_exited = B_TRUE; 48888b7b0f2SMatthew Ahrens cv_broadcast(&td_main->td_pfd->pd_cv); 48988b7b0f2SMatthew Ahrens mutex_exit(&td_main->td_pfd->pd_mtx); 490fa9e4066Sahrens } 491fa9e4066Sahrens 49288b7b0f2SMatthew Ahrens /* 49388b7b0f2SMatthew Ahrens * NB: dataset must not be changing on-disk (eg, is a snapshot or we are 49488b7b0f2SMatthew Ahrens * in syncing context). 49588b7b0f2SMatthew Ahrens */ 49688b7b0f2SMatthew Ahrens static int 497ad135b5dSChristopher Siden traverse_impl(spa_t *spa, dsl_dataset_t *ds, uint64_t objset, blkptr_t *rootbp, 498*7802d7bfSMatthew Ahrens uint64_t txg_start, zbookmark_phys_t *resume, int flags, 499ad135b5dSChristopher Siden blkptr_cb_t func, void *arg) 500fa9e4066Sahrens { 5016e0cbcaaSMatthew Ahrens traverse_data_t td; 5026e0cbcaaSMatthew Ahrens prefetch_data_t pd = { 0 }; 503*7802d7bfSMatthew Ahrens zbookmark_phys_t czb; 50488b7b0f2SMatthew Ahrens int err; 505fa9e4066Sahrens 506ad135b5dSChristopher Siden ASSERT(ds == NULL || objset == ds->ds_object); 507ad135b5dSChristopher Siden ASSERT(!(flags & TRAVERSE_PRE) || !(flags & TRAVERSE_POST)); 508ad135b5dSChristopher Siden 509b4709335SMatthew Ahrens /* 510b4709335SMatthew Ahrens * The data prefetching mechanism (the prefetch thread) is incompatible 511b4709335SMatthew Ahrens * with resuming from a bookmark. 512b4709335SMatthew Ahrens */ 513b4709335SMatthew Ahrens ASSERT(resume == NULL || !(flags & TRAVERSE_PREFETCH_DATA)); 514b4709335SMatthew Ahrens 51588b7b0f2SMatthew Ahrens td.td_spa = spa; 516ad135b5dSChristopher Siden td.td_objset = objset; 51788b7b0f2SMatthew Ahrens td.td_rootbp = rootbp; 51888b7b0f2SMatthew Ahrens td.td_min_txg = txg_start; 519ad135b5dSChristopher Siden td.td_resume = resume; 52088b7b0f2SMatthew Ahrens td.td_func = func; 52188b7b0f2SMatthew Ahrens td.td_arg = arg; 52288b7b0f2SMatthew Ahrens td.td_pfd = &pd; 52388b7b0f2SMatthew Ahrens td.td_flags = flags; 5247fd05ac4SMatthew Ahrens td.td_paused = B_FALSE; 52588b7b0f2SMatthew Ahrens 52644f92b7dSChris Kirby pd.pd_blks_max = zfs_pd_blks_max; 52788b7b0f2SMatthew Ahrens pd.pd_flags = flags; 52888b7b0f2SMatthew Ahrens mutex_init(&pd.pd_mtx, NULL, MUTEX_DEFAULT, NULL); 52988b7b0f2SMatthew Ahrens cv_init(&pd.pd_cv, NULL, CV_DEFAULT, NULL); 53088b7b0f2SMatthew Ahrens 5316e0cbcaaSMatthew Ahrens /* See comment on ZIL traversal in dsl_scan_visitds. */ 5323b2aab18SMatthew Ahrens if (ds != NULL && !dsl_dataset_is_snapshot(ds) && !BP_IS_HOLE(rootbp)) { 5333b2aab18SMatthew Ahrens uint32_t flags = ARC_WAIT; 5343b2aab18SMatthew Ahrens objset_phys_t *osp; 5353b2aab18SMatthew Ahrens arc_buf_t *buf; 5366e0cbcaaSMatthew Ahrens 5373b2aab18SMatthew Ahrens err = arc_read(NULL, td.td_spa, rootbp, 5383b2aab18SMatthew Ahrens arc_getbuf_func, &buf, 5393b2aab18SMatthew Ahrens ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, NULL); 5403b2aab18SMatthew Ahrens if (err != 0) 5416e0cbcaaSMatthew Ahrens return (err); 5426e0cbcaaSMatthew Ahrens 5433b2aab18SMatthew Ahrens osp = buf->b_data; 5443b2aab18SMatthew Ahrens traverse_zil(&td, &osp->os_zil_header); 5453b2aab18SMatthew Ahrens (void) arc_buf_remove_ref(buf, &buf); 5466e0cbcaaSMatthew Ahrens } 5476e0cbcaaSMatthew Ahrens 548b4709335SMatthew Ahrens if (!(flags & TRAVERSE_PREFETCH_DATA) || 54988b7b0f2SMatthew Ahrens 0 == taskq_dispatch(system_taskq, traverse_prefetch_thread, 55088b7b0f2SMatthew Ahrens &td, TQ_NOQUEUE)) 55188b7b0f2SMatthew Ahrens pd.pd_exited = B_TRUE; 55288b7b0f2SMatthew Ahrens 5536e0cbcaaSMatthew Ahrens SET_BOOKMARK(&czb, td.td_objset, 554b24ab676SJeff Bonwick ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 5551b912ec7SGeorge Wilson err = traverse_visitbp(&td, NULL, rootbp, &czb); 55688b7b0f2SMatthew Ahrens 55788b7b0f2SMatthew Ahrens mutex_enter(&pd.pd_mtx); 55888b7b0f2SMatthew Ahrens pd.pd_cancel = B_TRUE; 55988b7b0f2SMatthew Ahrens cv_broadcast(&pd.pd_cv); 56088b7b0f2SMatthew Ahrens while (!pd.pd_exited) 56188b7b0f2SMatthew Ahrens cv_wait(&pd.pd_cv, &pd.pd_mtx); 56288b7b0f2SMatthew Ahrens mutex_exit(&pd.pd_mtx); 56388b7b0f2SMatthew Ahrens 56488b7b0f2SMatthew Ahrens mutex_destroy(&pd.pd_mtx); 56588b7b0f2SMatthew Ahrens cv_destroy(&pd.pd_cv); 566fa9e4066Sahrens 56788b7b0f2SMatthew Ahrens return (err); 568fa9e4066Sahrens } 569fa9e4066Sahrens 57088b7b0f2SMatthew Ahrens /* 57188b7b0f2SMatthew Ahrens * NB: dataset must not be changing on-disk (eg, is a snapshot or we are 57288b7b0f2SMatthew Ahrens * in syncing context). 57388b7b0f2SMatthew Ahrens */ 57488b7b0f2SMatthew Ahrens int 57588b7b0f2SMatthew Ahrens traverse_dataset(dsl_dataset_t *ds, uint64_t txg_start, int flags, 57688b7b0f2SMatthew Ahrens blkptr_cb_t func, void *arg) 577fa9e4066Sahrens { 578ad135b5dSChristopher Siden return (traverse_impl(ds->ds_dir->dd_pool->dp_spa, ds, ds->ds_object, 579ad135b5dSChristopher Siden &ds->ds_phys->ds_bp, txg_start, NULL, flags, func, arg)); 580ad135b5dSChristopher Siden } 581ad135b5dSChristopher Siden 582ad135b5dSChristopher Siden int 583ad135b5dSChristopher Siden traverse_dataset_destroyed(spa_t *spa, blkptr_t *blkptr, 584*7802d7bfSMatthew Ahrens uint64_t txg_start, zbookmark_phys_t *resume, int flags, 585ad135b5dSChristopher Siden blkptr_cb_t func, void *arg) 586ad135b5dSChristopher Siden { 587ad135b5dSChristopher Siden return (traverse_impl(spa, NULL, ZB_DESTROYED_OBJSET, 588ad135b5dSChristopher Siden blkptr, txg_start, resume, flags, func, arg)); 589fa9e4066Sahrens } 590fa9e4066Sahrens 59188b7b0f2SMatthew Ahrens /* 59288b7b0f2SMatthew Ahrens * NB: pool must not be changing on-disk (eg, from zdb or sync context). 59388b7b0f2SMatthew Ahrens */ 59488b7b0f2SMatthew Ahrens int 595bbfd46c4SJeff Bonwick traverse_pool(spa_t *spa, uint64_t txg_start, int flags, 596bbfd46c4SJeff Bonwick blkptr_cb_t func, void *arg) 597fa9e4066Sahrens { 5987fd05ac4SMatthew Ahrens int err; 59988b7b0f2SMatthew Ahrens uint64_t obj; 60088b7b0f2SMatthew Ahrens dsl_pool_t *dp = spa_get_dsl(spa); 60188b7b0f2SMatthew Ahrens objset_t *mos = dp->dp_meta_objset; 602cd088ea4SVictor Latushkin boolean_t hard = (flags & TRAVERSE_HARD); 60388b7b0f2SMatthew Ahrens 60488b7b0f2SMatthew Ahrens /* visit the MOS */ 605ad135b5dSChristopher Siden err = traverse_impl(spa, NULL, 0, spa_get_rootblkptr(spa), 606ad135b5dSChristopher Siden txg_start, NULL, flags, func, arg); 6073b2aab18SMatthew Ahrens if (err != 0) 60888b7b0f2SMatthew Ahrens return (err); 60988b7b0f2SMatthew Ahrens 61088b7b0f2SMatthew Ahrens /* visit each dataset */ 6117fd05ac4SMatthew Ahrens for (obj = 1; err == 0; 612cd088ea4SVictor Latushkin err = dmu_object_next(mos, &obj, FALSE, txg_start)) { 61388b7b0f2SMatthew Ahrens dmu_object_info_t doi; 61488b7b0f2SMatthew Ahrens 61588b7b0f2SMatthew Ahrens err = dmu_object_info(mos, obj, &doi); 6163b2aab18SMatthew Ahrens if (err != 0) { 6177fd05ac4SMatthew Ahrens if (hard) 6187fd05ac4SMatthew Ahrens continue; 6197fd05ac4SMatthew Ahrens break; 620cd088ea4SVictor Latushkin } 62188b7b0f2SMatthew Ahrens 6222acef22dSMatthew Ahrens if (doi.doi_bonus_type == DMU_OT_DSL_DATASET) { 62388b7b0f2SMatthew Ahrens dsl_dataset_t *ds; 624468c413aSTim Haley uint64_t txg = txg_start; 625468c413aSTim Haley 6263b2aab18SMatthew Ahrens dsl_pool_config_enter(dp, FTAG); 62788b7b0f2SMatthew Ahrens err = dsl_dataset_hold_obj(dp, obj, FTAG, &ds); 6283b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 6293b2aab18SMatthew Ahrens if (err != 0) { 6307fd05ac4SMatthew Ahrens if (hard) 6317fd05ac4SMatthew Ahrens continue; 6327fd05ac4SMatthew Ahrens break; 633cd088ea4SVictor Latushkin } 634468c413aSTim Haley if (ds->ds_phys->ds_prev_snap_txg > txg) 635468c413aSTim Haley txg = ds->ds_phys->ds_prev_snap_txg; 636bbfd46c4SJeff Bonwick err = traverse_dataset(ds, txg, flags, func, arg); 63788b7b0f2SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 6387fd05ac4SMatthew Ahrens if (err != 0) 6397fd05ac4SMatthew Ahrens break; 64088b7b0f2SMatthew Ahrens } 641fa9e4066Sahrens } 64288b7b0f2SMatthew Ahrens if (err == ESRCH) 64388b7b0f2SMatthew Ahrens err = 0; 6447fd05ac4SMatthew Ahrens return (err); 645fa9e4066Sahrens } 646