1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #include <sys/zfs_context.h> 27 #include <sys/dmu_objset.h> 28 #include <sys/dmu_traverse.h> 29 #include <sys/dsl_dataset.h> 30 #include <sys/dsl_dir.h> 31 #include <sys/dsl_pool.h> 32 #include <sys/dnode.h> 33 #include <sys/spa.h> 34 #include <sys/zio.h> 35 #include <sys/dmu_impl.h> 36 #include <sys/sa.h> 37 #include <sys/sa_impl.h> 38 #include <sys/callb.h> 39 40 struct prefetch_data { 41 kmutex_t pd_mtx; 42 kcondvar_t pd_cv; 43 int pd_blks_max; 44 int pd_blks_fetched; 45 int pd_flags; 46 boolean_t pd_cancel; 47 boolean_t pd_exited; 48 }; 49 50 struct traverse_data { 51 spa_t *td_spa; 52 uint64_t td_objset; 53 blkptr_t *td_rootbp; 54 uint64_t td_min_txg; 55 int td_flags; 56 struct prefetch_data *td_pfd; 57 blkptr_cb_t *td_func; 58 void *td_arg; 59 }; 60 61 static int traverse_dnode(struct traverse_data *td, const dnode_phys_t *dnp, 62 arc_buf_t *buf, uint64_t objset, uint64_t object); 63 64 /* ARGSUSED */ 65 static int 66 traverse_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg) 67 { 68 struct traverse_data *td = arg; 69 zbookmark_t zb; 70 71 if (bp->blk_birth == 0) 72 return (0); 73 74 if (claim_txg == 0 && bp->blk_birth >= spa_first_txg(td->td_spa)) 75 return (0); 76 77 SET_BOOKMARK(&zb, td->td_objset, ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, 78 bp->blk_cksum.zc_word[ZIL_ZC_SEQ]); 79 80 (void) td->td_func(td->td_spa, zilog, bp, &zb, NULL, td->td_arg); 81 82 return (0); 83 } 84 85 /* ARGSUSED */ 86 static int 87 traverse_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg) 88 { 89 struct traverse_data *td = arg; 90 91 if (lrc->lrc_txtype == TX_WRITE) { 92 lr_write_t *lr = (lr_write_t *)lrc; 93 blkptr_t *bp = &lr->lr_blkptr; 94 zbookmark_t zb; 95 96 if (bp->blk_birth == 0) 97 return (0); 98 99 if (claim_txg == 0 || bp->blk_birth < claim_txg) 100 return (0); 101 102 SET_BOOKMARK(&zb, td->td_objset, lr->lr_foid, ZB_ZIL_LEVEL, 103 lr->lr_offset / BP_GET_LSIZE(bp)); 104 105 (void) td->td_func(td->td_spa, zilog, bp, &zb, NULL, 106 td->td_arg); 107 } 108 return (0); 109 } 110 111 static void 112 traverse_zil(struct traverse_data *td, zil_header_t *zh) 113 { 114 uint64_t claim_txg = zh->zh_claim_txg; 115 zilog_t *zilog; 116 117 /* 118 * We only want to visit blocks that have been claimed but not yet 119 * replayed; plus, in read-only mode, blocks that are already stable. 120 */ 121 if (claim_txg == 0 && spa_writeable(td->td_spa)) 122 return; 123 124 zilog = zil_alloc(spa_get_dsl(td->td_spa)->dp_meta_objset, zh); 125 126 (void) zil_parse(zilog, traverse_zil_block, traverse_zil_record, td, 127 claim_txg); 128 129 zil_free(zilog); 130 } 131 132 static int 133 traverse_visitbp(struct traverse_data *td, const dnode_phys_t *dnp, 134 arc_buf_t *pbuf, blkptr_t *bp, const zbookmark_t *zb) 135 { 136 zbookmark_t czb; 137 int err = 0, lasterr = 0; 138 arc_buf_t *buf = NULL; 139 struct prefetch_data *pd = td->td_pfd; 140 boolean_t hard = td->td_flags & TRAVERSE_HARD; 141 142 if (bp->blk_birth == 0) { 143 err = td->td_func(td->td_spa, NULL, NULL, zb, dnp, td->td_arg); 144 return (err); 145 } 146 147 if (bp->blk_birth <= td->td_min_txg) 148 return (0); 149 150 if (pd && !pd->pd_exited && 151 ((pd->pd_flags & TRAVERSE_PREFETCH_DATA) || 152 BP_GET_TYPE(bp) == DMU_OT_DNODE || BP_GET_LEVEL(bp) > 0)) { 153 mutex_enter(&pd->pd_mtx); 154 ASSERT(pd->pd_blks_fetched >= 0); 155 while (pd->pd_blks_fetched == 0 && !pd->pd_exited) 156 cv_wait(&pd->pd_cv, &pd->pd_mtx); 157 pd->pd_blks_fetched--; 158 cv_broadcast(&pd->pd_cv); 159 mutex_exit(&pd->pd_mtx); 160 } 161 162 if (td->td_flags & TRAVERSE_PRE) { 163 err = td->td_func(td->td_spa, NULL, bp, zb, dnp, td->td_arg); 164 if (err) 165 return (err); 166 } 167 168 if (BP_GET_LEVEL(bp) > 0) { 169 uint32_t flags = ARC_WAIT; 170 int i; 171 blkptr_t *cbp; 172 int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT; 173 174 err = arc_read(NULL, td->td_spa, bp, pbuf, 175 arc_getbuf_func, &buf, 176 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb); 177 if (err) 178 return (err); 179 180 /* recursively visitbp() blocks below this */ 181 cbp = buf->b_data; 182 for (i = 0; i < epb; i++, cbp++) { 183 SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object, 184 zb->zb_level - 1, 185 zb->zb_blkid * epb + i); 186 err = traverse_visitbp(td, dnp, buf, cbp, &czb); 187 if (err) { 188 if (!hard) 189 break; 190 lasterr = err; 191 } 192 } 193 } else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) { 194 uint32_t flags = ARC_WAIT; 195 int i; 196 int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT; 197 198 err = arc_read(NULL, td->td_spa, bp, pbuf, 199 arc_getbuf_func, &buf, 200 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb); 201 if (err) 202 return (err); 203 204 /* recursively visitbp() blocks below this */ 205 dnp = buf->b_data; 206 for (i = 0; i < epb; i++, dnp++) { 207 err = traverse_dnode(td, dnp, buf, zb->zb_objset, 208 zb->zb_blkid * epb + i); 209 if (err) { 210 if (!hard) 211 break; 212 lasterr = err; 213 } 214 } 215 } else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) { 216 uint32_t flags = ARC_WAIT; 217 objset_phys_t *osp; 218 dnode_phys_t *dnp; 219 220 err = arc_read_nolock(NULL, td->td_spa, bp, 221 arc_getbuf_func, &buf, 222 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb); 223 if (err) 224 return (err); 225 226 osp = buf->b_data; 227 traverse_zil(td, &osp->os_zil_header); 228 229 dnp = &osp->os_meta_dnode; 230 err = traverse_dnode(td, dnp, buf, zb->zb_objset, 231 DMU_META_DNODE_OBJECT); 232 if (err && hard) { 233 lasterr = err; 234 err = 0; 235 } 236 if (err == 0 && arc_buf_size(buf) >= sizeof (objset_phys_t)) { 237 dnp = &osp->os_userused_dnode; 238 err = traverse_dnode(td, dnp, buf, zb->zb_objset, 239 DMU_USERUSED_OBJECT); 240 } 241 if (err && hard) { 242 lasterr = err; 243 err = 0; 244 } 245 if (err == 0 && arc_buf_size(buf) >= sizeof (objset_phys_t)) { 246 dnp = &osp->os_groupused_dnode; 247 err = traverse_dnode(td, dnp, buf, zb->zb_objset, 248 DMU_GROUPUSED_OBJECT); 249 } 250 } 251 252 if (buf) 253 (void) arc_buf_remove_ref(buf, &buf); 254 255 if (err == 0 && lasterr == 0 && (td->td_flags & TRAVERSE_POST)) 256 err = td->td_func(td->td_spa, NULL, bp, zb, dnp, td->td_arg); 257 258 return (err != 0 ? err : lasterr); 259 } 260 261 static int 262 traverse_dnode(struct traverse_data *td, const dnode_phys_t *dnp, 263 arc_buf_t *buf, uint64_t objset, uint64_t object) 264 { 265 int j, err = 0, lasterr = 0; 266 zbookmark_t czb; 267 boolean_t hard = (td->td_flags & TRAVERSE_HARD); 268 269 for (j = 0; j < dnp->dn_nblkptr; j++) { 270 SET_BOOKMARK(&czb, objset, object, dnp->dn_nlevels - 1, j); 271 err = traverse_visitbp(td, dnp, buf, 272 (blkptr_t *)&dnp->dn_blkptr[j], &czb); 273 if (err) { 274 if (!hard) 275 break; 276 lasterr = err; 277 } 278 if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) { 279 SET_BOOKMARK(&czb, objset, 280 object, 0, DMU_SPILL_BLKID); 281 err = traverse_visitbp(td, dnp, buf, 282 (blkptr_t *)&dnp->dn_spill, &czb); 283 if (err) { 284 if (!hard) 285 break; 286 lasterr = err; 287 } 288 } 289 } 290 return (err != 0 ? err : lasterr); 291 } 292 293 /* ARGSUSED */ 294 static int 295 traverse_prefetcher(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 296 const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg) 297 { 298 struct prefetch_data *pfd = arg; 299 uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH; 300 301 ASSERT(pfd->pd_blks_fetched >= 0); 302 if (pfd->pd_cancel) 303 return (EINTR); 304 305 if (bp == NULL || !((pfd->pd_flags & TRAVERSE_PREFETCH_DATA) || 306 BP_GET_TYPE(bp) == DMU_OT_DNODE || BP_GET_LEVEL(bp) > 0) || 307 BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG) 308 return (0); 309 310 mutex_enter(&pfd->pd_mtx); 311 while (!pfd->pd_cancel && pfd->pd_blks_fetched >= pfd->pd_blks_max) 312 cv_wait(&pfd->pd_cv, &pfd->pd_mtx); 313 pfd->pd_blks_fetched++; 314 cv_broadcast(&pfd->pd_cv); 315 mutex_exit(&pfd->pd_mtx); 316 317 (void) arc_read_nolock(NULL, spa, bp, NULL, NULL, 318 ZIO_PRIORITY_ASYNC_READ, 319 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, 320 &aflags, zb); 321 322 return (0); 323 } 324 325 static void 326 traverse_prefetch_thread(void *arg) 327 { 328 struct traverse_data *td_main = arg; 329 struct traverse_data td = *td_main; 330 zbookmark_t czb; 331 332 td.td_func = traverse_prefetcher; 333 td.td_arg = td_main->td_pfd; 334 td.td_pfd = NULL; 335 336 SET_BOOKMARK(&czb, td.td_objset, 337 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 338 (void) traverse_visitbp(&td, NULL, NULL, td.td_rootbp, &czb); 339 340 mutex_enter(&td_main->td_pfd->pd_mtx); 341 td_main->td_pfd->pd_exited = B_TRUE; 342 cv_broadcast(&td_main->td_pfd->pd_cv); 343 mutex_exit(&td_main->td_pfd->pd_mtx); 344 } 345 346 /* 347 * NB: dataset must not be changing on-disk (eg, is a snapshot or we are 348 * in syncing context). 349 */ 350 static int 351 traverse_impl(spa_t *spa, uint64_t objset, blkptr_t *rootbp, 352 uint64_t txg_start, int flags, blkptr_cb_t func, void *arg) 353 { 354 struct traverse_data td; 355 struct prefetch_data pd = { 0 }; 356 zbookmark_t czb; 357 int err; 358 359 td.td_spa = spa; 360 td.td_objset = objset; 361 td.td_rootbp = rootbp; 362 td.td_min_txg = txg_start; 363 td.td_func = func; 364 td.td_arg = arg; 365 td.td_pfd = &pd; 366 td.td_flags = flags; 367 368 pd.pd_blks_max = 100; 369 pd.pd_flags = flags; 370 mutex_init(&pd.pd_mtx, NULL, MUTEX_DEFAULT, NULL); 371 cv_init(&pd.pd_cv, NULL, CV_DEFAULT, NULL); 372 373 if (!(flags & TRAVERSE_PREFETCH) || 374 0 == taskq_dispatch(system_taskq, traverse_prefetch_thread, 375 &td, TQ_NOQUEUE)) 376 pd.pd_exited = B_TRUE; 377 378 SET_BOOKMARK(&czb, objset, 379 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 380 err = traverse_visitbp(&td, NULL, NULL, rootbp, &czb); 381 382 mutex_enter(&pd.pd_mtx); 383 pd.pd_cancel = B_TRUE; 384 cv_broadcast(&pd.pd_cv); 385 while (!pd.pd_exited) 386 cv_wait(&pd.pd_cv, &pd.pd_mtx); 387 mutex_exit(&pd.pd_mtx); 388 389 mutex_destroy(&pd.pd_mtx); 390 cv_destroy(&pd.pd_cv); 391 392 return (err); 393 } 394 395 /* 396 * NB: dataset must not be changing on-disk (eg, is a snapshot or we are 397 * in syncing context). 398 */ 399 int 400 traverse_dataset(dsl_dataset_t *ds, uint64_t txg_start, int flags, 401 blkptr_cb_t func, void *arg) 402 { 403 return (traverse_impl(ds->ds_dir->dd_pool->dp_spa, ds->ds_object, 404 &ds->ds_phys->ds_bp, txg_start, flags, func, arg)); 405 } 406 407 /* 408 * NB: pool must not be changing on-disk (eg, from zdb or sync context). 409 */ 410 int 411 traverse_pool(spa_t *spa, uint64_t txg_start, int flags, 412 blkptr_cb_t func, void *arg) 413 { 414 int err, lasterr = 0; 415 uint64_t obj; 416 dsl_pool_t *dp = spa_get_dsl(spa); 417 objset_t *mos = dp->dp_meta_objset; 418 boolean_t hard = (flags & TRAVERSE_HARD); 419 420 /* visit the MOS */ 421 err = traverse_impl(spa, 0, spa_get_rootblkptr(spa), 422 txg_start, flags, func, arg); 423 if (err) 424 return (err); 425 426 /* visit each dataset */ 427 for (obj = 1; err == 0 || (err != ESRCH && hard); 428 err = dmu_object_next(mos, &obj, FALSE, txg_start)) { 429 dmu_object_info_t doi; 430 431 err = dmu_object_info(mos, obj, &doi); 432 if (err) { 433 if (!hard) 434 return (err); 435 lasterr = err; 436 continue; 437 } 438 439 if (doi.doi_type == DMU_OT_DSL_DATASET) { 440 dsl_dataset_t *ds; 441 uint64_t txg = txg_start; 442 443 rw_enter(&dp->dp_config_rwlock, RW_READER); 444 err = dsl_dataset_hold_obj(dp, obj, FTAG, &ds); 445 rw_exit(&dp->dp_config_rwlock); 446 if (err) { 447 if (!hard) 448 return (err); 449 lasterr = err; 450 continue; 451 } 452 if (ds->ds_phys->ds_prev_snap_txg > txg) 453 txg = ds->ds_phys->ds_prev_snap_txg; 454 err = traverse_dataset(ds, txg, flags, func, arg); 455 dsl_dataset_rele(ds, FTAG); 456 if (err) { 457 if (!hard) 458 return (err); 459 lasterr = err; 460 } 461 } 462 } 463 if (err == ESRCH) 464 err = 0; 465 return (err != 0 ? err : lasterr); 466 } 467