ZTS: Add LUKS sanity test
[zfs.git] / module / zfs / zio.c
blob3c7305e0e724cf7bf4d3f687bb48bde9551c90d1
1 /*
2 * CDDL HEADER START
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.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
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]
19 * CDDL HEADER END
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2011, 2022 by Delphix. All rights reserved.
24 * Copyright (c) 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2017, Intel Corporation.
26 * Copyright (c) 2019, 2023, 2024, Klara Inc.
27 * Copyright (c) 2019, Allan Jude
28 * Copyright (c) 2021, Datto, Inc.
29 * Copyright (c) 2021, 2024 by George Melikov. All rights reserved.
32 #include <sys/sysmacros.h>
33 #include <sys/zfs_context.h>
34 #include <sys/fm/fs/zfs.h>
35 #include <sys/spa.h>
36 #include <sys/txg.h>
37 #include <sys/spa_impl.h>
38 #include <sys/vdev_impl.h>
39 #include <sys/vdev_trim.h>
40 #include <sys/zio_impl.h>
41 #include <sys/zio_compress.h>
42 #include <sys/zio_checksum.h>
43 #include <sys/dmu_objset.h>
44 #include <sys/arc.h>
45 #include <sys/brt.h>
46 #include <sys/ddt.h>
47 #include <sys/blkptr.h>
48 #include <sys/zfeature.h>
49 #include <sys/dsl_scan.h>
50 #include <sys/metaslab_impl.h>
51 #include <sys/time.h>
52 #include <sys/trace_zfs.h>
53 #include <sys/abd.h>
54 #include <sys/dsl_crypt.h>
55 #include <cityhash.h>
58 * ==========================================================================
59 * I/O type descriptions
60 * ==========================================================================
62 const char *const zio_type_name[ZIO_TYPES] = {
64 * Note: Linux kernel thread name length is limited
65 * so these names will differ from upstream open zfs.
67 "z_null", "z_rd", "z_wr", "z_fr", "z_cl", "z_flush", "z_trim"
70 int zio_dva_throttle_enabled = B_TRUE;
71 static int zio_deadman_log_all = B_FALSE;
74 * ==========================================================================
75 * I/O kmem caches
76 * ==========================================================================
78 static kmem_cache_t *zio_cache;
79 static kmem_cache_t *zio_link_cache;
80 kmem_cache_t *zio_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
81 kmem_cache_t *zio_data_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
82 #if defined(ZFS_DEBUG) && !defined(_KERNEL)
83 static uint64_t zio_buf_cache_allocs[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
84 static uint64_t zio_buf_cache_frees[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
85 #endif
87 /* Mark IOs as "slow" if they take longer than 30 seconds */
88 static uint_t zio_slow_io_ms = (30 * MILLISEC);
90 #define BP_SPANB(indblkshift, level) \
91 (((uint64_t)1) << ((level) * ((indblkshift) - SPA_BLKPTRSHIFT)))
92 #define COMPARE_META_LEVEL 0x80000000ul
94 * The following actions directly effect the spa's sync-to-convergence logic.
95 * The values below define the sync pass when we start performing the action.
96 * Care should be taken when changing these values as they directly impact
97 * spa_sync() performance. Tuning these values may introduce subtle performance
98 * pathologies and should only be done in the context of performance analysis.
99 * These tunables will eventually be removed and replaced with #defines once
100 * enough analysis has been done to determine optimal values.
102 * The 'zfs_sync_pass_deferred_free' pass must be greater than 1 to ensure that
103 * regular blocks are not deferred.
105 * Starting in sync pass 8 (zfs_sync_pass_dont_compress), we disable
106 * compression (including of metadata). In practice, we don't have this
107 * many sync passes, so this has no effect.
109 * The original intent was that disabling compression would help the sync
110 * passes to converge. However, in practice disabling compression increases
111 * the average number of sync passes, because when we turn compression off, a
112 * lot of block's size will change and thus we have to re-allocate (not
113 * overwrite) them. It also increases the number of 128KB allocations (e.g.
114 * for indirect blocks and spacemaps) because these will not be compressed.
115 * The 128K allocations are especially detrimental to performance on highly
116 * fragmented systems, which may have very few free segments of this size,
117 * and may need to load new metaslabs to satisfy 128K allocations.
120 /* defer frees starting in this pass */
121 uint_t zfs_sync_pass_deferred_free = 2;
123 /* don't compress starting in this pass */
124 static uint_t zfs_sync_pass_dont_compress = 8;
126 /* rewrite new bps starting in this pass */
127 static uint_t zfs_sync_pass_rewrite = 2;
130 * An allocating zio is one that either currently has the DVA allocate
131 * stage set or will have it later in its lifetime.
133 #define IO_IS_ALLOCATING(zio) ((zio)->io_orig_pipeline & ZIO_STAGE_DVA_ALLOCATE)
136 * Enable smaller cores by excluding metadata
137 * allocations as well.
139 int zio_exclude_metadata = 0;
140 static int zio_requeue_io_start_cut_in_line = 1;
142 #ifdef ZFS_DEBUG
143 static const int zio_buf_debug_limit = 16384;
144 #else
145 static const int zio_buf_debug_limit = 0;
146 #endif
148 static inline void __zio_execute(zio_t *zio);
150 static void zio_taskq_dispatch(zio_t *, zio_taskq_type_t, boolean_t);
152 void
153 zio_init(void)
155 size_t c;
157 zio_cache = kmem_cache_create("zio_cache",
158 sizeof (zio_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
159 zio_link_cache = kmem_cache_create("zio_link_cache",
160 sizeof (zio_link_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
162 for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
163 size_t size = (c + 1) << SPA_MINBLOCKSHIFT;
164 size_t align, cflags, data_cflags;
165 char name[32];
168 * Create cache for each half-power of 2 size, starting from
169 * SPA_MINBLOCKSIZE. It should give us memory space efficiency
170 * of ~7/8, sufficient for transient allocations mostly using
171 * these caches.
173 size_t p2 = size;
174 while (!ISP2(p2))
175 p2 &= p2 - 1;
176 if (!IS_P2ALIGNED(size, p2 / 2))
177 continue;
179 #ifndef _KERNEL
181 * If we are using watchpoints, put each buffer on its own page,
182 * to eliminate the performance overhead of trapping to the
183 * kernel when modifying a non-watched buffer that shares the
184 * page with a watched buffer.
186 if (arc_watch && !IS_P2ALIGNED(size, PAGESIZE))
187 continue;
188 #endif
190 #if defined(__linux__) && defined(_KERNEL)
192 * Workaround issue of Linux vdev_disk.c, in some cases not
193 * linearizing buffers with disk sector crossing a page
194 * boundary. It is fine for hardware, but somehow required by
195 * LUKS. It is not typical for ZFS to produce such buffers, but
196 * it may happen if 6KB block is compressed to 4KB, while still
197 * having 2KB alignment. Banning the 6KB buffers helps vdevs
198 * with ashifh=12.
200 if (size > PAGESIZE && !IS_P2ALIGNED(size, PAGESIZE))
201 continue;
202 #endif
204 if (IS_P2ALIGNED(size, PAGESIZE))
205 align = PAGESIZE;
206 else
207 align = 1 << (highbit64(size ^ (size - 1)) - 1);
209 cflags = (zio_exclude_metadata || size > zio_buf_debug_limit) ?
210 KMC_NODEBUG : 0;
211 data_cflags = KMC_NODEBUG;
212 if (abd_size_alloc_linear(size)) {
213 cflags |= KMC_RECLAIMABLE;
214 data_cflags |= KMC_RECLAIMABLE;
216 if (cflags == data_cflags) {
218 * Resulting kmem caches would be identical.
219 * Save memory by creating only one.
221 (void) snprintf(name, sizeof (name),
222 "zio_buf_comb_%lu", (ulong_t)size);
223 zio_buf_cache[c] = kmem_cache_create(name, size, align,
224 NULL, NULL, NULL, NULL, NULL, cflags);
225 zio_data_buf_cache[c] = zio_buf_cache[c];
226 continue;
228 (void) snprintf(name, sizeof (name), "zio_buf_%lu",
229 (ulong_t)size);
230 zio_buf_cache[c] = kmem_cache_create(name, size, align,
231 NULL, NULL, NULL, NULL, NULL, cflags);
233 (void) snprintf(name, sizeof (name), "zio_data_buf_%lu",
234 (ulong_t)size);
235 zio_data_buf_cache[c] = kmem_cache_create(name, size, align,
236 NULL, NULL, NULL, NULL, NULL, data_cflags);
239 while (--c != 0) {
240 ASSERT(zio_buf_cache[c] != NULL);
241 if (zio_buf_cache[c - 1] == NULL)
242 zio_buf_cache[c - 1] = zio_buf_cache[c];
244 ASSERT(zio_data_buf_cache[c] != NULL);
245 if (zio_data_buf_cache[c - 1] == NULL)
246 zio_data_buf_cache[c - 1] = zio_data_buf_cache[c];
249 zio_inject_init();
251 lz4_init();
254 void
255 zio_fini(void)
257 size_t n = SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT;
259 #if defined(ZFS_DEBUG) && !defined(_KERNEL)
260 for (size_t i = 0; i < n; i++) {
261 if (zio_buf_cache_allocs[i] != zio_buf_cache_frees[i])
262 (void) printf("zio_fini: [%d] %llu != %llu\n",
263 (int)((i + 1) << SPA_MINBLOCKSHIFT),
264 (long long unsigned)zio_buf_cache_allocs[i],
265 (long long unsigned)zio_buf_cache_frees[i]);
267 #endif
270 * The same kmem cache can show up multiple times in both zio_buf_cache
271 * and zio_data_buf_cache. Do a wasteful but trivially correct scan to
272 * sort it out.
274 for (size_t i = 0; i < n; i++) {
275 kmem_cache_t *cache = zio_buf_cache[i];
276 if (cache == NULL)
277 continue;
278 for (size_t j = i; j < n; j++) {
279 if (cache == zio_buf_cache[j])
280 zio_buf_cache[j] = NULL;
281 if (cache == zio_data_buf_cache[j])
282 zio_data_buf_cache[j] = NULL;
284 kmem_cache_destroy(cache);
287 for (size_t i = 0; i < n; i++) {
288 kmem_cache_t *cache = zio_data_buf_cache[i];
289 if (cache == NULL)
290 continue;
291 for (size_t j = i; j < n; j++) {
292 if (cache == zio_data_buf_cache[j])
293 zio_data_buf_cache[j] = NULL;
295 kmem_cache_destroy(cache);
298 for (size_t i = 0; i < n; i++) {
299 VERIFY3P(zio_buf_cache[i], ==, NULL);
300 VERIFY3P(zio_data_buf_cache[i], ==, NULL);
303 kmem_cache_destroy(zio_link_cache);
304 kmem_cache_destroy(zio_cache);
306 zio_inject_fini();
308 lz4_fini();
312 * ==========================================================================
313 * Allocate and free I/O buffers
314 * ==========================================================================
317 #if defined(ZFS_DEBUG) && defined(_KERNEL)
318 #define ZFS_ZIO_BUF_CANARY 1
319 #endif
321 #ifdef ZFS_ZIO_BUF_CANARY
322 static const ulong_t zio_buf_canary = (ulong_t)0xdeadc0dedead210b;
325 * Use empty space after the buffer to detect overflows.
327 * Since zio_init() creates kmem caches only for certain set of buffer sizes,
328 * allocations of different sizes may have some unused space after the data.
329 * Filling part of that space with a known pattern on allocation and checking
330 * it on free should allow us to detect some buffer overflows.
332 static void
333 zio_buf_put_canary(ulong_t *p, size_t size, kmem_cache_t **cache, size_t c)
335 size_t off = P2ROUNDUP(size, sizeof (ulong_t));
336 ulong_t *canary = p + off / sizeof (ulong_t);
337 size_t asize = (c + 1) << SPA_MINBLOCKSHIFT;
338 if (c + 1 < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT &&
339 cache[c] == cache[c + 1])
340 asize = (c + 2) << SPA_MINBLOCKSHIFT;
341 for (; off < asize; canary++, off += sizeof (ulong_t))
342 *canary = zio_buf_canary;
345 static void
346 zio_buf_check_canary(ulong_t *p, size_t size, kmem_cache_t **cache, size_t c)
348 size_t off = P2ROUNDUP(size, sizeof (ulong_t));
349 ulong_t *canary = p + off / sizeof (ulong_t);
350 size_t asize = (c + 1) << SPA_MINBLOCKSHIFT;
351 if (c + 1 < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT &&
352 cache[c] == cache[c + 1])
353 asize = (c + 2) << SPA_MINBLOCKSHIFT;
354 for (; off < asize; canary++, off += sizeof (ulong_t)) {
355 if (unlikely(*canary != zio_buf_canary)) {
356 PANIC("ZIO buffer overflow %p (%zu) + %zu %#lx != %#lx",
357 p, size, (canary - p) * sizeof (ulong_t),
358 *canary, zio_buf_canary);
362 #endif
365 * Use zio_buf_alloc to allocate ZFS metadata. This data will appear in a
366 * crashdump if the kernel panics, so use it judiciously. Obviously, it's
367 * useful to inspect ZFS metadata, but if possible, we should avoid keeping
368 * excess / transient data in-core during a crashdump.
370 void *
371 zio_buf_alloc(size_t size)
373 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
375 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
376 #if defined(ZFS_DEBUG) && !defined(_KERNEL)
377 atomic_add_64(&zio_buf_cache_allocs[c], 1);
378 #endif
380 void *p = kmem_cache_alloc(zio_buf_cache[c], KM_PUSHPAGE);
381 #ifdef ZFS_ZIO_BUF_CANARY
382 zio_buf_put_canary(p, size, zio_buf_cache, c);
383 #endif
384 return (p);
388 * Use zio_data_buf_alloc to allocate data. The data will not appear in a
389 * crashdump if the kernel panics. This exists so that we will limit the amount
390 * of ZFS data that shows up in a kernel crashdump. (Thus reducing the amount
391 * of kernel heap dumped to disk when the kernel panics)
393 void *
394 zio_data_buf_alloc(size_t size)
396 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
398 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
400 void *p = kmem_cache_alloc(zio_data_buf_cache[c], KM_PUSHPAGE);
401 #ifdef ZFS_ZIO_BUF_CANARY
402 zio_buf_put_canary(p, size, zio_data_buf_cache, c);
403 #endif
404 return (p);
407 void
408 zio_buf_free(void *buf, size_t size)
410 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
412 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
413 #if defined(ZFS_DEBUG) && !defined(_KERNEL)
414 atomic_add_64(&zio_buf_cache_frees[c], 1);
415 #endif
417 #ifdef ZFS_ZIO_BUF_CANARY
418 zio_buf_check_canary(buf, size, zio_buf_cache, c);
419 #endif
420 kmem_cache_free(zio_buf_cache[c], buf);
423 void
424 zio_data_buf_free(void *buf, size_t size)
426 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
428 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
430 #ifdef ZFS_ZIO_BUF_CANARY
431 zio_buf_check_canary(buf, size, zio_data_buf_cache, c);
432 #endif
433 kmem_cache_free(zio_data_buf_cache[c], buf);
436 static void
437 zio_abd_free(void *abd, size_t size)
439 (void) size;
440 abd_free((abd_t *)abd);
444 * ==========================================================================
445 * Push and pop I/O transform buffers
446 * ==========================================================================
448 void
449 zio_push_transform(zio_t *zio, abd_t *data, uint64_t size, uint64_t bufsize,
450 zio_transform_func_t *transform)
452 zio_transform_t *zt = kmem_alloc(sizeof (zio_transform_t), KM_SLEEP);
454 zt->zt_orig_abd = zio->io_abd;
455 zt->zt_orig_size = zio->io_size;
456 zt->zt_bufsize = bufsize;
457 zt->zt_transform = transform;
459 zt->zt_next = zio->io_transform_stack;
460 zio->io_transform_stack = zt;
462 zio->io_abd = data;
463 zio->io_size = size;
466 void
467 zio_pop_transforms(zio_t *zio)
469 zio_transform_t *zt;
471 while ((zt = zio->io_transform_stack) != NULL) {
472 if (zt->zt_transform != NULL)
473 zt->zt_transform(zio,
474 zt->zt_orig_abd, zt->zt_orig_size);
476 if (zt->zt_bufsize != 0)
477 abd_free(zio->io_abd);
479 zio->io_abd = zt->zt_orig_abd;
480 zio->io_size = zt->zt_orig_size;
481 zio->io_transform_stack = zt->zt_next;
483 kmem_free(zt, sizeof (zio_transform_t));
488 * ==========================================================================
489 * I/O transform callbacks for subblocks, decompression, and decryption
490 * ==========================================================================
492 static void
493 zio_subblock(zio_t *zio, abd_t *data, uint64_t size)
495 ASSERT(zio->io_size > size);
497 if (zio->io_type == ZIO_TYPE_READ)
498 abd_copy(data, zio->io_abd, size);
501 static void
502 zio_decompress(zio_t *zio, abd_t *data, uint64_t size)
504 if (zio->io_error == 0) {
505 int ret = zio_decompress_data(BP_GET_COMPRESS(zio->io_bp),
506 zio->io_abd, data, zio->io_size, size,
507 &zio->io_prop.zp_complevel);
509 if (zio_injection_enabled && ret == 0)
510 ret = zio_handle_fault_injection(zio, EINVAL);
512 if (ret != 0)
513 zio->io_error = SET_ERROR(EIO);
517 static void
518 zio_decrypt(zio_t *zio, abd_t *data, uint64_t size)
520 int ret;
521 void *tmp;
522 blkptr_t *bp = zio->io_bp;
523 spa_t *spa = zio->io_spa;
524 uint64_t dsobj = zio->io_bookmark.zb_objset;
525 uint64_t lsize = BP_GET_LSIZE(bp);
526 dmu_object_type_t ot = BP_GET_TYPE(bp);
527 uint8_t salt[ZIO_DATA_SALT_LEN];
528 uint8_t iv[ZIO_DATA_IV_LEN];
529 uint8_t mac[ZIO_DATA_MAC_LEN];
530 boolean_t no_crypt = B_FALSE;
532 ASSERT(BP_USES_CRYPT(bp));
533 ASSERT3U(size, !=, 0);
535 if (zio->io_error != 0)
536 return;
539 * Verify the cksum of MACs stored in an indirect bp. It will always
540 * be possible to verify this since it does not require an encryption
541 * key.
543 if (BP_HAS_INDIRECT_MAC_CKSUM(bp)) {
544 zio_crypt_decode_mac_bp(bp, mac);
546 if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF) {
548 * We haven't decompressed the data yet, but
549 * zio_crypt_do_indirect_mac_checksum() requires
550 * decompressed data to be able to parse out the MACs
551 * from the indirect block. We decompress it now and
552 * throw away the result after we are finished.
554 abd_t *abd = abd_alloc_linear(lsize, B_TRUE);
555 ret = zio_decompress_data(BP_GET_COMPRESS(bp),
556 zio->io_abd, abd, zio->io_size, lsize,
557 &zio->io_prop.zp_complevel);
558 if (ret != 0) {
559 abd_free(abd);
560 ret = SET_ERROR(EIO);
561 goto error;
563 ret = zio_crypt_do_indirect_mac_checksum_abd(B_FALSE,
564 abd, lsize, BP_SHOULD_BYTESWAP(bp), mac);
565 abd_free(abd);
566 } else {
567 ret = zio_crypt_do_indirect_mac_checksum_abd(B_FALSE,
568 zio->io_abd, size, BP_SHOULD_BYTESWAP(bp), mac);
570 abd_copy(data, zio->io_abd, size);
572 if (zio_injection_enabled && ot != DMU_OT_DNODE && ret == 0) {
573 ret = zio_handle_decrypt_injection(spa,
574 &zio->io_bookmark, ot, ECKSUM);
576 if (ret != 0)
577 goto error;
579 return;
583 * If this is an authenticated block, just check the MAC. It would be
584 * nice to separate this out into its own flag, but when this was done,
585 * we had run out of bits in what is now zio_flag_t. Future cleanup
586 * could make this a flag bit.
588 if (BP_IS_AUTHENTICATED(bp)) {
589 if (ot == DMU_OT_OBJSET) {
590 ret = spa_do_crypt_objset_mac_abd(B_FALSE, spa,
591 dsobj, zio->io_abd, size, BP_SHOULD_BYTESWAP(bp));
592 } else {
593 zio_crypt_decode_mac_bp(bp, mac);
594 ret = spa_do_crypt_mac_abd(B_FALSE, spa, dsobj,
595 zio->io_abd, size, mac);
596 if (zio_injection_enabled && ret == 0) {
597 ret = zio_handle_decrypt_injection(spa,
598 &zio->io_bookmark, ot, ECKSUM);
601 abd_copy(data, zio->io_abd, size);
603 if (ret != 0)
604 goto error;
606 return;
609 zio_crypt_decode_params_bp(bp, salt, iv);
611 if (ot == DMU_OT_INTENT_LOG) {
612 tmp = abd_borrow_buf_copy(zio->io_abd, sizeof (zil_chain_t));
613 zio_crypt_decode_mac_zil(tmp, mac);
614 abd_return_buf(zio->io_abd, tmp, sizeof (zil_chain_t));
615 } else {
616 zio_crypt_decode_mac_bp(bp, mac);
619 ret = spa_do_crypt_abd(B_FALSE, spa, &zio->io_bookmark, BP_GET_TYPE(bp),
620 BP_GET_DEDUP(bp), BP_SHOULD_BYTESWAP(bp), salt, iv, mac, size, data,
621 zio->io_abd, &no_crypt);
622 if (no_crypt)
623 abd_copy(data, zio->io_abd, size);
625 if (ret != 0)
626 goto error;
628 return;
630 error:
631 /* assert that the key was found unless this was speculative */
632 ASSERT(ret != EACCES || (zio->io_flags & ZIO_FLAG_SPECULATIVE));
635 * If there was a decryption / authentication error return EIO as
636 * the io_error. If this was not a speculative zio, create an ereport.
638 if (ret == ECKSUM) {
639 zio->io_error = SET_ERROR(EIO);
640 if ((zio->io_flags & ZIO_FLAG_SPECULATIVE) == 0) {
641 spa_log_error(spa, &zio->io_bookmark,
642 BP_GET_LOGICAL_BIRTH(zio->io_bp));
643 (void) zfs_ereport_post(FM_EREPORT_ZFS_AUTHENTICATION,
644 spa, NULL, &zio->io_bookmark, zio, 0);
646 } else {
647 zio->io_error = ret;
652 * ==========================================================================
653 * I/O parent/child relationships and pipeline interlocks
654 * ==========================================================================
656 zio_t *
657 zio_walk_parents(zio_t *cio, zio_link_t **zl)
659 list_t *pl = &cio->io_parent_list;
661 *zl = (*zl == NULL) ? list_head(pl) : list_next(pl, *zl);
662 if (*zl == NULL)
663 return (NULL);
665 ASSERT((*zl)->zl_child == cio);
666 return ((*zl)->zl_parent);
669 zio_t *
670 zio_walk_children(zio_t *pio, zio_link_t **zl)
672 list_t *cl = &pio->io_child_list;
674 ASSERT(MUTEX_HELD(&pio->io_lock));
676 *zl = (*zl == NULL) ? list_head(cl) : list_next(cl, *zl);
677 if (*zl == NULL)
678 return (NULL);
680 ASSERT((*zl)->zl_parent == pio);
681 return ((*zl)->zl_child);
684 zio_t *
685 zio_unique_parent(zio_t *cio)
687 zio_link_t *zl = NULL;
688 zio_t *pio = zio_walk_parents(cio, &zl);
690 VERIFY3P(zio_walk_parents(cio, &zl), ==, NULL);
691 return (pio);
694 void
695 zio_add_child(zio_t *pio, zio_t *cio)
698 * Logical I/Os can have logical, gang, or vdev children.
699 * Gang I/Os can have gang or vdev children.
700 * Vdev I/Os can only have vdev children.
701 * The following ASSERT captures all of these constraints.
703 ASSERT3S(cio->io_child_type, <=, pio->io_child_type);
705 /* Parent should not have READY stage if child doesn't have it. */
706 IMPLY((cio->io_pipeline & ZIO_STAGE_READY) == 0 &&
707 (cio->io_child_type != ZIO_CHILD_VDEV),
708 (pio->io_pipeline & ZIO_STAGE_READY) == 0);
710 zio_link_t *zl = kmem_cache_alloc(zio_link_cache, KM_SLEEP);
711 zl->zl_parent = pio;
712 zl->zl_child = cio;
714 mutex_enter(&pio->io_lock);
715 mutex_enter(&cio->io_lock);
717 ASSERT(pio->io_state[ZIO_WAIT_DONE] == 0);
719 uint64_t *countp = pio->io_children[cio->io_child_type];
720 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
721 countp[w] += !cio->io_state[w];
723 list_insert_head(&pio->io_child_list, zl);
724 list_insert_head(&cio->io_parent_list, zl);
726 mutex_exit(&cio->io_lock);
727 mutex_exit(&pio->io_lock);
730 void
731 zio_add_child_first(zio_t *pio, zio_t *cio)
734 * Logical I/Os can have logical, gang, or vdev children.
735 * Gang I/Os can have gang or vdev children.
736 * Vdev I/Os can only have vdev children.
737 * The following ASSERT captures all of these constraints.
739 ASSERT3S(cio->io_child_type, <=, pio->io_child_type);
741 /* Parent should not have READY stage if child doesn't have it. */
742 IMPLY((cio->io_pipeline & ZIO_STAGE_READY) == 0 &&
743 (cio->io_child_type != ZIO_CHILD_VDEV),
744 (pio->io_pipeline & ZIO_STAGE_READY) == 0);
746 zio_link_t *zl = kmem_cache_alloc(zio_link_cache, KM_SLEEP);
747 zl->zl_parent = pio;
748 zl->zl_child = cio;
750 ASSERT(list_is_empty(&cio->io_parent_list));
751 list_insert_head(&cio->io_parent_list, zl);
753 mutex_enter(&pio->io_lock);
755 ASSERT(pio->io_state[ZIO_WAIT_DONE] == 0);
757 uint64_t *countp = pio->io_children[cio->io_child_type];
758 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
759 countp[w] += !cio->io_state[w];
761 list_insert_head(&pio->io_child_list, zl);
763 mutex_exit(&pio->io_lock);
766 static void
767 zio_remove_child(zio_t *pio, zio_t *cio, zio_link_t *zl)
769 ASSERT(zl->zl_parent == pio);
770 ASSERT(zl->zl_child == cio);
772 mutex_enter(&pio->io_lock);
773 mutex_enter(&cio->io_lock);
775 list_remove(&pio->io_child_list, zl);
776 list_remove(&cio->io_parent_list, zl);
778 mutex_exit(&cio->io_lock);
779 mutex_exit(&pio->io_lock);
780 kmem_cache_free(zio_link_cache, zl);
783 static boolean_t
784 zio_wait_for_children(zio_t *zio, uint8_t childbits, enum zio_wait_type wait)
786 boolean_t waiting = B_FALSE;
788 mutex_enter(&zio->io_lock);
789 ASSERT(zio->io_stall == NULL);
790 for (int c = 0; c < ZIO_CHILD_TYPES; c++) {
791 if (!(ZIO_CHILD_BIT_IS_SET(childbits, c)))
792 continue;
794 uint64_t *countp = &zio->io_children[c][wait];
795 if (*countp != 0) {
796 zio->io_stage >>= 1;
797 ASSERT3U(zio->io_stage, !=, ZIO_STAGE_OPEN);
798 zio->io_stall = countp;
799 waiting = B_TRUE;
800 break;
803 mutex_exit(&zio->io_lock);
804 return (waiting);
807 __attribute__((always_inline))
808 static inline void
809 zio_notify_parent(zio_t *pio, zio_t *zio, enum zio_wait_type wait,
810 zio_t **next_to_executep)
812 uint64_t *countp = &pio->io_children[zio->io_child_type][wait];
813 int *errorp = &pio->io_child_error[zio->io_child_type];
815 mutex_enter(&pio->io_lock);
816 if (zio->io_error && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
817 *errorp = zio_worst_error(*errorp, zio->io_error);
818 pio->io_reexecute |= zio->io_reexecute;
819 ASSERT3U(*countp, >, 0);
822 * Propogate the Direct I/O checksum verify failure to the parent.
824 if (zio->io_flags & ZIO_FLAG_DIO_CHKSUM_ERR)
825 pio->io_flags |= ZIO_FLAG_DIO_CHKSUM_ERR;
827 (*countp)--;
829 if (*countp == 0 && pio->io_stall == countp) {
830 zio_taskq_type_t type =
831 pio->io_stage < ZIO_STAGE_VDEV_IO_START ? ZIO_TASKQ_ISSUE :
832 ZIO_TASKQ_INTERRUPT;
833 pio->io_stall = NULL;
834 mutex_exit(&pio->io_lock);
837 * If we can tell the caller to execute this parent next, do
838 * so. We do this if the parent's zio type matches the child's
839 * type, or if it's a zio_null() with no done callback, and so
840 * has no actual work to do. Otherwise dispatch the parent zio
841 * in its own taskq.
843 * Having the caller execute the parent when possible reduces
844 * locking on the zio taskq's, reduces context switch
845 * overhead, and has no recursion penalty. Note that one
846 * read from disk typically causes at least 3 zio's: a
847 * zio_null(), the logical zio_read(), and then a physical
848 * zio. When the physical ZIO completes, we are able to call
849 * zio_done() on all 3 of these zio's from one invocation of
850 * zio_execute() by returning the parent back to
851 * zio_execute(). Since the parent isn't executed until this
852 * thread returns back to zio_execute(), the caller should do
853 * so promptly.
855 * In other cases, dispatching the parent prevents
856 * overflowing the stack when we have deeply nested
857 * parent-child relationships, as we do with the "mega zio"
858 * of writes for spa_sync(), and the chain of ZIL blocks.
860 if (next_to_executep != NULL && *next_to_executep == NULL &&
861 (pio->io_type == zio->io_type ||
862 (pio->io_type == ZIO_TYPE_NULL && !pio->io_done))) {
863 *next_to_executep = pio;
864 } else {
865 zio_taskq_dispatch(pio, type, B_FALSE);
867 } else {
868 mutex_exit(&pio->io_lock);
872 static void
873 zio_inherit_child_errors(zio_t *zio, enum zio_child c)
875 if (zio->io_child_error[c] != 0 && zio->io_error == 0)
876 zio->io_error = zio->io_child_error[c];
880 zio_bookmark_compare(const void *x1, const void *x2)
882 const zio_t *z1 = x1;
883 const zio_t *z2 = x2;
885 if (z1->io_bookmark.zb_objset < z2->io_bookmark.zb_objset)
886 return (-1);
887 if (z1->io_bookmark.zb_objset > z2->io_bookmark.zb_objset)
888 return (1);
890 if (z1->io_bookmark.zb_object < z2->io_bookmark.zb_object)
891 return (-1);
892 if (z1->io_bookmark.zb_object > z2->io_bookmark.zb_object)
893 return (1);
895 if (z1->io_bookmark.zb_level < z2->io_bookmark.zb_level)
896 return (-1);
897 if (z1->io_bookmark.zb_level > z2->io_bookmark.zb_level)
898 return (1);
900 if (z1->io_bookmark.zb_blkid < z2->io_bookmark.zb_blkid)
901 return (-1);
902 if (z1->io_bookmark.zb_blkid > z2->io_bookmark.zb_blkid)
903 return (1);
905 if (z1 < z2)
906 return (-1);
907 if (z1 > z2)
908 return (1);
910 return (0);
914 * ==========================================================================
915 * Create the various types of I/O (read, write, free, etc)
916 * ==========================================================================
918 static zio_t *
919 zio_create(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
920 abd_t *data, uint64_t lsize, uint64_t psize, zio_done_func_t *done,
921 void *private, zio_type_t type, zio_priority_t priority,
922 zio_flag_t flags, vdev_t *vd, uint64_t offset,
923 const zbookmark_phys_t *zb, enum zio_stage stage,
924 enum zio_stage pipeline)
926 zio_t *zio;
928 IMPLY(type != ZIO_TYPE_TRIM, psize <= SPA_MAXBLOCKSIZE);
929 ASSERT(P2PHASE(psize, SPA_MINBLOCKSIZE) == 0);
930 ASSERT(P2PHASE(offset, SPA_MINBLOCKSIZE) == 0);
932 ASSERT(!vd || spa_config_held(spa, SCL_STATE_ALL, RW_READER));
933 ASSERT(!bp || !(flags & ZIO_FLAG_CONFIG_WRITER));
934 ASSERT(vd || stage == ZIO_STAGE_OPEN);
936 IMPLY(lsize != psize, (flags & ZIO_FLAG_RAW_COMPRESS) != 0);
938 zio = kmem_cache_alloc(zio_cache, KM_SLEEP);
939 memset(zio, 0, sizeof (zio_t));
941 mutex_init(&zio->io_lock, NULL, MUTEX_NOLOCKDEP, NULL);
942 cv_init(&zio->io_cv, NULL, CV_DEFAULT, NULL);
944 list_create(&zio->io_parent_list, sizeof (zio_link_t),
945 offsetof(zio_link_t, zl_parent_node));
946 list_create(&zio->io_child_list, sizeof (zio_link_t),
947 offsetof(zio_link_t, zl_child_node));
948 metaslab_trace_init(&zio->io_alloc_list);
950 if (vd != NULL)
951 zio->io_child_type = ZIO_CHILD_VDEV;
952 else if (flags & ZIO_FLAG_GANG_CHILD)
953 zio->io_child_type = ZIO_CHILD_GANG;
954 else if (flags & ZIO_FLAG_DDT_CHILD)
955 zio->io_child_type = ZIO_CHILD_DDT;
956 else
957 zio->io_child_type = ZIO_CHILD_LOGICAL;
959 if (bp != NULL) {
960 if (type != ZIO_TYPE_WRITE ||
961 zio->io_child_type == ZIO_CHILD_DDT) {
962 zio->io_bp_copy = *bp;
963 zio->io_bp = &zio->io_bp_copy; /* so caller can free */
964 } else {
965 zio->io_bp = (blkptr_t *)bp;
967 zio->io_bp_orig = *bp;
968 if (zio->io_child_type == ZIO_CHILD_LOGICAL)
969 zio->io_logical = zio;
970 if (zio->io_child_type > ZIO_CHILD_GANG && BP_IS_GANG(bp))
971 pipeline |= ZIO_GANG_STAGES;
974 zio->io_spa = spa;
975 zio->io_txg = txg;
976 zio->io_done = done;
977 zio->io_private = private;
978 zio->io_type = type;
979 zio->io_priority = priority;
980 zio->io_vd = vd;
981 zio->io_offset = offset;
982 zio->io_orig_abd = zio->io_abd = data;
983 zio->io_orig_size = zio->io_size = psize;
984 zio->io_lsize = lsize;
985 zio->io_orig_flags = zio->io_flags = flags;
986 zio->io_orig_stage = zio->io_stage = stage;
987 zio->io_orig_pipeline = zio->io_pipeline = pipeline;
988 zio->io_pipeline_trace = ZIO_STAGE_OPEN;
989 zio->io_allocator = ZIO_ALLOCATOR_NONE;
991 zio->io_state[ZIO_WAIT_READY] = (stage >= ZIO_STAGE_READY) ||
992 (pipeline & ZIO_STAGE_READY) == 0;
993 zio->io_state[ZIO_WAIT_DONE] = (stage >= ZIO_STAGE_DONE);
995 if (zb != NULL)
996 zio->io_bookmark = *zb;
998 if (pio != NULL) {
999 zio->io_metaslab_class = pio->io_metaslab_class;
1000 if (zio->io_logical == NULL)
1001 zio->io_logical = pio->io_logical;
1002 if (zio->io_child_type == ZIO_CHILD_GANG)
1003 zio->io_gang_leader = pio->io_gang_leader;
1004 zio_add_child_first(pio, zio);
1007 taskq_init_ent(&zio->io_tqent);
1009 return (zio);
1012 void
1013 zio_destroy(zio_t *zio)
1015 metaslab_trace_fini(&zio->io_alloc_list);
1016 list_destroy(&zio->io_parent_list);
1017 list_destroy(&zio->io_child_list);
1018 mutex_destroy(&zio->io_lock);
1019 cv_destroy(&zio->io_cv);
1020 kmem_cache_free(zio_cache, zio);
1024 * ZIO intended to be between others. Provides synchronization at READY
1025 * and DONE pipeline stages and calls the respective callbacks.
1027 zio_t *
1028 zio_null(zio_t *pio, spa_t *spa, vdev_t *vd, zio_done_func_t *done,
1029 void *private, zio_flag_t flags)
1031 zio_t *zio;
1033 zio = zio_create(pio, spa, 0, NULL, NULL, 0, 0, done, private,
1034 ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
1035 ZIO_STAGE_OPEN, ZIO_INTERLOCK_PIPELINE);
1037 return (zio);
1041 * ZIO intended to be a root of a tree. Unlike null ZIO does not have a
1042 * READY pipeline stage (is ready on creation), so it should not be used
1043 * as child of any ZIO that may need waiting for grandchildren READY stage
1044 * (any other ZIO type).
1046 zio_t *
1047 zio_root(spa_t *spa, zio_done_func_t *done, void *private, zio_flag_t flags)
1049 zio_t *zio;
1051 zio = zio_create(NULL, spa, 0, NULL, NULL, 0, 0, done, private,
1052 ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, NULL, 0, NULL,
1053 ZIO_STAGE_OPEN, ZIO_ROOT_PIPELINE);
1055 return (zio);
1058 static int
1059 zfs_blkptr_verify_log(spa_t *spa, const blkptr_t *bp,
1060 enum blk_verify_flag blk_verify, const char *fmt, ...)
1062 va_list adx;
1063 char buf[256];
1065 va_start(adx, fmt);
1066 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
1067 va_end(adx);
1069 zfs_dbgmsg("bad blkptr at %px: "
1070 "DVA[0]=%#llx/%#llx "
1071 "DVA[1]=%#llx/%#llx "
1072 "DVA[2]=%#llx/%#llx "
1073 "prop=%#llx "
1074 "pad=%#llx,%#llx "
1075 "phys_birth=%#llx "
1076 "birth=%#llx "
1077 "fill=%#llx "
1078 "cksum=%#llx/%#llx/%#llx/%#llx",
1080 (long long)bp->blk_dva[0].dva_word[0],
1081 (long long)bp->blk_dva[0].dva_word[1],
1082 (long long)bp->blk_dva[1].dva_word[0],
1083 (long long)bp->blk_dva[1].dva_word[1],
1084 (long long)bp->blk_dva[2].dva_word[0],
1085 (long long)bp->blk_dva[2].dva_word[1],
1086 (long long)bp->blk_prop,
1087 (long long)bp->blk_pad[0],
1088 (long long)bp->blk_pad[1],
1089 (long long)BP_GET_PHYSICAL_BIRTH(bp),
1090 (long long)BP_GET_LOGICAL_BIRTH(bp),
1091 (long long)bp->blk_fill,
1092 (long long)bp->blk_cksum.zc_word[0],
1093 (long long)bp->blk_cksum.zc_word[1],
1094 (long long)bp->blk_cksum.zc_word[2],
1095 (long long)bp->blk_cksum.zc_word[3]);
1096 switch (blk_verify) {
1097 case BLK_VERIFY_HALT:
1098 zfs_panic_recover("%s: %s", spa_name(spa), buf);
1099 break;
1100 case BLK_VERIFY_LOG:
1101 zfs_dbgmsg("%s: %s", spa_name(spa), buf);
1102 break;
1103 case BLK_VERIFY_ONLY:
1104 break;
1107 return (1);
1111 * Verify the block pointer fields contain reasonable values. This means
1112 * it only contains known object types, checksum/compression identifiers,
1113 * block sizes within the maximum allowed limits, valid DVAs, etc.
1115 * If everything checks out B_TRUE is returned. The zfs_blkptr_verify
1116 * argument controls the behavior when an invalid field is detected.
1118 * Values for blk_verify_flag:
1119 * BLK_VERIFY_ONLY: evaluate the block
1120 * BLK_VERIFY_LOG: evaluate the block and log problems
1121 * BLK_VERIFY_HALT: call zfs_panic_recover on error
1123 * Values for blk_config_flag:
1124 * BLK_CONFIG_HELD: caller holds SCL_VDEV for writer
1125 * BLK_CONFIG_NEEDED: caller holds no config lock, SCL_VDEV will be
1126 * obtained for reader
1127 * BLK_CONFIG_SKIP: skip checks which require SCL_VDEV, for better
1128 * performance
1130 boolean_t
1131 zfs_blkptr_verify(spa_t *spa, const blkptr_t *bp,
1132 enum blk_config_flag blk_config, enum blk_verify_flag blk_verify)
1134 int errors = 0;
1136 if (unlikely(!DMU_OT_IS_VALID(BP_GET_TYPE(bp)))) {
1137 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1138 "blkptr at %px has invalid TYPE %llu",
1139 bp, (longlong_t)BP_GET_TYPE(bp));
1141 if (unlikely(BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_FUNCTIONS)) {
1142 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1143 "blkptr at %px has invalid COMPRESS %llu",
1144 bp, (longlong_t)BP_GET_COMPRESS(bp));
1146 if (unlikely(BP_GET_LSIZE(bp) > SPA_MAXBLOCKSIZE)) {
1147 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1148 "blkptr at %px has invalid LSIZE %llu",
1149 bp, (longlong_t)BP_GET_LSIZE(bp));
1151 if (BP_IS_EMBEDDED(bp)) {
1152 if (unlikely(BPE_GET_ETYPE(bp) >= NUM_BP_EMBEDDED_TYPES)) {
1153 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1154 "blkptr at %px has invalid ETYPE %llu",
1155 bp, (longlong_t)BPE_GET_ETYPE(bp));
1157 if (unlikely(BPE_GET_PSIZE(bp) > BPE_PAYLOAD_SIZE)) {
1158 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1159 "blkptr at %px has invalid PSIZE %llu",
1160 bp, (longlong_t)BPE_GET_PSIZE(bp));
1162 return (errors == 0);
1164 if (unlikely(BP_GET_CHECKSUM(bp) >= ZIO_CHECKSUM_FUNCTIONS)) {
1165 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1166 "blkptr at %px has invalid CHECKSUM %llu",
1167 bp, (longlong_t)BP_GET_CHECKSUM(bp));
1169 if (unlikely(BP_GET_PSIZE(bp) > SPA_MAXBLOCKSIZE)) {
1170 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1171 "blkptr at %px has invalid PSIZE %llu",
1172 bp, (longlong_t)BP_GET_PSIZE(bp));
1176 * Do not verify individual DVAs if the config is not trusted. This
1177 * will be done once the zio is executed in vdev_mirror_map_alloc.
1179 if (unlikely(!spa->spa_trust_config))
1180 return (errors == 0);
1182 switch (blk_config) {
1183 case BLK_CONFIG_HELD:
1184 ASSERT(spa_config_held(spa, SCL_VDEV, RW_WRITER));
1185 break;
1186 case BLK_CONFIG_NEEDED:
1187 spa_config_enter(spa, SCL_VDEV, bp, RW_READER);
1188 break;
1189 case BLK_CONFIG_SKIP:
1190 return (errors == 0);
1191 default:
1192 panic("invalid blk_config %u", blk_config);
1196 * Pool-specific checks.
1198 * Note: it would be nice to verify that the logical birth
1199 * and physical birth are not too large. However,
1200 * spa_freeze() allows the birth time of log blocks (and
1201 * dmu_sync()-ed blocks that are in the log) to be arbitrarily
1202 * large.
1204 for (int i = 0; i < BP_GET_NDVAS(bp); i++) {
1205 const dva_t *dva = &bp->blk_dva[i];
1206 uint64_t vdevid = DVA_GET_VDEV(dva);
1208 if (unlikely(vdevid >= spa->spa_root_vdev->vdev_children)) {
1209 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1210 "blkptr at %px DVA %u has invalid VDEV %llu",
1211 bp, i, (longlong_t)vdevid);
1212 continue;
1214 vdev_t *vd = spa->spa_root_vdev->vdev_child[vdevid];
1215 if (unlikely(vd == NULL)) {
1216 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1217 "blkptr at %px DVA %u has invalid VDEV %llu",
1218 bp, i, (longlong_t)vdevid);
1219 continue;
1221 if (unlikely(vd->vdev_ops == &vdev_hole_ops)) {
1222 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1223 "blkptr at %px DVA %u has hole VDEV %llu",
1224 bp, i, (longlong_t)vdevid);
1225 continue;
1227 if (vd->vdev_ops == &vdev_missing_ops) {
1229 * "missing" vdevs are valid during import, but we
1230 * don't have their detailed info (e.g. asize), so
1231 * we can't perform any more checks on them.
1233 continue;
1235 uint64_t offset = DVA_GET_OFFSET(dva);
1236 uint64_t asize = DVA_GET_ASIZE(dva);
1237 if (DVA_GET_GANG(dva))
1238 asize = vdev_gang_header_asize(vd);
1239 if (unlikely(offset + asize > vd->vdev_asize)) {
1240 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1241 "blkptr at %px DVA %u has invalid OFFSET %llu",
1242 bp, i, (longlong_t)offset);
1245 if (blk_config == BLK_CONFIG_NEEDED)
1246 spa_config_exit(spa, SCL_VDEV, bp);
1248 return (errors == 0);
1251 boolean_t
1252 zfs_dva_valid(spa_t *spa, const dva_t *dva, const blkptr_t *bp)
1254 (void) bp;
1255 uint64_t vdevid = DVA_GET_VDEV(dva);
1257 if (vdevid >= spa->spa_root_vdev->vdev_children)
1258 return (B_FALSE);
1260 vdev_t *vd = spa->spa_root_vdev->vdev_child[vdevid];
1261 if (vd == NULL)
1262 return (B_FALSE);
1264 if (vd->vdev_ops == &vdev_hole_ops)
1265 return (B_FALSE);
1267 if (vd->vdev_ops == &vdev_missing_ops) {
1268 return (B_FALSE);
1271 uint64_t offset = DVA_GET_OFFSET(dva);
1272 uint64_t asize = DVA_GET_ASIZE(dva);
1274 if (DVA_GET_GANG(dva))
1275 asize = vdev_gang_header_asize(vd);
1276 if (offset + asize > vd->vdev_asize)
1277 return (B_FALSE);
1279 return (B_TRUE);
1282 zio_t *
1283 zio_read(zio_t *pio, spa_t *spa, const blkptr_t *bp,
1284 abd_t *data, uint64_t size, zio_done_func_t *done, void *private,
1285 zio_priority_t priority, zio_flag_t flags, const zbookmark_phys_t *zb)
1287 zio_t *zio;
1289 zio = zio_create(pio, spa, BP_GET_BIRTH(bp), bp,
1290 data, size, size, done, private,
1291 ZIO_TYPE_READ, priority, flags, NULL, 0, zb,
1292 ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
1293 ZIO_DDT_CHILD_READ_PIPELINE : ZIO_READ_PIPELINE);
1295 return (zio);
1298 zio_t *
1299 zio_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
1300 abd_t *data, uint64_t lsize, uint64_t psize, const zio_prop_t *zp,
1301 zio_done_func_t *ready, zio_done_func_t *children_ready,
1302 zio_done_func_t *done, void *private, zio_priority_t priority,
1303 zio_flag_t flags, const zbookmark_phys_t *zb)
1305 zio_t *zio;
1306 enum zio_stage pipeline = zp->zp_direct_write == B_TRUE ?
1307 ZIO_DIRECT_WRITE_PIPELINE : (flags & ZIO_FLAG_DDT_CHILD) ?
1308 ZIO_DDT_CHILD_WRITE_PIPELINE : ZIO_WRITE_PIPELINE;
1311 zio = zio_create(pio, spa, txg, bp, data, lsize, psize, done, private,
1312 ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb,
1313 ZIO_STAGE_OPEN, pipeline);
1315 zio->io_ready = ready;
1316 zio->io_children_ready = children_ready;
1317 zio->io_prop = *zp;
1320 * Data can be NULL if we are going to call zio_write_override() to
1321 * provide the already-allocated BP. But we may need the data to
1322 * verify a dedup hit (if requested). In this case, don't try to
1323 * dedup (just take the already-allocated BP verbatim). Encrypted
1324 * dedup blocks need data as well so we also disable dedup in this
1325 * case.
1327 if (data == NULL &&
1328 (zio->io_prop.zp_dedup_verify || zio->io_prop.zp_encrypt)) {
1329 zio->io_prop.zp_dedup = zio->io_prop.zp_dedup_verify = B_FALSE;
1332 return (zio);
1335 zio_t *
1336 zio_rewrite(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, abd_t *data,
1337 uint64_t size, zio_done_func_t *done, void *private,
1338 zio_priority_t priority, zio_flag_t flags, zbookmark_phys_t *zb)
1340 zio_t *zio;
1342 zio = zio_create(pio, spa, txg, bp, data, size, size, done, private,
1343 ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_IO_REWRITE, NULL, 0, zb,
1344 ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE);
1346 return (zio);
1349 void
1350 zio_write_override(zio_t *zio, blkptr_t *bp, int copies, boolean_t nopwrite,
1351 boolean_t brtwrite)
1353 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
1354 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1355 ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
1356 ASSERT(zio->io_txg == spa_syncing_txg(zio->io_spa));
1357 ASSERT(!brtwrite || !nopwrite);
1360 * We must reset the io_prop to match the values that existed
1361 * when the bp was first written by dmu_sync() keeping in mind
1362 * that nopwrite and dedup are mutually exclusive.
1364 zio->io_prop.zp_dedup = nopwrite ? B_FALSE : zio->io_prop.zp_dedup;
1365 zio->io_prop.zp_nopwrite = nopwrite;
1366 zio->io_prop.zp_brtwrite = brtwrite;
1367 zio->io_prop.zp_copies = copies;
1368 zio->io_bp_override = bp;
1371 void
1372 zio_free(spa_t *spa, uint64_t txg, const blkptr_t *bp)
1375 (void) zfs_blkptr_verify(spa, bp, BLK_CONFIG_NEEDED, BLK_VERIFY_HALT);
1378 * The check for EMBEDDED is a performance optimization. We
1379 * process the free here (by ignoring it) rather than
1380 * putting it on the list and then processing it in zio_free_sync().
1382 if (BP_IS_EMBEDDED(bp))
1383 return;
1386 * Frees that are for the currently-syncing txg, are not going to be
1387 * deferred, and which will not need to do a read (i.e. not GANG or
1388 * DEDUP), can be processed immediately. Otherwise, put them on the
1389 * in-memory list for later processing.
1391 * Note that we only defer frees after zfs_sync_pass_deferred_free
1392 * when the log space map feature is disabled. [see relevant comment
1393 * in spa_sync_iterate_to_convergence()]
1395 if (BP_IS_GANG(bp) ||
1396 BP_GET_DEDUP(bp) ||
1397 txg != spa->spa_syncing_txg ||
1398 (spa_sync_pass(spa) >= zfs_sync_pass_deferred_free &&
1399 !spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP)) ||
1400 brt_maybe_exists(spa, bp)) {
1401 metaslab_check_free(spa, bp);
1402 bplist_append(&spa->spa_free_bplist[txg & TXG_MASK], bp);
1403 } else {
1404 VERIFY3P(zio_free_sync(NULL, spa, txg, bp, 0), ==, NULL);
1409 * To improve performance, this function may return NULL if we were able
1410 * to do the free immediately. This avoids the cost of creating a zio
1411 * (and linking it to the parent, etc).
1413 zio_t *
1414 zio_free_sync(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
1415 zio_flag_t flags)
1417 ASSERT(!BP_IS_HOLE(bp));
1418 ASSERT(spa_syncing_txg(spa) == txg);
1420 if (BP_IS_EMBEDDED(bp))
1421 return (NULL);
1423 metaslab_check_free(spa, bp);
1424 arc_freed(spa, bp);
1425 dsl_scan_freed(spa, bp);
1427 if (BP_IS_GANG(bp) ||
1428 BP_GET_DEDUP(bp) ||
1429 brt_maybe_exists(spa, bp)) {
1431 * GANG, DEDUP and BRT blocks can induce a read (for the gang
1432 * block header, the DDT or the BRT), so issue them
1433 * asynchronously so that this thread is not tied up.
1435 enum zio_stage stage =
1436 ZIO_FREE_PIPELINE | ZIO_STAGE_ISSUE_ASYNC;
1438 return (zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
1439 BP_GET_PSIZE(bp), NULL, NULL,
1440 ZIO_TYPE_FREE, ZIO_PRIORITY_NOW,
1441 flags, NULL, 0, NULL, ZIO_STAGE_OPEN, stage));
1442 } else {
1443 metaslab_free(spa, bp, txg, B_FALSE);
1444 return (NULL);
1448 zio_t *
1449 zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
1450 zio_done_func_t *done, void *private, zio_flag_t flags)
1452 zio_t *zio;
1454 (void) zfs_blkptr_verify(spa, bp, (flags & ZIO_FLAG_CONFIG_WRITER) ?
1455 BLK_CONFIG_HELD : BLK_CONFIG_NEEDED, BLK_VERIFY_HALT);
1457 if (BP_IS_EMBEDDED(bp))
1458 return (zio_null(pio, spa, NULL, NULL, NULL, 0));
1461 * A claim is an allocation of a specific block. Claims are needed
1462 * to support immediate writes in the intent log. The issue is that
1463 * immediate writes contain committed data, but in a txg that was
1464 * *not* committed. Upon opening the pool after an unclean shutdown,
1465 * the intent log claims all blocks that contain immediate write data
1466 * so that the SPA knows they're in use.
1468 * All claims *must* be resolved in the first txg -- before the SPA
1469 * starts allocating blocks -- so that nothing is allocated twice.
1470 * If txg == 0 we just verify that the block is claimable.
1472 ASSERT3U(BP_GET_LOGICAL_BIRTH(&spa->spa_uberblock.ub_rootbp), <,
1473 spa_min_claim_txg(spa));
1474 ASSERT(txg == spa_min_claim_txg(spa) || txg == 0);
1475 ASSERT(!BP_GET_DEDUP(bp) || !spa_writeable(spa)); /* zdb(8) */
1477 zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
1478 BP_GET_PSIZE(bp), done, private, ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW,
1479 flags, NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE);
1480 ASSERT0(zio->io_queued_timestamp);
1482 return (zio);
1485 zio_t *
1486 zio_trim(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
1487 zio_done_func_t *done, void *private, zio_priority_t priority,
1488 zio_flag_t flags, enum trim_flag trim_flags)
1490 zio_t *zio;
1492 ASSERT0(vd->vdev_children);
1493 ASSERT0(P2PHASE(offset, 1ULL << vd->vdev_ashift));
1494 ASSERT0(P2PHASE(size, 1ULL << vd->vdev_ashift));
1495 ASSERT3U(size, !=, 0);
1497 zio = zio_create(pio, vd->vdev_spa, 0, NULL, NULL, size, size, done,
1498 private, ZIO_TYPE_TRIM, priority, flags | ZIO_FLAG_PHYSICAL,
1499 vd, offset, NULL, ZIO_STAGE_OPEN, ZIO_TRIM_PIPELINE);
1500 zio->io_trim_flags = trim_flags;
1502 return (zio);
1505 zio_t *
1506 zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
1507 abd_t *data, int checksum, zio_done_func_t *done, void *private,
1508 zio_priority_t priority, zio_flag_t flags, boolean_t labels)
1510 zio_t *zio;
1512 ASSERT(vd->vdev_children == 0);
1513 ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
1514 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
1515 ASSERT3U(offset + size, <=, vd->vdev_psize);
1517 zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, size, done,
1518 private, ZIO_TYPE_READ, priority, flags | ZIO_FLAG_PHYSICAL, vd,
1519 offset, NULL, ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE);
1521 zio->io_prop.zp_checksum = checksum;
1523 return (zio);
1526 zio_t *
1527 zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
1528 abd_t *data, int checksum, zio_done_func_t *done, void *private,
1529 zio_priority_t priority, zio_flag_t flags, boolean_t labels)
1531 zio_t *zio;
1533 ASSERT(vd->vdev_children == 0);
1534 ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
1535 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
1536 ASSERT3U(offset + size, <=, vd->vdev_psize);
1538 zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, size, done,
1539 private, ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_PHYSICAL, vd,
1540 offset, NULL, ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE);
1542 zio->io_prop.zp_checksum = checksum;
1544 if (zio_checksum_table[checksum].ci_flags & ZCHECKSUM_FLAG_EMBEDDED) {
1546 * zec checksums are necessarily destructive -- they modify
1547 * the end of the write buffer to hold the verifier/checksum.
1548 * Therefore, we must make a local copy in case the data is
1549 * being written to multiple places in parallel.
1551 abd_t *wbuf = abd_alloc_sametype(data, size);
1552 abd_copy(wbuf, data, size);
1554 zio_push_transform(zio, wbuf, size, size, NULL);
1557 return (zio);
1561 * Create a child I/O to do some work for us.
1563 zio_t *
1564 zio_vdev_child_io(zio_t *pio, blkptr_t *bp, vdev_t *vd, uint64_t offset,
1565 abd_t *data, uint64_t size, int type, zio_priority_t priority,
1566 zio_flag_t flags, zio_done_func_t *done, void *private)
1568 enum zio_stage pipeline = ZIO_VDEV_CHILD_PIPELINE;
1569 zio_t *zio;
1572 * vdev child I/Os do not propagate their error to the parent.
1573 * Therefore, for correct operation the caller *must* check for
1574 * and handle the error in the child i/o's done callback.
1575 * The only exceptions are i/os that we don't care about
1576 * (OPTIONAL or REPAIR).
1578 ASSERT((flags & ZIO_FLAG_OPTIONAL) || (flags & ZIO_FLAG_IO_REPAIR) ||
1579 done != NULL);
1581 if (type == ZIO_TYPE_READ && bp != NULL) {
1583 * If we have the bp, then the child should perform the
1584 * checksum and the parent need not. This pushes error
1585 * detection as close to the leaves as possible and
1586 * eliminates redundant checksums in the interior nodes.
1588 pipeline |= ZIO_STAGE_CHECKSUM_VERIFY;
1589 pio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
1591 * We never allow the mirror VDEV to attempt reading from any
1592 * additional data copies after the first Direct I/O checksum
1593 * verify failure. This is to avoid bad data being written out
1594 * through the mirror during self healing. See comment in
1595 * vdev_mirror_io_done() for more details.
1597 ASSERT0(pio->io_flags & ZIO_FLAG_DIO_CHKSUM_ERR);
1598 } else if (type == ZIO_TYPE_WRITE &&
1599 pio->io_prop.zp_direct_write == B_TRUE) {
1601 * By default we only will verify checksums for Direct I/O
1602 * writes for Linux. FreeBSD is able to place user pages under
1603 * write protection before issuing them to the ZIO pipeline.
1605 * Checksum validation errors will only be reported through
1606 * the top-level VDEV, which is set by this child ZIO.
1608 ASSERT3P(bp, !=, NULL);
1609 ASSERT3U(pio->io_child_type, ==, ZIO_CHILD_LOGICAL);
1610 pipeline |= ZIO_STAGE_DIO_CHECKSUM_VERIFY;
1613 if (vd->vdev_ops->vdev_op_leaf) {
1614 ASSERT0(vd->vdev_children);
1615 offset += VDEV_LABEL_START_SIZE;
1618 flags |= ZIO_VDEV_CHILD_FLAGS(pio);
1621 * If we've decided to do a repair, the write is not speculative --
1622 * even if the original read was.
1624 if (flags & ZIO_FLAG_IO_REPAIR)
1625 flags &= ~ZIO_FLAG_SPECULATIVE;
1628 * If we're creating a child I/O that is not associated with a
1629 * top-level vdev, then the child zio is not an allocating I/O.
1630 * If this is a retried I/O then we ignore it since we will
1631 * have already processed the original allocating I/O.
1633 if (flags & ZIO_FLAG_IO_ALLOCATING &&
1634 (vd != vd->vdev_top || (flags & ZIO_FLAG_IO_RETRY))) {
1635 ASSERT(pio->io_metaslab_class != NULL);
1636 ASSERT(pio->io_metaslab_class->mc_alloc_throttle_enabled);
1637 ASSERT(type == ZIO_TYPE_WRITE);
1638 ASSERT(priority == ZIO_PRIORITY_ASYNC_WRITE);
1639 ASSERT(!(flags & ZIO_FLAG_IO_REPAIR));
1640 ASSERT(!(pio->io_flags & ZIO_FLAG_IO_REWRITE) ||
1641 pio->io_child_type == ZIO_CHILD_GANG);
1643 flags &= ~ZIO_FLAG_IO_ALLOCATING;
1646 zio = zio_create(pio, pio->io_spa, pio->io_txg, bp, data, size, size,
1647 done, private, type, priority, flags, vd, offset, &pio->io_bookmark,
1648 ZIO_STAGE_VDEV_IO_START >> 1, pipeline);
1649 ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
1651 return (zio);
1654 zio_t *
1655 zio_vdev_delegated_io(vdev_t *vd, uint64_t offset, abd_t *data, uint64_t size,
1656 zio_type_t type, zio_priority_t priority, zio_flag_t flags,
1657 zio_done_func_t *done, void *private)
1659 zio_t *zio;
1661 ASSERT(vd->vdev_ops->vdev_op_leaf);
1663 zio = zio_create(NULL, vd->vdev_spa, 0, NULL,
1664 data, size, size, done, private, type, priority,
1665 flags | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY | ZIO_FLAG_DELEGATED,
1666 vd, offset, NULL,
1667 ZIO_STAGE_VDEV_IO_START >> 1, ZIO_VDEV_CHILD_PIPELINE);
1669 return (zio);
1674 * Send a flush command to the given vdev. Unlike most zio creation functions,
1675 * the flush zios are issued immediately. You can wait on pio to pause until
1676 * the flushes complete.
1678 void
1679 zio_flush(zio_t *pio, vdev_t *vd)
1681 const zio_flag_t flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE |
1682 ZIO_FLAG_DONT_RETRY;
1684 if (vd->vdev_nowritecache)
1685 return;
1687 if (vd->vdev_children == 0) {
1688 zio_nowait(zio_create(pio, vd->vdev_spa, 0, NULL, NULL, 0, 0,
1689 NULL, NULL, ZIO_TYPE_FLUSH, ZIO_PRIORITY_NOW, flags, vd, 0,
1690 NULL, ZIO_STAGE_OPEN, ZIO_FLUSH_PIPELINE));
1691 } else {
1692 for (uint64_t c = 0; c < vd->vdev_children; c++)
1693 zio_flush(pio, vd->vdev_child[c]);
1697 void
1698 zio_shrink(zio_t *zio, uint64_t size)
1700 ASSERT3P(zio->io_executor, ==, NULL);
1701 ASSERT3U(zio->io_orig_size, ==, zio->io_size);
1702 ASSERT3U(size, <=, zio->io_size);
1705 * We don't shrink for raidz because of problems with the
1706 * reconstruction when reading back less than the block size.
1707 * Note, BP_IS_RAIDZ() assumes no compression.
1709 ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
1710 if (!BP_IS_RAIDZ(zio->io_bp)) {
1711 /* we are not doing a raw write */
1712 ASSERT3U(zio->io_size, ==, zio->io_lsize);
1713 zio->io_orig_size = zio->io_size = zio->io_lsize = size;
1718 * Round provided allocation size up to a value that can be allocated
1719 * by at least some vdev(s) in the pool with minimum or no additional
1720 * padding and without extra space usage on others
1722 static uint64_t
1723 zio_roundup_alloc_size(spa_t *spa, uint64_t size)
1725 if (size > spa->spa_min_alloc)
1726 return (roundup(size, spa->spa_gcd_alloc));
1727 return (spa->spa_min_alloc);
1730 size_t
1731 zio_get_compression_max_size(enum zio_compress compress, uint64_t gcd_alloc,
1732 uint64_t min_alloc, size_t s_len)
1734 size_t d_len;
1736 /* minimum 12.5% must be saved (legacy value, may be changed later) */
1737 d_len = s_len - (s_len >> 3);
1739 /* ZLE can't use exactly d_len bytes, it needs more, so ignore it */
1740 if (compress == ZIO_COMPRESS_ZLE)
1741 return (d_len);
1743 d_len = d_len - d_len % gcd_alloc;
1745 if (d_len < min_alloc)
1746 return (BPE_PAYLOAD_SIZE);
1747 return (d_len);
1751 * ==========================================================================
1752 * Prepare to read and write logical blocks
1753 * ==========================================================================
1756 static zio_t *
1757 zio_read_bp_init(zio_t *zio)
1759 blkptr_t *bp = zio->io_bp;
1760 uint64_t psize =
1761 BP_IS_EMBEDDED(bp) ? BPE_GET_PSIZE(bp) : BP_GET_PSIZE(bp);
1763 ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy);
1765 if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF &&
1766 zio->io_child_type == ZIO_CHILD_LOGICAL &&
1767 !(zio->io_flags & ZIO_FLAG_RAW_COMPRESS)) {
1768 zio_push_transform(zio, abd_alloc_sametype(zio->io_abd, psize),
1769 psize, psize, zio_decompress);
1772 if (((BP_IS_PROTECTED(bp) && !(zio->io_flags & ZIO_FLAG_RAW_ENCRYPT)) ||
1773 BP_HAS_INDIRECT_MAC_CKSUM(bp)) &&
1774 zio->io_child_type == ZIO_CHILD_LOGICAL) {
1775 zio_push_transform(zio, abd_alloc_sametype(zio->io_abd, psize),
1776 psize, psize, zio_decrypt);
1779 if (BP_IS_EMBEDDED(bp) && BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA) {
1780 int psize = BPE_GET_PSIZE(bp);
1781 void *data = abd_borrow_buf(zio->io_abd, psize);
1783 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1784 decode_embedded_bp_compressed(bp, data);
1785 abd_return_buf_copy(zio->io_abd, data, psize);
1786 } else {
1787 ASSERT(!BP_IS_EMBEDDED(bp));
1790 if (BP_GET_DEDUP(bp) && zio->io_child_type == ZIO_CHILD_LOGICAL)
1791 zio->io_pipeline = ZIO_DDT_READ_PIPELINE;
1793 return (zio);
1796 static zio_t *
1797 zio_write_bp_init(zio_t *zio)
1799 if (!IO_IS_ALLOCATING(zio))
1800 return (zio);
1802 ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
1804 if (zio->io_bp_override) {
1805 blkptr_t *bp = zio->io_bp;
1806 zio_prop_t *zp = &zio->io_prop;
1808 ASSERT(BP_GET_LOGICAL_BIRTH(bp) != zio->io_txg);
1810 *bp = *zio->io_bp_override;
1811 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1813 if (zp->zp_brtwrite)
1814 return (zio);
1816 ASSERT(!BP_GET_DEDUP(zio->io_bp_override));
1818 if (BP_IS_EMBEDDED(bp))
1819 return (zio);
1822 * If we've been overridden and nopwrite is set then
1823 * set the flag accordingly to indicate that a nopwrite
1824 * has already occurred.
1826 if (!BP_IS_HOLE(bp) && zp->zp_nopwrite) {
1827 ASSERT(!zp->zp_dedup);
1828 ASSERT3U(BP_GET_CHECKSUM(bp), ==, zp->zp_checksum);
1829 zio->io_flags |= ZIO_FLAG_NOPWRITE;
1830 return (zio);
1833 ASSERT(!zp->zp_nopwrite);
1835 if (BP_IS_HOLE(bp) || !zp->zp_dedup)
1836 return (zio);
1838 ASSERT((zio_checksum_table[zp->zp_checksum].ci_flags &
1839 ZCHECKSUM_FLAG_DEDUP) || zp->zp_dedup_verify);
1841 if (BP_GET_CHECKSUM(bp) == zp->zp_checksum &&
1842 !zp->zp_encrypt) {
1843 BP_SET_DEDUP(bp, 1);
1844 zio->io_pipeline |= ZIO_STAGE_DDT_WRITE;
1845 return (zio);
1849 * We were unable to handle this as an override bp, treat
1850 * it as a regular write I/O.
1852 zio->io_bp_override = NULL;
1853 *bp = zio->io_bp_orig;
1854 zio->io_pipeline = zio->io_orig_pipeline;
1857 return (zio);
1860 static zio_t *
1861 zio_write_compress(zio_t *zio)
1863 spa_t *spa = zio->io_spa;
1864 zio_prop_t *zp = &zio->io_prop;
1865 enum zio_compress compress = zp->zp_compress;
1866 blkptr_t *bp = zio->io_bp;
1867 uint64_t lsize = zio->io_lsize;
1868 uint64_t psize = zio->io_size;
1869 uint32_t pass = 1;
1872 * If our children haven't all reached the ready stage,
1873 * wait for them and then repeat this pipeline stage.
1875 if (zio_wait_for_children(zio, ZIO_CHILD_LOGICAL_BIT |
1876 ZIO_CHILD_GANG_BIT, ZIO_WAIT_READY)) {
1877 return (NULL);
1880 if (!IO_IS_ALLOCATING(zio))
1881 return (zio);
1883 if (zio->io_children_ready != NULL) {
1885 * Now that all our children are ready, run the callback
1886 * associated with this zio in case it wants to modify the
1887 * data to be written.
1889 ASSERT3U(zp->zp_level, >, 0);
1890 zio->io_children_ready(zio);
1893 ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
1894 ASSERT(zio->io_bp_override == NULL);
1896 if (!BP_IS_HOLE(bp) && BP_GET_LOGICAL_BIRTH(bp) == zio->io_txg) {
1898 * We're rewriting an existing block, which means we're
1899 * working on behalf of spa_sync(). For spa_sync() to
1900 * converge, it must eventually be the case that we don't
1901 * have to allocate new blocks. But compression changes
1902 * the blocksize, which forces a reallocate, and makes
1903 * convergence take longer. Therefore, after the first
1904 * few passes, stop compressing to ensure convergence.
1906 pass = spa_sync_pass(spa);
1908 ASSERT(zio->io_txg == spa_syncing_txg(spa));
1909 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1910 ASSERT(!BP_GET_DEDUP(bp));
1912 if (pass >= zfs_sync_pass_dont_compress)
1913 compress = ZIO_COMPRESS_OFF;
1915 /* Make sure someone doesn't change their mind on overwrites */
1916 ASSERT(BP_IS_EMBEDDED(bp) || BP_IS_GANG(bp) ||
1917 MIN(zp->zp_copies, spa_max_replication(spa))
1918 == BP_GET_NDVAS(bp));
1921 /* If it's a compressed write that is not raw, compress the buffer. */
1922 if (compress != ZIO_COMPRESS_OFF &&
1923 !(zio->io_flags & ZIO_FLAG_RAW_COMPRESS)) {
1924 abd_t *cabd = NULL;
1925 if (abd_cmp_zero(zio->io_abd, lsize) == 0)
1926 psize = 0;
1927 else if (compress == ZIO_COMPRESS_EMPTY)
1928 psize = lsize;
1929 else
1930 psize = zio_compress_data(compress, zio->io_abd, &cabd,
1931 lsize,
1932 zio_get_compression_max_size(compress,
1933 spa->spa_gcd_alloc, spa->spa_min_alloc, lsize),
1934 zp->zp_complevel);
1935 if (psize == 0) {
1936 compress = ZIO_COMPRESS_OFF;
1937 } else if (psize >= lsize) {
1938 compress = ZIO_COMPRESS_OFF;
1939 if (cabd != NULL)
1940 abd_free(cabd);
1941 } else if (!zp->zp_dedup && !zp->zp_encrypt &&
1942 psize <= BPE_PAYLOAD_SIZE &&
1943 zp->zp_level == 0 && !DMU_OT_HAS_FILL(zp->zp_type) &&
1944 spa_feature_is_enabled(spa, SPA_FEATURE_EMBEDDED_DATA)) {
1945 void *cbuf = abd_borrow_buf_copy(cabd, lsize);
1946 encode_embedded_bp_compressed(bp,
1947 cbuf, compress, lsize, psize);
1948 BPE_SET_ETYPE(bp, BP_EMBEDDED_TYPE_DATA);
1949 BP_SET_TYPE(bp, zio->io_prop.zp_type);
1950 BP_SET_LEVEL(bp, zio->io_prop.zp_level);
1951 abd_return_buf(cabd, cbuf, lsize);
1952 abd_free(cabd);
1953 BP_SET_LOGICAL_BIRTH(bp, zio->io_txg);
1954 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1955 ASSERT(spa_feature_is_active(spa,
1956 SPA_FEATURE_EMBEDDED_DATA));
1957 return (zio);
1958 } else {
1960 * Round compressed size up to the minimum allocation
1961 * size of the smallest-ashift device, and zero the
1962 * tail. This ensures that the compressed size of the
1963 * BP (and thus compressratio property) are correct,
1964 * in that we charge for the padding used to fill out
1965 * the last sector.
1967 size_t rounded = (size_t)zio_roundup_alloc_size(spa,
1968 psize);
1969 if (rounded >= lsize) {
1970 compress = ZIO_COMPRESS_OFF;
1971 abd_free(cabd);
1972 psize = lsize;
1973 } else {
1974 abd_zero_off(cabd, psize, rounded - psize);
1975 psize = rounded;
1976 zio_push_transform(zio, cabd,
1977 psize, lsize, NULL);
1982 * We were unable to handle this as an override bp, treat
1983 * it as a regular write I/O.
1985 zio->io_bp_override = NULL;
1986 *bp = zio->io_bp_orig;
1987 zio->io_pipeline = zio->io_orig_pipeline;
1989 } else if ((zio->io_flags & ZIO_FLAG_RAW_ENCRYPT) != 0 &&
1990 zp->zp_type == DMU_OT_DNODE) {
1992 * The DMU actually relies on the zio layer's compression
1993 * to free metadnode blocks that have had all contained
1994 * dnodes freed. As a result, even when doing a raw
1995 * receive, we must check whether the block can be compressed
1996 * to a hole.
1998 if (abd_cmp_zero(zio->io_abd, lsize) == 0) {
1999 psize = 0;
2000 compress = ZIO_COMPRESS_OFF;
2001 } else {
2002 psize = lsize;
2004 } else if (zio->io_flags & ZIO_FLAG_RAW_COMPRESS &&
2005 !(zio->io_flags & ZIO_FLAG_RAW_ENCRYPT)) {
2007 * If we are raw receiving an encrypted dataset we should not
2008 * take this codepath because it will change the on-disk block
2009 * and decryption will fail.
2011 size_t rounded = MIN((size_t)zio_roundup_alloc_size(spa, psize),
2012 lsize);
2014 if (rounded != psize) {
2015 abd_t *cdata = abd_alloc_linear(rounded, B_TRUE);
2016 abd_zero_off(cdata, psize, rounded - psize);
2017 abd_copy_off(cdata, zio->io_abd, 0, 0, psize);
2018 psize = rounded;
2019 zio_push_transform(zio, cdata,
2020 psize, rounded, NULL);
2022 } else {
2023 ASSERT3U(psize, !=, 0);
2027 * The final pass of spa_sync() must be all rewrites, but the first
2028 * few passes offer a trade-off: allocating blocks defers convergence,
2029 * but newly allocated blocks are sequential, so they can be written
2030 * to disk faster. Therefore, we allow the first few passes of
2031 * spa_sync() to allocate new blocks, but force rewrites after that.
2032 * There should only be a handful of blocks after pass 1 in any case.
2034 if (!BP_IS_HOLE(bp) && BP_GET_LOGICAL_BIRTH(bp) == zio->io_txg &&
2035 BP_GET_PSIZE(bp) == psize &&
2036 pass >= zfs_sync_pass_rewrite) {
2037 VERIFY3U(psize, !=, 0);
2038 enum zio_stage gang_stages = zio->io_pipeline & ZIO_GANG_STAGES;
2040 zio->io_pipeline = ZIO_REWRITE_PIPELINE | gang_stages;
2041 zio->io_flags |= ZIO_FLAG_IO_REWRITE;
2042 } else {
2043 BP_ZERO(bp);
2044 zio->io_pipeline = ZIO_WRITE_PIPELINE;
2047 if (psize == 0) {
2048 if (BP_GET_LOGICAL_BIRTH(&zio->io_bp_orig) != 0 &&
2049 spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) {
2050 BP_SET_LSIZE(bp, lsize);
2051 BP_SET_TYPE(bp, zp->zp_type);
2052 BP_SET_LEVEL(bp, zp->zp_level);
2053 BP_SET_BIRTH(bp, zio->io_txg, 0);
2055 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2056 } else {
2057 ASSERT(zp->zp_checksum != ZIO_CHECKSUM_GANG_HEADER);
2058 BP_SET_LSIZE(bp, lsize);
2059 BP_SET_TYPE(bp, zp->zp_type);
2060 BP_SET_LEVEL(bp, zp->zp_level);
2061 BP_SET_PSIZE(bp, psize);
2062 BP_SET_COMPRESS(bp, compress);
2063 BP_SET_CHECKSUM(bp, zp->zp_checksum);
2064 BP_SET_DEDUP(bp, zp->zp_dedup);
2065 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
2066 if (zp->zp_dedup) {
2067 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2068 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
2069 ASSERT(!zp->zp_encrypt ||
2070 DMU_OT_IS_ENCRYPTED(zp->zp_type));
2071 zio->io_pipeline = ZIO_DDT_WRITE_PIPELINE;
2073 if (zp->zp_nopwrite) {
2074 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2075 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
2076 zio->io_pipeline |= ZIO_STAGE_NOP_WRITE;
2079 return (zio);
2082 static zio_t *
2083 zio_free_bp_init(zio_t *zio)
2085 blkptr_t *bp = zio->io_bp;
2087 if (zio->io_child_type == ZIO_CHILD_LOGICAL) {
2088 if (BP_GET_DEDUP(bp))
2089 zio->io_pipeline = ZIO_DDT_FREE_PIPELINE;
2092 ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy);
2094 return (zio);
2098 * ==========================================================================
2099 * Execute the I/O pipeline
2100 * ==========================================================================
2103 static void
2104 zio_taskq_dispatch(zio_t *zio, zio_taskq_type_t q, boolean_t cutinline)
2106 spa_t *spa = zio->io_spa;
2107 zio_type_t t = zio->io_type;
2110 * If we're a config writer or a probe, the normal issue and
2111 * interrupt threads may all be blocked waiting for the config lock.
2112 * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL.
2114 if (zio->io_flags & (ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_PROBE))
2115 t = ZIO_TYPE_NULL;
2118 * A similar issue exists for the L2ARC write thread until L2ARC 2.0.
2120 if (t == ZIO_TYPE_WRITE && zio->io_vd && zio->io_vd->vdev_aux)
2121 t = ZIO_TYPE_NULL;
2124 * If this is a high priority I/O, then use the high priority taskq if
2125 * available or cut the line otherwise.
2127 if (zio->io_priority == ZIO_PRIORITY_SYNC_WRITE) {
2128 if (spa->spa_zio_taskq[t][q + 1].stqs_count != 0)
2129 q++;
2130 else
2131 cutinline = B_TRUE;
2134 ASSERT3U(q, <, ZIO_TASKQ_TYPES);
2136 spa_taskq_dispatch(spa, t, q, zio_execute, zio, cutinline);
2139 static boolean_t
2140 zio_taskq_member(zio_t *zio, zio_taskq_type_t q)
2142 spa_t *spa = zio->io_spa;
2144 taskq_t *tq = taskq_of_curthread();
2146 for (zio_type_t t = 0; t < ZIO_TYPES; t++) {
2147 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
2148 uint_t i;
2149 for (i = 0; i < tqs->stqs_count; i++) {
2150 if (tqs->stqs_taskq[i] == tq)
2151 return (B_TRUE);
2155 return (B_FALSE);
2158 static zio_t *
2159 zio_issue_async(zio_t *zio)
2161 ASSERT((zio->io_type != ZIO_TYPE_WRITE) || ZIO_HAS_ALLOCATOR(zio));
2162 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
2163 return (NULL);
2166 void
2167 zio_interrupt(void *zio)
2169 zio_taskq_dispatch(zio, ZIO_TASKQ_INTERRUPT, B_FALSE);
2172 void
2173 zio_delay_interrupt(zio_t *zio)
2176 * The timeout_generic() function isn't defined in userspace, so
2177 * rather than trying to implement the function, the zio delay
2178 * functionality has been disabled for userspace builds.
2181 #ifdef _KERNEL
2183 * If io_target_timestamp is zero, then no delay has been registered
2184 * for this IO, thus jump to the end of this function and "skip" the
2185 * delay; issuing it directly to the zio layer.
2187 if (zio->io_target_timestamp != 0) {
2188 hrtime_t now = gethrtime();
2190 if (now >= zio->io_target_timestamp) {
2192 * This IO has already taken longer than the target
2193 * delay to complete, so we don't want to delay it
2194 * any longer; we "miss" the delay and issue it
2195 * directly to the zio layer. This is likely due to
2196 * the target latency being set to a value less than
2197 * the underlying hardware can satisfy (e.g. delay
2198 * set to 1ms, but the disks take 10ms to complete an
2199 * IO request).
2202 DTRACE_PROBE2(zio__delay__miss, zio_t *, zio,
2203 hrtime_t, now);
2205 zio_interrupt(zio);
2206 } else {
2207 taskqid_t tid;
2208 hrtime_t diff = zio->io_target_timestamp - now;
2209 clock_t expire_at_tick = ddi_get_lbolt() +
2210 NSEC_TO_TICK(diff);
2212 DTRACE_PROBE3(zio__delay__hit, zio_t *, zio,
2213 hrtime_t, now, hrtime_t, diff);
2215 if (NSEC_TO_TICK(diff) == 0) {
2216 /* Our delay is less than a jiffy - just spin */
2217 zfs_sleep_until(zio->io_target_timestamp);
2218 zio_interrupt(zio);
2219 } else {
2221 * Use taskq_dispatch_delay() in the place of
2222 * OpenZFS's timeout_generic().
2224 tid = taskq_dispatch_delay(system_taskq,
2225 zio_interrupt, zio, TQ_NOSLEEP,
2226 expire_at_tick);
2227 if (tid == TASKQID_INVALID) {
2229 * Couldn't allocate a task. Just
2230 * finish the zio without a delay.
2232 zio_interrupt(zio);
2236 return;
2238 #endif
2239 DTRACE_PROBE1(zio__delay__skip, zio_t *, zio);
2240 zio_interrupt(zio);
2243 static void
2244 zio_deadman_impl(zio_t *pio, int ziodepth)
2246 zio_t *cio, *cio_next;
2247 zio_link_t *zl = NULL;
2248 vdev_t *vd = pio->io_vd;
2250 if (zio_deadman_log_all || (vd != NULL && vd->vdev_ops->vdev_op_leaf)) {
2251 vdev_queue_t *vq = vd ? &vd->vdev_queue : NULL;
2252 zbookmark_phys_t *zb = &pio->io_bookmark;
2253 uint64_t delta = gethrtime() - pio->io_timestamp;
2254 uint64_t failmode = spa_get_deadman_failmode(pio->io_spa);
2256 zfs_dbgmsg("slow zio[%d]: zio=%px timestamp=%llu "
2257 "delta=%llu queued=%llu io=%llu "
2258 "path=%s "
2259 "last=%llu type=%d "
2260 "priority=%d flags=0x%llx stage=0x%x "
2261 "pipeline=0x%x pipeline-trace=0x%x "
2262 "objset=%llu object=%llu "
2263 "level=%llu blkid=%llu "
2264 "offset=%llu size=%llu "
2265 "error=%d",
2266 ziodepth, pio, pio->io_timestamp,
2267 (u_longlong_t)delta, pio->io_delta, pio->io_delay,
2268 vd ? vd->vdev_path : "NULL",
2269 vq ? vq->vq_io_complete_ts : 0, pio->io_type,
2270 pio->io_priority, (u_longlong_t)pio->io_flags,
2271 pio->io_stage, pio->io_pipeline, pio->io_pipeline_trace,
2272 (u_longlong_t)zb->zb_objset, (u_longlong_t)zb->zb_object,
2273 (u_longlong_t)zb->zb_level, (u_longlong_t)zb->zb_blkid,
2274 (u_longlong_t)pio->io_offset, (u_longlong_t)pio->io_size,
2275 pio->io_error);
2276 (void) zfs_ereport_post(FM_EREPORT_ZFS_DEADMAN,
2277 pio->io_spa, vd, zb, pio, 0);
2279 if (failmode == ZIO_FAILURE_MODE_CONTINUE &&
2280 taskq_empty_ent(&pio->io_tqent)) {
2281 zio_interrupt(pio);
2285 mutex_enter(&pio->io_lock);
2286 for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) {
2287 cio_next = zio_walk_children(pio, &zl);
2288 zio_deadman_impl(cio, ziodepth + 1);
2290 mutex_exit(&pio->io_lock);
2294 * Log the critical information describing this zio and all of its children
2295 * using the zfs_dbgmsg() interface then post deadman event for the ZED.
2297 void
2298 zio_deadman(zio_t *pio, const char *tag)
2300 spa_t *spa = pio->io_spa;
2301 char *name = spa_name(spa);
2303 if (!zfs_deadman_enabled || spa_suspended(spa))
2304 return;
2306 zio_deadman_impl(pio, 0);
2308 switch (spa_get_deadman_failmode(spa)) {
2309 case ZIO_FAILURE_MODE_WAIT:
2310 zfs_dbgmsg("%s waiting for hung I/O to pool '%s'", tag, name);
2311 break;
2313 case ZIO_FAILURE_MODE_CONTINUE:
2314 zfs_dbgmsg("%s restarting hung I/O for pool '%s'", tag, name);
2315 break;
2317 case ZIO_FAILURE_MODE_PANIC:
2318 fm_panic("%s determined I/O to pool '%s' is hung.", tag, name);
2319 break;
2324 * Execute the I/O pipeline until one of the following occurs:
2325 * (1) the I/O completes; (2) the pipeline stalls waiting for
2326 * dependent child I/Os; (3) the I/O issues, so we're waiting
2327 * for an I/O completion interrupt; (4) the I/O is delegated by
2328 * vdev-level caching or aggregation; (5) the I/O is deferred
2329 * due to vdev-level queueing; (6) the I/O is handed off to
2330 * another thread. In all cases, the pipeline stops whenever
2331 * there's no CPU work; it never burns a thread in cv_wait_io().
2333 * There's no locking on io_stage because there's no legitimate way
2334 * for multiple threads to be attempting to process the same I/O.
2336 static zio_pipe_stage_t *zio_pipeline[];
2339 * zio_execute() is a wrapper around the static function
2340 * __zio_execute() so that we can force __zio_execute() to be
2341 * inlined. This reduces stack overhead which is important
2342 * because __zio_execute() is called recursively in several zio
2343 * code paths. zio_execute() itself cannot be inlined because
2344 * it is externally visible.
2346 void
2347 zio_execute(void *zio)
2349 fstrans_cookie_t cookie;
2351 cookie = spl_fstrans_mark();
2352 __zio_execute(zio);
2353 spl_fstrans_unmark(cookie);
2357 * Used to determine if in the current context the stack is sized large
2358 * enough to allow zio_execute() to be called recursively. A minimum
2359 * stack size of 16K is required to avoid needing to re-dispatch the zio.
2361 static boolean_t
2362 zio_execute_stack_check(zio_t *zio)
2364 #if !defined(HAVE_LARGE_STACKS)
2365 dsl_pool_t *dp = spa_get_dsl(zio->io_spa);
2367 /* Executing in txg_sync_thread() context. */
2368 if (dp && curthread == dp->dp_tx.tx_sync_thread)
2369 return (B_TRUE);
2371 /* Pool initialization outside of zio_taskq context. */
2372 if (dp && spa_is_initializing(dp->dp_spa) &&
2373 !zio_taskq_member(zio, ZIO_TASKQ_ISSUE) &&
2374 !zio_taskq_member(zio, ZIO_TASKQ_ISSUE_HIGH))
2375 return (B_TRUE);
2376 #else
2377 (void) zio;
2378 #endif /* HAVE_LARGE_STACKS */
2380 return (B_FALSE);
2383 __attribute__((always_inline))
2384 static inline void
2385 __zio_execute(zio_t *zio)
2387 ASSERT3U(zio->io_queued_timestamp, >, 0);
2389 while (zio->io_stage < ZIO_STAGE_DONE) {
2390 enum zio_stage pipeline = zio->io_pipeline;
2391 enum zio_stage stage = zio->io_stage;
2393 zio->io_executor = curthread;
2395 ASSERT(!MUTEX_HELD(&zio->io_lock));
2396 ASSERT(ISP2(stage));
2397 ASSERT(zio->io_stall == NULL);
2399 do {
2400 stage <<= 1;
2401 } while ((stage & pipeline) == 0);
2403 ASSERT(stage <= ZIO_STAGE_DONE);
2406 * If we are in interrupt context and this pipeline stage
2407 * will grab a config lock that is held across I/O,
2408 * or may wait for an I/O that needs an interrupt thread
2409 * to complete, issue async to avoid deadlock.
2411 * For VDEV_IO_START, we cut in line so that the io will
2412 * be sent to disk promptly.
2414 if ((stage & ZIO_BLOCKING_STAGES) && zio->io_vd == NULL &&
2415 zio_taskq_member(zio, ZIO_TASKQ_INTERRUPT)) {
2416 boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ?
2417 zio_requeue_io_start_cut_in_line : B_FALSE;
2418 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut);
2419 return;
2423 * If the current context doesn't have large enough stacks
2424 * the zio must be issued asynchronously to prevent overflow.
2426 if (zio_execute_stack_check(zio)) {
2427 boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ?
2428 zio_requeue_io_start_cut_in_line : B_FALSE;
2429 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut);
2430 return;
2433 zio->io_stage = stage;
2434 zio->io_pipeline_trace |= zio->io_stage;
2437 * The zio pipeline stage returns the next zio to execute
2438 * (typically the same as this one), or NULL if we should
2439 * stop.
2441 zio = zio_pipeline[highbit64(stage) - 1](zio);
2443 if (zio == NULL)
2444 return;
2450 * ==========================================================================
2451 * Initiate I/O, either sync or async
2452 * ==========================================================================
2455 zio_wait(zio_t *zio)
2458 * Some routines, like zio_free_sync(), may return a NULL zio
2459 * to avoid the performance overhead of creating and then destroying
2460 * an unneeded zio. For the callers' simplicity, we accept a NULL
2461 * zio and ignore it.
2463 if (zio == NULL)
2464 return (0);
2466 long timeout = MSEC_TO_TICK(zfs_deadman_ziotime_ms);
2467 int error;
2469 ASSERT3S(zio->io_stage, ==, ZIO_STAGE_OPEN);
2470 ASSERT3P(zio->io_executor, ==, NULL);
2472 zio->io_waiter = curthread;
2473 ASSERT0(zio->io_queued_timestamp);
2474 zio->io_queued_timestamp = gethrtime();
2476 if (zio->io_type == ZIO_TYPE_WRITE) {
2477 spa_select_allocator(zio);
2479 __zio_execute(zio);
2481 mutex_enter(&zio->io_lock);
2482 while (zio->io_executor != NULL) {
2483 error = cv_timedwait_io(&zio->io_cv, &zio->io_lock,
2484 ddi_get_lbolt() + timeout);
2486 if (zfs_deadman_enabled && error == -1 &&
2487 gethrtime() - zio->io_queued_timestamp >
2488 spa_deadman_ziotime(zio->io_spa)) {
2489 mutex_exit(&zio->io_lock);
2490 timeout = MSEC_TO_TICK(zfs_deadman_checktime_ms);
2491 zio_deadman(zio, FTAG);
2492 mutex_enter(&zio->io_lock);
2495 mutex_exit(&zio->io_lock);
2497 error = zio->io_error;
2498 zio_destroy(zio);
2500 return (error);
2503 void
2504 zio_nowait(zio_t *zio)
2507 * See comment in zio_wait().
2509 if (zio == NULL)
2510 return;
2512 ASSERT3P(zio->io_executor, ==, NULL);
2514 if (zio->io_child_type == ZIO_CHILD_LOGICAL &&
2515 list_is_empty(&zio->io_parent_list)) {
2516 zio_t *pio;
2519 * This is a logical async I/O with no parent to wait for it.
2520 * We add it to the spa_async_root_zio "Godfather" I/O which
2521 * will ensure they complete prior to unloading the pool.
2523 spa_t *spa = zio->io_spa;
2524 pio = spa->spa_async_zio_root[CPU_SEQID_UNSTABLE];
2526 zio_add_child(pio, zio);
2529 ASSERT0(zio->io_queued_timestamp);
2530 zio->io_queued_timestamp = gethrtime();
2531 if (zio->io_type == ZIO_TYPE_WRITE) {
2532 spa_select_allocator(zio);
2534 __zio_execute(zio);
2538 * ==========================================================================
2539 * Reexecute, cancel, or suspend/resume failed I/O
2540 * ==========================================================================
2543 static void
2544 zio_reexecute(void *arg)
2546 zio_t *pio = arg;
2547 zio_t *cio, *cio_next, *gio;
2549 ASSERT(pio->io_child_type == ZIO_CHILD_LOGICAL);
2550 ASSERT(pio->io_orig_stage == ZIO_STAGE_OPEN);
2551 ASSERT(pio->io_gang_leader == NULL);
2552 ASSERT(pio->io_gang_tree == NULL);
2554 mutex_enter(&pio->io_lock);
2555 pio->io_flags = pio->io_orig_flags;
2556 pio->io_stage = pio->io_orig_stage;
2557 pio->io_pipeline = pio->io_orig_pipeline;
2558 pio->io_reexecute = 0;
2559 pio->io_flags |= ZIO_FLAG_REEXECUTED;
2560 pio->io_pipeline_trace = 0;
2561 pio->io_error = 0;
2562 pio->io_state[ZIO_WAIT_READY] = (pio->io_stage >= ZIO_STAGE_READY) ||
2563 (pio->io_pipeline & ZIO_STAGE_READY) == 0;
2564 pio->io_state[ZIO_WAIT_DONE] = (pio->io_stage >= ZIO_STAGE_DONE);
2565 zio_link_t *zl = NULL;
2566 while ((gio = zio_walk_parents(pio, &zl)) != NULL) {
2567 for (int w = 0; w < ZIO_WAIT_TYPES; w++) {
2568 gio->io_children[pio->io_child_type][w] +=
2569 !pio->io_state[w];
2572 for (int c = 0; c < ZIO_CHILD_TYPES; c++)
2573 pio->io_child_error[c] = 0;
2575 if (IO_IS_ALLOCATING(pio))
2576 BP_ZERO(pio->io_bp);
2579 * As we reexecute pio's children, new children could be created.
2580 * New children go to the head of pio's io_child_list, however,
2581 * so we will (correctly) not reexecute them. The key is that
2582 * the remainder of pio's io_child_list, from 'cio_next' onward,
2583 * cannot be affected by any side effects of reexecuting 'cio'.
2585 zl = NULL;
2586 for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) {
2587 cio_next = zio_walk_children(pio, &zl);
2588 mutex_exit(&pio->io_lock);
2589 zio_reexecute(cio);
2590 mutex_enter(&pio->io_lock);
2592 mutex_exit(&pio->io_lock);
2595 * Now that all children have been reexecuted, execute the parent.
2596 * We don't reexecute "The Godfather" I/O here as it's the
2597 * responsibility of the caller to wait on it.
2599 if (!(pio->io_flags & ZIO_FLAG_GODFATHER)) {
2600 pio->io_queued_timestamp = gethrtime();
2601 __zio_execute(pio);
2605 void
2606 zio_suspend(spa_t *spa, zio_t *zio, zio_suspend_reason_t reason)
2608 if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_PANIC)
2609 fm_panic("Pool '%s' has encountered an uncorrectable I/O "
2610 "failure and the failure mode property for this pool "
2611 "is set to panic.", spa_name(spa));
2613 if (reason != ZIO_SUSPEND_MMP) {
2614 cmn_err(CE_WARN, "Pool '%s' has encountered an uncorrectable "
2615 "I/O failure and has been suspended.", spa_name(spa));
2618 (void) zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE, spa, NULL,
2619 NULL, NULL, 0);
2621 mutex_enter(&spa->spa_suspend_lock);
2623 if (spa->spa_suspend_zio_root == NULL)
2624 spa->spa_suspend_zio_root = zio_root(spa, NULL, NULL,
2625 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
2626 ZIO_FLAG_GODFATHER);
2628 spa->spa_suspended = reason;
2630 if (zio != NULL) {
2631 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
2632 ASSERT(zio != spa->spa_suspend_zio_root);
2633 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2634 ASSERT(zio_unique_parent(zio) == NULL);
2635 ASSERT(zio->io_stage == ZIO_STAGE_DONE);
2636 zio_add_child(spa->spa_suspend_zio_root, zio);
2639 mutex_exit(&spa->spa_suspend_lock);
2643 zio_resume(spa_t *spa)
2645 zio_t *pio;
2648 * Reexecute all previously suspended i/o.
2650 mutex_enter(&spa->spa_suspend_lock);
2651 if (spa->spa_suspended != ZIO_SUSPEND_NONE)
2652 cmn_err(CE_WARN, "Pool '%s' was suspended and is being "
2653 "resumed. Failed I/O will be retried.",
2654 spa_name(spa));
2655 spa->spa_suspended = ZIO_SUSPEND_NONE;
2656 cv_broadcast(&spa->spa_suspend_cv);
2657 pio = spa->spa_suspend_zio_root;
2658 spa->spa_suspend_zio_root = NULL;
2659 mutex_exit(&spa->spa_suspend_lock);
2661 if (pio == NULL)
2662 return (0);
2664 zio_reexecute(pio);
2665 return (zio_wait(pio));
2668 void
2669 zio_resume_wait(spa_t *spa)
2671 mutex_enter(&spa->spa_suspend_lock);
2672 while (spa_suspended(spa))
2673 cv_wait(&spa->spa_suspend_cv, &spa->spa_suspend_lock);
2674 mutex_exit(&spa->spa_suspend_lock);
2678 * ==========================================================================
2679 * Gang blocks.
2681 * A gang block is a collection of small blocks that looks to the DMU
2682 * like one large block. When zio_dva_allocate() cannot find a block
2683 * of the requested size, due to either severe fragmentation or the pool
2684 * being nearly full, it calls zio_write_gang_block() to construct the
2685 * block from smaller fragments.
2687 * A gang block consists of a gang header (zio_gbh_phys_t) and up to
2688 * three (SPA_GBH_NBLKPTRS) gang members. The gang header is just like
2689 * an indirect block: it's an array of block pointers. It consumes
2690 * only one sector and hence is allocatable regardless of fragmentation.
2691 * The gang header's bps point to its gang members, which hold the data.
2693 * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg>
2694 * as the verifier to ensure uniqueness of the SHA256 checksum.
2695 * Critically, the gang block bp's blk_cksum is the checksum of the data,
2696 * not the gang header. This ensures that data block signatures (needed for
2697 * deduplication) are independent of how the block is physically stored.
2699 * Gang blocks can be nested: a gang member may itself be a gang block.
2700 * Thus every gang block is a tree in which root and all interior nodes are
2701 * gang headers, and the leaves are normal blocks that contain user data.
2702 * The root of the gang tree is called the gang leader.
2704 * To perform any operation (read, rewrite, free, claim) on a gang block,
2705 * zio_gang_assemble() first assembles the gang tree (minus data leaves)
2706 * in the io_gang_tree field of the original logical i/o by recursively
2707 * reading the gang leader and all gang headers below it. This yields
2708 * an in-core tree containing the contents of every gang header and the
2709 * bps for every constituent of the gang block.
2711 * With the gang tree now assembled, zio_gang_issue() just walks the gang tree
2712 * and invokes a callback on each bp. To free a gang block, zio_gang_issue()
2713 * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp.
2714 * zio_claim_gang() provides a similarly trivial wrapper for zio_claim().
2715 * zio_read_gang() is a wrapper around zio_read() that omits reading gang
2716 * headers, since we already have those in io_gang_tree. zio_rewrite_gang()
2717 * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite()
2718 * of the gang header plus zio_checksum_compute() of the data to update the
2719 * gang header's blk_cksum as described above.
2721 * The two-phase assemble/issue model solves the problem of partial failure --
2722 * what if you'd freed part of a gang block but then couldn't read the
2723 * gang header for another part? Assembling the entire gang tree first
2724 * ensures that all the necessary gang header I/O has succeeded before
2725 * starting the actual work of free, claim, or write. Once the gang tree
2726 * is assembled, free and claim are in-memory operations that cannot fail.
2728 * In the event that a gang write fails, zio_dva_unallocate() walks the
2729 * gang tree to immediately free (i.e. insert back into the space map)
2730 * everything we've allocated. This ensures that we don't get ENOSPC
2731 * errors during repeated suspend/resume cycles due to a flaky device.
2733 * Gang rewrites only happen during sync-to-convergence. If we can't assemble
2734 * the gang tree, we won't modify the block, so we can safely defer the free
2735 * (knowing that the block is still intact). If we *can* assemble the gang
2736 * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free
2737 * each constituent bp and we can allocate a new block on the next sync pass.
2739 * In all cases, the gang tree allows complete recovery from partial failure.
2740 * ==========================================================================
2743 static void
2744 zio_gang_issue_func_done(zio_t *zio)
2746 abd_free(zio->io_abd);
2749 static zio_t *
2750 zio_read_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
2751 uint64_t offset)
2753 if (gn != NULL)
2754 return (pio);
2756 return (zio_read(pio, pio->io_spa, bp, abd_get_offset(data, offset),
2757 BP_GET_PSIZE(bp), zio_gang_issue_func_done,
2758 NULL, pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
2759 &pio->io_bookmark));
2762 static zio_t *
2763 zio_rewrite_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
2764 uint64_t offset)
2766 zio_t *zio;
2768 if (gn != NULL) {
2769 abd_t *gbh_abd =
2770 abd_get_from_buf(gn->gn_gbh, SPA_GANGBLOCKSIZE);
2771 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
2772 gbh_abd, SPA_GANGBLOCKSIZE, zio_gang_issue_func_done, NULL,
2773 pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
2774 &pio->io_bookmark);
2776 * As we rewrite each gang header, the pipeline will compute
2777 * a new gang block header checksum for it; but no one will
2778 * compute a new data checksum, so we do that here. The one
2779 * exception is the gang leader: the pipeline already computed
2780 * its data checksum because that stage precedes gang assembly.
2781 * (Presently, nothing actually uses interior data checksums;
2782 * this is just good hygiene.)
2784 if (gn != pio->io_gang_leader->io_gang_tree) {
2785 abd_t *buf = abd_get_offset(data, offset);
2787 zio_checksum_compute(zio, BP_GET_CHECKSUM(bp),
2788 buf, BP_GET_PSIZE(bp));
2790 abd_free(buf);
2793 * If we are here to damage data for testing purposes,
2794 * leave the GBH alone so that we can detect the damage.
2796 if (pio->io_gang_leader->io_flags & ZIO_FLAG_INDUCE_DAMAGE)
2797 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
2798 } else {
2799 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
2800 abd_get_offset(data, offset), BP_GET_PSIZE(bp),
2801 zio_gang_issue_func_done, NULL, pio->io_priority,
2802 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
2805 return (zio);
2808 static zio_t *
2809 zio_free_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
2810 uint64_t offset)
2812 (void) gn, (void) data, (void) offset;
2814 zio_t *zio = zio_free_sync(pio, pio->io_spa, pio->io_txg, bp,
2815 ZIO_GANG_CHILD_FLAGS(pio));
2816 if (zio == NULL) {
2817 zio = zio_null(pio, pio->io_spa,
2818 NULL, NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio));
2820 return (zio);
2823 static zio_t *
2824 zio_claim_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
2825 uint64_t offset)
2827 (void) gn, (void) data, (void) offset;
2828 return (zio_claim(pio, pio->io_spa, pio->io_txg, bp,
2829 NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio)));
2832 static zio_gang_issue_func_t *zio_gang_issue_func[ZIO_TYPES] = {
2833 NULL,
2834 zio_read_gang,
2835 zio_rewrite_gang,
2836 zio_free_gang,
2837 zio_claim_gang,
2838 NULL
2841 static void zio_gang_tree_assemble_done(zio_t *zio);
2843 static zio_gang_node_t *
2844 zio_gang_node_alloc(zio_gang_node_t **gnpp)
2846 zio_gang_node_t *gn;
2848 ASSERT(*gnpp == NULL);
2850 gn = kmem_zalloc(sizeof (*gn), KM_SLEEP);
2851 gn->gn_gbh = zio_buf_alloc(SPA_GANGBLOCKSIZE);
2852 *gnpp = gn;
2854 return (gn);
2857 static void
2858 zio_gang_node_free(zio_gang_node_t **gnpp)
2860 zio_gang_node_t *gn = *gnpp;
2862 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
2863 ASSERT(gn->gn_child[g] == NULL);
2865 zio_buf_free(gn->gn_gbh, SPA_GANGBLOCKSIZE);
2866 kmem_free(gn, sizeof (*gn));
2867 *gnpp = NULL;
2870 static void
2871 zio_gang_tree_free(zio_gang_node_t **gnpp)
2873 zio_gang_node_t *gn = *gnpp;
2875 if (gn == NULL)
2876 return;
2878 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
2879 zio_gang_tree_free(&gn->gn_child[g]);
2881 zio_gang_node_free(gnpp);
2884 static void
2885 zio_gang_tree_assemble(zio_t *gio, blkptr_t *bp, zio_gang_node_t **gnpp)
2887 zio_gang_node_t *gn = zio_gang_node_alloc(gnpp);
2888 abd_t *gbh_abd = abd_get_from_buf(gn->gn_gbh, SPA_GANGBLOCKSIZE);
2890 ASSERT(gio->io_gang_leader == gio);
2891 ASSERT(BP_IS_GANG(bp));
2893 zio_nowait(zio_read(gio, gio->io_spa, bp, gbh_abd, SPA_GANGBLOCKSIZE,
2894 zio_gang_tree_assemble_done, gn, gio->io_priority,
2895 ZIO_GANG_CHILD_FLAGS(gio), &gio->io_bookmark));
2898 static void
2899 zio_gang_tree_assemble_done(zio_t *zio)
2901 zio_t *gio = zio->io_gang_leader;
2902 zio_gang_node_t *gn = zio->io_private;
2903 blkptr_t *bp = zio->io_bp;
2905 ASSERT(gio == zio_unique_parent(zio));
2906 ASSERT(list_is_empty(&zio->io_child_list));
2908 if (zio->io_error)
2909 return;
2911 /* this ABD was created from a linear buf in zio_gang_tree_assemble */
2912 if (BP_SHOULD_BYTESWAP(bp))
2913 byteswap_uint64_array(abd_to_buf(zio->io_abd), zio->io_size);
2915 ASSERT3P(abd_to_buf(zio->io_abd), ==, gn->gn_gbh);
2916 ASSERT(zio->io_size == SPA_GANGBLOCKSIZE);
2917 ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
2919 abd_free(zio->io_abd);
2921 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
2922 blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
2923 if (!BP_IS_GANG(gbp))
2924 continue;
2925 zio_gang_tree_assemble(gio, gbp, &gn->gn_child[g]);
2929 static void
2930 zio_gang_tree_issue(zio_t *pio, zio_gang_node_t *gn, blkptr_t *bp, abd_t *data,
2931 uint64_t offset)
2933 zio_t *gio = pio->io_gang_leader;
2934 zio_t *zio;
2936 ASSERT(BP_IS_GANG(bp) == !!gn);
2937 ASSERT(BP_GET_CHECKSUM(bp) == BP_GET_CHECKSUM(gio->io_bp));
2938 ASSERT(BP_GET_LSIZE(bp) == BP_GET_PSIZE(bp) || gn == gio->io_gang_tree);
2941 * If you're a gang header, your data is in gn->gn_gbh.
2942 * If you're a gang member, your data is in 'data' and gn == NULL.
2944 zio = zio_gang_issue_func[gio->io_type](pio, bp, gn, data, offset);
2946 if (gn != NULL) {
2947 ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
2949 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
2950 blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
2951 if (BP_IS_HOLE(gbp))
2952 continue;
2953 zio_gang_tree_issue(zio, gn->gn_child[g], gbp, data,
2954 offset);
2955 offset += BP_GET_PSIZE(gbp);
2959 if (gn == gio->io_gang_tree)
2960 ASSERT3U(gio->io_size, ==, offset);
2962 if (zio != pio)
2963 zio_nowait(zio);
2966 static zio_t *
2967 zio_gang_assemble(zio_t *zio)
2969 blkptr_t *bp = zio->io_bp;
2971 ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == NULL);
2972 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2974 zio->io_gang_leader = zio;
2976 zio_gang_tree_assemble(zio, bp, &zio->io_gang_tree);
2978 return (zio);
2981 static zio_t *
2982 zio_gang_issue(zio_t *zio)
2984 blkptr_t *bp = zio->io_bp;
2986 if (zio_wait_for_children(zio, ZIO_CHILD_GANG_BIT, ZIO_WAIT_DONE)) {
2987 return (NULL);
2990 ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == zio);
2991 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2993 if (zio->io_child_error[ZIO_CHILD_GANG] == 0)
2994 zio_gang_tree_issue(zio, zio->io_gang_tree, bp, zio->io_abd,
2996 else
2997 zio_gang_tree_free(&zio->io_gang_tree);
2999 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
3001 return (zio);
3004 static void
3005 zio_gang_inherit_allocator(zio_t *pio, zio_t *cio)
3007 cio->io_allocator = pio->io_allocator;
3010 static void
3011 zio_write_gang_member_ready(zio_t *zio)
3013 zio_t *pio = zio_unique_parent(zio);
3014 dva_t *cdva = zio->io_bp->blk_dva;
3015 dva_t *pdva = pio->io_bp->blk_dva;
3016 uint64_t asize;
3017 zio_t *gio __maybe_unused = zio->io_gang_leader;
3019 if (BP_IS_HOLE(zio->io_bp))
3020 return;
3022 ASSERT(BP_IS_HOLE(&zio->io_bp_orig));
3024 ASSERT(zio->io_child_type == ZIO_CHILD_GANG);
3025 ASSERT3U(zio->io_prop.zp_copies, ==, gio->io_prop.zp_copies);
3026 ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(zio->io_bp));
3027 ASSERT3U(pio->io_prop.zp_copies, <=, BP_GET_NDVAS(pio->io_bp));
3028 VERIFY3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp));
3030 mutex_enter(&pio->io_lock);
3031 for (int d = 0; d < BP_GET_NDVAS(zio->io_bp); d++) {
3032 ASSERT(DVA_GET_GANG(&pdva[d]));
3033 asize = DVA_GET_ASIZE(&pdva[d]);
3034 asize += DVA_GET_ASIZE(&cdva[d]);
3035 DVA_SET_ASIZE(&pdva[d], asize);
3037 mutex_exit(&pio->io_lock);
3040 static void
3041 zio_write_gang_done(zio_t *zio)
3044 * The io_abd field will be NULL for a zio with no data. The io_flags
3045 * will initially have the ZIO_FLAG_NODATA bit flag set, but we can't
3046 * check for it here as it is cleared in zio_ready.
3048 if (zio->io_abd != NULL)
3049 abd_free(zio->io_abd);
3052 static zio_t *
3053 zio_write_gang_block(zio_t *pio, metaslab_class_t *mc)
3055 spa_t *spa = pio->io_spa;
3056 blkptr_t *bp = pio->io_bp;
3057 zio_t *gio = pio->io_gang_leader;
3058 zio_t *zio;
3059 zio_gang_node_t *gn, **gnpp;
3060 zio_gbh_phys_t *gbh;
3061 abd_t *gbh_abd;
3062 uint64_t txg = pio->io_txg;
3063 uint64_t resid = pio->io_size;
3064 uint64_t lsize;
3065 int copies = gio->io_prop.zp_copies;
3066 zio_prop_t zp;
3067 int error;
3068 boolean_t has_data = !(pio->io_flags & ZIO_FLAG_NODATA);
3071 * If one copy was requested, store 2 copies of the GBH, so that we
3072 * can still traverse all the data (e.g. to free or scrub) even if a
3073 * block is damaged. Note that we can't store 3 copies of the GBH in
3074 * all cases, e.g. with encryption, which uses DVA[2] for the IV+salt.
3076 int gbh_copies = copies;
3077 if (gbh_copies == 1) {
3078 gbh_copies = MIN(2, spa_max_replication(spa));
3081 ASSERT(ZIO_HAS_ALLOCATOR(pio));
3082 int flags = METASLAB_HINTBP_FAVOR | METASLAB_GANG_HEADER;
3083 if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
3084 ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
3085 ASSERT(has_data);
3087 flags |= METASLAB_ASYNC_ALLOC;
3088 VERIFY(zfs_refcount_held(&mc->mc_allocator[pio->io_allocator].
3089 mca_alloc_slots, pio));
3092 * The logical zio has already placed a reservation for
3093 * 'copies' allocation slots but gang blocks may require
3094 * additional copies. These additional copies
3095 * (i.e. gbh_copies - copies) are guaranteed to succeed
3096 * since metaslab_class_throttle_reserve() always allows
3097 * additional reservations for gang blocks.
3099 VERIFY(metaslab_class_throttle_reserve(mc, gbh_copies - copies,
3100 pio->io_allocator, pio, flags));
3103 error = metaslab_alloc(spa, mc, SPA_GANGBLOCKSIZE,
3104 bp, gbh_copies, txg, pio == gio ? NULL : gio->io_bp, flags,
3105 &pio->io_alloc_list, pio, pio->io_allocator);
3106 if (error) {
3107 if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
3108 ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
3109 ASSERT(has_data);
3112 * If we failed to allocate the gang block header then
3113 * we remove any additional allocation reservations that
3114 * we placed here. The original reservation will
3115 * be removed when the logical I/O goes to the ready
3116 * stage.
3118 metaslab_class_throttle_unreserve(mc,
3119 gbh_copies - copies, pio->io_allocator, pio);
3122 pio->io_error = error;
3123 return (pio);
3126 if (pio == gio) {
3127 gnpp = &gio->io_gang_tree;
3128 } else {
3129 gnpp = pio->io_private;
3130 ASSERT(pio->io_ready == zio_write_gang_member_ready);
3133 gn = zio_gang_node_alloc(gnpp);
3134 gbh = gn->gn_gbh;
3135 memset(gbh, 0, SPA_GANGBLOCKSIZE);
3136 gbh_abd = abd_get_from_buf(gbh, SPA_GANGBLOCKSIZE);
3139 * Create the gang header.
3141 zio = zio_rewrite(pio, spa, txg, bp, gbh_abd, SPA_GANGBLOCKSIZE,
3142 zio_write_gang_done, NULL, pio->io_priority,
3143 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
3145 zio_gang_inherit_allocator(pio, zio);
3148 * Create and nowait the gang children.
3150 for (int g = 0; resid != 0; resid -= lsize, g++) {
3151 lsize = P2ROUNDUP(resid / (SPA_GBH_NBLKPTRS - g),
3152 SPA_MINBLOCKSIZE);
3153 ASSERT(lsize >= SPA_MINBLOCKSIZE && lsize <= resid);
3155 zp.zp_checksum = gio->io_prop.zp_checksum;
3156 zp.zp_compress = ZIO_COMPRESS_OFF;
3157 zp.zp_complevel = gio->io_prop.zp_complevel;
3158 zp.zp_type = zp.zp_storage_type = DMU_OT_NONE;
3159 zp.zp_level = 0;
3160 zp.zp_copies = gio->io_prop.zp_copies;
3161 zp.zp_dedup = B_FALSE;
3162 zp.zp_dedup_verify = B_FALSE;
3163 zp.zp_nopwrite = B_FALSE;
3164 zp.zp_encrypt = gio->io_prop.zp_encrypt;
3165 zp.zp_byteorder = gio->io_prop.zp_byteorder;
3166 zp.zp_direct_write = B_FALSE;
3167 memset(zp.zp_salt, 0, ZIO_DATA_SALT_LEN);
3168 memset(zp.zp_iv, 0, ZIO_DATA_IV_LEN);
3169 memset(zp.zp_mac, 0, ZIO_DATA_MAC_LEN);
3171 zio_t *cio = zio_write(zio, spa, txg, &gbh->zg_blkptr[g],
3172 has_data ? abd_get_offset(pio->io_abd, pio->io_size -
3173 resid) : NULL, lsize, lsize, &zp,
3174 zio_write_gang_member_ready, NULL,
3175 zio_write_gang_done, &gn->gn_child[g], pio->io_priority,
3176 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
3178 zio_gang_inherit_allocator(zio, cio);
3180 if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
3181 ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
3182 ASSERT(has_data);
3185 * Gang children won't throttle but we should
3186 * account for their work, so reserve an allocation
3187 * slot for them here.
3189 VERIFY(metaslab_class_throttle_reserve(mc,
3190 zp.zp_copies, cio->io_allocator, cio, flags));
3192 zio_nowait(cio);
3196 * Set pio's pipeline to just wait for zio to finish.
3198 pio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
3200 zio_nowait(zio);
3202 return (pio);
3206 * The zio_nop_write stage in the pipeline determines if allocating a
3207 * new bp is necessary. The nopwrite feature can handle writes in
3208 * either syncing or open context (i.e. zil writes) and as a result is
3209 * mutually exclusive with dedup.
3211 * By leveraging a cryptographically secure checksum, such as SHA256, we
3212 * can compare the checksums of the new data and the old to determine if
3213 * allocating a new block is required. Note that our requirements for
3214 * cryptographic strength are fairly weak: there can't be any accidental
3215 * hash collisions, but we don't need to be secure against intentional
3216 * (malicious) collisions. To trigger a nopwrite, you have to be able
3217 * to write the file to begin with, and triggering an incorrect (hash
3218 * collision) nopwrite is no worse than simply writing to the file.
3219 * That said, there are no known attacks against the checksum algorithms
3220 * used for nopwrite, assuming that the salt and the checksums
3221 * themselves remain secret.
3223 static zio_t *
3224 zio_nop_write(zio_t *zio)
3226 blkptr_t *bp = zio->io_bp;
3227 blkptr_t *bp_orig = &zio->io_bp_orig;
3228 zio_prop_t *zp = &zio->io_prop;
3230 ASSERT(BP_IS_HOLE(bp));
3231 ASSERT(BP_GET_LEVEL(bp) == 0);
3232 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
3233 ASSERT(zp->zp_nopwrite);
3234 ASSERT(!zp->zp_dedup);
3235 ASSERT(zio->io_bp_override == NULL);
3236 ASSERT(IO_IS_ALLOCATING(zio));
3239 * Check to see if the original bp and the new bp have matching
3240 * characteristics (i.e. same checksum, compression algorithms, etc).
3241 * If they don't then just continue with the pipeline which will
3242 * allocate a new bp.
3244 if (BP_IS_HOLE(bp_orig) ||
3245 !(zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_flags &
3246 ZCHECKSUM_FLAG_NOPWRITE) ||
3247 BP_IS_ENCRYPTED(bp) || BP_IS_ENCRYPTED(bp_orig) ||
3248 BP_GET_CHECKSUM(bp) != BP_GET_CHECKSUM(bp_orig) ||
3249 BP_GET_COMPRESS(bp) != BP_GET_COMPRESS(bp_orig) ||
3250 BP_GET_DEDUP(bp) != BP_GET_DEDUP(bp_orig) ||
3251 zp->zp_copies != BP_GET_NDVAS(bp_orig))
3252 return (zio);
3255 * If the checksums match then reset the pipeline so that we
3256 * avoid allocating a new bp and issuing any I/O.
3258 if (ZIO_CHECKSUM_EQUAL(bp->blk_cksum, bp_orig->blk_cksum)) {
3259 ASSERT(zio_checksum_table[zp->zp_checksum].ci_flags &
3260 ZCHECKSUM_FLAG_NOPWRITE);
3261 ASSERT3U(BP_GET_PSIZE(bp), ==, BP_GET_PSIZE(bp_orig));
3262 ASSERT3U(BP_GET_LSIZE(bp), ==, BP_GET_LSIZE(bp_orig));
3263 ASSERT(zp->zp_compress != ZIO_COMPRESS_OFF);
3264 ASSERT3U(bp->blk_prop, ==, bp_orig->blk_prop);
3267 * If we're overwriting a block that is currently on an
3268 * indirect vdev, then ignore the nopwrite request and
3269 * allow a new block to be allocated on a concrete vdev.
3271 spa_config_enter(zio->io_spa, SCL_VDEV, FTAG, RW_READER);
3272 for (int d = 0; d < BP_GET_NDVAS(bp_orig); d++) {
3273 vdev_t *tvd = vdev_lookup_top(zio->io_spa,
3274 DVA_GET_VDEV(&bp_orig->blk_dva[d]));
3275 if (tvd->vdev_ops == &vdev_indirect_ops) {
3276 spa_config_exit(zio->io_spa, SCL_VDEV, FTAG);
3277 return (zio);
3280 spa_config_exit(zio->io_spa, SCL_VDEV, FTAG);
3282 *bp = *bp_orig;
3283 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
3284 zio->io_flags |= ZIO_FLAG_NOPWRITE;
3287 return (zio);
3291 * ==========================================================================
3292 * Block Reference Table
3293 * ==========================================================================
3295 static zio_t *
3296 zio_brt_free(zio_t *zio)
3298 blkptr_t *bp;
3300 bp = zio->io_bp;
3302 if (BP_GET_LEVEL(bp) > 0 ||
3303 BP_IS_METADATA(bp) ||
3304 !brt_maybe_exists(zio->io_spa, bp)) {
3305 return (zio);
3308 if (!brt_entry_decref(zio->io_spa, bp)) {
3310 * This isn't the last reference, so we cannot free
3311 * the data yet.
3313 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
3316 return (zio);
3320 * ==========================================================================
3321 * Dedup
3322 * ==========================================================================
3324 static void
3325 zio_ddt_child_read_done(zio_t *zio)
3327 blkptr_t *bp = zio->io_bp;
3328 ddt_t *ddt;
3329 ddt_entry_t *dde = zio->io_private;
3330 zio_t *pio = zio_unique_parent(zio);
3332 mutex_enter(&pio->io_lock);
3333 ddt = ddt_select(zio->io_spa, bp);
3335 if (zio->io_error == 0) {
3336 ddt_phys_variant_t v = ddt_phys_select(ddt, dde, bp);
3337 /* this phys variant doesn't need repair */
3338 ddt_phys_clear(dde->dde_phys, v);
3341 if (zio->io_error == 0 && dde->dde_io->dde_repair_abd == NULL)
3342 dde->dde_io->dde_repair_abd = zio->io_abd;
3343 else
3344 abd_free(zio->io_abd);
3345 mutex_exit(&pio->io_lock);
3348 static zio_t *
3349 zio_ddt_read_start(zio_t *zio)
3351 blkptr_t *bp = zio->io_bp;
3353 ASSERT(BP_GET_DEDUP(bp));
3354 ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
3355 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3357 if (zio->io_child_error[ZIO_CHILD_DDT]) {
3358 ddt_t *ddt = ddt_select(zio->io_spa, bp);
3359 ddt_entry_t *dde = ddt_repair_start(ddt, bp);
3360 ddt_phys_variant_t v_self = ddt_phys_select(ddt, dde, bp);
3361 ddt_univ_phys_t *ddp = dde->dde_phys;
3362 blkptr_t blk;
3364 ASSERT(zio->io_vsd == NULL);
3365 zio->io_vsd = dde;
3367 if (v_self == DDT_PHYS_NONE)
3368 return (zio);
3370 /* issue I/O for the other copies */
3371 for (int p = 0; p < DDT_NPHYS(ddt); p++) {
3372 ddt_phys_variant_t v = DDT_PHYS_VARIANT(ddt, p);
3374 if (ddt_phys_birth(ddp, v) == 0 || v == v_self)
3375 continue;
3377 ddt_bp_create(ddt->ddt_checksum, &dde->dde_key,
3378 ddp, v, &blk);
3379 zio_nowait(zio_read(zio, zio->io_spa, &blk,
3380 abd_alloc_for_io(zio->io_size, B_TRUE),
3381 zio->io_size, zio_ddt_child_read_done, dde,
3382 zio->io_priority, ZIO_DDT_CHILD_FLAGS(zio) |
3383 ZIO_FLAG_DONT_PROPAGATE, &zio->io_bookmark));
3385 return (zio);
3388 zio_nowait(zio_read(zio, zio->io_spa, bp,
3389 zio->io_abd, zio->io_size, NULL, NULL, zio->io_priority,
3390 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark));
3392 return (zio);
3395 static zio_t *
3396 zio_ddt_read_done(zio_t *zio)
3398 blkptr_t *bp = zio->io_bp;
3400 if (zio_wait_for_children(zio, ZIO_CHILD_DDT_BIT, ZIO_WAIT_DONE)) {
3401 return (NULL);
3404 ASSERT(BP_GET_DEDUP(bp));
3405 ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
3406 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3408 if (zio->io_child_error[ZIO_CHILD_DDT]) {
3409 ddt_t *ddt = ddt_select(zio->io_spa, bp);
3410 ddt_entry_t *dde = zio->io_vsd;
3411 if (ddt == NULL) {
3412 ASSERT(spa_load_state(zio->io_spa) != SPA_LOAD_NONE);
3413 return (zio);
3415 if (dde == NULL) {
3416 zio->io_stage = ZIO_STAGE_DDT_READ_START >> 1;
3417 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
3418 return (NULL);
3420 if (dde->dde_io->dde_repair_abd != NULL) {
3421 abd_copy(zio->io_abd, dde->dde_io->dde_repair_abd,
3422 zio->io_size);
3423 zio->io_child_error[ZIO_CHILD_DDT] = 0;
3425 ddt_repair_done(ddt, dde);
3426 zio->io_vsd = NULL;
3429 ASSERT(zio->io_vsd == NULL);
3431 return (zio);
3434 static boolean_t
3435 zio_ddt_collision(zio_t *zio, ddt_t *ddt, ddt_entry_t *dde)
3437 spa_t *spa = zio->io_spa;
3438 boolean_t do_raw = !!(zio->io_flags & ZIO_FLAG_RAW);
3440 ASSERT(!(zio->io_bp_override && do_raw));
3443 * Note: we compare the original data, not the transformed data,
3444 * because when zio->io_bp is an override bp, we will not have
3445 * pushed the I/O transforms. That's an important optimization
3446 * because otherwise we'd compress/encrypt all dmu_sync() data twice.
3447 * However, we should never get a raw, override zio so in these
3448 * cases we can compare the io_abd directly. This is useful because
3449 * it allows us to do dedup verification even if we don't have access
3450 * to the original data (for instance, if the encryption keys aren't
3451 * loaded).
3454 for (int p = 0; p < DDT_NPHYS(ddt); p++) {
3455 if (DDT_PHYS_IS_DITTO(ddt, p))
3456 continue;
3458 if (dde->dde_io == NULL)
3459 continue;
3461 zio_t *lio = dde->dde_io->dde_lead_zio[p];
3462 if (lio == NULL)
3463 continue;
3465 if (do_raw)
3466 return (lio->io_size != zio->io_size ||
3467 abd_cmp(zio->io_abd, lio->io_abd) != 0);
3469 return (lio->io_orig_size != zio->io_orig_size ||
3470 abd_cmp(zio->io_orig_abd, lio->io_orig_abd) != 0);
3473 for (int p = 0; p < DDT_NPHYS(ddt); p++) {
3474 ddt_phys_variant_t v = DDT_PHYS_VARIANT(ddt, p);
3475 uint64_t phys_birth = ddt_phys_birth(dde->dde_phys, v);
3477 if (phys_birth != 0 && do_raw) {
3478 blkptr_t blk = *zio->io_bp;
3479 uint64_t psize;
3480 abd_t *tmpabd;
3481 int error;
3483 ddt_bp_fill(dde->dde_phys, v, &blk, phys_birth);
3484 psize = BP_GET_PSIZE(&blk);
3486 if (psize != zio->io_size)
3487 return (B_TRUE);
3489 ddt_exit(ddt);
3491 tmpabd = abd_alloc_for_io(psize, B_TRUE);
3493 error = zio_wait(zio_read(NULL, spa, &blk, tmpabd,
3494 psize, NULL, NULL, ZIO_PRIORITY_SYNC_READ,
3495 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
3496 ZIO_FLAG_RAW, &zio->io_bookmark));
3498 if (error == 0) {
3499 if (abd_cmp(tmpabd, zio->io_abd) != 0)
3500 error = SET_ERROR(ENOENT);
3503 abd_free(tmpabd);
3504 ddt_enter(ddt);
3505 return (error != 0);
3506 } else if (phys_birth != 0) {
3507 arc_buf_t *abuf = NULL;
3508 arc_flags_t aflags = ARC_FLAG_WAIT;
3509 blkptr_t blk = *zio->io_bp;
3510 int error;
3512 ddt_bp_fill(dde->dde_phys, v, &blk, phys_birth);
3514 if (BP_GET_LSIZE(&blk) != zio->io_orig_size)
3515 return (B_TRUE);
3517 ddt_exit(ddt);
3519 error = arc_read(NULL, spa, &blk,
3520 arc_getbuf_func, &abuf, ZIO_PRIORITY_SYNC_READ,
3521 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
3522 &aflags, &zio->io_bookmark);
3524 if (error == 0) {
3525 if (abd_cmp_buf(zio->io_orig_abd, abuf->b_data,
3526 zio->io_orig_size) != 0)
3527 error = SET_ERROR(ENOENT);
3528 arc_buf_destroy(abuf, &abuf);
3531 ddt_enter(ddt);
3532 return (error != 0);
3536 return (B_FALSE);
3539 static void
3540 zio_ddt_child_write_done(zio_t *zio)
3542 ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
3543 ddt_entry_t *dde = zio->io_private;
3545 zio_link_t *zl = NULL;
3546 ASSERT3P(zio_walk_parents(zio, &zl), !=, NULL);
3548 int p = DDT_PHYS_FOR_COPIES(ddt, zio->io_prop.zp_copies);
3549 ddt_phys_variant_t v = DDT_PHYS_VARIANT(ddt, p);
3550 ddt_univ_phys_t *ddp = dde->dde_phys;
3552 ddt_enter(ddt);
3554 /* we're the lead, so once we're done there's no one else outstanding */
3555 if (dde->dde_io->dde_lead_zio[p] == zio)
3556 dde->dde_io->dde_lead_zio[p] = NULL;
3558 ddt_univ_phys_t *orig = &dde->dde_io->dde_orig_phys;
3560 if (zio->io_error != 0) {
3562 * The write failed, so we're about to abort the entire IO
3563 * chain. We need to revert the entry back to what it was at
3564 * the last time it was successfully extended.
3566 ddt_phys_copy(ddp, orig, v);
3567 ddt_phys_clear(orig, v);
3569 ddt_exit(ddt);
3570 return;
3574 * We've successfully added new DVAs to the entry. Clear the saved
3575 * state or, if there's still outstanding IO, remember it so we can
3576 * revert to a known good state if that IO fails.
3578 if (dde->dde_io->dde_lead_zio[p] == NULL)
3579 ddt_phys_clear(orig, v);
3580 else
3581 ddt_phys_copy(orig, ddp, v);
3584 * Add references for all dedup writes that were waiting on the
3585 * physical one, skipping any other physical writes that are waiting.
3587 zio_t *pio;
3588 zl = NULL;
3589 while ((pio = zio_walk_parents(zio, &zl)) != NULL) {
3590 if (!(pio->io_flags & ZIO_FLAG_DDT_CHILD))
3591 ddt_phys_addref(ddp, v);
3594 ddt_exit(ddt);
3597 static void
3598 zio_ddt_child_write_ready(zio_t *zio)
3600 ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
3601 ddt_entry_t *dde = zio->io_private;
3603 zio_link_t *zl = NULL;
3604 ASSERT3P(zio_walk_parents(zio, &zl), !=, NULL);
3606 int p = DDT_PHYS_FOR_COPIES(ddt, zio->io_prop.zp_copies);
3607 ddt_phys_variant_t v = DDT_PHYS_VARIANT(ddt, p);
3609 if (zio->io_error != 0)
3610 return;
3612 ddt_enter(ddt);
3614 ddt_phys_extend(dde->dde_phys, v, zio->io_bp);
3616 zio_t *pio;
3617 zl = NULL;
3618 while ((pio = zio_walk_parents(zio, &zl)) != NULL) {
3619 if (!(pio->io_flags & ZIO_FLAG_DDT_CHILD))
3620 ddt_bp_fill(dde->dde_phys, v, pio->io_bp, zio->io_txg);
3623 ddt_exit(ddt);
3626 static zio_t *
3627 zio_ddt_write(zio_t *zio)
3629 spa_t *spa = zio->io_spa;
3630 blkptr_t *bp = zio->io_bp;
3631 uint64_t txg = zio->io_txg;
3632 zio_prop_t *zp = &zio->io_prop;
3633 ddt_t *ddt = ddt_select(spa, bp);
3634 ddt_entry_t *dde;
3636 ASSERT(BP_GET_DEDUP(bp));
3637 ASSERT(BP_GET_CHECKSUM(bp) == zp->zp_checksum);
3638 ASSERT(BP_IS_HOLE(bp) || zio->io_bp_override);
3639 ASSERT(!(zio->io_bp_override && (zio->io_flags & ZIO_FLAG_RAW)));
3641 * Deduplication will not take place for Direct I/O writes. The
3642 * ddt_tree will be emptied in syncing context. Direct I/O writes take
3643 * place in the open-context. Direct I/O write can not attempt to
3644 * modify the ddt_tree while issuing out a write.
3646 ASSERT3B(zio->io_prop.zp_direct_write, ==, B_FALSE);
3648 ddt_enter(ddt);
3649 dde = ddt_lookup(ddt, bp);
3650 if (dde == NULL) {
3651 /* DDT size is over its quota so no new entries */
3652 zp->zp_dedup = B_FALSE;
3653 BP_SET_DEDUP(bp, B_FALSE);
3654 if (zio->io_bp_override == NULL)
3655 zio->io_pipeline = ZIO_WRITE_PIPELINE;
3656 ddt_exit(ddt);
3657 return (zio);
3660 if (zp->zp_dedup_verify && zio_ddt_collision(zio, ddt, dde)) {
3662 * If we're using a weak checksum, upgrade to a strong checksum
3663 * and try again. If we're already using a strong checksum,
3664 * we can't resolve it, so just convert to an ordinary write.
3665 * (And automatically e-mail a paper to Nature?)
3667 if (!(zio_checksum_table[zp->zp_checksum].ci_flags &
3668 ZCHECKSUM_FLAG_DEDUP)) {
3669 zp->zp_checksum = spa_dedup_checksum(spa);
3670 zio_pop_transforms(zio);
3671 zio->io_stage = ZIO_STAGE_OPEN;
3672 BP_ZERO(bp);
3673 } else {
3674 zp->zp_dedup = B_FALSE;
3675 BP_SET_DEDUP(bp, B_FALSE);
3677 ASSERT(!BP_GET_DEDUP(bp));
3678 zio->io_pipeline = ZIO_WRITE_PIPELINE;
3679 ddt_exit(ddt);
3680 return (zio);
3683 int p = DDT_PHYS_FOR_COPIES(ddt, zp->zp_copies);
3684 ddt_phys_variant_t v = DDT_PHYS_VARIANT(ddt, p);
3685 ddt_univ_phys_t *ddp = dde->dde_phys;
3688 * In the common cases, at this point we have a regular BP with no
3689 * allocated DVAs, and the corresponding DDT entry for its checksum.
3690 * Our goal is to fill the BP with enough DVAs to satisfy its copies=
3691 * requirement.
3693 * One of three things needs to happen to fulfill this:
3695 * - if the DDT entry has enough DVAs to satisfy the BP, we just copy
3696 * them out of the entry and return;
3698 * - if the DDT entry has no DVAs (ie its brand new), then we have to
3699 * issue the write as normal so that DVAs can be allocated and the
3700 * data land on disk. We then copy the DVAs into the DDT entry on
3701 * return.
3703 * - if the DDT entry has some DVAs, but too few, we have to issue the
3704 * write, adjusted to have allocate fewer copies. When it returns, we
3705 * add the new DVAs to the DDT entry, and update the BP to have the
3706 * full amount it originally requested.
3708 * In all cases, if there's already a writing IO in flight, we need to
3709 * defer the action until after the write is done. If our action is to
3710 * write, we need to adjust our request for additional DVAs to match
3711 * what will be in the DDT entry after it completes. In this way every
3712 * IO can be guaranteed to recieve enough DVAs simply by joining the
3713 * end of the chain and letting the sequence play out.
3717 * Number of DVAs in the DDT entry. If the BP is encrypted we ignore
3718 * the third one as normal.
3720 int have_dvas = ddt_phys_dva_count(ddp, v, BP_IS_ENCRYPTED(bp));
3721 IMPLY(have_dvas == 0, ddt_phys_birth(ddp, v) == 0);
3723 /* Number of DVAs requested bya the IO. */
3724 uint8_t need_dvas = zp->zp_copies;
3727 * What we do next depends on whether or not there's IO outstanding that
3728 * will update this entry.
3730 if (dde->dde_io == NULL || dde->dde_io->dde_lead_zio[p] == NULL) {
3732 * No IO outstanding, so we only need to worry about ourselves.
3736 * Override BPs bring their own DVAs and their own problems.
3738 if (zio->io_bp_override) {
3740 * For a brand-new entry, all the work has been done
3741 * for us, and we can just fill it out from the provided
3742 * block and leave.
3744 if (have_dvas == 0) {
3745 ASSERT(BP_GET_LOGICAL_BIRTH(bp) == txg);
3746 ASSERT(BP_EQUAL(bp, zio->io_bp_override));
3747 ddt_phys_extend(ddp, v, bp);
3748 ddt_phys_addref(ddp, v);
3749 ddt_exit(ddt);
3750 return (zio);
3754 * If we already have this entry, then we want to treat
3755 * it like a regular write. To do this we just wipe
3756 * them out and proceed like a regular write.
3758 * Even if there are some DVAs in the entry, we still
3759 * have to clear them out. We can't use them to fill
3760 * out the dedup entry, as they are all referenced
3761 * together by a bp already on disk, and will be freed
3762 * as a group.
3764 BP_ZERO_DVAS(bp);
3765 BP_SET_BIRTH(bp, 0, 0);
3769 * If there are enough DVAs in the entry to service our request,
3770 * then we can just use them as-is.
3772 if (have_dvas >= need_dvas) {
3773 ddt_bp_fill(ddp, v, bp, txg);
3774 ddt_phys_addref(ddp, v);
3775 ddt_exit(ddt);
3776 return (zio);
3780 * Otherwise, we have to issue IO to fill the entry up to the
3781 * amount we need.
3783 need_dvas -= have_dvas;
3784 } else {
3786 * There's a write in-flight. If there's already enough DVAs on
3787 * the entry, then either there were already enough to start
3788 * with, or the in-flight IO is between READY and DONE, and so
3789 * has extended the entry with new DVAs. Either way, we don't
3790 * need to do anything, we can just slot in behind it.
3793 if (zio->io_bp_override) {
3795 * If there's a write out, then we're soon going to
3796 * have our own copies of this block, so clear out the
3797 * override block and treat it as a regular dedup
3798 * write. See comment above.
3800 BP_ZERO_DVAS(bp);
3801 BP_SET_BIRTH(bp, 0, 0);
3804 if (have_dvas >= need_dvas) {
3806 * A minor point: there might already be enough
3807 * committed DVAs in the entry to service our request,
3808 * but we don't know which are completed and which are
3809 * allocated but not yet written. In this case, should
3810 * the IO for the new DVAs fail, we will be on the end
3811 * of the IO chain and will also recieve an error, even
3812 * though our request could have been serviced.
3814 * This is an extremely rare case, as it requires the
3815 * original block to be copied with a request for a
3816 * larger number of DVAs, then copied again requesting
3817 * the same (or already fulfilled) number of DVAs while
3818 * the first request is active, and then that first
3819 * request errors. In return, the logic required to
3820 * catch and handle it is complex. For now, I'm just
3821 * not going to bother with it.
3825 * We always fill the bp here as we may have arrived
3826 * after the in-flight write has passed READY, and so
3827 * missed out.
3829 ddt_bp_fill(ddp, v, bp, txg);
3830 zio_add_child(zio, dde->dde_io->dde_lead_zio[p]);
3831 ddt_exit(ddt);
3832 return (zio);
3836 * There's not enough in the entry yet, so we need to look at
3837 * the write in-flight and see how many DVAs it will have once
3838 * it completes.
3840 * The in-flight write has potentially had its copies request
3841 * reduced (if we're filling out an existing entry), so we need
3842 * to reach in and get the original write to find out what it is
3843 * expecting.
3845 * Note that the parent of the lead zio will always have the
3846 * highest zp_copies of any zio in the chain, because ones that
3847 * can be serviced without additional IO are always added to
3848 * the back of the chain.
3850 zio_link_t *zl = NULL;
3851 zio_t *pio =
3852 zio_walk_parents(dde->dde_io->dde_lead_zio[p], &zl);
3853 ASSERT(pio);
3854 uint8_t parent_dvas = pio->io_prop.zp_copies;
3856 if (parent_dvas >= need_dvas) {
3857 zio_add_child(zio, dde->dde_io->dde_lead_zio[p]);
3858 ddt_exit(ddt);
3859 return (zio);
3863 * Still not enough, so we will need to issue to get the
3864 * shortfall.
3866 need_dvas -= parent_dvas;
3870 * We need to write. We will create a new write with the copies
3871 * property adjusted to match the number of DVAs we need to need to
3872 * grow the DDT entry by to satisfy the request.
3874 zio_prop_t czp = *zp;
3875 czp.zp_copies = need_dvas;
3876 zio_t *cio = zio_write(zio, spa, txg, bp, zio->io_orig_abd,
3877 zio->io_orig_size, zio->io_orig_size, &czp,
3878 zio_ddt_child_write_ready, NULL,
3879 zio_ddt_child_write_done, dde, zio->io_priority,
3880 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
3882 zio_push_transform(cio, zio->io_abd, zio->io_size, 0, NULL);
3885 * We are the new lead zio, because our parent has the highest
3886 * zp_copies that has been requested for this entry so far.
3888 ddt_alloc_entry_io(dde);
3889 if (dde->dde_io->dde_lead_zio[p] == NULL) {
3891 * First time out, take a copy of the stable entry to revert
3892 * to if there's an error (see zio_ddt_child_write_done())
3894 ddt_phys_copy(&dde->dde_io->dde_orig_phys, dde->dde_phys, v);
3895 } else {
3897 * Make the existing chain our child, because it cannot
3898 * complete until we have.
3900 zio_add_child(cio, dde->dde_io->dde_lead_zio[p]);
3902 dde->dde_io->dde_lead_zio[p] = cio;
3904 ddt_exit(ddt);
3906 zio_nowait(cio);
3908 return (zio);
3911 static ddt_entry_t *freedde; /* for debugging */
3913 static zio_t *
3914 zio_ddt_free(zio_t *zio)
3916 spa_t *spa = zio->io_spa;
3917 blkptr_t *bp = zio->io_bp;
3918 ddt_t *ddt = ddt_select(spa, bp);
3919 ddt_entry_t *dde = NULL;
3921 ASSERT(BP_GET_DEDUP(bp));
3922 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3924 ddt_enter(ddt);
3925 freedde = dde = ddt_lookup(ddt, bp);
3926 if (dde) {
3927 ddt_phys_variant_t v = ddt_phys_select(ddt, dde, bp);
3928 if (v != DDT_PHYS_NONE)
3929 ddt_phys_decref(dde->dde_phys, v);
3931 ddt_exit(ddt);
3934 * When no entry was found, it must have been pruned,
3935 * so we can free it now instead of decrementing the
3936 * refcount in the DDT.
3938 if (!dde) {
3939 BP_SET_DEDUP(bp, 0);
3940 zio->io_pipeline |= ZIO_STAGE_DVA_FREE;
3943 return (zio);
3947 * ==========================================================================
3948 * Allocate and free blocks
3949 * ==========================================================================
3952 static zio_t *
3953 zio_io_to_allocate(spa_t *spa, int allocator)
3955 zio_t *zio;
3957 ASSERT(MUTEX_HELD(&spa->spa_allocs[allocator].spaa_lock));
3959 zio = avl_first(&spa->spa_allocs[allocator].spaa_tree);
3960 if (zio == NULL)
3961 return (NULL);
3963 ASSERT(IO_IS_ALLOCATING(zio));
3964 ASSERT(ZIO_HAS_ALLOCATOR(zio));
3967 * Try to place a reservation for this zio. If we're unable to
3968 * reserve then we throttle.
3970 ASSERT3U(zio->io_allocator, ==, allocator);
3971 if (!metaslab_class_throttle_reserve(zio->io_metaslab_class,
3972 zio->io_prop.zp_copies, allocator, zio, 0)) {
3973 return (NULL);
3976 avl_remove(&spa->spa_allocs[allocator].spaa_tree, zio);
3977 ASSERT3U(zio->io_stage, <, ZIO_STAGE_DVA_ALLOCATE);
3979 return (zio);
3982 static zio_t *
3983 zio_dva_throttle(zio_t *zio)
3985 spa_t *spa = zio->io_spa;
3986 zio_t *nio;
3987 metaslab_class_t *mc;
3989 /* locate an appropriate allocation class */
3990 mc = spa_preferred_class(spa, zio);
3992 if (zio->io_priority == ZIO_PRIORITY_SYNC_WRITE ||
3993 !mc->mc_alloc_throttle_enabled ||
3994 zio->io_child_type == ZIO_CHILD_GANG ||
3995 zio->io_flags & ZIO_FLAG_NODATA) {
3996 return (zio);
3999 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
4000 ASSERT(ZIO_HAS_ALLOCATOR(zio));
4001 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
4002 ASSERT3U(zio->io_queued_timestamp, >, 0);
4003 ASSERT(zio->io_stage == ZIO_STAGE_DVA_THROTTLE);
4005 int allocator = zio->io_allocator;
4006 zio->io_metaslab_class = mc;
4007 mutex_enter(&spa->spa_allocs[allocator].spaa_lock);
4008 avl_add(&spa->spa_allocs[allocator].spaa_tree, zio);
4009 nio = zio_io_to_allocate(spa, allocator);
4010 mutex_exit(&spa->spa_allocs[allocator].spaa_lock);
4011 return (nio);
4014 static void
4015 zio_allocate_dispatch(spa_t *spa, int allocator)
4017 zio_t *zio;
4019 mutex_enter(&spa->spa_allocs[allocator].spaa_lock);
4020 zio = zio_io_to_allocate(spa, allocator);
4021 mutex_exit(&spa->spa_allocs[allocator].spaa_lock);
4022 if (zio == NULL)
4023 return;
4025 ASSERT3U(zio->io_stage, ==, ZIO_STAGE_DVA_THROTTLE);
4026 ASSERT0(zio->io_error);
4027 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_TRUE);
4030 static zio_t *
4031 zio_dva_allocate(zio_t *zio)
4033 spa_t *spa = zio->io_spa;
4034 metaslab_class_t *mc;
4035 blkptr_t *bp = zio->io_bp;
4036 int error;
4037 int flags = 0;
4039 if (zio->io_gang_leader == NULL) {
4040 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
4041 zio->io_gang_leader = zio;
4044 ASSERT(BP_IS_HOLE(bp));
4045 ASSERT0(BP_GET_NDVAS(bp));
4046 ASSERT3U(zio->io_prop.zp_copies, >, 0);
4047 ASSERT3U(zio->io_prop.zp_copies, <=, spa_max_replication(spa));
4048 ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
4050 if (zio->io_flags & ZIO_FLAG_NODATA)
4051 flags |= METASLAB_DONT_THROTTLE;
4052 if (zio->io_flags & ZIO_FLAG_GANG_CHILD)
4053 flags |= METASLAB_GANG_CHILD;
4054 if (zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE)
4055 flags |= METASLAB_ASYNC_ALLOC;
4058 * if not already chosen, locate an appropriate allocation class
4060 mc = zio->io_metaslab_class;
4061 if (mc == NULL) {
4062 mc = spa_preferred_class(spa, zio);
4063 zio->io_metaslab_class = mc;
4067 * Try allocating the block in the usual metaslab class.
4068 * If that's full, allocate it in the normal class.
4069 * If that's full, allocate as a gang block,
4070 * and if all are full, the allocation fails (which shouldn't happen).
4072 * Note that we do not fall back on embedded slog (ZIL) space, to
4073 * preserve unfragmented slog space, which is critical for decent
4074 * sync write performance. If a log allocation fails, we will fall
4075 * back to spa_sync() which is abysmal for performance.
4077 ASSERT(ZIO_HAS_ALLOCATOR(zio));
4078 error = metaslab_alloc(spa, mc, zio->io_size, bp,
4079 zio->io_prop.zp_copies, zio->io_txg, NULL, flags,
4080 &zio->io_alloc_list, zio, zio->io_allocator);
4083 * Fallback to normal class when an alloc class is full
4085 if (error == ENOSPC && mc != spa_normal_class(spa)) {
4087 * When the dedup or special class is spilling into the normal
4088 * class, there can still be significant space available due
4089 * to deferred frees that are in-flight. We track the txg when
4090 * this occurred and back off adding new DDT entries for a few
4091 * txgs to allow the free blocks to be processed.
4093 if ((mc == spa_dedup_class(spa) || (spa_special_has_ddt(spa) &&
4094 mc == spa_special_class(spa))) &&
4095 spa->spa_dedup_class_full_txg != zio->io_txg) {
4096 spa->spa_dedup_class_full_txg = zio->io_txg;
4097 zfs_dbgmsg("%s[%d]: %s class spilling, req size %d, "
4098 "%llu allocated of %llu",
4099 spa_name(spa), (int)zio->io_txg,
4100 mc == spa_dedup_class(spa) ? "dedup" : "special",
4101 (int)zio->io_size,
4102 (u_longlong_t)metaslab_class_get_alloc(mc),
4103 (u_longlong_t)metaslab_class_get_space(mc));
4107 * If throttling, transfer reservation over to normal class.
4108 * The io_allocator slot can remain the same even though we
4109 * are switching classes.
4111 if (mc->mc_alloc_throttle_enabled &&
4112 (zio->io_flags & ZIO_FLAG_IO_ALLOCATING)) {
4113 metaslab_class_throttle_unreserve(mc,
4114 zio->io_prop.zp_copies, zio->io_allocator, zio);
4115 zio->io_flags &= ~ZIO_FLAG_IO_ALLOCATING;
4117 VERIFY(metaslab_class_throttle_reserve(
4118 spa_normal_class(spa),
4119 zio->io_prop.zp_copies, zio->io_allocator, zio,
4120 flags | METASLAB_MUST_RESERVE));
4122 zio->io_metaslab_class = mc = spa_normal_class(spa);
4123 if (zfs_flags & ZFS_DEBUG_METASLAB_ALLOC) {
4124 zfs_dbgmsg("%s: metaslab allocation failure, "
4125 "trying normal class: zio %px, size %llu, error %d",
4126 spa_name(spa), zio, (u_longlong_t)zio->io_size,
4127 error);
4130 error = metaslab_alloc(spa, mc, zio->io_size, bp,
4131 zio->io_prop.zp_copies, zio->io_txg, NULL, flags,
4132 &zio->io_alloc_list, zio, zio->io_allocator);
4135 if (error == ENOSPC && zio->io_size > SPA_MINBLOCKSIZE) {
4136 if (zfs_flags & ZFS_DEBUG_METASLAB_ALLOC) {
4137 zfs_dbgmsg("%s: metaslab allocation failure, "
4138 "trying ganging: zio %px, size %llu, error %d",
4139 spa_name(spa), zio, (u_longlong_t)zio->io_size,
4140 error);
4142 return (zio_write_gang_block(zio, mc));
4144 if (error != 0) {
4145 if (error != ENOSPC ||
4146 (zfs_flags & ZFS_DEBUG_METASLAB_ALLOC)) {
4147 zfs_dbgmsg("%s: metaslab allocation failure: zio %px, "
4148 "size %llu, error %d",
4149 spa_name(spa), zio, (u_longlong_t)zio->io_size,
4150 error);
4152 zio->io_error = error;
4155 return (zio);
4158 static zio_t *
4159 zio_dva_free(zio_t *zio)
4161 metaslab_free(zio->io_spa, zio->io_bp, zio->io_txg, B_FALSE);
4163 return (zio);
4166 static zio_t *
4167 zio_dva_claim(zio_t *zio)
4169 int error;
4171 error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg);
4172 if (error)
4173 zio->io_error = error;
4175 return (zio);
4179 * Undo an allocation. This is used by zio_done() when an I/O fails
4180 * and we want to give back the block we just allocated.
4181 * This handles both normal blocks and gang blocks.
4183 static void
4184 zio_dva_unallocate(zio_t *zio, zio_gang_node_t *gn, blkptr_t *bp)
4186 ASSERT(BP_GET_LOGICAL_BIRTH(bp) == zio->io_txg || BP_IS_HOLE(bp));
4187 ASSERT(zio->io_bp_override == NULL);
4189 if (!BP_IS_HOLE(bp)) {
4190 metaslab_free(zio->io_spa, bp, BP_GET_LOGICAL_BIRTH(bp),
4191 B_TRUE);
4194 if (gn != NULL) {
4195 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
4196 zio_dva_unallocate(zio, gn->gn_child[g],
4197 &gn->gn_gbh->zg_blkptr[g]);
4203 * Try to allocate an intent log block. Return 0 on success, errno on failure.
4206 zio_alloc_zil(spa_t *spa, objset_t *os, uint64_t txg, blkptr_t *new_bp,
4207 uint64_t size, boolean_t *slog)
4209 int error = 1;
4210 zio_alloc_list_t io_alloc_list;
4212 ASSERT(txg > spa_syncing_txg(spa));
4214 metaslab_trace_init(&io_alloc_list);
4217 * Block pointer fields are useful to metaslabs for stats and debugging.
4218 * Fill in the obvious ones before calling into metaslab_alloc().
4220 BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
4221 BP_SET_PSIZE(new_bp, size);
4222 BP_SET_LEVEL(new_bp, 0);
4225 * When allocating a zil block, we don't have information about
4226 * the final destination of the block except the objset it's part
4227 * of, so we just hash the objset ID to pick the allocator to get
4228 * some parallelism.
4230 int flags = METASLAB_ZIL;
4231 int allocator = (uint_t)cityhash1(os->os_dsl_dataset->ds_object)
4232 % spa->spa_alloc_count;
4233 error = metaslab_alloc(spa, spa_log_class(spa), size, new_bp, 1,
4234 txg, NULL, flags, &io_alloc_list, NULL, allocator);
4235 *slog = (error == 0);
4236 if (error != 0) {
4237 error = metaslab_alloc(spa, spa_embedded_log_class(spa), size,
4238 new_bp, 1, txg, NULL, flags,
4239 &io_alloc_list, NULL, allocator);
4241 if (error != 0) {
4242 error = metaslab_alloc(spa, spa_normal_class(spa), size,
4243 new_bp, 1, txg, NULL, flags,
4244 &io_alloc_list, NULL, allocator);
4246 metaslab_trace_fini(&io_alloc_list);
4248 if (error == 0) {
4249 BP_SET_LSIZE(new_bp, size);
4250 BP_SET_PSIZE(new_bp, size);
4251 BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF);
4252 BP_SET_CHECKSUM(new_bp,
4253 spa_version(spa) >= SPA_VERSION_SLIM_ZIL
4254 ? ZIO_CHECKSUM_ZILOG2 : ZIO_CHECKSUM_ZILOG);
4255 BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
4256 BP_SET_LEVEL(new_bp, 0);
4257 BP_SET_DEDUP(new_bp, 0);
4258 BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER);
4261 * encrypted blocks will require an IV and salt. We generate
4262 * these now since we will not be rewriting the bp at
4263 * rewrite time.
4265 if (os->os_encrypted) {
4266 uint8_t iv[ZIO_DATA_IV_LEN];
4267 uint8_t salt[ZIO_DATA_SALT_LEN];
4269 BP_SET_CRYPT(new_bp, B_TRUE);
4270 VERIFY0(spa_crypt_get_salt(spa,
4271 dmu_objset_id(os), salt));
4272 VERIFY0(zio_crypt_generate_iv(iv));
4274 zio_crypt_encode_params_bp(new_bp, salt, iv);
4276 } else {
4277 zfs_dbgmsg("%s: zil block allocation failure: "
4278 "size %llu, error %d", spa_name(spa), (u_longlong_t)size,
4279 error);
4282 return (error);
4286 * ==========================================================================
4287 * Read and write to physical devices
4288 * ==========================================================================
4292 * Issue an I/O to the underlying vdev. Typically the issue pipeline
4293 * stops after this stage and will resume upon I/O completion.
4294 * However, there are instances where the vdev layer may need to
4295 * continue the pipeline when an I/O was not issued. Since the I/O
4296 * that was sent to the vdev layer might be different than the one
4297 * currently active in the pipeline (see vdev_queue_io()), we explicitly
4298 * force the underlying vdev layers to call either zio_execute() or
4299 * zio_interrupt() to ensure that the pipeline continues with the correct I/O.
4301 static zio_t *
4302 zio_vdev_io_start(zio_t *zio)
4304 vdev_t *vd = zio->io_vd;
4305 uint64_t align;
4306 spa_t *spa = zio->io_spa;
4308 zio->io_delay = 0;
4310 ASSERT(zio->io_error == 0);
4311 ASSERT(zio->io_child_error[ZIO_CHILD_VDEV] == 0);
4313 if (vd == NULL) {
4314 if (!(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
4315 spa_config_enter(spa, SCL_ZIO, zio, RW_READER);
4318 * The mirror_ops handle multiple DVAs in a single BP.
4320 vdev_mirror_ops.vdev_op_io_start(zio);
4321 return (NULL);
4324 ASSERT3P(zio->io_logical, !=, zio);
4325 if (zio->io_type == ZIO_TYPE_WRITE) {
4326 ASSERT(spa->spa_trust_config);
4329 * Note: the code can handle other kinds of writes,
4330 * but we don't expect them.
4332 if (zio->io_vd->vdev_noalloc) {
4333 ASSERT(zio->io_flags &
4334 (ZIO_FLAG_PHYSICAL | ZIO_FLAG_SELF_HEAL |
4335 ZIO_FLAG_RESILVER | ZIO_FLAG_INDUCE_DAMAGE));
4339 align = 1ULL << vd->vdev_top->vdev_ashift;
4341 if (!(zio->io_flags & ZIO_FLAG_PHYSICAL) &&
4342 P2PHASE(zio->io_size, align) != 0) {
4343 /* Transform logical writes to be a full physical block size. */
4344 uint64_t asize = P2ROUNDUP(zio->io_size, align);
4345 abd_t *abuf = abd_alloc_sametype(zio->io_abd, asize);
4346 ASSERT(vd == vd->vdev_top);
4347 if (zio->io_type == ZIO_TYPE_WRITE) {
4348 abd_copy(abuf, zio->io_abd, zio->io_size);
4349 abd_zero_off(abuf, zio->io_size, asize - zio->io_size);
4351 zio_push_transform(zio, abuf, asize, asize, zio_subblock);
4355 * If this is not a physical io, make sure that it is properly aligned
4356 * before proceeding.
4358 if (!(zio->io_flags & ZIO_FLAG_PHYSICAL)) {
4359 ASSERT0(P2PHASE(zio->io_offset, align));
4360 ASSERT0(P2PHASE(zio->io_size, align));
4361 } else {
4363 * For physical writes, we allow 512b aligned writes and assume
4364 * the device will perform a read-modify-write as necessary.
4366 ASSERT0(P2PHASE(zio->io_offset, SPA_MINBLOCKSIZE));
4367 ASSERT0(P2PHASE(zio->io_size, SPA_MINBLOCKSIZE));
4370 VERIFY(zio->io_type != ZIO_TYPE_WRITE || spa_writeable(spa));
4373 * If this is a repair I/O, and there's no self-healing involved --
4374 * that is, we're just resilvering what we expect to resilver --
4375 * then don't do the I/O unless zio's txg is actually in vd's DTL.
4376 * This prevents spurious resilvering.
4378 * There are a few ways that we can end up creating these spurious
4379 * resilver i/os:
4381 * 1. A resilver i/o will be issued if any DVA in the BP has a
4382 * dirty DTL. The mirror code will issue resilver writes to
4383 * each DVA, including the one(s) that are not on vdevs with dirty
4384 * DTLs.
4386 * 2. With nested replication, which happens when we have a
4387 * "replacing" or "spare" vdev that's a child of a mirror or raidz.
4388 * For example, given mirror(replacing(A+B), C), it's likely that
4389 * only A is out of date (it's the new device). In this case, we'll
4390 * read from C, then use the data to resilver A+B -- but we don't
4391 * actually want to resilver B, just A. The top-level mirror has no
4392 * way to know this, so instead we just discard unnecessary repairs
4393 * as we work our way down the vdev tree.
4395 * 3. ZTEST also creates mirrors of mirrors, mirrors of raidz, etc.
4396 * The same logic applies to any form of nested replication: ditto
4397 * + mirror, RAID-Z + replacing, etc.
4399 * However, indirect vdevs point off to other vdevs which may have
4400 * DTL's, so we never bypass them. The child i/os on concrete vdevs
4401 * will be properly bypassed instead.
4403 * Leaf DTL_PARTIAL can be empty when a legitimate write comes from
4404 * a dRAID spare vdev. For example, when a dRAID spare is first
4405 * used, its spare blocks need to be written to but the leaf vdev's
4406 * of such blocks can have empty DTL_PARTIAL.
4408 * There seemed no clean way to allow such writes while bypassing
4409 * spurious ones. At this point, just avoid all bypassing for dRAID
4410 * for correctness.
4412 if ((zio->io_flags & ZIO_FLAG_IO_REPAIR) &&
4413 !(zio->io_flags & ZIO_FLAG_SELF_HEAL) &&
4414 zio->io_txg != 0 && /* not a delegated i/o */
4415 vd->vdev_ops != &vdev_indirect_ops &&
4416 vd->vdev_top->vdev_ops != &vdev_draid_ops &&
4417 !vdev_dtl_contains(vd, DTL_PARTIAL, zio->io_txg, 1)) {
4418 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
4419 zio_vdev_io_bypass(zio);
4420 return (zio);
4424 * Select the next best leaf I/O to process. Distributed spares are
4425 * excluded since they dispatch the I/O directly to a leaf vdev after
4426 * applying the dRAID mapping.
4428 if (vd->vdev_ops->vdev_op_leaf &&
4429 vd->vdev_ops != &vdev_draid_spare_ops &&
4430 (zio->io_type == ZIO_TYPE_READ ||
4431 zio->io_type == ZIO_TYPE_WRITE ||
4432 zio->io_type == ZIO_TYPE_TRIM)) {
4434 if (zio_handle_device_injection(vd, zio, ENOSYS) != 0) {
4436 * "no-op" injections return success, but do no actual
4437 * work. Just skip the remaining vdev stages.
4439 zio_vdev_io_bypass(zio);
4440 zio_interrupt(zio);
4441 return (NULL);
4444 if ((zio = vdev_queue_io(zio)) == NULL)
4445 return (NULL);
4447 if (!vdev_accessible(vd, zio)) {
4448 zio->io_error = SET_ERROR(ENXIO);
4449 zio_interrupt(zio);
4450 return (NULL);
4452 zio->io_delay = gethrtime();
4455 vd->vdev_ops->vdev_op_io_start(zio);
4456 return (NULL);
4459 static zio_t *
4460 zio_vdev_io_done(zio_t *zio)
4462 vdev_t *vd = zio->io_vd;
4463 vdev_ops_t *ops = vd ? vd->vdev_ops : &vdev_mirror_ops;
4464 boolean_t unexpected_error = B_FALSE;
4466 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV_BIT, ZIO_WAIT_DONE)) {
4467 return (NULL);
4470 ASSERT(zio->io_type == ZIO_TYPE_READ ||
4471 zio->io_type == ZIO_TYPE_WRITE ||
4472 zio->io_type == ZIO_TYPE_FLUSH ||
4473 zio->io_type == ZIO_TYPE_TRIM);
4475 if (zio->io_delay)
4476 zio->io_delay = gethrtime() - zio->io_delay;
4478 if (vd != NULL && vd->vdev_ops->vdev_op_leaf &&
4479 vd->vdev_ops != &vdev_draid_spare_ops) {
4480 if (zio->io_type != ZIO_TYPE_FLUSH)
4481 vdev_queue_io_done(zio);
4483 if (zio_injection_enabled && zio->io_error == 0)
4484 zio->io_error = zio_handle_device_injections(vd, zio,
4485 EIO, EILSEQ);
4487 if (zio_injection_enabled && zio->io_error == 0)
4488 zio->io_error = zio_handle_label_injection(zio, EIO);
4490 if (zio->io_error && zio->io_type != ZIO_TYPE_FLUSH &&
4491 zio->io_type != ZIO_TYPE_TRIM) {
4492 if (!vdev_accessible(vd, zio)) {
4493 zio->io_error = SET_ERROR(ENXIO);
4494 } else {
4495 unexpected_error = B_TRUE;
4500 ops->vdev_op_io_done(zio);
4502 if (unexpected_error && vd->vdev_remove_wanted == B_FALSE)
4503 VERIFY(vdev_probe(vd, zio) == NULL);
4505 return (zio);
4509 * This function is used to change the priority of an existing zio that is
4510 * currently in-flight. This is used by the arc to upgrade priority in the
4511 * event that a demand read is made for a block that is currently queued
4512 * as a scrub or async read IO. Otherwise, the high priority read request
4513 * would end up having to wait for the lower priority IO.
4515 void
4516 zio_change_priority(zio_t *pio, zio_priority_t priority)
4518 zio_t *cio, *cio_next;
4519 zio_link_t *zl = NULL;
4521 ASSERT3U(priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
4523 if (pio->io_vd != NULL && pio->io_vd->vdev_ops->vdev_op_leaf) {
4524 vdev_queue_change_io_priority(pio, priority);
4525 } else {
4526 pio->io_priority = priority;
4529 mutex_enter(&pio->io_lock);
4530 for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) {
4531 cio_next = zio_walk_children(pio, &zl);
4532 zio_change_priority(cio, priority);
4534 mutex_exit(&pio->io_lock);
4538 * For non-raidz ZIOs, we can just copy aside the bad data read from the
4539 * disk, and use that to finish the checksum ereport later.
4541 static void
4542 zio_vsd_default_cksum_finish(zio_cksum_report_t *zcr,
4543 const abd_t *good_buf)
4545 /* no processing needed */
4546 zfs_ereport_finish_checksum(zcr, good_buf, zcr->zcr_cbdata, B_FALSE);
4549 void
4550 zio_vsd_default_cksum_report(zio_t *zio, zio_cksum_report_t *zcr)
4552 void *abd = abd_alloc_sametype(zio->io_abd, zio->io_size);
4554 abd_copy(abd, zio->io_abd, zio->io_size);
4556 zcr->zcr_cbinfo = zio->io_size;
4557 zcr->zcr_cbdata = abd;
4558 zcr->zcr_finish = zio_vsd_default_cksum_finish;
4559 zcr->zcr_free = zio_abd_free;
4562 static zio_t *
4563 zio_vdev_io_assess(zio_t *zio)
4565 vdev_t *vd = zio->io_vd;
4567 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV_BIT, ZIO_WAIT_DONE)) {
4568 return (NULL);
4571 if (vd == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
4572 spa_config_exit(zio->io_spa, SCL_ZIO, zio);
4574 if (zio->io_vsd != NULL) {
4575 zio->io_vsd_ops->vsd_free(zio);
4576 zio->io_vsd = NULL;
4580 * If a Direct I/O operation has a checksum verify error then this I/O
4581 * should not attempt to be issued again.
4583 if (zio->io_flags & ZIO_FLAG_DIO_CHKSUM_ERR) {
4584 if (zio->io_type == ZIO_TYPE_WRITE) {
4585 ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_LOGICAL);
4586 ASSERT3U(zio->io_error, ==, EIO);
4588 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
4589 return (zio);
4592 if (zio_injection_enabled && zio->io_error == 0)
4593 zio->io_error = zio_handle_fault_injection(zio, EIO);
4596 * If the I/O failed, determine whether we should attempt to retry it.
4598 * On retry, we cut in line in the issue queue, since we don't want
4599 * compression/checksumming/etc. work to prevent our (cheap) IO reissue.
4601 if (zio->io_error && vd == NULL &&
4602 !(zio->io_flags & (ZIO_FLAG_DONT_RETRY | ZIO_FLAG_IO_RETRY))) {
4603 ASSERT(!(zio->io_flags & ZIO_FLAG_DONT_QUEUE)); /* not a leaf */
4604 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_BYPASS)); /* not a leaf */
4605 zio->io_error = 0;
4606 zio->io_flags |= ZIO_FLAG_IO_RETRY | ZIO_FLAG_DONT_AGGREGATE;
4607 zio->io_stage = ZIO_STAGE_VDEV_IO_START >> 1;
4608 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE,
4609 zio_requeue_io_start_cut_in_line);
4610 return (NULL);
4614 * If we got an error on a leaf device, convert it to ENXIO
4615 * if the device is not accessible at all.
4617 if (zio->io_error && vd != NULL && vd->vdev_ops->vdev_op_leaf &&
4618 !vdev_accessible(vd, zio))
4619 zio->io_error = SET_ERROR(ENXIO);
4622 * If we can't write to an interior vdev (mirror or RAID-Z),
4623 * set vdev_cant_write so that we stop trying to allocate from it.
4625 if (zio->io_error == ENXIO && zio->io_type == ZIO_TYPE_WRITE &&
4626 vd != NULL && !vd->vdev_ops->vdev_op_leaf) {
4627 vdev_dbgmsg(vd, "zio_vdev_io_assess(zio=%px) setting "
4628 "cant_write=TRUE due to write failure with ENXIO",
4629 zio);
4630 vd->vdev_cant_write = B_TRUE;
4634 * If a cache flush returns ENOTSUP or ENOTTY, we know that no future
4635 * attempts will ever succeed. In this case we set a persistent
4636 * boolean flag so that we don't bother with it in the future.
4638 if ((zio->io_error == ENOTSUP || zio->io_error == ENOTTY) &&
4639 zio->io_type == ZIO_TYPE_FLUSH && vd != NULL)
4640 vd->vdev_nowritecache = B_TRUE;
4642 if (zio->io_error)
4643 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
4645 return (zio);
4648 void
4649 zio_vdev_io_reissue(zio_t *zio)
4651 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
4652 ASSERT(zio->io_error == 0);
4654 zio->io_stage >>= 1;
4657 void
4658 zio_vdev_io_redone(zio_t *zio)
4660 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE);
4662 zio->io_stage >>= 1;
4665 void
4666 zio_vdev_io_bypass(zio_t *zio)
4668 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
4669 ASSERT(zio->io_error == 0);
4671 zio->io_flags |= ZIO_FLAG_IO_BYPASS;
4672 zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS >> 1;
4676 * ==========================================================================
4677 * Encrypt and store encryption parameters
4678 * ==========================================================================
4683 * This function is used for ZIO_STAGE_ENCRYPT. It is responsible for
4684 * managing the storage of encryption parameters and passing them to the
4685 * lower-level encryption functions.
4687 static zio_t *
4688 zio_encrypt(zio_t *zio)
4690 zio_prop_t *zp = &zio->io_prop;
4691 spa_t *spa = zio->io_spa;
4692 blkptr_t *bp = zio->io_bp;
4693 uint64_t psize = BP_GET_PSIZE(bp);
4694 uint64_t dsobj = zio->io_bookmark.zb_objset;
4695 dmu_object_type_t ot = BP_GET_TYPE(bp);
4696 void *enc_buf = NULL;
4697 abd_t *eabd = NULL;
4698 uint8_t salt[ZIO_DATA_SALT_LEN];
4699 uint8_t iv[ZIO_DATA_IV_LEN];
4700 uint8_t mac[ZIO_DATA_MAC_LEN];
4701 boolean_t no_crypt = B_FALSE;
4703 /* the root zio already encrypted the data */
4704 if (zio->io_child_type == ZIO_CHILD_GANG)
4705 return (zio);
4707 /* only ZIL blocks are re-encrypted on rewrite */
4708 if (!IO_IS_ALLOCATING(zio) && ot != DMU_OT_INTENT_LOG)
4709 return (zio);
4711 if (!(zp->zp_encrypt || BP_IS_ENCRYPTED(bp))) {
4712 BP_SET_CRYPT(bp, B_FALSE);
4713 return (zio);
4716 /* if we are doing raw encryption set the provided encryption params */
4717 if (zio->io_flags & ZIO_FLAG_RAW_ENCRYPT) {
4718 ASSERT0(BP_GET_LEVEL(bp));
4719 BP_SET_CRYPT(bp, B_TRUE);
4720 BP_SET_BYTEORDER(bp, zp->zp_byteorder);
4721 if (ot != DMU_OT_OBJSET)
4722 zio_crypt_encode_mac_bp(bp, zp->zp_mac);
4724 /* dnode blocks must be written out in the provided byteorder */
4725 if (zp->zp_byteorder != ZFS_HOST_BYTEORDER &&
4726 ot == DMU_OT_DNODE) {
4727 void *bswap_buf = zio_buf_alloc(psize);
4728 abd_t *babd = abd_get_from_buf(bswap_buf, psize);
4730 ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF);
4731 abd_copy_to_buf(bswap_buf, zio->io_abd, psize);
4732 dmu_ot_byteswap[DMU_OT_BYTESWAP(ot)].ob_func(bswap_buf,
4733 psize);
4735 abd_take_ownership_of_buf(babd, B_TRUE);
4736 zio_push_transform(zio, babd, psize, psize, NULL);
4739 if (DMU_OT_IS_ENCRYPTED(ot))
4740 zio_crypt_encode_params_bp(bp, zp->zp_salt, zp->zp_iv);
4741 return (zio);
4744 /* indirect blocks only maintain a cksum of the lower level MACs */
4745 if (BP_GET_LEVEL(bp) > 0) {
4746 BP_SET_CRYPT(bp, B_TRUE);
4747 VERIFY0(zio_crypt_do_indirect_mac_checksum_abd(B_TRUE,
4748 zio->io_orig_abd, BP_GET_LSIZE(bp), BP_SHOULD_BYTESWAP(bp),
4749 mac));
4750 zio_crypt_encode_mac_bp(bp, mac);
4751 return (zio);
4755 * Objset blocks are a special case since they have 2 256-bit MACs
4756 * embedded within them.
4758 if (ot == DMU_OT_OBJSET) {
4759 ASSERT0(DMU_OT_IS_ENCRYPTED(ot));
4760 ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF);
4761 BP_SET_CRYPT(bp, B_TRUE);
4762 VERIFY0(spa_do_crypt_objset_mac_abd(B_TRUE, spa, dsobj,
4763 zio->io_abd, psize, BP_SHOULD_BYTESWAP(bp)));
4764 return (zio);
4767 /* unencrypted object types are only authenticated with a MAC */
4768 if (!DMU_OT_IS_ENCRYPTED(ot)) {
4769 BP_SET_CRYPT(bp, B_TRUE);
4770 VERIFY0(spa_do_crypt_mac_abd(B_TRUE, spa, dsobj,
4771 zio->io_abd, psize, mac));
4772 zio_crypt_encode_mac_bp(bp, mac);
4773 return (zio);
4777 * Later passes of sync-to-convergence may decide to rewrite data
4778 * in place to avoid more disk reallocations. This presents a problem
4779 * for encryption because this constitutes rewriting the new data with
4780 * the same encryption key and IV. However, this only applies to blocks
4781 * in the MOS (particularly the spacemaps) and we do not encrypt the
4782 * MOS. We assert that the zio is allocating or an intent log write
4783 * to enforce this.
4785 ASSERT(IO_IS_ALLOCATING(zio) || ot == DMU_OT_INTENT_LOG);
4786 ASSERT(BP_GET_LEVEL(bp) == 0 || ot == DMU_OT_INTENT_LOG);
4787 ASSERT(spa_feature_is_active(spa, SPA_FEATURE_ENCRYPTION));
4788 ASSERT3U(psize, !=, 0);
4790 enc_buf = zio_buf_alloc(psize);
4791 eabd = abd_get_from_buf(enc_buf, psize);
4792 abd_take_ownership_of_buf(eabd, B_TRUE);
4795 * For an explanation of what encryption parameters are stored
4796 * where, see the block comment in zio_crypt.c.
4798 if (ot == DMU_OT_INTENT_LOG) {
4799 zio_crypt_decode_params_bp(bp, salt, iv);
4800 } else {
4801 BP_SET_CRYPT(bp, B_TRUE);
4804 /* Perform the encryption. This should not fail */
4805 VERIFY0(spa_do_crypt_abd(B_TRUE, spa, &zio->io_bookmark,
4806 BP_GET_TYPE(bp), BP_GET_DEDUP(bp), BP_SHOULD_BYTESWAP(bp),
4807 salt, iv, mac, psize, zio->io_abd, eabd, &no_crypt));
4809 /* encode encryption metadata into the bp */
4810 if (ot == DMU_OT_INTENT_LOG) {
4812 * ZIL blocks store the MAC in the embedded checksum, so the
4813 * transform must always be applied.
4815 zio_crypt_encode_mac_zil(enc_buf, mac);
4816 zio_push_transform(zio, eabd, psize, psize, NULL);
4817 } else {
4818 BP_SET_CRYPT(bp, B_TRUE);
4819 zio_crypt_encode_params_bp(bp, salt, iv);
4820 zio_crypt_encode_mac_bp(bp, mac);
4822 if (no_crypt) {
4823 ASSERT3U(ot, ==, DMU_OT_DNODE);
4824 abd_free(eabd);
4825 } else {
4826 zio_push_transform(zio, eabd, psize, psize, NULL);
4830 return (zio);
4834 * ==========================================================================
4835 * Generate and verify checksums
4836 * ==========================================================================
4838 static zio_t *
4839 zio_checksum_generate(zio_t *zio)
4841 blkptr_t *bp = zio->io_bp;
4842 enum zio_checksum checksum;
4844 if (bp == NULL) {
4846 * This is zio_write_phys().
4847 * We're either generating a label checksum, or none at all.
4849 checksum = zio->io_prop.zp_checksum;
4851 if (checksum == ZIO_CHECKSUM_OFF)
4852 return (zio);
4854 ASSERT(checksum == ZIO_CHECKSUM_LABEL);
4855 } else {
4856 if (BP_IS_GANG(bp) && zio->io_child_type == ZIO_CHILD_GANG) {
4857 ASSERT(!IO_IS_ALLOCATING(zio));
4858 checksum = ZIO_CHECKSUM_GANG_HEADER;
4859 } else {
4860 checksum = BP_GET_CHECKSUM(bp);
4864 zio_checksum_compute(zio, checksum, zio->io_abd, zio->io_size);
4866 return (zio);
4869 static zio_t *
4870 zio_checksum_verify(zio_t *zio)
4872 zio_bad_cksum_t info;
4873 blkptr_t *bp = zio->io_bp;
4874 int error;
4876 ASSERT(zio->io_vd != NULL);
4878 if (bp == NULL) {
4880 * This is zio_read_phys().
4881 * We're either verifying a label checksum, or nothing at all.
4883 if (zio->io_prop.zp_checksum == ZIO_CHECKSUM_OFF)
4884 return (zio);
4886 ASSERT3U(zio->io_prop.zp_checksum, ==, ZIO_CHECKSUM_LABEL);
4889 ASSERT0(zio->io_flags & ZIO_FLAG_DIO_CHKSUM_ERR);
4890 IMPLY(zio->io_flags & ZIO_FLAG_DIO_READ,
4891 !(zio->io_flags & ZIO_FLAG_SPECULATIVE));
4893 if ((error = zio_checksum_error(zio, &info)) != 0) {
4894 zio->io_error = error;
4895 if (error == ECKSUM &&
4896 !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
4897 if (zio->io_flags & ZIO_FLAG_DIO_READ) {
4898 zio->io_flags |= ZIO_FLAG_DIO_CHKSUM_ERR;
4899 zio_t *pio = zio_unique_parent(zio);
4901 * Any Direct I/O read that has a checksum
4902 * error must be treated as suspicous as the
4903 * contents of the buffer could be getting
4904 * manipulated while the I/O is taking place.
4906 * The checksum verify error will only be
4907 * reported here for disk and file VDEV's and
4908 * will be reported on those that the failure
4909 * occurred on. Other types of VDEV's report the
4910 * verify failure in their own code paths.
4912 if (pio->io_child_type == ZIO_CHILD_LOGICAL) {
4913 zio_dio_chksum_verify_error_report(zio);
4915 } else {
4916 mutex_enter(&zio->io_vd->vdev_stat_lock);
4917 zio->io_vd->vdev_stat.vs_checksum_errors++;
4918 mutex_exit(&zio->io_vd->vdev_stat_lock);
4919 (void) zfs_ereport_start_checksum(zio->io_spa,
4920 zio->io_vd, &zio->io_bookmark, zio,
4921 zio->io_offset, zio->io_size, &info);
4926 return (zio);
4929 static zio_t *
4930 zio_dio_checksum_verify(zio_t *zio)
4932 zio_t *pio = zio_unique_parent(zio);
4933 int error;
4935 ASSERT3P(zio->io_vd, !=, NULL);
4936 ASSERT3P(zio->io_bp, !=, NULL);
4937 ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
4938 ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE);
4939 ASSERT3B(pio->io_prop.zp_direct_write, ==, B_TRUE);
4940 ASSERT3U(pio->io_child_type, ==, ZIO_CHILD_LOGICAL);
4942 if (zfs_vdev_direct_write_verify == 0 || zio->io_error != 0)
4943 goto out;
4945 if ((error = zio_checksum_error(zio, NULL)) != 0) {
4946 zio->io_error = error;
4947 if (error == ECKSUM) {
4948 zio->io_flags |= ZIO_FLAG_DIO_CHKSUM_ERR;
4949 zio_dio_chksum_verify_error_report(zio);
4953 out:
4954 return (zio);
4959 * Called by RAID-Z to ensure we don't compute the checksum twice.
4961 void
4962 zio_checksum_verified(zio_t *zio)
4964 zio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
4968 * Report Direct I/O checksum verify error and create ZED event.
4970 void
4971 zio_dio_chksum_verify_error_report(zio_t *zio)
4973 ASSERT(zio->io_flags & ZIO_FLAG_DIO_CHKSUM_ERR);
4975 if (zio->io_child_type == ZIO_CHILD_LOGICAL)
4976 return;
4978 mutex_enter(&zio->io_vd->vdev_stat_lock);
4979 zio->io_vd->vdev_stat.vs_dio_verify_errors++;
4980 mutex_exit(&zio->io_vd->vdev_stat_lock);
4981 if (zio->io_type == ZIO_TYPE_WRITE) {
4983 * Convert checksum error for writes into EIO.
4985 zio->io_error = SET_ERROR(EIO);
4987 * Report dio_verify_wr ZED event.
4989 (void) zfs_ereport_post(FM_EREPORT_ZFS_DIO_VERIFY_WR,
4990 zio->io_spa, zio->io_vd, &zio->io_bookmark, zio, 0);
4991 } else {
4993 * Report dio_verify_rd ZED event.
4995 (void) zfs_ereport_post(FM_EREPORT_ZFS_DIO_VERIFY_RD,
4996 zio->io_spa, zio->io_vd, &zio->io_bookmark, zio, 0);
5001 * ==========================================================================
5002 * Error rank. Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other.
5003 * An error of 0 indicates success. ENXIO indicates whole-device failure,
5004 * which may be transient (e.g. unplugged) or permanent. ECKSUM and EIO
5005 * indicate errors that are specific to one I/O, and most likely permanent.
5006 * Any other error is presumed to be worse because we weren't expecting it.
5007 * ==========================================================================
5010 zio_worst_error(int e1, int e2)
5012 static int zio_error_rank[] = { 0, ENXIO, ECKSUM, EIO };
5013 int r1, r2;
5015 for (r1 = 0; r1 < sizeof (zio_error_rank) / sizeof (int); r1++)
5016 if (e1 == zio_error_rank[r1])
5017 break;
5019 for (r2 = 0; r2 < sizeof (zio_error_rank) / sizeof (int); r2++)
5020 if (e2 == zio_error_rank[r2])
5021 break;
5023 return (r1 > r2 ? e1 : e2);
5027 * ==========================================================================
5028 * I/O completion
5029 * ==========================================================================
5031 static zio_t *
5032 zio_ready(zio_t *zio)
5034 blkptr_t *bp = zio->io_bp;
5035 zio_t *pio, *pio_next;
5036 zio_link_t *zl = NULL;
5038 if (zio_wait_for_children(zio, ZIO_CHILD_LOGICAL_BIT |
5039 ZIO_CHILD_GANG_BIT | ZIO_CHILD_DDT_BIT, ZIO_WAIT_READY)) {
5040 return (NULL);
5043 if (zio->io_ready) {
5044 ASSERT(IO_IS_ALLOCATING(zio));
5045 ASSERT(BP_GET_LOGICAL_BIRTH(bp) == zio->io_txg ||
5046 BP_IS_HOLE(bp) || (zio->io_flags & ZIO_FLAG_NOPWRITE));
5047 ASSERT(zio->io_children[ZIO_CHILD_GANG][ZIO_WAIT_READY] == 0);
5049 zio->io_ready(zio);
5052 #ifdef ZFS_DEBUG
5053 if (bp != NULL && bp != &zio->io_bp_copy)
5054 zio->io_bp_copy = *bp;
5055 #endif
5057 if (zio->io_error != 0) {
5058 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
5060 if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
5061 ASSERT(IO_IS_ALLOCATING(zio));
5062 ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
5063 ASSERT(zio->io_metaslab_class != NULL);
5064 ASSERT(ZIO_HAS_ALLOCATOR(zio));
5067 * We were unable to allocate anything, unreserve and
5068 * issue the next I/O to allocate.
5070 metaslab_class_throttle_unreserve(
5071 zio->io_metaslab_class, zio->io_prop.zp_copies,
5072 zio->io_allocator, zio);
5073 zio_allocate_dispatch(zio->io_spa, zio->io_allocator);
5077 mutex_enter(&zio->io_lock);
5078 zio->io_state[ZIO_WAIT_READY] = 1;
5079 pio = zio_walk_parents(zio, &zl);
5080 mutex_exit(&zio->io_lock);
5083 * As we notify zio's parents, new parents could be added.
5084 * New parents go to the head of zio's io_parent_list, however,
5085 * so we will (correctly) not notify them. The remainder of zio's
5086 * io_parent_list, from 'pio_next' onward, cannot change because
5087 * all parents must wait for us to be done before they can be done.
5089 for (; pio != NULL; pio = pio_next) {
5090 pio_next = zio_walk_parents(zio, &zl);
5091 zio_notify_parent(pio, zio, ZIO_WAIT_READY, NULL);
5094 if (zio->io_flags & ZIO_FLAG_NODATA) {
5095 if (bp != NULL && BP_IS_GANG(bp)) {
5096 zio->io_flags &= ~ZIO_FLAG_NODATA;
5097 } else {
5098 ASSERT((uintptr_t)zio->io_abd < SPA_MAXBLOCKSIZE);
5099 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
5103 if (zio_injection_enabled &&
5104 zio->io_spa->spa_syncing_txg == zio->io_txg)
5105 zio_handle_ignored_writes(zio);
5107 return (zio);
5111 * Update the allocation throttle accounting.
5113 static void
5114 zio_dva_throttle_done(zio_t *zio)
5116 zio_t *lio __maybe_unused = zio->io_logical;
5117 zio_t *pio = zio_unique_parent(zio);
5118 vdev_t *vd = zio->io_vd;
5119 int flags = METASLAB_ASYNC_ALLOC;
5121 ASSERT3P(zio->io_bp, !=, NULL);
5122 ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE);
5123 ASSERT3U(zio->io_priority, ==, ZIO_PRIORITY_ASYNC_WRITE);
5124 ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
5125 ASSERT(vd != NULL);
5126 ASSERT3P(vd, ==, vd->vdev_top);
5127 ASSERT(zio_injection_enabled || !(zio->io_flags & ZIO_FLAG_IO_RETRY));
5128 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REPAIR));
5129 ASSERT(zio->io_flags & ZIO_FLAG_IO_ALLOCATING);
5130 ASSERT(!(lio->io_flags & ZIO_FLAG_IO_REWRITE));
5131 ASSERT(!(lio->io_orig_flags & ZIO_FLAG_NODATA));
5134 * Parents of gang children can have two flavors -- ones that
5135 * allocated the gang header (will have ZIO_FLAG_IO_REWRITE set)
5136 * and ones that allocated the constituent blocks. The allocation
5137 * throttle needs to know the allocating parent zio so we must find
5138 * it here.
5140 if (pio->io_child_type == ZIO_CHILD_GANG) {
5142 * If our parent is a rewrite gang child then our grandparent
5143 * would have been the one that performed the allocation.
5145 if (pio->io_flags & ZIO_FLAG_IO_REWRITE)
5146 pio = zio_unique_parent(pio);
5147 flags |= METASLAB_GANG_CHILD;
5150 ASSERT(IO_IS_ALLOCATING(pio));
5151 ASSERT(ZIO_HAS_ALLOCATOR(pio));
5152 ASSERT3P(zio, !=, zio->io_logical);
5153 ASSERT(zio->io_logical != NULL);
5154 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REPAIR));
5155 ASSERT0(zio->io_flags & ZIO_FLAG_NOPWRITE);
5156 ASSERT(zio->io_metaslab_class != NULL);
5158 mutex_enter(&pio->io_lock);
5159 metaslab_group_alloc_decrement(zio->io_spa, vd->vdev_id, pio, flags,
5160 pio->io_allocator, B_TRUE);
5161 mutex_exit(&pio->io_lock);
5163 metaslab_class_throttle_unreserve(zio->io_metaslab_class, 1,
5164 pio->io_allocator, pio);
5167 * Call into the pipeline to see if there is more work that
5168 * needs to be done. If there is work to be done it will be
5169 * dispatched to another taskq thread.
5171 zio_allocate_dispatch(zio->io_spa, pio->io_allocator);
5174 static zio_t *
5175 zio_done(zio_t *zio)
5178 * Always attempt to keep stack usage minimal here since
5179 * we can be called recursively up to 19 levels deep.
5181 const uint64_t psize = zio->io_size;
5182 zio_t *pio, *pio_next;
5183 zio_link_t *zl = NULL;
5186 * If our children haven't all completed,
5187 * wait for them and then repeat this pipeline stage.
5189 if (zio_wait_for_children(zio, ZIO_CHILD_ALL_BITS, ZIO_WAIT_DONE)) {
5190 return (NULL);
5194 * If the allocation throttle is enabled, then update the accounting.
5195 * We only track child I/Os that are part of an allocating async
5196 * write. We must do this since the allocation is performed
5197 * by the logical I/O but the actual write is done by child I/Os.
5199 if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING &&
5200 zio->io_child_type == ZIO_CHILD_VDEV) {
5201 ASSERT(zio->io_metaslab_class != NULL);
5202 ASSERT(zio->io_metaslab_class->mc_alloc_throttle_enabled);
5203 zio_dva_throttle_done(zio);
5207 * If the allocation throttle is enabled, verify that
5208 * we have decremented the refcounts for every I/O that was throttled.
5210 if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
5211 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
5212 ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
5213 ASSERT(zio->io_bp != NULL);
5214 ASSERT(ZIO_HAS_ALLOCATOR(zio));
5216 metaslab_group_alloc_verify(zio->io_spa, zio->io_bp, zio,
5217 zio->io_allocator);
5218 VERIFY(zfs_refcount_not_held(&zio->io_metaslab_class->
5219 mc_allocator[zio->io_allocator].mca_alloc_slots, zio));
5223 for (int c = 0; c < ZIO_CHILD_TYPES; c++)
5224 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
5225 ASSERT(zio->io_children[c][w] == 0);
5227 if (zio->io_bp != NULL && !BP_IS_EMBEDDED(zio->io_bp)) {
5228 ASSERT(zio->io_bp->blk_pad[0] == 0);
5229 ASSERT(zio->io_bp->blk_pad[1] == 0);
5230 ASSERT(memcmp(zio->io_bp, &zio->io_bp_copy,
5231 sizeof (blkptr_t)) == 0 ||
5232 (zio->io_bp == zio_unique_parent(zio)->io_bp));
5233 if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(zio->io_bp) &&
5234 zio->io_bp_override == NULL &&
5235 !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) {
5236 ASSERT3U(zio->io_prop.zp_copies, <=,
5237 BP_GET_NDVAS(zio->io_bp));
5238 ASSERT(BP_COUNT_GANG(zio->io_bp) == 0 ||
5239 (BP_COUNT_GANG(zio->io_bp) ==
5240 BP_GET_NDVAS(zio->io_bp)));
5242 if (zio->io_flags & ZIO_FLAG_NOPWRITE)
5243 VERIFY(BP_EQUAL(zio->io_bp, &zio->io_bp_orig));
5247 * If there were child vdev/gang/ddt errors, they apply to us now.
5249 zio_inherit_child_errors(zio, ZIO_CHILD_VDEV);
5250 zio_inherit_child_errors(zio, ZIO_CHILD_GANG);
5251 zio_inherit_child_errors(zio, ZIO_CHILD_DDT);
5254 * If the I/O on the transformed data was successful, generate any
5255 * checksum reports now while we still have the transformed data.
5257 if (zio->io_error == 0) {
5258 while (zio->io_cksum_report != NULL) {
5259 zio_cksum_report_t *zcr = zio->io_cksum_report;
5260 uint64_t align = zcr->zcr_align;
5261 uint64_t asize = P2ROUNDUP(psize, align);
5262 abd_t *adata = zio->io_abd;
5264 if (adata != NULL && asize != psize) {
5265 adata = abd_alloc(asize, B_TRUE);
5266 abd_copy(adata, zio->io_abd, psize);
5267 abd_zero_off(adata, psize, asize - psize);
5270 zio->io_cksum_report = zcr->zcr_next;
5271 zcr->zcr_next = NULL;
5272 zcr->zcr_finish(zcr, adata);
5273 zfs_ereport_free_checksum(zcr);
5275 if (adata != NULL && asize != psize)
5276 abd_free(adata);
5280 zio_pop_transforms(zio); /* note: may set zio->io_error */
5282 vdev_stat_update(zio, psize);
5285 * If this I/O is attached to a particular vdev is slow, exceeding
5286 * 30 seconds to complete, post an error described the I/O delay.
5287 * We ignore these errors if the device is currently unavailable.
5289 if (zio->io_delay >= MSEC2NSEC(zio_slow_io_ms)) {
5290 if (zio->io_vd != NULL && !vdev_is_dead(zio->io_vd)) {
5292 * We want to only increment our slow IO counters if
5293 * the IO is valid (i.e. not if the drive is removed).
5295 * zfs_ereport_post() will also do these checks, but
5296 * it can also ratelimit and have other failures, so we
5297 * need to increment the slow_io counters independent
5298 * of it.
5300 if (zfs_ereport_is_valid(FM_EREPORT_ZFS_DELAY,
5301 zio->io_spa, zio->io_vd, zio)) {
5302 mutex_enter(&zio->io_vd->vdev_stat_lock);
5303 zio->io_vd->vdev_stat.vs_slow_ios++;
5304 mutex_exit(&zio->io_vd->vdev_stat_lock);
5306 (void) zfs_ereport_post(FM_EREPORT_ZFS_DELAY,
5307 zio->io_spa, zio->io_vd, &zio->io_bookmark,
5308 zio, 0);
5313 if (zio->io_error) {
5315 * If this I/O is attached to a particular vdev,
5316 * generate an error message describing the I/O failure
5317 * at the block level. We ignore these errors if the
5318 * device is currently unavailable.
5320 if (zio->io_error != ECKSUM && zio->io_vd != NULL &&
5321 !vdev_is_dead(zio->io_vd) &&
5322 !(zio->io_flags & ZIO_FLAG_DIO_CHKSUM_ERR)) {
5323 int ret = zfs_ereport_post(FM_EREPORT_ZFS_IO,
5324 zio->io_spa, zio->io_vd, &zio->io_bookmark, zio, 0);
5325 if (ret != EALREADY) {
5326 mutex_enter(&zio->io_vd->vdev_stat_lock);
5327 if (zio->io_type == ZIO_TYPE_READ)
5328 zio->io_vd->vdev_stat.vs_read_errors++;
5329 else if (zio->io_type == ZIO_TYPE_WRITE)
5330 zio->io_vd->vdev_stat.vs_write_errors++;
5331 mutex_exit(&zio->io_vd->vdev_stat_lock);
5335 if ((zio->io_error == EIO || !(zio->io_flags &
5336 (ZIO_FLAG_SPECULATIVE | ZIO_FLAG_DONT_PROPAGATE))) &&
5337 !(zio->io_flags & ZIO_FLAG_DIO_CHKSUM_ERR) &&
5338 zio == zio->io_logical) {
5340 * For logical I/O requests, tell the SPA to log the
5341 * error and generate a logical data ereport.
5343 spa_log_error(zio->io_spa, &zio->io_bookmark,
5344 BP_GET_LOGICAL_BIRTH(zio->io_bp));
5345 (void) zfs_ereport_post(FM_EREPORT_ZFS_DATA,
5346 zio->io_spa, NULL, &zio->io_bookmark, zio, 0);
5350 if (zio->io_error && zio == zio->io_logical) {
5352 * Determine whether zio should be reexecuted. This will
5353 * propagate all the way to the root via zio_notify_parent().
5355 ASSERT(zio->io_vd == NULL && zio->io_bp != NULL);
5356 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
5358 if (IO_IS_ALLOCATING(zio) &&
5359 !(zio->io_flags & ZIO_FLAG_CANFAIL) &&
5360 !(zio->io_flags & ZIO_FLAG_DIO_CHKSUM_ERR)) {
5361 if (zio->io_error != ENOSPC)
5362 zio->io_reexecute |= ZIO_REEXECUTE_NOW;
5363 else
5364 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
5367 if ((zio->io_type == ZIO_TYPE_READ ||
5368 zio->io_type == ZIO_TYPE_FREE) &&
5369 !(zio->io_flags & ZIO_FLAG_SCAN_THREAD) &&
5370 zio->io_error == ENXIO &&
5371 spa_load_state(zio->io_spa) == SPA_LOAD_NONE &&
5372 spa_get_failmode(zio->io_spa) != ZIO_FAILURE_MODE_CONTINUE)
5373 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
5375 if (!(zio->io_flags & ZIO_FLAG_CANFAIL) && !zio->io_reexecute)
5376 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
5379 * Here is a possibly good place to attempt to do
5380 * either combinatorial reconstruction or error correction
5381 * based on checksums. It also might be a good place
5382 * to send out preliminary ereports before we suspend
5383 * processing.
5388 * If there were logical child errors, they apply to us now.
5389 * We defer this until now to avoid conflating logical child
5390 * errors with errors that happened to the zio itself when
5391 * updating vdev stats and reporting FMA events above.
5393 zio_inherit_child_errors(zio, ZIO_CHILD_LOGICAL);
5395 if ((zio->io_error || zio->io_reexecute) &&
5396 IO_IS_ALLOCATING(zio) && zio->io_gang_leader == zio &&
5397 !(zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)))
5398 zio_dva_unallocate(zio, zio->io_gang_tree, zio->io_bp);
5400 zio_gang_tree_free(&zio->io_gang_tree);
5403 * Godfather I/Os should never suspend.
5405 if ((zio->io_flags & ZIO_FLAG_GODFATHER) &&
5406 (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND))
5407 zio->io_reexecute &= ~ZIO_REEXECUTE_SUSPEND;
5409 if (zio->io_reexecute) {
5411 * A Direct I/O operation that has a checksum verify error
5412 * should not attempt to reexecute. Instead, the error should
5413 * just be propagated back.
5415 ASSERT(!(zio->io_flags & ZIO_FLAG_DIO_CHKSUM_ERR));
5418 * This is a logical I/O that wants to reexecute.
5420 * Reexecute is top-down. When an i/o fails, if it's not
5421 * the root, it simply notifies its parent and sticks around.
5422 * The parent, seeing that it still has children in zio_done(),
5423 * does the same. This percolates all the way up to the root.
5424 * The root i/o will reexecute or suspend the entire tree.
5426 * This approach ensures that zio_reexecute() honors
5427 * all the original i/o dependency relationships, e.g.
5428 * parents not executing until children are ready.
5430 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
5432 zio->io_gang_leader = NULL;
5434 mutex_enter(&zio->io_lock);
5435 zio->io_state[ZIO_WAIT_DONE] = 1;
5436 mutex_exit(&zio->io_lock);
5439 * "The Godfather" I/O monitors its children but is
5440 * not a true parent to them. It will track them through
5441 * the pipeline but severs its ties whenever they get into
5442 * trouble (e.g. suspended). This allows "The Godfather"
5443 * I/O to return status without blocking.
5445 zl = NULL;
5446 for (pio = zio_walk_parents(zio, &zl); pio != NULL;
5447 pio = pio_next) {
5448 zio_link_t *remove_zl = zl;
5449 pio_next = zio_walk_parents(zio, &zl);
5451 if ((pio->io_flags & ZIO_FLAG_GODFATHER) &&
5452 (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND)) {
5453 zio_remove_child(pio, zio, remove_zl);
5455 * This is a rare code path, so we don't
5456 * bother with "next_to_execute".
5458 zio_notify_parent(pio, zio, ZIO_WAIT_DONE,
5459 NULL);
5463 if ((pio = zio_unique_parent(zio)) != NULL) {
5465 * We're not a root i/o, so there's nothing to do
5466 * but notify our parent. Don't propagate errors
5467 * upward since we haven't permanently failed yet.
5469 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
5470 zio->io_flags |= ZIO_FLAG_DONT_PROPAGATE;
5472 * This is a rare code path, so we don't bother with
5473 * "next_to_execute".
5475 zio_notify_parent(pio, zio, ZIO_WAIT_DONE, NULL);
5476 } else if (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND) {
5478 * We'd fail again if we reexecuted now, so suspend
5479 * until conditions improve (e.g. device comes online).
5481 zio_suspend(zio->io_spa, zio, ZIO_SUSPEND_IOERR);
5482 } else {
5484 * Reexecution is potentially a huge amount of work.
5485 * Hand it off to the otherwise-unused claim taskq.
5487 spa_taskq_dispatch(zio->io_spa,
5488 ZIO_TYPE_CLAIM, ZIO_TASKQ_ISSUE,
5489 zio_reexecute, zio, B_FALSE);
5491 return (NULL);
5494 ASSERT(list_is_empty(&zio->io_child_list));
5495 ASSERT(zio->io_reexecute == 0);
5496 ASSERT(zio->io_error == 0 || (zio->io_flags & ZIO_FLAG_CANFAIL));
5499 * Report any checksum errors, since the I/O is complete.
5501 while (zio->io_cksum_report != NULL) {
5502 zio_cksum_report_t *zcr = zio->io_cksum_report;
5503 zio->io_cksum_report = zcr->zcr_next;
5504 zcr->zcr_next = NULL;
5505 zcr->zcr_finish(zcr, NULL);
5506 zfs_ereport_free_checksum(zcr);
5510 * It is the responsibility of the done callback to ensure that this
5511 * particular zio is no longer discoverable for adoption, and as
5512 * such, cannot acquire any new parents.
5514 if (zio->io_done)
5515 zio->io_done(zio);
5517 mutex_enter(&zio->io_lock);
5518 zio->io_state[ZIO_WAIT_DONE] = 1;
5519 mutex_exit(&zio->io_lock);
5522 * We are done executing this zio. We may want to execute a parent
5523 * next. See the comment in zio_notify_parent().
5525 zio_t *next_to_execute = NULL;
5526 zl = NULL;
5527 for (pio = zio_walk_parents(zio, &zl); pio != NULL; pio = pio_next) {
5528 zio_link_t *remove_zl = zl;
5529 pio_next = zio_walk_parents(zio, &zl);
5530 zio_remove_child(pio, zio, remove_zl);
5531 zio_notify_parent(pio, zio, ZIO_WAIT_DONE, &next_to_execute);
5534 if (zio->io_waiter != NULL) {
5535 mutex_enter(&zio->io_lock);
5536 zio->io_executor = NULL;
5537 cv_broadcast(&zio->io_cv);
5538 mutex_exit(&zio->io_lock);
5539 } else {
5540 zio_destroy(zio);
5543 return (next_to_execute);
5547 * ==========================================================================
5548 * I/O pipeline definition
5549 * ==========================================================================
5551 static zio_pipe_stage_t *zio_pipeline[] = {
5552 NULL,
5553 zio_read_bp_init,
5554 zio_write_bp_init,
5555 zio_free_bp_init,
5556 zio_issue_async,
5557 zio_write_compress,
5558 zio_encrypt,
5559 zio_checksum_generate,
5560 zio_nop_write,
5561 zio_brt_free,
5562 zio_ddt_read_start,
5563 zio_ddt_read_done,
5564 zio_ddt_write,
5565 zio_ddt_free,
5566 zio_gang_assemble,
5567 zio_gang_issue,
5568 zio_dva_throttle,
5569 zio_dva_allocate,
5570 zio_dva_free,
5571 zio_dva_claim,
5572 zio_ready,
5573 zio_vdev_io_start,
5574 zio_vdev_io_done,
5575 zio_vdev_io_assess,
5576 zio_checksum_verify,
5577 zio_dio_checksum_verify,
5578 zio_done
5585 * Compare two zbookmark_phys_t's to see which we would reach first in a
5586 * pre-order traversal of the object tree.
5588 * This is simple in every case aside from the meta-dnode object. For all other
5589 * objects, we traverse them in order (object 1 before object 2, and so on).
5590 * However, all of these objects are traversed while traversing object 0, since
5591 * the data it points to is the list of objects. Thus, we need to convert to a
5592 * canonical representation so we can compare meta-dnode bookmarks to
5593 * non-meta-dnode bookmarks.
5595 * We do this by calculating "equivalents" for each field of the zbookmark.
5596 * zbookmarks outside of the meta-dnode use their own object and level, and
5597 * calculate the level 0 equivalent (the first L0 blkid that is contained in the
5598 * blocks this bookmark refers to) by multiplying their blkid by their span
5599 * (the number of L0 blocks contained within one block at their level).
5600 * zbookmarks inside the meta-dnode calculate their object equivalent
5601 * (which is L0equiv * dnodes per data block), use 0 for their L0equiv, and use
5602 * level + 1<<31 (any value larger than a level could ever be) for their level.
5603 * This causes them to always compare before a bookmark in their object
5604 * equivalent, compare appropriately to bookmarks in other objects, and to
5605 * compare appropriately to other bookmarks in the meta-dnode.
5608 zbookmark_compare(uint16_t dbss1, uint8_t ibs1, uint16_t dbss2, uint8_t ibs2,
5609 const zbookmark_phys_t *zb1, const zbookmark_phys_t *zb2)
5612 * These variables represent the "equivalent" values for the zbookmark,
5613 * after converting zbookmarks inside the meta dnode to their
5614 * normal-object equivalents.
5616 uint64_t zb1obj, zb2obj;
5617 uint64_t zb1L0, zb2L0;
5618 uint64_t zb1level, zb2level;
5620 if (zb1->zb_object == zb2->zb_object &&
5621 zb1->zb_level == zb2->zb_level &&
5622 zb1->zb_blkid == zb2->zb_blkid)
5623 return (0);
5625 IMPLY(zb1->zb_level > 0, ibs1 >= SPA_MINBLOCKSHIFT);
5626 IMPLY(zb2->zb_level > 0, ibs2 >= SPA_MINBLOCKSHIFT);
5629 * BP_SPANB calculates the span in blocks.
5631 zb1L0 = (zb1->zb_blkid) * BP_SPANB(ibs1, zb1->zb_level);
5632 zb2L0 = (zb2->zb_blkid) * BP_SPANB(ibs2, zb2->zb_level);
5634 if (zb1->zb_object == DMU_META_DNODE_OBJECT) {
5635 zb1obj = zb1L0 * (dbss1 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT));
5636 zb1L0 = 0;
5637 zb1level = zb1->zb_level + COMPARE_META_LEVEL;
5638 } else {
5639 zb1obj = zb1->zb_object;
5640 zb1level = zb1->zb_level;
5643 if (zb2->zb_object == DMU_META_DNODE_OBJECT) {
5644 zb2obj = zb2L0 * (dbss2 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT));
5645 zb2L0 = 0;
5646 zb2level = zb2->zb_level + COMPARE_META_LEVEL;
5647 } else {
5648 zb2obj = zb2->zb_object;
5649 zb2level = zb2->zb_level;
5652 /* Now that we have a canonical representation, do the comparison. */
5653 if (zb1obj != zb2obj)
5654 return (zb1obj < zb2obj ? -1 : 1);
5655 else if (zb1L0 != zb2L0)
5656 return (zb1L0 < zb2L0 ? -1 : 1);
5657 else if (zb1level != zb2level)
5658 return (zb1level > zb2level ? -1 : 1);
5660 * This can (theoretically) happen if the bookmarks have the same object
5661 * and level, but different blkids, if the block sizes are not the same.
5662 * There is presently no way to change the indirect block sizes
5664 return (0);
5668 * This function checks the following: given that last_block is the place that
5669 * our traversal stopped last time, does that guarantee that we've visited
5670 * every node under subtree_root? Therefore, we can't just use the raw output
5671 * of zbookmark_compare. We have to pass in a modified version of
5672 * subtree_root; by incrementing the block id, and then checking whether
5673 * last_block is before or equal to that, we can tell whether or not having
5674 * visited last_block implies that all of subtree_root's children have been
5675 * visited.
5677 boolean_t
5678 zbookmark_subtree_completed(const dnode_phys_t *dnp,
5679 const zbookmark_phys_t *subtree_root, const zbookmark_phys_t *last_block)
5681 zbookmark_phys_t mod_zb = *subtree_root;
5682 mod_zb.zb_blkid++;
5683 ASSERT0(last_block->zb_level);
5685 /* The objset_phys_t isn't before anything. */
5686 if (dnp == NULL)
5687 return (B_FALSE);
5690 * We pass in 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT) for the
5691 * data block size in sectors, because that variable is only used if
5692 * the bookmark refers to a block in the meta-dnode. Since we don't
5693 * know without examining it what object it refers to, and there's no
5694 * harm in passing in this value in other cases, we always pass it in.
5696 * We pass in 0 for the indirect block size shift because zb2 must be
5697 * level 0. The indirect block size is only used to calculate the span
5698 * of the bookmark, but since the bookmark must be level 0, the span is
5699 * always 1, so the math works out.
5701 * If you make changes to how the zbookmark_compare code works, be sure
5702 * to make sure that this code still works afterwards.
5704 return (zbookmark_compare(dnp->dn_datablkszsec, dnp->dn_indblkshift,
5705 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT), 0, &mod_zb,
5706 last_block) <= 0);
5710 * This function is similar to zbookmark_subtree_completed(), but returns true
5711 * if subtree_root is equal or ahead of last_block, i.e. still to be done.
5713 boolean_t
5714 zbookmark_subtree_tbd(const dnode_phys_t *dnp,
5715 const zbookmark_phys_t *subtree_root, const zbookmark_phys_t *last_block)
5717 ASSERT0(last_block->zb_level);
5718 if (dnp == NULL)
5719 return (B_FALSE);
5720 return (zbookmark_compare(dnp->dn_datablkszsec, dnp->dn_indblkshift,
5721 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT), 0, subtree_root,
5722 last_block) >= 0);
5725 EXPORT_SYMBOL(zio_type_name);
5726 EXPORT_SYMBOL(zio_buf_alloc);
5727 EXPORT_SYMBOL(zio_data_buf_alloc);
5728 EXPORT_SYMBOL(zio_buf_free);
5729 EXPORT_SYMBOL(zio_data_buf_free);
5731 ZFS_MODULE_PARAM(zfs_zio, zio_, slow_io_ms, INT, ZMOD_RW,
5732 "Max I/O completion time (milliseconds) before marking it as slow");
5734 ZFS_MODULE_PARAM(zfs_zio, zio_, requeue_io_start_cut_in_line, INT, ZMOD_RW,
5735 "Prioritize requeued I/O");
5737 ZFS_MODULE_PARAM(zfs, zfs_, sync_pass_deferred_free, UINT, ZMOD_RW,
5738 "Defer frees starting in this pass");
5740 ZFS_MODULE_PARAM(zfs, zfs_, sync_pass_dont_compress, UINT, ZMOD_RW,
5741 "Don't compress starting in this pass");
5743 ZFS_MODULE_PARAM(zfs, zfs_, sync_pass_rewrite, UINT, ZMOD_RW,
5744 "Rewrite new bps starting in this pass");
5746 ZFS_MODULE_PARAM(zfs_zio, zio_, dva_throttle_enabled, INT, ZMOD_RW,
5747 "Throttle block allocations in the ZIO pipeline");
5749 ZFS_MODULE_PARAM(zfs_zio, zio_, deadman_log_all, INT, ZMOD_RW,
5750 "Log all slow ZIOs, not just those with vdevs");