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