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