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