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