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