]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/dsl_scan.c
Illumos 4881 - zfs send performance regression with embedded data
[mirror_zfs.git] / module / zfs / dsl_scan.c
CommitLineData
428870ff
BB
1/*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
2e528b49 23 * Copyright (c) 2013 by Delphix. All rights reserved.
428870ff
BB
24 */
25
26#include <sys/dsl_scan.h>
27#include <sys/dsl_pool.h>
28#include <sys/dsl_dataset.h>
29#include <sys/dsl_prop.h>
30#include <sys/dsl_dir.h>
31#include <sys/dsl_synctask.h>
32#include <sys/dnode.h>
33#include <sys/dmu_tx.h>
34#include <sys/dmu_objset.h>
35#include <sys/arc.h>
36#include <sys/zap.h>
37#include <sys/zio.h>
38#include <sys/zfs_context.h>
39#include <sys/fs/zfs.h>
40#include <sys/zfs_znode.h>
41#include <sys/spa_impl.h>
42#include <sys/vdev_impl.h>
43#include <sys/zil_impl.h>
44#include <sys/zio_checksum.h>
45#include <sys/ddt.h>
46#include <sys/sa.h>
47#include <sys/sa_impl.h>
9ae529ec 48#include <sys/zfeature.h>
428870ff
BB
49#ifdef _KERNEL
50#include <sys/zfs_vfsops.h>
51#endif
52
53typedef int (scan_cb_t)(dsl_pool_t *, const blkptr_t *, const zbookmark_t *);
54
428870ff 55static scan_cb_t dsl_scan_scrub_cb;
13fe0198 56static void dsl_scan_cancel_sync(void *, dmu_tx_t *);
428870ff
BB
57static void dsl_scan_sync_state(dsl_scan_t *, dmu_tx_t *tx);
58
572e2857
BB
59int zfs_top_maxinflight = 32; /* maximum I/Os per top-level */
60int zfs_resilver_delay = 2; /* number of ticks to delay resilver */
61int zfs_scrub_delay = 4; /* number of ticks to delay scrub */
62int zfs_scan_idle = 50; /* idle window in clock ticks */
63
428870ff
BB
64int zfs_scan_min_time_ms = 1000; /* min millisecs to scrub per txg */
65int zfs_free_min_time_ms = 1000; /* min millisecs to free per txg */
66int zfs_resilver_min_time_ms = 3000; /* min millisecs to resilver per txg */
c409e464 67int zfs_no_scrub_io = B_FALSE; /* set to disable scrub i/o */
fbeddd60 68int zfs_no_scrub_prefetch = B_FALSE; /* set to disable scrub prefetch */
428870ff
BB
69enum ddt_class zfs_scrub_ddt_class_max = DDT_CLASS_DUPLICATE;
70int dsl_scan_delay_completion = B_FALSE; /* set to delay scan completion */
71
72#define DSL_SCAN_IS_SCRUB_RESILVER(scn) \
73 ((scn)->scn_phys.scn_func == POOL_SCAN_SCRUB || \
74 (scn)->scn_phys.scn_func == POOL_SCAN_RESILVER)
75
428870ff
BB
76/* the order has to match pool_scan_type */
77static scan_cb_t *scan_funcs[POOL_SCAN_FUNCS] = {
78 NULL,
79 dsl_scan_scrub_cb, /* POOL_SCAN_SCRUB */
80 dsl_scan_scrub_cb, /* POOL_SCAN_RESILVER */
81};
82
83int
84dsl_scan_init(dsl_pool_t *dp, uint64_t txg)
85{
86 int err;
87 dsl_scan_t *scn;
88 spa_t *spa = dp->dp_spa;
89 uint64_t f;
90
91 scn = dp->dp_scan = kmem_zalloc(sizeof (dsl_scan_t), KM_SLEEP);
92 scn->scn_dp = dp;
93
2696dfaf
GW
94 /*
95 * It's possible that we're resuming a scan after a reboot so
96 * make sure that the scan_async_destroying flag is initialized
97 * appropriately.
98 */
99 ASSERT(!scn->scn_async_destroying);
100 scn->scn_async_destroying = spa_feature_is_active(dp->dp_spa,
fa86b5db 101 SPA_FEATURE_ASYNC_DESTROY);
2696dfaf 102
428870ff
BB
103 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
104 "scrub_func", sizeof (uint64_t), 1, &f);
105 if (err == 0) {
106 /*
107 * There was an old-style scrub in progress. Restart a
108 * new-style scrub from the beginning.
109 */
110 scn->scn_restart_txg = txg;
111 zfs_dbgmsg("old-style scrub was in progress; "
112 "restarting new-style scrub in txg %llu",
113 scn->scn_restart_txg);
114
115 /*
116 * Load the queue obj from the old location so that it
117 * can be freed by dsl_scan_done().
118 */
119 (void) zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
120 "scrub_queue", sizeof (uint64_t), 1,
121 &scn->scn_phys.scn_queue_obj);
122 } else {
123 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
124 DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS,
125 &scn->scn_phys);
4f2dcb3e
RY
126 /*
127 * Detect if the pool contains the signature of #2094. If it
128 * does properly update the scn->scn_phys structure and notify
129 * the administrator by setting an errata for the pool.
130 */
131 if (err == EOVERFLOW) {
132 uint64_t zaptmp[SCAN_PHYS_NUMINTS + 1];
133 VERIFY3S(SCAN_PHYS_NUMINTS, ==, 24);
134 VERIFY3S(offsetof(dsl_scan_phys_t, scn_flags), ==,
135 (23 * sizeof (uint64_t)));
136
137 err = zap_lookup(dp->dp_meta_objset,
138 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SCAN,
139 sizeof (uint64_t), SCAN_PHYS_NUMINTS + 1, &zaptmp);
140 if (err == 0) {
141 uint64_t overflow = zaptmp[SCAN_PHYS_NUMINTS];
142
143 if (overflow & ~DSL_SCAN_FLAGS_MASK ||
144 scn->scn_async_destroying) {
145 spa->spa_errata =
146 ZPOOL_ERRATA_ZOL_2094_ASYNC_DESTROY;
147 return (EOVERFLOW);
148 }
149
150 bcopy(zaptmp, &scn->scn_phys,
151 SCAN_PHYS_NUMINTS * sizeof (uint64_t));
152 scn->scn_phys.scn_flags = overflow;
153
154 /* Required scrub already in progress. */
155 if (scn->scn_phys.scn_state == DSS_FINISHED ||
156 scn->scn_phys.scn_state == DSS_CANCELED)
157 spa->spa_errata =
158 ZPOOL_ERRATA_ZOL_2094_SCRUB;
159 }
160 }
161
428870ff
BB
162 if (err == ENOENT)
163 return (0);
164 else if (err)
165 return (err);
166
167 if (scn->scn_phys.scn_state == DSS_SCANNING &&
168 spa_prev_software_version(dp->dp_spa) < SPA_VERSION_SCAN) {
169 /*
170 * A new-type scrub was in progress on an old
171 * pool, and the pool was accessed by old
172 * software. Restart from the beginning, since
173 * the old software may have changed the pool in
174 * the meantime.
175 */
176 scn->scn_restart_txg = txg;
177 zfs_dbgmsg("new-style scrub was modified "
178 "by old software; restarting in txg %llu",
179 scn->scn_restart_txg);
180 }
181 }
182
183 spa_scan_stat_init(spa);
184 return (0);
185}
186
187void
188dsl_scan_fini(dsl_pool_t *dp)
189{
190 if (dp->dp_scan) {
191 kmem_free(dp->dp_scan, sizeof (dsl_scan_t));
192 dp->dp_scan = NULL;
193 }
194}
195
196/* ARGSUSED */
197static int
13fe0198 198dsl_scan_setup_check(void *arg, dmu_tx_t *tx)
428870ff 199{
13fe0198 200 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
428870ff
BB
201
202 if (scn->scn_phys.scn_state == DSS_SCANNING)
2e528b49 203 return (SET_ERROR(EBUSY));
428870ff
BB
204
205 return (0);
206}
207
428870ff 208static void
13fe0198 209dsl_scan_setup_sync(void *arg, dmu_tx_t *tx)
428870ff 210{
13fe0198
MA
211 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
212 pool_scan_func_t *funcp = arg;
428870ff
BB
213 dmu_object_type_t ot = 0;
214 dsl_pool_t *dp = scn->scn_dp;
215 spa_t *spa = dp->dp_spa;
216
217 ASSERT(scn->scn_phys.scn_state != DSS_SCANNING);
218 ASSERT(*funcp > POOL_SCAN_NONE && *funcp < POOL_SCAN_FUNCS);
219 bzero(&scn->scn_phys, sizeof (scn->scn_phys));
220 scn->scn_phys.scn_func = *funcp;
221 scn->scn_phys.scn_state = DSS_SCANNING;
222 scn->scn_phys.scn_min_txg = 0;
223 scn->scn_phys.scn_max_txg = tx->tx_txg;
224 scn->scn_phys.scn_ddt_class_max = DDT_CLASSES - 1; /* the entire DDT */
225 scn->scn_phys.scn_start_time = gethrestime_sec();
226 scn->scn_phys.scn_errors = 0;
227 scn->scn_phys.scn_to_examine = spa->spa_root_vdev->vdev_stat.vs_alloc;
228 scn->scn_restart_txg = 0;
5d1f7fb6 229 scn->scn_done_txg = 0;
428870ff
BB
230 spa_scan_stat_init(spa);
231
232 if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
233 scn->scn_phys.scn_ddt_class_max = zfs_scrub_ddt_class_max;
234
235 /* rewrite all disk labels */
236 vdev_config_dirty(spa->spa_root_vdev);
237
238 if (vdev_resilver_needed(spa->spa_root_vdev,
239 &scn->scn_phys.scn_min_txg, &scn->scn_phys.scn_max_txg)) {
d1d7e268
MK
240 spa_event_notify(spa, NULL,
241 FM_EREPORT_ZFS_RESILVER_START);
428870ff 242 } else {
d1d7e268
MK
243 spa_event_notify(spa, NULL,
244 FM_EREPORT_ZFS_SCRUB_START);
428870ff
BB
245 }
246
247 spa->spa_scrub_started = B_TRUE;
248 /*
249 * If this is an incremental scrub, limit the DDT scrub phase
250 * to just the auto-ditto class (for correctness); the rest
251 * of the scrub should go faster using top-down pruning.
252 */
253 if (scn->scn_phys.scn_min_txg > TXG_INITIAL)
254 scn->scn_phys.scn_ddt_class_max = DDT_CLASS_DITTO;
255
256 }
257
258 /* back to the generic stuff */
259
260 if (dp->dp_blkstats == NULL) {
398f129c 261 dp->dp_blkstats = kmem_alloc(sizeof (zfs_all_blkstats_t),
20a083cb 262 KM_PUSHPAGE | KM_NODEBUG);
428870ff
BB
263 }
264 bzero(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
265
266 if (spa_version(spa) < SPA_VERSION_DSL_SCRUB)
267 ot = DMU_OT_ZAP_OTHER;
268
269 scn->scn_phys.scn_queue_obj = zap_create(dp->dp_meta_objset,
270 ot ? ot : DMU_OT_SCAN_QUEUE, DMU_OT_NONE, 0, tx);
271
272 dsl_scan_sync_state(scn, tx);
273
6f1ffb06 274 spa_history_log_internal(spa, "scan setup", tx,
428870ff
BB
275 "func=%u mintxg=%llu maxtxg=%llu",
276 *funcp, scn->scn_phys.scn_min_txg, scn->scn_phys.scn_max_txg);
277}
278
279/* ARGSUSED */
280static void
281dsl_scan_done(dsl_scan_t *scn, boolean_t complete, dmu_tx_t *tx)
282{
283 static const char *old_names[] = {
284 "scrub_bookmark",
285 "scrub_ddt_bookmark",
286 "scrub_ddt_class_max",
287 "scrub_queue",
288 "scrub_min_txg",
289 "scrub_max_txg",
290 "scrub_func",
291 "scrub_errors",
292 NULL
293 };
294
295 dsl_pool_t *dp = scn->scn_dp;
296 spa_t *spa = dp->dp_spa;
297 int i;
298
299 /* Remove any remnants of an old-style scrub. */
300 for (i = 0; old_names[i]; i++) {
301 (void) zap_remove(dp->dp_meta_objset,
302 DMU_POOL_DIRECTORY_OBJECT, old_names[i], tx);
303 }
304
305 if (scn->scn_phys.scn_queue_obj != 0) {
306 VERIFY(0 == dmu_object_free(dp->dp_meta_objset,
307 scn->scn_phys.scn_queue_obj, tx));
308 scn->scn_phys.scn_queue_obj = 0;
309 }
310
311 /*
312 * If we were "restarted" from a stopped state, don't bother
313 * with anything else.
314 */
315 if (scn->scn_phys.scn_state != DSS_SCANNING)
316 return;
317
318 if (complete)
319 scn->scn_phys.scn_state = DSS_FINISHED;
320 else
321 scn->scn_phys.scn_state = DSS_CANCELED;
322
6f1ffb06 323 spa_history_log_internal(spa, "scan done", tx,
428870ff
BB
324 "complete=%u", complete);
325
326 if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
327 mutex_enter(&spa->spa_scrub_lock);
328 while (spa->spa_scrub_inflight > 0) {
329 cv_wait(&spa->spa_scrub_io_cv,
330 &spa->spa_scrub_lock);
331 }
332 mutex_exit(&spa->spa_scrub_lock);
333 spa->spa_scrub_started = B_FALSE;
334 spa->spa_scrub_active = B_FALSE;
335
336 /*
337 * If the scrub/resilver completed, update all DTLs to
338 * reflect this. Whether it succeeded or not, vacate
339 * all temporary scrub DTLs.
340 */
341 vdev_dtl_reassess(spa->spa_root_vdev, tx->tx_txg,
342 complete ? scn->scn_phys.scn_max_txg : 0, B_TRUE);
343 if (complete) {
344 spa_event_notify(spa, NULL, scn->scn_phys.scn_min_txg ?
26685276
BB
345 FM_EREPORT_ZFS_RESILVER_FINISH :
346 FM_EREPORT_ZFS_SCRUB_FINISH);
428870ff
BB
347 }
348 spa_errlog_rotate(spa);
349
350 /*
351 * We may have finished replacing a device.
352 * Let the async thread assess this and handle the detach.
353 */
354 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
355 }
356
357 scn->scn_phys.scn_end_time = gethrestime_sec();
4f2dcb3e
RY
358
359 if (spa->spa_errata == ZPOOL_ERRATA_ZOL_2094_SCRUB)
360 spa->spa_errata = 0;
428870ff
BB
361}
362
363/* ARGSUSED */
364static int
13fe0198 365dsl_scan_cancel_check(void *arg, dmu_tx_t *tx)
428870ff 366{
13fe0198 367 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
428870ff
BB
368
369 if (scn->scn_phys.scn_state != DSS_SCANNING)
2e528b49 370 return (SET_ERROR(ENOENT));
428870ff
BB
371 return (0);
372}
373
374/* ARGSUSED */
375static void
13fe0198 376dsl_scan_cancel_sync(void *arg, dmu_tx_t *tx)
428870ff 377{
13fe0198 378 dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
428870ff
BB
379
380 dsl_scan_done(scn, B_FALSE, tx);
381 dsl_scan_sync_state(scn, tx);
382}
383
384int
385dsl_scan_cancel(dsl_pool_t *dp)
386{
13fe0198
MA
387 return (dsl_sync_task(spa_name(dp->dp_spa), dsl_scan_cancel_check,
388 dsl_scan_cancel_sync, NULL, 3));
428870ff
BB
389}
390
391static void dsl_scan_visitbp(blkptr_t *bp,
392 const zbookmark_t *zb, dnode_phys_t *dnp, arc_buf_t *pbuf,
393 dsl_dataset_t *ds, dsl_scan_t *scn, dmu_objset_type_t ostype,
394 dmu_tx_t *tx);
10be533e
BB
395inline __attribute__((always_inline)) static void dsl_scan_visitdnode(
396 dsl_scan_t *, dsl_dataset_t *ds, dmu_objset_type_t ostype,
428870ff
BB
397 dnode_phys_t *dnp, arc_buf_t *buf, uint64_t object, dmu_tx_t *tx);
398
399void
400dsl_free(dsl_pool_t *dp, uint64_t txg, const blkptr_t *bp)
401{
402 zio_free(dp->dp_spa, txg, bp);
403}
404
405void
406dsl_free_sync(zio_t *pio, dsl_pool_t *dp, uint64_t txg, const blkptr_t *bpp)
407{
408 ASSERT(dsl_pool_sync_context(dp));
409 zio_nowait(zio_free_sync(pio, dp->dp_spa, txg, bpp, pio->io_flags));
410}
411
428870ff
BB
412static uint64_t
413dsl_scan_ds_maxtxg(dsl_dataset_t *ds)
414{
415 uint64_t smt = ds->ds_dir->dd_pool->dp_scan->scn_phys.scn_max_txg;
416 if (dsl_dataset_is_snapshot(ds))
417 return (MIN(smt, ds->ds_phys->ds_creation_txg));
418 return (smt);
419}
420
421static void
422dsl_scan_sync_state(dsl_scan_t *scn, dmu_tx_t *tx)
423{
13fe0198 424 VERIFY0(zap_update(scn->scn_dp->dp_meta_objset,
428870ff
BB
425 DMU_POOL_DIRECTORY_OBJECT,
426 DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS,
427 &scn->scn_phys, tx));
428}
429
430static boolean_t
431dsl_scan_check_pause(dsl_scan_t *scn, const zbookmark_t *zb)
432{
433 uint64_t elapsed_nanosecs;
434 int mintime;
435
436 /* we never skip user/group accounting objects */
437 if (zb && (int64_t)zb->zb_object < 0)
438 return (B_FALSE);
439
440 if (scn->scn_pausing)
441 return (B_TRUE); /* we're already pausing */
442
9ae529ec 443 if (!ZB_IS_ZERO(&scn->scn_phys.scn_bookmark))
428870ff
BB
444 return (B_FALSE); /* we're resuming */
445
446 /* We only know how to resume from level-0 blocks. */
447 if (zb && zb->zb_level != 0)
448 return (B_FALSE);
449
450 mintime = (scn->scn_phys.scn_func == POOL_SCAN_RESILVER) ?
451 zfs_resilver_min_time_ms : zfs_scan_min_time_ms;
452 elapsed_nanosecs = gethrtime() - scn->scn_sync_start_time;
453 if (elapsed_nanosecs / NANOSEC > zfs_txg_timeout ||
63fd3c6c 454 (NSEC2MSEC(elapsed_nanosecs) > mintime &&
428870ff
BB
455 txg_sync_waiting(scn->scn_dp)) ||
456 spa_shutting_down(scn->scn_dp->dp_spa)) {
457 if (zb) {
458 dprintf("pausing at bookmark %llx/%llx/%llx/%llx\n",
459 (longlong_t)zb->zb_objset,
460 (longlong_t)zb->zb_object,
461 (longlong_t)zb->zb_level,
462 (longlong_t)zb->zb_blkid);
463 scn->scn_phys.scn_bookmark = *zb;
464 }
465 dprintf("pausing at DDT bookmark %llx/%llx/%llx/%llx\n",
466 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_class,
467 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_type,
468 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_checksum,
469 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_cursor);
470 scn->scn_pausing = B_TRUE;
471 return (B_TRUE);
472 }
473 return (B_FALSE);
474}
475
476typedef struct zil_scan_arg {
477 dsl_pool_t *zsa_dp;
478 zil_header_t *zsa_zh;
479} zil_scan_arg_t;
480
481/* ARGSUSED */
482static int
483dsl_scan_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
484{
485 zil_scan_arg_t *zsa = arg;
486 dsl_pool_t *dp = zsa->zsa_dp;
487 dsl_scan_t *scn = dp->dp_scan;
488 zil_header_t *zh = zsa->zsa_zh;
489 zbookmark_t zb;
490
b0bc7a84 491 if (BP_IS_HOLE(bp) || bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
428870ff
BB
492 return (0);
493
494 /*
495 * One block ("stubby") can be allocated a long time ago; we
496 * want to visit that one because it has been allocated
497 * (on-disk) even if it hasn't been claimed (even though for
498 * scrub there's nothing to do to it).
499 */
500 if (claim_txg == 0 && bp->blk_birth >= spa_first_txg(dp->dp_spa))
501 return (0);
502
503 SET_BOOKMARK(&zb, zh->zh_log.blk_cksum.zc_word[ZIL_ZC_OBJSET],
504 ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
505
506 VERIFY(0 == scan_funcs[scn->scn_phys.scn_func](dp, bp, &zb));
507 return (0);
508}
509
510/* ARGSUSED */
511static int
512dsl_scan_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg)
513{
514 if (lrc->lrc_txtype == TX_WRITE) {
515 zil_scan_arg_t *zsa = arg;
516 dsl_pool_t *dp = zsa->zsa_dp;
517 dsl_scan_t *scn = dp->dp_scan;
518 zil_header_t *zh = zsa->zsa_zh;
519 lr_write_t *lr = (lr_write_t *)lrc;
520 blkptr_t *bp = &lr->lr_blkptr;
521 zbookmark_t zb;
522
b0bc7a84
MG
523 if (BP_IS_HOLE(bp) ||
524 bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
428870ff
BB
525 return (0);
526
527 /*
528 * birth can be < claim_txg if this record's txg is
529 * already txg sync'ed (but this log block contains
530 * other records that are not synced)
531 */
532 if (claim_txg == 0 || bp->blk_birth < claim_txg)
533 return (0);
534
535 SET_BOOKMARK(&zb, zh->zh_log.blk_cksum.zc_word[ZIL_ZC_OBJSET],
536 lr->lr_foid, ZB_ZIL_LEVEL,
537 lr->lr_offset / BP_GET_LSIZE(bp));
538
539 VERIFY(0 == scan_funcs[scn->scn_phys.scn_func](dp, bp, &zb));
540 }
541 return (0);
542}
543
544static void
545dsl_scan_zil(dsl_pool_t *dp, zil_header_t *zh)
546{
547 uint64_t claim_txg = zh->zh_claim_txg;
548 zil_scan_arg_t zsa = { dp, zh };
549 zilog_t *zilog;
550
551 /*
552 * We only want to visit blocks that have been claimed but not yet
553 * replayed (or, in read-only mode, blocks that *would* be claimed).
554 */
555 if (claim_txg == 0 && spa_writeable(dp->dp_spa))
556 return;
557
558 zilog = zil_alloc(dp->dp_meta_objset, zh);
559
560 (void) zil_parse(zilog, dsl_scan_zil_block, dsl_scan_zil_record, &zsa,
561 claim_txg);
562
563 zil_free(zilog);
564}
565
566/* ARGSUSED */
567static void
568dsl_scan_prefetch(dsl_scan_t *scn, arc_buf_t *buf, blkptr_t *bp,
569 uint64_t objset, uint64_t object, uint64_t blkid)
570{
571 zbookmark_t czb;
572 uint32_t flags = ARC_NOWAIT | ARC_PREFETCH;
573
574 if (zfs_no_scrub_prefetch)
575 return;
576
577 if (BP_IS_HOLE(bp) || bp->blk_birth <= scn->scn_phys.scn_min_txg ||
578 (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_DNODE))
579 return;
580
581 SET_BOOKMARK(&czb, objset, object, BP_GET_LEVEL(bp), blkid);
582
428870ff 583 (void) arc_read(scn->scn_zio_root, scn->scn_dp->dp_spa, bp,
294f6806 584 NULL, NULL, ZIO_PRIORITY_ASYNC_READ,
572e2857 585 ZIO_FLAG_CANFAIL | ZIO_FLAG_SCAN_THREAD, &flags, &czb);
428870ff
BB
586}
587
588static boolean_t
589dsl_scan_check_resume(dsl_scan_t *scn, const dnode_phys_t *dnp,
590 const zbookmark_t *zb)
591{
592 /*
593 * We never skip over user/group accounting objects (obj<0)
594 */
9ae529ec 595 if (!ZB_IS_ZERO(&scn->scn_phys.scn_bookmark) &&
428870ff
BB
596 (int64_t)zb->zb_object >= 0) {
597 /*
598 * If we already visited this bp & everything below (in
599 * a prior txg sync), don't bother doing it again.
600 */
9ae529ec 601 if (zbookmark_is_before(dnp, zb, &scn->scn_phys.scn_bookmark))
428870ff
BB
602 return (B_TRUE);
603
604 /*
605 * If we found the block we're trying to resume from, or
606 * we went past it to a different object, zero it out to
607 * indicate that it's OK to start checking for pausing
608 * again.
609 */
610 if (bcmp(zb, &scn->scn_phys.scn_bookmark, sizeof (*zb)) == 0 ||
611 zb->zb_object > scn->scn_phys.scn_bookmark.zb_object) {
612 dprintf("resuming at %llx/%llx/%llx/%llx\n",
613 (longlong_t)zb->zb_objset,
614 (longlong_t)zb->zb_object,
615 (longlong_t)zb->zb_level,
616 (longlong_t)zb->zb_blkid);
617 bzero(&scn->scn_phys.scn_bookmark, sizeof (*zb));
618 }
619 }
620 return (B_FALSE);
621}
622
623/*
624 * Return nonzero on i/o error.
625 * Return new buf to write out in *bufp.
626 */
10be533e 627inline __attribute__((always_inline)) static int
428870ff
BB
628dsl_scan_recurse(dsl_scan_t *scn, dsl_dataset_t *ds, dmu_objset_type_t ostype,
629 dnode_phys_t *dnp, const blkptr_t *bp,
630 const zbookmark_t *zb, dmu_tx_t *tx, arc_buf_t **bufp)
631{
632 dsl_pool_t *dp = scn->scn_dp;
572e2857 633 int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCAN_THREAD;
428870ff
BB
634 int err;
635
636 if (BP_GET_LEVEL(bp) > 0) {
637 uint32_t flags = ARC_WAIT;
638 int i;
639 blkptr_t *cbp;
640 int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
641
294f6806 642 err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, bufp,
572e2857 643 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
428870ff
BB
644 if (err) {
645 scn->scn_phys.scn_errors++;
646 return (err);
647 }
648 for (i = 0, cbp = (*bufp)->b_data; i < epb; i++, cbp++) {
649 dsl_scan_prefetch(scn, *bufp, cbp, zb->zb_objset,
650 zb->zb_object, zb->zb_blkid * epb + i);
651 }
652 for (i = 0, cbp = (*bufp)->b_data; i < epb; i++, cbp++) {
653 zbookmark_t czb;
654
655 SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
656 zb->zb_level - 1,
657 zb->zb_blkid * epb + i);
658 dsl_scan_visitbp(cbp, &czb, dnp,
659 *bufp, ds, scn, ostype, tx);
660 }
661 } else if (BP_GET_TYPE(bp) == DMU_OT_USERGROUP_USED) {
662 uint32_t flags = ARC_WAIT;
663
294f6806 664 err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, bufp,
572e2857 665 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
428870ff
BB
666 if (err) {
667 scn->scn_phys.scn_errors++;
668 return (err);
669 }
670 } else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) {
671 uint32_t flags = ARC_WAIT;
672 dnode_phys_t *cdnp;
673 int i, j;
674 int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
675
294f6806 676 err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, bufp,
572e2857 677 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
428870ff
BB
678 if (err) {
679 scn->scn_phys.scn_errors++;
680 return (err);
681 }
682 for (i = 0, cdnp = (*bufp)->b_data; i < epb; i++, cdnp++) {
683 for (j = 0; j < cdnp->dn_nblkptr; j++) {
684 blkptr_t *cbp = &cdnp->dn_blkptr[j];
685 dsl_scan_prefetch(scn, *bufp, cbp,
686 zb->zb_objset, zb->zb_blkid * epb + i, j);
687 }
688 }
689 for (i = 0, cdnp = (*bufp)->b_data; i < epb; i++, cdnp++) {
690 dsl_scan_visitdnode(scn, ds, ostype,
691 cdnp, *bufp, zb->zb_blkid * epb + i, tx);
692 }
693
694 } else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
695 uint32_t flags = ARC_WAIT;
696 objset_phys_t *osp;
697
294f6806 698 err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, bufp,
572e2857 699 ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
428870ff
BB
700 if (err) {
701 scn->scn_phys.scn_errors++;
702 return (err);
703 }
704
705 osp = (*bufp)->b_data;
706
428870ff
BB
707 dsl_scan_visitdnode(scn, ds, osp->os_type,
708 &osp->os_meta_dnode, *bufp, DMU_META_DNODE_OBJECT, tx);
709
710 if (OBJSET_BUF_HAS_USERUSED(*bufp)) {
711 /*
712 * We also always visit user/group accounting
713 * objects, and never skip them, even if we are
714 * pausing. This is necessary so that the space
715 * deltas from this txg get integrated.
716 */
717 dsl_scan_visitdnode(scn, ds, osp->os_type,
718 &osp->os_groupused_dnode, *bufp,
719 DMU_GROUPUSED_OBJECT, tx);
720 dsl_scan_visitdnode(scn, ds, osp->os_type,
721 &osp->os_userused_dnode, *bufp,
722 DMU_USERUSED_OBJECT, tx);
723 }
724 }
725
726 return (0);
727}
728
10be533e 729inline __attribute__((always_inline)) static void
428870ff
BB
730dsl_scan_visitdnode(dsl_scan_t *scn, dsl_dataset_t *ds,
731 dmu_objset_type_t ostype, dnode_phys_t *dnp, arc_buf_t *buf,
732 uint64_t object, dmu_tx_t *tx)
733{
734 int j;
735
736 for (j = 0; j < dnp->dn_nblkptr; j++) {
737 zbookmark_t czb;
738
739 SET_BOOKMARK(&czb, ds ? ds->ds_object : 0, object,
740 dnp->dn_nlevels - 1, j);
741 dsl_scan_visitbp(&dnp->dn_blkptr[j],
742 &czb, dnp, buf, ds, scn, ostype, tx);
743 }
744
745 if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
746 zbookmark_t czb;
747 SET_BOOKMARK(&czb, ds ? ds->ds_object : 0, object,
748 0, DMU_SPILL_BLKID);
749 dsl_scan_visitbp(&dnp->dn_spill,
750 &czb, dnp, buf, ds, scn, ostype, tx);
751 }
752}
753
754/*
755 * The arguments are in this order because mdb can only print the
756 * first 5; we want them to be useful.
757 */
758static void
759dsl_scan_visitbp(blkptr_t *bp, const zbookmark_t *zb,
760 dnode_phys_t *dnp, arc_buf_t *pbuf,
761 dsl_dataset_t *ds, dsl_scan_t *scn, dmu_objset_type_t ostype,
762 dmu_tx_t *tx)
763{
764 dsl_pool_t *dp = scn->scn_dp;
765 arc_buf_t *buf = NULL;
161ce7ce
BB
766 blkptr_t *bp_toread;
767
20a083cb 768 bp_toread = kmem_alloc(sizeof (blkptr_t), KM_PUSHPAGE);
161ce7ce 769 *bp_toread = *bp;
428870ff
BB
770
771 /* ASSERT(pbuf == NULL || arc_released(pbuf)); */
772
773 if (dsl_scan_check_pause(scn, zb))
161ce7ce 774 goto out;
428870ff
BB
775
776 if (dsl_scan_check_resume(scn, dnp, zb))
161ce7ce 777 goto out;
428870ff 778
b0bc7a84 779 if (BP_IS_HOLE(bp))
161ce7ce 780 goto out;
428870ff
BB
781
782 scn->scn_visited_this_txg++;
783
b81c4ac9
BB
784 /*
785 * This debugging is commented out to conserve stack space. This
786 * function is called recursively and the debugging addes several
787 * bytes to the stack for each call. It can be commented back in
788 * if required to debug an issue in dsl_scan_visitbp().
789 *
790 * dprintf_bp(bp,
791 * "visiting ds=%p/%llu zb=%llx/%llx/%llx/%llx buf=%p bp=%p",
792 * ds, ds ? ds->ds_object : 0,
793 * zb->zb_objset, zb->zb_object, zb->zb_level, zb->zb_blkid,
794 * pbuf, bp);
795 */
428870ff
BB
796
797 if (bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
161ce7ce 798 goto out;
428870ff 799
161ce7ce 800 if (dsl_scan_recurse(scn, ds, ostype, dnp, bp_toread, zb, tx,
428870ff 801 &buf) != 0)
161ce7ce 802 goto out;
428870ff
BB
803
804 /*
805 * If dsl_scan_ddt() has aready visited this block, it will have
806 * already done any translations or scrubbing, so don't call the
807 * callback again.
808 */
809 if (ddt_class_contains(dp->dp_spa,
810 scn->scn_phys.scn_ddt_class_max, bp)) {
811 ASSERT(buf == NULL);
161ce7ce 812 goto out;
428870ff
BB
813 }
814
815 /*
816 * If this block is from the future (after cur_max_txg), then we
817 * are doing this on behalf of a deleted snapshot, and we will
818 * revisit the future block on the next pass of this dataset.
819 * Don't scan it now unless we need to because something
820 * under it was modified.
821 */
5d1f7fb6 822 if (BP_PHYSICAL_BIRTH(bp) <= scn->scn_phys.scn_cur_max_txg) {
428870ff
BB
823 scan_funcs[scn->scn_phys.scn_func](dp, bp, zb);
824 }
825 if (buf)
826 (void) arc_buf_remove_ref(buf, &buf);
161ce7ce 827out:
d1d7e268 828 kmem_free(bp_toread, sizeof (blkptr_t));
428870ff
BB
829}
830
831static void
832dsl_scan_visit_rootbp(dsl_scan_t *scn, dsl_dataset_t *ds, blkptr_t *bp,
833 dmu_tx_t *tx)
834{
835 zbookmark_t zb;
836
837 SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
838 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
839 dsl_scan_visitbp(bp, &zb, NULL, NULL,
840 ds, scn, DMU_OST_NONE, tx);
841
842 dprintf_ds(ds, "finished scan%s", "");
843}
844
845void
846dsl_scan_ds_destroyed(dsl_dataset_t *ds, dmu_tx_t *tx)
847{
848 dsl_pool_t *dp = ds->ds_dir->dd_pool;
849 dsl_scan_t *scn = dp->dp_scan;
850 uint64_t mintxg;
851
852 if (scn->scn_phys.scn_state != DSS_SCANNING)
853 return;
854
855 if (scn->scn_phys.scn_bookmark.zb_objset == ds->ds_object) {
856 if (dsl_dataset_is_snapshot(ds)) {
857 /* Note, scn_cur_{min,max}_txg stays the same. */
858 scn->scn_phys.scn_bookmark.zb_objset =
859 ds->ds_phys->ds_next_snap_obj;
860 zfs_dbgmsg("destroying ds %llu; currently traversing; "
861 "reset zb_objset to %llu",
862 (u_longlong_t)ds->ds_object,
863 (u_longlong_t)ds->ds_phys->ds_next_snap_obj);
864 scn->scn_phys.scn_flags |= DSF_VISIT_DS_AGAIN;
865 } else {
866 SET_BOOKMARK(&scn->scn_phys.scn_bookmark,
867 ZB_DESTROYED_OBJSET, 0, 0, 0);
868 zfs_dbgmsg("destroying ds %llu; currently traversing; "
869 "reset bookmark to -1,0,0,0",
870 (u_longlong_t)ds->ds_object);
871 }
872 } else if (zap_lookup_int_key(dp->dp_meta_objset,
873 scn->scn_phys.scn_queue_obj, ds->ds_object, &mintxg) == 0) {
874 ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
875 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
876 scn->scn_phys.scn_queue_obj, ds->ds_object, tx));
877 if (dsl_dataset_is_snapshot(ds)) {
878 /*
879 * We keep the same mintxg; it could be >
880 * ds_creation_txg if the previous snapshot was
881 * deleted too.
882 */
883 VERIFY(zap_add_int_key(dp->dp_meta_objset,
884 scn->scn_phys.scn_queue_obj,
885 ds->ds_phys->ds_next_snap_obj, mintxg, tx) == 0);
886 zfs_dbgmsg("destroying ds %llu; in queue; "
887 "replacing with %llu",
888 (u_longlong_t)ds->ds_object,
889 (u_longlong_t)ds->ds_phys->ds_next_snap_obj);
890 } else {
891 zfs_dbgmsg("destroying ds %llu; in queue; removing",
892 (u_longlong_t)ds->ds_object);
893 }
894 } else {
895 zfs_dbgmsg("destroying ds %llu; ignoring",
896 (u_longlong_t)ds->ds_object);
897 }
898
899 /*
900 * dsl_scan_sync() should be called after this, and should sync
901 * out our changed state, but just to be safe, do it here.
902 */
903 dsl_scan_sync_state(scn, tx);
904}
905
906void
907dsl_scan_ds_snapshotted(dsl_dataset_t *ds, dmu_tx_t *tx)
908{
909 dsl_pool_t *dp = ds->ds_dir->dd_pool;
910 dsl_scan_t *scn = dp->dp_scan;
911 uint64_t mintxg;
912
913 if (scn->scn_phys.scn_state != DSS_SCANNING)
914 return;
915
916 ASSERT(ds->ds_phys->ds_prev_snap_obj != 0);
917
918 if (scn->scn_phys.scn_bookmark.zb_objset == ds->ds_object) {
919 scn->scn_phys.scn_bookmark.zb_objset =
920 ds->ds_phys->ds_prev_snap_obj;
921 zfs_dbgmsg("snapshotting ds %llu; currently traversing; "
922 "reset zb_objset to %llu",
923 (u_longlong_t)ds->ds_object,
924 (u_longlong_t)ds->ds_phys->ds_prev_snap_obj);
925 } else if (zap_lookup_int_key(dp->dp_meta_objset,
926 scn->scn_phys.scn_queue_obj, ds->ds_object, &mintxg) == 0) {
927 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
928 scn->scn_phys.scn_queue_obj, ds->ds_object, tx));
929 VERIFY(zap_add_int_key(dp->dp_meta_objset,
930 scn->scn_phys.scn_queue_obj,
931 ds->ds_phys->ds_prev_snap_obj, mintxg, tx) == 0);
932 zfs_dbgmsg("snapshotting ds %llu; in queue; "
933 "replacing with %llu",
934 (u_longlong_t)ds->ds_object,
935 (u_longlong_t)ds->ds_phys->ds_prev_snap_obj);
936 }
937 dsl_scan_sync_state(scn, tx);
938}
939
940void
941dsl_scan_ds_clone_swapped(dsl_dataset_t *ds1, dsl_dataset_t *ds2, dmu_tx_t *tx)
942{
943 dsl_pool_t *dp = ds1->ds_dir->dd_pool;
944 dsl_scan_t *scn = dp->dp_scan;
945 uint64_t mintxg;
946
947 if (scn->scn_phys.scn_state != DSS_SCANNING)
948 return;
949
950 if (scn->scn_phys.scn_bookmark.zb_objset == ds1->ds_object) {
951 scn->scn_phys.scn_bookmark.zb_objset = ds2->ds_object;
952 zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
953 "reset zb_objset to %llu",
954 (u_longlong_t)ds1->ds_object,
955 (u_longlong_t)ds2->ds_object);
956 } else if (scn->scn_phys.scn_bookmark.zb_objset == ds2->ds_object) {
957 scn->scn_phys.scn_bookmark.zb_objset = ds1->ds_object;
958 zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
959 "reset zb_objset to %llu",
960 (u_longlong_t)ds2->ds_object,
961 (u_longlong_t)ds1->ds_object);
962 }
963
964 if (zap_lookup_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj,
965 ds1->ds_object, &mintxg) == 0) {
966 int err;
967
968 ASSERT3U(mintxg, ==, ds1->ds_phys->ds_prev_snap_txg);
969 ASSERT3U(mintxg, ==, ds2->ds_phys->ds_prev_snap_txg);
970 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
971 scn->scn_phys.scn_queue_obj, ds1->ds_object, tx));
972 err = zap_add_int_key(dp->dp_meta_objset,
973 scn->scn_phys.scn_queue_obj, ds2->ds_object, mintxg, tx);
974 VERIFY(err == 0 || err == EEXIST);
975 if (err == EEXIST) {
976 /* Both were there to begin with */
977 VERIFY(0 == zap_add_int_key(dp->dp_meta_objset,
978 scn->scn_phys.scn_queue_obj,
979 ds1->ds_object, mintxg, tx));
980 }
981 zfs_dbgmsg("clone_swap ds %llu; in queue; "
982 "replacing with %llu",
983 (u_longlong_t)ds1->ds_object,
984 (u_longlong_t)ds2->ds_object);
985 } else if (zap_lookup_int_key(dp->dp_meta_objset,
986 scn->scn_phys.scn_queue_obj, ds2->ds_object, &mintxg) == 0) {
987 ASSERT3U(mintxg, ==, ds1->ds_phys->ds_prev_snap_txg);
988 ASSERT3U(mintxg, ==, ds2->ds_phys->ds_prev_snap_txg);
989 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
990 scn->scn_phys.scn_queue_obj, ds2->ds_object, tx));
991 VERIFY(0 == zap_add_int_key(dp->dp_meta_objset,
992 scn->scn_phys.scn_queue_obj, ds1->ds_object, mintxg, tx));
993 zfs_dbgmsg("clone_swap ds %llu; in queue; "
994 "replacing with %llu",
995 (u_longlong_t)ds2->ds_object,
996 (u_longlong_t)ds1->ds_object);
997 }
998
999 dsl_scan_sync_state(scn, tx);
1000}
1001
1002struct enqueue_clones_arg {
1003 dmu_tx_t *tx;
1004 uint64_t originobj;
1005};
1006
1007/* ARGSUSED */
1008static int
13fe0198 1009enqueue_clones_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg)
428870ff
BB
1010{
1011 struct enqueue_clones_arg *eca = arg;
1012 dsl_dataset_t *ds;
1013 int err;
428870ff
BB
1014 dsl_scan_t *scn = dp->dp_scan;
1015
13fe0198
MA
1016 if (hds->ds_dir->dd_phys->dd_origin_obj != eca->originobj)
1017 return (0);
1018
1019 err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds);
428870ff
BB
1020 if (err)
1021 return (err);
1022
13fe0198
MA
1023 while (ds->ds_phys->ds_prev_snap_obj != eca->originobj) {
1024 dsl_dataset_t *prev;
1025 err = dsl_dataset_hold_obj(dp,
1026 ds->ds_phys->ds_prev_snap_obj, FTAG, &prev);
428870ff 1027
13fe0198
MA
1028 dsl_dataset_rele(ds, FTAG);
1029 if (err)
1030 return (err);
1031 ds = prev;
428870ff 1032 }
13fe0198
MA
1033 VERIFY(zap_add_int_key(dp->dp_meta_objset,
1034 scn->scn_phys.scn_queue_obj, ds->ds_object,
1035 ds->ds_phys->ds_prev_snap_txg, eca->tx) == 0);
428870ff
BB
1036 dsl_dataset_rele(ds, FTAG);
1037 return (0);
1038}
1039
1040static void
1041dsl_scan_visitds(dsl_scan_t *scn, uint64_t dsobj, dmu_tx_t *tx)
1042{
1043 dsl_pool_t *dp = scn->scn_dp;
1044 dsl_dataset_t *ds;
572e2857 1045 objset_t *os;
d6320ddb 1046 char *dsname;
428870ff
BB
1047
1048 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
1049
572e2857
BB
1050 if (dmu_objset_from_ds(ds, &os))
1051 goto out;
1052
1053 /*
1054 * Only the ZIL in the head (non-snapshot) is valid. Even though
1055 * snapshots can have ZIL block pointers (which may be the same
1056 * BP as in the head), they must be ignored. So we traverse the
1057 * ZIL here, rather than in scan_recurse(), because the regular
1058 * snapshot block-sharing rules don't apply to it.
1059 */
1060 if (DSL_SCAN_IS_SCRUB_RESILVER(scn) && !dsl_dataset_is_snapshot(ds))
1061 dsl_scan_zil(dp, &os->os_zil_header);
1062
428870ff
BB
1063 /*
1064 * Iterate over the bps in this ds.
1065 */
1066 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1067 dsl_scan_visit_rootbp(scn, ds, &ds->ds_phys->ds_bp, tx);
1068
20a083cb 1069 dsname = kmem_alloc(ZFS_MAXNAMELEN, KM_PUSHPAGE);
428870ff
BB
1070 dsl_dataset_name(ds, dsname);
1071 zfs_dbgmsg("scanned dataset %llu (%s) with min=%llu max=%llu; "
1072 "pausing=%u",
1073 (longlong_t)dsobj, dsname,
1074 (longlong_t)scn->scn_phys.scn_cur_min_txg,
1075 (longlong_t)scn->scn_phys.scn_cur_max_txg,
1076 (int)scn->scn_pausing);
1077 kmem_free(dsname, ZFS_MAXNAMELEN);
1078
1079 if (scn->scn_pausing)
1080 goto out;
1081
1082 /*
1083 * We've finished this pass over this dataset.
1084 */
1085
1086 /*
1087 * If we did not completely visit this dataset, do another pass.
1088 */
1089 if (scn->scn_phys.scn_flags & DSF_VISIT_DS_AGAIN) {
1090 zfs_dbgmsg("incomplete pass; visiting again");
1091 scn->scn_phys.scn_flags &= ~DSF_VISIT_DS_AGAIN;
1092 VERIFY(zap_add_int_key(dp->dp_meta_objset,
1093 scn->scn_phys.scn_queue_obj, ds->ds_object,
1094 scn->scn_phys.scn_cur_max_txg, tx) == 0);
1095 goto out;
1096 }
1097
1098 /*
1099 * Add descendent datasets to work queue.
1100 */
1101 if (ds->ds_phys->ds_next_snap_obj != 0) {
1102 VERIFY(zap_add_int_key(dp->dp_meta_objset,
1103 scn->scn_phys.scn_queue_obj, ds->ds_phys->ds_next_snap_obj,
1104 ds->ds_phys->ds_creation_txg, tx) == 0);
1105 }
1106 if (ds->ds_phys->ds_num_children > 1) {
1107 boolean_t usenext = B_FALSE;
1108 if (ds->ds_phys->ds_next_clones_obj != 0) {
1109 uint64_t count;
1110 /*
1111 * A bug in a previous version of the code could
1112 * cause upgrade_clones_cb() to not set
1113 * ds_next_snap_obj when it should, leading to a
1114 * missing entry. Therefore we can only use the
1115 * next_clones_obj when its count is correct.
1116 */
1117 int err = zap_count(dp->dp_meta_objset,
1118 ds->ds_phys->ds_next_clones_obj, &count);
1119 if (err == 0 &&
1120 count == ds->ds_phys->ds_num_children - 1)
1121 usenext = B_TRUE;
1122 }
1123
1124 if (usenext) {
13fe0198 1125 VERIFY0(zap_join_key(dp->dp_meta_objset,
428870ff
BB
1126 ds->ds_phys->ds_next_clones_obj,
1127 scn->scn_phys.scn_queue_obj,
13fe0198 1128 ds->ds_phys->ds_creation_txg, tx));
428870ff
BB
1129 } else {
1130 struct enqueue_clones_arg eca;
1131 eca.tx = tx;
1132 eca.originobj = ds->ds_object;
1133
13fe0198
MA
1134 VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
1135 enqueue_clones_cb, &eca, DS_FIND_CHILDREN));
428870ff
BB
1136 }
1137 }
1138
1139out:
1140 dsl_dataset_rele(ds, FTAG);
1141}
1142
1143/* ARGSUSED */
1144static int
13fe0198 1145enqueue_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg)
428870ff
BB
1146{
1147 dmu_tx_t *tx = arg;
1148 dsl_dataset_t *ds;
1149 int err;
428870ff
BB
1150 dsl_scan_t *scn = dp->dp_scan;
1151
13fe0198 1152 err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds);
428870ff
BB
1153 if (err)
1154 return (err);
1155
1156 while (ds->ds_phys->ds_prev_snap_obj != 0) {
1157 dsl_dataset_t *prev;
1158 err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
1159 FTAG, &prev);
1160 if (err) {
1161 dsl_dataset_rele(ds, FTAG);
1162 return (err);
1163 }
1164
1165 /*
1166 * If this is a clone, we don't need to worry about it for now.
1167 */
1168 if (prev->ds_phys->ds_next_snap_obj != ds->ds_object) {
1169 dsl_dataset_rele(ds, FTAG);
1170 dsl_dataset_rele(prev, FTAG);
1171 return (0);
1172 }
1173 dsl_dataset_rele(ds, FTAG);
1174 ds = prev;
1175 }
1176
1177 VERIFY(zap_add_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj,
1178 ds->ds_object, ds->ds_phys->ds_prev_snap_txg, tx) == 0);
1179 dsl_dataset_rele(ds, FTAG);
1180 return (0);
1181}
1182
1183/*
1184 * Scrub/dedup interaction.
1185 *
1186 * If there are N references to a deduped block, we don't want to scrub it
1187 * N times -- ideally, we should scrub it exactly once.
1188 *
1189 * We leverage the fact that the dde's replication class (enum ddt_class)
1190 * is ordered from highest replication class (DDT_CLASS_DITTO) to lowest
1191 * (DDT_CLASS_UNIQUE) so that we may walk the DDT in that order.
1192 *
1193 * To prevent excess scrubbing, the scrub begins by walking the DDT
1194 * to find all blocks with refcnt > 1, and scrubs each of these once.
1195 * Since there are two replication classes which contain blocks with
1196 * refcnt > 1, we scrub the highest replication class (DDT_CLASS_DITTO) first.
1197 * Finally the top-down scrub begins, only visiting blocks with refcnt == 1.
1198 *
1199 * There would be nothing more to say if a block's refcnt couldn't change
1200 * during a scrub, but of course it can so we must account for changes
1201 * in a block's replication class.
1202 *
1203 * Here's an example of what can occur:
1204 *
1205 * If a block has refcnt > 1 during the DDT scrub phase, but has refcnt == 1
1206 * when visited during the top-down scrub phase, it will be scrubbed twice.
1207 * This negates our scrub optimization, but is otherwise harmless.
1208 *
1209 * If a block has refcnt == 1 during the DDT scrub phase, but has refcnt > 1
1210 * on each visit during the top-down scrub phase, it will never be scrubbed.
1211 * To catch this, ddt_sync_entry() notifies the scrub code whenever a block's
1212 * reference class transitions to a higher level (i.e DDT_CLASS_UNIQUE to
1213 * DDT_CLASS_DUPLICATE); if it transitions from refcnt == 1 to refcnt > 1
1214 * while a scrub is in progress, it scrubs the block right then.
1215 */
1216static void
1217dsl_scan_ddt(dsl_scan_t *scn, dmu_tx_t *tx)
1218{
1219 ddt_bookmark_t *ddb = &scn->scn_phys.scn_ddt_bookmark;
2598c001 1220 ddt_entry_t dde;
428870ff
BB
1221 int error;
1222 uint64_t n = 0;
1223
2598c001
BB
1224 bzero(&dde, sizeof (ddt_entry_t));
1225
428870ff
BB
1226 while ((error = ddt_walk(scn->scn_dp->dp_spa, ddb, &dde)) == 0) {
1227 ddt_t *ddt;
1228
1229 if (ddb->ddb_class > scn->scn_phys.scn_ddt_class_max)
1230 break;
1231 dprintf("visiting ddb=%llu/%llu/%llu/%llx\n",
1232 (longlong_t)ddb->ddb_class,
1233 (longlong_t)ddb->ddb_type,
1234 (longlong_t)ddb->ddb_checksum,
1235 (longlong_t)ddb->ddb_cursor);
1236
1237 /* There should be no pending changes to the dedup table */
1238 ddt = scn->scn_dp->dp_spa->spa_ddt[ddb->ddb_checksum];
1239 ASSERT(avl_first(&ddt->ddt_tree) == NULL);
1240
1241 dsl_scan_ddt_entry(scn, ddb->ddb_checksum, &dde, tx);
1242 n++;
1243
1244 if (dsl_scan_check_pause(scn, NULL))
1245 break;
1246 }
1247
1248 zfs_dbgmsg("scanned %llu ddt entries with class_max = %u; pausing=%u",
1249 (longlong_t)n, (int)scn->scn_phys.scn_ddt_class_max,
1250 (int)scn->scn_pausing);
1251
1252 ASSERT(error == 0 || error == ENOENT);
1253 ASSERT(error != ENOENT ||
1254 ddb->ddb_class > scn->scn_phys.scn_ddt_class_max);
1255}
1256
1257/* ARGSUSED */
1258void
1259dsl_scan_ddt_entry(dsl_scan_t *scn, enum zio_checksum checksum,
1260 ddt_entry_t *dde, dmu_tx_t *tx)
1261{
1262 const ddt_key_t *ddk = &dde->dde_key;
1263 ddt_phys_t *ddp = dde->dde_phys;
1264 blkptr_t bp;
1265 zbookmark_t zb = { 0 };
d6320ddb 1266 int p;
428870ff
BB
1267
1268 if (scn->scn_phys.scn_state != DSS_SCANNING)
1269 return;
1270
d6320ddb 1271 for (p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
428870ff 1272 if (ddp->ddp_phys_birth == 0 ||
5d1f7fb6 1273 ddp->ddp_phys_birth > scn->scn_phys.scn_max_txg)
428870ff
BB
1274 continue;
1275 ddt_bp_create(checksum, ddk, ddp, &bp);
1276
1277 scn->scn_visited_this_txg++;
1278 scan_funcs[scn->scn_phys.scn_func](scn->scn_dp, &bp, &zb);
1279 }
1280}
1281
1282static void
1283dsl_scan_visit(dsl_scan_t *scn, dmu_tx_t *tx)
1284{
1285 dsl_pool_t *dp = scn->scn_dp;
40a39e11
BB
1286 zap_cursor_t *zc;
1287 zap_attribute_t *za;
428870ff
BB
1288
1289 if (scn->scn_phys.scn_ddt_bookmark.ddb_class <=
1290 scn->scn_phys.scn_ddt_class_max) {
1291 scn->scn_phys.scn_cur_min_txg = scn->scn_phys.scn_min_txg;
1292 scn->scn_phys.scn_cur_max_txg = scn->scn_phys.scn_max_txg;
1293 dsl_scan_ddt(scn, tx);
1294 if (scn->scn_pausing)
1295 return;
1296 }
1297
1298 if (scn->scn_phys.scn_bookmark.zb_objset == DMU_META_OBJSET) {
1299 /* First do the MOS & ORIGIN */
1300
1301 scn->scn_phys.scn_cur_min_txg = scn->scn_phys.scn_min_txg;
1302 scn->scn_phys.scn_cur_max_txg = scn->scn_phys.scn_max_txg;
1303 dsl_scan_visit_rootbp(scn, NULL,
1304 &dp->dp_meta_rootbp, tx);
1305 spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
1306 if (scn->scn_pausing)
1307 return;
1308
1309 if (spa_version(dp->dp_spa) < SPA_VERSION_DSL_SCRUB) {
13fe0198
MA
1310 VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
1311 enqueue_cb, tx, DS_FIND_CHILDREN));
428870ff
BB
1312 } else {
1313 dsl_scan_visitds(scn,
1314 dp->dp_origin_snap->ds_object, tx);
1315 }
1316 ASSERT(!scn->scn_pausing);
1317 } else if (scn->scn_phys.scn_bookmark.zb_objset !=
1318 ZB_DESTROYED_OBJSET) {
1319 /*
1320 * If we were paused, continue from here. Note if the
1321 * ds we were paused on was deleted, the zb_objset may
1322 * be -1, so we will skip this and find a new objset
1323 * below.
1324 */
1325 dsl_scan_visitds(scn, scn->scn_phys.scn_bookmark.zb_objset, tx);
1326 if (scn->scn_pausing)
1327 return;
1328 }
1329
1330 /*
1331 * In case we were paused right at the end of the ds, zero the
1332 * bookmark so we don't think that we're still trying to resume.
1333 */
1334 bzero(&scn->scn_phys.scn_bookmark, sizeof (zbookmark_t));
d1d7e268
MK
1335 zc = kmem_alloc(sizeof (zap_cursor_t), KM_PUSHPAGE);
1336 za = kmem_alloc(sizeof (zap_attribute_t), KM_PUSHPAGE);
428870ff
BB
1337
1338 /* keep pulling things out of the zap-object-as-queue */
40a39e11 1339 while (zap_cursor_init(zc, dp->dp_meta_objset,
428870ff 1340 scn->scn_phys.scn_queue_obj),
40a39e11 1341 zap_cursor_retrieve(zc, za) == 0) {
428870ff
BB
1342 dsl_dataset_t *ds;
1343 uint64_t dsobj;
1344
40a39e11 1345 dsobj = strtonum(za->za_name, NULL);
428870ff
BB
1346 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
1347 scn->scn_phys.scn_queue_obj, dsobj, tx));
1348
1349 /* Set up min/max txg */
1350 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
40a39e11 1351 if (za->za_first_integer != 0) {
428870ff
BB
1352 scn->scn_phys.scn_cur_min_txg =
1353 MAX(scn->scn_phys.scn_min_txg,
40a39e11 1354 za->za_first_integer);
428870ff
BB
1355 } else {
1356 scn->scn_phys.scn_cur_min_txg =
1357 MAX(scn->scn_phys.scn_min_txg,
1358 ds->ds_phys->ds_prev_snap_txg);
1359 }
1360 scn->scn_phys.scn_cur_max_txg = dsl_scan_ds_maxtxg(ds);
1361 dsl_dataset_rele(ds, FTAG);
1362
1363 dsl_scan_visitds(scn, dsobj, tx);
40a39e11 1364 zap_cursor_fini(zc);
428870ff 1365 if (scn->scn_pausing)
40a39e11 1366 goto out;
428870ff 1367 }
40a39e11
BB
1368 zap_cursor_fini(zc);
1369out:
d1d7e268
MK
1370 kmem_free(za, sizeof (zap_attribute_t));
1371 kmem_free(zc, sizeof (zap_cursor_t));
428870ff
BB
1372}
1373
9ae529ec
CS
1374static boolean_t
1375dsl_scan_free_should_pause(dsl_scan_t *scn)
428870ff 1376{
428870ff
BB
1377 uint64_t elapsed_nanosecs;
1378
78e2739d
MA
1379 if (zfs_recover)
1380 return (B_FALSE);
1381
428870ff 1382 elapsed_nanosecs = gethrtime() - scn->scn_sync_start_time;
9ae529ec 1383 return (elapsed_nanosecs / NANOSEC > zfs_txg_timeout ||
63fd3c6c 1384 (NSEC2MSEC(elapsed_nanosecs) > zfs_free_min_time_ms &&
428870ff 1385 txg_sync_waiting(scn->scn_dp)) ||
9ae529ec
CS
1386 spa_shutting_down(scn->scn_dp->dp_spa));
1387}
1388
1389static int
1390dsl_scan_free_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1391{
1392 dsl_scan_t *scn = arg;
1393
1394 if (!scn->scn_is_bptree ||
1395 (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_OBJSET)) {
1396 if (dsl_scan_free_should_pause(scn))
2e528b49 1397 return (SET_ERROR(ERESTART));
9ae529ec 1398 }
428870ff
BB
1399
1400 zio_nowait(zio_free_sync(scn->scn_zio_root, scn->scn_dp->dp_spa,
1401 dmu_tx_get_txg(tx), bp, 0));
1402 dsl_dir_diduse_space(tx->tx_pool->dp_free_dir, DD_USED_HEAD,
1403 -bp_get_dsize_sync(scn->scn_dp->dp_spa, bp),
1404 -BP_GET_PSIZE(bp), -BP_GET_UCSIZE(bp), tx);
1405 scn->scn_visited_this_txg++;
1406 return (0);
1407}
1408
1409boolean_t
1410dsl_scan_active(dsl_scan_t *scn)
1411{
1412 spa_t *spa = scn->scn_dp->dp_spa;
1413 uint64_t used = 0, comp, uncomp;
1414
1415 if (spa->spa_load_state != SPA_LOAD_NONE)
1416 return (B_FALSE);
1417 if (spa_shutting_down(spa))
1418 return (B_FALSE);
2696dfaf 1419 if (scn->scn_phys.scn_state == DSS_SCANNING ||
fbeddd60 1420 (scn->scn_async_destroying && !scn->scn_async_stalled))
428870ff
BB
1421 return (B_TRUE);
1422
1423 if (spa_version(scn->scn_dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
1424 (void) bpobj_space(&scn->scn_dp->dp_free_bpobj,
1425 &used, &comp, &uncomp);
1426 }
1427 return (used != 0);
1428}
1429
1430void
1431dsl_scan_sync(dsl_pool_t *dp, dmu_tx_t *tx)
1432{
1433 dsl_scan_t *scn = dp->dp_scan;
1434 spa_t *spa = dp->dp_spa;
fbeddd60 1435 int err = 0;
428870ff
BB
1436
1437 /*
1438 * Check for scn_restart_txg before checking spa_load_state, so
1439 * that we can restart an old-style scan while the pool is being
1440 * imported (see dsl_scan_init).
1441 */
1442 if (scn->scn_restart_txg != 0 &&
1443 scn->scn_restart_txg <= tx->tx_txg) {
1444 pool_scan_func_t func = POOL_SCAN_SCRUB;
1445 dsl_scan_done(scn, B_FALSE, tx);
1446 if (vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL))
1447 func = POOL_SCAN_RESILVER;
1448 zfs_dbgmsg("restarting scan func=%u txg=%llu",
1449 func, tx->tx_txg);
13fe0198 1450 dsl_scan_setup_sync(&func, tx);
428870ff
BB
1451 }
1452
fbeddd60
MA
1453 /*
1454 * If the scan is inactive due to a stalled async destroy, try again.
1455 */
1456 if ((!scn->scn_async_stalled && !dsl_scan_active(scn)) ||
428870ff
BB
1457 spa_sync_pass(dp->dp_spa) > 1)
1458 return;
1459
1460 scn->scn_visited_this_txg = 0;
1461 scn->scn_pausing = B_FALSE;
1462 scn->scn_sync_start_time = gethrtime();
1463 spa->spa_scrub_active = B_TRUE;
1464
1465 /*
fbeddd60
MA
1466 * First process the async destroys. If we pause, don't do
1467 * any scrubbing or resilvering. This ensures that there are no
1468 * async destroys while we are scanning, so the scan code doesn't
1469 * have to worry about traversing it. It is also faster to free the
1470 * blocks than to scrub them.
428870ff
BB
1471 */
1472 if (spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
9ae529ec 1473 scn->scn_is_bptree = B_FALSE;
428870ff
BB
1474 scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
1475 NULL, ZIO_FLAG_MUSTSUCCEED);
1476 err = bpobj_iterate(&dp->dp_free_bpobj,
9ae529ec 1477 dsl_scan_free_block_cb, scn, tx);
428870ff 1478 VERIFY3U(0, ==, zio_wait(scn->scn_zio_root));
9ae529ec 1479
fbeddd60
MA
1480 if (err != 0 && err != ERESTART)
1481 zfs_panic_recover("error %u from bpobj_iterate()", err);
1482 }
13fe0198 1483
fbeddd60
MA
1484 if (err == 0 && spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
1485 ASSERT(scn->scn_async_destroying);
1486 scn->scn_is_bptree = B_TRUE;
1487 scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
1488 NULL, ZIO_FLAG_MUSTSUCCEED);
1489 err = bptree_iterate(dp->dp_meta_objset,
1490 dp->dp_bptree_obj, B_TRUE, dsl_scan_free_block_cb, scn, tx);
1491 VERIFY0(zio_wait(scn->scn_zio_root));
1492
1493 if (err == EIO || err == ECKSUM) {
1494 err = 0;
1495 } else if (err != 0 && err != ERESTART) {
1496 zfs_panic_recover("error %u from "
1497 "traverse_dataset_destroyed()", err);
9ae529ec 1498 }
fbeddd60
MA
1499
1500 /*
1501 * If we didn't make progress, mark the async destroy as
1502 * stalled, so that we will not initiate a spa_sync() on
1503 * its behalf.
1504 */
1505 scn->scn_async_stalled = (scn->scn_visited_this_txg == 0);
1506
1507 if (bptree_is_empty(dp->dp_meta_objset, dp->dp_bptree_obj)) {
1508 /* finished; deactivate async destroy feature */
1509 spa_feature_decr(spa, SPA_FEATURE_ASYNC_DESTROY, tx);
1510 ASSERT(!spa_feature_is_active(spa,
1511 SPA_FEATURE_ASYNC_DESTROY));
1512 VERIFY0(zap_remove(dp->dp_meta_objset,
1513 DMU_POOL_DIRECTORY_OBJECT,
1514 DMU_POOL_BPTREE_OBJ, tx));
1515 VERIFY0(bptree_free(dp->dp_meta_objset,
1516 dp->dp_bptree_obj, tx));
1517 dp->dp_bptree_obj = 0;
1518 scn->scn_async_destroying = B_FALSE;
428870ff 1519 }
fbeddd60
MA
1520 }
1521 if (scn->scn_visited_this_txg) {
1522 zfs_dbgmsg("freed %llu blocks in %llums from "
1523 "free_bpobj/bptree txg %llu; err=%u",
1524 (longlong_t)scn->scn_visited_this_txg,
1525 (longlong_t)
1526 NSEC2MSEC(gethrtime() - scn->scn_sync_start_time),
1527 (longlong_t)tx->tx_txg, err);
1528 scn->scn_visited_this_txg = 0;
1529
1530 /*
1531 * Write out changes to the DDT that may be required as a
1532 * result of the blocks freed. This ensures that the DDT
1533 * is clean when a scrub/resilver runs.
1534 */
1535 ddt_sync(spa, tx->tx_txg);
1536 }
1537 if (err != 0)
1538 return;
1539 if (!scn->scn_async_destroying && zfs_free_leak_on_eio &&
1540 (dp->dp_free_dir->dd_phys->dd_used_bytes != 0 ||
1541 dp->dp_free_dir->dd_phys->dd_compressed_bytes != 0 ||
1542 dp->dp_free_dir->dd_phys->dd_uncompressed_bytes != 0)) {
1543 /*
1544 * We have finished background destroying, but there is still
1545 * some space left in the dp_free_dir. Transfer this leaked
1546 * space to the dp_leak_dir.
1547 */
1548 if (dp->dp_leak_dir == NULL) {
1549 rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
1550 (void) dsl_dir_create_sync(dp, dp->dp_root_dir,
1551 LEAK_DIR_NAME, tx);
1552 VERIFY0(dsl_pool_open_special_dir(dp,
1553 LEAK_DIR_NAME, &dp->dp_leak_dir));
1554 rrw_exit(&dp->dp_config_rwlock, FTAG);
1555 }
1556 dsl_dir_diduse_space(dp->dp_leak_dir, DD_USED_HEAD,
1557 dp->dp_free_dir->dd_phys->dd_used_bytes,
1558 dp->dp_free_dir->dd_phys->dd_compressed_bytes,
1559 dp->dp_free_dir->dd_phys->dd_uncompressed_bytes, tx);
1560 dsl_dir_diduse_space(dp->dp_free_dir, DD_USED_HEAD,
1561 -dp->dp_free_dir->dd_phys->dd_used_bytes,
1562 -dp->dp_free_dir->dd_phys->dd_compressed_bytes,
1563 -dp->dp_free_dir->dd_phys->dd_uncompressed_bytes, tx);
1564 }
1565 if (!scn->scn_async_destroying) {
9b67f605
MA
1566 /* finished; verify that space accounting went to zero */
1567 ASSERT0(dp->dp_free_dir->dd_phys->dd_used_bytes);
1568 ASSERT0(dp->dp_free_dir->dd_phys->dd_compressed_bytes);
1569 ASSERT0(dp->dp_free_dir->dd_phys->dd_uncompressed_bytes);
428870ff
BB
1570 }
1571
1572 if (scn->scn_phys.scn_state != DSS_SCANNING)
1573 return;
1574
5d1f7fb6
GW
1575 if (scn->scn_done_txg == tx->tx_txg) {
1576 ASSERT(!scn->scn_pausing);
1577 /* finished with scan. */
1578 zfs_dbgmsg("txg %llu scan complete", tx->tx_txg);
1579 dsl_scan_done(scn, B_TRUE, tx);
1580 ASSERT3U(spa->spa_scrub_inflight, ==, 0);
1581 dsl_scan_sync_state(scn, tx);
1582 return;
1583 }
1584
428870ff
BB
1585 if (scn->scn_phys.scn_ddt_bookmark.ddb_class <=
1586 scn->scn_phys.scn_ddt_class_max) {
1587 zfs_dbgmsg("doing scan sync txg %llu; "
1588 "ddt bm=%llu/%llu/%llu/%llx",
1589 (longlong_t)tx->tx_txg,
1590 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_class,
1591 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_type,
1592 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_checksum,
1593 (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_cursor);
1594 ASSERT(scn->scn_phys.scn_bookmark.zb_objset == 0);
1595 ASSERT(scn->scn_phys.scn_bookmark.zb_object == 0);
1596 ASSERT(scn->scn_phys.scn_bookmark.zb_level == 0);
1597 ASSERT(scn->scn_phys.scn_bookmark.zb_blkid == 0);
1598 } else {
1599 zfs_dbgmsg("doing scan sync txg %llu; bm=%llu/%llu/%llu/%llu",
1600 (longlong_t)tx->tx_txg,
1601 (longlong_t)scn->scn_phys.scn_bookmark.zb_objset,
1602 (longlong_t)scn->scn_phys.scn_bookmark.zb_object,
1603 (longlong_t)scn->scn_phys.scn_bookmark.zb_level,
1604 (longlong_t)scn->scn_phys.scn_bookmark.zb_blkid);
1605 }
1606
1607 scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
1608 NULL, ZIO_FLAG_CANFAIL);
13fe0198 1609 dsl_pool_config_enter(dp, FTAG);
428870ff 1610 dsl_scan_visit(scn, tx);
13fe0198 1611 dsl_pool_config_exit(dp, FTAG);
428870ff
BB
1612 (void) zio_wait(scn->scn_zio_root);
1613 scn->scn_zio_root = NULL;
1614
1615 zfs_dbgmsg("visited %llu blocks in %llums",
1616 (longlong_t)scn->scn_visited_this_txg,
63fd3c6c 1617 (longlong_t)NSEC2MSEC(gethrtime() - scn->scn_sync_start_time));
428870ff
BB
1618
1619 if (!scn->scn_pausing) {
5d1f7fb6
GW
1620 scn->scn_done_txg = tx->tx_txg + 1;
1621 zfs_dbgmsg("txg %llu traversal complete, waiting till txg %llu",
1622 tx->tx_txg, scn->scn_done_txg);
428870ff
BB
1623 }
1624
1625 if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
1626 mutex_enter(&spa->spa_scrub_lock);
1627 while (spa->spa_scrub_inflight > 0) {
1628 cv_wait(&spa->spa_scrub_io_cv,
1629 &spa->spa_scrub_lock);
1630 }
1631 mutex_exit(&spa->spa_scrub_lock);
1632 }
1633
1634 dsl_scan_sync_state(scn, tx);
1635}
1636
1637/*
1638 * This will start a new scan, or restart an existing one.
1639 */
1640void
1641dsl_resilver_restart(dsl_pool_t *dp, uint64_t txg)
1642{
1643 if (txg == 0) {
1644 dmu_tx_t *tx;
1645 tx = dmu_tx_create_dd(dp->dp_mos_dir);
1646 VERIFY(0 == dmu_tx_assign(tx, TXG_WAIT));
1647
1648 txg = dmu_tx_get_txg(tx);
1649 dp->dp_scan->scn_restart_txg = txg;
1650 dmu_tx_commit(tx);
1651 } else {
1652 dp->dp_scan->scn_restart_txg = txg;
1653 }
1654 zfs_dbgmsg("restarting resilver txg=%llu", txg);
1655}
1656
1657boolean_t
1658dsl_scan_resilvering(dsl_pool_t *dp)
1659{
1660 return (dp->dp_scan->scn_phys.scn_state == DSS_SCANNING &&
1661 dp->dp_scan->scn_phys.scn_func == POOL_SCAN_RESILVER);
1662}
1663
1664/*
1665 * scrub consumers
1666 */
1667
1668static void
1669count_block(zfs_all_blkstats_t *zab, const blkptr_t *bp)
1670{
1671 int i;
1672
1673 /*
1674 * If we resume after a reboot, zab will be NULL; don't record
1675 * incomplete stats in that case.
1676 */
1677 if (zab == NULL)
1678 return;
1679
1680 for (i = 0; i < 4; i++) {
1681 int l = (i < 2) ? BP_GET_LEVEL(bp) : DN_MAX_LEVELS;
1682 int t = (i & 1) ? BP_GET_TYPE(bp) : DMU_OT_TOTAL;
428870ff 1683 int equal;
9ae529ec
CS
1684 zfs_blkstat_t *zb;
1685
1686 if (t & DMU_OT_NEWTYPE)
1687 t = DMU_OT_OTHER;
428870ff 1688
9ae529ec 1689 zb = &zab->zab_type[l][t];
428870ff
BB
1690 zb->zb_count++;
1691 zb->zb_asize += BP_GET_ASIZE(bp);
1692 zb->zb_lsize += BP_GET_LSIZE(bp);
1693 zb->zb_psize += BP_GET_PSIZE(bp);
1694 zb->zb_gangs += BP_COUNT_GANG(bp);
1695
1696 switch (BP_GET_NDVAS(bp)) {
1697 case 2:
1698 if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1699 DVA_GET_VDEV(&bp->blk_dva[1]))
1700 zb->zb_ditto_2_of_2_samevdev++;
1701 break;
1702 case 3:
1703 equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1704 DVA_GET_VDEV(&bp->blk_dva[1])) +
1705 (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1706 DVA_GET_VDEV(&bp->blk_dva[2])) +
1707 (DVA_GET_VDEV(&bp->blk_dva[1]) ==
1708 DVA_GET_VDEV(&bp->blk_dva[2]));
1709 if (equal == 1)
1710 zb->zb_ditto_2_of_3_samevdev++;
1711 else if (equal == 3)
1712 zb->zb_ditto_3_of_3_samevdev++;
1713 break;
1714 }
1715 }
1716}
1717
1718static void
1719dsl_scan_scrub_done(zio_t *zio)
1720{
1721 spa_t *spa = zio->io_spa;
1722
1723 zio_data_buf_free(zio->io_data, zio->io_size);
1724
1725 mutex_enter(&spa->spa_scrub_lock);
1726 spa->spa_scrub_inflight--;
1727 cv_broadcast(&spa->spa_scrub_io_cv);
1728
1729 if (zio->io_error && (zio->io_error != ECKSUM ||
1730 !(zio->io_flags & ZIO_FLAG_SPECULATIVE))) {
1731 spa->spa_dsl_pool->dp_scan->scn_phys.scn_errors++;
1732 }
1733 mutex_exit(&spa->spa_scrub_lock);
1734}
1735
1736static int
1737dsl_scan_scrub_cb(dsl_pool_t *dp,
1738 const blkptr_t *bp, const zbookmark_t *zb)
1739{
1740 dsl_scan_t *scn = dp->dp_scan;
1741 size_t size = BP_GET_PSIZE(bp);
1742 spa_t *spa = dp->dp_spa;
1743 uint64_t phys_birth = BP_PHYSICAL_BIRTH(bp);
d6320ddb 1744 boolean_t needs_io = B_FALSE;
572e2857 1745 int zio_flags = ZIO_FLAG_SCAN_THREAD | ZIO_FLAG_RAW | ZIO_FLAG_CANFAIL;
572e2857 1746 int scan_delay = 0;
d6320ddb 1747 int d;
428870ff
BB
1748
1749 if (phys_birth <= scn->scn_phys.scn_min_txg ||
1750 phys_birth >= scn->scn_phys.scn_max_txg)
1751 return (0);
1752
1753 count_block(dp->dp_blkstats, bp);
1754
9b67f605
MA
1755 if (BP_IS_EMBEDDED(bp))
1756 return (0);
1757
428870ff
BB
1758 ASSERT(DSL_SCAN_IS_SCRUB_RESILVER(scn));
1759 if (scn->scn_phys.scn_func == POOL_SCAN_SCRUB) {
1760 zio_flags |= ZIO_FLAG_SCRUB;
428870ff 1761 needs_io = B_TRUE;
572e2857 1762 scan_delay = zfs_scrub_delay;
a117a6d6
GW
1763 } else {
1764 ASSERT3U(scn->scn_phys.scn_func, ==, POOL_SCAN_RESILVER);
428870ff 1765 zio_flags |= ZIO_FLAG_RESILVER;
428870ff 1766 needs_io = B_FALSE;
572e2857 1767 scan_delay = zfs_resilver_delay;
428870ff
BB
1768 }
1769
1770 /* If it's an intent log block, failure is expected. */
1771 if (zb->zb_level == ZB_ZIL_LEVEL)
1772 zio_flags |= ZIO_FLAG_SPECULATIVE;
1773
d6320ddb 1774 for (d = 0; d < BP_GET_NDVAS(bp); d++) {
428870ff
BB
1775 vdev_t *vd = vdev_lookup_top(spa,
1776 DVA_GET_VDEV(&bp->blk_dva[d]));
1777
1778 /*
1779 * Keep track of how much data we've examined so that
1780 * zpool(1M) status can make useful progress reports.
1781 */
1782 scn->scn_phys.scn_examined += DVA_GET_ASIZE(&bp->blk_dva[d]);
1783 spa->spa_scan_pass_exam += DVA_GET_ASIZE(&bp->blk_dva[d]);
1784
1785 /* if it's a resilver, this may not be in the target range */
1786 if (!needs_io) {
1787 if (DVA_GET_GANG(&bp->blk_dva[d])) {
1788 /*
1789 * Gang members may be spread across multiple
1790 * vdevs, so the best estimate we have is the
1791 * scrub range, which has already been checked.
1792 * XXX -- it would be better to change our
1793 * allocation policy to ensure that all
1794 * gang members reside on the same vdev.
1795 */
1796 needs_io = B_TRUE;
1797 } else {
1798 needs_io = vdev_dtl_contains(vd, DTL_PARTIAL,
1799 phys_birth, 1);
1800 }
1801 }
1802 }
1803
1804 if (needs_io && !zfs_no_scrub_io) {
572e2857
BB
1805 vdev_t *rvd = spa->spa_root_vdev;
1806 uint64_t maxinflight = rvd->vdev_children * zfs_top_maxinflight;
428870ff
BB
1807 void *data = zio_data_buf_alloc(size);
1808
1809 mutex_enter(&spa->spa_scrub_lock);
572e2857 1810 while (spa->spa_scrub_inflight >= maxinflight)
428870ff
BB
1811 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
1812 spa->spa_scrub_inflight++;
1813 mutex_exit(&spa->spa_scrub_lock);
1814
572e2857
BB
1815 /*
1816 * If we're seeing recent (zfs_scan_idle) "important" I/Os
1817 * then throttle our workload to limit the impact of a scan.
1818 */
1819 if (ddi_get_lbolt64() - spa->spa_last_io <= zfs_scan_idle)
1820 delay(scan_delay);
1821
428870ff 1822 zio_nowait(zio_read(NULL, spa, bp, data, size,
e8b96c60 1823 dsl_scan_scrub_done, NULL, ZIO_PRIORITY_SCRUB,
428870ff
BB
1824 zio_flags, zb));
1825 }
1826
1827 /* do not relocate this block */
1828 return (0);
1829}
1830
1831int
1832dsl_scan(dsl_pool_t *dp, pool_scan_func_t func)
1833{
1834 spa_t *spa = dp->dp_spa;
1835
1836 /*
1837 * Purge all vdev caches and probe all devices. We do this here
1838 * rather than in sync context because this requires a writer lock
1839 * on the spa_config lock, which we can't do from sync context. The
1840 * spa_scrub_reopen flag indicates that vdev_open() should not
1841 * attempt to start another scrub.
1842 */
1843 spa_vdev_state_enter(spa, SCL_NONE);
1844 spa->spa_scrub_reopen = B_TRUE;
1845 vdev_reopen(spa->spa_root_vdev);
1846 spa->spa_scrub_reopen = B_FALSE;
1847 (void) spa_vdev_state_exit(spa, NULL, 0);
1848
13fe0198
MA
1849 return (dsl_sync_task(spa_name(spa), dsl_scan_setup_check,
1850 dsl_scan_setup_sync, &func, 0));
428870ff 1851}
c409e464
BB
1852
1853#if defined(_KERNEL) && defined(HAVE_SPL)
1854module_param(zfs_top_maxinflight, int, 0644);
1855MODULE_PARM_DESC(zfs_top_maxinflight, "Max I/Os per top-level");
1856
1857module_param(zfs_resilver_delay, int, 0644);
1858MODULE_PARM_DESC(zfs_resilver_delay, "Number of ticks to delay resilver");
1859
1860module_param(zfs_scrub_delay, int, 0644);
1861MODULE_PARM_DESC(zfs_scrub_delay, "Number of ticks to delay scrub");
1862
1863module_param(zfs_scan_idle, int, 0644);
1864MODULE_PARM_DESC(zfs_scan_idle, "Idle window in clock ticks");
1865
1866module_param(zfs_scan_min_time_ms, int, 0644);
1867MODULE_PARM_DESC(zfs_scan_min_time_ms, "Min millisecs to scrub per txg");
1868
1869module_param(zfs_free_min_time_ms, int, 0644);
1870MODULE_PARM_DESC(zfs_free_min_time_ms, "Min millisecs to free per txg");
1871
1872module_param(zfs_resilver_min_time_ms, int, 0644);
1873MODULE_PARM_DESC(zfs_resilver_min_time_ms, "Min millisecs to resilver per txg");
1874
1875module_param(zfs_no_scrub_io, int, 0644);
1876MODULE_PARM_DESC(zfs_no_scrub_io, "Set to disable scrub I/O");
1877
1878module_param(zfs_no_scrub_prefetch, int, 0644);
1879MODULE_PARM_DESC(zfs_no_scrub_prefetch, "Set to disable scrub prefetching");
c409e464 1880#endif