4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
25 #include <sys/dsl_scan.h>
26 #include <sys/dsl_pool.h>
27 #include <sys/dsl_dataset.h>
28 #include <sys/dsl_prop.h>
29 #include <sys/dsl_dir.h>
30 #include <sys/dsl_synctask.h>
31 #include <sys/dnode.h>
32 #include <sys/dmu_tx.h>
33 #include <sys/dmu_objset.h>
37 #include <sys/zfs_context.h>
38 #include <sys/fs/zfs.h>
39 #include <sys/zfs_znode.h>
40 #include <sys/spa_impl.h>
41 #include <sys/vdev_impl.h>
42 #include <sys/zil_impl.h>
43 #include <sys/zio_checksum.h>
46 #include <sys/sa_impl.h>
48 #include <sys/zfs_vfsops.h>
51 typedef int (scan_cb_t
)(dsl_pool_t
*, const blkptr_t
*, const zbookmark_t
*);
53 static scan_cb_t dsl_scan_scrub_cb
;
54 static dsl_syncfunc_t dsl_scan_cancel_sync
;
55 static void dsl_scan_sync_state(dsl_scan_t
*, dmu_tx_t
*tx
);
57 int zfs_top_maxinflight
= 32; /* maximum I/Os per top-level */
58 int zfs_resilver_delay
= 2; /* number of ticks to delay resilver */
59 int zfs_scrub_delay
= 4; /* number of ticks to delay scrub */
60 int zfs_scan_idle
= 50; /* idle window in clock ticks */
62 int zfs_scan_min_time_ms
= 1000; /* min millisecs to scrub per txg */
63 int zfs_free_min_time_ms
= 1000; /* min millisecs to free per txg */
64 int zfs_resilver_min_time_ms
= 3000; /* min millisecs to resilver per txg */
65 boolean_t zfs_no_scrub_io
= B_FALSE
; /* set to disable scrub i/o */
66 boolean_t zfs_no_scrub_prefetch
= B_FALSE
; /* set to disable srub prefetching */
67 enum ddt_class zfs_scrub_ddt_class_max
= DDT_CLASS_DUPLICATE
;
68 int dsl_scan_delay_completion
= B_FALSE
; /* set to delay scan completion */
70 #define DSL_SCAN_IS_SCRUB_RESILVER(scn) \
71 ((scn)->scn_phys.scn_func == POOL_SCAN_SCRUB || \
72 (scn)->scn_phys.scn_func == POOL_SCAN_RESILVER)
74 extern int zfs_txg_timeout
;
76 /* the order has to match pool_scan_type */
77 static scan_cb_t
*scan_funcs
[POOL_SCAN_FUNCS
] = {
79 dsl_scan_scrub_cb
, /* POOL_SCAN_SCRUB */
80 dsl_scan_scrub_cb
, /* POOL_SCAN_RESILVER */
84 dsl_scan_init(dsl_pool_t
*dp
, uint64_t txg
)
88 spa_t
*spa
= dp
->dp_spa
;
91 scn
= dp
->dp_scan
= kmem_zalloc(sizeof (dsl_scan_t
), KM_SLEEP
);
94 err
= zap_lookup(dp
->dp_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
95 "scrub_func", sizeof (uint64_t), 1, &f
);
98 * There was an old-style scrub in progress. Restart a
99 * new-style scrub from the beginning.
101 scn
->scn_restart_txg
= txg
;
102 zfs_dbgmsg("old-style scrub was in progress; "
103 "restarting new-style scrub in txg %llu",
104 scn
->scn_restart_txg
);
107 * Load the queue obj from the old location so that it
108 * can be freed by dsl_scan_done().
110 (void) zap_lookup(dp
->dp_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
111 "scrub_queue", sizeof (uint64_t), 1,
112 &scn
->scn_phys
.scn_queue_obj
);
114 err
= zap_lookup(dp
->dp_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
115 DMU_POOL_SCAN
, sizeof (uint64_t), SCAN_PHYS_NUMINTS
,
122 if (scn
->scn_phys
.scn_state
== DSS_SCANNING
&&
123 spa_prev_software_version(dp
->dp_spa
) < SPA_VERSION_SCAN
) {
125 * A new-type scrub was in progress on an old
126 * pool, and the pool was accessed by old
127 * software. Restart from the beginning, since
128 * the old software may have changed the pool in
131 scn
->scn_restart_txg
= txg
;
132 zfs_dbgmsg("new-style scrub was modified "
133 "by old software; restarting in txg %llu",
134 scn
->scn_restart_txg
);
138 spa_scan_stat_init(spa
);
143 dsl_scan_fini(dsl_pool_t
*dp
)
146 kmem_free(dp
->dp_scan
, sizeof (dsl_scan_t
));
153 dsl_scan_setup_check(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
155 dsl_scan_t
*scn
= arg1
;
157 if (scn
->scn_phys
.scn_state
== DSS_SCANNING
)
165 dsl_scan_setup_sync(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
167 dsl_scan_t
*scn
= arg1
;
168 pool_scan_func_t
*funcp
= arg2
;
169 dmu_object_type_t ot
= 0;
170 dsl_pool_t
*dp
= scn
->scn_dp
;
171 spa_t
*spa
= dp
->dp_spa
;
173 ASSERT(scn
->scn_phys
.scn_state
!= DSS_SCANNING
);
174 ASSERT(*funcp
> POOL_SCAN_NONE
&& *funcp
< POOL_SCAN_FUNCS
);
175 bzero(&scn
->scn_phys
, sizeof (scn
->scn_phys
));
176 scn
->scn_phys
.scn_func
= *funcp
;
177 scn
->scn_phys
.scn_state
= DSS_SCANNING
;
178 scn
->scn_phys
.scn_min_txg
= 0;
179 scn
->scn_phys
.scn_max_txg
= tx
->tx_txg
;
180 scn
->scn_phys
.scn_ddt_class_max
= DDT_CLASSES
- 1; /* the entire DDT */
181 scn
->scn_phys
.scn_start_time
= gethrestime_sec();
182 scn
->scn_phys
.scn_errors
= 0;
183 scn
->scn_phys
.scn_to_examine
= spa
->spa_root_vdev
->vdev_stat
.vs_alloc
;
184 scn
->scn_restart_txg
= 0;
185 spa_scan_stat_init(spa
);
187 if (DSL_SCAN_IS_SCRUB_RESILVER(scn
)) {
188 scn
->scn_phys
.scn_ddt_class_max
= zfs_scrub_ddt_class_max
;
190 /* rewrite all disk labels */
191 vdev_config_dirty(spa
->spa_root_vdev
);
193 if (vdev_resilver_needed(spa
->spa_root_vdev
,
194 &scn
->scn_phys
.scn_min_txg
, &scn
->scn_phys
.scn_max_txg
)) {
195 spa_event_notify(spa
, NULL
, FM_EREPORT_ZFS_RESILVER_START
);
197 spa_event_notify(spa
, NULL
, FM_EREPORT_ZFS_SCRUB_START
);
200 spa
->spa_scrub_started
= B_TRUE
;
202 * If this is an incremental scrub, limit the DDT scrub phase
203 * to just the auto-ditto class (for correctness); the rest
204 * of the scrub should go faster using top-down pruning.
206 if (scn
->scn_phys
.scn_min_txg
> TXG_INITIAL
)
207 scn
->scn_phys
.scn_ddt_class_max
= DDT_CLASS_DITTO
;
211 /* back to the generic stuff */
213 if (dp
->dp_blkstats
== NULL
) {
214 dp
->dp_blkstats
= kmem_alloc(sizeof (zfs_all_blkstats_t
),
215 KM_SLEEP
| KM_NODEBUG
);
217 bzero(dp
->dp_blkstats
, sizeof (zfs_all_blkstats_t
));
219 if (spa_version(spa
) < SPA_VERSION_DSL_SCRUB
)
220 ot
= DMU_OT_ZAP_OTHER
;
222 scn
->scn_phys
.scn_queue_obj
= zap_create(dp
->dp_meta_objset
,
223 ot
? ot
: DMU_OT_SCAN_QUEUE
, DMU_OT_NONE
, 0, tx
);
225 dsl_scan_sync_state(scn
, tx
);
227 spa_history_log_internal(LOG_POOL_SCAN
, spa
, tx
,
228 "func=%u mintxg=%llu maxtxg=%llu",
229 *funcp
, scn
->scn_phys
.scn_min_txg
, scn
->scn_phys
.scn_max_txg
);
234 dsl_scan_done(dsl_scan_t
*scn
, boolean_t complete
, dmu_tx_t
*tx
)
236 static const char *old_names
[] = {
238 "scrub_ddt_bookmark",
239 "scrub_ddt_class_max",
248 dsl_pool_t
*dp
= scn
->scn_dp
;
249 spa_t
*spa
= dp
->dp_spa
;
252 /* Remove any remnants of an old-style scrub. */
253 for (i
= 0; old_names
[i
]; i
++) {
254 (void) zap_remove(dp
->dp_meta_objset
,
255 DMU_POOL_DIRECTORY_OBJECT
, old_names
[i
], tx
);
258 if (scn
->scn_phys
.scn_queue_obj
!= 0) {
259 VERIFY(0 == dmu_object_free(dp
->dp_meta_objset
,
260 scn
->scn_phys
.scn_queue_obj
, tx
));
261 scn
->scn_phys
.scn_queue_obj
= 0;
265 * If we were "restarted" from a stopped state, don't bother
266 * with anything else.
268 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
272 scn
->scn_phys
.scn_state
= DSS_FINISHED
;
274 scn
->scn_phys
.scn_state
= DSS_CANCELED
;
276 spa_history_log_internal(LOG_POOL_SCAN_DONE
, spa
, tx
,
277 "complete=%u", complete
);
279 if (DSL_SCAN_IS_SCRUB_RESILVER(scn
)) {
280 mutex_enter(&spa
->spa_scrub_lock
);
281 while (spa
->spa_scrub_inflight
> 0) {
282 cv_wait(&spa
->spa_scrub_io_cv
,
283 &spa
->spa_scrub_lock
);
285 mutex_exit(&spa
->spa_scrub_lock
);
286 spa
->spa_scrub_started
= B_FALSE
;
287 spa
->spa_scrub_active
= B_FALSE
;
290 * If the scrub/resilver completed, update all DTLs to
291 * reflect this. Whether it succeeded or not, vacate
292 * all temporary scrub DTLs.
294 vdev_dtl_reassess(spa
->spa_root_vdev
, tx
->tx_txg
,
295 complete
? scn
->scn_phys
.scn_max_txg
: 0, B_TRUE
);
297 spa_event_notify(spa
, NULL
, scn
->scn_phys
.scn_min_txg
?
298 FM_EREPORT_ZFS_RESILVER_FINISH
:
299 FM_EREPORT_ZFS_SCRUB_FINISH
);
301 spa_errlog_rotate(spa
);
304 * We may have finished replacing a device.
305 * Let the async thread assess this and handle the detach.
307 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
310 scn
->scn_phys
.scn_end_time
= gethrestime_sec();
315 dsl_scan_cancel_check(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
317 dsl_scan_t
*scn
= arg1
;
319 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
326 dsl_scan_cancel_sync(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
328 dsl_scan_t
*scn
= arg1
;
330 dsl_scan_done(scn
, B_FALSE
, tx
);
331 dsl_scan_sync_state(scn
, tx
);
335 dsl_scan_cancel(dsl_pool_t
*dp
)
337 boolean_t complete
= B_FALSE
;
340 err
= dsl_sync_task_do(dp
, dsl_scan_cancel_check
,
341 dsl_scan_cancel_sync
, dp
->dp_scan
, &complete
, 3);
345 static void dsl_scan_visitbp(blkptr_t
*bp
,
346 const zbookmark_t
*zb
, dnode_phys_t
*dnp
, arc_buf_t
*pbuf
,
347 dsl_dataset_t
*ds
, dsl_scan_t
*scn
, dmu_objset_type_t ostype
,
349 static void dsl_scan_visitdnode(dsl_scan_t
*, dsl_dataset_t
*ds
,
350 dmu_objset_type_t ostype
,
351 dnode_phys_t
*dnp
, arc_buf_t
*buf
, uint64_t object
, dmu_tx_t
*tx
);
354 dsl_free(dsl_pool_t
*dp
, uint64_t txg
, const blkptr_t
*bp
)
356 zio_free(dp
->dp_spa
, txg
, bp
);
360 dsl_free_sync(zio_t
*pio
, dsl_pool_t
*dp
, uint64_t txg
, const blkptr_t
*bpp
)
362 ASSERT(dsl_pool_sync_context(dp
));
363 zio_nowait(zio_free_sync(pio
, dp
->dp_spa
, txg
, bpp
, pio
->io_flags
));
367 dsl_read(zio_t
*pio
, spa_t
*spa
, const blkptr_t
*bpp
, arc_buf_t
*pbuf
,
368 arc_done_func_t
*done
, void *private, int priority
, int zio_flags
,
369 uint32_t *arc_flags
, const zbookmark_t
*zb
)
371 return (arc_read(pio
, spa
, bpp
, pbuf
, done
, private,
372 priority
, zio_flags
, arc_flags
, zb
));
376 dsl_read_nolock(zio_t
*pio
, spa_t
*spa
, const blkptr_t
*bpp
,
377 arc_done_func_t
*done
, void *private, int priority
, int zio_flags
,
378 uint32_t *arc_flags
, const zbookmark_t
*zb
)
380 return (arc_read_nolock(pio
, spa
, bpp
, done
, private,
381 priority
, zio_flags
, arc_flags
, zb
));
385 bookmark_is_zero(const zbookmark_t
*zb
)
387 return (zb
->zb_objset
== 0 && zb
->zb_object
== 0 &&
388 zb
->zb_level
== 0 && zb
->zb_blkid
== 0);
391 /* dnp is the dnode for zb1->zb_object */
393 bookmark_is_before(const dnode_phys_t
*dnp
, const zbookmark_t
*zb1
,
394 const zbookmark_t
*zb2
)
396 uint64_t zb1nextL0
, zb2thisobj
;
398 ASSERT(zb1
->zb_objset
== zb2
->zb_objset
);
399 ASSERT(zb2
->zb_level
== 0);
402 * A bookmark in the deadlist is considered to be after
405 if (zb2
->zb_object
== DMU_DEADLIST_OBJECT
)
408 /* The objset_phys_t isn't before anything. */
412 zb1nextL0
= (zb1
->zb_blkid
+ 1) <<
413 ((zb1
->zb_level
) * (dnp
->dn_indblkshift
- SPA_BLKPTRSHIFT
));
415 zb2thisobj
= zb2
->zb_object
? zb2
->zb_object
:
416 zb2
->zb_blkid
<< (DNODE_BLOCK_SHIFT
- DNODE_SHIFT
);
418 if (zb1
->zb_object
== DMU_META_DNODE_OBJECT
) {
419 uint64_t nextobj
= zb1nextL0
*
420 (dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
) >> DNODE_SHIFT
;
421 return (nextobj
<= zb2thisobj
);
424 if (zb1
->zb_object
< zb2thisobj
)
426 if (zb1
->zb_object
> zb2thisobj
)
428 if (zb2
->zb_object
== DMU_META_DNODE_OBJECT
)
430 return (zb1nextL0
<= zb2
->zb_blkid
);
434 dsl_scan_ds_maxtxg(dsl_dataset_t
*ds
)
436 uint64_t smt
= ds
->ds_dir
->dd_pool
->dp_scan
->scn_phys
.scn_max_txg
;
437 if (dsl_dataset_is_snapshot(ds
))
438 return (MIN(smt
, ds
->ds_phys
->ds_creation_txg
));
443 dsl_scan_sync_state(dsl_scan_t
*scn
, dmu_tx_t
*tx
)
445 VERIFY(0 == zap_update(scn
->scn_dp
->dp_meta_objset
,
446 DMU_POOL_DIRECTORY_OBJECT
,
447 DMU_POOL_SCAN
, sizeof (uint64_t), SCAN_PHYS_NUMINTS
,
448 &scn
->scn_phys
, tx
));
452 dsl_scan_check_pause(dsl_scan_t
*scn
, const zbookmark_t
*zb
)
454 uint64_t elapsed_nanosecs
;
457 /* we never skip user/group accounting objects */
458 if (zb
&& (int64_t)zb
->zb_object
< 0)
461 if (scn
->scn_pausing
)
462 return (B_TRUE
); /* we're already pausing */
464 if (!bookmark_is_zero(&scn
->scn_phys
.scn_bookmark
))
465 return (B_FALSE
); /* we're resuming */
467 /* We only know how to resume from level-0 blocks. */
468 if (zb
&& zb
->zb_level
!= 0)
471 mintime
= (scn
->scn_phys
.scn_func
== POOL_SCAN_RESILVER
) ?
472 zfs_resilver_min_time_ms
: zfs_scan_min_time_ms
;
473 elapsed_nanosecs
= gethrtime() - scn
->scn_sync_start_time
;
474 if (elapsed_nanosecs
/ NANOSEC
> zfs_txg_timeout
||
475 (elapsed_nanosecs
/ MICROSEC
> mintime
&&
476 txg_sync_waiting(scn
->scn_dp
)) ||
477 spa_shutting_down(scn
->scn_dp
->dp_spa
)) {
479 dprintf("pausing at bookmark %llx/%llx/%llx/%llx\n",
480 (longlong_t
)zb
->zb_objset
,
481 (longlong_t
)zb
->zb_object
,
482 (longlong_t
)zb
->zb_level
,
483 (longlong_t
)zb
->zb_blkid
);
484 scn
->scn_phys
.scn_bookmark
= *zb
;
486 dprintf("pausing at DDT bookmark %llx/%llx/%llx/%llx\n",
487 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_class
,
488 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_type
,
489 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_checksum
,
490 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_cursor
);
491 scn
->scn_pausing
= B_TRUE
;
497 typedef struct zil_scan_arg
{
499 zil_header_t
*zsa_zh
;
504 dsl_scan_zil_block(zilog_t
*zilog
, blkptr_t
*bp
, void *arg
, uint64_t claim_txg
)
506 zil_scan_arg_t
*zsa
= arg
;
507 dsl_pool_t
*dp
= zsa
->zsa_dp
;
508 dsl_scan_t
*scn
= dp
->dp_scan
;
509 zil_header_t
*zh
= zsa
->zsa_zh
;
512 if (bp
->blk_birth
<= scn
->scn_phys
.scn_cur_min_txg
)
516 * One block ("stubby") can be allocated a long time ago; we
517 * want to visit that one because it has been allocated
518 * (on-disk) even if it hasn't been claimed (even though for
519 * scrub there's nothing to do to it).
521 if (claim_txg
== 0 && bp
->blk_birth
>= spa_first_txg(dp
->dp_spa
))
524 SET_BOOKMARK(&zb
, zh
->zh_log
.blk_cksum
.zc_word
[ZIL_ZC_OBJSET
],
525 ZB_ZIL_OBJECT
, ZB_ZIL_LEVEL
, bp
->blk_cksum
.zc_word
[ZIL_ZC_SEQ
]);
527 VERIFY(0 == scan_funcs
[scn
->scn_phys
.scn_func
](dp
, bp
, &zb
));
533 dsl_scan_zil_record(zilog_t
*zilog
, lr_t
*lrc
, void *arg
, uint64_t claim_txg
)
535 if (lrc
->lrc_txtype
== TX_WRITE
) {
536 zil_scan_arg_t
*zsa
= arg
;
537 dsl_pool_t
*dp
= zsa
->zsa_dp
;
538 dsl_scan_t
*scn
= dp
->dp_scan
;
539 zil_header_t
*zh
= zsa
->zsa_zh
;
540 lr_write_t
*lr
= (lr_write_t
*)lrc
;
541 blkptr_t
*bp
= &lr
->lr_blkptr
;
544 if (bp
->blk_birth
<= scn
->scn_phys
.scn_cur_min_txg
)
548 * birth can be < claim_txg if this record's txg is
549 * already txg sync'ed (but this log block contains
550 * other records that are not synced)
552 if (claim_txg
== 0 || bp
->blk_birth
< claim_txg
)
555 SET_BOOKMARK(&zb
, zh
->zh_log
.blk_cksum
.zc_word
[ZIL_ZC_OBJSET
],
556 lr
->lr_foid
, ZB_ZIL_LEVEL
,
557 lr
->lr_offset
/ BP_GET_LSIZE(bp
));
559 VERIFY(0 == scan_funcs
[scn
->scn_phys
.scn_func
](dp
, bp
, &zb
));
565 dsl_scan_zil(dsl_pool_t
*dp
, zil_header_t
*zh
)
567 uint64_t claim_txg
= zh
->zh_claim_txg
;
568 zil_scan_arg_t zsa
= { dp
, zh
};
572 * We only want to visit blocks that have been claimed but not yet
573 * replayed (or, in read-only mode, blocks that *would* be claimed).
575 if (claim_txg
== 0 && spa_writeable(dp
->dp_spa
))
578 zilog
= zil_alloc(dp
->dp_meta_objset
, zh
);
580 (void) zil_parse(zilog
, dsl_scan_zil_block
, dsl_scan_zil_record
, &zsa
,
588 dsl_scan_prefetch(dsl_scan_t
*scn
, arc_buf_t
*buf
, blkptr_t
*bp
,
589 uint64_t objset
, uint64_t object
, uint64_t blkid
)
592 uint32_t flags
= ARC_NOWAIT
| ARC_PREFETCH
;
594 if (zfs_no_scrub_prefetch
)
597 if (BP_IS_HOLE(bp
) || bp
->blk_birth
<= scn
->scn_phys
.scn_min_txg
||
598 (BP_GET_LEVEL(bp
) == 0 && BP_GET_TYPE(bp
) != DMU_OT_DNODE
))
601 SET_BOOKMARK(&czb
, objset
, object
, BP_GET_LEVEL(bp
), blkid
);
604 * XXX need to make sure all of these arc_read() prefetches are
605 * done before setting xlateall (similar to dsl_read())
607 (void) arc_read(scn
->scn_zio_root
, scn
->scn_dp
->dp_spa
, bp
,
608 buf
, NULL
, NULL
, ZIO_PRIORITY_ASYNC_READ
,
609 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SCAN_THREAD
, &flags
, &czb
);
613 dsl_scan_check_resume(dsl_scan_t
*scn
, const dnode_phys_t
*dnp
,
614 const zbookmark_t
*zb
)
617 * We never skip over user/group accounting objects (obj<0)
619 if (!bookmark_is_zero(&scn
->scn_phys
.scn_bookmark
) &&
620 (int64_t)zb
->zb_object
>= 0) {
622 * If we already visited this bp & everything below (in
623 * a prior txg sync), don't bother doing it again.
625 if (bookmark_is_before(dnp
, zb
, &scn
->scn_phys
.scn_bookmark
))
629 * If we found the block we're trying to resume from, or
630 * we went past it to a different object, zero it out to
631 * indicate that it's OK to start checking for pausing
634 if (bcmp(zb
, &scn
->scn_phys
.scn_bookmark
, sizeof (*zb
)) == 0 ||
635 zb
->zb_object
> scn
->scn_phys
.scn_bookmark
.zb_object
) {
636 dprintf("resuming at %llx/%llx/%llx/%llx\n",
637 (longlong_t
)zb
->zb_objset
,
638 (longlong_t
)zb
->zb_object
,
639 (longlong_t
)zb
->zb_level
,
640 (longlong_t
)zb
->zb_blkid
);
641 bzero(&scn
->scn_phys
.scn_bookmark
, sizeof (*zb
));
648 * Return nonzero on i/o error.
649 * Return new buf to write out in *bufp.
651 __attribute__((always_inline
)) static int
652 dsl_scan_recurse(dsl_scan_t
*scn
, dsl_dataset_t
*ds
, dmu_objset_type_t ostype
,
653 dnode_phys_t
*dnp
, const blkptr_t
*bp
,
654 const zbookmark_t
*zb
, dmu_tx_t
*tx
, arc_buf_t
**bufp
)
656 dsl_pool_t
*dp
= scn
->scn_dp
;
657 int zio_flags
= ZIO_FLAG_CANFAIL
| ZIO_FLAG_SCAN_THREAD
;
660 if (BP_GET_LEVEL(bp
) > 0) {
661 uint32_t flags
= ARC_WAIT
;
664 int epb
= BP_GET_LSIZE(bp
) >> SPA_BLKPTRSHIFT
;
666 err
= arc_read_nolock(NULL
, dp
->dp_spa
, bp
,
667 arc_getbuf_func
, bufp
,
668 ZIO_PRIORITY_ASYNC_READ
, zio_flags
, &flags
, zb
);
670 scn
->scn_phys
.scn_errors
++;
673 for (i
= 0, cbp
= (*bufp
)->b_data
; i
< epb
; i
++, cbp
++) {
674 dsl_scan_prefetch(scn
, *bufp
, cbp
, zb
->zb_objset
,
675 zb
->zb_object
, zb
->zb_blkid
* epb
+ i
);
677 for (i
= 0, cbp
= (*bufp
)->b_data
; i
< epb
; i
++, cbp
++) {
680 SET_BOOKMARK(&czb
, zb
->zb_objset
, zb
->zb_object
,
682 zb
->zb_blkid
* epb
+ i
);
683 dsl_scan_visitbp(cbp
, &czb
, dnp
,
684 *bufp
, ds
, scn
, ostype
, tx
);
686 } else if (BP_GET_TYPE(bp
) == DMU_OT_USERGROUP_USED
) {
687 uint32_t flags
= ARC_WAIT
;
689 err
= arc_read_nolock(NULL
, dp
->dp_spa
, bp
,
690 arc_getbuf_func
, bufp
,
691 ZIO_PRIORITY_ASYNC_READ
, zio_flags
, &flags
, zb
);
693 scn
->scn_phys
.scn_errors
++;
696 } else if (BP_GET_TYPE(bp
) == DMU_OT_DNODE
) {
697 uint32_t flags
= ARC_WAIT
;
700 int epb
= BP_GET_LSIZE(bp
) >> DNODE_SHIFT
;
702 err
= arc_read_nolock(NULL
, dp
->dp_spa
, bp
,
703 arc_getbuf_func
, bufp
,
704 ZIO_PRIORITY_ASYNC_READ
, zio_flags
, &flags
, zb
);
706 scn
->scn_phys
.scn_errors
++;
709 for (i
= 0, cdnp
= (*bufp
)->b_data
; i
< epb
; i
++, cdnp
++) {
710 for (j
= 0; j
< cdnp
->dn_nblkptr
; j
++) {
711 blkptr_t
*cbp
= &cdnp
->dn_blkptr
[j
];
712 dsl_scan_prefetch(scn
, *bufp
, cbp
,
713 zb
->zb_objset
, zb
->zb_blkid
* epb
+ i
, j
);
716 for (i
= 0, cdnp
= (*bufp
)->b_data
; i
< epb
; i
++, cdnp
++) {
717 dsl_scan_visitdnode(scn
, ds
, ostype
,
718 cdnp
, *bufp
, zb
->zb_blkid
* epb
+ i
, tx
);
721 } else if (BP_GET_TYPE(bp
) == DMU_OT_OBJSET
) {
722 uint32_t flags
= ARC_WAIT
;
725 err
= arc_read_nolock(NULL
, dp
->dp_spa
, bp
,
726 arc_getbuf_func
, bufp
,
727 ZIO_PRIORITY_ASYNC_READ
, zio_flags
, &flags
, zb
);
729 scn
->scn_phys
.scn_errors
++;
733 osp
= (*bufp
)->b_data
;
735 dsl_scan_visitdnode(scn
, ds
, osp
->os_type
,
736 &osp
->os_meta_dnode
, *bufp
, DMU_META_DNODE_OBJECT
, tx
);
738 if (OBJSET_BUF_HAS_USERUSED(*bufp
)) {
740 * We also always visit user/group accounting
741 * objects, and never skip them, even if we are
742 * pausing. This is necessary so that the space
743 * deltas from this txg get integrated.
745 dsl_scan_visitdnode(scn
, ds
, osp
->os_type
,
746 &osp
->os_groupused_dnode
, *bufp
,
747 DMU_GROUPUSED_OBJECT
, tx
);
748 dsl_scan_visitdnode(scn
, ds
, osp
->os_type
,
749 &osp
->os_userused_dnode
, *bufp
,
750 DMU_USERUSED_OBJECT
, tx
);
757 __attribute__((always_inline
)) static void
758 dsl_scan_visitdnode(dsl_scan_t
*scn
, dsl_dataset_t
*ds
,
759 dmu_objset_type_t ostype
, dnode_phys_t
*dnp
, arc_buf_t
*buf
,
760 uint64_t object
, dmu_tx_t
*tx
)
764 for (j
= 0; j
< dnp
->dn_nblkptr
; j
++) {
767 SET_BOOKMARK(&czb
, ds
? ds
->ds_object
: 0, object
,
768 dnp
->dn_nlevels
- 1, j
);
769 dsl_scan_visitbp(&dnp
->dn_blkptr
[j
],
770 &czb
, dnp
, buf
, ds
, scn
, ostype
, tx
);
773 if (dnp
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
) {
775 SET_BOOKMARK(&czb
, ds
? ds
->ds_object
: 0, object
,
777 dsl_scan_visitbp(&dnp
->dn_spill
,
778 &czb
, dnp
, buf
, ds
, scn
, ostype
, tx
);
783 * The arguments are in this order because mdb can only print the
784 * first 5; we want them to be useful.
787 dsl_scan_visitbp(blkptr_t
*bp
, const zbookmark_t
*zb
,
788 dnode_phys_t
*dnp
, arc_buf_t
*pbuf
,
789 dsl_dataset_t
*ds
, dsl_scan_t
*scn
, dmu_objset_type_t ostype
,
792 dsl_pool_t
*dp
= scn
->scn_dp
;
793 arc_buf_t
*buf
= NULL
;
796 bp_toread
= kmem_alloc(sizeof (blkptr_t
), KM_SLEEP
);
799 /* ASSERT(pbuf == NULL || arc_released(pbuf)); */
801 if (dsl_scan_check_pause(scn
, zb
))
804 if (dsl_scan_check_resume(scn
, dnp
, zb
))
807 if (bp
->blk_birth
== 0)
810 scn
->scn_visited_this_txg
++;
813 "visiting ds=%p/%llu zb=%llx/%llx/%llx/%llx buf=%p bp=%p",
814 ds
, ds
? ds
->ds_object
: 0,
815 zb
->zb_objset
, zb
->zb_object
, zb
->zb_level
, zb
->zb_blkid
,
818 if (bp
->blk_birth
<= scn
->scn_phys
.scn_cur_min_txg
)
821 if (BP_GET_TYPE(bp
) != DMU_OT_USERGROUP_USED
) {
823 * For non-user-accounting blocks, we need to read the
824 * new bp (from a deleted snapshot, found in
825 * check_existing_xlation). If we used the old bp,
826 * pointers inside this block from before we resumed
827 * would be untranslated.
829 * For user-accounting blocks, we need to read the old
830 * bp, because we will apply the entire space delta to
831 * it (original untranslated -> translations from
832 * deleted snap -> now).
837 if (dsl_scan_recurse(scn
, ds
, ostype
, dnp
, bp_toread
, zb
, tx
,
842 * If dsl_scan_ddt() has aready visited this block, it will have
843 * already done any translations or scrubbing, so don't call the
846 if (ddt_class_contains(dp
->dp_spa
,
847 scn
->scn_phys
.scn_ddt_class_max
, bp
)) {
853 * If this block is from the future (after cur_max_txg), then we
854 * are doing this on behalf of a deleted snapshot, and we will
855 * revisit the future block on the next pass of this dataset.
856 * Don't scan it now unless we need to because something
857 * under it was modified.
859 if (bp
->blk_birth
<= scn
->scn_phys
.scn_cur_max_txg
) {
860 scan_funcs
[scn
->scn_phys
.scn_func
](dp
, bp
, zb
);
863 (void) arc_buf_remove_ref(buf
, &buf
);
865 kmem_free(bp_toread
, sizeof(blkptr_t
));
869 dsl_scan_visit_rootbp(dsl_scan_t
*scn
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
874 SET_BOOKMARK(&zb
, ds
? ds
->ds_object
: DMU_META_OBJSET
,
875 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
876 dsl_scan_visitbp(bp
, &zb
, NULL
, NULL
,
877 ds
, scn
, DMU_OST_NONE
, tx
);
879 dprintf_ds(ds
, "finished scan%s", "");
883 dsl_scan_ds_destroyed(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
885 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
886 dsl_scan_t
*scn
= dp
->dp_scan
;
889 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
892 if (scn
->scn_phys
.scn_bookmark
.zb_objset
== ds
->ds_object
) {
893 if (dsl_dataset_is_snapshot(ds
)) {
894 /* Note, scn_cur_{min,max}_txg stays the same. */
895 scn
->scn_phys
.scn_bookmark
.zb_objset
=
896 ds
->ds_phys
->ds_next_snap_obj
;
897 zfs_dbgmsg("destroying ds %llu; currently traversing; "
898 "reset zb_objset to %llu",
899 (u_longlong_t
)ds
->ds_object
,
900 (u_longlong_t
)ds
->ds_phys
->ds_next_snap_obj
);
901 scn
->scn_phys
.scn_flags
|= DSF_VISIT_DS_AGAIN
;
903 SET_BOOKMARK(&scn
->scn_phys
.scn_bookmark
,
904 ZB_DESTROYED_OBJSET
, 0, 0, 0);
905 zfs_dbgmsg("destroying ds %llu; currently traversing; "
906 "reset bookmark to -1,0,0,0",
907 (u_longlong_t
)ds
->ds_object
);
909 } else if (zap_lookup_int_key(dp
->dp_meta_objset
,
910 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
, &mintxg
) == 0) {
911 ASSERT3U(ds
->ds_phys
->ds_num_children
, <=, 1);
912 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
913 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
, tx
));
914 if (dsl_dataset_is_snapshot(ds
)) {
916 * We keep the same mintxg; it could be >
917 * ds_creation_txg if the previous snapshot was
920 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
921 scn
->scn_phys
.scn_queue_obj
,
922 ds
->ds_phys
->ds_next_snap_obj
, mintxg
, tx
) == 0);
923 zfs_dbgmsg("destroying ds %llu; in queue; "
924 "replacing with %llu",
925 (u_longlong_t
)ds
->ds_object
,
926 (u_longlong_t
)ds
->ds_phys
->ds_next_snap_obj
);
928 zfs_dbgmsg("destroying ds %llu; in queue; removing",
929 (u_longlong_t
)ds
->ds_object
);
932 zfs_dbgmsg("destroying ds %llu; ignoring",
933 (u_longlong_t
)ds
->ds_object
);
937 * dsl_scan_sync() should be called after this, and should sync
938 * out our changed state, but just to be safe, do it here.
940 dsl_scan_sync_state(scn
, tx
);
944 dsl_scan_ds_snapshotted(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
946 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
947 dsl_scan_t
*scn
= dp
->dp_scan
;
950 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
953 ASSERT(ds
->ds_phys
->ds_prev_snap_obj
!= 0);
955 if (scn
->scn_phys
.scn_bookmark
.zb_objset
== ds
->ds_object
) {
956 scn
->scn_phys
.scn_bookmark
.zb_objset
=
957 ds
->ds_phys
->ds_prev_snap_obj
;
958 zfs_dbgmsg("snapshotting ds %llu; currently traversing; "
959 "reset zb_objset to %llu",
960 (u_longlong_t
)ds
->ds_object
,
961 (u_longlong_t
)ds
->ds_phys
->ds_prev_snap_obj
);
962 } else if (zap_lookup_int_key(dp
->dp_meta_objset
,
963 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
, &mintxg
) == 0) {
964 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
965 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
, tx
));
966 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
967 scn
->scn_phys
.scn_queue_obj
,
968 ds
->ds_phys
->ds_prev_snap_obj
, mintxg
, tx
) == 0);
969 zfs_dbgmsg("snapshotting ds %llu; in queue; "
970 "replacing with %llu",
971 (u_longlong_t
)ds
->ds_object
,
972 (u_longlong_t
)ds
->ds_phys
->ds_prev_snap_obj
);
974 dsl_scan_sync_state(scn
, tx
);
978 dsl_scan_ds_clone_swapped(dsl_dataset_t
*ds1
, dsl_dataset_t
*ds2
, dmu_tx_t
*tx
)
980 dsl_pool_t
*dp
= ds1
->ds_dir
->dd_pool
;
981 dsl_scan_t
*scn
= dp
->dp_scan
;
984 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
987 if (scn
->scn_phys
.scn_bookmark
.zb_objset
== ds1
->ds_object
) {
988 scn
->scn_phys
.scn_bookmark
.zb_objset
= ds2
->ds_object
;
989 zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
990 "reset zb_objset to %llu",
991 (u_longlong_t
)ds1
->ds_object
,
992 (u_longlong_t
)ds2
->ds_object
);
993 } else if (scn
->scn_phys
.scn_bookmark
.zb_objset
== ds2
->ds_object
) {
994 scn
->scn_phys
.scn_bookmark
.zb_objset
= ds1
->ds_object
;
995 zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
996 "reset zb_objset to %llu",
997 (u_longlong_t
)ds2
->ds_object
,
998 (u_longlong_t
)ds1
->ds_object
);
1001 if (zap_lookup_int_key(dp
->dp_meta_objset
, scn
->scn_phys
.scn_queue_obj
,
1002 ds1
->ds_object
, &mintxg
) == 0) {
1005 ASSERT3U(mintxg
, ==, ds1
->ds_phys
->ds_prev_snap_txg
);
1006 ASSERT3U(mintxg
, ==, ds2
->ds_phys
->ds_prev_snap_txg
);
1007 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
1008 scn
->scn_phys
.scn_queue_obj
, ds1
->ds_object
, tx
));
1009 err
= zap_add_int_key(dp
->dp_meta_objset
,
1010 scn
->scn_phys
.scn_queue_obj
, ds2
->ds_object
, mintxg
, tx
);
1011 VERIFY(err
== 0 || err
== EEXIST
);
1012 if (err
== EEXIST
) {
1013 /* Both were there to begin with */
1014 VERIFY(0 == zap_add_int_key(dp
->dp_meta_objset
,
1015 scn
->scn_phys
.scn_queue_obj
,
1016 ds1
->ds_object
, mintxg
, tx
));
1018 zfs_dbgmsg("clone_swap ds %llu; in queue; "
1019 "replacing with %llu",
1020 (u_longlong_t
)ds1
->ds_object
,
1021 (u_longlong_t
)ds2
->ds_object
);
1022 } else if (zap_lookup_int_key(dp
->dp_meta_objset
,
1023 scn
->scn_phys
.scn_queue_obj
, ds2
->ds_object
, &mintxg
) == 0) {
1024 ASSERT3U(mintxg
, ==, ds1
->ds_phys
->ds_prev_snap_txg
);
1025 ASSERT3U(mintxg
, ==, ds2
->ds_phys
->ds_prev_snap_txg
);
1026 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
1027 scn
->scn_phys
.scn_queue_obj
, ds2
->ds_object
, tx
));
1028 VERIFY(0 == zap_add_int_key(dp
->dp_meta_objset
,
1029 scn
->scn_phys
.scn_queue_obj
, ds1
->ds_object
, mintxg
, tx
));
1030 zfs_dbgmsg("clone_swap ds %llu; in queue; "
1031 "replacing with %llu",
1032 (u_longlong_t
)ds2
->ds_object
,
1033 (u_longlong_t
)ds1
->ds_object
);
1036 dsl_scan_sync_state(scn
, tx
);
1039 struct enqueue_clones_arg
{
1046 enqueue_clones_cb(spa_t
*spa
, uint64_t dsobj
, const char *dsname
, void *arg
)
1048 struct enqueue_clones_arg
*eca
= arg
;
1051 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
1052 dsl_scan_t
*scn
= dp
->dp_scan
;
1054 err
= dsl_dataset_hold_obj(dp
, dsobj
, FTAG
, &ds
);
1058 if (ds
->ds_dir
->dd_phys
->dd_origin_obj
== eca
->originobj
) {
1059 while (ds
->ds_phys
->ds_prev_snap_obj
!= eca
->originobj
) {
1060 dsl_dataset_t
*prev
;
1061 err
= dsl_dataset_hold_obj(dp
,
1062 ds
->ds_phys
->ds_prev_snap_obj
, FTAG
, &prev
);
1064 dsl_dataset_rele(ds
, FTAG
);
1069 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
1070 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
,
1071 ds
->ds_phys
->ds_prev_snap_txg
, eca
->tx
) == 0);
1073 dsl_dataset_rele(ds
, FTAG
);
1078 dsl_scan_visitds(dsl_scan_t
*scn
, uint64_t dsobj
, dmu_tx_t
*tx
)
1080 dsl_pool_t
*dp
= scn
->scn_dp
;
1085 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp
, dsobj
, FTAG
, &ds
));
1087 if (dmu_objset_from_ds(ds
, &os
))
1091 * Only the ZIL in the head (non-snapshot) is valid. Even though
1092 * snapshots can have ZIL block pointers (which may be the same
1093 * BP as in the head), they must be ignored. So we traverse the
1094 * ZIL here, rather than in scan_recurse(), because the regular
1095 * snapshot block-sharing rules don't apply to it.
1097 if (DSL_SCAN_IS_SCRUB_RESILVER(scn
) && !dsl_dataset_is_snapshot(ds
))
1098 dsl_scan_zil(dp
, &os
->os_zil_header
);
1101 * Iterate over the bps in this ds.
1103 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
1104 dsl_scan_visit_rootbp(scn
, ds
, &ds
->ds_phys
->ds_bp
, tx
);
1106 dsname
= kmem_alloc(ZFS_MAXNAMELEN
, KM_SLEEP
);
1107 dsl_dataset_name(ds
, dsname
);
1108 zfs_dbgmsg("scanned dataset %llu (%s) with min=%llu max=%llu; "
1110 (longlong_t
)dsobj
, dsname
,
1111 (longlong_t
)scn
->scn_phys
.scn_cur_min_txg
,
1112 (longlong_t
)scn
->scn_phys
.scn_cur_max_txg
,
1113 (int)scn
->scn_pausing
);
1114 kmem_free(dsname
, ZFS_MAXNAMELEN
);
1116 if (scn
->scn_pausing
)
1120 * We've finished this pass over this dataset.
1124 * If we did not completely visit this dataset, do another pass.
1126 if (scn
->scn_phys
.scn_flags
& DSF_VISIT_DS_AGAIN
) {
1127 zfs_dbgmsg("incomplete pass; visiting again");
1128 scn
->scn_phys
.scn_flags
&= ~DSF_VISIT_DS_AGAIN
;
1129 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
1130 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
,
1131 scn
->scn_phys
.scn_cur_max_txg
, tx
) == 0);
1136 * Add descendent datasets to work queue.
1138 if (ds
->ds_phys
->ds_next_snap_obj
!= 0) {
1139 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
1140 scn
->scn_phys
.scn_queue_obj
, ds
->ds_phys
->ds_next_snap_obj
,
1141 ds
->ds_phys
->ds_creation_txg
, tx
) == 0);
1143 if (ds
->ds_phys
->ds_num_children
> 1) {
1144 boolean_t usenext
= B_FALSE
;
1145 if (ds
->ds_phys
->ds_next_clones_obj
!= 0) {
1148 * A bug in a previous version of the code could
1149 * cause upgrade_clones_cb() to not set
1150 * ds_next_snap_obj when it should, leading to a
1151 * missing entry. Therefore we can only use the
1152 * next_clones_obj when its count is correct.
1154 int err
= zap_count(dp
->dp_meta_objset
,
1155 ds
->ds_phys
->ds_next_clones_obj
, &count
);
1157 count
== ds
->ds_phys
->ds_num_children
- 1)
1162 VERIFY(zap_join_key(dp
->dp_meta_objset
,
1163 ds
->ds_phys
->ds_next_clones_obj
,
1164 scn
->scn_phys
.scn_queue_obj
,
1165 ds
->ds_phys
->ds_creation_txg
, tx
) == 0);
1167 struct enqueue_clones_arg eca
;
1169 eca
.originobj
= ds
->ds_object
;
1171 (void) dmu_objset_find_spa(ds
->ds_dir
->dd_pool
->dp_spa
,
1172 NULL
, enqueue_clones_cb
, &eca
, DS_FIND_CHILDREN
);
1177 dsl_dataset_rele(ds
, FTAG
);
1182 enqueue_cb(spa_t
*spa
, uint64_t dsobj
, const char *dsname
, void *arg
)
1187 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
1188 dsl_scan_t
*scn
= dp
->dp_scan
;
1190 err
= dsl_dataset_hold_obj(dp
, dsobj
, FTAG
, &ds
);
1194 while (ds
->ds_phys
->ds_prev_snap_obj
!= 0) {
1195 dsl_dataset_t
*prev
;
1196 err
= dsl_dataset_hold_obj(dp
, ds
->ds_phys
->ds_prev_snap_obj
,
1199 dsl_dataset_rele(ds
, FTAG
);
1204 * If this is a clone, we don't need to worry about it for now.
1206 if (prev
->ds_phys
->ds_next_snap_obj
!= ds
->ds_object
) {
1207 dsl_dataset_rele(ds
, FTAG
);
1208 dsl_dataset_rele(prev
, FTAG
);
1211 dsl_dataset_rele(ds
, FTAG
);
1215 VERIFY(zap_add_int_key(dp
->dp_meta_objset
, scn
->scn_phys
.scn_queue_obj
,
1216 ds
->ds_object
, ds
->ds_phys
->ds_prev_snap_txg
, tx
) == 0);
1217 dsl_dataset_rele(ds
, FTAG
);
1222 * Scrub/dedup interaction.
1224 * If there are N references to a deduped block, we don't want to scrub it
1225 * N times -- ideally, we should scrub it exactly once.
1227 * We leverage the fact that the dde's replication class (enum ddt_class)
1228 * is ordered from highest replication class (DDT_CLASS_DITTO) to lowest
1229 * (DDT_CLASS_UNIQUE) so that we may walk the DDT in that order.
1231 * To prevent excess scrubbing, the scrub begins by walking the DDT
1232 * to find all blocks with refcnt > 1, and scrubs each of these once.
1233 * Since there are two replication classes which contain blocks with
1234 * refcnt > 1, we scrub the highest replication class (DDT_CLASS_DITTO) first.
1235 * Finally the top-down scrub begins, only visiting blocks with refcnt == 1.
1237 * There would be nothing more to say if a block's refcnt couldn't change
1238 * during a scrub, but of course it can so we must account for changes
1239 * in a block's replication class.
1241 * Here's an example of what can occur:
1243 * If a block has refcnt > 1 during the DDT scrub phase, but has refcnt == 1
1244 * when visited during the top-down scrub phase, it will be scrubbed twice.
1245 * This negates our scrub optimization, but is otherwise harmless.
1247 * If a block has refcnt == 1 during the DDT scrub phase, but has refcnt > 1
1248 * on each visit during the top-down scrub phase, it will never be scrubbed.
1249 * To catch this, ddt_sync_entry() notifies the scrub code whenever a block's
1250 * reference class transitions to a higher level (i.e DDT_CLASS_UNIQUE to
1251 * DDT_CLASS_DUPLICATE); if it transitions from refcnt == 1 to refcnt > 1
1252 * while a scrub is in progress, it scrubs the block right then.
1255 dsl_scan_ddt(dsl_scan_t
*scn
, dmu_tx_t
*tx
)
1257 ddt_bookmark_t
*ddb
= &scn
->scn_phys
.scn_ddt_bookmark
;
1262 bzero(&dde
, sizeof (ddt_entry_t
));
1264 while ((error
= ddt_walk(scn
->scn_dp
->dp_spa
, ddb
, &dde
)) == 0) {
1267 if (ddb
->ddb_class
> scn
->scn_phys
.scn_ddt_class_max
)
1269 dprintf("visiting ddb=%llu/%llu/%llu/%llx\n",
1270 (longlong_t
)ddb
->ddb_class
,
1271 (longlong_t
)ddb
->ddb_type
,
1272 (longlong_t
)ddb
->ddb_checksum
,
1273 (longlong_t
)ddb
->ddb_cursor
);
1275 /* There should be no pending changes to the dedup table */
1276 ddt
= scn
->scn_dp
->dp_spa
->spa_ddt
[ddb
->ddb_checksum
];
1277 ASSERT(avl_first(&ddt
->ddt_tree
) == NULL
);
1279 dsl_scan_ddt_entry(scn
, ddb
->ddb_checksum
, &dde
, tx
);
1282 if (dsl_scan_check_pause(scn
, NULL
))
1286 zfs_dbgmsg("scanned %llu ddt entries with class_max = %u; pausing=%u",
1287 (longlong_t
)n
, (int)scn
->scn_phys
.scn_ddt_class_max
,
1288 (int)scn
->scn_pausing
);
1290 ASSERT(error
== 0 || error
== ENOENT
);
1291 ASSERT(error
!= ENOENT
||
1292 ddb
->ddb_class
> scn
->scn_phys
.scn_ddt_class_max
);
1297 dsl_scan_ddt_entry(dsl_scan_t
*scn
, enum zio_checksum checksum
,
1298 ddt_entry_t
*dde
, dmu_tx_t
*tx
)
1300 const ddt_key_t
*ddk
= &dde
->dde_key
;
1301 ddt_phys_t
*ddp
= dde
->dde_phys
;
1303 zbookmark_t zb
= { 0 };
1306 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
1309 for (p
= 0; p
< DDT_PHYS_TYPES
; p
++, ddp
++) {
1310 if (ddp
->ddp_phys_birth
== 0 ||
1311 ddp
->ddp_phys_birth
> scn
->scn_phys
.scn_cur_max_txg
)
1313 ddt_bp_create(checksum
, ddk
, ddp
, &bp
);
1315 scn
->scn_visited_this_txg
++;
1316 scan_funcs
[scn
->scn_phys
.scn_func
](scn
->scn_dp
, &bp
, &zb
);
1321 dsl_scan_visit(dsl_scan_t
*scn
, dmu_tx_t
*tx
)
1323 dsl_pool_t
*dp
= scn
->scn_dp
;
1327 if (scn
->scn_phys
.scn_ddt_bookmark
.ddb_class
<=
1328 scn
->scn_phys
.scn_ddt_class_max
) {
1329 scn
->scn_phys
.scn_cur_min_txg
= scn
->scn_phys
.scn_min_txg
;
1330 scn
->scn_phys
.scn_cur_max_txg
= scn
->scn_phys
.scn_max_txg
;
1331 dsl_scan_ddt(scn
, tx
);
1332 if (scn
->scn_pausing
)
1336 if (scn
->scn_phys
.scn_bookmark
.zb_objset
== DMU_META_OBJSET
) {
1337 /* First do the MOS & ORIGIN */
1339 scn
->scn_phys
.scn_cur_min_txg
= scn
->scn_phys
.scn_min_txg
;
1340 scn
->scn_phys
.scn_cur_max_txg
= scn
->scn_phys
.scn_max_txg
;
1341 dsl_scan_visit_rootbp(scn
, NULL
,
1342 &dp
->dp_meta_rootbp
, tx
);
1343 spa_set_rootblkptr(dp
->dp_spa
, &dp
->dp_meta_rootbp
);
1344 if (scn
->scn_pausing
)
1347 if (spa_version(dp
->dp_spa
) < SPA_VERSION_DSL_SCRUB
) {
1348 VERIFY(0 == dmu_objset_find_spa(dp
->dp_spa
,
1349 NULL
, enqueue_cb
, tx
, DS_FIND_CHILDREN
));
1351 dsl_scan_visitds(scn
,
1352 dp
->dp_origin_snap
->ds_object
, tx
);
1354 ASSERT(!scn
->scn_pausing
);
1355 } else if (scn
->scn_phys
.scn_bookmark
.zb_objset
!=
1356 ZB_DESTROYED_OBJSET
) {
1358 * If we were paused, continue from here. Note if the
1359 * ds we were paused on was deleted, the zb_objset may
1360 * be -1, so we will skip this and find a new objset
1363 dsl_scan_visitds(scn
, scn
->scn_phys
.scn_bookmark
.zb_objset
, tx
);
1364 if (scn
->scn_pausing
)
1369 * In case we were paused right at the end of the ds, zero the
1370 * bookmark so we don't think that we're still trying to resume.
1372 bzero(&scn
->scn_phys
.scn_bookmark
, sizeof (zbookmark_t
));
1374 /* keep pulling things out of the zap-object-as-queue */
1375 while (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
1376 scn
->scn_phys
.scn_queue_obj
),
1377 zap_cursor_retrieve(&zc
, &za
) == 0) {
1381 dsobj
= strtonum(za
.za_name
, NULL
);
1382 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
1383 scn
->scn_phys
.scn_queue_obj
, dsobj
, tx
));
1385 /* Set up min/max txg */
1386 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp
, dsobj
, FTAG
, &ds
));
1387 if (za
.za_first_integer
!= 0) {
1388 scn
->scn_phys
.scn_cur_min_txg
=
1389 MAX(scn
->scn_phys
.scn_min_txg
,
1390 za
.za_first_integer
);
1392 scn
->scn_phys
.scn_cur_min_txg
=
1393 MAX(scn
->scn_phys
.scn_min_txg
,
1394 ds
->ds_phys
->ds_prev_snap_txg
);
1396 scn
->scn_phys
.scn_cur_max_txg
= dsl_scan_ds_maxtxg(ds
);
1397 dsl_dataset_rele(ds
, FTAG
);
1399 dsl_scan_visitds(scn
, dsobj
, tx
);
1400 zap_cursor_fini(&zc
);
1401 if (scn
->scn_pausing
)
1404 zap_cursor_fini(&zc
);
1408 dsl_scan_free_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
1410 dsl_scan_t
*scn
= arg
;
1411 uint64_t elapsed_nanosecs
;
1413 elapsed_nanosecs
= gethrtime() - scn
->scn_sync_start_time
;
1415 if (elapsed_nanosecs
/ NANOSEC
> zfs_txg_timeout
||
1416 (elapsed_nanosecs
/ MICROSEC
> zfs_free_min_time_ms
&&
1417 txg_sync_waiting(scn
->scn_dp
)) ||
1418 spa_shutting_down(scn
->scn_dp
->dp_spa
))
1421 zio_nowait(zio_free_sync(scn
->scn_zio_root
, scn
->scn_dp
->dp_spa
,
1422 dmu_tx_get_txg(tx
), bp
, 0));
1423 dsl_dir_diduse_space(tx
->tx_pool
->dp_free_dir
, DD_USED_HEAD
,
1424 -bp_get_dsize_sync(scn
->scn_dp
->dp_spa
, bp
),
1425 -BP_GET_PSIZE(bp
), -BP_GET_UCSIZE(bp
), tx
);
1426 scn
->scn_visited_this_txg
++;
1431 dsl_scan_active(dsl_scan_t
*scn
)
1433 spa_t
*spa
= scn
->scn_dp
->dp_spa
;
1434 uint64_t used
= 0, comp
, uncomp
;
1436 if (spa
->spa_load_state
!= SPA_LOAD_NONE
)
1438 if (spa_shutting_down(spa
))
1441 if (scn
->scn_phys
.scn_state
== DSS_SCANNING
)
1444 if (spa_version(scn
->scn_dp
->dp_spa
) >= SPA_VERSION_DEADLISTS
) {
1445 (void) bpobj_space(&scn
->scn_dp
->dp_free_bpobj
,
1446 &used
, &comp
, &uncomp
);
1452 dsl_scan_sync(dsl_pool_t
*dp
, dmu_tx_t
*tx
)
1454 dsl_scan_t
*scn
= dp
->dp_scan
;
1455 spa_t
*spa
= dp
->dp_spa
;
1459 * Check for scn_restart_txg before checking spa_load_state, so
1460 * that we can restart an old-style scan while the pool is being
1461 * imported (see dsl_scan_init).
1463 if (scn
->scn_restart_txg
!= 0 &&
1464 scn
->scn_restart_txg
<= tx
->tx_txg
) {
1465 pool_scan_func_t func
= POOL_SCAN_SCRUB
;
1466 dsl_scan_done(scn
, B_FALSE
, tx
);
1467 if (vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
))
1468 func
= POOL_SCAN_RESILVER
;
1469 zfs_dbgmsg("restarting scan func=%u txg=%llu",
1471 dsl_scan_setup_sync(scn
, &func
, tx
);
1474 if (!dsl_scan_active(scn
) ||
1475 spa_sync_pass(dp
->dp_spa
) > 1)
1478 scn
->scn_visited_this_txg
= 0;
1479 scn
->scn_pausing
= B_FALSE
;
1480 scn
->scn_sync_start_time
= gethrtime();
1481 spa
->spa_scrub_active
= B_TRUE
;
1484 * First process the free list. If we pause the free, don't do
1485 * any scanning. This ensures that there is no free list when
1486 * we are scanning, so the scan code doesn't have to worry about
1489 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_DEADLISTS
) {
1490 scn
->scn_zio_root
= zio_root(dp
->dp_spa
, NULL
,
1491 NULL
, ZIO_FLAG_MUSTSUCCEED
);
1492 err
= bpobj_iterate(&dp
->dp_free_bpobj
,
1493 dsl_scan_free_cb
, scn
, tx
);
1494 VERIFY3U(0, ==, zio_wait(scn
->scn_zio_root
));
1495 if (scn
->scn_visited_this_txg
) {
1496 zfs_dbgmsg("freed %llu blocks in %llums from "
1497 "free_bpobj txg %llu",
1498 (longlong_t
)scn
->scn_visited_this_txg
,
1500 (gethrtime() - scn
->scn_sync_start_time
) / MICROSEC
,
1501 (longlong_t
)tx
->tx_txg
);
1502 scn
->scn_visited_this_txg
= 0;
1504 * Re-sync the ddt so that we can further modify
1505 * it when doing bprewrite.
1507 ddt_sync(spa
, tx
->tx_txg
);
1509 if (err
== ERESTART
)
1513 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
1516 if (scn
->scn_phys
.scn_ddt_bookmark
.ddb_class
<=
1517 scn
->scn_phys
.scn_ddt_class_max
) {
1518 zfs_dbgmsg("doing scan sync txg %llu; "
1519 "ddt bm=%llu/%llu/%llu/%llx",
1520 (longlong_t
)tx
->tx_txg
,
1521 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_class
,
1522 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_type
,
1523 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_checksum
,
1524 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_cursor
);
1525 ASSERT(scn
->scn_phys
.scn_bookmark
.zb_objset
== 0);
1526 ASSERT(scn
->scn_phys
.scn_bookmark
.zb_object
== 0);
1527 ASSERT(scn
->scn_phys
.scn_bookmark
.zb_level
== 0);
1528 ASSERT(scn
->scn_phys
.scn_bookmark
.zb_blkid
== 0);
1530 zfs_dbgmsg("doing scan sync txg %llu; bm=%llu/%llu/%llu/%llu",
1531 (longlong_t
)tx
->tx_txg
,
1532 (longlong_t
)scn
->scn_phys
.scn_bookmark
.zb_objset
,
1533 (longlong_t
)scn
->scn_phys
.scn_bookmark
.zb_object
,
1534 (longlong_t
)scn
->scn_phys
.scn_bookmark
.zb_level
,
1535 (longlong_t
)scn
->scn_phys
.scn_bookmark
.zb_blkid
);
1538 scn
->scn_zio_root
= zio_root(dp
->dp_spa
, NULL
,
1539 NULL
, ZIO_FLAG_CANFAIL
);
1540 dsl_scan_visit(scn
, tx
);
1541 (void) zio_wait(scn
->scn_zio_root
);
1542 scn
->scn_zio_root
= NULL
;
1544 zfs_dbgmsg("visited %llu blocks in %llums",
1545 (longlong_t
)scn
->scn_visited_this_txg
,
1546 (longlong_t
)(gethrtime() - scn
->scn_sync_start_time
) / MICROSEC
);
1548 if (!scn
->scn_pausing
) {
1549 /* finished with scan. */
1550 zfs_dbgmsg("finished scan txg %llu", (longlong_t
)tx
->tx_txg
);
1551 dsl_scan_done(scn
, B_TRUE
, tx
);
1554 if (DSL_SCAN_IS_SCRUB_RESILVER(scn
)) {
1555 mutex_enter(&spa
->spa_scrub_lock
);
1556 while (spa
->spa_scrub_inflight
> 0) {
1557 cv_wait(&spa
->spa_scrub_io_cv
,
1558 &spa
->spa_scrub_lock
);
1560 mutex_exit(&spa
->spa_scrub_lock
);
1563 dsl_scan_sync_state(scn
, tx
);
1567 * This will start a new scan, or restart an existing one.
1570 dsl_resilver_restart(dsl_pool_t
*dp
, uint64_t txg
)
1574 tx
= dmu_tx_create_dd(dp
->dp_mos_dir
);
1575 VERIFY(0 == dmu_tx_assign(tx
, TXG_WAIT
));
1577 txg
= dmu_tx_get_txg(tx
);
1578 dp
->dp_scan
->scn_restart_txg
= txg
;
1581 dp
->dp_scan
->scn_restart_txg
= txg
;
1583 zfs_dbgmsg("restarting resilver txg=%llu", txg
);
1587 dsl_scan_resilvering(dsl_pool_t
*dp
)
1589 return (dp
->dp_scan
->scn_phys
.scn_state
== DSS_SCANNING
&&
1590 dp
->dp_scan
->scn_phys
.scn_func
== POOL_SCAN_RESILVER
);
1598 count_block(zfs_all_blkstats_t
*zab
, const blkptr_t
*bp
)
1603 * If we resume after a reboot, zab will be NULL; don't record
1604 * incomplete stats in that case.
1609 for (i
= 0; i
< 4; i
++) {
1610 int l
= (i
< 2) ? BP_GET_LEVEL(bp
) : DN_MAX_LEVELS
;
1611 int t
= (i
& 1) ? BP_GET_TYPE(bp
) : DMU_OT_TOTAL
;
1612 zfs_blkstat_t
*zb
= &zab
->zab_type
[l
][t
];
1616 zb
->zb_asize
+= BP_GET_ASIZE(bp
);
1617 zb
->zb_lsize
+= BP_GET_LSIZE(bp
);
1618 zb
->zb_psize
+= BP_GET_PSIZE(bp
);
1619 zb
->zb_gangs
+= BP_COUNT_GANG(bp
);
1621 switch (BP_GET_NDVAS(bp
)) {
1623 if (DVA_GET_VDEV(&bp
->blk_dva
[0]) ==
1624 DVA_GET_VDEV(&bp
->blk_dva
[1]))
1625 zb
->zb_ditto_2_of_2_samevdev
++;
1628 equal
= (DVA_GET_VDEV(&bp
->blk_dva
[0]) ==
1629 DVA_GET_VDEV(&bp
->blk_dva
[1])) +
1630 (DVA_GET_VDEV(&bp
->blk_dva
[0]) ==
1631 DVA_GET_VDEV(&bp
->blk_dva
[2])) +
1632 (DVA_GET_VDEV(&bp
->blk_dva
[1]) ==
1633 DVA_GET_VDEV(&bp
->blk_dva
[2]));
1635 zb
->zb_ditto_2_of_3_samevdev
++;
1636 else if (equal
== 3)
1637 zb
->zb_ditto_3_of_3_samevdev
++;
1644 dsl_scan_scrub_done(zio_t
*zio
)
1646 spa_t
*spa
= zio
->io_spa
;
1648 zio_data_buf_free(zio
->io_data
, zio
->io_size
);
1650 mutex_enter(&spa
->spa_scrub_lock
);
1651 spa
->spa_scrub_inflight
--;
1652 cv_broadcast(&spa
->spa_scrub_io_cv
);
1654 if (zio
->io_error
&& (zio
->io_error
!= ECKSUM
||
1655 !(zio
->io_flags
& ZIO_FLAG_SPECULATIVE
))) {
1656 spa
->spa_dsl_pool
->dp_scan
->scn_phys
.scn_errors
++;
1658 mutex_exit(&spa
->spa_scrub_lock
);
1662 dsl_scan_scrub_cb(dsl_pool_t
*dp
,
1663 const blkptr_t
*bp
, const zbookmark_t
*zb
)
1665 dsl_scan_t
*scn
= dp
->dp_scan
;
1666 size_t size
= BP_GET_PSIZE(bp
);
1667 spa_t
*spa
= dp
->dp_spa
;
1668 uint64_t phys_birth
= BP_PHYSICAL_BIRTH(bp
);
1669 boolean_t needs_io
= B_FALSE
;
1670 int zio_flags
= ZIO_FLAG_SCAN_THREAD
| ZIO_FLAG_RAW
| ZIO_FLAG_CANFAIL
;
1671 int zio_priority
= 0;
1675 if (phys_birth
<= scn
->scn_phys
.scn_min_txg
||
1676 phys_birth
>= scn
->scn_phys
.scn_max_txg
)
1679 count_block(dp
->dp_blkstats
, bp
);
1681 ASSERT(DSL_SCAN_IS_SCRUB_RESILVER(scn
));
1682 if (scn
->scn_phys
.scn_func
== POOL_SCAN_SCRUB
) {
1683 zio_flags
|= ZIO_FLAG_SCRUB
;
1684 zio_priority
= ZIO_PRIORITY_SCRUB
;
1686 scan_delay
= zfs_scrub_delay
;
1687 } else if (scn
->scn_phys
.scn_func
== POOL_SCAN_RESILVER
) {
1688 zio_flags
|= ZIO_FLAG_RESILVER
;
1689 zio_priority
= ZIO_PRIORITY_RESILVER
;
1691 scan_delay
= zfs_resilver_delay
;
1694 /* If it's an intent log block, failure is expected. */
1695 if (zb
->zb_level
== ZB_ZIL_LEVEL
)
1696 zio_flags
|= ZIO_FLAG_SPECULATIVE
;
1698 for (d
= 0; d
< BP_GET_NDVAS(bp
); d
++) {
1699 vdev_t
*vd
= vdev_lookup_top(spa
,
1700 DVA_GET_VDEV(&bp
->blk_dva
[d
]));
1703 * Keep track of how much data we've examined so that
1704 * zpool(1M) status can make useful progress reports.
1706 scn
->scn_phys
.scn_examined
+= DVA_GET_ASIZE(&bp
->blk_dva
[d
]);
1707 spa
->spa_scan_pass_exam
+= DVA_GET_ASIZE(&bp
->blk_dva
[d
]);
1709 /* if it's a resilver, this may not be in the target range */
1711 if (DVA_GET_GANG(&bp
->blk_dva
[d
])) {
1713 * Gang members may be spread across multiple
1714 * vdevs, so the best estimate we have is the
1715 * scrub range, which has already been checked.
1716 * XXX -- it would be better to change our
1717 * allocation policy to ensure that all
1718 * gang members reside on the same vdev.
1722 needs_io
= vdev_dtl_contains(vd
, DTL_PARTIAL
,
1728 if (needs_io
&& !zfs_no_scrub_io
) {
1729 vdev_t
*rvd
= spa
->spa_root_vdev
;
1730 uint64_t maxinflight
= rvd
->vdev_children
* zfs_top_maxinflight
;
1731 void *data
= zio_data_buf_alloc(size
);
1733 mutex_enter(&spa
->spa_scrub_lock
);
1734 while (spa
->spa_scrub_inflight
>= maxinflight
)
1735 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
1736 spa
->spa_scrub_inflight
++;
1737 mutex_exit(&spa
->spa_scrub_lock
);
1740 * If we're seeing recent (zfs_scan_idle) "important" I/Os
1741 * then throttle our workload to limit the impact of a scan.
1743 if (ddi_get_lbolt64() - spa
->spa_last_io
<= zfs_scan_idle
)
1746 zio_nowait(zio_read(NULL
, spa
, bp
, data
, size
,
1747 dsl_scan_scrub_done
, NULL
, zio_priority
,
1751 /* do not relocate this block */
1756 dsl_scan(dsl_pool_t
*dp
, pool_scan_func_t func
)
1758 spa_t
*spa
= dp
->dp_spa
;
1761 * Purge all vdev caches and probe all devices. We do this here
1762 * rather than in sync context because this requires a writer lock
1763 * on the spa_config lock, which we can't do from sync context. The
1764 * spa_scrub_reopen flag indicates that vdev_open() should not
1765 * attempt to start another scrub.
1767 spa_vdev_state_enter(spa
, SCL_NONE
);
1768 spa
->spa_scrub_reopen
= B_TRUE
;
1769 vdev_reopen(spa
->spa_root_vdev
);
1770 spa
->spa_scrub_reopen
= B_FALSE
;
1771 (void) spa_vdev_state_exit(spa
, NULL
, 0);
1773 return (dsl_sync_task_do(dp
, dsl_scan_setup_check
,
1774 dsl_scan_setup_sync
, dp
->dp_scan
, &func
, 0));