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