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