]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zio.c
OpenZFS 9290 - device removal reduces redundancy of mirrors
[mirror_zfs.git] / module / zfs / zio.c
CommitLineData
34dc7c2f
BB
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/*
428870ff 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
64fc7762 23 * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
a38718a6 24 * Copyright (c) 2011 Nexenta Systems, Inc. All rights reserved.
34dc7c2f
BB
25 */
26
f1512ee6 27#include <sys/sysmacros.h>
34dc7c2f
BB
28#include <sys/zfs_context.h>
29#include <sys/fm/fs/zfs.h>
30#include <sys/spa.h>
31#include <sys/txg.h>
32#include <sys/spa_impl.h>
33#include <sys/vdev_impl.h>
34#include <sys/zio_impl.h>
35#include <sys/zio_compress.h>
36#include <sys/zio_checksum.h>
428870ff
BB
37#include <sys/dmu_objset.h>
38#include <sys/arc.h>
39#include <sys/ddt.h>
9b67f605 40#include <sys/blkptr.h>
b0bc7a84 41#include <sys/zfeature.h>
d4a72f23 42#include <sys/dsl_scan.h>
3dfb57a3 43#include <sys/metaslab_impl.h>
193a37cb 44#include <sys/time.h>
26ef0cc7 45#include <sys/trace_zio.h>
a6255b7f 46#include <sys/abd.h>
b5256303 47#include <sys/dsl_crypt.h>
34dc7c2f 48
34dc7c2f
BB
49/*
50 * ==========================================================================
51 * I/O type descriptions
52 * ==========================================================================
53 */
e8b96c60 54const char *zio_type_name[ZIO_TYPES] = {
3dfb57a3
DB
55 /*
56 * Note: Linux kernel thread name length is limited
57 * so these names will differ from upstream open zfs.
58 */
451041db 59 "z_null", "z_rd", "z_wr", "z_fr", "z_cl", "z_ioctl"
428870ff 60};
34dc7c2f 61
27f2b90d 62int zio_dva_throttle_enabled = B_TRUE;
3dfb57a3 63
34dc7c2f
BB
64/*
65 * ==========================================================================
66 * I/O kmem caches
67 * ==========================================================================
68 */
69kmem_cache_t *zio_cache;
d164b209 70kmem_cache_t *zio_link_cache;
34dc7c2f
BB
71kmem_cache_t *zio_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
72kmem_cache_t *zio_data_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
a6255b7f
DQ
73#if defined(ZFS_DEBUG) && !defined(_KERNEL)
74uint64_t zio_buf_cache_allocs[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
75uint64_t zio_buf_cache_frees[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
76#endif
77
a69052be 78int zio_delay_max = ZIO_DELAY_MAX;
34dc7c2f 79
98b25418
GW
80#define ZIO_PIPELINE_CONTINUE 0x100
81#define ZIO_PIPELINE_STOP 0x101
82
fcff0f35
PD
83#define BP_SPANB(indblkshift, level) \
84 (((uint64_t)1) << ((level) * ((indblkshift) - SPA_BLKPTRSHIFT)))
85#define COMPARE_META_LEVEL 0x80000000ul
55d85d5a
GW
86/*
87 * The following actions directly effect the spa's sync-to-convergence logic.
88 * The values below define the sync pass when we start performing the action.
89 * Care should be taken when changing these values as they directly impact
90 * spa_sync() performance. Tuning these values may introduce subtle performance
91 * pathologies and should only be done in the context of performance analysis.
92 * These tunables will eventually be removed and replaced with #defines once
93 * enough analysis has been done to determine optimal values.
94 *
95 * The 'zfs_sync_pass_deferred_free' pass must be greater than 1 to ensure that
96 * regular blocks are not deferred.
97 */
98int zfs_sync_pass_deferred_free = 2; /* defer frees starting in this pass */
99int zfs_sync_pass_dont_compress = 5; /* don't compress starting in this pass */
100int zfs_sync_pass_rewrite = 2; /* rewrite new bps starting in this pass */
101
34dc7c2f 102/*
b128c09f
BB
103 * An allocating zio is one that either currently has the DVA allocate
104 * stage set or will have it later in its lifetime.
34dc7c2f 105 */
428870ff
BB
106#define IO_IS_ALLOCATING(zio) ((zio)->io_orig_pipeline & ZIO_STAGE_DVA_ALLOCATE)
107
c409e464 108int zio_requeue_io_start_cut_in_line = 1;
428870ff
BB
109
110#ifdef ZFS_DEBUG
111int zio_buf_debug_limit = 16384;
112#else
113int zio_buf_debug_limit = 0;
114#endif
34dc7c2f 115
da6b4005
NB
116static inline void __zio_execute(zio_t *zio);
117
3dfb57a3
DB
118static void zio_taskq_dispatch(zio_t *, zio_taskq_type_t, boolean_t);
119
34dc7c2f
BB
120void
121zio_init(void)
122{
123 size_t c;
124 vmem_t *data_alloc_arena = NULL;
125
3941503c
BB
126 zio_cache = kmem_cache_create("zio_cache",
127 sizeof (zio_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
d164b209 128 zio_link_cache = kmem_cache_create("zio_link_cache",
6795a698 129 sizeof (zio_link_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
34dc7c2f
BB
130
131 /*
132 * For small buffers, we want a cache for each multiple of
f1512ee6
MA
133 * SPA_MINBLOCKSIZE. For larger buffers, we want a cache
134 * for each quarter-power of 2.
34dc7c2f
BB
135 */
136 for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
137 size_t size = (c + 1) << SPA_MINBLOCKSHIFT;
138 size_t p2 = size;
139 size_t align = 0;
6442f3cf 140 size_t cflags = (size > zio_buf_debug_limit) ? KMC_NODEBUG : 0;
34dc7c2f 141
34328f3c 142#if defined(_ILP32) && defined(_KERNEL)
f1512ee6
MA
143 /*
144 * Cache size limited to 1M on 32-bit platforms until ARC
145 * buffers no longer require virtual address space.
146 */
147 if (size > zfs_max_recordsize)
148 break;
149#endif
150
151 while (!ISP2(p2))
34dc7c2f
BB
152 p2 &= p2 - 1;
153
498877ba
MA
154#ifndef _KERNEL
155 /*
156 * If we are using watchpoints, put each buffer on its own page,
157 * to eliminate the performance overhead of trapping to the
158 * kernel when modifying a non-watched buffer that shares the
159 * page with a watched buffer.
160 */
161 if (arc_watch && !IS_P2ALIGNED(size, PAGESIZE))
162 continue;
fcf64f45
BB
163 /*
164 * Here's the problem - on 4K native devices in userland on
165 * Linux using O_DIRECT, buffers must be 4K aligned or I/O
166 * will fail with EINVAL, causing zdb (and others) to coredump.
167 * Since userland probably doesn't need optimized buffer caches,
168 * we just force 4K alignment on everything.
169 */
170 align = 8 * SPA_MINBLOCKSIZE;
171#else
24fa2034 172 if (size < PAGESIZE) {
34dc7c2f 173 align = SPA_MINBLOCKSIZE;
498877ba 174 } else if (IS_P2ALIGNED(size, p2 >> 2)) {
24fa2034 175 align = PAGESIZE;
34dc7c2f 176 }
fcf64f45 177#endif
34dc7c2f
BB
178
179 if (align != 0) {
180 char name[36];
181 (void) sprintf(name, "zio_buf_%lu", (ulong_t)size);
182 zio_buf_cache[c] = kmem_cache_create(name, size,
6442f3cf 183 align, NULL, NULL, NULL, NULL, NULL, cflags);
34dc7c2f
BB
184
185 (void) sprintf(name, "zio_data_buf_%lu", (ulong_t)size);
186 zio_data_buf_cache[c] = kmem_cache_create(name, size,
ae6ba3db 187 align, NULL, NULL, NULL, NULL,
6442f3cf 188 data_alloc_arena, cflags);
34dc7c2f
BB
189 }
190 }
191
192 while (--c != 0) {
193 ASSERT(zio_buf_cache[c] != NULL);
194 if (zio_buf_cache[c - 1] == NULL)
195 zio_buf_cache[c - 1] = zio_buf_cache[c];
196
197 ASSERT(zio_data_buf_cache[c] != NULL);
198 if (zio_data_buf_cache[c - 1] == NULL)
199 zio_data_buf_cache[c - 1] = zio_data_buf_cache[c];
200 }
201
34dc7c2f 202 zio_inject_init();
9759c60f
ED
203
204 lz4_init();
34dc7c2f
BB
205}
206
207void
208zio_fini(void)
209{
210 size_t c;
211 kmem_cache_t *last_cache = NULL;
212 kmem_cache_t *last_data_cache = NULL;
213
214 for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
f1512ee6
MA
215#ifdef _ILP32
216 /*
217 * Cache size limited to 1M on 32-bit platforms until ARC
218 * buffers no longer require virtual address space.
219 */
220 if (((c + 1) << SPA_MINBLOCKSHIFT) > zfs_max_recordsize)
221 break;
a6255b7f
DQ
222#endif
223#if defined(ZFS_DEBUG) && !defined(_KERNEL)
224 if (zio_buf_cache_allocs[c] != zio_buf_cache_frees[c])
225 (void) printf("zio_fini: [%d] %llu != %llu\n",
226 (int)((c + 1) << SPA_MINBLOCKSHIFT),
227 (long long unsigned)zio_buf_cache_allocs[c],
228 (long long unsigned)zio_buf_cache_frees[c]);
f1512ee6 229#endif
34dc7c2f
BB
230 if (zio_buf_cache[c] != last_cache) {
231 last_cache = zio_buf_cache[c];
232 kmem_cache_destroy(zio_buf_cache[c]);
233 }
234 zio_buf_cache[c] = NULL;
235
236 if (zio_data_buf_cache[c] != last_data_cache) {
237 last_data_cache = zio_data_buf_cache[c];
238 kmem_cache_destroy(zio_data_buf_cache[c]);
239 }
240 zio_data_buf_cache[c] = NULL;
241 }
242
d164b209 243 kmem_cache_destroy(zio_link_cache);
34dc7c2f
BB
244 kmem_cache_destroy(zio_cache);
245
246 zio_inject_fini();
9759c60f
ED
247
248 lz4_fini();
34dc7c2f
BB
249}
250
251/*
252 * ==========================================================================
253 * Allocate and free I/O buffers
254 * ==========================================================================
255 */
256
257/*
258 * Use zio_buf_alloc to allocate ZFS metadata. This data will appear in a
259 * crashdump if the kernel panics, so use it judiciously. Obviously, it's
260 * useful to inspect ZFS metadata, but if possible, we should avoid keeping
261 * excess / transient data in-core during a crashdump.
262 */
263void *
264zio_buf_alloc(size_t size)
265{
266 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
267
63e3a861 268 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
a6255b7f
DQ
269#if defined(ZFS_DEBUG) && !defined(_KERNEL)
270 atomic_add_64(&zio_buf_cache_allocs[c], 1);
271#endif
34dc7c2f 272
efcd79a8 273 return (kmem_cache_alloc(zio_buf_cache[c], KM_PUSHPAGE));
34dc7c2f
BB
274}
275
276/*
277 * Use zio_data_buf_alloc to allocate data. The data will not appear in a
278 * crashdump if the kernel panics. This exists so that we will limit the amount
279 * of ZFS data that shows up in a kernel crashdump. (Thus reducing the amount
280 * of kernel heap dumped to disk when the kernel panics)
281 */
282void *
283zio_data_buf_alloc(size_t size)
284{
285 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
286
63e3a861 287 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
34dc7c2f 288
efcd79a8 289 return (kmem_cache_alloc(zio_data_buf_cache[c], KM_PUSHPAGE));
34dc7c2f
BB
290}
291
292void
293zio_buf_free(void *buf, size_t size)
294{
295 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
296
63e3a861 297 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
a6255b7f
DQ
298#if defined(ZFS_DEBUG) && !defined(_KERNEL)
299 atomic_add_64(&zio_buf_cache_frees[c], 1);
300#endif
34dc7c2f
BB
301
302 kmem_cache_free(zio_buf_cache[c], buf);
303}
304
305void
306zio_data_buf_free(void *buf, size_t size)
307{
308 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
309
63e3a861 310 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
34dc7c2f
BB
311
312 kmem_cache_free(zio_data_buf_cache[c], buf);
313}
314
84c07ada
GN
315static void
316zio_abd_free(void *abd, size_t size)
317{
318 abd_free((abd_t *)abd);
319}
320
34dc7c2f
BB
321/*
322 * ==========================================================================
323 * Push and pop I/O transform buffers
324 * ==========================================================================
325 */
d3c2ae1c 326void
a6255b7f 327zio_push_transform(zio_t *zio, abd_t *data, uint64_t size, uint64_t bufsize,
e9aa730c 328 zio_transform_func_t *transform)
34dc7c2f 329{
79c76d5b 330 zio_transform_t *zt = kmem_alloc(sizeof (zio_transform_t), KM_SLEEP);
34dc7c2f 331
a6255b7f
DQ
332 /*
333 * Ensure that anyone expecting this zio to contain a linear ABD isn't
334 * going to get a nasty surprise when they try to access the data.
335 */
336 IMPLY(abd_is_linear(zio->io_abd), abd_is_linear(data));
337
338 zt->zt_orig_abd = zio->io_abd;
b128c09f 339 zt->zt_orig_size = zio->io_size;
34dc7c2f 340 zt->zt_bufsize = bufsize;
b128c09f 341 zt->zt_transform = transform;
34dc7c2f
BB
342
343 zt->zt_next = zio->io_transform_stack;
344 zio->io_transform_stack = zt;
345
a6255b7f 346 zio->io_abd = data;
34dc7c2f
BB
347 zio->io_size = size;
348}
349
d3c2ae1c 350void
b128c09f 351zio_pop_transforms(zio_t *zio)
34dc7c2f 352{
b128c09f
BB
353 zio_transform_t *zt;
354
355 while ((zt = zio->io_transform_stack) != NULL) {
356 if (zt->zt_transform != NULL)
357 zt->zt_transform(zio,
a6255b7f 358 zt->zt_orig_abd, zt->zt_orig_size);
34dc7c2f 359
428870ff 360 if (zt->zt_bufsize != 0)
a6255b7f 361 abd_free(zio->io_abd);
34dc7c2f 362
a6255b7f 363 zio->io_abd = zt->zt_orig_abd;
b128c09f
BB
364 zio->io_size = zt->zt_orig_size;
365 zio->io_transform_stack = zt->zt_next;
34dc7c2f 366
b128c09f 367 kmem_free(zt, sizeof (zio_transform_t));
34dc7c2f
BB
368 }
369}
370
b128c09f
BB
371/*
372 * ==========================================================================
b5256303 373 * I/O transform callbacks for subblocks, decompression, and decryption
b128c09f
BB
374 * ==========================================================================
375 */
376static void
a6255b7f 377zio_subblock(zio_t *zio, abd_t *data, uint64_t size)
b128c09f
BB
378{
379 ASSERT(zio->io_size > size);
380
381 if (zio->io_type == ZIO_TYPE_READ)
a6255b7f 382 abd_copy(data, zio->io_abd, size);
b128c09f
BB
383}
384
385static void
a6255b7f 386zio_decompress(zio_t *zio, abd_t *data, uint64_t size)
b128c09f 387{
a6255b7f
DQ
388 if (zio->io_error == 0) {
389 void *tmp = abd_borrow_buf(data, size);
390 int ret = zio_decompress_data(BP_GET_COMPRESS(zio->io_bp),
391 zio->io_abd, tmp, zio->io_size, size);
392 abd_return_buf_copy(data, tmp, size);
393
394 if (ret != 0)
395 zio->io_error = SET_ERROR(EIO);
396 }
b128c09f
BB
397}
398
b5256303
TC
399static void
400zio_decrypt(zio_t *zio, abd_t *data, uint64_t size)
401{
402 int ret;
403 void *tmp;
404 blkptr_t *bp = zio->io_bp;
ae76f45c
TC
405 spa_t *spa = zio->io_spa;
406 uint64_t dsobj = zio->io_bookmark.zb_objset;
b5256303
TC
407 uint64_t lsize = BP_GET_LSIZE(bp);
408 dmu_object_type_t ot = BP_GET_TYPE(bp);
409 uint8_t salt[ZIO_DATA_SALT_LEN];
410 uint8_t iv[ZIO_DATA_IV_LEN];
411 uint8_t mac[ZIO_DATA_MAC_LEN];
412 boolean_t no_crypt = B_FALSE;
413
414 ASSERT(BP_USES_CRYPT(bp));
415 ASSERT3U(size, !=, 0);
416
417 if (zio->io_error != 0)
418 return;
419
420 /*
421 * Verify the cksum of MACs stored in an indirect bp. It will always
422 * be possible to verify this since it does not require an encryption
423 * key.
424 */
425 if (BP_HAS_INDIRECT_MAC_CKSUM(bp)) {
426 zio_crypt_decode_mac_bp(bp, mac);
427
428 if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF) {
429 /*
430 * We haven't decompressed the data yet, but
431 * zio_crypt_do_indirect_mac_checksum() requires
432 * decompressed data to be able to parse out the MACs
433 * from the indirect block. We decompress it now and
434 * throw away the result after we are finished.
435 */
436 tmp = zio_buf_alloc(lsize);
437 ret = zio_decompress_data(BP_GET_COMPRESS(bp),
438 zio->io_abd, tmp, zio->io_size, lsize);
439 if (ret != 0) {
440 ret = SET_ERROR(EIO);
441 goto error;
442 }
443 ret = zio_crypt_do_indirect_mac_checksum(B_FALSE,
444 tmp, lsize, BP_SHOULD_BYTESWAP(bp), mac);
445 zio_buf_free(tmp, lsize);
446 } else {
447 ret = zio_crypt_do_indirect_mac_checksum_abd(B_FALSE,
448 zio->io_abd, size, BP_SHOULD_BYTESWAP(bp), mac);
449 }
450 abd_copy(data, zio->io_abd, size);
451
452 if (ret != 0)
453 goto error;
454
455 return;
456 }
457
458 /*
459 * If this is an authenticated block, just check the MAC. It would be
460 * nice to separate this out into its own flag, but for the moment
461 * enum zio_flag is out of bits.
462 */
463 if (BP_IS_AUTHENTICATED(bp)) {
464 if (ot == DMU_OT_OBJSET) {
ae76f45c
TC
465 ret = spa_do_crypt_objset_mac_abd(B_FALSE, spa,
466 dsobj, zio->io_abd, size, BP_SHOULD_BYTESWAP(bp));
b5256303
TC
467 } else {
468 zio_crypt_decode_mac_bp(bp, mac);
ae76f45c
TC
469 ret = spa_do_crypt_mac_abd(B_FALSE, spa, dsobj,
470 zio->io_abd, size, mac);
b5256303
TC
471 }
472 abd_copy(data, zio->io_abd, size);
473
474 if (ret != 0)
475 goto error;
476
477 return;
478 }
479
480 zio_crypt_decode_params_bp(bp, salt, iv);
481
482 if (ot == DMU_OT_INTENT_LOG) {
483 tmp = abd_borrow_buf_copy(zio->io_abd, sizeof (zil_chain_t));
484 zio_crypt_decode_mac_zil(tmp, mac);
485 abd_return_buf(zio->io_abd, tmp, sizeof (zil_chain_t));
486 } else {
487 zio_crypt_decode_mac_bp(bp, mac);
488 }
489
ae76f45c
TC
490 ret = spa_do_crypt_abd(B_FALSE, spa, dsobj, bp, bp->blk_birth,
491 size, data, zio->io_abd, iv, mac, salt, &no_crypt);
b5256303
TC
492 if (no_crypt)
493 abd_copy(data, zio->io_abd, size);
494
495 if (ret != 0)
496 goto error;
497
498 return;
499
500error:
501 /* assert that the key was found unless this was speculative */
502 ASSERT(ret != ENOENT || (zio->io_flags & ZIO_FLAG_SPECULATIVE));
503
504 /*
505 * If there was a decryption / authentication error return EIO as
506 * the io_error. If this was not a speculative zio, create an ereport.
507 */
508 if (ret == ECKSUM) {
a2c2ed1b 509 zio->io_error = SET_ERROR(EIO);
b5256303
TC
510 if ((zio->io_flags & ZIO_FLAG_SPECULATIVE) == 0) {
511 zfs_ereport_post(FM_EREPORT_ZFS_AUTHENTICATION,
ae76f45c 512 spa, NULL, &zio->io_bookmark, zio, 0, 0);
b5256303
TC
513 }
514 } else {
515 zio->io_error = ret;
516 }
517}
518
b128c09f
BB
519/*
520 * ==========================================================================
521 * I/O parent/child relationships and pipeline interlocks
522 * ==========================================================================
523 */
d164b209 524zio_t *
3dfb57a3 525zio_walk_parents(zio_t *cio, zio_link_t **zl)
d164b209 526{
d164b209 527 list_t *pl = &cio->io_parent_list;
b128c09f 528
3dfb57a3
DB
529 *zl = (*zl == NULL) ? list_head(pl) : list_next(pl, *zl);
530 if (*zl == NULL)
d164b209
BB
531 return (NULL);
532
3dfb57a3
DB
533 ASSERT((*zl)->zl_child == cio);
534 return ((*zl)->zl_parent);
d164b209
BB
535}
536
537zio_t *
3dfb57a3 538zio_walk_children(zio_t *pio, zio_link_t **zl)
d164b209 539{
d164b209
BB
540 list_t *cl = &pio->io_child_list;
541
a8b2e306
TC
542 ASSERT(MUTEX_HELD(&pio->io_lock));
543
3dfb57a3
DB
544 *zl = (*zl == NULL) ? list_head(cl) : list_next(cl, *zl);
545 if (*zl == NULL)
d164b209
BB
546 return (NULL);
547
3dfb57a3
DB
548 ASSERT((*zl)->zl_parent == pio);
549 return ((*zl)->zl_child);
d164b209
BB
550}
551
552zio_t *
553zio_unique_parent(zio_t *cio)
554{
3dfb57a3
DB
555 zio_link_t *zl = NULL;
556 zio_t *pio = zio_walk_parents(cio, &zl);
d164b209 557
3dfb57a3 558 VERIFY3P(zio_walk_parents(cio, &zl), ==, NULL);
d164b209
BB
559 return (pio);
560}
561
562void
563zio_add_child(zio_t *pio, zio_t *cio)
b128c09f 564{
79c76d5b 565 zio_link_t *zl = kmem_cache_alloc(zio_link_cache, KM_SLEEP);
d164b209
BB
566
567 /*
568 * Logical I/Os can have logical, gang, or vdev children.
569 * Gang I/Os can have gang or vdev children.
570 * Vdev I/Os can only have vdev children.
571 * The following ASSERT captures all of these constraints.
572 */
1ce23dca 573 ASSERT3S(cio->io_child_type, <=, pio->io_child_type);
d164b209
BB
574
575 zl->zl_parent = pio;
576 zl->zl_child = cio;
577
b128c09f 578 mutex_enter(&pio->io_lock);
a8b2e306 579 mutex_enter(&cio->io_lock);
d164b209
BB
580
581 ASSERT(pio->io_state[ZIO_WAIT_DONE] == 0);
582
1c27024e 583 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
d164b209
BB
584 pio->io_children[cio->io_child_type][w] += !cio->io_state[w];
585
586 list_insert_head(&pio->io_child_list, zl);
587 list_insert_head(&cio->io_parent_list, zl);
588
428870ff
BB
589 pio->io_child_count++;
590 cio->io_parent_count++;
591
d164b209 592 mutex_exit(&cio->io_lock);
a8b2e306 593 mutex_exit(&pio->io_lock);
b128c09f
BB
594}
595
34dc7c2f 596static void
d164b209 597zio_remove_child(zio_t *pio, zio_t *cio, zio_link_t *zl)
b128c09f 598{
d164b209
BB
599 ASSERT(zl->zl_parent == pio);
600 ASSERT(zl->zl_child == cio);
b128c09f
BB
601
602 mutex_enter(&pio->io_lock);
a8b2e306 603 mutex_enter(&cio->io_lock);
d164b209
BB
604
605 list_remove(&pio->io_child_list, zl);
606 list_remove(&cio->io_parent_list, zl);
607
428870ff
BB
608 pio->io_child_count--;
609 cio->io_parent_count--;
610
d164b209 611 mutex_exit(&cio->io_lock);
a8b2e306 612 mutex_exit(&pio->io_lock);
d164b209 613 kmem_cache_free(zio_link_cache, zl);
b128c09f
BB
614}
615
616static boolean_t
ddc751d5 617zio_wait_for_children(zio_t *zio, uint8_t childbits, enum zio_wait_type wait)
34dc7c2f 618{
b128c09f
BB
619 boolean_t waiting = B_FALSE;
620
621 mutex_enter(&zio->io_lock);
622 ASSERT(zio->io_stall == NULL);
ddc751d5
GW
623 for (int c = 0; c < ZIO_CHILD_TYPES; c++) {
624 if (!(ZIO_CHILD_BIT_IS_SET(childbits, c)))
625 continue;
626
627 uint64_t *countp = &zio->io_children[c][wait];
628 if (*countp != 0) {
629 zio->io_stage >>= 1;
630 ASSERT3U(zio->io_stage, !=, ZIO_STAGE_OPEN);
631 zio->io_stall = countp;
632 waiting = B_TRUE;
633 break;
634 }
b128c09f
BB
635 }
636 mutex_exit(&zio->io_lock);
b128c09f
BB
637 return (waiting);
638}
34dc7c2f 639
bf701a83
BB
640__attribute__((always_inline))
641static inline void
b128c09f
BB
642zio_notify_parent(zio_t *pio, zio_t *zio, enum zio_wait_type wait)
643{
644 uint64_t *countp = &pio->io_children[zio->io_child_type][wait];
645 int *errorp = &pio->io_child_error[zio->io_child_type];
34dc7c2f 646
b128c09f
BB
647 mutex_enter(&pio->io_lock);
648 if (zio->io_error && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
649 *errorp = zio_worst_error(*errorp, zio->io_error);
650 pio->io_reexecute |= zio->io_reexecute;
651 ASSERT3U(*countp, >, 0);
e8b96c60
MA
652
653 (*countp)--;
654
655 if (*countp == 0 && pio->io_stall == countp) {
3dfb57a3
DB
656 zio_taskq_type_t type =
657 pio->io_stage < ZIO_STAGE_VDEV_IO_START ? ZIO_TASKQ_ISSUE :
658 ZIO_TASKQ_INTERRUPT;
b128c09f
BB
659 pio->io_stall = NULL;
660 mutex_exit(&pio->io_lock);
3dfb57a3
DB
661 /*
662 * Dispatch the parent zio in its own taskq so that
663 * the child can continue to make progress. This also
664 * prevents overflowing the stack when we have deeply nested
665 * parent-child relationships.
666 */
667 zio_taskq_dispatch(pio, type, B_FALSE);
b128c09f
BB
668 } else {
669 mutex_exit(&pio->io_lock);
34dc7c2f
BB
670 }
671}
672
b128c09f
BB
673static void
674zio_inherit_child_errors(zio_t *zio, enum zio_child c)
675{
676 if (zio->io_child_error[c] != 0 && zio->io_error == 0)
677 zio->io_error = zio->io_child_error[c];
678}
679
3dfb57a3 680int
64fc7762 681zio_bookmark_compare(const void *x1, const void *x2)
3dfb57a3
DB
682{
683 const zio_t *z1 = x1;
684 const zio_t *z2 = x2;
3dfb57a3 685
64fc7762
MA
686 if (z1->io_bookmark.zb_objset < z2->io_bookmark.zb_objset)
687 return (-1);
688 if (z1->io_bookmark.zb_objset > z2->io_bookmark.zb_objset)
689 return (1);
3dfb57a3 690
64fc7762
MA
691 if (z1->io_bookmark.zb_object < z2->io_bookmark.zb_object)
692 return (-1);
693 if (z1->io_bookmark.zb_object > z2->io_bookmark.zb_object)
694 return (1);
3dfb57a3 695
64fc7762
MA
696 if (z1->io_bookmark.zb_level < z2->io_bookmark.zb_level)
697 return (-1);
698 if (z1->io_bookmark.zb_level > z2->io_bookmark.zb_level)
699 return (1);
700
701 if (z1->io_bookmark.zb_blkid < z2->io_bookmark.zb_blkid)
702 return (-1);
703 if (z1->io_bookmark.zb_blkid > z2->io_bookmark.zb_blkid)
704 return (1);
705
706 if (z1 < z2)
707 return (-1);
708 if (z1 > z2)
709 return (1);
710
711 return (0);
3dfb57a3
DB
712}
713
34dc7c2f
BB
714/*
715 * ==========================================================================
b128c09f 716 * Create the various types of I/O (read, write, free, etc)
34dc7c2f
BB
717 * ==========================================================================
718 */
719static zio_t *
428870ff 720zio_create(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
a6255b7f 721 abd_t *data, uint64_t lsize, uint64_t psize, zio_done_func_t *done,
2aa34383
DK
722 void *private, zio_type_t type, zio_priority_t priority,
723 enum zio_flag flags, vdev_t *vd, uint64_t offset,
724 const zbookmark_phys_t *zb, enum zio_stage stage,
725 enum zio_stage pipeline)
34dc7c2f
BB
726{
727 zio_t *zio;
728
2aa34383
DK
729 ASSERT3U(psize, <=, SPA_MAXBLOCKSIZE);
730 ASSERT(P2PHASE(psize, SPA_MINBLOCKSIZE) == 0);
b128c09f
BB
731 ASSERT(P2PHASE(offset, SPA_MINBLOCKSIZE) == 0);
732
733 ASSERT(!vd || spa_config_held(spa, SCL_STATE_ALL, RW_READER));
734 ASSERT(!bp || !(flags & ZIO_FLAG_CONFIG_WRITER));
735 ASSERT(vd || stage == ZIO_STAGE_OPEN);
34dc7c2f 736
b5256303 737 IMPLY(lsize != psize, (flags & ZIO_FLAG_RAW_COMPRESS) != 0);
2aa34383 738
79c76d5b 739 zio = kmem_cache_alloc(zio_cache, KM_SLEEP);
3941503c
BB
740 bzero(zio, sizeof (zio_t));
741
448d7aaa 742 mutex_init(&zio->io_lock, NULL, MUTEX_NOLOCKDEP, NULL);
3941503c
BB
743 cv_init(&zio->io_cv, NULL, CV_DEFAULT, NULL);
744
745 list_create(&zio->io_parent_list, sizeof (zio_link_t),
746 offsetof(zio_link_t, zl_parent_node));
747 list_create(&zio->io_child_list, sizeof (zio_link_t),
748 offsetof(zio_link_t, zl_child_node));
4e21fd06 749 metaslab_trace_init(&zio->io_alloc_list);
d164b209 750
b128c09f
BB
751 if (vd != NULL)
752 zio->io_child_type = ZIO_CHILD_VDEV;
753 else if (flags & ZIO_FLAG_GANG_CHILD)
754 zio->io_child_type = ZIO_CHILD_GANG;
428870ff
BB
755 else if (flags & ZIO_FLAG_DDT_CHILD)
756 zio->io_child_type = ZIO_CHILD_DDT;
b128c09f
BB
757 else
758 zio->io_child_type = ZIO_CHILD_LOGICAL;
759
34dc7c2f 760 if (bp != NULL) {
428870ff 761 zio->io_bp = (blkptr_t *)bp;
34dc7c2f
BB
762 zio->io_bp_copy = *bp;
763 zio->io_bp_orig = *bp;
428870ff
BB
764 if (type != ZIO_TYPE_WRITE ||
765 zio->io_child_type == ZIO_CHILD_DDT)
b128c09f 766 zio->io_bp = &zio->io_bp_copy; /* so caller can free */
9babb374 767 if (zio->io_child_type == ZIO_CHILD_LOGICAL)
b128c09f 768 zio->io_logical = zio;
9babb374
BB
769 if (zio->io_child_type > ZIO_CHILD_GANG && BP_IS_GANG(bp))
770 pipeline |= ZIO_GANG_STAGES;
34dc7c2f 771 }
b128c09f
BB
772
773 zio->io_spa = spa;
774 zio->io_txg = txg;
34dc7c2f
BB
775 zio->io_done = done;
776 zio->io_private = private;
777 zio->io_type = type;
778 zio->io_priority = priority;
b128c09f
BB
779 zio->io_vd = vd;
780 zio->io_offset = offset;
a6255b7f 781 zio->io_orig_abd = zio->io_abd = data;
2aa34383
DK
782 zio->io_orig_size = zio->io_size = psize;
783 zio->io_lsize = lsize;
b128c09f
BB
784 zio->io_orig_flags = zio->io_flags = flags;
785 zio->io_orig_stage = zio->io_stage = stage;
786 zio->io_orig_pipeline = zio->io_pipeline = pipeline;
3dfb57a3 787 zio->io_pipeline_trace = ZIO_STAGE_OPEN;
34dc7c2f 788
d164b209
BB
789 zio->io_state[ZIO_WAIT_READY] = (stage >= ZIO_STAGE_READY);
790 zio->io_state[ZIO_WAIT_DONE] = (stage >= ZIO_STAGE_DONE);
791
b128c09f
BB
792 if (zb != NULL)
793 zio->io_bookmark = *zb;
794
795 if (pio != NULL) {
b128c09f 796 if (zio->io_logical == NULL)
34dc7c2f 797 zio->io_logical = pio->io_logical;
9babb374
BB
798 if (zio->io_child_type == ZIO_CHILD_GANG)
799 zio->io_gang_leader = pio->io_gang_leader;
b128c09f 800 zio_add_child(pio, zio);
34dc7c2f
BB
801 }
802
a38718a6
GA
803 taskq_init_ent(&zio->io_tqent);
804
34dc7c2f
BB
805 return (zio);
806}
807
808static void
b128c09f 809zio_destroy(zio_t *zio)
34dc7c2f 810{
4e21fd06 811 metaslab_trace_fini(&zio->io_alloc_list);
3941503c
BB
812 list_destroy(&zio->io_parent_list);
813 list_destroy(&zio->io_child_list);
814 mutex_destroy(&zio->io_lock);
815 cv_destroy(&zio->io_cv);
b128c09f 816 kmem_cache_free(zio_cache, zio);
34dc7c2f
BB
817}
818
819zio_t *
d164b209 820zio_null(zio_t *pio, spa_t *spa, vdev_t *vd, zio_done_func_t *done,
428870ff 821 void *private, enum zio_flag flags)
34dc7c2f
BB
822{
823 zio_t *zio;
824
2aa34383 825 zio = zio_create(pio, spa, 0, NULL, NULL, 0, 0, done, private,
d164b209 826 ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
b128c09f 827 ZIO_STAGE_OPEN, ZIO_INTERLOCK_PIPELINE);
34dc7c2f
BB
828
829 return (zio);
830}
831
832zio_t *
428870ff 833zio_root(spa_t *spa, zio_done_func_t *done, void *private, enum zio_flag flags)
34dc7c2f 834{
d164b209 835 return (zio_null(NULL, spa, NULL, done, private, flags));
34dc7c2f
BB
836}
837
63e3a861
MA
838void
839zfs_blkptr_verify(spa_t *spa, const blkptr_t *bp)
840{
63e3a861
MA
841 if (!DMU_OT_IS_VALID(BP_GET_TYPE(bp))) {
842 zfs_panic_recover("blkptr at %p has invalid TYPE %llu",
843 bp, (longlong_t)BP_GET_TYPE(bp));
844 }
845 if (BP_GET_CHECKSUM(bp) >= ZIO_CHECKSUM_FUNCTIONS ||
846 BP_GET_CHECKSUM(bp) <= ZIO_CHECKSUM_ON) {
847 zfs_panic_recover("blkptr at %p has invalid CHECKSUM %llu",
848 bp, (longlong_t)BP_GET_CHECKSUM(bp));
849 }
850 if (BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_FUNCTIONS ||
851 BP_GET_COMPRESS(bp) <= ZIO_COMPRESS_ON) {
852 zfs_panic_recover("blkptr at %p has invalid COMPRESS %llu",
853 bp, (longlong_t)BP_GET_COMPRESS(bp));
854 }
855 if (BP_GET_LSIZE(bp) > SPA_MAXBLOCKSIZE) {
856 zfs_panic_recover("blkptr at %p has invalid LSIZE %llu",
857 bp, (longlong_t)BP_GET_LSIZE(bp));
858 }
859 if (BP_GET_PSIZE(bp) > SPA_MAXBLOCKSIZE) {
860 zfs_panic_recover("blkptr at %p has invalid PSIZE %llu",
861 bp, (longlong_t)BP_GET_PSIZE(bp));
862 }
863
864 if (BP_IS_EMBEDDED(bp)) {
865 if (BPE_GET_ETYPE(bp) > NUM_BP_EMBEDDED_TYPES) {
866 zfs_panic_recover("blkptr at %p has invalid ETYPE %llu",
867 bp, (longlong_t)BPE_GET_ETYPE(bp));
868 }
869 }
870
871 /*
872 * Pool-specific checks.
873 *
874 * Note: it would be nice to verify that the blk_birth and
875 * BP_PHYSICAL_BIRTH() are not too large. However, spa_freeze()
876 * allows the birth time of log blocks (and dmu_sync()-ed blocks
877 * that are in the log) to be arbitrarily large.
878 */
1c27024e 879 for (int i = 0; i < BP_GET_NDVAS(bp); i++) {
63e3a861 880 uint64_t vdevid = DVA_GET_VDEV(&bp->blk_dva[i]);
1c27024e 881
63e3a861
MA
882 if (vdevid >= spa->spa_root_vdev->vdev_children) {
883 zfs_panic_recover("blkptr at %p DVA %u has invalid "
884 "VDEV %llu",
885 bp, i, (longlong_t)vdevid);
ee3a23b8 886 continue;
63e3a861 887 }
1c27024e 888 vdev_t *vd = spa->spa_root_vdev->vdev_child[vdevid];
63e3a861
MA
889 if (vd == NULL) {
890 zfs_panic_recover("blkptr at %p DVA %u has invalid "
891 "VDEV %llu",
892 bp, i, (longlong_t)vdevid);
ee3a23b8 893 continue;
63e3a861
MA
894 }
895 if (vd->vdev_ops == &vdev_hole_ops) {
896 zfs_panic_recover("blkptr at %p DVA %u has hole "
897 "VDEV %llu",
898 bp, i, (longlong_t)vdevid);
ee3a23b8 899 continue;
63e3a861
MA
900 }
901 if (vd->vdev_ops == &vdev_missing_ops) {
902 /*
903 * "missing" vdevs are valid during import, but we
904 * don't have their detailed info (e.g. asize), so
905 * we can't perform any more checks on them.
906 */
907 continue;
908 }
1c27024e
DB
909 uint64_t offset = DVA_GET_OFFSET(&bp->blk_dva[i]);
910 uint64_t asize = DVA_GET_ASIZE(&bp->blk_dva[i]);
63e3a861
MA
911 if (BP_IS_GANG(bp))
912 asize = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
913 if (offset + asize > vd->vdev_asize) {
914 zfs_panic_recover("blkptr at %p DVA %u has invalid "
915 "OFFSET %llu",
916 bp, i, (longlong_t)offset);
917 }
918 }
919}
920
34dc7c2f 921zio_t *
b128c09f 922zio_read(zio_t *pio, spa_t *spa, const blkptr_t *bp,
a6255b7f 923 abd_t *data, uint64_t size, zio_done_func_t *done, void *private,
5dbd68a3 924 zio_priority_t priority, enum zio_flag flags, const zbookmark_phys_t *zb)
34dc7c2f
BB
925{
926 zio_t *zio;
927
63e3a861
MA
928 zfs_blkptr_verify(spa, bp);
929
428870ff 930 zio = zio_create(pio, spa, BP_PHYSICAL_BIRTH(bp), bp,
2aa34383 931 data, size, size, done, private,
b128c09f 932 ZIO_TYPE_READ, priority, flags, NULL, 0, zb,
428870ff
BB
933 ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
934 ZIO_DDT_CHILD_READ_PIPELINE : ZIO_READ_PIPELINE);
34dc7c2f 935
b128c09f
BB
936 return (zio);
937}
34dc7c2f 938
34dc7c2f 939zio_t *
b128c09f 940zio_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
a6255b7f 941 abd_t *data, uint64_t lsize, uint64_t psize, const zio_prop_t *zp,
bc77ba73
PD
942 zio_done_func_t *ready, zio_done_func_t *children_ready,
943 zio_done_func_t *physdone, zio_done_func_t *done,
944 void *private, zio_priority_t priority, enum zio_flag flags,
945 const zbookmark_phys_t *zb)
34dc7c2f
BB
946{
947 zio_t *zio;
948
b128c09f
BB
949 ASSERT(zp->zp_checksum >= ZIO_CHECKSUM_OFF &&
950 zp->zp_checksum < ZIO_CHECKSUM_FUNCTIONS &&
951 zp->zp_compress >= ZIO_COMPRESS_OFF &&
952 zp->zp_compress < ZIO_COMPRESS_FUNCTIONS &&
9ae529ec 953 DMU_OT_IS_VALID(zp->zp_type) &&
b128c09f 954 zp->zp_level < 32 &&
428870ff 955 zp->zp_copies > 0 &&
03c6040b 956 zp->zp_copies <= spa_max_replication(spa));
34dc7c2f 957
2aa34383 958 zio = zio_create(pio, spa, txg, bp, data, lsize, psize, done, private,
b128c09f 959 ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb,
428870ff
BB
960 ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
961 ZIO_DDT_CHILD_WRITE_PIPELINE : ZIO_WRITE_PIPELINE);
34dc7c2f
BB
962
963 zio->io_ready = ready;
bc77ba73 964 zio->io_children_ready = children_ready;
e8b96c60 965 zio->io_physdone = physdone;
b128c09f 966 zio->io_prop = *zp;
34dc7c2f 967
9b67f605
MA
968 /*
969 * Data can be NULL if we are going to call zio_write_override() to
970 * provide the already-allocated BP. But we may need the data to
971 * verify a dedup hit (if requested). In this case, don't try to
b5256303
TC
972 * dedup (just take the already-allocated BP verbatim). Encrypted
973 * dedup blocks need data as well so we also disable dedup in this
974 * case.
9b67f605 975 */
b5256303
TC
976 if (data == NULL &&
977 (zio->io_prop.zp_dedup_verify || zio->io_prop.zp_encrypt)) {
9b67f605
MA
978 zio->io_prop.zp_dedup = zio->io_prop.zp_dedup_verify = B_FALSE;
979 }
980
34dc7c2f
BB
981 return (zio);
982}
983
984zio_t *
a6255b7f 985zio_rewrite(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, abd_t *data,
e8b96c60 986 uint64_t size, zio_done_func_t *done, void *private,
5dbd68a3 987 zio_priority_t priority, enum zio_flag flags, zbookmark_phys_t *zb)
34dc7c2f
BB
988{
989 zio_t *zio;
990
2aa34383 991 zio = zio_create(pio, spa, txg, bp, data, size, size, done, private,
3dfb57a3 992 ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_IO_REWRITE, NULL, 0, zb,
b128c09f 993 ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE);
34dc7c2f
BB
994
995 return (zio);
996}
997
428870ff 998void
03c6040b 999zio_write_override(zio_t *zio, blkptr_t *bp, int copies, boolean_t nopwrite)
428870ff
BB
1000{
1001 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
1002 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1003 ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
1004 ASSERT(zio->io_txg == spa_syncing_txg(zio->io_spa));
1005
03c6040b
GW
1006 /*
1007 * We must reset the io_prop to match the values that existed
1008 * when the bp was first written by dmu_sync() keeping in mind
1009 * that nopwrite and dedup are mutually exclusive.
1010 */
1011 zio->io_prop.zp_dedup = nopwrite ? B_FALSE : zio->io_prop.zp_dedup;
1012 zio->io_prop.zp_nopwrite = nopwrite;
428870ff
BB
1013 zio->io_prop.zp_copies = copies;
1014 zio->io_bp_override = bp;
1015}
1016
1017void
1018zio_free(spa_t *spa, uint64_t txg, const blkptr_t *bp)
1019{
9b67f605 1020
a1d477c2
MA
1021 zfs_blkptr_verify(spa, bp);
1022
9b67f605
MA
1023 /*
1024 * The check for EMBEDDED is a performance optimization. We
1025 * process the free here (by ignoring it) rather than
1026 * putting it on the list and then processing it in zio_free_sync().
1027 */
1028 if (BP_IS_EMBEDDED(bp))
1029 return;
13fe0198 1030 metaslab_check_free(spa, bp);
2883cad5
MA
1031
1032 /*
1033 * Frees that are for the currently-syncing txg, are not going to be
1034 * deferred, and which will not need to do a read (i.e. not GANG or
1035 * DEDUP), can be processed immediately. Otherwise, put them on the
1036 * in-memory list for later processing.
1037 */
1038 if (BP_IS_GANG(bp) || BP_GET_DEDUP(bp) ||
1039 txg != spa->spa_syncing_txg ||
1040 spa_sync_pass(spa) >= zfs_sync_pass_deferred_free) {
1041 bplist_append(&spa->spa_free_bplist[txg & TXG_MASK], bp);
1042 } else {
1043 VERIFY0(zio_wait(zio_free_sync(NULL, spa, txg, bp, 0)));
1044 }
428870ff
BB
1045}
1046
34dc7c2f 1047zio_t *
428870ff
BB
1048zio_free_sync(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
1049 enum zio_flag flags)
34dc7c2f
BB
1050{
1051 zio_t *zio;
2883cad5 1052 enum zio_stage stage = ZIO_FREE_PIPELINE;
34dc7c2f 1053
428870ff
BB
1054 ASSERT(!BP_IS_HOLE(bp));
1055 ASSERT(spa_syncing_txg(spa) == txg);
55d85d5a 1056 ASSERT(spa_sync_pass(spa) < zfs_sync_pass_deferred_free);
34dc7c2f 1057
9b67f605
MA
1058 if (BP_IS_EMBEDDED(bp))
1059 return (zio_null(pio, spa, NULL, NULL, NULL, 0));
1060
13fe0198 1061 metaslab_check_free(spa, bp);
8c841793 1062 arc_freed(spa, bp);
d4a72f23 1063 dsl_scan_freed(spa, bp);
13fe0198 1064
2883cad5
MA
1065 /*
1066 * GANG and DEDUP blocks can induce a read (for the gang block header,
1067 * or the DDT), so issue them asynchronously so that this thread is
1068 * not tied up.
1069 */
1070 if (BP_IS_GANG(bp) || BP_GET_DEDUP(bp))
1071 stage |= ZIO_STAGE_ISSUE_ASYNC;
1072
b128c09f 1073 zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
2aa34383
DK
1074 BP_GET_PSIZE(bp), NULL, NULL, ZIO_TYPE_FREE, ZIO_PRIORITY_NOW,
1075 flags, NULL, 0, NULL, ZIO_STAGE_OPEN, stage);
2883cad5 1076
34dc7c2f
BB
1077 return (zio);
1078}
1079
1080zio_t *
428870ff
BB
1081zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
1082 zio_done_func_t *done, void *private, enum zio_flag flags)
34dc7c2f
BB
1083{
1084 zio_t *zio;
1085
a1d477c2 1086 zfs_blkptr_verify(spa, bp);
9b67f605
MA
1087
1088 if (BP_IS_EMBEDDED(bp))
1089 return (zio_null(pio, spa, NULL, NULL, NULL, 0));
1090
34dc7c2f
BB
1091 /*
1092 * A claim is an allocation of a specific block. Claims are needed
1093 * to support immediate writes in the intent log. The issue is that
1094 * immediate writes contain committed data, but in a txg that was
1095 * *not* committed. Upon opening the pool after an unclean shutdown,
1096 * the intent log claims all blocks that contain immediate write data
1097 * so that the SPA knows they're in use.
1098 *
1099 * All claims *must* be resolved in the first txg -- before the SPA
1100 * starts allocating blocks -- so that nothing is allocated twice.
428870ff 1101 * If txg == 0 we just verify that the block is claimable.
34dc7c2f
BB
1102 */
1103 ASSERT3U(spa->spa_uberblock.ub_rootbp.blk_birth, <, spa_first_txg(spa));
428870ff
BB
1104 ASSERT(txg == spa_first_txg(spa) || txg == 0);
1105 ASSERT(!BP_GET_DEDUP(bp) || !spa_writeable(spa)); /* zdb(1M) */
34dc7c2f 1106
b128c09f 1107 zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
2aa34383
DK
1108 BP_GET_PSIZE(bp), done, private, ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW,
1109 flags, NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE);
3dfb57a3 1110 ASSERT0(zio->io_queued_timestamp);
34dc7c2f
BB
1111
1112 return (zio);
1113}
1114
1115zio_t *
1116zio_ioctl(zio_t *pio, spa_t *spa, vdev_t *vd, int cmd,
e8b96c60 1117 zio_done_func_t *done, void *private, enum zio_flag flags)
34dc7c2f
BB
1118{
1119 zio_t *zio;
1120 int c;
1121
1122 if (vd->vdev_children == 0) {
2aa34383 1123 zio = zio_create(pio, spa, 0, NULL, NULL, 0, 0, done, private,
e8b96c60 1124 ZIO_TYPE_IOCTL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
34dc7c2f
BB
1125 ZIO_STAGE_OPEN, ZIO_IOCTL_PIPELINE);
1126
34dc7c2f
BB
1127 zio->io_cmd = cmd;
1128 } else {
d164b209 1129 zio = zio_null(pio, spa, NULL, NULL, NULL, flags);
34dc7c2f
BB
1130
1131 for (c = 0; c < vd->vdev_children; c++)
1132 zio_nowait(zio_ioctl(zio, spa, vd->vdev_child[c], cmd,
e8b96c60 1133 done, private, flags));
34dc7c2f
BB
1134 }
1135
1136 return (zio);
1137}
1138
34dc7c2f
BB
1139zio_t *
1140zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
a6255b7f 1141 abd_t *data, int checksum, zio_done_func_t *done, void *private,
e8b96c60 1142 zio_priority_t priority, enum zio_flag flags, boolean_t labels)
34dc7c2f
BB
1143{
1144 zio_t *zio;
34dc7c2f 1145
b128c09f
BB
1146 ASSERT(vd->vdev_children == 0);
1147 ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
1148 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
1149 ASSERT3U(offset + size, <=, vd->vdev_psize);
34dc7c2f 1150
2aa34383
DK
1151 zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, size, done,
1152 private, ZIO_TYPE_READ, priority, flags | ZIO_FLAG_PHYSICAL, vd,
1153 offset, NULL, ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE);
34dc7c2f 1154
b128c09f 1155 zio->io_prop.zp_checksum = checksum;
34dc7c2f
BB
1156
1157 return (zio);
1158}
1159
1160zio_t *
1161zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
a6255b7f 1162 abd_t *data, int checksum, zio_done_func_t *done, void *private,
e8b96c60 1163 zio_priority_t priority, enum zio_flag flags, boolean_t labels)
34dc7c2f 1164{
34dc7c2f 1165 zio_t *zio;
34dc7c2f 1166
b128c09f
BB
1167 ASSERT(vd->vdev_children == 0);
1168 ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
1169 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
1170 ASSERT3U(offset + size, <=, vd->vdev_psize);
34dc7c2f 1171
2aa34383
DK
1172 zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, size, done,
1173 private, ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_PHYSICAL, vd,
1174 offset, NULL, ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE);
34dc7c2f 1175
b128c09f 1176 zio->io_prop.zp_checksum = checksum;
34dc7c2f 1177
3c67d83a 1178 if (zio_checksum_table[checksum].ci_flags & ZCHECKSUM_FLAG_EMBEDDED) {
34dc7c2f 1179 /*
428870ff 1180 * zec checksums are necessarily destructive -- they modify
b128c09f 1181 * the end of the write buffer to hold the verifier/checksum.
34dc7c2f 1182 * Therefore, we must make a local copy in case the data is
b128c09f 1183 * being written to multiple places in parallel.
34dc7c2f 1184 */
a6255b7f
DQ
1185 abd_t *wbuf = abd_alloc_sametype(data, size);
1186 abd_copy(wbuf, data, size);
1187
b128c09f 1188 zio_push_transform(zio, wbuf, size, size, NULL);
34dc7c2f
BB
1189 }
1190
1191 return (zio);
1192}
1193
1194/*
b128c09f 1195 * Create a child I/O to do some work for us.
34dc7c2f
BB
1196 */
1197zio_t *
b128c09f 1198zio_vdev_child_io(zio_t *pio, blkptr_t *bp, vdev_t *vd, uint64_t offset,
4ea3f864
GM
1199 abd_t *data, uint64_t size, int type, zio_priority_t priority,
1200 enum zio_flag flags, zio_done_func_t *done, void *private)
34dc7c2f 1201{
428870ff 1202 enum zio_stage pipeline = ZIO_VDEV_CHILD_PIPELINE;
b128c09f
BB
1203 zio_t *zio;
1204
a1d477c2
MA
1205 /*
1206 * vdev child I/Os do not propagate their error to the parent.
1207 * Therefore, for correct operation the caller *must* check for
1208 * and handle the error in the child i/o's done callback.
1209 * The only exceptions are i/os that we don't care about
1210 * (OPTIONAL or REPAIR).
1211 */
1212 ASSERT((flags & ZIO_FLAG_OPTIONAL) || (flags & ZIO_FLAG_IO_REPAIR) ||
1213 done != NULL);
1214
34dc7c2f
BB
1215 if (type == ZIO_TYPE_READ && bp != NULL) {
1216 /*
1217 * If we have the bp, then the child should perform the
1218 * checksum and the parent need not. This pushes error
1219 * detection as close to the leaves as possible and
1220 * eliminates redundant checksums in the interior nodes.
1221 */
428870ff
BB
1222 pipeline |= ZIO_STAGE_CHECKSUM_VERIFY;
1223 pio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
34dc7c2f
BB
1224 }
1225
a1d477c2
MA
1226 if (vd->vdev_ops->vdev_op_leaf) {
1227 ASSERT0(vd->vdev_children);
b128c09f 1228 offset += VDEV_LABEL_START_SIZE;
a1d477c2 1229 }
b128c09f 1230
a1d477c2 1231 flags |= ZIO_VDEV_CHILD_FLAGS(pio);
428870ff
BB
1232
1233 /*
1234 * If we've decided to do a repair, the write is not speculative --
1235 * even if the original read was.
1236 */
1237 if (flags & ZIO_FLAG_IO_REPAIR)
1238 flags &= ~ZIO_FLAG_SPECULATIVE;
1239
3dfb57a3
DB
1240 /*
1241 * If we're creating a child I/O that is not associated with a
1242 * top-level vdev, then the child zio is not an allocating I/O.
1243 * If this is a retried I/O then we ignore it since we will
1244 * have already processed the original allocating I/O.
1245 */
1246 if (flags & ZIO_FLAG_IO_ALLOCATING &&
1247 (vd != vd->vdev_top || (flags & ZIO_FLAG_IO_RETRY))) {
6d00b5e1 1248 ASSERTV(metaslab_class_t *mc = spa_normal_class(pio->io_spa));
3dfb57a3
DB
1249
1250 ASSERT(mc->mc_alloc_throttle_enabled);
1251 ASSERT(type == ZIO_TYPE_WRITE);
1252 ASSERT(priority == ZIO_PRIORITY_ASYNC_WRITE);
1253 ASSERT(!(flags & ZIO_FLAG_IO_REPAIR));
1254 ASSERT(!(pio->io_flags & ZIO_FLAG_IO_REWRITE) ||
1255 pio->io_child_type == ZIO_CHILD_GANG);
1256
1257 flags &= ~ZIO_FLAG_IO_ALLOCATING;
1258 }
1259
1260
2aa34383 1261 zio = zio_create(pio, pio->io_spa, pio->io_txg, bp, data, size, size,
428870ff
BB
1262 done, private, type, priority, flags, vd, offset, &pio->io_bookmark,
1263 ZIO_STAGE_VDEV_IO_START >> 1, pipeline);
3dfb57a3 1264 ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
34dc7c2f 1265
e8b96c60
MA
1266 zio->io_physdone = pio->io_physdone;
1267 if (vd->vdev_ops->vdev_op_leaf && zio->io_logical != NULL)
1268 zio->io_logical->io_phys_children++;
1269
b128c09f 1270 return (zio);
34dc7c2f
BB
1271}
1272
b128c09f 1273zio_t *
a6255b7f 1274zio_vdev_delegated_io(vdev_t *vd, uint64_t offset, abd_t *data, uint64_t size,
9e052db4 1275 zio_type_t type, zio_priority_t priority, enum zio_flag flags,
e9aa730c 1276 zio_done_func_t *done, void *private)
34dc7c2f 1277{
b128c09f 1278 zio_t *zio;
34dc7c2f 1279
b128c09f 1280 ASSERT(vd->vdev_ops->vdev_op_leaf);
34dc7c2f 1281
b128c09f 1282 zio = zio_create(NULL, vd->vdev_spa, 0, NULL,
2aa34383 1283 data, size, size, done, private, type, priority,
e8b96c60 1284 flags | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY | ZIO_FLAG_DELEGATED,
b128c09f 1285 vd, offset, NULL,
428870ff 1286 ZIO_STAGE_VDEV_IO_START >> 1, ZIO_VDEV_CHILD_PIPELINE);
34dc7c2f 1287
b128c09f 1288 return (zio);
34dc7c2f
BB
1289}
1290
1291void
b128c09f 1292zio_flush(zio_t *zio, vdev_t *vd)
34dc7c2f 1293{
b128c09f 1294 zio_nowait(zio_ioctl(zio, zio->io_spa, vd, DKIOCFLUSHWRITECACHE,
e8b96c60 1295 NULL, NULL,
b128c09f 1296 ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY));
34dc7c2f
BB
1297}
1298
428870ff
BB
1299void
1300zio_shrink(zio_t *zio, uint64_t size)
1301{
1ce23dca
PS
1302 ASSERT3P(zio->io_executor, ==, NULL);
1303 ASSERT3U(zio->io_orig_size, ==, zio->io_size);
1304 ASSERT3U(size, <=, zio->io_size);
428870ff
BB
1305
1306 /*
1307 * We don't shrink for raidz because of problems with the
1308 * reconstruction when reading back less than the block size.
1309 * Note, BP_IS_RAIDZ() assumes no compression.
1310 */
1311 ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
2aa34383
DK
1312 if (!BP_IS_RAIDZ(zio->io_bp)) {
1313 /* we are not doing a raw write */
1314 ASSERT3U(zio->io_size, ==, zio->io_lsize);
1315 zio->io_orig_size = zio->io_size = zio->io_lsize = size;
1316 }
428870ff
BB
1317}
1318
34dc7c2f
BB
1319/*
1320 * ==========================================================================
b128c09f 1321 * Prepare to read and write logical blocks
34dc7c2f
BB
1322 * ==========================================================================
1323 */
b128c09f 1324
34dc7c2f 1325static int
b128c09f 1326zio_read_bp_init(zio_t *zio)
34dc7c2f 1327{
b128c09f 1328 blkptr_t *bp = zio->io_bp;
b5256303
TC
1329 uint64_t psize =
1330 BP_IS_EMBEDDED(bp) ? BPE_GET_PSIZE(bp) : BP_GET_PSIZE(bp);
34dc7c2f 1331
a1d477c2
MA
1332 ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy);
1333
fb5f0bc8 1334 if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF &&
9babb374 1335 zio->io_child_type == ZIO_CHILD_LOGICAL &&
b5256303 1336 !(zio->io_flags & ZIO_FLAG_RAW_COMPRESS)) {
a6255b7f
DQ
1337 zio_push_transform(zio, abd_alloc_sametype(zio->io_abd, psize),
1338 psize, psize, zio_decompress);
34dc7c2f 1339 }
34dc7c2f 1340
b5256303
TC
1341 if (((BP_IS_PROTECTED(bp) && !(zio->io_flags & ZIO_FLAG_RAW_ENCRYPT)) ||
1342 BP_HAS_INDIRECT_MAC_CKSUM(bp)) &&
1343 zio->io_child_type == ZIO_CHILD_LOGICAL) {
1344 zio_push_transform(zio, abd_alloc_sametype(zio->io_abd, psize),
1345 psize, psize, zio_decrypt);
1346 }
1347
9b67f605 1348 if (BP_IS_EMBEDDED(bp) && BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA) {
a6255b7f
DQ
1349 int psize = BPE_GET_PSIZE(bp);
1350 void *data = abd_borrow_buf(zio->io_abd, psize);
1351
9b67f605 1352 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
a6255b7f
DQ
1353 decode_embedded_bp_compressed(bp, data);
1354 abd_return_buf_copy(zio->io_abd, data, psize);
9b67f605
MA
1355 } else {
1356 ASSERT(!BP_IS_EMBEDDED(bp));
a1d477c2 1357 ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy);
9b67f605
MA
1358 }
1359
9ae529ec 1360 if (!DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) && BP_GET_LEVEL(bp) == 0)
b128c09f
BB
1361 zio->io_flags |= ZIO_FLAG_DONT_CACHE;
1362
428870ff
BB
1363 if (BP_GET_TYPE(bp) == DMU_OT_DDT_ZAP)
1364 zio->io_flags |= ZIO_FLAG_DONT_CACHE;
1365
1366 if (BP_GET_DEDUP(bp) && zio->io_child_type == ZIO_CHILD_LOGICAL)
1367 zio->io_pipeline = ZIO_DDT_READ_PIPELINE;
1368
b128c09f 1369 return (ZIO_PIPELINE_CONTINUE);
34dc7c2f
BB
1370}
1371
b128c09f
BB
1372static int
1373zio_write_bp_init(zio_t *zio)
34dc7c2f 1374{
b128c09f
BB
1375 if (!IO_IS_ALLOCATING(zio))
1376 return (ZIO_PIPELINE_CONTINUE);
34dc7c2f 1377
428870ff
BB
1378 ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
1379
1380 if (zio->io_bp_override) {
3dfb57a3
DB
1381 blkptr_t *bp = zio->io_bp;
1382 zio_prop_t *zp = &zio->io_prop;
1383
428870ff
BB
1384 ASSERT(bp->blk_birth != zio->io_txg);
1385 ASSERT(BP_GET_DEDUP(zio->io_bp_override) == 0);
1386
1387 *bp = *zio->io_bp_override;
1388 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1389
9b67f605
MA
1390 if (BP_IS_EMBEDDED(bp))
1391 return (ZIO_PIPELINE_CONTINUE);
1392
03c6040b
GW
1393 /*
1394 * If we've been overridden and nopwrite is set then
1395 * set the flag accordingly to indicate that a nopwrite
1396 * has already occurred.
1397 */
1398 if (!BP_IS_HOLE(bp) && zp->zp_nopwrite) {
1399 ASSERT(!zp->zp_dedup);
3dfb57a3 1400 ASSERT3U(BP_GET_CHECKSUM(bp), ==, zp->zp_checksum);
03c6040b
GW
1401 zio->io_flags |= ZIO_FLAG_NOPWRITE;
1402 return (ZIO_PIPELINE_CONTINUE);
1403 }
1404
1405 ASSERT(!zp->zp_nopwrite);
1406
428870ff
BB
1407 if (BP_IS_HOLE(bp) || !zp->zp_dedup)
1408 return (ZIO_PIPELINE_CONTINUE);
1409
3c67d83a
TH
1410 ASSERT((zio_checksum_table[zp->zp_checksum].ci_flags &
1411 ZCHECKSUM_FLAG_DEDUP) || zp->zp_dedup_verify);
428870ff 1412
b5256303
TC
1413 if (BP_GET_CHECKSUM(bp) == zp->zp_checksum &&
1414 !zp->zp_encrypt) {
428870ff
BB
1415 BP_SET_DEDUP(bp, 1);
1416 zio->io_pipeline |= ZIO_STAGE_DDT_WRITE;
1417 return (ZIO_PIPELINE_CONTINUE);
1418 }
3dfb57a3
DB
1419
1420 /*
1421 * We were unable to handle this as an override bp, treat
1422 * it as a regular write I/O.
1423 */
5511754b 1424 zio->io_bp_override = NULL;
3dfb57a3
DB
1425 *bp = zio->io_bp_orig;
1426 zio->io_pipeline = zio->io_orig_pipeline;
1427 }
1428
1429 return (ZIO_PIPELINE_CONTINUE);
1430}
1431
1432static int
1433zio_write_compress(zio_t *zio)
1434{
1435 spa_t *spa = zio->io_spa;
1436 zio_prop_t *zp = &zio->io_prop;
1437 enum zio_compress compress = zp->zp_compress;
1438 blkptr_t *bp = zio->io_bp;
1439 uint64_t lsize = zio->io_lsize;
1440 uint64_t psize = zio->io_size;
1441 int pass = 1;
1442
3dfb57a3
DB
1443 /*
1444 * If our children haven't all reached the ready stage,
1445 * wait for them and then repeat this pipeline stage.
1446 */
ddc751d5
GW
1447 if (zio_wait_for_children(zio, ZIO_CHILD_LOGICAL_BIT |
1448 ZIO_CHILD_GANG_BIT, ZIO_WAIT_READY)) {
3dfb57a3 1449 return (ZIO_PIPELINE_STOP);
ddc751d5 1450 }
3dfb57a3
DB
1451
1452 if (!IO_IS_ALLOCATING(zio))
1453 return (ZIO_PIPELINE_CONTINUE);
1454
1455 if (zio->io_children_ready != NULL) {
1456 /*
1457 * Now that all our children are ready, run the callback
1458 * associated with this zio in case it wants to modify the
1459 * data to be written.
1460 */
1461 ASSERT3U(zp->zp_level, >, 0);
1462 zio->io_children_ready(zio);
428870ff 1463 }
34dc7c2f 1464
3dfb57a3
DB
1465 ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
1466 ASSERT(zio->io_bp_override == NULL);
1467
b0bc7a84 1468 if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg) {
b128c09f
BB
1469 /*
1470 * We're rewriting an existing block, which means we're
1471 * working on behalf of spa_sync(). For spa_sync() to
1472 * converge, it must eventually be the case that we don't
1473 * have to allocate new blocks. But compression changes
1474 * the blocksize, which forces a reallocate, and makes
1475 * convergence take longer. Therefore, after the first
1476 * few passes, stop compressing to ensure convergence.
1477 */
428870ff
BB
1478 pass = spa_sync_pass(spa);
1479
1480 ASSERT(zio->io_txg == spa_syncing_txg(spa));
1481 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1482 ASSERT(!BP_GET_DEDUP(bp));
34dc7c2f 1483
55d85d5a 1484 if (pass >= zfs_sync_pass_dont_compress)
b128c09f 1485 compress = ZIO_COMPRESS_OFF;
34dc7c2f 1486
b128c09f 1487 /* Make sure someone doesn't change their mind on overwrites */
9b67f605 1488 ASSERT(BP_IS_EMBEDDED(bp) || MIN(zp->zp_copies + BP_IS_GANG(bp),
428870ff 1489 spa_max_replication(spa)) == BP_GET_NDVAS(bp));
b128c09f 1490 }
34dc7c2f 1491
2aa34383 1492 /* If it's a compressed write that is not raw, compress the buffer. */
b5256303
TC
1493 if (compress != ZIO_COMPRESS_OFF &&
1494 !(zio->io_flags & ZIO_FLAG_RAW_COMPRESS)) {
428870ff 1495 void *cbuf = zio_buf_alloc(lsize);
a6255b7f 1496 psize = zio_compress_data(compress, zio->io_abd, cbuf, lsize);
428870ff 1497 if (psize == 0 || psize == lsize) {
b128c09f 1498 compress = ZIO_COMPRESS_OFF;
428870ff 1499 zio_buf_free(cbuf, lsize);
b5256303
TC
1500 } else if (!zp->zp_dedup && !zp->zp_encrypt &&
1501 psize <= BPE_PAYLOAD_SIZE &&
9b67f605
MA
1502 zp->zp_level == 0 && !DMU_OT_HAS_FILL(zp->zp_type) &&
1503 spa_feature_is_enabled(spa, SPA_FEATURE_EMBEDDED_DATA)) {
1504 encode_embedded_bp_compressed(bp,
1505 cbuf, compress, lsize, psize);
1506 BPE_SET_ETYPE(bp, BP_EMBEDDED_TYPE_DATA);
1507 BP_SET_TYPE(bp, zio->io_prop.zp_type);
1508 BP_SET_LEVEL(bp, zio->io_prop.zp_level);
1509 zio_buf_free(cbuf, lsize);
1510 bp->blk_birth = zio->io_txg;
1511 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1512 ASSERT(spa_feature_is_active(spa,
1513 SPA_FEATURE_EMBEDDED_DATA));
1514 return (ZIO_PIPELINE_CONTINUE);
428870ff 1515 } else {
9b67f605 1516 /*
c3520e7f
MA
1517 * Round up compressed size up to the ashift
1518 * of the smallest-ashift device, and zero the tail.
1519 * This ensures that the compressed size of the BP
1520 * (and thus compressratio property) are correct,
1521 * in that we charge for the padding used to fill out
1522 * the last sector.
9b67f605 1523 */
c3520e7f 1524 ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT);
1c27024e 1525 size_t rounded = (size_t)P2ROUNDUP(psize,
c3520e7f
MA
1526 1ULL << spa->spa_min_ashift);
1527 if (rounded >= lsize) {
9b67f605
MA
1528 compress = ZIO_COMPRESS_OFF;
1529 zio_buf_free(cbuf, lsize);
c3520e7f 1530 psize = lsize;
9b67f605 1531 } else {
a6255b7f
DQ
1532 abd_t *cdata = abd_get_from_buf(cbuf, lsize);
1533 abd_take_ownership_of_buf(cdata, B_TRUE);
1534 abd_zero_off(cdata, psize, rounded - psize);
c3520e7f 1535 psize = rounded;
a6255b7f 1536 zio_push_transform(zio, cdata,
9b67f605
MA
1537 psize, lsize, NULL);
1538 }
b128c09f 1539 }
3dfb57a3
DB
1540
1541 /*
1542 * We were unable to handle this as an override bp, treat
1543 * it as a regular write I/O.
1544 */
1545 zio->io_bp_override = NULL;
1546 *bp = zio->io_bp_orig;
1547 zio->io_pipeline = zio->io_orig_pipeline;
1548
b1d21733
TC
1549 } else if ((zio->io_flags & ZIO_FLAG_RAW_ENCRYPT) != 0 &&
1550 zp->zp_type == DMU_OT_DNODE) {
1551 /*
1552 * The DMU actually relies on the zio layer's compression
1553 * to free metadnode blocks that have had all contained
1554 * dnodes freed. As a result, even when doing a raw
1555 * receive, we must check whether the block can be compressed
1556 * to a hole.
1557 */
1558 psize = zio_compress_data(ZIO_COMPRESS_EMPTY,
1559 zio->io_abd, NULL, lsize);
1560 if (psize == 0)
1561 compress = ZIO_COMPRESS_OFF;
2aa34383
DK
1562 } else {
1563 ASSERT3U(psize, !=, 0);
b128c09f 1564 }
34dc7c2f 1565
b128c09f
BB
1566 /*
1567 * The final pass of spa_sync() must be all rewrites, but the first
1568 * few passes offer a trade-off: allocating blocks defers convergence,
1569 * but newly allocated blocks are sequential, so they can be written
1570 * to disk faster. Therefore, we allow the first few passes of
1571 * spa_sync() to allocate new blocks, but force rewrites after that.
1572 * There should only be a handful of blocks after pass 1 in any case.
1573 */
b0bc7a84
MG
1574 if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg &&
1575 BP_GET_PSIZE(bp) == psize &&
55d85d5a 1576 pass >= zfs_sync_pass_rewrite) {
d6320ddb 1577 ASSERT(psize != 0);
1c27024e 1578 enum zio_stage gang_stages = zio->io_pipeline & ZIO_GANG_STAGES;
b128c09f
BB
1579 zio->io_pipeline = ZIO_REWRITE_PIPELINE | gang_stages;
1580 zio->io_flags |= ZIO_FLAG_IO_REWRITE;
1581 } else {
1582 BP_ZERO(bp);
1583 zio->io_pipeline = ZIO_WRITE_PIPELINE;
1584 }
34dc7c2f 1585
428870ff 1586 if (psize == 0) {
b0bc7a84
MG
1587 if (zio->io_bp_orig.blk_birth != 0 &&
1588 spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) {
1589 BP_SET_LSIZE(bp, lsize);
1590 BP_SET_TYPE(bp, zp->zp_type);
1591 BP_SET_LEVEL(bp, zp->zp_level);
1592 BP_SET_BIRTH(bp, zio->io_txg, 0);
1593 }
b128c09f
BB
1594 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1595 } else {
1596 ASSERT(zp->zp_checksum != ZIO_CHECKSUM_GANG_HEADER);
1597 BP_SET_LSIZE(bp, lsize);
b0bc7a84
MG
1598 BP_SET_TYPE(bp, zp->zp_type);
1599 BP_SET_LEVEL(bp, zp->zp_level);
428870ff 1600 BP_SET_PSIZE(bp, psize);
b128c09f
BB
1601 BP_SET_COMPRESS(bp, compress);
1602 BP_SET_CHECKSUM(bp, zp->zp_checksum);
428870ff 1603 BP_SET_DEDUP(bp, zp->zp_dedup);
b128c09f 1604 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
428870ff
BB
1605 if (zp->zp_dedup) {
1606 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1607 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
b5256303
TC
1608 ASSERT(!zp->zp_encrypt ||
1609 DMU_OT_IS_ENCRYPTED(zp->zp_type));
428870ff
BB
1610 zio->io_pipeline = ZIO_DDT_WRITE_PIPELINE;
1611 }
03c6040b
GW
1612 if (zp->zp_nopwrite) {
1613 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1614 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
1615 zio->io_pipeline |= ZIO_STAGE_NOP_WRITE;
1616 }
428870ff 1617 }
428870ff
BB
1618 return (ZIO_PIPELINE_CONTINUE);
1619}
1620
1621static int
1622zio_free_bp_init(zio_t *zio)
1623{
1624 blkptr_t *bp = zio->io_bp;
1625
1626 if (zio->io_child_type == ZIO_CHILD_LOGICAL) {
1627 if (BP_GET_DEDUP(bp))
1628 zio->io_pipeline = ZIO_DDT_FREE_PIPELINE;
b128c09f 1629 }
34dc7c2f 1630
a1d477c2
MA
1631 ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy);
1632
34dc7c2f
BB
1633 return (ZIO_PIPELINE_CONTINUE);
1634}
1635
b128c09f
BB
1636/*
1637 * ==========================================================================
1638 * Execute the I/O pipeline
1639 * ==========================================================================
1640 */
1641
1642static void
7ef5e54e 1643zio_taskq_dispatch(zio_t *zio, zio_taskq_type_t q, boolean_t cutinline)
34dc7c2f 1644{
428870ff 1645 spa_t *spa = zio->io_spa;
b128c09f 1646 zio_type_t t = zio->io_type;
a38718a6 1647 int flags = (cutinline ? TQ_FRONT : 0);
34dc7c2f
BB
1648
1649 /*
9babb374
BB
1650 * If we're a config writer or a probe, the normal issue and
1651 * interrupt threads may all be blocked waiting for the config lock.
1652 * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL.
34dc7c2f 1653 */
9babb374 1654 if (zio->io_flags & (ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_PROBE))
b128c09f 1655 t = ZIO_TYPE_NULL;
34dc7c2f
BB
1656
1657 /*
b128c09f 1658 * A similar issue exists for the L2ARC write thread until L2ARC 2.0.
34dc7c2f 1659 */
b128c09f
BB
1660 if (t == ZIO_TYPE_WRITE && zio->io_vd && zio->io_vd->vdev_aux)
1661 t = ZIO_TYPE_NULL;
34dc7c2f 1662
428870ff 1663 /*
7ef5e54e
AL
1664 * If this is a high priority I/O, then use the high priority taskq if
1665 * available.
428870ff
BB
1666 */
1667 if (zio->io_priority == ZIO_PRIORITY_NOW &&
7ef5e54e 1668 spa->spa_zio_taskq[t][q + 1].stqs_count != 0)
428870ff
BB
1669 q++;
1670
1671 ASSERT3U(q, <, ZIO_TASKQ_TYPES);
5cc556b4 1672
a38718a6
GA
1673 /*
1674 * NB: We are assuming that the zio can only be dispatched
1675 * to a single taskq at a time. It would be a grievous error
1676 * to dispatch the zio to another taskq at the same time.
1677 */
1678 ASSERT(taskq_empty_ent(&zio->io_tqent));
7ef5e54e
AL
1679 spa_taskq_dispatch_ent(spa, t, q, (task_func_t *)zio_execute, zio,
1680 flags, &zio->io_tqent);
b128c09f 1681}
34dc7c2f 1682
b128c09f 1683static boolean_t
7ef5e54e 1684zio_taskq_member(zio_t *zio, zio_taskq_type_t q)
b128c09f
BB
1685{
1686 kthread_t *executor = zio->io_executor;
1687 spa_t *spa = zio->io_spa;
34dc7c2f 1688
1c27024e 1689 for (zio_type_t t = 0; t < ZIO_TYPES; t++) {
7ef5e54e
AL
1690 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
1691 uint_t i;
1692 for (i = 0; i < tqs->stqs_count; i++) {
1693 if (taskq_member(tqs->stqs_taskq[i], executor))
1694 return (B_TRUE);
1695 }
1696 }
34dc7c2f 1697
b128c09f
BB
1698 return (B_FALSE);
1699}
34dc7c2f 1700
b128c09f
BB
1701static int
1702zio_issue_async(zio_t *zio)
1703{
428870ff 1704 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
b128c09f
BB
1705
1706 return (ZIO_PIPELINE_STOP);
34dc7c2f
BB
1707}
1708
b128c09f
BB
1709void
1710zio_interrupt(zio_t *zio)
34dc7c2f 1711{
428870ff 1712 zio_taskq_dispatch(zio, ZIO_TASKQ_INTERRUPT, B_FALSE);
b128c09f 1713}
34dc7c2f 1714
26ef0cc7
TH
1715void
1716zio_delay_interrupt(zio_t *zio)
1717{
1718 /*
1719 * The timeout_generic() function isn't defined in userspace, so
1720 * rather than trying to implement the function, the zio delay
1721 * functionality has been disabled for userspace builds.
1722 */
1723
1724#ifdef _KERNEL
1725 /*
1726 * If io_target_timestamp is zero, then no delay has been registered
1727 * for this IO, thus jump to the end of this function and "skip" the
1728 * delay; issuing it directly to the zio layer.
1729 */
1730 if (zio->io_target_timestamp != 0) {
1731 hrtime_t now = gethrtime();
1732
1733 if (now >= zio->io_target_timestamp) {
1734 /*
1735 * This IO has already taken longer than the target
1736 * delay to complete, so we don't want to delay it
1737 * any longer; we "miss" the delay and issue it
1738 * directly to the zio layer. This is likely due to
1739 * the target latency being set to a value less than
1740 * the underlying hardware can satisfy (e.g. delay
1741 * set to 1ms, but the disks take 10ms to complete an
1742 * IO request).
1743 */
1744
1745 DTRACE_PROBE2(zio__delay__miss, zio_t *, zio,
1746 hrtime_t, now);
1747
1748 zio_interrupt(zio);
1749 } else {
1750 taskqid_t tid;
1751 hrtime_t diff = zio->io_target_timestamp - now;
1752 clock_t expire_at_tick = ddi_get_lbolt() +
1753 NSEC_TO_TICK(diff);
1754
1755 DTRACE_PROBE3(zio__delay__hit, zio_t *, zio,
1756 hrtime_t, now, hrtime_t, diff);
1757
1758 if (NSEC_TO_TICK(diff) == 0) {
1759 /* Our delay is less than a jiffy - just spin */
1760 zfs_sleep_until(zio->io_target_timestamp);
1761 } else {
1762 /*
1763 * Use taskq_dispatch_delay() in the place of
1764 * OpenZFS's timeout_generic().
1765 */
1766 tid = taskq_dispatch_delay(system_taskq,
02730c33 1767 (task_func_t *)zio_interrupt,
26ef0cc7 1768 zio, TQ_NOSLEEP, expire_at_tick);
48d3eb40 1769 if (tid == TASKQID_INVALID) {
26ef0cc7
TH
1770 /*
1771 * Couldn't allocate a task. Just
1772 * finish the zio without a delay.
1773 */
1774 zio_interrupt(zio);
1775 }
1776 }
1777 }
1778 return;
1779 }
1780#endif
1781 DTRACE_PROBE1(zio__delay__skip, zio_t *, zio);
1782 zio_interrupt(zio);
1783}
1784
8fb1ede1
BB
1785static void
1786zio_deadman_impl(zio_t *pio)
1787{
1788 zio_t *cio, *cio_next;
1789 zio_link_t *zl = NULL;
1790 vdev_t *vd = pio->io_vd;
1791
1792 if (vd != NULL && vd->vdev_ops->vdev_op_leaf) {
1793 vdev_queue_t *vq = &vd->vdev_queue;
1794 zbookmark_phys_t *zb = &pio->io_bookmark;
1795 uint64_t delta = gethrtime() - pio->io_timestamp;
1796 uint64_t failmode = spa_get_deadman_failmode(pio->io_spa);
1797
1798 zfs_dbgmsg("slow zio: zio=%p timestamp=%llu "
1799 "delta=%llu queued=%llu io=%llu "
1800 "path=%s last=%llu "
1801 "type=%d priority=%d flags=0x%x "
1802 "stage=0x%x pipeline=0x%x pipeline-trace=0x%x "
1803 "objset=%llu object=%llu level=%llu blkid=%llu "
1804 "offset=%llu size=%llu error=%d",
1805 pio, pio->io_timestamp,
1806 delta, pio->io_delta, pio->io_delay,
1807 vd->vdev_path, vq->vq_io_complete_ts,
1808 pio->io_type, pio->io_priority, pio->io_flags,
1809 pio->io_state, pio->io_pipeline, pio->io_pipeline_trace,
1810 zb->zb_objset, zb->zb_object, zb->zb_level, zb->zb_blkid,
1811 pio->io_offset, pio->io_size, pio->io_error);
1812 zfs_ereport_post(FM_EREPORT_ZFS_DEADMAN,
1813 pio->io_spa, vd, zb, pio, 0, 0);
1814
1815 if (failmode == ZIO_FAILURE_MODE_CONTINUE &&
1816 taskq_empty_ent(&pio->io_tqent)) {
1817 zio_interrupt(pio);
1818 }
1819 }
1820
1821 mutex_enter(&pio->io_lock);
1822 for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) {
1823 cio_next = zio_walk_children(pio, &zl);
1824 zio_deadman_impl(cio);
1825 }
1826 mutex_exit(&pio->io_lock);
1827}
1828
1829/*
1830 * Log the critical information describing this zio and all of its children
1831 * using the zfs_dbgmsg() interface then post deadman event for the ZED.
1832 */
1833void
1834zio_deadman(zio_t *pio, char *tag)
1835{
1836 spa_t *spa = pio->io_spa;
1837 char *name = spa_name(spa);
1838
1839 if (!zfs_deadman_enabled || spa_suspended(spa))
1840 return;
1841
1842 zio_deadman_impl(pio);
1843
1844 switch (spa_get_deadman_failmode(spa)) {
1845 case ZIO_FAILURE_MODE_WAIT:
1846 zfs_dbgmsg("%s waiting for hung I/O to pool '%s'", tag, name);
1847 break;
1848
1849 case ZIO_FAILURE_MODE_CONTINUE:
1850 zfs_dbgmsg("%s restarting hung I/O for pool '%s'", tag, name);
1851 break;
1852
1853 case ZIO_FAILURE_MODE_PANIC:
1854 fm_panic("%s determined I/O to pool '%s' is hung.", tag, name);
1855 break;
1856 }
1857}
1858
b128c09f
BB
1859/*
1860 * Execute the I/O pipeline until one of the following occurs:
1861 * (1) the I/O completes; (2) the pipeline stalls waiting for
1862 * dependent child I/Os; (3) the I/O issues, so we're waiting
1863 * for an I/O completion interrupt; (4) the I/O is delegated by
1864 * vdev-level caching or aggregation; (5) the I/O is deferred
1865 * due to vdev-level queueing; (6) the I/O is handed off to
1866 * another thread. In all cases, the pipeline stops whenever
8e07b99b 1867 * there's no CPU work; it never burns a thread in cv_wait_io().
b128c09f
BB
1868 *
1869 * There's no locking on io_stage because there's no legitimate way
1870 * for multiple threads to be attempting to process the same I/O.
1871 */
428870ff 1872static zio_pipe_stage_t *zio_pipeline[];
34dc7c2f 1873
da6b4005
NB
1874/*
1875 * zio_execute() is a wrapper around the static function
1876 * __zio_execute() so that we can force __zio_execute() to be
1877 * inlined. This reduces stack overhead which is important
1878 * because __zio_execute() is called recursively in several zio
1879 * code paths. zio_execute() itself cannot be inlined because
1880 * it is externally visible.
1881 */
b128c09f
BB
1882void
1883zio_execute(zio_t *zio)
da6b4005 1884{
92119cc2
BB
1885 fstrans_cookie_t cookie;
1886
1887 cookie = spl_fstrans_mark();
da6b4005 1888 __zio_execute(zio);
92119cc2 1889 spl_fstrans_unmark(cookie);
da6b4005
NB
1890}
1891
b58986ee
BB
1892/*
1893 * Used to determine if in the current context the stack is sized large
1894 * enough to allow zio_execute() to be called recursively. A minimum
1895 * stack size of 16K is required to avoid needing to re-dispatch the zio.
1896 */
1897boolean_t
1898zio_execute_stack_check(zio_t *zio)
1899{
1900#if !defined(HAVE_LARGE_STACKS)
1901 dsl_pool_t *dp = spa_get_dsl(zio->io_spa);
1902
1903 /* Executing in txg_sync_thread() context. */
1904 if (dp && curthread == dp->dp_tx.tx_sync_thread)
1905 return (B_TRUE);
1906
1907 /* Pool initialization outside of zio_taskq context. */
1908 if (dp && spa_is_initializing(dp->dp_spa) &&
1909 !zio_taskq_member(zio, ZIO_TASKQ_ISSUE) &&
1910 !zio_taskq_member(zio, ZIO_TASKQ_ISSUE_HIGH))
1911 return (B_TRUE);
1912#endif /* HAVE_LARGE_STACKS */
1913
1914 return (B_FALSE);
1915}
1916
da6b4005
NB
1917__attribute__((always_inline))
1918static inline void
1919__zio_execute(zio_t *zio)
b128c09f
BB
1920{
1921 zio->io_executor = curthread;
34dc7c2f 1922
3dfb57a3
DB
1923 ASSERT3U(zio->io_queued_timestamp, >, 0);
1924
b128c09f 1925 while (zio->io_stage < ZIO_STAGE_DONE) {
428870ff
BB
1926 enum zio_stage pipeline = zio->io_pipeline;
1927 enum zio_stage stage = zio->io_stage;
b128c09f 1928 int rv;
34dc7c2f 1929
b128c09f 1930 ASSERT(!MUTEX_HELD(&zio->io_lock));
428870ff
BB
1931 ASSERT(ISP2(stage));
1932 ASSERT(zio->io_stall == NULL);
34dc7c2f 1933
428870ff
BB
1934 do {
1935 stage <<= 1;
1936 } while ((stage & pipeline) == 0);
b128c09f
BB
1937
1938 ASSERT(stage <= ZIO_STAGE_DONE);
34dc7c2f
BB
1939
1940 /*
b128c09f
BB
1941 * If we are in interrupt context and this pipeline stage
1942 * will grab a config lock that is held across I/O,
428870ff
BB
1943 * or may wait for an I/O that needs an interrupt thread
1944 * to complete, issue async to avoid deadlock.
1945 *
1946 * For VDEV_IO_START, we cut in line so that the io will
1947 * be sent to disk promptly.
34dc7c2f 1948 */
91579709
BB
1949 if ((stage & ZIO_BLOCKING_STAGES) && zio->io_vd == NULL &&
1950 zio_taskq_member(zio, ZIO_TASKQ_INTERRUPT)) {
b58986ee
BB
1951 boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ?
1952 zio_requeue_io_start_cut_in_line : B_FALSE;
91579709
BB
1953 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut);
1954 return;
1955 }
1956
1957 /*
b58986ee
BB
1958 * If the current context doesn't have large enough stacks
1959 * the zio must be issued asynchronously to prevent overflow.
91579709 1960 */
b58986ee
BB
1961 if (zio_execute_stack_check(zio)) {
1962 boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ?
1963 zio_requeue_io_start_cut_in_line : B_FALSE;
428870ff 1964 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut);
b128c09f 1965 return;
34dc7c2f
BB
1966 }
1967
b128c09f 1968 zio->io_stage = stage;
3dfb57a3 1969 zio->io_pipeline_trace |= zio->io_stage;
9bd274dd 1970 rv = zio_pipeline[highbit64(stage) - 1](zio);
34dc7c2f 1971
b128c09f
BB
1972 if (rv == ZIO_PIPELINE_STOP)
1973 return;
34dc7c2f 1974
b128c09f
BB
1975 ASSERT(rv == ZIO_PIPELINE_CONTINUE);
1976 }
34dc7c2f
BB
1977}
1978
da6b4005 1979
b128c09f
BB
1980/*
1981 * ==========================================================================
1982 * Initiate I/O, either sync or async
1983 * ==========================================================================
1984 */
1985int
1986zio_wait(zio_t *zio)
34dc7c2f 1987{
8fb1ede1 1988 long timeout = MSEC_TO_TICK(zfs_deadman_ziotime_ms);
b128c09f 1989 int error;
34dc7c2f 1990
1ce23dca
PS
1991 ASSERT3S(zio->io_stage, ==, ZIO_STAGE_OPEN);
1992 ASSERT3P(zio->io_executor, ==, NULL);
34dc7c2f 1993
b128c09f 1994 zio->io_waiter = curthread;
3dfb57a3
DB
1995 ASSERT0(zio->io_queued_timestamp);
1996 zio->io_queued_timestamp = gethrtime();
34dc7c2f 1997
da6b4005 1998 __zio_execute(zio);
34dc7c2f 1999
b128c09f 2000 mutex_enter(&zio->io_lock);
8fb1ede1
BB
2001 while (zio->io_executor != NULL) {
2002 error = cv_timedwait_io(&zio->io_cv, &zio->io_lock,
2003 ddi_get_lbolt() + timeout);
2004
2005 if (zfs_deadman_enabled && error == -1 &&
2006 gethrtime() - zio->io_queued_timestamp >
2007 spa_deadman_ziotime(zio->io_spa)) {
2008 mutex_exit(&zio->io_lock);
2009 timeout = MSEC_TO_TICK(zfs_deadman_checktime_ms);
2010 zio_deadman(zio, FTAG);
2011 mutex_enter(&zio->io_lock);
2012 }
2013 }
b128c09f 2014 mutex_exit(&zio->io_lock);
34dc7c2f 2015
b128c09f
BB
2016 error = zio->io_error;
2017 zio_destroy(zio);
34dc7c2f 2018
b128c09f
BB
2019 return (error);
2020}
34dc7c2f 2021
b128c09f
BB
2022void
2023zio_nowait(zio_t *zio)
2024{
1ce23dca 2025 ASSERT3P(zio->io_executor, ==, NULL);
34dc7c2f 2026
d164b209
BB
2027 if (zio->io_child_type == ZIO_CHILD_LOGICAL &&
2028 zio_unique_parent(zio) == NULL) {
8878261f
BB
2029 zio_t *pio;
2030
34dc7c2f 2031 /*
b128c09f 2032 * This is a logical async I/O with no parent to wait for it.
9babb374
BB
2033 * We add it to the spa_async_root_zio "Godfather" I/O which
2034 * will ensure they complete prior to unloading the pool.
34dc7c2f 2035 */
b128c09f 2036 spa_t *spa = zio->io_spa;
8878261f
BB
2037 kpreempt_disable();
2038 pio = spa->spa_async_zio_root[CPU_SEQID];
2039 kpreempt_enable();
9babb374 2040
8878261f 2041 zio_add_child(pio, zio);
b128c09f 2042 }
34dc7c2f 2043
3dfb57a3
DB
2044 ASSERT0(zio->io_queued_timestamp);
2045 zio->io_queued_timestamp = gethrtime();
da6b4005 2046 __zio_execute(zio);
b128c09f 2047}
34dc7c2f 2048
b128c09f
BB
2049/*
2050 * ==========================================================================
1ce23dca 2051 * Reexecute, cancel, or suspend/resume failed I/O
b128c09f
BB
2052 * ==========================================================================
2053 */
34dc7c2f 2054
b128c09f
BB
2055static void
2056zio_reexecute(zio_t *pio)
2057{
d164b209
BB
2058 zio_t *cio, *cio_next;
2059
2060 ASSERT(pio->io_child_type == ZIO_CHILD_LOGICAL);
2061 ASSERT(pio->io_orig_stage == ZIO_STAGE_OPEN);
9babb374
BB
2062 ASSERT(pio->io_gang_leader == NULL);
2063 ASSERT(pio->io_gang_tree == NULL);
34dc7c2f 2064
b128c09f
BB
2065 pio->io_flags = pio->io_orig_flags;
2066 pio->io_stage = pio->io_orig_stage;
2067 pio->io_pipeline = pio->io_orig_pipeline;
2068 pio->io_reexecute = 0;
03c6040b 2069 pio->io_flags |= ZIO_FLAG_REEXECUTED;
3dfb57a3 2070 pio->io_pipeline_trace = 0;
b128c09f 2071 pio->io_error = 0;
1c27024e 2072 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
d164b209 2073 pio->io_state[w] = 0;
1c27024e 2074 for (int c = 0; c < ZIO_CHILD_TYPES; c++)
b128c09f 2075 pio->io_child_error[c] = 0;
34dc7c2f 2076
428870ff
BB
2077 if (IO_IS_ALLOCATING(pio))
2078 BP_ZERO(pio->io_bp);
34dc7c2f 2079
b128c09f
BB
2080 /*
2081 * As we reexecute pio's children, new children could be created.
d164b209 2082 * New children go to the head of pio's io_child_list, however,
b128c09f 2083 * so we will (correctly) not reexecute them. The key is that
d164b209
BB
2084 * the remainder of pio's io_child_list, from 'cio_next' onward,
2085 * cannot be affected by any side effects of reexecuting 'cio'.
b128c09f 2086 */
1c27024e 2087 zio_link_t *zl = NULL;
a8b2e306 2088 mutex_enter(&pio->io_lock);
3dfb57a3
DB
2089 for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) {
2090 cio_next = zio_walk_children(pio, &zl);
1c27024e 2091 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
d164b209 2092 pio->io_children[cio->io_child_type][w]++;
b128c09f 2093 mutex_exit(&pio->io_lock);
d164b209 2094 zio_reexecute(cio);
a8b2e306 2095 mutex_enter(&pio->io_lock);
34dc7c2f 2096 }
a8b2e306 2097 mutex_exit(&pio->io_lock);
34dc7c2f 2098
b128c09f
BB
2099 /*
2100 * Now that all children have been reexecuted, execute the parent.
9babb374 2101 * We don't reexecute "The Godfather" I/O here as it's the
9e2c3bb4 2102 * responsibility of the caller to wait on it.
b128c09f 2103 */
3dfb57a3
DB
2104 if (!(pio->io_flags & ZIO_FLAG_GODFATHER)) {
2105 pio->io_queued_timestamp = gethrtime();
da6b4005 2106 __zio_execute(pio);
3dfb57a3 2107 }
34dc7c2f
BB
2108}
2109
b128c09f 2110void
cec3a0a1 2111zio_suspend(spa_t *spa, zio_t *zio, zio_suspend_reason_t reason)
34dc7c2f 2112{
b128c09f
BB
2113 if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_PANIC)
2114 fm_panic("Pool '%s' has encountered an uncorrectable I/O "
2115 "failure and the failure mode property for this pool "
2116 "is set to panic.", spa_name(spa));
34dc7c2f 2117
bf89c199
BB
2118 cmn_err(CE_WARN, "Pool '%s' has encountered an uncorrectable I/O "
2119 "failure and has been suspended.\n", spa_name(spa));
2120
b5256303
TC
2121 zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE, spa, NULL,
2122 NULL, NULL, 0, 0);
34dc7c2f 2123
b128c09f 2124 mutex_enter(&spa->spa_suspend_lock);
34dc7c2f 2125
b128c09f 2126 if (spa->spa_suspend_zio_root == NULL)
9babb374
BB
2127 spa->spa_suspend_zio_root = zio_root(spa, NULL, NULL,
2128 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
2129 ZIO_FLAG_GODFATHER);
34dc7c2f 2130
cec3a0a1 2131 spa->spa_suspended = reason;
34dc7c2f 2132
b128c09f 2133 if (zio != NULL) {
9babb374 2134 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
b128c09f
BB
2135 ASSERT(zio != spa->spa_suspend_zio_root);
2136 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
d164b209 2137 ASSERT(zio_unique_parent(zio) == NULL);
b128c09f
BB
2138 ASSERT(zio->io_stage == ZIO_STAGE_DONE);
2139 zio_add_child(spa->spa_suspend_zio_root, zio);
2140 }
34dc7c2f 2141
b128c09f
BB
2142 mutex_exit(&spa->spa_suspend_lock);
2143}
34dc7c2f 2144
9babb374 2145int
b128c09f
BB
2146zio_resume(spa_t *spa)
2147{
9babb374 2148 zio_t *pio;
34dc7c2f
BB
2149
2150 /*
b128c09f 2151 * Reexecute all previously suspended i/o.
34dc7c2f 2152 */
b128c09f 2153 mutex_enter(&spa->spa_suspend_lock);
cec3a0a1 2154 spa->spa_suspended = ZIO_SUSPEND_NONE;
b128c09f
BB
2155 cv_broadcast(&spa->spa_suspend_cv);
2156 pio = spa->spa_suspend_zio_root;
2157 spa->spa_suspend_zio_root = NULL;
2158 mutex_exit(&spa->spa_suspend_lock);
2159
2160 if (pio == NULL)
9babb374 2161 return (0);
34dc7c2f 2162
9babb374
BB
2163 zio_reexecute(pio);
2164 return (zio_wait(pio));
b128c09f
BB
2165}
2166
2167void
2168zio_resume_wait(spa_t *spa)
2169{
2170 mutex_enter(&spa->spa_suspend_lock);
2171 while (spa_suspended(spa))
2172 cv_wait(&spa->spa_suspend_cv, &spa->spa_suspend_lock);
2173 mutex_exit(&spa->spa_suspend_lock);
34dc7c2f
BB
2174}
2175
2176/*
2177 * ==========================================================================
b128c09f
BB
2178 * Gang blocks.
2179 *
2180 * A gang block is a collection of small blocks that looks to the DMU
2181 * like one large block. When zio_dva_allocate() cannot find a block
2182 * of the requested size, due to either severe fragmentation or the pool
2183 * being nearly full, it calls zio_write_gang_block() to construct the
2184 * block from smaller fragments.
2185 *
2186 * A gang block consists of a gang header (zio_gbh_phys_t) and up to
2187 * three (SPA_GBH_NBLKPTRS) gang members. The gang header is just like
2188 * an indirect block: it's an array of block pointers. It consumes
2189 * only one sector and hence is allocatable regardless of fragmentation.
2190 * The gang header's bps point to its gang members, which hold the data.
2191 *
2192 * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg>
2193 * as the verifier to ensure uniqueness of the SHA256 checksum.
2194 * Critically, the gang block bp's blk_cksum is the checksum of the data,
2195 * not the gang header. This ensures that data block signatures (needed for
2196 * deduplication) are independent of how the block is physically stored.
2197 *
2198 * Gang blocks can be nested: a gang member may itself be a gang block.
2199 * Thus every gang block is a tree in which root and all interior nodes are
2200 * gang headers, and the leaves are normal blocks that contain user data.
2201 * The root of the gang tree is called the gang leader.
2202 *
2203 * To perform any operation (read, rewrite, free, claim) on a gang block,
2204 * zio_gang_assemble() first assembles the gang tree (minus data leaves)
2205 * in the io_gang_tree field of the original logical i/o by recursively
2206 * reading the gang leader and all gang headers below it. This yields
2207 * an in-core tree containing the contents of every gang header and the
2208 * bps for every constituent of the gang block.
2209 *
2210 * With the gang tree now assembled, zio_gang_issue() just walks the gang tree
2211 * and invokes a callback on each bp. To free a gang block, zio_gang_issue()
2212 * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp.
2213 * zio_claim_gang() provides a similarly trivial wrapper for zio_claim().
2214 * zio_read_gang() is a wrapper around zio_read() that omits reading gang
2215 * headers, since we already have those in io_gang_tree. zio_rewrite_gang()
2216 * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite()
2217 * of the gang header plus zio_checksum_compute() of the data to update the
2218 * gang header's blk_cksum as described above.
2219 *
2220 * The two-phase assemble/issue model solves the problem of partial failure --
2221 * what if you'd freed part of a gang block but then couldn't read the
2222 * gang header for another part? Assembling the entire gang tree first
2223 * ensures that all the necessary gang header I/O has succeeded before
2224 * starting the actual work of free, claim, or write. Once the gang tree
2225 * is assembled, free and claim are in-memory operations that cannot fail.
2226 *
2227 * In the event that a gang write fails, zio_dva_unallocate() walks the
2228 * gang tree to immediately free (i.e. insert back into the space map)
2229 * everything we've allocated. This ensures that we don't get ENOSPC
2230 * errors during repeated suspend/resume cycles due to a flaky device.
2231 *
2232 * Gang rewrites only happen during sync-to-convergence. If we can't assemble
2233 * the gang tree, we won't modify the block, so we can safely defer the free
2234 * (knowing that the block is still intact). If we *can* assemble the gang
2235 * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free
2236 * each constituent bp and we can allocate a new block on the next sync pass.
2237 *
2238 * In all cases, the gang tree allows complete recovery from partial failure.
34dc7c2f
BB
2239 * ==========================================================================
2240 */
b128c09f 2241
a6255b7f
DQ
2242static void
2243zio_gang_issue_func_done(zio_t *zio)
2244{
2245 abd_put(zio->io_abd);
2246}
2247
b128c09f 2248static zio_t *
a6255b7f
DQ
2249zio_read_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
2250 uint64_t offset)
34dc7c2f 2251{
b128c09f
BB
2252 if (gn != NULL)
2253 return (pio);
34dc7c2f 2254
a6255b7f
DQ
2255 return (zio_read(pio, pio->io_spa, bp, abd_get_offset(data, offset),
2256 BP_GET_PSIZE(bp), zio_gang_issue_func_done,
2257 NULL, pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
b128c09f
BB
2258 &pio->io_bookmark));
2259}
2260
a6255b7f
DQ
2261static zio_t *
2262zio_rewrite_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
2263 uint64_t offset)
b128c09f
BB
2264{
2265 zio_t *zio;
2266
2267 if (gn != NULL) {
a6255b7f
DQ
2268 abd_t *gbh_abd =
2269 abd_get_from_buf(gn->gn_gbh, SPA_GANGBLOCKSIZE);
b128c09f 2270 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
a6255b7f
DQ
2271 gbh_abd, SPA_GANGBLOCKSIZE, zio_gang_issue_func_done, NULL,
2272 pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
2273 &pio->io_bookmark);
34dc7c2f 2274 /*
b128c09f
BB
2275 * As we rewrite each gang header, the pipeline will compute
2276 * a new gang block header checksum for it; but no one will
2277 * compute a new data checksum, so we do that here. The one
2278 * exception is the gang leader: the pipeline already computed
2279 * its data checksum because that stage precedes gang assembly.
2280 * (Presently, nothing actually uses interior data checksums;
2281 * this is just good hygiene.)
34dc7c2f 2282 */
9babb374 2283 if (gn != pio->io_gang_leader->io_gang_tree) {
a6255b7f
DQ
2284 abd_t *buf = abd_get_offset(data, offset);
2285
b128c09f 2286 zio_checksum_compute(zio, BP_GET_CHECKSUM(bp),
a6255b7f
DQ
2287 buf, BP_GET_PSIZE(bp));
2288
2289 abd_put(buf);
b128c09f 2290 }
428870ff
BB
2291 /*
2292 * If we are here to damage data for testing purposes,
2293 * leave the GBH alone so that we can detect the damage.
2294 */
2295 if (pio->io_gang_leader->io_flags & ZIO_FLAG_INDUCE_DAMAGE)
2296 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
34dc7c2f 2297 } else {
b128c09f 2298 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
a6255b7f
DQ
2299 abd_get_offset(data, offset), BP_GET_PSIZE(bp),
2300 zio_gang_issue_func_done, NULL, pio->io_priority,
b128c09f 2301 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
34dc7c2f
BB
2302 }
2303
b128c09f
BB
2304 return (zio);
2305}
34dc7c2f 2306
b128c09f 2307/* ARGSUSED */
a6255b7f
DQ
2308static zio_t *
2309zio_free_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
2310 uint64_t offset)
b128c09f 2311{
428870ff
BB
2312 return (zio_free_sync(pio, pio->io_spa, pio->io_txg, bp,
2313 ZIO_GANG_CHILD_FLAGS(pio)));
34dc7c2f
BB
2314}
2315
b128c09f 2316/* ARGSUSED */
a6255b7f
DQ
2317static zio_t *
2318zio_claim_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
2319 uint64_t offset)
34dc7c2f 2320{
b128c09f
BB
2321 return (zio_claim(pio, pio->io_spa, pio->io_txg, bp,
2322 NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio)));
2323}
2324
2325static zio_gang_issue_func_t *zio_gang_issue_func[ZIO_TYPES] = {
2326 NULL,
2327 zio_read_gang,
2328 zio_rewrite_gang,
2329 zio_free_gang,
2330 zio_claim_gang,
2331 NULL
2332};
34dc7c2f 2333
b128c09f 2334static void zio_gang_tree_assemble_done(zio_t *zio);
34dc7c2f 2335
b128c09f
BB
2336static zio_gang_node_t *
2337zio_gang_node_alloc(zio_gang_node_t **gnpp)
2338{
2339 zio_gang_node_t *gn;
34dc7c2f 2340
b128c09f 2341 ASSERT(*gnpp == NULL);
34dc7c2f 2342
79c76d5b 2343 gn = kmem_zalloc(sizeof (*gn), KM_SLEEP);
b128c09f
BB
2344 gn->gn_gbh = zio_buf_alloc(SPA_GANGBLOCKSIZE);
2345 *gnpp = gn;
34dc7c2f 2346
b128c09f 2347 return (gn);
34dc7c2f
BB
2348}
2349
34dc7c2f 2350static void
b128c09f 2351zio_gang_node_free(zio_gang_node_t **gnpp)
34dc7c2f 2352{
b128c09f 2353 zio_gang_node_t *gn = *gnpp;
34dc7c2f 2354
1c27024e 2355 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
b128c09f
BB
2356 ASSERT(gn->gn_child[g] == NULL);
2357
2358 zio_buf_free(gn->gn_gbh, SPA_GANGBLOCKSIZE);
2359 kmem_free(gn, sizeof (*gn));
2360 *gnpp = NULL;
34dc7c2f
BB
2361}
2362
b128c09f
BB
2363static void
2364zio_gang_tree_free(zio_gang_node_t **gnpp)
34dc7c2f 2365{
b128c09f 2366 zio_gang_node_t *gn = *gnpp;
34dc7c2f 2367
b128c09f
BB
2368 if (gn == NULL)
2369 return;
34dc7c2f 2370
1c27024e 2371 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
b128c09f 2372 zio_gang_tree_free(&gn->gn_child[g]);
34dc7c2f 2373
b128c09f 2374 zio_gang_node_free(gnpp);
34dc7c2f
BB
2375}
2376
b128c09f 2377static void
9babb374 2378zio_gang_tree_assemble(zio_t *gio, blkptr_t *bp, zio_gang_node_t **gnpp)
34dc7c2f 2379{
b128c09f 2380 zio_gang_node_t *gn = zio_gang_node_alloc(gnpp);
a6255b7f 2381 abd_t *gbh_abd = abd_get_from_buf(gn->gn_gbh, SPA_GANGBLOCKSIZE);
b128c09f 2382
9babb374 2383 ASSERT(gio->io_gang_leader == gio);
b128c09f 2384 ASSERT(BP_IS_GANG(bp));
34dc7c2f 2385
a6255b7f
DQ
2386 zio_nowait(zio_read(gio, gio->io_spa, bp, gbh_abd, SPA_GANGBLOCKSIZE,
2387 zio_gang_tree_assemble_done, gn, gio->io_priority,
2388 ZIO_GANG_CHILD_FLAGS(gio), &gio->io_bookmark));
b128c09f 2389}
34dc7c2f 2390
b128c09f
BB
2391static void
2392zio_gang_tree_assemble_done(zio_t *zio)
2393{
9babb374 2394 zio_t *gio = zio->io_gang_leader;
b128c09f
BB
2395 zio_gang_node_t *gn = zio->io_private;
2396 blkptr_t *bp = zio->io_bp;
34dc7c2f 2397
9babb374 2398 ASSERT(gio == zio_unique_parent(zio));
428870ff 2399 ASSERT(zio->io_child_count == 0);
34dc7c2f 2400
b128c09f
BB
2401 if (zio->io_error)
2402 return;
34dc7c2f 2403
a6255b7f 2404 /* this ABD was created from a linear buf in zio_gang_tree_assemble */
b128c09f 2405 if (BP_SHOULD_BYTESWAP(bp))
a6255b7f 2406 byteswap_uint64_array(abd_to_buf(zio->io_abd), zio->io_size);
34dc7c2f 2407
a6255b7f 2408 ASSERT3P(abd_to_buf(zio->io_abd), ==, gn->gn_gbh);
b128c09f 2409 ASSERT(zio->io_size == SPA_GANGBLOCKSIZE);
428870ff 2410 ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
34dc7c2f 2411
a6255b7f
DQ
2412 abd_put(zio->io_abd);
2413
1c27024e 2414 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
b128c09f
BB
2415 blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
2416 if (!BP_IS_GANG(gbp))
2417 continue;
9babb374 2418 zio_gang_tree_assemble(gio, gbp, &gn->gn_child[g]);
b128c09f 2419 }
34dc7c2f
BB
2420}
2421
b128c09f 2422static void
a6255b7f
DQ
2423zio_gang_tree_issue(zio_t *pio, zio_gang_node_t *gn, blkptr_t *bp, abd_t *data,
2424 uint64_t offset)
34dc7c2f 2425{
9babb374 2426 zio_t *gio = pio->io_gang_leader;
b128c09f 2427 zio_t *zio;
34dc7c2f 2428
b128c09f 2429 ASSERT(BP_IS_GANG(bp) == !!gn);
9babb374
BB
2430 ASSERT(BP_GET_CHECKSUM(bp) == BP_GET_CHECKSUM(gio->io_bp));
2431 ASSERT(BP_GET_LSIZE(bp) == BP_GET_PSIZE(bp) || gn == gio->io_gang_tree);
34dc7c2f 2432
b128c09f
BB
2433 /*
2434 * If you're a gang header, your data is in gn->gn_gbh.
2435 * If you're a gang member, your data is in 'data' and gn == NULL.
2436 */
a6255b7f 2437 zio = zio_gang_issue_func[gio->io_type](pio, bp, gn, data, offset);
34dc7c2f 2438
b128c09f 2439 if (gn != NULL) {
428870ff 2440 ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
34dc7c2f 2441
1c27024e 2442 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
b128c09f
BB
2443 blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
2444 if (BP_IS_HOLE(gbp))
2445 continue;
a6255b7f
DQ
2446 zio_gang_tree_issue(zio, gn->gn_child[g], gbp, data,
2447 offset);
2448 offset += BP_GET_PSIZE(gbp);
b128c09f 2449 }
34dc7c2f
BB
2450 }
2451
9babb374 2452 if (gn == gio->io_gang_tree)
a6255b7f 2453 ASSERT3U(gio->io_size, ==, offset);
34dc7c2f 2454
b128c09f
BB
2455 if (zio != pio)
2456 zio_nowait(zio);
34dc7c2f
BB
2457}
2458
2459static int
b128c09f 2460zio_gang_assemble(zio_t *zio)
34dc7c2f 2461{
b128c09f 2462 blkptr_t *bp = zio->io_bp;
34dc7c2f 2463
9babb374
BB
2464 ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == NULL);
2465 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2466
2467 zio->io_gang_leader = zio;
34dc7c2f 2468
b128c09f 2469 zio_gang_tree_assemble(zio, bp, &zio->io_gang_tree);
34dc7c2f
BB
2470
2471 return (ZIO_PIPELINE_CONTINUE);
2472}
2473
2474static int
b128c09f 2475zio_gang_issue(zio_t *zio)
34dc7c2f 2476{
b128c09f 2477 blkptr_t *bp = zio->io_bp;
34dc7c2f 2478
ddc751d5 2479 if (zio_wait_for_children(zio, ZIO_CHILD_GANG_BIT, ZIO_WAIT_DONE)) {
b128c09f 2480 return (ZIO_PIPELINE_STOP);
ddc751d5 2481 }
34dc7c2f 2482
9babb374
BB
2483 ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == zio);
2484 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
34dc7c2f 2485
b128c09f 2486 if (zio->io_child_error[ZIO_CHILD_GANG] == 0)
a6255b7f
DQ
2487 zio_gang_tree_issue(zio, zio->io_gang_tree, bp, zio->io_abd,
2488 0);
b128c09f 2489 else
9babb374 2490 zio_gang_tree_free(&zio->io_gang_tree);
34dc7c2f 2491
b128c09f 2492 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
34dc7c2f
BB
2493
2494 return (ZIO_PIPELINE_CONTINUE);
2495}
2496
2497static void
b128c09f 2498zio_write_gang_member_ready(zio_t *zio)
34dc7c2f 2499{
d164b209 2500 zio_t *pio = zio_unique_parent(zio);
34dc7c2f
BB
2501 dva_t *cdva = zio->io_bp->blk_dva;
2502 dva_t *pdva = pio->io_bp->blk_dva;
2503 uint64_t asize;
d1d7e268 2504 ASSERTV(zio_t *gio = zio->io_gang_leader);
34dc7c2f 2505
b128c09f
BB
2506 if (BP_IS_HOLE(zio->io_bp))
2507 return;
2508
2509 ASSERT(BP_IS_HOLE(&zio->io_bp_orig));
2510
2511 ASSERT(zio->io_child_type == ZIO_CHILD_GANG);
428870ff
BB
2512 ASSERT3U(zio->io_prop.zp_copies, ==, gio->io_prop.zp_copies);
2513 ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(zio->io_bp));
2514 ASSERT3U(pio->io_prop.zp_copies, <=, BP_GET_NDVAS(pio->io_bp));
34dc7c2f 2515 ASSERT3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp));
34dc7c2f
BB
2516
2517 mutex_enter(&pio->io_lock);
1c27024e 2518 for (int d = 0; d < BP_GET_NDVAS(zio->io_bp); d++) {
34dc7c2f
BB
2519 ASSERT(DVA_GET_GANG(&pdva[d]));
2520 asize = DVA_GET_ASIZE(&pdva[d]);
2521 asize += DVA_GET_ASIZE(&cdva[d]);
2522 DVA_SET_ASIZE(&pdva[d], asize);
2523 }
2524 mutex_exit(&pio->io_lock);
2525}
2526
a6255b7f
DQ
2527static void
2528zio_write_gang_done(zio_t *zio)
2529{
2530 abd_put(zio->io_abd);
2531}
2532
34dc7c2f 2533static int
b128c09f 2534zio_write_gang_block(zio_t *pio)
34dc7c2f 2535{
b128c09f 2536 spa_t *spa = pio->io_spa;
3dfb57a3 2537 metaslab_class_t *mc = spa_normal_class(spa);
b128c09f 2538 blkptr_t *bp = pio->io_bp;
9babb374 2539 zio_t *gio = pio->io_gang_leader;
b128c09f
BB
2540 zio_t *zio;
2541 zio_gang_node_t *gn, **gnpp;
34dc7c2f 2542 zio_gbh_phys_t *gbh;
a6255b7f 2543 abd_t *gbh_abd;
b128c09f
BB
2544 uint64_t txg = pio->io_txg;
2545 uint64_t resid = pio->io_size;
2546 uint64_t lsize;
428870ff 2547 int copies = gio->io_prop.zp_copies;
b5256303 2548 int gbh_copies;
b128c09f 2549 zio_prop_t zp;
1c27024e 2550 int error;
b5256303
TC
2551
2552 /*
2553 * encrypted blocks need DVA[2] free so encrypted gang headers can't
2554 * have a third copy.
2555 */
2556 gbh_copies = MIN(copies + 1, spa_max_replication(spa));
2557 if (gio->io_prop.zp_encrypt && gbh_copies >= SPA_DVAS_PER_BP)
2558 gbh_copies = SPA_DVAS_PER_BP - 1;
2559
1c27024e 2560 int flags = METASLAB_HINTBP_FAVOR | METASLAB_GANG_HEADER;
3dfb57a3
DB
2561 if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
2562 ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
2563 ASSERT(!(pio->io_flags & ZIO_FLAG_NODATA));
2564
2565 flags |= METASLAB_ASYNC_ALLOC;
2566 VERIFY(refcount_held(&mc->mc_alloc_slots, pio));
2567
2568 /*
2569 * The logical zio has already placed a reservation for
2570 * 'copies' allocation slots but gang blocks may require
2571 * additional copies. These additional copies
2572 * (i.e. gbh_copies - copies) are guaranteed to succeed
2573 * since metaslab_class_throttle_reserve() always allows
2574 * additional reservations for gang blocks.
2575 */
2576 VERIFY(metaslab_class_throttle_reserve(mc, gbh_copies - copies,
2577 pio, flags));
2578 }
2579
2580 error = metaslab_alloc(spa, mc, SPA_GANGBLOCKSIZE,
4e21fd06
DB
2581 bp, gbh_copies, txg, pio == gio ? NULL : gio->io_bp, flags,
2582 &pio->io_alloc_list, pio);
34dc7c2f 2583 if (error) {
3dfb57a3
DB
2584 if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
2585 ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
2586 ASSERT(!(pio->io_flags & ZIO_FLAG_NODATA));
2587
2588 /*
2589 * If we failed to allocate the gang block header then
2590 * we remove any additional allocation reservations that
2591 * we placed here. The original reservation will
2592 * be removed when the logical I/O goes to the ready
2593 * stage.
2594 */
2595 metaslab_class_throttle_unreserve(mc,
2596 gbh_copies - copies, pio);
2597 }
2598
b128c09f 2599 pio->io_error = error;
34dc7c2f
BB
2600 return (ZIO_PIPELINE_CONTINUE);
2601 }
2602
9babb374
BB
2603 if (pio == gio) {
2604 gnpp = &gio->io_gang_tree;
b128c09f
BB
2605 } else {
2606 gnpp = pio->io_private;
2607 ASSERT(pio->io_ready == zio_write_gang_member_ready);
34dc7c2f
BB
2608 }
2609
b128c09f
BB
2610 gn = zio_gang_node_alloc(gnpp);
2611 gbh = gn->gn_gbh;
2612 bzero(gbh, SPA_GANGBLOCKSIZE);
a6255b7f 2613 gbh_abd = abd_get_from_buf(gbh, SPA_GANGBLOCKSIZE);
34dc7c2f 2614
b128c09f
BB
2615 /*
2616 * Create the gang header.
2617 */
a6255b7f
DQ
2618 zio = zio_rewrite(pio, spa, txg, bp, gbh_abd, SPA_GANGBLOCKSIZE,
2619 zio_write_gang_done, NULL, pio->io_priority,
2620 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
34dc7c2f 2621
b128c09f
BB
2622 /*
2623 * Create and nowait the gang children.
2624 */
1c27024e 2625 for (int g = 0; resid != 0; resid -= lsize, g++) {
b128c09f
BB
2626 lsize = P2ROUNDUP(resid / (SPA_GBH_NBLKPTRS - g),
2627 SPA_MINBLOCKSIZE);
2628 ASSERT(lsize >= SPA_MINBLOCKSIZE && lsize <= resid);
2629
9babb374 2630 zp.zp_checksum = gio->io_prop.zp_checksum;
b128c09f
BB
2631 zp.zp_compress = ZIO_COMPRESS_OFF;
2632 zp.zp_type = DMU_OT_NONE;
2633 zp.zp_level = 0;
428870ff 2634 zp.zp_copies = gio->io_prop.zp_copies;
03c6040b
GW
2635 zp.zp_dedup = B_FALSE;
2636 zp.zp_dedup_verify = B_FALSE;
2637 zp.zp_nopwrite = B_FALSE;
4807c0ba
TC
2638 zp.zp_encrypt = gio->io_prop.zp_encrypt;
2639 zp.zp_byteorder = gio->io_prop.zp_byteorder;
b5256303
TC
2640 bzero(zp.zp_salt, ZIO_DATA_SALT_LEN);
2641 bzero(zp.zp_iv, ZIO_DATA_IV_LEN);
2642 bzero(zp.zp_mac, ZIO_DATA_MAC_LEN);
b128c09f 2643
1c27024e 2644 zio_t *cio = zio_write(zio, spa, txg, &gbh->zg_blkptr[g],
a6255b7f
DQ
2645 abd_get_offset(pio->io_abd, pio->io_size - resid), lsize,
2646 lsize, &zp, zio_write_gang_member_ready, NULL, NULL,
2647 zio_write_gang_done, &gn->gn_child[g], pio->io_priority,
3dfb57a3
DB
2648 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
2649
2650 if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
2651 ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
2652 ASSERT(!(pio->io_flags & ZIO_FLAG_NODATA));
2653
2654 /*
2655 * Gang children won't throttle but we should
2656 * account for their work, so reserve an allocation
2657 * slot for them here.
2658 */
2659 VERIFY(metaslab_class_throttle_reserve(mc,
2660 zp.zp_copies, cio, flags));
2661 }
2662 zio_nowait(cio);
b128c09f 2663 }
34dc7c2f
BB
2664
2665 /*
b128c09f 2666 * Set pio's pipeline to just wait for zio to finish.
34dc7c2f 2667 */
b128c09f
BB
2668 pio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2669
920dd524
ED
2670 /*
2671 * We didn't allocate this bp, so make sure it doesn't get unmarked.
2672 */
2673 pio->io_flags &= ~ZIO_FLAG_FASTWRITE;
2674
b128c09f
BB
2675 zio_nowait(zio);
2676
2677 return (ZIO_PIPELINE_CONTINUE);
34dc7c2f
BB
2678}
2679
03c6040b 2680/*
3c67d83a
TH
2681 * The zio_nop_write stage in the pipeline determines if allocating a
2682 * new bp is necessary. The nopwrite feature can handle writes in
2683 * either syncing or open context (i.e. zil writes) and as a result is
2684 * mutually exclusive with dedup.
2685 *
2686 * By leveraging a cryptographically secure checksum, such as SHA256, we
2687 * can compare the checksums of the new data and the old to determine if
2688 * allocating a new block is required. Note that our requirements for
2689 * cryptographic strength are fairly weak: there can't be any accidental
2690 * hash collisions, but we don't need to be secure against intentional
2691 * (malicious) collisions. To trigger a nopwrite, you have to be able
2692 * to write the file to begin with, and triggering an incorrect (hash
2693 * collision) nopwrite is no worse than simply writing to the file.
2694 * That said, there are no known attacks against the checksum algorithms
2695 * used for nopwrite, assuming that the salt and the checksums
2696 * themselves remain secret.
03c6040b
GW
2697 */
2698static int
2699zio_nop_write(zio_t *zio)
2700{
2701 blkptr_t *bp = zio->io_bp;
2702 blkptr_t *bp_orig = &zio->io_bp_orig;
2703 zio_prop_t *zp = &zio->io_prop;
2704
2705 ASSERT(BP_GET_LEVEL(bp) == 0);
2706 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
2707 ASSERT(zp->zp_nopwrite);
2708 ASSERT(!zp->zp_dedup);
2709 ASSERT(zio->io_bp_override == NULL);
2710 ASSERT(IO_IS_ALLOCATING(zio));
2711
2712 /*
2713 * Check to see if the original bp and the new bp have matching
2714 * characteristics (i.e. same checksum, compression algorithms, etc).
2715 * If they don't then just continue with the pipeline which will
2716 * allocate a new bp.
2717 */
2718 if (BP_IS_HOLE(bp_orig) ||
3c67d83a
TH
2719 !(zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_flags &
2720 ZCHECKSUM_FLAG_NOPWRITE) ||
b5256303 2721 BP_IS_ENCRYPTED(bp) || BP_IS_ENCRYPTED(bp_orig) ||
03c6040b
GW
2722 BP_GET_CHECKSUM(bp) != BP_GET_CHECKSUM(bp_orig) ||
2723 BP_GET_COMPRESS(bp) != BP_GET_COMPRESS(bp_orig) ||
2724 BP_GET_DEDUP(bp) != BP_GET_DEDUP(bp_orig) ||
2725 zp->zp_copies != BP_GET_NDVAS(bp_orig))
2726 return (ZIO_PIPELINE_CONTINUE);
2727
2728 /*
2729 * If the checksums match then reset the pipeline so that we
2730 * avoid allocating a new bp and issuing any I/O.
2731 */
2732 if (ZIO_CHECKSUM_EQUAL(bp->blk_cksum, bp_orig->blk_cksum)) {
3c67d83a
TH
2733 ASSERT(zio_checksum_table[zp->zp_checksum].ci_flags &
2734 ZCHECKSUM_FLAG_NOPWRITE);
03c6040b
GW
2735 ASSERT3U(BP_GET_PSIZE(bp), ==, BP_GET_PSIZE(bp_orig));
2736 ASSERT3U(BP_GET_LSIZE(bp), ==, BP_GET_LSIZE(bp_orig));
2737 ASSERT(zp->zp_compress != ZIO_COMPRESS_OFF);
2738 ASSERT(bcmp(&bp->blk_prop, &bp_orig->blk_prop,
2739 sizeof (uint64_t)) == 0);
2740
2741 *bp = *bp_orig;
2742 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2743 zio->io_flags |= ZIO_FLAG_NOPWRITE;
2744 }
2745
2746 return (ZIO_PIPELINE_CONTINUE);
2747}
2748
34dc7c2f
BB
2749/*
2750 * ==========================================================================
428870ff 2751 * Dedup
34dc7c2f
BB
2752 * ==========================================================================
2753 */
428870ff
BB
2754static void
2755zio_ddt_child_read_done(zio_t *zio)
2756{
2757 blkptr_t *bp = zio->io_bp;
2758 ddt_entry_t *dde = zio->io_private;
2759 ddt_phys_t *ddp;
2760 zio_t *pio = zio_unique_parent(zio);
2761
2762 mutex_enter(&pio->io_lock);
2763 ddp = ddt_phys_select(dde, bp);
2764 if (zio->io_error == 0)
2765 ddt_phys_clear(ddp); /* this ddp doesn't need repair */
a6255b7f
DQ
2766
2767 if (zio->io_error == 0 && dde->dde_repair_abd == NULL)
2768 dde->dde_repair_abd = zio->io_abd;
428870ff 2769 else
a6255b7f 2770 abd_free(zio->io_abd);
428870ff
BB
2771 mutex_exit(&pio->io_lock);
2772}
2773
2774static int
2775zio_ddt_read_start(zio_t *zio)
2776{
2777 blkptr_t *bp = zio->io_bp;
2778
2779 ASSERT(BP_GET_DEDUP(bp));
2780 ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
2781 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2782
2783 if (zio->io_child_error[ZIO_CHILD_DDT]) {
2784 ddt_t *ddt = ddt_select(zio->io_spa, bp);
2785 ddt_entry_t *dde = ddt_repair_start(ddt, bp);
2786 ddt_phys_t *ddp = dde->dde_phys;
2787 ddt_phys_t *ddp_self = ddt_phys_select(dde, bp);
2788 blkptr_t blk;
2789
2790 ASSERT(zio->io_vsd == NULL);
2791 zio->io_vsd = dde;
2792
2793 if (ddp_self == NULL)
2794 return (ZIO_PIPELINE_CONTINUE);
2795
1c27024e 2796 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
428870ff
BB
2797 if (ddp->ddp_phys_birth == 0 || ddp == ddp_self)
2798 continue;
2799 ddt_bp_create(ddt->ddt_checksum, &dde->dde_key, ddp,
2800 &blk);
2801 zio_nowait(zio_read(zio, zio->io_spa, &blk,
a6255b7f
DQ
2802 abd_alloc_for_io(zio->io_size, B_TRUE),
2803 zio->io_size, zio_ddt_child_read_done, dde,
2804 zio->io_priority, ZIO_DDT_CHILD_FLAGS(zio) |
2805 ZIO_FLAG_DONT_PROPAGATE, &zio->io_bookmark));
428870ff
BB
2806 }
2807 return (ZIO_PIPELINE_CONTINUE);
2808 }
2809
2810 zio_nowait(zio_read(zio, zio->io_spa, bp,
a6255b7f 2811 zio->io_abd, zio->io_size, NULL, NULL, zio->io_priority,
428870ff
BB
2812 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark));
2813
2814 return (ZIO_PIPELINE_CONTINUE);
2815}
2816
2817static int
2818zio_ddt_read_done(zio_t *zio)
2819{
2820 blkptr_t *bp = zio->io_bp;
2821
ddc751d5 2822 if (zio_wait_for_children(zio, ZIO_CHILD_DDT_BIT, ZIO_WAIT_DONE)) {
428870ff 2823 return (ZIO_PIPELINE_STOP);
ddc751d5 2824 }
428870ff
BB
2825
2826 ASSERT(BP_GET_DEDUP(bp));
2827 ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
2828 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2829
2830 if (zio->io_child_error[ZIO_CHILD_DDT]) {
2831 ddt_t *ddt = ddt_select(zio->io_spa, bp);
2832 ddt_entry_t *dde = zio->io_vsd;
2833 if (ddt == NULL) {
2834 ASSERT(spa_load_state(zio->io_spa) != SPA_LOAD_NONE);
2835 return (ZIO_PIPELINE_CONTINUE);
2836 }
2837 if (dde == NULL) {
2838 zio->io_stage = ZIO_STAGE_DDT_READ_START >> 1;
2839 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
2840 return (ZIO_PIPELINE_STOP);
2841 }
a6255b7f
DQ
2842 if (dde->dde_repair_abd != NULL) {
2843 abd_copy(zio->io_abd, dde->dde_repair_abd,
2844 zio->io_size);
428870ff
BB
2845 zio->io_child_error[ZIO_CHILD_DDT] = 0;
2846 }
2847 ddt_repair_done(ddt, dde);
2848 zio->io_vsd = NULL;
2849 }
2850
2851 ASSERT(zio->io_vsd == NULL);
2852
2853 return (ZIO_PIPELINE_CONTINUE);
2854}
2855
2856static boolean_t
2857zio_ddt_collision(zio_t *zio, ddt_t *ddt, ddt_entry_t *dde)
2858{
2859 spa_t *spa = zio->io_spa;
c17bcf83 2860 boolean_t do_raw = !!(zio->io_flags & ZIO_FLAG_RAW);
428870ff 2861
c17bcf83 2862 ASSERT(!(zio->io_bp_override && do_raw));
2aa34383 2863
428870ff
BB
2864 /*
2865 * Note: we compare the original data, not the transformed data,
2866 * because when zio->io_bp is an override bp, we will not have
2867 * pushed the I/O transforms. That's an important optimization
2868 * because otherwise we'd compress/encrypt all dmu_sync() data twice.
c17bcf83 2869 * However, we should never get a raw, override zio so in these
b5256303 2870 * cases we can compare the io_abd directly. This is useful because
c17bcf83
TC
2871 * it allows us to do dedup verification even if we don't have access
2872 * to the original data (for instance, if the encryption keys aren't
2873 * loaded).
428870ff 2874 */
c17bcf83 2875
1c27024e 2876 for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
428870ff
BB
2877 zio_t *lio = dde->dde_lead_zio[p];
2878
c17bcf83
TC
2879 if (lio != NULL && do_raw) {
2880 return (lio->io_size != zio->io_size ||
a6255b7f 2881 abd_cmp(zio->io_abd, lio->io_abd) != 0);
c17bcf83 2882 } else if (lio != NULL) {
428870ff 2883 return (lio->io_orig_size != zio->io_orig_size ||
a6255b7f 2884 abd_cmp(zio->io_orig_abd, lio->io_orig_abd) != 0);
428870ff
BB
2885 }
2886 }
2887
1c27024e 2888 for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
428870ff
BB
2889 ddt_phys_t *ddp = &dde->dde_phys[p];
2890
c17bcf83
TC
2891 if (ddp->ddp_phys_birth != 0 && do_raw) {
2892 blkptr_t blk = *zio->io_bp;
2893 uint64_t psize;
a6255b7f 2894 abd_t *tmpabd;
c17bcf83
TC
2895 int error;
2896
2897 ddt_bp_fill(ddp, &blk, ddp->ddp_phys_birth);
2898 psize = BP_GET_PSIZE(&blk);
2899
2900 if (psize != zio->io_size)
2901 return (B_TRUE);
2902
2903 ddt_exit(ddt);
2904
a6255b7f 2905 tmpabd = abd_alloc_for_io(psize, B_TRUE);
c17bcf83 2906
a6255b7f 2907 error = zio_wait(zio_read(NULL, spa, &blk, tmpabd,
c17bcf83
TC
2908 psize, NULL, NULL, ZIO_PRIORITY_SYNC_READ,
2909 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
2910 ZIO_FLAG_RAW, &zio->io_bookmark));
2911
2912 if (error == 0) {
a6255b7f 2913 if (abd_cmp(tmpabd, zio->io_abd) != 0)
c17bcf83
TC
2914 error = SET_ERROR(ENOENT);
2915 }
2916
a6255b7f 2917 abd_free(tmpabd);
c17bcf83
TC
2918 ddt_enter(ddt);
2919 return (error != 0);
2920 } else if (ddp->ddp_phys_birth != 0) {
428870ff 2921 arc_buf_t *abuf = NULL;
2a432414 2922 arc_flags_t aflags = ARC_FLAG_WAIT;
428870ff
BB
2923 blkptr_t blk = *zio->io_bp;
2924 int error;
2925
2926 ddt_bp_fill(ddp, &blk, ddp->ddp_phys_birth);
2927
c17bcf83
TC
2928 if (BP_GET_LSIZE(&blk) != zio->io_orig_size)
2929 return (B_TRUE);
2930
428870ff
BB
2931 ddt_exit(ddt);
2932
294f6806 2933 error = arc_read(NULL, spa, &blk,
428870ff
BB
2934 arc_getbuf_func, &abuf, ZIO_PRIORITY_SYNC_READ,
2935 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2936 &aflags, &zio->io_bookmark);
2937
2938 if (error == 0) {
a6255b7f 2939 if (abd_cmp_buf(zio->io_orig_abd, abuf->b_data,
428870ff 2940 zio->io_orig_size) != 0)
c17bcf83 2941 error = SET_ERROR(ENOENT);
d3c2ae1c 2942 arc_buf_destroy(abuf, &abuf);
428870ff
BB
2943 }
2944
2945 ddt_enter(ddt);
2946 return (error != 0);
2947 }
2948 }
2949
2950 return (B_FALSE);
2951}
2952
2953static void
2954zio_ddt_child_write_ready(zio_t *zio)
2955{
2956 int p = zio->io_prop.zp_copies;
2957 ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
2958 ddt_entry_t *dde = zio->io_private;
2959 ddt_phys_t *ddp = &dde->dde_phys[p];
2960 zio_t *pio;
2961
2962 if (zio->io_error)
2963 return;
2964
2965 ddt_enter(ddt);
2966
2967 ASSERT(dde->dde_lead_zio[p] == zio);
2968
2969 ddt_phys_fill(ddp, zio->io_bp);
2970
1c27024e 2971 zio_link_t *zl = NULL;
3dfb57a3 2972 while ((pio = zio_walk_parents(zio, &zl)) != NULL)
428870ff
BB
2973 ddt_bp_fill(ddp, pio->io_bp, zio->io_txg);
2974
2975 ddt_exit(ddt);
2976}
2977
2978static void
2979zio_ddt_child_write_done(zio_t *zio)
2980{
2981 int p = zio->io_prop.zp_copies;
2982 ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
2983 ddt_entry_t *dde = zio->io_private;
2984 ddt_phys_t *ddp = &dde->dde_phys[p];
2985
2986 ddt_enter(ddt);
2987
2988 ASSERT(ddp->ddp_refcnt == 0);
2989 ASSERT(dde->dde_lead_zio[p] == zio);
2990 dde->dde_lead_zio[p] = NULL;
2991
2992 if (zio->io_error == 0) {
3dfb57a3
DB
2993 zio_link_t *zl = NULL;
2994 while (zio_walk_parents(zio, &zl) != NULL)
428870ff
BB
2995 ddt_phys_addref(ddp);
2996 } else {
2997 ddt_phys_clear(ddp);
2998 }
2999
3000 ddt_exit(ddt);
3001}
3002
3003static void
3004zio_ddt_ditto_write_done(zio_t *zio)
3005{
3006 int p = DDT_PHYS_DITTO;
1c27024e 3007 ASSERTV(zio_prop_t *zp = &zio->io_prop);
428870ff
BB
3008 blkptr_t *bp = zio->io_bp;
3009 ddt_t *ddt = ddt_select(zio->io_spa, bp);
3010 ddt_entry_t *dde = zio->io_private;
3011 ddt_phys_t *ddp = &dde->dde_phys[p];
3012 ddt_key_t *ddk = &dde->dde_key;
3013
3014 ddt_enter(ddt);
3015
3016 ASSERT(ddp->ddp_refcnt == 0);
3017 ASSERT(dde->dde_lead_zio[p] == zio);
3018 dde->dde_lead_zio[p] = NULL;
3019
3020 if (zio->io_error == 0) {
3021 ASSERT(ZIO_CHECKSUM_EQUAL(bp->blk_cksum, ddk->ddk_cksum));
3022 ASSERT(zp->zp_copies < SPA_DVAS_PER_BP);
3023 ASSERT(zp->zp_copies == BP_GET_NDVAS(bp) - BP_IS_GANG(bp));
3024 if (ddp->ddp_phys_birth != 0)
3025 ddt_phys_free(ddt, ddk, ddp, zio->io_txg);
3026 ddt_phys_fill(ddp, bp);
3027 }
3028
3029 ddt_exit(ddt);
3030}
3031
3032static int
3033zio_ddt_write(zio_t *zio)
3034{
3035 spa_t *spa = zio->io_spa;
3036 blkptr_t *bp = zio->io_bp;
3037 uint64_t txg = zio->io_txg;
3038 zio_prop_t *zp = &zio->io_prop;
3039 int p = zp->zp_copies;
3040 int ditto_copies;
3041 zio_t *cio = NULL;
3042 zio_t *dio = NULL;
3043 ddt_t *ddt = ddt_select(spa, bp);
3044 ddt_entry_t *dde;
3045 ddt_phys_t *ddp;
3046
3047 ASSERT(BP_GET_DEDUP(bp));
3048 ASSERT(BP_GET_CHECKSUM(bp) == zp->zp_checksum);
3049 ASSERT(BP_IS_HOLE(bp) || zio->io_bp_override);
c17bcf83 3050 ASSERT(!(zio->io_bp_override && (zio->io_flags & ZIO_FLAG_RAW)));
428870ff
BB
3051
3052 ddt_enter(ddt);
3053 dde = ddt_lookup(ddt, bp, B_TRUE);
3054 ddp = &dde->dde_phys[p];
3055
3056 if (zp->zp_dedup_verify && zio_ddt_collision(zio, ddt, dde)) {
3057 /*
3058 * If we're using a weak checksum, upgrade to a strong checksum
3059 * and try again. If we're already using a strong checksum,
3060 * we can't resolve it, so just convert to an ordinary write.
3061 * (And automatically e-mail a paper to Nature?)
3062 */
3c67d83a
TH
3063 if (!(zio_checksum_table[zp->zp_checksum].ci_flags &
3064 ZCHECKSUM_FLAG_DEDUP)) {
428870ff
BB
3065 zp->zp_checksum = spa_dedup_checksum(spa);
3066 zio_pop_transforms(zio);
3067 zio->io_stage = ZIO_STAGE_OPEN;
3068 BP_ZERO(bp);
3069 } else {
03c6040b 3070 zp->zp_dedup = B_FALSE;
428870ff
BB
3071 }
3072 zio->io_pipeline = ZIO_WRITE_PIPELINE;
3073 ddt_exit(ddt);
3074 return (ZIO_PIPELINE_CONTINUE);
3075 }
3076
3077 ditto_copies = ddt_ditto_copies_needed(ddt, dde, ddp);
3078 ASSERT(ditto_copies < SPA_DVAS_PER_BP);
3079
3080 if (ditto_copies > ddt_ditto_copies_present(dde) &&
3081 dde->dde_lead_zio[DDT_PHYS_DITTO] == NULL) {
3082 zio_prop_t czp = *zp;
3083
3084 czp.zp_copies = ditto_copies;
3085
3086 /*
3087 * If we arrived here with an override bp, we won't have run
3088 * the transform stack, so we won't have the data we need to
3089 * generate a child i/o. So, toss the override bp and restart.
3090 * This is safe, because using the override bp is just an
3091 * optimization; and it's rare, so the cost doesn't matter.
3092 */
3093 if (zio->io_bp_override) {
3094 zio_pop_transforms(zio);
3095 zio->io_stage = ZIO_STAGE_OPEN;
3096 zio->io_pipeline = ZIO_WRITE_PIPELINE;
3097 zio->io_bp_override = NULL;
3098 BP_ZERO(bp);
3099 ddt_exit(ddt);
3100 return (ZIO_PIPELINE_CONTINUE);
3101 }
3102
a6255b7f 3103 dio = zio_write(zio, spa, txg, bp, zio->io_orig_abd,
2aa34383 3104 zio->io_orig_size, zio->io_orig_size, &czp, NULL, NULL,
bc77ba73 3105 NULL, zio_ddt_ditto_write_done, dde, zio->io_priority,
428870ff
BB
3106 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
3107
a6255b7f 3108 zio_push_transform(dio, zio->io_abd, zio->io_size, 0, NULL);
428870ff
BB
3109 dde->dde_lead_zio[DDT_PHYS_DITTO] = dio;
3110 }
3111
3112 if (ddp->ddp_phys_birth != 0 || dde->dde_lead_zio[p] != NULL) {
3113 if (ddp->ddp_phys_birth != 0)
3114 ddt_bp_fill(ddp, bp, txg);
3115 if (dde->dde_lead_zio[p] != NULL)
3116 zio_add_child(zio, dde->dde_lead_zio[p]);
3117 else
3118 ddt_phys_addref(ddp);
3119 } else if (zio->io_bp_override) {
3120 ASSERT(bp->blk_birth == txg);
3121 ASSERT(BP_EQUAL(bp, zio->io_bp_override));
3122 ddt_phys_fill(ddp, bp);
3123 ddt_phys_addref(ddp);
3124 } else {
a6255b7f 3125 cio = zio_write(zio, spa, txg, bp, zio->io_orig_abd,
2aa34383 3126 zio->io_orig_size, zio->io_orig_size, zp,
bc77ba73 3127 zio_ddt_child_write_ready, NULL, NULL,
428870ff
BB
3128 zio_ddt_child_write_done, dde, zio->io_priority,
3129 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
3130
a6255b7f 3131 zio_push_transform(cio, zio->io_abd, zio->io_size, 0, NULL);
428870ff
BB
3132 dde->dde_lead_zio[p] = cio;
3133 }
3134
3135 ddt_exit(ddt);
3136
3137 if (cio)
3138 zio_nowait(cio);
3139 if (dio)
3140 zio_nowait(dio);
3141
3142 return (ZIO_PIPELINE_CONTINUE);
3143}
3144
3145ddt_entry_t *freedde; /* for debugging */
b128c09f 3146
428870ff
BB
3147static int
3148zio_ddt_free(zio_t *zio)
3149{
3150 spa_t *spa = zio->io_spa;
3151 blkptr_t *bp = zio->io_bp;
3152 ddt_t *ddt = ddt_select(spa, bp);
3153 ddt_entry_t *dde;
3154 ddt_phys_t *ddp;
3155
3156 ASSERT(BP_GET_DEDUP(bp));
3157 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3158
3159 ddt_enter(ddt);
3160 freedde = dde = ddt_lookup(ddt, bp, B_TRUE);
5dc6af0e
BB
3161 if (dde) {
3162 ddp = ddt_phys_select(dde, bp);
3163 if (ddp)
3164 ddt_phys_decref(ddp);
3165 }
428870ff
BB
3166 ddt_exit(ddt);
3167
3168 return (ZIO_PIPELINE_CONTINUE);
3169}
3170
3171/*
3172 * ==========================================================================
3173 * Allocate and free blocks
3174 * ==========================================================================
3175 */
3dfb57a3
DB
3176
3177static zio_t *
3178zio_io_to_allocate(spa_t *spa)
3179{
3180 zio_t *zio;
3181
3182 ASSERT(MUTEX_HELD(&spa->spa_alloc_lock));
3183
3184 zio = avl_first(&spa->spa_alloc_tree);
3185 if (zio == NULL)
3186 return (NULL);
3187
3188 ASSERT(IO_IS_ALLOCATING(zio));
3189
3190 /*
3191 * Try to place a reservation for this zio. If we're unable to
3192 * reserve then we throttle.
3193 */
3194 if (!metaslab_class_throttle_reserve(spa_normal_class(spa),
3195 zio->io_prop.zp_copies, zio, 0)) {
3196 return (NULL);
3197 }
3198
3199 avl_remove(&spa->spa_alloc_tree, zio);
3200 ASSERT3U(zio->io_stage, <, ZIO_STAGE_DVA_ALLOCATE);
3201
3202 return (zio);
3203}
3204
3205static int
3206zio_dva_throttle(zio_t *zio)
3207{
3208 spa_t *spa = zio->io_spa;
3209 zio_t *nio;
3210
3211 if (zio->io_priority == ZIO_PRIORITY_SYNC_WRITE ||
3212 !spa_normal_class(zio->io_spa)->mc_alloc_throttle_enabled ||
3213 zio->io_child_type == ZIO_CHILD_GANG ||
3214 zio->io_flags & ZIO_FLAG_NODATA) {
3215 return (ZIO_PIPELINE_CONTINUE);
3216 }
3217
3218 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
3219
3220 ASSERT3U(zio->io_queued_timestamp, >, 0);
3221 ASSERT(zio->io_stage == ZIO_STAGE_DVA_THROTTLE);
3222
3223 mutex_enter(&spa->spa_alloc_lock);
3224
3225 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
3226 avl_add(&spa->spa_alloc_tree, zio);
3227
3228 nio = zio_io_to_allocate(zio->io_spa);
3229 mutex_exit(&spa->spa_alloc_lock);
3230
3231 if (nio == zio)
3232 return (ZIO_PIPELINE_CONTINUE);
3233
3234 if (nio != NULL) {
3dfb57a3
DB
3235 ASSERT(nio->io_stage == ZIO_STAGE_DVA_THROTTLE);
3236 /*
3237 * We are passing control to a new zio so make sure that
3238 * it is processed by a different thread. We do this to
3239 * avoid stack overflows that can occur when parents are
3240 * throttled and children are making progress. We allow
3241 * it to go to the head of the taskq since it's already
3242 * been waiting.
3243 */
3244 zio_taskq_dispatch(nio, ZIO_TASKQ_ISSUE, B_TRUE);
3245 }
3246 return (ZIO_PIPELINE_STOP);
3247}
3248
3249void
3250zio_allocate_dispatch(spa_t *spa)
3251{
3252 zio_t *zio;
3253
3254 mutex_enter(&spa->spa_alloc_lock);
3255 zio = zio_io_to_allocate(spa);
3256 mutex_exit(&spa->spa_alloc_lock);
3257 if (zio == NULL)
3258 return;
3259
3260 ASSERT3U(zio->io_stage, ==, ZIO_STAGE_DVA_THROTTLE);
3261 ASSERT0(zio->io_error);
3262 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_TRUE);
3263}
3264
34dc7c2f
BB
3265static int
3266zio_dva_allocate(zio_t *zio)
3267{
3268 spa_t *spa = zio->io_spa;
428870ff 3269 metaslab_class_t *mc = spa_normal_class(spa);
34dc7c2f
BB
3270 blkptr_t *bp = zio->io_bp;
3271 int error;
6d974228 3272 int flags = 0;
34dc7c2f 3273
9babb374
BB
3274 if (zio->io_gang_leader == NULL) {
3275 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
3276 zio->io_gang_leader = zio;
3277 }
3278
34dc7c2f 3279 ASSERT(BP_IS_HOLE(bp));
c99c9001 3280 ASSERT0(BP_GET_NDVAS(bp));
428870ff
BB
3281 ASSERT3U(zio->io_prop.zp_copies, >, 0);
3282 ASSERT3U(zio->io_prop.zp_copies, <=, spa_max_replication(spa));
34dc7c2f
BB
3283 ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
3284
920dd524 3285 flags |= (zio->io_flags & ZIO_FLAG_FASTWRITE) ? METASLAB_FASTWRITE : 0;
3dfb57a3
DB
3286 if (zio->io_flags & ZIO_FLAG_NODATA)
3287 flags |= METASLAB_DONT_THROTTLE;
3288 if (zio->io_flags & ZIO_FLAG_GANG_CHILD)
3289 flags |= METASLAB_GANG_CHILD;
3290 if (zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE)
3291 flags |= METASLAB_ASYNC_ALLOC;
3292
b128c09f 3293 error = metaslab_alloc(spa, mc, zio->io_size, bp,
4e21fd06
DB
3294 zio->io_prop.zp_copies, zio->io_txg, NULL, flags,
3295 &zio->io_alloc_list, zio);
34dc7c2f 3296
3dfb57a3 3297 if (error != 0) {
6d974228
GW
3298 spa_dbgmsg(spa, "%s: metaslab allocation failure: zio %p, "
3299 "size %llu, error %d", spa_name(spa), zio, zio->io_size,
3300 error);
b128c09f
BB
3301 if (error == ENOSPC && zio->io_size > SPA_MINBLOCKSIZE)
3302 return (zio_write_gang_block(zio));
34dc7c2f
BB
3303 zio->io_error = error;
3304 }
3305
3306 return (ZIO_PIPELINE_CONTINUE);
3307}
3308
3309static int
3310zio_dva_free(zio_t *zio)
3311{
b128c09f 3312 metaslab_free(zio->io_spa, zio->io_bp, zio->io_txg, B_FALSE);
34dc7c2f
BB
3313
3314 return (ZIO_PIPELINE_CONTINUE);
3315}
3316
3317static int
3318zio_dva_claim(zio_t *zio)
3319{
b128c09f
BB
3320 int error;
3321
3322 error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg);
3323 if (error)
3324 zio->io_error = error;
34dc7c2f
BB
3325
3326 return (ZIO_PIPELINE_CONTINUE);
3327}
3328
b128c09f
BB
3329/*
3330 * Undo an allocation. This is used by zio_done() when an I/O fails
3331 * and we want to give back the block we just allocated.
3332 * This handles both normal blocks and gang blocks.
3333 */
3334static void
3335zio_dva_unallocate(zio_t *zio, zio_gang_node_t *gn, blkptr_t *bp)
3336{
b128c09f 3337 ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp));
428870ff 3338 ASSERT(zio->io_bp_override == NULL);
b128c09f
BB
3339
3340 if (!BP_IS_HOLE(bp))
428870ff 3341 metaslab_free(zio->io_spa, bp, bp->blk_birth, B_TRUE);
b128c09f
BB
3342
3343 if (gn != NULL) {
1c27024e 3344 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
b128c09f
BB
3345 zio_dva_unallocate(zio, gn->gn_child[g],
3346 &gn->gn_gbh->zg_blkptr[g]);
3347 }
3348 }
3349}
3350
3351/*
3352 * Try to allocate an intent log block. Return 0 on success, errno on failure.
3353 */
3354int
b5256303
TC
3355zio_alloc_zil(spa_t *spa, objset_t *os, uint64_t txg, blkptr_t *new_bp,
3356 uint64_t size, boolean_t *slog)
b128c09f 3357{
428870ff 3358 int error = 1;
4e21fd06 3359 zio_alloc_list_t io_alloc_list;
b128c09f 3360
428870ff
BB
3361 ASSERT(txg > spa_syncing_txg(spa));
3362
4e21fd06 3363 metaslab_trace_init(&io_alloc_list);
1b7c1e5c
GDN
3364 error = metaslab_alloc(spa, spa_log_class(spa), size, new_bp, 1,
3365 txg, NULL, METASLAB_FASTWRITE, &io_alloc_list, NULL);
3366 if (error == 0) {
3367 *slog = TRUE;
3368 } else {
428870ff 3369 error = metaslab_alloc(spa, spa_normal_class(spa), size,
4e21fd06
DB
3370 new_bp, 1, txg, NULL, METASLAB_FASTWRITE,
3371 &io_alloc_list, NULL);
1b7c1e5c
GDN
3372 if (error == 0)
3373 *slog = FALSE;
ebf8e3a2 3374 }
4e21fd06 3375 metaslab_trace_fini(&io_alloc_list);
b128c09f
BB
3376
3377 if (error == 0) {
3378 BP_SET_LSIZE(new_bp, size);
3379 BP_SET_PSIZE(new_bp, size);
3380 BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF);
428870ff
BB
3381 BP_SET_CHECKSUM(new_bp,
3382 spa_version(spa) >= SPA_VERSION_SLIM_ZIL
3383 ? ZIO_CHECKSUM_ZILOG2 : ZIO_CHECKSUM_ZILOG);
b128c09f
BB
3384 BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
3385 BP_SET_LEVEL(new_bp, 0);
428870ff 3386 BP_SET_DEDUP(new_bp, 0);
b128c09f 3387 BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER);
b5256303
TC
3388
3389 /*
3390 * encrypted blocks will require an IV and salt. We generate
3391 * these now since we will not be rewriting the bp at
3392 * rewrite time.
3393 */
3394 if (os->os_encrypted) {
3395 uint8_t iv[ZIO_DATA_IV_LEN];
3396 uint8_t salt[ZIO_DATA_SALT_LEN];
3397
3398 BP_SET_CRYPT(new_bp, B_TRUE);
3399 VERIFY0(spa_crypt_get_salt(spa,
3400 dmu_objset_id(os), salt));
3401 VERIFY0(zio_crypt_generate_iv(iv));
3402
3403 zio_crypt_encode_params_bp(new_bp, salt, iv);
3404 }
1ce23dca
PS
3405 } else {
3406 zfs_dbgmsg("%s: zil block allocation failure: "
3407 "size %llu, error %d", spa_name(spa), size, error);
b128c09f
BB
3408 }
3409
3410 return (error);
3411}
3412
3413/*
428870ff 3414 * Free an intent log block.
b128c09f
BB
3415 */
3416void
428870ff 3417zio_free_zil(spa_t *spa, uint64_t txg, blkptr_t *bp)
b128c09f 3418{
428870ff 3419 ASSERT(BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG);
b128c09f
BB
3420 ASSERT(!BP_IS_GANG(bp));
3421
428870ff 3422 zio_free(spa, txg, bp);
b128c09f
BB
3423}
3424
34dc7c2f
BB
3425/*
3426 * ==========================================================================
3427 * Read and write to physical devices
3428 * ==========================================================================
3429 */
98b25418
GW
3430
3431
3432/*
3433 * Issue an I/O to the underlying vdev. Typically the issue pipeline
3434 * stops after this stage and will resume upon I/O completion.
3435 * However, there are instances where the vdev layer may need to
3436 * continue the pipeline when an I/O was not issued. Since the I/O
3437 * that was sent to the vdev layer might be different than the one
3438 * currently active in the pipeline (see vdev_queue_io()), we explicitly
3439 * force the underlying vdev layers to call either zio_execute() or
3440 * zio_interrupt() to ensure that the pipeline continues with the correct I/O.
3441 */
34dc7c2f
BB
3442static int
3443zio_vdev_io_start(zio_t *zio)
3444{
3445 vdev_t *vd = zio->io_vd;
34dc7c2f
BB
3446 uint64_t align;
3447 spa_t *spa = zio->io_spa;
3448
193a37cb
TH
3449 zio->io_delay = 0;
3450
b128c09f
BB
3451 ASSERT(zio->io_error == 0);
3452 ASSERT(zio->io_child_error[ZIO_CHILD_VDEV] == 0);
34dc7c2f 3453
b128c09f
BB
3454 if (vd == NULL) {
3455 if (!(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
3456 spa_config_enter(spa, SCL_ZIO, zio, RW_READER);
34dc7c2f 3457
b128c09f
BB
3458 /*
3459 * The mirror_ops handle multiple DVAs in a single BP.
3460 */
98b25418
GW
3461 vdev_mirror_ops.vdev_op_io_start(zio);
3462 return (ZIO_PIPELINE_STOP);
34dc7c2f
BB
3463 }
3464
3dfb57a3 3465 ASSERT3P(zio->io_logical, !=, zio);
a1d477c2
MA
3466 if (zio->io_type == ZIO_TYPE_WRITE && zio->io_vd->vdev_removing) {
3467 /*
3468 * Note: the code can handle other kinds of writes,
3469 * but we don't expect them.
3470 */
3471 ASSERT(zio->io_flags &
3472 (ZIO_FLAG_PHYSICAL | ZIO_FLAG_SELF_HEAL |
9e052db4 3473 ZIO_FLAG_RESILVER | ZIO_FLAG_INDUCE_DAMAGE));
a1d477c2 3474 }
3dfb57a3 3475
b128c09f
BB
3476 align = 1ULL << vd->vdev_top->vdev_ashift;
3477
b02fe35d
AR
3478 if (!(zio->io_flags & ZIO_FLAG_PHYSICAL) &&
3479 P2PHASE(zio->io_size, align) != 0) {
3480 /* Transform logical writes to be a full physical block size. */
34dc7c2f 3481 uint64_t asize = P2ROUNDUP(zio->io_size, align);
a6255b7f 3482 abd_t *abuf = abd_alloc_sametype(zio->io_abd, asize);
178e73b3 3483 ASSERT(vd == vd->vdev_top);
34dc7c2f 3484 if (zio->io_type == ZIO_TYPE_WRITE) {
a6255b7f
DQ
3485 abd_copy(abuf, zio->io_abd, zio->io_size);
3486 abd_zero_off(abuf, zio->io_size, asize - zio->io_size);
34dc7c2f 3487 }
b128c09f 3488 zio_push_transform(zio, abuf, asize, asize, zio_subblock);
34dc7c2f
BB
3489 }
3490
b02fe35d
AR
3491 /*
3492 * If this is not a physical io, make sure that it is properly aligned
3493 * before proceeding.
3494 */
3495 if (!(zio->io_flags & ZIO_FLAG_PHYSICAL)) {
3496 ASSERT0(P2PHASE(zio->io_offset, align));
3497 ASSERT0(P2PHASE(zio->io_size, align));
3498 } else {
3499 /*
3500 * For physical writes, we allow 512b aligned writes and assume
3501 * the device will perform a read-modify-write as necessary.
3502 */
3503 ASSERT0(P2PHASE(zio->io_offset, SPA_MINBLOCKSIZE));
3504 ASSERT0(P2PHASE(zio->io_size, SPA_MINBLOCKSIZE));
3505 }
3506
572e2857 3507 VERIFY(zio->io_type != ZIO_TYPE_WRITE || spa_writeable(spa));
fb5f0bc8
BB
3508
3509 /*
3510 * If this is a repair I/O, and there's no self-healing involved --
3511 * that is, we're just resilvering what we expect to resilver --
3512 * then don't do the I/O unless zio's txg is actually in vd's DTL.
9e052db4
MA
3513 * This prevents spurious resilvering.
3514 *
3515 * There are a few ways that we can end up creating these spurious
3516 * resilver i/os:
3517 *
3518 * 1. A resilver i/o will be issued if any DVA in the BP has a
3519 * dirty DTL. The mirror code will issue resilver writes to
3520 * each DVA, including the one(s) that are not on vdevs with dirty
3521 * DTLs.
3522 *
3523 * 2. With nested replication, which happens when we have a
3524 * "replacing" or "spare" vdev that's a child of a mirror or raidz.
3525 * For example, given mirror(replacing(A+B), C), it's likely that
3526 * only A is out of date (it's the new device). In this case, we'll
3527 * read from C, then use the data to resilver A+B -- but we don't
3528 * actually want to resilver B, just A. The top-level mirror has no
3529 * way to know this, so instead we just discard unnecessary repairs
3530 * as we work our way down the vdev tree.
3531 *
3532 * 3. ZTEST also creates mirrors of mirrors, mirrors of raidz, etc.
3533 * The same logic applies to any form of nested replication: ditto
3534 * + mirror, RAID-Z + replacing, etc.
3535 *
3536 * However, indirect vdevs point off to other vdevs which may have
3537 * DTL's, so we never bypass them. The child i/os on concrete vdevs
3538 * will be properly bypassed instead.
fb5f0bc8
BB
3539 */
3540 if ((zio->io_flags & ZIO_FLAG_IO_REPAIR) &&
3541 !(zio->io_flags & ZIO_FLAG_SELF_HEAL) &&
3542 zio->io_txg != 0 && /* not a delegated i/o */
9e052db4 3543 vd->vdev_ops != &vdev_indirect_ops &&
fb5f0bc8
BB
3544 !vdev_dtl_contains(vd, DTL_PARTIAL, zio->io_txg, 1)) {
3545 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
fb5f0bc8
BB
3546 zio_vdev_io_bypass(zio);
3547 return (ZIO_PIPELINE_CONTINUE);
3548 }
34dc7c2f 3549
b128c09f
BB
3550 if (vd->vdev_ops->vdev_op_leaf &&
3551 (zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE)) {
3552
b0bc7a84 3553 if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio))
d164b209 3554 return (ZIO_PIPELINE_CONTINUE);
b128c09f
BB
3555
3556 if ((zio = vdev_queue_io(zio)) == NULL)
3557 return (ZIO_PIPELINE_STOP);
3558
3559 if (!vdev_accessible(vd, zio)) {
2e528b49 3560 zio->io_error = SET_ERROR(ENXIO);
b128c09f
BB
3561 zio_interrupt(zio);
3562 return (ZIO_PIPELINE_STOP);
3563 }
67103816 3564 zio->io_delay = gethrtime();
b128c09f
BB
3565 }
3566
98b25418
GW
3567 vd->vdev_ops->vdev_op_io_start(zio);
3568 return (ZIO_PIPELINE_STOP);
34dc7c2f
BB
3569}
3570
3571static int
3572zio_vdev_io_done(zio_t *zio)
3573{
b128c09f
BB
3574 vdev_t *vd = zio->io_vd;
3575 vdev_ops_t *ops = vd ? vd->vdev_ops : &vdev_mirror_ops;
3576 boolean_t unexpected_error = B_FALSE;
34dc7c2f 3577
ddc751d5 3578 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV_BIT, ZIO_WAIT_DONE)) {
b128c09f 3579 return (ZIO_PIPELINE_STOP);
ddc751d5 3580 }
34dc7c2f 3581
b128c09f
BB
3582 ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
3583
193a37cb
TH
3584 if (zio->io_delay)
3585 zio->io_delay = gethrtime() - zio->io_delay;
3586
b128c09f
BB
3587 if (vd != NULL && vd->vdev_ops->vdev_op_leaf) {
3588
3589 vdev_queue_io_done(zio);
3590
3591 if (zio->io_type == ZIO_TYPE_WRITE)
3592 vdev_cache_write(zio);
3593
3594 if (zio_injection_enabled && zio->io_error == 0)
d977122d
DB
3595 zio->io_error = zio_handle_device_injections(vd, zio,
3596 EIO, EILSEQ);
b128c09f
BB
3597
3598 if (zio_injection_enabled && zio->io_error == 0)
3599 zio->io_error = zio_handle_label_injection(zio, EIO);
3600
3601 if (zio->io_error) {
3602 if (!vdev_accessible(vd, zio)) {
2e528b49 3603 zio->io_error = SET_ERROR(ENXIO);
b128c09f
BB
3604 } else {
3605 unexpected_error = B_TRUE;
3606 }
3607 }
3608 }
3609
3610 ops->vdev_op_io_done(zio);
34dc7c2f 3611
f43615d0 3612 if (unexpected_error)
d164b209 3613 VERIFY(vdev_probe(vd, zio) == NULL);
34dc7c2f 3614
b128c09f 3615 return (ZIO_PIPELINE_CONTINUE);
34dc7c2f
BB
3616}
3617
a8b2e306
TC
3618/*
3619 * This function is used to change the priority of an existing zio that is
3620 * currently in-flight. This is used by the arc to upgrade priority in the
3621 * event that a demand read is made for a block that is currently queued
3622 * as a scrub or async read IO. Otherwise, the high priority read request
3623 * would end up having to wait for the lower priority IO.
3624 */
3625void
3626zio_change_priority(zio_t *pio, zio_priority_t priority)
3627{
3628 zio_t *cio, *cio_next;
3629 zio_link_t *zl = NULL;
3630
3631 ASSERT3U(priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
3632
3633 if (pio->io_vd != NULL && pio->io_vd->vdev_ops->vdev_op_leaf) {
3634 vdev_queue_change_io_priority(pio, priority);
3635 } else {
3636 pio->io_priority = priority;
3637 }
3638
3639 mutex_enter(&pio->io_lock);
3640 for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) {
3641 cio_next = zio_walk_children(pio, &zl);
3642 zio_change_priority(cio, priority);
3643 }
3644 mutex_exit(&pio->io_lock);
3645}
3646
428870ff
BB
3647/*
3648 * For non-raidz ZIOs, we can just copy aside the bad data read from the
3649 * disk, and use that to finish the checksum ereport later.
3650 */
3651static void
3652zio_vsd_default_cksum_finish(zio_cksum_report_t *zcr,
84c07ada 3653 const abd_t *good_buf)
428870ff
BB
3654{
3655 /* no processing needed */
3656 zfs_ereport_finish_checksum(zcr, good_buf, zcr->zcr_cbdata, B_FALSE);
3657}
3658
3659/*ARGSUSED*/
3660void
3661zio_vsd_default_cksum_report(zio_t *zio, zio_cksum_report_t *zcr, void *ignored)
3662{
84c07ada 3663 void *abd = abd_alloc_sametype(zio->io_abd, zio->io_size);
428870ff 3664
84c07ada 3665 abd_copy(abd, zio->io_abd, zio->io_size);
428870ff
BB
3666
3667 zcr->zcr_cbinfo = zio->io_size;
84c07ada 3668 zcr->zcr_cbdata = abd;
428870ff 3669 zcr->zcr_finish = zio_vsd_default_cksum_finish;
84c07ada 3670 zcr->zcr_free = zio_abd_free;
428870ff
BB
3671}
3672
34dc7c2f
BB
3673static int
3674zio_vdev_io_assess(zio_t *zio)
3675{
3676 vdev_t *vd = zio->io_vd;
b128c09f 3677
ddc751d5 3678 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV_BIT, ZIO_WAIT_DONE)) {
b128c09f 3679 return (ZIO_PIPELINE_STOP);
ddc751d5 3680 }
b128c09f
BB
3681
3682 if (vd == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
3683 spa_config_exit(zio->io_spa, SCL_ZIO, zio);
3684
3685 if (zio->io_vsd != NULL) {
428870ff 3686 zio->io_vsd_ops->vsd_free(zio);
b128c09f 3687 zio->io_vsd = NULL;
34dc7c2f
BB
3688 }
3689
b128c09f 3690 if (zio_injection_enabled && zio->io_error == 0)
34dc7c2f
BB
3691 zio->io_error = zio_handle_fault_injection(zio, EIO);
3692
3693 /*
3694 * If the I/O failed, determine whether we should attempt to retry it.
428870ff
BB
3695 *
3696 * On retry, we cut in line in the issue queue, since we don't want
3697 * compression/checksumming/etc. work to prevent our (cheap) IO reissue.
34dc7c2f 3698 */
b128c09f
BB
3699 if (zio->io_error && vd == NULL &&
3700 !(zio->io_flags & (ZIO_FLAG_DONT_RETRY | ZIO_FLAG_IO_RETRY))) {
3701 ASSERT(!(zio->io_flags & ZIO_FLAG_DONT_QUEUE)); /* not a leaf */
3702 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_BYPASS)); /* not a leaf */
34dc7c2f 3703 zio->io_error = 0;
b128c09f
BB
3704 zio->io_flags |= ZIO_FLAG_IO_RETRY |
3705 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE;
428870ff
BB
3706 zio->io_stage = ZIO_STAGE_VDEV_IO_START >> 1;
3707 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE,
3708 zio_requeue_io_start_cut_in_line);
b128c09f 3709 return (ZIO_PIPELINE_STOP);
34dc7c2f
BB
3710 }
3711
b128c09f
BB
3712 /*
3713 * If we got an error on a leaf device, convert it to ENXIO
3714 * if the device is not accessible at all.
3715 */
3716 if (zio->io_error && vd != NULL && vd->vdev_ops->vdev_op_leaf &&
3717 !vdev_accessible(vd, zio))
2e528b49 3718 zio->io_error = SET_ERROR(ENXIO);
b128c09f
BB
3719
3720 /*
3721 * If we can't write to an interior vdev (mirror or RAID-Z),
3722 * set vdev_cant_write so that we stop trying to allocate from it.
3723 */
3724 if (zio->io_error == ENXIO && zio->io_type == ZIO_TYPE_WRITE &&
13fe0198 3725 vd != NULL && !vd->vdev_ops->vdev_op_leaf) {
b128c09f 3726 vd->vdev_cant_write = B_TRUE;
13fe0198 3727 }
b128c09f 3728
298ec40b
GM
3729 /*
3730 * If a cache flush returns ENOTSUP or ENOTTY, we know that no future
3731 * attempts will ever succeed. In this case we set a persistent bit so
3732 * that we don't bother with it in the future.
3733 */
3734 if ((zio->io_error == ENOTSUP || zio->io_error == ENOTTY) &&
3735 zio->io_type == ZIO_TYPE_IOCTL &&
3736 zio->io_cmd == DKIOCFLUSHWRITECACHE && vd != NULL)
3737 vd->vdev_nowritecache = B_TRUE;
3738
b128c09f
BB
3739 if (zio->io_error)
3740 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
3741
e8b96c60
MA
3742 if (vd != NULL && vd->vdev_ops->vdev_op_leaf &&
3743 zio->io_physdone != NULL) {
3744 ASSERT(!(zio->io_flags & ZIO_FLAG_DELEGATED));
3745 ASSERT(zio->io_child_type == ZIO_CHILD_VDEV);
3746 zio->io_physdone(zio->io_logical);
3747 }
3748
34dc7c2f
BB
3749 return (ZIO_PIPELINE_CONTINUE);
3750}
3751
3752void
3753zio_vdev_io_reissue(zio_t *zio)
3754{
3755 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
3756 ASSERT(zio->io_error == 0);
3757
428870ff 3758 zio->io_stage >>= 1;
34dc7c2f
BB
3759}
3760
3761void
3762zio_vdev_io_redone(zio_t *zio)
3763{
3764 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE);
3765
428870ff 3766 zio->io_stage >>= 1;
34dc7c2f
BB
3767}
3768
3769void
3770zio_vdev_io_bypass(zio_t *zio)
3771{
3772 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
3773 ASSERT(zio->io_error == 0);
3774
3775 zio->io_flags |= ZIO_FLAG_IO_BYPASS;
428870ff 3776 zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS >> 1;
34dc7c2f
BB
3777}
3778
b5256303
TC
3779/*
3780 * ==========================================================================
3781 * Encrypt and store encryption parameters
3782 * ==========================================================================
3783 */
3784
3785
3786/*
3787 * This function is used for ZIO_STAGE_ENCRYPT. It is responsible for
3788 * managing the storage of encryption parameters and passing them to the
3789 * lower-level encryption functions.
3790 */
3791static int
3792zio_encrypt(zio_t *zio)
3793{
3794 zio_prop_t *zp = &zio->io_prop;
3795 spa_t *spa = zio->io_spa;
3796 blkptr_t *bp = zio->io_bp;
3797 uint64_t psize = BP_GET_PSIZE(bp);
ae76f45c 3798 uint64_t dsobj = zio->io_bookmark.zb_objset;
b5256303
TC
3799 dmu_object_type_t ot = BP_GET_TYPE(bp);
3800 void *enc_buf = NULL;
3801 abd_t *eabd = NULL;
3802 uint8_t salt[ZIO_DATA_SALT_LEN];
3803 uint8_t iv[ZIO_DATA_IV_LEN];
3804 uint8_t mac[ZIO_DATA_MAC_LEN];
3805 boolean_t no_crypt = B_FALSE;
3806
3807 /* the root zio already encrypted the data */
3808 if (zio->io_child_type == ZIO_CHILD_GANG)
3809 return (ZIO_PIPELINE_CONTINUE);
3810
3811 /* only ZIL blocks are re-encrypted on rewrite */
3812 if (!IO_IS_ALLOCATING(zio) && ot != DMU_OT_INTENT_LOG)
3813 return (ZIO_PIPELINE_CONTINUE);
3814
3815 if (!(zp->zp_encrypt || BP_IS_ENCRYPTED(bp))) {
3816 BP_SET_CRYPT(bp, B_FALSE);
3817 return (ZIO_PIPELINE_CONTINUE);
3818 }
3819
3820 /* if we are doing raw encryption set the provided encryption params */
3821 if (zio->io_flags & ZIO_FLAG_RAW_ENCRYPT) {
ae76f45c 3822 ASSERT0(BP_GET_LEVEL(bp));
b5256303
TC
3823 BP_SET_CRYPT(bp, B_TRUE);
3824 BP_SET_BYTEORDER(bp, zp->zp_byteorder);
3825 if (ot != DMU_OT_OBJSET)
3826 zio_crypt_encode_mac_bp(bp, zp->zp_mac);
ae76f45c
TC
3827
3828 /* dnode blocks must be written out in the provided byteorder */
3829 if (zp->zp_byteorder != ZFS_HOST_BYTEORDER &&
3830 ot == DMU_OT_DNODE) {
3831 void *bswap_buf = zio_buf_alloc(psize);
3832 abd_t *babd = abd_get_from_buf(bswap_buf, psize);
3833
3834 ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF);
3835 abd_copy_to_buf(bswap_buf, zio->io_abd, psize);
3836 dmu_ot_byteswap[DMU_OT_BYTESWAP(ot)].ob_func(bswap_buf,
3837 psize);
3838
3839 abd_take_ownership_of_buf(babd, B_TRUE);
3840 zio_push_transform(zio, babd, psize, psize, NULL);
3841 }
3842
b5256303
TC
3843 if (DMU_OT_IS_ENCRYPTED(ot))
3844 zio_crypt_encode_params_bp(bp, zp->zp_salt, zp->zp_iv);
3845 return (ZIO_PIPELINE_CONTINUE);
3846 }
3847
3848 /* indirect blocks only maintain a cksum of the lower level MACs */
3849 if (BP_GET_LEVEL(bp) > 0) {
3850 BP_SET_CRYPT(bp, B_TRUE);
3851 VERIFY0(zio_crypt_do_indirect_mac_checksum_abd(B_TRUE,
3852 zio->io_orig_abd, BP_GET_LSIZE(bp), BP_SHOULD_BYTESWAP(bp),
3853 mac));
3854 zio_crypt_encode_mac_bp(bp, mac);
3855 return (ZIO_PIPELINE_CONTINUE);
3856 }
3857
3858 /*
3859 * Objset blocks are a special case since they have 2 256-bit MACs
3860 * embedded within them.
3861 */
3862 if (ot == DMU_OT_OBJSET) {
3863 ASSERT0(DMU_OT_IS_ENCRYPTED(ot));
3864 ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF);
3865 BP_SET_CRYPT(bp, B_TRUE);
ae76f45c
TC
3866 VERIFY0(spa_do_crypt_objset_mac_abd(B_TRUE, spa, dsobj,
3867 zio->io_abd, psize, BP_SHOULD_BYTESWAP(bp)));
b5256303
TC
3868 return (ZIO_PIPELINE_CONTINUE);
3869 }
3870
3871 /* unencrypted object types are only authenticated with a MAC */
3872 if (!DMU_OT_IS_ENCRYPTED(ot)) {
3873 BP_SET_CRYPT(bp, B_TRUE);
ae76f45c
TC
3874 VERIFY0(spa_do_crypt_mac_abd(B_TRUE, spa, dsobj,
3875 zio->io_abd, psize, mac));
b5256303
TC
3876 zio_crypt_encode_mac_bp(bp, mac);
3877 return (ZIO_PIPELINE_CONTINUE);
3878 }
3879
3880 /*
3881 * Later passes of sync-to-convergence may decide to rewrite data
3882 * in place to avoid more disk reallocations. This presents a problem
3883 * for encryption because this consitutes rewriting the new data with
3884 * the same encryption key and IV. However, this only applies to blocks
3885 * in the MOS (particularly the spacemaps) and we do not encrypt the
3886 * MOS. We assert that the zio is allocating or an intent log write
3887 * to enforce this.
3888 */
3889 ASSERT(IO_IS_ALLOCATING(zio) || ot == DMU_OT_INTENT_LOG);
3890 ASSERT(BP_GET_LEVEL(bp) == 0 || ot == DMU_OT_INTENT_LOG);
3891 ASSERT(spa_feature_is_active(spa, SPA_FEATURE_ENCRYPTION));
3892 ASSERT3U(psize, !=, 0);
3893
3894 enc_buf = zio_buf_alloc(psize);
3895 eabd = abd_get_from_buf(enc_buf, psize);
3896 abd_take_ownership_of_buf(eabd, B_TRUE);
3897
3898 /*
3899 * For an explanation of what encryption parameters are stored
3900 * where, see the block comment in zio_crypt.c.
3901 */
3902 if (ot == DMU_OT_INTENT_LOG) {
3903 zio_crypt_decode_params_bp(bp, salt, iv);
3904 } else {
3905 BP_SET_CRYPT(bp, B_TRUE);
3906 }
3907
3908 /* Perform the encryption. This should not fail */
ae76f45c
TC
3909 VERIFY0(spa_do_crypt_abd(B_TRUE, spa, dsobj, bp, zio->io_txg,
3910 psize, zio->io_abd, eabd, iv, mac, salt, &no_crypt));
b5256303
TC
3911
3912 /* encode encryption metadata into the bp */
3913 if (ot == DMU_OT_INTENT_LOG) {
3914 /*
3915 * ZIL blocks store the MAC in the embedded checksum, so the
3916 * transform must always be applied.
3917 */
3918 zio_crypt_encode_mac_zil(enc_buf, mac);
3919 zio_push_transform(zio, eabd, psize, psize, NULL);
3920 } else {
3921 BP_SET_CRYPT(bp, B_TRUE);
3922 zio_crypt_encode_params_bp(bp, salt, iv);
3923 zio_crypt_encode_mac_bp(bp, mac);
3924
3925 if (no_crypt) {
3926 ASSERT3U(ot, ==, DMU_OT_DNODE);
3927 abd_free(eabd);
3928 } else {
3929 zio_push_transform(zio, eabd, psize, psize, NULL);
3930 }
3931 }
3932
3933 return (ZIO_PIPELINE_CONTINUE);
3934}
3935
34dc7c2f
BB
3936/*
3937 * ==========================================================================
3938 * Generate and verify checksums
3939 * ==========================================================================
3940 */
3941static int
3942zio_checksum_generate(zio_t *zio)
3943{
34dc7c2f 3944 blkptr_t *bp = zio->io_bp;
b128c09f 3945 enum zio_checksum checksum;
34dc7c2f 3946
b128c09f
BB
3947 if (bp == NULL) {
3948 /*
3949 * This is zio_write_phys().
3950 * We're either generating a label checksum, or none at all.
3951 */
3952 checksum = zio->io_prop.zp_checksum;
34dc7c2f 3953
b128c09f
BB
3954 if (checksum == ZIO_CHECKSUM_OFF)
3955 return (ZIO_PIPELINE_CONTINUE);
3956
3957 ASSERT(checksum == ZIO_CHECKSUM_LABEL);
3958 } else {
3959 if (BP_IS_GANG(bp) && zio->io_child_type == ZIO_CHILD_GANG) {
3960 ASSERT(!IO_IS_ALLOCATING(zio));
3961 checksum = ZIO_CHECKSUM_GANG_HEADER;
3962 } else {
3963 checksum = BP_GET_CHECKSUM(bp);
3964 }
3965 }
34dc7c2f 3966
a6255b7f 3967 zio_checksum_compute(zio, checksum, zio->io_abd, zio->io_size);
34dc7c2f
BB
3968
3969 return (ZIO_PIPELINE_CONTINUE);
3970}
3971
3972static int
b128c09f 3973zio_checksum_verify(zio_t *zio)
34dc7c2f 3974{
428870ff 3975 zio_bad_cksum_t info;
b128c09f
BB
3976 blkptr_t *bp = zio->io_bp;
3977 int error;
34dc7c2f 3978
428870ff
BB
3979 ASSERT(zio->io_vd != NULL);
3980
b128c09f
BB
3981 if (bp == NULL) {
3982 /*
3983 * This is zio_read_phys().
3984 * We're either verifying a label checksum, or nothing at all.
3985 */
3986 if (zio->io_prop.zp_checksum == ZIO_CHECKSUM_OFF)
3987 return (ZIO_PIPELINE_CONTINUE);
34dc7c2f 3988
b128c09f
BB
3989 ASSERT(zio->io_prop.zp_checksum == ZIO_CHECKSUM_LABEL);
3990 }
34dc7c2f 3991
428870ff 3992 if ((error = zio_checksum_error(zio, &info)) != 0) {
b128c09f 3993 zio->io_error = error;
7a3066ff
MA
3994 if (error == ECKSUM &&
3995 !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
428870ff 3996 zfs_ereport_start_checksum(zio->io_spa,
b5256303
TC
3997 zio->io_vd, &zio->io_bookmark, zio,
3998 zio->io_offset, zio->io_size, NULL, &info);
b128c09f 3999 }
34dc7c2f
BB
4000 }
4001
4002 return (ZIO_PIPELINE_CONTINUE);
4003}
4004
4005/*
4006 * Called by RAID-Z to ensure we don't compute the checksum twice.
4007 */
4008void
4009zio_checksum_verified(zio_t *zio)
4010{
428870ff 4011 zio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
34dc7c2f
BB
4012}
4013
4014/*
b128c09f
BB
4015 * ==========================================================================
4016 * Error rank. Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other.
9b67f605 4017 * An error of 0 indicates success. ENXIO indicates whole-device failure,
b128c09f
BB
4018 * which may be transient (e.g. unplugged) or permament. ECKSUM and EIO
4019 * indicate errors that are specific to one I/O, and most likely permanent.
4020 * Any other error is presumed to be worse because we weren't expecting it.
4021 * ==========================================================================
34dc7c2f 4022 */
b128c09f
BB
4023int
4024zio_worst_error(int e1, int e2)
34dc7c2f 4025{
b128c09f
BB
4026 static int zio_error_rank[] = { 0, ENXIO, ECKSUM, EIO };
4027 int r1, r2;
4028
4029 for (r1 = 0; r1 < sizeof (zio_error_rank) / sizeof (int); r1++)
4030 if (e1 == zio_error_rank[r1])
4031 break;
34dc7c2f 4032
b128c09f
BB
4033 for (r2 = 0; r2 < sizeof (zio_error_rank) / sizeof (int); r2++)
4034 if (e2 == zio_error_rank[r2])
4035 break;
4036
4037 return (r1 > r2 ? e1 : e2);
34dc7c2f
BB
4038}
4039
4040/*
4041 * ==========================================================================
b128c09f 4042 * I/O completion
34dc7c2f
BB
4043 * ==========================================================================
4044 */
b128c09f
BB
4045static int
4046zio_ready(zio_t *zio)
34dc7c2f 4047{
b128c09f 4048 blkptr_t *bp = zio->io_bp;
d164b209 4049 zio_t *pio, *pio_next;
3dfb57a3 4050 zio_link_t *zl = NULL;
34dc7c2f 4051
ddc751d5
GW
4052 if (zio_wait_for_children(zio, ZIO_CHILD_GANG_BIT | ZIO_CHILD_DDT_BIT,
4053 ZIO_WAIT_READY)) {
9babb374 4054 return (ZIO_PIPELINE_STOP);
ddc751d5 4055 }
34dc7c2f 4056
9babb374 4057 if (zio->io_ready) {
b128c09f 4058 ASSERT(IO_IS_ALLOCATING(zio));
03c6040b
GW
4059 ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp) ||
4060 (zio->io_flags & ZIO_FLAG_NOPWRITE));
b128c09f 4061 ASSERT(zio->io_children[ZIO_CHILD_GANG][ZIO_WAIT_READY] == 0);
34dc7c2f 4062
b128c09f
BB
4063 zio->io_ready(zio);
4064 }
34dc7c2f 4065
b128c09f
BB
4066 if (bp != NULL && bp != &zio->io_bp_copy)
4067 zio->io_bp_copy = *bp;
34dc7c2f 4068
3dfb57a3 4069 if (zio->io_error != 0) {
b128c09f 4070 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
34dc7c2f 4071
3dfb57a3
DB
4072 if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
4073 ASSERT(IO_IS_ALLOCATING(zio));
4074 ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
4075 /*
4076 * We were unable to allocate anything, unreserve and
4077 * issue the next I/O to allocate.
4078 */
4079 metaslab_class_throttle_unreserve(
4080 spa_normal_class(zio->io_spa),
4081 zio->io_prop.zp_copies, zio);
4082 zio_allocate_dispatch(zio->io_spa);
4083 }
4084 }
4085
d164b209
BB
4086 mutex_enter(&zio->io_lock);
4087 zio->io_state[ZIO_WAIT_READY] = 1;
3dfb57a3 4088 pio = zio_walk_parents(zio, &zl);
d164b209
BB
4089 mutex_exit(&zio->io_lock);
4090
4091 /*
4092 * As we notify zio's parents, new parents could be added.
4093 * New parents go to the head of zio's io_parent_list, however,
4094 * so we will (correctly) not notify them. The remainder of zio's
4095 * io_parent_list, from 'pio_next' onward, cannot change because
4096 * all parents must wait for us to be done before they can be done.
4097 */
4098 for (; pio != NULL; pio = pio_next) {
3dfb57a3 4099 pio_next = zio_walk_parents(zio, &zl);
b128c09f 4100 zio_notify_parent(pio, zio, ZIO_WAIT_READY);
d164b209 4101 }
34dc7c2f 4102
428870ff
BB
4103 if (zio->io_flags & ZIO_FLAG_NODATA) {
4104 if (BP_IS_GANG(bp)) {
4105 zio->io_flags &= ~ZIO_FLAG_NODATA;
4106 } else {
a6255b7f 4107 ASSERT((uintptr_t)zio->io_abd < SPA_MAXBLOCKSIZE);
428870ff
BB
4108 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
4109 }
4110 }
4111
4112 if (zio_injection_enabled &&
4113 zio->io_spa->spa_syncing_txg == zio->io_txg)
4114 zio_handle_ignored_writes(zio);
4115
b128c09f 4116 return (ZIO_PIPELINE_CONTINUE);
34dc7c2f
BB
4117}
4118
3dfb57a3
DB
4119/*
4120 * Update the allocation throttle accounting.
4121 */
4122static void
4123zio_dva_throttle_done(zio_t *zio)
4124{
1c27024e 4125 ASSERTV(zio_t *lio = zio->io_logical);
3dfb57a3
DB
4126 zio_t *pio = zio_unique_parent(zio);
4127 vdev_t *vd = zio->io_vd;
4128 int flags = METASLAB_ASYNC_ALLOC;
4129
4130 ASSERT3P(zio->io_bp, !=, NULL);
4131 ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE);
4132 ASSERT3U(zio->io_priority, ==, ZIO_PRIORITY_ASYNC_WRITE);
4133 ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
4134 ASSERT(vd != NULL);
4135 ASSERT3P(vd, ==, vd->vdev_top);
21df134f
SB
4136 ASSERT(zio_injection_enabled || !(zio->io_flags & ZIO_FLAG_IO_RETRY));
4137 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REPAIR));
3dfb57a3
DB
4138 ASSERT(zio->io_flags & ZIO_FLAG_IO_ALLOCATING);
4139 ASSERT(!(lio->io_flags & ZIO_FLAG_IO_REWRITE));
4140 ASSERT(!(lio->io_orig_flags & ZIO_FLAG_NODATA));
4141
4142 /*
4143 * Parents of gang children can have two flavors -- ones that
4144 * allocated the gang header (will have ZIO_FLAG_IO_REWRITE set)
4145 * and ones that allocated the constituent blocks. The allocation
4146 * throttle needs to know the allocating parent zio so we must find
4147 * it here.
4148 */
4149 if (pio->io_child_type == ZIO_CHILD_GANG) {
4150 /*
4151 * If our parent is a rewrite gang child then our grandparent
4152 * would have been the one that performed the allocation.
4153 */
4154 if (pio->io_flags & ZIO_FLAG_IO_REWRITE)
4155 pio = zio_unique_parent(pio);
4156 flags |= METASLAB_GANG_CHILD;
4157 }
4158
4159 ASSERT(IO_IS_ALLOCATING(pio));
4160 ASSERT3P(zio, !=, zio->io_logical);
4161 ASSERT(zio->io_logical != NULL);
4162 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REPAIR));
4163 ASSERT0(zio->io_flags & ZIO_FLAG_NOPWRITE);
4164
4165 mutex_enter(&pio->io_lock);
4166 metaslab_group_alloc_decrement(zio->io_spa, vd->vdev_id, pio, flags);
4167 mutex_exit(&pio->io_lock);
4168
4169 metaslab_class_throttle_unreserve(spa_normal_class(zio->io_spa),
4170 1, pio);
4171
4172 /*
4173 * Call into the pipeline to see if there is more work that
4174 * needs to be done. If there is work to be done it will be
4175 * dispatched to another taskq thread.
4176 */
4177 zio_allocate_dispatch(zio->io_spa);
4178}
4179
b128c09f
BB
4180static int
4181zio_done(zio_t *zio)
34dc7c2f 4182{
3dfb57a3
DB
4183 /*
4184 * Always attempt to keep stack usage minimal here since
4185 * we can be called recurisvely up to 19 levels deep.
4186 */
84c07ada 4187 const uint64_t psize = zio->io_size;
d164b209 4188 zio_t *pio, *pio_next;
3dfb57a3 4189 zio_link_t *zl = NULL;
34dc7c2f 4190
b128c09f 4191 /*
9babb374 4192 * If our children haven't all completed,
b128c09f
BB
4193 * wait for them and then repeat this pipeline stage.
4194 */
ddc751d5 4195 if (zio_wait_for_children(zio, ZIO_CHILD_ALL_BITS, ZIO_WAIT_DONE)) {
b128c09f 4196 return (ZIO_PIPELINE_STOP);
ddc751d5 4197 }
34dc7c2f 4198
3dfb57a3
DB
4199 /*
4200 * If the allocation throttle is enabled, then update the accounting.
4201 * We only track child I/Os that are part of an allocating async
4202 * write. We must do this since the allocation is performed
4203 * by the logical I/O but the actual write is done by child I/Os.
4204 */
4205 if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING &&
4206 zio->io_child_type == ZIO_CHILD_VDEV) {
4207 ASSERT(spa_normal_class(
4208 zio->io_spa)->mc_alloc_throttle_enabled);
4209 zio_dva_throttle_done(zio);
4210 }
4211
4212 /*
4213 * If the allocation throttle is enabled, verify that
4214 * we have decremented the refcounts for every I/O that was throttled.
4215 */
4216 if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
4217 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
4218 ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
4219 ASSERT(zio->io_bp != NULL);
4220 metaslab_group_alloc_verify(zio->io_spa, zio->io_bp, zio);
4221 VERIFY(refcount_not_held(
4222 &(spa_normal_class(zio->io_spa)->mc_alloc_slots), zio));
4223 }
4224
4225
1c27024e
DB
4226 for (int c = 0; c < ZIO_CHILD_TYPES; c++)
4227 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
b128c09f
BB
4228 ASSERT(zio->io_children[c][w] == 0);
4229
9b67f605 4230 if (zio->io_bp != NULL && !BP_IS_EMBEDDED(zio->io_bp)) {
c776b317
BB
4231 ASSERT(zio->io_bp->blk_pad[0] == 0);
4232 ASSERT(zio->io_bp->blk_pad[1] == 0);
d1d7e268
MK
4233 ASSERT(bcmp(zio->io_bp, &zio->io_bp_copy,
4234 sizeof (blkptr_t)) == 0 ||
c776b317
BB
4235 (zio->io_bp == zio_unique_parent(zio)->io_bp));
4236 if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(zio->io_bp) &&
428870ff 4237 zio->io_bp_override == NULL &&
b128c09f 4238 !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) {
d1d7e268
MK
4239 ASSERT3U(zio->io_prop.zp_copies, <=,
4240 BP_GET_NDVAS(zio->io_bp));
c776b317 4241 ASSERT(BP_COUNT_GANG(zio->io_bp) == 0 ||
d1d7e268
MK
4242 (BP_COUNT_GANG(zio->io_bp) ==
4243 BP_GET_NDVAS(zio->io_bp)));
b128c09f 4244 }
03c6040b
GW
4245 if (zio->io_flags & ZIO_FLAG_NOPWRITE)
4246 VERIFY(BP_EQUAL(zio->io_bp, &zio->io_bp_orig));
b128c09f
BB
4247 }
4248
4249 /*
428870ff 4250 * If there were child vdev/gang/ddt errors, they apply to us now.
b128c09f
BB
4251 */
4252 zio_inherit_child_errors(zio, ZIO_CHILD_VDEV);
4253 zio_inherit_child_errors(zio, ZIO_CHILD_GANG);
428870ff
BB
4254 zio_inherit_child_errors(zio, ZIO_CHILD_DDT);
4255
4256 /*
4257 * If the I/O on the transformed data was successful, generate any
4258 * checksum reports now while we still have the transformed data.
4259 */
4260 if (zio->io_error == 0) {
4261 while (zio->io_cksum_report != NULL) {
4262 zio_cksum_report_t *zcr = zio->io_cksum_report;
4263 uint64_t align = zcr->zcr_align;
a6255b7f 4264 uint64_t asize = P2ROUNDUP(psize, align);
a6255b7f
DQ
4265 abd_t *adata = zio->io_abd;
4266
4267 if (asize != psize) {
84c07ada 4268 adata = abd_alloc(asize, B_TRUE);
a6255b7f
DQ
4269 abd_copy(adata, zio->io_abd, psize);
4270 abd_zero_off(adata, psize, asize - psize);
428870ff
BB
4271 }
4272
4273 zio->io_cksum_report = zcr->zcr_next;
4274 zcr->zcr_next = NULL;
84c07ada 4275 zcr->zcr_finish(zcr, adata);
428870ff
BB
4276 zfs_ereport_free_checksum(zcr);
4277
a6255b7f
DQ
4278 if (asize != psize)
4279 abd_free(adata);
428870ff
BB
4280 }
4281 }
b128c09f
BB
4282
4283 zio_pop_transforms(zio); /* note: may set zio->io_error */
4284
a6255b7f 4285 vdev_stat_update(zio, psize);
b128c09f 4286
a69052be 4287 /*
cc92e9d0 4288 * If this I/O is attached to a particular vdev is slow, exceeding
72f53c56
MJ
4289 * 30 seconds to complete, post an error described the I/O delay.
4290 * We ignore these errors if the device is currently unavailable.
a69052be 4291 */
193a37cb 4292 if (zio->io_delay >= MSEC2NSEC(zio_delay_max)) {
72f53c56
MJ
4293 if (zio->io_vd != NULL && !vdev_is_dead(zio->io_vd))
4294 zfs_ereport_post(FM_EREPORT_ZFS_DELAY, zio->io_spa,
b5256303 4295 zio->io_vd, &zio->io_bookmark, zio, 0, 0);
72f53c56 4296 }
a69052be 4297
b128c09f
BB
4298 if (zio->io_error) {
4299 /*
4300 * If this I/O is attached to a particular vdev,
4301 * generate an error message describing the I/O failure
4302 * at the block level. We ignore these errors if the
4303 * device is currently unavailable.
4304 */
c776b317 4305 if (zio->io_error != ECKSUM && zio->io_vd != NULL &&
02730c33 4306 !vdev_is_dead(zio->io_vd))
c776b317 4307 zfs_ereport_post(FM_EREPORT_ZFS_IO, zio->io_spa,
b5256303 4308 zio->io_vd, &zio->io_bookmark, zio, 0, 0);
34dc7c2f 4309
428870ff
BB
4310 if ((zio->io_error == EIO || !(zio->io_flags &
4311 (ZIO_FLAG_SPECULATIVE | ZIO_FLAG_DONT_PROPAGATE))) &&
c776b317 4312 zio == zio->io_logical) {
b128c09f
BB
4313 /*
4314 * For logical I/O requests, tell the SPA to log the
4315 * error and generate a logical data ereport.
4316 */
b5256303 4317 spa_log_error(zio->io_spa, &zio->io_bookmark);
d1d7e268 4318 zfs_ereport_post(FM_EREPORT_ZFS_DATA, zio->io_spa,
b5256303 4319 NULL, &zio->io_bookmark, zio, 0, 0);
b128c09f
BB
4320 }
4321 }
34dc7c2f 4322
c776b317 4323 if (zio->io_error && zio == zio->io_logical) {
b128c09f
BB
4324 /*
4325 * Determine whether zio should be reexecuted. This will
4326 * propagate all the way to the root via zio_notify_parent().
4327 */
c776b317 4328 ASSERT(zio->io_vd == NULL && zio->io_bp != NULL);
428870ff 4329 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
b128c09f 4330
428870ff
BB
4331 if (IO_IS_ALLOCATING(zio) &&
4332 !(zio->io_flags & ZIO_FLAG_CANFAIL)) {
b128c09f
BB
4333 if (zio->io_error != ENOSPC)
4334 zio->io_reexecute |= ZIO_REEXECUTE_NOW;
4335 else
4336 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
428870ff 4337 }
b128c09f
BB
4338
4339 if ((zio->io_type == ZIO_TYPE_READ ||
4340 zio->io_type == ZIO_TYPE_FREE) &&
572e2857 4341 !(zio->io_flags & ZIO_FLAG_SCAN_THREAD) &&
b128c09f 4342 zio->io_error == ENXIO &&
c776b317
BB
4343 spa_load_state(zio->io_spa) == SPA_LOAD_NONE &&
4344 spa_get_failmode(zio->io_spa) != ZIO_FAILURE_MODE_CONTINUE)
b128c09f
BB
4345 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
4346
4347 if (!(zio->io_flags & ZIO_FLAG_CANFAIL) && !zio->io_reexecute)
4348 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
428870ff
BB
4349
4350 /*
4351 * Here is a possibly good place to attempt to do
4352 * either combinatorial reconstruction or error correction
4353 * based on checksums. It also might be a good place
4354 * to send out preliminary ereports before we suspend
4355 * processing.
4356 */
34dc7c2f
BB
4357 }
4358
4359 /*
b128c09f
BB
4360 * If there were logical child errors, they apply to us now.
4361 * We defer this until now to avoid conflating logical child
4362 * errors with errors that happened to the zio itself when
4363 * updating vdev stats and reporting FMA events above.
34dc7c2f 4364 */
b128c09f 4365 zio_inherit_child_errors(zio, ZIO_CHILD_LOGICAL);
34dc7c2f 4366
428870ff
BB
4367 if ((zio->io_error || zio->io_reexecute) &&
4368 IO_IS_ALLOCATING(zio) && zio->io_gang_leader == zio &&
03c6040b 4369 !(zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)))
c776b317 4370 zio_dva_unallocate(zio, zio->io_gang_tree, zio->io_bp);
9babb374
BB
4371
4372 zio_gang_tree_free(&zio->io_gang_tree);
4373
4374 /*
4375 * Godfather I/Os should never suspend.
4376 */
4377 if ((zio->io_flags & ZIO_FLAG_GODFATHER) &&
4378 (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND))
a32494d2 4379 zio->io_reexecute &= ~ZIO_REEXECUTE_SUSPEND;
9babb374 4380
b128c09f
BB
4381 if (zio->io_reexecute) {
4382 /*
4383 * This is a logical I/O that wants to reexecute.
4384 *
4385 * Reexecute is top-down. When an i/o fails, if it's not
4386 * the root, it simply notifies its parent and sticks around.
4387 * The parent, seeing that it still has children in zio_done(),
4388 * does the same. This percolates all the way up to the root.
4389 * The root i/o will reexecute or suspend the entire tree.
4390 *
4391 * This approach ensures that zio_reexecute() honors
4392 * all the original i/o dependency relationships, e.g.
4393 * parents not executing until children are ready.
4394 */
4395 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
34dc7c2f 4396
9babb374 4397 zio->io_gang_leader = NULL;
b128c09f 4398
d164b209
BB
4399 mutex_enter(&zio->io_lock);
4400 zio->io_state[ZIO_WAIT_DONE] = 1;
4401 mutex_exit(&zio->io_lock);
4402
9babb374
BB
4403 /*
4404 * "The Godfather" I/O monitors its children but is
4405 * not a true parent to them. It will track them through
4406 * the pipeline but severs its ties whenever they get into
4407 * trouble (e.g. suspended). This allows "The Godfather"
4408 * I/O to return status without blocking.
4409 */
3dfb57a3
DB
4410 zl = NULL;
4411 for (pio = zio_walk_parents(zio, &zl); pio != NULL;
4412 pio = pio_next) {
4413 zio_link_t *remove_zl = zl;
4414 pio_next = zio_walk_parents(zio, &zl);
9babb374
BB
4415
4416 if ((pio->io_flags & ZIO_FLAG_GODFATHER) &&
4417 (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND)) {
3dfb57a3 4418 zio_remove_child(pio, zio, remove_zl);
9babb374
BB
4419 zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
4420 }
4421 }
4422
d164b209 4423 if ((pio = zio_unique_parent(zio)) != NULL) {
b128c09f
BB
4424 /*
4425 * We're not a root i/o, so there's nothing to do
4426 * but notify our parent. Don't propagate errors
4427 * upward since we haven't permanently failed yet.
4428 */
9babb374 4429 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
b128c09f
BB
4430 zio->io_flags |= ZIO_FLAG_DONT_PROPAGATE;
4431 zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
4432 } else if (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND) {
4433 /*
4434 * We'd fail again if we reexecuted now, so suspend
4435 * until conditions improve (e.g. device comes online).
4436 */
cec3a0a1 4437 zio_suspend(zio->io_spa, zio, ZIO_SUSPEND_IOERR);
b128c09f
BB
4438 } else {
4439 /*
4440 * Reexecution is potentially a huge amount of work.
4441 * Hand it off to the otherwise-unused claim taskq.
4442 */
a38718a6 4443 ASSERT(taskq_empty_ent(&zio->io_tqent));
7ef5e54e
AL
4444 spa_taskq_dispatch_ent(zio->io_spa,
4445 ZIO_TYPE_CLAIM, ZIO_TASKQ_ISSUE,
a38718a6
GA
4446 (task_func_t *)zio_reexecute, zio, 0,
4447 &zio->io_tqent);
b128c09f
BB
4448 }
4449 return (ZIO_PIPELINE_STOP);
34dc7c2f
BB
4450 }
4451
428870ff 4452 ASSERT(zio->io_child_count == 0);
b128c09f
BB
4453 ASSERT(zio->io_reexecute == 0);
4454 ASSERT(zio->io_error == 0 || (zio->io_flags & ZIO_FLAG_CANFAIL));
34dc7c2f 4455
428870ff
BB
4456 /*
4457 * Report any checksum errors, since the I/O is complete.
4458 */
4459 while (zio->io_cksum_report != NULL) {
4460 zio_cksum_report_t *zcr = zio->io_cksum_report;
4461 zio->io_cksum_report = zcr->zcr_next;
4462 zcr->zcr_next = NULL;
4463 zcr->zcr_finish(zcr, NULL);
4464 zfs_ereport_free_checksum(zcr);
4465 }
4466
920dd524 4467 if (zio->io_flags & ZIO_FLAG_FASTWRITE && zio->io_bp &&
9b67f605
MA
4468 !BP_IS_HOLE(zio->io_bp) && !BP_IS_EMBEDDED(zio->io_bp) &&
4469 !(zio->io_flags & ZIO_FLAG_NOPWRITE)) {
920dd524
ED
4470 metaslab_fastwrite_unmark(zio->io_spa, zio->io_bp);
4471 }
4472
d164b209
BB
4473 /*
4474 * It is the responsibility of the done callback to ensure that this
4475 * particular zio is no longer discoverable for adoption, and as
4476 * such, cannot acquire any new parents.
4477 */
b128c09f
BB
4478 if (zio->io_done)
4479 zio->io_done(zio);
34dc7c2f 4480
d164b209
BB
4481 mutex_enter(&zio->io_lock);
4482 zio->io_state[ZIO_WAIT_DONE] = 1;
4483 mutex_exit(&zio->io_lock);
34dc7c2f 4484
3dfb57a3
DB
4485 zl = NULL;
4486 for (pio = zio_walk_parents(zio, &zl); pio != NULL; pio = pio_next) {
4487 zio_link_t *remove_zl = zl;
4488 pio_next = zio_walk_parents(zio, &zl);
4489 zio_remove_child(pio, zio, remove_zl);
b128c09f
BB
4490 zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
4491 }
34dc7c2f 4492
b128c09f
BB
4493 if (zio->io_waiter != NULL) {
4494 mutex_enter(&zio->io_lock);
4495 zio->io_executor = NULL;
4496 cv_broadcast(&zio->io_cv);
4497 mutex_exit(&zio->io_lock);
4498 } else {
4499 zio_destroy(zio);
4500 }
34dc7c2f 4501
b128c09f 4502 return (ZIO_PIPELINE_STOP);
34dc7c2f
BB
4503}
4504
4505/*
b128c09f
BB
4506 * ==========================================================================
4507 * I/O pipeline definition
4508 * ==========================================================================
34dc7c2f 4509 */
428870ff 4510static zio_pipe_stage_t *zio_pipeline[] = {
b128c09f 4511 NULL,
b128c09f 4512 zio_read_bp_init,
3dfb57a3 4513 zio_write_bp_init,
428870ff
BB
4514 zio_free_bp_init,
4515 zio_issue_async,
3dfb57a3 4516 zio_write_compress,
b5256303 4517 zio_encrypt,
b128c09f 4518 zio_checksum_generate,
03c6040b 4519 zio_nop_write,
428870ff
BB
4520 zio_ddt_read_start,
4521 zio_ddt_read_done,
4522 zio_ddt_write,
4523 zio_ddt_free,
b128c09f
BB
4524 zio_gang_assemble,
4525 zio_gang_issue,
3dfb57a3 4526 zio_dva_throttle,
b128c09f
BB
4527 zio_dva_allocate,
4528 zio_dva_free,
4529 zio_dva_claim,
4530 zio_ready,
4531 zio_vdev_io_start,
4532 zio_vdev_io_done,
4533 zio_vdev_io_assess,
4534 zio_checksum_verify,
4535 zio_done
4536};
c28b2279 4537
9ae529ec 4538
9ae529ec 4539
9ae529ec 4540
fcff0f35
PD
4541/*
4542 * Compare two zbookmark_phys_t's to see which we would reach first in a
4543 * pre-order traversal of the object tree.
4544 *
4545 * This is simple in every case aside from the meta-dnode object. For all other
4546 * objects, we traverse them in order (object 1 before object 2, and so on).
4547 * However, all of these objects are traversed while traversing object 0, since
4548 * the data it points to is the list of objects. Thus, we need to convert to a
4549 * canonical representation so we can compare meta-dnode bookmarks to
4550 * non-meta-dnode bookmarks.
4551 *
4552 * We do this by calculating "equivalents" for each field of the zbookmark.
4553 * zbookmarks outside of the meta-dnode use their own object and level, and
4554 * calculate the level 0 equivalent (the first L0 blkid that is contained in the
4555 * blocks this bookmark refers to) by multiplying their blkid by their span
4556 * (the number of L0 blocks contained within one block at their level).
4557 * zbookmarks inside the meta-dnode calculate their object equivalent
4558 * (which is L0equiv * dnodes per data block), use 0 for their L0equiv, and use
4559 * level + 1<<31 (any value larger than a level could ever be) for their level.
4560 * This causes them to always compare before a bookmark in their object
4561 * equivalent, compare appropriately to bookmarks in other objects, and to
4562 * compare appropriately to other bookmarks in the meta-dnode.
4563 */
4564int
4565zbookmark_compare(uint16_t dbss1, uint8_t ibs1, uint16_t dbss2, uint8_t ibs2,
4566 const zbookmark_phys_t *zb1, const zbookmark_phys_t *zb2)
4567{
4568 /*
4569 * These variables represent the "equivalent" values for the zbookmark,
4570 * after converting zbookmarks inside the meta dnode to their
4571 * normal-object equivalents.
4572 */
4573 uint64_t zb1obj, zb2obj;
4574 uint64_t zb1L0, zb2L0;
4575 uint64_t zb1level, zb2level;
4576
4577 if (zb1->zb_object == zb2->zb_object &&
4578 zb1->zb_level == zb2->zb_level &&
4579 zb1->zb_blkid == zb2->zb_blkid)
4580 return (0);
9ae529ec 4581
fcff0f35
PD
4582 /*
4583 * BP_SPANB calculates the span in blocks.
4584 */
4585 zb1L0 = (zb1->zb_blkid) * BP_SPANB(ibs1, zb1->zb_level);
4586 zb2L0 = (zb2->zb_blkid) * BP_SPANB(ibs2, zb2->zb_level);
9ae529ec
CS
4587
4588 if (zb1->zb_object == DMU_META_DNODE_OBJECT) {
fcff0f35
PD
4589 zb1obj = zb1L0 * (dbss1 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT));
4590 zb1L0 = 0;
4591 zb1level = zb1->zb_level + COMPARE_META_LEVEL;
4592 } else {
4593 zb1obj = zb1->zb_object;
4594 zb1level = zb1->zb_level;
9ae529ec
CS
4595 }
4596
fcff0f35
PD
4597 if (zb2->zb_object == DMU_META_DNODE_OBJECT) {
4598 zb2obj = zb2L0 * (dbss2 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT));
4599 zb2L0 = 0;
4600 zb2level = zb2->zb_level + COMPARE_META_LEVEL;
4601 } else {
4602 zb2obj = zb2->zb_object;
4603 zb2level = zb2->zb_level;
4604 }
4605
4606 /* Now that we have a canonical representation, do the comparison. */
4607 if (zb1obj != zb2obj)
4608 return (zb1obj < zb2obj ? -1 : 1);
4609 else if (zb1L0 != zb2L0)
4610 return (zb1L0 < zb2L0 ? -1 : 1);
4611 else if (zb1level != zb2level)
4612 return (zb1level > zb2level ? -1 : 1);
4613 /*
4614 * This can (theoretically) happen if the bookmarks have the same object
4615 * and level, but different blkids, if the block sizes are not the same.
4616 * There is presently no way to change the indirect block sizes
4617 */
4618 return (0);
4619}
4620
4621/*
4622 * This function checks the following: given that last_block is the place that
4623 * our traversal stopped last time, does that guarantee that we've visited
4624 * every node under subtree_root? Therefore, we can't just use the raw output
4625 * of zbookmark_compare. We have to pass in a modified version of
4626 * subtree_root; by incrementing the block id, and then checking whether
4627 * last_block is before or equal to that, we can tell whether or not having
4628 * visited last_block implies that all of subtree_root's children have been
4629 * visited.
4630 */
4631boolean_t
4632zbookmark_subtree_completed(const dnode_phys_t *dnp,
4633 const zbookmark_phys_t *subtree_root, const zbookmark_phys_t *last_block)
4634{
4635 zbookmark_phys_t mod_zb = *subtree_root;
4636 mod_zb.zb_blkid++;
4637 ASSERT(last_block->zb_level == 0);
4638
4639 /* The objset_phys_t isn't before anything. */
4640 if (dnp == NULL)
9ae529ec 4641 return (B_FALSE);
fcff0f35
PD
4642
4643 /*
4644 * We pass in 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT) for the
4645 * data block size in sectors, because that variable is only used if
4646 * the bookmark refers to a block in the meta-dnode. Since we don't
4647 * know without examining it what object it refers to, and there's no
4648 * harm in passing in this value in other cases, we always pass it in.
4649 *
4650 * We pass in 0 for the indirect block size shift because zb2 must be
4651 * level 0. The indirect block size is only used to calculate the span
4652 * of the bookmark, but since the bookmark must be level 0, the span is
4653 * always 1, so the math works out.
4654 *
4655 * If you make changes to how the zbookmark_compare code works, be sure
4656 * to make sure that this code still works afterwards.
4657 */
4658 return (zbookmark_compare(dnp->dn_datablkszsec, dnp->dn_indblkshift,
4659 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT), 0, &mod_zb,
4660 last_block) <= 0);
9ae529ec
CS
4661}
4662
c28b2279 4663#if defined(_KERNEL) && defined(HAVE_SPL)
c28b2279 4664EXPORT_SYMBOL(zio_type_name);
81971b13
BB
4665EXPORT_SYMBOL(zio_buf_alloc);
4666EXPORT_SYMBOL(zio_data_buf_alloc);
4667EXPORT_SYMBOL(zio_buf_free);
4668EXPORT_SYMBOL(zio_data_buf_free);
c28b2279 4669
a69052be 4670module_param(zio_delay_max, int, 0644);
c409e464
BB
4671MODULE_PARM_DESC(zio_delay_max, "Max zio millisec delay before posting event");
4672
4673module_param(zio_requeue_io_start_cut_in_line, int, 0644);
4674MODULE_PARM_DESC(zio_requeue_io_start_cut_in_line, "Prioritize requeued I/O");
29dee3ee
CP
4675
4676module_param(zfs_sync_pass_deferred_free, int, 0644);
4677MODULE_PARM_DESC(zfs_sync_pass_deferred_free,
d1d7e268 4678 "Defer frees starting in this pass");
29dee3ee
CP
4679
4680module_param(zfs_sync_pass_dont_compress, int, 0644);
4681MODULE_PARM_DESC(zfs_sync_pass_dont_compress,
d1d7e268 4682 "Don't compress starting in this pass");
29dee3ee
CP
4683
4684module_param(zfs_sync_pass_rewrite, int, 0644);
4685MODULE_PARM_DESC(zfs_sync_pass_rewrite,
d1d7e268 4686 "Rewrite new bps starting in this pass");
3dfb57a3
DB
4687
4688module_param(zio_dva_throttle_enabled, int, 0644);
4689MODULE_PARM_DESC(zio_dva_throttle_enabled,
4690 "Throttle block allocations in the ZIO pipeline");
c28b2279 4691#endif