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