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