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) 2011, 2015 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
*,
54 const zbookmark_phys_t
*);
56 static scan_cb_t dsl_scan_scrub_cb
;
57 static void dsl_scan_cancel_sync(void *, dmu_tx_t
*);
58 static void dsl_scan_sync_state(dsl_scan_t
*, dmu_tx_t
*tx
);
60 int zfs_top_maxinflight
= 32; /* maximum I/Os per top-level */
61 int zfs_resilver_delay
= 2; /* number of ticks to delay resilver */
62 int zfs_scrub_delay
= 4; /* number of ticks to delay scrub */
63 int zfs_scan_idle
= 50; /* idle window in clock ticks */
65 int zfs_scan_min_time_ms
= 1000; /* min millisecs to scrub per txg */
66 int zfs_free_min_time_ms
= 1000; /* min millisecs to free per txg */
67 int zfs_resilver_min_time_ms
= 3000; /* min millisecs to resilver per txg */
68 int zfs_no_scrub_io
= B_FALSE
; /* set to disable scrub i/o */
69 int zfs_no_scrub_prefetch
= B_FALSE
; /* set to disable scrub prefetch */
70 enum ddt_class zfs_scrub_ddt_class_max
= DDT_CLASS_DUPLICATE
;
71 int dsl_scan_delay_completion
= B_FALSE
; /* set to delay scan completion */
72 /* max number of blocks to free in a single TXG */
73 ulong zfs_free_max_blocks
= 100000;
75 #define DSL_SCAN_IS_SCRUB_RESILVER(scn) \
76 ((scn)->scn_phys.scn_func == POOL_SCAN_SCRUB || \
77 (scn)->scn_phys.scn_func == POOL_SCAN_RESILVER)
80 * Enable/disable the processing of the free_bpobj object.
82 int zfs_free_bpobj_enabled
= 1;
84 /* the order has to match pool_scan_type */
85 static scan_cb_t
*scan_funcs
[POOL_SCAN_FUNCS
] = {
87 dsl_scan_scrub_cb
, /* POOL_SCAN_SCRUB */
88 dsl_scan_scrub_cb
, /* POOL_SCAN_RESILVER */
92 dsl_scan_init(dsl_pool_t
*dp
, uint64_t txg
)
96 spa_t
*spa
= dp
->dp_spa
;
99 scn
= dp
->dp_scan
= kmem_zalloc(sizeof (dsl_scan_t
), KM_SLEEP
);
103 * It's possible that we're resuming a scan after a reboot so
104 * make sure that the scan_async_destroying flag is initialized
107 ASSERT(!scn
->scn_async_destroying
);
108 scn
->scn_async_destroying
= spa_feature_is_active(dp
->dp_spa
,
109 SPA_FEATURE_ASYNC_DESTROY
);
111 err
= zap_lookup(dp
->dp_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
112 "scrub_func", sizeof (uint64_t), 1, &f
);
115 * There was an old-style scrub in progress. Restart a
116 * new-style scrub from the beginning.
118 scn
->scn_restart_txg
= txg
;
119 zfs_dbgmsg("old-style scrub was in progress; "
120 "restarting new-style scrub in txg %llu",
121 scn
->scn_restart_txg
);
124 * Load the queue obj from the old location so that it
125 * can be freed by dsl_scan_done().
127 (void) zap_lookup(dp
->dp_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
128 "scrub_queue", sizeof (uint64_t), 1,
129 &scn
->scn_phys
.scn_queue_obj
);
131 err
= zap_lookup(dp
->dp_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
132 DMU_POOL_SCAN
, sizeof (uint64_t), SCAN_PHYS_NUMINTS
,
135 * Detect if the pool contains the signature of #2094. If it
136 * does properly update the scn->scn_phys structure and notify
137 * the administrator by setting an errata for the pool.
139 if (err
== EOVERFLOW
) {
140 uint64_t zaptmp
[SCAN_PHYS_NUMINTS
+ 1];
141 VERIFY3S(SCAN_PHYS_NUMINTS
, ==, 24);
142 VERIFY3S(offsetof(dsl_scan_phys_t
, scn_flags
), ==,
143 (23 * sizeof (uint64_t)));
145 err
= zap_lookup(dp
->dp_meta_objset
,
146 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SCAN
,
147 sizeof (uint64_t), SCAN_PHYS_NUMINTS
+ 1, &zaptmp
);
149 uint64_t overflow
= zaptmp
[SCAN_PHYS_NUMINTS
];
151 if (overflow
& ~DSL_SCAN_FLAGS_MASK
||
152 scn
->scn_async_destroying
) {
154 ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY
;
158 bcopy(zaptmp
, &scn
->scn_phys
,
159 SCAN_PHYS_NUMINTS
* sizeof (uint64_t));
160 scn
->scn_phys
.scn_flags
= overflow
;
162 /* Required scrub already in progress. */
163 if (scn
->scn_phys
.scn_state
== DSS_FINISHED
||
164 scn
->scn_phys
.scn_state
== DSS_CANCELED
)
166 ZPOOL_ERRATA_ZOL_2094_SCRUB
;
175 if (scn
->scn_phys
.scn_state
== DSS_SCANNING
&&
176 spa_prev_software_version(dp
->dp_spa
) < SPA_VERSION_SCAN
) {
178 * A new-type scrub was in progress on an old
179 * pool, and the pool was accessed by old
180 * software. Restart from the beginning, since
181 * the old software may have changed the pool in
184 scn
->scn_restart_txg
= txg
;
185 zfs_dbgmsg("new-style scrub was modified "
186 "by old software; restarting in txg %llu",
187 scn
->scn_restart_txg
);
191 spa_scan_stat_init(spa
);
196 dsl_scan_fini(dsl_pool_t
*dp
)
199 kmem_free(dp
->dp_scan
, sizeof (dsl_scan_t
));
206 dsl_scan_setup_check(void *arg
, dmu_tx_t
*tx
)
208 dsl_scan_t
*scn
= dmu_tx_pool(tx
)->dp_scan
;
210 if (scn
->scn_phys
.scn_state
== DSS_SCANNING
)
211 return (SET_ERROR(EBUSY
));
217 dsl_scan_setup_sync(void *arg
, dmu_tx_t
*tx
)
219 dsl_scan_t
*scn
= dmu_tx_pool(tx
)->dp_scan
;
220 pool_scan_func_t
*funcp
= arg
;
221 dmu_object_type_t ot
= 0;
222 dsl_pool_t
*dp
= scn
->scn_dp
;
223 spa_t
*spa
= dp
->dp_spa
;
225 ASSERT(scn
->scn_phys
.scn_state
!= DSS_SCANNING
);
226 ASSERT(*funcp
> POOL_SCAN_NONE
&& *funcp
< POOL_SCAN_FUNCS
);
227 bzero(&scn
->scn_phys
, sizeof (scn
->scn_phys
));
228 scn
->scn_phys
.scn_func
= *funcp
;
229 scn
->scn_phys
.scn_state
= DSS_SCANNING
;
230 scn
->scn_phys
.scn_min_txg
= 0;
231 scn
->scn_phys
.scn_max_txg
= tx
->tx_txg
;
232 scn
->scn_phys
.scn_ddt_class_max
= DDT_CLASSES
- 1; /* the entire DDT */
233 scn
->scn_phys
.scn_start_time
= gethrestime_sec();
234 scn
->scn_phys
.scn_errors
= 0;
235 scn
->scn_phys
.scn_to_examine
= spa
->spa_root_vdev
->vdev_stat
.vs_alloc
;
236 scn
->scn_restart_txg
= 0;
237 scn
->scn_done_txg
= 0;
238 spa_scan_stat_init(spa
);
240 if (DSL_SCAN_IS_SCRUB_RESILVER(scn
)) {
241 scn
->scn_phys
.scn_ddt_class_max
= zfs_scrub_ddt_class_max
;
243 /* rewrite all disk labels */
244 vdev_config_dirty(spa
->spa_root_vdev
);
246 if (vdev_resilver_needed(spa
->spa_root_vdev
,
247 &scn
->scn_phys
.scn_min_txg
, &scn
->scn_phys
.scn_max_txg
)) {
248 spa_event_notify(spa
, NULL
,
249 FM_EREPORT_ZFS_RESILVER_START
);
251 spa_event_notify(spa
, NULL
,
252 FM_EREPORT_ZFS_SCRUB_START
);
255 spa
->spa_scrub_started
= B_TRUE
;
257 * If this is an incremental scrub, limit the DDT scrub phase
258 * to just the auto-ditto class (for correctness); the rest
259 * of the scrub should go faster using top-down pruning.
261 if (scn
->scn_phys
.scn_min_txg
> TXG_INITIAL
)
262 scn
->scn_phys
.scn_ddt_class_max
= DDT_CLASS_DITTO
;
266 /* back to the generic stuff */
268 if (dp
->dp_blkstats
== NULL
) {
270 vmem_alloc(sizeof (zfs_all_blkstats_t
), KM_SLEEP
);
272 bzero(dp
->dp_blkstats
, sizeof (zfs_all_blkstats_t
));
274 if (spa_version(spa
) < SPA_VERSION_DSL_SCRUB
)
275 ot
= DMU_OT_ZAP_OTHER
;
277 scn
->scn_phys
.scn_queue_obj
= zap_create(dp
->dp_meta_objset
,
278 ot
? ot
: DMU_OT_SCAN_QUEUE
, DMU_OT_NONE
, 0, tx
);
280 dsl_scan_sync_state(scn
, tx
);
282 spa_history_log_internal(spa
, "scan setup", tx
,
283 "func=%u mintxg=%llu maxtxg=%llu",
284 *funcp
, scn
->scn_phys
.scn_min_txg
, scn
->scn_phys
.scn_max_txg
);
289 dsl_scan_done(dsl_scan_t
*scn
, boolean_t complete
, dmu_tx_t
*tx
)
291 static const char *old_names
[] = {
293 "scrub_ddt_bookmark",
294 "scrub_ddt_class_max",
303 dsl_pool_t
*dp
= scn
->scn_dp
;
304 spa_t
*spa
= dp
->dp_spa
;
307 /* Remove any remnants of an old-style scrub. */
308 for (i
= 0; old_names
[i
]; i
++) {
309 (void) zap_remove(dp
->dp_meta_objset
,
310 DMU_POOL_DIRECTORY_OBJECT
, old_names
[i
], tx
);
313 if (scn
->scn_phys
.scn_queue_obj
!= 0) {
314 VERIFY(0 == dmu_object_free(dp
->dp_meta_objset
,
315 scn
->scn_phys
.scn_queue_obj
, tx
));
316 scn
->scn_phys
.scn_queue_obj
= 0;
320 * If we were "restarted" from a stopped state, don't bother
321 * with anything else.
323 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
327 scn
->scn_phys
.scn_state
= DSS_FINISHED
;
329 scn
->scn_phys
.scn_state
= DSS_CANCELED
;
331 spa_history_log_internal(spa
, "scan done", tx
,
332 "complete=%u", complete
);
334 if (DSL_SCAN_IS_SCRUB_RESILVER(scn
)) {
335 mutex_enter(&spa
->spa_scrub_lock
);
336 while (spa
->spa_scrub_inflight
> 0) {
337 cv_wait(&spa
->spa_scrub_io_cv
,
338 &spa
->spa_scrub_lock
);
340 mutex_exit(&spa
->spa_scrub_lock
);
341 spa
->spa_scrub_started
= B_FALSE
;
342 spa
->spa_scrub_active
= B_FALSE
;
345 * If the scrub/resilver completed, update all DTLs to
346 * reflect this. Whether it succeeded or not, vacate
347 * all temporary scrub DTLs.
349 vdev_dtl_reassess(spa
->spa_root_vdev
, tx
->tx_txg
,
350 complete
? scn
->scn_phys
.scn_max_txg
: 0, B_TRUE
);
352 spa_event_notify(spa
, NULL
, scn
->scn_phys
.scn_min_txg
?
353 FM_EREPORT_ZFS_RESILVER_FINISH
:
354 FM_EREPORT_ZFS_SCRUB_FINISH
);
356 spa_errlog_rotate(spa
);
359 * We may have finished replacing a device.
360 * Let the async thread assess this and handle the detach.
362 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
365 scn
->scn_phys
.scn_end_time
= gethrestime_sec();
367 if (spa
->spa_errata
== ZPOOL_ERRATA_ZOL_2094_SCRUB
)
373 dsl_scan_cancel_check(void *arg
, dmu_tx_t
*tx
)
375 dsl_scan_t
*scn
= dmu_tx_pool(tx
)->dp_scan
;
377 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
378 return (SET_ERROR(ENOENT
));
384 dsl_scan_cancel_sync(void *arg
, dmu_tx_t
*tx
)
386 dsl_scan_t
*scn
= dmu_tx_pool(tx
)->dp_scan
;
388 dsl_scan_done(scn
, B_FALSE
, tx
);
389 dsl_scan_sync_state(scn
, tx
);
393 dsl_scan_cancel(dsl_pool_t
*dp
)
395 return (dsl_sync_task(spa_name(dp
->dp_spa
), dsl_scan_cancel_check
,
396 dsl_scan_cancel_sync
, NULL
, 3, ZFS_SPACE_CHECK_RESERVED
));
399 static void dsl_scan_visitbp(blkptr_t
*bp
, const zbookmark_phys_t
*zb
,
400 dnode_phys_t
*dnp
, dsl_dataset_t
*ds
, dsl_scan_t
*scn
,
401 dmu_objset_type_t ostype
, dmu_tx_t
*tx
);
402 inline __attribute__((always_inline
)) static void dsl_scan_visitdnode(
403 dsl_scan_t
*, dsl_dataset_t
*ds
, dmu_objset_type_t ostype
,
404 dnode_phys_t
*dnp
, uint64_t object
, dmu_tx_t
*tx
);
407 dsl_free(dsl_pool_t
*dp
, uint64_t txg
, const blkptr_t
*bp
)
409 zio_free(dp
->dp_spa
, txg
, bp
);
413 dsl_free_sync(zio_t
*pio
, dsl_pool_t
*dp
, uint64_t txg
, const blkptr_t
*bpp
)
415 ASSERT(dsl_pool_sync_context(dp
));
416 zio_nowait(zio_free_sync(pio
, dp
->dp_spa
, txg
, bpp
, pio
->io_flags
));
420 dsl_scan_ds_maxtxg(dsl_dataset_t
*ds
)
422 uint64_t smt
= ds
->ds_dir
->dd_pool
->dp_scan
->scn_phys
.scn_max_txg
;
423 if (ds
->ds_is_snapshot
)
424 return (MIN(smt
, dsl_dataset_phys(ds
)->ds_creation_txg
));
429 dsl_scan_sync_state(dsl_scan_t
*scn
, dmu_tx_t
*tx
)
431 VERIFY0(zap_update(scn
->scn_dp
->dp_meta_objset
,
432 DMU_POOL_DIRECTORY_OBJECT
,
433 DMU_POOL_SCAN
, sizeof (uint64_t), SCAN_PHYS_NUMINTS
,
434 &scn
->scn_phys
, tx
));
437 extern int zfs_vdev_async_write_active_min_dirty_percent
;
440 dsl_scan_check_pause(dsl_scan_t
*scn
, const zbookmark_phys_t
*zb
)
442 uint64_t elapsed_nanosecs
;
446 /* we never skip user/group accounting objects */
447 if (zb
&& (int64_t)zb
->zb_object
< 0)
450 if (scn
->scn_pausing
)
451 return (B_TRUE
); /* we're already pausing */
453 if (!ZB_IS_ZERO(&scn
->scn_phys
.scn_bookmark
))
454 return (B_FALSE
); /* we're resuming */
456 /* We only know how to resume from level-0 blocks. */
457 if (zb
&& zb
->zb_level
!= 0)
462 * - we have scanned for the maximum time: an entire txg
463 * timeout (default 5 sec)
465 * - we have scanned for at least the minimum time (default 1 sec
466 * for scrub, 3 sec for resilver), and either we have sufficient
467 * dirty data that we are starting to write more quickly
468 * (default 30%), or someone is explicitly waiting for this txg
471 * - the spa is shutting down because this pool is being exported
472 * or the machine is rebooting.
474 mintime
= (scn
->scn_phys
.scn_func
== POOL_SCAN_RESILVER
) ?
475 zfs_resilver_min_time_ms
: zfs_scan_min_time_ms
;
476 elapsed_nanosecs
= gethrtime() - scn
->scn_sync_start_time
;
477 dirty_pct
= scn
->scn_dp
->dp_dirty_total
* 100 / zfs_dirty_data_max
;
478 if (elapsed_nanosecs
/ NANOSEC
>= zfs_txg_timeout
||
479 (NSEC2MSEC(elapsed_nanosecs
) > mintime
&&
480 (txg_sync_waiting(scn
->scn_dp
) ||
481 dirty_pct
>= zfs_vdev_async_write_active_min_dirty_percent
)) ||
482 spa_shutting_down(scn
->scn_dp
->dp_spa
)) {
484 dprintf("pausing at bookmark %llx/%llx/%llx/%llx\n",
485 (longlong_t
)zb
->zb_objset
,
486 (longlong_t
)zb
->zb_object
,
487 (longlong_t
)zb
->zb_level
,
488 (longlong_t
)zb
->zb_blkid
);
489 scn
->scn_phys
.scn_bookmark
= *zb
;
491 dprintf("pausing at DDT bookmark %llx/%llx/%llx/%llx\n",
492 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_class
,
493 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_type
,
494 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_checksum
,
495 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_cursor
);
496 scn
->scn_pausing
= B_TRUE
;
502 typedef struct zil_scan_arg
{
504 zil_header_t
*zsa_zh
;
509 dsl_scan_zil_block(zilog_t
*zilog
, blkptr_t
*bp
, void *arg
, uint64_t claim_txg
)
511 zil_scan_arg_t
*zsa
= arg
;
512 dsl_pool_t
*dp
= zsa
->zsa_dp
;
513 dsl_scan_t
*scn
= dp
->dp_scan
;
514 zil_header_t
*zh
= zsa
->zsa_zh
;
517 if (BP_IS_HOLE(bp
) || bp
->blk_birth
<= scn
->scn_phys
.scn_cur_min_txg
)
521 * One block ("stubby") can be allocated a long time ago; we
522 * want to visit that one because it has been allocated
523 * (on-disk) even if it hasn't been claimed (even though for
524 * scrub there's nothing to do to it).
526 if (claim_txg
== 0 && bp
->blk_birth
>= spa_first_txg(dp
->dp_spa
))
529 SET_BOOKMARK(&zb
, zh
->zh_log
.blk_cksum
.zc_word
[ZIL_ZC_OBJSET
],
530 ZB_ZIL_OBJECT
, ZB_ZIL_LEVEL
, bp
->blk_cksum
.zc_word
[ZIL_ZC_SEQ
]);
532 VERIFY(0 == scan_funcs
[scn
->scn_phys
.scn_func
](dp
, bp
, &zb
));
538 dsl_scan_zil_record(zilog_t
*zilog
, lr_t
*lrc
, void *arg
, uint64_t claim_txg
)
540 if (lrc
->lrc_txtype
== TX_WRITE
) {
541 zil_scan_arg_t
*zsa
= arg
;
542 dsl_pool_t
*dp
= zsa
->zsa_dp
;
543 dsl_scan_t
*scn
= dp
->dp_scan
;
544 zil_header_t
*zh
= zsa
->zsa_zh
;
545 lr_write_t
*lr
= (lr_write_t
*)lrc
;
546 blkptr_t
*bp
= &lr
->lr_blkptr
;
549 if (BP_IS_HOLE(bp
) ||
550 bp
->blk_birth
<= scn
->scn_phys
.scn_cur_min_txg
)
554 * birth can be < claim_txg if this record's txg is
555 * already txg sync'ed (but this log block contains
556 * other records that are not synced)
558 if (claim_txg
== 0 || bp
->blk_birth
< claim_txg
)
561 SET_BOOKMARK(&zb
, zh
->zh_log
.blk_cksum
.zc_word
[ZIL_ZC_OBJSET
],
562 lr
->lr_foid
, ZB_ZIL_LEVEL
,
563 lr
->lr_offset
/ BP_GET_LSIZE(bp
));
565 VERIFY(0 == scan_funcs
[scn
->scn_phys
.scn_func
](dp
, bp
, &zb
));
571 dsl_scan_zil(dsl_pool_t
*dp
, zil_header_t
*zh
)
573 uint64_t claim_txg
= zh
->zh_claim_txg
;
574 zil_scan_arg_t zsa
= { dp
, zh
};
578 * We only want to visit blocks that have been claimed but not yet
579 * replayed (or, in read-only mode, blocks that *would* be claimed).
581 if (claim_txg
== 0 && spa_writeable(dp
->dp_spa
))
584 zilog
= zil_alloc(dp
->dp_meta_objset
, zh
);
586 (void) zil_parse(zilog
, dsl_scan_zil_block
, dsl_scan_zil_record
, &zsa
,
594 dsl_scan_prefetch(dsl_scan_t
*scn
, arc_buf_t
*buf
, blkptr_t
*bp
,
595 uint64_t objset
, uint64_t object
, uint64_t blkid
)
597 zbookmark_phys_t czb
;
598 arc_flags_t flags
= ARC_FLAG_NOWAIT
| ARC_FLAG_PREFETCH
;
600 if (zfs_no_scrub_prefetch
)
603 if (BP_IS_HOLE(bp
) || bp
->blk_birth
<= scn
->scn_phys
.scn_min_txg
||
604 (BP_GET_LEVEL(bp
) == 0 && BP_GET_TYPE(bp
) != DMU_OT_DNODE
))
607 SET_BOOKMARK(&czb
, objset
, object
, BP_GET_LEVEL(bp
), blkid
);
609 (void) arc_read(scn
->scn_zio_root
, scn
->scn_dp
->dp_spa
, bp
,
610 NULL
, NULL
, ZIO_PRIORITY_ASYNC_READ
,
611 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SCAN_THREAD
, &flags
, &czb
);
615 dsl_scan_check_resume(dsl_scan_t
*scn
, const dnode_phys_t
*dnp
,
616 const zbookmark_phys_t
*zb
)
619 * We never skip over user/group accounting objects (obj<0)
621 if (!ZB_IS_ZERO(&scn
->scn_phys
.scn_bookmark
) &&
622 (int64_t)zb
->zb_object
>= 0) {
624 * If we already visited this bp & everything below (in
625 * a prior txg sync), don't bother doing it again.
627 if (zbookmark_subtree_completed(dnp
, zb
,
628 &scn
->scn_phys
.scn_bookmark
))
632 * If we found the block we're trying to resume from, or
633 * we went past it to a different object, zero it out to
634 * indicate that it's OK to start checking for pausing
637 if (bcmp(zb
, &scn
->scn_phys
.scn_bookmark
, sizeof (*zb
)) == 0 ||
638 zb
->zb_object
> scn
->scn_phys
.scn_bookmark
.zb_object
) {
639 dprintf("resuming at %llx/%llx/%llx/%llx\n",
640 (longlong_t
)zb
->zb_objset
,
641 (longlong_t
)zb
->zb_object
,
642 (longlong_t
)zb
->zb_level
,
643 (longlong_t
)zb
->zb_blkid
);
644 bzero(&scn
->scn_phys
.scn_bookmark
, sizeof (*zb
));
651 * Return nonzero on i/o error.
652 * Return new buf to write out in *bufp.
654 inline __attribute__((always_inline
)) static int
655 dsl_scan_recurse(dsl_scan_t
*scn
, dsl_dataset_t
*ds
, dmu_objset_type_t ostype
,
656 dnode_phys_t
*dnp
, const blkptr_t
*bp
,
657 const zbookmark_phys_t
*zb
, dmu_tx_t
*tx
)
659 dsl_pool_t
*dp
= scn
->scn_dp
;
660 int zio_flags
= ZIO_FLAG_CANFAIL
| ZIO_FLAG_SCAN_THREAD
;
663 if (BP_GET_LEVEL(bp
) > 0) {
664 arc_flags_t flags
= ARC_FLAG_WAIT
;
667 int epb
= BP_GET_LSIZE(bp
) >> SPA_BLKPTRSHIFT
;
670 err
= arc_read(NULL
, dp
->dp_spa
, bp
, arc_getbuf_func
, &buf
,
671 ZIO_PRIORITY_ASYNC_READ
, zio_flags
, &flags
, zb
);
673 scn
->scn_phys
.scn_errors
++;
676 for (i
= 0, cbp
= buf
->b_data
; i
< epb
; i
++, cbp
++) {
677 dsl_scan_prefetch(scn
, buf
, cbp
, zb
->zb_objset
,
678 zb
->zb_object
, zb
->zb_blkid
* epb
+ i
);
680 for (i
= 0, cbp
= buf
->b_data
; i
< epb
; i
++, cbp
++) {
681 zbookmark_phys_t czb
;
683 SET_BOOKMARK(&czb
, zb
->zb_objset
, zb
->zb_object
,
685 zb
->zb_blkid
* epb
+ i
);
686 dsl_scan_visitbp(cbp
, &czb
, dnp
,
687 ds
, scn
, ostype
, tx
);
689 (void) arc_buf_remove_ref(buf
, &buf
);
690 } else if (BP_GET_TYPE(bp
) == DMU_OT_DNODE
) {
691 arc_flags_t flags
= ARC_FLAG_WAIT
;
694 int epb
= BP_GET_LSIZE(bp
) >> DNODE_SHIFT
;
697 err
= arc_read(NULL
, dp
->dp_spa
, bp
, arc_getbuf_func
, &buf
,
698 ZIO_PRIORITY_ASYNC_READ
, zio_flags
, &flags
, zb
);
700 scn
->scn_phys
.scn_errors
++;
703 for (i
= 0, cdnp
= buf
->b_data
; i
< epb
; i
++, cdnp
++) {
704 for (j
= 0; j
< cdnp
->dn_nblkptr
; j
++) {
705 blkptr_t
*cbp
= &cdnp
->dn_blkptr
[j
];
706 dsl_scan_prefetch(scn
, buf
, cbp
,
707 zb
->zb_objset
, zb
->zb_blkid
* epb
+ i
, j
);
710 for (i
= 0, cdnp
= buf
->b_data
; i
< epb
; i
++, cdnp
++) {
711 dsl_scan_visitdnode(scn
, ds
, ostype
,
712 cdnp
, zb
->zb_blkid
* epb
+ i
, tx
);
715 (void) arc_buf_remove_ref(buf
, &buf
);
716 } else if (BP_GET_TYPE(bp
) == DMU_OT_OBJSET
) {
717 arc_flags_t flags
= ARC_FLAG_WAIT
;
721 err
= arc_read(NULL
, dp
->dp_spa
, bp
, arc_getbuf_func
, &buf
,
722 ZIO_PRIORITY_ASYNC_READ
, zio_flags
, &flags
, zb
);
724 scn
->scn_phys
.scn_errors
++;
730 dsl_scan_visitdnode(scn
, ds
, osp
->os_type
,
731 &osp
->os_meta_dnode
, DMU_META_DNODE_OBJECT
, tx
);
733 if (OBJSET_BUF_HAS_USERUSED(buf
)) {
735 * We also always visit user/group accounting
736 * objects, and never skip them, even if we are
737 * pausing. This is necessary so that the space
738 * deltas from this txg get integrated.
740 dsl_scan_visitdnode(scn
, ds
, osp
->os_type
,
741 &osp
->os_groupused_dnode
,
742 DMU_GROUPUSED_OBJECT
, tx
);
743 dsl_scan_visitdnode(scn
, ds
, osp
->os_type
,
744 &osp
->os_userused_dnode
,
745 DMU_USERUSED_OBJECT
, tx
);
747 (void) arc_buf_remove_ref(buf
, &buf
);
753 inline __attribute__((always_inline
)) static void
754 dsl_scan_visitdnode(dsl_scan_t
*scn
, dsl_dataset_t
*ds
,
755 dmu_objset_type_t ostype
, dnode_phys_t
*dnp
,
756 uint64_t object
, dmu_tx_t
*tx
)
760 for (j
= 0; j
< dnp
->dn_nblkptr
; j
++) {
761 zbookmark_phys_t czb
;
763 SET_BOOKMARK(&czb
, ds
? ds
->ds_object
: 0, object
,
764 dnp
->dn_nlevels
- 1, j
);
765 dsl_scan_visitbp(&dnp
->dn_blkptr
[j
],
766 &czb
, dnp
, ds
, scn
, ostype
, tx
);
769 if (dnp
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
) {
770 zbookmark_phys_t czb
;
771 SET_BOOKMARK(&czb
, ds
? ds
->ds_object
: 0, object
,
773 dsl_scan_visitbp(&dnp
->dn_spill
,
774 &czb
, dnp
, ds
, scn
, ostype
, tx
);
779 * The arguments are in this order because mdb can only print the
780 * first 5; we want them to be useful.
783 dsl_scan_visitbp(blkptr_t
*bp
, const zbookmark_phys_t
*zb
,
784 dnode_phys_t
*dnp
, dsl_dataset_t
*ds
, dsl_scan_t
*scn
,
785 dmu_objset_type_t ostype
, dmu_tx_t
*tx
)
787 dsl_pool_t
*dp
= scn
->scn_dp
;
790 bp_toread
= kmem_alloc(sizeof (blkptr_t
), KM_SLEEP
);
793 /* ASSERT(pbuf == NULL || arc_released(pbuf)); */
795 if (dsl_scan_check_pause(scn
, zb
))
798 if (dsl_scan_check_resume(scn
, dnp
, zb
))
804 scn
->scn_visited_this_txg
++;
807 * This debugging is commented out to conserve stack space. This
808 * function is called recursively and the debugging addes several
809 * bytes to the stack for each call. It can be commented back in
810 * if required to debug an issue in dsl_scan_visitbp().
813 * "visiting ds=%p/%llu zb=%llx/%llx/%llx/%llx bp=%p",
814 * ds, ds ? ds->ds_object : 0,
815 * zb->zb_objset, zb->zb_object, zb->zb_level, zb->zb_blkid,
819 if (bp
->blk_birth
<= scn
->scn_phys
.scn_cur_min_txg
)
822 if (dsl_scan_recurse(scn
, ds
, ostype
, dnp
, bp_toread
, zb
, tx
) != 0)
826 * If dsl_scan_ddt() has aready visited this block, it will have
827 * already done any translations or scrubbing, so don't call the
830 if (ddt_class_contains(dp
->dp_spa
,
831 scn
->scn_phys
.scn_ddt_class_max
, bp
)) {
836 * If this block is from the future (after cur_max_txg), then we
837 * are doing this on behalf of a deleted snapshot, and we will
838 * revisit the future block on the next pass of this dataset.
839 * Don't scan it now unless we need to because something
840 * under it was modified.
842 if (BP_PHYSICAL_BIRTH(bp
) <= scn
->scn_phys
.scn_cur_max_txg
) {
843 scan_funcs
[scn
->scn_phys
.scn_func
](dp
, bp
, zb
);
846 kmem_free(bp_toread
, sizeof (blkptr_t
));
850 dsl_scan_visit_rootbp(dsl_scan_t
*scn
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
855 SET_BOOKMARK(&zb
, ds
? ds
->ds_object
: DMU_META_OBJSET
,
856 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
857 dsl_scan_visitbp(bp
, &zb
, NULL
,
858 ds
, scn
, DMU_OST_NONE
, tx
);
860 dprintf_ds(ds
, "finished scan%s", "");
864 dsl_scan_ds_destroyed(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
866 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
867 dsl_scan_t
*scn
= dp
->dp_scan
;
870 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
873 if (scn
->scn_phys
.scn_bookmark
.zb_objset
== ds
->ds_object
) {
874 if (ds
->ds_is_snapshot
) {
875 /* Note, scn_cur_{min,max}_txg stays the same. */
876 scn
->scn_phys
.scn_bookmark
.zb_objset
=
877 dsl_dataset_phys(ds
)->ds_next_snap_obj
;
878 zfs_dbgmsg("destroying ds %llu; currently traversing; "
879 "reset zb_objset to %llu",
880 (u_longlong_t
)ds
->ds_object
,
881 (u_longlong_t
)dsl_dataset_phys(ds
)->
883 scn
->scn_phys
.scn_flags
|= DSF_VISIT_DS_AGAIN
;
885 SET_BOOKMARK(&scn
->scn_phys
.scn_bookmark
,
886 ZB_DESTROYED_OBJSET
, 0, 0, 0);
887 zfs_dbgmsg("destroying ds %llu; currently traversing; "
888 "reset bookmark to -1,0,0,0",
889 (u_longlong_t
)ds
->ds_object
);
891 } else if (zap_lookup_int_key(dp
->dp_meta_objset
,
892 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
, &mintxg
) == 0) {
893 ASSERT3U(dsl_dataset_phys(ds
)->ds_num_children
, <=, 1);
894 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
895 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
, tx
));
896 if (ds
->ds_is_snapshot
) {
898 * We keep the same mintxg; it could be >
899 * ds_creation_txg if the previous snapshot was
902 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
903 scn
->scn_phys
.scn_queue_obj
,
904 dsl_dataset_phys(ds
)->ds_next_snap_obj
,
906 zfs_dbgmsg("destroying ds %llu; in queue; "
907 "replacing with %llu",
908 (u_longlong_t
)ds
->ds_object
,
909 (u_longlong_t
)dsl_dataset_phys(ds
)->
912 zfs_dbgmsg("destroying ds %llu; in queue; removing",
913 (u_longlong_t
)ds
->ds_object
);
916 zfs_dbgmsg("destroying ds %llu; ignoring",
917 (u_longlong_t
)ds
->ds_object
);
921 * dsl_scan_sync() should be called after this, and should sync
922 * out our changed state, but just to be safe, do it here.
924 dsl_scan_sync_state(scn
, tx
);
928 dsl_scan_ds_snapshotted(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
930 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
931 dsl_scan_t
*scn
= dp
->dp_scan
;
934 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
937 ASSERT(dsl_dataset_phys(ds
)->ds_prev_snap_obj
!= 0);
939 if (scn
->scn_phys
.scn_bookmark
.zb_objset
== ds
->ds_object
) {
940 scn
->scn_phys
.scn_bookmark
.zb_objset
=
941 dsl_dataset_phys(ds
)->ds_prev_snap_obj
;
942 zfs_dbgmsg("snapshotting ds %llu; currently traversing; "
943 "reset zb_objset to %llu",
944 (u_longlong_t
)ds
->ds_object
,
945 (u_longlong_t
)dsl_dataset_phys(ds
)->ds_prev_snap_obj
);
946 } else if (zap_lookup_int_key(dp
->dp_meta_objset
,
947 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
, &mintxg
) == 0) {
948 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
949 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
, tx
));
950 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
951 scn
->scn_phys
.scn_queue_obj
,
952 dsl_dataset_phys(ds
)->ds_prev_snap_obj
, mintxg
, tx
) == 0);
953 zfs_dbgmsg("snapshotting ds %llu; in queue; "
954 "replacing with %llu",
955 (u_longlong_t
)ds
->ds_object
,
956 (u_longlong_t
)dsl_dataset_phys(ds
)->ds_prev_snap_obj
);
958 dsl_scan_sync_state(scn
, tx
);
962 dsl_scan_ds_clone_swapped(dsl_dataset_t
*ds1
, dsl_dataset_t
*ds2
, dmu_tx_t
*tx
)
964 dsl_pool_t
*dp
= ds1
->ds_dir
->dd_pool
;
965 dsl_scan_t
*scn
= dp
->dp_scan
;
968 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
971 if (scn
->scn_phys
.scn_bookmark
.zb_objset
== ds1
->ds_object
) {
972 scn
->scn_phys
.scn_bookmark
.zb_objset
= ds2
->ds_object
;
973 zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
974 "reset zb_objset to %llu",
975 (u_longlong_t
)ds1
->ds_object
,
976 (u_longlong_t
)ds2
->ds_object
);
977 } else if (scn
->scn_phys
.scn_bookmark
.zb_objset
== ds2
->ds_object
) {
978 scn
->scn_phys
.scn_bookmark
.zb_objset
= ds1
->ds_object
;
979 zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
980 "reset zb_objset to %llu",
981 (u_longlong_t
)ds2
->ds_object
,
982 (u_longlong_t
)ds1
->ds_object
);
985 if (zap_lookup_int_key(dp
->dp_meta_objset
, scn
->scn_phys
.scn_queue_obj
,
986 ds1
->ds_object
, &mintxg
) == 0) {
989 ASSERT3U(mintxg
, ==, dsl_dataset_phys(ds1
)->ds_prev_snap_txg
);
990 ASSERT3U(mintxg
, ==, dsl_dataset_phys(ds2
)->ds_prev_snap_txg
);
991 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
992 scn
->scn_phys
.scn_queue_obj
, ds1
->ds_object
, tx
));
993 err
= zap_add_int_key(dp
->dp_meta_objset
,
994 scn
->scn_phys
.scn_queue_obj
, ds2
->ds_object
, mintxg
, tx
);
995 VERIFY(err
== 0 || err
== EEXIST
);
997 /* Both were there to begin with */
998 VERIFY(0 == zap_add_int_key(dp
->dp_meta_objset
,
999 scn
->scn_phys
.scn_queue_obj
,
1000 ds1
->ds_object
, mintxg
, tx
));
1002 zfs_dbgmsg("clone_swap ds %llu; in queue; "
1003 "replacing with %llu",
1004 (u_longlong_t
)ds1
->ds_object
,
1005 (u_longlong_t
)ds2
->ds_object
);
1006 } else if (zap_lookup_int_key(dp
->dp_meta_objset
,
1007 scn
->scn_phys
.scn_queue_obj
, ds2
->ds_object
, &mintxg
) == 0) {
1008 ASSERT3U(mintxg
, ==, dsl_dataset_phys(ds1
)->ds_prev_snap_txg
);
1009 ASSERT3U(mintxg
, ==, dsl_dataset_phys(ds2
)->ds_prev_snap_txg
);
1010 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
1011 scn
->scn_phys
.scn_queue_obj
, ds2
->ds_object
, tx
));
1012 VERIFY(0 == zap_add_int_key(dp
->dp_meta_objset
,
1013 scn
->scn_phys
.scn_queue_obj
, ds1
->ds_object
, mintxg
, tx
));
1014 zfs_dbgmsg("clone_swap ds %llu; in queue; "
1015 "replacing with %llu",
1016 (u_longlong_t
)ds2
->ds_object
,
1017 (u_longlong_t
)ds1
->ds_object
);
1020 dsl_scan_sync_state(scn
, tx
);
1023 struct enqueue_clones_arg
{
1030 enqueue_clones_cb(dsl_pool_t
*dp
, dsl_dataset_t
*hds
, void *arg
)
1032 struct enqueue_clones_arg
*eca
= arg
;
1035 dsl_scan_t
*scn
= dp
->dp_scan
;
1037 if (dsl_dir_phys(hds
->ds_dir
)->dd_origin_obj
!= eca
->originobj
)
1040 err
= dsl_dataset_hold_obj(dp
, hds
->ds_object
, FTAG
, &ds
);
1044 while (dsl_dataset_phys(ds
)->ds_prev_snap_obj
!= eca
->originobj
) {
1045 dsl_dataset_t
*prev
;
1046 err
= dsl_dataset_hold_obj(dp
,
1047 dsl_dataset_phys(ds
)->ds_prev_snap_obj
, FTAG
, &prev
);
1049 dsl_dataset_rele(ds
, FTAG
);
1054 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
1055 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
,
1056 dsl_dataset_phys(ds
)->ds_prev_snap_txg
, eca
->tx
) == 0);
1057 dsl_dataset_rele(ds
, FTAG
);
1062 dsl_scan_visitds(dsl_scan_t
*scn
, uint64_t dsobj
, dmu_tx_t
*tx
)
1064 dsl_pool_t
*dp
= scn
->scn_dp
;
1069 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp
, dsobj
, FTAG
, &ds
));
1071 if (dmu_objset_from_ds(ds
, &os
))
1075 * Only the ZIL in the head (non-snapshot) is valid. Even though
1076 * snapshots can have ZIL block pointers (which may be the same
1077 * BP as in the head), they must be ignored. So we traverse the
1078 * ZIL here, rather than in scan_recurse(), because the regular
1079 * snapshot block-sharing rules don't apply to it.
1081 if (DSL_SCAN_IS_SCRUB_RESILVER(scn
) && !ds
->ds_is_snapshot
)
1082 dsl_scan_zil(dp
, &os
->os_zil_header
);
1085 * Iterate over the bps in this ds.
1087 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
1088 dsl_scan_visit_rootbp(scn
, ds
, &dsl_dataset_phys(ds
)->ds_bp
, tx
);
1090 dsname
= kmem_alloc(ZFS_MAXNAMELEN
, KM_SLEEP
);
1091 dsl_dataset_name(ds
, dsname
);
1092 zfs_dbgmsg("scanned dataset %llu (%s) with min=%llu max=%llu; "
1094 (longlong_t
)dsobj
, dsname
,
1095 (longlong_t
)scn
->scn_phys
.scn_cur_min_txg
,
1096 (longlong_t
)scn
->scn_phys
.scn_cur_max_txg
,
1097 (int)scn
->scn_pausing
);
1098 kmem_free(dsname
, ZFS_MAXNAMELEN
);
1100 if (scn
->scn_pausing
)
1104 * We've finished this pass over this dataset.
1108 * If we did not completely visit this dataset, do another pass.
1110 if (scn
->scn_phys
.scn_flags
& DSF_VISIT_DS_AGAIN
) {
1111 zfs_dbgmsg("incomplete pass; visiting again");
1112 scn
->scn_phys
.scn_flags
&= ~DSF_VISIT_DS_AGAIN
;
1113 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
1114 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
,
1115 scn
->scn_phys
.scn_cur_max_txg
, tx
) == 0);
1120 * Add descendent datasets to work queue.
1122 if (dsl_dataset_phys(ds
)->ds_next_snap_obj
!= 0) {
1123 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
1124 scn
->scn_phys
.scn_queue_obj
,
1125 dsl_dataset_phys(ds
)->ds_next_snap_obj
,
1126 dsl_dataset_phys(ds
)->ds_creation_txg
, tx
) == 0);
1128 if (dsl_dataset_phys(ds
)->ds_num_children
> 1) {
1129 boolean_t usenext
= B_FALSE
;
1130 if (dsl_dataset_phys(ds
)->ds_next_clones_obj
!= 0) {
1133 * A bug in a previous version of the code could
1134 * cause upgrade_clones_cb() to not set
1135 * ds_next_snap_obj when it should, leading to a
1136 * missing entry. Therefore we can only use the
1137 * next_clones_obj when its count is correct.
1139 int err
= zap_count(dp
->dp_meta_objset
,
1140 dsl_dataset_phys(ds
)->ds_next_clones_obj
, &count
);
1142 count
== dsl_dataset_phys(ds
)->ds_num_children
- 1)
1147 VERIFY0(zap_join_key(dp
->dp_meta_objset
,
1148 dsl_dataset_phys(ds
)->ds_next_clones_obj
,
1149 scn
->scn_phys
.scn_queue_obj
,
1150 dsl_dataset_phys(ds
)->ds_creation_txg
, tx
));
1152 struct enqueue_clones_arg eca
;
1154 eca
.originobj
= ds
->ds_object
;
1156 VERIFY0(dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
1157 enqueue_clones_cb
, &eca
, DS_FIND_CHILDREN
));
1162 dsl_dataset_rele(ds
, FTAG
);
1167 enqueue_cb(dsl_pool_t
*dp
, dsl_dataset_t
*hds
, void *arg
)
1172 dsl_scan_t
*scn
= dp
->dp_scan
;
1174 err
= dsl_dataset_hold_obj(dp
, hds
->ds_object
, FTAG
, &ds
);
1178 while (dsl_dataset_phys(ds
)->ds_prev_snap_obj
!= 0) {
1179 dsl_dataset_t
*prev
;
1180 err
= dsl_dataset_hold_obj(dp
,
1181 dsl_dataset_phys(ds
)->ds_prev_snap_obj
, FTAG
, &prev
);
1183 dsl_dataset_rele(ds
, FTAG
);
1188 * If this is a clone, we don't need to worry about it for now.
1190 if (dsl_dataset_phys(prev
)->ds_next_snap_obj
!= ds
->ds_object
) {
1191 dsl_dataset_rele(ds
, FTAG
);
1192 dsl_dataset_rele(prev
, FTAG
);
1195 dsl_dataset_rele(ds
, FTAG
);
1199 VERIFY(zap_add_int_key(dp
->dp_meta_objset
, scn
->scn_phys
.scn_queue_obj
,
1200 ds
->ds_object
, dsl_dataset_phys(ds
)->ds_prev_snap_txg
, tx
) == 0);
1201 dsl_dataset_rele(ds
, FTAG
);
1206 * Scrub/dedup interaction.
1208 * If there are N references to a deduped block, we don't want to scrub it
1209 * N times -- ideally, we should scrub it exactly once.
1211 * We leverage the fact that the dde's replication class (enum ddt_class)
1212 * is ordered from highest replication class (DDT_CLASS_DITTO) to lowest
1213 * (DDT_CLASS_UNIQUE) so that we may walk the DDT in that order.
1215 * To prevent excess scrubbing, the scrub begins by walking the DDT
1216 * to find all blocks with refcnt > 1, and scrubs each of these once.
1217 * Since there are two replication classes which contain blocks with
1218 * refcnt > 1, we scrub the highest replication class (DDT_CLASS_DITTO) first.
1219 * Finally the top-down scrub begins, only visiting blocks with refcnt == 1.
1221 * There would be nothing more to say if a block's refcnt couldn't change
1222 * during a scrub, but of course it can so we must account for changes
1223 * in a block's replication class.
1225 * Here's an example of what can occur:
1227 * If a block has refcnt > 1 during the DDT scrub phase, but has refcnt == 1
1228 * when visited during the top-down scrub phase, it will be scrubbed twice.
1229 * This negates our scrub optimization, but is otherwise harmless.
1231 * If a block has refcnt == 1 during the DDT scrub phase, but has refcnt > 1
1232 * on each visit during the top-down scrub phase, it will never be scrubbed.
1233 * To catch this, ddt_sync_entry() notifies the scrub code whenever a block's
1234 * reference class transitions to a higher level (i.e DDT_CLASS_UNIQUE to
1235 * DDT_CLASS_DUPLICATE); if it transitions from refcnt == 1 to refcnt > 1
1236 * while a scrub is in progress, it scrubs the block right then.
1239 dsl_scan_ddt(dsl_scan_t
*scn
, dmu_tx_t
*tx
)
1241 ddt_bookmark_t
*ddb
= &scn
->scn_phys
.scn_ddt_bookmark
;
1246 bzero(&dde
, sizeof (ddt_entry_t
));
1248 while ((error
= ddt_walk(scn
->scn_dp
->dp_spa
, ddb
, &dde
)) == 0) {
1251 if (ddb
->ddb_class
> scn
->scn_phys
.scn_ddt_class_max
)
1253 dprintf("visiting ddb=%llu/%llu/%llu/%llx\n",
1254 (longlong_t
)ddb
->ddb_class
,
1255 (longlong_t
)ddb
->ddb_type
,
1256 (longlong_t
)ddb
->ddb_checksum
,
1257 (longlong_t
)ddb
->ddb_cursor
);
1259 /* There should be no pending changes to the dedup table */
1260 ddt
= scn
->scn_dp
->dp_spa
->spa_ddt
[ddb
->ddb_checksum
];
1261 ASSERT(avl_first(&ddt
->ddt_tree
) == NULL
);
1263 dsl_scan_ddt_entry(scn
, ddb
->ddb_checksum
, &dde
, tx
);
1266 if (dsl_scan_check_pause(scn
, NULL
))
1270 zfs_dbgmsg("scanned %llu ddt entries with class_max = %u; pausing=%u",
1271 (longlong_t
)n
, (int)scn
->scn_phys
.scn_ddt_class_max
,
1272 (int)scn
->scn_pausing
);
1274 ASSERT(error
== 0 || error
== ENOENT
);
1275 ASSERT(error
!= ENOENT
||
1276 ddb
->ddb_class
> scn
->scn_phys
.scn_ddt_class_max
);
1281 dsl_scan_ddt_entry(dsl_scan_t
*scn
, enum zio_checksum checksum
,
1282 ddt_entry_t
*dde
, dmu_tx_t
*tx
)
1284 const ddt_key_t
*ddk
= &dde
->dde_key
;
1285 ddt_phys_t
*ddp
= dde
->dde_phys
;
1287 zbookmark_phys_t zb
= { 0 };
1290 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
1293 for (p
= 0; p
< DDT_PHYS_TYPES
; p
++, ddp
++) {
1294 if (ddp
->ddp_phys_birth
== 0 ||
1295 ddp
->ddp_phys_birth
> scn
->scn_phys
.scn_max_txg
)
1297 ddt_bp_create(checksum
, ddk
, ddp
, &bp
);
1299 scn
->scn_visited_this_txg
++;
1300 scan_funcs
[scn
->scn_phys
.scn_func
](scn
->scn_dp
, &bp
, &zb
);
1305 dsl_scan_visit(dsl_scan_t
*scn
, dmu_tx_t
*tx
)
1307 dsl_pool_t
*dp
= scn
->scn_dp
;
1309 zap_attribute_t
*za
;
1311 if (scn
->scn_phys
.scn_ddt_bookmark
.ddb_class
<=
1312 scn
->scn_phys
.scn_ddt_class_max
) {
1313 scn
->scn_phys
.scn_cur_min_txg
= scn
->scn_phys
.scn_min_txg
;
1314 scn
->scn_phys
.scn_cur_max_txg
= scn
->scn_phys
.scn_max_txg
;
1315 dsl_scan_ddt(scn
, tx
);
1316 if (scn
->scn_pausing
)
1320 if (scn
->scn_phys
.scn_bookmark
.zb_objset
== DMU_META_OBJSET
) {
1321 /* First do the MOS & ORIGIN */
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_visit_rootbp(scn
, NULL
,
1326 &dp
->dp_meta_rootbp
, tx
);
1327 spa_set_rootblkptr(dp
->dp_spa
, &dp
->dp_meta_rootbp
);
1328 if (scn
->scn_pausing
)
1331 if (spa_version(dp
->dp_spa
) < SPA_VERSION_DSL_SCRUB
) {
1332 VERIFY0(dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
1333 enqueue_cb
, tx
, DS_FIND_CHILDREN
));
1335 dsl_scan_visitds(scn
,
1336 dp
->dp_origin_snap
->ds_object
, tx
);
1338 ASSERT(!scn
->scn_pausing
);
1339 } else if (scn
->scn_phys
.scn_bookmark
.zb_objset
!=
1340 ZB_DESTROYED_OBJSET
) {
1342 * If we were paused, continue from here. Note if the
1343 * ds we were paused on was deleted, the zb_objset may
1344 * be -1, so we will skip this and find a new objset
1347 dsl_scan_visitds(scn
, scn
->scn_phys
.scn_bookmark
.zb_objset
, tx
);
1348 if (scn
->scn_pausing
)
1353 * In case we were paused right at the end of the ds, zero the
1354 * bookmark so we don't think that we're still trying to resume.
1356 bzero(&scn
->scn_phys
.scn_bookmark
, sizeof (zbookmark_phys_t
));
1357 zc
= kmem_alloc(sizeof (zap_cursor_t
), KM_SLEEP
);
1358 za
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
1360 /* keep pulling things out of the zap-object-as-queue */
1361 while (zap_cursor_init(zc
, dp
->dp_meta_objset
,
1362 scn
->scn_phys
.scn_queue_obj
),
1363 zap_cursor_retrieve(zc
, za
) == 0) {
1367 dsobj
= strtonum(za
->za_name
, NULL
);
1368 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
1369 scn
->scn_phys
.scn_queue_obj
, dsobj
, tx
));
1371 /* Set up min/max txg */
1372 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp
, dsobj
, FTAG
, &ds
));
1373 if (za
->za_first_integer
!= 0) {
1374 scn
->scn_phys
.scn_cur_min_txg
=
1375 MAX(scn
->scn_phys
.scn_min_txg
,
1376 za
->za_first_integer
);
1378 scn
->scn_phys
.scn_cur_min_txg
=
1379 MAX(scn
->scn_phys
.scn_min_txg
,
1380 dsl_dataset_phys(ds
)->ds_prev_snap_txg
);
1382 scn
->scn_phys
.scn_cur_max_txg
= dsl_scan_ds_maxtxg(ds
);
1383 dsl_dataset_rele(ds
, FTAG
);
1385 dsl_scan_visitds(scn
, dsobj
, tx
);
1386 zap_cursor_fini(zc
);
1387 if (scn
->scn_pausing
)
1390 zap_cursor_fini(zc
);
1392 kmem_free(za
, sizeof (zap_attribute_t
));
1393 kmem_free(zc
, sizeof (zap_cursor_t
));
1397 dsl_scan_free_should_pause(dsl_scan_t
*scn
)
1399 uint64_t elapsed_nanosecs
;
1404 if (scn
->scn_visited_this_txg
>= zfs_free_max_blocks
)
1407 elapsed_nanosecs
= gethrtime() - scn
->scn_sync_start_time
;
1408 return (elapsed_nanosecs
/ NANOSEC
> zfs_txg_timeout
||
1409 (NSEC2MSEC(elapsed_nanosecs
) > zfs_free_min_time_ms
&&
1410 txg_sync_waiting(scn
->scn_dp
)) ||
1411 spa_shutting_down(scn
->scn_dp
->dp_spa
));
1415 dsl_scan_free_block_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
1417 dsl_scan_t
*scn
= arg
;
1419 if (!scn
->scn_is_bptree
||
1420 (BP_GET_LEVEL(bp
) == 0 && BP_GET_TYPE(bp
) != DMU_OT_OBJSET
)) {
1421 if (dsl_scan_free_should_pause(scn
))
1422 return (SET_ERROR(ERESTART
));
1425 zio_nowait(zio_free_sync(scn
->scn_zio_root
, scn
->scn_dp
->dp_spa
,
1426 dmu_tx_get_txg(tx
), bp
, 0));
1427 dsl_dir_diduse_space(tx
->tx_pool
->dp_free_dir
, DD_USED_HEAD
,
1428 -bp_get_dsize_sync(scn
->scn_dp
->dp_spa
, bp
),
1429 -BP_GET_PSIZE(bp
), -BP_GET_UCSIZE(bp
), tx
);
1430 scn
->scn_visited_this_txg
++;
1435 dsl_scan_active(dsl_scan_t
*scn
)
1437 spa_t
*spa
= scn
->scn_dp
->dp_spa
;
1438 uint64_t used
= 0, comp
, uncomp
;
1440 if (spa
->spa_load_state
!= SPA_LOAD_NONE
)
1442 if (spa_shutting_down(spa
))
1444 if (scn
->scn_phys
.scn_state
== DSS_SCANNING
||
1445 (scn
->scn_async_destroying
&& !scn
->scn_async_stalled
))
1448 if (spa_version(scn
->scn_dp
->dp_spa
) >= SPA_VERSION_DEADLISTS
) {
1449 (void) bpobj_space(&scn
->scn_dp
->dp_free_bpobj
,
1450 &used
, &comp
, &uncomp
);
1456 dsl_scan_sync(dsl_pool_t
*dp
, dmu_tx_t
*tx
)
1458 dsl_scan_t
*scn
= dp
->dp_scan
;
1459 spa_t
*spa
= dp
->dp_spa
;
1463 * Check for scn_restart_txg before checking spa_load_state, so
1464 * that we can restart an old-style scan while the pool is being
1465 * imported (see dsl_scan_init).
1467 if (scn
->scn_restart_txg
!= 0 &&
1468 scn
->scn_restart_txg
<= tx
->tx_txg
) {
1469 pool_scan_func_t func
= POOL_SCAN_SCRUB
;
1470 dsl_scan_done(scn
, B_FALSE
, tx
);
1471 if (vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
))
1472 func
= POOL_SCAN_RESILVER
;
1473 zfs_dbgmsg("restarting scan func=%u txg=%llu",
1475 dsl_scan_setup_sync(&func
, tx
);
1479 * Only process scans in sync pass 1.
1481 if (spa_sync_pass(dp
->dp_spa
) > 1)
1485 * If the spa is shutting down, then stop scanning. This will
1486 * ensure that the scan does not dirty any new data during the
1489 if (spa_shutting_down(spa
))
1493 * If the scan is inactive due to a stalled async destroy, try again.
1495 if (!scn
->scn_async_stalled
&& !dsl_scan_active(scn
))
1498 scn
->scn_visited_this_txg
= 0;
1499 scn
->scn_pausing
= B_FALSE
;
1500 scn
->scn_sync_start_time
= gethrtime();
1501 spa
->spa_scrub_active
= B_TRUE
;
1504 * First process the async destroys. If we pause, don't do
1505 * any scrubbing or resilvering. This ensures that there are no
1506 * async destroys while we are scanning, so the scan code doesn't
1507 * have to worry about traversing it. It is also faster to free the
1508 * blocks than to scrub them.
1510 if (zfs_free_bpobj_enabled
&&
1511 spa_version(dp
->dp_spa
) >= SPA_VERSION_DEADLISTS
) {
1512 scn
->scn_is_bptree
= B_FALSE
;
1513 scn
->scn_zio_root
= zio_root(dp
->dp_spa
, NULL
,
1514 NULL
, ZIO_FLAG_MUSTSUCCEED
);
1515 err
= bpobj_iterate(&dp
->dp_free_bpobj
,
1516 dsl_scan_free_block_cb
, scn
, tx
);
1517 VERIFY3U(0, ==, zio_wait(scn
->scn_zio_root
));
1519 if (err
!= 0 && err
!= ERESTART
)
1520 zfs_panic_recover("error %u from bpobj_iterate()", err
);
1523 if (err
== 0 && spa_feature_is_active(spa
, SPA_FEATURE_ASYNC_DESTROY
)) {
1524 ASSERT(scn
->scn_async_destroying
);
1525 scn
->scn_is_bptree
= B_TRUE
;
1526 scn
->scn_zio_root
= zio_root(dp
->dp_spa
, NULL
,
1527 NULL
, ZIO_FLAG_MUSTSUCCEED
);
1528 err
= bptree_iterate(dp
->dp_meta_objset
,
1529 dp
->dp_bptree_obj
, B_TRUE
, dsl_scan_free_block_cb
, scn
, tx
);
1530 VERIFY0(zio_wait(scn
->scn_zio_root
));
1532 if (err
== EIO
|| err
== ECKSUM
) {
1534 } else if (err
!= 0 && err
!= ERESTART
) {
1535 zfs_panic_recover("error %u from "
1536 "traverse_dataset_destroyed()", err
);
1539 if (bptree_is_empty(dp
->dp_meta_objset
, dp
->dp_bptree_obj
)) {
1540 /* finished; deactivate async destroy feature */
1541 spa_feature_decr(spa
, SPA_FEATURE_ASYNC_DESTROY
, tx
);
1542 ASSERT(!spa_feature_is_active(spa
,
1543 SPA_FEATURE_ASYNC_DESTROY
));
1544 VERIFY0(zap_remove(dp
->dp_meta_objset
,
1545 DMU_POOL_DIRECTORY_OBJECT
,
1546 DMU_POOL_BPTREE_OBJ
, tx
));
1547 VERIFY0(bptree_free(dp
->dp_meta_objset
,
1548 dp
->dp_bptree_obj
, tx
));
1549 dp
->dp_bptree_obj
= 0;
1550 scn
->scn_async_destroying
= B_FALSE
;
1551 scn
->scn_async_stalled
= B_FALSE
;
1554 * If we didn't make progress, mark the async
1555 * destroy as stalled, so that we will not initiate
1556 * a spa_sync() on its behalf. Note that we only
1557 * check this if we are not finished, because if the
1558 * bptree had no blocks for us to visit, we can
1559 * finish without "making progress".
1561 scn
->scn_async_stalled
=
1562 (scn
->scn_visited_this_txg
== 0);
1565 if (scn
->scn_visited_this_txg
) {
1566 zfs_dbgmsg("freed %llu blocks in %llums from "
1567 "free_bpobj/bptree txg %llu; err=%u",
1568 (longlong_t
)scn
->scn_visited_this_txg
,
1570 NSEC2MSEC(gethrtime() - scn
->scn_sync_start_time
),
1571 (longlong_t
)tx
->tx_txg
, err
);
1572 scn
->scn_visited_this_txg
= 0;
1575 * Write out changes to the DDT that may be required as a
1576 * result of the blocks freed. This ensures that the DDT
1577 * is clean when a scrub/resilver runs.
1579 ddt_sync(spa
, tx
->tx_txg
);
1583 if (!scn
->scn_async_destroying
&& zfs_free_leak_on_eio
&&
1584 (dsl_dir_phys(dp
->dp_free_dir
)->dd_used_bytes
!= 0 ||
1585 dsl_dir_phys(dp
->dp_free_dir
)->dd_compressed_bytes
!= 0 ||
1586 dsl_dir_phys(dp
->dp_free_dir
)->dd_uncompressed_bytes
!= 0)) {
1588 * We have finished background destroying, but there is still
1589 * some space left in the dp_free_dir. Transfer this leaked
1590 * space to the dp_leak_dir.
1592 if (dp
->dp_leak_dir
== NULL
) {
1593 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
1594 (void) dsl_dir_create_sync(dp
, dp
->dp_root_dir
,
1596 VERIFY0(dsl_pool_open_special_dir(dp
,
1597 LEAK_DIR_NAME
, &dp
->dp_leak_dir
));
1598 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
1600 dsl_dir_diduse_space(dp
->dp_leak_dir
, DD_USED_HEAD
,
1601 dsl_dir_phys(dp
->dp_free_dir
)->dd_used_bytes
,
1602 dsl_dir_phys(dp
->dp_free_dir
)->dd_compressed_bytes
,
1603 dsl_dir_phys(dp
->dp_free_dir
)->dd_uncompressed_bytes
, tx
);
1604 dsl_dir_diduse_space(dp
->dp_free_dir
, DD_USED_HEAD
,
1605 -dsl_dir_phys(dp
->dp_free_dir
)->dd_used_bytes
,
1606 -dsl_dir_phys(dp
->dp_free_dir
)->dd_compressed_bytes
,
1607 -dsl_dir_phys(dp
->dp_free_dir
)->dd_uncompressed_bytes
, tx
);
1609 if (!scn
->scn_async_destroying
) {
1610 /* finished; verify that space accounting went to zero */
1611 ASSERT0(dsl_dir_phys(dp
->dp_free_dir
)->dd_used_bytes
);
1612 ASSERT0(dsl_dir_phys(dp
->dp_free_dir
)->dd_compressed_bytes
);
1613 ASSERT0(dsl_dir_phys(dp
->dp_free_dir
)->dd_uncompressed_bytes
);
1616 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
1619 if (scn
->scn_done_txg
== tx
->tx_txg
) {
1620 ASSERT(!scn
->scn_pausing
);
1621 /* finished with scan. */
1622 zfs_dbgmsg("txg %llu scan complete", tx
->tx_txg
);
1623 dsl_scan_done(scn
, B_TRUE
, tx
);
1624 ASSERT3U(spa
->spa_scrub_inflight
, ==, 0);
1625 dsl_scan_sync_state(scn
, tx
);
1629 if (scn
->scn_phys
.scn_ddt_bookmark
.ddb_class
<=
1630 scn
->scn_phys
.scn_ddt_class_max
) {
1631 zfs_dbgmsg("doing scan sync txg %llu; "
1632 "ddt bm=%llu/%llu/%llu/%llx",
1633 (longlong_t
)tx
->tx_txg
,
1634 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_class
,
1635 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_type
,
1636 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_checksum
,
1637 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_cursor
);
1638 ASSERT(scn
->scn_phys
.scn_bookmark
.zb_objset
== 0);
1639 ASSERT(scn
->scn_phys
.scn_bookmark
.zb_object
== 0);
1640 ASSERT(scn
->scn_phys
.scn_bookmark
.zb_level
== 0);
1641 ASSERT(scn
->scn_phys
.scn_bookmark
.zb_blkid
== 0);
1643 zfs_dbgmsg("doing scan sync txg %llu; bm=%llu/%llu/%llu/%llu",
1644 (longlong_t
)tx
->tx_txg
,
1645 (longlong_t
)scn
->scn_phys
.scn_bookmark
.zb_objset
,
1646 (longlong_t
)scn
->scn_phys
.scn_bookmark
.zb_object
,
1647 (longlong_t
)scn
->scn_phys
.scn_bookmark
.zb_level
,
1648 (longlong_t
)scn
->scn_phys
.scn_bookmark
.zb_blkid
);
1651 scn
->scn_zio_root
= zio_root(dp
->dp_spa
, NULL
,
1652 NULL
, ZIO_FLAG_CANFAIL
);
1653 dsl_pool_config_enter(dp
, FTAG
);
1654 dsl_scan_visit(scn
, tx
);
1655 dsl_pool_config_exit(dp
, FTAG
);
1656 (void) zio_wait(scn
->scn_zio_root
);
1657 scn
->scn_zio_root
= NULL
;
1659 zfs_dbgmsg("visited %llu blocks in %llums",
1660 (longlong_t
)scn
->scn_visited_this_txg
,
1661 (longlong_t
)NSEC2MSEC(gethrtime() - scn
->scn_sync_start_time
));
1663 if (!scn
->scn_pausing
) {
1664 scn
->scn_done_txg
= tx
->tx_txg
+ 1;
1665 zfs_dbgmsg("txg %llu traversal complete, waiting till txg %llu",
1666 tx
->tx_txg
, scn
->scn_done_txg
);
1669 if (DSL_SCAN_IS_SCRUB_RESILVER(scn
)) {
1670 mutex_enter(&spa
->spa_scrub_lock
);
1671 while (spa
->spa_scrub_inflight
> 0) {
1672 cv_wait(&spa
->spa_scrub_io_cv
,
1673 &spa
->spa_scrub_lock
);
1675 mutex_exit(&spa
->spa_scrub_lock
);
1678 dsl_scan_sync_state(scn
, tx
);
1682 * This will start a new scan, or restart an existing one.
1685 dsl_resilver_restart(dsl_pool_t
*dp
, uint64_t txg
)
1689 tx
= dmu_tx_create_dd(dp
->dp_mos_dir
);
1690 VERIFY(0 == dmu_tx_assign(tx
, TXG_WAIT
));
1692 txg
= dmu_tx_get_txg(tx
);
1693 dp
->dp_scan
->scn_restart_txg
= txg
;
1696 dp
->dp_scan
->scn_restart_txg
= txg
;
1698 zfs_dbgmsg("restarting resilver txg=%llu", txg
);
1702 dsl_scan_resilvering(dsl_pool_t
*dp
)
1704 return (dp
->dp_scan
->scn_phys
.scn_state
== DSS_SCANNING
&&
1705 dp
->dp_scan
->scn_phys
.scn_func
== POOL_SCAN_RESILVER
);
1713 count_block(zfs_all_blkstats_t
*zab
, const blkptr_t
*bp
)
1718 * If we resume after a reboot, zab will be NULL; don't record
1719 * incomplete stats in that case.
1724 for (i
= 0; i
< 4; i
++) {
1725 int l
= (i
< 2) ? BP_GET_LEVEL(bp
) : DN_MAX_LEVELS
;
1726 int t
= (i
& 1) ? BP_GET_TYPE(bp
) : DMU_OT_TOTAL
;
1730 if (t
& DMU_OT_NEWTYPE
)
1733 zb
= &zab
->zab_type
[l
][t
];
1735 zb
->zb_asize
+= BP_GET_ASIZE(bp
);
1736 zb
->zb_lsize
+= BP_GET_LSIZE(bp
);
1737 zb
->zb_psize
+= BP_GET_PSIZE(bp
);
1738 zb
->zb_gangs
+= BP_COUNT_GANG(bp
);
1740 switch (BP_GET_NDVAS(bp
)) {
1742 if (DVA_GET_VDEV(&bp
->blk_dva
[0]) ==
1743 DVA_GET_VDEV(&bp
->blk_dva
[1]))
1744 zb
->zb_ditto_2_of_2_samevdev
++;
1747 equal
= (DVA_GET_VDEV(&bp
->blk_dva
[0]) ==
1748 DVA_GET_VDEV(&bp
->blk_dva
[1])) +
1749 (DVA_GET_VDEV(&bp
->blk_dva
[0]) ==
1750 DVA_GET_VDEV(&bp
->blk_dva
[2])) +
1751 (DVA_GET_VDEV(&bp
->blk_dva
[1]) ==
1752 DVA_GET_VDEV(&bp
->blk_dva
[2]));
1754 zb
->zb_ditto_2_of_3_samevdev
++;
1755 else if (equal
== 3)
1756 zb
->zb_ditto_3_of_3_samevdev
++;
1763 dsl_scan_scrub_done(zio_t
*zio
)
1765 spa_t
*spa
= zio
->io_spa
;
1767 zio_data_buf_free(zio
->io_data
, zio
->io_size
);
1769 mutex_enter(&spa
->spa_scrub_lock
);
1770 spa
->spa_scrub_inflight
--;
1771 cv_broadcast(&spa
->spa_scrub_io_cv
);
1773 if (zio
->io_error
&& (zio
->io_error
!= ECKSUM
||
1774 !(zio
->io_flags
& ZIO_FLAG_SPECULATIVE
))) {
1775 spa
->spa_dsl_pool
->dp_scan
->scn_phys
.scn_errors
++;
1777 mutex_exit(&spa
->spa_scrub_lock
);
1781 dsl_scan_scrub_cb(dsl_pool_t
*dp
,
1782 const blkptr_t
*bp
, const zbookmark_phys_t
*zb
)
1784 dsl_scan_t
*scn
= dp
->dp_scan
;
1785 size_t size
= BP_GET_PSIZE(bp
);
1786 spa_t
*spa
= dp
->dp_spa
;
1787 uint64_t phys_birth
= BP_PHYSICAL_BIRTH(bp
);
1788 boolean_t needs_io
= B_FALSE
;
1789 int zio_flags
= ZIO_FLAG_SCAN_THREAD
| ZIO_FLAG_RAW
| ZIO_FLAG_CANFAIL
;
1793 if (phys_birth
<= scn
->scn_phys
.scn_min_txg
||
1794 phys_birth
>= scn
->scn_phys
.scn_max_txg
)
1797 count_block(dp
->dp_blkstats
, bp
);
1799 if (BP_IS_EMBEDDED(bp
))
1802 ASSERT(DSL_SCAN_IS_SCRUB_RESILVER(scn
));
1803 if (scn
->scn_phys
.scn_func
== POOL_SCAN_SCRUB
) {
1804 zio_flags
|= ZIO_FLAG_SCRUB
;
1806 scan_delay
= zfs_scrub_delay
;
1808 ASSERT3U(scn
->scn_phys
.scn_func
, ==, POOL_SCAN_RESILVER
);
1809 zio_flags
|= ZIO_FLAG_RESILVER
;
1811 scan_delay
= zfs_resilver_delay
;
1814 /* If it's an intent log block, failure is expected. */
1815 if (zb
->zb_level
== ZB_ZIL_LEVEL
)
1816 zio_flags
|= ZIO_FLAG_SPECULATIVE
;
1818 for (d
= 0; d
< BP_GET_NDVAS(bp
); d
++) {
1819 vdev_t
*vd
= vdev_lookup_top(spa
,
1820 DVA_GET_VDEV(&bp
->blk_dva
[d
]));
1823 * Keep track of how much data we've examined so that
1824 * zpool(1M) status can make useful progress reports.
1826 scn
->scn_phys
.scn_examined
+= DVA_GET_ASIZE(&bp
->blk_dva
[d
]);
1827 spa
->spa_scan_pass_exam
+= DVA_GET_ASIZE(&bp
->blk_dva
[d
]);
1829 /* if it's a resilver, this may not be in the target range */
1831 if (DVA_GET_GANG(&bp
->blk_dva
[d
])) {
1833 * Gang members may be spread across multiple
1834 * vdevs, so the best estimate we have is the
1835 * scrub range, which has already been checked.
1836 * XXX -- it would be better to change our
1837 * allocation policy to ensure that all
1838 * gang members reside on the same vdev.
1842 needs_io
= vdev_dtl_contains(vd
, DTL_PARTIAL
,
1848 if (needs_io
&& !zfs_no_scrub_io
) {
1849 vdev_t
*rvd
= spa
->spa_root_vdev
;
1850 uint64_t maxinflight
= rvd
->vdev_children
* zfs_top_maxinflight
;
1851 void *data
= zio_data_buf_alloc(size
);
1853 mutex_enter(&spa
->spa_scrub_lock
);
1854 while (spa
->spa_scrub_inflight
>= maxinflight
)
1855 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
1856 spa
->spa_scrub_inflight
++;
1857 mutex_exit(&spa
->spa_scrub_lock
);
1860 * If we're seeing recent (zfs_scan_idle) "important" I/Os
1861 * then throttle our workload to limit the impact of a scan.
1863 if (ddi_get_lbolt64() - spa
->spa_last_io
<= zfs_scan_idle
)
1866 zio_nowait(zio_read(NULL
, spa
, bp
, data
, size
,
1867 dsl_scan_scrub_done
, NULL
, ZIO_PRIORITY_SCRUB
,
1871 /* do not relocate this block */
1876 dsl_scan(dsl_pool_t
*dp
, pool_scan_func_t func
)
1878 spa_t
*spa
= dp
->dp_spa
;
1881 * Purge all vdev caches and probe all devices. We do this here
1882 * rather than in sync context because this requires a writer lock
1883 * on the spa_config lock, which we can't do from sync context. The
1884 * spa_scrub_reopen flag indicates that vdev_open() should not
1885 * attempt to start another scrub.
1887 spa_vdev_state_enter(spa
, SCL_NONE
);
1888 spa
->spa_scrub_reopen
= B_TRUE
;
1889 vdev_reopen(spa
->spa_root_vdev
);
1890 spa
->spa_scrub_reopen
= B_FALSE
;
1891 (void) spa_vdev_state_exit(spa
, NULL
, 0);
1893 return (dsl_sync_task(spa_name(spa
), dsl_scan_setup_check
,
1894 dsl_scan_setup_sync
, &func
, 0, ZFS_SPACE_CHECK_NONE
));
1897 #if defined(_KERNEL) && defined(HAVE_SPL)
1898 module_param(zfs_top_maxinflight
, int, 0644);
1899 MODULE_PARM_DESC(zfs_top_maxinflight
, "Max I/Os per top-level");
1901 module_param(zfs_resilver_delay
, int, 0644);
1902 MODULE_PARM_DESC(zfs_resilver_delay
, "Number of ticks to delay resilver");
1904 module_param(zfs_scrub_delay
, int, 0644);
1905 MODULE_PARM_DESC(zfs_scrub_delay
, "Number of ticks to delay scrub");
1907 module_param(zfs_scan_idle
, int, 0644);
1908 MODULE_PARM_DESC(zfs_scan_idle
, "Idle window in clock ticks");
1910 module_param(zfs_scan_min_time_ms
, int, 0644);
1911 MODULE_PARM_DESC(zfs_scan_min_time_ms
, "Min millisecs to scrub per txg");
1913 module_param(zfs_free_min_time_ms
, int, 0644);
1914 MODULE_PARM_DESC(zfs_free_min_time_ms
, "Min millisecs to free per txg");
1916 module_param(zfs_resilver_min_time_ms
, int, 0644);
1917 MODULE_PARM_DESC(zfs_resilver_min_time_ms
, "Min millisecs to resilver per txg");
1919 module_param(zfs_no_scrub_io
, int, 0644);
1920 MODULE_PARM_DESC(zfs_no_scrub_io
, "Set to disable scrub I/O");
1922 module_param(zfs_no_scrub_prefetch
, int, 0644);
1923 MODULE_PARM_DESC(zfs_no_scrub_prefetch
, "Set to disable scrub prefetching");
1925 module_param(zfs_free_max_blocks
, ulong
, 0644);
1926 MODULE_PARM_DESC(zfs_free_max_blocks
, "Max number of blocks freed in one txg");
1928 module_param(zfs_free_bpobj_enabled
, int, 0644);
1929 MODULE_PARM_DESC(zfs_free_bpobj_enabled
, "Enable processing of the free_bpobj");