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