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