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