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