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