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