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