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.
24 * Copyright 2016 Gary Mills
27 #include <sys/dsl_scan.h>
28 #include <sys/dsl_pool.h>
29 #include <sys/dsl_dataset.h>
30 #include <sys/dsl_prop.h>
31 #include <sys/dsl_dir.h>
32 #include <sys/dsl_synctask.h>
33 #include <sys/dnode.h>
34 #include <sys/dmu_tx.h>
35 #include <sys/dmu_objset.h>
39 #include <sys/zfs_context.h>
40 #include <sys/fs/zfs.h>
41 #include <sys/zfs_znode.h>
42 #include <sys/spa_impl.h>
43 #include <sys/vdev_impl.h>
44 #include <sys/zil_impl.h>
45 #include <sys/zio_checksum.h>
48 #include <sys/sa_impl.h>
49 #include <sys/zfeature.h>
51 #include <sys/zfs_vfsops.h>
54 typedef int (scan_cb_t
)(dsl_pool_t
*, const blkptr_t
*,
55 const zbookmark_phys_t
*);
57 static scan_cb_t dsl_scan_scrub_cb
;
58 static void dsl_scan_cancel_sync(void *, dmu_tx_t
*);
59 static void dsl_scan_sync_state(dsl_scan_t
*, dmu_tx_t
*tx
);
61 int zfs_top_maxinflight
= 32; /* maximum I/Os per top-level */
62 int zfs_resilver_delay
= 2; /* number of ticks to delay resilver */
63 int zfs_scrub_delay
= 4; /* number of ticks to delay scrub */
64 int zfs_scan_idle
= 50; /* idle window in clock ticks */
66 int zfs_scan_min_time_ms
= 1000; /* min millisecs to scrub per txg */
67 int zfs_free_min_time_ms
= 1000; /* min millisecs to free per txg */
68 int zfs_resilver_min_time_ms
= 3000; /* min millisecs to resilver per txg */
69 int zfs_no_scrub_io
= B_FALSE
; /* set to disable scrub i/o */
70 int zfs_no_scrub_prefetch
= B_FALSE
; /* set to disable scrub prefetch */
71 enum ddt_class zfs_scrub_ddt_class_max
= DDT_CLASS_DUPLICATE
;
72 int dsl_scan_delay_completion
= B_FALSE
; /* set to delay scan completion */
73 /* max number of blocks to free in a single TXG */
74 ulong zfs_free_max_blocks
= 100000;
76 #define DSL_SCAN_IS_SCRUB_RESILVER(scn) \
77 ((scn)->scn_phys.scn_func == POOL_SCAN_SCRUB || \
78 (scn)->scn_phys.scn_func == POOL_SCAN_RESILVER)
81 * Enable/disable the processing of the free_bpobj object.
83 int zfs_free_bpobj_enabled
= 1;
85 /* the order has to match pool_scan_type */
86 static scan_cb_t
*scan_funcs
[POOL_SCAN_FUNCS
] = {
88 dsl_scan_scrub_cb
, /* POOL_SCAN_SCRUB */
89 dsl_scan_scrub_cb
, /* POOL_SCAN_RESILVER */
93 dsl_scan_init(dsl_pool_t
*dp
, uint64_t txg
)
97 spa_t
*spa
= dp
->dp_spa
;
100 scn
= dp
->dp_scan
= kmem_zalloc(sizeof (dsl_scan_t
), KM_SLEEP
);
104 * It's possible that we're resuming a scan after a reboot so
105 * make sure that the scan_async_destroying flag is initialized
108 ASSERT(!scn
->scn_async_destroying
);
109 scn
->scn_async_destroying
= spa_feature_is_active(dp
->dp_spa
,
110 SPA_FEATURE_ASYNC_DESTROY
);
112 err
= zap_lookup(dp
->dp_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
113 "scrub_func", sizeof (uint64_t), 1, &f
);
116 * There was an old-style scrub in progress. Restart a
117 * new-style scrub from the beginning.
119 scn
->scn_restart_txg
= txg
;
120 zfs_dbgmsg("old-style scrub was in progress; "
121 "restarting new-style scrub in txg %llu",
122 scn
->scn_restart_txg
);
125 * Load the queue obj from the old location so that it
126 * can be freed by dsl_scan_done().
128 (void) zap_lookup(dp
->dp_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
129 "scrub_queue", sizeof (uint64_t), 1,
130 &scn
->scn_phys
.scn_queue_obj
);
132 err
= zap_lookup(dp
->dp_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
133 DMU_POOL_SCAN
, sizeof (uint64_t), SCAN_PHYS_NUMINTS
,
136 * Detect if the pool contains the signature of #2094. If it
137 * does properly update the scn->scn_phys structure and notify
138 * the administrator by setting an errata for the pool.
140 if (err
== EOVERFLOW
) {
141 uint64_t zaptmp
[SCAN_PHYS_NUMINTS
+ 1];
142 VERIFY3S(SCAN_PHYS_NUMINTS
, ==, 24);
143 VERIFY3S(offsetof(dsl_scan_phys_t
, scn_flags
), ==,
144 (23 * sizeof (uint64_t)));
146 err
= zap_lookup(dp
->dp_meta_objset
,
147 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SCAN
,
148 sizeof (uint64_t), SCAN_PHYS_NUMINTS
+ 1, &zaptmp
);
150 uint64_t overflow
= zaptmp
[SCAN_PHYS_NUMINTS
];
152 if (overflow
& ~DSL_SCAN_FLAGS_MASK
||
153 scn
->scn_async_destroying
) {
155 ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY
;
159 bcopy(zaptmp
, &scn
->scn_phys
,
160 SCAN_PHYS_NUMINTS
* sizeof (uint64_t));
161 scn
->scn_phys
.scn_flags
= overflow
;
163 /* Required scrub already in progress. */
164 if (scn
->scn_phys
.scn_state
== DSS_FINISHED
||
165 scn
->scn_phys
.scn_state
== DSS_CANCELED
)
167 ZPOOL_ERRATA_ZOL_2094_SCRUB
;
176 if (scn
->scn_phys
.scn_state
== DSS_SCANNING
&&
177 spa_prev_software_version(dp
->dp_spa
) < SPA_VERSION_SCAN
) {
179 * A new-type scrub was in progress on an old
180 * pool, and the pool was accessed by old
181 * software. Restart from the beginning, since
182 * the old software may have changed the pool in
185 scn
->scn_restart_txg
= txg
;
186 zfs_dbgmsg("new-style scrub was modified "
187 "by old software; restarting in txg %llu",
188 scn
->scn_restart_txg
);
192 spa_scan_stat_init(spa
);
197 dsl_scan_fini(dsl_pool_t
*dp
)
200 kmem_free(dp
->dp_scan
, sizeof (dsl_scan_t
));
207 dsl_scan_setup_check(void *arg
, dmu_tx_t
*tx
)
209 dsl_scan_t
*scn
= dmu_tx_pool(tx
)->dp_scan
;
211 if (scn
->scn_phys
.scn_state
== DSS_SCANNING
)
212 return (SET_ERROR(EBUSY
));
218 dsl_scan_setup_sync(void *arg
, dmu_tx_t
*tx
)
220 dsl_scan_t
*scn
= dmu_tx_pool(tx
)->dp_scan
;
221 pool_scan_func_t
*funcp
= arg
;
222 dmu_object_type_t ot
= 0;
223 dsl_pool_t
*dp
= scn
->scn_dp
;
224 spa_t
*spa
= dp
->dp_spa
;
226 ASSERT(scn
->scn_phys
.scn_state
!= DSS_SCANNING
);
227 ASSERT(*funcp
> POOL_SCAN_NONE
&& *funcp
< POOL_SCAN_FUNCS
);
228 bzero(&scn
->scn_phys
, sizeof (scn
->scn_phys
));
229 scn
->scn_phys
.scn_func
= *funcp
;
230 scn
->scn_phys
.scn_state
= DSS_SCANNING
;
231 scn
->scn_phys
.scn_min_txg
= 0;
232 scn
->scn_phys
.scn_max_txg
= tx
->tx_txg
;
233 scn
->scn_phys
.scn_ddt_class_max
= DDT_CLASSES
- 1; /* the entire DDT */
234 scn
->scn_phys
.scn_start_time
= gethrestime_sec();
235 scn
->scn_phys
.scn_errors
= 0;
236 scn
->scn_phys
.scn_to_examine
= spa
->spa_root_vdev
->vdev_stat
.vs_alloc
;
237 scn
->scn_restart_txg
= 0;
238 scn
->scn_done_txg
= 0;
239 spa_scan_stat_init(spa
);
241 if (DSL_SCAN_IS_SCRUB_RESILVER(scn
)) {
242 scn
->scn_phys
.scn_ddt_class_max
= zfs_scrub_ddt_class_max
;
244 /* rewrite all disk labels */
245 vdev_config_dirty(spa
->spa_root_vdev
);
247 if (vdev_resilver_needed(spa
->spa_root_vdev
,
248 &scn
->scn_phys
.scn_min_txg
, &scn
->scn_phys
.scn_max_txg
)) {
249 spa_event_notify(spa
, NULL
,
250 FM_EREPORT_ZFS_RESILVER_START
);
252 spa_event_notify(spa
, NULL
,
253 FM_EREPORT_ZFS_SCRUB_START
);
256 spa
->spa_scrub_started
= B_TRUE
;
258 * If this is an incremental scrub, limit the DDT scrub phase
259 * to just the auto-ditto class (for correctness); the rest
260 * of the scrub should go faster using top-down pruning.
262 if (scn
->scn_phys
.scn_min_txg
> TXG_INITIAL
)
263 scn
->scn_phys
.scn_ddt_class_max
= DDT_CLASS_DITTO
;
267 /* back to the generic stuff */
269 if (dp
->dp_blkstats
== NULL
) {
271 vmem_alloc(sizeof (zfs_all_blkstats_t
), KM_SLEEP
);
273 bzero(dp
->dp_blkstats
, sizeof (zfs_all_blkstats_t
));
275 if (spa_version(spa
) < SPA_VERSION_DSL_SCRUB
)
276 ot
= DMU_OT_ZAP_OTHER
;
278 scn
->scn_phys
.scn_queue_obj
= zap_create(dp
->dp_meta_objset
,
279 ot
? ot
: DMU_OT_SCAN_QUEUE
, DMU_OT_NONE
, 0, tx
);
281 dsl_scan_sync_state(scn
, tx
);
283 spa_history_log_internal(spa
, "scan setup", tx
,
284 "func=%u mintxg=%llu maxtxg=%llu",
285 *funcp
, scn
->scn_phys
.scn_min_txg
, scn
->scn_phys
.scn_max_txg
);
290 dsl_scan_done(dsl_scan_t
*scn
, boolean_t complete
, dmu_tx_t
*tx
)
292 static const char *old_names
[] = {
294 "scrub_ddt_bookmark",
295 "scrub_ddt_class_max",
304 dsl_pool_t
*dp
= scn
->scn_dp
;
305 spa_t
*spa
= dp
->dp_spa
;
308 /* Remove any remnants of an old-style scrub. */
309 for (i
= 0; old_names
[i
]; i
++) {
310 (void) zap_remove(dp
->dp_meta_objset
,
311 DMU_POOL_DIRECTORY_OBJECT
, old_names
[i
], tx
);
314 if (scn
->scn_phys
.scn_queue_obj
!= 0) {
315 VERIFY(0 == dmu_object_free(dp
->dp_meta_objset
,
316 scn
->scn_phys
.scn_queue_obj
, tx
));
317 scn
->scn_phys
.scn_queue_obj
= 0;
321 * If we were "restarted" from a stopped state, don't bother
322 * with anything else.
324 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
328 scn
->scn_phys
.scn_state
= DSS_FINISHED
;
330 scn
->scn_phys
.scn_state
= DSS_CANCELED
;
332 spa_history_log_internal(spa
, "scan done", tx
,
333 "complete=%u", complete
);
335 if (DSL_SCAN_IS_SCRUB_RESILVER(scn
)) {
336 mutex_enter(&spa
->spa_scrub_lock
);
337 while (spa
->spa_scrub_inflight
> 0) {
338 cv_wait(&spa
->spa_scrub_io_cv
,
339 &spa
->spa_scrub_lock
);
341 mutex_exit(&spa
->spa_scrub_lock
);
342 spa
->spa_scrub_started
= B_FALSE
;
343 spa
->spa_scrub_active
= B_FALSE
;
346 * If the scrub/resilver completed, update all DTLs to
347 * reflect this. Whether it succeeded or not, vacate
348 * all temporary scrub DTLs.
350 vdev_dtl_reassess(spa
->spa_root_vdev
, tx
->tx_txg
,
351 complete
? scn
->scn_phys
.scn_max_txg
: 0, B_TRUE
);
353 spa_event_notify(spa
, NULL
, scn
->scn_phys
.scn_min_txg
?
354 FM_EREPORT_ZFS_RESILVER_FINISH
:
355 FM_EREPORT_ZFS_SCRUB_FINISH
);
357 spa_errlog_rotate(spa
);
360 * We may have finished replacing a device.
361 * Let the async thread assess this and handle the detach.
363 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
366 scn
->scn_phys
.scn_end_time
= gethrestime_sec();
368 if (spa
->spa_errata
== ZPOOL_ERRATA_ZOL_2094_SCRUB
)
374 dsl_scan_cancel_check(void *arg
, dmu_tx_t
*tx
)
376 dsl_scan_t
*scn
= dmu_tx_pool(tx
)->dp_scan
;
378 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
379 return (SET_ERROR(ENOENT
));
385 dsl_scan_cancel_sync(void *arg
, dmu_tx_t
*tx
)
387 dsl_scan_t
*scn
= dmu_tx_pool(tx
)->dp_scan
;
389 dsl_scan_done(scn
, B_FALSE
, tx
);
390 dsl_scan_sync_state(scn
, tx
);
394 dsl_scan_cancel(dsl_pool_t
*dp
)
396 return (dsl_sync_task(spa_name(dp
->dp_spa
), dsl_scan_cancel_check
,
397 dsl_scan_cancel_sync
, NULL
, 3, ZFS_SPACE_CHECK_RESERVED
));
400 static void dsl_scan_visitbp(blkptr_t
*bp
, const zbookmark_phys_t
*zb
,
401 dnode_phys_t
*dnp
, dsl_dataset_t
*ds
, dsl_scan_t
*scn
,
402 dmu_objset_type_t ostype
, dmu_tx_t
*tx
);
403 inline __attribute__((always_inline
)) static void dsl_scan_visitdnode(
404 dsl_scan_t
*, dsl_dataset_t
*ds
, dmu_objset_type_t ostype
,
405 dnode_phys_t
*dnp
, uint64_t object
, dmu_tx_t
*tx
);
408 dsl_free(dsl_pool_t
*dp
, uint64_t txg
, const blkptr_t
*bp
)
410 zio_free(dp
->dp_spa
, txg
, bp
);
414 dsl_free_sync(zio_t
*pio
, dsl_pool_t
*dp
, uint64_t txg
, const blkptr_t
*bpp
)
416 ASSERT(dsl_pool_sync_context(dp
));
417 zio_nowait(zio_free_sync(pio
, dp
->dp_spa
, txg
, bpp
, pio
->io_flags
));
421 dsl_scan_ds_maxtxg(dsl_dataset_t
*ds
)
423 uint64_t smt
= ds
->ds_dir
->dd_pool
->dp_scan
->scn_phys
.scn_max_txg
;
424 if (ds
->ds_is_snapshot
)
425 return (MIN(smt
, dsl_dataset_phys(ds
)->ds_creation_txg
));
430 dsl_scan_sync_state(dsl_scan_t
*scn
, dmu_tx_t
*tx
)
432 VERIFY0(zap_update(scn
->scn_dp
->dp_meta_objset
,
433 DMU_POOL_DIRECTORY_OBJECT
,
434 DMU_POOL_SCAN
, sizeof (uint64_t), SCAN_PHYS_NUMINTS
,
435 &scn
->scn_phys
, tx
));
438 extern int zfs_vdev_async_write_active_min_dirty_percent
;
441 dsl_scan_check_pause(dsl_scan_t
*scn
, const zbookmark_phys_t
*zb
)
443 uint64_t elapsed_nanosecs
;
447 /* we never skip user/group accounting objects */
448 if (zb
&& (int64_t)zb
->zb_object
< 0)
451 if (scn
->scn_pausing
)
452 return (B_TRUE
); /* we're already pausing */
454 if (!ZB_IS_ZERO(&scn
->scn_phys
.scn_bookmark
))
455 return (B_FALSE
); /* we're resuming */
457 /* We only know how to resume from level-0 blocks. */
458 if (zb
&& zb
->zb_level
!= 0)
463 * - we have scanned for the maximum time: an entire txg
464 * timeout (default 5 sec)
466 * - we have scanned for at least the minimum time (default 1 sec
467 * for scrub, 3 sec for resilver), and either we have sufficient
468 * dirty data that we are starting to write more quickly
469 * (default 30%), or someone is explicitly waiting for this txg
472 * - the spa is shutting down because this pool is being exported
473 * or the machine is rebooting.
475 mintime
= (scn
->scn_phys
.scn_func
== POOL_SCAN_RESILVER
) ?
476 zfs_resilver_min_time_ms
: zfs_scan_min_time_ms
;
477 elapsed_nanosecs
= gethrtime() - scn
->scn_sync_start_time
;
478 dirty_pct
= scn
->scn_dp
->dp_dirty_total
* 100 / zfs_dirty_data_max
;
479 if (elapsed_nanosecs
/ NANOSEC
>= zfs_txg_timeout
||
480 (NSEC2MSEC(elapsed_nanosecs
) > mintime
&&
481 (txg_sync_waiting(scn
->scn_dp
) ||
482 dirty_pct
>= zfs_vdev_async_write_active_min_dirty_percent
)) ||
483 spa_shutting_down(scn
->scn_dp
->dp_spa
)) {
485 dprintf("pausing at bookmark %llx/%llx/%llx/%llx\n",
486 (longlong_t
)zb
->zb_objset
,
487 (longlong_t
)zb
->zb_object
,
488 (longlong_t
)zb
->zb_level
,
489 (longlong_t
)zb
->zb_blkid
);
490 scn
->scn_phys
.scn_bookmark
= *zb
;
492 dprintf("pausing at DDT bookmark %llx/%llx/%llx/%llx\n",
493 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_class
,
494 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_type
,
495 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_checksum
,
496 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_cursor
);
497 scn
->scn_pausing
= B_TRUE
;
503 typedef struct zil_scan_arg
{
505 zil_header_t
*zsa_zh
;
510 dsl_scan_zil_block(zilog_t
*zilog
, blkptr_t
*bp
, void *arg
, uint64_t claim_txg
)
512 zil_scan_arg_t
*zsa
= arg
;
513 dsl_pool_t
*dp
= zsa
->zsa_dp
;
514 dsl_scan_t
*scn
= dp
->dp_scan
;
515 zil_header_t
*zh
= zsa
->zsa_zh
;
518 if (BP_IS_HOLE(bp
) || bp
->blk_birth
<= scn
->scn_phys
.scn_cur_min_txg
)
522 * One block ("stubby") can be allocated a long time ago; we
523 * want to visit that one because it has been allocated
524 * (on-disk) even if it hasn't been claimed (even though for
525 * scrub there's nothing to do to it).
527 if (claim_txg
== 0 && bp
->blk_birth
>= spa_first_txg(dp
->dp_spa
))
530 SET_BOOKMARK(&zb
, zh
->zh_log
.blk_cksum
.zc_word
[ZIL_ZC_OBJSET
],
531 ZB_ZIL_OBJECT
, ZB_ZIL_LEVEL
, bp
->blk_cksum
.zc_word
[ZIL_ZC_SEQ
]);
533 VERIFY(0 == scan_funcs
[scn
->scn_phys
.scn_func
](dp
, bp
, &zb
));
539 dsl_scan_zil_record(zilog_t
*zilog
, lr_t
*lrc
, void *arg
, uint64_t claim_txg
)
541 if (lrc
->lrc_txtype
== TX_WRITE
) {
542 zil_scan_arg_t
*zsa
= arg
;
543 dsl_pool_t
*dp
= zsa
->zsa_dp
;
544 dsl_scan_t
*scn
= dp
->dp_scan
;
545 zil_header_t
*zh
= zsa
->zsa_zh
;
546 lr_write_t
*lr
= (lr_write_t
*)lrc
;
547 blkptr_t
*bp
= &lr
->lr_blkptr
;
550 if (BP_IS_HOLE(bp
) ||
551 bp
->blk_birth
<= scn
->scn_phys
.scn_cur_min_txg
)
555 * birth can be < claim_txg if this record's txg is
556 * already txg sync'ed (but this log block contains
557 * other records that are not synced)
559 if (claim_txg
== 0 || bp
->blk_birth
< claim_txg
)
562 SET_BOOKMARK(&zb
, zh
->zh_log
.blk_cksum
.zc_word
[ZIL_ZC_OBJSET
],
563 lr
->lr_foid
, ZB_ZIL_LEVEL
,
564 lr
->lr_offset
/ BP_GET_LSIZE(bp
));
566 VERIFY(0 == scan_funcs
[scn
->scn_phys
.scn_func
](dp
, bp
, &zb
));
572 dsl_scan_zil(dsl_pool_t
*dp
, zil_header_t
*zh
)
574 uint64_t claim_txg
= zh
->zh_claim_txg
;
575 zil_scan_arg_t zsa
= { dp
, zh
};
579 * We only want to visit blocks that have been claimed but not yet
580 * replayed (or, in read-only mode, blocks that *would* be claimed).
582 if (claim_txg
== 0 && spa_writeable(dp
->dp_spa
))
585 zilog
= zil_alloc(dp
->dp_meta_objset
, zh
);
587 (void) zil_parse(zilog
, dsl_scan_zil_block
, dsl_scan_zil_record
, &zsa
,
595 dsl_scan_prefetch(dsl_scan_t
*scn
, arc_buf_t
*buf
, blkptr_t
*bp
,
596 uint64_t objset
, uint64_t object
, uint64_t blkid
)
598 zbookmark_phys_t czb
;
599 arc_flags_t flags
= ARC_FLAG_NOWAIT
| ARC_FLAG_PREFETCH
;
601 if (zfs_no_scrub_prefetch
)
604 if (BP_IS_HOLE(bp
) || bp
->blk_birth
<= scn
->scn_phys
.scn_min_txg
||
605 (BP_GET_LEVEL(bp
) == 0 && BP_GET_TYPE(bp
) != DMU_OT_DNODE
))
608 SET_BOOKMARK(&czb
, objset
, object
, BP_GET_LEVEL(bp
), blkid
);
610 (void) arc_read(scn
->scn_zio_root
, scn
->scn_dp
->dp_spa
, bp
,
611 NULL
, NULL
, ZIO_PRIORITY_ASYNC_READ
,
612 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SCAN_THREAD
, &flags
, &czb
);
616 dsl_scan_check_resume(dsl_scan_t
*scn
, const dnode_phys_t
*dnp
,
617 const zbookmark_phys_t
*zb
)
620 * We never skip over user/group accounting objects (obj<0)
622 if (!ZB_IS_ZERO(&scn
->scn_phys
.scn_bookmark
) &&
623 (int64_t)zb
->zb_object
>= 0) {
625 * If we already visited this bp & everything below (in
626 * a prior txg sync), don't bother doing it again.
628 if (zbookmark_subtree_completed(dnp
, zb
,
629 &scn
->scn_phys
.scn_bookmark
))
633 * If we found the block we're trying to resume from, or
634 * we went past it to a different object, zero it out to
635 * indicate that it's OK to start checking for pausing
638 if (bcmp(zb
, &scn
->scn_phys
.scn_bookmark
, sizeof (*zb
)) == 0 ||
639 zb
->zb_object
> scn
->scn_phys
.scn_bookmark
.zb_object
) {
640 dprintf("resuming at %llx/%llx/%llx/%llx\n",
641 (longlong_t
)zb
->zb_objset
,
642 (longlong_t
)zb
->zb_object
,
643 (longlong_t
)zb
->zb_level
,
644 (longlong_t
)zb
->zb_blkid
);
645 bzero(&scn
->scn_phys
.scn_bookmark
, sizeof (*zb
));
652 * Return nonzero on i/o error.
653 * Return new buf to write out in *bufp.
655 inline __attribute__((always_inline
)) static int
656 dsl_scan_recurse(dsl_scan_t
*scn
, dsl_dataset_t
*ds
, dmu_objset_type_t ostype
,
657 dnode_phys_t
*dnp
, const blkptr_t
*bp
,
658 const zbookmark_phys_t
*zb
, dmu_tx_t
*tx
)
660 dsl_pool_t
*dp
= scn
->scn_dp
;
661 int zio_flags
= ZIO_FLAG_CANFAIL
| ZIO_FLAG_SCAN_THREAD
;
664 if (BP_GET_LEVEL(bp
) > 0) {
665 arc_flags_t flags
= ARC_FLAG_WAIT
;
668 int epb
= BP_GET_LSIZE(bp
) >> SPA_BLKPTRSHIFT
;
671 err
= arc_read(NULL
, dp
->dp_spa
, bp
, arc_getbuf_func
, &buf
,
672 ZIO_PRIORITY_ASYNC_READ
, zio_flags
, &flags
, zb
);
674 scn
->scn_phys
.scn_errors
++;
677 for (i
= 0, cbp
= buf
->b_data
; i
< epb
; i
++, cbp
++) {
678 dsl_scan_prefetch(scn
, buf
, cbp
, zb
->zb_objset
,
679 zb
->zb_object
, zb
->zb_blkid
* epb
+ i
);
681 for (i
= 0, cbp
= buf
->b_data
; i
< epb
; i
++, cbp
++) {
682 zbookmark_phys_t czb
;
684 SET_BOOKMARK(&czb
, zb
->zb_objset
, zb
->zb_object
,
686 zb
->zb_blkid
* epb
+ i
);
687 dsl_scan_visitbp(cbp
, &czb
, dnp
,
688 ds
, scn
, ostype
, tx
);
690 (void) arc_buf_remove_ref(buf
, &buf
);
691 } else if (BP_GET_TYPE(bp
) == DMU_OT_DNODE
) {
692 arc_flags_t flags
= ARC_FLAG_WAIT
;
695 int epb
= BP_GET_LSIZE(bp
) >> DNODE_SHIFT
;
698 err
= arc_read(NULL
, dp
->dp_spa
, bp
, arc_getbuf_func
, &buf
,
699 ZIO_PRIORITY_ASYNC_READ
, zio_flags
, &flags
, zb
);
701 scn
->scn_phys
.scn_errors
++;
704 for (i
= 0, cdnp
= buf
->b_data
; i
< epb
; i
++, cdnp
++) {
705 for (j
= 0; j
< cdnp
->dn_nblkptr
; j
++) {
706 blkptr_t
*cbp
= &cdnp
->dn_blkptr
[j
];
707 dsl_scan_prefetch(scn
, buf
, cbp
,
708 zb
->zb_objset
, zb
->zb_blkid
* epb
+ i
, j
);
711 for (i
= 0, cdnp
= buf
->b_data
; i
< epb
; i
++, cdnp
++) {
712 dsl_scan_visitdnode(scn
, ds
, ostype
,
713 cdnp
, zb
->zb_blkid
* epb
+ i
, tx
);
716 (void) arc_buf_remove_ref(buf
, &buf
);
717 } else if (BP_GET_TYPE(bp
) == DMU_OT_OBJSET
) {
718 arc_flags_t flags
= ARC_FLAG_WAIT
;
722 err
= arc_read(NULL
, dp
->dp_spa
, bp
, arc_getbuf_func
, &buf
,
723 ZIO_PRIORITY_ASYNC_READ
, zio_flags
, &flags
, zb
);
725 scn
->scn_phys
.scn_errors
++;
731 dsl_scan_visitdnode(scn
, ds
, osp
->os_type
,
732 &osp
->os_meta_dnode
, DMU_META_DNODE_OBJECT
, tx
);
734 if (OBJSET_BUF_HAS_USERUSED(buf
)) {
736 * We also always visit user/group accounting
737 * objects, and never skip them, even if we are
738 * pausing. This is necessary so that the space
739 * deltas from this txg get integrated.
741 dsl_scan_visitdnode(scn
, ds
, osp
->os_type
,
742 &osp
->os_groupused_dnode
,
743 DMU_GROUPUSED_OBJECT
, tx
);
744 dsl_scan_visitdnode(scn
, ds
, osp
->os_type
,
745 &osp
->os_userused_dnode
,
746 DMU_USERUSED_OBJECT
, tx
);
748 (void) arc_buf_remove_ref(buf
, &buf
);
754 inline __attribute__((always_inline
)) static void
755 dsl_scan_visitdnode(dsl_scan_t
*scn
, dsl_dataset_t
*ds
,
756 dmu_objset_type_t ostype
, dnode_phys_t
*dnp
,
757 uint64_t object
, dmu_tx_t
*tx
)
761 for (j
= 0; j
< dnp
->dn_nblkptr
; j
++) {
762 zbookmark_phys_t czb
;
764 SET_BOOKMARK(&czb
, ds
? ds
->ds_object
: 0, object
,
765 dnp
->dn_nlevels
- 1, j
);
766 dsl_scan_visitbp(&dnp
->dn_blkptr
[j
],
767 &czb
, dnp
, ds
, scn
, ostype
, tx
);
770 if (dnp
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
) {
771 zbookmark_phys_t czb
;
772 SET_BOOKMARK(&czb
, ds
? ds
->ds_object
: 0, object
,
774 dsl_scan_visitbp(&dnp
->dn_spill
,
775 &czb
, dnp
, ds
, scn
, ostype
, tx
);
780 * The arguments are in this order because mdb can only print the
781 * first 5; we want them to be useful.
784 dsl_scan_visitbp(blkptr_t
*bp
, const zbookmark_phys_t
*zb
,
785 dnode_phys_t
*dnp
, dsl_dataset_t
*ds
, dsl_scan_t
*scn
,
786 dmu_objset_type_t ostype
, dmu_tx_t
*tx
)
788 dsl_pool_t
*dp
= scn
->scn_dp
;
791 bp_toread
= kmem_alloc(sizeof (blkptr_t
), KM_SLEEP
);
794 /* ASSERT(pbuf == NULL || arc_released(pbuf)); */
796 if (dsl_scan_check_pause(scn
, zb
))
799 if (dsl_scan_check_resume(scn
, dnp
, zb
))
805 scn
->scn_visited_this_txg
++;
808 * This debugging is commented out to conserve stack space. This
809 * function is called recursively and the debugging addes several
810 * bytes to the stack for each call. It can be commented back in
811 * if required to debug an issue in dsl_scan_visitbp().
814 * "visiting ds=%p/%llu zb=%llx/%llx/%llx/%llx bp=%p",
815 * ds, ds ? ds->ds_object : 0,
816 * zb->zb_objset, zb->zb_object, zb->zb_level, zb->zb_blkid,
820 if (bp
->blk_birth
<= scn
->scn_phys
.scn_cur_min_txg
)
823 if (dsl_scan_recurse(scn
, ds
, ostype
, dnp
, bp_toread
, zb
, tx
) != 0)
827 * If dsl_scan_ddt() has aready visited this block, it will have
828 * already done any translations or scrubbing, so don't call the
831 if (ddt_class_contains(dp
->dp_spa
,
832 scn
->scn_phys
.scn_ddt_class_max
, bp
)) {
837 * If this block is from the future (after cur_max_txg), then we
838 * are doing this on behalf of a deleted snapshot, and we will
839 * revisit the future block on the next pass of this dataset.
840 * Don't scan it now unless we need to because something
841 * under it was modified.
843 if (BP_PHYSICAL_BIRTH(bp
) <= scn
->scn_phys
.scn_cur_max_txg
) {
844 scan_funcs
[scn
->scn_phys
.scn_func
](dp
, bp
, zb
);
847 kmem_free(bp_toread
, sizeof (blkptr_t
));
851 dsl_scan_visit_rootbp(dsl_scan_t
*scn
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
856 SET_BOOKMARK(&zb
, ds
? ds
->ds_object
: DMU_META_OBJSET
,
857 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
858 dsl_scan_visitbp(bp
, &zb
, NULL
,
859 ds
, scn
, DMU_OST_NONE
, tx
);
861 dprintf_ds(ds
, "finished scan%s", "");
865 dsl_scan_ds_destroyed(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
867 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
868 dsl_scan_t
*scn
= dp
->dp_scan
;
871 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
874 if (scn
->scn_phys
.scn_bookmark
.zb_objset
== ds
->ds_object
) {
875 if (ds
->ds_is_snapshot
) {
878 * - scn_cur_{min,max}_txg stays the same.
879 * - Setting the flag is not really necessary if
880 * scn_cur_max_txg == scn_max_txg, because there
881 * is nothing after this snapshot that we care
882 * about. However, we set it anyway and then
883 * ignore it when we retraverse it in
884 * dsl_scan_visitds().
886 scn
->scn_phys
.scn_bookmark
.zb_objset
=
887 dsl_dataset_phys(ds
)->ds_next_snap_obj
;
888 zfs_dbgmsg("destroying ds %llu; currently traversing; "
889 "reset zb_objset to %llu",
890 (u_longlong_t
)ds
->ds_object
,
891 (u_longlong_t
)dsl_dataset_phys(ds
)->
893 scn
->scn_phys
.scn_flags
|= DSF_VISIT_DS_AGAIN
;
895 SET_BOOKMARK(&scn
->scn_phys
.scn_bookmark
,
896 ZB_DESTROYED_OBJSET
, 0, 0, 0);
897 zfs_dbgmsg("destroying ds %llu; currently traversing; "
898 "reset bookmark to -1,0,0,0",
899 (u_longlong_t
)ds
->ds_object
);
901 } else if (zap_lookup_int_key(dp
->dp_meta_objset
,
902 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
, &mintxg
) == 0) {
903 ASSERT3U(dsl_dataset_phys(ds
)->ds_num_children
, <=, 1);
904 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
905 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
, tx
));
906 if (ds
->ds_is_snapshot
) {
908 * We keep the same mintxg; it could be >
909 * ds_creation_txg if the previous snapshot was
912 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
913 scn
->scn_phys
.scn_queue_obj
,
914 dsl_dataset_phys(ds
)->ds_next_snap_obj
,
916 zfs_dbgmsg("destroying ds %llu; in queue; "
917 "replacing with %llu",
918 (u_longlong_t
)ds
->ds_object
,
919 (u_longlong_t
)dsl_dataset_phys(ds
)->
922 zfs_dbgmsg("destroying ds %llu; in queue; removing",
923 (u_longlong_t
)ds
->ds_object
);
928 * dsl_scan_sync() should be called after this, and should sync
929 * out our changed state, but just to be safe, do it here.
931 dsl_scan_sync_state(scn
, tx
);
935 dsl_scan_ds_snapshotted(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
937 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
938 dsl_scan_t
*scn
= dp
->dp_scan
;
941 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
944 ASSERT(dsl_dataset_phys(ds
)->ds_prev_snap_obj
!= 0);
946 if (scn
->scn_phys
.scn_bookmark
.zb_objset
== ds
->ds_object
) {
947 scn
->scn_phys
.scn_bookmark
.zb_objset
=
948 dsl_dataset_phys(ds
)->ds_prev_snap_obj
;
949 zfs_dbgmsg("snapshotting ds %llu; currently traversing; "
950 "reset zb_objset to %llu",
951 (u_longlong_t
)ds
->ds_object
,
952 (u_longlong_t
)dsl_dataset_phys(ds
)->ds_prev_snap_obj
);
953 } else if (zap_lookup_int_key(dp
->dp_meta_objset
,
954 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
, &mintxg
) == 0) {
955 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
956 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
, tx
));
957 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
958 scn
->scn_phys
.scn_queue_obj
,
959 dsl_dataset_phys(ds
)->ds_prev_snap_obj
, mintxg
, tx
) == 0);
960 zfs_dbgmsg("snapshotting ds %llu; in queue; "
961 "replacing with %llu",
962 (u_longlong_t
)ds
->ds_object
,
963 (u_longlong_t
)dsl_dataset_phys(ds
)->ds_prev_snap_obj
);
965 dsl_scan_sync_state(scn
, tx
);
969 dsl_scan_ds_clone_swapped(dsl_dataset_t
*ds1
, dsl_dataset_t
*ds2
, dmu_tx_t
*tx
)
971 dsl_pool_t
*dp
= ds1
->ds_dir
->dd_pool
;
972 dsl_scan_t
*scn
= dp
->dp_scan
;
975 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
978 if (scn
->scn_phys
.scn_bookmark
.zb_objset
== ds1
->ds_object
) {
979 scn
->scn_phys
.scn_bookmark
.zb_objset
= ds2
->ds_object
;
980 zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
981 "reset zb_objset to %llu",
982 (u_longlong_t
)ds1
->ds_object
,
983 (u_longlong_t
)ds2
->ds_object
);
984 } else if (scn
->scn_phys
.scn_bookmark
.zb_objset
== ds2
->ds_object
) {
985 scn
->scn_phys
.scn_bookmark
.zb_objset
= ds1
->ds_object
;
986 zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
987 "reset zb_objset to %llu",
988 (u_longlong_t
)ds2
->ds_object
,
989 (u_longlong_t
)ds1
->ds_object
);
992 if (zap_lookup_int_key(dp
->dp_meta_objset
, scn
->scn_phys
.scn_queue_obj
,
993 ds1
->ds_object
, &mintxg
) == 0) {
996 ASSERT3U(mintxg
, ==, dsl_dataset_phys(ds1
)->ds_prev_snap_txg
);
997 ASSERT3U(mintxg
, ==, dsl_dataset_phys(ds2
)->ds_prev_snap_txg
);
998 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
999 scn
->scn_phys
.scn_queue_obj
, ds1
->ds_object
, tx
));
1000 err
= zap_add_int_key(dp
->dp_meta_objset
,
1001 scn
->scn_phys
.scn_queue_obj
, ds2
->ds_object
, mintxg
, tx
);
1002 VERIFY(err
== 0 || err
== EEXIST
);
1003 if (err
== EEXIST
) {
1004 /* Both were there to begin with */
1005 VERIFY(0 == zap_add_int_key(dp
->dp_meta_objset
,
1006 scn
->scn_phys
.scn_queue_obj
,
1007 ds1
->ds_object
, mintxg
, tx
));
1009 zfs_dbgmsg("clone_swap ds %llu; in queue; "
1010 "replacing with %llu",
1011 (u_longlong_t
)ds1
->ds_object
,
1012 (u_longlong_t
)ds2
->ds_object
);
1013 } else if (zap_lookup_int_key(dp
->dp_meta_objset
,
1014 scn
->scn_phys
.scn_queue_obj
, ds2
->ds_object
, &mintxg
) == 0) {
1015 ASSERT3U(mintxg
, ==, dsl_dataset_phys(ds1
)->ds_prev_snap_txg
);
1016 ASSERT3U(mintxg
, ==, dsl_dataset_phys(ds2
)->ds_prev_snap_txg
);
1017 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
1018 scn
->scn_phys
.scn_queue_obj
, ds2
->ds_object
, tx
));
1019 VERIFY(0 == zap_add_int_key(dp
->dp_meta_objset
,
1020 scn
->scn_phys
.scn_queue_obj
, ds1
->ds_object
, mintxg
, tx
));
1021 zfs_dbgmsg("clone_swap ds %llu; in queue; "
1022 "replacing with %llu",
1023 (u_longlong_t
)ds2
->ds_object
,
1024 (u_longlong_t
)ds1
->ds_object
);
1027 dsl_scan_sync_state(scn
, tx
);
1030 struct enqueue_clones_arg
{
1037 enqueue_clones_cb(dsl_pool_t
*dp
, dsl_dataset_t
*hds
, void *arg
)
1039 struct enqueue_clones_arg
*eca
= arg
;
1042 dsl_scan_t
*scn
= dp
->dp_scan
;
1044 if (dsl_dir_phys(hds
->ds_dir
)->dd_origin_obj
!= eca
->originobj
)
1047 err
= dsl_dataset_hold_obj(dp
, hds
->ds_object
, FTAG
, &ds
);
1051 while (dsl_dataset_phys(ds
)->ds_prev_snap_obj
!= eca
->originobj
) {
1052 dsl_dataset_t
*prev
;
1053 err
= dsl_dataset_hold_obj(dp
,
1054 dsl_dataset_phys(ds
)->ds_prev_snap_obj
, FTAG
, &prev
);
1056 dsl_dataset_rele(ds
, FTAG
);
1061 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
1062 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
,
1063 dsl_dataset_phys(ds
)->ds_prev_snap_txg
, eca
->tx
) == 0);
1064 dsl_dataset_rele(ds
, FTAG
);
1069 dsl_scan_visitds(dsl_scan_t
*scn
, uint64_t dsobj
, dmu_tx_t
*tx
)
1071 dsl_pool_t
*dp
= scn
->scn_dp
;
1076 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp
, dsobj
, FTAG
, &ds
));
1078 if (scn
->scn_phys
.scn_cur_min_txg
>=
1079 scn
->scn_phys
.scn_max_txg
) {
1081 * This can happen if this snapshot was created after the
1082 * scan started, and we already completed a previous snapshot
1083 * that was created after the scan started. This snapshot
1084 * only references blocks with:
1086 * birth < our ds_creation_txg
1087 * cur_min_txg is no less than ds_creation_txg.
1088 * We have already visited these blocks.
1090 * birth > scn_max_txg
1091 * The scan requested not to visit these blocks.
1093 * Subsequent snapshots (and clones) can reference our
1094 * blocks, or blocks with even higher birth times.
1095 * Therefore we do not need to visit them either,
1096 * so we do not add them to the work queue.
1098 * Note that checking for cur_min_txg >= cur_max_txg
1099 * is not sufficient, because in that case we may need to
1100 * visit subsequent snapshots. This happens when min_txg > 0,
1101 * which raises cur_min_txg. In this case we will visit
1102 * this dataset but skip all of its blocks, because the
1103 * rootbp's birth time is < cur_min_txg. Then we will
1104 * add the next snapshots/clones to the work queue.
1106 char *dsname
= kmem_alloc(MAXNAMELEN
, KM_SLEEP
);
1107 dsl_dataset_name(ds
, dsname
);
1108 zfs_dbgmsg("scanning dataset %llu (%s) is unnecessary because "
1109 "cur_min_txg (%llu) >= max_txg (%llu)",
1111 scn
->scn_phys
.scn_cur_min_txg
,
1112 scn
->scn_phys
.scn_max_txg
);
1113 kmem_free(dsname
, MAXNAMELEN
);
1118 if (dmu_objset_from_ds(ds
, &os
))
1122 * Only the ZIL in the head (non-snapshot) is valid. Even though
1123 * snapshots can have ZIL block pointers (which may be the same
1124 * BP as in the head), they must be ignored. So we traverse the
1125 * ZIL here, rather than in scan_recurse(), because the regular
1126 * snapshot block-sharing rules don't apply to it.
1128 if (DSL_SCAN_IS_SCRUB_RESILVER(scn
) && !ds
->ds_is_snapshot
)
1129 dsl_scan_zil(dp
, &os
->os_zil_header
);
1132 * Iterate over the bps in this ds.
1134 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
1135 dsl_scan_visit_rootbp(scn
, ds
, &dsl_dataset_phys(ds
)->ds_bp
, tx
);
1137 dsname
= kmem_alloc(ZFS_MAXNAMELEN
, KM_SLEEP
);
1138 dsl_dataset_name(ds
, dsname
);
1139 zfs_dbgmsg("scanned dataset %llu (%s) with min=%llu max=%llu; "
1141 (longlong_t
)dsobj
, dsname
,
1142 (longlong_t
)scn
->scn_phys
.scn_cur_min_txg
,
1143 (longlong_t
)scn
->scn_phys
.scn_cur_max_txg
,
1144 (int)scn
->scn_pausing
);
1145 kmem_free(dsname
, ZFS_MAXNAMELEN
);
1147 if (scn
->scn_pausing
)
1151 * We've finished this pass over this dataset.
1155 * If we did not completely visit this dataset, do another pass.
1157 if (scn
->scn_phys
.scn_flags
& DSF_VISIT_DS_AGAIN
) {
1158 zfs_dbgmsg("incomplete pass; visiting again");
1159 scn
->scn_phys
.scn_flags
&= ~DSF_VISIT_DS_AGAIN
;
1160 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
1161 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
,
1162 scn
->scn_phys
.scn_cur_max_txg
, tx
) == 0);
1167 * Add descendent datasets to work queue.
1169 if (dsl_dataset_phys(ds
)->ds_next_snap_obj
!= 0) {
1170 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
1171 scn
->scn_phys
.scn_queue_obj
,
1172 dsl_dataset_phys(ds
)->ds_next_snap_obj
,
1173 dsl_dataset_phys(ds
)->ds_creation_txg
, tx
) == 0);
1175 if (dsl_dataset_phys(ds
)->ds_num_children
> 1) {
1176 boolean_t usenext
= B_FALSE
;
1177 if (dsl_dataset_phys(ds
)->ds_next_clones_obj
!= 0) {
1180 * A bug in a previous version of the code could
1181 * cause upgrade_clones_cb() to not set
1182 * ds_next_snap_obj when it should, leading to a
1183 * missing entry. Therefore we can only use the
1184 * next_clones_obj when its count is correct.
1186 int err
= zap_count(dp
->dp_meta_objset
,
1187 dsl_dataset_phys(ds
)->ds_next_clones_obj
, &count
);
1189 count
== dsl_dataset_phys(ds
)->ds_num_children
- 1)
1194 VERIFY0(zap_join_key(dp
->dp_meta_objset
,
1195 dsl_dataset_phys(ds
)->ds_next_clones_obj
,
1196 scn
->scn_phys
.scn_queue_obj
,
1197 dsl_dataset_phys(ds
)->ds_creation_txg
, tx
));
1199 struct enqueue_clones_arg eca
;
1201 eca
.originobj
= ds
->ds_object
;
1203 VERIFY0(dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
1204 enqueue_clones_cb
, &eca
, DS_FIND_CHILDREN
));
1209 dsl_dataset_rele(ds
, FTAG
);
1214 enqueue_cb(dsl_pool_t
*dp
, dsl_dataset_t
*hds
, void *arg
)
1219 dsl_scan_t
*scn
= dp
->dp_scan
;
1221 err
= dsl_dataset_hold_obj(dp
, hds
->ds_object
, FTAG
, &ds
);
1225 while (dsl_dataset_phys(ds
)->ds_prev_snap_obj
!= 0) {
1226 dsl_dataset_t
*prev
;
1227 err
= dsl_dataset_hold_obj(dp
,
1228 dsl_dataset_phys(ds
)->ds_prev_snap_obj
, FTAG
, &prev
);
1230 dsl_dataset_rele(ds
, FTAG
);
1235 * If this is a clone, we don't need to worry about it for now.
1237 if (dsl_dataset_phys(prev
)->ds_next_snap_obj
!= ds
->ds_object
) {
1238 dsl_dataset_rele(ds
, FTAG
);
1239 dsl_dataset_rele(prev
, FTAG
);
1242 dsl_dataset_rele(ds
, FTAG
);
1246 VERIFY(zap_add_int_key(dp
->dp_meta_objset
, scn
->scn_phys
.scn_queue_obj
,
1247 ds
->ds_object
, dsl_dataset_phys(ds
)->ds_prev_snap_txg
, tx
) == 0);
1248 dsl_dataset_rele(ds
, FTAG
);
1253 * Scrub/dedup interaction.
1255 * If there are N references to a deduped block, we don't want to scrub it
1256 * N times -- ideally, we should scrub it exactly once.
1258 * We leverage the fact that the dde's replication class (enum ddt_class)
1259 * is ordered from highest replication class (DDT_CLASS_DITTO) to lowest
1260 * (DDT_CLASS_UNIQUE) so that we may walk the DDT in that order.
1262 * To prevent excess scrubbing, the scrub begins by walking the DDT
1263 * to find all blocks with refcnt > 1, and scrubs each of these once.
1264 * Since there are two replication classes which contain blocks with
1265 * refcnt > 1, we scrub the highest replication class (DDT_CLASS_DITTO) first.
1266 * Finally the top-down scrub begins, only visiting blocks with refcnt == 1.
1268 * There would be nothing more to say if a block's refcnt couldn't change
1269 * during a scrub, but of course it can so we must account for changes
1270 * in a block's replication class.
1272 * Here's an example of what can occur:
1274 * If a block has refcnt > 1 during the DDT scrub phase, but has refcnt == 1
1275 * when visited during the top-down scrub phase, it will be scrubbed twice.
1276 * This negates our scrub optimization, but is otherwise harmless.
1278 * If a block has refcnt == 1 during the DDT scrub phase, but has refcnt > 1
1279 * on each visit during the top-down scrub phase, it will never be scrubbed.
1280 * To catch this, ddt_sync_entry() notifies the scrub code whenever a block's
1281 * reference class transitions to a higher level (i.e DDT_CLASS_UNIQUE to
1282 * DDT_CLASS_DUPLICATE); if it transitions from refcnt == 1 to refcnt > 1
1283 * while a scrub is in progress, it scrubs the block right then.
1286 dsl_scan_ddt(dsl_scan_t
*scn
, dmu_tx_t
*tx
)
1288 ddt_bookmark_t
*ddb
= &scn
->scn_phys
.scn_ddt_bookmark
;
1293 bzero(&dde
, sizeof (ddt_entry_t
));
1295 while ((error
= ddt_walk(scn
->scn_dp
->dp_spa
, ddb
, &dde
)) == 0) {
1298 if (ddb
->ddb_class
> scn
->scn_phys
.scn_ddt_class_max
)
1300 dprintf("visiting ddb=%llu/%llu/%llu/%llx\n",
1301 (longlong_t
)ddb
->ddb_class
,
1302 (longlong_t
)ddb
->ddb_type
,
1303 (longlong_t
)ddb
->ddb_checksum
,
1304 (longlong_t
)ddb
->ddb_cursor
);
1306 /* There should be no pending changes to the dedup table */
1307 ddt
= scn
->scn_dp
->dp_spa
->spa_ddt
[ddb
->ddb_checksum
];
1308 ASSERT(avl_first(&ddt
->ddt_tree
) == NULL
);
1310 dsl_scan_ddt_entry(scn
, ddb
->ddb_checksum
, &dde
, tx
);
1313 if (dsl_scan_check_pause(scn
, NULL
))
1317 zfs_dbgmsg("scanned %llu ddt entries with class_max = %u; pausing=%u",
1318 (longlong_t
)n
, (int)scn
->scn_phys
.scn_ddt_class_max
,
1319 (int)scn
->scn_pausing
);
1321 ASSERT(error
== 0 || error
== ENOENT
);
1322 ASSERT(error
!= ENOENT
||
1323 ddb
->ddb_class
> scn
->scn_phys
.scn_ddt_class_max
);
1328 dsl_scan_ddt_entry(dsl_scan_t
*scn
, enum zio_checksum checksum
,
1329 ddt_entry_t
*dde
, dmu_tx_t
*tx
)
1331 const ddt_key_t
*ddk
= &dde
->dde_key
;
1332 ddt_phys_t
*ddp
= dde
->dde_phys
;
1334 zbookmark_phys_t zb
= { 0 };
1337 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
1340 for (p
= 0; p
< DDT_PHYS_TYPES
; p
++, ddp
++) {
1341 if (ddp
->ddp_phys_birth
== 0 ||
1342 ddp
->ddp_phys_birth
> scn
->scn_phys
.scn_max_txg
)
1344 ddt_bp_create(checksum
, ddk
, ddp
, &bp
);
1346 scn
->scn_visited_this_txg
++;
1347 scan_funcs
[scn
->scn_phys
.scn_func
](scn
->scn_dp
, &bp
, &zb
);
1352 dsl_scan_visit(dsl_scan_t
*scn
, dmu_tx_t
*tx
)
1354 dsl_pool_t
*dp
= scn
->scn_dp
;
1356 zap_attribute_t
*za
;
1358 if (scn
->scn_phys
.scn_ddt_bookmark
.ddb_class
<=
1359 scn
->scn_phys
.scn_ddt_class_max
) {
1360 scn
->scn_phys
.scn_cur_min_txg
= scn
->scn_phys
.scn_min_txg
;
1361 scn
->scn_phys
.scn_cur_max_txg
= scn
->scn_phys
.scn_max_txg
;
1362 dsl_scan_ddt(scn
, tx
);
1363 if (scn
->scn_pausing
)
1367 if (scn
->scn_phys
.scn_bookmark
.zb_objset
== DMU_META_OBJSET
) {
1368 /* First do the MOS & ORIGIN */
1370 scn
->scn_phys
.scn_cur_min_txg
= scn
->scn_phys
.scn_min_txg
;
1371 scn
->scn_phys
.scn_cur_max_txg
= scn
->scn_phys
.scn_max_txg
;
1372 dsl_scan_visit_rootbp(scn
, NULL
,
1373 &dp
->dp_meta_rootbp
, tx
);
1374 spa_set_rootblkptr(dp
->dp_spa
, &dp
->dp_meta_rootbp
);
1375 if (scn
->scn_pausing
)
1378 if (spa_version(dp
->dp_spa
) < SPA_VERSION_DSL_SCRUB
) {
1379 VERIFY0(dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
1380 enqueue_cb
, tx
, DS_FIND_CHILDREN
));
1382 dsl_scan_visitds(scn
,
1383 dp
->dp_origin_snap
->ds_object
, tx
);
1385 ASSERT(!scn
->scn_pausing
);
1386 } else if (scn
->scn_phys
.scn_bookmark
.zb_objset
!=
1387 ZB_DESTROYED_OBJSET
) {
1389 * If we were paused, continue from here. Note if the
1390 * ds we were paused on was deleted, the zb_objset may
1391 * be -1, so we will skip this and find a new objset
1394 dsl_scan_visitds(scn
, scn
->scn_phys
.scn_bookmark
.zb_objset
, tx
);
1395 if (scn
->scn_pausing
)
1400 * In case we were paused right at the end of the ds, zero the
1401 * bookmark so we don't think that we're still trying to resume.
1403 bzero(&scn
->scn_phys
.scn_bookmark
, sizeof (zbookmark_phys_t
));
1404 zc
= kmem_alloc(sizeof (zap_cursor_t
), KM_SLEEP
);
1405 za
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
1407 /* keep pulling things out of the zap-object-as-queue */
1408 while (zap_cursor_init(zc
, dp
->dp_meta_objset
,
1409 scn
->scn_phys
.scn_queue_obj
),
1410 zap_cursor_retrieve(zc
, za
) == 0) {
1414 dsobj
= strtonum(za
->za_name
, NULL
);
1415 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
1416 scn
->scn_phys
.scn_queue_obj
, dsobj
, tx
));
1418 /* Set up min/max txg */
1419 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp
, dsobj
, FTAG
, &ds
));
1420 if (za
->za_first_integer
!= 0) {
1421 scn
->scn_phys
.scn_cur_min_txg
=
1422 MAX(scn
->scn_phys
.scn_min_txg
,
1423 za
->za_first_integer
);
1425 scn
->scn_phys
.scn_cur_min_txg
=
1426 MAX(scn
->scn_phys
.scn_min_txg
,
1427 dsl_dataset_phys(ds
)->ds_prev_snap_txg
);
1429 scn
->scn_phys
.scn_cur_max_txg
= dsl_scan_ds_maxtxg(ds
);
1430 dsl_dataset_rele(ds
, FTAG
);
1432 dsl_scan_visitds(scn
, dsobj
, tx
);
1433 zap_cursor_fini(zc
);
1434 if (scn
->scn_pausing
)
1437 zap_cursor_fini(zc
);
1439 kmem_free(za
, sizeof (zap_attribute_t
));
1440 kmem_free(zc
, sizeof (zap_cursor_t
));
1444 dsl_scan_free_should_pause(dsl_scan_t
*scn
)
1446 uint64_t elapsed_nanosecs
;
1451 if (scn
->scn_visited_this_txg
>= zfs_free_max_blocks
)
1454 elapsed_nanosecs
= gethrtime() - scn
->scn_sync_start_time
;
1455 return (elapsed_nanosecs
/ NANOSEC
> zfs_txg_timeout
||
1456 (NSEC2MSEC(elapsed_nanosecs
) > zfs_free_min_time_ms
&&
1457 txg_sync_waiting(scn
->scn_dp
)) ||
1458 spa_shutting_down(scn
->scn_dp
->dp_spa
));
1462 dsl_scan_free_block_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
1464 dsl_scan_t
*scn
= arg
;
1466 if (!scn
->scn_is_bptree
||
1467 (BP_GET_LEVEL(bp
) == 0 && BP_GET_TYPE(bp
) != DMU_OT_OBJSET
)) {
1468 if (dsl_scan_free_should_pause(scn
))
1469 return (SET_ERROR(ERESTART
));
1472 zio_nowait(zio_free_sync(scn
->scn_zio_root
, scn
->scn_dp
->dp_spa
,
1473 dmu_tx_get_txg(tx
), bp
, 0));
1474 dsl_dir_diduse_space(tx
->tx_pool
->dp_free_dir
, DD_USED_HEAD
,
1475 -bp_get_dsize_sync(scn
->scn_dp
->dp_spa
, bp
),
1476 -BP_GET_PSIZE(bp
), -BP_GET_UCSIZE(bp
), tx
);
1477 scn
->scn_visited_this_txg
++;
1482 dsl_scan_active(dsl_scan_t
*scn
)
1484 spa_t
*spa
= scn
->scn_dp
->dp_spa
;
1485 uint64_t used
= 0, comp
, uncomp
;
1487 if (spa
->spa_load_state
!= SPA_LOAD_NONE
)
1489 if (spa_shutting_down(spa
))
1491 if (scn
->scn_phys
.scn_state
== DSS_SCANNING
||
1492 (scn
->scn_async_destroying
&& !scn
->scn_async_stalled
))
1495 if (spa_version(scn
->scn_dp
->dp_spa
) >= SPA_VERSION_DEADLISTS
) {
1496 (void) bpobj_space(&scn
->scn_dp
->dp_free_bpobj
,
1497 &used
, &comp
, &uncomp
);
1503 dsl_scan_sync(dsl_pool_t
*dp
, dmu_tx_t
*tx
)
1505 dsl_scan_t
*scn
= dp
->dp_scan
;
1506 spa_t
*spa
= dp
->dp_spa
;
1510 * Check for scn_restart_txg before checking spa_load_state, so
1511 * that we can restart an old-style scan while the pool is being
1512 * imported (see dsl_scan_init).
1514 if (scn
->scn_restart_txg
!= 0 &&
1515 scn
->scn_restart_txg
<= tx
->tx_txg
) {
1516 pool_scan_func_t func
= POOL_SCAN_SCRUB
;
1517 dsl_scan_done(scn
, B_FALSE
, tx
);
1518 if (vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
))
1519 func
= POOL_SCAN_RESILVER
;
1520 zfs_dbgmsg("restarting scan func=%u txg=%llu",
1522 dsl_scan_setup_sync(&func
, tx
);
1526 * Only process scans in sync pass 1.
1528 if (spa_sync_pass(dp
->dp_spa
) > 1)
1532 * If the spa is shutting down, then stop scanning. This will
1533 * ensure that the scan does not dirty any new data during the
1536 if (spa_shutting_down(spa
))
1540 * If the scan is inactive due to a stalled async destroy, try again.
1542 if (!scn
->scn_async_stalled
&& !dsl_scan_active(scn
))
1545 scn
->scn_visited_this_txg
= 0;
1546 scn
->scn_pausing
= B_FALSE
;
1547 scn
->scn_sync_start_time
= gethrtime();
1548 spa
->spa_scrub_active
= B_TRUE
;
1551 * First process the async destroys. If we pause, don't do
1552 * any scrubbing or resilvering. This ensures that there are no
1553 * async destroys while we are scanning, so the scan code doesn't
1554 * have to worry about traversing it. It is also faster to free the
1555 * blocks than to scrub them.
1557 if (zfs_free_bpobj_enabled
&&
1558 spa_version(dp
->dp_spa
) >= SPA_VERSION_DEADLISTS
) {
1559 scn
->scn_is_bptree
= B_FALSE
;
1560 scn
->scn_zio_root
= zio_root(dp
->dp_spa
, NULL
,
1561 NULL
, ZIO_FLAG_MUSTSUCCEED
);
1562 err
= bpobj_iterate(&dp
->dp_free_bpobj
,
1563 dsl_scan_free_block_cb
, scn
, tx
);
1564 VERIFY3U(0, ==, zio_wait(scn
->scn_zio_root
));
1566 if (err
!= 0 && err
!= ERESTART
)
1567 zfs_panic_recover("error %u from bpobj_iterate()", err
);
1570 if (err
== 0 && spa_feature_is_active(spa
, SPA_FEATURE_ASYNC_DESTROY
)) {
1571 ASSERT(scn
->scn_async_destroying
);
1572 scn
->scn_is_bptree
= B_TRUE
;
1573 scn
->scn_zio_root
= zio_root(dp
->dp_spa
, NULL
,
1574 NULL
, ZIO_FLAG_MUSTSUCCEED
);
1575 err
= bptree_iterate(dp
->dp_meta_objset
,
1576 dp
->dp_bptree_obj
, B_TRUE
, dsl_scan_free_block_cb
, scn
, tx
);
1577 VERIFY0(zio_wait(scn
->scn_zio_root
));
1579 if (err
== EIO
|| err
== ECKSUM
) {
1581 } else if (err
!= 0 && err
!= ERESTART
) {
1582 zfs_panic_recover("error %u from "
1583 "traverse_dataset_destroyed()", err
);
1586 if (bptree_is_empty(dp
->dp_meta_objset
, dp
->dp_bptree_obj
)) {
1587 /* finished; deactivate async destroy feature */
1588 spa_feature_decr(spa
, SPA_FEATURE_ASYNC_DESTROY
, tx
);
1589 ASSERT(!spa_feature_is_active(spa
,
1590 SPA_FEATURE_ASYNC_DESTROY
));
1591 VERIFY0(zap_remove(dp
->dp_meta_objset
,
1592 DMU_POOL_DIRECTORY_OBJECT
,
1593 DMU_POOL_BPTREE_OBJ
, tx
));
1594 VERIFY0(bptree_free(dp
->dp_meta_objset
,
1595 dp
->dp_bptree_obj
, tx
));
1596 dp
->dp_bptree_obj
= 0;
1597 scn
->scn_async_destroying
= B_FALSE
;
1598 scn
->scn_async_stalled
= B_FALSE
;
1601 * If we didn't make progress, mark the async
1602 * destroy as stalled, so that we will not initiate
1603 * a spa_sync() on its behalf. Note that we only
1604 * check this if we are not finished, because if the
1605 * bptree had no blocks for us to visit, we can
1606 * finish without "making progress".
1608 scn
->scn_async_stalled
=
1609 (scn
->scn_visited_this_txg
== 0);
1612 if (scn
->scn_visited_this_txg
) {
1613 zfs_dbgmsg("freed %llu blocks in %llums from "
1614 "free_bpobj/bptree txg %llu; err=%u",
1615 (longlong_t
)scn
->scn_visited_this_txg
,
1617 NSEC2MSEC(gethrtime() - scn
->scn_sync_start_time
),
1618 (longlong_t
)tx
->tx_txg
, err
);
1619 scn
->scn_visited_this_txg
= 0;
1622 * Write out changes to the DDT that may be required as a
1623 * result of the blocks freed. This ensures that the DDT
1624 * is clean when a scrub/resilver runs.
1626 ddt_sync(spa
, tx
->tx_txg
);
1630 if (dp
->dp_free_dir
!= NULL
&& !scn
->scn_async_destroying
&&
1631 zfs_free_leak_on_eio
&&
1632 (dsl_dir_phys(dp
->dp_free_dir
)->dd_used_bytes
!= 0 ||
1633 dsl_dir_phys(dp
->dp_free_dir
)->dd_compressed_bytes
!= 0 ||
1634 dsl_dir_phys(dp
->dp_free_dir
)->dd_uncompressed_bytes
!= 0)) {
1636 * We have finished background destroying, but there is still
1637 * some space left in the dp_free_dir. Transfer this leaked
1638 * space to the dp_leak_dir.
1640 if (dp
->dp_leak_dir
== NULL
) {
1641 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
1642 (void) dsl_dir_create_sync(dp
, dp
->dp_root_dir
,
1644 VERIFY0(dsl_pool_open_special_dir(dp
,
1645 LEAK_DIR_NAME
, &dp
->dp_leak_dir
));
1646 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
1648 dsl_dir_diduse_space(dp
->dp_leak_dir
, DD_USED_HEAD
,
1649 dsl_dir_phys(dp
->dp_free_dir
)->dd_used_bytes
,
1650 dsl_dir_phys(dp
->dp_free_dir
)->dd_compressed_bytes
,
1651 dsl_dir_phys(dp
->dp_free_dir
)->dd_uncompressed_bytes
, tx
);
1652 dsl_dir_diduse_space(dp
->dp_free_dir
, DD_USED_HEAD
,
1653 -dsl_dir_phys(dp
->dp_free_dir
)->dd_used_bytes
,
1654 -dsl_dir_phys(dp
->dp_free_dir
)->dd_compressed_bytes
,
1655 -dsl_dir_phys(dp
->dp_free_dir
)->dd_uncompressed_bytes
, tx
);
1657 if (dp
->dp_free_dir
!= NULL
&& !scn
->scn_async_destroying
) {
1658 /* finished; verify that space accounting went to zero */
1659 ASSERT0(dsl_dir_phys(dp
->dp_free_dir
)->dd_used_bytes
);
1660 ASSERT0(dsl_dir_phys(dp
->dp_free_dir
)->dd_compressed_bytes
);
1661 ASSERT0(dsl_dir_phys(dp
->dp_free_dir
)->dd_uncompressed_bytes
);
1664 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
1667 if (scn
->scn_done_txg
== tx
->tx_txg
) {
1668 ASSERT(!scn
->scn_pausing
);
1669 /* finished with scan. */
1670 zfs_dbgmsg("txg %llu scan complete", tx
->tx_txg
);
1671 dsl_scan_done(scn
, B_TRUE
, tx
);
1672 ASSERT3U(spa
->spa_scrub_inflight
, ==, 0);
1673 dsl_scan_sync_state(scn
, tx
);
1677 if (scn
->scn_phys
.scn_ddt_bookmark
.ddb_class
<=
1678 scn
->scn_phys
.scn_ddt_class_max
) {
1679 zfs_dbgmsg("doing scan sync txg %llu; "
1680 "ddt bm=%llu/%llu/%llu/%llx",
1681 (longlong_t
)tx
->tx_txg
,
1682 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_class
,
1683 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_type
,
1684 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_checksum
,
1685 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_cursor
);
1686 ASSERT(scn
->scn_phys
.scn_bookmark
.zb_objset
== 0);
1687 ASSERT(scn
->scn_phys
.scn_bookmark
.zb_object
== 0);
1688 ASSERT(scn
->scn_phys
.scn_bookmark
.zb_level
== 0);
1689 ASSERT(scn
->scn_phys
.scn_bookmark
.zb_blkid
== 0);
1691 zfs_dbgmsg("doing scan sync txg %llu; bm=%llu/%llu/%llu/%llu",
1692 (longlong_t
)tx
->tx_txg
,
1693 (longlong_t
)scn
->scn_phys
.scn_bookmark
.zb_objset
,
1694 (longlong_t
)scn
->scn_phys
.scn_bookmark
.zb_object
,
1695 (longlong_t
)scn
->scn_phys
.scn_bookmark
.zb_level
,
1696 (longlong_t
)scn
->scn_phys
.scn_bookmark
.zb_blkid
);
1699 scn
->scn_zio_root
= zio_root(dp
->dp_spa
, NULL
,
1700 NULL
, ZIO_FLAG_CANFAIL
);
1701 dsl_pool_config_enter(dp
, FTAG
);
1702 dsl_scan_visit(scn
, tx
);
1703 dsl_pool_config_exit(dp
, FTAG
);
1704 (void) zio_wait(scn
->scn_zio_root
);
1705 scn
->scn_zio_root
= NULL
;
1707 zfs_dbgmsg("visited %llu blocks in %llums",
1708 (longlong_t
)scn
->scn_visited_this_txg
,
1709 (longlong_t
)NSEC2MSEC(gethrtime() - scn
->scn_sync_start_time
));
1711 if (!scn
->scn_pausing
) {
1712 scn
->scn_done_txg
= tx
->tx_txg
+ 1;
1713 zfs_dbgmsg("txg %llu traversal complete, waiting till txg %llu",
1714 tx
->tx_txg
, scn
->scn_done_txg
);
1717 if (DSL_SCAN_IS_SCRUB_RESILVER(scn
)) {
1718 mutex_enter(&spa
->spa_scrub_lock
);
1719 while (spa
->spa_scrub_inflight
> 0) {
1720 cv_wait(&spa
->spa_scrub_io_cv
,
1721 &spa
->spa_scrub_lock
);
1723 mutex_exit(&spa
->spa_scrub_lock
);
1726 dsl_scan_sync_state(scn
, tx
);
1730 * This will start a new scan, or restart an existing one.
1733 dsl_resilver_restart(dsl_pool_t
*dp
, uint64_t txg
)
1737 tx
= dmu_tx_create_dd(dp
->dp_mos_dir
);
1738 VERIFY(0 == dmu_tx_assign(tx
, TXG_WAIT
));
1740 txg
= dmu_tx_get_txg(tx
);
1741 dp
->dp_scan
->scn_restart_txg
= txg
;
1744 dp
->dp_scan
->scn_restart_txg
= txg
;
1746 zfs_dbgmsg("restarting resilver txg=%llu", txg
);
1750 dsl_scan_resilvering(dsl_pool_t
*dp
)
1752 return (dp
->dp_scan
->scn_phys
.scn_state
== DSS_SCANNING
&&
1753 dp
->dp_scan
->scn_phys
.scn_func
== POOL_SCAN_RESILVER
);
1761 count_block(zfs_all_blkstats_t
*zab
, const blkptr_t
*bp
)
1766 * If we resume after a reboot, zab will be NULL; don't record
1767 * incomplete stats in that case.
1772 for (i
= 0; i
< 4; i
++) {
1773 int l
= (i
< 2) ? BP_GET_LEVEL(bp
) : DN_MAX_LEVELS
;
1774 int t
= (i
& 1) ? BP_GET_TYPE(bp
) : DMU_OT_TOTAL
;
1778 if (t
& DMU_OT_NEWTYPE
)
1781 zb
= &zab
->zab_type
[l
][t
];
1783 zb
->zb_asize
+= BP_GET_ASIZE(bp
);
1784 zb
->zb_lsize
+= BP_GET_LSIZE(bp
);
1785 zb
->zb_psize
+= BP_GET_PSIZE(bp
);
1786 zb
->zb_gangs
+= BP_COUNT_GANG(bp
);
1788 switch (BP_GET_NDVAS(bp
)) {
1790 if (DVA_GET_VDEV(&bp
->blk_dva
[0]) ==
1791 DVA_GET_VDEV(&bp
->blk_dva
[1]))
1792 zb
->zb_ditto_2_of_2_samevdev
++;
1795 equal
= (DVA_GET_VDEV(&bp
->blk_dva
[0]) ==
1796 DVA_GET_VDEV(&bp
->blk_dva
[1])) +
1797 (DVA_GET_VDEV(&bp
->blk_dva
[0]) ==
1798 DVA_GET_VDEV(&bp
->blk_dva
[2])) +
1799 (DVA_GET_VDEV(&bp
->blk_dva
[1]) ==
1800 DVA_GET_VDEV(&bp
->blk_dva
[2]));
1802 zb
->zb_ditto_2_of_3_samevdev
++;
1803 else if (equal
== 3)
1804 zb
->zb_ditto_3_of_3_samevdev
++;
1811 dsl_scan_scrub_done(zio_t
*zio
)
1813 spa_t
*spa
= zio
->io_spa
;
1815 zio_data_buf_free(zio
->io_data
, zio
->io_size
);
1817 mutex_enter(&spa
->spa_scrub_lock
);
1818 spa
->spa_scrub_inflight
--;
1819 cv_broadcast(&spa
->spa_scrub_io_cv
);
1821 if (zio
->io_error
&& (zio
->io_error
!= ECKSUM
||
1822 !(zio
->io_flags
& ZIO_FLAG_SPECULATIVE
))) {
1823 spa
->spa_dsl_pool
->dp_scan
->scn_phys
.scn_errors
++;
1825 mutex_exit(&spa
->spa_scrub_lock
);
1829 dsl_scan_scrub_cb(dsl_pool_t
*dp
,
1830 const blkptr_t
*bp
, const zbookmark_phys_t
*zb
)
1832 dsl_scan_t
*scn
= dp
->dp_scan
;
1833 size_t size
= BP_GET_PSIZE(bp
);
1834 spa_t
*spa
= dp
->dp_spa
;
1835 uint64_t phys_birth
= BP_PHYSICAL_BIRTH(bp
);
1836 boolean_t needs_io
= B_FALSE
;
1837 int zio_flags
= ZIO_FLAG_SCAN_THREAD
| ZIO_FLAG_RAW
| ZIO_FLAG_CANFAIL
;
1841 if (phys_birth
<= scn
->scn_phys
.scn_min_txg
||
1842 phys_birth
>= scn
->scn_phys
.scn_max_txg
)
1845 count_block(dp
->dp_blkstats
, bp
);
1847 if (BP_IS_EMBEDDED(bp
))
1850 ASSERT(DSL_SCAN_IS_SCRUB_RESILVER(scn
));
1851 if (scn
->scn_phys
.scn_func
== POOL_SCAN_SCRUB
) {
1852 zio_flags
|= ZIO_FLAG_SCRUB
;
1854 scan_delay
= zfs_scrub_delay
;
1856 ASSERT3U(scn
->scn_phys
.scn_func
, ==, POOL_SCAN_RESILVER
);
1857 zio_flags
|= ZIO_FLAG_RESILVER
;
1859 scan_delay
= zfs_resilver_delay
;
1862 /* If it's an intent log block, failure is expected. */
1863 if (zb
->zb_level
== ZB_ZIL_LEVEL
)
1864 zio_flags
|= ZIO_FLAG_SPECULATIVE
;
1866 for (d
= 0; d
< BP_GET_NDVAS(bp
); d
++) {
1867 vdev_t
*vd
= vdev_lookup_top(spa
,
1868 DVA_GET_VDEV(&bp
->blk_dva
[d
]));
1871 * Keep track of how much data we've examined so that
1872 * zpool(1M) status can make useful progress reports.
1874 scn
->scn_phys
.scn_examined
+= DVA_GET_ASIZE(&bp
->blk_dva
[d
]);
1875 spa
->spa_scan_pass_exam
+= DVA_GET_ASIZE(&bp
->blk_dva
[d
]);
1877 /* if it's a resilver, this may not be in the target range */
1879 if (DVA_GET_GANG(&bp
->blk_dva
[d
])) {
1881 * Gang members may be spread across multiple
1882 * vdevs, so the best estimate we have is the
1883 * scrub range, which has already been checked.
1884 * XXX -- it would be better to change our
1885 * allocation policy to ensure that all
1886 * gang members reside on the same vdev.
1890 needs_io
= vdev_dtl_contains(vd
, DTL_PARTIAL
,
1896 if (needs_io
&& !zfs_no_scrub_io
) {
1897 vdev_t
*rvd
= spa
->spa_root_vdev
;
1898 uint64_t maxinflight
= rvd
->vdev_children
* zfs_top_maxinflight
;
1899 void *data
= zio_data_buf_alloc(size
);
1901 mutex_enter(&spa
->spa_scrub_lock
);
1902 while (spa
->spa_scrub_inflight
>= maxinflight
)
1903 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
1904 spa
->spa_scrub_inflight
++;
1905 mutex_exit(&spa
->spa_scrub_lock
);
1908 * If we're seeing recent (zfs_scan_idle) "important" I/Os
1909 * then throttle our workload to limit the impact of a scan.
1911 if (ddi_get_lbolt64() - spa
->spa_last_io
<= zfs_scan_idle
)
1914 zio_nowait(zio_read(NULL
, spa
, bp
, data
, size
,
1915 dsl_scan_scrub_done
, NULL
, ZIO_PRIORITY_SCRUB
,
1919 /* do not relocate this block */
1924 dsl_scan(dsl_pool_t
*dp
, pool_scan_func_t func
)
1926 spa_t
*spa
= dp
->dp_spa
;
1929 * Purge all vdev caches and probe all devices. We do this here
1930 * rather than in sync context because this requires a writer lock
1931 * on the spa_config lock, which we can't do from sync context. The
1932 * spa_scrub_reopen flag indicates that vdev_open() should not
1933 * attempt to start another scrub.
1935 spa_vdev_state_enter(spa
, SCL_NONE
);
1936 spa
->spa_scrub_reopen
= B_TRUE
;
1937 vdev_reopen(spa
->spa_root_vdev
);
1938 spa
->spa_scrub_reopen
= B_FALSE
;
1939 (void) spa_vdev_state_exit(spa
, NULL
, 0);
1941 return (dsl_sync_task(spa_name(spa
), dsl_scan_setup_check
,
1942 dsl_scan_setup_sync
, &func
, 0, ZFS_SPACE_CHECK_NONE
));
1945 #if defined(_KERNEL) && defined(HAVE_SPL)
1946 module_param(zfs_top_maxinflight
, int, 0644);
1947 MODULE_PARM_DESC(zfs_top_maxinflight
, "Max I/Os per top-level");
1949 module_param(zfs_resilver_delay
, int, 0644);
1950 MODULE_PARM_DESC(zfs_resilver_delay
, "Number of ticks to delay resilver");
1952 module_param(zfs_scrub_delay
, int, 0644);
1953 MODULE_PARM_DESC(zfs_scrub_delay
, "Number of ticks to delay scrub");
1955 module_param(zfs_scan_idle
, int, 0644);
1956 MODULE_PARM_DESC(zfs_scan_idle
, "Idle window in clock ticks");
1958 module_param(zfs_scan_min_time_ms
, int, 0644);
1959 MODULE_PARM_DESC(zfs_scan_min_time_ms
, "Min millisecs to scrub per txg");
1961 module_param(zfs_free_min_time_ms
, int, 0644);
1962 MODULE_PARM_DESC(zfs_free_min_time_ms
, "Min millisecs to free per txg");
1964 module_param(zfs_resilver_min_time_ms
, int, 0644);
1965 MODULE_PARM_DESC(zfs_resilver_min_time_ms
, "Min millisecs to resilver per txg");
1967 module_param(zfs_no_scrub_io
, int, 0644);
1968 MODULE_PARM_DESC(zfs_no_scrub_io
, "Set to disable scrub I/O");
1970 module_param(zfs_no_scrub_prefetch
, int, 0644);
1971 MODULE_PARM_DESC(zfs_no_scrub_prefetch
, "Set to disable scrub prefetching");
1973 module_param(zfs_free_max_blocks
, ulong
, 0644);
1974 MODULE_PARM_DESC(zfs_free_max_blocks
, "Max number of blocks freed in one txg");
1976 module_param(zfs_free_bpobj_enabled
, int, 0644);
1977 MODULE_PARM_DESC(zfs_free_bpobj_enabled
, "Enable processing of the free_bpobj");