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