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. 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 39*44f92b7dSChris Kirby int zfs_pd_blks_max = 100; 40*44f92b7dSChris Kirby 4188b7b0f2SMatthew Ahrens struct prefetch_data { 4288b7b0f2SMatthew Ahrens kmutex_t pd_mtx; 4388b7b0f2SMatthew Ahrens kcondvar_t pd_cv; 4488b7b0f2SMatthew Ahrens int pd_blks_max; 4588b7b0f2SMatthew Ahrens int pd_blks_fetched; 4688b7b0f2SMatthew Ahrens int pd_flags; 4788b7b0f2SMatthew Ahrens boolean_t pd_cancel; 4888b7b0f2SMatthew Ahrens boolean_t pd_exited; 4988b7b0f2SMatthew Ahrens }; 5088b7b0f2SMatthew Ahrens 5188b7b0f2SMatthew Ahrens struct traverse_data { 5288b7b0f2SMatthew Ahrens spa_t *td_spa; 5388b7b0f2SMatthew Ahrens uint64_t td_objset; 5488b7b0f2SMatthew Ahrens blkptr_t *td_rootbp; 5588b7b0f2SMatthew Ahrens uint64_t td_min_txg; 5688b7b0f2SMatthew Ahrens int td_flags; 5788b7b0f2SMatthew Ahrens struct prefetch_data *td_pfd; 5888b7b0f2SMatthew Ahrens blkptr_cb_t *td_func; 5988b7b0f2SMatthew Ahrens void *td_arg; 6088b7b0f2SMatthew Ahrens }; 61fa9e4066Sahrens 6214843421SMatthew Ahrens static int traverse_dnode(struct traverse_data *td, const dnode_phys_t *dnp, 6314843421SMatthew Ahrens arc_buf_t *buf, uint64_t objset, uint64_t object); 6414843421SMatthew Ahrens 65ea8dc4b6Seschrock /* ARGSUSED */ 66b24ab676SJeff Bonwick static int 675dabedeeSbonwick traverse_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg) 68ea8dc4b6Seschrock { 6988b7b0f2SMatthew Ahrens struct traverse_data *td = arg; 7088b7b0f2SMatthew Ahrens zbookmark_t zb; 71ea8dc4b6Seschrock 7288b7b0f2SMatthew Ahrens if (bp->blk_birth == 0) 73b24ab676SJeff Bonwick return (0); 745dabedeeSbonwick 7588b7b0f2SMatthew Ahrens if (claim_txg == 0 && bp->blk_birth >= spa_first_txg(td->td_spa)) 76b24ab676SJeff Bonwick return (0); 7788b7b0f2SMatthew Ahrens 78b24ab676SJeff Bonwick SET_BOOKMARK(&zb, td->td_objset, ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, 79b24ab676SJeff Bonwick bp->blk_cksum.zc_word[ZIL_ZC_SEQ]); 80b24ab676SJeff Bonwick 813f9d6ad7SLin Ling (void) td->td_func(td->td_spa, zilog, bp, NULL, &zb, NULL, td->td_arg); 82b24ab676SJeff Bonwick 83b24ab676SJeff Bonwick return (0); 84ea8dc4b6Seschrock } 85ea8dc4b6Seschrock 86ea8dc4b6Seschrock /* ARGSUSED */ 87b24ab676SJeff Bonwick static int 885dabedeeSbonwick traverse_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg) 89ea8dc4b6Seschrock { 9088b7b0f2SMatthew Ahrens struct traverse_data *td = arg; 91ea8dc4b6Seschrock 92ea8dc4b6Seschrock if (lrc->lrc_txtype == TX_WRITE) { 93ea8dc4b6Seschrock lr_write_t *lr = (lr_write_t *)lrc; 94ea8dc4b6Seschrock blkptr_t *bp = &lr->lr_blkptr; 9588b7b0f2SMatthew Ahrens zbookmark_t zb; 96ea8dc4b6Seschrock 9788b7b0f2SMatthew Ahrens if (bp->blk_birth == 0) 98b24ab676SJeff Bonwick return (0); 995dabedeeSbonwick 10088b7b0f2SMatthew Ahrens if (claim_txg == 0 || bp->blk_birth < claim_txg) 101b24ab676SJeff Bonwick return (0); 102b24ab676SJeff Bonwick 103b24ab676SJeff Bonwick SET_BOOKMARK(&zb, td->td_objset, lr->lr_foid, ZB_ZIL_LEVEL, 104b24ab676SJeff Bonwick lr->lr_offset / BP_GET_LSIZE(bp)); 10588b7b0f2SMatthew Ahrens 1063f9d6ad7SLin Ling (void) td->td_func(td->td_spa, zilog, bp, NULL, &zb, NULL, 107b24ab676SJeff Bonwick td->td_arg); 108ea8dc4b6Seschrock } 109b24ab676SJeff Bonwick return (0); 110ea8dc4b6Seschrock } 111ea8dc4b6Seschrock 112ea8dc4b6Seschrock static void 11388b7b0f2SMatthew Ahrens traverse_zil(struct traverse_data *td, zil_header_t *zh) 114ea8dc4b6Seschrock { 1155dabedeeSbonwick uint64_t claim_txg = zh->zh_claim_txg; 116ea8dc4b6Seschrock zilog_t *zilog; 117ea8dc4b6Seschrock 1185dabedeeSbonwick /* 1195dabedeeSbonwick * We only want to visit blocks that have been claimed but not yet 120b24ab676SJeff Bonwick * replayed; plus, in read-only mode, blocks that are already stable. 1215dabedeeSbonwick */ 1228ad4d6ddSJeff Bonwick if (claim_txg == 0 && spa_writeable(td->td_spa)) 1235dabedeeSbonwick return; 1245dabedeeSbonwick 12588b7b0f2SMatthew Ahrens zilog = zil_alloc(spa_get_dsl(td->td_spa)->dp_meta_objset, zh); 126ea8dc4b6Seschrock 12788b7b0f2SMatthew Ahrens (void) zil_parse(zilog, traverse_zil_block, traverse_zil_record, td, 1285dabedeeSbonwick claim_txg); 129ea8dc4b6Seschrock 130ea8dc4b6Seschrock zil_free(zilog); 131ea8dc4b6Seschrock } 132ea8dc4b6Seschrock 133fa9e4066Sahrens static int 13488b7b0f2SMatthew Ahrens traverse_visitbp(struct traverse_data *td, const dnode_phys_t *dnp, 13588b7b0f2SMatthew Ahrens arc_buf_t *pbuf, blkptr_t *bp, const zbookmark_t *zb) 136fa9e4066Sahrens { 1376a0f0066SEric Taylor zbookmark_t czb; 138cd088ea4SVictor Latushkin int err = 0, lasterr = 0; 13988b7b0f2SMatthew Ahrens arc_buf_t *buf = NULL; 14088b7b0f2SMatthew Ahrens struct prefetch_data *pd = td->td_pfd; 141cd088ea4SVictor Latushkin boolean_t hard = td->td_flags & TRAVERSE_HARD; 142fa9e4066Sahrens 14388b7b0f2SMatthew Ahrens if (bp->blk_birth == 0) { 1443f9d6ad7SLin Ling err = td->td_func(td->td_spa, NULL, NULL, pbuf, zb, dnp, 1453f9d6ad7SLin Ling td->td_arg); 14688b7b0f2SMatthew Ahrens return (err); 147fa9e4066Sahrens } 148fa9e4066Sahrens 14988b7b0f2SMatthew Ahrens if (bp->blk_birth <= td->td_min_txg) 15088b7b0f2SMatthew Ahrens return (0); 151fa9e4066Sahrens 15288b7b0f2SMatthew Ahrens if (pd && !pd->pd_exited && 15388b7b0f2SMatthew Ahrens ((pd->pd_flags & TRAVERSE_PREFETCH_DATA) || 15488b7b0f2SMatthew Ahrens BP_GET_TYPE(bp) == DMU_OT_DNODE || BP_GET_LEVEL(bp) > 0)) { 15588b7b0f2SMatthew Ahrens mutex_enter(&pd->pd_mtx); 15688b7b0f2SMatthew Ahrens ASSERT(pd->pd_blks_fetched >= 0); 15788b7b0f2SMatthew Ahrens while (pd->pd_blks_fetched == 0 && !pd->pd_exited) 15888b7b0f2SMatthew Ahrens cv_wait(&pd->pd_cv, &pd->pd_mtx); 15988b7b0f2SMatthew Ahrens pd->pd_blks_fetched--; 16088b7b0f2SMatthew Ahrens cv_broadcast(&pd->pd_cv); 16188b7b0f2SMatthew Ahrens mutex_exit(&pd->pd_mtx); 162fa9e4066Sahrens } 163fa9e4066Sahrens 16488b7b0f2SMatthew Ahrens if (td->td_flags & TRAVERSE_PRE) { 1653f9d6ad7SLin Ling err = td->td_func(td->td_spa, NULL, bp, pbuf, zb, dnp, 1663f9d6ad7SLin Ling td->td_arg); 16788b7b0f2SMatthew Ahrens if (err) 16888b7b0f2SMatthew Ahrens return (err); 169fa9e4066Sahrens } 170fa9e4066Sahrens 17188b7b0f2SMatthew Ahrens if (BP_GET_LEVEL(bp) > 0) { 17288b7b0f2SMatthew Ahrens uint32_t flags = ARC_WAIT; 17388b7b0f2SMatthew Ahrens int i; 17488b7b0f2SMatthew Ahrens blkptr_t *cbp; 17588b7b0f2SMatthew Ahrens int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT; 17688b7b0f2SMatthew Ahrens 1773f9d6ad7SLin Ling err = dsl_read(NULL, td->td_spa, bp, pbuf, 17888b7b0f2SMatthew Ahrens arc_getbuf_func, &buf, 17988b7b0f2SMatthew Ahrens ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb); 18088b7b0f2SMatthew Ahrens if (err) 18188b7b0f2SMatthew Ahrens return (err); 18288b7b0f2SMatthew Ahrens 18388b7b0f2SMatthew Ahrens /* recursively visitbp() blocks below this */ 18488b7b0f2SMatthew Ahrens cbp = buf->b_data; 18588b7b0f2SMatthew Ahrens for (i = 0; i < epb; i++, cbp++) { 18688b7b0f2SMatthew Ahrens SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object, 18788b7b0f2SMatthew Ahrens zb->zb_level - 1, 18888b7b0f2SMatthew Ahrens zb->zb_blkid * epb + i); 18988b7b0f2SMatthew Ahrens err = traverse_visitbp(td, dnp, buf, cbp, &czb); 190cd088ea4SVictor Latushkin if (err) { 191cd088ea4SVictor Latushkin if (!hard) 192cd088ea4SVictor Latushkin break; 193cd088ea4SVictor Latushkin lasterr = err; 194cd088ea4SVictor Latushkin } 19588b7b0f2SMatthew Ahrens } 19688b7b0f2SMatthew Ahrens } else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) { 19788b7b0f2SMatthew Ahrens uint32_t flags = ARC_WAIT; 19814843421SMatthew Ahrens int i; 19988b7b0f2SMatthew Ahrens int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT; 20088b7b0f2SMatthew Ahrens 2013f9d6ad7SLin Ling err = dsl_read(NULL, td->td_spa, bp, pbuf, 20288b7b0f2SMatthew Ahrens arc_getbuf_func, &buf, 20388b7b0f2SMatthew Ahrens ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb); 20488b7b0f2SMatthew Ahrens if (err) 20588b7b0f2SMatthew Ahrens return (err); 20688b7b0f2SMatthew Ahrens 20788b7b0f2SMatthew Ahrens /* recursively visitbp() blocks below this */ 20888b7b0f2SMatthew Ahrens dnp = buf->b_data; 209cd088ea4SVictor Latushkin for (i = 0; i < epb; i++, dnp++) { 21014843421SMatthew Ahrens err = traverse_dnode(td, dnp, buf, zb->zb_objset, 21114843421SMatthew Ahrens zb->zb_blkid * epb + i); 212cd088ea4SVictor Latushkin if (err) { 213cd088ea4SVictor Latushkin if (!hard) 214cd088ea4SVictor Latushkin break; 215cd088ea4SVictor Latushkin lasterr = err; 216cd088ea4SVictor Latushkin } 217fa9e4066Sahrens } 21888b7b0f2SMatthew Ahrens } else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) { 21988b7b0f2SMatthew Ahrens uint32_t flags = ARC_WAIT; 22088b7b0f2SMatthew Ahrens objset_phys_t *osp; 22114843421SMatthew Ahrens dnode_phys_t *dnp; 22288b7b0f2SMatthew Ahrens 2233f9d6ad7SLin Ling err = dsl_read_nolock(NULL, td->td_spa, bp, 22488b7b0f2SMatthew Ahrens arc_getbuf_func, &buf, 22588b7b0f2SMatthew Ahrens ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb); 22688b7b0f2SMatthew Ahrens if (err) 22788b7b0f2SMatthew Ahrens return (err); 22888b7b0f2SMatthew Ahrens 22988b7b0f2SMatthew Ahrens osp = buf->b_data; 23088b7b0f2SMatthew Ahrens traverse_zil(td, &osp->os_zil_header); 231fa9e4066Sahrens 23214843421SMatthew Ahrens dnp = &osp->os_meta_dnode; 233b24ab676SJeff Bonwick err = traverse_dnode(td, dnp, buf, zb->zb_objset, 234b24ab676SJeff Bonwick DMU_META_DNODE_OBJECT); 235cd088ea4SVictor Latushkin if (err && hard) { 236cd088ea4SVictor Latushkin lasterr = err; 237cd088ea4SVictor Latushkin err = 0; 238cd088ea4SVictor Latushkin } 23914843421SMatthew Ahrens if (err == 0 && arc_buf_size(buf) >= sizeof (objset_phys_t)) { 24014843421SMatthew Ahrens dnp = &osp->os_userused_dnode; 24114843421SMatthew Ahrens err = traverse_dnode(td, dnp, buf, zb->zb_objset, 24214843421SMatthew Ahrens DMU_USERUSED_OBJECT); 24314843421SMatthew Ahrens } 244cd088ea4SVictor Latushkin if (err && hard) { 245cd088ea4SVictor Latushkin lasterr = err; 246cd088ea4SVictor Latushkin err = 0; 247cd088ea4SVictor Latushkin } 24814843421SMatthew Ahrens if (err == 0 && arc_buf_size(buf) >= sizeof (objset_phys_t)) { 24914843421SMatthew Ahrens dnp = &osp->os_groupused_dnode; 25014843421SMatthew Ahrens err = traverse_dnode(td, dnp, buf, zb->zb_objset, 25114843421SMatthew Ahrens DMU_GROUPUSED_OBJECT); 25288b7b0f2SMatthew Ahrens } 25388b7b0f2SMatthew Ahrens } 254fa9e4066Sahrens 25588b7b0f2SMatthew Ahrens if (buf) 25688b7b0f2SMatthew Ahrens (void) arc_buf_remove_ref(buf, &buf); 257fa9e4066Sahrens 2583f9d6ad7SLin Ling if (err == 0 && lasterr == 0 && (td->td_flags & TRAVERSE_POST)) { 2593f9d6ad7SLin Ling err = td->td_func(td->td_spa, NULL, bp, pbuf, zb, dnp, 2603f9d6ad7SLin Ling td->td_arg); 2613f9d6ad7SLin Ling } 262fa9e4066Sahrens 263cd088ea4SVictor Latushkin return (err != 0 ? err : lasterr); 264fa9e4066Sahrens } 265fa9e4066Sahrens 26614843421SMatthew Ahrens static int 26714843421SMatthew Ahrens traverse_dnode(struct traverse_data *td, const dnode_phys_t *dnp, 26814843421SMatthew Ahrens arc_buf_t *buf, uint64_t objset, uint64_t object) 26914843421SMatthew Ahrens { 270cd088ea4SVictor Latushkin int j, err = 0, lasterr = 0; 27114843421SMatthew Ahrens zbookmark_t czb; 272cd088ea4SVictor Latushkin boolean_t hard = (td->td_flags & TRAVERSE_HARD); 27314843421SMatthew Ahrens 27414843421SMatthew Ahrens for (j = 0; j < dnp->dn_nblkptr; j++) { 27514843421SMatthew Ahrens SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j); 27614843421SMatthew Ahrens err = traverse_visitbp(td, dnp, buf, 27714843421SMatthew Ahrens (blkptr_t *)&dnp->dn_blkptr[j], &czb); 278cd088ea4SVictor Latushkin if (err) { 279cd088ea4SVictor Latushkin if (!hard) 280cd088ea4SVictor Latushkin break; 281cd088ea4SVictor Latushkin lasterr = err; 282cd088ea4SVictor Latushkin } 2833f9d6ad7SLin Ling } 2843f9d6ad7SLin Ling 2853f9d6ad7SLin Ling if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) { 2863f9d6ad7SLin Ling SET_BOOKMARK(&czb, objset, 2873f9d6ad7SLin Ling object, 0, DMU_SPILL_BLKID); 2883f9d6ad7SLin Ling err = traverse_visitbp(td, dnp, buf, 2893f9d6ad7SLin Ling (blkptr_t *)&dnp->dn_spill, &czb); 2903f9d6ad7SLin Ling if (err) { 2913f9d6ad7SLin Ling if (!hard) 2923f9d6ad7SLin Ling return (err); 2933f9d6ad7SLin Ling lasterr = err; 2940a586ceaSMark Shellenbaum } 29514843421SMatthew Ahrens } 296cd088ea4SVictor Latushkin return (err != 0 ? err : lasterr); 29714843421SMatthew Ahrens } 29814843421SMatthew Ahrens 29988b7b0f2SMatthew Ahrens /* ARGSUSED */ 30088b7b0f2SMatthew Ahrens static int 301b24ab676SJeff Bonwick traverse_prefetcher(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 3023f9d6ad7SLin Ling arc_buf_t *pbuf, const zbookmark_t *zb, const dnode_phys_t *dnp, 3033f9d6ad7SLin Ling void *arg) 304e7cbe64fSgw { 30588b7b0f2SMatthew Ahrens struct prefetch_data *pfd = arg; 30688b7b0f2SMatthew Ahrens uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH; 307e7cbe64fSgw 30888b7b0f2SMatthew Ahrens ASSERT(pfd->pd_blks_fetched >= 0); 30988b7b0f2SMatthew Ahrens if (pfd->pd_cancel) 31088b7b0f2SMatthew Ahrens return (EINTR); 311e7cbe64fSgw 31288b7b0f2SMatthew Ahrens if (bp == NULL || !((pfd->pd_flags & TRAVERSE_PREFETCH_DATA) || 3136e1f5caaSNeil Perrin BP_GET_TYPE(bp) == DMU_OT_DNODE || BP_GET_LEVEL(bp) > 0) || 3146e1f5caaSNeil Perrin BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG) 315fa9e4066Sahrens return (0); 316fa9e4066Sahrens 31788b7b0f2SMatthew Ahrens mutex_enter(&pfd->pd_mtx); 31888b7b0f2SMatthew Ahrens while (!pfd->pd_cancel && pfd->pd_blks_fetched >= pfd->pd_blks_max) 31988b7b0f2SMatthew Ahrens cv_wait(&pfd->pd_cv, &pfd->pd_mtx); 32088b7b0f2SMatthew Ahrens pfd->pd_blks_fetched++; 32188b7b0f2SMatthew Ahrens cv_broadcast(&pfd->pd_cv); 32288b7b0f2SMatthew Ahrens mutex_exit(&pfd->pd_mtx); 323fa9e4066Sahrens 3243f9d6ad7SLin Ling (void) dsl_read(NULL, spa, bp, pbuf, NULL, NULL, 32588b7b0f2SMatthew Ahrens ZIO_PRIORITY_ASYNC_READ, 32688b7b0f2SMatthew Ahrens ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, 32788b7b0f2SMatthew Ahrens &aflags, zb); 328fa9e4066Sahrens 32988b7b0f2SMatthew Ahrens return (0); 330fa9e4066Sahrens } 331fa9e4066Sahrens 332fa9e4066Sahrens static void 33388b7b0f2SMatthew Ahrens traverse_prefetch_thread(void *arg) 334fa9e4066Sahrens { 33588b7b0f2SMatthew Ahrens struct traverse_data *td_main = arg; 33688b7b0f2SMatthew Ahrens struct traverse_data td = *td_main; 33788b7b0f2SMatthew Ahrens zbookmark_t czb; 338fa9e4066Sahrens 33988b7b0f2SMatthew Ahrens td.td_func = traverse_prefetcher; 34088b7b0f2SMatthew Ahrens td.td_arg = td_main->td_pfd; 34188b7b0f2SMatthew Ahrens td.td_pfd = NULL; 342fa9e4066Sahrens 343b24ab676SJeff Bonwick SET_BOOKMARK(&czb, td.td_objset, 344b24ab676SJeff Bonwick ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 34588b7b0f2SMatthew Ahrens (void) traverse_visitbp(&td, NULL, NULL, td.td_rootbp, &czb); 346fa9e4066Sahrens 34788b7b0f2SMatthew Ahrens mutex_enter(&td_main->td_pfd->pd_mtx); 34888b7b0f2SMatthew Ahrens td_main->td_pfd->pd_exited = B_TRUE; 34988b7b0f2SMatthew Ahrens cv_broadcast(&td_main->td_pfd->pd_cv); 35088b7b0f2SMatthew Ahrens mutex_exit(&td_main->td_pfd->pd_mtx); 351fa9e4066Sahrens } 352fa9e4066Sahrens 35388b7b0f2SMatthew Ahrens /* 35488b7b0f2SMatthew Ahrens * NB: dataset must not be changing on-disk (eg, is a snapshot or we are 35588b7b0f2SMatthew Ahrens * in syncing context). 35688b7b0f2SMatthew Ahrens */ 35788b7b0f2SMatthew Ahrens static int 35888b7b0f2SMatthew Ahrens traverse_impl(spa_t *spa, uint64_t objset, blkptr_t *rootbp, 35988b7b0f2SMatthew Ahrens uint64_t txg_start, int flags, blkptr_cb_t func, void *arg) 360fa9e4066Sahrens { 36188b7b0f2SMatthew Ahrens struct traverse_data td; 36288b7b0f2SMatthew Ahrens struct prefetch_data pd = { 0 }; 36388b7b0f2SMatthew Ahrens zbookmark_t czb; 36488b7b0f2SMatthew Ahrens int err; 365fa9e4066Sahrens 36688b7b0f2SMatthew Ahrens td.td_spa = spa; 36788b7b0f2SMatthew Ahrens td.td_objset = objset; 36888b7b0f2SMatthew Ahrens td.td_rootbp = rootbp; 36988b7b0f2SMatthew Ahrens td.td_min_txg = txg_start; 37088b7b0f2SMatthew Ahrens td.td_func = func; 37188b7b0f2SMatthew Ahrens td.td_arg = arg; 37288b7b0f2SMatthew Ahrens td.td_pfd = &pd; 37388b7b0f2SMatthew Ahrens td.td_flags = flags; 37488b7b0f2SMatthew Ahrens 375*44f92b7dSChris Kirby pd.pd_blks_max = zfs_pd_blks_max; 37688b7b0f2SMatthew Ahrens pd.pd_flags = flags; 37788b7b0f2SMatthew Ahrens mutex_init(&pd.pd_mtx, NULL, MUTEX_DEFAULT, NULL); 37888b7b0f2SMatthew Ahrens cv_init(&pd.pd_cv, NULL, CV_DEFAULT, NULL); 37988b7b0f2SMatthew Ahrens 38088b7b0f2SMatthew Ahrens if (!(flags & TRAVERSE_PREFETCH) || 38188b7b0f2SMatthew Ahrens 0 == taskq_dispatch(system_taskq, traverse_prefetch_thread, 38288b7b0f2SMatthew Ahrens &td, TQ_NOQUEUE)) 38388b7b0f2SMatthew Ahrens pd.pd_exited = B_TRUE; 38488b7b0f2SMatthew Ahrens 385b24ab676SJeff Bonwick SET_BOOKMARK(&czb, objset, 386b24ab676SJeff Bonwick ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 38788b7b0f2SMatthew Ahrens err = traverse_visitbp(&td, NULL, NULL, rootbp, &czb); 38888b7b0f2SMatthew Ahrens 38988b7b0f2SMatthew Ahrens mutex_enter(&pd.pd_mtx); 39088b7b0f2SMatthew Ahrens pd.pd_cancel = B_TRUE; 39188b7b0f2SMatthew Ahrens cv_broadcast(&pd.pd_cv); 39288b7b0f2SMatthew Ahrens while (!pd.pd_exited) 39388b7b0f2SMatthew Ahrens cv_wait(&pd.pd_cv, &pd.pd_mtx); 39488b7b0f2SMatthew Ahrens mutex_exit(&pd.pd_mtx); 39588b7b0f2SMatthew Ahrens 39688b7b0f2SMatthew Ahrens mutex_destroy(&pd.pd_mtx); 39788b7b0f2SMatthew Ahrens cv_destroy(&pd.pd_cv); 398fa9e4066Sahrens 39988b7b0f2SMatthew Ahrens return (err); 400fa9e4066Sahrens } 401fa9e4066Sahrens 40288b7b0f2SMatthew Ahrens /* 40388b7b0f2SMatthew Ahrens * NB: dataset must not be changing on-disk (eg, is a snapshot or we are 40488b7b0f2SMatthew Ahrens * in syncing context). 40588b7b0f2SMatthew Ahrens */ 40688b7b0f2SMatthew Ahrens int 40788b7b0f2SMatthew Ahrens traverse_dataset(dsl_dataset_t *ds, uint64_t txg_start, int flags, 40888b7b0f2SMatthew Ahrens blkptr_cb_t func, void *arg) 409fa9e4066Sahrens { 41088b7b0f2SMatthew Ahrens return (traverse_impl(ds->ds_dir->dd_pool->dp_spa, ds->ds_object, 41188b7b0f2SMatthew Ahrens &ds->ds_phys->ds_bp, txg_start, flags, func, arg)); 412fa9e4066Sahrens } 413fa9e4066Sahrens 41488b7b0f2SMatthew Ahrens /* 41588b7b0f2SMatthew Ahrens * NB: pool must not be changing on-disk (eg, from zdb or sync context). 41688b7b0f2SMatthew Ahrens */ 41788b7b0f2SMatthew Ahrens int 418bbfd46c4SJeff Bonwick traverse_pool(spa_t *spa, uint64_t txg_start, int flags, 419bbfd46c4SJeff Bonwick blkptr_cb_t func, void *arg) 420fa9e4066Sahrens { 421cd088ea4SVictor Latushkin int err, lasterr = 0; 42288b7b0f2SMatthew Ahrens uint64_t obj; 42388b7b0f2SMatthew Ahrens dsl_pool_t *dp = spa_get_dsl(spa); 42488b7b0f2SMatthew Ahrens objset_t *mos = dp->dp_meta_objset; 425cd088ea4SVictor Latushkin boolean_t hard = (flags & TRAVERSE_HARD); 42688b7b0f2SMatthew Ahrens 42788b7b0f2SMatthew Ahrens /* visit the MOS */ 42888b7b0f2SMatthew Ahrens err = traverse_impl(spa, 0, spa_get_rootblkptr(spa), 429bbfd46c4SJeff Bonwick txg_start, flags, func, arg); 43088b7b0f2SMatthew Ahrens if (err) 43188b7b0f2SMatthew Ahrens return (err); 43288b7b0f2SMatthew Ahrens 43388b7b0f2SMatthew Ahrens /* visit each dataset */ 434cd088ea4SVictor Latushkin for (obj = 1; err == 0 || (err != ESRCH && hard); 435cd088ea4SVictor Latushkin err = dmu_object_next(mos, &obj, FALSE, txg_start)) { 43688b7b0f2SMatthew Ahrens dmu_object_info_t doi; 43788b7b0f2SMatthew Ahrens 43888b7b0f2SMatthew Ahrens err = dmu_object_info(mos, obj, &doi); 439cd088ea4SVictor Latushkin if (err) { 440cd088ea4SVictor Latushkin if (!hard) 441cd088ea4SVictor Latushkin return (err); 442cd088ea4SVictor Latushkin lasterr = err; 443cd088ea4SVictor Latushkin continue; 444cd088ea4SVictor Latushkin } 44588b7b0f2SMatthew Ahrens 44688b7b0f2SMatthew Ahrens if (doi.doi_type == DMU_OT_DSL_DATASET) { 44788b7b0f2SMatthew Ahrens dsl_dataset_t *ds; 448468c413aSTim Haley uint64_t txg = txg_start; 449468c413aSTim Haley 45088b7b0f2SMatthew Ahrens rw_enter(&dp->dp_config_rwlock, RW_READER); 45188b7b0f2SMatthew Ahrens err = dsl_dataset_hold_obj(dp, obj, FTAG, &ds); 45288b7b0f2SMatthew Ahrens rw_exit(&dp->dp_config_rwlock); 453cd088ea4SVictor Latushkin if (err) { 454cd088ea4SVictor Latushkin if (!hard) 455cd088ea4SVictor Latushkin return (err); 456cd088ea4SVictor Latushkin lasterr = err; 457cd088ea4SVictor Latushkin continue; 458cd088ea4SVictor Latushkin } 459468c413aSTim Haley if (ds->ds_phys->ds_prev_snap_txg > txg) 460468c413aSTim Haley txg = ds->ds_phys->ds_prev_snap_txg; 461bbfd46c4SJeff Bonwick err = traverse_dataset(ds, txg, flags, func, arg); 46288b7b0f2SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 463cd088ea4SVictor Latushkin if (err) { 464cd088ea4SVictor Latushkin if (!hard) 465cd088ea4SVictor Latushkin return (err); 466cd088ea4SVictor Latushkin lasterr = err; 467cd088ea4SVictor Latushkin } 46888b7b0f2SMatthew Ahrens } 469fa9e4066Sahrens } 47088b7b0f2SMatthew Ahrens if (err == ESRCH) 47188b7b0f2SMatthew Ahrens err = 0; 472cd088ea4SVictor Latushkin return (err != 0 ? err : lasterr); 473fa9e4066Sahrens } 474