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