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