1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * This file and its contents are supplied under the terms of the
4 * Common Development and Distribution License ("CDDL"), version 1.0.
5 * You may only use this file in accordance with the terms of version
6 * 1.0 of the CDDL.
7 *
8 * A full copy of the text of the CDDL should have accompanied this
9 * source. A copy of the CDDL is also available via the Internet at
10 * https://opensource.org/license/CDDL-1.0.
11 */
12 /*
13 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
14 * Copyright (c) 2011, 2022 by Delphix. All rights reserved.
15 * Copyright (c) 2011 Nexenta Systems, Inc. All rights reserved.
16 * Copyright (c) 2017, Intel Corporation.
17 * Copyright (c) 2019, 2023, 2024, 2025, Klara, Inc.
18 * Copyright (c) 2019, Allan Jude
19 * Copyright (c) 2021, Datto, Inc.
20 * Copyright (c) 2021, 2024 by George Melikov. All rights reserved.
21 */
22
23 #include <sys/sysmacros.h>
24 #include <sys/zfs_context.h>
25 #include <sys/fm/fs/zfs.h>
26 #include <sys/spa.h>
27 #include <sys/txg.h>
28 #include <sys/spa_impl.h>
29 #include <sys/vdev_impl.h>
30 #include <sys/vdev_trim.h>
31 #include <sys/zio_impl.h>
32 #include <sys/zio_compress.h>
33 #include <sys/zio_checksum.h>
34 #include <sys/dmu_objset.h>
35 #include <sys/arc.h>
36 #include <sys/brt.h>
37 #include <sys/ddt.h>
38 #include <sys/blkptr.h>
39 #include <sys/zfeature.h>
40 #include <sys/dsl_scan.h>
41 #include <sys/metaslab_impl.h>
42 #include <sys/time.h>
43 #include <sys/trace_zfs.h>
44 #include <sys/abd.h>
45 #include <sys/dsl_crypt.h>
46 #include <cityhash.h>
47
48 /*
49 * ==========================================================================
50 * I/O type descriptions
51 * ==========================================================================
52 */
53 const char *const zio_type_name[ZIO_TYPES] = {
54 /*
55 * Note: Linux kernel thread name length is limited
56 * so these names will differ from upstream open zfs.
57 */
58 "z_null", "z_rd", "z_wr", "z_fr", "z_cl", "z_flush", "z_trim"
59 };
60
61 int zio_dva_throttle_enabled = B_TRUE;
62 static int zio_deadman_log_all = B_FALSE;
63
64 /*
65 * ==========================================================================
66 * I/O kmem caches
67 * ==========================================================================
68 */
69 static kmem_cache_t *zio_cache;
70 static kmem_cache_t *zio_link_cache;
71 kmem_cache_t *zio_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
72 kmem_cache_t *zio_data_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
73 #if defined(ZFS_DEBUG) && !defined(_KERNEL)
74 static uint64_t zio_buf_cache_allocs[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
75 static uint64_t zio_buf_cache_frees[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
76 #endif
77
78 /* Mark IOs as "slow" if they take longer than 30 seconds */
79 static uint_t zio_slow_io_ms = (30 * MILLISEC);
80
81 #define BP_SPANB(indblkshift, level) \
82 (((uint64_t)1) << ((level) * ((indblkshift) - SPA_BLKPTRSHIFT)))
83 #define COMPARE_META_LEVEL 0x80000000ul
84 /*
85 * The following actions directly effect the spa's sync-to-convergence logic.
86 * The values below define the sync pass when we start performing the action.
87 * Care should be taken when changing these values as they directly impact
88 * spa_sync() performance. Tuning these values may introduce subtle performance
89 * pathologies and should only be done in the context of performance analysis.
90 * These tunables will eventually be removed and replaced with #defines once
91 * enough analysis has been done to determine optimal values.
92 *
93 * The 'zfs_sync_pass_deferred_free' pass must be greater than 1 to ensure that
94 * regular blocks are not deferred.
95 *
96 * Starting in sync pass 8 (zfs_sync_pass_dont_compress), we disable
97 * compression (including of metadata). In practice, we don't have this
98 * many sync passes, so this has no effect.
99 *
100 * The original intent was that disabling compression would help the sync
101 * passes to converge. However, in practice disabling compression increases
102 * the average number of sync passes, because when we turn compression off, a
103 * lot of block's size will change and thus we have to re-allocate (not
104 * overwrite) them. It also increases the number of 128KB allocations (e.g.
105 * for indirect blocks and spacemaps) because these will not be compressed.
106 * The 128K allocations are especially detrimental to performance on highly
107 * fragmented systems, which may have very few free segments of this size,
108 * and may need to load new metaslabs to satisfy 128K allocations.
109 */
110
111 /* defer frees starting in this pass */
112 uint_t zfs_sync_pass_deferred_free = 2;
113
114 /* don't compress starting in this pass */
115 static uint_t zfs_sync_pass_dont_compress = 8;
116
117 /* rewrite new bps starting in this pass */
118 static uint_t zfs_sync_pass_rewrite = 2;
119
120 /*
121 * An allocating zio is one that either currently has the DVA allocate
122 * stage set or will have it later in its lifetime.
123 */
124 #define IO_IS_ALLOCATING(zio) ((zio)->io_orig_pipeline & ZIO_STAGE_DVA_ALLOCATE)
125
126 /*
127 * Enable smaller cores by excluding metadata
128 * allocations as well.
129 */
130 int zio_exclude_metadata = 0;
131 static int zio_requeue_io_start_cut_in_line = 1;
132
133 #ifdef ZFS_DEBUG
134 static const int zio_buf_debug_limit = 16384;
135 #else
136 static const int zio_buf_debug_limit = 0;
137 #endif
138
139 typedef struct zio_stats {
140 kstat_named_t ziostat_total_allocations;
141 kstat_named_t ziostat_alloc_class_fallbacks;
142 kstat_named_t ziostat_gang_writes;
143 kstat_named_t ziostat_gang_multilevel;
144 } zio_stats_t;
145
146 static zio_stats_t zio_stats = {
147 { "total_allocations", KSTAT_DATA_UINT64 },
148 { "alloc_class_fallbacks", KSTAT_DATA_UINT64 },
149 { "gang_writes", KSTAT_DATA_UINT64 },
150 { "gang_multilevel", KSTAT_DATA_UINT64 },
151 };
152
153 struct {
154 wmsum_t ziostat_total_allocations;
155 wmsum_t ziostat_alloc_class_fallbacks;
156 wmsum_t ziostat_gang_writes;
157 wmsum_t ziostat_gang_multilevel;
158 } ziostat_sums;
159
160 #define ZIOSTAT_BUMP(stat) wmsum_add(&ziostat_sums.stat, 1);
161
162 static kstat_t *zio_ksp;
163
164 static inline void __zio_execute(zio_t *zio);
165
166 static void zio_taskq_dispatch(zio_t *, zio_taskq_type_t, boolean_t);
167 static void zio_batch_join(zio_batch_t *, zio_t *);
168
169 static int
zio_kstats_update(kstat_t * ksp,int rw)170 zio_kstats_update(kstat_t *ksp, int rw)
171 {
172 zio_stats_t *zs = ksp->ks_data;
173 if (rw == KSTAT_WRITE)
174 return (EACCES);
175
176 zs->ziostat_total_allocations.value.ui64 =
177 wmsum_value(&ziostat_sums.ziostat_total_allocations);
178 zs->ziostat_alloc_class_fallbacks.value.ui64 =
179 wmsum_value(&ziostat_sums.ziostat_alloc_class_fallbacks);
180 zs->ziostat_gang_writes.value.ui64 =
181 wmsum_value(&ziostat_sums.ziostat_gang_writes);
182 zs->ziostat_gang_multilevel.value.ui64 =
183 wmsum_value(&ziostat_sums.ziostat_gang_multilevel);
184 return (0);
185 }
186
187 void
zio_init(void)188 zio_init(void)
189 {
190 size_t c;
191
192 zio_cache = kmem_cache_create("zio_cache",
193 sizeof (zio_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
194 zio_link_cache = kmem_cache_create("zio_link_cache",
195 sizeof (zio_link_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
196
197 wmsum_init(&ziostat_sums.ziostat_total_allocations, 0);
198 wmsum_init(&ziostat_sums.ziostat_alloc_class_fallbacks, 0);
199 wmsum_init(&ziostat_sums.ziostat_gang_writes, 0);
200 wmsum_init(&ziostat_sums.ziostat_gang_multilevel, 0);
201 zio_ksp = kstat_create("zfs", 0, "zio_stats",
202 "misc", KSTAT_TYPE_NAMED, sizeof (zio_stats) /
203 sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
204 if (zio_ksp != NULL) {
205 zio_ksp->ks_data = &zio_stats;
206 zio_ksp->ks_update = zio_kstats_update;
207 kstat_install(zio_ksp);
208 }
209
210 for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
211 size_t size = (c + 1) << SPA_MINBLOCKSHIFT;
212 size_t align, cflags, data_cflags;
213 char name[32];
214
215 /*
216 * Create cache for each half-power of 2 size, starting from
217 * SPA_MINBLOCKSIZE. It should give us memory space efficiency
218 * of ~7/8, sufficient for transient allocations mostly using
219 * these caches.
220 */
221 size_t p2 = size;
222 while (!ISP2(p2))
223 p2 &= p2 - 1;
224 if (!IS_P2ALIGNED(size, p2 / 2))
225 continue;
226
227 #ifndef _KERNEL
228 /*
229 * If we are using watchpoints, put each buffer on its own page,
230 * to eliminate the performance overhead of trapping to the
231 * kernel when modifying a non-watched buffer that shares the
232 * page with a watched buffer.
233 */
234 if (arc_watch && !IS_P2ALIGNED(size, PAGESIZE))
235 continue;
236 #endif
237
238 if (IS_P2ALIGNED(size, PAGESIZE))
239 align = PAGESIZE;
240 else
241 align = 1 << (highbit64(size ^ (size - 1)) - 1);
242
243 cflags = (zio_exclude_metadata || size > zio_buf_debug_limit) ?
244 KMC_NODEBUG : 0;
245 data_cflags = KMC_NODEBUG;
246 if (abd_size_alloc_linear(size)) {
247 cflags |= KMC_RECLAIMABLE;
248 data_cflags |= KMC_RECLAIMABLE;
249 }
250 if (cflags == data_cflags) {
251 /*
252 * Resulting kmem caches would be identical.
253 * Save memory by creating only one.
254 */
255 (void) snprintf(name, sizeof (name),
256 "zio_buf_comb_%lu", (ulong_t)size);
257 zio_buf_cache[c] = kmem_cache_create(name, size, align,
258 NULL, NULL, NULL, NULL, NULL, cflags);
259 zio_data_buf_cache[c] = zio_buf_cache[c];
260 continue;
261 }
262 (void) snprintf(name, sizeof (name), "zio_buf_%lu",
263 (ulong_t)size);
264 zio_buf_cache[c] = kmem_cache_create(name, size, align,
265 NULL, NULL, NULL, NULL, NULL, cflags);
266
267 (void) snprintf(name, sizeof (name), "zio_data_buf_%lu",
268 (ulong_t)size);
269 zio_data_buf_cache[c] = kmem_cache_create(name, size, align,
270 NULL, NULL, NULL, NULL, NULL, data_cflags);
271 }
272
273 while (--c != 0) {
274 ASSERT(zio_buf_cache[c] != NULL);
275 if (zio_buf_cache[c - 1] == NULL)
276 zio_buf_cache[c - 1] = zio_buf_cache[c];
277
278 ASSERT(zio_data_buf_cache[c] != NULL);
279 if (zio_data_buf_cache[c - 1] == NULL)
280 zio_data_buf_cache[c - 1] = zio_data_buf_cache[c];
281 }
282
283 zio_inject_init();
284
285 lz4_init();
286 }
287
288 void
zio_fini(void)289 zio_fini(void)
290 {
291 size_t n = SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT;
292
293 #if defined(ZFS_DEBUG) && !defined(_KERNEL)
294 for (size_t i = 0; i < n; i++) {
295 if (zio_buf_cache_allocs[i] != zio_buf_cache_frees[i])
296 (void) printf("zio_fini: [%d] %llu != %llu\n",
297 (int)((i + 1) << SPA_MINBLOCKSHIFT),
298 (long long unsigned)zio_buf_cache_allocs[i],
299 (long long unsigned)zio_buf_cache_frees[i]);
300 }
301 #endif
302
303 /*
304 * The same kmem cache can show up multiple times in both zio_buf_cache
305 * and zio_data_buf_cache. Do a wasteful but trivially correct scan to
306 * sort it out.
307 */
308 for (size_t i = 0; i < n; i++) {
309 kmem_cache_t *cache = zio_buf_cache[i];
310 if (cache == NULL)
311 continue;
312 for (size_t j = i; j < n; j++) {
313 if (cache == zio_buf_cache[j])
314 zio_buf_cache[j] = NULL;
315 if (cache == zio_data_buf_cache[j])
316 zio_data_buf_cache[j] = NULL;
317 }
318 kmem_cache_destroy(cache);
319 }
320
321 for (size_t i = 0; i < n; i++) {
322 kmem_cache_t *cache = zio_data_buf_cache[i];
323 if (cache == NULL)
324 continue;
325 for (size_t j = i; j < n; j++) {
326 if (cache == zio_data_buf_cache[j])
327 zio_data_buf_cache[j] = NULL;
328 }
329 kmem_cache_destroy(cache);
330 }
331
332 for (size_t i = 0; i < n; i++) {
333 VERIFY0P(zio_buf_cache[i]);
334 VERIFY0P(zio_data_buf_cache[i]);
335 }
336
337 if (zio_ksp != NULL) {
338 kstat_delete(zio_ksp);
339 zio_ksp = NULL;
340 }
341
342 wmsum_fini(&ziostat_sums.ziostat_total_allocations);
343 wmsum_fini(&ziostat_sums.ziostat_alloc_class_fallbacks);
344 wmsum_fini(&ziostat_sums.ziostat_gang_writes);
345 wmsum_fini(&ziostat_sums.ziostat_gang_multilevel);
346
347 kmem_cache_destroy(zio_link_cache);
348 kmem_cache_destroy(zio_cache);
349
350 zio_inject_fini();
351
352 lz4_fini();
353 }
354
355 /*
356 * ==========================================================================
357 * Allocate and free I/O buffers
358 * ==========================================================================
359 */
360
361 #if defined(ZFS_DEBUG) && defined(_KERNEL)
362 #define ZFS_ZIO_BUF_CANARY 1
363 #endif
364
365 #ifdef ZFS_ZIO_BUF_CANARY
366 static const ulong_t zio_buf_canary = (ulong_t)0xdeadc0dedead210b;
367
368 /*
369 * Use empty space after the buffer to detect overflows.
370 *
371 * Since zio_init() creates kmem caches only for certain set of buffer sizes,
372 * allocations of different sizes may have some unused space after the data.
373 * Filling part of that space with a known pattern on allocation and checking
374 * it on free should allow us to detect some buffer overflows.
375 */
376 static void
zio_buf_put_canary(ulong_t * p,size_t size,kmem_cache_t ** cache,size_t c)377 zio_buf_put_canary(ulong_t *p, size_t size, kmem_cache_t **cache, size_t c)
378 {
379 size_t off = P2ROUNDUP(size, sizeof (ulong_t));
380 ulong_t *canary = p + off / sizeof (ulong_t);
381 size_t asize = (c + 1) << SPA_MINBLOCKSHIFT;
382 if (c + 1 < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT &&
383 cache[c] == cache[c + 1])
384 asize = (c + 2) << SPA_MINBLOCKSHIFT;
385 for (; off < asize; canary++, off += sizeof (ulong_t))
386 *canary = zio_buf_canary;
387 }
388
389 static void
zio_buf_check_canary(ulong_t * p,size_t size,kmem_cache_t ** cache,size_t c)390 zio_buf_check_canary(ulong_t *p, size_t size, kmem_cache_t **cache, size_t c)
391 {
392 size_t off = P2ROUNDUP(size, sizeof (ulong_t));
393 ulong_t *canary = p + off / sizeof (ulong_t);
394 size_t asize = (c + 1) << SPA_MINBLOCKSHIFT;
395 if (c + 1 < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT &&
396 cache[c] == cache[c + 1])
397 asize = (c + 2) << SPA_MINBLOCKSHIFT;
398 for (; off < asize; canary++, off += sizeof (ulong_t)) {
399 if (unlikely(*canary != zio_buf_canary)) {
400 PANIC("ZIO buffer overflow %p (%zu) + %zu %#lx != %#lx",
401 p, size, (canary - p) * sizeof (ulong_t),
402 *canary, zio_buf_canary);
403 }
404 }
405 }
406 #endif
407
408 /*
409 * Use zio_buf_alloc to allocate ZFS metadata. This data will appear in a
410 * crashdump if the kernel panics, so use it judiciously. Obviously, it's
411 * useful to inspect ZFS metadata, but if possible, we should avoid keeping
412 * excess / transient data in-core during a crashdump.
413 */
414 void *
zio_buf_alloc(size_t size)415 zio_buf_alloc(size_t size)
416 {
417 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
418
419 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
420 #if defined(ZFS_DEBUG) && !defined(_KERNEL)
421 atomic_add_64(&zio_buf_cache_allocs[c], 1);
422 #endif
423
424 void *p = kmem_cache_alloc(zio_buf_cache[c], KM_PUSHPAGE);
425 #ifdef ZFS_ZIO_BUF_CANARY
426 zio_buf_put_canary(p, size, zio_buf_cache, c);
427 #endif
428 return (p);
429 }
430
431 /*
432 * Use zio_data_buf_alloc to allocate data. The data will not appear in a
433 * crashdump if the kernel panics. This exists so that we will limit the amount
434 * of ZFS data that shows up in a kernel crashdump. (Thus reducing the amount
435 * of kernel heap dumped to disk when the kernel panics)
436 */
437 void *
zio_data_buf_alloc(size_t size)438 zio_data_buf_alloc(size_t size)
439 {
440 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
441
442 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
443
444 void *p = kmem_cache_alloc(zio_data_buf_cache[c], KM_PUSHPAGE);
445 #ifdef ZFS_ZIO_BUF_CANARY
446 zio_buf_put_canary(p, size, zio_data_buf_cache, c);
447 #endif
448 return (p);
449 }
450
451 void
zio_buf_free(void * buf,size_t size)452 zio_buf_free(void *buf, size_t size)
453 {
454 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
455
456 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
457 #if defined(ZFS_DEBUG) && !defined(_KERNEL)
458 atomic_add_64(&zio_buf_cache_frees[c], 1);
459 #endif
460
461 #ifdef ZFS_ZIO_BUF_CANARY
462 zio_buf_check_canary(buf, size, zio_buf_cache, c);
463 #endif
464 kmem_cache_free(zio_buf_cache[c], buf);
465 }
466
467 void
zio_data_buf_free(void * buf,size_t size)468 zio_data_buf_free(void *buf, size_t size)
469 {
470 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
471
472 VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
473
474 #ifdef ZFS_ZIO_BUF_CANARY
475 zio_buf_check_canary(buf, size, zio_data_buf_cache, c);
476 #endif
477 kmem_cache_free(zio_data_buf_cache[c], buf);
478 }
479
480 static void
zio_abd_free(void * abd,size_t size)481 zio_abd_free(void *abd, size_t size)
482 {
483 (void) size;
484 abd_free((abd_t *)abd);
485 }
486
487 /*
488 * ==========================================================================
489 * Push and pop I/O transform buffers
490 * ==========================================================================
491 */
492 void
zio_push_transform(zio_t * zio,abd_t * data,uint64_t size,uint64_t bufsize,zio_transform_func_t * transform)493 zio_push_transform(zio_t *zio, abd_t *data, uint64_t size, uint64_t bufsize,
494 zio_transform_func_t *transform)
495 {
496 zio_transform_t *zt = kmem_alloc(sizeof (zio_transform_t), KM_SLEEP);
497
498 zt->zt_orig_abd = zio->io_abd;
499 zt->zt_orig_size = zio->io_size;
500 zt->zt_bufsize = bufsize;
501 zt->zt_transform = transform;
502
503 zt->zt_next = zio->io_transform_stack;
504 zio->io_transform_stack = zt;
505
506 zio->io_abd = data;
507 zio->io_size = size;
508 }
509
510 void
zio_pop_transforms(zio_t * zio)511 zio_pop_transforms(zio_t *zio)
512 {
513 zio_transform_t *zt;
514
515 while ((zt = zio->io_transform_stack) != NULL) {
516 if (zt->zt_transform != NULL)
517 zt->zt_transform(zio,
518 zt->zt_orig_abd, zt->zt_orig_size);
519
520 if (zt->zt_bufsize != 0)
521 abd_free(zio->io_abd);
522
523 zio->io_abd = zt->zt_orig_abd;
524 zio->io_size = zt->zt_orig_size;
525 zio->io_transform_stack = zt->zt_next;
526
527 kmem_free(zt, sizeof (zio_transform_t));
528 }
529 }
530
531 /*
532 * ==========================================================================
533 * I/O transform callbacks for subblocks, decompression, and decryption
534 * ==========================================================================
535 */
536 static void
zio_subblock(zio_t * zio,abd_t * data,uint64_t size)537 zio_subblock(zio_t *zio, abd_t *data, uint64_t size)
538 {
539 ASSERT(zio->io_size > size);
540
541 if (zio->io_type == ZIO_TYPE_READ)
542 abd_copy(data, zio->io_abd, size);
543 }
544
545 static void
zio_decompress(zio_t * zio,abd_t * data,uint64_t size)546 zio_decompress(zio_t *zio, abd_t *data, uint64_t size)
547 {
548 if (zio->io_error == 0) {
549 int ret = zio_decompress_data(BP_GET_COMPRESS(zio->io_bp),
550 zio->io_abd, data, zio->io_size, size,
551 &zio->io_prop.zp_complevel);
552
553 if (zio_injection_enabled && ret == 0)
554 ret = zio_handle_fault_injection(zio, EINVAL);
555
556 if (ret != 0)
557 zio->io_error = SET_ERROR(EIO);
558 }
559 }
560
561 static void
zio_decrypt(zio_t * zio,abd_t * data,uint64_t size)562 zio_decrypt(zio_t *zio, abd_t *data, uint64_t size)
563 {
564 int ret;
565 void *tmp;
566 blkptr_t *bp = zio->io_bp;
567 spa_t *spa = zio->io_spa;
568 uint64_t dsobj = zio->io_bookmark.zb_objset;
569 uint64_t lsize = BP_GET_LSIZE(bp);
570 dmu_object_type_t ot = BP_GET_TYPE(bp);
571 uint8_t salt[ZIO_DATA_SALT_LEN];
572 uint8_t iv[ZIO_DATA_IV_LEN];
573 uint8_t mac[ZIO_DATA_MAC_LEN];
574 boolean_t no_crypt = B_FALSE;
575
576 ASSERT(BP_USES_CRYPT(bp));
577 ASSERT3U(size, !=, 0);
578
579 if (zio->io_error != 0)
580 return;
581
582 /*
583 * Verify the cksum of MACs stored in an indirect bp. It will always
584 * be possible to verify this since it does not require an encryption
585 * key.
586 */
587 if (BP_HAS_INDIRECT_MAC_CKSUM(bp)) {
588 zio_crypt_decode_mac_bp(bp, mac);
589
590 if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF) {
591 /*
592 * We haven't decompressed the data yet, but
593 * zio_crypt_do_indirect_mac_checksum() requires
594 * decompressed data to be able to parse out the MACs
595 * from the indirect block. We decompress it now and
596 * throw away the result after we are finished.
597 */
598 abd_t *abd = abd_alloc_linear(lsize, B_TRUE);
599 ret = zio_decompress_data(BP_GET_COMPRESS(bp),
600 zio->io_abd, abd, zio->io_size, lsize,
601 &zio->io_prop.zp_complevel);
602 if (ret != 0) {
603 abd_free(abd);
604 ret = SET_ERROR(EIO);
605 goto error;
606 }
607 ret = zio_crypt_do_indirect_mac_checksum_abd(B_FALSE,
608 abd, lsize, BP_SHOULD_BYTESWAP(bp), mac);
609 abd_free(abd);
610 } else {
611 ret = zio_crypt_do_indirect_mac_checksum_abd(B_FALSE,
612 zio->io_abd, size, BP_SHOULD_BYTESWAP(bp), mac);
613 }
614 abd_copy(data, zio->io_abd, size);
615
616 if (zio_injection_enabled && ot != DMU_OT_DNODE && ret == 0) {
617 ret = zio_handle_decrypt_injection(spa,
618 &zio->io_bookmark, ot, ECKSUM);
619 }
620 if (ret != 0)
621 goto error;
622
623 return;
624 }
625
626 /*
627 * If this is an authenticated block, just check the MAC. It would be
628 * nice to separate this out into its own flag, but when this was done,
629 * we had run out of bits in what is now zio_flag_t. Future cleanup
630 * could make this a flag bit.
631 */
632 if (BP_IS_AUTHENTICATED(bp)) {
633 if (ot == DMU_OT_OBJSET) {
634 ret = spa_do_crypt_objset_mac_abd(B_FALSE, spa,
635 dsobj, zio->io_abd, size, BP_SHOULD_BYTESWAP(bp));
636 } else {
637 zio_crypt_decode_mac_bp(bp, mac);
638 ret = spa_do_crypt_mac_abd(B_FALSE, spa, dsobj,
639 zio->io_abd, size, mac);
640 if (zio_injection_enabled && ret == 0) {
641 ret = zio_handle_decrypt_injection(spa,
642 &zio->io_bookmark, ot, ECKSUM);
643 }
644 }
645 abd_copy(data, zio->io_abd, size);
646
647 if (ret != 0)
648 goto error;
649
650 return;
651 }
652
653 zio_crypt_decode_params_bp(bp, salt, iv);
654
655 if (ot == DMU_OT_INTENT_LOG) {
656 tmp = abd_borrow_buf_copy(zio->io_abd, sizeof (zil_chain_t));
657 zio_crypt_decode_mac_zil(tmp, mac);
658 abd_return_buf(zio->io_abd, tmp, sizeof (zil_chain_t));
659 } else {
660 zio_crypt_decode_mac_bp(bp, mac);
661 }
662
663 ret = spa_do_crypt_abd(B_FALSE, spa, &zio->io_bookmark, BP_GET_TYPE(bp),
664 BP_GET_DEDUP(bp), BP_SHOULD_BYTESWAP(bp), salt, iv, mac, size, data,
665 zio->io_abd, &no_crypt);
666 if (no_crypt)
667 abd_copy(data, zio->io_abd, size);
668
669 if (ret != 0)
670 goto error;
671
672 return;
673
674 error:
675 /* the key was found unless this was speculative or a thorough scrub */
676 ASSERT(ret != EACCES || (zio->io_flags & ZIO_FLAG_SPECULATIVE) ||
677 ((zio->io_flags & ZIO_FLAG_SCRUB) &&
678 !(zio->io_flags & ZIO_FLAG_RAW)));
679
680 /*
681 * If there was a decryption / authentication error return EIO as
682 * the io_error. If this was not a speculative zio, create an ereport.
683 */
684 if (ret == ECKSUM) {
685 zio->io_error = SET_ERROR(EIO);
686 if ((zio->io_flags & ZIO_FLAG_SPECULATIVE) == 0) {
687 spa_log_error(spa, &zio->io_bookmark,
688 BP_GET_PHYSICAL_BIRTH(zio->io_bp));
689 (void) zfs_ereport_post(FM_EREPORT_ZFS_AUTHENTICATION,
690 spa, NULL, &zio->io_bookmark, zio, 0);
691 }
692 } else {
693 zio->io_error = ret;
694 }
695 }
696
697 /*
698 * ==========================================================================
699 * I/O parent/child relationships and pipeline interlocks
700 * ==========================================================================
701 */
702 zio_t *
zio_walk_parents(zio_t * cio,zio_link_t ** zl)703 zio_walk_parents(zio_t *cio, zio_link_t **zl)
704 {
705 list_t *pl = &cio->io_parent_list;
706
707 *zl = (*zl == NULL) ? list_head(pl) : list_next(pl, *zl);
708 if (*zl == NULL)
709 return (NULL);
710
711 ASSERT((*zl)->zl_child == cio);
712 return ((*zl)->zl_parent);
713 }
714
715 zio_t *
zio_walk_children(zio_t * pio,zio_link_t ** zl)716 zio_walk_children(zio_t *pio, zio_link_t **zl)
717 {
718 list_t *cl = &pio->io_child_list;
719
720 ASSERT(MUTEX_HELD(&pio->io_lock));
721
722 *zl = (*zl == NULL) ? list_head(cl) : list_next(cl, *zl);
723 if (*zl == NULL)
724 return (NULL);
725
726 ASSERT((*zl)->zl_parent == pio);
727 return ((*zl)->zl_child);
728 }
729
730 zio_t *
zio_unique_parent(zio_t * cio)731 zio_unique_parent(zio_t *cio)
732 {
733 zio_link_t *zl = NULL;
734 zio_t *pio = zio_walk_parents(cio, &zl);
735
736 VERIFY3P(zio_walk_parents(cio, &zl), ==, NULL);
737 return (pio);
738 }
739
740 static void
zio_add_child_impl(zio_t * pio,zio_t * cio,boolean_t first)741 zio_add_child_impl(zio_t *pio, zio_t *cio, boolean_t first)
742 {
743 /*
744 * Logical I/Os can have logical, gang, or vdev children.
745 * Gang I/Os can have gang or vdev children.
746 * Vdev I/Os can only have vdev children.
747 * The following ASSERT captures all of these constraints.
748 */
749 ASSERT3S(cio->io_child_type, <=, pio->io_child_type);
750
751 /* Parent should not have READY stage if child doesn't have it. */
752 IMPLY((cio->io_pipeline & ZIO_STAGE_READY) == 0 &&
753 (cio->io_child_type != ZIO_CHILD_VDEV),
754 (pio->io_pipeline & ZIO_STAGE_READY) == 0);
755
756 zio_link_t *zl = kmem_cache_alloc(zio_link_cache, KM_SLEEP);
757 zl->zl_parent = pio;
758 zl->zl_child = cio;
759
760 mutex_enter(&pio->io_lock);
761
762 if (first)
763 ASSERT(list_is_empty(&cio->io_parent_list));
764 else
765 mutex_enter(&cio->io_lock);
766
767 ASSERT0(pio->io_state[ZIO_WAIT_DONE]);
768
769 uint64_t *countp = pio->io_children[cio->io_child_type];
770 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
771 countp[w] += !cio->io_state[w];
772
773 list_insert_head(&pio->io_child_list, zl);
774 list_insert_head(&cio->io_parent_list, zl);
775
776 if (!first)
777 mutex_exit(&cio->io_lock);
778
779 mutex_exit(&pio->io_lock);
780 }
781
782 void
zio_add_child(zio_t * pio,zio_t * cio)783 zio_add_child(zio_t *pio, zio_t *cio)
784 {
785 zio_add_child_impl(pio, cio, B_FALSE);
786 }
787
788 static void
zio_add_child_first(zio_t * pio,zio_t * cio)789 zio_add_child_first(zio_t *pio, zio_t *cio)
790 {
791 zio_add_child_impl(pio, cio, B_TRUE);
792 }
793
794 static void
zio_remove_child(zio_t * pio,zio_t * cio,zio_link_t * zl)795 zio_remove_child(zio_t *pio, zio_t *cio, zio_link_t *zl)
796 {
797 ASSERT(zl->zl_parent == pio);
798 ASSERT(zl->zl_child == cio);
799
800 mutex_enter(&pio->io_lock);
801 mutex_enter(&cio->io_lock);
802
803 list_remove(&pio->io_child_list, zl);
804 list_remove(&cio->io_parent_list, zl);
805
806 mutex_exit(&cio->io_lock);
807 mutex_exit(&pio->io_lock);
808 kmem_cache_free(zio_link_cache, zl);
809 }
810
811 static boolean_t
zio_wait_for_children(zio_t * zio,uint8_t childbits,enum zio_wait_type wait)812 zio_wait_for_children(zio_t *zio, uint8_t childbits, enum zio_wait_type wait)
813 {
814 boolean_t waiting = B_FALSE;
815
816 mutex_enter(&zio->io_lock);
817 ASSERT0P(zio->io_stall);
818 for (int c = 0; c < ZIO_CHILD_TYPES; c++) {
819 if (!(ZIO_CHILD_BIT_IS_SET(childbits, c)))
820 continue;
821
822 uint64_t *countp = &zio->io_children[c][wait];
823 if (*countp != 0) {
824 zio->io_stage >>= 1;
825 ASSERT3U(zio->io_stage, !=, ZIO_STAGE_OPEN);
826 zio->io_stall = countp;
827 waiting = B_TRUE;
828 break;
829 }
830 }
831 mutex_exit(&zio->io_lock);
832 return (waiting);
833 }
834
835 /*
836 * The zios a pipeline stage hands back to zio_execute() to run once the
837 * current one stops, chained through io_exec_next in the order they were
838 * added.
839 */
840 typedef struct zio_next {
841 zio_t *zn_list;
842 zio_t **zn_tailp; /* where the next one is appended */
843 } zio_next_t;
844
845 static inline void
zio_next_init(zio_next_t * next)846 zio_next_init(zio_next_t *next)
847 {
848 next->zn_list = NULL;
849 next->zn_tailp = &next->zn_list;
850 }
851
852 __attribute__((always_inline))
853 static inline void
zio_notify_parent(zio_t * pio,zio_t * zio,enum zio_wait_type wait,zio_next_t * nextp)854 zio_notify_parent(zio_t *pio, zio_t *zio, enum zio_wait_type wait,
855 zio_next_t *nextp)
856 {
857 uint64_t *countp = &pio->io_children[zio->io_child_type][wait];
858 int *errorp = &pio->io_child_error[zio->io_child_type];
859
860 mutex_enter(&pio->io_lock);
861 if (zio->io_error && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
862 *errorp = zio_worst_error(*errorp, zio->io_error);
863 pio->io_post |= zio->io_post;
864 ASSERT3U(*countp, >, 0);
865
866 (*countp)--;
867
868 if (*countp == 0 && pio->io_stall == countp) {
869 zio_taskq_type_t type =
870 pio->io_stage < ZIO_STAGE_VDEV_IO_START ? ZIO_TASKQ_ISSUE :
871 ZIO_TASKQ_INTERRUPT;
872 pio->io_stall = NULL;
873 mutex_exit(&pio->io_lock);
874
875 /*
876 * If we can tell the caller to execute this parent next, do
877 * so. We do this if the parent's zio type matches the child's
878 * type, or if it's a zio_null() with no done callback, and so
879 * has no actual work to do. Otherwise dispatch the parent zio
880 * in its own taskq.
881 *
882 * Having the caller execute the parent when possible reduces
883 * locking on the zio taskq's, reduces context switch
884 * overhead, and has no recursion penalty. Note that one
885 * read from disk typically causes at least 3 zio's: a
886 * zio_null(), the logical zio_read(), and then a physical
887 * zio. When the physical ZIO completes, we are able to call
888 * zio_done() on all 3 of these zio's from one invocation of
889 * zio_execute() by returning the parent back to
890 * zio_execute(). Since the parent isn't executed until this
891 * thread returns back to zio_execute(), the caller should do
892 * so promptly.
893 *
894 * In other cases, dispatching the parent prevents
895 * overflowing the stack when we have deeply nested
896 * parent-child relationships, as we do with the "mega zio"
897 * of writes for spa_sync(), and the chain of ZIL blocks.
898 *
899 * More than one parent may become executable at once, and all
900 * of them go back to the caller. It is the caller that keeps
901 * one and dispatches the rest, since only it knows what else
902 * is already waiting for its thread.
903 */
904 if (nextp != NULL &&
905 (pio->io_type == zio->io_type ||
906 (pio->io_type == ZIO_TYPE_NULL && !pio->io_done))) {
907 ASSERT3P(pio->io_exec_next, ==, NULL);
908 *nextp->zn_tailp = pio;
909 nextp->zn_tailp = &pio->io_exec_next;
910 } else {
911 zio_taskq_dispatch(pio, type, B_FALSE);
912 }
913 } else {
914 mutex_exit(&pio->io_lock);
915 }
916 }
917
918 static void
zio_inherit_child_errors(zio_t * zio,enum zio_child c)919 zio_inherit_child_errors(zio_t *zio, enum zio_child c)
920 {
921 if (zio->io_child_error[c] != 0 && zio->io_error == 0)
922 zio->io_error = zio->io_child_error[c];
923 }
924
925 int
zio_bookmark_compare(const void * x1,const void * x2)926 zio_bookmark_compare(const void *x1, const void *x2)
927 {
928 const zio_t *z1 = x1;
929 const zio_t *z2 = x2;
930 const zbookmark_phys_t *zb1 = &z1->io_bookmark;
931 const zbookmark_phys_t *zb2 = &z2->io_bookmark;
932
933 int cmp = TREE_CMP(zb1->zb_objset, zb2->zb_objset);
934 if (cmp != 0)
935 return (cmp);
936
937 cmp = TREE_CMP(zb1->zb_object, zb2->zb_object);
938 if (cmp != 0)
939 return (cmp);
940
941 cmp = TREE_CMP(zb1->zb_level, zb2->zb_level);
942 if (cmp != 0)
943 return (cmp);
944
945 cmp = TREE_CMP(zb1->zb_blkid, zb2->zb_blkid);
946 if (cmp != 0)
947 return (cmp);
948
949 return (TREE_PCMP(z1, z2));
950 }
951
952 /*
953 * ==========================================================================
954 * Create the various types of I/O (read, write, free, etc)
955 * ==========================================================================
956 */
957 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)958 zio_create(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
959 abd_t *data, uint64_t lsize, uint64_t psize, zio_done_func_t *done,
960 void *private, zio_type_t type, zio_priority_t priority,
961 zio_flag_t flags, vdev_t *vd, uint64_t offset,
962 const zbookmark_phys_t *zb, enum zio_stage stage,
963 enum zio_stage pipeline)
964 {
965 zio_t *zio;
966
967 IMPLY(type != ZIO_TYPE_TRIM, psize <= SPA_MAXBLOCKSIZE);
968 ASSERT0(P2PHASE(psize, SPA_MINBLOCKSIZE));
969 ASSERT0(P2PHASE(offset, SPA_MINBLOCKSIZE));
970
971 ASSERT(!vd || spa_config_held(spa, SCL_STATE_ALL, RW_READER));
972 ASSERT(!bp || !(flags & ZIO_FLAG_CONFIG_WRITER));
973 ASSERT(vd || stage == ZIO_STAGE_OPEN);
974
975 IMPLY(lsize != psize, (flags & ZIO_FLAG_RAW_COMPRESS) != 0);
976
977 zio = kmem_cache_alloc(zio_cache, KM_SLEEP);
978 memset(zio, 0, sizeof (zio_t));
979
980 mutex_init(&zio->io_lock, NULL, MUTEX_NOLOCKDEP, NULL);
981 cv_init(&zio->io_cv, NULL, CV_DEFAULT, NULL);
982
983 list_create(&zio->io_parent_list, sizeof (zio_link_t),
984 offsetof(zio_link_t, zl_parent_node));
985 list_create(&zio->io_child_list, sizeof (zio_link_t),
986 offsetof(zio_link_t, zl_child_node));
987 metaslab_trace_init(ZIO_ALLOC_LIST(zio));
988
989 if (vd != NULL)
990 zio->io_child_type = ZIO_CHILD_VDEV;
991 else if (flags & ZIO_FLAG_GANG_CHILD)
992 zio->io_child_type = ZIO_CHILD_GANG;
993 else if (flags & ZIO_FLAG_DDT_CHILD)
994 zio->io_child_type = ZIO_CHILD_DDT;
995 else
996 zio->io_child_type = ZIO_CHILD_LOGICAL;
997
998 if (bp != NULL) {
999 if (type != ZIO_TYPE_WRITE ||
1000 zio->io_child_type == ZIO_CHILD_DDT) {
1001 zio->io_bp_copy = *bp;
1002 zio->io_bp = &zio->io_bp_copy; /* so caller can free */
1003 } else {
1004 zio->io_bp = (blkptr_t *)bp;
1005 }
1006 zio->io_bp_orig = *bp;
1007 if (zio->io_child_type == ZIO_CHILD_LOGICAL)
1008 zio->io_logical = zio;
1009 if (zio->io_child_type > ZIO_CHILD_GANG && BP_IS_GANG(bp))
1010 pipeline |= ZIO_GANG_STAGES;
1011 if (flags & ZIO_FLAG_PREALLOCATED) {
1012 BP_ZERO_DVAS(zio->io_bp);
1013 BP_SET_BIRTH(zio->io_bp, 0, 0);
1014 }
1015 }
1016
1017 zio->io_spa = spa;
1018 zio->io_txg = txg;
1019 zio->io_done = done;
1020 zio->io_private = private;
1021 zio->io_type = type;
1022 zio->io_priority = priority;
1023 zio->io_vd = vd;
1024 zio->io_offset = offset;
1025 zio->io_orig_abd = zio->io_abd = data;
1026 zio->io_orig_size = zio->io_size = psize;
1027 zio->io_lsize = lsize;
1028 zio->io_orig_flags = zio->io_flags = flags;
1029 zio->io_orig_stage = zio->io_stage = stage;
1030 zio->io_orig_pipeline = zio->io_pipeline = pipeline;
1031 zio->io_pipeline_trace = ZIO_STAGE_OPEN;
1032 zio->io_allocator = ZIO_ALLOCATOR_NONE;
1033
1034 zio->io_state[ZIO_WAIT_READY] = (stage >= ZIO_STAGE_READY) ||
1035 (pipeline & ZIO_STAGE_READY) == 0;
1036 zio->io_state[ZIO_WAIT_DONE] = (stage >= ZIO_STAGE_DONE);
1037
1038 if (zb != NULL)
1039 zio->io_bookmark = *zb;
1040
1041 if (pio != NULL) {
1042 zio->io_metaslab_class = pio->io_metaslab_class;
1043 if (zio->io_logical == NULL)
1044 zio->io_logical = pio->io_logical;
1045 if (zio->io_child_type == ZIO_CHILD_GANG)
1046 zio->io_gang_leader = pio->io_gang_leader;
1047 zio_add_child_first(pio, zio);
1048 }
1049
1050 taskq_init_ent(&zio->io_tqent);
1051
1052 return (zio);
1053 }
1054
1055 void
zio_destroy(zio_t * zio)1056 zio_destroy(zio_t *zio)
1057 {
1058 ASSERT3P(zio->io_batch, ==, NULL);
1059 ASSERT3P(zio->io_child_batch, ==, NULL);
1060 ASSERT3P(zio->io_exec_next, ==, NULL);
1061 metaslab_trace_fini(ZIO_ALLOC_LIST(zio));
1062 list_destroy(&zio->io_parent_list);
1063 list_destroy(&zio->io_child_list);
1064 mutex_destroy(&zio->io_lock);
1065 cv_destroy(&zio->io_cv);
1066 kmem_cache_free(zio_cache, zio);
1067 }
1068
1069 /*
1070 * ZIO intended to be between others. Provides synchronization at READY
1071 * and DONE pipeline stages and calls the respective callbacks.
1072 */
1073 zio_t *
zio_null(zio_t * pio,spa_t * spa,vdev_t * vd,zio_done_func_t * done,void * private,zio_flag_t flags)1074 zio_null(zio_t *pio, spa_t *spa, vdev_t *vd, zio_done_func_t *done,
1075 void *private, zio_flag_t flags)
1076 {
1077 zio_t *zio;
1078
1079 zio = zio_create(pio, spa, 0, NULL, NULL, 0, 0, done, private,
1080 ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
1081 ZIO_STAGE_OPEN, ZIO_INTERLOCK_PIPELINE);
1082
1083 return (zio);
1084 }
1085
1086 /*
1087 * ZIO intended to be a root of a tree. Unlike null ZIO does not have a
1088 * READY pipeline stage (is ready on creation), so it should not be used
1089 * as child of any ZIO that may need waiting for grandchildren READY stage
1090 * (any other ZIO type).
1091 */
1092 zio_t *
zio_root(spa_t * spa,zio_done_func_t * done,void * private,zio_flag_t flags)1093 zio_root(spa_t *spa, zio_done_func_t *done, void *private, zio_flag_t flags)
1094 {
1095 zio_t *zio;
1096
1097 zio = zio_create(NULL, spa, 0, NULL, NULL, 0, 0, done, private,
1098 ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, NULL, 0, NULL,
1099 ZIO_STAGE_OPEN, ZIO_ROOT_PIPELINE);
1100
1101 return (zio);
1102 }
1103
1104 static int
zfs_blkptr_verify_log(spa_t * spa,const blkptr_t * bp,enum blk_verify_flag blk_verify,const char * fmt,...)1105 zfs_blkptr_verify_log(spa_t *spa, const blkptr_t *bp,
1106 enum blk_verify_flag blk_verify, const char *fmt, ...)
1107 {
1108 va_list adx;
1109 char buf[256];
1110
1111 va_start(adx, fmt);
1112 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
1113 va_end(adx);
1114
1115 zfs_dbgmsg("bad blkptr at %px: "
1116 "DVA[0]=%#llx/%#llx "
1117 "DVA[1]=%#llx/%#llx "
1118 "DVA[2]=%#llx/%#llx "
1119 "prop=%#llx "
1120 "prop2=%#llx "
1121 "pad=%#llx "
1122 "phys_birth=%#llx "
1123 "birth=%#llx "
1124 "fill=%#llx "
1125 "cksum=%#llx/%#llx/%#llx/%#llx",
1126 bp,
1127 (long long)bp->blk_dva[0].dva_word[0],
1128 (long long)bp->blk_dva[0].dva_word[1],
1129 (long long)bp->blk_dva[1].dva_word[0],
1130 (long long)bp->blk_dva[1].dva_word[1],
1131 (long long)bp->blk_dva[2].dva_word[0],
1132 (long long)bp->blk_dva[2].dva_word[1],
1133 (long long)bp->blk_prop,
1134 (long long)bp->blk_prop2,
1135 (long long)bp->blk_pad,
1136 (long long)BP_GET_RAW_PHYSICAL_BIRTH(bp),
1137 (long long)BP_GET_LOGICAL_BIRTH(bp),
1138 (long long)bp->blk_fill,
1139 (long long)bp->blk_cksum.zc_word[0],
1140 (long long)bp->blk_cksum.zc_word[1],
1141 (long long)bp->blk_cksum.zc_word[2],
1142 (long long)bp->blk_cksum.zc_word[3]);
1143 switch (blk_verify) {
1144 case BLK_VERIFY_HALT:
1145 zfs_panic_recover("%s: %s", spa_name(spa), buf);
1146 break;
1147 case BLK_VERIFY_LOG:
1148 zfs_dbgmsg("%s: %s", spa_name(spa), buf);
1149 break;
1150 case BLK_VERIFY_ONLY:
1151 break;
1152 }
1153
1154 return (1);
1155 }
1156
1157 /*
1158 * Verify the block pointer fields contain reasonable values. This means
1159 * it only contains known object types, checksum/compression identifiers,
1160 * block sizes within the maximum allowed limits, valid DVAs, etc.
1161 *
1162 * If everything checks out 0 is returned. The zfs_blkptr_verify
1163 * argument controls the behavior when an invalid field is detected.
1164 *
1165 * Values for blk_verify_flag:
1166 * BLK_VERIFY_ONLY: evaluate the block
1167 * BLK_VERIFY_LOG: evaluate the block and log problems
1168 * BLK_VERIFY_HALT: call zfs_panic_recover on error
1169 *
1170 * Values for blk_config_flag:
1171 * BLK_CONFIG_HELD: caller holds SCL_VDEV for writer
1172 * BLK_CONFIG_NEEDED: caller holds no config lock, SCL_VDEV will be
1173 * obtained for reader
1174 * BLK_CONFIG_SKIP: skip checks which require SCL_VDEV, for better
1175 * performance
1176 */
1177 int
zfs_blkptr_verify(spa_t * spa,const blkptr_t * bp,enum blk_config_flag blk_config,enum blk_verify_flag blk_verify)1178 zfs_blkptr_verify(spa_t *spa, const blkptr_t *bp,
1179 enum blk_config_flag blk_config, enum blk_verify_flag blk_verify)
1180 {
1181 int errors = 0;
1182
1183 if (unlikely(!DMU_OT_IS_VALID(BP_GET_TYPE(bp)))) {
1184 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1185 "blkptr at %px has invalid TYPE %llu",
1186 bp, (longlong_t)BP_GET_TYPE(bp));
1187 }
1188 if (unlikely(BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_FUNCTIONS)) {
1189 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1190 "blkptr at %px has invalid COMPRESS %llu",
1191 bp, (longlong_t)BP_GET_COMPRESS(bp));
1192 }
1193 if (unlikely(BP_GET_LSIZE(bp) > SPA_MAXBLOCKSIZE)) {
1194 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1195 "blkptr at %px has invalid LSIZE %llu",
1196 bp, (longlong_t)BP_GET_LSIZE(bp));
1197 }
1198 if (BP_IS_EMBEDDED(bp)) {
1199 if (unlikely(BPE_GET_ETYPE(bp) >= NUM_BP_EMBEDDED_TYPES)) {
1200 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1201 "blkptr at %px has invalid ETYPE %llu",
1202 bp, (longlong_t)BPE_GET_ETYPE(bp));
1203 }
1204 if (unlikely(BPE_GET_PSIZE(bp) > BPE_PAYLOAD_SIZE)) {
1205 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1206 "blkptr at %px has invalid PSIZE %llu",
1207 bp, (longlong_t)BPE_GET_PSIZE(bp));
1208 }
1209 return (errors ? ECKSUM : 0);
1210 } else if (BP_IS_HOLE(bp)) {
1211 /*
1212 * Holes are allowed (expected, even) to have no DVAs, no
1213 * checksum, and no psize.
1214 */
1215 return (errors ? ECKSUM : 0);
1216 } else if (unlikely(!DVA_IS_VALID(&bp->blk_dva[0]))) {
1217 /* Non-hole, non-embedded BPs _must_ have at least one DVA */
1218 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1219 "blkptr at %px has no valid DVAs", bp);
1220 }
1221 if (unlikely(BP_GET_CHECKSUM(bp) >= ZIO_CHECKSUM_FUNCTIONS)) {
1222 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1223 "blkptr at %px has invalid CHECKSUM %llu",
1224 bp, (longlong_t)BP_GET_CHECKSUM(bp));
1225 }
1226 if (unlikely(BP_GET_PSIZE(bp) > SPA_MAXBLOCKSIZE)) {
1227 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1228 "blkptr at %px has invalid PSIZE %llu",
1229 bp, (longlong_t)BP_GET_PSIZE(bp));
1230 }
1231
1232 /*
1233 * Do not verify individual DVAs if the config is not trusted. This
1234 * will be done once the zio is executed in vdev_mirror_map_alloc.
1235 */
1236 if (unlikely(!spa->spa_trust_config))
1237 return (errors ? ECKSUM : 0);
1238
1239 switch (blk_config) {
1240 case BLK_CONFIG_HELD:
1241 ASSERT(spa_config_held(spa, SCL_VDEV, RW_WRITER));
1242 break;
1243 case BLK_CONFIG_NEEDED:
1244 spa_config_enter(spa, SCL_VDEV, bp, RW_READER);
1245 break;
1246 case BLK_CONFIG_NEEDED_TRY:
1247 if (!spa_config_tryenter(spa, SCL_VDEV, bp, RW_READER))
1248 return (EBUSY);
1249 break;
1250 case BLK_CONFIG_SKIP:
1251 return (errors ? ECKSUM : 0);
1252 default:
1253 panic("invalid blk_config %u", blk_config);
1254 }
1255
1256 /*
1257 * Pool-specific checks.
1258 *
1259 * Note: it would be nice to verify that the logical birth
1260 * and physical birth are not too large. However,
1261 * spa_freeze() allows the birth time of log blocks (and
1262 * dmu_sync()-ed blocks that are in the log) to be arbitrarily
1263 * large.
1264 */
1265 for (int i = 0; i < BP_GET_NDVAS(bp); i++) {
1266 const dva_t *dva = &bp->blk_dva[i];
1267 uint64_t vdevid = DVA_GET_VDEV(dva);
1268
1269 if (unlikely(vdevid >= spa->spa_root_vdev->vdev_children)) {
1270 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1271 "blkptr at %px DVA %u has invalid VDEV %llu",
1272 bp, i, (longlong_t)vdevid);
1273 continue;
1274 }
1275 vdev_t *vd = spa->spa_root_vdev->vdev_child[vdevid];
1276 if (unlikely(vd == NULL)) {
1277 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1278 "blkptr at %px DVA %u has invalid VDEV %llu",
1279 bp, i, (longlong_t)vdevid);
1280 continue;
1281 }
1282 if (unlikely(vd->vdev_ops == &vdev_hole_ops)) {
1283 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1284 "blkptr at %px DVA %u has hole VDEV %llu",
1285 bp, i, (longlong_t)vdevid);
1286 continue;
1287 }
1288 if (vd->vdev_ops == &vdev_missing_ops) {
1289 /*
1290 * "missing" vdevs are valid during import, but we
1291 * don't have their detailed info (e.g. asize), so
1292 * we can't perform any more checks on them.
1293 */
1294 continue;
1295 }
1296 uint64_t offset = DVA_GET_OFFSET(dva);
1297 uint64_t asize = DVA_GET_ASIZE(dva);
1298 if (DVA_GET_GANG(dva))
1299 asize = vdev_gang_header_asize(vd);
1300 if (unlikely(offset + asize > vd->vdev_asize)) {
1301 errors += zfs_blkptr_verify_log(spa, bp, blk_verify,
1302 "blkptr at %px DVA %u has invalid OFFSET %llu",
1303 bp, i, (longlong_t)offset);
1304 }
1305 }
1306 if (blk_config == BLK_CONFIG_NEEDED || blk_config ==
1307 BLK_CONFIG_NEEDED_TRY)
1308 spa_config_exit(spa, SCL_VDEV, bp);
1309
1310 return (errors ? ECKSUM : 0);
1311 }
1312
1313 boolean_t
zfs_dva_valid(spa_t * spa,const dva_t * dva,const blkptr_t * bp)1314 zfs_dva_valid(spa_t *spa, const dva_t *dva, const blkptr_t *bp)
1315 {
1316 (void) bp;
1317 uint64_t vdevid = DVA_GET_VDEV(dva);
1318
1319 if (vdevid >= spa->spa_root_vdev->vdev_children)
1320 return (B_FALSE);
1321
1322 vdev_t *vd = spa->spa_root_vdev->vdev_child[vdevid];
1323 if (vd == NULL)
1324 return (B_FALSE);
1325
1326 if (vd->vdev_ops == &vdev_hole_ops)
1327 return (B_FALSE);
1328
1329 if (vd->vdev_ops == &vdev_missing_ops) {
1330 return (B_FALSE);
1331 }
1332
1333 uint64_t offset = DVA_GET_OFFSET(dva);
1334 uint64_t asize = DVA_GET_ASIZE(dva);
1335
1336 if (DVA_GET_GANG(dva))
1337 asize = vdev_gang_header_asize(vd);
1338 if (offset + asize > vd->vdev_asize)
1339 return (B_FALSE);
1340
1341 return (B_TRUE);
1342 }
1343
1344 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)1345 zio_read(zio_t *pio, spa_t *spa, const blkptr_t *bp,
1346 abd_t *data, uint64_t size, zio_done_func_t *done, void *private,
1347 zio_priority_t priority, zio_flag_t flags, const zbookmark_phys_t *zb)
1348 {
1349 zio_t *zio;
1350
1351 zio = zio_create(pio, spa, BP_GET_PHYSICAL_BIRTH(bp), bp,
1352 data, size, size, done, private,
1353 ZIO_TYPE_READ, priority, flags, NULL, 0, zb,
1354 ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
1355 ZIO_DDT_CHILD_READ_PIPELINE : ZIO_READ_PIPELINE);
1356
1357 return (zio);
1358 }
1359
1360 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)1361 zio_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
1362 abd_t *data, uint64_t lsize, uint64_t psize, const zio_prop_t *zp,
1363 zio_done_func_t *ready, zio_done_func_t *children_ready,
1364 zio_done_func_t *done, void *private, zio_priority_t priority,
1365 zio_flag_t flags, const zbookmark_phys_t *zb)
1366 {
1367 zio_t *zio;
1368 enum zio_stage pipeline = zp->zp_direct_write == B_TRUE ?
1369 ZIO_DIRECT_WRITE_PIPELINE : (flags & ZIO_FLAG_DDT_CHILD) ?
1370 ZIO_DDT_CHILD_WRITE_PIPELINE : ZIO_WRITE_PIPELINE;
1371
1372
1373 zio = zio_create(pio, spa, txg, bp, data, lsize, psize, done, private,
1374 ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb,
1375 ZIO_STAGE_OPEN, pipeline);
1376
1377 zio->io_ready = ready;
1378 zio->io_children_ready = children_ready;
1379 zio->io_prop = *zp;
1380
1381 /*
1382 * Data can be NULL if we are going to call zio_write_override() to
1383 * provide the already-allocated BP. But we may need the data to
1384 * verify a dedup hit (if requested). In this case, don't try to
1385 * dedup (just take the already-allocated BP verbatim). Encrypted
1386 * dedup blocks need data as well so we also disable dedup in this
1387 * case.
1388 */
1389 if (data == NULL &&
1390 (zio->io_prop.zp_dedup_verify || zio->io_prop.zp_encrypt)) {
1391 zio->io_prop.zp_dedup = zio->io_prop.zp_dedup_verify = B_FALSE;
1392 }
1393
1394 return (zio);
1395 }
1396
1397 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)1398 zio_rewrite(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, abd_t *data,
1399 uint64_t size, zio_done_func_t *done, void *private,
1400 zio_priority_t priority, zio_flag_t flags, zbookmark_phys_t *zb)
1401 {
1402 zio_t *zio;
1403
1404 zio = zio_create(pio, spa, txg, bp, data, size, size, done, private,
1405 ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_IO_REWRITE, NULL, 0, zb,
1406 ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE);
1407
1408 return (zio);
1409 }
1410
1411 void
zio_write_override(zio_t * zio,blkptr_t * bp,int copies,int gang_copies,boolean_t nopwrite,boolean_t brtwrite)1412 zio_write_override(zio_t *zio, blkptr_t *bp, int copies, int gang_copies,
1413 boolean_t nopwrite, boolean_t brtwrite)
1414 {
1415 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
1416 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1417 ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
1418 ASSERT(zio->io_txg == spa_syncing_txg(zio->io_spa));
1419 ASSERT(!brtwrite || !nopwrite);
1420
1421 /*
1422 * We must reset the io_prop to match the values that existed
1423 * when the bp was first written by dmu_sync() keeping in mind
1424 * that nopwrite and dedup are mutually exclusive.
1425 */
1426 zio->io_prop.zp_dedup = nopwrite ? B_FALSE : zio->io_prop.zp_dedup;
1427 zio->io_prop.zp_nopwrite = nopwrite;
1428 zio->io_prop.zp_brtwrite = brtwrite;
1429 zio->io_prop.zp_copies = copies;
1430 zio->io_prop.zp_gang_copies = gang_copies;
1431 zio->io_bp_override = bp;
1432 }
1433
1434 void
zio_free(spa_t * spa,uint64_t txg,const blkptr_t * bp)1435 zio_free(spa_t *spa, uint64_t txg, const blkptr_t *bp)
1436 {
1437
1438 (void) zfs_blkptr_verify(spa, bp, BLK_CONFIG_NEEDED, BLK_VERIFY_HALT);
1439
1440 /*
1441 * The check for EMBEDDED is a performance optimization. We
1442 * process the free here (by ignoring it) rather than
1443 * putting it on the list and then processing it in zio_free_sync().
1444 */
1445 if (BP_IS_EMBEDDED(bp))
1446 return;
1447
1448 /*
1449 * Frees that are for the currently-syncing txg, are not going to be
1450 * deferred, and which will not need to do a read (i.e. not GANG or
1451 * DEDUP), can be processed immediately. Otherwise, put them on the
1452 * in-memory list for later processing.
1453 *
1454 * Note that we only defer frees after zfs_sync_pass_deferred_free
1455 * when the log space map feature is disabled. [see relevant comment
1456 * in spa_sync_iterate_to_convergence()]
1457 */
1458 if (BP_IS_GANG(bp) ||
1459 BP_GET_DEDUP(bp) ||
1460 txg != spa->spa_syncing_txg ||
1461 (spa_sync_pass(spa) >= zfs_sync_pass_deferred_free &&
1462 !spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP)) ||
1463 brt_maybe_exists(spa, bp)) {
1464 metaslab_check_free(spa, bp);
1465 bplist_append(&spa->spa_free_bplist[txg & TXG_MASK], bp);
1466 } else {
1467 VERIFY0P(zio_free_sync(NULL, spa, txg, bp, 0));
1468 }
1469 }
1470
1471 /*
1472 * To improve performance, this function may return NULL if we were able
1473 * to do the free immediately. This avoids the cost of creating a zio
1474 * (and linking it to the parent, etc).
1475 */
1476 zio_t *
zio_free_sync(zio_t * pio,spa_t * spa,uint64_t txg,const blkptr_t * bp,zio_flag_t flags)1477 zio_free_sync(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
1478 zio_flag_t flags)
1479 {
1480 ASSERT(!BP_IS_HOLE(bp));
1481 ASSERT(spa_syncing_txg(spa) == txg);
1482
1483 if (BP_IS_EMBEDDED(bp))
1484 return (NULL);
1485
1486 metaslab_check_free(spa, bp);
1487 arc_freed(spa, bp);
1488 dsl_scan_freed(spa, bp);
1489
1490 if (BP_IS_GANG(bp) ||
1491 BP_GET_DEDUP(bp) ||
1492 brt_maybe_exists(spa, bp)) {
1493 /*
1494 * GANG, DEDUP and BRT blocks can induce a read (for the gang
1495 * block header, the DDT or the BRT), so issue them
1496 * asynchronously so that this thread is not tied up.
1497 */
1498 enum zio_stage stage =
1499 ZIO_FREE_PIPELINE | ZIO_STAGE_ISSUE_ASYNC;
1500
1501 return (zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
1502 BP_GET_PSIZE(bp), NULL, NULL,
1503 ZIO_TYPE_FREE, ZIO_PRIORITY_NOW,
1504 flags, NULL, 0, NULL, ZIO_STAGE_OPEN, stage));
1505 } else {
1506 metaslab_free(spa, bp, txg, B_FALSE);
1507 return (NULL);
1508 }
1509 }
1510
1511 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)1512 zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
1513 zio_done_func_t *done, void *private, zio_flag_t flags)
1514 {
1515 zio_t *zio;
1516
1517 (void) zfs_blkptr_verify(spa, bp, (flags & ZIO_FLAG_CONFIG_WRITER) ?
1518 BLK_CONFIG_HELD : BLK_CONFIG_NEEDED, BLK_VERIFY_HALT);
1519
1520 if (BP_IS_EMBEDDED(bp))
1521 return (zio_null(pio, spa, NULL, NULL, NULL, 0));
1522
1523 /*
1524 * A claim is an allocation of a specific block. Claims are needed
1525 * to support immediate writes in the intent log. The issue is that
1526 * immediate writes contain committed data, but in a txg that was
1527 * *not* committed. Upon opening the pool after an unclean shutdown,
1528 * the intent log claims all blocks that contain immediate write data
1529 * so that the SPA knows they're in use.
1530 *
1531 * All claims *must* be resolved in the first txg -- before the SPA
1532 * starts allocating blocks -- so that nothing is allocated twice.
1533 * If txg == 0 we just verify that the block is claimable.
1534 */
1535 ASSERT3U(BP_GET_LOGICAL_BIRTH(&spa->spa_uberblock.ub_rootbp), <,
1536 spa_min_claim_txg(spa));
1537 ASSERT(txg == spa_min_claim_txg(spa) || txg == 0);
1538 ASSERT(!BP_GET_DEDUP(bp) || !spa_writeable(spa)); /* zdb(8) */
1539
1540 zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
1541 BP_GET_PSIZE(bp), done, private, ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW,
1542 flags, NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE);
1543 ASSERT0(zio->io_queued_timestamp);
1544
1545 return (zio);
1546 }
1547
1548 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)1549 zio_trim(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
1550 zio_done_func_t *done, void *private, zio_priority_t priority,
1551 zio_flag_t flags, enum trim_flag trim_flags)
1552 {
1553 zio_t *zio;
1554
1555 ASSERT0(vd->vdev_children);
1556 ASSERT0(P2PHASE(offset, 1ULL << vd->vdev_ashift));
1557 ASSERT0(P2PHASE(size, 1ULL << vd->vdev_ashift));
1558 ASSERT3U(size, !=, 0);
1559
1560 zio = zio_create(pio, vd->vdev_spa, 0, NULL, NULL, size, size, done,
1561 private, ZIO_TYPE_TRIM, priority, flags | ZIO_FLAG_PHYSICAL,
1562 vd, offset, NULL, ZIO_STAGE_OPEN, ZIO_TRIM_PIPELINE);
1563 zio->io_trim_flags = trim_flags;
1564
1565 return (zio);
1566 }
1567
1568 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)1569 zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
1570 abd_t *data, int checksum, zio_done_func_t *done, void *private,
1571 zio_priority_t priority, zio_flag_t flags, boolean_t labels)
1572 {
1573 zio_t *zio;
1574
1575 ASSERT0(vd->vdev_children);
1576 ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
1577 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
1578 ASSERT3U(offset + size, <=, vd->vdev_psize);
1579
1580 zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, size, done,
1581 private, ZIO_TYPE_READ, priority, flags | ZIO_FLAG_PHYSICAL, vd,
1582 offset, NULL, ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE);
1583
1584 zio->io_prop.zp_checksum = checksum;
1585
1586 return (zio);
1587 }
1588
1589 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)1590 zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
1591 abd_t *data, int checksum, zio_done_func_t *done, void *private,
1592 zio_priority_t priority, zio_flag_t flags, boolean_t labels)
1593 {
1594 zio_t *zio;
1595
1596 ASSERT0(vd->vdev_children);
1597 ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
1598 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
1599 ASSERT3U(offset + size, <=, vd->vdev_psize);
1600
1601 zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, size, done,
1602 private, ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_PHYSICAL, vd,
1603 offset, NULL, ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE);
1604
1605 zio->io_prop.zp_checksum = checksum;
1606
1607 if (zio_checksum_table[checksum].ci_flags & ZCHECKSUM_FLAG_EMBEDDED) {
1608 /*
1609 * zec checksums are necessarily destructive -- they modify
1610 * the end of the write buffer to hold the verifier/checksum.
1611 * Therefore, we must make a local copy in case the data is
1612 * being written to multiple places in parallel.
1613 */
1614 abd_t *wbuf = abd_alloc_sametype(data, size);
1615 abd_copy(wbuf, data, size);
1616
1617 zio_push_transform(zio, wbuf, size, size, NULL);
1618 }
1619
1620 return (zio);
1621 }
1622
1623 /*
1624 * Create a child I/O to do some work for us.
1625 */
1626 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)1627 zio_vdev_child_io(zio_t *pio, blkptr_t *bp, vdev_t *vd, uint64_t offset,
1628 abd_t *data, uint64_t size, int type, zio_priority_t priority,
1629 zio_flag_t flags, zio_done_func_t *done, void *private)
1630 {
1631 enum zio_stage pipeline = ZIO_VDEV_CHILD_PIPELINE;
1632 zio_t *zio;
1633
1634 /*
1635 * vdev child I/Os do not propagate their error to the parent.
1636 * Therefore, for correct operation the caller *must* check for
1637 * and handle the error in the child i/o's done callback.
1638 * The only exceptions are i/os that we don't care about
1639 * (OPTIONAL or REPAIR).
1640 */
1641 ASSERT((flags & ZIO_FLAG_OPTIONAL) || (flags & ZIO_FLAG_IO_REPAIR) ||
1642 done != NULL);
1643
1644 if (type == ZIO_TYPE_READ && bp != NULL) {
1645 /*
1646 * If we have the bp, then the child should perform the
1647 * checksum and the parent need not. This pushes error
1648 * detection as close to the leaves as possible and
1649 * eliminates redundant checksums in the interior nodes.
1650 */
1651 pipeline |= ZIO_STAGE_CHECKSUM_VERIFY;
1652 pio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
1653 /*
1654 * We never allow the mirror VDEV to attempt reading from any
1655 * additional data copies after the first Direct I/O checksum
1656 * verify failure. This is to avoid bad data being written out
1657 * through the mirror during self healing. See comment in
1658 * vdev_mirror_io_done() for more details.
1659 */
1660 ASSERT0(pio->io_post & ZIO_POST_DIO_CHKSUM_ERR);
1661 } else if (type == ZIO_TYPE_WRITE &&
1662 pio->io_prop.zp_direct_write == B_TRUE) {
1663 /*
1664 * By default we only will verify checksums for Direct I/O
1665 * writes for Linux. FreeBSD is able to place user pages under
1666 * write protection before issuing them to the ZIO pipeline.
1667 *
1668 * Checksum validation errors will only be reported through
1669 * the top-level VDEV, which is set by this child ZIO.
1670 */
1671 ASSERT3P(bp, !=, NULL);
1672 ASSERT3U(pio->io_child_type, ==, ZIO_CHILD_LOGICAL);
1673 pipeline |= ZIO_STAGE_DIO_CHECKSUM_VERIFY;
1674 }
1675
1676 if (vd->vdev_ops->vdev_op_leaf) {
1677 ASSERT0(vd->vdev_children);
1678 offset += VDEV_LABEL_START_SIZE;
1679 }
1680
1681 flags |= ZIO_VDEV_CHILD_FLAGS(pio);
1682
1683 /*
1684 * If we've decided to do a repair, the write is not speculative --
1685 * even if the original read was. Rebuild is an exception since we
1686 * cannot always ensure its data integrity.
1687 */
1688 if ((flags & ZIO_FLAG_IO_REPAIR) &&
1689 pio->io_priority != ZIO_PRIORITY_REBUILD)
1690 flags &= ~ZIO_FLAG_SPECULATIVE;
1691
1692 /*
1693 * If we're creating a child I/O that is not associated with a
1694 * top-level vdev, then the child zio is not an allocating I/O.
1695 * If this is a retried I/O then we ignore it since we will
1696 * have already processed the original allocating I/O.
1697 */
1698 if (flags & ZIO_FLAG_ALLOC_THROTTLED &&
1699 (vd != vd->vdev_top || (flags & ZIO_FLAG_IO_RETRY)) &&
1700 type == ZIO_TYPE_WRITE) {
1701 ASSERT(pio->io_metaslab_class != NULL);
1702 ASSERT(pio->io_metaslab_class->mc_alloc_throttle_enabled);
1703 ASSERT(priority == ZIO_PRIORITY_ASYNC_WRITE);
1704 ASSERT(!(flags & ZIO_FLAG_IO_REPAIR));
1705 ASSERT(!(pio->io_flags & ZIO_FLAG_IO_REWRITE) ||
1706 pio->io_child_type == ZIO_CHILD_GANG);
1707
1708 flags &= ~ZIO_FLAG_ALLOC_THROTTLED;
1709 }
1710
1711 zio = zio_create(pio, pio->io_spa, pio->io_txg, bp, data, size, size,
1712 done, private, type, priority, flags, vd, offset, &pio->io_bookmark,
1713 ZIO_STAGE_VDEV_IO_START >> 1, pipeline);
1714 ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
1715
1716 if (pio->io_child_batch != NULL) {
1717 /*
1718 * Whatever wakes this child up, all it has left to do are the
1719 * few cheap stages of ZIO_VDEV_CHILD_PIPELINE, so it is better
1720 * run right there than dispatched.
1721 */
1722 zio->io_flags |= ZIO_FLAG_LIGHTWEIGHT;
1723
1724 /*
1725 * Only children that come back from the block layer gain
1726 * anything from a batch. Interior ones are dispatched by their
1727 * own child's zio_notify_parent() instead, as are distributed
1728 * spares, which are leaves that issue children of their own.
1729 * The scheduler may change before a queue slot is actually
1730 * taken, so vdev_should_queue_io() here only keeps the batch
1731 * away from vdevs that can never use it; the binding decision
1732 * is vdev_queue_io()'s.
1733 */
1734 if (vd->vdev_ops->vdev_op_leaf &&
1735 vd->vdev_ops != &vdev_draid_spare_ops &&
1736 !vdev_should_queue_io(zio)) {
1737 /*
1738 * The batch is dispatched to the taskq chosen for
1739 * whichever member arrives last, so they all have to
1740 * choose the same one. The flags that steer the choice
1741 * are vdev-inherited, and type and priority come from
1742 * the parent at every call site.
1743 */
1744 ASSERT3U(zio->io_type, ==, pio->io_type);
1745 ASSERT3U(zio->io_priority, ==, pio->io_priority);
1746 zio_batch_join(pio->io_child_batch, zio);
1747 }
1748 }
1749
1750 return (zio);
1751 }
1752
1753 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)1754 zio_vdev_delegated_io(vdev_t *vd, uint64_t offset, abd_t *data, uint64_t size,
1755 zio_type_t type, zio_priority_t priority, zio_flag_t flags,
1756 zio_done_func_t *done, void *private)
1757 {
1758 zio_t *zio;
1759
1760 ASSERT(vd->vdev_ops->vdev_op_leaf);
1761
1762 zio = zio_create(NULL, vd->vdev_spa, 0, NULL,
1763 data, size, size, done, private, type, priority,
1764 flags | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY | ZIO_FLAG_DELEGATED,
1765 vd, offset, NULL,
1766 ZIO_STAGE_VDEV_IO_START >> 1, ZIO_VDEV_CHILD_PIPELINE);
1767
1768 return (zio);
1769 }
1770
1771
1772 /*
1773 * Send a flush command to the given vdev. Unlike most zio creation functions,
1774 * the flush zios are issued immediately. You can wait on pio to pause until
1775 * the flushes complete.
1776 */
1777 void
zio_flush(zio_t * pio,vdev_t * vd)1778 zio_flush(zio_t *pio, vdev_t *vd)
1779 {
1780 const zio_flag_t flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE |
1781 ZIO_FLAG_DONT_RETRY;
1782
1783 if (vd->vdev_nowritecache)
1784 return;
1785
1786 if (vd->vdev_children == 0) {
1787 /*
1788 * A non-concrete vdev (a hole or indirect vdev left behind
1789 * by removing a log or data device) has no leaf device to
1790 * flush. Skip it; issuing a flush to an indirect vdev would
1791 * trip the ZIO_TYPE_WRITE assertion in
1792 * vdev_indirect_io_start().
1793 */
1794 if (!vdev_is_concrete(vd))
1795 return;
1796 zio_nowait(zio_create(pio, vd->vdev_spa, 0, NULL, NULL, 0, 0,
1797 NULL, NULL, ZIO_TYPE_FLUSH, ZIO_PRIORITY_NOW, flags, vd, 0,
1798 NULL, ZIO_STAGE_OPEN, ZIO_FLUSH_PIPELINE));
1799 } else {
1800 for (uint64_t c = 0; c < vd->vdev_children; c++)
1801 zio_flush(pio, vd->vdev_child[c]);
1802 }
1803 }
1804
1805 void
zio_shrink(zio_t * zio,uint64_t size)1806 zio_shrink(zio_t *zio, uint64_t size)
1807 {
1808 ASSERT0P(zio->io_executor);
1809 ASSERT3U(zio->io_orig_size, ==, zio->io_size);
1810 ASSERT3U(size, <=, zio->io_size);
1811
1812 /*
1813 * We don't shrink for raidz because of problems with the
1814 * reconstruction when reading back less than the block size.
1815 * Note, BP_IS_RAIDZ() assumes no compression.
1816 */
1817 ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
1818 if (!BP_IS_RAIDZ(zio->io_bp)) {
1819 /* we are not doing a raw write */
1820 ASSERT3U(zio->io_size, ==, zio->io_lsize);
1821 zio->io_orig_size = zio->io_size = zio->io_lsize = size;
1822 }
1823 }
1824
1825 /*
1826 * Round provided allocation size up to a value that can be allocated
1827 * by at least some vdev(s) in the pool with minimum or no additional
1828 * padding and without extra space usage on others
1829 */
1830 static uint64_t
zio_roundup_alloc_size(spa_t * spa,uint64_t size)1831 zio_roundup_alloc_size(spa_t *spa, uint64_t size)
1832 {
1833 if (size > spa->spa_min_alloc)
1834 return (roundup(size, spa->spa_gcd_alloc));
1835 return (spa->spa_min_alloc);
1836 }
1837
1838 size_t
zio_get_compression_max_size(enum zio_compress compress,uint64_t gcd_alloc,uint64_t min_alloc,size_t s_len)1839 zio_get_compression_max_size(enum zio_compress compress, uint64_t gcd_alloc,
1840 uint64_t min_alloc, size_t s_len)
1841 {
1842 size_t d_len;
1843
1844 /* minimum 12.5% must be saved (legacy value, may be changed later) */
1845 d_len = s_len - (s_len >> 3);
1846
1847 /* ZLE can't use exactly d_len bytes, it needs more, so ignore it */
1848 if (compress == ZIO_COMPRESS_ZLE)
1849 return (d_len);
1850
1851 d_len = d_len - d_len % gcd_alloc;
1852
1853 if (d_len < min_alloc)
1854 return (BPE_PAYLOAD_SIZE);
1855 return (d_len);
1856 }
1857
1858 /*
1859 * ==========================================================================
1860 * Prepare to read and write logical blocks
1861 * ==========================================================================
1862 */
1863
1864 static zio_t *
zio_read_bp_init(zio_t * zio)1865 zio_read_bp_init(zio_t *zio)
1866 {
1867 blkptr_t *bp = zio->io_bp;
1868 uint64_t psize =
1869 BP_IS_EMBEDDED(bp) ? BPE_GET_PSIZE(bp) : BP_GET_PSIZE(bp);
1870
1871 ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy);
1872
1873 if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF &&
1874 zio->io_child_type == ZIO_CHILD_LOGICAL &&
1875 !(zio->io_flags & ZIO_FLAG_RAW_COMPRESS)) {
1876 zio_push_transform(zio, abd_alloc_sametype(zio->io_abd, psize),
1877 psize, psize, zio_decompress);
1878 }
1879
1880 if (((BP_IS_PROTECTED(bp) && !(zio->io_flags & ZIO_FLAG_RAW_ENCRYPT)) ||
1881 BP_HAS_INDIRECT_MAC_CKSUM(bp)) &&
1882 zio->io_child_type == ZIO_CHILD_LOGICAL) {
1883 zio_push_transform(zio, abd_alloc_sametype(zio->io_abd, psize),
1884 psize, psize, zio_decrypt);
1885 }
1886
1887 if (BP_IS_EMBEDDED(bp) && BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA) {
1888 int psize = BPE_GET_PSIZE(bp);
1889 void *data = abd_borrow_buf(zio->io_abd, psize);
1890
1891 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1892 decode_embedded_bp_compressed(bp, data);
1893 abd_return_buf_copy(zio->io_abd, data, psize);
1894 } else {
1895 ASSERT(!BP_IS_EMBEDDED(bp));
1896 }
1897
1898 if (BP_GET_DEDUP(bp) && zio->io_child_type == ZIO_CHILD_LOGICAL)
1899 zio->io_pipeline = ZIO_DDT_READ_PIPELINE;
1900
1901 return (zio);
1902 }
1903
1904 static zio_t *
zio_write_bp_init(zio_t * zio)1905 zio_write_bp_init(zio_t *zio)
1906 {
1907 if (!IO_IS_ALLOCATING(zio))
1908 return (zio);
1909
1910 ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
1911
1912 if (zio->io_bp_override) {
1913 blkptr_t *bp = zio->io_bp;
1914 zio_prop_t *zp = &zio->io_prop;
1915
1916 ASSERT(BP_GET_BIRTH(bp) != zio->io_txg);
1917
1918 *bp = *zio->io_bp_override;
1919 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1920
1921 if (zp->zp_brtwrite)
1922 return (zio);
1923
1924 ASSERT(!BP_GET_DEDUP(zio->io_bp_override));
1925
1926 if (BP_IS_EMBEDDED(bp))
1927 return (zio);
1928
1929 /*
1930 * If we've been overridden and nopwrite is set then
1931 * set the flag accordingly to indicate that a nopwrite
1932 * has already occurred.
1933 */
1934 if (!BP_IS_HOLE(bp) && zp->zp_nopwrite) {
1935 ASSERT(!zp->zp_dedup);
1936 ASSERT3U(BP_GET_CHECKSUM(bp), ==, zp->zp_checksum);
1937 zio->io_flags |= ZIO_FLAG_NOPWRITE;
1938 return (zio);
1939 }
1940
1941 ASSERT(!zp->zp_nopwrite);
1942
1943 if (BP_IS_HOLE(bp) || !zp->zp_dedup)
1944 return (zio);
1945
1946 ASSERT((zio_checksum_table[zp->zp_checksum].ci_flags &
1947 ZCHECKSUM_FLAG_DEDUP) || zp->zp_dedup_verify);
1948
1949 if (BP_GET_CHECKSUM(bp) == zp->zp_checksum &&
1950 !zp->zp_encrypt) {
1951 BP_SET_DEDUP(bp, 1);
1952 zio->io_pipeline |= ZIO_STAGE_DDT_WRITE;
1953 return (zio);
1954 }
1955
1956 /*
1957 * We were unable to handle this as an override bp, treat
1958 * it as a regular write I/O.
1959 */
1960 zio->io_bp_override = NULL;
1961 *bp = zio->io_bp_orig;
1962 zio->io_pipeline = zio->io_orig_pipeline;
1963 }
1964
1965 return (zio);
1966 }
1967
1968 static zio_t *
zio_write_compress(zio_t * zio)1969 zio_write_compress(zio_t *zio)
1970 {
1971 spa_t *spa = zio->io_spa;
1972 zio_prop_t *zp = &zio->io_prop;
1973 enum zio_compress compress = zp->zp_compress;
1974 blkptr_t *bp = zio->io_bp;
1975 uint64_t lsize = zio->io_lsize;
1976 uint64_t psize = zio->io_size;
1977 uint32_t pass = 1;
1978
1979 /*
1980 * If our children haven't all reached the ready stage,
1981 * wait for them and then repeat this pipeline stage.
1982 */
1983 if (zio_wait_for_children(zio, ZIO_CHILD_LOGICAL_BIT |
1984 ZIO_CHILD_GANG_BIT, ZIO_WAIT_READY)) {
1985 return (NULL);
1986 }
1987
1988 if (!IO_IS_ALLOCATING(zio))
1989 return (zio);
1990
1991 if (zio->io_children_ready != NULL) {
1992 /*
1993 * Now that all our children are ready, run the callback
1994 * associated with this zio in case it wants to modify the
1995 * data to be written.
1996 */
1997 ASSERT3U(zp->zp_level, >, 0);
1998 zio->io_children_ready(zio);
1999 }
2000
2001 ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
2002 ASSERT0P(zio->io_bp_override);
2003
2004 if (!BP_IS_HOLE(bp) && BP_GET_BIRTH(bp) == zio->io_txg) {
2005 /*
2006 * We're rewriting an existing block, which means we're
2007 * working on behalf of spa_sync(). For spa_sync() to
2008 * converge, it must eventually be the case that we don't
2009 * have to allocate new blocks. But compression changes
2010 * the blocksize, which forces a reallocate, and makes
2011 * convergence take longer. Therefore, after the first
2012 * few passes, stop compressing to ensure convergence.
2013 */
2014 pass = spa_sync_pass(spa);
2015
2016 ASSERT(zio->io_txg == spa_syncing_txg(spa));
2017 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2018 ASSERT(!BP_GET_DEDUP(bp));
2019
2020 if (pass >= zfs_sync_pass_dont_compress)
2021 compress = ZIO_COMPRESS_OFF;
2022
2023 /* Make sure someone doesn't change their mind on overwrites */
2024 ASSERT(BP_IS_EMBEDDED(bp) || BP_IS_GANG(bp) ||
2025 MIN(zp->zp_copies, spa_max_replication(spa))
2026 == BP_GET_NDVAS(bp));
2027 }
2028
2029 /* If it's a compressed write that is not raw, compress the buffer. */
2030 if (compress != ZIO_COMPRESS_OFF &&
2031 !(zio->io_flags & ZIO_FLAG_RAW_COMPRESS)) {
2032 abd_t *cabd = NULL;
2033 if (abd_cmp_zero(zio->io_abd, lsize) == 0)
2034 psize = 0;
2035 else if (compress == ZIO_COMPRESS_EMPTY)
2036 psize = lsize;
2037 else
2038 psize = zio_compress_data(compress, zio->io_abd, &cabd,
2039 lsize,
2040 zio_get_compression_max_size(compress,
2041 spa->spa_gcd_alloc, spa->spa_min_alloc, lsize),
2042 zp->zp_complevel);
2043 if (psize == 0) {
2044 compress = ZIO_COMPRESS_OFF;
2045 } else if (psize >= lsize) {
2046 compress = ZIO_COMPRESS_OFF;
2047 if (cabd != NULL)
2048 abd_free(cabd);
2049 } else if (psize <= BPE_PAYLOAD_SIZE && !zp->zp_encrypt &&
2050 zp->zp_level == 0 && !DMU_OT_HAS_FILL(zp->zp_type) &&
2051 spa_feature_is_enabled(spa, SPA_FEATURE_EMBEDDED_DATA)) {
2052 void *cbuf = abd_borrow_buf_copy(cabd, lsize);
2053 encode_embedded_bp_compressed(bp,
2054 cbuf, compress, lsize, psize);
2055 BPE_SET_ETYPE(bp, BP_EMBEDDED_TYPE_DATA);
2056 BP_SET_TYPE(bp, zio->io_prop.zp_type);
2057 BP_SET_LEVEL(bp, zio->io_prop.zp_level);
2058 abd_return_buf(cabd, cbuf, lsize);
2059 abd_free(cabd);
2060 BP_SET_LOGICAL_BIRTH(bp, zio->io_txg);
2061 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2062 ASSERT(spa_feature_is_active(spa,
2063 SPA_FEATURE_EMBEDDED_DATA));
2064 return (zio);
2065 } else {
2066 /*
2067 * Round compressed size up to the minimum allocation
2068 * size of the smallest-ashift device, and zero the
2069 * tail. This ensures that the compressed size of the
2070 * BP (and thus compressratio property) are correct,
2071 * in that we charge for the padding used to fill out
2072 * the last sector.
2073 */
2074 size_t rounded = (size_t)zio_roundup_alloc_size(spa,
2075 psize);
2076 if (rounded >= lsize) {
2077 compress = ZIO_COMPRESS_OFF;
2078 abd_free(cabd);
2079 psize = lsize;
2080 } else {
2081 abd_zero_off(cabd, psize, rounded - psize);
2082 psize = rounded;
2083 zio_push_transform(zio, cabd,
2084 psize, lsize, NULL);
2085 }
2086 }
2087
2088 /*
2089 * We were unable to handle this as an override bp, treat
2090 * it as a regular write I/O.
2091 */
2092 zio->io_bp_override = NULL;
2093 *bp = zio->io_bp_orig;
2094 zio->io_pipeline = zio->io_orig_pipeline;
2095
2096 } else if ((zio->io_flags & ZIO_FLAG_RAW_ENCRYPT) != 0 &&
2097 zp->zp_type == DMU_OT_DNODE) {
2098 /*
2099 * The DMU actually relies on the zio layer's compression
2100 * to free metadnode blocks that have had all contained
2101 * dnodes freed. As a result, even when doing a raw
2102 * receive, we must check whether the block can be compressed
2103 * to a hole.
2104 */
2105 if (abd_cmp_zero(zio->io_abd, lsize) == 0) {
2106 psize = 0;
2107 compress = ZIO_COMPRESS_OFF;
2108 } else {
2109 psize = lsize;
2110 }
2111 } else if (zio->io_flags & ZIO_FLAG_RAW_COMPRESS &&
2112 !(zio->io_flags & ZIO_FLAG_RAW_ENCRYPT)) {
2113 /*
2114 * If we are raw receiving an encrypted dataset we should not
2115 * take this codepath because it will change the on-disk block
2116 * and decryption will fail.
2117 */
2118 size_t rounded = MIN((size_t)zio_roundup_alloc_size(spa, psize),
2119 lsize);
2120
2121 if (rounded != psize) {
2122 abd_t *cdata = abd_alloc_linear(rounded, B_TRUE);
2123 abd_zero_off(cdata, psize, rounded - psize);
2124 abd_copy_off(cdata, zio->io_abd, 0, 0, psize);
2125 psize = rounded;
2126 zio_push_transform(zio, cdata,
2127 psize, rounded, NULL);
2128 }
2129 } else {
2130 ASSERT3U(psize, !=, 0);
2131 }
2132
2133 /*
2134 * The final pass of spa_sync() must be all rewrites, but the first
2135 * few passes offer a trade-off: allocating blocks defers convergence,
2136 * but newly allocated blocks are sequential, so they can be written
2137 * to disk faster. Therefore, we allow the first few passes of
2138 * spa_sync() to allocate new blocks, but force rewrites after that.
2139 * There should only be a handful of blocks after pass 1 in any case.
2140 */
2141 if (!BP_IS_HOLE(bp) && BP_GET_BIRTH(bp) == zio->io_txg &&
2142 BP_GET_PSIZE(bp) == psize &&
2143 pass >= zfs_sync_pass_rewrite) {
2144 VERIFY3U(psize, !=, 0);
2145 enum zio_stage gang_stages = zio->io_pipeline & ZIO_GANG_STAGES;
2146
2147 zio->io_pipeline = ZIO_REWRITE_PIPELINE | gang_stages;
2148 zio->io_flags |= ZIO_FLAG_IO_REWRITE;
2149 } else {
2150 BP_ZERO(bp);
2151 zio->io_pipeline = ZIO_WRITE_PIPELINE;
2152 }
2153
2154 if (psize == 0) {
2155 if (BP_GET_LOGICAL_BIRTH(&zio->io_bp_orig) != 0 &&
2156 spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) {
2157 BP_SET_LSIZE(bp, lsize);
2158 BP_SET_TYPE(bp, zp->zp_type);
2159 BP_SET_LEVEL(bp, zp->zp_level);
2160 BP_SET_BIRTH(bp, zio->io_txg, 0);
2161 }
2162 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2163 } else {
2164 ASSERT(zp->zp_checksum != ZIO_CHECKSUM_GANG_HEADER);
2165 BP_SET_LSIZE(bp, lsize);
2166 BP_SET_TYPE(bp, zp->zp_type);
2167 BP_SET_LEVEL(bp, zp->zp_level);
2168 BP_SET_PSIZE(bp, psize);
2169 BP_SET_COMPRESS(bp, compress);
2170 BP_SET_CHECKSUM(bp, zp->zp_checksum);
2171 BP_SET_DEDUP(bp, zp->zp_dedup);
2172 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
2173 if (zp->zp_dedup) {
2174 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2175 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
2176 ASSERT(!zp->zp_encrypt ||
2177 DMU_OT_IS_ENCRYPTED(zp->zp_type));
2178 zio->io_pipeline = ZIO_DDT_WRITE_PIPELINE;
2179 }
2180 if (zp->zp_nopwrite) {
2181 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2182 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
2183 zio->io_pipeline |= ZIO_STAGE_NOP_WRITE;
2184 }
2185 }
2186 return (zio);
2187 }
2188
2189 static zio_t *
zio_free_bp_init(zio_t * zio)2190 zio_free_bp_init(zio_t *zio)
2191 {
2192 blkptr_t *bp = zio->io_bp;
2193
2194 if (zio->io_child_type == ZIO_CHILD_LOGICAL) {
2195 if (BP_GET_DEDUP(bp))
2196 /*
2197 * Keep the gang stages zio_create() added: if
2198 * zio_ddt_free() falls back to a plain free, they
2199 * free the gang members along with the header.
2200 */
2201 zio->io_pipeline |= ZIO_DDT_FREE_PIPELINE;
2202 }
2203
2204 ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy);
2205
2206 return (zio);
2207 }
2208
2209 /*
2210 * ==========================================================================
2211 * Execute the I/O pipeline
2212 * ==========================================================================
2213 */
2214
2215 static void
zio_taskq_dispatch_func(zio_t * zio,zio_taskq_type_t q,boolean_t cutinline,task_func_t * func)2216 zio_taskq_dispatch_func(zio_t *zio, zio_taskq_type_t q, boolean_t cutinline,
2217 task_func_t *func)
2218 {
2219 spa_t *spa = zio->io_spa;
2220 zio_type_t t = zio->io_type;
2221
2222 /*
2223 * If we're a config writer or a probe, the normal issue and
2224 * interrupt threads may all be blocked waiting for the config lock.
2225 * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL.
2226 */
2227 if (zio->io_flags & (ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_PROBE))
2228 t = ZIO_TYPE_NULL;
2229
2230 /*
2231 * A similar issue exists for the L2ARC write thread until L2ARC 2.0.
2232 */
2233 if (t == ZIO_TYPE_WRITE && zio->io_vd && zio->io_vd->vdev_aux)
2234 t = ZIO_TYPE_NULL;
2235
2236 /*
2237 * If this is a high priority I/O, then use the high priority taskq if
2238 * available or cut the line otherwise.
2239 */
2240 if (zio->io_priority == ZIO_PRIORITY_SYNC_WRITE) {
2241 if (spa->spa_zio_taskq[t][q + 1].stqs_count != 0)
2242 q++;
2243 else
2244 cutinline = B_TRUE;
2245 }
2246
2247 ASSERT3U(q, <, ZIO_TASKQ_TYPES);
2248
2249 spa_taskq_dispatch(spa, t, q, func, zio, cutinline);
2250 }
2251
2252 static void
zio_taskq_dispatch(zio_t * zio,zio_taskq_type_t q,boolean_t cutinline)2253 zio_taskq_dispatch(zio_t *zio, zio_taskq_type_t q, boolean_t cutinline)
2254 {
2255 zio_taskq_dispatch_func(zio, q, cutinline, zio_execute);
2256 }
2257
2258 static boolean_t
zio_taskq_member(zio_t * zio,zio_taskq_type_t q)2259 zio_taskq_member(zio_t *zio, zio_taskq_type_t q)
2260 {
2261 spa_t *spa = zio->io_spa;
2262
2263 taskq_t *tq = taskq_of_curthread();
2264
2265 for (zio_type_t t = 0; t < ZIO_TYPES; t++) {
2266 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
2267 uint_t i;
2268 for (i = 0; i < tqs->stqs_count; i++) {
2269 if (tqs->stqs_taskq[i] == tq)
2270 return (B_TRUE);
2271 }
2272 }
2273
2274 return (B_FALSE);
2275 }
2276
2277 static zio_t *
zio_issue_async(zio_t * zio)2278 zio_issue_async(zio_t *zio)
2279 {
2280 ASSERT((zio->io_type != ZIO_TYPE_WRITE) || ZIO_HAS_ALLOCATOR(zio));
2281
2282 /* Whatever may execute this again, it won't be this thread. */
2283 zio->io_pipeline &= ~ZIO_STAGE_ISSUE_ASYNC;
2284
2285 /*
2286 * A zio whose children are not ready yet, such as an indirect block
2287 * write, has nothing to do in WRITE_COMPRESS but wait for them, so a
2288 * thread dispatched for it would only block. Do that wait here and let
2289 * whoever wakes it up carry on, since that is not this thread anymore.
2290 */
2291 if ((zio->io_pipeline & ZIO_STAGE_WRITE_COMPRESS) &&
2292 zio_wait_for_children(zio, ZIO_CHILD_LOGICAL_BIT |
2293 ZIO_CHILD_GANG_BIT, ZIO_WAIT_READY))
2294 return (NULL);
2295
2296 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
2297 return (NULL);
2298 }
2299
2300 /*
2301 * ==========================================================================
2302 * Completion batching
2303 * ==========================================================================
2304 *
2305 * A vdev child's entire life after the block layer returns is three pipeline
2306 * stages: VDEV_IO_DONE, VDEV_IO_ASSESS and DONE (ZIO_VDEV_CHILD_PIPELINE).
2307 * For a parent with many children, such as RAIDZ or a mirror, every child but
2308 * the last does nothing there except decrement the parent's child count, yet
2309 * each one costs a taskq dispatch and a context switch to get there.
2310 *
2311 * A batch collects the children of one parent as they return, and once the last
2312 * of them is in, runs all of their completions, and then the parent's, on one
2313 * thread. Arrival happens in the block layer completion context, so it is
2314 * lock-free: bio_endio() on Linux can run in softirq, where the sleepable
2315 * mutex_t is not usable.
2316 *
2317 * Only children that actually arrive from the block layer join zb_arrived; one
2318 * that reaches its completion on a pipeline thread instead just releases its
2319 * hold and runs that completion itself, as it would have without any of this.
2320 * Building the list on arrival is what allows that, since such a child may run
2321 * all the way to zio_destroy() long before the batch does.
2322 *
2323 * A child that occupies a vdev queue slot must never be a member. The slot is
2324 * released by vdev_queue_io_done(), part of the deferred completion, while a
2325 * sibling may still be queued for a slot on another vdev whose slots are in
2326 * turn held by the members of other waiting batches -- a cycle that deadlocks.
2327 */
2328 static int zio_batch_enabled = 1;
2329
2330 /*
2331 * Open a batch collecting the completions of the vdev children this zio is
2332 * about to create, which do little but count down to it. Every one of those
2333 * children must be created before the matching zio_batch_rele().
2334 */
2335 void
zio_batch_create(zio_t * pio)2336 zio_batch_create(zio_t *pio)
2337 {
2338 zio_batch_t *zb;
2339
2340 ASSERT3P(pio->io_child_batch, ==, NULL);
2341
2342 if (!zio_batch_enabled)
2343 return;
2344
2345 zb = kmem_alloc(sizeof (*zb), KM_SLEEP);
2346 zb->zb_arrived = NULL;
2347 zb->zb_holds = 1; /* creator's hold */
2348 pio->io_child_batch = zb;
2349 }
2350
2351 /*
2352 * Free the batch and return the list of members that arrived on it, for the
2353 * caller to execute. Members arrive by prepending, and are equal peers of one
2354 * parent, so their order should not matter; the list is reversed into
2355 * completion order only because it is walked here anyway. Membership is
2356 * dropped in that walk, both because VDEV_IO_ASSESS may reissue a member, which
2357 * must not rejoin, and so that a member's later zio_batch_leave() does not
2358 * touch the batch once it is freed.
2359 */
2360 static zio_t *
zio_batch_run(zio_batch_t * zb)2361 zio_batch_run(zio_batch_t *zb)
2362 {
2363 zio_t *list = NULL, *zio, *next;
2364
2365 for (zio = zb->zb_arrived; zio != NULL; zio = next) {
2366 next = zio->io_exec_next;
2367 zio->io_batch = NULL;
2368 zio->io_exec_next = list;
2369 list = zio;
2370 }
2371
2372 kmem_free(zb, sizeof (*zb));
2373
2374 return (list);
2375 }
2376
2377 static void
zio_batch_execute(void * arg)2378 zio_batch_execute(void *arg)
2379 {
2380 zio_execute(zio_batch_run(((zio_t *)arg)->io_batch));
2381 }
2382
2383 static void
zio_batch_join(zio_batch_t * zb,zio_t * zio)2384 zio_batch_join(zio_batch_t *zb, zio_t *zio)
2385 {
2386 ASSERT3P(zio->io_batch, ==, NULL);
2387 atomic_inc_64(&zb->zb_holds);
2388 zio->io_batch = zb;
2389 }
2390
2391 /*
2392 * Close the batch, once all of its members have been created, dropping the hold
2393 * that kept it from running while they were still being created. Callers do
2394 * this before advancing the parent into VDEV_IO_DONE, where it will wait for
2395 * them; the members go ahead of it in the list, which is harmless, since all
2396 * they do there is decrement its child count. io_child_batch is cleared, so
2397 * that children created later, such as the repair writes from
2398 * vdev_raidz_io_done(), do not join a batch that is already gone. Returns the
2399 * parent, preceded by any members that arrived while it was still creating
2400 * them, for the caller to execute.
2401 */
2402 zio_t *
zio_batch_rele(zio_t * pio)2403 zio_batch_rele(zio_t *pio)
2404 {
2405 zio_batch_t *zb = pio->io_child_batch;
2406 zio_t *list, *last;
2407
2408 ASSERT3P(pio->io_exec_next, ==, NULL);
2409
2410 if (zb == NULL)
2411 return (pio);
2412
2413 pio->io_child_batch = NULL;
2414 if (atomic_dec_64_nv(&zb->zb_holds) != 0)
2415 return (pio);
2416
2417 if ((list = zio_batch_run(zb)) == NULL)
2418 return (pio);
2419
2420 last = list;
2421 while (last->io_exec_next != NULL)
2422 last = last->io_exec_next;
2423 last->io_exec_next = pio;
2424 return (list);
2425 }
2426
2427 /*
2428 * Called in place of a member's taskq dispatch, from the block layer
2429 * completion context. Returns B_TRUE if the zio was absorbed by a batch, in
2430 * which case the caller must not touch it again.
2431 */
2432 static boolean_t
zio_batch_arrive(zio_t * zio)2433 zio_batch_arrive(zio_t *zio)
2434 {
2435 zio_batch_t *zb = zio->io_batch;
2436 zio_t *head;
2437
2438 if (zb == NULL)
2439 return (B_FALSE);
2440
2441 /*
2442 * The completion is deferred, so take the service time here, while it
2443 * still is one: vdev_child_slow_outlier() sits out RAIDZ children based
2444 * on io_delta and io_delay. A non-zero io_delta also tells the stages
2445 * below when the block layer returned, as io_timestamp + io_delta.
2446 */
2447 ASSERT3U(zio->io_timestamp, !=, 0);
2448 zio->io_delta = gethrtime() - zio->io_timestamp;
2449
2450 do {
2451 head = zb->zb_arrived;
2452 zio->io_exec_next = head;
2453 } while (atomic_cas_ptr(&zb->zb_arrived, head, zio) != head);
2454
2455 if (atomic_dec_64_nv(&zb->zb_holds) == 0) {
2456 zio_taskq_dispatch_func(zio, ZIO_TASKQ_INTERRUPT, B_FALSE,
2457 zio_batch_execute);
2458 }
2459 return (B_TRUE);
2460 }
2461
2462 /*
2463 * Give up a membership, either because the zio is about to take a vdev queue
2464 * slot after all, or because it reached its completion on a pipeline thread
2465 * rather than from the block layer, and so will run that completion itself.
2466 * Clearing io_batch makes this idempotent. Returns the members for the caller
2467 * to execute if this was the last hold on the batch, and NULL otherwise.
2468 */
2469 zio_t *
zio_batch_leave(zio_t * zio)2470 zio_batch_leave(zio_t *zio)
2471 {
2472 zio_batch_t *zb = zio->io_batch;
2473
2474 if (likely(zb == NULL))
2475 return (NULL);
2476
2477 zio->io_batch = NULL;
2478 if (atomic_dec_64_nv(&zb->zb_holds) != 0)
2479 return (NULL);
2480
2481 return (zio_batch_run(zb));
2482 }
2483
2484 void
zio_interrupt(void * zio)2485 zio_interrupt(void *zio)
2486 {
2487 if (zio_batch_arrive(zio))
2488 return;
2489 zio_taskq_dispatch(zio, ZIO_TASKQ_INTERRUPT, B_FALSE);
2490 }
2491
2492 void
zio_delay_interrupt(zio_t * zio)2493 zio_delay_interrupt(zio_t *zio)
2494 {
2495 /*
2496 * The timeout_generic() function isn't defined in userspace, so
2497 * rather than trying to implement the function, the zio delay
2498 * functionality has been disabled for userspace builds.
2499 */
2500
2501 #ifdef _KERNEL
2502 /*
2503 * If io_target_timestamp is zero, then no delay has been registered
2504 * for this IO, thus jump to the end of this function and "skip" the
2505 * delay; issuing it directly to the zio layer.
2506 */
2507 if (zio->io_target_timestamp != 0) {
2508 hrtime_t now = gethrtime();
2509
2510 if (now >= zio->io_target_timestamp) {
2511 /*
2512 * This IO has already taken longer than the target
2513 * delay to complete, so we don't want to delay it
2514 * any longer; we "miss" the delay and issue it
2515 * directly to the zio layer. This is likely due to
2516 * the target latency being set to a value less than
2517 * the underlying hardware can satisfy (e.g. delay
2518 * set to 1ms, but the disks take 10ms to complete an
2519 * IO request).
2520 */
2521
2522 DTRACE_PROBE2(zio__delay__miss, zio_t *, zio,
2523 hrtime_t, now);
2524
2525 zio_interrupt(zio);
2526 } else {
2527 taskqid_t tid;
2528 hrtime_t diff = zio->io_target_timestamp - now;
2529 int ticks = MAX(1, NSEC_TO_TICK(diff));
2530 clock_t expire_at_tick = ddi_get_lbolt() + ticks;
2531
2532 DTRACE_PROBE3(zio__delay__hit, zio_t *, zio,
2533 hrtime_t, now, hrtime_t, diff);
2534
2535 tid = taskq_dispatch_delay(system_taskq, zio_interrupt,
2536 zio, TQ_NOSLEEP, expire_at_tick);
2537 if (tid == TASKQID_INVALID) {
2538 /*
2539 * Couldn't allocate a task. Just finish the
2540 * zio without a delay.
2541 */
2542 zio_interrupt(zio);
2543 }
2544 }
2545 return;
2546 }
2547 #endif
2548 DTRACE_PROBE1(zio__delay__skip, zio_t *, zio);
2549 zio_interrupt(zio);
2550 }
2551
2552 static void
zio_deadman_impl(zio_t * pio,int ziodepth)2553 zio_deadman_impl(zio_t *pio, int ziodepth)
2554 {
2555 zio_t *cio, *cio_next;
2556 zio_link_t *zl = NULL;
2557 vdev_t *vd = pio->io_vd;
2558 uint64_t failmode = spa_get_deadman_failmode(pio->io_spa);
2559
2560 if (zio_deadman_log_all || (vd != NULL && vd->vdev_ops->vdev_op_leaf)) {
2561 vdev_queue_t *vq = vd ? &vd->vdev_queue : NULL;
2562 zbookmark_phys_t *zb = &pio->io_bookmark;
2563 uint64_t delta = gethrtime() - pio->io_timestamp;
2564
2565 zfs_dbgmsg("slow zio[%d]: zio=%px timestamp=%llu "
2566 "delta=%llu queued=%llu io=%llu "
2567 "path=%s "
2568 "last=%llu type=%d "
2569 "priority=%d flags=0x%llx stage=0x%x "
2570 "pipeline=0x%x pipeline-trace=0x%x "
2571 "objset=%llu object=%llu "
2572 "level=%llu blkid=%llu "
2573 "offset=%llu size=%llu "
2574 "error=%d",
2575 ziodepth, pio, pio->io_timestamp,
2576 (u_longlong_t)delta, pio->io_delta, pio->io_delay,
2577 vd ? vd->vdev_path : "NULL",
2578 vq ? vq->vq_io_complete_ts : 0, pio->io_type,
2579 pio->io_priority, (u_longlong_t)pio->io_flags,
2580 pio->io_stage, pio->io_pipeline, pio->io_pipeline_trace,
2581 (u_longlong_t)zb->zb_objset, (u_longlong_t)zb->zb_object,
2582 (u_longlong_t)zb->zb_level, (u_longlong_t)zb->zb_blkid,
2583 (u_longlong_t)pio->io_offset, (u_longlong_t)pio->io_size,
2584 pio->io_error);
2585 (void) zfs_ereport_post(FM_EREPORT_ZFS_DEADMAN,
2586 pio->io_spa, vd, zb, pio, 0);
2587 }
2588
2589 if (vd != NULL && vd->vdev_ops->vdev_op_leaf &&
2590 list_is_empty(&pio->io_child_list) &&
2591 failmode == ZIO_FAILURE_MODE_CONTINUE &&
2592 taskq_empty_ent(&pio->io_tqent) &&
2593 pio->io_queue_state == ZIO_QS_ACTIVE) {
2594 pio->io_error = EINTR;
2595 zio_interrupt(pio);
2596 }
2597
2598 mutex_enter(&pio->io_lock);
2599 for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) {
2600 cio_next = zio_walk_children(pio, &zl);
2601 zio_deadman_impl(cio, ziodepth + 1);
2602 }
2603 mutex_exit(&pio->io_lock);
2604 }
2605
2606 /*
2607 * Log the critical information describing this zio and all of its children
2608 * using the zfs_dbgmsg() interface then post deadman event for the ZED.
2609 */
2610 void
zio_deadman(zio_t * pio,const char * tag)2611 zio_deadman(zio_t *pio, const char *tag)
2612 {
2613 spa_t *spa = pio->io_spa;
2614 char *name = spa_name(spa);
2615
2616 if (!zfs_deadman_enabled || spa_suspended(spa))
2617 return;
2618
2619 zio_deadman_impl(pio, 0);
2620
2621 switch (spa_get_deadman_failmode(spa)) {
2622 case ZIO_FAILURE_MODE_WAIT:
2623 zfs_dbgmsg("%s waiting for hung I/O to pool '%s'", tag, name);
2624 break;
2625
2626 case ZIO_FAILURE_MODE_CONTINUE:
2627 zfs_dbgmsg("%s restarting hung I/O for pool '%s'", tag, name);
2628 break;
2629
2630 case ZIO_FAILURE_MODE_PANIC:
2631 fm_panic("%s determined I/O to pool '%s' is hung.", tag, name);
2632 break;
2633 }
2634 }
2635
2636 /*
2637 * Execute the I/O pipeline until one of the following occurs:
2638 * (1) the I/O completes; (2) the pipeline stalls waiting for
2639 * dependent child I/Os; (3) the I/O issues, so we're waiting
2640 * for an I/O completion interrupt; (4) the I/O is delegated by
2641 * vdev-level caching or aggregation; (5) the I/O is deferred
2642 * due to vdev-level queueing; (6) the I/O is handed off to
2643 * another thread. In all cases, the pipeline stops whenever
2644 * there's no CPU work; it never burns a thread in cv_wait_io().
2645 *
2646 * There's no locking on io_stage because there's no legitimate way
2647 * for multiple threads to be attempting to process the same I/O.
2648 */
2649 static zio_pipe_stage_t *zio_pipeline[];
2650
2651 /*
2652 * zio_execute() is a wrapper around the static function
2653 * __zio_execute() so that we can force __zio_execute() to be
2654 * inlined. This reduces stack overhead which is important
2655 * because __zio_execute() is called recursively in several zio
2656 * code paths. zio_execute() itself cannot be inlined because
2657 * it is externally visible.
2658 */
2659 void
zio_execute(void * zio)2660 zio_execute(void *zio)
2661 {
2662 fstrans_cookie_t cookie;
2663
2664 cookie = spl_fstrans_mark();
2665 __zio_execute(zio);
2666 spl_fstrans_unmark(cookie);
2667 }
2668
2669 /*
2670 * Used to determine if in the current context the stack is sized large
2671 * enough to allow zio_execute() to be called recursively. A minimum
2672 * stack size of 16K is required to avoid needing to re-dispatch the zio.
2673 */
2674 static boolean_t
zio_execute_stack_check(zio_t * zio)2675 zio_execute_stack_check(zio_t *zio)
2676 {
2677 #if !defined(HAVE_LARGE_STACKS)
2678 dsl_pool_t *dp = spa_get_dsl(zio->io_spa);
2679
2680 /* Executing in txg_sync_thread() context. */
2681 if (dp && curthread == dp->dp_tx.tx_sync_thread)
2682 return (B_TRUE);
2683
2684 /* Pool initialization outside of zio_taskq context. */
2685 if (dp && spa_is_initializing(dp->dp_spa) &&
2686 !zio_taskq_member(zio, ZIO_TASKQ_ISSUE) &&
2687 !zio_taskq_member(zio, ZIO_TASKQ_ISSUE_HIGH))
2688 return (B_TRUE);
2689 #else
2690 (void) zio;
2691 #endif /* HAVE_LARGE_STACKS */
2692
2693 return (B_FALSE);
2694 }
2695
2696 /*
2697 * Run one pipeline stage, returning a list of zios to continue with, or NULL
2698 * if this thread is done with it.
2699 */
2700 __attribute__((always_inline))
2701 static inline zio_t *
zio_execute_stage(zio_t * zio)2702 zio_execute_stage(zio_t *zio)
2703 {
2704 enum zio_stage pipeline = zio->io_pipeline;
2705 enum zio_stage stage = zio->io_stage;
2706
2707 zio->io_executor = curthread;
2708
2709 ASSERT(!MUTEX_HELD(&zio->io_lock));
2710 ASSERT0P(zio->io_stall);
2711 ASSERT(ISP2(stage));
2712 ASSERT(pipeline & ~((stage << 1) - 1));
2713
2714 do {
2715 stage <<= 1;
2716 } while ((stage & pipeline) == 0);
2717
2718 ASSERT(stage <= ZIO_STAGE_DONE);
2719
2720 /*
2721 * If we are in interrupt context and this pipeline stage will grab
2722 * a config lock that is held across I/O, or may wait for an I/O that
2723 * needs an interrupt thread to complete, issue async to avoid deadlock.
2724 *
2725 * For VDEV_IO_START, we cut in line so that the io will be sent to
2726 * disk promptly.
2727 */
2728 if ((stage & ZIO_BLOCKING_STAGES) && zio->io_vd == NULL &&
2729 zio_taskq_member(zio, ZIO_TASKQ_INTERRUPT)) {
2730 boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ?
2731 zio_requeue_io_start_cut_in_line : B_FALSE;
2732 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut);
2733 return (NULL);
2734 }
2735
2736 /*
2737 * If the current context doesn't have large enough stacks
2738 * the zio must be issued asynchronously to prevent overflow.
2739 */
2740 if (zio_execute_stack_check(zio)) {
2741 boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ?
2742 zio_requeue_io_start_cut_in_line : B_FALSE;
2743 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut);
2744 return (NULL);
2745 }
2746
2747 zio->io_stage = stage;
2748 zio->io_pipeline_trace |= zio->io_stage;
2749
2750 /*
2751 * The zio pipeline stage returns the next zio to execute (typically
2752 * the same as this one), or NULL if we should stop. It may also
2753 * chain more zios to it for us to execute later.
2754 */
2755 return (zio_pipeline[highbit64(stage) - 1](zio));
2756 }
2757
2758 /*
2759 * Take all but the first of the zios a stage handed back off its head, and
2760 * prepend the rest to those already pending. Dispatch heavyweight ZIOs except
2761 * the last, so that they could run in parallel.
2762 */
2763 static inline void
zio_execute_defer(zio_t * zio,zio_t ** pendingp)2764 zio_execute_defer(zio_t *zio, zio_t **pendingp)
2765 {
2766 zio_t *list = NULL, **tailp = &list;
2767 zio_t *next;
2768
2769 for (zio_t *cur = zio->io_exec_next; cur != NULL; cur = next) {
2770 next = cur->io_exec_next;
2771 cur->io_exec_next = NULL;
2772 if ((next != NULL || *pendingp != NULL) &&
2773 !(cur->io_flags & ZIO_FLAG_LIGHTWEIGHT)) {
2774 zio_taskq_dispatch(cur,
2775 cur->io_stage < ZIO_STAGE_VDEV_IO_START ?
2776 ZIO_TASKQ_ISSUE : ZIO_TASKQ_INTERRUPT, B_FALSE);
2777 continue;
2778 }
2779 *tailp = cur;
2780 tailp = &cur->io_exec_next;
2781 }
2782
2783 *tailp = *pendingp;
2784 *pendingp = list;
2785 zio->io_exec_next = NULL;
2786 }
2787
2788 __attribute__((always_inline))
2789 static inline void
__zio_execute(zio_t * zio)2790 __zio_execute(zio_t *zio)
2791 {
2792 zio_t *pending = zio->io_exec_next;
2793 zio->io_exec_next = NULL;
2794
2795 for (;;) {
2796 zio_t *last = zio;
2797 while ((zio = zio_execute_stage(zio)) != NULL) {
2798 if (zio->io_exec_next != NULL)
2799 zio_execute_defer(zio, &pending);
2800
2801 /*
2802 * A heavyweight zio is dispatched if others are already
2803 * waiting for this thread to let them run in parallel.
2804 */
2805 if (zio != last && pending != NULL &&
2806 !(zio->io_flags & ZIO_FLAG_LIGHTWEIGHT)) {
2807 zio_taskq_dispatch(zio,
2808 zio->io_stage < ZIO_STAGE_VDEV_IO_START ?
2809 ZIO_TASKQ_ISSUE : ZIO_TASKQ_INTERRUPT,
2810 B_FALSE);
2811 break;
2812 }
2813 last = zio;
2814 }
2815
2816 if ((zio = pending) == NULL)
2817 return;
2818 pending = zio->io_exec_next;
2819 zio->io_exec_next = NULL;
2820 }
2821 }
2822
2823
2824 /*
2825 * ==========================================================================
2826 * Initiate I/O, either sync or async
2827 * ==========================================================================
2828 */
2829 int
zio_wait(zio_t * zio)2830 zio_wait(zio_t *zio)
2831 {
2832 /*
2833 * Some routines, like zio_free_sync(), may return a NULL zio
2834 * to avoid the performance overhead of creating and then destroying
2835 * an unneeded zio. For the callers' simplicity, we accept a NULL
2836 * zio and ignore it.
2837 */
2838 if (zio == NULL)
2839 return (0);
2840
2841 long timeout = MSEC_TO_TICK(zfs_deadman_ziotime_ms);
2842 int error;
2843
2844 ASSERT3S(zio->io_stage, ==, ZIO_STAGE_OPEN);
2845 ASSERT0P(zio->io_executor);
2846
2847 zio->io_waiter = curthread;
2848 ASSERT0(zio->io_queued_timestamp);
2849 zio->io_queued_timestamp = gethrtime();
2850
2851 if (zio->io_type == ZIO_TYPE_WRITE) {
2852 spa_select_allocator(zio);
2853 }
2854 __zio_execute(zio);
2855
2856 mutex_enter(&zio->io_lock);
2857 while (zio->io_executor != NULL) {
2858 error = cv_timedwait_io(&zio->io_cv, &zio->io_lock,
2859 ddi_get_lbolt() + timeout);
2860
2861 if (zfs_deadman_enabled && error == -1 &&
2862 gethrtime() - zio->io_queued_timestamp >
2863 spa_deadman_ziotime(zio->io_spa)) {
2864 mutex_exit(&zio->io_lock);
2865 timeout = MSEC_TO_TICK(zfs_deadman_checktime_ms);
2866 zio_deadman(zio, FTAG);
2867 mutex_enter(&zio->io_lock);
2868 }
2869 }
2870 mutex_exit(&zio->io_lock);
2871
2872 error = zio->io_error;
2873 zio_destroy(zio);
2874
2875 return (error);
2876 }
2877
2878 void
zio_nowait(zio_t * zio)2879 zio_nowait(zio_t *zio)
2880 {
2881 /*
2882 * See comment in zio_wait().
2883 */
2884 if (zio == NULL)
2885 return;
2886
2887 ASSERT0P(zio->io_executor);
2888
2889 if (zio->io_child_type == ZIO_CHILD_LOGICAL &&
2890 list_is_empty(&zio->io_parent_list)) {
2891 zio_t *pio;
2892
2893 /*
2894 * This is a logical async I/O with no parent to wait for it.
2895 * We add it to the spa_async_root_zio "Godfather" I/O which
2896 * will ensure they complete prior to unloading the pool.
2897 */
2898 spa_t *spa = zio->io_spa;
2899 pio = spa->spa_async_zio_root[CPU_SEQID_UNSTABLE];
2900
2901 zio_add_child(pio, zio);
2902 }
2903
2904 ASSERT0(zio->io_queued_timestamp);
2905 zio->io_queued_timestamp = gethrtime();
2906 if (zio->io_type == ZIO_TYPE_WRITE) {
2907 spa_select_allocator(zio);
2908 }
2909 __zio_execute(zio);
2910 }
2911
2912 /*
2913 * ==========================================================================
2914 * Reexecute, cancel, or suspend/resume failed I/O
2915 * ==========================================================================
2916 */
2917
2918 static void
zio_reexecute(void * arg)2919 zio_reexecute(void *arg)
2920 {
2921 zio_t *pio = arg;
2922 zio_t *cio, *cio_next, *gio;
2923
2924 ASSERT(pio->io_child_type == ZIO_CHILD_LOGICAL);
2925 ASSERT(pio->io_orig_stage == ZIO_STAGE_OPEN);
2926 ASSERT0P(pio->io_gang_leader);
2927 ASSERT0P(pio->io_gang_tree);
2928
2929 mutex_enter(&pio->io_lock);
2930 pio->io_flags = pio->io_orig_flags;
2931 pio->io_stage = pio->io_orig_stage;
2932 pio->io_pipeline = pio->io_orig_pipeline;
2933 pio->io_post = 0;
2934 pio->io_flags |= ZIO_FLAG_REEXECUTED;
2935 pio->io_pipeline_trace = 0;
2936 pio->io_error = 0;
2937 pio->io_state[ZIO_WAIT_READY] = (pio->io_stage >= ZIO_STAGE_READY) ||
2938 (pio->io_pipeline & ZIO_STAGE_READY) == 0;
2939 pio->io_state[ZIO_WAIT_DONE] = (pio->io_stage >= ZIO_STAGE_DONE);
2940
2941 /*
2942 * It's possible for a failed ZIO to be a descendant of more than one
2943 * ZIO tree. When reexecuting it, we have to be sure to add its wait
2944 * states to all parent wait counts.
2945 *
2946 * Those parents, in turn, may have other children that are currently
2947 * active, usually because they've already been reexecuted after
2948 * resuming. Those children may be executing and may call
2949 * zio_notify_parent() at the same time as we're updating our parent's
2950 * counts. To avoid races while updating the counts, we take
2951 * gio->io_lock before each update.
2952 */
2953 zio_link_t *zl = NULL;
2954 while ((gio = zio_walk_parents(pio, &zl)) != NULL) {
2955 mutex_enter(&gio->io_lock);
2956 for (int w = 0; w < ZIO_WAIT_TYPES; w++) {
2957 gio->io_children[pio->io_child_type][w] +=
2958 !pio->io_state[w];
2959 }
2960 mutex_exit(&gio->io_lock);
2961 }
2962
2963 for (int c = 0; c < ZIO_CHILD_TYPES; c++)
2964 pio->io_child_error[c] = 0;
2965
2966 if (IO_IS_ALLOCATING(pio))
2967 BP_ZERO(pio->io_bp);
2968
2969 /*
2970 * As we reexecute pio's children, new children could be created.
2971 * New children go to the head of pio's io_child_list, however,
2972 * so we will (correctly) not reexecute them. The key is that
2973 * the remainder of pio's io_child_list, from 'cio_next' onward,
2974 * cannot be affected by any side effects of reexecuting 'cio'.
2975 */
2976 zl = NULL;
2977 for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) {
2978 cio_next = zio_walk_children(pio, &zl);
2979 mutex_exit(&pio->io_lock);
2980 zio_reexecute(cio);
2981 mutex_enter(&pio->io_lock);
2982 }
2983 mutex_exit(&pio->io_lock);
2984
2985 /*
2986 * Now that all children have been reexecuted, execute the parent.
2987 * We don't reexecute "The Godfather" I/O here as it's the
2988 * responsibility of the caller to wait on it.
2989 */
2990 if (!(pio->io_flags & ZIO_FLAG_GODFATHER)) {
2991 pio->io_queued_timestamp = gethrtime();
2992 __zio_execute(pio);
2993 }
2994 }
2995
2996 void
zio_suspend(spa_t * spa,zio_t * zio,zio_suspend_reason_t reason)2997 zio_suspend(spa_t *spa, zio_t *zio, zio_suspend_reason_t reason)
2998 {
2999 if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_PANIC)
3000 fm_panic("Pool '%s' has encountered an uncorrectable I/O "
3001 "failure and the failure mode property for this pool "
3002 "is set to panic.", spa_name(spa));
3003
3004 if (reason != ZIO_SUSPEND_MMP) {
3005 cmn_err(CE_WARN, "Pool '%s' has encountered an uncorrectable "
3006 "I/O failure and has been suspended.", spa_name(spa));
3007 }
3008
3009 (void) zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE, spa, NULL,
3010 NULL, NULL, 0);
3011
3012 mutex_enter(&spa->spa_suspend_lock);
3013
3014 if (spa->spa_suspend_zio_root == NULL)
3015 spa->spa_suspend_zio_root = zio_root(spa, NULL, NULL,
3016 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
3017 ZIO_FLAG_GODFATHER);
3018
3019 spa->spa_suspended = reason;
3020
3021 if (zio != NULL) {
3022 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
3023 ASSERT(zio != spa->spa_suspend_zio_root);
3024 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3025 ASSERT0P(zio_unique_parent(zio));
3026 ASSERT(zio->io_stage == ZIO_STAGE_DONE);
3027 zio_add_child(spa->spa_suspend_zio_root, zio);
3028 }
3029
3030 mutex_exit(&spa->spa_suspend_lock);
3031
3032 txg_wait_kick(spa->spa_dsl_pool);
3033 }
3034
3035 int
zio_resume(spa_t * spa)3036 zio_resume(spa_t *spa)
3037 {
3038 zio_t *pio;
3039
3040 /*
3041 * Reexecute all previously suspended i/o.
3042 */
3043 mutex_enter(&spa->spa_suspend_lock);
3044 if (spa->spa_suspended != ZIO_SUSPEND_NONE)
3045 cmn_err(CE_WARN, "Pool '%s' was suspended and is being "
3046 "resumed. Failed I/O will be retried.",
3047 spa_name(spa));
3048 spa->spa_suspended = ZIO_SUSPEND_NONE;
3049 cv_broadcast(&spa->spa_suspend_cv);
3050 pio = spa->spa_suspend_zio_root;
3051 spa->spa_suspend_zio_root = NULL;
3052 mutex_exit(&spa->spa_suspend_lock);
3053
3054 if (pio == NULL)
3055 return (0);
3056
3057 zio_reexecute(pio);
3058 return (zio_wait(pio));
3059 }
3060
3061 void
zio_resume_wait(spa_t * spa)3062 zio_resume_wait(spa_t *spa)
3063 {
3064 mutex_enter(&spa->spa_suspend_lock);
3065 while (spa_suspended(spa))
3066 cv_wait(&spa->spa_suspend_cv, &spa->spa_suspend_lock);
3067 mutex_exit(&spa->spa_suspend_lock);
3068 }
3069
3070 /*
3071 * ==========================================================================
3072 * Gang blocks.
3073 *
3074 * A gang block is a collection of small blocks that looks to the DMU
3075 * like one large block. When zio_dva_allocate() cannot find a block
3076 * of the requested size, due to either severe fragmentation or the pool
3077 * being nearly full, it calls zio_write_gang_block() to construct the
3078 * block from smaller fragments.
3079 *
3080 * A gang block consists of a a gang header and up to gbh_nblkptrs(size)
3081 * gang members. The gang header is like an indirect block: it's an array
3082 * of block pointers, though the header has a small tail (a zio_eck_t)
3083 * that stores an embedded checksum. It is allocated using only a single
3084 * sector as the requested size, and hence is allocatable regardless of
3085 * fragmentation. Its size is determined by the smallest allocatable
3086 * asize of the vdevs it was allocated on. The gang header's bps point
3087 * to its gang members, which hold the data.
3088 *
3089 * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg>
3090 * as the verifier to ensure uniqueness of the SHA256 checksum.
3091 * Critically, the gang block bp's blk_cksum is the checksum of the data,
3092 * not the gang header. This ensures that data block signatures (needed for
3093 * deduplication) are independent of how the block is physically stored.
3094 *
3095 * Gang blocks can be nested: a gang member may itself be a gang block.
3096 * Thus every gang block is a tree in which root and all interior nodes are
3097 * gang headers, and the leaves are normal blocks that contain user data.
3098 * The root of the gang tree is called the gang leader.
3099 *
3100 * To perform any operation (read, rewrite, free, claim) on a gang block,
3101 * zio_gang_assemble() first assembles the gang tree (minus data leaves)
3102 * in the io_gang_tree field of the original logical i/o by recursively
3103 * reading the gang leader and all gang headers below it. This yields
3104 * an in-core tree containing the contents of every gang header and the
3105 * bps for every constituent of the gang block.
3106 *
3107 * With the gang tree now assembled, zio_gang_issue() just walks the gang tree
3108 * and invokes a callback on each bp. To free a gang block, zio_gang_issue()
3109 * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp.
3110 * zio_claim_gang() provides a similarly trivial wrapper for zio_claim().
3111 * zio_read_gang() is a wrapper around zio_read() that omits reading gang
3112 * headers, since we already have those in io_gang_tree. zio_rewrite_gang()
3113 * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite()
3114 * of the gang header plus zio_checksum_compute() of the data to update the
3115 * gang header's blk_cksum as described above.
3116 *
3117 * The two-phase assemble/issue model solves the problem of partial failure --
3118 * what if you'd freed part of a gang block but then couldn't read the
3119 * gang header for another part? Assembling the entire gang tree first
3120 * ensures that all the necessary gang header I/O has succeeded before
3121 * starting the actual work of free, claim, or write. Once the gang tree
3122 * is assembled, free and claim are in-memory operations that cannot fail.
3123 *
3124 * In the event that a gang write fails, zio_dva_unallocate() walks the
3125 * gang tree to immediately free (i.e. insert back into the space map)
3126 * everything we've allocated. This ensures that we don't get ENOSPC
3127 * errors during repeated suspend/resume cycles due to a flaky device.
3128 *
3129 * Gang rewrites only happen during sync-to-convergence. If we can't assemble
3130 * the gang tree, we won't modify the block, so we can safely defer the free
3131 * (knowing that the block is still intact). If we *can* assemble the gang
3132 * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free
3133 * each constituent bp and we can allocate a new block on the next sync pass.
3134 *
3135 * In all cases, the gang tree allows complete recovery from partial failure.
3136 * ==========================================================================
3137 */
3138
3139 static void
zio_gang_issue_func_done(zio_t * zio)3140 zio_gang_issue_func_done(zio_t *zio)
3141 {
3142 abd_free(zio->io_abd);
3143 }
3144
3145 static zio_t *
zio_read_gang(zio_t * pio,blkptr_t * bp,zio_gang_node_t * gn,abd_t * data,uint64_t offset)3146 zio_read_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
3147 uint64_t offset)
3148 {
3149 if (gn != NULL)
3150 return (pio);
3151
3152 return (zio_read(pio, pio->io_spa, bp, abd_get_offset(data, offset),
3153 BP_GET_PSIZE(bp), zio_gang_issue_func_done,
3154 NULL, pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
3155 &pio->io_bookmark));
3156 }
3157
3158 static zio_t *
zio_rewrite_gang(zio_t * pio,blkptr_t * bp,zio_gang_node_t * gn,abd_t * data,uint64_t offset)3159 zio_rewrite_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
3160 uint64_t offset)
3161 {
3162 zio_t *zio;
3163
3164 if (gn != NULL) {
3165 abd_t *gbh_abd =
3166 abd_get_from_buf(gn->gn_gbh, gn->gn_gangblocksize);
3167 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
3168 gbh_abd, gn->gn_gangblocksize, zio_gang_issue_func_done,
3169 NULL, pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
3170 &pio->io_bookmark);
3171 /*
3172 * As we rewrite each gang header, the pipeline will compute
3173 * a new gang block header checksum for it; but no one will
3174 * compute a new data checksum, so we do that here. The one
3175 * exception is the gang leader: the pipeline already computed
3176 * its data checksum because that stage precedes gang assembly.
3177 * (Presently, nothing actually uses interior data checksums;
3178 * this is just good hygiene.)
3179 */
3180 if (gn != pio->io_gang_leader->io_gang_tree) {
3181 abd_t *buf = abd_get_offset(data, offset);
3182
3183 zio_checksum_compute(zio, BP_GET_CHECKSUM(bp),
3184 buf, BP_GET_PSIZE(bp));
3185
3186 abd_free(buf);
3187 }
3188 /*
3189 * If we are here to damage data for testing purposes,
3190 * leave the GBH alone so that we can detect the damage.
3191 */
3192 if (pio->io_gang_leader->io_flags & ZIO_FLAG_INDUCE_DAMAGE)
3193 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
3194 } else {
3195 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
3196 abd_get_offset(data, offset), BP_GET_PSIZE(bp),
3197 zio_gang_issue_func_done, NULL, pio->io_priority,
3198 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
3199 }
3200
3201 return (zio);
3202 }
3203
3204 static zio_t *
zio_free_gang(zio_t * pio,blkptr_t * bp,zio_gang_node_t * gn,abd_t * data,uint64_t offset)3205 zio_free_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
3206 uint64_t offset)
3207 {
3208 (void) gn, (void) data, (void) offset;
3209
3210 zio_t *zio = zio_free_sync(pio, pio->io_spa, pio->io_txg, bp,
3211 ZIO_GANG_CHILD_FLAGS(pio));
3212 if (zio == NULL) {
3213 zio = zio_null(pio, pio->io_spa,
3214 NULL, NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio));
3215 }
3216 return (zio);
3217 }
3218
3219 static zio_t *
zio_claim_gang(zio_t * pio,blkptr_t * bp,zio_gang_node_t * gn,abd_t * data,uint64_t offset)3220 zio_claim_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
3221 uint64_t offset)
3222 {
3223 (void) gn, (void) data, (void) offset;
3224 return (zio_claim(pio, pio->io_spa, pio->io_txg, bp,
3225 NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio)));
3226 }
3227
3228 static zio_gang_issue_func_t *zio_gang_issue_func[ZIO_TYPES] = {
3229 NULL,
3230 zio_read_gang,
3231 zio_rewrite_gang,
3232 zio_free_gang,
3233 zio_claim_gang,
3234 NULL
3235 };
3236
3237 static void zio_gang_tree_assemble_done(zio_t *zio);
3238
3239 static zio_gang_node_t *
zio_gang_node_alloc(zio_gang_node_t ** gnpp,uint64_t gangblocksize)3240 zio_gang_node_alloc(zio_gang_node_t **gnpp, uint64_t gangblocksize)
3241 {
3242 zio_gang_node_t *gn;
3243
3244 ASSERT0P(*gnpp);
3245
3246 gn = kmem_zalloc(sizeof (*gn) +
3247 (gbh_nblkptrs(gangblocksize) * sizeof (gn)), KM_SLEEP);
3248 gn->gn_gangblocksize = gn->gn_allocsize = gangblocksize;
3249 gn->gn_gbh = zio_buf_alloc(gangblocksize);
3250 *gnpp = gn;
3251
3252 return (gn);
3253 }
3254
3255 static void
zio_gang_node_free(zio_gang_node_t ** gnpp)3256 zio_gang_node_free(zio_gang_node_t **gnpp)
3257 {
3258 zio_gang_node_t *gn = *gnpp;
3259
3260 for (int g = 0; g < gbh_nblkptrs(gn->gn_allocsize); g++)
3261 ASSERT0P(gn->gn_child[g]);
3262
3263 zio_buf_free(gn->gn_gbh, gn->gn_allocsize);
3264 kmem_free(gn, sizeof (*gn) +
3265 (gbh_nblkptrs(gn->gn_allocsize) * sizeof (gn)));
3266 *gnpp = NULL;
3267 }
3268
3269 static void
zio_gang_tree_free(zio_gang_node_t ** gnpp)3270 zio_gang_tree_free(zio_gang_node_t **gnpp)
3271 {
3272 zio_gang_node_t *gn = *gnpp;
3273
3274 if (gn == NULL)
3275 return;
3276
3277 for (int g = 0; g < gbh_nblkptrs(gn->gn_allocsize); g++)
3278 zio_gang_tree_free(&gn->gn_child[g]);
3279
3280 zio_gang_node_free(gnpp);
3281 }
3282
3283 static void
zio_gang_tree_assemble(zio_t * gio,blkptr_t * bp,zio_gang_node_t ** gnpp)3284 zio_gang_tree_assemble(zio_t *gio, blkptr_t *bp, zio_gang_node_t **gnpp)
3285 {
3286 uint64_t gangblocksize = UINT64_MAX;
3287 if (spa_feature_is_active(gio->io_spa,
3288 SPA_FEATURE_DYNAMIC_GANG_HEADER)) {
3289 spa_config_enter(gio->io_spa, SCL_VDEV, FTAG, RW_READER);
3290 for (int dva = 0; dva < BP_GET_NDVAS(bp); dva++) {
3291 vdev_t *vd = vdev_lookup_top(gio->io_spa,
3292 DVA_GET_VDEV(&bp->blk_dva[dva]));
3293 uint64_t psize = vdev_gang_header_psize(vd);
3294 gangblocksize = MIN(gangblocksize, psize);
3295 }
3296 spa_config_exit(gio->io_spa, SCL_VDEV, FTAG);
3297 } else {
3298 gangblocksize = SPA_OLD_GANGBLOCKSIZE;
3299 }
3300 ASSERT3U(gangblocksize, !=, UINT64_MAX);
3301 zio_gang_node_t *gn = zio_gang_node_alloc(gnpp, gangblocksize);
3302 abd_t *gbh_abd = abd_get_from_buf(gn->gn_gbh, gangblocksize);
3303
3304 ASSERT(gio->io_gang_leader == gio);
3305 ASSERT(BP_IS_GANG(bp));
3306
3307 zio_nowait(zio_read(gio, gio->io_spa, bp, gbh_abd, gangblocksize,
3308 zio_gang_tree_assemble_done, gn, gio->io_priority,
3309 ZIO_GANG_CHILD_FLAGS(gio), &gio->io_bookmark));
3310 }
3311
3312 static void
zio_gang_tree_assemble_done(zio_t * zio)3313 zio_gang_tree_assemble_done(zio_t *zio)
3314 {
3315 zio_t *gio = zio->io_gang_leader;
3316 zio_gang_node_t *gn = zio->io_private;
3317 blkptr_t *bp = zio->io_bp;
3318
3319 ASSERT(gio == zio_unique_parent(zio));
3320 ASSERT(list_is_empty(&zio->io_child_list));
3321
3322 if (zio->io_error)
3323 return;
3324
3325 /* this ABD was created from a linear buf in zio_gang_tree_assemble */
3326 if (BP_SHOULD_BYTESWAP(bp))
3327 byteswap_uint64_array(abd_to_buf(zio->io_abd), zio->io_size);
3328
3329 ASSERT3P(abd_to_buf(zio->io_abd), ==, gn->gn_gbh);
3330 /*
3331 * If this was an old-style gangblock, the gangblocksize should have
3332 * been updated in zio_checksum_error to reflect that.
3333 */
3334 ASSERT3U(gbh_eck(gn->gn_gbh, gn->gn_gangblocksize)->zec_magic,
3335 ==, ZEC_MAGIC);
3336
3337 abd_free(zio->io_abd);
3338
3339 for (int g = 0; g < gbh_nblkptrs(gn->gn_gangblocksize); g++) {
3340 blkptr_t *gbp = gbh_bp(gn->gn_gbh, g);
3341 if (!BP_IS_GANG(gbp))
3342 continue;
3343 zio_gang_tree_assemble(gio, gbp, &gn->gn_child[g]);
3344 }
3345 }
3346
3347 static void
zio_gang_tree_issue(zio_t * pio,zio_gang_node_t * gn,blkptr_t * bp,abd_t * data,uint64_t offset)3348 zio_gang_tree_issue(zio_t *pio, zio_gang_node_t *gn, blkptr_t *bp, abd_t *data,
3349 uint64_t offset)
3350 {
3351 zio_t *gio = pio->io_gang_leader;
3352 zio_t *zio;
3353
3354 ASSERT(BP_IS_GANG(bp) == !!gn);
3355 ASSERT(BP_GET_CHECKSUM(bp) == BP_GET_CHECKSUM(gio->io_bp));
3356 ASSERT(BP_GET_LSIZE(bp) == BP_GET_PSIZE(bp) || gn == gio->io_gang_tree);
3357
3358 /*
3359 * If you're a gang header, your data is in gn->gn_gbh.
3360 * If you're a gang member, your data is in 'data' and gn == NULL.
3361 */
3362 zio = zio_gang_issue_func[gio->io_type](pio, bp, gn, data, offset);
3363
3364 if (gn != NULL) {
3365 ASSERT3U(gbh_eck(gn->gn_gbh,
3366 gn->gn_gangblocksize)->zec_magic, ==, ZEC_MAGIC);
3367
3368 for (int g = 0; g < gbh_nblkptrs(gn->gn_gangblocksize); g++) {
3369 blkptr_t *gbp = gbh_bp(gn->gn_gbh, g);
3370 if (BP_IS_HOLE(gbp))
3371 continue;
3372 zio_gang_tree_issue(zio, gn->gn_child[g], gbp, data,
3373 offset);
3374 offset += BP_GET_PSIZE(gbp);
3375 }
3376 }
3377
3378 if (gn == gio->io_gang_tree)
3379 ASSERT3U(gio->io_size, ==, offset);
3380
3381 if (zio != pio)
3382 zio_nowait(zio);
3383 }
3384
3385 static zio_t *
zio_gang_assemble(zio_t * zio)3386 zio_gang_assemble(zio_t *zio)
3387 {
3388 blkptr_t *bp = zio->io_bp;
3389
3390 ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == NULL);
3391 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
3392
3393 zio->io_gang_leader = zio;
3394
3395 zio_gang_tree_assemble(zio, bp, &zio->io_gang_tree);
3396
3397 return (zio);
3398 }
3399
3400 static zio_t *
zio_gang_issue(zio_t * zio)3401 zio_gang_issue(zio_t *zio)
3402 {
3403 blkptr_t *bp = zio->io_bp;
3404
3405 if (zio_wait_for_children(zio, ZIO_CHILD_GANG_BIT, ZIO_WAIT_DONE)) {
3406 return (NULL);
3407 }
3408
3409 ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == zio);
3410 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
3411
3412 if (zio->io_child_error[ZIO_CHILD_GANG] == 0)
3413 zio_gang_tree_issue(zio, zio->io_gang_tree, bp, zio->io_abd,
3414 0);
3415 else
3416 zio_gang_tree_free(&zio->io_gang_tree);
3417
3418 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
3419
3420 return (zio);
3421 }
3422
3423 static void
zio_inherit_allocator(zio_t * pio,zio_t * cio)3424 zio_inherit_allocator(zio_t *pio, zio_t *cio)
3425 {
3426 cio->io_allocator = pio->io_allocator;
3427 }
3428
3429 static void
zio_write_gang_member_ready(zio_t * zio)3430 zio_write_gang_member_ready(zio_t *zio)
3431 {
3432 zio_t *pio = zio_unique_parent(zio);
3433 dva_t *cdva = zio->io_bp->blk_dva;
3434 dva_t *pdva = pio->io_bp->blk_dva;
3435 uint64_t asize;
3436 zio_t *gio __maybe_unused = zio->io_gang_leader;
3437
3438 if (BP_IS_HOLE(zio->io_bp))
3439 return;
3440
3441 /*
3442 * If we're getting direct-invoked from zio_write_gang_block(),
3443 * the bp_orig will be set.
3444 */
3445 ASSERT(BP_IS_HOLE(&zio->io_bp_orig) ||
3446 zio->io_flags & ZIO_FLAG_PREALLOCATED);
3447
3448 ASSERT(zio->io_child_type == ZIO_CHILD_GANG);
3449 ASSERT3U(zio->io_prop.zp_copies, ==, gio->io_prop.zp_copies);
3450 ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(zio->io_bp));
3451 ASSERT3U(pio->io_prop.zp_copies, <=, BP_GET_NDVAS(pio->io_bp));
3452 VERIFY3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp));
3453
3454 mutex_enter(&pio->io_lock);
3455 for (int d = 0; d < BP_GET_NDVAS(zio->io_bp); d++) {
3456 ASSERT(DVA_GET_GANG(&pdva[d]));
3457 asize = DVA_GET_ASIZE(&pdva[d]);
3458 asize += DVA_GET_ASIZE(&cdva[d]);
3459 DVA_SET_ASIZE(&pdva[d], asize);
3460 }
3461 mutex_exit(&pio->io_lock);
3462 }
3463
3464 static void
zio_write_gang_done(zio_t * zio)3465 zio_write_gang_done(zio_t *zio)
3466 {
3467 /*
3468 * The io_abd field will be NULL for a zio with no data. The io_flags
3469 * will initially have the ZIO_FLAG_NODATA bit flag set, but we can't
3470 * check for it here as it is cleared in zio_ready.
3471 */
3472 if (zio->io_abd != NULL)
3473 abd_free(zio->io_abd);
3474 }
3475
3476 static void
zio_update_feature(void * arg,dmu_tx_t * tx)3477 zio_update_feature(void *arg, dmu_tx_t *tx)
3478 {
3479 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
3480 spa_feature_incr(spa, (spa_feature_t)(uintptr_t)arg, tx);
3481 }
3482
3483 static zio_t *
zio_write_gang_block(zio_t * pio,metaslab_class_t * mc)3484 zio_write_gang_block(zio_t *pio, metaslab_class_t *mc)
3485 {
3486 spa_t *spa = pio->io_spa;
3487 blkptr_t *bp = pio->io_bp;
3488 zio_t *gio = pio->io_gang_leader;
3489 zio_t *zio;
3490 zio_gang_node_t *gn, **gnpp;
3491 zio_gbh_phys_t *gbh;
3492 abd_t *gbh_abd;
3493 uint64_t txg = pio->io_txg;
3494 uint64_t resid = pio->io_size;
3495 zio_prop_t zp;
3496 int error;
3497 boolean_t has_data = !(pio->io_flags & ZIO_FLAG_NODATA);
3498
3499 /*
3500 * Store multiple copies of the GBH, so that we can still traverse
3501 * all the data (e.g. to free or scrub) even if a block is damaged.
3502 * This value respects the redundant_metadata property.
3503 */
3504 int gbh_copies = gio->io_prop.zp_gang_copies;
3505 if (gbh_copies == 0) {
3506 /*
3507 * This should only happen in the case where we're filling in
3508 * DDT entries for a parent that wants more copies than the DDT
3509 * has. In that case, we cannot gang without creating a mixed
3510 * blkptr, which is illegal.
3511 */
3512 ASSERT3U(gio->io_child_type, ==, ZIO_CHILD_DDT);
3513 pio->io_error = EAGAIN;
3514 return (pio);
3515 }
3516 ASSERT3S(gbh_copies, >, 0);
3517 ASSERT3S(gbh_copies, <=, SPA_DVAS_PER_BP);
3518
3519 ASSERT(ZIO_HAS_ALLOCATOR(pio));
3520 int flags = METASLAB_GANG_HEADER;
3521 if (pio->io_flags & ZIO_FLAG_ALLOC_THROTTLED) {
3522 ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
3523 ASSERT(has_data);
3524
3525 flags |= METASLAB_ASYNC_ALLOC;
3526 }
3527
3528 uint64_t gangblocksize = SPA_OLD_GANGBLOCKSIZE;
3529 uint64_t candidate = gangblocksize;
3530 error = metaslab_alloc_range(spa, mc, gangblocksize, gangblocksize,
3531 bp, gbh_copies, txg, pio == gio ? NULL : gio->io_bp, flags,
3532 ZIO_ALLOC_LIST(pio), pio->io_allocator, pio, &candidate);
3533 if (error) {
3534 pio->io_error = error;
3535 return (pio);
3536 }
3537 if (spa_feature_is_active(spa, SPA_FEATURE_DYNAMIC_GANG_HEADER))
3538 gangblocksize = candidate;
3539
3540 if (pio == gio) {
3541 gnpp = &gio->io_gang_tree;
3542 } else {
3543 gnpp = pio->io_private;
3544 ASSERT(pio->io_ready == zio_write_gang_member_ready);
3545 }
3546
3547 gn = zio_gang_node_alloc(gnpp, gangblocksize);
3548 gbh = gn->gn_gbh;
3549 memset(gbh, 0, gangblocksize);
3550 gbh_abd = abd_get_from_buf(gbh, gangblocksize);
3551
3552 /*
3553 * Create the gang header.
3554 */
3555 zio = zio_rewrite(pio, spa, txg, bp, gbh_abd, gangblocksize,
3556 zio_write_gang_done, NULL, pio->io_priority,
3557 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
3558
3559 zio_inherit_allocator(pio, zio);
3560 if (pio->io_flags & ZIO_FLAG_ALLOC_THROTTLED) {
3561 boolean_t more;
3562 VERIFY(metaslab_class_throttle_reserve(mc, zio->io_allocator,
3563 gbh_copies, zio->io_size, B_TRUE, &more));
3564 zio->io_flags |= ZIO_FLAG_ALLOC_THROTTLED;
3565 }
3566
3567 /*
3568 * Create and nowait the gang children. First, we try to do
3569 * opportunistic allocations. If that fails to generate enough
3570 * space, we fall back to normal zio_write calls for nested gang.
3571 */
3572 int g;
3573 boolean_t any_failed = B_FALSE;
3574 for (g = 0; resid != 0; g++) {
3575 flags &= METASLAB_ASYNC_ALLOC;
3576 flags |= METASLAB_GANG_CHILD;
3577 zp.zp_checksum = gio->io_prop.zp_checksum;
3578 zp.zp_compress = ZIO_COMPRESS_OFF;
3579 zp.zp_complevel = gio->io_prop.zp_complevel;
3580 zp.zp_type = zp.zp_storage_type = DMU_OT_NONE;
3581 zp.zp_level = 0;
3582 zp.zp_copies = gio->io_prop.zp_copies;
3583 zp.zp_gang_copies = gio->io_prop.zp_gang_copies;
3584 zp.zp_dedup = B_FALSE;
3585 zp.zp_dedup_verify = B_FALSE;
3586 zp.zp_nopwrite = B_FALSE;
3587 zp.zp_encrypt = gio->io_prop.zp_encrypt;
3588 zp.zp_byteorder = gio->io_prop.zp_byteorder;
3589 zp.zp_direct_write = B_FALSE;
3590 memset(zp.zp_salt, 0, ZIO_DATA_SALT_LEN);
3591 memset(zp.zp_iv, 0, ZIO_DATA_IV_LEN);
3592 memset(zp.zp_mac, 0, ZIO_DATA_MAC_LEN);
3593
3594 uint64_t min_size = zio_roundup_alloc_size(spa,
3595 resid / (gbh_nblkptrs(gangblocksize) - g));
3596 min_size = MIN(min_size, resid);
3597 bp = &((blkptr_t *)gbh)[g];
3598
3599 zio_alloc_list_t cio_list;
3600 metaslab_trace_init(&cio_list);
3601 uint64_t allocated_size = UINT64_MAX;
3602 error = metaslab_alloc_range(spa, mc, min_size, resid,
3603 bp, gio->io_prop.zp_copies, txg, NULL,
3604 flags, &cio_list, zio->io_allocator, NULL, &allocated_size);
3605
3606 boolean_t allocated = error == 0;
3607 any_failed |= !allocated;
3608
3609 uint64_t psize = allocated ? MIN(resid, allocated_size) :
3610 min_size;
3611 ASSERT3U(psize, >=, min_size);
3612
3613 zio_t *cio = zio_write(zio, spa, txg, bp, has_data ?
3614 abd_get_offset(pio->io_abd, pio->io_size - resid) : NULL,
3615 psize, psize, &zp, zio_write_gang_member_ready, NULL,
3616 zio_write_gang_done, &gn->gn_child[g], pio->io_priority,
3617 ZIO_GANG_CHILD_FLAGS(pio) |
3618 (allocated ? ZIO_FLAG_PREALLOCATED : 0), &pio->io_bookmark);
3619
3620 resid -= psize;
3621 zio_inherit_allocator(zio, cio);
3622 if (allocated) {
3623 metaslab_trace_move(&cio_list, ZIO_ALLOC_LIST(cio));
3624 metaslab_group_alloc_increment_all(spa,
3625 &cio->io_bp_orig, zio->io_allocator, flags, psize,
3626 cio);
3627 }
3628 /*
3629 * We do not reserve for the child writes, since we already
3630 * reserved for the parent. Unreserve though will be called
3631 * for individual children. We can do this since sum of all
3632 * child's physical sizes is equal to parent's physical size.
3633 * It would not work for potentially bigger allocation sizes.
3634 */
3635
3636 zio_nowait(cio);
3637 }
3638
3639 /*
3640 * If we used more gang children than the old limit, we must already be
3641 * using the new headers. No need to update anything, just move on.
3642 *
3643 * Otherwise, we might be in a case where we need to turn on the new
3644 * feature, so we check that. We enable the new feature if we didn't
3645 * manage to fit everything into 3 gang children and we could have
3646 * written more than that.
3647 */
3648 if (g > gbh_nblkptrs(SPA_OLD_GANGBLOCKSIZE)) {
3649 ASSERT(spa_feature_is_active(spa,
3650 SPA_FEATURE_DYNAMIC_GANG_HEADER));
3651 } else if (any_failed && candidate > SPA_OLD_GANGBLOCKSIZE &&
3652 spa_feature_is_enabled(spa, SPA_FEATURE_DYNAMIC_GANG_HEADER) &&
3653 !spa_feature_is_active(spa, SPA_FEATURE_DYNAMIC_GANG_HEADER)) {
3654 dmu_tx_t *tx = dmu_tx_create_assigned(spa->spa_dsl_pool,
3655 MAX(txg, spa_syncing_txg(spa) + 1));
3656 dsl_sync_task_nowait(spa->spa_dsl_pool,
3657 zio_update_feature,
3658 (void *)SPA_FEATURE_DYNAMIC_GANG_HEADER, tx);
3659 dmu_tx_commit(tx);
3660 }
3661
3662 /*
3663 * Set pio's pipeline to just wait for zio to finish.
3664 */
3665 pio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
3666
3667 zio_nowait(zio);
3668
3669 return (pio);
3670 }
3671
3672 /*
3673 * The zio_nop_write stage in the pipeline determines if allocating a
3674 * new bp is necessary. The nopwrite feature can handle writes in
3675 * either syncing or open context (i.e. zil writes) and as a result is
3676 * mutually exclusive with dedup.
3677 *
3678 * By leveraging a cryptographically secure checksum, such as SHA256, we
3679 * can compare the checksums of the new data and the old to determine if
3680 * allocating a new block is required. Note that our requirements for
3681 * cryptographic strength are fairly weak: there can't be any accidental
3682 * hash collisions, but we don't need to be secure against intentional
3683 * (malicious) collisions. To trigger a nopwrite, you have to be able
3684 * to write the file to begin with, and triggering an incorrect (hash
3685 * collision) nopwrite is no worse than simply writing to the file.
3686 * That said, there are no known attacks against the checksum algorithms
3687 * used for nopwrite, assuming that the salt and the checksums
3688 * themselves remain secret.
3689 */
3690 static zio_t *
zio_nop_write(zio_t * zio)3691 zio_nop_write(zio_t *zio)
3692 {
3693 blkptr_t *bp = zio->io_bp;
3694 blkptr_t *bp_orig = &zio->io_bp_orig;
3695 zio_prop_t *zp = &zio->io_prop;
3696
3697 ASSERT(BP_IS_HOLE(bp));
3698 ASSERT0(BP_GET_LEVEL(bp));
3699 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
3700 ASSERT(zp->zp_nopwrite);
3701 ASSERT(!zp->zp_dedup);
3702 ASSERT0P(zio->io_bp_override);
3703 ASSERT(IO_IS_ALLOCATING(zio));
3704
3705 /*
3706 * Check to see if the original bp and the new bp have matching
3707 * characteristics (i.e. same checksum, compression algorithms, etc).
3708 * If they don't then just continue with the pipeline which will
3709 * allocate a new bp.
3710 */
3711 if (BP_IS_HOLE(bp_orig) ||
3712 !(zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_flags &
3713 ZCHECKSUM_FLAG_NOPWRITE) ||
3714 BP_IS_ENCRYPTED(bp) || BP_IS_ENCRYPTED(bp_orig) ||
3715 BP_GET_CHECKSUM(bp) != BP_GET_CHECKSUM(bp_orig) ||
3716 BP_GET_COMPRESS(bp) != BP_GET_COMPRESS(bp_orig) ||
3717 BP_GET_DEDUP(bp) != BP_GET_DEDUP(bp_orig) ||
3718 zp->zp_copies != BP_GET_NDVAS(bp_orig))
3719 return (zio);
3720
3721 /*
3722 * If the checksums match then reset the pipeline so that we
3723 * avoid allocating a new bp and issuing any I/O.
3724 */
3725 if (ZIO_CHECKSUM_EQUAL(bp->blk_cksum, bp_orig->blk_cksum)) {
3726 ASSERT(zio_checksum_table[zp->zp_checksum].ci_flags &
3727 ZCHECKSUM_FLAG_NOPWRITE);
3728 ASSERT3U(BP_GET_PSIZE(bp), ==, BP_GET_PSIZE(bp_orig));
3729 ASSERT3U(BP_GET_LSIZE(bp), ==, BP_GET_LSIZE(bp_orig));
3730 ASSERT(zp->zp_compress != ZIO_COMPRESS_OFF);
3731 ASSERT3U(bp->blk_prop, ==, bp_orig->blk_prop);
3732
3733 /*
3734 * If we're overwriting a block that is currently on an
3735 * indirect vdev, then ignore the nopwrite request and
3736 * allow a new block to be allocated on a concrete vdev.
3737 */
3738 spa_config_enter(zio->io_spa, SCL_VDEV, FTAG, RW_READER);
3739 for (int d = 0; d < BP_GET_NDVAS(bp_orig); d++) {
3740 vdev_t *tvd = vdev_lookup_top(zio->io_spa,
3741 DVA_GET_VDEV(&bp_orig->blk_dva[d]));
3742 if (tvd->vdev_ops == &vdev_indirect_ops) {
3743 spa_config_exit(zio->io_spa, SCL_VDEV, FTAG);
3744 return (zio);
3745 }
3746 }
3747 spa_config_exit(zio->io_spa, SCL_VDEV, FTAG);
3748
3749 *bp = *bp_orig;
3750 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
3751 zio->io_flags |= ZIO_FLAG_NOPWRITE;
3752 }
3753
3754 return (zio);
3755 }
3756
3757 /*
3758 * ==========================================================================
3759 * Block Reference Table
3760 * ==========================================================================
3761 */
3762 static zio_t *
zio_brt_free(zio_t * zio)3763 zio_brt_free(zio_t *zio)
3764 {
3765 blkptr_t *bp;
3766
3767 bp = zio->io_bp;
3768
3769 if (BP_GET_LEVEL(bp) > 0 ||
3770 BP_IS_METADATA(bp) ||
3771 !brt_maybe_exists(zio->io_spa, bp)) {
3772 return (zio);
3773 }
3774
3775 if (!brt_entry_decref(zio->io_spa, bp)) {
3776 /*
3777 * This isn't the last reference, so we cannot free
3778 * the data yet.
3779 */
3780 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
3781 }
3782
3783 return (zio);
3784 }
3785
3786 /*
3787 * ==========================================================================
3788 * Dedup
3789 * ==========================================================================
3790 */
3791 static void
zio_ddt_child_read_done(zio_t * zio)3792 zio_ddt_child_read_done(zio_t *zio)
3793 {
3794 blkptr_t *bp = zio->io_bp;
3795 ddt_t *ddt;
3796 ddt_entry_t *dde = zio->io_private;
3797 zio_t *pio = zio_unique_parent(zio);
3798
3799 mutex_enter(&pio->io_lock);
3800 ddt = ddt_select(zio->io_spa, bp);
3801
3802 if (zio->io_error == 0) {
3803 ddt_phys_variant_t v = ddt_phys_select(ddt, dde, bp);
3804 /* this phys variant doesn't need repair */
3805 ddt_phys_clear(dde->dde_phys, v);
3806 }
3807
3808 if (zio->io_error == 0 && dde->dde_io->dde_repair_abd == NULL)
3809 dde->dde_io->dde_repair_abd = zio->io_abd;
3810 else
3811 abd_free(zio->io_abd);
3812 mutex_exit(&pio->io_lock);
3813 }
3814
3815 static zio_t *
zio_ddt_read_start(zio_t * zio)3816 zio_ddt_read_start(zio_t *zio)
3817 {
3818 blkptr_t *bp = zio->io_bp;
3819
3820 ASSERT(BP_GET_DEDUP(bp));
3821 ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
3822 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3823
3824 if (zio->io_child_error[ZIO_CHILD_DDT]) {
3825 ddt_t *ddt = ddt_select(zio->io_spa, bp);
3826 ddt_entry_t *dde = ddt_repair_start(ddt, bp);
3827 ddt_phys_variant_t v_self = ddt_phys_select(ddt, dde, bp);
3828 ddt_univ_phys_t *ddp = dde->dde_phys;
3829 blkptr_t blk;
3830
3831 ASSERT0P(zio->io_vsd);
3832 zio->io_vsd = dde;
3833
3834 if (v_self == DDT_PHYS_NONE)
3835 return (zio);
3836
3837 /* issue I/O for the other copies */
3838 for (int p = 0; p < DDT_NPHYS(ddt); p++) {
3839 ddt_phys_variant_t v = DDT_PHYS_VARIANT(ddt, p);
3840
3841 if (ddt_phys_birth(ddp, v) == 0 || v == v_self)
3842 continue;
3843
3844 ddt_bp_create(ddt->ddt_checksum, &dde->dde_key,
3845 ddp, v, &blk);
3846 zio_nowait(zio_read(zio, zio->io_spa, &blk,
3847 abd_alloc_for_io(zio->io_size, B_TRUE),
3848 zio->io_size, zio_ddt_child_read_done, dde,
3849 zio->io_priority, ZIO_DDT_CHILD_FLAGS(zio) |
3850 ZIO_FLAG_DONT_PROPAGATE, &zio->io_bookmark));
3851 }
3852 return (zio);
3853 }
3854
3855 zio_nowait(zio_read(zio, zio->io_spa, bp,
3856 zio->io_abd, zio->io_size, NULL, NULL, zio->io_priority,
3857 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark));
3858
3859 return (zio);
3860 }
3861
3862 static zio_t *
zio_ddt_read_done(zio_t * zio)3863 zio_ddt_read_done(zio_t *zio)
3864 {
3865 blkptr_t *bp = zio->io_bp;
3866
3867 if (zio_wait_for_children(zio, ZIO_CHILD_DDT_BIT, ZIO_WAIT_DONE)) {
3868 return (NULL);
3869 }
3870
3871 ASSERT(BP_GET_DEDUP(bp));
3872 ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
3873 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3874
3875 if (zio->io_child_error[ZIO_CHILD_DDT]) {
3876 ddt_t *ddt = ddt_select(zio->io_spa, bp);
3877 ddt_entry_t *dde = zio->io_vsd;
3878 if (ddt == NULL) {
3879 ASSERT(spa_load_state(zio->io_spa) != SPA_LOAD_NONE);
3880 return (zio);
3881 }
3882 if (dde == NULL) {
3883 zio->io_stage = ZIO_STAGE_DDT_READ_START >> 1;
3884 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
3885 return (NULL);
3886 }
3887 if (dde->dde_io->dde_repair_abd != NULL) {
3888 abd_copy(zio->io_abd, dde->dde_io->dde_repair_abd,
3889 zio->io_size);
3890 zio->io_child_error[ZIO_CHILD_DDT] = 0;
3891 }
3892 ddt_repair_done(ddt, dde);
3893 zio->io_vsd = NULL;
3894 }
3895
3896 ASSERT0P(zio->io_vsd);
3897
3898 return (zio);
3899 }
3900
3901 static boolean_t
zio_ddt_collision(zio_t * zio,ddt_t * ddt,ddt_entry_t * dde)3902 zio_ddt_collision(zio_t *zio, ddt_t *ddt, ddt_entry_t *dde)
3903 {
3904 spa_t *spa = zio->io_spa;
3905 boolean_t do_raw = !!(zio->io_flags & ZIO_FLAG_RAW);
3906
3907 ASSERT(!(zio->io_bp_override && do_raw));
3908
3909 /*
3910 * Note: we compare the original data, not the transformed data,
3911 * because when zio->io_bp is an override bp, we will not have
3912 * pushed the I/O transforms. That's an important optimization
3913 * because otherwise we'd compress/encrypt all dmu_sync() data twice.
3914 * However, we should never get a raw, override zio so in these
3915 * cases we can compare the io_abd directly. This is useful because
3916 * it allows us to do dedup verification even if we don't have access
3917 * to the original data (for instance, if the encryption keys aren't
3918 * loaded).
3919 */
3920
3921 for (int p = 0; p < DDT_NPHYS(ddt); p++) {
3922 if (DDT_PHYS_IS_DITTO(ddt, p))
3923 continue;
3924
3925 if (dde->dde_io == NULL)
3926 continue;
3927
3928 /*
3929 * Lock dde_io to prevent the lead zio from completing
3930 * and freeing its ABD while we compare against it.
3931 */
3932 mutex_enter(&dde->dde_io->dde_io_lock);
3933 zio_t *lio = dde->dde_io->dde_lead_zio[p];
3934 if (lio == NULL) {
3935 mutex_exit(&dde->dde_io->dde_io_lock);
3936 continue;
3937 }
3938 boolean_t collision;
3939 if (do_raw) {
3940 collision = lio->io_size != zio->io_size ||
3941 abd_cmp(zio->io_abd, lio->io_abd) != 0;
3942 } else {
3943 collision = lio->io_orig_size != zio->io_orig_size ||
3944 abd_cmp(zio->io_orig_abd, lio->io_orig_abd) != 0;
3945 }
3946 mutex_exit(&dde->dde_io->dde_io_lock);
3947 return (collision);
3948 }
3949
3950 for (int p = 0; p < DDT_NPHYS(ddt); p++) {
3951 ddt_phys_variant_t v = DDT_PHYS_VARIANT(ddt, p);
3952 uint64_t phys_birth = ddt_phys_birth(dde->dde_phys, v);
3953
3954 if (phys_birth != 0 && do_raw) {
3955 blkptr_t blk = *zio->io_bp;
3956 uint64_t psize;
3957 abd_t *tmpabd;
3958 int error;
3959
3960 ddt_bp_fill(dde->dde_phys, v, &blk, phys_birth);
3961 psize = BP_GET_PSIZE(&blk);
3962
3963 if (psize != zio->io_size)
3964 return (B_TRUE);
3965
3966 ddt_exit(ddt);
3967
3968 tmpabd = abd_alloc_for_io(psize, B_TRUE);
3969
3970 error = zio_wait(zio_read(NULL, spa, &blk, tmpabd,
3971 psize, NULL, NULL, ZIO_PRIORITY_SYNC_READ,
3972 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
3973 ZIO_FLAG_RAW, &zio->io_bookmark));
3974
3975 if (error == 0) {
3976 if (abd_cmp(tmpabd, zio->io_abd) != 0)
3977 error = SET_ERROR(ENOENT);
3978 }
3979
3980 abd_free(tmpabd);
3981 ddt_enter(ddt);
3982 return (error != 0);
3983 } else if (phys_birth != 0) {
3984 arc_buf_t *abuf = NULL;
3985 arc_flags_t aflags = ARC_FLAG_WAIT;
3986 blkptr_t blk = *zio->io_bp;
3987 int error;
3988
3989 ddt_bp_fill(dde->dde_phys, v, &blk, phys_birth);
3990
3991 if (BP_GET_LSIZE(&blk) != zio->io_orig_size)
3992 return (B_TRUE);
3993
3994 ddt_exit(ddt);
3995
3996 error = arc_read(NULL, spa, &blk,
3997 arc_getbuf_func, &abuf, ZIO_PRIORITY_SYNC_READ,
3998 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
3999 &aflags, &zio->io_bookmark);
4000
4001 if (error == 0) {
4002 if (abd_cmp_buf(zio->io_orig_abd, abuf->b_data,
4003 zio->io_orig_size) != 0)
4004 error = SET_ERROR(ENOENT);
4005 arc_buf_destroy(abuf, &abuf);
4006 }
4007
4008 ddt_enter(ddt);
4009 return (error != 0);
4010 }
4011 }
4012
4013 return (B_FALSE);
4014 }
4015
4016 static void
zio_ddt_child_write_done(zio_t * zio)4017 zio_ddt_child_write_done(zio_t *zio)
4018 {
4019 ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
4020 ddt_entry_t *dde = zio->io_private;
4021
4022 zio_link_t *zl = NULL;
4023 ASSERT3P(zio_walk_parents(zio, &zl), !=, NULL);
4024
4025 int p = DDT_PHYS_FOR_COPIES(ddt, zio->io_prop.zp_copies);
4026 ddt_phys_variant_t v = DDT_PHYS_VARIANT(ddt, p);
4027 ddt_univ_phys_t *ddp = dde->dde_phys;
4028
4029 mutex_enter(&dde->dde_io->dde_io_lock);
4030
4031 /* we're the lead, so once we're done there's no one else outstanding */
4032 if (dde->dde_io->dde_lead_zio[p] == zio)
4033 dde->dde_io->dde_lead_zio[p] = NULL;
4034
4035 ddt_univ_phys_t *orig = &dde->dde_io->dde_orig_phys;
4036
4037 if (zio->io_error != 0) {
4038 /*
4039 * The write failed, so we're about to abort the entire IO
4040 * chain. We need to revert the entry back to what it was at
4041 * the last time it was successfully extended.
4042 */
4043 ddt_phys_unextend(ddp, orig, v);
4044 ddt_phys_clear(orig, v);
4045
4046 mutex_exit(&dde->dde_io->dde_io_lock);
4047
4048 /*
4049 * Undo the optimistic refcount increments that were done in
4050 * zio_ddt_write() for all non-DDT-child parents. Since errors
4051 * are rare, taking the global lock here is acceptable.
4052 */
4053 ddt_enter(ddt);
4054 zio_t *pio;
4055 zl = NULL;
4056 while ((pio = zio_walk_parents(zio, &zl)) != NULL) {
4057 if (!(pio->io_flags & ZIO_FLAG_DDT_CHILD))
4058 ddt_phys_decref(ddp, v);
4059 }
4060 ddt_exit(ddt);
4061 return;
4062 }
4063
4064 /*
4065 * We've successfully added new DVAs to the entry. Clear the saved
4066 * state or, if there's still outstanding IO, remember it so we can
4067 * revert to a known good state if that IO fails.
4068 */
4069 if (dde->dde_io->dde_lead_zio[p] == NULL)
4070 ddt_phys_clear(orig, v);
4071 else
4072 ddt_phys_copy(orig, ddp, v);
4073
4074 mutex_exit(&dde->dde_io->dde_io_lock);
4075 }
4076
4077 static void
zio_ddt_child_write_ready(zio_t * zio)4078 zio_ddt_child_write_ready(zio_t *zio)
4079 {
4080 ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
4081 ddt_entry_t *dde = zio->io_private;
4082
4083 zio_link_t *zl = NULL;
4084 ASSERT3P(zio_walk_parents(zio, &zl), !=, NULL);
4085
4086 int p = DDT_PHYS_FOR_COPIES(ddt, zio->io_prop.zp_copies);
4087 ddt_phys_variant_t v = DDT_PHYS_VARIANT(ddt, p);
4088
4089 if (ddt_phys_is_gang(dde->dde_phys, v)) {
4090 for (int i = 0; i < BP_GET_NDVAS(zio->io_bp); i++) {
4091 dva_t *d = &zio->io_bp->blk_dva[i];
4092 metaslab_group_alloc_decrement(zio->io_spa,
4093 DVA_GET_VDEV(d), zio->io_allocator,
4094 METASLAB_ASYNC_ALLOC, zio->io_size, zio);
4095 }
4096 zio->io_error = EAGAIN;
4097 }
4098
4099 if (zio->io_error != 0)
4100 return;
4101
4102 mutex_enter(&dde->dde_io->dde_io_lock);
4103
4104 ddt_phys_extend(dde->dde_phys, v, zio->io_bp);
4105
4106 zio_t *pio;
4107 zl = NULL;
4108 while ((pio = zio_walk_parents(zio, &zl)) != NULL) {
4109 if (!(pio->io_flags & ZIO_FLAG_DDT_CHILD))
4110 ddt_bp_fill(dde->dde_phys, v, pio->io_bp, zio->io_txg);
4111 }
4112
4113 mutex_exit(&dde->dde_io->dde_io_lock);
4114 }
4115
4116 static zio_t *
zio_ddt_write(zio_t * zio)4117 zio_ddt_write(zio_t *zio)
4118 {
4119 spa_t *spa = zio->io_spa;
4120 blkptr_t *bp = zio->io_bp;
4121 uint64_t txg = zio->io_txg;
4122 zio_prop_t *zp = &zio->io_prop;
4123 ddt_t *ddt = ddt_select(spa, bp);
4124 ddt_entry_t *dde;
4125
4126 ASSERT(BP_GET_DEDUP(bp));
4127 ASSERT(BP_GET_CHECKSUM(bp) == zp->zp_checksum);
4128 ASSERT(BP_IS_HOLE(bp) || zio->io_bp_override);
4129 ASSERT(!(zio->io_bp_override && (zio->io_flags & ZIO_FLAG_RAW)));
4130 /*
4131 * Deduplication will not take place for Direct I/O writes. The
4132 * ddt_tree will be emptied in syncing context. Direct I/O writes take
4133 * place in the open-context. Direct I/O write can not attempt to
4134 * modify the ddt_tree while issuing out a write.
4135 */
4136 ASSERT3B(zio->io_prop.zp_direct_write, ==, B_FALSE);
4137
4138 ddt_enter(ddt);
4139 /*
4140 * Search DDT for matching entry. Skip DVAs verification here, since
4141 * they can go only from override, and once we get here the override
4142 * pointer can't have "D" flag to be confused with pruned DDT entries.
4143 */
4144 IMPLY(zio->io_bp_override, !BP_GET_DEDUP(zio->io_bp_override));
4145 dde = ddt_lookup(ddt, bp, B_FALSE);
4146 if (dde == NULL) {
4147 /* DDT size is over its quota so no new entries */
4148 ddt_exit(ddt);
4149 zp->zp_dedup = B_FALSE;
4150 BP_SET_DEDUP(bp, B_FALSE);
4151 if (zio->io_bp_override == NULL)
4152 zio->io_pipeline = ZIO_WRITE_PIPELINE;
4153 return (zio);
4154 }
4155
4156 if (zp->zp_dedup_verify && zio_ddt_collision(zio, ddt, dde)) {
4157 /*
4158 * If we're using a weak checksum, upgrade to a strong checksum
4159 * and try again. If we're already using a strong checksum,
4160 * we can't resolve it, so just convert to an ordinary write.
4161 * (And automatically e-mail a paper to Nature?)
4162 */
4163 ddt_exit(ddt);
4164 if (!(zio_checksum_table[zp->zp_checksum].ci_flags &
4165 ZCHECKSUM_FLAG_DEDUP)) {
4166 zp->zp_checksum = spa_dedup_checksum(spa);
4167 zio_pop_transforms(zio);
4168 zio->io_stage = ZIO_STAGE_OPEN;
4169 BP_ZERO(bp);
4170 } else {
4171 zp->zp_dedup = B_FALSE;
4172 BP_SET_DEDUP(bp, B_FALSE);
4173 }
4174 ASSERT(!BP_GET_DEDUP(bp));
4175 zio->io_pipeline = ZIO_WRITE_PIPELINE;
4176 return (zio);
4177 }
4178
4179 int p = DDT_PHYS_FOR_COPIES(ddt, zp->zp_copies);
4180 ddt_phys_variant_t v = DDT_PHYS_VARIANT(ddt, p);
4181
4182 /*
4183 * In the common cases, at this point we have a regular BP with no
4184 * allocated DVAs, and the corresponding DDT entry for its checksum.
4185 * Our goal is to fill the BP with enough DVAs to satisfy its copies=
4186 * requirement.
4187 *
4188 * One of three things needs to happen to fulfill this:
4189 *
4190 * - if the DDT entry has enough DVAs to satisfy the BP, we just copy
4191 * them out of the entry and return;
4192 *
4193 * - if the DDT entry has no DVAs (ie its brand new), then we have to
4194 * issue the write as normal so that DVAs can be allocated and the
4195 * data land on disk. We then copy the DVAs into the DDT entry on
4196 * return.
4197 *
4198 * - if the DDT entry has some DVAs, but too few, we have to issue the
4199 * write, adjusted to have allocate fewer copies. When it returns, we
4200 * add the new DVAs to the DDT entry, and update the BP to have the
4201 * full amount it originally requested.
4202 *
4203 * In all cases, if there's already a writing IO in flight, we need to
4204 * defer the action until after the write is done. If our action is to
4205 * write, we need to adjust our request for additional DVAs to match
4206 * what will be in the DDT entry after it completes. In this way every
4207 * IO can be guaranteed to recieve enough DVAs simply by joining the
4208 * end of the chain and letting the sequence play out.
4209 */
4210
4211 /* Number of DVAs requested by the IO. */
4212 uint8_t need_dvas = zp->zp_copies;
4213 /* Number of DVAs in outstanding writes for this dde. */
4214 uint8_t parent_dvas = 0;
4215
4216 /*
4217 * What we do next depends on whether or not there's IO outstanding
4218 * that will update this entry. If dde_io exists, we need to hold
4219 * its lock to safely check and use dde_lead_zio.
4220 */
4221 ddt_entry_io_t *dde_io = dde->dde_io;
4222 if (dde_io != NULL)
4223 mutex_enter(&dde_io->dde_io_lock);
4224
4225 /*
4226 * Number of DVAs in the DDT entry. If the BP is encrypted we ignore
4227 * the third one as normal.
4228 *
4229 * Must be computed after taking dde_io_lock (if held) to avoid
4230 * racing with ddt_phys_unextend() in zio_ddt_child_write_done()
4231 * error path, which can zero DVAs under dde_io_lock. Without the
4232 * lock, a stale have_dvas causes ddt_bp_fill() to copy a zeroed
4233 * DVA into the BP, producing a hole that reads back as zeros.
4234 */
4235 ddt_univ_phys_t *ddp = dde->dde_phys;
4236 int have_dvas = ddt_phys_dva_count(ddp, v, BP_IS_ENCRYPTED(bp));
4237 IMPLY(have_dvas == 0, ddt_phys_birth(ddp, v) == 0);
4238 boolean_t is_ganged = ddt_phys_is_gang(ddp, v);
4239
4240 if (dde_io == NULL || dde_io->dde_lead_zio[p] == NULL) {
4241 /*
4242 * No IO outstanding, so we only need to worry about ourselves.
4243 */
4244
4245 /*
4246 * Override BPs bring their own DVAs and their own problems.
4247 */
4248 if (zio->io_bp_override) {
4249 /*
4250 * For a brand-new entry, all the work has been done
4251 * for us, and we can just fill it out from the provided
4252 * block and leave.
4253 */
4254 if (have_dvas == 0) {
4255 if (dde_io != NULL)
4256 mutex_exit(&dde_io->dde_io_lock);
4257 ASSERT(BP_GET_BIRTH(bp) == txg);
4258 ASSERT(BP_EQUAL(bp, zio->io_bp_override));
4259 ddt_phys_extend(ddp, v, bp);
4260 ddt_phys_addref(ddp, v);
4261 ddt_exit(ddt);
4262 return (zio);
4263 }
4264
4265 /*
4266 * If we already have this entry, then we want to treat
4267 * it like a regular write. To do this we just wipe
4268 * them out and proceed like a regular write.
4269 *
4270 * Even if there are some DVAs in the entry, we still
4271 * have to clear them out. We can't use them to fill
4272 * out the dedup entry, as they are all referenced
4273 * together by a bp already on disk, and will be freed
4274 * as a group.
4275 */
4276 BP_ZERO_DVAS(bp);
4277 BP_SET_BIRTH(bp, 0, 0);
4278 }
4279
4280 /*
4281 * If there are enough DVAs in the entry to service our request,
4282 * then we can just use them as-is.
4283 */
4284 if (have_dvas >= need_dvas) {
4285 if (dde_io != NULL)
4286 mutex_exit(&dde_io->dde_io_lock);
4287
4288 /*
4289 * For rewrite operations, try preserving the original
4290 * logical birth time. If the result matches the
4291 * original BP, this becomes a NOP.
4292 */
4293 if (zp->zp_rewrite) {
4294 uint64_t orig_logical_birth =
4295 BP_GET_LOGICAL_BIRTH(&zio->io_bp_orig);
4296 ddt_bp_fill(ddp, v, bp, orig_logical_birth);
4297 if (BP_EQUAL(bp, &zio->io_bp_orig)) {
4298 /* We can skip accounting. */
4299 ddt_exit(ddt);
4300 zio->io_flags |= ZIO_FLAG_NOPWRITE;
4301 return (zio);
4302 }
4303 }
4304
4305 ddt_bp_fill(ddp, v, bp, txg);
4306 ddt_phys_addref(ddp, v);
4307 ddt_exit(ddt);
4308 return (zio);
4309 }
4310
4311 /*
4312 * Otherwise, we have to issue IO to fill the entry up to the
4313 * amount we need.
4314 */
4315 need_dvas -= have_dvas;
4316 } else {
4317 /*
4318 * There's a write in-flight. If there's already enough DVAs on
4319 * the entry, then either there were already enough to start
4320 * with, or the in-flight IO is between READY and DONE, and so
4321 * has extended the entry with new DVAs. Either way, we don't
4322 * need to do anything, we can just slot in behind it.
4323 */
4324
4325 if (zio->io_bp_override) {
4326 /*
4327 * If there's a write out, then we're soon going to
4328 * have our own copies of this block, so clear out the
4329 * override block and treat it as a regular dedup
4330 * write. See comment above.
4331 */
4332 BP_ZERO_DVAS(bp);
4333 BP_SET_BIRTH(bp, 0, 0);
4334 }
4335
4336 if (have_dvas >= need_dvas) {
4337 /*
4338 * A minor point: there might already be enough
4339 * committed DVAs in the entry to service our request,
4340 * but we don't know which are completed and which are
4341 * allocated but not yet written. In this case, should
4342 * the IO for the new DVAs fail, we will be on the end
4343 * of the IO chain and will also recieve an error, even
4344 * though our request could have been serviced.
4345 *
4346 * This is an extremely rare case, as it requires the
4347 * original block to be copied with a request for a
4348 * larger number of DVAs, then copied again requesting
4349 * the same (or already fulfilled) number of DVAs while
4350 * the first request is active, and then that first
4351 * request errors. In return, the logic required to
4352 * catch and handle it is complex. For now, I'm just
4353 * not going to bother with it.
4354 */
4355
4356 /*
4357 * We always fill the bp here as we may have arrived
4358 * after the in-flight write has passed READY, and so
4359 * missed out.
4360 */
4361 ddt_bp_fill(ddp, v, bp, txg);
4362 piggyback:
4363 zio_add_child(zio, dde_io->dde_lead_zio[p]);
4364
4365 /*
4366 * Optimistically increment refcount for this parent.
4367 * If the write fails, zio_ddt_child_write_done() will
4368 * decrement for all non-DDT-child parents.
4369 */
4370 ddt_phys_addref(ddp, v);
4371 mutex_exit(&dde_io->dde_io_lock);
4372 ddt_exit(ddt);
4373 return (zio);
4374 }
4375
4376 /*
4377 * There's not enough in the entry yet, so we need to look at
4378 * the write in-flight and see how many DVAs it will have once
4379 * it completes.
4380 *
4381 * The in-flight write has potentially had its copies request
4382 * reduced (if we're filling out an existing entry), so we need
4383 * to reach in and get the original write to find out what it is
4384 * expecting.
4385 *
4386 * Note that the parent of the lead zio will always have the
4387 * highest zp_copies of any zio in the chain, because ones that
4388 * can be serviced without additional IO are always added to
4389 * the back of the chain.
4390 */
4391 zio_link_t *zl = NULL;
4392 zio_t *pio =
4393 zio_walk_parents(dde->dde_io->dde_lead_zio[p], &zl);
4394 ASSERT(pio);
4395 parent_dvas = pio->io_prop.zp_copies;
4396
4397 if (parent_dvas >= need_dvas)
4398 goto piggyback;
4399
4400 /*
4401 * Still not enough, so we will need to issue to get the
4402 * shortfall.
4403 */
4404 need_dvas -= parent_dvas;
4405 }
4406
4407 if (is_ganged) {
4408 if (dde_io != NULL)
4409 mutex_exit(&dde_io->dde_io_lock);
4410 ddt_exit(ddt);
4411 zp->zp_dedup = B_FALSE;
4412 BP_SET_DEDUP(bp, B_FALSE);
4413 zio->io_pipeline = ZIO_WRITE_PIPELINE;
4414 return (zio);
4415 }
4416
4417 /*
4418 * We need to write. We will create a new write with the copies
4419 * property adjusted to match the number of DVAs we need to grow
4420 * the DDT entry by to satisfy the request.
4421 */
4422 zio_prop_t czp;
4423 if (have_dvas > 0 || parent_dvas > 0) {
4424 czp = *zp;
4425 czp.zp_copies = need_dvas;
4426 czp.zp_gang_copies = 0;
4427 zp = &czp;
4428 } else {
4429 ASSERT3U(zp->zp_copies, ==, need_dvas);
4430 }
4431
4432 zio_t *cio = zio_write(zio, spa, txg, bp, zio->io_orig_abd,
4433 zio->io_orig_size, zio->io_orig_size, zp,
4434 zio_ddt_child_write_ready, NULL,
4435 zio_ddt_child_write_done, dde, zio->io_priority,
4436 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
4437 zio_inherit_allocator(zio, cio);
4438
4439 zio_push_transform(cio, zio->io_abd, zio->io_size, 0, NULL);
4440
4441 /*
4442 * We are the new lead zio, because our parent has the highest
4443 * zp_copies that has been requested for this entry so far.
4444 */
4445 if (dde_io == NULL) {
4446 /*
4447 * New dde_io. No lock needed since no other thread can have
4448 * a reference yet.
4449 */
4450 ddt_alloc_entry_io(dde);
4451 dde_io = dde->dde_io;
4452 /*
4453 * First time out, take a copy of the stable entry to revert
4454 * to if there's an error (see zio_ddt_child_write_done())
4455 */
4456 ddt_phys_copy(&dde_io->dde_orig_phys, dde->dde_phys, v);
4457 dde_io->dde_lead_zio[p] = cio;
4458 } else {
4459 if (dde_io->dde_lead_zio[p] == NULL) {
4460 /*
4461 * First time out, take a copy of the stable entry
4462 * to revert to if there's an error (see
4463 * zio_ddt_child_write_done())
4464 */
4465 ddt_phys_copy(&dde_io->dde_orig_phys, dde->dde_phys,
4466 v);
4467 } else {
4468 /*
4469 * Make the existing chain our child, because it
4470 * cannot complete until we have.
4471 */
4472 zio_add_child(cio, dde_io->dde_lead_zio[p]);
4473 }
4474 dde_io->dde_lead_zio[p] = cio;
4475 mutex_exit(&dde_io->dde_io_lock);
4476 }
4477
4478 /*
4479 * Optimistically increment the refcount for this dedup write.
4480 * If the write fails, zio_ddt_child_write_done() will decrement
4481 * for all non-DDT-child parents.
4482 */
4483 ddt_phys_addref(ddp, v);
4484
4485 ddt_exit(ddt);
4486
4487 zio_nowait(cio);
4488
4489 return (zio);
4490 }
4491
4492 static ddt_entry_t *freedde; /* for debugging */
4493
4494 static zio_t *
zio_ddt_free(zio_t * zio)4495 zio_ddt_free(zio_t *zio)
4496 {
4497 spa_t *spa = zio->io_spa;
4498 blkptr_t *bp = zio->io_bp;
4499 ddt_t *ddt = ddt_select(spa, bp);
4500 ddt_entry_t *dde = NULL;
4501
4502 ASSERT(BP_GET_DEDUP(bp));
4503 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
4504
4505 ddt_enter(ddt);
4506 freedde = dde = ddt_lookup(ddt, bp, B_TRUE);
4507 if (dde) {
4508 ddt_phys_variant_t v = ddt_phys_select(ddt, dde, bp);
4509 if (v != DDT_PHYS_NONE)
4510 ddt_phys_decref(dde->dde_phys, v);
4511 else
4512 /*
4513 * No phys matches this BP; ddt_lookup() returned a
4514 * fresh, empty entry because the key is not in the
4515 * table at all (eg the original entry was pruned).
4516 * There is no reference to release, so we need to do
4517 * a normal (not dedup) free. Clear dde so we fall
4518 * into the block below.
4519 */
4520 dde = NULL;
4521 }
4522 ddt_exit(ddt);
4523
4524 if (dde) {
4525 /*
4526 * DDT entry found and the refcount has been decremented.
4527 * Stop the pipeline — there is nothing more to do right now.
4528 */
4529 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
4530 } else {
4531 /*
4532 * No DDT entry; the block must have been pruned from the
4533 * table. Clear the DEDUP bit so it is treated as a normal
4534 * block from here on. BRT_FREE and DVA_FREE follow in the
4535 * pipeline and will handle any cloned references and the
4536 * actual block free respectively, along with the gang stages
4537 * for a gang BP.
4538 *
4539 * Only flat (FDT) tables are ever pruned, so a miss against
4540 * a traditional table means the table and the BP disagree,
4541 * which should not be possible. The plain free below is
4542 * still the best we can do for this BP, but leave a trace.
4543 */
4544 if (!(ddt->ddt_flags & DDT_FLAG_FLAT)) {
4545 zfs_dbgmsg("%s: no matching traditional DDT phys for "
4546 "dedup BP DVA[0]=<%llu:%llx:%llx> phys_birth=%llu; "
4547 "freeing without a refcount decrement",
4548 spa_name(spa),
4549 (u_longlong_t)DVA_GET_VDEV(&bp->blk_dva[0]),
4550 (u_longlong_t)DVA_GET_OFFSET(&bp->blk_dva[0]),
4551 (u_longlong_t)DVA_GET_ASIZE(&bp->blk_dva[0]),
4552 (u_longlong_t)BP_GET_PHYSICAL_BIRTH(bp));
4553 }
4554 BP_SET_DEDUP(bp, 0);
4555 }
4556
4557 return (zio);
4558 }
4559
4560 /*
4561 * ==========================================================================
4562 * Allocate and free blocks
4563 * ==========================================================================
4564 */
4565
4566 static zio_t *
zio_io_to_allocate(metaslab_class_allocator_t * mca,boolean_t * more)4567 zio_io_to_allocate(metaslab_class_allocator_t *mca, boolean_t *more)
4568 {
4569 zio_t *zio;
4570
4571 ASSERT(MUTEX_HELD(&mca->mca_lock));
4572
4573 zio = avl_first(&mca->mca_tree);
4574 if (zio == NULL) {
4575 *more = B_FALSE;
4576 return (NULL);
4577 }
4578
4579 ASSERT(IO_IS_ALLOCATING(zio));
4580 ASSERT(ZIO_HAS_ALLOCATOR(zio));
4581
4582 /*
4583 * Try to place a reservation for this zio. If we're unable to
4584 * reserve then we throttle.
4585 */
4586 if (!metaslab_class_throttle_reserve(zio->io_metaslab_class,
4587 zio->io_allocator, zio->io_prop.zp_copies, zio->io_size,
4588 B_FALSE, more)) {
4589 return (NULL);
4590 }
4591 zio->io_flags |= ZIO_FLAG_ALLOC_THROTTLED;
4592
4593 avl_remove(&mca->mca_tree, zio);
4594 ASSERT3U(zio->io_stage, <, ZIO_STAGE_DVA_ALLOCATE);
4595
4596 if (avl_is_empty(&mca->mca_tree))
4597 *more = B_FALSE;
4598 return (zio);
4599 }
4600
4601 static zio_t *
zio_dva_throttle(zio_t * zio)4602 zio_dva_throttle(zio_t *zio)
4603 {
4604 spa_t *spa = zio->io_spa;
4605 zio_t *nio;
4606 metaslab_class_t *mc;
4607 boolean_t more;
4608
4609 /*
4610 * If not already chosen, choose an appropriate allocation class.
4611 */
4612 mc = zio->io_metaslab_class;
4613 if (mc == NULL)
4614 mc = spa_preferred_class(spa, zio);
4615
4616 if (zio->io_priority == ZIO_PRIORITY_SYNC_WRITE ||
4617 !mc->mc_alloc_throttle_enabled ||
4618 zio->io_child_type == ZIO_CHILD_GANG ||
4619 zio->io_flags & ZIO_FLAG_NODATA) {
4620 return (zio);
4621 }
4622
4623 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
4624 ASSERT(ZIO_HAS_ALLOCATOR(zio));
4625 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
4626 ASSERT3U(zio->io_queued_timestamp, >, 0);
4627 ASSERT(zio->io_stage == ZIO_STAGE_DVA_THROTTLE);
4628
4629 zio->io_metaslab_class = mc;
4630 metaslab_class_allocator_t *mca = &mc->mc_allocator[zio->io_allocator];
4631 mutex_enter(&mca->mca_lock);
4632 avl_add(&mca->mca_tree, zio);
4633 nio = zio_io_to_allocate(mca, &more);
4634 mutex_exit(&mca->mca_lock);
4635 return (nio);
4636 }
4637
4638 static void
zio_allocate_dispatch(metaslab_class_t * mc,int allocator)4639 zio_allocate_dispatch(metaslab_class_t *mc, int allocator)
4640 {
4641 metaslab_class_allocator_t *mca = &mc->mc_allocator[allocator];
4642 zio_t *zio;
4643 boolean_t more;
4644
4645 do {
4646 mutex_enter(&mca->mca_lock);
4647 zio = zio_io_to_allocate(mca, &more);
4648 mutex_exit(&mca->mca_lock);
4649 if (zio == NULL)
4650 return;
4651
4652 ASSERT3U(zio->io_stage, ==, ZIO_STAGE_DVA_THROTTLE);
4653 ASSERT0(zio->io_error);
4654 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_TRUE);
4655 } while (more);
4656 }
4657
4658 static zio_t *
zio_dva_allocate(zio_t * zio)4659 zio_dva_allocate(zio_t *zio)
4660 {
4661 spa_t *spa = zio->io_spa;
4662 metaslab_class_t *mc, *newmc;
4663 blkptr_t *bp = zio->io_bp;
4664 int error;
4665 int flags = 0;
4666
4667 if (zio->io_gang_leader == NULL) {
4668 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
4669 zio->io_gang_leader = zio;
4670 }
4671 if (zio->io_flags & ZIO_FLAG_PREALLOCATED) {
4672 ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_GANG);
4673 memcpy(zio->io_bp->blk_dva, zio->io_bp_orig.blk_dva,
4674 3 * sizeof (dva_t));
4675 BP_SET_LOGICAL_BIRTH(zio->io_bp,
4676 BP_GET_LOGICAL_BIRTH(&zio->io_bp_orig));
4677 BP_SET_PHYSICAL_BIRTH(zio->io_bp,
4678 BP_GET_RAW_PHYSICAL_BIRTH(&zio->io_bp_orig));
4679 return (zio);
4680 }
4681
4682 ASSERT(BP_IS_HOLE(bp));
4683 ASSERT0(BP_GET_NDVAS(bp));
4684 ASSERT3U(zio->io_prop.zp_copies, >, 0);
4685
4686 ASSERT3U(zio->io_prop.zp_copies, <=, spa_max_replication(spa));
4687 ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
4688
4689 if (zio->io_flags & ZIO_FLAG_GANG_CHILD)
4690 flags |= METASLAB_GANG_CHILD;
4691 if (zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE)
4692 flags |= METASLAB_ASYNC_ALLOC;
4693
4694 /*
4695 * If not already chosen, choose an appropriate allocation class.
4696 */
4697 mc = zio->io_metaslab_class;
4698 if (mc == NULL) {
4699 mc = spa_preferred_class(spa, zio);
4700 zio->io_metaslab_class = mc;
4701 }
4702 ZIOSTAT_BUMP(ziostat_total_allocations);
4703
4704 again:
4705 /*
4706 * Try allocating the block in the usual metaslab class.
4707 * If that's full, allocate it in some other class(es).
4708 * If that's full, allocate as a gang block,
4709 * and if all are full, the allocation fails (which shouldn't happen).
4710 *
4711 * Note that we do not fall back on embedded slog (ZIL) space, to
4712 * preserve unfragmented slog space, which is critical for decent
4713 * sync write performance. If a log allocation fails, we will fall
4714 * back to spa_sync() which is abysmal for performance.
4715 */
4716 ASSERT(ZIO_HAS_ALLOCATOR(zio));
4717 error = metaslab_alloc(spa, mc, zio->io_size, bp,
4718 zio->io_prop.zp_copies, zio->io_txg, NULL, flags,
4719 ZIO_ALLOC_LIST(zio), zio->io_allocator, zio);
4720
4721 /*
4722 * When the dedup or special class is spilling into the normal class,
4723 * there can still be significant space available due to deferred
4724 * frees that are in-flight. We track the txg when this occurred and
4725 * back off adding new DDT entries for a few txgs to allow the free
4726 * blocks to be processed.
4727 */
4728 if (error == ENOSPC && spa->spa_dedup_class_full_txg != zio->io_txg &&
4729 (mc == spa_dedup_class(spa) || (mc == spa_special_class(spa) &&
4730 !spa_has_dedup(spa) && spa_special_has_ddt(spa)))) {
4731 spa->spa_dedup_class_full_txg = zio->io_txg;
4732 zfs_dbgmsg("%s[%llu]: %s class spilling, req size %llu, "
4733 "%llu allocated of %llu",
4734 spa_name(spa), (u_longlong_t)zio->io_txg,
4735 metaslab_class_get_name(mc),
4736 (u_longlong_t)zio->io_size,
4737 (u_longlong_t)metaslab_class_get_alloc(mc),
4738 (u_longlong_t)metaslab_class_get_space(mc));
4739 }
4740
4741 /*
4742 * Fall back to some other class when this one is full.
4743 */
4744 if (error == ENOSPC && (newmc = spa_preferred_class(spa, zio)) != mc) {
4745 /*
4746 * If we are holding old class reservation, drop it.
4747 * Dispatch the next ZIO(s) there if some are waiting.
4748 */
4749 if (zio->io_flags & ZIO_FLAG_ALLOC_THROTTLED) {
4750 if (metaslab_class_throttle_unreserve(mc,
4751 zio->io_allocator, zio->io_prop.zp_copies,
4752 zio->io_size)) {
4753 zio_allocate_dispatch(zio->io_metaslab_class,
4754 zio->io_allocator);
4755 }
4756 zio->io_flags &= ~ZIO_FLAG_ALLOC_THROTTLED;
4757 }
4758
4759 if (zfs_flags & ZFS_DEBUG_METASLAB_ALLOC) {
4760 zfs_dbgmsg("%s: metaslab allocation failure in %s "
4761 "class, trying fallback to %s class: zio %px, "
4762 "size %llu, error %d", spa_name(spa),
4763 metaslab_class_get_name(mc),
4764 metaslab_class_get_name(newmc),
4765 zio, (u_longlong_t)zio->io_size, error);
4766 }
4767 zio->io_metaslab_class = mc = newmc;
4768 ZIOSTAT_BUMP(ziostat_alloc_class_fallbacks);
4769
4770 /*
4771 * If the new class uses throttling, return to that pipeline
4772 * stage. Otherwise just do another allocation attempt.
4773 */
4774 if (zio->io_priority != ZIO_PRIORITY_SYNC_WRITE &&
4775 mc->mc_alloc_throttle_enabled &&
4776 zio->io_child_type != ZIO_CHILD_GANG &&
4777 !(zio->io_flags & ZIO_FLAG_NODATA)) {
4778 zio->io_stage = ZIO_STAGE_DVA_THROTTLE >> 1;
4779 return (zio);
4780 }
4781 goto again;
4782 }
4783
4784 if (error == ENOSPC && zio->io_size > spa->spa_min_alloc) {
4785 if (zfs_flags & ZFS_DEBUG_METASLAB_ALLOC) {
4786 zfs_dbgmsg("%s: metaslab allocation failure, "
4787 "trying ganging: zio %px, size %llu, error %d",
4788 spa_name(spa), zio, (u_longlong_t)zio->io_size,
4789 error);
4790 }
4791 ZIOSTAT_BUMP(ziostat_gang_writes);
4792 if (flags & METASLAB_GANG_CHILD)
4793 ZIOSTAT_BUMP(ziostat_gang_multilevel);
4794 return (zio_write_gang_block(zio, mc));
4795 }
4796 if (error != 0) {
4797 if (error != ENOSPC ||
4798 (zfs_flags & ZFS_DEBUG_METASLAB_ALLOC)) {
4799 zfs_dbgmsg("%s: metaslab allocation failure: zio %px, "
4800 "size %llu, error %d",
4801 spa_name(spa), zio, (u_longlong_t)zio->io_size,
4802 error);
4803 }
4804 zio->io_error = error;
4805 } else if (zio->io_prop.zp_rewrite) {
4806 /*
4807 * For rewrite operations, preserve the logical birth time
4808 * but set the physical birth time to the current txg.
4809 */
4810 uint64_t logical_birth = BP_GET_LOGICAL_BIRTH(&zio->io_bp_orig);
4811 ASSERT3U(logical_birth, <=, zio->io_txg);
4812 BP_SET_BIRTH(zio->io_bp, logical_birth, zio->io_txg);
4813 BP_SET_REWRITE(zio->io_bp, 1);
4814 }
4815
4816 return (zio);
4817 }
4818
4819 static zio_t *
zio_dva_free(zio_t * zio)4820 zio_dva_free(zio_t *zio)
4821 {
4822 metaslab_free(zio->io_spa, zio->io_bp, zio->io_txg, B_FALSE);
4823
4824 return (zio);
4825 }
4826
4827 static zio_t *
zio_dva_claim(zio_t * zio)4828 zio_dva_claim(zio_t *zio)
4829 {
4830 int error;
4831
4832 error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg);
4833 if (error)
4834 zio->io_error = error;
4835
4836 return (zio);
4837 }
4838
4839 /*
4840 * Undo an allocation. This is used by zio_done() when an I/O fails
4841 * and we want to give back the block we just allocated.
4842 * This handles both normal blocks and gang blocks.
4843 */
4844 static void
zio_dva_unallocate(zio_t * zio,zio_gang_node_t * gn,blkptr_t * bp)4845 zio_dva_unallocate(zio_t *zio, zio_gang_node_t *gn, blkptr_t *bp)
4846 {
4847 ASSERT(BP_GET_BIRTH(bp) == zio->io_txg || BP_IS_HOLE(bp));
4848 ASSERT0P(zio->io_bp_override);
4849
4850 if (!BP_IS_HOLE(bp)) {
4851 metaslab_free(zio->io_spa, bp, BP_GET_BIRTH(bp), B_TRUE);
4852 }
4853
4854 if (gn != NULL) {
4855 for (int g = 0; g < gbh_nblkptrs(gn->gn_gangblocksize); g++) {
4856 zio_dva_unallocate(zio, gn->gn_child[g],
4857 gbh_bp(gn->gn_gbh, g));
4858 }
4859 }
4860 }
4861
4862 /*
4863 * Try to allocate an intent log block. Return 0 on success, errno on failure.
4864 */
4865 int
zio_alloc_zil(spa_t * spa,objset_t * os,uint64_t txg,blkptr_t * new_bp,uint64_t min_size,uint64_t max_size,boolean_t * slog,boolean_t allow_larger)4866 zio_alloc_zil(spa_t *spa, objset_t *os, uint64_t txg, blkptr_t *new_bp,
4867 uint64_t min_size, uint64_t max_size, boolean_t *slog,
4868 boolean_t allow_larger)
4869 {
4870 int error;
4871 zio_alloc_list_t io_alloc_list;
4872 uint64_t alloc_size = 0;
4873
4874 ASSERT(txg > spa_syncing_txg(spa));
4875 ASSERT3U(min_size, <=, max_size);
4876
4877 metaslab_trace_init(&io_alloc_list);
4878
4879 /*
4880 * Block pointer fields are useful to metaslabs for stats and debugging.
4881 * Fill in the obvious ones before calling into metaslab_alloc().
4882 */
4883 BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
4884 BP_SET_PSIZE(new_bp, max_size);
4885 BP_SET_LEVEL(new_bp, 0);
4886
4887 /*
4888 * When allocating a zil block, we don't have information about
4889 * the final destination of the block except the objset it's part
4890 * of, so we just hash the objset ID to pick the allocator to get
4891 * some parallelism.
4892 */
4893 int flags = METASLAB_ZIL;
4894 int allocator = (uint_t)cityhash1(os->os_dsl_dataset->ds_object)
4895 % spa->spa_alloc_count;
4896 ZIOSTAT_BUMP(ziostat_total_allocations);
4897
4898 /* Try log class (dedicated slog devices) first */
4899 error = metaslab_alloc_range(spa, spa_log_class(spa), min_size,
4900 max_size, new_bp, 1, txg, NULL, flags, &io_alloc_list, allocator,
4901 NULL, &alloc_size);
4902 *slog = (error == 0);
4903
4904 /* Try special_embedded_log class (reserved on special vdevs) */
4905 if (error != 0) {
4906 error = metaslab_alloc_range(spa,
4907 spa_special_embedded_log_class(spa), min_size, max_size,
4908 new_bp, 1, txg, NULL, flags, &io_alloc_list, allocator,
4909 NULL, &alloc_size);
4910 }
4911
4912 /* Try special class (general special vdev allocation) */
4913 if (error != 0) {
4914 error = metaslab_alloc_range(spa, spa_special_class(spa),
4915 min_size, max_size, new_bp, 1, txg, NULL, flags,
4916 &io_alloc_list, allocator, NULL, &alloc_size);
4917 }
4918
4919 /* Try embedded_log class (reserved on normal vdevs) */
4920 if (error != 0) {
4921 error = metaslab_alloc_range(spa, spa_embedded_log_class(spa),
4922 min_size, max_size, new_bp, 1, txg, NULL, flags,
4923 &io_alloc_list, allocator, NULL, &alloc_size);
4924 }
4925
4926 /* Finally fall back to normal class */
4927 if (error != 0) {
4928 ZIOSTAT_BUMP(ziostat_alloc_class_fallbacks);
4929 error = metaslab_alloc_range(spa, spa_normal_class(spa),
4930 min_size, max_size, new_bp, 1, txg, NULL, flags,
4931 &io_alloc_list, allocator, NULL, &alloc_size);
4932 }
4933 metaslab_trace_fini(&io_alloc_list);
4934
4935 if (error == 0) {
4936 if (!allow_larger)
4937 alloc_size = MIN(alloc_size, max_size);
4938 else if (max_size <= SPA_OLD_MAXBLOCKSIZE)
4939 alloc_size = MIN(alloc_size, SPA_OLD_MAXBLOCKSIZE);
4940 alloc_size = P2ALIGN_TYPED(alloc_size, ZIL_MIN_BLKSZ, uint64_t);
4941
4942 BP_SET_LSIZE(new_bp, alloc_size);
4943 BP_SET_PSIZE(new_bp, alloc_size);
4944 BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF);
4945 BP_SET_CHECKSUM(new_bp,
4946 spa_version(spa) >= SPA_VERSION_SLIM_ZIL
4947 ? ZIO_CHECKSUM_ZILOG2 : ZIO_CHECKSUM_ZILOG);
4948 BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
4949 BP_SET_LEVEL(new_bp, 0);
4950 BP_SET_DEDUP(new_bp, 0);
4951 BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER);
4952
4953 /*
4954 * encrypted blocks will require an IV and salt. We generate
4955 * these now since we will not be rewriting the bp at
4956 * rewrite time.
4957 */
4958 if (os->os_encrypted) {
4959 uint8_t iv[ZIO_DATA_IV_LEN];
4960 uint8_t salt[ZIO_DATA_SALT_LEN];
4961
4962 BP_SET_CRYPT(new_bp, B_TRUE);
4963 VERIFY0(spa_crypt_get_salt(spa,
4964 dmu_objset_id(os), salt));
4965 VERIFY0(zio_crypt_generate_iv(iv));
4966
4967 zio_crypt_encode_params_bp(new_bp, salt, iv);
4968 }
4969 } else {
4970 zfs_dbgmsg("%s: zil block allocation failure: "
4971 "min_size %llu, max_size %llu, error %d", spa_name(spa),
4972 (u_longlong_t)min_size, (u_longlong_t)max_size, error);
4973 }
4974
4975 return (error);
4976 }
4977
4978 /*
4979 * ==========================================================================
4980 * Read and write to physical devices
4981 * ==========================================================================
4982 */
4983
4984 /*
4985 * Issue an I/O to the underlying vdev. Typically the issue pipeline
4986 * stops after this stage and will resume upon I/O completion.
4987 * However, there are instances where the vdev layer may need to
4988 * continue the pipeline when an I/O was not issued. Since the I/O
4989 * that was sent to the vdev layer might be different than the one
4990 * currently active in the pipeline (see vdev_queue_io()), we explicitly
4991 * force the underlying vdev layers to call either zio_execute() or
4992 * zio_interrupt() to ensure that the pipeline continues with the correct I/O.
4993 */
4994 static zio_t *
zio_vdev_io_start(zio_t * zio)4995 zio_vdev_io_start(zio_t *zio)
4996 {
4997 vdev_t *vd = zio->io_vd;
4998 uint64_t align;
4999 spa_t *spa = zio->io_spa;
5000
5001 zio->io_delta = 0;
5002 zio->io_delay = 0;
5003
5004 ASSERT0(zio->io_error);
5005 ASSERT0(zio->io_child_error[ZIO_CHILD_VDEV]);
5006
5007 if (vd == NULL) {
5008 if (!(zio->io_flags & ZIO_FLAG_CONFIG_WRITER)) {
5009 /*
5010 * A deadlock workaround. The ddt_prune_unique_entries()
5011 * -> prune_candidates_sync() code path takes the
5012 * SCL_ZIO reader lock and may request it again here.
5013 * If there is another thread who wants the SCL_ZIO
5014 * writer lock, then scl_write_wanted will be set.
5015 * Thus, the spa_config_enter_priority() is used to
5016 * ignore pending writer requests.
5017 *
5018 * The locking should be revised to remove the need
5019 * for this workaround. If that's not workable then
5020 * it should only be applied to the zios involved in
5021 * the pruning process. This impacts the read/write
5022 * I/O balance while pruning.
5023 */
5024 if (spa->spa_active_ddt_prune)
5025 spa_config_enter_priority(spa, SCL_ZIO, zio,
5026 RW_READER);
5027 else
5028 spa_config_enter(spa, SCL_ZIO, zio,
5029 RW_READER);
5030 }
5031
5032 /*
5033 * The mirror_ops handle multiple DVAs in a single BP.
5034 */
5035 vdev_mirror_ops.vdev_op_io_start(zio);
5036 return (NULL);
5037 }
5038
5039 ASSERT3P(zio->io_logical, !=, zio);
5040 if (zio->io_type == ZIO_TYPE_WRITE) {
5041 ASSERT(spa->spa_trust_config);
5042
5043 /*
5044 * Note: the code can handle other kinds of writes,
5045 * but we don't expect them.
5046 */
5047 if (zio->io_vd->vdev_noalloc) {
5048 ASSERT(zio->io_flags &
5049 (ZIO_FLAG_PHYSICAL | ZIO_FLAG_SELF_HEAL |
5050 ZIO_FLAG_RESILVER | ZIO_FLAG_INDUCE_DAMAGE));
5051 }
5052 }
5053
5054 align = 1ULL << vd->vdev_top->vdev_ashift;
5055
5056 if (!(zio->io_flags & ZIO_FLAG_PHYSICAL) &&
5057 P2PHASE(zio->io_size, align) != 0) {
5058 /* Transform logical writes to be a full physical block size. */
5059 uint64_t asize = P2ROUNDUP(zio->io_size, align);
5060 abd_t *abuf = abd_alloc_sametype(zio->io_abd, asize);
5061 ASSERT(vd == vd->vdev_top);
5062 if (zio->io_type == ZIO_TYPE_WRITE) {
5063 abd_copy(abuf, zio->io_abd, zio->io_size);
5064 abd_zero_off(abuf, zio->io_size, asize - zio->io_size);
5065 }
5066 zio_push_transform(zio, abuf, asize, asize, zio_subblock);
5067 }
5068
5069 /*
5070 * If this is not a physical io, make sure that it is properly aligned
5071 * before proceeding.
5072 */
5073 if (!(zio->io_flags & ZIO_FLAG_PHYSICAL)) {
5074 ASSERT0(P2PHASE(zio->io_offset, align));
5075 ASSERT0(P2PHASE(zio->io_size, align));
5076 } else {
5077 /*
5078 * For physical writes, we allow 512b aligned writes and assume
5079 * the device will perform a read-modify-write as necessary.
5080 */
5081 ASSERT0(P2PHASE(zio->io_offset, SPA_MINBLOCKSIZE));
5082 ASSERT0(P2PHASE(zio->io_size, SPA_MINBLOCKSIZE));
5083 }
5084
5085 VERIFY(zio->io_type != ZIO_TYPE_WRITE || spa_writeable(spa));
5086
5087 /*
5088 * If this is a repair I/O, and there's no self-healing involved --
5089 * that is, we're just resilvering what we expect to resilver --
5090 * then don't do the I/O unless zio's txg is actually in vd's DTL.
5091 * This prevents spurious resilvering.
5092 *
5093 * There are a few ways that we can end up creating these spurious
5094 * resilver i/os:
5095 *
5096 * 1. A resilver i/o will be issued if any DVA in the BP has a
5097 * dirty DTL. The mirror code will issue resilver writes to
5098 * each DVA, including the one(s) that are not on vdevs with dirty
5099 * DTLs.
5100 *
5101 * 2. With nested replication, which happens when we have a
5102 * "replacing" or "spare" vdev that's a child of a mirror or raidz.
5103 * For example, given mirror(replacing(A+B), C), it's likely that
5104 * only A is out of date (it's the new device). In this case, we'll
5105 * read from C, then use the data to resilver A+B -- but we don't
5106 * actually want to resilver B, just A. The top-level mirror has no
5107 * way to know this, so instead we just discard unnecessary repairs
5108 * as we work our way down the vdev tree.
5109 *
5110 * 3. ZTEST also creates mirrors of mirrors, mirrors of raidz, etc.
5111 * The same logic applies to any form of nested replication: ditto
5112 * + mirror, RAID-Z + replacing, etc.
5113 *
5114 * However, indirect vdevs point off to other vdevs which may have
5115 * DTL's, so we never bypass them. The child i/os on concrete vdevs
5116 * will be properly bypassed instead.
5117 *
5118 * Leaf DTL_PARTIAL can be empty when a legitimate write comes from
5119 * a dRAID spare vdev. For example, when a dRAID spare is first
5120 * used, its spare blocks need to be written to but the leaf vdev's
5121 * of such blocks can have empty DTL_PARTIAL.
5122 *
5123 * There seemed no clean way to allow such writes while bypassing
5124 * spurious ones. At this point, just avoid all bypassing for dRAID
5125 * for correctness.
5126 */
5127 if ((zio->io_flags & ZIO_FLAG_IO_REPAIR) &&
5128 !(zio->io_flags & ZIO_FLAG_SELF_HEAL) &&
5129 zio->io_txg != 0 && /* not a delegated i/o */
5130 vd->vdev_ops != &vdev_indirect_ops &&
5131 vd->vdev_top->vdev_ops != &vdev_draid_ops &&
5132 !vdev_dtl_contains(vd, DTL_PARTIAL, zio->io_txg, 1)) {
5133 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
5134 zio_vdev_io_bypass(zio);
5135 return (zio);
5136 }
5137
5138 /*
5139 * Select the next best leaf I/O to process. Distributed spares are
5140 * excluded since they dispatch the I/O directly to a leaf vdev after
5141 * applying the dRAID mapping.
5142 */
5143 if (vd->vdev_ops->vdev_op_leaf &&
5144 vd->vdev_ops != &vdev_draid_spare_ops &&
5145 (zio->io_type == ZIO_TYPE_READ ||
5146 zio->io_type == ZIO_TYPE_WRITE ||
5147 zio->io_type == ZIO_TYPE_TRIM)) {
5148
5149 if ((zio = vdev_queue_io(zio)) == NULL)
5150 return (NULL);
5151
5152 if (!vdev_accessible(vd, zio)) {
5153 zio->io_error = SET_ERROR(ENXIO);
5154 zio_interrupt(zio);
5155 return (NULL);
5156 }
5157 zio->io_delay = gethrtime();
5158
5159 int error = zio_handle_device_injections(vd, zio, ENOSYS,
5160 EFAULT);
5161 if (error == ENOSYS || (error == EFAULT &&
5162 !(zio->io_flags & ZIO_FLAG_IO_REPAIR))) {
5163 /*
5164 * "no-op" injections return success, but do no actual
5165 * work. Just return it. "io-prefail" injections are
5166 * similar, but don't return success.
5167 */
5168 if (error == EFAULT)
5169 zio->io_error = EIO;
5170 zio_delay_interrupt(zio);
5171 return (NULL);
5172 }
5173 }
5174
5175 vd->vdev_ops->vdev_op_io_start(zio);
5176 return (NULL);
5177 }
5178
5179 static zio_t *
zio_vdev_io_done(zio_t * zio)5180 zio_vdev_io_done(zio_t *zio)
5181 {
5182 vdev_t *vd = zio->io_vd;
5183 vdev_ops_t *ops = vd ? vd->vdev_ops : &vdev_mirror_ops;
5184 boolean_t unexpected_error = B_FALSE;
5185
5186 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV_BIT, ZIO_WAIT_DONE)) {
5187 return (NULL);
5188 }
5189
5190 ASSERT(zio->io_type == ZIO_TYPE_READ ||
5191 zio->io_type == ZIO_TYPE_WRITE ||
5192 zio->io_type == ZIO_TYPE_FLUSH ||
5193 zio->io_type == ZIO_TYPE_TRIM);
5194
5195 if (zio->io_delay) {
5196 /* io_delta is set only if the completion was deferred. */
5197 zio->io_delay = (zio->io_delta != 0 ?
5198 zio->io_timestamp + zio->io_delta : gethrtime()) -
5199 zio->io_delay;
5200 }
5201
5202 if (vd != NULL && vd->vdev_ops->vdev_op_leaf &&
5203 vd->vdev_ops != &vdev_draid_spare_ops) {
5204 if (zio->io_type != ZIO_TYPE_FLUSH)
5205 vdev_queue_io_done(zio);
5206
5207 if (zio_injection_enabled && zio->io_error == 0)
5208 zio->io_error = zio_handle_device_injections(vd, zio,
5209 EIO, EILSEQ);
5210
5211 if (zio_injection_enabled && zio->io_error == 0)
5212 zio->io_error = zio_handle_label_injection(zio, EIO);
5213
5214 if (zio->io_error && zio->io_type != ZIO_TYPE_FLUSH &&
5215 zio->io_type != ZIO_TYPE_TRIM) {
5216 if (!vdev_accessible(vd, zio)) {
5217 zio->io_error = SET_ERROR(ENXIO);
5218 } else {
5219 unexpected_error = B_TRUE;
5220 }
5221 }
5222 }
5223
5224 /*
5225 * This zio got here on a pipeline thread rather than from the block
5226 * layer, so it runs its own completion and gives up its membership.
5227 * The batch is chained only below, to keep it clear of whatever
5228 * vdev_op_io_done() may do with this zio.
5229 */
5230 zio_t *batch = zio_batch_leave(zio);
5231
5232 ops->vdev_op_io_done(zio);
5233
5234 if (unexpected_error && vd->vdev_remove_wanted == B_FALSE)
5235 VERIFY0P(vdev_probe(vd, zio));
5236
5237 zio->io_exec_next = batch;
5238 return (zio);
5239 }
5240
5241 /*
5242 * This function is used to change the priority of an existing zio that is
5243 * currently in-flight. This is used by the arc to upgrade priority in the
5244 * event that a demand read is made for a block that is currently queued
5245 * as a scrub or async read IO. Otherwise, the high priority read request
5246 * would end up having to wait for the lower priority IO.
5247 */
5248 void
zio_change_priority(zio_t * pio,zio_priority_t priority)5249 zio_change_priority(zio_t *pio, zio_priority_t priority)
5250 {
5251 zio_t *cio, *cio_next;
5252 zio_link_t *zl = NULL;
5253
5254 ASSERT3U(priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
5255
5256 if (pio->io_vd != NULL && pio->io_vd->vdev_ops->vdev_op_leaf) {
5257 vdev_queue_change_io_priority(pio, priority);
5258 } else {
5259 pio->io_priority = priority;
5260 }
5261
5262 mutex_enter(&pio->io_lock);
5263 for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) {
5264 cio_next = zio_walk_children(pio, &zl);
5265 zio_change_priority(cio, priority);
5266 }
5267 mutex_exit(&pio->io_lock);
5268 }
5269
5270 /*
5271 * For non-raidz ZIOs, we can just copy aside the bad data read from the
5272 * disk, and use that to finish the checksum ereport later.
5273 */
5274 static void
zio_vsd_default_cksum_finish(zio_cksum_report_t * zcr,const abd_t * good_buf)5275 zio_vsd_default_cksum_finish(zio_cksum_report_t *zcr,
5276 const abd_t *good_buf)
5277 {
5278 /* no processing needed */
5279 zfs_ereport_finish_checksum(zcr, good_buf, zcr->zcr_cbdata, B_FALSE);
5280 }
5281
5282 void
zio_vsd_default_cksum_report(zio_t * zio,zio_cksum_report_t * zcr)5283 zio_vsd_default_cksum_report(zio_t *zio, zio_cksum_report_t *zcr)
5284 {
5285 void *abd = abd_alloc_sametype(zio->io_abd, zio->io_size);
5286
5287 abd_copy(abd, zio->io_abd, zio->io_size);
5288
5289 zcr->zcr_cbinfo = zio->io_size;
5290 zcr->zcr_cbdata = abd;
5291 zcr->zcr_finish = zio_vsd_default_cksum_finish;
5292 zcr->zcr_free = zio_abd_free;
5293 }
5294
5295 static zio_t *
zio_vdev_io_assess(zio_t * zio)5296 zio_vdev_io_assess(zio_t *zio)
5297 {
5298 vdev_t *vd = zio->io_vd;
5299
5300 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV_BIT, ZIO_WAIT_DONE)) {
5301 return (NULL);
5302 }
5303
5304 /* A repair write bypass skips VDEV_IO_DONE entirely. */
5305 zio->io_exec_next = zio_batch_leave(zio);
5306
5307 if (vd == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
5308 spa_config_exit(zio->io_spa, SCL_ZIO, zio);
5309
5310 if (zio->io_vsd != NULL) {
5311 zio->io_vsd_ops->vsd_free(zio);
5312 zio->io_vsd = NULL;
5313 }
5314
5315 /*
5316 * If a Direct I/O operation has a checksum verify error then this I/O
5317 * should not attempt to be issued again.
5318 */
5319 if (zio->io_post & ZIO_POST_DIO_CHKSUM_ERR) {
5320 if (zio->io_type == ZIO_TYPE_WRITE) {
5321 ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_LOGICAL);
5322 ASSERT3U(zio->io_error, ==, EIO);
5323 }
5324 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
5325 return (zio);
5326 }
5327
5328 if (zio_injection_enabled && zio->io_error == 0)
5329 zio->io_error = zio_handle_fault_injection(zio, EIO);
5330
5331 /*
5332 * If the I/O failed, determine whether we should attempt to retry it.
5333 *
5334 * On retry, we cut in line in the issue queue, since we don't want
5335 * compression/checksumming/etc. work to prevent our (cheap) IO reissue.
5336 */
5337 if (zio->io_error && vd == NULL &&
5338 !(zio->io_flags & (ZIO_FLAG_DONT_RETRY | ZIO_FLAG_IO_RETRY))) {
5339 ASSERT(!(zio->io_flags & ZIO_FLAG_DONT_QUEUE)); /* not a leaf */
5340 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_BYPASS)); /* not a leaf */
5341 zio->io_error = 0;
5342 zio->io_flags |= ZIO_FLAG_IO_RETRY | ZIO_FLAG_DONT_AGGREGATE;
5343 zio->io_stage = ZIO_STAGE_VDEV_IO_START >> 1;
5344 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE,
5345 zio_requeue_io_start_cut_in_line);
5346 return (NULL);
5347 }
5348
5349 /*
5350 * If we got an error on a leaf device, convert it to ENXIO
5351 * if the device is not accessible at all.
5352 */
5353 if (zio->io_error && vd != NULL && vd->vdev_ops->vdev_op_leaf &&
5354 !vdev_accessible(vd, zio))
5355 zio->io_error = SET_ERROR(ENXIO);
5356
5357 /*
5358 * If we can't write to an interior vdev (mirror or RAID-Z),
5359 * set vdev_cant_write so that we stop trying to allocate from it.
5360 */
5361 if (zio->io_error == ENXIO && zio->io_type == ZIO_TYPE_WRITE &&
5362 vd != NULL && !vd->vdev_ops->vdev_op_leaf) {
5363 vdev_dbgmsg(vd, "zio_vdev_io_assess(zio=%px) setting "
5364 "cant_write=TRUE due to write failure with ENXIO",
5365 zio);
5366 vd->vdev_cant_write = B_TRUE;
5367 }
5368
5369 /*
5370 * If a cache flush returns ENOTSUP we know that no future
5371 * attempts will ever succeed. In this case we set a persistent
5372 * boolean flag so that we don't bother with it in the future, and
5373 * then we act like the flush succeeded.
5374 */
5375 if (zio->io_error == ENOTSUP && zio->io_type == ZIO_TYPE_FLUSH &&
5376 vd != NULL) {
5377 vd->vdev_nowritecache = B_TRUE;
5378 zio->io_error = 0;
5379 }
5380
5381 if (zio->io_error)
5382 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
5383
5384 return (zio);
5385 }
5386
5387 void
zio_vdev_io_reissue(zio_t * zio)5388 zio_vdev_io_reissue(zio_t *zio)
5389 {
5390 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
5391 ASSERT0(zio->io_error);
5392
5393 zio->io_stage >>= 1;
5394 }
5395
5396 void
zio_vdev_io_redone(zio_t * zio)5397 zio_vdev_io_redone(zio_t *zio)
5398 {
5399 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE);
5400
5401 zio->io_stage >>= 1;
5402 }
5403
5404 void
zio_vdev_io_bypass(zio_t * zio)5405 zio_vdev_io_bypass(zio_t *zio)
5406 {
5407 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
5408 ASSERT0(zio->io_error);
5409
5410 zio->io_flags |= ZIO_FLAG_IO_BYPASS;
5411 zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS >> 1;
5412 }
5413
5414 /*
5415 * ==========================================================================
5416 * Encrypt and store encryption parameters
5417 * ==========================================================================
5418 */
5419
5420
5421 /*
5422 * This function is used for ZIO_STAGE_ENCRYPT. It is responsible for
5423 * managing the storage of encryption parameters and passing them to the
5424 * lower-level encryption functions.
5425 */
5426 static zio_t *
zio_encrypt(zio_t * zio)5427 zio_encrypt(zio_t *zio)
5428 {
5429 zio_prop_t *zp = &zio->io_prop;
5430 spa_t *spa = zio->io_spa;
5431 blkptr_t *bp = zio->io_bp;
5432 uint64_t psize = BP_GET_PSIZE(bp);
5433 uint64_t dsobj = zio->io_bookmark.zb_objset;
5434 dmu_object_type_t ot = BP_GET_TYPE(bp);
5435 void *enc_buf = NULL;
5436 abd_t *eabd = NULL;
5437 uint8_t salt[ZIO_DATA_SALT_LEN];
5438 uint8_t iv[ZIO_DATA_IV_LEN];
5439 uint8_t mac[ZIO_DATA_MAC_LEN];
5440 boolean_t no_crypt = B_FALSE;
5441
5442 /* the root zio already encrypted the data */
5443 if (zio->io_child_type == ZIO_CHILD_GANG)
5444 return (zio);
5445
5446 /* only ZIL blocks are re-encrypted on rewrite */
5447 if (!IO_IS_ALLOCATING(zio) && ot != DMU_OT_INTENT_LOG)
5448 return (zio);
5449
5450 if (!(zp->zp_encrypt || BP_IS_ENCRYPTED(bp))) {
5451 BP_SET_CRYPT(bp, B_FALSE);
5452 return (zio);
5453 }
5454
5455 /* if we are doing raw encryption set the provided encryption params */
5456 if (zio->io_flags & ZIO_FLAG_RAW_ENCRYPT) {
5457 ASSERT0(BP_GET_LEVEL(bp));
5458 BP_SET_CRYPT(bp, B_TRUE);
5459 BP_SET_BYTEORDER(bp, zp->zp_byteorder);
5460 if (ot != DMU_OT_OBJSET)
5461 zio_crypt_encode_mac_bp(bp, zp->zp_mac);
5462
5463 /* dnode blocks must be written out in the provided byteorder */
5464 if (zp->zp_byteorder != ZFS_HOST_BYTEORDER &&
5465 ot == DMU_OT_DNODE) {
5466 void *bswap_buf = zio_buf_alloc(psize);
5467 abd_t *babd = abd_get_from_buf(bswap_buf, psize);
5468
5469 ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF);
5470 abd_copy_to_buf(bswap_buf, zio->io_abd, psize);
5471 dmu_ot_byteswap[DMU_OT_BYTESWAP(ot)].ob_func(bswap_buf,
5472 psize);
5473
5474 abd_take_ownership_of_buf(babd, B_TRUE);
5475 zio_push_transform(zio, babd, psize, psize, NULL);
5476 }
5477
5478 if (DMU_OT_IS_ENCRYPTED(ot))
5479 zio_crypt_encode_params_bp(bp, zp->zp_salt, zp->zp_iv);
5480 return (zio);
5481 }
5482
5483 /* indirect blocks only maintain a cksum of the lower level MACs */
5484 if (BP_GET_LEVEL(bp) > 0) {
5485 BP_SET_CRYPT(bp, B_TRUE);
5486 VERIFY0(zio_crypt_do_indirect_mac_checksum_abd(B_TRUE,
5487 zio->io_orig_abd, BP_GET_LSIZE(bp), BP_SHOULD_BYTESWAP(bp),
5488 mac));
5489 zio_crypt_encode_mac_bp(bp, mac);
5490 return (zio);
5491 }
5492
5493 /*
5494 * Objset blocks are a special case since they have 2 256-bit MACs
5495 * embedded within them.
5496 */
5497 if (ot == DMU_OT_OBJSET) {
5498 ASSERT0(DMU_OT_IS_ENCRYPTED(ot));
5499 ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF);
5500 BP_SET_CRYPT(bp, B_TRUE);
5501 VERIFY0(spa_do_crypt_objset_mac_abd(B_TRUE, spa, dsobj,
5502 zio->io_abd, psize, BP_SHOULD_BYTESWAP(bp)));
5503 return (zio);
5504 }
5505
5506 /* unencrypted object types are only authenticated with a MAC */
5507 if (!DMU_OT_IS_ENCRYPTED(ot)) {
5508 BP_SET_CRYPT(bp, B_TRUE);
5509 VERIFY0(spa_do_crypt_mac_abd(B_TRUE, spa, dsobj,
5510 zio->io_abd, psize, mac));
5511 zio_crypt_encode_mac_bp(bp, mac);
5512 return (zio);
5513 }
5514
5515 /*
5516 * Later passes of sync-to-convergence may decide to rewrite data
5517 * in place to avoid more disk reallocations. This presents a problem
5518 * for encryption because this constitutes rewriting the new data with
5519 * the same encryption key and IV. However, this only applies to blocks
5520 * in the MOS (particularly the spacemaps) and we do not encrypt the
5521 * MOS. We assert that the zio is allocating or an intent log write
5522 * to enforce this.
5523 */
5524 ASSERT(IO_IS_ALLOCATING(zio) || ot == DMU_OT_INTENT_LOG);
5525 ASSERT(BP_GET_LEVEL(bp) == 0 || ot == DMU_OT_INTENT_LOG);
5526 ASSERT(spa_feature_is_active(spa, SPA_FEATURE_ENCRYPTION));
5527 ASSERT3U(psize, !=, 0);
5528
5529 enc_buf = zio_buf_alloc(psize);
5530 eabd = abd_get_from_buf(enc_buf, psize);
5531 abd_take_ownership_of_buf(eabd, B_TRUE);
5532
5533 /*
5534 * For an explanation of what encryption parameters are stored
5535 * where, see the block comment in zio_crypt.c.
5536 */
5537 if (ot == DMU_OT_INTENT_LOG) {
5538 zio_crypt_decode_params_bp(bp, salt, iv);
5539 } else {
5540 BP_SET_CRYPT(bp, B_TRUE);
5541 }
5542
5543 /* Perform the encryption. This should not fail */
5544 VERIFY0(spa_do_crypt_abd(B_TRUE, spa, &zio->io_bookmark,
5545 BP_GET_TYPE(bp), BP_GET_DEDUP(bp), BP_SHOULD_BYTESWAP(bp),
5546 salt, iv, mac, psize, zio->io_abd, eabd, &no_crypt));
5547
5548 /* encode encryption metadata into the bp */
5549 if (ot == DMU_OT_INTENT_LOG) {
5550 /*
5551 * ZIL blocks store the MAC in the embedded checksum, so the
5552 * transform must always be applied.
5553 */
5554 zio_crypt_encode_mac_zil(enc_buf, mac);
5555 zio_push_transform(zio, eabd, psize, psize, NULL);
5556 } else {
5557 BP_SET_CRYPT(bp, B_TRUE);
5558 zio_crypt_encode_params_bp(bp, salt, iv);
5559 zio_crypt_encode_mac_bp(bp, mac);
5560
5561 if (no_crypt) {
5562 ASSERT3U(ot, ==, DMU_OT_DNODE);
5563 abd_free(eabd);
5564 } else {
5565 zio_push_transform(zio, eabd, psize, psize, NULL);
5566 }
5567 }
5568
5569 return (zio);
5570 }
5571
5572 /*
5573 * ==========================================================================
5574 * Generate and verify checksums
5575 * ==========================================================================
5576 */
5577 static zio_t *
zio_checksum_generate(zio_t * zio)5578 zio_checksum_generate(zio_t *zio)
5579 {
5580 blkptr_t *bp = zio->io_bp;
5581 enum zio_checksum checksum;
5582
5583 if (bp == NULL) {
5584 /*
5585 * This is zio_write_phys().
5586 * We're either generating a label checksum, or none at all.
5587 */
5588 checksum = zio->io_prop.zp_checksum;
5589
5590 if (checksum == ZIO_CHECKSUM_OFF)
5591 return (zio);
5592
5593 ASSERT(checksum == ZIO_CHECKSUM_LABEL);
5594 } else {
5595 if (BP_IS_GANG(bp) && zio->io_child_type == ZIO_CHILD_GANG) {
5596 ASSERT(!IO_IS_ALLOCATING(zio));
5597 checksum = ZIO_CHECKSUM_GANG_HEADER;
5598 } else {
5599 checksum = BP_GET_CHECKSUM(bp);
5600 }
5601 }
5602
5603 zio_checksum_compute(zio, checksum, zio->io_abd, zio->io_size);
5604
5605 return (zio);
5606 }
5607
5608 static zio_t *
zio_checksum_verify(zio_t * zio)5609 zio_checksum_verify(zio_t *zio)
5610 {
5611 zio_bad_cksum_t info;
5612 blkptr_t *bp = zio->io_bp;
5613 int error;
5614
5615 ASSERT(zio->io_vd != NULL);
5616
5617 if (bp == NULL) {
5618 /*
5619 * This is zio_read_phys().
5620 * We're either verifying a label checksum, or nothing at all.
5621 */
5622 if (zio->io_prop.zp_checksum == ZIO_CHECKSUM_OFF)
5623 return (zio);
5624
5625 ASSERT3U(zio->io_prop.zp_checksum, ==, ZIO_CHECKSUM_LABEL);
5626 }
5627
5628 ASSERT0(zio->io_post & ZIO_POST_DIO_CHKSUM_ERR);
5629 IMPLY(zio->io_flags & ZIO_FLAG_DIO_READ,
5630 !(zio->io_flags & ZIO_FLAG_SPECULATIVE));
5631
5632 if ((error = zio_checksum_error(zio, &info)) != 0) {
5633 zio->io_error = error;
5634 if (error == ECKSUM &&
5635 !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
5636 if (zio->io_flags & ZIO_FLAG_DIO_READ) {
5637 zio->io_post |= ZIO_POST_DIO_CHKSUM_ERR;
5638 zio_t *pio = zio_unique_parent(zio);
5639 /*
5640 * Any Direct I/O read that has a checksum
5641 * error must be treated as suspicous as the
5642 * contents of the buffer could be getting
5643 * manipulated while the I/O is taking place.
5644 *
5645 * The checksum verify error will only be
5646 * reported here for disk and file VDEV's and
5647 * will be reported on those that the failure
5648 * occurred on. Other types of VDEV's report the
5649 * verify failure in their own code paths.
5650 */
5651 if (pio->io_child_type == ZIO_CHILD_LOGICAL) {
5652 zio_dio_chksum_verify_error_report(zio);
5653 }
5654 } else {
5655 mutex_enter(&zio->io_vd->vdev_stat_lock);
5656 zio->io_vd->vdev_stat.vs_checksum_errors++;
5657 mutex_exit(&zio->io_vd->vdev_stat_lock);
5658 (void) zfs_ereport_start_checksum(zio->io_spa,
5659 zio->io_vd, &zio->io_bookmark, zio,
5660 zio->io_offset, zio->io_size, &info);
5661 }
5662 }
5663 }
5664
5665 return (zio);
5666 }
5667
5668 static zio_t *
zio_dio_checksum_verify(zio_t * zio)5669 zio_dio_checksum_verify(zio_t *zio)
5670 {
5671 zio_t *pio = zio_unique_parent(zio);
5672 int error;
5673
5674 ASSERT3P(zio->io_vd, !=, NULL);
5675 ASSERT3P(zio->io_bp, !=, NULL);
5676 ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
5677 ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE);
5678 ASSERT3B(pio->io_prop.zp_direct_write, ==, B_TRUE);
5679 ASSERT3U(pio->io_child_type, ==, ZIO_CHILD_LOGICAL);
5680
5681 if (zfs_vdev_direct_write_verify == 0 || zio->io_error != 0)
5682 goto out;
5683
5684 if ((error = zio_checksum_error(zio, NULL)) != 0) {
5685 zio->io_error = error;
5686 if (error == ECKSUM) {
5687 zio->io_post |= ZIO_POST_DIO_CHKSUM_ERR;
5688 zio_dio_chksum_verify_error_report(zio);
5689 }
5690 }
5691
5692 out:
5693 return (zio);
5694 }
5695
5696
5697 /*
5698 * Called by RAID-Z to ensure we don't compute the checksum twice.
5699 */
5700 void
zio_checksum_verified(zio_t * zio)5701 zio_checksum_verified(zio_t *zio)
5702 {
5703 zio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
5704 }
5705
5706 /*
5707 * Report Direct I/O checksum verify error and create ZED event.
5708 */
5709 void
zio_dio_chksum_verify_error_report(zio_t * zio)5710 zio_dio_chksum_verify_error_report(zio_t *zio)
5711 {
5712 ASSERT(zio->io_post & ZIO_POST_DIO_CHKSUM_ERR);
5713
5714 if (zio->io_child_type == ZIO_CHILD_LOGICAL)
5715 return;
5716
5717 mutex_enter(&zio->io_vd->vdev_stat_lock);
5718 zio->io_vd->vdev_stat.vs_dio_verify_errors++;
5719 mutex_exit(&zio->io_vd->vdev_stat_lock);
5720 if (zio->io_type == ZIO_TYPE_WRITE) {
5721 /*
5722 * Convert checksum error for writes into EIO.
5723 */
5724 zio->io_error = SET_ERROR(EIO);
5725 /*
5726 * Report dio_verify_wr ZED event, rate limited.
5727 */
5728 if (zfs_ratelimit(&zio->io_vd->vdev_dio_verify_rl))
5729 (void) zfs_ereport_post(FM_EREPORT_ZFS_DIO_VERIFY_WR,
5730 zio->io_spa, zio->io_vd, &zio->io_bookmark, zio, 0);
5731 } else {
5732 /*
5733 * Report dio_verify_rd ZED event, rate limited.
5734 */
5735 if (zfs_ratelimit(&zio->io_vd->vdev_dio_verify_rl))
5736 (void) zfs_ereport_post(FM_EREPORT_ZFS_DIO_VERIFY_RD,
5737 zio->io_spa, zio->io_vd, &zio->io_bookmark, zio, 0);
5738 }
5739 }
5740
5741 /*
5742 * ==========================================================================
5743 * Error rank. Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other.
5744 * An error of 0 indicates success. ENXIO indicates whole-device failure,
5745 * which may be transient (e.g. unplugged) or permanent. ECKSUM and EIO
5746 * indicate errors that are specific to one I/O, and most likely permanent.
5747 * Any other error is presumed to be worse because we weren't expecting it.
5748 * ==========================================================================
5749 */
5750 int
zio_worst_error(int e1,int e2)5751 zio_worst_error(int e1, int e2)
5752 {
5753 static int zio_error_rank[] = { 0, ENXIO, ECKSUM, EIO };
5754 int r1, r2;
5755
5756 for (r1 = 0; r1 < sizeof (zio_error_rank) / sizeof (int); r1++)
5757 if (e1 == zio_error_rank[r1])
5758 break;
5759
5760 for (r2 = 0; r2 < sizeof (zio_error_rank) / sizeof (int); r2++)
5761 if (e2 == zio_error_rank[r2])
5762 break;
5763
5764 return (r1 > r2 ? e1 : e2);
5765 }
5766
5767 /*
5768 * ==========================================================================
5769 * I/O completion
5770 * ==========================================================================
5771 */
5772 static zio_t *
zio_ready(zio_t * zio)5773 zio_ready(zio_t *zio)
5774 {
5775 blkptr_t *bp = zio->io_bp;
5776 zio_t *pio, *pio_next;
5777 zio_link_t *zl = NULL;
5778
5779 if (zio_wait_for_children(zio, ZIO_CHILD_LOGICAL_BIT |
5780 ZIO_CHILD_GANG_BIT | ZIO_CHILD_DDT_BIT, ZIO_WAIT_READY)) {
5781 return (NULL);
5782 }
5783
5784 if (zio_injection_enabled) {
5785 hrtime_t target = zio_handle_ready_delay(zio);
5786 if (target != 0 && zio->io_target_timestamp == 0) {
5787 zio->io_stage >>= 1;
5788 zio->io_target_timestamp = target;
5789 zio_delay_interrupt(zio);
5790 return (NULL);
5791 }
5792 }
5793
5794 if (zio->io_ready) {
5795 ASSERT(IO_IS_ALLOCATING(zio));
5796 ASSERT(BP_GET_BIRTH(bp) == zio->io_txg ||
5797 BP_IS_HOLE(bp) || (zio->io_flags & ZIO_FLAG_NOPWRITE));
5798 ASSERT0(zio->io_children[ZIO_CHILD_GANG][ZIO_WAIT_READY]);
5799
5800 zio->io_ready(zio);
5801 }
5802
5803 #ifdef ZFS_DEBUG
5804 if (bp != NULL && bp != &zio->io_bp_copy)
5805 zio->io_bp_copy = *bp;
5806 #endif
5807
5808 if (zio->io_error != 0) {
5809 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
5810
5811 if (zio->io_flags & ZIO_FLAG_ALLOC_THROTTLED) {
5812 ASSERT(IO_IS_ALLOCATING(zio));
5813 ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
5814 ASSERT(zio->io_metaslab_class != NULL);
5815 ASSERT(ZIO_HAS_ALLOCATOR(zio));
5816
5817 /*
5818 * We were unable to allocate anything, unreserve and
5819 * issue the next I/O to allocate.
5820 */
5821 if (metaslab_class_throttle_unreserve(
5822 zio->io_metaslab_class, zio->io_allocator,
5823 zio->io_prop.zp_copies, zio->io_size)) {
5824 zio_allocate_dispatch(zio->io_metaslab_class,
5825 zio->io_allocator);
5826 }
5827 }
5828 }
5829
5830 mutex_enter(&zio->io_lock);
5831 zio->io_state[ZIO_WAIT_READY] = 1;
5832 pio = zio_walk_parents(zio, &zl);
5833 mutex_exit(&zio->io_lock);
5834
5835 /*
5836 * As we notify zio's parents, new parents could be added.
5837 * New parents go to the head of zio's io_parent_list, however,
5838 * so we will (correctly) not notify them. The remainder of zio's
5839 * io_parent_list, from 'pio_next' onward, cannot change because
5840 * all parents must wait for us to be done before they can be done.
5841 */
5842 zio_next_t next;
5843 zio_next_init(&next);
5844 for (; pio != NULL; pio = pio_next) {
5845 pio_next = zio_walk_parents(zio, &zl);
5846 zio_notify_parent(pio, zio, ZIO_WAIT_READY, &next);
5847 }
5848 ASSERT3P(zio->io_exec_next, ==, NULL);
5849 zio->io_exec_next = next.zn_list;
5850
5851 if (zio->io_flags & ZIO_FLAG_NODATA) {
5852 if (bp != NULL && BP_IS_GANG(bp)) {
5853 zio->io_flags &= ~ZIO_FLAG_NODATA;
5854 } else {
5855 ASSERT((uintptr_t)zio->io_abd < SPA_MAXBLOCKSIZE);
5856 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
5857 }
5858 }
5859
5860 if (zio_injection_enabled &&
5861 zio->io_spa->spa_syncing_txg == zio->io_txg)
5862 zio_handle_ignored_writes(zio);
5863
5864 return (zio);
5865 }
5866
5867 /*
5868 * Update the allocation throttle accounting.
5869 */
5870 static void
zio_dva_throttle_done(zio_t * zio)5871 zio_dva_throttle_done(zio_t *zio)
5872 {
5873 zio_t *pio = zio_unique_parent(zio);
5874 vdev_t *vd = zio->io_vd;
5875 int flags = METASLAB_ASYNC_ALLOC;
5876 const void *tag = pio;
5877 uint64_t size = pio->io_size;
5878
5879 ASSERT3P(zio->io_bp, !=, NULL);
5880 ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE);
5881 ASSERT3U(zio->io_priority, ==, ZIO_PRIORITY_ASYNC_WRITE);
5882 ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
5883 ASSERT(vd != NULL);
5884 ASSERT3P(vd, ==, vd->vdev_top);
5885 ASSERT(zio_injection_enabled || !(zio->io_flags & ZIO_FLAG_IO_RETRY));
5886 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REPAIR));
5887 ASSERT(zio->io_flags & ZIO_FLAG_ALLOC_THROTTLED);
5888
5889 /*
5890 * Parents of gang children can have two flavors -- ones that allocated
5891 * the gang header (will have ZIO_FLAG_IO_REWRITE set) and ones that
5892 * allocated the constituent blocks. The first use their parent as tag.
5893 * We set the size to match the original allocation call for that case.
5894 */
5895 if (pio->io_child_type == ZIO_CHILD_GANG &&
5896 (pio->io_flags & ZIO_FLAG_IO_REWRITE)) {
5897 tag = zio_unique_parent(pio);
5898 size = SPA_OLD_GANGBLOCKSIZE;
5899 }
5900
5901 ASSERT(IO_IS_ALLOCATING(pio) || (pio->io_child_type == ZIO_CHILD_GANG &&
5902 (pio->io_flags & ZIO_FLAG_IO_REWRITE)));
5903 ASSERT(ZIO_HAS_ALLOCATOR(pio));
5904 ASSERT3P(zio, !=, zio->io_logical);
5905 ASSERT(zio->io_logical != NULL);
5906 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REPAIR));
5907 ASSERT0(zio->io_flags & ZIO_FLAG_NOPWRITE);
5908 ASSERT(zio->io_metaslab_class != NULL);
5909 ASSERT(zio->io_metaslab_class->mc_alloc_throttle_enabled);
5910
5911 metaslab_group_alloc_decrement(zio->io_spa, vd->vdev_id,
5912 pio->io_allocator, flags, size, tag);
5913
5914 if (metaslab_class_throttle_unreserve(pio->io_metaslab_class,
5915 pio->io_allocator, 1, pio->io_size)) {
5916 zio_allocate_dispatch(zio->io_metaslab_class,
5917 pio->io_allocator);
5918 }
5919 }
5920
5921 static void
zio_done_postread_done(zio_t * zio)5922 zio_done_postread_done(zio_t *zio)
5923 {
5924 abd_free(zio->io_abd);
5925 }
5926
5927 static zio_t *
zio_done(zio_t * zio)5928 zio_done(zio_t *zio)
5929 {
5930 /*
5931 * Always attempt to keep stack usage minimal here since
5932 * we can be called recursively up to 19 levels deep.
5933 */
5934 const uint64_t psize = zio->io_size;
5935 zio_t *pio, *pio_next;
5936 zio_link_t *zl = NULL;
5937
5938 /*
5939 * If our children haven't all completed,
5940 * wait for them and then repeat this pipeline stage.
5941 */
5942 if (zio_wait_for_children(zio, ZIO_CHILD_ALL_BITS, ZIO_WAIT_DONE)) {
5943 return (NULL);
5944 }
5945
5946 /*
5947 * If the allocation throttle is enabled, then update the accounting.
5948 * We only track child I/Os that are part of an allocating async
5949 * write. We must do this since the allocation is performed
5950 * by the logical I/O but the actual write is done by child I/Os.
5951 */
5952 if (zio->io_flags & ZIO_FLAG_ALLOC_THROTTLED &&
5953 zio->io_child_type == ZIO_CHILD_VDEV)
5954 zio_dva_throttle_done(zio);
5955
5956 for (int c = 0; c < ZIO_CHILD_TYPES; c++)
5957 for (int w = 0; w < ZIO_WAIT_TYPES; w++)
5958 ASSERT0(zio->io_children[c][w]);
5959
5960 if (zio->io_bp != NULL && !BP_IS_EMBEDDED(zio->io_bp)) {
5961 ASSERT(memcmp(zio->io_bp, &zio->io_bp_copy,
5962 sizeof (blkptr_t)) == 0 ||
5963 (zio->io_bp == zio_unique_parent(zio)->io_bp));
5964 if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(zio->io_bp) &&
5965 zio->io_bp_override == NULL &&
5966 !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) {
5967 ASSERT3U(zio->io_prop.zp_copies, <=,
5968 BP_GET_NDVAS(zio->io_bp));
5969 ASSERT(BP_COUNT_GANG(zio->io_bp) == 0 ||
5970 (BP_COUNT_GANG(zio->io_bp) ==
5971 BP_GET_NDVAS(zio->io_bp)));
5972 }
5973 if (zio->io_flags & ZIO_FLAG_NOPWRITE)
5974 VERIFY(BP_EQUAL(zio->io_bp, &zio->io_bp_orig));
5975 }
5976
5977 /*
5978 * If there were child vdev/gang/ddt errors, they apply to us now.
5979 */
5980 zio_inherit_child_errors(zio, ZIO_CHILD_VDEV);
5981 zio_inherit_child_errors(zio, ZIO_CHILD_GANG);
5982 zio_inherit_child_errors(zio, ZIO_CHILD_DDT);
5983
5984 /*
5985 * If the I/O on the transformed data was successful, generate any
5986 * checksum reports now while we still have the transformed data.
5987 */
5988 if (zio->io_error == 0) {
5989 while (zio->io_cksum_report != NULL) {
5990 zio_cksum_report_t *zcr = zio->io_cksum_report;
5991 uint64_t align = zcr->zcr_align;
5992 uint64_t asize = P2ROUNDUP(psize, align);
5993 abd_t *adata = zio->io_abd;
5994
5995 if (adata != NULL && asize != psize) {
5996 adata = abd_alloc(asize, B_TRUE);
5997 abd_copy(adata, zio->io_abd, psize);
5998 abd_zero_off(adata, psize, asize - psize);
5999 }
6000
6001 zio->io_cksum_report = zcr->zcr_next;
6002 zcr->zcr_next = NULL;
6003 zcr->zcr_finish(zcr, adata);
6004 zfs_ereport_free_checksum(zcr);
6005
6006 if (adata != NULL && asize != psize)
6007 abd_free(adata);
6008 }
6009 }
6010
6011 zio_pop_transforms(zio); /* note: may set zio->io_error */
6012
6013 /*
6014 * During thorough scrub, if the dataset key is not loaded, decryption
6015 * or MAC verification fails with EACCES (spa_do_crypt_abd() and the
6016 * MAC helpers). Since the block's checksum was already successfully
6017 * verified by zio_checksum_verify() before we got here, treat it as
6018 * success and move on; this is as much as we can do without the keys
6019 * loaded.
6020 */
6021 if (zio->io_error == EACCES && (zio->io_flags & ZIO_FLAG_SCRUB) &&
6022 !(zio->io_flags & ZIO_FLAG_RAW))
6023 zio->io_error = 0;
6024
6025 vdev_stat_update(zio, psize);
6026
6027 /*
6028 * If this I/O is attached to a particular vdev is slow, exceeding
6029 * 30 seconds to complete, post an error described the I/O delay.
6030 * We ignore these errors if the device is currently unavailable.
6031 */
6032 if (zio->io_delay >= MSEC2NSEC(zio_slow_io_ms)) {
6033 if (zio->io_vd != NULL && !vdev_is_dead(zio->io_vd)) {
6034 /*
6035 * We want to only increment our slow IO counters if
6036 * the IO is valid (i.e. not if the drive is removed).
6037 *
6038 * zfs_ereport_post() will also do these checks, but
6039 * it can also ratelimit and have other failures, so we
6040 * need to increment the slow_io counters independent
6041 * of it.
6042 */
6043 if (zfs_ereport_is_valid(FM_EREPORT_ZFS_DELAY,
6044 zio->io_spa, zio->io_vd, zio)) {
6045 mutex_enter(&zio->io_vd->vdev_stat_lock);
6046 zio->io_vd->vdev_stat.vs_slow_ios++;
6047 mutex_exit(&zio->io_vd->vdev_stat_lock);
6048
6049 if (zio->io_vd->vdev_slow_io_events) {
6050 (void) zfs_ereport_post(
6051 FM_EREPORT_ZFS_DELAY,
6052 zio->io_spa, zio->io_vd,
6053 &zio->io_bookmark, zio, 0);
6054 }
6055 }
6056 }
6057 }
6058
6059 if (zio->io_error) {
6060 /*
6061 * If this I/O is attached to a particular vdev,
6062 * generate an error message describing the I/O failure
6063 * at the block level. We ignore these errors if the
6064 * device is currently unavailable.
6065 */
6066 if (zio->io_error != ECKSUM && zio->io_vd != NULL &&
6067 !vdev_is_dead(zio->io_vd) &&
6068 !(zio->io_post & ZIO_POST_DIO_CHKSUM_ERR)) {
6069 int ret = zfs_ereport_post(FM_EREPORT_ZFS_IO,
6070 zio->io_spa, zio->io_vd, &zio->io_bookmark, zio, 0);
6071 if (ret != EALREADY) {
6072 mutex_enter(&zio->io_vd->vdev_stat_lock);
6073 if (zio->io_type == ZIO_TYPE_READ)
6074 zio->io_vd->vdev_stat.vs_read_errors++;
6075 else if (zio->io_type == ZIO_TYPE_WRITE)
6076 zio->io_vd->vdev_stat.vs_write_errors++;
6077 mutex_exit(&zio->io_vd->vdev_stat_lock);
6078 }
6079 }
6080
6081 if ((zio->io_error == EIO || !(zio->io_flags &
6082 (ZIO_FLAG_SPECULATIVE | ZIO_FLAG_DONT_PROPAGATE))) &&
6083 !(zio->io_post & ZIO_POST_DIO_CHKSUM_ERR) &&
6084 zio == zio->io_logical) {
6085 /*
6086 * For logical I/O requests, tell the SPA to log the
6087 * error and generate a logical data ereport.
6088 */
6089 spa_log_error(zio->io_spa, &zio->io_bookmark,
6090 BP_GET_PHYSICAL_BIRTH(zio->io_bp));
6091 (void) zfs_ereport_post(FM_EREPORT_ZFS_DATA,
6092 zio->io_spa, NULL, &zio->io_bookmark, zio, 0);
6093 }
6094 }
6095
6096 if (zio->io_error && zio == zio->io_logical) {
6097
6098 /*
6099 * A DDT child tried to create a mixed gang/non-gang BP. We're
6100 * going to have to just retry as a non-dedup IO.
6101 */
6102 if (zio->io_error == EAGAIN && IO_IS_ALLOCATING(zio) &&
6103 zio->io_prop.zp_dedup) {
6104 zio->io_post |= ZIO_POST_REEXECUTE;
6105 zio->io_prop.zp_dedup = B_FALSE;
6106 }
6107 /*
6108 * Determine whether zio should be reexecuted. This will
6109 * propagate all the way to the root via zio_notify_parent().
6110 */
6111 ASSERT(zio->io_vd == NULL && zio->io_bp != NULL);
6112 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
6113
6114 if (IO_IS_ALLOCATING(zio) &&
6115 !(zio->io_flags & ZIO_FLAG_CANFAIL) &&
6116 !(zio->io_post & ZIO_POST_DIO_CHKSUM_ERR)) {
6117 if (zio->io_error != ENOSPC)
6118 zio->io_post |= ZIO_POST_REEXECUTE;
6119 else
6120 zio->io_post |= ZIO_POST_SUSPEND;
6121 }
6122
6123 if ((zio->io_type == ZIO_TYPE_READ ||
6124 zio->io_type == ZIO_TYPE_FREE) &&
6125 !(zio->io_flags & ZIO_FLAG_SCAN_THREAD) &&
6126 zio->io_error == ENXIO &&
6127 spa_load_state(zio->io_spa) == SPA_LOAD_NONE &&
6128 spa_get_failmode(zio->io_spa) != ZIO_FAILURE_MODE_CONTINUE)
6129 zio->io_post |= ZIO_POST_SUSPEND;
6130
6131 if (!(zio->io_flags & ZIO_FLAG_CANFAIL) &&
6132 !(zio->io_post & (ZIO_POST_REEXECUTE|ZIO_POST_SUSPEND)))
6133 zio->io_post |= ZIO_POST_SUSPEND;
6134
6135 /*
6136 * Here is a possibly good place to attempt to do
6137 * either combinatorial reconstruction or error correction
6138 * based on checksums. It also might be a good place
6139 * to send out preliminary ereports before we suspend
6140 * processing.
6141 */
6142 }
6143
6144 /*
6145 * If there were logical child errors, they apply to us now.
6146 * We defer this until now to avoid conflating logical child
6147 * errors with errors that happened to the zio itself when
6148 * updating vdev stats and reporting FMA events above.
6149 */
6150 zio_inherit_child_errors(zio, ZIO_CHILD_LOGICAL);
6151
6152 if ((zio->io_error ||
6153 (zio->io_post & (ZIO_POST_REEXECUTE|ZIO_POST_SUSPEND))) &&
6154 IO_IS_ALLOCATING(zio) && zio->io_gang_leader == zio &&
6155 !(zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)))
6156 zio_dva_unallocate(zio, zio->io_gang_tree, zio->io_bp);
6157
6158 zio_gang_tree_free(&zio->io_gang_tree);
6159
6160 /*
6161 * Godfather I/Os should never suspend.
6162 */
6163 if ((zio->io_flags & ZIO_FLAG_GODFATHER) &&
6164 (zio->io_post & ZIO_POST_SUSPEND))
6165 zio->io_post &= ~ZIO_POST_SUSPEND;
6166
6167 if (zio->io_post & (ZIO_POST_REEXECUTE|ZIO_POST_SUSPEND)) {
6168 /*
6169 * A Direct I/O operation that has a checksum verify error
6170 * should not attempt to reexecute. Instead, the error should
6171 * just be propagated back.
6172 */
6173 ASSERT0(zio->io_post & ZIO_POST_DIO_CHKSUM_ERR);
6174
6175 /*
6176 * This is a logical I/O that wants to reexecute.
6177 *
6178 * Reexecute is top-down. When an i/o fails, if it's not
6179 * the root, it simply notifies its parent and sticks around.
6180 * The parent, seeing that it still has children in zio_done(),
6181 * does the same. This percolates all the way up to the root.
6182 * The root i/o will reexecute or suspend the entire tree.
6183 *
6184 * This approach ensures that zio_reexecute() honors
6185 * all the original i/o dependency relationships, e.g.
6186 * parents not executing until children are ready.
6187 */
6188 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
6189
6190 zio->io_gang_leader = NULL;
6191
6192 mutex_enter(&zio->io_lock);
6193 zio->io_state[ZIO_WAIT_DONE] = 1;
6194 mutex_exit(&zio->io_lock);
6195
6196 /*
6197 * "The Godfather" I/O monitors its children but is
6198 * not a true parent to them. It will track them through
6199 * the pipeline but severs its ties whenever they get into
6200 * trouble (e.g. suspended). This allows "The Godfather"
6201 * I/O to return status without blocking.
6202 */
6203 zl = NULL;
6204 for (pio = zio_walk_parents(zio, &zl); pio != NULL;
6205 pio = pio_next) {
6206 zio_link_t *remove_zl = zl;
6207 pio_next = zio_walk_parents(zio, &zl);
6208
6209 if ((pio->io_flags & ZIO_FLAG_GODFATHER) &&
6210 (zio->io_post & ZIO_POST_SUSPEND)) {
6211 zio_remove_child(pio, zio, remove_zl);
6212 /*
6213 * This is a rare code path, so we don't
6214 * bother with the "next" list.
6215 */
6216 zio_notify_parent(pio, zio, ZIO_WAIT_DONE,
6217 NULL);
6218 }
6219 }
6220
6221 if ((pio = zio_unique_parent(zio)) != NULL) {
6222 /*
6223 * We're not a root i/o, so there's nothing to do
6224 * but notify our parent. Don't propagate errors
6225 * upward since we haven't permanently failed yet.
6226 */
6227 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
6228 zio->io_flags |= ZIO_FLAG_DONT_PROPAGATE;
6229 /*
6230 * This is a rare code path, so we don't bother with
6231 * the "next" list.
6232 */
6233 zio_notify_parent(pio, zio, ZIO_WAIT_DONE, NULL);
6234 } else if (zio->io_post & ZIO_POST_SUSPEND) {
6235 /*
6236 * We'd fail again if we reexecuted now, so suspend
6237 * until conditions improve (e.g. device comes online).
6238 */
6239 zio_suspend(zio->io_spa, zio, ZIO_SUSPEND_IOERR);
6240 } else {
6241 ASSERT(zio->io_post & ZIO_POST_REEXECUTE);
6242 /*
6243 * Reexecution is potentially a huge amount of work.
6244 * Hand it off to the otherwise-unused claim taskq.
6245 */
6246 spa_taskq_dispatch(zio->io_spa,
6247 ZIO_TYPE_CLAIM, ZIO_TASKQ_ISSUE,
6248 zio_reexecute, zio, B_FALSE);
6249 }
6250 return (NULL);
6251 }
6252
6253 ASSERT(list_is_empty(&zio->io_child_list));
6254 ASSERT0(zio->io_post & ZIO_POST_REEXECUTE);
6255 ASSERT0(zio->io_post & ZIO_POST_SUSPEND);
6256 ASSERT(zio->io_error == 0 || (zio->io_flags & ZIO_FLAG_CANFAIL));
6257
6258 /*
6259 * Report any checksum errors, since the I/O is complete.
6260 */
6261 while (zio->io_cksum_report != NULL) {
6262 zio_cksum_report_t *zcr = zio->io_cksum_report;
6263 zio->io_cksum_report = zcr->zcr_next;
6264 zcr->zcr_next = NULL;
6265 zcr->zcr_finish(zcr, NULL);
6266 zfs_ereport_free_checksum(zcr);
6267 }
6268
6269 if (zio->io_flags & ZIO_FLAG_POSTREAD) {
6270 ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE);
6271 zl = NULL;
6272 zio_t *pio = zio_walk_parents(zio, &zl);
6273 blkptr_t *bp = zio->io_bp;
6274 abd_t *abd = abd_alloc_for_io(BP_GET_PSIZE(bp), B_FALSE);
6275 zio_priority_t prio = zio->io_priority ==
6276 ZIO_PRIORITY_SYNC_WRITE ? ZIO_PRIORITY_SYNC_READ :
6277 ZIO_PRIORITY_SCRUB;
6278 zio_t *cio = zio_vdev_child_io(pio, zio->io_bp, zio->io_vd,
6279 zio->io_offset, abd, zio->io_size, ZIO_TYPE_READ, prio,
6280 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW | ZIO_FLAG_CANFAIL |
6281 ZIO_FLAG_RESILVER | ZIO_FLAG_DONT_PROPAGATE,
6282 zio_done_postread_done, NULL);
6283 cio->io_flags &= ~ZIO_FLAG_ALLOC_THROTTLED;
6284 zio_nowait(cio);
6285 }
6286
6287 /*
6288 * It is the responsibility of the done callback to ensure that this
6289 * particular zio is no longer discoverable for adoption, and as
6290 * such, cannot acquire any new parents.
6291 */
6292 if (zio->io_done)
6293 zio->io_done(zio);
6294
6295 mutex_enter(&zio->io_lock);
6296 zio->io_state[ZIO_WAIT_DONE] = 1;
6297 mutex_exit(&zio->io_lock);
6298
6299 /*
6300 * We are done executing this zio. We may want to execute some of its
6301 * parents next. See the comment in zio_notify_parent().
6302 */
6303 zio_next_t next;
6304 zio_next_init(&next);
6305 zl = NULL;
6306 for (pio = zio_walk_parents(zio, &zl); pio != NULL; pio = pio_next) {
6307 zio_link_t *remove_zl = zl;
6308 pio_next = zio_walk_parents(zio, &zl);
6309 zio_remove_child(pio, zio, remove_zl);
6310 zio_notify_parent(pio, zio, ZIO_WAIT_DONE, &next);
6311 }
6312
6313 if (zio->io_waiter != NULL) {
6314 mutex_enter(&zio->io_lock);
6315 zio->io_executor = NULL;
6316 cv_broadcast(&zio->io_cv);
6317 mutex_exit(&zio->io_lock);
6318 } else {
6319 zio_destroy(zio);
6320 }
6321
6322 return (next.zn_list);
6323 }
6324
6325 /*
6326 * ==========================================================================
6327 * I/O pipeline definition
6328 * ==========================================================================
6329 */
6330 static zio_pipe_stage_t *zio_pipeline[] = {
6331 NULL,
6332 zio_read_bp_init,
6333 zio_write_bp_init,
6334 zio_free_bp_init,
6335 zio_issue_async,
6336 zio_write_compress,
6337 zio_encrypt,
6338 zio_checksum_generate,
6339 zio_nop_write,
6340 zio_ddt_read_start,
6341 zio_ddt_read_done,
6342 zio_ddt_write,
6343 zio_ddt_free,
6344 zio_brt_free,
6345 zio_gang_assemble,
6346 zio_gang_issue,
6347 zio_dva_throttle,
6348 zio_dva_allocate,
6349 zio_dva_free,
6350 zio_dva_claim,
6351 zio_ready,
6352 zio_vdev_io_start,
6353 zio_vdev_io_done,
6354 zio_vdev_io_assess,
6355 zio_checksum_verify,
6356 zio_dio_checksum_verify,
6357 zio_done
6358 };
6359
6360
6361
6362
6363 /*
6364 * Compare two zbookmark_phys_t's to see which we would reach first in a
6365 * pre-order traversal of the object tree.
6366 *
6367 * This is simple in every case aside from the meta-dnode object. For all other
6368 * objects, we traverse them in order (object 1 before object 2, and so on).
6369 * However, all of these objects are traversed while traversing object 0, since
6370 * the data it points to is the list of objects. Thus, we need to convert to a
6371 * canonical representation so we can compare meta-dnode bookmarks to
6372 * non-meta-dnode bookmarks.
6373 *
6374 * We do this by calculating "equivalents" for each field of the zbookmark.
6375 * zbookmarks outside of the meta-dnode use their own object and level, and
6376 * calculate the level 0 equivalent (the first L0 blkid that is contained in the
6377 * blocks this bookmark refers to) by multiplying their blkid by their span
6378 * (the number of L0 blocks contained within one block at their level).
6379 * zbookmarks inside the meta-dnode calculate their object equivalent
6380 * (which is L0equiv * dnodes per data block), use 0 for their L0equiv, and use
6381 * level + 1<<31 (any value larger than a level could ever be) for their level.
6382 * This causes them to always compare before a bookmark in their object
6383 * equivalent, compare appropriately to bookmarks in other objects, and to
6384 * compare appropriately to other bookmarks in the meta-dnode.
6385 */
6386 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)6387 zbookmark_compare(uint16_t dbss1, uint8_t ibs1, uint16_t dbss2, uint8_t ibs2,
6388 const zbookmark_phys_t *zb1, const zbookmark_phys_t *zb2)
6389 {
6390 /*
6391 * These variables represent the "equivalent" values for the zbookmark,
6392 * after converting zbookmarks inside the meta dnode to their
6393 * normal-object equivalents.
6394 */
6395 uint64_t zb1obj, zb2obj;
6396 uint64_t zb1L0, zb2L0;
6397 uint64_t zb1level, zb2level;
6398
6399 if (zb1->zb_object == zb2->zb_object &&
6400 zb1->zb_level == zb2->zb_level &&
6401 zb1->zb_blkid == zb2->zb_blkid)
6402 return (0);
6403
6404 if (zb1->zb_level < 0 || zb2->zb_level < 0) {
6405 /*
6406 * "Negative" levels are ZB_ROOT_LEVEL, ZB_ZIL_LEVEL or
6407 * ZB_DNODE_LEVEL, and represent some sort of auxiliary dataset
6408 * block or object. In this case, we're usually being called
6409 * from dsl_scan or dmu_traverse.
6410 *
6411 * These "levels" are more like a "type" signal, not directly
6412 * comparable, but we have to do something. So we order them in
6413 * the order we would see them during a typical scan or
6414 * traverse:
6415 *
6416 * - ZB_ROOT_LEVEL: the "top" block carrying the dataset head
6417 * - ZB_ZIL_LEVEL: the head ZIL block attached to the dataset
6418 * - ZB_DNODE_LEVEL: "virtual" position representing an
6419 * entire object. Sorts ahead of the true
6420 * data blocks for the object.
6421 * - level >= 0: data blocks
6422 *
6423 * We work through these cases from top to bottom, with
6424 * appropriate tiebreaks for each kind.
6425 */
6426
6427 /*
6428 * Root level wins. It shouldn't be possible for both to be the
6429 * root level in this per-dataset tree, and there's no obvious
6430 * tiebreaker, but we handle it as a defensive measure.
6431 */
6432 if (zb1->zb_level == ZB_ROOT_LEVEL &&
6433 zb2->zb_level == ZB_ROOT_LEVEL)
6434 return (TREE_PCMP(zb1, zb2));
6435 if (zb1->zb_level == ZB_ROOT_LEVEL)
6436 return (-1);
6437 if (zb2->zb_level == ZB_ROOT_LEVEL)
6438 return (1);
6439
6440 /* ZIL bookmarks have valid blkid, so the earlier one wins. */
6441 if (zb1->zb_level == ZB_ZIL_LEVEL &&
6442 zb2->zb_level == ZB_ZIL_LEVEL)
6443 return (TREE_CMP(zb1->zb_blkid, zb2->zb_blkid));
6444 if (zb1->zb_level == ZB_ZIL_LEVEL)
6445 return (-1);
6446 if (zb2->zb_level == ZB_ZIL_LEVEL)
6447 return (1);
6448
6449 /*
6450 * If we get this far, then at least one is ZB_DNODE_LEVEL, and
6451 * the other is either ZB_DNODE_LEVEL or a data block.
6452 * Regardless, the one with the lower-numbered object wins -
6453 * earler ZB_DNODE_LEVEL beats later, but data block on earlier
6454 * objects beats the virtual marker on later objects.
6455 */
6456 int cmp = TREE_CMP(zb1->zb_object, zb2->zb_object);
6457 if (cmp != 0)
6458 return (cmp);
6459
6460 if (zb1->zb_level == ZB_DNODE_LEVEL)
6461 return (-1);
6462 return (1);
6463 }
6464
6465 IMPLY(zb1->zb_level > 0, ibs1 >= SPA_MINBLOCKSHIFT);
6466 IMPLY(zb2->zb_level > 0, ibs2 >= SPA_MINBLOCKSHIFT);
6467
6468 /*
6469 * BP_SPANB calculates the span in blocks.
6470 */
6471 zb1L0 = (zb1->zb_blkid) * BP_SPANB(ibs1, zb1->zb_level);
6472 zb2L0 = (zb2->zb_blkid) * BP_SPANB(ibs2, zb2->zb_level);
6473
6474 if (zb1->zb_object == DMU_META_DNODE_OBJECT) {
6475 zb1obj = zb1L0 * (dbss1 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT));
6476 zb1L0 = 0;
6477 zb1level = zb1->zb_level + COMPARE_META_LEVEL;
6478 } else {
6479 zb1obj = zb1->zb_object;
6480 zb1level = zb1->zb_level;
6481 }
6482
6483 if (zb2->zb_object == DMU_META_DNODE_OBJECT) {
6484 zb2obj = zb2L0 * (dbss2 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT));
6485 zb2L0 = 0;
6486 zb2level = zb2->zb_level + COMPARE_META_LEVEL;
6487 } else {
6488 zb2obj = zb2->zb_object;
6489 zb2level = zb2->zb_level;
6490 }
6491
6492 /* Now that we have a canonical representation, do the comparison. */
6493 if (zb1obj != zb2obj)
6494 return (zb1obj < zb2obj ? -1 : 1);
6495 else if (zb1L0 != zb2L0)
6496 return (zb1L0 < zb2L0 ? -1 : 1);
6497 else if (zb1level != zb2level)
6498 return (zb1level > zb2level ? -1 : 1);
6499 /*
6500 * This can (theoretically) happen if the bookmarks have the same object
6501 * and level, but different blkids, if the block sizes are not the same.
6502 * There is presently no way to change the indirect block sizes
6503 */
6504 return (0);
6505 }
6506
6507 /*
6508 * This function checks the following: given that last_block is the place that
6509 * our traversal stopped last time, does that guarantee that we've visited
6510 * every node under subtree_root? Therefore, we can't just use the raw output
6511 * of zbookmark_compare. We have to pass in a modified version of
6512 * subtree_root; by incrementing the block id, and then checking whether
6513 * last_block is before or equal to that, we can tell whether or not having
6514 * visited last_block implies that all of subtree_root's children have been
6515 * visited.
6516 */
6517 boolean_t
zbookmark_subtree_completed(const dnode_phys_t * dnp,const zbookmark_phys_t * subtree_root,const zbookmark_phys_t * last_block)6518 zbookmark_subtree_completed(const dnode_phys_t *dnp,
6519 const zbookmark_phys_t *subtree_root, const zbookmark_phys_t *last_block)
6520 {
6521 zbookmark_phys_t mod_zb = *subtree_root;
6522 mod_zb.zb_blkid++;
6523 ASSERT0(last_block->zb_level);
6524
6525 /* The objset_phys_t isn't before anything. */
6526 if (dnp == NULL)
6527 return (B_FALSE);
6528
6529 /*
6530 * We pass in 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT) for the
6531 * data block size in sectors, because that variable is only used if
6532 * the bookmark refers to a block in the meta-dnode. Since we don't
6533 * know without examining it what object it refers to, and there's no
6534 * harm in passing in this value in other cases, we always pass it in.
6535 *
6536 * We pass in 0 for the indirect block size shift because zb2 must be
6537 * level 0. The indirect block size is only used to calculate the span
6538 * of the bookmark, but since the bookmark must be level 0, the span is
6539 * always 1, so the math works out.
6540 *
6541 * If you make changes to how the zbookmark_compare code works, be sure
6542 * to make sure that this code still works afterwards.
6543 */
6544 return (zbookmark_compare(dnp->dn_datablkszsec, dnp->dn_indblkshift,
6545 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT), 0, &mod_zb,
6546 last_block) <= 0);
6547 }
6548
6549 /*
6550 * This function is similar to zbookmark_subtree_completed(), but returns true
6551 * if subtree_root is equal or ahead of last_block, i.e. still to be done.
6552 */
6553 boolean_t
zbookmark_subtree_tbd(const dnode_phys_t * dnp,const zbookmark_phys_t * subtree_root,const zbookmark_phys_t * last_block)6554 zbookmark_subtree_tbd(const dnode_phys_t *dnp,
6555 const zbookmark_phys_t *subtree_root, const zbookmark_phys_t *last_block)
6556 {
6557 ASSERT0(last_block->zb_level);
6558 if (dnp == NULL)
6559 return (B_FALSE);
6560 return (zbookmark_compare(dnp->dn_datablkszsec, dnp->dn_indblkshift,
6561 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT), 0, subtree_root,
6562 last_block) >= 0);
6563 }
6564
6565 EXPORT_SYMBOL(zio_type_name);
6566 EXPORT_SYMBOL(zio_buf_alloc);
6567 EXPORT_SYMBOL(zio_data_buf_alloc);
6568 EXPORT_SYMBOL(zio_buf_free);
6569 EXPORT_SYMBOL(zio_data_buf_free);
6570
6571 ZFS_MODULE_PARAM(zfs_zio, zio_, slow_io_ms, INT, ZMOD_RW,
6572 "Max I/O completion time (milliseconds) before marking it as slow");
6573
6574 ZFS_MODULE_PARAM(zfs_zio, zio_, requeue_io_start_cut_in_line, INT, ZMOD_RW,
6575 "Prioritize requeued I/O");
6576
6577 ZFS_MODULE_PARAM(zfs_zio, zio_, batch_enabled, INT, ZMOD_RW,
6578 "Batch processing of vdev children I/O completions");
6579
6580 ZFS_MODULE_PARAM(zfs, zfs_, sync_pass_deferred_free, UINT, ZMOD_RW,
6581 "Defer frees starting in this pass");
6582
6583 ZFS_MODULE_PARAM(zfs, zfs_, sync_pass_dont_compress, UINT, ZMOD_RW,
6584 "Don't compress starting in this pass");
6585
6586 ZFS_MODULE_PARAM(zfs, zfs_, sync_pass_rewrite, UINT, ZMOD_RW,
6587 "Rewrite new bps starting in this pass");
6588
6589 ZFS_MODULE_PARAM(zfs_zio, zio_, dva_throttle_enabled, INT, ZMOD_RW,
6590 "Throttle block allocations in the ZIO pipeline");
6591
6592 ZFS_MODULE_PARAM(zfs_zio, zio_, deadman_log_all, INT, ZMOD_RW,
6593 "Log all slow ZIOs, not just those with vdevs");
6594