]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zio.c
Change KM_PUSHPAGE -> KM_SLEEP
[mirror_zfs.git] / module / zfs / zio.c
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, 2014 by Delphix. All rights reserved.
24 * Copyright (c) 2011 Nexenta Systems, Inc. All rights reserved.
25 */
26
27 #include <sys/zfs_context.h>
28 #include <sys/fm/fs/zfs.h>
29 #include <sys/spa.h>
30 #include <sys/txg.h>
31 #include <sys/spa_impl.h>
32 #include <sys/vdev_impl.h>
33 #include <sys/zio_impl.h>
34 #include <sys/zio_compress.h>
35 #include <sys/zio_checksum.h>
36 #include <sys/dmu_objset.h>
37 #include <sys/arc.h>
38 #include <sys/ddt.h>
39 #include <sys/blkptr.h>
40 #include <sys/zfeature.h>
41
42 /*
43 * ==========================================================================
44 * I/O type descriptions
45 * ==========================================================================
46 */
47 const char *zio_type_name[ZIO_TYPES] = {
48 "z_null", "z_rd", "z_wr", "z_fr", "z_cl", "z_ioctl"
49 };
50
51 /*
52 * ==========================================================================
53 * I/O kmem caches
54 * ==========================================================================
55 */
56 kmem_cache_t *zio_cache;
57 kmem_cache_t *zio_link_cache;
58 kmem_cache_t *zio_vdev_cache;
59 kmem_cache_t *zio_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
60 kmem_cache_t *zio_data_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
61 int zio_bulk_flags = 0;
62 int zio_delay_max = ZIO_DELAY_MAX;
63
64 /*
65 * The following actions directly effect the spa's sync-to-convergence logic.
66 * The values below define the sync pass when we start performing the action.
67 * Care should be taken when changing these values as they directly impact
68 * spa_sync() performance. Tuning these values may introduce subtle performance
69 * pathologies and should only be done in the context of performance analysis.
70 * These tunables will eventually be removed and replaced with #defines once
71 * enough analysis has been done to determine optimal values.
72 *
73 * The 'zfs_sync_pass_deferred_free' pass must be greater than 1 to ensure that
74 * regular blocks are not deferred.
75 */
76 int zfs_sync_pass_deferred_free = 2; /* defer frees starting in this pass */
77 int zfs_sync_pass_dont_compress = 5; /* don't compress starting in this pass */
78 int zfs_sync_pass_rewrite = 2; /* rewrite new bps starting in this pass */
79
80 /*
81 * An allocating zio is one that either currently has the DVA allocate
82 * stage set or will have it later in its lifetime.
83 */
84 #define IO_IS_ALLOCATING(zio) ((zio)->io_orig_pipeline & ZIO_STAGE_DVA_ALLOCATE)
85
86 int zio_requeue_io_start_cut_in_line = 1;
87
88 #ifdef ZFS_DEBUG
89 int zio_buf_debug_limit = 16384;
90 #else
91 int zio_buf_debug_limit = 0;
92 #endif
93
94 static inline void __zio_execute(zio_t *zio);
95
96 static int
97 zio_cons(void *arg, void *unused, int kmflag)
98 {
99 zio_t *zio = arg;
100
101 bzero(zio, sizeof (zio_t));
102
103 mutex_init(&zio->io_lock, NULL, MUTEX_DEFAULT, NULL);
104 cv_init(&zio->io_cv, NULL, CV_DEFAULT, NULL);
105
106 list_create(&zio->io_parent_list, sizeof (zio_link_t),
107 offsetof(zio_link_t, zl_parent_node));
108 list_create(&zio->io_child_list, sizeof (zio_link_t),
109 offsetof(zio_link_t, zl_child_node));
110
111 return (0);
112 }
113
114 static void
115 zio_dest(void *arg, void *unused)
116 {
117 zio_t *zio = arg;
118
119 mutex_destroy(&zio->io_lock);
120 cv_destroy(&zio->io_cv);
121 list_destroy(&zio->io_parent_list);
122 list_destroy(&zio->io_child_list);
123 }
124
125 void
126 zio_init(void)
127 {
128 size_t c;
129 vmem_t *data_alloc_arena = NULL;
130
131 zio_cache = kmem_cache_create("zio_cache", sizeof (zio_t), 0,
132 zio_cons, zio_dest, NULL, NULL, NULL, 0);
133 zio_link_cache = kmem_cache_create("zio_link_cache",
134 sizeof (zio_link_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
135 zio_vdev_cache = kmem_cache_create("zio_vdev_cache", sizeof (vdev_io_t),
136 PAGESIZE, NULL, NULL, NULL, NULL, NULL, 0);
137
138 /*
139 * For small buffers, we want a cache for each multiple of
140 * SPA_MINBLOCKSIZE. For medium-size buffers, we want a cache
141 * for each quarter-power of 2. For large buffers, we want
142 * a cache for each multiple of PAGESIZE.
143 */
144 for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
145 size_t size = (c + 1) << SPA_MINBLOCKSHIFT;
146 size_t p2 = size;
147 size_t align = 0;
148
149 while (p2 & (p2 - 1))
150 p2 &= p2 - 1;
151
152 #ifndef _KERNEL
153 /*
154 * If we are using watchpoints, put each buffer on its own page,
155 * to eliminate the performance overhead of trapping to the
156 * kernel when modifying a non-watched buffer that shares the
157 * page with a watched buffer.
158 */
159 if (arc_watch && !IS_P2ALIGNED(size, PAGESIZE))
160 continue;
161 #endif
162 if (size <= 4 * SPA_MINBLOCKSIZE) {
163 align = SPA_MINBLOCKSIZE;
164 } else if (IS_P2ALIGNED(size, PAGESIZE)) {
165 align = PAGESIZE;
166 } else if (IS_P2ALIGNED(size, p2 >> 2)) {
167 align = p2 >> 2;
168 }
169
170 if (align != 0) {
171 char name[36];
172 int flags = zio_bulk_flags;
173
174 (void) sprintf(name, "zio_buf_%lu", (ulong_t)size);
175 zio_buf_cache[c] = kmem_cache_create(name, size,
176 align, NULL, NULL, NULL, NULL, NULL, flags);
177
178 (void) sprintf(name, "zio_data_buf_%lu", (ulong_t)size);
179 zio_data_buf_cache[c] = kmem_cache_create(name, size,
180 align, NULL, NULL, NULL, NULL,
181 data_alloc_arena, flags);
182 }
183 }
184
185 while (--c != 0) {
186 ASSERT(zio_buf_cache[c] != NULL);
187 if (zio_buf_cache[c - 1] == NULL)
188 zio_buf_cache[c - 1] = zio_buf_cache[c];
189
190 ASSERT(zio_data_buf_cache[c] != NULL);
191 if (zio_data_buf_cache[c - 1] == NULL)
192 zio_data_buf_cache[c - 1] = zio_data_buf_cache[c];
193 }
194
195 zio_inject_init();
196
197 lz4_init();
198 }
199
200 void
201 zio_fini(void)
202 {
203 size_t c;
204 kmem_cache_t *last_cache = NULL;
205 kmem_cache_t *last_data_cache = NULL;
206
207 for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
208 if (zio_buf_cache[c] != last_cache) {
209 last_cache = zio_buf_cache[c];
210 kmem_cache_destroy(zio_buf_cache[c]);
211 }
212 zio_buf_cache[c] = NULL;
213
214 if (zio_data_buf_cache[c] != last_data_cache) {
215 last_data_cache = zio_data_buf_cache[c];
216 kmem_cache_destroy(zio_data_buf_cache[c]);
217 }
218 zio_data_buf_cache[c] = NULL;
219 }
220
221 kmem_cache_destroy(zio_vdev_cache);
222 kmem_cache_destroy(zio_link_cache);
223 kmem_cache_destroy(zio_cache);
224
225 zio_inject_fini();
226
227 lz4_fini();
228 }
229
230 /*
231 * ==========================================================================
232 * Allocate and free I/O buffers
233 * ==========================================================================
234 */
235
236 /*
237 * Use zio_buf_alloc to allocate ZFS metadata. This data will appear in a
238 * crashdump if the kernel panics, so use it judiciously. Obviously, it's
239 * useful to inspect ZFS metadata, but if possible, we should avoid keeping
240 * excess / transient data in-core during a crashdump.
241 */
242 void *
243 zio_buf_alloc(size_t size)
244 {
245 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
246
247 ASSERT3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
248
249 return (kmem_cache_alloc(zio_buf_cache[c], KM_PUSHPAGE));
250 }
251
252 /*
253 * Use zio_data_buf_alloc to allocate data. The data will not appear in a
254 * crashdump if the kernel panics. This exists so that we will limit the amount
255 * of ZFS data that shows up in a kernel crashdump. (Thus reducing the amount
256 * of kernel heap dumped to disk when the kernel panics)
257 */
258 void *
259 zio_data_buf_alloc(size_t size)
260 {
261 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
262
263 ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
264
265 return (kmem_cache_alloc(zio_data_buf_cache[c], KM_PUSHPAGE));
266 }
267
268 void
269 zio_buf_free(void *buf, size_t size)
270 {
271 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
272
273 ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
274
275 kmem_cache_free(zio_buf_cache[c], buf);
276 }
277
278 void
279 zio_data_buf_free(void *buf, size_t size)
280 {
281 size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
282
283 ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
284
285 kmem_cache_free(zio_data_buf_cache[c], buf);
286 }
287
288 /*
289 * Dedicated I/O buffers to ensure that memory fragmentation never prevents
290 * or significantly delays the issuing of a zio. These buffers are used
291 * to aggregate I/O and could be used for raidz stripes.
292 */
293 void *
294 zio_vdev_alloc(void)
295 {
296 return (kmem_cache_alloc(zio_vdev_cache, KM_PUSHPAGE));
297 }
298
299 void
300 zio_vdev_free(void *buf)
301 {
302 kmem_cache_free(zio_vdev_cache, buf);
303
304 }
305
306 /*
307 * ==========================================================================
308 * Push and pop I/O transform buffers
309 * ==========================================================================
310 */
311 static void
312 zio_push_transform(zio_t *zio, void *data, uint64_t size, uint64_t bufsize,
313 zio_transform_func_t *transform)
314 {
315 zio_transform_t *zt = kmem_alloc(sizeof (zio_transform_t), KM_SLEEP);
316
317 zt->zt_orig_data = zio->io_data;
318 zt->zt_orig_size = zio->io_size;
319 zt->zt_bufsize = bufsize;
320 zt->zt_transform = transform;
321
322 zt->zt_next = zio->io_transform_stack;
323 zio->io_transform_stack = zt;
324
325 zio->io_data = data;
326 zio->io_size = size;
327 }
328
329 static void
330 zio_pop_transforms(zio_t *zio)
331 {
332 zio_transform_t *zt;
333
334 while ((zt = zio->io_transform_stack) != NULL) {
335 if (zt->zt_transform != NULL)
336 zt->zt_transform(zio,
337 zt->zt_orig_data, zt->zt_orig_size);
338
339 if (zt->zt_bufsize != 0)
340 zio_buf_free(zio->io_data, zt->zt_bufsize);
341
342 zio->io_data = zt->zt_orig_data;
343 zio->io_size = zt->zt_orig_size;
344 zio->io_transform_stack = zt->zt_next;
345
346 kmem_free(zt, sizeof (zio_transform_t));
347 }
348 }
349
350 /*
351 * ==========================================================================
352 * I/O transform callbacks for subblocks and decompression
353 * ==========================================================================
354 */
355 static void
356 zio_subblock(zio_t *zio, void *data, uint64_t size)
357 {
358 ASSERT(zio->io_size > size);
359
360 if (zio->io_type == ZIO_TYPE_READ)
361 bcopy(zio->io_data, data, size);
362 }
363
364 static void
365 zio_decompress(zio_t *zio, void *data, uint64_t size)
366 {
367 if (zio->io_error == 0 &&
368 zio_decompress_data(BP_GET_COMPRESS(zio->io_bp),
369 zio->io_data, data, zio->io_size, size) != 0)
370 zio->io_error = SET_ERROR(EIO);
371 }
372
373 /*
374 * ==========================================================================
375 * I/O parent/child relationships and pipeline interlocks
376 * ==========================================================================
377 */
378 /*
379 * NOTE - Callers to zio_walk_parents() and zio_walk_children must
380 * continue calling these functions until they return NULL.
381 * Otherwise, the next caller will pick up the list walk in
382 * some indeterminate state. (Otherwise every caller would
383 * have to pass in a cookie to keep the state represented by
384 * io_walk_link, which gets annoying.)
385 */
386 zio_t *
387 zio_walk_parents(zio_t *cio)
388 {
389 zio_link_t *zl = cio->io_walk_link;
390 list_t *pl = &cio->io_parent_list;
391
392 zl = (zl == NULL) ? list_head(pl) : list_next(pl, zl);
393 cio->io_walk_link = zl;
394
395 if (zl == NULL)
396 return (NULL);
397
398 ASSERT(zl->zl_child == cio);
399 return (zl->zl_parent);
400 }
401
402 zio_t *
403 zio_walk_children(zio_t *pio)
404 {
405 zio_link_t *zl = pio->io_walk_link;
406 list_t *cl = &pio->io_child_list;
407
408 zl = (zl == NULL) ? list_head(cl) : list_next(cl, zl);
409 pio->io_walk_link = zl;
410
411 if (zl == NULL)
412 return (NULL);
413
414 ASSERT(zl->zl_parent == pio);
415 return (zl->zl_child);
416 }
417
418 zio_t *
419 zio_unique_parent(zio_t *cio)
420 {
421 zio_t *pio = zio_walk_parents(cio);
422
423 VERIFY(zio_walk_parents(cio) == NULL);
424 return (pio);
425 }
426
427 void
428 zio_add_child(zio_t *pio, zio_t *cio)
429 {
430 zio_link_t *zl = kmem_cache_alloc(zio_link_cache, KM_SLEEP);
431 int w;
432
433 /*
434 * Logical I/Os can have logical, gang, or vdev children.
435 * Gang I/Os can have gang or vdev children.
436 * Vdev I/Os can only have vdev children.
437 * The following ASSERT captures all of these constraints.
438 */
439 ASSERT(cio->io_child_type <= pio->io_child_type);
440
441 zl->zl_parent = pio;
442 zl->zl_child = cio;
443
444 mutex_enter(&cio->io_lock);
445 mutex_enter(&pio->io_lock);
446
447 ASSERT(pio->io_state[ZIO_WAIT_DONE] == 0);
448
449 for (w = 0; w < ZIO_WAIT_TYPES; w++)
450 pio->io_children[cio->io_child_type][w] += !cio->io_state[w];
451
452 list_insert_head(&pio->io_child_list, zl);
453 list_insert_head(&cio->io_parent_list, zl);
454
455 pio->io_child_count++;
456 cio->io_parent_count++;
457
458 mutex_exit(&pio->io_lock);
459 mutex_exit(&cio->io_lock);
460 }
461
462 static void
463 zio_remove_child(zio_t *pio, zio_t *cio, zio_link_t *zl)
464 {
465 ASSERT(zl->zl_parent == pio);
466 ASSERT(zl->zl_child == cio);
467
468 mutex_enter(&cio->io_lock);
469 mutex_enter(&pio->io_lock);
470
471 list_remove(&pio->io_child_list, zl);
472 list_remove(&cio->io_parent_list, zl);
473
474 pio->io_child_count--;
475 cio->io_parent_count--;
476
477 mutex_exit(&pio->io_lock);
478 mutex_exit(&cio->io_lock);
479
480 kmem_cache_free(zio_link_cache, zl);
481 }
482
483 static boolean_t
484 zio_wait_for_children(zio_t *zio, enum zio_child child, enum zio_wait_type wait)
485 {
486 uint64_t *countp = &zio->io_children[child][wait];
487 boolean_t waiting = B_FALSE;
488
489 mutex_enter(&zio->io_lock);
490 ASSERT(zio->io_stall == NULL);
491 if (*countp != 0) {
492 zio->io_stage >>= 1;
493 zio->io_stall = countp;
494 waiting = B_TRUE;
495 }
496 mutex_exit(&zio->io_lock);
497
498 return (waiting);
499 }
500
501 __attribute__((always_inline))
502 static inline void
503 zio_notify_parent(zio_t *pio, zio_t *zio, enum zio_wait_type wait)
504 {
505 uint64_t *countp = &pio->io_children[zio->io_child_type][wait];
506 int *errorp = &pio->io_child_error[zio->io_child_type];
507
508 mutex_enter(&pio->io_lock);
509 if (zio->io_error && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
510 *errorp = zio_worst_error(*errorp, zio->io_error);
511 pio->io_reexecute |= zio->io_reexecute;
512 ASSERT3U(*countp, >, 0);
513
514 (*countp)--;
515
516 if (*countp == 0 && pio->io_stall == countp) {
517 pio->io_stall = NULL;
518 mutex_exit(&pio->io_lock);
519 __zio_execute(pio);
520 } else {
521 mutex_exit(&pio->io_lock);
522 }
523 }
524
525 static void
526 zio_inherit_child_errors(zio_t *zio, enum zio_child c)
527 {
528 if (zio->io_child_error[c] != 0 && zio->io_error == 0)
529 zio->io_error = zio->io_child_error[c];
530 }
531
532 /*
533 * ==========================================================================
534 * Create the various types of I/O (read, write, free, etc)
535 * ==========================================================================
536 */
537 static zio_t *
538 zio_create(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
539 void *data, uint64_t size, zio_done_func_t *done, void *private,
540 zio_type_t type, zio_priority_t priority, enum zio_flag flags,
541 vdev_t *vd, uint64_t offset, const zbookmark_phys_t *zb,
542 enum zio_stage stage, enum zio_stage pipeline)
543 {
544 zio_t *zio;
545
546 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
547 ASSERT(P2PHASE(size, SPA_MINBLOCKSIZE) == 0);
548 ASSERT(P2PHASE(offset, SPA_MINBLOCKSIZE) == 0);
549
550 ASSERT(!vd || spa_config_held(spa, SCL_STATE_ALL, RW_READER));
551 ASSERT(!bp || !(flags & ZIO_FLAG_CONFIG_WRITER));
552 ASSERT(vd || stage == ZIO_STAGE_OPEN);
553
554 zio = kmem_cache_alloc(zio_cache, KM_SLEEP);
555
556 if (vd != NULL)
557 zio->io_child_type = ZIO_CHILD_VDEV;
558 else if (flags & ZIO_FLAG_GANG_CHILD)
559 zio->io_child_type = ZIO_CHILD_GANG;
560 else if (flags & ZIO_FLAG_DDT_CHILD)
561 zio->io_child_type = ZIO_CHILD_DDT;
562 else
563 zio->io_child_type = ZIO_CHILD_LOGICAL;
564
565 if (bp != NULL) {
566 zio->io_logical = NULL;
567 zio->io_bp = (blkptr_t *)bp;
568 zio->io_bp_copy = *bp;
569 zio->io_bp_orig = *bp;
570 if (type != ZIO_TYPE_WRITE ||
571 zio->io_child_type == ZIO_CHILD_DDT)
572 zio->io_bp = &zio->io_bp_copy; /* so caller can free */
573 if (zio->io_child_type == ZIO_CHILD_LOGICAL)
574 zio->io_logical = zio;
575 if (zio->io_child_type > ZIO_CHILD_GANG && BP_IS_GANG(bp))
576 pipeline |= ZIO_GANG_STAGES;
577 } else {
578 zio->io_logical = NULL;
579 zio->io_bp = NULL;
580 bzero(&zio->io_bp_copy, sizeof (blkptr_t));
581 bzero(&zio->io_bp_orig, sizeof (blkptr_t));
582 }
583
584 zio->io_spa = spa;
585 zio->io_txg = txg;
586 zio->io_ready = NULL;
587 zio->io_physdone = NULL;
588 zio->io_done = done;
589 zio->io_private = private;
590 zio->io_prev_space_delta = 0;
591 zio->io_type = type;
592 zio->io_priority = priority;
593 zio->io_vd = vd;
594 zio->io_vsd = NULL;
595 zio->io_vsd_ops = NULL;
596 zio->io_offset = offset;
597 zio->io_timestamp = 0;
598 zio->io_delta = 0;
599 zio->io_delay = 0;
600 zio->io_orig_data = zio->io_data = data;
601 zio->io_orig_size = zio->io_size = size;
602 zio->io_orig_flags = zio->io_flags = flags;
603 zio->io_orig_stage = zio->io_stage = stage;
604 zio->io_orig_pipeline = zio->io_pipeline = pipeline;
605 bzero(&zio->io_prop, sizeof (zio_prop_t));
606 zio->io_cmd = 0;
607 zio->io_reexecute = 0;
608 zio->io_bp_override = NULL;
609 zio->io_walk_link = NULL;
610 zio->io_transform_stack = NULL;
611 zio->io_error = 0;
612 zio->io_child_count = 0;
613 zio->io_phys_children = 0;
614 zio->io_parent_count = 0;
615 zio->io_stall = NULL;
616 zio->io_gang_leader = NULL;
617 zio->io_gang_tree = NULL;
618 zio->io_executor = NULL;
619 zio->io_waiter = NULL;
620 zio->io_cksum_report = NULL;
621 zio->io_ena = 0;
622 bzero(zio->io_child_error, sizeof (int) * ZIO_CHILD_TYPES);
623 bzero(zio->io_children,
624 sizeof (uint64_t) * ZIO_CHILD_TYPES * ZIO_WAIT_TYPES);
625 bzero(&zio->io_bookmark, sizeof (zbookmark_phys_t));
626
627 zio->io_state[ZIO_WAIT_READY] = (stage >= ZIO_STAGE_READY);
628 zio->io_state[ZIO_WAIT_DONE] = (stage >= ZIO_STAGE_DONE);
629
630 if (zb != NULL)
631 zio->io_bookmark = *zb;
632
633 if (pio != NULL) {
634 if (zio->io_logical == NULL)
635 zio->io_logical = pio->io_logical;
636 if (zio->io_child_type == ZIO_CHILD_GANG)
637 zio->io_gang_leader = pio->io_gang_leader;
638 zio_add_child(pio, zio);
639 }
640
641 taskq_init_ent(&zio->io_tqent);
642
643 return (zio);
644 }
645
646 static void
647 zio_destroy(zio_t *zio)
648 {
649 kmem_cache_free(zio_cache, zio);
650 }
651
652 zio_t *
653 zio_null(zio_t *pio, spa_t *spa, vdev_t *vd, zio_done_func_t *done,
654 void *private, enum zio_flag flags)
655 {
656 zio_t *zio;
657
658 zio = zio_create(pio, spa, 0, NULL, NULL, 0, done, private,
659 ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
660 ZIO_STAGE_OPEN, ZIO_INTERLOCK_PIPELINE);
661
662 return (zio);
663 }
664
665 zio_t *
666 zio_root(spa_t *spa, zio_done_func_t *done, void *private, enum zio_flag flags)
667 {
668 return (zio_null(NULL, spa, NULL, done, private, flags));
669 }
670
671 zio_t *
672 zio_read(zio_t *pio, spa_t *spa, const blkptr_t *bp,
673 void *data, uint64_t size, zio_done_func_t *done, void *private,
674 zio_priority_t priority, enum zio_flag flags, const zbookmark_phys_t *zb)
675 {
676 zio_t *zio;
677
678 zio = zio_create(pio, spa, BP_PHYSICAL_BIRTH(bp), bp,
679 data, size, done, private,
680 ZIO_TYPE_READ, priority, flags, NULL, 0, zb,
681 ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
682 ZIO_DDT_CHILD_READ_PIPELINE : ZIO_READ_PIPELINE);
683
684 return (zio);
685 }
686
687 zio_t *
688 zio_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
689 void *data, uint64_t size, const zio_prop_t *zp,
690 zio_done_func_t *ready, zio_done_func_t *physdone, zio_done_func_t *done,
691 void *private,
692 zio_priority_t priority, enum zio_flag flags, const zbookmark_phys_t *zb)
693 {
694 zio_t *zio;
695
696 ASSERT(zp->zp_checksum >= ZIO_CHECKSUM_OFF &&
697 zp->zp_checksum < ZIO_CHECKSUM_FUNCTIONS &&
698 zp->zp_compress >= ZIO_COMPRESS_OFF &&
699 zp->zp_compress < ZIO_COMPRESS_FUNCTIONS &&
700 DMU_OT_IS_VALID(zp->zp_type) &&
701 zp->zp_level < 32 &&
702 zp->zp_copies > 0 &&
703 zp->zp_copies <= spa_max_replication(spa));
704
705 zio = zio_create(pio, spa, txg, bp, data, size, done, private,
706 ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb,
707 ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
708 ZIO_DDT_CHILD_WRITE_PIPELINE : ZIO_WRITE_PIPELINE);
709
710 zio->io_ready = ready;
711 zio->io_physdone = physdone;
712 zio->io_prop = *zp;
713
714 /*
715 * Data can be NULL if we are going to call zio_write_override() to
716 * provide the already-allocated BP. But we may need the data to
717 * verify a dedup hit (if requested). In this case, don't try to
718 * dedup (just take the already-allocated BP verbatim).
719 */
720 if (data == NULL && zio->io_prop.zp_dedup_verify) {
721 zio->io_prop.zp_dedup = zio->io_prop.zp_dedup_verify = B_FALSE;
722 }
723
724 return (zio);
725 }
726
727 zio_t *
728 zio_rewrite(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, void *data,
729 uint64_t size, zio_done_func_t *done, void *private,
730 zio_priority_t priority, enum zio_flag flags, zbookmark_phys_t *zb)
731 {
732 zio_t *zio;
733
734 zio = zio_create(pio, spa, txg, bp, data, size, done, private,
735 ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb,
736 ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE);
737
738 return (zio);
739 }
740
741 void
742 zio_write_override(zio_t *zio, blkptr_t *bp, int copies, boolean_t nopwrite)
743 {
744 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
745 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
746 ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
747 ASSERT(zio->io_txg == spa_syncing_txg(zio->io_spa));
748
749 /*
750 * We must reset the io_prop to match the values that existed
751 * when the bp was first written by dmu_sync() keeping in mind
752 * that nopwrite and dedup are mutually exclusive.
753 */
754 zio->io_prop.zp_dedup = nopwrite ? B_FALSE : zio->io_prop.zp_dedup;
755 zio->io_prop.zp_nopwrite = nopwrite;
756 zio->io_prop.zp_copies = copies;
757 zio->io_bp_override = bp;
758 }
759
760 void
761 zio_free(spa_t *spa, uint64_t txg, const blkptr_t *bp)
762 {
763
764 /*
765 * The check for EMBEDDED is a performance optimization. We
766 * process the free here (by ignoring it) rather than
767 * putting it on the list and then processing it in zio_free_sync().
768 */
769 if (BP_IS_EMBEDDED(bp))
770 return;
771 metaslab_check_free(spa, bp);
772
773 /*
774 * Frees that are for the currently-syncing txg, are not going to be
775 * deferred, and which will not need to do a read (i.e. not GANG or
776 * DEDUP), can be processed immediately. Otherwise, put them on the
777 * in-memory list for later processing.
778 */
779 if (BP_IS_GANG(bp) || BP_GET_DEDUP(bp) ||
780 txg != spa->spa_syncing_txg ||
781 spa_sync_pass(spa) >= zfs_sync_pass_deferred_free) {
782 bplist_append(&spa->spa_free_bplist[txg & TXG_MASK], bp);
783 } else {
784 VERIFY0(zio_wait(zio_free_sync(NULL, spa, txg, bp, 0)));
785 }
786 }
787
788 zio_t *
789 zio_free_sync(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
790 enum zio_flag flags)
791 {
792 zio_t *zio;
793 enum zio_stage stage = ZIO_FREE_PIPELINE;
794
795 ASSERT(!BP_IS_HOLE(bp));
796 ASSERT(spa_syncing_txg(spa) == txg);
797 ASSERT(spa_sync_pass(spa) < zfs_sync_pass_deferred_free);
798
799 if (BP_IS_EMBEDDED(bp))
800 return (zio_null(pio, spa, NULL, NULL, NULL, 0));
801
802 metaslab_check_free(spa, bp);
803 arc_freed(spa, bp);
804
805 /*
806 * GANG and DEDUP blocks can induce a read (for the gang block header,
807 * or the DDT), so issue them asynchronously so that this thread is
808 * not tied up.
809 */
810 if (BP_IS_GANG(bp) || BP_GET_DEDUP(bp))
811 stage |= ZIO_STAGE_ISSUE_ASYNC;
812
813 zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
814 NULL, NULL, ZIO_TYPE_FREE, ZIO_PRIORITY_NOW, flags,
815 NULL, 0, NULL, ZIO_STAGE_OPEN, stage);
816
817 return (zio);
818 }
819
820 zio_t *
821 zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
822 zio_done_func_t *done, void *private, enum zio_flag flags)
823 {
824 zio_t *zio;
825
826 dprintf_bp(bp, "claiming in txg %llu", txg);
827
828 if (BP_IS_EMBEDDED(bp))
829 return (zio_null(pio, spa, NULL, NULL, NULL, 0));
830
831 /*
832 * A claim is an allocation of a specific block. Claims are needed
833 * to support immediate writes in the intent log. The issue is that
834 * immediate writes contain committed data, but in a txg that was
835 * *not* committed. Upon opening the pool after an unclean shutdown,
836 * the intent log claims all blocks that contain immediate write data
837 * so that the SPA knows they're in use.
838 *
839 * All claims *must* be resolved in the first txg -- before the SPA
840 * starts allocating blocks -- so that nothing is allocated twice.
841 * If txg == 0 we just verify that the block is claimable.
842 */
843 ASSERT3U(spa->spa_uberblock.ub_rootbp.blk_birth, <, spa_first_txg(spa));
844 ASSERT(txg == spa_first_txg(spa) || txg == 0);
845 ASSERT(!BP_GET_DEDUP(bp) || !spa_writeable(spa)); /* zdb(1M) */
846
847 zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
848 done, private, ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW, flags,
849 NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE);
850
851 return (zio);
852 }
853
854 zio_t *
855 zio_ioctl(zio_t *pio, spa_t *spa, vdev_t *vd, int cmd,
856 zio_done_func_t *done, void *private, enum zio_flag flags)
857 {
858 zio_t *zio;
859 int c;
860
861 if (vd->vdev_children == 0) {
862 zio = zio_create(pio, spa, 0, NULL, NULL, 0, done, private,
863 ZIO_TYPE_IOCTL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
864 ZIO_STAGE_OPEN, ZIO_IOCTL_PIPELINE);
865
866 zio->io_cmd = cmd;
867 } else {
868 zio = zio_null(pio, spa, NULL, NULL, NULL, flags);
869
870 for (c = 0; c < vd->vdev_children; c++)
871 zio_nowait(zio_ioctl(zio, spa, vd->vdev_child[c], cmd,
872 done, private, flags));
873 }
874
875 return (zio);
876 }
877
878 zio_t *
879 zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
880 void *data, int checksum, zio_done_func_t *done, void *private,
881 zio_priority_t priority, enum zio_flag flags, boolean_t labels)
882 {
883 zio_t *zio;
884
885 ASSERT(vd->vdev_children == 0);
886 ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
887 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
888 ASSERT3U(offset + size, <=, vd->vdev_psize);
889
890 zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, done, private,
891 ZIO_TYPE_READ, priority, flags | ZIO_FLAG_PHYSICAL, vd, offset,
892 NULL, ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE);
893
894 zio->io_prop.zp_checksum = checksum;
895
896 return (zio);
897 }
898
899 zio_t *
900 zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
901 void *data, int checksum, zio_done_func_t *done, void *private,
902 zio_priority_t priority, enum zio_flag flags, boolean_t labels)
903 {
904 zio_t *zio;
905
906 ASSERT(vd->vdev_children == 0);
907 ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
908 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
909 ASSERT3U(offset + size, <=, vd->vdev_psize);
910
911 zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, done, private,
912 ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_PHYSICAL, vd, offset,
913 NULL, ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE);
914
915 zio->io_prop.zp_checksum = checksum;
916
917 if (zio_checksum_table[checksum].ci_eck) {
918 /*
919 * zec checksums are necessarily destructive -- they modify
920 * the end of the write buffer to hold the verifier/checksum.
921 * Therefore, we must make a local copy in case the data is
922 * being written to multiple places in parallel.
923 */
924 void *wbuf = zio_buf_alloc(size);
925 bcopy(data, wbuf, size);
926 zio_push_transform(zio, wbuf, size, size, NULL);
927 }
928
929 return (zio);
930 }
931
932 /*
933 * Create a child I/O to do some work for us.
934 */
935 zio_t *
936 zio_vdev_child_io(zio_t *pio, blkptr_t *bp, vdev_t *vd, uint64_t offset,
937 void *data, uint64_t size, int type, zio_priority_t priority,
938 enum zio_flag flags, zio_done_func_t *done, void *private)
939 {
940 enum zio_stage pipeline = ZIO_VDEV_CHILD_PIPELINE;
941 zio_t *zio;
942
943 ASSERT(vd->vdev_parent ==
944 (pio->io_vd ? pio->io_vd : pio->io_spa->spa_root_vdev));
945
946 if (type == ZIO_TYPE_READ && bp != NULL) {
947 /*
948 * If we have the bp, then the child should perform the
949 * checksum and the parent need not. This pushes error
950 * detection as close to the leaves as possible and
951 * eliminates redundant checksums in the interior nodes.
952 */
953 pipeline |= ZIO_STAGE_CHECKSUM_VERIFY;
954 pio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
955 }
956
957 if (vd->vdev_children == 0)
958 offset += VDEV_LABEL_START_SIZE;
959
960 flags |= ZIO_VDEV_CHILD_FLAGS(pio) | ZIO_FLAG_DONT_PROPAGATE;
961
962 /*
963 * If we've decided to do a repair, the write is not speculative --
964 * even if the original read was.
965 */
966 if (flags & ZIO_FLAG_IO_REPAIR)
967 flags &= ~ZIO_FLAG_SPECULATIVE;
968
969 zio = zio_create(pio, pio->io_spa, pio->io_txg, bp, data, size,
970 done, private, type, priority, flags, vd, offset, &pio->io_bookmark,
971 ZIO_STAGE_VDEV_IO_START >> 1, pipeline);
972
973 zio->io_physdone = pio->io_physdone;
974 if (vd->vdev_ops->vdev_op_leaf && zio->io_logical != NULL)
975 zio->io_logical->io_phys_children++;
976
977 return (zio);
978 }
979
980 zio_t *
981 zio_vdev_delegated_io(vdev_t *vd, uint64_t offset, void *data, uint64_t size,
982 int type, zio_priority_t priority, enum zio_flag flags,
983 zio_done_func_t *done, void *private)
984 {
985 zio_t *zio;
986
987 ASSERT(vd->vdev_ops->vdev_op_leaf);
988
989 zio = zio_create(NULL, vd->vdev_spa, 0, NULL,
990 data, size, done, private, type, priority,
991 flags | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY | ZIO_FLAG_DELEGATED,
992 vd, offset, NULL,
993 ZIO_STAGE_VDEV_IO_START >> 1, ZIO_VDEV_CHILD_PIPELINE);
994
995 return (zio);
996 }
997
998 void
999 zio_flush(zio_t *zio, vdev_t *vd)
1000 {
1001 zio_nowait(zio_ioctl(zio, zio->io_spa, vd, DKIOCFLUSHWRITECACHE,
1002 NULL, NULL,
1003 ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY));
1004 }
1005
1006 void
1007 zio_shrink(zio_t *zio, uint64_t size)
1008 {
1009 ASSERT(zio->io_executor == NULL);
1010 ASSERT(zio->io_orig_size == zio->io_size);
1011 ASSERT(size <= zio->io_size);
1012
1013 /*
1014 * We don't shrink for raidz because of problems with the
1015 * reconstruction when reading back less than the block size.
1016 * Note, BP_IS_RAIDZ() assumes no compression.
1017 */
1018 ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
1019 if (!BP_IS_RAIDZ(zio->io_bp))
1020 zio->io_orig_size = zio->io_size = size;
1021 }
1022
1023 /*
1024 * ==========================================================================
1025 * Prepare to read and write logical blocks
1026 * ==========================================================================
1027 */
1028
1029 static int
1030 zio_read_bp_init(zio_t *zio)
1031 {
1032 blkptr_t *bp = zio->io_bp;
1033
1034 if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF &&
1035 zio->io_child_type == ZIO_CHILD_LOGICAL &&
1036 !(zio->io_flags & ZIO_FLAG_RAW)) {
1037 uint64_t psize =
1038 BP_IS_EMBEDDED(bp) ? BPE_GET_PSIZE(bp) : BP_GET_PSIZE(bp);
1039 void *cbuf = zio_buf_alloc(psize);
1040
1041 zio_push_transform(zio, cbuf, psize, psize, zio_decompress);
1042 }
1043
1044 if (BP_IS_EMBEDDED(bp) && BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA) {
1045 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1046 decode_embedded_bp_compressed(bp, zio->io_data);
1047 } else {
1048 ASSERT(!BP_IS_EMBEDDED(bp));
1049 }
1050
1051 if (!DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) && BP_GET_LEVEL(bp) == 0)
1052 zio->io_flags |= ZIO_FLAG_DONT_CACHE;
1053
1054 if (BP_GET_TYPE(bp) == DMU_OT_DDT_ZAP)
1055 zio->io_flags |= ZIO_FLAG_DONT_CACHE;
1056
1057 if (BP_GET_DEDUP(bp) && zio->io_child_type == ZIO_CHILD_LOGICAL)
1058 zio->io_pipeline = ZIO_DDT_READ_PIPELINE;
1059
1060 return (ZIO_PIPELINE_CONTINUE);
1061 }
1062
1063 static int
1064 zio_write_bp_init(zio_t *zio)
1065 {
1066 spa_t *spa = zio->io_spa;
1067 zio_prop_t *zp = &zio->io_prop;
1068 enum zio_compress compress = zp->zp_compress;
1069 blkptr_t *bp = zio->io_bp;
1070 uint64_t lsize = zio->io_size;
1071 uint64_t psize = lsize;
1072 int pass = 1;
1073
1074 /*
1075 * If our children haven't all reached the ready stage,
1076 * wait for them and then repeat this pipeline stage.
1077 */
1078 if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_READY) ||
1079 zio_wait_for_children(zio, ZIO_CHILD_LOGICAL, ZIO_WAIT_READY))
1080 return (ZIO_PIPELINE_STOP);
1081
1082 if (!IO_IS_ALLOCATING(zio))
1083 return (ZIO_PIPELINE_CONTINUE);
1084
1085 ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
1086
1087 if (zio->io_bp_override) {
1088 ASSERT(bp->blk_birth != zio->io_txg);
1089 ASSERT(BP_GET_DEDUP(zio->io_bp_override) == 0);
1090
1091 *bp = *zio->io_bp_override;
1092 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1093
1094 if (BP_IS_EMBEDDED(bp))
1095 return (ZIO_PIPELINE_CONTINUE);
1096
1097 /*
1098 * If we've been overridden and nopwrite is set then
1099 * set the flag accordingly to indicate that a nopwrite
1100 * has already occurred.
1101 */
1102 if (!BP_IS_HOLE(bp) && zp->zp_nopwrite) {
1103 ASSERT(!zp->zp_dedup);
1104 zio->io_flags |= ZIO_FLAG_NOPWRITE;
1105 return (ZIO_PIPELINE_CONTINUE);
1106 }
1107
1108 ASSERT(!zp->zp_nopwrite);
1109
1110 if (BP_IS_HOLE(bp) || !zp->zp_dedup)
1111 return (ZIO_PIPELINE_CONTINUE);
1112
1113 ASSERT(zio_checksum_table[zp->zp_checksum].ci_dedup ||
1114 zp->zp_dedup_verify);
1115
1116 if (BP_GET_CHECKSUM(bp) == zp->zp_checksum) {
1117 BP_SET_DEDUP(bp, 1);
1118 zio->io_pipeline |= ZIO_STAGE_DDT_WRITE;
1119 return (ZIO_PIPELINE_CONTINUE);
1120 }
1121 zio->io_bp_override = NULL;
1122 BP_ZERO(bp);
1123 }
1124
1125 if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg) {
1126 /*
1127 * We're rewriting an existing block, which means we're
1128 * working on behalf of spa_sync(). For spa_sync() to
1129 * converge, it must eventually be the case that we don't
1130 * have to allocate new blocks. But compression changes
1131 * the blocksize, which forces a reallocate, and makes
1132 * convergence take longer. Therefore, after the first
1133 * few passes, stop compressing to ensure convergence.
1134 */
1135 pass = spa_sync_pass(spa);
1136
1137 ASSERT(zio->io_txg == spa_syncing_txg(spa));
1138 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1139 ASSERT(!BP_GET_DEDUP(bp));
1140
1141 if (pass >= zfs_sync_pass_dont_compress)
1142 compress = ZIO_COMPRESS_OFF;
1143
1144 /* Make sure someone doesn't change their mind on overwrites */
1145 ASSERT(BP_IS_EMBEDDED(bp) || MIN(zp->zp_copies + BP_IS_GANG(bp),
1146 spa_max_replication(spa)) == BP_GET_NDVAS(bp));
1147 }
1148
1149 if (compress != ZIO_COMPRESS_OFF) {
1150 void *cbuf = zio_buf_alloc(lsize);
1151 psize = zio_compress_data(compress, zio->io_data, cbuf, lsize);
1152 if (psize == 0 || psize == lsize) {
1153 compress = ZIO_COMPRESS_OFF;
1154 zio_buf_free(cbuf, lsize);
1155 } else if (!zp->zp_dedup && psize <= BPE_PAYLOAD_SIZE &&
1156 zp->zp_level == 0 && !DMU_OT_HAS_FILL(zp->zp_type) &&
1157 spa_feature_is_enabled(spa, SPA_FEATURE_EMBEDDED_DATA)) {
1158 encode_embedded_bp_compressed(bp,
1159 cbuf, compress, lsize, psize);
1160 BPE_SET_ETYPE(bp, BP_EMBEDDED_TYPE_DATA);
1161 BP_SET_TYPE(bp, zio->io_prop.zp_type);
1162 BP_SET_LEVEL(bp, zio->io_prop.zp_level);
1163 zio_buf_free(cbuf, lsize);
1164 bp->blk_birth = zio->io_txg;
1165 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1166 ASSERT(spa_feature_is_active(spa,
1167 SPA_FEATURE_EMBEDDED_DATA));
1168 return (ZIO_PIPELINE_CONTINUE);
1169 } else {
1170 /*
1171 * Round up compressed size to MINBLOCKSIZE and
1172 * zero the tail.
1173 */
1174 size_t rounded =
1175 P2ROUNDUP(psize, (size_t)SPA_MINBLOCKSIZE);
1176 if (rounded > psize) {
1177 bzero((char *)cbuf + psize, rounded - psize);
1178 psize = rounded;
1179 }
1180 if (psize == lsize) {
1181 compress = ZIO_COMPRESS_OFF;
1182 zio_buf_free(cbuf, lsize);
1183 } else {
1184 zio_push_transform(zio, cbuf,
1185 psize, lsize, NULL);
1186 }
1187 }
1188 }
1189
1190 /*
1191 * The final pass of spa_sync() must be all rewrites, but the first
1192 * few passes offer a trade-off: allocating blocks defers convergence,
1193 * but newly allocated blocks are sequential, so they can be written
1194 * to disk faster. Therefore, we allow the first few passes of
1195 * spa_sync() to allocate new blocks, but force rewrites after that.
1196 * There should only be a handful of blocks after pass 1 in any case.
1197 */
1198 if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg &&
1199 BP_GET_PSIZE(bp) == psize &&
1200 pass >= zfs_sync_pass_rewrite) {
1201 enum zio_stage gang_stages = zio->io_pipeline & ZIO_GANG_STAGES;
1202 ASSERT(psize != 0);
1203 zio->io_pipeline = ZIO_REWRITE_PIPELINE | gang_stages;
1204 zio->io_flags |= ZIO_FLAG_IO_REWRITE;
1205 } else {
1206 BP_ZERO(bp);
1207 zio->io_pipeline = ZIO_WRITE_PIPELINE;
1208 }
1209
1210 if (psize == 0) {
1211 if (zio->io_bp_orig.blk_birth != 0 &&
1212 spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) {
1213 BP_SET_LSIZE(bp, lsize);
1214 BP_SET_TYPE(bp, zp->zp_type);
1215 BP_SET_LEVEL(bp, zp->zp_level);
1216 BP_SET_BIRTH(bp, zio->io_txg, 0);
1217 }
1218 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1219 } else {
1220 ASSERT(zp->zp_checksum != ZIO_CHECKSUM_GANG_HEADER);
1221 BP_SET_LSIZE(bp, lsize);
1222 BP_SET_TYPE(bp, zp->zp_type);
1223 BP_SET_LEVEL(bp, zp->zp_level);
1224 BP_SET_PSIZE(bp, psize);
1225 BP_SET_COMPRESS(bp, compress);
1226 BP_SET_CHECKSUM(bp, zp->zp_checksum);
1227 BP_SET_DEDUP(bp, zp->zp_dedup);
1228 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
1229 if (zp->zp_dedup) {
1230 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1231 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
1232 zio->io_pipeline = ZIO_DDT_WRITE_PIPELINE;
1233 }
1234 if (zp->zp_nopwrite) {
1235 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1236 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
1237 zio->io_pipeline |= ZIO_STAGE_NOP_WRITE;
1238 }
1239 }
1240
1241 return (ZIO_PIPELINE_CONTINUE);
1242 }
1243
1244 static int
1245 zio_free_bp_init(zio_t *zio)
1246 {
1247 blkptr_t *bp = zio->io_bp;
1248
1249 if (zio->io_child_type == ZIO_CHILD_LOGICAL) {
1250 if (BP_GET_DEDUP(bp))
1251 zio->io_pipeline = ZIO_DDT_FREE_PIPELINE;
1252 }
1253
1254 return (ZIO_PIPELINE_CONTINUE);
1255 }
1256
1257 /*
1258 * ==========================================================================
1259 * Execute the I/O pipeline
1260 * ==========================================================================
1261 */
1262
1263 static void
1264 zio_taskq_dispatch(zio_t *zio, zio_taskq_type_t q, boolean_t cutinline)
1265 {
1266 spa_t *spa = zio->io_spa;
1267 zio_type_t t = zio->io_type;
1268 int flags = (cutinline ? TQ_FRONT : 0);
1269
1270 /*
1271 * If we're a config writer or a probe, the normal issue and
1272 * interrupt threads may all be blocked waiting for the config lock.
1273 * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL.
1274 */
1275 if (zio->io_flags & (ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_PROBE))
1276 t = ZIO_TYPE_NULL;
1277
1278 /*
1279 * A similar issue exists for the L2ARC write thread until L2ARC 2.0.
1280 */
1281 if (t == ZIO_TYPE_WRITE && zio->io_vd && zio->io_vd->vdev_aux)
1282 t = ZIO_TYPE_NULL;
1283
1284 /*
1285 * If this is a high priority I/O, then use the high priority taskq if
1286 * available.
1287 */
1288 if (zio->io_priority == ZIO_PRIORITY_NOW &&
1289 spa->spa_zio_taskq[t][q + 1].stqs_count != 0)
1290 q++;
1291
1292 ASSERT3U(q, <, ZIO_TASKQ_TYPES);
1293
1294 /*
1295 * NB: We are assuming that the zio can only be dispatched
1296 * to a single taskq at a time. It would be a grievous error
1297 * to dispatch the zio to another taskq at the same time.
1298 */
1299 ASSERT(taskq_empty_ent(&zio->io_tqent));
1300 spa_taskq_dispatch_ent(spa, t, q, (task_func_t *)zio_execute, zio,
1301 flags, &zio->io_tqent);
1302 }
1303
1304 static boolean_t
1305 zio_taskq_member(zio_t *zio, zio_taskq_type_t q)
1306 {
1307 kthread_t *executor = zio->io_executor;
1308 spa_t *spa = zio->io_spa;
1309 zio_type_t t;
1310
1311 for (t = 0; t < ZIO_TYPES; t++) {
1312 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
1313 uint_t i;
1314 for (i = 0; i < tqs->stqs_count; i++) {
1315 if (taskq_member(tqs->stqs_taskq[i], executor))
1316 return (B_TRUE);
1317 }
1318 }
1319
1320 return (B_FALSE);
1321 }
1322
1323 static int
1324 zio_issue_async(zio_t *zio)
1325 {
1326 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
1327
1328 return (ZIO_PIPELINE_STOP);
1329 }
1330
1331 void
1332 zio_interrupt(zio_t *zio)
1333 {
1334 zio_taskq_dispatch(zio, ZIO_TASKQ_INTERRUPT, B_FALSE);
1335 }
1336
1337 /*
1338 * Execute the I/O pipeline until one of the following occurs:
1339 * (1) the I/O completes; (2) the pipeline stalls waiting for
1340 * dependent child I/Os; (3) the I/O issues, so we're waiting
1341 * for an I/O completion interrupt; (4) the I/O is delegated by
1342 * vdev-level caching or aggregation; (5) the I/O is deferred
1343 * due to vdev-level queueing; (6) the I/O is handed off to
1344 * another thread. In all cases, the pipeline stops whenever
1345 * there's no CPU work; it never burns a thread in cv_wait_io().
1346 *
1347 * There's no locking on io_stage because there's no legitimate way
1348 * for multiple threads to be attempting to process the same I/O.
1349 */
1350 static zio_pipe_stage_t *zio_pipeline[];
1351
1352 /*
1353 * zio_execute() is a wrapper around the static function
1354 * __zio_execute() so that we can force __zio_execute() to be
1355 * inlined. This reduces stack overhead which is important
1356 * because __zio_execute() is called recursively in several zio
1357 * code paths. zio_execute() itself cannot be inlined because
1358 * it is externally visible.
1359 */
1360 void
1361 zio_execute(zio_t *zio)
1362 {
1363 fstrans_cookie_t cookie;
1364
1365 cookie = spl_fstrans_mark();
1366 __zio_execute(zio);
1367 spl_fstrans_unmark(cookie);
1368 }
1369
1370 __attribute__((always_inline))
1371 static inline void
1372 __zio_execute(zio_t *zio)
1373 {
1374 zio->io_executor = curthread;
1375
1376 while (zio->io_stage < ZIO_STAGE_DONE) {
1377 enum zio_stage pipeline = zio->io_pipeline;
1378 enum zio_stage stage = zio->io_stage;
1379 dsl_pool_t *dp;
1380 boolean_t cut;
1381 int rv;
1382
1383 ASSERT(!MUTEX_HELD(&zio->io_lock));
1384 ASSERT(ISP2(stage));
1385 ASSERT(zio->io_stall == NULL);
1386
1387 do {
1388 stage <<= 1;
1389 } while ((stage & pipeline) == 0);
1390
1391 ASSERT(stage <= ZIO_STAGE_DONE);
1392
1393 dp = spa_get_dsl(zio->io_spa);
1394 cut = (stage == ZIO_STAGE_VDEV_IO_START) ?
1395 zio_requeue_io_start_cut_in_line : B_FALSE;
1396
1397 /*
1398 * If we are in interrupt context and this pipeline stage
1399 * will grab a config lock that is held across I/O,
1400 * or may wait for an I/O that needs an interrupt thread
1401 * to complete, issue async to avoid deadlock.
1402 *
1403 * For VDEV_IO_START, we cut in line so that the io will
1404 * be sent to disk promptly.
1405 */
1406 if ((stage & ZIO_BLOCKING_STAGES) && zio->io_vd == NULL &&
1407 zio_taskq_member(zio, ZIO_TASKQ_INTERRUPT)) {
1408 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut);
1409 return;
1410 }
1411
1412 /*
1413 * If we executing in the context of the tx_sync_thread,
1414 * or we are performing pool initialization outside of a
1415 * zio_taskq[ZIO_TASKQ_ISSUE|ZIO_TASKQ_ISSUE_HIGH] context.
1416 * Then issue the zio asynchronously to minimize stack usage
1417 * for these deep call paths.
1418 */
1419 if ((dp && curthread == dp->dp_tx.tx_sync_thread) ||
1420 (dp && spa_is_initializing(dp->dp_spa) &&
1421 !zio_taskq_member(zio, ZIO_TASKQ_ISSUE) &&
1422 !zio_taskq_member(zio, ZIO_TASKQ_ISSUE_HIGH))) {
1423 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut);
1424 return;
1425 }
1426
1427 zio->io_stage = stage;
1428 rv = zio_pipeline[highbit64(stage) - 1](zio);
1429
1430 if (rv == ZIO_PIPELINE_STOP)
1431 return;
1432
1433 ASSERT(rv == ZIO_PIPELINE_CONTINUE);
1434 }
1435 }
1436
1437
1438 /*
1439 * ==========================================================================
1440 * Initiate I/O, either sync or async
1441 * ==========================================================================
1442 */
1443 int
1444 zio_wait(zio_t *zio)
1445 {
1446 int error;
1447
1448 ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
1449 ASSERT(zio->io_executor == NULL);
1450
1451 zio->io_waiter = curthread;
1452
1453 __zio_execute(zio);
1454
1455 mutex_enter(&zio->io_lock);
1456 while (zio->io_executor != NULL)
1457 cv_wait_io(&zio->io_cv, &zio->io_lock);
1458 mutex_exit(&zio->io_lock);
1459
1460 error = zio->io_error;
1461 zio_destroy(zio);
1462
1463 return (error);
1464 }
1465
1466 void
1467 zio_nowait(zio_t *zio)
1468 {
1469 ASSERT(zio->io_executor == NULL);
1470
1471 if (zio->io_child_type == ZIO_CHILD_LOGICAL &&
1472 zio_unique_parent(zio) == NULL) {
1473 zio_t *pio;
1474
1475 /*
1476 * This is a logical async I/O with no parent to wait for it.
1477 * We add it to the spa_async_root_zio "Godfather" I/O which
1478 * will ensure they complete prior to unloading the pool.
1479 */
1480 spa_t *spa = zio->io_spa;
1481 kpreempt_disable();
1482 pio = spa->spa_async_zio_root[CPU_SEQID];
1483 kpreempt_enable();
1484
1485 zio_add_child(pio, zio);
1486 }
1487
1488 __zio_execute(zio);
1489 }
1490
1491 /*
1492 * ==========================================================================
1493 * Reexecute or suspend/resume failed I/O
1494 * ==========================================================================
1495 */
1496
1497 static void
1498 zio_reexecute(zio_t *pio)
1499 {
1500 zio_t *cio, *cio_next;
1501 int c, w;
1502
1503 ASSERT(pio->io_child_type == ZIO_CHILD_LOGICAL);
1504 ASSERT(pio->io_orig_stage == ZIO_STAGE_OPEN);
1505 ASSERT(pio->io_gang_leader == NULL);
1506 ASSERT(pio->io_gang_tree == NULL);
1507
1508 pio->io_flags = pio->io_orig_flags;
1509 pio->io_stage = pio->io_orig_stage;
1510 pio->io_pipeline = pio->io_orig_pipeline;
1511 pio->io_reexecute = 0;
1512 pio->io_flags |= ZIO_FLAG_REEXECUTED;
1513 pio->io_error = 0;
1514 for (w = 0; w < ZIO_WAIT_TYPES; w++)
1515 pio->io_state[w] = 0;
1516 for (c = 0; c < ZIO_CHILD_TYPES; c++)
1517 pio->io_child_error[c] = 0;
1518
1519 if (IO_IS_ALLOCATING(pio))
1520 BP_ZERO(pio->io_bp);
1521
1522 /*
1523 * As we reexecute pio's children, new children could be created.
1524 * New children go to the head of pio's io_child_list, however,
1525 * so we will (correctly) not reexecute them. The key is that
1526 * the remainder of pio's io_child_list, from 'cio_next' onward,
1527 * cannot be affected by any side effects of reexecuting 'cio'.
1528 */
1529 for (cio = zio_walk_children(pio); cio != NULL; cio = cio_next) {
1530 cio_next = zio_walk_children(pio);
1531 mutex_enter(&pio->io_lock);
1532 for (w = 0; w < ZIO_WAIT_TYPES; w++)
1533 pio->io_children[cio->io_child_type][w]++;
1534 mutex_exit(&pio->io_lock);
1535 zio_reexecute(cio);
1536 }
1537
1538 /*
1539 * Now that all children have been reexecuted, execute the parent.
1540 * We don't reexecute "The Godfather" I/O here as it's the
1541 * responsibility of the caller to wait on him.
1542 */
1543 if (!(pio->io_flags & ZIO_FLAG_GODFATHER))
1544 __zio_execute(pio);
1545 }
1546
1547 void
1548 zio_suspend(spa_t *spa, zio_t *zio)
1549 {
1550 if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_PANIC)
1551 fm_panic("Pool '%s' has encountered an uncorrectable I/O "
1552 "failure and the failure mode property for this pool "
1553 "is set to panic.", spa_name(spa));
1554
1555 cmn_err(CE_WARN, "Pool '%s' has encountered an uncorrectable I/O "
1556 "failure and has been suspended.\n", spa_name(spa));
1557
1558 zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE, spa, NULL, NULL, 0, 0);
1559
1560 mutex_enter(&spa->spa_suspend_lock);
1561
1562 if (spa->spa_suspend_zio_root == NULL)
1563 spa->spa_suspend_zio_root = zio_root(spa, NULL, NULL,
1564 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
1565 ZIO_FLAG_GODFATHER);
1566
1567 spa->spa_suspended = B_TRUE;
1568
1569 if (zio != NULL) {
1570 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
1571 ASSERT(zio != spa->spa_suspend_zio_root);
1572 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1573 ASSERT(zio_unique_parent(zio) == NULL);
1574 ASSERT(zio->io_stage == ZIO_STAGE_DONE);
1575 zio_add_child(spa->spa_suspend_zio_root, zio);
1576 }
1577
1578 mutex_exit(&spa->spa_suspend_lock);
1579 }
1580
1581 int
1582 zio_resume(spa_t *spa)
1583 {
1584 zio_t *pio;
1585
1586 /*
1587 * Reexecute all previously suspended i/o.
1588 */
1589 mutex_enter(&spa->spa_suspend_lock);
1590 spa->spa_suspended = B_FALSE;
1591 cv_broadcast(&spa->spa_suspend_cv);
1592 pio = spa->spa_suspend_zio_root;
1593 spa->spa_suspend_zio_root = NULL;
1594 mutex_exit(&spa->spa_suspend_lock);
1595
1596 if (pio == NULL)
1597 return (0);
1598
1599 zio_reexecute(pio);
1600 return (zio_wait(pio));
1601 }
1602
1603 void
1604 zio_resume_wait(spa_t *spa)
1605 {
1606 mutex_enter(&spa->spa_suspend_lock);
1607 while (spa_suspended(spa))
1608 cv_wait(&spa->spa_suspend_cv, &spa->spa_suspend_lock);
1609 mutex_exit(&spa->spa_suspend_lock);
1610 }
1611
1612 /*
1613 * ==========================================================================
1614 * Gang blocks.
1615 *
1616 * A gang block is a collection of small blocks that looks to the DMU
1617 * like one large block. When zio_dva_allocate() cannot find a block
1618 * of the requested size, due to either severe fragmentation or the pool
1619 * being nearly full, it calls zio_write_gang_block() to construct the
1620 * block from smaller fragments.
1621 *
1622 * A gang block consists of a gang header (zio_gbh_phys_t) and up to
1623 * three (SPA_GBH_NBLKPTRS) gang members. The gang header is just like
1624 * an indirect block: it's an array of block pointers. It consumes
1625 * only one sector and hence is allocatable regardless of fragmentation.
1626 * The gang header's bps point to its gang members, which hold the data.
1627 *
1628 * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg>
1629 * as the verifier to ensure uniqueness of the SHA256 checksum.
1630 * Critically, the gang block bp's blk_cksum is the checksum of the data,
1631 * not the gang header. This ensures that data block signatures (needed for
1632 * deduplication) are independent of how the block is physically stored.
1633 *
1634 * Gang blocks can be nested: a gang member may itself be a gang block.
1635 * Thus every gang block is a tree in which root and all interior nodes are
1636 * gang headers, and the leaves are normal blocks that contain user data.
1637 * The root of the gang tree is called the gang leader.
1638 *
1639 * To perform any operation (read, rewrite, free, claim) on a gang block,
1640 * zio_gang_assemble() first assembles the gang tree (minus data leaves)
1641 * in the io_gang_tree field of the original logical i/o by recursively
1642 * reading the gang leader and all gang headers below it. This yields
1643 * an in-core tree containing the contents of every gang header and the
1644 * bps for every constituent of the gang block.
1645 *
1646 * With the gang tree now assembled, zio_gang_issue() just walks the gang tree
1647 * and invokes a callback on each bp. To free a gang block, zio_gang_issue()
1648 * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp.
1649 * zio_claim_gang() provides a similarly trivial wrapper for zio_claim().
1650 * zio_read_gang() is a wrapper around zio_read() that omits reading gang
1651 * headers, since we already have those in io_gang_tree. zio_rewrite_gang()
1652 * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite()
1653 * of the gang header plus zio_checksum_compute() of the data to update the
1654 * gang header's blk_cksum as described above.
1655 *
1656 * The two-phase assemble/issue model solves the problem of partial failure --
1657 * what if you'd freed part of a gang block but then couldn't read the
1658 * gang header for another part? Assembling the entire gang tree first
1659 * ensures that all the necessary gang header I/O has succeeded before
1660 * starting the actual work of free, claim, or write. Once the gang tree
1661 * is assembled, free and claim are in-memory operations that cannot fail.
1662 *
1663 * In the event that a gang write fails, zio_dva_unallocate() walks the
1664 * gang tree to immediately free (i.e. insert back into the space map)
1665 * everything we've allocated. This ensures that we don't get ENOSPC
1666 * errors during repeated suspend/resume cycles due to a flaky device.
1667 *
1668 * Gang rewrites only happen during sync-to-convergence. If we can't assemble
1669 * the gang tree, we won't modify the block, so we can safely defer the free
1670 * (knowing that the block is still intact). If we *can* assemble the gang
1671 * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free
1672 * each constituent bp and we can allocate a new block on the next sync pass.
1673 *
1674 * In all cases, the gang tree allows complete recovery from partial failure.
1675 * ==========================================================================
1676 */
1677
1678 static zio_t *
1679 zio_read_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1680 {
1681 if (gn != NULL)
1682 return (pio);
1683
1684 return (zio_read(pio, pio->io_spa, bp, data, BP_GET_PSIZE(bp),
1685 NULL, NULL, pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
1686 &pio->io_bookmark));
1687 }
1688
1689 zio_t *
1690 zio_rewrite_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1691 {
1692 zio_t *zio;
1693
1694 if (gn != NULL) {
1695 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
1696 gn->gn_gbh, SPA_GANGBLOCKSIZE, NULL, NULL, pio->io_priority,
1697 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
1698 /*
1699 * As we rewrite each gang header, the pipeline will compute
1700 * a new gang block header checksum for it; but no one will
1701 * compute a new data checksum, so we do that here. The one
1702 * exception is the gang leader: the pipeline already computed
1703 * its data checksum because that stage precedes gang assembly.
1704 * (Presently, nothing actually uses interior data checksums;
1705 * this is just good hygiene.)
1706 */
1707 if (gn != pio->io_gang_leader->io_gang_tree) {
1708 zio_checksum_compute(zio, BP_GET_CHECKSUM(bp),
1709 data, BP_GET_PSIZE(bp));
1710 }
1711 /*
1712 * If we are here to damage data for testing purposes,
1713 * leave the GBH alone so that we can detect the damage.
1714 */
1715 if (pio->io_gang_leader->io_flags & ZIO_FLAG_INDUCE_DAMAGE)
1716 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
1717 } else {
1718 zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
1719 data, BP_GET_PSIZE(bp), NULL, NULL, pio->io_priority,
1720 ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
1721 }
1722
1723 return (zio);
1724 }
1725
1726 /* ARGSUSED */
1727 zio_t *
1728 zio_free_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1729 {
1730 return (zio_free_sync(pio, pio->io_spa, pio->io_txg, bp,
1731 ZIO_GANG_CHILD_FLAGS(pio)));
1732 }
1733
1734 /* ARGSUSED */
1735 zio_t *
1736 zio_claim_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, void *data)
1737 {
1738 return (zio_claim(pio, pio->io_spa, pio->io_txg, bp,
1739 NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio)));
1740 }
1741
1742 static zio_gang_issue_func_t *zio_gang_issue_func[ZIO_TYPES] = {
1743 NULL,
1744 zio_read_gang,
1745 zio_rewrite_gang,
1746 zio_free_gang,
1747 zio_claim_gang,
1748 NULL
1749 };
1750
1751 static void zio_gang_tree_assemble_done(zio_t *zio);
1752
1753 static zio_gang_node_t *
1754 zio_gang_node_alloc(zio_gang_node_t **gnpp)
1755 {
1756 zio_gang_node_t *gn;
1757
1758 ASSERT(*gnpp == NULL);
1759
1760 gn = kmem_zalloc(sizeof (*gn), KM_SLEEP);
1761 gn->gn_gbh = zio_buf_alloc(SPA_GANGBLOCKSIZE);
1762 *gnpp = gn;
1763
1764 return (gn);
1765 }
1766
1767 static void
1768 zio_gang_node_free(zio_gang_node_t **gnpp)
1769 {
1770 zio_gang_node_t *gn = *gnpp;
1771 int g;
1772
1773 for (g = 0; g < SPA_GBH_NBLKPTRS; g++)
1774 ASSERT(gn->gn_child[g] == NULL);
1775
1776 zio_buf_free(gn->gn_gbh, SPA_GANGBLOCKSIZE);
1777 kmem_free(gn, sizeof (*gn));
1778 *gnpp = NULL;
1779 }
1780
1781 static void
1782 zio_gang_tree_free(zio_gang_node_t **gnpp)
1783 {
1784 zio_gang_node_t *gn = *gnpp;
1785 int g;
1786
1787 if (gn == NULL)
1788 return;
1789
1790 for (g = 0; g < SPA_GBH_NBLKPTRS; g++)
1791 zio_gang_tree_free(&gn->gn_child[g]);
1792
1793 zio_gang_node_free(gnpp);
1794 }
1795
1796 static void
1797 zio_gang_tree_assemble(zio_t *gio, blkptr_t *bp, zio_gang_node_t **gnpp)
1798 {
1799 zio_gang_node_t *gn = zio_gang_node_alloc(gnpp);
1800
1801 ASSERT(gio->io_gang_leader == gio);
1802 ASSERT(BP_IS_GANG(bp));
1803
1804 zio_nowait(zio_read(gio, gio->io_spa, bp, gn->gn_gbh,
1805 SPA_GANGBLOCKSIZE, zio_gang_tree_assemble_done, gn,
1806 gio->io_priority, ZIO_GANG_CHILD_FLAGS(gio), &gio->io_bookmark));
1807 }
1808
1809 static void
1810 zio_gang_tree_assemble_done(zio_t *zio)
1811 {
1812 zio_t *gio = zio->io_gang_leader;
1813 zio_gang_node_t *gn = zio->io_private;
1814 blkptr_t *bp = zio->io_bp;
1815 int g;
1816
1817 ASSERT(gio == zio_unique_parent(zio));
1818 ASSERT(zio->io_child_count == 0);
1819
1820 if (zio->io_error)
1821 return;
1822
1823 if (BP_SHOULD_BYTESWAP(bp))
1824 byteswap_uint64_array(zio->io_data, zio->io_size);
1825
1826 ASSERT(zio->io_data == gn->gn_gbh);
1827 ASSERT(zio->io_size == SPA_GANGBLOCKSIZE);
1828 ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
1829
1830 for (g = 0; g < SPA_GBH_NBLKPTRS; g++) {
1831 blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
1832 if (!BP_IS_GANG(gbp))
1833 continue;
1834 zio_gang_tree_assemble(gio, gbp, &gn->gn_child[g]);
1835 }
1836 }
1837
1838 static void
1839 zio_gang_tree_issue(zio_t *pio, zio_gang_node_t *gn, blkptr_t *bp, void *data)
1840 {
1841 zio_t *gio = pio->io_gang_leader;
1842 zio_t *zio;
1843 int g;
1844
1845 ASSERT(BP_IS_GANG(bp) == !!gn);
1846 ASSERT(BP_GET_CHECKSUM(bp) == BP_GET_CHECKSUM(gio->io_bp));
1847 ASSERT(BP_GET_LSIZE(bp) == BP_GET_PSIZE(bp) || gn == gio->io_gang_tree);
1848
1849 /*
1850 * If you're a gang header, your data is in gn->gn_gbh.
1851 * If you're a gang member, your data is in 'data' and gn == NULL.
1852 */
1853 zio = zio_gang_issue_func[gio->io_type](pio, bp, gn, data);
1854
1855 if (gn != NULL) {
1856 ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
1857
1858 for (g = 0; g < SPA_GBH_NBLKPTRS; g++) {
1859 blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
1860 if (BP_IS_HOLE(gbp))
1861 continue;
1862 zio_gang_tree_issue(zio, gn->gn_child[g], gbp, data);
1863 data = (char *)data + BP_GET_PSIZE(gbp);
1864 }
1865 }
1866
1867 if (gn == gio->io_gang_tree)
1868 ASSERT3P((char *)gio->io_data + gio->io_size, ==, data);
1869
1870 if (zio != pio)
1871 zio_nowait(zio);
1872 }
1873
1874 static int
1875 zio_gang_assemble(zio_t *zio)
1876 {
1877 blkptr_t *bp = zio->io_bp;
1878
1879 ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == NULL);
1880 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
1881
1882 zio->io_gang_leader = zio;
1883
1884 zio_gang_tree_assemble(zio, bp, &zio->io_gang_tree);
1885
1886 return (ZIO_PIPELINE_CONTINUE);
1887 }
1888
1889 static int
1890 zio_gang_issue(zio_t *zio)
1891 {
1892 blkptr_t *bp = zio->io_bp;
1893
1894 if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE))
1895 return (ZIO_PIPELINE_STOP);
1896
1897 ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == zio);
1898 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
1899
1900 if (zio->io_child_error[ZIO_CHILD_GANG] == 0)
1901 zio_gang_tree_issue(zio, zio->io_gang_tree, bp, zio->io_data);
1902 else
1903 zio_gang_tree_free(&zio->io_gang_tree);
1904
1905 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1906
1907 return (ZIO_PIPELINE_CONTINUE);
1908 }
1909
1910 static void
1911 zio_write_gang_member_ready(zio_t *zio)
1912 {
1913 zio_t *pio = zio_unique_parent(zio);
1914 dva_t *cdva = zio->io_bp->blk_dva;
1915 dva_t *pdva = pio->io_bp->blk_dva;
1916 uint64_t asize;
1917 int d;
1918 ASSERTV(zio_t *gio = zio->io_gang_leader);
1919
1920 if (BP_IS_HOLE(zio->io_bp))
1921 return;
1922
1923 ASSERT(BP_IS_HOLE(&zio->io_bp_orig));
1924
1925 ASSERT(zio->io_child_type == ZIO_CHILD_GANG);
1926 ASSERT3U(zio->io_prop.zp_copies, ==, gio->io_prop.zp_copies);
1927 ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(zio->io_bp));
1928 ASSERT3U(pio->io_prop.zp_copies, <=, BP_GET_NDVAS(pio->io_bp));
1929 ASSERT3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp));
1930
1931 mutex_enter(&pio->io_lock);
1932 for (d = 0; d < BP_GET_NDVAS(zio->io_bp); d++) {
1933 ASSERT(DVA_GET_GANG(&pdva[d]));
1934 asize = DVA_GET_ASIZE(&pdva[d]);
1935 asize += DVA_GET_ASIZE(&cdva[d]);
1936 DVA_SET_ASIZE(&pdva[d], asize);
1937 }
1938 mutex_exit(&pio->io_lock);
1939 }
1940
1941 static int
1942 zio_write_gang_block(zio_t *pio)
1943 {
1944 spa_t *spa = pio->io_spa;
1945 blkptr_t *bp = pio->io_bp;
1946 zio_t *gio = pio->io_gang_leader;
1947 zio_t *zio;
1948 zio_gang_node_t *gn, **gnpp;
1949 zio_gbh_phys_t *gbh;
1950 uint64_t txg = pio->io_txg;
1951 uint64_t resid = pio->io_size;
1952 uint64_t lsize;
1953 int copies = gio->io_prop.zp_copies;
1954 int gbh_copies = MIN(copies + 1, spa_max_replication(spa));
1955 zio_prop_t zp;
1956 int g, error;
1957
1958 error = metaslab_alloc(spa, spa_normal_class(spa), SPA_GANGBLOCKSIZE,
1959 bp, gbh_copies, txg, pio == gio ? NULL : gio->io_bp,
1960 METASLAB_HINTBP_FAVOR | METASLAB_GANG_HEADER);
1961 if (error) {
1962 pio->io_error = error;
1963 return (ZIO_PIPELINE_CONTINUE);
1964 }
1965
1966 if (pio == gio) {
1967 gnpp = &gio->io_gang_tree;
1968 } else {
1969 gnpp = pio->io_private;
1970 ASSERT(pio->io_ready == zio_write_gang_member_ready);
1971 }
1972
1973 gn = zio_gang_node_alloc(gnpp);
1974 gbh = gn->gn_gbh;
1975 bzero(gbh, SPA_GANGBLOCKSIZE);
1976
1977 /*
1978 * Create the gang header.
1979 */
1980 zio = zio_rewrite(pio, spa, txg, bp, gbh, SPA_GANGBLOCKSIZE, NULL, NULL,
1981 pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
1982
1983 /*
1984 * Create and nowait the gang children.
1985 */
1986 for (g = 0; resid != 0; resid -= lsize, g++) {
1987 lsize = P2ROUNDUP(resid / (SPA_GBH_NBLKPTRS - g),
1988 SPA_MINBLOCKSIZE);
1989 ASSERT(lsize >= SPA_MINBLOCKSIZE && lsize <= resid);
1990
1991 zp.zp_checksum = gio->io_prop.zp_checksum;
1992 zp.zp_compress = ZIO_COMPRESS_OFF;
1993 zp.zp_type = DMU_OT_NONE;
1994 zp.zp_level = 0;
1995 zp.zp_copies = gio->io_prop.zp_copies;
1996 zp.zp_dedup = B_FALSE;
1997 zp.zp_dedup_verify = B_FALSE;
1998 zp.zp_nopwrite = B_FALSE;
1999
2000 zio_nowait(zio_write(zio, spa, txg, &gbh->zg_blkptr[g],
2001 (char *)pio->io_data + (pio->io_size - resid), lsize, &zp,
2002 zio_write_gang_member_ready, NULL, NULL, &gn->gn_child[g],
2003 pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
2004 &pio->io_bookmark));
2005 }
2006
2007 /*
2008 * Set pio's pipeline to just wait for zio to finish.
2009 */
2010 pio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2011
2012 /*
2013 * We didn't allocate this bp, so make sure it doesn't get unmarked.
2014 */
2015 pio->io_flags &= ~ZIO_FLAG_FASTWRITE;
2016
2017 zio_nowait(zio);
2018
2019 return (ZIO_PIPELINE_CONTINUE);
2020 }
2021
2022 /*
2023 * The zio_nop_write stage in the pipeline determines if allocating
2024 * a new bp is necessary. By leveraging a cryptographically secure checksum,
2025 * such as SHA256, we can compare the checksums of the new data and the old
2026 * to determine if allocating a new block is required. The nopwrite
2027 * feature can handle writes in either syncing or open context (i.e. zil
2028 * writes) and as a result is mutually exclusive with dedup.
2029 */
2030 static int
2031 zio_nop_write(zio_t *zio)
2032 {
2033 blkptr_t *bp = zio->io_bp;
2034 blkptr_t *bp_orig = &zio->io_bp_orig;
2035 zio_prop_t *zp = &zio->io_prop;
2036
2037 ASSERT(BP_GET_LEVEL(bp) == 0);
2038 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
2039 ASSERT(zp->zp_nopwrite);
2040 ASSERT(!zp->zp_dedup);
2041 ASSERT(zio->io_bp_override == NULL);
2042 ASSERT(IO_IS_ALLOCATING(zio));
2043
2044 /*
2045 * Check to see if the original bp and the new bp have matching
2046 * characteristics (i.e. same checksum, compression algorithms, etc).
2047 * If they don't then just continue with the pipeline which will
2048 * allocate a new bp.
2049 */
2050 if (BP_IS_HOLE(bp_orig) ||
2051 !zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_dedup ||
2052 BP_GET_CHECKSUM(bp) != BP_GET_CHECKSUM(bp_orig) ||
2053 BP_GET_COMPRESS(bp) != BP_GET_COMPRESS(bp_orig) ||
2054 BP_GET_DEDUP(bp) != BP_GET_DEDUP(bp_orig) ||
2055 zp->zp_copies != BP_GET_NDVAS(bp_orig))
2056 return (ZIO_PIPELINE_CONTINUE);
2057
2058 /*
2059 * If the checksums match then reset the pipeline so that we
2060 * avoid allocating a new bp and issuing any I/O.
2061 */
2062 if (ZIO_CHECKSUM_EQUAL(bp->blk_cksum, bp_orig->blk_cksum)) {
2063 ASSERT(zio_checksum_table[zp->zp_checksum].ci_dedup);
2064 ASSERT3U(BP_GET_PSIZE(bp), ==, BP_GET_PSIZE(bp_orig));
2065 ASSERT3U(BP_GET_LSIZE(bp), ==, BP_GET_LSIZE(bp_orig));
2066 ASSERT(zp->zp_compress != ZIO_COMPRESS_OFF);
2067 ASSERT(bcmp(&bp->blk_prop, &bp_orig->blk_prop,
2068 sizeof (uint64_t)) == 0);
2069
2070 *bp = *bp_orig;
2071 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2072 zio->io_flags |= ZIO_FLAG_NOPWRITE;
2073 }
2074
2075 return (ZIO_PIPELINE_CONTINUE);
2076 }
2077
2078 /*
2079 * ==========================================================================
2080 * Dedup
2081 * ==========================================================================
2082 */
2083 static void
2084 zio_ddt_child_read_done(zio_t *zio)
2085 {
2086 blkptr_t *bp = zio->io_bp;
2087 ddt_entry_t *dde = zio->io_private;
2088 ddt_phys_t *ddp;
2089 zio_t *pio = zio_unique_parent(zio);
2090
2091 mutex_enter(&pio->io_lock);
2092 ddp = ddt_phys_select(dde, bp);
2093 if (zio->io_error == 0)
2094 ddt_phys_clear(ddp); /* this ddp doesn't need repair */
2095 if (zio->io_error == 0 && dde->dde_repair_data == NULL)
2096 dde->dde_repair_data = zio->io_data;
2097 else
2098 zio_buf_free(zio->io_data, zio->io_size);
2099 mutex_exit(&pio->io_lock);
2100 }
2101
2102 static int
2103 zio_ddt_read_start(zio_t *zio)
2104 {
2105 blkptr_t *bp = zio->io_bp;
2106 int p;
2107
2108 ASSERT(BP_GET_DEDUP(bp));
2109 ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
2110 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2111
2112 if (zio->io_child_error[ZIO_CHILD_DDT]) {
2113 ddt_t *ddt = ddt_select(zio->io_spa, bp);
2114 ddt_entry_t *dde = ddt_repair_start(ddt, bp);
2115 ddt_phys_t *ddp = dde->dde_phys;
2116 ddt_phys_t *ddp_self = ddt_phys_select(dde, bp);
2117 blkptr_t blk;
2118
2119 ASSERT(zio->io_vsd == NULL);
2120 zio->io_vsd = dde;
2121
2122 if (ddp_self == NULL)
2123 return (ZIO_PIPELINE_CONTINUE);
2124
2125 for (p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
2126 if (ddp->ddp_phys_birth == 0 || ddp == ddp_self)
2127 continue;
2128 ddt_bp_create(ddt->ddt_checksum, &dde->dde_key, ddp,
2129 &blk);
2130 zio_nowait(zio_read(zio, zio->io_spa, &blk,
2131 zio_buf_alloc(zio->io_size), zio->io_size,
2132 zio_ddt_child_read_done, dde, zio->io_priority,
2133 ZIO_DDT_CHILD_FLAGS(zio) | ZIO_FLAG_DONT_PROPAGATE,
2134 &zio->io_bookmark));
2135 }
2136 return (ZIO_PIPELINE_CONTINUE);
2137 }
2138
2139 zio_nowait(zio_read(zio, zio->io_spa, bp,
2140 zio->io_data, zio->io_size, NULL, NULL, zio->io_priority,
2141 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark));
2142
2143 return (ZIO_PIPELINE_CONTINUE);
2144 }
2145
2146 static int
2147 zio_ddt_read_done(zio_t *zio)
2148 {
2149 blkptr_t *bp = zio->io_bp;
2150
2151 if (zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_DONE))
2152 return (ZIO_PIPELINE_STOP);
2153
2154 ASSERT(BP_GET_DEDUP(bp));
2155 ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
2156 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2157
2158 if (zio->io_child_error[ZIO_CHILD_DDT]) {
2159 ddt_t *ddt = ddt_select(zio->io_spa, bp);
2160 ddt_entry_t *dde = zio->io_vsd;
2161 if (ddt == NULL) {
2162 ASSERT(spa_load_state(zio->io_spa) != SPA_LOAD_NONE);
2163 return (ZIO_PIPELINE_CONTINUE);
2164 }
2165 if (dde == NULL) {
2166 zio->io_stage = ZIO_STAGE_DDT_READ_START >> 1;
2167 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
2168 return (ZIO_PIPELINE_STOP);
2169 }
2170 if (dde->dde_repair_data != NULL) {
2171 bcopy(dde->dde_repair_data, zio->io_data, zio->io_size);
2172 zio->io_child_error[ZIO_CHILD_DDT] = 0;
2173 }
2174 ddt_repair_done(ddt, dde);
2175 zio->io_vsd = NULL;
2176 }
2177
2178 ASSERT(zio->io_vsd == NULL);
2179
2180 return (ZIO_PIPELINE_CONTINUE);
2181 }
2182
2183 static boolean_t
2184 zio_ddt_collision(zio_t *zio, ddt_t *ddt, ddt_entry_t *dde)
2185 {
2186 spa_t *spa = zio->io_spa;
2187 int p;
2188
2189 /*
2190 * Note: we compare the original data, not the transformed data,
2191 * because when zio->io_bp is an override bp, we will not have
2192 * pushed the I/O transforms. That's an important optimization
2193 * because otherwise we'd compress/encrypt all dmu_sync() data twice.
2194 */
2195 for (p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
2196 zio_t *lio = dde->dde_lead_zio[p];
2197
2198 if (lio != NULL) {
2199 return (lio->io_orig_size != zio->io_orig_size ||
2200 bcmp(zio->io_orig_data, lio->io_orig_data,
2201 zio->io_orig_size) != 0);
2202 }
2203 }
2204
2205 for (p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
2206 ddt_phys_t *ddp = &dde->dde_phys[p];
2207
2208 if (ddp->ddp_phys_birth != 0) {
2209 arc_buf_t *abuf = NULL;
2210 uint32_t aflags = ARC_WAIT;
2211 blkptr_t blk = *zio->io_bp;
2212 int error;
2213
2214 ddt_bp_fill(ddp, &blk, ddp->ddp_phys_birth);
2215
2216 ddt_exit(ddt);
2217
2218 error = arc_read(NULL, spa, &blk,
2219 arc_getbuf_func, &abuf, ZIO_PRIORITY_SYNC_READ,
2220 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
2221 &aflags, &zio->io_bookmark);
2222
2223 if (error == 0) {
2224 if (arc_buf_size(abuf) != zio->io_orig_size ||
2225 bcmp(abuf->b_data, zio->io_orig_data,
2226 zio->io_orig_size) != 0)
2227 error = SET_ERROR(EEXIST);
2228 VERIFY(arc_buf_remove_ref(abuf, &abuf));
2229 }
2230
2231 ddt_enter(ddt);
2232 return (error != 0);
2233 }
2234 }
2235
2236 return (B_FALSE);
2237 }
2238
2239 static void
2240 zio_ddt_child_write_ready(zio_t *zio)
2241 {
2242 int p = zio->io_prop.zp_copies;
2243 ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
2244 ddt_entry_t *dde = zio->io_private;
2245 ddt_phys_t *ddp = &dde->dde_phys[p];
2246 zio_t *pio;
2247
2248 if (zio->io_error)
2249 return;
2250
2251 ddt_enter(ddt);
2252
2253 ASSERT(dde->dde_lead_zio[p] == zio);
2254
2255 ddt_phys_fill(ddp, zio->io_bp);
2256
2257 while ((pio = zio_walk_parents(zio)) != NULL)
2258 ddt_bp_fill(ddp, pio->io_bp, zio->io_txg);
2259
2260 ddt_exit(ddt);
2261 }
2262
2263 static void
2264 zio_ddt_child_write_done(zio_t *zio)
2265 {
2266 int p = zio->io_prop.zp_copies;
2267 ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
2268 ddt_entry_t *dde = zio->io_private;
2269 ddt_phys_t *ddp = &dde->dde_phys[p];
2270
2271 ddt_enter(ddt);
2272
2273 ASSERT(ddp->ddp_refcnt == 0);
2274 ASSERT(dde->dde_lead_zio[p] == zio);
2275 dde->dde_lead_zio[p] = NULL;
2276
2277 if (zio->io_error == 0) {
2278 while (zio_walk_parents(zio) != NULL)
2279 ddt_phys_addref(ddp);
2280 } else {
2281 ddt_phys_clear(ddp);
2282 }
2283
2284 ddt_exit(ddt);
2285 }
2286
2287 static void
2288 zio_ddt_ditto_write_done(zio_t *zio)
2289 {
2290 int p = DDT_PHYS_DITTO;
2291 blkptr_t *bp = zio->io_bp;
2292 ddt_t *ddt = ddt_select(zio->io_spa, bp);
2293 ddt_entry_t *dde = zio->io_private;
2294 ddt_phys_t *ddp = &dde->dde_phys[p];
2295 ddt_key_t *ddk = &dde->dde_key;
2296 ASSERTV(zio_prop_t *zp = &zio->io_prop);
2297
2298 ddt_enter(ddt);
2299
2300 ASSERT(ddp->ddp_refcnt == 0);
2301 ASSERT(dde->dde_lead_zio[p] == zio);
2302 dde->dde_lead_zio[p] = NULL;
2303
2304 if (zio->io_error == 0) {
2305 ASSERT(ZIO_CHECKSUM_EQUAL(bp->blk_cksum, ddk->ddk_cksum));
2306 ASSERT(zp->zp_copies < SPA_DVAS_PER_BP);
2307 ASSERT(zp->zp_copies == BP_GET_NDVAS(bp) - BP_IS_GANG(bp));
2308 if (ddp->ddp_phys_birth != 0)
2309 ddt_phys_free(ddt, ddk, ddp, zio->io_txg);
2310 ddt_phys_fill(ddp, bp);
2311 }
2312
2313 ddt_exit(ddt);
2314 }
2315
2316 static int
2317 zio_ddt_write(zio_t *zio)
2318 {
2319 spa_t *spa = zio->io_spa;
2320 blkptr_t *bp = zio->io_bp;
2321 uint64_t txg = zio->io_txg;
2322 zio_prop_t *zp = &zio->io_prop;
2323 int p = zp->zp_copies;
2324 int ditto_copies;
2325 zio_t *cio = NULL;
2326 zio_t *dio = NULL;
2327 ddt_t *ddt = ddt_select(spa, bp);
2328 ddt_entry_t *dde;
2329 ddt_phys_t *ddp;
2330
2331 ASSERT(BP_GET_DEDUP(bp));
2332 ASSERT(BP_GET_CHECKSUM(bp) == zp->zp_checksum);
2333 ASSERT(BP_IS_HOLE(bp) || zio->io_bp_override);
2334
2335 ddt_enter(ddt);
2336 dde = ddt_lookup(ddt, bp, B_TRUE);
2337 ddp = &dde->dde_phys[p];
2338
2339 if (zp->zp_dedup_verify && zio_ddt_collision(zio, ddt, dde)) {
2340 /*
2341 * If we're using a weak checksum, upgrade to a strong checksum
2342 * and try again. If we're already using a strong checksum,
2343 * we can't resolve it, so just convert to an ordinary write.
2344 * (And automatically e-mail a paper to Nature?)
2345 */
2346 if (!zio_checksum_table[zp->zp_checksum].ci_dedup) {
2347 zp->zp_checksum = spa_dedup_checksum(spa);
2348 zio_pop_transforms(zio);
2349 zio->io_stage = ZIO_STAGE_OPEN;
2350 BP_ZERO(bp);
2351 } else {
2352 zp->zp_dedup = B_FALSE;
2353 }
2354 zio->io_pipeline = ZIO_WRITE_PIPELINE;
2355 ddt_exit(ddt);
2356 return (ZIO_PIPELINE_CONTINUE);
2357 }
2358
2359 ditto_copies = ddt_ditto_copies_needed(ddt, dde, ddp);
2360 ASSERT(ditto_copies < SPA_DVAS_PER_BP);
2361
2362 if (ditto_copies > ddt_ditto_copies_present(dde) &&
2363 dde->dde_lead_zio[DDT_PHYS_DITTO] == NULL) {
2364 zio_prop_t czp = *zp;
2365
2366 czp.zp_copies = ditto_copies;
2367
2368 /*
2369 * If we arrived here with an override bp, we won't have run
2370 * the transform stack, so we won't have the data we need to
2371 * generate a child i/o. So, toss the override bp and restart.
2372 * This is safe, because using the override bp is just an
2373 * optimization; and it's rare, so the cost doesn't matter.
2374 */
2375 if (zio->io_bp_override) {
2376 zio_pop_transforms(zio);
2377 zio->io_stage = ZIO_STAGE_OPEN;
2378 zio->io_pipeline = ZIO_WRITE_PIPELINE;
2379 zio->io_bp_override = NULL;
2380 BP_ZERO(bp);
2381 ddt_exit(ddt);
2382 return (ZIO_PIPELINE_CONTINUE);
2383 }
2384
2385 dio = zio_write(zio, spa, txg, bp, zio->io_orig_data,
2386 zio->io_orig_size, &czp, NULL, NULL,
2387 zio_ddt_ditto_write_done, dde, zio->io_priority,
2388 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
2389
2390 zio_push_transform(dio, zio->io_data, zio->io_size, 0, NULL);
2391 dde->dde_lead_zio[DDT_PHYS_DITTO] = dio;
2392 }
2393
2394 if (ddp->ddp_phys_birth != 0 || dde->dde_lead_zio[p] != NULL) {
2395 if (ddp->ddp_phys_birth != 0)
2396 ddt_bp_fill(ddp, bp, txg);
2397 if (dde->dde_lead_zio[p] != NULL)
2398 zio_add_child(zio, dde->dde_lead_zio[p]);
2399 else
2400 ddt_phys_addref(ddp);
2401 } else if (zio->io_bp_override) {
2402 ASSERT(bp->blk_birth == txg);
2403 ASSERT(BP_EQUAL(bp, zio->io_bp_override));
2404 ddt_phys_fill(ddp, bp);
2405 ddt_phys_addref(ddp);
2406 } else {
2407 cio = zio_write(zio, spa, txg, bp, zio->io_orig_data,
2408 zio->io_orig_size, zp, zio_ddt_child_write_ready, NULL,
2409 zio_ddt_child_write_done, dde, zio->io_priority,
2410 ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
2411
2412 zio_push_transform(cio, zio->io_data, zio->io_size, 0, NULL);
2413 dde->dde_lead_zio[p] = cio;
2414 }
2415
2416 ddt_exit(ddt);
2417
2418 if (cio)
2419 zio_nowait(cio);
2420 if (dio)
2421 zio_nowait(dio);
2422
2423 return (ZIO_PIPELINE_CONTINUE);
2424 }
2425
2426 ddt_entry_t *freedde; /* for debugging */
2427
2428 static int
2429 zio_ddt_free(zio_t *zio)
2430 {
2431 spa_t *spa = zio->io_spa;
2432 blkptr_t *bp = zio->io_bp;
2433 ddt_t *ddt = ddt_select(spa, bp);
2434 ddt_entry_t *dde;
2435 ddt_phys_t *ddp;
2436
2437 ASSERT(BP_GET_DEDUP(bp));
2438 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2439
2440 ddt_enter(ddt);
2441 freedde = dde = ddt_lookup(ddt, bp, B_TRUE);
2442 if (dde) {
2443 ddp = ddt_phys_select(dde, bp);
2444 if (ddp)
2445 ddt_phys_decref(ddp);
2446 }
2447 ddt_exit(ddt);
2448
2449 return (ZIO_PIPELINE_CONTINUE);
2450 }
2451
2452 /*
2453 * ==========================================================================
2454 * Allocate and free blocks
2455 * ==========================================================================
2456 */
2457 static int
2458 zio_dva_allocate(zio_t *zio)
2459 {
2460 spa_t *spa = zio->io_spa;
2461 metaslab_class_t *mc = spa_normal_class(spa);
2462 blkptr_t *bp = zio->io_bp;
2463 int error;
2464 int flags = 0;
2465
2466 if (zio->io_gang_leader == NULL) {
2467 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2468 zio->io_gang_leader = zio;
2469 }
2470
2471 ASSERT(BP_IS_HOLE(bp));
2472 ASSERT0(BP_GET_NDVAS(bp));
2473 ASSERT3U(zio->io_prop.zp_copies, >, 0);
2474 ASSERT3U(zio->io_prop.zp_copies, <=, spa_max_replication(spa));
2475 ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
2476
2477 /*
2478 * The dump device does not support gang blocks so allocation on
2479 * behalf of the dump device (i.e. ZIO_FLAG_NODATA) must avoid
2480 * the "fast" gang feature.
2481 */
2482 flags |= (zio->io_flags & ZIO_FLAG_NODATA) ? METASLAB_GANG_AVOID : 0;
2483 flags |= (zio->io_flags & ZIO_FLAG_GANG_CHILD) ?
2484 METASLAB_GANG_CHILD : 0;
2485 flags |= (zio->io_flags & ZIO_FLAG_FASTWRITE) ? METASLAB_FASTWRITE : 0;
2486 error = metaslab_alloc(spa, mc, zio->io_size, bp,
2487 zio->io_prop.zp_copies, zio->io_txg, NULL, flags);
2488
2489 if (error) {
2490 spa_dbgmsg(spa, "%s: metaslab allocation failure: zio %p, "
2491 "size %llu, error %d", spa_name(spa), zio, zio->io_size,
2492 error);
2493 if (error == ENOSPC && zio->io_size > SPA_MINBLOCKSIZE)
2494 return (zio_write_gang_block(zio));
2495 zio->io_error = error;
2496 }
2497
2498 return (ZIO_PIPELINE_CONTINUE);
2499 }
2500
2501 static int
2502 zio_dva_free(zio_t *zio)
2503 {
2504 metaslab_free(zio->io_spa, zio->io_bp, zio->io_txg, B_FALSE);
2505
2506 return (ZIO_PIPELINE_CONTINUE);
2507 }
2508
2509 static int
2510 zio_dva_claim(zio_t *zio)
2511 {
2512 int error;
2513
2514 error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg);
2515 if (error)
2516 zio->io_error = error;
2517
2518 return (ZIO_PIPELINE_CONTINUE);
2519 }
2520
2521 /*
2522 * Undo an allocation. This is used by zio_done() when an I/O fails
2523 * and we want to give back the block we just allocated.
2524 * This handles both normal blocks and gang blocks.
2525 */
2526 static void
2527 zio_dva_unallocate(zio_t *zio, zio_gang_node_t *gn, blkptr_t *bp)
2528 {
2529 int g;
2530
2531 ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp));
2532 ASSERT(zio->io_bp_override == NULL);
2533
2534 if (!BP_IS_HOLE(bp))
2535 metaslab_free(zio->io_spa, bp, bp->blk_birth, B_TRUE);
2536
2537 if (gn != NULL) {
2538 for (g = 0; g < SPA_GBH_NBLKPTRS; g++) {
2539 zio_dva_unallocate(zio, gn->gn_child[g],
2540 &gn->gn_gbh->zg_blkptr[g]);
2541 }
2542 }
2543 }
2544
2545 /*
2546 * Try to allocate an intent log block. Return 0 on success, errno on failure.
2547 */
2548 int
2549 zio_alloc_zil(spa_t *spa, uint64_t txg, blkptr_t *new_bp, uint64_t size,
2550 boolean_t use_slog)
2551 {
2552 int error = 1;
2553
2554 ASSERT(txg > spa_syncing_txg(spa));
2555
2556 /*
2557 * ZIL blocks are always contiguous (i.e. not gang blocks) so we
2558 * set the METASLAB_GANG_AVOID flag so that they don't "fast gang"
2559 * when allocating them.
2560 */
2561 if (use_slog) {
2562 error = metaslab_alloc(spa, spa_log_class(spa), size,
2563 new_bp, 1, txg, NULL,
2564 METASLAB_FASTWRITE | METASLAB_GANG_AVOID);
2565 }
2566
2567 if (error) {
2568 error = metaslab_alloc(spa, spa_normal_class(spa), size,
2569 new_bp, 1, txg, NULL,
2570 METASLAB_FASTWRITE);
2571 }
2572
2573 if (error == 0) {
2574 BP_SET_LSIZE(new_bp, size);
2575 BP_SET_PSIZE(new_bp, size);
2576 BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF);
2577 BP_SET_CHECKSUM(new_bp,
2578 spa_version(spa) >= SPA_VERSION_SLIM_ZIL
2579 ? ZIO_CHECKSUM_ZILOG2 : ZIO_CHECKSUM_ZILOG);
2580 BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
2581 BP_SET_LEVEL(new_bp, 0);
2582 BP_SET_DEDUP(new_bp, 0);
2583 BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER);
2584 }
2585
2586 return (error);
2587 }
2588
2589 /*
2590 * Free an intent log block.
2591 */
2592 void
2593 zio_free_zil(spa_t *spa, uint64_t txg, blkptr_t *bp)
2594 {
2595 ASSERT(BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG);
2596 ASSERT(!BP_IS_GANG(bp));
2597
2598 zio_free(spa, txg, bp);
2599 }
2600
2601 /*
2602 * ==========================================================================
2603 * Read and write to physical devices
2604 * ==========================================================================
2605 */
2606 static int
2607 zio_vdev_io_start(zio_t *zio)
2608 {
2609 vdev_t *vd = zio->io_vd;
2610 uint64_t align;
2611 spa_t *spa = zio->io_spa;
2612
2613 ASSERT(zio->io_error == 0);
2614 ASSERT(zio->io_child_error[ZIO_CHILD_VDEV] == 0);
2615
2616 if (vd == NULL) {
2617 if (!(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
2618 spa_config_enter(spa, SCL_ZIO, zio, RW_READER);
2619
2620 /*
2621 * The mirror_ops handle multiple DVAs in a single BP.
2622 */
2623 return (vdev_mirror_ops.vdev_op_io_start(zio));
2624 }
2625
2626 /*
2627 * We keep track of time-sensitive I/Os so that the scan thread
2628 * can quickly react to certain workloads. In particular, we care
2629 * about non-scrubbing, top-level reads and writes with the following
2630 * characteristics:
2631 * - synchronous writes of user data to non-slog devices
2632 * - any reads of user data
2633 * When these conditions are met, adjust the timestamp of spa_last_io
2634 * which allows the scan thread to adjust its workload accordingly.
2635 */
2636 if (!(zio->io_flags & ZIO_FLAG_SCAN_THREAD) && zio->io_bp != NULL &&
2637 vd == vd->vdev_top && !vd->vdev_islog &&
2638 zio->io_bookmark.zb_objset != DMU_META_OBJSET &&
2639 zio->io_txg != spa_syncing_txg(spa)) {
2640 uint64_t old = spa->spa_last_io;
2641 uint64_t new = ddi_get_lbolt64();
2642 if (old != new)
2643 (void) atomic_cas_64(&spa->spa_last_io, old, new);
2644 }
2645
2646 align = 1ULL << vd->vdev_top->vdev_ashift;
2647
2648 if (!(zio->io_flags & ZIO_FLAG_PHYSICAL) &&
2649 P2PHASE(zio->io_size, align) != 0) {
2650 /* Transform logical writes to be a full physical block size. */
2651 uint64_t asize = P2ROUNDUP(zio->io_size, align);
2652 char *abuf = zio_buf_alloc(asize);
2653 ASSERT(vd == vd->vdev_top);
2654 if (zio->io_type == ZIO_TYPE_WRITE) {
2655 bcopy(zio->io_data, abuf, zio->io_size);
2656 bzero(abuf + zio->io_size, asize - zio->io_size);
2657 }
2658 zio_push_transform(zio, abuf, asize, asize, zio_subblock);
2659 }
2660
2661 /*
2662 * If this is not a physical io, make sure that it is properly aligned
2663 * before proceeding.
2664 */
2665 if (!(zio->io_flags & ZIO_FLAG_PHYSICAL)) {
2666 ASSERT0(P2PHASE(zio->io_offset, align));
2667 ASSERT0(P2PHASE(zio->io_size, align));
2668 } else {
2669 /*
2670 * For physical writes, we allow 512b aligned writes and assume
2671 * the device will perform a read-modify-write as necessary.
2672 */
2673 ASSERT0(P2PHASE(zio->io_offset, SPA_MINBLOCKSIZE));
2674 ASSERT0(P2PHASE(zio->io_size, SPA_MINBLOCKSIZE));
2675 }
2676
2677 VERIFY(zio->io_type != ZIO_TYPE_WRITE || spa_writeable(spa));
2678
2679 /*
2680 * If this is a repair I/O, and there's no self-healing involved --
2681 * that is, we're just resilvering what we expect to resilver --
2682 * then don't do the I/O unless zio's txg is actually in vd's DTL.
2683 * This prevents spurious resilvering with nested replication.
2684 * For example, given a mirror of mirrors, (A+B)+(C+D), if only
2685 * A is out of date, we'll read from C+D, then use the data to
2686 * resilver A+B -- but we don't actually want to resilver B, just A.
2687 * The top-level mirror has no way to know this, so instead we just
2688 * discard unnecessary repairs as we work our way down the vdev tree.
2689 * The same logic applies to any form of nested replication:
2690 * ditto + mirror, RAID-Z + replacing, etc. This covers them all.
2691 */
2692 if ((zio->io_flags & ZIO_FLAG_IO_REPAIR) &&
2693 !(zio->io_flags & ZIO_FLAG_SELF_HEAL) &&
2694 zio->io_txg != 0 && /* not a delegated i/o */
2695 !vdev_dtl_contains(vd, DTL_PARTIAL, zio->io_txg, 1)) {
2696 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
2697 zio_vdev_io_bypass(zio);
2698 return (ZIO_PIPELINE_CONTINUE);
2699 }
2700
2701 if (vd->vdev_ops->vdev_op_leaf &&
2702 (zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE)) {
2703
2704 if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio))
2705 return (ZIO_PIPELINE_CONTINUE);
2706
2707 if ((zio = vdev_queue_io(zio)) == NULL)
2708 return (ZIO_PIPELINE_STOP);
2709
2710 if (!vdev_accessible(vd, zio)) {
2711 zio->io_error = SET_ERROR(ENXIO);
2712 zio_interrupt(zio);
2713 return (ZIO_PIPELINE_STOP);
2714 }
2715 }
2716
2717 return (vd->vdev_ops->vdev_op_io_start(zio));
2718 }
2719
2720 static int
2721 zio_vdev_io_done(zio_t *zio)
2722 {
2723 vdev_t *vd = zio->io_vd;
2724 vdev_ops_t *ops = vd ? vd->vdev_ops : &vdev_mirror_ops;
2725 boolean_t unexpected_error = B_FALSE;
2726
2727 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE))
2728 return (ZIO_PIPELINE_STOP);
2729
2730 ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
2731
2732 if (vd != NULL && vd->vdev_ops->vdev_op_leaf) {
2733
2734 vdev_queue_io_done(zio);
2735
2736 if (zio->io_type == ZIO_TYPE_WRITE)
2737 vdev_cache_write(zio);
2738
2739 if (zio_injection_enabled && zio->io_error == 0)
2740 zio->io_error = zio_handle_device_injection(vd,
2741 zio, EIO);
2742
2743 if (zio_injection_enabled && zio->io_error == 0)
2744 zio->io_error = zio_handle_label_injection(zio, EIO);
2745
2746 if (zio->io_error) {
2747 if (!vdev_accessible(vd, zio)) {
2748 zio->io_error = SET_ERROR(ENXIO);
2749 } else {
2750 unexpected_error = B_TRUE;
2751 }
2752 }
2753 }
2754
2755 ops->vdev_op_io_done(zio);
2756
2757 if (unexpected_error)
2758 VERIFY(vdev_probe(vd, zio) == NULL);
2759
2760 return (ZIO_PIPELINE_CONTINUE);
2761 }
2762
2763 /*
2764 * For non-raidz ZIOs, we can just copy aside the bad data read from the
2765 * disk, and use that to finish the checksum ereport later.
2766 */
2767 static void
2768 zio_vsd_default_cksum_finish(zio_cksum_report_t *zcr,
2769 const void *good_buf)
2770 {
2771 /* no processing needed */
2772 zfs_ereport_finish_checksum(zcr, good_buf, zcr->zcr_cbdata, B_FALSE);
2773 }
2774
2775 /*ARGSUSED*/
2776 void
2777 zio_vsd_default_cksum_report(zio_t *zio, zio_cksum_report_t *zcr, void *ignored)
2778 {
2779 void *buf = zio_buf_alloc(zio->io_size);
2780
2781 bcopy(zio->io_data, buf, zio->io_size);
2782
2783 zcr->zcr_cbinfo = zio->io_size;
2784 zcr->zcr_cbdata = buf;
2785 zcr->zcr_finish = zio_vsd_default_cksum_finish;
2786 zcr->zcr_free = zio_buf_free;
2787 }
2788
2789 static int
2790 zio_vdev_io_assess(zio_t *zio)
2791 {
2792 vdev_t *vd = zio->io_vd;
2793
2794 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE))
2795 return (ZIO_PIPELINE_STOP);
2796
2797 if (vd == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
2798 spa_config_exit(zio->io_spa, SCL_ZIO, zio);
2799
2800 if (zio->io_vsd != NULL) {
2801 zio->io_vsd_ops->vsd_free(zio);
2802 zio->io_vsd = NULL;
2803 }
2804
2805 if (zio_injection_enabled && zio->io_error == 0)
2806 zio->io_error = zio_handle_fault_injection(zio, EIO);
2807
2808 /*
2809 * If the I/O failed, determine whether we should attempt to retry it.
2810 *
2811 * On retry, we cut in line in the issue queue, since we don't want
2812 * compression/checksumming/etc. work to prevent our (cheap) IO reissue.
2813 */
2814 if (zio->io_error && vd == NULL &&
2815 !(zio->io_flags & (ZIO_FLAG_DONT_RETRY | ZIO_FLAG_IO_RETRY))) {
2816 ASSERT(!(zio->io_flags & ZIO_FLAG_DONT_QUEUE)); /* not a leaf */
2817 ASSERT(!(zio->io_flags & ZIO_FLAG_IO_BYPASS)); /* not a leaf */
2818 zio->io_error = 0;
2819 zio->io_flags |= ZIO_FLAG_IO_RETRY |
2820 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE;
2821 zio->io_stage = ZIO_STAGE_VDEV_IO_START >> 1;
2822 zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE,
2823 zio_requeue_io_start_cut_in_line);
2824 return (ZIO_PIPELINE_STOP);
2825 }
2826
2827 /*
2828 * If we got an error on a leaf device, convert it to ENXIO
2829 * if the device is not accessible at all.
2830 */
2831 if (zio->io_error && vd != NULL && vd->vdev_ops->vdev_op_leaf &&
2832 !vdev_accessible(vd, zio))
2833 zio->io_error = SET_ERROR(ENXIO);
2834
2835 /*
2836 * If we can't write to an interior vdev (mirror or RAID-Z),
2837 * set vdev_cant_write so that we stop trying to allocate from it.
2838 */
2839 if (zio->io_error == ENXIO && zio->io_type == ZIO_TYPE_WRITE &&
2840 vd != NULL && !vd->vdev_ops->vdev_op_leaf) {
2841 vd->vdev_cant_write = B_TRUE;
2842 }
2843
2844 if (zio->io_error)
2845 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2846
2847 if (vd != NULL && vd->vdev_ops->vdev_op_leaf &&
2848 zio->io_physdone != NULL) {
2849 ASSERT(!(zio->io_flags & ZIO_FLAG_DELEGATED));
2850 ASSERT(zio->io_child_type == ZIO_CHILD_VDEV);
2851 zio->io_physdone(zio->io_logical);
2852 }
2853
2854 return (ZIO_PIPELINE_CONTINUE);
2855 }
2856
2857 void
2858 zio_vdev_io_reissue(zio_t *zio)
2859 {
2860 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
2861 ASSERT(zio->io_error == 0);
2862
2863 zio->io_stage >>= 1;
2864 }
2865
2866 void
2867 zio_vdev_io_redone(zio_t *zio)
2868 {
2869 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE);
2870
2871 zio->io_stage >>= 1;
2872 }
2873
2874 void
2875 zio_vdev_io_bypass(zio_t *zio)
2876 {
2877 ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
2878 ASSERT(zio->io_error == 0);
2879
2880 zio->io_flags |= ZIO_FLAG_IO_BYPASS;
2881 zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS >> 1;
2882 }
2883
2884 /*
2885 * ==========================================================================
2886 * Generate and verify checksums
2887 * ==========================================================================
2888 */
2889 static int
2890 zio_checksum_generate(zio_t *zio)
2891 {
2892 blkptr_t *bp = zio->io_bp;
2893 enum zio_checksum checksum;
2894
2895 if (bp == NULL) {
2896 /*
2897 * This is zio_write_phys().
2898 * We're either generating a label checksum, or none at all.
2899 */
2900 checksum = zio->io_prop.zp_checksum;
2901
2902 if (checksum == ZIO_CHECKSUM_OFF)
2903 return (ZIO_PIPELINE_CONTINUE);
2904
2905 ASSERT(checksum == ZIO_CHECKSUM_LABEL);
2906 } else {
2907 if (BP_IS_GANG(bp) && zio->io_child_type == ZIO_CHILD_GANG) {
2908 ASSERT(!IO_IS_ALLOCATING(zio));
2909 checksum = ZIO_CHECKSUM_GANG_HEADER;
2910 } else {
2911 checksum = BP_GET_CHECKSUM(bp);
2912 }
2913 }
2914
2915 zio_checksum_compute(zio, checksum, zio->io_data, zio->io_size);
2916
2917 return (ZIO_PIPELINE_CONTINUE);
2918 }
2919
2920 static int
2921 zio_checksum_verify(zio_t *zio)
2922 {
2923 zio_bad_cksum_t info;
2924 blkptr_t *bp = zio->io_bp;
2925 int error;
2926
2927 ASSERT(zio->io_vd != NULL);
2928
2929 if (bp == NULL) {
2930 /*
2931 * This is zio_read_phys().
2932 * We're either verifying a label checksum, or nothing at all.
2933 */
2934 if (zio->io_prop.zp_checksum == ZIO_CHECKSUM_OFF)
2935 return (ZIO_PIPELINE_CONTINUE);
2936
2937 ASSERT(zio->io_prop.zp_checksum == ZIO_CHECKSUM_LABEL);
2938 }
2939
2940 if ((error = zio_checksum_error(zio, &info)) != 0) {
2941 zio->io_error = error;
2942 if (!(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
2943 zfs_ereport_start_checksum(zio->io_spa,
2944 zio->io_vd, zio, zio->io_offset,
2945 zio->io_size, NULL, &info);
2946 }
2947 }
2948
2949 return (ZIO_PIPELINE_CONTINUE);
2950 }
2951
2952 /*
2953 * Called by RAID-Z to ensure we don't compute the checksum twice.
2954 */
2955 void
2956 zio_checksum_verified(zio_t *zio)
2957 {
2958 zio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
2959 }
2960
2961 /*
2962 * ==========================================================================
2963 * Error rank. Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other.
2964 * An error of 0 indicates success. ENXIO indicates whole-device failure,
2965 * which may be transient (e.g. unplugged) or permament. ECKSUM and EIO
2966 * indicate errors that are specific to one I/O, and most likely permanent.
2967 * Any other error is presumed to be worse because we weren't expecting it.
2968 * ==========================================================================
2969 */
2970 int
2971 zio_worst_error(int e1, int e2)
2972 {
2973 static int zio_error_rank[] = { 0, ENXIO, ECKSUM, EIO };
2974 int r1, r2;
2975
2976 for (r1 = 0; r1 < sizeof (zio_error_rank) / sizeof (int); r1++)
2977 if (e1 == zio_error_rank[r1])
2978 break;
2979
2980 for (r2 = 0; r2 < sizeof (zio_error_rank) / sizeof (int); r2++)
2981 if (e2 == zio_error_rank[r2])
2982 break;
2983
2984 return (r1 > r2 ? e1 : e2);
2985 }
2986
2987 /*
2988 * ==========================================================================
2989 * I/O completion
2990 * ==========================================================================
2991 */
2992 static int
2993 zio_ready(zio_t *zio)
2994 {
2995 blkptr_t *bp = zio->io_bp;
2996 zio_t *pio, *pio_next;
2997
2998 if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_READY) ||
2999 zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_READY))
3000 return (ZIO_PIPELINE_STOP);
3001
3002 if (zio->io_ready) {
3003 ASSERT(IO_IS_ALLOCATING(zio));
3004 ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp) ||
3005 (zio->io_flags & ZIO_FLAG_NOPWRITE));
3006 ASSERT(zio->io_children[ZIO_CHILD_GANG][ZIO_WAIT_READY] == 0);
3007
3008 zio->io_ready(zio);
3009 }
3010
3011 if (bp != NULL && bp != &zio->io_bp_copy)
3012 zio->io_bp_copy = *bp;
3013
3014 if (zio->io_error)
3015 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
3016
3017 mutex_enter(&zio->io_lock);
3018 zio->io_state[ZIO_WAIT_READY] = 1;
3019 pio = zio_walk_parents(zio);
3020 mutex_exit(&zio->io_lock);
3021
3022 /*
3023 * As we notify zio's parents, new parents could be added.
3024 * New parents go to the head of zio's io_parent_list, however,
3025 * so we will (correctly) not notify them. The remainder of zio's
3026 * io_parent_list, from 'pio_next' onward, cannot change because
3027 * all parents must wait for us to be done before they can be done.
3028 */
3029 for (; pio != NULL; pio = pio_next) {
3030 pio_next = zio_walk_parents(zio);
3031 zio_notify_parent(pio, zio, ZIO_WAIT_READY);
3032 }
3033
3034 if (zio->io_flags & ZIO_FLAG_NODATA) {
3035 if (BP_IS_GANG(bp)) {
3036 zio->io_flags &= ~ZIO_FLAG_NODATA;
3037 } else {
3038 ASSERT((uintptr_t)zio->io_data < SPA_MAXBLOCKSIZE);
3039 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
3040 }
3041 }
3042
3043 if (zio_injection_enabled &&
3044 zio->io_spa->spa_syncing_txg == zio->io_txg)
3045 zio_handle_ignored_writes(zio);
3046
3047 return (ZIO_PIPELINE_CONTINUE);
3048 }
3049
3050 static int
3051 zio_done(zio_t *zio)
3052 {
3053 zio_t *pio, *pio_next;
3054 int c, w;
3055
3056 /*
3057 * If our children haven't all completed,
3058 * wait for them and then repeat this pipeline stage.
3059 */
3060 if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE) ||
3061 zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE) ||
3062 zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_DONE) ||
3063 zio_wait_for_children(zio, ZIO_CHILD_LOGICAL, ZIO_WAIT_DONE))
3064 return (ZIO_PIPELINE_STOP);
3065
3066 for (c = 0; c < ZIO_CHILD_TYPES; c++)
3067 for (w = 0; w < ZIO_WAIT_TYPES; w++)
3068 ASSERT(zio->io_children[c][w] == 0);
3069
3070 if (zio->io_bp != NULL && !BP_IS_EMBEDDED(zio->io_bp)) {
3071 ASSERT(zio->io_bp->blk_pad[0] == 0);
3072 ASSERT(zio->io_bp->blk_pad[1] == 0);
3073 ASSERT(bcmp(zio->io_bp, &zio->io_bp_copy,
3074 sizeof (blkptr_t)) == 0 ||
3075 (zio->io_bp == zio_unique_parent(zio)->io_bp));
3076 if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(zio->io_bp) &&
3077 zio->io_bp_override == NULL &&
3078 !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) {
3079 ASSERT(!BP_SHOULD_BYTESWAP(zio->io_bp));
3080 ASSERT3U(zio->io_prop.zp_copies, <=,
3081 BP_GET_NDVAS(zio->io_bp));
3082 ASSERT(BP_COUNT_GANG(zio->io_bp) == 0 ||
3083 (BP_COUNT_GANG(zio->io_bp) ==
3084 BP_GET_NDVAS(zio->io_bp)));
3085 }
3086 if (zio->io_flags & ZIO_FLAG_NOPWRITE)
3087 VERIFY(BP_EQUAL(zio->io_bp, &zio->io_bp_orig));
3088 }
3089
3090 /*
3091 * If there were child vdev/gang/ddt errors, they apply to us now.
3092 */
3093 zio_inherit_child_errors(zio, ZIO_CHILD_VDEV);
3094 zio_inherit_child_errors(zio, ZIO_CHILD_GANG);
3095 zio_inherit_child_errors(zio, ZIO_CHILD_DDT);
3096
3097 /*
3098 * If the I/O on the transformed data was successful, generate any
3099 * checksum reports now while we still have the transformed data.
3100 */
3101 if (zio->io_error == 0) {
3102 while (zio->io_cksum_report != NULL) {
3103 zio_cksum_report_t *zcr = zio->io_cksum_report;
3104 uint64_t align = zcr->zcr_align;
3105 uint64_t asize = P2ROUNDUP(zio->io_size, align);
3106 char *abuf = zio->io_data;
3107
3108 if (asize != zio->io_size) {
3109 abuf = zio_buf_alloc(asize);
3110 bcopy(zio->io_data, abuf, zio->io_size);
3111 bzero(abuf+zio->io_size, asize-zio->io_size);
3112 }
3113
3114 zio->io_cksum_report = zcr->zcr_next;
3115 zcr->zcr_next = NULL;
3116 zcr->zcr_finish(zcr, abuf);
3117 zfs_ereport_free_checksum(zcr);
3118
3119 if (asize != zio->io_size)
3120 zio_buf_free(abuf, asize);
3121 }
3122 }
3123
3124 zio_pop_transforms(zio); /* note: may set zio->io_error */
3125
3126 vdev_stat_update(zio, zio->io_size);
3127
3128 /*
3129 * If this I/O is attached to a particular vdev is slow, exceeding
3130 * 30 seconds to complete, post an error described the I/O delay.
3131 * We ignore these errors if the device is currently unavailable.
3132 */
3133 if (zio->io_delay >= MSEC_TO_TICK(zio_delay_max)) {
3134 if (zio->io_vd != NULL && !vdev_is_dead(zio->io_vd))
3135 zfs_ereport_post(FM_EREPORT_ZFS_DELAY, zio->io_spa,
3136 zio->io_vd, zio, 0, 0);
3137 }
3138
3139 if (zio->io_error) {
3140 /*
3141 * If this I/O is attached to a particular vdev,
3142 * generate an error message describing the I/O failure
3143 * at the block level. We ignore these errors if the
3144 * device is currently unavailable.
3145 */
3146 if (zio->io_error != ECKSUM && zio->io_vd != NULL &&
3147 !vdev_is_dead(zio->io_vd))
3148 zfs_ereport_post(FM_EREPORT_ZFS_IO, zio->io_spa,
3149 zio->io_vd, zio, 0, 0);
3150
3151 if ((zio->io_error == EIO || !(zio->io_flags &
3152 (ZIO_FLAG_SPECULATIVE | ZIO_FLAG_DONT_PROPAGATE))) &&
3153 zio == zio->io_logical) {
3154 /*
3155 * For logical I/O requests, tell the SPA to log the
3156 * error and generate a logical data ereport.
3157 */
3158 spa_log_error(zio->io_spa, zio);
3159 zfs_ereport_post(FM_EREPORT_ZFS_DATA, zio->io_spa,
3160 NULL, zio, 0, 0);
3161 }
3162 }
3163
3164 if (zio->io_error && zio == zio->io_logical) {
3165 /*
3166 * Determine whether zio should be reexecuted. This will
3167 * propagate all the way to the root via zio_notify_parent().
3168 */
3169 ASSERT(zio->io_vd == NULL && zio->io_bp != NULL);
3170 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3171
3172 if (IO_IS_ALLOCATING(zio) &&
3173 !(zio->io_flags & ZIO_FLAG_CANFAIL)) {
3174 if (zio->io_error != ENOSPC)
3175 zio->io_reexecute |= ZIO_REEXECUTE_NOW;
3176 else
3177 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
3178 }
3179
3180 if ((zio->io_type == ZIO_TYPE_READ ||
3181 zio->io_type == ZIO_TYPE_FREE) &&
3182 !(zio->io_flags & ZIO_FLAG_SCAN_THREAD) &&
3183 zio->io_error == ENXIO &&
3184 spa_load_state(zio->io_spa) == SPA_LOAD_NONE &&
3185 spa_get_failmode(zio->io_spa) != ZIO_FAILURE_MODE_CONTINUE)
3186 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
3187
3188 if (!(zio->io_flags & ZIO_FLAG_CANFAIL) && !zio->io_reexecute)
3189 zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
3190
3191 /*
3192 * Here is a possibly good place to attempt to do
3193 * either combinatorial reconstruction or error correction
3194 * based on checksums. It also might be a good place
3195 * to send out preliminary ereports before we suspend
3196 * processing.
3197 */
3198 }
3199
3200 /*
3201 * If there were logical child errors, they apply to us now.
3202 * We defer this until now to avoid conflating logical child
3203 * errors with errors that happened to the zio itself when
3204 * updating vdev stats and reporting FMA events above.
3205 */
3206 zio_inherit_child_errors(zio, ZIO_CHILD_LOGICAL);
3207
3208 if ((zio->io_error || zio->io_reexecute) &&
3209 IO_IS_ALLOCATING(zio) && zio->io_gang_leader == zio &&
3210 !(zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)))
3211 zio_dva_unallocate(zio, zio->io_gang_tree, zio->io_bp);
3212
3213 zio_gang_tree_free(&zio->io_gang_tree);
3214
3215 /*
3216 * Godfather I/Os should never suspend.
3217 */
3218 if ((zio->io_flags & ZIO_FLAG_GODFATHER) &&
3219 (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND))
3220 zio->io_reexecute = 0;
3221
3222 if (zio->io_reexecute) {
3223 /*
3224 * This is a logical I/O that wants to reexecute.
3225 *
3226 * Reexecute is top-down. When an i/o fails, if it's not
3227 * the root, it simply notifies its parent and sticks around.
3228 * The parent, seeing that it still has children in zio_done(),
3229 * does the same. This percolates all the way up to the root.
3230 * The root i/o will reexecute or suspend the entire tree.
3231 *
3232 * This approach ensures that zio_reexecute() honors
3233 * all the original i/o dependency relationships, e.g.
3234 * parents not executing until children are ready.
3235 */
3236 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3237
3238 zio->io_gang_leader = NULL;
3239
3240 mutex_enter(&zio->io_lock);
3241 zio->io_state[ZIO_WAIT_DONE] = 1;
3242 mutex_exit(&zio->io_lock);
3243
3244 /*
3245 * "The Godfather" I/O monitors its children but is
3246 * not a true parent to them. It will track them through
3247 * the pipeline but severs its ties whenever they get into
3248 * trouble (e.g. suspended). This allows "The Godfather"
3249 * I/O to return status without blocking.
3250 */
3251 for (pio = zio_walk_parents(zio); pio != NULL; pio = pio_next) {
3252 zio_link_t *zl = zio->io_walk_link;
3253 pio_next = zio_walk_parents(zio);
3254
3255 if ((pio->io_flags & ZIO_FLAG_GODFATHER) &&
3256 (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND)) {
3257 zio_remove_child(pio, zio, zl);
3258 zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
3259 }
3260 }
3261
3262 if ((pio = zio_unique_parent(zio)) != NULL) {
3263 /*
3264 * We're not a root i/o, so there's nothing to do
3265 * but notify our parent. Don't propagate errors
3266 * upward since we haven't permanently failed yet.
3267 */
3268 ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
3269 zio->io_flags |= ZIO_FLAG_DONT_PROPAGATE;
3270 zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
3271 } else if (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND) {
3272 /*
3273 * We'd fail again if we reexecuted now, so suspend
3274 * until conditions improve (e.g. device comes online).
3275 */
3276 zio_suspend(zio->io_spa, zio);
3277 } else {
3278 /*
3279 * Reexecution is potentially a huge amount of work.
3280 * Hand it off to the otherwise-unused claim taskq.
3281 */
3282 ASSERT(taskq_empty_ent(&zio->io_tqent));
3283 spa_taskq_dispatch_ent(zio->io_spa,
3284 ZIO_TYPE_CLAIM, ZIO_TASKQ_ISSUE,
3285 (task_func_t *)zio_reexecute, zio, 0,
3286 &zio->io_tqent);
3287 }
3288 return (ZIO_PIPELINE_STOP);
3289 }
3290
3291 ASSERT(zio->io_child_count == 0);
3292 ASSERT(zio->io_reexecute == 0);
3293 ASSERT(zio->io_error == 0 || (zio->io_flags & ZIO_FLAG_CANFAIL));
3294
3295 /*
3296 * Report any checksum errors, since the I/O is complete.
3297 */
3298 while (zio->io_cksum_report != NULL) {
3299 zio_cksum_report_t *zcr = zio->io_cksum_report;
3300 zio->io_cksum_report = zcr->zcr_next;
3301 zcr->zcr_next = NULL;
3302 zcr->zcr_finish(zcr, NULL);
3303 zfs_ereport_free_checksum(zcr);
3304 }
3305
3306 if (zio->io_flags & ZIO_FLAG_FASTWRITE && zio->io_bp &&
3307 !BP_IS_HOLE(zio->io_bp) && !BP_IS_EMBEDDED(zio->io_bp) &&
3308 !(zio->io_flags & ZIO_FLAG_NOPWRITE)) {
3309 metaslab_fastwrite_unmark(zio->io_spa, zio->io_bp);
3310 }
3311
3312 /*
3313 * It is the responsibility of the done callback to ensure that this
3314 * particular zio is no longer discoverable for adoption, and as
3315 * such, cannot acquire any new parents.
3316 */
3317 if (zio->io_done)
3318 zio->io_done(zio);
3319
3320 mutex_enter(&zio->io_lock);
3321 zio->io_state[ZIO_WAIT_DONE] = 1;
3322 mutex_exit(&zio->io_lock);
3323
3324 for (pio = zio_walk_parents(zio); pio != NULL; pio = pio_next) {
3325 zio_link_t *zl = zio->io_walk_link;
3326 pio_next = zio_walk_parents(zio);
3327 zio_remove_child(pio, zio, zl);
3328 zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
3329 }
3330
3331 if (zio->io_waiter != NULL) {
3332 mutex_enter(&zio->io_lock);
3333 zio->io_executor = NULL;
3334 cv_broadcast(&zio->io_cv);
3335 mutex_exit(&zio->io_lock);
3336 } else {
3337 zio_destroy(zio);
3338 }
3339
3340 return (ZIO_PIPELINE_STOP);
3341 }
3342
3343 /*
3344 * ==========================================================================
3345 * I/O pipeline definition
3346 * ==========================================================================
3347 */
3348 static zio_pipe_stage_t *zio_pipeline[] = {
3349 NULL,
3350 zio_read_bp_init,
3351 zio_free_bp_init,
3352 zio_issue_async,
3353 zio_write_bp_init,
3354 zio_checksum_generate,
3355 zio_nop_write,
3356 zio_ddt_read_start,
3357 zio_ddt_read_done,
3358 zio_ddt_write,
3359 zio_ddt_free,
3360 zio_gang_assemble,
3361 zio_gang_issue,
3362 zio_dva_allocate,
3363 zio_dva_free,
3364 zio_dva_claim,
3365 zio_ready,
3366 zio_vdev_io_start,
3367 zio_vdev_io_done,
3368 zio_vdev_io_assess,
3369 zio_checksum_verify,
3370 zio_done
3371 };
3372
3373 /* dnp is the dnode for zb1->zb_object */
3374 boolean_t
3375 zbookmark_is_before(const dnode_phys_t *dnp, const zbookmark_phys_t *zb1,
3376 const zbookmark_phys_t *zb2)
3377 {
3378 uint64_t zb1nextL0, zb2thisobj;
3379
3380 ASSERT(zb1->zb_objset == zb2->zb_objset);
3381 ASSERT(zb2->zb_level == 0);
3382
3383 /* The objset_phys_t isn't before anything. */
3384 if (dnp == NULL)
3385 return (B_FALSE);
3386
3387 zb1nextL0 = (zb1->zb_blkid + 1) <<
3388 ((zb1->zb_level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT));
3389
3390 zb2thisobj = zb2->zb_object ? zb2->zb_object :
3391 zb2->zb_blkid << (DNODE_BLOCK_SHIFT - DNODE_SHIFT);
3392
3393 if (zb1->zb_object == DMU_META_DNODE_OBJECT) {
3394 uint64_t nextobj = zb1nextL0 *
3395 (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT) >> DNODE_SHIFT;
3396 return (nextobj <= zb2thisobj);
3397 }
3398
3399 if (zb1->zb_object < zb2thisobj)
3400 return (B_TRUE);
3401 if (zb1->zb_object > zb2thisobj)
3402 return (B_FALSE);
3403 if (zb2->zb_object == DMU_META_DNODE_OBJECT)
3404 return (B_FALSE);
3405 return (zb1nextL0 <= zb2->zb_blkid);
3406 }
3407
3408 #if defined(_KERNEL) && defined(HAVE_SPL)
3409 /* Fault injection */
3410 EXPORT_SYMBOL(zio_injection_enabled);
3411 EXPORT_SYMBOL(zio_inject_fault);
3412 EXPORT_SYMBOL(zio_inject_list_next);
3413 EXPORT_SYMBOL(zio_clear_fault);
3414 EXPORT_SYMBOL(zio_handle_fault_injection);
3415 EXPORT_SYMBOL(zio_handle_device_injection);
3416 EXPORT_SYMBOL(zio_handle_label_injection);
3417 EXPORT_SYMBOL(zio_type_name);
3418
3419 module_param(zio_bulk_flags, int, 0644);
3420 MODULE_PARM_DESC(zio_bulk_flags, "Additional flags to pass to bulk buffers");
3421
3422 module_param(zio_delay_max, int, 0644);
3423 MODULE_PARM_DESC(zio_delay_max, "Max zio millisec delay before posting event");
3424
3425 module_param(zio_requeue_io_start_cut_in_line, int, 0644);
3426 MODULE_PARM_DESC(zio_requeue_io_start_cut_in_line, "Prioritize requeued I/O");
3427
3428 module_param(zfs_sync_pass_deferred_free, int, 0644);
3429 MODULE_PARM_DESC(zfs_sync_pass_deferred_free,
3430 "Defer frees starting in this pass");
3431
3432 module_param(zfs_sync_pass_dont_compress, int, 0644);
3433 MODULE_PARM_DESC(zfs_sync_pass_dont_compress,
3434 "Don't compress starting in this pass");
3435
3436 module_param(zfs_sync_pass_rewrite, int, 0644);
3437 MODULE_PARM_DESC(zfs_sync_pass_rewrite,
3438 "Rewrite new bps starting in this pass");
3439 #endif