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