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