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. 23be6fd75aSMatthew Ahrens * Copyright (c) 2013 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; 58ad135b5dSChristopher Siden zbookmark_t *td_resume; 5988b7b0f2SMatthew Ahrens int td_flags; 606e0cbcaaSMatthew Ahrens prefetch_data_t *td_pfd; 6188b7b0f2SMatthew Ahrens blkptr_cb_t *td_func; 6288b7b0f2SMatthew Ahrens void *td_arg; 636e0cbcaaSMatthew Ahrens } traverse_data_t; 64fa9e4066Sahrens 656e0cbcaaSMatthew Ahrens static int traverse_dnode(traverse_data_t *td, const dnode_phys_t *dnp, 661b912ec7SGeorge Wilson uint64_t objset, uint64_t object); 67b4709335SMatthew Ahrens static void prefetch_dnode_metadata(traverse_data_t *td, const dnode_phys_t *, 681b912ec7SGeorge Wilson uint64_t objset, uint64_t object); 6914843421SMatthew Ahrens 70b24ab676SJeff Bonwick static int 715dabedeeSbonwick traverse_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg) 72ea8dc4b6Seschrock { 736e0cbcaaSMatthew Ahrens traverse_data_t *td = arg; 7488b7b0f2SMatthew Ahrens zbookmark_t zb; 75ea8dc4b6Seschrock 7643466aaeSMax Grossman if (BP_IS_HOLE(bp)) 77b24ab676SJeff Bonwick return (0); 785dabedeeSbonwick 7988b7b0f2SMatthew Ahrens if (claim_txg == 0 && bp->blk_birth >= spa_first_txg(td->td_spa)) 80b24ab676SJeff Bonwick return (0); 8188b7b0f2SMatthew Ahrens 82b24ab676SJeff Bonwick SET_BOOKMARK(&zb, td->td_objset, ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, 83b24ab676SJeff Bonwick bp->blk_cksum.zc_word[ZIL_ZC_SEQ]); 84b24ab676SJeff Bonwick 851b912ec7SGeorge Wilson (void) td->td_func(td->td_spa, zilog, bp, &zb, NULL, td->td_arg); 86b24ab676SJeff Bonwick 87b24ab676SJeff Bonwick return (0); 88ea8dc4b6Seschrock } 89ea8dc4b6Seschrock 90b24ab676SJeff Bonwick static int 915dabedeeSbonwick traverse_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg) 92ea8dc4b6Seschrock { 936e0cbcaaSMatthew Ahrens traverse_data_t *td = arg; 94ea8dc4b6Seschrock 95ea8dc4b6Seschrock if (lrc->lrc_txtype == TX_WRITE) { 96ea8dc4b6Seschrock lr_write_t *lr = (lr_write_t *)lrc; 97ea8dc4b6Seschrock blkptr_t *bp = &lr->lr_blkptr; 9888b7b0f2SMatthew Ahrens zbookmark_t zb; 99ea8dc4b6Seschrock 10043466aaeSMax Grossman if (BP_IS_HOLE(bp)) 101b24ab676SJeff Bonwick return (0); 1025dabedeeSbonwick 10388b7b0f2SMatthew Ahrens if (claim_txg == 0 || bp->blk_birth < claim_txg) 104b24ab676SJeff Bonwick return (0); 105b24ab676SJeff Bonwick 1066e0cbcaaSMatthew Ahrens SET_BOOKMARK(&zb, td->td_objset, lr->lr_foid, 1076e0cbcaaSMatthew Ahrens ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp)); 10888b7b0f2SMatthew Ahrens 1091b912ec7SGeorge Wilson (void) td->td_func(td->td_spa, zilog, bp, &zb, NULL, 110b24ab676SJeff Bonwick td->td_arg); 111ea8dc4b6Seschrock } 112b24ab676SJeff Bonwick return (0); 113ea8dc4b6Seschrock } 114ea8dc4b6Seschrock 115ea8dc4b6Seschrock static void 1166e0cbcaaSMatthew Ahrens traverse_zil(traverse_data_t *td, zil_header_t *zh) 117ea8dc4b6Seschrock { 1185dabedeeSbonwick uint64_t claim_txg = zh->zh_claim_txg; 119ea8dc4b6Seschrock zilog_t *zilog; 120ea8dc4b6Seschrock 1215dabedeeSbonwick /* 1225dabedeeSbonwick * We only want to visit blocks that have been claimed but not yet 123b24ab676SJeff Bonwick * replayed; plus, in read-only mode, blocks that are already stable. 1245dabedeeSbonwick */ 1258ad4d6ddSJeff Bonwick if (claim_txg == 0 && spa_writeable(td->td_spa)) 1265dabedeeSbonwick return; 1275dabedeeSbonwick 12888b7b0f2SMatthew Ahrens zilog = zil_alloc(spa_get_dsl(td->td_spa)->dp_meta_objset, zh); 129ea8dc4b6Seschrock 13088b7b0f2SMatthew Ahrens (void) zil_parse(zilog, traverse_zil_block, traverse_zil_record, td, 1315dabedeeSbonwick claim_txg); 132ea8dc4b6Seschrock 133ea8dc4b6Seschrock zil_free(zilog); 134ea8dc4b6Seschrock } 135ea8dc4b6Seschrock 136ad135b5dSChristopher Siden typedef enum resume_skip { 137ad135b5dSChristopher Siden RESUME_SKIP_ALL, 138ad135b5dSChristopher Siden RESUME_SKIP_NONE, 139ad135b5dSChristopher Siden RESUME_SKIP_CHILDREN 140ad135b5dSChristopher Siden } resume_skip_t; 141ad135b5dSChristopher Siden 142ad135b5dSChristopher Siden /* 143ad135b5dSChristopher Siden * Returns RESUME_SKIP_ALL if td indicates that we are resuming a traversal and 144ad135b5dSChristopher Siden * the block indicated by zb does not need to be visited at all. Returns 145ad135b5dSChristopher Siden * RESUME_SKIP_CHILDREN if we are resuming a post traversal and we reach the 146ad135b5dSChristopher Siden * resume point. This indicates that this block should be visited but not its 147ad135b5dSChristopher Siden * children (since they must have been visited in a previous traversal). 148ad135b5dSChristopher Siden * Otherwise returns RESUME_SKIP_NONE. 149ad135b5dSChristopher Siden */ 150ad135b5dSChristopher Siden static resume_skip_t 151ad135b5dSChristopher Siden resume_skip_check(traverse_data_t *td, const dnode_phys_t *dnp, 152ad135b5dSChristopher Siden const zbookmark_t *zb) 153ad135b5dSChristopher Siden { 154ad135b5dSChristopher Siden if (td->td_resume != NULL && !ZB_IS_ZERO(td->td_resume)) { 155ad135b5dSChristopher Siden /* 156ad135b5dSChristopher Siden * If we already visited this bp & everything below, 157ad135b5dSChristopher Siden * don't bother doing it again. 158ad135b5dSChristopher Siden */ 159ad135b5dSChristopher Siden if (zbookmark_is_before(dnp, zb, td->td_resume)) 160ad135b5dSChristopher Siden return (RESUME_SKIP_ALL); 161ad135b5dSChristopher Siden 162ad135b5dSChristopher Siden /* 163ad135b5dSChristopher Siden * If we found the block we're trying to resume from, zero 164ad135b5dSChristopher Siden * the bookmark out to indicate that we have resumed. 165ad135b5dSChristopher Siden */ 166ad135b5dSChristopher Siden ASSERT3U(zb->zb_object, <=, td->td_resume->zb_object); 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 176ad135b5dSChristopher Siden static void 177ad135b5dSChristopher Siden traverse_pause(traverse_data_t *td, const zbookmark_t *zb) 178ad135b5dSChristopher Siden { 179ad135b5dSChristopher Siden ASSERT(td->td_resume != NULL); 180fb09f5aaSMadhav Suresh ASSERT0(zb->zb_level); 181ad135b5dSChristopher Siden bcopy(zb, td->td_resume, sizeof (*td->td_resume)); 182ad135b5dSChristopher Siden } 183ad135b5dSChristopher Siden 184b4709335SMatthew Ahrens static void 185b4709335SMatthew Ahrens traverse_prefetch_metadata(traverse_data_t *td, 1861b912ec7SGeorge Wilson const blkptr_t *bp, const zbookmark_t *zb) 187b4709335SMatthew Ahrens { 188b4709335SMatthew Ahrens uint32_t flags = ARC_NOWAIT | ARC_PREFETCH; 189b4709335SMatthew Ahrens 190b4709335SMatthew Ahrens if (!(td->td_flags & TRAVERSE_PREFETCH_METADATA)) 191b4709335SMatthew Ahrens return; 192b4709335SMatthew Ahrens /* 193b4709335SMatthew Ahrens * If we are in the process of resuming, don't prefetch, because 194b4709335SMatthew Ahrens * some children will not be needed (and in fact may have already 195b4709335SMatthew Ahrens * been freed). 196b4709335SMatthew Ahrens */ 197b4709335SMatthew Ahrens if (td->td_resume != NULL && !ZB_IS_ZERO(td->td_resume)) 198b4709335SMatthew Ahrens return; 199b4709335SMatthew Ahrens if (BP_IS_HOLE(bp) || bp->blk_birth <= td->td_min_txg) 200b4709335SMatthew Ahrens return; 201b4709335SMatthew Ahrens if (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_DNODE) 202b4709335SMatthew Ahrens return; 203b4709335SMatthew Ahrens 2041b912ec7SGeorge Wilson (void) arc_read(NULL, td->td_spa, bp, NULL, NULL, 2051b912ec7SGeorge Wilson ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb); 206b4709335SMatthew Ahrens } 207b4709335SMatthew Ahrens 208fa9e4066Sahrens static int 2096e0cbcaaSMatthew Ahrens traverse_visitbp(traverse_data_t *td, const dnode_phys_t *dnp, 2101b912ec7SGeorge Wilson const blkptr_t *bp, const zbookmark_t *zb) 211fa9e4066Sahrens { 2126a0f0066SEric Taylor zbookmark_t czb; 213cd088ea4SVictor Latushkin int err = 0, lasterr = 0; 21488b7b0f2SMatthew Ahrens arc_buf_t *buf = NULL; 2156e0cbcaaSMatthew Ahrens prefetch_data_t *pd = td->td_pfd; 216cd088ea4SVictor Latushkin boolean_t hard = td->td_flags & TRAVERSE_HARD; 217ad135b5dSChristopher Siden boolean_t pause = B_FALSE; 218ad135b5dSChristopher Siden 219ad135b5dSChristopher Siden switch (resume_skip_check(td, dnp, zb)) { 220ad135b5dSChristopher Siden case RESUME_SKIP_ALL: 221ad135b5dSChristopher Siden return (0); 222ad135b5dSChristopher Siden case RESUME_SKIP_CHILDREN: 223ad135b5dSChristopher Siden goto post; 224ad135b5dSChristopher Siden case RESUME_SKIP_NONE: 225ad135b5dSChristopher Siden break; 226ad135b5dSChristopher Siden default: 227ad135b5dSChristopher Siden ASSERT(0); 228ad135b5dSChristopher Siden } 229fa9e4066Sahrens 23043466aaeSMax Grossman if (bp->blk_birth == 0) { 23143466aaeSMax Grossman if (spa_feature_is_active(td->td_spa, SPA_FEATURE_HOLE_BIRTH)) { 23243466aaeSMax Grossman /* 23343466aaeSMax Grossman * Since this block has a birth time of 0 it must be a 23443466aaeSMax Grossman * hole created before the SPA_FEATURE_HOLE_BIRTH 23543466aaeSMax Grossman * feature was enabled. If SPA_FEATURE_HOLE_BIRTH 23643466aaeSMax Grossman * was enabled before the min_txg for this traveral we 23743466aaeSMax Grossman * know the hole must have been created before the 23843466aaeSMax Grossman * min_txg for this traveral, so we can skip it. If 23943466aaeSMax Grossman * SPA_FEATURE_HOLE_BIRTH was enabled after the min_txg 24043466aaeSMax Grossman * for this traveral we cannot tell if the hole was 24143466aaeSMax Grossman * created before or after the min_txg for this 24243466aaeSMax Grossman * traversal, so we cannot skip it. 24343466aaeSMax Grossman */ 24443466aaeSMax Grossman uint64_t hole_birth_enabled_txg; 24543466aaeSMax Grossman VERIFY(spa_feature_enabled_txg(td->td_spa, 24643466aaeSMax Grossman SPA_FEATURE_HOLE_BIRTH, &hole_birth_enabled_txg)); 24743466aaeSMax Grossman if (hole_birth_enabled_txg < td->td_min_txg) 24843466aaeSMax Grossman return (0); 24943466aaeSMax Grossman } 25043466aaeSMax Grossman } else if (bp->blk_birth <= td->td_min_txg) { 25143466aaeSMax Grossman return (0); 25243466aaeSMax Grossman } 25343466aaeSMax Grossman 254ad135b5dSChristopher Siden if (BP_IS_HOLE(bp)) { 25543466aaeSMax Grossman err = td->td_func(td->td_spa, NULL, bp, zb, dnp, td->td_arg); 25688b7b0f2SMatthew Ahrens return (err); 257fa9e4066Sahrens } 258fa9e4066Sahrens 25988b7b0f2SMatthew Ahrens if (pd && !pd->pd_exited && 26088b7b0f2SMatthew Ahrens ((pd->pd_flags & TRAVERSE_PREFETCH_DATA) || 26188b7b0f2SMatthew Ahrens BP_GET_TYPE(bp) == DMU_OT_DNODE || BP_GET_LEVEL(bp) > 0)) { 26288b7b0f2SMatthew Ahrens mutex_enter(&pd->pd_mtx); 26388b7b0f2SMatthew Ahrens ASSERT(pd->pd_blks_fetched >= 0); 26488b7b0f2SMatthew Ahrens while (pd->pd_blks_fetched == 0 && !pd->pd_exited) 26588b7b0f2SMatthew Ahrens cv_wait(&pd->pd_cv, &pd->pd_mtx); 26688b7b0f2SMatthew Ahrens pd->pd_blks_fetched--; 26788b7b0f2SMatthew Ahrens cv_broadcast(&pd->pd_cv); 26888b7b0f2SMatthew Ahrens mutex_exit(&pd->pd_mtx); 269fa9e4066Sahrens } 270fa9e4066Sahrens 27188b7b0f2SMatthew Ahrens if (td->td_flags & TRAVERSE_PRE) { 2721b912ec7SGeorge Wilson err = td->td_func(td->td_spa, NULL, bp, zb, dnp, 2733f9d6ad7SLin Ling td->td_arg); 27499d5e173STim Haley if (err == TRAVERSE_VISIT_NO_CHILDREN) 27599d5e173STim Haley return (0); 276ad135b5dSChristopher Siden if (err == ERESTART) 277ad135b5dSChristopher Siden pause = B_TRUE; /* handle pausing at a common point */ 278ad135b5dSChristopher Siden if (err != 0) 279ad135b5dSChristopher Siden goto post; 280fa9e4066Sahrens } 281fa9e4066Sahrens 28288b7b0f2SMatthew Ahrens if (BP_GET_LEVEL(bp) > 0) { 28388b7b0f2SMatthew Ahrens uint32_t flags = ARC_WAIT; 28488b7b0f2SMatthew Ahrens int i; 28588b7b0f2SMatthew Ahrens blkptr_t *cbp; 28688b7b0f2SMatthew Ahrens int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT; 28788b7b0f2SMatthew Ahrens 2881b912ec7SGeorge Wilson err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf, 28988b7b0f2SMatthew Ahrens ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb); 2903b2aab18SMatthew Ahrens if (err != 0) 29188b7b0f2SMatthew Ahrens return (err); 292b4709335SMatthew Ahrens cbp = buf->b_data; 293b4709335SMatthew Ahrens 294b4709335SMatthew Ahrens for (i = 0; i < epb; i++) { 295b4709335SMatthew Ahrens SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object, 296b4709335SMatthew Ahrens zb->zb_level - 1, 297b4709335SMatthew Ahrens zb->zb_blkid * epb + i); 2981b912ec7SGeorge Wilson traverse_prefetch_metadata(td, &cbp[i], &czb); 299b4709335SMatthew Ahrens } 30088b7b0f2SMatthew Ahrens 30188b7b0f2SMatthew Ahrens /* recursively visitbp() blocks below this */ 302b4709335SMatthew Ahrens for (i = 0; i < epb; i++) { 30388b7b0f2SMatthew Ahrens SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object, 30488b7b0f2SMatthew Ahrens zb->zb_level - 1, 30588b7b0f2SMatthew Ahrens zb->zb_blkid * epb + i); 3061b912ec7SGeorge Wilson err = traverse_visitbp(td, dnp, &cbp[i], &czb); 3073b2aab18SMatthew Ahrens if (err != 0) { 308cd088ea4SVictor Latushkin if (!hard) 309cd088ea4SVictor Latushkin break; 310cd088ea4SVictor Latushkin lasterr = err; 311cd088ea4SVictor Latushkin } 31288b7b0f2SMatthew Ahrens } 31388b7b0f2SMatthew Ahrens } else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) { 31488b7b0f2SMatthew Ahrens uint32_t flags = ARC_WAIT; 31514843421SMatthew Ahrens int i; 31688b7b0f2SMatthew Ahrens int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT; 31788b7b0f2SMatthew Ahrens 3181b912ec7SGeorge Wilson err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf, 31988b7b0f2SMatthew Ahrens ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb); 3203b2aab18SMatthew Ahrens if (err != 0) 32188b7b0f2SMatthew Ahrens return (err); 322b4709335SMatthew Ahrens dnp = buf->b_data; 323b4709335SMatthew Ahrens 324b4709335SMatthew Ahrens for (i = 0; i < epb; i++) { 3251b912ec7SGeorge Wilson prefetch_dnode_metadata(td, &dnp[i], zb->zb_objset, 326b4709335SMatthew Ahrens zb->zb_blkid * epb + i); 327b4709335SMatthew Ahrens } 32888b7b0f2SMatthew Ahrens 32988b7b0f2SMatthew Ahrens /* recursively visitbp() blocks below this */ 330b4709335SMatthew Ahrens for (i = 0; i < epb; i++) { 3311b912ec7SGeorge Wilson err = traverse_dnode(td, &dnp[i], zb->zb_objset, 33214843421SMatthew Ahrens zb->zb_blkid * epb + i); 3333b2aab18SMatthew Ahrens if (err != 0) { 334cd088ea4SVictor Latushkin if (!hard) 335cd088ea4SVictor Latushkin break; 336cd088ea4SVictor Latushkin lasterr = err; 337cd088ea4SVictor Latushkin } 338fa9e4066Sahrens } 33988b7b0f2SMatthew Ahrens } else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) { 34088b7b0f2SMatthew Ahrens uint32_t flags = ARC_WAIT; 34188b7b0f2SMatthew Ahrens objset_phys_t *osp; 34214843421SMatthew Ahrens dnode_phys_t *dnp; 34388b7b0f2SMatthew Ahrens 3441b912ec7SGeorge Wilson err = arc_read(NULL, td->td_spa, bp, arc_getbuf_func, &buf, 34588b7b0f2SMatthew Ahrens ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb); 3463b2aab18SMatthew Ahrens if (err != 0) 34788b7b0f2SMatthew Ahrens return (err); 34888b7b0f2SMatthew Ahrens 34988b7b0f2SMatthew Ahrens osp = buf->b_data; 35014843421SMatthew Ahrens dnp = &osp->os_meta_dnode; 3511b912ec7SGeorge Wilson prefetch_dnode_metadata(td, dnp, zb->zb_objset, 352b4709335SMatthew Ahrens DMU_META_DNODE_OBJECT); 353b4709335SMatthew Ahrens if (arc_buf_size(buf) >= sizeof (objset_phys_t)) { 354b4709335SMatthew Ahrens prefetch_dnode_metadata(td, &osp->os_groupused_dnode, 355*48f1b90eSMatthew Ahrens zb->zb_objset, DMU_GROUPUSED_OBJECT); 356*48f1b90eSMatthew Ahrens prefetch_dnode_metadata(td, &osp->os_userused_dnode, 3571b912ec7SGeorge Wilson zb->zb_objset, DMU_USERUSED_OBJECT); 358b4709335SMatthew Ahrens } 359b4709335SMatthew Ahrens 3601b912ec7SGeorge Wilson err = traverse_dnode(td, dnp, zb->zb_objset, 361b24ab676SJeff Bonwick DMU_META_DNODE_OBJECT); 362cd088ea4SVictor Latushkin if (err && hard) { 363cd088ea4SVictor Latushkin lasterr = err; 364cd088ea4SVictor Latushkin err = 0; 365cd088ea4SVictor Latushkin } 36614843421SMatthew Ahrens if (err == 0 && arc_buf_size(buf) >= sizeof (objset_phys_t)) { 367*48f1b90eSMatthew Ahrens dnp = &osp->os_groupused_dnode; 3681b912ec7SGeorge Wilson err = traverse_dnode(td, dnp, zb->zb_objset, 369*48f1b90eSMatthew Ahrens DMU_GROUPUSED_OBJECT); 37014843421SMatthew Ahrens } 371cd088ea4SVictor Latushkin if (err && hard) { 372cd088ea4SVictor Latushkin lasterr = err; 373cd088ea4SVictor Latushkin err = 0; 374cd088ea4SVictor Latushkin } 37514843421SMatthew Ahrens if (err == 0 && arc_buf_size(buf) >= sizeof (objset_phys_t)) { 376*48f1b90eSMatthew Ahrens dnp = &osp->os_userused_dnode; 3771b912ec7SGeorge Wilson err = traverse_dnode(td, dnp, zb->zb_objset, 378*48f1b90eSMatthew Ahrens DMU_USERUSED_OBJECT); 37988b7b0f2SMatthew Ahrens } 38088b7b0f2SMatthew Ahrens } 381fa9e4066Sahrens 38288b7b0f2SMatthew Ahrens if (buf) 38388b7b0f2SMatthew Ahrens (void) arc_buf_remove_ref(buf, &buf); 384fa9e4066Sahrens 385ad135b5dSChristopher Siden post: 3868b36997aSMatthew Ahrens if (err == 0 && (td->td_flags & TRAVERSE_POST)) { 3871b912ec7SGeorge Wilson err = td->td_func(td->td_spa, NULL, bp, zb, dnp, td->td_arg); 388ad135b5dSChristopher Siden if (err == ERESTART) 389ad135b5dSChristopher Siden pause = B_TRUE; 390ad135b5dSChristopher Siden } 391ad135b5dSChristopher Siden 392ad135b5dSChristopher Siden if (pause && td->td_resume != NULL) { 393ad135b5dSChristopher Siden ASSERT3U(err, ==, ERESTART); 394ad135b5dSChristopher Siden ASSERT(!hard); 395ad135b5dSChristopher Siden traverse_pause(td, zb); 3963f9d6ad7SLin Ling } 397fa9e4066Sahrens 398cd088ea4SVictor Latushkin return (err != 0 ? err : lasterr); 399fa9e4066Sahrens } 400fa9e4066Sahrens 401b4709335SMatthew Ahrens static void 402b4709335SMatthew Ahrens prefetch_dnode_metadata(traverse_data_t *td, const dnode_phys_t *dnp, 4031b912ec7SGeorge Wilson uint64_t objset, uint64_t object) 404b4709335SMatthew Ahrens { 405b4709335SMatthew Ahrens int j; 406b4709335SMatthew Ahrens zbookmark_t czb; 407b4709335SMatthew Ahrens 408b4709335SMatthew Ahrens for (j = 0; j < dnp->dn_nblkptr; j++) { 409b4709335SMatthew Ahrens SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j); 4101b912ec7SGeorge Wilson traverse_prefetch_metadata(td, &dnp->dn_blkptr[j], &czb); 411b4709335SMatthew Ahrens } 412b4709335SMatthew Ahrens 413b4709335SMatthew Ahrens if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) { 414b4709335SMatthew Ahrens SET_BOOKMARK(&czb, objset, object, 0, DMU_SPILL_BLKID); 4151b912ec7SGeorge Wilson traverse_prefetch_metadata(td, &dnp->dn_spill, &czb); 416b4709335SMatthew Ahrens } 417b4709335SMatthew Ahrens } 418b4709335SMatthew Ahrens 41914843421SMatthew Ahrens static int 4206e0cbcaaSMatthew Ahrens traverse_dnode(traverse_data_t *td, const dnode_phys_t *dnp, 4211b912ec7SGeorge Wilson uint64_t objset, uint64_t object) 42214843421SMatthew Ahrens { 423cd088ea4SVictor Latushkin int j, err = 0, lasterr = 0; 42414843421SMatthew Ahrens zbookmark_t czb; 425cd088ea4SVictor Latushkin boolean_t hard = (td->td_flags & TRAVERSE_HARD); 42614843421SMatthew Ahrens 42714843421SMatthew Ahrens for (j = 0; j < dnp->dn_nblkptr; j++) { 42814843421SMatthew Ahrens SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j); 4291b912ec7SGeorge Wilson err = traverse_visitbp(td, dnp, &dnp->dn_blkptr[j], &czb); 4303b2aab18SMatthew Ahrens if (err != 0) { 431cd088ea4SVictor Latushkin if (!hard) 432cd088ea4SVictor Latushkin break; 433cd088ea4SVictor Latushkin lasterr = err; 434cd088ea4SVictor Latushkin } 4353f9d6ad7SLin Ling } 4363f9d6ad7SLin Ling 4373f9d6ad7SLin Ling if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) { 438b4709335SMatthew Ahrens SET_BOOKMARK(&czb, objset, object, 0, DMU_SPILL_BLKID); 4391b912ec7SGeorge Wilson err = traverse_visitbp(td, dnp, &dnp->dn_spill, &czb); 4403b2aab18SMatthew Ahrens if (err != 0) { 4413f9d6ad7SLin Ling if (!hard) 4423f9d6ad7SLin Ling return (err); 4433f9d6ad7SLin Ling lasterr = err; 4440a586ceaSMark Shellenbaum } 44514843421SMatthew Ahrens } 446cd088ea4SVictor Latushkin return (err != 0 ? err : lasterr); 44714843421SMatthew Ahrens } 44814843421SMatthew Ahrens 44988b7b0f2SMatthew Ahrens /* ARGSUSED */ 45088b7b0f2SMatthew Ahrens static int 451b24ab676SJeff Bonwick traverse_prefetcher(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 4521b912ec7SGeorge Wilson const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg) 453e7cbe64fSgw { 4546e0cbcaaSMatthew Ahrens prefetch_data_t *pfd = arg; 45588b7b0f2SMatthew Ahrens uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH; 456e7cbe64fSgw 45788b7b0f2SMatthew Ahrens ASSERT(pfd->pd_blks_fetched >= 0); 45888b7b0f2SMatthew Ahrens if (pfd->pd_cancel) 459be6fd75aSMatthew Ahrens return (SET_ERROR(EINTR)); 460e7cbe64fSgw 46143466aaeSMax Grossman if (BP_IS_HOLE(bp) || 46243466aaeSMax Grossman !((pfd->pd_flags & TRAVERSE_PREFETCH_DATA) || 4636e1f5caaSNeil Perrin BP_GET_TYPE(bp) == DMU_OT_DNODE || BP_GET_LEVEL(bp) > 0) || 4646e1f5caaSNeil Perrin BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG) 465fa9e4066Sahrens return (0); 466fa9e4066Sahrens 46788b7b0f2SMatthew Ahrens mutex_enter(&pfd->pd_mtx); 46888b7b0f2SMatthew Ahrens while (!pfd->pd_cancel && pfd->pd_blks_fetched >= pfd->pd_blks_max) 46988b7b0f2SMatthew Ahrens cv_wait(&pfd->pd_cv, &pfd->pd_mtx); 47088b7b0f2SMatthew Ahrens pfd->pd_blks_fetched++; 47188b7b0f2SMatthew Ahrens cv_broadcast(&pfd->pd_cv); 47288b7b0f2SMatthew Ahrens mutex_exit(&pfd->pd_mtx); 473fa9e4066Sahrens 4741b912ec7SGeorge Wilson (void) arc_read(NULL, spa, bp, NULL, NULL, ZIO_PRIORITY_ASYNC_READ, 4751b912ec7SGeorge Wilson ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &aflags, zb); 476fa9e4066Sahrens 47788b7b0f2SMatthew Ahrens return (0); 478fa9e4066Sahrens } 479fa9e4066Sahrens 480fa9e4066Sahrens static void 48188b7b0f2SMatthew Ahrens traverse_prefetch_thread(void *arg) 482fa9e4066Sahrens { 4836e0cbcaaSMatthew Ahrens traverse_data_t *td_main = arg; 4846e0cbcaaSMatthew Ahrens traverse_data_t td = *td_main; 48588b7b0f2SMatthew Ahrens zbookmark_t czb; 486fa9e4066Sahrens 48788b7b0f2SMatthew Ahrens td.td_func = traverse_prefetcher; 48888b7b0f2SMatthew Ahrens td.td_arg = td_main->td_pfd; 48988b7b0f2SMatthew Ahrens td.td_pfd = NULL; 490fa9e4066Sahrens 491b24ab676SJeff Bonwick SET_BOOKMARK(&czb, td.td_objset, 492b24ab676SJeff Bonwick ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 4931b912ec7SGeorge Wilson (void) traverse_visitbp(&td, NULL, td.td_rootbp, &czb); 494fa9e4066Sahrens 49588b7b0f2SMatthew Ahrens mutex_enter(&td_main->td_pfd->pd_mtx); 49688b7b0f2SMatthew Ahrens td_main->td_pfd->pd_exited = B_TRUE; 49788b7b0f2SMatthew Ahrens cv_broadcast(&td_main->td_pfd->pd_cv); 49888b7b0f2SMatthew Ahrens mutex_exit(&td_main->td_pfd->pd_mtx); 499fa9e4066Sahrens } 500fa9e4066Sahrens 50188b7b0f2SMatthew Ahrens /* 50288b7b0f2SMatthew Ahrens * NB: dataset must not be changing on-disk (eg, is a snapshot or we are 50388b7b0f2SMatthew Ahrens * in syncing context). 50488b7b0f2SMatthew Ahrens */ 50588b7b0f2SMatthew Ahrens static int 506ad135b5dSChristopher Siden traverse_impl(spa_t *spa, dsl_dataset_t *ds, uint64_t objset, blkptr_t *rootbp, 507ad135b5dSChristopher Siden uint64_t txg_start, zbookmark_t *resume, int flags, 508ad135b5dSChristopher Siden blkptr_cb_t func, void *arg) 509fa9e4066Sahrens { 5106e0cbcaaSMatthew Ahrens traverse_data_t td; 5116e0cbcaaSMatthew Ahrens prefetch_data_t pd = { 0 }; 51288b7b0f2SMatthew Ahrens zbookmark_t czb; 51388b7b0f2SMatthew Ahrens int err; 514fa9e4066Sahrens 515ad135b5dSChristopher Siden ASSERT(ds == NULL || objset == ds->ds_object); 516ad135b5dSChristopher Siden ASSERT(!(flags & TRAVERSE_PRE) || !(flags & TRAVERSE_POST)); 517ad135b5dSChristopher Siden 518b4709335SMatthew Ahrens /* 519b4709335SMatthew Ahrens * The data prefetching mechanism (the prefetch thread) is incompatible 520b4709335SMatthew Ahrens * with resuming from a bookmark. 521b4709335SMatthew Ahrens */ 522b4709335SMatthew Ahrens ASSERT(resume == NULL || !(flags & TRAVERSE_PREFETCH_DATA)); 523b4709335SMatthew Ahrens 52488b7b0f2SMatthew Ahrens td.td_spa = spa; 525ad135b5dSChristopher Siden td.td_objset = objset; 52688b7b0f2SMatthew Ahrens td.td_rootbp = rootbp; 52788b7b0f2SMatthew Ahrens td.td_min_txg = txg_start; 528ad135b5dSChristopher Siden td.td_resume = resume; 52988b7b0f2SMatthew Ahrens td.td_func = func; 53088b7b0f2SMatthew Ahrens td.td_arg = arg; 53188b7b0f2SMatthew Ahrens td.td_pfd = &pd; 53288b7b0f2SMatthew Ahrens td.td_flags = flags; 53388b7b0f2SMatthew Ahrens 53444f92b7dSChris Kirby pd.pd_blks_max = zfs_pd_blks_max; 53588b7b0f2SMatthew Ahrens pd.pd_flags = flags; 53688b7b0f2SMatthew Ahrens mutex_init(&pd.pd_mtx, NULL, MUTEX_DEFAULT, NULL); 53788b7b0f2SMatthew Ahrens cv_init(&pd.pd_cv, NULL, CV_DEFAULT, NULL); 53888b7b0f2SMatthew Ahrens 5396e0cbcaaSMatthew Ahrens /* See comment on ZIL traversal in dsl_scan_visitds. */ 5403b2aab18SMatthew Ahrens if (ds != NULL && !dsl_dataset_is_snapshot(ds) && !BP_IS_HOLE(rootbp)) { 5413b2aab18SMatthew Ahrens uint32_t flags = ARC_WAIT; 5423b2aab18SMatthew Ahrens objset_phys_t *osp; 5433b2aab18SMatthew Ahrens arc_buf_t *buf; 5446e0cbcaaSMatthew Ahrens 5453b2aab18SMatthew Ahrens err = arc_read(NULL, td.td_spa, rootbp, 5463b2aab18SMatthew Ahrens arc_getbuf_func, &buf, 5473b2aab18SMatthew Ahrens ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, NULL); 5483b2aab18SMatthew Ahrens if (err != 0) 5496e0cbcaaSMatthew Ahrens return (err); 5506e0cbcaaSMatthew Ahrens 5513b2aab18SMatthew Ahrens osp = buf->b_data; 5523b2aab18SMatthew Ahrens traverse_zil(&td, &osp->os_zil_header); 5533b2aab18SMatthew Ahrens (void) arc_buf_remove_ref(buf, &buf); 5546e0cbcaaSMatthew Ahrens } 5556e0cbcaaSMatthew Ahrens 556b4709335SMatthew Ahrens if (!(flags & TRAVERSE_PREFETCH_DATA) || 55788b7b0f2SMatthew Ahrens 0 == taskq_dispatch(system_taskq, traverse_prefetch_thread, 55888b7b0f2SMatthew Ahrens &td, TQ_NOQUEUE)) 55988b7b0f2SMatthew Ahrens pd.pd_exited = B_TRUE; 56088b7b0f2SMatthew Ahrens 5616e0cbcaaSMatthew Ahrens SET_BOOKMARK(&czb, td.td_objset, 562b24ab676SJeff Bonwick ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 5631b912ec7SGeorge Wilson err = traverse_visitbp(&td, NULL, rootbp, &czb); 56488b7b0f2SMatthew Ahrens 56588b7b0f2SMatthew Ahrens mutex_enter(&pd.pd_mtx); 56688b7b0f2SMatthew Ahrens pd.pd_cancel = B_TRUE; 56788b7b0f2SMatthew Ahrens cv_broadcast(&pd.pd_cv); 56888b7b0f2SMatthew Ahrens while (!pd.pd_exited) 56988b7b0f2SMatthew Ahrens cv_wait(&pd.pd_cv, &pd.pd_mtx); 57088b7b0f2SMatthew Ahrens mutex_exit(&pd.pd_mtx); 57188b7b0f2SMatthew Ahrens 57288b7b0f2SMatthew Ahrens mutex_destroy(&pd.pd_mtx); 57388b7b0f2SMatthew Ahrens cv_destroy(&pd.pd_cv); 574fa9e4066Sahrens 57588b7b0f2SMatthew Ahrens return (err); 576fa9e4066Sahrens } 577fa9e4066Sahrens 57888b7b0f2SMatthew Ahrens /* 57988b7b0f2SMatthew Ahrens * NB: dataset must not be changing on-disk (eg, is a snapshot or we are 58088b7b0f2SMatthew Ahrens * in syncing context). 58188b7b0f2SMatthew Ahrens */ 58288b7b0f2SMatthew Ahrens int 58388b7b0f2SMatthew Ahrens traverse_dataset(dsl_dataset_t *ds, uint64_t txg_start, int flags, 58488b7b0f2SMatthew Ahrens blkptr_cb_t func, void *arg) 585fa9e4066Sahrens { 586ad135b5dSChristopher Siden return (traverse_impl(ds->ds_dir->dd_pool->dp_spa, ds, ds->ds_object, 587ad135b5dSChristopher Siden &ds->ds_phys->ds_bp, txg_start, NULL, flags, func, arg)); 588ad135b5dSChristopher Siden } 589ad135b5dSChristopher Siden 590ad135b5dSChristopher Siden int 591ad135b5dSChristopher Siden traverse_dataset_destroyed(spa_t *spa, blkptr_t *blkptr, 592ad135b5dSChristopher Siden uint64_t txg_start, zbookmark_t *resume, int flags, 593ad135b5dSChristopher Siden blkptr_cb_t func, void *arg) 594ad135b5dSChristopher Siden { 595ad135b5dSChristopher Siden return (traverse_impl(spa, NULL, ZB_DESTROYED_OBJSET, 596ad135b5dSChristopher Siden blkptr, txg_start, resume, flags, func, arg)); 597fa9e4066Sahrens } 598fa9e4066Sahrens 59988b7b0f2SMatthew Ahrens /* 60088b7b0f2SMatthew Ahrens * NB: pool must not be changing on-disk (eg, from zdb or sync context). 60188b7b0f2SMatthew Ahrens */ 60288b7b0f2SMatthew Ahrens int 603bbfd46c4SJeff Bonwick traverse_pool(spa_t *spa, uint64_t txg_start, int flags, 604bbfd46c4SJeff Bonwick blkptr_cb_t func, void *arg) 605fa9e4066Sahrens { 606cd088ea4SVictor Latushkin int err, lasterr = 0; 60788b7b0f2SMatthew Ahrens uint64_t obj; 60888b7b0f2SMatthew Ahrens dsl_pool_t *dp = spa_get_dsl(spa); 60988b7b0f2SMatthew Ahrens objset_t *mos = dp->dp_meta_objset; 610cd088ea4SVictor Latushkin boolean_t hard = (flags & TRAVERSE_HARD); 61188b7b0f2SMatthew Ahrens 61288b7b0f2SMatthew Ahrens /* visit the MOS */ 613ad135b5dSChristopher Siden err = traverse_impl(spa, NULL, 0, spa_get_rootblkptr(spa), 614ad135b5dSChristopher Siden txg_start, NULL, flags, func, arg); 6153b2aab18SMatthew Ahrens if (err != 0) 61688b7b0f2SMatthew Ahrens return (err); 61788b7b0f2SMatthew Ahrens 61888b7b0f2SMatthew Ahrens /* visit each dataset */ 619cd088ea4SVictor Latushkin for (obj = 1; err == 0 || (err != ESRCH && hard); 620cd088ea4SVictor Latushkin err = dmu_object_next(mos, &obj, FALSE, txg_start)) { 62188b7b0f2SMatthew Ahrens dmu_object_info_t doi; 62288b7b0f2SMatthew Ahrens 62388b7b0f2SMatthew Ahrens err = dmu_object_info(mos, obj, &doi); 6243b2aab18SMatthew Ahrens if (err != 0) { 625cd088ea4SVictor Latushkin if (!hard) 626cd088ea4SVictor Latushkin return (err); 627cd088ea4SVictor Latushkin lasterr = err; 628cd088ea4SVictor Latushkin continue; 629cd088ea4SVictor Latushkin } 63088b7b0f2SMatthew Ahrens 6312acef22dSMatthew Ahrens if (doi.doi_bonus_type == DMU_OT_DSL_DATASET) { 63288b7b0f2SMatthew Ahrens dsl_dataset_t *ds; 633468c413aSTim Haley uint64_t txg = txg_start; 634468c413aSTim Haley 6353b2aab18SMatthew Ahrens dsl_pool_config_enter(dp, FTAG); 63688b7b0f2SMatthew Ahrens err = dsl_dataset_hold_obj(dp, obj, FTAG, &ds); 6373b2aab18SMatthew Ahrens dsl_pool_config_exit(dp, FTAG); 6383b2aab18SMatthew Ahrens if (err != 0) { 639cd088ea4SVictor Latushkin if (!hard) 640cd088ea4SVictor Latushkin return (err); 641cd088ea4SVictor Latushkin lasterr = err; 642cd088ea4SVictor Latushkin continue; 643cd088ea4SVictor Latushkin } 644468c413aSTim Haley if (ds->ds_phys->ds_prev_snap_txg > txg) 645468c413aSTim Haley txg = ds->ds_phys->ds_prev_snap_txg; 646bbfd46c4SJeff Bonwick err = traverse_dataset(ds, txg, flags, func, arg); 64788b7b0f2SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 6483b2aab18SMatthew Ahrens if (err != 0) { 649cd088ea4SVictor Latushkin if (!hard) 650cd088ea4SVictor Latushkin return (err); 651cd088ea4SVictor Latushkin lasterr = err; 652cd088ea4SVictor Latushkin } 65388b7b0f2SMatthew Ahrens } 654fa9e4066Sahrens } 65588b7b0f2SMatthew Ahrens if (err == ESRCH) 65688b7b0f2SMatthew Ahrens err = 0; 657cd088ea4SVictor Latushkin return (err != 0 ? err : lasterr); 658fa9e4066Sahrens } 659