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_defrag_cb
;
54 static scan_cb_t dsl_scan_scrub_cb
;
55 static scan_cb_t dsl_scan_remove_cb
;
56 static dsl_syncfunc_t dsl_scan_cancel_sync
;
57 static void dsl_scan_sync_state(dsl_scan_t
*, dmu_tx_t
*tx
);
59 int zfs_scan_min_time_ms
= 1000; /* min millisecs to scrub per txg */
60 int zfs_free_min_time_ms
= 1000; /* min millisecs to free per txg */
61 int zfs_resilver_min_time_ms
= 3000; /* min millisecs to resilver per txg */
62 boolean_t zfs_no_scrub_io
= B_FALSE
; /* set to disable scrub i/o */
63 boolean_t zfs_no_scrub_prefetch
= B_FALSE
; /* set to disable srub prefetching */
64 enum ddt_class zfs_scrub_ddt_class_max
= DDT_CLASS_DUPLICATE
;
65 int dsl_scan_delay_completion
= B_FALSE
; /* set to delay scan completion */
67 #define DSL_SCAN_IS_SCRUB_RESILVER(scn) \
68 ((scn)->scn_phys.scn_func == POOL_SCAN_SCRUB || \
69 (scn)->scn_phys.scn_func == POOL_SCAN_RESILVER)
71 extern int zfs_txg_timeout
;
73 /* the order has to match pool_scan_type */
74 static scan_cb_t
*scan_funcs
[POOL_SCAN_FUNCS
] = {
76 dsl_scan_scrub_cb
, /* POOL_SCAN_SCRUB */
77 dsl_scan_scrub_cb
, /* POOL_SCAN_RESILVER */
81 dsl_scan_init(dsl_pool_t
*dp
, uint64_t txg
)
85 spa_t
*spa
= dp
->dp_spa
;
88 scn
= dp
->dp_scan
= kmem_zalloc(sizeof (dsl_scan_t
), KM_SLEEP
);
91 err
= zap_lookup(dp
->dp_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
92 "scrub_func", sizeof (uint64_t), 1, &f
);
95 * There was an old-style scrub in progress. Restart a
96 * new-style scrub from the beginning.
98 scn
->scn_restart_txg
= txg
;
99 zfs_dbgmsg("old-style scrub was in progress; "
100 "restarting new-style scrub in txg %llu",
101 scn
->scn_restart_txg
);
104 * Load the queue obj from the old location so that it
105 * can be freed by dsl_scan_done().
107 (void) zap_lookup(dp
->dp_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
108 "scrub_queue", sizeof (uint64_t), 1,
109 &scn
->scn_phys
.scn_queue_obj
);
111 err
= zap_lookup(dp
->dp_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
112 DMU_POOL_SCAN
, sizeof (uint64_t), SCAN_PHYS_NUMINTS
,
119 if (scn
->scn_phys
.scn_state
== DSS_SCANNING
&&
120 spa_prev_software_version(dp
->dp_spa
) < SPA_VERSION_SCAN
) {
122 * A new-type scrub was in progress on an old
123 * pool, and the pool was accessed by old
124 * software. Restart from the beginning, since
125 * the old software may have changed the pool in
128 scn
->scn_restart_txg
= txg
;
129 zfs_dbgmsg("new-style scrub was modified "
130 "by old software; restarting in txg %llu",
131 scn
->scn_restart_txg
);
135 spa_scan_stat_init(spa
);
140 dsl_scan_fini(dsl_pool_t
*dp
)
143 kmem_free(dp
->dp_scan
, sizeof (dsl_scan_t
));
150 dsl_scan_setup_check(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
152 dsl_scan_t
*scn
= arg1
;
154 if (scn
->scn_phys
.scn_state
== DSS_SCANNING
)
162 dsl_scan_setup_sync(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
164 dsl_scan_t
*scn
= arg1
;
165 pool_scan_func_t
*funcp
= arg2
;
166 dmu_object_type_t ot
= 0;
167 dsl_pool_t
*dp
= scn
->scn_dp
;
168 spa_t
*spa
= dp
->dp_spa
;
170 ASSERT(scn
->scn_phys
.scn_state
!= DSS_SCANNING
);
171 ASSERT(*funcp
> POOL_SCAN_NONE
&& *funcp
< POOL_SCAN_FUNCS
);
172 bzero(&scn
->scn_phys
, sizeof (scn
->scn_phys
));
173 scn
->scn_phys
.scn_func
= *funcp
;
174 scn
->scn_phys
.scn_state
= DSS_SCANNING
;
175 scn
->scn_phys
.scn_min_txg
= 0;
176 scn
->scn_phys
.scn_max_txg
= tx
->tx_txg
;
177 scn
->scn_phys
.scn_ddt_class_max
= DDT_CLASSES
- 1; /* the entire DDT */
178 scn
->scn_phys
.scn_start_time
= gethrestime_sec();
179 scn
->scn_phys
.scn_errors
= 0;
180 scn
->scn_phys
.scn_to_examine
= spa
->spa_root_vdev
->vdev_stat
.vs_alloc
;
181 scn
->scn_restart_txg
= 0;
182 spa_scan_stat_init(spa
);
184 if (DSL_SCAN_IS_SCRUB_RESILVER(scn
)) {
185 scn
->scn_phys
.scn_ddt_class_max
= zfs_scrub_ddt_class_max
;
187 /* rewrite all disk labels */
188 vdev_config_dirty(spa
->spa_root_vdev
);
190 if (vdev_resilver_needed(spa
->spa_root_vdev
,
191 &scn
->scn_phys
.scn_min_txg
, &scn
->scn_phys
.scn_max_txg
)) {
192 spa_event_notify(spa
, NULL
, ESC_ZFS_RESILVER_START
);
194 spa_event_notify(spa
, NULL
, ESC_ZFS_SCRUB_START
);
197 spa
->spa_scrub_started
= B_TRUE
;
199 * If this is an incremental scrub, limit the DDT scrub phase
200 * to just the auto-ditto class (for correctness); the rest
201 * of the scrub should go faster using top-down pruning.
203 if (scn
->scn_phys
.scn_min_txg
> TXG_INITIAL
)
204 scn
->scn_phys
.scn_ddt_class_max
= DDT_CLASS_DITTO
;
208 /* back to the generic stuff */
210 if (dp
->dp_blkstats
== NULL
) {
212 kmem_alloc(sizeof (zfs_all_blkstats_t
), KM_SLEEP
);
214 bzero(dp
->dp_blkstats
, sizeof (zfs_all_blkstats_t
));
216 if (spa_version(spa
) < SPA_VERSION_DSL_SCRUB
)
217 ot
= DMU_OT_ZAP_OTHER
;
219 scn
->scn_phys
.scn_queue_obj
= zap_create(dp
->dp_meta_objset
,
220 ot
? ot
: DMU_OT_SCAN_QUEUE
, DMU_OT_NONE
, 0, tx
);
222 dsl_scan_sync_state(scn
, tx
);
224 spa_history_log_internal(LOG_POOL_SCAN
, spa
, tx
,
225 "func=%u mintxg=%llu maxtxg=%llu",
226 *funcp
, scn
->scn_phys
.scn_min_txg
, scn
->scn_phys
.scn_max_txg
);
231 dsl_scan_done(dsl_scan_t
*scn
, boolean_t complete
, dmu_tx_t
*tx
)
233 static const char *old_names
[] = {
235 "scrub_ddt_bookmark",
236 "scrub_ddt_class_max",
245 dsl_pool_t
*dp
= scn
->scn_dp
;
246 spa_t
*spa
= dp
->dp_spa
;
249 /* Remove any remnants of an old-style scrub. */
250 for (i
= 0; old_names
[i
]; i
++) {
251 (void) zap_remove(dp
->dp_meta_objset
,
252 DMU_POOL_DIRECTORY_OBJECT
, old_names
[i
], tx
);
255 if (scn
->scn_phys
.scn_queue_obj
!= 0) {
256 VERIFY(0 == dmu_object_free(dp
->dp_meta_objset
,
257 scn
->scn_phys
.scn_queue_obj
, tx
));
258 scn
->scn_phys
.scn_queue_obj
= 0;
262 * If we were "restarted" from a stopped state, don't bother
263 * with anything else.
265 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
269 scn
->scn_phys
.scn_state
= DSS_FINISHED
;
271 scn
->scn_phys
.scn_state
= DSS_CANCELED
;
273 spa_history_log_internal(LOG_POOL_SCAN_DONE
, spa
, tx
,
274 "complete=%u", complete
);
276 if (DSL_SCAN_IS_SCRUB_RESILVER(scn
)) {
277 mutex_enter(&spa
->spa_scrub_lock
);
278 while (spa
->spa_scrub_inflight
> 0) {
279 cv_wait(&spa
->spa_scrub_io_cv
,
280 &spa
->spa_scrub_lock
);
282 mutex_exit(&spa
->spa_scrub_lock
);
283 spa
->spa_scrub_started
= B_FALSE
;
284 spa
->spa_scrub_active
= B_FALSE
;
287 * If the scrub/resilver completed, update all DTLs to
288 * reflect this. Whether it succeeded or not, vacate
289 * all temporary scrub DTLs.
291 vdev_dtl_reassess(spa
->spa_root_vdev
, tx
->tx_txg
,
292 complete
? scn
->scn_phys
.scn_max_txg
: 0, B_TRUE
);
294 spa_event_notify(spa
, NULL
, scn
->scn_phys
.scn_min_txg
?
295 ESC_ZFS_RESILVER_FINISH
: ESC_ZFS_SCRUB_FINISH
);
297 spa_errlog_rotate(spa
);
300 * We may have finished replacing a device.
301 * Let the async thread assess this and handle the detach.
303 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
306 scn
->scn_phys
.scn_end_time
= gethrestime_sec();
311 dsl_scan_cancel_check(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
313 dsl_scan_t
*scn
= arg1
;
315 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
322 dsl_scan_cancel_sync(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
324 dsl_scan_t
*scn
= arg1
;
326 dsl_scan_done(scn
, B_FALSE
, tx
);
327 dsl_scan_sync_state(scn
, tx
);
331 dsl_scan_cancel(dsl_pool_t
*dp
)
333 boolean_t complete
= B_FALSE
;
336 err
= dsl_sync_task_do(dp
, dsl_scan_cancel_check
,
337 dsl_scan_cancel_sync
, dp
->dp_scan
, &complete
, 3);
341 static void dsl_scan_visitbp(blkptr_t
*bp
,
342 const zbookmark_t
*zb
, dnode_phys_t
*dnp
, arc_buf_t
*pbuf
,
343 dsl_dataset_t
*ds
, dsl_scan_t
*scn
, dmu_objset_type_t ostype
,
345 static void dsl_scan_visitdnode(dsl_scan_t
*, dsl_dataset_t
*ds
,
346 dmu_objset_type_t ostype
,
347 dnode_phys_t
*dnp
, arc_buf_t
*buf
, uint64_t object
, dmu_tx_t
*tx
);
350 dsl_free(dsl_pool_t
*dp
, uint64_t txg
, const blkptr_t
*bp
)
352 zio_free(dp
->dp_spa
, txg
, bp
);
356 dsl_free_sync(zio_t
*pio
, dsl_pool_t
*dp
, uint64_t txg
, const blkptr_t
*bpp
)
358 ASSERT(dsl_pool_sync_context(dp
));
359 zio_nowait(zio_free_sync(pio
, dp
->dp_spa
, txg
, bpp
, pio
->io_flags
));
363 dsl_read(zio_t
*pio
, spa_t
*spa
, const blkptr_t
*bpp
, arc_buf_t
*pbuf
,
364 arc_done_func_t
*done
, void *private, int priority
, int zio_flags
,
365 uint32_t *arc_flags
, const zbookmark_t
*zb
)
367 return (arc_read(pio
, spa
, bpp
, pbuf
, done
, private,
368 priority
, zio_flags
, arc_flags
, zb
));
372 dsl_read_nolock(zio_t
*pio
, spa_t
*spa
, const blkptr_t
*bpp
,
373 arc_done_func_t
*done
, void *private, int priority
, int zio_flags
,
374 uint32_t *arc_flags
, const zbookmark_t
*zb
)
376 return (arc_read_nolock(pio
, spa
, bpp
, done
, private,
377 priority
, zio_flags
, arc_flags
, zb
));
381 bookmark_is_zero(const zbookmark_t
*zb
)
383 return (zb
->zb_objset
== 0 && zb
->zb_object
== 0 &&
384 zb
->zb_level
== 0 && zb
->zb_blkid
== 0);
387 /* dnp is the dnode for zb1->zb_object */
389 bookmark_is_before(const dnode_phys_t
*dnp
, const zbookmark_t
*zb1
,
390 const zbookmark_t
*zb2
)
392 uint64_t zb1nextL0
, zb2thisobj
;
394 ASSERT(zb1
->zb_objset
== zb2
->zb_objset
);
395 ASSERT(zb2
->zb_level
== 0);
398 * A bookmark in the deadlist is considered to be after
401 if (zb2
->zb_object
== DMU_DEADLIST_OBJECT
)
404 /* The objset_phys_t isn't before anything. */
408 zb1nextL0
= (zb1
->zb_blkid
+ 1) <<
409 ((zb1
->zb_level
) * (dnp
->dn_indblkshift
- SPA_BLKPTRSHIFT
));
411 zb2thisobj
= zb2
->zb_object
? zb2
->zb_object
:
412 zb2
->zb_blkid
<< (DNODE_BLOCK_SHIFT
- DNODE_SHIFT
);
414 if (zb1
->zb_object
== DMU_META_DNODE_OBJECT
) {
415 uint64_t nextobj
= zb1nextL0
*
416 (dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
) >> DNODE_SHIFT
;
417 return (nextobj
<= zb2thisobj
);
420 if (zb1
->zb_object
< zb2thisobj
)
422 if (zb1
->zb_object
> zb2thisobj
)
424 if (zb2
->zb_object
== DMU_META_DNODE_OBJECT
)
426 return (zb1nextL0
<= zb2
->zb_blkid
);
430 dsl_scan_ds_maxtxg(dsl_dataset_t
*ds
)
432 uint64_t smt
= ds
->ds_dir
->dd_pool
->dp_scan
->scn_phys
.scn_max_txg
;
433 if (dsl_dataset_is_snapshot(ds
))
434 return (MIN(smt
, ds
->ds_phys
->ds_creation_txg
));
439 dsl_scan_sync_state(dsl_scan_t
*scn
, dmu_tx_t
*tx
)
441 VERIFY(0 == zap_update(scn
->scn_dp
->dp_meta_objset
,
442 DMU_POOL_DIRECTORY_OBJECT
,
443 DMU_POOL_SCAN
, sizeof (uint64_t), SCAN_PHYS_NUMINTS
,
444 &scn
->scn_phys
, tx
));
448 dsl_scan_check_pause(dsl_scan_t
*scn
, const zbookmark_t
*zb
)
450 uint64_t elapsed_nanosecs
;
453 /* we never skip user/group accounting objects */
454 if (zb
&& (int64_t)zb
->zb_object
< 0)
457 if (scn
->scn_pausing
)
458 return (B_TRUE
); /* we're already pausing */
460 if (!bookmark_is_zero(&scn
->scn_phys
.scn_bookmark
))
461 return (B_FALSE
); /* we're resuming */
463 /* We only know how to resume from level-0 blocks. */
464 if (zb
&& zb
->zb_level
!= 0)
467 mintime
= (scn
->scn_phys
.scn_func
== POOL_SCAN_RESILVER
) ?
468 zfs_resilver_min_time_ms
: zfs_scan_min_time_ms
;
469 elapsed_nanosecs
= gethrtime() - scn
->scn_sync_start_time
;
470 if (elapsed_nanosecs
/ NANOSEC
> zfs_txg_timeout
||
471 (elapsed_nanosecs
/ MICROSEC
> mintime
&&
472 txg_sync_waiting(scn
->scn_dp
)) ||
473 spa_shutting_down(scn
->scn_dp
->dp_spa
)) {
475 dprintf("pausing at bookmark %llx/%llx/%llx/%llx\n",
476 (longlong_t
)zb
->zb_objset
,
477 (longlong_t
)zb
->zb_object
,
478 (longlong_t
)zb
->zb_level
,
479 (longlong_t
)zb
->zb_blkid
);
480 scn
->scn_phys
.scn_bookmark
= *zb
;
482 dprintf("pausing at DDT bookmark %llx/%llx/%llx/%llx\n",
483 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_class
,
484 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_type
,
485 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_checksum
,
486 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_cursor
);
487 scn
->scn_pausing
= B_TRUE
;
493 typedef struct zil_scan_arg
{
495 zil_header_t
*zsa_zh
;
500 dsl_scan_zil_block(zilog_t
*zilog
, blkptr_t
*bp
, void *arg
, uint64_t claim_txg
)
502 zil_scan_arg_t
*zsa
= arg
;
503 dsl_pool_t
*dp
= zsa
->zsa_dp
;
504 dsl_scan_t
*scn
= dp
->dp_scan
;
505 zil_header_t
*zh
= zsa
->zsa_zh
;
508 if (bp
->blk_birth
<= scn
->scn_phys
.scn_cur_min_txg
)
512 * One block ("stubby") can be allocated a long time ago; we
513 * want to visit that one because it has been allocated
514 * (on-disk) even if it hasn't been claimed (even though for
515 * scrub there's nothing to do to it).
517 if (claim_txg
== 0 && bp
->blk_birth
>= spa_first_txg(dp
->dp_spa
))
520 SET_BOOKMARK(&zb
, zh
->zh_log
.blk_cksum
.zc_word
[ZIL_ZC_OBJSET
],
521 ZB_ZIL_OBJECT
, ZB_ZIL_LEVEL
, bp
->blk_cksum
.zc_word
[ZIL_ZC_SEQ
]);
523 VERIFY(0 == scan_funcs
[scn
->scn_phys
.scn_func
](dp
, bp
, &zb
));
529 dsl_scan_zil_record(zilog_t
*zilog
, lr_t
*lrc
, void *arg
, uint64_t claim_txg
)
531 if (lrc
->lrc_txtype
== TX_WRITE
) {
532 zil_scan_arg_t
*zsa
= arg
;
533 dsl_pool_t
*dp
= zsa
->zsa_dp
;
534 dsl_scan_t
*scn
= dp
->dp_scan
;
535 zil_header_t
*zh
= zsa
->zsa_zh
;
536 lr_write_t
*lr
= (lr_write_t
*)lrc
;
537 blkptr_t
*bp
= &lr
->lr_blkptr
;
540 if (bp
->blk_birth
<= scn
->scn_phys
.scn_cur_min_txg
)
544 * birth can be < claim_txg if this record's txg is
545 * already txg sync'ed (but this log block contains
546 * other records that are not synced)
548 if (claim_txg
== 0 || bp
->blk_birth
< claim_txg
)
551 SET_BOOKMARK(&zb
, zh
->zh_log
.blk_cksum
.zc_word
[ZIL_ZC_OBJSET
],
552 lr
->lr_foid
, ZB_ZIL_LEVEL
,
553 lr
->lr_offset
/ BP_GET_LSIZE(bp
));
555 VERIFY(0 == scan_funcs
[scn
->scn_phys
.scn_func
](dp
, bp
, &zb
));
561 dsl_scan_zil(dsl_pool_t
*dp
, zil_header_t
*zh
)
563 uint64_t claim_txg
= zh
->zh_claim_txg
;
564 zil_scan_arg_t zsa
= { dp
, zh
};
568 * We only want to visit blocks that have been claimed but not yet
569 * replayed (or, in read-only mode, blocks that *would* be claimed).
571 if (claim_txg
== 0 && spa_writeable(dp
->dp_spa
))
574 zilog
= zil_alloc(dp
->dp_meta_objset
, zh
);
576 (void) zil_parse(zilog
, dsl_scan_zil_block
, dsl_scan_zil_record
, &zsa
,
584 dsl_scan_prefetch(dsl_scan_t
*scn
, arc_buf_t
*buf
, blkptr_t
*bp
,
585 uint64_t objset
, uint64_t object
, uint64_t blkid
)
588 uint32_t flags
= ARC_NOWAIT
| ARC_PREFETCH
;
590 if (zfs_no_scrub_prefetch
)
593 if (BP_IS_HOLE(bp
) || bp
->blk_birth
<= scn
->scn_phys
.scn_min_txg
||
594 (BP_GET_LEVEL(bp
) == 0 && BP_GET_TYPE(bp
) != DMU_OT_DNODE
))
597 SET_BOOKMARK(&czb
, objset
, object
, BP_GET_LEVEL(bp
), blkid
);
600 * XXX need to make sure all of these arc_read() prefetches are
601 * done before setting xlateall (similar to dsl_read())
603 (void) arc_read(scn
->scn_zio_root
, scn
->scn_dp
->dp_spa
, bp
,
604 buf
, NULL
, NULL
, ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_CANFAIL
,
609 dsl_scan_check_resume(dsl_scan_t
*scn
, const dnode_phys_t
*dnp
,
610 const zbookmark_t
*zb
)
613 * We never skip over user/group accounting objects (obj<0)
615 if (!bookmark_is_zero(&scn
->scn_phys
.scn_bookmark
) &&
616 (int64_t)zb
->zb_object
>= 0) {
618 * If we already visited this bp & everything below (in
619 * a prior txg sync), don't bother doing it again.
621 if (bookmark_is_before(dnp
, zb
, &scn
->scn_phys
.scn_bookmark
))
625 * If we found the block we're trying to resume from, or
626 * we went past it to a different object, zero it out to
627 * indicate that it's OK to start checking for pausing
630 if (bcmp(zb
, &scn
->scn_phys
.scn_bookmark
, sizeof (*zb
)) == 0 ||
631 zb
->zb_object
> scn
->scn_phys
.scn_bookmark
.zb_object
) {
632 dprintf("resuming at %llx/%llx/%llx/%llx\n",
633 (longlong_t
)zb
->zb_objset
,
634 (longlong_t
)zb
->zb_object
,
635 (longlong_t
)zb
->zb_level
,
636 (longlong_t
)zb
->zb_blkid
);
637 bzero(&scn
->scn_phys
.scn_bookmark
, sizeof (*zb
));
644 * Return nonzero on i/o error.
645 * Return new buf to write out in *bufp.
648 dsl_scan_recurse(dsl_scan_t
*scn
, dsl_dataset_t
*ds
, dmu_objset_type_t ostype
,
649 dnode_phys_t
*dnp
, const blkptr_t
*bp
,
650 const zbookmark_t
*zb
, dmu_tx_t
*tx
, arc_buf_t
**bufp
)
652 dsl_pool_t
*dp
= scn
->scn_dp
;
655 if (BP_GET_LEVEL(bp
) > 0) {
656 uint32_t flags
= ARC_WAIT
;
659 int epb
= BP_GET_LSIZE(bp
) >> SPA_BLKPTRSHIFT
;
661 err
= arc_read_nolock(NULL
, dp
->dp_spa
, bp
,
662 arc_getbuf_func
, bufp
,
663 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_CANFAIL
, &flags
, zb
);
665 scn
->scn_phys
.scn_errors
++;
668 for (i
= 0, cbp
= (*bufp
)->b_data
; i
< epb
; i
++, cbp
++) {
669 dsl_scan_prefetch(scn
, *bufp
, cbp
, zb
->zb_objset
,
670 zb
->zb_object
, zb
->zb_blkid
* epb
+ i
);
672 for (i
= 0, cbp
= (*bufp
)->b_data
; i
< epb
; i
++, cbp
++) {
675 SET_BOOKMARK(&czb
, zb
->zb_objset
, zb
->zb_object
,
677 zb
->zb_blkid
* epb
+ i
);
678 dsl_scan_visitbp(cbp
, &czb
, dnp
,
679 *bufp
, ds
, scn
, ostype
, tx
);
681 } else if (BP_GET_TYPE(bp
) == DMU_OT_USERGROUP_USED
) {
682 uint32_t flags
= ARC_WAIT
;
684 err
= arc_read_nolock(NULL
, dp
->dp_spa
, bp
,
685 arc_getbuf_func
, bufp
,
686 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_CANFAIL
, &flags
, zb
);
688 scn
->scn_phys
.scn_errors
++;
691 } else if (BP_GET_TYPE(bp
) == DMU_OT_DNODE
) {
692 uint32_t flags
= ARC_WAIT
;
695 int epb
= BP_GET_LSIZE(bp
) >> DNODE_SHIFT
;
697 err
= arc_read_nolock(NULL
, dp
->dp_spa
, bp
,
698 arc_getbuf_func
, bufp
,
699 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_CANFAIL
, &flags
, zb
);
701 scn
->scn_phys
.scn_errors
++;
704 for (i
= 0, cdnp
= (*bufp
)->b_data
; i
< epb
; i
++, cdnp
++) {
705 for (j
= 0; j
< cdnp
->dn_nblkptr
; j
++) {
706 blkptr_t
*cbp
= &cdnp
->dn_blkptr
[j
];
707 dsl_scan_prefetch(scn
, *bufp
, cbp
,
708 zb
->zb_objset
, zb
->zb_blkid
* epb
+ i
, j
);
711 for (i
= 0, cdnp
= (*bufp
)->b_data
; i
< epb
; i
++, cdnp
++) {
712 dsl_scan_visitdnode(scn
, ds
, ostype
,
713 cdnp
, *bufp
, zb
->zb_blkid
* epb
+ i
, tx
);
716 } else if (BP_GET_TYPE(bp
) == DMU_OT_OBJSET
) {
717 uint32_t flags
= ARC_WAIT
;
720 err
= arc_read_nolock(NULL
, dp
->dp_spa
, bp
,
721 arc_getbuf_func
, bufp
,
722 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_CANFAIL
, &flags
, zb
);
724 scn
->scn_phys
.scn_errors
++;
728 osp
= (*bufp
)->b_data
;
730 if (DSL_SCAN_IS_SCRUB_RESILVER(scn
))
731 dsl_scan_zil(dp
, &osp
->os_zil_header
);
733 dsl_scan_visitdnode(scn
, ds
, osp
->os_type
,
734 &osp
->os_meta_dnode
, *bufp
, DMU_META_DNODE_OBJECT
, tx
);
736 if (OBJSET_BUF_HAS_USERUSED(*bufp
)) {
738 * We also always visit user/group accounting
739 * objects, and never skip them, even if we are
740 * pausing. This is necessary so that the space
741 * deltas from this txg get integrated.
743 dsl_scan_visitdnode(scn
, ds
, osp
->os_type
,
744 &osp
->os_groupused_dnode
, *bufp
,
745 DMU_GROUPUSED_OBJECT
, tx
);
746 dsl_scan_visitdnode(scn
, ds
, osp
->os_type
,
747 &osp
->os_userused_dnode
, *bufp
,
748 DMU_USERUSED_OBJECT
, tx
);
756 dsl_scan_visitdnode(dsl_scan_t
*scn
, dsl_dataset_t
*ds
,
757 dmu_objset_type_t ostype
, dnode_phys_t
*dnp
, arc_buf_t
*buf
,
758 uint64_t object
, dmu_tx_t
*tx
)
762 for (j
= 0; j
< dnp
->dn_nblkptr
; j
++) {
765 SET_BOOKMARK(&czb
, ds
? ds
->ds_object
: 0, object
,
766 dnp
->dn_nlevels
- 1, j
);
767 dsl_scan_visitbp(&dnp
->dn_blkptr
[j
],
768 &czb
, dnp
, buf
, ds
, scn
, ostype
, tx
);
771 if (dnp
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
) {
773 SET_BOOKMARK(&czb
, ds
? ds
->ds_object
: 0, object
,
775 dsl_scan_visitbp(&dnp
->dn_spill
,
776 &czb
, dnp
, buf
, ds
, scn
, ostype
, tx
);
781 * The arguments are in this order because mdb can only print the
782 * first 5; we want them to be useful.
785 dsl_scan_visitbp(blkptr_t
*bp
, const zbookmark_t
*zb
,
786 dnode_phys_t
*dnp
, arc_buf_t
*pbuf
,
787 dsl_dataset_t
*ds
, dsl_scan_t
*scn
, dmu_objset_type_t ostype
,
790 dsl_pool_t
*dp
= scn
->scn_dp
;
791 arc_buf_t
*buf
= NULL
;
792 blkptr_t bp_toread
= *bp
;
794 /* ASSERT(pbuf == NULL || arc_released(pbuf)); */
796 if (dsl_scan_check_pause(scn
, zb
))
799 if (dsl_scan_check_resume(scn
, dnp
, zb
))
802 if (bp
->blk_birth
== 0)
805 scn
->scn_visited_this_txg
++;
808 "visiting ds=%p/%llu zb=%llx/%llx/%llx/%llx buf=%p bp=%p",
809 ds
, ds
? ds
->ds_object
: 0,
810 zb
->zb_objset
, zb
->zb_object
, zb
->zb_level
, zb
->zb_blkid
,
813 if (bp
->blk_birth
<= scn
->scn_phys
.scn_cur_min_txg
)
816 if (BP_GET_TYPE(bp
) != DMU_OT_USERGROUP_USED
) {
818 * For non-user-accounting blocks, we need to read the
819 * new bp (from a deleted snapshot, found in
820 * check_existing_xlation). If we used the old bp,
821 * pointers inside this block from before we resumed
822 * would be untranslated.
824 * For user-accounting blocks, we need to read the old
825 * bp, because we will apply the entire space delta to
826 * it (original untranslated -> translations from
827 * deleted snap -> now).
832 if (dsl_scan_recurse(scn
, ds
, ostype
, dnp
, &bp_toread
, zb
, tx
,
837 * If dsl_scan_ddt() has aready visited this block, it will have
838 * already done any translations or scrubbing, so don't call the
841 if (ddt_class_contains(dp
->dp_spa
,
842 scn
->scn_phys
.scn_ddt_class_max
, bp
)) {
848 * If this block is from the future (after cur_max_txg), then we
849 * are doing this on behalf of a deleted snapshot, and we will
850 * revisit the future block on the next pass of this dataset.
851 * Don't scan it now unless we need to because something
852 * under it was modified.
854 if (bp
->blk_birth
<= scn
->scn_phys
.scn_cur_max_txg
) {
855 scan_funcs
[scn
->scn_phys
.scn_func
](dp
, bp
, zb
);
858 (void) arc_buf_remove_ref(buf
, &buf
);
862 dsl_scan_visit_rootbp(dsl_scan_t
*scn
, dsl_dataset_t
*ds
, blkptr_t
*bp
,
867 SET_BOOKMARK(&zb
, ds
? ds
->ds_object
: DMU_META_OBJSET
,
868 ZB_ROOT_OBJECT
, ZB_ROOT_LEVEL
, ZB_ROOT_BLKID
);
869 dsl_scan_visitbp(bp
, &zb
, NULL
, NULL
,
870 ds
, scn
, DMU_OST_NONE
, tx
);
872 dprintf_ds(ds
, "finished scan%s", "");
876 dsl_scan_ds_destroyed(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
878 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
879 dsl_scan_t
*scn
= dp
->dp_scan
;
882 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
885 if (scn
->scn_phys
.scn_bookmark
.zb_objset
== ds
->ds_object
) {
886 if (dsl_dataset_is_snapshot(ds
)) {
887 /* Note, scn_cur_{min,max}_txg stays the same. */
888 scn
->scn_phys
.scn_bookmark
.zb_objset
=
889 ds
->ds_phys
->ds_next_snap_obj
;
890 zfs_dbgmsg("destroying ds %llu; currently traversing; "
891 "reset zb_objset to %llu",
892 (u_longlong_t
)ds
->ds_object
,
893 (u_longlong_t
)ds
->ds_phys
->ds_next_snap_obj
);
894 scn
->scn_phys
.scn_flags
|= DSF_VISIT_DS_AGAIN
;
896 SET_BOOKMARK(&scn
->scn_phys
.scn_bookmark
,
897 ZB_DESTROYED_OBJSET
, 0, 0, 0);
898 zfs_dbgmsg("destroying ds %llu; currently traversing; "
899 "reset bookmark to -1,0,0,0",
900 (u_longlong_t
)ds
->ds_object
);
902 } else if (zap_lookup_int_key(dp
->dp_meta_objset
,
903 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
, &mintxg
) == 0) {
904 ASSERT3U(ds
->ds_phys
->ds_num_children
, <=, 1);
905 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
906 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
, tx
));
907 if (dsl_dataset_is_snapshot(ds
)) {
909 * We keep the same mintxg; it could be >
910 * ds_creation_txg if the previous snapshot was
913 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
914 scn
->scn_phys
.scn_queue_obj
,
915 ds
->ds_phys
->ds_next_snap_obj
, mintxg
, tx
) == 0);
916 zfs_dbgmsg("destroying ds %llu; in queue; "
917 "replacing with %llu",
918 (u_longlong_t
)ds
->ds_object
,
919 (u_longlong_t
)ds
->ds_phys
->ds_next_snap_obj
);
921 zfs_dbgmsg("destroying ds %llu; in queue; removing",
922 (u_longlong_t
)ds
->ds_object
);
925 zfs_dbgmsg("destroying ds %llu; ignoring",
926 (u_longlong_t
)ds
->ds_object
);
930 * dsl_scan_sync() should be called after this, and should sync
931 * out our changed state, but just to be safe, do it here.
933 dsl_scan_sync_state(scn
, tx
);
937 dsl_scan_ds_snapshotted(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
939 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
940 dsl_scan_t
*scn
= dp
->dp_scan
;
943 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
946 ASSERT(ds
->ds_phys
->ds_prev_snap_obj
!= 0);
948 if (scn
->scn_phys
.scn_bookmark
.zb_objset
== ds
->ds_object
) {
949 scn
->scn_phys
.scn_bookmark
.zb_objset
=
950 ds
->ds_phys
->ds_prev_snap_obj
;
951 zfs_dbgmsg("snapshotting ds %llu; currently traversing; "
952 "reset zb_objset to %llu",
953 (u_longlong_t
)ds
->ds_object
,
954 (u_longlong_t
)ds
->ds_phys
->ds_prev_snap_obj
);
955 } else if (zap_lookup_int_key(dp
->dp_meta_objset
,
956 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
, &mintxg
) == 0) {
957 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
958 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
, tx
));
959 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
960 scn
->scn_phys
.scn_queue_obj
,
961 ds
->ds_phys
->ds_prev_snap_obj
, mintxg
, tx
) == 0);
962 zfs_dbgmsg("snapshotting ds %llu; in queue; "
963 "replacing with %llu",
964 (u_longlong_t
)ds
->ds_object
,
965 (u_longlong_t
)ds
->ds_phys
->ds_prev_snap_obj
);
967 dsl_scan_sync_state(scn
, tx
);
971 dsl_scan_ds_clone_swapped(dsl_dataset_t
*ds1
, dsl_dataset_t
*ds2
, dmu_tx_t
*tx
)
973 dsl_pool_t
*dp
= ds1
->ds_dir
->dd_pool
;
974 dsl_scan_t
*scn
= dp
->dp_scan
;
977 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
980 if (scn
->scn_phys
.scn_bookmark
.zb_objset
== ds1
->ds_object
) {
981 scn
->scn_phys
.scn_bookmark
.zb_objset
= ds2
->ds_object
;
982 zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
983 "reset zb_objset to %llu",
984 (u_longlong_t
)ds1
->ds_object
,
985 (u_longlong_t
)ds2
->ds_object
);
986 } else if (scn
->scn_phys
.scn_bookmark
.zb_objset
== ds2
->ds_object
) {
987 scn
->scn_phys
.scn_bookmark
.zb_objset
= ds1
->ds_object
;
988 zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
989 "reset zb_objset to %llu",
990 (u_longlong_t
)ds2
->ds_object
,
991 (u_longlong_t
)ds1
->ds_object
);
994 if (zap_lookup_int_key(dp
->dp_meta_objset
, scn
->scn_phys
.scn_queue_obj
,
995 ds1
->ds_object
, &mintxg
) == 0) {
998 ASSERT3U(mintxg
, ==, ds1
->ds_phys
->ds_prev_snap_txg
);
999 ASSERT3U(mintxg
, ==, ds2
->ds_phys
->ds_prev_snap_txg
);
1000 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
1001 scn
->scn_phys
.scn_queue_obj
, ds1
->ds_object
, tx
));
1002 err
= zap_add_int_key(dp
->dp_meta_objset
,
1003 scn
->scn_phys
.scn_queue_obj
, ds2
->ds_object
, mintxg
, tx
);
1004 VERIFY(err
== 0 || err
== EEXIST
);
1005 if (err
== EEXIST
) {
1006 /* Both were there to begin with */
1007 VERIFY(0 == zap_add_int_key(dp
->dp_meta_objset
,
1008 scn
->scn_phys
.scn_queue_obj
,
1009 ds1
->ds_object
, mintxg
, tx
));
1011 zfs_dbgmsg("clone_swap ds %llu; in queue; "
1012 "replacing with %llu",
1013 (u_longlong_t
)ds1
->ds_object
,
1014 (u_longlong_t
)ds2
->ds_object
);
1015 } else if (zap_lookup_int_key(dp
->dp_meta_objset
,
1016 scn
->scn_phys
.scn_queue_obj
, ds2
->ds_object
, &mintxg
) == 0) {
1017 ASSERT3U(mintxg
, ==, ds1
->ds_phys
->ds_prev_snap_txg
);
1018 ASSERT3U(mintxg
, ==, ds2
->ds_phys
->ds_prev_snap_txg
);
1019 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
1020 scn
->scn_phys
.scn_queue_obj
, ds2
->ds_object
, tx
));
1021 VERIFY(0 == zap_add_int_key(dp
->dp_meta_objset
,
1022 scn
->scn_phys
.scn_queue_obj
, ds1
->ds_object
, mintxg
, tx
));
1023 zfs_dbgmsg("clone_swap ds %llu; in queue; "
1024 "replacing with %llu",
1025 (u_longlong_t
)ds2
->ds_object
,
1026 (u_longlong_t
)ds1
->ds_object
);
1029 dsl_scan_sync_state(scn
, tx
);
1032 struct enqueue_clones_arg
{
1039 enqueue_clones_cb(spa_t
*spa
, uint64_t dsobj
, const char *dsname
, void *arg
)
1041 struct enqueue_clones_arg
*eca
= arg
;
1044 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
1045 dsl_scan_t
*scn
= dp
->dp_scan
;
1047 err
= dsl_dataset_hold_obj(dp
, dsobj
, FTAG
, &ds
);
1051 if (ds
->ds_dir
->dd_phys
->dd_origin_obj
== eca
->originobj
) {
1052 while (ds
->ds_phys
->ds_prev_snap_obj
!= eca
->originobj
) {
1053 dsl_dataset_t
*prev
;
1054 err
= dsl_dataset_hold_obj(dp
,
1055 ds
->ds_phys
->ds_prev_snap_obj
, FTAG
, &prev
);
1057 dsl_dataset_rele(ds
, FTAG
);
1062 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
1063 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
,
1064 ds
->ds_phys
->ds_prev_snap_txg
, eca
->tx
) == 0);
1066 dsl_dataset_rele(ds
, FTAG
);
1071 dsl_scan_visitds(dsl_scan_t
*scn
, uint64_t dsobj
, dmu_tx_t
*tx
)
1073 dsl_pool_t
*dp
= scn
->scn_dp
;
1076 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp
, dsobj
, FTAG
, &ds
));
1079 * Iterate over the bps in this ds.
1081 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
1082 dsl_scan_visit_rootbp(scn
, ds
, &ds
->ds_phys
->ds_bp
, tx
);
1084 char *dsname
= kmem_alloc(ZFS_MAXNAMELEN
, KM_SLEEP
);
1085 dsl_dataset_name(ds
, dsname
);
1086 zfs_dbgmsg("scanned dataset %llu (%s) with min=%llu max=%llu; "
1088 (longlong_t
)dsobj
, dsname
,
1089 (longlong_t
)scn
->scn_phys
.scn_cur_min_txg
,
1090 (longlong_t
)scn
->scn_phys
.scn_cur_max_txg
,
1091 (int)scn
->scn_pausing
);
1092 kmem_free(dsname
, ZFS_MAXNAMELEN
);
1094 if (scn
->scn_pausing
)
1098 * We've finished this pass over this dataset.
1102 * If we did not completely visit this dataset, do another pass.
1104 if (scn
->scn_phys
.scn_flags
& DSF_VISIT_DS_AGAIN
) {
1105 zfs_dbgmsg("incomplete pass; visiting again");
1106 scn
->scn_phys
.scn_flags
&= ~DSF_VISIT_DS_AGAIN
;
1107 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
1108 scn
->scn_phys
.scn_queue_obj
, ds
->ds_object
,
1109 scn
->scn_phys
.scn_cur_max_txg
, tx
) == 0);
1114 * Add descendent datasets to work queue.
1116 if (ds
->ds_phys
->ds_next_snap_obj
!= 0) {
1117 VERIFY(zap_add_int_key(dp
->dp_meta_objset
,
1118 scn
->scn_phys
.scn_queue_obj
, ds
->ds_phys
->ds_next_snap_obj
,
1119 ds
->ds_phys
->ds_creation_txg
, tx
) == 0);
1121 if (ds
->ds_phys
->ds_num_children
> 1) {
1122 boolean_t usenext
= B_FALSE
;
1123 if (ds
->ds_phys
->ds_next_clones_obj
!= 0) {
1126 * A bug in a previous version of the code could
1127 * cause upgrade_clones_cb() to not set
1128 * ds_next_snap_obj when it should, leading to a
1129 * missing entry. Therefore we can only use the
1130 * next_clones_obj when its count is correct.
1132 int err
= zap_count(dp
->dp_meta_objset
,
1133 ds
->ds_phys
->ds_next_clones_obj
, &count
);
1135 count
== ds
->ds_phys
->ds_num_children
- 1)
1140 VERIFY(zap_join_key(dp
->dp_meta_objset
,
1141 ds
->ds_phys
->ds_next_clones_obj
,
1142 scn
->scn_phys
.scn_queue_obj
,
1143 ds
->ds_phys
->ds_creation_txg
, tx
) == 0);
1145 struct enqueue_clones_arg eca
;
1147 eca
.originobj
= ds
->ds_object
;
1149 (void) dmu_objset_find_spa(ds
->ds_dir
->dd_pool
->dp_spa
,
1150 NULL
, enqueue_clones_cb
, &eca
, DS_FIND_CHILDREN
);
1155 dsl_dataset_rele(ds
, FTAG
);
1160 enqueue_cb(spa_t
*spa
, uint64_t dsobj
, const char *dsname
, void *arg
)
1165 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
1166 dsl_scan_t
*scn
= dp
->dp_scan
;
1168 err
= dsl_dataset_hold_obj(dp
, dsobj
, FTAG
, &ds
);
1172 while (ds
->ds_phys
->ds_prev_snap_obj
!= 0) {
1173 dsl_dataset_t
*prev
;
1174 err
= dsl_dataset_hold_obj(dp
, ds
->ds_phys
->ds_prev_snap_obj
,
1177 dsl_dataset_rele(ds
, FTAG
);
1182 * If this is a clone, we don't need to worry about it for now.
1184 if (prev
->ds_phys
->ds_next_snap_obj
!= ds
->ds_object
) {
1185 dsl_dataset_rele(ds
, FTAG
);
1186 dsl_dataset_rele(prev
, FTAG
);
1189 dsl_dataset_rele(ds
, FTAG
);
1193 VERIFY(zap_add_int_key(dp
->dp_meta_objset
, scn
->scn_phys
.scn_queue_obj
,
1194 ds
->ds_object
, ds
->ds_phys
->ds_prev_snap_txg
, tx
) == 0);
1195 dsl_dataset_rele(ds
, FTAG
);
1200 * Scrub/dedup interaction.
1202 * If there are N references to a deduped block, we don't want to scrub it
1203 * N times -- ideally, we should scrub it exactly once.
1205 * We leverage the fact that the dde's replication class (enum ddt_class)
1206 * is ordered from highest replication class (DDT_CLASS_DITTO) to lowest
1207 * (DDT_CLASS_UNIQUE) so that we may walk the DDT in that order.
1209 * To prevent excess scrubbing, the scrub begins by walking the DDT
1210 * to find all blocks with refcnt > 1, and scrubs each of these once.
1211 * Since there are two replication classes which contain blocks with
1212 * refcnt > 1, we scrub the highest replication class (DDT_CLASS_DITTO) first.
1213 * Finally the top-down scrub begins, only visiting blocks with refcnt == 1.
1215 * There would be nothing more to say if a block's refcnt couldn't change
1216 * during a scrub, but of course it can so we must account for changes
1217 * in a block's replication class.
1219 * Here's an example of what can occur:
1221 * If a block has refcnt > 1 during the DDT scrub phase, but has refcnt == 1
1222 * when visited during the top-down scrub phase, it will be scrubbed twice.
1223 * This negates our scrub optimization, but is otherwise harmless.
1225 * If a block has refcnt == 1 during the DDT scrub phase, but has refcnt > 1
1226 * on each visit during the top-down scrub phase, it will never be scrubbed.
1227 * To catch this, ddt_sync_entry() notifies the scrub code whenever a block's
1228 * reference class transitions to a higher level (i.e DDT_CLASS_UNIQUE to
1229 * DDT_CLASS_DUPLICATE); if it transitions from refcnt == 1 to refcnt > 1
1230 * while a scrub is in progress, it scrubs the block right then.
1233 dsl_scan_ddt(dsl_scan_t
*scn
, dmu_tx_t
*tx
)
1235 ddt_bookmark_t
*ddb
= &scn
->scn_phys
.scn_ddt_bookmark
;
1236 ddt_entry_t dde
= { 0 };
1240 while ((error
= ddt_walk(scn
->scn_dp
->dp_spa
, ddb
, &dde
)) == 0) {
1243 if (ddb
->ddb_class
> scn
->scn_phys
.scn_ddt_class_max
)
1245 dprintf("visiting ddb=%llu/%llu/%llu/%llx\n",
1246 (longlong_t
)ddb
->ddb_class
,
1247 (longlong_t
)ddb
->ddb_type
,
1248 (longlong_t
)ddb
->ddb_checksum
,
1249 (longlong_t
)ddb
->ddb_cursor
);
1251 /* There should be no pending changes to the dedup table */
1252 ddt
= scn
->scn_dp
->dp_spa
->spa_ddt
[ddb
->ddb_checksum
];
1253 ASSERT(avl_first(&ddt
->ddt_tree
) == NULL
);
1255 dsl_scan_ddt_entry(scn
, ddb
->ddb_checksum
, &dde
, tx
);
1258 if (dsl_scan_check_pause(scn
, NULL
))
1262 zfs_dbgmsg("scanned %llu ddt entries with class_max = %u; pausing=%u",
1263 (longlong_t
)n
, (int)scn
->scn_phys
.scn_ddt_class_max
,
1264 (int)scn
->scn_pausing
);
1266 ASSERT(error
== 0 || error
== ENOENT
);
1267 ASSERT(error
!= ENOENT
||
1268 ddb
->ddb_class
> scn
->scn_phys
.scn_ddt_class_max
);
1273 dsl_scan_ddt_entry(dsl_scan_t
*scn
, enum zio_checksum checksum
,
1274 ddt_entry_t
*dde
, dmu_tx_t
*tx
)
1276 const ddt_key_t
*ddk
= &dde
->dde_key
;
1277 ddt_phys_t
*ddp
= dde
->dde_phys
;
1279 zbookmark_t zb
= { 0 };
1281 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
1284 for (int p
= 0; p
< DDT_PHYS_TYPES
; p
++, ddp
++) {
1285 if (ddp
->ddp_phys_birth
== 0 ||
1286 ddp
->ddp_phys_birth
> scn
->scn_phys
.scn_cur_max_txg
)
1288 ddt_bp_create(checksum
, ddk
, ddp
, &bp
);
1290 scn
->scn_visited_this_txg
++;
1291 scan_funcs
[scn
->scn_phys
.scn_func
](scn
->scn_dp
, &bp
, &zb
);
1296 dsl_scan_visit(dsl_scan_t
*scn
, dmu_tx_t
*tx
)
1298 dsl_pool_t
*dp
= scn
->scn_dp
;
1302 if (scn
->scn_phys
.scn_ddt_bookmark
.ddb_class
<=
1303 scn
->scn_phys
.scn_ddt_class_max
) {
1304 scn
->scn_phys
.scn_cur_min_txg
= scn
->scn_phys
.scn_min_txg
;
1305 scn
->scn_phys
.scn_cur_max_txg
= scn
->scn_phys
.scn_max_txg
;
1306 dsl_scan_ddt(scn
, tx
);
1307 if (scn
->scn_pausing
)
1311 if (scn
->scn_phys
.scn_bookmark
.zb_objset
== DMU_META_OBJSET
) {
1312 /* First do the MOS & ORIGIN */
1314 scn
->scn_phys
.scn_cur_min_txg
= scn
->scn_phys
.scn_min_txg
;
1315 scn
->scn_phys
.scn_cur_max_txg
= scn
->scn_phys
.scn_max_txg
;
1316 dsl_scan_visit_rootbp(scn
, NULL
,
1317 &dp
->dp_meta_rootbp
, tx
);
1318 spa_set_rootblkptr(dp
->dp_spa
, &dp
->dp_meta_rootbp
);
1319 if (scn
->scn_pausing
)
1322 if (spa_version(dp
->dp_spa
) < SPA_VERSION_DSL_SCRUB
) {
1323 VERIFY(0 == dmu_objset_find_spa(dp
->dp_spa
,
1324 NULL
, enqueue_cb
, tx
, DS_FIND_CHILDREN
));
1326 dsl_scan_visitds(scn
,
1327 dp
->dp_origin_snap
->ds_object
, tx
);
1329 ASSERT(!scn
->scn_pausing
);
1330 } else if (scn
->scn_phys
.scn_bookmark
.zb_objset
!=
1331 ZB_DESTROYED_OBJSET
) {
1333 * If we were paused, continue from here. Note if the
1334 * ds we were paused on was deleted, the zb_objset may
1335 * be -1, so we will skip this and find a new objset
1338 dsl_scan_visitds(scn
, scn
->scn_phys
.scn_bookmark
.zb_objset
, tx
);
1339 if (scn
->scn_pausing
)
1344 * In case we were paused right at the end of the ds, zero the
1345 * bookmark so we don't think that we're still trying to resume.
1347 bzero(&scn
->scn_phys
.scn_bookmark
, sizeof (zbookmark_t
));
1349 /* keep pulling things out of the zap-object-as-queue */
1350 while (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
1351 scn
->scn_phys
.scn_queue_obj
),
1352 zap_cursor_retrieve(&zc
, &za
) == 0) {
1356 dsobj
= strtonum(za
.za_name
, NULL
);
1357 VERIFY3U(0, ==, zap_remove_int(dp
->dp_meta_objset
,
1358 scn
->scn_phys
.scn_queue_obj
, dsobj
, tx
));
1360 /* Set up min/max txg */
1361 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp
, dsobj
, FTAG
, &ds
));
1362 if (za
.za_first_integer
!= 0) {
1363 scn
->scn_phys
.scn_cur_min_txg
=
1364 MAX(scn
->scn_phys
.scn_min_txg
,
1365 za
.za_first_integer
);
1367 scn
->scn_phys
.scn_cur_min_txg
=
1368 MAX(scn
->scn_phys
.scn_min_txg
,
1369 ds
->ds_phys
->ds_prev_snap_txg
);
1371 scn
->scn_phys
.scn_cur_max_txg
= dsl_scan_ds_maxtxg(ds
);
1372 dsl_dataset_rele(ds
, FTAG
);
1374 dsl_scan_visitds(scn
, dsobj
, tx
);
1375 zap_cursor_fini(&zc
);
1376 if (scn
->scn_pausing
)
1379 zap_cursor_fini(&zc
);
1383 dsl_scan_free_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
1385 dsl_scan_t
*scn
= arg
;
1386 uint64_t elapsed_nanosecs
;
1388 elapsed_nanosecs
= gethrtime() - scn
->scn_sync_start_time
;
1390 if (elapsed_nanosecs
/ NANOSEC
> zfs_txg_timeout
||
1391 (elapsed_nanosecs
/ MICROSEC
> zfs_free_min_time_ms
&&
1392 txg_sync_waiting(scn
->scn_dp
)) ||
1393 spa_shutting_down(scn
->scn_dp
->dp_spa
))
1396 zio_nowait(zio_free_sync(scn
->scn_zio_root
, scn
->scn_dp
->dp_spa
,
1397 dmu_tx_get_txg(tx
), bp
, 0));
1398 dsl_dir_diduse_space(tx
->tx_pool
->dp_free_dir
, DD_USED_HEAD
,
1399 -bp_get_dsize_sync(scn
->scn_dp
->dp_spa
, bp
),
1400 -BP_GET_PSIZE(bp
), -BP_GET_UCSIZE(bp
), tx
);
1401 scn
->scn_visited_this_txg
++;
1406 dsl_scan_active(dsl_scan_t
*scn
)
1408 spa_t
*spa
= scn
->scn_dp
->dp_spa
;
1409 uint64_t used
= 0, comp
, uncomp
;
1411 if (spa
->spa_load_state
!= SPA_LOAD_NONE
)
1413 if (spa_shutting_down(spa
))
1416 if (scn
->scn_phys
.scn_state
== DSS_SCANNING
)
1419 if (spa_version(scn
->scn_dp
->dp_spa
) >= SPA_VERSION_DEADLISTS
) {
1420 (void) bpobj_space(&scn
->scn_dp
->dp_free_bpobj
,
1421 &used
, &comp
, &uncomp
);
1427 dsl_scan_sync(dsl_pool_t
*dp
, dmu_tx_t
*tx
)
1429 dsl_scan_t
*scn
= dp
->dp_scan
;
1430 spa_t
*spa
= dp
->dp_spa
;
1434 * Check for scn_restart_txg before checking spa_load_state, so
1435 * that we can restart an old-style scan while the pool is being
1436 * imported (see dsl_scan_init).
1438 if (scn
->scn_restart_txg
!= 0 &&
1439 scn
->scn_restart_txg
<= tx
->tx_txg
) {
1440 pool_scan_func_t func
= POOL_SCAN_SCRUB
;
1441 dsl_scan_done(scn
, B_FALSE
, tx
);
1442 if (vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
))
1443 func
= POOL_SCAN_RESILVER
;
1444 zfs_dbgmsg("restarting scan func=%u txg=%llu",
1446 dsl_scan_setup_sync(scn
, &func
, tx
);
1450 if (!dsl_scan_active(scn
) ||
1451 spa_sync_pass(dp
->dp_spa
) > 1)
1454 scn
->scn_visited_this_txg
= 0;
1455 scn
->scn_pausing
= B_FALSE
;
1456 scn
->scn_sync_start_time
= gethrtime();
1457 spa
->spa_scrub_active
= B_TRUE
;
1460 * First process the free list. If we pause the free, don't do
1461 * any scanning. This ensures that there is no free list when
1462 * we are scanning, so the scan code doesn't have to worry about
1465 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_DEADLISTS
) {
1466 scn
->scn_zio_root
= zio_root(dp
->dp_spa
, NULL
,
1467 NULL
, ZIO_FLAG_MUSTSUCCEED
);
1468 err
= bpobj_iterate(&dp
->dp_free_bpobj
,
1469 dsl_scan_free_cb
, scn
, tx
);
1470 VERIFY3U(0, ==, zio_wait(scn
->scn_zio_root
));
1471 if (scn
->scn_visited_this_txg
) {
1472 zfs_dbgmsg("freed %llu blocks in %llums from "
1473 "free_bpobj txg %llu",
1474 (longlong_t
)scn
->scn_visited_this_txg
,
1476 (gethrtime() - scn
->scn_sync_start_time
) / MICROSEC
,
1477 (longlong_t
)tx
->tx_txg
);
1478 scn
->scn_visited_this_txg
= 0;
1480 * Re-sync the ddt so that we can further modify
1481 * it when doing bprewrite.
1483 ddt_sync(spa
, tx
->tx_txg
);
1485 if (err
== ERESTART
)
1489 if (scn
->scn_phys
.scn_state
!= DSS_SCANNING
)
1493 if (scn
->scn_phys
.scn_ddt_bookmark
.ddb_class
<=
1494 scn
->scn_phys
.scn_ddt_class_max
) {
1495 zfs_dbgmsg("doing scan sync txg %llu; "
1496 "ddt bm=%llu/%llu/%llu/%llx",
1497 (longlong_t
)tx
->tx_txg
,
1498 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_class
,
1499 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_type
,
1500 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_checksum
,
1501 (longlong_t
)scn
->scn_phys
.scn_ddt_bookmark
.ddb_cursor
);
1502 ASSERT(scn
->scn_phys
.scn_bookmark
.zb_objset
== 0);
1503 ASSERT(scn
->scn_phys
.scn_bookmark
.zb_object
== 0);
1504 ASSERT(scn
->scn_phys
.scn_bookmark
.zb_level
== 0);
1505 ASSERT(scn
->scn_phys
.scn_bookmark
.zb_blkid
== 0);
1507 zfs_dbgmsg("doing scan sync txg %llu; bm=%llu/%llu/%llu/%llu",
1508 (longlong_t
)tx
->tx_txg
,
1509 (longlong_t
)scn
->scn_phys
.scn_bookmark
.zb_objset
,
1510 (longlong_t
)scn
->scn_phys
.scn_bookmark
.zb_object
,
1511 (longlong_t
)scn
->scn_phys
.scn_bookmark
.zb_level
,
1512 (longlong_t
)scn
->scn_phys
.scn_bookmark
.zb_blkid
);
1515 scn
->scn_zio_root
= zio_root(dp
->dp_spa
, NULL
,
1516 NULL
, ZIO_FLAG_CANFAIL
);
1517 dsl_scan_visit(scn
, tx
);
1518 (void) zio_wait(scn
->scn_zio_root
);
1519 scn
->scn_zio_root
= NULL
;
1521 zfs_dbgmsg("visited %llu blocks in %llums",
1522 (longlong_t
)scn
->scn_visited_this_txg
,
1523 (longlong_t
)(gethrtime() - scn
->scn_sync_start_time
) / MICROSEC
);
1525 if (!scn
->scn_pausing
) {
1526 /* finished with scan. */
1527 zfs_dbgmsg("finished scan txg %llu", (longlong_t
)tx
->tx_txg
);
1528 dsl_scan_done(scn
, B_TRUE
, tx
);
1531 if (DSL_SCAN_IS_SCRUB_RESILVER(scn
)) {
1532 mutex_enter(&spa
->spa_scrub_lock
);
1533 while (spa
->spa_scrub_inflight
> 0) {
1534 cv_wait(&spa
->spa_scrub_io_cv
,
1535 &spa
->spa_scrub_lock
);
1537 mutex_exit(&spa
->spa_scrub_lock
);
1540 dsl_scan_sync_state(scn
, tx
);
1544 * This will start a new scan, or restart an existing one.
1547 dsl_resilver_restart(dsl_pool_t
*dp
, uint64_t txg
)
1551 tx
= dmu_tx_create_dd(dp
->dp_mos_dir
);
1552 VERIFY(0 == dmu_tx_assign(tx
, TXG_WAIT
));
1554 txg
= dmu_tx_get_txg(tx
);
1555 dp
->dp_scan
->scn_restart_txg
= txg
;
1558 dp
->dp_scan
->scn_restart_txg
= txg
;
1560 zfs_dbgmsg("restarting resilver txg=%llu", txg
);
1564 dsl_scan_resilvering(dsl_pool_t
*dp
)
1566 return (dp
->dp_scan
->scn_phys
.scn_state
== DSS_SCANNING
&&
1567 dp
->dp_scan
->scn_phys
.scn_func
== POOL_SCAN_RESILVER
);
1575 count_block(zfs_all_blkstats_t
*zab
, const blkptr_t
*bp
)
1580 * If we resume after a reboot, zab will be NULL; don't record
1581 * incomplete stats in that case.
1586 for (i
= 0; i
< 4; i
++) {
1587 int l
= (i
< 2) ? BP_GET_LEVEL(bp
) : DN_MAX_LEVELS
;
1588 int t
= (i
& 1) ? BP_GET_TYPE(bp
) : DMU_OT_TOTAL
;
1589 zfs_blkstat_t
*zb
= &zab
->zab_type
[l
][t
];
1593 zb
->zb_asize
+= BP_GET_ASIZE(bp
);
1594 zb
->zb_lsize
+= BP_GET_LSIZE(bp
);
1595 zb
->zb_psize
+= BP_GET_PSIZE(bp
);
1596 zb
->zb_gangs
+= BP_COUNT_GANG(bp
);
1598 switch (BP_GET_NDVAS(bp
)) {
1600 if (DVA_GET_VDEV(&bp
->blk_dva
[0]) ==
1601 DVA_GET_VDEV(&bp
->blk_dva
[1]))
1602 zb
->zb_ditto_2_of_2_samevdev
++;
1605 equal
= (DVA_GET_VDEV(&bp
->blk_dva
[0]) ==
1606 DVA_GET_VDEV(&bp
->blk_dva
[1])) +
1607 (DVA_GET_VDEV(&bp
->blk_dva
[0]) ==
1608 DVA_GET_VDEV(&bp
->blk_dva
[2])) +
1609 (DVA_GET_VDEV(&bp
->blk_dva
[1]) ==
1610 DVA_GET_VDEV(&bp
->blk_dva
[2]));
1612 zb
->zb_ditto_2_of_3_samevdev
++;
1613 else if (equal
== 3)
1614 zb
->zb_ditto_3_of_3_samevdev
++;
1621 dsl_scan_scrub_done(zio_t
*zio
)
1623 spa_t
*spa
= zio
->io_spa
;
1625 zio_data_buf_free(zio
->io_data
, zio
->io_size
);
1627 mutex_enter(&spa
->spa_scrub_lock
);
1628 spa
->spa_scrub_inflight
--;
1629 cv_broadcast(&spa
->spa_scrub_io_cv
);
1631 if (zio
->io_error
&& (zio
->io_error
!= ECKSUM
||
1632 !(zio
->io_flags
& ZIO_FLAG_SPECULATIVE
))) {
1633 spa
->spa_dsl_pool
->dp_scan
->scn_phys
.scn_errors
++;
1635 mutex_exit(&spa
->spa_scrub_lock
);
1639 dsl_scan_scrub_cb(dsl_pool_t
*dp
,
1640 const blkptr_t
*bp
, const zbookmark_t
*zb
)
1642 dsl_scan_t
*scn
= dp
->dp_scan
;
1643 size_t size
= BP_GET_PSIZE(bp
);
1644 spa_t
*spa
= dp
->dp_spa
;
1645 uint64_t phys_birth
= BP_PHYSICAL_BIRTH(bp
);
1647 int zio_flags
= ZIO_FLAG_SCRUB_THREAD
| ZIO_FLAG_RAW
| ZIO_FLAG_CANFAIL
;
1650 if (phys_birth
<= scn
->scn_phys
.scn_min_txg
||
1651 phys_birth
>= scn
->scn_phys
.scn_max_txg
)
1654 count_block(dp
->dp_blkstats
, bp
);
1656 ASSERT(DSL_SCAN_IS_SCRUB_RESILVER(scn
));
1657 if (scn
->scn_phys
.scn_func
== POOL_SCAN_SCRUB
) {
1658 zio_flags
|= ZIO_FLAG_SCRUB
;
1659 zio_priority
= ZIO_PRIORITY_SCRUB
;
1661 } else if (scn
->scn_phys
.scn_func
== POOL_SCAN_RESILVER
) {
1662 zio_flags
|= ZIO_FLAG_RESILVER
;
1663 zio_priority
= ZIO_PRIORITY_RESILVER
;
1667 /* If it's an intent log block, failure is expected. */
1668 if (zb
->zb_level
== ZB_ZIL_LEVEL
)
1669 zio_flags
|= ZIO_FLAG_SPECULATIVE
;
1671 for (int d
= 0; d
< BP_GET_NDVAS(bp
); d
++) {
1672 vdev_t
*vd
= vdev_lookup_top(spa
,
1673 DVA_GET_VDEV(&bp
->blk_dva
[d
]));
1676 * Keep track of how much data we've examined so that
1677 * zpool(1M) status can make useful progress reports.
1679 scn
->scn_phys
.scn_examined
+= DVA_GET_ASIZE(&bp
->blk_dva
[d
]);
1680 spa
->spa_scan_pass_exam
+= DVA_GET_ASIZE(&bp
->blk_dva
[d
]);
1682 /* if it's a resilver, this may not be in the target range */
1684 if (DVA_GET_GANG(&bp
->blk_dva
[d
])) {
1686 * Gang members may be spread across multiple
1687 * vdevs, so the best estimate we have is the
1688 * scrub range, which has already been checked.
1689 * XXX -- it would be better to change our
1690 * allocation policy to ensure that all
1691 * gang members reside on the same vdev.
1695 needs_io
= vdev_dtl_contains(vd
, DTL_PARTIAL
,
1701 if (needs_io
&& !zfs_no_scrub_io
) {
1702 void *data
= zio_data_buf_alloc(size
);
1704 mutex_enter(&spa
->spa_scrub_lock
);
1705 while (spa
->spa_scrub_inflight
>= spa
->spa_scrub_maxinflight
)
1706 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
1707 spa
->spa_scrub_inflight
++;
1708 mutex_exit(&spa
->spa_scrub_lock
);
1710 zio_nowait(zio_read(NULL
, spa
, bp
, data
, size
,
1711 dsl_scan_scrub_done
, NULL
, zio_priority
,
1715 /* do not relocate this block */
1720 dsl_scan(dsl_pool_t
*dp
, pool_scan_func_t func
)
1722 spa_t
*spa
= dp
->dp_spa
;
1725 * Purge all vdev caches and probe all devices. We do this here
1726 * rather than in sync context because this requires a writer lock
1727 * on the spa_config lock, which we can't do from sync context. The
1728 * spa_scrub_reopen flag indicates that vdev_open() should not
1729 * attempt to start another scrub.
1731 spa_vdev_state_enter(spa
, SCL_NONE
);
1732 spa
->spa_scrub_reopen
= B_TRUE
;
1733 vdev_reopen(spa
->spa_root_vdev
);
1734 spa
->spa_scrub_reopen
= B_FALSE
;
1735 (void) spa_vdev_state_exit(spa
, NULL
, 0);
1737 return (dsl_sync_task_do(dp
, dsl_scan_setup_check
,
1738 dsl_scan_setup_sync
, dp
->dp_scan
, &func
, 0));