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