Print this page
4045 zfs write throttle & i/o scheduler performance work
Reviewed by: George Wilson <george.wilson@delphix.com>
Reviewed by: Adam Leventhal <ahl@delphix.com>
Reviewed by: Christopher Siden <christopher.siden@delphix.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/fs/zfs/zio.c
+++ new/usr/src/uts/common/fs/zfs/zio.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) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 * Copyright (c) 2013 by Delphix. All rights reserved.
24 24 * Copyright (c) 2011 Nexenta Systems, Inc. All rights reserved.
25 25 */
26 26
27 27 #include <sys/zfs_context.h>
28 28 #include <sys/fm/fs/zfs.h>
29 29 #include <sys/spa.h>
30 30 #include <sys/txg.h>
31 31 #include <sys/spa_impl.h>
↓ open down ↓ |
31 lines elided |
↑ open up ↑ |
32 32 #include <sys/vdev_impl.h>
33 33 #include <sys/zio_impl.h>
34 34 #include <sys/zio_compress.h>
35 35 #include <sys/zio_checksum.h>
36 36 #include <sys/dmu_objset.h>
37 37 #include <sys/arc.h>
38 38 #include <sys/ddt.h>
39 39
40 40 /*
41 41 * ==========================================================================
42 - * I/O priority table
43 - * ==========================================================================
44 - */
45 -uint8_t zio_priority_table[ZIO_PRIORITY_TABLE_SIZE] = {
46 - 0, /* ZIO_PRIORITY_NOW */
47 - 0, /* ZIO_PRIORITY_SYNC_READ */
48 - 0, /* ZIO_PRIORITY_SYNC_WRITE */
49 - 0, /* ZIO_PRIORITY_LOG_WRITE */
50 - 1, /* ZIO_PRIORITY_CACHE_FILL */
51 - 1, /* ZIO_PRIORITY_AGG */
52 - 4, /* ZIO_PRIORITY_FREE */
53 - 4, /* ZIO_PRIORITY_ASYNC_WRITE */
54 - 6, /* ZIO_PRIORITY_ASYNC_READ */
55 - 10, /* ZIO_PRIORITY_RESILVER */
56 - 20, /* ZIO_PRIORITY_SCRUB */
57 - 2, /* ZIO_PRIORITY_DDT_PREFETCH */
58 -};
59 -
60 -/*
61 - * ==========================================================================
62 42 * I/O type descriptions
63 43 * ==========================================================================
64 44 */
65 -char *zio_type_name[ZIO_TYPES] = {
45 +const char *zio_type_name[ZIO_TYPES] = {
66 46 "zio_null", "zio_read", "zio_write", "zio_free", "zio_claim",
67 47 "zio_ioctl"
68 48 };
69 49
70 50 /*
71 51 * ==========================================================================
72 52 * I/O kmem caches
73 53 * ==========================================================================
74 54 */
75 55 kmem_cache_t *zio_cache;
76 56 kmem_cache_t *zio_link_cache;
77 57 kmem_cache_t *zio_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
78 58 kmem_cache_t *zio_data_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
79 59
80 60 #ifdef _KERNEL
81 61 extern vmem_t *zio_alloc_arena;
82 62 #endif
83 63 extern int zfs_mg_alloc_failures;
84 64
85 65 /*
86 66 * The following actions directly effect the spa's sync-to-convergence logic.
87 67 * The values below define the sync pass when we start performing the action.
88 68 * Care should be taken when changing these values as they directly impact
89 69 * spa_sync() performance. Tuning these values may introduce subtle performance
90 70 * pathologies and should only be done in the context of performance analysis.
91 71 * These tunables will eventually be removed and replaced with #defines once
92 72 * enough analysis has been done to determine optimal values.
93 73 *
94 74 * The 'zfs_sync_pass_deferred_free' pass must be greater than 1 to ensure that
95 75 * regular blocks are not deferred.
96 76 */
97 77 int zfs_sync_pass_deferred_free = 2; /* defer frees starting in this pass */
98 78 int zfs_sync_pass_dont_compress = 5; /* don't compress starting in this pass */
99 79 int zfs_sync_pass_rewrite = 2; /* rewrite new bps starting in this pass */
100 80
101 81 /*
102 82 * An allocating zio is one that either currently has the DVA allocate
103 83 * stage set or will have it later in its lifetime.
104 84 */
105 85 #define IO_IS_ALLOCATING(zio) ((zio)->io_orig_pipeline & ZIO_STAGE_DVA_ALLOCATE)
106 86
107 87 boolean_t zio_requeue_io_start_cut_in_line = B_TRUE;
108 88
109 89 #ifdef ZFS_DEBUG
110 90 int zio_buf_debug_limit = 16384;
111 91 #else
112 92 int zio_buf_debug_limit = 0;
113 93 #endif
114 94
115 95 void
116 96 zio_init(void)
117 97 {
118 98 size_t c;
119 99 vmem_t *data_alloc_arena = NULL;
120 100
121 101 #ifdef _KERNEL
122 102 data_alloc_arena = zio_alloc_arena;
123 103 #endif
124 104 zio_cache = kmem_cache_create("zio_cache",
125 105 sizeof (zio_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
126 106 zio_link_cache = kmem_cache_create("zio_link_cache",
127 107 sizeof (zio_link_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
128 108
129 109 /*
130 110 * For small buffers, we want a cache for each multiple of
131 111 * SPA_MINBLOCKSIZE. For medium-size buffers, we want a cache
132 112 * for each quarter-power of 2. For large buffers, we want
133 113 * a cache for each multiple of PAGESIZE.
134 114 */
135 115 for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
136 116 size_t size = (c + 1) << SPA_MINBLOCKSHIFT;
137 117 size_t p2 = size;
138 118 size_t align = 0;
139 119 size_t cflags = (size > zio_buf_debug_limit) ? KMC_NODEBUG : 0;
140 120
141 121 while (p2 & (p2 - 1))
142 122 p2 &= p2 - 1;
143 123
144 124 #ifndef _KERNEL
145 125 /*
146 126 * If we are using watchpoints, put each buffer on its own page,
147 127 * to eliminate the performance overhead of trapping to the
148 128 * kernel when modifying a non-watched buffer that shares the
149 129 * page with a watched buffer.
150 130 */
151 131 if (arc_watch && !IS_P2ALIGNED(size, PAGESIZE))
152 132 continue;
153 133 #endif
154 134 if (size <= 4 * SPA_MINBLOCKSIZE) {
155 135 align = SPA_MINBLOCKSIZE;
156 136 } else if (IS_P2ALIGNED(size, PAGESIZE)) {
157 137 align = PAGESIZE;
158 138 } else if (IS_P2ALIGNED(size, p2 >> 2)) {
159 139 align = p2 >> 2;
160 140 }
161 141
162 142 if (align != 0) {
163 143 char name[36];
164 144 (void) sprintf(name, "zio_buf_%lu", (ulong_t)size);
165 145 zio_buf_cache[c] = kmem_cache_create(name, size,
166 146 align, NULL, NULL, NULL, NULL, NULL, cflags);
167 147
168 148 /*
169 149 * Since zio_data bufs do not appear in crash dumps, we
170 150 * pass KMC_NOTOUCH so that no allocator metadata is
171 151 * stored with the buffers.
172 152 */
173 153 (void) sprintf(name, "zio_data_buf_%lu", (ulong_t)size);
174 154 zio_data_buf_cache[c] = kmem_cache_create(name, size,
175 155 align, NULL, NULL, NULL, NULL, data_alloc_arena,
176 156 cflags | KMC_NOTOUCH);
177 157 }
178 158 }
179 159
180 160 while (--c != 0) {
181 161 ASSERT(zio_buf_cache[c] != NULL);
182 162 if (zio_buf_cache[c - 1] == NULL)
183 163 zio_buf_cache[c - 1] = zio_buf_cache[c];
184 164
185 165 ASSERT(zio_data_buf_cache[c] != NULL);
186 166 if (zio_data_buf_cache[c - 1] == NULL)
187 167 zio_data_buf_cache[c - 1] = zio_data_buf_cache[c];
188 168 }
189 169
190 170 /*
191 171 * The zio write taskqs have 1 thread per cpu, allow 1/2 of the taskqs
192 172 * to fail 3 times per txg or 8 failures, whichever is greater.
193 173 */
194 174 zfs_mg_alloc_failures = MAX((3 * max_ncpus / 2), 8);
195 175
196 176 zio_inject_init();
197 177 }
198 178
199 179 void
200 180 zio_fini(void)
201 181 {
202 182 size_t c;
203 183 kmem_cache_t *last_cache = NULL;
204 184 kmem_cache_t *last_data_cache = NULL;
205 185
206 186 for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
207 187 if (zio_buf_cache[c] != last_cache) {
208 188 last_cache = zio_buf_cache[c];
209 189 kmem_cache_destroy(zio_buf_cache[c]);
210 190 }
211 191 zio_buf_cache[c] = NULL;
212 192
213 193 if (zio_data_buf_cache[c] != last_data_cache) {
214 194 last_data_cache = zio_data_buf_cache[c];
215 195 kmem_cache_destroy(zio_data_buf_cache[c]);
216 196 }
217 197 zio_data_buf_cache[c] = NULL;
218 198 }
219 199
220 200 kmem_cache_destroy(zio_link_cache);
221 201 kmem_cache_destroy(zio_cache);
222 202
223 203 zio_inject_fini();
224 204 }
225 205
226 206 /*
227 207 * ==========================================================================
228 208 * Allocate and free I/O buffers
229 209 * ==========================================================================
230 210 */
231 211
232 212 /*
233 213 * Use zio_buf_alloc to allocate ZFS metadata. This data will appear in a
234 214 * crashdump if the kernel panics, so use it judiciously. Obviously, it's
235 215 * useful to inspect ZFS metadata, but if possible, we should avoid keeping
236 216 * excess / transient data in-core during a crashdump.
237 217 */
238 218 void *
239 219 zio_buf_alloc(size_t size)
240 220 {
241 221 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
242 222
243 223 ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
244 224
245 225 return (kmem_cache_alloc(zio_buf_cache[c], KM_PUSHPAGE));
246 226 }
247 227
248 228 /*
249 229 * Use zio_data_buf_alloc to allocate data. The data will not appear in a
250 230 * crashdump if the kernel panics. This exists so that we will limit the amount
251 231 * of ZFS data that shows up in a kernel crashdump. (Thus reducing the amount
252 232 * of kernel heap dumped to disk when the kernel panics)
253 233 */
254 234 void *
255 235 zio_data_buf_alloc(size_t size)
256 236 {
257 237 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
258 238
259 239 ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
260 240
261 241 return (kmem_cache_alloc(zio_data_buf_cache[c], KM_PUSHPAGE));
262 242 }
263 243
264 244 void
265 245 zio_buf_free(void *buf, size_t size)
266 246 {
267 247 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
268 248
269 249 ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
270 250
271 251 kmem_cache_free(zio_buf_cache[c], buf);
272 252 }
273 253
274 254 void
275 255 zio_data_buf_free(void *buf, size_t size)
276 256 {
277 257 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
278 258
279 259 ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
280 260
281 261 kmem_cache_free(zio_data_buf_cache[c], buf);
282 262 }
283 263
284 264 /*
285 265 * ==========================================================================
286 266 * Push and pop I/O transform buffers
287 267 * ==========================================================================
288 268 */
289 269 static void
290 270 zio_push_transform(zio_t *zio, void *data, uint64_t size, uint64_t bufsize,
291 271 zio_transform_func_t *transform)
292 272 {
293 273 zio_transform_t *zt = kmem_alloc(sizeof (zio_transform_t), KM_SLEEP);
294 274
295 275 zt->zt_orig_data = zio->io_data;
296 276 zt->zt_orig_size = zio->io_size;
297 277 zt->zt_bufsize = bufsize;
298 278 zt->zt_transform = transform;
299 279
300 280 zt->zt_next = zio->io_transform_stack;
301 281 zio->io_transform_stack = zt;
302 282
303 283 zio->io_data = data;
304 284 zio->io_size = size;
305 285 }
306 286
307 287 static void
308 288 zio_pop_transforms(zio_t *zio)
309 289 {
310 290 zio_transform_t *zt;
311 291
312 292 while ((zt = zio->io_transform_stack) != NULL) {
313 293 if (zt->zt_transform != NULL)
314 294 zt->zt_transform(zio,
315 295 zt->zt_orig_data, zt->zt_orig_size);
316 296
317 297 if (zt->zt_bufsize != 0)
318 298 zio_buf_free(zio->io_data, zt->zt_bufsize);
319 299
320 300 zio->io_data = zt->zt_orig_data;
321 301 zio->io_size = zt->zt_orig_size;
322 302 zio->io_transform_stack = zt->zt_next;
323 303
324 304 kmem_free(zt, sizeof (zio_transform_t));
325 305 }
326 306 }
327 307
328 308 /*
329 309 * ==========================================================================
330 310 * I/O transform callbacks for subblocks and decompression
331 311 * ==========================================================================
332 312 */
333 313 static void
334 314 zio_subblock(zio_t *zio, void *data, uint64_t size)
335 315 {
336 316 ASSERT(zio->io_size > size);
337 317
338 318 if (zio->io_type == ZIO_TYPE_READ)
339 319 bcopy(zio->io_data, data, size);
340 320 }
341 321
342 322 static void
343 323 zio_decompress(zio_t *zio, void *data, uint64_t size)
344 324 {
345 325 if (zio->io_error == 0 &&
346 326 zio_decompress_data(BP_GET_COMPRESS(zio->io_bp),
347 327 zio->io_data, data, zio->io_size, size) != 0)
348 328 zio->io_error = SET_ERROR(EIO);
349 329 }
350 330
351 331 /*
352 332 * ==========================================================================
353 333 * I/O parent/child relationships and pipeline interlocks
354 334 * ==========================================================================
355 335 */
356 336 /*
357 337 * NOTE - Callers to zio_walk_parents() and zio_walk_children must
358 338 * continue calling these functions until they return NULL.
359 339 * Otherwise, the next caller will pick up the list walk in
360 340 * some indeterminate state. (Otherwise every caller would
361 341 * have to pass in a cookie to keep the state represented by
362 342 * io_walk_link, which gets annoying.)
363 343 */
364 344 zio_t *
365 345 zio_walk_parents(zio_t *cio)
366 346 {
367 347 zio_link_t *zl = cio->io_walk_link;
368 348 list_t *pl = &cio->io_parent_list;
369 349
370 350 zl = (zl == NULL) ? list_head(pl) : list_next(pl, zl);
371 351 cio->io_walk_link = zl;
372 352
373 353 if (zl == NULL)
374 354 return (NULL);
375 355
376 356 ASSERT(zl->zl_child == cio);
377 357 return (zl->zl_parent);
378 358 }
379 359
380 360 zio_t *
381 361 zio_walk_children(zio_t *pio)
382 362 {
383 363 zio_link_t *zl = pio->io_walk_link;
384 364 list_t *cl = &pio->io_child_list;
385 365
386 366 zl = (zl == NULL) ? list_head(cl) : list_next(cl, zl);
387 367 pio->io_walk_link = zl;
388 368
389 369 if (zl == NULL)
390 370 return (NULL);
391 371
392 372 ASSERT(zl->zl_parent == pio);
393 373 return (zl->zl_child);
394 374 }
395 375
396 376 zio_t *
397 377 zio_unique_parent(zio_t *cio)
398 378 {
399 379 zio_t *pio = zio_walk_parents(cio);
400 380
401 381 VERIFY(zio_walk_parents(cio) == NULL);
402 382 return (pio);
403 383 }
404 384
405 385 void
406 386 zio_add_child(zio_t *pio, zio_t *cio)
407 387 {
408 388 zio_link_t *zl = kmem_cache_alloc(zio_link_cache, KM_SLEEP);
409 389
410 390 /*
411 391 * Logical I/Os can have logical, gang, or vdev children.
412 392 * Gang I/Os can have gang or vdev children.
413 393 * Vdev I/Os can only have vdev children.
414 394 * The following ASSERT captures all of these constraints.
415 395 */
416 396 ASSERT(cio->io_child_type <= pio->io_child_type);
417 397
418 398 zl->zl_parent = pio;
419 399 zl->zl_child = cio;
420 400
421 401 mutex_enter(&cio->io_lock);
422 402 mutex_enter(&pio->io_lock);
423 403
424 404 ASSERT(pio->io_state[ZIO_WAIT_DONE] == 0);
425 405
426 406 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
427 407 pio->io_children[cio->io_child_type][w] += !cio->io_state[w];
428 408
429 409 list_insert_head(&pio->io_child_list, zl);
430 410 list_insert_head(&cio->io_parent_list, zl);
431 411
432 412 pio->io_child_count++;
433 413 cio->io_parent_count++;
434 414
435 415 mutex_exit(&pio->io_lock);
436 416 mutex_exit(&cio->io_lock);
437 417 }
438 418
439 419 static void
440 420 zio_remove_child(zio_t *pio, zio_t *cio, zio_link_t *zl)
441 421 {
442 422 ASSERT(zl->zl_parent == pio);
443 423 ASSERT(zl->zl_child == cio);
444 424
445 425 mutex_enter(&cio->io_lock);
446 426 mutex_enter(&pio->io_lock);
447 427
448 428 list_remove(&pio->io_child_list, zl);
449 429 list_remove(&cio->io_parent_list, zl);
450 430
451 431 pio->io_child_count--;
452 432 cio->io_parent_count--;
453 433
454 434 mutex_exit(&pio->io_lock);
455 435 mutex_exit(&cio->io_lock);
456 436
457 437 kmem_cache_free(zio_link_cache, zl);
458 438 }
459 439
460 440 static boolean_t
461 441 zio_wait_for_children(zio_t *zio, enum zio_child child, enum zio_wait_type wait)
462 442 {
463 443 uint64_t *countp = &zio->io_children[child][wait];
464 444 boolean_t waiting = B_FALSE;
465 445
466 446 mutex_enter(&zio->io_lock);
467 447 ASSERT(zio->io_stall == NULL);
468 448 if (*countp != 0) {
469 449 zio->io_stage >>= 1;
470 450 zio->io_stall = countp;
471 451 waiting = B_TRUE;
472 452 }
473 453 mutex_exit(&zio->io_lock);
474 454
475 455 return (waiting);
476 456 }
477 457
478 458 static void
↓ open down ↓ |
403 lines elided |
↑ open up ↑ |
479 459 zio_notify_parent(zio_t *pio, zio_t *zio, enum zio_wait_type wait)
480 460 {
481 461 uint64_t *countp = &pio->io_children[zio->io_child_type][wait];
482 462 int *errorp = &pio->io_child_error[zio->io_child_type];
483 463
484 464 mutex_enter(&pio->io_lock);
485 465 if (zio->io_error && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
486 466 *errorp = zio_worst_error(*errorp, zio->io_error);
487 467 pio->io_reexecute |= zio->io_reexecute;
488 468 ASSERT3U(*countp, >, 0);
489 - if (--*countp == 0 && pio->io_stall == countp) {
469 +
470 + (*countp)--;
471 +
472 + if (*countp == 0 && pio->io_stall == countp) {
490 473 pio->io_stall = NULL;
491 474 mutex_exit(&pio->io_lock);
492 475 zio_execute(pio);
493 476 } else {
494 477 mutex_exit(&pio->io_lock);
495 478 }
496 479 }
497 480
498 481 static void
499 482 zio_inherit_child_errors(zio_t *zio, enum zio_child c)
500 483 {
501 484 if (zio->io_child_error[c] != 0 && zio->io_error == 0)
502 485 zio->io_error = zio->io_child_error[c];
↓ open down ↓ |
3 lines elided |
↑ open up ↑ |
503 486 }
504 487
505 488 /*
506 489 * ==========================================================================
507 490 * Create the various types of I/O (read, write, free, etc)
508 491 * ==========================================================================
509 492 */
510 493 static zio_t *
511 494 zio_create(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
512 495 void *data, uint64_t size, zio_done_func_t *done, void *private,
513 - zio_type_t type, int priority, enum zio_flag flags,
496 + zio_type_t type, zio_priority_t priority, enum zio_flag flags,
514 497 vdev_t *vd, uint64_t offset, const zbookmark_t *zb,
515 498 enum zio_stage stage, enum zio_stage pipeline)
516 499 {
517 500 zio_t *zio;
518 501
519 502 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
520 503 ASSERT(P2PHASE(size, SPA_MINBLOCKSIZE) == 0);
521 504 ASSERT(P2PHASE(offset, SPA_MINBLOCKSIZE) == 0);
522 505
523 506 ASSERT(!vd || spa_config_held(spa, SCL_STATE_ALL, RW_READER));
524 507 ASSERT(!bp || !(flags & ZIO_FLAG_CONFIG_WRITER));
525 508 ASSERT(vd || stage == ZIO_STAGE_OPEN);
526 509
527 510 zio = kmem_cache_alloc(zio_cache, KM_SLEEP);
528 511 bzero(zio, sizeof (zio_t));
529 512
530 513 mutex_init(&zio->io_lock, NULL, MUTEX_DEFAULT, NULL);
531 514 cv_init(&zio->io_cv, NULL, CV_DEFAULT, NULL);
532 515
533 516 list_create(&zio->io_parent_list, sizeof (zio_link_t),
534 517 offsetof(zio_link_t, zl_parent_node));
535 518 list_create(&zio->io_child_list, sizeof (zio_link_t),
536 519 offsetof(zio_link_t, zl_child_node));
537 520
538 521 if (vd != NULL)
539 522 zio->io_child_type = ZIO_CHILD_VDEV;
540 523 else if (flags & ZIO_FLAG_GANG_CHILD)
541 524 zio->io_child_type = ZIO_CHILD_GANG;
542 525 else if (flags & ZIO_FLAG_DDT_CHILD)
543 526 zio->io_child_type = ZIO_CHILD_DDT;
544 527 else
545 528 zio->io_child_type = ZIO_CHILD_LOGICAL;
546 529
547 530 if (bp != NULL) {
548 531 zio->io_bp = (blkptr_t *)bp;
549 532 zio->io_bp_copy = *bp;
550 533 zio->io_bp_orig = *bp;
551 534 if (type != ZIO_TYPE_WRITE ||
552 535 zio->io_child_type == ZIO_CHILD_DDT)
553 536 zio->io_bp = &zio->io_bp_copy; /* so caller can free */
554 537 if (zio->io_child_type == ZIO_CHILD_LOGICAL)
555 538 zio->io_logical = zio;
556 539 if (zio->io_child_type > ZIO_CHILD_GANG && BP_IS_GANG(bp))
557 540 pipeline |= ZIO_GANG_STAGES;
558 541 }
559 542
560 543 zio->io_spa = spa;
561 544 zio->io_txg = txg;
562 545 zio->io_done = done;
563 546 zio->io_private = private;
564 547 zio->io_type = type;
565 548 zio->io_priority = priority;
566 549 zio->io_vd = vd;
567 550 zio->io_offset = offset;
568 551 zio->io_orig_data = zio->io_data = data;
569 552 zio->io_orig_size = zio->io_size = size;
570 553 zio->io_orig_flags = zio->io_flags = flags;
571 554 zio->io_orig_stage = zio->io_stage = stage;
572 555 zio->io_orig_pipeline = zio->io_pipeline = pipeline;
573 556
574 557 zio->io_state[ZIO_WAIT_READY] = (stage >= ZIO_STAGE_READY);
575 558 zio->io_state[ZIO_WAIT_DONE] = (stage >= ZIO_STAGE_DONE);
576 559
577 560 if (zb != NULL)
578 561 zio->io_bookmark = *zb;
579 562
580 563 if (pio != NULL) {
581 564 if (zio->io_logical == NULL)
582 565 zio->io_logical = pio->io_logical;
583 566 if (zio->io_child_type == ZIO_CHILD_GANG)
584 567 zio->io_gang_leader = pio->io_gang_leader;
585 568 zio_add_child(pio, zio);
586 569 }
587 570
588 571 return (zio);
589 572 }
590 573
591 574 static void
592 575 zio_destroy(zio_t *zio)
593 576 {
594 577 list_destroy(&zio->io_parent_list);
595 578 list_destroy(&zio->io_child_list);
596 579 mutex_destroy(&zio->io_lock);
597 580 cv_destroy(&zio->io_cv);
598 581 kmem_cache_free(zio_cache, zio);
599 582 }
600 583
601 584 zio_t *
602 585 zio_null(zio_t *pio, spa_t *spa, vdev_t *vd, zio_done_func_t *done,
603 586 void *private, enum zio_flag flags)
604 587 {
605 588 zio_t *zio;
606 589
607 590 zio = zio_create(pio, spa, 0, NULL, NULL, 0, done, private,
608 591 ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
609 592 ZIO_STAGE_OPEN, ZIO_INTERLOCK_PIPELINE);
610 593
611 594 return (zio);
612 595 }
↓ open down ↓ |
89 lines elided |
↑ open up ↑ |
613 596
614 597 zio_t *
615 598 zio_root(spa_t *spa, zio_done_func_t *done, void *private, enum zio_flag flags)
616 599 {
617 600 return (zio_null(NULL, spa, NULL, done, private, flags));
618 601 }
619 602
620 603 zio_t *
621 604 zio_read(zio_t *pio, spa_t *spa, const blkptr_t *bp,
622 605 void *data, uint64_t size, zio_done_func_t *done, void *private,
623 - int priority, enum zio_flag flags, const zbookmark_t *zb)
606 + zio_priority_t priority, enum zio_flag flags, const zbookmark_t *zb)
624 607 {
625 608 zio_t *zio;
626 609
627 610 zio = zio_create(pio, spa, BP_PHYSICAL_BIRTH(bp), bp,
628 611 data, size, done, private,
629 612 ZIO_TYPE_READ, priority, flags, NULL, 0, zb,
630 613 ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
631 614 ZIO_DDT_CHILD_READ_PIPELINE : ZIO_READ_PIPELINE);
632 615
633 616 return (zio);
634 617 }
635 618
636 619 zio_t *
637 620 zio_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
638 621 void *data, uint64_t size, const zio_prop_t *zp,
639 - zio_done_func_t *ready, zio_done_func_t *done, void *private,
640 - int priority, enum zio_flag flags, const zbookmark_t *zb)
622 + zio_done_func_t *ready, zio_done_func_t *physdone, zio_done_func_t *done,
623 + void *private,
624 + zio_priority_t priority, enum zio_flag flags, const zbookmark_t *zb)
641 625 {
642 626 zio_t *zio;
643 627
644 628 ASSERT(zp->zp_checksum >= ZIO_CHECKSUM_OFF &&
645 629 zp->zp_checksum < ZIO_CHECKSUM_FUNCTIONS &&
646 630 zp->zp_compress >= ZIO_COMPRESS_OFF &&
647 631 zp->zp_compress < ZIO_COMPRESS_FUNCTIONS &&
648 632 DMU_OT_IS_VALID(zp->zp_type) &&
649 633 zp->zp_level < 32 &&
650 634 zp->zp_copies > 0 &&
651 635 zp->zp_copies <= spa_max_replication(spa));
652 636
653 637 zio = zio_create(pio, spa, txg, bp, data, size, done, private,
654 638 ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb,
655 639 ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
656 640 ZIO_DDT_CHILD_WRITE_PIPELINE : ZIO_WRITE_PIPELINE);
657 641
658 642 zio->io_ready = ready;
643 + zio->io_physdone = physdone;
659 644 zio->io_prop = *zp;
660 645
661 646 return (zio);
662 647 }
663 648
664 649 zio_t *
665 650 zio_rewrite(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, void *data,
666 - uint64_t size, zio_done_func_t *done, void *private, int priority,
667 - enum zio_flag flags, zbookmark_t *zb)
651 + uint64_t size, zio_done_func_t *done, void *private,
652 + zio_priority_t priority, enum zio_flag flags, zbookmark_t *zb)
668 653 {
669 654 zio_t *zio;
670 655
671 656 zio = zio_create(pio, spa, txg, bp, data, size, done, private,
672 657 ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb,
673 658 ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE);
674 659
675 660 return (zio);
676 661 }
677 662
678 663 void
679 664 zio_write_override(zio_t *zio, blkptr_t *bp, int copies, boolean_t nopwrite)
680 665 {
681 666 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
682 667 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
683 668 ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
684 669 ASSERT(zio->io_txg == spa_syncing_txg(zio->io_spa));
685 670
686 671 /*
687 672 * We must reset the io_prop to match the values that existed
688 673 * when the bp was first written by dmu_sync() keeping in mind
689 674 * that nopwrite and dedup are mutually exclusive.
690 675 */
691 676 zio->io_prop.zp_dedup = nopwrite ? B_FALSE : zio->io_prop.zp_dedup;
692 677 zio->io_prop.zp_nopwrite = nopwrite;
693 678 zio->io_prop.zp_copies = copies;
694 679 zio->io_bp_override = bp;
695 680 }
696 681
697 682 void
698 683 zio_free(spa_t *spa, uint64_t txg, const blkptr_t *bp)
699 684 {
700 685 metaslab_check_free(spa, bp);
701 686
702 687 /*
703 688 * Frees that are for the currently-syncing txg, are not going to be
704 689 * deferred, and which will not need to do a read (i.e. not GANG or
705 690 * DEDUP), can be processed immediately. Otherwise, put them on the
706 691 * in-memory list for later processing.
707 692 */
708 693 if (BP_IS_GANG(bp) || BP_GET_DEDUP(bp) ||
709 694 txg != spa->spa_syncing_txg ||
710 695 spa_sync_pass(spa) >= zfs_sync_pass_deferred_free) {
711 696 bplist_append(&spa->spa_free_bplist[txg & TXG_MASK], bp);
712 697 } else {
713 698 VERIFY0(zio_wait(zio_free_sync(NULL, spa, txg, bp, 0)));
714 699 }
715 700 }
716 701
717 702 zio_t *
718 703 zio_free_sync(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
719 704 enum zio_flag flags)
720 705 {
721 706 zio_t *zio;
722 707 enum zio_stage stage = ZIO_FREE_PIPELINE;
723 708
724 709 dprintf_bp(bp, "freeing in txg %llu, pass %u",
725 710 (longlong_t)txg, spa->spa_sync_pass);
726 711
727 712 ASSERT(!BP_IS_HOLE(bp));
728 713 ASSERT(spa_syncing_txg(spa) == txg);
729 714 ASSERT(spa_sync_pass(spa) < zfs_sync_pass_deferred_free);
730 715
731 716 metaslab_check_free(spa, bp);
732 717 arc_freed(spa, bp);
↓ open down ↓ |
55 lines elided |
↑ open up ↑ |
733 718
734 719 /*
735 720 * GANG and DEDUP blocks can induce a read (for the gang block header,
736 721 * or the DDT), so issue them asynchronously so that this thread is
737 722 * not tied up.
738 723 */
739 724 if (BP_IS_GANG(bp) || BP_GET_DEDUP(bp))
740 725 stage |= ZIO_STAGE_ISSUE_ASYNC;
741 726
742 727 zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
743 - NULL, NULL, ZIO_TYPE_FREE, ZIO_PRIORITY_FREE, flags,
728 + NULL, NULL, ZIO_TYPE_FREE, ZIO_PRIORITY_NOW, flags,
744 729 NULL, 0, NULL, ZIO_STAGE_OPEN, stage);
745 730
746 731
747 732 return (zio);
748 733 }
749 734
750 735 zio_t *
751 736 zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
752 737 zio_done_func_t *done, void *private, enum zio_flag flags)
753 738 {
754 739 zio_t *zio;
755 740
756 741 /*
757 742 * A claim is an allocation of a specific block. Claims are needed
758 743 * to support immediate writes in the intent log. The issue is that
759 744 * immediate writes contain committed data, but in a txg that was
760 745 * *not* committed. Upon opening the pool after an unclean shutdown,
761 746 * the intent log claims all blocks that contain immediate write data
762 747 * so that the SPA knows they're in use.
763 748 *
764 749 * All claims *must* be resolved in the first txg -- before the SPA
765 750 * starts allocating blocks -- so that nothing is allocated twice.
766 751 * If txg == 0 we just verify that the block is claimable.
767 752 */
768 753 ASSERT3U(spa->spa_uberblock.ub_rootbp.blk_birth, <, spa_first_txg(spa));
769 754 ASSERT(txg == spa_first_txg(spa) || txg == 0);
770 755 ASSERT(!BP_GET_DEDUP(bp) || !spa_writeable(spa)); /* zdb(1M) */
↓ open down ↓ |
17 lines elided |
↑ open up ↑ |
771 756
772 757 zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
773 758 done, private, ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW, flags,
774 759 NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE);
775 760
776 761 return (zio);
777 762 }
778 763
779 764 zio_t *
780 765 zio_ioctl(zio_t *pio, spa_t *spa, vdev_t *vd, int cmd,
781 - zio_done_func_t *done, void *private, int priority, enum zio_flag flags)
766 + zio_done_func_t *done, void *private, enum zio_flag flags)
782 767 {
783 768 zio_t *zio;
784 769 int c;
785 770
786 771 if (vd->vdev_children == 0) {
787 772 zio = zio_create(pio, spa, 0, NULL, NULL, 0, done, private,
788 - ZIO_TYPE_IOCTL, priority, flags, vd, 0, NULL,
773 + ZIO_TYPE_IOCTL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
789 774 ZIO_STAGE_OPEN, ZIO_IOCTL_PIPELINE);
790 775
791 776 zio->io_cmd = cmd;
792 777 } else {
793 778 zio = zio_null(pio, spa, NULL, NULL, NULL, flags);
794 779
795 780 for (c = 0; c < vd->vdev_children; c++)
796 781 zio_nowait(zio_ioctl(zio, spa, vd->vdev_child[c], cmd,
797 - done, private, priority, flags));
782 + done, private, flags));
798 783 }
799 784
800 785 return (zio);
801 786 }
802 787
803 788 zio_t *
804 789 zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
805 790 void *data, int checksum, zio_done_func_t *done, void *private,
806 - int priority, enum zio_flag flags, boolean_t labels)
791 + zio_priority_t priority, enum zio_flag flags, boolean_t labels)
807 792 {
808 793 zio_t *zio;
809 794
810 795 ASSERT(vd->vdev_children == 0);
811 796 ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
812 797 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
813 798 ASSERT3U(offset + size, <=, vd->vdev_psize);
814 799
815 800 zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, done, private,
816 801 ZIO_TYPE_READ, priority, flags, vd, offset, NULL,
817 802 ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE);
818 803
819 804 zio->io_prop.zp_checksum = checksum;
820 805
821 806 return (zio);
822 807 }
823 808
824 809 zio_t *
825 810 zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
826 811 void *data, int checksum, zio_done_func_t *done, void *private,
827 - int priority, enum zio_flag flags, boolean_t labels)
812 + zio_priority_t priority, enum zio_flag flags, boolean_t labels)
828 813 {
829 814 zio_t *zio;
830 815
831 816 ASSERT(vd->vdev_children == 0);
832 817 ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
833 818 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
834 819 ASSERT3U(offset + size, <=, vd->vdev_psize);
835 820
836 821 zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, done, private,
837 822 ZIO_TYPE_WRITE, priority, flags, vd, offset, NULL,
838 823 ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE);
839 824
840 825 zio->io_prop.zp_checksum = checksum;
841 826
842 827 if (zio_checksum_table[checksum].ci_eck) {
843 828 /*
844 829 * zec checksums are necessarily destructive -- they modify
845 830 * the end of the write buffer to hold the verifier/checksum.
846 831 * Therefore, we must make a local copy in case the data is
847 832 * being written to multiple places in parallel.
848 833 */
849 834 void *wbuf = zio_buf_alloc(size);
850 835 bcopy(data, wbuf, size);
851 836 zio_push_transform(zio, wbuf, size, size, NULL);
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
852 837 }
853 838
854 839 return (zio);
855 840 }
856 841
857 842 /*
858 843 * Create a child I/O to do some work for us.
859 844 */
860 845 zio_t *
861 846 zio_vdev_child_io(zio_t *pio, blkptr_t *bp, vdev_t *vd, uint64_t offset,
862 - void *data, uint64_t size, int type, int priority, enum zio_flag flags,
863 - zio_done_func_t *done, void *private)
847 + void *data, uint64_t size, int type, zio_priority_t priority,
848 + enum zio_flag flags, zio_done_func_t *done, void *private)
864 849 {
865 850 enum zio_stage pipeline = ZIO_VDEV_CHILD_PIPELINE;
866 851 zio_t *zio;
867 852
868 853 ASSERT(vd->vdev_parent ==
869 854 (pio->io_vd ? pio->io_vd : pio->io_spa->spa_root_vdev));
870 855
871 856 if (type == ZIO_TYPE_READ && bp != NULL) {
872 857 /*
873 858 * If we have the bp, then the child should perform the
874 859 * checksum and the parent need not. This pushes error
875 860 * detection as close to the leaves as possible and
876 861 * eliminates redundant checksums in the interior nodes.
877 862 */
878 863 pipeline |= ZIO_STAGE_CHECKSUM_VERIFY;
879 864 pio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
880 865 }
881 866
882 867 if (vd->vdev_children == 0)
883 868 offset += VDEV_LABEL_START_SIZE;
884 869
885 870 flags |= ZIO_VDEV_CHILD_FLAGS(pio) | ZIO_FLAG_DONT_PROPAGATE;
886 871
887 872 /*
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
888 873 * If we've decided to do a repair, the write is not speculative --
889 874 * even if the original read was.
890 875 */
891 876 if (flags & ZIO_FLAG_IO_REPAIR)
892 877 flags &= ~ZIO_FLAG_SPECULATIVE;
893 878
894 879 zio = zio_create(pio, pio->io_spa, pio->io_txg, bp, data, size,
895 880 done, private, type, priority, flags, vd, offset, &pio->io_bookmark,
896 881 ZIO_STAGE_VDEV_IO_START >> 1, pipeline);
897 882
883 + zio->io_physdone = pio->io_physdone;
884 + if (vd->vdev_ops->vdev_op_leaf && zio->io_logical != NULL)
885 + zio->io_logical->io_phys_children++;
886 +
898 887 return (zio);
899 888 }
900 889
901 890 zio_t *
902 891 zio_vdev_delegated_io(vdev_t *vd, uint64_t offset, void *data, uint64_t size,
903 - int type, int priority, enum zio_flag flags,
892 + int type, zio_priority_t priority, enum zio_flag flags,
904 893 zio_done_func_t *done, void *private)
905 894 {
906 895 zio_t *zio;
907 896
908 897 ASSERT(vd->vdev_ops->vdev_op_leaf);
909 898
910 899 zio = zio_create(NULL, vd->vdev_spa, 0, NULL,
911 900 data, size, done, private, type, priority,
912 - flags | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY,
901 + flags | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY | ZIO_FLAG_DELEGATED,
913 902 vd, offset, NULL,
914 903 ZIO_STAGE_VDEV_IO_START >> 1, ZIO_VDEV_CHILD_PIPELINE);
915 904
916 905 return (zio);
917 906 }
918 907
919 908 void
920 909 zio_flush(zio_t *zio, vdev_t *vd)
921 910 {
922 911 zio_nowait(zio_ioctl(zio, zio->io_spa, vd, DKIOCFLUSHWRITECACHE,
923 - NULL, NULL, ZIO_PRIORITY_NOW,
912 + NULL, NULL,
924 913 ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY));
925 914 }
926 915
927 916 void
928 917 zio_shrink(zio_t *zio, uint64_t size)
929 918 {
930 919 ASSERT(zio->io_executor == NULL);
931 920 ASSERT(zio->io_orig_size == zio->io_size);
932 921 ASSERT(size <= zio->io_size);
933 922
934 923 /*
935 924 * We don't shrink for raidz because of problems with the
936 925 * reconstruction when reading back less than the block size.
937 926 * Note, BP_IS_RAIDZ() assumes no compression.
938 927 */
939 928 ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
940 929 if (!BP_IS_RAIDZ(zio->io_bp))
941 930 zio->io_orig_size = zio->io_size = size;
942 931 }
943 932
944 933 /*
945 934 * ==========================================================================
946 935 * Prepare to read and write logical blocks
947 936 * ==========================================================================
948 937 */
949 938
950 939 static int
951 940 zio_read_bp_init(zio_t *zio)
952 941 {
953 942 blkptr_t *bp = zio->io_bp;
954 943
955 944 if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF &&
956 945 zio->io_child_type == ZIO_CHILD_LOGICAL &&
957 946 !(zio->io_flags & ZIO_FLAG_RAW)) {
958 947 uint64_t psize = BP_GET_PSIZE(bp);
959 948 void *cbuf = zio_buf_alloc(psize);
960 949
961 950 zio_push_transform(zio, cbuf, psize, psize, zio_decompress);
962 951 }
963 952
964 953 if (!DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) && BP_GET_LEVEL(bp) == 0)
965 954 zio->io_flags |= ZIO_FLAG_DONT_CACHE;
966 955
967 956 if (BP_GET_TYPE(bp) == DMU_OT_DDT_ZAP)
968 957 zio->io_flags |= ZIO_FLAG_DONT_CACHE;
969 958
970 959 if (BP_GET_DEDUP(bp) && zio->io_child_type == ZIO_CHILD_LOGICAL)
971 960 zio->io_pipeline = ZIO_DDT_READ_PIPELINE;
972 961
973 962 return (ZIO_PIPELINE_CONTINUE);
974 963 }
975 964
976 965 static int
977 966 zio_write_bp_init(zio_t *zio)
978 967 {
979 968 spa_t *spa = zio->io_spa;
980 969 zio_prop_t *zp = &zio->io_prop;
981 970 enum zio_compress compress = zp->zp_compress;
982 971 blkptr_t *bp = zio->io_bp;
983 972 uint64_t lsize = zio->io_size;
984 973 uint64_t psize = lsize;
985 974 int pass = 1;
986 975
987 976 /*
988 977 * If our children haven't all reached the ready stage,
989 978 * wait for them and then repeat this pipeline stage.
990 979 */
991 980 if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_READY) ||
992 981 zio_wait_for_children(zio, ZIO_CHILD_LOGICAL, ZIO_WAIT_READY))
993 982 return (ZIO_PIPELINE_STOP);
994 983
995 984 if (!IO_IS_ALLOCATING(zio))
996 985 return (ZIO_PIPELINE_CONTINUE);
997 986
998 987 ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
999 988
1000 989 if (zio->io_bp_override) {
1001 990 ASSERT(bp->blk_birth != zio->io_txg);
1002 991 ASSERT(BP_GET_DEDUP(zio->io_bp_override) == 0);
1003 992
1004 993 *bp = *zio->io_bp_override;
1005 994 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1006 995
1007 996 /*
1008 997 * If we've been overridden and nopwrite is set then
1009 998 * set the flag accordingly to indicate that a nopwrite
1010 999 * has already occurred.
1011 1000 */
1012 1001 if (!BP_IS_HOLE(bp) && zp->zp_nopwrite) {
1013 1002 ASSERT(!zp->zp_dedup);
1014 1003 zio->io_flags |= ZIO_FLAG_NOPWRITE;
1015 1004 return (ZIO_PIPELINE_CONTINUE);
1016 1005 }
1017 1006
1018 1007 ASSERT(!zp->zp_nopwrite);
1019 1008
1020 1009 if (BP_IS_HOLE(bp) || !zp->zp_dedup)
1021 1010 return (ZIO_PIPELINE_CONTINUE);
1022 1011
1023 1012 ASSERT(zio_checksum_table[zp->zp_checksum].ci_dedup ||
1024 1013 zp->zp_dedup_verify);
1025 1014
1026 1015 if (BP_GET_CHECKSUM(bp) == zp->zp_checksum) {
1027 1016 BP_SET_DEDUP(bp, 1);
1028 1017 zio->io_pipeline |= ZIO_STAGE_DDT_WRITE;
1029 1018 return (ZIO_PIPELINE_CONTINUE);
1030 1019 }
1031 1020 zio->io_bp_override = NULL;
1032 1021 BP_ZERO(bp);
1033 1022 }
1034 1023
1035 1024 if (bp->blk_birth == zio->io_txg) {
1036 1025 /*
1037 1026 * We're rewriting an existing block, which means we're
1038 1027 * working on behalf of spa_sync(). For spa_sync() to
1039 1028 * converge, it must eventually be the case that we don't
1040 1029 * have to allocate new blocks. But compression changes
1041 1030 * the blocksize, which forces a reallocate, and makes
1042 1031 * convergence take longer. Therefore, after the first
1043 1032 * few passes, stop compressing to ensure convergence.
1044 1033 */
1045 1034 pass = spa_sync_pass(spa);
1046 1035
1047 1036 ASSERT(zio->io_txg == spa_syncing_txg(spa));
1048 1037 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1049 1038 ASSERT(!BP_GET_DEDUP(bp));
1050 1039
1051 1040 if (pass >= zfs_sync_pass_dont_compress)
1052 1041 compress = ZIO_COMPRESS_OFF;
1053 1042
1054 1043 /* Make sure someone doesn't change their mind on overwrites */
1055 1044 ASSERT(MIN(zp->zp_copies + BP_IS_GANG(bp),
1056 1045 spa_max_replication(spa)) == BP_GET_NDVAS(bp));
1057 1046 }
1058 1047
1059 1048 if (compress != ZIO_COMPRESS_OFF) {
1060 1049 void *cbuf = zio_buf_alloc(lsize);
1061 1050 psize = zio_compress_data(compress, zio->io_data, cbuf, lsize);
1062 1051 if (psize == 0 || psize == lsize) {
1063 1052 compress = ZIO_COMPRESS_OFF;
1064 1053 zio_buf_free(cbuf, lsize);
1065 1054 } else {
1066 1055 ASSERT(psize < lsize);
1067 1056 zio_push_transform(zio, cbuf, psize, lsize, NULL);
1068 1057 }
1069 1058 }
1070 1059
1071 1060 /*
1072 1061 * The final pass of spa_sync() must be all rewrites, but the first
1073 1062 * few passes offer a trade-off: allocating blocks defers convergence,
1074 1063 * but newly allocated blocks are sequential, so they can be written
1075 1064 * to disk faster. Therefore, we allow the first few passes of
1076 1065 * spa_sync() to allocate new blocks, but force rewrites after that.
1077 1066 * There should only be a handful of blocks after pass 1 in any case.
1078 1067 */
1079 1068 if (bp->blk_birth == zio->io_txg && BP_GET_PSIZE(bp) == psize &&
1080 1069 pass >= zfs_sync_pass_rewrite) {
1081 1070 ASSERT(psize != 0);
1082 1071 enum zio_stage gang_stages = zio->io_pipeline & ZIO_GANG_STAGES;
1083 1072 zio->io_pipeline = ZIO_REWRITE_PIPELINE | gang_stages;
1084 1073 zio->io_flags |= ZIO_FLAG_IO_REWRITE;
1085 1074 } else {
1086 1075 BP_ZERO(bp);
1087 1076 zio->io_pipeline = ZIO_WRITE_PIPELINE;
1088 1077 }
1089 1078
1090 1079 if (psize == 0) {
1091 1080 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1092 1081 } else {
1093 1082 ASSERT(zp->zp_checksum != ZIO_CHECKSUM_GANG_HEADER);
1094 1083 BP_SET_LSIZE(bp, lsize);
1095 1084 BP_SET_PSIZE(bp, psize);
1096 1085 BP_SET_COMPRESS(bp, compress);
1097 1086 BP_SET_CHECKSUM(bp, zp->zp_checksum);
1098 1087 BP_SET_TYPE(bp, zp->zp_type);
1099 1088 BP_SET_LEVEL(bp, zp->zp_level);
1100 1089 BP_SET_DEDUP(bp, zp->zp_dedup);
1101 1090 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
1102 1091 if (zp->zp_dedup) {
1103 1092 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1104 1093 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
1105 1094 zio->io_pipeline = ZIO_DDT_WRITE_PIPELINE;
1106 1095 }
1107 1096 if (zp->zp_nopwrite) {
1108 1097 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1109 1098 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
1110 1099 zio->io_pipeline |= ZIO_STAGE_NOP_WRITE;
1111 1100 }
1112 1101 }
1113 1102
1114 1103 return (ZIO_PIPELINE_CONTINUE);
1115 1104 }
1116 1105
1117 1106 static int
1118 1107 zio_free_bp_init(zio_t *zio)
1119 1108 {
1120 1109 blkptr_t *bp = zio->io_bp;
1121 1110
1122 1111 if (zio->io_child_type == ZIO_CHILD_LOGICAL) {
1123 1112 if (BP_GET_DEDUP(bp))
1124 1113 zio->io_pipeline = ZIO_DDT_FREE_PIPELINE;
1125 1114 }
1126 1115
1127 1116 return (ZIO_PIPELINE_CONTINUE);
1128 1117 }
1129 1118
1130 1119 /*
1131 1120 * ==========================================================================
1132 1121 * Execute the I/O pipeline
1133 1122 * ==========================================================================
1134 1123 */
1135 1124
1136 1125 static void
1137 1126 zio_taskq_dispatch(zio_t *zio, zio_taskq_type_t q, boolean_t cutinline)
1138 1127 {
1139 1128 spa_t *spa = zio->io_spa;
1140 1129 zio_type_t t = zio->io_type;
1141 1130 int flags = (cutinline ? TQ_FRONT : 0);
1142 1131
1143 1132 /*
1144 1133 * If we're a config writer or a probe, the normal issue and
1145 1134 * interrupt threads may all be blocked waiting for the config lock.
1146 1135 * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL.
1147 1136 */
1148 1137 if (zio->io_flags & (ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_PROBE))
1149 1138 t = ZIO_TYPE_NULL;
1150 1139
1151 1140 /*
1152 1141 * A similar issue exists for the L2ARC write thread until L2ARC 2.0.
1153 1142 */
1154 1143 if (t == ZIO_TYPE_WRITE && zio->io_vd && zio->io_vd->vdev_aux)
1155 1144 t = ZIO_TYPE_NULL;
1156 1145
1157 1146 /*
1158 1147 * If this is a high priority I/O, then use the high priority taskq if
1159 1148 * available.
1160 1149 */
1161 1150 if (zio->io_priority == ZIO_PRIORITY_NOW &&
1162 1151 spa->spa_zio_taskq[t][q + 1].stqs_count != 0)
1163 1152 q++;
1164 1153
1165 1154 ASSERT3U(q, <, ZIO_TASKQ_TYPES);
1166 1155
1167 1156 /*
1168 1157 * NB: We are assuming that the zio can only be dispatched
1169 1158 * to a single taskq at a time. It would be a grievous error
1170 1159 * to dispatch the zio to another taskq at the same time.
1171 1160 */
1172 1161 ASSERT(zio->io_tqent.tqent_next == NULL);
1173 1162 spa_taskq_dispatch_ent(spa, t, q, (task_func_t *)zio_execute, zio,
1174 1163 flags, &zio->io_tqent);
1175 1164 }
1176 1165
1177 1166 static boolean_t
1178 1167 zio_taskq_member(zio_t *zio, zio_taskq_type_t q)
1179 1168 {
1180 1169 kthread_t *executor = zio->io_executor;
1181 1170 spa_t *spa = zio->io_spa;
1182 1171
1183 1172 for (zio_type_t t = 0; t < ZIO_TYPES; t++) {
1184 1173 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
1185 1174 uint_t i;
1186 1175 for (i = 0; i < tqs->stqs_count; i++) {
1187 1176 if (taskq_member(tqs->stqs_taskq[i], executor))
1188 1177 return (B_TRUE);
1189 1178 }
1190 1179 }
1191 1180
1192 1181 return (B_FALSE);
1193 1182 }
1194 1183
1195 1184 static int
1196 1185 zio_issue_async(zio_t *zio)
1197 1186 {
1198 1187 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
1199 1188
1200 1189 return (ZIO_PIPELINE_STOP);
1201 1190 }
1202 1191
1203 1192 void
1204 1193 zio_interrupt(zio_t *zio)
1205 1194 {
1206 1195 zio_taskq_dispatch(zio, ZIO_TASKQ_INTERRUPT, B_FALSE);
1207 1196 }
1208 1197
1209 1198 /*
1210 1199 * Execute the I/O pipeline until one of the following occurs:
1211 1200 *
1212 1201 * (1) the I/O completes
1213 1202 * (2) the pipeline stalls waiting for dependent child I/Os
1214 1203 * (3) the I/O issues, so we're waiting for an I/O completion interrupt
1215 1204 * (4) the I/O is delegated by vdev-level caching or aggregation
1216 1205 * (5) the I/O is deferred due to vdev-level queueing
1217 1206 * (6) the I/O is handed off to another thread.
1218 1207 *
1219 1208 * In all cases, the pipeline stops whenever there's no CPU work; it never
1220 1209 * burns a thread in cv_wait().
1221 1210 *
1222 1211 * There's no locking on io_stage because there's no legitimate way
1223 1212 * for multiple threads to be attempting to process the same I/O.
1224 1213 */
1225 1214 static zio_pipe_stage_t *zio_pipeline[];
1226 1215
1227 1216 void
1228 1217 zio_execute(zio_t *zio)
1229 1218 {
1230 1219 zio->io_executor = curthread;
1231 1220
1232 1221 while (zio->io_stage < ZIO_STAGE_DONE) {
1233 1222 enum zio_stage pipeline = zio->io_pipeline;
1234 1223 enum zio_stage stage = zio->io_stage;
1235 1224 int rv;
1236 1225
1237 1226 ASSERT(!MUTEX_HELD(&zio->io_lock));
1238 1227 ASSERT(ISP2(stage));
1239 1228 ASSERT(zio->io_stall == NULL);
1240 1229
1241 1230 do {
1242 1231 stage <<= 1;
1243 1232 } while ((stage & pipeline) == 0);
1244 1233
1245 1234 ASSERT(stage <= ZIO_STAGE_DONE);
1246 1235
1247 1236 /*
1248 1237 * If we are in interrupt context and this pipeline stage
1249 1238 * will grab a config lock that is held across I/O,
1250 1239 * or may wait for an I/O that needs an interrupt thread
1251 1240 * to complete, issue async to avoid deadlock.
1252 1241 *
1253 1242 * For VDEV_IO_START, we cut in line so that the io will
1254 1243 * be sent to disk promptly.
1255 1244 */
1256 1245 if ((stage & ZIO_BLOCKING_STAGES) && zio->io_vd == NULL &&
1257 1246 zio_taskq_member(zio, ZIO_TASKQ_INTERRUPT)) {
1258 1247 boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ?
1259 1248 zio_requeue_io_start_cut_in_line : B_FALSE;
1260 1249 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut);
1261 1250 return;
1262 1251 }
1263 1252
1264 1253 zio->io_stage = stage;
1265 1254 rv = zio_pipeline[highbit(stage) - 1](zio);
1266 1255
1267 1256 if (rv == ZIO_PIPELINE_STOP)
1268 1257 return;
1269 1258
1270 1259 ASSERT(rv == ZIO_PIPELINE_CONTINUE);
1271 1260 }
1272 1261 }
1273 1262
1274 1263 /*
1275 1264 * ==========================================================================
1276 1265 * Initiate I/O, either sync or async
1277 1266 * ==========================================================================
1278 1267 */
1279 1268 int
1280 1269 zio_wait(zio_t *zio)
1281 1270 {
1282 1271 int error;
1283 1272
1284 1273 ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
1285 1274 ASSERT(zio->io_executor == NULL);
1286 1275
1287 1276 zio->io_waiter = curthread;
1288 1277
1289 1278 zio_execute(zio);
1290 1279
1291 1280 mutex_enter(&zio->io_lock);
1292 1281 while (zio->io_executor != NULL)
1293 1282 cv_wait(&zio->io_cv, &zio->io_lock);
1294 1283 mutex_exit(&zio->io_lock);
1295 1284
1296 1285 error = zio->io_error;
1297 1286 zio_destroy(zio);
1298 1287
1299 1288 return (error);
1300 1289 }
1301 1290
1302 1291 void
1303 1292 zio_nowait(zio_t *zio)
1304 1293 {
1305 1294 ASSERT(zio->io_executor == NULL);
1306 1295
1307 1296 if (zio->io_child_type == ZIO_CHILD_LOGICAL &&
1308 1297 zio_unique_parent(zio) == NULL) {
1309 1298 /*
1310 1299 * This is a logical async I/O with no parent to wait for it.
1311 1300 * We add it to the spa_async_root_zio "Godfather" I/O which
1312 1301 * will ensure they complete prior to unloading the pool.
1313 1302 */
1314 1303 spa_t *spa = zio->io_spa;
1315 1304
1316 1305 zio_add_child(spa->spa_async_zio_root, zio);
1317 1306 }
1318 1307
1319 1308 zio_execute(zio);
1320 1309 }
1321 1310
1322 1311 /*
1323 1312 * ==========================================================================
1324 1313 * Reexecute or suspend/resume failed I/O
1325 1314 * ==========================================================================
1326 1315 */
1327 1316
1328 1317 static void
1329 1318 zio_reexecute(zio_t *pio)
1330 1319 {
1331 1320 zio_t *cio, *cio_next;
1332 1321
1333 1322 ASSERT(pio->io_child_type == ZIO_CHILD_LOGICAL);
1334 1323 ASSERT(pio->io_orig_stage == ZIO_STAGE_OPEN);
1335 1324 ASSERT(pio->io_gang_leader == NULL);
1336 1325 ASSERT(pio->io_gang_tree == NULL);
1337 1326
1338 1327 pio->io_flags = pio->io_orig_flags;
1339 1328 pio->io_stage = pio->io_orig_stage;
1340 1329 pio->io_pipeline = pio->io_orig_pipeline;
1341 1330 pio->io_reexecute = 0;
1342 1331 pio->io_flags |= ZIO_FLAG_REEXECUTED;
1343 1332 pio->io_error = 0;
1344 1333 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
1345 1334 pio->io_state[w] = 0;
1346 1335 for (int c = 0; c < ZIO_CHILD_TYPES; c++)
1347 1336 pio->io_child_error[c] = 0;
1348 1337
1349 1338 if (IO_IS_ALLOCATING(pio))
1350 1339 BP_ZERO(pio->io_bp);
1351 1340
1352 1341 /*
1353 1342 * As we reexecute pio's children, new children could be created.
1354 1343 * New children go to the head of pio's io_child_list, however,
1355 1344 * so we will (correctly) not reexecute them. The key is that
1356 1345 * the remainder of pio's io_child_list, from 'cio_next' onward,
1357 1346 * cannot be affected by any side effects of reexecuting 'cio'.
1358 1347 */
1359 1348 for (cio = zio_walk_children(pio); cio != NULL; cio = cio_next) {
1360 1349 cio_next = zio_walk_children(pio);
1361 1350 mutex_enter(&pio->io_lock);
1362 1351 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
1363 1352 pio->io_children[cio->io_child_type][w]++;
1364 1353 mutex_exit(&pio->io_lock);
1365 1354 zio_reexecute(cio);
1366 1355 }
1367 1356
1368 1357 /*
1369 1358 * Now that all children have been reexecuted, execute the parent.
1370 1359 * We don't reexecute "The Godfather" I/O here as it's the
1371 1360 * responsibility of the caller to wait on him.
1372 1361 */
1373 1362 if (!(pio->io_flags & ZIO_FLAG_GODFATHER))
1374 1363 zio_execute(pio);
1375 1364 }
1376 1365
1377 1366 void
1378 1367 zio_suspend(spa_t *spa, zio_t *zio)
1379 1368 {
1380 1369 if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_PANIC)
1381 1370 fm_panic("Pool '%s' has encountered an uncorrectable I/O "
1382 1371 "failure and the failure mode property for this pool "
1383 1372 "is set to panic.", spa_name(spa));
1384 1373
1385 1374 zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE, spa, NULL, NULL, 0, 0);
1386 1375
1387 1376 mutex_enter(&spa->spa_suspend_lock);
1388 1377
1389 1378 if (spa->spa_suspend_zio_root == NULL)
1390 1379 spa->spa_suspend_zio_root = zio_root(spa, NULL, NULL,
1391 1380 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
1392 1381 ZIO_FLAG_GODFATHER);
1393 1382
1394 1383 spa->spa_suspended = B_TRUE;
1395 1384
1396 1385 if (zio != NULL) {
1397 1386 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
1398 1387 ASSERT(zio != spa->spa_suspend_zio_root);
1399 1388 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1400 1389 ASSERT(zio_unique_parent(zio) == NULL);
1401 1390 ASSERT(zio->io_stage == ZIO_STAGE_DONE);
1402 1391 zio_add_child(spa->spa_suspend_zio_root, zio);
1403 1392 }
1404 1393
1405 1394 mutex_exit(&spa->spa_suspend_lock);
1406 1395 }
1407 1396
1408 1397 int
1409 1398 zio_resume(spa_t *spa)
1410 1399 {
1411 1400 zio_t *pio;
1412 1401
1413 1402 /*
1414 1403 * Reexecute all previously suspended i/o.
1415 1404 */
1416 1405 mutex_enter(&spa->spa_suspend_lock);
1417 1406 spa->spa_suspended = B_FALSE;
1418 1407 cv_broadcast(&spa->spa_suspend_cv);
1419 1408 pio = spa->spa_suspend_zio_root;
1420 1409 spa->spa_suspend_zio_root = NULL;
1421 1410 mutex_exit(&spa->spa_suspend_lock);
1422 1411
1423 1412 if (pio == NULL)
1424 1413 return (0);
1425 1414
1426 1415 zio_reexecute(pio);
1427 1416 return (zio_wait(pio));
1428 1417 }
1429 1418
1430 1419 void
1431 1420 zio_resume_wait(spa_t *spa)
1432 1421 {
1433 1422 mutex_enter(&spa->spa_suspend_lock);
1434 1423 while (spa_suspended(spa))
1435 1424 cv_wait(&spa->spa_suspend_cv, &spa->spa_suspend_lock);
1436 1425 mutex_exit(&spa->spa_suspend_lock);
1437 1426 }
1438 1427
1439 1428 /*
1440 1429 * ==========================================================================
1441 1430 * Gang blocks.
1442 1431 *
1443 1432 * A gang block is a collection of small blocks that looks to the DMU
1444 1433 * like one large block. When zio_dva_allocate() cannot find a block
1445 1434 * of the requested size, due to either severe fragmentation or the pool
1446 1435 * being nearly full, it calls zio_write_gang_block() to construct the
1447 1436 * block from smaller fragments.
1448 1437 *
1449 1438 * A gang block consists of a gang header (zio_gbh_phys_t) and up to
1450 1439 * three (SPA_GBH_NBLKPTRS) gang members. The gang header is just like
1451 1440 * an indirect block: it's an array of block pointers. It consumes
1452 1441 * only one sector and hence is allocatable regardless of fragmentation.
1453 1442 * The gang header's bps point to its gang members, which hold the data.
1454 1443 *
1455 1444 * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg>
1456 1445 * as the verifier to ensure uniqueness of the SHA256 checksum.
1457 1446 * Critically, the gang block bp's blk_cksum is the checksum of the data,
1458 1447 * not the gang header. This ensures that data block signatures (needed for
1459 1448 * deduplication) are independent of how the block is physically stored.
1460 1449 *
1461 1450 * Gang blocks can be nested: a gang member may itself be a gang block.
1462 1451 * Thus every gang block is a tree in which root and all interior nodes are
1463 1452 * gang headers, and the leaves are normal blocks that contain user data.
1464 1453 * The root of the gang tree is called the gang leader.
1465 1454 *
1466 1455 * To perform any operation (read, rewrite, free, claim) on a gang block,
1467 1456 * zio_gang_assemble() first assembles the gang tree (minus data leaves)
1468 1457 * in the io_gang_tree field of the original logical i/o by recursively
1469 1458 * reading the gang leader and all gang headers below it. This yields
1470 1459 * an in-core tree containing the contents of every gang header and the
1471 1460 * bps for every constituent of the gang block.
1472 1461 *
1473 1462 * With the gang tree now assembled, zio_gang_issue() just walks the gang tree
1474 1463 * and invokes a callback on each bp. To free a gang block, zio_gang_issue()
1475 1464 * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp.
1476 1465 * zio_claim_gang() provides a similarly trivial wrapper for zio_claim().
1477 1466 * zio_read_gang() is a wrapper around zio_read() that omits reading gang
1478 1467 * headers, since we already have those in io_gang_tree. zio_rewrite_gang()
1479 1468 * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite()
1480 1469 * of the gang header plus zio_checksum_compute() of the data to update the
1481 1470 * gang header's blk_cksum as described above.
1482 1471 *
1483 1472 * The two-phase assemble/issue model solves the problem of partial failure --
1484 1473 * what if you'd freed part of a gang block but then couldn't read the
1485 1474 * gang header for another part? Assembling the entire gang tree first
1486 1475 * ensures that all the necessary gang header I/O has succeeded before
1487 1476 * starting the actual work of free, claim, or write. Once the gang tree
1488 1477 * is assembled, free and claim are in-memory operations that cannot fail.
1489 1478 *
1490 1479 * In the event that a gang write fails, zio_dva_unallocate() walks the
1491 1480 * gang tree to immediately free (i.e. insert back into the space map)
1492 1481 * everything we've allocated. This ensures that we don't get ENOSPC
1493 1482 * errors during repeated suspend/resume cycles due to a flaky device.
1494 1483 *
1495 1484 * Gang rewrites only happen during sync-to-convergence. If we can't assemble
1496 1485 * the gang tree, we won't modify the block, so we can safely defer the free
1497 1486 * (knowing that the block is still intact). If we *can* assemble the gang
1498 1487 * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free
1499 1488 * each constituent bp and we can allocate a new block on the next sync pass.
1500 1489 *
1501 1490 * In all cases, the gang tree allows complete recovery from partial failure.
1502 1491 * ==========================================================================
1503 1492 */
1504 1493
1505 1494 static zio_t *
1506 1495 zio_read_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1507 1496 {
1508 1497 if (gn != NULL)
1509 1498 return (pio);
1510 1499
1511 1500 return (zio_read(pio, pio->io_spa, bp, data, BP_GET_PSIZE(bp),
1512 1501 NULL, NULL, pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
1513 1502 &pio->io_bookmark));
1514 1503 }
1515 1504
1516 1505 zio_t *
1517 1506 zio_rewrite_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1518 1507 {
1519 1508 zio_t *zio;
1520 1509
1521 1510 if (gn != NULL) {
1522 1511 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
1523 1512 gn->gn_gbh, SPA_GANGBLOCKSIZE, NULL, NULL, pio->io_priority,
1524 1513 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
1525 1514 /*
1526 1515 * As we rewrite each gang header, the pipeline will compute
1527 1516 * a new gang block header checksum for it; but no one will
1528 1517 * compute a new data checksum, so we do that here. The one
1529 1518 * exception is the gang leader: the pipeline already computed
1530 1519 * its data checksum because that stage precedes gang assembly.
1531 1520 * (Presently, nothing actually uses interior data checksums;
1532 1521 * this is just good hygiene.)
1533 1522 */
1534 1523 if (gn != pio->io_gang_leader->io_gang_tree) {
1535 1524 zio_checksum_compute(zio, BP_GET_CHECKSUM(bp),
1536 1525 data, BP_GET_PSIZE(bp));
1537 1526 }
1538 1527 /*
1539 1528 * If we are here to damage data for testing purposes,
1540 1529 * leave the GBH alone so that we can detect the damage.
1541 1530 */
1542 1531 if (pio->io_gang_leader->io_flags & ZIO_FLAG_INDUCE_DAMAGE)
1543 1532 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
1544 1533 } else {
1545 1534 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
1546 1535 data, BP_GET_PSIZE(bp), NULL, NULL, pio->io_priority,
1547 1536 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
1548 1537 }
1549 1538
1550 1539 return (zio);
1551 1540 }
1552 1541
1553 1542 /* ARGSUSED */
1554 1543 zio_t *
1555 1544 zio_free_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1556 1545 {
1557 1546 return (zio_free_sync(pio, pio->io_spa, pio->io_txg, bp,
1558 1547 ZIO_GANG_CHILD_FLAGS(pio)));
1559 1548 }
1560 1549
1561 1550 /* ARGSUSED */
1562 1551 zio_t *
1563 1552 zio_claim_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1564 1553 {
1565 1554 return (zio_claim(pio, pio->io_spa, pio->io_txg, bp,
1566 1555 NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio)));
1567 1556 }
1568 1557
1569 1558 static zio_gang_issue_func_t *zio_gang_issue_func[ZIO_TYPES] = {
1570 1559 NULL,
1571 1560 zio_read_gang,
1572 1561 zio_rewrite_gang,
1573 1562 zio_free_gang,
1574 1563 zio_claim_gang,
1575 1564 NULL
1576 1565 };
1577 1566
1578 1567 static void zio_gang_tree_assemble_done(zio_t *zio);
1579 1568
1580 1569 static zio_gang_node_t *
1581 1570 zio_gang_node_alloc(zio_gang_node_t **gnpp)
1582 1571 {
1583 1572 zio_gang_node_t *gn;
1584 1573
1585 1574 ASSERT(*gnpp == NULL);
1586 1575
1587 1576 gn = kmem_zalloc(sizeof (*gn), KM_SLEEP);
1588 1577 gn->gn_gbh = zio_buf_alloc(SPA_GANGBLOCKSIZE);
1589 1578 *gnpp = gn;
1590 1579
1591 1580 return (gn);
1592 1581 }
1593 1582
1594 1583 static void
1595 1584 zio_gang_node_free(zio_gang_node_t **gnpp)
1596 1585 {
1597 1586 zio_gang_node_t *gn = *gnpp;
1598 1587
1599 1588 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
1600 1589 ASSERT(gn->gn_child[g] == NULL);
1601 1590
1602 1591 zio_buf_free(gn->gn_gbh, SPA_GANGBLOCKSIZE);
1603 1592 kmem_free(gn, sizeof (*gn));
1604 1593 *gnpp = NULL;
1605 1594 }
1606 1595
1607 1596 static void
1608 1597 zio_gang_tree_free(zio_gang_node_t **gnpp)
1609 1598 {
1610 1599 zio_gang_node_t *gn = *gnpp;
1611 1600
1612 1601 if (gn == NULL)
1613 1602 return;
1614 1603
1615 1604 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
1616 1605 zio_gang_tree_free(&gn->gn_child[g]);
1617 1606
1618 1607 zio_gang_node_free(gnpp);
1619 1608 }
1620 1609
1621 1610 static void
1622 1611 zio_gang_tree_assemble(zio_t *gio, blkptr_t *bp, zio_gang_node_t **gnpp)
1623 1612 {
1624 1613 zio_gang_node_t *gn = zio_gang_node_alloc(gnpp);
1625 1614
1626 1615 ASSERT(gio->io_gang_leader == gio);
1627 1616 ASSERT(BP_IS_GANG(bp));
1628 1617
1629 1618 zio_nowait(zio_read(gio, gio->io_spa, bp, gn->gn_gbh,
1630 1619 SPA_GANGBLOCKSIZE, zio_gang_tree_assemble_done, gn,
1631 1620 gio->io_priority, ZIO_GANG_CHILD_FLAGS(gio), &gio->io_bookmark));
1632 1621 }
1633 1622
1634 1623 static void
1635 1624 zio_gang_tree_assemble_done(zio_t *zio)
1636 1625 {
1637 1626 zio_t *gio = zio->io_gang_leader;
1638 1627 zio_gang_node_t *gn = zio->io_private;
1639 1628 blkptr_t *bp = zio->io_bp;
1640 1629
1641 1630 ASSERT(gio == zio_unique_parent(zio));
1642 1631 ASSERT(zio->io_child_count == 0);
1643 1632
1644 1633 if (zio->io_error)
1645 1634 return;
1646 1635
1647 1636 if (BP_SHOULD_BYTESWAP(bp))
1648 1637 byteswap_uint64_array(zio->io_data, zio->io_size);
1649 1638
1650 1639 ASSERT(zio->io_data == gn->gn_gbh);
1651 1640 ASSERT(zio->io_size == SPA_GANGBLOCKSIZE);
1652 1641 ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
1653 1642
1654 1643 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
1655 1644 blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
1656 1645 if (!BP_IS_GANG(gbp))
1657 1646 continue;
1658 1647 zio_gang_tree_assemble(gio, gbp, &gn->gn_child[g]);
1659 1648 }
1660 1649 }
1661 1650
1662 1651 static void
1663 1652 zio_gang_tree_issue(zio_t *pio, zio_gang_node_t *gn, blkptr_t *bp, void *data)
1664 1653 {
1665 1654 zio_t *gio = pio->io_gang_leader;
1666 1655 zio_t *zio;
1667 1656
1668 1657 ASSERT(BP_IS_GANG(bp) == !!gn);
1669 1658 ASSERT(BP_GET_CHECKSUM(bp) == BP_GET_CHECKSUM(gio->io_bp));
1670 1659 ASSERT(BP_GET_LSIZE(bp) == BP_GET_PSIZE(bp) || gn == gio->io_gang_tree);
1671 1660
1672 1661 /*
1673 1662 * If you're a gang header, your data is in gn->gn_gbh.
1674 1663 * If you're a gang member, your data is in 'data' and gn == NULL.
1675 1664 */
1676 1665 zio = zio_gang_issue_func[gio->io_type](pio, bp, gn, data);
1677 1666
1678 1667 if (gn != NULL) {
1679 1668 ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
1680 1669
1681 1670 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
1682 1671 blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
1683 1672 if (BP_IS_HOLE(gbp))
1684 1673 continue;
1685 1674 zio_gang_tree_issue(zio, gn->gn_child[g], gbp, data);
1686 1675 data = (char *)data + BP_GET_PSIZE(gbp);
1687 1676 }
1688 1677 }
1689 1678
1690 1679 if (gn == gio->io_gang_tree)
1691 1680 ASSERT3P((char *)gio->io_data + gio->io_size, ==, data);
1692 1681
1693 1682 if (zio != pio)
1694 1683 zio_nowait(zio);
1695 1684 }
1696 1685
1697 1686 static int
1698 1687 zio_gang_assemble(zio_t *zio)
1699 1688 {
1700 1689 blkptr_t *bp = zio->io_bp;
1701 1690
1702 1691 ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == NULL);
1703 1692 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
1704 1693
1705 1694 zio->io_gang_leader = zio;
1706 1695
1707 1696 zio_gang_tree_assemble(zio, bp, &zio->io_gang_tree);
1708 1697
1709 1698 return (ZIO_PIPELINE_CONTINUE);
1710 1699 }
1711 1700
1712 1701 static int
1713 1702 zio_gang_issue(zio_t *zio)
1714 1703 {
1715 1704 blkptr_t *bp = zio->io_bp;
1716 1705
1717 1706 if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE))
1718 1707 return (ZIO_PIPELINE_STOP);
1719 1708
1720 1709 ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == zio);
1721 1710 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
1722 1711
1723 1712 if (zio->io_child_error[ZIO_CHILD_GANG] == 0)
1724 1713 zio_gang_tree_issue(zio, zio->io_gang_tree, bp, zio->io_data);
1725 1714 else
1726 1715 zio_gang_tree_free(&zio->io_gang_tree);
1727 1716
1728 1717 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1729 1718
1730 1719 return (ZIO_PIPELINE_CONTINUE);
1731 1720 }
1732 1721
1733 1722 static void
1734 1723 zio_write_gang_member_ready(zio_t *zio)
1735 1724 {
1736 1725 zio_t *pio = zio_unique_parent(zio);
1737 1726 zio_t *gio = zio->io_gang_leader;
1738 1727 dva_t *cdva = zio->io_bp->blk_dva;
1739 1728 dva_t *pdva = pio->io_bp->blk_dva;
1740 1729 uint64_t asize;
1741 1730
1742 1731 if (BP_IS_HOLE(zio->io_bp))
1743 1732 return;
1744 1733
1745 1734 ASSERT(BP_IS_HOLE(&zio->io_bp_orig));
1746 1735
1747 1736 ASSERT(zio->io_child_type == ZIO_CHILD_GANG);
1748 1737 ASSERT3U(zio->io_prop.zp_copies, ==, gio->io_prop.zp_copies);
1749 1738 ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(zio->io_bp));
1750 1739 ASSERT3U(pio->io_prop.zp_copies, <=, BP_GET_NDVAS(pio->io_bp));
1751 1740 ASSERT3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp));
1752 1741
1753 1742 mutex_enter(&pio->io_lock);
1754 1743 for (int d = 0; d < BP_GET_NDVAS(zio->io_bp); d++) {
1755 1744 ASSERT(DVA_GET_GANG(&pdva[d]));
1756 1745 asize = DVA_GET_ASIZE(&pdva[d]);
1757 1746 asize += DVA_GET_ASIZE(&cdva[d]);
1758 1747 DVA_SET_ASIZE(&pdva[d], asize);
1759 1748 }
1760 1749 mutex_exit(&pio->io_lock);
1761 1750 }
1762 1751
1763 1752 static int
1764 1753 zio_write_gang_block(zio_t *pio)
1765 1754 {
1766 1755 spa_t *spa = pio->io_spa;
1767 1756 blkptr_t *bp = pio->io_bp;
1768 1757 zio_t *gio = pio->io_gang_leader;
1769 1758 zio_t *zio;
1770 1759 zio_gang_node_t *gn, **gnpp;
1771 1760 zio_gbh_phys_t *gbh;
1772 1761 uint64_t txg = pio->io_txg;
1773 1762 uint64_t resid = pio->io_size;
1774 1763 uint64_t lsize;
1775 1764 int copies = gio->io_prop.zp_copies;
1776 1765 int gbh_copies = MIN(copies + 1, spa_max_replication(spa));
1777 1766 zio_prop_t zp;
1778 1767 int error;
1779 1768
1780 1769 error = metaslab_alloc(spa, spa_normal_class(spa), SPA_GANGBLOCKSIZE,
1781 1770 bp, gbh_copies, txg, pio == gio ? NULL : gio->io_bp,
1782 1771 METASLAB_HINTBP_FAVOR | METASLAB_GANG_HEADER);
1783 1772 if (error) {
1784 1773 pio->io_error = error;
1785 1774 return (ZIO_PIPELINE_CONTINUE);
1786 1775 }
1787 1776
1788 1777 if (pio == gio) {
1789 1778 gnpp = &gio->io_gang_tree;
1790 1779 } else {
1791 1780 gnpp = pio->io_private;
1792 1781 ASSERT(pio->io_ready == zio_write_gang_member_ready);
1793 1782 }
1794 1783
1795 1784 gn = zio_gang_node_alloc(gnpp);
1796 1785 gbh = gn->gn_gbh;
1797 1786 bzero(gbh, SPA_GANGBLOCKSIZE);
1798 1787
1799 1788 /*
1800 1789 * Create the gang header.
1801 1790 */
1802 1791 zio = zio_rewrite(pio, spa, txg, bp, gbh, SPA_GANGBLOCKSIZE, NULL, NULL,
1803 1792 pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
1804 1793
1805 1794 /*
1806 1795 * Create and nowait the gang children.
1807 1796 */
1808 1797 for (int g = 0; resid != 0; resid -= lsize, g++) {
1809 1798 lsize = P2ROUNDUP(resid / (SPA_GBH_NBLKPTRS - g),
1810 1799 SPA_MINBLOCKSIZE);
1811 1800 ASSERT(lsize >= SPA_MINBLOCKSIZE && lsize <= resid);
1812 1801
1813 1802 zp.zp_checksum = gio->io_prop.zp_checksum;
↓ open down ↓ |
880 lines elided |
↑ open up ↑ |
1814 1803 zp.zp_compress = ZIO_COMPRESS_OFF;
1815 1804 zp.zp_type = DMU_OT_NONE;
1816 1805 zp.zp_level = 0;
1817 1806 zp.zp_copies = gio->io_prop.zp_copies;
1818 1807 zp.zp_dedup = B_FALSE;
1819 1808 zp.zp_dedup_verify = B_FALSE;
1820 1809 zp.zp_nopwrite = B_FALSE;
1821 1810
1822 1811 zio_nowait(zio_write(zio, spa, txg, &gbh->zg_blkptr[g],
1823 1812 (char *)pio->io_data + (pio->io_size - resid), lsize, &zp,
1824 - zio_write_gang_member_ready, NULL, &gn->gn_child[g],
1813 + zio_write_gang_member_ready, NULL, NULL, &gn->gn_child[g],
1825 1814 pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
1826 1815 &pio->io_bookmark));
1827 1816 }
1828 1817
1829 1818 /*
1830 1819 * Set pio's pipeline to just wait for zio to finish.
1831 1820 */
1832 1821 pio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1833 1822
1834 1823 zio_nowait(zio);
1835 1824
1836 1825 return (ZIO_PIPELINE_CONTINUE);
1837 1826 }
1838 1827
1839 1828 /*
1840 1829 * The zio_nop_write stage in the pipeline determines if allocating
1841 1830 * a new bp is necessary. By leveraging a cryptographically secure checksum,
1842 1831 * such as SHA256, we can compare the checksums of the new data and the old
1843 1832 * to determine if allocating a new block is required. The nopwrite
1844 1833 * feature can handle writes in either syncing or open context (i.e. zil
1845 1834 * writes) and as a result is mutually exclusive with dedup.
1846 1835 */
1847 1836 static int
1848 1837 zio_nop_write(zio_t *zio)
1849 1838 {
1850 1839 blkptr_t *bp = zio->io_bp;
1851 1840 blkptr_t *bp_orig = &zio->io_bp_orig;
1852 1841 zio_prop_t *zp = &zio->io_prop;
1853 1842
1854 1843 ASSERT(BP_GET_LEVEL(bp) == 0);
1855 1844 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
1856 1845 ASSERT(zp->zp_nopwrite);
1857 1846 ASSERT(!zp->zp_dedup);
1858 1847 ASSERT(zio->io_bp_override == NULL);
1859 1848 ASSERT(IO_IS_ALLOCATING(zio));
1860 1849
1861 1850 /*
1862 1851 * Check to see if the original bp and the new bp have matching
1863 1852 * characteristics (i.e. same checksum, compression algorithms, etc).
1864 1853 * If they don't then just continue with the pipeline which will
1865 1854 * allocate a new bp.
1866 1855 */
1867 1856 if (BP_IS_HOLE(bp_orig) ||
1868 1857 !zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_dedup ||
1869 1858 BP_GET_CHECKSUM(bp) != BP_GET_CHECKSUM(bp_orig) ||
1870 1859 BP_GET_COMPRESS(bp) != BP_GET_COMPRESS(bp_orig) ||
1871 1860 BP_GET_DEDUP(bp) != BP_GET_DEDUP(bp_orig) ||
1872 1861 zp->zp_copies != BP_GET_NDVAS(bp_orig))
1873 1862 return (ZIO_PIPELINE_CONTINUE);
1874 1863
1875 1864 /*
1876 1865 * If the checksums match then reset the pipeline so that we
1877 1866 * avoid allocating a new bp and issuing any I/O.
1878 1867 */
1879 1868 if (ZIO_CHECKSUM_EQUAL(bp->blk_cksum, bp_orig->blk_cksum)) {
1880 1869 ASSERT(zio_checksum_table[zp->zp_checksum].ci_dedup);
1881 1870 ASSERT3U(BP_GET_PSIZE(bp), ==, BP_GET_PSIZE(bp_orig));
1882 1871 ASSERT3U(BP_GET_LSIZE(bp), ==, BP_GET_LSIZE(bp_orig));
1883 1872 ASSERT(zp->zp_compress != ZIO_COMPRESS_OFF);
1884 1873 ASSERT(bcmp(&bp->blk_prop, &bp_orig->blk_prop,
1885 1874 sizeof (uint64_t)) == 0);
1886 1875
1887 1876 *bp = *bp_orig;
1888 1877 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1889 1878 zio->io_flags |= ZIO_FLAG_NOPWRITE;
1890 1879 }
1891 1880
1892 1881 return (ZIO_PIPELINE_CONTINUE);
1893 1882 }
1894 1883
1895 1884 /*
1896 1885 * ==========================================================================
1897 1886 * Dedup
1898 1887 * ==========================================================================
1899 1888 */
1900 1889 static void
1901 1890 zio_ddt_child_read_done(zio_t *zio)
1902 1891 {
1903 1892 blkptr_t *bp = zio->io_bp;
1904 1893 ddt_entry_t *dde = zio->io_private;
1905 1894 ddt_phys_t *ddp;
1906 1895 zio_t *pio = zio_unique_parent(zio);
1907 1896
1908 1897 mutex_enter(&pio->io_lock);
1909 1898 ddp = ddt_phys_select(dde, bp);
1910 1899 if (zio->io_error == 0)
1911 1900 ddt_phys_clear(ddp); /* this ddp doesn't need repair */
1912 1901 if (zio->io_error == 0 && dde->dde_repair_data == NULL)
1913 1902 dde->dde_repair_data = zio->io_data;
1914 1903 else
1915 1904 zio_buf_free(zio->io_data, zio->io_size);
1916 1905 mutex_exit(&pio->io_lock);
1917 1906 }
1918 1907
1919 1908 static int
1920 1909 zio_ddt_read_start(zio_t *zio)
1921 1910 {
1922 1911 blkptr_t *bp = zio->io_bp;
1923 1912
1924 1913 ASSERT(BP_GET_DEDUP(bp));
1925 1914 ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
1926 1915 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1927 1916
1928 1917 if (zio->io_child_error[ZIO_CHILD_DDT]) {
1929 1918 ddt_t *ddt = ddt_select(zio->io_spa, bp);
1930 1919 ddt_entry_t *dde = ddt_repair_start(ddt, bp);
1931 1920 ddt_phys_t *ddp = dde->dde_phys;
1932 1921 ddt_phys_t *ddp_self = ddt_phys_select(dde, bp);
1933 1922 blkptr_t blk;
1934 1923
1935 1924 ASSERT(zio->io_vsd == NULL);
1936 1925 zio->io_vsd = dde;
1937 1926
1938 1927 if (ddp_self == NULL)
1939 1928 return (ZIO_PIPELINE_CONTINUE);
1940 1929
1941 1930 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
1942 1931 if (ddp->ddp_phys_birth == 0 || ddp == ddp_self)
1943 1932 continue;
1944 1933 ddt_bp_create(ddt->ddt_checksum, &dde->dde_key, ddp,
1945 1934 &blk);
1946 1935 zio_nowait(zio_read(zio, zio->io_spa, &blk,
1947 1936 zio_buf_alloc(zio->io_size), zio->io_size,
1948 1937 zio_ddt_child_read_done, dde, zio->io_priority,
1949 1938 ZIO_DDT_CHILD_FLAGS(zio) | ZIO_FLAG_DONT_PROPAGATE,
1950 1939 &zio->io_bookmark));
1951 1940 }
1952 1941 return (ZIO_PIPELINE_CONTINUE);
1953 1942 }
1954 1943
1955 1944 zio_nowait(zio_read(zio, zio->io_spa, bp,
1956 1945 zio->io_data, zio->io_size, NULL, NULL, zio->io_priority,
1957 1946 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark));
1958 1947
1959 1948 return (ZIO_PIPELINE_CONTINUE);
1960 1949 }
1961 1950
1962 1951 static int
1963 1952 zio_ddt_read_done(zio_t *zio)
1964 1953 {
1965 1954 blkptr_t *bp = zio->io_bp;
1966 1955
1967 1956 if (zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_DONE))
1968 1957 return (ZIO_PIPELINE_STOP);
1969 1958
1970 1959 ASSERT(BP_GET_DEDUP(bp));
1971 1960 ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
1972 1961 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1973 1962
1974 1963 if (zio->io_child_error[ZIO_CHILD_DDT]) {
1975 1964 ddt_t *ddt = ddt_select(zio->io_spa, bp);
1976 1965 ddt_entry_t *dde = zio->io_vsd;
1977 1966 if (ddt == NULL) {
1978 1967 ASSERT(spa_load_state(zio->io_spa) != SPA_LOAD_NONE);
1979 1968 return (ZIO_PIPELINE_CONTINUE);
1980 1969 }
1981 1970 if (dde == NULL) {
1982 1971 zio->io_stage = ZIO_STAGE_DDT_READ_START >> 1;
1983 1972 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
1984 1973 return (ZIO_PIPELINE_STOP);
1985 1974 }
1986 1975 if (dde->dde_repair_data != NULL) {
1987 1976 bcopy(dde->dde_repair_data, zio->io_data, zio->io_size);
1988 1977 zio->io_child_error[ZIO_CHILD_DDT] = 0;
1989 1978 }
1990 1979 ddt_repair_done(ddt, dde);
1991 1980 zio->io_vsd = NULL;
1992 1981 }
1993 1982
1994 1983 ASSERT(zio->io_vsd == NULL);
1995 1984
1996 1985 return (ZIO_PIPELINE_CONTINUE);
1997 1986 }
1998 1987
1999 1988 static boolean_t
2000 1989 zio_ddt_collision(zio_t *zio, ddt_t *ddt, ddt_entry_t *dde)
2001 1990 {
2002 1991 spa_t *spa = zio->io_spa;
2003 1992
2004 1993 /*
2005 1994 * Note: we compare the original data, not the transformed data,
2006 1995 * because when zio->io_bp is an override bp, we will not have
2007 1996 * pushed the I/O transforms. That's an important optimization
2008 1997 * because otherwise we'd compress/encrypt all dmu_sync() data twice.
2009 1998 */
2010 1999 for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
2011 2000 zio_t *lio = dde->dde_lead_zio[p];
2012 2001
2013 2002 if (lio != NULL) {
2014 2003 return (lio->io_orig_size != zio->io_orig_size ||
2015 2004 bcmp(zio->io_orig_data, lio->io_orig_data,
2016 2005 zio->io_orig_size) != 0);
2017 2006 }
2018 2007 }
2019 2008
2020 2009 for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
2021 2010 ddt_phys_t *ddp = &dde->dde_phys[p];
2022 2011
2023 2012 if (ddp->ddp_phys_birth != 0) {
2024 2013 arc_buf_t *abuf = NULL;
2025 2014 uint32_t aflags = ARC_WAIT;
2026 2015 blkptr_t blk = *zio->io_bp;
2027 2016 int error;
2028 2017
2029 2018 ddt_bp_fill(ddp, &blk, ddp->ddp_phys_birth);
2030 2019
2031 2020 ddt_exit(ddt);
2032 2021
2033 2022 error = arc_read(NULL, spa, &blk,
2034 2023 arc_getbuf_func, &abuf, ZIO_PRIORITY_SYNC_READ,
2035 2024 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2036 2025 &aflags, &zio->io_bookmark);
2037 2026
2038 2027 if (error == 0) {
2039 2028 if (arc_buf_size(abuf) != zio->io_orig_size ||
2040 2029 bcmp(abuf->b_data, zio->io_orig_data,
2041 2030 zio->io_orig_size) != 0)
2042 2031 error = SET_ERROR(EEXIST);
2043 2032 VERIFY(arc_buf_remove_ref(abuf, &abuf));
2044 2033 }
2045 2034
2046 2035 ddt_enter(ddt);
2047 2036 return (error != 0);
2048 2037 }
2049 2038 }
2050 2039
2051 2040 return (B_FALSE);
2052 2041 }
2053 2042
2054 2043 static void
2055 2044 zio_ddt_child_write_ready(zio_t *zio)
2056 2045 {
2057 2046 int p = zio->io_prop.zp_copies;
2058 2047 ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
2059 2048 ddt_entry_t *dde = zio->io_private;
2060 2049 ddt_phys_t *ddp = &dde->dde_phys[p];
2061 2050 zio_t *pio;
2062 2051
2063 2052 if (zio->io_error)
2064 2053 return;
2065 2054
2066 2055 ddt_enter(ddt);
2067 2056
2068 2057 ASSERT(dde->dde_lead_zio[p] == zio);
2069 2058
2070 2059 ddt_phys_fill(ddp, zio->io_bp);
2071 2060
2072 2061 while ((pio = zio_walk_parents(zio)) != NULL)
2073 2062 ddt_bp_fill(ddp, pio->io_bp, zio->io_txg);
2074 2063
2075 2064 ddt_exit(ddt);
2076 2065 }
2077 2066
2078 2067 static void
2079 2068 zio_ddt_child_write_done(zio_t *zio)
2080 2069 {
2081 2070 int p = zio->io_prop.zp_copies;
2082 2071 ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
2083 2072 ddt_entry_t *dde = zio->io_private;
2084 2073 ddt_phys_t *ddp = &dde->dde_phys[p];
2085 2074
2086 2075 ddt_enter(ddt);
2087 2076
2088 2077 ASSERT(ddp->ddp_refcnt == 0);
2089 2078 ASSERT(dde->dde_lead_zio[p] == zio);
2090 2079 dde->dde_lead_zio[p] = NULL;
2091 2080
2092 2081 if (zio->io_error == 0) {
2093 2082 while (zio_walk_parents(zio) != NULL)
2094 2083 ddt_phys_addref(ddp);
2095 2084 } else {
2096 2085 ddt_phys_clear(ddp);
2097 2086 }
2098 2087
2099 2088 ddt_exit(ddt);
2100 2089 }
2101 2090
2102 2091 static void
2103 2092 zio_ddt_ditto_write_done(zio_t *zio)
2104 2093 {
2105 2094 int p = DDT_PHYS_DITTO;
2106 2095 zio_prop_t *zp = &zio->io_prop;
2107 2096 blkptr_t *bp = zio->io_bp;
2108 2097 ddt_t *ddt = ddt_select(zio->io_spa, bp);
2109 2098 ddt_entry_t *dde = zio->io_private;
2110 2099 ddt_phys_t *ddp = &dde->dde_phys[p];
2111 2100 ddt_key_t *ddk = &dde->dde_key;
2112 2101
2113 2102 ddt_enter(ddt);
2114 2103
2115 2104 ASSERT(ddp->ddp_refcnt == 0);
2116 2105 ASSERT(dde->dde_lead_zio[p] == zio);
2117 2106 dde->dde_lead_zio[p] = NULL;
2118 2107
2119 2108 if (zio->io_error == 0) {
2120 2109 ASSERT(ZIO_CHECKSUM_EQUAL(bp->blk_cksum, ddk->ddk_cksum));
2121 2110 ASSERT(zp->zp_copies < SPA_DVAS_PER_BP);
2122 2111 ASSERT(zp->zp_copies == BP_GET_NDVAS(bp) - BP_IS_GANG(bp));
2123 2112 if (ddp->ddp_phys_birth != 0)
2124 2113 ddt_phys_free(ddt, ddk, ddp, zio->io_txg);
2125 2114 ddt_phys_fill(ddp, bp);
2126 2115 }
2127 2116
2128 2117 ddt_exit(ddt);
2129 2118 }
2130 2119
2131 2120 static int
2132 2121 zio_ddt_write(zio_t *zio)
2133 2122 {
2134 2123 spa_t *spa = zio->io_spa;
2135 2124 blkptr_t *bp = zio->io_bp;
2136 2125 uint64_t txg = zio->io_txg;
2137 2126 zio_prop_t *zp = &zio->io_prop;
2138 2127 int p = zp->zp_copies;
2139 2128 int ditto_copies;
2140 2129 zio_t *cio = NULL;
2141 2130 zio_t *dio = NULL;
2142 2131 ddt_t *ddt = ddt_select(spa, bp);
2143 2132 ddt_entry_t *dde;
2144 2133 ddt_phys_t *ddp;
2145 2134
2146 2135 ASSERT(BP_GET_DEDUP(bp));
2147 2136 ASSERT(BP_GET_CHECKSUM(bp) == zp->zp_checksum);
2148 2137 ASSERT(BP_IS_HOLE(bp) || zio->io_bp_override);
2149 2138
2150 2139 ddt_enter(ddt);
2151 2140 dde = ddt_lookup(ddt, bp, B_TRUE);
2152 2141 ddp = &dde->dde_phys[p];
2153 2142
2154 2143 if (zp->zp_dedup_verify && zio_ddt_collision(zio, ddt, dde)) {
2155 2144 /*
2156 2145 * If we're using a weak checksum, upgrade to a strong checksum
2157 2146 * and try again. If we're already using a strong checksum,
2158 2147 * we can't resolve it, so just convert to an ordinary write.
2159 2148 * (And automatically e-mail a paper to Nature?)
2160 2149 */
2161 2150 if (!zio_checksum_table[zp->zp_checksum].ci_dedup) {
2162 2151 zp->zp_checksum = spa_dedup_checksum(spa);
2163 2152 zio_pop_transforms(zio);
2164 2153 zio->io_stage = ZIO_STAGE_OPEN;
2165 2154 BP_ZERO(bp);
2166 2155 } else {
2167 2156 zp->zp_dedup = B_FALSE;
2168 2157 }
2169 2158 zio->io_pipeline = ZIO_WRITE_PIPELINE;
2170 2159 ddt_exit(ddt);
2171 2160 return (ZIO_PIPELINE_CONTINUE);
2172 2161 }
2173 2162
2174 2163 ditto_copies = ddt_ditto_copies_needed(ddt, dde, ddp);
2175 2164 ASSERT(ditto_copies < SPA_DVAS_PER_BP);
2176 2165
2177 2166 if (ditto_copies > ddt_ditto_copies_present(dde) &&
2178 2167 dde->dde_lead_zio[DDT_PHYS_DITTO] == NULL) {
2179 2168 zio_prop_t czp = *zp;
2180 2169
2181 2170 czp.zp_copies = ditto_copies;
2182 2171
2183 2172 /*
2184 2173 * If we arrived here with an override bp, we won't have run
2185 2174 * the transform stack, so we won't have the data we need to
2186 2175 * generate a child i/o. So, toss the override bp and restart.
2187 2176 * This is safe, because using the override bp is just an
2188 2177 * optimization; and it's rare, so the cost doesn't matter.
2189 2178 */
2190 2179 if (zio->io_bp_override) {
↓ open down ↓ |
356 lines elided |
↑ open up ↑ |
2191 2180 zio_pop_transforms(zio);
2192 2181 zio->io_stage = ZIO_STAGE_OPEN;
2193 2182 zio->io_pipeline = ZIO_WRITE_PIPELINE;
2194 2183 zio->io_bp_override = NULL;
2195 2184 BP_ZERO(bp);
2196 2185 ddt_exit(ddt);
2197 2186 return (ZIO_PIPELINE_CONTINUE);
2198 2187 }
2199 2188
2200 2189 dio = zio_write(zio, spa, txg, bp, zio->io_orig_data,
2201 - zio->io_orig_size, &czp, NULL,
2190 + zio->io_orig_size, &czp, NULL, NULL,
2202 2191 zio_ddt_ditto_write_done, dde, zio->io_priority,
2203 2192 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
2204 2193
2205 2194 zio_push_transform(dio, zio->io_data, zio->io_size, 0, NULL);
2206 2195 dde->dde_lead_zio[DDT_PHYS_DITTO] = dio;
2207 2196 }
2208 2197
2209 2198 if (ddp->ddp_phys_birth != 0 || dde->dde_lead_zio[p] != NULL) {
2210 2199 if (ddp->ddp_phys_birth != 0)
2211 2200 ddt_bp_fill(ddp, bp, txg);
2212 2201 if (dde->dde_lead_zio[p] != NULL)
↓ open down ↓ |
1 lines elided |
↑ open up ↑ |
2213 2202 zio_add_child(zio, dde->dde_lead_zio[p]);
2214 2203 else
2215 2204 ddt_phys_addref(ddp);
2216 2205 } else if (zio->io_bp_override) {
2217 2206 ASSERT(bp->blk_birth == txg);
2218 2207 ASSERT(BP_EQUAL(bp, zio->io_bp_override));
2219 2208 ddt_phys_fill(ddp, bp);
2220 2209 ddt_phys_addref(ddp);
2221 2210 } else {
2222 2211 cio = zio_write(zio, spa, txg, bp, zio->io_orig_data,
2223 - zio->io_orig_size, zp, zio_ddt_child_write_ready,
2212 + zio->io_orig_size, zp, zio_ddt_child_write_ready, NULL,
2224 2213 zio_ddt_child_write_done, dde, zio->io_priority,
2225 2214 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
2226 2215
2227 2216 zio_push_transform(cio, zio->io_data, zio->io_size, 0, NULL);
2228 2217 dde->dde_lead_zio[p] = cio;
2229 2218 }
2230 2219
2231 2220 ddt_exit(ddt);
2232 2221
2233 2222 if (cio)
2234 2223 zio_nowait(cio);
2235 2224 if (dio)
2236 2225 zio_nowait(dio);
2237 2226
2238 2227 return (ZIO_PIPELINE_CONTINUE);
2239 2228 }
2240 2229
2241 2230 ddt_entry_t *freedde; /* for debugging */
2242 2231
2243 2232 static int
2244 2233 zio_ddt_free(zio_t *zio)
2245 2234 {
2246 2235 spa_t *spa = zio->io_spa;
2247 2236 blkptr_t *bp = zio->io_bp;
2248 2237 ddt_t *ddt = ddt_select(spa, bp);
2249 2238 ddt_entry_t *dde;
2250 2239 ddt_phys_t *ddp;
2251 2240
2252 2241 ASSERT(BP_GET_DEDUP(bp));
2253 2242 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2254 2243
2255 2244 ddt_enter(ddt);
2256 2245 freedde = dde = ddt_lookup(ddt, bp, B_TRUE);
2257 2246 ddp = ddt_phys_select(dde, bp);
2258 2247 ddt_phys_decref(ddp);
2259 2248 ddt_exit(ddt);
2260 2249
2261 2250 return (ZIO_PIPELINE_CONTINUE);
2262 2251 }
2263 2252
2264 2253 /*
2265 2254 * ==========================================================================
2266 2255 * Allocate and free blocks
2267 2256 * ==========================================================================
2268 2257 */
2269 2258 static int
2270 2259 zio_dva_allocate(zio_t *zio)
2271 2260 {
2272 2261 spa_t *spa = zio->io_spa;
2273 2262 metaslab_class_t *mc = spa_normal_class(spa);
2274 2263 blkptr_t *bp = zio->io_bp;
2275 2264 int error;
2276 2265 int flags = 0;
2277 2266
2278 2267 if (zio->io_gang_leader == NULL) {
2279 2268 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2280 2269 zio->io_gang_leader = zio;
2281 2270 }
2282 2271
2283 2272 ASSERT(BP_IS_HOLE(bp));
2284 2273 ASSERT0(BP_GET_NDVAS(bp));
2285 2274 ASSERT3U(zio->io_prop.zp_copies, >, 0);
2286 2275 ASSERT3U(zio->io_prop.zp_copies, <=, spa_max_replication(spa));
2287 2276 ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
2288 2277
2289 2278 /*
2290 2279 * The dump device does not support gang blocks so allocation on
2291 2280 * behalf of the dump device (i.e. ZIO_FLAG_NODATA) must avoid
2292 2281 * the "fast" gang feature.
2293 2282 */
2294 2283 flags |= (zio->io_flags & ZIO_FLAG_NODATA) ? METASLAB_GANG_AVOID : 0;
2295 2284 flags |= (zio->io_flags & ZIO_FLAG_GANG_CHILD) ?
2296 2285 METASLAB_GANG_CHILD : 0;
2297 2286 error = metaslab_alloc(spa, mc, zio->io_size, bp,
2298 2287 zio->io_prop.zp_copies, zio->io_txg, NULL, flags);
2299 2288
2300 2289 if (error) {
2301 2290 spa_dbgmsg(spa, "%s: metaslab allocation failure: zio %p, "
2302 2291 "size %llu, error %d", spa_name(spa), zio, zio->io_size,
2303 2292 error);
2304 2293 if (error == ENOSPC && zio->io_size > SPA_MINBLOCKSIZE)
2305 2294 return (zio_write_gang_block(zio));
2306 2295 zio->io_error = error;
2307 2296 }
2308 2297
2309 2298 return (ZIO_PIPELINE_CONTINUE);
2310 2299 }
2311 2300
2312 2301 static int
2313 2302 zio_dva_free(zio_t *zio)
2314 2303 {
2315 2304 metaslab_free(zio->io_spa, zio->io_bp, zio->io_txg, B_FALSE);
2316 2305
2317 2306 return (ZIO_PIPELINE_CONTINUE);
2318 2307 }
2319 2308
2320 2309 static int
2321 2310 zio_dva_claim(zio_t *zio)
2322 2311 {
2323 2312 int error;
2324 2313
2325 2314 error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg);
2326 2315 if (error)
2327 2316 zio->io_error = error;
2328 2317
2329 2318 return (ZIO_PIPELINE_CONTINUE);
2330 2319 }
2331 2320
2332 2321 /*
2333 2322 * Undo an allocation. This is used by zio_done() when an I/O fails
2334 2323 * and we want to give back the block we just allocated.
2335 2324 * This handles both normal blocks and gang blocks.
2336 2325 */
2337 2326 static void
2338 2327 zio_dva_unallocate(zio_t *zio, zio_gang_node_t *gn, blkptr_t *bp)
2339 2328 {
2340 2329 ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp));
2341 2330 ASSERT(zio->io_bp_override == NULL);
2342 2331
2343 2332 if (!BP_IS_HOLE(bp))
2344 2333 metaslab_free(zio->io_spa, bp, bp->blk_birth, B_TRUE);
2345 2334
2346 2335 if (gn != NULL) {
2347 2336 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
2348 2337 zio_dva_unallocate(zio, gn->gn_child[g],
2349 2338 &gn->gn_gbh->zg_blkptr[g]);
2350 2339 }
2351 2340 }
2352 2341 }
2353 2342
2354 2343 /*
2355 2344 * Try to allocate an intent log block. Return 0 on success, errno on failure.
2356 2345 */
2357 2346 int
2358 2347 zio_alloc_zil(spa_t *spa, uint64_t txg, blkptr_t *new_bp, blkptr_t *old_bp,
2359 2348 uint64_t size, boolean_t use_slog)
2360 2349 {
2361 2350 int error = 1;
2362 2351
2363 2352 ASSERT(txg > spa_syncing_txg(spa));
2364 2353
2365 2354 /*
2366 2355 * ZIL blocks are always contiguous (i.e. not gang blocks) so we
2367 2356 * set the METASLAB_GANG_AVOID flag so that they don't "fast gang"
2368 2357 * when allocating them.
2369 2358 */
2370 2359 if (use_slog) {
2371 2360 error = metaslab_alloc(spa, spa_log_class(spa), size,
2372 2361 new_bp, 1, txg, old_bp,
2373 2362 METASLAB_HINTBP_AVOID | METASLAB_GANG_AVOID);
2374 2363 }
2375 2364
2376 2365 if (error) {
2377 2366 error = metaslab_alloc(spa, spa_normal_class(spa), size,
2378 2367 new_bp, 1, txg, old_bp,
2379 2368 METASLAB_HINTBP_AVOID | METASLAB_GANG_AVOID);
2380 2369 }
2381 2370
2382 2371 if (error == 0) {
2383 2372 BP_SET_LSIZE(new_bp, size);
2384 2373 BP_SET_PSIZE(new_bp, size);
2385 2374 BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF);
2386 2375 BP_SET_CHECKSUM(new_bp,
2387 2376 spa_version(spa) >= SPA_VERSION_SLIM_ZIL
2388 2377 ? ZIO_CHECKSUM_ZILOG2 : ZIO_CHECKSUM_ZILOG);
2389 2378 BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
2390 2379 BP_SET_LEVEL(new_bp, 0);
2391 2380 BP_SET_DEDUP(new_bp, 0);
2392 2381 BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER);
2393 2382 }
2394 2383
2395 2384 return (error);
2396 2385 }
2397 2386
2398 2387 /*
2399 2388 * Free an intent log block.
2400 2389 */
2401 2390 void
2402 2391 zio_free_zil(spa_t *spa, uint64_t txg, blkptr_t *bp)
2403 2392 {
2404 2393 ASSERT(BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG);
2405 2394 ASSERT(!BP_IS_GANG(bp));
2406 2395
2407 2396 zio_free(spa, txg, bp);
2408 2397 }
2409 2398
2410 2399 /*
2411 2400 * ==========================================================================
2412 2401 * Read and write to physical devices
2413 2402 * ==========================================================================
2414 2403 */
2415 2404 static int
2416 2405 zio_vdev_io_start(zio_t *zio)
2417 2406 {
2418 2407 vdev_t *vd = zio->io_vd;
2419 2408 uint64_t align;
2420 2409 spa_t *spa = zio->io_spa;
2421 2410
2422 2411 ASSERT(zio->io_error == 0);
2423 2412 ASSERT(zio->io_child_error[ZIO_CHILD_VDEV] == 0);
2424 2413
2425 2414 if (vd == NULL) {
2426 2415 if (!(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
2427 2416 spa_config_enter(spa, SCL_ZIO, zio, RW_READER);
2428 2417
2429 2418 /*
2430 2419 * The mirror_ops handle multiple DVAs in a single BP.
2431 2420 */
2432 2421 return (vdev_mirror_ops.vdev_op_io_start(zio));
2433 2422 }
2434 2423
2435 2424 /*
2436 2425 * We keep track of time-sensitive I/Os so that the scan thread
2437 2426 * can quickly react to certain workloads. In particular, we care
2438 2427 * about non-scrubbing, top-level reads and writes with the following
2439 2428 * characteristics:
2440 2429 * - synchronous writes of user data to non-slog devices
2441 2430 * - any reads of user data
2442 2431 * When these conditions are met, adjust the timestamp of spa_last_io
2443 2432 * which allows the scan thread to adjust its workload accordingly.
2444 2433 */
2445 2434 if (!(zio->io_flags & ZIO_FLAG_SCAN_THREAD) && zio->io_bp != NULL &&
2446 2435 vd == vd->vdev_top && !vd->vdev_islog &&
2447 2436 zio->io_bookmark.zb_objset != DMU_META_OBJSET &&
2448 2437 zio->io_txg != spa_syncing_txg(spa)) {
2449 2438 uint64_t old = spa->spa_last_io;
2450 2439 uint64_t new = ddi_get_lbolt64();
2451 2440 if (old != new)
2452 2441 (void) atomic_cas_64(&spa->spa_last_io, old, new);
2453 2442 }
2454 2443
2455 2444 align = 1ULL << vd->vdev_top->vdev_ashift;
2456 2445
2457 2446 if (P2PHASE(zio->io_size, align) != 0) {
2458 2447 uint64_t asize = P2ROUNDUP(zio->io_size, align);
2459 2448 char *abuf = zio_buf_alloc(asize);
2460 2449 ASSERT(vd == vd->vdev_top);
2461 2450 if (zio->io_type == ZIO_TYPE_WRITE) {
2462 2451 bcopy(zio->io_data, abuf, zio->io_size);
2463 2452 bzero(abuf + zio->io_size, asize - zio->io_size);
2464 2453 }
2465 2454 zio_push_transform(zio, abuf, asize, asize, zio_subblock);
2466 2455 }
2467 2456
2468 2457 ASSERT(P2PHASE(zio->io_offset, align) == 0);
2469 2458 ASSERT(P2PHASE(zio->io_size, align) == 0);
2470 2459 VERIFY(zio->io_type != ZIO_TYPE_WRITE || spa_writeable(spa));
2471 2460
2472 2461 /*
2473 2462 * If this is a repair I/O, and there's no self-healing involved --
2474 2463 * that is, we're just resilvering what we expect to resilver --
2475 2464 * then don't do the I/O unless zio's txg is actually in vd's DTL.
2476 2465 * This prevents spurious resilvering with nested replication.
2477 2466 * For example, given a mirror of mirrors, (A+B)+(C+D), if only
2478 2467 * A is out of date, we'll read from C+D, then use the data to
2479 2468 * resilver A+B -- but we don't actually want to resilver B, just A.
2480 2469 * The top-level mirror has no way to know this, so instead we just
2481 2470 * discard unnecessary repairs as we work our way down the vdev tree.
2482 2471 * The same logic applies to any form of nested replication:
2483 2472 * ditto + mirror, RAID-Z + replacing, etc. This covers them all.
2484 2473 */
2485 2474 if ((zio->io_flags & ZIO_FLAG_IO_REPAIR) &&
2486 2475 !(zio->io_flags & ZIO_FLAG_SELF_HEAL) &&
2487 2476 zio->io_txg != 0 && /* not a delegated i/o */
2488 2477 !vdev_dtl_contains(vd, DTL_PARTIAL, zio->io_txg, 1)) {
2489 2478 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
2490 2479 zio_vdev_io_bypass(zio);
2491 2480 return (ZIO_PIPELINE_CONTINUE);
2492 2481 }
2493 2482
2494 2483 if (vd->vdev_ops->vdev_op_leaf &&
2495 2484 (zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE)) {
2496 2485
2497 2486 if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio) == 0)
2498 2487 return (ZIO_PIPELINE_CONTINUE);
2499 2488
2500 2489 if ((zio = vdev_queue_io(zio)) == NULL)
2501 2490 return (ZIO_PIPELINE_STOP);
2502 2491
2503 2492 if (!vdev_accessible(vd, zio)) {
2504 2493 zio->io_error = SET_ERROR(ENXIO);
2505 2494 zio_interrupt(zio);
2506 2495 return (ZIO_PIPELINE_STOP);
2507 2496 }
2508 2497 }
2509 2498
2510 2499 return (vd->vdev_ops->vdev_op_io_start(zio));
2511 2500 }
2512 2501
2513 2502 static int
2514 2503 zio_vdev_io_done(zio_t *zio)
2515 2504 {
2516 2505 vdev_t *vd = zio->io_vd;
2517 2506 vdev_ops_t *ops = vd ? vd->vdev_ops : &vdev_mirror_ops;
2518 2507 boolean_t unexpected_error = B_FALSE;
2519 2508
2520 2509 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE))
2521 2510 return (ZIO_PIPELINE_STOP);
2522 2511
2523 2512 ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
2524 2513
2525 2514 if (vd != NULL && vd->vdev_ops->vdev_op_leaf) {
2526 2515
2527 2516 vdev_queue_io_done(zio);
2528 2517
2529 2518 if (zio->io_type == ZIO_TYPE_WRITE)
2530 2519 vdev_cache_write(zio);
2531 2520
2532 2521 if (zio_injection_enabled && zio->io_error == 0)
2533 2522 zio->io_error = zio_handle_device_injection(vd,
2534 2523 zio, EIO);
2535 2524
2536 2525 if (zio_injection_enabled && zio->io_error == 0)
2537 2526 zio->io_error = zio_handle_label_injection(zio, EIO);
2538 2527
2539 2528 if (zio->io_error) {
2540 2529 if (!vdev_accessible(vd, zio)) {
2541 2530 zio->io_error = SET_ERROR(ENXIO);
2542 2531 } else {
2543 2532 unexpected_error = B_TRUE;
2544 2533 }
2545 2534 }
2546 2535 }
2547 2536
2548 2537 ops->vdev_op_io_done(zio);
2549 2538
2550 2539 if (unexpected_error)
2551 2540 VERIFY(vdev_probe(vd, zio) == NULL);
2552 2541
2553 2542 return (ZIO_PIPELINE_CONTINUE);
2554 2543 }
2555 2544
2556 2545 /*
2557 2546 * For non-raidz ZIOs, we can just copy aside the bad data read from the
2558 2547 * disk, and use that to finish the checksum ereport later.
2559 2548 */
2560 2549 static void
2561 2550 zio_vsd_default_cksum_finish(zio_cksum_report_t *zcr,
2562 2551 const void *good_buf)
2563 2552 {
2564 2553 /* no processing needed */
2565 2554 zfs_ereport_finish_checksum(zcr, good_buf, zcr->zcr_cbdata, B_FALSE);
2566 2555 }
2567 2556
2568 2557 /*ARGSUSED*/
2569 2558 void
2570 2559 zio_vsd_default_cksum_report(zio_t *zio, zio_cksum_report_t *zcr, void *ignored)
2571 2560 {
2572 2561 void *buf = zio_buf_alloc(zio->io_size);
2573 2562
2574 2563 bcopy(zio->io_data, buf, zio->io_size);
2575 2564
2576 2565 zcr->zcr_cbinfo = zio->io_size;
2577 2566 zcr->zcr_cbdata = buf;
2578 2567 zcr->zcr_finish = zio_vsd_default_cksum_finish;
2579 2568 zcr->zcr_free = zio_buf_free;
2580 2569 }
2581 2570
2582 2571 static int
2583 2572 zio_vdev_io_assess(zio_t *zio)
2584 2573 {
2585 2574 vdev_t *vd = zio->io_vd;
2586 2575
2587 2576 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE))
2588 2577 return (ZIO_PIPELINE_STOP);
2589 2578
2590 2579 if (vd == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
2591 2580 spa_config_exit(zio->io_spa, SCL_ZIO, zio);
2592 2581
2593 2582 if (zio->io_vsd != NULL) {
2594 2583 zio->io_vsd_ops->vsd_free(zio);
2595 2584 zio->io_vsd = NULL;
2596 2585 }
2597 2586
2598 2587 if (zio_injection_enabled && zio->io_error == 0)
2599 2588 zio->io_error = zio_handle_fault_injection(zio, EIO);
2600 2589
2601 2590 /*
2602 2591 * If the I/O failed, determine whether we should attempt to retry it.
2603 2592 *
2604 2593 * On retry, we cut in line in the issue queue, since we don't want
2605 2594 * compression/checksumming/etc. work to prevent our (cheap) IO reissue.
2606 2595 */
2607 2596 if (zio->io_error && vd == NULL &&
2608 2597 !(zio->io_flags & (ZIO_FLAG_DONT_RETRY | ZIO_FLAG_IO_RETRY))) {
2609 2598 ASSERT(!(zio->io_flags & ZIO_FLAG_DONT_QUEUE)); /* not a leaf */
2610 2599 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_BYPASS)); /* not a leaf */
2611 2600 zio->io_error = 0;
2612 2601 zio->io_flags |= ZIO_FLAG_IO_RETRY |
2613 2602 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE;
2614 2603 zio->io_stage = ZIO_STAGE_VDEV_IO_START >> 1;
2615 2604 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE,
2616 2605 zio_requeue_io_start_cut_in_line);
2617 2606 return (ZIO_PIPELINE_STOP);
2618 2607 }
2619 2608
2620 2609 /*
2621 2610 * If we got an error on a leaf device, convert it to ENXIO
2622 2611 * if the device is not accessible at all.
2623 2612 */
2624 2613 if (zio->io_error && vd != NULL && vd->vdev_ops->vdev_op_leaf &&
2625 2614 !vdev_accessible(vd, zio))
2626 2615 zio->io_error = SET_ERROR(ENXIO);
2627 2616
2628 2617 /*
2629 2618 * If we can't write to an interior vdev (mirror or RAID-Z),
↓ open down ↓ |
396 lines elided |
↑ open up ↑ |
2630 2619 * set vdev_cant_write so that we stop trying to allocate from it.
2631 2620 */
2632 2621 if (zio->io_error == ENXIO && zio->io_type == ZIO_TYPE_WRITE &&
2633 2622 vd != NULL && !vd->vdev_ops->vdev_op_leaf) {
2634 2623 vd->vdev_cant_write = B_TRUE;
2635 2624 }
2636 2625
2637 2626 if (zio->io_error)
2638 2627 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2639 2628
2629 + if (vd != NULL && vd->vdev_ops->vdev_op_leaf &&
2630 + zio->io_physdone != NULL) {
2631 + ASSERT(!(zio->io_flags & ZIO_FLAG_DELEGATED));
2632 + ASSERT(zio->io_child_type == ZIO_CHILD_VDEV);
2633 + zio->io_physdone(zio->io_logical);
2634 + }
2635 +
2640 2636 return (ZIO_PIPELINE_CONTINUE);
2641 2637 }
2642 2638
2643 2639 void
2644 2640 zio_vdev_io_reissue(zio_t *zio)
2645 2641 {
2646 2642 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
2647 2643 ASSERT(zio->io_error == 0);
2648 2644
2649 2645 zio->io_stage >>= 1;
2650 2646 }
2651 2647
2652 2648 void
2653 2649 zio_vdev_io_redone(zio_t *zio)
2654 2650 {
2655 2651 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE);
2656 2652
2657 2653 zio->io_stage >>= 1;
2658 2654 }
2659 2655
2660 2656 void
2661 2657 zio_vdev_io_bypass(zio_t *zio)
2662 2658 {
2663 2659 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
2664 2660 ASSERT(zio->io_error == 0);
2665 2661
2666 2662 zio->io_flags |= ZIO_FLAG_IO_BYPASS;
2667 2663 zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS >> 1;
2668 2664 }
2669 2665
2670 2666 /*
2671 2667 * ==========================================================================
2672 2668 * Generate and verify checksums
2673 2669 * ==========================================================================
2674 2670 */
2675 2671 static int
2676 2672 zio_checksum_generate(zio_t *zio)
2677 2673 {
2678 2674 blkptr_t *bp = zio->io_bp;
2679 2675 enum zio_checksum checksum;
2680 2676
2681 2677 if (bp == NULL) {
2682 2678 /*
2683 2679 * This is zio_write_phys().
2684 2680 * We're either generating a label checksum, or none at all.
2685 2681 */
2686 2682 checksum = zio->io_prop.zp_checksum;
2687 2683
2688 2684 if (checksum == ZIO_CHECKSUM_OFF)
2689 2685 return (ZIO_PIPELINE_CONTINUE);
2690 2686
2691 2687 ASSERT(checksum == ZIO_CHECKSUM_LABEL);
2692 2688 } else {
2693 2689 if (BP_IS_GANG(bp) && zio->io_child_type == ZIO_CHILD_GANG) {
2694 2690 ASSERT(!IO_IS_ALLOCATING(zio));
2695 2691 checksum = ZIO_CHECKSUM_GANG_HEADER;
2696 2692 } else {
2697 2693 checksum = BP_GET_CHECKSUM(bp);
2698 2694 }
2699 2695 }
2700 2696
2701 2697 zio_checksum_compute(zio, checksum, zio->io_data, zio->io_size);
2702 2698
2703 2699 return (ZIO_PIPELINE_CONTINUE);
2704 2700 }
2705 2701
2706 2702 static int
2707 2703 zio_checksum_verify(zio_t *zio)
2708 2704 {
2709 2705 zio_bad_cksum_t info;
2710 2706 blkptr_t *bp = zio->io_bp;
2711 2707 int error;
2712 2708
2713 2709 ASSERT(zio->io_vd != NULL);
2714 2710
2715 2711 if (bp == NULL) {
2716 2712 /*
2717 2713 * This is zio_read_phys().
2718 2714 * We're either verifying a label checksum, or nothing at all.
2719 2715 */
2720 2716 if (zio->io_prop.zp_checksum == ZIO_CHECKSUM_OFF)
2721 2717 return (ZIO_PIPELINE_CONTINUE);
2722 2718
2723 2719 ASSERT(zio->io_prop.zp_checksum == ZIO_CHECKSUM_LABEL);
2724 2720 }
2725 2721
2726 2722 if ((error = zio_checksum_error(zio, &info)) != 0) {
2727 2723 zio->io_error = error;
2728 2724 if (!(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
2729 2725 zfs_ereport_start_checksum(zio->io_spa,
2730 2726 zio->io_vd, zio, zio->io_offset,
2731 2727 zio->io_size, NULL, &info);
2732 2728 }
2733 2729 }
2734 2730
2735 2731 return (ZIO_PIPELINE_CONTINUE);
2736 2732 }
2737 2733
2738 2734 /*
2739 2735 * Called by RAID-Z to ensure we don't compute the checksum twice.
2740 2736 */
2741 2737 void
2742 2738 zio_checksum_verified(zio_t *zio)
2743 2739 {
2744 2740 zio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
2745 2741 }
2746 2742
2747 2743 /*
2748 2744 * ==========================================================================
2749 2745 * Error rank. Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other.
2750 2746 * An error of 0 indictes success. ENXIO indicates whole-device failure,
2751 2747 * which may be transient (e.g. unplugged) or permament. ECKSUM and EIO
2752 2748 * indicate errors that are specific to one I/O, and most likely permanent.
2753 2749 * Any other error is presumed to be worse because we weren't expecting it.
2754 2750 * ==========================================================================
2755 2751 */
2756 2752 int
2757 2753 zio_worst_error(int e1, int e2)
2758 2754 {
2759 2755 static int zio_error_rank[] = { 0, ENXIO, ECKSUM, EIO };
2760 2756 int r1, r2;
2761 2757
2762 2758 for (r1 = 0; r1 < sizeof (zio_error_rank) / sizeof (int); r1++)
2763 2759 if (e1 == zio_error_rank[r1])
2764 2760 break;
2765 2761
2766 2762 for (r2 = 0; r2 < sizeof (zio_error_rank) / sizeof (int); r2++)
2767 2763 if (e2 == zio_error_rank[r2])
2768 2764 break;
2769 2765
2770 2766 return (r1 > r2 ? e1 : e2);
2771 2767 }
2772 2768
2773 2769 /*
2774 2770 * ==========================================================================
2775 2771 * I/O completion
2776 2772 * ==========================================================================
2777 2773 */
2778 2774 static int
2779 2775 zio_ready(zio_t *zio)
2780 2776 {
2781 2777 blkptr_t *bp = zio->io_bp;
2782 2778 zio_t *pio, *pio_next;
2783 2779
2784 2780 if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_READY) ||
2785 2781 zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_READY))
2786 2782 return (ZIO_PIPELINE_STOP);
2787 2783
2788 2784 if (zio->io_ready) {
2789 2785 ASSERT(IO_IS_ALLOCATING(zio));
2790 2786 ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp) ||
2791 2787 (zio->io_flags & ZIO_FLAG_NOPWRITE));
2792 2788 ASSERT(zio->io_children[ZIO_CHILD_GANG][ZIO_WAIT_READY] == 0);
2793 2789
2794 2790 zio->io_ready(zio);
2795 2791 }
2796 2792
2797 2793 if (bp != NULL && bp != &zio->io_bp_copy)
2798 2794 zio->io_bp_copy = *bp;
2799 2795
2800 2796 if (zio->io_error)
2801 2797 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2802 2798
2803 2799 mutex_enter(&zio->io_lock);
2804 2800 zio->io_state[ZIO_WAIT_READY] = 1;
2805 2801 pio = zio_walk_parents(zio);
2806 2802 mutex_exit(&zio->io_lock);
2807 2803
2808 2804 /*
2809 2805 * As we notify zio's parents, new parents could be added.
2810 2806 * New parents go to the head of zio's io_parent_list, however,
2811 2807 * so we will (correctly) not notify them. The remainder of zio's
2812 2808 * io_parent_list, from 'pio_next' onward, cannot change because
2813 2809 * all parents must wait for us to be done before they can be done.
2814 2810 */
2815 2811 for (; pio != NULL; pio = pio_next) {
2816 2812 pio_next = zio_walk_parents(zio);
2817 2813 zio_notify_parent(pio, zio, ZIO_WAIT_READY);
2818 2814 }
2819 2815
2820 2816 if (zio->io_flags & ZIO_FLAG_NODATA) {
2821 2817 if (BP_IS_GANG(bp)) {
2822 2818 zio->io_flags &= ~ZIO_FLAG_NODATA;
2823 2819 } else {
2824 2820 ASSERT((uintptr_t)zio->io_data < SPA_MAXBLOCKSIZE);
2825 2821 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
2826 2822 }
2827 2823 }
2828 2824
2829 2825 if (zio_injection_enabled &&
2830 2826 zio->io_spa->spa_syncing_txg == zio->io_txg)
2831 2827 zio_handle_ignored_writes(zio);
2832 2828
2833 2829 return (ZIO_PIPELINE_CONTINUE);
2834 2830 }
2835 2831
2836 2832 static int
2837 2833 zio_done(zio_t *zio)
2838 2834 {
2839 2835 spa_t *spa = zio->io_spa;
2840 2836 zio_t *lio = zio->io_logical;
2841 2837 blkptr_t *bp = zio->io_bp;
2842 2838 vdev_t *vd = zio->io_vd;
2843 2839 uint64_t psize = zio->io_size;
2844 2840 zio_t *pio, *pio_next;
2845 2841
2846 2842 /*
2847 2843 * If our children haven't all completed,
2848 2844 * wait for them and then repeat this pipeline stage.
2849 2845 */
2850 2846 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE) ||
2851 2847 zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE) ||
2852 2848 zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_DONE) ||
2853 2849 zio_wait_for_children(zio, ZIO_CHILD_LOGICAL, ZIO_WAIT_DONE))
2854 2850 return (ZIO_PIPELINE_STOP);
2855 2851
2856 2852 for (int c = 0; c < ZIO_CHILD_TYPES; c++)
2857 2853 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
2858 2854 ASSERT(zio->io_children[c][w] == 0);
2859 2855
2860 2856 if (bp != NULL) {
2861 2857 ASSERT(bp->blk_pad[0] == 0);
2862 2858 ASSERT(bp->blk_pad[1] == 0);
2863 2859 ASSERT(bcmp(bp, &zio->io_bp_copy, sizeof (blkptr_t)) == 0 ||
2864 2860 (bp == zio_unique_parent(zio)->io_bp));
2865 2861 if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(bp) &&
2866 2862 zio->io_bp_override == NULL &&
2867 2863 !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) {
2868 2864 ASSERT(!BP_SHOULD_BYTESWAP(bp));
2869 2865 ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(bp));
2870 2866 ASSERT(BP_COUNT_GANG(bp) == 0 ||
2871 2867 (BP_COUNT_GANG(bp) == BP_GET_NDVAS(bp)));
2872 2868 }
2873 2869 if (zio->io_flags & ZIO_FLAG_NOPWRITE)
2874 2870 VERIFY(BP_EQUAL(bp, &zio->io_bp_orig));
2875 2871 }
2876 2872
2877 2873 /*
2878 2874 * If there were child vdev/gang/ddt errors, they apply to us now.
2879 2875 */
2880 2876 zio_inherit_child_errors(zio, ZIO_CHILD_VDEV);
2881 2877 zio_inherit_child_errors(zio, ZIO_CHILD_GANG);
2882 2878 zio_inherit_child_errors(zio, ZIO_CHILD_DDT);
2883 2879
2884 2880 /*
2885 2881 * If the I/O on the transformed data was successful, generate any
2886 2882 * checksum reports now while we still have the transformed data.
2887 2883 */
2888 2884 if (zio->io_error == 0) {
2889 2885 while (zio->io_cksum_report != NULL) {
2890 2886 zio_cksum_report_t *zcr = zio->io_cksum_report;
2891 2887 uint64_t align = zcr->zcr_align;
2892 2888 uint64_t asize = P2ROUNDUP(psize, align);
2893 2889 char *abuf = zio->io_data;
2894 2890
2895 2891 if (asize != psize) {
2896 2892 abuf = zio_buf_alloc(asize);
2897 2893 bcopy(zio->io_data, abuf, psize);
2898 2894 bzero(abuf + psize, asize - psize);
2899 2895 }
2900 2896
2901 2897 zio->io_cksum_report = zcr->zcr_next;
2902 2898 zcr->zcr_next = NULL;
2903 2899 zcr->zcr_finish(zcr, abuf);
2904 2900 zfs_ereport_free_checksum(zcr);
2905 2901
2906 2902 if (asize != psize)
2907 2903 zio_buf_free(abuf, asize);
2908 2904 }
2909 2905 }
2910 2906
2911 2907 zio_pop_transforms(zio); /* note: may set zio->io_error */
2912 2908
2913 2909 vdev_stat_update(zio, psize);
2914 2910
2915 2911 if (zio->io_error) {
2916 2912 /*
2917 2913 * If this I/O is attached to a particular vdev,
2918 2914 * generate an error message describing the I/O failure
2919 2915 * at the block level. We ignore these errors if the
2920 2916 * device is currently unavailable.
2921 2917 */
2922 2918 if (zio->io_error != ECKSUM && vd != NULL && !vdev_is_dead(vd))
2923 2919 zfs_ereport_post(FM_EREPORT_ZFS_IO, spa, vd, zio, 0, 0);
2924 2920
2925 2921 if ((zio->io_error == EIO || !(zio->io_flags &
2926 2922 (ZIO_FLAG_SPECULATIVE | ZIO_FLAG_DONT_PROPAGATE))) &&
2927 2923 zio == lio) {
2928 2924 /*
2929 2925 * For logical I/O requests, tell the SPA to log the
2930 2926 * error and generate a logical data ereport.
2931 2927 */
2932 2928 spa_log_error(spa, zio);
2933 2929 zfs_ereport_post(FM_EREPORT_ZFS_DATA, spa, NULL, zio,
2934 2930 0, 0);
2935 2931 }
2936 2932 }
2937 2933
2938 2934 if (zio->io_error && zio == lio) {
2939 2935 /*
2940 2936 * Determine whether zio should be reexecuted. This will
2941 2937 * propagate all the way to the root via zio_notify_parent().
2942 2938 */
2943 2939 ASSERT(vd == NULL && bp != NULL);
2944 2940 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2945 2941
2946 2942 if (IO_IS_ALLOCATING(zio) &&
2947 2943 !(zio->io_flags & ZIO_FLAG_CANFAIL)) {
2948 2944 if (zio->io_error != ENOSPC)
2949 2945 zio->io_reexecute |= ZIO_REEXECUTE_NOW;
2950 2946 else
2951 2947 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
2952 2948 }
2953 2949
2954 2950 if ((zio->io_type == ZIO_TYPE_READ ||
2955 2951 zio->io_type == ZIO_TYPE_FREE) &&
2956 2952 !(zio->io_flags & ZIO_FLAG_SCAN_THREAD) &&
2957 2953 zio->io_error == ENXIO &&
2958 2954 spa_load_state(spa) == SPA_LOAD_NONE &&
2959 2955 spa_get_failmode(spa) != ZIO_FAILURE_MODE_CONTINUE)
2960 2956 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
2961 2957
2962 2958 if (!(zio->io_flags & ZIO_FLAG_CANFAIL) && !zio->io_reexecute)
2963 2959 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
2964 2960
2965 2961 /*
2966 2962 * Here is a possibly good place to attempt to do
2967 2963 * either combinatorial reconstruction or error correction
2968 2964 * based on checksums. It also might be a good place
2969 2965 * to send out preliminary ereports before we suspend
2970 2966 * processing.
2971 2967 */
2972 2968 }
2973 2969
2974 2970 /*
2975 2971 * If there were logical child errors, they apply to us now.
2976 2972 * We defer this until now to avoid conflating logical child
2977 2973 * errors with errors that happened to the zio itself when
2978 2974 * updating vdev stats and reporting FMA events above.
2979 2975 */
2980 2976 zio_inherit_child_errors(zio, ZIO_CHILD_LOGICAL);
2981 2977
2982 2978 if ((zio->io_error || zio->io_reexecute) &&
2983 2979 IO_IS_ALLOCATING(zio) && zio->io_gang_leader == zio &&
2984 2980 !(zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)))
2985 2981 zio_dva_unallocate(zio, zio->io_gang_tree, bp);
2986 2982
2987 2983 zio_gang_tree_free(&zio->io_gang_tree);
2988 2984
2989 2985 /*
2990 2986 * Godfather I/Os should never suspend.
2991 2987 */
2992 2988 if ((zio->io_flags & ZIO_FLAG_GODFATHER) &&
2993 2989 (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND))
2994 2990 zio->io_reexecute = 0;
2995 2991
2996 2992 if (zio->io_reexecute) {
2997 2993 /*
2998 2994 * This is a logical I/O that wants to reexecute.
2999 2995 *
3000 2996 * Reexecute is top-down. When an i/o fails, if it's not
3001 2997 * the root, it simply notifies its parent and sticks around.
3002 2998 * The parent, seeing that it still has children in zio_done(),
3003 2999 * does the same. This percolates all the way up to the root.
3004 3000 * The root i/o will reexecute or suspend the entire tree.
3005 3001 *
3006 3002 * This approach ensures that zio_reexecute() honors
3007 3003 * all the original i/o dependency relationships, e.g.
3008 3004 * parents not executing until children are ready.
3009 3005 */
3010 3006 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3011 3007
3012 3008 zio->io_gang_leader = NULL;
3013 3009
3014 3010 mutex_enter(&zio->io_lock);
3015 3011 zio->io_state[ZIO_WAIT_DONE] = 1;
3016 3012 mutex_exit(&zio->io_lock);
3017 3013
3018 3014 /*
3019 3015 * "The Godfather" I/O monitors its children but is
3020 3016 * not a true parent to them. It will track them through
3021 3017 * the pipeline but severs its ties whenever they get into
3022 3018 * trouble (e.g. suspended). This allows "The Godfather"
3023 3019 * I/O to return status without blocking.
3024 3020 */
3025 3021 for (pio = zio_walk_parents(zio); pio != NULL; pio = pio_next) {
3026 3022 zio_link_t *zl = zio->io_walk_link;
3027 3023 pio_next = zio_walk_parents(zio);
3028 3024
3029 3025 if ((pio->io_flags & ZIO_FLAG_GODFATHER) &&
3030 3026 (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND)) {
3031 3027 zio_remove_child(pio, zio, zl);
3032 3028 zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
3033 3029 }
3034 3030 }
3035 3031
3036 3032 if ((pio = zio_unique_parent(zio)) != NULL) {
3037 3033 /*
3038 3034 * We're not a root i/o, so there's nothing to do
3039 3035 * but notify our parent. Don't propagate errors
3040 3036 * upward since we haven't permanently failed yet.
3041 3037 */
3042 3038 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
3043 3039 zio->io_flags |= ZIO_FLAG_DONT_PROPAGATE;
3044 3040 zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
3045 3041 } else if (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND) {
3046 3042 /*
3047 3043 * We'd fail again if we reexecuted now, so suspend
3048 3044 * until conditions improve (e.g. device comes online).
3049 3045 */
3050 3046 zio_suspend(spa, zio);
3051 3047 } else {
3052 3048 /*
3053 3049 * Reexecution is potentially a huge amount of work.
3054 3050 * Hand it off to the otherwise-unused claim taskq.
3055 3051 */
3056 3052 ASSERT(zio->io_tqent.tqent_next == NULL);
3057 3053 spa_taskq_dispatch_ent(spa, ZIO_TYPE_CLAIM,
3058 3054 ZIO_TASKQ_ISSUE, (task_func_t *)zio_reexecute, zio,
3059 3055 0, &zio->io_tqent);
3060 3056 }
3061 3057 return (ZIO_PIPELINE_STOP);
3062 3058 }
3063 3059
3064 3060 ASSERT(zio->io_child_count == 0);
3065 3061 ASSERT(zio->io_reexecute == 0);
3066 3062 ASSERT(zio->io_error == 0 || (zio->io_flags & ZIO_FLAG_CANFAIL));
3067 3063
3068 3064 /*
3069 3065 * Report any checksum errors, since the I/O is complete.
3070 3066 */
3071 3067 while (zio->io_cksum_report != NULL) {
3072 3068 zio_cksum_report_t *zcr = zio->io_cksum_report;
3073 3069 zio->io_cksum_report = zcr->zcr_next;
3074 3070 zcr->zcr_next = NULL;
3075 3071 zcr->zcr_finish(zcr, NULL);
3076 3072 zfs_ereport_free_checksum(zcr);
3077 3073 }
3078 3074
3079 3075 /*
3080 3076 * It is the responsibility of the done callback to ensure that this
3081 3077 * particular zio is no longer discoverable for adoption, and as
3082 3078 * such, cannot acquire any new parents.
3083 3079 */
3084 3080 if (zio->io_done)
3085 3081 zio->io_done(zio);
3086 3082
3087 3083 mutex_enter(&zio->io_lock);
3088 3084 zio->io_state[ZIO_WAIT_DONE] = 1;
3089 3085 mutex_exit(&zio->io_lock);
3090 3086
3091 3087 for (pio = zio_walk_parents(zio); pio != NULL; pio = pio_next) {
3092 3088 zio_link_t *zl = zio->io_walk_link;
3093 3089 pio_next = zio_walk_parents(zio);
3094 3090 zio_remove_child(pio, zio, zl);
3095 3091 zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
3096 3092 }
3097 3093
3098 3094 if (zio->io_waiter != NULL) {
3099 3095 mutex_enter(&zio->io_lock);
3100 3096 zio->io_executor = NULL;
3101 3097 cv_broadcast(&zio->io_cv);
3102 3098 mutex_exit(&zio->io_lock);
3103 3099 } else {
3104 3100 zio_destroy(zio);
3105 3101 }
3106 3102
3107 3103 return (ZIO_PIPELINE_STOP);
3108 3104 }
3109 3105
3110 3106 /*
3111 3107 * ==========================================================================
3112 3108 * I/O pipeline definition
3113 3109 * ==========================================================================
3114 3110 */
3115 3111 static zio_pipe_stage_t *zio_pipeline[] = {
3116 3112 NULL,
3117 3113 zio_read_bp_init,
3118 3114 zio_free_bp_init,
3119 3115 zio_issue_async,
3120 3116 zio_write_bp_init,
3121 3117 zio_checksum_generate,
3122 3118 zio_nop_write,
3123 3119 zio_ddt_read_start,
3124 3120 zio_ddt_read_done,
3125 3121 zio_ddt_write,
3126 3122 zio_ddt_free,
3127 3123 zio_gang_assemble,
3128 3124 zio_gang_issue,
3129 3125 zio_dva_allocate,
3130 3126 zio_dva_free,
3131 3127 zio_dva_claim,
3132 3128 zio_ready,
3133 3129 zio_vdev_io_start,
3134 3130 zio_vdev_io_done,
3135 3131 zio_vdev_io_assess,
3136 3132 zio_checksum_verify,
3137 3133 zio_done
3138 3134 };
3139 3135
3140 3136 /* dnp is the dnode for zb1->zb_object */
3141 3137 boolean_t
3142 3138 zbookmark_is_before(const dnode_phys_t *dnp, const zbookmark_t *zb1,
3143 3139 const zbookmark_t *zb2)
3144 3140 {
3145 3141 uint64_t zb1nextL0, zb2thisobj;
3146 3142
3147 3143 ASSERT(zb1->zb_objset == zb2->zb_objset);
3148 3144 ASSERT(zb2->zb_level == 0);
3149 3145
3150 3146 /*
3151 3147 * A bookmark in the deadlist is considered to be after
3152 3148 * everything else.
3153 3149 */
3154 3150 if (zb2->zb_object == DMU_DEADLIST_OBJECT)
3155 3151 return (B_TRUE);
3156 3152
3157 3153 /* The objset_phys_t isn't before anything. */
3158 3154 if (dnp == NULL)
3159 3155 return (B_FALSE);
3160 3156
3161 3157 zb1nextL0 = (zb1->zb_blkid + 1) <<
3162 3158 ((zb1->zb_level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT));
3163 3159
3164 3160 zb2thisobj = zb2->zb_object ? zb2->zb_object :
3165 3161 zb2->zb_blkid << (DNODE_BLOCK_SHIFT - DNODE_SHIFT);
3166 3162
3167 3163 if (zb1->zb_object == DMU_META_DNODE_OBJECT) {
3168 3164 uint64_t nextobj = zb1nextL0 *
3169 3165 (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT) >> DNODE_SHIFT;
3170 3166 return (nextobj <= zb2thisobj);
3171 3167 }
3172 3168
3173 3169 if (zb1->zb_object < zb2thisobj)
3174 3170 return (B_TRUE);
3175 3171 if (zb1->zb_object > zb2thisobj)
3176 3172 return (B_FALSE);
3177 3173 if (zb2->zb_object == DMU_META_DNODE_OBJECT)
3178 3174 return (B_FALSE);
3179 3175 return (zb1nextL0 <= zb2->zb_blkid);
3180 3176 }
↓ open down ↓ |
531 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX