4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
26 #include <sys/dsl_scan.h>
27 #include <sys/dsl_pool.h>
28 #include <sys/dsl_dataset.h>
29 #include <sys/dsl_prop.h>
30 #include <sys/dsl_dir.h>
31 #include <sys/dsl_synctask.h>
32 #include <sys/dnode.h>
33 #include <sys/dmu_tx.h>
34 #include <sys/dmu_objset.h>
38 #include <sys/zfs_context.h>
39 #include <sys/fs/zfs.h>
40 #include <sys/zfs_znode.h>
41 #include <sys/spa_impl.h>
42 #include <sys/vdev_impl.h>
43 #include <sys/zil_impl.h>
44 #include <sys/zio_checksum.h>
47 #include <sys/sa_impl.h>
48 #include <sys/zfeature.h>
50 #include <sys/zfs_vfsops.h>
53 typedef int (scan_cb_t
)(dsl_pool_t
*, const blkptr_t
*,
54 const zbookmark_phys_t
*);
56 static scan_cb_t dsl_scan_scrub_cb
;
57 static void dsl_scan_cancel_sync(void *, dmu_tx_t
*);
58 static void dsl_scan_sync_state(dsl_scan_t
*, dmu_tx_t
*tx
);
60 int zfs_top_maxinflight
= 32; /* maximum I/Os per top-level */
61 int zfs_resilver_delay
= 2; /* number of ticks to delay resilver */
62 int zfs_scrub_delay
= 4; /* number of ticks to delay scrub */
63 int zfs_scan_idle
= 50; /* idle window in clock ticks */
65 int zfs_scan_min_time_ms
= 1000; /* min millisecs to scrub per txg */
66 int zfs_free_min_time_ms
= 1000; /* min millisecs to free per txg */
67 int zfs_resilver_min_time_ms
= 3000; /* min millisecs to resilver per txg */
68 int zfs_no_scrub_io
= B_FALSE
; /* set to disable scrub i/o */
69 int zfs_no_scrub_prefetch
= B_FALSE
; /* set to disable scrub prefetch */
70 enum ddt_class zfs_scrub_ddt_class_max
= DDT_CLASS_DUPLICATE
;
71 int dsl_scan_delay_completion
= B_FALSE
; /* set to delay scan completion */
72 /* max number of blocks to free in a single TXG */
73 ulong zfs_free_max_blocks
= 100000;
75 #define DSL_SCAN_IS_SCRUB_RESILVER(scn) \
76 ((scn)->scn_phys.scn_func == POOL_SCAN_SCRUB || \
77 (scn)->scn_phys.scn_func == POOL_SCAN_RESILVER)
80 * Enable/disable the processing of the free_bpobj object.
82 int zfs_free_bpobj_enabled
= 1;
84 /* the order has to match pool_scan_type */
85 static scan_cb_t
*scan_funcs
[POOL_SCAN_FUNCS
] = {
87 dsl_scan_scrub_cb
, /* POOL_SCAN_SCRUB */
88 dsl_scan_scrub_cb
, /* POOL_SCAN_RESILVER */
92 dsl_scan_init(dsl_pool_t
*dp
, uint64_t txg
)
96 spa_t
*spa
= dp
->dp_spa
;
99 scn
= dp
->dp_scan
= kmem_zalloc(sizeof (dsl_scan_t
), KM_SLEEP
);
103 * It's possible that we're resuming a scan after a reboot so
104 * make sure that the scan_async_destroying flag is initialized
107 ASSERT(!scn
->scn_async_destroying
);
108 scn
->scn_async_destroying
= spa_feature_is_active(dp
->dp_spa
,
109 SPA_FEATURE_ASYNC_DESTROY
);
111 err
= zap_lookup(dp
->dp_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
112 "scrub_func", sizeof (uint64_t), 1, &f
);
115 * There was an old-style scrub in progress. Restart a
116 * new-style scrub from the beginning.
118 scn
->scn_restart_txg
= txg
;
119 zfs_dbgmsg("old-style scrub was in progress; "
120 "restarting new-style scrub in txg %llu",
121 scn
->scn_restart_txg
);
124 * Load the queue obj from the old location so that it
125 * can be freed by dsl_scan_done().
127 (void) zap_lookup(dp
->dp_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
128 "scrub_queue", sizeof (uint64_t), 1,
129 &scn
->scn_phys
.scn_queue_obj
);
131 err
= zap_lookup(dp
->dp_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
132 DMU_POOL_SCAN
, sizeof (uint64_t), SCAN_PHYS_NUMINTS
,
135 * Detect if the pool contains the signature of #2094. If it
136 * does properly update the scn->scn_phys structure and notify
137 * the administrator by setting an errata for the pool.
139 if (err
== EOVERFLOW
) {
140 uint64_t zaptmp
[SCAN_PHYS_NUMINTS
+ 1];
141 VERIFY3S(SCAN_PHYS_NUMINTS
, ==, 24);
142 VERIFY3S(offsetof(dsl_scan_phys_t
, scn_flags
), ==,
143 (23 * sizeof (uint64_t)));
145 err
= zap_lookup(dp
->dp_meta_objset
,
146 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SCAN
,
147 sizeof (uint64_t), SCAN_PHYS_NUMINTS
+ 1, &zaptmp
);
149 uint64_t overflow
= zaptmp
[SCAN_PHYS_NUMINTS
];
151 if (overflow
& ~DSL_SCAN_FLAGS_MASK
||
152 scn
->scn_async_destroying
) {
154 ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY
;
158 bcopy(zaptmp
, &scn
->scn_phys
,
159 SCAN_PHYS_NUMINTS
* sizeof (uint64_t));
160 scn
->scn_phys
.scn_flags
= overflow
;
162 /* Required scrub already in progress. */
163 if (scn
->scn_phys
.scn_state
== DSS_FINISHED
||
164 scn
->scn_phys
.scn_state
== DSS_CANCELED
)
166 ZPOOL_ERRATA_ZOL_2094_SCRUB
;
175 if (scn
->scn_phys
.scn_state
== DSS_SCANNING
&&
176 spa_prev_software_version(dp
->dp_spa
) < SPA_VERSION_SCAN
) {
178 * A new-type scrub was in progress on an old
179 * pool, and the pool was accessed by old
180 * software. Restart from the beginning, since
181 * the old software may have changed the pool in
184 scn
->scn_restart_txg
= txg
;
185 zfs_dbgmsg("new-style scrub was modified "
186 "by old software; restarting in txg %llu",
187 scn
->scn_restart_txg
);
191 spa_scan_stat_init(spa
);
196 dsl_scan_fini(dsl_pool_t
*dp
)
199 kmem_free(dp
->dp_scan
, sizeof (dsl_scan_t
));
206 dsl_scan_setup_check(void *arg
, dmu_tx_t
*tx
)
208 dsl_scan_t
*scn
= dmu_tx_pool(tx
)->dp_scan
;
210 if (scn
->scn_phys
.scn_state
== DSS_SCANNING
)
211 return (SET_ERROR(EBUSY
));
217 dsl_scan_setup_sync(void *arg
, dmu_tx_t
*tx
)
219 dsl_scan_t
*scn
= dmu_tx_pool(tx
)->dp_scan
;
220 pool_scan_func_t
*funcp
= arg
;
221 dmu_object_type_t ot
= 0;
222 dsl_pool_t
*dp
= scn
->scn_dp
;
223 spa_t
*spa
= dp
->dp_spa
;
225 ASSERT(scn
->scn_phys
.scn_state
!= DSS_SCANNING
);
226 ASSERT(*funcp
> POOL_SCAN_NONE
&& *funcp
< POOL_SCAN_FUNCS
);
227 bzero(&scn
->scn_phys
, sizeof (scn
->scn_phys
));
228 scn
->scn_phys
.scn_func
= *funcp
;
229 scn
->scn_phys
.scn_state
= DSS_SCANNING
;
230 scn
->scn_phys
.scn_min_txg
= 0;
231 scn
->scn_phys
.scn_max_txg
= tx
->tx_txg
;
232 scn
->scn_phys
.scn_ddt_class_max
= DDT_CLASSES
- 1; /* the entire DDT */
233 scn
->scn_phys
.scn_start_time
= gethrestime_sec();
234 scn
->scn_phys
.scn_errors
= 0;
235 scn
->scn_phys
.scn_to_examine
= spa
->spa_root_vdev
->vdev_stat
.vs_alloc
;
236 scn
->scn_restart_txg
= 0;
237 scn
->scn_done_txg
= 0;
238 spa_scan_stat_init(spa
);
240 if (DSL_SCAN_IS_SCRUB_RESILVER(scn
)) {
241 scn
->scn_phys
.scn_ddt_class_max
= zfs_scrub_ddt_class_max
;
243 /* rewrite all disk labels */
244 vdev_config_dirty(spa
->spa_root_vdev
);
246 if (vdev_resilver_needed(spa
->spa_root_vdev
,
247 &scn
->scn_phys
.scn_min_txg
, &scn
->scn_phys
.scn_max_txg
)) {
248 spa_event_notify(spa
, NULL
,
249 FM_EREPORT_ZFS_RESILVER_START
);
251 spa_event_notify(spa
, NULL
,
252 FM_EREPORT_ZFS_SCRUB_START
);
255 spa
->spa_scrub_started
= B_TRUE
;
257 * If this is an incremental scrub, limit the DDT scrub phase
258 * to just the auto-ditto class (for correctness); the rest
259 * of the scrub should go faster using top-down pruning.
261 if (scn
->scn_phys
.scn_min_txg
> TXG_INITIAL
)
262 scn
->scn_phys
.scn_ddt_class_max
= DDT_CLASS_DITTO
;
266 /* back to the generic stuff */
268 if (dp
->dp_blkstats
== NULL
) {
270 vmem_alloc(sizeof (zfs_all_blkstats_t
), KM_SLEEP
);
272 bzero(dp
->dp_blkstats
, sizeof (zfs_all_blkstats_t
));
274 if (spa_version(spa
) < SPA_VERSION_DSL_SCRUB
)
275 ot
= DMU_OT_ZAP_OTHER
;
277 scn
->scn_phys
.scn_queue_obj
= zap_create(dp
->dp_meta_objset
,
278 ot
? ot
: DMU_OT_SCAN_QUEUE
, DMU_OT_NONE
, 0, tx
);
280 dsl_scan_sync_state(scn
, tx
);
282 spa_history_log_internal(spa
, "scan setup", tx
,
283 "func=%u mintxg=%llu maxtxg=%llu",
284 *funcp
, scn
->scn_phys
.scn_min_txg
, scn
->scn_phys
.scn_max_txg
);
289 dsl_scan_done(dsl_scan_t
*scn
, boolean_t complete
, dmu_tx_t
*tx
)
291 static const char *old_names
[] = {
293 "scrub_ddt_bookmark",
294 "scrub_ddt_class_max",
303 dsl_pool_t
*dp
= scn
->scn_dp
;
304 spa_t
*spa
= dp
->dp_spa
;
307 /* Remove any remnants of an old-style scrub. */
308 for (i
= 0; old_names
[i
]; i
++) {
309 (void) zap_remove(dp
->dp_meta_objset
,
310 DMU_POOL_DIRECTORY_OBJECT
, old_names
[i
], tx
);
313 if (scn
->scn_phys
.scn_queue_obj
!= 0) {
314 VERIFY(0 == dmu_object_free(dp
->dp_meta_objset
,
315 scn
->scn_phys
.scn_queue_obj
, tx
));
316 scn
->scn_phys
.scn_queue_obj
= 0;
320 * If we were "restarted" from a stopped state, don't bother
321 * with anything else.
323 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
327 scn
->scn_phys
.scn_state
= DSS_FINISHED
;
329 scn
->scn_phys
.scn_state
= DSS_CANCELED
;
331 spa_history_log_internal(spa
, "scan done", tx
,
332 "complete=%u", complete
);
334 if (DSL_SCAN_IS_SCRUB_RESILVER(scn
)) {
335 mutex_enter(&spa
->spa_scrub_lock
);
336 while (spa
->spa_scrub_inflight
> 0) {
337 cv_wait(&spa
->spa_scrub_io_cv
,
338 &spa
->spa_scrub_lock
);
340 mutex_exit(&spa
->spa_scrub_lock
);
341 spa
->spa_scrub_started
= B_FALSE
;
342 spa
->spa_scrub_active
= B_FALSE
;
345 * If the scrub/resilver completed, update all DTLs to
346 * reflect this. Whether it succeeded or not, vacate
347 * all temporary scrub DTLs.
349 vdev_dtl_reassess(spa
->spa_root_vdev
, tx
->tx_txg
,
350 complete
? scn
->scn_phys
.scn_max_txg
: 0, B_TRUE
);
352 spa_event_notify(spa
, NULL
, scn
->scn_phys
.scn_min_txg
?
353 FM_EREPORT_ZFS_RESILVER_FINISH
:
354 FM_EREPORT_ZFS_SCRUB_FINISH
);
356 spa_errlog_rotate(spa
);
359 * We may have finished replacing a device.
360 * Let the async thread assess this and handle the detach.
362 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
365 scn
->scn_phys
.scn_end_time
= gethrestime_sec();
367 if (spa
->spa_errata
== ZPOOL_ERRATA_ZOL_2094_SCRUB
)
373 dsl_scan_cancel_check(void *arg
, dmu_tx_t
*tx
)
375 dsl_scan_t
*scn
= dmu_tx_pool(tx
)->dp_scan
;
377 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
378 return (SET_ERROR(ENOENT
));
384 dsl_scan_cancel_sync(void *arg
, dmu_tx_t
*tx
)
386 dsl_scan_t
*scn
= dmu_tx_pool(tx
)->dp_scan
;
388 dsl_scan_done(scn
, B_FALSE
, tx
);
389 dsl_scan_sync_state(scn
, tx
);
393 dsl_scan_cancel(dsl_pool_t
*dp
)
395 return (dsl_sync_task(spa_name(dp
->dp_spa
), dsl_scan_cancel_check
,
396 dsl_scan_cancel_sync
, NULL
, 3, ZFS_SPACE_CHECK_RESERVED
));
399 static void dsl_scan_visitbp(blkptr_t
*bp
, const zbookmark_phys_t
*zb
,
400 dnode_phys_t
*dnp
, dsl_dataset_t
*ds
, dsl_scan_t
*scn
,
401 dmu_objset_type_t ostype
, dmu_tx_t
*tx
);
402 inline __attribute__((always_inline
)) static void dsl_scan_visitdnode(
403 dsl_scan_t
*, dsl_dataset_t
*ds
, dmu_objset_type_t ostype
,
404 dnode_phys_t
*dnp
, uint64_t object
, dmu_tx_t
*tx
);
407 dsl_free(dsl_pool_t
*dp
, uint64_t txg
, const blkptr_t
*bp
)
409 zio_free(dp
->dp_spa
, txg
, bp
);
413 dsl_free_sync(zio_t
*pio
, dsl_pool_t
*dp
, uint64_t txg
, const blkptr_t
*bpp
)
415 ASSERT(dsl_pool_sync_context(dp
));
416 zio_nowait(zio_free_sync(pio
, dp
->dp_spa
, txg
, bpp
, pio
->io_flags
));
420 dsl_scan_ds_maxtxg(dsl_dataset_t
*ds
)
422 uint64_t smt
= ds
->ds_dir
->dd_pool
->dp_scan
->scn_phys
.scn_max_txg
;
423 if (ds
->ds_is_snapshot
)
424 return (MIN(smt
, dsl_dataset_phys(ds
)->ds_creation_txg
));
429 dsl_scan_sync_state(dsl_scan_t
*scn
, dmu_tx_t
*tx
)
431 VERIFY0(zap_update(scn
->scn_dp
->dp_meta_objset
,
432 DMU_POOL_DIRECTORY_OBJECT
,
433 DMU_POOL_SCAN
, sizeof (uint64_t), SCAN_PHYS_NUMINTS
,
434 &scn
->scn_phys
, tx
));
437 extern int zfs_vdev_async_write_active_min_dirty_percent
;
440 dsl_scan_check_pause(dsl_scan_t
*scn
, const zbookmark_phys_t
*zb
)
442 uint64_t elapsed_nanosecs
;
446 /* we never skip user/group accounting objects */
447 if (zb
&& (int64_t)zb
->zb_object
< 0)
450 if (scn
->scn_pausing
)
451 return (B_TRUE
); /* we're already pausing */
453 if (!ZB_IS_ZERO(&scn
->scn_phys
.scn_bookmark
))
454 return (B_FALSE
); /* we're resuming */
456 /* We only know how to resume from level-0 blocks. */
457 if (zb
&& zb
->zb_level
!= 0)
462 * - we have scanned for the maximum time: an entire txg
463 * timeout (default 5 sec)
465 * - we have scanned for at least the minimum time (default 1 sec
466 * for scrub, 3 sec for resilver), and either we have sufficient
467 * dirty data that we are starting to write more quickly
468 * (default 30%), or someone is explicitly waiting for this txg
471 * - the spa is shutting down because this pool is being exported
472 * or the machine is rebooting.
474 mintime
= (scn
->scn_phys
.scn_func
== POOL_SCAN_RESILVER
) ?
475 zfs_resilver_min_time_ms
: zfs_scan_min_time_ms
;
476 elapsed_nanosecs
= gethrtime() - scn
->scn_sync_start_time
;
477 dirty_pct
= scn
->scn_dp
->dp_dirty_total
* 100 / zfs_dirty_data_max
;
478 if (elapsed_nanosecs
/ NANOSEC
>= zfs_txg_timeout
||
479 (NSEC2MSEC(elapsed_nanosecs
) > mintime
&&
480 (txg_sync_waiting(scn
->scn_dp
) ||
481 dirty_pct
>= zfs_vdev_async_write_active_min_dirty_percent
)) ||
482 spa_shutting_down(scn
->scn_dp
->dp_spa
)) {
484 dprintf("pausing at bookmark %llx/%llx/%llx/%llx\n",
485 (longlong_t
)zb
->zb_objset
,
486 (longlong_t
)zb
->zb_object
,
487 (longlong_t
)zb
->zb_level
,
488 (longlong_t
)zb
->zb_blkid
);
489 scn
->scn_phys
.scn_bookmark
= *zb
;
491 dprintf("pausing at DDT bookmark %llx/%llx/%llx/%llx\n",
492 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_class
,
493 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_type
,
494 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_checksum
,
495 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_cursor
);
496 scn
->scn_pausing
= B_TRUE
;
502 typedef struct zil_scan_arg
{
504 zil_header_t
*zsa_zh
;
509 dsl_scan_zil_block(zilog_t
*zilog
, blkptr_t
*bp
, void *arg
, uint64_t claim_txg
)
511 zil_scan_arg_t
*zsa
= arg
;
512 dsl_pool_t
*dp
= zsa
->zsa_dp
;
513 dsl_scan_t
*scn
= dp
->dp_scan
;
514 zil_header_t
*zh
= zsa
->zsa_zh
;
517 if (BP_IS_HOLE(bp
) || bp
->blk_birth
<= scn
->scn_phys
.scn_cur_min_txg
)
521 * One block ("stubby") can be allocated a long time ago; we
522 * want to visit that one because it has been allocated
523 * (on-disk) even if it hasn't been claimed (even though for
524 * scrub there's nothing to do to it).
526 if (claim_txg
== 0 && bp
->blk_birth
>= spa_first_txg(dp
->dp_spa
))
529 SET_BOOKMARK(&zb
, zh
->zh_log
.blk_cksum
.zc_word
[ZIL_ZC_OBJSET
],
530 ZB_ZIL_OBJECT
, ZB_ZIL_LEVEL
, bp
->blk_cksum
.zc_word
[ZIL_ZC_SEQ
]);
532 VERIFY(0 == scan_funcs
[scn
->scn_phys
.scn_func
](dp
, bp
, &zb
));
538 dsl_scan_zil_record(zilog_t
*zilog
, lr_t
*lrc
, void *arg
, uint64_t claim_txg
)
540 if (lrc
->lrc_txtype
== TX_WRITE
) {
541 zil_scan_arg_t
*zsa
= arg
;
542 dsl_pool_t
*dp
= zsa
->zsa_dp
;
543 dsl_scan_t
*scn
= dp
->dp_scan
;
544 zil_header_t
*zh
= zsa
->zsa_zh
;
545 lr_write_t
*lr
= (lr_write_t
*)lrc
;
546 blkptr_t
*bp
= &lr
->lr_blkptr
;
549 if (BP_IS_HOLE(bp
) ||
550 bp
->blk_birth
<= scn
->scn_phys
.scn_cur_min_txg
)
554 * birth can be < claim_txg if this record's txg is
555 * already txg sync'ed (but this log block contains
556 * other records that are not synced)
558 if (claim_txg
== 0 || bp
->blk_birth
< claim_txg
)
561 SET_BOOKMARK(&zb
, zh
->zh_log
.blk_cksum
.zc_word
[ZIL_ZC_OBJSET
],
562 lr
->lr_foid
, ZB_ZIL_LEVEL
,
563 lr
->lr_offset
/ BP_GET_LSIZE(bp
));
565 VERIFY(0 == scan_funcs
[scn
->scn_phys
.scn_func
](dp
, bp
, &zb
));
571 dsl_scan_zil(dsl_pool_t
*dp
, zil_header_t
*zh
)
573 uint64_t claim_txg
= zh
->zh_claim_txg
;
574 zil_scan_arg_t zsa
= { dp
, zh
};
578 * We only want to visit blocks that have been claimed but not yet
579 * replayed (or, in read-only mode, blocks that *would* be claimed).
581 if (claim_txg
== 0 && spa_writeable(dp
->dp_spa
))
584 zilog
= zil_alloc(dp
->dp_meta_objset
, zh
);
586 (void) zil_parse(zilog
, dsl_scan_zil_block
, dsl_scan_zil_record
, &zsa
,
594 dsl_scan_prefetch(dsl_scan_t
*scn
, arc_buf_t
*buf
, blkptr_t
*bp
,
595 uint64_t objset
, uint64_t object
, uint64_t blkid
)
597 zbookmark_phys_t czb
;
598 arc_flags_t flags
= ARC_FLAG_NOWAIT
| ARC_FLAG_PREFETCH
;
600 if (zfs_no_scrub_prefetch
)
603 if (BP_IS_HOLE(bp
) || bp
->blk_birth
<= scn
->scn_phys
.scn_min_txg
||
604 (BP_GET_LEVEL(bp
) == 0 && BP_GET_TYPE(bp
) != DMU_OT_DNODE
))
607 SET_BOOKMARK(&czb
, objset
, object
, BP_GET_LEVEL(bp
), blkid
);
609 (void) arc_read(scn
->scn_zio_root
, scn
->scn_dp
->dp_spa
, bp
,
610 NULL
, NULL
, ZIO_PRIORITY_ASYNC_READ
,
611 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SCAN_THREAD
, &flags
, &czb
);
615 dsl_scan_check_resume(dsl_scan_t
*scn
, const dnode_phys_t
*dnp
,
616 const zbookmark_phys_t
*zb
)
619 * We never skip over user/group accounting objects (obj<0)
621 if (!ZB_IS_ZERO(&scn
->scn_phys
.scn_bookmark
) &&
622 (int64_t)zb
->zb_object
>= 0) {
624 * If we already visited this bp & everything below (in
625 * a prior txg sync), don't bother doing it again.
627 if (zbookmark_subtree_completed(dnp
, zb
,
628 &scn
->scn_phys
.scn_bookmark
))
632 * If we found the block we're trying to resume from, or
633 * we went past it to a different object, zero it out to
634 * indicate that it's OK to start checking for pausing
637 if (bcmp(zb
, &scn
->scn_phys
.scn_bookmark
, sizeof (*zb
)) == 0 ||
638 zb
->zb_object
> scn
->scn_phys
.scn_bookmark
.zb_object
) {
639 dprintf("resuming at %llx/%llx/%llx/%llx\n",
640 (longlong_t
)zb
->zb_objset
,
641 (longlong_t
)zb
->zb_object
,
642 (longlong_t
)zb
->zb_level
,
643 (longlong_t
)zb
->zb_blkid
);
644 bzero(&scn
->scn_phys
.scn_bookmark
, sizeof (*zb
));
651 * Return nonzero on i/o error.
652 * Return new buf to write out in *bufp.
654 inline __attribute__((always_inline
)) static int
655 dsl_scan_recurse(dsl_scan_t
*scn
, dsl_dataset_t
*ds
, dmu_objset_type_t ostype
,
656 dnode_phys_t
*dnp
, const blkptr_t
*bp
,
657 const zbookmark_phys_t
*zb
, dmu_tx_t
*tx
)
659 dsl_pool_t
*dp
= scn
->scn_dp
;
660 int zio_flags
= ZIO_FLAG_CANFAIL
| ZIO_FLAG_SCAN_THREAD
;
663 if (BP_GET_LEVEL(bp
) > 0) {
664 arc_flags_t flags
= ARC_FLAG_WAIT
;
667 int epb
= BP_GET_LSIZE(bp
) >> SPA_BLKPTRSHIFT
;
670 err
= arc_read(NULL
, dp
->dp_spa
, bp
, arc_getbuf_func
, &buf
,
671 ZIO_PRIORITY_ASYNC_READ
, zio_flags
, &flags
, zb
);
673 scn
->scn_phys
.scn_errors
++;
676 for (i
= 0, cbp
= buf
->b_data
; i
< epb
; i
++, cbp
++) {
677 dsl_scan_prefetch(scn
, buf
, cbp
, zb
->zb_objset
,
678 zb
->zb_object
, zb
->zb_blkid
* epb
+ i
);
680 for (i
= 0, cbp
= buf
->b_data
; i
< epb
; i
++, cbp
++) {
681 zbookmark_phys_t czb
;
683 SET_BOOKMARK(&czb
, zb
->zb_objset
, zb
->zb_object
,
685 zb
->zb_blkid
* epb
+ i
);
686 dsl_scan_visitbp(cbp
, &czb
, dnp
,
687 ds
, scn
, ostype
, tx
);
689 (void) arc_buf_remove_ref(buf
, &buf
);
690 } else if (BP_GET_TYPE(bp
) == DMU_OT_DNODE
) {
691 arc_flags_t flags
= ARC_FLAG_WAIT
;
694 int epb
= BP_GET_LSIZE(bp
) >> DNODE_SHIFT
;
697 err
= arc_read(NULL
, dp
->dp_spa
, bp
, arc_getbuf_func
, &buf
,
698 ZIO_PRIORITY_ASYNC_READ
, zio_flags
, &flags
, zb
);
700 scn
->scn_phys
.scn_errors
++;
703 for (i
= 0, cdnp
= buf
->b_data
; i
< epb
; i
++, cdnp
++) {
704 for (j
= 0; j
< cdnp
->dn_nblkptr
; j
++) {
705 blkptr_t
*cbp
= &cdnp
->dn_blkptr
[j
];
706 dsl_scan_prefetch(scn
, buf
, cbp
,
707 zb
->zb_objset
, zb
->zb_blkid
* epb
+ i
, j
);
710 for (i
= 0, cdnp
= buf
->b_data
; i
< epb
; i
++, cdnp
++) {
711 dsl_scan_visitdnode(scn
, ds
, ostype
,
712 cdnp
, zb
->zb_blkid
* epb
+ i
, tx
);
715 (void) arc_buf_remove_ref(buf
, &buf
);
716 } else if (BP_GET_TYPE(bp
) == DMU_OT_OBJSET
) {
717 arc_flags_t flags
= ARC_FLAG_WAIT
;
721 err
= arc_read(NULL
, dp
->dp_spa
, bp
, arc_getbuf_func
, &buf
,
722 ZIO_PRIORITY_ASYNC_READ
, zio_flags
, &flags
, zb
);
724 scn
->scn_phys
.scn_errors
++;
730 dsl_scan_visitdnode(scn
, ds
, osp
->os_type
,
731 &osp
->os_meta_dnode
, DMU_META_DNODE_OBJECT
, tx
);
733 if (OBJSET_BUF_HAS_USERUSED(buf
)) {
735 * We also always visit user/group accounting
736 * objects, and never skip them, even if we are
737 * pausing. This is necessary so that the space
738 * deltas from this txg get integrated.
740 dsl_scan_visitdnode(scn
, ds
, osp
->os_type
,
741 &osp
->os_groupused_dnode
,
742 DMU_GROUPUSED_OBJECT
, tx
);
743 dsl_scan_visitdnode(scn
, ds
, osp
->os_type
,
744 &osp
->os_userused_dnode
,
745 DMU_USERUSED_OBJECT
, tx
);
747 (void) arc_buf_remove_ref(buf
, &buf
);
753 inline __attribute__((always_inline
)) static void
754 dsl_scan_visitdnode(dsl_scan_t
*scn
, dsl_dataset_t
*ds
,
755 dmu_objset_type_t ostype
, dnode_phys_t
*dnp
,
756 uint64_t object
, dmu_tx_t
*tx
)
760 for (j
= 0; j
< dnp
->dn_nblkptr
; j
++) {
761 zbookmark_phys_t czb
;
763 SET_BOOKMARK(&czb
, ds
? ds
->ds_object
: 0, object
,
764 dnp
->dn_nlevels
- 1, j
);
765 dsl_scan_visitbp(&dnp
->dn_blkptr
[j
],
766 &czb
, dnp
, ds
, scn
, ostype
, tx
);
769 if (dnp
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
) {
770 zbookmark_phys_t czb
;
771 SET_BOOKMARK(&czb
, ds
? ds
->ds_object
: 0, object
,
773 dsl_scan_visitbp(&dnp
->dn_spill
,
774 &czb
, dnp
, ds
, scn
, ostype
, tx
);
779 * The arguments are in this order because mdb can only print the
780 * first 5; we want them to be useful.
783 dsl_scan_visitbp(blkptr_t
*bp
, const zbookmark_phys_t
*zb
,
784 dnode_phys_t
*dnp
, dsl_dataset_t
*ds
, dsl_scan_t
*scn
,
785 dmu_objset_type_t ostype
, dmu_tx_t
*tx
)
787 dsl_pool_t
*dp
= scn
->scn_dp
;
790 bp_toread
= kmem_alloc(sizeof (blkptr_t
), KM_SLEEP
);
793 /* ASSERT(pbuf == NULL || arc_released(pbuf)); */
795 if (dsl_scan_check_pause(scn
, zb
))
798 if (dsl_scan_check_resume(scn
, dnp
, zb
))
804 scn
->scn_visited_this_txg
++;
807 * This debugging is commented out to conserve stack space. This
808 * function is called recursively and the debugging addes several
809 * bytes to the stack for each call. It can be commented back in
810 * if required to debug an issue in dsl_scan_visitbp().
813 * "visiting ds=%p/%llu zb=%llx/%llx/%llx/%llx bp=%p",
814 * ds, ds ? ds->ds_object : 0,
815 * zb->zb_objset, zb->zb_object, zb->zb_level, zb->zb_blkid,
819 if (bp
->blk_birth
<= scn
->scn_phys
.scn_cur_min_txg
)
822 if (dsl_scan_recurse(scn
, ds
, ostype
, dnp
, bp_toread
, zb
, tx
) != 0)
826 * If dsl_scan_ddt() has aready visited this block, it will have
827 * already done any translations or scrubbing, so don't call the
830 if (ddt_class_contains(dp
->dp_spa
,
831 scn
->scn_phys
.scn_ddt_class_max
, bp
)) {
836 * If this block is from the future (after cur_max_txg), then we
837 * are doing this on behalf of a deleted snapshot, and we will
838 * revisit the future block on the next pass of this dataset.
839 * Don't scan it now unless we need to because something
840 * under it was modified.
842 if (BP_PHYSICAL_BIRTH(bp
) <= scn
->scn_phys
.scn_cur_max_txg
) {
843 scan_funcs
[scn
->scn_phys
.scn_func
](dp
, bp
, zb
);
846 kmem_free(bp_toread
, sizeof (blkptr_t
));
850 dsl_scan_visit_rootbp(dsl_scan_t
*scn
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
855 SET_BOOKMARK(&zb
, ds
? ds
->ds_object
: DMU_META_OBJSET
,
856 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
857 dsl_scan_visitbp(bp
, &zb
, NULL
,
858 ds
, scn
, DMU_OST_NONE
, tx
);
860 dprintf_ds(ds
, "finished scan%s", "");
864 dsl_scan_ds_destroyed(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
866 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
867 dsl_scan_t
*scn
= dp
->dp_scan
;
870 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
873 if (scn
->scn_phys
.scn_bookmark
.zb_objset
== ds
->ds_object
) {
874 if (ds
->ds_is_snapshot
) {
877 * - scn_cur_{min,max}_txg stays the same.
878 * - Setting the flag is not really necessary if
879 * scn_cur_max_txg == scn_max_txg, because there
880 * is nothing after this snapshot that we care
881 * about. However, we set it anyway and then
882 * ignore it when we retraverse it in
883 * dsl_scan_visitds().
885 scn
->scn_phys
.scn_bookmark
.zb_objset
=
886 dsl_dataset_phys(ds
)->ds_next_snap_obj
;
887 zfs_dbgmsg("destroying ds %llu; currently traversing; "
888 "reset zb_objset to %llu",
889 (u_longlong_t
)ds
->ds_object
,
890 (u_longlong_t
)dsl_dataset_phys(ds
)->
892 scn
->scn_phys
.scn_flags
|= DSF_VISIT_DS_AGAIN
;
894 SET_BOOKMARK(&scn
->scn_phys
.scn_bookmark
,
895 ZB_DESTROYED_OBJSET
, 0, 0, 0);
896 zfs_dbgmsg("destroying ds %llu; currently traversing; "
897 "reset bookmark to -1,0,0,0",
898 (u_longlong_t
)ds
->ds_object
);
900 } else if (zap_lookup_int_key(dp
->dp_meta_objset
,
901 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
, &mintxg
) == 0) {
902 ASSERT3U(dsl_dataset_phys(ds
)->ds_num_children
, <=, 1);
903 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
904 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
, tx
));
905 if (ds
->ds_is_snapshot
) {
907 * We keep the same mintxg; it could be >
908 * ds_creation_txg if the previous snapshot was
911 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
912 scn
->scn_phys
.scn_queue_obj
,
913 dsl_dataset_phys(ds
)->ds_next_snap_obj
,
915 zfs_dbgmsg("destroying ds %llu; in queue; "
916 "replacing with %llu",
917 (u_longlong_t
)ds
->ds_object
,
918 (u_longlong_t
)dsl_dataset_phys(ds
)->
921 zfs_dbgmsg("destroying ds %llu; in queue; removing",
922 (u_longlong_t
)ds
->ds_object
);
927 * dsl_scan_sync() should be called after this, and should sync
928 * out our changed state, but just to be safe, do it here.
930 dsl_scan_sync_state(scn
, tx
);
934 dsl_scan_ds_snapshotted(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
936 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
937 dsl_scan_t
*scn
= dp
->dp_scan
;
940 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
943 ASSERT(dsl_dataset_phys(ds
)->ds_prev_snap_obj
!= 0);
945 if (scn
->scn_phys
.scn_bookmark
.zb_objset
== ds
->ds_object
) {
946 scn
->scn_phys
.scn_bookmark
.zb_objset
=
947 dsl_dataset_phys(ds
)->ds_prev_snap_obj
;
948 zfs_dbgmsg("snapshotting ds %llu; currently traversing; "
949 "reset zb_objset to %llu",
950 (u_longlong_t
)ds
->ds_object
,
951 (u_longlong_t
)dsl_dataset_phys(ds
)->ds_prev_snap_obj
);
952 } else if (zap_lookup_int_key(dp
->dp_meta_objset
,
953 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
, &mintxg
) == 0) {
954 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
955 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
, tx
));
956 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
957 scn
->scn_phys
.scn_queue_obj
,
958 dsl_dataset_phys(ds
)->ds_prev_snap_obj
, mintxg
, tx
) == 0);
959 zfs_dbgmsg("snapshotting ds %llu; in queue; "
960 "replacing with %llu",
961 (u_longlong_t
)ds
->ds_object
,
962 (u_longlong_t
)dsl_dataset_phys(ds
)->ds_prev_snap_obj
);
964 dsl_scan_sync_state(scn
, tx
);
968 dsl_scan_ds_clone_swapped(dsl_dataset_t
*ds1
, dsl_dataset_t
*ds2
, dmu_tx_t
*tx
)
970 dsl_pool_t
*dp
= ds1
->ds_dir
->dd_pool
;
971 dsl_scan_t
*scn
= dp
->dp_scan
;
974 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
977 if (scn
->scn_phys
.scn_bookmark
.zb_objset
== ds1
->ds_object
) {
978 scn
->scn_phys
.scn_bookmark
.zb_objset
= ds2
->ds_object
;
979 zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
980 "reset zb_objset to %llu",
981 (u_longlong_t
)ds1
->ds_object
,
982 (u_longlong_t
)ds2
->ds_object
);
983 } else if (scn
->scn_phys
.scn_bookmark
.zb_objset
== ds2
->ds_object
) {
984 scn
->scn_phys
.scn_bookmark
.zb_objset
= ds1
->ds_object
;
985 zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
986 "reset zb_objset to %llu",
987 (u_longlong_t
)ds2
->ds_object
,
988 (u_longlong_t
)ds1
->ds_object
);
991 if (zap_lookup_int_key(dp
->dp_meta_objset
, scn
->scn_phys
.scn_queue_obj
,
992 ds1
->ds_object
, &mintxg
) == 0) {
995 ASSERT3U(mintxg
, ==, dsl_dataset_phys(ds1
)->ds_prev_snap_txg
);
996 ASSERT3U(mintxg
, ==, dsl_dataset_phys(ds2
)->ds_prev_snap_txg
);
997 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
998 scn
->scn_phys
.scn_queue_obj
, ds1
->ds_object
, tx
));
999 err
= zap_add_int_key(dp
->dp_meta_objset
,
1000 scn
->scn_phys
.scn_queue_obj
, ds2
->ds_object
, mintxg
, tx
);
1001 VERIFY(err
== 0 || err
== EEXIST
);
1002 if (err
== EEXIST
) {
1003 /* Both were there to begin with */
1004 VERIFY(0 == zap_add_int_key(dp
->dp_meta_objset
,
1005 scn
->scn_phys
.scn_queue_obj
,
1006 ds1
->ds_object
, mintxg
, tx
));
1008 zfs_dbgmsg("clone_swap ds %llu; in queue; "
1009 "replacing with %llu",
1010 (u_longlong_t
)ds1
->ds_object
,
1011 (u_longlong_t
)ds2
->ds_object
);
1012 } else if (zap_lookup_int_key(dp
->dp_meta_objset
,
1013 scn
->scn_phys
.scn_queue_obj
, ds2
->ds_object
, &mintxg
) == 0) {
1014 ASSERT3U(mintxg
, ==, dsl_dataset_phys(ds1
)->ds_prev_snap_txg
);
1015 ASSERT3U(mintxg
, ==, dsl_dataset_phys(ds2
)->ds_prev_snap_txg
);
1016 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
1017 scn
->scn_phys
.scn_queue_obj
, ds2
->ds_object
, tx
));
1018 VERIFY(0 == zap_add_int_key(dp
->dp_meta_objset
,
1019 scn
->scn_phys
.scn_queue_obj
, ds1
->ds_object
, mintxg
, tx
));
1020 zfs_dbgmsg("clone_swap ds %llu; in queue; "
1021 "replacing with %llu",
1022 (u_longlong_t
)ds2
->ds_object
,
1023 (u_longlong_t
)ds1
->ds_object
);
1026 dsl_scan_sync_state(scn
, tx
);
1029 struct enqueue_clones_arg
{
1036 enqueue_clones_cb(dsl_pool_t
*dp
, dsl_dataset_t
*hds
, void *arg
)
1038 struct enqueue_clones_arg
*eca
= arg
;
1041 dsl_scan_t
*scn
= dp
->dp_scan
;
1043 if (dsl_dir_phys(hds
->ds_dir
)->dd_origin_obj
!= eca
->originobj
)
1046 err
= dsl_dataset_hold_obj(dp
, hds
->ds_object
, FTAG
, &ds
);
1050 while (dsl_dataset_phys(ds
)->ds_prev_snap_obj
!= eca
->originobj
) {
1051 dsl_dataset_t
*prev
;
1052 err
= dsl_dataset_hold_obj(dp
,
1053 dsl_dataset_phys(ds
)->ds_prev_snap_obj
, FTAG
, &prev
);
1055 dsl_dataset_rele(ds
, FTAG
);
1060 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
1061 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
,
1062 dsl_dataset_phys(ds
)->ds_prev_snap_txg
, eca
->tx
) == 0);
1063 dsl_dataset_rele(ds
, FTAG
);
1068 dsl_scan_visitds(dsl_scan_t
*scn
, uint64_t dsobj
, dmu_tx_t
*tx
)
1070 dsl_pool_t
*dp
= scn
->scn_dp
;
1075 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp
, dsobj
, FTAG
, &ds
));
1077 if (scn
->scn_phys
.scn_cur_min_txg
>=
1078 scn
->scn_phys
.scn_max_txg
) {
1080 * This can happen if this snapshot was created after the
1081 * scan started, and we already completed a previous snapshot
1082 * that was created after the scan started. This snapshot
1083 * only references blocks with:
1085 * birth < our ds_creation_txg
1086 * cur_min_txg is no less than ds_creation_txg.
1087 * We have already visited these blocks.
1089 * birth > scn_max_txg
1090 * The scan requested not to visit these blocks.
1092 * Subsequent snapshots (and clones) can reference our
1093 * blocks, or blocks with even higher birth times.
1094 * Therefore we do not need to visit them either,
1095 * so we do not add them to the work queue.
1097 * Note that checking for cur_min_txg >= cur_max_txg
1098 * is not sufficient, because in that case we may need to
1099 * visit subsequent snapshots. This happens when min_txg > 0,
1100 * which raises cur_min_txg. In this case we will visit
1101 * this dataset but skip all of its blocks, because the
1102 * rootbp's birth time is < cur_min_txg. Then we will
1103 * add the next snapshots/clones to the work queue.
1105 char *dsname
= kmem_alloc(MAXNAMELEN
, KM_SLEEP
);
1106 dsl_dataset_name(ds
, dsname
);
1107 zfs_dbgmsg("scanning dataset %llu (%s) is unnecessary because "
1108 "cur_min_txg (%llu) >= max_txg (%llu)",
1110 scn
->scn_phys
.scn_cur_min_txg
,
1111 scn
->scn_phys
.scn_max_txg
);
1112 kmem_free(dsname
, MAXNAMELEN
);
1117 if (dmu_objset_from_ds(ds
, &os
))
1121 * Only the ZIL in the head (non-snapshot) is valid. Even though
1122 * snapshots can have ZIL block pointers (which may be the same
1123 * BP as in the head), they must be ignored. So we traverse the
1124 * ZIL here, rather than in scan_recurse(), because the regular
1125 * snapshot block-sharing rules don't apply to it.
1127 if (DSL_SCAN_IS_SCRUB_RESILVER(scn
) && !ds
->ds_is_snapshot
)
1128 dsl_scan_zil(dp
, &os
->os_zil_header
);
1131 * Iterate over the bps in this ds.
1133 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
1134 dsl_scan_visit_rootbp(scn
, ds
, &dsl_dataset_phys(ds
)->ds_bp
, tx
);
1136 dsname
= kmem_alloc(ZFS_MAXNAMELEN
, KM_SLEEP
);
1137 dsl_dataset_name(ds
, dsname
);
1138 zfs_dbgmsg("scanned dataset %llu (%s) with min=%llu max=%llu; "
1140 (longlong_t
)dsobj
, dsname
,
1141 (longlong_t
)scn
->scn_phys
.scn_cur_min_txg
,
1142 (longlong_t
)scn
->scn_phys
.scn_cur_max_txg
,
1143 (int)scn
->scn_pausing
);
1144 kmem_free(dsname
, ZFS_MAXNAMELEN
);
1146 if (scn
->scn_pausing
)
1150 * We've finished this pass over this dataset.
1154 * If we did not completely visit this dataset, do another pass.
1156 if (scn
->scn_phys
.scn_flags
& DSF_VISIT_DS_AGAIN
) {
1157 zfs_dbgmsg("incomplete pass; visiting again");
1158 scn
->scn_phys
.scn_flags
&= ~DSF_VISIT_DS_AGAIN
;
1159 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
1160 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
,
1161 scn
->scn_phys
.scn_cur_max_txg
, tx
) == 0);
1166 * Add descendent datasets to work queue.
1168 if (dsl_dataset_phys(ds
)->ds_next_snap_obj
!= 0) {
1169 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
1170 scn
->scn_phys
.scn_queue_obj
,
1171 dsl_dataset_phys(ds
)->ds_next_snap_obj
,
1172 dsl_dataset_phys(ds
)->ds_creation_txg
, tx
) == 0);
1174 if (dsl_dataset_phys(ds
)->ds_num_children
> 1) {
1175 boolean_t usenext
= B_FALSE
;
1176 if (dsl_dataset_phys(ds
)->ds_next_clones_obj
!= 0) {
1179 * A bug in a previous version of the code could
1180 * cause upgrade_clones_cb() to not set
1181 * ds_next_snap_obj when it should, leading to a
1182 * missing entry. Therefore we can only use the
1183 * next_clones_obj when its count is correct.
1185 int err
= zap_count(dp
->dp_meta_objset
,
1186 dsl_dataset_phys(ds
)->ds_next_clones_obj
, &count
);
1188 count
== dsl_dataset_phys(ds
)->ds_num_children
- 1)
1193 VERIFY0(zap_join_key(dp
->dp_meta_objset
,
1194 dsl_dataset_phys(ds
)->ds_next_clones_obj
,
1195 scn
->scn_phys
.scn_queue_obj
,
1196 dsl_dataset_phys(ds
)->ds_creation_txg
, tx
));
1198 struct enqueue_clones_arg eca
;
1200 eca
.originobj
= ds
->ds_object
;
1202 VERIFY0(dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
1203 enqueue_clones_cb
, &eca
, DS_FIND_CHILDREN
));
1208 dsl_dataset_rele(ds
, FTAG
);
1213 enqueue_cb(dsl_pool_t
*dp
, dsl_dataset_t
*hds
, void *arg
)
1218 dsl_scan_t
*scn
= dp
->dp_scan
;
1220 err
= dsl_dataset_hold_obj(dp
, hds
->ds_object
, FTAG
, &ds
);
1224 while (dsl_dataset_phys(ds
)->ds_prev_snap_obj
!= 0) {
1225 dsl_dataset_t
*prev
;
1226 err
= dsl_dataset_hold_obj(dp
,
1227 dsl_dataset_phys(ds
)->ds_prev_snap_obj
, FTAG
, &prev
);
1229 dsl_dataset_rele(ds
, FTAG
);
1234 * If this is a clone, we don't need to worry about it for now.
1236 if (dsl_dataset_phys(prev
)->ds_next_snap_obj
!= ds
->ds_object
) {
1237 dsl_dataset_rele(ds
, FTAG
);
1238 dsl_dataset_rele(prev
, FTAG
);
1241 dsl_dataset_rele(ds
, FTAG
);
1245 VERIFY(zap_add_int_key(dp
->dp_meta_objset
, scn
->scn_phys
.scn_queue_obj
,
1246 ds
->ds_object
, dsl_dataset_phys(ds
)->ds_prev_snap_txg
, tx
) == 0);
1247 dsl_dataset_rele(ds
, FTAG
);
1252 * Scrub/dedup interaction.
1254 * If there are N references to a deduped block, we don't want to scrub it
1255 * N times -- ideally, we should scrub it exactly once.
1257 * We leverage the fact that the dde's replication class (enum ddt_class)
1258 * is ordered from highest replication class (DDT_CLASS_DITTO) to lowest
1259 * (DDT_CLASS_UNIQUE) so that we may walk the DDT in that order.
1261 * To prevent excess scrubbing, the scrub begins by walking the DDT
1262 * to find all blocks with refcnt > 1, and scrubs each of these once.
1263 * Since there are two replication classes which contain blocks with
1264 * refcnt > 1, we scrub the highest replication class (DDT_CLASS_DITTO) first.
1265 * Finally the top-down scrub begins, only visiting blocks with refcnt == 1.
1267 * There would be nothing more to say if a block's refcnt couldn't change
1268 * during a scrub, but of course it can so we must account for changes
1269 * in a block's replication class.
1271 * Here's an example of what can occur:
1273 * If a block has refcnt > 1 during the DDT scrub phase, but has refcnt == 1
1274 * when visited during the top-down scrub phase, it will be scrubbed twice.
1275 * This negates our scrub optimization, but is otherwise harmless.
1277 * If a block has refcnt == 1 during the DDT scrub phase, but has refcnt > 1
1278 * on each visit during the top-down scrub phase, it will never be scrubbed.
1279 * To catch this, ddt_sync_entry() notifies the scrub code whenever a block's
1280 * reference class transitions to a higher level (i.e DDT_CLASS_UNIQUE to
1281 * DDT_CLASS_DUPLICATE); if it transitions from refcnt == 1 to refcnt > 1
1282 * while a scrub is in progress, it scrubs the block right then.
1285 dsl_scan_ddt(dsl_scan_t
*scn
, dmu_tx_t
*tx
)
1287 ddt_bookmark_t
*ddb
= &scn
->scn_phys
.scn_ddt_bookmark
;
1292 bzero(&dde
, sizeof (ddt_entry_t
));
1294 while ((error
= ddt_walk(scn
->scn_dp
->dp_spa
, ddb
, &dde
)) == 0) {
1297 if (ddb
->ddb_class
> scn
->scn_phys
.scn_ddt_class_max
)
1299 dprintf("visiting ddb=%llu/%llu/%llu/%llx\n",
1300 (longlong_t
)ddb
->ddb_class
,
1301 (longlong_t
)ddb
->ddb_type
,
1302 (longlong_t
)ddb
->ddb_checksum
,
1303 (longlong_t
)ddb
->ddb_cursor
);
1305 /* There should be no pending changes to the dedup table */
1306 ddt
= scn
->scn_dp
->dp_spa
->spa_ddt
[ddb
->ddb_checksum
];
1307 ASSERT(avl_first(&ddt
->ddt_tree
) == NULL
);
1309 dsl_scan_ddt_entry(scn
, ddb
->ddb_checksum
, &dde
, tx
);
1312 if (dsl_scan_check_pause(scn
, NULL
))
1316 zfs_dbgmsg("scanned %llu ddt entries with class_max = %u; pausing=%u",
1317 (longlong_t
)n
, (int)scn
->scn_phys
.scn_ddt_class_max
,
1318 (int)scn
->scn_pausing
);
1320 ASSERT(error
== 0 || error
== ENOENT
);
1321 ASSERT(error
!= ENOENT
||
1322 ddb
->ddb_class
> scn
->scn_phys
.scn_ddt_class_max
);
1327 dsl_scan_ddt_entry(dsl_scan_t
*scn
, enum zio_checksum checksum
,
1328 ddt_entry_t
*dde
, dmu_tx_t
*tx
)
1330 const ddt_key_t
*ddk
= &dde
->dde_key
;
1331 ddt_phys_t
*ddp
= dde
->dde_phys
;
1333 zbookmark_phys_t zb
= { 0 };
1336 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
1339 for (p
= 0; p
< DDT_PHYS_TYPES
; p
++, ddp
++) {
1340 if (ddp
->ddp_phys_birth
== 0 ||
1341 ddp
->ddp_phys_birth
> scn
->scn_phys
.scn_max_txg
)
1343 ddt_bp_create(checksum
, ddk
, ddp
, &bp
);
1345 scn
->scn_visited_this_txg
++;
1346 scan_funcs
[scn
->scn_phys
.scn_func
](scn
->scn_dp
, &bp
, &zb
);
1351 dsl_scan_visit(dsl_scan_t
*scn
, dmu_tx_t
*tx
)
1353 dsl_pool_t
*dp
= scn
->scn_dp
;
1355 zap_attribute_t
*za
;
1357 if (scn
->scn_phys
.scn_ddt_bookmark
.ddb_class
<=
1358 scn
->scn_phys
.scn_ddt_class_max
) {
1359 scn
->scn_phys
.scn_cur_min_txg
= scn
->scn_phys
.scn_min_txg
;
1360 scn
->scn_phys
.scn_cur_max_txg
= scn
->scn_phys
.scn_max_txg
;
1361 dsl_scan_ddt(scn
, tx
);
1362 if (scn
->scn_pausing
)
1366 if (scn
->scn_phys
.scn_bookmark
.zb_objset
== DMU_META_OBJSET
) {
1367 /* First do the MOS & ORIGIN */
1369 scn
->scn_phys
.scn_cur_min_txg
= scn
->scn_phys
.scn_min_txg
;
1370 scn
->scn_phys
.scn_cur_max_txg
= scn
->scn_phys
.scn_max_txg
;
1371 dsl_scan_visit_rootbp(scn
, NULL
,
1372 &dp
->dp_meta_rootbp
, tx
);
1373 spa_set_rootblkptr(dp
->dp_spa
, &dp
->dp_meta_rootbp
);
1374 if (scn
->scn_pausing
)
1377 if (spa_version(dp
->dp_spa
) < SPA_VERSION_DSL_SCRUB
) {
1378 VERIFY0(dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
1379 enqueue_cb
, tx
, DS_FIND_CHILDREN
));
1381 dsl_scan_visitds(scn
,
1382 dp
->dp_origin_snap
->ds_object
, tx
);
1384 ASSERT(!scn
->scn_pausing
);
1385 } else if (scn
->scn_phys
.scn_bookmark
.zb_objset
!=
1386 ZB_DESTROYED_OBJSET
) {
1388 * If we were paused, continue from here. Note if the
1389 * ds we were paused on was deleted, the zb_objset may
1390 * be -1, so we will skip this and find a new objset
1393 dsl_scan_visitds(scn
, scn
->scn_phys
.scn_bookmark
.zb_objset
, tx
);
1394 if (scn
->scn_pausing
)
1399 * In case we were paused right at the end of the ds, zero the
1400 * bookmark so we don't think that we're still trying to resume.
1402 bzero(&scn
->scn_phys
.scn_bookmark
, sizeof (zbookmark_phys_t
));
1403 zc
= kmem_alloc(sizeof (zap_cursor_t
), KM_SLEEP
);
1404 za
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
1406 /* keep pulling things out of the zap-object-as-queue */
1407 while (zap_cursor_init(zc
, dp
->dp_meta_objset
,
1408 scn
->scn_phys
.scn_queue_obj
),
1409 zap_cursor_retrieve(zc
, za
) == 0) {
1413 dsobj
= strtonum(za
->za_name
, NULL
);
1414 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
1415 scn
->scn_phys
.scn_queue_obj
, dsobj
, tx
));
1417 /* Set up min/max txg */
1418 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp
, dsobj
, FTAG
, &ds
));
1419 if (za
->za_first_integer
!= 0) {
1420 scn
->scn_phys
.scn_cur_min_txg
=
1421 MAX(scn
->scn_phys
.scn_min_txg
,
1422 za
->za_first_integer
);
1424 scn
->scn_phys
.scn_cur_min_txg
=
1425 MAX(scn
->scn_phys
.scn_min_txg
,
1426 dsl_dataset_phys(ds
)->ds_prev_snap_txg
);
1428 scn
->scn_phys
.scn_cur_max_txg
= dsl_scan_ds_maxtxg(ds
);
1429 dsl_dataset_rele(ds
, FTAG
);
1431 dsl_scan_visitds(scn
, dsobj
, tx
);
1432 zap_cursor_fini(zc
);
1433 if (scn
->scn_pausing
)
1436 zap_cursor_fini(zc
);
1438 kmem_free(za
, sizeof (zap_attribute_t
));
1439 kmem_free(zc
, sizeof (zap_cursor_t
));
1443 dsl_scan_free_should_pause(dsl_scan_t
*scn
)
1445 uint64_t elapsed_nanosecs
;
1450 if (scn
->scn_visited_this_txg
>= zfs_free_max_blocks
)
1453 elapsed_nanosecs
= gethrtime() - scn
->scn_sync_start_time
;
1454 return (elapsed_nanosecs
/ NANOSEC
> zfs_txg_timeout
||
1455 (NSEC2MSEC(elapsed_nanosecs
) > zfs_free_min_time_ms
&&
1456 txg_sync_waiting(scn
->scn_dp
)) ||
1457 spa_shutting_down(scn
->scn_dp
->dp_spa
));
1461 dsl_scan_free_block_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
1463 dsl_scan_t
*scn
= arg
;
1465 if (!scn
->scn_is_bptree
||
1466 (BP_GET_LEVEL(bp
) == 0 && BP_GET_TYPE(bp
) != DMU_OT_OBJSET
)) {
1467 if (dsl_scan_free_should_pause(scn
))
1468 return (SET_ERROR(ERESTART
));
1471 zio_nowait(zio_free_sync(scn
->scn_zio_root
, scn
->scn_dp
->dp_spa
,
1472 dmu_tx_get_txg(tx
), bp
, 0));
1473 dsl_dir_diduse_space(tx
->tx_pool
->dp_free_dir
, DD_USED_HEAD
,
1474 -bp_get_dsize_sync(scn
->scn_dp
->dp_spa
, bp
),
1475 -BP_GET_PSIZE(bp
), -BP_GET_UCSIZE(bp
), tx
);
1476 scn
->scn_visited_this_txg
++;
1481 dsl_scan_active(dsl_scan_t
*scn
)
1483 spa_t
*spa
= scn
->scn_dp
->dp_spa
;
1484 uint64_t used
= 0, comp
, uncomp
;
1486 if (spa
->spa_load_state
!= SPA_LOAD_NONE
)
1488 if (spa_shutting_down(spa
))
1490 if (scn
->scn_phys
.scn_state
== DSS_SCANNING
||
1491 (scn
->scn_async_destroying
&& !scn
->scn_async_stalled
))
1494 if (spa_version(scn
->scn_dp
->dp_spa
) >= SPA_VERSION_DEADLISTS
) {
1495 (void) bpobj_space(&scn
->scn_dp
->dp_free_bpobj
,
1496 &used
, &comp
, &uncomp
);
1502 dsl_scan_sync(dsl_pool_t
*dp
, dmu_tx_t
*tx
)
1504 dsl_scan_t
*scn
= dp
->dp_scan
;
1505 spa_t
*spa
= dp
->dp_spa
;
1509 * Check for scn_restart_txg before checking spa_load_state, so
1510 * that we can restart an old-style scan while the pool is being
1511 * imported (see dsl_scan_init).
1513 if (scn
->scn_restart_txg
!= 0 &&
1514 scn
->scn_restart_txg
<= tx
->tx_txg
) {
1515 pool_scan_func_t func
= POOL_SCAN_SCRUB
;
1516 dsl_scan_done(scn
, B_FALSE
, tx
);
1517 if (vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
))
1518 func
= POOL_SCAN_RESILVER
;
1519 zfs_dbgmsg("restarting scan func=%u txg=%llu",
1521 dsl_scan_setup_sync(&func
, tx
);
1525 * Only process scans in sync pass 1.
1527 if (spa_sync_pass(dp
->dp_spa
) > 1)
1531 * If the spa is shutting down, then stop scanning. This will
1532 * ensure that the scan does not dirty any new data during the
1535 if (spa_shutting_down(spa
))
1539 * If the scan is inactive due to a stalled async destroy, try again.
1541 if (!scn
->scn_async_stalled
&& !dsl_scan_active(scn
))
1544 scn
->scn_visited_this_txg
= 0;
1545 scn
->scn_pausing
= B_FALSE
;
1546 scn
->scn_sync_start_time
= gethrtime();
1547 spa
->spa_scrub_active
= B_TRUE
;
1550 * First process the async destroys. If we pause, don't do
1551 * any scrubbing or resilvering. This ensures that there are no
1552 * async destroys while we are scanning, so the scan code doesn't
1553 * have to worry about traversing it. It is also faster to free the
1554 * blocks than to scrub them.
1556 if (zfs_free_bpobj_enabled
&&
1557 spa_version(dp
->dp_spa
) >= SPA_VERSION_DEADLISTS
) {
1558 scn
->scn_is_bptree
= B_FALSE
;
1559 scn
->scn_zio_root
= zio_root(dp
->dp_spa
, NULL
,
1560 NULL
, ZIO_FLAG_MUSTSUCCEED
);
1561 err
= bpobj_iterate(&dp
->dp_free_bpobj
,
1562 dsl_scan_free_block_cb
, scn
, tx
);
1563 VERIFY3U(0, ==, zio_wait(scn
->scn_zio_root
));
1565 if (err
!= 0 && err
!= ERESTART
)
1566 zfs_panic_recover("error %u from bpobj_iterate()", err
);
1569 if (err
== 0 && spa_feature_is_active(spa
, SPA_FEATURE_ASYNC_DESTROY
)) {
1570 ASSERT(scn
->scn_async_destroying
);
1571 scn
->scn_is_bptree
= B_TRUE
;
1572 scn
->scn_zio_root
= zio_root(dp
->dp_spa
, NULL
,
1573 NULL
, ZIO_FLAG_MUSTSUCCEED
);
1574 err
= bptree_iterate(dp
->dp_meta_objset
,
1575 dp
->dp_bptree_obj
, B_TRUE
, dsl_scan_free_block_cb
, scn
, tx
);
1576 VERIFY0(zio_wait(scn
->scn_zio_root
));
1578 if (err
== EIO
|| err
== ECKSUM
) {
1580 } else if (err
!= 0 && err
!= ERESTART
) {
1581 zfs_panic_recover("error %u from "
1582 "traverse_dataset_destroyed()", err
);
1585 if (bptree_is_empty(dp
->dp_meta_objset
, dp
->dp_bptree_obj
)) {
1586 /* finished; deactivate async destroy feature */
1587 spa_feature_decr(spa
, SPA_FEATURE_ASYNC_DESTROY
, tx
);
1588 ASSERT(!spa_feature_is_active(spa
,
1589 SPA_FEATURE_ASYNC_DESTROY
));
1590 VERIFY0(zap_remove(dp
->dp_meta_objset
,
1591 DMU_POOL_DIRECTORY_OBJECT
,
1592 DMU_POOL_BPTREE_OBJ
, tx
));
1593 VERIFY0(bptree_free(dp
->dp_meta_objset
,
1594 dp
->dp_bptree_obj
, tx
));
1595 dp
->dp_bptree_obj
= 0;
1596 scn
->scn_async_destroying
= B_FALSE
;
1597 scn
->scn_async_stalled
= B_FALSE
;
1600 * If we didn't make progress, mark the async
1601 * destroy as stalled, so that we will not initiate
1602 * a spa_sync() on its behalf. Note that we only
1603 * check this if we are not finished, because if the
1604 * bptree had no blocks for us to visit, we can
1605 * finish without "making progress".
1607 scn
->scn_async_stalled
=
1608 (scn
->scn_visited_this_txg
== 0);
1611 if (scn
->scn_visited_this_txg
) {
1612 zfs_dbgmsg("freed %llu blocks in %llums from "
1613 "free_bpobj/bptree txg %llu; err=%u",
1614 (longlong_t
)scn
->scn_visited_this_txg
,
1616 NSEC2MSEC(gethrtime() - scn
->scn_sync_start_time
),
1617 (longlong_t
)tx
->tx_txg
, err
);
1618 scn
->scn_visited_this_txg
= 0;
1621 * Write out changes to the DDT that may be required as a
1622 * result of the blocks freed. This ensures that the DDT
1623 * is clean when a scrub/resilver runs.
1625 ddt_sync(spa
, tx
->tx_txg
);
1629 if (!scn
->scn_async_destroying
&& zfs_free_leak_on_eio
&&
1630 (dsl_dir_phys(dp
->dp_free_dir
)->dd_used_bytes
!= 0 ||
1631 dsl_dir_phys(dp
->dp_free_dir
)->dd_compressed_bytes
!= 0 ||
1632 dsl_dir_phys(dp
->dp_free_dir
)->dd_uncompressed_bytes
!= 0)) {
1634 * We have finished background destroying, but there is still
1635 * some space left in the dp_free_dir. Transfer this leaked
1636 * space to the dp_leak_dir.
1638 if (dp
->dp_leak_dir
== NULL
) {
1639 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
1640 (void) dsl_dir_create_sync(dp
, dp
->dp_root_dir
,
1642 VERIFY0(dsl_pool_open_special_dir(dp
,
1643 LEAK_DIR_NAME
, &dp
->dp_leak_dir
));
1644 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
1646 dsl_dir_diduse_space(dp
->dp_leak_dir
, DD_USED_HEAD
,
1647 dsl_dir_phys(dp
->dp_free_dir
)->dd_used_bytes
,
1648 dsl_dir_phys(dp
->dp_free_dir
)->dd_compressed_bytes
,
1649 dsl_dir_phys(dp
->dp_free_dir
)->dd_uncompressed_bytes
, tx
);
1650 dsl_dir_diduse_space(dp
->dp_free_dir
, DD_USED_HEAD
,
1651 -dsl_dir_phys(dp
->dp_free_dir
)->dd_used_bytes
,
1652 -dsl_dir_phys(dp
->dp_free_dir
)->dd_compressed_bytes
,
1653 -dsl_dir_phys(dp
->dp_free_dir
)->dd_uncompressed_bytes
, tx
);
1655 if (!scn
->scn_async_destroying
) {
1656 /* finished; verify that space accounting went to zero */
1657 ASSERT0(dsl_dir_phys(dp
->dp_free_dir
)->dd_used_bytes
);
1658 ASSERT0(dsl_dir_phys(dp
->dp_free_dir
)->dd_compressed_bytes
);
1659 ASSERT0(dsl_dir_phys(dp
->dp_free_dir
)->dd_uncompressed_bytes
);
1662 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
1665 if (scn
->scn_done_txg
== tx
->tx_txg
) {
1666 ASSERT(!scn
->scn_pausing
);
1667 /* finished with scan. */
1668 zfs_dbgmsg("txg %llu scan complete", tx
->tx_txg
);
1669 dsl_scan_done(scn
, B_TRUE
, tx
);
1670 ASSERT3U(spa
->spa_scrub_inflight
, ==, 0);
1671 dsl_scan_sync_state(scn
, tx
);
1675 if (scn
->scn_phys
.scn_ddt_bookmark
.ddb_class
<=
1676 scn
->scn_phys
.scn_ddt_class_max
) {
1677 zfs_dbgmsg("doing scan sync txg %llu; "
1678 "ddt bm=%llu/%llu/%llu/%llx",
1679 (longlong_t
)tx
->tx_txg
,
1680 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_class
,
1681 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_type
,
1682 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_checksum
,
1683 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_cursor
);
1684 ASSERT(scn
->scn_phys
.scn_bookmark
.zb_objset
== 0);
1685 ASSERT(scn
->scn_phys
.scn_bookmark
.zb_object
== 0);
1686 ASSERT(scn
->scn_phys
.scn_bookmark
.zb_level
== 0);
1687 ASSERT(scn
->scn_phys
.scn_bookmark
.zb_blkid
== 0);
1689 zfs_dbgmsg("doing scan sync txg %llu; bm=%llu/%llu/%llu/%llu",
1690 (longlong_t
)tx
->tx_txg
,
1691 (longlong_t
)scn
->scn_phys
.scn_bookmark
.zb_objset
,
1692 (longlong_t
)scn
->scn_phys
.scn_bookmark
.zb_object
,
1693 (longlong_t
)scn
->scn_phys
.scn_bookmark
.zb_level
,
1694 (longlong_t
)scn
->scn_phys
.scn_bookmark
.zb_blkid
);
1697 scn
->scn_zio_root
= zio_root(dp
->dp_spa
, NULL
,
1698 NULL
, ZIO_FLAG_CANFAIL
);
1699 dsl_pool_config_enter(dp
, FTAG
);
1700 dsl_scan_visit(scn
, tx
);
1701 dsl_pool_config_exit(dp
, FTAG
);
1702 (void) zio_wait(scn
->scn_zio_root
);
1703 scn
->scn_zio_root
= NULL
;
1705 zfs_dbgmsg("visited %llu blocks in %llums",
1706 (longlong_t
)scn
->scn_visited_this_txg
,
1707 (longlong_t
)NSEC2MSEC(gethrtime() - scn
->scn_sync_start_time
));
1709 if (!scn
->scn_pausing
) {
1710 scn
->scn_done_txg
= tx
->tx_txg
+ 1;
1711 zfs_dbgmsg("txg %llu traversal complete, waiting till txg %llu",
1712 tx
->tx_txg
, scn
->scn_done_txg
);
1715 if (DSL_SCAN_IS_SCRUB_RESILVER(scn
)) {
1716 mutex_enter(&spa
->spa_scrub_lock
);
1717 while (spa
->spa_scrub_inflight
> 0) {
1718 cv_wait(&spa
->spa_scrub_io_cv
,
1719 &spa
->spa_scrub_lock
);
1721 mutex_exit(&spa
->spa_scrub_lock
);
1724 dsl_scan_sync_state(scn
, tx
);
1728 * This will start a new scan, or restart an existing one.
1731 dsl_resilver_restart(dsl_pool_t
*dp
, uint64_t txg
)
1735 tx
= dmu_tx_create_dd(dp
->dp_mos_dir
);
1736 VERIFY(0 == dmu_tx_assign(tx
, TXG_WAIT
));
1738 txg
= dmu_tx_get_txg(tx
);
1739 dp
->dp_scan
->scn_restart_txg
= txg
;
1742 dp
->dp_scan
->scn_restart_txg
= txg
;
1744 zfs_dbgmsg("restarting resilver txg=%llu", txg
);
1748 dsl_scan_resilvering(dsl_pool_t
*dp
)
1750 return (dp
->dp_scan
->scn_phys
.scn_state
== DSS_SCANNING
&&
1751 dp
->dp_scan
->scn_phys
.scn_func
== POOL_SCAN_RESILVER
);
1759 count_block(zfs_all_blkstats_t
*zab
, const blkptr_t
*bp
)
1764 * If we resume after a reboot, zab will be NULL; don't record
1765 * incomplete stats in that case.
1770 for (i
= 0; i
< 4; i
++) {
1771 int l
= (i
< 2) ? BP_GET_LEVEL(bp
) : DN_MAX_LEVELS
;
1772 int t
= (i
& 1) ? BP_GET_TYPE(bp
) : DMU_OT_TOTAL
;
1776 if (t
& DMU_OT_NEWTYPE
)
1779 zb
= &zab
->zab_type
[l
][t
];
1781 zb
->zb_asize
+= BP_GET_ASIZE(bp
);
1782 zb
->zb_lsize
+= BP_GET_LSIZE(bp
);
1783 zb
->zb_psize
+= BP_GET_PSIZE(bp
);
1784 zb
->zb_gangs
+= BP_COUNT_GANG(bp
);
1786 switch (BP_GET_NDVAS(bp
)) {
1788 if (DVA_GET_VDEV(&bp
->blk_dva
[0]) ==
1789 DVA_GET_VDEV(&bp
->blk_dva
[1]))
1790 zb
->zb_ditto_2_of_2_samevdev
++;
1793 equal
= (DVA_GET_VDEV(&bp
->blk_dva
[0]) ==
1794 DVA_GET_VDEV(&bp
->blk_dva
[1])) +
1795 (DVA_GET_VDEV(&bp
->blk_dva
[0]) ==
1796 DVA_GET_VDEV(&bp
->blk_dva
[2])) +
1797 (DVA_GET_VDEV(&bp
->blk_dva
[1]) ==
1798 DVA_GET_VDEV(&bp
->blk_dva
[2]));
1800 zb
->zb_ditto_2_of_3_samevdev
++;
1801 else if (equal
== 3)
1802 zb
->zb_ditto_3_of_3_samevdev
++;
1809 dsl_scan_scrub_done(zio_t
*zio
)
1811 spa_t
*spa
= zio
->io_spa
;
1813 zio_data_buf_free(zio
->io_data
, zio
->io_size
);
1815 mutex_enter(&spa
->spa_scrub_lock
);
1816 spa
->spa_scrub_inflight
--;
1817 cv_broadcast(&spa
->spa_scrub_io_cv
);
1819 if (zio
->io_error
&& (zio
->io_error
!= ECKSUM
||
1820 !(zio
->io_flags
& ZIO_FLAG_SPECULATIVE
))) {
1821 spa
->spa_dsl_pool
->dp_scan
->scn_phys
.scn_errors
++;
1823 mutex_exit(&spa
->spa_scrub_lock
);
1827 dsl_scan_scrub_cb(dsl_pool_t
*dp
,
1828 const blkptr_t
*bp
, const zbookmark_phys_t
*zb
)
1830 dsl_scan_t
*scn
= dp
->dp_scan
;
1831 size_t size
= BP_GET_PSIZE(bp
);
1832 spa_t
*spa
= dp
->dp_spa
;
1833 uint64_t phys_birth
= BP_PHYSICAL_BIRTH(bp
);
1834 boolean_t needs_io
= B_FALSE
;
1835 int zio_flags
= ZIO_FLAG_SCAN_THREAD
| ZIO_FLAG_RAW
| ZIO_FLAG_CANFAIL
;
1839 if (phys_birth
<= scn
->scn_phys
.scn_min_txg
||
1840 phys_birth
>= scn
->scn_phys
.scn_max_txg
)
1843 count_block(dp
->dp_blkstats
, bp
);
1845 if (BP_IS_EMBEDDED(bp
))
1848 ASSERT(DSL_SCAN_IS_SCRUB_RESILVER(scn
));
1849 if (scn
->scn_phys
.scn_func
== POOL_SCAN_SCRUB
) {
1850 zio_flags
|= ZIO_FLAG_SCRUB
;
1852 scan_delay
= zfs_scrub_delay
;
1854 ASSERT3U(scn
->scn_phys
.scn_func
, ==, POOL_SCAN_RESILVER
);
1855 zio_flags
|= ZIO_FLAG_RESILVER
;
1857 scan_delay
= zfs_resilver_delay
;
1860 /* If it's an intent log block, failure is expected. */
1861 if (zb
->zb_level
== ZB_ZIL_LEVEL
)
1862 zio_flags
|= ZIO_FLAG_SPECULATIVE
;
1864 for (d
= 0; d
< BP_GET_NDVAS(bp
); d
++) {
1865 vdev_t
*vd
= vdev_lookup_top(spa
,
1866 DVA_GET_VDEV(&bp
->blk_dva
[d
]));
1869 * Keep track of how much data we've examined so that
1870 * zpool(1M) status can make useful progress reports.
1872 scn
->scn_phys
.scn_examined
+= DVA_GET_ASIZE(&bp
->blk_dva
[d
]);
1873 spa
->spa_scan_pass_exam
+= DVA_GET_ASIZE(&bp
->blk_dva
[d
]);
1875 /* if it's a resilver, this may not be in the target range */
1877 if (DVA_GET_GANG(&bp
->blk_dva
[d
])) {
1879 * Gang members may be spread across multiple
1880 * vdevs, so the best estimate we have is the
1881 * scrub range, which has already been checked.
1882 * XXX -- it would be better to change our
1883 * allocation policy to ensure that all
1884 * gang members reside on the same vdev.
1888 needs_io
= vdev_dtl_contains(vd
, DTL_PARTIAL
,
1894 if (needs_io
&& !zfs_no_scrub_io
) {
1895 vdev_t
*rvd
= spa
->spa_root_vdev
;
1896 uint64_t maxinflight
= rvd
->vdev_children
* zfs_top_maxinflight
;
1897 void *data
= zio_data_buf_alloc(size
);
1899 mutex_enter(&spa
->spa_scrub_lock
);
1900 while (spa
->spa_scrub_inflight
>= maxinflight
)
1901 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
1902 spa
->spa_scrub_inflight
++;
1903 mutex_exit(&spa
->spa_scrub_lock
);
1906 * If we're seeing recent (zfs_scan_idle) "important" I/Os
1907 * then throttle our workload to limit the impact of a scan.
1909 if (ddi_get_lbolt64() - spa
->spa_last_io
<= zfs_scan_idle
)
1912 zio_nowait(zio_read(NULL
, spa
, bp
, data
, size
,
1913 dsl_scan_scrub_done
, NULL
, ZIO_PRIORITY_SCRUB
,
1917 /* do not relocate this block */
1922 dsl_scan(dsl_pool_t
*dp
, pool_scan_func_t func
)
1924 spa_t
*spa
= dp
->dp_spa
;
1927 * Purge all vdev caches and probe all devices. We do this here
1928 * rather than in sync context because this requires a writer lock
1929 * on the spa_config lock, which we can't do from sync context. The
1930 * spa_scrub_reopen flag indicates that vdev_open() should not
1931 * attempt to start another scrub.
1933 spa_vdev_state_enter(spa
, SCL_NONE
);
1934 spa
->spa_scrub_reopen
= B_TRUE
;
1935 vdev_reopen(spa
->spa_root_vdev
);
1936 spa
->spa_scrub_reopen
= B_FALSE
;
1937 (void) spa_vdev_state_exit(spa
, NULL
, 0);
1939 return (dsl_sync_task(spa_name(spa
), dsl_scan_setup_check
,
1940 dsl_scan_setup_sync
, &func
, 0, ZFS_SPACE_CHECK_NONE
));
1943 #if defined(_KERNEL) && defined(HAVE_SPL)
1944 module_param(zfs_top_maxinflight
, int, 0644);
1945 MODULE_PARM_DESC(zfs_top_maxinflight
, "Max I/Os per top-level");
1947 module_param(zfs_resilver_delay
, int, 0644);
1948 MODULE_PARM_DESC(zfs_resilver_delay
, "Number of ticks to delay resilver");
1950 module_param(zfs_scrub_delay
, int, 0644);
1951 MODULE_PARM_DESC(zfs_scrub_delay
, "Number of ticks to delay scrub");
1953 module_param(zfs_scan_idle
, int, 0644);
1954 MODULE_PARM_DESC(zfs_scan_idle
, "Idle window in clock ticks");
1956 module_param(zfs_scan_min_time_ms
, int, 0644);
1957 MODULE_PARM_DESC(zfs_scan_min_time_ms
, "Min millisecs to scrub per txg");
1959 module_param(zfs_free_min_time_ms
, int, 0644);
1960 MODULE_PARM_DESC(zfs_free_min_time_ms
, "Min millisecs to free per txg");
1962 module_param(zfs_resilver_min_time_ms
, int, 0644);
1963 MODULE_PARM_DESC(zfs_resilver_min_time_ms
, "Min millisecs to resilver per txg");
1965 module_param(zfs_no_scrub_io
, int, 0644);
1966 MODULE_PARM_DESC(zfs_no_scrub_io
, "Set to disable scrub I/O");
1968 module_param(zfs_no_scrub_prefetch
, int, 0644);
1969 MODULE_PARM_DESC(zfs_no_scrub_prefetch
, "Set to disable scrub prefetching");
1971 module_param(zfs_free_max_blocks
, ulong
, 0644);
1972 MODULE_PARM_DESC(zfs_free_max_blocks
, "Max number of blocks freed in one txg");
1974 module_param(zfs_free_bpobj_enabled
, int, 0644);
1975 MODULE_PARM_DESC(zfs_free_bpobj_enabled
, "Enable processing of the free_bpobj");