Optimize RAIDZ expansion
[zfs.git] / module / zfs / zio.c
blobf4d7e57542a18dc83fb84d6e5e094d8d4146eda8
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 (IS_P2ALIGNED(size, PAGESIZE))
191 align = PAGESIZE;
192 else
193 align = 1 << (highbit64(size ^ (size - 1)) - 1);
195 cflags = (zio_exclude_metadata || size > zio_buf_debug_limit) ?
196 KMC_NODEBUG : 0;
197 data_cflags = KMC_NODEBUG;
198 if (abd_size_alloc_linear(size)) {
199 cflags |= KMC_RECLAIMABLE;
200 data_cflags |= KMC_RECLAIMABLE;
202 if (cflags == data_cflags) {
204 * Resulting kmem caches would be identical.
205 * Save memory by creating only one.
207 (void) snprintf(name, sizeof (name),
208 "zio_buf_comb_%lu", (ulong_t)size);
209 zio_buf_cache[c] = kmem_cache_create(name, size, align,
210 NULL, NULL, NULL, NULL, NULL, cflags);
211 zio_data_buf_cache[c] = zio_buf_cache[c];
212 continue;
214 (void) snprintf(name, sizeof (name), "zio_buf_%lu",
215 (ulong_t)size);
216 zio_buf_cache[c] = kmem_cache_create(name, size, align,
217 NULL, NULL, NULL, NULL, NULL, cflags);
219 (void) snprintf(name, sizeof (name), "zio_data_buf_%lu",
220 (ulong_t)size);
221 zio_data_buf_cache[c] = kmem_cache_create(name, size, align,
222 NULL, NULL, NULL, NULL, NULL, data_cflags);
225 while (--c != 0) {
226 ASSERT(zio_buf_cache[c] != NULL);
227 if (zio_buf_cache[c - 1] == NULL)
228 zio_buf_cache[c - 1] = zio_buf_cache[c];
230 ASSERT(zio_data_buf_cache[c] != NULL);
231 if (zio_data_buf_cache[c - 1] == NULL)
232 zio_data_buf_cache[c - 1] = zio_data_buf_cache[c];
235 zio_inject_init();
237 lz4_init();
240 void
241 zio_fini(void)
243 size_t n = SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT;
245 #if defined(ZFS_DEBUG) && !defined(_KERNEL)
246 for (size_t i = 0; i < n; i++) {
247 if (zio_buf_cache_allocs[i] != zio_buf_cache_frees[i])
248 (void) printf("zio_fini: [%d] %llu != %llu\n",
249 (int)((i + 1) << SPA_MINBLOCKSHIFT),
250 (long long unsigned)zio_buf_cache_allocs[i],
251 (long long unsigned)zio_buf_cache_frees[i]);
253 #endif
256 * The same kmem cache can show up multiple times in both zio_buf_cache
257 * and zio_data_buf_cache. Do a wasteful but trivially correct scan to
258 * sort it out.
260 for (size_t i = 0; i < n; i++) {
261 kmem_cache_t *cache = zio_buf_cache[i];
262 if (cache == NULL)
263 continue;
264 for (size_t j = i; j < n; j++) {
265 if (cache == zio_buf_cache[j])
266 zio_buf_cache[j] = NULL;
267 if (cache == zio_data_buf_cache[j])
268 zio_data_buf_cache[j] = NULL;
270 kmem_cache_destroy(cache);
273 for (size_t i = 0; i < n; i++) {
274 kmem_cache_t *cache = zio_data_buf_cache[i];
275 if (cache == NULL)
276 continue;
277 for (size_t j = i; j < n; j++) {
278 if (cache == zio_data_buf_cache[j])
279 zio_data_buf_cache[j] = NULL;
281 kmem_cache_destroy(cache);
284 for (size_t i = 0; i < n; i++) {
285 VERIFY3P(zio_buf_cache[i], ==, NULL);
286 VERIFY3P(zio_data_buf_cache[i], ==, NULL);
289 kmem_cache_destroy(zio_link_cache);
290 kmem_cache_destroy(zio_cache);
292 zio_inject_fini();
294 lz4_fini();
298 * ==========================================================================
299 * Allocate and free I/O buffers
300 * ==========================================================================
303 #if defined(ZFS_DEBUG) && defined(_KERNEL)
304 #define ZFS_ZIO_BUF_CANARY 1
305 #endif
307 #ifdef ZFS_ZIO_BUF_CANARY
308 static const ulong_t zio_buf_canary = (ulong_t)0xdeadc0dedead210b;
311 * Use empty space after the buffer to detect overflows.
313 * Since zio_init() creates kmem caches only for certain set of buffer sizes,
314 * allocations of different sizes may have some unused space after the data.
315 * Filling part of that space with a known pattern on allocation and checking
316 * it on free should allow us to detect some buffer overflows.
318 static void
319 zio_buf_put_canary(ulong_t *p, size_t size, kmem_cache_t **cache, size_t c)
321 size_t off = P2ROUNDUP(size, sizeof (ulong_t));
322 ulong_t *canary = p + off / sizeof (ulong_t);
323 size_t asize = (c + 1) << SPA_MINBLOCKSHIFT;
324 if (c + 1 < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT &&
325 cache[c] == cache[c + 1])
326 asize = (c + 2) << SPA_MINBLOCKSHIFT;
327 for (; off < asize; canary++, off += sizeof (ulong_t))
328 *canary = zio_buf_canary;
331 static void
332 zio_buf_check_canary(ulong_t *p, size_t size, kmem_cache_t **cache, size_t c)
334 size_t off = P2ROUNDUP(size, sizeof (ulong_t));
335 ulong_t *canary = p + off / sizeof (ulong_t);
336 size_t asize = (c + 1) << SPA_MINBLOCKSHIFT;
337 if (c + 1 < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT &&
338 cache[c] == cache[c + 1])
339 asize = (c + 2) << SPA_MINBLOCKSHIFT;
340 for (; off < asize; canary++, off += sizeof (ulong_t)) {
341 if (unlikely(*canary != zio_buf_canary)) {
342 PANIC("ZIO buffer overflow %p (%zu) + %zu %#lx != %#lx",
343 p, size, (canary - p) * sizeof (ulong_t),
344 *canary, zio_buf_canary);
348 #endif
351 * Use zio_buf_alloc to allocate ZFS metadata. This data will appear in a
352 * crashdump if the kernel panics, so use it judiciously. Obviously, it's
353 * useful to inspect ZFS metadata, but if possible, we should avoid keeping
354 * excess / transient data in-core during a crashdump.
356 void *
357 zio_buf_alloc(size_t size)
359 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
361 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
362 #if defined(ZFS_DEBUG) && !defined(_KERNEL)
363 atomic_add_64(&zio_buf_cache_allocs[c], 1);
364 #endif
366 void *p = kmem_cache_alloc(zio_buf_cache[c], KM_PUSHPAGE);
367 #ifdef ZFS_ZIO_BUF_CANARY
368 zio_buf_put_canary(p, size, zio_buf_cache, c);
369 #endif
370 return (p);
374 * Use zio_data_buf_alloc to allocate data. The data will not appear in a
375 * crashdump if the kernel panics. This exists so that we will limit the amount
376 * of ZFS data that shows up in a kernel crashdump. (Thus reducing the amount
377 * of kernel heap dumped to disk when the kernel panics)
379 void *
380 zio_data_buf_alloc(size_t size)
382 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
384 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
386 void *p = kmem_cache_alloc(zio_data_buf_cache[c], KM_PUSHPAGE);
387 #ifdef ZFS_ZIO_BUF_CANARY
388 zio_buf_put_canary(p, size, zio_data_buf_cache, c);
389 #endif
390 return (p);
393 void
394 zio_buf_free(void *buf, size_t size)
396 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
398 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
399 #if defined(ZFS_DEBUG) && !defined(_KERNEL)
400 atomic_add_64(&zio_buf_cache_frees[c], 1);
401 #endif
403 #ifdef ZFS_ZIO_BUF_CANARY
404 zio_buf_check_canary(buf, size, zio_buf_cache, c);
405 #endif
406 kmem_cache_free(zio_buf_cache[c], buf);
409 void
410 zio_data_buf_free(void *buf, size_t size)
412 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
414 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
416 #ifdef ZFS_ZIO_BUF_CANARY
417 zio_buf_check_canary(buf, size, zio_data_buf_cache, c);
418 #endif
419 kmem_cache_free(zio_data_buf_cache[c], buf);
422 static void
423 zio_abd_free(void *abd, size_t size)
425 (void) size;
426 abd_free((abd_t *)abd);
430 * ==========================================================================
431 * Push and pop I/O transform buffers
432 * ==========================================================================
434 void
435 zio_push_transform(zio_t *zio, abd_t *data, uint64_t size, uint64_t bufsize,
436 zio_transform_func_t *transform)
438 zio_transform_t *zt = kmem_alloc(sizeof (zio_transform_t), KM_SLEEP);
440 zt->zt_orig_abd = zio->io_abd;
441 zt->zt_orig_size = zio->io_size;
442 zt->zt_bufsize = bufsize;
443 zt->zt_transform = transform;
445 zt->zt_next = zio->io_transform_stack;
446 zio->io_transform_stack = zt;
448 zio->io_abd = data;
449 zio->io_size = size;
452 void
453 zio_pop_transforms(zio_t *zio)
455 zio_transform_t *zt;
457 while ((zt = zio->io_transform_stack) != NULL) {
458 if (zt->zt_transform != NULL)
459 zt->zt_transform(zio,
460 zt->zt_orig_abd, zt->zt_orig_size);
462 if (zt->zt_bufsize != 0)
463 abd_free(zio->io_abd);
465 zio->io_abd = zt->zt_orig_abd;
466 zio->io_size = zt->zt_orig_size;
467 zio->io_transform_stack = zt->zt_next;
469 kmem_free(zt, sizeof (zio_transform_t));
474 * ==========================================================================
475 * I/O transform callbacks for subblocks, decompression, and decryption
476 * ==========================================================================
478 static void
479 zio_subblock(zio_t *zio, abd_t *data, uint64_t size)
481 ASSERT(zio->io_size > size);
483 if (zio->io_type == ZIO_TYPE_READ)
484 abd_copy(data, zio->io_abd, size);
487 static void
488 zio_decompress(zio_t *zio, abd_t *data, uint64_t size)
490 if (zio->io_error == 0) {
491 int ret = zio_decompress_data(BP_GET_COMPRESS(zio->io_bp),
492 zio->io_abd, data, zio->io_size, size,
493 &zio->io_prop.zp_complevel);
495 if (zio_injection_enabled && ret == 0)
496 ret = zio_handle_fault_injection(zio, EINVAL);
498 if (ret != 0)
499 zio->io_error = SET_ERROR(EIO);
503 static void
504 zio_decrypt(zio_t *zio, abd_t *data, uint64_t size)
506 int ret;
507 void *tmp;
508 blkptr_t *bp = zio->io_bp;
509 spa_t *spa = zio->io_spa;
510 uint64_t dsobj = zio->io_bookmark.zb_objset;
511 uint64_t lsize = BP_GET_LSIZE(bp);
512 dmu_object_type_t ot = BP_GET_TYPE(bp);
513 uint8_t salt[ZIO_DATA_SALT_LEN];
514 uint8_t iv[ZIO_DATA_IV_LEN];
515 uint8_t mac[ZIO_DATA_MAC_LEN];
516 boolean_t no_crypt = B_FALSE;
518 ASSERT(BP_USES_CRYPT(bp));
519 ASSERT3U(size, !=, 0);
521 if (zio->io_error != 0)
522 return;
525 * Verify the cksum of MACs stored in an indirect bp. It will always
526 * be possible to verify this since it does not require an encryption
527 * key.
529 if (BP_HAS_INDIRECT_MAC_CKSUM(bp)) {
530 zio_crypt_decode_mac_bp(bp, mac);
532 if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF) {
534 * We haven't decompressed the data yet, but
535 * zio_crypt_do_indirect_mac_checksum() requires
536 * decompressed data to be able to parse out the MACs
537 * from the indirect block. We decompress it now and
538 * throw away the result after we are finished.
540 abd_t *abd = abd_alloc_linear(lsize, B_TRUE);
541 ret = zio_decompress_data(BP_GET_COMPRESS(bp),
542 zio->io_abd, abd, zio->io_size, lsize,
543 &zio->io_prop.zp_complevel);
544 if (ret != 0) {
545 abd_free(abd);
546 ret = SET_ERROR(EIO);
547 goto error;
549 ret = zio_crypt_do_indirect_mac_checksum_abd(B_FALSE,
550 abd, lsize, BP_SHOULD_BYTESWAP(bp), mac);
551 abd_free(abd);
552 } else {
553 ret = zio_crypt_do_indirect_mac_checksum_abd(B_FALSE,
554 zio->io_abd, size, BP_SHOULD_BYTESWAP(bp), mac);
556 abd_copy(data, zio->io_abd, size);
558 if (zio_injection_enabled && ot != DMU_OT_DNODE && ret == 0) {
559 ret = zio_handle_decrypt_injection(spa,
560 &zio->io_bookmark, ot, ECKSUM);
562 if (ret != 0)
563 goto error;
565 return;
569 * If this is an authenticated block, just check the MAC. It would be
570 * nice to separate this out into its own flag, but when this was done,
571 * we had run out of bits in what is now zio_flag_t. Future cleanup
572 * could make this a flag bit.
574 if (BP_IS_AUTHENTICATED(bp)) {
575 if (ot == DMU_OT_OBJSET) {
576 ret = spa_do_crypt_objset_mac_abd(B_FALSE, spa,
577 dsobj, zio->io_abd, size, BP_SHOULD_BYTESWAP(bp));
578 } else {
579 zio_crypt_decode_mac_bp(bp, mac);
580 ret = spa_do_crypt_mac_abd(B_FALSE, spa, dsobj,
581 zio->io_abd, size, mac);
582 if (zio_injection_enabled && ret == 0) {
583 ret = zio_handle_decrypt_injection(spa,
584 &zio->io_bookmark, ot, ECKSUM);
587 abd_copy(data, zio->io_abd, size);
589 if (ret != 0)
590 goto error;
592 return;
595 zio_crypt_decode_params_bp(bp, salt, iv);
597 if (ot == DMU_OT_INTENT_LOG) {
598 tmp = abd_borrow_buf_copy(zio->io_abd, sizeof (zil_chain_t));
599 zio_crypt_decode_mac_zil(tmp, mac);
600 abd_return_buf(zio->io_abd, tmp, sizeof (zil_chain_t));
601 } else {
602 zio_crypt_decode_mac_bp(bp, mac);
605 ret = spa_do_crypt_abd(B_FALSE, spa, &zio->io_bookmark, BP_GET_TYPE(bp),
606 BP_GET_DEDUP(bp), BP_SHOULD_BYTESWAP(bp), salt, iv, mac, size, data,
607 zio->io_abd, &no_crypt);
608 if (no_crypt)
609 abd_copy(data, zio->io_abd, size);
611 if (ret != 0)
612 goto error;
614 return;
616 error:
617 /* assert that the key was found unless this was speculative */
618 ASSERT(ret != EACCES || (zio->io_flags & ZIO_FLAG_SPECULATIVE));
621 * If there was a decryption / authentication error return EIO as
622 * the io_error. If this was not a speculative zio, create an ereport.
624 if (ret == ECKSUM) {
625 zio->io_error = SET_ERROR(EIO);
626 if ((zio->io_flags & ZIO_FLAG_SPECULATIVE) == 0) {
627 spa_log_error(spa, &zio->io_bookmark,
628 BP_GET_LOGICAL_BIRTH(zio->io_bp));
629 (void) zfs_ereport_post(FM_EREPORT_ZFS_AUTHENTICATION,
630 spa, NULL, &zio->io_bookmark, zio, 0);
632 } else {
633 zio->io_error = ret;
638 * ==========================================================================
639 * I/O parent/child relationships and pipeline interlocks
640 * ==========================================================================
642 zio_t *
643 zio_walk_parents(zio_t *cio, zio_link_t **zl)
645 list_t *pl = &cio->io_parent_list;
647 *zl = (*zl == NULL) ? list_head(pl) : list_next(pl, *zl);
648 if (*zl == NULL)
649 return (NULL);
651 ASSERT((*zl)->zl_child == cio);
652 return ((*zl)->zl_parent);
655 zio_t *
656 zio_walk_children(zio_t *pio, zio_link_t **zl)
658 list_t *cl = &pio->io_child_list;
660 ASSERT(MUTEX_HELD(&pio->io_lock));
662 *zl = (*zl == NULL) ? list_head(cl) : list_next(cl, *zl);
663 if (*zl == NULL)
664 return (NULL);
666 ASSERT((*zl)->zl_parent == pio);
667 return ((*zl)->zl_child);
670 zio_t *
671 zio_unique_parent(zio_t *cio)
673 zio_link_t *zl = NULL;
674 zio_t *pio = zio_walk_parents(cio, &zl);
676 VERIFY3P(zio_walk_parents(cio, &zl), ==, NULL);
677 return (pio);
680 void
681 zio_add_child(zio_t *pio, zio_t *cio)
684 * Logical I/Os can have logical, gang, or vdev children.
685 * Gang I/Os can have gang or vdev children.
686 * Vdev I/Os can only have vdev children.
687 * The following ASSERT captures all of these constraints.
689 ASSERT3S(cio->io_child_type, <=, pio->io_child_type);
691 /* Parent should not have READY stage if child doesn't have it. */
692 IMPLY((cio->io_pipeline & ZIO_STAGE_READY) == 0 &&
693 (cio->io_child_type != ZIO_CHILD_VDEV),
694 (pio->io_pipeline & ZIO_STAGE_READY) == 0);
696 zio_link_t *zl = kmem_cache_alloc(zio_link_cache, KM_SLEEP);
697 zl->zl_parent = pio;
698 zl->zl_child = cio;
700 mutex_enter(&pio->io_lock);
701 mutex_enter(&cio->io_lock);
703 ASSERT(pio->io_state[ZIO_WAIT_DONE] == 0);
705 uint64_t *countp = pio->io_children[cio->io_child_type];
706 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
707 countp[w] += !cio->io_state[w];
709 list_insert_head(&pio->io_child_list, zl);
710 list_insert_head(&cio->io_parent_list, zl);
712 mutex_exit(&cio->io_lock);
713 mutex_exit(&pio->io_lock);
716 void
717 zio_add_child_first(zio_t *pio, zio_t *cio)
720 * Logical I/Os can have logical, gang, or vdev children.
721 * Gang I/Os can have gang or vdev children.
722 * Vdev I/Os can only have vdev children.
723 * The following ASSERT captures all of these constraints.
725 ASSERT3S(cio->io_child_type, <=, pio->io_child_type);
727 /* Parent should not have READY stage if child doesn't have it. */
728 IMPLY((cio->io_pipeline & ZIO_STAGE_READY) == 0 &&
729 (cio->io_child_type != ZIO_CHILD_VDEV),
730 (pio->io_pipeline & ZIO_STAGE_READY) == 0);
732 zio_link_t *zl = kmem_cache_alloc(zio_link_cache, KM_SLEEP);
733 zl->zl_parent = pio;
734 zl->zl_child = cio;
736 ASSERT(list_is_empty(&cio->io_parent_list));
737 list_insert_head(&cio->io_parent_list, zl);
739 mutex_enter(&pio->io_lock);
741 ASSERT(pio->io_state[ZIO_WAIT_DONE] == 0);
743 uint64_t *countp = pio->io_children[cio->io_child_type];
744 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
745 countp[w] += !cio->io_state[w];
747 list_insert_head(&pio->io_child_list, zl);
749 mutex_exit(&pio->io_lock);
752 static void
753 zio_remove_child(zio_t *pio, zio_t *cio, zio_link_t *zl)
755 ASSERT(zl->zl_parent == pio);
756 ASSERT(zl->zl_child == cio);
758 mutex_enter(&pio->io_lock);
759 mutex_enter(&cio->io_lock);
761 list_remove(&pio->io_child_list, zl);
762 list_remove(&cio->io_parent_list, zl);
764 mutex_exit(&cio->io_lock);
765 mutex_exit(&pio->io_lock);
766 kmem_cache_free(zio_link_cache, zl);
769 static boolean_t
770 zio_wait_for_children(zio_t *zio, uint8_t childbits, enum zio_wait_type wait)
772 boolean_t waiting = B_FALSE;
774 mutex_enter(&zio->io_lock);
775 ASSERT(zio->io_stall == NULL);
776 for (int c = 0; c < ZIO_CHILD_TYPES; c++) {
777 if (!(ZIO_CHILD_BIT_IS_SET(childbits, c)))
778 continue;
780 uint64_t *countp = &zio->io_children[c][wait];
781 if (*countp != 0) {
782 zio->io_stage >>= 1;
783 ASSERT3U(zio->io_stage, !=, ZIO_STAGE_OPEN);
784 zio->io_stall = countp;
785 waiting = B_TRUE;
786 break;
789 mutex_exit(&zio->io_lock);
790 return (waiting);
793 __attribute__((always_inline))
794 static inline void
795 zio_notify_parent(zio_t *pio, zio_t *zio, enum zio_wait_type wait,
796 zio_t **next_to_executep)
798 uint64_t *countp = &pio->io_children[zio->io_child_type][wait];
799 int *errorp = &pio->io_child_error[zio->io_child_type];
801 mutex_enter(&pio->io_lock);
802 if (zio->io_error && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
803 *errorp = zio_worst_error(*errorp, zio->io_error);
804 pio->io_reexecute |= zio->io_reexecute;
805 ASSERT3U(*countp, >, 0);
808 * Propogate the Direct I/O checksum verify failure to the parent.
810 if (zio->io_flags & ZIO_FLAG_DIO_CHKSUM_ERR)
811 pio->io_flags |= ZIO_FLAG_DIO_CHKSUM_ERR;
813 (*countp)--;
815 if (*countp == 0 && pio->io_stall == countp) {
816 zio_taskq_type_t type =
817 pio->io_stage < ZIO_STAGE_VDEV_IO_START ? ZIO_TASKQ_ISSUE :
818 ZIO_TASKQ_INTERRUPT;
819 pio->io_stall = NULL;
820 mutex_exit(&pio->io_lock);
823 * If we can tell the caller to execute this parent next, do
824 * so. We do this if the parent's zio type matches the child's
825 * type, or if it's a zio_null() with no done callback, and so
826 * has no actual work to do. Otherwise dispatch the parent zio
827 * in its own taskq.
829 * Having the caller execute the parent when possible reduces
830 * locking on the zio taskq's, reduces context switch
831 * overhead, and has no recursion penalty. Note that one
832 * read from disk typically causes at least 3 zio's: a
833 * zio_null(), the logical zio_read(), and then a physical
834 * zio. When the physical ZIO completes, we are able to call
835 * zio_done() on all 3 of these zio's from one invocation of
836 * zio_execute() by returning the parent back to
837 * zio_execute(). Since the parent isn't executed until this
838 * thread returns back to zio_execute(), the caller should do
839 * so promptly.
841 * In other cases, dispatching the parent prevents
842 * overflowing the stack when we have deeply nested
843 * parent-child relationships, as we do with the "mega zio"
844 * of writes for spa_sync(), and the chain of ZIL blocks.
846 if (next_to_executep != NULL && *next_to_executep == NULL &&
847 (pio->io_type == zio->io_type ||
848 (pio->io_type == ZIO_TYPE_NULL && !pio->io_done))) {
849 *next_to_executep = pio;
850 } else {
851 zio_taskq_dispatch(pio, type, B_FALSE);
853 } else {
854 mutex_exit(&pio->io_lock);
858 static void
859 zio_inherit_child_errors(zio_t *zio, enum zio_child c)
861 if (zio->io_child_error[c] != 0 && zio->io_error == 0)
862 zio->io_error = zio->io_child_error[c];
866 zio_bookmark_compare(const void *x1, const void *x2)
868 const zio_t *z1 = x1;
869 const zio_t *z2 = x2;
871 if (z1->io_bookmark.zb_objset < z2->io_bookmark.zb_objset)
872 return (-1);
873 if (z1->io_bookmark.zb_objset > z2->io_bookmark.zb_objset)
874 return (1);
876 if (z1->io_bookmark.zb_object < z2->io_bookmark.zb_object)
877 return (-1);
878 if (z1->io_bookmark.zb_object > z2->io_bookmark.zb_object)
879 return (1);
881 if (z1->io_bookmark.zb_level < z2->io_bookmark.zb_level)
882 return (-1);
883 if (z1->io_bookmark.zb_level > z2->io_bookmark.zb_level)
884 return (1);
886 if (z1->io_bookmark.zb_blkid < z2->io_bookmark.zb_blkid)
887 return (-1);
888 if (z1->io_bookmark.zb_blkid > z2->io_bookmark.zb_blkid)
889 return (1);
891 if (z1 < z2)
892 return (-1);
893 if (z1 > z2)
894 return (1);
896 return (0);
900 * ==========================================================================
901 * Create the various types of I/O (read, write, free, etc)
902 * ==========================================================================
904 static zio_t *
905 zio_create(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
906 abd_t *data, uint64_t lsize, uint64_t psize, zio_done_func_t *done,
907 void *private, zio_type_t type, zio_priority_t priority,
908 zio_flag_t flags, vdev_t *vd, uint64_t offset,
909 const zbookmark_phys_t *zb, enum zio_stage stage,
910 enum zio_stage pipeline)
912 zio_t *zio;
914 IMPLY(type != ZIO_TYPE_TRIM, psize <= SPA_MAXBLOCKSIZE);
915 ASSERT(P2PHASE(psize, SPA_MINBLOCKSIZE) == 0);
916 ASSERT(P2PHASE(offset, SPA_MINBLOCKSIZE) == 0);
918 ASSERT(!vd || spa_config_held(spa, SCL_STATE_ALL, RW_READER));
919 ASSERT(!bp || !(flags & ZIO_FLAG_CONFIG_WRITER));
920 ASSERT(vd || stage == ZIO_STAGE_OPEN);
922 IMPLY(lsize != psize, (flags & ZIO_FLAG_RAW_COMPRESS) != 0);
924 zio = kmem_cache_alloc(zio_cache, KM_SLEEP);
925 memset(zio, 0, sizeof (zio_t));
927 mutex_init(&zio->io_lock, NULL, MUTEX_NOLOCKDEP, NULL);
928 cv_init(&zio->io_cv, NULL, CV_DEFAULT, NULL);
930 list_create(&zio->io_parent_list, sizeof (zio_link_t),
931 offsetof(zio_link_t, zl_parent_node));
932 list_create(&zio->io_child_list, sizeof (zio_link_t),
933 offsetof(zio_link_t, zl_child_node));
934 metaslab_trace_init(&zio->io_alloc_list);
936 if (vd != NULL)
937 zio->io_child_type = ZIO_CHILD_VDEV;
938 else if (flags & ZIO_FLAG_GANG_CHILD)
939 zio->io_child_type = ZIO_CHILD_GANG;
940 else if (flags & ZIO_FLAG_DDT_CHILD)
941 zio->io_child_type = ZIO_CHILD_DDT;
942 else
943 zio->io_child_type = ZIO_CHILD_LOGICAL;
945 if (bp != NULL) {
946 if (type != ZIO_TYPE_WRITE ||
947 zio->io_child_type == ZIO_CHILD_DDT) {
948 zio->io_bp_copy = *bp;
949 zio->io_bp = &zio->io_bp_copy; /* so caller can free */
950 } else {
951 zio->io_bp = (blkptr_t *)bp;
953 zio->io_bp_orig = *bp;
954 if (zio->io_child_type == ZIO_CHILD_LOGICAL)
955 zio->io_logical = zio;
956 if (zio->io_child_type > ZIO_CHILD_GANG && BP_IS_GANG(bp))
957 pipeline |= ZIO_GANG_STAGES;
960 zio->io_spa = spa;
961 zio->io_txg = txg;
962 zio->io_done = done;
963 zio->io_private = private;
964 zio->io_type = type;
965 zio->io_priority = priority;
966 zio->io_vd = vd;
967 zio->io_offset = offset;
968 zio->io_orig_abd = zio->io_abd = data;
969 zio->io_orig_size = zio->io_size = psize;
970 zio->io_lsize = lsize;
971 zio->io_orig_flags = zio->io_flags = flags;
972 zio->io_orig_stage = zio->io_stage = stage;
973 zio->io_orig_pipeline = zio->io_pipeline = pipeline;
974 zio->io_pipeline_trace = ZIO_STAGE_OPEN;
975 zio->io_allocator = ZIO_ALLOCATOR_NONE;
977 zio->io_state[ZIO_WAIT_READY] = (stage >= ZIO_STAGE_READY) ||
978 (pipeline & ZIO_STAGE_READY) == 0;
979 zio->io_state[ZIO_WAIT_DONE] = (stage >= ZIO_STAGE_DONE);
981 if (zb != NULL)
982 zio->io_bookmark = *zb;
984 if (pio != NULL) {
985 zio->io_metaslab_class = pio->io_metaslab_class;
986 if (zio->io_logical == NULL)
987 zio->io_logical = pio->io_logical;
988 if (zio->io_child_type == ZIO_CHILD_GANG)
989 zio->io_gang_leader = pio->io_gang_leader;
990 zio_add_child_first(pio, zio);
993 taskq_init_ent(&zio->io_tqent);
995 return (zio);
998 void
999 zio_destroy(zio_t *zio)
1001 metaslab_trace_fini(&zio->io_alloc_list);
1002 list_destroy(&zio->io_parent_list);
1003 list_destroy(&zio->io_child_list);
1004 mutex_destroy(&zio->io_lock);
1005 cv_destroy(&zio->io_cv);
1006 kmem_cache_free(zio_cache, zio);
1010 * ZIO intended to be between others. Provides synchronization at READY
1011 * and DONE pipeline stages and calls the respective callbacks.
1013 zio_t *
1014 zio_null(zio_t *pio, spa_t *spa, vdev_t *vd, zio_done_func_t *done,
1015 void *private, zio_flag_t flags)
1017 zio_t *zio;
1019 zio = zio_create(pio, spa, 0, NULL, NULL, 0, 0, done, private,
1020 ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
1021 ZIO_STAGE_OPEN, ZIO_INTERLOCK_PIPELINE);
1023 return (zio);
1027 * ZIO intended to be a root of a tree. Unlike null ZIO does not have a
1028 * READY pipeline stage (is ready on creation), so it should not be used
1029 * as child of any ZIO that may need waiting for grandchildren READY stage
1030 * (any other ZIO type).
1032 zio_t *
1033 zio_root(spa_t *spa, zio_done_func_t *done, void *private, zio_flag_t flags)
1035 zio_t *zio;
1037 zio = zio_create(NULL, spa, 0, NULL, NULL, 0, 0, done, private,
1038 ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, NULL, 0, NULL,
1039 ZIO_STAGE_OPEN, ZIO_ROOT_PIPELINE);
1041 return (zio);
1044 static int
1045 zfs_blkptr_verify_log(spa_t *spa, const blkptr_t *bp,
1046 enum blk_verify_flag blk_verify, const char *fmt, ...)
1048 va_list adx;
1049 char buf[256];
1051 va_start(adx, fmt);
1052 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
1053 va_end(adx);
1055 zfs_dbgmsg("bad blkptr at %px: "
1056 "DVA[0]=%#llx/%#llx "
1057 "DVA[1]=%#llx/%#llx "
1058 "DVA[2]=%#llx/%#llx "
1059 "prop=%#llx "
1060 "pad=%#llx,%#llx "
1061 "phys_birth=%#llx "
1062 "birth=%#llx "
1063 "fill=%#llx "
1064 "cksum=%#llx/%#llx/%#llx/%#llx",
1066 (long long)bp->blk_dva[0].dva_word[0],
1067 (long long)bp->blk_dva[0].dva_word[1],
1068 (long long)bp->blk_dva[1].dva_word[0],
1069 (long long)bp->blk_dva[1].dva_word[1],
1070 (long long)bp->blk_dva[2].dva_word[0],
1071 (long long)bp->blk_dva[2].dva_word[1],
1072 (long long)bp->blk_prop,
1073 (long long)bp->blk_pad[0],
1074 (long long)bp->blk_pad[1],
1075 (long long)BP_GET_PHYSICAL_BIRTH(bp),
1076 (long long)BP_GET_LOGICAL_BIRTH(bp),
1077 (long long)bp->blk_fill,
1078 (long long)bp->blk_cksum.zc_word[0],
1079 (long long)bp->blk_cksum.zc_word[1],
1080 (long long)bp->blk_cksum.zc_word[2],
1081 (long long)bp->blk_cksum.zc_word[3]);
1082 switch (blk_verify) {
1083 case BLK_VERIFY_HALT:
1084 zfs_panic_recover("%s: %s", spa_name(spa), buf);
1085 break;
1086 case BLK_VERIFY_LOG:
1087 zfs_dbgmsg("%s: %s", spa_name(spa), buf);
1088 break;
1089 case BLK_VERIFY_ONLY:
1090 break;
1093 return (1);
1097 * Verify the block pointer fields contain reasonable values. This means
1098 * it only contains known object types, checksum/compression identifiers,
1099 * block sizes within the maximum allowed limits, valid DVAs, etc.
1101 * If everything checks out B_TRUE is returned. The zfs_blkptr_verify
1102 * argument controls the behavior when an invalid field is detected.
1104 * Values for blk_verify_flag:
1105 * BLK_VERIFY_ONLY: evaluate the block
1106 * BLK_VERIFY_LOG: evaluate the block and log problems
1107 * BLK_VERIFY_HALT: call zfs_panic_recover on error
1109 * Values for blk_config_flag:
1110 * BLK_CONFIG_HELD: caller holds SCL_VDEV for writer
1111 * BLK_CONFIG_NEEDED: caller holds no config lock, SCL_VDEV will be
1112 * obtained for reader
1113 * BLK_CONFIG_SKIP: skip checks which require SCL_VDEV, for better
1114 * performance
1116 boolean_t
1117 zfs_blkptr_verify(spa_t *spa, const blkptr_t *bp,
1118 enum blk_config_flag blk_config, enum blk_verify_flag blk_verify)
1120 int errors = 0;
1122 if (unlikely(!DMU_OT_IS_VALID(BP_GET_TYPE(bp)))) {
1123 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1124 "blkptr at %px has invalid TYPE %llu",
1125 bp, (longlong_t)BP_GET_TYPE(bp));
1127 if (unlikely(BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_FUNCTIONS)) {
1128 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1129 "blkptr at %px has invalid COMPRESS %llu",
1130 bp, (longlong_t)BP_GET_COMPRESS(bp));
1132 if (unlikely(BP_GET_LSIZE(bp) > SPA_MAXBLOCKSIZE)) {
1133 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1134 "blkptr at %px has invalid LSIZE %llu",
1135 bp, (longlong_t)BP_GET_LSIZE(bp));
1137 if (BP_IS_EMBEDDED(bp)) {
1138 if (unlikely(BPE_GET_ETYPE(bp) >= NUM_BP_EMBEDDED_TYPES)) {
1139 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1140 "blkptr at %px has invalid ETYPE %llu",
1141 bp, (longlong_t)BPE_GET_ETYPE(bp));
1143 if (unlikely(BPE_GET_PSIZE(bp) > BPE_PAYLOAD_SIZE)) {
1144 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1145 "blkptr at %px has invalid PSIZE %llu",
1146 bp, (longlong_t)BPE_GET_PSIZE(bp));
1148 return (errors == 0);
1150 if (unlikely(BP_GET_CHECKSUM(bp) >= ZIO_CHECKSUM_FUNCTIONS)) {
1151 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1152 "blkptr at %px has invalid CHECKSUM %llu",
1153 bp, (longlong_t)BP_GET_CHECKSUM(bp));
1155 if (unlikely(BP_GET_PSIZE(bp) > SPA_MAXBLOCKSIZE)) {
1156 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1157 "blkptr at %px has invalid PSIZE %llu",
1158 bp, (longlong_t)BP_GET_PSIZE(bp));
1162 * Do not verify individual DVAs if the config is not trusted. This
1163 * will be done once the zio is executed in vdev_mirror_map_alloc.
1165 if (unlikely(!spa->spa_trust_config))
1166 return (errors == 0);
1168 switch (blk_config) {
1169 case BLK_CONFIG_HELD:
1170 ASSERT(spa_config_held(spa, SCL_VDEV, RW_WRITER));
1171 break;
1172 case BLK_CONFIG_NEEDED:
1173 spa_config_enter(spa, SCL_VDEV, bp, RW_READER);
1174 break;
1175 case BLK_CONFIG_SKIP:
1176 return (errors == 0);
1177 default:
1178 panic("invalid blk_config %u", blk_config);
1182 * Pool-specific checks.
1184 * Note: it would be nice to verify that the logical birth
1185 * and physical birth are not too large. However,
1186 * spa_freeze() allows the birth time of log blocks (and
1187 * dmu_sync()-ed blocks that are in the log) to be arbitrarily
1188 * large.
1190 for (int i = 0; i < BP_GET_NDVAS(bp); i++) {
1191 const dva_t *dva = &bp->blk_dva[i];
1192 uint64_t vdevid = DVA_GET_VDEV(dva);
1194 if (unlikely(vdevid >= spa->spa_root_vdev->vdev_children)) {
1195 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1196 "blkptr at %px DVA %u has invalid VDEV %llu",
1197 bp, i, (longlong_t)vdevid);
1198 continue;
1200 vdev_t *vd = spa->spa_root_vdev->vdev_child[vdevid];
1201 if (unlikely(vd == NULL)) {
1202 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1203 "blkptr at %px DVA %u has invalid VDEV %llu",
1204 bp, i, (longlong_t)vdevid);
1205 continue;
1207 if (unlikely(vd->vdev_ops == &vdev_hole_ops)) {
1208 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1209 "blkptr at %px DVA %u has hole VDEV %llu",
1210 bp, i, (longlong_t)vdevid);
1211 continue;
1213 if (vd->vdev_ops == &vdev_missing_ops) {
1215 * "missing" vdevs are valid during import, but we
1216 * don't have their detailed info (e.g. asize), so
1217 * we can't perform any more checks on them.
1219 continue;
1221 uint64_t offset = DVA_GET_OFFSET(dva);
1222 uint64_t asize = DVA_GET_ASIZE(dva);
1223 if (DVA_GET_GANG(dva))
1224 asize = vdev_gang_header_asize(vd);
1225 if (unlikely(offset + asize > vd->vdev_asize)) {
1226 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1227 "blkptr at %px DVA %u has invalid OFFSET %llu",
1228 bp, i, (longlong_t)offset);
1231 if (blk_config == BLK_CONFIG_NEEDED)
1232 spa_config_exit(spa, SCL_VDEV, bp);
1234 return (errors == 0);
1237 boolean_t
1238 zfs_dva_valid(spa_t *spa, const dva_t *dva, const blkptr_t *bp)
1240 (void) bp;
1241 uint64_t vdevid = DVA_GET_VDEV(dva);
1243 if (vdevid >= spa->spa_root_vdev->vdev_children)
1244 return (B_FALSE);
1246 vdev_t *vd = spa->spa_root_vdev->vdev_child[vdevid];
1247 if (vd == NULL)
1248 return (B_FALSE);
1250 if (vd->vdev_ops == &vdev_hole_ops)
1251 return (B_FALSE);
1253 if (vd->vdev_ops == &vdev_missing_ops) {
1254 return (B_FALSE);
1257 uint64_t offset = DVA_GET_OFFSET(dva);
1258 uint64_t asize = DVA_GET_ASIZE(dva);
1260 if (DVA_GET_GANG(dva))
1261 asize = vdev_gang_header_asize(vd);
1262 if (offset + asize > vd->vdev_asize)
1263 return (B_FALSE);
1265 return (B_TRUE);
1268 zio_t *
1269 zio_read(zio_t *pio, spa_t *spa, const blkptr_t *bp,
1270 abd_t *data, uint64_t size, zio_done_func_t *done, void *private,
1271 zio_priority_t priority, zio_flag_t flags, const zbookmark_phys_t *zb)
1273 zio_t *zio;
1275 zio = zio_create(pio, spa, BP_GET_BIRTH(bp), bp,
1276 data, size, size, done, private,
1277 ZIO_TYPE_READ, priority, flags, NULL, 0, zb,
1278 ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
1279 ZIO_DDT_CHILD_READ_PIPELINE : ZIO_READ_PIPELINE);
1281 return (zio);
1284 zio_t *
1285 zio_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
1286 abd_t *data, uint64_t lsize, uint64_t psize, const zio_prop_t *zp,
1287 zio_done_func_t *ready, zio_done_func_t *children_ready,
1288 zio_done_func_t *done, void *private, zio_priority_t priority,
1289 zio_flag_t flags, const zbookmark_phys_t *zb)
1291 zio_t *zio;
1292 enum zio_stage pipeline = zp->zp_direct_write == B_TRUE ?
1293 ZIO_DIRECT_WRITE_PIPELINE : (flags & ZIO_FLAG_DDT_CHILD) ?
1294 ZIO_DDT_CHILD_WRITE_PIPELINE : ZIO_WRITE_PIPELINE;
1297 zio = zio_create(pio, spa, txg, bp, data, lsize, psize, done, private,
1298 ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb,
1299 ZIO_STAGE_OPEN, pipeline);
1301 zio->io_ready = ready;
1302 zio->io_children_ready = children_ready;
1303 zio->io_prop = *zp;
1306 * Data can be NULL if we are going to call zio_write_override() to
1307 * provide the already-allocated BP. But we may need the data to
1308 * verify a dedup hit (if requested). In this case, don't try to
1309 * dedup (just take the already-allocated BP verbatim). Encrypted
1310 * dedup blocks need data as well so we also disable dedup in this
1311 * case.
1313 if (data == NULL &&
1314 (zio->io_prop.zp_dedup_verify || zio->io_prop.zp_encrypt)) {
1315 zio->io_prop.zp_dedup = zio->io_prop.zp_dedup_verify = B_FALSE;
1318 return (zio);
1321 zio_t *
1322 zio_rewrite(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, abd_t *data,
1323 uint64_t size, zio_done_func_t *done, void *private,
1324 zio_priority_t priority, zio_flag_t flags, zbookmark_phys_t *zb)
1326 zio_t *zio;
1328 zio = zio_create(pio, spa, txg, bp, data, size, size, done, private,
1329 ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_IO_REWRITE, NULL, 0, zb,
1330 ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE);
1332 return (zio);
1335 void
1336 zio_write_override(zio_t *zio, blkptr_t *bp, int copies, boolean_t nopwrite,
1337 boolean_t brtwrite)
1339 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
1340 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1341 ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
1342 ASSERT(zio->io_txg == spa_syncing_txg(zio->io_spa));
1343 ASSERT(!brtwrite || !nopwrite);
1346 * We must reset the io_prop to match the values that existed
1347 * when the bp was first written by dmu_sync() keeping in mind
1348 * that nopwrite and dedup are mutually exclusive.
1350 zio->io_prop.zp_dedup = nopwrite ? B_FALSE : zio->io_prop.zp_dedup;
1351 zio->io_prop.zp_nopwrite = nopwrite;
1352 zio->io_prop.zp_brtwrite = brtwrite;
1353 zio->io_prop.zp_copies = copies;
1354 zio->io_bp_override = bp;
1357 void
1358 zio_free(spa_t *spa, uint64_t txg, const blkptr_t *bp)
1361 (void) zfs_blkptr_verify(spa, bp, BLK_CONFIG_NEEDED, BLK_VERIFY_HALT);
1364 * The check for EMBEDDED is a performance optimization. We
1365 * process the free here (by ignoring it) rather than
1366 * putting it on the list and then processing it in zio_free_sync().
1368 if (BP_IS_EMBEDDED(bp))
1369 return;
1372 * Frees that are for the currently-syncing txg, are not going to be
1373 * deferred, and which will not need to do a read (i.e. not GANG or
1374 * DEDUP), can be processed immediately. Otherwise, put them on the
1375 * in-memory list for later processing.
1377 * Note that we only defer frees after zfs_sync_pass_deferred_free
1378 * when the log space map feature is disabled. [see relevant comment
1379 * in spa_sync_iterate_to_convergence()]
1381 if (BP_IS_GANG(bp) ||
1382 BP_GET_DEDUP(bp) ||
1383 txg != spa->spa_syncing_txg ||
1384 (spa_sync_pass(spa) >= zfs_sync_pass_deferred_free &&
1385 !spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP)) ||
1386 brt_maybe_exists(spa, bp)) {
1387 metaslab_check_free(spa, bp);
1388 bplist_append(&spa->spa_free_bplist[txg & TXG_MASK], bp);
1389 } else {
1390 VERIFY3P(zio_free_sync(NULL, spa, txg, bp, 0), ==, NULL);
1395 * To improve performance, this function may return NULL if we were able
1396 * to do the free immediately. This avoids the cost of creating a zio
1397 * (and linking it to the parent, etc).
1399 zio_t *
1400 zio_free_sync(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
1401 zio_flag_t flags)
1403 ASSERT(!BP_IS_HOLE(bp));
1404 ASSERT(spa_syncing_txg(spa) == txg);
1406 if (BP_IS_EMBEDDED(bp))
1407 return (NULL);
1409 metaslab_check_free(spa, bp);
1410 arc_freed(spa, bp);
1411 dsl_scan_freed(spa, bp);
1413 if (BP_IS_GANG(bp) ||
1414 BP_GET_DEDUP(bp) ||
1415 brt_maybe_exists(spa, bp)) {
1417 * GANG, DEDUP and BRT blocks can induce a read (for the gang
1418 * block header, the DDT or the BRT), so issue them
1419 * asynchronously so that this thread is not tied up.
1421 enum zio_stage stage =
1422 ZIO_FREE_PIPELINE | ZIO_STAGE_ISSUE_ASYNC;
1424 return (zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
1425 BP_GET_PSIZE(bp), NULL, NULL,
1426 ZIO_TYPE_FREE, ZIO_PRIORITY_NOW,
1427 flags, NULL, 0, NULL, ZIO_STAGE_OPEN, stage));
1428 } else {
1429 metaslab_free(spa, bp, txg, B_FALSE);
1430 return (NULL);
1434 zio_t *
1435 zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
1436 zio_done_func_t *done, void *private, zio_flag_t flags)
1438 zio_t *zio;
1440 (void) zfs_blkptr_verify(spa, bp, (flags & ZIO_FLAG_CONFIG_WRITER) ?
1441 BLK_CONFIG_HELD : BLK_CONFIG_NEEDED, BLK_VERIFY_HALT);
1443 if (BP_IS_EMBEDDED(bp))
1444 return (zio_null(pio, spa, NULL, NULL, NULL, 0));
1447 * A claim is an allocation of a specific block. Claims are needed
1448 * to support immediate writes in the intent log. The issue is that
1449 * immediate writes contain committed data, but in a txg that was
1450 * *not* committed. Upon opening the pool after an unclean shutdown,
1451 * the intent log claims all blocks that contain immediate write data
1452 * so that the SPA knows they're in use.
1454 * All claims *must* be resolved in the first txg -- before the SPA
1455 * starts allocating blocks -- so that nothing is allocated twice.
1456 * If txg == 0 we just verify that the block is claimable.
1458 ASSERT3U(BP_GET_LOGICAL_BIRTH(&spa->spa_uberblock.ub_rootbp), <,
1459 spa_min_claim_txg(spa));
1460 ASSERT(txg == spa_min_claim_txg(spa) || txg == 0);
1461 ASSERT(!BP_GET_DEDUP(bp) || !spa_writeable(spa)); /* zdb(8) */
1463 zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
1464 BP_GET_PSIZE(bp), done, private, ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW,
1465 flags, NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE);
1466 ASSERT0(zio->io_queued_timestamp);
1468 return (zio);
1471 zio_t *
1472 zio_trim(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
1473 zio_done_func_t *done, void *private, zio_priority_t priority,
1474 zio_flag_t flags, enum trim_flag trim_flags)
1476 zio_t *zio;
1478 ASSERT0(vd->vdev_children);
1479 ASSERT0(P2PHASE(offset, 1ULL << vd->vdev_ashift));
1480 ASSERT0(P2PHASE(size, 1ULL << vd->vdev_ashift));
1481 ASSERT3U(size, !=, 0);
1483 zio = zio_create(pio, vd->vdev_spa, 0, NULL, NULL, size, size, done,
1484 private, ZIO_TYPE_TRIM, priority, flags | ZIO_FLAG_PHYSICAL,
1485 vd, offset, NULL, ZIO_STAGE_OPEN, ZIO_TRIM_PIPELINE);
1486 zio->io_trim_flags = trim_flags;
1488 return (zio);
1491 zio_t *
1492 zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
1493 abd_t *data, int checksum, zio_done_func_t *done, void *private,
1494 zio_priority_t priority, zio_flag_t flags, boolean_t labels)
1496 zio_t *zio;
1498 ASSERT(vd->vdev_children == 0);
1499 ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
1500 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
1501 ASSERT3U(offset + size, <=, vd->vdev_psize);
1503 zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, size, done,
1504 private, ZIO_TYPE_READ, priority, flags | ZIO_FLAG_PHYSICAL, vd,
1505 offset, NULL, ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE);
1507 zio->io_prop.zp_checksum = checksum;
1509 return (zio);
1512 zio_t *
1513 zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
1514 abd_t *data, int checksum, zio_done_func_t *done, void *private,
1515 zio_priority_t priority, zio_flag_t flags, boolean_t labels)
1517 zio_t *zio;
1519 ASSERT(vd->vdev_children == 0);
1520 ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
1521 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
1522 ASSERT3U(offset + size, <=, vd->vdev_psize);
1524 zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, size, done,
1525 private, ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_PHYSICAL, vd,
1526 offset, NULL, ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE);
1528 zio->io_prop.zp_checksum = checksum;
1530 if (zio_checksum_table[checksum].ci_flags & ZCHECKSUM_FLAG_EMBEDDED) {
1532 * zec checksums are necessarily destructive -- they modify
1533 * the end of the write buffer to hold the verifier/checksum.
1534 * Therefore, we must make a local copy in case the data is
1535 * being written to multiple places in parallel.
1537 abd_t *wbuf = abd_alloc_sametype(data, size);
1538 abd_copy(wbuf, data, size);
1540 zio_push_transform(zio, wbuf, size, size, NULL);
1543 return (zio);
1547 * Create a child I/O to do some work for us.
1549 zio_t *
1550 zio_vdev_child_io(zio_t *pio, blkptr_t *bp, vdev_t *vd, uint64_t offset,
1551 abd_t *data, uint64_t size, int type, zio_priority_t priority,
1552 zio_flag_t flags, zio_done_func_t *done, void *private)
1554 enum zio_stage pipeline = ZIO_VDEV_CHILD_PIPELINE;
1555 zio_t *zio;
1558 * vdev child I/Os do not propagate their error to the parent.
1559 * Therefore, for correct operation the caller *must* check for
1560 * and handle the error in the child i/o's done callback.
1561 * The only exceptions are i/os that we don't care about
1562 * (OPTIONAL or REPAIR).
1564 ASSERT((flags & ZIO_FLAG_OPTIONAL) || (flags & ZIO_FLAG_IO_REPAIR) ||
1565 done != NULL);
1567 if (type == ZIO_TYPE_READ && bp != NULL) {
1569 * If we have the bp, then the child should perform the
1570 * checksum and the parent need not. This pushes error
1571 * detection as close to the leaves as possible and
1572 * eliminates redundant checksums in the interior nodes.
1574 pipeline |= ZIO_STAGE_CHECKSUM_VERIFY;
1575 pio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
1577 * We never allow the mirror VDEV to attempt reading from any
1578 * additional data copies after the first Direct I/O checksum
1579 * verify failure. This is to avoid bad data being written out
1580 * through the mirror during self healing. See comment in
1581 * vdev_mirror_io_done() for more details.
1583 ASSERT0(pio->io_flags & ZIO_FLAG_DIO_CHKSUM_ERR);
1584 } else if (type == ZIO_TYPE_WRITE &&
1585 pio->io_prop.zp_direct_write == B_TRUE) {
1587 * By default we only will verify checksums for Direct I/O
1588 * writes for Linux. FreeBSD is able to place user pages under
1589 * write protection before issuing them to the ZIO pipeline.
1591 * Checksum validation errors will only be reported through
1592 * the top-level VDEV, which is set by this child ZIO.
1594 ASSERT3P(bp, !=, NULL);
1595 ASSERT3U(pio->io_child_type, ==, ZIO_CHILD_LOGICAL);
1596 pipeline |= ZIO_STAGE_DIO_CHECKSUM_VERIFY;
1599 if (vd->vdev_ops->vdev_op_leaf) {
1600 ASSERT0(vd->vdev_children);
1601 offset += VDEV_LABEL_START_SIZE;
1604 flags |= ZIO_VDEV_CHILD_FLAGS(pio);
1607 * If we've decided to do a repair, the write is not speculative --
1608 * even if the original read was.
1610 if (flags & ZIO_FLAG_IO_REPAIR)
1611 flags &= ~ZIO_FLAG_SPECULATIVE;
1614 * If we're creating a child I/O that is not associated with a
1615 * top-level vdev, then the child zio is not an allocating I/O.
1616 * If this is a retried I/O then we ignore it since we will
1617 * have already processed the original allocating I/O.
1619 if (flags & ZIO_FLAG_IO_ALLOCATING &&
1620 (vd != vd->vdev_top || (flags & ZIO_FLAG_IO_RETRY))) {
1621 ASSERT(pio->io_metaslab_class != NULL);
1622 ASSERT(pio->io_metaslab_class->mc_alloc_throttle_enabled);
1623 ASSERT(type == ZIO_TYPE_WRITE);
1624 ASSERT(priority == ZIO_PRIORITY_ASYNC_WRITE);
1625 ASSERT(!(flags & ZIO_FLAG_IO_REPAIR));
1626 ASSERT(!(pio->io_flags & ZIO_FLAG_IO_REWRITE) ||
1627 pio->io_child_type == ZIO_CHILD_GANG);
1629 flags &= ~ZIO_FLAG_IO_ALLOCATING;
1632 zio = zio_create(pio, pio->io_spa, pio->io_txg, bp, data, size, size,
1633 done, private, type, priority, flags, vd, offset, &pio->io_bookmark,
1634 ZIO_STAGE_VDEV_IO_START >> 1, pipeline);
1635 ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
1637 return (zio);
1640 zio_t *
1641 zio_vdev_delegated_io(vdev_t *vd, uint64_t offset, abd_t *data, uint64_t size,
1642 zio_type_t type, zio_priority_t priority, zio_flag_t flags,
1643 zio_done_func_t *done, void *private)
1645 zio_t *zio;
1647 ASSERT(vd->vdev_ops->vdev_op_leaf);
1649 zio = zio_create(NULL, vd->vdev_spa, 0, NULL,
1650 data, size, size, done, private, type, priority,
1651 flags | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY | ZIO_FLAG_DELEGATED,
1652 vd, offset, NULL,
1653 ZIO_STAGE_VDEV_IO_START >> 1, ZIO_VDEV_CHILD_PIPELINE);
1655 return (zio);
1660 * Send a flush command to the given vdev. Unlike most zio creation functions,
1661 * the flush zios are issued immediately. You can wait on pio to pause until
1662 * the flushes complete.
1664 void
1665 zio_flush(zio_t *pio, vdev_t *vd)
1667 const zio_flag_t flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE |
1668 ZIO_FLAG_DONT_RETRY;
1670 if (vd->vdev_nowritecache)
1671 return;
1673 if (vd->vdev_children == 0) {
1674 zio_nowait(zio_create(pio, vd->vdev_spa, 0, NULL, NULL, 0, 0,
1675 NULL, NULL, ZIO_TYPE_FLUSH, ZIO_PRIORITY_NOW, flags, vd, 0,
1676 NULL, ZIO_STAGE_OPEN, ZIO_FLUSH_PIPELINE));
1677 } else {
1678 for (uint64_t c = 0; c < vd->vdev_children; c++)
1679 zio_flush(pio, vd->vdev_child[c]);
1683 void
1684 zio_shrink(zio_t *zio, uint64_t size)
1686 ASSERT3P(zio->io_executor, ==, NULL);
1687 ASSERT3U(zio->io_orig_size, ==, zio->io_size);
1688 ASSERT3U(size, <=, zio->io_size);
1691 * We don't shrink for raidz because of problems with the
1692 * reconstruction when reading back less than the block size.
1693 * Note, BP_IS_RAIDZ() assumes no compression.
1695 ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
1696 if (!BP_IS_RAIDZ(zio->io_bp)) {
1697 /* we are not doing a raw write */
1698 ASSERT3U(zio->io_size, ==, zio->io_lsize);
1699 zio->io_orig_size = zio->io_size = zio->io_lsize = size;
1704 * Round provided allocation size up to a value that can be allocated
1705 * by at least some vdev(s) in the pool with minimum or no additional
1706 * padding and without extra space usage on others
1708 static uint64_t
1709 zio_roundup_alloc_size(spa_t *spa, uint64_t size)
1711 if (size > spa->spa_min_alloc)
1712 return (roundup(size, spa->spa_gcd_alloc));
1713 return (spa->spa_min_alloc);
1716 size_t
1717 zio_get_compression_max_size(enum zio_compress compress, uint64_t gcd_alloc,
1718 uint64_t min_alloc, size_t s_len)
1720 size_t d_len;
1722 /* minimum 12.5% must be saved (legacy value, may be changed later) */
1723 d_len = s_len - (s_len >> 3);
1725 /* ZLE can't use exactly d_len bytes, it needs more, so ignore it */
1726 if (compress == ZIO_COMPRESS_ZLE)
1727 return (d_len);
1729 d_len = d_len - d_len % gcd_alloc;
1731 if (d_len < min_alloc)
1732 return (BPE_PAYLOAD_SIZE);
1733 return (d_len);
1737 * ==========================================================================
1738 * Prepare to read and write logical blocks
1739 * ==========================================================================
1742 static zio_t *
1743 zio_read_bp_init(zio_t *zio)
1745 blkptr_t *bp = zio->io_bp;
1746 uint64_t psize =
1747 BP_IS_EMBEDDED(bp) ? BPE_GET_PSIZE(bp) : BP_GET_PSIZE(bp);
1749 ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy);
1751 if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF &&
1752 zio->io_child_type == ZIO_CHILD_LOGICAL &&
1753 !(zio->io_flags & ZIO_FLAG_RAW_COMPRESS)) {
1754 zio_push_transform(zio, abd_alloc_sametype(zio->io_abd, psize),
1755 psize, psize, zio_decompress);
1758 if (((BP_IS_PROTECTED(bp) && !(zio->io_flags & ZIO_FLAG_RAW_ENCRYPT)) ||
1759 BP_HAS_INDIRECT_MAC_CKSUM(bp)) &&
1760 zio->io_child_type == ZIO_CHILD_LOGICAL) {
1761 zio_push_transform(zio, abd_alloc_sametype(zio->io_abd, psize),
1762 psize, psize, zio_decrypt);
1765 if (BP_IS_EMBEDDED(bp) && BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA) {
1766 int psize = BPE_GET_PSIZE(bp);
1767 void *data = abd_borrow_buf(zio->io_abd, psize);
1769 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1770 decode_embedded_bp_compressed(bp, data);
1771 abd_return_buf_copy(zio->io_abd, data, psize);
1772 } else {
1773 ASSERT(!BP_IS_EMBEDDED(bp));
1776 if (BP_GET_DEDUP(bp) && zio->io_child_type == ZIO_CHILD_LOGICAL)
1777 zio->io_pipeline = ZIO_DDT_READ_PIPELINE;
1779 return (zio);
1782 static zio_t *
1783 zio_write_bp_init(zio_t *zio)
1785 if (!IO_IS_ALLOCATING(zio))
1786 return (zio);
1788 ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
1790 if (zio->io_bp_override) {
1791 blkptr_t *bp = zio->io_bp;
1792 zio_prop_t *zp = &zio->io_prop;
1794 ASSERT(BP_GET_LOGICAL_BIRTH(bp) != zio->io_txg);
1796 *bp = *zio->io_bp_override;
1797 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1799 if (zp->zp_brtwrite)
1800 return (zio);
1802 ASSERT(!BP_GET_DEDUP(zio->io_bp_override));
1804 if (BP_IS_EMBEDDED(bp))
1805 return (zio);
1808 * If we've been overridden and nopwrite is set then
1809 * set the flag accordingly to indicate that a nopwrite
1810 * has already occurred.
1812 if (!BP_IS_HOLE(bp) && zp->zp_nopwrite) {
1813 ASSERT(!zp->zp_dedup);
1814 ASSERT3U(BP_GET_CHECKSUM(bp), ==, zp->zp_checksum);
1815 zio->io_flags |= ZIO_FLAG_NOPWRITE;
1816 return (zio);
1819 ASSERT(!zp->zp_nopwrite);
1821 if (BP_IS_HOLE(bp) || !zp->zp_dedup)
1822 return (zio);
1824 ASSERT((zio_checksum_table[zp->zp_checksum].ci_flags &
1825 ZCHECKSUM_FLAG_DEDUP) || zp->zp_dedup_verify);
1827 if (BP_GET_CHECKSUM(bp) == zp->zp_checksum &&
1828 !zp->zp_encrypt) {
1829 BP_SET_DEDUP(bp, 1);
1830 zio->io_pipeline |= ZIO_STAGE_DDT_WRITE;
1831 return (zio);
1835 * We were unable to handle this as an override bp, treat
1836 * it as a regular write I/O.
1838 zio->io_bp_override = NULL;
1839 *bp = zio->io_bp_orig;
1840 zio->io_pipeline = zio->io_orig_pipeline;
1843 return (zio);
1846 static zio_t *
1847 zio_write_compress(zio_t *zio)
1849 spa_t *spa = zio->io_spa;
1850 zio_prop_t *zp = &zio->io_prop;
1851 enum zio_compress compress = zp->zp_compress;
1852 blkptr_t *bp = zio->io_bp;
1853 uint64_t lsize = zio->io_lsize;
1854 uint64_t psize = zio->io_size;
1855 uint32_t pass = 1;
1858 * If our children haven't all reached the ready stage,
1859 * wait for them and then repeat this pipeline stage.
1861 if (zio_wait_for_children(zio, ZIO_CHILD_LOGICAL_BIT |
1862 ZIO_CHILD_GANG_BIT, ZIO_WAIT_READY)) {
1863 return (NULL);
1866 if (!IO_IS_ALLOCATING(zio))
1867 return (zio);
1869 if (zio->io_children_ready != NULL) {
1871 * Now that all our children are ready, run the callback
1872 * associated with this zio in case it wants to modify the
1873 * data to be written.
1875 ASSERT3U(zp->zp_level, >, 0);
1876 zio->io_children_ready(zio);
1879 ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
1880 ASSERT(zio->io_bp_override == NULL);
1882 if (!BP_IS_HOLE(bp) && BP_GET_LOGICAL_BIRTH(bp) == zio->io_txg) {
1884 * We're rewriting an existing block, which means we're
1885 * working on behalf of spa_sync(). For spa_sync() to
1886 * converge, it must eventually be the case that we don't
1887 * have to allocate new blocks. But compression changes
1888 * the blocksize, which forces a reallocate, and makes
1889 * convergence take longer. Therefore, after the first
1890 * few passes, stop compressing to ensure convergence.
1892 pass = spa_sync_pass(spa);
1894 ASSERT(zio->io_txg == spa_syncing_txg(spa));
1895 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1896 ASSERT(!BP_GET_DEDUP(bp));
1898 if (pass >= zfs_sync_pass_dont_compress)
1899 compress = ZIO_COMPRESS_OFF;
1901 /* Make sure someone doesn't change their mind on overwrites */
1902 ASSERT(BP_IS_EMBEDDED(bp) || BP_IS_GANG(bp) ||
1903 MIN(zp->zp_copies, spa_max_replication(spa))
1904 == BP_GET_NDVAS(bp));
1907 /* If it's a compressed write that is not raw, compress the buffer. */
1908 if (compress != ZIO_COMPRESS_OFF &&
1909 !(zio->io_flags & ZIO_FLAG_RAW_COMPRESS)) {
1910 abd_t *cabd = NULL;
1911 if (abd_cmp_zero(zio->io_abd, lsize) == 0)
1912 psize = 0;
1913 else if (compress == ZIO_COMPRESS_EMPTY)
1914 psize = lsize;
1915 else
1916 psize = zio_compress_data(compress, zio->io_abd, &cabd,
1917 lsize,
1918 zio_get_compression_max_size(compress,
1919 spa->spa_gcd_alloc, spa->spa_min_alloc, lsize),
1920 zp->zp_complevel);
1921 if (psize == 0) {
1922 compress = ZIO_COMPRESS_OFF;
1923 } else if (psize >= lsize) {
1924 compress = ZIO_COMPRESS_OFF;
1925 if (cabd != NULL)
1926 abd_free(cabd);
1927 } else if (!zp->zp_dedup && !zp->zp_encrypt &&
1928 psize <= BPE_PAYLOAD_SIZE &&
1929 zp->zp_level == 0 && !DMU_OT_HAS_FILL(zp->zp_type) &&
1930 spa_feature_is_enabled(spa, SPA_FEATURE_EMBEDDED_DATA)) {
1931 void *cbuf = abd_borrow_buf_copy(cabd, lsize);
1932 encode_embedded_bp_compressed(bp,
1933 cbuf, compress, lsize, psize);
1934 BPE_SET_ETYPE(bp, BP_EMBEDDED_TYPE_DATA);
1935 BP_SET_TYPE(bp, zio->io_prop.zp_type);
1936 BP_SET_LEVEL(bp, zio->io_prop.zp_level);
1937 abd_return_buf(cabd, cbuf, lsize);
1938 abd_free(cabd);
1939 BP_SET_LOGICAL_BIRTH(bp, zio->io_txg);
1940 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1941 ASSERT(spa_feature_is_active(spa,
1942 SPA_FEATURE_EMBEDDED_DATA));
1943 return (zio);
1944 } else {
1946 * Round compressed size up to the minimum allocation
1947 * size of the smallest-ashift device, and zero the
1948 * tail. This ensures that the compressed size of the
1949 * BP (and thus compressratio property) are correct,
1950 * in that we charge for the padding used to fill out
1951 * the last sector.
1953 size_t rounded = (size_t)zio_roundup_alloc_size(spa,
1954 psize);
1955 if (rounded >= lsize) {
1956 compress = ZIO_COMPRESS_OFF;
1957 abd_free(cabd);
1958 psize = lsize;
1959 } else {
1960 abd_zero_off(cabd, psize, rounded - psize);
1961 psize = rounded;
1962 zio_push_transform(zio, cabd,
1963 psize, lsize, NULL);
1968 * We were unable to handle this as an override bp, treat
1969 * it as a regular write I/O.
1971 zio->io_bp_override = NULL;
1972 *bp = zio->io_bp_orig;
1973 zio->io_pipeline = zio->io_orig_pipeline;
1975 } else if ((zio->io_flags & ZIO_FLAG_RAW_ENCRYPT) != 0 &&
1976 zp->zp_type == DMU_OT_DNODE) {
1978 * The DMU actually relies on the zio layer's compression
1979 * to free metadnode blocks that have had all contained
1980 * dnodes freed. As a result, even when doing a raw
1981 * receive, we must check whether the block can be compressed
1982 * to a hole.
1984 if (abd_cmp_zero(zio->io_abd, lsize) == 0) {
1985 psize = 0;
1986 compress = ZIO_COMPRESS_OFF;
1987 } else {
1988 psize = lsize;
1990 } else if (zio->io_flags & ZIO_FLAG_RAW_COMPRESS &&
1991 !(zio->io_flags & ZIO_FLAG_RAW_ENCRYPT)) {
1993 * If we are raw receiving an encrypted dataset we should not
1994 * take this codepath because it will change the on-disk block
1995 * and decryption will fail.
1997 size_t rounded = MIN((size_t)zio_roundup_alloc_size(spa, psize),
1998 lsize);
2000 if (rounded != psize) {
2001 abd_t *cdata = abd_alloc_linear(rounded, B_TRUE);
2002 abd_zero_off(cdata, psize, rounded - psize);
2003 abd_copy_off(cdata, zio->io_abd, 0, 0, psize);
2004 psize = rounded;
2005 zio_push_transform(zio, cdata,
2006 psize, rounded, NULL);
2008 } else {
2009 ASSERT3U(psize, !=, 0);
2013 * The final pass of spa_sync() must be all rewrites, but the first
2014 * few passes offer a trade-off: allocating blocks defers convergence,
2015 * but newly allocated blocks are sequential, so they can be written
2016 * to disk faster. Therefore, we allow the first few passes of
2017 * spa_sync() to allocate new blocks, but force rewrites after that.
2018 * There should only be a handful of blocks after pass 1 in any case.
2020 if (!BP_IS_HOLE(bp) && BP_GET_LOGICAL_BIRTH(bp) == zio->io_txg &&
2021 BP_GET_PSIZE(bp) == psize &&
2022 pass >= zfs_sync_pass_rewrite) {
2023 VERIFY3U(psize, !=, 0);
2024 enum zio_stage gang_stages = zio->io_pipeline & ZIO_GANG_STAGES;
2026 zio->io_pipeline = ZIO_REWRITE_PIPELINE | gang_stages;
2027 zio->io_flags |= ZIO_FLAG_IO_REWRITE;
2028 } else {
2029 BP_ZERO(bp);
2030 zio->io_pipeline = ZIO_WRITE_PIPELINE;
2033 if (psize == 0) {
2034 if (BP_GET_LOGICAL_BIRTH(&zio->io_bp_orig) != 0 &&
2035 spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) {
2036 BP_SET_LSIZE(bp, lsize);
2037 BP_SET_TYPE(bp, zp->zp_type);
2038 BP_SET_LEVEL(bp, zp->zp_level);
2039 BP_SET_BIRTH(bp, zio->io_txg, 0);
2041 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2042 } else {
2043 ASSERT(zp->zp_checksum != ZIO_CHECKSUM_GANG_HEADER);
2044 BP_SET_LSIZE(bp, lsize);
2045 BP_SET_TYPE(bp, zp->zp_type);
2046 BP_SET_LEVEL(bp, zp->zp_level);
2047 BP_SET_PSIZE(bp, psize);
2048 BP_SET_COMPRESS(bp, compress);
2049 BP_SET_CHECKSUM(bp, zp->zp_checksum);
2050 BP_SET_DEDUP(bp, zp->zp_dedup);
2051 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
2052 if (zp->zp_dedup) {
2053 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2054 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
2055 ASSERT(!zp->zp_encrypt ||
2056 DMU_OT_IS_ENCRYPTED(zp->zp_type));
2057 zio->io_pipeline = ZIO_DDT_WRITE_PIPELINE;
2059 if (zp->zp_nopwrite) {
2060 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2061 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
2062 zio->io_pipeline |= ZIO_STAGE_NOP_WRITE;
2065 return (zio);
2068 static zio_t *
2069 zio_free_bp_init(zio_t *zio)
2071 blkptr_t *bp = zio->io_bp;
2073 if (zio->io_child_type == ZIO_CHILD_LOGICAL) {
2074 if (BP_GET_DEDUP(bp))
2075 zio->io_pipeline = ZIO_DDT_FREE_PIPELINE;
2078 ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy);
2080 return (zio);
2084 * ==========================================================================
2085 * Execute the I/O pipeline
2086 * ==========================================================================
2089 static void
2090 zio_taskq_dispatch(zio_t *zio, zio_taskq_type_t q, boolean_t cutinline)
2092 spa_t *spa = zio->io_spa;
2093 zio_type_t t = zio->io_type;
2096 * If we're a config writer or a probe, the normal issue and
2097 * interrupt threads may all be blocked waiting for the config lock.
2098 * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL.
2100 if (zio->io_flags & (ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_PROBE))
2101 t = ZIO_TYPE_NULL;
2104 * A similar issue exists for the L2ARC write thread until L2ARC 2.0.
2106 if (t == ZIO_TYPE_WRITE && zio->io_vd && zio->io_vd->vdev_aux)
2107 t = ZIO_TYPE_NULL;
2110 * If this is a high priority I/O, then use the high priority taskq if
2111 * available or cut the line otherwise.
2113 if (zio->io_priority == ZIO_PRIORITY_SYNC_WRITE) {
2114 if (spa->spa_zio_taskq[t][q + 1].stqs_count != 0)
2115 q++;
2116 else
2117 cutinline = B_TRUE;
2120 ASSERT3U(q, <, ZIO_TASKQ_TYPES);
2122 spa_taskq_dispatch(spa, t, q, zio_execute, zio, cutinline);
2125 static boolean_t
2126 zio_taskq_member(zio_t *zio, zio_taskq_type_t q)
2128 spa_t *spa = zio->io_spa;
2130 taskq_t *tq = taskq_of_curthread();
2132 for (zio_type_t t = 0; t < ZIO_TYPES; t++) {
2133 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
2134 uint_t i;
2135 for (i = 0; i < tqs->stqs_count; i++) {
2136 if (tqs->stqs_taskq[i] == tq)
2137 return (B_TRUE);
2141 return (B_FALSE);
2144 static zio_t *
2145 zio_issue_async(zio_t *zio)
2147 ASSERT((zio->io_type != ZIO_TYPE_WRITE) || ZIO_HAS_ALLOCATOR(zio));
2148 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
2149 return (NULL);
2152 void
2153 zio_interrupt(void *zio)
2155 zio_taskq_dispatch(zio, ZIO_TASKQ_INTERRUPT, B_FALSE);
2158 void
2159 zio_delay_interrupt(zio_t *zio)
2162 * The timeout_generic() function isn't defined in userspace, so
2163 * rather than trying to implement the function, the zio delay
2164 * functionality has been disabled for userspace builds.
2167 #ifdef _KERNEL
2169 * If io_target_timestamp is zero, then no delay has been registered
2170 * for this IO, thus jump to the end of this function and "skip" the
2171 * delay; issuing it directly to the zio layer.
2173 if (zio->io_target_timestamp != 0) {
2174 hrtime_t now = gethrtime();
2176 if (now >= zio->io_target_timestamp) {
2178 * This IO has already taken longer than the target
2179 * delay to complete, so we don't want to delay it
2180 * any longer; we "miss" the delay and issue it
2181 * directly to the zio layer. This is likely due to
2182 * the target latency being set to a value less than
2183 * the underlying hardware can satisfy (e.g. delay
2184 * set to 1ms, but the disks take 10ms to complete an
2185 * IO request).
2188 DTRACE_PROBE2(zio__delay__miss, zio_t *, zio,
2189 hrtime_t, now);
2191 zio_interrupt(zio);
2192 } else {
2193 taskqid_t tid;
2194 hrtime_t diff = zio->io_target_timestamp - now;
2195 int ticks = MAX(1, NSEC_TO_TICK(diff));
2196 clock_t expire_at_tick = ddi_get_lbolt() + ticks;
2198 DTRACE_PROBE3(zio__delay__hit, zio_t *, zio,
2199 hrtime_t, now, hrtime_t, diff);
2201 tid = taskq_dispatch_delay(system_taskq, zio_interrupt,
2202 zio, TQ_NOSLEEP, expire_at_tick);
2203 if (tid == TASKQID_INVALID) {
2205 * Couldn't allocate a task. Just finish the
2206 * zio without a delay.
2208 zio_interrupt(zio);
2211 return;
2213 #endif
2214 DTRACE_PROBE1(zio__delay__skip, zio_t *, zio);
2215 zio_interrupt(zio);
2218 static void
2219 zio_deadman_impl(zio_t *pio, int ziodepth)
2221 zio_t *cio, *cio_next;
2222 zio_link_t *zl = NULL;
2223 vdev_t *vd = pio->io_vd;
2225 if (zio_deadman_log_all || (vd != NULL && vd->vdev_ops->vdev_op_leaf)) {
2226 vdev_queue_t *vq = vd ? &vd->vdev_queue : NULL;
2227 zbookmark_phys_t *zb = &pio->io_bookmark;
2228 uint64_t delta = gethrtime() - pio->io_timestamp;
2229 uint64_t failmode = spa_get_deadman_failmode(pio->io_spa);
2231 zfs_dbgmsg("slow zio[%d]: zio=%px timestamp=%llu "
2232 "delta=%llu queued=%llu io=%llu "
2233 "path=%s "
2234 "last=%llu type=%d "
2235 "priority=%d flags=0x%llx stage=0x%x "
2236 "pipeline=0x%x pipeline-trace=0x%x "
2237 "objset=%llu object=%llu "
2238 "level=%llu blkid=%llu "
2239 "offset=%llu size=%llu "
2240 "error=%d",
2241 ziodepth, pio, pio->io_timestamp,
2242 (u_longlong_t)delta, pio->io_delta, pio->io_delay,
2243 vd ? vd->vdev_path : "NULL",
2244 vq ? vq->vq_io_complete_ts : 0, pio->io_type,
2245 pio->io_priority, (u_longlong_t)pio->io_flags,
2246 pio->io_stage, pio->io_pipeline, pio->io_pipeline_trace,
2247 (u_longlong_t)zb->zb_objset, (u_longlong_t)zb->zb_object,
2248 (u_longlong_t)zb->zb_level, (u_longlong_t)zb->zb_blkid,
2249 (u_longlong_t)pio->io_offset, (u_longlong_t)pio->io_size,
2250 pio->io_error);
2251 (void) zfs_ereport_post(FM_EREPORT_ZFS_DEADMAN,
2252 pio->io_spa, vd, zb, pio, 0);
2254 if (failmode == ZIO_FAILURE_MODE_CONTINUE &&
2255 taskq_empty_ent(&pio->io_tqent)) {
2256 zio_interrupt(pio);
2260 mutex_enter(&pio->io_lock);
2261 for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) {
2262 cio_next = zio_walk_children(pio, &zl);
2263 zio_deadman_impl(cio, ziodepth + 1);
2265 mutex_exit(&pio->io_lock);
2269 * Log the critical information describing this zio and all of its children
2270 * using the zfs_dbgmsg() interface then post deadman event for the ZED.
2272 void
2273 zio_deadman(zio_t *pio, const char *tag)
2275 spa_t *spa = pio->io_spa;
2276 char *name = spa_name(spa);
2278 if (!zfs_deadman_enabled || spa_suspended(spa))
2279 return;
2281 zio_deadman_impl(pio, 0);
2283 switch (spa_get_deadman_failmode(spa)) {
2284 case ZIO_FAILURE_MODE_WAIT:
2285 zfs_dbgmsg("%s waiting for hung I/O to pool '%s'", tag, name);
2286 break;
2288 case ZIO_FAILURE_MODE_CONTINUE:
2289 zfs_dbgmsg("%s restarting hung I/O for pool '%s'", tag, name);
2290 break;
2292 case ZIO_FAILURE_MODE_PANIC:
2293 fm_panic("%s determined I/O to pool '%s' is hung.", tag, name);
2294 break;
2299 * Execute the I/O pipeline until one of the following occurs:
2300 * (1) the I/O completes; (2) the pipeline stalls waiting for
2301 * dependent child I/Os; (3) the I/O issues, so we're waiting
2302 * for an I/O completion interrupt; (4) the I/O is delegated by
2303 * vdev-level caching or aggregation; (5) the I/O is deferred
2304 * due to vdev-level queueing; (6) the I/O is handed off to
2305 * another thread. In all cases, the pipeline stops whenever
2306 * there's no CPU work; it never burns a thread in cv_wait_io().
2308 * There's no locking on io_stage because there's no legitimate way
2309 * for multiple threads to be attempting to process the same I/O.
2311 static zio_pipe_stage_t *zio_pipeline[];
2314 * zio_execute() is a wrapper around the static function
2315 * __zio_execute() so that we can force __zio_execute() to be
2316 * inlined. This reduces stack overhead which is important
2317 * because __zio_execute() is called recursively in several zio
2318 * code paths. zio_execute() itself cannot be inlined because
2319 * it is externally visible.
2321 void
2322 zio_execute(void *zio)
2324 fstrans_cookie_t cookie;
2326 cookie = spl_fstrans_mark();
2327 __zio_execute(zio);
2328 spl_fstrans_unmark(cookie);
2332 * Used to determine if in the current context the stack is sized large
2333 * enough to allow zio_execute() to be called recursively. A minimum
2334 * stack size of 16K is required to avoid needing to re-dispatch the zio.
2336 static boolean_t
2337 zio_execute_stack_check(zio_t *zio)
2339 #if !defined(HAVE_LARGE_STACKS)
2340 dsl_pool_t *dp = spa_get_dsl(zio->io_spa);
2342 /* Executing in txg_sync_thread() context. */
2343 if (dp && curthread == dp->dp_tx.tx_sync_thread)
2344 return (B_TRUE);
2346 /* Pool initialization outside of zio_taskq context. */
2347 if (dp && spa_is_initializing(dp->dp_spa) &&
2348 !zio_taskq_member(zio, ZIO_TASKQ_ISSUE) &&
2349 !zio_taskq_member(zio, ZIO_TASKQ_ISSUE_HIGH))
2350 return (B_TRUE);
2351 #else
2352 (void) zio;
2353 #endif /* HAVE_LARGE_STACKS */
2355 return (B_FALSE);
2358 __attribute__((always_inline))
2359 static inline void
2360 __zio_execute(zio_t *zio)
2362 ASSERT3U(zio->io_queued_timestamp, >, 0);
2364 while (zio->io_stage < ZIO_STAGE_DONE) {
2365 enum zio_stage pipeline = zio->io_pipeline;
2366 enum zio_stage stage = zio->io_stage;
2368 zio->io_executor = curthread;
2370 ASSERT(!MUTEX_HELD(&zio->io_lock));
2371 ASSERT(ISP2(stage));
2372 ASSERT(zio->io_stall == NULL);
2374 do {
2375 stage <<= 1;
2376 } while ((stage & pipeline) == 0);
2378 ASSERT(stage <= ZIO_STAGE_DONE);
2381 * If we are in interrupt context and this pipeline stage
2382 * will grab a config lock that is held across I/O,
2383 * or may wait for an I/O that needs an interrupt thread
2384 * to complete, issue async to avoid deadlock.
2386 * For VDEV_IO_START, we cut in line so that the io will
2387 * be sent to disk promptly.
2389 if ((stage & ZIO_BLOCKING_STAGES) && zio->io_vd == NULL &&
2390 zio_taskq_member(zio, ZIO_TASKQ_INTERRUPT)) {
2391 boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ?
2392 zio_requeue_io_start_cut_in_line : B_FALSE;
2393 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut);
2394 return;
2398 * If the current context doesn't have large enough stacks
2399 * the zio must be issued asynchronously to prevent overflow.
2401 if (zio_execute_stack_check(zio)) {
2402 boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ?
2403 zio_requeue_io_start_cut_in_line : B_FALSE;
2404 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut);
2405 return;
2408 zio->io_stage = stage;
2409 zio->io_pipeline_trace |= zio->io_stage;
2412 * The zio pipeline stage returns the next zio to execute
2413 * (typically the same as this one), or NULL if we should
2414 * stop.
2416 zio = zio_pipeline[highbit64(stage) - 1](zio);
2418 if (zio == NULL)
2419 return;
2425 * ==========================================================================
2426 * Initiate I/O, either sync or async
2427 * ==========================================================================
2430 zio_wait(zio_t *zio)
2433 * Some routines, like zio_free_sync(), may return a NULL zio
2434 * to avoid the performance overhead of creating and then destroying
2435 * an unneeded zio. For the callers' simplicity, we accept a NULL
2436 * zio and ignore it.
2438 if (zio == NULL)
2439 return (0);
2441 long timeout = MSEC_TO_TICK(zfs_deadman_ziotime_ms);
2442 int error;
2444 ASSERT3S(zio->io_stage, ==, ZIO_STAGE_OPEN);
2445 ASSERT3P(zio->io_executor, ==, NULL);
2447 zio->io_waiter = curthread;
2448 ASSERT0(zio->io_queued_timestamp);
2449 zio->io_queued_timestamp = gethrtime();
2451 if (zio->io_type == ZIO_TYPE_WRITE) {
2452 spa_select_allocator(zio);
2454 __zio_execute(zio);
2456 mutex_enter(&zio->io_lock);
2457 while (zio->io_executor != NULL) {
2458 error = cv_timedwait_io(&zio->io_cv, &zio->io_lock,
2459 ddi_get_lbolt() + timeout);
2461 if (zfs_deadman_enabled && error == -1 &&
2462 gethrtime() - zio->io_queued_timestamp >
2463 spa_deadman_ziotime(zio->io_spa)) {
2464 mutex_exit(&zio->io_lock);
2465 timeout = MSEC_TO_TICK(zfs_deadman_checktime_ms);
2466 zio_deadman(zio, FTAG);
2467 mutex_enter(&zio->io_lock);
2470 mutex_exit(&zio->io_lock);
2472 error = zio->io_error;
2473 zio_destroy(zio);
2475 return (error);
2478 void
2479 zio_nowait(zio_t *zio)
2482 * See comment in zio_wait().
2484 if (zio == NULL)
2485 return;
2487 ASSERT3P(zio->io_executor, ==, NULL);
2489 if (zio->io_child_type == ZIO_CHILD_LOGICAL &&
2490 list_is_empty(&zio->io_parent_list)) {
2491 zio_t *pio;
2494 * This is a logical async I/O with no parent to wait for it.
2495 * We add it to the spa_async_root_zio "Godfather" I/O which
2496 * will ensure they complete prior to unloading the pool.
2498 spa_t *spa = zio->io_spa;
2499 pio = spa->spa_async_zio_root[CPU_SEQID_UNSTABLE];
2501 zio_add_child(pio, zio);
2504 ASSERT0(zio->io_queued_timestamp);
2505 zio->io_queued_timestamp = gethrtime();
2506 if (zio->io_type == ZIO_TYPE_WRITE) {
2507 spa_select_allocator(zio);
2509 __zio_execute(zio);
2513 * ==========================================================================
2514 * Reexecute, cancel, or suspend/resume failed I/O
2515 * ==========================================================================
2518 static void
2519 zio_reexecute(void *arg)
2521 zio_t *pio = arg;
2522 zio_t *cio, *cio_next, *gio;
2524 ASSERT(pio->io_child_type == ZIO_CHILD_LOGICAL);
2525 ASSERT(pio->io_orig_stage == ZIO_STAGE_OPEN);
2526 ASSERT(pio->io_gang_leader == NULL);
2527 ASSERT(pio->io_gang_tree == NULL);
2529 mutex_enter(&pio->io_lock);
2530 pio->io_flags = pio->io_orig_flags;
2531 pio->io_stage = pio->io_orig_stage;
2532 pio->io_pipeline = pio->io_orig_pipeline;
2533 pio->io_reexecute = 0;
2534 pio->io_flags |= ZIO_FLAG_REEXECUTED;
2535 pio->io_pipeline_trace = 0;
2536 pio->io_error = 0;
2537 pio->io_state[ZIO_WAIT_READY] = (pio->io_stage >= ZIO_STAGE_READY) ||
2538 (pio->io_pipeline & ZIO_STAGE_READY) == 0;
2539 pio->io_state[ZIO_WAIT_DONE] = (pio->io_stage >= ZIO_STAGE_DONE);
2540 zio_link_t *zl = NULL;
2541 while ((gio = zio_walk_parents(pio, &zl)) != NULL) {
2542 for (int w = 0; w < ZIO_WAIT_TYPES; w++) {
2543 gio->io_children[pio->io_child_type][w] +=
2544 !pio->io_state[w];
2547 for (int c = 0; c < ZIO_CHILD_TYPES; c++)
2548 pio->io_child_error[c] = 0;
2550 if (IO_IS_ALLOCATING(pio))
2551 BP_ZERO(pio->io_bp);
2554 * As we reexecute pio's children, new children could be created.
2555 * New children go to the head of pio's io_child_list, however,
2556 * so we will (correctly) not reexecute them. The key is that
2557 * the remainder of pio's io_child_list, from 'cio_next' onward,
2558 * cannot be affected by any side effects of reexecuting 'cio'.
2560 zl = NULL;
2561 for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) {
2562 cio_next = zio_walk_children(pio, &zl);
2563 mutex_exit(&pio->io_lock);
2564 zio_reexecute(cio);
2565 mutex_enter(&pio->io_lock);
2567 mutex_exit(&pio->io_lock);
2570 * Now that all children have been reexecuted, execute the parent.
2571 * We don't reexecute "The Godfather" I/O here as it's the
2572 * responsibility of the caller to wait on it.
2574 if (!(pio->io_flags & ZIO_FLAG_GODFATHER)) {
2575 pio->io_queued_timestamp = gethrtime();
2576 __zio_execute(pio);
2580 void
2581 zio_suspend(spa_t *spa, zio_t *zio, zio_suspend_reason_t reason)
2583 if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_PANIC)
2584 fm_panic("Pool '%s' has encountered an uncorrectable I/O "
2585 "failure and the failure mode property for this pool "
2586 "is set to panic.", spa_name(spa));
2588 if (reason != ZIO_SUSPEND_MMP) {
2589 cmn_err(CE_WARN, "Pool '%s' has encountered an uncorrectable "
2590 "I/O failure and has been suspended.", spa_name(spa));
2593 (void) zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE, spa, NULL,
2594 NULL, NULL, 0);
2596 mutex_enter(&spa->spa_suspend_lock);
2598 if (spa->spa_suspend_zio_root == NULL)
2599 spa->spa_suspend_zio_root = zio_root(spa, NULL, NULL,
2600 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
2601 ZIO_FLAG_GODFATHER);
2603 spa->spa_suspended = reason;
2605 if (zio != NULL) {
2606 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
2607 ASSERT(zio != spa->spa_suspend_zio_root);
2608 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2609 ASSERT(zio_unique_parent(zio) == NULL);
2610 ASSERT(zio->io_stage == ZIO_STAGE_DONE);
2611 zio_add_child(spa->spa_suspend_zio_root, zio);
2614 mutex_exit(&spa->spa_suspend_lock);
2618 zio_resume(spa_t *spa)
2620 zio_t *pio;
2623 * Reexecute all previously suspended i/o.
2625 mutex_enter(&spa->spa_suspend_lock);
2626 if (spa->spa_suspended != ZIO_SUSPEND_NONE)
2627 cmn_err(CE_WARN, "Pool '%s' was suspended and is being "
2628 "resumed. Failed I/O will be retried.",
2629 spa_name(spa));
2630 spa->spa_suspended = ZIO_SUSPEND_NONE;
2631 cv_broadcast(&spa->spa_suspend_cv);
2632 pio = spa->spa_suspend_zio_root;
2633 spa->spa_suspend_zio_root = NULL;
2634 mutex_exit(&spa->spa_suspend_lock);
2636 if (pio == NULL)
2637 return (0);
2639 zio_reexecute(pio);
2640 return (zio_wait(pio));
2643 void
2644 zio_resume_wait(spa_t *spa)
2646 mutex_enter(&spa->spa_suspend_lock);
2647 while (spa_suspended(spa))
2648 cv_wait(&spa->spa_suspend_cv, &spa->spa_suspend_lock);
2649 mutex_exit(&spa->spa_suspend_lock);
2653 * ==========================================================================
2654 * Gang blocks.
2656 * A gang block is a collection of small blocks that looks to the DMU
2657 * like one large block. When zio_dva_allocate() cannot find a block
2658 * of the requested size, due to either severe fragmentation or the pool
2659 * being nearly full, it calls zio_write_gang_block() to construct the
2660 * block from smaller fragments.
2662 * A gang block consists of a gang header (zio_gbh_phys_t) and up to
2663 * three (SPA_GBH_NBLKPTRS) gang members. The gang header is just like
2664 * an indirect block: it's an array of block pointers. It consumes
2665 * only one sector and hence is allocatable regardless of fragmentation.
2666 * The gang header's bps point to its gang members, which hold the data.
2668 * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg>
2669 * as the verifier to ensure uniqueness of the SHA256 checksum.
2670 * Critically, the gang block bp's blk_cksum is the checksum of the data,
2671 * not the gang header. This ensures that data block signatures (needed for
2672 * deduplication) are independent of how the block is physically stored.
2674 * Gang blocks can be nested: a gang member may itself be a gang block.
2675 * Thus every gang block is a tree in which root and all interior nodes are
2676 * gang headers, and the leaves are normal blocks that contain user data.
2677 * The root of the gang tree is called the gang leader.
2679 * To perform any operation (read, rewrite, free, claim) on a gang block,
2680 * zio_gang_assemble() first assembles the gang tree (minus data leaves)
2681 * in the io_gang_tree field of the original logical i/o by recursively
2682 * reading the gang leader and all gang headers below it. This yields
2683 * an in-core tree containing the contents of every gang header and the
2684 * bps for every constituent of the gang block.
2686 * With the gang tree now assembled, zio_gang_issue() just walks the gang tree
2687 * and invokes a callback on each bp. To free a gang block, zio_gang_issue()
2688 * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp.
2689 * zio_claim_gang() provides a similarly trivial wrapper for zio_claim().
2690 * zio_read_gang() is a wrapper around zio_read() that omits reading gang
2691 * headers, since we already have those in io_gang_tree. zio_rewrite_gang()
2692 * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite()
2693 * of the gang header plus zio_checksum_compute() of the data to update the
2694 * gang header's blk_cksum as described above.
2696 * The two-phase assemble/issue model solves the problem of partial failure --
2697 * what if you'd freed part of a gang block but then couldn't read the
2698 * gang header for another part? Assembling the entire gang tree first
2699 * ensures that all the necessary gang header I/O has succeeded before
2700 * starting the actual work of free, claim, or write. Once the gang tree
2701 * is assembled, free and claim are in-memory operations that cannot fail.
2703 * In the event that a gang write fails, zio_dva_unallocate() walks the
2704 * gang tree to immediately free (i.e. insert back into the space map)
2705 * everything we've allocated. This ensures that we don't get ENOSPC
2706 * errors during repeated suspend/resume cycles due to a flaky device.
2708 * Gang rewrites only happen during sync-to-convergence. If we can't assemble
2709 * the gang tree, we won't modify the block, so we can safely defer the free
2710 * (knowing that the block is still intact). If we *can* assemble the gang
2711 * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free
2712 * each constituent bp and we can allocate a new block on the next sync pass.
2714 * In all cases, the gang tree allows complete recovery from partial failure.
2715 * ==========================================================================
2718 static void
2719 zio_gang_issue_func_done(zio_t *zio)
2721 abd_free(zio->io_abd);
2724 static zio_t *
2725 zio_read_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
2726 uint64_t offset)
2728 if (gn != NULL)
2729 return (pio);
2731 return (zio_read(pio, pio->io_spa, bp, abd_get_offset(data, offset),
2732 BP_GET_PSIZE(bp), zio_gang_issue_func_done,
2733 NULL, pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
2734 &pio->io_bookmark));
2737 static zio_t *
2738 zio_rewrite_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
2739 uint64_t offset)
2741 zio_t *zio;
2743 if (gn != NULL) {
2744 abd_t *gbh_abd =
2745 abd_get_from_buf(gn->gn_gbh, SPA_GANGBLOCKSIZE);
2746 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
2747 gbh_abd, SPA_GANGBLOCKSIZE, zio_gang_issue_func_done, NULL,
2748 pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
2749 &pio->io_bookmark);
2751 * As we rewrite each gang header, the pipeline will compute
2752 * a new gang block header checksum for it; but no one will
2753 * compute a new data checksum, so we do that here. The one
2754 * exception is the gang leader: the pipeline already computed
2755 * its data checksum because that stage precedes gang assembly.
2756 * (Presently, nothing actually uses interior data checksums;
2757 * this is just good hygiene.)
2759 if (gn != pio->io_gang_leader->io_gang_tree) {
2760 abd_t *buf = abd_get_offset(data, offset);
2762 zio_checksum_compute(zio, BP_GET_CHECKSUM(bp),
2763 buf, BP_GET_PSIZE(bp));
2765 abd_free(buf);
2768 * If we are here to damage data for testing purposes,
2769 * leave the GBH alone so that we can detect the damage.
2771 if (pio->io_gang_leader->io_flags & ZIO_FLAG_INDUCE_DAMAGE)
2772 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
2773 } else {
2774 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
2775 abd_get_offset(data, offset), BP_GET_PSIZE(bp),
2776 zio_gang_issue_func_done, NULL, pio->io_priority,
2777 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
2780 return (zio);
2783 static zio_t *
2784 zio_free_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
2785 uint64_t offset)
2787 (void) gn, (void) data, (void) offset;
2789 zio_t *zio = zio_free_sync(pio, pio->io_spa, pio->io_txg, bp,
2790 ZIO_GANG_CHILD_FLAGS(pio));
2791 if (zio == NULL) {
2792 zio = zio_null(pio, pio->io_spa,
2793 NULL, NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio));
2795 return (zio);
2798 static zio_t *
2799 zio_claim_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
2800 uint64_t offset)
2802 (void) gn, (void) data, (void) offset;
2803 return (zio_claim(pio, pio->io_spa, pio->io_txg, bp,
2804 NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio)));
2807 static zio_gang_issue_func_t *zio_gang_issue_func[ZIO_TYPES] = {
2808 NULL,
2809 zio_read_gang,
2810 zio_rewrite_gang,
2811 zio_free_gang,
2812 zio_claim_gang,
2813 NULL
2816 static void zio_gang_tree_assemble_done(zio_t *zio);
2818 static zio_gang_node_t *
2819 zio_gang_node_alloc(zio_gang_node_t **gnpp)
2821 zio_gang_node_t *gn;
2823 ASSERT(*gnpp == NULL);
2825 gn = kmem_zalloc(sizeof (*gn), KM_SLEEP);
2826 gn->gn_gbh = zio_buf_alloc(SPA_GANGBLOCKSIZE);
2827 *gnpp = gn;
2829 return (gn);
2832 static void
2833 zio_gang_node_free(zio_gang_node_t **gnpp)
2835 zio_gang_node_t *gn = *gnpp;
2837 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
2838 ASSERT(gn->gn_child[g] == NULL);
2840 zio_buf_free(gn->gn_gbh, SPA_GANGBLOCKSIZE);
2841 kmem_free(gn, sizeof (*gn));
2842 *gnpp = NULL;
2845 static void
2846 zio_gang_tree_free(zio_gang_node_t **gnpp)
2848 zio_gang_node_t *gn = *gnpp;
2850 if (gn == NULL)
2851 return;
2853 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
2854 zio_gang_tree_free(&gn->gn_child[g]);
2856 zio_gang_node_free(gnpp);
2859 static void
2860 zio_gang_tree_assemble(zio_t *gio, blkptr_t *bp, zio_gang_node_t **gnpp)
2862 zio_gang_node_t *gn = zio_gang_node_alloc(gnpp);
2863 abd_t *gbh_abd = abd_get_from_buf(gn->gn_gbh, SPA_GANGBLOCKSIZE);
2865 ASSERT(gio->io_gang_leader == gio);
2866 ASSERT(BP_IS_GANG(bp));
2868 zio_nowait(zio_read(gio, gio->io_spa, bp, gbh_abd, SPA_GANGBLOCKSIZE,
2869 zio_gang_tree_assemble_done, gn, gio->io_priority,
2870 ZIO_GANG_CHILD_FLAGS(gio), &gio->io_bookmark));
2873 static void
2874 zio_gang_tree_assemble_done(zio_t *zio)
2876 zio_t *gio = zio->io_gang_leader;
2877 zio_gang_node_t *gn = zio->io_private;
2878 blkptr_t *bp = zio->io_bp;
2880 ASSERT(gio == zio_unique_parent(zio));
2881 ASSERT(list_is_empty(&zio->io_child_list));
2883 if (zio->io_error)
2884 return;
2886 /* this ABD was created from a linear buf in zio_gang_tree_assemble */
2887 if (BP_SHOULD_BYTESWAP(bp))
2888 byteswap_uint64_array(abd_to_buf(zio->io_abd), zio->io_size);
2890 ASSERT3P(abd_to_buf(zio->io_abd), ==, gn->gn_gbh);
2891 ASSERT(zio->io_size == SPA_GANGBLOCKSIZE);
2892 ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
2894 abd_free(zio->io_abd);
2896 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
2897 blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
2898 if (!BP_IS_GANG(gbp))
2899 continue;
2900 zio_gang_tree_assemble(gio, gbp, &gn->gn_child[g]);
2904 static void
2905 zio_gang_tree_issue(zio_t *pio, zio_gang_node_t *gn, blkptr_t *bp, abd_t *data,
2906 uint64_t offset)
2908 zio_t *gio = pio->io_gang_leader;
2909 zio_t *zio;
2911 ASSERT(BP_IS_GANG(bp) == !!gn);
2912 ASSERT(BP_GET_CHECKSUM(bp) == BP_GET_CHECKSUM(gio->io_bp));
2913 ASSERT(BP_GET_LSIZE(bp) == BP_GET_PSIZE(bp) || gn == gio->io_gang_tree);
2916 * If you're a gang header, your data is in gn->gn_gbh.
2917 * If you're a gang member, your data is in 'data' and gn == NULL.
2919 zio = zio_gang_issue_func[gio->io_type](pio, bp, gn, data, offset);
2921 if (gn != NULL) {
2922 ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
2924 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
2925 blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
2926 if (BP_IS_HOLE(gbp))
2927 continue;
2928 zio_gang_tree_issue(zio, gn->gn_child[g], gbp, data,
2929 offset);
2930 offset += BP_GET_PSIZE(gbp);
2934 if (gn == gio->io_gang_tree)
2935 ASSERT3U(gio->io_size, ==, offset);
2937 if (zio != pio)
2938 zio_nowait(zio);
2941 static zio_t *
2942 zio_gang_assemble(zio_t *zio)
2944 blkptr_t *bp = zio->io_bp;
2946 ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == NULL);
2947 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2949 zio->io_gang_leader = zio;
2951 zio_gang_tree_assemble(zio, bp, &zio->io_gang_tree);
2953 return (zio);
2956 static zio_t *
2957 zio_gang_issue(zio_t *zio)
2959 blkptr_t *bp = zio->io_bp;
2961 if (zio_wait_for_children(zio, ZIO_CHILD_GANG_BIT, ZIO_WAIT_DONE)) {
2962 return (NULL);
2965 ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == zio);
2966 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2968 if (zio->io_child_error[ZIO_CHILD_GANG] == 0)
2969 zio_gang_tree_issue(zio, zio->io_gang_tree, bp, zio->io_abd,
2971 else
2972 zio_gang_tree_free(&zio->io_gang_tree);
2974 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2976 return (zio);
2979 static void
2980 zio_gang_inherit_allocator(zio_t *pio, zio_t *cio)
2982 cio->io_allocator = pio->io_allocator;
2985 static void
2986 zio_write_gang_member_ready(zio_t *zio)
2988 zio_t *pio = zio_unique_parent(zio);
2989 dva_t *cdva = zio->io_bp->blk_dva;
2990 dva_t *pdva = pio->io_bp->blk_dva;
2991 uint64_t asize;
2992 zio_t *gio __maybe_unused = zio->io_gang_leader;
2994 if (BP_IS_HOLE(zio->io_bp))
2995 return;
2997 ASSERT(BP_IS_HOLE(&zio->io_bp_orig));
2999 ASSERT(zio->io_child_type == ZIO_CHILD_GANG);
3000 ASSERT3U(zio->io_prop.zp_copies, ==, gio->io_prop.zp_copies);
3001 ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(zio->io_bp));
3002 ASSERT3U(pio->io_prop.zp_copies, <=, BP_GET_NDVAS(pio->io_bp));
3003 VERIFY3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp));
3005 mutex_enter(&pio->io_lock);
3006 for (int d = 0; d < BP_GET_NDVAS(zio->io_bp); d++) {
3007 ASSERT(DVA_GET_GANG(&pdva[d]));
3008 asize = DVA_GET_ASIZE(&pdva[d]);
3009 asize += DVA_GET_ASIZE(&cdva[d]);
3010 DVA_SET_ASIZE(&pdva[d], asize);
3012 mutex_exit(&pio->io_lock);
3015 static void
3016 zio_write_gang_done(zio_t *zio)
3019 * The io_abd field will be NULL for a zio with no data. The io_flags
3020 * will initially have the ZIO_FLAG_NODATA bit flag set, but we can't
3021 * check for it here as it is cleared in zio_ready.
3023 if (zio->io_abd != NULL)
3024 abd_free(zio->io_abd);
3027 static zio_t *
3028 zio_write_gang_block(zio_t *pio, metaslab_class_t *mc)
3030 spa_t *spa = pio->io_spa;
3031 blkptr_t *bp = pio->io_bp;
3032 zio_t *gio = pio->io_gang_leader;
3033 zio_t *zio;
3034 zio_gang_node_t *gn, **gnpp;
3035 zio_gbh_phys_t *gbh;
3036 abd_t *gbh_abd;
3037 uint64_t txg = pio->io_txg;
3038 uint64_t resid = pio->io_size;
3039 uint64_t lsize;
3040 int copies = gio->io_prop.zp_copies;
3041 zio_prop_t zp;
3042 int error;
3043 boolean_t has_data = !(pio->io_flags & ZIO_FLAG_NODATA);
3046 * If one copy was requested, store 2 copies of the GBH, so that we
3047 * can still traverse all the data (e.g. to free or scrub) even if a
3048 * block is damaged. Note that we can't store 3 copies of the GBH in
3049 * all cases, e.g. with encryption, which uses DVA[2] for the IV+salt.
3051 int gbh_copies = copies;
3052 if (gbh_copies == 1) {
3053 gbh_copies = MIN(2, spa_max_replication(spa));
3056 ASSERT(ZIO_HAS_ALLOCATOR(pio));
3057 int flags = METASLAB_HINTBP_FAVOR | METASLAB_GANG_HEADER;
3058 if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
3059 ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
3060 ASSERT(has_data);
3062 flags |= METASLAB_ASYNC_ALLOC;
3063 VERIFY(zfs_refcount_held(&mc->mc_allocator[pio->io_allocator].
3064 mca_alloc_slots, pio));
3067 * The logical zio has already placed a reservation for
3068 * 'copies' allocation slots but gang blocks may require
3069 * additional copies. These additional copies
3070 * (i.e. gbh_copies - copies) are guaranteed to succeed
3071 * since metaslab_class_throttle_reserve() always allows
3072 * additional reservations for gang blocks.
3074 VERIFY(metaslab_class_throttle_reserve(mc, gbh_copies - copies,
3075 pio->io_allocator, pio, flags));
3078 error = metaslab_alloc(spa, mc, SPA_GANGBLOCKSIZE,
3079 bp, gbh_copies, txg, pio == gio ? NULL : gio->io_bp, flags,
3080 &pio->io_alloc_list, pio, pio->io_allocator);
3081 if (error) {
3082 if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
3083 ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
3084 ASSERT(has_data);
3087 * If we failed to allocate the gang block header then
3088 * we remove any additional allocation reservations that
3089 * we placed here. The original reservation will
3090 * be removed when the logical I/O goes to the ready
3091 * stage.
3093 metaslab_class_throttle_unreserve(mc,
3094 gbh_copies - copies, pio->io_allocator, pio);
3097 pio->io_error = error;
3098 return (pio);
3101 if (pio == gio) {
3102 gnpp = &gio->io_gang_tree;
3103 } else {
3104 gnpp = pio->io_private;
3105 ASSERT(pio->io_ready == zio_write_gang_member_ready);
3108 gn = zio_gang_node_alloc(gnpp);
3109 gbh = gn->gn_gbh;
3110 memset(gbh, 0, SPA_GANGBLOCKSIZE);
3111 gbh_abd = abd_get_from_buf(gbh, SPA_GANGBLOCKSIZE);
3114 * Create the gang header.
3116 zio = zio_rewrite(pio, spa, txg, bp, gbh_abd, SPA_GANGBLOCKSIZE,
3117 zio_write_gang_done, NULL, pio->io_priority,
3118 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
3120 zio_gang_inherit_allocator(pio, zio);
3123 * Create and nowait the gang children.
3125 for (int g = 0; resid != 0; resid -= lsize, g++) {
3126 lsize = P2ROUNDUP(resid / (SPA_GBH_NBLKPTRS - g),
3127 SPA_MINBLOCKSIZE);
3128 ASSERT(lsize >= SPA_MINBLOCKSIZE && lsize <= resid);
3130 zp.zp_checksum = gio->io_prop.zp_checksum;
3131 zp.zp_compress = ZIO_COMPRESS_OFF;
3132 zp.zp_complevel = gio->io_prop.zp_complevel;
3133 zp.zp_type = zp.zp_storage_type = DMU_OT_NONE;
3134 zp.zp_level = 0;
3135 zp.zp_copies = gio->io_prop.zp_copies;
3136 zp.zp_dedup = B_FALSE;
3137 zp.zp_dedup_verify = B_FALSE;
3138 zp.zp_nopwrite = B_FALSE;
3139 zp.zp_encrypt = gio->io_prop.zp_encrypt;
3140 zp.zp_byteorder = gio->io_prop.zp_byteorder;
3141 zp.zp_direct_write = B_FALSE;
3142 memset(zp.zp_salt, 0, ZIO_DATA_SALT_LEN);
3143 memset(zp.zp_iv, 0, ZIO_DATA_IV_LEN);
3144 memset(zp.zp_mac, 0, ZIO_DATA_MAC_LEN);
3146 zio_t *cio = zio_write(zio, spa, txg, &gbh->zg_blkptr[g],
3147 has_data ? abd_get_offset(pio->io_abd, pio->io_size -
3148 resid) : NULL, lsize, lsize, &zp,
3149 zio_write_gang_member_ready, NULL,
3150 zio_write_gang_done, &gn->gn_child[g], pio->io_priority,
3151 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
3153 zio_gang_inherit_allocator(zio, cio);
3155 if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
3156 ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
3157 ASSERT(has_data);
3160 * Gang children won't throttle but we should
3161 * account for their work, so reserve an allocation
3162 * slot for them here.
3164 VERIFY(metaslab_class_throttle_reserve(mc,
3165 zp.zp_copies, cio->io_allocator, cio, flags));
3167 zio_nowait(cio);
3171 * Set pio's pipeline to just wait for zio to finish.
3173 pio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
3175 zio_nowait(zio);
3177 return (pio);
3181 * The zio_nop_write stage in the pipeline determines if allocating a
3182 * new bp is necessary. The nopwrite feature can handle writes in
3183 * either syncing or open context (i.e. zil writes) and as a result is
3184 * mutually exclusive with dedup.
3186 * By leveraging a cryptographically secure checksum, such as SHA256, we
3187 * can compare the checksums of the new data and the old to determine if
3188 * allocating a new block is required. Note that our requirements for
3189 * cryptographic strength are fairly weak: there can't be any accidental
3190 * hash collisions, but we don't need to be secure against intentional
3191 * (malicious) collisions. To trigger a nopwrite, you have to be able
3192 * to write the file to begin with, and triggering an incorrect (hash
3193 * collision) nopwrite is no worse than simply writing to the file.
3194 * That said, there are no known attacks against the checksum algorithms
3195 * used for nopwrite, assuming that the salt and the checksums
3196 * themselves remain secret.
3198 static zio_t *
3199 zio_nop_write(zio_t *zio)
3201 blkptr_t *bp = zio->io_bp;
3202 blkptr_t *bp_orig = &zio->io_bp_orig;
3203 zio_prop_t *zp = &zio->io_prop;
3205 ASSERT(BP_IS_HOLE(bp));
3206 ASSERT(BP_GET_LEVEL(bp) == 0);
3207 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
3208 ASSERT(zp->zp_nopwrite);
3209 ASSERT(!zp->zp_dedup);
3210 ASSERT(zio->io_bp_override == NULL);
3211 ASSERT(IO_IS_ALLOCATING(zio));
3214 * Check to see if the original bp and the new bp have matching
3215 * characteristics (i.e. same checksum, compression algorithms, etc).
3216 * If they don't then just continue with the pipeline which will
3217 * allocate a new bp.
3219 if (BP_IS_HOLE(bp_orig) ||
3220 !(zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_flags &
3221 ZCHECKSUM_FLAG_NOPWRITE) ||
3222 BP_IS_ENCRYPTED(bp) || BP_IS_ENCRYPTED(bp_orig) ||
3223 BP_GET_CHECKSUM(bp) != BP_GET_CHECKSUM(bp_orig) ||
3224 BP_GET_COMPRESS(bp) != BP_GET_COMPRESS(bp_orig) ||
3225 BP_GET_DEDUP(bp) != BP_GET_DEDUP(bp_orig) ||
3226 zp->zp_copies != BP_GET_NDVAS(bp_orig))
3227 return (zio);
3230 * If the checksums match then reset the pipeline so that we
3231 * avoid allocating a new bp and issuing any I/O.
3233 if (ZIO_CHECKSUM_EQUAL(bp->blk_cksum, bp_orig->blk_cksum)) {
3234 ASSERT(zio_checksum_table[zp->zp_checksum].ci_flags &
3235 ZCHECKSUM_FLAG_NOPWRITE);
3236 ASSERT3U(BP_GET_PSIZE(bp), ==, BP_GET_PSIZE(bp_orig));
3237 ASSERT3U(BP_GET_LSIZE(bp), ==, BP_GET_LSIZE(bp_orig));
3238 ASSERT(zp->zp_compress != ZIO_COMPRESS_OFF);
3239 ASSERT3U(bp->blk_prop, ==, bp_orig->blk_prop);
3242 * If we're overwriting a block that is currently on an
3243 * indirect vdev, then ignore the nopwrite request and
3244 * allow a new block to be allocated on a concrete vdev.
3246 spa_config_enter(zio->io_spa, SCL_VDEV, FTAG, RW_READER);
3247 for (int d = 0; d < BP_GET_NDVAS(bp_orig); d++) {
3248 vdev_t *tvd = vdev_lookup_top(zio->io_spa,
3249 DVA_GET_VDEV(&bp_orig->blk_dva[d]));
3250 if (tvd->vdev_ops == &vdev_indirect_ops) {
3251 spa_config_exit(zio->io_spa, SCL_VDEV, FTAG);
3252 return (zio);
3255 spa_config_exit(zio->io_spa, SCL_VDEV, FTAG);
3257 *bp = *bp_orig;
3258 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
3259 zio->io_flags |= ZIO_FLAG_NOPWRITE;
3262 return (zio);
3266 * ==========================================================================
3267 * Block Reference Table
3268 * ==========================================================================
3270 static zio_t *
3271 zio_brt_free(zio_t *zio)
3273 blkptr_t *bp;
3275 bp = zio->io_bp;
3277 if (BP_GET_LEVEL(bp) > 0 ||
3278 BP_IS_METADATA(bp) ||
3279 !brt_maybe_exists(zio->io_spa, bp)) {
3280 return (zio);
3283 if (!brt_entry_decref(zio->io_spa, bp)) {
3285 * This isn't the last reference, so we cannot free
3286 * the data yet.
3288 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
3291 return (zio);
3295 * ==========================================================================
3296 * Dedup
3297 * ==========================================================================
3299 static void
3300 zio_ddt_child_read_done(zio_t *zio)
3302 blkptr_t *bp = zio->io_bp;
3303 ddt_t *ddt;
3304 ddt_entry_t *dde = zio->io_private;
3305 zio_t *pio = zio_unique_parent(zio);
3307 mutex_enter(&pio->io_lock);
3308 ddt = ddt_select(zio->io_spa, bp);
3310 if (zio->io_error == 0) {
3311 ddt_phys_variant_t v = ddt_phys_select(ddt, dde, bp);
3312 /* this phys variant doesn't need repair */
3313 ddt_phys_clear(dde->dde_phys, v);
3316 if (zio->io_error == 0 && dde->dde_io->dde_repair_abd == NULL)
3317 dde->dde_io->dde_repair_abd = zio->io_abd;
3318 else
3319 abd_free(zio->io_abd);
3320 mutex_exit(&pio->io_lock);
3323 static zio_t *
3324 zio_ddt_read_start(zio_t *zio)
3326 blkptr_t *bp = zio->io_bp;
3328 ASSERT(BP_GET_DEDUP(bp));
3329 ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
3330 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3332 if (zio->io_child_error[ZIO_CHILD_DDT]) {
3333 ddt_t *ddt = ddt_select(zio->io_spa, bp);
3334 ddt_entry_t *dde = ddt_repair_start(ddt, bp);
3335 ddt_phys_variant_t v_self = ddt_phys_select(ddt, dde, bp);
3336 ddt_univ_phys_t *ddp = dde->dde_phys;
3337 blkptr_t blk;
3339 ASSERT(zio->io_vsd == NULL);
3340 zio->io_vsd = dde;
3342 if (v_self == DDT_PHYS_NONE)
3343 return (zio);
3345 /* issue I/O for the other copies */
3346 for (int p = 0; p < DDT_NPHYS(ddt); p++) {
3347 ddt_phys_variant_t v = DDT_PHYS_VARIANT(ddt, p);
3349 if (ddt_phys_birth(ddp, v) == 0 || v == v_self)
3350 continue;
3352 ddt_bp_create(ddt->ddt_checksum, &dde->dde_key,
3353 ddp, v, &blk);
3354 zio_nowait(zio_read(zio, zio->io_spa, &blk,
3355 abd_alloc_for_io(zio->io_size, B_TRUE),
3356 zio->io_size, zio_ddt_child_read_done, dde,
3357 zio->io_priority, ZIO_DDT_CHILD_FLAGS(zio) |
3358 ZIO_FLAG_DONT_PROPAGATE, &zio->io_bookmark));
3360 return (zio);
3363 zio_nowait(zio_read(zio, zio->io_spa, bp,
3364 zio->io_abd, zio->io_size, NULL, NULL, zio->io_priority,
3365 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark));
3367 return (zio);
3370 static zio_t *
3371 zio_ddt_read_done(zio_t *zio)
3373 blkptr_t *bp = zio->io_bp;
3375 if (zio_wait_for_children(zio, ZIO_CHILD_DDT_BIT, ZIO_WAIT_DONE)) {
3376 return (NULL);
3379 ASSERT(BP_GET_DEDUP(bp));
3380 ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
3381 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3383 if (zio->io_child_error[ZIO_CHILD_DDT]) {
3384 ddt_t *ddt = ddt_select(zio->io_spa, bp);
3385 ddt_entry_t *dde = zio->io_vsd;
3386 if (ddt == NULL) {
3387 ASSERT(spa_load_state(zio->io_spa) != SPA_LOAD_NONE);
3388 return (zio);
3390 if (dde == NULL) {
3391 zio->io_stage = ZIO_STAGE_DDT_READ_START >> 1;
3392 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
3393 return (NULL);
3395 if (dde->dde_io->dde_repair_abd != NULL) {
3396 abd_copy(zio->io_abd, dde->dde_io->dde_repair_abd,
3397 zio->io_size);
3398 zio->io_child_error[ZIO_CHILD_DDT] = 0;
3400 ddt_repair_done(ddt, dde);
3401 zio->io_vsd = NULL;
3404 ASSERT(zio->io_vsd == NULL);
3406 return (zio);
3409 static boolean_t
3410 zio_ddt_collision(zio_t *zio, ddt_t *ddt, ddt_entry_t *dde)
3412 spa_t *spa = zio->io_spa;
3413 boolean_t do_raw = !!(zio->io_flags & ZIO_FLAG_RAW);
3415 ASSERT(!(zio->io_bp_override && do_raw));
3418 * Note: we compare the original data, not the transformed data,
3419 * because when zio->io_bp is an override bp, we will not have
3420 * pushed the I/O transforms. That's an important optimization
3421 * because otherwise we'd compress/encrypt all dmu_sync() data twice.
3422 * However, we should never get a raw, override zio so in these
3423 * cases we can compare the io_abd directly. This is useful because
3424 * it allows us to do dedup verification even if we don't have access
3425 * to the original data (for instance, if the encryption keys aren't
3426 * loaded).
3429 for (int p = 0; p < DDT_NPHYS(ddt); p++) {
3430 if (DDT_PHYS_IS_DITTO(ddt, p))
3431 continue;
3433 if (dde->dde_io == NULL)
3434 continue;
3436 zio_t *lio = dde->dde_io->dde_lead_zio[p];
3437 if (lio == NULL)
3438 continue;
3440 if (do_raw)
3441 return (lio->io_size != zio->io_size ||
3442 abd_cmp(zio->io_abd, lio->io_abd) != 0);
3444 return (lio->io_orig_size != zio->io_orig_size ||
3445 abd_cmp(zio->io_orig_abd, lio->io_orig_abd) != 0);
3448 for (int p = 0; p < DDT_NPHYS(ddt); p++) {
3449 ddt_phys_variant_t v = DDT_PHYS_VARIANT(ddt, p);
3450 uint64_t phys_birth = ddt_phys_birth(dde->dde_phys, v);
3452 if (phys_birth != 0 && do_raw) {
3453 blkptr_t blk = *zio->io_bp;
3454 uint64_t psize;
3455 abd_t *tmpabd;
3456 int error;
3458 ddt_bp_fill(dde->dde_phys, v, &blk, phys_birth);
3459 psize = BP_GET_PSIZE(&blk);
3461 if (psize != zio->io_size)
3462 return (B_TRUE);
3464 ddt_exit(ddt);
3466 tmpabd = abd_alloc_for_io(psize, B_TRUE);
3468 error = zio_wait(zio_read(NULL, spa, &blk, tmpabd,
3469 psize, NULL, NULL, ZIO_PRIORITY_SYNC_READ,
3470 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
3471 ZIO_FLAG_RAW, &zio->io_bookmark));
3473 if (error == 0) {
3474 if (abd_cmp(tmpabd, zio->io_abd) != 0)
3475 error = SET_ERROR(ENOENT);
3478 abd_free(tmpabd);
3479 ddt_enter(ddt);
3480 return (error != 0);
3481 } else if (phys_birth != 0) {
3482 arc_buf_t *abuf = NULL;
3483 arc_flags_t aflags = ARC_FLAG_WAIT;
3484 blkptr_t blk = *zio->io_bp;
3485 int error;
3487 ddt_bp_fill(dde->dde_phys, v, &blk, phys_birth);
3489 if (BP_GET_LSIZE(&blk) != zio->io_orig_size)
3490 return (B_TRUE);
3492 ddt_exit(ddt);
3494 error = arc_read(NULL, spa, &blk,
3495 arc_getbuf_func, &abuf, ZIO_PRIORITY_SYNC_READ,
3496 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
3497 &aflags, &zio->io_bookmark);
3499 if (error == 0) {
3500 if (abd_cmp_buf(zio->io_orig_abd, abuf->b_data,
3501 zio->io_orig_size) != 0)
3502 error = SET_ERROR(ENOENT);
3503 arc_buf_destroy(abuf, &abuf);
3506 ddt_enter(ddt);
3507 return (error != 0);
3511 return (B_FALSE);
3514 static void
3515 zio_ddt_child_write_done(zio_t *zio)
3517 ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
3518 ddt_entry_t *dde = zio->io_private;
3520 zio_link_t *zl = NULL;
3521 ASSERT3P(zio_walk_parents(zio, &zl), !=, NULL);
3523 int p = DDT_PHYS_FOR_COPIES(ddt, zio->io_prop.zp_copies);
3524 ddt_phys_variant_t v = DDT_PHYS_VARIANT(ddt, p);
3525 ddt_univ_phys_t *ddp = dde->dde_phys;
3527 ddt_enter(ddt);
3529 /* we're the lead, so once we're done there's no one else outstanding */
3530 if (dde->dde_io->dde_lead_zio[p] == zio)
3531 dde->dde_io->dde_lead_zio[p] = NULL;
3533 ddt_univ_phys_t *orig = &dde->dde_io->dde_orig_phys;
3535 if (zio->io_error != 0) {
3537 * The write failed, so we're about to abort the entire IO
3538 * chain. We need to revert the entry back to what it was at
3539 * the last time it was successfully extended.
3541 ddt_phys_copy(ddp, orig, v);
3542 ddt_phys_clear(orig, v);
3544 ddt_exit(ddt);
3545 return;
3549 * We've successfully added new DVAs to the entry. Clear the saved
3550 * state or, if there's still outstanding IO, remember it so we can
3551 * revert to a known good state if that IO fails.
3553 if (dde->dde_io->dde_lead_zio[p] == NULL)
3554 ddt_phys_clear(orig, v);
3555 else
3556 ddt_phys_copy(orig, ddp, v);
3559 * Add references for all dedup writes that were waiting on the
3560 * physical one, skipping any other physical writes that are waiting.
3562 zio_t *pio;
3563 zl = NULL;
3564 while ((pio = zio_walk_parents(zio, &zl)) != NULL) {
3565 if (!(pio->io_flags & ZIO_FLAG_DDT_CHILD))
3566 ddt_phys_addref(ddp, v);
3569 ddt_exit(ddt);
3572 static void
3573 zio_ddt_child_write_ready(zio_t *zio)
3575 ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
3576 ddt_entry_t *dde = zio->io_private;
3578 zio_link_t *zl = NULL;
3579 ASSERT3P(zio_walk_parents(zio, &zl), !=, NULL);
3581 int p = DDT_PHYS_FOR_COPIES(ddt, zio->io_prop.zp_copies);
3582 ddt_phys_variant_t v = DDT_PHYS_VARIANT(ddt, p);
3584 if (zio->io_error != 0)
3585 return;
3587 ddt_enter(ddt);
3589 ddt_phys_extend(dde->dde_phys, v, zio->io_bp);
3591 zio_t *pio;
3592 zl = NULL;
3593 while ((pio = zio_walk_parents(zio, &zl)) != NULL) {
3594 if (!(pio->io_flags & ZIO_FLAG_DDT_CHILD))
3595 ddt_bp_fill(dde->dde_phys, v, pio->io_bp, zio->io_txg);
3598 ddt_exit(ddt);
3601 static zio_t *
3602 zio_ddt_write(zio_t *zio)
3604 spa_t *spa = zio->io_spa;
3605 blkptr_t *bp = zio->io_bp;
3606 uint64_t txg = zio->io_txg;
3607 zio_prop_t *zp = &zio->io_prop;
3608 ddt_t *ddt = ddt_select(spa, bp);
3609 ddt_entry_t *dde;
3611 ASSERT(BP_GET_DEDUP(bp));
3612 ASSERT(BP_GET_CHECKSUM(bp) == zp->zp_checksum);
3613 ASSERT(BP_IS_HOLE(bp) || zio->io_bp_override);
3614 ASSERT(!(zio->io_bp_override && (zio->io_flags & ZIO_FLAG_RAW)));
3616 * Deduplication will not take place for Direct I/O writes. The
3617 * ddt_tree will be emptied in syncing context. Direct I/O writes take
3618 * place in the open-context. Direct I/O write can not attempt to
3619 * modify the ddt_tree while issuing out a write.
3621 ASSERT3B(zio->io_prop.zp_direct_write, ==, B_FALSE);
3623 ddt_enter(ddt);
3624 dde = ddt_lookup(ddt, bp);
3625 if (dde == NULL) {
3626 /* DDT size is over its quota so no new entries */
3627 zp->zp_dedup = B_FALSE;
3628 BP_SET_DEDUP(bp, B_FALSE);
3629 if (zio->io_bp_override == NULL)
3630 zio->io_pipeline = ZIO_WRITE_PIPELINE;
3631 ddt_exit(ddt);
3632 return (zio);
3635 if (zp->zp_dedup_verify && zio_ddt_collision(zio, ddt, dde)) {
3637 * If we're using a weak checksum, upgrade to a strong checksum
3638 * and try again. If we're already using a strong checksum,
3639 * we can't resolve it, so just convert to an ordinary write.
3640 * (And automatically e-mail a paper to Nature?)
3642 if (!(zio_checksum_table[zp->zp_checksum].ci_flags &
3643 ZCHECKSUM_FLAG_DEDUP)) {
3644 zp->zp_checksum = spa_dedup_checksum(spa);
3645 zio_pop_transforms(zio);
3646 zio->io_stage = ZIO_STAGE_OPEN;
3647 BP_ZERO(bp);
3648 } else {
3649 zp->zp_dedup = B_FALSE;
3650 BP_SET_DEDUP(bp, B_FALSE);
3652 ASSERT(!BP_GET_DEDUP(bp));
3653 zio->io_pipeline = ZIO_WRITE_PIPELINE;
3654 ddt_exit(ddt);
3655 return (zio);
3658 int p = DDT_PHYS_FOR_COPIES(ddt, zp->zp_copies);
3659 ddt_phys_variant_t v = DDT_PHYS_VARIANT(ddt, p);
3660 ddt_univ_phys_t *ddp = dde->dde_phys;
3663 * In the common cases, at this point we have a regular BP with no
3664 * allocated DVAs, and the corresponding DDT entry for its checksum.
3665 * Our goal is to fill the BP with enough DVAs to satisfy its copies=
3666 * requirement.
3668 * One of three things needs to happen to fulfill this:
3670 * - if the DDT entry has enough DVAs to satisfy the BP, we just copy
3671 * them out of the entry and return;
3673 * - if the DDT entry has no DVAs (ie its brand new), then we have to
3674 * issue the write as normal so that DVAs can be allocated and the
3675 * data land on disk. We then copy the DVAs into the DDT entry on
3676 * return.
3678 * - if the DDT entry has some DVAs, but too few, we have to issue the
3679 * write, adjusted to have allocate fewer copies. When it returns, we
3680 * add the new DVAs to the DDT entry, and update the BP to have the
3681 * full amount it originally requested.
3683 * In all cases, if there's already a writing IO in flight, we need to
3684 * defer the action until after the write is done. If our action is to
3685 * write, we need to adjust our request for additional DVAs to match
3686 * what will be in the DDT entry after it completes. In this way every
3687 * IO can be guaranteed to recieve enough DVAs simply by joining the
3688 * end of the chain and letting the sequence play out.
3692 * Number of DVAs in the DDT entry. If the BP is encrypted we ignore
3693 * the third one as normal.
3695 int have_dvas = ddt_phys_dva_count(ddp, v, BP_IS_ENCRYPTED(bp));
3696 IMPLY(have_dvas == 0, ddt_phys_birth(ddp, v) == 0);
3698 /* Number of DVAs requested bya the IO. */
3699 uint8_t need_dvas = zp->zp_copies;
3702 * What we do next depends on whether or not there's IO outstanding that
3703 * will update this entry.
3705 if (dde->dde_io == NULL || dde->dde_io->dde_lead_zio[p] == NULL) {
3707 * No IO outstanding, so we only need to worry about ourselves.
3711 * Override BPs bring their own DVAs and their own problems.
3713 if (zio->io_bp_override) {
3715 * For a brand-new entry, all the work has been done
3716 * for us, and we can just fill it out from the provided
3717 * block and leave.
3719 if (have_dvas == 0) {
3720 ASSERT(BP_GET_LOGICAL_BIRTH(bp) == txg);
3721 ASSERT(BP_EQUAL(bp, zio->io_bp_override));
3722 ddt_phys_extend(ddp, v, bp);
3723 ddt_phys_addref(ddp, v);
3724 ddt_exit(ddt);
3725 return (zio);
3729 * If we already have this entry, then we want to treat
3730 * it like a regular write. To do this we just wipe
3731 * them out and proceed like a regular write.
3733 * Even if there are some DVAs in the entry, we still
3734 * have to clear them out. We can't use them to fill
3735 * out the dedup entry, as they are all referenced
3736 * together by a bp already on disk, and will be freed
3737 * as a group.
3739 BP_ZERO_DVAS(bp);
3740 BP_SET_BIRTH(bp, 0, 0);
3744 * If there are enough DVAs in the entry to service our request,
3745 * then we can just use them as-is.
3747 if (have_dvas >= need_dvas) {
3748 ddt_bp_fill(ddp, v, bp, txg);
3749 ddt_phys_addref(ddp, v);
3750 ddt_exit(ddt);
3751 return (zio);
3755 * Otherwise, we have to issue IO to fill the entry up to the
3756 * amount we need.
3758 need_dvas -= have_dvas;
3759 } else {
3761 * There's a write in-flight. If there's already enough DVAs on
3762 * the entry, then either there were already enough to start
3763 * with, or the in-flight IO is between READY and DONE, and so
3764 * has extended the entry with new DVAs. Either way, we don't
3765 * need to do anything, we can just slot in behind it.
3768 if (zio->io_bp_override) {
3770 * If there's a write out, then we're soon going to
3771 * have our own copies of this block, so clear out the
3772 * override block and treat it as a regular dedup
3773 * write. See comment above.
3775 BP_ZERO_DVAS(bp);
3776 BP_SET_BIRTH(bp, 0, 0);
3779 if (have_dvas >= need_dvas) {
3781 * A minor point: there might already be enough
3782 * committed DVAs in the entry to service our request,
3783 * but we don't know which are completed and which are
3784 * allocated but not yet written. In this case, should
3785 * the IO for the new DVAs fail, we will be on the end
3786 * of the IO chain and will also recieve an error, even
3787 * though our request could have been serviced.
3789 * This is an extremely rare case, as it requires the
3790 * original block to be copied with a request for a
3791 * larger number of DVAs, then copied again requesting
3792 * the same (or already fulfilled) number of DVAs while
3793 * the first request is active, and then that first
3794 * request errors. In return, the logic required to
3795 * catch and handle it is complex. For now, I'm just
3796 * not going to bother with it.
3800 * We always fill the bp here as we may have arrived
3801 * after the in-flight write has passed READY, and so
3802 * missed out.
3804 ddt_bp_fill(ddp, v, bp, txg);
3805 zio_add_child(zio, dde->dde_io->dde_lead_zio[p]);
3806 ddt_exit(ddt);
3807 return (zio);
3811 * There's not enough in the entry yet, so we need to look at
3812 * the write in-flight and see how many DVAs it will have once
3813 * it completes.
3815 * The in-flight write has potentially had its copies request
3816 * reduced (if we're filling out an existing entry), so we need
3817 * to reach in and get the original write to find out what it is
3818 * expecting.
3820 * Note that the parent of the lead zio will always have the
3821 * highest zp_copies of any zio in the chain, because ones that
3822 * can be serviced without additional IO are always added to
3823 * the back of the chain.
3825 zio_link_t *zl = NULL;
3826 zio_t *pio =
3827 zio_walk_parents(dde->dde_io->dde_lead_zio[p], &zl);
3828 ASSERT(pio);
3829 uint8_t parent_dvas = pio->io_prop.zp_copies;
3831 if (parent_dvas >= need_dvas) {
3832 zio_add_child(zio, dde->dde_io->dde_lead_zio[p]);
3833 ddt_exit(ddt);
3834 return (zio);
3838 * Still not enough, so we will need to issue to get the
3839 * shortfall.
3841 need_dvas -= parent_dvas;
3845 * We need to write. We will create a new write with the copies
3846 * property adjusted to match the number of DVAs we need to need to
3847 * grow the DDT entry by to satisfy the request.
3849 zio_prop_t czp = *zp;
3850 czp.zp_copies = need_dvas;
3851 zio_t *cio = zio_write(zio, spa, txg, bp, zio->io_orig_abd,
3852 zio->io_orig_size, zio->io_orig_size, &czp,
3853 zio_ddt_child_write_ready, NULL,
3854 zio_ddt_child_write_done, dde, zio->io_priority,
3855 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
3857 zio_push_transform(cio, zio->io_abd, zio->io_size, 0, NULL);
3860 * We are the new lead zio, because our parent has the highest
3861 * zp_copies that has been requested for this entry so far.
3863 ddt_alloc_entry_io(dde);
3864 if (dde->dde_io->dde_lead_zio[p] == NULL) {
3866 * First time out, take a copy of the stable entry to revert
3867 * to if there's an error (see zio_ddt_child_write_done())
3869 ddt_phys_copy(&dde->dde_io->dde_orig_phys, dde->dde_phys, v);
3870 } else {
3872 * Make the existing chain our child, because it cannot
3873 * complete until we have.
3875 zio_add_child(cio, dde->dde_io->dde_lead_zio[p]);
3877 dde->dde_io->dde_lead_zio[p] = cio;
3879 ddt_exit(ddt);
3881 zio_nowait(cio);
3883 return (zio);
3886 static ddt_entry_t *freedde; /* for debugging */
3888 static zio_t *
3889 zio_ddt_free(zio_t *zio)
3891 spa_t *spa = zio->io_spa;
3892 blkptr_t *bp = zio->io_bp;
3893 ddt_t *ddt = ddt_select(spa, bp);
3894 ddt_entry_t *dde = NULL;
3896 ASSERT(BP_GET_DEDUP(bp));
3897 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3899 ddt_enter(ddt);
3900 freedde = dde = ddt_lookup(ddt, bp);
3901 if (dde) {
3902 ddt_phys_variant_t v = ddt_phys_select(ddt, dde, bp);
3903 if (v != DDT_PHYS_NONE)
3904 ddt_phys_decref(dde->dde_phys, v);
3906 ddt_exit(ddt);
3909 * When no entry was found, it must have been pruned,
3910 * so we can free it now instead of decrementing the
3911 * refcount in the DDT.
3913 if (!dde) {
3914 BP_SET_DEDUP(bp, 0);
3915 zio->io_pipeline |= ZIO_STAGE_DVA_FREE;
3918 return (zio);
3922 * ==========================================================================
3923 * Allocate and free blocks
3924 * ==========================================================================
3927 static zio_t *
3928 zio_io_to_allocate(spa_t *spa, int allocator)
3930 zio_t *zio;
3932 ASSERT(MUTEX_HELD(&spa->spa_allocs[allocator].spaa_lock));
3934 zio = avl_first(&spa->spa_allocs[allocator].spaa_tree);
3935 if (zio == NULL)
3936 return (NULL);
3938 ASSERT(IO_IS_ALLOCATING(zio));
3939 ASSERT(ZIO_HAS_ALLOCATOR(zio));
3942 * Try to place a reservation for this zio. If we're unable to
3943 * reserve then we throttle.
3945 ASSERT3U(zio->io_allocator, ==, allocator);
3946 if (!metaslab_class_throttle_reserve(zio->io_metaslab_class,
3947 zio->io_prop.zp_copies, allocator, zio, 0)) {
3948 return (NULL);
3951 avl_remove(&spa->spa_allocs[allocator].spaa_tree, zio);
3952 ASSERT3U(zio->io_stage, <, ZIO_STAGE_DVA_ALLOCATE);
3954 return (zio);
3957 static zio_t *
3958 zio_dva_throttle(zio_t *zio)
3960 spa_t *spa = zio->io_spa;
3961 zio_t *nio;
3962 metaslab_class_t *mc;
3964 /* locate an appropriate allocation class */
3965 mc = spa_preferred_class(spa, zio);
3967 if (zio->io_priority == ZIO_PRIORITY_SYNC_WRITE ||
3968 !mc->mc_alloc_throttle_enabled ||
3969 zio->io_child_type == ZIO_CHILD_GANG ||
3970 zio->io_flags & ZIO_FLAG_NODATA) {
3971 return (zio);
3974 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
3975 ASSERT(ZIO_HAS_ALLOCATOR(zio));
3976 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
3977 ASSERT3U(zio->io_queued_timestamp, >, 0);
3978 ASSERT(zio->io_stage == ZIO_STAGE_DVA_THROTTLE);
3980 int allocator = zio->io_allocator;
3981 zio->io_metaslab_class = mc;
3982 mutex_enter(&spa->spa_allocs[allocator].spaa_lock);
3983 avl_add(&spa->spa_allocs[allocator].spaa_tree, zio);
3984 nio = zio_io_to_allocate(spa, allocator);
3985 mutex_exit(&spa->spa_allocs[allocator].spaa_lock);
3986 return (nio);
3989 static void
3990 zio_allocate_dispatch(spa_t *spa, int allocator)
3992 zio_t *zio;
3994 mutex_enter(&spa->spa_allocs[allocator].spaa_lock);
3995 zio = zio_io_to_allocate(spa, allocator);
3996 mutex_exit(&spa->spa_allocs[allocator].spaa_lock);
3997 if (zio == NULL)
3998 return;
4000 ASSERT3U(zio->io_stage, ==, ZIO_STAGE_DVA_THROTTLE);
4001 ASSERT0(zio->io_error);
4002 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_TRUE);
4005 static zio_t *
4006 zio_dva_allocate(zio_t *zio)
4008 spa_t *spa = zio->io_spa;
4009 metaslab_class_t *mc;
4010 blkptr_t *bp = zio->io_bp;
4011 int error;
4012 int flags = 0;
4014 if (zio->io_gang_leader == NULL) {
4015 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
4016 zio->io_gang_leader = zio;
4019 ASSERT(BP_IS_HOLE(bp));
4020 ASSERT0(BP_GET_NDVAS(bp));
4021 ASSERT3U(zio->io_prop.zp_copies, >, 0);
4022 ASSERT3U(zio->io_prop.zp_copies, <=, spa_max_replication(spa));
4023 ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
4025 if (zio->io_flags & ZIO_FLAG_NODATA)
4026 flags |= METASLAB_DONT_THROTTLE;
4027 if (zio->io_flags & ZIO_FLAG_GANG_CHILD)
4028 flags |= METASLAB_GANG_CHILD;
4029 if (zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE)
4030 flags |= METASLAB_ASYNC_ALLOC;
4033 * if not already chosen, locate an appropriate allocation class
4035 mc = zio->io_metaslab_class;
4036 if (mc == NULL) {
4037 mc = spa_preferred_class(spa, zio);
4038 zio->io_metaslab_class = mc;
4042 * Try allocating the block in the usual metaslab class.
4043 * If that's full, allocate it in the normal class.
4044 * If that's full, allocate as a gang block,
4045 * and if all are full, the allocation fails (which shouldn't happen).
4047 * Note that we do not fall back on embedded slog (ZIL) space, to
4048 * preserve unfragmented slog space, which is critical for decent
4049 * sync write performance. If a log allocation fails, we will fall
4050 * back to spa_sync() which is abysmal for performance.
4052 ASSERT(ZIO_HAS_ALLOCATOR(zio));
4053 error = metaslab_alloc(spa, mc, zio->io_size, bp,
4054 zio->io_prop.zp_copies, zio->io_txg, NULL, flags,
4055 &zio->io_alloc_list, zio, zio->io_allocator);
4058 * Fallback to normal class when an alloc class is full
4060 if (error == ENOSPC && mc != spa_normal_class(spa)) {
4062 * When the dedup or special class is spilling into the normal
4063 * class, there can still be significant space available due
4064 * to deferred frees that are in-flight. We track the txg when
4065 * this occurred and back off adding new DDT entries for a few
4066 * txgs to allow the free blocks to be processed.
4068 if ((mc == spa_dedup_class(spa) || (spa_special_has_ddt(spa) &&
4069 mc == spa_special_class(spa))) &&
4070 spa->spa_dedup_class_full_txg != zio->io_txg) {
4071 spa->spa_dedup_class_full_txg = zio->io_txg;
4072 zfs_dbgmsg("%s[%d]: %s class spilling, req size %d, "
4073 "%llu allocated of %llu",
4074 spa_name(spa), (int)zio->io_txg,
4075 mc == spa_dedup_class(spa) ? "dedup" : "special",
4076 (int)zio->io_size,
4077 (u_longlong_t)metaslab_class_get_alloc(mc),
4078 (u_longlong_t)metaslab_class_get_space(mc));
4082 * If throttling, transfer reservation over to normal class.
4083 * The io_allocator slot can remain the same even though we
4084 * are switching classes.
4086 if (mc->mc_alloc_throttle_enabled &&
4087 (zio->io_flags & ZIO_FLAG_IO_ALLOCATING)) {
4088 metaslab_class_throttle_unreserve(mc,
4089 zio->io_prop.zp_copies, zio->io_allocator, zio);
4090 zio->io_flags &= ~ZIO_FLAG_IO_ALLOCATING;
4092 VERIFY(metaslab_class_throttle_reserve(
4093 spa_normal_class(spa),
4094 zio->io_prop.zp_copies, zio->io_allocator, zio,
4095 flags | METASLAB_MUST_RESERVE));
4097 zio->io_metaslab_class = mc = spa_normal_class(spa);
4098 if (zfs_flags & ZFS_DEBUG_METASLAB_ALLOC) {
4099 zfs_dbgmsg("%s: metaslab allocation failure, "
4100 "trying normal class: zio %px, size %llu, error %d",
4101 spa_name(spa), zio, (u_longlong_t)zio->io_size,
4102 error);
4105 error = metaslab_alloc(spa, mc, zio->io_size, bp,
4106 zio->io_prop.zp_copies, zio->io_txg, NULL, flags,
4107 &zio->io_alloc_list, zio, zio->io_allocator);
4110 if (error == ENOSPC && zio->io_size > SPA_MINBLOCKSIZE) {
4111 if (zfs_flags & ZFS_DEBUG_METASLAB_ALLOC) {
4112 zfs_dbgmsg("%s: metaslab allocation failure, "
4113 "trying ganging: zio %px, size %llu, error %d",
4114 spa_name(spa), zio, (u_longlong_t)zio->io_size,
4115 error);
4117 return (zio_write_gang_block(zio, mc));
4119 if (error != 0) {
4120 if (error != ENOSPC ||
4121 (zfs_flags & ZFS_DEBUG_METASLAB_ALLOC)) {
4122 zfs_dbgmsg("%s: metaslab allocation failure: zio %px, "
4123 "size %llu, error %d",
4124 spa_name(spa), zio, (u_longlong_t)zio->io_size,
4125 error);
4127 zio->io_error = error;
4130 return (zio);
4133 static zio_t *
4134 zio_dva_free(zio_t *zio)
4136 metaslab_free(zio->io_spa, zio->io_bp, zio->io_txg, B_FALSE);
4138 return (zio);
4141 static zio_t *
4142 zio_dva_claim(zio_t *zio)
4144 int error;
4146 error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg);
4147 if (error)
4148 zio->io_error = error;
4150 return (zio);
4154 * Undo an allocation. This is used by zio_done() when an I/O fails
4155 * and we want to give back the block we just allocated.
4156 * This handles both normal blocks and gang blocks.
4158 static void
4159 zio_dva_unallocate(zio_t *zio, zio_gang_node_t *gn, blkptr_t *bp)
4161 ASSERT(BP_GET_LOGICAL_BIRTH(bp) == zio->io_txg || BP_IS_HOLE(bp));
4162 ASSERT(zio->io_bp_override == NULL);
4164 if (!BP_IS_HOLE(bp)) {
4165 metaslab_free(zio->io_spa, bp, BP_GET_LOGICAL_BIRTH(bp),
4166 B_TRUE);
4169 if (gn != NULL) {
4170 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
4171 zio_dva_unallocate(zio, gn->gn_child[g],
4172 &gn->gn_gbh->zg_blkptr[g]);
4178 * Try to allocate an intent log block. Return 0 on success, errno on failure.
4181 zio_alloc_zil(spa_t *spa, objset_t *os, uint64_t txg, blkptr_t *new_bp,
4182 uint64_t size, boolean_t *slog)
4184 int error = 1;
4185 zio_alloc_list_t io_alloc_list;
4187 ASSERT(txg > spa_syncing_txg(spa));
4189 metaslab_trace_init(&io_alloc_list);
4192 * Block pointer fields are useful to metaslabs for stats and debugging.
4193 * Fill in the obvious ones before calling into metaslab_alloc().
4195 BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
4196 BP_SET_PSIZE(new_bp, size);
4197 BP_SET_LEVEL(new_bp, 0);
4200 * When allocating a zil block, we don't have information about
4201 * the final destination of the block except the objset it's part
4202 * of, so we just hash the objset ID to pick the allocator to get
4203 * some parallelism.
4205 int flags = METASLAB_ZIL;
4206 int allocator = (uint_t)cityhash1(os->os_dsl_dataset->ds_object)
4207 % spa->spa_alloc_count;
4208 error = metaslab_alloc(spa, spa_log_class(spa), size, new_bp, 1,
4209 txg, NULL, flags, &io_alloc_list, NULL, allocator);
4210 *slog = (error == 0);
4211 if (error != 0) {
4212 error = metaslab_alloc(spa, spa_embedded_log_class(spa), size,
4213 new_bp, 1, txg, NULL, flags,
4214 &io_alloc_list, NULL, allocator);
4216 if (error != 0) {
4217 error = metaslab_alloc(spa, spa_normal_class(spa), size,
4218 new_bp, 1, txg, NULL, flags,
4219 &io_alloc_list, NULL, allocator);
4221 metaslab_trace_fini(&io_alloc_list);
4223 if (error == 0) {
4224 BP_SET_LSIZE(new_bp, size);
4225 BP_SET_PSIZE(new_bp, size);
4226 BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF);
4227 BP_SET_CHECKSUM(new_bp,
4228 spa_version(spa) >= SPA_VERSION_SLIM_ZIL
4229 ? ZIO_CHECKSUM_ZILOG2 : ZIO_CHECKSUM_ZILOG);
4230 BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
4231 BP_SET_LEVEL(new_bp, 0);
4232 BP_SET_DEDUP(new_bp, 0);
4233 BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER);
4236 * encrypted blocks will require an IV and salt. We generate
4237 * these now since we will not be rewriting the bp at
4238 * rewrite time.
4240 if (os->os_encrypted) {
4241 uint8_t iv[ZIO_DATA_IV_LEN];
4242 uint8_t salt[ZIO_DATA_SALT_LEN];
4244 BP_SET_CRYPT(new_bp, B_TRUE);
4245 VERIFY0(spa_crypt_get_salt(spa,
4246 dmu_objset_id(os), salt));
4247 VERIFY0(zio_crypt_generate_iv(iv));
4249 zio_crypt_encode_params_bp(new_bp, salt, iv);
4251 } else {
4252 zfs_dbgmsg("%s: zil block allocation failure: "
4253 "size %llu, error %d", spa_name(spa), (u_longlong_t)size,
4254 error);
4257 return (error);
4261 * ==========================================================================
4262 * Read and write to physical devices
4263 * ==========================================================================
4267 * Issue an I/O to the underlying vdev. Typically the issue pipeline
4268 * stops after this stage and will resume upon I/O completion.
4269 * However, there are instances where the vdev layer may need to
4270 * continue the pipeline when an I/O was not issued. Since the I/O
4271 * that was sent to the vdev layer might be different than the one
4272 * currently active in the pipeline (see vdev_queue_io()), we explicitly
4273 * force the underlying vdev layers to call either zio_execute() or
4274 * zio_interrupt() to ensure that the pipeline continues with the correct I/O.
4276 static zio_t *
4277 zio_vdev_io_start(zio_t *zio)
4279 vdev_t *vd = zio->io_vd;
4280 uint64_t align;
4281 spa_t *spa = zio->io_spa;
4283 zio->io_delay = 0;
4285 ASSERT(zio->io_error == 0);
4286 ASSERT(zio->io_child_error[ZIO_CHILD_VDEV] == 0);
4288 if (vd == NULL) {
4289 if (!(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
4290 spa_config_enter(spa, SCL_ZIO, zio, RW_READER);
4293 * The mirror_ops handle multiple DVAs in a single BP.
4295 vdev_mirror_ops.vdev_op_io_start(zio);
4296 return (NULL);
4299 ASSERT3P(zio->io_logical, !=, zio);
4300 if (zio->io_type == ZIO_TYPE_WRITE) {
4301 ASSERT(spa->spa_trust_config);
4304 * Note: the code can handle other kinds of writes,
4305 * but we don't expect them.
4307 if (zio->io_vd->vdev_noalloc) {
4308 ASSERT(zio->io_flags &
4309 (ZIO_FLAG_PHYSICAL | ZIO_FLAG_SELF_HEAL |
4310 ZIO_FLAG_RESILVER | ZIO_FLAG_INDUCE_DAMAGE));
4314 align = 1ULL << vd->vdev_top->vdev_ashift;
4316 if (!(zio->io_flags & ZIO_FLAG_PHYSICAL) &&
4317 P2PHASE(zio->io_size, align) != 0) {
4318 /* Transform logical writes to be a full physical block size. */
4319 uint64_t asize = P2ROUNDUP(zio->io_size, align);
4320 abd_t *abuf = abd_alloc_sametype(zio->io_abd, asize);
4321 ASSERT(vd == vd->vdev_top);
4322 if (zio->io_type == ZIO_TYPE_WRITE) {
4323 abd_copy(abuf, zio->io_abd, zio->io_size);
4324 abd_zero_off(abuf, zio->io_size, asize - zio->io_size);
4326 zio_push_transform(zio, abuf, asize, asize, zio_subblock);
4330 * If this is not a physical io, make sure that it is properly aligned
4331 * before proceeding.
4333 if (!(zio->io_flags & ZIO_FLAG_PHYSICAL)) {
4334 ASSERT0(P2PHASE(zio->io_offset, align));
4335 ASSERT0(P2PHASE(zio->io_size, align));
4336 } else {
4338 * For physical writes, we allow 512b aligned writes and assume
4339 * the device will perform a read-modify-write as necessary.
4341 ASSERT0(P2PHASE(zio->io_offset, SPA_MINBLOCKSIZE));
4342 ASSERT0(P2PHASE(zio->io_size, SPA_MINBLOCKSIZE));
4345 VERIFY(zio->io_type != ZIO_TYPE_WRITE || spa_writeable(spa));
4348 * If this is a repair I/O, and there's no self-healing involved --
4349 * that is, we're just resilvering what we expect to resilver --
4350 * then don't do the I/O unless zio's txg is actually in vd's DTL.
4351 * This prevents spurious resilvering.
4353 * There are a few ways that we can end up creating these spurious
4354 * resilver i/os:
4356 * 1. A resilver i/o will be issued if any DVA in the BP has a
4357 * dirty DTL. The mirror code will issue resilver writes to
4358 * each DVA, including the one(s) that are not on vdevs with dirty
4359 * DTLs.
4361 * 2. With nested replication, which happens when we have a
4362 * "replacing" or "spare" vdev that's a child of a mirror or raidz.
4363 * For example, given mirror(replacing(A+B), C), it's likely that
4364 * only A is out of date (it's the new device). In this case, we'll
4365 * read from C, then use the data to resilver A+B -- but we don't
4366 * actually want to resilver B, just A. The top-level mirror has no
4367 * way to know this, so instead we just discard unnecessary repairs
4368 * as we work our way down the vdev tree.
4370 * 3. ZTEST also creates mirrors of mirrors, mirrors of raidz, etc.
4371 * The same logic applies to any form of nested replication: ditto
4372 * + mirror, RAID-Z + replacing, etc.
4374 * However, indirect vdevs point off to other vdevs which may have
4375 * DTL's, so we never bypass them. The child i/os on concrete vdevs
4376 * will be properly bypassed instead.
4378 * Leaf DTL_PARTIAL can be empty when a legitimate write comes from
4379 * a dRAID spare vdev. For example, when a dRAID spare is first
4380 * used, its spare blocks need to be written to but the leaf vdev's
4381 * of such blocks can have empty DTL_PARTIAL.
4383 * There seemed no clean way to allow such writes while bypassing
4384 * spurious ones. At this point, just avoid all bypassing for dRAID
4385 * for correctness.
4387 if ((zio->io_flags & ZIO_FLAG_IO_REPAIR) &&
4388 !(zio->io_flags & ZIO_FLAG_SELF_HEAL) &&
4389 zio->io_txg != 0 && /* not a delegated i/o */
4390 vd->vdev_ops != &vdev_indirect_ops &&
4391 vd->vdev_top->vdev_ops != &vdev_draid_ops &&
4392 !vdev_dtl_contains(vd, DTL_PARTIAL, zio->io_txg, 1)) {
4393 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
4394 zio_vdev_io_bypass(zio);
4395 return (zio);
4399 * Select the next best leaf I/O to process. Distributed spares are
4400 * excluded since they dispatch the I/O directly to a leaf vdev after
4401 * applying the dRAID mapping.
4403 if (vd->vdev_ops->vdev_op_leaf &&
4404 vd->vdev_ops != &vdev_draid_spare_ops &&
4405 (zio->io_type == ZIO_TYPE_READ ||
4406 zio->io_type == ZIO_TYPE_WRITE ||
4407 zio->io_type == ZIO_TYPE_TRIM)) {
4409 if (zio_handle_device_injection(vd, zio, ENOSYS) != 0) {
4411 * "no-op" injections return success, but do no actual
4412 * work. Just skip the remaining vdev stages.
4414 zio_vdev_io_bypass(zio);
4415 zio_interrupt(zio);
4416 return (NULL);
4419 if ((zio = vdev_queue_io(zio)) == NULL)
4420 return (NULL);
4422 if (!vdev_accessible(vd, zio)) {
4423 zio->io_error = SET_ERROR(ENXIO);
4424 zio_interrupt(zio);
4425 return (NULL);
4427 zio->io_delay = gethrtime();
4430 vd->vdev_ops->vdev_op_io_start(zio);
4431 return (NULL);
4434 static zio_t *
4435 zio_vdev_io_done(zio_t *zio)
4437 vdev_t *vd = zio->io_vd;
4438 vdev_ops_t *ops = vd ? vd->vdev_ops : &vdev_mirror_ops;
4439 boolean_t unexpected_error = B_FALSE;
4441 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV_BIT, ZIO_WAIT_DONE)) {
4442 return (NULL);
4445 ASSERT(zio->io_type == ZIO_TYPE_READ ||
4446 zio->io_type == ZIO_TYPE_WRITE ||
4447 zio->io_type == ZIO_TYPE_FLUSH ||
4448 zio->io_type == ZIO_TYPE_TRIM);
4450 if (zio->io_delay)
4451 zio->io_delay = gethrtime() - zio->io_delay;
4453 if (vd != NULL && vd->vdev_ops->vdev_op_leaf &&
4454 vd->vdev_ops != &vdev_draid_spare_ops) {
4455 if (zio->io_type != ZIO_TYPE_FLUSH)
4456 vdev_queue_io_done(zio);
4458 if (zio_injection_enabled && zio->io_error == 0)
4459 zio->io_error = zio_handle_device_injections(vd, zio,
4460 EIO, EILSEQ);
4462 if (zio_injection_enabled && zio->io_error == 0)
4463 zio->io_error = zio_handle_label_injection(zio, EIO);
4465 if (zio->io_error && zio->io_type != ZIO_TYPE_FLUSH &&
4466 zio->io_type != ZIO_TYPE_TRIM) {
4467 if (!vdev_accessible(vd, zio)) {
4468 zio->io_error = SET_ERROR(ENXIO);
4469 } else {
4470 unexpected_error = B_TRUE;
4475 ops->vdev_op_io_done(zio);
4477 if (unexpected_error && vd->vdev_remove_wanted == B_FALSE)
4478 VERIFY(vdev_probe(vd, zio) == NULL);
4480 return (zio);
4484 * This function is used to change the priority of an existing zio that is
4485 * currently in-flight. This is used by the arc to upgrade priority in the
4486 * event that a demand read is made for a block that is currently queued
4487 * as a scrub or async read IO. Otherwise, the high priority read request
4488 * would end up having to wait for the lower priority IO.
4490 void
4491 zio_change_priority(zio_t *pio, zio_priority_t priority)
4493 zio_t *cio, *cio_next;
4494 zio_link_t *zl = NULL;
4496 ASSERT3U(priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
4498 if (pio->io_vd != NULL && pio->io_vd->vdev_ops->vdev_op_leaf) {
4499 vdev_queue_change_io_priority(pio, priority);
4500 } else {
4501 pio->io_priority = priority;
4504 mutex_enter(&pio->io_lock);
4505 for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) {
4506 cio_next = zio_walk_children(pio, &zl);
4507 zio_change_priority(cio, priority);
4509 mutex_exit(&pio->io_lock);
4513 * For non-raidz ZIOs, we can just copy aside the bad data read from the
4514 * disk, and use that to finish the checksum ereport later.
4516 static void
4517 zio_vsd_default_cksum_finish(zio_cksum_report_t *zcr,
4518 const abd_t *good_buf)
4520 /* no processing needed */
4521 zfs_ereport_finish_checksum(zcr, good_buf, zcr->zcr_cbdata, B_FALSE);
4524 void
4525 zio_vsd_default_cksum_report(zio_t *zio, zio_cksum_report_t *zcr)
4527 void *abd = abd_alloc_sametype(zio->io_abd, zio->io_size);
4529 abd_copy(abd, zio->io_abd, zio->io_size);
4531 zcr->zcr_cbinfo = zio->io_size;
4532 zcr->zcr_cbdata = abd;
4533 zcr->zcr_finish = zio_vsd_default_cksum_finish;
4534 zcr->zcr_free = zio_abd_free;
4537 static zio_t *
4538 zio_vdev_io_assess(zio_t *zio)
4540 vdev_t *vd = zio->io_vd;
4542 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV_BIT, ZIO_WAIT_DONE)) {
4543 return (NULL);
4546 if (vd == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
4547 spa_config_exit(zio->io_spa, SCL_ZIO, zio);
4549 if (zio->io_vsd != NULL) {
4550 zio->io_vsd_ops->vsd_free(zio);
4551 zio->io_vsd = NULL;
4555 * If a Direct I/O operation has a checksum verify error then this I/O
4556 * should not attempt to be issued again.
4558 if (zio->io_flags & ZIO_FLAG_DIO_CHKSUM_ERR) {
4559 if (zio->io_type == ZIO_TYPE_WRITE) {
4560 ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_LOGICAL);
4561 ASSERT3U(zio->io_error, ==, EIO);
4563 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
4564 return (zio);
4567 if (zio_injection_enabled && zio->io_error == 0)
4568 zio->io_error = zio_handle_fault_injection(zio, EIO);
4571 * If the I/O failed, determine whether we should attempt to retry it.
4573 * On retry, we cut in line in the issue queue, since we don't want
4574 * compression/checksumming/etc. work to prevent our (cheap) IO reissue.
4576 if (zio->io_error && vd == NULL &&
4577 !(zio->io_flags & (ZIO_FLAG_DONT_RETRY | ZIO_FLAG_IO_RETRY))) {
4578 ASSERT(!(zio->io_flags & ZIO_FLAG_DONT_QUEUE)); /* not a leaf */
4579 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_BYPASS)); /* not a leaf */
4580 zio->io_error = 0;
4581 zio->io_flags |= ZIO_FLAG_IO_RETRY | ZIO_FLAG_DONT_AGGREGATE;
4582 zio->io_stage = ZIO_STAGE_VDEV_IO_START >> 1;
4583 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE,
4584 zio_requeue_io_start_cut_in_line);
4585 return (NULL);
4589 * If we got an error on a leaf device, convert it to ENXIO
4590 * if the device is not accessible at all.
4592 if (zio->io_error && vd != NULL && vd->vdev_ops->vdev_op_leaf &&
4593 !vdev_accessible(vd, zio))
4594 zio->io_error = SET_ERROR(ENXIO);
4597 * If we can't write to an interior vdev (mirror or RAID-Z),
4598 * set vdev_cant_write so that we stop trying to allocate from it.
4600 if (zio->io_error == ENXIO && zio->io_type == ZIO_TYPE_WRITE &&
4601 vd != NULL && !vd->vdev_ops->vdev_op_leaf) {
4602 vdev_dbgmsg(vd, "zio_vdev_io_assess(zio=%px) setting "
4603 "cant_write=TRUE due to write failure with ENXIO",
4604 zio);
4605 vd->vdev_cant_write = B_TRUE;
4609 * If a cache flush returns ENOTSUP or ENOTTY, we know that no future
4610 * attempts will ever succeed. In this case we set a persistent
4611 * boolean flag so that we don't bother with it in the future.
4613 if ((zio->io_error == ENOTSUP || zio->io_error == ENOTTY) &&
4614 zio->io_type == ZIO_TYPE_FLUSH && vd != NULL)
4615 vd->vdev_nowritecache = B_TRUE;
4617 if (zio->io_error)
4618 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
4620 return (zio);
4623 void
4624 zio_vdev_io_reissue(zio_t *zio)
4626 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
4627 ASSERT(zio->io_error == 0);
4629 zio->io_stage >>= 1;
4632 void
4633 zio_vdev_io_redone(zio_t *zio)
4635 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE);
4637 zio->io_stage >>= 1;
4640 void
4641 zio_vdev_io_bypass(zio_t *zio)
4643 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
4644 ASSERT(zio->io_error == 0);
4646 zio->io_flags |= ZIO_FLAG_IO_BYPASS;
4647 zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS >> 1;
4651 * ==========================================================================
4652 * Encrypt and store encryption parameters
4653 * ==========================================================================
4658 * This function is used for ZIO_STAGE_ENCRYPT. It is responsible for
4659 * managing the storage of encryption parameters and passing them to the
4660 * lower-level encryption functions.
4662 static zio_t *
4663 zio_encrypt(zio_t *zio)
4665 zio_prop_t *zp = &zio->io_prop;
4666 spa_t *spa = zio->io_spa;
4667 blkptr_t *bp = zio->io_bp;
4668 uint64_t psize = BP_GET_PSIZE(bp);
4669 uint64_t dsobj = zio->io_bookmark.zb_objset;
4670 dmu_object_type_t ot = BP_GET_TYPE(bp);
4671 void *enc_buf = NULL;
4672 abd_t *eabd = NULL;
4673 uint8_t salt[ZIO_DATA_SALT_LEN];
4674 uint8_t iv[ZIO_DATA_IV_LEN];
4675 uint8_t mac[ZIO_DATA_MAC_LEN];
4676 boolean_t no_crypt = B_FALSE;
4678 /* the root zio already encrypted the data */
4679 if (zio->io_child_type == ZIO_CHILD_GANG)
4680 return (zio);
4682 /* only ZIL blocks are re-encrypted on rewrite */
4683 if (!IO_IS_ALLOCATING(zio) && ot != DMU_OT_INTENT_LOG)
4684 return (zio);
4686 if (!(zp->zp_encrypt || BP_IS_ENCRYPTED(bp))) {
4687 BP_SET_CRYPT(bp, B_FALSE);
4688 return (zio);
4691 /* if we are doing raw encryption set the provided encryption params */
4692 if (zio->io_flags & ZIO_FLAG_RAW_ENCRYPT) {
4693 ASSERT0(BP_GET_LEVEL(bp));
4694 BP_SET_CRYPT(bp, B_TRUE);
4695 BP_SET_BYTEORDER(bp, zp->zp_byteorder);
4696 if (ot != DMU_OT_OBJSET)
4697 zio_crypt_encode_mac_bp(bp, zp->zp_mac);
4699 /* dnode blocks must be written out in the provided byteorder */
4700 if (zp->zp_byteorder != ZFS_HOST_BYTEORDER &&
4701 ot == DMU_OT_DNODE) {
4702 void *bswap_buf = zio_buf_alloc(psize);
4703 abd_t *babd = abd_get_from_buf(bswap_buf, psize);
4705 ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF);
4706 abd_copy_to_buf(bswap_buf, zio->io_abd, psize);
4707 dmu_ot_byteswap[DMU_OT_BYTESWAP(ot)].ob_func(bswap_buf,
4708 psize);
4710 abd_take_ownership_of_buf(babd, B_TRUE);
4711 zio_push_transform(zio, babd, psize, psize, NULL);
4714 if (DMU_OT_IS_ENCRYPTED(ot))
4715 zio_crypt_encode_params_bp(bp, zp->zp_salt, zp->zp_iv);
4716 return (zio);
4719 /* indirect blocks only maintain a cksum of the lower level MACs */
4720 if (BP_GET_LEVEL(bp) > 0) {
4721 BP_SET_CRYPT(bp, B_TRUE);
4722 VERIFY0(zio_crypt_do_indirect_mac_checksum_abd(B_TRUE,
4723 zio->io_orig_abd, BP_GET_LSIZE(bp), BP_SHOULD_BYTESWAP(bp),
4724 mac));
4725 zio_crypt_encode_mac_bp(bp, mac);
4726 return (zio);
4730 * Objset blocks are a special case since they have 2 256-bit MACs
4731 * embedded within them.
4733 if (ot == DMU_OT_OBJSET) {
4734 ASSERT0(DMU_OT_IS_ENCRYPTED(ot));
4735 ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF);
4736 BP_SET_CRYPT(bp, B_TRUE);
4737 VERIFY0(spa_do_crypt_objset_mac_abd(B_TRUE, spa, dsobj,
4738 zio->io_abd, psize, BP_SHOULD_BYTESWAP(bp)));
4739 return (zio);
4742 /* unencrypted object types are only authenticated with a MAC */
4743 if (!DMU_OT_IS_ENCRYPTED(ot)) {
4744 BP_SET_CRYPT(bp, B_TRUE);
4745 VERIFY0(spa_do_crypt_mac_abd(B_TRUE, spa, dsobj,
4746 zio->io_abd, psize, mac));
4747 zio_crypt_encode_mac_bp(bp, mac);
4748 return (zio);
4752 * Later passes of sync-to-convergence may decide to rewrite data
4753 * in place to avoid more disk reallocations. This presents a problem
4754 * for encryption because this constitutes rewriting the new data with
4755 * the same encryption key and IV. However, this only applies to blocks
4756 * in the MOS (particularly the spacemaps) and we do not encrypt the
4757 * MOS. We assert that the zio is allocating or an intent log write
4758 * to enforce this.
4760 ASSERT(IO_IS_ALLOCATING(zio) || ot == DMU_OT_INTENT_LOG);
4761 ASSERT(BP_GET_LEVEL(bp) == 0 || ot == DMU_OT_INTENT_LOG);
4762 ASSERT(spa_feature_is_active(spa, SPA_FEATURE_ENCRYPTION));
4763 ASSERT3U(psize, !=, 0);
4765 enc_buf = zio_buf_alloc(psize);
4766 eabd = abd_get_from_buf(enc_buf, psize);
4767 abd_take_ownership_of_buf(eabd, B_TRUE);
4770 * For an explanation of what encryption parameters are stored
4771 * where, see the block comment in zio_crypt.c.
4773 if (ot == DMU_OT_INTENT_LOG) {
4774 zio_crypt_decode_params_bp(bp, salt, iv);
4775 } else {
4776 BP_SET_CRYPT(bp, B_TRUE);
4779 /* Perform the encryption. This should not fail */
4780 VERIFY0(spa_do_crypt_abd(B_TRUE, spa, &zio->io_bookmark,
4781 BP_GET_TYPE(bp), BP_GET_DEDUP(bp), BP_SHOULD_BYTESWAP(bp),
4782 salt, iv, mac, psize, zio->io_abd, eabd, &no_crypt));
4784 /* encode encryption metadata into the bp */
4785 if (ot == DMU_OT_INTENT_LOG) {
4787 * ZIL blocks store the MAC in the embedded checksum, so the
4788 * transform must always be applied.
4790 zio_crypt_encode_mac_zil(enc_buf, mac);
4791 zio_push_transform(zio, eabd, psize, psize, NULL);
4792 } else {
4793 BP_SET_CRYPT(bp, B_TRUE);
4794 zio_crypt_encode_params_bp(bp, salt, iv);
4795 zio_crypt_encode_mac_bp(bp, mac);
4797 if (no_crypt) {
4798 ASSERT3U(ot, ==, DMU_OT_DNODE);
4799 abd_free(eabd);
4800 } else {
4801 zio_push_transform(zio, eabd, psize, psize, NULL);
4805 return (zio);
4809 * ==========================================================================
4810 * Generate and verify checksums
4811 * ==========================================================================
4813 static zio_t *
4814 zio_checksum_generate(zio_t *zio)
4816 blkptr_t *bp = zio->io_bp;
4817 enum zio_checksum checksum;
4819 if (bp == NULL) {
4821 * This is zio_write_phys().
4822 * We're either generating a label checksum, or none at all.
4824 checksum = zio->io_prop.zp_checksum;
4826 if (checksum == ZIO_CHECKSUM_OFF)
4827 return (zio);
4829 ASSERT(checksum == ZIO_CHECKSUM_LABEL);
4830 } else {
4831 if (BP_IS_GANG(bp) && zio->io_child_type == ZIO_CHILD_GANG) {
4832 ASSERT(!IO_IS_ALLOCATING(zio));
4833 checksum = ZIO_CHECKSUM_GANG_HEADER;
4834 } else {
4835 checksum = BP_GET_CHECKSUM(bp);
4839 zio_checksum_compute(zio, checksum, zio->io_abd, zio->io_size);
4841 return (zio);
4844 static zio_t *
4845 zio_checksum_verify(zio_t *zio)
4847 zio_bad_cksum_t info;
4848 blkptr_t *bp = zio->io_bp;
4849 int error;
4851 ASSERT(zio->io_vd != NULL);
4853 if (bp == NULL) {
4855 * This is zio_read_phys().
4856 * We're either verifying a label checksum, or nothing at all.
4858 if (zio->io_prop.zp_checksum == ZIO_CHECKSUM_OFF)
4859 return (zio);
4861 ASSERT3U(zio->io_prop.zp_checksum, ==, ZIO_CHECKSUM_LABEL);
4864 ASSERT0(zio->io_flags & ZIO_FLAG_DIO_CHKSUM_ERR);
4865 IMPLY(zio->io_flags & ZIO_FLAG_DIO_READ,
4866 !(zio->io_flags & ZIO_FLAG_SPECULATIVE));
4868 if ((error = zio_checksum_error(zio, &info)) != 0) {
4869 zio->io_error = error;
4870 if (error == ECKSUM &&
4871 !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
4872 if (zio->io_flags & ZIO_FLAG_DIO_READ) {
4873 zio->io_flags |= ZIO_FLAG_DIO_CHKSUM_ERR;
4874 zio_t *pio = zio_unique_parent(zio);
4876 * Any Direct I/O read that has a checksum
4877 * error must be treated as suspicous as the
4878 * contents of the buffer could be getting
4879 * manipulated while the I/O is taking place.
4881 * The checksum verify error will only be
4882 * reported here for disk and file VDEV's and
4883 * will be reported on those that the failure
4884 * occurred on. Other types of VDEV's report the
4885 * verify failure in their own code paths.
4887 if (pio->io_child_type == ZIO_CHILD_LOGICAL) {
4888 zio_dio_chksum_verify_error_report(zio);
4890 } else {
4891 mutex_enter(&zio->io_vd->vdev_stat_lock);
4892 zio->io_vd->vdev_stat.vs_checksum_errors++;
4893 mutex_exit(&zio->io_vd->vdev_stat_lock);
4894 (void) zfs_ereport_start_checksum(zio->io_spa,
4895 zio->io_vd, &zio->io_bookmark, zio,
4896 zio->io_offset, zio->io_size, &info);
4901 return (zio);
4904 static zio_t *
4905 zio_dio_checksum_verify(zio_t *zio)
4907 zio_t *pio = zio_unique_parent(zio);
4908 int error;
4910 ASSERT3P(zio->io_vd, !=, NULL);
4911 ASSERT3P(zio->io_bp, !=, NULL);
4912 ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
4913 ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE);
4914 ASSERT3B(pio->io_prop.zp_direct_write, ==, B_TRUE);
4915 ASSERT3U(pio->io_child_type, ==, ZIO_CHILD_LOGICAL);
4917 if (zfs_vdev_direct_write_verify == 0 || zio->io_error != 0)
4918 goto out;
4920 if ((error = zio_checksum_error(zio, NULL)) != 0) {
4921 zio->io_error = error;
4922 if (error == ECKSUM) {
4923 zio->io_flags |= ZIO_FLAG_DIO_CHKSUM_ERR;
4924 zio_dio_chksum_verify_error_report(zio);
4928 out:
4929 return (zio);
4934 * Called by RAID-Z to ensure we don't compute the checksum twice.
4936 void
4937 zio_checksum_verified(zio_t *zio)
4939 zio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
4943 * Report Direct I/O checksum verify error and create ZED event.
4945 void
4946 zio_dio_chksum_verify_error_report(zio_t *zio)
4948 ASSERT(zio->io_flags & ZIO_FLAG_DIO_CHKSUM_ERR);
4950 if (zio->io_child_type == ZIO_CHILD_LOGICAL)
4951 return;
4953 mutex_enter(&zio->io_vd->vdev_stat_lock);
4954 zio->io_vd->vdev_stat.vs_dio_verify_errors++;
4955 mutex_exit(&zio->io_vd->vdev_stat_lock);
4956 if (zio->io_type == ZIO_TYPE_WRITE) {
4958 * Convert checksum error for writes into EIO.
4960 zio->io_error = SET_ERROR(EIO);
4962 * Report dio_verify_wr ZED event.
4964 (void) zfs_ereport_post(FM_EREPORT_ZFS_DIO_VERIFY_WR,
4965 zio->io_spa, zio->io_vd, &zio->io_bookmark, zio, 0);
4966 } else {
4968 * Report dio_verify_rd ZED event.
4970 (void) zfs_ereport_post(FM_EREPORT_ZFS_DIO_VERIFY_RD,
4971 zio->io_spa, zio->io_vd, &zio->io_bookmark, zio, 0);
4976 * ==========================================================================
4977 * Error rank. Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other.
4978 * An error of 0 indicates success. ENXIO indicates whole-device failure,
4979 * which may be transient (e.g. unplugged) or permanent. ECKSUM and EIO
4980 * indicate errors that are specific to one I/O, and most likely permanent.
4981 * Any other error is presumed to be worse because we weren't expecting it.
4982 * ==========================================================================
4985 zio_worst_error(int e1, int e2)
4987 static int zio_error_rank[] = { 0, ENXIO, ECKSUM, EIO };
4988 int r1, r2;
4990 for (r1 = 0; r1 < sizeof (zio_error_rank) / sizeof (int); r1++)
4991 if (e1 == zio_error_rank[r1])
4992 break;
4994 for (r2 = 0; r2 < sizeof (zio_error_rank) / sizeof (int); r2++)
4995 if (e2 == zio_error_rank[r2])
4996 break;
4998 return (r1 > r2 ? e1 : e2);
5002 * ==========================================================================
5003 * I/O completion
5004 * ==========================================================================
5006 static zio_t *
5007 zio_ready(zio_t *zio)
5009 blkptr_t *bp = zio->io_bp;
5010 zio_t *pio, *pio_next;
5011 zio_link_t *zl = NULL;
5013 if (zio_wait_for_children(zio, ZIO_CHILD_LOGICAL_BIT |
5014 ZIO_CHILD_GANG_BIT | ZIO_CHILD_DDT_BIT, ZIO_WAIT_READY)) {
5015 return (NULL);
5018 if (zio->io_ready) {
5019 ASSERT(IO_IS_ALLOCATING(zio));
5020 ASSERT(BP_GET_LOGICAL_BIRTH(bp) == zio->io_txg ||
5021 BP_IS_HOLE(bp) || (zio->io_flags & ZIO_FLAG_NOPWRITE));
5022 ASSERT(zio->io_children[ZIO_CHILD_GANG][ZIO_WAIT_READY] == 0);
5024 zio->io_ready(zio);
5027 #ifdef ZFS_DEBUG
5028 if (bp != NULL && bp != &zio->io_bp_copy)
5029 zio->io_bp_copy = *bp;
5030 #endif
5032 if (zio->io_error != 0) {
5033 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
5035 if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
5036 ASSERT(IO_IS_ALLOCATING(zio));
5037 ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
5038 ASSERT(zio->io_metaslab_class != NULL);
5039 ASSERT(ZIO_HAS_ALLOCATOR(zio));
5042 * We were unable to allocate anything, unreserve and
5043 * issue the next I/O to allocate.
5045 metaslab_class_throttle_unreserve(
5046 zio->io_metaslab_class, zio->io_prop.zp_copies,
5047 zio->io_allocator, zio);
5048 zio_allocate_dispatch(zio->io_spa, zio->io_allocator);
5052 mutex_enter(&zio->io_lock);
5053 zio->io_state[ZIO_WAIT_READY] = 1;
5054 pio = zio_walk_parents(zio, &zl);
5055 mutex_exit(&zio->io_lock);
5058 * As we notify zio's parents, new parents could be added.
5059 * New parents go to the head of zio's io_parent_list, however,
5060 * so we will (correctly) not notify them. The remainder of zio's
5061 * io_parent_list, from 'pio_next' onward, cannot change because
5062 * all parents must wait for us to be done before they can be done.
5064 for (; pio != NULL; pio = pio_next) {
5065 pio_next = zio_walk_parents(zio, &zl);
5066 zio_notify_parent(pio, zio, ZIO_WAIT_READY, NULL);
5069 if (zio->io_flags & ZIO_FLAG_NODATA) {
5070 if (bp != NULL && BP_IS_GANG(bp)) {
5071 zio->io_flags &= ~ZIO_FLAG_NODATA;
5072 } else {
5073 ASSERT((uintptr_t)zio->io_abd < SPA_MAXBLOCKSIZE);
5074 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
5078 if (zio_injection_enabled &&
5079 zio->io_spa->spa_syncing_txg == zio->io_txg)
5080 zio_handle_ignored_writes(zio);
5082 return (zio);
5086 * Update the allocation throttle accounting.
5088 static void
5089 zio_dva_throttle_done(zio_t *zio)
5091 zio_t *lio __maybe_unused = zio->io_logical;
5092 zio_t *pio = zio_unique_parent(zio);
5093 vdev_t *vd = zio->io_vd;
5094 int flags = METASLAB_ASYNC_ALLOC;
5096 ASSERT3P(zio->io_bp, !=, NULL);
5097 ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE);
5098 ASSERT3U(zio->io_priority, ==, ZIO_PRIORITY_ASYNC_WRITE);
5099 ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
5100 ASSERT(vd != NULL);
5101 ASSERT3P(vd, ==, vd->vdev_top);
5102 ASSERT(zio_injection_enabled || !(zio->io_flags & ZIO_FLAG_IO_RETRY));
5103 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REPAIR));
5104 ASSERT(zio->io_flags & ZIO_FLAG_IO_ALLOCATING);
5105 ASSERT(!(lio->io_flags & ZIO_FLAG_IO_REWRITE));
5106 ASSERT(!(lio->io_orig_flags & ZIO_FLAG_NODATA));
5109 * Parents of gang children can have two flavors -- ones that
5110 * allocated the gang header (will have ZIO_FLAG_IO_REWRITE set)
5111 * and ones that allocated the constituent blocks. The allocation
5112 * throttle needs to know the allocating parent zio so we must find
5113 * it here.
5115 if (pio->io_child_type == ZIO_CHILD_GANG) {
5117 * If our parent is a rewrite gang child then our grandparent
5118 * would have been the one that performed the allocation.
5120 if (pio->io_flags & ZIO_FLAG_IO_REWRITE)
5121 pio = zio_unique_parent(pio);
5122 flags |= METASLAB_GANG_CHILD;
5125 ASSERT(IO_IS_ALLOCATING(pio));
5126 ASSERT(ZIO_HAS_ALLOCATOR(pio));
5127 ASSERT3P(zio, !=, zio->io_logical);
5128 ASSERT(zio->io_logical != NULL);
5129 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REPAIR));
5130 ASSERT0(zio->io_flags & ZIO_FLAG_NOPWRITE);
5131 ASSERT(zio->io_metaslab_class != NULL);
5133 mutex_enter(&pio->io_lock);
5134 metaslab_group_alloc_decrement(zio->io_spa, vd->vdev_id, pio, flags,
5135 pio->io_allocator, B_TRUE);
5136 mutex_exit(&pio->io_lock);
5138 metaslab_class_throttle_unreserve(zio->io_metaslab_class, 1,
5139 pio->io_allocator, pio);
5142 * Call into the pipeline to see if there is more work that
5143 * needs to be done. If there is work to be done it will be
5144 * dispatched to another taskq thread.
5146 zio_allocate_dispatch(zio->io_spa, pio->io_allocator);
5149 static zio_t *
5150 zio_done(zio_t *zio)
5153 * Always attempt to keep stack usage minimal here since
5154 * we can be called recursively up to 19 levels deep.
5156 const uint64_t psize = zio->io_size;
5157 zio_t *pio, *pio_next;
5158 zio_link_t *zl = NULL;
5161 * If our children haven't all completed,
5162 * wait for them and then repeat this pipeline stage.
5164 if (zio_wait_for_children(zio, ZIO_CHILD_ALL_BITS, ZIO_WAIT_DONE)) {
5165 return (NULL);
5169 * If the allocation throttle is enabled, then update the accounting.
5170 * We only track child I/Os that are part of an allocating async
5171 * write. We must do this since the allocation is performed
5172 * by the logical I/O but the actual write is done by child I/Os.
5174 if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING &&
5175 zio->io_child_type == ZIO_CHILD_VDEV) {
5176 ASSERT(zio->io_metaslab_class != NULL);
5177 ASSERT(zio->io_metaslab_class->mc_alloc_throttle_enabled);
5178 zio_dva_throttle_done(zio);
5182 * If the allocation throttle is enabled, verify that
5183 * we have decremented the refcounts for every I/O that was throttled.
5185 if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
5186 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
5187 ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
5188 ASSERT(zio->io_bp != NULL);
5189 ASSERT(ZIO_HAS_ALLOCATOR(zio));
5191 metaslab_group_alloc_verify(zio->io_spa, zio->io_bp, zio,
5192 zio->io_allocator);
5193 VERIFY(zfs_refcount_not_held(&zio->io_metaslab_class->
5194 mc_allocator[zio->io_allocator].mca_alloc_slots, zio));
5198 for (int c = 0; c < ZIO_CHILD_TYPES; c++)
5199 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
5200 ASSERT(zio->io_children[c][w] == 0);
5202 if (zio->io_bp != NULL && !BP_IS_EMBEDDED(zio->io_bp)) {
5203 ASSERT(zio->io_bp->blk_pad[0] == 0);
5204 ASSERT(zio->io_bp->blk_pad[1] == 0);
5205 ASSERT(memcmp(zio->io_bp, &zio->io_bp_copy,
5206 sizeof (blkptr_t)) == 0 ||
5207 (zio->io_bp == zio_unique_parent(zio)->io_bp));
5208 if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(zio->io_bp) &&
5209 zio->io_bp_override == NULL &&
5210 !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) {
5211 ASSERT3U(zio->io_prop.zp_copies, <=,
5212 BP_GET_NDVAS(zio->io_bp));
5213 ASSERT(BP_COUNT_GANG(zio->io_bp) == 0 ||
5214 (BP_COUNT_GANG(zio->io_bp) ==
5215 BP_GET_NDVAS(zio->io_bp)));
5217 if (zio->io_flags & ZIO_FLAG_NOPWRITE)
5218 VERIFY(BP_EQUAL(zio->io_bp, &zio->io_bp_orig));
5222 * If there were child vdev/gang/ddt errors, they apply to us now.
5224 zio_inherit_child_errors(zio, ZIO_CHILD_VDEV);
5225 zio_inherit_child_errors(zio, ZIO_CHILD_GANG);
5226 zio_inherit_child_errors(zio, ZIO_CHILD_DDT);
5229 * If the I/O on the transformed data was successful, generate any
5230 * checksum reports now while we still have the transformed data.
5232 if (zio->io_error == 0) {
5233 while (zio->io_cksum_report != NULL) {
5234 zio_cksum_report_t *zcr = zio->io_cksum_report;
5235 uint64_t align = zcr->zcr_align;
5236 uint64_t asize = P2ROUNDUP(psize, align);
5237 abd_t *adata = zio->io_abd;
5239 if (adata != NULL && asize != psize) {
5240 adata = abd_alloc(asize, B_TRUE);
5241 abd_copy(adata, zio->io_abd, psize);
5242 abd_zero_off(adata, psize, asize - psize);
5245 zio->io_cksum_report = zcr->zcr_next;
5246 zcr->zcr_next = NULL;
5247 zcr->zcr_finish(zcr, adata);
5248 zfs_ereport_free_checksum(zcr);
5250 if (adata != NULL && asize != psize)
5251 abd_free(adata);
5255 zio_pop_transforms(zio); /* note: may set zio->io_error */
5257 vdev_stat_update(zio, psize);
5260 * If this I/O is attached to a particular vdev is slow, exceeding
5261 * 30 seconds to complete, post an error described the I/O delay.
5262 * We ignore these errors if the device is currently unavailable.
5264 if (zio->io_delay >= MSEC2NSEC(zio_slow_io_ms)) {
5265 if (zio->io_vd != NULL && !vdev_is_dead(zio->io_vd)) {
5267 * We want to only increment our slow IO counters if
5268 * the IO is valid (i.e. not if the drive is removed).
5270 * zfs_ereport_post() will also do these checks, but
5271 * it can also ratelimit and have other failures, so we
5272 * need to increment the slow_io counters independent
5273 * of it.
5275 if (zfs_ereport_is_valid(FM_EREPORT_ZFS_DELAY,
5276 zio->io_spa, zio->io_vd, zio)) {
5277 mutex_enter(&zio->io_vd->vdev_stat_lock);
5278 zio->io_vd->vdev_stat.vs_slow_ios++;
5279 mutex_exit(&zio->io_vd->vdev_stat_lock);
5281 (void) zfs_ereport_post(FM_EREPORT_ZFS_DELAY,
5282 zio->io_spa, zio->io_vd, &zio->io_bookmark,
5283 zio, 0);
5288 if (zio->io_error) {
5290 * If this I/O is attached to a particular vdev,
5291 * generate an error message describing the I/O failure
5292 * at the block level. We ignore these errors if the
5293 * device is currently unavailable.
5295 if (zio->io_error != ECKSUM && zio->io_vd != NULL &&
5296 !vdev_is_dead(zio->io_vd) &&
5297 !(zio->io_flags & ZIO_FLAG_DIO_CHKSUM_ERR)) {
5298 int ret = zfs_ereport_post(FM_EREPORT_ZFS_IO,
5299 zio->io_spa, zio->io_vd, &zio->io_bookmark, zio, 0);
5300 if (ret != EALREADY) {
5301 mutex_enter(&zio->io_vd->vdev_stat_lock);
5302 if (zio->io_type == ZIO_TYPE_READ)
5303 zio->io_vd->vdev_stat.vs_read_errors++;
5304 else if (zio->io_type == ZIO_TYPE_WRITE)
5305 zio->io_vd->vdev_stat.vs_write_errors++;
5306 mutex_exit(&zio->io_vd->vdev_stat_lock);
5310 if ((zio->io_error == EIO || !(zio->io_flags &
5311 (ZIO_FLAG_SPECULATIVE | ZIO_FLAG_DONT_PROPAGATE))) &&
5312 !(zio->io_flags & ZIO_FLAG_DIO_CHKSUM_ERR) &&
5313 zio == zio->io_logical) {
5315 * For logical I/O requests, tell the SPA to log the
5316 * error and generate a logical data ereport.
5318 spa_log_error(zio->io_spa, &zio->io_bookmark,
5319 BP_GET_LOGICAL_BIRTH(zio->io_bp));
5320 (void) zfs_ereport_post(FM_EREPORT_ZFS_DATA,
5321 zio->io_spa, NULL, &zio->io_bookmark, zio, 0);
5325 if (zio->io_error && zio == zio->io_logical) {
5327 * Determine whether zio should be reexecuted. This will
5328 * propagate all the way to the root via zio_notify_parent().
5330 ASSERT(zio->io_vd == NULL && zio->io_bp != NULL);
5331 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
5333 if (IO_IS_ALLOCATING(zio) &&
5334 !(zio->io_flags & ZIO_FLAG_CANFAIL) &&
5335 !(zio->io_flags & ZIO_FLAG_DIO_CHKSUM_ERR)) {
5336 if (zio->io_error != ENOSPC)
5337 zio->io_reexecute |= ZIO_REEXECUTE_NOW;
5338 else
5339 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
5342 if ((zio->io_type == ZIO_TYPE_READ ||
5343 zio->io_type == ZIO_TYPE_FREE) &&
5344 !(zio->io_flags & ZIO_FLAG_SCAN_THREAD) &&
5345 zio->io_error == ENXIO &&
5346 spa_load_state(zio->io_spa) == SPA_LOAD_NONE &&
5347 spa_get_failmode(zio->io_spa) != ZIO_FAILURE_MODE_CONTINUE)
5348 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
5350 if (!(zio->io_flags & ZIO_FLAG_CANFAIL) && !zio->io_reexecute)
5351 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
5354 * Here is a possibly good place to attempt to do
5355 * either combinatorial reconstruction or error correction
5356 * based on checksums. It also might be a good place
5357 * to send out preliminary ereports before we suspend
5358 * processing.
5363 * If there were logical child errors, they apply to us now.
5364 * We defer this until now to avoid conflating logical child
5365 * errors with errors that happened to the zio itself when
5366 * updating vdev stats and reporting FMA events above.
5368 zio_inherit_child_errors(zio, ZIO_CHILD_LOGICAL);
5370 if ((zio->io_error || zio->io_reexecute) &&
5371 IO_IS_ALLOCATING(zio) && zio->io_gang_leader == zio &&
5372 !(zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)))
5373 zio_dva_unallocate(zio, zio->io_gang_tree, zio->io_bp);
5375 zio_gang_tree_free(&zio->io_gang_tree);
5378 * Godfather I/Os should never suspend.
5380 if ((zio->io_flags & ZIO_FLAG_GODFATHER) &&
5381 (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND))
5382 zio->io_reexecute &= ~ZIO_REEXECUTE_SUSPEND;
5384 if (zio->io_reexecute) {
5386 * A Direct I/O operation that has a checksum verify error
5387 * should not attempt to reexecute. Instead, the error should
5388 * just be propagated back.
5390 ASSERT(!(zio->io_flags & ZIO_FLAG_DIO_CHKSUM_ERR));
5393 * This is a logical I/O that wants to reexecute.
5395 * Reexecute is top-down. When an i/o fails, if it's not
5396 * the root, it simply notifies its parent and sticks around.
5397 * The parent, seeing that it still has children in zio_done(),
5398 * does the same. This percolates all the way up to the root.
5399 * The root i/o will reexecute or suspend the entire tree.
5401 * This approach ensures that zio_reexecute() honors
5402 * all the original i/o dependency relationships, e.g.
5403 * parents not executing until children are ready.
5405 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
5407 zio->io_gang_leader = NULL;
5409 mutex_enter(&zio->io_lock);
5410 zio->io_state[ZIO_WAIT_DONE] = 1;
5411 mutex_exit(&zio->io_lock);
5414 * "The Godfather" I/O monitors its children but is
5415 * not a true parent to them. It will track them through
5416 * the pipeline but severs its ties whenever they get into
5417 * trouble (e.g. suspended). This allows "The Godfather"
5418 * I/O to return status without blocking.
5420 zl = NULL;
5421 for (pio = zio_walk_parents(zio, &zl); pio != NULL;
5422 pio = pio_next) {
5423 zio_link_t *remove_zl = zl;
5424 pio_next = zio_walk_parents(zio, &zl);
5426 if ((pio->io_flags & ZIO_FLAG_GODFATHER) &&
5427 (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND)) {
5428 zio_remove_child(pio, zio, remove_zl);
5430 * This is a rare code path, so we don't
5431 * bother with "next_to_execute".
5433 zio_notify_parent(pio, zio, ZIO_WAIT_DONE,
5434 NULL);
5438 if ((pio = zio_unique_parent(zio)) != NULL) {
5440 * We're not a root i/o, so there's nothing to do
5441 * but notify our parent. Don't propagate errors
5442 * upward since we haven't permanently failed yet.
5444 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
5445 zio->io_flags |= ZIO_FLAG_DONT_PROPAGATE;
5447 * This is a rare code path, so we don't bother with
5448 * "next_to_execute".
5450 zio_notify_parent(pio, zio, ZIO_WAIT_DONE, NULL);
5451 } else if (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND) {
5453 * We'd fail again if we reexecuted now, so suspend
5454 * until conditions improve (e.g. device comes online).
5456 zio_suspend(zio->io_spa, zio, ZIO_SUSPEND_IOERR);
5457 } else {
5459 * Reexecution is potentially a huge amount of work.
5460 * Hand it off to the otherwise-unused claim taskq.
5462 spa_taskq_dispatch(zio->io_spa,
5463 ZIO_TYPE_CLAIM, ZIO_TASKQ_ISSUE,
5464 zio_reexecute, zio, B_FALSE);
5466 return (NULL);
5469 ASSERT(list_is_empty(&zio->io_child_list));
5470 ASSERT(zio->io_reexecute == 0);
5471 ASSERT(zio->io_error == 0 || (zio->io_flags & ZIO_FLAG_CANFAIL));
5474 * Report any checksum errors, since the I/O is complete.
5476 while (zio->io_cksum_report != NULL) {
5477 zio_cksum_report_t *zcr = zio->io_cksum_report;
5478 zio->io_cksum_report = zcr->zcr_next;
5479 zcr->zcr_next = NULL;
5480 zcr->zcr_finish(zcr, NULL);
5481 zfs_ereport_free_checksum(zcr);
5485 * It is the responsibility of the done callback to ensure that this
5486 * particular zio is no longer discoverable for adoption, and as
5487 * such, cannot acquire any new parents.
5489 if (zio->io_done)
5490 zio->io_done(zio);
5492 mutex_enter(&zio->io_lock);
5493 zio->io_state[ZIO_WAIT_DONE] = 1;
5494 mutex_exit(&zio->io_lock);
5497 * We are done executing this zio. We may want to execute a parent
5498 * next. See the comment in zio_notify_parent().
5500 zio_t *next_to_execute = NULL;
5501 zl = NULL;
5502 for (pio = zio_walk_parents(zio, &zl); pio != NULL; pio = pio_next) {
5503 zio_link_t *remove_zl = zl;
5504 pio_next = zio_walk_parents(zio, &zl);
5505 zio_remove_child(pio, zio, remove_zl);
5506 zio_notify_parent(pio, zio, ZIO_WAIT_DONE, &next_to_execute);
5509 if (zio->io_waiter != NULL) {
5510 mutex_enter(&zio->io_lock);
5511 zio->io_executor = NULL;
5512 cv_broadcast(&zio->io_cv);
5513 mutex_exit(&zio->io_lock);
5514 } else {
5515 zio_destroy(zio);
5518 return (next_to_execute);
5522 * ==========================================================================
5523 * I/O pipeline definition
5524 * ==========================================================================
5526 static zio_pipe_stage_t *zio_pipeline[] = {
5527 NULL,
5528 zio_read_bp_init,
5529 zio_write_bp_init,
5530 zio_free_bp_init,
5531 zio_issue_async,
5532 zio_write_compress,
5533 zio_encrypt,
5534 zio_checksum_generate,
5535 zio_nop_write,
5536 zio_brt_free,
5537 zio_ddt_read_start,
5538 zio_ddt_read_done,
5539 zio_ddt_write,
5540 zio_ddt_free,
5541 zio_gang_assemble,
5542 zio_gang_issue,
5543 zio_dva_throttle,
5544 zio_dva_allocate,
5545 zio_dva_free,
5546 zio_dva_claim,
5547 zio_ready,
5548 zio_vdev_io_start,
5549 zio_vdev_io_done,
5550 zio_vdev_io_assess,
5551 zio_checksum_verify,
5552 zio_dio_checksum_verify,
5553 zio_done
5560 * Compare two zbookmark_phys_t's to see which we would reach first in a
5561 * pre-order traversal of the object tree.
5563 * This is simple in every case aside from the meta-dnode object. For all other
5564 * objects, we traverse them in order (object 1 before object 2, and so on).
5565 * However, all of these objects are traversed while traversing object 0, since
5566 * the data it points to is the list of objects. Thus, we need to convert to a
5567 * canonical representation so we can compare meta-dnode bookmarks to
5568 * non-meta-dnode bookmarks.
5570 * We do this by calculating "equivalents" for each field of the zbookmark.
5571 * zbookmarks outside of the meta-dnode use their own object and level, and
5572 * calculate the level 0 equivalent (the first L0 blkid that is contained in the
5573 * blocks this bookmark refers to) by multiplying their blkid by their span
5574 * (the number of L0 blocks contained within one block at their level).
5575 * zbookmarks inside the meta-dnode calculate their object equivalent
5576 * (which is L0equiv * dnodes per data block), use 0 for their L0equiv, and use
5577 * level + 1<<31 (any value larger than a level could ever be) for their level.
5578 * This causes them to always compare before a bookmark in their object
5579 * equivalent, compare appropriately to bookmarks in other objects, and to
5580 * compare appropriately to other bookmarks in the meta-dnode.
5583 zbookmark_compare(uint16_t dbss1, uint8_t ibs1, uint16_t dbss2, uint8_t ibs2,
5584 const zbookmark_phys_t *zb1, const zbookmark_phys_t *zb2)
5587 * These variables represent the "equivalent" values for the zbookmark,
5588 * after converting zbookmarks inside the meta dnode to their
5589 * normal-object equivalents.
5591 uint64_t zb1obj, zb2obj;
5592 uint64_t zb1L0, zb2L0;
5593 uint64_t zb1level, zb2level;
5595 if (zb1->zb_object == zb2->zb_object &&
5596 zb1->zb_level == zb2->zb_level &&
5597 zb1->zb_blkid == zb2->zb_blkid)
5598 return (0);
5600 IMPLY(zb1->zb_level > 0, ibs1 >= SPA_MINBLOCKSHIFT);
5601 IMPLY(zb2->zb_level > 0, ibs2 >= SPA_MINBLOCKSHIFT);
5604 * BP_SPANB calculates the span in blocks.
5606 zb1L0 = (zb1->zb_blkid) * BP_SPANB(ibs1, zb1->zb_level);
5607 zb2L0 = (zb2->zb_blkid) * BP_SPANB(ibs2, zb2->zb_level);
5609 if (zb1->zb_object == DMU_META_DNODE_OBJECT) {
5610 zb1obj = zb1L0 * (dbss1 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT));
5611 zb1L0 = 0;
5612 zb1level = zb1->zb_level + COMPARE_META_LEVEL;
5613 } else {
5614 zb1obj = zb1->zb_object;
5615 zb1level = zb1->zb_level;
5618 if (zb2->zb_object == DMU_META_DNODE_OBJECT) {
5619 zb2obj = zb2L0 * (dbss2 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT));
5620 zb2L0 = 0;
5621 zb2level = zb2->zb_level + COMPARE_META_LEVEL;
5622 } else {
5623 zb2obj = zb2->zb_object;
5624 zb2level = zb2->zb_level;
5627 /* Now that we have a canonical representation, do the comparison. */
5628 if (zb1obj != zb2obj)
5629 return (zb1obj < zb2obj ? -1 : 1);
5630 else if (zb1L0 != zb2L0)
5631 return (zb1L0 < zb2L0 ? -1 : 1);
5632 else if (zb1level != zb2level)
5633 return (zb1level > zb2level ? -1 : 1);
5635 * This can (theoretically) happen if the bookmarks have the same object
5636 * and level, but different blkids, if the block sizes are not the same.
5637 * There is presently no way to change the indirect block sizes
5639 return (0);
5643 * This function checks the following: given that last_block is the place that
5644 * our traversal stopped last time, does that guarantee that we've visited
5645 * every node under subtree_root? Therefore, we can't just use the raw output
5646 * of zbookmark_compare. We have to pass in a modified version of
5647 * subtree_root; by incrementing the block id, and then checking whether
5648 * last_block is before or equal to that, we can tell whether or not having
5649 * visited last_block implies that all of subtree_root's children have been
5650 * visited.
5652 boolean_t
5653 zbookmark_subtree_completed(const dnode_phys_t *dnp,
5654 const zbookmark_phys_t *subtree_root, const zbookmark_phys_t *last_block)
5656 zbookmark_phys_t mod_zb = *subtree_root;
5657 mod_zb.zb_blkid++;
5658 ASSERT0(last_block->zb_level);
5660 /* The objset_phys_t isn't before anything. */
5661 if (dnp == NULL)
5662 return (B_FALSE);
5665 * We pass in 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT) for the
5666 * data block size in sectors, because that variable is only used if
5667 * the bookmark refers to a block in the meta-dnode. Since we don't
5668 * know without examining it what object it refers to, and there's no
5669 * harm in passing in this value in other cases, we always pass it in.
5671 * We pass in 0 for the indirect block size shift because zb2 must be
5672 * level 0. The indirect block size is only used to calculate the span
5673 * of the bookmark, but since the bookmark must be level 0, the span is
5674 * always 1, so the math works out.
5676 * If you make changes to how the zbookmark_compare code works, be sure
5677 * to make sure that this code still works afterwards.
5679 return (zbookmark_compare(dnp->dn_datablkszsec, dnp->dn_indblkshift,
5680 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT), 0, &mod_zb,
5681 last_block) <= 0);
5685 * This function is similar to zbookmark_subtree_completed(), but returns true
5686 * if subtree_root is equal or ahead of last_block, i.e. still to be done.
5688 boolean_t
5689 zbookmark_subtree_tbd(const dnode_phys_t *dnp,
5690 const zbookmark_phys_t *subtree_root, const zbookmark_phys_t *last_block)
5692 ASSERT0(last_block->zb_level);
5693 if (dnp == NULL)
5694 return (B_FALSE);
5695 return (zbookmark_compare(dnp->dn_datablkszsec, dnp->dn_indblkshift,
5696 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT), 0, subtree_root,
5697 last_block) >= 0);
5700 EXPORT_SYMBOL(zio_type_name);
5701 EXPORT_SYMBOL(zio_buf_alloc);
5702 EXPORT_SYMBOL(zio_data_buf_alloc);
5703 EXPORT_SYMBOL(zio_buf_free);
5704 EXPORT_SYMBOL(zio_data_buf_free);
5706 ZFS_MODULE_PARAM(zfs_zio, zio_, slow_io_ms, INT, ZMOD_RW,
5707 "Max I/O completion time (milliseconds) before marking it as slow");
5709 ZFS_MODULE_PARAM(zfs_zio, zio_, requeue_io_start_cut_in_line, INT, ZMOD_RW,
5710 "Prioritize requeued I/O");
5712 ZFS_MODULE_PARAM(zfs, zfs_, sync_pass_deferred_free, UINT, ZMOD_RW,
5713 "Defer frees starting in this pass");
5715 ZFS_MODULE_PARAM(zfs, zfs_, sync_pass_dont_compress, UINT, ZMOD_RW,
5716 "Don't compress starting in this pass");
5718 ZFS_MODULE_PARAM(zfs, zfs_, sync_pass_rewrite, UINT, ZMOD_RW,
5719 "Rewrite new bps starting in this pass");
5721 ZFS_MODULE_PARAM(zfs_zio, zio_, dva_throttle_enabled, INT, ZMOD_RW,
5722 "Throttle block allocations in the ZIO pipeline");
5724 ZFS_MODULE_PARAM(zfs_zio, zio_, deadman_log_all, INT, ZMOD_RW,
5725 "Log all slow ZIOs, not just those with vdevs");