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