]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/dsl_scan.c
MFC 248571,248976,249004,249042,249188,249195-249196,249206,249207,249319,
[FreeBSD/stable/9.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) 2013 by Delphix. All rights reserved.
24  */
25
26 #include <sys/dsl_scan.h>
27 #include <sys/dsl_pool.h>
28 #include <sys/dsl_dataset.h>
29 #include <sys/dsl_prop.h>
30 #include <sys/dsl_dir.h>
31 #include <sys/dsl_synctask.h>
32 #include <sys/dnode.h>
33 #include <sys/dmu_tx.h>
34 #include <sys/dmu_objset.h>
35 #include <sys/arc.h>
36 #include <sys/zap.h>
37 #include <sys/zio.h>
38 #include <sys/zfs_context.h>
39 #include <sys/fs/zfs.h>
40 #include <sys/zfs_znode.h>
41 #include <sys/spa_impl.h>
42 #include <sys/vdev_impl.h>
43 #include <sys/zil_impl.h>
44 #include <sys/zio_checksum.h>
45 #include <sys/ddt.h>
46 #include <sys/sa.h>
47 #include <sys/sa_impl.h>
48 #include <sys/zfeature.h>
49 #ifdef _KERNEL
50 #include <sys/zfs_vfsops.h>
51 #endif
52
53 typedef int (scan_cb_t)(dsl_pool_t *, const blkptr_t *, const zbookmark_t *);
54
55 static scan_cb_t dsl_scan_defrag_cb;
56 static scan_cb_t dsl_scan_scrub_cb;
57 static scan_cb_t dsl_scan_remove_cb;
58 static void dsl_scan_cancel_sync(void *, dmu_tx_t *);
59 static void dsl_scan_sync_state(dsl_scan_t *, dmu_tx_t *tx);
60
61 unsigned int zfs_top_maxinflight = 32;  /* maximum I/Os per top-level */
62 unsigned int zfs_resilver_delay = 2;    /* number of ticks to delay resilver */
63 unsigned int zfs_scrub_delay = 4;       /* number of ticks to delay scrub */
64 unsigned int zfs_scan_idle = 50;        /* idle window in clock ticks */
65
66 unsigned int zfs_scan_min_time_ms = 1000; /* min millisecs to scrub per txg */
67 unsigned int zfs_free_min_time_ms = 1000; /* min millisecs to free per txg */
68 unsigned int zfs_resilver_min_time_ms = 3000; /* min millisecs to resilver
69                                                  per txg */
70 boolean_t zfs_no_scrub_io = B_FALSE; /* set to disable scrub i/o */
71 boolean_t zfs_no_scrub_prefetch = B_FALSE; /* set to disable srub prefetching */
72
73 SYSCTL_DECL(_vfs_zfs);
74 TUNABLE_INT("vfs.zfs.top_maxinflight", &zfs_top_maxinflight);
75 SYSCTL_UINT(_vfs_zfs, OID_AUTO, top_maxinflight, CTLFLAG_RW,
76     &zfs_top_maxinflight, 0, "Maximum I/Os per top-level vdev");
77 TUNABLE_INT("vfs.zfs.resilver_delay", &zfs_resilver_delay);
78 SYSCTL_UINT(_vfs_zfs, OID_AUTO, resilver_delay, CTLFLAG_RW,
79     &zfs_resilver_delay, 0, "Number of ticks to delay resilver");
80 TUNABLE_INT("vfs.zfs.scrub_delay", &zfs_scrub_delay);
81 SYSCTL_UINT(_vfs_zfs, OID_AUTO, scrub_delay, CTLFLAG_RW,
82     &zfs_scrub_delay, 0, "Number of ticks to delay scrub");
83 TUNABLE_INT("vfs.zfs.scan_idle", &zfs_scan_idle);
84 SYSCTL_UINT(_vfs_zfs, OID_AUTO, scan_idle, CTLFLAG_RW,
85     &zfs_scan_idle, 0, "Idle scan window in clock ticks");
86 TUNABLE_INT("vfs.zfs.scan_min_time_ms", &zfs_scan_min_time_ms);
87 SYSCTL_UINT(_vfs_zfs, OID_AUTO, scan_min_time_ms, CTLFLAG_RW,
88     &zfs_scan_min_time_ms, 0, "Min millisecs to scrub per txg");
89 TUNABLE_INT("vfs.zfs.free_min_time_ms", &zfs_free_min_time_ms);
90 SYSCTL_UINT(_vfs_zfs, OID_AUTO, free_min_time_ms, CTLFLAG_RW,
91     &zfs_free_min_time_ms, 0, "Min millisecs to free per txg");
92 TUNABLE_INT("vfs.zfs.resilver_min_time_ms", &zfs_resilver_min_time_ms);
93 SYSCTL_UINT(_vfs_zfs, OID_AUTO, resilver_min_time_ms, CTLFLAG_RW,
94     &zfs_resilver_min_time_ms, 0, "Min millisecs to resilver per txg");
95 TUNABLE_INT("vfs.zfs.no_scrub_io", &zfs_no_scrub_io);
96 SYSCTL_INT(_vfs_zfs, OID_AUTO, no_scrub_io, CTLFLAG_RW,
97     &zfs_no_scrub_io, 0, "Disable scrub I/O");
98 TUNABLE_INT("vfs.zfs.no_scrub_prefetch", &zfs_no_scrub_prefetch);
99 SYSCTL_INT(_vfs_zfs, OID_AUTO, no_scrub_prefetch, CTLFLAG_RW,
100     &zfs_no_scrub_prefetch, 0, "Disable scrub prefetching");
101
102 enum ddt_class zfs_scrub_ddt_class_max = DDT_CLASS_DUPLICATE;
103
104 #define DSL_SCAN_IS_SCRUB_RESILVER(scn) \
105         ((scn)->scn_phys.scn_func == POOL_SCAN_SCRUB || \
106         (scn)->scn_phys.scn_func == POOL_SCAN_RESILVER)
107
108 extern int zfs_txg_timeout;
109
110 /* the order has to match pool_scan_type */
111 static scan_cb_t *scan_funcs[POOL_SCAN_FUNCS] = {
112         NULL,
113         dsl_scan_scrub_cb,      /* POOL_SCAN_SCRUB */
114         dsl_scan_scrub_cb,      /* POOL_SCAN_RESILVER */
115 };
116
117 int
118 dsl_scan_init(dsl_pool_t *dp, uint64_t txg)
119 {
120         int err;
121         dsl_scan_t *scn;
122         spa_t *spa = dp->dp_spa;
123         uint64_t f;
124
125         scn = dp->dp_scan = kmem_zalloc(sizeof (dsl_scan_t), KM_SLEEP);
126         scn->scn_dp = dp;
127
128         err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
129             "scrub_func", sizeof (uint64_t), 1, &f);
130         if (err == 0) {
131                 /*
132                  * There was an old-style scrub in progress.  Restart a
133                  * new-style scrub from the beginning.
134                  */
135                 scn->scn_restart_txg = txg;
136                 zfs_dbgmsg("old-style scrub was in progress; "
137                     "restarting new-style scrub in txg %llu",
138                     scn->scn_restart_txg);
139
140                 /*
141                  * Load the queue obj from the old location so that it
142                  * can be freed by dsl_scan_done().
143                  */
144                 (void) zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
145                     "scrub_queue", sizeof (uint64_t), 1,
146                     &scn->scn_phys.scn_queue_obj);
147         } else {
148                 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
149                     DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS,
150                     &scn->scn_phys);
151                 if (err == ENOENT)
152                         return (0);
153                 else if (err)
154                         return (err);
155
156                 if (scn->scn_phys.scn_state == DSS_SCANNING &&
157                     spa_prev_software_version(dp->dp_spa) < SPA_VERSION_SCAN) {
158                         /*
159                          * A new-type scrub was in progress on an old
160                          * pool, and the pool was accessed by old
161                          * software.  Restart from the beginning, since
162                          * the old software may have changed the pool in
163                          * the meantime.
164                          */
165                         scn->scn_restart_txg = txg;
166                         zfs_dbgmsg("new-style scrub was modified "
167                             "by old software; restarting in txg %llu",
168                             scn->scn_restart_txg);
169                 }
170         }
171
172         spa_scan_stat_init(spa);
173         return (0);
174 }
175
176 void
177 dsl_scan_fini(dsl_pool_t *dp)
178 {
179         if (dp->dp_scan) {
180                 kmem_free(dp->dp_scan, sizeof (dsl_scan_t));
181                 dp->dp_scan = NULL;
182         }
183 }
184
185 /* ARGSUSED */
186 static int
187 dsl_scan_setup_check(void *arg, dmu_tx_t *tx)
188 {
189         dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
190
191         if (scn->scn_phys.scn_state == DSS_SCANNING)
192                 return (SET_ERROR(EBUSY));
193
194         return (0);
195 }
196
197 static void
198 dsl_scan_setup_sync(void *arg, dmu_tx_t *tx)
199 {
200         dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
201         pool_scan_func_t *funcp = arg;
202         dmu_object_type_t ot = 0;
203         dsl_pool_t *dp = scn->scn_dp;
204         spa_t *spa = dp->dp_spa;
205
206         ASSERT(scn->scn_phys.scn_state != DSS_SCANNING);
207         ASSERT(*funcp > POOL_SCAN_NONE && *funcp < POOL_SCAN_FUNCS);
208         bzero(&scn->scn_phys, sizeof (scn->scn_phys));
209         scn->scn_phys.scn_func = *funcp;
210         scn->scn_phys.scn_state = DSS_SCANNING;
211         scn->scn_phys.scn_min_txg = 0;
212         scn->scn_phys.scn_max_txg = tx->tx_txg;
213         scn->scn_phys.scn_ddt_class_max = DDT_CLASSES - 1; /* the entire DDT */
214         scn->scn_phys.scn_start_time = gethrestime_sec();
215         scn->scn_phys.scn_errors = 0;
216         scn->scn_phys.scn_to_examine = spa->spa_root_vdev->vdev_stat.vs_alloc;
217         scn->scn_restart_txg = 0;
218         spa_scan_stat_init(spa);
219
220         if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
221                 scn->scn_phys.scn_ddt_class_max = zfs_scrub_ddt_class_max;
222
223                 /* rewrite all disk labels */
224                 vdev_config_dirty(spa->spa_root_vdev);
225
226                 if (vdev_resilver_needed(spa->spa_root_vdev,
227                     &scn->scn_phys.scn_min_txg, &scn->scn_phys.scn_max_txg)) {
228                         spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_START);
229                 } else {
230                         spa_event_notify(spa, NULL, ESC_ZFS_SCRUB_START);
231                 }
232
233                 spa->spa_scrub_started = B_TRUE;
234                 /*
235                  * If this is an incremental scrub, limit the DDT scrub phase
236                  * to just the auto-ditto class (for correctness); the rest
237                  * of the scrub should go faster using top-down pruning.
238                  */
239                 if (scn->scn_phys.scn_min_txg > TXG_INITIAL)
240                         scn->scn_phys.scn_ddt_class_max = DDT_CLASS_DITTO;
241
242         }
243
244         /* back to the generic stuff */
245
246         if (dp->dp_blkstats == NULL) {
247                 dp->dp_blkstats =
248                     kmem_alloc(sizeof (zfs_all_blkstats_t), KM_SLEEP);
249         }
250         bzero(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
251
252         if (spa_version(spa) < SPA_VERSION_DSL_SCRUB)
253                 ot = DMU_OT_ZAP_OTHER;
254
255         scn->scn_phys.scn_queue_obj = zap_create(dp->dp_meta_objset,
256             ot ? ot : DMU_OT_SCAN_QUEUE, DMU_OT_NONE, 0, tx);
257
258         dsl_scan_sync_state(scn, tx);
259
260         spa_history_log_internal(spa, "scan setup", tx,
261             "func=%u mintxg=%llu maxtxg=%llu",
262             *funcp, scn->scn_phys.scn_min_txg, scn->scn_phys.scn_max_txg);
263 }
264
265 /* ARGSUSED */
266 static void
267 dsl_scan_done(dsl_scan_t *scn, boolean_t complete, dmu_tx_t *tx)
268 {
269         static const char *old_names[] = {
270                 "scrub_bookmark",
271                 "scrub_ddt_bookmark",
272                 "scrub_ddt_class_max",
273                 "scrub_queue",
274                 "scrub_min_txg",
275                 "scrub_max_txg",
276                 "scrub_func",
277                 "scrub_errors",
278                 NULL
279         };
280
281         dsl_pool_t *dp = scn->scn_dp;
282         spa_t *spa = dp->dp_spa;
283         int i;
284
285         /* Remove any remnants of an old-style scrub. */
286         for (i = 0; old_names[i]; i++) {
287                 (void) zap_remove(dp->dp_meta_objset,
288                     DMU_POOL_DIRECTORY_OBJECT, old_names[i], tx);
289         }
290
291         if (scn->scn_phys.scn_queue_obj != 0) {
292                 VERIFY(0 == dmu_object_free(dp->dp_meta_objset,
293                     scn->scn_phys.scn_queue_obj, tx));
294                 scn->scn_phys.scn_queue_obj = 0;
295         }
296
297         /*
298          * If we were "restarted" from a stopped state, don't bother
299          * with anything else.
300          */
301         if (scn->scn_phys.scn_state != DSS_SCANNING)
302                 return;
303
304         if (complete)
305                 scn->scn_phys.scn_state = DSS_FINISHED;
306         else
307                 scn->scn_phys.scn_state = DSS_CANCELED;
308
309         spa_history_log_internal(spa, "scan done", tx,
310             "complete=%u", complete);
311
312         if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
313                 mutex_enter(&spa->spa_scrub_lock);
314                 while (spa->spa_scrub_inflight > 0) {
315                         cv_wait(&spa->spa_scrub_io_cv,
316                             &spa->spa_scrub_lock);
317                 }
318                 mutex_exit(&spa->spa_scrub_lock);
319                 spa->spa_scrub_started = B_FALSE;
320                 spa->spa_scrub_active = B_FALSE;
321
322                 /*
323                  * If the scrub/resilver completed, update all DTLs to
324                  * reflect this.  Whether it succeeded or not, vacate
325                  * all temporary scrub DTLs.
326                  */
327                 vdev_dtl_reassess(spa->spa_root_vdev, tx->tx_txg,
328                     complete ? scn->scn_phys.scn_max_txg : 0, B_TRUE);
329                 if (complete) {
330                         spa_event_notify(spa, NULL, scn->scn_phys.scn_min_txg ?
331                             ESC_ZFS_RESILVER_FINISH : ESC_ZFS_SCRUB_FINISH);
332                 }
333                 spa_errlog_rotate(spa);
334
335                 /*
336                  * We may have finished replacing a device.
337                  * Let the async thread assess this and handle the detach.
338                  */
339                 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
340         }
341
342         scn->scn_phys.scn_end_time = gethrestime_sec();
343 }
344
345 /* ARGSUSED */
346 static int
347 dsl_scan_cancel_check(void *arg, dmu_tx_t *tx)
348 {
349         dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
350
351         if (scn->scn_phys.scn_state != DSS_SCANNING)
352                 return (SET_ERROR(ENOENT));
353         return (0);
354 }
355
356 /* ARGSUSED */
357 static void
358 dsl_scan_cancel_sync(void *arg, dmu_tx_t *tx)
359 {
360         dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
361
362         dsl_scan_done(scn, B_FALSE, tx);
363         dsl_scan_sync_state(scn, tx);
364 }
365
366 int
367 dsl_scan_cancel(dsl_pool_t *dp)
368 {
369         return (dsl_sync_task(spa_name(dp->dp_spa), dsl_scan_cancel_check,
370             dsl_scan_cancel_sync, NULL, 3));
371 }
372
373 static void dsl_scan_visitbp(blkptr_t *bp,
374     const zbookmark_t *zb, dnode_phys_t *dnp, arc_buf_t *pbuf,
375     dsl_dataset_t *ds, dsl_scan_t *scn, dmu_objset_type_t ostype,
376     dmu_tx_t *tx);
377 static void dsl_scan_visitdnode(dsl_scan_t *, dsl_dataset_t *ds,
378     dmu_objset_type_t ostype,
379     dnode_phys_t *dnp, arc_buf_t *buf, uint64_t object, dmu_tx_t *tx);
380
381 void
382 dsl_free(dsl_pool_t *dp, uint64_t txg, const blkptr_t *bp)
383 {
384         zio_free(dp->dp_spa, txg, bp);
385 }
386
387 void
388 dsl_free_sync(zio_t *pio, dsl_pool_t *dp, uint64_t txg, const blkptr_t *bpp)
389 {
390         ASSERT(dsl_pool_sync_context(dp));
391         zio_nowait(zio_free_sync(pio, dp->dp_spa, txg, bpp, pio->io_flags));
392 }
393
394 static uint64_t
395 dsl_scan_ds_maxtxg(dsl_dataset_t *ds)
396 {
397         uint64_t smt = ds->ds_dir->dd_pool->dp_scan->scn_phys.scn_max_txg;
398         if (dsl_dataset_is_snapshot(ds))
399                 return (MIN(smt, ds->ds_phys->ds_creation_txg));
400         return (smt);
401 }
402
403 static void
404 dsl_scan_sync_state(dsl_scan_t *scn, dmu_tx_t *tx)
405 {
406         VERIFY0(zap_update(scn->scn_dp->dp_meta_objset,
407             DMU_POOL_DIRECTORY_OBJECT,
408             DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS,
409             &scn->scn_phys, tx));
410 }
411
412 static boolean_t
413 dsl_scan_check_pause(dsl_scan_t *scn, const zbookmark_t *zb)
414 {
415         uint64_t elapsed_nanosecs;
416         unsigned int mintime;
417
418         /* we never skip user/group accounting objects */
419         if (zb && (int64_t)zb->zb_object < 0)
420                 return (B_FALSE);
421
422         if (scn->scn_pausing)
423                 return (B_TRUE); /* we're already pausing */
424
425         if (!ZB_IS_ZERO(&scn->scn_phys.scn_bookmark))
426                 return (B_FALSE); /* we're resuming */
427
428         /* We only know how to resume from level-0 blocks. */
429         if (zb && zb->zb_level != 0)
430                 return (B_FALSE);
431
432         mintime = (scn->scn_phys.scn_func == POOL_SCAN_RESILVER) ?
433             zfs_resilver_min_time_ms : zfs_scan_min_time_ms;
434         elapsed_nanosecs = gethrtime() - scn->scn_sync_start_time;
435         if (elapsed_nanosecs / NANOSEC > zfs_txg_timeout ||
436             (elapsed_nanosecs / MICROSEC > mintime &&
437             txg_sync_waiting(scn->scn_dp)) ||
438             spa_shutting_down(scn->scn_dp->dp_spa)) {
439                 if (zb) {
440                         dprintf("pausing at bookmark %llx/%llx/%llx/%llx\n",
441                             (longlong_t)zb->zb_objset,
442                             (longlong_t)zb->zb_object,
443                             (longlong_t)zb->zb_level,
444                             (longlong_t)zb->zb_blkid);
445                         scn->scn_phys.scn_bookmark = *zb;
446                 }
447                 dprintf("pausing at DDT bookmark %llx/%llx/%llx/%llx\n",
448                     (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_class,
449                     (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_type,
450                     (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_checksum,
451                     (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_cursor);
452                 scn->scn_pausing = B_TRUE;
453                 return (B_TRUE);
454         }
455         return (B_FALSE);
456 }
457
458 typedef struct zil_scan_arg {
459         dsl_pool_t      *zsa_dp;
460         zil_header_t    *zsa_zh;
461 } zil_scan_arg_t;
462
463 /* ARGSUSED */
464 static int
465 dsl_scan_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
466 {
467         zil_scan_arg_t *zsa = arg;
468         dsl_pool_t *dp = zsa->zsa_dp;
469         dsl_scan_t *scn = dp->dp_scan;
470         zil_header_t *zh = zsa->zsa_zh;
471         zbookmark_t zb;
472
473         if (bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
474                 return (0);
475
476         /*
477          * One block ("stubby") can be allocated a long time ago; we
478          * want to visit that one because it has been allocated
479          * (on-disk) even if it hasn't been claimed (even though for
480          * scrub there's nothing to do to it).
481          */
482         if (claim_txg == 0 && bp->blk_birth >= spa_first_txg(dp->dp_spa))
483                 return (0);
484
485         SET_BOOKMARK(&zb, zh->zh_log.blk_cksum.zc_word[ZIL_ZC_OBJSET],
486             ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
487
488         VERIFY(0 == scan_funcs[scn->scn_phys.scn_func](dp, bp, &zb));
489         return (0);
490 }
491
492 /* ARGSUSED */
493 static int
494 dsl_scan_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg)
495 {
496         if (lrc->lrc_txtype == TX_WRITE) {
497                 zil_scan_arg_t *zsa = arg;
498                 dsl_pool_t *dp = zsa->zsa_dp;
499                 dsl_scan_t *scn = dp->dp_scan;
500                 zil_header_t *zh = zsa->zsa_zh;
501                 lr_write_t *lr = (lr_write_t *)lrc;
502                 blkptr_t *bp = &lr->lr_blkptr;
503                 zbookmark_t zb;
504
505                 if (bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
506                         return (0);
507
508                 /*
509                  * birth can be < claim_txg if this record's txg is
510                  * already txg sync'ed (but this log block contains
511                  * other records that are not synced)
512                  */
513                 if (claim_txg == 0 || bp->blk_birth < claim_txg)
514                         return (0);
515
516                 SET_BOOKMARK(&zb, zh->zh_log.blk_cksum.zc_word[ZIL_ZC_OBJSET],
517                     lr->lr_foid, ZB_ZIL_LEVEL,
518                     lr->lr_offset / BP_GET_LSIZE(bp));
519
520                 VERIFY(0 == scan_funcs[scn->scn_phys.scn_func](dp, bp, &zb));
521         }
522         return (0);
523 }
524
525 static void
526 dsl_scan_zil(dsl_pool_t *dp, zil_header_t *zh)
527 {
528         uint64_t claim_txg = zh->zh_claim_txg;
529         zil_scan_arg_t zsa = { dp, zh };
530         zilog_t *zilog;
531
532         /*
533          * We only want to visit blocks that have been claimed but not yet
534          * replayed (or, in read-only mode, blocks that *would* be claimed).
535          */
536         if (claim_txg == 0 && spa_writeable(dp->dp_spa))
537                 return;
538
539         zilog = zil_alloc(dp->dp_meta_objset, zh);
540
541         (void) zil_parse(zilog, dsl_scan_zil_block, dsl_scan_zil_record, &zsa,
542             claim_txg);
543
544         zil_free(zilog);
545 }
546
547 /* ARGSUSED */
548 static void
549 dsl_scan_prefetch(dsl_scan_t *scn, arc_buf_t *buf, blkptr_t *bp,
550     uint64_t objset, uint64_t object, uint64_t blkid)
551 {
552         zbookmark_t czb;
553         uint32_t flags = ARC_NOWAIT | ARC_PREFETCH;
554
555         if (zfs_no_scrub_prefetch)
556                 return;
557
558         if (BP_IS_HOLE(bp) || bp->blk_birth <= scn->scn_phys.scn_min_txg ||
559             (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_DNODE))
560                 return;
561
562         SET_BOOKMARK(&czb, objset, object, BP_GET_LEVEL(bp), blkid);
563
564         (void) arc_read(scn->scn_zio_root, scn->scn_dp->dp_spa, bp,
565             NULL, NULL, ZIO_PRIORITY_ASYNC_READ,
566             ZIO_FLAG_CANFAIL | ZIO_FLAG_SCAN_THREAD, &flags, &czb);
567 }
568
569 static boolean_t
570 dsl_scan_check_resume(dsl_scan_t *scn, const dnode_phys_t *dnp,
571     const zbookmark_t *zb)
572 {
573         /*
574          * We never skip over user/group accounting objects (obj<0)
575          */
576         if (!ZB_IS_ZERO(&scn->scn_phys.scn_bookmark) &&
577             (int64_t)zb->zb_object >= 0) {
578                 /*
579                  * If we already visited this bp & everything below (in
580                  * a prior txg sync), don't bother doing it again.
581                  */
582                 if (zbookmark_is_before(dnp, zb, &scn->scn_phys.scn_bookmark))
583                         return (B_TRUE);
584
585                 /*
586                  * If we found the block we're trying to resume from, or
587                  * we went past it to a different object, zero it out to
588                  * indicate that it's OK to start checking for pausing
589                  * again.
590                  */
591                 if (bcmp(zb, &scn->scn_phys.scn_bookmark, sizeof (*zb)) == 0 ||
592                     zb->zb_object > scn->scn_phys.scn_bookmark.zb_object) {
593                         dprintf("resuming at %llx/%llx/%llx/%llx\n",
594                             (longlong_t)zb->zb_objset,
595                             (longlong_t)zb->zb_object,
596                             (longlong_t)zb->zb_level,
597                             (longlong_t)zb->zb_blkid);
598                         bzero(&scn->scn_phys.scn_bookmark, sizeof (*zb));
599                 }
600         }
601         return (B_FALSE);
602 }
603
604 /*
605  * Return nonzero on i/o error.
606  * Return new buf to write out in *bufp.
607  */
608 static int
609 dsl_scan_recurse(dsl_scan_t *scn, dsl_dataset_t *ds, dmu_objset_type_t ostype,
610     dnode_phys_t *dnp, const blkptr_t *bp,
611     const zbookmark_t *zb, dmu_tx_t *tx, arc_buf_t **bufp)
612 {
613         dsl_pool_t *dp = scn->scn_dp;
614         int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCAN_THREAD;
615         int err;
616
617         if (BP_GET_LEVEL(bp) > 0) {
618                 uint32_t flags = ARC_WAIT;
619                 int i;
620                 blkptr_t *cbp;
621                 int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
622
623                 err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, bufp,
624                     ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
625                 if (err) {
626                         scn->scn_phys.scn_errors++;
627                         return (err);
628                 }
629                 for (i = 0, cbp = (*bufp)->b_data; i < epb; i++, cbp++) {
630                         dsl_scan_prefetch(scn, *bufp, cbp, zb->zb_objset,
631                             zb->zb_object, zb->zb_blkid * epb + i);
632                 }
633                 for (i = 0, cbp = (*bufp)->b_data; i < epb; i++, cbp++) {
634                         zbookmark_t czb;
635
636                         SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
637                             zb->zb_level - 1,
638                             zb->zb_blkid * epb + i);
639                         dsl_scan_visitbp(cbp, &czb, dnp,
640                             *bufp, ds, scn, ostype, tx);
641                 }
642         } else if (BP_GET_TYPE(bp) == DMU_OT_USERGROUP_USED) {
643                 uint32_t flags = ARC_WAIT;
644
645                 err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, bufp,
646                     ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
647                 if (err) {
648                         scn->scn_phys.scn_errors++;
649                         return (err);
650                 }
651         } else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) {
652                 uint32_t flags = ARC_WAIT;
653                 dnode_phys_t *cdnp;
654                 int i, j;
655                 int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
656
657                 err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, bufp,
658                     ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
659                 if (err) {
660                         scn->scn_phys.scn_errors++;
661                         return (err);
662                 }
663                 for (i = 0, cdnp = (*bufp)->b_data; i < epb; i++, cdnp++) {
664                         for (j = 0; j < cdnp->dn_nblkptr; j++) {
665                                 blkptr_t *cbp = &cdnp->dn_blkptr[j];
666                                 dsl_scan_prefetch(scn, *bufp, cbp,
667                                     zb->zb_objset, zb->zb_blkid * epb + i, j);
668                         }
669                 }
670                 for (i = 0, cdnp = (*bufp)->b_data; i < epb; i++, cdnp++) {
671                         dsl_scan_visitdnode(scn, ds, ostype,
672                             cdnp, *bufp, zb->zb_blkid * epb + i, tx);
673                 }
674
675         } else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
676                 uint32_t flags = ARC_WAIT;
677                 objset_phys_t *osp;
678
679                 err = arc_read(NULL, dp->dp_spa, bp, 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(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg)
979 {
980         struct enqueue_clones_arg *eca = arg;
981         dsl_dataset_t *ds;
982         int err;
983         dsl_scan_t *scn = dp->dp_scan;
984
985         if (hds->ds_dir->dd_phys->dd_origin_obj != eca->originobj)
986                 return (0);
987
988         err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds);
989         if (err)
990                 return (err);
991
992         while (ds->ds_phys->ds_prev_snap_obj != eca->originobj) {
993                 dsl_dataset_t *prev;
994                 err = dsl_dataset_hold_obj(dp,
995                     ds->ds_phys->ds_prev_snap_obj, FTAG, &prev);
996
997                 dsl_dataset_rele(ds, FTAG);
998                 if (err)
999                         return (err);
1000                 ds = prev;
1001         }
1002         VERIFY(zap_add_int_key(dp->dp_meta_objset,
1003             scn->scn_phys.scn_queue_obj, ds->ds_object,
1004             ds->ds_phys->ds_prev_snap_txg, eca->tx) == 0);
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                         VERIFY0(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));
1097                 } else {
1098                         struct enqueue_clones_arg eca;
1099                         eca.tx = tx;
1100                         eca.originobj = ds->ds_object;
1101
1102                         VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
1103                             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(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg)
1114 {
1115         dmu_tx_t *tx = arg;
1116         dsl_dataset_t *ds;
1117         int err;
1118         dsl_scan_t *scn = dp->dp_scan;
1119
1120         err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds);
1121         if (err)
1122                 return (err);
1123
1124         while (ds->ds_phys->ds_prev_snap_obj != 0) {
1125                 dsl_dataset_t *prev;
1126                 err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
1127                     FTAG, &prev);
1128                 if (err) {
1129                         dsl_dataset_rele(ds, FTAG);
1130                         return (err);
1131                 }
1132
1133                 /*
1134                  * If this is a clone, we don't need to worry about it for now.
1135                  */
1136                 if (prev->ds_phys->ds_next_snap_obj != ds->ds_object) {
1137                         dsl_dataset_rele(ds, FTAG);
1138                         dsl_dataset_rele(prev, FTAG);
1139                         return (0);
1140                 }
1141                 dsl_dataset_rele(ds, FTAG);
1142                 ds = prev;
1143         }
1144
1145         VERIFY(zap_add_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj,
1146             ds->ds_object, ds->ds_phys->ds_prev_snap_txg, tx) == 0);
1147         dsl_dataset_rele(ds, FTAG);
1148         return (0);
1149 }
1150
1151 /*
1152  * Scrub/dedup interaction.
1153  *
1154  * If there are N references to a deduped block, we don't want to scrub it
1155  * N times -- ideally, we should scrub it exactly once.
1156  *
1157  * We leverage the fact that the dde's replication class (enum ddt_class)
1158  * is ordered from highest replication class (DDT_CLASS_DITTO) to lowest
1159  * (DDT_CLASS_UNIQUE) so that we may walk the DDT in that order.
1160  *
1161  * To prevent excess scrubbing, the scrub begins by walking the DDT
1162  * to find all blocks with refcnt > 1, and scrubs each of these once.
1163  * Since there are two replication classes which contain blocks with
1164  * refcnt > 1, we scrub the highest replication class (DDT_CLASS_DITTO) first.
1165  * Finally the top-down scrub begins, only visiting blocks with refcnt == 1.
1166  *
1167  * There would be nothing more to say if a block's refcnt couldn't change
1168  * during a scrub, but of course it can so we must account for changes
1169  * in a block's replication class.
1170  *
1171  * Here's an example of what can occur:
1172  *
1173  * If a block has refcnt > 1 during the DDT scrub phase, but has refcnt == 1
1174  * when visited during the top-down scrub phase, it will be scrubbed twice.
1175  * This negates our scrub optimization, but is otherwise harmless.
1176  *
1177  * If a block has refcnt == 1 during the DDT scrub phase, but has refcnt > 1
1178  * on each visit during the top-down scrub phase, it will never be scrubbed.
1179  * To catch this, ddt_sync_entry() notifies the scrub code whenever a block's
1180  * reference class transitions to a higher level (i.e DDT_CLASS_UNIQUE to
1181  * DDT_CLASS_DUPLICATE); if it transitions from refcnt == 1 to refcnt > 1
1182  * while a scrub is in progress, it scrubs the block right then.
1183  */
1184 static void
1185 dsl_scan_ddt(dsl_scan_t *scn, dmu_tx_t *tx)
1186 {
1187         ddt_bookmark_t *ddb = &scn->scn_phys.scn_ddt_bookmark;
1188         ddt_entry_t dde = { 0 };
1189         int error;
1190         uint64_t n = 0;
1191
1192         while ((error = ddt_walk(scn->scn_dp->dp_spa, ddb, &dde)) == 0) {
1193                 ddt_t *ddt;
1194
1195                 if (ddb->ddb_class > scn->scn_phys.scn_ddt_class_max)
1196                         break;
1197                 dprintf("visiting ddb=%llu/%llu/%llu/%llx\n",
1198                     (longlong_t)ddb->ddb_class,
1199                     (longlong_t)ddb->ddb_type,
1200                     (longlong_t)ddb->ddb_checksum,
1201                     (longlong_t)ddb->ddb_cursor);
1202
1203                 /* There should be no pending changes to the dedup table */
1204                 ddt = scn->scn_dp->dp_spa->spa_ddt[ddb->ddb_checksum];
1205                 ASSERT(avl_first(&ddt->ddt_tree) == NULL);
1206
1207                 dsl_scan_ddt_entry(scn, ddb->ddb_checksum, &dde, tx);
1208                 n++;
1209
1210                 if (dsl_scan_check_pause(scn, NULL))
1211                         break;
1212         }
1213
1214         zfs_dbgmsg("scanned %llu ddt entries with class_max = %u; pausing=%u",
1215             (longlong_t)n, (int)scn->scn_phys.scn_ddt_class_max,
1216             (int)scn->scn_pausing);
1217
1218         ASSERT(error == 0 || error == ENOENT);
1219         ASSERT(error != ENOENT ||
1220             ddb->ddb_class > scn->scn_phys.scn_ddt_class_max);
1221 }
1222
1223 /* ARGSUSED */
1224 void
1225 dsl_scan_ddt_entry(dsl_scan_t *scn, enum zio_checksum checksum,
1226     ddt_entry_t *dde, dmu_tx_t *tx)
1227 {
1228         const ddt_key_t *ddk = &dde->dde_key;
1229         ddt_phys_t *ddp = dde->dde_phys;
1230         blkptr_t bp;
1231         zbookmark_t zb = { 0 };
1232
1233         if (scn->scn_phys.scn_state != DSS_SCANNING)
1234                 return;
1235
1236         for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
1237                 if (ddp->ddp_phys_birth == 0 ||
1238                     ddp->ddp_phys_birth > scn->scn_phys.scn_cur_max_txg)
1239                         continue;
1240                 ddt_bp_create(checksum, ddk, ddp, &bp);
1241
1242                 scn->scn_visited_this_txg++;
1243                 scan_funcs[scn->scn_phys.scn_func](scn->scn_dp, &bp, &zb);
1244         }
1245 }
1246
1247 static void
1248 dsl_scan_visit(dsl_scan_t *scn, dmu_tx_t *tx)
1249 {
1250         dsl_pool_t *dp = scn->scn_dp;
1251         zap_cursor_t zc;
1252         zap_attribute_t za;
1253
1254         if (scn->scn_phys.scn_ddt_bookmark.ddb_class <=
1255             scn->scn_phys.scn_ddt_class_max) {
1256                 scn->scn_phys.scn_cur_min_txg = scn->scn_phys.scn_min_txg;
1257                 scn->scn_phys.scn_cur_max_txg = scn->scn_phys.scn_max_txg;
1258                 dsl_scan_ddt(scn, tx);
1259                 if (scn->scn_pausing)
1260                         return;
1261         }
1262
1263         if (scn->scn_phys.scn_bookmark.zb_objset == DMU_META_OBJSET) {
1264                 /* First do the MOS & ORIGIN */
1265
1266                 scn->scn_phys.scn_cur_min_txg = scn->scn_phys.scn_min_txg;
1267                 scn->scn_phys.scn_cur_max_txg = scn->scn_phys.scn_max_txg;
1268                 dsl_scan_visit_rootbp(scn, NULL,
1269                     &dp->dp_meta_rootbp, tx);
1270                 spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
1271                 if (scn->scn_pausing)
1272                         return;
1273
1274                 if (spa_version(dp->dp_spa) < SPA_VERSION_DSL_SCRUB) {
1275                         VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
1276                             enqueue_cb, tx, DS_FIND_CHILDREN));
1277                 } else {
1278                         dsl_scan_visitds(scn,
1279                             dp->dp_origin_snap->ds_object, tx);
1280                 }
1281                 ASSERT(!scn->scn_pausing);
1282         } else if (scn->scn_phys.scn_bookmark.zb_objset !=
1283             ZB_DESTROYED_OBJSET) {
1284                 /*
1285                  * If we were paused, continue from here.  Note if the
1286                  * ds we were paused on was deleted, the zb_objset may
1287                  * be -1, so we will skip this and find a new objset
1288                  * below.
1289                  */
1290                 dsl_scan_visitds(scn, scn->scn_phys.scn_bookmark.zb_objset, tx);
1291                 if (scn->scn_pausing)
1292                         return;
1293         }
1294
1295         /*
1296          * In case we were paused right at the end of the ds, zero the
1297          * bookmark so we don't think that we're still trying to resume.
1298          */
1299         bzero(&scn->scn_phys.scn_bookmark, sizeof (zbookmark_t));
1300
1301         /* keep pulling things out of the zap-object-as-queue */
1302         while (zap_cursor_init(&zc, dp->dp_meta_objset,
1303             scn->scn_phys.scn_queue_obj),
1304             zap_cursor_retrieve(&zc, &za) == 0) {
1305                 dsl_dataset_t *ds;
1306                 uint64_t dsobj;
1307
1308                 dsobj = strtonum(za.za_name, NULL);
1309                 VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
1310                     scn->scn_phys.scn_queue_obj, dsobj, tx));
1311
1312                 /* Set up min/max txg */
1313                 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
1314                 if (za.za_first_integer != 0) {
1315                         scn->scn_phys.scn_cur_min_txg =
1316                             MAX(scn->scn_phys.scn_min_txg,
1317                             za.za_first_integer);
1318                 } else {
1319                         scn->scn_phys.scn_cur_min_txg =
1320                             MAX(scn->scn_phys.scn_min_txg,
1321                             ds->ds_phys->ds_prev_snap_txg);
1322                 }
1323                 scn->scn_phys.scn_cur_max_txg = dsl_scan_ds_maxtxg(ds);
1324                 dsl_dataset_rele(ds, FTAG);
1325
1326                 dsl_scan_visitds(scn, dsobj, tx);
1327                 zap_cursor_fini(&zc);
1328                 if (scn->scn_pausing)
1329                         return;
1330         }
1331         zap_cursor_fini(&zc);
1332 }
1333
1334 static boolean_t
1335 dsl_scan_free_should_pause(dsl_scan_t *scn)
1336 {
1337         uint64_t elapsed_nanosecs;
1338
1339         elapsed_nanosecs = gethrtime() - scn->scn_sync_start_time;
1340         return (elapsed_nanosecs / NANOSEC > zfs_txg_timeout ||
1341             (elapsed_nanosecs / MICROSEC > zfs_free_min_time_ms &&
1342             txg_sync_waiting(scn->scn_dp)) ||
1343             spa_shutting_down(scn->scn_dp->dp_spa));
1344 }
1345
1346 static int
1347 dsl_scan_free_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1348 {
1349         dsl_scan_t *scn = arg;
1350
1351         if (!scn->scn_is_bptree ||
1352             (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_OBJSET)) {
1353                 if (dsl_scan_free_should_pause(scn))
1354                         return (SET_ERROR(ERESTART));
1355         }
1356
1357         zio_nowait(zio_free_sync(scn->scn_zio_root, scn->scn_dp->dp_spa,
1358             dmu_tx_get_txg(tx), bp, 0));
1359         dsl_dir_diduse_space(tx->tx_pool->dp_free_dir, DD_USED_HEAD,
1360             -bp_get_dsize_sync(scn->scn_dp->dp_spa, bp),
1361             -BP_GET_PSIZE(bp), -BP_GET_UCSIZE(bp), tx);
1362         scn->scn_visited_this_txg++;
1363         return (0);
1364 }
1365
1366 boolean_t
1367 dsl_scan_active(dsl_scan_t *scn)
1368 {
1369         spa_t *spa = scn->scn_dp->dp_spa;
1370         uint64_t used = 0, comp, uncomp;
1371
1372         if (spa->spa_load_state != SPA_LOAD_NONE)
1373                 return (B_FALSE);
1374         if (spa_shutting_down(spa))
1375                 return (B_FALSE);
1376
1377         if (scn->scn_phys.scn_state == DSS_SCANNING)
1378                 return (B_TRUE);
1379
1380         if (spa_feature_is_active(spa,
1381             &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY])) {
1382                 return (B_TRUE);
1383         }
1384         if (spa_version(scn->scn_dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
1385                 (void) bpobj_space(&scn->scn_dp->dp_free_bpobj,
1386                     &used, &comp, &uncomp);
1387         }
1388         return (used != 0);
1389 }
1390
1391 void
1392 dsl_scan_sync(dsl_pool_t *dp, dmu_tx_t *tx)
1393 {
1394         dsl_scan_t *scn = dp->dp_scan;
1395         spa_t *spa = dp->dp_spa;
1396         int err;
1397
1398         /*
1399          * Check for scn_restart_txg before checking spa_load_state, so
1400          * that we can restart an old-style scan while the pool is being
1401          * imported (see dsl_scan_init).
1402          */
1403         if (scn->scn_restart_txg != 0 &&
1404             scn->scn_restart_txg <= tx->tx_txg) {
1405                 pool_scan_func_t func = POOL_SCAN_SCRUB;
1406                 dsl_scan_done(scn, B_FALSE, tx);
1407                 if (vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL))
1408                         func = POOL_SCAN_RESILVER;
1409                 zfs_dbgmsg("restarting scan func=%u txg=%llu",
1410                     func, tx->tx_txg);
1411                 dsl_scan_setup_sync(&func, tx);
1412         }
1413
1414         if (!dsl_scan_active(scn) ||
1415             spa_sync_pass(dp->dp_spa) > 1)
1416                 return;
1417
1418         scn->scn_visited_this_txg = 0;
1419         scn->scn_pausing = B_FALSE;
1420         scn->scn_sync_start_time = gethrtime();
1421         spa->spa_scrub_active = B_TRUE;
1422
1423         /*
1424          * First process the free list.  If we pause the free, don't do
1425          * any scanning.  This ensures that there is no free list when
1426          * we are scanning, so the scan code doesn't have to worry about
1427          * traversing it.
1428          */
1429         if (spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
1430                 scn->scn_is_bptree = B_FALSE;
1431                 scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
1432                     NULL, ZIO_FLAG_MUSTSUCCEED);
1433                 err = bpobj_iterate(&dp->dp_free_bpobj,
1434                     dsl_scan_free_block_cb, scn, tx);
1435                 VERIFY3U(0, ==, zio_wait(scn->scn_zio_root));
1436
1437                 if (err == 0 && spa_feature_is_active(spa,
1438                     &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY])) {
1439                         scn->scn_is_bptree = B_TRUE;
1440                         scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
1441                             NULL, ZIO_FLAG_MUSTSUCCEED);
1442                         err = bptree_iterate(dp->dp_meta_objset,
1443                             dp->dp_bptree_obj, B_TRUE, dsl_scan_free_block_cb,
1444                             scn, tx);
1445                         VERIFY0(zio_wait(scn->scn_zio_root));
1446
1447                         if (err == 0) {
1448                                 zfeature_info_t *feat = &spa_feature_table
1449                                     [SPA_FEATURE_ASYNC_DESTROY];
1450                                 /* finished; deactivate async destroy feature */
1451                                 spa_feature_decr(spa, feat, tx);
1452                                 ASSERT(!spa_feature_is_active(spa, feat));
1453                                 VERIFY0(zap_remove(dp->dp_meta_objset,
1454                                     DMU_POOL_DIRECTORY_OBJECT,
1455                                     DMU_POOL_BPTREE_OBJ, tx));
1456                                 VERIFY0(bptree_free(dp->dp_meta_objset,
1457                                     dp->dp_bptree_obj, tx));
1458                                 dp->dp_bptree_obj = 0;
1459                         }
1460                 }
1461                 if (scn->scn_visited_this_txg) {
1462                         zfs_dbgmsg("freed %llu blocks in %llums from "
1463                             "free_bpobj/bptree txg %llu",
1464                             (longlong_t)scn->scn_visited_this_txg,
1465                             (longlong_t)
1466                             (gethrtime() - scn->scn_sync_start_time) / MICROSEC,
1467                             (longlong_t)tx->tx_txg);
1468                         scn->scn_visited_this_txg = 0;
1469                         /*
1470                          * Re-sync the ddt so that we can further modify
1471                          * it when doing bprewrite.
1472                          */
1473                         ddt_sync(spa, tx->tx_txg);
1474                 }
1475                 if (err == ERESTART)
1476                         return;
1477         }
1478
1479         if (scn->scn_phys.scn_state != DSS_SCANNING)
1480                 return;
1481
1482         if (scn->scn_phys.scn_ddt_bookmark.ddb_class <=
1483             scn->scn_phys.scn_ddt_class_max) {
1484                 zfs_dbgmsg("doing scan sync txg %llu; "
1485                     "ddt bm=%llu/%llu/%llu/%llx",
1486                     (longlong_t)tx->tx_txg,
1487                     (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_class,
1488                     (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_type,
1489                     (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_checksum,
1490                     (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_cursor);
1491                 ASSERT(scn->scn_phys.scn_bookmark.zb_objset == 0);
1492                 ASSERT(scn->scn_phys.scn_bookmark.zb_object == 0);
1493                 ASSERT(scn->scn_phys.scn_bookmark.zb_level == 0);
1494                 ASSERT(scn->scn_phys.scn_bookmark.zb_blkid == 0);
1495         } else {
1496                 zfs_dbgmsg("doing scan sync txg %llu; bm=%llu/%llu/%llu/%llu",
1497                     (longlong_t)tx->tx_txg,
1498                     (longlong_t)scn->scn_phys.scn_bookmark.zb_objset,
1499                     (longlong_t)scn->scn_phys.scn_bookmark.zb_object,
1500                     (longlong_t)scn->scn_phys.scn_bookmark.zb_level,
1501                     (longlong_t)scn->scn_phys.scn_bookmark.zb_blkid);
1502         }
1503
1504         scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
1505             NULL, ZIO_FLAG_CANFAIL);
1506         dsl_pool_config_enter(dp, FTAG);
1507         dsl_scan_visit(scn, tx);
1508         dsl_pool_config_exit(dp, FTAG);
1509         (void) zio_wait(scn->scn_zio_root);
1510         scn->scn_zio_root = NULL;
1511
1512         zfs_dbgmsg("visited %llu blocks in %llums",
1513             (longlong_t)scn->scn_visited_this_txg,
1514             (longlong_t)(gethrtime() - scn->scn_sync_start_time) / MICROSEC);
1515
1516         if (!scn->scn_pausing) {
1517                 /* finished with scan. */
1518                 zfs_dbgmsg("finished scan txg %llu", (longlong_t)tx->tx_txg);
1519                 dsl_scan_done(scn, B_TRUE, tx);
1520         }
1521
1522         if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
1523                 mutex_enter(&spa->spa_scrub_lock);
1524                 while (spa->spa_scrub_inflight > 0) {
1525                         cv_wait(&spa->spa_scrub_io_cv,
1526                             &spa->spa_scrub_lock);
1527                 }
1528                 mutex_exit(&spa->spa_scrub_lock);
1529         }
1530
1531         dsl_scan_sync_state(scn, tx);
1532 }
1533
1534 /*
1535  * This will start a new scan, or restart an existing one.
1536  */
1537 void
1538 dsl_resilver_restart(dsl_pool_t *dp, uint64_t txg)
1539 {
1540         if (txg == 0) {
1541                 dmu_tx_t *tx;
1542                 tx = dmu_tx_create_dd(dp->dp_mos_dir);
1543                 VERIFY(0 == dmu_tx_assign(tx, TXG_WAIT));
1544
1545                 txg = dmu_tx_get_txg(tx);
1546                 dp->dp_scan->scn_restart_txg = txg;
1547                 dmu_tx_commit(tx);
1548         } else {
1549                 dp->dp_scan->scn_restart_txg = txg;
1550         }
1551         zfs_dbgmsg("restarting resilver txg=%llu", txg);
1552 }
1553
1554 boolean_t
1555 dsl_scan_resilvering(dsl_pool_t *dp)
1556 {
1557         return (dp->dp_scan->scn_phys.scn_state == DSS_SCANNING &&
1558             dp->dp_scan->scn_phys.scn_func == POOL_SCAN_RESILVER);
1559 }
1560
1561 /*
1562  * scrub consumers
1563  */
1564
1565 static void
1566 count_block(zfs_all_blkstats_t *zab, const blkptr_t *bp)
1567 {
1568         int i;
1569
1570         /*
1571          * If we resume after a reboot, zab will be NULL; don't record
1572          * incomplete stats in that case.
1573          */
1574         if (zab == NULL)
1575                 return;
1576
1577         for (i = 0; i < 4; i++) {
1578                 int l = (i < 2) ? BP_GET_LEVEL(bp) : DN_MAX_LEVELS;
1579                 int t = (i & 1) ? BP_GET_TYPE(bp) : DMU_OT_TOTAL;
1580                 if (t & DMU_OT_NEWTYPE)
1581                         t = DMU_OT_OTHER;
1582                 zfs_blkstat_t *zb = &zab->zab_type[l][t];
1583                 int equal;
1584
1585                 zb->zb_count++;
1586                 zb->zb_asize += BP_GET_ASIZE(bp);
1587                 zb->zb_lsize += BP_GET_LSIZE(bp);
1588                 zb->zb_psize += BP_GET_PSIZE(bp);
1589                 zb->zb_gangs += BP_COUNT_GANG(bp);
1590
1591                 switch (BP_GET_NDVAS(bp)) {
1592                 case 2:
1593                         if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1594                             DVA_GET_VDEV(&bp->blk_dva[1]))
1595                                 zb->zb_ditto_2_of_2_samevdev++;
1596                         break;
1597                 case 3:
1598                         equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1599                             DVA_GET_VDEV(&bp->blk_dva[1])) +
1600                             (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1601                             DVA_GET_VDEV(&bp->blk_dva[2])) +
1602                             (DVA_GET_VDEV(&bp->blk_dva[1]) ==
1603                             DVA_GET_VDEV(&bp->blk_dva[2]));
1604                         if (equal == 1)
1605                                 zb->zb_ditto_2_of_3_samevdev++;
1606                         else if (equal == 3)
1607                                 zb->zb_ditto_3_of_3_samevdev++;
1608                         break;
1609                 }
1610         }
1611 }
1612
1613 static void
1614 dsl_scan_scrub_done(zio_t *zio)
1615 {
1616         spa_t *spa = zio->io_spa;
1617
1618         zio_data_buf_free(zio->io_data, zio->io_size);
1619
1620         mutex_enter(&spa->spa_scrub_lock);
1621         spa->spa_scrub_inflight--;
1622         cv_broadcast(&spa->spa_scrub_io_cv);
1623
1624         if (zio->io_error && (zio->io_error != ECKSUM ||
1625             !(zio->io_flags & ZIO_FLAG_SPECULATIVE))) {
1626                 spa->spa_dsl_pool->dp_scan->scn_phys.scn_errors++;
1627         }
1628         mutex_exit(&spa->spa_scrub_lock);
1629 }
1630
1631 static int
1632 dsl_scan_scrub_cb(dsl_pool_t *dp,
1633     const blkptr_t *bp, const zbookmark_t *zb)
1634 {
1635         dsl_scan_t *scn = dp->dp_scan;
1636         size_t size = BP_GET_PSIZE(bp);
1637         spa_t *spa = dp->dp_spa;
1638         uint64_t phys_birth = BP_PHYSICAL_BIRTH(bp);
1639         boolean_t needs_io;
1640         int zio_flags = ZIO_FLAG_SCAN_THREAD | ZIO_FLAG_RAW | ZIO_FLAG_CANFAIL;
1641         int zio_priority;
1642         unsigned int scan_delay = 0;
1643
1644         if (phys_birth <= scn->scn_phys.scn_min_txg ||
1645             phys_birth >= scn->scn_phys.scn_max_txg)
1646                 return (0);
1647
1648         count_block(dp->dp_blkstats, bp);
1649
1650         ASSERT(DSL_SCAN_IS_SCRUB_RESILVER(scn));
1651         if (scn->scn_phys.scn_func == POOL_SCAN_SCRUB) {
1652                 zio_flags |= ZIO_FLAG_SCRUB;
1653                 zio_priority = ZIO_PRIORITY_SCRUB;
1654                 needs_io = B_TRUE;
1655                 scan_delay = zfs_scrub_delay;
1656         } else {
1657                 ASSERT3U(scn->scn_phys.scn_func, ==, POOL_SCAN_RESILVER);
1658                 zio_flags |= ZIO_FLAG_RESILVER;
1659                 zio_priority = ZIO_PRIORITY_RESILVER;
1660                 needs_io = B_FALSE;
1661                 scan_delay = zfs_resilver_delay;
1662         }
1663
1664         /* If it's an intent log block, failure is expected. */
1665         if (zb->zb_level == ZB_ZIL_LEVEL)
1666                 zio_flags |= ZIO_FLAG_SPECULATIVE;
1667
1668         for (int d = 0; d < BP_GET_NDVAS(bp); d++) {
1669                 vdev_t *vd = vdev_lookup_top(spa,
1670                     DVA_GET_VDEV(&bp->blk_dva[d]));
1671
1672                 /*
1673                  * Keep track of how much data we've examined so that
1674                  * zpool(1M) status can make useful progress reports.
1675                  */
1676                 scn->scn_phys.scn_examined += DVA_GET_ASIZE(&bp->blk_dva[d]);
1677                 spa->spa_scan_pass_exam += DVA_GET_ASIZE(&bp->blk_dva[d]);
1678
1679                 /* if it's a resilver, this may not be in the target range */
1680                 if (!needs_io) {
1681                         if (DVA_GET_GANG(&bp->blk_dva[d])) {
1682                                 /*
1683                                  * Gang members may be spread across multiple
1684                                  * vdevs, so the best estimate we have is the
1685                                  * scrub range, which has already been checked.
1686                                  * XXX -- it would be better to change our
1687                                  * allocation policy to ensure that all
1688                                  * gang members reside on the same vdev.
1689                                  */
1690                                 needs_io = B_TRUE;
1691                         } else {
1692                                 needs_io = vdev_dtl_contains(vd, DTL_PARTIAL,
1693                                     phys_birth, 1);
1694                         }
1695                 }
1696         }
1697
1698         if (needs_io && !zfs_no_scrub_io) {
1699                 vdev_t *rvd = spa->spa_root_vdev;
1700                 uint64_t maxinflight = rvd->vdev_children *
1701                     MAX(zfs_top_maxinflight, 1);
1702                 void *data = zio_data_buf_alloc(size);
1703
1704                 mutex_enter(&spa->spa_scrub_lock);
1705                 while (spa->spa_scrub_inflight >= 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                 /*
1711                  * If we're seeing recent (zfs_scan_idle) "important" I/Os
1712                  * then throttle our workload to limit the impact of a scan.
1713                  */
1714                 if (ddi_get_lbolt64() - spa->spa_last_io <= zfs_scan_idle)
1715                         delay(MAX((int)scan_delay, 0));
1716
1717                 zio_nowait(zio_read(NULL, spa, bp, data, size,
1718                     dsl_scan_scrub_done, NULL, zio_priority,
1719                     zio_flags, zb));
1720         }
1721
1722         /* do not relocate this block */
1723         return (0);
1724 }
1725
1726 int
1727 dsl_scan(dsl_pool_t *dp, pool_scan_func_t func)
1728 {
1729         spa_t *spa = dp->dp_spa;
1730
1731         /*
1732          * Purge all vdev caches and probe all devices.  We do this here
1733          * rather than in sync context because this requires a writer lock
1734          * on the spa_config lock, which we can't do from sync context.  The
1735          * spa_scrub_reopen flag indicates that vdev_open() should not
1736          * attempt to start another scrub.
1737          */
1738         spa_vdev_state_enter(spa, SCL_NONE);
1739         spa->spa_scrub_reopen = B_TRUE;
1740         vdev_reopen(spa->spa_root_vdev);
1741         spa->spa_scrub_reopen = B_FALSE;
1742         (void) spa_vdev_state_exit(spa, NULL, 0);
1743
1744         return (dsl_sync_task(spa_name(spa), dsl_scan_setup_check,
1745             dsl_scan_setup_sync, &func, 0));
1746 }