1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 223f9d6ad7SLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23f78cdc34SPaul Dagnelie * Copyright (c) 2011, 2018 by Delphix. All rights reserved. 245aeb9474SGarrett D'Amore * Copyright (c) 2011 Nexenta Systems, Inc. All rights reserved. 25c3d26abcSMatthew Ahrens * Copyright (c) 2014 Integros [integros.com] 26*663207adSDon Brady * Copyright (c) 2017, Intel Corporation. 27fa9e4066Sahrens */ 28fa9e4066Sahrens 29de710d24SJosef 'Jeff' Sipek #include <sys/sysmacros.h> 30fa9e4066Sahrens #include <sys/zfs_context.h> 31ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h> 32fa9e4066Sahrens #include <sys/spa.h> 33fa9e4066Sahrens #include <sys/txg.h> 34fa9e4066Sahrens #include <sys/spa_impl.h> 35fa9e4066Sahrens #include <sys/vdev_impl.h> 36fa9e4066Sahrens #include <sys/zio_impl.h> 37fa9e4066Sahrens #include <sys/zio_compress.h> 38fa9e4066Sahrens #include <sys/zio_checksum.h> 39b24ab676SJeff Bonwick #include <sys/dmu_objset.h> 40b24ab676SJeff Bonwick #include <sys/arc.h> 41b24ab676SJeff Bonwick #include <sys/ddt.h> 425d7b4d43SMatthew Ahrens #include <sys/blkptr.h> 4343466aaeSMax Grossman #include <sys/zfeature.h> 440f7643c7SGeorge Wilson #include <sys/metaslab_impl.h> 45770499e1SDan Kimmel #include <sys/abd.h> 46f78cdc34SPaul Dagnelie #include <sys/cityhash.h> 47fa9e4066Sahrens 48fa9e4066Sahrens /* 49fa9e4066Sahrens * ========================================================================== 50fa9e4066Sahrens * I/O type descriptions 51fa9e4066Sahrens * ========================================================================== 52fa9e4066Sahrens */ 5369962b56SMatthew Ahrens const char *zio_type_name[ZIO_TYPES] = { 5480eb36f2SGeorge Wilson "zio_null", "zio_read", "zio_write", "zio_free", "zio_claim", 5580eb36f2SGeorge Wilson "zio_ioctl" 5680eb36f2SGeorge Wilson }; 57fa9e4066Sahrens 580f7643c7SGeorge Wilson boolean_t zio_dva_throttle_enabled = B_TRUE; 590f7643c7SGeorge Wilson 60fa9e4066Sahrens /* 61fa9e4066Sahrens * ========================================================================== 62fa9e4066Sahrens * I/O kmem caches 63fa9e4066Sahrens * ========================================================================== 64fa9e4066Sahrens */ 65ccae0b50Seschrock kmem_cache_t *zio_cache; 66a3f829aeSBill Moore kmem_cache_t *zio_link_cache; 67fa9e4066Sahrens kmem_cache_t *zio_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT]; 68ad23a2dbSjohansen kmem_cache_t *zio_data_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT]; 69ad23a2dbSjohansen 70ad23a2dbSjohansen #ifdef _KERNEL 71ad23a2dbSjohansen extern vmem_t *zio_alloc_arena; 72ad23a2dbSjohansen #endif 73fa9e4066Sahrens 74738f37bcSGeorge Wilson #define ZIO_PIPELINE_CONTINUE 0x100 75738f37bcSGeorge Wilson #define ZIO_PIPELINE_STOP 0x101 76738f37bcSGeorge Wilson 77a2cdcdd2SPaul Dagnelie #define BP_SPANB(indblkshift, level) \ 78a2cdcdd2SPaul Dagnelie (((uint64_t)1) << ((level) * ((indblkshift) - SPA_BLKPTRSHIFT))) 79a2cdcdd2SPaul Dagnelie #define COMPARE_META_LEVEL 0x80000000ul 8001f55e48SGeorge Wilson /* 8101f55e48SGeorge Wilson * The following actions directly effect the spa's sync-to-convergence logic. 8201f55e48SGeorge Wilson * The values below define the sync pass when we start performing the action. 8301f55e48SGeorge Wilson * Care should be taken when changing these values as they directly impact 8401f55e48SGeorge Wilson * spa_sync() performance. Tuning these values may introduce subtle performance 8501f55e48SGeorge Wilson * pathologies and should only be done in the context of performance analysis. 8601f55e48SGeorge Wilson * These tunables will eventually be removed and replaced with #defines once 8701f55e48SGeorge Wilson * enough analysis has been done to determine optimal values. 8801f55e48SGeorge Wilson * 8901f55e48SGeorge Wilson * The 'zfs_sync_pass_deferred_free' pass must be greater than 1 to ensure that 9001f55e48SGeorge Wilson * regular blocks are not deferred. 9101f55e48SGeorge Wilson */ 9201f55e48SGeorge Wilson int zfs_sync_pass_deferred_free = 2; /* defer frees starting in this pass */ 9301f55e48SGeorge Wilson int zfs_sync_pass_dont_compress = 5; /* don't compress starting in this pass */ 9401f55e48SGeorge Wilson int zfs_sync_pass_rewrite = 2; /* rewrite new bps starting in this pass */ 9501f55e48SGeorge Wilson 960a4e9518Sgw /* 97e14bb325SJeff Bonwick * An allocating zio is one that either currently has the DVA allocate 98e14bb325SJeff Bonwick * stage set or will have it later in its lifetime. 990a4e9518Sgw */ 100b24ab676SJeff Bonwick #define IO_IS_ALLOCATING(zio) ((zio)->io_orig_pipeline & ZIO_STAGE_DVA_ALLOCATE) 101b24ab676SJeff Bonwick 10235a5a358SJonathan Adams boolean_t zio_requeue_io_start_cut_in_line = B_TRUE; 10335a5a358SJonathan Adams 104b24ab676SJeff Bonwick #ifdef ZFS_DEBUG 105b24ab676SJeff Bonwick int zio_buf_debug_limit = 16384; 106b24ab676SJeff Bonwick #else 107b24ab676SJeff Bonwick int zio_buf_debug_limit = 0; 108b24ab676SJeff Bonwick #endif 1090a4e9518Sgw 1100f7643c7SGeorge Wilson static void zio_taskq_dispatch(zio_t *, zio_taskq_type_t, boolean_t); 1110f7643c7SGeorge Wilson 112fa9e4066Sahrens void 113fa9e4066Sahrens zio_init(void) 114fa9e4066Sahrens { 115fa9e4066Sahrens size_t c; 116ad23a2dbSjohansen vmem_t *data_alloc_arena = NULL; 117ad23a2dbSjohansen 118ad23a2dbSjohansen #ifdef _KERNEL 119ad23a2dbSjohansen data_alloc_arena = zio_alloc_arena; 120ad23a2dbSjohansen #endif 121a3f829aeSBill Moore zio_cache = kmem_cache_create("zio_cache", 122a3f829aeSBill Moore sizeof (zio_t), 0, NULL, NULL, NULL, NULL, NULL, 0); 123a3f829aeSBill Moore zio_link_cache = kmem_cache_create("zio_link_cache", 124a3f829aeSBill Moore sizeof (zio_link_t), 0, NULL, NULL, NULL, NULL, NULL, 0); 125ccae0b50Seschrock 126fa9e4066Sahrens /* 127fa9e4066Sahrens * For small buffers, we want a cache for each multiple of 128b5152584SMatthew Ahrens * SPA_MINBLOCKSIZE. For larger buffers, we want a cache 129b5152584SMatthew Ahrens * for each quarter-power of 2. 130fa9e4066Sahrens */ 131fa9e4066Sahrens for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) { 132fa9e4066Sahrens size_t size = (c + 1) << SPA_MINBLOCKSHIFT; 133fa9e4066Sahrens size_t p2 = size; 134fa9e4066Sahrens size_t align = 0; 135e291592aSJonathan Adams size_t cflags = (size > zio_buf_debug_limit) ? KMC_NODEBUG : 0; 136fa9e4066Sahrens 137de710d24SJosef 'Jeff' Sipek while (!ISP2(p2)) 138fa9e4066Sahrens p2 &= p2 - 1; 139fa9e4066Sahrens 140cd1c8b85SMatthew Ahrens #ifndef _KERNEL 141cd1c8b85SMatthew Ahrens /* 142cd1c8b85SMatthew Ahrens * If we are using watchpoints, put each buffer on its own page, 143cd1c8b85SMatthew Ahrens * to eliminate the performance overhead of trapping to the 144cd1c8b85SMatthew Ahrens * kernel when modifying a non-watched buffer that shares the 145cd1c8b85SMatthew Ahrens * page with a watched buffer. 146cd1c8b85SMatthew Ahrens */ 147cd1c8b85SMatthew Ahrens if (arc_watch && !IS_P2ALIGNED(size, PAGESIZE)) 148cd1c8b85SMatthew Ahrens continue; 149cd1c8b85SMatthew Ahrens #endif 150fa9e4066Sahrens if (size <= 4 * SPA_MINBLOCKSIZE) { 151fa9e4066Sahrens align = SPA_MINBLOCKSIZE; 152cd1c8b85SMatthew Ahrens } else if (IS_P2ALIGNED(size, p2 >> 2)) { 153b5152584SMatthew Ahrens align = MIN(p2 >> 2, PAGESIZE); 154fa9e4066Sahrens } 155fa9e4066Sahrens 156fa9e4066Sahrens if (align != 0) { 157ad23a2dbSjohansen char name[36]; 1585ad82045Snd (void) sprintf(name, "zio_buf_%lu", (ulong_t)size); 159fa9e4066Sahrens zio_buf_cache[c] = kmem_cache_create(name, size, 160e291592aSJonathan Adams align, NULL, NULL, NULL, NULL, NULL, cflags); 161ad23a2dbSjohansen 162e291592aSJonathan Adams /* 163e291592aSJonathan Adams * Since zio_data bufs do not appear in crash dumps, we 164e291592aSJonathan Adams * pass KMC_NOTOUCH so that no allocator metadata is 165e291592aSJonathan Adams * stored with the buffers. 166e291592aSJonathan Adams */ 167ad23a2dbSjohansen (void) sprintf(name, "zio_data_buf_%lu", (ulong_t)size); 168ad23a2dbSjohansen zio_data_buf_cache[c] = kmem_cache_create(name, size, 169ad23a2dbSjohansen align, NULL, NULL, NULL, NULL, data_alloc_arena, 170e291592aSJonathan Adams cflags | KMC_NOTOUCH); 171fa9e4066Sahrens } 172fa9e4066Sahrens } 173fa9e4066Sahrens 174fa9e4066Sahrens while (--c != 0) { 175fa9e4066Sahrens ASSERT(zio_buf_cache[c] != NULL); 176fa9e4066Sahrens if (zio_buf_cache[c - 1] == NULL) 177fa9e4066Sahrens zio_buf_cache[c - 1] = zio_buf_cache[c]; 178ad23a2dbSjohansen 179ad23a2dbSjohansen ASSERT(zio_data_buf_cache[c] != NULL); 180ad23a2dbSjohansen if (zio_data_buf_cache[c - 1] == NULL) 181ad23a2dbSjohansen zio_data_buf_cache[c - 1] = zio_data_buf_cache[c]; 182fa9e4066Sahrens } 183ea8dc4b6Seschrock 184ea8dc4b6Seschrock zio_inject_init(); 185fa9e4066Sahrens } 186fa9e4066Sahrens 187fa9e4066Sahrens void 188fa9e4066Sahrens zio_fini(void) 189fa9e4066Sahrens { 190fa9e4066Sahrens size_t c; 191fa9e4066Sahrens kmem_cache_t *last_cache = NULL; 192ad23a2dbSjohansen kmem_cache_t *last_data_cache = NULL; 193fa9e4066Sahrens 194fa9e4066Sahrens for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) { 195fa9e4066Sahrens if (zio_buf_cache[c] != last_cache) { 196fa9e4066Sahrens last_cache = zio_buf_cache[c]; 197fa9e4066Sahrens kmem_cache_destroy(zio_buf_cache[c]); 198fa9e4066Sahrens } 199fa9e4066Sahrens zio_buf_cache[c] = NULL; 200ad23a2dbSjohansen 201ad23a2dbSjohansen if (zio_data_buf_cache[c] != last_data_cache) { 202ad23a2dbSjohansen last_data_cache = zio_data_buf_cache[c]; 203ad23a2dbSjohansen kmem_cache_destroy(zio_data_buf_cache[c]); 204ad23a2dbSjohansen } 205ad23a2dbSjohansen zio_data_buf_cache[c] = NULL; 206fa9e4066Sahrens } 207ea8dc4b6Seschrock 208a3f829aeSBill Moore kmem_cache_destroy(zio_link_cache); 209ccae0b50Seschrock kmem_cache_destroy(zio_cache); 210ccae0b50Seschrock 211ea8dc4b6Seschrock zio_inject_fini(); 212fa9e4066Sahrens } 213fa9e4066Sahrens 214fa9e4066Sahrens /* 215fa9e4066Sahrens * ========================================================================== 216fa9e4066Sahrens * Allocate and free I/O buffers 217fa9e4066Sahrens * ========================================================================== 218fa9e4066Sahrens */ 219ad23a2dbSjohansen 220ad23a2dbSjohansen /* 221ad23a2dbSjohansen * Use zio_buf_alloc to allocate ZFS metadata. This data will appear in a 222ad23a2dbSjohansen * crashdump if the kernel panics, so use it judiciously. Obviously, it's 223ad23a2dbSjohansen * useful to inspect ZFS metadata, but if possible, we should avoid keeping 224ad23a2dbSjohansen * excess / transient data in-core during a crashdump. 225ad23a2dbSjohansen */ 226fa9e4066Sahrens void * 227fa9e4066Sahrens zio_buf_alloc(size_t size) 228fa9e4066Sahrens { 229fa9e4066Sahrens size_t c = (size - 1) >> SPA_MINBLOCKSHIFT; 230fa9e4066Sahrens 231f63ab3d5SMatthew Ahrens VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT); 232fa9e4066Sahrens 2331ab7f2deSmaybee return (kmem_cache_alloc(zio_buf_cache[c], KM_PUSHPAGE)); 234fa9e4066Sahrens } 235fa9e4066Sahrens 236ad23a2dbSjohansen /* 237ad23a2dbSjohansen * Use zio_data_buf_alloc to allocate data. The data will not appear in a 238ad23a2dbSjohansen * crashdump if the kernel panics. This exists so that we will limit the amount 239ad23a2dbSjohansen * of ZFS data that shows up in a kernel crashdump. (Thus reducing the amount 240ad23a2dbSjohansen * of kernel heap dumped to disk when the kernel panics) 241ad23a2dbSjohansen */ 242ad23a2dbSjohansen void * 243ad23a2dbSjohansen zio_data_buf_alloc(size_t size) 244ad23a2dbSjohansen { 245ad23a2dbSjohansen size_t c = (size - 1) >> SPA_MINBLOCKSHIFT; 246ad23a2dbSjohansen 247f63ab3d5SMatthew Ahrens VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT); 248ad23a2dbSjohansen 2491ab7f2deSmaybee return (kmem_cache_alloc(zio_data_buf_cache[c], KM_PUSHPAGE)); 250ad23a2dbSjohansen } 251ad23a2dbSjohansen 252fa9e4066Sahrens void 253fa9e4066Sahrens zio_buf_free(void *buf, size_t size) 254fa9e4066Sahrens { 255fa9e4066Sahrens size_t c = (size - 1) >> SPA_MINBLOCKSHIFT; 256fa9e4066Sahrens 257f63ab3d5SMatthew Ahrens VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT); 258fa9e4066Sahrens 259fa9e4066Sahrens kmem_cache_free(zio_buf_cache[c], buf); 260fa9e4066Sahrens } 261fa9e4066Sahrens 262ad23a2dbSjohansen void 263ad23a2dbSjohansen zio_data_buf_free(void *buf, size_t size) 264ad23a2dbSjohansen { 265ad23a2dbSjohansen size_t c = (size - 1) >> SPA_MINBLOCKSHIFT; 266ad23a2dbSjohansen 267f63ab3d5SMatthew Ahrens VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT); 268ad23a2dbSjohansen 269ad23a2dbSjohansen kmem_cache_free(zio_data_buf_cache[c], buf); 270ad23a2dbSjohansen } 271b3995adbSahrens 272fa9e4066Sahrens /* 273fa9e4066Sahrens * ========================================================================== 274fa9e4066Sahrens * Push and pop I/O transform buffers 275fa9e4066Sahrens * ========================================================================== 276fa9e4066Sahrens */ 277dcbf3bd6SGeorge Wilson void 278770499e1SDan Kimmel zio_push_transform(zio_t *zio, abd_t *data, uint64_t size, uint64_t bufsize, 2799a686fbcSPaul Dagnelie zio_transform_func_t *transform) 280fa9e4066Sahrens { 281fa9e4066Sahrens zio_transform_t *zt = kmem_alloc(sizeof (zio_transform_t), KM_SLEEP); 282fa9e4066Sahrens 283770499e1SDan Kimmel /* 284770499e1SDan Kimmel * Ensure that anyone expecting this zio to contain a linear ABD isn't 285770499e1SDan Kimmel * going to get a nasty surprise when they try to access the data. 286770499e1SDan Kimmel */ 287770499e1SDan Kimmel IMPLY(abd_is_linear(zio->io_abd), abd_is_linear(data)); 288770499e1SDan Kimmel 289770499e1SDan Kimmel zt->zt_orig_abd = zio->io_abd; 290e14bb325SJeff Bonwick zt->zt_orig_size = zio->io_size; 291fa9e4066Sahrens zt->zt_bufsize = bufsize; 292e14bb325SJeff Bonwick zt->zt_transform = transform; 293fa9e4066Sahrens 294fa9e4066Sahrens zt->zt_next = zio->io_transform_stack; 295fa9e4066Sahrens zio->io_transform_stack = zt; 296fa9e4066Sahrens 297770499e1SDan Kimmel zio->io_abd = data; 298fa9e4066Sahrens zio->io_size = size; 299fa9e4066Sahrens } 300fa9e4066Sahrens 301dcbf3bd6SGeorge Wilson void 302e14bb325SJeff Bonwick zio_pop_transforms(zio_t *zio) 303fa9e4066Sahrens { 304e14bb325SJeff Bonwick zio_transform_t *zt; 305e14bb325SJeff Bonwick 306e14bb325SJeff Bonwick while ((zt = zio->io_transform_stack) != NULL) { 307e14bb325SJeff Bonwick if (zt->zt_transform != NULL) 308e14bb325SJeff Bonwick zt->zt_transform(zio, 309770499e1SDan Kimmel zt->zt_orig_abd, zt->zt_orig_size); 310fa9e4066Sahrens 311b24ab676SJeff Bonwick if (zt->zt_bufsize != 0) 312770499e1SDan Kimmel abd_free(zio->io_abd); 313fa9e4066Sahrens 314770499e1SDan Kimmel zio->io_abd = zt->zt_orig_abd; 315e14bb325SJeff Bonwick zio->io_size = zt->zt_orig_size; 316e14bb325SJeff Bonwick zio->io_transform_stack = zt->zt_next; 317fa9e4066Sahrens 318e14bb325SJeff Bonwick kmem_free(zt, sizeof (zio_transform_t)); 319fa9e4066Sahrens } 320fa9e4066Sahrens } 321fa9e4066Sahrens 322e14bb325SJeff Bonwick /* 323e14bb325SJeff Bonwick * ========================================================================== 324e14bb325SJeff Bonwick * I/O transform callbacks for subblocks and decompression 325e14bb325SJeff Bonwick * ========================================================================== 326e14bb325SJeff Bonwick */ 327e14bb325SJeff Bonwick static void 328770499e1SDan Kimmel zio_subblock(zio_t *zio, abd_t *data, uint64_t size) 329e14bb325SJeff Bonwick { 330e14bb325SJeff Bonwick ASSERT(zio->io_size > size); 331e14bb325SJeff Bonwick 332e14bb325SJeff Bonwick if (zio->io_type == ZIO_TYPE_READ) 333770499e1SDan Kimmel abd_copy(data, zio->io_abd, size); 334e14bb325SJeff Bonwick } 335e14bb325SJeff Bonwick 336e14bb325SJeff Bonwick static void 337770499e1SDan Kimmel zio_decompress(zio_t *zio, abd_t *data, uint64_t size) 338e14bb325SJeff Bonwick { 339770499e1SDan Kimmel if (zio->io_error == 0) { 340770499e1SDan Kimmel void *tmp = abd_borrow_buf(data, size); 341770499e1SDan Kimmel int ret = zio_decompress_data(BP_GET_COMPRESS(zio->io_bp), 342770499e1SDan Kimmel zio->io_abd, tmp, zio->io_size, size); 343770499e1SDan Kimmel abd_return_buf_copy(data, tmp, size); 344770499e1SDan Kimmel 345770499e1SDan Kimmel if (ret != 0) 346770499e1SDan Kimmel zio->io_error = SET_ERROR(EIO); 347770499e1SDan Kimmel } 348e14bb325SJeff Bonwick } 349e14bb325SJeff Bonwick 350e14bb325SJeff Bonwick /* 351e14bb325SJeff Bonwick * ========================================================================== 352e14bb325SJeff Bonwick * I/O parent/child relationships and pipeline interlocks 353e14bb325SJeff Bonwick * ========================================================================== 354e14bb325SJeff Bonwick */ 355a3f829aeSBill Moore zio_t * 3560f7643c7SGeorge Wilson zio_walk_parents(zio_t *cio, zio_link_t **zl) 357a3f829aeSBill Moore { 358a3f829aeSBill Moore list_t *pl = &cio->io_parent_list; 359e14bb325SJeff Bonwick 3600f7643c7SGeorge Wilson *zl = (*zl == NULL) ? list_head(pl) : list_next(pl, *zl); 3610f7643c7SGeorge Wilson if (*zl == NULL) 362a3f829aeSBill Moore return (NULL); 363a3f829aeSBill Moore 3640f7643c7SGeorge Wilson ASSERT((*zl)->zl_child == cio); 3650f7643c7SGeorge Wilson return ((*zl)->zl_parent); 366a3f829aeSBill Moore } 367a3f829aeSBill Moore 368a3f829aeSBill Moore zio_t * 3690f7643c7SGeorge Wilson zio_walk_children(zio_t *pio, zio_link_t **zl) 370a3f829aeSBill Moore { 371a3f829aeSBill Moore list_t *cl = &pio->io_child_list; 372a3f829aeSBill Moore 3730f7643c7SGeorge Wilson *zl = (*zl == NULL) ? list_head(cl) : list_next(cl, *zl); 3740f7643c7SGeorge Wilson if (*zl == NULL) 375a3f829aeSBill Moore return (NULL); 376a3f829aeSBill Moore 3770f7643c7SGeorge Wilson ASSERT((*zl)->zl_parent == pio); 3780f7643c7SGeorge Wilson return ((*zl)->zl_child); 379a3f829aeSBill Moore } 380a3f829aeSBill Moore 381a3f829aeSBill Moore zio_t * 382a3f829aeSBill Moore zio_unique_parent(zio_t *cio) 383a3f829aeSBill Moore { 3840f7643c7SGeorge Wilson zio_link_t *zl = NULL; 3850f7643c7SGeorge Wilson zio_t *pio = zio_walk_parents(cio, &zl); 386a3f829aeSBill Moore 3870f7643c7SGeorge Wilson VERIFY3P(zio_walk_parents(cio, &zl), ==, NULL); 388a3f829aeSBill Moore return (pio); 389a3f829aeSBill Moore } 390a3f829aeSBill Moore 391a3f829aeSBill Moore void 392a3f829aeSBill Moore zio_add_child(zio_t *pio, zio_t *cio) 393e14bb325SJeff Bonwick { 394a3f829aeSBill Moore zio_link_t *zl = kmem_cache_alloc(zio_link_cache, KM_SLEEP); 395a3f829aeSBill Moore 396a3f829aeSBill Moore /* 397a3f829aeSBill Moore * Logical I/Os can have logical, gang, or vdev children. 398a3f829aeSBill Moore * Gang I/Os can have gang or vdev children. 399a3f829aeSBill Moore * Vdev I/Os can only have vdev children. 400a3f829aeSBill Moore * The following ASSERT captures all of these constraints. 401a3f829aeSBill Moore */ 4021271e4b1SPrakash Surya ASSERT3S(cio->io_child_type, <=, pio->io_child_type); 403a3f829aeSBill Moore 404a3f829aeSBill Moore zl->zl_parent = pio; 405a3f829aeSBill Moore zl->zl_child = cio; 406a3f829aeSBill Moore 407a3f829aeSBill Moore mutex_enter(&cio->io_lock); 408e14bb325SJeff Bonwick mutex_enter(&pio->io_lock); 409a3f829aeSBill Moore 410a3f829aeSBill Moore ASSERT(pio->io_state[ZIO_WAIT_DONE] == 0); 411a3f829aeSBill Moore 412a3f829aeSBill Moore for (int w = 0; w < ZIO_WAIT_TYPES; w++) 413a3f829aeSBill Moore pio->io_children[cio->io_child_type][w] += !cio->io_state[w]; 414a3f829aeSBill Moore 415a3f829aeSBill Moore list_insert_head(&pio->io_child_list, zl); 416a3f829aeSBill Moore list_insert_head(&cio->io_parent_list, zl); 417a3f829aeSBill Moore 418b24ab676SJeff Bonwick pio->io_child_count++; 419b24ab676SJeff Bonwick cio->io_parent_count++; 420b24ab676SJeff Bonwick 421e14bb325SJeff Bonwick mutex_exit(&pio->io_lock); 422a3f829aeSBill Moore mutex_exit(&cio->io_lock); 423e14bb325SJeff Bonwick } 424e14bb325SJeff Bonwick 425fa9e4066Sahrens static void 426a3f829aeSBill Moore zio_remove_child(zio_t *pio, zio_t *cio, zio_link_t *zl) 427e14bb325SJeff Bonwick { 428a3f829aeSBill Moore ASSERT(zl->zl_parent == pio); 429a3f829aeSBill Moore ASSERT(zl->zl_child == cio); 430e14bb325SJeff Bonwick 431a3f829aeSBill Moore mutex_enter(&cio->io_lock); 432e14bb325SJeff Bonwick mutex_enter(&pio->io_lock); 433a3f829aeSBill Moore 434a3f829aeSBill Moore list_remove(&pio->io_child_list, zl); 435a3f829aeSBill Moore list_remove(&cio->io_parent_list, zl); 436a3f829aeSBill Moore 437b24ab676SJeff Bonwick pio->io_child_count--; 438b24ab676SJeff Bonwick cio->io_parent_count--; 439b24ab676SJeff Bonwick 440e14bb325SJeff Bonwick mutex_exit(&pio->io_lock); 441a3f829aeSBill Moore mutex_exit(&cio->io_lock); 442a3f829aeSBill Moore 443a3f829aeSBill Moore kmem_cache_free(zio_link_cache, zl); 444e14bb325SJeff Bonwick } 445e14bb325SJeff Bonwick 446e14bb325SJeff Bonwick static boolean_t 447d6e1c446SGeorge Wilson zio_wait_for_children(zio_t *zio, uint8_t childbits, enum zio_wait_type wait) 448fa9e4066Sahrens { 449e14bb325SJeff Bonwick boolean_t waiting = B_FALSE; 450e14bb325SJeff Bonwick 451e14bb325SJeff Bonwick mutex_enter(&zio->io_lock); 452e14bb325SJeff Bonwick ASSERT(zio->io_stall == NULL); 453d6e1c446SGeorge Wilson for (int c = 0; c < ZIO_CHILD_TYPES; c++) { 454d6e1c446SGeorge Wilson if (!(ZIO_CHILD_BIT_IS_SET(childbits, c))) 455d6e1c446SGeorge Wilson continue; 456d6e1c446SGeorge Wilson 457d6e1c446SGeorge Wilson uint64_t *countp = &zio->io_children[c][wait]; 458d6e1c446SGeorge Wilson if (*countp != 0) { 459d6e1c446SGeorge Wilson zio->io_stage >>= 1; 460d6e1c446SGeorge Wilson ASSERT3U(zio->io_stage, !=, ZIO_STAGE_OPEN); 461d6e1c446SGeorge Wilson zio->io_stall = countp; 462d6e1c446SGeorge Wilson waiting = B_TRUE; 463d6e1c446SGeorge Wilson break; 464d6e1c446SGeorge Wilson } 465e14bb325SJeff Bonwick } 466e14bb325SJeff Bonwick mutex_exit(&zio->io_lock); 467e14bb325SJeff Bonwick return (waiting); 468e14bb325SJeff Bonwick } 469fa9e4066Sahrens 470e14bb325SJeff Bonwick static void 471e14bb325SJeff Bonwick zio_notify_parent(zio_t *pio, zio_t *zio, enum zio_wait_type wait) 472e14bb325SJeff Bonwick { 473e14bb325SJeff Bonwick uint64_t *countp = &pio->io_children[zio->io_child_type][wait]; 474e14bb325SJeff Bonwick int *errorp = &pio->io_child_error[zio->io_child_type]; 475fa9e4066Sahrens 476e14bb325SJeff Bonwick mutex_enter(&pio->io_lock); 477e14bb325SJeff Bonwick if (zio->io_error && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE)) 478e14bb325SJeff Bonwick *errorp = zio_worst_error(*errorp, zio->io_error); 479e14bb325SJeff Bonwick pio->io_reexecute |= zio->io_reexecute; 480e14bb325SJeff Bonwick ASSERT3U(*countp, >, 0); 48169962b56SMatthew Ahrens 48269962b56SMatthew Ahrens (*countp)--; 48369962b56SMatthew Ahrens 48469962b56SMatthew Ahrens if (*countp == 0 && pio->io_stall == countp) { 4850f7643c7SGeorge Wilson zio_taskq_type_t type = 4860f7643c7SGeorge Wilson pio->io_stage < ZIO_STAGE_VDEV_IO_START ? ZIO_TASKQ_ISSUE : 4870f7643c7SGeorge Wilson ZIO_TASKQ_INTERRUPT; 488e14bb325SJeff Bonwick pio->io_stall = NULL; 489e14bb325SJeff Bonwick mutex_exit(&pio->io_lock); 4900f7643c7SGeorge Wilson /* 4910f7643c7SGeorge Wilson * Dispatch the parent zio in its own taskq so that 4920f7643c7SGeorge Wilson * the child can continue to make progress. This also 4930f7643c7SGeorge Wilson * prevents overflowing the stack when we have deeply nested 4940f7643c7SGeorge Wilson * parent-child relationships. 4950f7643c7SGeorge Wilson */ 4960f7643c7SGeorge Wilson zio_taskq_dispatch(pio, type, B_FALSE); 497e14bb325SJeff Bonwick } else { 498e14bb325SJeff Bonwick mutex_exit(&pio->io_lock); 499fa9e4066Sahrens } 500fa9e4066Sahrens } 501fa9e4066Sahrens 502e14bb325SJeff Bonwick static void 503e14bb325SJeff Bonwick zio_inherit_child_errors(zio_t *zio, enum zio_child c) 504e14bb325SJeff Bonwick { 505e14bb325SJeff Bonwick if (zio->io_child_error[c] != 0 && zio->io_error == 0) 506e14bb325SJeff Bonwick zio->io_error = zio->io_child_error[c]; 507e14bb325SJeff Bonwick } 508e14bb325SJeff Bonwick 5090f7643c7SGeorge Wilson int 51094c2d0ebSMatthew Ahrens zio_bookmark_compare(const void *x1, const void *x2) 5110f7643c7SGeorge Wilson { 5120f7643c7SGeorge Wilson const zio_t *z1 = x1; 5130f7643c7SGeorge Wilson const zio_t *z2 = x2; 5140f7643c7SGeorge Wilson 51594c2d0ebSMatthew Ahrens if (z1->io_bookmark.zb_objset < z2->io_bookmark.zb_objset) 5160f7643c7SGeorge Wilson return (-1); 51794c2d0ebSMatthew Ahrens if (z1->io_bookmark.zb_objset > z2->io_bookmark.zb_objset) 5180f7643c7SGeorge Wilson return (1); 5190f7643c7SGeorge Wilson 52094c2d0ebSMatthew Ahrens if (z1->io_bookmark.zb_object < z2->io_bookmark.zb_object) 5210f7643c7SGeorge Wilson return (-1); 52294c2d0ebSMatthew Ahrens if (z1->io_bookmark.zb_object > z2->io_bookmark.zb_object) 52394c2d0ebSMatthew Ahrens return (1); 52494c2d0ebSMatthew Ahrens 52594c2d0ebSMatthew Ahrens if (z1->io_bookmark.zb_level < z2->io_bookmark.zb_level) 52694c2d0ebSMatthew Ahrens return (-1); 52794c2d0ebSMatthew Ahrens if (z1->io_bookmark.zb_level > z2->io_bookmark.zb_level) 52894c2d0ebSMatthew Ahrens return (1); 52994c2d0ebSMatthew Ahrens 53094c2d0ebSMatthew Ahrens if (z1->io_bookmark.zb_blkid < z2->io_bookmark.zb_blkid) 53194c2d0ebSMatthew Ahrens return (-1); 53294c2d0ebSMatthew Ahrens if (z1->io_bookmark.zb_blkid > z2->io_bookmark.zb_blkid) 5330f7643c7SGeorge Wilson return (1); 5340f7643c7SGeorge Wilson 5350f7643c7SGeorge Wilson if (z1 < z2) 5360f7643c7SGeorge Wilson return (-1); 5370f7643c7SGeorge Wilson if (z1 > z2) 5380f7643c7SGeorge Wilson return (1); 5390f7643c7SGeorge Wilson 5400f7643c7SGeorge Wilson return (0); 5410f7643c7SGeorge Wilson } 5420f7643c7SGeorge Wilson 543fa9e4066Sahrens /* 544fa9e4066Sahrens * ========================================================================== 545e14bb325SJeff Bonwick * Create the various types of I/O (read, write, free, etc) 546fa9e4066Sahrens * ========================================================================== 547fa9e4066Sahrens */ 548fa9e4066Sahrens static zio_t * 549b24ab676SJeff Bonwick zio_create(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp, 550770499e1SDan Kimmel abd_t *data, uint64_t lsize, uint64_t psize, zio_done_func_t *done, 5515602294fSDan Kimmel void *private, zio_type_t type, zio_priority_t priority, 5525602294fSDan Kimmel enum zio_flag flags, vdev_t *vd, uint64_t offset, 5535602294fSDan Kimmel const zbookmark_phys_t *zb, enum zio_stage stage, enum zio_stage pipeline) 554fa9e4066Sahrens { 555fa9e4066Sahrens zio_t *zio; 556fa9e4066Sahrens 5575602294fSDan Kimmel ASSERT3U(psize, <=, SPA_MAXBLOCKSIZE); 5585602294fSDan Kimmel ASSERT(P2PHASE(psize, SPA_MINBLOCKSIZE) == 0); 559e14bb325SJeff Bonwick ASSERT(P2PHASE(offset, SPA_MINBLOCKSIZE) == 0); 560fa9e4066Sahrens 561e14bb325SJeff Bonwick ASSERT(!vd || spa_config_held(spa, SCL_STATE_ALL, RW_READER)); 562e14bb325SJeff Bonwick ASSERT(!bp || !(flags & ZIO_FLAG_CONFIG_WRITER)); 563e14bb325SJeff Bonwick ASSERT(vd || stage == ZIO_STAGE_OPEN); 564088f3894Sahrens 5655602294fSDan Kimmel IMPLY(lsize != psize, (flags & ZIO_FLAG_RAW) != 0); 5665602294fSDan Kimmel 567ccae0b50Seschrock zio = kmem_cache_alloc(zio_cache, KM_SLEEP); 568ccae0b50Seschrock bzero(zio, sizeof (zio_t)); 569e14bb325SJeff Bonwick 570e14bb325SJeff Bonwick mutex_init(&zio->io_lock, NULL, MUTEX_DEFAULT, NULL); 571e14bb325SJeff Bonwick cv_init(&zio->io_cv, NULL, CV_DEFAULT, NULL); 572e14bb325SJeff Bonwick 573a3f829aeSBill Moore list_create(&zio->io_parent_list, sizeof (zio_link_t), 574a3f829aeSBill Moore offsetof(zio_link_t, zl_parent_node)); 575a3f829aeSBill Moore list_create(&zio->io_child_list, sizeof (zio_link_t), 576a3f829aeSBill Moore offsetof(zio_link_t, zl_child_node)); 5778363e80aSGeorge Wilson metaslab_trace_init(&zio->io_alloc_list); 578a3f829aeSBill Moore 579e14bb325SJeff Bonwick if (vd != NULL) 580e14bb325SJeff Bonwick zio->io_child_type = ZIO_CHILD_VDEV; 581e14bb325SJeff Bonwick else if (flags & ZIO_FLAG_GANG_CHILD) 582e14bb325SJeff Bonwick zio->io_child_type = ZIO_CHILD_GANG; 583b24ab676SJeff Bonwick else if (flags & ZIO_FLAG_DDT_CHILD) 584b24ab676SJeff Bonwick zio->io_child_type = ZIO_CHILD_DDT; 585e14bb325SJeff Bonwick else 586e14bb325SJeff Bonwick zio->io_child_type = ZIO_CHILD_LOGICAL; 587e14bb325SJeff Bonwick 588fa9e4066Sahrens if (bp != NULL) { 589b24ab676SJeff Bonwick zio->io_bp = (blkptr_t *)bp; 590fa9e4066Sahrens zio->io_bp_copy = *bp; 591fa9e4066Sahrens zio->io_bp_orig = *bp; 592b24ab676SJeff Bonwick if (type != ZIO_TYPE_WRITE || 593b24ab676SJeff Bonwick zio->io_child_type == ZIO_CHILD_DDT) 594e14bb325SJeff Bonwick zio->io_bp = &zio->io_bp_copy; /* so caller can free */ 595f5383399SBill Moore if (zio->io_child_type == ZIO_CHILD_LOGICAL) 596e14bb325SJeff Bonwick zio->io_logical = zio; 597f5383399SBill Moore if (zio->io_child_type > ZIO_CHILD_GANG && BP_IS_GANG(bp)) 598f5383399SBill Moore pipeline |= ZIO_GANG_STAGES; 599fa9e4066Sahrens } 600e14bb325SJeff Bonwick 601e14bb325SJeff Bonwick zio->io_spa = spa; 602e14bb325SJeff Bonwick zio->io_txg = txg; 603fa9e4066Sahrens zio->io_done = done; 604fa9e4066Sahrens zio->io_private = private; 605fa9e4066Sahrens zio->io_type = type; 606fa9e4066Sahrens zio->io_priority = priority; 607e14bb325SJeff Bonwick zio->io_vd = vd; 608e14bb325SJeff Bonwick zio->io_offset = offset; 609770499e1SDan Kimmel zio->io_orig_abd = zio->io_abd = data; 6105602294fSDan Kimmel zio->io_orig_size = zio->io_size = psize; 6115602294fSDan Kimmel zio->io_lsize = lsize; 612e14bb325SJeff Bonwick zio->io_orig_flags = zio->io_flags = flags; 613e14bb325SJeff Bonwick zio->io_orig_stage = zio->io_stage = stage; 614e14bb325SJeff Bonwick zio->io_orig_pipeline = zio->io_pipeline = pipeline; 6150f7643c7SGeorge Wilson zio->io_pipeline_trace = ZIO_STAGE_OPEN; 616fa9e4066Sahrens 617a3f829aeSBill Moore zio->io_state[ZIO_WAIT_READY] = (stage >= ZIO_STAGE_READY); 618a3f829aeSBill Moore zio->io_state[ZIO_WAIT_DONE] = (stage >= ZIO_STAGE_DONE); 619a3f829aeSBill Moore 620e14bb325SJeff Bonwick if (zb != NULL) 621e14bb325SJeff Bonwick zio->io_bookmark = *zb; 622e14bb325SJeff Bonwick 623e14bb325SJeff Bonwick if (pio != NULL) { 624*663207adSDon Brady if (zio->io_metaslab_class == NULL) 625*663207adSDon Brady zio->io_metaslab_class = pio->io_metaslab_class; 626e14bb325SJeff Bonwick if (zio->io_logical == NULL) 627ea8dc4b6Seschrock zio->io_logical = pio->io_logical; 628f5383399SBill Moore if (zio->io_child_type == ZIO_CHILD_GANG) 629f5383399SBill Moore zio->io_gang_leader = pio->io_gang_leader; 630e14bb325SJeff Bonwick zio_add_child(pio, zio); 631fa9e4066Sahrens } 632fa9e4066Sahrens 633fa9e4066Sahrens return (zio); 634fa9e4066Sahrens } 635fa9e4066Sahrens 6360a4e9518Sgw static void 637e14bb325SJeff Bonwick zio_destroy(zio_t *zio) 6380a4e9518Sgw { 6398363e80aSGeorge Wilson metaslab_trace_fini(&zio->io_alloc_list); 640a3f829aeSBill Moore list_destroy(&zio->io_parent_list); 641a3f829aeSBill Moore list_destroy(&zio->io_child_list); 642e14bb325SJeff Bonwick mutex_destroy(&zio->io_lock); 643e14bb325SJeff Bonwick cv_destroy(&zio->io_cv); 644e14bb325SJeff Bonwick kmem_cache_free(zio_cache, zio); 6450a4e9518Sgw } 6460a4e9518Sgw 647fa9e4066Sahrens zio_t * 648a3f829aeSBill Moore zio_null(zio_t *pio, spa_t *spa, vdev_t *vd, zio_done_func_t *done, 649b24ab676SJeff Bonwick void *private, enum zio_flag flags) 650fa9e4066Sahrens { 651fa9e4066Sahrens zio_t *zio; 652fa9e4066Sahrens 6535602294fSDan Kimmel zio = zio_create(pio, spa, 0, NULL, NULL, 0, 0, done, private, 654a3f829aeSBill Moore ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL, 655e14bb325SJeff Bonwick ZIO_STAGE_OPEN, ZIO_INTERLOCK_PIPELINE); 656fa9e4066Sahrens 657fa9e4066Sahrens return (zio); 658fa9e4066Sahrens } 659fa9e4066Sahrens 660fa9e4066Sahrens zio_t * 661b24ab676SJeff Bonwick zio_root(spa_t *spa, zio_done_func_t *done, void *private, enum zio_flag flags) 662fa9e4066Sahrens { 663a3f829aeSBill Moore return (zio_null(NULL, spa, NULL, done, private, flags)); 664fa9e4066Sahrens } 665fa9e4066Sahrens 666f63ab3d5SMatthew Ahrens void 667f63ab3d5SMatthew Ahrens zfs_blkptr_verify(spa_t *spa, const blkptr_t *bp) 668f63ab3d5SMatthew Ahrens { 669f63ab3d5SMatthew Ahrens if (!DMU_OT_IS_VALID(BP_GET_TYPE(bp))) { 670f63ab3d5SMatthew Ahrens zfs_panic_recover("blkptr at %p has invalid TYPE %llu", 671f63ab3d5SMatthew Ahrens bp, (longlong_t)BP_GET_TYPE(bp)); 672f63ab3d5SMatthew Ahrens } 673f63ab3d5SMatthew Ahrens if (BP_GET_CHECKSUM(bp) >= ZIO_CHECKSUM_FUNCTIONS || 674f63ab3d5SMatthew Ahrens BP_GET_CHECKSUM(bp) <= ZIO_CHECKSUM_ON) { 675f63ab3d5SMatthew Ahrens zfs_panic_recover("blkptr at %p has invalid CHECKSUM %llu", 676f63ab3d5SMatthew Ahrens bp, (longlong_t)BP_GET_CHECKSUM(bp)); 677f63ab3d5SMatthew Ahrens } 678f63ab3d5SMatthew Ahrens if (BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_FUNCTIONS || 679f63ab3d5SMatthew Ahrens BP_GET_COMPRESS(bp) <= ZIO_COMPRESS_ON) { 680f63ab3d5SMatthew Ahrens zfs_panic_recover("blkptr at %p has invalid COMPRESS %llu", 681f63ab3d5SMatthew Ahrens bp, (longlong_t)BP_GET_COMPRESS(bp)); 682f63ab3d5SMatthew Ahrens } 683f63ab3d5SMatthew Ahrens if (BP_GET_LSIZE(bp) > SPA_MAXBLOCKSIZE) { 684f63ab3d5SMatthew Ahrens zfs_panic_recover("blkptr at %p has invalid LSIZE %llu", 685f63ab3d5SMatthew Ahrens bp, (longlong_t)BP_GET_LSIZE(bp)); 686f63ab3d5SMatthew Ahrens } 687f63ab3d5SMatthew Ahrens if (BP_GET_PSIZE(bp) > SPA_MAXBLOCKSIZE) { 688f63ab3d5SMatthew Ahrens zfs_panic_recover("blkptr at %p has invalid PSIZE %llu", 689f63ab3d5SMatthew Ahrens bp, (longlong_t)BP_GET_PSIZE(bp)); 690f63ab3d5SMatthew Ahrens } 691f63ab3d5SMatthew Ahrens 692f63ab3d5SMatthew Ahrens if (BP_IS_EMBEDDED(bp)) { 693f63ab3d5SMatthew Ahrens if (BPE_GET_ETYPE(bp) > NUM_BP_EMBEDDED_TYPES) { 694f63ab3d5SMatthew Ahrens zfs_panic_recover("blkptr at %p has invalid ETYPE %llu", 695f63ab3d5SMatthew Ahrens bp, (longlong_t)BPE_GET_ETYPE(bp)); 696f63ab3d5SMatthew Ahrens } 697f63ab3d5SMatthew Ahrens } 698f63ab3d5SMatthew Ahrens 6996f793812SPavel Zakharov /* 7006f793812SPavel Zakharov * Do not verify individual DVAs if the config is not trusted. This 7016f793812SPavel Zakharov * will be done once the zio is executed in vdev_mirror_map_alloc. 7026f793812SPavel Zakharov */ 7036f793812SPavel Zakharov if (!spa->spa_trust_config) 7046f793812SPavel Zakharov return; 7056f793812SPavel Zakharov 706f63ab3d5SMatthew Ahrens /* 707f63ab3d5SMatthew Ahrens * Pool-specific checks. 708f63ab3d5SMatthew Ahrens * 709f63ab3d5SMatthew Ahrens * Note: it would be nice to verify that the blk_birth and 710f63ab3d5SMatthew Ahrens * BP_PHYSICAL_BIRTH() are not too large. However, spa_freeze() 711f63ab3d5SMatthew Ahrens * allows the birth time of log blocks (and dmu_sync()-ed blocks 712f63ab3d5SMatthew Ahrens * that are in the log) to be arbitrarily large. 713f63ab3d5SMatthew Ahrens */ 714f63ab3d5SMatthew Ahrens for (int i = 0; i < BP_GET_NDVAS(bp); i++) { 715f63ab3d5SMatthew Ahrens uint64_t vdevid = DVA_GET_VDEV(&bp->blk_dva[i]); 716f63ab3d5SMatthew Ahrens if (vdevid >= spa->spa_root_vdev->vdev_children) { 717f63ab3d5SMatthew Ahrens zfs_panic_recover("blkptr at %p DVA %u has invalid " 718f63ab3d5SMatthew Ahrens "VDEV %llu", 719f63ab3d5SMatthew Ahrens bp, i, (longlong_t)vdevid); 7205897eb49SJustin Gibbs continue; 721f63ab3d5SMatthew Ahrens } 722f63ab3d5SMatthew Ahrens vdev_t *vd = spa->spa_root_vdev->vdev_child[vdevid]; 723f63ab3d5SMatthew Ahrens if (vd == NULL) { 724f63ab3d5SMatthew Ahrens zfs_panic_recover("blkptr at %p DVA %u has invalid " 725f63ab3d5SMatthew Ahrens "VDEV %llu", 726f63ab3d5SMatthew Ahrens bp, i, (longlong_t)vdevid); 7275897eb49SJustin Gibbs continue; 728f63ab3d5SMatthew Ahrens } 729f63ab3d5SMatthew Ahrens if (vd->vdev_ops == &vdev_hole_ops) { 730f63ab3d5SMatthew Ahrens zfs_panic_recover("blkptr at %p DVA %u has hole " 731f63ab3d5SMatthew Ahrens "VDEV %llu", 732f63ab3d5SMatthew Ahrens bp, i, (longlong_t)vdevid); 7335897eb49SJustin Gibbs continue; 734f63ab3d5SMatthew Ahrens } 735f63ab3d5SMatthew Ahrens if (vd->vdev_ops == &vdev_missing_ops) { 736f63ab3d5SMatthew Ahrens /* 737f63ab3d5SMatthew Ahrens * "missing" vdevs are valid during import, but we 738f63ab3d5SMatthew Ahrens * don't have their detailed info (e.g. asize), so 739f63ab3d5SMatthew Ahrens * we can't perform any more checks on them. 740f63ab3d5SMatthew Ahrens */ 741f63ab3d5SMatthew Ahrens continue; 742f63ab3d5SMatthew Ahrens } 743f63ab3d5SMatthew Ahrens uint64_t offset = DVA_GET_OFFSET(&bp->blk_dva[i]); 744f63ab3d5SMatthew Ahrens uint64_t asize = DVA_GET_ASIZE(&bp->blk_dva[i]); 745f63ab3d5SMatthew Ahrens if (BP_IS_GANG(bp)) 746f63ab3d5SMatthew Ahrens asize = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE); 747f63ab3d5SMatthew Ahrens if (offset + asize > vd->vdev_asize) { 748f63ab3d5SMatthew Ahrens zfs_panic_recover("blkptr at %p DVA %u has invalid " 749f63ab3d5SMatthew Ahrens "OFFSET %llu", 750f63ab3d5SMatthew Ahrens bp, i, (longlong_t)offset); 751f63ab3d5SMatthew Ahrens } 752f63ab3d5SMatthew Ahrens } 753f63ab3d5SMatthew Ahrens } 754f63ab3d5SMatthew Ahrens 7556f793812SPavel Zakharov boolean_t 7566f793812SPavel Zakharov zfs_dva_valid(spa_t *spa, const dva_t *dva, const blkptr_t *bp) 7576f793812SPavel Zakharov { 7586f793812SPavel Zakharov uint64_t vdevid = DVA_GET_VDEV(dva); 7596f793812SPavel Zakharov 7606f793812SPavel Zakharov if (vdevid >= spa->spa_root_vdev->vdev_children) 7616f793812SPavel Zakharov return (B_FALSE); 7626f793812SPavel Zakharov 7636f793812SPavel Zakharov vdev_t *vd = spa->spa_root_vdev->vdev_child[vdevid]; 7646f793812SPavel Zakharov if (vd == NULL) 7656f793812SPavel Zakharov return (B_FALSE); 7666f793812SPavel Zakharov 7676f793812SPavel Zakharov if (vd->vdev_ops == &vdev_hole_ops) 7686f793812SPavel Zakharov return (B_FALSE); 7696f793812SPavel Zakharov 7706f793812SPavel Zakharov if (vd->vdev_ops == &vdev_missing_ops) { 7716f793812SPavel Zakharov return (B_FALSE); 7726f793812SPavel Zakharov } 7736f793812SPavel Zakharov 7746f793812SPavel Zakharov uint64_t offset = DVA_GET_OFFSET(dva); 7756f793812SPavel Zakharov uint64_t asize = DVA_GET_ASIZE(dva); 7766f793812SPavel Zakharov 7776f793812SPavel Zakharov if (BP_IS_GANG(bp)) 7786f793812SPavel Zakharov asize = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE); 7796f793812SPavel Zakharov if (offset + asize > vd->vdev_asize) 7806f793812SPavel Zakharov return (B_FALSE); 7816f793812SPavel Zakharov 7826f793812SPavel Zakharov return (B_TRUE); 7836f793812SPavel Zakharov } 7846f793812SPavel Zakharov 785fa9e4066Sahrens zio_t * 786e14bb325SJeff Bonwick zio_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, 787770499e1SDan Kimmel abd_t *data, uint64_t size, zio_done_func_t *done, void *private, 7887802d7bfSMatthew Ahrens zio_priority_t priority, enum zio_flag flags, const zbookmark_phys_t *zb) 789fa9e4066Sahrens { 790fa9e4066Sahrens zio_t *zio; 791fa9e4066Sahrens 792f63ab3d5SMatthew Ahrens zfs_blkptr_verify(spa, bp); 793f63ab3d5SMatthew Ahrens 794b24ab676SJeff Bonwick zio = zio_create(pio, spa, BP_PHYSICAL_BIRTH(bp), bp, 7955602294fSDan Kimmel data, size, size, done, private, 796e14bb325SJeff Bonwick ZIO_TYPE_READ, priority, flags, NULL, 0, zb, 797b24ab676SJeff Bonwick ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ? 798b24ab676SJeff Bonwick ZIO_DDT_CHILD_READ_PIPELINE : ZIO_READ_PIPELINE); 799fa9e4066Sahrens 800fa9e4066Sahrens return (zio); 801fa9e4066Sahrens } 802fa9e4066Sahrens 803fa9e4066Sahrens zio_t * 804e14bb325SJeff Bonwick zio_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, 805770499e1SDan Kimmel abd_t *data, uint64_t lsize, uint64_t psize, const zio_prop_t *zp, 8068df0bcf0SPaul Dagnelie zio_done_func_t *ready, zio_done_func_t *children_ready, 8078df0bcf0SPaul Dagnelie zio_done_func_t *physdone, zio_done_func_t *done, 8088df0bcf0SPaul Dagnelie void *private, zio_priority_t priority, enum zio_flag flags, 8098df0bcf0SPaul Dagnelie const zbookmark_phys_t *zb) 810fa9e4066Sahrens { 811fa9e4066Sahrens zio_t *zio; 812fa9e4066Sahrens 813e14bb325SJeff Bonwick ASSERT(zp->zp_checksum >= ZIO_CHECKSUM_OFF && 814e14bb325SJeff Bonwick zp->zp_checksum < ZIO_CHECKSUM_FUNCTIONS && 815e14bb325SJeff Bonwick zp->zp_compress >= ZIO_COMPRESS_OFF && 816e14bb325SJeff Bonwick zp->zp_compress < ZIO_COMPRESS_FUNCTIONS && 817ad135b5dSChristopher Siden DMU_OT_IS_VALID(zp->zp_type) && 818e14bb325SJeff Bonwick zp->zp_level < 32 && 819b24ab676SJeff Bonwick zp->zp_copies > 0 && 82080901aeaSGeorge Wilson zp->zp_copies <= spa_max_replication(spa)); 8210a4e9518Sgw 8225602294fSDan Kimmel zio = zio_create(pio, spa, txg, bp, data, lsize, psize, done, private, 823e14bb325SJeff Bonwick ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb, 824b24ab676SJeff Bonwick ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ? 825b24ab676SJeff Bonwick ZIO_DDT_CHILD_WRITE_PIPELINE : ZIO_WRITE_PIPELINE); 826fa9e4066Sahrens 827c717a561Smaybee zio->io_ready = ready; 8288df0bcf0SPaul Dagnelie zio->io_children_ready = children_ready; 82969962b56SMatthew Ahrens zio->io_physdone = physdone; 830e14bb325SJeff Bonwick zio->io_prop = *zp; 831fa9e4066Sahrens 8325d7b4d43SMatthew Ahrens /* 8335d7b4d43SMatthew Ahrens * Data can be NULL if we are going to call zio_write_override() to 8345d7b4d43SMatthew Ahrens * provide the already-allocated BP. But we may need the data to 8355d7b4d43SMatthew Ahrens * verify a dedup hit (if requested). In this case, don't try to 8365d7b4d43SMatthew Ahrens * dedup (just take the already-allocated BP verbatim). 8375d7b4d43SMatthew Ahrens */ 8385d7b4d43SMatthew Ahrens if (data == NULL && zio->io_prop.zp_dedup_verify) { 8395d7b4d43SMatthew Ahrens zio->io_prop.zp_dedup = zio->io_prop.zp_dedup_verify = B_FALSE; 8405d7b4d43SMatthew Ahrens } 8415d7b4d43SMatthew Ahrens 842fa9e4066Sahrens return (zio); 843fa9e4066Sahrens } 844fa9e4066Sahrens 845fa9e4066Sahrens zio_t * 846770499e1SDan Kimmel zio_rewrite(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, abd_t *data, 84769962b56SMatthew Ahrens uint64_t size, zio_done_func_t *done, void *private, 8487802d7bfSMatthew Ahrens zio_priority_t priority, enum zio_flag flags, zbookmark_phys_t *zb) 849fa9e4066Sahrens { 850fa9e4066Sahrens zio_t *zio; 851fa9e4066Sahrens 8525602294fSDan Kimmel zio = zio_create(pio, spa, txg, bp, data, size, size, done, private, 8530f7643c7SGeorge Wilson ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_IO_REWRITE, NULL, 0, zb, 854e14bb325SJeff Bonwick ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE); 855fa9e4066Sahrens 856fa9e4066Sahrens return (zio); 857fa9e4066Sahrens } 858fa9e4066Sahrens 859b24ab676SJeff Bonwick void 86080901aeaSGeorge Wilson zio_write_override(zio_t *zio, blkptr_t *bp, int copies, boolean_t nopwrite) 861b24ab676SJeff Bonwick { 862b24ab676SJeff Bonwick ASSERT(zio->io_type == ZIO_TYPE_WRITE); 863b24ab676SJeff Bonwick ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 864b24ab676SJeff Bonwick ASSERT(zio->io_stage == ZIO_STAGE_OPEN); 865b24ab676SJeff Bonwick ASSERT(zio->io_txg == spa_syncing_txg(zio->io_spa)); 866b24ab676SJeff Bonwick 86780901aeaSGeorge Wilson /* 86880901aeaSGeorge Wilson * We must reset the io_prop to match the values that existed 86980901aeaSGeorge Wilson * when the bp was first written by dmu_sync() keeping in mind 87080901aeaSGeorge Wilson * that nopwrite and dedup are mutually exclusive. 87180901aeaSGeorge Wilson */ 87280901aeaSGeorge Wilson zio->io_prop.zp_dedup = nopwrite ? B_FALSE : zio->io_prop.zp_dedup; 87380901aeaSGeorge Wilson zio->io_prop.zp_nopwrite = nopwrite; 874b24ab676SJeff Bonwick zio->io_prop.zp_copies = copies; 875b24ab676SJeff Bonwick zio->io_bp_override = bp; 876b24ab676SJeff Bonwick } 877b24ab676SJeff Bonwick 878b24ab676SJeff Bonwick void 879b24ab676SJeff Bonwick zio_free(spa_t *spa, uint64_t txg, const blkptr_t *bp) 880b24ab676SJeff Bonwick { 8815d7b4d43SMatthew Ahrens 8825cabbc6bSPrashanth Sreenivasa zfs_blkptr_verify(spa, bp); 8835cabbc6bSPrashanth Sreenivasa 8845d7b4d43SMatthew Ahrens /* 8855d7b4d43SMatthew Ahrens * The check for EMBEDDED is a performance optimization. We 8865d7b4d43SMatthew Ahrens * process the free here (by ignoring it) rather than 8875d7b4d43SMatthew Ahrens * putting it on the list and then processing it in zio_free_sync(). 8885d7b4d43SMatthew Ahrens */ 8895d7b4d43SMatthew Ahrens if (BP_IS_EMBEDDED(bp)) 8905d7b4d43SMatthew Ahrens return; 8913b2aab18SMatthew Ahrens metaslab_check_free(spa, bp); 8929cb154a3SMatthew Ahrens 8939cb154a3SMatthew Ahrens /* 8949cb154a3SMatthew Ahrens * Frees that are for the currently-syncing txg, are not going to be 8959cb154a3SMatthew Ahrens * deferred, and which will not need to do a read (i.e. not GANG or 8969cb154a3SMatthew Ahrens * DEDUP), can be processed immediately. Otherwise, put them on the 8979cb154a3SMatthew Ahrens * in-memory list for later processing. 8989cb154a3SMatthew Ahrens */ 8999cb154a3SMatthew Ahrens if (BP_IS_GANG(bp) || BP_GET_DEDUP(bp) || 9009cb154a3SMatthew Ahrens txg != spa->spa_syncing_txg || 9019cb154a3SMatthew Ahrens spa_sync_pass(spa) >= zfs_sync_pass_deferred_free) { 9029cb154a3SMatthew Ahrens bplist_append(&spa->spa_free_bplist[txg & TXG_MASK], bp); 9039cb154a3SMatthew Ahrens } else { 9049cb154a3SMatthew Ahrens VERIFY0(zio_wait(zio_free_sync(NULL, spa, txg, bp, 0))); 9059cb154a3SMatthew Ahrens } 906b24ab676SJeff Bonwick } 907b24ab676SJeff Bonwick 908fa9e4066Sahrens zio_t * 909b24ab676SJeff Bonwick zio_free_sync(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp, 910b24ab676SJeff Bonwick enum zio_flag flags) 911fa9e4066Sahrens { 912fa9e4066Sahrens zio_t *zio; 9139cb154a3SMatthew Ahrens enum zio_stage stage = ZIO_FREE_PIPELINE; 914fa9e4066Sahrens 915fa9e4066Sahrens ASSERT(!BP_IS_HOLE(bp)); 916b24ab676SJeff Bonwick ASSERT(spa_syncing_txg(spa) == txg); 91701f55e48SGeorge Wilson ASSERT(spa_sync_pass(spa) < zfs_sync_pass_deferred_free); 918fa9e4066Sahrens 9195d7b4d43SMatthew Ahrens if (BP_IS_EMBEDDED(bp)) 9205d7b4d43SMatthew Ahrens return (zio_null(pio, spa, NULL, NULL, NULL, 0)); 9215d7b4d43SMatthew Ahrens 9223b2aab18SMatthew Ahrens metaslab_check_free(spa, bp); 9236e6d5868SMatthew Ahrens arc_freed(spa, bp); 9243b2aab18SMatthew Ahrens 9259cb154a3SMatthew Ahrens /* 9269cb154a3SMatthew Ahrens * GANG and DEDUP blocks can induce a read (for the gang block header, 9279cb154a3SMatthew Ahrens * or the DDT), so issue them asynchronously so that this thread is 9289cb154a3SMatthew Ahrens * not tied up. 9299cb154a3SMatthew Ahrens */ 9309cb154a3SMatthew Ahrens if (BP_IS_GANG(bp) || BP_GET_DEDUP(bp)) 9319cb154a3SMatthew Ahrens stage |= ZIO_STAGE_ISSUE_ASYNC; 9329cb154a3SMatthew Ahrens 933e14bb325SJeff Bonwick zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp), 9345602294fSDan Kimmel BP_GET_PSIZE(bp), NULL, NULL, ZIO_TYPE_FREE, ZIO_PRIORITY_NOW, 9355602294fSDan Kimmel flags, NULL, 0, NULL, ZIO_STAGE_OPEN, stage); 9369cb154a3SMatthew Ahrens 937fa9e4066Sahrens return (zio); 938fa9e4066Sahrens } 939fa9e4066Sahrens 940fa9e4066Sahrens zio_t * 941b24ab676SJeff Bonwick zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp, 942b24ab676SJeff Bonwick zio_done_func_t *done, void *private, enum zio_flag flags) 943fa9e4066Sahrens { 944fa9e4066Sahrens zio_t *zio; 945fa9e4066Sahrens 9465cabbc6bSPrashanth Sreenivasa zfs_blkptr_verify(spa, bp); 9475d7b4d43SMatthew Ahrens 9485d7b4d43SMatthew Ahrens if (BP_IS_EMBEDDED(bp)) 9495d7b4d43SMatthew Ahrens return (zio_null(pio, spa, NULL, NULL, NULL, 0)); 9505d7b4d43SMatthew Ahrens 951fa9e4066Sahrens /* 952fa9e4066Sahrens * A claim is an allocation of a specific block. Claims are needed 953fa9e4066Sahrens * to support immediate writes in the intent log. The issue is that 954fa9e4066Sahrens * immediate writes contain committed data, but in a txg that was 955fa9e4066Sahrens * *not* committed. Upon opening the pool after an unclean shutdown, 956fa9e4066Sahrens * the intent log claims all blocks that contain immediate write data 957fa9e4066Sahrens * so that the SPA knows they're in use. 958fa9e4066Sahrens * 959fa9e4066Sahrens * All claims *must* be resolved in the first txg -- before the SPA 960fa9e4066Sahrens * starts allocating blocks -- so that nothing is allocated twice. 961b24ab676SJeff Bonwick * If txg == 0 we just verify that the block is claimable. 962fa9e4066Sahrens */ 96386714001SSerapheim Dimitropoulos ASSERT3U(spa->spa_uberblock.ub_rootbp.blk_birth, <, 96486714001SSerapheim Dimitropoulos spa_min_claim_txg(spa)); 96586714001SSerapheim Dimitropoulos ASSERT(txg == spa_min_claim_txg(spa) || txg == 0); 966b24ab676SJeff Bonwick ASSERT(!BP_GET_DEDUP(bp) || !spa_writeable(spa)); /* zdb(1M) */ 967fa9e4066Sahrens 968e14bb325SJeff Bonwick zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp), 9695602294fSDan Kimmel BP_GET_PSIZE(bp), done, private, ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW, 9705602294fSDan Kimmel flags, NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE); 9710f7643c7SGeorge Wilson ASSERT0(zio->io_queued_timestamp); 972fa9e4066Sahrens 973fa9e4066Sahrens return (zio); 974fa9e4066Sahrens } 975fa9e4066Sahrens 976fa9e4066Sahrens zio_t * 977fa9e4066Sahrens zio_ioctl(zio_t *pio, spa_t *spa, vdev_t *vd, int cmd, 97869962b56SMatthew Ahrens zio_done_func_t *done, void *private, enum zio_flag flags) 979fa9e4066Sahrens { 980fa9e4066Sahrens zio_t *zio; 981fa9e4066Sahrens int c; 982fa9e4066Sahrens 983fa9e4066Sahrens if (vd->vdev_children == 0) { 9845602294fSDan Kimmel zio = zio_create(pio, spa, 0, NULL, NULL, 0, 0, done, private, 98569962b56SMatthew Ahrens ZIO_TYPE_IOCTL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL, 986fa9e4066Sahrens ZIO_STAGE_OPEN, ZIO_IOCTL_PIPELINE); 987fa9e4066Sahrens 988fa9e4066Sahrens zio->io_cmd = cmd; 989fa9e4066Sahrens } else { 990a3f829aeSBill Moore zio = zio_null(pio, spa, NULL, NULL, NULL, flags); 991fa9e4066Sahrens 992fa9e4066Sahrens for (c = 0; c < vd->vdev_children; c++) 993fa9e4066Sahrens zio_nowait(zio_ioctl(zio, spa, vd->vdev_child[c], cmd, 99469962b56SMatthew Ahrens done, private, flags)); 995fa9e4066Sahrens } 996fa9e4066Sahrens 997fa9e4066Sahrens return (zio); 998fa9e4066Sahrens } 999fa9e4066Sahrens 1000fa9e4066Sahrens zio_t * 1001fa9e4066Sahrens zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size, 1002770499e1SDan Kimmel abd_t *data, int checksum, zio_done_func_t *done, void *private, 100369962b56SMatthew Ahrens zio_priority_t priority, enum zio_flag flags, boolean_t labels) 1004fa9e4066Sahrens { 1005fa9e4066Sahrens zio_t *zio; 10060a4e9518Sgw 1007e14bb325SJeff Bonwick ASSERT(vd->vdev_children == 0); 1008e14bb325SJeff Bonwick ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE || 1009e14bb325SJeff Bonwick offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE); 1010e14bb325SJeff Bonwick ASSERT3U(offset + size, <=, vd->vdev_psize); 1011fa9e4066Sahrens 10125602294fSDan Kimmel zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, size, done, 10135602294fSDan Kimmel private, ZIO_TYPE_READ, priority, flags | ZIO_FLAG_PHYSICAL, vd, 10145602294fSDan Kimmel offset, NULL, ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE); 1015fa9e4066Sahrens 1016e14bb325SJeff Bonwick zio->io_prop.zp_checksum = checksum; 1017fa9e4066Sahrens 1018fa9e4066Sahrens return (zio); 1019fa9e4066Sahrens } 1020fa9e4066Sahrens 1021fa9e4066Sahrens zio_t * 1022fa9e4066Sahrens zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size, 1023770499e1SDan Kimmel abd_t *data, int checksum, zio_done_func_t *done, void *private, 102469962b56SMatthew Ahrens zio_priority_t priority, enum zio_flag flags, boolean_t labels) 1025fa9e4066Sahrens { 1026fa9e4066Sahrens zio_t *zio; 10270a4e9518Sgw 1028e14bb325SJeff Bonwick ASSERT(vd->vdev_children == 0); 1029e14bb325SJeff Bonwick ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE || 1030e14bb325SJeff Bonwick offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE); 1031e14bb325SJeff Bonwick ASSERT3U(offset + size, <=, vd->vdev_psize); 1032fa9e4066Sahrens 10335602294fSDan Kimmel zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, size, done, 10345602294fSDan Kimmel private, ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_PHYSICAL, vd, 10355602294fSDan Kimmel offset, NULL, ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE); 1036fa9e4066Sahrens 1037e14bb325SJeff Bonwick zio->io_prop.zp_checksum = checksum; 1038fa9e4066Sahrens 103945818ee1SMatthew Ahrens if (zio_checksum_table[checksum].ci_flags & ZCHECKSUM_FLAG_EMBEDDED) { 1040fa9e4066Sahrens /* 10416e1f5caaSNeil Perrin * zec checksums are necessarily destructive -- they modify 1042e14bb325SJeff Bonwick * the end of the write buffer to hold the verifier/checksum. 1043fa9e4066Sahrens * Therefore, we must make a local copy in case the data is 1044e14bb325SJeff Bonwick * being written to multiple places in parallel. 1045fa9e4066Sahrens */ 1046770499e1SDan Kimmel abd_t *wbuf = abd_alloc_sametype(data, size); 1047770499e1SDan Kimmel abd_copy(wbuf, data, size); 1048770499e1SDan Kimmel 1049e14bb325SJeff Bonwick zio_push_transform(zio, wbuf, size, size, NULL); 1050fa9e4066Sahrens } 1051fa9e4066Sahrens 1052fa9e4066Sahrens return (zio); 1053fa9e4066Sahrens } 1054fa9e4066Sahrens 1055fa9e4066Sahrens /* 1056e14bb325SJeff Bonwick * Create a child I/O to do some work for us. 1057fa9e4066Sahrens */ 1058fa9e4066Sahrens zio_t * 1059e14bb325SJeff Bonwick zio_vdev_child_io(zio_t *pio, blkptr_t *bp, vdev_t *vd, uint64_t offset, 1060770499e1SDan Kimmel abd_t *data, uint64_t size, int type, zio_priority_t priority, 1061dcbf3bd6SGeorge Wilson enum zio_flag flags, zio_done_func_t *done, void *private) 1062fa9e4066Sahrens { 1063b24ab676SJeff Bonwick enum zio_stage pipeline = ZIO_VDEV_CHILD_PIPELINE; 1064e14bb325SJeff Bonwick zio_t *zio; 1065e14bb325SJeff Bonwick 10665cabbc6bSPrashanth Sreenivasa /* 10675cabbc6bSPrashanth Sreenivasa * vdev child I/Os do not propagate their error to the parent. 10685cabbc6bSPrashanth Sreenivasa * Therefore, for correct operation the caller *must* check for 10695cabbc6bSPrashanth Sreenivasa * and handle the error in the child i/o's done callback. 10705cabbc6bSPrashanth Sreenivasa * The only exceptions are i/os that we don't care about 10715cabbc6bSPrashanth Sreenivasa * (OPTIONAL or REPAIR). 10725cabbc6bSPrashanth Sreenivasa */ 10735cabbc6bSPrashanth Sreenivasa ASSERT((flags & ZIO_FLAG_OPTIONAL) || (flags & ZIO_FLAG_IO_REPAIR) || 10745cabbc6bSPrashanth Sreenivasa done != NULL); 10755cabbc6bSPrashanth Sreenivasa 1076fa9e4066Sahrens if (type == ZIO_TYPE_READ && bp != NULL) { 1077fa9e4066Sahrens /* 1078fa9e4066Sahrens * If we have the bp, then the child should perform the 1079fa9e4066Sahrens * checksum and the parent need not. This pushes error 1080fa9e4066Sahrens * detection as close to the leaves as possible and 1081fa9e4066Sahrens * eliminates redundant checksums in the interior nodes. 1082fa9e4066Sahrens */ 1083b24ab676SJeff Bonwick pipeline |= ZIO_STAGE_CHECKSUM_VERIFY; 1084b24ab676SJeff Bonwick pio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY; 1085fa9e4066Sahrens } 1086fa9e4066Sahrens 10875cabbc6bSPrashanth Sreenivasa if (vd->vdev_ops->vdev_op_leaf) { 10885cabbc6bSPrashanth Sreenivasa ASSERT0(vd->vdev_children); 1089e14bb325SJeff Bonwick offset += VDEV_LABEL_START_SIZE; 10905cabbc6bSPrashanth Sreenivasa } 1091e14bb325SJeff Bonwick 10925cabbc6bSPrashanth Sreenivasa flags |= ZIO_VDEV_CHILD_FLAGS(pio); 1093b24ab676SJeff Bonwick 1094b24ab676SJeff Bonwick /* 1095b24ab676SJeff Bonwick * If we've decided to do a repair, the write is not speculative -- 1096b24ab676SJeff Bonwick * even if the original read was. 1097b24ab676SJeff Bonwick */ 1098b24ab676SJeff Bonwick if (flags & ZIO_FLAG_IO_REPAIR) 1099b24ab676SJeff Bonwick flags &= ~ZIO_FLAG_SPECULATIVE; 1100b24ab676SJeff Bonwick 11010f7643c7SGeorge Wilson /* 11020f7643c7SGeorge Wilson * If we're creating a child I/O that is not associated with a 11030f7643c7SGeorge Wilson * top-level vdev, then the child zio is not an allocating I/O. 11040f7643c7SGeorge Wilson * If this is a retried I/O then we ignore it since we will 11050f7643c7SGeorge Wilson * have already processed the original allocating I/O. 11060f7643c7SGeorge Wilson */ 11070f7643c7SGeorge Wilson if (flags & ZIO_FLAG_IO_ALLOCATING && 11080f7643c7SGeorge Wilson (vd != vd->vdev_top || (flags & ZIO_FLAG_IO_RETRY))) { 1109*663207adSDon Brady ASSERT(pio->io_metaslab_class != NULL); 1110*663207adSDon Brady ASSERT(pio->io_metaslab_class->mc_alloc_throttle_enabled); 11110f7643c7SGeorge Wilson ASSERT(type == ZIO_TYPE_WRITE); 11120f7643c7SGeorge Wilson ASSERT(priority == ZIO_PRIORITY_ASYNC_WRITE); 11130f7643c7SGeorge Wilson ASSERT(!(flags & ZIO_FLAG_IO_REPAIR)); 11140f7643c7SGeorge Wilson ASSERT(!(pio->io_flags & ZIO_FLAG_IO_REWRITE) || 11150f7643c7SGeorge Wilson pio->io_child_type == ZIO_CHILD_GANG); 11160f7643c7SGeorge Wilson 11170f7643c7SGeorge Wilson flags &= ~ZIO_FLAG_IO_ALLOCATING; 11180f7643c7SGeorge Wilson } 11190f7643c7SGeorge Wilson 11205602294fSDan Kimmel zio = zio_create(pio, pio->io_spa, pio->io_txg, bp, data, size, size, 1121b24ab676SJeff Bonwick done, private, type, priority, flags, vd, offset, &pio->io_bookmark, 1122b24ab676SJeff Bonwick ZIO_STAGE_VDEV_IO_START >> 1, pipeline); 11230f7643c7SGeorge Wilson ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV); 1124fa9e4066Sahrens 112569962b56SMatthew Ahrens zio->io_physdone = pio->io_physdone; 112669962b56SMatthew Ahrens if (vd->vdev_ops->vdev_op_leaf && zio->io_logical != NULL) 112769962b56SMatthew Ahrens zio->io_logical->io_phys_children++; 112869962b56SMatthew Ahrens 1129e14bb325SJeff Bonwick return (zio); 113032b87932Sek } 113132b87932Sek 1132e14bb325SJeff Bonwick zio_t * 1133770499e1SDan Kimmel zio_vdev_delegated_io(vdev_t *vd, uint64_t offset, abd_t *data, uint64_t size, 11343a4b1be9SMatthew Ahrens zio_type_t type, zio_priority_t priority, enum zio_flag flags, 11359a686fbcSPaul Dagnelie zio_done_func_t *done, void *private) 1136fa9e4066Sahrens { 1137e14bb325SJeff Bonwick zio_t *zio; 1138fa9e4066Sahrens 1139e14bb325SJeff Bonwick ASSERT(vd->vdev_ops->vdev_op_leaf); 1140fa9e4066Sahrens 1141e14bb325SJeff Bonwick zio = zio_create(NULL, vd->vdev_spa, 0, NULL, 11425602294fSDan Kimmel data, size, size, done, private, type, priority, 114369962b56SMatthew Ahrens flags | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY | ZIO_FLAG_DELEGATED, 1144e14bb325SJeff Bonwick vd, offset, NULL, 1145b24ab676SJeff Bonwick ZIO_STAGE_VDEV_IO_START >> 1, ZIO_VDEV_CHILD_PIPELINE); 1146fa9e4066Sahrens 1147e14bb325SJeff Bonwick return (zio); 1148e05725b1Sbonwick } 1149e05725b1Sbonwick 1150e05725b1Sbonwick void 1151e14bb325SJeff Bonwick zio_flush(zio_t *zio, vdev_t *vd) 1152e05725b1Sbonwick { 1153e14bb325SJeff Bonwick zio_nowait(zio_ioctl(zio, zio->io_spa, vd, DKIOCFLUSHWRITECACHE, 115469962b56SMatthew Ahrens NULL, NULL, 1155e14bb325SJeff Bonwick ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY)); 1156fa9e4066Sahrens } 1157fa9e4066Sahrens 11586e1f5caaSNeil Perrin void 11596e1f5caaSNeil Perrin zio_shrink(zio_t *zio, uint64_t size) 11606e1f5caaSNeil Perrin { 11611271e4b1SPrakash Surya ASSERT3P(zio->io_executor, ==, NULL); 11621271e4b1SPrakash Surya ASSERT3P(zio->io_orig_size, ==, zio->io_size); 11631271e4b1SPrakash Surya ASSERT3U(size, <=, zio->io_size); 11646e1f5caaSNeil Perrin 11656e1f5caaSNeil Perrin /* 11666e1f5caaSNeil Perrin * We don't shrink for raidz because of problems with the 11676e1f5caaSNeil Perrin * reconstruction when reading back less than the block size. 11686e1f5caaSNeil Perrin * Note, BP_IS_RAIDZ() assumes no compression. 11696e1f5caaSNeil Perrin */ 11706e1f5caaSNeil Perrin ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF); 11715602294fSDan Kimmel if (!BP_IS_RAIDZ(zio->io_bp)) { 11725602294fSDan Kimmel /* we are not doing a raw write */ 11735602294fSDan Kimmel ASSERT3U(zio->io_size, ==, zio->io_lsize); 11745602294fSDan Kimmel zio->io_orig_size = zio->io_size = zio->io_lsize = size; 11755602294fSDan Kimmel } 11766e1f5caaSNeil Perrin } 11776e1f5caaSNeil Perrin 1178fa9e4066Sahrens /* 1179fa9e4066Sahrens * ========================================================================== 1180e14bb325SJeff Bonwick * Prepare to read and write logical blocks 1181fa9e4066Sahrens * ========================================================================== 1182fa9e4066Sahrens */ 1183e14bb325SJeff Bonwick 1184e05725b1Sbonwick static int 1185e14bb325SJeff Bonwick zio_read_bp_init(zio_t *zio) 1186fa9e4066Sahrens { 1187e14bb325SJeff Bonwick blkptr_t *bp = zio->io_bp; 1188e05725b1Sbonwick 11895cabbc6bSPrashanth Sreenivasa ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy); 11905cabbc6bSPrashanth Sreenivasa 119103361682SJeff Bonwick if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF && 1192f5383399SBill Moore zio->io_child_type == ZIO_CHILD_LOGICAL && 1193f5383399SBill Moore !(zio->io_flags & ZIO_FLAG_RAW)) { 11945d7b4d43SMatthew Ahrens uint64_t psize = 11955d7b4d43SMatthew Ahrens BP_IS_EMBEDDED(bp) ? BPE_GET_PSIZE(bp) : BP_GET_PSIZE(bp); 1196770499e1SDan Kimmel zio_push_transform(zio, abd_alloc_sametype(zio->io_abd, psize), 1197770499e1SDan Kimmel psize, psize, zio_decompress); 1198e14bb325SJeff Bonwick } 1199fa9e4066Sahrens 12005d7b4d43SMatthew Ahrens if (BP_IS_EMBEDDED(bp) && BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA) { 12015d7b4d43SMatthew Ahrens zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 1202770499e1SDan Kimmel 1203770499e1SDan Kimmel int psize = BPE_GET_PSIZE(bp); 1204770499e1SDan Kimmel void *data = abd_borrow_buf(zio->io_abd, psize); 1205770499e1SDan Kimmel decode_embedded_bp_compressed(bp, data); 1206770499e1SDan Kimmel abd_return_buf_copy(zio->io_abd, data, psize); 12075d7b4d43SMatthew Ahrens } else { 12085d7b4d43SMatthew Ahrens ASSERT(!BP_IS_EMBEDDED(bp)); 12095cabbc6bSPrashanth Sreenivasa ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy); 12105d7b4d43SMatthew Ahrens } 12115d7b4d43SMatthew Ahrens 1212ad135b5dSChristopher Siden if (!DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) && BP_GET_LEVEL(bp) == 0) 1213e14bb325SJeff Bonwick zio->io_flags |= ZIO_FLAG_DONT_CACHE; 1214fa9e4066Sahrens 1215bbfd46c4SJeff Bonwick if (BP_GET_TYPE(bp) == DMU_OT_DDT_ZAP) 1216bbfd46c4SJeff Bonwick zio->io_flags |= ZIO_FLAG_DONT_CACHE; 1217bbfd46c4SJeff Bonwick 1218b24ab676SJeff Bonwick if (BP_GET_DEDUP(bp) && zio->io_child_type == ZIO_CHILD_LOGICAL) 1219b24ab676SJeff Bonwick zio->io_pipeline = ZIO_DDT_READ_PIPELINE; 1220b24ab676SJeff Bonwick 1221e14bb325SJeff Bonwick return (ZIO_PIPELINE_CONTINUE); 1222fa9e4066Sahrens } 1223fa9e4066Sahrens 1224e05725b1Sbonwick static int 1225e14bb325SJeff Bonwick zio_write_bp_init(zio_t *zio) 12260a4e9518Sgw { 1227e14bb325SJeff Bonwick if (!IO_IS_ALLOCATING(zio)) 1228e14bb325SJeff Bonwick return (ZIO_PIPELINE_CONTINUE); 12290a4e9518Sgw 1230b24ab676SJeff Bonwick ASSERT(zio->io_child_type != ZIO_CHILD_DDT); 1231b24ab676SJeff Bonwick 1232b24ab676SJeff Bonwick if (zio->io_bp_override) { 12330f7643c7SGeorge Wilson blkptr_t *bp = zio->io_bp; 12340f7643c7SGeorge Wilson zio_prop_t *zp = &zio->io_prop; 12350f7643c7SGeorge Wilson 1236b24ab676SJeff Bonwick ASSERT(bp->blk_birth != zio->io_txg); 1237b24ab676SJeff Bonwick ASSERT(BP_GET_DEDUP(zio->io_bp_override) == 0); 1238b24ab676SJeff Bonwick 1239b24ab676SJeff Bonwick *bp = *zio->io_bp_override; 1240b24ab676SJeff Bonwick zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 1241b24ab676SJeff Bonwick 12425d7b4d43SMatthew Ahrens if (BP_IS_EMBEDDED(bp)) 12435d7b4d43SMatthew Ahrens return (ZIO_PIPELINE_CONTINUE); 12445d7b4d43SMatthew Ahrens 124580901aeaSGeorge Wilson /* 124680901aeaSGeorge Wilson * If we've been overridden and nopwrite is set then 124780901aeaSGeorge Wilson * set the flag accordingly to indicate that a nopwrite 124880901aeaSGeorge Wilson * has already occurred. 124980901aeaSGeorge Wilson */ 125080901aeaSGeorge Wilson if (!BP_IS_HOLE(bp) && zp->zp_nopwrite) { 125180901aeaSGeorge Wilson ASSERT(!zp->zp_dedup); 12520f7643c7SGeorge Wilson ASSERT3U(BP_GET_CHECKSUM(bp), ==, zp->zp_checksum); 125380901aeaSGeorge Wilson zio->io_flags |= ZIO_FLAG_NOPWRITE; 125480901aeaSGeorge Wilson return (ZIO_PIPELINE_CONTINUE); 125580901aeaSGeorge Wilson } 125680901aeaSGeorge Wilson 125780901aeaSGeorge Wilson ASSERT(!zp->zp_nopwrite); 125880901aeaSGeorge Wilson 1259b24ab676SJeff Bonwick if (BP_IS_HOLE(bp) || !zp->zp_dedup) 1260b24ab676SJeff Bonwick return (ZIO_PIPELINE_CONTINUE); 1261b24ab676SJeff Bonwick 126245818ee1SMatthew Ahrens ASSERT((zio_checksum_table[zp->zp_checksum].ci_flags & 126345818ee1SMatthew Ahrens ZCHECKSUM_FLAG_DEDUP) || zp->zp_dedup_verify); 1264b24ab676SJeff Bonwick 1265b24ab676SJeff Bonwick if (BP_GET_CHECKSUM(bp) == zp->zp_checksum) { 1266b24ab676SJeff Bonwick BP_SET_DEDUP(bp, 1); 1267b24ab676SJeff Bonwick zio->io_pipeline |= ZIO_STAGE_DDT_WRITE; 1268b24ab676SJeff Bonwick return (ZIO_PIPELINE_CONTINUE); 1269b24ab676SJeff Bonwick } 12700f7643c7SGeorge Wilson 12710f7643c7SGeorge Wilson /* 12720f7643c7SGeorge Wilson * We were unable to handle this as an override bp, treat 12730f7643c7SGeorge Wilson * it as a regular write I/O. 12740f7643c7SGeorge Wilson */ 1275b39b744bSMatthew Ahrens zio->io_bp_override = NULL; 12760f7643c7SGeorge Wilson *bp = zio->io_bp_orig; 12770f7643c7SGeorge Wilson zio->io_pipeline = zio->io_orig_pipeline; 1278b24ab676SJeff Bonwick } 12790a4e9518Sgw 12800f7643c7SGeorge Wilson return (ZIO_PIPELINE_CONTINUE); 12810f7643c7SGeorge Wilson } 12820f7643c7SGeorge Wilson 12830f7643c7SGeorge Wilson static int 12840f7643c7SGeorge Wilson zio_write_compress(zio_t *zio) 12850f7643c7SGeorge Wilson { 12860f7643c7SGeorge Wilson spa_t *spa = zio->io_spa; 12870f7643c7SGeorge Wilson zio_prop_t *zp = &zio->io_prop; 12880f7643c7SGeorge Wilson enum zio_compress compress = zp->zp_compress; 12890f7643c7SGeorge Wilson blkptr_t *bp = zio->io_bp; 12905602294fSDan Kimmel uint64_t lsize = zio->io_lsize; 12915602294fSDan Kimmel uint64_t psize = zio->io_size; 12920f7643c7SGeorge Wilson int pass = 1; 12930f7643c7SGeorge Wilson 12945602294fSDan Kimmel EQUIV(lsize != psize, (zio->io_flags & ZIO_FLAG_RAW) != 0); 12955602294fSDan Kimmel 12960f7643c7SGeorge Wilson /* 12970f7643c7SGeorge Wilson * If our children haven't all reached the ready stage, 12980f7643c7SGeorge Wilson * wait for them and then repeat this pipeline stage. 12990f7643c7SGeorge Wilson */ 1300d6e1c446SGeorge Wilson if (zio_wait_for_children(zio, ZIO_CHILD_LOGICAL_BIT | 1301d6e1c446SGeorge Wilson ZIO_CHILD_GANG_BIT, ZIO_WAIT_READY)) { 13020f7643c7SGeorge Wilson return (ZIO_PIPELINE_STOP); 1303d6e1c446SGeorge Wilson } 13040f7643c7SGeorge Wilson 13050f7643c7SGeorge Wilson if (!IO_IS_ALLOCATING(zio)) 13060f7643c7SGeorge Wilson return (ZIO_PIPELINE_CONTINUE); 13070f7643c7SGeorge Wilson 13080f7643c7SGeorge Wilson if (zio->io_children_ready != NULL) { 13090f7643c7SGeorge Wilson /* 13100f7643c7SGeorge Wilson * Now that all our children are ready, run the callback 13110f7643c7SGeorge Wilson * associated with this zio in case it wants to modify the 13120f7643c7SGeorge Wilson * data to be written. 13130f7643c7SGeorge Wilson */ 13140f7643c7SGeorge Wilson ASSERT3U(zp->zp_level, >, 0); 13150f7643c7SGeorge Wilson zio->io_children_ready(zio); 13160f7643c7SGeorge Wilson } 13170f7643c7SGeorge Wilson 13180f7643c7SGeorge Wilson ASSERT(zio->io_child_type != ZIO_CHILD_DDT); 13190f7643c7SGeorge Wilson ASSERT(zio->io_bp_override == NULL); 13200f7643c7SGeorge Wilson 132143466aaeSMax Grossman if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg) { 1322e14bb325SJeff Bonwick /* 1323e14bb325SJeff Bonwick * We're rewriting an existing block, which means we're 1324e14bb325SJeff Bonwick * working on behalf of spa_sync(). For spa_sync() to 1325e14bb325SJeff Bonwick * converge, it must eventually be the case that we don't 1326e14bb325SJeff Bonwick * have to allocate new blocks. But compression changes 1327e14bb325SJeff Bonwick * the blocksize, which forces a reallocate, and makes 1328e14bb325SJeff Bonwick * convergence take longer. Therefore, after the first 1329e14bb325SJeff Bonwick * few passes, stop compressing to ensure convergence. 1330e14bb325SJeff Bonwick */ 1331b24ab676SJeff Bonwick pass = spa_sync_pass(spa); 1332b24ab676SJeff Bonwick 1333b24ab676SJeff Bonwick ASSERT(zio->io_txg == spa_syncing_txg(spa)); 1334b24ab676SJeff Bonwick ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 1335b24ab676SJeff Bonwick ASSERT(!BP_GET_DEDUP(bp)); 1336e05725b1Sbonwick 133701f55e48SGeorge Wilson if (pass >= zfs_sync_pass_dont_compress) 1338e14bb325SJeff Bonwick compress = ZIO_COMPRESS_OFF; 1339e05725b1Sbonwick 1340e14bb325SJeff Bonwick /* Make sure someone doesn't change their mind on overwrites */ 13415d7b4d43SMatthew Ahrens ASSERT(BP_IS_EMBEDDED(bp) || MIN(zp->zp_copies + BP_IS_GANG(bp), 1342b24ab676SJeff Bonwick spa_max_replication(spa)) == BP_GET_NDVAS(bp)); 1343e14bb325SJeff Bonwick } 1344fa9e4066Sahrens 13455602294fSDan Kimmel /* If it's a compressed write that is not raw, compress the buffer. */ 13465602294fSDan Kimmel if (compress != ZIO_COMPRESS_OFF && psize == lsize) { 1347b24ab676SJeff Bonwick void *cbuf = zio_buf_alloc(lsize); 1348770499e1SDan Kimmel psize = zio_compress_data(compress, zio->io_abd, cbuf, lsize); 1349b24ab676SJeff Bonwick if (psize == 0 || psize == lsize) { 1350e14bb325SJeff Bonwick compress = ZIO_COMPRESS_OFF; 1351b24ab676SJeff Bonwick zio_buf_free(cbuf, lsize); 13525d7b4d43SMatthew Ahrens } else if (!zp->zp_dedup && psize <= BPE_PAYLOAD_SIZE && 13535d7b4d43SMatthew Ahrens zp->zp_level == 0 && !DMU_OT_HAS_FILL(zp->zp_type) && 13545d7b4d43SMatthew Ahrens spa_feature_is_enabled(spa, SPA_FEATURE_EMBEDDED_DATA)) { 13555d7b4d43SMatthew Ahrens encode_embedded_bp_compressed(bp, 13565d7b4d43SMatthew Ahrens cbuf, compress, lsize, psize); 13575d7b4d43SMatthew Ahrens BPE_SET_ETYPE(bp, BP_EMBEDDED_TYPE_DATA); 13585d7b4d43SMatthew Ahrens BP_SET_TYPE(bp, zio->io_prop.zp_type); 13595d7b4d43SMatthew Ahrens BP_SET_LEVEL(bp, zio->io_prop.zp_level); 13605d7b4d43SMatthew Ahrens zio_buf_free(cbuf, lsize); 13615d7b4d43SMatthew Ahrens bp->blk_birth = zio->io_txg; 13625d7b4d43SMatthew Ahrens zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 13635d7b4d43SMatthew Ahrens ASSERT(spa_feature_is_active(spa, 13645d7b4d43SMatthew Ahrens SPA_FEATURE_EMBEDDED_DATA)); 13655d7b4d43SMatthew Ahrens return (ZIO_PIPELINE_CONTINUE); 1366b24ab676SJeff Bonwick } else { 13675d7b4d43SMatthew Ahrens /* 136881cd5c55SMatthew Ahrens * Round up compressed size up to the ashift 136981cd5c55SMatthew Ahrens * of the smallest-ashift device, and zero the tail. 137081cd5c55SMatthew Ahrens * This ensures that the compressed size of the BP 137181cd5c55SMatthew Ahrens * (and thus compressratio property) are correct, 137281cd5c55SMatthew Ahrens * in that we charge for the padding used to fill out 137381cd5c55SMatthew Ahrens * the last sector. 13745d7b4d43SMatthew Ahrens */ 137581cd5c55SMatthew Ahrens ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT); 137681cd5c55SMatthew Ahrens size_t rounded = (size_t)P2ROUNDUP(psize, 137781cd5c55SMatthew Ahrens 1ULL << spa->spa_min_ashift); 137881cd5c55SMatthew Ahrens if (rounded >= lsize) { 13795d7b4d43SMatthew Ahrens compress = ZIO_COMPRESS_OFF; 13805d7b4d43SMatthew Ahrens zio_buf_free(cbuf, lsize); 138181cd5c55SMatthew Ahrens psize = lsize; 13825d7b4d43SMatthew Ahrens } else { 1383770499e1SDan Kimmel abd_t *cdata = abd_get_from_buf(cbuf, lsize); 1384770499e1SDan Kimmel abd_take_ownership_of_buf(cdata, B_TRUE); 1385770499e1SDan Kimmel abd_zero_off(cdata, psize, rounded - psize); 138681cd5c55SMatthew Ahrens psize = rounded; 1387770499e1SDan Kimmel zio_push_transform(zio, cdata, 13885d7b4d43SMatthew Ahrens psize, lsize, NULL); 13895d7b4d43SMatthew Ahrens } 1390e14bb325SJeff Bonwick } 13910f7643c7SGeorge Wilson 13920f7643c7SGeorge Wilson /* 13930f7643c7SGeorge Wilson * We were unable to handle this as an override bp, treat 13940f7643c7SGeorge Wilson * it as a regular write I/O. 13950f7643c7SGeorge Wilson */ 13960f7643c7SGeorge Wilson zio->io_bp_override = NULL; 13970f7643c7SGeorge Wilson *bp = zio->io_bp_orig; 13980f7643c7SGeorge Wilson zio->io_pipeline = zio->io_orig_pipeline; 13995602294fSDan Kimmel } else { 14005602294fSDan Kimmel ASSERT3U(psize, !=, 0); 1401e14bb325SJeff Bonwick } 1402c717a561Smaybee 1403e14bb325SJeff Bonwick /* 1404e14bb325SJeff Bonwick * The final pass of spa_sync() must be all rewrites, but the first 1405e14bb325SJeff Bonwick * few passes offer a trade-off: allocating blocks defers convergence, 1406e14bb325SJeff Bonwick * but newly allocated blocks are sequential, so they can be written 1407e14bb325SJeff Bonwick * to disk faster. Therefore, we allow the first few passes of 1408e14bb325SJeff Bonwick * spa_sync() to allocate new blocks, but force rewrites after that. 1409e14bb325SJeff Bonwick * There should only be a handful of blocks after pass 1 in any case. 1410e14bb325SJeff Bonwick */ 141143466aaeSMax Grossman if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg && 141243466aaeSMax Grossman BP_GET_PSIZE(bp) == psize && 141301f55e48SGeorge Wilson pass >= zfs_sync_pass_rewrite) { 1414*663207adSDon Brady VERIFY3U(psize, !=, 0); 1415b24ab676SJeff Bonwick enum zio_stage gang_stages = zio->io_pipeline & ZIO_GANG_STAGES; 1416*663207adSDon Brady 1417e14bb325SJeff Bonwick zio->io_pipeline = ZIO_REWRITE_PIPELINE | gang_stages; 1418e14bb325SJeff Bonwick zio->io_flags |= ZIO_FLAG_IO_REWRITE; 1419e14bb325SJeff Bonwick } else { 1420e14bb325SJeff Bonwick BP_ZERO(bp); 1421e14bb325SJeff Bonwick zio->io_pipeline = ZIO_WRITE_PIPELINE; 1422e14bb325SJeff Bonwick } 1423fa9e4066Sahrens 1424b24ab676SJeff Bonwick if (psize == 0) { 142543466aaeSMax Grossman if (zio->io_bp_orig.blk_birth != 0 && 142643466aaeSMax Grossman spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) { 142743466aaeSMax Grossman BP_SET_LSIZE(bp, lsize); 142843466aaeSMax Grossman BP_SET_TYPE(bp, zp->zp_type); 142943466aaeSMax Grossman BP_SET_LEVEL(bp, zp->zp_level); 143043466aaeSMax Grossman BP_SET_BIRTH(bp, zio->io_txg, 0); 143143466aaeSMax Grossman } 1432e14bb325SJeff Bonwick zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 1433e14bb325SJeff Bonwick } else { 1434e14bb325SJeff Bonwick ASSERT(zp->zp_checksum != ZIO_CHECKSUM_GANG_HEADER); 1435e14bb325SJeff Bonwick BP_SET_LSIZE(bp, lsize); 143643466aaeSMax Grossman BP_SET_TYPE(bp, zp->zp_type); 143743466aaeSMax Grossman BP_SET_LEVEL(bp, zp->zp_level); 1438b24ab676SJeff Bonwick BP_SET_PSIZE(bp, psize); 1439e14bb325SJeff Bonwick BP_SET_COMPRESS(bp, compress); 1440e14bb325SJeff Bonwick BP_SET_CHECKSUM(bp, zp->zp_checksum); 1441b24ab676SJeff Bonwick BP_SET_DEDUP(bp, zp->zp_dedup); 1442e14bb325SJeff Bonwick BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER); 1443b24ab676SJeff Bonwick if (zp->zp_dedup) { 1444b24ab676SJeff Bonwick ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 1445b24ab676SJeff Bonwick ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE)); 1446b24ab676SJeff Bonwick zio->io_pipeline = ZIO_DDT_WRITE_PIPELINE; 1447b24ab676SJeff Bonwick } 144880901aeaSGeorge Wilson if (zp->zp_nopwrite) { 144980901aeaSGeorge Wilson ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 145080901aeaSGeorge Wilson ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE)); 145180901aeaSGeorge Wilson zio->io_pipeline |= ZIO_STAGE_NOP_WRITE; 145280901aeaSGeorge Wilson } 1453b24ab676SJeff Bonwick } 1454b24ab676SJeff Bonwick return (ZIO_PIPELINE_CONTINUE); 1455b24ab676SJeff Bonwick } 1456b24ab676SJeff Bonwick 1457b24ab676SJeff Bonwick static int 1458b24ab676SJeff Bonwick zio_free_bp_init(zio_t *zio) 1459b24ab676SJeff Bonwick { 1460b24ab676SJeff Bonwick blkptr_t *bp = zio->io_bp; 1461b24ab676SJeff Bonwick 1462b24ab676SJeff Bonwick if (zio->io_child_type == ZIO_CHILD_LOGICAL) { 1463b24ab676SJeff Bonwick if (BP_GET_DEDUP(bp)) 1464b24ab676SJeff Bonwick zio->io_pipeline = ZIO_DDT_FREE_PIPELINE; 1465e14bb325SJeff Bonwick } 1466fa9e4066Sahrens 14675cabbc6bSPrashanth Sreenivasa ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy); 14685cabbc6bSPrashanth Sreenivasa 1469e05725b1Sbonwick return (ZIO_PIPELINE_CONTINUE); 1470fa9e4066Sahrens } 1471fa9e4066Sahrens 1472e14bb325SJeff Bonwick /* 1473e14bb325SJeff Bonwick * ========================================================================== 1474e14bb325SJeff Bonwick * Execute the I/O pipeline 1475e14bb325SJeff Bonwick * ========================================================================== 1476e14bb325SJeff Bonwick */ 1477e14bb325SJeff Bonwick 1478e14bb325SJeff Bonwick static void 1479ec94d322SAdam Leventhal zio_taskq_dispatch(zio_t *zio, zio_taskq_type_t q, boolean_t cutinline) 1480fa9e4066Sahrens { 148180eb36f2SGeorge Wilson spa_t *spa = zio->io_spa; 1482e14bb325SJeff Bonwick zio_type_t t = zio->io_type; 14835aeb9474SGarrett D'Amore int flags = (cutinline ? TQ_FRONT : 0); 14840a4e9518Sgw 14850a4e9518Sgw /* 1486bbe36defSGeorge Wilson * If we're a config writer or a probe, the normal issue and 1487bbe36defSGeorge Wilson * interrupt threads may all be blocked waiting for the config lock. 1488bbe36defSGeorge Wilson * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL. 14890a4e9518Sgw */ 1490bbe36defSGeorge Wilson if (zio->io_flags & (ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_PROBE)) 1491e14bb325SJeff Bonwick t = ZIO_TYPE_NULL; 14920a4e9518Sgw 14930a4e9518Sgw /* 1494e14bb325SJeff Bonwick * A similar issue exists for the L2ARC write thread until L2ARC 2.0. 14950a4e9518Sgw */ 1496e14bb325SJeff Bonwick if (t == ZIO_TYPE_WRITE && zio->io_vd && zio->io_vd->vdev_aux) 1497e14bb325SJeff Bonwick t = ZIO_TYPE_NULL; 14980a4e9518Sgw 149980eb36f2SGeorge Wilson /* 1500ec94d322SAdam Leventhal * If this is a high priority I/O, then use the high priority taskq if 1501ec94d322SAdam Leventhal * available. 150280eb36f2SGeorge Wilson */ 15032258ad0bSGeorge Wilson if ((zio->io_priority == ZIO_PRIORITY_NOW || 15042258ad0bSGeorge Wilson zio->io_priority == ZIO_PRIORITY_SYNC_WRITE) && 1505ec94d322SAdam Leventhal spa->spa_zio_taskq[t][q + 1].stqs_count != 0) 150680eb36f2SGeorge Wilson q++; 150780eb36f2SGeorge Wilson 150880eb36f2SGeorge Wilson ASSERT3U(q, <, ZIO_TASKQ_TYPES); 15095aeb9474SGarrett D'Amore 15105aeb9474SGarrett D'Amore /* 15115aeb9474SGarrett D'Amore * NB: We are assuming that the zio can only be dispatched 15125aeb9474SGarrett D'Amore * to a single taskq at a time. It would be a grievous error 15135aeb9474SGarrett D'Amore * to dispatch the zio to another taskq at the same time. 15145aeb9474SGarrett D'Amore */ 15155aeb9474SGarrett D'Amore ASSERT(zio->io_tqent.tqent_next == NULL); 1516ec94d322SAdam Leventhal spa_taskq_dispatch_ent(spa, t, q, (task_func_t *)zio_execute, zio, 1517ec94d322SAdam Leventhal flags, &zio->io_tqent); 1518e14bb325SJeff Bonwick } 15190a4e9518Sgw 1520e14bb325SJeff Bonwick static boolean_t 1521ec94d322SAdam Leventhal zio_taskq_member(zio_t *zio, zio_taskq_type_t q) 1522e14bb325SJeff Bonwick { 1523e14bb325SJeff Bonwick kthread_t *executor = zio->io_executor; 1524e14bb325SJeff Bonwick spa_t *spa = zio->io_spa; 15250a4e9518Sgw 1526ec94d322SAdam Leventhal for (zio_type_t t = 0; t < ZIO_TYPES; t++) { 1527ec94d322SAdam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; 1528ec94d322SAdam Leventhal uint_t i; 1529ec94d322SAdam Leventhal for (i = 0; i < tqs->stqs_count; i++) { 1530ec94d322SAdam Leventhal if (taskq_member(tqs->stqs_taskq[i], executor)) 1531ec94d322SAdam Leventhal return (B_TRUE); 1532ec94d322SAdam Leventhal } 1533ec94d322SAdam Leventhal } 15340a4e9518Sgw 1535e14bb325SJeff Bonwick return (B_FALSE); 1536e14bb325SJeff Bonwick } 1537e05725b1Sbonwick 1538e14bb325SJeff Bonwick static int 1539e14bb325SJeff Bonwick zio_issue_async(zio_t *zio) 1540e14bb325SJeff Bonwick { 154135a5a358SJonathan Adams zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE); 1542e14bb325SJeff Bonwick 1543e14bb325SJeff Bonwick return (ZIO_PIPELINE_STOP); 15440a4e9518Sgw } 15450a4e9518Sgw 1546e14bb325SJeff Bonwick void 1547e14bb325SJeff Bonwick zio_interrupt(zio_t *zio) 15480a4e9518Sgw { 154935a5a358SJonathan Adams zio_taskq_dispatch(zio, ZIO_TASKQ_INTERRUPT, B_FALSE); 1550e14bb325SJeff Bonwick } 15510a4e9518Sgw 155297e81309SPrakash Surya void 155397e81309SPrakash Surya zio_delay_interrupt(zio_t *zio) 155497e81309SPrakash Surya { 155597e81309SPrakash Surya /* 155697e81309SPrakash Surya * The timeout_generic() function isn't defined in userspace, so 155797e81309SPrakash Surya * rather than trying to implement the function, the zio delay 155897e81309SPrakash Surya * functionality has been disabled for userspace builds. 155997e81309SPrakash Surya */ 156097e81309SPrakash Surya 156197e81309SPrakash Surya #ifdef _KERNEL 156297e81309SPrakash Surya /* 156397e81309SPrakash Surya * If io_target_timestamp is zero, then no delay has been registered 156497e81309SPrakash Surya * for this IO, thus jump to the end of this function and "skip" the 156597e81309SPrakash Surya * delay; issuing it directly to the zio layer. 156697e81309SPrakash Surya */ 156797e81309SPrakash Surya if (zio->io_target_timestamp != 0) { 156897e81309SPrakash Surya hrtime_t now = gethrtime(); 156997e81309SPrakash Surya 157097e81309SPrakash Surya if (now >= zio->io_target_timestamp) { 157197e81309SPrakash Surya /* 157297e81309SPrakash Surya * This IO has already taken longer than the target 157397e81309SPrakash Surya * delay to complete, so we don't want to delay it 157497e81309SPrakash Surya * any longer; we "miss" the delay and issue it 157597e81309SPrakash Surya * directly to the zio layer. This is likely due to 157697e81309SPrakash Surya * the target latency being set to a value less than 157797e81309SPrakash Surya * the underlying hardware can satisfy (e.g. delay 157897e81309SPrakash Surya * set to 1ms, but the disks take 10ms to complete an 157997e81309SPrakash Surya * IO request). 158097e81309SPrakash Surya */ 158197e81309SPrakash Surya 158297e81309SPrakash Surya DTRACE_PROBE2(zio__delay__miss, zio_t *, zio, 158397e81309SPrakash Surya hrtime_t, now); 158497e81309SPrakash Surya 158597e81309SPrakash Surya zio_interrupt(zio); 158697e81309SPrakash Surya } else { 158797e81309SPrakash Surya hrtime_t diff = zio->io_target_timestamp - now; 158897e81309SPrakash Surya 158997e81309SPrakash Surya DTRACE_PROBE3(zio__delay__hit, zio_t *, zio, 159097e81309SPrakash Surya hrtime_t, now, hrtime_t, diff); 159197e81309SPrakash Surya 159297e81309SPrakash Surya (void) timeout_generic(CALLOUT_NORMAL, 159397e81309SPrakash Surya (void (*)(void *))zio_interrupt, zio, diff, 1, 0); 159497e81309SPrakash Surya } 159597e81309SPrakash Surya 159697e81309SPrakash Surya return; 159797e81309SPrakash Surya } 159897e81309SPrakash Surya #endif 159997e81309SPrakash Surya 160097e81309SPrakash Surya DTRACE_PROBE1(zio__delay__skip, zio_t *, zio); 160197e81309SPrakash Surya zio_interrupt(zio); 160297e81309SPrakash Surya } 160397e81309SPrakash Surya 1604e14bb325SJeff Bonwick /* 1605e14bb325SJeff Bonwick * Execute the I/O pipeline until one of the following occurs: 1606f7170741SWill Andrews * 1607f7170741SWill Andrews * (1) the I/O completes 1608f7170741SWill Andrews * (2) the pipeline stalls waiting for dependent child I/Os 1609f7170741SWill Andrews * (3) the I/O issues, so we're waiting for an I/O completion interrupt 1610f7170741SWill Andrews * (4) the I/O is delegated by vdev-level caching or aggregation 1611f7170741SWill Andrews * (5) the I/O is deferred due to vdev-level queueing 1612f7170741SWill Andrews * (6) the I/O is handed off to another thread. 1613f7170741SWill Andrews * 1614f7170741SWill Andrews * In all cases, the pipeline stops whenever there's no CPU work; it never 1615f7170741SWill Andrews * burns a thread in cv_wait(). 1616e14bb325SJeff Bonwick * 1617e14bb325SJeff Bonwick * There's no locking on io_stage because there's no legitimate way 1618e14bb325SJeff Bonwick * for multiple threads to be attempting to process the same I/O. 1619e14bb325SJeff Bonwick */ 1620b24ab676SJeff Bonwick static zio_pipe_stage_t *zio_pipeline[]; 16210a4e9518Sgw 1622e14bb325SJeff Bonwick void 1623e14bb325SJeff Bonwick zio_execute(zio_t *zio) 1624e14bb325SJeff Bonwick { 1625e14bb325SJeff Bonwick zio->io_executor = curthread; 16260a4e9518Sgw 16270f7643c7SGeorge Wilson ASSERT3U(zio->io_queued_timestamp, >, 0); 16280f7643c7SGeorge Wilson 1629e14bb325SJeff Bonwick while (zio->io_stage < ZIO_STAGE_DONE) { 1630b24ab676SJeff Bonwick enum zio_stage pipeline = zio->io_pipeline; 1631b24ab676SJeff Bonwick enum zio_stage stage = zio->io_stage; 1632e14bb325SJeff Bonwick int rv; 16330a4e9518Sgw 1634e14bb325SJeff Bonwick ASSERT(!MUTEX_HELD(&zio->io_lock)); 1635b24ab676SJeff Bonwick ASSERT(ISP2(stage)); 1636b24ab676SJeff Bonwick ASSERT(zio->io_stall == NULL); 16370a4e9518Sgw 1638b24ab676SJeff Bonwick do { 1639b24ab676SJeff Bonwick stage <<= 1; 1640b24ab676SJeff Bonwick } while ((stage & pipeline) == 0); 1641e14bb325SJeff Bonwick 1642e14bb325SJeff Bonwick ASSERT(stage <= ZIO_STAGE_DONE); 16430a4e9518Sgw 16440a4e9518Sgw /* 1645e14bb325SJeff Bonwick * If we are in interrupt context and this pipeline stage 1646e14bb325SJeff Bonwick * will grab a config lock that is held across I/O, 1647b24ab676SJeff Bonwick * or may wait for an I/O that needs an interrupt thread 1648b24ab676SJeff Bonwick * to complete, issue async to avoid deadlock. 164935a5a358SJonathan Adams * 165035a5a358SJonathan Adams * For VDEV_IO_START, we cut in line so that the io will 165135a5a358SJonathan Adams * be sent to disk promptly. 16520a4e9518Sgw */ 1653b24ab676SJeff Bonwick if ((stage & ZIO_BLOCKING_STAGES) && zio->io_vd == NULL && 1654e14bb325SJeff Bonwick zio_taskq_member(zio, ZIO_TASKQ_INTERRUPT)) { 165535a5a358SJonathan Adams boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ? 165635a5a358SJonathan Adams zio_requeue_io_start_cut_in_line : B_FALSE; 165735a5a358SJonathan Adams zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut); 1658e14bb325SJeff Bonwick return; 16590a4e9518Sgw } 16600a4e9518Sgw 1661e14bb325SJeff Bonwick zio->io_stage = stage; 16620f7643c7SGeorge Wilson zio->io_pipeline_trace |= zio->io_stage; 1663bf16b11eSMatthew Ahrens rv = zio_pipeline[highbit64(stage) - 1](zio); 16640a4e9518Sgw 1665e14bb325SJeff Bonwick if (rv == ZIO_PIPELINE_STOP) 1666e14bb325SJeff Bonwick return; 16670a4e9518Sgw 1668e14bb325SJeff Bonwick ASSERT(rv == ZIO_PIPELINE_CONTINUE); 1669e14bb325SJeff Bonwick } 16700a4e9518Sgw } 16710a4e9518Sgw 1672e14bb325SJeff Bonwick /* 1673e14bb325SJeff Bonwick * ========================================================================== 1674e14bb325SJeff Bonwick * Initiate I/O, either sync or async 1675e14bb325SJeff Bonwick * ========================================================================== 1676e14bb325SJeff Bonwick */ 1677e14bb325SJeff Bonwick int 1678e14bb325SJeff Bonwick zio_wait(zio_t *zio) 16790a4e9518Sgw { 1680e14bb325SJeff Bonwick int error; 16810a4e9518Sgw 16821271e4b1SPrakash Surya ASSERT3P(zio->io_stage, ==, ZIO_STAGE_OPEN); 16831271e4b1SPrakash Surya ASSERT3P(zio->io_executor, ==, NULL); 16840a4e9518Sgw 1685e14bb325SJeff Bonwick zio->io_waiter = curthread; 16860f7643c7SGeorge Wilson ASSERT0(zio->io_queued_timestamp); 16870f7643c7SGeorge Wilson zio->io_queued_timestamp = gethrtime(); 1688e05725b1Sbonwick 1689e14bb325SJeff Bonwick zio_execute(zio); 16900a4e9518Sgw 1691e14bb325SJeff Bonwick mutex_enter(&zio->io_lock); 1692e14bb325SJeff Bonwick while (zio->io_executor != NULL) 1693e14bb325SJeff Bonwick cv_wait(&zio->io_cv, &zio->io_lock); 1694e14bb325SJeff Bonwick mutex_exit(&zio->io_lock); 169532b87932Sek 1696e14bb325SJeff Bonwick error = zio->io_error; 1697e14bb325SJeff Bonwick zio_destroy(zio); 169832b87932Sek 1699e14bb325SJeff Bonwick return (error); 170032b87932Sek } 170132b87932Sek 1702e14bb325SJeff Bonwick void 1703e14bb325SJeff Bonwick zio_nowait(zio_t *zio) 17040a4e9518Sgw { 17051271e4b1SPrakash Surya ASSERT3P(zio->io_executor, ==, NULL); 1706fa9e4066Sahrens 1707a3f829aeSBill Moore if (zio->io_child_type == ZIO_CHILD_LOGICAL && 1708a3f829aeSBill Moore zio_unique_parent(zio) == NULL) { 1709ea8dc4b6Seschrock /* 1710e14bb325SJeff Bonwick * This is a logical async I/O with no parent to wait for it. 171154d692b7SGeorge Wilson * We add it to the spa_async_root_zio "Godfather" I/O which 171254d692b7SGeorge Wilson * will ensure they complete prior to unloading the pool. 1713ea8dc4b6Seschrock */ 1714e14bb325SJeff Bonwick spa_t *spa = zio->io_spa; 171554d692b7SGeorge Wilson 17166f834bc1SMatthew Ahrens zio_add_child(spa->spa_async_zio_root[CPU_SEQID], zio); 1717e14bb325SJeff Bonwick } 1718ea8dc4b6Seschrock 17190f7643c7SGeorge Wilson ASSERT0(zio->io_queued_timestamp); 17200f7643c7SGeorge Wilson zio->io_queued_timestamp = gethrtime(); 1721e14bb325SJeff Bonwick zio_execute(zio); 1722e14bb325SJeff Bonwick } 1723ea8dc4b6Seschrock 1724e14bb325SJeff Bonwick /* 1725e14bb325SJeff Bonwick * ========================================================================== 17261271e4b1SPrakash Surya * Reexecute, cancel, or suspend/resume failed I/O 1727e14bb325SJeff Bonwick * ========================================================================== 1728e14bb325SJeff Bonwick */ 1729fa9e4066Sahrens 1730e14bb325SJeff Bonwick static void 1731e14bb325SJeff Bonwick zio_reexecute(zio_t *pio) 1732e14bb325SJeff Bonwick { 1733a3f829aeSBill Moore zio_t *cio, *cio_next; 1734a3f829aeSBill Moore 1735a3f829aeSBill Moore ASSERT(pio->io_child_type == ZIO_CHILD_LOGICAL); 1736a3f829aeSBill Moore ASSERT(pio->io_orig_stage == ZIO_STAGE_OPEN); 1737f5383399SBill Moore ASSERT(pio->io_gang_leader == NULL); 1738f5383399SBill Moore ASSERT(pio->io_gang_tree == NULL); 1739e05725b1Sbonwick 1740e14bb325SJeff Bonwick pio->io_flags = pio->io_orig_flags; 1741e14bb325SJeff Bonwick pio->io_stage = pio->io_orig_stage; 1742e14bb325SJeff Bonwick pio->io_pipeline = pio->io_orig_pipeline; 1743e14bb325SJeff Bonwick pio->io_reexecute = 0; 174480901aeaSGeorge Wilson pio->io_flags |= ZIO_FLAG_REEXECUTED; 17450f7643c7SGeorge Wilson pio->io_pipeline_trace = 0; 1746e14bb325SJeff Bonwick pio->io_error = 0; 1747a3f829aeSBill Moore for (int w = 0; w < ZIO_WAIT_TYPES; w++) 1748a3f829aeSBill Moore pio->io_state[w] = 0; 1749e14bb325SJeff Bonwick for (int c = 0; c < ZIO_CHILD_TYPES; c++) 1750e14bb325SJeff Bonwick pio->io_child_error[c] = 0; 17510a4e9518Sgw 1752b24ab676SJeff Bonwick if (IO_IS_ALLOCATING(pio)) 1753b24ab676SJeff Bonwick BP_ZERO(pio->io_bp); 1754d58459f4Sek 1755e14bb325SJeff Bonwick /* 1756e14bb325SJeff Bonwick * As we reexecute pio's children, new children could be created. 1757a3f829aeSBill Moore * New children go to the head of pio's io_child_list, however, 1758e14bb325SJeff Bonwick * so we will (correctly) not reexecute them. The key is that 1759a3f829aeSBill Moore * the remainder of pio's io_child_list, from 'cio_next' onward, 1760a3f829aeSBill Moore * cannot be affected by any side effects of reexecuting 'cio'. 1761e14bb325SJeff Bonwick */ 17620f7643c7SGeorge Wilson zio_link_t *zl = NULL; 17630f7643c7SGeorge Wilson for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) { 17640f7643c7SGeorge Wilson cio_next = zio_walk_children(pio, &zl); 1765e14bb325SJeff Bonwick mutex_enter(&pio->io_lock); 1766a3f829aeSBill Moore for (int w = 0; w < ZIO_WAIT_TYPES; w++) 1767a3f829aeSBill Moore pio->io_children[cio->io_child_type][w]++; 1768e14bb325SJeff Bonwick mutex_exit(&pio->io_lock); 1769a3f829aeSBill Moore zio_reexecute(cio); 1770fa9e4066Sahrens } 1771e05725b1Sbonwick 1772e14bb325SJeff Bonwick /* 1773e14bb325SJeff Bonwick * Now that all children have been reexecuted, execute the parent. 177454d692b7SGeorge Wilson * We don't reexecute "The Godfather" I/O here as it's the 177548bbca81SDaniel Hoffman * responsibility of the caller to wait on it. 1776e14bb325SJeff Bonwick */ 17770f7643c7SGeorge Wilson if (!(pio->io_flags & ZIO_FLAG_GODFATHER)) { 17780f7643c7SGeorge Wilson pio->io_queued_timestamp = gethrtime(); 177954d692b7SGeorge Wilson zio_execute(pio); 17800f7643c7SGeorge Wilson } 17810a4e9518Sgw } 17820a4e9518Sgw 1783e14bb325SJeff Bonwick void 1784e0f1c0afSOlaf Faaland zio_suspend(spa_t *spa, zio_t *zio, zio_suspend_reason_t reason) 17850a4e9518Sgw { 1786e14bb325SJeff Bonwick if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_PANIC) 1787e14bb325SJeff Bonwick fm_panic("Pool '%s' has encountered an uncorrectable I/O " 1788e14bb325SJeff Bonwick "failure and the failure mode property for this pool " 1789e14bb325SJeff Bonwick "is set to panic.", spa_name(spa)); 17900a4e9518Sgw 1791e14bb325SJeff Bonwick zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE, spa, NULL, NULL, 0, 0); 17920a4e9518Sgw 1793e14bb325SJeff Bonwick mutex_enter(&spa->spa_suspend_lock); 1794fa9e4066Sahrens 1795e14bb325SJeff Bonwick if (spa->spa_suspend_zio_root == NULL) 179654d692b7SGeorge Wilson spa->spa_suspend_zio_root = zio_root(spa, NULL, NULL, 179754d692b7SGeorge Wilson ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 179854d692b7SGeorge Wilson ZIO_FLAG_GODFATHER); 1799fa9e4066Sahrens 1800e0f1c0afSOlaf Faaland spa->spa_suspended = reason; 1801fa9e4066Sahrens 1802e14bb325SJeff Bonwick if (zio != NULL) { 180354d692b7SGeorge Wilson ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER)); 1804e14bb325SJeff Bonwick ASSERT(zio != spa->spa_suspend_zio_root); 1805e14bb325SJeff Bonwick ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 1806a3f829aeSBill Moore ASSERT(zio_unique_parent(zio) == NULL); 1807e14bb325SJeff Bonwick ASSERT(zio->io_stage == ZIO_STAGE_DONE); 1808e14bb325SJeff Bonwick zio_add_child(spa->spa_suspend_zio_root, zio); 1809e14bb325SJeff Bonwick } 1810fa9e4066Sahrens 1811e14bb325SJeff Bonwick mutex_exit(&spa->spa_suspend_lock); 1812e14bb325SJeff Bonwick } 1813fa9e4066Sahrens 181454d692b7SGeorge Wilson int 1815e14bb325SJeff Bonwick zio_resume(spa_t *spa) 1816e14bb325SJeff Bonwick { 181754d692b7SGeorge Wilson zio_t *pio; 1818fa9e4066Sahrens 1819b3995adbSahrens /* 1820e14bb325SJeff Bonwick * Reexecute all previously suspended i/o. 1821b3995adbSahrens */ 1822e14bb325SJeff Bonwick mutex_enter(&spa->spa_suspend_lock); 1823e0f1c0afSOlaf Faaland spa->spa_suspended = ZIO_SUSPEND_NONE; 1824e14bb325SJeff Bonwick cv_broadcast(&spa->spa_suspend_cv); 1825e14bb325SJeff Bonwick pio = spa->spa_suspend_zio_root; 1826e14bb325SJeff Bonwick spa->spa_suspend_zio_root = NULL; 1827e14bb325SJeff Bonwick mutex_exit(&spa->spa_suspend_lock); 1828e14bb325SJeff Bonwick 1829e14bb325SJeff Bonwick if (pio == NULL) 183054d692b7SGeorge Wilson return (0); 1831e14bb325SJeff Bonwick 183254d692b7SGeorge Wilson zio_reexecute(pio); 183354d692b7SGeorge Wilson return (zio_wait(pio)); 1834e14bb325SJeff Bonwick } 1835e14bb325SJeff Bonwick 1836e14bb325SJeff Bonwick void 1837e14bb325SJeff Bonwick zio_resume_wait(spa_t *spa) 1838e14bb325SJeff Bonwick { 1839e14bb325SJeff Bonwick mutex_enter(&spa->spa_suspend_lock); 1840e14bb325SJeff Bonwick while (spa_suspended(spa)) 1841e14bb325SJeff Bonwick cv_wait(&spa->spa_suspend_cv, &spa->spa_suspend_lock); 1842e14bb325SJeff Bonwick mutex_exit(&spa->spa_suspend_lock); 1843fa9e4066Sahrens } 1844fa9e4066Sahrens 1845fa9e4066Sahrens /* 1846fa9e4066Sahrens * ========================================================================== 1847e14bb325SJeff Bonwick * Gang blocks. 1848e14bb325SJeff Bonwick * 1849e14bb325SJeff Bonwick * A gang block is a collection of small blocks that looks to the DMU 1850e14bb325SJeff Bonwick * like one large block. When zio_dva_allocate() cannot find a block 1851e14bb325SJeff Bonwick * of the requested size, due to either severe fragmentation or the pool 1852e14bb325SJeff Bonwick * being nearly full, it calls zio_write_gang_block() to construct the 1853e14bb325SJeff Bonwick * block from smaller fragments. 1854e14bb325SJeff Bonwick * 1855e14bb325SJeff Bonwick * A gang block consists of a gang header (zio_gbh_phys_t) and up to 1856e14bb325SJeff Bonwick * three (SPA_GBH_NBLKPTRS) gang members. The gang header is just like 1857e14bb325SJeff Bonwick * an indirect block: it's an array of block pointers. It consumes 1858e14bb325SJeff Bonwick * only one sector and hence is allocatable regardless of fragmentation. 1859e14bb325SJeff Bonwick * The gang header's bps point to its gang members, which hold the data. 1860e14bb325SJeff Bonwick * 1861e14bb325SJeff Bonwick * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg> 1862e14bb325SJeff Bonwick * as the verifier to ensure uniqueness of the SHA256 checksum. 1863e14bb325SJeff Bonwick * Critically, the gang block bp's blk_cksum is the checksum of the data, 1864e14bb325SJeff Bonwick * not the gang header. This ensures that data block signatures (needed for 1865e14bb325SJeff Bonwick * deduplication) are independent of how the block is physically stored. 1866e14bb325SJeff Bonwick * 1867e14bb325SJeff Bonwick * Gang blocks can be nested: a gang member may itself be a gang block. 1868e14bb325SJeff Bonwick * Thus every gang block is a tree in which root and all interior nodes are 1869e14bb325SJeff Bonwick * gang headers, and the leaves are normal blocks that contain user data. 1870e14bb325SJeff Bonwick * The root of the gang tree is called the gang leader. 1871e14bb325SJeff Bonwick * 1872e14bb325SJeff Bonwick * To perform any operation (read, rewrite, free, claim) on a gang block, 1873e14bb325SJeff Bonwick * zio_gang_assemble() first assembles the gang tree (minus data leaves) 1874e14bb325SJeff Bonwick * in the io_gang_tree field of the original logical i/o by recursively 1875e14bb325SJeff Bonwick * reading the gang leader and all gang headers below it. This yields 1876e14bb325SJeff Bonwick * an in-core tree containing the contents of every gang header and the 1877e14bb325SJeff Bonwick * bps for every constituent of the gang block. 1878e14bb325SJeff Bonwick * 1879e14bb325SJeff Bonwick * With the gang tree now assembled, zio_gang_issue() just walks the gang tree 1880e14bb325SJeff Bonwick * and invokes a callback on each bp. To free a gang block, zio_gang_issue() 1881e14bb325SJeff Bonwick * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp. 1882e14bb325SJeff Bonwick * zio_claim_gang() provides a similarly trivial wrapper for zio_claim(). 1883e14bb325SJeff Bonwick * zio_read_gang() is a wrapper around zio_read() that omits reading gang 1884e14bb325SJeff Bonwick * headers, since we already have those in io_gang_tree. zio_rewrite_gang() 1885e14bb325SJeff Bonwick * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite() 1886e14bb325SJeff Bonwick * of the gang header plus zio_checksum_compute() of the data to update the 1887e14bb325SJeff Bonwick * gang header's blk_cksum as described above. 1888e14bb325SJeff Bonwick * 1889e14bb325SJeff Bonwick * The two-phase assemble/issue model solves the problem of partial failure -- 1890e14bb325SJeff Bonwick * what if you'd freed part of a gang block but then couldn't read the 1891e14bb325SJeff Bonwick * gang header for another part? Assembling the entire gang tree first 1892e14bb325SJeff Bonwick * ensures that all the necessary gang header I/O has succeeded before 1893e14bb325SJeff Bonwick * starting the actual work of free, claim, or write. Once the gang tree 1894e14bb325SJeff Bonwick * is assembled, free and claim are in-memory operations that cannot fail. 1895e14bb325SJeff Bonwick * 1896e14bb325SJeff Bonwick * In the event that a gang write fails, zio_dva_unallocate() walks the 1897e14bb325SJeff Bonwick * gang tree to immediately free (i.e. insert back into the space map) 1898e14bb325SJeff Bonwick * everything we've allocated. This ensures that we don't get ENOSPC 1899e14bb325SJeff Bonwick * errors during repeated suspend/resume cycles due to a flaky device. 1900e14bb325SJeff Bonwick * 1901e14bb325SJeff Bonwick * Gang rewrites only happen during sync-to-convergence. If we can't assemble 1902e14bb325SJeff Bonwick * the gang tree, we won't modify the block, so we can safely defer the free 1903e14bb325SJeff Bonwick * (knowing that the block is still intact). If we *can* assemble the gang 1904e14bb325SJeff Bonwick * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free 1905e14bb325SJeff Bonwick * each constituent bp and we can allocate a new block on the next sync pass. 1906e14bb325SJeff Bonwick * 1907e14bb325SJeff Bonwick * In all cases, the gang tree allows complete recovery from partial failure. 1908fa9e4066Sahrens * ========================================================================== 1909fa9e4066Sahrens */ 1910e14bb325SJeff Bonwick 1911770499e1SDan Kimmel static void 1912770499e1SDan Kimmel zio_gang_issue_func_done(zio_t *zio) 1913770499e1SDan Kimmel { 1914770499e1SDan Kimmel abd_put(zio->io_abd); 1915770499e1SDan Kimmel } 1916770499e1SDan Kimmel 1917e14bb325SJeff Bonwick static zio_t * 1918770499e1SDan Kimmel zio_read_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data, 1919770499e1SDan Kimmel uint64_t offset) 1920fa9e4066Sahrens { 1921e14bb325SJeff Bonwick if (gn != NULL) 1922e14bb325SJeff Bonwick return (pio); 1923fa9e4066Sahrens 1924770499e1SDan Kimmel return (zio_read(pio, pio->io_spa, bp, abd_get_offset(data, offset), 1925770499e1SDan Kimmel BP_GET_PSIZE(bp), zio_gang_issue_func_done, 1926770499e1SDan Kimmel NULL, pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio), 1927e14bb325SJeff Bonwick &pio->io_bookmark)); 1928e14bb325SJeff Bonwick } 1929e14bb325SJeff Bonwick 1930770499e1SDan Kimmel static zio_t * 1931770499e1SDan Kimmel zio_rewrite_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data, 1932770499e1SDan Kimmel uint64_t offset) 1933e14bb325SJeff Bonwick { 1934e14bb325SJeff Bonwick zio_t *zio; 1935e14bb325SJeff Bonwick 1936e14bb325SJeff Bonwick if (gn != NULL) { 1937770499e1SDan Kimmel abd_t *gbh_abd = 1938770499e1SDan Kimmel abd_get_from_buf(gn->gn_gbh, SPA_GANGBLOCKSIZE); 1939e14bb325SJeff Bonwick zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp, 1940770499e1SDan Kimmel gbh_abd, SPA_GANGBLOCKSIZE, zio_gang_issue_func_done, NULL, 1941770499e1SDan Kimmel pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio), 1942770499e1SDan Kimmel &pio->io_bookmark); 1943fa9e4066Sahrens /* 1944e14bb325SJeff Bonwick * As we rewrite each gang header, the pipeline will compute 1945e14bb325SJeff Bonwick * a new gang block header checksum for it; but no one will 1946e14bb325SJeff Bonwick * compute a new data checksum, so we do that here. The one 1947e14bb325SJeff Bonwick * exception is the gang leader: the pipeline already computed 1948e14bb325SJeff Bonwick * its data checksum because that stage precedes gang assembly. 1949e14bb325SJeff Bonwick * (Presently, nothing actually uses interior data checksums; 1950e14bb325SJeff Bonwick * this is just good hygiene.) 1951fa9e4066Sahrens */ 1952f5383399SBill Moore if (gn != pio->io_gang_leader->io_gang_tree) { 1953770499e1SDan Kimmel abd_t *buf = abd_get_offset(data, offset); 1954770499e1SDan Kimmel 1955e14bb325SJeff Bonwick zio_checksum_compute(zio, BP_GET_CHECKSUM(bp), 1956770499e1SDan Kimmel buf, BP_GET_PSIZE(bp)); 1957770499e1SDan Kimmel 1958770499e1SDan Kimmel abd_put(buf); 1959e14bb325SJeff Bonwick } 1960b24ab676SJeff Bonwick /* 1961b24ab676SJeff Bonwick * If we are here to damage data for testing purposes, 1962b24ab676SJeff Bonwick * leave the GBH alone so that we can detect the damage. 1963b24ab676SJeff Bonwick */ 1964b24ab676SJeff Bonwick if (pio->io_gang_leader->io_flags & ZIO_FLAG_INDUCE_DAMAGE) 1965b24ab676SJeff Bonwick zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES; 1966fa9e4066Sahrens } else { 1967e14bb325SJeff Bonwick zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp, 1968770499e1SDan Kimmel abd_get_offset(data, offset), BP_GET_PSIZE(bp), 1969770499e1SDan Kimmel zio_gang_issue_func_done, NULL, pio->io_priority, 1970e14bb325SJeff Bonwick ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark); 1971fa9e4066Sahrens } 1972fa9e4066Sahrens 1973e14bb325SJeff Bonwick return (zio); 1974e14bb325SJeff Bonwick } 1975fa9e4066Sahrens 1976e14bb325SJeff Bonwick /* ARGSUSED */ 1977770499e1SDan Kimmel static zio_t * 1978770499e1SDan Kimmel zio_free_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data, 1979770499e1SDan Kimmel uint64_t offset) 1980e14bb325SJeff Bonwick { 1981b24ab676SJeff Bonwick return (zio_free_sync(pio, pio->io_spa, pio->io_txg, bp, 1982b24ab676SJeff Bonwick ZIO_GANG_CHILD_FLAGS(pio))); 1983fa9e4066Sahrens } 1984fa9e4066Sahrens 1985e14bb325SJeff Bonwick /* ARGSUSED */ 1986770499e1SDan Kimmel static zio_t * 1987770499e1SDan Kimmel zio_claim_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data, 1988770499e1SDan Kimmel uint64_t offset) 1989fa9e4066Sahrens { 1990e14bb325SJeff Bonwick return (zio_claim(pio, pio->io_spa, pio->io_txg, bp, 1991e14bb325SJeff Bonwick NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio))); 1992e14bb325SJeff Bonwick } 1993fa9e4066Sahrens 1994e14bb325SJeff Bonwick static zio_gang_issue_func_t *zio_gang_issue_func[ZIO_TYPES] = { 1995e14bb325SJeff Bonwick NULL, 1996e14bb325SJeff Bonwick zio_read_gang, 1997e14bb325SJeff Bonwick zio_rewrite_gang, 1998e14bb325SJeff Bonwick zio_free_gang, 1999e14bb325SJeff Bonwick zio_claim_gang, 2000e14bb325SJeff Bonwick NULL 2001e14bb325SJeff Bonwick }; 2002fa9e4066Sahrens 2003e14bb325SJeff Bonwick static void zio_gang_tree_assemble_done(zio_t *zio); 2004fa9e4066Sahrens 2005e14bb325SJeff Bonwick static zio_gang_node_t * 2006e14bb325SJeff Bonwick zio_gang_node_alloc(zio_gang_node_t **gnpp) 2007e14bb325SJeff Bonwick { 2008e14bb325SJeff Bonwick zio_gang_node_t *gn; 2009fa9e4066Sahrens 2010e14bb325SJeff Bonwick ASSERT(*gnpp == NULL); 2011fa9e4066Sahrens 2012e14bb325SJeff Bonwick gn = kmem_zalloc(sizeof (*gn), KM_SLEEP); 2013e14bb325SJeff Bonwick gn->gn_gbh = zio_buf_alloc(SPA_GANGBLOCKSIZE); 2014e14bb325SJeff Bonwick *gnpp = gn; 2015e14bb325SJeff Bonwick 2016e14bb325SJeff Bonwick return (gn); 2017fa9e4066Sahrens } 2018fa9e4066Sahrens 2019fa9e4066Sahrens static void 2020e14bb325SJeff Bonwick zio_gang_node_free(zio_gang_node_t **gnpp) 2021fa9e4066Sahrens { 2022e14bb325SJeff Bonwick zio_gang_node_t *gn = *gnpp; 2023fa9e4066Sahrens 2024e14bb325SJeff Bonwick for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) 2025e14bb325SJeff Bonwick ASSERT(gn->gn_child[g] == NULL); 2026e14bb325SJeff Bonwick 2027e14bb325SJeff Bonwick zio_buf_free(gn->gn_gbh, SPA_GANGBLOCKSIZE); 2028e14bb325SJeff Bonwick kmem_free(gn, sizeof (*gn)); 2029e14bb325SJeff Bonwick *gnpp = NULL; 2030fa9e4066Sahrens } 2031fa9e4066Sahrens 2032e14bb325SJeff Bonwick static void 2033e14bb325SJeff Bonwick zio_gang_tree_free(zio_gang_node_t **gnpp) 2034fa9e4066Sahrens { 2035e14bb325SJeff Bonwick zio_gang_node_t *gn = *gnpp; 2036fa9e4066Sahrens 2037e14bb325SJeff Bonwick if (gn == NULL) 2038e14bb325SJeff Bonwick return; 2039fa9e4066Sahrens 2040e14bb325SJeff Bonwick for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) 2041e14bb325SJeff Bonwick zio_gang_tree_free(&gn->gn_child[g]); 2042fa9e4066Sahrens 2043e14bb325SJeff Bonwick zio_gang_node_free(gnpp); 2044fa9e4066Sahrens } 2045fa9e4066Sahrens 2046e14bb325SJeff Bonwick static void 2047f5383399SBill Moore zio_gang_tree_assemble(zio_t *gio, blkptr_t *bp, zio_gang_node_t **gnpp) 2048fa9e4066Sahrens { 2049e14bb325SJeff Bonwick zio_gang_node_t *gn = zio_gang_node_alloc(gnpp); 2050770499e1SDan Kimmel abd_t *gbh_abd = abd_get_from_buf(gn->gn_gbh, SPA_GANGBLOCKSIZE); 2051e14bb325SJeff Bonwick 2052f5383399SBill Moore ASSERT(gio->io_gang_leader == gio); 2053e14bb325SJeff Bonwick ASSERT(BP_IS_GANG(bp)); 2054fa9e4066Sahrens 2055770499e1SDan Kimmel zio_nowait(zio_read(gio, gio->io_spa, bp, gbh_abd, SPA_GANGBLOCKSIZE, 2056770499e1SDan Kimmel zio_gang_tree_assemble_done, gn, gio->io_priority, 2057770499e1SDan Kimmel ZIO_GANG_CHILD_FLAGS(gio), &gio->io_bookmark)); 2058e14bb325SJeff Bonwick } 2059fa9e4066Sahrens 2060e14bb325SJeff Bonwick static void 2061e14bb325SJeff Bonwick zio_gang_tree_assemble_done(zio_t *zio) 2062e14bb325SJeff Bonwick { 2063f5383399SBill Moore zio_t *gio = zio->io_gang_leader; 2064e14bb325SJeff Bonwick zio_gang_node_t *gn = zio->io_private; 2065e14bb325SJeff Bonwick blkptr_t *bp = zio->io_bp; 2066fa9e4066Sahrens 2067f5383399SBill Moore ASSERT(gio == zio_unique_parent(zio)); 2068b24ab676SJeff Bonwick ASSERT(zio->io_child_count == 0); 2069fa9e4066Sahrens 2070e14bb325SJeff Bonwick if (zio->io_error) 2071e14bb325SJeff Bonwick return; 2072fa9e4066Sahrens 2073770499e1SDan Kimmel /* this ABD was created from a linear buf in zio_gang_tree_assemble */ 2074e14bb325SJeff Bonwick if (BP_SHOULD_BYTESWAP(bp)) 2075770499e1SDan Kimmel byteswap_uint64_array(abd_to_buf(zio->io_abd), zio->io_size); 2076fa9e4066Sahrens 2077770499e1SDan Kimmel ASSERT3P(abd_to_buf(zio->io_abd), ==, gn->gn_gbh); 2078e14bb325SJeff Bonwick ASSERT(zio->io_size == SPA_GANGBLOCKSIZE); 20796e1f5caaSNeil Perrin ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC); 2080e05725b1Sbonwick 2081770499e1SDan Kimmel abd_put(zio->io_abd); 2082770499e1SDan Kimmel 2083e14bb325SJeff Bonwick for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) { 2084e14bb325SJeff Bonwick blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g]; 2085e14bb325SJeff Bonwick if (!BP_IS_GANG(gbp)) 2086e14bb325SJeff Bonwick continue; 2087f5383399SBill Moore zio_gang_tree_assemble(gio, gbp, &gn->gn_child[g]); 2088e14bb325SJeff Bonwick } 2089fa9e4066Sahrens } 2090fa9e4066Sahrens 2091e14bb325SJeff Bonwick static void 2092770499e1SDan Kimmel zio_gang_tree_issue(zio_t *pio, zio_gang_node_t *gn, blkptr_t *bp, abd_t *data, 2093770499e1SDan Kimmel uint64_t offset) 2094fa9e4066Sahrens { 2095f5383399SBill Moore zio_t *gio = pio->io_gang_leader; 2096e14bb325SJeff Bonwick zio_t *zio; 2097fa9e4066Sahrens 2098e14bb325SJeff Bonwick ASSERT(BP_IS_GANG(bp) == !!gn); 2099f5383399SBill Moore ASSERT(BP_GET_CHECKSUM(bp) == BP_GET_CHECKSUM(gio->io_bp)); 2100f5383399SBill Moore ASSERT(BP_GET_LSIZE(bp) == BP_GET_PSIZE(bp) || gn == gio->io_gang_tree); 2101fa9e4066Sahrens 2102e14bb325SJeff Bonwick /* 2103e14bb325SJeff Bonwick * If you're a gang header, your data is in gn->gn_gbh. 2104e14bb325SJeff Bonwick * If you're a gang member, your data is in 'data' and gn == NULL. 2105e14bb325SJeff Bonwick */ 2106770499e1SDan Kimmel zio = zio_gang_issue_func[gio->io_type](pio, bp, gn, data, offset); 2107fa9e4066Sahrens 2108e14bb325SJeff Bonwick if (gn != NULL) { 21096e1f5caaSNeil Perrin ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC); 2110fa9e4066Sahrens 2111e14bb325SJeff Bonwick for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) { 2112e14bb325SJeff Bonwick blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g]; 2113e14bb325SJeff Bonwick if (BP_IS_HOLE(gbp)) 2114e14bb325SJeff Bonwick continue; 2115770499e1SDan Kimmel zio_gang_tree_issue(zio, gn->gn_child[g], gbp, data, 2116770499e1SDan Kimmel offset); 2117770499e1SDan Kimmel offset += BP_GET_PSIZE(gbp); 2118e14bb325SJeff Bonwick } 2119fa9e4066Sahrens } 2120fa9e4066Sahrens 2121f5383399SBill Moore if (gn == gio->io_gang_tree) 2122770499e1SDan Kimmel ASSERT3U(gio->io_size, ==, offset); 2123e05725b1Sbonwick 2124e14bb325SJeff Bonwick if (zio != pio) 2125e14bb325SJeff Bonwick zio_nowait(zio); 2126fa9e4066Sahrens } 2127fa9e4066Sahrens 2128e05725b1Sbonwick static int 2129e14bb325SJeff Bonwick zio_gang_assemble(zio_t *zio) 2130fa9e4066Sahrens { 2131e14bb325SJeff Bonwick blkptr_t *bp = zio->io_bp; 2132fa9e4066Sahrens 2133f5383399SBill Moore ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == NULL); 2134f5383399SBill Moore ASSERT(zio->io_child_type > ZIO_CHILD_GANG); 2135f5383399SBill Moore 2136f5383399SBill Moore zio->io_gang_leader = zio; 2137fa9e4066Sahrens 2138e14bb325SJeff Bonwick zio_gang_tree_assemble(zio, bp, &zio->io_gang_tree); 2139e05725b1Sbonwick 2140e05725b1Sbonwick return (ZIO_PIPELINE_CONTINUE); 2141fa9e4066Sahrens } 2142fa9e4066Sahrens 2143e05725b1Sbonwick static int 2144e14bb325SJeff Bonwick zio_gang_issue(zio_t *zio) 2145fa9e4066Sahrens { 2146e14bb325SJeff Bonwick blkptr_t *bp = zio->io_bp; 2147fa9e4066Sahrens 2148d6e1c446SGeorge Wilson if (zio_wait_for_children(zio, ZIO_CHILD_GANG_BIT, ZIO_WAIT_DONE)) { 2149e14bb325SJeff Bonwick return (ZIO_PIPELINE_STOP); 2150d6e1c446SGeorge Wilson } 2151fa9e4066Sahrens 2152f5383399SBill Moore ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == zio); 2153f5383399SBill Moore ASSERT(zio->io_child_type > ZIO_CHILD_GANG); 2154fa9e4066Sahrens 2155e14bb325SJeff Bonwick if (zio->io_child_error[ZIO_CHILD_GANG] == 0) 2156770499e1SDan Kimmel zio_gang_tree_issue(zio, zio->io_gang_tree, bp, zio->io_abd, 2157770499e1SDan Kimmel 0); 2158e14bb325SJeff Bonwick else 2159f5383399SBill Moore zio_gang_tree_free(&zio->io_gang_tree); 2160fa9e4066Sahrens 2161e14bb325SJeff Bonwick zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 2162e05725b1Sbonwick 2163e05725b1Sbonwick return (ZIO_PIPELINE_CONTINUE); 2164fa9e4066Sahrens } 2165fa9e4066Sahrens 2166fa9e4066Sahrens static void 2167e14bb325SJeff Bonwick zio_write_gang_member_ready(zio_t *zio) 2168fa9e4066Sahrens { 2169a3f829aeSBill Moore zio_t *pio = zio_unique_parent(zio); 2170f5383399SBill Moore zio_t *gio = zio->io_gang_leader; 217144cd46caSbillm dva_t *cdva = zio->io_bp->blk_dva; 217244cd46caSbillm dva_t *pdva = pio->io_bp->blk_dva; 2173fa9e4066Sahrens uint64_t asize; 2174fa9e4066Sahrens 2175e14bb325SJeff Bonwick if (BP_IS_HOLE(zio->io_bp)) 2176e14bb325SJeff Bonwick return; 2177e14bb325SJeff Bonwick 2178e14bb325SJeff Bonwick ASSERT(BP_IS_HOLE(&zio->io_bp_orig)); 2179e14bb325SJeff Bonwick 2180e14bb325SJeff Bonwick ASSERT(zio->io_child_type == ZIO_CHILD_GANG); 2181b24ab676SJeff Bonwick ASSERT3U(zio->io_prop.zp_copies, ==, gio->io_prop.zp_copies); 2182b24ab676SJeff Bonwick ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(zio->io_bp)); 2183b24ab676SJeff Bonwick ASSERT3U(pio->io_prop.zp_copies, <=, BP_GET_NDVAS(pio->io_bp)); 218444cd46caSbillm ASSERT3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp)); 2185fa9e4066Sahrens 2186fa9e4066Sahrens mutex_enter(&pio->io_lock); 2187e14bb325SJeff Bonwick for (int d = 0; d < BP_GET_NDVAS(zio->io_bp); d++) { 218844cd46caSbillm ASSERT(DVA_GET_GANG(&pdva[d])); 218944cd46caSbillm asize = DVA_GET_ASIZE(&pdva[d]); 219044cd46caSbillm asize += DVA_GET_ASIZE(&cdva[d]); 219144cd46caSbillm DVA_SET_ASIZE(&pdva[d], asize); 219244cd46caSbillm } 2193fa9e4066Sahrens mutex_exit(&pio->io_lock); 2194fa9e4066Sahrens } 2195fa9e4066Sahrens 2196770499e1SDan Kimmel static void 2197770499e1SDan Kimmel zio_write_gang_done(zio_t *zio) 2198770499e1SDan Kimmel { 21997341a7deSBrad Lewis /* 22007341a7deSBrad Lewis * The io_abd field will be NULL for a zio with no data. The io_flags 22017341a7deSBrad Lewis * will initially have the ZIO_FLAG_NODATA bit flag set, but we can't 22027341a7deSBrad Lewis * check for it here as it is cleared in zio_ready. 22037341a7deSBrad Lewis */ 22047341a7deSBrad Lewis if (zio->io_abd != NULL) 22057341a7deSBrad Lewis abd_put(zio->io_abd); 2206770499e1SDan Kimmel } 2207770499e1SDan Kimmel 22080a4e9518Sgw static int 2209e14bb325SJeff Bonwick zio_write_gang_block(zio_t *pio) 2210fa9e4066Sahrens { 2211e14bb325SJeff Bonwick spa_t *spa = pio->io_spa; 22120f7643c7SGeorge Wilson metaslab_class_t *mc = spa_normal_class(spa); 2213e14bb325SJeff Bonwick blkptr_t *bp = pio->io_bp; 2214f5383399SBill Moore zio_t *gio = pio->io_gang_leader; 2215e14bb325SJeff Bonwick zio_t *zio; 2216e14bb325SJeff Bonwick zio_gang_node_t *gn, **gnpp; 2217fa9e4066Sahrens zio_gbh_phys_t *gbh; 2218770499e1SDan Kimmel abd_t *gbh_abd; 2219e14bb325SJeff Bonwick uint64_t txg = pio->io_txg; 2220e14bb325SJeff Bonwick uint64_t resid = pio->io_size; 2221e14bb325SJeff Bonwick uint64_t lsize; 2222b24ab676SJeff Bonwick int copies = gio->io_prop.zp_copies; 2223b24ab676SJeff Bonwick int gbh_copies = MIN(copies + 1, spa_max_replication(spa)); 2224e14bb325SJeff Bonwick zio_prop_t zp; 2225fa9e4066Sahrens int error; 22267341a7deSBrad Lewis boolean_t has_data = !(pio->io_flags & ZIO_FLAG_NODATA); 2227fa9e4066Sahrens 22280f7643c7SGeorge Wilson int flags = METASLAB_HINTBP_FAVOR | METASLAB_GANG_HEADER; 22290f7643c7SGeorge Wilson if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) { 22300f7643c7SGeorge Wilson ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE); 22317341a7deSBrad Lewis ASSERT(has_data); 22320f7643c7SGeorge Wilson 22330f7643c7SGeorge Wilson flags |= METASLAB_ASYNC_ALLOC; 2234e914ace2STim Schumacher VERIFY(zfs_refcount_held(&mc->mc_alloc_slots[pio->io_allocator], 2235f78cdc34SPaul Dagnelie pio)); 22360f7643c7SGeorge Wilson 22370f7643c7SGeorge Wilson /* 22380f7643c7SGeorge Wilson * The logical zio has already placed a reservation for 22390f7643c7SGeorge Wilson * 'copies' allocation slots but gang blocks may require 22400f7643c7SGeorge Wilson * additional copies. These additional copies 22410f7643c7SGeorge Wilson * (i.e. gbh_copies - copies) are guaranteed to succeed 22420f7643c7SGeorge Wilson * since metaslab_class_throttle_reserve() always allows 22430f7643c7SGeorge Wilson * additional reservations for gang blocks. 22440f7643c7SGeorge Wilson */ 22450f7643c7SGeorge Wilson VERIFY(metaslab_class_throttle_reserve(mc, gbh_copies - copies, 2246f78cdc34SPaul Dagnelie pio->io_allocator, pio, flags)); 22470f7643c7SGeorge Wilson } 22480f7643c7SGeorge Wilson 22490f7643c7SGeorge Wilson error = metaslab_alloc(spa, mc, SPA_GANGBLOCKSIZE, 22508363e80aSGeorge Wilson bp, gbh_copies, txg, pio == gio ? NULL : gio->io_bp, flags, 2251f78cdc34SPaul Dagnelie &pio->io_alloc_list, pio, pio->io_allocator); 2252e05725b1Sbonwick if (error) { 22530f7643c7SGeorge Wilson if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) { 22540f7643c7SGeorge Wilson ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE); 22557341a7deSBrad Lewis ASSERT(has_data); 22560f7643c7SGeorge Wilson 22570f7643c7SGeorge Wilson /* 22580f7643c7SGeorge Wilson * If we failed to allocate the gang block header then 22590f7643c7SGeorge Wilson * we remove any additional allocation reservations that 22600f7643c7SGeorge Wilson * we placed here. The original reservation will 22610f7643c7SGeorge Wilson * be removed when the logical I/O goes to the ready 22620f7643c7SGeorge Wilson * stage. 22630f7643c7SGeorge Wilson */ 22640f7643c7SGeorge Wilson metaslab_class_throttle_unreserve(mc, 2265f78cdc34SPaul Dagnelie gbh_copies - copies, pio->io_allocator, pio); 22660f7643c7SGeorge Wilson } 2267e14bb325SJeff Bonwick pio->io_error = error; 2268e05725b1Sbonwick return (ZIO_PIPELINE_CONTINUE); 2269e05725b1Sbonwick } 2270fa9e4066Sahrens 2271f5383399SBill Moore if (pio == gio) { 2272f5383399SBill Moore gnpp = &gio->io_gang_tree; 2273e14bb325SJeff Bonwick } else { 2274e14bb325SJeff Bonwick gnpp = pio->io_private; 2275e14bb325SJeff Bonwick ASSERT(pio->io_ready == zio_write_gang_member_ready); 2276fa9e4066Sahrens } 2277fa9e4066Sahrens 2278e14bb325SJeff Bonwick gn = zio_gang_node_alloc(gnpp); 2279e14bb325SJeff Bonwick gbh = gn->gn_gbh; 2280e14bb325SJeff Bonwick bzero(gbh, SPA_GANGBLOCKSIZE); 2281770499e1SDan Kimmel gbh_abd = abd_get_from_buf(gbh, SPA_GANGBLOCKSIZE); 2282fa9e4066Sahrens 2283e14bb325SJeff Bonwick /* 2284e14bb325SJeff Bonwick * Create the gang header. 2285e14bb325SJeff Bonwick */ 2286770499e1SDan Kimmel zio = zio_rewrite(pio, spa, txg, bp, gbh_abd, SPA_GANGBLOCKSIZE, 2287770499e1SDan Kimmel zio_write_gang_done, NULL, pio->io_priority, 2288770499e1SDan Kimmel ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark); 2289fa9e4066Sahrens 2290e14bb325SJeff Bonwick /* 2291e14bb325SJeff Bonwick * Create and nowait the gang children. 2292e14bb325SJeff Bonwick */ 2293e14bb325SJeff Bonwick for (int g = 0; resid != 0; resid -= lsize, g++) { 2294e14bb325SJeff Bonwick lsize = P2ROUNDUP(resid / (SPA_GBH_NBLKPTRS - g), 2295e14bb325SJeff Bonwick SPA_MINBLOCKSIZE); 2296e14bb325SJeff Bonwick ASSERT(lsize >= SPA_MINBLOCKSIZE && lsize <= resid); 2297e14bb325SJeff Bonwick 2298f5383399SBill Moore zp.zp_checksum = gio->io_prop.zp_checksum; 2299e14bb325SJeff Bonwick zp.zp_compress = ZIO_COMPRESS_OFF; 2300e14bb325SJeff Bonwick zp.zp_type = DMU_OT_NONE; 2301e14bb325SJeff Bonwick zp.zp_level = 0; 2302b24ab676SJeff Bonwick zp.zp_copies = gio->io_prop.zp_copies; 230380901aeaSGeorge Wilson zp.zp_dedup = B_FALSE; 230480901aeaSGeorge Wilson zp.zp_dedup_verify = B_FALSE; 230580901aeaSGeorge Wilson zp.zp_nopwrite = B_FALSE; 2306e14bb325SJeff Bonwick 23070f7643c7SGeorge Wilson zio_t *cio = zio_write(zio, spa, txg, &gbh->zg_blkptr[g], 23087341a7deSBrad Lewis has_data ? abd_get_offset(pio->io_abd, pio->io_size - 23097341a7deSBrad Lewis resid) : NULL, lsize, lsize, &zp, 23107341a7deSBrad Lewis zio_write_gang_member_ready, NULL, NULL, 2311770499e1SDan Kimmel zio_write_gang_done, &gn->gn_child[g], pio->io_priority, 23120f7643c7SGeorge Wilson ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark); 23130f7643c7SGeorge Wilson 23140f7643c7SGeorge Wilson if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) { 23150f7643c7SGeorge Wilson ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE); 23167341a7deSBrad Lewis ASSERT(has_data); 23170f7643c7SGeorge Wilson 23180f7643c7SGeorge Wilson /* 23190f7643c7SGeorge Wilson * Gang children won't throttle but we should 23200f7643c7SGeorge Wilson * account for their work, so reserve an allocation 23210f7643c7SGeorge Wilson * slot for them here. 23220f7643c7SGeorge Wilson */ 23230f7643c7SGeorge Wilson VERIFY(metaslab_class_throttle_reserve(mc, 2324f78cdc34SPaul Dagnelie zp.zp_copies, cio->io_allocator, cio, flags)); 23250f7643c7SGeorge Wilson } 23260f7643c7SGeorge Wilson zio_nowait(cio); 2327e14bb325SJeff Bonwick } 2328e05725b1Sbonwick 232944cd46caSbillm /* 2330e14bb325SJeff Bonwick * Set pio's pipeline to just wait for zio to finish. 233144cd46caSbillm */ 2332e14bb325SJeff Bonwick pio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 2333e14bb325SJeff Bonwick 2334e14bb325SJeff Bonwick zio_nowait(zio); 2335e14bb325SJeff Bonwick 2336e14bb325SJeff Bonwick return (ZIO_PIPELINE_CONTINUE); 2337fa9e4066Sahrens } 2338fa9e4066Sahrens 233980901aeaSGeorge Wilson /* 234045818ee1SMatthew Ahrens * The zio_nop_write stage in the pipeline determines if allocating a 234145818ee1SMatthew Ahrens * new bp is necessary. The nopwrite feature can handle writes in 234245818ee1SMatthew Ahrens * either syncing or open context (i.e. zil writes) and as a result is 234345818ee1SMatthew Ahrens * mutually exclusive with dedup. 234445818ee1SMatthew Ahrens * 234545818ee1SMatthew Ahrens * By leveraging a cryptographically secure checksum, such as SHA256, we 234645818ee1SMatthew Ahrens * can compare the checksums of the new data and the old to determine if 234745818ee1SMatthew Ahrens * allocating a new block is required. Note that our requirements for 234845818ee1SMatthew Ahrens * cryptographic strength are fairly weak: there can't be any accidental 234945818ee1SMatthew Ahrens * hash collisions, but we don't need to be secure against intentional 235045818ee1SMatthew Ahrens * (malicious) collisions. To trigger a nopwrite, you have to be able 235145818ee1SMatthew Ahrens * to write the file to begin with, and triggering an incorrect (hash 235245818ee1SMatthew Ahrens * collision) nopwrite is no worse than simply writing to the file. 235345818ee1SMatthew Ahrens * That said, there are no known attacks against the checksum algorithms 235445818ee1SMatthew Ahrens * used for nopwrite, assuming that the salt and the checksums 235545818ee1SMatthew Ahrens * themselves remain secret. 235680901aeaSGeorge Wilson */ 235780901aeaSGeorge Wilson static int 235880901aeaSGeorge Wilson zio_nop_write(zio_t *zio) 235980901aeaSGeorge Wilson { 236080901aeaSGeorge Wilson blkptr_t *bp = zio->io_bp; 236180901aeaSGeorge Wilson blkptr_t *bp_orig = &zio->io_bp_orig; 236280901aeaSGeorge Wilson zio_prop_t *zp = &zio->io_prop; 236380901aeaSGeorge Wilson 236480901aeaSGeorge Wilson ASSERT(BP_GET_LEVEL(bp) == 0); 236580901aeaSGeorge Wilson ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE)); 236680901aeaSGeorge Wilson ASSERT(zp->zp_nopwrite); 236780901aeaSGeorge Wilson ASSERT(!zp->zp_dedup); 236880901aeaSGeorge Wilson ASSERT(zio->io_bp_override == NULL); 236980901aeaSGeorge Wilson ASSERT(IO_IS_ALLOCATING(zio)); 237080901aeaSGeorge Wilson 237180901aeaSGeorge Wilson /* 237280901aeaSGeorge Wilson * Check to see if the original bp and the new bp have matching 237380901aeaSGeorge Wilson * characteristics (i.e. same checksum, compression algorithms, etc). 237480901aeaSGeorge Wilson * If they don't then just continue with the pipeline which will 237580901aeaSGeorge Wilson * allocate a new bp. 237680901aeaSGeorge Wilson */ 237780901aeaSGeorge Wilson if (BP_IS_HOLE(bp_orig) || 237845818ee1SMatthew Ahrens !(zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_flags & 237945818ee1SMatthew Ahrens ZCHECKSUM_FLAG_NOPWRITE) || 238080901aeaSGeorge Wilson BP_GET_CHECKSUM(bp) != BP_GET_CHECKSUM(bp_orig) || 238180901aeaSGeorge Wilson BP_GET_COMPRESS(bp) != BP_GET_COMPRESS(bp_orig) || 238280901aeaSGeorge Wilson BP_GET_DEDUP(bp) != BP_GET_DEDUP(bp_orig) || 238380901aeaSGeorge Wilson zp->zp_copies != BP_GET_NDVAS(bp_orig)) 238480901aeaSGeorge Wilson return (ZIO_PIPELINE_CONTINUE); 238580901aeaSGeorge Wilson 238680901aeaSGeorge Wilson /* 238780901aeaSGeorge Wilson * If the checksums match then reset the pipeline so that we 238880901aeaSGeorge Wilson * avoid allocating a new bp and issuing any I/O. 238980901aeaSGeorge Wilson */ 239080901aeaSGeorge Wilson if (ZIO_CHECKSUM_EQUAL(bp->blk_cksum, bp_orig->blk_cksum)) { 239145818ee1SMatthew Ahrens ASSERT(zio_checksum_table[zp->zp_checksum].ci_flags & 239245818ee1SMatthew Ahrens ZCHECKSUM_FLAG_NOPWRITE); 239380901aeaSGeorge Wilson ASSERT3U(BP_GET_PSIZE(bp), ==, BP_GET_PSIZE(bp_orig)); 239480901aeaSGeorge Wilson ASSERT3U(BP_GET_LSIZE(bp), ==, BP_GET_LSIZE(bp_orig)); 239580901aeaSGeorge Wilson ASSERT(zp->zp_compress != ZIO_COMPRESS_OFF); 239680901aeaSGeorge Wilson ASSERT(bcmp(&bp->blk_prop, &bp_orig->blk_prop, 239780901aeaSGeorge Wilson sizeof (uint64_t)) == 0); 239880901aeaSGeorge Wilson 239980901aeaSGeorge Wilson *bp = *bp_orig; 240080901aeaSGeorge Wilson zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 240180901aeaSGeorge Wilson zio->io_flags |= ZIO_FLAG_NOPWRITE; 240280901aeaSGeorge Wilson } 240380901aeaSGeorge Wilson 240480901aeaSGeorge Wilson return (ZIO_PIPELINE_CONTINUE); 240580901aeaSGeorge Wilson } 240680901aeaSGeorge Wilson 2407fa9e4066Sahrens /* 2408fa9e4066Sahrens * ========================================================================== 2409b24ab676SJeff Bonwick * Dedup 2410fa9e4066Sahrens * ========================================================================== 2411fa9e4066Sahrens */ 2412b24ab676SJeff Bonwick static void 2413b24ab676SJeff Bonwick zio_ddt_child_read_done(zio_t *zio) 2414b24ab676SJeff Bonwick { 2415b24ab676SJeff Bonwick blkptr_t *bp = zio->io_bp; 2416b24ab676SJeff Bonwick ddt_entry_t *dde = zio->io_private; 2417b24ab676SJeff Bonwick ddt_phys_t *ddp; 2418b24ab676SJeff Bonwick zio_t *pio = zio_unique_parent(zio); 2419b24ab676SJeff Bonwick 2420b24ab676SJeff Bonwick mutex_enter(&pio->io_lock); 2421b24ab676SJeff Bonwick ddp = ddt_phys_select(dde, bp); 2422b24ab676SJeff Bonwick if (zio->io_error == 0) 2423b24ab676SJeff Bonwick ddt_phys_clear(ddp); /* this ddp doesn't need repair */ 2424770499e1SDan Kimmel 2425770499e1SDan Kimmel if (zio->io_error == 0 && dde->dde_repair_abd == NULL) 2426770499e1SDan Kimmel dde->dde_repair_abd = zio->io_abd; 2427b24ab676SJeff Bonwick else 2428770499e1SDan Kimmel abd_free(zio->io_abd); 2429b24ab676SJeff Bonwick mutex_exit(&pio->io_lock); 2430b24ab676SJeff Bonwick } 2431b24ab676SJeff Bonwick 2432b24ab676SJeff Bonwick static int 2433b24ab676SJeff Bonwick zio_ddt_read_start(zio_t *zio) 2434b24ab676SJeff Bonwick { 2435b24ab676SJeff Bonwick blkptr_t *bp = zio->io_bp; 2436b24ab676SJeff Bonwick 2437b24ab676SJeff Bonwick ASSERT(BP_GET_DEDUP(bp)); 2438b24ab676SJeff Bonwick ASSERT(BP_GET_PSIZE(bp) == zio->io_size); 2439b24ab676SJeff Bonwick ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 2440b24ab676SJeff Bonwick 2441b24ab676SJeff Bonwick if (zio->io_child_error[ZIO_CHILD_DDT]) { 2442b24ab676SJeff Bonwick ddt_t *ddt = ddt_select(zio->io_spa, bp); 2443b24ab676SJeff Bonwick ddt_entry_t *dde = ddt_repair_start(ddt, bp); 2444b24ab676SJeff Bonwick ddt_phys_t *ddp = dde->dde_phys; 2445b24ab676SJeff Bonwick ddt_phys_t *ddp_self = ddt_phys_select(dde, bp); 2446b24ab676SJeff Bonwick blkptr_t blk; 2447b24ab676SJeff Bonwick 2448b24ab676SJeff Bonwick ASSERT(zio->io_vsd == NULL); 2449b24ab676SJeff Bonwick zio->io_vsd = dde; 2450b24ab676SJeff Bonwick 2451b24ab676SJeff Bonwick if (ddp_self == NULL) 2452b24ab676SJeff Bonwick return (ZIO_PIPELINE_CONTINUE); 2453b24ab676SJeff Bonwick 2454b24ab676SJeff Bonwick for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) { 2455b24ab676SJeff Bonwick if (ddp->ddp_phys_birth == 0 || ddp == ddp_self) 2456b24ab676SJeff Bonwick continue; 2457bbfd46c4SJeff Bonwick ddt_bp_create(ddt->ddt_checksum, &dde->dde_key, ddp, 2458bbfd46c4SJeff Bonwick &blk); 2459b24ab676SJeff Bonwick zio_nowait(zio_read(zio, zio->io_spa, &blk, 2460770499e1SDan Kimmel abd_alloc_for_io(zio->io_size, B_TRUE), 2461770499e1SDan Kimmel zio->io_size, zio_ddt_child_read_done, dde, 2462770499e1SDan Kimmel zio->io_priority, ZIO_DDT_CHILD_FLAGS(zio) | 2463770499e1SDan Kimmel ZIO_FLAG_DONT_PROPAGATE, &zio->io_bookmark)); 2464b24ab676SJeff Bonwick } 2465b24ab676SJeff Bonwick return (ZIO_PIPELINE_CONTINUE); 2466b24ab676SJeff Bonwick } 2467b24ab676SJeff Bonwick 2468b24ab676SJeff Bonwick zio_nowait(zio_read(zio, zio->io_spa, bp, 2469770499e1SDan Kimmel zio->io_abd, zio->io_size, NULL, NULL, zio->io_priority, 2470b24ab676SJeff Bonwick ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark)); 2471b24ab676SJeff Bonwick 2472b24ab676SJeff Bonwick return (ZIO_PIPELINE_CONTINUE); 2473b24ab676SJeff Bonwick } 2474e14bb325SJeff Bonwick 2475b24ab676SJeff Bonwick static int 2476b24ab676SJeff Bonwick zio_ddt_read_done(zio_t *zio) 2477b24ab676SJeff Bonwick { 2478b24ab676SJeff Bonwick blkptr_t *bp = zio->io_bp; 2479b24ab676SJeff Bonwick 2480d6e1c446SGeorge Wilson if (zio_wait_for_children(zio, ZIO_CHILD_DDT_BIT, ZIO_WAIT_DONE)) { 2481b24ab676SJeff Bonwick return (ZIO_PIPELINE_STOP); 2482d6e1c446SGeorge Wilson } 2483b24ab676SJeff Bonwick 2484b24ab676SJeff Bonwick ASSERT(BP_GET_DEDUP(bp)); 2485b24ab676SJeff Bonwick ASSERT(BP_GET_PSIZE(bp) == zio->io_size); 2486b24ab676SJeff Bonwick ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 2487b24ab676SJeff Bonwick 2488b24ab676SJeff Bonwick if (zio->io_child_error[ZIO_CHILD_DDT]) { 2489b24ab676SJeff Bonwick ddt_t *ddt = ddt_select(zio->io_spa, bp); 2490b24ab676SJeff Bonwick ddt_entry_t *dde = zio->io_vsd; 2491b24ab676SJeff Bonwick if (ddt == NULL) { 2492b16da2e2SGeorge Wilson ASSERT(spa_load_state(zio->io_spa) != SPA_LOAD_NONE); 2493b24ab676SJeff Bonwick return (ZIO_PIPELINE_CONTINUE); 2494b24ab676SJeff Bonwick } 2495b24ab676SJeff Bonwick if (dde == NULL) { 2496b24ab676SJeff Bonwick zio->io_stage = ZIO_STAGE_DDT_READ_START >> 1; 249735a5a358SJonathan Adams zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE); 2498b24ab676SJeff Bonwick return (ZIO_PIPELINE_STOP); 2499b24ab676SJeff Bonwick } 2500770499e1SDan Kimmel if (dde->dde_repair_abd != NULL) { 2501770499e1SDan Kimmel abd_copy(zio->io_abd, dde->dde_repair_abd, 2502770499e1SDan Kimmel zio->io_size); 2503b24ab676SJeff Bonwick zio->io_child_error[ZIO_CHILD_DDT] = 0; 2504b24ab676SJeff Bonwick } 2505b24ab676SJeff Bonwick ddt_repair_done(ddt, dde); 2506b24ab676SJeff Bonwick zio->io_vsd = NULL; 2507b24ab676SJeff Bonwick } 2508b24ab676SJeff Bonwick 2509b24ab676SJeff Bonwick ASSERT(zio->io_vsd == NULL); 2510b24ab676SJeff Bonwick 2511b24ab676SJeff Bonwick return (ZIO_PIPELINE_CONTINUE); 2512b24ab676SJeff Bonwick } 2513b24ab676SJeff Bonwick 2514b24ab676SJeff Bonwick static boolean_t 2515b24ab676SJeff Bonwick zio_ddt_collision(zio_t *zio, ddt_t *ddt, ddt_entry_t *dde) 2516b24ab676SJeff Bonwick { 2517b24ab676SJeff Bonwick spa_t *spa = zio->io_spa; 25185602294fSDan Kimmel boolean_t do_raw = (zio->io_flags & ZIO_FLAG_RAW); 25195602294fSDan Kimmel 25205602294fSDan Kimmel /* We should never get a raw, override zio */ 25215602294fSDan Kimmel ASSERT(!(zio->io_bp_override && do_raw)); 2522b24ab676SJeff Bonwick 2523b24ab676SJeff Bonwick /* 2524b24ab676SJeff Bonwick * Note: we compare the original data, not the transformed data, 2525b24ab676SJeff Bonwick * because when zio->io_bp is an override bp, we will not have 2526b24ab676SJeff Bonwick * pushed the I/O transforms. That's an important optimization 2527b24ab676SJeff Bonwick * because otherwise we'd compress/encrypt all dmu_sync() data twice. 2528b24ab676SJeff Bonwick */ 2529b24ab676SJeff Bonwick for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) { 2530b24ab676SJeff Bonwick zio_t *lio = dde->dde_lead_zio[p]; 2531b24ab676SJeff Bonwick 2532b24ab676SJeff Bonwick if (lio != NULL) { 2533b24ab676SJeff Bonwick return (lio->io_orig_size != zio->io_orig_size || 2534770499e1SDan Kimmel abd_cmp(zio->io_orig_abd, lio->io_orig_abd, 2535b24ab676SJeff Bonwick zio->io_orig_size) != 0); 2536b24ab676SJeff Bonwick } 2537b24ab676SJeff Bonwick } 2538b24ab676SJeff Bonwick 2539b24ab676SJeff Bonwick for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) { 2540b24ab676SJeff Bonwick ddt_phys_t *ddp = &dde->dde_phys[p]; 2541b24ab676SJeff Bonwick 2542b24ab676SJeff Bonwick if (ddp->ddp_phys_birth != 0) { 2543b24ab676SJeff Bonwick arc_buf_t *abuf = NULL; 25447adb730bSGeorge Wilson arc_flags_t aflags = ARC_FLAG_WAIT; 25455602294fSDan Kimmel int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE; 2546b24ab676SJeff Bonwick blkptr_t blk = *zio->io_bp; 2547b24ab676SJeff Bonwick int error; 2548b24ab676SJeff Bonwick 2549b24ab676SJeff Bonwick ddt_bp_fill(ddp, &blk, ddp->ddp_phys_birth); 2550b24ab676SJeff Bonwick 2551b24ab676SJeff Bonwick ddt_exit(ddt); 2552b24ab676SJeff Bonwick 25535602294fSDan Kimmel /* 25545602294fSDan Kimmel * Intuitively, it would make more sense to compare 2555770499e1SDan Kimmel * io_abd than io_orig_abd in the raw case since you 25565602294fSDan Kimmel * don't want to look at any transformations that have 25575602294fSDan Kimmel * happened to the data. However, for raw I/Os the 2558770499e1SDan Kimmel * data will actually be the same in io_abd and 2559770499e1SDan Kimmel * io_orig_abd, so all we have to do is issue this as 25605602294fSDan Kimmel * a raw ARC read. 25615602294fSDan Kimmel */ 25625602294fSDan Kimmel if (do_raw) { 25635602294fSDan Kimmel zio_flags |= ZIO_FLAG_RAW; 25645602294fSDan Kimmel ASSERT3U(zio->io_size, ==, zio->io_orig_size); 2565770499e1SDan Kimmel ASSERT0(abd_cmp(zio->io_abd, zio->io_orig_abd, 25665602294fSDan Kimmel zio->io_size)); 25675602294fSDan Kimmel ASSERT3P(zio->io_transform_stack, ==, NULL); 25685602294fSDan Kimmel } 25695602294fSDan Kimmel 25701b912ec7SGeorge Wilson error = arc_read(NULL, spa, &blk, 2571b24ab676SJeff Bonwick arc_getbuf_func, &abuf, ZIO_PRIORITY_SYNC_READ, 25725602294fSDan Kimmel zio_flags, &aflags, &zio->io_bookmark); 2573b24ab676SJeff Bonwick 2574b24ab676SJeff Bonwick if (error == 0) { 2575b24ab676SJeff Bonwick if (arc_buf_size(abuf) != zio->io_orig_size || 2576770499e1SDan Kimmel abd_cmp_buf(zio->io_orig_abd, abuf->b_data, 2577b24ab676SJeff Bonwick zio->io_orig_size) != 0) 2578be6fd75aSMatthew Ahrens error = SET_ERROR(EEXIST); 2579dcbf3bd6SGeorge Wilson arc_buf_destroy(abuf, &abuf); 2580b24ab676SJeff Bonwick } 2581b24ab676SJeff Bonwick 2582b24ab676SJeff Bonwick ddt_enter(ddt); 2583b24ab676SJeff Bonwick return (error != 0); 2584b24ab676SJeff Bonwick } 2585b24ab676SJeff Bonwick } 2586b24ab676SJeff Bonwick 2587b24ab676SJeff Bonwick return (B_FALSE); 2588b24ab676SJeff Bonwick } 2589b24ab676SJeff Bonwick 2590b24ab676SJeff Bonwick static void 2591b24ab676SJeff Bonwick zio_ddt_child_write_ready(zio_t *zio) 2592b24ab676SJeff Bonwick { 2593b24ab676SJeff Bonwick int p = zio->io_prop.zp_copies; 2594b24ab676SJeff Bonwick ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp); 2595b24ab676SJeff Bonwick ddt_entry_t *dde = zio->io_private; 2596b24ab676SJeff Bonwick ddt_phys_t *ddp = &dde->dde_phys[p]; 2597b24ab676SJeff Bonwick zio_t *pio; 2598b24ab676SJeff Bonwick 2599b24ab676SJeff Bonwick if (zio->io_error) 2600b24ab676SJeff Bonwick return; 2601b24ab676SJeff Bonwick 2602b24ab676SJeff Bonwick ddt_enter(ddt); 2603b24ab676SJeff Bonwick 2604b24ab676SJeff Bonwick ASSERT(dde->dde_lead_zio[p] == zio); 2605b24ab676SJeff Bonwick 2606b24ab676SJeff Bonwick ddt_phys_fill(ddp, zio->io_bp); 2607b24ab676SJeff Bonwick 26080f7643c7SGeorge Wilson zio_link_t *zl = NULL; 26090f7643c7SGeorge Wilson while ((pio = zio_walk_parents(zio, &zl)) != NULL) 2610b24ab676SJeff Bonwick ddt_bp_fill(ddp, pio->io_bp, zio->io_txg); 2611b24ab676SJeff Bonwick 2612b24ab676SJeff Bonwick ddt_exit(ddt); 2613b24ab676SJeff Bonwick } 2614b24ab676SJeff Bonwick 2615b24ab676SJeff Bonwick static void 2616b24ab676SJeff Bonwick zio_ddt_child_write_done(zio_t *zio) 2617b24ab676SJeff Bonwick { 2618b24ab676SJeff Bonwick int p = zio->io_prop.zp_copies; 2619b24ab676SJeff Bonwick ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp); 2620b24ab676SJeff Bonwick ddt_entry_t *dde = zio->io_private; 2621b24ab676SJeff Bonwick ddt_phys_t *ddp = &dde->dde_phys[p]; 2622b24ab676SJeff Bonwick 2623b24ab676SJeff Bonwick ddt_enter(ddt); 2624b24ab676SJeff Bonwick 2625b24ab676SJeff Bonwick ASSERT(ddp->ddp_refcnt == 0); 2626b24ab676SJeff Bonwick ASSERT(dde->dde_lead_zio[p] == zio); 2627b24ab676SJeff Bonwick dde->dde_lead_zio[p] = NULL; 2628b24ab676SJeff Bonwick 2629b24ab676SJeff Bonwick if (zio->io_error == 0) { 26300f7643c7SGeorge Wilson zio_link_t *zl = NULL; 26310f7643c7SGeorge Wilson while (zio_walk_parents(zio, &zl) != NULL) 2632b24ab676SJeff Bonwick ddt_phys_addref(ddp); 2633b24ab676SJeff Bonwick } else { 2634b24ab676SJeff Bonwick ddt_phys_clear(ddp); 2635b24ab676SJeff Bonwick } 2636b24ab676SJeff Bonwick 2637b24ab676SJeff Bonwick ddt_exit(ddt); 2638b24ab676SJeff Bonwick } 2639b24ab676SJeff Bonwick 2640b24ab676SJeff Bonwick static void 2641b24ab676SJeff Bonwick zio_ddt_ditto_write_done(zio_t *zio) 2642b24ab676SJeff Bonwick { 2643b24ab676SJeff Bonwick int p = DDT_PHYS_DITTO; 2644b24ab676SJeff Bonwick zio_prop_t *zp = &zio->io_prop; 2645b24ab676SJeff Bonwick blkptr_t *bp = zio->io_bp; 2646b24ab676SJeff Bonwick ddt_t *ddt = ddt_select(zio->io_spa, bp); 2647b24ab676SJeff Bonwick ddt_entry_t *dde = zio->io_private; 2648b24ab676SJeff Bonwick ddt_phys_t *ddp = &dde->dde_phys[p]; 2649b24ab676SJeff Bonwick ddt_key_t *ddk = &dde->dde_key; 2650b24ab676SJeff Bonwick 2651b24ab676SJeff Bonwick ddt_enter(ddt); 2652b24ab676SJeff Bonwick 2653b24ab676SJeff Bonwick ASSERT(ddp->ddp_refcnt == 0); 2654b24ab676SJeff Bonwick ASSERT(dde->dde_lead_zio[p] == zio); 2655b24ab676SJeff Bonwick dde->dde_lead_zio[p] = NULL; 2656b24ab676SJeff Bonwick 2657b24ab676SJeff Bonwick if (zio->io_error == 0) { 2658b24ab676SJeff Bonwick ASSERT(ZIO_CHECKSUM_EQUAL(bp->blk_cksum, ddk->ddk_cksum)); 2659b24ab676SJeff Bonwick ASSERT(zp->zp_copies < SPA_DVAS_PER_BP); 2660b24ab676SJeff Bonwick ASSERT(zp->zp_copies == BP_GET_NDVAS(bp) - BP_IS_GANG(bp)); 2661b24ab676SJeff Bonwick if (ddp->ddp_phys_birth != 0) 2662b24ab676SJeff Bonwick ddt_phys_free(ddt, ddk, ddp, zio->io_txg); 2663b24ab676SJeff Bonwick ddt_phys_fill(ddp, bp); 2664b24ab676SJeff Bonwick } 2665b24ab676SJeff Bonwick 2666b24ab676SJeff Bonwick ddt_exit(ddt); 2667b24ab676SJeff Bonwick } 2668b24ab676SJeff Bonwick 2669b24ab676SJeff Bonwick static int 2670b24ab676SJeff Bonwick zio_ddt_write(zio_t *zio) 2671b24ab676SJeff Bonwick { 2672b24ab676SJeff Bonwick spa_t *spa = zio->io_spa; 2673b24ab676SJeff Bonwick blkptr_t *bp = zio->io_bp; 2674b24ab676SJeff Bonwick uint64_t txg = zio->io_txg; 2675b24ab676SJeff Bonwick zio_prop_t *zp = &zio->io_prop; 2676b24ab676SJeff Bonwick int p = zp->zp_copies; 2677b24ab676SJeff Bonwick int ditto_copies; 2678b24ab676SJeff Bonwick zio_t *cio = NULL; 2679b24ab676SJeff Bonwick zio_t *dio = NULL; 2680b24ab676SJeff Bonwick ddt_t *ddt = ddt_select(spa, bp); 2681b24ab676SJeff Bonwick ddt_entry_t *dde; 2682b24ab676SJeff Bonwick ddt_phys_t *ddp; 2683b24ab676SJeff Bonwick 2684b24ab676SJeff Bonwick ASSERT(BP_GET_DEDUP(bp)); 2685b24ab676SJeff Bonwick ASSERT(BP_GET_CHECKSUM(bp) == zp->zp_checksum); 2686b24ab676SJeff Bonwick ASSERT(BP_IS_HOLE(bp) || zio->io_bp_override); 26875602294fSDan Kimmel ASSERT(!(zio->io_bp_override && (zio->io_flags & ZIO_FLAG_RAW))); 2688b24ab676SJeff Bonwick 2689b24ab676SJeff Bonwick ddt_enter(ddt); 2690b24ab676SJeff Bonwick dde = ddt_lookup(ddt, bp, B_TRUE); 2691b24ab676SJeff Bonwick ddp = &dde->dde_phys[p]; 2692b24ab676SJeff Bonwick 2693b24ab676SJeff Bonwick if (zp->zp_dedup_verify && zio_ddt_collision(zio, ddt, dde)) { 2694b24ab676SJeff Bonwick /* 2695b24ab676SJeff Bonwick * If we're using a weak checksum, upgrade to a strong checksum 2696b24ab676SJeff Bonwick * and try again. If we're already using a strong checksum, 2697b24ab676SJeff Bonwick * we can't resolve it, so just convert to an ordinary write. 2698b24ab676SJeff Bonwick * (And automatically e-mail a paper to Nature?) 2699b24ab676SJeff Bonwick */ 270045818ee1SMatthew Ahrens if (!(zio_checksum_table[zp->zp_checksum].ci_flags & 270145818ee1SMatthew Ahrens ZCHECKSUM_FLAG_DEDUP)) { 2702b24ab676SJeff Bonwick zp->zp_checksum = spa_dedup_checksum(spa); 2703b24ab676SJeff Bonwick zio_pop_transforms(zio); 2704b24ab676SJeff Bonwick zio->io_stage = ZIO_STAGE_OPEN; 2705b24ab676SJeff Bonwick BP_ZERO(bp); 2706b24ab676SJeff Bonwick } else { 270780901aeaSGeorge Wilson zp->zp_dedup = B_FALSE; 27085602294fSDan Kimmel BP_SET_DEDUP(bp, B_FALSE); 2709b24ab676SJeff Bonwick } 27105602294fSDan Kimmel ASSERT(!BP_GET_DEDUP(bp)); 2711b24ab676SJeff Bonwick zio->io_pipeline = ZIO_WRITE_PIPELINE; 2712b24ab676SJeff Bonwick ddt_exit(ddt); 2713b24ab676SJeff Bonwick return (ZIO_PIPELINE_CONTINUE); 2714b24ab676SJeff Bonwick } 2715b24ab676SJeff Bonwick 2716b24ab676SJeff Bonwick ditto_copies = ddt_ditto_copies_needed(ddt, dde, ddp); 2717b24ab676SJeff Bonwick ASSERT(ditto_copies < SPA_DVAS_PER_BP); 2718b24ab676SJeff Bonwick 2719b24ab676SJeff Bonwick if (ditto_copies > ddt_ditto_copies_present(dde) && 2720b24ab676SJeff Bonwick dde->dde_lead_zio[DDT_PHYS_DITTO] == NULL) { 2721b24ab676SJeff Bonwick zio_prop_t czp = *zp; 2722b24ab676SJeff Bonwick 2723b24ab676SJeff Bonwick czp.zp_copies = ditto_copies; 2724b24ab676SJeff Bonwick 2725b24ab676SJeff Bonwick /* 2726b24ab676SJeff Bonwick * If we arrived here with an override bp, we won't have run 2727b24ab676SJeff Bonwick * the transform stack, so we won't have the data we need to 2728b24ab676SJeff Bonwick * generate a child i/o. So, toss the override bp and restart. 2729b24ab676SJeff Bonwick * This is safe, because using the override bp is just an 2730b24ab676SJeff Bonwick * optimization; and it's rare, so the cost doesn't matter. 2731b24ab676SJeff Bonwick */ 2732b24ab676SJeff Bonwick if (zio->io_bp_override) { 2733b24ab676SJeff Bonwick zio_pop_transforms(zio); 2734b24ab676SJeff Bonwick zio->io_stage = ZIO_STAGE_OPEN; 2735b24ab676SJeff Bonwick zio->io_pipeline = ZIO_WRITE_PIPELINE; 2736b24ab676SJeff Bonwick zio->io_bp_override = NULL; 2737b24ab676SJeff Bonwick BP_ZERO(bp); 2738b24ab676SJeff Bonwick ddt_exit(ddt); 2739b24ab676SJeff Bonwick return (ZIO_PIPELINE_CONTINUE); 2740b24ab676SJeff Bonwick } 2741b24ab676SJeff Bonwick 2742770499e1SDan Kimmel dio = zio_write(zio, spa, txg, bp, zio->io_orig_abd, 27435602294fSDan Kimmel zio->io_orig_size, zio->io_orig_size, &czp, NULL, NULL, 27448df0bcf0SPaul Dagnelie NULL, zio_ddt_ditto_write_done, dde, zio->io_priority, 2745b24ab676SJeff Bonwick ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark); 2746b24ab676SJeff Bonwick 2747770499e1SDan Kimmel zio_push_transform(dio, zio->io_abd, zio->io_size, 0, NULL); 2748b24ab676SJeff Bonwick dde->dde_lead_zio[DDT_PHYS_DITTO] = dio; 2749b24ab676SJeff Bonwick } 2750b24ab676SJeff Bonwick 2751b24ab676SJeff Bonwick if (ddp->ddp_phys_birth != 0 || dde->dde_lead_zio[p] != NULL) { 2752b24ab676SJeff Bonwick if (ddp->ddp_phys_birth != 0) 2753b24ab676SJeff Bonwick ddt_bp_fill(ddp, bp, txg); 2754b24ab676SJeff Bonwick if (dde->dde_lead_zio[p] != NULL) 2755b24ab676SJeff Bonwick zio_add_child(zio, dde->dde_lead_zio[p]); 2756b24ab676SJeff Bonwick else 2757b24ab676SJeff Bonwick ddt_phys_addref(ddp); 2758b24ab676SJeff Bonwick } else if (zio->io_bp_override) { 2759b24ab676SJeff Bonwick ASSERT(bp->blk_birth == txg); 2760b24ab676SJeff Bonwick ASSERT(BP_EQUAL(bp, zio->io_bp_override)); 2761b24ab676SJeff Bonwick ddt_phys_fill(ddp, bp); 2762b24ab676SJeff Bonwick ddt_phys_addref(ddp); 2763b24ab676SJeff Bonwick } else { 2764770499e1SDan Kimmel cio = zio_write(zio, spa, txg, bp, zio->io_orig_abd, 27655602294fSDan Kimmel zio->io_orig_size, zio->io_orig_size, zp, 27668df0bcf0SPaul Dagnelie zio_ddt_child_write_ready, NULL, NULL, 2767b24ab676SJeff Bonwick zio_ddt_child_write_done, dde, zio->io_priority, 2768b24ab676SJeff Bonwick ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark); 2769b24ab676SJeff Bonwick 2770770499e1SDan Kimmel zio_push_transform(cio, zio->io_abd, zio->io_size, 0, NULL); 2771b24ab676SJeff Bonwick dde->dde_lead_zio[p] = cio; 2772b24ab676SJeff Bonwick } 2773b24ab676SJeff Bonwick 2774b24ab676SJeff Bonwick ddt_exit(ddt); 2775b24ab676SJeff Bonwick 2776b24ab676SJeff Bonwick if (cio) 2777b24ab676SJeff Bonwick zio_nowait(cio); 2778b24ab676SJeff Bonwick if (dio) 2779b24ab676SJeff Bonwick zio_nowait(dio); 2780b24ab676SJeff Bonwick 2781b24ab676SJeff Bonwick return (ZIO_PIPELINE_CONTINUE); 2782b24ab676SJeff Bonwick } 2783b24ab676SJeff Bonwick 27843f9d6ad7SLin Ling ddt_entry_t *freedde; /* for debugging */ 27853f9d6ad7SLin Ling 2786b24ab676SJeff Bonwick static int 2787b24ab676SJeff Bonwick zio_ddt_free(zio_t *zio) 2788b24ab676SJeff Bonwick { 2789b24ab676SJeff Bonwick spa_t *spa = zio->io_spa; 2790b24ab676SJeff Bonwick blkptr_t *bp = zio->io_bp; 2791b24ab676SJeff Bonwick ddt_t *ddt = ddt_select(spa, bp); 2792b24ab676SJeff Bonwick ddt_entry_t *dde; 2793b24ab676SJeff Bonwick ddt_phys_t *ddp; 2794b24ab676SJeff Bonwick 2795b24ab676SJeff Bonwick ASSERT(BP_GET_DEDUP(bp)); 2796b24ab676SJeff Bonwick ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 2797b24ab676SJeff Bonwick 2798b24ab676SJeff Bonwick ddt_enter(ddt); 27993f9d6ad7SLin Ling freedde = dde = ddt_lookup(ddt, bp, B_TRUE); 2800b24ab676SJeff Bonwick ddp = ddt_phys_select(dde, bp); 2801b24ab676SJeff Bonwick ddt_phys_decref(ddp); 2802b24ab676SJeff Bonwick ddt_exit(ddt); 2803b24ab676SJeff Bonwick 2804b24ab676SJeff Bonwick return (ZIO_PIPELINE_CONTINUE); 2805b24ab676SJeff Bonwick } 2806b24ab676SJeff Bonwick 2807b24ab676SJeff Bonwick /* 2808b24ab676SJeff Bonwick * ========================================================================== 2809b24ab676SJeff Bonwick * Allocate and free blocks 2810b24ab676SJeff Bonwick * ========================================================================== 2811b24ab676SJeff Bonwick */ 28120f7643c7SGeorge Wilson 28130f7643c7SGeorge Wilson static zio_t * 2814f78cdc34SPaul Dagnelie zio_io_to_allocate(spa_t *spa, int allocator) 28150f7643c7SGeorge Wilson { 28160f7643c7SGeorge Wilson zio_t *zio; 28170f7643c7SGeorge Wilson 2818f78cdc34SPaul Dagnelie ASSERT(MUTEX_HELD(&spa->spa_alloc_locks[allocator])); 28190f7643c7SGeorge Wilson 2820f78cdc34SPaul Dagnelie zio = avl_first(&spa->spa_alloc_trees[allocator]); 28210f7643c7SGeorge Wilson if (zio == NULL) 28220f7643c7SGeorge Wilson return (NULL); 28230f7643c7SGeorge Wilson 28240f7643c7SGeorge Wilson ASSERT(IO_IS_ALLOCATING(zio)); 28250f7643c7SGeorge Wilson 28260f7643c7SGeorge Wilson /* 28270f7643c7SGeorge Wilson * Try to place a reservation for this zio. If we're unable to 28280f7643c7SGeorge Wilson * reserve then we throttle. 28290f7643c7SGeorge Wilson */ 2830f78cdc34SPaul Dagnelie ASSERT3U(zio->io_allocator, ==, allocator); 2831*663207adSDon Brady if (!metaslab_class_throttle_reserve(zio->io_metaslab_class, 2832f78cdc34SPaul Dagnelie zio->io_prop.zp_copies, zio->io_allocator, zio, 0)) { 28330f7643c7SGeorge Wilson return (NULL); 28340f7643c7SGeorge Wilson } 28350f7643c7SGeorge Wilson 2836f78cdc34SPaul Dagnelie avl_remove(&spa->spa_alloc_trees[allocator], zio); 28370f7643c7SGeorge Wilson ASSERT3U(zio->io_stage, <, ZIO_STAGE_DVA_ALLOCATE); 28380f7643c7SGeorge Wilson 28390f7643c7SGeorge Wilson return (zio); 28400f7643c7SGeorge Wilson } 28410f7643c7SGeorge Wilson 28420f7643c7SGeorge Wilson static int 28430f7643c7SGeorge Wilson zio_dva_throttle(zio_t *zio) 28440f7643c7SGeorge Wilson { 28450f7643c7SGeorge Wilson spa_t *spa = zio->io_spa; 28460f7643c7SGeorge Wilson zio_t *nio; 2847*663207adSDon Brady metaslab_class_t *mc; 2848*663207adSDon Brady 2849*663207adSDon Brady /* locate an appropriate allocation class */ 2850*663207adSDon Brady mc = spa_preferred_class(spa, zio->io_size, zio->io_prop.zp_type, 2851*663207adSDon Brady zio->io_prop.zp_level, zio->io_prop.zp_zpl_smallblk); 28520f7643c7SGeorge Wilson 28530f7643c7SGeorge Wilson if (zio->io_priority == ZIO_PRIORITY_SYNC_WRITE || 2854*663207adSDon Brady !mc->mc_alloc_throttle_enabled || 28550f7643c7SGeorge Wilson zio->io_child_type == ZIO_CHILD_GANG || 28560f7643c7SGeorge Wilson zio->io_flags & ZIO_FLAG_NODATA) { 28570f7643c7SGeorge Wilson return (ZIO_PIPELINE_CONTINUE); 28580f7643c7SGeorge Wilson } 28590f7643c7SGeorge Wilson 28600f7643c7SGeorge Wilson ASSERT(zio->io_child_type > ZIO_CHILD_GANG); 28610f7643c7SGeorge Wilson 28620f7643c7SGeorge Wilson ASSERT3U(zio->io_queued_timestamp, >, 0); 28630f7643c7SGeorge Wilson ASSERT(zio->io_stage == ZIO_STAGE_DVA_THROTTLE); 28640f7643c7SGeorge Wilson 2865f78cdc34SPaul Dagnelie zbookmark_phys_t *bm = &zio->io_bookmark; 2866f78cdc34SPaul Dagnelie /* 2867f78cdc34SPaul Dagnelie * We want to try to use as many allocators as possible to help improve 2868f78cdc34SPaul Dagnelie * performance, but we also want logically adjacent IOs to be physically 2869f78cdc34SPaul Dagnelie * adjacent to improve sequential read performance. We chunk each object 2870f78cdc34SPaul Dagnelie * into 2^20 block regions, and then hash based on the objset, object, 2871f78cdc34SPaul Dagnelie * level, and region to accomplish both of these goals. 2872f78cdc34SPaul Dagnelie */ 2873f78cdc34SPaul Dagnelie zio->io_allocator = cityhash4(bm->zb_objset, bm->zb_object, 2874f78cdc34SPaul Dagnelie bm->zb_level, bm->zb_blkid >> 20) % spa->spa_alloc_count; 2875f78cdc34SPaul Dagnelie mutex_enter(&spa->spa_alloc_locks[zio->io_allocator]); 28760f7643c7SGeorge Wilson ASSERT(zio->io_type == ZIO_TYPE_WRITE); 2877*663207adSDon Brady zio->io_metaslab_class = mc; 2878f78cdc34SPaul Dagnelie avl_add(&spa->spa_alloc_trees[zio->io_allocator], zio); 2879*663207adSDon Brady nio = zio_io_to_allocate(spa, zio->io_allocator); 2880f78cdc34SPaul Dagnelie mutex_exit(&spa->spa_alloc_locks[zio->io_allocator]); 28810f7643c7SGeorge Wilson 28820f7643c7SGeorge Wilson if (nio == zio) 28830f7643c7SGeorge Wilson return (ZIO_PIPELINE_CONTINUE); 28840f7643c7SGeorge Wilson 28850f7643c7SGeorge Wilson if (nio != NULL) { 28860f7643c7SGeorge Wilson ASSERT(nio->io_stage == ZIO_STAGE_DVA_THROTTLE); 28870f7643c7SGeorge Wilson /* 28880f7643c7SGeorge Wilson * We are passing control to a new zio so make sure that 28890f7643c7SGeorge Wilson * it is processed by a different thread. We do this to 28900f7643c7SGeorge Wilson * avoid stack overflows that can occur when parents are 28910f7643c7SGeorge Wilson * throttled and children are making progress. We allow 28920f7643c7SGeorge Wilson * it to go to the head of the taskq since it's already 28930f7643c7SGeorge Wilson * been waiting. 28940f7643c7SGeorge Wilson */ 28950f7643c7SGeorge Wilson zio_taskq_dispatch(nio, ZIO_TASKQ_ISSUE, B_TRUE); 28960f7643c7SGeorge Wilson } 28970f7643c7SGeorge Wilson return (ZIO_PIPELINE_STOP); 28980f7643c7SGeorge Wilson } 28990f7643c7SGeorge Wilson 2900*663207adSDon Brady static void 2901f78cdc34SPaul Dagnelie zio_allocate_dispatch(spa_t *spa, int allocator) 29020f7643c7SGeorge Wilson { 29030f7643c7SGeorge Wilson zio_t *zio; 29040f7643c7SGeorge Wilson 2905f78cdc34SPaul Dagnelie mutex_enter(&spa->spa_alloc_locks[allocator]); 2906f78cdc34SPaul Dagnelie zio = zio_io_to_allocate(spa, allocator); 2907f78cdc34SPaul Dagnelie mutex_exit(&spa->spa_alloc_locks[allocator]); 29080f7643c7SGeorge Wilson if (zio == NULL) 29090f7643c7SGeorge Wilson return; 29100f7643c7SGeorge Wilson 29110f7643c7SGeorge Wilson ASSERT3U(zio->io_stage, ==, ZIO_STAGE_DVA_THROTTLE); 29120f7643c7SGeorge Wilson ASSERT0(zio->io_error); 29130f7643c7SGeorge Wilson zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_TRUE); 29140f7643c7SGeorge Wilson } 29150f7643c7SGeorge Wilson 2916e05725b1Sbonwick static int 2917fa9e4066Sahrens zio_dva_allocate(zio_t *zio) 2918fa9e4066Sahrens { 29198654d025Sperrin spa_t *spa = zio->io_spa; 2920*663207adSDon Brady metaslab_class_t *mc; 2921fa9e4066Sahrens blkptr_t *bp = zio->io_bp; 2922fa9e4066Sahrens int error; 292309c9d376SGeorge Wilson int flags = 0; 2924fa9e4066Sahrens 2925f5383399SBill Moore if (zio->io_gang_leader == NULL) { 2926f5383399SBill Moore ASSERT(zio->io_child_type > ZIO_CHILD_GANG); 2927f5383399SBill Moore zio->io_gang_leader = zio; 2928f5383399SBill Moore } 2929f5383399SBill Moore 2930fa9e4066Sahrens ASSERT(BP_IS_HOLE(bp)); 2931fb09f5aaSMadhav Suresh ASSERT0(BP_GET_NDVAS(bp)); 2932b24ab676SJeff Bonwick ASSERT3U(zio->io_prop.zp_copies, >, 0); 2933b24ab676SJeff Bonwick ASSERT3U(zio->io_prop.zp_copies, <=, spa_max_replication(spa)); 2934fa9e4066Sahrens ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp)); 2935fa9e4066Sahrens 2936*663207adSDon Brady if (zio->io_flags & ZIO_FLAG_NODATA) 29370f7643c7SGeorge Wilson flags |= METASLAB_DONT_THROTTLE; 2938*663207adSDon Brady if (zio->io_flags & ZIO_FLAG_GANG_CHILD) 29390f7643c7SGeorge Wilson flags |= METASLAB_GANG_CHILD; 2940*663207adSDon Brady if (zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE) 29410f7643c7SGeorge Wilson flags |= METASLAB_ASYNC_ALLOC; 2942*663207adSDon Brady 2943*663207adSDon Brady /* 2944*663207adSDon Brady * if not already chosen, locate an appropriate allocation class 2945*663207adSDon Brady */ 2946*663207adSDon Brady mc = zio->io_metaslab_class; 2947*663207adSDon Brady if (mc == NULL) { 2948*663207adSDon Brady mc = spa_preferred_class(spa, zio->io_size, 2949*663207adSDon Brady zio->io_prop.zp_type, zio->io_prop.zp_level, 2950*663207adSDon Brady zio->io_prop.zp_zpl_smallblk); 2951*663207adSDon Brady zio->io_metaslab_class = mc; 29520f7643c7SGeorge Wilson } 29530f7643c7SGeorge Wilson 2954e14bb325SJeff Bonwick error = metaslab_alloc(spa, mc, zio->io_size, bp, 29558363e80aSGeorge Wilson zio->io_prop.zp_copies, zio->io_txg, NULL, flags, 2956f78cdc34SPaul Dagnelie &zio->io_alloc_list, zio, zio->io_allocator); 2957fa9e4066Sahrens 2958*663207adSDon Brady /* 2959*663207adSDon Brady * Fallback to normal class when an alloc class is full 2960*663207adSDon Brady */ 2961*663207adSDon Brady if (error == ENOSPC && mc != spa_normal_class(spa)) { 2962*663207adSDon Brady /* 2963*663207adSDon Brady * If throttling, transfer reservation over to normal class. 2964*663207adSDon Brady * The io_allocator slot can remain the same even though we 2965*663207adSDon Brady * are switching classes. 2966*663207adSDon Brady */ 2967*663207adSDon Brady if (mc->mc_alloc_throttle_enabled && 2968*663207adSDon Brady (zio->io_flags & ZIO_FLAG_IO_ALLOCATING)) { 2969*663207adSDon Brady metaslab_class_throttle_unreserve(mc, 2970*663207adSDon Brady zio->io_prop.zp_copies, zio->io_allocator, zio); 2971*663207adSDon Brady zio->io_flags &= ~ZIO_FLAG_IO_ALLOCATING; 2972*663207adSDon Brady 2973*663207adSDon Brady mc = spa_normal_class(spa); 2974*663207adSDon Brady VERIFY(metaslab_class_throttle_reserve(mc, 2975*663207adSDon Brady zio->io_prop.zp_copies, zio->io_allocator, zio, 2976*663207adSDon Brady flags | METASLAB_MUST_RESERVE)); 2977*663207adSDon Brady } else { 2978*663207adSDon Brady mc = spa_normal_class(spa); 2979*663207adSDon Brady } 2980*663207adSDon Brady zio->io_metaslab_class = mc; 2981*663207adSDon Brady 2982*663207adSDon Brady error = metaslab_alloc(spa, mc, zio->io_size, bp, 2983*663207adSDon Brady zio->io_prop.zp_copies, zio->io_txg, NULL, flags, 2984*663207adSDon Brady &zio->io_alloc_list, zio, zio->io_allocator); 2985*663207adSDon Brady } 2986*663207adSDon Brady 29870f7643c7SGeorge Wilson if (error != 0) { 298821f7c81cSMatthew Ahrens zfs_dbgmsg("%s: metaslab allocation failure: zio %p, " 298909c9d376SGeorge Wilson "size %llu, error %d", spa_name(spa), zio, zio->io_size, 299009c9d376SGeorge Wilson error); 2991e14bb325SJeff Bonwick if (error == ENOSPC && zio->io_size > SPA_MINBLOCKSIZE) 2992e14bb325SJeff Bonwick return (zio_write_gang_block(zio)); 2993fa9e4066Sahrens zio->io_error = error; 2994fa9e4066Sahrens } 2995e05725b1Sbonwick 2996e05725b1Sbonwick return (ZIO_PIPELINE_CONTINUE); 2997fa9e4066Sahrens } 2998fa9e4066Sahrens 2999e05725b1Sbonwick static int 3000fa9e4066Sahrens zio_dva_free(zio_t *zio) 3001fa9e4066Sahrens { 3002e14bb325SJeff Bonwick metaslab_free(zio->io_spa, zio->io_bp, zio->io_txg, B_FALSE); 3003fa9e4066Sahrens 3004e05725b1Sbonwick return (ZIO_PIPELINE_CONTINUE); 3005fa9e4066Sahrens } 3006fa9e4066Sahrens 3007e05725b1Sbonwick static int 3008fa9e4066Sahrens zio_dva_claim(zio_t *zio) 3009fa9e4066Sahrens { 3010e14bb325SJeff Bonwick int error; 3011e14bb325SJeff Bonwick 3012e14bb325SJeff Bonwick error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg); 3013e14bb325SJeff Bonwick if (error) 3014e14bb325SJeff Bonwick zio->io_error = error; 3015fa9e4066Sahrens 3016e05725b1Sbonwick return (ZIO_PIPELINE_CONTINUE); 3017fa9e4066Sahrens } 3018fa9e4066Sahrens 3019e14bb325SJeff Bonwick /* 3020e14bb325SJeff Bonwick * Undo an allocation. This is used by zio_done() when an I/O fails 3021e14bb325SJeff Bonwick * and we want to give back the block we just allocated. 3022e14bb325SJeff Bonwick * This handles both normal blocks and gang blocks. 3023e14bb325SJeff Bonwick */ 3024e14bb325SJeff Bonwick static void 3025e14bb325SJeff Bonwick zio_dva_unallocate(zio_t *zio, zio_gang_node_t *gn, blkptr_t *bp) 3026e14bb325SJeff Bonwick { 3027e14bb325SJeff Bonwick ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp)); 3028b24ab676SJeff Bonwick ASSERT(zio->io_bp_override == NULL); 3029e14bb325SJeff Bonwick 3030e14bb325SJeff Bonwick if (!BP_IS_HOLE(bp)) 3031b24ab676SJeff Bonwick metaslab_free(zio->io_spa, bp, bp->blk_birth, B_TRUE); 3032e14bb325SJeff Bonwick 3033e14bb325SJeff Bonwick if (gn != NULL) { 3034e14bb325SJeff Bonwick for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) { 3035e14bb325SJeff Bonwick zio_dva_unallocate(zio, gn->gn_child[g], 3036e14bb325SJeff Bonwick &gn->gn_gbh->zg_blkptr[g]); 3037e14bb325SJeff Bonwick } 3038e14bb325SJeff Bonwick } 3039e14bb325SJeff Bonwick } 3040e14bb325SJeff Bonwick 3041e14bb325SJeff Bonwick /* 3042e14bb325SJeff Bonwick * Try to allocate an intent log block. Return 0 on success, errno on failure. 3043e14bb325SJeff Bonwick */ 3044e14bb325SJeff Bonwick int 3045f78cdc34SPaul Dagnelie zio_alloc_zil(spa_t *spa, uint64_t objset, uint64_t txg, blkptr_t *new_bp, 3046f78cdc34SPaul Dagnelie blkptr_t *old_bp, uint64_t size, boolean_t *slog) 3047e14bb325SJeff Bonwick { 3048e09fa4daSNeil Perrin int error = 1; 30498363e80aSGeorge Wilson zio_alloc_list_t io_alloc_list; 3050e14bb325SJeff Bonwick 3051b24ab676SJeff Bonwick ASSERT(txg > spa_syncing_txg(spa)); 3052b24ab676SJeff Bonwick 30538363e80aSGeorge Wilson metaslab_trace_init(&io_alloc_list); 3054*663207adSDon Brady 3055*663207adSDon Brady /* 3056*663207adSDon Brady * Block pointer fields are useful to metaslabs for stats and debugging. 3057*663207adSDon Brady * Fill in the obvious ones before calling into metaslab_alloc(). 3058*663207adSDon Brady */ 3059*663207adSDon Brady BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG); 3060*663207adSDon Brady BP_SET_PSIZE(new_bp, size); 3061*663207adSDon Brady BP_SET_LEVEL(new_bp, 0); 3062*663207adSDon Brady 3063f78cdc34SPaul Dagnelie /* 3064f78cdc34SPaul Dagnelie * When allocating a zil block, we don't have information about 3065f78cdc34SPaul Dagnelie * the final destination of the block except the objset it's part 3066f78cdc34SPaul Dagnelie * of, so we just hash the objset ID to pick the allocator to get 3067f78cdc34SPaul Dagnelie * some parallelism. 3068f78cdc34SPaul Dagnelie */ 3069c5ee4681SAlexander Motin error = metaslab_alloc(spa, spa_log_class(spa), size, new_bp, 1, 3070f78cdc34SPaul Dagnelie txg, old_bp, METASLAB_HINTBP_AVOID, &io_alloc_list, NULL, 3071f78cdc34SPaul Dagnelie cityhash4(0, 0, 0, objset) % spa->spa_alloc_count); 3072c5ee4681SAlexander Motin if (error == 0) { 3073c5ee4681SAlexander Motin *slog = TRUE; 3074c5ee4681SAlexander Motin } else { 3075b24ab676SJeff Bonwick error = metaslab_alloc(spa, spa_normal_class(spa), size, 30768363e80aSGeorge Wilson new_bp, 1, txg, old_bp, METASLAB_HINTBP_AVOID, 3077f78cdc34SPaul Dagnelie &io_alloc_list, NULL, cityhash4(0, 0, 0, objset) % 3078f78cdc34SPaul Dagnelie spa->spa_alloc_count); 3079c5ee4681SAlexander Motin if (error == 0) 3080c5ee4681SAlexander Motin *slog = FALSE; 3081840345f6SGeorge Wilson } 30828363e80aSGeorge Wilson metaslab_trace_fini(&io_alloc_list); 3083e14bb325SJeff Bonwick 3084e14bb325SJeff Bonwick if (error == 0) { 3085e14bb325SJeff Bonwick BP_SET_LSIZE(new_bp, size); 3086e14bb325SJeff Bonwick BP_SET_PSIZE(new_bp, size); 3087e14bb325SJeff Bonwick BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF); 30886e1f5caaSNeil Perrin BP_SET_CHECKSUM(new_bp, 30896e1f5caaSNeil Perrin spa_version(spa) >= SPA_VERSION_SLIM_ZIL 30906e1f5caaSNeil Perrin ? ZIO_CHECKSUM_ZILOG2 : ZIO_CHECKSUM_ZILOG); 3091e14bb325SJeff Bonwick BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG); 3092e14bb325SJeff Bonwick BP_SET_LEVEL(new_bp, 0); 3093b24ab676SJeff Bonwick BP_SET_DEDUP(new_bp, 0); 3094e14bb325SJeff Bonwick BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER); 30951271e4b1SPrakash Surya } else { 30961271e4b1SPrakash Surya zfs_dbgmsg("%s: zil block allocation failure: " 30971271e4b1SPrakash Surya "size %llu, error %d", spa_name(spa), size, error); 3098e14bb325SJeff Bonwick } 3099e14bb325SJeff Bonwick 3100e14bb325SJeff Bonwick return (error); 3101e14bb325SJeff Bonwick } 3102e14bb325SJeff Bonwick 3103fa9e4066Sahrens /* 3104fa9e4066Sahrens * ========================================================================== 3105fa9e4066Sahrens * Read and write to physical devices 3106fa9e4066Sahrens * ========================================================================== 3107fa9e4066Sahrens */ 3108738f37bcSGeorge Wilson 3109738f37bcSGeorge Wilson 3110738f37bcSGeorge Wilson /* 3111738f37bcSGeorge Wilson * Issue an I/O to the underlying vdev. Typically the issue pipeline 3112738f37bcSGeorge Wilson * stops after this stage and will resume upon I/O completion. 3113738f37bcSGeorge Wilson * However, there are instances where the vdev layer may need to 3114738f37bcSGeorge Wilson * continue the pipeline when an I/O was not issued. Since the I/O 3115738f37bcSGeorge Wilson * that was sent to the vdev layer might be different than the one 3116738f37bcSGeorge Wilson * currently active in the pipeline (see vdev_queue_io()), we explicitly 3117738f37bcSGeorge Wilson * force the underlying vdev layers to call either zio_execute() or 3118738f37bcSGeorge Wilson * zio_interrupt() to ensure that the pipeline continues with the correct I/O. 3119738f37bcSGeorge Wilson */ 3120e05725b1Sbonwick static int 312144cd46caSbillm zio_vdev_io_start(zio_t *zio) 3122fa9e4066Sahrens { 3123fa9e4066Sahrens vdev_t *vd = zio->io_vd; 312444cd46caSbillm uint64_t align; 31250a4e9518Sgw spa_t *spa = zio->io_spa; 31260a4e9518Sgw 3127e14bb325SJeff Bonwick ASSERT(zio->io_error == 0); 3128e14bb325SJeff Bonwick ASSERT(zio->io_child_error[ZIO_CHILD_VDEV] == 0); 3129fa9e4066Sahrens 3130e14bb325SJeff Bonwick if (vd == NULL) { 3131e14bb325SJeff Bonwick if (!(zio->io_flags & ZIO_FLAG_CONFIG_WRITER)) 3132e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ZIO, zio, RW_READER); 3133fa9e4066Sahrens 3134e14bb325SJeff Bonwick /* 3135e14bb325SJeff Bonwick * The mirror_ops handle multiple DVAs in a single BP. 3136e14bb325SJeff Bonwick */ 3137738f37bcSGeorge Wilson vdev_mirror_ops.vdev_op_io_start(zio); 3138738f37bcSGeorge Wilson return (ZIO_PIPELINE_STOP); 3139fa9e4066Sahrens } 3140fa9e4066Sahrens 31410f7643c7SGeorge Wilson ASSERT3P(zio->io_logical, !=, zio); 31426f793812SPavel Zakharov if (zio->io_type == ZIO_TYPE_WRITE) { 31436f793812SPavel Zakharov ASSERT(spa->spa_trust_config); 31446f793812SPavel Zakharov 31456f793812SPavel Zakharov if (zio->io_vd->vdev_removing) { 31463a4b1be9SMatthew Ahrens /* 31473a4b1be9SMatthew Ahrens * Note: the code can handle other kinds of writes, 31483a4b1be9SMatthew Ahrens * but we don't expect them. 31493a4b1be9SMatthew Ahrens */ 31506f793812SPavel Zakharov ASSERT(zio->io_flags & 31516f793812SPavel Zakharov (ZIO_FLAG_PHYSICAL | ZIO_FLAG_SELF_HEAL | 31523a4b1be9SMatthew Ahrens ZIO_FLAG_RESILVER | ZIO_FLAG_INDUCE_DAMAGE)); 31536f793812SPavel Zakharov } 31545cabbc6bSPrashanth Sreenivasa } 31550f7643c7SGeorge Wilson 315644ecc532SGeorge Wilson /* 315744ecc532SGeorge Wilson * We keep track of time-sensitive I/Os so that the scan thread 315844ecc532SGeorge Wilson * can quickly react to certain workloads. In particular, we care 315944ecc532SGeorge Wilson * about non-scrubbing, top-level reads and writes with the following 316044ecc532SGeorge Wilson * characteristics: 3161738f37bcSGeorge Wilson * - synchronous writes of user data to non-slog devices 316244ecc532SGeorge Wilson * - any reads of user data 316344ecc532SGeorge Wilson * When these conditions are met, adjust the timestamp of spa_last_io 316444ecc532SGeorge Wilson * which allows the scan thread to adjust its workload accordingly. 316544ecc532SGeorge Wilson */ 316644ecc532SGeorge Wilson if (!(zio->io_flags & ZIO_FLAG_SCAN_THREAD) && zio->io_bp != NULL && 316744ecc532SGeorge Wilson vd == vd->vdev_top && !vd->vdev_islog && 316844ecc532SGeorge Wilson zio->io_bookmark.zb_objset != DMU_META_OBJSET && 316944ecc532SGeorge Wilson zio->io_txg != spa_syncing_txg(spa)) { 317044ecc532SGeorge Wilson uint64_t old = spa->spa_last_io; 317144ecc532SGeorge Wilson uint64_t new = ddi_get_lbolt64(); 317244ecc532SGeorge Wilson if (old != new) 317344ecc532SGeorge Wilson (void) atomic_cas_64(&spa->spa_last_io, old, new); 317444ecc532SGeorge Wilson } 317544ecc532SGeorge Wilson 3176e14bb325SJeff Bonwick align = 1ULL << vd->vdev_top->vdev_ashift; 3177e14bb325SJeff Bonwick 31782a104a52SAlex Reece if (!(zio->io_flags & ZIO_FLAG_PHYSICAL) && 31792a104a52SAlex Reece P2PHASE(zio->io_size, align) != 0) { 31802a104a52SAlex Reece /* Transform logical writes to be a full physical block size. */ 3181ecc2d604Sbonwick uint64_t asize = P2ROUNDUP(zio->io_size, align); 3182770499e1SDan Kimmel abd_t *abuf = abd_alloc_sametype(zio->io_abd, asize); 3183e14bb325SJeff Bonwick ASSERT(vd == vd->vdev_top); 3184ecc2d604Sbonwick if (zio->io_type == ZIO_TYPE_WRITE) { 3185770499e1SDan Kimmel abd_copy(abuf, zio->io_abd, zio->io_size); 3186770499e1SDan Kimmel abd_zero_off(abuf, zio->io_size, asize - zio->io_size); 3187ecc2d604Sbonwick } 3188e14bb325SJeff Bonwick zio_push_transform(zio, abuf, asize, asize, zio_subblock); 3189ecc2d604Sbonwick } 3190ecc2d604Sbonwick 31912a104a52SAlex Reece /* 31922a104a52SAlex Reece * If this is not a physical io, make sure that it is properly aligned 31932a104a52SAlex Reece * before proceeding. 31942a104a52SAlex Reece */ 31952a104a52SAlex Reece if (!(zio->io_flags & ZIO_FLAG_PHYSICAL)) { 31962a104a52SAlex Reece ASSERT0(P2PHASE(zio->io_offset, align)); 31972a104a52SAlex Reece ASSERT0(P2PHASE(zio->io_size, align)); 31982a104a52SAlex Reece } else { 31992a104a52SAlex Reece /* 32002a104a52SAlex Reece * For physical writes, we allow 512b aligned writes and assume 32012a104a52SAlex Reece * the device will perform a read-modify-write as necessary. 32022a104a52SAlex Reece */ 32032a104a52SAlex Reece ASSERT0(P2PHASE(zio->io_offset, SPA_MINBLOCKSIZE)); 32042a104a52SAlex Reece ASSERT0(P2PHASE(zio->io_size, SPA_MINBLOCKSIZE)); 32052a104a52SAlex Reece } 32062a104a52SAlex Reece 3207f9af39baSGeorge Wilson VERIFY(zio->io_type != ZIO_TYPE_WRITE || spa_writeable(spa)); 32088ad4d6ddSJeff Bonwick 32098ad4d6ddSJeff Bonwick /* 32108ad4d6ddSJeff Bonwick * If this is a repair I/O, and there's no self-healing involved -- 32118ad4d6ddSJeff Bonwick * that is, we're just resilvering what we expect to resilver -- 32128ad4d6ddSJeff Bonwick * then don't do the I/O unless zio's txg is actually in vd's DTL. 32133a4b1be9SMatthew Ahrens * This prevents spurious resilvering. 32143a4b1be9SMatthew Ahrens * 32153a4b1be9SMatthew Ahrens * There are a few ways that we can end up creating these spurious 32163a4b1be9SMatthew Ahrens * resilver i/os: 32173a4b1be9SMatthew Ahrens * 32183a4b1be9SMatthew Ahrens * 1. A resilver i/o will be issued if any DVA in the BP has a 32193a4b1be9SMatthew Ahrens * dirty DTL. The mirror code will issue resilver writes to 32203a4b1be9SMatthew Ahrens * each DVA, including the one(s) that are not on vdevs with dirty 32213a4b1be9SMatthew Ahrens * DTLs. 32223a4b1be9SMatthew Ahrens * 32233a4b1be9SMatthew Ahrens * 2. With nested replication, which happens when we have a 32243a4b1be9SMatthew Ahrens * "replacing" or "spare" vdev that's a child of a mirror or raidz. 32253a4b1be9SMatthew Ahrens * For example, given mirror(replacing(A+B), C), it's likely that 32263a4b1be9SMatthew Ahrens * only A is out of date (it's the new device). In this case, we'll 32273a4b1be9SMatthew Ahrens * read from C, then use the data to resilver A+B -- but we don't 32283a4b1be9SMatthew Ahrens * actually want to resilver B, just A. The top-level mirror has no 32293a4b1be9SMatthew Ahrens * way to know this, so instead we just discard unnecessary repairs 32303a4b1be9SMatthew Ahrens * as we work our way down the vdev tree. 32313a4b1be9SMatthew Ahrens * 32323a4b1be9SMatthew Ahrens * 3. ZTEST also creates mirrors of mirrors, mirrors of raidz, etc. 32333a4b1be9SMatthew Ahrens * The same logic applies to any form of nested replication: ditto 32343a4b1be9SMatthew Ahrens * + mirror, RAID-Z + replacing, etc. 32353a4b1be9SMatthew Ahrens * 32363a4b1be9SMatthew Ahrens * However, indirect vdevs point off to other vdevs which may have 32373a4b1be9SMatthew Ahrens * DTL's, so we never bypass them. The child i/os on concrete vdevs 32383a4b1be9SMatthew Ahrens * will be properly bypassed instead. 32398ad4d6ddSJeff Bonwick */ 32408ad4d6ddSJeff Bonwick if ((zio->io_flags & ZIO_FLAG_IO_REPAIR) && 32418ad4d6ddSJeff Bonwick !(zio->io_flags & ZIO_FLAG_SELF_HEAL) && 32428ad4d6ddSJeff Bonwick zio->io_txg != 0 && /* not a delegated i/o */ 32433a4b1be9SMatthew Ahrens vd->vdev_ops != &vdev_indirect_ops && 32448ad4d6ddSJeff Bonwick !vdev_dtl_contains(vd, DTL_PARTIAL, zio->io_txg, 1)) { 32458ad4d6ddSJeff Bonwick ASSERT(zio->io_type == ZIO_TYPE_WRITE); 32468ad4d6ddSJeff Bonwick zio_vdev_io_bypass(zio); 32478ad4d6ddSJeff Bonwick return (ZIO_PIPELINE_CONTINUE); 32488ad4d6ddSJeff Bonwick } 3249fa9e4066Sahrens 3250e14bb325SJeff Bonwick if (vd->vdev_ops->vdev_op_leaf && 3251e14bb325SJeff Bonwick (zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE)) { 3252e14bb325SJeff Bonwick 325343466aaeSMax Grossman if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio)) 3254a3f829aeSBill Moore return (ZIO_PIPELINE_CONTINUE); 3255e14bb325SJeff Bonwick 3256e14bb325SJeff Bonwick if ((zio = vdev_queue_io(zio)) == NULL) 3257e14bb325SJeff Bonwick return (ZIO_PIPELINE_STOP); 3258e14bb325SJeff Bonwick 3259e14bb325SJeff Bonwick if (!vdev_accessible(vd, zio)) { 3260be6fd75aSMatthew Ahrens zio->io_error = SET_ERROR(ENXIO); 3261e14bb325SJeff Bonwick zio_interrupt(zio); 3262e14bb325SJeff Bonwick return (ZIO_PIPELINE_STOP); 3263e14bb325SJeff Bonwick } 3264e14bb325SJeff Bonwick } 3265e14bb325SJeff Bonwick 3266738f37bcSGeorge Wilson vd->vdev_ops->vdev_op_io_start(zio); 3267738f37bcSGeorge Wilson return (ZIO_PIPELINE_STOP); 3268fa9e4066Sahrens } 3269fa9e4066Sahrens 3270e05725b1Sbonwick static int 3271fa9e4066Sahrens zio_vdev_io_done(zio_t *zio) 3272fa9e4066Sahrens { 3273e14bb325SJeff Bonwick vdev_t *vd = zio->io_vd; 3274e14bb325SJeff Bonwick vdev_ops_t *ops = vd ? vd->vdev_ops : &vdev_mirror_ops; 3275e14bb325SJeff Bonwick boolean_t unexpected_error = B_FALSE; 3276e05725b1Sbonwick 3277d6e1c446SGeorge Wilson if (zio_wait_for_children(zio, ZIO_CHILD_VDEV_BIT, ZIO_WAIT_DONE)) { 3278e14bb325SJeff Bonwick return (ZIO_PIPELINE_STOP); 3279d6e1c446SGeorge Wilson } 3280fa9e4066Sahrens 3281e14bb325SJeff Bonwick ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE); 3282e14bb325SJeff Bonwick 3283e14bb325SJeff Bonwick if (vd != NULL && vd->vdev_ops->vdev_op_leaf) { 3284e14bb325SJeff Bonwick 3285e14bb325SJeff Bonwick vdev_queue_io_done(zio); 3286fa9e4066Sahrens 3287e14bb325SJeff Bonwick if (zio->io_type == ZIO_TYPE_WRITE) 3288e14bb325SJeff Bonwick vdev_cache_write(zio); 3289e14bb325SJeff Bonwick 3290e14bb325SJeff Bonwick if (zio_injection_enabled && zio->io_error == 0) 32918956713aSEric Schrock zio->io_error = zio_handle_device_injection(vd, 32928956713aSEric Schrock zio, EIO); 3293e14bb325SJeff Bonwick 3294e14bb325SJeff Bonwick if (zio_injection_enabled && zio->io_error == 0) 3295e14bb325SJeff Bonwick zio->io_error = zio_handle_label_injection(zio, EIO); 3296e14bb325SJeff Bonwick 3297e14bb325SJeff Bonwick if (zio->io_error) { 3298e14bb325SJeff Bonwick if (!vdev_accessible(vd, zio)) { 3299be6fd75aSMatthew Ahrens zio->io_error = SET_ERROR(ENXIO); 3300e14bb325SJeff Bonwick } else { 3301e14bb325SJeff Bonwick unexpected_error = B_TRUE; 3302e14bb325SJeff Bonwick } 3303e14bb325SJeff Bonwick } 330451ece835Seschrock } 3305fa9e4066Sahrens 3306e14bb325SJeff Bonwick ops->vdev_op_io_done(zio); 3307e14bb325SJeff Bonwick 3308e14bb325SJeff Bonwick if (unexpected_error) 3309a3f829aeSBill Moore VERIFY(vdev_probe(vd, zio) == NULL); 3310e14bb325SJeff Bonwick 3311e14bb325SJeff Bonwick return (ZIO_PIPELINE_CONTINUE); 3312fa9e4066Sahrens } 3313fa9e4066Sahrens 331422fe2c88SJonathan Adams /* 331522fe2c88SJonathan Adams * For non-raidz ZIOs, we can just copy aside the bad data read from the 331622fe2c88SJonathan Adams * disk, and use that to finish the checksum ereport later. 331722fe2c88SJonathan Adams */ 331822fe2c88SJonathan Adams static void 331922fe2c88SJonathan Adams zio_vsd_default_cksum_finish(zio_cksum_report_t *zcr, 332022fe2c88SJonathan Adams const void *good_buf) 332122fe2c88SJonathan Adams { 332222fe2c88SJonathan Adams /* no processing needed */ 332322fe2c88SJonathan Adams zfs_ereport_finish_checksum(zcr, good_buf, zcr->zcr_cbdata, B_FALSE); 332422fe2c88SJonathan Adams } 332522fe2c88SJonathan Adams 332622fe2c88SJonathan Adams /*ARGSUSED*/ 332722fe2c88SJonathan Adams void 332822fe2c88SJonathan Adams zio_vsd_default_cksum_report(zio_t *zio, zio_cksum_report_t *zcr, void *ignored) 332922fe2c88SJonathan Adams { 333022fe2c88SJonathan Adams void *buf = zio_buf_alloc(zio->io_size); 333122fe2c88SJonathan Adams 3332770499e1SDan Kimmel abd_copy_to_buf(buf, zio->io_abd, zio->io_size); 333322fe2c88SJonathan Adams 333422fe2c88SJonathan Adams zcr->zcr_cbinfo = zio->io_size; 333522fe2c88SJonathan Adams zcr->zcr_cbdata = buf; 333622fe2c88SJonathan Adams zcr->zcr_finish = zio_vsd_default_cksum_finish; 333722fe2c88SJonathan Adams zcr->zcr_free = zio_buf_free; 333822fe2c88SJonathan Adams } 333922fe2c88SJonathan Adams 3340e05725b1Sbonwick static int 3341fa9e4066Sahrens zio_vdev_io_assess(zio_t *zio) 3342fa9e4066Sahrens { 3343fa9e4066Sahrens vdev_t *vd = zio->io_vd; 3344e14bb325SJeff Bonwick 3345d6e1c446SGeorge Wilson if (zio_wait_for_children(zio, ZIO_CHILD_VDEV_BIT, ZIO_WAIT_DONE)) { 3346e14bb325SJeff Bonwick return (ZIO_PIPELINE_STOP); 3347d6e1c446SGeorge Wilson } 3348e14bb325SJeff Bonwick 3349e14bb325SJeff Bonwick if (vd == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_WRITER)) 3350e14bb325SJeff Bonwick spa_config_exit(zio->io_spa, SCL_ZIO, zio); 3351e14bb325SJeff Bonwick 3352e14bb325SJeff Bonwick if (zio->io_vsd != NULL) { 335322fe2c88SJonathan Adams zio->io_vsd_ops->vsd_free(zio); 3354e14bb325SJeff Bonwick zio->io_vsd = NULL; 3355ecc2d604Sbonwick } 3356ecc2d604Sbonwick 3357e14bb325SJeff Bonwick if (zio_injection_enabled && zio->io_error == 0) 3358ea8dc4b6Seschrock zio->io_error = zio_handle_fault_injection(zio, EIO); 3359ea8dc4b6Seschrock 3360fa9e4066Sahrens /* 3361fa9e4066Sahrens * If the I/O failed, determine whether we should attempt to retry it. 336235a5a358SJonathan Adams * 336335a5a358SJonathan Adams * On retry, we cut in line in the issue queue, since we don't want 336435a5a358SJonathan Adams * compression/checksumming/etc. work to prevent our (cheap) IO reissue. 3365fa9e4066Sahrens */ 3366e14bb325SJeff Bonwick if (zio->io_error && vd == NULL && 3367e14bb325SJeff Bonwick !(zio->io_flags & (ZIO_FLAG_DONT_RETRY | ZIO_FLAG_IO_RETRY))) { 3368e14bb325SJeff Bonwick ASSERT(!(zio->io_flags & ZIO_FLAG_DONT_QUEUE)); /* not a leaf */ 3369e14bb325SJeff Bonwick ASSERT(!(zio->io_flags & ZIO_FLAG_IO_BYPASS)); /* not a leaf */ 3370fa9e4066Sahrens zio->io_error = 0; 3371e14bb325SJeff Bonwick zio->io_flags |= ZIO_FLAG_IO_RETRY | 3372e14bb325SJeff Bonwick ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE; 3373b24ab676SJeff Bonwick zio->io_stage = ZIO_STAGE_VDEV_IO_START >> 1; 337435a5a358SJonathan Adams zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, 337535a5a358SJonathan Adams zio_requeue_io_start_cut_in_line); 3376e14bb325SJeff Bonwick return (ZIO_PIPELINE_STOP); 3377ea8dc4b6Seschrock } 3378fa9e4066Sahrens 3379e14bb325SJeff Bonwick /* 3380e14bb325SJeff Bonwick * If we got an error on a leaf device, convert it to ENXIO 3381e14bb325SJeff Bonwick * if the device is not accessible at all. 3382e14bb325SJeff Bonwick */ 3383e14bb325SJeff Bonwick if (zio->io_error && vd != NULL && vd->vdev_ops->vdev_op_leaf && 3384e14bb325SJeff Bonwick !vdev_accessible(vd, zio)) 3385be6fd75aSMatthew Ahrens zio->io_error = SET_ERROR(ENXIO); 3386e14bb325SJeff Bonwick 3387e14bb325SJeff Bonwick /* 3388e14bb325SJeff Bonwick * If we can't write to an interior vdev (mirror or RAID-Z), 3389e14bb325SJeff Bonwick * set vdev_cant_write so that we stop trying to allocate from it. 3390e14bb325SJeff Bonwick */ 3391e14bb325SJeff Bonwick if (zio->io_error == ENXIO && zio->io_type == ZIO_TYPE_WRITE && 33923b2aab18SMatthew Ahrens vd != NULL && !vd->vdev_ops->vdev_op_leaf) { 3393e14bb325SJeff Bonwick vd->vdev_cant_write = B_TRUE; 33943b2aab18SMatthew Ahrens } 3395e14bb325SJeff Bonwick 3396295438baSHans Rosenfeld /* 3397295438baSHans Rosenfeld * If a cache flush returns ENOTSUP or ENOTTY, we know that no future 3398295438baSHans Rosenfeld * attempts will ever succeed. In this case we set a persistent bit so 3399295438baSHans Rosenfeld * that we don't bother with it in the future. 3400295438baSHans Rosenfeld */ 3401295438baSHans Rosenfeld if ((zio->io_error == ENOTSUP || zio->io_error == ENOTTY) && 3402295438baSHans Rosenfeld zio->io_type == ZIO_TYPE_IOCTL && 3403295438baSHans Rosenfeld zio->io_cmd == DKIOCFLUSHWRITECACHE && vd != NULL) 3404295438baSHans Rosenfeld vd->vdev_nowritecache = B_TRUE; 3405295438baSHans Rosenfeld 3406e14bb325SJeff Bonwick if (zio->io_error) 3407e14bb325SJeff Bonwick zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 3408e14bb325SJeff Bonwick 340969962b56SMatthew Ahrens if (vd != NULL && vd->vdev_ops->vdev_op_leaf && 341069962b56SMatthew Ahrens zio->io_physdone != NULL) { 341169962b56SMatthew Ahrens ASSERT(!(zio->io_flags & ZIO_FLAG_DELEGATED)); 341269962b56SMatthew Ahrens ASSERT(zio->io_child_type == ZIO_CHILD_VDEV); 341369962b56SMatthew Ahrens zio->io_physdone(zio->io_logical); 341469962b56SMatthew Ahrens } 341569962b56SMatthew Ahrens 3416e05725b1Sbonwick return (ZIO_PIPELINE_CONTINUE); 3417fa9e4066Sahrens } 3418fa9e4066Sahrens 3419fa9e4066Sahrens void 3420fa9e4066Sahrens zio_vdev_io_reissue(zio_t *zio) 3421fa9e4066Sahrens { 3422fa9e4066Sahrens ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START); 3423fa9e4066Sahrens ASSERT(zio->io_error == 0); 3424fa9e4066Sahrens 3425b24ab676SJeff Bonwick zio->io_stage >>= 1; 3426fa9e4066Sahrens } 3427fa9e4066Sahrens 3428fa9e4066Sahrens void 3429fa9e4066Sahrens zio_vdev_io_redone(zio_t *zio) 3430fa9e4066Sahrens { 3431fa9e4066Sahrens ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE); 3432fa9e4066Sahrens 3433b24ab676SJeff Bonwick zio->io_stage >>= 1; 3434fa9e4066Sahrens } 3435fa9e4066Sahrens 3436fa9e4066Sahrens void 3437fa9e4066Sahrens zio_vdev_io_bypass(zio_t *zio) 3438fa9e4066Sahrens { 3439fa9e4066Sahrens ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START); 3440fa9e4066Sahrens ASSERT(zio->io_error == 0); 3441fa9e4066Sahrens 3442fa9e4066Sahrens zio->io_flags |= ZIO_FLAG_IO_BYPASS; 3443b24ab676SJeff Bonwick zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS >> 1; 3444fa9e4066Sahrens } 3445fa9e4066Sahrens 3446fa9e4066Sahrens /* 3447fa9e4066Sahrens * ========================================================================== 3448fa9e4066Sahrens * Generate and verify checksums 3449fa9e4066Sahrens * ========================================================================== 3450fa9e4066Sahrens */ 3451e05725b1Sbonwick static int 3452fa9e4066Sahrens zio_checksum_generate(zio_t *zio) 3453fa9e4066Sahrens { 3454fa9e4066Sahrens blkptr_t *bp = zio->io_bp; 3455e14bb325SJeff Bonwick enum zio_checksum checksum; 3456fa9e4066Sahrens 3457e14bb325SJeff Bonwick if (bp == NULL) { 3458e14bb325SJeff Bonwick /* 3459e14bb325SJeff Bonwick * This is zio_write_phys(). 3460e14bb325SJeff Bonwick * We're either generating a label checksum, or none at all. 3461e14bb325SJeff Bonwick */ 3462e14bb325SJeff Bonwick checksum = zio->io_prop.zp_checksum; 3463e14bb325SJeff Bonwick 3464e14bb325SJeff Bonwick if (checksum == ZIO_CHECKSUM_OFF) 3465e14bb325SJeff Bonwick return (ZIO_PIPELINE_CONTINUE); 3466fa9e4066Sahrens 3467e14bb325SJeff Bonwick ASSERT(checksum == ZIO_CHECKSUM_LABEL); 3468e14bb325SJeff Bonwick } else { 3469e14bb325SJeff Bonwick if (BP_IS_GANG(bp) && zio->io_child_type == ZIO_CHILD_GANG) { 3470e14bb325SJeff Bonwick ASSERT(!IO_IS_ALLOCATING(zio)); 3471e14bb325SJeff Bonwick checksum = ZIO_CHECKSUM_GANG_HEADER; 3472e14bb325SJeff Bonwick } else { 3473e14bb325SJeff Bonwick checksum = BP_GET_CHECKSUM(bp); 3474e14bb325SJeff Bonwick } 3475e14bb325SJeff Bonwick } 3476fa9e4066Sahrens 3477770499e1SDan Kimmel zio_checksum_compute(zio, checksum, zio->io_abd, zio->io_size); 3478fa9e4066Sahrens 3479e05725b1Sbonwick return (ZIO_PIPELINE_CONTINUE); 3480fa9e4066Sahrens } 3481fa9e4066Sahrens 3482e05725b1Sbonwick static int 3483e14bb325SJeff Bonwick zio_checksum_verify(zio_t *zio) 3484fa9e4066Sahrens { 348522fe2c88SJonathan Adams zio_bad_cksum_t info; 3486e14bb325SJeff Bonwick blkptr_t *bp = zio->io_bp; 3487e14bb325SJeff Bonwick int error; 3488fa9e4066Sahrens 3489b24ab676SJeff Bonwick ASSERT(zio->io_vd != NULL); 3490b24ab676SJeff Bonwick 3491e14bb325SJeff Bonwick if (bp == NULL) { 3492e14bb325SJeff Bonwick /* 3493e14bb325SJeff Bonwick * This is zio_read_phys(). 3494e14bb325SJeff Bonwick * We're either verifying a label checksum, or nothing at all. 3495e14bb325SJeff Bonwick */ 3496e14bb325SJeff Bonwick if (zio->io_prop.zp_checksum == ZIO_CHECKSUM_OFF) 3497e14bb325SJeff Bonwick return (ZIO_PIPELINE_CONTINUE); 3498fa9e4066Sahrens 3499e14bb325SJeff Bonwick ASSERT(zio->io_prop.zp_checksum == ZIO_CHECKSUM_LABEL); 3500e14bb325SJeff Bonwick } 3501fa9e4066Sahrens 350222fe2c88SJonathan Adams if ((error = zio_checksum_error(zio, &info)) != 0) { 3503e14bb325SJeff Bonwick zio->io_error = error; 3504373dc1cfSMatthew Ahrens if (error == ECKSUM && 3505373dc1cfSMatthew Ahrens !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) { 350622fe2c88SJonathan Adams zfs_ereport_start_checksum(zio->io_spa, 350722fe2c88SJonathan Adams zio->io_vd, zio, zio->io_offset, 350822fe2c88SJonathan Adams zio->io_size, NULL, &info); 3509e14bb325SJeff Bonwick } 3510fa9e4066Sahrens } 3511fa9e4066Sahrens 3512e05725b1Sbonwick return (ZIO_PIPELINE_CONTINUE); 3513fa9e4066Sahrens } 3514fa9e4066Sahrens 3515fa9e4066Sahrens /* 3516fa9e4066Sahrens * Called by RAID-Z to ensure we don't compute the checksum twice. 3517fa9e4066Sahrens */ 3518fa9e4066Sahrens void 3519fa9e4066Sahrens zio_checksum_verified(zio_t *zio) 3520fa9e4066Sahrens { 3521b24ab676SJeff Bonwick zio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY; 3522fa9e4066Sahrens } 3523fa9e4066Sahrens 3524fa9e4066Sahrens /* 3525e14bb325SJeff Bonwick * ========================================================================== 3526e14bb325SJeff Bonwick * Error rank. Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other. 35275d7b4d43SMatthew Ahrens * An error of 0 indicates success. ENXIO indicates whole-device failure, 3528e14bb325SJeff Bonwick * which may be transient (e.g. unplugged) or permament. ECKSUM and EIO 3529e14bb325SJeff Bonwick * indicate errors that are specific to one I/O, and most likely permanent. 3530e14bb325SJeff Bonwick * Any other error is presumed to be worse because we weren't expecting it. 3531e14bb325SJeff Bonwick * ========================================================================== 3532fa9e4066Sahrens */ 3533e14bb325SJeff Bonwick int 3534e14bb325SJeff Bonwick zio_worst_error(int e1, int e2) 3535fa9e4066Sahrens { 3536e14bb325SJeff Bonwick static int zio_error_rank[] = { 0, ENXIO, ECKSUM, EIO }; 3537e14bb325SJeff Bonwick int r1, r2; 3538e14bb325SJeff Bonwick 3539e14bb325SJeff Bonwick for (r1 = 0; r1 < sizeof (zio_error_rank) / sizeof (int); r1++) 3540e14bb325SJeff Bonwick if (e1 == zio_error_rank[r1]) 3541e14bb325SJeff Bonwick break; 3542e14bb325SJeff Bonwick 3543e14bb325SJeff Bonwick for (r2 = 0; r2 < sizeof (zio_error_rank) / sizeof (int); r2++) 3544e14bb325SJeff Bonwick if (e2 == zio_error_rank[r2]) 3545e14bb325SJeff Bonwick break; 354644cd46caSbillm 3547e14bb325SJeff Bonwick return (r1 > r2 ? e1 : e2); 3548fa9e4066Sahrens } 3549fa9e4066Sahrens 3550fa9e4066Sahrens /* 3551fa9e4066Sahrens * ========================================================================== 3552e14bb325SJeff Bonwick * I/O completion 3553fa9e4066Sahrens * ========================================================================== 3554fa9e4066Sahrens */ 3555e14bb325SJeff Bonwick static int 3556e14bb325SJeff Bonwick zio_ready(zio_t *zio) 3557fa9e4066Sahrens { 3558e14bb325SJeff Bonwick blkptr_t *bp = zio->io_bp; 3559a3f829aeSBill Moore zio_t *pio, *pio_next; 35600f7643c7SGeorge Wilson zio_link_t *zl = NULL; 3561fa9e4066Sahrens 3562d6e1c446SGeorge Wilson if (zio_wait_for_children(zio, ZIO_CHILD_GANG_BIT | ZIO_CHILD_DDT_BIT, 3563d6e1c446SGeorge Wilson ZIO_WAIT_READY)) { 3564f5383399SBill Moore return (ZIO_PIPELINE_STOP); 3565d6e1c446SGeorge Wilson } 3566fa9e4066Sahrens 3567f5383399SBill Moore if (zio->io_ready) { 3568e14bb325SJeff Bonwick ASSERT(IO_IS_ALLOCATING(zio)); 356980901aeaSGeorge Wilson ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp) || 357080901aeaSGeorge Wilson (zio->io_flags & ZIO_FLAG_NOPWRITE)); 3571e14bb325SJeff Bonwick ASSERT(zio->io_children[ZIO_CHILD_GANG][ZIO_WAIT_READY] == 0); 3572fa9e4066Sahrens 3573e14bb325SJeff Bonwick zio->io_ready(zio); 3574e14bb325SJeff Bonwick } 3575fa9e4066Sahrens 3576e14bb325SJeff Bonwick if (bp != NULL && bp != &zio->io_bp_copy) 3577e14bb325SJeff Bonwick zio->io_bp_copy = *bp; 3578fa9e4066Sahrens 35790f7643c7SGeorge Wilson if (zio->io_error != 0) { 3580e14bb325SJeff Bonwick zio->io_pipeline = ZIO_INTERLOCK_PIPELINE; 3581fa9e4066Sahrens 35820f7643c7SGeorge Wilson if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) { 35830f7643c7SGeorge Wilson ASSERT(IO_IS_ALLOCATING(zio)); 35840f7643c7SGeorge Wilson ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE); 3585*663207adSDon Brady ASSERT(zio->io_metaslab_class != NULL); 3586*663207adSDon Brady 35870f7643c7SGeorge Wilson /* 35880f7643c7SGeorge Wilson * We were unable to allocate anything, unreserve and 35890f7643c7SGeorge Wilson * issue the next I/O to allocate. 35900f7643c7SGeorge Wilson */ 35910f7643c7SGeorge Wilson metaslab_class_throttle_unreserve( 3592*663207adSDon Brady zio->io_metaslab_class, zio->io_prop.zp_copies, 3593*663207adSDon Brady zio->io_allocator, zio); 3594f78cdc34SPaul Dagnelie zio_allocate_dispatch(zio->io_spa, zio->io_allocator); 35950f7643c7SGeorge Wilson } 35960f7643c7SGeorge Wilson } 35970f7643c7SGeorge Wilson 3598a3f829aeSBill Moore mutex_enter(&zio->io_lock); 3599a3f829aeSBill Moore zio->io_state[ZIO_WAIT_READY] = 1; 36000f7643c7SGeorge Wilson pio = zio_walk_parents(zio, &zl); 3601a3f829aeSBill Moore mutex_exit(&zio->io_lock); 3602a3f829aeSBill Moore 3603a3f829aeSBill Moore /* 3604a3f829aeSBill Moore * As we notify zio's parents, new parents could be added. 3605a3f829aeSBill Moore * New parents go to the head of zio's io_parent_list, however, 3606a3f829aeSBill Moore * so we will (correctly) not notify them. The remainder of zio's 3607a3f829aeSBill Moore * io_parent_list, from 'pio_next' onward, cannot change because 3608a3f829aeSBill Moore * all parents must wait for us to be done before they can be done. 3609a3f829aeSBill Moore */ 3610a3f829aeSBill Moore for (; pio != NULL; pio = pio_next) { 36110f7643c7SGeorge Wilson pio_next = zio_walk_parents(zio, &zl); 3612e14bb325SJeff Bonwick zio_notify_parent(pio, zio, ZIO_WAIT_READY); 3613a3f829aeSBill Moore } 3614fa9e4066Sahrens 3615b24ab676SJeff Bonwick if (zio->io_flags & ZIO_FLAG_NODATA) { 3616b24ab676SJeff Bonwick if (BP_IS_GANG(bp)) { 3617b24ab676SJeff Bonwick zio->io_flags &= ~ZIO_FLAG_NODATA; 3618b24ab676SJeff Bonwick } else { 3619770499e1SDan Kimmel ASSERT((uintptr_t)zio->io_abd < SPA_MAXBLOCKSIZE); 3620b24ab676SJeff Bonwick zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES; 3621b24ab676SJeff Bonwick } 3622b24ab676SJeff Bonwick } 3623b24ab676SJeff Bonwick 3624a33cae98STim Haley if (zio_injection_enabled && 3625a33cae98STim Haley zio->io_spa->spa_syncing_txg == zio->io_txg) 3626a33cae98STim Haley zio_handle_ignored_writes(zio); 3627a33cae98STim Haley 3628e14bb325SJeff Bonwick return (ZIO_PIPELINE_CONTINUE); 3629fa9e4066Sahrens } 3630fa9e4066Sahrens 36310f7643c7SGeorge Wilson /* 36320f7643c7SGeorge Wilson * Update the allocation throttle accounting. 36330f7643c7SGeorge Wilson */ 36340f7643c7SGeorge Wilson static void 36350f7643c7SGeorge Wilson zio_dva_throttle_done(zio_t *zio) 36360f7643c7SGeorge Wilson { 36370f7643c7SGeorge Wilson zio_t *lio = zio->io_logical; 36380f7643c7SGeorge Wilson zio_t *pio = zio_unique_parent(zio); 36390f7643c7SGeorge Wilson vdev_t *vd = zio->io_vd; 36400f7643c7SGeorge Wilson int flags = METASLAB_ASYNC_ALLOC; 36410f7643c7SGeorge Wilson 36420f7643c7SGeorge Wilson ASSERT3P(zio->io_bp, !=, NULL); 36430f7643c7SGeorge Wilson ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE); 36440f7643c7SGeorge Wilson ASSERT3U(zio->io_priority, ==, ZIO_PRIORITY_ASYNC_WRITE); 36450f7643c7SGeorge Wilson ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV); 36460f7643c7SGeorge Wilson ASSERT(vd != NULL); 36470f7643c7SGeorge Wilson ASSERT3P(vd, ==, vd->vdev_top); 36480f7643c7SGeorge Wilson ASSERT(!(zio->io_flags & (ZIO_FLAG_IO_REPAIR | ZIO_FLAG_IO_RETRY))); 36490f7643c7SGeorge Wilson ASSERT(zio->io_flags & ZIO_FLAG_IO_ALLOCATING); 36500f7643c7SGeorge Wilson ASSERT(!(lio->io_flags & ZIO_FLAG_IO_REWRITE)); 36510f7643c7SGeorge Wilson ASSERT(!(lio->io_orig_flags & ZIO_FLAG_NODATA)); 36520f7643c7SGeorge Wilson 36530f7643c7SGeorge Wilson /* 36540f7643c7SGeorge Wilson * Parents of gang children can have two flavors -- ones that 36550f7643c7SGeorge Wilson * allocated the gang header (will have ZIO_FLAG_IO_REWRITE set) 36560f7643c7SGeorge Wilson * and ones that allocated the constituent blocks. The allocation 36570f7643c7SGeorge Wilson * throttle needs to know the allocating parent zio so we must find 36580f7643c7SGeorge Wilson * it here. 36590f7643c7SGeorge Wilson */ 36600f7643c7SGeorge Wilson if (pio->io_child_type == ZIO_CHILD_GANG) { 36610f7643c7SGeorge Wilson /* 36620f7643c7SGeorge Wilson * If our parent is a rewrite gang child then our grandparent 36630f7643c7SGeorge Wilson * would have been the one that performed the allocation. 36640f7643c7SGeorge Wilson */ 36650f7643c7SGeorge Wilson if (pio->io_flags & ZIO_FLAG_IO_REWRITE) 36660f7643c7SGeorge Wilson pio = zio_unique_parent(pio); 36670f7643c7SGeorge Wilson flags |= METASLAB_GANG_CHILD; 36680f7643c7SGeorge Wilson } 36690f7643c7SGeorge Wilson 36700f7643c7SGeorge Wilson ASSERT(IO_IS_ALLOCATING(pio)); 36710f7643c7SGeorge Wilson ASSERT3P(zio, !=, zio->io_logical); 36720f7643c7SGeorge Wilson ASSERT(zio->io_logical != NULL); 36730f7643c7SGeorge Wilson ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REPAIR)); 36740f7643c7SGeorge Wilson ASSERT0(zio->io_flags & ZIO_FLAG_NOPWRITE); 3675*663207adSDon Brady ASSERT(zio->io_metaslab_class != NULL); 36760f7643c7SGeorge Wilson 36770f7643c7SGeorge Wilson mutex_enter(&pio->io_lock); 3678f78cdc34SPaul Dagnelie metaslab_group_alloc_decrement(zio->io_spa, vd->vdev_id, pio, flags, 3679f78cdc34SPaul Dagnelie pio->io_allocator, B_TRUE); 36800f7643c7SGeorge Wilson mutex_exit(&pio->io_lock); 36810f7643c7SGeorge Wilson 3682*663207adSDon Brady metaslab_class_throttle_unreserve(zio->io_metaslab_class, 1, 3683*663207adSDon Brady pio->io_allocator, pio); 36840f7643c7SGeorge Wilson 36850f7643c7SGeorge Wilson /* 36860f7643c7SGeorge Wilson * Call into the pipeline to see if there is more work that 36870f7643c7SGeorge Wilson * needs to be done. If there is work to be done it will be 36880f7643c7SGeorge Wilson * dispatched to another taskq thread. 36890f7643c7SGeorge Wilson */ 3690f78cdc34SPaul Dagnelie zio_allocate_dispatch(zio->io_spa, pio->io_allocator); 36910f7643c7SGeorge Wilson } 36920f7643c7SGeorge Wilson 3693e14bb325SJeff Bonwick static int 3694e14bb325SJeff Bonwick zio_done(zio_t *zio) 3695d63d470bSgw { 3696e14bb325SJeff Bonwick spa_t *spa = zio->io_spa; 3697e14bb325SJeff Bonwick zio_t *lio = zio->io_logical; 3698e14bb325SJeff Bonwick blkptr_t *bp = zio->io_bp; 3699e14bb325SJeff Bonwick vdev_t *vd = zio->io_vd; 3700e14bb325SJeff Bonwick uint64_t psize = zio->io_size; 3701a3f829aeSBill Moore zio_t *pio, *pio_next; 37020f7643c7SGeorge Wilson zio_link_t *zl = NULL; 3703d63d470bSgw 3704e14bb325SJeff Bonwick /* 3705f5383399SBill Moore * If our children haven't all completed, 3706e14bb325SJeff Bonwick * wait for them and then repeat this pipeline stage. 3707e14bb325SJeff Bonwick */ 3708d6e1c446SGeorge Wilson if (zio_wait_for_children(zio, ZIO_CHILD_ALL_BITS, ZIO_WAIT_DONE)) { 3709e14bb325SJeff Bonwick return (ZIO_PIPELINE_STOP); 3710d6e1c446SGeorge Wilson } 3711d63d470bSgw 37120f7643c7SGeorge Wilson /* 37130f7643c7SGeorge Wilson * If the allocation throttle is enabled, then update the accounting. 37140f7643c7SGeorge Wilson * We only track child I/Os that are part of an allocating async 37150f7643c7SGeorge Wilson * write. We must do this since the allocation is performed 37160f7643c7SGeorge Wilson * by the logical I/O but the actual write is done by child I/Os. 37170f7643c7SGeorge Wilson */ 37180f7643c7SGeorge Wilson if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING && 37190f7643c7SGeorge Wilson zio->io_child_type == ZIO_CHILD_VDEV) { 3720*663207adSDon Brady ASSERT(zio->io_metaslab_class != NULL); 3721*663207adSDon Brady ASSERT(zio->io_metaslab_class->mc_alloc_throttle_enabled); 37220f7643c7SGeorge Wilson zio_dva_throttle_done(zio); 37230f7643c7SGeorge Wilson } 37240f7643c7SGeorge Wilson 37250f7643c7SGeorge Wilson /* 37260f7643c7SGeorge Wilson * If the allocation throttle is enabled, verify that 37270f7643c7SGeorge Wilson * we have decremented the refcounts for every I/O that was throttled. 37280f7643c7SGeorge Wilson */ 37290f7643c7SGeorge Wilson if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) { 37300f7643c7SGeorge Wilson ASSERT(zio->io_type == ZIO_TYPE_WRITE); 37310f7643c7SGeorge Wilson ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE); 37320f7643c7SGeorge Wilson ASSERT(bp != NULL); 3733*663207adSDon Brady 3734f78cdc34SPaul Dagnelie metaslab_group_alloc_verify(spa, zio->io_bp, zio, 3735f78cdc34SPaul Dagnelie zio->io_allocator); 3736e914ace2STim Schumacher VERIFY(zfs_refcount_not_held( 3737*663207adSDon Brady &zio->io_metaslab_class->mc_alloc_slots[zio->io_allocator], 3738*663207adSDon Brady zio)); 37390f7643c7SGeorge Wilson } 37400f7643c7SGeorge Wilson 3741e14bb325SJeff Bonwick for (int c = 0; c < ZIO_CHILD_TYPES; c++) 3742e14bb325SJeff Bonwick for (int w = 0; w < ZIO_WAIT_TYPES; w++) 3743e14bb325SJeff Bonwick ASSERT(zio->io_children[c][w] == 0); 3744e14bb325SJeff Bonwick 37455d7b4d43SMatthew Ahrens if (bp != NULL && !BP_IS_EMBEDDED(bp)) { 3746e14bb325SJeff Bonwick ASSERT(bp->blk_pad[0] == 0); 3747e14bb325SJeff Bonwick ASSERT(bp->blk_pad[1] == 0); 3748e14bb325SJeff Bonwick ASSERT(bcmp(bp, &zio->io_bp_copy, sizeof (blkptr_t)) == 0 || 3749a3f829aeSBill Moore (bp == zio_unique_parent(zio)->io_bp)); 3750e14bb325SJeff Bonwick if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(bp) && 3751b24ab676SJeff Bonwick zio->io_bp_override == NULL && 3752e14bb325SJeff Bonwick !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) { 3753e14bb325SJeff Bonwick ASSERT(!BP_SHOULD_BYTESWAP(bp)); 3754b24ab676SJeff Bonwick ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(bp)); 3755e14bb325SJeff Bonwick ASSERT(BP_COUNT_GANG(bp) == 0 || 3756e14bb325SJeff Bonwick (BP_COUNT_GANG(bp) == BP_GET_NDVAS(bp))); 3757e14bb325SJeff Bonwick } 375880901aeaSGeorge Wilson if (zio->io_flags & ZIO_FLAG_NOPWRITE) 375980901aeaSGeorge Wilson VERIFY(BP_EQUAL(bp, &zio->io_bp_orig)); 3760e14bb325SJeff Bonwick } 3761fa9e4066Sahrens 3762e14bb325SJeff Bonwick /* 3763b24ab676SJeff Bonwick * If there were child vdev/gang/ddt errors, they apply to us now. 3764e14bb325SJeff Bonwick */ 3765e14bb325SJeff Bonwick zio_inherit_child_errors(zio, ZIO_CHILD_VDEV); 3766e14bb325SJeff Bonwick zio_inherit_child_errors(zio, ZIO_CHILD_GANG); 3767b24ab676SJeff Bonwick zio_inherit_child_errors(zio, ZIO_CHILD_DDT); 3768b24ab676SJeff Bonwick 3769b24ab676SJeff Bonwick /* 3770b24ab676SJeff Bonwick * If the I/O on the transformed data was successful, generate any 3771b24ab676SJeff Bonwick * checksum reports now while we still have the transformed data. 3772b24ab676SJeff Bonwick */ 3773b24ab676SJeff Bonwick if (zio->io_error == 0) { 3774b24ab676SJeff Bonwick while (zio->io_cksum_report != NULL) { 3775b24ab676SJeff Bonwick zio_cksum_report_t *zcr = zio->io_cksum_report; 3776b24ab676SJeff Bonwick uint64_t align = zcr->zcr_align; 3777b24ab676SJeff Bonwick uint64_t asize = P2ROUNDUP(psize, align); 3778770499e1SDan Kimmel char *abuf = NULL; 3779770499e1SDan Kimmel abd_t *adata = zio->io_abd; 3780b24ab676SJeff Bonwick 3781b24ab676SJeff Bonwick if (asize != psize) { 3782770499e1SDan Kimmel adata = abd_alloc_linear(asize, B_TRUE); 3783770499e1SDan Kimmel abd_copy(adata, zio->io_abd, psize); 3784770499e1SDan Kimmel abd_zero_off(adata, psize, asize - psize); 3785b24ab676SJeff Bonwick } 3786b24ab676SJeff Bonwick 3787770499e1SDan Kimmel if (adata != NULL) 3788770499e1SDan Kimmel abuf = abd_borrow_buf_copy(adata, asize); 3789770499e1SDan Kimmel 3790b24ab676SJeff Bonwick zio->io_cksum_report = zcr->zcr_next; 3791b24ab676SJeff Bonwick zcr->zcr_next = NULL; 3792b24ab676SJeff Bonwick zcr->zcr_finish(zcr, abuf); 3793b24ab676SJeff Bonwick zfs_ereport_free_checksum(zcr); 3794b24ab676SJeff Bonwick 3795770499e1SDan Kimmel if (adata != NULL) 3796770499e1SDan Kimmel abd_return_buf(adata, abuf, asize); 3797770499e1SDan Kimmel 3798b24ab676SJeff Bonwick if (asize != psize) 3799770499e1SDan Kimmel abd_free(adata); 3800b24ab676SJeff Bonwick } 3801b24ab676SJeff Bonwick } 3802e14bb325SJeff Bonwick 3803e14bb325SJeff Bonwick zio_pop_transforms(zio); /* note: may set zio->io_error */ 3804e14bb325SJeff Bonwick 3805e14bb325SJeff Bonwick vdev_stat_update(zio, psize); 3806e14bb325SJeff Bonwick 3807e14bb325SJeff Bonwick if (zio->io_error) { 3808e14bb325SJeff Bonwick /* 3809e14bb325SJeff Bonwick * If this I/O is attached to a particular vdev, 3810e14bb325SJeff Bonwick * generate an error message describing the I/O failure 3811e14bb325SJeff Bonwick * at the block level. We ignore these errors if the 3812e14bb325SJeff Bonwick * device is currently unavailable. 3813e14bb325SJeff Bonwick */ 3814e14bb325SJeff Bonwick if (zio->io_error != ECKSUM && vd != NULL && !vdev_is_dead(vd)) 3815e14bb325SJeff Bonwick zfs_ereport_post(FM_EREPORT_ZFS_IO, spa, vd, zio, 0, 0); 3816e14bb325SJeff Bonwick 38178f18d1faSGeorge Wilson if ((zio->io_error == EIO || !(zio->io_flags & 38188f18d1faSGeorge Wilson (ZIO_FLAG_SPECULATIVE | ZIO_FLAG_DONT_PROPAGATE))) && 38198f18d1faSGeorge Wilson zio == lio) { 3820e14bb325SJeff Bonwick /* 3821e14bb325SJeff Bonwick * For logical I/O requests, tell the SPA to log the 3822e14bb325SJeff Bonwick * error and generate a logical data ereport. 3823e14bb325SJeff Bonwick */ 3824e14bb325SJeff Bonwick spa_log_error(spa, zio); 3825e14bb325SJeff Bonwick zfs_ereport_post(FM_EREPORT_ZFS_DATA, spa, NULL, zio, 3826e14bb325SJeff Bonwick 0, 0); 3827e14bb325SJeff Bonwick } 3828e14bb325SJeff Bonwick } 3829fa9e4066Sahrens 3830e14bb325SJeff Bonwick if (zio->io_error && zio == lio) { 3831e14bb325SJeff Bonwick /* 3832e14bb325SJeff Bonwick * Determine whether zio should be reexecuted. This will 3833e14bb325SJeff Bonwick * propagate all the way to the root via zio_notify_parent(). 3834e14bb325SJeff Bonwick */ 3835e14bb325SJeff Bonwick ASSERT(vd == NULL && bp != NULL); 3836b24ab676SJeff Bonwick ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 3837e14bb325SJeff Bonwick 3838b24ab676SJeff Bonwick if (IO_IS_ALLOCATING(zio) && 3839b24ab676SJeff Bonwick !(zio->io_flags & ZIO_FLAG_CANFAIL)) { 3840e14bb325SJeff Bonwick if (zio->io_error != ENOSPC) 3841e14bb325SJeff Bonwick zio->io_reexecute |= ZIO_REEXECUTE_NOW; 3842e14bb325SJeff Bonwick else 3843e14bb325SJeff Bonwick zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND; 3844b24ab676SJeff Bonwick } 3845e14bb325SJeff Bonwick 3846e14bb325SJeff Bonwick if ((zio->io_type == ZIO_TYPE_READ || 3847e14bb325SJeff Bonwick zio->io_type == ZIO_TYPE_FREE) && 384844ecc532SGeorge Wilson !(zio->io_flags & ZIO_FLAG_SCAN_THREAD) && 3849e14bb325SJeff Bonwick zio->io_error == ENXIO && 3850b16da2e2SGeorge Wilson spa_load_state(spa) == SPA_LOAD_NONE && 3851e14bb325SJeff Bonwick spa_get_failmode(spa) != ZIO_FAILURE_MODE_CONTINUE) 3852e14bb325SJeff Bonwick zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND; 3853e14bb325SJeff Bonwick 3854e14bb325SJeff Bonwick if (!(zio->io_flags & ZIO_FLAG_CANFAIL) && !zio->io_reexecute) 3855e14bb325SJeff Bonwick zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND; 385622fe2c88SJonathan Adams 385722fe2c88SJonathan Adams /* 385822fe2c88SJonathan Adams * Here is a possibly good place to attempt to do 385922fe2c88SJonathan Adams * either combinatorial reconstruction or error correction 386022fe2c88SJonathan Adams * based on checksums. It also might be a good place 386122fe2c88SJonathan Adams * to send out preliminary ereports before we suspend 386222fe2c88SJonathan Adams * processing. 386322fe2c88SJonathan Adams */ 3864d63d470bSgw } 3865d63d470bSgw 386667bd71c6Sperrin /* 3867e14bb325SJeff Bonwick * If there were logical child errors, they apply to us now. 3868e14bb325SJeff Bonwick * We defer this until now to avoid conflating logical child 3869e14bb325SJeff Bonwick * errors with errors that happened to the zio itself when 3870e14bb325SJeff Bonwick * updating vdev stats and reporting FMA events above. 387167bd71c6Sperrin */ 3872e14bb325SJeff Bonwick zio_inherit_child_errors(zio, ZIO_CHILD_LOGICAL); 38738654d025Sperrin 3874b24ab676SJeff Bonwick if ((zio->io_error || zio->io_reexecute) && 3875b24ab676SJeff Bonwick IO_IS_ALLOCATING(zio) && zio->io_gang_leader == zio && 387680901aeaSGeorge Wilson !(zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE))) 3877f5383399SBill Moore zio_dva_unallocate(zio, zio->io_gang_tree, bp); 3878f5383399SBill Moore 3879f5383399SBill Moore zio_gang_tree_free(&zio->io_gang_tree); 3880f5383399SBill Moore 388133a372edSGeorge Wilson /* 388233a372edSGeorge Wilson * Godfather I/Os should never suspend. 388333a372edSGeorge Wilson */ 388433a372edSGeorge Wilson if ((zio->io_flags & ZIO_FLAG_GODFATHER) && 388533a372edSGeorge Wilson (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND)) 388633a372edSGeorge Wilson zio->io_reexecute = 0; 388733a372edSGeorge Wilson 388833a372edSGeorge Wilson if (zio->io_reexecute) { 3889e14bb325SJeff Bonwick /* 3890e14bb325SJeff Bonwick * This is a logical I/O that wants to reexecute. 3891e14bb325SJeff Bonwick * 3892e14bb325SJeff Bonwick * Reexecute is top-down. When an i/o fails, if it's not 3893e14bb325SJeff Bonwick * the root, it simply notifies its parent and sticks around. 3894e14bb325SJeff Bonwick * The parent, seeing that it still has children in zio_done(), 3895e14bb325SJeff Bonwick * does the same. This percolates all the way up to the root. 3896e14bb325SJeff Bonwick * The root i/o will reexecute or suspend the entire tree. 3897e14bb325SJeff Bonwick * 3898e14bb325SJeff Bonwick * This approach ensures that zio_reexecute() honors 3899e14bb325SJeff Bonwick * all the original i/o dependency relationships, e.g. 3900e14bb325SJeff Bonwick * parents not executing until children are ready. 3901e14bb325SJeff Bonwick */ 3902e14bb325SJeff Bonwick ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL); 3903fa9e4066Sahrens 3904f5383399SBill Moore zio->io_gang_leader = NULL; 3905e14bb325SJeff Bonwick 3906a3f829aeSBill Moore mutex_enter(&zio->io_lock); 3907a3f829aeSBill Moore zio->io_state[ZIO_WAIT_DONE] = 1; 3908a3f829aeSBill Moore mutex_exit(&zio->io_lock); 3909a3f829aeSBill Moore 391054d692b7SGeorge Wilson /* 391154d692b7SGeorge Wilson * "The Godfather" I/O monitors its children but is 391254d692b7SGeorge Wilson * not a true parent to them. It will track them through 391354d692b7SGeorge Wilson * the pipeline but severs its ties whenever they get into 391454d692b7SGeorge Wilson * trouble (e.g. suspended). This allows "The Godfather" 391554d692b7SGeorge Wilson * I/O to return status without blocking. 391654d692b7SGeorge Wilson */ 39170f7643c7SGeorge Wilson zl = NULL; 39180f7643c7SGeorge Wilson for (pio = zio_walk_parents(zio, &zl); pio != NULL; 39190f7643c7SGeorge Wilson pio = pio_next) { 39200f7643c7SGeorge Wilson zio_link_t *remove_zl = zl; 39210f7643c7SGeorge Wilson pio_next = zio_walk_parents(zio, &zl); 392254d692b7SGeorge Wilson 392354d692b7SGeorge Wilson if ((pio->io_flags & ZIO_FLAG_GODFATHER) && 392454d692b7SGeorge Wilson (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND)) { 39250f7643c7SGeorge Wilson zio_remove_child(pio, zio, remove_zl); 392654d692b7SGeorge Wilson zio_notify_parent(pio, zio, ZIO_WAIT_DONE); 392754d692b7SGeorge Wilson } 392854d692b7SGeorge Wilson } 392954d692b7SGeorge Wilson 3930a3f829aeSBill Moore if ((pio = zio_unique_parent(zio)) != NULL) { 3931e14bb325SJeff Bonwick /* 3932e14bb325SJeff Bonwick * We're not a root i/o, so there's nothing to do 3933e14bb325SJeff Bonwick * but notify our parent. Don't propagate errors 3934e14bb325SJeff Bonwick * upward since we haven't permanently failed yet. 3935e14bb325SJeff Bonwick */ 393633a372edSGeorge Wilson ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER)); 3937e14bb325SJeff Bonwick zio->io_flags |= ZIO_FLAG_DONT_PROPAGATE; 3938e14bb325SJeff Bonwick zio_notify_parent(pio, zio, ZIO_WAIT_DONE); 3939e14bb325SJeff Bonwick } else if (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND) { 3940e14bb325SJeff Bonwick /* 3941e14bb325SJeff Bonwick * We'd fail again if we reexecuted now, so suspend 3942e14bb325SJeff Bonwick * until conditions improve (e.g. device comes online). 3943e14bb325SJeff Bonwick */ 3944e0f1c0afSOlaf Faaland zio_suspend(zio->io_spa, zio, ZIO_SUSPEND_IOERR); 3945e14bb325SJeff Bonwick } else { 3946e14bb325SJeff Bonwick /* 3947e14bb325SJeff Bonwick * Reexecution is potentially a huge amount of work. 3948e14bb325SJeff Bonwick * Hand it off to the otherwise-unused claim taskq. 3949e14bb325SJeff Bonwick */ 39505aeb9474SGarrett D'Amore ASSERT(zio->io_tqent.tqent_next == NULL); 3951ec94d322SAdam Leventhal spa_taskq_dispatch_ent(spa, ZIO_TYPE_CLAIM, 3952ec94d322SAdam Leventhal ZIO_TASKQ_ISSUE, (task_func_t *)zio_reexecute, zio, 3953ec94d322SAdam Leventhal 0, &zio->io_tqent); 3954e14bb325SJeff Bonwick } 3955e14bb325SJeff Bonwick return (ZIO_PIPELINE_STOP); 3956fa9e4066Sahrens } 3957fa9e4066Sahrens 3958b24ab676SJeff Bonwick ASSERT(zio->io_child_count == 0); 395933a372edSGeorge Wilson ASSERT(zio->io_reexecute == 0); 3960e14bb325SJeff Bonwick ASSERT(zio->io_error == 0 || (zio->io_flags & ZIO_FLAG_CANFAIL)); 3961fa9e4066Sahrens 3962b24ab676SJeff Bonwick /* 3963b24ab676SJeff Bonwick * Report any checksum errors, since the I/O is complete. 3964b24ab676SJeff Bonwick */ 396522fe2c88SJonathan Adams while (zio->io_cksum_report != NULL) { 3966b24ab676SJeff Bonwick zio_cksum_report_t *zcr = zio->io_cksum_report; 3967b24ab676SJeff Bonwick zio->io_cksum_report = zcr->zcr_next; 3968b24ab676SJeff Bonwick zcr->zcr_next = NULL; 3969b24ab676SJeff Bonwick zcr->zcr_finish(zcr, NULL); 3970b24ab676SJeff Bonwick zfs_ereport_free_checksum(zcr); 397122fe2c88SJonathan Adams } 397222fe2c88SJonathan Adams 3973a3f829aeSBill Moore /* 3974a3f829aeSBill Moore * It is the responsibility of the done callback to ensure that this 3975a3f829aeSBill Moore * particular zio is no longer discoverable for adoption, and as 3976a3f829aeSBill Moore * such, cannot acquire any new parents. 3977a3f829aeSBill Moore */ 3978e14bb325SJeff Bonwick if (zio->io_done) 3979e14bb325SJeff Bonwick zio->io_done(zio); 3980fa9e4066Sahrens 3981a3f829aeSBill Moore mutex_enter(&zio->io_lock); 3982a3f829aeSBill Moore zio->io_state[ZIO_WAIT_DONE] = 1; 3983a3f829aeSBill Moore mutex_exit(&zio->io_lock); 3984fa9e4066Sahrens 39850f7643c7SGeorge Wilson zl = NULL; 39860f7643c7SGeorge Wilson for (pio = zio_walk_parents(zio, &zl); pio != NULL; pio = pio_next) { 39870f7643c7SGeorge Wilson zio_link_t *remove_zl = zl; 39880f7643c7SGeorge Wilson pio_next = zio_walk_parents(zio, &zl); 39890f7643c7SGeorge Wilson zio_remove_child(pio, zio, remove_zl); 3990e14bb325SJeff Bonwick zio_notify_parent(pio, zio, ZIO_WAIT_DONE); 3991e14bb325SJeff Bonwick } 3992fa9e4066Sahrens 3993e14bb325SJeff Bonwick if (zio->io_waiter != NULL) { 3994e14bb325SJeff Bonwick mutex_enter(&zio->io_lock); 3995e14bb325SJeff Bonwick zio->io_executor = NULL; 3996e14bb325SJeff Bonwick cv_broadcast(&zio->io_cv); 3997e14bb325SJeff Bonwick mutex_exit(&zio->io_lock); 3998e14bb325SJeff Bonwick } else { 3999e14bb325SJeff Bonwick zio_destroy(zio); 4000e14bb325SJeff Bonwick } 4001fa9e4066Sahrens 4002e14bb325SJeff Bonwick return (ZIO_PIPELINE_STOP); 4003fa9e4066Sahrens } 400446341222Sperrin 400546341222Sperrin /* 4006e14bb325SJeff Bonwick * ========================================================================== 4007e14bb325SJeff Bonwick * I/O pipeline definition 4008e14bb325SJeff Bonwick * ========================================================================== 400946341222Sperrin */ 4010b24ab676SJeff Bonwick static zio_pipe_stage_t *zio_pipeline[] = { 4011e14bb325SJeff Bonwick NULL, 4012e14bb325SJeff Bonwick zio_read_bp_init, 40130f7643c7SGeorge Wilson zio_write_bp_init, 4014b24ab676SJeff Bonwick zio_free_bp_init, 4015b24ab676SJeff Bonwick zio_issue_async, 40160f7643c7SGeorge Wilson zio_write_compress, 4017e14bb325SJeff Bonwick zio_checksum_generate, 401880901aeaSGeorge Wilson zio_nop_write, 4019b24ab676SJeff Bonwick zio_ddt_read_start, 4020b24ab676SJeff Bonwick zio_ddt_read_done, 4021b24ab676SJeff Bonwick zio_ddt_write, 4022b24ab676SJeff Bonwick zio_ddt_free, 4023e14bb325SJeff Bonwick zio_gang_assemble, 4024e14bb325SJeff Bonwick zio_gang_issue, 40250f7643c7SGeorge Wilson zio_dva_throttle, 4026e14bb325SJeff Bonwick zio_dva_allocate, 4027e14bb325SJeff Bonwick zio_dva_free, 4028e14bb325SJeff Bonwick zio_dva_claim, 4029e14bb325SJeff Bonwick zio_ready, 4030e14bb325SJeff Bonwick zio_vdev_io_start, 4031e14bb325SJeff Bonwick zio_vdev_io_done, 4032e14bb325SJeff Bonwick zio_vdev_io_assess, 4033e14bb325SJeff Bonwick zio_checksum_verify, 4034e14bb325SJeff Bonwick zio_done 4035e14bb325SJeff Bonwick }; 4036ad135b5dSChristopher Siden 4037ad135b5dSChristopher Siden 4038ad135b5dSChristopher Siden 4039ad135b5dSChristopher Siden 4040a2cdcdd2SPaul Dagnelie /* 4041a2cdcdd2SPaul Dagnelie * Compare two zbookmark_phys_t's to see which we would reach first in a 4042a2cdcdd2SPaul Dagnelie * pre-order traversal of the object tree. 4043a2cdcdd2SPaul Dagnelie * 4044a2cdcdd2SPaul Dagnelie * This is simple in every case aside from the meta-dnode object. For all other 4045a2cdcdd2SPaul Dagnelie * objects, we traverse them in order (object 1 before object 2, and so on). 4046a2cdcdd2SPaul Dagnelie * However, all of these objects are traversed while traversing object 0, since 4047a2cdcdd2SPaul Dagnelie * the data it points to is the list of objects. Thus, we need to convert to a 4048a2cdcdd2SPaul Dagnelie * canonical representation so we can compare meta-dnode bookmarks to 4049a2cdcdd2SPaul Dagnelie * non-meta-dnode bookmarks. 4050a2cdcdd2SPaul Dagnelie * 4051a2cdcdd2SPaul Dagnelie * We do this by calculating "equivalents" for each field of the zbookmark. 4052a2cdcdd2SPaul Dagnelie * zbookmarks outside of the meta-dnode use their own object and level, and 4053a2cdcdd2SPaul Dagnelie * calculate the level 0 equivalent (the first L0 blkid that is contained in the 4054a2cdcdd2SPaul Dagnelie * blocks this bookmark refers to) by multiplying their blkid by their span 4055a2cdcdd2SPaul Dagnelie * (the number of L0 blocks contained within one block at their level). 4056a2cdcdd2SPaul Dagnelie * zbookmarks inside the meta-dnode calculate their object equivalent 4057a2cdcdd2SPaul Dagnelie * (which is L0equiv * dnodes per data block), use 0 for their L0equiv, and use 4058a2cdcdd2SPaul Dagnelie * level + 1<<31 (any value larger than a level could ever be) for their level. 4059a2cdcdd2SPaul Dagnelie * This causes them to always compare before a bookmark in their object 4060a2cdcdd2SPaul Dagnelie * equivalent, compare appropriately to bookmarks in other objects, and to 4061a2cdcdd2SPaul Dagnelie * compare appropriately to other bookmarks in the meta-dnode. 4062a2cdcdd2SPaul Dagnelie */ 4063a2cdcdd2SPaul Dagnelie int 4064a2cdcdd2SPaul Dagnelie zbookmark_compare(uint16_t dbss1, uint8_t ibs1, uint16_t dbss2, uint8_t ibs2, 4065a2cdcdd2SPaul Dagnelie const zbookmark_phys_t *zb1, const zbookmark_phys_t *zb2) 4066a2cdcdd2SPaul Dagnelie { 4067a2cdcdd2SPaul Dagnelie /* 4068a2cdcdd2SPaul Dagnelie * These variables represent the "equivalent" values for the zbookmark, 4069a2cdcdd2SPaul Dagnelie * after converting zbookmarks inside the meta dnode to their 4070a2cdcdd2SPaul Dagnelie * normal-object equivalents. 4071a2cdcdd2SPaul Dagnelie */ 4072a2cdcdd2SPaul Dagnelie uint64_t zb1obj, zb2obj; 4073a2cdcdd2SPaul Dagnelie uint64_t zb1L0, zb2L0; 4074a2cdcdd2SPaul Dagnelie uint64_t zb1level, zb2level; 4075ad135b5dSChristopher Siden 4076a2cdcdd2SPaul Dagnelie if (zb1->zb_object == zb2->zb_object && 4077a2cdcdd2SPaul Dagnelie zb1->zb_level == zb2->zb_level && 4078a2cdcdd2SPaul Dagnelie zb1->zb_blkid == zb2->zb_blkid) 4079a2cdcdd2SPaul Dagnelie return (0); 4080a2cdcdd2SPaul Dagnelie 4081a2cdcdd2SPaul Dagnelie /* 4082a2cdcdd2SPaul Dagnelie * BP_SPANB calculates the span in blocks. 4083a2cdcdd2SPaul Dagnelie */ 4084a2cdcdd2SPaul Dagnelie zb1L0 = (zb1->zb_blkid) * BP_SPANB(ibs1, zb1->zb_level); 4085a2cdcdd2SPaul Dagnelie zb2L0 = (zb2->zb_blkid) * BP_SPANB(ibs2, zb2->zb_level); 4086ad135b5dSChristopher Siden 4087ad135b5dSChristopher Siden if (zb1->zb_object == DMU_META_DNODE_OBJECT) { 4088a2cdcdd2SPaul Dagnelie zb1obj = zb1L0 * (dbss1 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT)); 4089a2cdcdd2SPaul Dagnelie zb1L0 = 0; 4090a2cdcdd2SPaul Dagnelie zb1level = zb1->zb_level + COMPARE_META_LEVEL; 4091a2cdcdd2SPaul Dagnelie } else { 4092a2cdcdd2SPaul Dagnelie zb1obj = zb1->zb_object; 4093a2cdcdd2SPaul Dagnelie zb1level = zb1->zb_level; 4094ad135b5dSChristopher Siden } 4095ad135b5dSChristopher Siden 4096a2cdcdd2SPaul Dagnelie if (zb2->zb_object == DMU_META_DNODE_OBJECT) { 4097a2cdcdd2SPaul Dagnelie zb2obj = zb2L0 * (dbss2 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT)); 4098a2cdcdd2SPaul Dagnelie zb2L0 = 0; 4099a2cdcdd2SPaul Dagnelie zb2level = zb2->zb_level + COMPARE_META_LEVEL; 4100a2cdcdd2SPaul Dagnelie } else { 4101a2cdcdd2SPaul Dagnelie zb2obj = zb2->zb_object; 4102a2cdcdd2SPaul Dagnelie zb2level = zb2->zb_level; 4103a2cdcdd2SPaul Dagnelie } 4104a2cdcdd2SPaul Dagnelie 4105a2cdcdd2SPaul Dagnelie /* Now that we have a canonical representation, do the comparison. */ 4106a2cdcdd2SPaul Dagnelie if (zb1obj != zb2obj) 4107a2cdcdd2SPaul Dagnelie return (zb1obj < zb2obj ? -1 : 1); 4108a2cdcdd2SPaul Dagnelie else if (zb1L0 != zb2L0) 4109a2cdcdd2SPaul Dagnelie return (zb1L0 < zb2L0 ? -1 : 1); 4110a2cdcdd2SPaul Dagnelie else if (zb1level != zb2level) 4111a2cdcdd2SPaul Dagnelie return (zb1level > zb2level ? -1 : 1); 4112a2cdcdd2SPaul Dagnelie /* 4113a2cdcdd2SPaul Dagnelie * This can (theoretically) happen if the bookmarks have the same object 4114a2cdcdd2SPaul Dagnelie * and level, but different blkids, if the block sizes are not the same. 4115a2cdcdd2SPaul Dagnelie * There is presently no way to change the indirect block sizes 4116a2cdcdd2SPaul Dagnelie */ 4117a2cdcdd2SPaul Dagnelie return (0); 4118a2cdcdd2SPaul Dagnelie } 4119a2cdcdd2SPaul Dagnelie 4120a2cdcdd2SPaul Dagnelie /* 4121a2cdcdd2SPaul Dagnelie * This function checks the following: given that last_block is the place that 4122a2cdcdd2SPaul Dagnelie * our traversal stopped last time, does that guarantee that we've visited 4123a2cdcdd2SPaul Dagnelie * every node under subtree_root? Therefore, we can't just use the raw output 4124a2cdcdd2SPaul Dagnelie * of zbookmark_compare. We have to pass in a modified version of 4125a2cdcdd2SPaul Dagnelie * subtree_root; by incrementing the block id, and then checking whether 4126a2cdcdd2SPaul Dagnelie * last_block is before or equal to that, we can tell whether or not having 4127a2cdcdd2SPaul Dagnelie * visited last_block implies that all of subtree_root's children have been 4128a2cdcdd2SPaul Dagnelie * visited. 4129a2cdcdd2SPaul Dagnelie */ 4130a2cdcdd2SPaul Dagnelie boolean_t 4131a2cdcdd2SPaul Dagnelie zbookmark_subtree_completed(const dnode_phys_t *dnp, 4132a2cdcdd2SPaul Dagnelie const zbookmark_phys_t *subtree_root, const zbookmark_phys_t *last_block) 4133a2cdcdd2SPaul Dagnelie { 4134a2cdcdd2SPaul Dagnelie zbookmark_phys_t mod_zb = *subtree_root; 4135a2cdcdd2SPaul Dagnelie mod_zb.zb_blkid++; 4136a2cdcdd2SPaul Dagnelie ASSERT(last_block->zb_level == 0); 4137a2cdcdd2SPaul Dagnelie 4138a2cdcdd2SPaul Dagnelie /* The objset_phys_t isn't before anything. */ 4139a2cdcdd2SPaul Dagnelie if (dnp == NULL) 4140ad135b5dSChristopher Siden return (B_FALSE); 4141a2cdcdd2SPaul Dagnelie 4142a2cdcdd2SPaul Dagnelie /* 4143a2cdcdd2SPaul Dagnelie * We pass in 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT) for the 4144a2cdcdd2SPaul Dagnelie * data block size in sectors, because that variable is only used if 4145a2cdcdd2SPaul Dagnelie * the bookmark refers to a block in the meta-dnode. Since we don't 4146a2cdcdd2SPaul Dagnelie * know without examining it what object it refers to, and there's no 4147a2cdcdd2SPaul Dagnelie * harm in passing in this value in other cases, we always pass it in. 4148a2cdcdd2SPaul Dagnelie * 4149a2cdcdd2SPaul Dagnelie * We pass in 0 for the indirect block size shift because zb2 must be 4150a2cdcdd2SPaul Dagnelie * level 0. The indirect block size is only used to calculate the span 4151a2cdcdd2SPaul Dagnelie * of the bookmark, but since the bookmark must be level 0, the span is 4152a2cdcdd2SPaul Dagnelie * always 1, so the math works out. 4153a2cdcdd2SPaul Dagnelie * 4154a2cdcdd2SPaul Dagnelie * If you make changes to how the zbookmark_compare code works, be sure 4155a2cdcdd2SPaul Dagnelie * to make sure that this code still works afterwards. 4156a2cdcdd2SPaul Dagnelie */ 4157a2cdcdd2SPaul Dagnelie return (zbookmark_compare(dnp->dn_datablkszsec, dnp->dn_indblkshift, 4158a2cdcdd2SPaul Dagnelie 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT), 0, &mod_zb, 4159a2cdcdd2SPaul Dagnelie last_block) <= 0); 4160ad135b5dSChristopher Siden } 4161