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