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 /* 22*3f9d6ad7SLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23fa9e4066Sahrens */ 24fa9e4066Sahrens 25fa9e4066Sahrens #include <sys/zfs_context.h> 26fa9e4066Sahrens #include <sys/dmu_objset.h> 27fa9e4066Sahrens #include <sys/dmu_traverse.h> 28fa9e4066Sahrens #include <sys/dsl_dataset.h> 29fa9e4066Sahrens #include <sys/dsl_dir.h> 30fa9e4066Sahrens #include <sys/dsl_pool.h> 31fa9e4066Sahrens #include <sys/dnode.h> 32fa9e4066Sahrens #include <sys/spa.h> 33fa9e4066Sahrens #include <sys/zio.h> 34fa9e4066Sahrens #include <sys/dmu_impl.h> 350a586ceaSMark Shellenbaum #include <sys/sa.h> 360a586ceaSMark Shellenbaum #include <sys/sa_impl.h> 3788b7b0f2SMatthew Ahrens #include <sys/callb.h> 3888b7b0f2SMatthew Ahrens 3988b7b0f2SMatthew Ahrens struct prefetch_data { 4088b7b0f2SMatthew Ahrens kmutex_t pd_mtx; 4188b7b0f2SMatthew Ahrens kcondvar_t pd_cv; 4288b7b0f2SMatthew Ahrens int pd_blks_max; 4388b7b0f2SMatthew Ahrens int pd_blks_fetched; 4488b7b0f2SMatthew Ahrens int pd_flags; 4588b7b0f2SMatthew Ahrens boolean_t pd_cancel; 4688b7b0f2SMatthew Ahrens boolean_t pd_exited; 4788b7b0f2SMatthew Ahrens }; 4888b7b0f2SMatthew Ahrens 4988b7b0f2SMatthew Ahrens struct traverse_data { 5088b7b0f2SMatthew Ahrens spa_t *td_spa; 5188b7b0f2SMatthew Ahrens uint64_t td_objset; 5288b7b0f2SMatthew Ahrens blkptr_t *td_rootbp; 5388b7b0f2SMatthew Ahrens uint64_t td_min_txg; 5488b7b0f2SMatthew Ahrens int td_flags; 5588b7b0f2SMatthew Ahrens struct prefetch_data *td_pfd; 5688b7b0f2SMatthew Ahrens blkptr_cb_t *td_func; 5788b7b0f2SMatthew Ahrens void *td_arg; 5888b7b0f2SMatthew Ahrens }; 59fa9e4066Sahrens 6014843421SMatthew Ahrens static int traverse_dnode(struct traverse_data *td, const dnode_phys_t *dnp, 6114843421SMatthew Ahrens arc_buf_t *buf, uint64_t objset, uint64_t object); 6214843421SMatthew Ahrens 63ea8dc4b6Seschrock /* ARGSUSED */ 64b24ab676SJeff Bonwick static int 655dabedeeSbonwick traverse_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg) 66ea8dc4b6Seschrock { 6788b7b0f2SMatthew Ahrens struct traverse_data *td = arg; 6888b7b0f2SMatthew Ahrens zbookmark_t zb; 69ea8dc4b6Seschrock 7088b7b0f2SMatthew Ahrens if (bp->blk_birth == 0) 71b24ab676SJeff Bonwick return (0); 725dabedeeSbonwick 7388b7b0f2SMatthew Ahrens if (claim_txg == 0 && bp->blk_birth >= spa_first_txg(td->td_spa)) 74b24ab676SJeff Bonwick return (0); 7588b7b0f2SMatthew Ahrens 76b24ab676SJeff Bonwick SET_BOOKMARK(&zb, td->td_objset, ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, 77b24ab676SJeff Bonwick bp->blk_cksum.zc_word[ZIL_ZC_SEQ]); 78b24ab676SJeff Bonwick 79*3f9d6ad7SLin Ling (void) td->td_func(td->td_spa, zilog, bp, NULL, &zb, NULL, td->td_arg); 80b24ab676SJeff Bonwick 81b24ab676SJeff Bonwick return (0); 82ea8dc4b6Seschrock } 83ea8dc4b6Seschrock 84ea8dc4b6Seschrock /* ARGSUSED */ 85b24ab676SJeff Bonwick static int 865dabedeeSbonwick traverse_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg) 87ea8dc4b6Seschrock { 8888b7b0f2SMatthew Ahrens struct traverse_data *td = arg; 89ea8dc4b6Seschrock 90ea8dc4b6Seschrock if (lrc->lrc_txtype == TX_WRITE) { 91ea8dc4b6Seschrock lr_write_t *lr = (lr_write_t *)lrc; 92ea8dc4b6Seschrock blkptr_t *bp = &lr->lr_blkptr; 9388b7b0f2SMatthew Ahrens zbookmark_t zb; 94ea8dc4b6Seschrock 9588b7b0f2SMatthew Ahrens if (bp->blk_birth == 0) 96b24ab676SJeff Bonwick return (0); 975dabedeeSbonwick 9888b7b0f2SMatthew Ahrens if (claim_txg == 0 || bp->blk_birth < claim_txg) 99b24ab676SJeff Bonwick return (0); 100b24ab676SJeff Bonwick 101b24ab676SJeff Bonwick SET_BOOKMARK(&zb, td->td_objset, lr->lr_foid, ZB_ZIL_LEVEL, 102b24ab676SJeff Bonwick lr->lr_offset / BP_GET_LSIZE(bp)); 10388b7b0f2SMatthew Ahrens 104*3f9d6ad7SLin Ling (void) td->td_func(td->td_spa, zilog, bp, NULL, &zb, NULL, 105b24ab676SJeff Bonwick td->td_arg); 106ea8dc4b6Seschrock } 107b24ab676SJeff Bonwick return (0); 108ea8dc4b6Seschrock } 109ea8dc4b6Seschrock 110ea8dc4b6Seschrock static void 11188b7b0f2SMatthew Ahrens traverse_zil(struct traverse_data *td, zil_header_t *zh) 112ea8dc4b6Seschrock { 1135dabedeeSbonwick uint64_t claim_txg = zh->zh_claim_txg; 114ea8dc4b6Seschrock zilog_t *zilog; 115ea8dc4b6Seschrock 1165dabedeeSbonwick /* 1175dabedeeSbonwick * We only want to visit blocks that have been claimed but not yet 118b24ab676SJeff Bonwick * replayed; plus, in read-only mode, blocks that are already stable. 1195dabedeeSbonwick */ 1208ad4d6ddSJeff Bonwick if (claim_txg == 0 && spa_writeable(td->td_spa)) 1215dabedeeSbonwick return; 1225dabedeeSbonwick 12388b7b0f2SMatthew Ahrens zilog = zil_alloc(spa_get_dsl(td->td_spa)->dp_meta_objset, zh); 124ea8dc4b6Seschrock 12588b7b0f2SMatthew Ahrens (void) zil_parse(zilog, traverse_zil_block, traverse_zil_record, td, 1265dabedeeSbonwick claim_txg); 127ea8dc4b6Seschrock 128ea8dc4b6Seschrock zil_free(zilog); 129ea8dc4b6Seschrock } 130ea8dc4b6Seschrock 131fa9e4066Sahrens static int 13288b7b0f2SMatthew Ahrens traverse_visitbp(struct traverse_data *td, const dnode_phys_t *dnp, 13388b7b0f2SMatthew Ahrens arc_buf_t *pbuf, blkptr_t *bp, const zbookmark_t *zb) 134fa9e4066Sahrens { 1356a0f0066SEric Taylor zbookmark_t czb; 136cd088ea4SVictor Latushkin int err = 0, lasterr = 0; 13788b7b0f2SMatthew Ahrens arc_buf_t *buf = NULL; 13888b7b0f2SMatthew Ahrens struct prefetch_data *pd = td->td_pfd; 139cd088ea4SVictor Latushkin boolean_t hard = td->td_flags & TRAVERSE_HARD; 140fa9e4066Sahrens 14188b7b0f2SMatthew Ahrens if (bp->blk_birth == 0) { 142*3f9d6ad7SLin Ling err = td->td_func(td->td_spa, NULL, NULL, pbuf, zb, dnp, 143*3f9d6ad7SLin Ling 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) { 163*3f9d6ad7SLin Ling err = td->td_func(td->td_spa, NULL, bp, pbuf, zb, dnp, 164*3f9d6ad7SLin Ling td->td_arg); 16588b7b0f2SMatthew Ahrens if (err) 16688b7b0f2SMatthew Ahrens return (err); 167fa9e4066Sahrens } 168fa9e4066Sahrens 16988b7b0f2SMatthew Ahrens if (BP_GET_LEVEL(bp) > 0) { 17088b7b0f2SMatthew Ahrens uint32_t flags = ARC_WAIT; 17188b7b0f2SMatthew Ahrens int i; 17288b7b0f2SMatthew Ahrens blkptr_t *cbp; 17388b7b0f2SMatthew Ahrens int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT; 17488b7b0f2SMatthew Ahrens 175*3f9d6ad7SLin Ling err = dsl_read(NULL, td->td_spa, bp, pbuf, 17688b7b0f2SMatthew Ahrens arc_getbuf_func, &buf, 17788b7b0f2SMatthew Ahrens ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb); 17888b7b0f2SMatthew Ahrens if (err) 17988b7b0f2SMatthew Ahrens return (err); 18088b7b0f2SMatthew Ahrens 18188b7b0f2SMatthew Ahrens /* recursively visitbp() blocks below this */ 18288b7b0f2SMatthew Ahrens cbp = buf->b_data; 18388b7b0f2SMatthew Ahrens for (i = 0; i < epb; i++, cbp++) { 18488b7b0f2SMatthew Ahrens SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object, 18588b7b0f2SMatthew Ahrens zb->zb_level - 1, 18688b7b0f2SMatthew Ahrens zb->zb_blkid * epb + i); 18788b7b0f2SMatthew Ahrens err = traverse_visitbp(td, dnp, buf, cbp, &czb); 188cd088ea4SVictor Latushkin if (err) { 189cd088ea4SVictor Latushkin if (!hard) 190cd088ea4SVictor Latushkin break; 191cd088ea4SVictor Latushkin lasterr = err; 192cd088ea4SVictor Latushkin } 19388b7b0f2SMatthew Ahrens } 19488b7b0f2SMatthew Ahrens } else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) { 19588b7b0f2SMatthew Ahrens uint32_t flags = ARC_WAIT; 19614843421SMatthew Ahrens int i; 19788b7b0f2SMatthew Ahrens int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT; 19888b7b0f2SMatthew Ahrens 199*3f9d6ad7SLin Ling err = dsl_read(NULL, td->td_spa, bp, pbuf, 20088b7b0f2SMatthew Ahrens arc_getbuf_func, &buf, 20188b7b0f2SMatthew Ahrens ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb); 20288b7b0f2SMatthew Ahrens if (err) 20388b7b0f2SMatthew Ahrens return (err); 20488b7b0f2SMatthew Ahrens 20588b7b0f2SMatthew Ahrens /* recursively visitbp() blocks below this */ 20688b7b0f2SMatthew Ahrens dnp = buf->b_data; 207cd088ea4SVictor Latushkin for (i = 0; i < epb; i++, dnp++) { 20814843421SMatthew Ahrens err = traverse_dnode(td, dnp, buf, zb->zb_objset, 20914843421SMatthew Ahrens zb->zb_blkid * epb + i); 210cd088ea4SVictor Latushkin if (err) { 211cd088ea4SVictor Latushkin if (!hard) 212cd088ea4SVictor Latushkin break; 213cd088ea4SVictor Latushkin lasterr = err; 214cd088ea4SVictor Latushkin } 215fa9e4066Sahrens } 21688b7b0f2SMatthew Ahrens } else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) { 21788b7b0f2SMatthew Ahrens uint32_t flags = ARC_WAIT; 21888b7b0f2SMatthew Ahrens objset_phys_t *osp; 21914843421SMatthew Ahrens dnode_phys_t *dnp; 22088b7b0f2SMatthew Ahrens 221*3f9d6ad7SLin Ling err = dsl_read_nolock(NULL, td->td_spa, bp, 22288b7b0f2SMatthew Ahrens arc_getbuf_func, &buf, 22388b7b0f2SMatthew Ahrens ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb); 22488b7b0f2SMatthew Ahrens if (err) 22588b7b0f2SMatthew Ahrens return (err); 22688b7b0f2SMatthew Ahrens 22788b7b0f2SMatthew Ahrens osp = buf->b_data; 22888b7b0f2SMatthew Ahrens traverse_zil(td, &osp->os_zil_header); 229fa9e4066Sahrens 23014843421SMatthew Ahrens dnp = &osp->os_meta_dnode; 231b24ab676SJeff Bonwick err = traverse_dnode(td, dnp, buf, zb->zb_objset, 232b24ab676SJeff Bonwick DMU_META_DNODE_OBJECT); 233cd088ea4SVictor Latushkin if (err && hard) { 234cd088ea4SVictor Latushkin lasterr = err; 235cd088ea4SVictor Latushkin err = 0; 236cd088ea4SVictor Latushkin } 23714843421SMatthew Ahrens if (err == 0 && arc_buf_size(buf) >= sizeof (objset_phys_t)) { 23814843421SMatthew Ahrens dnp = &osp->os_userused_dnode; 23914843421SMatthew Ahrens err = traverse_dnode(td, dnp, buf, zb->zb_objset, 24014843421SMatthew Ahrens DMU_USERUSED_OBJECT); 24114843421SMatthew Ahrens } 242cd088ea4SVictor Latushkin if (err && hard) { 243cd088ea4SVictor Latushkin lasterr = err; 244cd088ea4SVictor Latushkin err = 0; 245cd088ea4SVictor Latushkin } 24614843421SMatthew Ahrens if (err == 0 && arc_buf_size(buf) >= sizeof (objset_phys_t)) { 24714843421SMatthew Ahrens dnp = &osp->os_groupused_dnode; 24814843421SMatthew Ahrens err = traverse_dnode(td, dnp, buf, zb->zb_objset, 24914843421SMatthew Ahrens DMU_GROUPUSED_OBJECT); 25088b7b0f2SMatthew Ahrens } 25188b7b0f2SMatthew Ahrens } 252fa9e4066Sahrens 25388b7b0f2SMatthew Ahrens if (buf) 25488b7b0f2SMatthew Ahrens (void) arc_buf_remove_ref(buf, &buf); 255fa9e4066Sahrens 256*3f9d6ad7SLin Ling if (err == 0 && lasterr == 0 && (td->td_flags & TRAVERSE_POST)) { 257*3f9d6ad7SLin Ling err = td->td_func(td->td_spa, NULL, bp, pbuf, zb, dnp, 258*3f9d6ad7SLin Ling td->td_arg); 259*3f9d6ad7SLin Ling } 260fa9e4066Sahrens 261cd088ea4SVictor Latushkin return (err != 0 ? err : lasterr); 262fa9e4066Sahrens } 263fa9e4066Sahrens 26414843421SMatthew Ahrens static int 26514843421SMatthew Ahrens traverse_dnode(struct traverse_data *td, const dnode_phys_t *dnp, 26614843421SMatthew Ahrens arc_buf_t *buf, uint64_t objset, uint64_t object) 26714843421SMatthew Ahrens { 268cd088ea4SVictor Latushkin int j, err = 0, lasterr = 0; 26914843421SMatthew Ahrens zbookmark_t czb; 270cd088ea4SVictor Latushkin boolean_t hard = (td->td_flags & TRAVERSE_HARD); 27114843421SMatthew Ahrens 27214843421SMatthew Ahrens for (j = 0; j < dnp->dn_nblkptr; j++) { 27314843421SMatthew Ahrens SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j); 27414843421SMatthew Ahrens err = traverse_visitbp(td, dnp, buf, 27514843421SMatthew Ahrens (blkptr_t *)&dnp->dn_blkptr[j], &czb); 276cd088ea4SVictor Latushkin if (err) { 277cd088ea4SVictor Latushkin if (!hard) 278cd088ea4SVictor Latushkin break; 279cd088ea4SVictor Latushkin lasterr = err; 280cd088ea4SVictor Latushkin } 281*3f9d6ad7SLin Ling } 282*3f9d6ad7SLin Ling 283*3f9d6ad7SLin Ling if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) { 284*3f9d6ad7SLin Ling SET_BOOKMARK(&czb, objset, 285*3f9d6ad7SLin Ling object, 0, DMU_SPILL_BLKID); 286*3f9d6ad7SLin Ling err = traverse_visitbp(td, dnp, buf, 287*3f9d6ad7SLin Ling (blkptr_t *)&dnp->dn_spill, &czb); 288*3f9d6ad7SLin Ling if (err) { 289*3f9d6ad7SLin Ling if (!hard) 290*3f9d6ad7SLin Ling return (err); 291*3f9d6ad7SLin Ling lasterr = err; 2920a586ceaSMark Shellenbaum } 29314843421SMatthew Ahrens } 294cd088ea4SVictor Latushkin return (err != 0 ? err : lasterr); 29514843421SMatthew Ahrens } 29614843421SMatthew Ahrens 29788b7b0f2SMatthew Ahrens /* ARGSUSED */ 29888b7b0f2SMatthew Ahrens static int 299b24ab676SJeff Bonwick traverse_prefetcher(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 300*3f9d6ad7SLin Ling arc_buf_t *pbuf, const zbookmark_t *zb, const dnode_phys_t *dnp, 301*3f9d6ad7SLin Ling void *arg) 302e7cbe64fSgw { 30388b7b0f2SMatthew Ahrens struct prefetch_data *pfd = arg; 30488b7b0f2SMatthew Ahrens uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH; 305e7cbe64fSgw 30688b7b0f2SMatthew Ahrens ASSERT(pfd->pd_blks_fetched >= 0); 30788b7b0f2SMatthew Ahrens if (pfd->pd_cancel) 30888b7b0f2SMatthew Ahrens return (EINTR); 309e7cbe64fSgw 31088b7b0f2SMatthew Ahrens if (bp == NULL || !((pfd->pd_flags & TRAVERSE_PREFETCH_DATA) || 3116e1f5caaSNeil Perrin BP_GET_TYPE(bp) == DMU_OT_DNODE || BP_GET_LEVEL(bp) > 0) || 3126e1f5caaSNeil Perrin BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG) 313fa9e4066Sahrens return (0); 314fa9e4066Sahrens 31588b7b0f2SMatthew Ahrens mutex_enter(&pfd->pd_mtx); 31688b7b0f2SMatthew Ahrens while (!pfd->pd_cancel && pfd->pd_blks_fetched >= pfd->pd_blks_max) 31788b7b0f2SMatthew Ahrens cv_wait(&pfd->pd_cv, &pfd->pd_mtx); 31888b7b0f2SMatthew Ahrens pfd->pd_blks_fetched++; 31988b7b0f2SMatthew Ahrens cv_broadcast(&pfd->pd_cv); 32088b7b0f2SMatthew Ahrens mutex_exit(&pfd->pd_mtx); 321fa9e4066Sahrens 322*3f9d6ad7SLin Ling (void) dsl_read(NULL, spa, bp, pbuf, NULL, NULL, 32388b7b0f2SMatthew Ahrens ZIO_PRIORITY_ASYNC_READ, 32488b7b0f2SMatthew Ahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, 32588b7b0f2SMatthew Ahrens &aflags, zb); 326fa9e4066Sahrens 32788b7b0f2SMatthew Ahrens return (0); 328fa9e4066Sahrens } 329fa9e4066Sahrens 330fa9e4066Sahrens static void 33188b7b0f2SMatthew Ahrens traverse_prefetch_thread(void *arg) 332fa9e4066Sahrens { 33388b7b0f2SMatthew Ahrens struct traverse_data *td_main = arg; 33488b7b0f2SMatthew Ahrens struct traverse_data td = *td_main; 33588b7b0f2SMatthew Ahrens zbookmark_t czb; 336fa9e4066Sahrens 33788b7b0f2SMatthew Ahrens td.td_func = traverse_prefetcher; 33888b7b0f2SMatthew Ahrens td.td_arg = td_main->td_pfd; 33988b7b0f2SMatthew Ahrens td.td_pfd = NULL; 340fa9e4066Sahrens 341b24ab676SJeff Bonwick SET_BOOKMARK(&czb, td.td_objset, 342b24ab676SJeff Bonwick ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 34388b7b0f2SMatthew Ahrens (void) traverse_visitbp(&td, NULL, NULL, td.td_rootbp, &czb); 344fa9e4066Sahrens 34588b7b0f2SMatthew Ahrens mutex_enter(&td_main->td_pfd->pd_mtx); 34688b7b0f2SMatthew Ahrens td_main->td_pfd->pd_exited = B_TRUE; 34788b7b0f2SMatthew Ahrens cv_broadcast(&td_main->td_pfd->pd_cv); 34888b7b0f2SMatthew Ahrens mutex_exit(&td_main->td_pfd->pd_mtx); 349fa9e4066Sahrens } 350fa9e4066Sahrens 35188b7b0f2SMatthew Ahrens /* 35288b7b0f2SMatthew Ahrens * NB: dataset must not be changing on-disk (eg, is a snapshot or we are 35388b7b0f2SMatthew Ahrens * in syncing context). 35488b7b0f2SMatthew Ahrens */ 35588b7b0f2SMatthew Ahrens static int 35688b7b0f2SMatthew Ahrens traverse_impl(spa_t *spa, uint64_t objset, blkptr_t *rootbp, 35788b7b0f2SMatthew Ahrens uint64_t txg_start, int flags, blkptr_cb_t func, void *arg) 358fa9e4066Sahrens { 35988b7b0f2SMatthew Ahrens struct traverse_data td; 36088b7b0f2SMatthew Ahrens struct prefetch_data pd = { 0 }; 36188b7b0f2SMatthew Ahrens zbookmark_t czb; 36288b7b0f2SMatthew Ahrens int err; 363fa9e4066Sahrens 36488b7b0f2SMatthew Ahrens td.td_spa = spa; 36588b7b0f2SMatthew Ahrens td.td_objset = objset; 36688b7b0f2SMatthew Ahrens td.td_rootbp = rootbp; 36788b7b0f2SMatthew Ahrens td.td_min_txg = txg_start; 36888b7b0f2SMatthew Ahrens td.td_func = func; 36988b7b0f2SMatthew Ahrens td.td_arg = arg; 37088b7b0f2SMatthew Ahrens td.td_pfd = &pd; 37188b7b0f2SMatthew Ahrens td.td_flags = flags; 37288b7b0f2SMatthew Ahrens 37388b7b0f2SMatthew Ahrens pd.pd_blks_max = 100; 37488b7b0f2SMatthew Ahrens pd.pd_flags = flags; 37588b7b0f2SMatthew Ahrens mutex_init(&pd.pd_mtx, NULL, MUTEX_DEFAULT, NULL); 37688b7b0f2SMatthew Ahrens cv_init(&pd.pd_cv, NULL, CV_DEFAULT, NULL); 37788b7b0f2SMatthew Ahrens 37888b7b0f2SMatthew Ahrens if (!(flags & TRAVERSE_PREFETCH) || 37988b7b0f2SMatthew Ahrens 0 == taskq_dispatch(system_taskq, traverse_prefetch_thread, 38088b7b0f2SMatthew Ahrens &td, TQ_NOQUEUE)) 38188b7b0f2SMatthew Ahrens pd.pd_exited = B_TRUE; 38288b7b0f2SMatthew Ahrens 383b24ab676SJeff Bonwick SET_BOOKMARK(&czb, objset, 384b24ab676SJeff Bonwick ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 38588b7b0f2SMatthew Ahrens err = traverse_visitbp(&td, NULL, NULL, rootbp, &czb); 38688b7b0f2SMatthew Ahrens 38788b7b0f2SMatthew Ahrens mutex_enter(&pd.pd_mtx); 38888b7b0f2SMatthew Ahrens pd.pd_cancel = B_TRUE; 38988b7b0f2SMatthew Ahrens cv_broadcast(&pd.pd_cv); 39088b7b0f2SMatthew Ahrens while (!pd.pd_exited) 39188b7b0f2SMatthew Ahrens cv_wait(&pd.pd_cv, &pd.pd_mtx); 39288b7b0f2SMatthew Ahrens mutex_exit(&pd.pd_mtx); 39388b7b0f2SMatthew Ahrens 39488b7b0f2SMatthew Ahrens mutex_destroy(&pd.pd_mtx); 39588b7b0f2SMatthew Ahrens cv_destroy(&pd.pd_cv); 396fa9e4066Sahrens 39788b7b0f2SMatthew Ahrens return (err); 398fa9e4066Sahrens } 399fa9e4066Sahrens 40088b7b0f2SMatthew Ahrens /* 40188b7b0f2SMatthew Ahrens * NB: dataset must not be changing on-disk (eg, is a snapshot or we are 40288b7b0f2SMatthew Ahrens * in syncing context). 40388b7b0f2SMatthew Ahrens */ 40488b7b0f2SMatthew Ahrens int 40588b7b0f2SMatthew Ahrens traverse_dataset(dsl_dataset_t *ds, uint64_t txg_start, int flags, 40688b7b0f2SMatthew Ahrens blkptr_cb_t func, void *arg) 407fa9e4066Sahrens { 40888b7b0f2SMatthew Ahrens return (traverse_impl(ds->ds_dir->dd_pool->dp_spa, ds->ds_object, 40988b7b0f2SMatthew Ahrens &ds->ds_phys->ds_bp, txg_start, flags, func, arg)); 410fa9e4066Sahrens } 411fa9e4066Sahrens 41288b7b0f2SMatthew Ahrens /* 41388b7b0f2SMatthew Ahrens * NB: pool must not be changing on-disk (eg, from zdb or sync context). 41488b7b0f2SMatthew Ahrens */ 41588b7b0f2SMatthew Ahrens int 416bbfd46c4SJeff Bonwick traverse_pool(spa_t *spa, uint64_t txg_start, int flags, 417bbfd46c4SJeff Bonwick blkptr_cb_t func, void *arg) 418fa9e4066Sahrens { 419cd088ea4SVictor Latushkin int err, lasterr = 0; 42088b7b0f2SMatthew Ahrens uint64_t obj; 42188b7b0f2SMatthew Ahrens dsl_pool_t *dp = spa_get_dsl(spa); 42288b7b0f2SMatthew Ahrens objset_t *mos = dp->dp_meta_objset; 423cd088ea4SVictor Latushkin boolean_t hard = (flags & TRAVERSE_HARD); 42488b7b0f2SMatthew Ahrens 42588b7b0f2SMatthew Ahrens /* visit the MOS */ 42688b7b0f2SMatthew Ahrens err = traverse_impl(spa, 0, spa_get_rootblkptr(spa), 427bbfd46c4SJeff Bonwick txg_start, flags, func, arg); 42888b7b0f2SMatthew Ahrens if (err) 42988b7b0f2SMatthew Ahrens return (err); 43088b7b0f2SMatthew Ahrens 43188b7b0f2SMatthew Ahrens /* visit each dataset */ 432cd088ea4SVictor Latushkin for (obj = 1; err == 0 || (err != ESRCH && hard); 433cd088ea4SVictor Latushkin err = dmu_object_next(mos, &obj, FALSE, txg_start)) { 43488b7b0f2SMatthew Ahrens dmu_object_info_t doi; 43588b7b0f2SMatthew Ahrens 43688b7b0f2SMatthew Ahrens err = dmu_object_info(mos, obj, &doi); 437cd088ea4SVictor Latushkin if (err) { 438cd088ea4SVictor Latushkin if (!hard) 439cd088ea4SVictor Latushkin return (err); 440cd088ea4SVictor Latushkin lasterr = err; 441cd088ea4SVictor Latushkin continue; 442cd088ea4SVictor Latushkin } 44388b7b0f2SMatthew Ahrens 44488b7b0f2SMatthew Ahrens if (doi.doi_type == DMU_OT_DSL_DATASET) { 44588b7b0f2SMatthew Ahrens dsl_dataset_t *ds; 446468c413aSTim Haley uint64_t txg = txg_start; 447468c413aSTim Haley 44888b7b0f2SMatthew Ahrens rw_enter(&dp->dp_config_rwlock, RW_READER); 44988b7b0f2SMatthew Ahrens err = dsl_dataset_hold_obj(dp, obj, FTAG, &ds); 45088b7b0f2SMatthew Ahrens rw_exit(&dp->dp_config_rwlock); 451cd088ea4SVictor Latushkin if (err) { 452cd088ea4SVictor Latushkin if (!hard) 453cd088ea4SVictor Latushkin return (err); 454cd088ea4SVictor Latushkin lasterr = err; 455cd088ea4SVictor Latushkin continue; 456cd088ea4SVictor Latushkin } 457468c413aSTim Haley if (ds->ds_phys->ds_prev_snap_txg > txg) 458468c413aSTim Haley txg = ds->ds_phys->ds_prev_snap_txg; 459bbfd46c4SJeff Bonwick err = traverse_dataset(ds, txg, flags, func, arg); 46088b7b0f2SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 461cd088ea4SVictor Latushkin if (err) { 462cd088ea4SVictor Latushkin if (!hard) 463cd088ea4SVictor Latushkin return (err); 464cd088ea4SVictor Latushkin lasterr = err; 465cd088ea4SVictor Latushkin } 46688b7b0f2SMatthew Ahrens } 467fa9e4066Sahrens } 46888b7b0f2SMatthew Ahrens if (err == ESRCH) 46988b7b0f2SMatthew Ahrens err = 0; 470cd088ea4SVictor Latushkin return (err != 0 ? err : lasterr); 471fa9e4066Sahrens } 472