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