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