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