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