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