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