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