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