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