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