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