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