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