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