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 /* 226e1f5caaSNeil Perrin * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 23fa9e4066Sahrens * Use is subject to license terms. 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> 36*0a586ceaSMark Shellenbaum #include <sys/sa.h> 37*0a586ceaSMark Shellenbaum #include <sys/sa_impl.h> 3888b7b0f2SMatthew Ahrens #include <sys/callb.h> 3988b7b0f2SMatthew Ahrens 4088b7b0f2SMatthew Ahrens struct prefetch_data { 4188b7b0f2SMatthew Ahrens kmutex_t pd_mtx; 4288b7b0f2SMatthew Ahrens kcondvar_t pd_cv; 4388b7b0f2SMatthew Ahrens int pd_blks_max; 4488b7b0f2SMatthew Ahrens int pd_blks_fetched; 4588b7b0f2SMatthew Ahrens int pd_flags; 4688b7b0f2SMatthew Ahrens boolean_t pd_cancel; 4788b7b0f2SMatthew Ahrens boolean_t pd_exited; 4888b7b0f2SMatthew Ahrens }; 4988b7b0f2SMatthew Ahrens 5088b7b0f2SMatthew Ahrens struct traverse_data { 5188b7b0f2SMatthew Ahrens spa_t *td_spa; 5288b7b0f2SMatthew Ahrens uint64_t td_objset; 5388b7b0f2SMatthew Ahrens blkptr_t *td_rootbp; 5488b7b0f2SMatthew Ahrens uint64_t td_min_txg; 5588b7b0f2SMatthew Ahrens int td_flags; 5688b7b0f2SMatthew Ahrens struct prefetch_data *td_pfd; 5788b7b0f2SMatthew Ahrens blkptr_cb_t *td_func; 5888b7b0f2SMatthew Ahrens void *td_arg; 5988b7b0f2SMatthew Ahrens }; 60fa9e4066Sahrens 6114843421SMatthew Ahrens static int traverse_dnode(struct traverse_data *td, const dnode_phys_t *dnp, 6214843421SMatthew Ahrens arc_buf_t *buf, uint64_t objset, uint64_t object); 6314843421SMatthew Ahrens 64ea8dc4b6Seschrock /* ARGSUSED */ 65b24ab676SJeff Bonwick static int 665dabedeeSbonwick traverse_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg) 67ea8dc4b6Seschrock { 6888b7b0f2SMatthew Ahrens struct traverse_data *td = arg; 6988b7b0f2SMatthew Ahrens zbookmark_t zb; 70ea8dc4b6Seschrock 7188b7b0f2SMatthew Ahrens if (bp->blk_birth == 0) 72b24ab676SJeff Bonwick return (0); 735dabedeeSbonwick 7488b7b0f2SMatthew Ahrens if (claim_txg == 0 && bp->blk_birth >= spa_first_txg(td->td_spa)) 75b24ab676SJeff Bonwick return (0); 7688b7b0f2SMatthew Ahrens 77b24ab676SJeff Bonwick SET_BOOKMARK(&zb, td->td_objset, ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, 78b24ab676SJeff Bonwick bp->blk_cksum.zc_word[ZIL_ZC_SEQ]); 79b24ab676SJeff Bonwick 80b24ab676SJeff Bonwick (void) td->td_func(td->td_spa, zilog, bp, &zb, NULL, td->td_arg); 81b24ab676SJeff Bonwick 82b24ab676SJeff Bonwick return (0); 83ea8dc4b6Seschrock } 84ea8dc4b6Seschrock 85ea8dc4b6Seschrock /* ARGSUSED */ 86b24ab676SJeff Bonwick static int 875dabedeeSbonwick traverse_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg) 88ea8dc4b6Seschrock { 8988b7b0f2SMatthew Ahrens struct traverse_data *td = arg; 90ea8dc4b6Seschrock 91ea8dc4b6Seschrock if (lrc->lrc_txtype == TX_WRITE) { 92ea8dc4b6Seschrock lr_write_t *lr = (lr_write_t *)lrc; 93ea8dc4b6Seschrock blkptr_t *bp = &lr->lr_blkptr; 9488b7b0f2SMatthew Ahrens zbookmark_t zb; 95ea8dc4b6Seschrock 9688b7b0f2SMatthew Ahrens if (bp->blk_birth == 0) 97b24ab676SJeff Bonwick return (0); 985dabedeeSbonwick 9988b7b0f2SMatthew Ahrens if (claim_txg == 0 || bp->blk_birth < claim_txg) 100b24ab676SJeff Bonwick return (0); 101b24ab676SJeff Bonwick 102b24ab676SJeff Bonwick SET_BOOKMARK(&zb, td->td_objset, lr->lr_foid, ZB_ZIL_LEVEL, 103b24ab676SJeff Bonwick lr->lr_offset / BP_GET_LSIZE(bp)); 10488b7b0f2SMatthew Ahrens 105b24ab676SJeff Bonwick (void) td->td_func(td->td_spa, zilog, bp, &zb, NULL, 106b24ab676SJeff Bonwick td->td_arg); 107ea8dc4b6Seschrock } 108b24ab676SJeff Bonwick return (0); 109ea8dc4b6Seschrock } 110ea8dc4b6Seschrock 111ea8dc4b6Seschrock static void 11288b7b0f2SMatthew Ahrens traverse_zil(struct traverse_data *td, zil_header_t *zh) 113ea8dc4b6Seschrock { 1145dabedeeSbonwick uint64_t claim_txg = zh->zh_claim_txg; 115ea8dc4b6Seschrock zilog_t *zilog; 116ea8dc4b6Seschrock 1175dabedeeSbonwick /* 1185dabedeeSbonwick * We only want to visit blocks that have been claimed but not yet 119b24ab676SJeff Bonwick * replayed; plus, in read-only mode, blocks that are already stable. 1205dabedeeSbonwick */ 1218ad4d6ddSJeff Bonwick if (claim_txg == 0 && spa_writeable(td->td_spa)) 1225dabedeeSbonwick return; 1235dabedeeSbonwick 12488b7b0f2SMatthew Ahrens zilog = zil_alloc(spa_get_dsl(td->td_spa)->dp_meta_objset, zh); 125ea8dc4b6Seschrock 12688b7b0f2SMatthew Ahrens (void) zil_parse(zilog, traverse_zil_block, traverse_zil_record, td, 1275dabedeeSbonwick claim_txg); 128ea8dc4b6Seschrock 129ea8dc4b6Seschrock zil_free(zilog); 130ea8dc4b6Seschrock } 131ea8dc4b6Seschrock 132fa9e4066Sahrens static int 13388b7b0f2SMatthew Ahrens traverse_visitbp(struct traverse_data *td, const dnode_phys_t *dnp, 13488b7b0f2SMatthew Ahrens arc_buf_t *pbuf, blkptr_t *bp, const zbookmark_t *zb) 135fa9e4066Sahrens { 1366a0f0066SEric Taylor zbookmark_t czb; 137cd088ea4SVictor Latushkin int err = 0, lasterr = 0; 13888b7b0f2SMatthew Ahrens arc_buf_t *buf = NULL; 13988b7b0f2SMatthew Ahrens struct prefetch_data *pd = td->td_pfd; 140cd088ea4SVictor Latushkin boolean_t hard = td->td_flags & TRAVERSE_HARD; 141fa9e4066Sahrens 14288b7b0f2SMatthew Ahrens if (bp->blk_birth == 0) { 143b24ab676SJeff Bonwick err = td->td_func(td->td_spa, NULL, NULL, zb, dnp, td->td_arg); 14488b7b0f2SMatthew Ahrens return (err); 145fa9e4066Sahrens } 146fa9e4066Sahrens 14788b7b0f2SMatthew Ahrens if (bp->blk_birth <= td->td_min_txg) 14888b7b0f2SMatthew Ahrens return (0); 149fa9e4066Sahrens 15088b7b0f2SMatthew Ahrens if (pd && !pd->pd_exited && 15188b7b0f2SMatthew Ahrens ((pd->pd_flags & TRAVERSE_PREFETCH_DATA) || 15288b7b0f2SMatthew Ahrens BP_GET_TYPE(bp) == DMU_OT_DNODE || BP_GET_LEVEL(bp) > 0)) { 15388b7b0f2SMatthew Ahrens mutex_enter(&pd->pd_mtx); 15488b7b0f2SMatthew Ahrens ASSERT(pd->pd_blks_fetched >= 0); 15588b7b0f2SMatthew Ahrens while (pd->pd_blks_fetched == 0 && !pd->pd_exited) 15688b7b0f2SMatthew Ahrens cv_wait(&pd->pd_cv, &pd->pd_mtx); 15788b7b0f2SMatthew Ahrens pd->pd_blks_fetched--; 15888b7b0f2SMatthew Ahrens cv_broadcast(&pd->pd_cv); 15988b7b0f2SMatthew Ahrens mutex_exit(&pd->pd_mtx); 160fa9e4066Sahrens } 161fa9e4066Sahrens 16288b7b0f2SMatthew Ahrens if (td->td_flags & TRAVERSE_PRE) { 163b24ab676SJeff Bonwick err = td->td_func(td->td_spa, NULL, bp, zb, dnp, td->td_arg); 16488b7b0f2SMatthew Ahrens if (err) 16588b7b0f2SMatthew Ahrens return (err); 166fa9e4066Sahrens } 167fa9e4066Sahrens 16888b7b0f2SMatthew Ahrens if (BP_GET_LEVEL(bp) > 0) { 16988b7b0f2SMatthew Ahrens uint32_t flags = ARC_WAIT; 17088b7b0f2SMatthew Ahrens int i; 17188b7b0f2SMatthew Ahrens blkptr_t *cbp; 17288b7b0f2SMatthew Ahrens int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT; 17388b7b0f2SMatthew Ahrens 17488b7b0f2SMatthew Ahrens err = arc_read(NULL, td->td_spa, bp, pbuf, 17588b7b0f2SMatthew Ahrens arc_getbuf_func, &buf, 17688b7b0f2SMatthew Ahrens ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb); 17788b7b0f2SMatthew Ahrens if (err) 17888b7b0f2SMatthew Ahrens return (err); 17988b7b0f2SMatthew Ahrens 18088b7b0f2SMatthew Ahrens /* recursively visitbp() blocks below this */ 18188b7b0f2SMatthew Ahrens cbp = buf->b_data; 18288b7b0f2SMatthew Ahrens for (i = 0; i < epb; i++, cbp++) { 18388b7b0f2SMatthew Ahrens SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object, 18488b7b0f2SMatthew Ahrens zb->zb_level - 1, 18588b7b0f2SMatthew Ahrens zb->zb_blkid * epb + i); 18688b7b0f2SMatthew Ahrens err = traverse_visitbp(td, dnp, buf, cbp, &czb); 187cd088ea4SVictor Latushkin if (err) { 188cd088ea4SVictor Latushkin if (!hard) 189cd088ea4SVictor Latushkin break; 190cd088ea4SVictor Latushkin lasterr = err; 191cd088ea4SVictor Latushkin } 19288b7b0f2SMatthew Ahrens } 19388b7b0f2SMatthew Ahrens } else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) { 19488b7b0f2SMatthew Ahrens uint32_t flags = ARC_WAIT; 19514843421SMatthew Ahrens int i; 19688b7b0f2SMatthew Ahrens int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT; 19788b7b0f2SMatthew Ahrens 19888b7b0f2SMatthew Ahrens err = arc_read(NULL, td->td_spa, bp, pbuf, 19988b7b0f2SMatthew Ahrens arc_getbuf_func, &buf, 20088b7b0f2SMatthew Ahrens ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb); 20188b7b0f2SMatthew Ahrens if (err) 20288b7b0f2SMatthew Ahrens return (err); 20388b7b0f2SMatthew Ahrens 20488b7b0f2SMatthew Ahrens /* recursively visitbp() blocks below this */ 20588b7b0f2SMatthew Ahrens dnp = buf->b_data; 206cd088ea4SVictor Latushkin for (i = 0; i < epb; i++, dnp++) { 20714843421SMatthew Ahrens err = traverse_dnode(td, dnp, buf, zb->zb_objset, 20814843421SMatthew Ahrens zb->zb_blkid * epb + i); 209cd088ea4SVictor Latushkin if (err) { 210cd088ea4SVictor Latushkin if (!hard) 211cd088ea4SVictor Latushkin break; 212cd088ea4SVictor Latushkin lasterr = err; 213cd088ea4SVictor Latushkin } 214fa9e4066Sahrens } 21588b7b0f2SMatthew Ahrens } else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) { 21688b7b0f2SMatthew Ahrens uint32_t flags = ARC_WAIT; 21788b7b0f2SMatthew Ahrens objset_phys_t *osp; 21814843421SMatthew Ahrens dnode_phys_t *dnp; 21988b7b0f2SMatthew Ahrens 22088b7b0f2SMatthew Ahrens err = arc_read_nolock(NULL, td->td_spa, bp, 22188b7b0f2SMatthew Ahrens arc_getbuf_func, &buf, 22288b7b0f2SMatthew Ahrens ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb); 22388b7b0f2SMatthew Ahrens if (err) 22488b7b0f2SMatthew Ahrens return (err); 22588b7b0f2SMatthew Ahrens 22688b7b0f2SMatthew Ahrens osp = buf->b_data; 22788b7b0f2SMatthew Ahrens traverse_zil(td, &osp->os_zil_header); 228fa9e4066Sahrens 22914843421SMatthew Ahrens dnp = &osp->os_meta_dnode; 230b24ab676SJeff Bonwick err = traverse_dnode(td, dnp, buf, zb->zb_objset, 231b24ab676SJeff Bonwick DMU_META_DNODE_OBJECT); 232cd088ea4SVictor Latushkin if (err && hard) { 233cd088ea4SVictor Latushkin lasterr = err; 234cd088ea4SVictor Latushkin err = 0; 235cd088ea4SVictor Latushkin } 23614843421SMatthew Ahrens if (err == 0 && arc_buf_size(buf) >= sizeof (objset_phys_t)) { 23714843421SMatthew Ahrens dnp = &osp->os_userused_dnode; 23814843421SMatthew Ahrens err = traverse_dnode(td, dnp, buf, zb->zb_objset, 23914843421SMatthew Ahrens DMU_USERUSED_OBJECT); 24014843421SMatthew Ahrens } 241cd088ea4SVictor Latushkin if (err && hard) { 242cd088ea4SVictor Latushkin lasterr = err; 243cd088ea4SVictor Latushkin err = 0; 244cd088ea4SVictor Latushkin } 24514843421SMatthew Ahrens if (err == 0 && arc_buf_size(buf) >= sizeof (objset_phys_t)) { 24614843421SMatthew Ahrens dnp = &osp->os_groupused_dnode; 24714843421SMatthew Ahrens err = traverse_dnode(td, dnp, buf, zb->zb_objset, 24814843421SMatthew Ahrens DMU_GROUPUSED_OBJECT); 24988b7b0f2SMatthew Ahrens } 25088b7b0f2SMatthew Ahrens } 251fa9e4066Sahrens 25288b7b0f2SMatthew Ahrens if (buf) 25388b7b0f2SMatthew Ahrens (void) arc_buf_remove_ref(buf, &buf); 254fa9e4066Sahrens 255cd088ea4SVictor Latushkin if (err == 0 && lasterr == 0 && (td->td_flags & TRAVERSE_POST)) 256b24ab676SJeff Bonwick err = td->td_func(td->td_spa, NULL, bp, zb, dnp, td->td_arg); 257fa9e4066Sahrens 258cd088ea4SVictor Latushkin return (err != 0 ? err : lasterr); 259fa9e4066Sahrens } 260fa9e4066Sahrens 26114843421SMatthew Ahrens static int 26214843421SMatthew Ahrens traverse_dnode(struct traverse_data *td, const dnode_phys_t *dnp, 26314843421SMatthew Ahrens arc_buf_t *buf, uint64_t objset, uint64_t object) 26414843421SMatthew Ahrens { 265cd088ea4SVictor Latushkin int j, err = 0, lasterr = 0; 26614843421SMatthew Ahrens zbookmark_t czb; 267cd088ea4SVictor Latushkin boolean_t hard = (td->td_flags & TRAVERSE_HARD); 26814843421SMatthew Ahrens 26914843421SMatthew Ahrens for (j = 0; j < dnp->dn_nblkptr; j++) { 27014843421SMatthew Ahrens SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j); 27114843421SMatthew Ahrens err = traverse_visitbp(td, dnp, buf, 27214843421SMatthew Ahrens (blkptr_t *)&dnp->dn_blkptr[j], &czb); 273cd088ea4SVictor Latushkin if (err) { 274cd088ea4SVictor Latushkin if (!hard) 275cd088ea4SVictor Latushkin break; 276cd088ea4SVictor Latushkin lasterr = err; 277cd088ea4SVictor Latushkin } 278*0a586ceaSMark Shellenbaum if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) { 279*0a586ceaSMark Shellenbaum SET_BOOKMARK(&czb, objset, 280*0a586ceaSMark Shellenbaum object, 0, DMU_SPILL_BLKID); 281*0a586ceaSMark Shellenbaum err = traverse_visitbp(td, dnp, buf, 282*0a586ceaSMark Shellenbaum (blkptr_t *)&dnp->dn_spill, &czb); 283*0a586ceaSMark Shellenbaum if (err) { 284*0a586ceaSMark Shellenbaum if (!hard) 285*0a586ceaSMark Shellenbaum break; 286*0a586ceaSMark Shellenbaum lasterr = err; 287*0a586ceaSMark Shellenbaum } 288*0a586ceaSMark Shellenbaum } 28914843421SMatthew Ahrens } 290cd088ea4SVictor Latushkin return (err != 0 ? err : lasterr); 29114843421SMatthew Ahrens } 29214843421SMatthew Ahrens 29388b7b0f2SMatthew Ahrens /* ARGSUSED */ 29488b7b0f2SMatthew Ahrens static int 295b24ab676SJeff Bonwick traverse_prefetcher(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 296b24ab676SJeff Bonwick const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg) 297e7cbe64fSgw { 29888b7b0f2SMatthew Ahrens struct prefetch_data *pfd = arg; 29988b7b0f2SMatthew Ahrens uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH; 300e7cbe64fSgw 30188b7b0f2SMatthew Ahrens ASSERT(pfd->pd_blks_fetched >= 0); 30288b7b0f2SMatthew Ahrens if (pfd->pd_cancel) 30388b7b0f2SMatthew Ahrens return (EINTR); 304e7cbe64fSgw 30588b7b0f2SMatthew Ahrens if (bp == NULL || !((pfd->pd_flags & TRAVERSE_PREFETCH_DATA) || 3066e1f5caaSNeil Perrin BP_GET_TYPE(bp) == DMU_OT_DNODE || BP_GET_LEVEL(bp) > 0) || 3076e1f5caaSNeil Perrin BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG) 308fa9e4066Sahrens return (0); 309fa9e4066Sahrens 31088b7b0f2SMatthew Ahrens mutex_enter(&pfd->pd_mtx); 31188b7b0f2SMatthew Ahrens while (!pfd->pd_cancel && pfd->pd_blks_fetched >= pfd->pd_blks_max) 31288b7b0f2SMatthew Ahrens cv_wait(&pfd->pd_cv, &pfd->pd_mtx); 31388b7b0f2SMatthew Ahrens pfd->pd_blks_fetched++; 31488b7b0f2SMatthew Ahrens cv_broadcast(&pfd->pd_cv); 31588b7b0f2SMatthew Ahrens mutex_exit(&pfd->pd_mtx); 316fa9e4066Sahrens 31788b7b0f2SMatthew Ahrens (void) arc_read_nolock(NULL, spa, bp, NULL, NULL, 31888b7b0f2SMatthew Ahrens ZIO_PRIORITY_ASYNC_READ, 31988b7b0f2SMatthew Ahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, 32088b7b0f2SMatthew Ahrens &aflags, zb); 321fa9e4066Sahrens 32288b7b0f2SMatthew Ahrens return (0); 323fa9e4066Sahrens } 324fa9e4066Sahrens 325fa9e4066Sahrens static void 32688b7b0f2SMatthew Ahrens traverse_prefetch_thread(void *arg) 327fa9e4066Sahrens { 32888b7b0f2SMatthew Ahrens struct traverse_data *td_main = arg; 32988b7b0f2SMatthew Ahrens struct traverse_data td = *td_main; 33088b7b0f2SMatthew Ahrens zbookmark_t czb; 331fa9e4066Sahrens 33288b7b0f2SMatthew Ahrens td.td_func = traverse_prefetcher; 33388b7b0f2SMatthew Ahrens td.td_arg = td_main->td_pfd; 33488b7b0f2SMatthew Ahrens td.td_pfd = NULL; 335fa9e4066Sahrens 336b24ab676SJeff Bonwick SET_BOOKMARK(&czb, td.td_objset, 337b24ab676SJeff Bonwick ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 33888b7b0f2SMatthew Ahrens (void) traverse_visitbp(&td, NULL, NULL, td.td_rootbp, &czb); 339fa9e4066Sahrens 34088b7b0f2SMatthew Ahrens mutex_enter(&td_main->td_pfd->pd_mtx); 34188b7b0f2SMatthew Ahrens td_main->td_pfd->pd_exited = B_TRUE; 34288b7b0f2SMatthew Ahrens cv_broadcast(&td_main->td_pfd->pd_cv); 34388b7b0f2SMatthew Ahrens mutex_exit(&td_main->td_pfd->pd_mtx); 344fa9e4066Sahrens } 345fa9e4066Sahrens 34688b7b0f2SMatthew Ahrens /* 34788b7b0f2SMatthew Ahrens * NB: dataset must not be changing on-disk (eg, is a snapshot or we are 34888b7b0f2SMatthew Ahrens * in syncing context). 34988b7b0f2SMatthew Ahrens */ 35088b7b0f2SMatthew Ahrens static int 35188b7b0f2SMatthew Ahrens traverse_impl(spa_t *spa, uint64_t objset, blkptr_t *rootbp, 35288b7b0f2SMatthew Ahrens uint64_t txg_start, int flags, blkptr_cb_t func, void *arg) 353fa9e4066Sahrens { 35488b7b0f2SMatthew Ahrens struct traverse_data td; 35588b7b0f2SMatthew Ahrens struct prefetch_data pd = { 0 }; 35688b7b0f2SMatthew Ahrens zbookmark_t czb; 35788b7b0f2SMatthew Ahrens int err; 358fa9e4066Sahrens 35988b7b0f2SMatthew Ahrens td.td_spa = spa; 36088b7b0f2SMatthew Ahrens td.td_objset = objset; 36188b7b0f2SMatthew Ahrens td.td_rootbp = rootbp; 36288b7b0f2SMatthew Ahrens td.td_min_txg = txg_start; 36388b7b0f2SMatthew Ahrens td.td_func = func; 36488b7b0f2SMatthew Ahrens td.td_arg = arg; 36588b7b0f2SMatthew Ahrens td.td_pfd = &pd; 36688b7b0f2SMatthew Ahrens td.td_flags = flags; 36788b7b0f2SMatthew Ahrens 36888b7b0f2SMatthew Ahrens pd.pd_blks_max = 100; 36988b7b0f2SMatthew Ahrens pd.pd_flags = flags; 37088b7b0f2SMatthew Ahrens mutex_init(&pd.pd_mtx, NULL, MUTEX_DEFAULT, NULL); 37188b7b0f2SMatthew Ahrens cv_init(&pd.pd_cv, NULL, CV_DEFAULT, NULL); 37288b7b0f2SMatthew Ahrens 37388b7b0f2SMatthew Ahrens if (!(flags & TRAVERSE_PREFETCH) || 37488b7b0f2SMatthew Ahrens 0 == taskq_dispatch(system_taskq, traverse_prefetch_thread, 37588b7b0f2SMatthew Ahrens &td, TQ_NOQUEUE)) 37688b7b0f2SMatthew Ahrens pd.pd_exited = B_TRUE; 37788b7b0f2SMatthew Ahrens 378b24ab676SJeff Bonwick SET_BOOKMARK(&czb, objset, 379b24ab676SJeff Bonwick ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 38088b7b0f2SMatthew Ahrens err = traverse_visitbp(&td, NULL, NULL, rootbp, &czb); 38188b7b0f2SMatthew Ahrens 38288b7b0f2SMatthew Ahrens mutex_enter(&pd.pd_mtx); 38388b7b0f2SMatthew Ahrens pd.pd_cancel = B_TRUE; 38488b7b0f2SMatthew Ahrens cv_broadcast(&pd.pd_cv); 38588b7b0f2SMatthew Ahrens while (!pd.pd_exited) 38688b7b0f2SMatthew Ahrens cv_wait(&pd.pd_cv, &pd.pd_mtx); 38788b7b0f2SMatthew Ahrens mutex_exit(&pd.pd_mtx); 38888b7b0f2SMatthew Ahrens 38988b7b0f2SMatthew Ahrens mutex_destroy(&pd.pd_mtx); 39088b7b0f2SMatthew Ahrens cv_destroy(&pd.pd_cv); 391fa9e4066Sahrens 39288b7b0f2SMatthew Ahrens return (err); 393fa9e4066Sahrens } 394fa9e4066Sahrens 39588b7b0f2SMatthew Ahrens /* 39688b7b0f2SMatthew Ahrens * NB: dataset must not be changing on-disk (eg, is a snapshot or we are 39788b7b0f2SMatthew Ahrens * in syncing context). 39888b7b0f2SMatthew Ahrens */ 39988b7b0f2SMatthew Ahrens int 40088b7b0f2SMatthew Ahrens traverse_dataset(dsl_dataset_t *ds, uint64_t txg_start, int flags, 40188b7b0f2SMatthew Ahrens blkptr_cb_t func, void *arg) 402fa9e4066Sahrens { 40388b7b0f2SMatthew Ahrens return (traverse_impl(ds->ds_dir->dd_pool->dp_spa, ds->ds_object, 40488b7b0f2SMatthew Ahrens &ds->ds_phys->ds_bp, txg_start, flags, func, arg)); 405fa9e4066Sahrens } 406fa9e4066Sahrens 40788b7b0f2SMatthew Ahrens /* 40888b7b0f2SMatthew Ahrens * NB: pool must not be changing on-disk (eg, from zdb or sync context). 40988b7b0f2SMatthew Ahrens */ 41088b7b0f2SMatthew Ahrens int 411bbfd46c4SJeff Bonwick traverse_pool(spa_t *spa, uint64_t txg_start, int flags, 412bbfd46c4SJeff Bonwick blkptr_cb_t func, void *arg) 413fa9e4066Sahrens { 414cd088ea4SVictor Latushkin int err, lasterr = 0; 41588b7b0f2SMatthew Ahrens uint64_t obj; 41688b7b0f2SMatthew Ahrens dsl_pool_t *dp = spa_get_dsl(spa); 41788b7b0f2SMatthew Ahrens objset_t *mos = dp->dp_meta_objset; 418cd088ea4SVictor Latushkin boolean_t hard = (flags & TRAVERSE_HARD); 41988b7b0f2SMatthew Ahrens 42088b7b0f2SMatthew Ahrens /* visit the MOS */ 42188b7b0f2SMatthew Ahrens err = traverse_impl(spa, 0, spa_get_rootblkptr(spa), 422bbfd46c4SJeff Bonwick txg_start, flags, func, arg); 42388b7b0f2SMatthew Ahrens if (err) 42488b7b0f2SMatthew Ahrens return (err); 42588b7b0f2SMatthew Ahrens 42688b7b0f2SMatthew Ahrens /* visit each dataset */ 427cd088ea4SVictor Latushkin for (obj = 1; err == 0 || (err != ESRCH && hard); 428cd088ea4SVictor Latushkin err = dmu_object_next(mos, &obj, FALSE, txg_start)) { 42988b7b0f2SMatthew Ahrens dmu_object_info_t doi; 43088b7b0f2SMatthew Ahrens 43188b7b0f2SMatthew Ahrens err = dmu_object_info(mos, obj, &doi); 432cd088ea4SVictor Latushkin if (err) { 433cd088ea4SVictor Latushkin if (!hard) 434cd088ea4SVictor Latushkin return (err); 435cd088ea4SVictor Latushkin lasterr = err; 436cd088ea4SVictor Latushkin continue; 437cd088ea4SVictor Latushkin } 43888b7b0f2SMatthew Ahrens 43988b7b0f2SMatthew Ahrens if (doi.doi_type == DMU_OT_DSL_DATASET) { 44088b7b0f2SMatthew Ahrens dsl_dataset_t *ds; 441468c413aSTim Haley uint64_t txg = txg_start; 442468c413aSTim Haley 44388b7b0f2SMatthew Ahrens rw_enter(&dp->dp_config_rwlock, RW_READER); 44488b7b0f2SMatthew Ahrens err = dsl_dataset_hold_obj(dp, obj, FTAG, &ds); 44588b7b0f2SMatthew Ahrens rw_exit(&dp->dp_config_rwlock); 446cd088ea4SVictor Latushkin if (err) { 447cd088ea4SVictor Latushkin if (!hard) 448cd088ea4SVictor Latushkin return (err); 449cd088ea4SVictor Latushkin lasterr = err; 450cd088ea4SVictor Latushkin continue; 451cd088ea4SVictor Latushkin } 452468c413aSTim Haley if (ds->ds_phys->ds_prev_snap_txg > txg) 453468c413aSTim Haley txg = ds->ds_phys->ds_prev_snap_txg; 454bbfd46c4SJeff Bonwick err = traverse_dataset(ds, txg, flags, func, arg); 45588b7b0f2SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 456cd088ea4SVictor Latushkin if (err) { 457cd088ea4SVictor Latushkin if (!hard) 458cd088ea4SVictor Latushkin return (err); 459cd088ea4SVictor Latushkin lasterr = err; 460cd088ea4SVictor Latushkin } 46188b7b0f2SMatthew Ahrens } 462fa9e4066Sahrens } 46388b7b0f2SMatthew Ahrens if (err == ESRCH) 46488b7b0f2SMatthew Ahrens err = 0; 465cd088ea4SVictor Latushkin return (err != 0 ? err : lasterr); 466fa9e4066Sahrens } 467