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.
25 #include <sys/dsl_scan.h>
26 #include <sys/dsl_pool.h>
27 #include <sys/dsl_dataset.h>
28 #include <sys/dsl_prop.h>
29 #include <sys/dsl_dir.h>
30 #include <sys/dsl_synctask.h>
31 #include <sys/dnode.h>
32 #include <sys/dmu_tx.h>
33 #include <sys/dmu_objset.h>
37 #include <sys/zfs_context.h>
38 #include <sys/fs/zfs.h>
39 #include <sys/zfs_znode.h>
40 #include <sys/spa_impl.h>
41 #include <sys/vdev_impl.h>
42 #include <sys/zil_impl.h>
43 #include <sys/zio_checksum.h>
46 #include <sys/sa_impl.h>
48 #include <sys/zfs_vfsops.h>
51 typedef int (scan_cb_t
)(dsl_pool_t
*, const blkptr_t
*, const zbookmark_t
*);
53 static scan_cb_t dsl_scan_defrag_cb
;
54 static scan_cb_t dsl_scan_scrub_cb
;
55 static scan_cb_t dsl_scan_remove_cb
;
56 static dsl_syncfunc_t dsl_scan_cancel_sync
;
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 boolean_t zfs_no_scrub_io
= B_FALSE
; /* set to disable scrub i/o */
68 boolean_t 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 extern int zfs_txg_timeout
;
78 /* the order has to match pool_scan_type */
79 static scan_cb_t
*scan_funcs
[POOL_SCAN_FUNCS
] = {
81 dsl_scan_scrub_cb
, /* POOL_SCAN_SCRUB */
82 dsl_scan_scrub_cb
, /* POOL_SCAN_RESILVER */
86 dsl_scan_init(dsl_pool_t
*dp
, uint64_t txg
)
90 spa_t
*spa
= dp
->dp_spa
;
93 scn
= dp
->dp_scan
= kmem_zalloc(sizeof (dsl_scan_t
), KM_SLEEP
);
96 err
= zap_lookup(dp
->dp_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
97 "scrub_func", sizeof (uint64_t), 1, &f
);
100 * There was an old-style scrub in progress. Restart a
101 * new-style scrub from the beginning.
103 scn
->scn_restart_txg
= txg
;
104 zfs_dbgmsg("old-style scrub was in progress; "
105 "restarting new-style scrub in txg %llu",
106 scn
->scn_restart_txg
);
109 * Load the queue obj from the old location so that it
110 * can be freed by dsl_scan_done().
112 (void) zap_lookup(dp
->dp_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
113 "scrub_queue", sizeof (uint64_t), 1,
114 &scn
->scn_phys
.scn_queue_obj
);
116 err
= zap_lookup(dp
->dp_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
117 DMU_POOL_SCAN
, sizeof (uint64_t), SCAN_PHYS_NUMINTS
,
124 if (scn
->scn_phys
.scn_state
== DSS_SCANNING
&&
125 spa_prev_software_version(dp
->dp_spa
) < SPA_VERSION_SCAN
) {
127 * A new-type scrub was in progress on an old
128 * pool, and the pool was accessed by old
129 * software. Restart from the beginning, since
130 * the old software may have changed the pool in
133 scn
->scn_restart_txg
= txg
;
134 zfs_dbgmsg("new-style scrub was modified "
135 "by old software; restarting in txg %llu",
136 scn
->scn_restart_txg
);
140 spa_scan_stat_init(spa
);
145 dsl_scan_fini(dsl_pool_t
*dp
)
148 kmem_free(dp
->dp_scan
, sizeof (dsl_scan_t
));
155 dsl_scan_setup_check(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
157 dsl_scan_t
*scn
= arg1
;
159 if (scn
->scn_phys
.scn_state
== DSS_SCANNING
)
167 dsl_scan_setup_sync(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
169 dsl_scan_t
*scn
= arg1
;
170 pool_scan_func_t
*funcp
= arg2
;
171 dmu_object_type_t ot
= 0;
172 dsl_pool_t
*dp
= scn
->scn_dp
;
173 spa_t
*spa
= dp
->dp_spa
;
175 ASSERT(scn
->scn_phys
.scn_state
!= DSS_SCANNING
);
176 ASSERT(*funcp
> POOL_SCAN_NONE
&& *funcp
< POOL_SCAN_FUNCS
);
177 bzero(&scn
->scn_phys
, sizeof (scn
->scn_phys
));
178 scn
->scn_phys
.scn_func
= *funcp
;
179 scn
->scn_phys
.scn_state
= DSS_SCANNING
;
180 scn
->scn_phys
.scn_min_txg
= 0;
181 scn
->scn_phys
.scn_max_txg
= tx
->tx_txg
;
182 scn
->scn_phys
.scn_ddt_class_max
= DDT_CLASSES
- 1; /* the entire DDT */
183 scn
->scn_phys
.scn_start_time
= gethrestime_sec();
184 scn
->scn_phys
.scn_errors
= 0;
185 scn
->scn_phys
.scn_to_examine
= spa
->spa_root_vdev
->vdev_stat
.vs_alloc
;
186 scn
->scn_restart_txg
= 0;
187 spa_scan_stat_init(spa
);
189 if (DSL_SCAN_IS_SCRUB_RESILVER(scn
)) {
190 scn
->scn_phys
.scn_ddt_class_max
= zfs_scrub_ddt_class_max
;
192 /* rewrite all disk labels */
193 vdev_config_dirty(spa
->spa_root_vdev
);
195 if (vdev_resilver_needed(spa
->spa_root_vdev
,
196 &scn
->scn_phys
.scn_min_txg
, &scn
->scn_phys
.scn_max_txg
)) {
197 spa_event_notify(spa
, NULL
, ESC_ZFS_RESILVER_START
);
199 spa_event_notify(spa
, NULL
, ESC_ZFS_SCRUB_START
);
202 spa
->spa_scrub_started
= B_TRUE
;
204 * If this is an incremental scrub, limit the DDT scrub phase
205 * to just the auto-ditto class (for correctness); the rest
206 * of the scrub should go faster using top-down pruning.
208 if (scn
->scn_phys
.scn_min_txg
> TXG_INITIAL
)
209 scn
->scn_phys
.scn_ddt_class_max
= DDT_CLASS_DITTO
;
213 /* back to the generic stuff */
215 if (dp
->dp_blkstats
== NULL
) {
217 kmem_alloc(sizeof (zfs_all_blkstats_t
), KM_SLEEP
);
219 bzero(dp
->dp_blkstats
, sizeof (zfs_all_blkstats_t
));
221 if (spa_version(spa
) < SPA_VERSION_DSL_SCRUB
)
222 ot
= DMU_OT_ZAP_OTHER
;
224 scn
->scn_phys
.scn_queue_obj
= zap_create(dp
->dp_meta_objset
,
225 ot
? ot
: DMU_OT_SCAN_QUEUE
, DMU_OT_NONE
, 0, tx
);
227 dsl_scan_sync_state(scn
, tx
);
229 spa_history_log_internal(LOG_POOL_SCAN
, spa
, tx
,
230 "func=%u mintxg=%llu maxtxg=%llu",
231 *funcp
, scn
->scn_phys
.scn_min_txg
, scn
->scn_phys
.scn_max_txg
);
236 dsl_scan_done(dsl_scan_t
*scn
, boolean_t complete
, dmu_tx_t
*tx
)
238 static const char *old_names
[] = {
240 "scrub_ddt_bookmark",
241 "scrub_ddt_class_max",
250 dsl_pool_t
*dp
= scn
->scn_dp
;
251 spa_t
*spa
= dp
->dp_spa
;
254 /* Remove any remnants of an old-style scrub. */
255 for (i
= 0; old_names
[i
]; i
++) {
256 (void) zap_remove(dp
->dp_meta_objset
,
257 DMU_POOL_DIRECTORY_OBJECT
, old_names
[i
], tx
);
260 if (scn
->scn_phys
.scn_queue_obj
!= 0) {
261 VERIFY(0 == dmu_object_free(dp
->dp_meta_objset
,
262 scn
->scn_phys
.scn_queue_obj
, tx
));
263 scn
->scn_phys
.scn_queue_obj
= 0;
267 * If we were "restarted" from a stopped state, don't bother
268 * with anything else.
270 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
274 scn
->scn_phys
.scn_state
= DSS_FINISHED
;
276 scn
->scn_phys
.scn_state
= DSS_CANCELED
;
278 spa_history_log_internal(LOG_POOL_SCAN_DONE
, spa
, tx
,
279 "complete=%u", complete
);
281 if (DSL_SCAN_IS_SCRUB_RESILVER(scn
)) {
282 mutex_enter(&spa
->spa_scrub_lock
);
283 while (spa
->spa_scrub_inflight
> 0) {
284 cv_wait(&spa
->spa_scrub_io_cv
,
285 &spa
->spa_scrub_lock
);
287 mutex_exit(&spa
->spa_scrub_lock
);
288 spa
->spa_scrub_started
= B_FALSE
;
289 spa
->spa_scrub_active
= B_FALSE
;
292 * If the scrub/resilver completed, update all DTLs to
293 * reflect this. Whether it succeeded or not, vacate
294 * all temporary scrub DTLs.
296 vdev_dtl_reassess(spa
->spa_root_vdev
, tx
->tx_txg
,
297 complete
? scn
->scn_phys
.scn_max_txg
: 0, B_TRUE
);
299 spa_event_notify(spa
, NULL
, scn
->scn_phys
.scn_min_txg
?
300 ESC_ZFS_RESILVER_FINISH
: ESC_ZFS_SCRUB_FINISH
);
302 spa_errlog_rotate(spa
);
305 * We may have finished replacing a device.
306 * Let the async thread assess this and handle the detach.
308 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
311 scn
->scn_phys
.scn_end_time
= gethrestime_sec();
316 dsl_scan_cancel_check(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
318 dsl_scan_t
*scn
= arg1
;
320 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
327 dsl_scan_cancel_sync(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
329 dsl_scan_t
*scn
= arg1
;
331 dsl_scan_done(scn
, B_FALSE
, tx
);
332 dsl_scan_sync_state(scn
, tx
);
336 dsl_scan_cancel(dsl_pool_t
*dp
)
338 boolean_t complete
= B_FALSE
;
341 err
= dsl_sync_task_do(dp
, dsl_scan_cancel_check
,
342 dsl_scan_cancel_sync
, dp
->dp_scan
, &complete
, 3);
346 static void dsl_scan_visitbp(blkptr_t
*bp
,
347 const zbookmark_t
*zb
, dnode_phys_t
*dnp
, arc_buf_t
*pbuf
,
348 dsl_dataset_t
*ds
, dsl_scan_t
*scn
, dmu_objset_type_t ostype
,
350 static void dsl_scan_visitdnode(dsl_scan_t
*, dsl_dataset_t
*ds
,
351 dmu_objset_type_t ostype
,
352 dnode_phys_t
*dnp
, arc_buf_t
*buf
, uint64_t object
, dmu_tx_t
*tx
);
355 dsl_free(dsl_pool_t
*dp
, uint64_t txg
, const blkptr_t
*bp
)
357 zio_free(dp
->dp_spa
, txg
, bp
);
361 dsl_free_sync(zio_t
*pio
, dsl_pool_t
*dp
, uint64_t txg
, const blkptr_t
*bpp
)
363 ASSERT(dsl_pool_sync_context(dp
));
364 zio_nowait(zio_free_sync(pio
, dp
->dp_spa
, txg
, bpp
, pio
->io_flags
));
368 dsl_read(zio_t
*pio
, spa_t
*spa
, const blkptr_t
*bpp
, arc_buf_t
*pbuf
,
369 arc_done_func_t
*done
, void *private, int priority
, int zio_flags
,
370 uint32_t *arc_flags
, const zbookmark_t
*zb
)
372 return (arc_read(pio
, spa
, bpp
, pbuf
, done
, private,
373 priority
, zio_flags
, arc_flags
, zb
));
377 dsl_read_nolock(zio_t
*pio
, spa_t
*spa
, const blkptr_t
*bpp
,
378 arc_done_func_t
*done
, void *private, int priority
, int zio_flags
,
379 uint32_t *arc_flags
, const zbookmark_t
*zb
)
381 return (arc_read_nolock(pio
, spa
, bpp
, done
, private,
382 priority
, zio_flags
, arc_flags
, zb
));
386 bookmark_is_zero(const zbookmark_t
*zb
)
388 return (zb
->zb_objset
== 0 && zb
->zb_object
== 0 &&
389 zb
->zb_level
== 0 && zb
->zb_blkid
== 0);
392 /* dnp is the dnode for zb1->zb_object */
394 bookmark_is_before(const dnode_phys_t
*dnp
, const zbookmark_t
*zb1
,
395 const zbookmark_t
*zb2
)
397 uint64_t zb1nextL0
, zb2thisobj
;
399 ASSERT(zb1
->zb_objset
== zb2
->zb_objset
);
400 ASSERT(zb2
->zb_level
== 0);
403 * A bookmark in the deadlist is considered to be after
406 if (zb2
->zb_object
== DMU_DEADLIST_OBJECT
)
409 /* The objset_phys_t isn't before anything. */
413 zb1nextL0
= (zb1
->zb_blkid
+ 1) <<
414 ((zb1
->zb_level
) * (dnp
->dn_indblkshift
- SPA_BLKPTRSHIFT
));
416 zb2thisobj
= zb2
->zb_object
? zb2
->zb_object
:
417 zb2
->zb_blkid
<< (DNODE_BLOCK_SHIFT
- DNODE_SHIFT
);
419 if (zb1
->zb_object
== DMU_META_DNODE_OBJECT
) {
420 uint64_t nextobj
= zb1nextL0
*
421 (dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
) >> DNODE_SHIFT
;
422 return (nextobj
<= zb2thisobj
);
425 if (zb1
->zb_object
< zb2thisobj
)
427 if (zb1
->zb_object
> zb2thisobj
)
429 if (zb2
->zb_object
== DMU_META_DNODE_OBJECT
)
431 return (zb1nextL0
<= zb2
->zb_blkid
);
435 dsl_scan_ds_maxtxg(dsl_dataset_t
*ds
)
437 uint64_t smt
= ds
->ds_dir
->dd_pool
->dp_scan
->scn_phys
.scn_max_txg
;
438 if (dsl_dataset_is_snapshot(ds
))
439 return (MIN(smt
, ds
->ds_phys
->ds_creation_txg
));
444 dsl_scan_sync_state(dsl_scan_t
*scn
, dmu_tx_t
*tx
)
446 VERIFY(0 == zap_update(scn
->scn_dp
->dp_meta_objset
,
447 DMU_POOL_DIRECTORY_OBJECT
,
448 DMU_POOL_SCAN
, sizeof (uint64_t), SCAN_PHYS_NUMINTS
,
449 &scn
->scn_phys
, tx
));
453 dsl_scan_check_pause(dsl_scan_t
*scn
, const zbookmark_t
*zb
)
455 uint64_t elapsed_nanosecs
;
458 /* we never skip user/group accounting objects */
459 if (zb
&& (int64_t)zb
->zb_object
< 0)
462 if (scn
->scn_pausing
)
463 return (B_TRUE
); /* we're already pausing */
465 if (!bookmark_is_zero(&scn
->scn_phys
.scn_bookmark
))
466 return (B_FALSE
); /* we're resuming */
468 /* We only know how to resume from level-0 blocks. */
469 if (zb
&& zb
->zb_level
!= 0)
472 mintime
= (scn
->scn_phys
.scn_func
== POOL_SCAN_RESILVER
) ?
473 zfs_resilver_min_time_ms
: zfs_scan_min_time_ms
;
474 elapsed_nanosecs
= gethrtime() - scn
->scn_sync_start_time
;
475 if (elapsed_nanosecs
/ NANOSEC
> zfs_txg_timeout
||
476 (elapsed_nanosecs
/ MICROSEC
> mintime
&&
477 txg_sync_waiting(scn
->scn_dp
)) ||
478 spa_shutting_down(scn
->scn_dp
->dp_spa
)) {
480 dprintf("pausing at bookmark %llx/%llx/%llx/%llx\n",
481 (longlong_t
)zb
->zb_objset
,
482 (longlong_t
)zb
->zb_object
,
483 (longlong_t
)zb
->zb_level
,
484 (longlong_t
)zb
->zb_blkid
);
485 scn
->scn_phys
.scn_bookmark
= *zb
;
487 dprintf("pausing at DDT bookmark %llx/%llx/%llx/%llx\n",
488 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_class
,
489 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_type
,
490 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_checksum
,
491 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_cursor
);
492 scn
->scn_pausing
= B_TRUE
;
498 typedef struct zil_scan_arg
{
500 zil_header_t
*zsa_zh
;
505 dsl_scan_zil_block(zilog_t
*zilog
, blkptr_t
*bp
, void *arg
, uint64_t claim_txg
)
507 zil_scan_arg_t
*zsa
= arg
;
508 dsl_pool_t
*dp
= zsa
->zsa_dp
;
509 dsl_scan_t
*scn
= dp
->dp_scan
;
510 zil_header_t
*zh
= zsa
->zsa_zh
;
513 if (bp
->blk_birth
<= scn
->scn_phys
.scn_cur_min_txg
)
517 * One block ("stubby") can be allocated a long time ago; we
518 * want to visit that one because it has been allocated
519 * (on-disk) even if it hasn't been claimed (even though for
520 * scrub there's nothing to do to it).
522 if (claim_txg
== 0 && bp
->blk_birth
>= spa_first_txg(dp
->dp_spa
))
525 SET_BOOKMARK(&zb
, zh
->zh_log
.blk_cksum
.zc_word
[ZIL_ZC_OBJSET
],
526 ZB_ZIL_OBJECT
, ZB_ZIL_LEVEL
, bp
->blk_cksum
.zc_word
[ZIL_ZC_SEQ
]);
528 VERIFY(0 == scan_funcs
[scn
->scn_phys
.scn_func
](dp
, bp
, &zb
));
534 dsl_scan_zil_record(zilog_t
*zilog
, lr_t
*lrc
, void *arg
, uint64_t claim_txg
)
536 if (lrc
->lrc_txtype
== TX_WRITE
) {
537 zil_scan_arg_t
*zsa
= arg
;
538 dsl_pool_t
*dp
= zsa
->zsa_dp
;
539 dsl_scan_t
*scn
= dp
->dp_scan
;
540 zil_header_t
*zh
= zsa
->zsa_zh
;
541 lr_write_t
*lr
= (lr_write_t
*)lrc
;
542 blkptr_t
*bp
= &lr
->lr_blkptr
;
545 if (bp
->blk_birth
<= scn
->scn_phys
.scn_cur_min_txg
)
549 * birth can be < claim_txg if this record's txg is
550 * already txg sync'ed (but this log block contains
551 * other records that are not synced)
553 if (claim_txg
== 0 || bp
->blk_birth
< claim_txg
)
556 SET_BOOKMARK(&zb
, zh
->zh_log
.blk_cksum
.zc_word
[ZIL_ZC_OBJSET
],
557 lr
->lr_foid
, ZB_ZIL_LEVEL
,
558 lr
->lr_offset
/ BP_GET_LSIZE(bp
));
560 VERIFY(0 == scan_funcs
[scn
->scn_phys
.scn_func
](dp
, bp
, &zb
));
566 dsl_scan_zil(dsl_pool_t
*dp
, zil_header_t
*zh
)
568 uint64_t claim_txg
= zh
->zh_claim_txg
;
569 zil_scan_arg_t zsa
= { dp
, zh
};
573 * We only want to visit blocks that have been claimed but not yet
574 * replayed (or, in read-only mode, blocks that *would* be claimed).
576 if (claim_txg
== 0 && spa_writeable(dp
->dp_spa
))
579 zilog
= zil_alloc(dp
->dp_meta_objset
, zh
);
581 (void) zil_parse(zilog
, dsl_scan_zil_block
, dsl_scan_zil_record
, &zsa
,
589 dsl_scan_prefetch(dsl_scan_t
*scn
, arc_buf_t
*buf
, blkptr_t
*bp
,
590 uint64_t objset
, uint64_t object
, uint64_t blkid
)
593 uint32_t flags
= ARC_NOWAIT
| ARC_PREFETCH
;
595 if (zfs_no_scrub_prefetch
)
598 if (BP_IS_HOLE(bp
) || bp
->blk_birth
<= scn
->scn_phys
.scn_min_txg
||
599 (BP_GET_LEVEL(bp
) == 0 && BP_GET_TYPE(bp
) != DMU_OT_DNODE
))
602 SET_BOOKMARK(&czb
, objset
, object
, BP_GET_LEVEL(bp
), blkid
);
605 * XXX need to make sure all of these arc_read() prefetches are
606 * done before setting xlateall (similar to dsl_read())
608 (void) arc_read(scn
->scn_zio_root
, scn
->scn_dp
->dp_spa
, bp
,
609 buf
, NULL
, NULL
, ZIO_PRIORITY_ASYNC_READ
,
610 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SCAN_THREAD
, &flags
, &czb
);
614 dsl_scan_check_resume(dsl_scan_t
*scn
, const dnode_phys_t
*dnp
,
615 const zbookmark_t
*zb
)
618 * We never skip over user/group accounting objects (obj<0)
620 if (!bookmark_is_zero(&scn
->scn_phys
.scn_bookmark
) &&
621 (int64_t)zb
->zb_object
>= 0) {
623 * If we already visited this bp & everything below (in
624 * a prior txg sync), don't bother doing it again.
626 if (bookmark_is_before(dnp
, zb
, &scn
->scn_phys
.scn_bookmark
))
630 * If we found the block we're trying to resume from, or
631 * we went past it to a different object, zero it out to
632 * indicate that it's OK to start checking for pausing
635 if (bcmp(zb
, &scn
->scn_phys
.scn_bookmark
, sizeof (*zb
)) == 0 ||
636 zb
->zb_object
> scn
->scn_phys
.scn_bookmark
.zb_object
) {
637 dprintf("resuming at %llx/%llx/%llx/%llx\n",
638 (longlong_t
)zb
->zb_objset
,
639 (longlong_t
)zb
->zb_object
,
640 (longlong_t
)zb
->zb_level
,
641 (longlong_t
)zb
->zb_blkid
);
642 bzero(&scn
->scn_phys
.scn_bookmark
, sizeof (*zb
));
649 * Return nonzero on i/o error.
650 * Return new buf to write out in *bufp.
653 dsl_scan_recurse(dsl_scan_t
*scn
, dsl_dataset_t
*ds
, dmu_objset_type_t ostype
,
654 dnode_phys_t
*dnp
, const blkptr_t
*bp
,
655 const zbookmark_t
*zb
, dmu_tx_t
*tx
, arc_buf_t
**bufp
)
657 dsl_pool_t
*dp
= scn
->scn_dp
;
658 int zio_flags
= ZIO_FLAG_CANFAIL
| ZIO_FLAG_SCAN_THREAD
;
661 if (BP_GET_LEVEL(bp
) > 0) {
662 uint32_t flags
= ARC_WAIT
;
665 int epb
= BP_GET_LSIZE(bp
) >> SPA_BLKPTRSHIFT
;
667 err
= arc_read_nolock(NULL
, dp
->dp_spa
, bp
,
668 arc_getbuf_func
, bufp
,
669 ZIO_PRIORITY_ASYNC_READ
, zio_flags
, &flags
, zb
);
671 scn
->scn_phys
.scn_errors
++;
674 for (i
= 0, cbp
= (*bufp
)->b_data
; i
< epb
; i
++, cbp
++) {
675 dsl_scan_prefetch(scn
, *bufp
, cbp
, zb
->zb_objset
,
676 zb
->zb_object
, zb
->zb_blkid
* epb
+ i
);
678 for (i
= 0, cbp
= (*bufp
)->b_data
; i
< epb
; i
++, cbp
++) {
681 SET_BOOKMARK(&czb
, zb
->zb_objset
, zb
->zb_object
,
683 zb
->zb_blkid
* epb
+ i
);
684 dsl_scan_visitbp(cbp
, &czb
, dnp
,
685 *bufp
, ds
, scn
, ostype
, tx
);
687 } else if (BP_GET_TYPE(bp
) == DMU_OT_USERGROUP_USED
) {
688 uint32_t flags
= ARC_WAIT
;
690 err
= arc_read_nolock(NULL
, dp
->dp_spa
, bp
,
691 arc_getbuf_func
, bufp
,
692 ZIO_PRIORITY_ASYNC_READ
, zio_flags
, &flags
, zb
);
694 scn
->scn_phys
.scn_errors
++;
697 } else if (BP_GET_TYPE(bp
) == DMU_OT_DNODE
) {
698 uint32_t flags
= ARC_WAIT
;
701 int epb
= BP_GET_LSIZE(bp
) >> DNODE_SHIFT
;
703 err
= arc_read_nolock(NULL
, dp
->dp_spa
, bp
,
704 arc_getbuf_func
, bufp
,
705 ZIO_PRIORITY_ASYNC_READ
, zio_flags
, &flags
, zb
);
707 scn
->scn_phys
.scn_errors
++;
710 for (i
= 0, cdnp
= (*bufp
)->b_data
; i
< epb
; i
++, cdnp
++) {
711 for (j
= 0; j
< cdnp
->dn_nblkptr
; j
++) {
712 blkptr_t
*cbp
= &cdnp
->dn_blkptr
[j
];
713 dsl_scan_prefetch(scn
, *bufp
, cbp
,
714 zb
->zb_objset
, zb
->zb_blkid
* epb
+ i
, j
);
717 for (i
= 0, cdnp
= (*bufp
)->b_data
; i
< epb
; i
++, cdnp
++) {
718 dsl_scan_visitdnode(scn
, ds
, ostype
,
719 cdnp
, *bufp
, zb
->zb_blkid
* epb
+ i
, tx
);
722 } else if (BP_GET_TYPE(bp
) == DMU_OT_OBJSET
) {
723 uint32_t flags
= ARC_WAIT
;
726 err
= arc_read_nolock(NULL
, dp
->dp_spa
, bp
,
727 arc_getbuf_func
, bufp
,
728 ZIO_PRIORITY_ASYNC_READ
, zio_flags
, &flags
, zb
);
730 scn
->scn_phys
.scn_errors
++;
734 osp
= (*bufp
)->b_data
;
736 dsl_scan_visitdnode(scn
, ds
, osp
->os_type
,
737 &osp
->os_meta_dnode
, *bufp
, DMU_META_DNODE_OBJECT
, tx
);
739 if (OBJSET_BUF_HAS_USERUSED(*bufp
)) {
741 * We also always visit user/group accounting
742 * objects, and never skip them, even if we are
743 * pausing. This is necessary so that the space
744 * deltas from this txg get integrated.
746 dsl_scan_visitdnode(scn
, ds
, osp
->os_type
,
747 &osp
->os_groupused_dnode
, *bufp
,
748 DMU_GROUPUSED_OBJECT
, tx
);
749 dsl_scan_visitdnode(scn
, ds
, osp
->os_type
,
750 &osp
->os_userused_dnode
, *bufp
,
751 DMU_USERUSED_OBJECT
, tx
);
759 dsl_scan_visitdnode(dsl_scan_t
*scn
, dsl_dataset_t
*ds
,
760 dmu_objset_type_t ostype
, dnode_phys_t
*dnp
, arc_buf_t
*buf
,
761 uint64_t object
, dmu_tx_t
*tx
)
765 for (j
= 0; j
< dnp
->dn_nblkptr
; j
++) {
768 SET_BOOKMARK(&czb
, ds
? ds
->ds_object
: 0, object
,
769 dnp
->dn_nlevels
- 1, j
);
770 dsl_scan_visitbp(&dnp
->dn_blkptr
[j
],
771 &czb
, dnp
, buf
, ds
, scn
, ostype
, tx
);
774 if (dnp
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
) {
776 SET_BOOKMARK(&czb
, ds
? ds
->ds_object
: 0, object
,
778 dsl_scan_visitbp(&dnp
->dn_spill
,
779 &czb
, dnp
, buf
, ds
, scn
, ostype
, tx
);
784 * The arguments are in this order because mdb can only print the
785 * first 5; we want them to be useful.
788 dsl_scan_visitbp(blkptr_t
*bp
, const zbookmark_t
*zb
,
789 dnode_phys_t
*dnp
, arc_buf_t
*pbuf
,
790 dsl_dataset_t
*ds
, dsl_scan_t
*scn
, dmu_objset_type_t ostype
,
793 dsl_pool_t
*dp
= scn
->scn_dp
;
794 arc_buf_t
*buf
= NULL
;
795 blkptr_t bp_toread
= *bp
;
797 /* ASSERT(pbuf == NULL || arc_released(pbuf)); */
799 if (dsl_scan_check_pause(scn
, zb
))
802 if (dsl_scan_check_resume(scn
, dnp
, zb
))
805 if (bp
->blk_birth
== 0)
808 scn
->scn_visited_this_txg
++;
811 "visiting ds=%p/%llu zb=%llx/%llx/%llx/%llx buf=%p bp=%p",
812 ds
, ds
? ds
->ds_object
: 0,
813 zb
->zb_objset
, zb
->zb_object
, zb
->zb_level
, zb
->zb_blkid
,
816 if (bp
->blk_birth
<= scn
->scn_phys
.scn_cur_min_txg
)
819 if (BP_GET_TYPE(bp
) != DMU_OT_USERGROUP_USED
) {
821 * For non-user-accounting blocks, we need to read the
822 * new bp (from a deleted snapshot, found in
823 * check_existing_xlation). If we used the old bp,
824 * pointers inside this block from before we resumed
825 * would be untranslated.
827 * For user-accounting blocks, we need to read the old
828 * bp, because we will apply the entire space delta to
829 * it (original untranslated -> translations from
830 * deleted snap -> now).
835 if (dsl_scan_recurse(scn
, ds
, ostype
, dnp
, &bp_toread
, zb
, tx
,
840 * If dsl_scan_ddt() has aready visited this block, it will have
841 * already done any translations or scrubbing, so don't call the
844 if (ddt_class_contains(dp
->dp_spa
,
845 scn
->scn_phys
.scn_ddt_class_max
, bp
)) {
851 * If this block is from the future (after cur_max_txg), then we
852 * are doing this on behalf of a deleted snapshot, and we will
853 * revisit the future block on the next pass of this dataset.
854 * Don't scan it now unless we need to because something
855 * under it was modified.
857 if (bp
->blk_birth
<= scn
->scn_phys
.scn_cur_max_txg
) {
858 scan_funcs
[scn
->scn_phys
.scn_func
](dp
, bp
, zb
);
861 (void) arc_buf_remove_ref(buf
, &buf
);
865 dsl_scan_visit_rootbp(dsl_scan_t
*scn
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
870 SET_BOOKMARK(&zb
, ds
? ds
->ds_object
: DMU_META_OBJSET
,
871 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
872 dsl_scan_visitbp(bp
, &zb
, NULL
, NULL
,
873 ds
, scn
, DMU_OST_NONE
, tx
);
875 dprintf_ds(ds
, "finished scan%s", "");
879 dsl_scan_ds_destroyed(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
881 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
882 dsl_scan_t
*scn
= dp
->dp_scan
;
885 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
888 if (scn
->scn_phys
.scn_bookmark
.zb_objset
== ds
->ds_object
) {
889 if (dsl_dataset_is_snapshot(ds
)) {
890 /* Note, scn_cur_{min,max}_txg stays the same. */
891 scn
->scn_phys
.scn_bookmark
.zb_objset
=
892 ds
->ds_phys
->ds_next_snap_obj
;
893 zfs_dbgmsg("destroying ds %llu; currently traversing; "
894 "reset zb_objset to %llu",
895 (u_longlong_t
)ds
->ds_object
,
896 (u_longlong_t
)ds
->ds_phys
->ds_next_snap_obj
);
897 scn
->scn_phys
.scn_flags
|= DSF_VISIT_DS_AGAIN
;
899 SET_BOOKMARK(&scn
->scn_phys
.scn_bookmark
,
900 ZB_DESTROYED_OBJSET
, 0, 0, 0);
901 zfs_dbgmsg("destroying ds %llu; currently traversing; "
902 "reset bookmark to -1,0,0,0",
903 (u_longlong_t
)ds
->ds_object
);
905 } else if (zap_lookup_int_key(dp
->dp_meta_objset
,
906 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
, &mintxg
) == 0) {
907 ASSERT3U(ds
->ds_phys
->ds_num_children
, <=, 1);
908 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
909 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
, tx
));
910 if (dsl_dataset_is_snapshot(ds
)) {
912 * We keep the same mintxg; it could be >
913 * ds_creation_txg if the previous snapshot was
916 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
917 scn
->scn_phys
.scn_queue_obj
,
918 ds
->ds_phys
->ds_next_snap_obj
, mintxg
, tx
) == 0);
919 zfs_dbgmsg("destroying ds %llu; in queue; "
920 "replacing with %llu",
921 (u_longlong_t
)ds
->ds_object
,
922 (u_longlong_t
)ds
->ds_phys
->ds_next_snap_obj
);
924 zfs_dbgmsg("destroying ds %llu; in queue; removing",
925 (u_longlong_t
)ds
->ds_object
);
928 zfs_dbgmsg("destroying ds %llu; ignoring",
929 (u_longlong_t
)ds
->ds_object
);
933 * dsl_scan_sync() should be called after this, and should sync
934 * out our changed state, but just to be safe, do it here.
936 dsl_scan_sync_state(scn
, tx
);
940 dsl_scan_ds_snapshotted(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
942 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
943 dsl_scan_t
*scn
= dp
->dp_scan
;
946 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
949 ASSERT(ds
->ds_phys
->ds_prev_snap_obj
!= 0);
951 if (scn
->scn_phys
.scn_bookmark
.zb_objset
== ds
->ds_object
) {
952 scn
->scn_phys
.scn_bookmark
.zb_objset
=
953 ds
->ds_phys
->ds_prev_snap_obj
;
954 zfs_dbgmsg("snapshotting ds %llu; currently traversing; "
955 "reset zb_objset to %llu",
956 (u_longlong_t
)ds
->ds_object
,
957 (u_longlong_t
)ds
->ds_phys
->ds_prev_snap_obj
);
958 } else if (zap_lookup_int_key(dp
->dp_meta_objset
,
959 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
, &mintxg
) == 0) {
960 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
961 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
, tx
));
962 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
963 scn
->scn_phys
.scn_queue_obj
,
964 ds
->ds_phys
->ds_prev_snap_obj
, mintxg
, tx
) == 0);
965 zfs_dbgmsg("snapshotting ds %llu; in queue; "
966 "replacing with %llu",
967 (u_longlong_t
)ds
->ds_object
,
968 (u_longlong_t
)ds
->ds_phys
->ds_prev_snap_obj
);
970 dsl_scan_sync_state(scn
, tx
);
974 dsl_scan_ds_clone_swapped(dsl_dataset_t
*ds1
, dsl_dataset_t
*ds2
, dmu_tx_t
*tx
)
976 dsl_pool_t
*dp
= ds1
->ds_dir
->dd_pool
;
977 dsl_scan_t
*scn
= dp
->dp_scan
;
980 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
983 if (scn
->scn_phys
.scn_bookmark
.zb_objset
== ds1
->ds_object
) {
984 scn
->scn_phys
.scn_bookmark
.zb_objset
= ds2
->ds_object
;
985 zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
986 "reset zb_objset to %llu",
987 (u_longlong_t
)ds1
->ds_object
,
988 (u_longlong_t
)ds2
->ds_object
);
989 } else if (scn
->scn_phys
.scn_bookmark
.zb_objset
== ds2
->ds_object
) {
990 scn
->scn_phys
.scn_bookmark
.zb_objset
= ds1
->ds_object
;
991 zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
992 "reset zb_objset to %llu",
993 (u_longlong_t
)ds2
->ds_object
,
994 (u_longlong_t
)ds1
->ds_object
);
997 if (zap_lookup_int_key(dp
->dp_meta_objset
, scn
->scn_phys
.scn_queue_obj
,
998 ds1
->ds_object
, &mintxg
) == 0) {
1001 ASSERT3U(mintxg
, ==, ds1
->ds_phys
->ds_prev_snap_txg
);
1002 ASSERT3U(mintxg
, ==, ds2
->ds_phys
->ds_prev_snap_txg
);
1003 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
1004 scn
->scn_phys
.scn_queue_obj
, ds1
->ds_object
, tx
));
1005 err
= zap_add_int_key(dp
->dp_meta_objset
,
1006 scn
->scn_phys
.scn_queue_obj
, ds2
->ds_object
, mintxg
, tx
);
1007 VERIFY(err
== 0 || err
== EEXIST
);
1008 if (err
== EEXIST
) {
1009 /* Both were there to begin with */
1010 VERIFY(0 == zap_add_int_key(dp
->dp_meta_objset
,
1011 scn
->scn_phys
.scn_queue_obj
,
1012 ds1
->ds_object
, mintxg
, tx
));
1014 zfs_dbgmsg("clone_swap ds %llu; in queue; "
1015 "replacing with %llu",
1016 (u_longlong_t
)ds1
->ds_object
,
1017 (u_longlong_t
)ds2
->ds_object
);
1018 } else if (zap_lookup_int_key(dp
->dp_meta_objset
,
1019 scn
->scn_phys
.scn_queue_obj
, ds2
->ds_object
, &mintxg
) == 0) {
1020 ASSERT3U(mintxg
, ==, ds1
->ds_phys
->ds_prev_snap_txg
);
1021 ASSERT3U(mintxg
, ==, ds2
->ds_phys
->ds_prev_snap_txg
);
1022 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
1023 scn
->scn_phys
.scn_queue_obj
, ds2
->ds_object
, tx
));
1024 VERIFY(0 == zap_add_int_key(dp
->dp_meta_objset
,
1025 scn
->scn_phys
.scn_queue_obj
, ds1
->ds_object
, mintxg
, tx
));
1026 zfs_dbgmsg("clone_swap ds %llu; in queue; "
1027 "replacing with %llu",
1028 (u_longlong_t
)ds2
->ds_object
,
1029 (u_longlong_t
)ds1
->ds_object
);
1032 dsl_scan_sync_state(scn
, tx
);
1035 struct enqueue_clones_arg
{
1042 enqueue_clones_cb(spa_t
*spa
, uint64_t dsobj
, const char *dsname
, void *arg
)
1044 struct enqueue_clones_arg
*eca
= arg
;
1047 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
1048 dsl_scan_t
*scn
= dp
->dp_scan
;
1050 err
= dsl_dataset_hold_obj(dp
, dsobj
, FTAG
, &ds
);
1054 if (ds
->ds_dir
->dd_phys
->dd_origin_obj
== eca
->originobj
) {
1055 while (ds
->ds_phys
->ds_prev_snap_obj
!= eca
->originobj
) {
1056 dsl_dataset_t
*prev
;
1057 err
= dsl_dataset_hold_obj(dp
,
1058 ds
->ds_phys
->ds_prev_snap_obj
, FTAG
, &prev
);
1060 dsl_dataset_rele(ds
, FTAG
);
1065 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
1066 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
,
1067 ds
->ds_phys
->ds_prev_snap_txg
, eca
->tx
) == 0);
1069 dsl_dataset_rele(ds
, FTAG
);
1074 dsl_scan_visitds(dsl_scan_t
*scn
, uint64_t dsobj
, dmu_tx_t
*tx
)
1076 dsl_pool_t
*dp
= scn
->scn_dp
;
1081 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp
, dsobj
, FTAG
, &ds
));
1083 if (dmu_objset_from_ds(ds
, &os
))
1087 * Only the ZIL in the head (non-snapshot) is valid. Even though
1088 * snapshots can have ZIL block pointers (which may be the same
1089 * BP as in the head), they must be ignored. So we traverse the
1090 * ZIL here, rather than in scan_recurse(), because the regular
1091 * snapshot block-sharing rules don't apply to it.
1093 if (DSL_SCAN_IS_SCRUB_RESILVER(scn
) && !dsl_dataset_is_snapshot(ds
))
1094 dsl_scan_zil(dp
, &os
->os_zil_header
);
1097 * Iterate over the bps in this ds.
1099 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
1100 dsl_scan_visit_rootbp(scn
, ds
, &ds
->ds_phys
->ds_bp
, tx
);
1102 dsname
= kmem_alloc(ZFS_MAXNAMELEN
, KM_SLEEP
);
1103 dsl_dataset_name(ds
, dsname
);
1104 zfs_dbgmsg("scanned dataset %llu (%s) with min=%llu max=%llu; "
1106 (longlong_t
)dsobj
, dsname
,
1107 (longlong_t
)scn
->scn_phys
.scn_cur_min_txg
,
1108 (longlong_t
)scn
->scn_phys
.scn_cur_max_txg
,
1109 (int)scn
->scn_pausing
);
1110 kmem_free(dsname
, ZFS_MAXNAMELEN
);
1112 if (scn
->scn_pausing
)
1116 * We've finished this pass over this dataset.
1120 * If we did not completely visit this dataset, do another pass.
1122 if (scn
->scn_phys
.scn_flags
& DSF_VISIT_DS_AGAIN
) {
1123 zfs_dbgmsg("incomplete pass; visiting again");
1124 scn
->scn_phys
.scn_flags
&= ~DSF_VISIT_DS_AGAIN
;
1125 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
1126 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
,
1127 scn
->scn_phys
.scn_cur_max_txg
, tx
) == 0);
1132 * Add descendent datasets to work queue.
1134 if (ds
->ds_phys
->ds_next_snap_obj
!= 0) {
1135 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
1136 scn
->scn_phys
.scn_queue_obj
, ds
->ds_phys
->ds_next_snap_obj
,
1137 ds
->ds_phys
->ds_creation_txg
, tx
) == 0);
1139 if (ds
->ds_phys
->ds_num_children
> 1) {
1140 boolean_t usenext
= B_FALSE
;
1141 if (ds
->ds_phys
->ds_next_clones_obj
!= 0) {
1144 * A bug in a previous version of the code could
1145 * cause upgrade_clones_cb() to not set
1146 * ds_next_snap_obj when it should, leading to a
1147 * missing entry. Therefore we can only use the
1148 * next_clones_obj when its count is correct.
1150 int err
= zap_count(dp
->dp_meta_objset
,
1151 ds
->ds_phys
->ds_next_clones_obj
, &count
);
1153 count
== ds
->ds_phys
->ds_num_children
- 1)
1158 VERIFY(zap_join_key(dp
->dp_meta_objset
,
1159 ds
->ds_phys
->ds_next_clones_obj
,
1160 scn
->scn_phys
.scn_queue_obj
,
1161 ds
->ds_phys
->ds_creation_txg
, tx
) == 0);
1163 struct enqueue_clones_arg eca
;
1165 eca
.originobj
= ds
->ds_object
;
1167 (void) dmu_objset_find_spa(ds
->ds_dir
->dd_pool
->dp_spa
,
1168 NULL
, enqueue_clones_cb
, &eca
, DS_FIND_CHILDREN
);
1173 dsl_dataset_rele(ds
, FTAG
);
1178 enqueue_cb(spa_t
*spa
, uint64_t dsobj
, const char *dsname
, void *arg
)
1183 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
1184 dsl_scan_t
*scn
= dp
->dp_scan
;
1186 err
= dsl_dataset_hold_obj(dp
, dsobj
, FTAG
, &ds
);
1190 while (ds
->ds_phys
->ds_prev_snap_obj
!= 0) {
1191 dsl_dataset_t
*prev
;
1192 err
= dsl_dataset_hold_obj(dp
, ds
->ds_phys
->ds_prev_snap_obj
,
1195 dsl_dataset_rele(ds
, FTAG
);
1200 * If this is a clone, we don't need to worry about it for now.
1202 if (prev
->ds_phys
->ds_next_snap_obj
!= ds
->ds_object
) {
1203 dsl_dataset_rele(ds
, FTAG
);
1204 dsl_dataset_rele(prev
, FTAG
);
1207 dsl_dataset_rele(ds
, FTAG
);
1211 VERIFY(zap_add_int_key(dp
->dp_meta_objset
, scn
->scn_phys
.scn_queue_obj
,
1212 ds
->ds_object
, ds
->ds_phys
->ds_prev_snap_txg
, tx
) == 0);
1213 dsl_dataset_rele(ds
, FTAG
);
1218 * Scrub/dedup interaction.
1220 * If there are N references to a deduped block, we don't want to scrub it
1221 * N times -- ideally, we should scrub it exactly once.
1223 * We leverage the fact that the dde's replication class (enum ddt_class)
1224 * is ordered from highest replication class (DDT_CLASS_DITTO) to lowest
1225 * (DDT_CLASS_UNIQUE) so that we may walk the DDT in that order.
1227 * To prevent excess scrubbing, the scrub begins by walking the DDT
1228 * to find all blocks with refcnt > 1, and scrubs each of these once.
1229 * Since there are two replication classes which contain blocks with
1230 * refcnt > 1, we scrub the highest replication class (DDT_CLASS_DITTO) first.
1231 * Finally the top-down scrub begins, only visiting blocks with refcnt == 1.
1233 * There would be nothing more to say if a block's refcnt couldn't change
1234 * during a scrub, but of course it can so we must account for changes
1235 * in a block's replication class.
1237 * Here's an example of what can occur:
1239 * If a block has refcnt > 1 during the DDT scrub phase, but has refcnt == 1
1240 * when visited during the top-down scrub phase, it will be scrubbed twice.
1241 * This negates our scrub optimization, but is otherwise harmless.
1243 * If a block has refcnt == 1 during the DDT scrub phase, but has refcnt > 1
1244 * on each visit during the top-down scrub phase, it will never be scrubbed.
1245 * To catch this, ddt_sync_entry() notifies the scrub code whenever a block's
1246 * reference class transitions to a higher level (i.e DDT_CLASS_UNIQUE to
1247 * DDT_CLASS_DUPLICATE); if it transitions from refcnt == 1 to refcnt > 1
1248 * while a scrub is in progress, it scrubs the block right then.
1251 dsl_scan_ddt(dsl_scan_t
*scn
, dmu_tx_t
*tx
)
1253 ddt_bookmark_t
*ddb
= &scn
->scn_phys
.scn_ddt_bookmark
;
1254 ddt_entry_t dde
= { 0 };
1258 while ((error
= ddt_walk(scn
->scn_dp
->dp_spa
, ddb
, &dde
)) == 0) {
1261 if (ddb
->ddb_class
> scn
->scn_phys
.scn_ddt_class_max
)
1263 dprintf("visiting ddb=%llu/%llu/%llu/%llx\n",
1264 (longlong_t
)ddb
->ddb_class
,
1265 (longlong_t
)ddb
->ddb_type
,
1266 (longlong_t
)ddb
->ddb_checksum
,
1267 (longlong_t
)ddb
->ddb_cursor
);
1269 /* There should be no pending changes to the dedup table */
1270 ddt
= scn
->scn_dp
->dp_spa
->spa_ddt
[ddb
->ddb_checksum
];
1271 ASSERT(avl_first(&ddt
->ddt_tree
) == NULL
);
1273 dsl_scan_ddt_entry(scn
, ddb
->ddb_checksum
, &dde
, tx
);
1276 if (dsl_scan_check_pause(scn
, NULL
))
1280 zfs_dbgmsg("scanned %llu ddt entries with class_max = %u; pausing=%u",
1281 (longlong_t
)n
, (int)scn
->scn_phys
.scn_ddt_class_max
,
1282 (int)scn
->scn_pausing
);
1284 ASSERT(error
== 0 || error
== ENOENT
);
1285 ASSERT(error
!= ENOENT
||
1286 ddb
->ddb_class
> scn
->scn_phys
.scn_ddt_class_max
);
1291 dsl_scan_ddt_entry(dsl_scan_t
*scn
, enum zio_checksum checksum
,
1292 ddt_entry_t
*dde
, dmu_tx_t
*tx
)
1294 const ddt_key_t
*ddk
= &dde
->dde_key
;
1295 ddt_phys_t
*ddp
= dde
->dde_phys
;
1297 zbookmark_t zb
= { 0 };
1300 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
1303 for (p
= 0; p
< DDT_PHYS_TYPES
; p
++, ddp
++) {
1304 if (ddp
->ddp_phys_birth
== 0 ||
1305 ddp
->ddp_phys_birth
> scn
->scn_phys
.scn_cur_max_txg
)
1307 ddt_bp_create(checksum
, ddk
, ddp
, &bp
);
1309 scn
->scn_visited_this_txg
++;
1310 scan_funcs
[scn
->scn_phys
.scn_func
](scn
->scn_dp
, &bp
, &zb
);
1315 dsl_scan_visit(dsl_scan_t
*scn
, dmu_tx_t
*tx
)
1317 dsl_pool_t
*dp
= scn
->scn_dp
;
1321 if (scn
->scn_phys
.scn_ddt_bookmark
.ddb_class
<=
1322 scn
->scn_phys
.scn_ddt_class_max
) {
1323 scn
->scn_phys
.scn_cur_min_txg
= scn
->scn_phys
.scn_min_txg
;
1324 scn
->scn_phys
.scn_cur_max_txg
= scn
->scn_phys
.scn_max_txg
;
1325 dsl_scan_ddt(scn
, tx
);
1326 if (scn
->scn_pausing
)
1330 if (scn
->scn_phys
.scn_bookmark
.zb_objset
== DMU_META_OBJSET
) {
1331 /* First do the MOS & ORIGIN */
1333 scn
->scn_phys
.scn_cur_min_txg
= scn
->scn_phys
.scn_min_txg
;
1334 scn
->scn_phys
.scn_cur_max_txg
= scn
->scn_phys
.scn_max_txg
;
1335 dsl_scan_visit_rootbp(scn
, NULL
,
1336 &dp
->dp_meta_rootbp
, tx
);
1337 spa_set_rootblkptr(dp
->dp_spa
, &dp
->dp_meta_rootbp
);
1338 if (scn
->scn_pausing
)
1341 if (spa_version(dp
->dp_spa
) < SPA_VERSION_DSL_SCRUB
) {
1342 VERIFY(0 == dmu_objset_find_spa(dp
->dp_spa
,
1343 NULL
, enqueue_cb
, tx
, DS_FIND_CHILDREN
));
1345 dsl_scan_visitds(scn
,
1346 dp
->dp_origin_snap
->ds_object
, tx
);
1348 ASSERT(!scn
->scn_pausing
);
1349 } else if (scn
->scn_phys
.scn_bookmark
.zb_objset
!=
1350 ZB_DESTROYED_OBJSET
) {
1352 * If we were paused, continue from here. Note if the
1353 * ds we were paused on was deleted, the zb_objset may
1354 * be -1, so we will skip this and find a new objset
1357 dsl_scan_visitds(scn
, scn
->scn_phys
.scn_bookmark
.zb_objset
, tx
);
1358 if (scn
->scn_pausing
)
1363 * In case we were paused right at the end of the ds, zero the
1364 * bookmark so we don't think that we're still trying to resume.
1366 bzero(&scn
->scn_phys
.scn_bookmark
, sizeof (zbookmark_t
));
1368 /* keep pulling things out of the zap-object-as-queue */
1369 while (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
1370 scn
->scn_phys
.scn_queue_obj
),
1371 zap_cursor_retrieve(&zc
, &za
) == 0) {
1375 dsobj
= strtonum(za
.za_name
, NULL
);
1376 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
1377 scn
->scn_phys
.scn_queue_obj
, dsobj
, tx
));
1379 /* Set up min/max txg */
1380 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp
, dsobj
, FTAG
, &ds
));
1381 if (za
.za_first_integer
!= 0) {
1382 scn
->scn_phys
.scn_cur_min_txg
=
1383 MAX(scn
->scn_phys
.scn_min_txg
,
1384 za
.za_first_integer
);
1386 scn
->scn_phys
.scn_cur_min_txg
=
1387 MAX(scn
->scn_phys
.scn_min_txg
,
1388 ds
->ds_phys
->ds_prev_snap_txg
);
1390 scn
->scn_phys
.scn_cur_max_txg
= dsl_scan_ds_maxtxg(ds
);
1391 dsl_dataset_rele(ds
, FTAG
);
1393 dsl_scan_visitds(scn
, dsobj
, tx
);
1394 zap_cursor_fini(&zc
);
1395 if (scn
->scn_pausing
)
1398 zap_cursor_fini(&zc
);
1402 dsl_scan_free_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
1404 dsl_scan_t
*scn
= arg
;
1405 uint64_t elapsed_nanosecs
;
1407 elapsed_nanosecs
= gethrtime() - scn
->scn_sync_start_time
;
1409 if (elapsed_nanosecs
/ NANOSEC
> zfs_txg_timeout
||
1410 (elapsed_nanosecs
/ MICROSEC
> zfs_free_min_time_ms
&&
1411 txg_sync_waiting(scn
->scn_dp
)) ||
1412 spa_shutting_down(scn
->scn_dp
->dp_spa
))
1415 zio_nowait(zio_free_sync(scn
->scn_zio_root
, scn
->scn_dp
->dp_spa
,
1416 dmu_tx_get_txg(tx
), bp
, 0));
1417 dsl_dir_diduse_space(tx
->tx_pool
->dp_free_dir
, DD_USED_HEAD
,
1418 -bp_get_dsize_sync(scn
->scn_dp
->dp_spa
, bp
),
1419 -BP_GET_PSIZE(bp
), -BP_GET_UCSIZE(bp
), tx
);
1420 scn
->scn_visited_this_txg
++;
1425 dsl_scan_active(dsl_scan_t
*scn
)
1427 spa_t
*spa
= scn
->scn_dp
->dp_spa
;
1428 uint64_t used
= 0, comp
, uncomp
;
1430 if (spa
->spa_load_state
!= SPA_LOAD_NONE
)
1432 if (spa_shutting_down(spa
))
1435 if (scn
->scn_phys
.scn_state
== DSS_SCANNING
)
1438 if (spa_version(scn
->scn_dp
->dp_spa
) >= SPA_VERSION_DEADLISTS
) {
1439 (void) bpobj_space(&scn
->scn_dp
->dp_free_bpobj
,
1440 &used
, &comp
, &uncomp
);
1446 dsl_scan_sync(dsl_pool_t
*dp
, dmu_tx_t
*tx
)
1448 dsl_scan_t
*scn
= dp
->dp_scan
;
1449 spa_t
*spa
= dp
->dp_spa
;
1453 * Check for scn_restart_txg before checking spa_load_state, so
1454 * that we can restart an old-style scan while the pool is being
1455 * imported (see dsl_scan_init).
1457 if (scn
->scn_restart_txg
!= 0 &&
1458 scn
->scn_restart_txg
<= tx
->tx_txg
) {
1459 pool_scan_func_t func
= POOL_SCAN_SCRUB
;
1460 dsl_scan_done(scn
, B_FALSE
, tx
);
1461 if (vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
))
1462 func
= POOL_SCAN_RESILVER
;
1463 zfs_dbgmsg("restarting scan func=%u txg=%llu",
1465 dsl_scan_setup_sync(scn
, &func
, tx
);
1468 if (!dsl_scan_active(scn
) ||
1469 spa_sync_pass(dp
->dp_spa
) > 1)
1472 scn
->scn_visited_this_txg
= 0;
1473 scn
->scn_pausing
= B_FALSE
;
1474 scn
->scn_sync_start_time
= gethrtime();
1475 spa
->spa_scrub_active
= B_TRUE
;
1478 * First process the free list. If we pause the free, don't do
1479 * any scanning. This ensures that there is no free list when
1480 * we are scanning, so the scan code doesn't have to worry about
1483 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_DEADLISTS
) {
1484 scn
->scn_zio_root
= zio_root(dp
->dp_spa
, NULL
,
1485 NULL
, ZIO_FLAG_MUSTSUCCEED
);
1486 err
= bpobj_iterate(&dp
->dp_free_bpobj
,
1487 dsl_scan_free_cb
, scn
, tx
);
1488 VERIFY3U(0, ==, zio_wait(scn
->scn_zio_root
));
1489 if (scn
->scn_visited_this_txg
) {
1490 zfs_dbgmsg("freed %llu blocks in %llums from "
1491 "free_bpobj txg %llu",
1492 (longlong_t
)scn
->scn_visited_this_txg
,
1494 (gethrtime() - scn
->scn_sync_start_time
) / MICROSEC
,
1495 (longlong_t
)tx
->tx_txg
);
1496 scn
->scn_visited_this_txg
= 0;
1498 * Re-sync the ddt so that we can further modify
1499 * it when doing bprewrite.
1501 ddt_sync(spa
, tx
->tx_txg
);
1503 if (err
== ERESTART
)
1507 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
1510 if (scn
->scn_phys
.scn_ddt_bookmark
.ddb_class
<=
1511 scn
->scn_phys
.scn_ddt_class_max
) {
1512 zfs_dbgmsg("doing scan sync txg %llu; "
1513 "ddt bm=%llu/%llu/%llu/%llx",
1514 (longlong_t
)tx
->tx_txg
,
1515 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_class
,
1516 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_type
,
1517 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_checksum
,
1518 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_cursor
);
1519 ASSERT(scn
->scn_phys
.scn_bookmark
.zb_objset
== 0);
1520 ASSERT(scn
->scn_phys
.scn_bookmark
.zb_object
== 0);
1521 ASSERT(scn
->scn_phys
.scn_bookmark
.zb_level
== 0);
1522 ASSERT(scn
->scn_phys
.scn_bookmark
.zb_blkid
== 0);
1524 zfs_dbgmsg("doing scan sync txg %llu; bm=%llu/%llu/%llu/%llu",
1525 (longlong_t
)tx
->tx_txg
,
1526 (longlong_t
)scn
->scn_phys
.scn_bookmark
.zb_objset
,
1527 (longlong_t
)scn
->scn_phys
.scn_bookmark
.zb_object
,
1528 (longlong_t
)scn
->scn_phys
.scn_bookmark
.zb_level
,
1529 (longlong_t
)scn
->scn_phys
.scn_bookmark
.zb_blkid
);
1532 scn
->scn_zio_root
= zio_root(dp
->dp_spa
, NULL
,
1533 NULL
, ZIO_FLAG_CANFAIL
);
1534 dsl_scan_visit(scn
, tx
);
1535 (void) zio_wait(scn
->scn_zio_root
);
1536 scn
->scn_zio_root
= NULL
;
1538 zfs_dbgmsg("visited %llu blocks in %llums",
1539 (longlong_t
)scn
->scn_visited_this_txg
,
1540 (longlong_t
)(gethrtime() - scn
->scn_sync_start_time
) / MICROSEC
);
1542 if (!scn
->scn_pausing
) {
1543 /* finished with scan. */
1544 zfs_dbgmsg("finished scan txg %llu", (longlong_t
)tx
->tx_txg
);
1545 dsl_scan_done(scn
, B_TRUE
, tx
);
1548 if (DSL_SCAN_IS_SCRUB_RESILVER(scn
)) {
1549 mutex_enter(&spa
->spa_scrub_lock
);
1550 while (spa
->spa_scrub_inflight
> 0) {
1551 cv_wait(&spa
->spa_scrub_io_cv
,
1552 &spa
->spa_scrub_lock
);
1554 mutex_exit(&spa
->spa_scrub_lock
);
1557 dsl_scan_sync_state(scn
, tx
);
1561 * This will start a new scan, or restart an existing one.
1564 dsl_resilver_restart(dsl_pool_t
*dp
, uint64_t txg
)
1568 tx
= dmu_tx_create_dd(dp
->dp_mos_dir
);
1569 VERIFY(0 == dmu_tx_assign(tx
, TXG_WAIT
));
1571 txg
= dmu_tx_get_txg(tx
);
1572 dp
->dp_scan
->scn_restart_txg
= txg
;
1575 dp
->dp_scan
->scn_restart_txg
= txg
;
1577 zfs_dbgmsg("restarting resilver txg=%llu", txg
);
1581 dsl_scan_resilvering(dsl_pool_t
*dp
)
1583 return (dp
->dp_scan
->scn_phys
.scn_state
== DSS_SCANNING
&&
1584 dp
->dp_scan
->scn_phys
.scn_func
== POOL_SCAN_RESILVER
);
1592 count_block(zfs_all_blkstats_t
*zab
, const blkptr_t
*bp
)
1597 * If we resume after a reboot, zab will be NULL; don't record
1598 * incomplete stats in that case.
1603 for (i
= 0; i
< 4; i
++) {
1604 int l
= (i
< 2) ? BP_GET_LEVEL(bp
) : DN_MAX_LEVELS
;
1605 int t
= (i
& 1) ? BP_GET_TYPE(bp
) : DMU_OT_TOTAL
;
1606 zfs_blkstat_t
*zb
= &zab
->zab_type
[l
][t
];
1610 zb
->zb_asize
+= BP_GET_ASIZE(bp
);
1611 zb
->zb_lsize
+= BP_GET_LSIZE(bp
);
1612 zb
->zb_psize
+= BP_GET_PSIZE(bp
);
1613 zb
->zb_gangs
+= BP_COUNT_GANG(bp
);
1615 switch (BP_GET_NDVAS(bp
)) {
1617 if (DVA_GET_VDEV(&bp
->blk_dva
[0]) ==
1618 DVA_GET_VDEV(&bp
->blk_dva
[1]))
1619 zb
->zb_ditto_2_of_2_samevdev
++;
1622 equal
= (DVA_GET_VDEV(&bp
->blk_dva
[0]) ==
1623 DVA_GET_VDEV(&bp
->blk_dva
[1])) +
1624 (DVA_GET_VDEV(&bp
->blk_dva
[0]) ==
1625 DVA_GET_VDEV(&bp
->blk_dva
[2])) +
1626 (DVA_GET_VDEV(&bp
->blk_dva
[1]) ==
1627 DVA_GET_VDEV(&bp
->blk_dva
[2]));
1629 zb
->zb_ditto_2_of_3_samevdev
++;
1630 else if (equal
== 3)
1631 zb
->zb_ditto_3_of_3_samevdev
++;
1638 dsl_scan_scrub_done(zio_t
*zio
)
1640 spa_t
*spa
= zio
->io_spa
;
1642 zio_data_buf_free(zio
->io_data
, zio
->io_size
);
1644 mutex_enter(&spa
->spa_scrub_lock
);
1645 spa
->spa_scrub_inflight
--;
1646 cv_broadcast(&spa
->spa_scrub_io_cv
);
1648 if (zio
->io_error
&& (zio
->io_error
!= ECKSUM
||
1649 !(zio
->io_flags
& ZIO_FLAG_SPECULATIVE
))) {
1650 spa
->spa_dsl_pool
->dp_scan
->scn_phys
.scn_errors
++;
1652 mutex_exit(&spa
->spa_scrub_lock
);
1656 dsl_scan_scrub_cb(dsl_pool_t
*dp
,
1657 const blkptr_t
*bp
, const zbookmark_t
*zb
)
1659 dsl_scan_t
*scn
= dp
->dp_scan
;
1660 size_t size
= BP_GET_PSIZE(bp
);
1661 spa_t
*spa
= dp
->dp_spa
;
1662 uint64_t phys_birth
= BP_PHYSICAL_BIRTH(bp
);
1663 boolean_t needs_io
= B_FALSE
;
1664 int zio_flags
= ZIO_FLAG_SCAN_THREAD
| ZIO_FLAG_RAW
| ZIO_FLAG_CANFAIL
;
1665 int zio_priority
= 0;
1669 if (phys_birth
<= scn
->scn_phys
.scn_min_txg
||
1670 phys_birth
>= scn
->scn_phys
.scn_max_txg
)
1673 count_block(dp
->dp_blkstats
, bp
);
1675 ASSERT(DSL_SCAN_IS_SCRUB_RESILVER(scn
));
1676 if (scn
->scn_phys
.scn_func
== POOL_SCAN_SCRUB
) {
1677 zio_flags
|= ZIO_FLAG_SCRUB
;
1678 zio_priority
= ZIO_PRIORITY_SCRUB
;
1680 scan_delay
= zfs_scrub_delay
;
1681 } else if (scn
->scn_phys
.scn_func
== POOL_SCAN_RESILVER
) {
1682 zio_flags
|= ZIO_FLAG_RESILVER
;
1683 zio_priority
= ZIO_PRIORITY_RESILVER
;
1685 scan_delay
= zfs_resilver_delay
;
1688 /* If it's an intent log block, failure is expected. */
1689 if (zb
->zb_level
== ZB_ZIL_LEVEL
)
1690 zio_flags
|= ZIO_FLAG_SPECULATIVE
;
1692 for (d
= 0; d
< BP_GET_NDVAS(bp
); d
++) {
1693 vdev_t
*vd
= vdev_lookup_top(spa
,
1694 DVA_GET_VDEV(&bp
->blk_dva
[d
]));
1697 * Keep track of how much data we've examined so that
1698 * zpool(1M) status can make useful progress reports.
1700 scn
->scn_phys
.scn_examined
+= DVA_GET_ASIZE(&bp
->blk_dva
[d
]);
1701 spa
->spa_scan_pass_exam
+= DVA_GET_ASIZE(&bp
->blk_dva
[d
]);
1703 /* if it's a resilver, this may not be in the target range */
1705 if (DVA_GET_GANG(&bp
->blk_dva
[d
])) {
1707 * Gang members may be spread across multiple
1708 * vdevs, so the best estimate we have is the
1709 * scrub range, which has already been checked.
1710 * XXX -- it would be better to change our
1711 * allocation policy to ensure that all
1712 * gang members reside on the same vdev.
1716 needs_io
= vdev_dtl_contains(vd
, DTL_PARTIAL
,
1722 if (needs_io
&& !zfs_no_scrub_io
) {
1723 vdev_t
*rvd
= spa
->spa_root_vdev
;
1724 uint64_t maxinflight
= rvd
->vdev_children
* zfs_top_maxinflight
;
1725 void *data
= zio_data_buf_alloc(size
);
1727 mutex_enter(&spa
->spa_scrub_lock
);
1728 while (spa
->spa_scrub_inflight
>= maxinflight
)
1729 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
1730 spa
->spa_scrub_inflight
++;
1731 mutex_exit(&spa
->spa_scrub_lock
);
1734 * If we're seeing recent (zfs_scan_idle) "important" I/Os
1735 * then throttle our workload to limit the impact of a scan.
1737 if (ddi_get_lbolt64() - spa
->spa_last_io
<= zfs_scan_idle
)
1740 zio_nowait(zio_read(NULL
, spa
, bp
, data
, size
,
1741 dsl_scan_scrub_done
, NULL
, zio_priority
,
1745 /* do not relocate this block */
1750 dsl_scan(dsl_pool_t
*dp
, pool_scan_func_t func
)
1752 spa_t
*spa
= dp
->dp_spa
;
1755 * Purge all vdev caches and probe all devices. We do this here
1756 * rather than in sync context because this requires a writer lock
1757 * on the spa_config lock, which we can't do from sync context. The
1758 * spa_scrub_reopen flag indicates that vdev_open() should not
1759 * attempt to start another scrub.
1761 spa_vdev_state_enter(spa
, SCL_NONE
);
1762 spa
->spa_scrub_reopen
= B_TRUE
;
1763 vdev_reopen(spa
->spa_root_vdev
);
1764 spa
->spa_scrub_reopen
= B_FALSE
;
1765 (void) spa_vdev_state_exit(spa
, NULL
, 0);
1767 return (dsl_sync_task_do(dp
, dsl_scan_setup_check
,
1768 dsl_scan_setup_sync
, dp
->dp_scan
, &func
, 0));