fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER START
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * The contents of this file are subject to the terms of the
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * Common Development and Distribution License (the "License").
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * You may not use this file except in compliance with the License.
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fa9e4066f08beec538e775443c5be79dd423fcabahrens * or http://www.opensolaris.org/os/licensing.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * See the License for the specific language governing permissions
fa9e4066f08beec538e775443c5be79dd423fcabahrens * and limitations under the License.
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * When distributing Covered Code, include this CDDL HEADER in each
fa9e4066f08beec538e775443c5be79dd423fcabahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If applicable, add the following below this CDDL HEADER, with the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * fields enclosed by brackets "[]" replaced with your own identifying
fa9e4066f08beec538e775443c5be79dd423fcabahrens * information: Portions Copyright [yyyy] [name of copyright owner]
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER END
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
8df0bcf0df7622a075cc6e52f659d2fcfdd08cdcPaul Dagnelie * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
5aeb94743e3be0c51e86f73096334611ae3a058eGarrett D'Amore * Copyright (c) 2011 Nexenta Systems, Inc. All rights reserved.
c3d26abc9ee97b4f60233556aadeb57e0bd30bb9Matthew Ahrens * Copyright (c) 2014 Integros [integros.com]
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrens
de710d24d2fae4468e64da999e1d952a247f142cJosef 'Jeff' Sipek#include <sys/sysmacros.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/zfs_context.h>
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock#include <sys/fm/fs/zfs.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/spa.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/txg.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/spa_impl.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/vdev_impl.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/zio_impl.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/zio_compress.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/zio_checksum.h>
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick#include <sys/dmu_objset.h>
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick#include <sys/arc.h>
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick#include <sys/ddt.h>
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens#include <sys/blkptr.h>
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman#include <sys/zfeature.h>
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson#include <sys/metaslab_impl.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
fa9e4066f08beec538e775443c5be79dd423fcabahrens * I/O type descriptions
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensconst char *zio_type_name[ZIO_TYPES] = {
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson "zio_null", "zio_read", "zio_write", "zio_free", "zio_claim",
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson "zio_ioctl"
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson};
fa9e4066f08beec538e775443c5be79dd423fcabahrens
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonboolean_t zio_dva_throttle_enabled = B_TRUE;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
fa9e4066f08beec538e775443c5be79dd423fcabahrens * I/O kmem caches
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
ccae0b50330edda9b094cee1ec6a0ad35443e8b0eschrockkmem_cache_t *zio_cache;
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moorekmem_cache_t *zio_link_cache;
fa9e4066f08beec538e775443c5be79dd423fcabahrenskmem_cache_t *zio_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansenkmem_cache_t *zio_data_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen#ifdef _KERNEL
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansenextern vmem_t *zio_alloc_arena;
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen#endif
fa9e4066f08beec538e775443c5be79dd423fcabahrens
738f37bc3dcd61e8a893af0f2d466d76690b70ecGeorge Wilson#define ZIO_PIPELINE_CONTINUE 0x100
738f37bc3dcd61e8a893af0f2d466d76690b70ecGeorge Wilson#define ZIO_PIPELINE_STOP 0x101
738f37bc3dcd61e8a893af0f2d466d76690b70ecGeorge Wilson
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie#define BP_SPANB(indblkshift, level) \
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie (((uint64_t)1) << ((level) * ((indblkshift) - SPA_BLKPTRSHIFT)))
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie#define COMPARE_META_LEVEL 0x80000000ul
01f55e48fb4d524eaf70687728aa51b7762e2e97George Wilson/*
01f55e48fb4d524eaf70687728aa51b7762e2e97George Wilson * The following actions directly effect the spa's sync-to-convergence logic.
01f55e48fb4d524eaf70687728aa51b7762e2e97George Wilson * The values below define the sync pass when we start performing the action.
01f55e48fb4d524eaf70687728aa51b7762e2e97George Wilson * Care should be taken when changing these values as they directly impact
01f55e48fb4d524eaf70687728aa51b7762e2e97George Wilson * spa_sync() performance. Tuning these values may introduce subtle performance
01f55e48fb4d524eaf70687728aa51b7762e2e97George Wilson * pathologies and should only be done in the context of performance analysis.
01f55e48fb4d524eaf70687728aa51b7762e2e97George Wilson * These tunables will eventually be removed and replaced with #defines once
01f55e48fb4d524eaf70687728aa51b7762e2e97George Wilson * enough analysis has been done to determine optimal values.
01f55e48fb4d524eaf70687728aa51b7762e2e97George Wilson *
01f55e48fb4d524eaf70687728aa51b7762e2e97George Wilson * The 'zfs_sync_pass_deferred_free' pass must be greater than 1 to ensure that
01f55e48fb4d524eaf70687728aa51b7762e2e97George Wilson * regular blocks are not deferred.
01f55e48fb4d524eaf70687728aa51b7762e2e97George Wilson */
01f55e48fb4d524eaf70687728aa51b7762e2e97George Wilsonint zfs_sync_pass_deferred_free = 2; /* defer frees starting in this pass */
01f55e48fb4d524eaf70687728aa51b7762e2e97George Wilsonint zfs_sync_pass_dont_compress = 5; /* don't compress starting in this pass */
01f55e48fb4d524eaf70687728aa51b7762e2e97George Wilsonint zfs_sync_pass_rewrite = 2; /* rewrite new bps starting in this pass */
01f55e48fb4d524eaf70687728aa51b7762e2e97George Wilson
0a4e9518a44f226be6d39383330b5b1792d2f184gw/*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * An allocating zio is one that either currently has the DVA allocate
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * stage set or will have it later in its lifetime.
0a4e9518a44f226be6d39383330b5b1792d2f184gw */
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick#define IO_IS_ALLOCATING(zio) ((zio)->io_orig_pipeline & ZIO_STAGE_DVA_ALLOCATE)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
35a5a3587fd94b666239c157d3722745250ccbd7Jonathan Adamsboolean_t zio_requeue_io_start_cut_in_line = B_TRUE;
35a5a3587fd94b666239c157d3722745250ccbd7Jonathan Adams
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick#ifdef ZFS_DEBUG
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickint zio_buf_debug_limit = 16384;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick#else
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickint zio_buf_debug_limit = 0;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick#endif
0a4e9518a44f226be6d39383330b5b1792d2f184gw
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonstatic void zio_taskq_dispatch(zio_t *, zio_taskq_type_t, boolean_t);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid
fa9e4066f08beec538e775443c5be79dd423fcabahrenszio_init(void)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens size_t c;
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen vmem_t *data_alloc_arena = NULL;
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen#ifdef _KERNEL
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen data_alloc_arena = zio_alloc_arena;
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen#endif
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore zio_cache = kmem_cache_create("zio_cache",
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore sizeof (zio_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore zio_link_cache = kmem_cache_create("zio_link_cache",
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore sizeof (zio_link_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
ccae0b50330edda9b094cee1ec6a0ad35443e8b0eschrock
fa9e4066f08beec538e775443c5be79dd423fcabahrens /*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * For small buffers, we want a cache for each multiple of
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens * SPA_MINBLOCKSIZE. For larger buffers, we want a cache
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens * for each quarter-power of 2.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrens for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens size_t size = (c + 1) << SPA_MINBLOCKSHIFT;
fa9e4066f08beec538e775443c5be79dd423fcabahrens size_t p2 = size;
fa9e4066f08beec538e775443c5be79dd423fcabahrens size_t align = 0;
e291592ab12a560fc73b0610963bb3fe66aab341Jonathan Adams size_t cflags = (size > zio_buf_debug_limit) ? KMC_NODEBUG : 0;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
de710d24d2fae4468e64da999e1d952a247f142cJosef 'Jeff' Sipek while (!ISP2(p2))
fa9e4066f08beec538e775443c5be79dd423fcabahrens p2 &= p2 - 1;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
cd1c8b85eb30b568e9816221430c479ace7a559dMatthew Ahrens#ifndef _KERNEL
cd1c8b85eb30b568e9816221430c479ace7a559dMatthew Ahrens /*
cd1c8b85eb30b568e9816221430c479ace7a559dMatthew Ahrens * If we are using watchpoints, put each buffer on its own page,
cd1c8b85eb30b568e9816221430c479ace7a559dMatthew Ahrens * to eliminate the performance overhead of trapping to the
cd1c8b85eb30b568e9816221430c479ace7a559dMatthew Ahrens * kernel when modifying a non-watched buffer that shares the
cd1c8b85eb30b568e9816221430c479ace7a559dMatthew Ahrens * page with a watched buffer.
cd1c8b85eb30b568e9816221430c479ace7a559dMatthew Ahrens */
cd1c8b85eb30b568e9816221430c479ace7a559dMatthew Ahrens if (arc_watch && !IS_P2ALIGNED(size, PAGESIZE))
cd1c8b85eb30b568e9816221430c479ace7a559dMatthew Ahrens continue;
cd1c8b85eb30b568e9816221430c479ace7a559dMatthew Ahrens#endif
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (size <= 4 * SPA_MINBLOCKSIZE) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens align = SPA_MINBLOCKSIZE;
cd1c8b85eb30b568e9816221430c479ace7a559dMatthew Ahrens } else if (IS_P2ALIGNED(size, p2 >> 2)) {
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens align = MIN(p2 >> 2, PAGESIZE);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (align != 0) {
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen char name[36];
5ad820458efd0fdb914baff9c1447c22b819fa23nd (void) sprintf(name, "zio_buf_%lu", (ulong_t)size);
fa9e4066f08beec538e775443c5be79dd423fcabahrens zio_buf_cache[c] = kmem_cache_create(name, size,
e291592ab12a560fc73b0610963bb3fe66aab341Jonathan Adams align, NULL, NULL, NULL, NULL, NULL, cflags);
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen
e291592ab12a560fc73b0610963bb3fe66aab341Jonathan Adams /*
e291592ab12a560fc73b0610963bb3fe66aab341Jonathan Adams * Since zio_data bufs do not appear in crash dumps, we
e291592ab12a560fc73b0610963bb3fe66aab341Jonathan Adams * pass KMC_NOTOUCH so that no allocator metadata is
e291592ab12a560fc73b0610963bb3fe66aab341Jonathan Adams * stored with the buffers.
e291592ab12a560fc73b0610963bb3fe66aab341Jonathan Adams */
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen (void) sprintf(name, "zio_data_buf_%lu", (ulong_t)size);
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen zio_data_buf_cache[c] = kmem_cache_create(name, size,
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen align, NULL, NULL, NULL, NULL, data_alloc_arena,
e291592ab12a560fc73b0610963bb3fe66aab341Jonathan Adams cflags | KMC_NOTOUCH);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens while (--c != 0) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT(zio_buf_cache[c] != NULL);
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (zio_buf_cache[c - 1] == NULL)
fa9e4066f08beec538e775443c5be79dd423fcabahrens zio_buf_cache[c - 1] = zio_buf_cache[c];
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen ASSERT(zio_data_buf_cache[c] != NULL);
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen if (zio_data_buf_cache[c - 1] == NULL)
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen zio_data_buf_cache[c - 1] = zio_data_buf_cache[c];
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock zio_inject_init();
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid
fa9e4066f08beec538e775443c5be79dd423fcabahrenszio_fini(void)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens size_t c;
fa9e4066f08beec538e775443c5be79dd423fcabahrens kmem_cache_t *last_cache = NULL;
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen kmem_cache_t *last_data_cache = NULL;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (zio_buf_cache[c] != last_cache) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens last_cache = zio_buf_cache[c];
fa9e4066f08beec538e775443c5be79dd423fcabahrens kmem_cache_destroy(zio_buf_cache[c]);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens zio_buf_cache[c] = NULL;
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen if (zio_data_buf_cache[c] != last_data_cache) {
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen last_data_cache = zio_data_buf_cache[c];
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen kmem_cache_destroy(zio_data_buf_cache[c]);
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen }
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen zio_data_buf_cache[c] = NULL;
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore kmem_cache_destroy(zio_link_cache);
ccae0b50330edda9b094cee1ec6a0ad35443e8b0eschrock kmem_cache_destroy(zio_cache);
ccae0b50330edda9b094cee1ec6a0ad35443e8b0eschrock
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock zio_inject_fini();
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Allocate and free I/O buffers
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen/*
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen * Use zio_buf_alloc to allocate ZFS metadata. This data will appear in a
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen * crashdump if the kernel panics, so use it judiciously. Obviously, it's
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen * useful to inspect ZFS metadata, but if possible, we should avoid keeping
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen * excess / transient data in-core during a crashdump.
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen */
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid *
fa9e4066f08beec538e775443c5be79dd423fcabahrenszio_buf_alloc(size_t size)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee return (kmem_cache_alloc(zio_buf_cache[c], KM_PUSHPAGE));
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen/*
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen * Use zio_data_buf_alloc to allocate data. The data will not appear in a
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen * crashdump if the kernel panics. This exists so that we will limit the amount
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen * of ZFS data that shows up in a kernel crashdump. (Thus reducing the amount
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen * of kernel heap dumped to disk when the kernel panics)
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen */
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansenvoid *
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansenzio_data_buf_alloc(size_t size)
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen{
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen
1ab7f2ded02e7a1bc3c73516eb27efa79bf2a2ffmaybee return (kmem_cache_alloc(zio_data_buf_cache[c], KM_PUSHPAGE));
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen}
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid
fa9e4066f08beec538e775443c5be79dd423fcabahrenszio_buf_free(void *buf, size_t size)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens kmem_cache_free(zio_buf_cache[c], buf);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansenvoid
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansenzio_data_buf_free(void *buf, size_t size)
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen{
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen kmem_cache_free(zio_data_buf_cache[c], buf);
ad23a2db4cfc94c0ed1d58554479ce8d2e7e5768johansen}
b3995adb6d3bcb24a1d4e737d5cfd6773f6df99aahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Push and pop I/O transform buffers
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
dcbf3bd6a1f1360fc1afcee9e22c6dcff7844bf2George Wilsonvoid
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_push_transform(zio_t *zio, void *data, uint64_t size, uint64_t bufsize,
9a686fbc186e8e2a64e9a5094d44c7d6fa0ea167Paul Dagnelie zio_transform_func_t *transform)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens zio_transform_t *zt = kmem_alloc(sizeof (zio_transform_t), KM_SLEEP);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zt->zt_orig_data = zio->io_data;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zt->zt_orig_size = zio->io_size;
fa9e4066f08beec538e775443c5be79dd423fcabahrens zt->zt_bufsize = bufsize;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zt->zt_transform = transform;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens zt->zt_next = zio->io_transform_stack;
fa9e4066f08beec538e775443c5be79dd423fcabahrens zio->io_transform_stack = zt;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens zio->io_data = data;
fa9e4066f08beec538e775443c5be79dd423fcabahrens zio->io_size = size;
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
dcbf3bd6a1f1360fc1afcee9e22c6dcff7844bf2George Wilsonvoid
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_pop_transforms(zio_t *zio)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_transform_t *zt;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick while ((zt = zio->io_transform_stack) != NULL) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zt->zt_transform != NULL)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zt->zt_transform(zio,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zt->zt_orig_data, zt->zt_orig_size);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (zt->zt_bufsize != 0)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_buf_free(zio->io_data, zt->zt_bufsize);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_data = zt->zt_orig_data;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_size = zt->zt_orig_size;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_transform_stack = zt->zt_next;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick kmem_free(zt, sizeof (zio_transform_t));
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick/*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * ==========================================================================
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * I/O transform callbacks for subblocks and decompression
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * ==========================================================================
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickstatic void
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_subblock(zio_t *zio, void *data, uint64_t size)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(zio->io_size > size);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zio->io_type == ZIO_TYPE_READ)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick bcopy(zio->io_data, data, size);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick}
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickstatic void
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_decompress(zio_t *zio, void *data, uint64_t size)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zio->io_error == 0 &&
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_decompress_data(BP_GET_COMPRESS(zio->io_bp),
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_data, data, zio->io_size, size) != 0)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens zio->io_error = SET_ERROR(EIO);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick}
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick/*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * ==========================================================================
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * I/O parent/child relationships and pipeline interlocks
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * ==========================================================================
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick */
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moorezio_t *
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonzio_walk_parents(zio_t *cio, zio_link_t **zl)
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore{
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore list_t *pl = &cio->io_parent_list;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson *zl = (*zl == NULL) ? list_head(pl) : list_next(pl, *zl);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (*zl == NULL)
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore return (NULL);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT((*zl)->zl_child == cio);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return ((*zl)->zl_parent);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore}
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moorezio_t *
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonzio_walk_children(zio_t *pio, zio_link_t **zl)
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore{
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore list_t *cl = &pio->io_child_list;
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson *zl = (*zl == NULL) ? list_head(cl) : list_next(cl, *zl);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (*zl == NULL)
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore return (NULL);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT((*zl)->zl_parent == pio);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return ((*zl)->zl_child);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore}
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moorezio_t *
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moorezio_unique_parent(zio_t *cio)
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore{
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio_link_t *zl = NULL;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio_t *pio = zio_walk_parents(cio, &zl);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson VERIFY3P(zio_walk_parents(cio, &zl), ==, NULL);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore return (pio);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore}
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moorevoid
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moorezio_add_child(zio_t *pio, zio_t *cio)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick{
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore zio_link_t *zl = kmem_cache_alloc(zio_link_cache, KM_SLEEP);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore /*
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore * Logical I/Os can have logical, gang, or vdev children.
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore * Gang I/Os can have gang or vdev children.
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore * Vdev I/Os can only have vdev children.
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore * The following ASSERT captures all of these constraints.
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore */
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore ASSERT(cio->io_child_type <= pio->io_child_type);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore zl->zl_parent = pio;
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore zl->zl_child = cio;
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore mutex_enter(&cio->io_lock);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick mutex_enter(&pio->io_lock);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore ASSERT(pio->io_state[ZIO_WAIT_DONE] == 0);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore for (int w = 0; w < ZIO_WAIT_TYPES; w++)
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore pio->io_children[cio->io_child_type][w] += !cio->io_state[w];
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore list_insert_head(&pio->io_child_list, zl);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore list_insert_head(&cio->io_parent_list, zl);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick pio->io_child_count++;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick cio->io_parent_count++;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick mutex_exit(&pio->io_lock);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore mutex_exit(&cio->io_lock);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick}
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
fa9e4066f08beec538e775443c5be79dd423fcabahrensstatic void
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moorezio_remove_child(zio_t *pio, zio_t *cio, zio_link_t *zl)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick{
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore ASSERT(zl->zl_parent == pio);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore ASSERT(zl->zl_child == cio);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore mutex_enter(&cio->io_lock);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick mutex_enter(&pio->io_lock);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore list_remove(&pio->io_child_list, zl);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore list_remove(&cio->io_parent_list, zl);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick pio->io_child_count--;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick cio->io_parent_count--;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick mutex_exit(&pio->io_lock);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore mutex_exit(&cio->io_lock);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore kmem_cache_free(zio_link_cache, zl);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick}
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickstatic boolean_t
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_wait_for_children(zio_t *zio, enum zio_child child, enum zio_wait_type wait)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick uint64_t *countp = &zio->io_children[child][wait];
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick boolean_t waiting = B_FALSE;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick mutex_enter(&zio->io_lock);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(zio->io_stall == NULL);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (*countp != 0) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_stage >>= 1;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT3U(zio->io_stage, !=, ZIO_STAGE_OPEN);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_stall = countp;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick waiting = B_TRUE;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick mutex_exit(&zio->io_lock);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return (waiting);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickstatic void
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_notify_parent(zio_t *pio, zio_t *zio, enum zio_wait_type wait)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick uint64_t *countp = &pio->io_children[zio->io_child_type][wait];
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick int *errorp = &pio->io_child_error[zio->io_child_type];
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick mutex_enter(&pio->io_lock);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zio->io_error && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick *errorp = zio_worst_error(*errorp, zio->io_error);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick pio->io_reexecute |= zio->io_reexecute;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT3U(*countp, >, 0);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens (*countp)--;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (*countp == 0 && pio->io_stall == countp) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio_taskq_type_t type =
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson pio->io_stage < ZIO_STAGE_VDEV_IO_START ? ZIO_TASKQ_ISSUE :
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ZIO_TASKQ_INTERRUPT;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick pio->io_stall = NULL;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick mutex_exit(&pio->io_lock);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson /*
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * Dispatch the parent zio in its own taskq so that
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * the child can continue to make progress. This also
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * prevents overflowing the stack when we have deeply nested
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * parent-child relationships.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio_taskq_dispatch(pio, type, B_FALSE);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick } else {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick mutex_exit(&pio->io_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickstatic void
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_inherit_child_errors(zio_t *zio, enum zio_child c)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zio->io_child_error[c] != 0 && zio->io_error == 0)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_error = zio->io_child_error[c];
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick}
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonint
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonzio_timestamp_compare(const void *x1, const void *x2)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson{
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson const zio_t *z1 = x1;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson const zio_t *z2 = x2;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (z1->io_queued_timestamp < z2->io_queued_timestamp)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return (-1);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (z1->io_queued_timestamp > z2->io_queued_timestamp)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return (1);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (z1->io_offset < z2->io_offset)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return (-1);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (z1->io_offset > z2->io_offset)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return (1);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (z1 < z2)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return (-1);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (z1 > z2)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return (1);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return (0);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson}
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * Create the various types of I/O (read, write, free, etc)
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrensstatic zio_t *
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickzio_create(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel void *data, uint64_t lsize, uint64_t psize, zio_done_func_t *done,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel void *private, zio_type_t type, zio_priority_t priority,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel enum zio_flag flags, vdev_t *vd, uint64_t offset,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel const zbookmark_phys_t *zb, enum zio_stage stage, enum zio_stage pipeline)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens zio_t *zio;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel ASSERT3U(psize, <=, SPA_MAXBLOCKSIZE);
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel ASSERT(P2PHASE(psize, SPA_MINBLOCKSIZE) == 0);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(P2PHASE(offset, SPA_MINBLOCKSIZE) == 0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(!vd || spa_config_held(spa, SCL_STATE_ALL, RW_READER));
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(!bp || !(flags & ZIO_FLAG_CONFIG_WRITER));
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(vd || stage == ZIO_STAGE_OPEN);
088f389458728c464569a5506b58070254fa4f7dahrens
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel IMPLY(lsize != psize, (flags & ZIO_FLAG_RAW) != 0);
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel
ccae0b50330edda9b094cee1ec6a0ad35443e8b0eschrock zio = kmem_cache_alloc(zio_cache, KM_SLEEP);
ccae0b50330edda9b094cee1ec6a0ad35443e8b0eschrock bzero(zio, sizeof (zio_t));
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick mutex_init(&zio->io_lock, NULL, MUTEX_DEFAULT, NULL);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick cv_init(&zio->io_cv, NULL, CV_DEFAULT, NULL);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore list_create(&zio->io_parent_list, sizeof (zio_link_t),
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore offsetof(zio_link_t, zl_parent_node));
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore list_create(&zio->io_child_list, sizeof (zio_link_t),
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore offsetof(zio_link_t, zl_child_node));
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_trace_init(&zio->io_alloc_list);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (vd != NULL)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_child_type = ZIO_CHILD_VDEV;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick else if (flags & ZIO_FLAG_GANG_CHILD)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_child_type = ZIO_CHILD_GANG;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick else if (flags & ZIO_FLAG_DDT_CHILD)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_child_type = ZIO_CHILD_DDT;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick else
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_child_type = ZIO_CHILD_LOGICAL;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (bp != NULL) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_bp = (blkptr_t *)bp;
fa9e4066f08beec538e775443c5be79dd423fcabahrens zio->io_bp_copy = *bp;
fa9e4066f08beec538e775443c5be79dd423fcabahrens zio->io_bp_orig = *bp;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (type != ZIO_TYPE_WRITE ||
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_child_type == ZIO_CHILD_DDT)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_bp = &zio->io_bp_copy; /* so caller can free */
f5383399322244b3008a624bcd7e93d67037d177Bill Moore if (zio->io_child_type == ZIO_CHILD_LOGICAL)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_logical = zio;
f5383399322244b3008a624bcd7e93d67037d177Bill Moore if (zio->io_child_type > ZIO_CHILD_GANG && BP_IS_GANG(bp))
f5383399322244b3008a624bcd7e93d67037d177Bill Moore pipeline |= ZIO_GANG_STAGES;
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_spa = spa;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_txg = txg;
fa9e4066f08beec538e775443c5be79dd423fcabahrens zio->io_done = done;
fa9e4066f08beec538e775443c5be79dd423fcabahrens zio->io_private = private;
fa9e4066f08beec538e775443c5be79dd423fcabahrens zio->io_type = type;
fa9e4066f08beec538e775443c5be79dd423fcabahrens zio->io_priority = priority;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_vd = vd;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_offset = offset;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_orig_data = zio->io_data = data;
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel zio->io_orig_size = zio->io_size = psize;
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel zio->io_lsize = lsize;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_orig_flags = zio->io_flags = flags;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_orig_stage = zio->io_stage = stage;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_orig_pipeline = zio->io_pipeline = pipeline;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio->io_pipeline_trace = ZIO_STAGE_OPEN;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore zio->io_state[ZIO_WAIT_READY] = (stage >= ZIO_STAGE_READY);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore zio->io_state[ZIO_WAIT_DONE] = (stage >= ZIO_STAGE_DONE);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zb != NULL)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_bookmark = *zb;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (pio != NULL) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zio->io_logical == NULL)
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock zio->io_logical = pio->io_logical;
f5383399322244b3008a624bcd7e93d67037d177Bill Moore if (zio->io_child_type == ZIO_CHILD_GANG)
f5383399322244b3008a624bcd7e93d67037d177Bill Moore zio->io_gang_leader = pio->io_gang_leader;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_add_child(pio, zio);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (zio);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
0a4e9518a44f226be6d39383330b5b1792d2f184gwstatic void
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_destroy(zio_t *zio)
0a4e9518a44f226be6d39383330b5b1792d2f184gw{
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_trace_fini(&zio->io_alloc_list);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore list_destroy(&zio->io_parent_list);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore list_destroy(&zio->io_child_list);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick mutex_destroy(&zio->io_lock);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick cv_destroy(&zio->io_cv);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick kmem_cache_free(zio_cache, zio);
0a4e9518a44f226be6d39383330b5b1792d2f184gw}
0a4e9518a44f226be6d39383330b5b1792d2f184gw
fa9e4066f08beec538e775443c5be79dd423fcabahrenszio_t *
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moorezio_null(zio_t *pio, spa_t *spa, vdev_t *vd, zio_done_func_t *done,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick void *private, enum zio_flag flags)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens zio_t *zio;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel zio = zio_create(pio, spa, 0, NULL, NULL, 0, 0, done, private,
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ZIO_STAGE_OPEN, ZIO_INTERLOCK_PIPELINE);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (zio);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrenszio_t *
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickzio_root(spa_t *spa, zio_done_func_t *done, void *private, enum zio_flag flags)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore return (zio_null(NULL, spa, NULL, done, private, flags));
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrensvoid
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrenszfs_blkptr_verify(spa_t *spa, const blkptr_t *bp)
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens{
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens if (!DMU_OT_IS_VALID(BP_GET_TYPE(bp))) {
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens zfs_panic_recover("blkptr at %p has invalid TYPE %llu",
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens bp, (longlong_t)BP_GET_TYPE(bp));
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens }
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens if (BP_GET_CHECKSUM(bp) >= ZIO_CHECKSUM_FUNCTIONS ||
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens BP_GET_CHECKSUM(bp) <= ZIO_CHECKSUM_ON) {
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens zfs_panic_recover("blkptr at %p has invalid CHECKSUM %llu",
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens bp, (longlong_t)BP_GET_CHECKSUM(bp));
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens }
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens if (BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_FUNCTIONS ||
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens BP_GET_COMPRESS(bp) <= ZIO_COMPRESS_ON) {
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens zfs_panic_recover("blkptr at %p has invalid COMPRESS %llu",
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens bp, (longlong_t)BP_GET_COMPRESS(bp));
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens }
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens if (BP_GET_LSIZE(bp) > SPA_MAXBLOCKSIZE) {
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens zfs_panic_recover("blkptr at %p has invalid LSIZE %llu",
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens bp, (longlong_t)BP_GET_LSIZE(bp));
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens }
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens if (BP_GET_PSIZE(bp) > SPA_MAXBLOCKSIZE) {
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens zfs_panic_recover("blkptr at %p has invalid PSIZE %llu",
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens bp, (longlong_t)BP_GET_PSIZE(bp));
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens }
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens if (BP_IS_EMBEDDED(bp)) {
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens if (BPE_GET_ETYPE(bp) > NUM_BP_EMBEDDED_TYPES) {
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens zfs_panic_recover("blkptr at %p has invalid ETYPE %llu",
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens bp, (longlong_t)BPE_GET_ETYPE(bp));
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens }
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens }
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens /*
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens * Pool-specific checks.
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens *
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens * Note: it would be nice to verify that the blk_birth and
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens * BP_PHYSICAL_BIRTH() are not too large. However, spa_freeze()
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens * allows the birth time of log blocks (and dmu_sync()-ed blocks
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens * that are in the log) to be arbitrarily large.
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens */
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens for (int i = 0; i < BP_GET_NDVAS(bp); i++) {
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens uint64_t vdevid = DVA_GET_VDEV(&bp->blk_dva[i]);
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens if (vdevid >= spa->spa_root_vdev->vdev_children) {
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens zfs_panic_recover("blkptr at %p DVA %u has invalid "
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens "VDEV %llu",
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens bp, i, (longlong_t)vdevid);
5897eb49ccde82d19214b71984f57935e7e313d1Justin Gibbs continue;
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens }
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens vdev_t *vd = spa->spa_root_vdev->vdev_child[vdevid];
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens if (vd == NULL) {
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens zfs_panic_recover("blkptr at %p DVA %u has invalid "
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens "VDEV %llu",
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens bp, i, (longlong_t)vdevid);
5897eb49ccde82d19214b71984f57935e7e313d1Justin Gibbs continue;
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens }
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens if (vd->vdev_ops == &vdev_hole_ops) {
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens zfs_panic_recover("blkptr at %p DVA %u has hole "
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens "VDEV %llu",
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens bp, i, (longlong_t)vdevid);
5897eb49ccde82d19214b71984f57935e7e313d1Justin Gibbs continue;
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens }
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens if (vd->vdev_ops == &vdev_missing_ops) {
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens /*
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens * "missing" vdevs are valid during import, but we
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens * don't have their detailed info (e.g. asize), so
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens * we can't perform any more checks on them.
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens */
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens continue;
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens }
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens uint64_t offset = DVA_GET_OFFSET(&bp->blk_dva[i]);
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens uint64_t asize = DVA_GET_ASIZE(&bp->blk_dva[i]);
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens if (BP_IS_GANG(bp))
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens asize = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens if (offset + asize > vd->vdev_asize) {
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens zfs_panic_recover("blkptr at %p DVA %u has invalid "
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens "OFFSET %llu",
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens bp, i, (longlong_t)offset);
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens }
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens }
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens}
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrenszio_t *
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_read(zio_t *pio, spa_t *spa, const blkptr_t *bp,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick void *data, uint64_t size, zio_done_func_t *done, void *private,
7802d7bf98dec568dadf72286893b1fe5abd8602Matthew Ahrens zio_priority_t priority, enum zio_flag flags, const zbookmark_phys_t *zb)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens zio_t *zio;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens zfs_blkptr_verify(spa, bp);
f63ab3d5a84a12b474655fc7e700db3efba6c4c9Matthew Ahrens
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio = zio_create(pio, spa, BP_PHYSICAL_BIRTH(bp), bp,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel data, size, size, done, private,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ZIO_TYPE_READ, priority, flags, NULL, 0, zb,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ZIO_DDT_CHILD_READ_PIPELINE : ZIO_READ_PIPELINE);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (zio);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrenszio_t *
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel void *data, uint64_t lsize, uint64_t psize, const zio_prop_t *zp,
8df0bcf0df7622a075cc6e52f659d2fcfdd08cdcPaul Dagnelie zio_done_func_t *ready, zio_done_func_t *children_ready,
8df0bcf0df7622a075cc6e52f659d2fcfdd08cdcPaul Dagnelie zio_done_func_t *physdone, zio_done_func_t *done,
8df0bcf0df7622a075cc6e52f659d2fcfdd08cdcPaul Dagnelie void *private, zio_priority_t priority, enum zio_flag flags,
8df0bcf0df7622a075cc6e52f659d2fcfdd08cdcPaul Dagnelie const zbookmark_phys_t *zb)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens zio_t *zio;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(zp->zp_checksum >= ZIO_CHECKSUM_OFF &&
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zp->zp_checksum < ZIO_CHECKSUM_FUNCTIONS &&
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zp->zp_compress >= ZIO_COMPRESS_OFF &&
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zp->zp_compress < ZIO_COMPRESS_FUNCTIONS &&
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden DMU_OT_IS_VALID(zp->zp_type) &&
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zp->zp_level < 32 &&
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zp->zp_copies > 0 &&
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson zp->zp_copies <= spa_max_replication(spa));
0a4e9518a44f226be6d39383330b5b1792d2f184gw
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel zio = zio_create(pio, spa, txg, bp, data, lsize, psize, done, private,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ZIO_DDT_CHILD_WRITE_PIPELINE : ZIO_WRITE_PIPELINE);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee zio->io_ready = ready;
8df0bcf0df7622a075cc6e52f659d2fcfdd08cdcPaul Dagnelie zio->io_children_ready = children_ready;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zio->io_physdone = physdone;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_prop = *zp;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens /*
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * Data can be NULL if we are going to call zio_write_override() to
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * provide the already-allocated BP. But we may need the data to
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * verify a dedup hit (if requested). In this case, don't try to
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * dedup (just take the already-allocated BP verbatim).
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens */
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens if (data == NULL && zio->io_prop.zp_dedup_verify) {
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens zio->io_prop.zp_dedup = zio->io_prop.zp_dedup_verify = B_FALSE;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens }
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (zio);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrenszio_t *
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_rewrite(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, void *data,
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens uint64_t size, zio_done_func_t *done, void *private,
7802d7bf98dec568dadf72286893b1fe5abd8602Matthew Ahrens zio_priority_t priority, enum zio_flag flags, zbookmark_phys_t *zb)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens zio_t *zio;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel zio = zio_create(pio, spa, txg, bp, data, size, size, done, private,
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_IO_REWRITE, NULL, 0, zb,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (zio);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickvoid
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilsonzio_write_override(zio_t *zio, blkptr_t *bp, int copies, boolean_t nopwrite)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(zio->io_type == ZIO_TYPE_WRITE);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(zio->io_txg == spa_syncing_txg(zio->io_spa));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson /*
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson * We must reset the io_prop to match the values that existed
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson * when the bp was first written by dmu_sync() keeping in mind
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson * that nopwrite and dedup are mutually exclusive.
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson */
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson zio->io_prop.zp_dedup = nopwrite ? B_FALSE : zio->io_prop.zp_dedup;
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson zio->io_prop.zp_nopwrite = nopwrite;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_prop.zp_copies = copies;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_bp_override = bp;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickvoid
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickzio_free(spa_t *spa, uint64_t txg, const blkptr_t *bp)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens /*
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * The check for EMBEDDED is a performance optimization. We
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * process the free here (by ignoring it) rather than
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * putting it on the list and then processing it in zio_free_sync().
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens */
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens if (BP_IS_EMBEDDED(bp))
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens return;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens metaslab_check_free(spa, bp);
9cb154a3c9f170904dce9bad5bd5a7d256b922a4Matthew Ahrens
9cb154a3c9f170904dce9bad5bd5a7d256b922a4Matthew Ahrens /*
9cb154a3c9f170904dce9bad5bd5a7d256b922a4Matthew Ahrens * Frees that are for the currently-syncing txg, are not going to be
9cb154a3c9f170904dce9bad5bd5a7d256b922a4Matthew Ahrens * deferred, and which will not need to do a read (i.e. not GANG or
9cb154a3c9f170904dce9bad5bd5a7d256b922a4Matthew Ahrens * DEDUP), can be processed immediately. Otherwise, put them on the
9cb154a3c9f170904dce9bad5bd5a7d256b922a4Matthew Ahrens * in-memory list for later processing.
9cb154a3c9f170904dce9bad5bd5a7d256b922a4Matthew Ahrens */
9cb154a3c9f170904dce9bad5bd5a7d256b922a4Matthew Ahrens if (BP_IS_GANG(bp) || BP_GET_DEDUP(bp) ||
9cb154a3c9f170904dce9bad5bd5a7d256b922a4Matthew Ahrens txg != spa->spa_syncing_txg ||
9cb154a3c9f170904dce9bad5bd5a7d256b922a4Matthew Ahrens spa_sync_pass(spa) >= zfs_sync_pass_deferred_free) {
9cb154a3c9f170904dce9bad5bd5a7d256b922a4Matthew Ahrens bplist_append(&spa->spa_free_bplist[txg & TXG_MASK], bp);
9cb154a3c9f170904dce9bad5bd5a7d256b922a4Matthew Ahrens } else {
9cb154a3c9f170904dce9bad5bd5a7d256b922a4Matthew Ahrens VERIFY0(zio_wait(zio_free_sync(NULL, spa, txg, bp, 0)));
9cb154a3c9f170904dce9bad5bd5a7d256b922a4Matthew Ahrens }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
fa9e4066f08beec538e775443c5be79dd423fcabahrenszio_t *
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickzio_free_sync(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick enum zio_flag flags)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens zio_t *zio;
9cb154a3c9f170904dce9bad5bd5a7d256b922a4Matthew Ahrens enum zio_stage stage = ZIO_FREE_PIPELINE;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT(!BP_IS_HOLE(bp));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(spa_syncing_txg(spa) == txg);
01f55e48fb4d524eaf70687728aa51b7762e2e97George Wilson ASSERT(spa_sync_pass(spa) < zfs_sync_pass_deferred_free);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens if (BP_IS_EMBEDDED(bp))
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens return (zio_null(pio, spa, NULL, NULL, NULL, 0));
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens metaslab_check_free(spa, bp);
6e6d5868f52089b9026785bd90257a3d3f6e5ee2Matthew Ahrens arc_freed(spa, bp);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
9cb154a3c9f170904dce9bad5bd5a7d256b922a4Matthew Ahrens /*
9cb154a3c9f170904dce9bad5bd5a7d256b922a4Matthew Ahrens * GANG and DEDUP blocks can induce a read (for the gang block header,
9cb154a3c9f170904dce9bad5bd5a7d256b922a4Matthew Ahrens * or the DDT), so issue them asynchronously so that this thread is
9cb154a3c9f170904dce9bad5bd5a7d256b922a4Matthew Ahrens * not tied up.
9cb154a3c9f170904dce9bad5bd5a7d256b922a4Matthew Ahrens */
9cb154a3c9f170904dce9bad5bd5a7d256b922a4Matthew Ahrens if (BP_IS_GANG(bp) || BP_GET_DEDUP(bp))
9cb154a3c9f170904dce9bad5bd5a7d256b922a4Matthew Ahrens stage |= ZIO_STAGE_ISSUE_ASYNC;
9cb154a3c9f170904dce9bad5bd5a7d256b922a4Matthew Ahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel BP_GET_PSIZE(bp), NULL, NULL, ZIO_TYPE_FREE, ZIO_PRIORITY_NOW,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel flags, NULL, 0, NULL, ZIO_STAGE_OPEN, stage);
9cb154a3c9f170904dce9bad5bd5a7d256b922a4Matthew Ahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (zio);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrenszio_t *
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickzio_claim(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_done_func_t *done, void *private, enum zio_flag flags)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens zio_t *zio;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens dprintf_bp(bp, "claiming in txg %llu", txg);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens if (BP_IS_EMBEDDED(bp))
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens return (zio_null(pio, spa, NULL, NULL, NULL, 0));
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens /*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * A claim is an allocation of a specific block. Claims are needed
fa9e4066f08beec538e775443c5be79dd423fcabahrens * to support immediate writes in the intent log. The issue is that
fa9e4066f08beec538e775443c5be79dd423fcabahrens * immediate writes contain committed data, but in a txg that was
fa9e4066f08beec538e775443c5be79dd423fcabahrens * *not* committed. Upon opening the pool after an unclean shutdown,
fa9e4066f08beec538e775443c5be79dd423fcabahrens * the intent log claims all blocks that contain immediate write data
fa9e4066f08beec538e775443c5be79dd423fcabahrens * so that the SPA knows they're in use.
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * All claims *must* be resolved in the first txg -- before the SPA
fa9e4066f08beec538e775443c5be79dd423fcabahrens * starts allocating blocks -- so that nothing is allocated twice.
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * If txg == 0 we just verify that the block is claimable.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT3U(spa->spa_uberblock.ub_rootbp.blk_birth, <, spa_first_txg(spa));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(txg == spa_first_txg(spa) || txg == 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(!BP_GET_DEDUP(bp) || !spa_writeable(spa)); /* zdb(1M) */
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel BP_GET_PSIZE(bp), done, private, ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel flags, NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT0(zio->io_queued_timestamp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (zio);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrenszio_t *
fa9e4066f08beec538e775443c5be79dd423fcabahrenszio_ioctl(zio_t *pio, spa_t *spa, vdev_t *vd, int cmd,
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zio_done_func_t *done, void *private, enum zio_flag flags)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens zio_t *zio;
fa9e4066f08beec538e775443c5be79dd423fcabahrens int c;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (vd->vdev_children == 0) {
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel zio = zio_create(pio, spa, 0, NULL, NULL, 0, 0, done, private,
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens ZIO_TYPE_IOCTL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
fa9e4066f08beec538e775443c5be79dd423fcabahrens ZIO_STAGE_OPEN, ZIO_IOCTL_PIPELINE);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens zio->io_cmd = cmd;
fa9e4066f08beec538e775443c5be79dd423fcabahrens } else {
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore zio = zio_null(pio, spa, NULL, NULL, NULL, flags);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens for (c = 0; c < vd->vdev_children; c++)
fa9e4066f08beec538e775443c5be79dd423fcabahrens zio_nowait(zio_ioctl(zio, spa, vd->vdev_child[c], cmd,
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens done, private, flags));
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (zio);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrenszio_t *
fa9e4066f08beec538e775443c5be79dd423fcabahrenszio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
fa9e4066f08beec538e775443c5be79dd423fcabahrens void *data, int checksum, zio_done_func_t *done, void *private,
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zio_priority_t priority, enum zio_flag flags, boolean_t labels)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens zio_t *zio;
0a4e9518a44f226be6d39383330b5b1792d2f184gw
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(vd->vdev_children == 0);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT3U(offset + size, <=, vd->vdev_psize);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, size, done,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel private, ZIO_TYPE_READ, priority, flags | ZIO_FLAG_PHYSICAL, vd,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel offset, NULL, ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_prop.zp_checksum = checksum;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (zio);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrenszio_t *
fa9e4066f08beec538e775443c5be79dd423fcabahrenszio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
fa9e4066f08beec538e775443c5be79dd423fcabahrens void *data, int checksum, zio_done_func_t *done, void *private,
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zio_priority_t priority, enum zio_flag flags, boolean_t labels)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens zio_t *zio;
0a4e9518a44f226be6d39383330b5b1792d2f184gw
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(vd->vdev_children == 0);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT3U(offset + size, <=, vd->vdev_psize);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, size, done,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel private, ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_PHYSICAL, vd,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel offset, NULL, ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_prop.zp_checksum = checksum;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (zio_checksum_table[checksum].ci_flags & ZCHECKSUM_FLAG_EMBEDDED) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens /*
6e1f5caa9321646aa4212d48e32a0d241866d85dNeil Perrin * zec checksums are necessarily destructive -- they modify
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * the end of the write buffer to hold the verifier/checksum.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Therefore, we must make a local copy in case the data is
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * being written to multiple places in parallel.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick void *wbuf = zio_buf_alloc(size);
fa9e4066f08beec538e775443c5be79dd423fcabahrens bcopy(data, wbuf, size);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_push_transform(zio, wbuf, size, size, NULL);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (zio);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * Create a child I/O to do some work for us.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrenszio_t *
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_vdev_child_io(zio_t *pio, blkptr_t *bp, vdev_t *vd, uint64_t offset,
dcbf3bd6a1f1360fc1afcee9e22c6dcff7844bf2George Wilson void *data, uint64_t size, int type, zio_priority_t priority,
dcbf3bd6a1f1360fc1afcee9e22c6dcff7844bf2George Wilson enum zio_flag flags, zio_done_func_t *done, void *private)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick enum zio_stage pipeline = ZIO_VDEV_CHILD_PIPELINE;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_t *zio;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(vd->vdev_parent ==
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick (pio->io_vd ? pio->io_vd : pio->io_spa->spa_root_vdev));
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (type == ZIO_TYPE_READ && bp != NULL) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens /*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If we have the bp, then the child should perform the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * checksum and the parent need not. This pushes error
fa9e4066f08beec538e775443c5be79dd423fcabahrens * detection as close to the leaves as possible and
fa9e4066f08beec538e775443c5be79dd423fcabahrens * eliminates redundant checksums in the interior nodes.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick pipeline |= ZIO_STAGE_CHECKSUM_VERIFY;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick pio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (vd->vdev_children == 0)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick offset += VDEV_LABEL_START_SIZE;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick flags |= ZIO_VDEV_CHILD_FLAGS(pio) | ZIO_FLAG_DONT_PROPAGATE;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick /*
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * If we've decided to do a repair, the write is not speculative --
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * even if the original read was.
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick */
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (flags & ZIO_FLAG_IO_REPAIR)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick flags &= ~ZIO_FLAG_SPECULATIVE;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson /*
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * If we're creating a child I/O that is not associated with a
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * top-level vdev, then the child zio is not an allocating I/O.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * If this is a retried I/O then we ignore it since we will
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * have already processed the original allocating I/O.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (flags & ZIO_FLAG_IO_ALLOCATING &&
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson (vd != vd->vdev_top || (flags & ZIO_FLAG_IO_RETRY))) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson metaslab_class_t *mc = spa_normal_class(pio->io_spa);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(mc->mc_alloc_throttle_enabled);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(type == ZIO_TYPE_WRITE);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(priority == ZIO_PRIORITY_ASYNC_WRITE);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(!(flags & ZIO_FLAG_IO_REPAIR));
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(!(pio->io_flags & ZIO_FLAG_IO_REWRITE) ||
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson pio->io_child_type == ZIO_CHILD_GANG);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson flags &= ~ZIO_FLAG_IO_ALLOCATING;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson }
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel zio = zio_create(pio, pio->io_spa, pio->io_txg, bp, data, size, size,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick done, private, type, priority, flags, vd, offset, &pio->io_bookmark,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ZIO_STAGE_VDEV_IO_START >> 1, pipeline);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zio->io_physdone = pio->io_physdone;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (vd->vdev_ops->vdev_op_leaf && zio->io_logical != NULL)
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zio->io_logical->io_phys_children++;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return (zio);
32b87932f3ef0887d873b7f6d2d1943799b2afc0ek}
32b87932f3ef0887d873b7f6d2d1943799b2afc0ek
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_t *
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_vdev_delegated_io(vdev_t *vd, uint64_t offset, void *data, uint64_t size,
9a686fbc186e8e2a64e9a5094d44c7d6fa0ea167Paul Dagnelie int type, zio_priority_t priority, enum zio_flag flags,
9a686fbc186e8e2a64e9a5094d44c7d6fa0ea167Paul Dagnelie zio_done_func_t *done, void *private)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_t *zio;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(vd->vdev_ops->vdev_op_leaf);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio = zio_create(NULL, vd->vdev_spa, 0, NULL,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel data, size, size, done, private, type, priority,
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens flags | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY | ZIO_FLAG_DELEGATED,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick vd, offset, NULL,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ZIO_STAGE_VDEV_IO_START >> 1, ZIO_VDEV_CHILD_PIPELINE);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return (zio);
e05725b117836db173257fae43fb0746eb857fb5bonwick}
e05725b117836db173257fae43fb0746eb857fb5bonwick
e05725b117836db173257fae43fb0746eb857fb5bonwickvoid
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_flush(zio_t *zio, vdev_t *vd)
e05725b117836db173257fae43fb0746eb857fb5bonwick{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_nowait(zio_ioctl(zio, zio->io_spa, vd, DKIOCFLUSHWRITECACHE,
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens NULL, NULL,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY));
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
6e1f5caa9321646aa4212d48e32a0d241866d85dNeil Perrinvoid
6e1f5caa9321646aa4212d48e32a0d241866d85dNeil Perrinzio_shrink(zio_t *zio, uint64_t size)
6e1f5caa9321646aa4212d48e32a0d241866d85dNeil Perrin{
6e1f5caa9321646aa4212d48e32a0d241866d85dNeil Perrin ASSERT(zio->io_executor == NULL);
6e1f5caa9321646aa4212d48e32a0d241866d85dNeil Perrin ASSERT(zio->io_orig_size == zio->io_size);
6e1f5caa9321646aa4212d48e32a0d241866d85dNeil Perrin ASSERT(size <= zio->io_size);
6e1f5caa9321646aa4212d48e32a0d241866d85dNeil Perrin
6e1f5caa9321646aa4212d48e32a0d241866d85dNeil Perrin /*
6e1f5caa9321646aa4212d48e32a0d241866d85dNeil Perrin * We don't shrink for raidz because of problems with the
6e1f5caa9321646aa4212d48e32a0d241866d85dNeil Perrin * reconstruction when reading back less than the block size.
6e1f5caa9321646aa4212d48e32a0d241866d85dNeil Perrin * Note, BP_IS_RAIDZ() assumes no compression.
6e1f5caa9321646aa4212d48e32a0d241866d85dNeil Perrin */
6e1f5caa9321646aa4212d48e32a0d241866d85dNeil Perrin ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel if (!BP_IS_RAIDZ(zio->io_bp)) {
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel /* we are not doing a raw write */
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel ASSERT3U(zio->io_size, ==, zio->io_lsize);
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel zio->io_orig_size = zio->io_size = zio->io_lsize = size;
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel }
6e1f5caa9321646aa4212d48e32a0d241866d85dNeil Perrin}
6e1f5caa9321646aa4212d48e32a0d241866d85dNeil Perrin
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * Prepare to read and write logical blocks
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e05725b117836db173257fae43fb0746eb857fb5bonwickstatic int
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_read_bp_init(zio_t *zio)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick blkptr_t *bp = zio->io_bp;
e05725b117836db173257fae43fb0746eb857fb5bonwick
03361682bf38acf5bcc36ee83a0d6277731eee68Jeff Bonwick if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF &&
f5383399322244b3008a624bcd7e93d67037d177Bill Moore zio->io_child_type == ZIO_CHILD_LOGICAL &&
f5383399322244b3008a624bcd7e93d67037d177Bill Moore !(zio->io_flags & ZIO_FLAG_RAW)) {
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens uint64_t psize =
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens BP_IS_EMBEDDED(bp) ? BPE_GET_PSIZE(bp) : BP_GET_PSIZE(bp);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick void *cbuf = zio_buf_alloc(psize);
e05725b117836db173257fae43fb0746eb857fb5bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_push_transform(zio, cbuf, psize, psize, zio_decompress);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens if (BP_IS_EMBEDDED(bp) && BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA) {
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens decode_embedded_bp_compressed(bp, zio->io_data);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens } else {
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens ASSERT(!BP_IS_EMBEDDED(bp));
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens }
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden if (!DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) && BP_GET_LEVEL(bp) == 0)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_flags |= ZIO_FLAG_DONT_CACHE;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick if (BP_GET_TYPE(bp) == DMU_OT_DDT_ZAP)
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick zio->io_flags |= ZIO_FLAG_DONT_CACHE;
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (BP_GET_DEDUP(bp) && zio->io_child_type == ZIO_CHILD_LOGICAL)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_pipeline = ZIO_DDT_READ_PIPELINE;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return (ZIO_PIPELINE_CONTINUE);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e05725b117836db173257fae43fb0746eb857fb5bonwickstatic int
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_write_bp_init(zio_t *zio)
0a4e9518a44f226be6d39383330b5b1792d2f184gw{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (!IO_IS_ALLOCATING(zio))
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return (ZIO_PIPELINE_CONTINUE);
0a4e9518a44f226be6d39383330b5b1792d2f184gw
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (zio->io_bp_override) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson blkptr_t *bp = zio->io_bp;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio_prop_t *zp = &zio->io_prop;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(bp->blk_birth != zio->io_txg);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(BP_GET_DEDUP(zio->io_bp_override) == 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick *bp = *zio->io_bp_override;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens if (BP_IS_EMBEDDED(bp))
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens return (ZIO_PIPELINE_CONTINUE);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson /*
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson * If we've been overridden and nopwrite is set then
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson * set the flag accordingly to indicate that a nopwrite
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson * has already occurred.
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson */
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson if (!BP_IS_HOLE(bp) && zp->zp_nopwrite) {
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson ASSERT(!zp->zp_dedup);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT3U(BP_GET_CHECKSUM(bp), ==, zp->zp_checksum);
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson zio->io_flags |= ZIO_FLAG_NOPWRITE;
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson return (ZIO_PIPELINE_CONTINUE);
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson }
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson ASSERT(!zp->zp_nopwrite);
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (BP_IS_HOLE(bp) || !zp->zp_dedup)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (ZIO_PIPELINE_CONTINUE);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ASSERT((zio_checksum_table[zp->zp_checksum].ci_flags &
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ZCHECKSUM_FLAG_DEDUP) || zp->zp_dedup_verify);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (BP_GET_CHECKSUM(bp) == zp->zp_checksum) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick BP_SET_DEDUP(bp, 1);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_pipeline |= ZIO_STAGE_DDT_WRITE;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (ZIO_PIPELINE_CONTINUE);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson /*
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * We were unable to handle this as an override bp, treat
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * it as a regular write I/O.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson */
b39b744be78c6327db43c1f69d11c2f5909f73cbMatthew Ahrens zio->io_bp_override = NULL;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson *bp = zio->io_bp_orig;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio->io_pipeline = zio->io_orig_pipeline;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
0a4e9518a44f226be6d39383330b5b1792d2f184gw
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return (ZIO_PIPELINE_CONTINUE);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson}
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonstatic int
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonzio_write_compress(zio_t *zio)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson{
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson spa_t *spa = zio->io_spa;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio_prop_t *zp = &zio->io_prop;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson enum zio_compress compress = zp->zp_compress;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson blkptr_t *bp = zio->io_bp;
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel uint64_t lsize = zio->io_lsize;
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel uint64_t psize = zio->io_size;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson int pass = 1;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel EQUIV(lsize != psize, (zio->io_flags & ZIO_FLAG_RAW) != 0);
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson /*
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * If our children haven't all reached the ready stage,
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * wait for them and then repeat this pipeline stage.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_READY) ||
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio_wait_for_children(zio, ZIO_CHILD_LOGICAL, ZIO_WAIT_READY))
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return (ZIO_PIPELINE_STOP);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (!IO_IS_ALLOCATING(zio))
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return (ZIO_PIPELINE_CONTINUE);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (zio->io_children_ready != NULL) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson /*
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * Now that all our children are ready, run the callback
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * associated with this zio in case it wants to modify the
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * data to be written.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT3U(zp->zp_level, >, 0);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio->io_children_ready(zio);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson }
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(zio->io_bp_override == NULL);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick /*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * We're rewriting an existing block, which means we're
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * working on behalf of spa_sync(). For spa_sync() to
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * converge, it must eventually be the case that we don't
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * have to allocate new blocks. But compression changes
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * the blocksize, which forces a reallocate, and makes
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * convergence take longer. Therefore, after the first
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * few passes, stop compressing to ensure convergence.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick */
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick pass = spa_sync_pass(spa);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(zio->io_txg == spa_syncing_txg(spa));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(!BP_GET_DEDUP(bp));
e05725b117836db173257fae43fb0746eb857fb5bonwick
01f55e48fb4d524eaf70687728aa51b7762e2e97George Wilson if (pass >= zfs_sync_pass_dont_compress)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick compress = ZIO_COMPRESS_OFF;
e05725b117836db173257fae43fb0746eb857fb5bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick /* Make sure someone doesn't change their mind on overwrites */
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens ASSERT(BP_IS_EMBEDDED(bp) || MIN(zp->zp_copies + BP_IS_GANG(bp),
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick spa_max_replication(spa)) == BP_GET_NDVAS(bp));
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel /* If it's a compressed write that is not raw, compress the buffer. */
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel if (compress != ZIO_COMPRESS_OFF && psize == lsize) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick void *cbuf = zio_buf_alloc(lsize);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick psize = zio_compress_data(compress, zio->io_data, cbuf, lsize);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (psize == 0 || psize == lsize) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick compress = ZIO_COMPRESS_OFF;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_buf_free(cbuf, lsize);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens } else if (!zp->zp_dedup && psize <= BPE_PAYLOAD_SIZE &&
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens zp->zp_level == 0 && !DMU_OT_HAS_FILL(zp->zp_type) &&
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens spa_feature_is_enabled(spa, SPA_FEATURE_EMBEDDED_DATA)) {
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens encode_embedded_bp_compressed(bp,
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens cbuf, compress, lsize, psize);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens BPE_SET_ETYPE(bp, BP_EMBEDDED_TYPE_DATA);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens BP_SET_TYPE(bp, zio->io_prop.zp_type);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens BP_SET_LEVEL(bp, zio->io_prop.zp_level);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens zio_buf_free(cbuf, lsize);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens bp->blk_birth = zio->io_txg;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens ASSERT(spa_feature_is_active(spa,
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens SPA_FEATURE_EMBEDDED_DATA));
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens return (ZIO_PIPELINE_CONTINUE);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick } else {
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens /*
81cd5c555f505484180a62ca5a2fbb00d70c57d6Matthew Ahrens * Round up compressed size up to the ashift
81cd5c555f505484180a62ca5a2fbb00d70c57d6Matthew Ahrens * of the smallest-ashift device, and zero the tail.
81cd5c555f505484180a62ca5a2fbb00d70c57d6Matthew Ahrens * This ensures that the compressed size of the BP
81cd5c555f505484180a62ca5a2fbb00d70c57d6Matthew Ahrens * (and thus compressratio property) are correct,
81cd5c555f505484180a62ca5a2fbb00d70c57d6Matthew Ahrens * in that we charge for the padding used to fill out
81cd5c555f505484180a62ca5a2fbb00d70c57d6Matthew Ahrens * the last sector.
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens */
81cd5c555f505484180a62ca5a2fbb00d70c57d6Matthew Ahrens ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT);
81cd5c555f505484180a62ca5a2fbb00d70c57d6Matthew Ahrens size_t rounded = (size_t)P2ROUNDUP(psize,
81cd5c555f505484180a62ca5a2fbb00d70c57d6Matthew Ahrens 1ULL << spa->spa_min_ashift);
81cd5c555f505484180a62ca5a2fbb00d70c57d6Matthew Ahrens if (rounded >= lsize) {
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens compress = ZIO_COMPRESS_OFF;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens zio_buf_free(cbuf, lsize);
81cd5c555f505484180a62ca5a2fbb00d70c57d6Matthew Ahrens psize = lsize;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens } else {
81cd5c555f505484180a62ca5a2fbb00d70c57d6Matthew Ahrens bzero((char *)cbuf + psize, rounded - psize);
81cd5c555f505484180a62ca5a2fbb00d70c57d6Matthew Ahrens psize = rounded;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens zio_push_transform(zio, cbuf,
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens psize, lsize, NULL);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens }
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson /*
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * We were unable to handle this as an override bp, treat
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * it as a regular write I/O.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio->io_bp_override = NULL;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson *bp = zio->io_bp_orig;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio->io_pipeline = zio->io_orig_pipeline;
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel } else {
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel ASSERT3U(psize, !=, 0);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick /*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * The final pass of spa_sync() must be all rewrites, but the first
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * few passes offer a trade-off: allocating blocks defers convergence,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * but newly allocated blocks are sequential, so they can be written
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * to disk faster. Therefore, we allow the first few passes of
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * spa_sync() to allocate new blocks, but force rewrites after that.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * There should only be a handful of blocks after pass 1 in any case.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick */
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg &&
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman BP_GET_PSIZE(bp) == psize &&
01f55e48fb4d524eaf70687728aa51b7762e2e97George Wilson pass >= zfs_sync_pass_rewrite) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(psize != 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick enum zio_stage gang_stages = zio->io_pipeline & ZIO_GANG_STAGES;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_pipeline = ZIO_REWRITE_PIPELINE | gang_stages;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_flags |= ZIO_FLAG_IO_REWRITE;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick } else {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick BP_ZERO(bp);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_pipeline = ZIO_WRITE_PIPELINE;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (psize == 0) {
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman if (zio->io_bp_orig.blk_birth != 0 &&
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) {
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman BP_SET_LSIZE(bp, lsize);
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman BP_SET_TYPE(bp, zp->zp_type);
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman BP_SET_LEVEL(bp, zp->zp_level);
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman BP_SET_BIRTH(bp, zio->io_txg, 0);
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman }
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick } else {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(zp->zp_checksum != ZIO_CHECKSUM_GANG_HEADER);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick BP_SET_LSIZE(bp, lsize);
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman BP_SET_TYPE(bp, zp->zp_type);
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman BP_SET_LEVEL(bp, zp->zp_level);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick BP_SET_PSIZE(bp, psize);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick BP_SET_COMPRESS(bp, compress);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick BP_SET_CHECKSUM(bp, zp->zp_checksum);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick BP_SET_DEDUP(bp, zp->zp_dedup);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (zp->zp_dedup) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_pipeline = ZIO_DDT_WRITE_PIPELINE;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson if (zp->zp_nopwrite) {
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson zio->io_pipeline |= ZIO_STAGE_NOP_WRITE;
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (ZIO_PIPELINE_CONTINUE);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickstatic int
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickzio_free_bp_init(zio_t *zio)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick blkptr_t *bp = zio->io_bp;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (zio->io_child_type == ZIO_CHILD_LOGICAL) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (BP_GET_DEDUP(bp))
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_pipeline = ZIO_DDT_FREE_PIPELINE;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e05725b117836db173257fae43fb0746eb857fb5bonwick return (ZIO_PIPELINE_CONTINUE);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick/*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * ==========================================================================
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * Execute the I/O pipeline
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * ==========================================================================
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickstatic void
ec94d32216ed5705f5176582355cc311cf848e73Adam Leventhalzio_taskq_dispatch(zio_t *zio, zio_taskq_type_t q, boolean_t cutinline)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson spa_t *spa = zio->io_spa;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_type_t t = zio->io_type;
5aeb94743e3be0c51e86f73096334611ae3a058eGarrett D'Amore int flags = (cutinline ? TQ_FRONT : 0);
0a4e9518a44f226be6d39383330b5b1792d2f184gw
0a4e9518a44f226be6d39383330b5b1792d2f184gw /*
bbe36defdfa03da1119804d4ca2d48c1fc6c4ce1George Wilson * If we're a config writer or a probe, the normal issue and
bbe36defdfa03da1119804d4ca2d48c1fc6c4ce1George Wilson * interrupt threads may all be blocked waiting for the config lock.
bbe36defdfa03da1119804d4ca2d48c1fc6c4ce1George Wilson * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL.
0a4e9518a44f226be6d39383330b5b1792d2f184gw */
bbe36defdfa03da1119804d4ca2d48c1fc6c4ce1George Wilson if (zio->io_flags & (ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_PROBE))
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick t = ZIO_TYPE_NULL;
0a4e9518a44f226be6d39383330b5b1792d2f184gw
0a4e9518a44f226be6d39383330b5b1792d2f184gw /*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * A similar issue exists for the L2ARC write thread until L2ARC 2.0.
0a4e9518a44f226be6d39383330b5b1792d2f184gw */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (t == ZIO_TYPE_WRITE && zio->io_vd && zio->io_vd->vdev_aux)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick t = ZIO_TYPE_NULL;
0a4e9518a44f226be6d39383330b5b1792d2f184gw
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson /*
ec94d32216ed5705f5176582355cc311cf848e73Adam Leventhal * If this is a high priority I/O, then use the high priority taskq if
ec94d32216ed5705f5176582355cc311cf848e73Adam Leventhal * available.
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson */
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson if (zio->io_priority == ZIO_PRIORITY_NOW &&
ec94d32216ed5705f5176582355cc311cf848e73Adam Leventhal spa->spa_zio_taskq[t][q + 1].stqs_count != 0)
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson q++;
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson
80eb36f241abf8c076119fb4c49a55fd61ebc710George Wilson ASSERT3U(q, <, ZIO_TASKQ_TYPES);
5aeb94743e3be0c51e86f73096334611ae3a058eGarrett D'Amore
5aeb94743e3be0c51e86f73096334611ae3a058eGarrett D'Amore /*
5aeb94743e3be0c51e86f73096334611ae3a058eGarrett D'Amore * NB: We are assuming that the zio can only be dispatched
5aeb94743e3be0c51e86f73096334611ae3a058eGarrett D'Amore * to a single taskq at a time. It would be a grievous error
5aeb94743e3be0c51e86f73096334611ae3a058eGarrett D'Amore * to dispatch the zio to another taskq at the same time.
5aeb94743e3be0c51e86f73096334611ae3a058eGarrett D'Amore */
5aeb94743e3be0c51e86f73096334611ae3a058eGarrett D'Amore ASSERT(zio->io_tqent.tqent_next == NULL);
ec94d32216ed5705f5176582355cc311cf848e73Adam Leventhal spa_taskq_dispatch_ent(spa, t, q, (task_func_t *)zio_execute, zio,
ec94d32216ed5705f5176582355cc311cf848e73Adam Leventhal flags, &zio->io_tqent);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick}
0a4e9518a44f226be6d39383330b5b1792d2f184gw
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickstatic boolean_t
ec94d32216ed5705f5176582355cc311cf848e73Adam Leventhalzio_taskq_member(zio_t *zio, zio_taskq_type_t q)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick kthread_t *executor = zio->io_executor;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick spa_t *spa = zio->io_spa;
0a4e9518a44f226be6d39383330b5b1792d2f184gw
ec94d32216ed5705f5176582355cc311cf848e73Adam Leventhal for (zio_type_t t = 0; t < ZIO_TYPES; t++) {
ec94d32216ed5705f5176582355cc311cf848e73Adam Leventhal spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
ec94d32216ed5705f5176582355cc311cf848e73Adam Leventhal uint_t i;
ec94d32216ed5705f5176582355cc311cf848e73Adam Leventhal for (i = 0; i < tqs->stqs_count; i++) {
ec94d32216ed5705f5176582355cc311cf848e73Adam Leventhal if (taskq_member(tqs->stqs_taskq[i], executor))
ec94d32216ed5705f5176582355cc311cf848e73Adam Leventhal return (B_TRUE);
ec94d32216ed5705f5176582355cc311cf848e73Adam Leventhal }
ec94d32216ed5705f5176582355cc311cf848e73Adam Leventhal }
0a4e9518a44f226be6d39383330b5b1792d2f184gw
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return (B_FALSE);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick}
e05725b117836db173257fae43fb0746eb857fb5bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickstatic int
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_issue_async(zio_t *zio)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick{
35a5a3587fd94b666239c157d3722745250ccbd7Jonathan Adams zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return (ZIO_PIPELINE_STOP);
0a4e9518a44f226be6d39383330b5b1792d2f184gw}
0a4e9518a44f226be6d39383330b5b1792d2f184gw
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickvoid
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_interrupt(zio_t *zio)
0a4e9518a44f226be6d39383330b5b1792d2f184gw{
35a5a3587fd94b666239c157d3722745250ccbd7Jonathan Adams zio_taskq_dispatch(zio, ZIO_TASKQ_INTERRUPT, B_FALSE);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick}
0a4e9518a44f226be6d39383330b5b1792d2f184gw
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Suryavoid
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Suryazio_delay_interrupt(zio_t *zio)
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya{
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya /*
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya * The timeout_generic() function isn't defined in userspace, so
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya * rather than trying to implement the function, the zio delay
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya * functionality has been disabled for userspace builds.
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya */
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya#ifdef _KERNEL
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya /*
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya * If io_target_timestamp is zero, then no delay has been registered
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya * for this IO, thus jump to the end of this function and "skip" the
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya * delay; issuing it directly to the zio layer.
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya */
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya if (zio->io_target_timestamp != 0) {
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya hrtime_t now = gethrtime();
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya if (now >= zio->io_target_timestamp) {
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya /*
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya * This IO has already taken longer than the target
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya * delay to complete, so we don't want to delay it
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya * any longer; we "miss" the delay and issue it
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya * directly to the zio layer. This is likely due to
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya * the target latency being set to a value less than
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya * the underlying hardware can satisfy (e.g. delay
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya * set to 1ms, but the disks take 10ms to complete an
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya * IO request).
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya */
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya DTRACE_PROBE2(zio__delay__miss, zio_t *, zio,
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya hrtime_t, now);
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya zio_interrupt(zio);
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya } else {
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya hrtime_t diff = zio->io_target_timestamp - now;
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya DTRACE_PROBE3(zio__delay__hit, zio_t *, zio,
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya hrtime_t, now, hrtime_t, diff);
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya (void) timeout_generic(CALLOUT_NORMAL,
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya (void (*)(void *))zio_interrupt, zio, diff, 1, 0);
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya }
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya return;
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya }
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya#endif
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya DTRACE_PROBE1(zio__delay__skip, zio_t *, zio);
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya zio_interrupt(zio);
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya}
97e81309571898df9fdd94aab1216dfcf23e060bPrakash Surya
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick/*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * Execute the I/O pipeline until one of the following occurs:
f7170741490edba9d1d9c697c177c887172bc741Will Andrews *
f7170741490edba9d1d9c697c177c887172bc741Will Andrews * (1) the I/O completes
f7170741490edba9d1d9c697c177c887172bc741Will Andrews * (2) the pipeline stalls waiting for dependent child I/Os
f7170741490edba9d1d9c697c177c887172bc741Will Andrews * (3) the I/O issues, so we're waiting for an I/O completion interrupt
f7170741490edba9d1d9c697c177c887172bc741Will Andrews * (4) the I/O is delegated by vdev-level caching or aggregation
f7170741490edba9d1d9c697c177c887172bc741Will Andrews * (5) the I/O is deferred due to vdev-level queueing
f7170741490edba9d1d9c697c177c887172bc741Will Andrews * (6) the I/O is handed off to another thread.
f7170741490edba9d1d9c697c177c887172bc741Will Andrews *
f7170741490edba9d1d9c697c177c887172bc741Will Andrews * In all cases, the pipeline stops whenever there's no CPU work; it never
f7170741490edba9d1d9c697c177c887172bc741Will Andrews * burns a thread in cv_wait().
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick *
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * There's no locking on io_stage because there's no legitimate way
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * for multiple threads to be attempting to process the same I/O.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick */
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickstatic zio_pipe_stage_t *zio_pipeline[];
0a4e9518a44f226be6d39383330b5b1792d2f184gw
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickvoid
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_execute(zio_t *zio)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_executor = curthread;
0a4e9518a44f226be6d39383330b5b1792d2f184gw
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT3U(zio->io_queued_timestamp, >, 0);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick while (zio->io_stage < ZIO_STAGE_DONE) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick enum zio_stage pipeline = zio->io_pipeline;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick enum zio_stage stage = zio->io_stage;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick int rv;
0a4e9518a44f226be6d39383330b5b1792d2f184gw
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(!MUTEX_HELD(&zio->io_lock));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(ISP2(stage));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(zio->io_stall == NULL);
0a4e9518a44f226be6d39383330b5b1792d2f184gw
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick do {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick stage <<= 1;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick } while ((stage & pipeline) == 0);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(stage <= ZIO_STAGE_DONE);
0a4e9518a44f226be6d39383330b5b1792d2f184gw
0a4e9518a44f226be6d39383330b5b1792d2f184gw /*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * If we are in interrupt context and this pipeline stage
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * will grab a config lock that is held across I/O,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * or may wait for an I/O that needs an interrupt thread
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * to complete, issue async to avoid deadlock.
35a5a3587fd94b666239c157d3722745250ccbd7Jonathan Adams *
35a5a3587fd94b666239c157d3722745250ccbd7Jonathan Adams * For VDEV_IO_START, we cut in line so that the io will
35a5a3587fd94b666239c157d3722745250ccbd7Jonathan Adams * be sent to disk promptly.
0a4e9518a44f226be6d39383330b5b1792d2f184gw */
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if ((stage & ZIO_BLOCKING_STAGES) && zio->io_vd == NULL &&
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_taskq_member(zio, ZIO_TASKQ_INTERRUPT)) {
35a5a3587fd94b666239c157d3722745250ccbd7Jonathan Adams boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ?
35a5a3587fd94b666239c157d3722745250ccbd7Jonathan Adams zio_requeue_io_start_cut_in_line : B_FALSE;
35a5a3587fd94b666239c157d3722745250ccbd7Jonathan Adams zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return;
0a4e9518a44f226be6d39383330b5b1792d2f184gw }
0a4e9518a44f226be6d39383330b5b1792d2f184gw
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_stage = stage;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio->io_pipeline_trace |= zio->io_stage;
bf16b11e8deb633dd6c4296d46e92399d1582df4Matthew Ahrens rv = zio_pipeline[highbit64(stage) - 1](zio);
0a4e9518a44f226be6d39383330b5b1792d2f184gw
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (rv == ZIO_PIPELINE_STOP)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return;
0a4e9518a44f226be6d39383330b5b1792d2f184gw
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(rv == ZIO_PIPELINE_CONTINUE);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
0a4e9518a44f226be6d39383330b5b1792d2f184gw}
0a4e9518a44f226be6d39383330b5b1792d2f184gw
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick/*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * ==========================================================================
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * Initiate I/O, either sync or async
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * ==========================================================================
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickint
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_wait(zio_t *zio)
0a4e9518a44f226be6d39383330b5b1792d2f184gw{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick int error;
0a4e9518a44f226be6d39383330b5b1792d2f184gw
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(zio->io_executor == NULL);
0a4e9518a44f226be6d39383330b5b1792d2f184gw
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_waiter = curthread;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT0(zio->io_queued_timestamp);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio->io_queued_timestamp = gethrtime();
e05725b117836db173257fae43fb0746eb857fb5bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_execute(zio);
0a4e9518a44f226be6d39383330b5b1792d2f184gw
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick mutex_enter(&zio->io_lock);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick while (zio->io_executor != NULL)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick cv_wait(&zio->io_cv, &zio->io_lock);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick mutex_exit(&zio->io_lock);
32b87932f3ef0887d873b7f6d2d1943799b2afc0ek
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick error = zio->io_error;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_destroy(zio);
32b87932f3ef0887d873b7f6d2d1943799b2afc0ek
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return (error);
32b87932f3ef0887d873b7f6d2d1943799b2afc0ek}
32b87932f3ef0887d873b7f6d2d1943799b2afc0ek
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickvoid
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_nowait(zio_t *zio)
0a4e9518a44f226be6d39383330b5b1792d2f184gw{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(zio->io_executor == NULL);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore if (zio->io_child_type == ZIO_CHILD_LOGICAL &&
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore zio_unique_parent(zio) == NULL) {
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock /*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * This is a logical async I/O with no parent to wait for it.
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson * We add it to the spa_async_root_zio "Godfather" I/O which
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson * will ensure they complete prior to unloading the pool.
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick spa_t *spa = zio->io_spa;
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson
6f834bc197c703a6568554c889157fb345bac079Matthew Ahrens zio_add_child(spa->spa_async_zio_root[CPU_SEQID], zio);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT0(zio->io_queued_timestamp);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio->io_queued_timestamp = gethrtime();
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_execute(zio);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick}
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick/*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * ==========================================================================
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * Reexecute or suspend/resume failed I/O
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * ==========================================================================
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick */
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickstatic void
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_reexecute(zio_t *pio)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick{
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore zio_t *cio, *cio_next;
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore ASSERT(pio->io_child_type == ZIO_CHILD_LOGICAL);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore ASSERT(pio->io_orig_stage == ZIO_STAGE_OPEN);
f5383399322244b3008a624bcd7e93d67037d177Bill Moore ASSERT(pio->io_gang_leader == NULL);
f5383399322244b3008a624bcd7e93d67037d177Bill Moore ASSERT(pio->io_gang_tree == NULL);
e05725b117836db173257fae43fb0746eb857fb5bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick pio->io_flags = pio->io_orig_flags;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick pio->io_stage = pio->io_orig_stage;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick pio->io_pipeline = pio->io_orig_pipeline;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick pio->io_reexecute = 0;
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson pio->io_flags |= ZIO_FLAG_REEXECUTED;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson pio->io_pipeline_trace = 0;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick pio->io_error = 0;
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore for (int w = 0; w < ZIO_WAIT_TYPES; w++)
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore pio->io_state[w] = 0;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick for (int c = 0; c < ZIO_CHILD_TYPES; c++)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick pio->io_child_error[c] = 0;
0a4e9518a44f226be6d39383330b5b1792d2f184gw
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (IO_IS_ALLOCATING(pio))
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick BP_ZERO(pio->io_bp);
d58459f4b19c5a2644c36745685ba4cd45418287ek
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick /*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * As we reexecute pio's children, new children could be created.
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore * New children go to the head of pio's io_child_list, however,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * so we will (correctly) not reexecute them. The key is that
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore * the remainder of pio's io_child_list, from 'cio_next' onward,
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore * cannot be affected by any side effects of reexecuting 'cio'.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio_link_t *zl = NULL;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson cio_next = zio_walk_children(pio, &zl);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick mutex_enter(&pio->io_lock);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore for (int w = 0; w < ZIO_WAIT_TYPES; w++)
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore pio->io_children[cio->io_child_type][w]++;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick mutex_exit(&pio->io_lock);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore zio_reexecute(cio);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
e05725b117836db173257fae43fb0746eb857fb5bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick /*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * Now that all children have been reexecuted, execute the parent.
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson * We don't reexecute "The Godfather" I/O here as it's the
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson * responsibility of the caller to wait on him.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (!(pio->io_flags & ZIO_FLAG_GODFATHER)) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson pio->io_queued_timestamp = gethrtime();
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson zio_execute(pio);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson }
0a4e9518a44f226be6d39383330b5b1792d2f184gw}
0a4e9518a44f226be6d39383330b5b1792d2f184gw
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickvoid
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_suspend(spa_t *spa, zio_t *zio)
0a4e9518a44f226be6d39383330b5b1792d2f184gw{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_PANIC)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick fm_panic("Pool '%s' has encountered an uncorrectable I/O "
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick "failure and the failure mode property for this pool "
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick "is set to panic.", spa_name(spa));
0a4e9518a44f226be6d39383330b5b1792d2f184gw
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE, spa, NULL, NULL, 0, 0);
0a4e9518a44f226be6d39383330b5b1792d2f184gw
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick mutex_enter(&spa->spa_suspend_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (spa->spa_suspend_zio_root == NULL)
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson spa->spa_suspend_zio_root = zio_root(spa, NULL, NULL,
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson ZIO_FLAG_GODFATHER);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick spa->spa_suspended = B_TRUE;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zio != NULL) {
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(zio != spa->spa_suspend_zio_root);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore ASSERT(zio_unique_parent(zio) == NULL);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(zio->io_stage == ZIO_STAGE_DONE);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_add_child(spa->spa_suspend_zio_root, zio);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick mutex_exit(&spa->spa_suspend_lock);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilsonint
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_resume(spa_t *spa)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick{
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson zio_t *pio;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
b3995adb6d3bcb24a1d4e737d5cfd6773f6df99aahrens /*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * Reexecute all previously suspended i/o.
b3995adb6d3bcb24a1d4e737d5cfd6773f6df99aahrens */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick mutex_enter(&spa->spa_suspend_lock);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick spa->spa_suspended = B_FALSE;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick cv_broadcast(&spa->spa_suspend_cv);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick pio = spa->spa_suspend_zio_root;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick spa->spa_suspend_zio_root = NULL;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick mutex_exit(&spa->spa_suspend_lock);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (pio == NULL)
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson return (0);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson zio_reexecute(pio);
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson return (zio_wait(pio));
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick}
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickvoid
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_resume_wait(spa_t *spa)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick mutex_enter(&spa->spa_suspend_lock);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick while (spa_suspended(spa))
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick cv_wait(&spa->spa_suspend_cv, &spa->spa_suspend_lock);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick mutex_exit(&spa->spa_suspend_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * Gang blocks.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick *
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * A gang block is a collection of small blocks that looks to the DMU
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * like one large block. When zio_dva_allocate() cannot find a block
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * of the requested size, due to either severe fragmentation or the pool
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * being nearly full, it calls zio_write_gang_block() to construct the
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * block from smaller fragments.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick *
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * A gang block consists of a gang header (zio_gbh_phys_t) and up to
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * three (SPA_GBH_NBLKPTRS) gang members. The gang header is just like
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * an indirect block: it's an array of block pointers. It consumes
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * only one sector and hence is allocatable regardless of fragmentation.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * The gang header's bps point to its gang members, which hold the data.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick *
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg>
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * as the verifier to ensure uniqueness of the SHA256 checksum.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * Critically, the gang block bp's blk_cksum is the checksum of the data,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * not the gang header. This ensures that data block signatures (needed for
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * deduplication) are independent of how the block is physically stored.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick *
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * Gang blocks can be nested: a gang member may itself be a gang block.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * Thus every gang block is a tree in which root and all interior nodes are
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * gang headers, and the leaves are normal blocks that contain user data.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * The root of the gang tree is called the gang leader.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick *
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * To perform any operation (read, rewrite, free, claim) on a gang block,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * zio_gang_assemble() first assembles the gang tree (minus data leaves)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * in the io_gang_tree field of the original logical i/o by recursively
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * reading the gang leader and all gang headers below it. This yields
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * an in-core tree containing the contents of every gang header and the
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * bps for every constituent of the gang block.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick *
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * With the gang tree now assembled, zio_gang_issue() just walks the gang tree
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * and invokes a callback on each bp. To free a gang block, zio_gang_issue()
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * zio_claim_gang() provides a similarly trivial wrapper for zio_claim().
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * zio_read_gang() is a wrapper around zio_read() that omits reading gang
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * headers, since we already have those in io_gang_tree. zio_rewrite_gang()
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite()
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * of the gang header plus zio_checksum_compute() of the data to update the
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * gang header's blk_cksum as described above.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick *
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * The two-phase assemble/issue model solves the problem of partial failure --
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * what if you'd freed part of a gang block but then couldn't read the
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * gang header for another part? Assembling the entire gang tree first
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * ensures that all the necessary gang header I/O has succeeded before
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * starting the actual work of free, claim, or write. Once the gang tree
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * is assembled, free and claim are in-memory operations that cannot fail.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick *
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * In the event that a gang write fails, zio_dva_unallocate() walks the
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * gang tree to immediately free (i.e. insert back into the space map)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * everything we've allocated. This ensures that we don't get ENOSPC
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * errors during repeated suspend/resume cycles due to a flaky device.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick *
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * Gang rewrites only happen during sync-to-convergence. If we can't assemble
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * the gang tree, we won't modify the block, so we can safely defer the free
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * (knowing that the block is still intact). If we *can* assemble the gang
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * each constituent bp and we can allocate a new block on the next sync pass.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick *
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * In all cases, the gang tree allows complete recovery from partial failure.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickstatic zio_t *
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_read_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (gn != NULL)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return (pio);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return (zio_read(pio, pio->io_spa, bp, data, BP_GET_PSIZE(bp),
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick NULL, NULL, pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick &pio->io_bookmark));
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick}
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_t *
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_rewrite_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_t *zio;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (gn != NULL) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick gn->gn_gbh, SPA_GANGBLOCKSIZE, NULL, NULL, pio->io_priority,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
fa9e4066f08beec538e775443c5be79dd423fcabahrens /*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * As we rewrite each gang header, the pipeline will compute
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * a new gang block header checksum for it; but no one will
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * compute a new data checksum, so we do that here. The one
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * exception is the gang leader: the pipeline already computed
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * its data checksum because that stage precedes gang assembly.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * (Presently, nothing actually uses interior data checksums;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * this is just good hygiene.)
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
f5383399322244b3008a624bcd7e93d67037d177Bill Moore if (gn != pio->io_gang_leader->io_gang_tree) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_checksum_compute(zio, BP_GET_CHECKSUM(bp),
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick data, BP_GET_PSIZE(bp));
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick /*
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * If we are here to damage data for testing purposes,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * leave the GBH alone so that we can detect the damage.
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick */
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (pio->io_gang_leader->io_flags & ZIO_FLAG_INDUCE_DAMAGE)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
fa9e4066f08beec538e775443c5be79dd423fcabahrens } else {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick data, BP_GET_PSIZE(bp), NULL, NULL, pio->io_priority,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return (zio);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick/* ARGSUSED */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_t *
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_free_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (zio_free_sync(pio, pio->io_spa, pio->io_txg, bp,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ZIO_GANG_CHILD_FLAGS(pio)));
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick/* ARGSUSED */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_t *
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_claim_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return (zio_claim(pio, pio->io_spa, pio->io_txg, bp,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio)));
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickstatic zio_gang_issue_func_t *zio_gang_issue_func[ZIO_TYPES] = {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick NULL,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_read_gang,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_rewrite_gang,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_free_gang,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_claim_gang,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick NULL
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick};
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickstatic void zio_gang_tree_assemble_done(zio_t *zio);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickstatic zio_gang_node_t *
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_gang_node_alloc(zio_gang_node_t **gnpp)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_gang_node_t *gn;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(*gnpp == NULL);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick gn = kmem_zalloc(sizeof (*gn), KM_SLEEP);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick gn->gn_gbh = zio_buf_alloc(SPA_GANGBLOCKSIZE);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick *gnpp = gn;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return (gn);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensstatic void
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_gang_node_free(zio_gang_node_t **gnpp)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_gang_node_t *gn = *gnpp;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(gn->gn_child[g] == NULL);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_buf_free(gn->gn_gbh, SPA_GANGBLOCKSIZE);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick kmem_free(gn, sizeof (*gn));
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick *gnpp = NULL;
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickstatic void
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_gang_tree_free(zio_gang_node_t **gnpp)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_gang_node_t *gn = *gnpp;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (gn == NULL)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_gang_tree_free(&gn->gn_child[g]);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_gang_node_free(gnpp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickstatic void
f5383399322244b3008a624bcd7e93d67037d177Bill Moorezio_gang_tree_assemble(zio_t *gio, blkptr_t *bp, zio_gang_node_t **gnpp)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_gang_node_t *gn = zio_gang_node_alloc(gnpp);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
f5383399322244b3008a624bcd7e93d67037d177Bill Moore ASSERT(gio->io_gang_leader == gio);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(BP_IS_GANG(bp));
fa9e4066f08beec538e775443c5be79dd423fcabahrens
f5383399322244b3008a624bcd7e93d67037d177Bill Moore zio_nowait(zio_read(gio, gio->io_spa, bp, gn->gn_gbh,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick SPA_GANGBLOCKSIZE, zio_gang_tree_assemble_done, gn,
f5383399322244b3008a624bcd7e93d67037d177Bill Moore gio->io_priority, ZIO_GANG_CHILD_FLAGS(gio), &gio->io_bookmark));
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickstatic void
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_gang_tree_assemble_done(zio_t *zio)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick{
f5383399322244b3008a624bcd7e93d67037d177Bill Moore zio_t *gio = zio->io_gang_leader;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_gang_node_t *gn = zio->io_private;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick blkptr_t *bp = zio->io_bp;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
f5383399322244b3008a624bcd7e93d67037d177Bill Moore ASSERT(gio == zio_unique_parent(zio));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(zio->io_child_count == 0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zio->io_error)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (BP_SHOULD_BYTESWAP(bp))
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick byteswap_uint64_array(zio->io_data, zio->io_size);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(zio->io_data == gn->gn_gbh);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(zio->io_size == SPA_GANGBLOCKSIZE);
6e1f5caa9321646aa4212d48e32a0d241866d85dNeil Perrin ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
e05725b117836db173257fae43fb0746eb857fb5bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (!BP_IS_GANG(gbp))
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick continue;
f5383399322244b3008a624bcd7e93d67037d177Bill Moore zio_gang_tree_assemble(gio, gbp, &gn->gn_child[g]);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickstatic void
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_gang_tree_issue(zio_t *pio, zio_gang_node_t *gn, blkptr_t *bp, void *data)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
f5383399322244b3008a624bcd7e93d67037d177Bill Moore zio_t *gio = pio->io_gang_leader;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_t *zio;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(BP_IS_GANG(bp) == !!gn);
f5383399322244b3008a624bcd7e93d67037d177Bill Moore ASSERT(BP_GET_CHECKSUM(bp) == BP_GET_CHECKSUM(gio->io_bp));
f5383399322244b3008a624bcd7e93d67037d177Bill Moore ASSERT(BP_GET_LSIZE(bp) == BP_GET_PSIZE(bp) || gn == gio->io_gang_tree);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick /*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * If you're a gang header, your data is in gn->gn_gbh.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * If you're a gang member, your data is in 'data' and gn == NULL.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick */
f5383399322244b3008a624bcd7e93d67037d177Bill Moore zio = zio_gang_issue_func[gio->io_type](pio, bp, gn, data);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (gn != NULL) {
6e1f5caa9321646aa4212d48e32a0d241866d85dNeil Perrin ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (BP_IS_HOLE(gbp))
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick continue;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_gang_tree_issue(zio, gn->gn_child[g], gbp, data);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick data = (char *)data + BP_GET_PSIZE(gbp);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
f5383399322244b3008a624bcd7e93d67037d177Bill Moore if (gn == gio->io_gang_tree)
f5383399322244b3008a624bcd7e93d67037d177Bill Moore ASSERT3P((char *)gio->io_data + gio->io_size, ==, data);
e05725b117836db173257fae43fb0746eb857fb5bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zio != pio)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_nowait(zio);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e05725b117836db173257fae43fb0746eb857fb5bonwickstatic int
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_gang_assemble(zio_t *zio)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick blkptr_t *bp = zio->io_bp;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
f5383399322244b3008a624bcd7e93d67037d177Bill Moore ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == NULL);
f5383399322244b3008a624bcd7e93d67037d177Bill Moore ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
f5383399322244b3008a624bcd7e93d67037d177Bill Moore
f5383399322244b3008a624bcd7e93d67037d177Bill Moore zio->io_gang_leader = zio;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_gang_tree_assemble(zio, bp, &zio->io_gang_tree);
e05725b117836db173257fae43fb0746eb857fb5bonwick
e05725b117836db173257fae43fb0746eb857fb5bonwick return (ZIO_PIPELINE_CONTINUE);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e05725b117836db173257fae43fb0746eb857fb5bonwickstatic int
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_gang_issue(zio_t *zio)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick blkptr_t *bp = zio->io_bp;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE))
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return (ZIO_PIPELINE_STOP);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
f5383399322244b3008a624bcd7e93d67037d177Bill Moore ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == zio);
f5383399322244b3008a624bcd7e93d67037d177Bill Moore ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zio->io_child_error[ZIO_CHILD_GANG] == 0)
f5383399322244b3008a624bcd7e93d67037d177Bill Moore zio_gang_tree_issue(zio, zio->io_gang_tree, bp, zio->io_data);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick else
f5383399322244b3008a624bcd7e93d67037d177Bill Moore zio_gang_tree_free(&zio->io_gang_tree);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
e05725b117836db173257fae43fb0746eb857fb5bonwick
e05725b117836db173257fae43fb0746eb857fb5bonwick return (ZIO_PIPELINE_CONTINUE);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensstatic void
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_write_gang_member_ready(zio_t *zio)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore zio_t *pio = zio_unique_parent(zio);
f5383399322244b3008a624bcd7e93d67037d177Bill Moore zio_t *gio = zio->io_gang_leader;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm dva_t *cdva = zio->io_bp->blk_dva;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm dva_t *pdva = pio->io_bp->blk_dva;
fa9e4066f08beec538e775443c5be79dd423fcabahrens uint64_t asize;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (BP_IS_HOLE(zio->io_bp))
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(BP_IS_HOLE(&zio->io_bp_orig));
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(zio->io_child_type == ZIO_CHILD_GANG);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT3U(zio->io_prop.zp_copies, ==, gio->io_prop.zp_copies);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(zio->io_bp));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT3U(pio->io_prop.zp_copies, <=, BP_GET_NDVAS(pio->io_bp));
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm ASSERT3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp));
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_enter(&pio->io_lock);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick for (int d = 0; d < BP_GET_NDVAS(zio->io_bp); d++) {
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm ASSERT(DVA_GET_GANG(&pdva[d]));
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm asize = DVA_GET_ASIZE(&pdva[d]);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm asize += DVA_GET_ASIZE(&cdva[d]);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm DVA_SET_ASIZE(&pdva[d], asize);
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm }
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_exit(&pio->io_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
0a4e9518a44f226be6d39383330b5b1792d2f184gwstatic int
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_write_gang_block(zio_t *pio)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick spa_t *spa = pio->io_spa;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson metaslab_class_t *mc = spa_normal_class(spa);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick blkptr_t *bp = pio->io_bp;
f5383399322244b3008a624bcd7e93d67037d177Bill Moore zio_t *gio = pio->io_gang_leader;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_t *zio;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_gang_node_t *gn, **gnpp;
fa9e4066f08beec538e775443c5be79dd423fcabahrens zio_gbh_phys_t *gbh;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick uint64_t txg = pio->io_txg;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick uint64_t resid = pio->io_size;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick uint64_t lsize;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick int copies = gio->io_prop.zp_copies;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick int gbh_copies = MIN(copies + 1, spa_max_replication(spa));
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_prop_t zp;
fa9e4066f08beec538e775443c5be79dd423fcabahrens int error;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson int flags = METASLAB_HINTBP_FAVOR | METASLAB_GANG_HEADER;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(!(pio->io_flags & ZIO_FLAG_NODATA));
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson flags |= METASLAB_ASYNC_ALLOC;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson VERIFY(refcount_held(&mc->mc_alloc_slots, pio));
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson /*
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * The logical zio has already placed a reservation for
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * 'copies' allocation slots but gang blocks may require
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * additional copies. These additional copies
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * (i.e. gbh_copies - copies) are guaranteed to succeed
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * since metaslab_class_throttle_reserve() always allows
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * additional reservations for gang blocks.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson VERIFY(metaslab_class_throttle_reserve(mc, gbh_copies - copies,
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson pio, flags));
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson }
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson error = metaslab_alloc(spa, mc, SPA_GANGBLOCKSIZE,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson bp, gbh_copies, txg, pio == gio ? NULL : gio->io_bp, flags,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson &pio->io_alloc_list, pio);
e05725b117836db173257fae43fb0746eb857fb5bonwick if (error) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(!(pio->io_flags & ZIO_FLAG_NODATA));
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson /*
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * If we failed to allocate the gang block header then
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * we remove any additional allocation reservations that
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * we placed here. The original reservation will
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * be removed when the logical I/O goes to the ready
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * stage.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson metaslab_class_throttle_unreserve(mc,
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson gbh_copies - copies, pio);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson }
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick pio->io_error = error;
e05725b117836db173257fae43fb0746eb857fb5bonwick return (ZIO_PIPELINE_CONTINUE);
e05725b117836db173257fae43fb0746eb857fb5bonwick }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
f5383399322244b3008a624bcd7e93d67037d177Bill Moore if (pio == gio) {
f5383399322244b3008a624bcd7e93d67037d177Bill Moore gnpp = &gio->io_gang_tree;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick } else {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick gnpp = pio->io_private;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(pio->io_ready == zio_write_gang_member_ready);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick gn = zio_gang_node_alloc(gnpp);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick gbh = gn->gn_gbh;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick bzero(gbh, SPA_GANGBLOCKSIZE);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick /*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * Create the gang header.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio = zio_rewrite(pio, spa, txg, bp, gbh, SPA_GANGBLOCKSIZE, NULL, NULL,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick /*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * Create and nowait the gang children.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick for (int g = 0; resid != 0; resid -= lsize, g++) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick lsize = P2ROUNDUP(resid / (SPA_GBH_NBLKPTRS - g),
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick SPA_MINBLOCKSIZE);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(lsize >= SPA_MINBLOCKSIZE && lsize <= resid);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
f5383399322244b3008a624bcd7e93d67037d177Bill Moore zp.zp_checksum = gio->io_prop.zp_checksum;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zp.zp_compress = ZIO_COMPRESS_OFF;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zp.zp_type = DMU_OT_NONE;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zp.zp_level = 0;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zp.zp_copies = gio->io_prop.zp_copies;
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson zp.zp_dedup = B_FALSE;
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson zp.zp_dedup_verify = B_FALSE;
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson zp.zp_nopwrite = B_FALSE;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio_t *cio = zio_write(zio, spa, txg, &gbh->zg_blkptr[g],
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel (char *)pio->io_data + (pio->io_size - resid), lsize, lsize,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel &zp, zio_write_gang_member_ready, NULL, NULL, NULL,
8df0bcf0df7622a075cc6e52f659d2fcfdd08cdcPaul Dagnelie &gn->gn_child[g], pio->io_priority,
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(!(pio->io_flags & ZIO_FLAG_NODATA));
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson /*
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * Gang children won't throttle but we should
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * account for their work, so reserve an allocation
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * slot for them here.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson VERIFY(metaslab_class_throttle_reserve(mc,
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zp.zp_copies, cio, flags));
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson }
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio_nowait(cio);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
e05725b117836db173257fae43fb0746eb857fb5bonwick
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm /*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * Set pio's pipeline to just wait for zio to finish.
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick pio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_nowait(zio);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return (ZIO_PIPELINE_CONTINUE);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson/*
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * The zio_nop_write stage in the pipeline determines if allocating a
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * new bp is necessary. The nopwrite feature can handle writes in
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * either syncing or open context (i.e. zil writes) and as a result is
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * mutually exclusive with dedup.
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens *
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * By leveraging a cryptographically secure checksum, such as SHA256, we
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * can compare the checksums of the new data and the old to determine if
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * allocating a new block is required. Note that our requirements for
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * cryptographic strength are fairly weak: there can't be any accidental
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * hash collisions, but we don't need to be secure against intentional
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * (malicious) collisions. To trigger a nopwrite, you have to be able
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * to write the file to begin with, and triggering an incorrect (hash
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * collision) nopwrite is no worse than simply writing to the file.
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * That said, there are no known attacks against the checksum algorithms
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * used for nopwrite, assuming that the salt and the checksums
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * themselves remain secret.
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson */
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilsonstatic int
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilsonzio_nop_write(zio_t *zio)
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson{
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson blkptr_t *bp = zio->io_bp;
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson blkptr_t *bp_orig = &zio->io_bp_orig;
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson zio_prop_t *zp = &zio->io_prop;
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson ASSERT(BP_GET_LEVEL(bp) == 0);
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson ASSERT(zp->zp_nopwrite);
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson ASSERT(!zp->zp_dedup);
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson ASSERT(zio->io_bp_override == NULL);
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson ASSERT(IO_IS_ALLOCATING(zio));
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson /*
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson * Check to see if the original bp and the new bp have matching
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson * characteristics (i.e. same checksum, compression algorithms, etc).
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson * If they don't then just continue with the pipeline which will
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson * allocate a new bp.
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson */
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson if (BP_IS_HOLE(bp_orig) ||
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens !(zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_flags &
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ZCHECKSUM_FLAG_NOPWRITE) ||
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson BP_GET_CHECKSUM(bp) != BP_GET_CHECKSUM(bp_orig) ||
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson BP_GET_COMPRESS(bp) != BP_GET_COMPRESS(bp_orig) ||
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson BP_GET_DEDUP(bp) != BP_GET_DEDUP(bp_orig) ||
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson zp->zp_copies != BP_GET_NDVAS(bp_orig))
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson return (ZIO_PIPELINE_CONTINUE);
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson /*
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson * If the checksums match then reset the pipeline so that we
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson * avoid allocating a new bp and issuing any I/O.
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson */
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson if (ZIO_CHECKSUM_EQUAL(bp->blk_cksum, bp_orig->blk_cksum)) {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ASSERT(zio_checksum_table[zp->zp_checksum].ci_flags &
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ZCHECKSUM_FLAG_NOPWRITE);
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson ASSERT3U(BP_GET_PSIZE(bp), ==, BP_GET_PSIZE(bp_orig));
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson ASSERT3U(BP_GET_LSIZE(bp), ==, BP_GET_LSIZE(bp_orig));
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson ASSERT(zp->zp_compress != ZIO_COMPRESS_OFF);
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson ASSERT(bcmp(&bp->blk_prop, &bp_orig->blk_prop,
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson sizeof (uint64_t)) == 0);
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson *bp = *bp_orig;
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson zio->io_flags |= ZIO_FLAG_NOPWRITE;
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson }
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson return (ZIO_PIPELINE_CONTINUE);
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson}
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * Dedup
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickstatic void
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickzio_ddt_child_read_done(zio_t *zio)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick blkptr_t *bp = zio->io_bp;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_entry_t *dde = zio->io_private;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_phys_t *ddp;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_t *pio = zio_unique_parent(zio);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick mutex_enter(&pio->io_lock);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddp = ddt_phys_select(dde, bp);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (zio->io_error == 0)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_phys_clear(ddp); /* this ddp doesn't need repair */
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (zio->io_error == 0 && dde->dde_repair_data == NULL)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dde->dde_repair_data = zio->io_data;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick else
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_buf_free(zio->io_data, zio->io_size);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick mutex_exit(&pio->io_lock);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickstatic int
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickzio_ddt_read_start(zio_t *zio)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick blkptr_t *bp = zio->io_bp;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(BP_GET_DEDUP(bp));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (zio->io_child_error[ZIO_CHILD_DDT]) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_t *ddt = ddt_select(zio->io_spa, bp);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_entry_t *dde = ddt_repair_start(ddt, bp);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_phys_t *ddp = dde->dde_phys;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_phys_t *ddp_self = ddt_phys_select(dde, bp);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick blkptr_t blk;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(zio->io_vsd == NULL);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_vsd = dde;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (ddp_self == NULL)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (ZIO_PIPELINE_CONTINUE);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (ddp->ddp_phys_birth == 0 || ddp == ddp_self)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick continue;
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick ddt_bp_create(ddt->ddt_checksum, &dde->dde_key, ddp,
bbfd46c40e81c7d954cec28db66453ec5ab44613Jeff Bonwick &blk);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_nowait(zio_read(zio, zio->io_spa, &blk,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_buf_alloc(zio->io_size), zio->io_size,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_ddt_child_read_done, dde, zio->io_priority,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ZIO_DDT_CHILD_FLAGS(zio) | ZIO_FLAG_DONT_PROPAGATE,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick &zio->io_bookmark));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (ZIO_PIPELINE_CONTINUE);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_nowait(zio_read(zio, zio->io_spa, bp,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_data, zio->io_size, NULL, NULL, zio->io_priority,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (ZIO_PIPELINE_CONTINUE);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickstatic int
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickzio_ddt_read_done(zio_t *zio)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick blkptr_t *bp = zio->io_bp;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_DONE))
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (ZIO_PIPELINE_STOP);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(BP_GET_DEDUP(bp));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (zio->io_child_error[ZIO_CHILD_DDT]) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_t *ddt = ddt_select(zio->io_spa, bp);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_entry_t *dde = zio->io_vsd;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (ddt == NULL) {
b16da2e29e074fb6eaeadc4fd7d17ae7340ba240George Wilson ASSERT(spa_load_state(zio->io_spa) != SPA_LOAD_NONE);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (ZIO_PIPELINE_CONTINUE);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (dde == NULL) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_stage = ZIO_STAGE_DDT_READ_START >> 1;
35a5a3587fd94b666239c157d3722745250ccbd7Jonathan Adams zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (ZIO_PIPELINE_STOP);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (dde->dde_repair_data != NULL) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick bcopy(dde->dde_repair_data, zio->io_data, zio->io_size);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_child_error[ZIO_CHILD_DDT] = 0;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_repair_done(ddt, dde);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_vsd = NULL;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(zio->io_vsd == NULL);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (ZIO_PIPELINE_CONTINUE);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickstatic boolean_t
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickzio_ddt_collision(zio_t *zio, ddt_t *ddt, ddt_entry_t *dde)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick spa_t *spa = zio->io_spa;
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel boolean_t do_raw = (zio->io_flags & ZIO_FLAG_RAW);
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel /* We should never get a raw, override zio */
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel ASSERT(!(zio->io_bp_override && do_raw));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick /*
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * Note: we compare the original data, not the transformed data,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * because when zio->io_bp is an override bp, we will not have
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * pushed the I/O transforms. That's an important optimization
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * because otherwise we'd compress/encrypt all dmu_sync() data twice.
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick */
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_t *lio = dde->dde_lead_zio[p];
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (lio != NULL) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (lio->io_orig_size != zio->io_orig_size ||
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick bcmp(zio->io_orig_data, lio->io_orig_data,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_orig_size) != 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_phys_t *ddp = &dde->dde_phys[p];
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (ddp->ddp_phys_birth != 0) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick arc_buf_t *abuf = NULL;
7adb730b589e553bf3b1ccfd9bae2df91c5c1061George Wilson arc_flags_t aflags = ARC_FLAG_WAIT;
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick blkptr_t blk = *zio->io_bp;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick int error;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_bp_fill(ddp, &blk, ddp->ddp_phys_birth);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_exit(ddt);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel /*
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel * Intuitively, it would make more sense to compare
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel * io_data than io_orig_data in the raw case since you
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel * don't want to look at any transformations that have
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel * happened to the data. However, for raw I/Os the
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel * data will actually be the same in io_data and
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel * io_orig_data, so all we have to do is issue this as
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel * a raw ARC read.
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel */
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel if (do_raw) {
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel zio_flags |= ZIO_FLAG_RAW;
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel ASSERT3U(zio->io_size, ==, zio->io_orig_size);
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel ASSERT0(bcmp(zio->io_data, zio->io_orig_data,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel zio->io_size));
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel ASSERT3P(zio->io_transform_stack, ==, NULL);
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel }
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel
1b912ec7100c10e7243bf0879af0fe580e08c73dGeorge Wilson error = arc_read(NULL, spa, &blk,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick arc_getbuf_func, &abuf, ZIO_PRIORITY_SYNC_READ,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel zio_flags, &aflags, &zio->io_bookmark);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (error == 0) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (arc_buf_size(abuf) != zio->io_orig_size ||
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick bcmp(abuf->b_data, zio->io_orig_data,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_orig_size) != 0)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens error = SET_ERROR(EEXIST);
dcbf3bd6a1f1360fc1afcee9e22c6dcff7844bf2George Wilson arc_buf_destroy(abuf, &abuf);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_enter(ddt);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (error != 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (B_FALSE);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickstatic void
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickzio_ddt_child_write_ready(zio_t *zio)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick int p = zio->io_prop.zp_copies;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_entry_t *dde = zio->io_private;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_phys_t *ddp = &dde->dde_phys[p];
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_t *pio;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (zio->io_error)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_enter(ddt);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(dde->dde_lead_zio[p] == zio);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_phys_fill(ddp, zio->io_bp);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio_link_t *zl = NULL;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson while ((pio = zio_walk_parents(zio, &zl)) != NULL)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_bp_fill(ddp, pio->io_bp, zio->io_txg);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_exit(ddt);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickstatic void
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickzio_ddt_child_write_done(zio_t *zio)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick int p = zio->io_prop.zp_copies;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_entry_t *dde = zio->io_private;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_phys_t *ddp = &dde->dde_phys[p];
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_enter(ddt);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(ddp->ddp_refcnt == 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(dde->dde_lead_zio[p] == zio);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dde->dde_lead_zio[p] = NULL;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (zio->io_error == 0) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio_link_t *zl = NULL;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson while (zio_walk_parents(zio, &zl) != NULL)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_phys_addref(ddp);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick } else {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_phys_clear(ddp);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_exit(ddt);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickstatic void
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickzio_ddt_ditto_write_done(zio_t *zio)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick int p = DDT_PHYS_DITTO;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_prop_t *zp = &zio->io_prop;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick blkptr_t *bp = zio->io_bp;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_t *ddt = ddt_select(zio->io_spa, bp);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_entry_t *dde = zio->io_private;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_phys_t *ddp = &dde->dde_phys[p];
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_key_t *ddk = &dde->dde_key;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_enter(ddt);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(ddp->ddp_refcnt == 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(dde->dde_lead_zio[p] == zio);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dde->dde_lead_zio[p] = NULL;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (zio->io_error == 0) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(ZIO_CHECKSUM_EQUAL(bp->blk_cksum, ddk->ddk_cksum));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(zp->zp_copies < SPA_DVAS_PER_BP);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(zp->zp_copies == BP_GET_NDVAS(bp) - BP_IS_GANG(bp));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (ddp->ddp_phys_birth != 0)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_phys_free(ddt, ddk, ddp, zio->io_txg);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_phys_fill(ddp, bp);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_exit(ddt);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickstatic int
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickzio_ddt_write(zio_t *zio)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick spa_t *spa = zio->io_spa;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick blkptr_t *bp = zio->io_bp;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick uint64_t txg = zio->io_txg;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_prop_t *zp = &zio->io_prop;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick int p = zp->zp_copies;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick int ditto_copies;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_t *cio = NULL;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_t *dio = NULL;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_t *ddt = ddt_select(spa, bp);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_entry_t *dde;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_phys_t *ddp;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(BP_GET_DEDUP(bp));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(BP_GET_CHECKSUM(bp) == zp->zp_checksum);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(BP_IS_HOLE(bp) || zio->io_bp_override);
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel ASSERT(!(zio->io_bp_override && (zio->io_flags & ZIO_FLAG_RAW)));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_enter(ddt);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dde = ddt_lookup(ddt, bp, B_TRUE);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddp = &dde->dde_phys[p];
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (zp->zp_dedup_verify && zio_ddt_collision(zio, ddt, dde)) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick /*
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * If we're using a weak checksum, upgrade to a strong checksum
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * and try again. If we're already using a strong checksum,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * we can't resolve it, so just convert to an ordinary write.
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * (And automatically e-mail a paper to Nature?)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (!(zio_checksum_table[zp->zp_checksum].ci_flags &
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ZCHECKSUM_FLAG_DEDUP)) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zp->zp_checksum = spa_dedup_checksum(spa);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_pop_transforms(zio);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_stage = ZIO_STAGE_OPEN;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick BP_ZERO(bp);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick } else {
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson zp->zp_dedup = B_FALSE;
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel BP_SET_DEDUP(bp, B_FALSE);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel ASSERT(!BP_GET_DEDUP(bp));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_pipeline = ZIO_WRITE_PIPELINE;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_exit(ddt);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (ZIO_PIPELINE_CONTINUE);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ditto_copies = ddt_ditto_copies_needed(ddt, dde, ddp);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(ditto_copies < SPA_DVAS_PER_BP);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (ditto_copies > ddt_ditto_copies_present(dde) &&
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dde->dde_lead_zio[DDT_PHYS_DITTO] == NULL) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_prop_t czp = *zp;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick czp.zp_copies = ditto_copies;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick /*
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * If we arrived here with an override bp, we won't have run
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * the transform stack, so we won't have the data we need to
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * generate a child i/o. So, toss the override bp and restart.
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * This is safe, because using the override bp is just an
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * optimization; and it's rare, so the cost doesn't matter.
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick */
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (zio->io_bp_override) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_pop_transforms(zio);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_stage = ZIO_STAGE_OPEN;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_pipeline = ZIO_WRITE_PIPELINE;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_bp_override = NULL;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick BP_ZERO(bp);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_exit(ddt);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (ZIO_PIPELINE_CONTINUE);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dio = zio_write(zio, spa, txg, bp, zio->io_orig_data,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel zio->io_orig_size, zio->io_orig_size, &czp, NULL, NULL,
8df0bcf0df7622a075cc6e52f659d2fcfdd08cdcPaul Dagnelie NULL, zio_ddt_ditto_write_done, dde, zio->io_priority,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_push_transform(dio, zio->io_data, zio->io_size, 0, NULL);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dde->dde_lead_zio[DDT_PHYS_DITTO] = dio;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (ddp->ddp_phys_birth != 0 || dde->dde_lead_zio[p] != NULL) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (ddp->ddp_phys_birth != 0)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_bp_fill(ddp, bp, txg);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (dde->dde_lead_zio[p] != NULL)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_add_child(zio, dde->dde_lead_zio[p]);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick else
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_phys_addref(ddp);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick } else if (zio->io_bp_override) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(bp->blk_birth == txg);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(BP_EQUAL(bp, zio->io_bp_override));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_phys_fill(ddp, bp);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_phys_addref(ddp);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick } else {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick cio = zio_write(zio, spa, txg, bp, zio->io_orig_data,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel zio->io_orig_size, zio->io_orig_size, zp,
8df0bcf0df7622a075cc6e52f659d2fcfdd08cdcPaul Dagnelie zio_ddt_child_write_ready, NULL, NULL,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_ddt_child_write_done, dde, zio->io_priority,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_push_transform(cio, zio->io_data, zio->io_size, 0, NULL);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dde->dde_lead_zio[p] = cio;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_exit(ddt);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (cio)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_nowait(cio);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (dio)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_nowait(dio);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (ZIO_PIPELINE_CONTINUE);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Lingddt_entry_t *freedde; /* for debugging */
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickstatic int
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickzio_ddt_free(zio_t *zio)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick spa_t *spa = zio->io_spa;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick blkptr_t *bp = zio->io_bp;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_t *ddt = ddt_select(spa, bp);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_entry_t *dde;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_phys_t *ddp;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(BP_GET_DEDUP(bp));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_enter(ddt);
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling freedde = dde = ddt_lookup(ddt, bp, B_TRUE);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddp = ddt_phys_select(dde, bp);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_phys_decref(ddp);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ddt_exit(ddt);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick return (ZIO_PIPELINE_CONTINUE);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick}
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick/*
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * ==========================================================================
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * Allocate and free blocks
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * ==========================================================================
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonstatic zio_t *
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonzio_io_to_allocate(spa_t *spa)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson{
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio_t *zio;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(MUTEX_HELD(&spa->spa_alloc_lock));
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio = avl_first(&spa->spa_alloc_tree);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (zio == NULL)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return (NULL);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(IO_IS_ALLOCATING(zio));
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson /*
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * Try to place a reservation for this zio. If we're unable to
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * reserve then we throttle.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (!metaslab_class_throttle_reserve(spa_normal_class(spa),
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio->io_prop.zp_copies, zio, 0)) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return (NULL);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson }
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson avl_remove(&spa->spa_alloc_tree, zio);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT3U(zio->io_stage, <, ZIO_STAGE_DVA_ALLOCATE);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return (zio);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson}
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonstatic int
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonzio_dva_throttle(zio_t *zio)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson{
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson spa_t *spa = zio->io_spa;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio_t *nio;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (zio->io_priority == ZIO_PRIORITY_SYNC_WRITE ||
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson !spa_normal_class(zio->io_spa)->mc_alloc_throttle_enabled ||
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio->io_child_type == ZIO_CHILD_GANG ||
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio->io_flags & ZIO_FLAG_NODATA) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return (ZIO_PIPELINE_CONTINUE);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson }
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT3U(zio->io_queued_timestamp, >, 0);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(zio->io_stage == ZIO_STAGE_DVA_THROTTLE);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson mutex_enter(&spa->spa_alloc_lock);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(zio->io_type == ZIO_TYPE_WRITE);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson avl_add(&spa->spa_alloc_tree, zio);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson nio = zio_io_to_allocate(zio->io_spa);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson mutex_exit(&spa->spa_alloc_lock);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (nio == zio)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return (ZIO_PIPELINE_CONTINUE);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (nio != NULL) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT3U(nio->io_queued_timestamp, <=,
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio->io_queued_timestamp);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(nio->io_stage == ZIO_STAGE_DVA_THROTTLE);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson /*
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * We are passing control to a new zio so make sure that
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * it is processed by a different thread. We do this to
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * avoid stack overflows that can occur when parents are
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * throttled and children are making progress. We allow
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * it to go to the head of the taskq since it's already
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * been waiting.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio_taskq_dispatch(nio, ZIO_TASKQ_ISSUE, B_TRUE);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson }
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return (ZIO_PIPELINE_STOP);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson}
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonvoid
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonzio_allocate_dispatch(spa_t *spa)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson{
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio_t *zio;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson mutex_enter(&spa->spa_alloc_lock);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio = zio_io_to_allocate(spa);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson mutex_exit(&spa->spa_alloc_lock);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (zio == NULL)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson return;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT3U(zio->io_stage, ==, ZIO_STAGE_DVA_THROTTLE);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT0(zio->io_error);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_TRUE);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson}
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
e05725b117836db173257fae43fb0746eb857fb5bonwickstatic int
fa9e4066f08beec538e775443c5be79dd423fcabahrenszio_dva_allocate(zio_t *zio)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
8654d0253136055bd4cc2423d87378e8a37f2eb5perrin spa_t *spa = zio->io_spa;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick metaslab_class_t *mc = spa_normal_class(spa);
fa9e4066f08beec538e775443c5be79dd423fcabahrens blkptr_t *bp = zio->io_bp;
fa9e4066f08beec538e775443c5be79dd423fcabahrens int error;
09c9d376e8ccb8fbba74f33cc268964464092b62George Wilson int flags = 0;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
f5383399322244b3008a624bcd7e93d67037d177Bill Moore if (zio->io_gang_leader == NULL) {
f5383399322244b3008a624bcd7e93d67037d177Bill Moore ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
f5383399322244b3008a624bcd7e93d67037d177Bill Moore zio->io_gang_leader = zio;
f5383399322244b3008a624bcd7e93d67037d177Bill Moore }
f5383399322244b3008a624bcd7e93d67037d177Bill Moore
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT(BP_IS_HOLE(bp));
fb09f5aad449c97fe309678f3f604982b563a96fMadhav Suresh ASSERT0(BP_GET_NDVAS(bp));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT3U(zio->io_prop.zp_copies, >, 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT3U(zio->io_prop.zp_copies, <=, spa_max_replication(spa));
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
fa9e4066f08beec538e775443c5be79dd423fcabahrens
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (zio->io_flags & ZIO_FLAG_NODATA) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson flags |= METASLAB_DONT_THROTTLE;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson }
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (zio->io_flags & ZIO_FLAG_GANG_CHILD) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson flags |= METASLAB_GANG_CHILD;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson }
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson flags |= METASLAB_ASYNC_ALLOC;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson }
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick error = metaslab_alloc(spa, mc, zio->io_size, bp,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson zio->io_prop.zp_copies, zio->io_txg, NULL, flags,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson &zio->io_alloc_list, zio);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (error != 0) {
09c9d376e8ccb8fbba74f33cc268964464092b62George Wilson spa_dbgmsg(spa, "%s: metaslab allocation failure: zio %p, "
09c9d376e8ccb8fbba74f33cc268964464092b62George Wilson "size %llu, error %d", spa_name(spa), zio, zio->io_size,
09c9d376e8ccb8fbba74f33cc268964464092b62George Wilson error);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (error == ENOSPC && zio->io_size > SPA_MINBLOCKSIZE)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return (zio_write_gang_block(zio));
fa9e4066f08beec538e775443c5be79dd423fcabahrens zio->io_error = error;
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
e05725b117836db173257fae43fb0746eb857fb5bonwick
e05725b117836db173257fae43fb0746eb857fb5bonwick return (ZIO_PIPELINE_CONTINUE);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e05725b117836db173257fae43fb0746eb857fb5bonwickstatic int
fa9e4066f08beec538e775443c5be79dd423fcabahrenszio_dva_free(zio_t *zio)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick metaslab_free(zio->io_spa, zio->io_bp, zio->io_txg, B_FALSE);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e05725b117836db173257fae43fb0746eb857fb5bonwick return (ZIO_PIPELINE_CONTINUE);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e05725b117836db173257fae43fb0746eb857fb5bonwickstatic int
fa9e4066f08beec538e775443c5be79dd423fcabahrenszio_dva_claim(zio_t *zio)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick int error;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (error)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_error = error;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e05725b117836db173257fae43fb0746eb857fb5bonwick return (ZIO_PIPELINE_CONTINUE);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick/*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * Undo an allocation. This is used by zio_done() when an I/O fails
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * and we want to give back the block we just allocated.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * This handles both normal blocks and gang blocks.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickstatic void
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_dva_unallocate(zio_t *zio, zio_gang_node_t *gn, blkptr_t *bp)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(zio->io_bp_override == NULL);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (!BP_IS_HOLE(bp))
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick metaslab_free(zio->io_spa, bp, bp->blk_birth, B_TRUE);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (gn != NULL) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_dva_unallocate(zio, gn->gn_child[g],
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick &gn->gn_gbh->zg_blkptr[g]);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick}
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick/*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * Try to allocate an intent log block. Return 0 on success, errno on failure.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickint
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickzio_alloc_zil(spa_t *spa, uint64_t txg, blkptr_t *new_bp, blkptr_t *old_bp,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick uint64_t size, boolean_t use_slog)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick{
e09fa4dacfb671e707d50a55ae9b5cc191e1b8cbNeil Perrin int error = 1;
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson zio_alloc_list_t io_alloc_list;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(txg > spa_syncing_txg(spa));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_trace_init(&io_alloc_list);
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson
840345f69c01af33ad169d6b4a6ae7ea78b4218aGeorge Wilson if (use_slog) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick error = metaslab_alloc(spa, spa_log_class(spa), size,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson new_bp, 1, txg, old_bp, METASLAB_HINTBP_AVOID,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson &io_alloc_list, NULL);
840345f69c01af33ad169d6b4a6ae7ea78b4218aGeorge Wilson }
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
840345f69c01af33ad169d6b4a6ae7ea78b4218aGeorge Wilson if (error) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick error = metaslab_alloc(spa, spa_normal_class(spa), size,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson new_bp, 1, txg, old_bp, METASLAB_HINTBP_AVOID,
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson &io_alloc_list, NULL);
840345f69c01af33ad169d6b4a6ae7ea78b4218aGeorge Wilson }
8363e80ae72609660f6090766ca8c2c18aa53f0cGeorge Wilson metaslab_trace_fini(&io_alloc_list);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (error == 0) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick BP_SET_LSIZE(new_bp, size);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick BP_SET_PSIZE(new_bp, size);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF);
6e1f5caa9321646aa4212d48e32a0d241866d85dNeil Perrin BP_SET_CHECKSUM(new_bp,
6e1f5caa9321646aa4212d48e32a0d241866d85dNeil Perrin spa_version(spa) >= SPA_VERSION_SLIM_ZIL
6e1f5caa9321646aa4212d48e32a0d241866d85dNeil Perrin ? ZIO_CHECKSUM_ZILOG2 : ZIO_CHECKSUM_ZILOG);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick BP_SET_LEVEL(new_bp, 0);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick BP_SET_DEDUP(new_bp, 0);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return (error);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick}
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick/*
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * Free an intent log block.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickvoid
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickzio_free_zil(spa_t *spa, uint64_t txg, blkptr_t *bp)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(!BP_IS_GANG(bp));
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_free(spa, txg, bp);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick}
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Read and write to physical devices
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
738f37bc3dcd61e8a893af0f2d466d76690b70ecGeorge Wilson
738f37bc3dcd61e8a893af0f2d466d76690b70ecGeorge Wilson
738f37bc3dcd61e8a893af0f2d466d76690b70ecGeorge Wilson/*
738f37bc3dcd61e8a893af0f2d466d76690b70ecGeorge Wilson * Issue an I/O to the underlying vdev. Typically the issue pipeline
738f37bc3dcd61e8a893af0f2d466d76690b70ecGeorge Wilson * stops after this stage and will resume upon I/O completion.
738f37bc3dcd61e8a893af0f2d466d76690b70ecGeorge Wilson * However, there are instances where the vdev layer may need to
738f37bc3dcd61e8a893af0f2d466d76690b70ecGeorge Wilson * continue the pipeline when an I/O was not issued. Since the I/O
738f37bc3dcd61e8a893af0f2d466d76690b70ecGeorge Wilson * that was sent to the vdev layer might be different than the one
738f37bc3dcd61e8a893af0f2d466d76690b70ecGeorge Wilson * currently active in the pipeline (see vdev_queue_io()), we explicitly
738f37bc3dcd61e8a893af0f2d466d76690b70ecGeorge Wilson * force the underlying vdev layers to call either zio_execute() or
738f37bc3dcd61e8a893af0f2d466d76690b70ecGeorge Wilson * zio_interrupt() to ensure that the pipeline continues with the correct I/O.
738f37bc3dcd61e8a893af0f2d466d76690b70ecGeorge Wilson */
e05725b117836db173257fae43fb0746eb857fb5bonwickstatic int
44cd46cadd9aab751dae6a4023c1cb5bf316d274billmzio_vdev_io_start(zio_t *zio)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens vdev_t *vd = zio->io_vd;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm uint64_t align;
0a4e9518a44f226be6d39383330b5b1792d2f184gw spa_t *spa = zio->io_spa;
0a4e9518a44f226be6d39383330b5b1792d2f184gw
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(zio->io_error == 0);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(zio->io_child_error[ZIO_CHILD_VDEV] == 0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (vd == NULL) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (!(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick spa_config_enter(spa, SCL_ZIO, zio, RW_READER);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick /*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * The mirror_ops handle multiple DVAs in a single BP.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick */
738f37bc3dcd61e8a893af0f2d466d76690b70ecGeorge Wilson vdev_mirror_ops.vdev_op_io_start(zio);
738f37bc3dcd61e8a893af0f2d466d76690b70ecGeorge Wilson return (ZIO_PIPELINE_STOP);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT3P(zio->io_logical, !=, zio);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
44ecc5327ab4ce0750dcca2a17e05566bf2812e2George Wilson /*
44ecc5327ab4ce0750dcca2a17e05566bf2812e2George Wilson * We keep track of time-sensitive I/Os so that the scan thread
44ecc5327ab4ce0750dcca2a17e05566bf2812e2George Wilson * can quickly react to certain workloads. In particular, we care
44ecc5327ab4ce0750dcca2a17e05566bf2812e2George Wilson * about non-scrubbing, top-level reads and writes with the following
44ecc5327ab4ce0750dcca2a17e05566bf2812e2George Wilson * characteristics:
738f37bc3dcd61e8a893af0f2d466d76690b70ecGeorge Wilson * - synchronous writes of user data to non-slog devices
44ecc5327ab4ce0750dcca2a17e05566bf2812e2George Wilson * - any reads of user data
44ecc5327ab4ce0750dcca2a17e05566bf2812e2George Wilson * When these conditions are met, adjust the timestamp of spa_last_io
44ecc5327ab4ce0750dcca2a17e05566bf2812e2George Wilson * which allows the scan thread to adjust its workload accordingly.
44ecc5327ab4ce0750dcca2a17e05566bf2812e2George Wilson */
44ecc5327ab4ce0750dcca2a17e05566bf2812e2George Wilson if (!(zio->io_flags & ZIO_FLAG_SCAN_THREAD) && zio->io_bp != NULL &&
44ecc5327ab4ce0750dcca2a17e05566bf2812e2George Wilson vd == vd->vdev_top && !vd->vdev_islog &&
44ecc5327ab4ce0750dcca2a17e05566bf2812e2George Wilson zio->io_bookmark.zb_objset != DMU_META_OBJSET &&
44ecc5327ab4ce0750dcca2a17e05566bf2812e2George Wilson zio->io_txg != spa_syncing_txg(spa)) {
44ecc5327ab4ce0750dcca2a17e05566bf2812e2George Wilson uint64_t old = spa->spa_last_io;
44ecc5327ab4ce0750dcca2a17e05566bf2812e2George Wilson uint64_t new = ddi_get_lbolt64();
44ecc5327ab4ce0750dcca2a17e05566bf2812e2George Wilson if (old != new)
44ecc5327ab4ce0750dcca2a17e05566bf2812e2George Wilson (void) atomic_cas_64(&spa->spa_last_io, old, new);
44ecc5327ab4ce0750dcca2a17e05566bf2812e2George Wilson }
44ecc5327ab4ce0750dcca2a17e05566bf2812e2George Wilson
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick align = 1ULL << vd->vdev_top->vdev_ashift;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece if (!(zio->io_flags & ZIO_FLAG_PHYSICAL) &&
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece P2PHASE(zio->io_size, align) != 0) {
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece /* Transform logical writes to be a full physical block size. */
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick uint64_t asize = P2ROUNDUP(zio->io_size, align);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick char *abuf = zio_buf_alloc(asize);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(vd == vd->vdev_top);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick if (zio->io_type == ZIO_TYPE_WRITE) {
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick bcopy(zio->io_data, abuf, zio->io_size);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick bzero(abuf + zio->io_size, asize - zio->io_size);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick }
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_push_transform(zio, abuf, asize, asize, zio_subblock);
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick }
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece /*
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece * If this is not a physical io, make sure that it is properly aligned
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece * before proceeding.
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece */
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece if (!(zio->io_flags & ZIO_FLAG_PHYSICAL)) {
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece ASSERT0(P2PHASE(zio->io_offset, align));
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece ASSERT0(P2PHASE(zio->io_size, align));
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece } else {
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece /*
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece * For physical writes, we allow 512b aligned writes and assume
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece * the device will perform a read-modify-write as necessary.
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece */
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece ASSERT0(P2PHASE(zio->io_offset, SPA_MINBLOCKSIZE));
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece ASSERT0(P2PHASE(zio->io_size, SPA_MINBLOCKSIZE));
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece }
2a104a5236475eb73aa41eaaf3ed9f3ccbe0ca55Alex Reece
f9af39bacaaa0f9dda3b75ff6858b9f3988a39afGeorge Wilson VERIFY(zio->io_type != ZIO_TYPE_WRITE || spa_writeable(spa));
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick /*
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick * If this is a repair I/O, and there's no self-healing involved --
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick * that is, we're just resilvering what we expect to resilver --
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick * then don't do the I/O unless zio's txg is actually in vd's DTL.
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick * This prevents spurious resilvering with nested replication.
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick * For example, given a mirror of mirrors, (A+B)+(C+D), if only
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick * A is out of date, we'll read from C+D, then use the data to
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick * resilver A+B -- but we don't actually want to resilver B, just A.
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick * The top-level mirror has no way to know this, so instead we just
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick * discard unnecessary repairs as we work our way down the vdev tree.
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick * The same logic applies to any form of nested replication:
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick * ditto + mirror, RAID-Z + replacing, etc. This covers them all.
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick */
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick if ((zio->io_flags & ZIO_FLAG_IO_REPAIR) &&
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick !(zio->io_flags & ZIO_FLAG_SELF_HEAL) &&
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick zio->io_txg != 0 && /* not a delegated i/o */
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick !vdev_dtl_contains(vd, DTL_PARTIAL, zio->io_txg, 1)) {
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick ASSERT(zio->io_type == ZIO_TYPE_WRITE);
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick zio_vdev_io_bypass(zio);
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick return (ZIO_PIPELINE_CONTINUE);
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (vd->vdev_ops->vdev_op_leaf &&
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick (zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE)) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio))
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore return (ZIO_PIPELINE_CONTINUE);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if ((zio = vdev_queue_io(zio)) == NULL)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return (ZIO_PIPELINE_STOP);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (!vdev_accessible(vd, zio)) {
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens zio->io_error = SET_ERROR(ENXIO);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_interrupt(zio);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return (ZIO_PIPELINE_STOP);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
738f37bc3dcd61e8a893af0f2d466d76690b70ecGeorge Wilson vd->vdev_ops->vdev_op_io_start(zio);
738f37bc3dcd61e8a893af0f2d466d76690b70ecGeorge Wilson return (ZIO_PIPELINE_STOP);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e05725b117836db173257fae43fb0746eb857fb5bonwickstatic int
fa9e4066f08beec538e775443c5be79dd423fcabahrenszio_vdev_io_done(zio_t *zio)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick vdev_t *vd = zio->io_vd;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick vdev_ops_t *ops = vd ? vd->vdev_ops : &vdev_mirror_ops;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick boolean_t unexpected_error = B_FALSE;
e05725b117836db173257fae43fb0746eb857fb5bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE))
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return (ZIO_PIPELINE_STOP);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (vd != NULL && vd->vdev_ops->vdev_op_leaf) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick vdev_queue_io_done(zio);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zio->io_type == ZIO_TYPE_WRITE)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick vdev_cache_write(zio);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zio_injection_enabled && zio->io_error == 0)
8956713aded83a741173fcd4f9ef1c83521fbea9Eric Schrock zio->io_error = zio_handle_device_injection(vd,
8956713aded83a741173fcd4f9ef1c83521fbea9Eric Schrock zio, EIO);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zio_injection_enabled && zio->io_error == 0)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_error = zio_handle_label_injection(zio, EIO);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zio->io_error) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (!vdev_accessible(vd, zio)) {
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens zio->io_error = SET_ERROR(ENXIO);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick } else {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick unexpected_error = B_TRUE;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
51ece83525fa18f5e72627610f480dffc7e492fdeschrock }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ops->vdev_op_io_done(zio);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (unexpected_error)
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore VERIFY(vdev_probe(vd, zio) == NULL);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return (ZIO_PIPELINE_CONTINUE);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams/*
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams * For non-raidz ZIOs, we can just copy aside the bad data read from the
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams * disk, and use that to finish the checksum ereport later.
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams */
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adamsstatic void
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adamszio_vsd_default_cksum_finish(zio_cksum_report_t *zcr,
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams const void *good_buf)
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams{
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams /* no processing needed */
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams zfs_ereport_finish_checksum(zcr, good_buf, zcr->zcr_cbdata, B_FALSE);
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams}
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams/*ARGSUSED*/
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adamsvoid
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adamszio_vsd_default_cksum_report(zio_t *zio, zio_cksum_report_t *zcr, void *ignored)
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams{
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams void *buf = zio_buf_alloc(zio->io_size);
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams bcopy(zio->io_data, buf, zio->io_size);
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams zcr->zcr_cbinfo = zio->io_size;
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams zcr->zcr_cbdata = buf;
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams zcr->zcr_finish = zio_vsd_default_cksum_finish;
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams zcr->zcr_free = zio_buf_free;
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams}
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams
e05725b117836db173257fae43fb0746eb857fb5bonwickstatic int
fa9e4066f08beec538e775443c5be79dd423fcabahrenszio_vdev_io_assess(zio_t *zio)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens vdev_t *vd = zio->io_vd;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE))
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return (ZIO_PIPELINE_STOP);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (vd == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick spa_config_exit(zio->io_spa, SCL_ZIO, zio);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zio->io_vsd != NULL) {
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams zio->io_vsd_ops->vsd_free(zio);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_vsd = NULL;
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick }
ecc2d604e885a75cc75e647b5641af99d5a6f4a6bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zio_injection_enabled && zio->io_error == 0)
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock zio->io_error = zio_handle_fault_injection(zio, EIO);
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock
fa9e4066f08beec538e775443c5be79dd423fcabahrens /*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If the I/O failed, determine whether we should attempt to retry it.
35a5a3587fd94b666239c157d3722745250ccbd7Jonathan Adams *
35a5a3587fd94b666239c157d3722745250ccbd7Jonathan Adams * On retry, we cut in line in the issue queue, since we don't want
35a5a3587fd94b666239c157d3722745250ccbd7Jonathan Adams * compression/checksumming/etc. work to prevent our (cheap) IO reissue.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zio->io_error && vd == NULL &&
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick !(zio->io_flags & (ZIO_FLAG_DONT_RETRY | ZIO_FLAG_IO_RETRY))) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(!(zio->io_flags & ZIO_FLAG_DONT_QUEUE)); /* not a leaf */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(!(zio->io_flags & ZIO_FLAG_IO_BYPASS)); /* not a leaf */
fa9e4066f08beec538e775443c5be79dd423fcabahrens zio->io_error = 0;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_flags |= ZIO_FLAG_IO_RETRY |
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_stage = ZIO_STAGE_VDEV_IO_START >> 1;
35a5a3587fd94b666239c157d3722745250ccbd7Jonathan Adams zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE,
35a5a3587fd94b666239c157d3722745250ccbd7Jonathan Adams zio_requeue_io_start_cut_in_line);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return (ZIO_PIPELINE_STOP);
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick /*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * If we got an error on a leaf device, convert it to ENXIO
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * if the device is not accessible at all.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zio->io_error && vd != NULL && vd->vdev_ops->vdev_op_leaf &&
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick !vdev_accessible(vd, zio))
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens zio->io_error = SET_ERROR(ENXIO);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick /*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * If we can't write to an interior vdev (mirror or RAID-Z),
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * set vdev_cant_write so that we stop trying to allocate from it.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zio->io_error == ENXIO && zio->io_type == ZIO_TYPE_WRITE &&
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens vd != NULL && !vd->vdev_ops->vdev_op_leaf) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick vd->vdev_cant_write = B_TRUE;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
295438ba3230419314faaa889a2616f561658bd5Hans Rosenfeld /*
295438ba3230419314faaa889a2616f561658bd5Hans Rosenfeld * If a cache flush returns ENOTSUP or ENOTTY, we know that no future
295438ba3230419314faaa889a2616f561658bd5Hans Rosenfeld * attempts will ever succeed. In this case we set a persistent bit so
295438ba3230419314faaa889a2616f561658bd5Hans Rosenfeld * that we don't bother with it in the future.
295438ba3230419314faaa889a2616f561658bd5Hans Rosenfeld */
295438ba3230419314faaa889a2616f561658bd5Hans Rosenfeld if ((zio->io_error == ENOTSUP || zio->io_error == ENOTTY) &&
295438ba3230419314faaa889a2616f561658bd5Hans Rosenfeld zio->io_type == ZIO_TYPE_IOCTL &&
295438ba3230419314faaa889a2616f561658bd5Hans Rosenfeld zio->io_cmd == DKIOCFLUSHWRITECACHE && vd != NULL)
295438ba3230419314faaa889a2616f561658bd5Hans Rosenfeld vd->vdev_nowritecache = B_TRUE;
295438ba3230419314faaa889a2616f561658bd5Hans Rosenfeld
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zio->io_error)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (vd != NULL && vd->vdev_ops->vdev_op_leaf &&
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zio->io_physdone != NULL) {
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens ASSERT(!(zio->io_flags & ZIO_FLAG_DELEGATED));
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens ASSERT(zio->io_child_type == ZIO_CHILD_VDEV);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zio->io_physdone(zio->io_logical);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens }
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
e05725b117836db173257fae43fb0746eb857fb5bonwick return (ZIO_PIPELINE_CONTINUE);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid
fa9e4066f08beec538e775443c5be79dd423fcabahrenszio_vdev_io_reissue(zio_t *zio)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT(zio->io_error == 0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_stage >>= 1;
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid
fa9e4066f08beec538e775443c5be79dd423fcabahrenszio_vdev_io_redone(zio_t *zio)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_stage >>= 1;
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid
fa9e4066f08beec538e775443c5be79dd423fcabahrenszio_vdev_io_bypass(zio_t *zio)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT(zio->io_error == 0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens zio->io_flags |= ZIO_FLAG_IO_BYPASS;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS >> 1;
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Generate and verify checksums
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
e05725b117836db173257fae43fb0746eb857fb5bonwickstatic int
fa9e4066f08beec538e775443c5be79dd423fcabahrenszio_checksum_generate(zio_t *zio)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens blkptr_t *bp = zio->io_bp;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick enum zio_checksum checksum;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (bp == NULL) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick /*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * This is zio_write_phys().
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * We're either generating a label checksum, or none at all.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick checksum = zio->io_prop.zp_checksum;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (checksum == ZIO_CHECKSUM_OFF)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return (ZIO_PIPELINE_CONTINUE);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(checksum == ZIO_CHECKSUM_LABEL);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick } else {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (BP_IS_GANG(bp) && zio->io_child_type == ZIO_CHILD_GANG) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(!IO_IS_ALLOCATING(zio));
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick checksum = ZIO_CHECKSUM_GANG_HEADER;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick } else {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick checksum = BP_GET_CHECKSUM(bp);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_checksum_compute(zio, checksum, zio->io_data, zio->io_size);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e05725b117836db173257fae43fb0746eb857fb5bonwick return (ZIO_PIPELINE_CONTINUE);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e05725b117836db173257fae43fb0746eb857fb5bonwickstatic int
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_checksum_verify(zio_t *zio)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams zio_bad_cksum_t info;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick blkptr_t *bp = zio->io_bp;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick int error;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(zio->io_vd != NULL);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (bp == NULL) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick /*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * This is zio_read_phys().
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * We're either verifying a label checksum, or nothing at all.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zio->io_prop.zp_checksum == ZIO_CHECKSUM_OFF)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return (ZIO_PIPELINE_CONTINUE);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(zio->io_prop.zp_checksum == ZIO_CHECKSUM_LABEL);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams if ((error = zio_checksum_error(zio, &info)) != 0) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_error = error;
373dc1cf9a4e0791397a9b268cdac1f664af58a8Matthew Ahrens if (error == ECKSUM &&
373dc1cf9a4e0791397a9b268cdac1f664af58a8Matthew Ahrens !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams zfs_ereport_start_checksum(zio->io_spa,
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams zio->io_vd, zio, zio->io_offset,
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams zio->io_size, NULL, &info);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e05725b117836db173257fae43fb0746eb857fb5bonwick return (ZIO_PIPELINE_CONTINUE);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Called by RAID-Z to ensure we don't compute the checksum twice.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid
fa9e4066f08beec538e775443c5be79dd423fcabahrenszio_checksum_verified(zio_t *zio)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * ==========================================================================
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * Error rank. Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other.
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * An error of 0 indicates success. ENXIO indicates whole-device failure,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * which may be transient (e.g. unplugged) or permament. ECKSUM and EIO
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * indicate errors that are specific to one I/O, and most likely permanent.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * Any other error is presumed to be worse because we weren't expecting it.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * ==========================================================================
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickint
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_worst_error(int e1, int e2)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick static int zio_error_rank[] = { 0, ENXIO, ECKSUM, EIO };
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick int r1, r2;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick for (r1 = 0; r1 < sizeof (zio_error_rank) / sizeof (int); r1++)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (e1 == zio_error_rank[r1])
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick break;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick for (r2 = 0; r2 < sizeof (zio_error_rank) / sizeof (int); r2++)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (e2 == zio_error_rank[r2])
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick break;
44cd46cadd9aab751dae6a4023c1cb5bf316d274billm
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return (r1 > r2 ? e1 : e2);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * I/O completion
fa9e4066f08beec538e775443c5be79dd423fcabahrens * ==========================================================================
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickstatic int
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_ready(zio_t *zio)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick blkptr_t *bp = zio->io_bp;
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore zio_t *pio, *pio_next;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio_link_t *zl = NULL;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_READY) ||
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_READY))
f5383399322244b3008a624bcd7e93d67037d177Bill Moore return (ZIO_PIPELINE_STOP);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
f5383399322244b3008a624bcd7e93d67037d177Bill Moore if (zio->io_ready) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(IO_IS_ALLOCATING(zio));
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp) ||
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson (zio->io_flags & ZIO_FLAG_NOPWRITE));
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(zio->io_children[ZIO_CHILD_GANG][ZIO_WAIT_READY] == 0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_ready(zio);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (bp != NULL && bp != &zio->io_bp_copy)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_bp_copy = *bp;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (zio->io_error != 0) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(IO_IS_ALLOCATING(zio));
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson /*
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * We were unable to allocate anything, unreserve and
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * issue the next I/O to allocate.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson metaslab_class_throttle_unreserve(
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson spa_normal_class(zio->io_spa),
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio->io_prop.zp_copies, zio);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio_allocate_dispatch(zio->io_spa);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson }
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson }
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore mutex_enter(&zio->io_lock);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore zio->io_state[ZIO_WAIT_READY] = 1;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson pio = zio_walk_parents(zio, &zl);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore mutex_exit(&zio->io_lock);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore /*
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore * As we notify zio's parents, new parents could be added.
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore * New parents go to the head of zio's io_parent_list, however,
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore * so we will (correctly) not notify them. The remainder of zio's
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore * io_parent_list, from 'pio_next' onward, cannot change because
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore * all parents must wait for us to be done before they can be done.
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore */
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore for (; pio != NULL; pio = pio_next) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson pio_next = zio_walk_parents(zio, &zl);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_notify_parent(pio, zio, ZIO_WAIT_READY);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (zio->io_flags & ZIO_FLAG_NODATA) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (BP_IS_GANG(bp)) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_flags &= ~ZIO_FLAG_NODATA;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick } else {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT((uintptr_t)zio->io_data < SPA_MAXBLOCKSIZE);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
a33cae9802e94744efee12a7a77c89360645eae8Tim Haley if (zio_injection_enabled &&
a33cae9802e94744efee12a7a77c89360645eae8Tim Haley zio->io_spa->spa_syncing_txg == zio->io_txg)
a33cae9802e94744efee12a7a77c89360645eae8Tim Haley zio_handle_ignored_writes(zio);
a33cae9802e94744efee12a7a77c89360645eae8Tim Haley
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return (ZIO_PIPELINE_CONTINUE);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson/*
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * Update the allocation throttle accounting.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonstatic void
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonzio_dva_throttle_done(zio_t *zio)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson{
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio_t *lio = zio->io_logical;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio_t *pio = zio_unique_parent(zio);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson vdev_t *vd = zio->io_vd;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson int flags = METASLAB_ASYNC_ALLOC;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT3P(zio->io_bp, !=, NULL);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT3U(zio->io_priority, ==, ZIO_PRIORITY_ASYNC_WRITE);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(vd != NULL);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT3P(vd, ==, vd->vdev_top);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(!(zio->io_flags & (ZIO_FLAG_IO_REPAIR | ZIO_FLAG_IO_RETRY)));
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(zio->io_flags & ZIO_FLAG_IO_ALLOCATING);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(!(lio->io_flags & ZIO_FLAG_IO_REWRITE));
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(!(lio->io_orig_flags & ZIO_FLAG_NODATA));
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson /*
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * Parents of gang children can have two flavors -- ones that
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * allocated the gang header (will have ZIO_FLAG_IO_REWRITE set)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * and ones that allocated the constituent blocks. The allocation
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * throttle needs to know the allocating parent zio so we must find
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * it here.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (pio->io_child_type == ZIO_CHILD_GANG) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson /*
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * If our parent is a rewrite gang child then our grandparent
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * would have been the one that performed the allocation.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (pio->io_flags & ZIO_FLAG_IO_REWRITE)
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson pio = zio_unique_parent(pio);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson flags |= METASLAB_GANG_CHILD;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson }
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(IO_IS_ALLOCATING(pio));
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT3P(zio, !=, zio->io_logical);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(zio->io_logical != NULL);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REPAIR));
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT0(zio->io_flags & ZIO_FLAG_NOPWRITE);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson mutex_enter(&pio->io_lock);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson metaslab_group_alloc_decrement(zio->io_spa, vd->vdev_id, pio, flags);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson mutex_exit(&pio->io_lock);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson metaslab_class_throttle_unreserve(spa_normal_class(zio->io_spa),
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson 1, pio);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson /*
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * Call into the pipeline to see if there is more work that
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * needs to be done. If there is work to be done it will be
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * dispatched to another taskq thread.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio_allocate_dispatch(zio->io_spa);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson}
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickstatic int
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwickzio_done(zio_t *zio)
d63d470b66194d40e74d5a669bd21e215bd63fc2gw{
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick spa_t *spa = zio->io_spa;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_t *lio = zio->io_logical;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick blkptr_t *bp = zio->io_bp;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick vdev_t *vd = zio->io_vd;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick uint64_t psize = zio->io_size;
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore zio_t *pio, *pio_next;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson metaslab_class_t *mc = spa_normal_class(spa);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio_link_t *zl = NULL;
d63d470b66194d40e74d5a669bd21e215bd63fc2gw
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick /*
f5383399322244b3008a624bcd7e93d67037d177Bill Moore * If our children haven't all completed,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * wait for them and then repeat this pipeline stage.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE) ||
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE) ||
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_DONE) ||
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_wait_for_children(zio, ZIO_CHILD_LOGICAL, ZIO_WAIT_DONE))
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return (ZIO_PIPELINE_STOP);
d63d470b66194d40e74d5a669bd21e215bd63fc2gw
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson /*
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * If the allocation throttle is enabled, then update the accounting.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * We only track child I/Os that are part of an allocating async
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * write. We must do this since the allocation is performed
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * by the logical I/O but the actual write is done by child I/Os.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING &&
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio->io_child_type == ZIO_CHILD_VDEV) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(mc->mc_alloc_throttle_enabled);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio_dva_throttle_done(zio);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson }
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson /*
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * If the allocation throttle is enabled, verify that
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * we have decremented the refcounts for every I/O that was throttled.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(zio->io_type == ZIO_TYPE_WRITE);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson ASSERT(bp != NULL);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson metaslab_group_alloc_verify(spa, zio->io_bp, zio);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson VERIFY(refcount_not_held(&mc->mc_alloc_slots, zio));
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson }
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick for (int c = 0; c < ZIO_CHILD_TYPES; c++)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick for (int w = 0; w < ZIO_WAIT_TYPES; w++)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(zio->io_children[c][w] == 0);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens if (bp != NULL && !BP_IS_EMBEDDED(bp)) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(bp->blk_pad[0] == 0);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(bp->blk_pad[1] == 0);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(bcmp(bp, &zio->io_bp_copy, sizeof (blkptr_t)) == 0 ||
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore (bp == zio_unique_parent(zio)->io_bp));
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(bp) &&
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_bp_override == NULL &&
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(!BP_SHOULD_BYTESWAP(bp));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(bp));
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(BP_COUNT_GANG(bp) == 0 ||
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick (BP_COUNT_GANG(bp) == BP_GET_NDVAS(bp)));
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson if (zio->io_flags & ZIO_FLAG_NOPWRITE)
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson VERIFY(BP_EQUAL(bp, &zio->io_bp_orig));
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick /*
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * If there were child vdev/gang/ddt errors, they apply to us now.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_inherit_child_errors(zio, ZIO_CHILD_VDEV);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_inherit_child_errors(zio, ZIO_CHILD_GANG);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_inherit_child_errors(zio, ZIO_CHILD_DDT);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick /*
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * If the I/O on the transformed data was successful, generate any
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * checksum reports now while we still have the transformed data.
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick */
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (zio->io_error == 0) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick while (zio->io_cksum_report != NULL) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_cksum_report_t *zcr = zio->io_cksum_report;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick uint64_t align = zcr->zcr_align;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick uint64_t asize = P2ROUNDUP(psize, align);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick char *abuf = zio->io_data;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (asize != psize) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick abuf = zio_buf_alloc(asize);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick bcopy(zio->io_data, abuf, psize);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick bzero(abuf + psize, asize - psize);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_cksum_report = zcr->zcr_next;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zcr->zcr_next = NULL;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zcr->zcr_finish(zcr, abuf);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zfs_ereport_free_checksum(zcr);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (asize != psize)
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_buf_free(abuf, asize);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_pop_transforms(zio); /* note: may set zio->io_error */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick vdev_stat_update(zio, psize);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zio->io_error) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick /*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * If this I/O is attached to a particular vdev,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * generate an error message describing the I/O failure
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * at the block level. We ignore these errors if the
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * device is currently unavailable.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zio->io_error != ECKSUM && vd != NULL && !vdev_is_dead(vd))
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zfs_ereport_post(FM_EREPORT_ZFS_IO, spa, vd, zio, 0, 0);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
8f18d1fadf6a0c20fac9ff7259a5368faa3c3bfbGeorge Wilson if ((zio->io_error == EIO || !(zio->io_flags &
8f18d1fadf6a0c20fac9ff7259a5368faa3c3bfbGeorge Wilson (ZIO_FLAG_SPECULATIVE | ZIO_FLAG_DONT_PROPAGATE))) &&
8f18d1fadf6a0c20fac9ff7259a5368faa3c3bfbGeorge Wilson zio == lio) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick /*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * For logical I/O requests, tell the SPA to log the
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * error and generate a logical data ereport.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick spa_log_error(spa, zio);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zfs_ereport_post(FM_EREPORT_ZFS_DATA, spa, NULL, zio,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick 0, 0);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zio->io_error && zio == lio) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick /*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * Determine whether zio should be reexecuted. This will
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * propagate all the way to the root via zio_notify_parent().
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(vd == NULL && bp != NULL);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (IO_IS_ALLOCATING(zio) &&
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick !(zio->io_flags & ZIO_FLAG_CANFAIL)) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zio->io_error != ENOSPC)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_reexecute |= ZIO_REEXECUTE_NOW;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick else
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if ((zio->io_type == ZIO_TYPE_READ ||
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_type == ZIO_TYPE_FREE) &&
44ecc5327ab4ce0750dcca2a17e05566bf2812e2George Wilson !(zio->io_flags & ZIO_FLAG_SCAN_THREAD) &&
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_error == ENXIO &&
b16da2e29e074fb6eaeadc4fd7d17ae7340ba240George Wilson spa_load_state(spa) == SPA_LOAD_NONE &&
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick spa_get_failmode(spa) != ZIO_FAILURE_MODE_CONTINUE)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (!(zio->io_flags & ZIO_FLAG_CANFAIL) && !zio->io_reexecute)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams /*
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams * Here is a possibly good place to attempt to do
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams * either combinatorial reconstruction or error correction
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams * based on checksums. It also might be a good place
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams * to send out preliminary ereports before we suspend
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams * processing.
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams */
d63d470b66194d40e74d5a669bd21e215bd63fc2gw }
d63d470b66194d40e74d5a669bd21e215bd63fc2gw
67bd71c6cc629bab3aa0d595c624a667f1574254perrin /*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * If there were logical child errors, they apply to us now.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * We defer this until now to avoid conflating logical child
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * errors with errors that happened to the zio itself when
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * updating vdev stats and reporting FMA events above.
67bd71c6cc629bab3aa0d595c624a667f1574254perrin */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_inherit_child_errors(zio, ZIO_CHILD_LOGICAL);
8654d0253136055bd4cc2423d87378e8a37f2eb5perrin
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if ((zio->io_error || zio->io_reexecute) &&
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick IO_IS_ALLOCATING(zio) && zio->io_gang_leader == zio &&
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson !(zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)))
f5383399322244b3008a624bcd7e93d67037d177Bill Moore zio_dva_unallocate(zio, zio->io_gang_tree, bp);
f5383399322244b3008a624bcd7e93d67037d177Bill Moore
f5383399322244b3008a624bcd7e93d67037d177Bill Moore zio_gang_tree_free(&zio->io_gang_tree);
f5383399322244b3008a624bcd7e93d67037d177Bill Moore
33a372ed2393167a0e329c831ace971f147dddb6George Wilson /*
33a372ed2393167a0e329c831ace971f147dddb6George Wilson * Godfather I/Os should never suspend.
33a372ed2393167a0e329c831ace971f147dddb6George Wilson */
33a372ed2393167a0e329c831ace971f147dddb6George Wilson if ((zio->io_flags & ZIO_FLAG_GODFATHER) &&
33a372ed2393167a0e329c831ace971f147dddb6George Wilson (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND))
33a372ed2393167a0e329c831ace971f147dddb6George Wilson zio->io_reexecute = 0;
33a372ed2393167a0e329c831ace971f147dddb6George Wilson
33a372ed2393167a0e329c831ace971f147dddb6George Wilson if (zio->io_reexecute) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick /*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * This is a logical I/O that wants to reexecute.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick *
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * Reexecute is top-down. When an i/o fails, if it's not
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * the root, it simply notifies its parent and sticks around.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * The parent, seeing that it still has children in zio_done(),
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * does the same. This percolates all the way up to the root.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * The root i/o will reexecute or suspend the entire tree.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick *
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * This approach ensures that zio_reexecute() honors
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * all the original i/o dependency relationships, e.g.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * parents not executing until children are ready.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
f5383399322244b3008a624bcd7e93d67037d177Bill Moore zio->io_gang_leader = NULL;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore mutex_enter(&zio->io_lock);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore zio->io_state[ZIO_WAIT_DONE] = 1;
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore mutex_exit(&zio->io_lock);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson /*
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson * "The Godfather" I/O monitors its children but is
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson * not a true parent to them. It will track them through
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson * the pipeline but severs its ties whenever they get into
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson * trouble (e.g. suspended). This allows "The Godfather"
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson * I/O to return status without blocking.
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zl = NULL;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson for (pio = zio_walk_parents(zio, &zl); pio != NULL;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson pio = pio_next) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio_link_t *remove_zl = zl;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson pio_next = zio_walk_parents(zio, &zl);
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson if ((pio->io_flags & ZIO_FLAG_GODFATHER) &&
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND)) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio_remove_child(pio, zio, remove_zl);
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson }
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson }
54d692b75b7a6f90ce7787309da5451f7458e66aGeorge Wilson
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore if ((pio = zio_unique_parent(zio)) != NULL) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick /*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * We're not a root i/o, so there's nothing to do
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * but notify our parent. Don't propagate errors
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * upward since we haven't permanently failed yet.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick */
33a372ed2393167a0e329c831ace971f147dddb6George Wilson ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_flags |= ZIO_FLAG_DONT_PROPAGATE;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick } else if (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick /*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * We'd fail again if we reexecuted now, so suspend
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * until conditions improve (e.g. device comes online).
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_suspend(spa, zio);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick } else {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick /*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * Reexecution is potentially a huge amount of work.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * Hand it off to the otherwise-unused claim taskq.
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick */
5aeb94743e3be0c51e86f73096334611ae3a058eGarrett D'Amore ASSERT(zio->io_tqent.tqent_next == NULL);
ec94d32216ed5705f5176582355cc311cf848e73Adam Leventhal spa_taskq_dispatch_ent(spa, ZIO_TYPE_CLAIM,
ec94d32216ed5705f5176582355cc311cf848e73Adam Leventhal ZIO_TASKQ_ISSUE, (task_func_t *)zio_reexecute, zio,
ec94d32216ed5705f5176582355cc311cf848e73Adam Leventhal 0, &zio->io_tqent);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return (ZIO_PIPELINE_STOP);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(zio->io_child_count == 0);
33a372ed2393167a0e329c831ace971f147dddb6George Wilson ASSERT(zio->io_reexecute == 0);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick ASSERT(zio->io_error == 0 || (zio->io_flags & ZIO_FLAG_CANFAIL));
fa9e4066f08beec538e775443c5be79dd423fcabahrens
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick /*
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * Report any checksum errors, since the I/O is complete.
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick */
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams while (zio->io_cksum_report != NULL) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_cksum_report_t *zcr = zio->io_cksum_report;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio->io_cksum_report = zcr->zcr_next;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zcr->zcr_next = NULL;
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zcr->zcr_finish(zcr, NULL);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zfs_ereport_free_checksum(zcr);
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams }
22fe2c8844be88ebae6478ca1b0b92c8ec2aef54Jonathan Adams
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore /*
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore * It is the responsibility of the done callback to ensure that this
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore * particular zio is no longer discoverable for adoption, and as
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore * such, cannot acquire any new parents.
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore */
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zio->io_done)
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_done(zio);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore mutex_enter(&zio->io_lock);
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore zio->io_state[ZIO_WAIT_DONE] = 1;
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore mutex_exit(&zio->io_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zl = NULL;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson for (pio = zio_walk_parents(zio, &zl); pio != NULL; pio = pio_next) {
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio_link_t *remove_zl = zl;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson pio_next = zio_walk_parents(zio, &zl);
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio_remove_child(pio, zio, remove_zl);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick if (zio->io_waiter != NULL) {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick mutex_enter(&zio->io_lock);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio->io_executor = NULL;
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick cv_broadcast(&zio->io_cv);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick mutex_exit(&zio->io_lock);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick } else {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_destroy(zio);
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick return (ZIO_PIPELINE_STOP);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
4634122238f8ab356123a2e7f4cea74d05655c17perrin
4634122238f8ab356123a2e7f4cea74d05655c17perrin/*
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * ==========================================================================
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * I/O pipeline definition
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick * ==========================================================================
4634122238f8ab356123a2e7f4cea74d05655c17perrin */
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickstatic zio_pipe_stage_t *zio_pipeline[] = {
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick NULL,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_read_bp_init,
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio_write_bp_init,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_free_bp_init,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_issue_async,
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio_write_compress,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_checksum_generate,
80901aea8e78a2c20751f61f01bebd1d5b5c2ba5George Wilson zio_nop_write,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_ddt_read_start,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_ddt_read_done,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_ddt_write,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick zio_ddt_free,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_gang_assemble,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_gang_issue,
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio_dva_throttle,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_dva_allocate,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_dva_free,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_dva_claim,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_ready,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_vdev_io_start,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_vdev_io_done,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_vdev_io_assess,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_checksum_verify,
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick zio_done
e14bb3258d05c1b1077e2db7cf77088924e56919Jeff Bonwick};
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie/*
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * Compare two zbookmark_phys_t's to see which we would reach first in a
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * pre-order traversal of the object tree.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie *
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * This is simple in every case aside from the meta-dnode object. For all other
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * objects, we traverse them in order (object 1 before object 2, and so on).
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * However, all of these objects are traversed while traversing object 0, since
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * the data it points to is the list of objects. Thus, we need to convert to a
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * canonical representation so we can compare meta-dnode bookmarks to
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * non-meta-dnode bookmarks.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie *
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * We do this by calculating "equivalents" for each field of the zbookmark.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * zbookmarks outside of the meta-dnode use their own object and level, and
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * calculate the level 0 equivalent (the first L0 blkid that is contained in the
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * blocks this bookmark refers to) by multiplying their blkid by their span
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * (the number of L0 blocks contained within one block at their level).
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * zbookmarks inside the meta-dnode calculate their object equivalent
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * (which is L0equiv * dnodes per data block), use 0 for their L0equiv, and use
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * level + 1<<31 (any value larger than a level could ever be) for their level.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * This causes them to always compare before a bookmark in their object
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * equivalent, compare appropriately to bookmarks in other objects, and to
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * compare appropriately to other bookmarks in the meta-dnode.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelieint
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliezbookmark_compare(uint16_t dbss1, uint8_t ibs1, uint16_t dbss2, uint8_t ibs2,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie const zbookmark_phys_t *zb1, const zbookmark_phys_t *zb2)
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie{
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie /*
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * These variables represent the "equivalent" values for the zbookmark,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * after converting zbookmarks inside the meta dnode to their
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * normal-object equivalents.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie uint64_t zb1obj, zb2obj;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie uint64_t zb1L0, zb2L0;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie uint64_t zb1level, zb2level;
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (zb1->zb_object == zb2->zb_object &&
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie zb1->zb_level == zb2->zb_level &&
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie zb1->zb_blkid == zb2->zb_blkid)
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie return (0);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie /*
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * BP_SPANB calculates the span in blocks.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie zb1L0 = (zb1->zb_blkid) * BP_SPANB(ibs1, zb1->zb_level);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie zb2L0 = (zb2->zb_blkid) * BP_SPANB(ibs2, zb2->zb_level);
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden if (zb1->zb_object == DMU_META_DNODE_OBJECT) {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie zb1obj = zb1L0 * (dbss1 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT));
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie zb1L0 = 0;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie zb1level = zb1->zb_level + COMPARE_META_LEVEL;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie } else {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie zb1obj = zb1->zb_object;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie zb1level = zb1->zb_level;
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden }
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (zb2->zb_object == DMU_META_DNODE_OBJECT) {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie zb2obj = zb2L0 * (dbss2 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT));
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie zb2L0 = 0;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie zb2level = zb2->zb_level + COMPARE_META_LEVEL;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie } else {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie zb2obj = zb2->zb_object;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie zb2level = zb2->zb_level;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie }
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie /* Now that we have a canonical representation, do the comparison. */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (zb1obj != zb2obj)
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie return (zb1obj < zb2obj ? -1 : 1);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie else if (zb1L0 != zb2L0)
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie return (zb1L0 < zb2L0 ? -1 : 1);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie else if (zb1level != zb2level)
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie return (zb1level > zb2level ? -1 : 1);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie /*
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * This can (theoretically) happen if the bookmarks have the same object
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * and level, but different blkids, if the block sizes are not the same.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * There is presently no way to change the indirect block sizes
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie return (0);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie}
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie/*
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * This function checks the following: given that last_block is the place that
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * our traversal stopped last time, does that guarantee that we've visited
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * every node under subtree_root? Therefore, we can't just use the raw output
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * of zbookmark_compare. We have to pass in a modified version of
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * subtree_root; by incrementing the block id, and then checking whether
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * last_block is before or equal to that, we can tell whether or not having
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * visited last_block implies that all of subtree_root's children have been
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * visited.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelieboolean_t
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliezbookmark_subtree_completed(const dnode_phys_t *dnp,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie const zbookmark_phys_t *subtree_root, const zbookmark_phys_t *last_block)
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie{
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie zbookmark_phys_t mod_zb = *subtree_root;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie mod_zb.zb_blkid++;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie ASSERT(last_block->zb_level == 0);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie /* The objset_phys_t isn't before anything. */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (dnp == NULL)
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden return (B_FALSE);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie /*
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * We pass in 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT) for the
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * data block size in sectors, because that variable is only used if
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * the bookmark refers to a block in the meta-dnode. Since we don't
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * know without examining it what object it refers to, and there's no
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * harm in passing in this value in other cases, we always pass it in.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie *
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * We pass in 0 for the indirect block size shift because zb2 must be
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * level 0. The indirect block size is only used to calculate the span
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * of the bookmark, but since the bookmark must be level 0, the span is
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * always 1, so the math works out.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie *
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * If you make changes to how the zbookmark_compare code works, be sure
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * to make sure that this code still works afterwards.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie return (zbookmark_compare(dnp->dn_datablkszsec, dnp->dn_indblkshift,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT), 0, &mod_zb,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie last_block) <= 0);
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden}