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