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