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.
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.
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]
22 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012 by Delphix. All rights reserved.
26 #include <sys/dsl_scan.h>
27 #include <sys/dsl_pool.h>
28 #include <sys/dsl_dataset.h>
29 #include <sys/dsl_prop.h>
30 #include <sys/dsl_dir.h>
31 #include <sys/dsl_synctask.h>
32 #include <sys/dnode.h>
33 #include <sys/dmu_tx.h>
34 #include <sys/dmu_objset.h>
38 #include <sys/zfs_context.h>
39 #include <sys/fs/zfs.h>
40 #include <sys/zfs_znode.h>
41 #include <sys/spa_impl.h>
42 #include <sys/vdev_impl.h>
43 #include <sys/zil_impl.h>
44 #include <sys/zio_checksum.h>
47 #include <sys/sa_impl.h>
48 #include <sys/zfeature.h>
50 #include <sys/zfs_vfsops.h>
53 typedef int (scan_cb_t)(dsl_pool_t *, const blkptr_t *, const zbookmark_t *);
55 static scan_cb_t dsl_scan_scrub_cb;
56 static void dsl_scan_cancel_sync(void *, dmu_tx_t *);
57 static void dsl_scan_sync_state(dsl_scan_t *, dmu_tx_t *tx);
59 int zfs_top_maxinflight = 32; /* maximum I/Os per top-level */
60 int zfs_resilver_delay = 2; /* number of ticks to delay resilver */
61 int zfs_scrub_delay = 4; /* number of ticks to delay scrub */
62 int zfs_scan_idle = 50; /* idle window in clock ticks */
64 int zfs_scan_min_time_ms = 1000; /* min millisecs to scrub per txg */
65 int zfs_free_min_time_ms = 1000; /* min millisecs to free per txg */
66 int zfs_resilver_min_time_ms = 3000; /* min millisecs to resilver per txg */
67 int zfs_no_scrub_io = B_FALSE; /* set to disable scrub i/o */
68 int zfs_no_scrub_prefetch = B_FALSE; /* set to disable srub prefetching */
69 enum ddt_class zfs_scrub_ddt_class_max = DDT_CLASS_DUPLICATE;
70 int dsl_scan_delay_completion = B_FALSE; /* set to delay scan completion */
72 #define DSL_SCAN_IS_SCRUB_RESILVER(scn) \
73 ((scn)->scn_phys.scn_func == POOL_SCAN_SCRUB || \
74 (scn)->scn_phys.scn_func == POOL_SCAN_RESILVER)
76 /* the order has to match pool_scan_type */
77 static scan_cb_t *scan_funcs[POOL_SCAN_FUNCS] = {
79 dsl_scan_scrub_cb, /* POOL_SCAN_SCRUB */
80 dsl_scan_scrub_cb, /* POOL_SCAN_RESILVER */
84 dsl_scan_init(dsl_pool_t *dp, uint64_t txg)
88 spa_t *spa = dp->dp_spa;
91 scn = dp->dp_scan = kmem_zalloc(sizeof (dsl_scan_t), KM_SLEEP);
94 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
95 "scrub_func", sizeof (uint64_t), 1, &f);
98 * There was an old-style scrub in progress. Restart a
99 * new-style scrub from the beginning.
101 scn->scn_restart_txg = txg;
102 zfs_dbgmsg("old-style scrub was in progress; "
103 "restarting new-style scrub in txg %llu",
104 scn->scn_restart_txg);
107 * Load the queue obj from the old location so that it
108 * can be freed by dsl_scan_done().
110 (void) zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
111 "scrub_queue", sizeof (uint64_t), 1,
112 &scn->scn_phys.scn_queue_obj);
114 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
115 DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS,
122 if (scn->scn_phys.scn_state == DSS_SCANNING &&
123 spa_prev_software_version(dp->dp_spa) < SPA_VERSION_SCAN) {
125 * A new-type scrub was in progress on an old
126 * pool, and the pool was accessed by old
127 * software. Restart from the beginning, since
128 * the old software may have changed the pool in
131 scn->scn_restart_txg = txg;
132 zfs_dbgmsg("new-style scrub was modified "
133 "by old software; restarting in txg %llu",
134 scn->scn_restart_txg);
138 spa_scan_stat_init(spa);
143 dsl_scan_fini(dsl_pool_t *dp)
146 kmem_free(dp->dp_scan, sizeof (dsl_scan_t));
153 dsl_scan_setup_check(void *arg, dmu_tx_t *tx)
155 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
157 if (scn->scn_phys.scn_state == DSS_SCANNING)
164 dsl_scan_setup_sync(void *arg, dmu_tx_t *tx)
166 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
167 pool_scan_func_t *funcp = arg;
168 dmu_object_type_t ot = 0;
169 dsl_pool_t *dp = scn->scn_dp;
170 spa_t *spa = dp->dp_spa;
172 ASSERT(scn->scn_phys.scn_state != DSS_SCANNING);
173 ASSERT(*funcp > POOL_SCAN_NONE && *funcp < POOL_SCAN_FUNCS);
174 bzero(&scn->scn_phys, sizeof (scn->scn_phys));
175 scn->scn_phys.scn_func = *funcp;
176 scn->scn_phys.scn_state = DSS_SCANNING;
177 scn->scn_phys.scn_min_txg = 0;
178 scn->scn_phys.scn_max_txg = tx->tx_txg;
179 scn->scn_phys.scn_ddt_class_max = DDT_CLASSES - 1; /* the entire DDT */
180 scn->scn_phys.scn_start_time = gethrestime_sec();
181 scn->scn_phys.scn_errors = 0;
182 scn->scn_phys.scn_to_examine = spa->spa_root_vdev->vdev_stat.vs_alloc;
183 scn->scn_restart_txg = 0;
184 spa_scan_stat_init(spa);
186 if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
187 scn->scn_phys.scn_ddt_class_max = zfs_scrub_ddt_class_max;
189 /* rewrite all disk labels */
190 vdev_config_dirty(spa->spa_root_vdev);
192 if (vdev_resilver_needed(spa->spa_root_vdev,
193 &scn->scn_phys.scn_min_txg, &scn->scn_phys.scn_max_txg)) {
194 spa_event_notify(spa, NULL, FM_EREPORT_ZFS_RESILVER_START);
196 spa_event_notify(spa, NULL, FM_EREPORT_ZFS_SCRUB_START);
199 spa->spa_scrub_started = B_TRUE;
201 * If this is an incremental scrub, limit the DDT scrub phase
202 * to just the auto-ditto class (for correctness); the rest
203 * of the scrub should go faster using top-down pruning.
205 if (scn->scn_phys.scn_min_txg > TXG_INITIAL)
206 scn->scn_phys.scn_ddt_class_max = DDT_CLASS_DITTO;
210 /* back to the generic stuff */
212 if (dp->dp_blkstats == NULL) {
213 dp->dp_blkstats = kmem_alloc(sizeof (zfs_all_blkstats_t),
214 KM_PUSHPAGE | KM_NODEBUG);
216 bzero(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
218 if (spa_version(spa) < SPA_VERSION_DSL_SCRUB)
219 ot = DMU_OT_ZAP_OTHER;
221 scn->scn_phys.scn_queue_obj = zap_create(dp->dp_meta_objset,
222 ot ? ot : DMU_OT_SCAN_QUEUE, DMU_OT_NONE, 0, tx);
224 dsl_scan_sync_state(scn, tx);
226 spa_history_log_internal(spa, "scan setup", tx,
227 "func=%u mintxg=%llu maxtxg=%llu",
228 *funcp, scn->scn_phys.scn_min_txg, scn->scn_phys.scn_max_txg);
233 dsl_scan_done(dsl_scan_t *scn, boolean_t complete, dmu_tx_t *tx)
235 static const char *old_names[] = {
237 "scrub_ddt_bookmark",
238 "scrub_ddt_class_max",
247 dsl_pool_t *dp = scn->scn_dp;
248 spa_t *spa = dp->dp_spa;
251 /* Remove any remnants of an old-style scrub. */
252 for (i = 0; old_names[i]; i++) {
253 (void) zap_remove(dp->dp_meta_objset,
254 DMU_POOL_DIRECTORY_OBJECT, old_names[i], tx);
257 if (scn->scn_phys.scn_queue_obj != 0) {
258 VERIFY(0 == dmu_object_free(dp->dp_meta_objset,
259 scn->scn_phys.scn_queue_obj, tx));
260 scn->scn_phys.scn_queue_obj = 0;
264 * If we were "restarted" from a stopped state, don't bother
265 * with anything else.
267 if (scn->scn_phys.scn_state != DSS_SCANNING)
271 scn->scn_phys.scn_state = DSS_FINISHED;
273 scn->scn_phys.scn_state = DSS_CANCELED;
275 spa_history_log_internal(spa, "scan done", tx,
276 "complete=%u", complete);
278 if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
279 mutex_enter(&spa->spa_scrub_lock);
280 while (spa->spa_scrub_inflight > 0) {
281 cv_wait(&spa->spa_scrub_io_cv,
282 &spa->spa_scrub_lock);
284 mutex_exit(&spa->spa_scrub_lock);
285 spa->spa_scrub_started = B_FALSE;
286 spa->spa_scrub_active = B_FALSE;
289 * If the scrub/resilver completed, update all DTLs to
290 * reflect this. Whether it succeeded or not, vacate
291 * all temporary scrub DTLs.
293 vdev_dtl_reassess(spa->spa_root_vdev, tx->tx_txg,
294 complete ? scn->scn_phys.scn_max_txg : 0, B_TRUE);
296 spa_event_notify(spa, NULL, scn->scn_phys.scn_min_txg ?
297 FM_EREPORT_ZFS_RESILVER_FINISH :
298 FM_EREPORT_ZFS_SCRUB_FINISH);
300 spa_errlog_rotate(spa);
303 * We may have finished replacing a device.
304 * Let the async thread assess this and handle the detach.
306 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
309 scn->scn_phys.scn_end_time = gethrestime_sec();
314 dsl_scan_cancel_check(void *arg, dmu_tx_t *tx)
316 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
318 if (scn->scn_phys.scn_state != DSS_SCANNING)
325 dsl_scan_cancel_sync(void *arg, dmu_tx_t *tx)
327 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
329 dsl_scan_done(scn, B_FALSE, tx);
330 dsl_scan_sync_state(scn, tx);
334 dsl_scan_cancel(dsl_pool_t *dp)
336 return (dsl_sync_task(spa_name(dp->dp_spa), dsl_scan_cancel_check,
337 dsl_scan_cancel_sync, NULL, 3));
340 static void dsl_scan_visitbp(blkptr_t *bp,
341 const zbookmark_t *zb, dnode_phys_t *dnp, arc_buf_t *pbuf,
342 dsl_dataset_t *ds, dsl_scan_t *scn, dmu_objset_type_t ostype,
344 inline __attribute__((always_inline)) static void dsl_scan_visitdnode(
345 dsl_scan_t *, dsl_dataset_t *ds, dmu_objset_type_t ostype,
346 dnode_phys_t *dnp, arc_buf_t *buf, uint64_t object, dmu_tx_t *tx);
349 dsl_free(dsl_pool_t *dp, uint64_t txg, const blkptr_t *bp)
351 zio_free(dp->dp_spa, txg, bp);
355 dsl_free_sync(zio_t *pio, dsl_pool_t *dp, uint64_t txg, const blkptr_t *bpp)
357 ASSERT(dsl_pool_sync_context(dp));
358 zio_nowait(zio_free_sync(pio, dp->dp_spa, txg, bpp, pio->io_flags));
362 dsl_scan_ds_maxtxg(dsl_dataset_t *ds)
364 uint64_t smt = ds->ds_dir->dd_pool->dp_scan->scn_phys.scn_max_txg;
365 if (dsl_dataset_is_snapshot(ds))
366 return (MIN(smt, ds->ds_phys->ds_creation_txg));
371 dsl_scan_sync_state(dsl_scan_t *scn, dmu_tx_t *tx)
373 VERIFY0(zap_update(scn->scn_dp->dp_meta_objset,
374 DMU_POOL_DIRECTORY_OBJECT,
375 DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS,
376 &scn->scn_phys, tx));
380 dsl_scan_check_pause(dsl_scan_t *scn, const zbookmark_t *zb)
382 uint64_t elapsed_nanosecs;
385 /* we never skip user/group accounting objects */
386 if (zb && (int64_t)zb->zb_object < 0)
389 if (scn->scn_pausing)
390 return (B_TRUE); /* we're already pausing */
392 if (!ZB_IS_ZERO(&scn->scn_phys.scn_bookmark))
393 return (B_FALSE); /* we're resuming */
395 /* We only know how to resume from level-0 blocks. */
396 if (zb && zb->zb_level != 0)
399 mintime = (scn->scn_phys.scn_func == POOL_SCAN_RESILVER) ?
400 zfs_resilver_min_time_ms : zfs_scan_min_time_ms;
401 elapsed_nanosecs = gethrtime() - scn->scn_sync_start_time;
402 if (elapsed_nanosecs / NANOSEC > zfs_txg_timeout ||
403 (elapsed_nanosecs / MICROSEC > mintime &&
404 txg_sync_waiting(scn->scn_dp)) ||
405 spa_shutting_down(scn->scn_dp->dp_spa)) {
407 dprintf("pausing at bookmark %llx/%llx/%llx/%llx\n",
408 (longlong_t)zb->zb_objset,
409 (longlong_t)zb->zb_object,
410 (longlong_t)zb->zb_level,
411 (longlong_t)zb->zb_blkid);
412 scn->scn_phys.scn_bookmark = *zb;
414 dprintf("pausing at DDT bookmark %llx/%llx/%llx/%llx\n",
415 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_class,
416 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_type,
417 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_checksum,
418 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_cursor);
419 scn->scn_pausing = B_TRUE;
425 typedef struct zil_scan_arg {
427 zil_header_t *zsa_zh;
432 dsl_scan_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
434 zil_scan_arg_t *zsa = arg;
435 dsl_pool_t *dp = zsa->zsa_dp;
436 dsl_scan_t *scn = dp->dp_scan;
437 zil_header_t *zh = zsa->zsa_zh;
440 if (bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
444 * One block ("stubby") can be allocated a long time ago; we
445 * want to visit that one because it has been allocated
446 * (on-disk) even if it hasn't been claimed (even though for
447 * scrub there's nothing to do to it).
449 if (claim_txg == 0 && bp->blk_birth >= spa_first_txg(dp->dp_spa))
452 SET_BOOKMARK(&zb, zh->zh_log.blk_cksum.zc_word[ZIL_ZC_OBJSET],
453 ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
455 VERIFY(0 == scan_funcs[scn->scn_phys.scn_func](dp, bp, &zb));
461 dsl_scan_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg)
463 if (lrc->lrc_txtype == TX_WRITE) {
464 zil_scan_arg_t *zsa = arg;
465 dsl_pool_t *dp = zsa->zsa_dp;
466 dsl_scan_t *scn = dp->dp_scan;
467 zil_header_t *zh = zsa->zsa_zh;
468 lr_write_t *lr = (lr_write_t *)lrc;
469 blkptr_t *bp = &lr->lr_blkptr;
472 if (bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
476 * birth can be < claim_txg if this record's txg is
477 * already txg sync'ed (but this log block contains
478 * other records that are not synced)
480 if (claim_txg == 0 || bp->blk_birth < claim_txg)
483 SET_BOOKMARK(&zb, zh->zh_log.blk_cksum.zc_word[ZIL_ZC_OBJSET],
484 lr->lr_foid, ZB_ZIL_LEVEL,
485 lr->lr_offset / BP_GET_LSIZE(bp));
487 VERIFY(0 == scan_funcs[scn->scn_phys.scn_func](dp, bp, &zb));
493 dsl_scan_zil(dsl_pool_t *dp, zil_header_t *zh)
495 uint64_t claim_txg = zh->zh_claim_txg;
496 zil_scan_arg_t zsa = { dp, zh };
500 * We only want to visit blocks that have been claimed but not yet
501 * replayed (or, in read-only mode, blocks that *would* be claimed).
503 if (claim_txg == 0 && spa_writeable(dp->dp_spa))
506 zilog = zil_alloc(dp->dp_meta_objset, zh);
508 (void) zil_parse(zilog, dsl_scan_zil_block, dsl_scan_zil_record, &zsa,
516 dsl_scan_prefetch(dsl_scan_t *scn, arc_buf_t *buf, blkptr_t *bp,
517 uint64_t objset, uint64_t object, uint64_t blkid)
520 uint32_t flags = ARC_NOWAIT | ARC_PREFETCH;
522 if (zfs_no_scrub_prefetch)
525 if (BP_IS_HOLE(bp) || bp->blk_birth <= scn->scn_phys.scn_min_txg ||
526 (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_DNODE))
529 SET_BOOKMARK(&czb, objset, object, BP_GET_LEVEL(bp), blkid);
531 (void) arc_read(scn->scn_zio_root, scn->scn_dp->dp_spa, bp,
532 NULL, NULL, ZIO_PRIORITY_ASYNC_READ,
533 ZIO_FLAG_CANFAIL | ZIO_FLAG_SCAN_THREAD, &flags, &czb);
537 dsl_scan_check_resume(dsl_scan_t *scn, const dnode_phys_t *dnp,
538 const zbookmark_t *zb)
541 * We never skip over user/group accounting objects (obj<0)
543 if (!ZB_IS_ZERO(&scn->scn_phys.scn_bookmark) &&
544 (int64_t)zb->zb_object >= 0) {
546 * If we already visited this bp & everything below (in
547 * a prior txg sync), don't bother doing it again.
549 if (zbookmark_is_before(dnp, zb, &scn->scn_phys.scn_bookmark))
553 * If we found the block we're trying to resume from, or
554 * we went past it to a different object, zero it out to
555 * indicate that it's OK to start checking for pausing
558 if (bcmp(zb, &scn->scn_phys.scn_bookmark, sizeof (*zb)) == 0 ||
559 zb->zb_object > scn->scn_phys.scn_bookmark.zb_object) {
560 dprintf("resuming at %llx/%llx/%llx/%llx\n",
561 (longlong_t)zb->zb_objset,
562 (longlong_t)zb->zb_object,
563 (longlong_t)zb->zb_level,
564 (longlong_t)zb->zb_blkid);
565 bzero(&scn->scn_phys.scn_bookmark, sizeof (*zb));
572 * Return nonzero on i/o error.
573 * Return new buf to write out in *bufp.
575 inline __attribute__((always_inline)) static int
576 dsl_scan_recurse(dsl_scan_t *scn, dsl_dataset_t *ds, dmu_objset_type_t ostype,
577 dnode_phys_t *dnp, const blkptr_t *bp,
578 const zbookmark_t *zb, dmu_tx_t *tx, arc_buf_t **bufp)
580 dsl_pool_t *dp = scn->scn_dp;
581 int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCAN_THREAD;
584 if (BP_GET_LEVEL(bp) > 0) {
585 uint32_t flags = ARC_WAIT;
588 int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
590 err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, bufp,
591 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
593 scn->scn_phys.scn_errors++;
596 for (i = 0, cbp = (*bufp)->b_data; i < epb; i++, cbp++) {
597 dsl_scan_prefetch(scn, *bufp, cbp, zb->zb_objset,
598 zb->zb_object, zb->zb_blkid * epb + i);
600 for (i = 0, cbp = (*bufp)->b_data; i < epb; i++, cbp++) {
603 SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
605 zb->zb_blkid * epb + i);
606 dsl_scan_visitbp(cbp, &czb, dnp,
607 *bufp, ds, scn, ostype, tx);
609 } else if (BP_GET_TYPE(bp) == DMU_OT_USERGROUP_USED) {
610 uint32_t flags = ARC_WAIT;
612 err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, bufp,
613 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
615 scn->scn_phys.scn_errors++;
618 } else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) {
619 uint32_t flags = ARC_WAIT;
622 int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
624 err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, bufp,
625 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
627 scn->scn_phys.scn_errors++;
630 for (i = 0, cdnp = (*bufp)->b_data; i < epb; i++, cdnp++) {
631 for (j = 0; j < cdnp->dn_nblkptr; j++) {
632 blkptr_t *cbp = &cdnp->dn_blkptr[j];
633 dsl_scan_prefetch(scn, *bufp, cbp,
634 zb->zb_objset, zb->zb_blkid * epb + i, j);
637 for (i = 0, cdnp = (*bufp)->b_data; i < epb; i++, cdnp++) {
638 dsl_scan_visitdnode(scn, ds, ostype,
639 cdnp, *bufp, zb->zb_blkid * epb + i, tx);
642 } else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
643 uint32_t flags = ARC_WAIT;
646 err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, bufp,
647 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
649 scn->scn_phys.scn_errors++;
653 osp = (*bufp)->b_data;
655 dsl_scan_visitdnode(scn, ds, osp->os_type,
656 &osp->os_meta_dnode, *bufp, DMU_META_DNODE_OBJECT, tx);
658 if (OBJSET_BUF_HAS_USERUSED(*bufp)) {
660 * We also always visit user/group accounting
661 * objects, and never skip them, even if we are
662 * pausing. This is necessary so that the space
663 * deltas from this txg get integrated.
665 dsl_scan_visitdnode(scn, ds, osp->os_type,
666 &osp->os_groupused_dnode, *bufp,
667 DMU_GROUPUSED_OBJECT, tx);
668 dsl_scan_visitdnode(scn, ds, osp->os_type,
669 &osp->os_userused_dnode, *bufp,
670 DMU_USERUSED_OBJECT, tx);
677 inline __attribute__((always_inline)) static void
678 dsl_scan_visitdnode(dsl_scan_t *scn, dsl_dataset_t *ds,
679 dmu_objset_type_t ostype, dnode_phys_t *dnp, arc_buf_t *buf,
680 uint64_t object, dmu_tx_t *tx)
684 for (j = 0; j < dnp->dn_nblkptr; j++) {
687 SET_BOOKMARK(&czb, ds ? ds->ds_object : 0, object,
688 dnp->dn_nlevels - 1, j);
689 dsl_scan_visitbp(&dnp->dn_blkptr[j],
690 &czb, dnp, buf, ds, scn, ostype, tx);
693 if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
695 SET_BOOKMARK(&czb, ds ? ds->ds_object : 0, object,
697 dsl_scan_visitbp(&dnp->dn_spill,
698 &czb, dnp, buf, ds, scn, ostype, tx);
703 * The arguments are in this order because mdb can only print the
704 * first 5; we want them to be useful.
707 dsl_scan_visitbp(blkptr_t *bp, const zbookmark_t *zb,
708 dnode_phys_t *dnp, arc_buf_t *pbuf,
709 dsl_dataset_t *ds, dsl_scan_t *scn, dmu_objset_type_t ostype,
712 dsl_pool_t *dp = scn->scn_dp;
713 arc_buf_t *buf = NULL;
716 bp_toread = kmem_alloc(sizeof (blkptr_t), KM_PUSHPAGE);
719 /* ASSERT(pbuf == NULL || arc_released(pbuf)); */
721 if (dsl_scan_check_pause(scn, zb))
724 if (dsl_scan_check_resume(scn, dnp, zb))
727 if (bp->blk_birth == 0)
730 scn->scn_visited_this_txg++;
733 * This debugging is commented out to conserve stack space. This
734 * function is called recursively and the debugging addes several
735 * bytes to the stack for each call. It can be commented back in
736 * if required to debug an issue in dsl_scan_visitbp().
739 * "visiting ds=%p/%llu zb=%llx/%llx/%llx/%llx buf=%p bp=%p",
740 * ds, ds ? ds->ds_object : 0,
741 * zb->zb_objset, zb->zb_object, zb->zb_level, zb->zb_blkid,
745 if (bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
748 if (dsl_scan_recurse(scn, ds, ostype, dnp, bp_toread, zb, tx,
753 * If dsl_scan_ddt() has aready visited this block, it will have
754 * already done any translations or scrubbing, so don't call the
757 if (ddt_class_contains(dp->dp_spa,
758 scn->scn_phys.scn_ddt_class_max, bp)) {
764 * If this block is from the future (after cur_max_txg), then we
765 * are doing this on behalf of a deleted snapshot, and we will
766 * revisit the future block on the next pass of this dataset.
767 * Don't scan it now unless we need to because something
768 * under it was modified.
770 if (bp->blk_birth <= scn->scn_phys.scn_cur_max_txg) {
771 scan_funcs[scn->scn_phys.scn_func](dp, bp, zb);
774 (void) arc_buf_remove_ref(buf, &buf);
776 kmem_free(bp_toread, sizeof(blkptr_t));
780 dsl_scan_visit_rootbp(dsl_scan_t *scn, dsl_dataset_t *ds, blkptr_t *bp,
785 SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
786 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
787 dsl_scan_visitbp(bp, &zb, NULL, NULL,
788 ds, scn, DMU_OST_NONE, tx);
790 dprintf_ds(ds, "finished scan%s", "");
794 dsl_scan_ds_destroyed(dsl_dataset_t *ds, dmu_tx_t *tx)
796 dsl_pool_t *dp = ds->ds_dir->dd_pool;
797 dsl_scan_t *scn = dp->dp_scan;
800 if (scn->scn_phys.scn_state != DSS_SCANNING)
803 if (scn->scn_phys.scn_bookmark.zb_objset == ds->ds_object) {
804 if (dsl_dataset_is_snapshot(ds)) {
805 /* Note, scn_cur_{min,max}_txg stays the same. */
806 scn->scn_phys.scn_bookmark.zb_objset =
807 ds->ds_phys->ds_next_snap_obj;
808 zfs_dbgmsg("destroying ds %llu; currently traversing; "
809 "reset zb_objset to %llu",
810 (u_longlong_t)ds->ds_object,
811 (u_longlong_t)ds->ds_phys->ds_next_snap_obj);
812 scn->scn_phys.scn_flags |= DSF_VISIT_DS_AGAIN;
814 SET_BOOKMARK(&scn->scn_phys.scn_bookmark,
815 ZB_DESTROYED_OBJSET, 0, 0, 0);
816 zfs_dbgmsg("destroying ds %llu; currently traversing; "
817 "reset bookmark to -1,0,0,0",
818 (u_longlong_t)ds->ds_object);
820 } else if (zap_lookup_int_key(dp->dp_meta_objset,
821 scn->scn_phys.scn_queue_obj, ds->ds_object, &mintxg) == 0) {
822 ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
823 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
824 scn->scn_phys.scn_queue_obj, ds->ds_object, tx));
825 if (dsl_dataset_is_snapshot(ds)) {
827 * We keep the same mintxg; it could be >
828 * ds_creation_txg if the previous snapshot was
831 VERIFY(zap_add_int_key(dp->dp_meta_objset,
832 scn->scn_phys.scn_queue_obj,
833 ds->ds_phys->ds_next_snap_obj, mintxg, tx) == 0);
834 zfs_dbgmsg("destroying ds %llu; in queue; "
835 "replacing with %llu",
836 (u_longlong_t)ds->ds_object,
837 (u_longlong_t)ds->ds_phys->ds_next_snap_obj);
839 zfs_dbgmsg("destroying ds %llu; in queue; removing",
840 (u_longlong_t)ds->ds_object);
843 zfs_dbgmsg("destroying ds %llu; ignoring",
844 (u_longlong_t)ds->ds_object);
848 * dsl_scan_sync() should be called after this, and should sync
849 * out our changed state, but just to be safe, do it here.
851 dsl_scan_sync_state(scn, tx);
855 dsl_scan_ds_snapshotted(dsl_dataset_t *ds, dmu_tx_t *tx)
857 dsl_pool_t *dp = ds->ds_dir->dd_pool;
858 dsl_scan_t *scn = dp->dp_scan;
861 if (scn->scn_phys.scn_state != DSS_SCANNING)
864 ASSERT(ds->ds_phys->ds_prev_snap_obj != 0);
866 if (scn->scn_phys.scn_bookmark.zb_objset == ds->ds_object) {
867 scn->scn_phys.scn_bookmark.zb_objset =
868 ds->ds_phys->ds_prev_snap_obj;
869 zfs_dbgmsg("snapshotting ds %llu; currently traversing; "
870 "reset zb_objset to %llu",
871 (u_longlong_t)ds->ds_object,
872 (u_longlong_t)ds->ds_phys->ds_prev_snap_obj);
873 } else if (zap_lookup_int_key(dp->dp_meta_objset,
874 scn->scn_phys.scn_queue_obj, ds->ds_object, &mintxg) == 0) {
875 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
876 scn->scn_phys.scn_queue_obj, ds->ds_object, tx));
877 VERIFY(zap_add_int_key(dp->dp_meta_objset,
878 scn->scn_phys.scn_queue_obj,
879 ds->ds_phys->ds_prev_snap_obj, mintxg, tx) == 0);
880 zfs_dbgmsg("snapshotting ds %llu; in queue; "
881 "replacing with %llu",
882 (u_longlong_t)ds->ds_object,
883 (u_longlong_t)ds->ds_phys->ds_prev_snap_obj);
885 dsl_scan_sync_state(scn, tx);
889 dsl_scan_ds_clone_swapped(dsl_dataset_t *ds1, dsl_dataset_t *ds2, dmu_tx_t *tx)
891 dsl_pool_t *dp = ds1->ds_dir->dd_pool;
892 dsl_scan_t *scn = dp->dp_scan;
895 if (scn->scn_phys.scn_state != DSS_SCANNING)
898 if (scn->scn_phys.scn_bookmark.zb_objset == ds1->ds_object) {
899 scn->scn_phys.scn_bookmark.zb_objset = ds2->ds_object;
900 zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
901 "reset zb_objset to %llu",
902 (u_longlong_t)ds1->ds_object,
903 (u_longlong_t)ds2->ds_object);
904 } else if (scn->scn_phys.scn_bookmark.zb_objset == ds2->ds_object) {
905 scn->scn_phys.scn_bookmark.zb_objset = ds1->ds_object;
906 zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
907 "reset zb_objset to %llu",
908 (u_longlong_t)ds2->ds_object,
909 (u_longlong_t)ds1->ds_object);
912 if (zap_lookup_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj,
913 ds1->ds_object, &mintxg) == 0) {
916 ASSERT3U(mintxg, ==, ds1->ds_phys->ds_prev_snap_txg);
917 ASSERT3U(mintxg, ==, ds2->ds_phys->ds_prev_snap_txg);
918 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
919 scn->scn_phys.scn_queue_obj, ds1->ds_object, tx));
920 err = zap_add_int_key(dp->dp_meta_objset,
921 scn->scn_phys.scn_queue_obj, ds2->ds_object, mintxg, tx);
922 VERIFY(err == 0 || err == EEXIST);
924 /* Both were there to begin with */
925 VERIFY(0 == zap_add_int_key(dp->dp_meta_objset,
926 scn->scn_phys.scn_queue_obj,
927 ds1->ds_object, mintxg, tx));
929 zfs_dbgmsg("clone_swap ds %llu; in queue; "
930 "replacing with %llu",
931 (u_longlong_t)ds1->ds_object,
932 (u_longlong_t)ds2->ds_object);
933 } else if (zap_lookup_int_key(dp->dp_meta_objset,
934 scn->scn_phys.scn_queue_obj, ds2->ds_object, &mintxg) == 0) {
935 ASSERT3U(mintxg, ==, ds1->ds_phys->ds_prev_snap_txg);
936 ASSERT3U(mintxg, ==, ds2->ds_phys->ds_prev_snap_txg);
937 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
938 scn->scn_phys.scn_queue_obj, ds2->ds_object, tx));
939 VERIFY(0 == zap_add_int_key(dp->dp_meta_objset,
940 scn->scn_phys.scn_queue_obj, ds1->ds_object, mintxg, tx));
941 zfs_dbgmsg("clone_swap ds %llu; in queue; "
942 "replacing with %llu",
943 (u_longlong_t)ds2->ds_object,
944 (u_longlong_t)ds1->ds_object);
947 dsl_scan_sync_state(scn, tx);
950 struct enqueue_clones_arg {
957 enqueue_clones_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg)
959 struct enqueue_clones_arg *eca = arg;
962 dsl_scan_t *scn = dp->dp_scan;
964 if (hds->ds_dir->dd_phys->dd_origin_obj != eca->originobj)
967 err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds);
971 while (ds->ds_phys->ds_prev_snap_obj != eca->originobj) {
973 err = dsl_dataset_hold_obj(dp,
974 ds->ds_phys->ds_prev_snap_obj, FTAG, &prev);
976 dsl_dataset_rele(ds, FTAG);
981 VERIFY(zap_add_int_key(dp->dp_meta_objset,
982 scn->scn_phys.scn_queue_obj, ds->ds_object,
983 ds->ds_phys->ds_prev_snap_txg, eca->tx) == 0);
984 dsl_dataset_rele(ds, FTAG);
989 dsl_scan_visitds(dsl_scan_t *scn, uint64_t dsobj, dmu_tx_t *tx)
991 dsl_pool_t *dp = scn->scn_dp;
996 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
998 if (dmu_objset_from_ds(ds, &os))
1002 * Only the ZIL in the head (non-snapshot) is valid. Even though
1003 * snapshots can have ZIL block pointers (which may be the same
1004 * BP as in the head), they must be ignored. So we traverse the
1005 * ZIL here, rather than in scan_recurse(), because the regular
1006 * snapshot block-sharing rules don't apply to it.
1008 if (DSL_SCAN_IS_SCRUB_RESILVER(scn) && !dsl_dataset_is_snapshot(ds))
1009 dsl_scan_zil(dp, &os->os_zil_header);
1012 * Iterate over the bps in this ds.
1014 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1015 dsl_scan_visit_rootbp(scn, ds, &ds->ds_phys->ds_bp, tx);
1017 dsname = kmem_alloc(ZFS_MAXNAMELEN, KM_PUSHPAGE);
1018 dsl_dataset_name(ds, dsname);
1019 zfs_dbgmsg("scanned dataset %llu (%s) with min=%llu max=%llu; "
1021 (longlong_t)dsobj, dsname,
1022 (longlong_t)scn->scn_phys.scn_cur_min_txg,
1023 (longlong_t)scn->scn_phys.scn_cur_max_txg,
1024 (int)scn->scn_pausing);
1025 kmem_free(dsname, ZFS_MAXNAMELEN);
1027 if (scn->scn_pausing)
1031 * We've finished this pass over this dataset.
1035 * If we did not completely visit this dataset, do another pass.
1037 if (scn->scn_phys.scn_flags & DSF_VISIT_DS_AGAIN) {
1038 zfs_dbgmsg("incomplete pass; visiting again");
1039 scn->scn_phys.scn_flags &= ~DSF_VISIT_DS_AGAIN;
1040 VERIFY(zap_add_int_key(dp->dp_meta_objset,
1041 scn->scn_phys.scn_queue_obj, ds->ds_object,
1042 scn->scn_phys.scn_cur_max_txg, tx) == 0);
1047 * Add descendent datasets to work queue.
1049 if (ds->ds_phys->ds_next_snap_obj != 0) {
1050 VERIFY(zap_add_int_key(dp->dp_meta_objset,
1051 scn->scn_phys.scn_queue_obj, ds->ds_phys->ds_next_snap_obj,
1052 ds->ds_phys->ds_creation_txg, tx) == 0);
1054 if (ds->ds_phys->ds_num_children > 1) {
1055 boolean_t usenext = B_FALSE;
1056 if (ds->ds_phys->ds_next_clones_obj != 0) {
1059 * A bug in a previous version of the code could
1060 * cause upgrade_clones_cb() to not set
1061 * ds_next_snap_obj when it should, leading to a
1062 * missing entry. Therefore we can only use the
1063 * next_clones_obj when its count is correct.
1065 int err = zap_count(dp->dp_meta_objset,
1066 ds->ds_phys->ds_next_clones_obj, &count);
1068 count == ds->ds_phys->ds_num_children - 1)
1073 VERIFY0(zap_join_key(dp->dp_meta_objset,
1074 ds->ds_phys->ds_next_clones_obj,
1075 scn->scn_phys.scn_queue_obj,
1076 ds->ds_phys->ds_creation_txg, tx));
1078 struct enqueue_clones_arg eca;
1080 eca.originobj = ds->ds_object;
1082 VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
1083 enqueue_clones_cb, &eca, DS_FIND_CHILDREN));
1088 dsl_dataset_rele(ds, FTAG);
1093 enqueue_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg)
1098 dsl_scan_t *scn = dp->dp_scan;
1100 err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds);
1104 while (ds->ds_phys->ds_prev_snap_obj != 0) {
1105 dsl_dataset_t *prev;
1106 err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
1109 dsl_dataset_rele(ds, FTAG);
1114 * If this is a clone, we don't need to worry about it for now.
1116 if (prev->ds_phys->ds_next_snap_obj != ds->ds_object) {
1117 dsl_dataset_rele(ds, FTAG);
1118 dsl_dataset_rele(prev, FTAG);
1121 dsl_dataset_rele(ds, FTAG);
1125 VERIFY(zap_add_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj,
1126 ds->ds_object, ds->ds_phys->ds_prev_snap_txg, tx) == 0);
1127 dsl_dataset_rele(ds, FTAG);
1132 * Scrub/dedup interaction.
1134 * If there are N references to a deduped block, we don't want to scrub it
1135 * N times -- ideally, we should scrub it exactly once.
1137 * We leverage the fact that the dde's replication class (enum ddt_class)
1138 * is ordered from highest replication class (DDT_CLASS_DITTO) to lowest
1139 * (DDT_CLASS_UNIQUE) so that we may walk the DDT in that order.
1141 * To prevent excess scrubbing, the scrub begins by walking the DDT
1142 * to find all blocks with refcnt > 1, and scrubs each of these once.
1143 * Since there are two replication classes which contain blocks with
1144 * refcnt > 1, we scrub the highest replication class (DDT_CLASS_DITTO) first.
1145 * Finally the top-down scrub begins, only visiting blocks with refcnt == 1.
1147 * There would be nothing more to say if a block's refcnt couldn't change
1148 * during a scrub, but of course it can so we must account for changes
1149 * in a block's replication class.
1151 * Here's an example of what can occur:
1153 * If a block has refcnt > 1 during the DDT scrub phase, but has refcnt == 1
1154 * when visited during the top-down scrub phase, it will be scrubbed twice.
1155 * This negates our scrub optimization, but is otherwise harmless.
1157 * If a block has refcnt == 1 during the DDT scrub phase, but has refcnt > 1
1158 * on each visit during the top-down scrub phase, it will never be scrubbed.
1159 * To catch this, ddt_sync_entry() notifies the scrub code whenever a block's
1160 * reference class transitions to a higher level (i.e DDT_CLASS_UNIQUE to
1161 * DDT_CLASS_DUPLICATE); if it transitions from refcnt == 1 to refcnt > 1
1162 * while a scrub is in progress, it scrubs the block right then.
1165 dsl_scan_ddt(dsl_scan_t *scn, dmu_tx_t *tx)
1167 ddt_bookmark_t *ddb = &scn->scn_phys.scn_ddt_bookmark;
1172 bzero(&dde, sizeof (ddt_entry_t));
1174 while ((error = ddt_walk(scn->scn_dp->dp_spa, ddb, &dde)) == 0) {
1177 if (ddb->ddb_class > scn->scn_phys.scn_ddt_class_max)
1179 dprintf("visiting ddb=%llu/%llu/%llu/%llx\n",
1180 (longlong_t)ddb->ddb_class,
1181 (longlong_t)ddb->ddb_type,
1182 (longlong_t)ddb->ddb_checksum,
1183 (longlong_t)ddb->ddb_cursor);
1185 /* There should be no pending changes to the dedup table */
1186 ddt = scn->scn_dp->dp_spa->spa_ddt[ddb->ddb_checksum];
1187 ASSERT(avl_first(&ddt->ddt_tree) == NULL);
1189 dsl_scan_ddt_entry(scn, ddb->ddb_checksum, &dde, tx);
1192 if (dsl_scan_check_pause(scn, NULL))
1196 zfs_dbgmsg("scanned %llu ddt entries with class_max = %u; pausing=%u",
1197 (longlong_t)n, (int)scn->scn_phys.scn_ddt_class_max,
1198 (int)scn->scn_pausing);
1200 ASSERT(error == 0 || error == ENOENT);
1201 ASSERT(error != ENOENT ||
1202 ddb->ddb_class > scn->scn_phys.scn_ddt_class_max);
1207 dsl_scan_ddt_entry(dsl_scan_t *scn, enum zio_checksum checksum,
1208 ddt_entry_t *dde, dmu_tx_t *tx)
1210 const ddt_key_t *ddk = &dde->dde_key;
1211 ddt_phys_t *ddp = dde->dde_phys;
1213 zbookmark_t zb = { 0 };
1216 if (scn->scn_phys.scn_state != DSS_SCANNING)
1219 for (p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
1220 if (ddp->ddp_phys_birth == 0 ||
1221 ddp->ddp_phys_birth > scn->scn_phys.scn_cur_max_txg)
1223 ddt_bp_create(checksum, ddk, ddp, &bp);
1225 scn->scn_visited_this_txg++;
1226 scan_funcs[scn->scn_phys.scn_func](scn->scn_dp, &bp, &zb);
1231 dsl_scan_visit(dsl_scan_t *scn, dmu_tx_t *tx)
1233 dsl_pool_t *dp = scn->scn_dp;
1235 zap_attribute_t *za;
1237 if (scn->scn_phys.scn_ddt_bookmark.ddb_class <=
1238 scn->scn_phys.scn_ddt_class_max) {
1239 scn->scn_phys.scn_cur_min_txg = scn->scn_phys.scn_min_txg;
1240 scn->scn_phys.scn_cur_max_txg = scn->scn_phys.scn_max_txg;
1241 dsl_scan_ddt(scn, tx);
1242 if (scn->scn_pausing)
1246 if (scn->scn_phys.scn_bookmark.zb_objset == DMU_META_OBJSET) {
1247 /* First do the MOS & ORIGIN */
1249 scn->scn_phys.scn_cur_min_txg = scn->scn_phys.scn_min_txg;
1250 scn->scn_phys.scn_cur_max_txg = scn->scn_phys.scn_max_txg;
1251 dsl_scan_visit_rootbp(scn, NULL,
1252 &dp->dp_meta_rootbp, tx);
1253 spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
1254 if (scn->scn_pausing)
1257 if (spa_version(dp->dp_spa) < SPA_VERSION_DSL_SCRUB) {
1258 VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
1259 enqueue_cb, tx, DS_FIND_CHILDREN));
1261 dsl_scan_visitds(scn,
1262 dp->dp_origin_snap->ds_object, tx);
1264 ASSERT(!scn->scn_pausing);
1265 } else if (scn->scn_phys.scn_bookmark.zb_objset !=
1266 ZB_DESTROYED_OBJSET) {
1268 * If we were paused, continue from here. Note if the
1269 * ds we were paused on was deleted, the zb_objset may
1270 * be -1, so we will skip this and find a new objset
1273 dsl_scan_visitds(scn, scn->scn_phys.scn_bookmark.zb_objset, tx);
1274 if (scn->scn_pausing)
1279 * In case we were paused right at the end of the ds, zero the
1280 * bookmark so we don't think that we're still trying to resume.
1282 bzero(&scn->scn_phys.scn_bookmark, sizeof (zbookmark_t));
1283 zc = kmem_alloc(sizeof(zap_cursor_t), KM_PUSHPAGE);
1284 za = kmem_alloc(sizeof(zap_attribute_t), KM_PUSHPAGE);
1286 /* keep pulling things out of the zap-object-as-queue */
1287 while (zap_cursor_init(zc, dp->dp_meta_objset,
1288 scn->scn_phys.scn_queue_obj),
1289 zap_cursor_retrieve(zc, za) == 0) {
1293 dsobj = strtonum(za->za_name, NULL);
1294 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
1295 scn->scn_phys.scn_queue_obj, dsobj, tx));
1297 /* Set up min/max txg */
1298 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
1299 if (za->za_first_integer != 0) {
1300 scn->scn_phys.scn_cur_min_txg =
1301 MAX(scn->scn_phys.scn_min_txg,
1302 za->za_first_integer);
1304 scn->scn_phys.scn_cur_min_txg =
1305 MAX(scn->scn_phys.scn_min_txg,
1306 ds->ds_phys->ds_prev_snap_txg);
1308 scn->scn_phys.scn_cur_max_txg = dsl_scan_ds_maxtxg(ds);
1309 dsl_dataset_rele(ds, FTAG);
1311 dsl_scan_visitds(scn, dsobj, tx);
1312 zap_cursor_fini(zc);
1313 if (scn->scn_pausing)
1316 zap_cursor_fini(zc);
1318 kmem_free(za, sizeof(zap_attribute_t));
1319 kmem_free(zc, sizeof(zap_cursor_t));
1323 dsl_scan_free_should_pause(dsl_scan_t *scn)
1325 uint64_t elapsed_nanosecs;
1327 elapsed_nanosecs = gethrtime() - scn->scn_sync_start_time;
1328 return (elapsed_nanosecs / NANOSEC > zfs_txg_timeout ||
1329 (elapsed_nanosecs / MICROSEC > zfs_free_min_time_ms &&
1330 txg_sync_waiting(scn->scn_dp)) ||
1331 spa_shutting_down(scn->scn_dp->dp_spa));
1335 dsl_scan_free_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1337 dsl_scan_t *scn = arg;
1339 if (!scn->scn_is_bptree ||
1340 (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_OBJSET)) {
1341 if (dsl_scan_free_should_pause(scn))
1345 zio_nowait(zio_free_sync(scn->scn_zio_root, scn->scn_dp->dp_spa,
1346 dmu_tx_get_txg(tx), bp, 0));
1347 dsl_dir_diduse_space(tx->tx_pool->dp_free_dir, DD_USED_HEAD,
1348 -bp_get_dsize_sync(scn->scn_dp->dp_spa, bp),
1349 -BP_GET_PSIZE(bp), -BP_GET_UCSIZE(bp), tx);
1350 scn->scn_visited_this_txg++;
1355 dsl_scan_active(dsl_scan_t *scn)
1357 spa_t *spa = scn->scn_dp->dp_spa;
1358 uint64_t used = 0, comp, uncomp;
1360 if (spa->spa_load_state != SPA_LOAD_NONE)
1362 if (spa_shutting_down(spa))
1365 if (scn->scn_phys.scn_state == DSS_SCANNING)
1368 if (spa_feature_is_active(spa,
1369 &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY])) {
1372 if (spa_version(scn->scn_dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
1373 (void) bpobj_space(&scn->scn_dp->dp_free_bpobj,
1374 &used, &comp, &uncomp);
1380 dsl_scan_sync(dsl_pool_t *dp, dmu_tx_t *tx)
1382 dsl_scan_t *scn = dp->dp_scan;
1383 spa_t *spa = dp->dp_spa;
1387 * Check for scn_restart_txg before checking spa_load_state, so
1388 * that we can restart an old-style scan while the pool is being
1389 * imported (see dsl_scan_init).
1391 if (scn->scn_restart_txg != 0 &&
1392 scn->scn_restart_txg <= tx->tx_txg) {
1393 pool_scan_func_t func = POOL_SCAN_SCRUB;
1394 dsl_scan_done(scn, B_FALSE, tx);
1395 if (vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL))
1396 func = POOL_SCAN_RESILVER;
1397 zfs_dbgmsg("restarting scan func=%u txg=%llu",
1399 dsl_scan_setup_sync(&func, tx);
1402 if (!dsl_scan_active(scn) ||
1403 spa_sync_pass(dp->dp_spa) > 1)
1406 scn->scn_visited_this_txg = 0;
1407 scn->scn_pausing = B_FALSE;
1408 scn->scn_sync_start_time = gethrtime();
1409 spa->spa_scrub_active = B_TRUE;
1412 * First process the free list. If we pause the free, don't do
1413 * any scanning. This ensures that there is no free list when
1414 * we are scanning, so the scan code doesn't have to worry about
1417 if (spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
1418 scn->scn_is_bptree = B_FALSE;
1419 scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
1420 NULL, ZIO_FLAG_MUSTSUCCEED);
1421 err = bpobj_iterate(&dp->dp_free_bpobj,
1422 dsl_scan_free_block_cb, scn, tx);
1423 VERIFY3U(0, ==, zio_wait(scn->scn_zio_root));
1425 if (err == 0 && spa_feature_is_active(spa,
1426 &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY])) {
1427 scn->scn_is_bptree = B_TRUE;
1428 scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
1429 NULL, ZIO_FLAG_MUSTSUCCEED);
1430 err = bptree_iterate(dp->dp_meta_objset,
1431 dp->dp_bptree_obj, B_TRUE, dsl_scan_free_block_cb,
1433 VERIFY0(zio_wait(scn->scn_zio_root));
1436 zfeature_info_t *feat = &spa_feature_table
1437 [SPA_FEATURE_ASYNC_DESTROY];
1438 /* finished; deactivate async destroy feature */
1439 spa_feature_decr(spa, feat, tx);
1440 ASSERT(!spa_feature_is_active(spa, feat));
1441 VERIFY0(zap_remove(dp->dp_meta_objset,
1442 DMU_POOL_DIRECTORY_OBJECT,
1443 DMU_POOL_BPTREE_OBJ, tx));
1444 VERIFY0(bptree_free(dp->dp_meta_objset,
1445 dp->dp_bptree_obj, tx));
1446 dp->dp_bptree_obj = 0;
1449 if (scn->scn_visited_this_txg) {
1450 zfs_dbgmsg("freed %llu blocks in %llums from "
1451 "free_bpobj/bptree txg %llu",
1452 (longlong_t)scn->scn_visited_this_txg,
1454 (gethrtime() - scn->scn_sync_start_time) / MICROSEC,
1455 (longlong_t)tx->tx_txg);
1456 scn->scn_visited_this_txg = 0;
1458 * Re-sync the ddt so that we can further modify
1459 * it when doing bprewrite.
1461 ddt_sync(spa, tx->tx_txg);
1463 if (err == ERESTART)
1467 if (scn->scn_phys.scn_state != DSS_SCANNING)
1470 if (scn->scn_phys.scn_ddt_bookmark.ddb_class <=
1471 scn->scn_phys.scn_ddt_class_max) {
1472 zfs_dbgmsg("doing scan sync txg %llu; "
1473 "ddt bm=%llu/%llu/%llu/%llx",
1474 (longlong_t)tx->tx_txg,
1475 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_class,
1476 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_type,
1477 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_checksum,
1478 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_cursor);
1479 ASSERT(scn->scn_phys.scn_bookmark.zb_objset == 0);
1480 ASSERT(scn->scn_phys.scn_bookmark.zb_object == 0);
1481 ASSERT(scn->scn_phys.scn_bookmark.zb_level == 0);
1482 ASSERT(scn->scn_phys.scn_bookmark.zb_blkid == 0);
1484 zfs_dbgmsg("doing scan sync txg %llu; bm=%llu/%llu/%llu/%llu",
1485 (longlong_t)tx->tx_txg,
1486 (longlong_t)scn->scn_phys.scn_bookmark.zb_objset,
1487 (longlong_t)scn->scn_phys.scn_bookmark.zb_object,
1488 (longlong_t)scn->scn_phys.scn_bookmark.zb_level,
1489 (longlong_t)scn->scn_phys.scn_bookmark.zb_blkid);
1492 scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
1493 NULL, ZIO_FLAG_CANFAIL);
1494 dsl_pool_config_enter(dp, FTAG);
1495 dsl_scan_visit(scn, tx);
1496 dsl_pool_config_exit(dp, FTAG);
1497 (void) zio_wait(scn->scn_zio_root);
1498 scn->scn_zio_root = NULL;
1500 zfs_dbgmsg("visited %llu blocks in %llums",
1501 (longlong_t)scn->scn_visited_this_txg,
1502 (longlong_t)(gethrtime() - scn->scn_sync_start_time) / MICROSEC);
1504 if (!scn->scn_pausing) {
1505 /* finished with scan. */
1506 zfs_dbgmsg("finished scan txg %llu", (longlong_t)tx->tx_txg);
1507 dsl_scan_done(scn, B_TRUE, tx);
1510 if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
1511 mutex_enter(&spa->spa_scrub_lock);
1512 while (spa->spa_scrub_inflight > 0) {
1513 cv_wait(&spa->spa_scrub_io_cv,
1514 &spa->spa_scrub_lock);
1516 mutex_exit(&spa->spa_scrub_lock);
1519 dsl_scan_sync_state(scn, tx);
1523 * This will start a new scan, or restart an existing one.
1526 dsl_resilver_restart(dsl_pool_t *dp, uint64_t txg)
1530 tx = dmu_tx_create_dd(dp->dp_mos_dir);
1531 VERIFY(0 == dmu_tx_assign(tx, TXG_WAIT));
1533 txg = dmu_tx_get_txg(tx);
1534 dp->dp_scan->scn_restart_txg = txg;
1537 dp->dp_scan->scn_restart_txg = txg;
1539 zfs_dbgmsg("restarting resilver txg=%llu", txg);
1543 dsl_scan_resilvering(dsl_pool_t *dp)
1545 return (dp->dp_scan->scn_phys.scn_state == DSS_SCANNING &&
1546 dp->dp_scan->scn_phys.scn_func == POOL_SCAN_RESILVER);
1554 count_block(zfs_all_blkstats_t *zab, const blkptr_t *bp)
1559 * If we resume after a reboot, zab will be NULL; don't record
1560 * incomplete stats in that case.
1565 for (i = 0; i < 4; i++) {
1566 int l = (i < 2) ? BP_GET_LEVEL(bp) : DN_MAX_LEVELS;
1567 int t = (i & 1) ? BP_GET_TYPE(bp) : DMU_OT_TOTAL;
1571 if (t & DMU_OT_NEWTYPE)
1574 zb = &zab->zab_type[l][t];
1576 zb->zb_asize += BP_GET_ASIZE(bp);
1577 zb->zb_lsize += BP_GET_LSIZE(bp);
1578 zb->zb_psize += BP_GET_PSIZE(bp);
1579 zb->zb_gangs += BP_COUNT_GANG(bp);
1581 switch (BP_GET_NDVAS(bp)) {
1583 if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1584 DVA_GET_VDEV(&bp->blk_dva[1]))
1585 zb->zb_ditto_2_of_2_samevdev++;
1588 equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1589 DVA_GET_VDEV(&bp->blk_dva[1])) +
1590 (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1591 DVA_GET_VDEV(&bp->blk_dva[2])) +
1592 (DVA_GET_VDEV(&bp->blk_dva[1]) ==
1593 DVA_GET_VDEV(&bp->blk_dva[2]));
1595 zb->zb_ditto_2_of_3_samevdev++;
1596 else if (equal == 3)
1597 zb->zb_ditto_3_of_3_samevdev++;
1604 dsl_scan_scrub_done(zio_t *zio)
1606 spa_t *spa = zio->io_spa;
1608 zio_data_buf_free(zio->io_data, zio->io_size);
1610 mutex_enter(&spa->spa_scrub_lock);
1611 spa->spa_scrub_inflight--;
1612 cv_broadcast(&spa->spa_scrub_io_cv);
1614 if (zio->io_error && (zio->io_error != ECKSUM ||
1615 !(zio->io_flags & ZIO_FLAG_SPECULATIVE))) {
1616 spa->spa_dsl_pool->dp_scan->scn_phys.scn_errors++;
1618 mutex_exit(&spa->spa_scrub_lock);
1622 dsl_scan_scrub_cb(dsl_pool_t *dp,
1623 const blkptr_t *bp, const zbookmark_t *zb)
1625 dsl_scan_t *scn = dp->dp_scan;
1626 size_t size = BP_GET_PSIZE(bp);
1627 spa_t *spa = dp->dp_spa;
1628 uint64_t phys_birth = BP_PHYSICAL_BIRTH(bp);
1629 boolean_t needs_io = B_FALSE;
1630 int zio_flags = ZIO_FLAG_SCAN_THREAD | ZIO_FLAG_RAW | ZIO_FLAG_CANFAIL;
1631 int zio_priority = 0;
1635 if (phys_birth <= scn->scn_phys.scn_min_txg ||
1636 phys_birth >= scn->scn_phys.scn_max_txg)
1639 count_block(dp->dp_blkstats, bp);
1641 ASSERT(DSL_SCAN_IS_SCRUB_RESILVER(scn));
1642 if (scn->scn_phys.scn_func == POOL_SCAN_SCRUB) {
1643 zio_flags |= ZIO_FLAG_SCRUB;
1644 zio_priority = ZIO_PRIORITY_SCRUB;
1646 scan_delay = zfs_scrub_delay;
1648 ASSERT3U(scn->scn_phys.scn_func, ==, POOL_SCAN_RESILVER);
1649 zio_flags |= ZIO_FLAG_RESILVER;
1650 zio_priority = ZIO_PRIORITY_RESILVER;
1652 scan_delay = zfs_resilver_delay;
1655 /* If it's an intent log block, failure is expected. */
1656 if (zb->zb_level == ZB_ZIL_LEVEL)
1657 zio_flags |= ZIO_FLAG_SPECULATIVE;
1659 for (d = 0; d < BP_GET_NDVAS(bp); d++) {
1660 vdev_t *vd = vdev_lookup_top(spa,
1661 DVA_GET_VDEV(&bp->blk_dva[d]));
1664 * Keep track of how much data we've examined so that
1665 * zpool(1M) status can make useful progress reports.
1667 scn->scn_phys.scn_examined += DVA_GET_ASIZE(&bp->blk_dva[d]);
1668 spa->spa_scan_pass_exam += DVA_GET_ASIZE(&bp->blk_dva[d]);
1670 /* if it's a resilver, this may not be in the target range */
1672 if (DVA_GET_GANG(&bp->blk_dva[d])) {
1674 * Gang members may be spread across multiple
1675 * vdevs, so the best estimate we have is the
1676 * scrub range, which has already been checked.
1677 * XXX -- it would be better to change our
1678 * allocation policy to ensure that all
1679 * gang members reside on the same vdev.
1683 needs_io = vdev_dtl_contains(vd, DTL_PARTIAL,
1689 if (needs_io && !zfs_no_scrub_io) {
1690 vdev_t *rvd = spa->spa_root_vdev;
1691 uint64_t maxinflight = rvd->vdev_children * zfs_top_maxinflight;
1692 void *data = zio_data_buf_alloc(size);
1694 mutex_enter(&spa->spa_scrub_lock);
1695 while (spa->spa_scrub_inflight >= maxinflight)
1696 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
1697 spa->spa_scrub_inflight++;
1698 mutex_exit(&spa->spa_scrub_lock);
1701 * If we're seeing recent (zfs_scan_idle) "important" I/Os
1702 * then throttle our workload to limit the impact of a scan.
1704 if (ddi_get_lbolt64() - spa->spa_last_io <= zfs_scan_idle)
1707 zio_nowait(zio_read(NULL, spa, bp, data, size,
1708 dsl_scan_scrub_done, NULL, zio_priority,
1712 /* do not relocate this block */
1717 dsl_scan(dsl_pool_t *dp, pool_scan_func_t func)
1719 spa_t *spa = dp->dp_spa;
1722 * Purge all vdev caches and probe all devices. We do this here
1723 * rather than in sync context because this requires a writer lock
1724 * on the spa_config lock, which we can't do from sync context. The
1725 * spa_scrub_reopen flag indicates that vdev_open() should not
1726 * attempt to start another scrub.
1728 spa_vdev_state_enter(spa, SCL_NONE);
1729 spa->spa_scrub_reopen = B_TRUE;
1730 vdev_reopen(spa->spa_root_vdev);
1731 spa->spa_scrub_reopen = B_FALSE;
1732 (void) spa_vdev_state_exit(spa, NULL, 0);
1734 return (dsl_sync_task(spa_name(spa), dsl_scan_setup_check,
1735 dsl_scan_setup_sync, &func, 0));
1738 #if defined(_KERNEL) && defined(HAVE_SPL)
1739 module_param(zfs_top_maxinflight, int, 0644);
1740 MODULE_PARM_DESC(zfs_top_maxinflight, "Max I/Os per top-level");
1742 module_param(zfs_resilver_delay, int, 0644);
1743 MODULE_PARM_DESC(zfs_resilver_delay, "Number of ticks to delay resilver");
1745 module_param(zfs_scrub_delay, int, 0644);
1746 MODULE_PARM_DESC(zfs_scrub_delay, "Number of ticks to delay scrub");
1748 module_param(zfs_scan_idle, int, 0644);
1749 MODULE_PARM_DESC(zfs_scan_idle, "Idle window in clock ticks");
1751 module_param(zfs_scan_min_time_ms, int, 0644);
1752 MODULE_PARM_DESC(zfs_scan_min_time_ms, "Min millisecs to scrub per txg");
1754 module_param(zfs_free_min_time_ms, int, 0644);
1755 MODULE_PARM_DESC(zfs_free_min_time_ms, "Min millisecs to free per txg");
1757 module_param(zfs_resilver_min_time_ms, int, 0644);
1758 MODULE_PARM_DESC(zfs_resilver_min_time_ms, "Min millisecs to resilver per txg");
1760 module_param(zfs_no_scrub_io, int, 0644);
1761 MODULE_PARM_DESC(zfs_no_scrub_io, "Set to disable scrub I/O");
1763 module_param(zfs_no_scrub_prefetch, int, 0644);
1764 MODULE_PARM_DESC(zfs_no_scrub_prefetch, "Set to disable scrub prefetching");