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