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