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