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; 587802d7bfSMatthew Ahrens zbookmark_phys_t *td_resume; 5988b7b0f2SMatthew Ahrens int td_flags; 606e0cbcaaSMatthew Ahrens prefetch_data_t *td_pfd; 617fd05ac4SMatthew Ahrens boolean_t td_paused; 62f7950bf1SMatthew Ahrens uint64_t td_hole_birth_enabled_txg; 6388b7b0f2SMatthew Ahrens blkptr_cb_t *td_func; 6488b7b0f2SMatthew Ahrens void *td_arg; 656e0cbcaaSMatthew Ahrens } traverse_data_t; 66fa9e4066Sahrens 676e0cbcaaSMatthew Ahrens static int traverse_dnode(traverse_data_t *td, const dnode_phys_t *dnp, 681b912ec7SGeorge Wilson uint64_t objset, uint64_t object); 69b4709335SMatthew Ahrens static void prefetch_dnode_metadata(traverse_data_t *td, const dnode_phys_t *, 701b912ec7SGeorge Wilson uint64_t objset, uint64_t object); 7114843421SMatthew Ahrens 72b24ab676SJeff Bonwick static int 735dabedeeSbonwick traverse_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg) 74ea8dc4b6Seschrock { 756e0cbcaaSMatthew Ahrens traverse_data_t *td = arg; 767802d7bfSMatthew Ahrens zbookmark_phys_t zb; 77ea8dc4b6Seschrock 7843466aaeSMax Grossman if (BP_IS_HOLE(bp)) 79b24ab676SJeff Bonwick return (0); 805dabedeeSbonwick 8188b7b0f2SMatthew Ahrens if (claim_txg == 0 && bp->blk_birth >= spa_first_txg(td->td_spa)) 82b24ab676SJeff Bonwick return (0); 8388b7b0f2SMatthew Ahrens 84b24ab676SJeff Bonwick SET_BOOKMARK(&zb, td->td_objset, ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, 85b24ab676SJeff Bonwick bp->blk_cksum.zc_word[ZIL_ZC_SEQ]); 86b24ab676SJeff Bonwick 871b912ec7SGeorge Wilson (void) td->td_func(td->td_spa, zilog, bp, &zb, NULL, td->td_arg); 88b24ab676SJeff Bonwick 89b24ab676SJeff Bonwick return (0); 90ea8dc4b6Seschrock } 91ea8dc4b6Seschrock 92b24ab676SJeff Bonwick static int 935dabedeeSbonwick traverse_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg) 94ea8dc4b6Seschrock { 956e0cbcaaSMatthew Ahrens traverse_data_t *td = arg; 96ea8dc4b6Seschrock 97ea8dc4b6Seschrock if (lrc->lrc_txtype == TX_WRITE) { 98ea8dc4b6Seschrock lr_write_t *lr = (lr_write_t *)lrc; 99ea8dc4b6Seschrock blkptr_t *bp = &lr->lr_blkptr; 1007802d7bfSMatthew Ahrens zbookmark_phys_t zb; 101ea8dc4b6Seschrock 10243466aaeSMax Grossman if (BP_IS_HOLE(bp)) 103b24ab676SJeff Bonwick return (0); 1045dabedeeSbonwick 10588b7b0f2SMatthew Ahrens if (claim_txg == 0 || bp->blk_birth < claim_txg) 106b24ab676SJeff Bonwick return (0); 107b24ab676SJeff Bonwick 1086e0cbcaaSMatthew Ahrens SET_BOOKMARK(&zb, td->td_objset, lr->lr_foid, 1096e0cbcaaSMatthew Ahrens ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp)); 11088b7b0f2SMatthew Ahrens 1111b912ec7SGeorge Wilson (void) td->td_func(td->td_spa, zilog, bp, &zb, NULL, 112b24ab676SJeff Bonwick td->td_arg); 113ea8dc4b6Seschrock } 114b24ab676SJeff Bonwick return (0); 115ea8dc4b6Seschrock } 116ea8dc4b6Seschrock 117ea8dc4b6Seschrock static void 1186e0cbcaaSMatthew Ahrens traverse_zil(traverse_data_t *td, zil_header_t *zh) 119ea8dc4b6Seschrock { 1205dabedeeSbonwick uint64_t claim_txg = zh->zh_claim_txg; 121ea8dc4b6Seschrock zilog_t *zilog; 122ea8dc4b6Seschrock 1235dabedeeSbonwick /* 1245dabedeeSbonwick * We only want to visit blocks that have been claimed but not yet 125b24ab676SJeff Bonwick * replayed; plus, in read-only mode, blocks that are already stable. 1265dabedeeSbonwick */ 1278ad4d6ddSJeff Bonwick if (claim_txg == 0 && spa_writeable(td->td_spa)) 1285dabedeeSbonwick return; 1295dabedeeSbonwick 13088b7b0f2SMatthew Ahrens zilog = zil_alloc(spa_get_dsl(td->td_spa)->dp_meta_objset, zh); 131ea8dc4b6Seschrock 13288b7b0f2SMatthew Ahrens (void) zil_parse(zilog, traverse_zil_block, traverse_zil_record, td, 1335dabedeeSbonwick claim_txg); 134ea8dc4b6Seschrock 135ea8dc4b6Seschrock zil_free(zilog); 136ea8dc4b6Seschrock } 137ea8dc4b6Seschrock 138ad135b5dSChristopher Siden typedef enum resume_skip { 139ad135b5dSChristopher Siden RESUME_SKIP_ALL, 140ad135b5dSChristopher Siden RESUME_SKIP_NONE, 141ad135b5dSChristopher Siden RESUME_SKIP_CHILDREN 142ad135b5dSChristopher Siden } resume_skip_t; 143ad135b5dSChristopher Siden 144ad135b5dSChristopher Siden /* 145ad135b5dSChristopher Siden * Returns RESUME_SKIP_ALL if td indicates that we are resuming a traversal and 146ad135b5dSChristopher Siden * the block indicated by zb does not need to be visited at all. Returns 147ad135b5dSChristopher Siden * RESUME_SKIP_CHILDREN if we are resuming a post traversal and we reach the 148ad135b5dSChristopher Siden * resume point. This indicates that this block should be visited but not its 149ad135b5dSChristopher Siden * children (since they must have been visited in a previous traversal). 150ad135b5dSChristopher Siden * Otherwise returns RESUME_SKIP_NONE. 151ad135b5dSChristopher Siden */ 152ad135b5dSChristopher Siden static resume_skip_t 153ad135b5dSChristopher Siden resume_skip_check(traverse_data_t *td, const dnode_phys_t *dnp, 1547802d7bfSMatthew Ahrens const zbookmark_phys_t *zb) 155ad135b5dSChristopher Siden { 156ad135b5dSChristopher Siden if (td->td_resume != NULL && !ZB_IS_ZERO(td->td_resume)) { 157ad135b5dSChristopher Siden /* 158ad135b5dSChristopher Siden * If we already visited this bp & everything below, 159ad135b5dSChristopher Siden * don't bother doing it again. 160ad135b5dSChristopher Siden */ 161ad135b5dSChristopher Siden if (zbookmark_is_before(dnp, zb, td->td_resume)) 162ad135b5dSChristopher Siden return (RESUME_SKIP_ALL); 163ad135b5dSChristopher Siden 164ad135b5dSChristopher Siden /* 165ad135b5dSChristopher Siden * If we found the block we're trying to resume from, zero 166ad135b5dSChristopher Siden * the bookmark out to indicate that we have resumed. 167ad135b5dSChristopher Siden */ 168ad135b5dSChristopher Siden if (bcmp(zb, td->td_resume, sizeof (*zb)) == 0) { 169ad135b5dSChristopher Siden bzero(td->td_resume, sizeof (*zb)); 170ad135b5dSChristopher Siden if (td->td_flags & TRAVERSE_POST) 171ad135b5dSChristopher Siden return (RESUME_SKIP_CHILDREN); 172ad135b5dSChristopher Siden } 173ad135b5dSChristopher Siden } 174ad135b5dSChristopher Siden return (RESUME_SKIP_NONE); 175ad135b5dSChristopher Siden } 176ad135b5dSChristopher Siden 177b4709335SMatthew Ahrens static void 178b4709335SMatthew Ahrens traverse_prefetch_metadata(traverse_data_t *td, 1797802d7bfSMatthew Ahrens const blkptr_t *bp, const zbookmark_phys_t *zb) 180b4709335SMatthew Ahrens { 1817adb730bSGeorge Wilson arc_flags_t flags = ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH; 182b4709335SMatthew Ahrens 183b4709335SMatthew Ahrens if (!(td->td_flags & TRAVERSE_PREFETCH_METADATA)) 184b4709335SMatthew Ahrens return; 185b4709335SMatthew Ahrens /* 186b4709335SMatthew Ahrens * If we are in the process of resuming, don't prefetch, because 187b4709335SMatthew Ahrens * some children will not be needed (and in fact may have already 188b4709335SMatthew Ahrens * been freed). 189b4709335SMatthew Ahrens */ 190b4709335SMatthew Ahrens if (td->td_resume != NULL && !ZB_IS_ZERO(td->td_resume)) 191b4709335SMatthew Ahrens return; 192b4709335SMatthew Ahrens if (BP_IS_HOLE(bp) || bp->blk_birth <= td->td_min_txg) 193b4709335SMatthew Ahrens return; 194b4709335SMatthew Ahrens if (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_DNODE) 195b4709335SMatthew Ahrens return; 196b4709335SMatthew Ahrens 1971b912ec7SGeorge Wilson (void) arc_read(NULL, td->td_spa, bp, NULL, NULL, 1981b912ec7SGeorge Wilson ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb); 199b4709335SMatthew Ahrens } 200b4709335SMatthew Ahrens 20106315b79SMatthew Ahrens static boolean_t 20206315b79SMatthew Ahrens prefetch_needed(prefetch_data_t *pfd, const blkptr_t *bp) 20306315b79SMatthew Ahrens { 20406315b79SMatthew Ahrens ASSERT(pfd->pd_flags & TRAVERSE_PREFETCH_DATA); 20506315b79SMatthew Ahrens if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp) || 20606315b79SMatthew Ahrens BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG) 20706315b79SMatthew Ahrens return (B_FALSE); 20806315b79SMatthew Ahrens return (B_TRUE); 20906315b79SMatthew Ahrens } 21006315b79SMatthew Ahrens 211fa9e4066Sahrens static int 2126e0cbcaaSMatthew Ahrens traverse_visitbp(traverse_data_t *td, const dnode_phys_t *dnp, 2137802d7bfSMatthew Ahrens const blkptr_t *bp, const zbookmark_phys_t *zb) 214fa9e4066Sahrens { 2157802d7bfSMatthew Ahrens zbookmark_phys_t czb; 2167fd05ac4SMatthew Ahrens int err = 0; 21788b7b0f2SMatthew Ahrens arc_buf_t *buf = NULL; 2186e0cbcaaSMatthew Ahrens prefetch_data_t *pd = td->td_pfd; 219cd088ea4SVictor Latushkin boolean_t hard = td->td_flags & TRAVERSE_HARD; 220ad135b5dSChristopher Siden 221ad135b5dSChristopher Siden switch (resume_skip_check(td, dnp, zb)) { 222ad135b5dSChristopher Siden case RESUME_SKIP_ALL: 223ad135b5dSChristopher Siden return (0); 224ad135b5dSChristopher Siden case RESUME_SKIP_CHILDREN: 225ad135b5dSChristopher Siden goto post; 226ad135b5dSChristopher Siden case RESUME_SKIP_NONE: 227ad135b5dSChristopher Siden break; 228ad135b5dSChristopher Siden default: 229ad135b5dSChristopher Siden ASSERT(0); 230ad135b5dSChristopher Siden } 231fa9e4066Sahrens 23243466aaeSMax Grossman if (bp->blk_birth == 0) { 233f7950bf1SMatthew Ahrens /* 234f7950bf1SMatthew Ahrens * Since this block has a birth time of 0 it must be a 235f7950bf1SMatthew Ahrens * hole created before the SPA_FEATURE_HOLE_BIRTH 236f7950bf1SMatthew Ahrens * feature was enabled. If SPA_FEATURE_HOLE_BIRTH 237f7950bf1SMatthew Ahrens * was enabled before the min_txg for this traveral we 238f7950bf1SMatthew Ahrens * know the hole must have been created before the 239f7950bf1SMatthew Ahrens * min_txg for this traveral, so we can skip it. If 240f7950bf1SMatthew Ahrens * SPA_FEATURE_HOLE_BIRTH was enabled after the min_txg 241f7950bf1SMatthew Ahrens * for this traveral we cannot tell if the hole was 242f7950bf1SMatthew Ahrens * created before or after the min_txg for this 243f7950bf1SMatthew Ahrens * traversal, so we cannot skip it. 244f7950bf1SMatthew Ahrens */ 245f7950bf1SMatthew Ahrens if (td->td_hole_birth_enabled_txg < td->td_min_txg) 246f7950bf1SMatthew Ahrens return (0); 24743466aaeSMax Grossman } else if (bp->blk_birth <= td->td_min_txg) { 24843466aaeSMax Grossman return (0); 24943466aaeSMax Grossman } 25043466aaeSMax Grossman 25106315b79SMatthew Ahrens if (pd != NULL && !pd->pd_exited && prefetch_needed(pd, bp)) { 25288b7b0f2SMatthew Ahrens mutex_enter(&pd->pd_mtx); 25388b7b0f2SMatthew Ahrens ASSERT(pd->pd_blks_fetched >= 0); 25488b7b0f2SMatthew Ahrens while (pd->pd_blks_fetched == 0 && !pd->pd_exited) 25588b7b0f2SMatthew Ahrens cv_wait(&pd->pd_cv, &pd->pd_mtx); 25688b7b0f2SMatthew Ahrens pd->pd_blks_fetched--; 25788b7b0f2SMatthew Ahrens cv_broadcast(&pd->pd_cv); 25888b7b0f2SMatthew Ahrens mutex_exit(&pd->pd_mtx); 259fa9e4066Sahrens } 260fa9e4066Sahrens 26106315b79SMatthew Ahrens if (BP_IS_HOLE(bp)) { 26206315b79SMatthew Ahrens err = td->td_func(td->td_spa, NULL, bp, zb, dnp, td->td_arg); 26306315b79SMatthew Ahrens if (err != 0) 26406315b79SMatthew Ahrens goto post; 26506315b79SMatthew Ahrens return (0); 26606315b79SMatthew Ahrens } 26706315b79SMatthew Ahrens 26888b7b0f2SMatthew Ahrens if (td->td_flags & TRAVERSE_PRE) { 2691b912ec7SGeorge Wilson err = td->td_func(td->td_spa, NULL, bp, zb, dnp, 2703f9d6ad7SLin Ling td->td_arg); 27199d5e173STim Haley if (err == TRAVERSE_VISIT_NO_CHILDREN) 27299d5e173STim Haley return (0); 273ad135b5dSChristopher Siden if (err != 0) 274ad135b5dSChristopher Siden goto post; 275fa9e4066Sahrens } 276fa9e4066Sahrens 27788b7b0f2SMatthew Ahrens if (BP_GET_LEVEL(bp) > 0) { 2787adb730bSGeorge Wilson arc_flags_t flags = ARC_FLAG_WAIT; 27988b7b0f2SMatthew Ahrens int i; 28088b7b0f2SMatthew Ahrens blkptr_t *cbp; 28188b7b0f2SMatthew Ahrens int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT; 28288b7b0f2SMatthew Ahrens 2831b912ec7SGeorge Wilson err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf, 28488b7b0f2SMatthew Ahrens ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb); 2853b2aab18SMatthew Ahrens if (err != 0) 2867fd05ac4SMatthew Ahrens goto post; 287b4709335SMatthew Ahrens cbp = buf->b_data; 288b4709335SMatthew Ahrens 289b4709335SMatthew Ahrens for (i = 0; i < epb; i++) { 290b4709335SMatthew Ahrens SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object, 291b4709335SMatthew Ahrens zb->zb_level - 1, 292b4709335SMatthew Ahrens zb->zb_blkid * epb + i); 2931b912ec7SGeorge Wilson traverse_prefetch_metadata(td, &cbp[i], &czb); 294b4709335SMatthew Ahrens } 29588b7b0f2SMatthew Ahrens 29688b7b0f2SMatthew Ahrens /* recursively visitbp() blocks below this */ 297b4709335SMatthew Ahrens for (i = 0; i < epb; i++) { 29888b7b0f2SMatthew Ahrens SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object, 29988b7b0f2SMatthew Ahrens zb->zb_level - 1, 30088b7b0f2SMatthew Ahrens zb->zb_blkid * epb + i); 3011b912ec7SGeorge Wilson err = traverse_visitbp(td, dnp, &cbp[i], &czb); 3027fd05ac4SMatthew Ahrens if (err != 0) 3037fd05ac4SMatthew Ahrens break; 30488b7b0f2SMatthew Ahrens } 30588b7b0f2SMatthew Ahrens } else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) { 3067adb730bSGeorge Wilson arc_flags_t flags = ARC_FLAG_WAIT; 30714843421SMatthew Ahrens int i; 30888b7b0f2SMatthew Ahrens int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT; 30988b7b0f2SMatthew Ahrens 3101b912ec7SGeorge Wilson err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf, 31188b7b0f2SMatthew Ahrens ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb); 3123b2aab18SMatthew Ahrens if (err != 0) 3137fd05ac4SMatthew Ahrens goto post; 314b4709335SMatthew Ahrens dnp = buf->b_data; 315b4709335SMatthew Ahrens 316b4709335SMatthew Ahrens for (i = 0; i < epb; i++) { 3171b912ec7SGeorge Wilson prefetch_dnode_metadata(td, &dnp[i], zb->zb_objset, 318b4709335SMatthew Ahrens zb->zb_blkid * epb + i); 319b4709335SMatthew Ahrens } 32088b7b0f2SMatthew Ahrens 32188b7b0f2SMatthew Ahrens /* recursively visitbp() blocks below this */ 322b4709335SMatthew Ahrens for (i = 0; i < epb; i++) { 3231b912ec7SGeorge Wilson err = traverse_dnode(td, &dnp[i], zb->zb_objset, 32414843421SMatthew Ahrens zb->zb_blkid * epb + i); 3257fd05ac4SMatthew Ahrens if (err != 0) 3267fd05ac4SMatthew Ahrens break; 327fa9e4066Sahrens } 32888b7b0f2SMatthew Ahrens } else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) { 3297adb730bSGeorge Wilson arc_flags_t flags = ARC_FLAG_WAIT; 33088b7b0f2SMatthew Ahrens objset_phys_t *osp; 33114843421SMatthew Ahrens dnode_phys_t *dnp; 33288b7b0f2SMatthew Ahrens 3331b912ec7SGeorge Wilson err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf, 33488b7b0f2SMatthew Ahrens ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb); 3353b2aab18SMatthew Ahrens if (err != 0) 3367fd05ac4SMatthew Ahrens goto post; 33788b7b0f2SMatthew Ahrens 33888b7b0f2SMatthew Ahrens osp = buf->b_data; 33914843421SMatthew Ahrens dnp = &osp->os_meta_dnode; 3401b912ec7SGeorge Wilson prefetch_dnode_metadata(td, dnp, zb->zb_objset, 341b4709335SMatthew Ahrens DMU_META_DNODE_OBJECT); 342b4709335SMatthew Ahrens if (arc_buf_size(buf) >= sizeof (objset_phys_t)) { 343b4709335SMatthew Ahrens prefetch_dnode_metadata(td, &osp->os_groupused_dnode, 34448f1b90eSMatthew Ahrens zb->zb_objset, DMU_GROUPUSED_OBJECT); 34548f1b90eSMatthew Ahrens prefetch_dnode_metadata(td, &osp->os_userused_dnode, 3461b912ec7SGeorge Wilson zb->zb_objset, DMU_USERUSED_OBJECT); 347b4709335SMatthew Ahrens } 348b4709335SMatthew Ahrens 3491b912ec7SGeorge Wilson err = traverse_dnode(td, dnp, zb->zb_objset, 350b24ab676SJeff Bonwick DMU_META_DNODE_OBJECT); 35114843421SMatthew Ahrens if (err == 0 && arc_buf_size(buf) >= sizeof (objset_phys_t)) { 35248f1b90eSMatthew Ahrens dnp = &osp->os_groupused_dnode; 3531b912ec7SGeorge Wilson err = traverse_dnode(td, dnp, zb->zb_objset, 35448f1b90eSMatthew Ahrens DMU_GROUPUSED_OBJECT); 35514843421SMatthew Ahrens } 35614843421SMatthew Ahrens if (err == 0 && arc_buf_size(buf) >= sizeof (objset_phys_t)) { 35748f1b90eSMatthew Ahrens dnp = &osp->os_userused_dnode; 3581b912ec7SGeorge Wilson err = traverse_dnode(td, dnp, zb->zb_objset, 35948f1b90eSMatthew Ahrens DMU_USERUSED_OBJECT); 36088b7b0f2SMatthew Ahrens } 36188b7b0f2SMatthew Ahrens } 362fa9e4066Sahrens 36388b7b0f2SMatthew Ahrens if (buf) 36488b7b0f2SMatthew Ahrens (void) arc_buf_remove_ref(buf, &buf); 365fa9e4066Sahrens 366ad135b5dSChristopher Siden post: 3677fd05ac4SMatthew Ahrens if (err == 0 && (td->td_flags & TRAVERSE_POST)) 3681b912ec7SGeorge Wilson err = td->td_func(td->td_spa, NULL, bp, zb, dnp, td->td_arg); 3697fd05ac4SMatthew Ahrens 3707fd05ac4SMatthew Ahrens if (hard && (err == EIO || err == ECKSUM)) { 3717fd05ac4SMatthew Ahrens /* 3727fd05ac4SMatthew Ahrens * Ignore this disk error as requested by the HARD flag, 3737fd05ac4SMatthew Ahrens * and continue traversal. 3747fd05ac4SMatthew Ahrens */ 3757fd05ac4SMatthew Ahrens err = 0; 376ad135b5dSChristopher Siden } 377ad135b5dSChristopher Siden 3787fd05ac4SMatthew Ahrens /* 3797fd05ac4SMatthew Ahrens * If we are stopping here, set td_resume. 3807fd05ac4SMatthew Ahrens */ 3817fd05ac4SMatthew Ahrens if (td->td_resume != NULL && err != 0 && !td->td_paused) { 3827fd05ac4SMatthew Ahrens td->td_resume->zb_objset = zb->zb_objset; 3837fd05ac4SMatthew Ahrens td->td_resume->zb_object = zb->zb_object; 3847fd05ac4SMatthew Ahrens td->td_resume->zb_level = 0; 3857fd05ac4SMatthew Ahrens /* 3867fd05ac4SMatthew Ahrens * If we have stopped on an indirect block (e.g. due to 3877fd05ac4SMatthew Ahrens * i/o error), we have not visited anything below it. 3887fd05ac4SMatthew Ahrens * Set the bookmark to the first level-0 block that we need 3897fd05ac4SMatthew Ahrens * to visit. This way, the resuming code does not need to 3907fd05ac4SMatthew Ahrens * deal with resuming from indirect blocks. 3917fd05ac4SMatthew Ahrens */ 3927fd05ac4SMatthew Ahrens td->td_resume->zb_blkid = zb->zb_blkid << 3937fd05ac4SMatthew Ahrens (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT)); 3947fd05ac4SMatthew Ahrens td->td_paused = B_TRUE; 3953f9d6ad7SLin Ling } 396fa9e4066Sahrens 3977fd05ac4SMatthew Ahrens return (err); 398fa9e4066Sahrens } 399fa9e4066Sahrens 400b4709335SMatthew Ahrens static void 401b4709335SMatthew Ahrens prefetch_dnode_metadata(traverse_data_t *td, const dnode_phys_t *dnp, 4021b912ec7SGeorge Wilson uint64_t objset, uint64_t object) 403b4709335SMatthew Ahrens { 404b4709335SMatthew Ahrens int j; 4057802d7bfSMatthew Ahrens zbookmark_phys_t czb; 406b4709335SMatthew Ahrens 407b4709335SMatthew Ahrens for (j = 0; j < dnp->dn_nblkptr; j++) { 408b4709335SMatthew Ahrens SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j); 4091b912ec7SGeorge Wilson traverse_prefetch_metadata(td, &dnp->dn_blkptr[j], &czb); 410b4709335SMatthew Ahrens } 411b4709335SMatthew Ahrens 412b4709335SMatthew Ahrens if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) { 413b4709335SMatthew Ahrens SET_BOOKMARK(&czb, objset, object, 0, DMU_SPILL_BLKID); 4141b912ec7SGeorge Wilson traverse_prefetch_metadata(td, &dnp->dn_spill, &czb); 415b4709335SMatthew Ahrens } 416b4709335SMatthew Ahrens } 417b4709335SMatthew Ahrens 41814843421SMatthew Ahrens static int 4196e0cbcaaSMatthew Ahrens traverse_dnode(traverse_data_t *td, const dnode_phys_t *dnp, 4201b912ec7SGeorge Wilson uint64_t objset, uint64_t object) 42114843421SMatthew Ahrens { 4227fd05ac4SMatthew Ahrens int j, err = 0; 4237802d7bfSMatthew Ahrens zbookmark_phys_t czb; 42414843421SMatthew Ahrens 42514843421SMatthew Ahrens for (j = 0; j < dnp->dn_nblkptr; j++) { 42614843421SMatthew Ahrens SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j); 4271b912ec7SGeorge Wilson err = traverse_visitbp(td, dnp, &dnp->dn_blkptr[j], &czb); 4287fd05ac4SMatthew Ahrens if (err != 0) 4297fd05ac4SMatthew Ahrens break; 4303f9d6ad7SLin Ling } 4313f9d6ad7SLin Ling 4322a89c2c5SJustin T. Gibbs if (err == 0 && dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) { 433b4709335SMatthew Ahrens SET_BOOKMARK(&czb, objset, object, 0, DMU_SPILL_BLKID); 4341b912ec7SGeorge Wilson err = traverse_visitbp(td, dnp, &dnp->dn_spill, &czb); 43514843421SMatthew Ahrens } 4367fd05ac4SMatthew Ahrens return (err); 43714843421SMatthew Ahrens } 43814843421SMatthew Ahrens 43988b7b0f2SMatthew Ahrens /* ARGSUSED */ 44088b7b0f2SMatthew Ahrens static int 441b24ab676SJeff Bonwick traverse_prefetcher(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 4427802d7bfSMatthew Ahrens const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) 443e7cbe64fSgw { 4446e0cbcaaSMatthew Ahrens prefetch_data_t *pfd = arg; 4457adb730bSGeorge Wilson arc_flags_t aflags = ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH; 446e7cbe64fSgw 44788b7b0f2SMatthew Ahrens ASSERT(pfd->pd_blks_fetched >= 0); 44888b7b0f2SMatthew Ahrens if (pfd->pd_cancel) 449be6fd75aSMatthew Ahrens return (SET_ERROR(EINTR)); 450e7cbe64fSgw 45106315b79SMatthew Ahrens if (!prefetch_needed(pfd, bp)) 452fa9e4066Sahrens return (0); 453fa9e4066Sahrens 45488b7b0f2SMatthew Ahrens mutex_enter(&pfd->pd_mtx); 45588b7b0f2SMatthew Ahrens while (!pfd->pd_cancel && pfd->pd_blks_fetched >= pfd->pd_blks_max) 45688b7b0f2SMatthew Ahrens cv_wait(&pfd->pd_cv, &pfd->pd_mtx); 45788b7b0f2SMatthew Ahrens pfd->pd_blks_fetched++; 45888b7b0f2SMatthew Ahrens cv_broadcast(&pfd->pd_cv); 45988b7b0f2SMatthew Ahrens mutex_exit(&pfd->pd_mtx); 460fa9e4066Sahrens 4611b912ec7SGeorge Wilson (void) arc_read(NULL, spa, bp, NULL, NULL, ZIO_PRIORITY_ASYNC_READ, 4621b912ec7SGeorge Wilson ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &aflags, zb); 463fa9e4066Sahrens 46488b7b0f2SMatthew Ahrens return (0); 465fa9e4066Sahrens } 466fa9e4066Sahrens 467fa9e4066Sahrens static void 46888b7b0f2SMatthew Ahrens traverse_prefetch_thread(void *arg) 469fa9e4066Sahrens { 4706e0cbcaaSMatthew Ahrens traverse_data_t *td_main = arg; 4716e0cbcaaSMatthew Ahrens traverse_data_t td = *td_main; 4727802d7bfSMatthew Ahrens zbookmark_phys_t czb; 473fa9e4066Sahrens 47488b7b0f2SMatthew Ahrens td.td_func = traverse_prefetcher; 47588b7b0f2SMatthew Ahrens td.td_arg = td_main->td_pfd; 47688b7b0f2SMatthew Ahrens td.td_pfd = NULL; 477fa9e4066Sahrens 478b24ab676SJeff Bonwick SET_BOOKMARK(&czb, td.td_objset, 479b24ab676SJeff Bonwick ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 4801b912ec7SGeorge Wilson (void) traverse_visitbp(&td, NULL, td.td_rootbp, &czb); 481fa9e4066Sahrens 48288b7b0f2SMatthew Ahrens mutex_enter(&td_main->td_pfd->pd_mtx); 48388b7b0f2SMatthew Ahrens td_main->td_pfd->pd_exited = B_TRUE; 48488b7b0f2SMatthew Ahrens cv_broadcast(&td_main->td_pfd->pd_cv); 48588b7b0f2SMatthew Ahrens mutex_exit(&td_main->td_pfd->pd_mtx); 486fa9e4066Sahrens } 487fa9e4066Sahrens 48888b7b0f2SMatthew Ahrens /* 48988b7b0f2SMatthew Ahrens * NB: dataset must not be changing on-disk (eg, is a snapshot or we are 49088b7b0f2SMatthew Ahrens * in syncing context). 49188b7b0f2SMatthew Ahrens */ 49288b7b0f2SMatthew Ahrens static int 493ad135b5dSChristopher Siden traverse_impl(spa_t *spa, dsl_dataset_t *ds, uint64_t objset, blkptr_t *rootbp, 4947802d7bfSMatthew Ahrens uint64_t txg_start, zbookmark_phys_t *resume, int flags, 495ad135b5dSChristopher Siden blkptr_cb_t func, void *arg) 496fa9e4066Sahrens { 4976e0cbcaaSMatthew Ahrens traverse_data_t td; 4986e0cbcaaSMatthew Ahrens prefetch_data_t pd = { 0 }; 4997802d7bfSMatthew Ahrens zbookmark_phys_t czb; 50088b7b0f2SMatthew Ahrens int err; 501fa9e4066Sahrens 502ad135b5dSChristopher Siden ASSERT(ds == NULL || objset == ds->ds_object); 503ad135b5dSChristopher Siden ASSERT(!(flags & TRAVERSE_PRE) || !(flags & TRAVERSE_POST)); 504ad135b5dSChristopher Siden 505b4709335SMatthew Ahrens /* 506b4709335SMatthew Ahrens * The data prefetching mechanism (the prefetch thread) is incompatible 507b4709335SMatthew Ahrens * with resuming from a bookmark. 508b4709335SMatthew Ahrens */ 509b4709335SMatthew Ahrens ASSERT(resume == NULL || !(flags & TRAVERSE_PREFETCH_DATA)); 510b4709335SMatthew Ahrens 51188b7b0f2SMatthew Ahrens td.td_spa = spa; 512ad135b5dSChristopher Siden td.td_objset = objset; 51388b7b0f2SMatthew Ahrens td.td_rootbp = rootbp; 51488b7b0f2SMatthew Ahrens td.td_min_txg = txg_start; 515ad135b5dSChristopher Siden td.td_resume = resume; 51688b7b0f2SMatthew Ahrens td.td_func = func; 51788b7b0f2SMatthew Ahrens td.td_arg = arg; 51888b7b0f2SMatthew Ahrens td.td_pfd = &pd; 51988b7b0f2SMatthew Ahrens td.td_flags = flags; 5207fd05ac4SMatthew Ahrens td.td_paused = B_FALSE; 52188b7b0f2SMatthew Ahrens 522f7950bf1SMatthew Ahrens if (spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) { 523f7950bf1SMatthew Ahrens VERIFY(spa_feature_enabled_txg(spa, 524f7950bf1SMatthew Ahrens SPA_FEATURE_HOLE_BIRTH, &td.td_hole_birth_enabled_txg)); 525f7950bf1SMatthew Ahrens } else { 526f7950bf1SMatthew Ahrens td.td_hole_birth_enabled_txg = 0; 527f7950bf1SMatthew Ahrens } 528f7950bf1SMatthew Ahrens 52944f92b7dSChris Kirby pd.pd_blks_max = zfs_pd_blks_max; 53088b7b0f2SMatthew Ahrens pd.pd_flags = flags; 53188b7b0f2SMatthew Ahrens mutex_init(&pd.pd_mtx, NULL, MUTEX_DEFAULT, NULL); 53288b7b0f2SMatthew Ahrens cv_init(&pd.pd_cv, NULL, CV_DEFAULT, NULL); 53388b7b0f2SMatthew Ahrens 5346e0cbcaaSMatthew Ahrens /* See comment on ZIL traversal in dsl_scan_visitds. */ 535*bc9014e6SJustin Gibbs if (ds != NULL && !ds->ds_is_snapshot && !BP_IS_HOLE(rootbp)) { 5367adb730bSGeorge Wilson arc_flags_t flags = ARC_FLAG_WAIT; 5373b2aab18SMatthew Ahrens objset_phys_t *osp; 5383b2aab18SMatthew Ahrens arc_buf_t *buf; 5396e0cbcaaSMatthew Ahrens 5403b2aab18SMatthew Ahrens err = arc_read(NULL, td.td_spa, rootbp, 5413b2aab18SMatthew Ahrens arc_getbuf_func, &buf, 5423b2aab18SMatthew Ahrens ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, NULL); 5433b2aab18SMatthew Ahrens if (err != 0) 5446e0cbcaaSMatthew Ahrens return (err); 5456e0cbcaaSMatthew Ahrens 5463b2aab18SMatthew Ahrens osp = buf->b_data; 5473b2aab18SMatthew Ahrens traverse_zil(&td, &osp->os_zil_header); 5483b2aab18SMatthew Ahrens (void) arc_buf_remove_ref(buf, &buf); 5496e0cbcaaSMatthew Ahrens } 5506e0cbcaaSMatthew Ahrens 551b4709335SMatthew Ahrens if (!(flags & TRAVERSE_PREFETCH_DATA) || 55288b7b0f2SMatthew Ahrens 0 == taskq_dispatch(system_taskq, traverse_prefetch_thread, 55388b7b0f2SMatthew Ahrens &td, TQ_NOQUEUE)) 55488b7b0f2SMatthew Ahrens pd.pd_exited = B_TRUE; 55588b7b0f2SMatthew Ahrens 5566e0cbcaaSMatthew Ahrens SET_BOOKMARK(&czb, td.td_objset, 557b24ab676SJeff Bonwick ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 5581b912ec7SGeorge Wilson err = traverse_visitbp(&td, NULL, rootbp, &czb); 55988b7b0f2SMatthew Ahrens 56088b7b0f2SMatthew Ahrens mutex_enter(&pd.pd_mtx); 56188b7b0f2SMatthew Ahrens pd.pd_cancel = B_TRUE; 56288b7b0f2SMatthew Ahrens cv_broadcast(&pd.pd_cv); 56388b7b0f2SMatthew Ahrens while (!pd.pd_exited) 56488b7b0f2SMatthew Ahrens cv_wait(&pd.pd_cv, &pd.pd_mtx); 56588b7b0f2SMatthew Ahrens mutex_exit(&pd.pd_mtx); 56688b7b0f2SMatthew Ahrens 56788b7b0f2SMatthew Ahrens mutex_destroy(&pd.pd_mtx); 56888b7b0f2SMatthew Ahrens cv_destroy(&pd.pd_cv); 569fa9e4066Sahrens 57088b7b0f2SMatthew Ahrens return (err); 571fa9e4066Sahrens } 572fa9e4066Sahrens 57388b7b0f2SMatthew Ahrens /* 57488b7b0f2SMatthew Ahrens * NB: dataset must not be changing on-disk (eg, is a snapshot or we are 57588b7b0f2SMatthew Ahrens * in syncing context). 57688b7b0f2SMatthew Ahrens */ 57788b7b0f2SMatthew Ahrens int 57888b7b0f2SMatthew Ahrens traverse_dataset(dsl_dataset_t *ds, uint64_t txg_start, int flags, 57988b7b0f2SMatthew Ahrens blkptr_cb_t func, void *arg) 580fa9e4066Sahrens { 581ad135b5dSChristopher Siden return (traverse_impl(ds->ds_dir->dd_pool->dp_spa, ds, ds->ds_object, 582c1379625SJustin T. Gibbs &dsl_dataset_phys(ds)->ds_bp, txg_start, NULL, flags, func, arg)); 583ad135b5dSChristopher Siden } 584ad135b5dSChristopher Siden 585ad135b5dSChristopher Siden int 586ad135b5dSChristopher Siden traverse_dataset_destroyed(spa_t *spa, blkptr_t *blkptr, 5877802d7bfSMatthew Ahrens uint64_t txg_start, zbookmark_phys_t *resume, int flags, 588ad135b5dSChristopher Siden blkptr_cb_t func, void *arg) 589ad135b5dSChristopher Siden { 590ad135b5dSChristopher Siden return (traverse_impl(spa, NULL, ZB_DESTROYED_OBJSET, 591ad135b5dSChristopher Siden blkptr, txg_start, resume, flags, func, arg)); 592fa9e4066Sahrens } 593fa9e4066Sahrens 59488b7b0f2SMatthew Ahrens /* 59588b7b0f2SMatthew Ahrens * NB: pool must not be changing on-disk (eg, from zdb or sync context). 59688b7b0f2SMatthew Ahrens */ 59788b7b0f2SMatthew Ahrens int 598bbfd46c4SJeff Bonwick traverse_pool(spa_t *spa, uint64_t txg_start, int flags, 599bbfd46c4SJeff Bonwick blkptr_cb_t func, void *arg) 600fa9e4066Sahrens { 6017fd05ac4SMatthew Ahrens int err; 60288b7b0f2SMatthew Ahrens uint64_t obj; 60388b7b0f2SMatthew Ahrens dsl_pool_t *dp = spa_get_dsl(spa); 60488b7b0f2SMatthew Ahrens objset_t *mos = dp->dp_meta_objset; 605cd088ea4SVictor Latushkin boolean_t hard = (flags & TRAVERSE_HARD); 60688b7b0f2SMatthew Ahrens 60788b7b0f2SMatthew Ahrens /* visit the MOS */ 608ad135b5dSChristopher Siden err = traverse_impl(spa, NULL, 0, spa_get_rootblkptr(spa), 609ad135b5dSChristopher Siden txg_start, NULL, flags, func, arg); 6103b2aab18SMatthew Ahrens if (err != 0) 61188b7b0f2SMatthew Ahrens return (err); 61288b7b0f2SMatthew Ahrens 61388b7b0f2SMatthew Ahrens /* visit each dataset */ 6147fd05ac4SMatthew Ahrens for (obj = 1; err == 0; 615cd088ea4SVictor Latushkin err = dmu_object_next(mos, &obj, FALSE, txg_start)) { 61688b7b0f2SMatthew Ahrens dmu_object_info_t doi; 61788b7b0f2SMatthew Ahrens 61888b7b0f2SMatthew Ahrens err = dmu_object_info(mos, obj, &doi); 6193b2aab18SMatthew Ahrens if (err != 0) { 6207fd05ac4SMatthew Ahrens if (hard) 6217fd05ac4SMatthew Ahrens continue; 6227fd05ac4SMatthew Ahrens break; 623cd088ea4SVictor Latushkin } 62488b7b0f2SMatthew Ahrens 6252acef22dSMatthew Ahrens if (doi.doi_bonus_type == DMU_OT_DSL_DATASET) { 62688b7b0f2SMatthew Ahrens dsl_dataset_t *ds; 627468c413aSTim Haley uint64_t txg = txg_start; 628468c413aSTim Haley 6293b2aab18SMatthew Ahrens dsl_pool_config_enter(dp, FTAG); 63088b7b0f2SMatthew Ahrens err = dsl_dataset_hold_obj(dp, obj, FTAG, &ds); 6313b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 6323b2aab18SMatthew Ahrens if (err != 0) { 6337fd05ac4SMatthew Ahrens if (hard) 6347fd05ac4SMatthew Ahrens continue; 6357fd05ac4SMatthew Ahrens break; 636cd088ea4SVictor Latushkin } 637c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_txg > txg) 638c1379625SJustin T. Gibbs txg = dsl_dataset_phys(ds)->ds_prev_snap_txg; 639bbfd46c4SJeff Bonwick err = traverse_dataset(ds, txg, flags, func, arg); 64088b7b0f2SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 6417fd05ac4SMatthew Ahrens if (err != 0) 6427fd05ac4SMatthew Ahrens break; 64388b7b0f2SMatthew Ahrens } 644fa9e4066Sahrens } 64588b7b0f2SMatthew Ahrens if (err == ESRCH) 64688b7b0f2SMatthew Ahrens err = 0; 6477fd05ac4SMatthew Ahrens return (err); 648fa9e4066Sahrens } 649