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