1c1cb2cd8Shaad /*
2c1cb2cd8Shaad * CDDL HEADER START
3c1cb2cd8Shaad *
4c1cb2cd8Shaad * The contents of this file are subject to the terms of the
5c1cb2cd8Shaad * Common Development and Distribution License (the "License").
6c1cb2cd8Shaad * You may not use this file except in compliance with the License.
7c1cb2cd8Shaad *
8c1cb2cd8Shaad * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9c1cb2cd8Shaad * or http://www.opensolaris.org/os/licensing.
10c1cb2cd8Shaad * See the License for the specific language governing permissions
11c1cb2cd8Shaad * and limitations under the License.
12c1cb2cd8Shaad *
13c1cb2cd8Shaad * When distributing Covered Code, include this CDDL HEADER in each
14c1cb2cd8Shaad * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15c1cb2cd8Shaad * If applicable, add the following below this CDDL HEADER, with the
16c1cb2cd8Shaad * fields enclosed by brackets "[]" replaced with your own identifying
17c1cb2cd8Shaad * information: Portions Copyright [yyyy] [name of copyright owner]
18c1cb2cd8Shaad *
19c1cb2cd8Shaad * CDDL HEADER END
20c1cb2cd8Shaad */
21c1cb2cd8Shaad /*
22eada09acSchs * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23eada09acSchs * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
24eada09acSchs * Copyright (c) 2011 Nexenta Systems, Inc. All rights reserved.
25eada09acSchs * Copyright (c) 2014 Integros [integros.com]
26c1cb2cd8Shaad */
27c1cb2cd8Shaad
28eada09acSchs #include <sys/sysmacros.h>
29c1cb2cd8Shaad #include <sys/zfs_context.h>
30c1cb2cd8Shaad #include <sys/fm/fs/zfs.h>
31c1cb2cd8Shaad #include <sys/spa.h>
32c1cb2cd8Shaad #include <sys/txg.h>
33c1cb2cd8Shaad #include <sys/spa_impl.h>
34c1cb2cd8Shaad #include <sys/vdev_impl.h>
35c1cb2cd8Shaad #include <sys/zio_impl.h>
36c1cb2cd8Shaad #include <sys/zio_compress.h>
37c1cb2cd8Shaad #include <sys/zio_checksum.h>
38a252d550Shaad #include <sys/dmu_objset.h>
39a252d550Shaad #include <sys/arc.h>
40a252d550Shaad #include <sys/ddt.h>
41eada09acSchs #include <sys/trim_map.h>
42eada09acSchs #include <sys/blkptr.h>
43eada09acSchs #include <sys/zfeature.h>
44eada09acSchs #include <sys/metaslab_impl.h>
45c1cb2cd8Shaad
46eada09acSchs SYSCTL_DECL(_vfs_zfs);
47eada09acSchs SYSCTL_NODE(_vfs_zfs, OID_AUTO, zio, CTLFLAG_RW, 0, "ZFS ZIO");
48eada09acSchs #ifdef __NetBSD__
49*43dd5240Sjdolecek const int zio_use_uma = 1;
50eada09acSchs #else
51eada09acSchs #if defined(__amd64__)
52eada09acSchs static int zio_use_uma = 1;
53eada09acSchs #else
54eada09acSchs static int zio_use_uma = 0;
55eada09acSchs #endif
56eada09acSchs #endif
57eada09acSchs SYSCTL_INT(_vfs_zfs_zio, OID_AUTO, use_uma, CTLFLAG_RDTUN, &zio_use_uma, 0,
58eada09acSchs "Use uma(9) for ZIO allocations");
59eada09acSchs static int zio_exclude_metadata = 0;
60eada09acSchs SYSCTL_INT(_vfs_zfs_zio, OID_AUTO, exclude_metadata, CTLFLAG_RDTUN, &zio_exclude_metadata, 0,
61eada09acSchs "Exclude metadata buffers from dumps as well");
62eada09acSchs
63eada09acSchs zio_trim_stats_t zio_trim_stats = {
64eada09acSchs { "bytes", KSTAT_DATA_UINT64,
65eada09acSchs "Number of bytes successfully TRIMmed" },
66eada09acSchs { "success", KSTAT_DATA_UINT64,
67eada09acSchs "Number of successful TRIM requests" },
68eada09acSchs { "unsupported", KSTAT_DATA_UINT64,
69eada09acSchs "Number of TRIM requests that failed because TRIM is not supported" },
70eada09acSchs { "failed", KSTAT_DATA_UINT64,
71eada09acSchs "Number of TRIM requests that failed for reasons other than not supported" },
72c1cb2cd8Shaad };
73c1cb2cd8Shaad
74eada09acSchs static kstat_t *zio_trim_ksp;
75eada09acSchs
76c1cb2cd8Shaad /*
77c1cb2cd8Shaad * ==========================================================================
78c1cb2cd8Shaad * I/O type descriptions
79c1cb2cd8Shaad * ==========================================================================
80c1cb2cd8Shaad */
81eada09acSchs const char *zio_type_name[ZIO_TYPES] = {
82a252d550Shaad "zio_null", "zio_read", "zio_write", "zio_free", "zio_claim",
83a252d550Shaad "zio_ioctl"
84a252d550Shaad };
85c1cb2cd8Shaad
86eada09acSchs boolean_t zio_dva_throttle_enabled = B_TRUE;
87eada09acSchs SYSCTL_INT(_vfs_zfs_zio, OID_AUTO, dva_throttle_enabled, CTLFLAG_RDTUN,
88eada09acSchs &zio_dva_throttle_enabled, 0, "");
89eada09acSchs
90c1cb2cd8Shaad /*
91c1cb2cd8Shaad * ==========================================================================
92c1cb2cd8Shaad * I/O kmem caches
93c1cb2cd8Shaad * ==========================================================================
94c1cb2cd8Shaad */
95c1cb2cd8Shaad kmem_cache_t *zio_cache;
96a252d550Shaad kmem_cache_t *zio_link_cache;
97c1cb2cd8Shaad kmem_cache_t *zio_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
98c1cb2cd8Shaad kmem_cache_t *zio_data_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
99c1cb2cd8Shaad
100eada09acSchs #ifdef _KERNEL
101c1cb2cd8Shaad extern vmem_t *zio_alloc_arena;
102c1cb2cd8Shaad #endif
103c1cb2cd8Shaad
104eada09acSchs #define ZIO_PIPELINE_CONTINUE 0x100
105eada09acSchs #define ZIO_PIPELINE_STOP 0x101
106eada09acSchs
107eada09acSchs #define BP_SPANB(indblkshift, level) \
108eada09acSchs (((uint64_t)1) << ((level) * ((indblkshift) - SPA_BLKPTRSHIFT)))
109eada09acSchs #define COMPARE_META_LEVEL 0x80000000ul
110eada09acSchs /*
111eada09acSchs * The following actions directly effect the spa's sync-to-convergence logic.
112eada09acSchs * The values below define the sync pass when we start performing the action.
113eada09acSchs * Care should be taken when changing these values as they directly impact
114eada09acSchs * spa_sync() performance. Tuning these values may introduce subtle performance
115eada09acSchs * pathologies and should only be done in the context of performance analysis.
116eada09acSchs * These tunables will eventually be removed and replaced with #defines once
117eada09acSchs * enough analysis has been done to determine optimal values.
118eada09acSchs *
119eada09acSchs * The 'zfs_sync_pass_deferred_free' pass must be greater than 1 to ensure that
120eada09acSchs * regular blocks are not deferred.
121eada09acSchs */
122eada09acSchs int zfs_sync_pass_deferred_free = 2; /* defer frees starting in this pass */
123eada09acSchs SYSCTL_INT(_vfs_zfs, OID_AUTO, sync_pass_deferred_free, CTLFLAG_RDTUN,
124eada09acSchs &zfs_sync_pass_deferred_free, 0, "defer frees starting in this pass");
125eada09acSchs int zfs_sync_pass_dont_compress = 5; /* don't compress starting in this pass */
126eada09acSchs SYSCTL_INT(_vfs_zfs, OID_AUTO, sync_pass_dont_compress, CTLFLAG_RDTUN,
127eada09acSchs &zfs_sync_pass_dont_compress, 0, "don't compress starting in this pass");
128eada09acSchs int zfs_sync_pass_rewrite = 2; /* rewrite new bps starting in this pass */
129eada09acSchs SYSCTL_INT(_vfs_zfs, OID_AUTO, sync_pass_rewrite, CTLFLAG_RDTUN,
130eada09acSchs &zfs_sync_pass_rewrite, 0, "rewrite new bps starting in this pass");
131eada09acSchs
132c1cb2cd8Shaad /*
133c1cb2cd8Shaad * An allocating zio is one that either currently has the DVA allocate
134c1cb2cd8Shaad * stage set or will have it later in its lifetime.
135c1cb2cd8Shaad */
136a252d550Shaad #define IO_IS_ALLOCATING(zio) ((zio)->io_orig_pipeline & ZIO_STAGE_DVA_ALLOCATE)
137a252d550Shaad
138a252d550Shaad boolean_t zio_requeue_io_start_cut_in_line = B_TRUE;
139a252d550Shaad
140eada09acSchs #ifdef illumos
141a252d550Shaad #ifdef ZFS_DEBUG
142a252d550Shaad int zio_buf_debug_limit = 16384;
143a252d550Shaad #else
144a252d550Shaad int zio_buf_debug_limit = 0;
145a252d550Shaad #endif
146eada09acSchs #endif
147eada09acSchs
148eada09acSchs static void zio_taskq_dispatch(zio_t *, zio_taskq_type_t, boolean_t);
149c1cb2cd8Shaad
150c1cb2cd8Shaad void
zio_init(void)151c1cb2cd8Shaad zio_init(void)
152c1cb2cd8Shaad {
153c1cb2cd8Shaad size_t c;
154a252d550Shaad zio_cache = kmem_cache_create("zio_cache",
155a252d550Shaad sizeof (zio_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
156a252d550Shaad zio_link_cache = kmem_cache_create("zio_link_cache",
157a252d550Shaad sizeof (zio_link_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
158c1cb2cd8Shaad
159eada09acSchs if (!zio_use_uma)
160eada09acSchs goto out;
161eada09acSchs
162c1cb2cd8Shaad /*
163c1cb2cd8Shaad * For small buffers, we want a cache for each multiple of
164eada09acSchs * SPA_MINBLOCKSIZE. For larger buffers, we want a cache
165eada09acSchs * for each quarter-power of 2.
166c1cb2cd8Shaad */
167c1cb2cd8Shaad for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
168c1cb2cd8Shaad size_t size = (c + 1) << SPA_MINBLOCKSHIFT;
169c1cb2cd8Shaad size_t p2 = size;
170c1cb2cd8Shaad size_t align = 0;
171eada09acSchs int cflags = zio_exclude_metadata ? KMC_NODEBUG : 0;
172c1cb2cd8Shaad
173eada09acSchs while (!ISP2(p2))
174c1cb2cd8Shaad p2 &= p2 - 1;
175c1cb2cd8Shaad
176eada09acSchs #ifdef illumos
177eada09acSchs #ifndef _KERNEL
178eada09acSchs /*
179eada09acSchs * If we are using watchpoints, put each buffer on its own page,
180eada09acSchs * to eliminate the performance overhead of trapping to the
181eada09acSchs * kernel when modifying a non-watched buffer that shares the
182eada09acSchs * page with a watched buffer.
183eada09acSchs */
184eada09acSchs if (arc_watch && !IS_P2ALIGNED(size, PAGESIZE))
185eada09acSchs continue;
186eada09acSchs #endif
187eada09acSchs #endif /* illumos */
188c1cb2cd8Shaad if (size <= 4 * SPA_MINBLOCKSIZE) {
189c1cb2cd8Shaad align = SPA_MINBLOCKSIZE;
190eada09acSchs } else if (IS_P2ALIGNED(size, p2 >> 2)) {
191eada09acSchs align = MIN(p2 >> 2, PAGESIZE);
192c1cb2cd8Shaad }
193c1cb2cd8Shaad
194c1cb2cd8Shaad if (align != 0) {
195c1cb2cd8Shaad char name[36];
196eada09acSchs (void) sprintf(name, "zio_buf_%lu", (ulong_t)size);
197c1cb2cd8Shaad zio_buf_cache[c] = kmem_cache_create(name, size,
198eada09acSchs align, NULL, NULL, NULL, NULL, NULL, cflags);
199c1cb2cd8Shaad
200eada09acSchs /*
201eada09acSchs * Since zio_data bufs do not appear in crash dumps, we
202eada09acSchs * pass KMC_NOTOUCH so that no allocator metadata is
203eada09acSchs * stored with the buffers.
204eada09acSchs */
205eada09acSchs (void) sprintf(name, "zio_data_buf_%lu", (ulong_t)size);
206c1cb2cd8Shaad zio_data_buf_cache[c] = kmem_cache_create(name, size,
207eada09acSchs align, NULL, NULL, NULL, NULL, NULL,
208eada09acSchs cflags | KMC_NOTOUCH | KMC_NODEBUG);
209c1cb2cd8Shaad }
210c1cb2cd8Shaad }
211c1cb2cd8Shaad
212c1cb2cd8Shaad while (--c != 0) {
213c1cb2cd8Shaad ASSERT(zio_buf_cache[c] != NULL);
214c1cb2cd8Shaad if (zio_buf_cache[c - 1] == NULL)
215c1cb2cd8Shaad zio_buf_cache[c - 1] = zio_buf_cache[c];
216c1cb2cd8Shaad
217c1cb2cd8Shaad ASSERT(zio_data_buf_cache[c] != NULL);
218c1cb2cd8Shaad if (zio_data_buf_cache[c - 1] == NULL)
219c1cb2cd8Shaad zio_data_buf_cache[c - 1] = zio_data_buf_cache[c];
220c1cb2cd8Shaad }
221eada09acSchs out:
222eada09acSchs
223c1cb2cd8Shaad zio_inject_init();
224eada09acSchs
225eada09acSchs zio_trim_ksp = kstat_create("zfs", 0, "zio_trim", "misc",
226eada09acSchs KSTAT_TYPE_NAMED,
227eada09acSchs sizeof(zio_trim_stats) / sizeof(kstat_named_t),
228eada09acSchs KSTAT_FLAG_VIRTUAL);
229eada09acSchs
230eada09acSchs if (zio_trim_ksp != NULL) {
231eada09acSchs zio_trim_ksp->ks_data = &zio_trim_stats;
232eada09acSchs kstat_install(zio_trim_ksp);
233eada09acSchs }
234c1cb2cd8Shaad }
235c1cb2cd8Shaad
236c1cb2cd8Shaad void
zio_fini(void)237c1cb2cd8Shaad zio_fini(void)
238c1cb2cd8Shaad {
239c1cb2cd8Shaad size_t c;
240c1cb2cd8Shaad kmem_cache_t *last_cache = NULL;
241c1cb2cd8Shaad kmem_cache_t *last_data_cache = NULL;
242c1cb2cd8Shaad
243c1cb2cd8Shaad for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
244c1cb2cd8Shaad if (zio_buf_cache[c] != last_cache) {
245c1cb2cd8Shaad last_cache = zio_buf_cache[c];
246c1cb2cd8Shaad kmem_cache_destroy(zio_buf_cache[c]);
247c1cb2cd8Shaad }
248c1cb2cd8Shaad zio_buf_cache[c] = NULL;
249c1cb2cd8Shaad
250c1cb2cd8Shaad if (zio_data_buf_cache[c] != last_data_cache) {
251c1cb2cd8Shaad last_data_cache = zio_data_buf_cache[c];
252c1cb2cd8Shaad kmem_cache_destroy(zio_data_buf_cache[c]);
253c1cb2cd8Shaad }
254c1cb2cd8Shaad zio_data_buf_cache[c] = NULL;
255c1cb2cd8Shaad }
256c1cb2cd8Shaad
257a252d550Shaad kmem_cache_destroy(zio_link_cache);
258c1cb2cd8Shaad kmem_cache_destroy(zio_cache);
259c1cb2cd8Shaad
260c1cb2cd8Shaad zio_inject_fini();
261eada09acSchs
262eada09acSchs if (zio_trim_ksp != NULL) {
263eada09acSchs kstat_delete(zio_trim_ksp);
264eada09acSchs zio_trim_ksp = NULL;
265eada09acSchs }
266c1cb2cd8Shaad }
267c1cb2cd8Shaad
268c1cb2cd8Shaad /*
269c1cb2cd8Shaad * ==========================================================================
270c1cb2cd8Shaad * Allocate and free I/O buffers
271c1cb2cd8Shaad * ==========================================================================
272c1cb2cd8Shaad */
273c1cb2cd8Shaad
274c1cb2cd8Shaad /*
275c1cb2cd8Shaad * Use zio_buf_alloc to allocate ZFS metadata. This data will appear in a
276c1cb2cd8Shaad * crashdump if the kernel panics, so use it judiciously. Obviously, it's
277c1cb2cd8Shaad * useful to inspect ZFS metadata, but if possible, we should avoid keeping
278c1cb2cd8Shaad * excess / transient data in-core during a crashdump.
279c1cb2cd8Shaad */
280eada09acSchs static void *
zio_buf_alloc_impl(size_t size,boolean_t canwait)281eada09acSchs zio_buf_alloc_impl(size_t size, boolean_t canwait)
282eada09acSchs {
283eada09acSchs size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
284eada09acSchs int flags = zio_exclude_metadata ? KM_NODEBUG : 0;
285eada09acSchs
286eada09acSchs VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
287eada09acSchs
288eada09acSchs if (zio_use_uma) {
289eada09acSchs return (kmem_cache_alloc(zio_buf_cache[c],
290eada09acSchs canwait ? KM_PUSHPAGE : KM_NOSLEEP));
291eada09acSchs } else {
292eada09acSchs return (kmem_alloc(size,
293eada09acSchs (canwait ? KM_SLEEP : KM_NOSLEEP) | flags));
294eada09acSchs }
295eada09acSchs }
296eada09acSchs
297c1cb2cd8Shaad void *
zio_buf_alloc(size_t size)298c1cb2cd8Shaad zio_buf_alloc(size_t size)
299c1cb2cd8Shaad {
300eada09acSchs return (zio_buf_alloc_impl(size, B_TRUE));
301eada09acSchs }
302eada09acSchs
303eada09acSchs void *
zio_buf_alloc_nowait(size_t size)304eada09acSchs zio_buf_alloc_nowait(size_t size)
305eada09acSchs {
306eada09acSchs return (zio_buf_alloc_impl(size, B_FALSE));
307c1cb2cd8Shaad }
308c1cb2cd8Shaad
309c1cb2cd8Shaad /*
310c1cb2cd8Shaad * Use zio_data_buf_alloc to allocate data. The data will not appear in a
311c1cb2cd8Shaad * crashdump if the kernel panics. This exists so that we will limit the amount
312c1cb2cd8Shaad * of ZFS data that shows up in a kernel crashdump. (Thus reducing the amount
313c1cb2cd8Shaad * of kernel heap dumped to disk when the kernel panics)
314c1cb2cd8Shaad */
315c1cb2cd8Shaad void *
zio_data_buf_alloc(size_t size)316c1cb2cd8Shaad zio_data_buf_alloc(size_t size)
317c1cb2cd8Shaad {
318c1cb2cd8Shaad size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
319c1cb2cd8Shaad
320eada09acSchs VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
321eada09acSchs
322eada09acSchs if (zio_use_uma)
323c1cb2cd8Shaad return (kmem_cache_alloc(zio_data_buf_cache[c], KM_PUSHPAGE));
324eada09acSchs else
325eada09acSchs return (kmem_alloc(size, KM_SLEEP | KM_NODEBUG));
326c1cb2cd8Shaad }
327c1cb2cd8Shaad
328c1cb2cd8Shaad void
zio_buf_free(void * buf,size_t size)329c1cb2cd8Shaad zio_buf_free(void *buf, size_t size)
330c1cb2cd8Shaad {
331c1cb2cd8Shaad size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
332c1cb2cd8Shaad
333eada09acSchs VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
334c1cb2cd8Shaad
335eada09acSchs if (zio_use_uma)
336c1cb2cd8Shaad kmem_cache_free(zio_buf_cache[c], buf);
337eada09acSchs else
338eada09acSchs kmem_free(buf, size);
339c1cb2cd8Shaad }
340c1cb2cd8Shaad
341c1cb2cd8Shaad void
zio_data_buf_free(void * buf,size_t size)342c1cb2cd8Shaad zio_data_buf_free(void *buf, size_t size)
343c1cb2cd8Shaad {
344c1cb2cd8Shaad size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
345c1cb2cd8Shaad
346eada09acSchs VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
347c1cb2cd8Shaad
348eada09acSchs if (zio_use_uma)
349c1cb2cd8Shaad kmem_cache_free(zio_data_buf_cache[c], buf);
350eada09acSchs else
351eada09acSchs kmem_free(buf, size);
352c1cb2cd8Shaad }
353c1cb2cd8Shaad
354c1cb2cd8Shaad /*
355c1cb2cd8Shaad * ==========================================================================
356c1cb2cd8Shaad * Push and pop I/O transform buffers
357c1cb2cd8Shaad * ==========================================================================
358c1cb2cd8Shaad */
359eada09acSchs void
zio_push_transform(zio_t * zio,void * data,uint64_t size,uint64_t bufsize,zio_transform_func_t * transform)360c1cb2cd8Shaad zio_push_transform(zio_t *zio, void *data, uint64_t size, uint64_t bufsize,
361c1cb2cd8Shaad zio_transform_func_t *transform)
362c1cb2cd8Shaad {
363c1cb2cd8Shaad zio_transform_t *zt = kmem_alloc(sizeof (zio_transform_t), KM_SLEEP);
364c1cb2cd8Shaad
365c1cb2cd8Shaad zt->zt_orig_data = zio->io_data;
366c1cb2cd8Shaad zt->zt_orig_size = zio->io_size;
367c1cb2cd8Shaad zt->zt_bufsize = bufsize;
368c1cb2cd8Shaad zt->zt_transform = transform;
369c1cb2cd8Shaad
370c1cb2cd8Shaad zt->zt_next = zio->io_transform_stack;
371c1cb2cd8Shaad zio->io_transform_stack = zt;
372c1cb2cd8Shaad
373c1cb2cd8Shaad zio->io_data = data;
374c1cb2cd8Shaad zio->io_size = size;
375c1cb2cd8Shaad }
376c1cb2cd8Shaad
377eada09acSchs void
zio_pop_transforms(zio_t * zio)378c1cb2cd8Shaad zio_pop_transforms(zio_t *zio)
379c1cb2cd8Shaad {
380c1cb2cd8Shaad zio_transform_t *zt;
381c1cb2cd8Shaad
382c1cb2cd8Shaad while ((zt = zio->io_transform_stack) != NULL) {
383c1cb2cd8Shaad if (zt->zt_transform != NULL)
384c1cb2cd8Shaad zt->zt_transform(zio,
385c1cb2cd8Shaad zt->zt_orig_data, zt->zt_orig_size);
386c1cb2cd8Shaad
387a252d550Shaad if (zt->zt_bufsize != 0)
388c1cb2cd8Shaad zio_buf_free(zio->io_data, zt->zt_bufsize);
389c1cb2cd8Shaad
390c1cb2cd8Shaad zio->io_data = zt->zt_orig_data;
391c1cb2cd8Shaad zio->io_size = zt->zt_orig_size;
392c1cb2cd8Shaad zio->io_transform_stack = zt->zt_next;
393c1cb2cd8Shaad
394c1cb2cd8Shaad kmem_free(zt, sizeof (zio_transform_t));
395c1cb2cd8Shaad }
396c1cb2cd8Shaad }
397c1cb2cd8Shaad
398c1cb2cd8Shaad /*
399c1cb2cd8Shaad * ==========================================================================
400c1cb2cd8Shaad * I/O transform callbacks for subblocks and decompression
401c1cb2cd8Shaad * ==========================================================================
402c1cb2cd8Shaad */
403c1cb2cd8Shaad static void
zio_subblock(zio_t * zio,void * data,uint64_t size)404c1cb2cd8Shaad zio_subblock(zio_t *zio, void *data, uint64_t size)
405c1cb2cd8Shaad {
406c1cb2cd8Shaad ASSERT(zio->io_size > size);
407c1cb2cd8Shaad
408c1cb2cd8Shaad if (zio->io_type == ZIO_TYPE_READ)
409c1cb2cd8Shaad bcopy(zio->io_data, data, size);
410c1cb2cd8Shaad }
411c1cb2cd8Shaad
412c1cb2cd8Shaad static void
zio_decompress(zio_t * zio,void * data,uint64_t size)413c1cb2cd8Shaad zio_decompress(zio_t *zio, void *data, uint64_t size)
414c1cb2cd8Shaad {
415c1cb2cd8Shaad if (zio->io_error == 0 &&
416c1cb2cd8Shaad zio_decompress_data(BP_GET_COMPRESS(zio->io_bp),
417a252d550Shaad zio->io_data, data, zio->io_size, size) != 0)
418eada09acSchs zio->io_error = SET_ERROR(EIO);
419c1cb2cd8Shaad }
420c1cb2cd8Shaad
421c1cb2cd8Shaad /*
422c1cb2cd8Shaad * ==========================================================================
423c1cb2cd8Shaad * I/O parent/child relationships and pipeline interlocks
424c1cb2cd8Shaad * ==========================================================================
425c1cb2cd8Shaad */
426a252d550Shaad zio_t *
zio_walk_parents(zio_t * cio,zio_link_t ** zl)427eada09acSchs zio_walk_parents(zio_t *cio, zio_link_t **zl)
428c1cb2cd8Shaad {
429a252d550Shaad list_t *pl = &cio->io_parent_list;
430a252d550Shaad
431eada09acSchs *zl = (*zl == NULL) ? list_head(pl) : list_next(pl, *zl);
432eada09acSchs if (*zl == NULL)
433a252d550Shaad return (NULL);
434a252d550Shaad
435eada09acSchs ASSERT((*zl)->zl_child == cio);
436eada09acSchs return ((*zl)->zl_parent);
437a252d550Shaad }
438a252d550Shaad
439a252d550Shaad zio_t *
zio_walk_children(zio_t * pio,zio_link_t ** zl)440eada09acSchs zio_walk_children(zio_t *pio, zio_link_t **zl)
441a252d550Shaad {
442a252d550Shaad list_t *cl = &pio->io_child_list;
443a252d550Shaad
444eada09acSchs *zl = (*zl == NULL) ? list_head(cl) : list_next(cl, *zl);
445eada09acSchs if (*zl == NULL)
446a252d550Shaad return (NULL);
447a252d550Shaad
448eada09acSchs ASSERT((*zl)->zl_parent == pio);
449eada09acSchs return ((*zl)->zl_child);
450a252d550Shaad }
451a252d550Shaad
452a252d550Shaad zio_t *
zio_unique_parent(zio_t * cio)453a252d550Shaad zio_unique_parent(zio_t *cio)
454a252d550Shaad {
455eada09acSchs zio_link_t *zl = NULL;
456eada09acSchs zio_t *pio = zio_walk_parents(cio, &zl);
457a252d550Shaad
458eada09acSchs VERIFY3P(zio_walk_parents(cio, &zl), ==, NULL);
459a252d550Shaad return (pio);
460a252d550Shaad }
461a252d550Shaad
462a252d550Shaad void
zio_add_child(zio_t * pio,zio_t * cio)463a252d550Shaad zio_add_child(zio_t *pio, zio_t *cio)
464a252d550Shaad {
465a252d550Shaad zio_link_t *zl = kmem_cache_alloc(zio_link_cache, KM_SLEEP);
466a252d550Shaad
467a252d550Shaad /*
468a252d550Shaad * Logical I/Os can have logical, gang, or vdev children.
469a252d550Shaad * Gang I/Os can have gang or vdev children.
470a252d550Shaad * Vdev I/Os can only have vdev children.
471a252d550Shaad * The following ASSERT captures all of these constraints.
472a252d550Shaad */
473a252d550Shaad ASSERT(cio->io_child_type <= pio->io_child_type);
474a252d550Shaad
475a252d550Shaad zl->zl_parent = pio;
476a252d550Shaad zl->zl_child = cio;
477a252d550Shaad
478a252d550Shaad mutex_enter(&cio->io_lock);
479c1cb2cd8Shaad mutex_enter(&pio->io_lock);
480a252d550Shaad
481a252d550Shaad ASSERT(pio->io_state[ZIO_WAIT_DONE] == 0);
482a252d550Shaad
483a252d550Shaad for (int w = 0; w < ZIO_WAIT_TYPES; w++)
484a252d550Shaad pio->io_children[cio->io_child_type][w] += !cio->io_state[w];
485a252d550Shaad
486a252d550Shaad list_insert_head(&pio->io_child_list, zl);
487a252d550Shaad list_insert_head(&cio->io_parent_list, zl);
488a252d550Shaad
489a252d550Shaad pio->io_child_count++;
490a252d550Shaad cio->io_parent_count++;
491a252d550Shaad
492c1cb2cd8Shaad mutex_exit(&pio->io_lock);
493a252d550Shaad mutex_exit(&cio->io_lock);
494c1cb2cd8Shaad }
495c1cb2cd8Shaad
496c1cb2cd8Shaad static void
zio_remove_child(zio_t * pio,zio_t * cio,zio_link_t * zl)497a252d550Shaad zio_remove_child(zio_t *pio, zio_t *cio, zio_link_t *zl)
498c1cb2cd8Shaad {
499a252d550Shaad ASSERT(zl->zl_parent == pio);
500a252d550Shaad ASSERT(zl->zl_child == cio);
501c1cb2cd8Shaad
502a252d550Shaad mutex_enter(&cio->io_lock);
503c1cb2cd8Shaad mutex_enter(&pio->io_lock);
504a252d550Shaad
505a252d550Shaad list_remove(&pio->io_child_list, zl);
506a252d550Shaad list_remove(&cio->io_parent_list, zl);
507a252d550Shaad
508a252d550Shaad pio->io_child_count--;
509a252d550Shaad cio->io_parent_count--;
510a252d550Shaad
511c1cb2cd8Shaad mutex_exit(&pio->io_lock);
512a252d550Shaad mutex_exit(&cio->io_lock);
513a252d550Shaad
514a252d550Shaad kmem_cache_free(zio_link_cache, zl);
515c1cb2cd8Shaad }
516c1cb2cd8Shaad
517c1cb2cd8Shaad static boolean_t
zio_wait_for_children(zio_t * zio,enum zio_child child,enum zio_wait_type wait)518c1cb2cd8Shaad zio_wait_for_children(zio_t *zio, enum zio_child child, enum zio_wait_type wait)
519c1cb2cd8Shaad {
520c1cb2cd8Shaad uint64_t *countp = &zio->io_children[child][wait];
521c1cb2cd8Shaad boolean_t waiting = B_FALSE;
522c1cb2cd8Shaad
523c1cb2cd8Shaad mutex_enter(&zio->io_lock);
524c1cb2cd8Shaad ASSERT(zio->io_stall == NULL);
525c1cb2cd8Shaad if (*countp != 0) {
526a252d550Shaad zio->io_stage >>= 1;
527eada09acSchs ASSERT3U(zio->io_stage, !=, ZIO_STAGE_OPEN);
528c1cb2cd8Shaad zio->io_stall = countp;
529c1cb2cd8Shaad waiting = B_TRUE;
530c1cb2cd8Shaad }
531c1cb2cd8Shaad mutex_exit(&zio->io_lock);
532c1cb2cd8Shaad
533c1cb2cd8Shaad return (waiting);
534c1cb2cd8Shaad }
535c1cb2cd8Shaad
536c1cb2cd8Shaad static void
zio_notify_parent(zio_t * pio,zio_t * zio,enum zio_wait_type wait)537c1cb2cd8Shaad zio_notify_parent(zio_t *pio, zio_t *zio, enum zio_wait_type wait)
538c1cb2cd8Shaad {
539c1cb2cd8Shaad uint64_t *countp = &pio->io_children[zio->io_child_type][wait];
540c1cb2cd8Shaad int *errorp = &pio->io_child_error[zio->io_child_type];
541c1cb2cd8Shaad
542c1cb2cd8Shaad mutex_enter(&pio->io_lock);
543c1cb2cd8Shaad if (zio->io_error && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
544c1cb2cd8Shaad *errorp = zio_worst_error(*errorp, zio->io_error);
545c1cb2cd8Shaad pio->io_reexecute |= zio->io_reexecute;
546c1cb2cd8Shaad ASSERT3U(*countp, >, 0);
547eada09acSchs
548eada09acSchs (*countp)--;
549eada09acSchs
550eada09acSchs if (*countp == 0 && pio->io_stall == countp) {
551eada09acSchs zio_taskq_type_t type =
552eada09acSchs pio->io_stage < ZIO_STAGE_VDEV_IO_START ? ZIO_TASKQ_ISSUE :
553eada09acSchs ZIO_TASKQ_INTERRUPT;
554c1cb2cd8Shaad pio->io_stall = NULL;
555c1cb2cd8Shaad mutex_exit(&pio->io_lock);
556eada09acSchs /*
557eada09acSchs * Dispatch the parent zio in its own taskq so that
558eada09acSchs * the child can continue to make progress. This also
559eada09acSchs * prevents overflowing the stack when we have deeply nested
560eada09acSchs * parent-child relationships.
561eada09acSchs */
562eada09acSchs zio_taskq_dispatch(pio, type, B_FALSE);
563c1cb2cd8Shaad } else {
564c1cb2cd8Shaad mutex_exit(&pio->io_lock);
565c1cb2cd8Shaad }
566c1cb2cd8Shaad }
567c1cb2cd8Shaad
568c1cb2cd8Shaad static void
zio_inherit_child_errors(zio_t * zio,enum zio_child c)569c1cb2cd8Shaad zio_inherit_child_errors(zio_t *zio, enum zio_child c)
570c1cb2cd8Shaad {
571c1cb2cd8Shaad if (zio->io_child_error[c] != 0 && zio->io_error == 0)
572c1cb2cd8Shaad zio->io_error = zio->io_child_error[c];
573c1cb2cd8Shaad }
574c1cb2cd8Shaad
575eada09acSchs int
zio_timestamp_compare(const void * x1,const void * x2)576eada09acSchs zio_timestamp_compare(const void *x1, const void *x2)
577eada09acSchs {
578eada09acSchs const zio_t *z1 = x1;
579eada09acSchs const zio_t *z2 = x2;
580eada09acSchs
581eada09acSchs if (z1->io_queued_timestamp < z2->io_queued_timestamp)
582eada09acSchs return (-1);
583eada09acSchs if (z1->io_queued_timestamp > z2->io_queued_timestamp)
584eada09acSchs return (1);
585eada09acSchs
586eada09acSchs if (z1->io_bookmark.zb_objset < z2->io_bookmark.zb_objset)
587eada09acSchs return (-1);
588eada09acSchs if (z1->io_bookmark.zb_objset > z2->io_bookmark.zb_objset)
589eada09acSchs return (1);
590eada09acSchs
591eada09acSchs if (z1->io_bookmark.zb_object < z2->io_bookmark.zb_object)
592eada09acSchs return (-1);
593eada09acSchs if (z1->io_bookmark.zb_object > z2->io_bookmark.zb_object)
594eada09acSchs return (1);
595eada09acSchs
596eada09acSchs if (z1->io_bookmark.zb_level < z2->io_bookmark.zb_level)
597eada09acSchs return (-1);
598eada09acSchs if (z1->io_bookmark.zb_level > z2->io_bookmark.zb_level)
599eada09acSchs return (1);
600eada09acSchs
601eada09acSchs if (z1->io_bookmark.zb_blkid < z2->io_bookmark.zb_blkid)
602eada09acSchs return (-1);
603eada09acSchs if (z1->io_bookmark.zb_blkid > z2->io_bookmark.zb_blkid)
604eada09acSchs return (1);
605eada09acSchs
606eada09acSchs if (z1 < z2)
607eada09acSchs return (-1);
608eada09acSchs if (z1 > z2)
609eada09acSchs return (1);
610eada09acSchs
611eada09acSchs return (0);
612eada09acSchs }
613eada09acSchs
614c1cb2cd8Shaad /*
615c1cb2cd8Shaad * ==========================================================================
616c1cb2cd8Shaad * Create the various types of I/O (read, write, free, etc)
617c1cb2cd8Shaad * ==========================================================================
618c1cb2cd8Shaad */
619c1cb2cd8Shaad static zio_t *
zio_create(zio_t * pio,spa_t * spa,uint64_t txg,const blkptr_t * bp,void * data,uint64_t size,zio_done_func_t * done,void * private,zio_type_t type,zio_priority_t priority,enum zio_flag flags,vdev_t * vd,uint64_t offset,const zbookmark_phys_t * zb,enum zio_stage stage,enum zio_stage pipeline)620a252d550Shaad zio_create(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
621c1cb2cd8Shaad void *data, uint64_t size, zio_done_func_t *done, void *private,
622eada09acSchs zio_type_t type, zio_priority_t priority, enum zio_flag flags,
623eada09acSchs vdev_t *vd, uint64_t offset, const zbookmark_phys_t *zb,
624a252d550Shaad enum zio_stage stage, enum zio_stage pipeline)
625c1cb2cd8Shaad {
626c1cb2cd8Shaad zio_t *zio;
627c1cb2cd8Shaad
628eada09acSchs ASSERT3U(type == ZIO_TYPE_FREE || size, <=, SPA_MAXBLOCKSIZE);
629c1cb2cd8Shaad ASSERT(P2PHASE(size, SPA_MINBLOCKSIZE) == 0);
630c1cb2cd8Shaad ASSERT(P2PHASE(offset, SPA_MINBLOCKSIZE) == 0);
631c1cb2cd8Shaad
632c1cb2cd8Shaad ASSERT(!vd || spa_config_held(spa, SCL_STATE_ALL, RW_READER));
633c1cb2cd8Shaad ASSERT(!bp || !(flags & ZIO_FLAG_CONFIG_WRITER));
634c1cb2cd8Shaad ASSERT(vd || stage == ZIO_STAGE_OPEN);
635c1cb2cd8Shaad
636c1cb2cd8Shaad zio = kmem_cache_alloc(zio_cache, KM_SLEEP);
637c1cb2cd8Shaad bzero(zio, sizeof (zio_t));
638c1cb2cd8Shaad
639c1cb2cd8Shaad mutex_init(&zio->io_lock, NULL, MUTEX_DEFAULT, NULL);
640c1cb2cd8Shaad cv_init(&zio->io_cv, NULL, CV_DEFAULT, NULL);
641c1cb2cd8Shaad
642a252d550Shaad list_create(&zio->io_parent_list, sizeof (zio_link_t),
643a252d550Shaad offsetof(zio_link_t, zl_parent_node));
644a252d550Shaad list_create(&zio->io_child_list, sizeof (zio_link_t),
645a252d550Shaad offsetof(zio_link_t, zl_child_node));
646eada09acSchs metaslab_trace_init(&zio->io_alloc_list);
647a252d550Shaad
648c1cb2cd8Shaad if (vd != NULL)
649c1cb2cd8Shaad zio->io_child_type = ZIO_CHILD_VDEV;
650c1cb2cd8Shaad else if (flags & ZIO_FLAG_GANG_CHILD)
651c1cb2cd8Shaad zio->io_child_type = ZIO_CHILD_GANG;
652a252d550Shaad else if (flags & ZIO_FLAG_DDT_CHILD)
653a252d550Shaad zio->io_child_type = ZIO_CHILD_DDT;
654c1cb2cd8Shaad else
655c1cb2cd8Shaad zio->io_child_type = ZIO_CHILD_LOGICAL;
656c1cb2cd8Shaad
657c1cb2cd8Shaad if (bp != NULL) {
658a252d550Shaad zio->io_bp = (blkptr_t *)bp;
659c1cb2cd8Shaad zio->io_bp_copy = *bp;
660c1cb2cd8Shaad zio->io_bp_orig = *bp;
661a252d550Shaad if (type != ZIO_TYPE_WRITE ||
662a252d550Shaad zio->io_child_type == ZIO_CHILD_DDT)
663c1cb2cd8Shaad zio->io_bp = &zio->io_bp_copy; /* so caller can free */
664a252d550Shaad if (zio->io_child_type == ZIO_CHILD_LOGICAL)
665c1cb2cd8Shaad zio->io_logical = zio;
666a252d550Shaad if (zio->io_child_type > ZIO_CHILD_GANG && BP_IS_GANG(bp))
667a252d550Shaad pipeline |= ZIO_GANG_STAGES;
668c1cb2cd8Shaad }
669c1cb2cd8Shaad
670c1cb2cd8Shaad zio->io_spa = spa;
671c1cb2cd8Shaad zio->io_txg = txg;
672c1cb2cd8Shaad zio->io_done = done;
673c1cb2cd8Shaad zio->io_private = private;
674c1cb2cd8Shaad zio->io_type = type;
675c1cb2cd8Shaad zio->io_priority = priority;
676c1cb2cd8Shaad zio->io_vd = vd;
677c1cb2cd8Shaad zio->io_offset = offset;
678a252d550Shaad zio->io_orig_data = zio->io_data = data;
679a252d550Shaad zio->io_orig_size = zio->io_size = size;
680c1cb2cd8Shaad zio->io_orig_flags = zio->io_flags = flags;
681c1cb2cd8Shaad zio->io_orig_stage = zio->io_stage = stage;
682c1cb2cd8Shaad zio->io_orig_pipeline = zio->io_pipeline = pipeline;
683eada09acSchs zio->io_pipeline_trace = ZIO_STAGE_OPEN;
684c1cb2cd8Shaad
685a252d550Shaad zio->io_state[ZIO_WAIT_READY] = (stage >= ZIO_STAGE_READY);
686a252d550Shaad zio->io_state[ZIO_WAIT_DONE] = (stage >= ZIO_STAGE_DONE);
687a252d550Shaad
688c1cb2cd8Shaad if (zb != NULL)
689c1cb2cd8Shaad zio->io_bookmark = *zb;
690c1cb2cd8Shaad
691c1cb2cd8Shaad if (pio != NULL) {
692c1cb2cd8Shaad if (zio->io_logical == NULL)
693c1cb2cd8Shaad zio->io_logical = pio->io_logical;
694a252d550Shaad if (zio->io_child_type == ZIO_CHILD_GANG)
695a252d550Shaad zio->io_gang_leader = pio->io_gang_leader;
696c1cb2cd8Shaad zio_add_child(pio, zio);
697c1cb2cd8Shaad }
698c1cb2cd8Shaad
699c1cb2cd8Shaad return (zio);
700c1cb2cd8Shaad }
701c1cb2cd8Shaad
702c1cb2cd8Shaad static void
zio_destroy(zio_t * zio)703c1cb2cd8Shaad zio_destroy(zio_t *zio)
704c1cb2cd8Shaad {
705eada09acSchs metaslab_trace_fini(&zio->io_alloc_list);
706a252d550Shaad list_destroy(&zio->io_parent_list);
707a252d550Shaad list_destroy(&zio->io_child_list);
708c1cb2cd8Shaad mutex_destroy(&zio->io_lock);
709c1cb2cd8Shaad cv_destroy(&zio->io_cv);
710c1cb2cd8Shaad kmem_cache_free(zio_cache, zio);
711c1cb2cd8Shaad }
712c1cb2cd8Shaad
713c1cb2cd8Shaad zio_t *
zio_null(zio_t * pio,spa_t * spa,vdev_t * vd,zio_done_func_t * done,void * private,enum zio_flag flags)714a252d550Shaad zio_null(zio_t *pio, spa_t *spa, vdev_t *vd, zio_done_func_t *done,
715a252d550Shaad void *private, enum zio_flag flags)
716c1cb2cd8Shaad {
717c1cb2cd8Shaad zio_t *zio;
718c1cb2cd8Shaad
719c1cb2cd8Shaad zio = zio_create(pio, spa, 0, NULL, NULL, 0, done, private,
720a252d550Shaad ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
721c1cb2cd8Shaad ZIO_STAGE_OPEN, ZIO_INTERLOCK_PIPELINE);
722c1cb2cd8Shaad
723c1cb2cd8Shaad return (zio);
724c1cb2cd8Shaad }
725c1cb2cd8Shaad
726c1cb2cd8Shaad zio_t *
zio_root(spa_t * spa,zio_done_func_t * done,void * private,enum zio_flag flags)727a252d550Shaad zio_root(spa_t *spa, zio_done_func_t *done, void *private, enum zio_flag flags)
728c1cb2cd8Shaad {
729a252d550Shaad return (zio_null(NULL, spa, NULL, done, private, flags));
730c1cb2cd8Shaad }
731c1cb2cd8Shaad
732eada09acSchs void
zfs_blkptr_verify(spa_t * spa,const blkptr_t * bp)733eada09acSchs zfs_blkptr_verify(spa_t *spa, const blkptr_t *bp)
734eada09acSchs {
735eada09acSchs if (!DMU_OT_IS_VALID(BP_GET_TYPE(bp))) {
736eada09acSchs zfs_panic_recover("blkptr at %p has invalid TYPE %llu",
737eada09acSchs bp, (longlong_t)BP_GET_TYPE(bp));
738eada09acSchs }
739eada09acSchs if (BP_GET_CHECKSUM(bp) >= ZIO_CHECKSUM_FUNCTIONS ||
740eada09acSchs BP_GET_CHECKSUM(bp) <= ZIO_CHECKSUM_ON) {
741eada09acSchs zfs_panic_recover("blkptr at %p has invalid CHECKSUM %llu",
742eada09acSchs bp, (longlong_t)BP_GET_CHECKSUM(bp));
743eada09acSchs }
744eada09acSchs if (BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_FUNCTIONS ||
745eada09acSchs BP_GET_COMPRESS(bp) <= ZIO_COMPRESS_ON) {
746eada09acSchs zfs_panic_recover("blkptr at %p has invalid COMPRESS %llu",
747eada09acSchs bp, (longlong_t)BP_GET_COMPRESS(bp));
748eada09acSchs }
749eada09acSchs if (BP_GET_LSIZE(bp) > SPA_MAXBLOCKSIZE) {
750eada09acSchs zfs_panic_recover("blkptr at %p has invalid LSIZE %llu",
751eada09acSchs bp, (longlong_t)BP_GET_LSIZE(bp));
752eada09acSchs }
753eada09acSchs if (BP_GET_PSIZE(bp) > SPA_MAXBLOCKSIZE) {
754eada09acSchs zfs_panic_recover("blkptr at %p has invalid PSIZE %llu",
755eada09acSchs bp, (longlong_t)BP_GET_PSIZE(bp));
756eada09acSchs }
757eada09acSchs
758eada09acSchs if (BP_IS_EMBEDDED(bp)) {
759eada09acSchs if (BPE_GET_ETYPE(bp) > NUM_BP_EMBEDDED_TYPES) {
760eada09acSchs zfs_panic_recover("blkptr at %p has invalid ETYPE %llu",
761eada09acSchs bp, (longlong_t)BPE_GET_ETYPE(bp));
762eada09acSchs }
763eada09acSchs }
764eada09acSchs
765eada09acSchs /*
766eada09acSchs * Pool-specific checks.
767eada09acSchs *
768eada09acSchs * Note: it would be nice to verify that the blk_birth and
769eada09acSchs * BP_PHYSICAL_BIRTH() are not too large. However, spa_freeze()
770eada09acSchs * allows the birth time of log blocks (and dmu_sync()-ed blocks
771eada09acSchs * that are in the log) to be arbitrarily large.
772eada09acSchs */
773eada09acSchs for (int i = 0; i < BP_GET_NDVAS(bp); i++) {
774eada09acSchs uint64_t vdevid = DVA_GET_VDEV(&bp->blk_dva[i]);
775eada09acSchs if (vdevid >= spa->spa_root_vdev->vdev_children) {
776eada09acSchs zfs_panic_recover("blkptr at %p DVA %u has invalid "
777eada09acSchs "VDEV %llu",
778eada09acSchs bp, i, (longlong_t)vdevid);
779eada09acSchs continue;
780eada09acSchs }
781eada09acSchs vdev_t *vd = spa->spa_root_vdev->vdev_child[vdevid];
782eada09acSchs if (vd == NULL) {
783eada09acSchs zfs_panic_recover("blkptr at %p DVA %u has invalid "
784eada09acSchs "VDEV %llu",
785eada09acSchs bp, i, (longlong_t)vdevid);
786eada09acSchs continue;
787eada09acSchs }
788eada09acSchs if (vd->vdev_ops == &vdev_hole_ops) {
789eada09acSchs zfs_panic_recover("blkptr at %p DVA %u has hole "
790eada09acSchs "VDEV %llu",
791eada09acSchs bp, i, (longlong_t)vdevid);
792eada09acSchs continue;
793eada09acSchs }
794eada09acSchs if (vd->vdev_ops == &vdev_missing_ops) {
795eada09acSchs /*
796eada09acSchs * "missing" vdevs are valid during import, but we
797eada09acSchs * don't have their detailed info (e.g. asize), so
798eada09acSchs * we can't perform any more checks on them.
799eada09acSchs */
800eada09acSchs continue;
801eada09acSchs }
802eada09acSchs uint64_t offset = DVA_GET_OFFSET(&bp->blk_dva[i]);
803eada09acSchs uint64_t asize = DVA_GET_ASIZE(&bp->blk_dva[i]);
804eada09acSchs if (BP_IS_GANG(bp))
805eada09acSchs asize = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
806eada09acSchs if (offset + asize > vd->vdev_asize) {
807eada09acSchs zfs_panic_recover("blkptr at %p DVA %u has invalid "
808eada09acSchs "OFFSET %llu",
809eada09acSchs bp, i, (longlong_t)offset);
810eada09acSchs }
811eada09acSchs }
812eada09acSchs }
813eada09acSchs
814c1cb2cd8Shaad zio_t *
zio_read(zio_t * pio,spa_t * spa,const blkptr_t * bp,void * data,uint64_t size,zio_done_func_t * done,void * private,zio_priority_t priority,enum zio_flag flags,const zbookmark_phys_t * zb)815c1cb2cd8Shaad zio_read(zio_t *pio, spa_t *spa, const blkptr_t *bp,
816c1cb2cd8Shaad void *data, uint64_t size, zio_done_func_t *done, void *private,
817eada09acSchs zio_priority_t priority, enum zio_flag flags, const zbookmark_phys_t *zb)
818c1cb2cd8Shaad {
819c1cb2cd8Shaad zio_t *zio;
820c1cb2cd8Shaad
821eada09acSchs zfs_blkptr_verify(spa, bp);
822eada09acSchs
823a252d550Shaad zio = zio_create(pio, spa, BP_PHYSICAL_BIRTH(bp), bp,
824c1cb2cd8Shaad data, size, done, private,
825c1cb2cd8Shaad ZIO_TYPE_READ, priority, flags, NULL, 0, zb,
826a252d550Shaad ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
827a252d550Shaad ZIO_DDT_CHILD_READ_PIPELINE : ZIO_READ_PIPELINE);
828c1cb2cd8Shaad
829c1cb2cd8Shaad return (zio);
830c1cb2cd8Shaad }
831c1cb2cd8Shaad
832c1cb2cd8Shaad zio_t *
zio_write(zio_t * pio,spa_t * spa,uint64_t txg,blkptr_t * bp,void * data,uint64_t size,const zio_prop_t * zp,zio_done_func_t * ready,zio_done_func_t * children_ready,zio_done_func_t * physdone,zio_done_func_t * done,void * private,zio_priority_t priority,enum zio_flag flags,const zbookmark_phys_t * zb)833c1cb2cd8Shaad zio_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
834a252d550Shaad void *data, uint64_t size, const zio_prop_t *zp,
835eada09acSchs zio_done_func_t *ready, zio_done_func_t *children_ready,
836eada09acSchs zio_done_func_t *physdone, zio_done_func_t *done,
837eada09acSchs void *private, zio_priority_t priority, enum zio_flag flags,
838eada09acSchs const zbookmark_phys_t *zb)
839c1cb2cd8Shaad {
840c1cb2cd8Shaad zio_t *zio;
841c1cb2cd8Shaad
842c1cb2cd8Shaad ASSERT(zp->zp_checksum >= ZIO_CHECKSUM_OFF &&
843c1cb2cd8Shaad zp->zp_checksum < ZIO_CHECKSUM_FUNCTIONS &&
844c1cb2cd8Shaad zp->zp_compress >= ZIO_COMPRESS_OFF &&
845c1cb2cd8Shaad zp->zp_compress < ZIO_COMPRESS_FUNCTIONS &&
846eada09acSchs DMU_OT_IS_VALID(zp->zp_type) &&
847c1cb2cd8Shaad zp->zp_level < 32 &&
848a252d550Shaad zp->zp_copies > 0 &&
849eada09acSchs zp->zp_copies <= spa_max_replication(spa));
850c1cb2cd8Shaad
851c1cb2cd8Shaad zio = zio_create(pio, spa, txg, bp, data, size, done, private,
852c1cb2cd8Shaad ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb,
853a252d550Shaad ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
854a252d550Shaad ZIO_DDT_CHILD_WRITE_PIPELINE : ZIO_WRITE_PIPELINE);
855c1cb2cd8Shaad
856c1cb2cd8Shaad zio->io_ready = ready;
857eada09acSchs zio->io_children_ready = children_ready;
858eada09acSchs zio->io_physdone = physdone;
859c1cb2cd8Shaad zio->io_prop = *zp;
860c1cb2cd8Shaad
861eada09acSchs /*
862eada09acSchs * Data can be NULL if we are going to call zio_write_override() to
863eada09acSchs * provide the already-allocated BP. But we may need the data to
864eada09acSchs * verify a dedup hit (if requested). In this case, don't try to
865eada09acSchs * dedup (just take the already-allocated BP verbatim).
866eada09acSchs */
867eada09acSchs if (data == NULL && zio->io_prop.zp_dedup_verify) {
868eada09acSchs zio->io_prop.zp_dedup = zio->io_prop.zp_dedup_verify = B_FALSE;
869eada09acSchs }
870eada09acSchs
871c1cb2cd8Shaad return (zio);
872c1cb2cd8Shaad }
873c1cb2cd8Shaad
874c1cb2cd8Shaad zio_t *
zio_rewrite(zio_t * pio,spa_t * spa,uint64_t txg,blkptr_t * bp,void * data,uint64_t size,zio_done_func_t * done,void * private,zio_priority_t priority,enum zio_flag flags,zbookmark_phys_t * zb)875c1cb2cd8Shaad zio_rewrite(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, void *data,
876eada09acSchs uint64_t size, zio_done_func_t *done, void *private,
877eada09acSchs zio_priority_t priority, enum zio_flag flags, zbookmark_phys_t *zb)
878c1cb2cd8Shaad {
879c1cb2cd8Shaad zio_t *zio;
880c1cb2cd8Shaad
881c1cb2cd8Shaad zio = zio_create(pio, spa, txg, bp, data, size, done, private,
882eada09acSchs ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_IO_REWRITE, NULL, 0, zb,
883c1cb2cd8Shaad ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE);
884c1cb2cd8Shaad
885c1cb2cd8Shaad return (zio);
886c1cb2cd8Shaad }
887c1cb2cd8Shaad
888a252d550Shaad void
zio_write_override(zio_t * zio,blkptr_t * bp,int copies,boolean_t nopwrite)889eada09acSchs zio_write_override(zio_t *zio, blkptr_t *bp, int copies, boolean_t nopwrite)
890a252d550Shaad {
891a252d550Shaad ASSERT(zio->io_type == ZIO_TYPE_WRITE);
892a252d550Shaad ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
893a252d550Shaad ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
894a252d550Shaad ASSERT(zio->io_txg == spa_syncing_txg(zio->io_spa));
895a252d550Shaad
896eada09acSchs /*
897eada09acSchs * We must reset the io_prop to match the values that existed
898eada09acSchs * when the bp was first written by dmu_sync() keeping in mind
899eada09acSchs * that nopwrite and dedup are mutually exclusive.
900eada09acSchs */
901eada09acSchs zio->io_prop.zp_dedup = nopwrite ? B_FALSE : zio->io_prop.zp_dedup;
902eada09acSchs zio->io_prop.zp_nopwrite = nopwrite;
903a252d550Shaad zio->io_prop.zp_copies = copies;
904a252d550Shaad zio->io_bp_override = bp;
905a252d550Shaad }
906a252d550Shaad
907a252d550Shaad void
zio_free(spa_t * spa,uint64_t txg,const blkptr_t * bp)908a252d550Shaad zio_free(spa_t *spa, uint64_t txg, const blkptr_t *bp)
909a252d550Shaad {
910eada09acSchs
911eada09acSchs /*
912eada09acSchs * The check for EMBEDDED is a performance optimization. We
913eada09acSchs * process the free here (by ignoring it) rather than
914eada09acSchs * putting it on the list and then processing it in zio_free_sync().
915eada09acSchs */
916eada09acSchs if (BP_IS_EMBEDDED(bp))
917eada09acSchs return;
918eada09acSchs metaslab_check_free(spa, bp);
919eada09acSchs
920eada09acSchs /*
921eada09acSchs * Frees that are for the currently-syncing txg, are not going to be
922eada09acSchs * deferred, and which will not need to do a read (i.e. not GANG or
923eada09acSchs * DEDUP), can be processed immediately. Otherwise, put them on the
924eada09acSchs * in-memory list for later processing.
925eada09acSchs */
926eada09acSchs if (zfs_trim_enabled || BP_IS_GANG(bp) || BP_GET_DEDUP(bp) ||
927eada09acSchs txg != spa->spa_syncing_txg ||
928eada09acSchs spa_sync_pass(spa) >= zfs_sync_pass_deferred_free) {
929eada09acSchs bplist_append(&spa->spa_free_bplist[txg & TXG_MASK], bp);
930eada09acSchs } else {
931eada09acSchs VERIFY0(zio_wait(zio_free_sync(NULL, spa, txg, bp,
932eada09acSchs BP_GET_PSIZE(bp), 0)));
933eada09acSchs }
934a252d550Shaad }
935a252d550Shaad
936c1cb2cd8Shaad zio_t *
zio_free_sync(zio_t * pio,spa_t * spa,uint64_t txg,const blkptr_t * bp,uint64_t size,enum zio_flag flags)937a252d550Shaad zio_free_sync(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
938eada09acSchs uint64_t size, enum zio_flag flags)
939c1cb2cd8Shaad {
940c1cb2cd8Shaad zio_t *zio;
941eada09acSchs enum zio_stage stage = ZIO_FREE_PIPELINE;
942c1cb2cd8Shaad
943c1cb2cd8Shaad ASSERT(!BP_IS_HOLE(bp));
944a252d550Shaad ASSERT(spa_syncing_txg(spa) == txg);
945eada09acSchs ASSERT(spa_sync_pass(spa) < zfs_sync_pass_deferred_free);
946c1cb2cd8Shaad
947eada09acSchs if (BP_IS_EMBEDDED(bp))
948eada09acSchs return (zio_null(pio, spa, NULL, NULL, NULL, 0));
949eada09acSchs
950eada09acSchs metaslab_check_free(spa, bp);
951eada09acSchs arc_freed(spa, bp);
952eada09acSchs
953eada09acSchs if (zfs_trim_enabled)
954eada09acSchs stage |= ZIO_STAGE_ISSUE_ASYNC | ZIO_STAGE_VDEV_IO_START |
955eada09acSchs ZIO_STAGE_VDEV_IO_ASSESS;
956eada09acSchs /*
957eada09acSchs * GANG and DEDUP blocks can induce a read (for the gang block header,
958eada09acSchs * or the DDT), so issue them asynchronously so that this thread is
959eada09acSchs * not tied up.
960eada09acSchs */
961eada09acSchs else if (BP_IS_GANG(bp) || BP_GET_DEDUP(bp))
962eada09acSchs stage |= ZIO_STAGE_ISSUE_ASYNC;
963eada09acSchs
964eada09acSchs flags |= ZIO_FLAG_DONT_QUEUE;
965eada09acSchs
966eada09acSchs zio = zio_create(pio, spa, txg, bp, NULL, size,
967eada09acSchs NULL, NULL, ZIO_TYPE_FREE, ZIO_PRIORITY_NOW, flags,
968eada09acSchs NULL, 0, NULL, ZIO_STAGE_OPEN, stage);
969c1cb2cd8Shaad
970c1cb2cd8Shaad return (zio);
971c1cb2cd8Shaad }
972c1cb2cd8Shaad
973c1cb2cd8Shaad zio_t *
zio_claim(zio_t * pio,spa_t * spa,uint64_t txg,const blkptr_t * bp,zio_done_func_t * done,void * private,enum zio_flag flags)974a252d550Shaad zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
975a252d550Shaad zio_done_func_t *done, void *private, enum zio_flag flags)
976c1cb2cd8Shaad {
977c1cb2cd8Shaad zio_t *zio;
978c1cb2cd8Shaad
979eada09acSchs dprintf_bp(bp, "claiming in txg %llu", txg);
980eada09acSchs
981eada09acSchs if (BP_IS_EMBEDDED(bp))
982eada09acSchs return (zio_null(pio, spa, NULL, NULL, NULL, 0));
983eada09acSchs
984c1cb2cd8Shaad /*
985c1cb2cd8Shaad * A claim is an allocation of a specific block. Claims are needed
986c1cb2cd8Shaad * to support immediate writes in the intent log. The issue is that
987c1cb2cd8Shaad * immediate writes contain committed data, but in a txg that was
988c1cb2cd8Shaad * *not* committed. Upon opening the pool after an unclean shutdown,
989c1cb2cd8Shaad * the intent log claims all blocks that contain immediate write data
990c1cb2cd8Shaad * so that the SPA knows they're in use.
991c1cb2cd8Shaad *
992c1cb2cd8Shaad * All claims *must* be resolved in the first txg -- before the SPA
993c1cb2cd8Shaad * starts allocating blocks -- so that nothing is allocated twice.
994a252d550Shaad * If txg == 0 we just verify that the block is claimable.
995c1cb2cd8Shaad */
996c1cb2cd8Shaad ASSERT3U(spa->spa_uberblock.ub_rootbp.blk_birth, <, spa_first_txg(spa));
997a252d550Shaad ASSERT(txg == spa_first_txg(spa) || txg == 0);
998a252d550Shaad ASSERT(!BP_GET_DEDUP(bp) || !spa_writeable(spa)); /* zdb(1M) */
999c1cb2cd8Shaad
1000c1cb2cd8Shaad zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
1001c1cb2cd8Shaad done, private, ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW, flags,
1002c1cb2cd8Shaad NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE);
1003eada09acSchs ASSERT0(zio->io_queued_timestamp);
1004c1cb2cd8Shaad
1005c1cb2cd8Shaad return (zio);
1006c1cb2cd8Shaad }
1007c1cb2cd8Shaad
1008c1cb2cd8Shaad zio_t *
zio_ioctl(zio_t * pio,spa_t * spa,vdev_t * vd,int cmd,uint64_t offset,uint64_t size,zio_done_func_t * done,void * private,zio_priority_t priority,enum zio_flag flags)1009eada09acSchs zio_ioctl(zio_t *pio, spa_t *spa, vdev_t *vd, int cmd, uint64_t offset,
1010eada09acSchs uint64_t size, zio_done_func_t *done, void *private,
1011eada09acSchs zio_priority_t priority, enum zio_flag flags)
1012c1cb2cd8Shaad {
1013c1cb2cd8Shaad zio_t *zio;
1014c1cb2cd8Shaad int c;
1015c1cb2cd8Shaad
1016c1cb2cd8Shaad if (vd->vdev_children == 0) {
1017eada09acSchs zio = zio_create(pio, spa, 0, NULL, NULL, size, done, private,
1018eada09acSchs ZIO_TYPE_IOCTL, priority, flags, vd, offset, NULL,
1019c1cb2cd8Shaad ZIO_STAGE_OPEN, ZIO_IOCTL_PIPELINE);
1020c1cb2cd8Shaad
1021c1cb2cd8Shaad zio->io_cmd = cmd;
1022c1cb2cd8Shaad } else {
1023a252d550Shaad zio = zio_null(pio, spa, NULL, NULL, NULL, flags);
1024c1cb2cd8Shaad
1025c1cb2cd8Shaad for (c = 0; c < vd->vdev_children; c++)
1026c1cb2cd8Shaad zio_nowait(zio_ioctl(zio, spa, vd->vdev_child[c], cmd,
1027eada09acSchs offset, size, done, private, priority, flags));
1028c1cb2cd8Shaad }
1029c1cb2cd8Shaad
1030c1cb2cd8Shaad return (zio);
1031c1cb2cd8Shaad }
1032c1cb2cd8Shaad
1033c1cb2cd8Shaad zio_t *
zio_read_phys(zio_t * pio,vdev_t * vd,uint64_t offset,uint64_t size,void * data,int checksum,zio_done_func_t * done,void * private,zio_priority_t priority,enum zio_flag flags,boolean_t labels)1034c1cb2cd8Shaad zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
1035c1cb2cd8Shaad void *data, int checksum, zio_done_func_t *done, void *private,
1036eada09acSchs zio_priority_t priority, enum zio_flag flags, boolean_t labels)
1037c1cb2cd8Shaad {
1038c1cb2cd8Shaad zio_t *zio;
1039c1cb2cd8Shaad
1040c1cb2cd8Shaad ASSERT(vd->vdev_children == 0);
1041c1cb2cd8Shaad ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
1042c1cb2cd8Shaad offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
1043c1cb2cd8Shaad ASSERT3U(offset + size, <=, vd->vdev_psize);
1044c1cb2cd8Shaad
1045c1cb2cd8Shaad zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, done, private,
1046eada09acSchs ZIO_TYPE_READ, priority, flags | ZIO_FLAG_PHYSICAL, vd, offset,
1047eada09acSchs NULL, ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE);
1048c1cb2cd8Shaad
1049c1cb2cd8Shaad zio->io_prop.zp_checksum = checksum;
1050c1cb2cd8Shaad
1051c1cb2cd8Shaad return (zio);
1052c1cb2cd8Shaad }
1053c1cb2cd8Shaad
1054c1cb2cd8Shaad zio_t *
zio_write_phys(zio_t * pio,vdev_t * vd,uint64_t offset,uint64_t size,void * data,int checksum,zio_done_func_t * done,void * private,zio_priority_t priority,enum zio_flag flags,boolean_t labels)1055c1cb2cd8Shaad zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
1056c1cb2cd8Shaad void *data, int checksum, zio_done_func_t *done, void *private,
1057eada09acSchs zio_priority_t priority, enum zio_flag flags, boolean_t labels)
1058c1cb2cd8Shaad {
1059c1cb2cd8Shaad zio_t *zio;
1060c1cb2cd8Shaad
1061c1cb2cd8Shaad ASSERT(vd->vdev_children == 0);
1062c1cb2cd8Shaad ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
1063c1cb2cd8Shaad offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
1064c1cb2cd8Shaad ASSERT3U(offset + size, <=, vd->vdev_psize);
1065c1cb2cd8Shaad
1066c1cb2cd8Shaad zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, done, private,
1067eada09acSchs ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_PHYSICAL, vd, offset,
1068eada09acSchs NULL, ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE);
1069c1cb2cd8Shaad
1070c1cb2cd8Shaad zio->io_prop.zp_checksum = checksum;
1071c1cb2cd8Shaad
1072eada09acSchs if (zio_checksum_table[checksum].ci_flags & ZCHECKSUM_FLAG_EMBEDDED) {
1073c1cb2cd8Shaad /*
1074a252d550Shaad * zec checksums are necessarily destructive -- they modify
1075c1cb2cd8Shaad * the end of the write buffer to hold the verifier/checksum.
1076c1cb2cd8Shaad * Therefore, we must make a local copy in case the data is
1077c1cb2cd8Shaad * being written to multiple places in parallel.
1078c1cb2cd8Shaad */
1079c1cb2cd8Shaad void *wbuf = zio_buf_alloc(size);
1080c1cb2cd8Shaad bcopy(data, wbuf, size);
1081c1cb2cd8Shaad zio_push_transform(zio, wbuf, size, size, NULL);
1082c1cb2cd8Shaad }
1083c1cb2cd8Shaad
1084c1cb2cd8Shaad return (zio);
1085c1cb2cd8Shaad }
1086c1cb2cd8Shaad
1087c1cb2cd8Shaad /*
1088c1cb2cd8Shaad * Create a child I/O to do some work for us.
1089c1cb2cd8Shaad */
1090c1cb2cd8Shaad zio_t *
zio_vdev_child_io(zio_t * pio,blkptr_t * bp,vdev_t * vd,uint64_t offset,void * data,uint64_t size,int type,zio_priority_t priority,enum zio_flag flags,zio_done_func_t * done,void * private)1091c1cb2cd8Shaad zio_vdev_child_io(zio_t *pio, blkptr_t *bp, vdev_t *vd, uint64_t offset,
1092eada09acSchs void *data, uint64_t size, int type, zio_priority_t priority,
1093eada09acSchs enum zio_flag flags, zio_done_func_t *done, void *private)
1094c1cb2cd8Shaad {
1095a252d550Shaad enum zio_stage pipeline = ZIO_VDEV_CHILD_PIPELINE;
1096c1cb2cd8Shaad zio_t *zio;
1097c1cb2cd8Shaad
1098c1cb2cd8Shaad ASSERT(vd->vdev_parent ==
1099c1cb2cd8Shaad (pio->io_vd ? pio->io_vd : pio->io_spa->spa_root_vdev));
1100c1cb2cd8Shaad
1101c1cb2cd8Shaad if (type == ZIO_TYPE_READ && bp != NULL) {
1102c1cb2cd8Shaad /*
1103c1cb2cd8Shaad * If we have the bp, then the child should perform the
1104c1cb2cd8Shaad * checksum and the parent need not. This pushes error
1105c1cb2cd8Shaad * detection as close to the leaves as possible and
1106c1cb2cd8Shaad * eliminates redundant checksums in the interior nodes.
1107c1cb2cd8Shaad */
1108a252d550Shaad pipeline |= ZIO_STAGE_CHECKSUM_VERIFY;
1109a252d550Shaad pio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
1110c1cb2cd8Shaad }
1111c1cb2cd8Shaad
1112eada09acSchs /* Not all IO types require vdev io done stage e.g. free */
1113eada09acSchs if (!(pio->io_pipeline & ZIO_STAGE_VDEV_IO_DONE))
1114eada09acSchs pipeline &= ~ZIO_STAGE_VDEV_IO_DONE;
1115eada09acSchs
1116c1cb2cd8Shaad if (vd->vdev_children == 0)
1117c1cb2cd8Shaad offset += VDEV_LABEL_START_SIZE;
1118c1cb2cd8Shaad
1119a252d550Shaad flags |= ZIO_VDEV_CHILD_FLAGS(pio) | ZIO_FLAG_DONT_PROPAGATE;
1120a252d550Shaad
1121a252d550Shaad /*
1122a252d550Shaad * If we've decided to do a repair, the write is not speculative --
1123a252d550Shaad * even if the original read was.
1124a252d550Shaad */
1125a252d550Shaad if (flags & ZIO_FLAG_IO_REPAIR)
1126a252d550Shaad flags &= ~ZIO_FLAG_SPECULATIVE;
1127a252d550Shaad
1128eada09acSchs /*
1129eada09acSchs * If we're creating a child I/O that is not associated with a
1130eada09acSchs * top-level vdev, then the child zio is not an allocating I/O.
1131eada09acSchs * If this is a retried I/O then we ignore it since we will
1132eada09acSchs * have already processed the original allocating I/O.
1133eada09acSchs */
1134eada09acSchs if (flags & ZIO_FLAG_IO_ALLOCATING &&
1135eada09acSchs (vd != vd->vdev_top || (flags & ZIO_FLAG_IO_RETRY))) {
1136eada09acSchs metaslab_class_t *mc = spa_normal_class(pio->io_spa);
1137eada09acSchs
1138eada09acSchs ASSERT(mc->mc_alloc_throttle_enabled);
1139eada09acSchs ASSERT(type == ZIO_TYPE_WRITE);
1140eada09acSchs ASSERT(priority == ZIO_PRIORITY_ASYNC_WRITE);
1141eada09acSchs ASSERT(!(flags & ZIO_FLAG_IO_REPAIR));
1142eada09acSchs ASSERT(!(pio->io_flags & ZIO_FLAG_IO_REWRITE) ||
1143eada09acSchs pio->io_child_type == ZIO_CHILD_GANG);
1144eada09acSchs
1145eada09acSchs flags &= ~ZIO_FLAG_IO_ALLOCATING;
1146eada09acSchs }
1147eada09acSchs
1148c1cb2cd8Shaad zio = zio_create(pio, pio->io_spa, pio->io_txg, bp, data, size,
1149a252d550Shaad done, private, type, priority, flags, vd, offset, &pio->io_bookmark,
1150a252d550Shaad ZIO_STAGE_VDEV_IO_START >> 1, pipeline);
1151eada09acSchs ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
1152eada09acSchs
1153eada09acSchs zio->io_physdone = pio->io_physdone;
1154eada09acSchs if (vd->vdev_ops->vdev_op_leaf && zio->io_logical != NULL)
1155eada09acSchs zio->io_logical->io_phys_children++;
1156c1cb2cd8Shaad
1157c1cb2cd8Shaad return (zio);
1158c1cb2cd8Shaad }
1159c1cb2cd8Shaad
1160c1cb2cd8Shaad zio_t *
zio_vdev_delegated_io(vdev_t * vd,uint64_t offset,void * data,uint64_t size,int type,zio_priority_t priority,enum zio_flag flags,zio_done_func_t * done,void * private)1161c1cb2cd8Shaad zio_vdev_delegated_io(vdev_t *vd, uint64_t offset, void *data, uint64_t size,
1162eada09acSchs int type, zio_priority_t priority, enum zio_flag flags,
1163a252d550Shaad zio_done_func_t *done, void *private)
1164c1cb2cd8Shaad {
1165c1cb2cd8Shaad zio_t *zio;
1166c1cb2cd8Shaad
1167c1cb2cd8Shaad ASSERT(vd->vdev_ops->vdev_op_leaf);
1168c1cb2cd8Shaad
1169c1cb2cd8Shaad zio = zio_create(NULL, vd->vdev_spa, 0, NULL,
1170c1cb2cd8Shaad data, size, done, private, type, priority,
1171eada09acSchs flags | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY | ZIO_FLAG_DELEGATED,
1172c1cb2cd8Shaad vd, offset, NULL,
1173a252d550Shaad ZIO_STAGE_VDEV_IO_START >> 1, ZIO_VDEV_CHILD_PIPELINE);
1174c1cb2cd8Shaad
1175c1cb2cd8Shaad return (zio);
1176c1cb2cd8Shaad }
1177c1cb2cd8Shaad
1178c1cb2cd8Shaad void
zio_flush(zio_t * zio,vdev_t * vd)1179c1cb2cd8Shaad zio_flush(zio_t *zio, vdev_t *vd)
1180c1cb2cd8Shaad {
1181eada09acSchs zio_nowait(zio_ioctl(zio, zio->io_spa, vd, DKIOCFLUSHWRITECACHE, 0, 0,
1182c1cb2cd8Shaad NULL, NULL, ZIO_PRIORITY_NOW,
1183c1cb2cd8Shaad ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY));
1184c1cb2cd8Shaad }
1185c1cb2cd8Shaad
1186eada09acSchs zio_t *
zio_trim(zio_t * zio,spa_t * spa,vdev_t * vd,uint64_t offset,uint64_t size)1187eada09acSchs zio_trim(zio_t *zio, spa_t *spa, vdev_t *vd, uint64_t offset, uint64_t size)
1188eada09acSchs {
1189eada09acSchs
1190eada09acSchs ASSERT(vd->vdev_ops->vdev_op_leaf);
1191eada09acSchs
1192eada09acSchs return (zio_create(zio, spa, 0, NULL, NULL, size, NULL, NULL,
1193eada09acSchs ZIO_TYPE_FREE, ZIO_PRIORITY_TRIM, ZIO_FLAG_DONT_AGGREGATE |
1194eada09acSchs ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY,
1195eada09acSchs vd, offset, NULL, ZIO_STAGE_OPEN, ZIO_FREE_PHYS_PIPELINE));
1196eada09acSchs }
1197eada09acSchs
1198a252d550Shaad void
zio_shrink(zio_t * zio,uint64_t size)1199a252d550Shaad zio_shrink(zio_t *zio, uint64_t size)
1200a252d550Shaad {
1201a252d550Shaad ASSERT(zio->io_executor == NULL);
1202a252d550Shaad ASSERT(zio->io_orig_size == zio->io_size);
1203a252d550Shaad ASSERT(size <= zio->io_size);
1204a252d550Shaad
1205a252d550Shaad /*
1206a252d550Shaad * We don't shrink for raidz because of problems with the
1207a252d550Shaad * reconstruction when reading back less than the block size.
1208a252d550Shaad * Note, BP_IS_RAIDZ() assumes no compression.
1209a252d550Shaad */
1210a252d550Shaad ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
1211a252d550Shaad if (!BP_IS_RAIDZ(zio->io_bp))
1212a252d550Shaad zio->io_orig_size = zio->io_size = size;
1213a252d550Shaad }
1214a252d550Shaad
1215c1cb2cd8Shaad /*
1216c1cb2cd8Shaad * ==========================================================================
1217c1cb2cd8Shaad * Prepare to read and write logical blocks
1218c1cb2cd8Shaad * ==========================================================================
1219c1cb2cd8Shaad */
1220c1cb2cd8Shaad
1221c1cb2cd8Shaad static int
zio_read_bp_init(zio_t * zio)1222c1cb2cd8Shaad zio_read_bp_init(zio_t *zio)
1223c1cb2cd8Shaad {
1224c1cb2cd8Shaad blkptr_t *bp = zio->io_bp;
1225c1cb2cd8Shaad
1226a252d550Shaad if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF &&
1227a252d550Shaad zio->io_child_type == ZIO_CHILD_LOGICAL &&
1228a252d550Shaad !(zio->io_flags & ZIO_FLAG_RAW)) {
1229eada09acSchs uint64_t psize =
1230eada09acSchs BP_IS_EMBEDDED(bp) ? BPE_GET_PSIZE(bp) : BP_GET_PSIZE(bp);
1231a252d550Shaad void *cbuf = zio_buf_alloc(psize);
1232c1cb2cd8Shaad
1233a252d550Shaad zio_push_transform(zio, cbuf, psize, psize, zio_decompress);
1234c1cb2cd8Shaad }
1235c1cb2cd8Shaad
1236eada09acSchs if (BP_IS_EMBEDDED(bp) && BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA) {
1237eada09acSchs zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1238eada09acSchs decode_embedded_bp_compressed(bp, zio->io_data);
1239eada09acSchs } else {
1240eada09acSchs ASSERT(!BP_IS_EMBEDDED(bp));
1241eada09acSchs }
1242eada09acSchs
1243eada09acSchs if (!DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) && BP_GET_LEVEL(bp) == 0)
1244c1cb2cd8Shaad zio->io_flags |= ZIO_FLAG_DONT_CACHE;
1245c1cb2cd8Shaad
1246a252d550Shaad if (BP_GET_TYPE(bp) == DMU_OT_DDT_ZAP)
1247a252d550Shaad zio->io_flags |= ZIO_FLAG_DONT_CACHE;
1248a252d550Shaad
1249a252d550Shaad if (BP_GET_DEDUP(bp) && zio->io_child_type == ZIO_CHILD_LOGICAL)
1250a252d550Shaad zio->io_pipeline = ZIO_DDT_READ_PIPELINE;
1251a252d550Shaad
1252c1cb2cd8Shaad return (ZIO_PIPELINE_CONTINUE);
1253c1cb2cd8Shaad }
1254c1cb2cd8Shaad
1255c1cb2cd8Shaad static int
zio_write_bp_init(zio_t * zio)1256c1cb2cd8Shaad zio_write_bp_init(zio_t *zio)
1257c1cb2cd8Shaad {
1258eada09acSchs if (!IO_IS_ALLOCATING(zio))
1259eada09acSchs return (ZIO_PIPELINE_CONTINUE);
1260eada09acSchs
1261eada09acSchs ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
1262eada09acSchs
1263eada09acSchs if (zio->io_bp_override) {
1264eada09acSchs blkptr_t *bp = zio->io_bp;
1265eada09acSchs zio_prop_t *zp = &zio->io_prop;
1266eada09acSchs
1267eada09acSchs ASSERT(bp->blk_birth != zio->io_txg);
1268eada09acSchs ASSERT(BP_GET_DEDUP(zio->io_bp_override) == 0);
1269eada09acSchs
1270eada09acSchs *bp = *zio->io_bp_override;
1271eada09acSchs zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1272eada09acSchs
1273eada09acSchs if (BP_IS_EMBEDDED(bp))
1274eada09acSchs return (ZIO_PIPELINE_CONTINUE);
1275eada09acSchs
1276eada09acSchs /*
1277eada09acSchs * If we've been overridden and nopwrite is set then
1278eada09acSchs * set the flag accordingly to indicate that a nopwrite
1279eada09acSchs * has already occurred.
1280eada09acSchs */
1281eada09acSchs if (!BP_IS_HOLE(bp) && zp->zp_nopwrite) {
1282eada09acSchs ASSERT(!zp->zp_dedup);
1283eada09acSchs ASSERT3U(BP_GET_CHECKSUM(bp), ==, zp->zp_checksum);
1284eada09acSchs zio->io_flags |= ZIO_FLAG_NOPWRITE;
1285eada09acSchs return (ZIO_PIPELINE_CONTINUE);
1286eada09acSchs }
1287eada09acSchs
1288eada09acSchs ASSERT(!zp->zp_nopwrite);
1289eada09acSchs
1290eada09acSchs if (BP_IS_HOLE(bp) || !zp->zp_dedup)
1291eada09acSchs return (ZIO_PIPELINE_CONTINUE);
1292eada09acSchs
1293eada09acSchs ASSERT((zio_checksum_table[zp->zp_checksum].ci_flags &
1294eada09acSchs ZCHECKSUM_FLAG_DEDUP) || zp->zp_dedup_verify);
1295eada09acSchs
1296eada09acSchs if (BP_GET_CHECKSUM(bp) == zp->zp_checksum) {
1297eada09acSchs BP_SET_DEDUP(bp, 1);
1298eada09acSchs zio->io_pipeline |= ZIO_STAGE_DDT_WRITE;
1299eada09acSchs return (ZIO_PIPELINE_CONTINUE);
1300eada09acSchs }
1301eada09acSchs
1302eada09acSchs /*
1303eada09acSchs * We were unable to handle this as an override bp, treat
1304eada09acSchs * it as a regular write I/O.
1305eada09acSchs */
1306eada09acSchs zio->io_bp_override = NULL;
1307eada09acSchs *bp = zio->io_bp_orig;
1308eada09acSchs zio->io_pipeline = zio->io_orig_pipeline;
1309eada09acSchs }
1310eada09acSchs
1311eada09acSchs return (ZIO_PIPELINE_CONTINUE);
1312eada09acSchs }
1313eada09acSchs
1314eada09acSchs static int
zio_write_compress(zio_t * zio)1315eada09acSchs zio_write_compress(zio_t *zio)
1316eada09acSchs {
1317a252d550Shaad spa_t *spa = zio->io_spa;
1318c1cb2cd8Shaad zio_prop_t *zp = &zio->io_prop;
1319a252d550Shaad enum zio_compress compress = zp->zp_compress;
1320c1cb2cd8Shaad blkptr_t *bp = zio->io_bp;
1321c1cb2cd8Shaad uint64_t lsize = zio->io_size;
1322a252d550Shaad uint64_t psize = lsize;
1323c1cb2cd8Shaad int pass = 1;
1324c1cb2cd8Shaad
1325c1cb2cd8Shaad /*
1326c1cb2cd8Shaad * If our children haven't all reached the ready stage,
1327c1cb2cd8Shaad * wait for them and then repeat this pipeline stage.
1328c1cb2cd8Shaad */
1329c1cb2cd8Shaad if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_READY) ||
1330c1cb2cd8Shaad zio_wait_for_children(zio, ZIO_CHILD_LOGICAL, ZIO_WAIT_READY))
1331c1cb2cd8Shaad return (ZIO_PIPELINE_STOP);
1332c1cb2cd8Shaad
1333c1cb2cd8Shaad if (!IO_IS_ALLOCATING(zio))
1334c1cb2cd8Shaad return (ZIO_PIPELINE_CONTINUE);
1335c1cb2cd8Shaad
1336eada09acSchs if (zio->io_children_ready != NULL) {
1337eada09acSchs /*
1338eada09acSchs * Now that all our children are ready, run the callback
1339eada09acSchs * associated with this zio in case it wants to modify the
1340eada09acSchs * data to be written.
1341eada09acSchs */
1342eada09acSchs ASSERT3U(zp->zp_level, >, 0);
1343eada09acSchs zio->io_children_ready(zio);
1344eada09acSchs }
1345eada09acSchs
1346a252d550Shaad ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
1347eada09acSchs ASSERT(zio->io_bp_override == NULL);
1348a252d550Shaad
1349eada09acSchs if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg) {
1350c1cb2cd8Shaad /*
1351c1cb2cd8Shaad * We're rewriting an existing block, which means we're
1352c1cb2cd8Shaad * working on behalf of spa_sync(). For spa_sync() to
1353c1cb2cd8Shaad * converge, it must eventually be the case that we don't
1354c1cb2cd8Shaad * have to allocate new blocks. But compression changes
1355c1cb2cd8Shaad * the blocksize, which forces a reallocate, and makes
1356c1cb2cd8Shaad * convergence take longer. Therefore, after the first
1357c1cb2cd8Shaad * few passes, stop compressing to ensure convergence.
1358c1cb2cd8Shaad */
1359a252d550Shaad pass = spa_sync_pass(spa);
1360a252d550Shaad
1361a252d550Shaad ASSERT(zio->io_txg == spa_syncing_txg(spa));
1362a252d550Shaad ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1363a252d550Shaad ASSERT(!BP_GET_DEDUP(bp));
1364c1cb2cd8Shaad
1365eada09acSchs if (pass >= zfs_sync_pass_dont_compress)
1366c1cb2cd8Shaad compress = ZIO_COMPRESS_OFF;
1367c1cb2cd8Shaad
1368c1cb2cd8Shaad /* Make sure someone doesn't change their mind on overwrites */
1369eada09acSchs ASSERT(BP_IS_EMBEDDED(bp) || MIN(zp->zp_copies + BP_IS_GANG(bp),
1370a252d550Shaad spa_max_replication(spa)) == BP_GET_NDVAS(bp));
1371c1cb2cd8Shaad }
1372c1cb2cd8Shaad
1373c1cb2cd8Shaad if (compress != ZIO_COMPRESS_OFF) {
1374a252d550Shaad void *cbuf = zio_buf_alloc(lsize);
1375a252d550Shaad psize = zio_compress_data(compress, zio->io_data, cbuf, lsize);
1376a252d550Shaad if (psize == 0 || psize == lsize) {
1377c1cb2cd8Shaad compress = ZIO_COMPRESS_OFF;
1378a252d550Shaad zio_buf_free(cbuf, lsize);
1379eada09acSchs } else if (!zp->zp_dedup && psize <= BPE_PAYLOAD_SIZE &&
1380eada09acSchs zp->zp_level == 0 && !DMU_OT_HAS_FILL(zp->zp_type) &&
1381eada09acSchs spa_feature_is_enabled(spa, SPA_FEATURE_EMBEDDED_DATA)) {
1382eada09acSchs encode_embedded_bp_compressed(bp,
1383eada09acSchs cbuf, compress, lsize, psize);
1384eada09acSchs BPE_SET_ETYPE(bp, BP_EMBEDDED_TYPE_DATA);
1385eada09acSchs BP_SET_TYPE(bp, zio->io_prop.zp_type);
1386eada09acSchs BP_SET_LEVEL(bp, zio->io_prop.zp_level);
1387eada09acSchs zio_buf_free(cbuf, lsize);
1388eada09acSchs bp->blk_birth = zio->io_txg;
1389eada09acSchs zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1390eada09acSchs ASSERT(spa_feature_is_active(spa,
1391eada09acSchs SPA_FEATURE_EMBEDDED_DATA));
1392eada09acSchs return (ZIO_PIPELINE_CONTINUE);
1393a252d550Shaad } else {
1394eada09acSchs /*
1395eada09acSchs * Round up compressed size up to the ashift
1396eada09acSchs * of the smallest-ashift device, and zero the tail.
1397eada09acSchs * This ensures that the compressed size of the BP
1398eada09acSchs * (and thus compressratio property) are correct,
1399eada09acSchs * in that we charge for the padding used to fill out
1400eada09acSchs * the last sector.
1401eada09acSchs */
1402eada09acSchs ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT);
1403eada09acSchs size_t rounded = (size_t)P2ROUNDUP(psize,
1404eada09acSchs 1ULL << spa->spa_min_ashift);
1405eada09acSchs if (rounded >= lsize) {
1406eada09acSchs compress = ZIO_COMPRESS_OFF;
1407eada09acSchs zio_buf_free(cbuf, lsize);
1408eada09acSchs psize = lsize;
1409eada09acSchs } else {
1410eada09acSchs bzero((char *)cbuf + psize, rounded - psize);
1411eada09acSchs psize = rounded;
1412eada09acSchs zio_push_transform(zio, cbuf,
1413eada09acSchs psize, lsize, NULL);
1414c1cb2cd8Shaad }
1415c1cb2cd8Shaad }
1416c1cb2cd8Shaad
1417c1cb2cd8Shaad /*
1418eada09acSchs * We were unable to handle this as an override bp, treat
1419eada09acSchs * it as a regular write I/O.
1420eada09acSchs */
1421eada09acSchs zio->io_bp_override = NULL;
1422eada09acSchs *bp = zio->io_bp_orig;
1423eada09acSchs zio->io_pipeline = zio->io_orig_pipeline;
1424eada09acSchs }
1425eada09acSchs
1426eada09acSchs /*
1427c1cb2cd8Shaad * The final pass of spa_sync() must be all rewrites, but the first
1428c1cb2cd8Shaad * few passes offer a trade-off: allocating blocks defers convergence,
1429c1cb2cd8Shaad * but newly allocated blocks are sequential, so they can be written
1430c1cb2cd8Shaad * to disk faster. Therefore, we allow the first few passes of
1431c1cb2cd8Shaad * spa_sync() to allocate new blocks, but force rewrites after that.
1432c1cb2cd8Shaad * There should only be a handful of blocks after pass 1 in any case.
1433c1cb2cd8Shaad */
1434eada09acSchs if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg &&
1435eada09acSchs BP_GET_PSIZE(bp) == psize &&
1436eada09acSchs pass >= zfs_sync_pass_rewrite) {
1437a252d550Shaad ASSERT(psize != 0);
1438a252d550Shaad enum zio_stage gang_stages = zio->io_pipeline & ZIO_GANG_STAGES;
1439c1cb2cd8Shaad zio->io_pipeline = ZIO_REWRITE_PIPELINE | gang_stages;
1440c1cb2cd8Shaad zio->io_flags |= ZIO_FLAG_IO_REWRITE;
1441c1cb2cd8Shaad } else {
1442c1cb2cd8Shaad BP_ZERO(bp);
1443c1cb2cd8Shaad zio->io_pipeline = ZIO_WRITE_PIPELINE;
1444c1cb2cd8Shaad }
1445c1cb2cd8Shaad
1446a252d550Shaad if (psize == 0) {
1447eada09acSchs if (zio->io_bp_orig.blk_birth != 0 &&
1448eada09acSchs spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) {
1449eada09acSchs BP_SET_LSIZE(bp, lsize);
1450eada09acSchs BP_SET_TYPE(bp, zp->zp_type);
1451eada09acSchs BP_SET_LEVEL(bp, zp->zp_level);
1452eada09acSchs BP_SET_BIRTH(bp, zio->io_txg, 0);
1453eada09acSchs }
1454c1cb2cd8Shaad zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1455c1cb2cd8Shaad } else {
1456c1cb2cd8Shaad ASSERT(zp->zp_checksum != ZIO_CHECKSUM_GANG_HEADER);
1457c1cb2cd8Shaad BP_SET_LSIZE(bp, lsize);
1458eada09acSchs BP_SET_TYPE(bp, zp->zp_type);
1459eada09acSchs BP_SET_LEVEL(bp, zp->zp_level);
1460a252d550Shaad BP_SET_PSIZE(bp, psize);
1461c1cb2cd8Shaad BP_SET_COMPRESS(bp, compress);
1462c1cb2cd8Shaad BP_SET_CHECKSUM(bp, zp->zp_checksum);
1463a252d550Shaad BP_SET_DEDUP(bp, zp->zp_dedup);
1464c1cb2cd8Shaad BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
1465a252d550Shaad if (zp->zp_dedup) {
1466a252d550Shaad ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1467a252d550Shaad ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
1468a252d550Shaad zio->io_pipeline = ZIO_DDT_WRITE_PIPELINE;
1469a252d550Shaad }
1470eada09acSchs if (zp->zp_nopwrite) {
1471eada09acSchs ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1472eada09acSchs ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
1473eada09acSchs zio->io_pipeline |= ZIO_STAGE_NOP_WRITE;
1474a252d550Shaad }
1475eada09acSchs }
1476a252d550Shaad return (ZIO_PIPELINE_CONTINUE);
1477a252d550Shaad }
1478a252d550Shaad
1479a252d550Shaad static int
zio_free_bp_init(zio_t * zio)1480a252d550Shaad zio_free_bp_init(zio_t *zio)
1481a252d550Shaad {
1482a252d550Shaad blkptr_t *bp = zio->io_bp;
1483a252d550Shaad
1484a252d550Shaad if (zio->io_child_type == ZIO_CHILD_LOGICAL) {
1485a252d550Shaad if (BP_GET_DEDUP(bp))
1486a252d550Shaad zio->io_pipeline = ZIO_DDT_FREE_PIPELINE;
1487c1cb2cd8Shaad }
1488c1cb2cd8Shaad
1489c1cb2cd8Shaad return (ZIO_PIPELINE_CONTINUE);
1490c1cb2cd8Shaad }
1491c1cb2cd8Shaad
1492c1cb2cd8Shaad /*
1493c1cb2cd8Shaad * ==========================================================================
1494c1cb2cd8Shaad * Execute the I/O pipeline
1495c1cb2cd8Shaad * ==========================================================================
1496c1cb2cd8Shaad */
1497c1cb2cd8Shaad
1498c1cb2cd8Shaad static void
zio_taskq_dispatch(zio_t * zio,zio_taskq_type_t q,boolean_t cutinline)1499eada09acSchs zio_taskq_dispatch(zio_t *zio, zio_taskq_type_t q, boolean_t cutinline)
1500c1cb2cd8Shaad {
1501a252d550Shaad spa_t *spa = zio->io_spa;
1502c1cb2cd8Shaad zio_type_t t = zio->io_type;
1503eada09acSchs int flags = (cutinline ? TQ_FRONT : 0);
1504eada09acSchs
1505eada09acSchs ASSERT(q == ZIO_TASKQ_ISSUE || q == ZIO_TASKQ_INTERRUPT);
1506c1cb2cd8Shaad
1507c1cb2cd8Shaad /*
1508a252d550Shaad * If we're a config writer or a probe, the normal issue and
1509a252d550Shaad * interrupt threads may all be blocked waiting for the config lock.
1510a252d550Shaad * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL.
1511c1cb2cd8Shaad */
1512a252d550Shaad if (zio->io_flags & (ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_PROBE))
1513c1cb2cd8Shaad t = ZIO_TYPE_NULL;
1514c1cb2cd8Shaad
1515c1cb2cd8Shaad /*
1516c1cb2cd8Shaad * A similar issue exists for the L2ARC write thread until L2ARC 2.0.
1517c1cb2cd8Shaad */
1518c1cb2cd8Shaad if (t == ZIO_TYPE_WRITE && zio->io_vd && zio->io_vd->vdev_aux)
1519c1cb2cd8Shaad t = ZIO_TYPE_NULL;
1520c1cb2cd8Shaad
1521a252d550Shaad /*
1522eada09acSchs * If this is a high priority I/O, then use the high priority taskq if
1523eada09acSchs * available.
1524a252d550Shaad */
1525a252d550Shaad if (zio->io_priority == ZIO_PRIORITY_NOW &&
1526eada09acSchs spa->spa_zio_taskq[t][q + 1].stqs_count != 0)
1527a252d550Shaad q++;
1528a252d550Shaad
1529a252d550Shaad ASSERT3U(q, <, ZIO_TASKQ_TYPES);
1530eada09acSchs
1531eada09acSchs /*
1532eada09acSchs * NB: We are assuming that the zio can only be dispatched
1533eada09acSchs * to a single taskq at a time. It would be a grievous error
1534eada09acSchs * to dispatch the zio to another taskq at the same time.
1535eada09acSchs */
1536a96fd3ecShannken #if defined(illumos) || !defined(_KERNEL)
1537eada09acSchs ASSERT(zio->io_tqent.tqent_next == NULL);
1538a96fd3ecShannken #elif defined(__NetBSD__)
1539a96fd3ecShannken ASSERT(zio->io_tqent.tqent_queued == 0);
1540eada09acSchs #else
1541eada09acSchs ASSERT(zio->io_tqent.tqent_task.ta_pending == 0);
1542eada09acSchs #endif
1543eada09acSchs spa_taskq_dispatch_ent(spa, t, q, (task_func_t *)zio_execute, zio,
1544eada09acSchs flags, &zio->io_tqent);
1545c1cb2cd8Shaad }
1546c1cb2cd8Shaad
1547c1cb2cd8Shaad static boolean_t
zio_taskq_member(zio_t * zio,zio_taskq_type_t q)1548eada09acSchs zio_taskq_member(zio_t *zio, zio_taskq_type_t q)
1549c1cb2cd8Shaad {
1550c1cb2cd8Shaad kthread_t *executor = zio->io_executor;
1551c1cb2cd8Shaad spa_t *spa = zio->io_spa;
1552c1cb2cd8Shaad
1553eada09acSchs for (zio_type_t t = 0; t < ZIO_TYPES; t++) {
1554eada09acSchs spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
1555eada09acSchs uint_t i;
1556eada09acSchs for (i = 0; i < tqs->stqs_count; i++) {
1557eada09acSchs if (taskq_member(tqs->stqs_taskq[i], executor))
1558c1cb2cd8Shaad return (B_TRUE);
1559eada09acSchs }
1560eada09acSchs }
1561c1cb2cd8Shaad
1562c1cb2cd8Shaad return (B_FALSE);
1563c1cb2cd8Shaad }
1564c1cb2cd8Shaad
1565c1cb2cd8Shaad static int
zio_issue_async(zio_t * zio)1566c1cb2cd8Shaad zio_issue_async(zio_t *zio)
1567c1cb2cd8Shaad {
1568a252d550Shaad zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
1569c1cb2cd8Shaad
1570c1cb2cd8Shaad return (ZIO_PIPELINE_STOP);
1571c1cb2cd8Shaad }
1572c1cb2cd8Shaad
1573c1cb2cd8Shaad void
zio_interrupt(zio_t * zio)1574c1cb2cd8Shaad zio_interrupt(zio_t *zio)
1575c1cb2cd8Shaad {
1576a252d550Shaad zio_taskq_dispatch(zio, ZIO_TASKQ_INTERRUPT, B_FALSE);
1577c1cb2cd8Shaad }
1578c1cb2cd8Shaad
1579eada09acSchs void
zio_delay_interrupt(zio_t * zio)1580eada09acSchs zio_delay_interrupt(zio_t *zio)
1581eada09acSchs {
1582eada09acSchs /*
1583eada09acSchs * The timeout_generic() function isn't defined in userspace, so
1584eada09acSchs * rather than trying to implement the function, the zio delay
1585eada09acSchs * functionality has been disabled for userspace builds.
1586eada09acSchs */
1587eada09acSchs
1588eada09acSchs #ifndef __NetBSD__
1589eada09acSchs /* XXXNETBSD implement timeout_generic() with a callout_t in zio_t */
1590eada09acSchs /*
1591eada09acSchs * If io_target_timestamp is zero, then no delay has been registered
1592eada09acSchs * for this IO, thus jump to the end of this function and "skip" the
1593eada09acSchs * delay; issuing it directly to the zio layer.
1594eada09acSchs */
1595eada09acSchs if (zio->io_target_timestamp != 0) {
1596eada09acSchs hrtime_t now = gethrtime();
1597eada09acSchs
1598eada09acSchs if (now >= zio->io_target_timestamp) {
1599eada09acSchs /*
1600eada09acSchs * This IO has already taken longer than the target
1601eada09acSchs * delay to complete, so we don't want to delay it
1602eada09acSchs * any longer; we "miss" the delay and issue it
1603eada09acSchs * directly to the zio layer. This is likely due to
1604eada09acSchs * the target latency being set to a value less than
1605eada09acSchs * the underlying hardware can satisfy (e.g. delay
1606eada09acSchs * set to 1ms, but the disks take 10ms to complete an
1607eada09acSchs * IO request).
1608eada09acSchs */
1609eada09acSchs
1610eada09acSchs DTRACE_PROBE2(zio__delay__miss, zio_t *, zio,
1611eada09acSchs hrtime_t, now);
1612eada09acSchs
1613eada09acSchs zio_interrupt(zio);
1614eada09acSchs } else {
1615eada09acSchs hrtime_t diff = zio->io_target_timestamp - now;
1616eada09acSchs
1617eada09acSchs DTRACE_PROBE3(zio__delay__hit, zio_t *, zio,
1618eada09acSchs hrtime_t, now, hrtime_t, diff);
1619eada09acSchs
1620eada09acSchs (void) timeout_generic(CALLOUT_NORMAL,
1621eada09acSchs (void (*)(void *))zio_interrupt, zio, diff, 1, 0);
1622eada09acSchs }
1623eada09acSchs
1624eada09acSchs return;
1625eada09acSchs }
1626eada09acSchs #endif
1627eada09acSchs
1628eada09acSchs DTRACE_PROBE1(zio__delay__skip, zio_t *, zio);
1629eada09acSchs zio_interrupt(zio);
1630eada09acSchs }
1631eada09acSchs
1632c1cb2cd8Shaad /*
1633c1cb2cd8Shaad * Execute the I/O pipeline until one of the following occurs:
1634eada09acSchs *
1635eada09acSchs * (1) the I/O completes
1636eada09acSchs * (2) the pipeline stalls waiting for dependent child I/Os
1637eada09acSchs * (3) the I/O issues, so we're waiting for an I/O completion interrupt
1638eada09acSchs * (4) the I/O is delegated by vdev-level caching or aggregation
1639eada09acSchs * (5) the I/O is deferred due to vdev-level queueing
1640eada09acSchs * (6) the I/O is handed off to another thread.
1641eada09acSchs *
1642eada09acSchs * In all cases, the pipeline stops whenever there's no CPU work; it never
1643eada09acSchs * burns a thread in cv_wait().
1644c1cb2cd8Shaad *
1645c1cb2cd8Shaad * There's no locking on io_stage because there's no legitimate way
1646c1cb2cd8Shaad * for multiple threads to be attempting to process the same I/O.
1647c1cb2cd8Shaad */
1648a252d550Shaad static zio_pipe_stage_t *zio_pipeline[];
1649c1cb2cd8Shaad
1650c1cb2cd8Shaad void
zio_execute(zio_t * zio)1651c1cb2cd8Shaad zio_execute(zio_t *zio)
1652c1cb2cd8Shaad {
1653c1cb2cd8Shaad zio->io_executor = curthread;
1654c1cb2cd8Shaad
1655eada09acSchs ASSERT3U(zio->io_queued_timestamp, >, 0);
1656eada09acSchs
1657c1cb2cd8Shaad while (zio->io_stage < ZIO_STAGE_DONE) {
1658a252d550Shaad enum zio_stage pipeline = zio->io_pipeline;
1659a252d550Shaad enum zio_stage stage = zio->io_stage;
1660c1cb2cd8Shaad int rv;
1661c1cb2cd8Shaad
1662c1cb2cd8Shaad ASSERT(!MUTEX_HELD(&zio->io_lock));
1663a252d550Shaad ASSERT(ISP2(stage));
1664a252d550Shaad ASSERT(zio->io_stall == NULL);
1665c1cb2cd8Shaad
1666a252d550Shaad do {
1667a252d550Shaad stage <<= 1;
1668a252d550Shaad } while ((stage & pipeline) == 0);
1669c1cb2cd8Shaad
1670c1cb2cd8Shaad ASSERT(stage <= ZIO_STAGE_DONE);
1671c1cb2cd8Shaad
1672c1cb2cd8Shaad /*
1673c1cb2cd8Shaad * If we are in interrupt context and this pipeline stage
1674c1cb2cd8Shaad * will grab a config lock that is held across I/O,
1675a252d550Shaad * or may wait for an I/O that needs an interrupt thread
1676a252d550Shaad * to complete, issue async to avoid deadlock.
1677a252d550Shaad *
1678a252d550Shaad * For VDEV_IO_START, we cut in line so that the io will
1679a252d550Shaad * be sent to disk promptly.
1680c1cb2cd8Shaad */
1681a252d550Shaad if ((stage & ZIO_BLOCKING_STAGES) && zio->io_vd == NULL &&
1682c1cb2cd8Shaad zio_taskq_member(zio, ZIO_TASKQ_INTERRUPT)) {
1683a252d550Shaad boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ?
1684a252d550Shaad zio_requeue_io_start_cut_in_line : B_FALSE;
1685a252d550Shaad zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut);
1686c1cb2cd8Shaad return;
1687c1cb2cd8Shaad }
1688c1cb2cd8Shaad
1689c1cb2cd8Shaad zio->io_stage = stage;
1690eada09acSchs zio->io_pipeline_trace |= zio->io_stage;
1691eada09acSchs rv = zio_pipeline[highbit64(stage) - 1](zio);
1692c1cb2cd8Shaad
1693c1cb2cd8Shaad if (rv == ZIO_PIPELINE_STOP)
1694c1cb2cd8Shaad return;
1695c1cb2cd8Shaad
1696c1cb2cd8Shaad ASSERT(rv == ZIO_PIPELINE_CONTINUE);
1697c1cb2cd8Shaad }
1698c1cb2cd8Shaad }
1699c1cb2cd8Shaad
1700c1cb2cd8Shaad /*
1701c1cb2cd8Shaad * ==========================================================================
1702c1cb2cd8Shaad * Initiate I/O, either sync or async
1703c1cb2cd8Shaad * ==========================================================================
1704c1cb2cd8Shaad */
1705c1cb2cd8Shaad int
zio_wait(zio_t * zio)1706c1cb2cd8Shaad zio_wait(zio_t *zio)
1707c1cb2cd8Shaad {
1708c1cb2cd8Shaad int error;
1709c1cb2cd8Shaad
1710c1cb2cd8Shaad ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
1711c1cb2cd8Shaad ASSERT(zio->io_executor == NULL);
1712c1cb2cd8Shaad
1713c1cb2cd8Shaad zio->io_waiter = curthread;
1714eada09acSchs ASSERT0(zio->io_queued_timestamp);
1715eada09acSchs zio->io_queued_timestamp = gethrtime();
1716c1cb2cd8Shaad
1717c1cb2cd8Shaad zio_execute(zio);
1718c1cb2cd8Shaad
1719c1cb2cd8Shaad mutex_enter(&zio->io_lock);
1720c1cb2cd8Shaad while (zio->io_executor != NULL)
1721c1cb2cd8Shaad cv_wait(&zio->io_cv, &zio->io_lock);
1722c1cb2cd8Shaad mutex_exit(&zio->io_lock);
1723c1cb2cd8Shaad
1724c1cb2cd8Shaad error = zio->io_error;
1725c1cb2cd8Shaad zio_destroy(zio);
1726c1cb2cd8Shaad
1727c1cb2cd8Shaad return (error);
1728c1cb2cd8Shaad }
1729c1cb2cd8Shaad
1730c1cb2cd8Shaad void
zio_nowait(zio_t * zio)1731c1cb2cd8Shaad zio_nowait(zio_t *zio)
1732c1cb2cd8Shaad {
1733c1cb2cd8Shaad ASSERT(zio->io_executor == NULL);
1734c1cb2cd8Shaad
1735a252d550Shaad if (zio->io_child_type == ZIO_CHILD_LOGICAL &&
1736a252d550Shaad zio_unique_parent(zio) == NULL) {
1737c1cb2cd8Shaad /*
1738c1cb2cd8Shaad * This is a logical async I/O with no parent to wait for it.
1739a252d550Shaad * We add it to the spa_async_root_zio "Godfather" I/O which
1740a252d550Shaad * will ensure they complete prior to unloading the pool.
1741c1cb2cd8Shaad */
1742c1cb2cd8Shaad spa_t *spa = zio->io_spa;
1743a252d550Shaad
1744eada09acSchs zio_add_child(spa->spa_async_zio_root[CPU_SEQID], zio);
1745c1cb2cd8Shaad }
1746c1cb2cd8Shaad
1747eada09acSchs ASSERT0(zio->io_queued_timestamp);
1748eada09acSchs zio->io_queued_timestamp = gethrtime();
1749c1cb2cd8Shaad zio_execute(zio);
1750c1cb2cd8Shaad }
1751c1cb2cd8Shaad
1752c1cb2cd8Shaad /*
1753c1cb2cd8Shaad * ==========================================================================
1754c1cb2cd8Shaad * Reexecute or suspend/resume failed I/O
1755c1cb2cd8Shaad * ==========================================================================
1756c1cb2cd8Shaad */
1757c1cb2cd8Shaad
1758c1cb2cd8Shaad static void
zio_reexecute(zio_t * pio)1759c1cb2cd8Shaad zio_reexecute(zio_t *pio)
1760c1cb2cd8Shaad {
1761a252d550Shaad zio_t *cio, *cio_next;
1762a252d550Shaad
1763a252d550Shaad ASSERT(pio->io_child_type == ZIO_CHILD_LOGICAL);
1764a252d550Shaad ASSERT(pio->io_orig_stage == ZIO_STAGE_OPEN);
1765a252d550Shaad ASSERT(pio->io_gang_leader == NULL);
1766a252d550Shaad ASSERT(pio->io_gang_tree == NULL);
1767c1cb2cd8Shaad
1768c1cb2cd8Shaad pio->io_flags = pio->io_orig_flags;
1769c1cb2cd8Shaad pio->io_stage = pio->io_orig_stage;
1770c1cb2cd8Shaad pio->io_pipeline = pio->io_orig_pipeline;
1771c1cb2cd8Shaad pio->io_reexecute = 0;
1772eada09acSchs pio->io_flags |= ZIO_FLAG_REEXECUTED;
1773eada09acSchs pio->io_pipeline_trace = 0;
1774c1cb2cd8Shaad pio->io_error = 0;
1775a252d550Shaad for (int w = 0; w < ZIO_WAIT_TYPES; w++)
1776a252d550Shaad pio->io_state[w] = 0;
1777c1cb2cd8Shaad for (int c = 0; c < ZIO_CHILD_TYPES; c++)
1778c1cb2cd8Shaad pio->io_child_error[c] = 0;
1779c1cb2cd8Shaad
1780a252d550Shaad if (IO_IS_ALLOCATING(pio))
1781a252d550Shaad BP_ZERO(pio->io_bp);
1782c1cb2cd8Shaad
1783c1cb2cd8Shaad /*
1784c1cb2cd8Shaad * As we reexecute pio's children, new children could be created.
1785a252d550Shaad * New children go to the head of pio's io_child_list, however,
1786c1cb2cd8Shaad * so we will (correctly) not reexecute them. The key is that
1787a252d550Shaad * the remainder of pio's io_child_list, from 'cio_next' onward,
1788a252d550Shaad * cannot be affected by any side effects of reexecuting 'cio'.
1789c1cb2cd8Shaad */
1790eada09acSchs zio_link_t *zl = NULL;
1791eada09acSchs for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) {
1792eada09acSchs cio_next = zio_walk_children(pio, &zl);
1793c1cb2cd8Shaad mutex_enter(&pio->io_lock);
1794a252d550Shaad for (int w = 0; w < ZIO_WAIT_TYPES; w++)
1795a252d550Shaad pio->io_children[cio->io_child_type][w]++;
1796c1cb2cd8Shaad mutex_exit(&pio->io_lock);
1797a252d550Shaad zio_reexecute(cio);
1798c1cb2cd8Shaad }
1799c1cb2cd8Shaad
1800c1cb2cd8Shaad /*
1801c1cb2cd8Shaad * Now that all children have been reexecuted, execute the parent.
1802a252d550Shaad * We don't reexecute "The Godfather" I/O here as it's the
1803a252d550Shaad * responsibility of the caller to wait on him.
1804c1cb2cd8Shaad */
1805eada09acSchs if (!(pio->io_flags & ZIO_FLAG_GODFATHER)) {
1806eada09acSchs pio->io_queued_timestamp = gethrtime();
1807c1cb2cd8Shaad zio_execute(pio);
1808c1cb2cd8Shaad }
1809eada09acSchs }
1810c1cb2cd8Shaad
1811c1cb2cd8Shaad void
zio_suspend(spa_t * spa,zio_t * zio)1812c1cb2cd8Shaad zio_suspend(spa_t *spa, zio_t *zio)
1813c1cb2cd8Shaad {
1814c1cb2cd8Shaad if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_PANIC)
1815c1cb2cd8Shaad fm_panic("Pool '%s' has encountered an uncorrectable I/O "
1816c1cb2cd8Shaad "failure and the failure mode property for this pool "
1817c1cb2cd8Shaad "is set to panic.", spa_name(spa));
1818c1cb2cd8Shaad
1819c1cb2cd8Shaad zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE, spa, NULL, NULL, 0, 0);
1820c1cb2cd8Shaad
1821c1cb2cd8Shaad mutex_enter(&spa->spa_suspend_lock);
1822c1cb2cd8Shaad
1823c1cb2cd8Shaad if (spa->spa_suspend_zio_root == NULL)
1824a252d550Shaad spa->spa_suspend_zio_root = zio_root(spa, NULL, NULL,
1825a252d550Shaad ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
1826a252d550Shaad ZIO_FLAG_GODFATHER);
1827c1cb2cd8Shaad
1828c1cb2cd8Shaad spa->spa_suspended = B_TRUE;
1829c1cb2cd8Shaad
1830c1cb2cd8Shaad if (zio != NULL) {
1831a252d550Shaad ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
1832c1cb2cd8Shaad ASSERT(zio != spa->spa_suspend_zio_root);
1833c1cb2cd8Shaad ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1834a252d550Shaad ASSERT(zio_unique_parent(zio) == NULL);
1835c1cb2cd8Shaad ASSERT(zio->io_stage == ZIO_STAGE_DONE);
1836c1cb2cd8Shaad zio_add_child(spa->spa_suspend_zio_root, zio);
1837c1cb2cd8Shaad }
1838c1cb2cd8Shaad
1839c1cb2cd8Shaad mutex_exit(&spa->spa_suspend_lock);
1840c1cb2cd8Shaad }
1841c1cb2cd8Shaad
1842a252d550Shaad int
zio_resume(spa_t * spa)1843c1cb2cd8Shaad zio_resume(spa_t *spa)
1844c1cb2cd8Shaad {
1845a252d550Shaad zio_t *pio;
1846c1cb2cd8Shaad
1847c1cb2cd8Shaad /*
1848c1cb2cd8Shaad * Reexecute all previously suspended i/o.
1849c1cb2cd8Shaad */
1850c1cb2cd8Shaad mutex_enter(&spa->spa_suspend_lock);
1851c1cb2cd8Shaad spa->spa_suspended = B_FALSE;
1852c1cb2cd8Shaad cv_broadcast(&spa->spa_suspend_cv);
1853c1cb2cd8Shaad pio = spa->spa_suspend_zio_root;
1854c1cb2cd8Shaad spa->spa_suspend_zio_root = NULL;
1855c1cb2cd8Shaad mutex_exit(&spa->spa_suspend_lock);
1856c1cb2cd8Shaad
1857c1cb2cd8Shaad if (pio == NULL)
1858a252d550Shaad return (0);
1859c1cb2cd8Shaad
1860a252d550Shaad zio_reexecute(pio);
1861a252d550Shaad return (zio_wait(pio));
1862c1cb2cd8Shaad }
1863c1cb2cd8Shaad
1864c1cb2cd8Shaad void
zio_resume_wait(spa_t * spa)1865c1cb2cd8Shaad zio_resume_wait(spa_t *spa)
1866c1cb2cd8Shaad {
1867c1cb2cd8Shaad mutex_enter(&spa->spa_suspend_lock);
1868c1cb2cd8Shaad while (spa_suspended(spa))
1869c1cb2cd8Shaad cv_wait(&spa->spa_suspend_cv, &spa->spa_suspend_lock);
1870c1cb2cd8Shaad mutex_exit(&spa->spa_suspend_lock);
1871c1cb2cd8Shaad }
1872c1cb2cd8Shaad
1873c1cb2cd8Shaad /*
1874c1cb2cd8Shaad * ==========================================================================
1875c1cb2cd8Shaad * Gang blocks.
1876c1cb2cd8Shaad *
1877c1cb2cd8Shaad * A gang block is a collection of small blocks that looks to the DMU
1878c1cb2cd8Shaad * like one large block. When zio_dva_allocate() cannot find a block
1879c1cb2cd8Shaad * of the requested size, due to either severe fragmentation or the pool
1880c1cb2cd8Shaad * being nearly full, it calls zio_write_gang_block() to construct the
1881c1cb2cd8Shaad * block from smaller fragments.
1882c1cb2cd8Shaad *
1883c1cb2cd8Shaad * A gang block consists of a gang header (zio_gbh_phys_t) and up to
1884c1cb2cd8Shaad * three (SPA_GBH_NBLKPTRS) gang members. The gang header is just like
1885c1cb2cd8Shaad * an indirect block: it's an array of block pointers. It consumes
1886c1cb2cd8Shaad * only one sector and hence is allocatable regardless of fragmentation.
1887c1cb2cd8Shaad * The gang header's bps point to its gang members, which hold the data.
1888c1cb2cd8Shaad *
1889c1cb2cd8Shaad * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg>
1890c1cb2cd8Shaad * as the verifier to ensure uniqueness of the SHA256 checksum.
1891c1cb2cd8Shaad * Critically, the gang block bp's blk_cksum is the checksum of the data,
1892c1cb2cd8Shaad * not the gang header. This ensures that data block signatures (needed for
1893c1cb2cd8Shaad * deduplication) are independent of how the block is physically stored.
1894c1cb2cd8Shaad *
1895c1cb2cd8Shaad * Gang blocks can be nested: a gang member may itself be a gang block.
1896c1cb2cd8Shaad * Thus every gang block is a tree in which root and all interior nodes are
1897c1cb2cd8Shaad * gang headers, and the leaves are normal blocks that contain user data.
1898c1cb2cd8Shaad * The root of the gang tree is called the gang leader.
1899c1cb2cd8Shaad *
1900c1cb2cd8Shaad * To perform any operation (read, rewrite, free, claim) on a gang block,
1901c1cb2cd8Shaad * zio_gang_assemble() first assembles the gang tree (minus data leaves)
1902c1cb2cd8Shaad * in the io_gang_tree field of the original logical i/o by recursively
1903c1cb2cd8Shaad * reading the gang leader and all gang headers below it. This yields
1904c1cb2cd8Shaad * an in-core tree containing the contents of every gang header and the
1905c1cb2cd8Shaad * bps for every constituent of the gang block.
1906c1cb2cd8Shaad *
1907c1cb2cd8Shaad * With the gang tree now assembled, zio_gang_issue() just walks the gang tree
1908c1cb2cd8Shaad * and invokes a callback on each bp. To free a gang block, zio_gang_issue()
1909c1cb2cd8Shaad * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp.
1910c1cb2cd8Shaad * zio_claim_gang() provides a similarly trivial wrapper for zio_claim().
1911c1cb2cd8Shaad * zio_read_gang() is a wrapper around zio_read() that omits reading gang
1912c1cb2cd8Shaad * headers, since we already have those in io_gang_tree. zio_rewrite_gang()
1913c1cb2cd8Shaad * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite()
1914c1cb2cd8Shaad * of the gang header plus zio_checksum_compute() of the data to update the
1915c1cb2cd8Shaad * gang header's blk_cksum as described above.
1916c1cb2cd8Shaad *
1917c1cb2cd8Shaad * The two-phase assemble/issue model solves the problem of partial failure --
1918c1cb2cd8Shaad * what if you'd freed part of a gang block but then couldn't read the
1919c1cb2cd8Shaad * gang header for another part? Assembling the entire gang tree first
1920c1cb2cd8Shaad * ensures that all the necessary gang header I/O has succeeded before
1921c1cb2cd8Shaad * starting the actual work of free, claim, or write. Once the gang tree
1922c1cb2cd8Shaad * is assembled, free and claim are in-memory operations that cannot fail.
1923c1cb2cd8Shaad *
1924c1cb2cd8Shaad * In the event that a gang write fails, zio_dva_unallocate() walks the
1925c1cb2cd8Shaad * gang tree to immediately free (i.e. insert back into the space map)
1926c1cb2cd8Shaad * everything we've allocated. This ensures that we don't get ENOSPC
1927c1cb2cd8Shaad * errors during repeated suspend/resume cycles due to a flaky device.
1928c1cb2cd8Shaad *
1929c1cb2cd8Shaad * Gang rewrites only happen during sync-to-convergence. If we can't assemble
1930c1cb2cd8Shaad * the gang tree, we won't modify the block, so we can safely defer the free
1931c1cb2cd8Shaad * (knowing that the block is still intact). If we *can* assemble the gang
1932c1cb2cd8Shaad * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free
1933c1cb2cd8Shaad * each constituent bp and we can allocate a new block on the next sync pass.
1934c1cb2cd8Shaad *
1935c1cb2cd8Shaad * In all cases, the gang tree allows complete recovery from partial failure.
1936c1cb2cd8Shaad * ==========================================================================
1937c1cb2cd8Shaad */
1938c1cb2cd8Shaad
1939c1cb2cd8Shaad static zio_t *
zio_read_gang(zio_t * pio,blkptr_t * bp,zio_gang_node_t * gn,void * data)1940c1cb2cd8Shaad zio_read_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1941c1cb2cd8Shaad {
1942c1cb2cd8Shaad if (gn != NULL)
1943c1cb2cd8Shaad return (pio);
1944c1cb2cd8Shaad
1945c1cb2cd8Shaad return (zio_read(pio, pio->io_spa, bp, data, BP_GET_PSIZE(bp),
1946c1cb2cd8Shaad NULL, NULL, pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
1947c1cb2cd8Shaad &pio->io_bookmark));
1948c1cb2cd8Shaad }
1949c1cb2cd8Shaad
1950c1cb2cd8Shaad zio_t *
zio_rewrite_gang(zio_t * pio,blkptr_t * bp,zio_gang_node_t * gn,void * data)1951c1cb2cd8Shaad zio_rewrite_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1952c1cb2cd8Shaad {
1953c1cb2cd8Shaad zio_t *zio;
1954c1cb2cd8Shaad
1955c1cb2cd8Shaad if (gn != NULL) {
1956c1cb2cd8Shaad zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
1957c1cb2cd8Shaad gn->gn_gbh, SPA_GANGBLOCKSIZE, NULL, NULL, pio->io_priority,
1958c1cb2cd8Shaad ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
1959c1cb2cd8Shaad /*
1960c1cb2cd8Shaad * As we rewrite each gang header, the pipeline will compute
1961c1cb2cd8Shaad * a new gang block header checksum for it; but no one will
1962c1cb2cd8Shaad * compute a new data checksum, so we do that here. The one
1963c1cb2cd8Shaad * exception is the gang leader: the pipeline already computed
1964c1cb2cd8Shaad * its data checksum because that stage precedes gang assembly.
1965c1cb2cd8Shaad * (Presently, nothing actually uses interior data checksums;
1966c1cb2cd8Shaad * this is just good hygiene.)
1967c1cb2cd8Shaad */
1968a252d550Shaad if (gn != pio->io_gang_leader->io_gang_tree) {
1969c1cb2cd8Shaad zio_checksum_compute(zio, BP_GET_CHECKSUM(bp),
1970c1cb2cd8Shaad data, BP_GET_PSIZE(bp));
1971c1cb2cd8Shaad }
1972a252d550Shaad /*
1973a252d550Shaad * If we are here to damage data for testing purposes,
1974a252d550Shaad * leave the GBH alone so that we can detect the damage.
1975a252d550Shaad */
1976a252d550Shaad if (pio->io_gang_leader->io_flags & ZIO_FLAG_INDUCE_DAMAGE)
1977a252d550Shaad zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
1978c1cb2cd8Shaad } else {
1979c1cb2cd8Shaad zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
1980c1cb2cd8Shaad data, BP_GET_PSIZE(bp), NULL, NULL, pio->io_priority,
1981c1cb2cd8Shaad ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
1982c1cb2cd8Shaad }
1983c1cb2cd8Shaad
1984c1cb2cd8Shaad return (zio);
1985c1cb2cd8Shaad }
1986c1cb2cd8Shaad
1987c1cb2cd8Shaad /* ARGSUSED */
1988c1cb2cd8Shaad zio_t *
zio_free_gang(zio_t * pio,blkptr_t * bp,zio_gang_node_t * gn,void * data)1989c1cb2cd8Shaad zio_free_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1990c1cb2cd8Shaad {
1991a252d550Shaad return (zio_free_sync(pio, pio->io_spa, pio->io_txg, bp,
1992eada09acSchs BP_IS_GANG(bp) ? SPA_GANGBLOCKSIZE : BP_GET_PSIZE(bp),
1993a252d550Shaad ZIO_GANG_CHILD_FLAGS(pio)));
1994c1cb2cd8Shaad }
1995c1cb2cd8Shaad
1996c1cb2cd8Shaad /* ARGSUSED */
1997c1cb2cd8Shaad zio_t *
zio_claim_gang(zio_t * pio,blkptr_t * bp,zio_gang_node_t * gn,void * data)1998c1cb2cd8Shaad zio_claim_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1999c1cb2cd8Shaad {
2000c1cb2cd8Shaad return (zio_claim(pio, pio->io_spa, pio->io_txg, bp,
2001c1cb2cd8Shaad NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio)));
2002c1cb2cd8Shaad }
2003c1cb2cd8Shaad
2004c1cb2cd8Shaad static zio_gang_issue_func_t *zio_gang_issue_func[ZIO_TYPES] = {
2005c1cb2cd8Shaad NULL,
2006c1cb2cd8Shaad zio_read_gang,
2007c1cb2cd8Shaad zio_rewrite_gang,
2008c1cb2cd8Shaad zio_free_gang,
2009c1cb2cd8Shaad zio_claim_gang,
2010c1cb2cd8Shaad NULL
2011c1cb2cd8Shaad };
2012c1cb2cd8Shaad
2013c1cb2cd8Shaad static void zio_gang_tree_assemble_done(zio_t *zio);
2014c1cb2cd8Shaad
2015c1cb2cd8Shaad static zio_gang_node_t *
zio_gang_node_alloc(zio_gang_node_t ** gnpp)2016c1cb2cd8Shaad zio_gang_node_alloc(zio_gang_node_t **gnpp)
2017c1cb2cd8Shaad {
2018c1cb2cd8Shaad zio_gang_node_t *gn;
2019c1cb2cd8Shaad
2020c1cb2cd8Shaad ASSERT(*gnpp == NULL);
2021c1cb2cd8Shaad
2022c1cb2cd8Shaad gn = kmem_zalloc(sizeof (*gn), KM_SLEEP);
2023c1cb2cd8Shaad gn->gn_gbh = zio_buf_alloc(SPA_GANGBLOCKSIZE);
2024c1cb2cd8Shaad *gnpp = gn;
2025c1cb2cd8Shaad
2026c1cb2cd8Shaad return (gn);
2027c1cb2cd8Shaad }
2028c1cb2cd8Shaad
2029c1cb2cd8Shaad static void
zio_gang_node_free(zio_gang_node_t ** gnpp)2030c1cb2cd8Shaad zio_gang_node_free(zio_gang_node_t **gnpp)
2031c1cb2cd8Shaad {
2032c1cb2cd8Shaad zio_gang_node_t *gn = *gnpp;
2033c1cb2cd8Shaad
2034c1cb2cd8Shaad for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
2035c1cb2cd8Shaad ASSERT(gn->gn_child[g] == NULL);
2036c1cb2cd8Shaad
2037c1cb2cd8Shaad zio_buf_free(gn->gn_gbh, SPA_GANGBLOCKSIZE);
2038c1cb2cd8Shaad kmem_free(gn, sizeof (*gn));
2039c1cb2cd8Shaad *gnpp = NULL;
2040c1cb2cd8Shaad }
2041c1cb2cd8Shaad
2042c1cb2cd8Shaad static void
zio_gang_tree_free(zio_gang_node_t ** gnpp)2043c1cb2cd8Shaad zio_gang_tree_free(zio_gang_node_t **gnpp)
2044c1cb2cd8Shaad {
2045c1cb2cd8Shaad zio_gang_node_t *gn = *gnpp;
2046c1cb2cd8Shaad
2047c1cb2cd8Shaad if (gn == NULL)
2048c1cb2cd8Shaad return;
2049c1cb2cd8Shaad
2050c1cb2cd8Shaad for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
2051c1cb2cd8Shaad zio_gang_tree_free(&gn->gn_child[g]);
2052c1cb2cd8Shaad
2053c1cb2cd8Shaad zio_gang_node_free(gnpp);
2054c1cb2cd8Shaad }
2055c1cb2cd8Shaad
2056c1cb2cd8Shaad static void
zio_gang_tree_assemble(zio_t * gio,blkptr_t * bp,zio_gang_node_t ** gnpp)2057a252d550Shaad zio_gang_tree_assemble(zio_t *gio, blkptr_t *bp, zio_gang_node_t **gnpp)
2058c1cb2cd8Shaad {
2059c1cb2cd8Shaad zio_gang_node_t *gn = zio_gang_node_alloc(gnpp);
2060c1cb2cd8Shaad
2061a252d550Shaad ASSERT(gio->io_gang_leader == gio);
2062c1cb2cd8Shaad ASSERT(BP_IS_GANG(bp));
2063c1cb2cd8Shaad
2064a252d550Shaad zio_nowait(zio_read(gio, gio->io_spa, bp, gn->gn_gbh,
2065c1cb2cd8Shaad SPA_GANGBLOCKSIZE, zio_gang_tree_assemble_done, gn,
2066a252d550Shaad gio->io_priority, ZIO_GANG_CHILD_FLAGS(gio), &gio->io_bookmark));
2067c1cb2cd8Shaad }
2068c1cb2cd8Shaad
2069c1cb2cd8Shaad static void
zio_gang_tree_assemble_done(zio_t * zio)2070c1cb2cd8Shaad zio_gang_tree_assemble_done(zio_t *zio)
2071c1cb2cd8Shaad {
2072a252d550Shaad zio_t *gio = zio->io_gang_leader;
2073c1cb2cd8Shaad zio_gang_node_t *gn = zio->io_private;
2074c1cb2cd8Shaad blkptr_t *bp = zio->io_bp;
2075c1cb2cd8Shaad
2076a252d550Shaad ASSERT(gio == zio_unique_parent(zio));
2077a252d550Shaad ASSERT(zio->io_child_count == 0);
2078c1cb2cd8Shaad
2079c1cb2cd8Shaad if (zio->io_error)
2080c1cb2cd8Shaad return;
2081c1cb2cd8Shaad
2082c1cb2cd8Shaad if (BP_SHOULD_BYTESWAP(bp))
2083c1cb2cd8Shaad byteswap_uint64_array(zio->io_data, zio->io_size);
2084c1cb2cd8Shaad
2085c1cb2cd8Shaad ASSERT(zio->io_data == gn->gn_gbh);
2086c1cb2cd8Shaad ASSERT(zio->io_size == SPA_GANGBLOCKSIZE);
2087a252d550Shaad ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
2088c1cb2cd8Shaad
2089c1cb2cd8Shaad for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
2090c1cb2cd8Shaad blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
2091c1cb2cd8Shaad if (!BP_IS_GANG(gbp))
2092c1cb2cd8Shaad continue;
2093a252d550Shaad zio_gang_tree_assemble(gio, gbp, &gn->gn_child[g]);
2094c1cb2cd8Shaad }
2095c1cb2cd8Shaad }
2096c1cb2cd8Shaad
2097c1cb2cd8Shaad static void
zio_gang_tree_issue(zio_t * pio,zio_gang_node_t * gn,blkptr_t * bp,void * data)2098c1cb2cd8Shaad zio_gang_tree_issue(zio_t *pio, zio_gang_node_t *gn, blkptr_t *bp, void *data)
2099c1cb2cd8Shaad {
2100a252d550Shaad zio_t *gio = pio->io_gang_leader;
2101c1cb2cd8Shaad zio_t *zio;
2102c1cb2cd8Shaad
2103c1cb2cd8Shaad ASSERT(BP_IS_GANG(bp) == !!gn);
2104a252d550Shaad ASSERT(BP_GET_CHECKSUM(bp) == BP_GET_CHECKSUM(gio->io_bp));
2105a252d550Shaad ASSERT(BP_GET_LSIZE(bp) == BP_GET_PSIZE(bp) || gn == gio->io_gang_tree);
2106c1cb2cd8Shaad
2107c1cb2cd8Shaad /*
2108c1cb2cd8Shaad * If you're a gang header, your data is in gn->gn_gbh.
2109c1cb2cd8Shaad * If you're a gang member, your data is in 'data' and gn == NULL.
2110c1cb2cd8Shaad */
2111a252d550Shaad zio = zio_gang_issue_func[gio->io_type](pio, bp, gn, data);
2112c1cb2cd8Shaad
2113c1cb2cd8Shaad if (gn != NULL) {
2114a252d550Shaad ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
2115c1cb2cd8Shaad
2116c1cb2cd8Shaad for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
2117c1cb2cd8Shaad blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
2118c1cb2cd8Shaad if (BP_IS_HOLE(gbp))
2119c1cb2cd8Shaad continue;
2120c1cb2cd8Shaad zio_gang_tree_issue(zio, gn->gn_child[g], gbp, data);
2121c1cb2cd8Shaad data = (char *)data + BP_GET_PSIZE(gbp);
2122c1cb2cd8Shaad }
2123c1cb2cd8Shaad }
2124c1cb2cd8Shaad
2125eada09acSchs if (gn == gio->io_gang_tree && gio->io_data != NULL)
2126a252d550Shaad ASSERT3P((char *)gio->io_data + gio->io_size, ==, data);
2127c1cb2cd8Shaad
2128c1cb2cd8Shaad if (zio != pio)
2129c1cb2cd8Shaad zio_nowait(zio);
2130c1cb2cd8Shaad }
2131c1cb2cd8Shaad
2132c1cb2cd8Shaad static int
zio_gang_assemble(zio_t * zio)2133c1cb2cd8Shaad zio_gang_assemble(zio_t *zio)
2134c1cb2cd8Shaad {
2135c1cb2cd8Shaad blkptr_t *bp = zio->io_bp;
2136c1cb2cd8Shaad
2137a252d550Shaad ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == NULL);
2138a252d550Shaad ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2139a252d550Shaad
2140a252d550Shaad zio->io_gang_leader = zio;
2141c1cb2cd8Shaad
2142c1cb2cd8Shaad zio_gang_tree_assemble(zio, bp, &zio->io_gang_tree);
2143c1cb2cd8Shaad
2144c1cb2cd8Shaad return (ZIO_PIPELINE_CONTINUE);
2145c1cb2cd8Shaad }
2146c1cb2cd8Shaad
2147c1cb2cd8Shaad static int
zio_gang_issue(zio_t * zio)2148c1cb2cd8Shaad zio_gang_issue(zio_t *zio)
2149c1cb2cd8Shaad {
2150c1cb2cd8Shaad blkptr_t *bp = zio->io_bp;
2151c1cb2cd8Shaad
2152c1cb2cd8Shaad if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE))
2153c1cb2cd8Shaad return (ZIO_PIPELINE_STOP);
2154c1cb2cd8Shaad
2155a252d550Shaad ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == zio);
2156a252d550Shaad ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2157c1cb2cd8Shaad
2158c1cb2cd8Shaad if (zio->io_child_error[ZIO_CHILD_GANG] == 0)
2159a252d550Shaad zio_gang_tree_issue(zio, zio->io_gang_tree, bp, zio->io_data);
2160c1cb2cd8Shaad else
2161a252d550Shaad zio_gang_tree_free(&zio->io_gang_tree);
2162c1cb2cd8Shaad
2163c1cb2cd8Shaad zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2164c1cb2cd8Shaad
2165c1cb2cd8Shaad return (ZIO_PIPELINE_CONTINUE);
2166c1cb2cd8Shaad }
2167c1cb2cd8Shaad
2168c1cb2cd8Shaad static void
zio_write_gang_member_ready(zio_t * zio)2169c1cb2cd8Shaad zio_write_gang_member_ready(zio_t *zio)
2170c1cb2cd8Shaad {
2171a252d550Shaad zio_t *pio = zio_unique_parent(zio);
2172a252d550Shaad zio_t *gio = zio->io_gang_leader;
2173c1cb2cd8Shaad dva_t *cdva = zio->io_bp->blk_dva;
2174c1cb2cd8Shaad dva_t *pdva = pio->io_bp->blk_dva;
2175c1cb2cd8Shaad uint64_t asize;
2176c1cb2cd8Shaad
2177c1cb2cd8Shaad if (BP_IS_HOLE(zio->io_bp))
2178c1cb2cd8Shaad return;
2179c1cb2cd8Shaad
2180c1cb2cd8Shaad ASSERT(BP_IS_HOLE(&zio->io_bp_orig));
2181c1cb2cd8Shaad
2182c1cb2cd8Shaad ASSERT(zio->io_child_type == ZIO_CHILD_GANG);
2183a252d550Shaad ASSERT3U(zio->io_prop.zp_copies, ==, gio->io_prop.zp_copies);
2184a252d550Shaad ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(zio->io_bp));
2185a252d550Shaad ASSERT3U(pio->io_prop.zp_copies, <=, BP_GET_NDVAS(pio->io_bp));
2186c1cb2cd8Shaad ASSERT3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp));
2187c1cb2cd8Shaad
2188c1cb2cd8Shaad mutex_enter(&pio->io_lock);
2189c1cb2cd8Shaad for (int d = 0; d < BP_GET_NDVAS(zio->io_bp); d++) {
2190c1cb2cd8Shaad ASSERT(DVA_GET_GANG(&pdva[d]));
2191c1cb2cd8Shaad asize = DVA_GET_ASIZE(&pdva[d]);
2192c1cb2cd8Shaad asize += DVA_GET_ASIZE(&cdva[d]);
2193c1cb2cd8Shaad DVA_SET_ASIZE(&pdva[d], asize);
2194c1cb2cd8Shaad }
2195c1cb2cd8Shaad mutex_exit(&pio->io_lock);
2196c1cb2cd8Shaad }
2197c1cb2cd8Shaad
2198c1cb2cd8Shaad static int
zio_write_gang_block(zio_t * pio)2199c1cb2cd8Shaad zio_write_gang_block(zio_t *pio)
2200c1cb2cd8Shaad {
2201c1cb2cd8Shaad spa_t *spa = pio->io_spa;
2202eada09acSchs metaslab_class_t *mc = spa_normal_class(spa);
2203c1cb2cd8Shaad blkptr_t *bp = pio->io_bp;
2204a252d550Shaad zio_t *gio = pio->io_gang_leader;
2205c1cb2cd8Shaad zio_t *zio;
2206c1cb2cd8Shaad zio_gang_node_t *gn, **gnpp;
2207c1cb2cd8Shaad zio_gbh_phys_t *gbh;
2208c1cb2cd8Shaad uint64_t txg = pio->io_txg;
2209c1cb2cd8Shaad uint64_t resid = pio->io_size;
2210c1cb2cd8Shaad uint64_t lsize;
2211a252d550Shaad int copies = gio->io_prop.zp_copies;
2212a252d550Shaad int gbh_copies = MIN(copies + 1, spa_max_replication(spa));
2213c1cb2cd8Shaad zio_prop_t zp;
2214c1cb2cd8Shaad int error;
2215c1cb2cd8Shaad
2216eada09acSchs int flags = METASLAB_HINTBP_FAVOR | METASLAB_GANG_HEADER;
2217eada09acSchs if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
2218eada09acSchs ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
2219eada09acSchs ASSERT(!(pio->io_flags & ZIO_FLAG_NODATA));
2220eada09acSchs
2221eada09acSchs flags |= METASLAB_ASYNC_ALLOC;
2222eada09acSchs VERIFY(refcount_held(&mc->mc_alloc_slots, pio));
2223eada09acSchs
2224eada09acSchs /*
2225eada09acSchs * The logical zio has already placed a reservation for
2226eada09acSchs * 'copies' allocation slots but gang blocks may require
2227eada09acSchs * additional copies. These additional copies
2228eada09acSchs * (i.e. gbh_copies - copies) are guaranteed to succeed
2229eada09acSchs * since metaslab_class_throttle_reserve() always allows
2230eada09acSchs * additional reservations for gang blocks.
2231eada09acSchs */
2232eada09acSchs VERIFY(metaslab_class_throttle_reserve(mc, gbh_copies - copies,
2233eada09acSchs pio, flags));
2234eada09acSchs }
2235eada09acSchs
2236eada09acSchs error = metaslab_alloc(spa, mc, SPA_GANGBLOCKSIZE,
2237eada09acSchs bp, gbh_copies, txg, pio == gio ? NULL : gio->io_bp, flags,
2238eada09acSchs &pio->io_alloc_list, pio);
2239c1cb2cd8Shaad if (error) {
2240eada09acSchs if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
2241eada09acSchs ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
2242eada09acSchs ASSERT(!(pio->io_flags & ZIO_FLAG_NODATA));
2243eada09acSchs
2244eada09acSchs /*
2245eada09acSchs * If we failed to allocate the gang block header then
2246eada09acSchs * we remove any additional allocation reservations that
2247eada09acSchs * we placed here. The original reservation will
2248eada09acSchs * be removed when the logical I/O goes to the ready
2249eada09acSchs * stage.
2250eada09acSchs */
2251eada09acSchs metaslab_class_throttle_unreserve(mc,
2252eada09acSchs gbh_copies - copies, pio);
2253eada09acSchs }
2254c1cb2cd8Shaad pio->io_error = error;
2255c1cb2cd8Shaad return (ZIO_PIPELINE_CONTINUE);
2256c1cb2cd8Shaad }
2257c1cb2cd8Shaad
2258a252d550Shaad if (pio == gio) {
2259a252d550Shaad gnpp = &gio->io_gang_tree;
2260c1cb2cd8Shaad } else {
2261c1cb2cd8Shaad gnpp = pio->io_private;
2262c1cb2cd8Shaad ASSERT(pio->io_ready == zio_write_gang_member_ready);
2263c1cb2cd8Shaad }
2264c1cb2cd8Shaad
2265c1cb2cd8Shaad gn = zio_gang_node_alloc(gnpp);
2266c1cb2cd8Shaad gbh = gn->gn_gbh;
2267c1cb2cd8Shaad bzero(gbh, SPA_GANGBLOCKSIZE);
2268c1cb2cd8Shaad
2269c1cb2cd8Shaad /*
2270c1cb2cd8Shaad * Create the gang header.
2271c1cb2cd8Shaad */
2272c1cb2cd8Shaad zio = zio_rewrite(pio, spa, txg, bp, gbh, SPA_GANGBLOCKSIZE, NULL, NULL,
2273c1cb2cd8Shaad pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
2274c1cb2cd8Shaad
2275c1cb2cd8Shaad /*
2276c1cb2cd8Shaad * Create and nowait the gang children.
2277c1cb2cd8Shaad */
2278c1cb2cd8Shaad for (int g = 0; resid != 0; resid -= lsize, g++) {
2279c1cb2cd8Shaad lsize = P2ROUNDUP(resid / (SPA_GBH_NBLKPTRS - g),
2280c1cb2cd8Shaad SPA_MINBLOCKSIZE);
2281c1cb2cd8Shaad ASSERT(lsize >= SPA_MINBLOCKSIZE && lsize <= resid);
2282c1cb2cd8Shaad
2283a252d550Shaad zp.zp_checksum = gio->io_prop.zp_checksum;
2284c1cb2cd8Shaad zp.zp_compress = ZIO_COMPRESS_OFF;
2285c1cb2cd8Shaad zp.zp_type = DMU_OT_NONE;
2286c1cb2cd8Shaad zp.zp_level = 0;
2287a252d550Shaad zp.zp_copies = gio->io_prop.zp_copies;
2288eada09acSchs zp.zp_dedup = B_FALSE;
2289eada09acSchs zp.zp_dedup_verify = B_FALSE;
2290eada09acSchs zp.zp_nopwrite = B_FALSE;
2291c1cb2cd8Shaad
2292eada09acSchs zio_t *cio = zio_write(zio, spa, txg, &gbh->zg_blkptr[g],
2293c1cb2cd8Shaad (char *)pio->io_data + (pio->io_size - resid), lsize, &zp,
2294eada09acSchs zio_write_gang_member_ready, NULL, NULL, NULL,
2295eada09acSchs &gn->gn_child[g], pio->io_priority,
2296eada09acSchs ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
2297eada09acSchs
2298eada09acSchs if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
2299eada09acSchs ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
2300eada09acSchs ASSERT(!(pio->io_flags & ZIO_FLAG_NODATA));
2301eada09acSchs
2302eada09acSchs /*
2303eada09acSchs * Gang children won't throttle but we should
2304eada09acSchs * account for their work, so reserve an allocation
2305eada09acSchs * slot for them here.
2306eada09acSchs */
2307eada09acSchs VERIFY(metaslab_class_throttle_reserve(mc,
2308eada09acSchs zp.zp_copies, cio, flags));
2309eada09acSchs }
2310eada09acSchs zio_nowait(cio);
2311c1cb2cd8Shaad }
2312c1cb2cd8Shaad
2313c1cb2cd8Shaad /*
2314c1cb2cd8Shaad * Set pio's pipeline to just wait for zio to finish.
2315c1cb2cd8Shaad */
2316c1cb2cd8Shaad pio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2317c1cb2cd8Shaad
2318c1cb2cd8Shaad zio_nowait(zio);
2319c1cb2cd8Shaad
2320c1cb2cd8Shaad return (ZIO_PIPELINE_CONTINUE);
2321c1cb2cd8Shaad }
2322c1cb2cd8Shaad
2323c1cb2cd8Shaad /*
2324eada09acSchs * The zio_nop_write stage in the pipeline determines if allocating a
2325eada09acSchs * new bp is necessary. The nopwrite feature can handle writes in
2326eada09acSchs * either syncing or open context (i.e. zil writes) and as a result is
2327eada09acSchs * mutually exclusive with dedup.
2328eada09acSchs *
2329eada09acSchs * By leveraging a cryptographically secure checksum, such as SHA256, we
2330eada09acSchs * can compare the checksums of the new data and the old to determine if
2331eada09acSchs * allocating a new block is required. Note that our requirements for
2332eada09acSchs * cryptographic strength are fairly weak: there can't be any accidental
2333eada09acSchs * hash collisions, but we don't need to be secure against intentional
2334eada09acSchs * (malicious) collisions. To trigger a nopwrite, you have to be able
2335eada09acSchs * to write the file to begin with, and triggering an incorrect (hash
2336eada09acSchs * collision) nopwrite is no worse than simply writing to the file.
2337eada09acSchs * That said, there are no known attacks against the checksum algorithms
2338eada09acSchs * used for nopwrite, assuming that the salt and the checksums
2339eada09acSchs * themselves remain secret.
2340eada09acSchs */
2341eada09acSchs static int
zio_nop_write(zio_t * zio)2342eada09acSchs zio_nop_write(zio_t *zio)
2343eada09acSchs {
2344eada09acSchs blkptr_t *bp = zio->io_bp;
2345eada09acSchs blkptr_t *bp_orig = &zio->io_bp_orig;
2346eada09acSchs zio_prop_t *zp = &zio->io_prop;
2347eada09acSchs
2348eada09acSchs ASSERT(BP_GET_LEVEL(bp) == 0);
2349eada09acSchs ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
2350eada09acSchs ASSERT(zp->zp_nopwrite);
2351eada09acSchs ASSERT(!zp->zp_dedup);
2352eada09acSchs ASSERT(zio->io_bp_override == NULL);
2353eada09acSchs ASSERT(IO_IS_ALLOCATING(zio));
2354eada09acSchs
2355eada09acSchs /*
2356eada09acSchs * Check to see if the original bp and the new bp have matching
2357eada09acSchs * characteristics (i.e. same checksum, compression algorithms, etc).
2358eada09acSchs * If they don't then just continue with the pipeline which will
2359eada09acSchs * allocate a new bp.
2360eada09acSchs */
2361eada09acSchs if (BP_IS_HOLE(bp_orig) ||
2362eada09acSchs !(zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_flags &
2363eada09acSchs ZCHECKSUM_FLAG_NOPWRITE) ||
2364eada09acSchs BP_GET_CHECKSUM(bp) != BP_GET_CHECKSUM(bp_orig) ||
2365eada09acSchs BP_GET_COMPRESS(bp) != BP_GET_COMPRESS(bp_orig) ||
2366eada09acSchs BP_GET_DEDUP(bp) != BP_GET_DEDUP(bp_orig) ||
2367eada09acSchs zp->zp_copies != BP_GET_NDVAS(bp_orig))
2368eada09acSchs return (ZIO_PIPELINE_CONTINUE);
2369eada09acSchs
2370eada09acSchs /*
2371eada09acSchs * If the checksums match then reset the pipeline so that we
2372eada09acSchs * avoid allocating a new bp and issuing any I/O.
2373eada09acSchs */
2374eada09acSchs if (ZIO_CHECKSUM_EQUAL(bp->blk_cksum, bp_orig->blk_cksum)) {
2375eada09acSchs ASSERT(zio_checksum_table[zp->zp_checksum].ci_flags &
2376eada09acSchs ZCHECKSUM_FLAG_NOPWRITE);
2377eada09acSchs ASSERT3U(BP_GET_PSIZE(bp), ==, BP_GET_PSIZE(bp_orig));
2378eada09acSchs ASSERT3U(BP_GET_LSIZE(bp), ==, BP_GET_LSIZE(bp_orig));
2379eada09acSchs ASSERT(zp->zp_compress != ZIO_COMPRESS_OFF);
2380eada09acSchs ASSERT(bcmp(&bp->blk_prop, &bp_orig->blk_prop,
2381eada09acSchs sizeof (uint64_t)) == 0);
2382eada09acSchs
2383eada09acSchs *bp = *bp_orig;
2384eada09acSchs zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2385eada09acSchs zio->io_flags |= ZIO_FLAG_NOPWRITE;
2386eada09acSchs }
2387eada09acSchs
2388eada09acSchs return (ZIO_PIPELINE_CONTINUE);
2389eada09acSchs }
2390eada09acSchs
2391eada09acSchs /*
2392c1cb2cd8Shaad * ==========================================================================
2393a252d550Shaad * Dedup
2394a252d550Shaad * ==========================================================================
2395a252d550Shaad */
2396a252d550Shaad static void
zio_ddt_child_read_done(zio_t * zio)2397a252d550Shaad zio_ddt_child_read_done(zio_t *zio)
2398a252d550Shaad {
2399a252d550Shaad blkptr_t *bp = zio->io_bp;
2400a252d550Shaad ddt_entry_t *dde = zio->io_private;
2401a252d550Shaad ddt_phys_t *ddp;
2402a252d550Shaad zio_t *pio = zio_unique_parent(zio);
2403a252d550Shaad
2404a252d550Shaad mutex_enter(&pio->io_lock);
2405a252d550Shaad ddp = ddt_phys_select(dde, bp);
2406a252d550Shaad if (zio->io_error == 0)
2407a252d550Shaad ddt_phys_clear(ddp); /* this ddp doesn't need repair */
2408a252d550Shaad if (zio->io_error == 0 && dde->dde_repair_data == NULL)
2409a252d550Shaad dde->dde_repair_data = zio->io_data;
2410a252d550Shaad else
2411a252d550Shaad zio_buf_free(zio->io_data, zio->io_size);
2412a252d550Shaad mutex_exit(&pio->io_lock);
2413a252d550Shaad }
2414a252d550Shaad
2415a252d550Shaad static int
zio_ddt_read_start(zio_t * zio)2416a252d550Shaad zio_ddt_read_start(zio_t *zio)
2417a252d550Shaad {
2418a252d550Shaad blkptr_t *bp = zio->io_bp;
2419a252d550Shaad
2420a252d550Shaad ASSERT(BP_GET_DEDUP(bp));
2421a252d550Shaad ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
2422a252d550Shaad ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2423a252d550Shaad
2424a252d550Shaad if (zio->io_child_error[ZIO_CHILD_DDT]) {
2425a252d550Shaad ddt_t *ddt = ddt_select(zio->io_spa, bp);
2426a252d550Shaad ddt_entry_t *dde = ddt_repair_start(ddt, bp);
2427a252d550Shaad ddt_phys_t *ddp = dde->dde_phys;
2428a252d550Shaad ddt_phys_t *ddp_self = ddt_phys_select(dde, bp);
2429a252d550Shaad blkptr_t blk;
2430a252d550Shaad
2431a252d550Shaad ASSERT(zio->io_vsd == NULL);
2432a252d550Shaad zio->io_vsd = dde;
2433a252d550Shaad
2434a252d550Shaad if (ddp_self == NULL)
2435a252d550Shaad return (ZIO_PIPELINE_CONTINUE);
2436a252d550Shaad
2437a252d550Shaad for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
2438a252d550Shaad if (ddp->ddp_phys_birth == 0 || ddp == ddp_self)
2439a252d550Shaad continue;
2440a252d550Shaad ddt_bp_create(ddt->ddt_checksum, &dde->dde_key, ddp,
2441a252d550Shaad &blk);
2442a252d550Shaad zio_nowait(zio_read(zio, zio->io_spa, &blk,
2443a252d550Shaad zio_buf_alloc(zio->io_size), zio->io_size,
2444a252d550Shaad zio_ddt_child_read_done, dde, zio->io_priority,
2445a252d550Shaad ZIO_DDT_CHILD_FLAGS(zio) | ZIO_FLAG_DONT_PROPAGATE,
2446a252d550Shaad &zio->io_bookmark));
2447a252d550Shaad }
2448a252d550Shaad return (ZIO_PIPELINE_CONTINUE);
2449a252d550Shaad }
2450a252d550Shaad
2451a252d550Shaad zio_nowait(zio_read(zio, zio->io_spa, bp,
2452a252d550Shaad zio->io_data, zio->io_size, NULL, NULL, zio->io_priority,
2453a252d550Shaad ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark));
2454a252d550Shaad
2455a252d550Shaad return (ZIO_PIPELINE_CONTINUE);
2456a252d550Shaad }
2457a252d550Shaad
2458a252d550Shaad static int
zio_ddt_read_done(zio_t * zio)2459a252d550Shaad zio_ddt_read_done(zio_t *zio)
2460a252d550Shaad {
2461a252d550Shaad blkptr_t *bp = zio->io_bp;
2462a252d550Shaad
2463a252d550Shaad if (zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_DONE))
2464a252d550Shaad return (ZIO_PIPELINE_STOP);
2465a252d550Shaad
2466a252d550Shaad ASSERT(BP_GET_DEDUP(bp));
2467a252d550Shaad ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
2468a252d550Shaad ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2469a252d550Shaad
2470a252d550Shaad if (zio->io_child_error[ZIO_CHILD_DDT]) {
2471a252d550Shaad ddt_t *ddt = ddt_select(zio->io_spa, bp);
2472a252d550Shaad ddt_entry_t *dde = zio->io_vsd;
2473a252d550Shaad if (ddt == NULL) {
2474a252d550Shaad ASSERT(spa_load_state(zio->io_spa) != SPA_LOAD_NONE);
2475a252d550Shaad return (ZIO_PIPELINE_CONTINUE);
2476a252d550Shaad }
2477a252d550Shaad if (dde == NULL) {
2478a252d550Shaad zio->io_stage = ZIO_STAGE_DDT_READ_START >> 1;
2479a252d550Shaad zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
2480a252d550Shaad return (ZIO_PIPELINE_STOP);
2481a252d550Shaad }
2482a252d550Shaad if (dde->dde_repair_data != NULL) {
2483a252d550Shaad bcopy(dde->dde_repair_data, zio->io_data, zio->io_size);
2484a252d550Shaad zio->io_child_error[ZIO_CHILD_DDT] = 0;
2485a252d550Shaad }
2486a252d550Shaad ddt_repair_done(ddt, dde);
2487a252d550Shaad zio->io_vsd = NULL;
2488a252d550Shaad }
2489a252d550Shaad
2490a252d550Shaad ASSERT(zio->io_vsd == NULL);
2491a252d550Shaad
2492a252d550Shaad return (ZIO_PIPELINE_CONTINUE);
2493a252d550Shaad }
2494a252d550Shaad
2495a252d550Shaad static boolean_t
zio_ddt_collision(zio_t * zio,ddt_t * ddt,ddt_entry_t * dde)2496a252d550Shaad zio_ddt_collision(zio_t *zio, ddt_t *ddt, ddt_entry_t *dde)
2497a252d550Shaad {
2498a252d550Shaad spa_t *spa = zio->io_spa;
2499a252d550Shaad
2500a252d550Shaad /*
2501a252d550Shaad * Note: we compare the original data, not the transformed data,
2502a252d550Shaad * because when zio->io_bp is an override bp, we will not have
2503a252d550Shaad * pushed the I/O transforms. That's an important optimization
2504a252d550Shaad * because otherwise we'd compress/encrypt all dmu_sync() data twice.
2505a252d550Shaad */
2506a252d550Shaad for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
2507a252d550Shaad zio_t *lio = dde->dde_lead_zio[p];
2508a252d550Shaad
2509a252d550Shaad if (lio != NULL) {
2510a252d550Shaad return (lio->io_orig_size != zio->io_orig_size ||
2511a252d550Shaad bcmp(zio->io_orig_data, lio->io_orig_data,
2512a252d550Shaad zio->io_orig_size) != 0);
2513a252d550Shaad }
2514a252d550Shaad }
2515a252d550Shaad
2516a252d550Shaad for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
2517a252d550Shaad ddt_phys_t *ddp = &dde->dde_phys[p];
2518a252d550Shaad
2519a252d550Shaad if (ddp->ddp_phys_birth != 0) {
2520a252d550Shaad arc_buf_t *abuf = NULL;
2521eada09acSchs arc_flags_t aflags = ARC_FLAG_WAIT;
2522a252d550Shaad blkptr_t blk = *zio->io_bp;
2523a252d550Shaad int error;
2524a252d550Shaad
2525a252d550Shaad ddt_bp_fill(ddp, &blk, ddp->ddp_phys_birth);
2526a252d550Shaad
2527a252d550Shaad ddt_exit(ddt);
2528a252d550Shaad
2529eada09acSchs error = arc_read(NULL, spa, &blk,
2530a252d550Shaad arc_getbuf_func, &abuf, ZIO_PRIORITY_SYNC_READ,
2531a252d550Shaad ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2532a252d550Shaad &aflags, &zio->io_bookmark);
2533a252d550Shaad
2534a252d550Shaad if (error == 0) {
2535a252d550Shaad if (arc_buf_size(abuf) != zio->io_orig_size ||
2536a252d550Shaad bcmp(abuf->b_data, zio->io_orig_data,
2537a252d550Shaad zio->io_orig_size) != 0)
2538eada09acSchs error = SET_ERROR(EEXIST);
2539eada09acSchs arc_buf_destroy(abuf, &abuf);
2540a252d550Shaad }
2541a252d550Shaad
2542a252d550Shaad ddt_enter(ddt);
2543a252d550Shaad return (error != 0);
2544a252d550Shaad }
2545a252d550Shaad }
2546a252d550Shaad
2547a252d550Shaad return (B_FALSE);
2548a252d550Shaad }
2549a252d550Shaad
2550a252d550Shaad static void
zio_ddt_child_write_ready(zio_t * zio)2551a252d550Shaad zio_ddt_child_write_ready(zio_t *zio)
2552a252d550Shaad {
2553a252d550Shaad int p = zio->io_prop.zp_copies;
2554a252d550Shaad ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
2555a252d550Shaad ddt_entry_t *dde = zio->io_private;
2556a252d550Shaad ddt_phys_t *ddp = &dde->dde_phys[p];
2557a252d550Shaad zio_t *pio;
2558a252d550Shaad
2559a252d550Shaad if (zio->io_error)
2560a252d550Shaad return;
2561a252d550Shaad
2562a252d550Shaad ddt_enter(ddt);
2563a252d550Shaad
2564a252d550Shaad ASSERT(dde->dde_lead_zio[p] == zio);
2565a252d550Shaad
2566a252d550Shaad ddt_phys_fill(ddp, zio->io_bp);
2567a252d550Shaad
2568eada09acSchs zio_link_t *zl = NULL;
2569eada09acSchs while ((pio = zio_walk_parents(zio, &zl)) != NULL)
2570a252d550Shaad ddt_bp_fill(ddp, pio->io_bp, zio->io_txg);
2571a252d550Shaad
2572a252d550Shaad ddt_exit(ddt);
2573a252d550Shaad }
2574a252d550Shaad
2575a252d550Shaad static void
zio_ddt_child_write_done(zio_t * zio)2576a252d550Shaad zio_ddt_child_write_done(zio_t *zio)
2577a252d550Shaad {
2578a252d550Shaad int p = zio->io_prop.zp_copies;
2579a252d550Shaad ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
2580a252d550Shaad ddt_entry_t *dde = zio->io_private;
2581a252d550Shaad ddt_phys_t *ddp = &dde->dde_phys[p];
2582a252d550Shaad
2583a252d550Shaad ddt_enter(ddt);
2584a252d550Shaad
2585a252d550Shaad ASSERT(ddp->ddp_refcnt == 0);
2586a252d550Shaad ASSERT(dde->dde_lead_zio[p] == zio);
2587a252d550Shaad dde->dde_lead_zio[p] = NULL;
2588a252d550Shaad
2589a252d550Shaad if (zio->io_error == 0) {
2590eada09acSchs zio_link_t *zl = NULL;
2591eada09acSchs while (zio_walk_parents(zio, &zl) != NULL)
2592a252d550Shaad ddt_phys_addref(ddp);
2593a252d550Shaad } else {
2594a252d550Shaad ddt_phys_clear(ddp);
2595a252d550Shaad }
2596a252d550Shaad
2597a252d550Shaad ddt_exit(ddt);
2598a252d550Shaad }
2599a252d550Shaad
2600a252d550Shaad static void
zio_ddt_ditto_write_done(zio_t * zio)2601a252d550Shaad zio_ddt_ditto_write_done(zio_t *zio)
2602a252d550Shaad {
2603a252d550Shaad int p = DDT_PHYS_DITTO;
2604a252d550Shaad zio_prop_t *zp = &zio->io_prop;
2605a252d550Shaad blkptr_t *bp = zio->io_bp;
2606a252d550Shaad ddt_t *ddt = ddt_select(zio->io_spa, bp);
2607a252d550Shaad ddt_entry_t *dde = zio->io_private;
2608a252d550Shaad ddt_phys_t *ddp = &dde->dde_phys[p];
2609a252d550Shaad ddt_key_t *ddk = &dde->dde_key;
2610a252d550Shaad
2611a252d550Shaad ddt_enter(ddt);
2612a252d550Shaad
2613a252d550Shaad ASSERT(ddp->ddp_refcnt == 0);
2614a252d550Shaad ASSERT(dde->dde_lead_zio[p] == zio);
2615a252d550Shaad dde->dde_lead_zio[p] = NULL;
2616a252d550Shaad
2617a252d550Shaad if (zio->io_error == 0) {
2618a252d550Shaad ASSERT(ZIO_CHECKSUM_EQUAL(bp->blk_cksum, ddk->ddk_cksum));
2619a252d550Shaad ASSERT(zp->zp_copies < SPA_DVAS_PER_BP);
2620a252d550Shaad ASSERT(zp->zp_copies == BP_GET_NDVAS(bp) - BP_IS_GANG(bp));
2621a252d550Shaad if (ddp->ddp_phys_birth != 0)
2622a252d550Shaad ddt_phys_free(ddt, ddk, ddp, zio->io_txg);
2623a252d550Shaad ddt_phys_fill(ddp, bp);
2624a252d550Shaad }
2625a252d550Shaad
2626a252d550Shaad ddt_exit(ddt);
2627a252d550Shaad }
2628a252d550Shaad
2629a252d550Shaad static int
zio_ddt_write(zio_t * zio)2630a252d550Shaad zio_ddt_write(zio_t *zio)
2631a252d550Shaad {
2632a252d550Shaad spa_t *spa = zio->io_spa;
2633a252d550Shaad blkptr_t *bp = zio->io_bp;
2634a252d550Shaad uint64_t txg = zio->io_txg;
2635a252d550Shaad zio_prop_t *zp = &zio->io_prop;
2636a252d550Shaad int p = zp->zp_copies;
2637a252d550Shaad int ditto_copies;
2638a252d550Shaad zio_t *cio = NULL;
2639a252d550Shaad zio_t *dio = NULL;
2640a252d550Shaad ddt_t *ddt = ddt_select(spa, bp);
2641a252d550Shaad ddt_entry_t *dde;
2642a252d550Shaad ddt_phys_t *ddp;
2643a252d550Shaad
2644a252d550Shaad ASSERT(BP_GET_DEDUP(bp));
2645a252d550Shaad ASSERT(BP_GET_CHECKSUM(bp) == zp->zp_checksum);
2646a252d550Shaad ASSERT(BP_IS_HOLE(bp) || zio->io_bp_override);
2647a252d550Shaad
2648a252d550Shaad ddt_enter(ddt);
2649a252d550Shaad dde = ddt_lookup(ddt, bp, B_TRUE);
2650a252d550Shaad ddp = &dde->dde_phys[p];
2651a252d550Shaad
2652a252d550Shaad if (zp->zp_dedup_verify && zio_ddt_collision(zio, ddt, dde)) {
2653a252d550Shaad /*
2654a252d550Shaad * If we're using a weak checksum, upgrade to a strong checksum
2655a252d550Shaad * and try again. If we're already using a strong checksum,
2656a252d550Shaad * we can't resolve it, so just convert to an ordinary write.
2657a252d550Shaad * (And automatically e-mail a paper to Nature?)
2658a252d550Shaad */
2659eada09acSchs if (!(zio_checksum_table[zp->zp_checksum].ci_flags &
2660eada09acSchs ZCHECKSUM_FLAG_DEDUP)) {
2661a252d550Shaad zp->zp_checksum = spa_dedup_checksum(spa);
2662a252d550Shaad zio_pop_transforms(zio);
2663a252d550Shaad zio->io_stage = ZIO_STAGE_OPEN;
2664a252d550Shaad BP_ZERO(bp);
2665a252d550Shaad } else {
2666eada09acSchs zp->zp_dedup = B_FALSE;
2667a252d550Shaad }
2668a252d550Shaad zio->io_pipeline = ZIO_WRITE_PIPELINE;
2669a252d550Shaad ddt_exit(ddt);
2670a252d550Shaad return (ZIO_PIPELINE_CONTINUE);
2671a252d550Shaad }
2672a252d550Shaad
2673a252d550Shaad ditto_copies = ddt_ditto_copies_needed(ddt, dde, ddp);
2674a252d550Shaad ASSERT(ditto_copies < SPA_DVAS_PER_BP);
2675a252d550Shaad
2676a252d550Shaad if (ditto_copies > ddt_ditto_copies_present(dde) &&
2677a252d550Shaad dde->dde_lead_zio[DDT_PHYS_DITTO] == NULL) {
2678a252d550Shaad zio_prop_t czp = *zp;
2679a252d550Shaad
2680a252d550Shaad czp.zp_copies = ditto_copies;
2681a252d550Shaad
2682a252d550Shaad /*
2683a252d550Shaad * If we arrived here with an override bp, we won't have run
2684a252d550Shaad * the transform stack, so we won't have the data we need to
2685a252d550Shaad * generate a child i/o. So, toss the override bp and restart.
2686a252d550Shaad * This is safe, because using the override bp is just an
2687a252d550Shaad * optimization; and it's rare, so the cost doesn't matter.
2688a252d550Shaad */
2689a252d550Shaad if (zio->io_bp_override) {
2690a252d550Shaad zio_pop_transforms(zio);
2691a252d550Shaad zio->io_stage = ZIO_STAGE_OPEN;
2692a252d550Shaad zio->io_pipeline = ZIO_WRITE_PIPELINE;
2693a252d550Shaad zio->io_bp_override = NULL;
2694a252d550Shaad BP_ZERO(bp);
2695a252d550Shaad ddt_exit(ddt);
2696a252d550Shaad return (ZIO_PIPELINE_CONTINUE);
2697a252d550Shaad }
2698a252d550Shaad
2699a252d550Shaad dio = zio_write(zio, spa, txg, bp, zio->io_orig_data,
2700eada09acSchs zio->io_orig_size, &czp, NULL, NULL,
2701eada09acSchs NULL, zio_ddt_ditto_write_done, dde, zio->io_priority,
2702a252d550Shaad ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
2703a252d550Shaad
2704a252d550Shaad zio_push_transform(dio, zio->io_data, zio->io_size, 0, NULL);
2705a252d550Shaad dde->dde_lead_zio[DDT_PHYS_DITTO] = dio;
2706a252d550Shaad }
2707a252d550Shaad
2708a252d550Shaad if (ddp->ddp_phys_birth != 0 || dde->dde_lead_zio[p] != NULL) {
2709a252d550Shaad if (ddp->ddp_phys_birth != 0)
2710a252d550Shaad ddt_bp_fill(ddp, bp, txg);
2711a252d550Shaad if (dde->dde_lead_zio[p] != NULL)
2712a252d550Shaad zio_add_child(zio, dde->dde_lead_zio[p]);
2713a252d550Shaad else
2714a252d550Shaad ddt_phys_addref(ddp);
2715a252d550Shaad } else if (zio->io_bp_override) {
2716a252d550Shaad ASSERT(bp->blk_birth == txg);
2717a252d550Shaad ASSERT(BP_EQUAL(bp, zio->io_bp_override));
2718a252d550Shaad ddt_phys_fill(ddp, bp);
2719a252d550Shaad ddt_phys_addref(ddp);
2720a252d550Shaad } else {
2721a252d550Shaad cio = zio_write(zio, spa, txg, bp, zio->io_orig_data,
2722eada09acSchs zio->io_orig_size, zp,
2723eada09acSchs zio_ddt_child_write_ready, NULL, NULL,
2724a252d550Shaad zio_ddt_child_write_done, dde, zio->io_priority,
2725a252d550Shaad ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
2726a252d550Shaad
2727a252d550Shaad zio_push_transform(cio, zio->io_data, zio->io_size, 0, NULL);
2728a252d550Shaad dde->dde_lead_zio[p] = cio;
2729a252d550Shaad }
2730a252d550Shaad
2731a252d550Shaad ddt_exit(ddt);
2732a252d550Shaad
2733a252d550Shaad if (cio)
2734a252d550Shaad zio_nowait(cio);
2735a252d550Shaad if (dio)
2736a252d550Shaad zio_nowait(dio);
2737a252d550Shaad
2738a252d550Shaad return (ZIO_PIPELINE_CONTINUE);
2739a252d550Shaad }
2740a252d550Shaad
2741eada09acSchs ddt_entry_t *freedde; /* for debugging */
2742eada09acSchs
2743a252d550Shaad static int
zio_ddt_free(zio_t * zio)2744a252d550Shaad zio_ddt_free(zio_t *zio)
2745a252d550Shaad {
2746a252d550Shaad spa_t *spa = zio->io_spa;
2747a252d550Shaad blkptr_t *bp = zio->io_bp;
2748a252d550Shaad ddt_t *ddt = ddt_select(spa, bp);
2749a252d550Shaad ddt_entry_t *dde;
2750a252d550Shaad ddt_phys_t *ddp;
2751a252d550Shaad
2752a252d550Shaad ASSERT(BP_GET_DEDUP(bp));
2753a252d550Shaad ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2754a252d550Shaad
2755a252d550Shaad ddt_enter(ddt);
2756eada09acSchs freedde = dde = ddt_lookup(ddt, bp, B_TRUE);
2757a252d550Shaad ddp = ddt_phys_select(dde, bp);
2758a252d550Shaad ddt_phys_decref(ddp);
2759a252d550Shaad ddt_exit(ddt);
2760a252d550Shaad
2761a252d550Shaad return (ZIO_PIPELINE_CONTINUE);
2762a252d550Shaad }
2763a252d550Shaad
2764a252d550Shaad /*
2765a252d550Shaad * ==========================================================================
2766c1cb2cd8Shaad * Allocate and free blocks
2767c1cb2cd8Shaad * ==========================================================================
2768c1cb2cd8Shaad */
2769eada09acSchs
2770eada09acSchs static zio_t *
zio_io_to_allocate(spa_t * spa)2771eada09acSchs zio_io_to_allocate(spa_t *spa)
2772eada09acSchs {
2773eada09acSchs zio_t *zio;
2774eada09acSchs
2775eada09acSchs ASSERT(MUTEX_HELD(&spa->spa_alloc_lock));
2776eada09acSchs
2777eada09acSchs zio = avl_first(&spa->spa_alloc_tree);
2778eada09acSchs if (zio == NULL)
2779eada09acSchs return (NULL);
2780eada09acSchs
2781eada09acSchs ASSERT(IO_IS_ALLOCATING(zio));
2782eada09acSchs
2783eada09acSchs /*
2784eada09acSchs * Try to place a reservation for this zio. If we're unable to
2785eada09acSchs * reserve then we throttle.
2786eada09acSchs */
2787eada09acSchs if (!metaslab_class_throttle_reserve(spa_normal_class(spa),
2788eada09acSchs zio->io_prop.zp_copies, zio, 0)) {
2789eada09acSchs return (NULL);
2790eada09acSchs }
2791eada09acSchs
2792eada09acSchs avl_remove(&spa->spa_alloc_tree, zio);
2793eada09acSchs ASSERT3U(zio->io_stage, <, ZIO_STAGE_DVA_ALLOCATE);
2794eada09acSchs
2795eada09acSchs return (zio);
2796eada09acSchs }
2797eada09acSchs
2798eada09acSchs static int
zio_dva_throttle(zio_t * zio)2799eada09acSchs zio_dva_throttle(zio_t *zio)
2800eada09acSchs {
2801eada09acSchs spa_t *spa = zio->io_spa;
2802eada09acSchs zio_t *nio;
2803eada09acSchs
2804eada09acSchs if (zio->io_priority == ZIO_PRIORITY_SYNC_WRITE ||
2805eada09acSchs !spa_normal_class(zio->io_spa)->mc_alloc_throttle_enabled ||
2806eada09acSchs zio->io_child_type == ZIO_CHILD_GANG ||
2807eada09acSchs zio->io_flags & ZIO_FLAG_NODATA) {
2808eada09acSchs return (ZIO_PIPELINE_CONTINUE);
2809eada09acSchs }
2810eada09acSchs
2811eada09acSchs ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2812eada09acSchs
2813eada09acSchs ASSERT3U(zio->io_queued_timestamp, >, 0);
2814eada09acSchs ASSERT(zio->io_stage == ZIO_STAGE_DVA_THROTTLE);
2815eada09acSchs
2816eada09acSchs mutex_enter(&spa->spa_alloc_lock);
2817eada09acSchs
2818eada09acSchs ASSERT(zio->io_type == ZIO_TYPE_WRITE);
2819eada09acSchs avl_add(&spa->spa_alloc_tree, zio);
2820eada09acSchs
2821eada09acSchs nio = zio_io_to_allocate(zio->io_spa);
2822eada09acSchs mutex_exit(&spa->spa_alloc_lock);
2823eada09acSchs
2824eada09acSchs if (nio == zio)
2825eada09acSchs return (ZIO_PIPELINE_CONTINUE);
2826eada09acSchs
2827eada09acSchs if (nio != NULL) {
2828eada09acSchs ASSERT3U(nio->io_queued_timestamp, <=,
2829eada09acSchs zio->io_queued_timestamp);
2830eada09acSchs ASSERT(nio->io_stage == ZIO_STAGE_DVA_THROTTLE);
2831eada09acSchs /*
2832eada09acSchs * We are passing control to a new zio so make sure that
2833eada09acSchs * it is processed by a different thread. We do this to
2834eada09acSchs * avoid stack overflows that can occur when parents are
2835eada09acSchs * throttled and children are making progress. We allow
2836eada09acSchs * it to go to the head of the taskq since it's already
2837eada09acSchs * been waiting.
2838eada09acSchs */
2839eada09acSchs zio_taskq_dispatch(nio, ZIO_TASKQ_ISSUE, B_TRUE);
2840eada09acSchs }
2841eada09acSchs return (ZIO_PIPELINE_STOP);
2842eada09acSchs }
2843eada09acSchs
2844eada09acSchs void
zio_allocate_dispatch(spa_t * spa)2845eada09acSchs zio_allocate_dispatch(spa_t *spa)
2846eada09acSchs {
2847eada09acSchs zio_t *zio;
2848eada09acSchs
2849eada09acSchs mutex_enter(&spa->spa_alloc_lock);
2850eada09acSchs zio = zio_io_to_allocate(spa);
2851eada09acSchs mutex_exit(&spa->spa_alloc_lock);
2852eada09acSchs if (zio == NULL)
2853eada09acSchs return;
2854eada09acSchs
2855eada09acSchs ASSERT3U(zio->io_stage, ==, ZIO_STAGE_DVA_THROTTLE);
2856eada09acSchs ASSERT0(zio->io_error);
2857eada09acSchs zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_TRUE);
2858eada09acSchs }
2859eada09acSchs
2860c1cb2cd8Shaad static int
zio_dva_allocate(zio_t * zio)2861c1cb2cd8Shaad zio_dva_allocate(zio_t *zio)
2862c1cb2cd8Shaad {
2863c1cb2cd8Shaad spa_t *spa = zio->io_spa;
2864a252d550Shaad metaslab_class_t *mc = spa_normal_class(spa);
2865c1cb2cd8Shaad blkptr_t *bp = zio->io_bp;
2866c1cb2cd8Shaad int error;
2867eada09acSchs int flags = 0;
2868c1cb2cd8Shaad
2869a252d550Shaad if (zio->io_gang_leader == NULL) {
2870a252d550Shaad ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2871a252d550Shaad zio->io_gang_leader = zio;
2872a252d550Shaad }
2873a252d550Shaad
2874c1cb2cd8Shaad ASSERT(BP_IS_HOLE(bp));
2875eada09acSchs ASSERT0(BP_GET_NDVAS(bp));
2876a252d550Shaad ASSERT3U(zio->io_prop.zp_copies, >, 0);
2877a252d550Shaad ASSERT3U(zio->io_prop.zp_copies, <=, spa_max_replication(spa));
2878c1cb2cd8Shaad ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
2879c1cb2cd8Shaad
2880eada09acSchs if (zio->io_flags & ZIO_FLAG_NODATA) {
2881eada09acSchs flags |= METASLAB_DONT_THROTTLE;
2882eada09acSchs }
2883eada09acSchs if (zio->io_flags & ZIO_FLAG_GANG_CHILD) {
2884eada09acSchs flags |= METASLAB_GANG_CHILD;
2885eada09acSchs }
2886eada09acSchs if (zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE) {
2887eada09acSchs flags |= METASLAB_ASYNC_ALLOC;
2888eada09acSchs }
2889c1cb2cd8Shaad
2890eada09acSchs error = metaslab_alloc(spa, mc, zio->io_size, bp,
2891eada09acSchs zio->io_prop.zp_copies, zio->io_txg, NULL, flags,
2892eada09acSchs &zio->io_alloc_list, zio);
2893eada09acSchs
2894eada09acSchs if (error != 0) {
2895eada09acSchs spa_dbgmsg(spa, "%s: metaslab allocation failure: zio %p, "
2896eada09acSchs "size %llu, error %d", spa_name(spa), zio, zio->io_size,
2897eada09acSchs error);
2898c1cb2cd8Shaad if (error == ENOSPC && zio->io_size > SPA_MINBLOCKSIZE)
2899c1cb2cd8Shaad return (zio_write_gang_block(zio));
2900c1cb2cd8Shaad zio->io_error = error;
2901c1cb2cd8Shaad }
2902c1cb2cd8Shaad
2903c1cb2cd8Shaad return (ZIO_PIPELINE_CONTINUE);
2904c1cb2cd8Shaad }
2905c1cb2cd8Shaad
2906c1cb2cd8Shaad static int
zio_dva_free(zio_t * zio)2907c1cb2cd8Shaad zio_dva_free(zio_t *zio)
2908c1cb2cd8Shaad {
2909c1cb2cd8Shaad metaslab_free(zio->io_spa, zio->io_bp, zio->io_txg, B_FALSE);
2910c1cb2cd8Shaad
2911c1cb2cd8Shaad return (ZIO_PIPELINE_CONTINUE);
2912c1cb2cd8Shaad }
2913c1cb2cd8Shaad
2914c1cb2cd8Shaad static int
zio_dva_claim(zio_t * zio)2915c1cb2cd8Shaad zio_dva_claim(zio_t *zio)
2916c1cb2cd8Shaad {
2917c1cb2cd8Shaad int error;
2918c1cb2cd8Shaad
2919c1cb2cd8Shaad error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg);
2920c1cb2cd8Shaad if (error)
2921c1cb2cd8Shaad zio->io_error = error;
2922c1cb2cd8Shaad
2923c1cb2cd8Shaad return (ZIO_PIPELINE_CONTINUE);
2924c1cb2cd8Shaad }
2925c1cb2cd8Shaad
2926c1cb2cd8Shaad /*
2927c1cb2cd8Shaad * Undo an allocation. This is used by zio_done() when an I/O fails
2928c1cb2cd8Shaad * and we want to give back the block we just allocated.
2929c1cb2cd8Shaad * This handles both normal blocks and gang blocks.
2930c1cb2cd8Shaad */
2931c1cb2cd8Shaad static void
zio_dva_unallocate(zio_t * zio,zio_gang_node_t * gn,blkptr_t * bp)2932c1cb2cd8Shaad zio_dva_unallocate(zio_t *zio, zio_gang_node_t *gn, blkptr_t *bp)
2933c1cb2cd8Shaad {
2934c1cb2cd8Shaad ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp));
2935a252d550Shaad ASSERT(zio->io_bp_override == NULL);
2936c1cb2cd8Shaad
2937c1cb2cd8Shaad if (!BP_IS_HOLE(bp))
2938a252d550Shaad metaslab_free(zio->io_spa, bp, bp->blk_birth, B_TRUE);
2939c1cb2cd8Shaad
2940c1cb2cd8Shaad if (gn != NULL) {
2941c1cb2cd8Shaad for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
2942c1cb2cd8Shaad zio_dva_unallocate(zio, gn->gn_child[g],
2943c1cb2cd8Shaad &gn->gn_gbh->zg_blkptr[g]);
2944c1cb2cd8Shaad }
2945c1cb2cd8Shaad }
2946c1cb2cd8Shaad }
2947c1cb2cd8Shaad
2948c1cb2cd8Shaad /*
2949c1cb2cd8Shaad * Try to allocate an intent log block. Return 0 on success, errno on failure.
2950c1cb2cd8Shaad */
2951c1cb2cd8Shaad int
zio_alloc_zil(spa_t * spa,uint64_t txg,blkptr_t * new_bp,blkptr_t * old_bp,uint64_t size,boolean_t * slog)2952a252d550Shaad zio_alloc_zil(spa_t *spa, uint64_t txg, blkptr_t *new_bp, blkptr_t *old_bp,
2953eada09acSchs uint64_t size, boolean_t *slog)
2954c1cb2cd8Shaad {
2955a252d550Shaad int error = 1;
2956eada09acSchs zio_alloc_list_t io_alloc_list;
2957c1cb2cd8Shaad
2958a252d550Shaad ASSERT(txg > spa_syncing_txg(spa));
2959a252d550Shaad
2960eada09acSchs metaslab_trace_init(&io_alloc_list);
2961eada09acSchs error = metaslab_alloc(spa, spa_log_class(spa), size, new_bp, 1,
2962eada09acSchs txg, old_bp, METASLAB_HINTBP_AVOID, &io_alloc_list, NULL);
2963eada09acSchs if (error == 0) {
2964eada09acSchs *slog = TRUE;
2965eada09acSchs } else {
2966a252d550Shaad error = metaslab_alloc(spa, spa_normal_class(spa), size,
2967eada09acSchs new_bp, 1, txg, old_bp, METASLAB_HINTBP_AVOID,
2968eada09acSchs &io_alloc_list, NULL);
2969eada09acSchs if (error == 0)
2970eada09acSchs *slog = FALSE;
2971eada09acSchs }
2972eada09acSchs metaslab_trace_fini(&io_alloc_list);
2973c1cb2cd8Shaad
2974c1cb2cd8Shaad if (error == 0) {
2975c1cb2cd8Shaad BP_SET_LSIZE(new_bp, size);
2976c1cb2cd8Shaad BP_SET_PSIZE(new_bp, size);
2977c1cb2cd8Shaad BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF);
2978a252d550Shaad BP_SET_CHECKSUM(new_bp,
2979a252d550Shaad spa_version(spa) >= SPA_VERSION_SLIM_ZIL
2980a252d550Shaad ? ZIO_CHECKSUM_ZILOG2 : ZIO_CHECKSUM_ZILOG);
2981c1cb2cd8Shaad BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
2982c1cb2cd8Shaad BP_SET_LEVEL(new_bp, 0);
2983a252d550Shaad BP_SET_DEDUP(new_bp, 0);
2984c1cb2cd8Shaad BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER);
2985c1cb2cd8Shaad }
2986c1cb2cd8Shaad
2987c1cb2cd8Shaad return (error);
2988c1cb2cd8Shaad }
2989c1cb2cd8Shaad
2990c1cb2cd8Shaad /*
2991a252d550Shaad * Free an intent log block.
2992c1cb2cd8Shaad */
2993c1cb2cd8Shaad void
zio_free_zil(spa_t * spa,uint64_t txg,blkptr_t * bp)2994a252d550Shaad zio_free_zil(spa_t *spa, uint64_t txg, blkptr_t *bp)
2995c1cb2cd8Shaad {
2996a252d550Shaad ASSERT(BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG);
2997c1cb2cd8Shaad ASSERT(!BP_IS_GANG(bp));
2998c1cb2cd8Shaad
2999a252d550Shaad zio_free(spa, txg, bp);
3000c1cb2cd8Shaad }
3001c1cb2cd8Shaad
3002c1cb2cd8Shaad /*
3003c1cb2cd8Shaad * ==========================================================================
3004eada09acSchs * Read, write and delete to physical devices
3005c1cb2cd8Shaad * ==========================================================================
3006c1cb2cd8Shaad */
3007eada09acSchs
3008eada09acSchs
3009eada09acSchs /*
3010eada09acSchs * Issue an I/O to the underlying vdev. Typically the issue pipeline
3011eada09acSchs * stops after this stage and will resume upon I/O completion.
3012eada09acSchs * However, there are instances where the vdev layer may need to
3013eada09acSchs * continue the pipeline when an I/O was not issued. Since the I/O
3014eada09acSchs * that was sent to the vdev layer might be different than the one
3015eada09acSchs * currently active in the pipeline (see vdev_queue_io()), we explicitly
3016eada09acSchs * force the underlying vdev layers to call either zio_execute() or
3017eada09acSchs * zio_interrupt() to ensure that the pipeline continues with the correct I/O.
3018eada09acSchs */
3019c1cb2cd8Shaad static int
zio_vdev_io_start(zio_t * zio)3020c1cb2cd8Shaad zio_vdev_io_start(zio_t *zio)
3021c1cb2cd8Shaad {
3022c1cb2cd8Shaad vdev_t *vd = zio->io_vd;
3023c1cb2cd8Shaad uint64_t align;
3024c1cb2cd8Shaad spa_t *spa = zio->io_spa;
3025eada09acSchs int ret;
3026c1cb2cd8Shaad
3027c1cb2cd8Shaad ASSERT(zio->io_error == 0);
3028c1cb2cd8Shaad ASSERT(zio->io_child_error[ZIO_CHILD_VDEV] == 0);
3029c1cb2cd8Shaad
3030c1cb2cd8Shaad if (vd == NULL) {
3031c1cb2cd8Shaad if (!(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
3032c1cb2cd8Shaad spa_config_enter(spa, SCL_ZIO, zio, RW_READER);
3033c1cb2cd8Shaad
3034c1cb2cd8Shaad /*
3035c1cb2cd8Shaad * The mirror_ops handle multiple DVAs in a single BP.
3036c1cb2cd8Shaad */
3037eada09acSchs vdev_mirror_ops.vdev_op_io_start(zio);
3038eada09acSchs return (ZIO_PIPELINE_STOP);
3039eada09acSchs }
3040eada09acSchs
3041eada09acSchs if (vd->vdev_ops->vdev_op_leaf && zio->io_type == ZIO_TYPE_FREE &&
3042eada09acSchs zio->io_priority == ZIO_PRIORITY_NOW) {
3043eada09acSchs trim_map_free(vd, zio->io_offset, zio->io_size, zio->io_txg);
3044eada09acSchs return (ZIO_PIPELINE_CONTINUE);
3045eada09acSchs }
3046eada09acSchs
3047eada09acSchs ASSERT3P(zio->io_logical, !=, zio);
3048eada09acSchs
3049eada09acSchs /*
3050eada09acSchs * We keep track of time-sensitive I/Os so that the scan thread
3051eada09acSchs * can quickly react to certain workloads. In particular, we care
3052eada09acSchs * about non-scrubbing, top-level reads and writes with the following
3053eada09acSchs * characteristics:
3054eada09acSchs * - synchronous writes of user data to non-slog devices
3055eada09acSchs * - any reads of user data
3056eada09acSchs * When these conditions are met, adjust the timestamp of spa_last_io
3057eada09acSchs * which allows the scan thread to adjust its workload accordingly.
3058eada09acSchs */
3059eada09acSchs if (!(zio->io_flags & ZIO_FLAG_SCAN_THREAD) && zio->io_bp != NULL &&
3060eada09acSchs vd == vd->vdev_top && !vd->vdev_islog &&
3061eada09acSchs zio->io_bookmark.zb_objset != DMU_META_OBJSET &&
3062eada09acSchs zio->io_txg != spa_syncing_txg(spa)) {
3063eada09acSchs uint64_t old = spa->spa_last_io;
3064eada09acSchs uint64_t new = ddi_get_lbolt64();
3065eada09acSchs if (old != new)
3066eada09acSchs (void) atomic_cas_64(&spa->spa_last_io, old, new);
3067c1cb2cd8Shaad }
3068c1cb2cd8Shaad
3069c1cb2cd8Shaad align = 1ULL << vd->vdev_top->vdev_ashift;
3070c1cb2cd8Shaad
3071eada09acSchs if (!(zio->io_flags & ZIO_FLAG_PHYSICAL) &&
3072eada09acSchs P2PHASE(zio->io_size, align) != 0) {
3073eada09acSchs /* Transform logical writes to be a full physical block size. */
3074c1cb2cd8Shaad uint64_t asize = P2ROUNDUP(zio->io_size, align);
3075eada09acSchs char *abuf = NULL;
3076eada09acSchs if (zio->io_type == ZIO_TYPE_READ ||
3077eada09acSchs zio->io_type == ZIO_TYPE_WRITE)
3078eada09acSchs abuf = zio_buf_alloc(asize);
3079c1cb2cd8Shaad ASSERT(vd == vd->vdev_top);
3080c1cb2cd8Shaad if (zio->io_type == ZIO_TYPE_WRITE) {
3081c1cb2cd8Shaad bcopy(zio->io_data, abuf, zio->io_size);
3082c1cb2cd8Shaad bzero(abuf + zio->io_size, asize - zio->io_size);
3083c1cb2cd8Shaad }
3084eada09acSchs zio_push_transform(zio, abuf, asize, abuf ? asize : 0,
3085eada09acSchs zio_subblock);
3086c1cb2cd8Shaad }
3087c1cb2cd8Shaad
3088eada09acSchs /*
3089eada09acSchs * If this is not a physical io, make sure that it is properly aligned
3090eada09acSchs * before proceeding.
3091eada09acSchs */
3092eada09acSchs if (!(zio->io_flags & ZIO_FLAG_PHYSICAL)) {
3093eada09acSchs ASSERT0(P2PHASE(zio->io_offset, align));
3094eada09acSchs ASSERT0(P2PHASE(zio->io_size, align));
3095eada09acSchs } else {
3096eada09acSchs /*
3097eada09acSchs * For the physical io we allow alignment
3098eada09acSchs * to a logical block size.
3099eada09acSchs */
3100eada09acSchs uint64_t log_align =
3101eada09acSchs 1ULL << vd->vdev_top->vdev_logical_ashift;
3102eada09acSchs ASSERT0(P2PHASE(zio->io_offset, log_align));
3103eada09acSchs ASSERT0(P2PHASE(zio->io_size, log_align));
3104eada09acSchs }
3105eada09acSchs
3106eada09acSchs VERIFY(zio->io_type == ZIO_TYPE_READ || spa_writeable(spa));
3107a252d550Shaad
3108a252d550Shaad /*
3109a252d550Shaad * If this is a repair I/O, and there's no self-healing involved --
3110a252d550Shaad * that is, we're just resilvering what we expect to resilver --
3111a252d550Shaad * then don't do the I/O unless zio's txg is actually in vd's DTL.
3112a252d550Shaad * This prevents spurious resilvering with nested replication.
3113a252d550Shaad * For example, given a mirror of mirrors, (A+B)+(C+D), if only
3114a252d550Shaad * A is out of date, we'll read from C+D, then use the data to
3115a252d550Shaad * resilver A+B -- but we don't actually want to resilver B, just A.
3116a252d550Shaad * The top-level mirror has no way to know this, so instead we just
3117a252d550Shaad * discard unnecessary repairs as we work our way down the vdev tree.
3118a252d550Shaad * The same logic applies to any form of nested replication:
3119a252d550Shaad * ditto + mirror, RAID-Z + replacing, etc. This covers them all.
3120a252d550Shaad */
3121a252d550Shaad if ((zio->io_flags & ZIO_FLAG_IO_REPAIR) &&
3122a252d550Shaad !(zio->io_flags & ZIO_FLAG_SELF_HEAL) &&
3123a252d550Shaad zio->io_txg != 0 && /* not a delegated i/o */
3124a252d550Shaad !vdev_dtl_contains(vd, DTL_PARTIAL, zio->io_txg, 1)) {
3125a252d550Shaad ASSERT(zio->io_type == ZIO_TYPE_WRITE);
3126a252d550Shaad zio_vdev_io_bypass(zio);
3127a252d550Shaad return (ZIO_PIPELINE_CONTINUE);
3128a252d550Shaad }
3129c1cb2cd8Shaad
3130eada09acSchs if (vd->vdev_ops->vdev_op_leaf) {
3131eada09acSchs switch (zio->io_type) {
3132eada09acSchs case ZIO_TYPE_READ:
3133eada09acSchs if (vdev_cache_read(zio))
3134a252d550Shaad return (ZIO_PIPELINE_CONTINUE);
3135eada09acSchs /* FALLTHROUGH */
3136eada09acSchs case ZIO_TYPE_WRITE:
3137eada09acSchs case ZIO_TYPE_FREE:
3138c1cb2cd8Shaad if ((zio = vdev_queue_io(zio)) == NULL)
3139c1cb2cd8Shaad return (ZIO_PIPELINE_STOP);
3140c1cb2cd8Shaad
3141c1cb2cd8Shaad if (!vdev_accessible(vd, zio)) {
3142eada09acSchs zio->io_error = SET_ERROR(ENXIO);
3143c1cb2cd8Shaad zio_interrupt(zio);
3144c1cb2cd8Shaad return (ZIO_PIPELINE_STOP);
3145c1cb2cd8Shaad }
3146eada09acSchs break;
3147eada09acSchs }
3148eada09acSchs /*
3149eada09acSchs * Note that we ignore repair writes for TRIM because they can
3150eada09acSchs * conflict with normal writes. This isn't an issue because, by
3151eada09acSchs * definition, we only repair blocks that aren't freed.
3152eada09acSchs */
3153eada09acSchs if (zio->io_type == ZIO_TYPE_WRITE &&
3154eada09acSchs !(zio->io_flags & ZIO_FLAG_IO_REPAIR) &&
3155eada09acSchs !trim_map_write_start(zio))
3156eada09acSchs return (ZIO_PIPELINE_STOP);
3157c1cb2cd8Shaad }
3158c1cb2cd8Shaad
3159eada09acSchs vd->vdev_ops->vdev_op_io_start(zio);
3160eada09acSchs return (ZIO_PIPELINE_STOP);
3161c1cb2cd8Shaad }
3162c1cb2cd8Shaad
3163c1cb2cd8Shaad static int
zio_vdev_io_done(zio_t * zio)3164c1cb2cd8Shaad zio_vdev_io_done(zio_t *zio)
3165c1cb2cd8Shaad {
3166c1cb2cd8Shaad vdev_t *vd = zio->io_vd;
3167c1cb2cd8Shaad vdev_ops_t *ops = vd ? vd->vdev_ops : &vdev_mirror_ops;
3168c1cb2cd8Shaad boolean_t unexpected_error = B_FALSE;
3169c1cb2cd8Shaad
3170c1cb2cd8Shaad if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE))
3171c1cb2cd8Shaad return (ZIO_PIPELINE_STOP);
3172c1cb2cd8Shaad
3173eada09acSchs ASSERT(zio->io_type == ZIO_TYPE_READ ||
3174eada09acSchs zio->io_type == ZIO_TYPE_WRITE || zio->io_type == ZIO_TYPE_FREE);
3175c1cb2cd8Shaad
3176eada09acSchs if (vd != NULL && vd->vdev_ops->vdev_op_leaf &&
3177eada09acSchs (zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE ||
3178eada09acSchs zio->io_type == ZIO_TYPE_FREE)) {
3179eada09acSchs
3180eada09acSchs if (zio->io_type == ZIO_TYPE_WRITE &&
3181eada09acSchs !(zio->io_flags & ZIO_FLAG_IO_REPAIR))
3182eada09acSchs trim_map_write_done(zio);
3183c1cb2cd8Shaad
3184c1cb2cd8Shaad vdev_queue_io_done(zio);
3185c1cb2cd8Shaad
3186c1cb2cd8Shaad if (zio->io_type == ZIO_TYPE_WRITE)
3187c1cb2cd8Shaad vdev_cache_write(zio);
3188c1cb2cd8Shaad
3189c1cb2cd8Shaad if (zio_injection_enabled && zio->io_error == 0)
3190a252d550Shaad zio->io_error = zio_handle_device_injection(vd,
3191a252d550Shaad zio, EIO);
3192c1cb2cd8Shaad
3193c1cb2cd8Shaad if (zio_injection_enabled && zio->io_error == 0)
3194c1cb2cd8Shaad zio->io_error = zio_handle_label_injection(zio, EIO);
3195c1cb2cd8Shaad
3196c1cb2cd8Shaad if (zio->io_error) {
3197eada09acSchs if (zio->io_error == ENOTSUP &&
3198eada09acSchs zio->io_type == ZIO_TYPE_FREE) {
3199eada09acSchs /* Not all devices support TRIM. */
3200eada09acSchs } else if (!vdev_accessible(vd, zio)) {
3201eada09acSchs zio->io_error = SET_ERROR(ENXIO);
3202c1cb2cd8Shaad } else {
3203c1cb2cd8Shaad unexpected_error = B_TRUE;
3204c1cb2cd8Shaad }
3205c1cb2cd8Shaad }
3206c1cb2cd8Shaad }
3207c1cb2cd8Shaad
3208c1cb2cd8Shaad ops->vdev_op_io_done(zio);
3209c1cb2cd8Shaad
3210c1cb2cd8Shaad if (unexpected_error)
3211a252d550Shaad VERIFY(vdev_probe(vd, zio) == NULL);
3212c1cb2cd8Shaad
3213c1cb2cd8Shaad return (ZIO_PIPELINE_CONTINUE);
3214c1cb2cd8Shaad }
3215c1cb2cd8Shaad
3216a252d550Shaad /*
3217a252d550Shaad * For non-raidz ZIOs, we can just copy aside the bad data read from the
3218a252d550Shaad * disk, and use that to finish the checksum ereport later.
3219a252d550Shaad */
3220a252d550Shaad static void
zio_vsd_default_cksum_finish(zio_cksum_report_t * zcr,const void * good_buf)3221a252d550Shaad zio_vsd_default_cksum_finish(zio_cksum_report_t *zcr,
3222a252d550Shaad const void *good_buf)
3223a252d550Shaad {
3224a252d550Shaad /* no processing needed */
3225a252d550Shaad zfs_ereport_finish_checksum(zcr, good_buf, zcr->zcr_cbdata, B_FALSE);
3226a252d550Shaad }
3227a252d550Shaad
3228a252d550Shaad /*ARGSUSED*/
3229a252d550Shaad void
zio_vsd_default_cksum_report(zio_t * zio,zio_cksum_report_t * zcr,void * ignored)3230a252d550Shaad zio_vsd_default_cksum_report(zio_t *zio, zio_cksum_report_t *zcr, void *ignored)
3231a252d550Shaad {
3232a252d550Shaad void *buf = zio_buf_alloc(zio->io_size);
3233a252d550Shaad
3234a252d550Shaad bcopy(zio->io_data, buf, zio->io_size);
3235a252d550Shaad
3236a252d550Shaad zcr->zcr_cbinfo = zio->io_size;
3237a252d550Shaad zcr->zcr_cbdata = buf;
3238a252d550Shaad zcr->zcr_finish = zio_vsd_default_cksum_finish;
3239a252d550Shaad zcr->zcr_free = zio_buf_free;
3240a252d550Shaad }
3241a252d550Shaad
3242c1cb2cd8Shaad static int
zio_vdev_io_assess(zio_t * zio)3243c1cb2cd8Shaad zio_vdev_io_assess(zio_t *zio)
3244c1cb2cd8Shaad {
3245c1cb2cd8Shaad vdev_t *vd = zio->io_vd;
3246c1cb2cd8Shaad
3247c1cb2cd8Shaad if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE))
3248c1cb2cd8Shaad return (ZIO_PIPELINE_STOP);
3249c1cb2cd8Shaad
3250c1cb2cd8Shaad if (vd == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
3251c1cb2cd8Shaad spa_config_exit(zio->io_spa, SCL_ZIO, zio);
3252c1cb2cd8Shaad
3253c1cb2cd8Shaad if (zio->io_vsd != NULL) {
3254a252d550Shaad zio->io_vsd_ops->vsd_free(zio);
3255c1cb2cd8Shaad zio->io_vsd = NULL;
3256c1cb2cd8Shaad }
3257c1cb2cd8Shaad
3258c1cb2cd8Shaad if (zio_injection_enabled && zio->io_error == 0)
3259c1cb2cd8Shaad zio->io_error = zio_handle_fault_injection(zio, EIO);
3260c1cb2cd8Shaad
3261eada09acSchs if (zio->io_type == ZIO_TYPE_FREE &&
3262eada09acSchs zio->io_priority != ZIO_PRIORITY_NOW) {
3263eada09acSchs switch (zio->io_error) {
3264eada09acSchs case 0:
3265eada09acSchs ZIO_TRIM_STAT_INCR(bytes, zio->io_size);
3266eada09acSchs ZIO_TRIM_STAT_BUMP(success);
3267eada09acSchs break;
3268eada09acSchs case EOPNOTSUPP:
3269eada09acSchs ZIO_TRIM_STAT_BUMP(unsupported);
3270eada09acSchs break;
3271eada09acSchs default:
3272eada09acSchs ZIO_TRIM_STAT_BUMP(failed);
3273eada09acSchs break;
3274eada09acSchs }
3275eada09acSchs }
3276eada09acSchs
3277c1cb2cd8Shaad /*
3278c1cb2cd8Shaad * If the I/O failed, determine whether we should attempt to retry it.
3279a252d550Shaad *
3280a252d550Shaad * On retry, we cut in line in the issue queue, since we don't want
3281a252d550Shaad * compression/checksumming/etc. work to prevent our (cheap) IO reissue.
3282c1cb2cd8Shaad */
3283c1cb2cd8Shaad if (zio->io_error && vd == NULL &&
3284c1cb2cd8Shaad !(zio->io_flags & (ZIO_FLAG_DONT_RETRY | ZIO_FLAG_IO_RETRY))) {
3285c1cb2cd8Shaad ASSERT(!(zio->io_flags & ZIO_FLAG_DONT_QUEUE)); /* not a leaf */
3286c1cb2cd8Shaad ASSERT(!(zio->io_flags & ZIO_FLAG_IO_BYPASS)); /* not a leaf */
3287c1cb2cd8Shaad zio->io_error = 0;
3288c1cb2cd8Shaad zio->io_flags |= ZIO_FLAG_IO_RETRY |
3289c1cb2cd8Shaad ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE;
3290a252d550Shaad zio->io_stage = ZIO_STAGE_VDEV_IO_START >> 1;
3291a252d550Shaad zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE,
3292a252d550Shaad zio_requeue_io_start_cut_in_line);
3293c1cb2cd8Shaad return (ZIO_PIPELINE_STOP);
3294c1cb2cd8Shaad }
3295c1cb2cd8Shaad
3296c1cb2cd8Shaad /*
3297c1cb2cd8Shaad * If we got an error on a leaf device, convert it to ENXIO
3298c1cb2cd8Shaad * if the device is not accessible at all.
3299c1cb2cd8Shaad */
3300c1cb2cd8Shaad if (zio->io_error && vd != NULL && vd->vdev_ops->vdev_op_leaf &&
3301c1cb2cd8Shaad !vdev_accessible(vd, zio))
3302eada09acSchs zio->io_error = SET_ERROR(ENXIO);
3303c1cb2cd8Shaad
3304c1cb2cd8Shaad /*
3305c1cb2cd8Shaad * If we can't write to an interior vdev (mirror or RAID-Z),
3306c1cb2cd8Shaad * set vdev_cant_write so that we stop trying to allocate from it.
3307c1cb2cd8Shaad */
3308c1cb2cd8Shaad if (zio->io_error == ENXIO && zio->io_type == ZIO_TYPE_WRITE &&
3309eada09acSchs vd != NULL && !vd->vdev_ops->vdev_op_leaf) {
3310c1cb2cd8Shaad vd->vdev_cant_write = B_TRUE;
3311eada09acSchs }
3312c1cb2cd8Shaad
3313c1cb2cd8Shaad if (zio->io_error)
3314c1cb2cd8Shaad zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
3315c1cb2cd8Shaad
3316eada09acSchs if (vd != NULL && vd->vdev_ops->vdev_op_leaf &&
3317eada09acSchs zio->io_physdone != NULL) {
3318eada09acSchs ASSERT(!(zio->io_flags & ZIO_FLAG_DELEGATED));
3319eada09acSchs ASSERT(zio->io_child_type == ZIO_CHILD_VDEV);
3320eada09acSchs zio->io_physdone(zio->io_logical);
3321eada09acSchs }
3322eada09acSchs
3323c1cb2cd8Shaad return (ZIO_PIPELINE_CONTINUE);
3324c1cb2cd8Shaad }
3325c1cb2cd8Shaad
3326c1cb2cd8Shaad void
zio_vdev_io_reissue(zio_t * zio)3327c1cb2cd8Shaad zio_vdev_io_reissue(zio_t *zio)
3328c1cb2cd8Shaad {
3329c1cb2cd8Shaad ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
3330c1cb2cd8Shaad ASSERT(zio->io_error == 0);
3331c1cb2cd8Shaad
3332a252d550Shaad zio->io_stage >>= 1;
3333c1cb2cd8Shaad }
3334c1cb2cd8Shaad
3335c1cb2cd8Shaad void
zio_vdev_io_redone(zio_t * zio)3336c1cb2cd8Shaad zio_vdev_io_redone(zio_t *zio)
3337c1cb2cd8Shaad {
3338c1cb2cd8Shaad ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE);
3339c1cb2cd8Shaad
3340a252d550Shaad zio->io_stage >>= 1;
3341c1cb2cd8Shaad }
3342c1cb2cd8Shaad
3343c1cb2cd8Shaad void
zio_vdev_io_bypass(zio_t * zio)3344c1cb2cd8Shaad zio_vdev_io_bypass(zio_t *zio)
3345c1cb2cd8Shaad {
3346c1cb2cd8Shaad ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
3347c1cb2cd8Shaad ASSERT(zio->io_error == 0);
3348c1cb2cd8Shaad
3349c1cb2cd8Shaad zio->io_flags |= ZIO_FLAG_IO_BYPASS;
3350a252d550Shaad zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS >> 1;
3351c1cb2cd8Shaad }
3352c1cb2cd8Shaad
3353c1cb2cd8Shaad /*
3354c1cb2cd8Shaad * ==========================================================================
3355c1cb2cd8Shaad * Generate and verify checksums
3356c1cb2cd8Shaad * ==========================================================================
3357c1cb2cd8Shaad */
3358c1cb2cd8Shaad static int
zio_checksum_generate(zio_t * zio)3359c1cb2cd8Shaad zio_checksum_generate(zio_t *zio)
3360c1cb2cd8Shaad {
3361c1cb2cd8Shaad blkptr_t *bp = zio->io_bp;
3362c1cb2cd8Shaad enum zio_checksum checksum;
3363c1cb2cd8Shaad
3364c1cb2cd8Shaad if (bp == NULL) {
3365c1cb2cd8Shaad /*
3366c1cb2cd8Shaad * This is zio_write_phys().
3367c1cb2cd8Shaad * We're either generating a label checksum, or none at all.
3368c1cb2cd8Shaad */
3369c1cb2cd8Shaad checksum = zio->io_prop.zp_checksum;
3370c1cb2cd8Shaad
3371c1cb2cd8Shaad if (checksum == ZIO_CHECKSUM_OFF)
3372c1cb2cd8Shaad return (ZIO_PIPELINE_CONTINUE);
3373c1cb2cd8Shaad
3374c1cb2cd8Shaad ASSERT(checksum == ZIO_CHECKSUM_LABEL);
3375c1cb2cd8Shaad } else {
3376c1cb2cd8Shaad if (BP_IS_GANG(bp) && zio->io_child_type == ZIO_CHILD_GANG) {
3377c1cb2cd8Shaad ASSERT(!IO_IS_ALLOCATING(zio));
3378c1cb2cd8Shaad checksum = ZIO_CHECKSUM_GANG_HEADER;
3379c1cb2cd8Shaad } else {
3380c1cb2cd8Shaad checksum = BP_GET_CHECKSUM(bp);
3381c1cb2cd8Shaad }
3382c1cb2cd8Shaad }
3383c1cb2cd8Shaad
3384c1cb2cd8Shaad zio_checksum_compute(zio, checksum, zio->io_data, zio->io_size);
3385c1cb2cd8Shaad
3386c1cb2cd8Shaad return (ZIO_PIPELINE_CONTINUE);
3387c1cb2cd8Shaad }
3388c1cb2cd8Shaad
3389c1cb2cd8Shaad static int
zio_checksum_verify(zio_t * zio)3390c1cb2cd8Shaad zio_checksum_verify(zio_t *zio)
3391c1cb2cd8Shaad {
3392a252d550Shaad zio_bad_cksum_t info;
3393c1cb2cd8Shaad blkptr_t *bp = zio->io_bp;
3394c1cb2cd8Shaad int error;
3395c1cb2cd8Shaad
3396a252d550Shaad ASSERT(zio->io_vd != NULL);
3397a252d550Shaad
3398c1cb2cd8Shaad if (bp == NULL) {
3399c1cb2cd8Shaad /*
3400c1cb2cd8Shaad * This is zio_read_phys().
3401c1cb2cd8Shaad * We're either verifying a label checksum, or nothing at all.
3402c1cb2cd8Shaad */
3403c1cb2cd8Shaad if (zio->io_prop.zp_checksum == ZIO_CHECKSUM_OFF)
3404c1cb2cd8Shaad return (ZIO_PIPELINE_CONTINUE);
3405c1cb2cd8Shaad
3406c1cb2cd8Shaad ASSERT(zio->io_prop.zp_checksum == ZIO_CHECKSUM_LABEL);
3407c1cb2cd8Shaad }
3408c1cb2cd8Shaad
3409a252d550Shaad if ((error = zio_checksum_error(zio, &info)) != 0) {
3410c1cb2cd8Shaad zio->io_error = error;
3411eada09acSchs if (error == ECKSUM &&
3412eada09acSchs !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
3413a252d550Shaad zfs_ereport_start_checksum(zio->io_spa,
3414a252d550Shaad zio->io_vd, zio, zio->io_offset,
3415a252d550Shaad zio->io_size, NULL, &info);
3416c1cb2cd8Shaad }
3417c1cb2cd8Shaad }
3418c1cb2cd8Shaad
3419c1cb2cd8Shaad return (ZIO_PIPELINE_CONTINUE);
3420c1cb2cd8Shaad }
3421c1cb2cd8Shaad
3422c1cb2cd8Shaad /*
3423c1cb2cd8Shaad * Called by RAID-Z to ensure we don't compute the checksum twice.
3424c1cb2cd8Shaad */
3425c1cb2cd8Shaad void
zio_checksum_verified(zio_t * zio)3426c1cb2cd8Shaad zio_checksum_verified(zio_t *zio)
3427c1cb2cd8Shaad {
3428a252d550Shaad zio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
3429c1cb2cd8Shaad }
3430c1cb2cd8Shaad
3431c1cb2cd8Shaad /*
3432c1cb2cd8Shaad * ==========================================================================
3433c1cb2cd8Shaad * Error rank. Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other.
3434eada09acSchs * An error of 0 indicates success. ENXIO indicates whole-device failure,
3435c1cb2cd8Shaad * which may be transient (e.g. unplugged) or permament. ECKSUM and EIO
3436c1cb2cd8Shaad * indicate errors that are specific to one I/O, and most likely permanent.
3437c1cb2cd8Shaad * Any other error is presumed to be worse because we weren't expecting it.
3438c1cb2cd8Shaad * ==========================================================================
3439c1cb2cd8Shaad */
3440c1cb2cd8Shaad int
zio_worst_error(int e1,int e2)3441c1cb2cd8Shaad zio_worst_error(int e1, int e2)
3442c1cb2cd8Shaad {
3443c1cb2cd8Shaad static int zio_error_rank[] = { 0, ENXIO, ECKSUM, EIO };
3444c1cb2cd8Shaad int r1, r2;
3445c1cb2cd8Shaad
3446c1cb2cd8Shaad for (r1 = 0; r1 < sizeof (zio_error_rank) / sizeof (int); r1++)
3447c1cb2cd8Shaad if (e1 == zio_error_rank[r1])
3448c1cb2cd8Shaad break;
3449c1cb2cd8Shaad
3450c1cb2cd8Shaad for (r2 = 0; r2 < sizeof (zio_error_rank) / sizeof (int); r2++)
3451c1cb2cd8Shaad if (e2 == zio_error_rank[r2])
3452c1cb2cd8Shaad break;
3453c1cb2cd8Shaad
3454c1cb2cd8Shaad return (r1 > r2 ? e1 : e2);
3455c1cb2cd8Shaad }
3456c1cb2cd8Shaad
3457c1cb2cd8Shaad /*
3458c1cb2cd8Shaad * ==========================================================================
3459c1cb2cd8Shaad * I/O completion
3460c1cb2cd8Shaad * ==========================================================================
3461c1cb2cd8Shaad */
3462c1cb2cd8Shaad static int
zio_ready(zio_t * zio)3463c1cb2cd8Shaad zio_ready(zio_t *zio)
3464c1cb2cd8Shaad {
3465c1cb2cd8Shaad blkptr_t *bp = zio->io_bp;
3466a252d550Shaad zio_t *pio, *pio_next;
3467eada09acSchs zio_link_t *zl = NULL;
3468c1cb2cd8Shaad
3469a252d550Shaad if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_READY) ||
3470a252d550Shaad zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_READY))
3471c1cb2cd8Shaad return (ZIO_PIPELINE_STOP);
3472c1cb2cd8Shaad
3473a252d550Shaad if (zio->io_ready) {
3474c1cb2cd8Shaad ASSERT(IO_IS_ALLOCATING(zio));
3475eada09acSchs ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp) ||
3476eada09acSchs (zio->io_flags & ZIO_FLAG_NOPWRITE));
3477c1cb2cd8Shaad ASSERT(zio->io_children[ZIO_CHILD_GANG][ZIO_WAIT_READY] == 0);
3478c1cb2cd8Shaad
3479c1cb2cd8Shaad zio->io_ready(zio);
3480c1cb2cd8Shaad }
3481c1cb2cd8Shaad
3482c1cb2cd8Shaad if (bp != NULL && bp != &zio->io_bp_copy)
3483c1cb2cd8Shaad zio->io_bp_copy = *bp;
3484c1cb2cd8Shaad
3485eada09acSchs if (zio->io_error != 0) {
3486c1cb2cd8Shaad zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
3487c1cb2cd8Shaad
3488eada09acSchs if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
3489eada09acSchs ASSERT(IO_IS_ALLOCATING(zio));
3490eada09acSchs ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
3491eada09acSchs /*
3492eada09acSchs * We were unable to allocate anything, unreserve and
3493eada09acSchs * issue the next I/O to allocate.
3494eada09acSchs */
3495eada09acSchs metaslab_class_throttle_unreserve(
3496eada09acSchs spa_normal_class(zio->io_spa),
3497eada09acSchs zio->io_prop.zp_copies, zio);
3498eada09acSchs zio_allocate_dispatch(zio->io_spa);
3499eada09acSchs }
3500eada09acSchs }
3501eada09acSchs
3502a252d550Shaad mutex_enter(&zio->io_lock);
3503a252d550Shaad zio->io_state[ZIO_WAIT_READY] = 1;
3504eada09acSchs pio = zio_walk_parents(zio, &zl);
3505a252d550Shaad mutex_exit(&zio->io_lock);
3506a252d550Shaad
3507a252d550Shaad /*
3508a252d550Shaad * As we notify zio's parents, new parents could be added.
3509a252d550Shaad * New parents go to the head of zio's io_parent_list, however,
3510a252d550Shaad * so we will (correctly) not notify them. The remainder of zio's
3511a252d550Shaad * io_parent_list, from 'pio_next' onward, cannot change because
3512a252d550Shaad * all parents must wait for us to be done before they can be done.
3513a252d550Shaad */
3514a252d550Shaad for (; pio != NULL; pio = pio_next) {
3515eada09acSchs pio_next = zio_walk_parents(zio, &zl);
3516c1cb2cd8Shaad zio_notify_parent(pio, zio, ZIO_WAIT_READY);
3517a252d550Shaad }
3518a252d550Shaad
3519a252d550Shaad if (zio->io_flags & ZIO_FLAG_NODATA) {
3520a252d550Shaad if (BP_IS_GANG(bp)) {
3521a252d550Shaad zio->io_flags &= ~ZIO_FLAG_NODATA;
3522a252d550Shaad } else {
3523a252d550Shaad ASSERT((uintptr_t)zio->io_data < SPA_MAXBLOCKSIZE);
3524a252d550Shaad zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
3525a252d550Shaad }
3526a252d550Shaad }
3527a252d550Shaad
3528a252d550Shaad if (zio_injection_enabled &&
3529a252d550Shaad zio->io_spa->spa_syncing_txg == zio->io_txg)
3530a252d550Shaad zio_handle_ignored_writes(zio);
3531c1cb2cd8Shaad
3532c1cb2cd8Shaad return (ZIO_PIPELINE_CONTINUE);
3533c1cb2cd8Shaad }
3534c1cb2cd8Shaad
3535eada09acSchs /*
3536eada09acSchs * Update the allocation throttle accounting.
3537eada09acSchs */
3538eada09acSchs static void
zio_dva_throttle_done(zio_t * zio)3539eada09acSchs zio_dva_throttle_done(zio_t *zio)
3540eada09acSchs {
3541eada09acSchs zio_t *lio = zio->io_logical;
3542eada09acSchs zio_t *pio = zio_unique_parent(zio);
3543eada09acSchs vdev_t *vd = zio->io_vd;
3544eada09acSchs int flags = METASLAB_ASYNC_ALLOC;
3545eada09acSchs
3546eada09acSchs ASSERT3P(zio->io_bp, !=, NULL);
3547eada09acSchs ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE);
3548eada09acSchs ASSERT3U(zio->io_priority, ==, ZIO_PRIORITY_ASYNC_WRITE);
3549eada09acSchs ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
3550eada09acSchs ASSERT(vd != NULL);
3551eada09acSchs ASSERT3P(vd, ==, vd->vdev_top);
3552eada09acSchs ASSERT(!(zio->io_flags & (ZIO_FLAG_IO_REPAIR | ZIO_FLAG_IO_RETRY)));
3553eada09acSchs ASSERT(zio->io_flags & ZIO_FLAG_IO_ALLOCATING);
3554eada09acSchs ASSERT(!(lio->io_flags & ZIO_FLAG_IO_REWRITE));
3555eada09acSchs ASSERT(!(lio->io_orig_flags & ZIO_FLAG_NODATA));
3556eada09acSchs
3557eada09acSchs /*
3558eada09acSchs * Parents of gang children can have two flavors -- ones that
3559eada09acSchs * allocated the gang header (will have ZIO_FLAG_IO_REWRITE set)
3560eada09acSchs * and ones that allocated the constituent blocks. The allocation
3561eada09acSchs * throttle needs to know the allocating parent zio so we must find
3562eada09acSchs * it here.
3563eada09acSchs */
3564eada09acSchs if (pio->io_child_type == ZIO_CHILD_GANG) {
3565eada09acSchs /*
3566eada09acSchs * If our parent is a rewrite gang child then our grandparent
3567eada09acSchs * would have been the one that performed the allocation.
3568eada09acSchs */
3569eada09acSchs if (pio->io_flags & ZIO_FLAG_IO_REWRITE)
3570eada09acSchs pio = zio_unique_parent(pio);
3571eada09acSchs flags |= METASLAB_GANG_CHILD;
3572eada09acSchs }
3573eada09acSchs
3574eada09acSchs ASSERT(IO_IS_ALLOCATING(pio));
3575eada09acSchs ASSERT3P(zio, !=, zio->io_logical);
3576eada09acSchs ASSERT(zio->io_logical != NULL);
3577eada09acSchs ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REPAIR));
3578eada09acSchs ASSERT0(zio->io_flags & ZIO_FLAG_NOPWRITE);
3579eada09acSchs
3580eada09acSchs mutex_enter(&pio->io_lock);
3581eada09acSchs metaslab_group_alloc_decrement(zio->io_spa, vd->vdev_id, pio, flags);
3582eada09acSchs mutex_exit(&pio->io_lock);
3583eada09acSchs
3584eada09acSchs metaslab_class_throttle_unreserve(spa_normal_class(zio->io_spa),
3585eada09acSchs 1, pio);
3586eada09acSchs
3587eada09acSchs /*
3588eada09acSchs * Call into the pipeline to see if there is more work that
3589eada09acSchs * needs to be done. If there is work to be done it will be
3590eada09acSchs * dispatched to another taskq thread.
3591eada09acSchs */
3592eada09acSchs zio_allocate_dispatch(zio->io_spa);
3593eada09acSchs }
3594eada09acSchs
3595c1cb2cd8Shaad static int
zio_done(zio_t * zio)3596c1cb2cd8Shaad zio_done(zio_t *zio)
3597c1cb2cd8Shaad {
3598c1cb2cd8Shaad spa_t *spa = zio->io_spa;
3599c1cb2cd8Shaad zio_t *lio = zio->io_logical;
3600c1cb2cd8Shaad blkptr_t *bp = zio->io_bp;
3601c1cb2cd8Shaad vdev_t *vd = zio->io_vd;
3602c1cb2cd8Shaad uint64_t psize = zio->io_size;
3603a252d550Shaad zio_t *pio, *pio_next;
3604eada09acSchs metaslab_class_t *mc = spa_normal_class(spa);
3605eada09acSchs zio_link_t *zl = NULL;
3606c1cb2cd8Shaad
3607c1cb2cd8Shaad /*
3608a252d550Shaad * If our children haven't all completed,
3609c1cb2cd8Shaad * wait for them and then repeat this pipeline stage.
3610c1cb2cd8Shaad */
3611c1cb2cd8Shaad if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE) ||
3612c1cb2cd8Shaad zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE) ||
3613a252d550Shaad zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_DONE) ||
3614c1cb2cd8Shaad zio_wait_for_children(zio, ZIO_CHILD_LOGICAL, ZIO_WAIT_DONE))
3615c1cb2cd8Shaad return (ZIO_PIPELINE_STOP);
3616c1cb2cd8Shaad
3617eada09acSchs /*
3618eada09acSchs * If the allocation throttle is enabled, then update the accounting.
3619eada09acSchs * We only track child I/Os that are part of an allocating async
3620eada09acSchs * write. We must do this since the allocation is performed
3621eada09acSchs * by the logical I/O but the actual write is done by child I/Os.
3622eada09acSchs */
3623eada09acSchs if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING &&
3624eada09acSchs zio->io_child_type == ZIO_CHILD_VDEV) {
3625eada09acSchs ASSERT(mc->mc_alloc_throttle_enabled);
3626eada09acSchs zio_dva_throttle_done(zio);
3627eada09acSchs }
3628eada09acSchs
3629eada09acSchs /*
3630eada09acSchs * If the allocation throttle is enabled, verify that
3631eada09acSchs * we have decremented the refcounts for every I/O that was throttled.
3632eada09acSchs */
3633eada09acSchs if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
3634eada09acSchs ASSERT(zio->io_type == ZIO_TYPE_WRITE);
3635eada09acSchs ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
3636eada09acSchs ASSERT(bp != NULL);
3637eada09acSchs metaslab_group_alloc_verify(spa, zio->io_bp, zio);
3638eada09acSchs VERIFY(refcount_not_held(&mc->mc_alloc_slots, zio));
3639eada09acSchs }
3640eada09acSchs
3641c1cb2cd8Shaad for (int c = 0; c < ZIO_CHILD_TYPES; c++)
3642c1cb2cd8Shaad for (int w = 0; w < ZIO_WAIT_TYPES; w++)
3643c1cb2cd8Shaad ASSERT(zio->io_children[c][w] == 0);
3644c1cb2cd8Shaad
3645eada09acSchs if (bp != NULL && !BP_IS_EMBEDDED(bp)) {
3646c1cb2cd8Shaad ASSERT(bp->blk_pad[0] == 0);
3647c1cb2cd8Shaad ASSERT(bp->blk_pad[1] == 0);
3648c1cb2cd8Shaad ASSERT(bcmp(bp, &zio->io_bp_copy, sizeof (blkptr_t)) == 0 ||
3649a252d550Shaad (bp == zio_unique_parent(zio)->io_bp));
3650c1cb2cd8Shaad if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(bp) &&
3651a252d550Shaad zio->io_bp_override == NULL &&
3652c1cb2cd8Shaad !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) {
3653c1cb2cd8Shaad ASSERT(!BP_SHOULD_BYTESWAP(bp));
3654a252d550Shaad ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(bp));
3655c1cb2cd8Shaad ASSERT(BP_COUNT_GANG(bp) == 0 ||
3656c1cb2cd8Shaad (BP_COUNT_GANG(bp) == BP_GET_NDVAS(bp)));
3657c1cb2cd8Shaad }
3658eada09acSchs if (zio->io_flags & ZIO_FLAG_NOPWRITE)
3659eada09acSchs VERIFY(BP_EQUAL(bp, &zio->io_bp_orig));
3660c1cb2cd8Shaad }
3661c1cb2cd8Shaad
3662c1cb2cd8Shaad /*
3663a252d550Shaad * If there were child vdev/gang/ddt errors, they apply to us now.
3664c1cb2cd8Shaad */
3665c1cb2cd8Shaad zio_inherit_child_errors(zio, ZIO_CHILD_VDEV);
3666c1cb2cd8Shaad zio_inherit_child_errors(zio, ZIO_CHILD_GANG);
3667a252d550Shaad zio_inherit_child_errors(zio, ZIO_CHILD_DDT);
3668a252d550Shaad
3669a252d550Shaad /*
3670a252d550Shaad * If the I/O on the transformed data was successful, generate any
3671a252d550Shaad * checksum reports now while we still have the transformed data.
3672a252d550Shaad */
3673a252d550Shaad if (zio->io_error == 0) {
3674a252d550Shaad while (zio->io_cksum_report != NULL) {
3675a252d550Shaad zio_cksum_report_t *zcr = zio->io_cksum_report;
3676a252d550Shaad uint64_t align = zcr->zcr_align;
3677a252d550Shaad uint64_t asize = P2ROUNDUP(psize, align);
3678a252d550Shaad char *abuf = zio->io_data;
3679a252d550Shaad
3680a252d550Shaad if (asize != psize) {
3681a252d550Shaad abuf = zio_buf_alloc(asize);
3682a252d550Shaad bcopy(zio->io_data, abuf, psize);
3683a252d550Shaad bzero(abuf + psize, asize - psize);
3684a252d550Shaad }
3685a252d550Shaad
3686a252d550Shaad zio->io_cksum_report = zcr->zcr_next;
3687a252d550Shaad zcr->zcr_next = NULL;
3688a252d550Shaad zcr->zcr_finish(zcr, abuf);
3689a252d550Shaad zfs_ereport_free_checksum(zcr);
3690a252d550Shaad
3691a252d550Shaad if (asize != psize)
3692a252d550Shaad zio_buf_free(abuf, asize);
3693a252d550Shaad }
3694a252d550Shaad }
3695c1cb2cd8Shaad
3696c1cb2cd8Shaad zio_pop_transforms(zio); /* note: may set zio->io_error */
3697c1cb2cd8Shaad
3698c1cb2cd8Shaad vdev_stat_update(zio, psize);
3699c1cb2cd8Shaad
3700c1cb2cd8Shaad if (zio->io_error) {
3701c1cb2cd8Shaad /*
3702c1cb2cd8Shaad * If this I/O is attached to a particular vdev,
3703c1cb2cd8Shaad * generate an error message describing the I/O failure
3704c1cb2cd8Shaad * at the block level. We ignore these errors if the
3705c1cb2cd8Shaad * device is currently unavailable.
3706c1cb2cd8Shaad */
3707c1cb2cd8Shaad if (zio->io_error != ECKSUM && vd != NULL && !vdev_is_dead(vd))
3708c1cb2cd8Shaad zfs_ereport_post(FM_EREPORT_ZFS_IO, spa, vd, zio, 0, 0);
3709c1cb2cd8Shaad
3710a252d550Shaad if ((zio->io_error == EIO || !(zio->io_flags &
3711a252d550Shaad (ZIO_FLAG_SPECULATIVE | ZIO_FLAG_DONT_PROPAGATE))) &&
3712a252d550Shaad zio == lio) {
3713c1cb2cd8Shaad /*
3714c1cb2cd8Shaad * For logical I/O requests, tell the SPA to log the
3715c1cb2cd8Shaad * error and generate a logical data ereport.
3716c1cb2cd8Shaad */
3717c1cb2cd8Shaad spa_log_error(spa, zio);
3718c1cb2cd8Shaad zfs_ereport_post(FM_EREPORT_ZFS_DATA, spa, NULL, zio,
3719c1cb2cd8Shaad 0, 0);
3720c1cb2cd8Shaad }
3721c1cb2cd8Shaad }
3722c1cb2cd8Shaad
3723c1cb2cd8Shaad if (zio->io_error && zio == lio) {
3724c1cb2cd8Shaad /*
3725c1cb2cd8Shaad * Determine whether zio should be reexecuted. This will
3726c1cb2cd8Shaad * propagate all the way to the root via zio_notify_parent().
3727c1cb2cd8Shaad */
3728c1cb2cd8Shaad ASSERT(vd == NULL && bp != NULL);
3729a252d550Shaad ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3730c1cb2cd8Shaad
3731a252d550Shaad if (IO_IS_ALLOCATING(zio) &&
3732a252d550Shaad !(zio->io_flags & ZIO_FLAG_CANFAIL)) {
3733c1cb2cd8Shaad if (zio->io_error != ENOSPC)
3734c1cb2cd8Shaad zio->io_reexecute |= ZIO_REEXECUTE_NOW;
3735c1cb2cd8Shaad else
3736c1cb2cd8Shaad zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
3737a252d550Shaad }
3738c1cb2cd8Shaad
3739c1cb2cd8Shaad if ((zio->io_type == ZIO_TYPE_READ ||
3740c1cb2cd8Shaad zio->io_type == ZIO_TYPE_FREE) &&
3741eada09acSchs !(zio->io_flags & ZIO_FLAG_SCAN_THREAD) &&
3742c1cb2cd8Shaad zio->io_error == ENXIO &&
3743a252d550Shaad spa_load_state(spa) == SPA_LOAD_NONE &&
3744c1cb2cd8Shaad spa_get_failmode(spa) != ZIO_FAILURE_MODE_CONTINUE)
3745c1cb2cd8Shaad zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
3746c1cb2cd8Shaad
3747c1cb2cd8Shaad if (!(zio->io_flags & ZIO_FLAG_CANFAIL) && !zio->io_reexecute)
3748c1cb2cd8Shaad zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
3749a252d550Shaad
3750a252d550Shaad /*
3751a252d550Shaad * Here is a possibly good place to attempt to do
3752a252d550Shaad * either combinatorial reconstruction or error correction
3753a252d550Shaad * based on checksums. It also might be a good place
3754a252d550Shaad * to send out preliminary ereports before we suspend
3755a252d550Shaad * processing.
3756a252d550Shaad */
3757c1cb2cd8Shaad }
3758c1cb2cd8Shaad
3759c1cb2cd8Shaad /*
3760c1cb2cd8Shaad * If there were logical child errors, they apply to us now.
3761c1cb2cd8Shaad * We defer this until now to avoid conflating logical child
3762c1cb2cd8Shaad * errors with errors that happened to the zio itself when
3763c1cb2cd8Shaad * updating vdev stats and reporting FMA events above.
3764c1cb2cd8Shaad */
3765c1cb2cd8Shaad zio_inherit_child_errors(zio, ZIO_CHILD_LOGICAL);
3766c1cb2cd8Shaad
3767a252d550Shaad if ((zio->io_error || zio->io_reexecute) &&
3768a252d550Shaad IO_IS_ALLOCATING(zio) && zio->io_gang_leader == zio &&
3769eada09acSchs !(zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)))
3770a252d550Shaad zio_dva_unallocate(zio, zio->io_gang_tree, bp);
3771a252d550Shaad
3772a252d550Shaad zio_gang_tree_free(&zio->io_gang_tree);
3773a252d550Shaad
3774a252d550Shaad /*
3775a252d550Shaad * Godfather I/Os should never suspend.
3776a252d550Shaad */
3777a252d550Shaad if ((zio->io_flags & ZIO_FLAG_GODFATHER) &&
3778a252d550Shaad (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND))
3779a252d550Shaad zio->io_reexecute = 0;
3780a252d550Shaad
3781c1cb2cd8Shaad if (zio->io_reexecute) {
3782c1cb2cd8Shaad /*
3783c1cb2cd8Shaad * This is a logical I/O that wants to reexecute.
3784c1cb2cd8Shaad *
3785c1cb2cd8Shaad * Reexecute is top-down. When an i/o fails, if it's not
3786c1cb2cd8Shaad * the root, it simply notifies its parent and sticks around.
3787c1cb2cd8Shaad * The parent, seeing that it still has children in zio_done(),
3788c1cb2cd8Shaad * does the same. This percolates all the way up to the root.
3789c1cb2cd8Shaad * The root i/o will reexecute or suspend the entire tree.
3790c1cb2cd8Shaad *
3791c1cb2cd8Shaad * This approach ensures that zio_reexecute() honors
3792c1cb2cd8Shaad * all the original i/o dependency relationships, e.g.
3793c1cb2cd8Shaad * parents not executing until children are ready.
3794c1cb2cd8Shaad */
3795c1cb2cd8Shaad ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3796c1cb2cd8Shaad
3797a252d550Shaad zio->io_gang_leader = NULL;
3798c1cb2cd8Shaad
3799a252d550Shaad mutex_enter(&zio->io_lock);
3800a252d550Shaad zio->io_state[ZIO_WAIT_DONE] = 1;
3801a252d550Shaad mutex_exit(&zio->io_lock);
3802c1cb2cd8Shaad
3803a252d550Shaad /*
3804a252d550Shaad * "The Godfather" I/O monitors its children but is
3805a252d550Shaad * not a true parent to them. It will track them through
3806a252d550Shaad * the pipeline but severs its ties whenever they get into
3807a252d550Shaad * trouble (e.g. suspended). This allows "The Godfather"
3808a252d550Shaad * I/O to return status without blocking.
3809a252d550Shaad */
3810eada09acSchs zl = NULL;
3811eada09acSchs for (pio = zio_walk_parents(zio, &zl); pio != NULL;
3812eada09acSchs pio = pio_next) {
3813eada09acSchs zio_link_t *remove_zl = zl;
3814eada09acSchs pio_next = zio_walk_parents(zio, &zl);
3815a252d550Shaad
3816a252d550Shaad if ((pio->io_flags & ZIO_FLAG_GODFATHER) &&
3817a252d550Shaad (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND)) {
3818eada09acSchs zio_remove_child(pio, zio, remove_zl);
3819a252d550Shaad zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
3820a252d550Shaad }
3821a252d550Shaad }
3822a252d550Shaad
3823a252d550Shaad if ((pio = zio_unique_parent(zio)) != NULL) {
3824c1cb2cd8Shaad /*
3825c1cb2cd8Shaad * We're not a root i/o, so there's nothing to do
3826c1cb2cd8Shaad * but notify our parent. Don't propagate errors
3827c1cb2cd8Shaad * upward since we haven't permanently failed yet.
3828c1cb2cd8Shaad */
3829a252d550Shaad ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
3830c1cb2cd8Shaad zio->io_flags |= ZIO_FLAG_DONT_PROPAGATE;
3831c1cb2cd8Shaad zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
3832c1cb2cd8Shaad } else if (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND) {
3833c1cb2cd8Shaad /*
3834c1cb2cd8Shaad * We'd fail again if we reexecuted now, so suspend
3835c1cb2cd8Shaad * until conditions improve (e.g. device comes online).
3836c1cb2cd8Shaad */
3837c1cb2cd8Shaad zio_suspend(spa, zio);
3838c1cb2cd8Shaad } else {
3839c1cb2cd8Shaad /*
3840c1cb2cd8Shaad * Reexecution is potentially a huge amount of work.
3841c1cb2cd8Shaad * Hand it off to the otherwise-unused claim taskq.
3842c1cb2cd8Shaad */
3843a96fd3ecShannken #if defined(illumos) || !defined(_KERNEL)
3844eada09acSchs ASSERT(zio->io_tqent.tqent_next == NULL);
3845a96fd3ecShannken #elif defined(__NetBSD__)
3846a96fd3ecShannken ASSERT(zio->io_tqent.tqent_queued == 0);
3847eada09acSchs #else
3848eada09acSchs ASSERT(zio->io_tqent.tqent_task.ta_pending == 0);
3849eada09acSchs #endif
3850eada09acSchs spa_taskq_dispatch_ent(spa, ZIO_TYPE_CLAIM,
3851eada09acSchs ZIO_TASKQ_ISSUE, (task_func_t *)zio_reexecute, zio,
3852eada09acSchs 0, &zio->io_tqent);
3853c1cb2cd8Shaad }
3854c1cb2cd8Shaad return (ZIO_PIPELINE_STOP);
3855c1cb2cd8Shaad }
3856c1cb2cd8Shaad
3857a252d550Shaad ASSERT(zio->io_child_count == 0);
3858c1cb2cd8Shaad ASSERT(zio->io_reexecute == 0);
3859c1cb2cd8Shaad ASSERT(zio->io_error == 0 || (zio->io_flags & ZIO_FLAG_CANFAIL));
3860c1cb2cd8Shaad
3861a252d550Shaad /*
3862a252d550Shaad * Report any checksum errors, since the I/O is complete.
3863a252d550Shaad */
3864a252d550Shaad while (zio->io_cksum_report != NULL) {
3865a252d550Shaad zio_cksum_report_t *zcr = zio->io_cksum_report;
3866a252d550Shaad zio->io_cksum_report = zcr->zcr_next;
3867a252d550Shaad zcr->zcr_next = NULL;
3868a252d550Shaad zcr->zcr_finish(zcr, NULL);
3869a252d550Shaad zfs_ereport_free_checksum(zcr);
3870a252d550Shaad }
3871a252d550Shaad
3872a252d550Shaad /*
3873a252d550Shaad * It is the responsibility of the done callback to ensure that this
3874a252d550Shaad * particular zio is no longer discoverable for adoption, and as
3875a252d550Shaad * such, cannot acquire any new parents.
3876a252d550Shaad */
3877c1cb2cd8Shaad if (zio->io_done)
3878c1cb2cd8Shaad zio->io_done(zio);
3879c1cb2cd8Shaad
3880a252d550Shaad mutex_enter(&zio->io_lock);
3881a252d550Shaad zio->io_state[ZIO_WAIT_DONE] = 1;
3882a252d550Shaad mutex_exit(&zio->io_lock);
3883c1cb2cd8Shaad
3884eada09acSchs zl = NULL;
3885eada09acSchs for (pio = zio_walk_parents(zio, &zl); pio != NULL; pio = pio_next) {
3886eada09acSchs zio_link_t *remove_zl = zl;
3887eada09acSchs pio_next = zio_walk_parents(zio, &zl);
3888eada09acSchs zio_remove_child(pio, zio, remove_zl);
3889c1cb2cd8Shaad zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
3890c1cb2cd8Shaad }
3891c1cb2cd8Shaad
3892c1cb2cd8Shaad if (zio->io_waiter != NULL) {
3893c1cb2cd8Shaad mutex_enter(&zio->io_lock);
3894c1cb2cd8Shaad zio->io_executor = NULL;
3895c1cb2cd8Shaad cv_broadcast(&zio->io_cv);
3896c1cb2cd8Shaad mutex_exit(&zio->io_lock);
3897c1cb2cd8Shaad } else {
3898c1cb2cd8Shaad zio_destroy(zio);
3899c1cb2cd8Shaad }
3900c1cb2cd8Shaad
3901c1cb2cd8Shaad return (ZIO_PIPELINE_STOP);
3902c1cb2cd8Shaad }
3903c1cb2cd8Shaad
3904c1cb2cd8Shaad /*
3905c1cb2cd8Shaad * ==========================================================================
3906c1cb2cd8Shaad * I/O pipeline definition
3907c1cb2cd8Shaad * ==========================================================================
3908c1cb2cd8Shaad */
3909a252d550Shaad static zio_pipe_stage_t *zio_pipeline[] = {
3910c1cb2cd8Shaad NULL,
3911c1cb2cd8Shaad zio_read_bp_init,
3912eada09acSchs zio_write_bp_init,
3913a252d550Shaad zio_free_bp_init,
3914a252d550Shaad zio_issue_async,
3915eada09acSchs zio_write_compress,
3916c1cb2cd8Shaad zio_checksum_generate,
3917eada09acSchs zio_nop_write,
3918a252d550Shaad zio_ddt_read_start,
3919a252d550Shaad zio_ddt_read_done,
3920a252d550Shaad zio_ddt_write,
3921a252d550Shaad zio_ddt_free,
3922c1cb2cd8Shaad zio_gang_assemble,
3923c1cb2cd8Shaad zio_gang_issue,
3924eada09acSchs zio_dva_throttle,
3925c1cb2cd8Shaad zio_dva_allocate,
3926c1cb2cd8Shaad zio_dva_free,
3927c1cb2cd8Shaad zio_dva_claim,
3928c1cb2cd8Shaad zio_ready,
3929c1cb2cd8Shaad zio_vdev_io_start,
3930c1cb2cd8Shaad zio_vdev_io_done,
3931c1cb2cd8Shaad zio_vdev_io_assess,
3932c1cb2cd8Shaad zio_checksum_verify,
3933c1cb2cd8Shaad zio_done
3934c1cb2cd8Shaad };
3935eada09acSchs
3936eada09acSchs
3937eada09acSchs
3938eada09acSchs
3939eada09acSchs /*
3940eada09acSchs * Compare two zbookmark_phys_t's to see which we would reach first in a
3941eada09acSchs * pre-order traversal of the object tree.
3942eada09acSchs *
3943eada09acSchs * This is simple in every case aside from the meta-dnode object. For all other
3944eada09acSchs * objects, we traverse them in order (object 1 before object 2, and so on).
3945eada09acSchs * However, all of these objects are traversed while traversing object 0, since
3946eada09acSchs * the data it points to is the list of objects. Thus, we need to convert to a
3947eada09acSchs * canonical representation so we can compare meta-dnode bookmarks to
3948eada09acSchs * non-meta-dnode bookmarks.
3949eada09acSchs *
3950eada09acSchs * We do this by calculating "equivalents" for each field of the zbookmark.
3951eada09acSchs * zbookmarks outside of the meta-dnode use their own object and level, and
3952eada09acSchs * calculate the level 0 equivalent (the first L0 blkid that is contained in the
3953eada09acSchs * blocks this bookmark refers to) by multiplying their blkid by their span
3954eada09acSchs * (the number of L0 blocks contained within one block at their level).
3955eada09acSchs * zbookmarks inside the meta-dnode calculate their object equivalent
3956eada09acSchs * (which is L0equiv * dnodes per data block), use 0 for their L0equiv, and use
3957eada09acSchs * level + 1<<31 (any value larger than a level could ever be) for their level.
3958eada09acSchs * This causes them to always compare before a bookmark in their object
3959eada09acSchs * equivalent, compare appropriately to bookmarks in other objects, and to
3960eada09acSchs * compare appropriately to other bookmarks in the meta-dnode.
3961eada09acSchs */
3962eada09acSchs int
zbookmark_compare(uint16_t dbss1,uint8_t ibs1,uint16_t dbss2,uint8_t ibs2,const zbookmark_phys_t * zb1,const zbookmark_phys_t * zb2)3963eada09acSchs zbookmark_compare(uint16_t dbss1, uint8_t ibs1, uint16_t dbss2, uint8_t ibs2,
3964eada09acSchs const zbookmark_phys_t *zb1, const zbookmark_phys_t *zb2)
3965eada09acSchs {
3966eada09acSchs /*
3967eada09acSchs * These variables represent the "equivalent" values for the zbookmark,
3968eada09acSchs * after converting zbookmarks inside the meta dnode to their
3969eada09acSchs * normal-object equivalents.
3970eada09acSchs */
3971eada09acSchs uint64_t zb1obj, zb2obj;
3972eada09acSchs uint64_t zb1L0, zb2L0;
3973eada09acSchs uint64_t zb1level, zb2level;
3974eada09acSchs
3975eada09acSchs if (zb1->zb_object == zb2->zb_object &&
3976eada09acSchs zb1->zb_level == zb2->zb_level &&
3977eada09acSchs zb1->zb_blkid == zb2->zb_blkid)
3978eada09acSchs return (0);
3979eada09acSchs
3980eada09acSchs /*
3981eada09acSchs * BP_SPANB calculates the span in blocks.
3982eada09acSchs */
3983eada09acSchs zb1L0 = (zb1->zb_blkid) * BP_SPANB(ibs1, zb1->zb_level);
3984eada09acSchs zb2L0 = (zb2->zb_blkid) * BP_SPANB(ibs2, zb2->zb_level);
3985eada09acSchs
3986eada09acSchs if (zb1->zb_object == DMU_META_DNODE_OBJECT) {
3987eada09acSchs zb1obj = zb1L0 * (dbss1 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT));
3988eada09acSchs zb1L0 = 0;
3989eada09acSchs zb1level = zb1->zb_level + COMPARE_META_LEVEL;
3990eada09acSchs } else {
3991eada09acSchs zb1obj = zb1->zb_object;
3992eada09acSchs zb1level = zb1->zb_level;
3993eada09acSchs }
3994eada09acSchs
3995eada09acSchs if (zb2->zb_object == DMU_META_DNODE_OBJECT) {
3996eada09acSchs zb2obj = zb2L0 * (dbss2 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT));
3997eada09acSchs zb2L0 = 0;
3998eada09acSchs zb2level = zb2->zb_level + COMPARE_META_LEVEL;
3999eada09acSchs } else {
4000eada09acSchs zb2obj = zb2->zb_object;
4001eada09acSchs zb2level = zb2->zb_level;
4002eada09acSchs }
4003eada09acSchs
4004eada09acSchs /* Now that we have a canonical representation, do the comparison. */
4005eada09acSchs if (zb1obj != zb2obj)
4006eada09acSchs return (zb1obj < zb2obj ? -1 : 1);
4007eada09acSchs else if (zb1L0 != zb2L0)
4008eada09acSchs return (zb1L0 < zb2L0 ? -1 : 1);
4009eada09acSchs else if (zb1level != zb2level)
4010eada09acSchs return (zb1level > zb2level ? -1 : 1);
4011eada09acSchs /*
4012eada09acSchs * This can (theoretically) happen if the bookmarks have the same object
4013eada09acSchs * and level, but different blkids, if the block sizes are not the same.
4014eada09acSchs * There is presently no way to change the indirect block sizes
4015eada09acSchs */
4016eada09acSchs return (0);
4017eada09acSchs }
4018eada09acSchs
4019eada09acSchs /*
4020eada09acSchs * This function checks the following: given that last_block is the place that
4021eada09acSchs * our traversal stopped last time, does that guarantee that we've visited
4022eada09acSchs * every node under subtree_root? Therefore, we can't just use the raw output
4023eada09acSchs * of zbookmark_compare. We have to pass in a modified version of
4024eada09acSchs * subtree_root; by incrementing the block id, and then checking whether
4025eada09acSchs * last_block is before or equal to that, we can tell whether or not having
4026eada09acSchs * visited last_block implies that all of subtree_root's children have been
4027eada09acSchs * visited.
4028eada09acSchs */
4029eada09acSchs boolean_t
zbookmark_subtree_completed(const dnode_phys_t * dnp,const zbookmark_phys_t * subtree_root,const zbookmark_phys_t * last_block)4030eada09acSchs zbookmark_subtree_completed(const dnode_phys_t *dnp,
4031eada09acSchs const zbookmark_phys_t *subtree_root, const zbookmark_phys_t *last_block)
4032eada09acSchs {
4033eada09acSchs zbookmark_phys_t mod_zb = *subtree_root;
4034eada09acSchs mod_zb.zb_blkid++;
4035eada09acSchs ASSERT(last_block->zb_level == 0);
4036eada09acSchs
4037eada09acSchs /* The objset_phys_t isn't before anything. */
4038eada09acSchs if (dnp == NULL)
4039eada09acSchs return (B_FALSE);
4040eada09acSchs
4041eada09acSchs /*
4042eada09acSchs * We pass in 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT) for the
4043eada09acSchs * data block size in sectors, because that variable is only used if
4044eada09acSchs * the bookmark refers to a block in the meta-dnode. Since we don't
4045eada09acSchs * know without examining it what object it refers to, and there's no
4046eada09acSchs * harm in passing in this value in other cases, we always pass it in.
4047eada09acSchs *
4048eada09acSchs * We pass in 0 for the indirect block size shift because zb2 must be
4049eada09acSchs * level 0. The indirect block size is only used to calculate the span
4050eada09acSchs * of the bookmark, but since the bookmark must be level 0, the span is
4051eada09acSchs * always 1, so the math works out.
4052eada09acSchs *
4053eada09acSchs * If you make changes to how the zbookmark_compare code works, be sure
4054eada09acSchs * to make sure that this code still works afterwards.
4055eada09acSchs */
4056eada09acSchs return (zbookmark_compare(dnp->dn_datablkszsec, dnp->dn_indblkshift,
4057eada09acSchs 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT), 0, &mod_zb,
4058eada09acSchs last_block) <= 0);
4059eada09acSchs }
4060