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, 2016 by Delphix. All rights reserved.
24 * Copyright 2016 Gary Mills
25 * Copyright (c) 2017 Datto Inc.
26 * Copyright 2017 Joyent, Inc.
29 #include <sys/dsl_scan.h>
30 #include <sys/dsl_pool.h>
31 #include <sys/dsl_dataset.h>
32 #include <sys/dsl_prop.h>
33 #include <sys/dsl_dir.h>
34 #include <sys/dsl_synctask.h>
35 #include <sys/dnode.h>
36 #include <sys/dmu_tx.h>
37 #include <sys/dmu_objset.h>
41 #include <sys/zfs_context.h>
42 #include <sys/fs/zfs.h>
43 #include <sys/zfs_znode.h>
44 #include <sys/spa_impl.h>
45 #include <sys/vdev_impl.h>
46 #include <sys/zil_impl.h>
47 #include <sys/zio_checksum.h>
50 #include <sys/sa_impl.h>
51 #include <sys/zfeature.h>
54 #include <sys/zfs_vfsops.h>
57 typedef int (scan_cb_t
)(dsl_pool_t
*, const blkptr_t
*,
58 const zbookmark_phys_t
*);
60 static scan_cb_t dsl_scan_scrub_cb
;
61 static void dsl_scan_cancel_sync(void *, dmu_tx_t
*);
62 static void dsl_scan_sync_state(dsl_scan_t
*, dmu_tx_t
*);
63 static boolean_t
dsl_scan_restarting(dsl_scan_t
*, dmu_tx_t
*);
65 int zfs_top_maxinflight
= 32; /* maximum I/Os per top-level */
66 int zfs_resilver_delay
= 2; /* number of ticks to delay resilver */
67 int zfs_scrub_delay
= 4; /* number of ticks to delay scrub */
68 int zfs_scan_idle
= 50; /* idle window in clock ticks */
70 int zfs_scan_min_time_ms
= 1000; /* min millisecs to scrub per txg */
71 int zfs_free_min_time_ms
= 1000; /* min millisecs to free per txg */
72 int zfs_resilver_min_time_ms
= 3000; /* min millisecs to resilver per txg */
73 int zfs_no_scrub_io
= B_FALSE
; /* set to disable scrub i/o */
74 int zfs_no_scrub_prefetch
= B_FALSE
; /* set to disable scrub prefetch */
75 enum ddt_class zfs_scrub_ddt_class_max
= DDT_CLASS_DUPLICATE
;
76 int dsl_scan_delay_completion
= B_FALSE
; /* set to delay scan completion */
77 /* max number of blocks to free in a single TXG */
78 unsigned long zfs_free_max_blocks
= 100000;
80 #define DSL_SCAN_IS_SCRUB_RESILVER(scn) \
81 ((scn)->scn_phys.scn_func == POOL_SCAN_SCRUB || \
82 (scn)->scn_phys.scn_func == POOL_SCAN_RESILVER)
85 * Enable/disable the processing of the free_bpobj object.
87 int zfs_free_bpobj_enabled
= 1;
89 /* the order has to match pool_scan_type */
90 static scan_cb_t
*scan_funcs
[POOL_SCAN_FUNCS
] = {
92 dsl_scan_scrub_cb
, /* POOL_SCAN_SCRUB */
93 dsl_scan_scrub_cb
, /* POOL_SCAN_RESILVER */
97 dsl_scan_init(dsl_pool_t
*dp
, uint64_t txg
)
101 spa_t
*spa
= dp
->dp_spa
;
104 scn
= dp
->dp_scan
= kmem_zalloc(sizeof (dsl_scan_t
), KM_SLEEP
);
108 * It's possible that we're resuming a scan after a reboot so
109 * make sure that the scan_async_destroying flag is initialized
112 ASSERT(!scn
->scn_async_destroying
);
113 scn
->scn_async_destroying
= spa_feature_is_active(dp
->dp_spa
,
114 SPA_FEATURE_ASYNC_DESTROY
);
116 err
= zap_lookup(dp
->dp_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
117 "scrub_func", sizeof (uint64_t), 1, &f
);
120 * There was an old-style scrub in progress. Restart a
121 * new-style scrub from the beginning.
123 scn
->scn_restart_txg
= txg
;
124 zfs_dbgmsg("old-style scrub was in progress; "
125 "restarting new-style scrub in txg %llu",
126 scn
->scn_restart_txg
);
129 * Load the queue obj from the old location so that it
130 * can be freed by dsl_scan_done().
132 (void) zap_lookup(dp
->dp_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
133 "scrub_queue", sizeof (uint64_t), 1,
134 &scn
->scn_phys
.scn_queue_obj
);
136 err
= zap_lookup(dp
->dp_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
137 DMU_POOL_SCAN
, sizeof (uint64_t), SCAN_PHYS_NUMINTS
,
140 * Detect if the pool contains the signature of #2094. If it
141 * does properly update the scn->scn_phys structure and notify
142 * the administrator by setting an errata for the pool.
144 if (err
== EOVERFLOW
) {
145 uint64_t zaptmp
[SCAN_PHYS_NUMINTS
+ 1];
146 VERIFY3S(SCAN_PHYS_NUMINTS
, ==, 24);
147 VERIFY3S(offsetof(dsl_scan_phys_t
, scn_flags
), ==,
148 (23 * sizeof (uint64_t)));
150 err
= zap_lookup(dp
->dp_meta_objset
,
151 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SCAN
,
152 sizeof (uint64_t), SCAN_PHYS_NUMINTS
+ 1, &zaptmp
);
154 uint64_t overflow
= zaptmp
[SCAN_PHYS_NUMINTS
];
156 if (overflow
& ~DSL_SCAN_FLAGS_MASK
||
157 scn
->scn_async_destroying
) {
159 ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY
;
163 bcopy(zaptmp
, &scn
->scn_phys
,
164 SCAN_PHYS_NUMINTS
* sizeof (uint64_t));
165 scn
->scn_phys
.scn_flags
= overflow
;
167 /* Required scrub already in progress. */
168 if (scn
->scn_phys
.scn_state
== DSS_FINISHED
||
169 scn
->scn_phys
.scn_state
== DSS_CANCELED
)
171 ZPOOL_ERRATA_ZOL_2094_SCRUB
;
180 if (scn
->scn_phys
.scn_state
== DSS_SCANNING
&&
181 spa_prev_software_version(dp
->dp_spa
) < SPA_VERSION_SCAN
) {
183 * A new-type scrub was in progress on an old
184 * pool, and the pool was accessed by old
185 * software. Restart from the beginning, since
186 * the old software may have changed the pool in
189 scn
->scn_restart_txg
= txg
;
190 zfs_dbgmsg("new-style scrub was modified "
191 "by old software; restarting in txg %llu",
192 scn
->scn_restart_txg
);
196 spa_scan_stat_init(spa
);
201 dsl_scan_fini(dsl_pool_t
*dp
)
204 kmem_free(dp
->dp_scan
, sizeof (dsl_scan_t
));
211 dsl_scan_setup_check(void *arg
, dmu_tx_t
*tx
)
213 dsl_scan_t
*scn
= dmu_tx_pool(tx
)->dp_scan
;
215 if (scn
->scn_phys
.scn_state
== DSS_SCANNING
)
216 return (SET_ERROR(EBUSY
));
222 dsl_scan_setup_sync(void *arg
, dmu_tx_t
*tx
)
224 dsl_scan_t
*scn
= dmu_tx_pool(tx
)->dp_scan
;
225 pool_scan_func_t
*funcp
= arg
;
226 dmu_object_type_t ot
= 0;
227 dsl_pool_t
*dp
= scn
->scn_dp
;
228 spa_t
*spa
= dp
->dp_spa
;
230 ASSERT(scn
->scn_phys
.scn_state
!= DSS_SCANNING
);
231 ASSERT(*funcp
> POOL_SCAN_NONE
&& *funcp
< POOL_SCAN_FUNCS
);
232 bzero(&scn
->scn_phys
, sizeof (scn
->scn_phys
));
233 scn
->scn_phys
.scn_func
= *funcp
;
234 scn
->scn_phys
.scn_state
= DSS_SCANNING
;
235 scn
->scn_phys
.scn_min_txg
= 0;
236 scn
->scn_phys
.scn_max_txg
= tx
->tx_txg
;
237 scn
->scn_phys
.scn_ddt_class_max
= DDT_CLASSES
- 1; /* the entire DDT */
238 scn
->scn_phys
.scn_start_time
= gethrestime_sec();
239 scn
->scn_phys
.scn_errors
= 0;
240 scn
->scn_phys
.scn_to_examine
= spa
->spa_root_vdev
->vdev_stat
.vs_alloc
;
241 scn
->scn_restart_txg
= 0;
242 scn
->scn_done_txg
= 0;
243 spa_scan_stat_init(spa
);
245 if (DSL_SCAN_IS_SCRUB_RESILVER(scn
)) {
246 scn
->scn_phys
.scn_ddt_class_max
= zfs_scrub_ddt_class_max
;
248 /* rewrite all disk labels */
249 vdev_config_dirty(spa
->spa_root_vdev
);
251 if (vdev_resilver_needed(spa
->spa_root_vdev
,
252 &scn
->scn_phys
.scn_min_txg
, &scn
->scn_phys
.scn_max_txg
)) {
253 spa_event_notify(spa
, NULL
, NULL
,
254 ESC_ZFS_RESILVER_START
);
256 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_SCRUB_START
);
259 spa
->spa_scrub_started
= B_TRUE
;
261 * If this is an incremental scrub, limit the DDT scrub phase
262 * to just the auto-ditto class (for correctness); the rest
263 * of the scrub should go faster using top-down pruning.
265 if (scn
->scn_phys
.scn_min_txg
> TXG_INITIAL
)
266 scn
->scn_phys
.scn_ddt_class_max
= DDT_CLASS_DITTO
;
270 /* back to the generic stuff */
272 if (dp
->dp_blkstats
== NULL
) {
274 vmem_alloc(sizeof (zfs_all_blkstats_t
), KM_SLEEP
);
276 bzero(dp
->dp_blkstats
, sizeof (zfs_all_blkstats_t
));
278 if (spa_version(spa
) < SPA_VERSION_DSL_SCRUB
)
279 ot
= DMU_OT_ZAP_OTHER
;
281 scn
->scn_phys
.scn_queue_obj
= zap_create(dp
->dp_meta_objset
,
282 ot
? ot
: DMU_OT_SCAN_QUEUE
, DMU_OT_NONE
, 0, tx
);
284 dsl_scan_sync_state(scn
, tx
);
286 spa_history_log_internal(spa
, "scan setup", tx
,
287 "func=%u mintxg=%llu maxtxg=%llu",
288 *funcp
, scn
->scn_phys
.scn_min_txg
, scn
->scn_phys
.scn_max_txg
);
293 dsl_scan_done(dsl_scan_t
*scn
, boolean_t complete
, dmu_tx_t
*tx
)
295 static const char *old_names
[] = {
297 "scrub_ddt_bookmark",
298 "scrub_ddt_class_max",
307 dsl_pool_t
*dp
= scn
->scn_dp
;
308 spa_t
*spa
= dp
->dp_spa
;
311 /* Remove any remnants of an old-style scrub. */
312 for (i
= 0; old_names
[i
]; i
++) {
313 (void) zap_remove(dp
->dp_meta_objset
,
314 DMU_POOL_DIRECTORY_OBJECT
, old_names
[i
], tx
);
317 if (scn
->scn_phys
.scn_queue_obj
!= 0) {
318 VERIFY(0 == dmu_object_free(dp
->dp_meta_objset
,
319 scn
->scn_phys
.scn_queue_obj
, tx
));
320 scn
->scn_phys
.scn_queue_obj
= 0;
323 scn
->scn_phys
.scn_flags
&= ~DSF_SCRUB_PAUSED
;
326 * If we were "restarted" from a stopped state, don't bother
327 * with anything else.
329 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
333 scn
->scn_phys
.scn_state
= DSS_FINISHED
;
335 scn
->scn_phys
.scn_state
= DSS_CANCELED
;
337 if (dsl_scan_restarting(scn
, tx
))
338 spa_history_log_internal(spa
, "scan aborted, restarting", tx
,
339 "errors=%llu", spa_get_errlog_size(spa
));
341 spa_history_log_internal(spa
, "scan cancelled", tx
,
342 "errors=%llu", spa_get_errlog_size(spa
));
344 spa_history_log_internal(spa
, "scan done", tx
,
345 "errors=%llu", spa_get_errlog_size(spa
));
347 if (DSL_SCAN_IS_SCRUB_RESILVER(scn
)) {
348 mutex_enter(&spa
->spa_scrub_lock
);
349 while (spa
->spa_scrub_inflight
> 0) {
350 cv_wait(&spa
->spa_scrub_io_cv
,
351 &spa
->spa_scrub_lock
);
353 mutex_exit(&spa
->spa_scrub_lock
);
354 spa
->spa_scrub_started
= B_FALSE
;
355 spa
->spa_scrub_active
= B_FALSE
;
358 * If the scrub/resilver completed, update all DTLs to
359 * reflect this. Whether it succeeded or not, vacate
360 * all temporary scrub DTLs.
362 vdev_dtl_reassess(spa
->spa_root_vdev
, tx
->tx_txg
,
363 complete
? scn
->scn_phys
.scn_max_txg
: 0, B_TRUE
);
365 spa_event_notify(spa
, NULL
, NULL
,
366 scn
->scn_phys
.scn_min_txg
?
367 ESC_ZFS_RESILVER_FINISH
: ESC_ZFS_SCRUB_FINISH
);
369 spa_errlog_rotate(spa
);
372 * We may have finished replacing a device.
373 * Let the async thread assess this and handle the detach.
375 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
378 scn
->scn_phys
.scn_end_time
= gethrestime_sec();
380 if (spa
->spa_errata
== ZPOOL_ERRATA_ZOL_2094_SCRUB
)
386 dsl_scan_cancel_check(void *arg
, dmu_tx_t
*tx
)
388 dsl_scan_t
*scn
= dmu_tx_pool(tx
)->dp_scan
;
390 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
391 return (SET_ERROR(ENOENT
));
397 dsl_scan_cancel_sync(void *arg
, dmu_tx_t
*tx
)
399 dsl_scan_t
*scn
= dmu_tx_pool(tx
)->dp_scan
;
401 dsl_scan_done(scn
, B_FALSE
, tx
);
402 dsl_scan_sync_state(scn
, tx
);
406 dsl_scan_cancel(dsl_pool_t
*dp
)
408 return (dsl_sync_task(spa_name(dp
->dp_spa
), dsl_scan_cancel_check
,
409 dsl_scan_cancel_sync
, NULL
, 3, ZFS_SPACE_CHECK_RESERVED
));
413 dsl_scan_is_paused_scrub(const dsl_scan_t
*scn
)
415 if (dsl_scan_scrubbing(scn
->scn_dp
) &&
416 scn
->scn_phys
.scn_flags
& DSF_SCRUB_PAUSED
)
423 dsl_scrub_pause_resume_check(void *arg
, dmu_tx_t
*tx
)
425 pool_scrub_cmd_t
*cmd
= arg
;
426 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
427 dsl_scan_t
*scn
= dp
->dp_scan
;
429 if (*cmd
== POOL_SCRUB_PAUSE
) {
430 /* can't pause a scrub when there is no in-progress scrub */
431 if (!dsl_scan_scrubbing(dp
))
432 return (SET_ERROR(ENOENT
));
434 /* can't pause a paused scrub */
435 if (dsl_scan_is_paused_scrub(scn
))
436 return (SET_ERROR(EBUSY
));
437 } else if (*cmd
!= POOL_SCRUB_NORMAL
) {
438 return (SET_ERROR(ENOTSUP
));
445 dsl_scrub_pause_resume_sync(void *arg
, dmu_tx_t
*tx
)
447 pool_scrub_cmd_t
*cmd
= arg
;
448 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
449 spa_t
*spa
= dp
->dp_spa
;
450 dsl_scan_t
*scn
= dp
->dp_scan
;
453 if (*cmd
== POOL_SCRUB_PAUSE
) {
454 /* can't pause a scrub when there is no in-progress scrub */
455 spa
->spa_scan_pass_scrub_pause
= gethrestime_sec();
456 scn
->scn_phys
.scn_flags
|= DSF_SCRUB_PAUSED
;
457 dsl_scan_sync_state(scn
, tx
);
459 ASSERT3U(*cmd
, ==, POOL_SCRUB_NORMAL
);
460 if (dsl_scan_is_paused_scrub(scn
)) {
462 * We need to keep track of how much time we spend
463 * paused per pass so that we can adjust the scrub rate
464 * shown in the output of 'zpool status'
466 spa
->spa_scan_pass_scrub_spent_paused
+=
467 gethrestime_sec() - spa
->spa_scan_pass_scrub_pause
;
468 spa
->spa_scan_pass_scrub_pause
= 0;
469 scn
->scn_phys
.scn_flags
&= ~DSF_SCRUB_PAUSED
;
470 dsl_scan_sync_state(scn
, tx
);
476 * Set scrub pause/resume state if it makes sense to do so
479 dsl_scrub_set_pause_resume(const dsl_pool_t
*dp
, pool_scrub_cmd_t cmd
)
481 return (dsl_sync_task(spa_name(dp
->dp_spa
),
482 dsl_scrub_pause_resume_check
, dsl_scrub_pause_resume_sync
, &cmd
, 3,
483 ZFS_SPACE_CHECK_RESERVED
));
487 dsl_scan_scrubbing(const dsl_pool_t
*dp
)
489 dsl_scan_t
*scn
= dp
->dp_scan
;
491 if (scn
->scn_phys
.scn_state
== DSS_SCANNING
&&
492 scn
->scn_phys
.scn_func
== POOL_SCAN_SCRUB
)
498 static void dsl_scan_visitbp(blkptr_t
*bp
, const zbookmark_phys_t
*zb
,
499 dnode_phys_t
*dnp
, dsl_dataset_t
*ds
, dsl_scan_t
*scn
,
500 dmu_objset_type_t ostype
, dmu_tx_t
*tx
);
501 inline __attribute__((always_inline
)) static void dsl_scan_visitdnode(
502 dsl_scan_t
*, dsl_dataset_t
*ds
, dmu_objset_type_t ostype
,
503 dnode_phys_t
*dnp
, uint64_t object
, dmu_tx_t
*tx
);
506 dsl_free(dsl_pool_t
*dp
, uint64_t txg
, const blkptr_t
*bp
)
508 zio_free(dp
->dp_spa
, txg
, bp
);
512 dsl_free_sync(zio_t
*pio
, dsl_pool_t
*dp
, uint64_t txg
, const blkptr_t
*bpp
)
514 ASSERT(dsl_pool_sync_context(dp
));
515 zio_nowait(zio_free_sync(pio
, dp
->dp_spa
, txg
, bpp
, pio
->io_flags
));
519 dsl_scan_ds_maxtxg(dsl_dataset_t
*ds
)
521 uint64_t smt
= ds
->ds_dir
->dd_pool
->dp_scan
->scn_phys
.scn_max_txg
;
522 if (ds
->ds_is_snapshot
)
523 return (MIN(smt
, dsl_dataset_phys(ds
)->ds_creation_txg
));
528 dsl_scan_sync_state(dsl_scan_t
*scn
, dmu_tx_t
*tx
)
530 VERIFY0(zap_update(scn
->scn_dp
->dp_meta_objset
,
531 DMU_POOL_DIRECTORY_OBJECT
,
532 DMU_POOL_SCAN
, sizeof (uint64_t), SCAN_PHYS_NUMINTS
,
533 &scn
->scn_phys
, tx
));
536 extern int zfs_vdev_async_write_active_min_dirty_percent
;
539 dsl_scan_check_suspend(dsl_scan_t
*scn
, const zbookmark_phys_t
*zb
)
541 uint64_t elapsed_nanosecs
;
545 /* we never skip user/group accounting objects */
546 if (zb
&& (int64_t)zb
->zb_object
< 0)
549 if (scn
->scn_suspending
)
550 return (B_TRUE
); /* we're already suspending */
552 if (!ZB_IS_ZERO(&scn
->scn_phys
.scn_bookmark
))
553 return (B_FALSE
); /* we're resuming */
555 /* We only know how to resume from level-0 blocks. */
556 if (zb
&& zb
->zb_level
!= 0)
561 * - we have scanned for the maximum time: an entire txg
562 * timeout (default 5 sec)
564 * - we have scanned for at least the minimum time (default 1 sec
565 * for scrub, 3 sec for resilver), and either we have sufficient
566 * dirty data that we are starting to write more quickly
567 * (default 30%), or someone is explicitly waiting for this txg
570 * - the spa is shutting down because this pool is being exported
571 * or the machine is rebooting.
573 mintime
= (scn
->scn_phys
.scn_func
== POOL_SCAN_RESILVER
) ?
574 zfs_resilver_min_time_ms
: zfs_scan_min_time_ms
;
575 elapsed_nanosecs
= gethrtime() - scn
->scn_sync_start_time
;
576 dirty_pct
= scn
->scn_dp
->dp_dirty_total
* 100 / zfs_dirty_data_max
;
577 if (elapsed_nanosecs
/ NANOSEC
>= zfs_txg_timeout
||
578 (NSEC2MSEC(elapsed_nanosecs
) > mintime
&&
579 (txg_sync_waiting(scn
->scn_dp
) ||
580 dirty_pct
>= zfs_vdev_async_write_active_min_dirty_percent
)) ||
581 spa_shutting_down(scn
->scn_dp
->dp_spa
)) {
583 dprintf("suspending at bookmark %llx/%llx/%llx/%llx\n",
584 (longlong_t
)zb
->zb_objset
,
585 (longlong_t
)zb
->zb_object
,
586 (longlong_t
)zb
->zb_level
,
587 (longlong_t
)zb
->zb_blkid
);
588 scn
->scn_phys
.scn_bookmark
= *zb
;
590 dprintf("suspending at DDT bookmark %llx/%llx/%llx/%llx\n",
591 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_class
,
592 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_type
,
593 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_checksum
,
594 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_cursor
);
595 scn
->scn_suspending
= B_TRUE
;
601 typedef struct zil_scan_arg
{
603 zil_header_t
*zsa_zh
;
608 dsl_scan_zil_block(zilog_t
*zilog
, blkptr_t
*bp
, void *arg
, uint64_t claim_txg
)
610 zil_scan_arg_t
*zsa
= arg
;
611 dsl_pool_t
*dp
= zsa
->zsa_dp
;
612 dsl_scan_t
*scn
= dp
->dp_scan
;
613 zil_header_t
*zh
= zsa
->zsa_zh
;
616 if (BP_IS_HOLE(bp
) || bp
->blk_birth
<= scn
->scn_phys
.scn_cur_min_txg
)
620 * One block ("stubby") can be allocated a long time ago; we
621 * want to visit that one because it has been allocated
622 * (on-disk) even if it hasn't been claimed (even though for
623 * scrub there's nothing to do to it).
625 if (claim_txg
== 0 && bp
->blk_birth
>= spa_first_txg(dp
->dp_spa
))
628 SET_BOOKMARK(&zb
, zh
->zh_log
.blk_cksum
.zc_word
[ZIL_ZC_OBJSET
],
629 ZB_ZIL_OBJECT
, ZB_ZIL_LEVEL
, bp
->blk_cksum
.zc_word
[ZIL_ZC_SEQ
]);
631 VERIFY(0 == scan_funcs
[scn
->scn_phys
.scn_func
](dp
, bp
, &zb
));
637 dsl_scan_zil_record(zilog_t
*zilog
, lr_t
*lrc
, void *arg
, uint64_t claim_txg
)
639 if (lrc
->lrc_txtype
== TX_WRITE
) {
640 zil_scan_arg_t
*zsa
= arg
;
641 dsl_pool_t
*dp
= zsa
->zsa_dp
;
642 dsl_scan_t
*scn
= dp
->dp_scan
;
643 zil_header_t
*zh
= zsa
->zsa_zh
;
644 lr_write_t
*lr
= (lr_write_t
*)lrc
;
645 blkptr_t
*bp
= &lr
->lr_blkptr
;
648 if (BP_IS_HOLE(bp
) ||
649 bp
->blk_birth
<= scn
->scn_phys
.scn_cur_min_txg
)
653 * birth can be < claim_txg if this record's txg is
654 * already txg sync'ed (but this log block contains
655 * other records that are not synced)
657 if (claim_txg
== 0 || bp
->blk_birth
< claim_txg
)
660 SET_BOOKMARK(&zb
, zh
->zh_log
.blk_cksum
.zc_word
[ZIL_ZC_OBJSET
],
661 lr
->lr_foid
, ZB_ZIL_LEVEL
,
662 lr
->lr_offset
/ BP_GET_LSIZE(bp
));
664 VERIFY(0 == scan_funcs
[scn
->scn_phys
.scn_func
](dp
, bp
, &zb
));
670 dsl_scan_zil(dsl_pool_t
*dp
, zil_header_t
*zh
)
672 uint64_t claim_txg
= zh
->zh_claim_txg
;
673 zil_scan_arg_t zsa
= { dp
, zh
};
677 * We only want to visit blocks that have been claimed but not yet
678 * replayed (or, in read-only mode, blocks that *would* be claimed).
680 if (claim_txg
== 0 && spa_writeable(dp
->dp_spa
))
683 zilog
= zil_alloc(dp
->dp_meta_objset
, zh
);
685 (void) zil_parse(zilog
, dsl_scan_zil_block
, dsl_scan_zil_record
, &zsa
,
693 dsl_scan_prefetch(dsl_scan_t
*scn
, arc_buf_t
*buf
, blkptr_t
*bp
,
694 uint64_t objset
, uint64_t object
, uint64_t blkid
)
696 zbookmark_phys_t czb
;
697 arc_flags_t flags
= ARC_FLAG_NOWAIT
| ARC_FLAG_PREFETCH
;
699 if (zfs_no_scrub_prefetch
)
702 if (BP_IS_HOLE(bp
) || bp
->blk_birth
<= scn
->scn_phys
.scn_min_txg
||
703 (BP_GET_LEVEL(bp
) == 0 && BP_GET_TYPE(bp
) != DMU_OT_DNODE
))
706 SET_BOOKMARK(&czb
, objset
, object
, BP_GET_LEVEL(bp
), blkid
);
708 (void) arc_read(scn
->scn_zio_root
, scn
->scn_dp
->dp_spa
, bp
,
709 NULL
, NULL
, ZIO_PRIORITY_ASYNC_READ
,
710 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SCAN_THREAD
, &flags
, &czb
);
714 dsl_scan_check_resume(dsl_scan_t
*scn
, const dnode_phys_t
*dnp
,
715 const zbookmark_phys_t
*zb
)
718 * We never skip over user/group accounting objects (obj<0)
720 if (!ZB_IS_ZERO(&scn
->scn_phys
.scn_bookmark
) &&
721 (int64_t)zb
->zb_object
>= 0) {
723 * If we already visited this bp & everything below (in
724 * a prior txg sync), don't bother doing it again.
726 if (zbookmark_subtree_completed(dnp
, zb
,
727 &scn
->scn_phys
.scn_bookmark
))
731 * If we found the block we're trying to resume from, or
732 * we went past it to a different object, zero it out to
733 * indicate that it's OK to start checking for suspending
736 if (bcmp(zb
, &scn
->scn_phys
.scn_bookmark
, sizeof (*zb
)) == 0 ||
737 zb
->zb_object
> scn
->scn_phys
.scn_bookmark
.zb_object
) {
738 dprintf("resuming at %llx/%llx/%llx/%llx\n",
739 (longlong_t
)zb
->zb_objset
,
740 (longlong_t
)zb
->zb_object
,
741 (longlong_t
)zb
->zb_level
,
742 (longlong_t
)zb
->zb_blkid
);
743 bzero(&scn
->scn_phys
.scn_bookmark
, sizeof (*zb
));
750 * Return nonzero on i/o error.
751 * Return new buf to write out in *bufp.
753 inline __attribute__((always_inline
)) static int
754 dsl_scan_recurse(dsl_scan_t
*scn
, dsl_dataset_t
*ds
, dmu_objset_type_t ostype
,
755 dnode_phys_t
*dnp
, const blkptr_t
*bp
,
756 const zbookmark_phys_t
*zb
, dmu_tx_t
*tx
)
758 dsl_pool_t
*dp
= scn
->scn_dp
;
759 int zio_flags
= ZIO_FLAG_CANFAIL
| ZIO_FLAG_SCAN_THREAD
;
762 if (BP_GET_LEVEL(bp
) > 0) {
763 arc_flags_t flags
= ARC_FLAG_WAIT
;
766 int epb
= BP_GET_LSIZE(bp
) >> SPA_BLKPTRSHIFT
;
769 err
= arc_read(NULL
, dp
->dp_spa
, bp
, arc_getbuf_func
, &buf
,
770 ZIO_PRIORITY_ASYNC_READ
, zio_flags
, &flags
, zb
);
772 scn
->scn_phys
.scn_errors
++;
775 for (i
= 0, cbp
= buf
->b_data
; i
< epb
; i
++, cbp
++) {
776 dsl_scan_prefetch(scn
, buf
, cbp
, zb
->zb_objset
,
777 zb
->zb_object
, zb
->zb_blkid
* epb
+ i
);
779 for (i
= 0, cbp
= buf
->b_data
; i
< epb
; i
++, cbp
++) {
780 zbookmark_phys_t czb
;
782 SET_BOOKMARK(&czb
, zb
->zb_objset
, zb
->zb_object
,
784 zb
->zb_blkid
* epb
+ i
);
785 dsl_scan_visitbp(cbp
, &czb
, dnp
,
786 ds
, scn
, ostype
, tx
);
788 arc_buf_destroy(buf
, &buf
);
789 } else if (BP_GET_TYPE(bp
) == DMU_OT_DNODE
) {
790 arc_flags_t flags
= ARC_FLAG_WAIT
;
793 int epb
= BP_GET_LSIZE(bp
) >> DNODE_SHIFT
;
796 err
= arc_read(NULL
, dp
->dp_spa
, bp
, arc_getbuf_func
, &buf
,
797 ZIO_PRIORITY_ASYNC_READ
, zio_flags
, &flags
, zb
);
799 scn
->scn_phys
.scn_errors
++;
802 for (i
= 0, cdnp
= buf
->b_data
; i
< epb
;
803 i
+= cdnp
->dn_extra_slots
+ 1,
804 cdnp
+= cdnp
->dn_extra_slots
+ 1) {
805 for (j
= 0; j
< cdnp
->dn_nblkptr
; j
++) {
806 blkptr_t
*cbp
= &cdnp
->dn_blkptr
[j
];
807 dsl_scan_prefetch(scn
, buf
, cbp
,
808 zb
->zb_objset
, zb
->zb_blkid
* epb
+ i
, j
);
811 for (i
= 0, cdnp
= buf
->b_data
; i
< epb
;
812 i
+= cdnp
->dn_extra_slots
+ 1,
813 cdnp
+= cdnp
->dn_extra_slots
+ 1) {
814 dsl_scan_visitdnode(scn
, ds
, ostype
,
815 cdnp
, zb
->zb_blkid
* epb
+ i
, tx
);
818 arc_buf_destroy(buf
, &buf
);
819 } else if (BP_GET_TYPE(bp
) == DMU_OT_OBJSET
) {
820 arc_flags_t flags
= ARC_FLAG_WAIT
;
824 err
= arc_read(NULL
, dp
->dp_spa
, bp
, arc_getbuf_func
, &buf
,
825 ZIO_PRIORITY_ASYNC_READ
, zio_flags
, &flags
, zb
);
827 scn
->scn_phys
.scn_errors
++;
833 dsl_scan_visitdnode(scn
, ds
, osp
->os_type
,
834 &osp
->os_meta_dnode
, DMU_META_DNODE_OBJECT
, tx
);
836 if (OBJSET_BUF_HAS_USERUSED(buf
)) {
838 * We also always visit user/group accounting
839 * objects, and never skip them, even if we are
840 * suspending. This is necessary so that the space
841 * deltas from this txg get integrated.
843 dsl_scan_visitdnode(scn
, ds
, osp
->os_type
,
844 &osp
->os_groupused_dnode
,
845 DMU_GROUPUSED_OBJECT
, tx
);
846 dsl_scan_visitdnode(scn
, ds
, osp
->os_type
,
847 &osp
->os_userused_dnode
,
848 DMU_USERUSED_OBJECT
, tx
);
850 arc_buf_destroy(buf
, &buf
);
856 inline __attribute__((always_inline
)) static void
857 dsl_scan_visitdnode(dsl_scan_t
*scn
, dsl_dataset_t
*ds
,
858 dmu_objset_type_t ostype
, dnode_phys_t
*dnp
,
859 uint64_t object
, dmu_tx_t
*tx
)
863 for (j
= 0; j
< dnp
->dn_nblkptr
; j
++) {
864 zbookmark_phys_t czb
;
866 SET_BOOKMARK(&czb
, ds
? ds
->ds_object
: 0, object
,
867 dnp
->dn_nlevels
- 1, j
);
868 dsl_scan_visitbp(&dnp
->dn_blkptr
[j
],
869 &czb
, dnp
, ds
, scn
, ostype
, tx
);
872 if (dnp
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
) {
873 zbookmark_phys_t czb
;
874 SET_BOOKMARK(&czb
, ds
? ds
->ds_object
: 0, object
,
876 dsl_scan_visitbp(DN_SPILL_BLKPTR(dnp
),
877 &czb
, dnp
, ds
, scn
, ostype
, tx
);
882 * The arguments are in this order because mdb can only print the
883 * first 5; we want them to be useful.
886 dsl_scan_visitbp(blkptr_t
*bp
, const zbookmark_phys_t
*zb
,
887 dnode_phys_t
*dnp
, dsl_dataset_t
*ds
, dsl_scan_t
*scn
,
888 dmu_objset_type_t ostype
, dmu_tx_t
*tx
)
890 dsl_pool_t
*dp
= scn
->scn_dp
;
893 bp_toread
= kmem_alloc(sizeof (blkptr_t
), KM_SLEEP
);
896 /* ASSERT(pbuf == NULL || arc_released(pbuf)); */
898 if (dsl_scan_check_suspend(scn
, zb
))
901 if (dsl_scan_check_resume(scn
, dnp
, zb
))
907 scn
->scn_visited_this_txg
++;
910 * This debugging is commented out to conserve stack space. This
911 * function is called recursively and the debugging addes several
912 * bytes to the stack for each call. It can be commented back in
913 * if required to debug an issue in dsl_scan_visitbp().
916 * "visiting ds=%p/%llu zb=%llx/%llx/%llx/%llx bp=%p",
917 * ds, ds ? ds->ds_object : 0,
918 * zb->zb_objset, zb->zb_object, zb->zb_level, zb->zb_blkid,
922 if (bp
->blk_birth
<= scn
->scn_phys
.scn_cur_min_txg
)
925 if (dsl_scan_recurse(scn
, ds
, ostype
, dnp
, bp_toread
, zb
, tx
) != 0)
929 * If dsl_scan_ddt() has already visited this block, it will have
930 * already done any translations or scrubbing, so don't call the
933 if (ddt_class_contains(dp
->dp_spa
,
934 scn
->scn_phys
.scn_ddt_class_max
, bp
)) {
939 * If this block is from the future (after cur_max_txg), then we
940 * are doing this on behalf of a deleted snapshot, and we will
941 * revisit the future block on the next pass of this dataset.
942 * Don't scan it now unless we need to because something
943 * under it was modified.
945 if (BP_PHYSICAL_BIRTH(bp
) <= scn
->scn_phys
.scn_cur_max_txg
) {
946 scan_funcs
[scn
->scn_phys
.scn_func
](dp
, bp
, zb
);
949 kmem_free(bp_toread
, sizeof (blkptr_t
));
953 dsl_scan_visit_rootbp(dsl_scan_t
*scn
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
958 SET_BOOKMARK(&zb
, ds
? ds
->ds_object
: DMU_META_OBJSET
,
959 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
960 dsl_scan_visitbp(bp
, &zb
, NULL
,
961 ds
, scn
, DMU_OST_NONE
, tx
);
963 dprintf_ds(ds
, "finished scan%s", "");
967 dsl_scan_ds_destroyed(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
969 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
970 dsl_scan_t
*scn
= dp
->dp_scan
;
973 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
976 if (scn
->scn_phys
.scn_bookmark
.zb_objset
== ds
->ds_object
) {
977 if (ds
->ds_is_snapshot
) {
980 * - scn_cur_{min,max}_txg stays the same.
981 * - Setting the flag is not really necessary if
982 * scn_cur_max_txg == scn_max_txg, because there
983 * is nothing after this snapshot that we care
984 * about. However, we set it anyway and then
985 * ignore it when we retraverse it in
986 * dsl_scan_visitds().
988 scn
->scn_phys
.scn_bookmark
.zb_objset
=
989 dsl_dataset_phys(ds
)->ds_next_snap_obj
;
990 zfs_dbgmsg("destroying ds %llu; currently traversing; "
991 "reset zb_objset to %llu",
992 (u_longlong_t
)ds
->ds_object
,
993 (u_longlong_t
)dsl_dataset_phys(ds
)->
995 scn
->scn_phys
.scn_flags
|= DSF_VISIT_DS_AGAIN
;
997 SET_BOOKMARK(&scn
->scn_phys
.scn_bookmark
,
998 ZB_DESTROYED_OBJSET
, 0, 0, 0);
999 zfs_dbgmsg("destroying ds %llu; currently traversing; "
1000 "reset bookmark to -1,0,0,0",
1001 (u_longlong_t
)ds
->ds_object
);
1003 } else if (zap_lookup_int_key(dp
->dp_meta_objset
,
1004 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
, &mintxg
) == 0) {
1005 ASSERT3U(dsl_dataset_phys(ds
)->ds_num_children
, <=, 1);
1006 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
1007 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
, tx
));
1008 if (ds
->ds_is_snapshot
) {
1010 * We keep the same mintxg; it could be >
1011 * ds_creation_txg if the previous snapshot was
1014 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
1015 scn
->scn_phys
.scn_queue_obj
,
1016 dsl_dataset_phys(ds
)->ds_next_snap_obj
,
1018 zfs_dbgmsg("destroying ds %llu; in queue; "
1019 "replacing with %llu",
1020 (u_longlong_t
)ds
->ds_object
,
1021 (u_longlong_t
)dsl_dataset_phys(ds
)->
1024 zfs_dbgmsg("destroying ds %llu; in queue; removing",
1025 (u_longlong_t
)ds
->ds_object
);
1030 * dsl_scan_sync() should be called after this, and should sync
1031 * out our changed state, but just to be safe, do it here.
1033 dsl_scan_sync_state(scn
, tx
);
1037 dsl_scan_ds_snapshotted(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
1039 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
1040 dsl_scan_t
*scn
= dp
->dp_scan
;
1043 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
1046 ASSERT(dsl_dataset_phys(ds
)->ds_prev_snap_obj
!= 0);
1048 if (scn
->scn_phys
.scn_bookmark
.zb_objset
== ds
->ds_object
) {
1049 scn
->scn_phys
.scn_bookmark
.zb_objset
=
1050 dsl_dataset_phys(ds
)->ds_prev_snap_obj
;
1051 zfs_dbgmsg("snapshotting ds %llu; currently traversing; "
1052 "reset zb_objset to %llu",
1053 (u_longlong_t
)ds
->ds_object
,
1054 (u_longlong_t
)dsl_dataset_phys(ds
)->ds_prev_snap_obj
);
1055 } else if (zap_lookup_int_key(dp
->dp_meta_objset
,
1056 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
, &mintxg
) == 0) {
1057 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
1058 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
, tx
));
1059 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
1060 scn
->scn_phys
.scn_queue_obj
,
1061 dsl_dataset_phys(ds
)->ds_prev_snap_obj
, mintxg
, tx
) == 0);
1062 zfs_dbgmsg("snapshotting ds %llu; in queue; "
1063 "replacing with %llu",
1064 (u_longlong_t
)ds
->ds_object
,
1065 (u_longlong_t
)dsl_dataset_phys(ds
)->ds_prev_snap_obj
);
1067 dsl_scan_sync_state(scn
, tx
);
1071 dsl_scan_ds_clone_swapped(dsl_dataset_t
*ds1
, dsl_dataset_t
*ds2
, dmu_tx_t
*tx
)
1073 dsl_pool_t
*dp
= ds1
->ds_dir
->dd_pool
;
1074 dsl_scan_t
*scn
= dp
->dp_scan
;
1077 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
1080 if (scn
->scn_phys
.scn_bookmark
.zb_objset
== ds1
->ds_object
) {
1081 scn
->scn_phys
.scn_bookmark
.zb_objset
= ds2
->ds_object
;
1082 zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
1083 "reset zb_objset to %llu",
1084 (u_longlong_t
)ds1
->ds_object
,
1085 (u_longlong_t
)ds2
->ds_object
);
1086 } else if (scn
->scn_phys
.scn_bookmark
.zb_objset
== ds2
->ds_object
) {
1087 scn
->scn_phys
.scn_bookmark
.zb_objset
= ds1
->ds_object
;
1088 zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
1089 "reset zb_objset to %llu",
1090 (u_longlong_t
)ds2
->ds_object
,
1091 (u_longlong_t
)ds1
->ds_object
);
1094 if (zap_lookup_int_key(dp
->dp_meta_objset
, scn
->scn_phys
.scn_queue_obj
,
1095 ds1
->ds_object
, &mintxg
) == 0) {
1098 ASSERT3U(mintxg
, ==, dsl_dataset_phys(ds1
)->ds_prev_snap_txg
);
1099 ASSERT3U(mintxg
, ==, dsl_dataset_phys(ds2
)->ds_prev_snap_txg
);
1100 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
1101 scn
->scn_phys
.scn_queue_obj
, ds1
->ds_object
, tx
));
1102 err
= zap_add_int_key(dp
->dp_meta_objset
,
1103 scn
->scn_phys
.scn_queue_obj
, ds2
->ds_object
, mintxg
, tx
);
1104 VERIFY(err
== 0 || err
== EEXIST
);
1105 if (err
== EEXIST
) {
1106 /* Both were there to begin with */
1107 VERIFY(0 == zap_add_int_key(dp
->dp_meta_objset
,
1108 scn
->scn_phys
.scn_queue_obj
,
1109 ds1
->ds_object
, mintxg
, tx
));
1111 zfs_dbgmsg("clone_swap ds %llu; in queue; "
1112 "replacing with %llu",
1113 (u_longlong_t
)ds1
->ds_object
,
1114 (u_longlong_t
)ds2
->ds_object
);
1115 } else if (zap_lookup_int_key(dp
->dp_meta_objset
,
1116 scn
->scn_phys
.scn_queue_obj
, ds2
->ds_object
, &mintxg
) == 0) {
1117 ASSERT3U(mintxg
, ==, dsl_dataset_phys(ds1
)->ds_prev_snap_txg
);
1118 ASSERT3U(mintxg
, ==, dsl_dataset_phys(ds2
)->ds_prev_snap_txg
);
1119 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
1120 scn
->scn_phys
.scn_queue_obj
, ds2
->ds_object
, tx
));
1121 VERIFY(0 == zap_add_int_key(dp
->dp_meta_objset
,
1122 scn
->scn_phys
.scn_queue_obj
, ds1
->ds_object
, mintxg
, tx
));
1123 zfs_dbgmsg("clone_swap ds %llu; in queue; "
1124 "replacing with %llu",
1125 (u_longlong_t
)ds2
->ds_object
,
1126 (u_longlong_t
)ds1
->ds_object
);
1129 dsl_scan_sync_state(scn
, tx
);
1132 struct enqueue_clones_arg
{
1139 enqueue_clones_cb(dsl_pool_t
*dp
, dsl_dataset_t
*hds
, void *arg
)
1141 struct enqueue_clones_arg
*eca
= arg
;
1144 dsl_scan_t
*scn
= dp
->dp_scan
;
1146 if (dsl_dir_phys(hds
->ds_dir
)->dd_origin_obj
!= eca
->originobj
)
1149 err
= dsl_dataset_hold_obj(dp
, hds
->ds_object
, FTAG
, &ds
);
1153 while (dsl_dataset_phys(ds
)->ds_prev_snap_obj
!= eca
->originobj
) {
1154 dsl_dataset_t
*prev
;
1155 err
= dsl_dataset_hold_obj(dp
,
1156 dsl_dataset_phys(ds
)->ds_prev_snap_obj
, FTAG
, &prev
);
1158 dsl_dataset_rele(ds
, FTAG
);
1163 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
1164 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
,
1165 dsl_dataset_phys(ds
)->ds_prev_snap_txg
, eca
->tx
) == 0);
1166 dsl_dataset_rele(ds
, FTAG
);
1171 dsl_scan_visitds(dsl_scan_t
*scn
, uint64_t dsobj
, dmu_tx_t
*tx
)
1173 dsl_pool_t
*dp
= scn
->scn_dp
;
1178 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp
, dsobj
, FTAG
, &ds
));
1180 if (scn
->scn_phys
.scn_cur_min_txg
>=
1181 scn
->scn_phys
.scn_max_txg
) {
1183 * This can happen if this snapshot was created after the
1184 * scan started, and we already completed a previous snapshot
1185 * that was created after the scan started. This snapshot
1186 * only references blocks with:
1188 * birth < our ds_creation_txg
1189 * cur_min_txg is no less than ds_creation_txg.
1190 * We have already visited these blocks.
1192 * birth > scn_max_txg
1193 * The scan requested not to visit these blocks.
1195 * Subsequent snapshots (and clones) can reference our
1196 * blocks, or blocks with even higher birth times.
1197 * Therefore we do not need to visit them either,
1198 * so we do not add them to the work queue.
1200 * Note that checking for cur_min_txg >= cur_max_txg
1201 * is not sufficient, because in that case we may need to
1202 * visit subsequent snapshots. This happens when min_txg > 0,
1203 * which raises cur_min_txg. In this case we will visit
1204 * this dataset but skip all of its blocks, because the
1205 * rootbp's birth time is < cur_min_txg. Then we will
1206 * add the next snapshots/clones to the work queue.
1208 char *dsname
= kmem_alloc(ZFS_MAX_DATASET_NAME_LEN
, KM_SLEEP
);
1209 dsl_dataset_name(ds
, dsname
);
1210 zfs_dbgmsg("scanning dataset %llu (%s) is unnecessary because "
1211 "cur_min_txg (%llu) >= max_txg (%llu)",
1213 scn
->scn_phys
.scn_cur_min_txg
,
1214 scn
->scn_phys
.scn_max_txg
);
1215 kmem_free(dsname
, MAXNAMELEN
);
1220 if (dmu_objset_from_ds(ds
, &os
))
1224 * Only the ZIL in the head (non-snapshot) is valid. Even though
1225 * snapshots can have ZIL block pointers (which may be the same
1226 * BP as in the head), they must be ignored. So we traverse the
1227 * ZIL here, rather than in scan_recurse(), because the regular
1228 * snapshot block-sharing rules don't apply to it.
1230 if (DSL_SCAN_IS_SCRUB_RESILVER(scn
) && !ds
->ds_is_snapshot
)
1231 dsl_scan_zil(dp
, &os
->os_zil_header
);
1234 * Iterate over the bps in this ds.
1236 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
1237 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
1238 dsl_scan_visit_rootbp(scn
, ds
, &dsl_dataset_phys(ds
)->ds_bp
, tx
);
1239 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
1241 dsname
= kmem_alloc(ZFS_MAX_DATASET_NAME_LEN
, KM_SLEEP
);
1242 dsl_dataset_name(ds
, dsname
);
1243 zfs_dbgmsg("scanned dataset %llu (%s) with min=%llu max=%llu; "
1245 (longlong_t
)dsobj
, dsname
,
1246 (longlong_t
)scn
->scn_phys
.scn_cur_min_txg
,
1247 (longlong_t
)scn
->scn_phys
.scn_cur_max_txg
,
1248 (int)scn
->scn_suspending
);
1249 kmem_free(dsname
, ZFS_MAX_DATASET_NAME_LEN
);
1251 if (scn
->scn_suspending
)
1255 * We've finished this pass over this dataset.
1259 * If we did not completely visit this dataset, do another pass.
1261 if (scn
->scn_phys
.scn_flags
& DSF_VISIT_DS_AGAIN
) {
1262 zfs_dbgmsg("incomplete pass; visiting again");
1263 scn
->scn_phys
.scn_flags
&= ~DSF_VISIT_DS_AGAIN
;
1264 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
1265 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
,
1266 scn
->scn_phys
.scn_cur_max_txg
, tx
) == 0);
1271 * Add descendent datasets to work queue.
1273 if (dsl_dataset_phys(ds
)->ds_next_snap_obj
!= 0) {
1274 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
1275 scn
->scn_phys
.scn_queue_obj
,
1276 dsl_dataset_phys(ds
)->ds_next_snap_obj
,
1277 dsl_dataset_phys(ds
)->ds_creation_txg
, tx
) == 0);
1279 if (dsl_dataset_phys(ds
)->ds_num_children
> 1) {
1280 boolean_t usenext
= B_FALSE
;
1281 if (dsl_dataset_phys(ds
)->ds_next_clones_obj
!= 0) {
1284 * A bug in a previous version of the code could
1285 * cause upgrade_clones_cb() to not set
1286 * ds_next_snap_obj when it should, leading to a
1287 * missing entry. Therefore we can only use the
1288 * next_clones_obj when its count is correct.
1290 int err
= zap_count(dp
->dp_meta_objset
,
1291 dsl_dataset_phys(ds
)->ds_next_clones_obj
, &count
);
1293 count
== dsl_dataset_phys(ds
)->ds_num_children
- 1)
1298 VERIFY0(zap_join_key(dp
->dp_meta_objset
,
1299 dsl_dataset_phys(ds
)->ds_next_clones_obj
,
1300 scn
->scn_phys
.scn_queue_obj
,
1301 dsl_dataset_phys(ds
)->ds_creation_txg
, tx
));
1303 struct enqueue_clones_arg eca
;
1305 eca
.originobj
= ds
->ds_object
;
1307 VERIFY0(dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
1308 enqueue_clones_cb
, &eca
, DS_FIND_CHILDREN
));
1313 dsl_dataset_rele(ds
, FTAG
);
1318 enqueue_cb(dsl_pool_t
*dp
, dsl_dataset_t
*hds
, void *arg
)
1323 dsl_scan_t
*scn
= dp
->dp_scan
;
1325 err
= dsl_dataset_hold_obj(dp
, hds
->ds_object
, FTAG
, &ds
);
1329 while (dsl_dataset_phys(ds
)->ds_prev_snap_obj
!= 0) {
1330 dsl_dataset_t
*prev
;
1331 err
= dsl_dataset_hold_obj(dp
,
1332 dsl_dataset_phys(ds
)->ds_prev_snap_obj
, FTAG
, &prev
);
1334 dsl_dataset_rele(ds
, FTAG
);
1339 * If this is a clone, we don't need to worry about it for now.
1341 if (dsl_dataset_phys(prev
)->ds_next_snap_obj
!= ds
->ds_object
) {
1342 dsl_dataset_rele(ds
, FTAG
);
1343 dsl_dataset_rele(prev
, FTAG
);
1346 dsl_dataset_rele(ds
, FTAG
);
1350 VERIFY(zap_add_int_key(dp
->dp_meta_objset
, scn
->scn_phys
.scn_queue_obj
,
1351 ds
->ds_object
, dsl_dataset_phys(ds
)->ds_prev_snap_txg
, tx
) == 0);
1352 dsl_dataset_rele(ds
, FTAG
);
1357 * Scrub/dedup interaction.
1359 * If there are N references to a deduped block, we don't want to scrub it
1360 * N times -- ideally, we should scrub it exactly once.
1362 * We leverage the fact that the dde's replication class (enum ddt_class)
1363 * is ordered from highest replication class (DDT_CLASS_DITTO) to lowest
1364 * (DDT_CLASS_UNIQUE) so that we may walk the DDT in that order.
1366 * To prevent excess scrubbing, the scrub begins by walking the DDT
1367 * to find all blocks with refcnt > 1, and scrubs each of these once.
1368 * Since there are two replication classes which contain blocks with
1369 * refcnt > 1, we scrub the highest replication class (DDT_CLASS_DITTO) first.
1370 * Finally the top-down scrub begins, only visiting blocks with refcnt == 1.
1372 * There would be nothing more to say if a block's refcnt couldn't change
1373 * during a scrub, but of course it can so we must account for changes
1374 * in a block's replication class.
1376 * Here's an example of what can occur:
1378 * If a block has refcnt > 1 during the DDT scrub phase, but has refcnt == 1
1379 * when visited during the top-down scrub phase, it will be scrubbed twice.
1380 * This negates our scrub optimization, but is otherwise harmless.
1382 * If a block has refcnt == 1 during the DDT scrub phase, but has refcnt > 1
1383 * on each visit during the top-down scrub phase, it will never be scrubbed.
1384 * To catch this, ddt_sync_entry() notifies the scrub code whenever a block's
1385 * reference class transitions to a higher level (i.e DDT_CLASS_UNIQUE to
1386 * DDT_CLASS_DUPLICATE); if it transitions from refcnt == 1 to refcnt > 1
1387 * while a scrub is in progress, it scrubs the block right then.
1390 dsl_scan_ddt(dsl_scan_t
*scn
, dmu_tx_t
*tx
)
1392 ddt_bookmark_t
*ddb
= &scn
->scn_phys
.scn_ddt_bookmark
;
1397 bzero(&dde
, sizeof (ddt_entry_t
));
1399 while ((error
= ddt_walk(scn
->scn_dp
->dp_spa
, ddb
, &dde
)) == 0) {
1402 if (ddb
->ddb_class
> scn
->scn_phys
.scn_ddt_class_max
)
1404 dprintf("visiting ddb=%llu/%llu/%llu/%llx\n",
1405 (longlong_t
)ddb
->ddb_class
,
1406 (longlong_t
)ddb
->ddb_type
,
1407 (longlong_t
)ddb
->ddb_checksum
,
1408 (longlong_t
)ddb
->ddb_cursor
);
1410 /* There should be no pending changes to the dedup table */
1411 ddt
= scn
->scn_dp
->dp_spa
->spa_ddt
[ddb
->ddb_checksum
];
1412 ASSERT(avl_first(&ddt
->ddt_tree
) == NULL
);
1414 dsl_scan_ddt_entry(scn
, ddb
->ddb_checksum
, &dde
, tx
);
1417 if (dsl_scan_check_suspend(scn
, NULL
))
1421 zfs_dbgmsg("scanned %llu ddt entries with class_max = %u; "
1422 "suspending=%u", (longlong_t
)n
,
1423 (int)scn
->scn_phys
.scn_ddt_class_max
, (int)scn
->scn_suspending
);
1425 ASSERT(error
== 0 || error
== ENOENT
);
1426 ASSERT(error
!= ENOENT
||
1427 ddb
->ddb_class
> scn
->scn_phys
.scn_ddt_class_max
);
1432 dsl_scan_ddt_entry(dsl_scan_t
*scn
, enum zio_checksum checksum
,
1433 ddt_entry_t
*dde
, dmu_tx_t
*tx
)
1435 const ddt_key_t
*ddk
= &dde
->dde_key
;
1436 ddt_phys_t
*ddp
= dde
->dde_phys
;
1438 zbookmark_phys_t zb
= { 0 };
1441 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
1444 for (p
= 0; p
< DDT_PHYS_TYPES
; p
++, ddp
++) {
1445 if (ddp
->ddp_phys_birth
== 0 ||
1446 ddp
->ddp_phys_birth
> scn
->scn_phys
.scn_max_txg
)
1448 ddt_bp_create(checksum
, ddk
, ddp
, &bp
);
1450 scn
->scn_visited_this_txg
++;
1451 scan_funcs
[scn
->scn_phys
.scn_func
](scn
->scn_dp
, &bp
, &zb
);
1456 dsl_scan_visit(dsl_scan_t
*scn
, dmu_tx_t
*tx
)
1458 dsl_pool_t
*dp
= scn
->scn_dp
;
1460 zap_attribute_t
*za
;
1462 if (scn
->scn_phys
.scn_ddt_bookmark
.ddb_class
<=
1463 scn
->scn_phys
.scn_ddt_class_max
) {
1464 scn
->scn_phys
.scn_cur_min_txg
= scn
->scn_phys
.scn_min_txg
;
1465 scn
->scn_phys
.scn_cur_max_txg
= scn
->scn_phys
.scn_max_txg
;
1466 dsl_scan_ddt(scn
, tx
);
1467 if (scn
->scn_suspending
)
1471 if (scn
->scn_phys
.scn_bookmark
.zb_objset
== DMU_META_OBJSET
) {
1472 /* First do the MOS & ORIGIN */
1474 scn
->scn_phys
.scn_cur_min_txg
= scn
->scn_phys
.scn_min_txg
;
1475 scn
->scn_phys
.scn_cur_max_txg
= scn
->scn_phys
.scn_max_txg
;
1476 dsl_scan_visit_rootbp(scn
, NULL
,
1477 &dp
->dp_meta_rootbp
, tx
);
1478 spa_set_rootblkptr(dp
->dp_spa
, &dp
->dp_meta_rootbp
);
1479 if (scn
->scn_suspending
)
1482 if (spa_version(dp
->dp_spa
) < SPA_VERSION_DSL_SCRUB
) {
1483 VERIFY0(dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
1484 enqueue_cb
, tx
, DS_FIND_CHILDREN
));
1486 dsl_scan_visitds(scn
,
1487 dp
->dp_origin_snap
->ds_object
, tx
);
1489 ASSERT(!scn
->scn_suspending
);
1490 } else if (scn
->scn_phys
.scn_bookmark
.zb_objset
!=
1491 ZB_DESTROYED_OBJSET
) {
1493 * If we were suspended, continue from here. Note if the
1494 * ds we were suspended on was deleted, the zb_objset may
1495 * be -1, so we will skip this and find a new objset
1498 dsl_scan_visitds(scn
, scn
->scn_phys
.scn_bookmark
.zb_objset
, tx
);
1499 if (scn
->scn_suspending
)
1504 * In case we were suspended right at the end of the ds, zero the
1505 * bookmark so we don't think that we're still trying to resume.
1507 bzero(&scn
->scn_phys
.scn_bookmark
, sizeof (zbookmark_phys_t
));
1508 zc
= kmem_alloc(sizeof (zap_cursor_t
), KM_SLEEP
);
1509 za
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
1511 /* keep pulling things out of the zap-object-as-queue */
1512 while (zap_cursor_init(zc
, dp
->dp_meta_objset
,
1513 scn
->scn_phys
.scn_queue_obj
),
1514 zap_cursor_retrieve(zc
, za
) == 0) {
1518 dsobj
= zfs_strtonum(za
->za_name
, NULL
);
1519 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
1520 scn
->scn_phys
.scn_queue_obj
, dsobj
, tx
));
1522 /* Set up min/max txg */
1523 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp
, dsobj
, FTAG
, &ds
));
1524 if (za
->za_first_integer
!= 0) {
1525 scn
->scn_phys
.scn_cur_min_txg
=
1526 MAX(scn
->scn_phys
.scn_min_txg
,
1527 za
->za_first_integer
);
1529 scn
->scn_phys
.scn_cur_min_txg
=
1530 MAX(scn
->scn_phys
.scn_min_txg
,
1531 dsl_dataset_phys(ds
)->ds_prev_snap_txg
);
1533 scn
->scn_phys
.scn_cur_max_txg
= dsl_scan_ds_maxtxg(ds
);
1534 dsl_dataset_rele(ds
, FTAG
);
1536 dsl_scan_visitds(scn
, dsobj
, tx
);
1537 zap_cursor_fini(zc
);
1538 if (scn
->scn_suspending
)
1541 zap_cursor_fini(zc
);
1543 kmem_free(za
, sizeof (zap_attribute_t
));
1544 kmem_free(zc
, sizeof (zap_cursor_t
));
1548 dsl_scan_free_should_suspend(dsl_scan_t
*scn
)
1550 uint64_t elapsed_nanosecs
;
1555 if (scn
->scn_visited_this_txg
>= zfs_free_max_blocks
)
1558 elapsed_nanosecs
= gethrtime() - scn
->scn_sync_start_time
;
1559 return (elapsed_nanosecs
/ NANOSEC
> zfs_txg_timeout
||
1560 (NSEC2MSEC(elapsed_nanosecs
) > zfs_free_min_time_ms
&&
1561 txg_sync_waiting(scn
->scn_dp
)) ||
1562 spa_shutting_down(scn
->scn_dp
->dp_spa
));
1566 dsl_scan_free_block_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
1568 dsl_scan_t
*scn
= arg
;
1570 if (!scn
->scn_is_bptree
||
1571 (BP_GET_LEVEL(bp
) == 0 && BP_GET_TYPE(bp
) != DMU_OT_OBJSET
)) {
1572 if (dsl_scan_free_should_suspend(scn
))
1573 return (SET_ERROR(ERESTART
));
1576 zio_nowait(zio_free_sync(scn
->scn_zio_root
, scn
->scn_dp
->dp_spa
,
1577 dmu_tx_get_txg(tx
), bp
, 0));
1578 dsl_dir_diduse_space(tx
->tx_pool
->dp_free_dir
, DD_USED_HEAD
,
1579 -bp_get_dsize_sync(scn
->scn_dp
->dp_spa
, bp
),
1580 -BP_GET_PSIZE(bp
), -BP_GET_UCSIZE(bp
), tx
);
1581 scn
->scn_visited_this_txg
++;
1586 dsl_scan_active(dsl_scan_t
*scn
)
1588 spa_t
*spa
= scn
->scn_dp
->dp_spa
;
1589 uint64_t used
= 0, comp
, uncomp
;
1591 if (spa
->spa_load_state
!= SPA_LOAD_NONE
)
1593 if (spa_shutting_down(spa
))
1595 if ((scn
->scn_phys
.scn_state
== DSS_SCANNING
&&
1596 !dsl_scan_is_paused_scrub(scn
)) ||
1597 (scn
->scn_async_destroying
&& !scn
->scn_async_stalled
))
1600 if (spa_version(scn
->scn_dp
->dp_spa
) >= SPA_VERSION_DEADLISTS
) {
1601 (void) bpobj_space(&scn
->scn_dp
->dp_free_bpobj
,
1602 &used
, &comp
, &uncomp
);
1607 /* Called whenever a txg syncs. */
1609 dsl_scan_sync(dsl_pool_t
*dp
, dmu_tx_t
*tx
)
1611 dsl_scan_t
*scn
= dp
->dp_scan
;
1612 spa_t
*spa
= dp
->dp_spa
;
1616 * Check for scn_restart_txg before checking spa_load_state, so
1617 * that we can restart an old-style scan while the pool is being
1618 * imported (see dsl_scan_init).
1620 if (dsl_scan_restarting(scn
, tx
)) {
1621 pool_scan_func_t func
= POOL_SCAN_SCRUB
;
1622 dsl_scan_done(scn
, B_FALSE
, tx
);
1623 if (vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
))
1624 func
= POOL_SCAN_RESILVER
;
1625 zfs_dbgmsg("restarting scan func=%u txg=%llu",
1627 dsl_scan_setup_sync(&func
, tx
);
1631 * Only process scans in sync pass 1.
1633 if (spa_sync_pass(dp
->dp_spa
) > 1)
1637 * If the spa is shutting down, then stop scanning. This will
1638 * ensure that the scan does not dirty any new data during the
1641 if (spa_shutting_down(spa
))
1645 * If the scan is inactive due to a stalled async destroy, try again.
1647 if (!scn
->scn_async_stalled
&& !dsl_scan_active(scn
))
1650 scn
->scn_visited_this_txg
= 0;
1651 scn
->scn_suspending
= B_FALSE
;
1652 scn
->scn_sync_start_time
= gethrtime();
1653 spa
->spa_scrub_active
= B_TRUE
;
1656 * First process the async destroys. If we suspend, don't do
1657 * any scrubbing or resilvering. This ensures that there are no
1658 * async destroys while we are scanning, so the scan code doesn't
1659 * have to worry about traversing it. It is also faster to free the
1660 * blocks than to scrub them.
1662 if (zfs_free_bpobj_enabled
&&
1663 spa_version(dp
->dp_spa
) >= SPA_VERSION_DEADLISTS
) {
1664 scn
->scn_is_bptree
= B_FALSE
;
1665 scn
->scn_zio_root
= zio_root(dp
->dp_spa
, NULL
,
1666 NULL
, ZIO_FLAG_MUSTSUCCEED
);
1667 err
= bpobj_iterate(&dp
->dp_free_bpobj
,
1668 dsl_scan_free_block_cb
, scn
, tx
);
1669 VERIFY3U(0, ==, zio_wait(scn
->scn_zio_root
));
1671 if (err
!= 0 && err
!= ERESTART
)
1672 zfs_panic_recover("error %u from bpobj_iterate()", err
);
1675 if (err
== 0 && spa_feature_is_active(spa
, SPA_FEATURE_ASYNC_DESTROY
)) {
1676 ASSERT(scn
->scn_async_destroying
);
1677 scn
->scn_is_bptree
= B_TRUE
;
1678 scn
->scn_zio_root
= zio_root(dp
->dp_spa
, NULL
,
1679 NULL
, ZIO_FLAG_MUSTSUCCEED
);
1680 err
= bptree_iterate(dp
->dp_meta_objset
,
1681 dp
->dp_bptree_obj
, B_TRUE
, dsl_scan_free_block_cb
, scn
, tx
);
1682 VERIFY0(zio_wait(scn
->scn_zio_root
));
1684 if (err
== EIO
|| err
== ECKSUM
) {
1686 } else if (err
!= 0 && err
!= ERESTART
) {
1687 zfs_panic_recover("error %u from "
1688 "traverse_dataset_destroyed()", err
);
1691 if (bptree_is_empty(dp
->dp_meta_objset
, dp
->dp_bptree_obj
)) {
1692 /* finished; deactivate async destroy feature */
1693 spa_feature_decr(spa
, SPA_FEATURE_ASYNC_DESTROY
, tx
);
1694 ASSERT(!spa_feature_is_active(spa
,
1695 SPA_FEATURE_ASYNC_DESTROY
));
1696 VERIFY0(zap_remove(dp
->dp_meta_objset
,
1697 DMU_POOL_DIRECTORY_OBJECT
,
1698 DMU_POOL_BPTREE_OBJ
, tx
));
1699 VERIFY0(bptree_free(dp
->dp_meta_objset
,
1700 dp
->dp_bptree_obj
, tx
));
1701 dp
->dp_bptree_obj
= 0;
1702 scn
->scn_async_destroying
= B_FALSE
;
1703 scn
->scn_async_stalled
= B_FALSE
;
1706 * If we didn't make progress, mark the async
1707 * destroy as stalled, so that we will not initiate
1708 * a spa_sync() on its behalf. Note that we only
1709 * check this if we are not finished, because if the
1710 * bptree had no blocks for us to visit, we can
1711 * finish without "making progress".
1713 scn
->scn_async_stalled
=
1714 (scn
->scn_visited_this_txg
== 0);
1717 if (scn
->scn_visited_this_txg
) {
1718 zfs_dbgmsg("freed %llu blocks in %llums from "
1719 "free_bpobj/bptree txg %llu; err=%u",
1720 (longlong_t
)scn
->scn_visited_this_txg
,
1722 NSEC2MSEC(gethrtime() - scn
->scn_sync_start_time
),
1723 (longlong_t
)tx
->tx_txg
, err
);
1724 scn
->scn_visited_this_txg
= 0;
1727 * Write out changes to the DDT that may be required as a
1728 * result of the blocks freed. This ensures that the DDT
1729 * is clean when a scrub/resilver runs.
1731 ddt_sync(spa
, tx
->tx_txg
);
1735 if (dp
->dp_free_dir
!= NULL
&& !scn
->scn_async_destroying
&&
1736 zfs_free_leak_on_eio
&&
1737 (dsl_dir_phys(dp
->dp_free_dir
)->dd_used_bytes
!= 0 ||
1738 dsl_dir_phys(dp
->dp_free_dir
)->dd_compressed_bytes
!= 0 ||
1739 dsl_dir_phys(dp
->dp_free_dir
)->dd_uncompressed_bytes
!= 0)) {
1741 * We have finished background destroying, but there is still
1742 * some space left in the dp_free_dir. Transfer this leaked
1743 * space to the dp_leak_dir.
1745 if (dp
->dp_leak_dir
== NULL
) {
1746 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
1747 (void) dsl_dir_create_sync(dp
, dp
->dp_root_dir
,
1749 VERIFY0(dsl_pool_open_special_dir(dp
,
1750 LEAK_DIR_NAME
, &dp
->dp_leak_dir
));
1751 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
1753 dsl_dir_diduse_space(dp
->dp_leak_dir
, DD_USED_HEAD
,
1754 dsl_dir_phys(dp
->dp_free_dir
)->dd_used_bytes
,
1755 dsl_dir_phys(dp
->dp_free_dir
)->dd_compressed_bytes
,
1756 dsl_dir_phys(dp
->dp_free_dir
)->dd_uncompressed_bytes
, tx
);
1757 dsl_dir_diduse_space(dp
->dp_free_dir
, DD_USED_HEAD
,
1758 -dsl_dir_phys(dp
->dp_free_dir
)->dd_used_bytes
,
1759 -dsl_dir_phys(dp
->dp_free_dir
)->dd_compressed_bytes
,
1760 -dsl_dir_phys(dp
->dp_free_dir
)->dd_uncompressed_bytes
, tx
);
1762 if (dp
->dp_free_dir
!= NULL
&& !scn
->scn_async_destroying
) {
1763 /* finished; verify that space accounting went to zero */
1764 ASSERT0(dsl_dir_phys(dp
->dp_free_dir
)->dd_used_bytes
);
1765 ASSERT0(dsl_dir_phys(dp
->dp_free_dir
)->dd_compressed_bytes
);
1766 ASSERT0(dsl_dir_phys(dp
->dp_free_dir
)->dd_uncompressed_bytes
);
1769 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
1772 if (scn
->scn_done_txg
== tx
->tx_txg
) {
1773 ASSERT(!scn
->scn_suspending
);
1774 /* finished with scan. */
1775 zfs_dbgmsg("txg %llu scan complete", tx
->tx_txg
);
1776 dsl_scan_done(scn
, B_TRUE
, tx
);
1777 ASSERT3U(spa
->spa_scrub_inflight
, ==, 0);
1778 dsl_scan_sync_state(scn
, tx
);
1782 if (dsl_scan_is_paused_scrub(scn
))
1785 if (scn
->scn_phys
.scn_ddt_bookmark
.ddb_class
<=
1786 scn
->scn_phys
.scn_ddt_class_max
) {
1787 zfs_dbgmsg("doing scan sync txg %llu; "
1788 "ddt bm=%llu/%llu/%llu/%llx",
1789 (longlong_t
)tx
->tx_txg
,
1790 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_class
,
1791 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_type
,
1792 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_checksum
,
1793 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_cursor
);
1794 ASSERT(scn
->scn_phys
.scn_bookmark
.zb_objset
== 0);
1795 ASSERT(scn
->scn_phys
.scn_bookmark
.zb_object
== 0);
1796 ASSERT(scn
->scn_phys
.scn_bookmark
.zb_level
== 0);
1797 ASSERT(scn
->scn_phys
.scn_bookmark
.zb_blkid
== 0);
1799 zfs_dbgmsg("doing scan sync txg %llu; bm=%llu/%llu/%llu/%llu",
1800 (longlong_t
)tx
->tx_txg
,
1801 (longlong_t
)scn
->scn_phys
.scn_bookmark
.zb_objset
,
1802 (longlong_t
)scn
->scn_phys
.scn_bookmark
.zb_object
,
1803 (longlong_t
)scn
->scn_phys
.scn_bookmark
.zb_level
,
1804 (longlong_t
)scn
->scn_phys
.scn_bookmark
.zb_blkid
);
1807 scn
->scn_zio_root
= zio_root(dp
->dp_spa
, NULL
,
1808 NULL
, ZIO_FLAG_CANFAIL
);
1809 dsl_pool_config_enter(dp
, FTAG
);
1810 dsl_scan_visit(scn
, tx
);
1811 dsl_pool_config_exit(dp
, FTAG
);
1812 (void) zio_wait(scn
->scn_zio_root
);
1813 scn
->scn_zio_root
= NULL
;
1815 zfs_dbgmsg("visited %llu blocks in %llums",
1816 (longlong_t
)scn
->scn_visited_this_txg
,
1817 (longlong_t
)NSEC2MSEC(gethrtime() - scn
->scn_sync_start_time
));
1819 if (!scn
->scn_suspending
) {
1820 scn
->scn_done_txg
= tx
->tx_txg
+ 1;
1821 zfs_dbgmsg("txg %llu traversal complete, waiting till txg %llu",
1822 tx
->tx_txg
, scn
->scn_done_txg
);
1825 if (DSL_SCAN_IS_SCRUB_RESILVER(scn
)) {
1826 mutex_enter(&spa
->spa_scrub_lock
);
1827 while (spa
->spa_scrub_inflight
> 0) {
1828 cv_wait(&spa
->spa_scrub_io_cv
,
1829 &spa
->spa_scrub_lock
);
1831 mutex_exit(&spa
->spa_scrub_lock
);
1834 dsl_scan_sync_state(scn
, tx
);
1838 * This will start a new scan, or restart an existing one.
1841 dsl_resilver_restart(dsl_pool_t
*dp
, uint64_t txg
)
1845 tx
= dmu_tx_create_dd(dp
->dp_mos_dir
);
1846 VERIFY(0 == dmu_tx_assign(tx
, TXG_WAIT
));
1848 txg
= dmu_tx_get_txg(tx
);
1849 dp
->dp_scan
->scn_restart_txg
= txg
;
1852 dp
->dp_scan
->scn_restart_txg
= txg
;
1854 zfs_dbgmsg("restarting resilver txg=%llu", txg
);
1858 dsl_scan_resilvering(dsl_pool_t
*dp
)
1860 return (dp
->dp_scan
->scn_phys
.scn_state
== DSS_SCANNING
&&
1861 dp
->dp_scan
->scn_phys
.scn_func
== POOL_SCAN_RESILVER
);
1869 count_block(zfs_all_blkstats_t
*zab
, const blkptr_t
*bp
)
1874 * If we resume after a reboot, zab will be NULL; don't record
1875 * incomplete stats in that case.
1880 for (i
= 0; i
< 4; i
++) {
1881 int l
= (i
< 2) ? BP_GET_LEVEL(bp
) : DN_MAX_LEVELS
;
1882 int t
= (i
& 1) ? BP_GET_TYPE(bp
) : DMU_OT_TOTAL
;
1886 if (t
& DMU_OT_NEWTYPE
)
1889 zb
= &zab
->zab_type
[l
][t
];
1891 zb
->zb_asize
+= BP_GET_ASIZE(bp
);
1892 zb
->zb_lsize
+= BP_GET_LSIZE(bp
);
1893 zb
->zb_psize
+= BP_GET_PSIZE(bp
);
1894 zb
->zb_gangs
+= BP_COUNT_GANG(bp
);
1896 switch (BP_GET_NDVAS(bp
)) {
1898 if (DVA_GET_VDEV(&bp
->blk_dva
[0]) ==
1899 DVA_GET_VDEV(&bp
->blk_dva
[1]))
1900 zb
->zb_ditto_2_of_2_samevdev
++;
1903 equal
= (DVA_GET_VDEV(&bp
->blk_dva
[0]) ==
1904 DVA_GET_VDEV(&bp
->blk_dva
[1])) +
1905 (DVA_GET_VDEV(&bp
->blk_dva
[0]) ==
1906 DVA_GET_VDEV(&bp
->blk_dva
[2])) +
1907 (DVA_GET_VDEV(&bp
->blk_dva
[1]) ==
1908 DVA_GET_VDEV(&bp
->blk_dva
[2]));
1910 zb
->zb_ditto_2_of_3_samevdev
++;
1911 else if (equal
== 3)
1912 zb
->zb_ditto_3_of_3_samevdev
++;
1919 dsl_scan_scrub_done(zio_t
*zio
)
1921 spa_t
*spa
= zio
->io_spa
;
1923 abd_free(zio
->io_abd
);
1925 mutex_enter(&spa
->spa_scrub_lock
);
1926 spa
->spa_scrub_inflight
--;
1927 cv_broadcast(&spa
->spa_scrub_io_cv
);
1929 if (zio
->io_error
&& (zio
->io_error
!= ECKSUM
||
1930 !(zio
->io_flags
& ZIO_FLAG_SPECULATIVE
))) {
1931 spa
->spa_dsl_pool
->dp_scan
->scn_phys
.scn_errors
++;
1933 mutex_exit(&spa
->spa_scrub_lock
);
1937 dsl_scan_need_resilver(spa_t
*spa
, const dva_t
*dva
, size_t psize
,
1938 uint64_t phys_birth
)
1942 if (DVA_GET_GANG(dva
)) {
1944 * Gang members may be spread across multiple
1945 * vdevs, so the best estimate we have is the
1946 * scrub range, which has already been checked.
1947 * XXX -- it would be better to change our
1948 * allocation policy to ensure that all
1949 * gang members reside on the same vdev.
1954 vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(dva
));
1957 * Check if the txg falls within the range which must be
1958 * resilvered. DVAs outside this range can always be skipped.
1960 if (!vdev_dtl_contains(vd
, DTL_PARTIAL
, phys_birth
, 1))
1964 * Check if the top-level vdev must resilver this offset.
1965 * When the offset does not intersect with a dirty leaf DTL
1966 * then it may be possible to skip the resilver IO. The psize
1967 * is provided instead of asize to simplify the check for RAIDZ.
1969 if (!vdev_dtl_need_resilver(vd
, DVA_GET_OFFSET(dva
), psize
))
1976 dsl_scan_scrub_cb(dsl_pool_t
*dp
,
1977 const blkptr_t
*bp
, const zbookmark_phys_t
*zb
)
1979 dsl_scan_t
*scn
= dp
->dp_scan
;
1980 size_t psize
= BP_GET_PSIZE(bp
);
1981 spa_t
*spa
= dp
->dp_spa
;
1982 uint64_t phys_birth
= BP_PHYSICAL_BIRTH(bp
);
1983 boolean_t needs_io
= B_FALSE
;
1984 int zio_flags
= ZIO_FLAG_SCAN_THREAD
| ZIO_FLAG_RAW
| ZIO_FLAG_CANFAIL
;
1988 if (phys_birth
<= scn
->scn_phys
.scn_min_txg
||
1989 phys_birth
>= scn
->scn_phys
.scn_max_txg
)
1992 count_block(dp
->dp_blkstats
, bp
);
1994 if (BP_IS_EMBEDDED(bp
))
1997 ASSERT(DSL_SCAN_IS_SCRUB_RESILVER(scn
));
1998 if (scn
->scn_phys
.scn_func
== POOL_SCAN_SCRUB
) {
1999 zio_flags
|= ZIO_FLAG_SCRUB
;
2001 scan_delay
= zfs_scrub_delay
;
2003 ASSERT3U(scn
->scn_phys
.scn_func
, ==, POOL_SCAN_RESILVER
);
2004 zio_flags
|= ZIO_FLAG_RESILVER
;
2006 scan_delay
= zfs_resilver_delay
;
2009 /* If it's an intent log block, failure is expected. */
2010 if (zb
->zb_level
== ZB_ZIL_LEVEL
)
2011 zio_flags
|= ZIO_FLAG_SPECULATIVE
;
2013 for (d
= 0; d
< BP_GET_NDVAS(bp
); d
++) {
2014 const dva_t
*dva
= &bp
->blk_dva
[d
];
2017 * Keep track of how much data we've examined so that
2018 * zpool(1M) status can make useful progress reports.
2020 scn
->scn_phys
.scn_examined
+= DVA_GET_ASIZE(dva
);
2021 spa
->spa_scan_pass_exam
+= DVA_GET_ASIZE(dva
);
2023 /* if it's a resilver, this may not be in the target range */
2025 needs_io
= dsl_scan_need_resilver(spa
, dva
, psize
,
2029 if (needs_io
&& !zfs_no_scrub_io
) {
2030 vdev_t
*rvd
= spa
->spa_root_vdev
;
2031 uint64_t maxinflight
= rvd
->vdev_children
* zfs_top_maxinflight
;
2033 mutex_enter(&spa
->spa_scrub_lock
);
2034 while (spa
->spa_scrub_inflight
>= maxinflight
)
2035 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
2036 spa
->spa_scrub_inflight
++;
2037 mutex_exit(&spa
->spa_scrub_lock
);
2040 * If we're seeing recent (zfs_scan_idle) "important" I/Os
2041 * then throttle our workload to limit the impact of a scan.
2043 if (ddi_get_lbolt64() - spa
->spa_last_io
<= zfs_scan_idle
)
2046 zio_nowait(zio_read(NULL
, spa
, bp
,
2047 abd_alloc_for_io(psize
, B_FALSE
),
2048 psize
, dsl_scan_scrub_done
, NULL
,
2049 ZIO_PRIORITY_SCRUB
, zio_flags
, zb
));
2052 /* do not relocate this block */
2057 * Called by the ZFS_IOC_POOL_SCAN ioctl to start a scrub or resilver.
2058 * Can also be called to resume a paused scrub.
2061 dsl_scan(dsl_pool_t
*dp
, pool_scan_func_t func
)
2063 spa_t
*spa
= dp
->dp_spa
;
2064 dsl_scan_t
*scn
= dp
->dp_scan
;
2067 * Purge all vdev caches and probe all devices. We do this here
2068 * rather than in sync context because this requires a writer lock
2069 * on the spa_config lock, which we can't do from sync context. The
2070 * spa_scrub_reopen flag indicates that vdev_open() should not
2071 * attempt to start another scrub.
2073 spa_vdev_state_enter(spa
, SCL_NONE
);
2074 spa
->spa_scrub_reopen
= B_TRUE
;
2075 vdev_reopen(spa
->spa_root_vdev
);
2076 spa
->spa_scrub_reopen
= B_FALSE
;
2077 (void) spa_vdev_state_exit(spa
, NULL
, 0);
2079 if (func
== POOL_SCAN_SCRUB
&& dsl_scan_is_paused_scrub(scn
)) {
2080 /* got scrub start cmd, resume paused scrub */
2081 int err
= dsl_scrub_set_pause_resume(scn
->scn_dp
,
2086 return (SET_ERROR(err
));
2089 return (dsl_sync_task(spa_name(spa
), dsl_scan_setup_check
,
2090 dsl_scan_setup_sync
, &func
, 0, ZFS_SPACE_CHECK_NONE
));
2094 dsl_scan_restarting(dsl_scan_t
*scn
, dmu_tx_t
*tx
)
2096 return (scn
->scn_restart_txg
!= 0 &&
2097 scn
->scn_restart_txg
<= tx
->tx_txg
);
2100 #if defined(_KERNEL) && defined(HAVE_SPL)
2101 module_param(zfs_top_maxinflight
, int, 0644);
2102 MODULE_PARM_DESC(zfs_top_maxinflight
, "Max I/Os per top-level");
2104 module_param(zfs_resilver_delay
, int, 0644);
2105 MODULE_PARM_DESC(zfs_resilver_delay
, "Number of ticks to delay resilver");
2107 module_param(zfs_scrub_delay
, int, 0644);
2108 MODULE_PARM_DESC(zfs_scrub_delay
, "Number of ticks to delay scrub");
2110 module_param(zfs_scan_idle
, int, 0644);
2111 MODULE_PARM_DESC(zfs_scan_idle
, "Idle window in clock ticks");
2113 module_param(zfs_scan_min_time_ms
, int, 0644);
2114 MODULE_PARM_DESC(zfs_scan_min_time_ms
, "Min millisecs to scrub per txg");
2116 module_param(zfs_free_min_time_ms
, int, 0644);
2117 MODULE_PARM_DESC(zfs_free_min_time_ms
, "Min millisecs to free per txg");
2119 module_param(zfs_resilver_min_time_ms
, int, 0644);
2120 MODULE_PARM_DESC(zfs_resilver_min_time_ms
, "Min millisecs to resilver per txg");
2122 module_param(zfs_no_scrub_io
, int, 0644);
2123 MODULE_PARM_DESC(zfs_no_scrub_io
, "Set to disable scrub I/O");
2125 module_param(zfs_no_scrub_prefetch
, int, 0644);
2126 MODULE_PARM_DESC(zfs_no_scrub_prefetch
, "Set to disable scrub prefetching");
2129 module_param(zfs_free_max_blocks
, ulong
, 0644);
2130 MODULE_PARM_DESC(zfs_free_max_blocks
, "Max number of blocks freed in one txg");
2132 module_param(zfs_free_bpobj_enabled
, int, 0644);
2133 MODULE_PARM_DESC(zfs_free_bpobj_enabled
, "Enable processing of the free_bpobj");